diff --git a/.claude/agents/architect.md b/.claude/agents/architect.md index 6146a171..014c0a98 100644 --- a/.claude/agents/architect.md +++ b/.claude/agents/architect.md @@ -1,56 +1,25 @@ --- name: architect -description: Pipeline stage 1. Designs an implementation plan for a feature or non-trivial change before any code is written. Writes only plan files. +description: Optional design role. Plans a change before implementation; writes only ignored plan files. tools: Read, Grep, Glob, Bash, Write, Edit -model: opus skills: - placement - - responsibility-map - - port-design - - typed-errors - - wiring - - docs-adr - - plugin-api --- -You design changes for this repository. You do not write or edit code; you -write only the plan directory `.claude/plans//` named in your prompt -(contract: `.claude/skills/feature/plan-bus.md`); a hook enforces that for the -edit tools, and you never write files from Bash. -Apply the preloaded procedure skills: `placement` → `10-placement.md`, -`responsibility-map` → `20-map.md` when a type, module or crate is restructured -or near an early warning, `port-design`/`typed-errors`/`wiring` for the design, -`docs-adr` for the documents decision, and write the plan as `30-design.md`. -Run Cargo only through `.claude/hooks/cargo-serial.sh` (for example -`cargo xtask survey `). +Design the task using AGENTS.md's workflow, architecture and verification policy. +Read the layer table, affected crate cards, relevant owner sections and current +ADRs. Load `responsibility-map`, `port-design`, `typed-errors`, `wiring`, `docs-adr`, +`plugin-api` or `performance` only when the task needs that procedure. -Read CONTRIBUTING.md, docs/ARCHITECTURE.md, the relevant layer notes, -the ADR index and every accepted ADR relevant to the touched crates, and their -`//!` documentation. Then read the relevant code and produce a plan: +Write only the ignored plan location supplied in the task; do not edit code or +write files from Bash. The plan may be one compact document. Cover goal, approach, +affected crates/layers and files, reuse, public surface/errors, risks, tests and +branch/base. For structural changes include invariants, transitions, lifetimes, +late-result handling and resulting counts. Use responsibility-map where the +split is unclear. Early warnings are advisory. +Record any required docs/ADR changes and verify APIs before citing them. -1. **Goal** — one or two sentences, including what is explicitly out of scope. -2. **Approach** — the chosen design and why; name the alternative you rejected and the reason. -3. **Changes** — files to create or modify, in the order they should be done, each with a one-line description. -4. **Tests** — what must be covered and at which level. -5. **Risks** — breaking changes, migrations, performance, anything the reviewer should look at hardest. -6. **Commits** — proposed Conventional Commit headers, the feature branch name - and its target/base (`master` for release-ready work, `dev` for integration). -7. **Placement** — for each new type or function, its crate, layer and reason, - following "Where code goes" in AGENTS.md. -8. **Shape** — types gaining fields and their resulting field counts; extract - responsibilities before adding to a type over 15 fields or crossing the limit. -9. **Public surface and errors** — new public items and ports, explicit dependency - inputs, and the typed error of each new fallible API. Identify any global or - quit-handler changes; new ones violate the design rules. -10. **Reuse** — existing persistence, shutdown, filesystem and fake mechanisms - used or extracted. Verify that an API exists before naming it as available. -11. **Docs/ADR** — documents to update and whether a new ADR is needed. For new - crates, include the short ownership card described in AGENTS.md. - -Prefer the smallest design that solves the stated problem. Use the ownership -examples in AGENTS.md; existing mixed responsibilities are not precedents. -Reject plans that add I/O or protocol types to domain code, extend a broad -workspace trait, grow a type over the field limit, or bypass a check. -If ambiguity changes the design, report the open questions. -Save plans in ignored `.claude/plans/`, never in `docs/`. End with the Result -block from the plan contract. +Prefer the smallest design that solves the problem while keeping architecture +invariants. Identify material unresolved choices; equivalent implementation of +an already authorized plan does not need another approval. Return the plan and +any concrete blocker. Optional handoff format: `.claude/skills/feature/plan-bus.md`. diff --git a/.claude/agents/implementer.md b/.claude/agents/implementer.md index 85a3aea3..942ca40b 100644 --- a/.claude/agents/implementer.md +++ b/.claude/agents/implementer.md @@ -1,38 +1,27 @@ --- name: implementer -description: Pipeline stage 2. Implements an approved plan (or fixes review findings) on a feature branch, with tests. -model: sonnet +description: Optional build role. Implements an authorized change or review fixes with behavior tests. skills: - - owner-extract - - typed-errors - - wiring - - plugin-api - behavior-tests - - gate --- -You implement an approved plan in this repository. Read the plan directory named -in your prompt (`30-design.md` and the files before it) and keep `40-steps.md` -up to date, one transformation per step. Apply the preloaded procedure skills; -run `gate` in quick mode after each step. Run every Cargo command through -`.claude/hooks/cargo-serial.sh`. +Implement the authorized task or plan on its named feature branch. Read the +supplied plan and relevant architecture/ADRs first; follow AGENTS.md's workflow, +reuse, design and verification rules. Keep the diff limited to this task and +preserve unrelated work. +Load `owner-extract`, `typed-errors`, `wiring`, `plugin-api` and `performance` +when relevant; consult `gate` for the applicable checks and completion scope. -- Work on the feature branch named in the plan, based on its recorded target; - never directly on `master` or `dev`. -- Follow the plan. If it turns out to be wrong or incomplete, stop and report what you found instead of improvising a different design. -- Keep naming and formatting consistent while following the ownership rules in - AGENTS.md; do not copy existing architectural debt. -- If placement requires breaking a design rule, stop and report the conflict; - do not work around a check or silently change the approved design. -- Follow the AGENTS.md design rules; pass dependencies through constructors. -- Near 800 lines, extract a responsibility into a type with private state; - moving methods of the same type to another file does not solve the problem. -- Reuse the mechanisms listed in AGENTS.md. Check that shared APIs exist; - report a missing mechanism for planned extraction instead of duplicating it. -- Write behavior tests alongside the code. Before reporting, run the applicable - "Definition of done" commands in AGENTS.md and record each result. -- Keep the diff limited to the task — no drive-by refactors or reformatting. -- Commit only when the user has explicitly requested it and the orchestrator - relays that request; use the Conventional Commit headers from the plan. +Resolve routine implementation details within the authorized design and report +material deviations. Ask only when scope/authority changes or an unresolved +choice requires it. Do not bypass architecture limits or guards. Early warnings +invite inspection rather than automatic extraction. -Report back: what changed (files), what you ran and the results, deviations from the plan, and anything left undone. +Write behavior tests with the code and use the smallest useful check after a +coherent edit. Keep one compact progress/evidence record when needed; detailed +step files are optional. Reuse valid checks and report pending completion checks +under the input-based Definition of done. Do not launch an independent full +pipeline at every handoff. + +Report changed files, commands/results, meaningful deviations and anything left +undone. Commit, push or open a PR only when the user explicitly requests it. diff --git a/.claude/agents/reviewer.md b/.claude/agents/reviewer.md index a8082184..13bf5f4c 100644 --- a/.claude/agents/reviewer.md +++ b/.claude/agents/reviewer.md @@ -1,42 +1,28 @@ --- name: reviewer -description: Pipeline stage 4. Reviews changes against the recorded PR target for correctness, design and convention compliance. Writes only its review file. +description: Optional review role. Reviews changes against the recorded target; writes only ignored review notes. tools: Read, Grep, Glob, Bash, Write -model: opus skills: - design-review - - plugin-api - - gate --- -You review changes against the target recorded in the plan (`master` or `dev`). -You do not edit project files; you write only `60-review.md` in the plan -directory named in your prompt (never from Bash), using the preloaded -`design-review` table. -Read the plan files and the touched layer notes and relevant ADRs. - -Use `git diff ...HEAD` for committed changes; Git compares HEAD to its -merge base with the target. Also inspect `git diff HEAD` and relevant untracked -files, because commit authorization may follow review. Never substitute `master` -for an integration target: that includes unrelated work. If no target is supplied, -resolve it from the PR or branch plan before reviewing. - -Read enough surrounding code to judge each change in context. Check, in this order: - -1. **Correctness** — logic errors, unhandled cases, races, resource leaks, broken error handling. -2. **Plan conformance** — does it do what the plan said, and only that? -3. **Tests** — do they actually assert the behaviour, or just execute it? -4. **Design** — unnecessary complexity, duplication of existing code, leaky abstractions. -5. **Conventions** — commit headers and branch name per CONTRIBUTING.md; breaking changes marked with `!` and a `BREAKING CHANGE:` footer; no manual edits to release files. - -Every rule in the AGENTS.md sections "Design rules", "Reuse before writing", "Tests" -and "Documents" is blocking. Existing debt does not justify growth. Also block: -methods moved to another file without extracting an owner; a broad workspace trait -grown instead of a narrow consumer port; a future API described as available; -vendor and application changes mixed in one change. - -Run `.claude/hooks/cargo-serial.sh cargo xtask architecture` and report the `xtask/baseline.toml` delta; reject increases. - -Report only findings you have verified in the code. For each: `file:line`, severity (`blocking` / `should-fix` / `nit`), a concrete failure scenario, and a suggested fix. Rank most severe first. - -End with a verdict: **approve** (no blocking findings) or **changes requested**. +Review against the task's recorded branch/base and AGENTS.md's rules. Read the +plan, relevant layer notes/ADRs and enough surrounding code to assess behavior. +Use `git diff ...HEAD` for committed changes, `git diff HEAD` for staged +and unstaged changes, and relevant untracked files. Resolve an absent target +from the task/PR before reviewing; do not silently substitute master. +Load `plugin-api` for contribution/guest contracts and `performance` for hot +paths; consult `gate` to assess missing or stale verification evidence. + +Write only the supplied review location under `.claude/plans/`; never edit +project files or write files from Bash. A section in a compact plan is enough. +Apply design-review to correctness, tests, responsibility, public surface and +conventions. Design/release/debt invariants remain blocking. Vendor and app +changes may share a PR when separate commits and the prescribed merge method +preserve their separation (CONTRIBUTING.md). + +Read existing verification evidence; request only missing, stale or suspect +checks. A review pass does not turn pending checks into a pass. Report verified +findings with file:line, severity, concrete consequence and smallest fix. Return +approve or changes requested; actionable findings continue to implementation +under AGENTS.md, without an arbitrary round-count stop. diff --git a/.claude/agents/tester.md b/.claude/agents/tester.md index 8762dc73..c8d82292 100644 --- a/.claude/agents/tester.md +++ b/.claude/agents/tester.md @@ -1,35 +1,26 @@ --- name: tester -description: Pipeline stage 3. Verifies an implementation - runs build, lint and tests, exercises the change, and adds missing test coverage. -model: sonnet +description: Optional verification role. Runs checks selected by changed inputs and adds missing behavior coverage. skills: - behavior-tests - - plugin-api - - gate --- -You verify a change in this repository. You may add or edit tests; you do not change production code. -Run `gate` in full mode (through `.claude/hooks/cargo-serial.sh`) and write -`50-gate.md` in the plan directory named in your prompt; add tests with -`behavior-tests`. +Verify the supplied change under AGENTS.md's input-based completion policy. +Read the task, existing evidence and affected source/configuration first. You +may add or edit tests; do not change production code. Report a defect with a +failing behavior test where practical and send production fixes back to the +implementer. +The file guard permits dedicated test files; send inline-test edits inside +production files to the implementer too. This is a role handoff, not a request +for new user approval. Load `plugin-api`/`performance` when applicable and use +`gate` for completion checks. -1. Run the applicable "Definition of done" commands from AGENTS.md, including - architecture and generated-doc checks. Record exact commands and results; - for documentation-only work, use its documented verification scope. -2. Compare the tests against the plan's test strategy. Add tests for uncovered behaviour, edge cases and error paths. -3. Exercise the change the way a user would, not just through unit tests, when that is possible. -4. Try to break it: empty input, boundaries, invalid state, concurrency where relevant. +Use the smallest affected behavior target and consumers; run broad verification +only when its impact or the assigned completion scope requires it. Reuse valid +PASS results. Add coverage for relevant boundaries, refusals, errors and races; +exercise user actions when possible. Follow behavior-tests and shared port-fake +rules rather than inspecting private model/view state. -- Name tests and files after behavior. Extend the existing file for that - behavior; create one only when the behavior has no test file. -- Drive actions, events, rendering and public methods. Do not inspect or mutate - private view/model fields to prove the fix. -- Reuse session `test-support` through dev-dependencies and extend its existing - port fakes. If a missing capability has no shared home, propose its extraction - rather than inventing a duplicate fake. -- A regression test reproduces the observable failure, not the internal state - that happened to cause it. - -If you find a defect, write a failing test that reproduces it and report it — do not fix production code yourself. - -Report back: commands run with pass/fail, tests added, defects found (with reproduction), and what you could not verify. +Record commands, scope/source/configuration, results and pending checks once in +the supplied compact plan or evidence location. Report added tests, reproduced +defects and checks that could not run. A stage handoff alone requires no full gate. diff --git a/.claude/hooks/cargo-serial.sh b/.claude/hooks/cargo-serial.sh index 586a9878..d8d59dab 100755 --- a/.claude/hooks/cargo-serial.sh +++ b/.claude/hooks/cargo-serial.sh @@ -1,17 +1,31 @@ #!/usr/bin/env bash -# Runs one Cargo command with CARGO_BUILD_JOBS=2 under one lock shared by every -# agent of this user on the machine (AGENTS.md, Hard rules). The lock is held -# by this script only: the command and anything it leaves running in the -# background do not inherit it. +# Optional command coordination; see CONTRIBUTING.md, Cargo resources. +# Caller CARGO_BUILD_JOBS and Cargo configuration are preserved. +# Default: linked worktrees share a lock in their Git common directory. +# NOCTERM_CARGO_LOCK selects another path, or "off" disables the lock. # Usage: .claude/hooks/cargo-serial.sh cargo test -p nocterm-ssh -# NOCTERM_CARGO_LOCK overrides the lock path (tests only). +# Only the wrapper holds the lock; children/background jobs do not inherit it. set -uo pipefail if [ "$#" -eq 0 ]; then echo "usage: $0 cargo " >&2 exit 64 fi -export CARGO_BUILD_JOBS=2 -lock="${NOCTERM_CARGO_LOCK:-/tmp/nocterm-cargo-$(id -u).lock}" +lock="${NOCTERM_CARGO_LOCK:-}" +if [ "$lock" = off ]; then + exec "$@" +fi +if ! command -v flock >/dev/null 2>&1; then + echo "cargo-serial: locking requires flock; install it, run Cargo directly, or set NOCTERM_CARGO_LOCK=off" >&2 + exit 69 +fi +if [ -z "$lock" ]; then + common=$(git rev-parse --git-common-dir) || { + echo "cargo-serial: outside a Git repository; set NOCTERM_CARGO_LOCK to a path or off" >&2 + exit 64 + } + common=$(CDPATH= cd -- "$common" && pwd -P) || exit 75 + lock="$common/nocterm-cargo.lock" +fi waiting() { echo "cargo-serial: waiting for another Cargo command to finish" >&2; } # Runs the command as a child that a signal to this script reaches too, and # returns its status. A background job of a non-interactive shell starts with @@ -28,16 +42,6 @@ run() { done return "$status" } -if command -v flock >/dev/null 2>&1; then - exec 9>>"$lock" || exit 75 - flock -n 9 || { waiting; flock 9 || exit 75; } - run "$@" - exit $? -fi -# Portable fallback without flock: an atomic directory, removed on exit. -until mkdir "$lock.d" 2>/dev/null; do - [ -n "${announced:-}" ] || { waiting; announced=1; } - sleep 2 -done -trap 'rmdir "$lock.d"' EXIT +exec 9>>"$lock" || exit 75 +flock -n 9 || { waiting; flock 9 || exit 75; } run "$@" diff --git a/.claude/hooks/guard_bash.py b/.claude/hooks/guard_bash.py index 1af80242..632b3ff0 100755 --- a/.claude/hooks/guard_bash.py +++ b/.claude/hooks/guard_bash.py @@ -6,8 +6,6 @@ - `master` and `dev` change only through pull requests: no commits, merges, cherry-picks, reverts or patches on them, and no pushes to them. - Tags are created by release automation only. -- Compiling Cargo commands run through `cargo-serial.sh`, which applies - `CARGO_BUILD_JOBS=2` and one lock shared by every agent of the user. - Release files and debt inventories are changed with Edit/Write, where `guard_files.py` can check them, not with shell redirection. @@ -25,12 +23,6 @@ import hooklib EVENT = "PreToolUse" -WRAPPER = "cargo-serial.sh" -COMPILING = { - "build", "b", "check", "c", "clippy", "test", "t", "run", "r", "xtask", "doc", "d", - "bench", "nextest", "llvm-cov", "install", "rustc", "fix", -} -CARGO_OPTIONS_WITH_VALUE = {"--config", "-Z", "-C", "--color"} PROTECTED = { "CHANGELOG.md", "version.txt", ".release-please-manifest.json", "xtask/baseline.toml", "xtask/oversized-files.toml", @@ -448,27 +440,6 @@ def check_git(state, assignments, words): hooklib.deny(EVENT, f"{', '.join(sorted(reached))} change only through pull requests; push the feature branch instead (CONTRIBUTING.md, Branching).") -def check_cargo(state, assignments, words): - if cargo_subcommand(words[1:]) in COMPILING: - wrapper = hooklib.project_dir(state.payload) / ".claude" / "hooks" / WRAPPER - suggestion = " ".join([*map(shlex.quote, assignments), shlex.quote(str(wrapper)), shlex.join(words)]) - hooklib.deny( - EVENT, - "Compiling Cargo commands run through the shared lock with CARGO_BUILD_JOBS=2 " - f"(AGENTS.md, Hard rules). Run it as (keeping any pipes and redirections):\n{suggestion}", - ) - - -def cargo_subcommand(arguments): - while arguments: - argument, arguments = arguments[0], arguments[1:] - if argument in CARGO_OPTIONS_WITH_VALUE: - arguments = arguments[1:] - elif not argument.startswith(("-", "+")): - return argument - return None - - def writes_protected(state, words, targets): """Whether a command rewrites a protected file in place.""" if any(state.protected(target) for target in targets): @@ -509,8 +480,6 @@ def check(state, command, depth=0): check_git(state, assignments, words) elif name == "eval": check(state, " ".join(words[1:]), depth + 1) - elif name == "cargo": - check_cargo(state, assignments, words) elif name in SHELLS and shell_script(words): check(state, shell_script(words), depth + 1) diff --git a/.claude/hooks/guard_files.py b/.claude/hooks/guard_files.py index ac44b0e8..a2ce563d 100755 --- a/.claude/hooks/guard_files.py +++ b/.claude/hooks/guard_files.py @@ -4,11 +4,11 @@ - Release files are owned by release automation and are never edited. - Debt inventories (`xtask/baseline.toml`, `xtask/oversized-files.toml`) may only shrink: new entries and raised counts are denied. -- DTO exemptions in `xtask/architecture-policy.toml` need design review, so - adding one asks the user. -- Pipeline stages keep to their role (AGENTS.md, Pipeline): `architect` and +- DTO additions and other architecture policy changes need their own design + review; isolation changes include arrays and removed restrictions. +- Delegated agents keep to their explicit role (AGENTS.md, Workflow): `architect` and `reviewer` write only their plan files; `tester` edits tests, and editing - anything else asks the user. + anything else is handed back to the implementer. """ import sys import tomllib @@ -38,9 +38,14 @@ def is_test_file(path): def check_role(agent, path): """Stage-specific write limits; `path` is None outside any checkout.""" if agent in PLAN_ONLY and not (path or "").startswith(PLANS): - hooklib.deny(EVENT, f"The {agent} stage does not write code; write its output under {PLANS}/ (AGENTS.md, Pipeline).") + hooklib.deny(EVENT, f"The {agent} stage does not write code; write its output under {PLANS}/ (AGENTS.md, Workflow; .claude/agents/).") if agent == "tester" and path and not path.startswith(PLANS) and not is_test_file(path): - hooklib.ask(EVENT, f"The tester stage changes tests only; {path} looks like production code (AGENTS.md, Pipeline).") + hooklib.deny( + EVENT, + f"The tester stage does not edit production files: {path}. Send the proposed change " + "to the implementer, including changes confined to an inline test module. " + "This is a role handoff, not a request for user approval (.claude/agents/tester.md).", + ) def flatten(document, prefix=""): @@ -67,6 +72,33 @@ def growth(before, after): return grown +def policy_review(before, after): + """DTO removals shrink exemptions; all other policy changes need review. + + Isolation lists may be allowlists or denylists, so removals cannot safely + be treated as reductions of authority. Compare parsed values, not TOML text. + """ + reasons = [] + old_dto, new_dto = before.get("dto", {}), after.get("dto", {}) + if old_dto != new_dto: + if not isinstance(old_dto, dict) or not isinstance(new_dto, dict): + reasons.append("Changing the plain-data exemption structure needs design review") + else: + changed = [key for key, value in new_dto.items() if old_dto.get(key) != value] + if changed: + reasons.append("Adding or changing plain-data exemptions needs design review: " + ", ".join(sorted(changed))) + sections = sorted( + key for key in before.keys() | after.keys() + if key != "dto" and before.get(key) != after.get(key) + ) + if sections: + reasons.append( + "Changing architecture policy sections needs design review, including isolation " + "permissions and restrictions: " + ", ".join(sections) + ) + return reasons + + def parse(text): try: return tomllib.loads(text) @@ -97,11 +129,14 @@ def main(): hooklib.deny(EVENT, f"{path} would no longer be valid TOML.") if before is None: return + if path == POLICY: + reasons = policy_review(before, after) + if reasons: + hooklib.ask(EVENT, "; ".join(reasons) + " (docs/architecture/guardrails.md).") + return grown = growth(before, after) if not grown: return - if path == POLICY: - hooklib.ask(EVENT, "Adding a plain-data exemption needs design review (docs/architecture/guardrails.md): " + "; ".join(grown)) hooklib.deny( EVENT, f"{path} is an inventory that may only shrink (ADR 0012, AGENTS.md design rule 6). " diff --git a/.claude/hooks/hooklib.py b/.claude/hooks/hooklib.py index 49f05178..74ab0dd1 100755 --- a/.claude/hooks/hooklib.py +++ b/.claude/hooks/hooklib.py @@ -4,7 +4,6 @@ deterministic guards for rules in AGENTS.md; CI remains the authoritative gate. """ import json -import os import sys from pathlib import Path @@ -13,10 +12,6 @@ def event(): return json.load(sys.stdin) -def project_dir(payload): - return Path(os.environ.get("CLAUDE_PROJECT_DIR") or payload.get("cwd") or ".").resolve() - - def absolute(payload, path): path = Path(path) if not path.is_absolute(): @@ -63,6 +58,33 @@ def resulting_text(payload): return text +def applied_edits(payload): + """Exact fragments of a successful Edit/MultiEdit; Write has no old text.""" + data = payload.get("tool_input") or {} + if payload.get("tool_name") == "Edit": + return [data] + if payload.get("tool_name") == "MultiEdit": + return data.get("edits") + return None + + +def previous_text(payload, text): + """Undo only unambiguous replacements to recover a post-edit predecessor. + + Repeated replacement text, deletions and replace_all are ambiguous without + a pre-edit snapshot. Leave them unknown rather than guessing the edit site. + """ + edits = applied_edits(payload) + if not edits: + return None + for edit in reversed(edits): + old, new = edit.get("old_string"), edit.get("new_string") + if old is None or not new or edit.get("replace_all") or text.count(new) != 1: + return None + text = text.replace(new, old, 1) + return text + + def deny(event_name, reason): print(json.dumps({ "hookSpecificOutput": { diff --git a/.claude/hooks/post_edit.py b/.claude/hooks/post_edit.py index bb509d0b..01a84a28 100755 --- a/.claude/hooks/post_edit.py +++ b/.claude/hooks/post_edit.py @@ -3,12 +3,13 @@ Adds context for the agent; it never blocks. The signals mirror the early warnings of `cargo xtask architecture` and the design-review skill: -- a Rust file at or above 640 lines needs a split designed by responsibility; +- crossing 640 lines or another 40-line band invites responsibility inspection; - a new Rust file holding only `impl` blocks moves methods without an owner; - a catch-all module name hides a responsibility; - a new crate manifest starts the crate-new workflow; - a plugin declaration, the plugin model or the plugin catalog follows the plugin-api skill. """ +import difflib import re import sys from pathlib import Path @@ -18,22 +19,84 @@ EVENT = "PostToolUse" WARN_LINES = 640 LIMIT = 800 +WARN_STEP = 40 ROOTS = ("src/", "crates/", "xtask/") GENERIC = {"utils", "util", "helpers", "helper", "misc", "common", "stuff", "extra", "ext"} DEFINITION = re.compile(r"^\s*(?:pub(?:\([^)]*\))?\s+)?(?:struct|enum|union|trait|type)\s+\w", re.MULTILINE) -PLUGIN_PATHS = re.compile(r"(?:^|/)plugin\.rs$|^crates/plugin-model/|^src/shell_setup(?:\.rs$|/)") +PLUGIN_CONTRACT = re.compile(r"(?:^|/)plugin\.rs$|^crates/plugin-(?:model|api|abi|ports)/") +PLUGIN_REGISTRATION = re.compile( + r"\b(?:PLUGIN|PLUGINS|CORE|PluginCatalog|SystemPlugin|SystemContribution|" + r"register_panel|register_widget|nocterm_plugin_api)\b|\bplugin::catalog\b" +) IMPL = re.compile(r"^\s*(?:unsafe\s+)?impl\b", re.MULTILINE) -def signals(path, text, created): +def line_count(text): + return text.count("\n") + (0 if text.endswith("\n") or not text else 1) + + +def size_warning(text, payload): + lines = line_count(text) + if lines > LIMIT: + return True + if lines < WARN_LINES: + return False + before = hooklib.previous_text(payload, text) + if before is not None: + return lines // WARN_STEP > line_count(before) // WARN_STEP + edits = hooklib.applied_edits(payload) + if edits and all(not edit.get("replace_all") for edit in edits): + # A deletion or ambiguous replacement can still prove there was no + # line growth. At most one trailing unterminated line can be added. + delta = sum( + edit.get("new_string", "").count("\n") - edit.get("old_string", "").count("\n") + for edit in edits + ) + same_endings = all( + edit.get("old_string") and edit.get("new_string") + and edit["old_string"].endswith("\n") == edit["new_string"].endswith("\n") + for edit in edits + ) + if delta < 0 or (delta == 0 and same_endings): + return False + # Write supplies no predecessor. Unknown edits remain conservative; no + # persistent session cache is needed to suppress known unchanged/shrinking edits. + return True + + +def changed_code(payload, text): + edits = hooklib.applied_edits(payload) + lines = [] + if edits is None: + lines = text.splitlines() + else: + for edit in edits: + before = edit.get("old_string", "").splitlines() + after = edit.get("new_string", "").splitlines() + for kind, start, end, new_start, new_end in difflib.SequenceMatcher(None, before, after).get_opcodes(): + if kind != "equal": + lines.extend(before[start:end]) + lines.extend(after[new_start:new_end]) + # This is a routing hint, not a Rust parser or a replacement for review. + return "\n".join(line for line in lines if line.strip() and not line.lstrip().startswith("//")) + + +def signals(path, text, created, payload): found = [] if path.endswith(".rs") and path.startswith(ROOTS): - lines = text.count("\n") + (0 if text.endswith("\n") or not text else 1) - if lines >= WARN_LINES: + lines = line_count(text) + if size_warning(text, payload): + status = "Hard limit exceeded" if lines > LIMIT else "Advisory size warning" + instruction = ( + f"Reduce the file to at most {LIMIT} lines; split production code by responsibility. " + if lines > LIMIT else + "Inspect its responsibility and remaining room; use responsibility-map if separation is unclear. " + "A warning alone does not require extraction. " + ) found.append( - f"{path} has {lines} lines (warn at {WARN_LINES}, limit {LIMIT}). Before it grows, run the " - "responsibility-map skill on its main type and extract an owner with private state; " - "do not cut the file at the limit or move methods of the same type to another file." + f"{status}: {path} has {lines} lines (advisory at {WARN_LINES}, hard limit {LIMIT}). " + f"{instruction}If a split is needed, extract an owner, " + "rather than moving methods of the same type to another file." ) stem = Path(path).stem if created and stem in GENERIC: @@ -46,12 +109,13 @@ def signals(path, text, created): f"{path} only implements types declared elsewhere. Moving methods of a parent type to a new " "file is file slicing, not an owner extraction: use the owner-extract skill (AGENTS.md design rules 1 and 7)." ) - if path.endswith(".rs") and path.startswith(ROOTS) and PLUGIN_PATHS.search(path): - found.append( - f"{path} declares, models or composes plugins. Follow the plugin-api skill: system plugins are " - "`pub static PLUGIN`, core owners declare `CORE`, registration goes through the workspace " - "contract, and nothing is exposed to user plugins by a getter or a `pub` item." - ) + changed = changed_code(payload, text) + if changed and (PLUGIN_CONTRACT.search(path) or PLUGIN_REGISTRATION.search(changed)): + found.append( + f"{path} declares, models or composes plugins. Follow the plugin-api skill: system plugins are " + "`pub static PLUGIN`, core owners declare `CORE`, registration goes through the workspace " + "contract, and nothing is exposed to user plugins by a getter or a `pub` item." + ) if created and re.fullmatch(r"crates/[^/]+/Cargo\.toml", path): found.append( f"{path} starts a new crate. Follow the crate-new skill: placement, ownership card, layer metadata, " @@ -72,7 +136,7 @@ def main(): text = hooklib.absolute(payload, data.get("file_path")).read_text(encoding="utf-8") except (FileNotFoundError, UnicodeDecodeError): return - found = signals(path, text, created) + found = signals(path, text, created, payload) if found: hooklib.context(EVENT, "\n".join(found)) diff --git a/.claude/skills/behavior-tests/SKILL.md b/.claude/skills/behavior-tests/SKILL.md index fd367807..da9dc38f 100644 --- a/.claude/skills/behavior-tests/SKILL.md +++ b/.claude/skills/behavior-tests/SKILL.md @@ -13,8 +13,9 @@ and so do files named after review rounds. - **characterize** (before a refactor): cover every behaviour that passes through the code about to move, run the tests on the **unchanged** code and - see them pass. Record the test names in `40-steps.md`; they must stay - unedited through the refactor. + see them pass. Record relevant tests in the compact plan. Preserve scenarios + and assertions through the refactor; mechanical import, location and fixture + adaptations are allowed. Explain any change to expected behavior separately. - **cover** (new or changed behaviour): one test per rule or outcome, including refusals and errors (`typed-errors` variants), written red-green where practical. @@ -41,14 +42,16 @@ and so do files named after review rounds. `gpui_kit` instead of adding another copy. - Assert outcomes the user or caller sees, not call sequences: the rendered state, the event, the persisted document, the returned error variant. + When reducing external work is itself the requirement, counters on shared + ports may assert requests/bytes avoided; use `performance` for cost evidence. ## Run -Through the shared lock, narrowest first: +Choose the narrowest useful check; Cargo resources follow CONTRIBUTING.md: ```sh -.claude/hooks/cargo-serial.sh cargo test -p +cargo test -p ``` -End with the Result block listing tests added and their pass state on the -unchanged code (characterize) or after the change (cover). +Report tests added and their pass state on unchanged code (characterize) +or after the change (cover), in the shared plan or handoff. diff --git a/.claude/skills/crate-extend/SKILL.md b/.claude/skills/crate-extend/SKILL.md index b8c1a67d..5c9e57cf 100644 --- a/.claude/skills/crate-extend/SKILL.md +++ b/.claude/skills/crate-extend/SKILL.md @@ -1,51 +1,28 @@ --- name: crate-extend -description: Workflow for adding behaviour to an existing crate — confirm it fits the crate's card, check the touched files and types against early warnings, make room with a refactor first if needed, then build, verify and review. Use for most features and non-trivial changes inside one crate. +description: Add behavior to an existing crate, checking ownership, affected APIs and tests; refactor only where the design or hard limits require it. argument-hint: " [crate] [target: master|dev]" --- -# Workflow: extend a crate +# Extend a crate Task: $ARGUMENTS -You orchestrate; pass the plan directory, target branch and verification scope -to every subagent (`.claude/skills/feature/plan-bus.md`). - -## 0. Task - -Write `00-task.md`: change, target, branch `/`, verification scope, -success metric (the behaviour, plus "no new early warnings for touched files" -or the specific counts if a split is needed). - -## 1. Design (architect) - -1. `placement` → `10-placement.md`. If the change belongs to another crate or a - new crate, switch to that workflow; if it spans layers, split it per layer. -2. Survey the target: `.claude/hooks/cargo-serial.sh cargo xtask survey `. - For each file or type the change will grow, check its early warning: - - a type that would exceed 12 fields, a file that would exceed 640 lines or a - type in 4 impl files → run `responsibility-map` and plan the extraction - **before** the feature (it becomes the first steps, or a separate - `refactor` run on its own branch if large); - - under the thresholds → continue. -3. `port-design` for any new collaborator; `typed-errors` for new fallible APIs; - `wiring` for new settings, persistence or installation changes. -4. `docs-adr` decision (usually: card update only). -5. `30-design.md` including expected field and line counts after the change. - -Show `30-design.md` to the user and wait for approval. - -## 2. Build (implementer) - -Make room first (`behavior-tests` characterize → `owner-extract`), then the -change with `behavior-tests` cover. `gate` quick after each step; `40-steps.md`. - -## 3. Verify (tester) — `gate` full; add missing tests. - -## 4. Review (reviewer) — `design-review` → `60-review.md`; two-round limit. - -## 5. Wrap up - -Report gate results, verdict, baseline delta, early warnings before/after, and -proposed commits (refactor commits separate from the feature commit). Commit -only if the user asked. +Follow AGENTS.md's Workflow and Definition of done. One agent and one compact +plan are sufficient for scoped work; use [the handoff contract](../feature/plan-bus.md) +when delegating. Record behavior, target/base, branch, affected crate/layer and checks. + +1. Use `placement` to confirm the behavior fits the crate's ownership card and + allowed dependencies. Separate responsibilities across layers where needed. +2. Inspect the files/types that will grow. Use `cargo xtask survey ` + and `responsibility-map` when ownership is unclear. Early warnings are + advisory: explain consequential growth, but extract only where responsibility + or hard limits require it. Do not require a no-new-warnings metric. +3. Design new collaborators with `port-design`, fallible APIs with `typed-errors`, + and installation/persistence changes with `wiring`; apply `docs-adr` as needed. +4. Implement with `behavior-tests`. If extraction is needed, characterize behavior + first and use `owner-extract`; keep a cohesive fix on one branch where useful. +5. Use `gate` for affected behavior and consumers. Review with `design-review`, + fix findings, then finish the applicable input-based completion checks. +6. Report behavior, checks/results, relevant boundary/count changes and remaining + limitations. Commit/push/PR only when requested. diff --git a/.claude/skills/crate-new/SKILL.md b/.claude/skills/crate-new/SKILL.md index f7f720aa..10f23824 100644 --- a/.claude/skills/crate-new/SKILL.md +++ b/.claude/skills/crate-new/SKILL.md @@ -1,72 +1,30 @@ --- name: crate-new -description: Workflow for adding a new workspace crate — placement proves a new crate is needed, the design fixes its card, ports, errors and wiring, then build, verify and review. Use when a responsibility has no fitting crate or the user asks for a new crate. +description: Add a workspace crate when placement establishes a distinct responsibility, with ownership card, allowed dependencies, typed ports and behavior coverage. argument-hint: " [target: master|dev]" --- -# Workflow: new crate +# New crate Task: $ARGUMENTS -You orchestrate; procedure skills do the work and write the plan directory -(`.claude/skills/feature/plan-bus.md`). Pass the plan directory path, the -target branch and the verification scope to every subagent. - -## 0. Task - -Create `.claude/plans//00-task.md`: responsibility, target (`master` -unless the user says `dev`), branch `feat/` or `refactor/`, -verification scope (full gate), success metric (crate exists with a card, -layer and tests; the code it replaces, if any, is gone from the old crate). - -## 1. Design (architect) - -Spawn `architect` to run, in order: - -1. `placement` → `10-placement.md`. It must conclude **new crate** with at least - two criteria met. If it concludes "module of an existing crate", stop and - switch to `crate-extend`. -2. `port-design` for every capability the crate uses or provides. -3. `docs-adr` decides the documents: an ADR when the crate takes a - responsibility from another crate or adds a port between layers; otherwise - its card and layer-note entry. -4. `30-design.md` with: crate name `nocterm-` in `crates/`, layer, - draft card, public surface, error type, dependencies (checked against the - layer table), wiring step, tests per layer, ADR, commit plan. - -Show `30-design.md` to the user and wait for approval (AGENTS.md gate). - -## 2. Build (implementer) - -Skeleton, in one step: - -- `crates//Cargo.toml` like `crates/containers/Cargo.toml`: `edition`, - `license-file` and `lints` from the workspace, `publish = false`, a - `description`, and `[package.metadata.nocterm] layer = ""`. -- An entry `nocterm- = { path = "crates/" }` under - `[workspace.dependencies]` in the root `Cargo.toml` (`crates/*` are members). -- `crates//src/lib.rs` starting with the card (`Owns:`, `Does not own:`, `Ports`, - `Settings`, `Globals`, `Invariants`), modules named for what they own, - `error.rs` (`typed-errors`). -- `test-support` fakes behind a feature, used only from dev-dependencies. - -Then each behaviour with `behavior-tests` (cover), `wiring` if the crate is -installed by the app, and `gate` quick after each step. Run `cargo xtask docs` -(dependency graph changes) and record steps in `40-steps.md`. - -## 3. Verify (tester) - -`gate` full, `behavior-tests` cover for gaps, plus `cargo machete ---skip-target-dir` and the license-notice check (new manifest). If notices are -stale, regenerate them (`python3 scripts/generate-license-notices.py`). - -## 4. Review (reviewer) - -`design-review` → `60-review.md`. Blocking findings go back to the -implementer; after two failed rounds escalate. - -## 5. Wrap up - -Report each gate command with its result, the review verdict, the baseline -delta, and proposed commits (`feat(): …` / `refactor(): …`, ADR as -`docs: …`). Commit only if the user asked. +Follow AGENTS.md's Workflow and Definition of done. Record responsibility, +branch/base, layer, public surface, dependencies, risks and check scope in a +compact plan; [structured handoffs](../feature/plan-bus.md) are optional. + +1. `placement` must justify a new crate with its criteria; otherwise use a module + and `crate-extend`. Use `port-design`/`typed-errors` for collaborators and + fallible APIs, `wiring` for installation, and `docs-adr` for the boundary decision. +2. Add `crates//Cargo.toml` like `crates/containers/Cargo.toml`: inherited + edition/license/lints, `publish = false`, description and + `[package.metadata.nocterm] layer = ""`. Add its path dependency under + root `[workspace.dependencies]`; `crates/*` supplies workspace membership. +3. Start `lib.rs` with the ownership card (Owns, Does not own, Ports, Settings, + Globals, Invariants). Name modules after responsibilities. Add typed errors + for fallible APIs and feature-gated test-support fakes where needed. +4. Implement behavior with `behavior-tests` and explicit dependencies. Apply + scoped `gate` checks after coherent edits; regenerate docs once dependency + changes settle. Include dependency/notice checks for the new manifest. +5. Review with `design-review`, fix findings and finish the applicable input-based + completion checks. Report responsibility, boundary changes, evidence and + limitations. Commit/push/PR only when requested. diff --git a/.claude/skills/crate-split/SKILL.md b/.claude/skills/crate-split/SKILL.md index e65d784c..8d5ba09c 100644 --- a/.claude/skills/crate-split/SKILL.md +++ b/.claude/skills/crate-split/SKILL.md @@ -1,78 +1,32 @@ --- name: crate-split -description: Workflow for moving a module or responsibility out of a crate into a new or existing crate — survey module references, decide the target layer per piece, escalate layer conflicts, move behind characterization tests, update cards and record an ADR. Use when a crate owns several subjects or a module's dependencies belong to another layer. +description: Move a responsibility between crates, checking layer dependencies, narrow contracts, characterization coverage and an ADR for the new boundary. argument-hint: "[::] [target: master|dev]" --- -# Workflow: split a crate +# Split a crate Subject: $ARGUMENTS -Crate boundaries change, so this workflow always ends with an ADR. Pass the -plan directory, target branch and verification scope to every subagent -(`.claude/skills/feature/plan-bus.md`). - -## 0. Task - -`00-task.md`: subject, target, branch `refactor/`, metric (for example -"`nocterm-agent` lines −N; new crate `nocterm-` owns ``; no -`crate::` references left in the old crate"). - -## 1. Survey and placement (architect) - -1. `responsibility-map` on the crate. Use the `modules` section: a candidate - module has most references to one or two workspace crates and few to its - siblings (`crate::…`). -2. For each candidate run `placement`, using the references as the dependency - list. Every dependency must be allowed by the candidate's layer. - -### Layer conflicts: stop and escalate - -When a candidate's dependencies span layers, it is not one responsibility yet. -Example from `nocterm-agent` (feature): its `thread` module references -`nocterm_ai` (domain) 162 times and `nocterm_workspace` (ui) 62 times, and -holds GPUI state with background work. As a service crate it could not depend -on `nocterm-workspace`; as a feature crate it would be a second agent feature -that the first must not depend on. Escalate with the options instead of -choosing: - -- split the module first: the domain part (transcript rules, `nocterm_ai` - types) moves down; the workspace-facing part stays in the feature; -- move the narrow contract it needs from `nocterm-workspace` into a lower - crate (needs its own ADR); -- keep the module and run `refactor` inside the crate instead. - -Write the conflict and options to `10-placement.md` and return `escalate`. - -3. `port-design` for each reference that crosses the new boundary in both - directions; reject a cycle (the old crate and the new one must not depend on - each other through the same API). -4. `docs-adr`: draft the ADR (context, decision, alternatives) in - `30-design.md`; it is written to `docs/adr/` in the build stage. -5. `30-design.md`: target crate (new via the `crate-new` skeleton, or existing), - files moved, public surface, ports, wiring change, step order, commits. - -Show the design to the user and wait for approval. - -## 2. Move (implementer) - -1. `behavior-tests` characterize through the old crate's public API. -2. Create the target crate (`crate-new` skeleton) or a module in the existing - one; move code in compiling steps; re-export nothing from the old crate as - a compatibility shim unless the design says so, with a removal step. -3. Move tests with the code they cover; the characterization tests stay green. -4. Update both cards, the layer note, `cargo xtask docs`, and write the ADR. -5. `gate` quick after each step; `40-steps.md`. - -## 3. Verify (tester) - -`gate` full plus `cargo machete --skip-target-dir` and the license-notice -check. Rerun the survey on both crates against the metric. - -## 4. Review (reviewer) — `design-review`; two-round limit. - -## 5. Wrap up - -Report metric before/after, gate results, ADR number, baseline delta, verdict -and proposed commits (crate creation, each move, ADR). Commit only if the user -asked. +Follow AGENTS.md's Workflow and Definition of done. Record branch/base, ownership +move, metric, risks and checks in a compact plan; use [structured handoffs](../feature/plan-bus.md) +when helpful. A crate-boundary change requires an ADR. + +1. Survey ownership and references with `responsibility-map`; apply `placement` + to each piece and check its dependency layer. A module spanning incompatible + layers needs to be separated, not placed in the nearest crate. +2. Resolve boundary problems within the authorized design: split domain and + presentation responsibilities, move a narrow contract down, or keep the + responsibility in its existing crate. Present options to the user only when + a material choice remains or the solution changes authorized scope/authority. +3. Use `port-design` for cross-boundary collaborators and reject dependency cycles. + Design target crate/module, public surface, typed errors, wiring, move order, + tests and the `docs-adr` decision. Use the `crate-new` skeleton if needed. +4. Characterize through the old API, move in coherent compiling steps and keep + tests with the code they cover. Avoid compatibility shims unless the design + requires one and gives its removal condition. +5. Update ownership cards, layer notes and ADR; regenerate references when the + combined dependency changes settle. Use `gate` for both sides and consumers. +6. Review with `design-review`, fix findings and finish the applicable completion + checks. Report ownership/metric changes, checks/results and limitations. + Vendor and app changes require separate commits; commit/push/PR only on request. diff --git a/.claude/skills/debt-paydown/SKILL.md b/.claude/skills/debt-paydown/SKILL.md index 7d93c35d..8236972a 100644 --- a/.claude/skills/debt-paydown/SKILL.md +++ b/.claude/skills/debt-paydown/SKILL.md @@ -17,7 +17,7 @@ branch and verification scope to every subagent If no entry is given, list candidates from `xtask/baseline.toml` and `xtask/oversized-files.toml` with `cargo xtask survey ` facts and propose the cheapest one with the most value (small counts first). Record the -entry, its current value and the target value in `00-task.md`; branch +entry, its current value and the target value in the compact plan (or `00-task.md`); branch `refactor/`. ## 1. Route by kind @@ -31,8 +31,10 @@ entry, its current value and the target value in `00-task.md`; branch | `domain_io`, `domain_dependencies` | `port-design` (contract in the domain, implementation in an adapter) | | `complexity_expects` | split the function by the decisions it makes, or extract an owner; never move the `#[expect]` | -Follow the chosen workflow's design approval, build, verify and review -stages. +Follow the relevant procedures under AGENTS.md's Workflow and input-based +Definition of done. Use one compact plan or structured handoffs as useful; +record the entry and goal without requiring every stage or output file. +Changing a baseline entry does not by itself require another full pipeline. ## 2. Lower the record diff --git a/.claude/skills/design-review/SKILL.md b/.claude/skills/design-review/SKILL.md index d4b2f5da..f6e62309 100644 --- a/.claude/skills/design-review/SKILL.md +++ b/.claude/skills/design-review/SKILL.md @@ -6,13 +6,36 @@ user-invocable: false # Design review -Input: the diff against the recorded target, including uncommitted changes -(`git diff ...` plus `git diff` and untracked files), and the plan -directory. Output: `60-review.md`. +Input: the task, recorded target and existing evidence. Inspect committed changes +with `git diff ...HEAD`, staged and unstaged changes with `git diff HEAD`, +and relevant untracked files from `git ls-files --others --exclude-standard`. +Read surrounding owners/consumers as needed. Record findings in the compact plan. -Automated checks already cover limits and debt counts; this review covers -intent. Read the plan's `10-placement.md`, `20-map.md` and `30-design.md` -first and check the code against them. +Automated checks cover limits and debt counts; this review covers intent. +Inspect existing scoped/final evidence and pending checks. Do not rerun full +architecture or tests just because review started; the `gate` skill governs +reuse and any missing/suspect check. Finish the applicable input-based Definition +of done after review fixes. Read relevant plan/placement/map notes when present +and check the code against the authorized task. + +## Ownership and cost + +For changed state or boundaries, trace one actual use case through its owners: + +- Who owns the authoritative state, its invariant and all transitions? Does the + proposed split reduce coupling, or just rearrange fields to satisfy a count? +- Who creates/releases subscriptions, tasks and resources? What happens on + close, cancellation, account/session changes and delivery of a stale result? +- Is each module cohesive and understandable? Independent responsibilities need + separation even below limits; small pure modules need no artificial owner. +- For affected hot paths, is work bounded and proportional to changed/visible + data? Inspect redraw/notification fan-out and cache invalidation. Use + `performance` for relevant runtime/GUI evidence; do not demand unrelated benchmarks. + +Structural PASS is evidence about measured rules, not semantic approval. The +current public-field metric covers feature/service; impl spread is counted above +four files; ownership cards are checked for labels. Review the design rules even +where those checks do not report a violation. ## Anti-patterns @@ -33,13 +56,16 @@ first and check the code against them. | 13 | A duplicated non-fake test helper (for example a `click` wrapper) | advisory | | 14 | Crate card, layer note or ADR not updated for a boundary change | blocking for boundary changes | | 15 | `#[allow]`/`#[expect]` for `too_many_lines`/`cognitive_complexity`; raised baseline; new oversized entry | always | -| 16 | A file or type that crossed an early warning in this change with no map or split plan | advisory | -| 17 | A built-in panel or widget outside a `PLUGIN` or the registration path, a panel entity held outside the workspace, an eagerly created widget, or a getter or `pub` item added for plugins | blocking | +| 16 | A touched responsibility near a hard limit whose growth has not been considered | advisory | +| 17 | A contribution bypassing its `PLUGIN`/`CORE` catalog or registration, a panel entity held outside the workspace, an eagerly created widget, or a native getter misrepresented as guest capability access | blocking; honor the explicit core/required-panel exceptions in `plugin-api` | +| 18 | Mirrored authoritative state, stale async delivery, missing cancellation/teardown or cache invalidation | blocking when a concrete incorrect transition or unbounded resource lifetime is possible | +| 19 | Blocking/expensive work added to render/input paths, redundant full refreshes, unbounded background work or caches | blocking for a demonstrated regression or an identifiable unbounded path; give the workload and consequence | -For each finding give: file:line, the rule (AGENTS.md section or ADR), the -concrete consequence, and the smallest fix. +For each finding give file:line, the rule (AGENTS.md section or ADR), a triggering +scenario/consequence and the smallest fix. Distinguish evidence from hypotheses; +an unmeasured speedup is not a verified result. Style preferences alone are advisory. -## Output: `60-review.md` +## Review to record ``` # Review against @@ -50,5 +76,5 @@ concrete consequence, and the smallest fix. Verdict: approve | changes requested ``` -Changes requested go back to the implementer; after two failed rounds escalate -to the user (AGENTS.md "Pipeline"). +Fix actionable findings under AGENTS.md's Workflow. Escalate material unresolved +choices or concrete blockers, without a fixed round count. diff --git a/.claude/skills/docs-adr/SKILL.md b/.claude/skills/docs-adr/SKILL.md index a1155ccb..6fcbedc9 100644 --- a/.claude/skills/docs-adr/SKILL.md +++ b/.claude/skills/docs-adr/SKILL.md @@ -47,6 +47,8 @@ Status: accepted Add a row to `docs/adr/README.md` (`| [NNNN](NNNN-slug.md) | |`). Accepted records are never edited; a later record supersedes them and says so. +For supersession, link both directions in the mutable index and name which +parts changed, so readers can find the current rule without reading all history. No session status, dates of work, or "implemented by" notes. ## 3. Cards and notes @@ -58,16 +60,18 @@ No session status, dates of work, or "implemented by" notes. or leaves the layer or a port between layers changes. - `cargo xtask docs` regenerates `docs/reference/*` and `docs/architecture/dependencies.md`; run it after changing settings, tokens, - actions, keymap or internal Cargo dependencies. + actions, keymap or internal Cargo dependencies, once the combined inputs + settle. Prose-only changes do not require Cargo generators or architecture + scans; use the documentation scope in `gate`. ## 4. Stale references For each removed or renamed symbol, module or crate: ```sh -grep -rn "" docs/ AGENTS.md CONTRIBUTING.md .claude/skills .claude/agents +rg -n "" docs/ AGENTS.md CONTRIBUTING.md .claude/skills .claude/agents ``` Update current documents; leave accepted ADRs as historical text. -End with the Result block listing documents written or changed. +Report documents written or changed in the shared plan or handoff. diff --git a/.claude/skills/feature/SKILL.md b/.claude/skills/feature/SKILL.md index 0ada63ed..844ab5e2 100644 --- a/.claude/skills/feature/SKILL.md +++ b/.claude/skills/feature/SKILL.md @@ -1,52 +1,42 @@ --- name: feature -description: Run the full agent pipeline (architect → implementer → tester → reviewer) for a feature or non-trivial change. Use when the user asks to build a feature end to end or invokes /feature. +description: Carry a feature or non-trivial change through design, implementation, verification and review, scaling delegation and planning to the task. Use for end-to-end work or /feature. --- -Drive the pipeline described in AGENTS.md for the task: $ARGUMENTS +Task: $ARGUMENTS -You are the orchestrator. Subagents start with no context, so every prompt must -carry the task, plan directory, branch name, target/base branch, verification scope -and prior findings, plus paths to docs/ARCHITECTURE.md, relevant layer notes and -docs/adr/README.md (AGENTS.md is loaded automatically). Decisions live in -`.claude/plans//` as described in [plan-bus.md](plan-bus.md); every stage -reads the files earlier stages wrote and ends with its Result block. +Follow AGENTS.md's Workflow and Definition of done. Use one agent for scoped work; +delegate distinct responsibilities when complexity, risk or independent work +benefits from it. The role files in `.claude/agents/` are optional and inherit +caller model settings. Never assume a delegate has unseen context: supply task, +branch/base, plan/evidence location, scope and relevant prior findings. -0. **Route** — classify the task and follow the matching workflow skill instead - of the generic stages below when one fits: +1. Record a compact goal, approach, affected crates/layers, risks and check scope. + Use [plan-bus.md](plan-bus.md) for optional structured handoffs. Honor an + existing authorized plan; resolve only material unanswered choices. + For changed ownership, name the invariant, transitions and lifetime; for + consequential alternatives, compare correctness, reuse, cost and maintenance. +2. Select relevant procedures rather than launching every workflow: - | Task | Workflow | + | Need | Procedure/workflow | | --- | --- | - | responsibility with no fitting crate, or "new crate" | `crate-new` | - | behaviour added inside one crate | `crate-extend` | - | restructure inside a crate, early warning or limit hit | `refactor` | - | move a module or subject to another crate | `crate-split` | - | shrink an `xtask/baseline.toml` or oversized-file entry | `debt-paydown` | + | Responsibility with no fitting crate | `crate-new` | + | Behavior inside an existing crate | `crate-extend` | + | Restructure within a crate | `refactor` | + | Move ownership across crates | `crate-split` | + | Shrink a debt inventory entry | `debt-paydown` | + | Rendering, caches or other hot paths | `performance` | - Spanning several rows: run them as separate branches in the order - refactor → split → extend. None fits (cross-cutting, app-only): continue - with the stages below, using the procedure skills each agent preloads. - -1. **Design** — spawn `architect` with the task and reading paths above. Require - placement, shape, public surface, reuse, typed errors, docs/ADR and the target - branch in the plan. Show it to the user and wait for approval; an explicit - instruction to implement an already supplied plan supplies that approval. - Relay unresolved design questions. -2. **Branch** — create `/` from the recorded target: `master` for - release-ready changes or `dev` for integration, per CONTRIBUTING.md. Verify - the local base against its remote; do not reset or overwrite unrelated work. -3. **Build** — spawn `implementer` with the full approved plan. -4. **Verify** — spawn `tester` with the plan and the implementer's report. If it reports defects or red checks, send them to `implementer` and verify again. -5. **Review** — spawn `reviewer` with the plan and target. Include committed, - uncommitted and untracked task changes; review must finish before requesting - commit authorization. Require the blocking design checklist. Send `blocking` - and `should-fix` findings to `implementer`, then re-run Verify and Review. -6. **Stop conditions** — after two failed Verify or Review rounds, stop and escalate to the user with the outstanding findings. -7. **Wrap up** — summarise what was built, each applicable verification command - and result, review verdict and remaining nits. Propose Conventional Commit - headers and a PR title. Include the target branch and the `xtask/baseline.toml` - delta in the proposed PR description. Keep vendor changes separate from - application changes. Commit, push and open the PR only if the user asks. - -Documentation-only changes skip subagents under AGENTS.md and use its documented -verification scope. + A cohesive change may combine them on one branch; an early warning alone + does not require a refactor workflow. Split independently reviewable work + when useful, respecting the user's requested base/branch and existing work. +3. Implement the authorized design with behavior tests. Use `gate` scoped mode + after coherent edits where a check answers a concrete uncertainty. +4. Review committed, uncommitted and relevant untracked task changes against the + recorded target. Use `design-review`; fix actionable findings and verify + affected inputs. Escalation follows AGENTS.md, without a fixed round count. +5. Assign one completion executor when working in parallel. Finish the + input-based Definition of done after review fixes; preserve valid evidence. +6. Report what changed, commands/results, scope and remaining limitations. + Propose commit/PR text only when useful. Vendor and app changes belong in + separate commits; commit, push and PR actions require the user's request. diff --git a/.claude/skills/feature/plan-bus.md b/.claude/skills/feature/plan-bus.md index bb24499b..d9c68642 100644 --- a/.claude/skills/feature/plan-bus.md +++ b/.claude/skills/feature/plan-bus.md @@ -1,39 +1,46 @@ -# Plan directory contract - -Subagents start without context, so every workflow keeps its decisions in one -ignored directory, `.claude/plans//`, and each step reads what earlier -steps wrote. Chat summaries are not a substitute: if a decision is not in the -directory, the next step does not know it. - -| File | Written by | Contents | -| --- | --- | --- | -| `00-task.md` | workflow | task, workflow name, target branch (`master`/`dev`), feature branch, verification scope, **success metric** (from `cargo xtask survey`, e.g. "`Thread` fields 15 → ≤ 9; files ≥ 640 lines in `thread/`: 0") | -| `10-placement.md` | `placement` | crate, layer, reason, card check, new-crate decision | -| `20-map.md` | `responsibility-map` | survey facts, matrix interpretation, candidate owners with draft cards | -| `30-design.md` | architect / workflow | owners, ports, errors, wiring, tests, docs/ADR, commit plan; approved by the user | -| `40-steps.md` | implementer | ordered steps with `- [ ]` / `- [x]`, one transformation each, gate result per step | -| `50-gate.md` | `gate` | each command, its result, baseline delta, early warnings before/after | -| `60-review.md` | `design-review` / reviewer | findings (`file:line`, severity, scenario, fix) and verdict | - -Rules: - -- Create the directory in the first step; never write plans into `docs/`. -- Append, do not rewrite, earlier decisions; a changed decision records why. -- A step is complete only when its file exists with the sections above filled. - -## Result block - -Every procedure skill ends its turn with this block, and the workflow checks it -before the next step: - -``` -## Result -status: done | blocked | escalate -wrote: -next: -escalation: +# Plans and handoffs + +Use one compact document under ignored `.claude/plans//` by default. +Record the goal, branch/base, scope, approach, material decisions and risks, +checks/results and remaining work. Scale detail to the task. No plan files are +required for trivial edits; AGENTS.md governs authorization and completion. +For ownership changes include state, invariant, transitions and lifetime. For +hot-path changes include the representative workload and cost evidence when +applicable; these are short sections, not additional stages or mandatory files. + +Delegates receive the task and relevant context or a plan location explicitly. +Do not assume either that every tool shares history or that every delegate +starts without it. Record durable decisions that the next owner needs; avoid +copying the same evidence into several reports. + +For larger work, the following are optional sections or separate files: + +| Section / suggested file | Useful content | +| --- | --- | +| Task / `00-task.md` | goal, branch/base, scope, behavior or migration metric | +| Placement / `10-placement.md` | crate/layer choice and boundary rationale | +| Map / `20-map.md` | survey facts and candidate owners for a structural change | +| Design / `30-design.md` | approach, public surface, errors, tests, docs/ADR | +| Progress / `40-steps.md` | meaningful steps and deviations | +| Evidence / `50-gate.md` | command, source/configuration, scope, result/log, pending checks | +| Review / `60-review.md` | ranked findings and verdict | + +Use only sections relevant to the task. A stage is complete when its work and +needed evidence are available, not when a file list has been filled. Revise a +changed decision with a short reason; an append-only transcript is unnecessary. +Store plans/progress here rather than in current-system `docs/`. + +A handoff may use this optional compact block (a normal concise report is fine): + +```text +status: done | blocked | needs-decision +changed: +evidence: +remaining: ``` -`blocked` means a check or precondition failed and the step can be retried after -a fix; `escalate` means the decision belongs to the user (a layer conflict, a -new ADR, a rule that cannot be met). Never continue past `escalate`. +All procedure-skill output filenames and Result blocks are suggestions under +this contract; their content may live in the shared compact plan. A new ADR or +routine implementation detail does not itself require another user approval. +Never proceed with a genuinely required unresolved authorization; follow +AGENTS.md for material decisions and concrete blockers. diff --git a/.claude/skills/gate/SKILL.md b/.claude/skills/gate/SKILL.md index 4ae8ba77..d14cb08a 100644 --- a/.claude/skills/gate/SKILL.md +++ b/.claude/skills/gate/SKILL.md @@ -1,71 +1,139 @@ --- name: gate -description: Run the definition-of-done checks through the shared Cargo lock and record exact results, the debt baseline delta and early warnings in 50-gate.md. Quick mode after each step, full mode before review or commit. +description: Select local checks by changed inputs, reuse valid evidence, and finish the applicable Definition of done without per-edit or per-stage command bundles. user-invocable: false --- -# Gate +# Verification -Rules: AGENTS.md "Definition of done" and hard rules. Every Cargo command runs -through `.claude/hooks/cargo-serial.sh`, which sets `CARGO_BUILD_JOBS=2` and -holds one lock shared by every agent on the machine; the project hook rejects -compiling Cargo commands that bypass it. +AGENTS.md sets the workflow and requires this procedure's completion checks. +This file owns the detailed check matrix and commands; roles refer here rather +than copying them. Required CI is independent. Cargo resource settings follow +CONTRIBUTING.md; the coordination helper is optional. -## Quick mode (after each step) +## Select by the uncertainty -```sh -W=.claude/hooks/cargo-serial.sh -$W cargo fmt --all --check -$W cargo clippy -p --all-targets -- -D warnings -$W cargo test -p -$W cargo xtask architecture -``` +| Question/change | Useful scoped check | +| --- | --- | +| Reproduced regression | One `cargo test -p --lib ` or existing integration target/filter | +| Behavior spans an owner/package | Affected test targets and consumers | +| API/compilation uncertainty | `cargo check -p --lib` plus affected consumers, unless tests already compile them | +| Lint failure | Clippy for the affected package/targets/features | +| Rust formatting | Format changed sources; use `cargo fmt --all --check` for completion | +| Rendering, caching or other hot paths | Use `performance` for representative cost and correctness evidence; choose relevant GUI/behavior tests | +| Ownership/guarded structure | Inspect changed counts; survey when useful; run architecture for affected guardrails | +| Skills/agent instructions | Configuration validator, links and stale references | +| Hooks/scripts | Their behavior tests and language syntax/configuration checks | +| Prose only | Links/references; Cargo/generated checks only for affected generator inputs | -## Full mode (before review, commit or "done") +Finish a coherent edit before checking. Do not edit checked inputs while their +check runs. A test already compiles its target; do not routinely precede it with +both check and build. In parallel work, coordinate one executor and avoid +queuing the same pending check twice. -```sh -W=.claude/hooks/cargo-serial.sh -$W cargo fmt --all --check -$W cargo build --workspace --all-targets -$W cargo clippy --workspace --all-targets -- -D warnings -$W cargo test --workspace -$W cargo xtask architecture -$W cargo xtask docs --check +## Completion and failures + +At integrated completion, choose the applicable AGENTS.md scope after review +fixes. Broad shared changes, uncertain workspace impact or an explicit +full-verification request need the full workspace suite below. A small change +does not inherit that suite merely because a stage or worktree ended. Apply +required plugin-feature, +SSH/hub, guest, dependency/notice and vendor checks when their inputs change; +confirm fixtures actually ran, rather than counting skipped/zero tests. + +Rerun a failing target first. Preserve successful evidence when relevant +source/configuration is unchanged; identify which results a fix invalidates. +A new stage/report does not invalidate evidence. Reviewers inspect it and rerun +only missing, stale or specifically suspect checks. Keep stable targets and +investigate concrete stale artifacts before cleaning or touching all sources. +Do not change checks, architecture limits or baselines to obtain a pass. + +## Evidence + +One compact evidence section in the plan is enough: + +```text +Command | Source/configuration and targets/features | Result | Log if needed +Reused: +Pending/unavailable: ``` -Add when relevant: +Record the worktree/source snapshot, including relevant uncommitted/untracked +inputs. Separate queue wait and execution time when available. Report scoped +coverage as scoped; a completion pass does not assert a full-workspace pass. + +## Definition of done -| Changed | Also run | +Run the checks applicable to the integrated change, and report each actual +command and result plus omitted/unavailable checks and why: + +| Changed inputs / impact | Local completion checks | | --- | --- | -| SSH/SFTP code | `NOCTERM_REQUIRE_SSHD=1 $W cargo test -p nocterm-ssh --test sshd` | -| dependencies, vendored manifests, licenses, bundled assets | `$W cargo machete --skip-target-dir`; `python3 scripts/generate-license-notices.py --check-inputs` | -| public docs comments | `$W cargo doc --workspace --no-deps -j1` | -| `.claude/hooks/` | `python3 scripts/test-claude-hooks.py` | -| `.claude/skills/`, `.claude/agents/` | `python3 scripts/check-claude-config.py` | +| Documentation or agent instructions | Links and stale references; `python3 scripts/check-claude-config.py` for skills/agent configuration | +| Hooks or repository scripts only | Relevant script tests and syntax/configuration validators; no Cargo unless Rust or generator inputs are affected | +| Scoped Rust behavior | Formatting, lint and behavior tests for affected targets and consumers; architecture checks when guarded structure/ownership changes; generated references when their inputs change | +| Broad shared APIs, dependency/feature graph changes affecting many consumers, cross-cutting behavior, or uncertain workspace impact | Full workspace suite below, plus affected feature/platform fixtures | +| Explicit request for full verification | Full workspace suite and relevant additional checks | -## Interpreting +The full workspace suite is `cargo fmt --all --check`, +`cargo build --workspace --all-targets`, +`cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`, +`cargo xtask architecture` and `cargo xtask docs --check`. A scoped result must +not be presented as a full-workspace pass. Prose/tooling-only edits do not +require Cargo, generated-doc checks or product runtime tests when their inputs +are unchanged. -- A failure stops the gate; fix it, never weaken a check, never `--no-verify`. -- **Baseline**: `git diff xtask/baseline.toml` must show only decreases. If the - architecture check reports debt that shrank, lower the entry in the same - change. -- **Early warnings** from `cargo xtask architecture` are not failures. A - warning for a file or type this change grew means: record it in - `50-gate.md`, and if it is within one step of the limit, run - `responsibility-map` before adding more. +Additional checks follow actual changed inputs: -## Output: `50-gate.md` +- SSH/SFTP changes run the required SSH fixture; hub adapter changes run the + hub end-to-end test (see Commands). +- User-plugin code (`nocterm-plugin-*`, `plugins-ui`, + `src/shell_setup/user_plugin*`) also runs workspace clippy and tests with + `--features nocterm/user-plugins`; the default build compiles it out. + Plugin SDK/example changes also build/test the example and run the gated + WASM guest test. Preserve API/ABI compatibility checks for contract changes. +- Dependencies/manifests require applicable unused-dependency and notice checks; + vendored changes follow CONTRIBUTING.md's replay/history checks. +- Hook changes run `python3 scripts/test-claude-hooks.py`; configuration-validator + changes run `python3 scripts/test-claude-config.py` as well. -``` -# Gate () -| Command | Result | -| --- | --- | -| cargo fmt --all --check | pass | -… -Baseline delta: -Early warnings touched by this change: -Not run: +Commit, push and PR creation still require the user's request. + +## Commands + +```sh +# Choose resource settings locally; cargo-serial.sh is optional. +cargo run +cargo fmt --all --check +cargo build --workspace --all-targets +cargo clippy --workspace --all-targets -- -D warnings +cargo test --workspace +cargo clippy --workspace --all-targets --features nocterm/user-plugins -- -D warnings +cargo test --workspace --features nocterm/user-plugins +cargo test --locked --manifest-path examples/plugins/focus-timer/Cargo.toml +cargo build --locked --release --target wasm32-unknown-unknown --manifest-path examples/plugins/focus-timer/Cargo.toml +NOCTERM_GUEST_WASM=$PWD/examples/plugins/focus-timer/target/wasm32-unknown-unknown/release/focus_timer.wasm NOCTERM_REQUIRE_WASM_GUEST=1 cargo test -p nocterm --features nocterm/user-plugins user_plugin +NOCTERM_REQUIRE_SSHD=1 cargo test -p nocterm-ssh --test sshd +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm-hub-io --test hub +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm hub_live +cargo xtask docs +cargo xtask docs --check +cargo xtask architecture +cargo xtask survey [--json] [--type ] +.claude/hooks/cargo-serial.sh cargo +python3 scripts/test-claude-hooks.py +python3 scripts/check-claude-config.py +python3 scripts/test-claude-config.py +python3 scripts/check-plugin-api-compat.py +cargo machete --skip-target-dir +cargo doc --workspace --no-deps -j1 +python3 scripts/generate-license-notices.py --check-inputs ``` -Report exactly what ran and its result; mark anything skipped and why. -End with the Result block (`blocked` if any command failed). +Linux system dependencies and setup are in [README.md](../../../README.md). GUI checks +require an X11 or Wayland session and Vulkan driver. Regenerate references after +changing schemas, tokens, actions, keymap or internal Cargo dependencies. +Regenerate `THIRD_PARTY_NOTICES.txt` (`python3 scripts/generate-license-notices.py`, +needs `cargo-about` 0.9.2) after changing dependencies, vendored manifests or license +files, or bundled assets; CI and the pre-push hook reject stale notices +([docs/PACKAGING.md](../../../docs/PACKAGING.md)). diff --git a/.claude/skills/owner-extract/SKILL.md b/.claude/skills/owner-extract/SKILL.md index 7e18c164..90ff6c5d 100644 --- a/.claude/skills/owner-extract/SKILL.md +++ b/.claude/skills/owner-extract/SKILL.md @@ -1,20 +1,21 @@ --- name: owner-extract -description: Extract one responsibility of a type into an owner type with private state and transition methods, behind characterization tests. Use for a candidate from responsibility-map, when a type nears 15 fields, or when a file nears 800 lines — never cut a file or move methods of the same type instead. +description: Extract a confirmed state responsibility into a private owner with transition methods and behavior coverage. Use when ownership requires a split; approaching a size limit prompts inspection, not mechanical extraction. user-invocable: false --- # Owner extraction -Input: one candidate from `20-map.md`. Output: source changes and an updated -`40-steps.md` (`.claude/skills/feature/plan-bus.md`). +Input: a confirmed candidate in the compact plan. Record the change and evidence +there (`.claude/skills/feature/plan-bus.md`); separate stage files are optional. This is the transformation AGENTS.md design rules 1, 2 and 7 ask for. Moving methods of the parent into another file is not an extraction and fails review. ## Preconditions -- `20-map.md` names the owner, its fields, its methods and what it returns. +- The plan names the owner, state, invariant, transitions, lifetime and results. + Joint field access or a lower field count alone does not justify the owner. - Characterization tests exist for the behaviour that goes through those methods (`behavior-tests` in characterize mode). If they do not, write them first and see them pass on the unchanged code. @@ -35,9 +36,11 @@ methods of the parent into another file is not an extraction and fails review. short coordinator call that applies the returned effect. 4. **Delete the moved fields** from the parent. Do not leave mirrors or `pub(crate)` getters that expose the owner's internals. -5. **Test the owner directly** with unit tests for its invariants; keep the - characterization tests unchanged and green. -6. Run `gate` in quick mode. +5. **Test the owner directly** through its methods for invariants; preserve the + characterization scenarios and assertions. Mechanical imports/fixtures may move. +6. Use `gate` scoped mode for the owner/characterization behavior after the + coherent extraction when needed. Reuse unchanged results; no command bundle + after each moved method and no full gate for each extracted owner. ## Done when @@ -45,7 +48,8 @@ methods of the parent into another file is not an extraction and fails review. - The parent's impl-file count did not grow (`cargo xtask survey --type `). - The owner has no `pub`/`pub(crate)` fields and does not take the parent. - The owner's module name is a noun for what it owns. -- Characterization tests pass without edits; the owner has its own tests. +- Characterization behavior/expectations remain intact and pass; owner invariants + have coverage. Explain any fixture/API adaptation without weakening assertions. - `xtask/baseline.toml` entries went down or stayed equal; lower any that went down (the check fails until you do). @@ -59,4 +63,4 @@ methods of the parent into another file is not an extraction and fails review. | Fields moved but the parent keeps setters for each | the parent still owns the transitions | | Splitting a function into `step1`/`step2` helpers | complexity moved, not reduced | -End with the Result block and the before/after field counts. +Report behavior evidence and before/after field counts in the shared plan or handoff. diff --git a/.claude/skills/performance/SKILL.md b/.claude/skills/performance/SKILL.md new file mode 100644 index 00000000..87b3281f --- /dev/null +++ b/.claude/skills/performance/SKILL.md @@ -0,0 +1,77 @@ +--- +name: performance +description: Evaluate changed hot paths, rendering, repeated I/O or caches using representative costs and behavior. Use for optimization work or changes that affect input latency, redraws, large collections, background jobs or retained memory; not as a benchmark gate for every edit. +user-invocable: false +--- + +# Runtime cost and rendering + +AGENTS.md's Performance rule applies. Keep notes and results in the existing +compact plan; this procedure creates no extra approval or completion stage. + +## Find the cost + +1. Name the user action and the owner doing the work: typing, scrolling, + streaming output, opening a panel, refresh or background synchronization. +2. Inspect the whole affected path, including consumers. Estimate frequency × + work per event, collection sizes and retained memory. Look for repeated + scans/sorts/parsing, large clones, allocations, task spawning and fan-out. +3. For an optimization, capture a representative baseline using existing + profiling, tracing, counters or a focused benchmark. Choose the metric that + answers the issue: input/frame latency, calls/bytes, CPU or peak/retained memory. + Record workload, build mode and environment; compare the same conditions. + Use distributions or repeated samples for noisy timing, not one fast run. + +Compare plausible solutions when the tradeoff matters. First remove avoidable +work or improve the algorithm/data access; then consider batching, incremental +updates or caching. Account for invalidation, memory and maintenance cost. +Do not introduce a cache/framework just because it might help later. + +## Keep interaction responsive + +- Render and input callbacks consume prepared state; perform I/O and expensive + parsing/decoding in the appropriate adapter/service with bounded background + work. Moving work to a task does not make it free: bound concurrency/queues, + deduplicate equivalent requests and cancel obsolete work. +- Notify the affected owner only when its observable state changes. Coalesce + bursts without losing the final update or delaying required feedback. +- Reuse stable view identities and unaffected render regions. For large lists, + prefer existing virtualization and prepare only visible/needed data. Avoid + rebuilding the entire collection for one row or reparsing text every frame. +- Check theme/font/scale changes, resizing, scrolling, focus, selection and + content updates where affected. A reused frame must still respond correctly. +- Background watchers/pollers have a defined lifetime and cadence; share a + source when existing ownership permits it. Stop unused work on close/disable, + or explain why the underlying service must continue independently. + +## Cache contract + +Before adding or changing a cache, state: + +| Concern | Required answer | +| --- | --- | +| Value | Which repeated expensive work is avoided; why reuse is likely | +| Owner | Who owns entries and releases them; reuse an existing cache if it fits | +| Key | All semantic inputs, revisions and relevant account/session/source identity | +| Freshness | Events/revisions that invalidate entries; TTL only where staleness is acceptable | +| Bounds | Entry/byte budget, eviction and in-flight request limit appropriate to the data | +| Delivery | Old results cannot overwrite newer state or cross an account/session boundary | +| Failure | Miss/error/cancellation behavior; retries and negative caching are bounded | + +Keep authoritative state and access checks outside the cache. Release entries +when their scope ends; do not retain secrets or stale privileges for reuse. + +## Verify the affected behavior and cost + +Exercise cold/miss and warm/hit paths, changed inputs/invalidation, eviction and +late completion where applicable. Reuse public behavior tests and shared fakes. +Cost assertions may count externally requested work (for example network calls) +when reducing it is the requirement; avoid private-field and timing-flaky tests. + +For render changes, use relevant GUI/render tests and inspect the actual affected +interaction when a GUI session is available. Check visual correctness as well as +work avoided, including idle and repeated updates. A compilation pass or a static +screenshot alone does not establish responsive interaction or correct invalidation. +Report measured before/after results and tradeoffs; operation counts establish +less work, not a measured latency improvement. If profiling or GUI execution is +unavailable, state exactly what remains unverified. Completion checks use `gate`. diff --git a/.claude/skills/placement/SKILL.md b/.claude/skills/placement/SKILL.md index be815aeb..382c812b 100644 --- a/.claude/skills/placement/SKILL.md +++ b/.claude/skills/placement/SKILL.md @@ -7,7 +7,7 @@ user-invocable: false # Placement Input: one responsibility in a sentence ("parse container listings", "retain -chat drafts across restarts"). Output: `10-placement.md` in the plan directory +chat drafts across restarts"). Record the decision in the compact plan (contract: `.claude/skills/feature/plan-bus.md`). The rules are AGENTS.md "Where code goes" and docs/ARCHITECTURE.md layers; this @@ -15,10 +15,11 @@ is the procedure for applying them. ## 1. Classify the responsibility -Answer in order; the first "yes" decides the layer. +Classify each responsibility using the layer table; separate mixed responsibilities. | Question | Layer | | --- | --- | +| Is it a reusable low-level primitive with no product policy? | foundation, within its allowed dependencies | | Is it a rule, format or contract with no I/O and no GPUI? | domain | | Does it perform real filesystem, network, process, D-Bus or codec I/O behind a contract? | adapter | | Is it a GPUI entity with background work and no view? | service | @@ -30,39 +31,42 @@ file and validate it") into one piece per layer before going on. ## 2. Find the candidate crate -1. Read `docs/architecture/dependencies.md` and the layer note for the layer. +1. Find candidate owners in `docs/architecture/dependencies.md`; read their + relevant layer-note sections, not every subsystem in the layer. 2. For each candidate crate, read the `//!` card in its `lib.rs` (or `main.rs`). The responsibility must fit `Owns:` and must not appear in `Does not own:`. 3. Check the dependencies the code needs against the layer table in docs/ARCHITECTURE.md. A feature never depends on another feature: features meet through a narrow workspace port (`port-design` skill). -4. Search the ADR index (`docs/adr/README.md`) for the subject and read every - accepted record that names the candidate crate. +4. Search the ADR index (`docs/adr/README.md`) for decisions governing the + changed contracts. Follow supersession links; read older context when needed + to resolve a constraint. An incidental crate mention is not a reading requirement. ## 3. Module or new crate -Propose a new crate when **two or more** hold: +First exclude candidates whose layer/dependencies or ownership card forbid the +responsibility. One such conflict is enough; never put it there as a module. +Reuse a fitting existing crate. Propose a new crate when no existing owner fits; +justify its cohesive responsibility, dependency boundary and consumers. -- it needs a dependency set the candidate crate must not or does not have; -- it belongs to a different layer than the candidate crate; -- it changes for different reasons and has independent tests; -- adding it would put it in the candidate's `Does not own:` or grow the card's - `Owns:` with an unrelated subject. - -Otherwise it is a module of the existing crate, named after the responsibility -its type owns (never `utils`, `helpers`, `common`, `misc`). +Within a fitting crate, use a responsibility-named module (never `utils`, +`helpers`, `common`, `misc`). Independent invariants, lifetimes and reasons to +change guide separation; line counts or independent tests alone do not decide it. +Compare meaningful alternatives by correctness, reuse, coupling and runtime cost. Existing mixed responsibilities are migration debt, not precedent: "agent already does X" is not a reason to put more X there. -## 4. Stop conditions +## 4. Boundary decisions -- The code needs a dependency its layer forbids → `escalate` with the options - (move a narrow contract down a layer, split the responsibility, new crate). +- If the code needs a dependency its layer forbids, resolve the boundary within + the authorized design (move a narrow contract down, split the responsibility, + or choose a fitting crate). Ask only when a material choice or scope change + remains; never introduce a forbidden dependency. - The decision changes a crate boundary or a port between layers → mark "ADR needed" for the `docs-adr` skill. -## Output: `10-placement.md` +## Decision to record ``` # Placement: @@ -74,4 +78,4 @@ already does X" is not a reason to put more X there. - Module: — owns ``` -End with the Result block from the plan contract. +Report placement and any material unresolved decision in the shared plan. diff --git a/.claude/skills/plugin-api/SKILL.md b/.claude/skills/plugin-api/SKILL.md index a2a6cb0a..590ff7eb 100644 --- a/.claude/skills/plugin-api/SKILL.md +++ b/.claude/skills/plugin-api/SKILL.md @@ -1,10 +1,10 @@ --- name: plugin-api -description: Add, change or remove a plugin or anything the window shell hosts. Covers a system plugin (pub static PLUGIN, SystemPlugin with its id, name, description and required flag), a panel or status-bar widget contribution (SystemContribution, register_panel, register_widget, panel and widget factories), a contribution kind, icon, width class or group spec, and the plugin catalog in src/shell_setup.rs. Use when a task asks to expose app data or actions to plugins, user-written widgets or panels, or an API "handle" ("ручка"). No user plugin runtime exists yet, so step 0 stops and escalates instead of adding a getter. Also covers a plugin settings schema (SettingsSchema, FieldGroup, FieldSpec, FieldKind, SettingValue), its SectionBinding to the owner's settings section and the shared plugin settings form. Not for ports between built-in crates (port-design), MCP tools for AI agents (crates/ai tool catalog), GPUI actions or settings sections that no plugin schema exposes. +description: Work on built-in plugins, shell panels/widgets or feature-gated user plugins. Route built-in contributions through the catalog and workspace registration; route guest capabilities through the existing plugin API/ports/host/runtime with grants, compatibility and safe-mode boundaries. Not for ordinary ports between built-in crates or AI MCP tools. user-invocable: false --- -# Plugin API: system plugins and contributions +# Plugin APIs and contributions Rules: AGENTS.md "Where code goes" and design rules 2 and 3; crate card `crates/plugin-model/src/lib.rs`; ADR `docs/adr/0082-system-plugins-and-core-contributions.md` @@ -17,8 +17,33 @@ registration types in `crates/workspace/src/workspace/registration.rs`; catalog ## 0. Route - Consumer is the shell, the layout editor or the Plugins page → this skill. - Consumer is another built-in feature → `port-design`. -- Consumer is a user plugin or user script → stop. No runtime exists. Never add a - `Workspace` method, an `Entity` getter or a `pub` field for it. Result `escalate`. +- Consumer is a user plugin → follow the user-plugin route below. It already + exists behind `user-plugins`; a native getter or `pub` field is not a guest API. +- Consumer is an arbitrary user script → identify the supported entrypoint and + trust boundary. Do not invent native script execution or access to app internals. + Resolve a genuinely new authority decision under AGENTS.md's workflow policy. + +## User-plugin route + +Read `docs/plugins/README.md`, the relevant crate cards and ADRs +`docs/adr/0089-user-plugin-contract.md`, +`docs/adr/0090-user-plugin-host-and-passive-providers.md`, +`docs/adr/0091-user-plugin-code-runs-in-process-on-wasmi.md`, +`docs/adr/0092-plugin-sdk-and-developer-sessions.md` and +`docs/adr/0093-user-plugins-start-off-and-the-api-stays-compatible.md`. + +- Contract/capabilities belong to `nocterm-plugin-api`; native providers use + `nocterm-plugin-ports`. The host owns grants/effects, package and WASM adapters + own loading/execution, the view crate draws admitted views, and SDK/ABI define + the guest interface. Inspect existing types before proposing a new surface. +- Compose through `src/shell_setup/user_plugins.rs`. Preserve capability grants, + consent, gesture/rate/resource bounds, safe mode and the default-off release + policy. Do not expose GPUI entities or arbitrary native access to guests. +- Contract changes preserve API/ABI compatibility and generated references. + Run changed-input checks from AGENTS.md, including the user-plugin feature + matrix and SDK/example/gated guest tests when applicable. + +The following sections describe built-in contributions, not guest registration. ## 1. Declare A core function (connections, files, the workspace shell) declares @@ -102,10 +127,11 @@ the port simply omits the widget. Never reach a native source from a view. ## 5. Regenerate `docs/reference/plugins.md` is generated from the catalog. After a plugin, descriptor or -settings change run `.claude/hooks/cargo-serial.sh cargo test -p nocterm plugins_reference` -with `NOCTERM_BLESS=1` set, and commit the result. +settings change run `cargo test -p nocterm plugins_reference` +with `NOCTERM_BLESS=1` set, and include the generated result in the change. +Commit only when the user requests it. -## Output (in `30-design.md`) +## Suggested design notes (compact plan or `30-design.md`) ### Plugin - Owner crate, name, required (and why), contributions ### Contribution @@ -113,4 +139,4 @@ with `NOCTERM_BLESS=1` set, and commit the result. - Default changes: none | - Tests: -End with the Result block. +Report the relevant design, tests and remaining decisions concisely. diff --git a/.claude/skills/port-design/SKILL.md b/.claude/skills/port-design/SKILL.md index a4b1e8a2..7c9edf34 100644 --- a/.claude/skills/port-design/SKILL.md +++ b/.claude/skills/port-design/SKILL.md @@ -7,8 +7,8 @@ user-invocable: false # Port design Input: a consumer and what it needs ("the snippets panel needs to paste into the -user's terminal"). Output: a "Ports" section in `30-design.md` -(`.claude/skills/feature/plan-bus.md`), then code. +user's terminal"). Record the contract in the compact plan +(`.claude/skills/feature/plan-bus.md`) before implementing it. Rules: AGENTS.md "Where code goes" item 5, design rules 3 and 4; ADR 0006 (workspace ports), 0017 (local filesystem capabilities), 0032 (narrow terminal @@ -20,20 +20,22 @@ Search for an existing contract before writing one: - `nocterm-session`: `Transport`, `RemoteFs`, `LocalFs`/`LocalMutations`, `CredentialStore`, `HostExec`, `ProgramOpener`. -- `nocterm-workspace`: the narrow terminal and connection seams - (`LocalTerminal`, `UserTerminalPaste`, `TerminalCapabilities`, - `ConnectionDirectory`, …). +- `nocterm-workspace-ports`: terminal contracts (`UserTerminalPaste`, + `TerminalCapabilities`, …), available below the shell (ADR 0062). +- `nocterm-workspace`: shell seams (`LocalTerminal`, `ConnectionDirectory`, …). - `nocterm-store`: `Persisted`, `SnapshotWriter`, `AppendPersisted`, `ShutdownCoordinator`. -Reuse a contract only if the consumer needs **all** of it. Never add a method -to an existing broad trait for a new feature pair: write a separate port. +Reuse a cohesive contract when its dependencies, lifetime and authority fit. +Calling only some methods is not by itself a reason to wrap or duplicate it. +Use a narrower port when extra capabilities grant unwanted authority or couple +independent responsibilities. Never grow a broad trait for a new feature pair. ## 2. Shape - **Name for the consumer's need**: `UserTerminalPaste`, not `TerminalApi`; `ConnectionDirectory`, not `ConnectionsService`. -- **Minimal surface**: the methods this consumer calls today. More than about - six methods, or methods only another consumer uses, means two ports. +- **Minimal surface**: expose the cohesive capability needed today. Split by + independent responsibility or authority, not an arbitrary method count. - **Owner of the contract**: the lowest layer both sides may depend on — a domain crate (pure contract, e.g. session), `workspace` for feature-to-feature seams, a service crate for GPUI handles. Use `placement` if unsure. @@ -57,7 +59,7 @@ to an existing broad trait for a new feature pair: write a separate port. - The real implementation lives in the adapter (I/O) or the providing feature. - Composition injects it in `src/bootstrap.rs` (`wiring`). -## Output (in `30-design.md`) +## Contract to record ``` ### Port @@ -70,4 +72,4 @@ to an existing broad trait for a new feature pair: write a separate port. - Rejected alternative: because … ``` -End with the Result block. +Report the relevant changes, evidence and remaining work concisely. diff --git a/.claude/skills/refactor/SKILL.md b/.claude/skills/refactor/SKILL.md index f25e9974..f7e1a321 100644 --- a/.claude/skills/refactor/SKILL.md +++ b/.claude/skills/refactor/SKILL.md @@ -1,53 +1,28 @@ --- name: refactor -description: Workflow for a behaviour-preserving restructuring inside a crate — map responsibilities from survey facts, pin behaviour with characterization tests, extract owners one at a time, lower the debt baseline. Use when a file or type hits an early warning or limit, or the user asks to split, clean up or restructure code within a crate. +description: Restructure within a crate without changing behavior, using responsibility facts and characterization tests; use for a needed extraction or requested cleanup. argument-hint: " [package] [target: master|dev]" --- -# Workflow: refactor within a crate +# Refactor within a crate Subject: $ARGUMENTS -Behaviour does not change. Pass the plan directory, target branch and -verification scope to every subagent (`.claude/skills/feature/plan-bus.md`). - -## 0. Task - -`00-task.md` with a measurable metric taken from the survey, for example -"`` fields 14 → ≤ 9; files ≥ 640 lines in `/`: 2 → 0; -`impl_spread` entry for `` 6 → ≤ 4". Branch `refactor/`. - -## 1. Map (architect) - -1. `responsibility-map` → `20-map.md`. Its matrix pattern decides the route: - - hidden owners → step 2 per candidate; - - fields are already owners, coordination is the problem → design named - effects or per-workflow coordinators (ADR 0044) in `30-design.md`; - - a module that would rather be a crate → switch to `crate-split`. -2. `30-design.md`: candidates in order (smallest safe extraction first), each - with its expected counts; characterization tests to write; ADR decision via - `docs-adr` (none if crate boundaries hold). - -Show the map and design to the user and wait for approval. - -## 2. Extract (implementer), per candidate - -1. `behavior-tests` characterize — pass on unchanged code; names recorded. -2. `owner-extract` — one owner per step. -3. `gate` quick; tick the step in `40-steps.md` with before/after counts. - -Never mix behaviour changes into these steps. If a step needs one, stop and -report. - -## 3. Verify (tester) - -`gate` full. Rerun the survey and compare with the metric in `00-task.md`; -characterization tests unchanged and green. - -## 4. Review (reviewer) — `design-review`; two-round limit. - -## 5. Wrap up - -Report the metric before/after, gate results, baseline entries lowered and the -verdict. One `refactor(): …` commit per extracted owner is preferred. -Commit only if the user asked. +Follow AGENTS.md's Workflow and Definition of done, using a compact plan or +[structured handoff](../feature/plan-bus.md) as useful. Record branch/base, +behavior to preserve and a relevant structural metric. An early warning alone +does not require this workflow. + +1. Use `responsibility-map` to identify real owners. If fields already represent + owners, consider coordination by use case or named effects (ADR 0044). + Ownership moving to another crate uses `crate-split` instead. +2. Design extractions in a safe order, with expected counts, characterization + coverage and a `docs-adr` decision. Apply AGENTS.md's authorization policy; + routine details within an authorized design do not require another approval. +3. Characterize behavior on unchanged code, then `owner-extract` one responsibility + at a time. Preserve behavior; report any behavior change needed outside scope. +4. Use `gate` for affected behavior after coherent edits, reusing valid evidence. + Lower/remove debt inventory entries when the measured debt shrinks. +5. Review with `design-review`, fix findings and finish the applicable input-based + completion checks. Report before/after metric, behavior evidence and limitations. + Commit/push/PR only when requested. diff --git a/.claude/skills/responsibility-map/SKILL.md b/.claude/skills/responsibility-map/SKILL.md index f5e0a850..66d24c27 100644 --- a/.claude/skills/responsibility-map/SKILL.md +++ b/.claude/skills/responsibility-map/SKILL.md @@ -1,30 +1,29 @@ --- name: responsibility-map -description: Read-only analysis of a type, module or crate that finds its responsibilities and candidate state owners from `cargo xtask survey` facts. Run before any split, owner extraction or crate split, and when a file or type reaches an early-warning threshold. +description: Read-only analysis of a type, module or crate that finds its responsibilities and candidate state owners from `cargo xtask survey` facts. Use for a split or unclear ownership; early warnings invite inspection rather than mandatory extraction. user-invocable: false --- # Responsibility map -Input: a type (`AgentThread`), a module (`agent/thread`) or a crate. Output: -`20-map.md` in the plan directory (`.claude/skills/feature/plan-bus.md`). This -step writes no source code. +Input: a type (`AgentThread`), a module (`agent/thread`) or a crate. Record facts +and the proposed owners in the compact plan (`.claude/skills/feature/plan-bus.md`). +This step writes no source code. The goal is to find where responsibilities separate **by state**, because an owner is a set of fields with the transitions that keep them consistent -(AGENTS.md design rules 1, 2 and 7). Line counts only say that a split is due; -they never say where. +(AGENTS.md design rules 1, 2 and 7). Line counts signal remaining room; they do not decide whether or where to split. ## 1. Collect facts -Run through the shared lock (AGENTS.md hard rules): +Use a survey when it helps resolve ownership (Cargo resources follow CONTRIBUTING.md): ```sh -.claude/hooks/cargo-serial.sh cargo xtask survey --json > .claude/plans//survey.json -.claude/hooks/cargo-serial.sh cargo xtask survey --type +cargo xtask survey --json > .claude/plans//survey.json +cargo xtask survey --type ``` -Record in `20-map.md`: the warnings, the largest files, the type's fields, +Record relevant facts in the plan: the warnings, the largest files, the type's fields, methods and impl files, and the module reference counts. The survey is syntactic: it keys types by name and does not expand macros, so confirm any surprising fact in the source. @@ -37,11 +36,15 @@ coordinators use are listed separately: assign them by hand. | Pattern | Meaning | Next step | | --- | --- | --- | -| A cluster of 2+ fields with its own methods | a hidden owner | `owner-extract` | -| Every field is its own cluster and most methods are cross-cluster or coordinators | the fields are already owners; the type is a coordinator with too large a surface | split coordination by use case: named effects returned by owners (ADR 0044) or a separate coordinator per workflow | +| A cluster of 2+ fields with its own methods | possible owner | confirm one invariant and lifetime before `owner-extract` | +| Every field is its own cluster and most methods are cross-cluster or coordinators | possible coordination among existing owners | inspect actual types and transitions; split by use case only if responsibilities differ (ADR 0044) | | Many methods with no direct field access | delegating wrappers or free functions | move each next to the owner it calls, or into a pure function of the domain | | A field no method touches | state used only through destructuring or by another type | confirm; it may belong to that other type | -| Impl blocks of one type in 4+ files | file slicing already happened | treat each file's methods as a candidate owner, check its fields | +| Impl blocks of one type in 4+ files | possible file slicing | inspect responsibilities; do not treat each existing file as a future owner | + +Clusters reflect syntactic co-access, not semantic ownership. Confirm which +fields must change together, who is authoritative, and when work ends. A cohesive +type may need no split; pure logic may need functions rather than a state owner. For a module or crate, use the `modules` section: a top-level module whose references go mostly to one workspace crate, and which has few references to @@ -57,6 +60,7 @@ For every candidate owner write: - a draft card: `Owns:`, `Does not own:`, `Invariants:`; - its fields, its transition methods, and what it returns to the parent (a value, a typed error, or a named effect for the coordinator to apply); +- creation/teardown, cancellation and the destination of late asynchronous results; - expected field counts after extraction for both the parent and the owner. Reject a candidate that would need the parent passed back into it (`&mut Parent`, @@ -64,10 +68,11 @@ the parent's `Context`): it is not a separate responsibility yet. ## 4. Card drift -Compare what the code does with the crate card. List every responsibility that -is under `Does not own:` or missing from `Owns:`; these go to `docs-adr`. +Compare code with the crate card. A conflicting responsibility needs a placement +fix; do not legitimize it by expanding the card. Use `docs-adr` for actual boundary +decisions and stale descriptions after ownership is resolved. -## Output: `20-map.md` +## Decision to record ``` # Responsibility map: @@ -82,5 +87,5 @@ is under `Does not own:` or missing from `Owns:`; these go to `docs-adr`. ## Recommended order (smallest safe extraction first) ``` -End with the Result block; `next` is usually `behavior-tests` (characterize) -and then `owner-extract`. +Report the map and next useful step; `behavior-tests` and `owner-extract` +apply when an extraction is needed. diff --git a/.claude/skills/typed-errors/SKILL.md b/.claude/skills/typed-errors/SKILL.md index d7421005..616a2cea 100644 --- a/.claude/skills/typed-errors/SKILL.md +++ b/.claude/skills/typed-errors/SKILL.md @@ -45,4 +45,4 @@ count may only shrink. - [ ] causes retained with `#[source]`/`#[from]`/`SharedError` - [ ] variants carry data, not formatted text -End with the Result block. +Report the relevant changes, evidence and remaining work concisely. diff --git a/.claude/skills/wiring/SKILL.md b/.claude/skills/wiring/SKILL.md index f50b8423..8296d9d7 100644 --- a/.claude/skills/wiring/SKILL.md +++ b/.claude/skills/wiring/SKILL.md @@ -33,11 +33,10 @@ installations), 0001/0056/0057 (settings sections and pages). ## 2. Order in `bootstrap` -Insert the new step after everything it depends on and before everything that -uses it. The existing order is: settings → UI and shutdown coordinator → themes -→ layout → terminal dependencies → connections → terminal and vault → snippets -→ agent → files → keymap → monitor → settings pages → `Booted`. GUI tests use -the same `bootstrap` with fakes in `Services`; add the fake there too. +Read the current `bootstrap` in `src/bootstrap.rs`. Insert a step after its +dependencies and before its consumers; do not maintain a second ordered list +here. GUI tests use the same bootstrap with fakes in `Services`; update the +affected supplied capability there too. ## 3. Persistence: pick, do not write @@ -68,6 +67,6 @@ Writing `std::fs::write` in a feature or service is a review blocker. - [ ] the installation is retained, not recreated per window - [ ] persistence uses a row of the table above and is registered for shutdown - [ ] bootstrap order respects dependencies; GUI test services updated -- [ ] references regenerated (`cargo xtask docs`) +- [ ] references regenerated when generator inputs changed (`gate`) -End with the Result block. +Report the relevant changes, evidence and remaining work concisely. diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..e4e9345a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +# Keeps the build context of deploy/runner/Dockerfile small. Nothing else in +# the repository builds with docker. +.git +.claude +.playwright-mcp +**/target +node_modules +/target*/ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 5956122b..5e1cc416 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,82 +28,73 @@ permissions: contents: read pull-requests: read -jobs: - vendor-patches: - name: Vendored source replay - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v5 - with: - ref: ${{ inputs.ref || github.ref }} - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: Test archive authentication and replay failures - run: python3 scripts/test-vendor-patches.py - - name: Replay published archives and patches - run: python3 scripts/check-vendor-patches.py - - agent-guardrails: - name: Agent hooks and skills - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v5 - with: - ref: ${{ inputs.ref || github.ref }} - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: Check Claude Code hooks - run: python3 -B scripts/test-claude-hooks.py - - name: Check Claude Code configuration - run: | - python3 -B scripts/test-claude-config.py - python3 -B scripts/check-claude-config.py +env: + CARGO_TERM_COLOR: always + # No debugger symbols in CI: links are faster and the caches several times smaller. + # A failing test still names itself and its assertion. + CARGO_PROFILE_DEV_DEBUG: "0" + CARGO_PROFILE_TEST_DEBUG: "0" - plugin-api-compat: - name: Plugin API compatibility +# How the jobs share work: `lint` checks (clippy, rustdoc) and `test` builds, so neither +# repeats the other's compilation; `user-plugins` tests only the packages the feature +# changes. Caches are saved by pushes to `dev` and `master`, also when a step fails, and +# pull requests restore them. +jobs: + policy: + name: Repository policy runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 15 steps: - uses: actions/checkout@v5 with: ref: ${{ inputs.ref || github.ref }} + # Plugin API compatibility compares with the latest release tag. fetch-depth: 0 - uses: actions/setup-python@v5 with: python-version: "3.12" - - name: Test the compatibility rules - run: python3 -B scripts/test-plugin-api-compat.py - - name: Compare the API surface with the latest release - run: python3 -B scripts/check-plugin-api-compat.py - - release-pipeline: - name: Release pipeline regressions - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v5 + - uses: actions/setup-node@v4 with: - ref: ${{ inputs.ref || github.ref }} + node-version: "22" - uses: dtolnay/rust-toolchain@stable with: toolchain: "1.98.0" - - uses: actions/setup-node@v4 + - uses: taiki-e/install-action@v2 with: - node-version: "22" - - name: Install isolated updater test dependencies - run: npm install --prefix "$RUNNER_TEMP/release-pipeline-tests" --ignore-scripts --no-audit --no-fund release-please@17.3.0 yaml@2.9.1 - - name: Check release updates and validation wiring + tool: cargo-machete@0.9.2,cargo-deny + # Every step runs even when an earlier one fails, so one run reports every problem. + - name: Agent hooks and configuration + run: | + python3 -B scripts/test-claude-hooks.py + python3 -B scripts/test-claude-config.py + python3 -B scripts/check-claude-config.py + - name: Plugin API compatibility + if: ${{ !cancelled() }} + run: | + python3 -B scripts/test-plugin-api-compat.py + python3 -B scripts/check-plugin-api-compat.py + - name: Release pipeline + if: ${{ !cancelled() }} env: NODE_PATH: ${{ runner.temp }}/release-pipeline-tests/node_modules - run: node scripts/test-release-pipeline.cjs - - name: Check release notice preparation - run: python3 scripts/test-prepare-release-pr.py - - name: Check architectural history policy fixtures - run: python3 scripts/test-architecture-history.py + run: | + npm install --prefix "$RUNNER_TEMP/release-pipeline-tests" --ignore-scripts --no-audit --no-fund release-please@17.3.0 yaml@2.9.1 + node scripts/test-release-pipeline.cjs + python3 scripts/test-prepare-release-pr.py + python3 scripts/test-architecture-history.py + # Packaging rejects stale notices only after merge; catch the drift while the change + # is open. Release pull requests get their notices regenerated by release.yml, which + # then validates the prepared commit through this workflow. + - name: Third-party notice inputs + if: ${{ !cancelled() && !startsWith(github.head_ref, 'release-please--') }} + run: python3 scripts/generate-license-notices.py --check-inputs + # Manifest-only. --with-metadata is not used: it rewrites Cargo.lock. + - name: Unused dependencies + if: ${{ !cancelled() }} + run: cargo machete --skip-target-dir + - name: Dependency advisories + if: ${{ !cancelled() }} + run: cargo deny check advisories conventions: name: Conventional Commits @@ -139,6 +130,7 @@ jobs: scripts/lint-commit-msg.sh "$RUNNER_TEMP/title" - name: Lint commits + if: ${{ !cancelled() }} env: BASE: ${{ steps.pr.outputs.base || github.event.pull_request.base.sha }} HEAD: ${{ inputs.ref || github.event.pull_request.head.sha }} @@ -151,6 +143,7 @@ jobs: exit $status - name: Check architectural baseline and vendor commit separation + if: ${{ !cancelled() }} env: BASE: ${{ steps.pr.outputs.base || github.event.pull_request.base.sha }} HEAD: ${{ inputs.ref || github.event.pull_request.head.sha }} @@ -178,43 +171,47 @@ jobs: git cat-file -e "$BASE^{commit}" || git fetch --no-tags origin "$BASE" python3 scripts/check-architecture-history.py --base "$BASE" --head "$HEAD" - # Packaging rejects stale notices only after merge; catch the drift here, - # while the change that caused it is still open. - notices: - name: Third-party notices - # Release pull requests get their notices regenerated by release.yml, - # which then validates the prepared commit through this workflow. - if: ${{ !startsWith(github.head_ref, 'release-please--') }} + lint: + name: Lint and docs runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 45 + env: + RUSTDOCFLAGS: "-D warnings" steps: - uses: actions/checkout@v5 with: ref: ${{ inputs.ref || github.ref }} + - name: Install native dependencies + run: | + sudo apt-get update + sudo apt-get install -y build-essential pkg-config clang cmake libssl-dev \ + libfontconfig1-dev libfreetype6-dev libxkbcommon-dev libxkbcommon-x11-dev \ + libwayland-dev libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev \ + libvulkan-dev - uses: dtolnay/rust-toolchain@stable with: toolchain: "1.98.0" - - name: Check notice inputs - run: python3 scripts/generate-license-notices.py --check-inputs - - # Manifest-only and fast. --with-metadata is not used: it rewrites Cargo.lock. - unused-dependencies: - name: Unused dependencies - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v5 - with: - ref: ${{ inputs.ref || github.ref }} - - uses: dtolnay/rust-toolchain@stable + components: rustfmt, clippy + - uses: Swatinem/rust-cache@v2 with: - toolchain: "1.98.0" - - name: Install cargo-machete - run: cargo install --locked --version 0.9.2 cargo-machete - - run: cargo machete --skip-target-dir + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} + - run: cargo fmt --all --check + - name: Clippy + if: ${{ !cancelled() }} + run: cargo clippy --locked --workspace --all-targets -- -D warnings + # The user-plugin track is compiled out of default builds until it ships + # (docs/architecture/guardrails.md); every commit must also build with it. + # Only the crates the feature changes are checked again. + - name: Clippy with user plugins + if: ${{ !cancelled() }} + run: cargo clippy --locked --workspace --all-targets --features nocterm/user-plugins -- -D warnings + - name: Rustdoc + if: ${{ !cancelled() }} + run: cargo doc --locked --workspace --no-deps - rust: - name: Build, lint, tests and documentation + test: + name: Build and test runs-on: ubuntu-latest timeout-minutes: 60 env: @@ -235,12 +232,23 @@ jobs: - uses: dtolnay/rust-toolchain@stable with: toolchain: "1.98.0" - components: rustfmt, clippy - uses: Swatinem/rust-cache@v2 - - run: cargo fmt --all --check - - run: cargo build --locked --workspace --all-targets - - run: cargo clippy --locked --workspace --all-targets -- -D warnings + with: + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} + # One compilation for everything below: the tests reuse it, and the xtask binary is + # run directly, because `cargo xtask` resolves features for xtask alone and rebuilds. + - name: Build + id: build + run: cargo build --locked --workspace --all-targets + - name: Generated documents are current + if: ${{ !cancelled() && steps.build.outcome == 'success' }} + run: target/debug/xtask docs --check + - name: Architecture rules + if: ${{ !cancelled() && steps.build.outcome == 'success' }} + run: target/debug/xtask architecture - name: Run workspace tests + if: ${{ !cancelled() && steps.build.outcome == 'success' }} run: | set -o pipefail cargo test --locked --workspace 2>&1 | tee "$RUNNER_TEMP/nocterm-workspace-tests.log" @@ -258,17 +266,11 @@ jobs: escaped = chunk.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") print("::error title=Workspace test failure::" + escaped) PYTHON - - run: cargo test --manifest-path vendor/alacritty_terminal/Cargo.toml --lib - - name: Verify native menu patch - run: cargo test --manifest-path vendor/gpui-component/Cargo.toml --locked --features test-support --lib menu::app_menu_bar - - run: cargo xtask docs --check - - run: cargo xtask architecture - - run: cargo doc --locked --workspace --no-deps - # The user-plugin track is compiled out of default builds until it ships - # (docs/architecture/guardrails.md). Every commit must also build with it. + # The workspace tests above already run every plugin crate. The feature changes only the + # packages that declare it, so only they are tested again with it, against a real guest. user-plugins: - name: User plugins feature build + name: User plugins runs-on: ubuntu-latest timeout-minutes: 60 steps: @@ -289,36 +291,71 @@ jobs: targets: wasm32-unknown-unknown - uses: Swatinem/rust-cache@v2 with: - key: user-plugins + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} workspaces: | . -> target examples/plugins/focus-timer -> target - - run: cargo clippy --locked --workspace --all-targets --features nocterm/user-plugins -- -D warnings - # The example code plugin is a workspace of its own. The workspace tests below load the - # module it builds, so a Rust guest runs through the real check and runtime. + # The example code plugin is a workspace of its own. The tests below load the module + # it builds, so a Rust guest runs through the real check and runtime. - name: Build and test the example code plugin run: | cargo clippy --locked --all-targets --manifest-path examples/plugins/focus-timer/Cargo.toml -- -D warnings cargo test --locked --manifest-path examples/plugins/focus-timer/Cargo.toml cargo build --locked --release --target wasm32-unknown-unknown \ --manifest-path examples/plugins/focus-timer/Cargo.toml - - run: cargo test --locked --workspace --features nocterm/user-plugins + - name: Test the packages the feature changes env: NOCTERM_GUEST_WASM: ${{ github.workspace }}/examples/plugins/focus-timer/target/wasm32-unknown-unknown/release/focus_timer.wasm NOCTERM_REQUIRE_WASM_GUEST: "1" + run: | + packages=$(cargo metadata --locked --no-deps --format-version 1 | jq -r \ + '.packages[] | select(.features | has("user-plugins") or has("user")) | "--package=\(.name)"') + echo "Testing with user plugins:" $packages + cargo test --locked --features nocterm/user-plugins $packages - advisories: - name: Dependency advisories + vendored: + name: Vendored crates runs-on: ubuntu-latest - timeout-minutes: 15 + timeout-minutes: 30 steps: - uses: actions/checkout@v5 with: ref: ${{ inputs.ref || github.ref }} - - uses: taiki-e/install-action@v2 + - uses: actions/setup-python@v5 with: - tool: cargo-deny - - run: cargo deny check advisories + python-version: "3.12" + - name: Test archive authentication and replay failures + run: python3 scripts/test-vendor-patches.py + - name: Replay published archives and patches + if: ${{ !cancelled() }} + run: python3 scripts/check-vendor-patches.py + - name: Install native dependencies + if: ${{ !cancelled() }} + run: | + sudo apt-get update + sudo apt-get install -y build-essential pkg-config clang cmake libssl-dev \ + libfontconfig1-dev libfreetype6-dev libxkbcommon-dev libxkbcommon-x11-dev \ + libwayland-dev libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev \ + libvulkan-dev mesa-vulkan-drivers dbus + - uses: dtolnay/rust-toolchain@stable + if: ${{ !cancelled() }} + with: + toolchain: "1.98.0" + - uses: Swatinem/rust-cache@v2 + if: ${{ !cancelled() }} + with: + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} + workspaces: | + vendor/alacritty_terminal -> target + vendor/gpui-component -> target + - name: Test the vendored terminal emulator + if: ${{ !cancelled() }} + run: cargo test --manifest-path vendor/alacritty_terminal/Cargo.toml --lib + - name: Verify native menu patch + if: ${{ !cancelled() }} + run: cargo test --manifest-path vendor/gpui-component/Cargo.toml --locked --features test-support --lib menu::app_menu_bar device-unlock-native: name: Device unlock on ${{ matrix.os }} @@ -337,6 +374,9 @@ jobs: toolchain: "1.98.0" components: clippy - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} - name: Compile and link native adapters run: cargo build --locked -p nocterm-vault-store -p nocterm-device-unlock --all-targets - name: Lint native adapters @@ -350,7 +390,7 @@ jobs: ci-passed: name: CI passed if: always() - needs: [agent-guardrails, plugin-api-compat, release-pipeline, conventions, integration-history, notices, unused-dependencies, rust, user-plugins, advisories, device-unlock-native, vendor-patches] + needs: [policy, conventions, integration-history, lint, test, user-plugins, vendored, device-unlock-native] runs-on: ubuntu-latest timeout-minutes: 5 steps: diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index bec49539..68fbb182 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -1,10 +1,10 @@ name: Package # Builds installers: Windows (Inno Setup), Linux .deb, .rpm and AppImage. -# Runs on master pushes, manually, or from release.yml for release attachments. +# Runs manually, or from release.yml: once to validate a prepared release commit and +# once to attach the installers to the published release. A push to master builds no +# installers of its own; release.yml already builds them for that commit. on: - push: - branches: [master] workflow_dispatch: workflow_call: inputs: @@ -36,7 +36,9 @@ jobs: toolchain: "1.98.0" - uses: Swatinem/rust-cache@v2 - name: Install notice generator - run: cargo install --locked --version 0.9.2 --features cli cargo-about + uses: taiki-e/install-action@v2 + with: + tool: cargo-about@0.9.2 - name: Verify notices run: python3 scripts/generate-license-notices.py --check - name: Reject tracked release artifacts @@ -49,9 +51,9 @@ jobs: raise SystemExit("Release artifacts must not be tracked: " + ", ".join(artifacts)) PYTHON + # Installers build while the notices are checked; only the upload waits for both. linux: name: Linux packages - needs: licenses # The oldest supported runner keeps the glibc requirement low. runs-on: ubuntu-22.04 timeout-minutes: 60 @@ -71,8 +73,11 @@ jobs: toolchain: "1.98.0" - uses: Swatinem/rust-cache@v2 - name: Install packaging tools + uses: taiki-e/install-action@v2 + with: + tool: cargo-deb,cargo-generate-rpm + - name: Install AppImage tool run: | - cargo install --locked cargo-deb cargo-generate-rpm mkdir -p "$HOME/.local/bin" curl -fsSL -o "$HOME/.local/bin/appimagetool" \ https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage @@ -90,7 +95,6 @@ jobs: windows: name: Windows installer - needs: licenses runs-on: windows-latest timeout-minutes: 60 steps: @@ -122,7 +126,7 @@ jobs: upload: name: Attach to release if: inputs.tag != '' - needs: [linux, windows] + needs: [licenses, linux, windows] runs-on: ubuntu-latest timeout-minutes: 10 steps: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 139e3b6a..4b596db0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,7 +42,9 @@ jobs: if: steps.release.outputs.pr != '' - name: Install notice generator if: steps.release.outputs.pr != '' - run: cargo install --locked --version 0.9.2 --features cli cargo-about + uses: taiki-e/install-action@v2 + with: + tool: cargo-about@0.9.2 - name: Prepare release notices if: steps.release.outputs.pr != '' id: prepare diff --git a/.gitignore b/.gitignore index 04aad6fc..1f70ac52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ # Rust / Cargo target/ +/target-contract-msrv/ +# Per-worktree Cargo target directories (target-) +/target-*/ **/*.rs.bk *.pdb @@ -66,3 +69,23 @@ ehthumbs.db .claude/worktrees/ .claude/plans/ mcp.db + +# Playwright MCP session logs and snapshots +.playwright-mcp/ + +# Local data, dumps and transcripts that must never be published +*.sqlite +*.sqlite3 +*.db +*.dump +*.p12 +*.pfx +id_rsa* +id_ecdsa* +id_ed25519* +!crates/ssh-keys/src/tests/fixtures/** +.claude/*.local.* +.claude/reviews/ +CLAUDE.local.md +.aider* +.cursor/ diff --git a/AGENTS.md b/AGENTS.md index af31d82f..e1f4b657 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,201 +1,137 @@ # Agent guide -Instructions for AI agents working in this repository. Humans: see [CONTRIBUTING.md](CONTRIBUTING.md). +Follow [CONTRIBUTING.md](CONTRIBUTING.md) for Git, PR and vendor rules. ## Hard rules -- Follow [CONTRIBUTING.md](CONTRIBUTING.md): Conventional Commits, `/` branches, never commit to `master` or `dev` directly. Record the target (`master` release / `dev` integration) and branch from it. -- Never edit `CHANGELOG.md`, `version.txt`, `.release-please-manifest.json` or create tags — releases are automated ([docs/RELEASING.md](docs/RELEASING.md)). -- Never bypass hooks (`--no-verify`) or weaken a check to make it pass. -- Run every Cargo command with `CARGO_BUILD_JOBS=2` and serialize top-level Cargo - commands, including those from other agents, to stay within available memory: - prefix them with `.claude/hooks/cargo-serial.sh`, which does both. -- Commit, push and open PRs only when the user asks. -- Report verification honestly: what was run, what passed, what wasn't checked. -- Keep Rust files at or under 800 lines ([CONTRIBUTING.md](CONTRIBUTING.md#code-size)): split a - growing file into child modules by responsibility instead of adding to it. Never add an - entry to `xtask/oversized-files.toml`. +- Record the target (`master` release / `dev` integration); branch from it using + `/`. Never commit directly to `master` or `dev`. +- Commit, push and open PRs only when the user asks; use Conventional Commits. +- Never edit `CHANGELOG.md`, `version.txt`, `.release-please-manifest.json` or + create tags: releases are automated. Never bypass hooks or weaken checks. +- Rust files stay at or under 800 lines, tests included. This is a ceiling, not + a target; never add an entry to `xtask/oversized-files.toml`. +- Report actual verification and its limits. A structural check does not prove + correct ownership, rendering or performance. ## Read first -Before designing or changing code, read [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md), -the layer note of each touched crate, the [ADR index](docs/adr/README.md) and relevant -accepted decisions, and the crate's `//!` documentation in `lib.rs`. -Accepted ADRs are immutable; supersede a decision with a new ADR. +Read [the layer table](docs/ARCHITECTURE.md#layers), the affected crate's `//!` +card and relevant sections of its layer note. Use the [ADR index](docs/adr/README.md) +to find current decisions for the changed contracts, including superseding records. +Expand reading when dependencies or ownership remain unclear; an incidental crate +mention does not require reading every historical ADR. Accepted ADRs are immutable. ## Where code goes -Name the crate and layer in the plan, in this order: +Name the crate/layer and responsibility before non-trivial changes: -1. Rules and data formats without I/O belong in domain crates. -2. Real filesystem, network and process I/O behind contracts belongs in adapters. -3. GPUI entities with background work and no views belong in service crates. -4. Views and presentation state belong in feature crates. -5. Features communicate through narrow workspace ports named for the consumer's - need; do not expand a broad trait for another feature pair. +1. Pure rules and formats: domain; reusable low-level primitives: foundation. +2. Real filesystem, network and process I/O behind contracts: adapters. +3. GPUI entities with background work and no views: services. +4. Views and presentation: features; shared components: ui. Composition: app. +5. Features meet through narrow consumer-named ports, never feature dependencies. -If the right crate does not exist, propose it instead of using the nearest crate. -Existing mixed responsibilities are migration debt, not a pattern for new code. -Use `Composer` and `SessionState` ([ADR 0009](docs/adr/0009-thread-composer.md), -[ADR 0010](docs/adr/0010-session-lifecycle.md)), `containers` / `containers-ui`, and -`src/bootstrap.rs` as examples of ownership and composition. +Reject a candidate that violates its layer or ownership card. Reuse a fitting +crate; propose a new one if none fits. Existing mixed responsibilities are debt. +For unclear placement or extraction use [placement](.claude/skills/placement/SKILL.md) +and [responsibility-map](.claude/skills/responsibility-map/SKILL.md). + +## Design rules + +1. Keep state within one responsibility. Before adding an owner, name its state, + invariant, transitions and lifetime, including cancellation/late results. + Do not add fields above 15 or spread a parent's impl into more files. +2. Stateful types gain no `pub`/`pub(crate)` fields. DTOs/settings may expose + plain data; transitions preserve invariants and one authoritative state. +3. No new `impl Global`, reads of another crate's global, or `on_app_quit`. + Inject collaborators explicitly; compose their lifecycle in bootstrap. +4. Fallible cross-crate APIs use typed errors (`thiserror`), preserving causes; + convert to text only for display/logging. +5. No I/O in domain code or protocol-specific types (including ACP) in its APIs; + no domain rules in views. Existing violations do not authorize growth. +6. No new `#[expect]`/`#[allow]` for `clippy::too_many_lines` or + `clippy::cognitive_complexity`. `xtask/baseline.toml` may only shrink; + lower entries when debt shrinks. See [guardrails](docs/architecture/guardrails.md). +7. Keep files focused and easy to read. Separate independent responsibilities + when they emerge, even below warning thresholds. Extract state with its + transitions, or cohesive pure functions; never split just by line count, + invent empty owners, or fill a file to its limit. Shared types remain with + their owner. Keep vendor and application changes in separate commits. + +Warnings (640 lines, 12 fields, four impl files) invite inspection, not automatic +extraction. Compare plausible alternatives for consequential decisions: correctness, +reuse, runtime/memory cost and maintenance. Choose the simplest adequate solution; +avoid speculative frameworks and an exhaustive search for a theoretical optimum. ## Reuse before writing -- Ordered persistence: `nocterm_store::{Persisted, ShutdownCoordinator}` - ([ADR 0013](docs/adr/0013-persistence-shutdown-service.md)); the common service - uses `nocterm_core::persist::{WriteQueue, WriteGate, ShutdownDeadline}` - ([ADR 0004](docs/adr/0004-write-queue.md)). -- Atomic files: `nocterm_core::fs::{write_private_atomic, create_private, -AtomicPublication}` for private data; `nocterm_core::persist::{save, save_text, -save_preserving}` for configuration. Check their symlink and format semantics - before reuse ([ADR 0015](docs/adr/0015-private-file-publication.md)). -- Shutdown, link-safe filesystem traversal and test fakes: inspect existing - implementations before adding one; propose shared extraction for missing reuse. - -Layout, settings, snippets, connection state, Runtime chat/favorites and output -recording use the shared persistence service; keyed documents and append streams -share its driver. Native filesystem capabilities are -`nocterm-session::local_fs::{LocalFs, LocalMutations}`, implemented by the injected -`nocterm-localfs` adapter. Session `test-support` supplies shared in-memory port -fakes for dev-dependencies. `ProgramOpener` requests external local programs; the -injected `nocterm-local::LocalProgramOpener` owns their bounded native launch/wait -lifecycle. `nocterm-local::fs` is not an API. +Search for an existing capability and check its semantics before adding one: -## Design rules +- Persistence: `nocterm_store::{Persisted, ShutdownCoordinator}` and its keyed/append + owners; lower-level ordering: `nocterm_core::persist::{WriteQueue, WriteGate, ShutdownDeadline}`. +- Private files: `nocterm_core::fs::{write_private_atomic, create_private, AtomicPublication}`; + configuration: `nocterm_core::persist::{save, save_text, save_preserving}`. + Check [symlink/publication semantics](docs/adr/0015-private-file-publication.md). +- Native files: `nocterm-session::local_fs::{LocalFs, LocalMutations}` via + `nocterm-localfs`; programs: `ProgramOpener` via `nocterm-local::LocalProgramOpener`. + `nocterm-local::fs` is not an API. Reuse shutdown, traversal and shared port fakes. + +Use [port-design](.claude/skills/port-design/SKILL.md) for contracts and +[wiring](.claude/skills/wiring/SKILL.md) for retained installation/persistence. + +## Performance -These rules block review. `cargo xtask architecture` checks layers, file size, -complexity attributes and the exact debt inventory: field counts, public state, -impl spread, globals and panicking reads, domain I/O, quit handlers and string -errors. See [architecture guardrails](docs/architecture/guardrails.md) and -[ADR 0012](docs/adr/0012-architecture-debt-ratchets.md). Entries in -`xtask/baseline.toml` may only decrease; update them when debt shrinks. - -1. Add state only within a type's single responsibility. Do not add a field when - the resulting count exceeds 15 or create a type over that limit; extract an owner with - private fields and transition methods. Do not spread its impl across more files. -2. Stateful types gain no `pub` or `pub(crate)` fields. DTOs and settings may expose - plain data; methods preserve stateful invariants. -3. No new `impl Global`, reads of another crate's global, or `on_app_quit` handlers. - Pass collaborators through explicit dependencies and constructors; compose - them in bootstrap. Existing globals do not authorize new service lookup. -4. Fallible APIs crossing crate boundaries use typed errors (`thiserror`); - convert them to `String` at display only. -5. Add no I/O to domain code or protocol-specific types (including ACP) to its APIs, and no - domain rules to views. Existing violations do not authorize growth. -6. Never add `#[expect]` or `#[allow]` for `clippy::too_many_lines` or - `clippy::cognitive_complexity`, and never raise a baseline to pass a check. -7. Extract responsibilities into modules that own types, not extra methods of a - parent type. Keep vendor changes separate from application code changes. +For changed hot paths, assess frequency × work per event and retained memory. +Keep blocking I/O and expensive computation off render/input callbacks. Remove +redundant work, repeated parsing/scans and unchanged-state notifications; use bounded, +coalesced background work with cancellation. Check the affected visible behavior. +Cache worthwhile repeated work with an explicit owner, complete key, invalidation, +memory bound and teardown; never use a cache as authorization or a second source +of truth. Follow [performance](.claude/skills/performance/SKILL.md) for rendering, +cache and cost evidence. Measure representative before/after cases for optimization +claims; describe unmeasured expectations honestly. Routine edits need no benchmark suite. ## Tests -- Test behavior through actions, events, rendering and public methods; do not - read or write private view/model fields to reproduce a regression. -- Name tests and files after behavior, and extend its existing test file. Do not - create files named after review rounds (`audit`, `verification`, `round2`, etc.). -- Reuse existing port fakes and extend their capabilities. If they cannot be - shared yet, propose a shared home instead of duplicating another fake. +Test through public behavior, actions, events and rendering, not private view/model +fields. Extend behavior-named tests; reuse/extend shared port fakes. Refactors keep +assertions and observable behavior; mechanical import/fixture moves are allowed. +Use [behavior-tests](.claude/skills/behavior-tests/SKILL.md) for coverage and examples. ## Documents -- `docs/` describes the current system. Plans, progress, session logs and audit - trails belong in ignored `.claude/plans/` or the PR description. -- Never record session authorization or implementation status in `docs/`. -- When removing or renaming a symbol, search `docs/` and update current references; - accepted ADRs retain historical context. -- Cross-cutting decisions get an ADR. Architecture notes describe layers, ports - and invariants; product instructions belong in user documentation. -- For new crates, start `lib.rs` with a short `//!` card: purpose, `Owns:`, - `Does not own:`, ports used/provided, settings section, globals and invariants. - Describe actual boundaries; existing crates gain cards when their boundaries change. - -## Pipeline - -Non-trivial work goes through four stages. Each stage is a subagent in `.claude/agents/`; the main session orchestrates (run it with `/feature `). - -| # | Stage | Agent | Writes code | Output | -| --- | ------ | ------------- | ----------- | ------------------------------------------------- | -| 1 | Design | `architect` | plan only | plan: files, steps, risks, test strategy | -| 2 | Build | `implementer` | yes | code + tests on a feature branch | -| 3 | Verify | `tester` | tests only | test run results, added coverage, reproduced bugs | -| 4 | Review | `reviewer` | plan only | findings ranked by severity, verdict | - -Gates: - -- **Design → Build**: the user approves the plan. -- **Verify → Review**: build, lint and tests are green. -- **Review → Done**: no blocking findings. Blocking findings go back to `implementer`; after two failed rounds, stop and escalate to the user. - -Trivial changes (typos, one-line fixes, docs) skip the pipeline. - -`/feature` routes a task to a workflow skill (`crate-new`, `crate-extend`, -`refactor`, `crate-split`, `debt-paydown`) built from procedure skills that each -agent preloads. Stages share decisions through `.claude/plans//` -([plan contract](.claude/skills/feature/plan-bus.md)). Committed hooks in -`.claude/settings.json` enforce the hard rules and stage roles -([ADR 0059](docs/adr/0059-architecture-procedure-skills.md)); CI stays authoritative. -Pass the target branch and verification scope to every stage. Review includes -uncommitted changes as well as commits so it can finish before commit authorization. +Keep `docs/` about the current system; plans, progress and audit trails go in ignored +`.claude/plans/`. Do not record session authorization/status in `docs/`. +Update current references when symbols move; accepted ADRs keep historical context. +Cross-cutting decisions get an ADR. New crates start with a short `//!` ownership +card; update existing cards when boundaries change. Details: [docs-adr](.claude/skills/docs-adr/SKILL.md). + +## Workflow + +Use one agent and one compact ignored plan for ordinary non-trivial work: goal, +branch/base, affected owners/layers, approach, risks and checks. Trivial edits need +no plan. Load only procedures relevant to the task; output filenames are optional. +Delegate independent work when complexity/risk warrants it; supply scope and evidence. +A request to implement a plan authorizes routine decisions within it. Ask only for +material unresolved choices or changes of scope/authority. Review committed, +staged, unstaged and relevant untracked task changes using [design-review](.claude/skills/design-review/SKILL.md). +Fix actionable findings; escalate concrete blockers or repeated lack of progress. +No blocking findings may remain at completion. Preserve unrelated work. + +## Verification policy + +Use [gate](.claude/skills/gate/SKILL.md) to select checks by changed inputs and +consumers. Check coherent edits, not each moved method; a test already compiles +its target. Reuse results while relevant source/configuration stays unchanged. +For parallel work, use one verification executor and do not edit checked inputs +while checks run. Keep stable Cargo targets; investigate before clearing caches. +Cargo uses local settings; the coordination helper is optional (CONTRIBUTING.md). ## Definition of done -Code changes require `cargo fmt --all --check`, -`cargo build --workspace --all-targets`, -`cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`, -`cargo xtask architecture` and `cargo xtask docs --check` to pass. Run additional -checks below when relevant (for example mandatory SSH/SFTP tests for SSH changes). Changes to -user plugins (`nocterm-plugin-*`, `plugins-ui`, `src/shell_setup/user_plugin*`) also run -`cargo clippy --workspace --all-targets --features nocterm/user-plugins -- -D warnings` and -`cargo test --workspace --features nocterm/user-plugins`: the default build compiles the feature out. -Changes to the plugin SDK or the example (`nocterm-plugin-sdk`, `examples/plugins/`) also build and -test the example and run the gated guest test (see Commands). -For documentation-only changes, check links, stale references and generated-doc -freshness; architecture checks still apply. Build, lint and runtime tests may be -omitted because executable inputs did not change. - -Report each command and its result. Report omitted or unavailable checks with the -reason. No blocking review findings may remain; commit, push and PR creation -still require the user's request. - -## Scopes - -A commit scope is the crate directory name under `crates/` (`agent-runtime`, -`connections-model`, …). Special scopes: `app` (root `src/`), `xtask`, `deps`. - -## Commands - -```sh -# Agents prefix each cargo command with .claude/hooks/cargo-serial.sh. -export CARGO_BUILD_JOBS=2 -cargo run -cargo fmt --all --check -cargo build --workspace --all-targets -cargo clippy --workspace --all-targets -- -D warnings -cargo test --workspace -cargo clippy --workspace --all-targets --features nocterm/user-plugins -- -D warnings -cargo test --workspace --features nocterm/user-plugins -cargo test --locked --manifest-path examples/plugins/focus-timer/Cargo.toml -cargo build --locked --release --target wasm32-unknown-unknown --manifest-path examples/plugins/focus-timer/Cargo.toml -NOCTERM_GUEST_WASM=$PWD/examples/plugins/focus-timer/target/wasm32-unknown-unknown/release/focus_timer.wasm NOCTERM_REQUIRE_WASM_GUEST=1 cargo test -p nocterm --features nocterm/user-plugins user_plugin -NOCTERM_REQUIRE_SSHD=1 cargo test -p nocterm-ssh --test sshd -cargo xtask docs -cargo xtask docs --check -cargo xtask architecture -cargo xtask survey [--json] [--type ] -.claude/hooks/cargo-serial.sh cargo -python3 scripts/test-claude-hooks.py -python3 scripts/check-claude-config.py -python3 scripts/check-plugin-api-compat.py -cargo machete --skip-target-dir -cargo doc --workspace --no-deps -j1 -python3 scripts/generate-license-notices.py --check-inputs -``` - -Linux system dependencies and setup are in [README.md](README.md). GUI checks -require an X11 or Wayland session and Vulkan driver. Regenerate references after -changing schemas, tokens, actions, keymap or internal Cargo dependencies. -Regenerate `THIRD_PARTY_NOTICES.txt` (`python3 scripts/generate-license-notices.py`, -needs `cargo-about` 0.9.2) after changing dependencies, vendored manifests or license -files, or bundled assets; CI and the pre-push hook reject stale notices -([docs/PACKAGING.md](docs/PACKAGING.md)). +After review fixes, finish the [completion matrix and applicable fixtures](.claude/skills/gate/SKILL.md#definition-of-done). +Broad shared changes, uncertain workspace impact or an explicit full-verification +request require the full suite there. Required CI and limits remain unchanged. +Report actual commands/results, scope and omitted checks with reasons; a scoped +pass is not a workspace pass. Commit, push and PR still require the user's request. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 83a0f1f1..0f590776 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,10 +87,13 @@ Pick the type by what the *user of the software* sees, not by which files change ## Code size A Rust source file may have at most 800 lines, tests included. `cargo xtask architecture` -(run in CI) enforces it. When a file approaches the limit, split it by responsibility into -child modules (`foo.rs` + `foo/part.rs`); move a large `mod tests` into `foo/tests.rs`. -For production code, an extracted module owns a responsibility and its own type; -moving more methods of the same growing type into another file is not an extraction. +(run in CI) enforces this ceiling; it is not a target size. Keep files focused on +one coherent responsibility. Separate independent responsibilities as they emerge, +even below warning thresholds, into named child modules; move a large `mod tests` +into `foo/tests.rs`. Stateful extractions own their state and transitions; pure +logic can be a cohesive module of functions without an artificial owner type. +Moving more methods of the same growing type into another file is not an extraction. +Do not fragment a coherent module just to meet an arbitrary preferred line count. The legacy exception list `xtask/oversized-files.toml` is empty; never add an entry to make a check pass. @@ -111,6 +114,51 @@ constrains large structs, implementation spread, public state, globals, shutdown hooks, string errors and domain I/O. Lower or remove entries when code improves; never regenerate or raise the inventory. See [architecture guardrails](docs/architecture/guardrails.md). +## Verification + +During development, choose the smallest check that answers the current question: +one regression test, one test target, a library or an affected package. Batch a +coherent edit before checking it. Do not run a fixed build/lint/test/architecture +bundle after every edit or extraction. A test compiles its target; running both +`cargo check` and `cargo build` first is usually redundant. + +At completion, follow the input-based [completion matrix](.claude/skills/gate/SKILL.md#definition-of-done). +Scoped changes use affected targets and consumers; broad shared changes or +uncertain workspace impact need full workspace verification. Required CI is +independent. Reuse valid results and rerun a failing check first; keep evidence +tied to its source/configuration. The [gate procedure](.claude/skills/gate/SKILL.md) +provides examples. Documentation and script-only work use relevant links, +script tests and configuration validators unless they affect Rust/generator inputs. + +For changed hot paths, account for work per event, frequency and retained memory. +Rendering and input callbacks consume prepared state; expensive work has bounded +background execution. Cache repeated work only with clear keys, invalidation, +bounds and lifecycle. Verify affected interaction and measure optimization claims +with representative before/after workloads; see the [performance procedure](.claude/skills/performance/SKILL.md). + +### Cargo resources + +Cargo keeps the caller's `CARGO_BUILD_JOBS`, Cargo configuration and normal +defaults. Choose a local budget for the machine; no fixed job count is required. +When parallel builds need coordination, the optional helper serializes commands: + +```sh +.claude/hooks/cargo-serial.sh cargo test -p nocterm-ssh +``` + +Its default lock lives in the Git common directory, so linked worktrees share it +and independent clones do not. `NOCTERM_CARGO_LOCK=` selects another lock +(use an absolute path to coordinate different working directories or repositories). +`NOCTERM_CARGO_LOCK=off` runs without the helper lock. The helper needs `flock` +when locking; if unavailable it fails promptly with instructions. Direct Cargo +is supported on every platform and keeps Cargo's own target-directory locking. + +For shared verification, coordinate one executor and avoid duplicate checks on +the same inputs. Keep a stable target directory per worktree; do not touch all +sources or clean caches to start another check. Investigate a concrete stale +artifact first. Local resource settings may be supplied in the shell or personal +configuration and do not belong in committed product rules. + ## Pull requests Vendored forks have reproducible binary patches against checksum-pinned published diff --git a/Cargo.lock b/Cargo.lock index 591b52d3..6e5181cb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -236,7 +236,7 @@ dependencies = [ "rustix-openpty", "serde", "signal-hook", - "unicode-width", + "unicode-width 0.2.2", "vte", "windows-sys 0.59.0", ] @@ -282,7 +282,7 @@ checksum = "f211a51805bc641f3ad5b7664c77d2547af685cc33b4cd8d31964027a46f13f1" dependencies = [ "anstyle", "memchr", - "unicode-width", + "unicode-width 0.2.2", ] [[package]] @@ -434,9 +434,9 @@ dependencies = [ [[package]] name = "async-executor" -version = "1.14.0" +version = "1.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c96bf972d85afc50bf5ab8fe2d54d1586b4e0b46c97c50a0c9e71e2f7bcd812a" +checksum = "f426a31126c735126297bc5ea7a521263c9faff4f63ca415267ee625651c4f37" dependencies = [ "async-task", "concurrent-queue", @@ -1251,7 +1251,7 @@ checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" dependencies = [ "serde", "termcolor", - "unicode-width", + "unicode-width 0.2.2", ] [[package]] @@ -1581,9 +1581,9 @@ checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "crypto-bigint" -version = "0.7.5" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a52aa3fcda4e6302a9f48734f234d35d4721b96f8fe07d073f07ce9df4f0271" +checksum = "914c195c4d7fbc5ed7f88d14cbf85914d5c664967658d43404b263913a477e82" dependencies = [ "cpubits", "ctutils", @@ -1648,9 +1648,9 @@ dependencies = [ [[package]] name = "ctutils" -version = "0.4.2" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5515a3834141de9eafb9717ad39eea8247b5674e6066c404e8c4b365d2a29e" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" dependencies = [ "cmov", "subtle", @@ -3774,6 +3774,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "hybrid-array" version = "0.4.15" @@ -3800,6 +3806,7 @@ dependencies = [ "http", "http-body", "httparse", + "httpdate", "itoa", "pin-project-lite", "smallvec", @@ -3821,7 +3828,20 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", - "webpki-roots 1.0.9", + "webpki-roots 1.0.11", +] + +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", ] [[package]] @@ -4535,6 +4555,38 @@ dependencies = [ "value-bag", ] +[[package]] +name = "logos" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2c55a318a87600ea870ff8c2012148b44bf18b74fad48d0f835c38c7d07c5f" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-codegen" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58b3ffaa284e1350d017a57d04ada118c4583cf260c8fb01e0fe28a2e9cf8970" +dependencies = [ + "fnv", + "proc-macro2", + "quote", + "regex-automata", + "regex-syntax", + "syn 2.0.119", +] + +[[package]] +name = "logos-derive" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52d3a9855747c17eaf4383823f135220716ab49bea5fbea7dd42cc9a92f8aa31" +dependencies = [ + "logos-codegen", +] + [[package]] name = "loop9" version = "0.1.5" @@ -4763,6 +4815,28 @@ dependencies = [ "paste", ] +[[package]] +name = "miette" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" +dependencies = [ + "cfg-if", + "miette-derive", + "unicode-width 0.1.14", +] + +[[package]] +name = "miette-derive" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "mime" version = "0.3.17" @@ -4861,6 +4935,12 @@ dependencies = [ "pxfm", ] +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + [[package]] name = "multiversion_no_op" version = "1.0.0" @@ -4955,11 +5035,12 @@ dependencies = [ "nocterm-agent-runtime", "nocterm-agent-settings-ui", "nocterm-agent-storage", - "nocterm-agent-usage", + "nocterm-agent-tools", "nocterm-ai", "nocterm-connections", "nocterm-connections-io", "nocterm-connections-model", + "nocterm-connections-store", "nocterm-containers", "nocterm-containers-ui", "nocterm-core", @@ -4968,6 +5049,12 @@ dependencies = [ "nocterm-directory-io", "nocterm-explorer", "nocterm-files", + "nocterm-hub-agents", + "nocterm-hub-agents-model", + "nocterm-hub-io", + "nocterm-hubs", + "nocterm-hubs-model", + "nocterm-hubs-ui", "nocterm-image-codec", "nocterm-keymap", "nocterm-keymap-ui", @@ -4980,6 +5067,7 @@ dependencies = [ "nocterm-plugin-enablement", "nocterm-plugin-host", "nocterm-plugin-model", + "nocterm-plugin-net", "nocterm-plugin-packages", "nocterm-plugin-ports", "nocterm-plugin-view", @@ -5001,6 +5089,7 @@ dependencies = [ "nocterm-snippets-store", "nocterm-snippets-ui", "nocterm-ssh", + "nocterm-ssh-keys", "nocterm-store", "nocterm-terminal", "nocterm-theme-service", @@ -5018,6 +5107,7 @@ dependencies = [ "serde_json", "tempfile", "thiserror 2.0.21", + "tokio", "toml 1.1.6+spec-1.1.0", "tracing", "tracing-subscriber", @@ -5063,14 +5153,16 @@ dependencies = [ "nocterm-agent-providers", "nocterm-agent-runtime", "nocterm-ai", + "nocterm-hub-agents", + "nocterm-hubs-model", "nocterm-plugin-model", "nocterm-project-model", "nocterm-session", "nocterm-store", + "nocterm-transfers", "nocterm-ui", "nocterm-workspace", "nocterm-workspace-ports", - "serde", "serde_json", "tempfile", "thiserror 2.0.21", @@ -5081,15 +5173,16 @@ dependencies = [ name = "nocterm-agent-chat" version = "0.0.0" dependencies = [ - "async-channel", "futures", "gpui-pre", "nocterm-agent-directory", "nocterm-agent-runtime", "nocterm-ai", "nocterm-core", + "nocterm-hubs-model", "nocterm-session", "nocterm-store", + "nocterm-transfers", "nocterm-workspace-ports", "serde", "serde_json", @@ -5104,6 +5197,7 @@ version = "0.0.0" dependencies = [ "gpui-pre", "nocterm-ai", + "nocterm-hubs-model", "nocterm-store", ] @@ -5125,7 +5219,10 @@ dependencies = [ "nocterm-agent-providers", "nocterm-ai", "nocterm-core", + "nocterm-hubs-model", + "nocterm-session", "nocterm-store", + "nocterm-transfers", "serde_json", "tempfile", "thiserror 2.0.21", @@ -5155,21 +5252,18 @@ dependencies = [ "nocterm-ai", "nocterm-core", "nocterm-session", - "serde_json", "tempfile", "toml 1.1.6+spec-1.1.0", "uuid", ] [[package]] -name = "nocterm-agent-usage" +name = "nocterm-agent-tools" version = "0.0.0" dependencies = [ + "async-channel", "futures", - "gpui-pre-reqwest", - "nocterm-ai", - "serde_json", - "tempfile", + "nocterm-session", ] [[package]] @@ -5180,14 +5274,17 @@ dependencies = [ "base64 0.22.1", "futures", "nocterm-core", + "nocterm-hubs-model", "nocterm-project-model", "nocterm-session", "nocterm-settings", + "nocterm-transfers", "regex", "schemars 1.2.2", "serde", "serde_json", "serde_with", + "sha2 0.10.9", "tempfile", "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", @@ -5205,12 +5302,14 @@ dependencies = [ "nocterm-plugin-model", "nocterm-project-model", "nocterm-session", + "nocterm-ssh-keys", "nocterm-store", "nocterm-ui", "nocterm-workspace", "nocterm-workspace-ports", "serde_json", "tempfile", + "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", ] @@ -5234,6 +5333,7 @@ dependencies = [ name = "nocterm-connections-model" version = "0.0.0" dependencies = [ + "futures", "nocterm-core", "nocterm-session", "serde", @@ -5256,6 +5356,7 @@ dependencies = [ "nocterm-store", "schemars 1.2.2", "serde", + "serde_json", "tempfile", "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", @@ -5273,6 +5374,16 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "nocterm-containers-service" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-containers", + "nocterm-session", +] + [[package]] name = "nocterm-containers-ui" version = "0.0.0" @@ -5280,6 +5391,7 @@ dependencies = [ "futures", "gpui-kit", "nocterm-containers", + "nocterm-containers-service", "nocterm-plugin-model", "nocterm-session", "nocterm-settings", @@ -5296,6 +5408,7 @@ dependencies = [ "futures", "nix 0.28.0", "serde", + "sha2 0.10.9", "tempfile", "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", @@ -5378,6 +5491,173 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "nocterm-hub-agents" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-ai", + "nocterm-hub-agents-model", + "nocterm-hubs-model", + "nocterm-ssh-keys", + "thiserror 2.0.21", + "tracing", +] + +[[package]] +name = "nocterm-hub-agents-model" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-hubs-model", + "nocterm-session", + "thiserror 2.0.21", +] + +[[package]] +name = "nocterm-hub-client" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "base64 0.22.1", + "hex", + "hyper-util", + "nocterm-hub-grpc", + "nocterm-hub-model", + "nocterm-hub-proto", + "nocterm-keys", + "rustls", + "serde", + "sha2 0.11.0", + "thiserror 2.0.21", + "tokio", + "tokio-rustls", + "tokio-stream", + "tonic", + "tower", + "tracing", + "webpki-roots 1.0.11", + "zeroize", +] + +[[package]] +name = "nocterm-hub-grpc" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "nocterm-hub-proto", + "prost", + "prost-types", + "tonic", + "tonic-prost", + "tonic-prost-build", +] + +[[package]] +name = "nocterm-hub-io" +version = "0.0.0" +dependencies = [ + "async-channel", + "base64 0.22.1", + "futures", + "hex", + "nocterm-ai", + "nocterm-connections-model", + "nocterm-core", + "nocterm-hub-agents-model", + "nocterm-hub-client", + "nocterm-hub-proto", + "nocterm-hubs-model", + "nocterm-keys", + "nocterm-session", + "nocterm-vault", + "serde", + "serde_json", + "sha2 0.10.9", + "tempfile", + "thiserror 2.0.21", + "tokio", + "toml 1.1.6+spec-1.1.0", + "tonic", + "tracing", + "uuid", + "zeroize", +] + +[[package]] +name = "nocterm-hub-model" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "ed25519-dalek", + "hex", + "serde", + "sha2 0.11.0", + "thiserror 2.0.21", + "uuid", +] + +[[package]] +name = "nocterm-hub-proto" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "nocterm-hub-model", + "prost", + "prost-build", + "protox", +] + +[[package]] +name = "nocterm-hubs" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-core", + "nocterm-hubs-model", + "nocterm-session", + "nocterm-settings", + "nocterm-store", + "thiserror 2.0.21", + "tracing", +] + +[[package]] +name = "nocterm-hubs-model" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-core", + "nocterm-session", + "nocterm-settings", + "schemars 1.2.2", + "serde", + "thiserror 2.0.21", + "toml 1.1.6+spec-1.1.0", +] + +[[package]] +name = "nocterm-hubs-ui" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-kit", + "nocterm-core", + "nocterm-hub-agents", + "nocterm-hub-agents-model", + "nocterm-hubs", + "nocterm-hubs-model", + "nocterm-session", + "nocterm-settings", + "nocterm-ssh-keys", + "nocterm-store", + "nocterm-ui", + "nocterm-workspace", + "thiserror 2.0.21", +] + [[package]] name = "nocterm-image-codec" version = "0.0.0" @@ -5392,6 +5672,7 @@ version = "0.0.0" dependencies = [ "gpui-kit", "nocterm-core", + "nocterm-store", "serde", "tempfile", "thiserror 2.0.21", @@ -5410,6 +5691,23 @@ dependencies = [ "nocterm-ui", ] +[[package]] +name = "nocterm-keys" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "argon2", + "chacha20poly1305", + "ed25519-dalek", + "getrandom 0.4.3", + "hex", + "hkdf 0.13.0", + "nocterm-hub-model", + "sha2 0.11.0", + "thiserror 2.0.21", + "zeroize", +] + [[package]] name = "nocterm-layout-ui" version = "0.0.0" @@ -5431,12 +5729,14 @@ version = "0.0.0" dependencies = [ "async-channel", "chrono", + "filedescriptor", "futures", "nix 0.28.0", "nocterm-core", "nocterm-session", "portable-pty", "process-wrap", + "rustix 1.1.5", "tempfile", "thiserror 2.0.21", "tracing", @@ -5470,6 +5770,16 @@ dependencies = [ "toml 1.1.6+spec-1.1.0", ] +[[package]] +name = "nocterm-monitor-service" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-monitor", + "nocterm-session", +] + [[package]] name = "nocterm-monitor-ui" version = "0.0.0" @@ -5478,6 +5788,7 @@ dependencies = [ "gpui-kit", "nocterm-containers", "nocterm-monitor", + "nocterm-monitor-service", "nocterm-plugin-api", "nocterm-plugin-enablement", "nocterm-plugin-model", @@ -5548,6 +5859,15 @@ version = "0.0.0" dependencies = [ "serde", "thiserror 2.0.21", + "unicode-general-category", +] + +[[package]] +name = "nocterm-plugin-net" +version = "0.0.0" +dependencies = [ + "gpui-pre-reqwest", + "nocterm-plugin-api", ] [[package]] @@ -5825,9 +6145,11 @@ name = "nocterm-ssh" version = "0.0.0" dependencies = [ "futures", + "getrandom 0.4.3", "hmac 0.13.0", "nocterm-core", "nocterm-session", + "nocterm-ssh-keys", "russh", "russh-sftp", "rustix 1.1.5", @@ -5835,11 +6157,23 @@ dependencies = [ "tempfile", "thiserror 2.0.21", "tokio", + "tokio-socks", "tracing", "uuid", "zeroize", ] +[[package]] +name = "nocterm-ssh-keys" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-core", + "nocterm-session", + "ssh-key", + "thiserror 2.0.21", +] + [[package]] name = "nocterm-store" version = "0.0.0" @@ -5859,13 +6193,14 @@ dependencies = [ name = "nocterm-terminal" version = "0.0.0" dependencies = [ - "encoding_rs", "futures", "gpui-kit", + "nocterm-hubs-model", "nocterm-project-model", "nocterm-project-providers", "nocterm-session", "nocterm-settings", + "nocterm-ssh-keys", "nocterm-store", "nocterm-terminal-model", "nocterm-theme-service", @@ -6032,6 +6367,7 @@ dependencies = [ "gpui-kit", "nocterm-session", "nocterm-settings", + "nocterm-ssh-keys", "nocterm-store", "nocterm-ui", "nocterm-vault", @@ -6044,6 +6380,8 @@ name = "nocterm-vt" version = "0.0.0" dependencies = [ "alacritty_terminal", + "encoding_rs", + "nocterm-session", "regex", "regex-syntax", "thiserror 2.0.21", @@ -6094,6 +6432,7 @@ version = "0.0.0" dependencies = [ "gpui-pre", "nocterm-core", + "nocterm-hubs-model", "nocterm-project-model", "nocterm-session", "thiserror 2.0.21", @@ -6969,6 +7308,17 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "petgraph" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" +dependencies = [ + "fixedbitset", + "hashbrown 0.15.5", + "indexmap 2.14.2", +] + [[package]] name = "phc" version = "0.6.1" @@ -7451,6 +7801,118 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "prost" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528ac67416ff8646872a3c02cad9cc4ee5dc9f9540c9b10771855c95cb2e5ae1" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" +dependencies = [ + "heck 0.5.0", + "itertools 0.14.0", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "pulldown-cmark", + "pulldown-cmark-to-cmark", + "regex", + "syn 2.0.119", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" +dependencies = [ + "anyhow", + "itertools 0.14.0", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "prost-reflect" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01b80ea363c31af2de2b92e3c07ed1156628f7838c4afb4df75ee78a37fedbd1" +dependencies = [ + "logos", + "miette", + "prost", + "prost-types", +] + +[[package]] +name = "prost-types" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f94967dc7688f3054c7fac87473ffae4cc4c3904800e2d9f5b857246d8963b0a" +dependencies = [ + "prost", +] + +[[package]] +name = "protox" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43240d7f22cf77d1625885e547acf874dd63cd2790f29f4a38e86256f41996f" +dependencies = [ + "bytes", + "miette", + "prost", + "prost-reflect", + "prost-types", + "protox-parse", + "thiserror 2.0.21", +] + +[[package]] +name = "protox-parse" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34efee6fad0cf6d71642d20a66bfe0c281919c9577e7c66d20360dfcb7549272" +dependencies = [ + "logos", + "miette", + "prost-types", + "thiserror 2.0.21", +] + +[[package]] +name = "pulldown-cmark" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db2be46835f013dd3f4fa7f5f7402c537787b54ebb6fe2b94bbdd7e2669bb7b" +dependencies = [ + "bitflags 2.13.2", + "memchr", + "unicase", +] + +[[package]] +name = "pulldown-cmark-to-cmark" +version = "22.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84bbb29c624230c4bd1047bbdb2aa47e41c860e9665ce62ba9504eebe91bf867" +dependencies = [ + "pulldown-cmark", +] + [[package]] name = "pulp" version = "0.22.3" @@ -8279,10 +8741,11 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.45" +version = "0.23.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ + "log", "once_cell", "ring", "rustls-pki-types", @@ -8324,9 +8787,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.103.15" +version = "0.103.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281" dependencies = [ "ring", "rustls-pki-types", @@ -9658,9 +10121,9 @@ checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" [[package]] name = "tokio" -version = "1.53.1" +version = "1.54.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed" +checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" dependencies = [ "bytes", "libc", @@ -9706,6 +10169,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", + "tokio-util", +] + [[package]] name = "tokio-util" version = "0.7.19" @@ -9814,6 +10289,74 @@ version = "1.1.2+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" +[[package]] +name = "tonic" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ea7e687c08008d22518056608ad0c1cc721204d30ed48c8439a88ae5b9fc73" +dependencies = [ + "async-trait", + "base64 0.22.1", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "socket2", + "sync_wrapper", + "tokio", + "tokio-stream", + "tokio-util", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic-build" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "040c96f2ebcbe22118dfa258310dad07002ccf92139370cf69532e4047c7d400" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tonic-prost" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fe9efdab8bb668f5ef526e21c247c8cb658853a754983cb70f1dac3fc4f462a" +dependencies = [ + "bytes", + "prost", + "tonic", +] + +[[package]] +name = "tonic-prost-build" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b0e0e10c4980146d75d5acfe4f7a9822278130ca2025c0dc25c82f7307d63ac" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "prost-types", + "quote", + "syn 2.0.119", + "tempfile", + "tonic-build", +] + [[package]] name = "tower" version = "0.5.3" @@ -9822,11 +10365,15 @@ checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" dependencies = [ "futures-core", "futures-util", + "indexmap 2.14.2", "pin-project-lite", + "slab", "sync_wrapper", "tokio", + "tokio-util", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -10152,6 +10699,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce61d488bcdc9bc8b5d1772c404828b17fc481c0a582b5581e95fb233aef503e" +[[package]] +name = "unicode-general-category" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24adfe8311434967077a6adff125729161e6e4934d76f6b7c55318ac5c9246d3" + [[package]] name = "unicode-id" version = "0.3.7" @@ -10194,6 +10747,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "unicode-width" version = "0.2.2" @@ -10616,7 +11175,7 @@ dependencies = [ "bumpalo", "leb128fmt", "memchr", - "unicode-width", + "unicode-width 0.2.2", "wasm-encoder", ] @@ -10754,14 +11313,14 @@ version = "0.26.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" dependencies = [ - "webpki-roots 1.0.9", + "webpki-roots 1.0.11", ] [[package]] name = "webpki-roots" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +checksum = "f5f280bbb2b287bc8e7de7e3852945a0ff50d1bbcb5ed42b6c7a568c546df171" dependencies = [ "rustls-pki-types", ] @@ -11796,6 +12355,7 @@ dependencies = [ "nocterm-ai", "nocterm-design", "nocterm-explorer", + "nocterm-hubs-model", "nocterm-monitor", "nocterm-plugin-abi", "nocterm-plugin-api", diff --git a/Cargo.toml b/Cargo.toml index 9467c43a..320a911c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ nocterm-ai.workspace = true nocterm-agent-providers.workspace = true nocterm-image-codec.workspace = true nocterm-agent-storage.workspace = true -nocterm-agent-usage.workspace = true nocterm-agent-runtime.workspace = true +nocterm-agent-tools.workspace = true nocterm-acp.workspace = true nocterm-agent.workspace = true nocterm-agent-settings-ui.workspace = true @@ -23,6 +23,7 @@ nocterm-device-unlock.workspace = true nocterm-connections.workspace = true nocterm-connections-model.workspace = true nocterm-connections-io.workspace = true +nocterm-connections-store.workspace = true nocterm-containers-ui.workspace = true nocterm-containers.workspace = true nocterm-snippets-ui.workspace = true @@ -45,6 +46,12 @@ nocterm-project-environment.workspace = true nocterm-project-container-fs.workspace = true nocterm-directory-io.workspace = true nocterm-explorer.workspace = true +nocterm-hub-io.workspace = true +nocterm-hub-agents.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-hubs-model.workspace = true +nocterm-hubs.workspace = true +nocterm-hubs-ui.workspace = true nocterm-project-service.workspace = true nocterm-project-storage.workspace = true nocterm-projects.workspace = true @@ -53,6 +60,7 @@ nocterm-settings.workspace = true nocterm-store.workspace = true nocterm-settings-ui.workspace = true nocterm-ssh.workspace = true +nocterm-ssh-keys.workspace = true nocterm-terminal.workspace = true nocterm-transfers.workspace = true nocterm-transfer-runtime.workspace = true @@ -67,6 +75,7 @@ nocterm-plugin-model.workspace = true nocterm-plugin-api = { workspace = true, optional = true } nocterm-plugin-host = { workspace = true, optional = true } nocterm-plugin-packages = { workspace = true, optional = true } +nocterm-plugin-net = { workspace = true, optional = true } nocterm-plugin-ports = { workspace = true, optional = true } nocterm-plugin-view = { workspace = true, optional = true } nocterm-plugin-wasm = { workspace = true, optional = true } @@ -90,6 +99,7 @@ user-plugins = [ "dep:nocterm-plugin-api", "dep:nocterm-plugin-host", "dep:nocterm-plugin-packages", + "dep:nocterm-plugin-net", "dep:nocterm-plugin-ports", "dep:nocterm-plugin-view", "dep:nocterm-plugin-wasm", @@ -164,6 +174,8 @@ openssh-clients = "*" winresource = "0.1" [dev-dependencies] +nocterm-plugin-ports = { workspace = true, features = ["test-support"] } +nocterm-connections-store = { workspace = true, features = ["test-support"] } async-io.workspace = true nocterm-monitor.workspace = true nocterm-window-layout.workspace = true @@ -172,15 +184,19 @@ uuid.workspace = true nocterm-store = { workspace = true, features = ["test-support"] } nocterm-snippets = { workspace = true, features = ["test-support"] } nocterm-connections-model = { workspace = true, features = ["test-support"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } nocterm-ai = { workspace = true, features = ["test-support"] } nocterm-device-unlock = { workspace = true, features = ["test-support"] } nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } +nocterm-vault = { workspace = true, features = ["test-support"] } nocterm-workspace = { workspace = true, features = ["test-support"] } nocterm-project-model = { workspace = true, features = ["test-support"] } nocterm-transfers = { workspace = true, features = ["test-support"] } nocterm-themes = { workspace = true, features = ["test-support"] } serde_json.workspace = true tempfile.workspace = true +tokio.workspace = true # Writes the guest modules that the plugin check tests run (user-plugins builds only). wat.workspace = true gpui-kit = { workspace = true, features = ["test-support"] } @@ -221,9 +237,9 @@ nocterm-agent-directory = { path = "crates/agent-directory" } nocterm-agent-settings-ui = { path = "crates/agent-settings-ui" } nocterm-workspace-ports = { path = "crates/workspace-ports" } nocterm-agent-chat = { path = "crates/agent-chat" } +nocterm-agent-tools = { path = "crates/agent-tools" } nocterm-image-codec = { path = "crates/image-codec" } nocterm-agent-storage = { path = "crates/agent-storage" } -nocterm-agent-usage = { path = "crates/agent-usage" } nocterm-acp = { path = "crates/acp" } nocterm-agent = { path = "crates/agent" } nocterm-agent-runtime = { path = "crates/agent-runtime" } @@ -235,6 +251,7 @@ nocterm-connections-store = { path = "crates/connections-store" } nocterm-connections-model = { path = "crates/connections-model" } nocterm-connections-io = { path = "crates/connections-io" } nocterm-containers = { path = "crates/containers" } +nocterm-containers-service = { path = "crates/containers-service" } nocterm-containers-ui = { path = "crates/containers-ui" } nocterm-snippets = { path = "crates/snippets" } nocterm-snippets-ui = { path = "crates/snippets-ui" } @@ -243,12 +260,19 @@ nocterm-snippets-io = { path = "crates/snippets-io" } nocterm-core = { path = "crates/core" } nocterm-design = { path = "crates/design" } nocterm-files = { path = "crates/files" } +nocterm-hubs-model = { path = "crates/hubs-model" } +nocterm-hub-agents-model = { path = "crates/hub-agents-model" } +nocterm-hub-agents = { path = "crates/hub-agents" } +nocterm-hub-io = { path = "crates/hub-io" } +nocterm-hubs = { path = "crates/hubs" } +nocterm-hubs-ui = { path = "crates/hubs-ui" } nocterm-keymap = { path = "crates/keymap" } nocterm-keymap-ui = { path = "crates/keymap-ui" } nocterm-layout-ui = { path = "crates/layout-ui" } nocterm-local = { path = "crates/local" } nocterm-localfs = { path = "crates/localfs" } nocterm-monitor = { path = "crates/monitor" } +nocterm-monitor-service = { path = "crates/monitor-service" } nocterm-monitor-ui = { path = "crates/monitor-ui" } nocterm-plugin-abi = { path = "crates/plugin-abi" } nocterm-plugin-wasm = { path = "crates/plugin-wasm" } @@ -256,6 +280,7 @@ nocterm-plugin-api = { path = "crates/plugin-api" } nocterm-plugin-enablement = { path = "crates/plugin-enablement" } nocterm-plugin-host = { path = "crates/plugin-host" } nocterm-plugin-packages = { path = "crates/plugin-packages" } +nocterm-plugin-net = { path = "crates/plugin-net" } nocterm-plugin-ports = { path = "crates/plugin-ports" } nocterm-plugin-view = { path = "crates/plugin-view" } nocterm-plugin-model = { path = "crates/plugin-model" } @@ -265,6 +290,7 @@ nocterm-settings = { path = "crates/settings" } nocterm-store = { path = "crates/store" } nocterm-settings-ui = { path = "crates/settings-ui" } nocterm-ssh = { path = "crates/ssh" } +nocterm-ssh-keys = { path = "crates/ssh-keys" } nocterm-terminal = { path = "crates/terminal" } nocterm-transfers = { path = "crates/transfers" } nocterm-transfer-runtime = { path = "crates/transfer-runtime" } @@ -278,10 +304,16 @@ nocterm-workspace = { path = "crates/workspace" } nocterm-vault = { path = "crates/vault" } nocterm-vault-store = { path = "crates/vault-store" } nocterm-vault-ui = { path = "crates/vault-ui" } +# The hub contract lives outside this workspace (ADR 0096); only nocterm-hub-io may use it. +nocterm-hub-client = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } +nocterm-hub-model = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } +nocterm-hub-proto = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } +nocterm-keys = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } agent-client-protocol = "=2.2.0" agent-client-protocol-schema = { version = "=1.9.1", features = ["unstable_session_fork", "unstable_end_turn_token_usage"] } base64 = "0.22" +hex = "0.4.3" async-io = "2" async-process = "2" @@ -300,17 +332,21 @@ anyhow = "1" async-channel = "2" directories = "6" futures = "0.3" +sha2 = "0.10.9" gpui-kit = "0.7" # GPUI alone, for crates that must not reach the component library. gpui = { package = "gpui-pre", version = "0.3.7", default-features = false } parking_lot = "0.12" portable-pty = "0.9" +prost = "0.14.4" regex = "1" # Zed's reqwest fork, already in the tree through GPUI. reqwest = { package = "gpui-pre-reqwest", version = "0.12", default-features = false, features = ["blocking", "rustls-tls"] } regex-syntax = "0.8" russh = { version = "0.63", default-features = false, features = ["ring", "flate2", "rsa"] } russh-sftp = "3" +# Pinned to the version `russh` resolves, so key text round-trips between the two (ADR 0102). +ssh-key = { version = "=0.7.0-rc.11", default-features = false, features = ["ed25519", "p256", "p384", "p521", "rsa", "encryption"] } schemars = "1" serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -324,6 +360,7 @@ tokio = { version = "1", features = ["rt-multi-thread", "net", "time", "sync", " toml = "1" toml_edit = "0.25" tracing = "0.1" +tonic = { version = "0.14", default-features = false } # Writes the guest modules the wasm tests run. Dev only: the host reads binary modules. wat = "1.262" tracing-subscriber = { version = "0.3", features = ["env-filter"] } @@ -372,6 +409,11 @@ wasmi_core = { opt-level = 3 } wasmi_ir = { opt-level = 3 } wasmparser = { opt-level = 3 } postcard = { opt-level = 3 } +# `cargo xtask architecture` parses every source file with syn several times; unoptimised, +# that made it the slowest check. Optimised syn also expands derive macros faster. +proc-macro2 = { opt-level = 3 } +syn = { opt-level = 3 } +xtask = { opt-level = 1 } [profile.release] codegen-units = 1 diff --git a/README.md b/README.md index 0785e2d1..3156012d 100644 --- a/README.md +++ b/README.md @@ -342,12 +342,18 @@ cargo build --workspace --all-targets cargo clippy --workspace --all-targets -- -D warnings cargo test --workspace NOCTERM_REQUIRE_SSHD=1 cargo test -p nocterm-ssh --test sshd +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm-hub-io --test hub +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm hub_live cargo xtask docs cargo xtask docs --check cargo xtask architecture cargo doc --workspace --no-deps ``` +The hub tests (the adapter and `hub_live`, which drives the windows) need a running hub (`docker compose up` in the server's repository) and +`NOCTERM_HUB_ADDRESS`, `NOCTERM_HUB_FINGERPRINT`, `NOCTERM_HUB_USERNAME` and `NOCTERM_HUB_PASSWORD`. +Without `NOCTERM_REQUIRE_HUB=1` it passes when they are missing; with it, missing variables or a +missing hub fail the run. The SSH tests start private local OpenSSH servers with temporary keys. With `NOCTERM_REQUIRE_SSHD=1`, missing `sshd` fails instead of skipping the tests. Some OpenSSH installations need `/run/sshd` created by the system administrator. @@ -373,6 +379,14 @@ for tests; the native shutdown diagnostic still needs an upstream reproduction. The terminal emulator is a maintained local patch of alacritty_terminal 0.26.0. See [the patch notes](vendor/alacritty_terminal/NOCTERM.md) before updating it. +## Hubs + +Sign in to a self-hosted Nocterm server with **Hubs: Sign in to server** in the command palette +(or from the Servers settings page), verify the certificate fingerprint and sign in. The hub's +terminal sessions are listed in the Sessions panel of any window, and keep running when you +close their tabs. +See [hubs](docs/HUBS.md). + ## AI agents The right AI panel supports ACP agents Claude, Codex, Hermes and custom diff --git a/THIRD_PARTY_NOTICES.txt b/THIRD_PARTY_NOTICES.txt index 8b8024cd..ded084f5 100644 --- a/THIRD_PARTY_NOTICES.txt +++ b/THIRD_PARTY_NOTICES.txt @@ -2,9 +2,9 @@ Nocterm third-party licenses and notices ======================================== Generated by scripts/generate-license-notices.py using cargo-about 0.9.2. -Content SHA-256: 3c6f1e5a1ca72407d4ef079c38fcc465f76d50df77cc20e858c9b93e2e4485b3 -Input fingerprint: f108f9fc1f5ca654b4c445a28bf1e608c9f7ce01f6ce434a2474d22bdf833ecf -Cargo.lock SHA-256 (release version masked): 342a7922e45f873e4ba5580b112b7716bcfe715c1af98dd47e88ac8727128603 +Content SHA-256: 3cc524fae167d3e7f55f6133dc4c3b1acdcb53209ef087c91d5d69e8c04a52b7 +Input fingerprint: 14ece3bbf27e860bd598da338189d40d7acd1779e8f3719df669e45dafd05674 +Cargo.lock SHA-256 (release version masked): 2b2bf6b37fd5a884b37b99d97b196e2f5f4989e0e95914a9a09dd70dae34c1d8 Scope: runtime and build dependencies of Nocterm and its optional vault broker for Linux x86_64, Windows x86_64, macOS x86_64 and macOS arm64. @@ -190,9 +190,9 @@ Authors: Wim Looman ; Allen Bui Source: https://crates.io/api/v1/crates/async-compression/0.4.50/download Repository: https://github.com/Nullus157/async-compression -async-executor 1.14.0: Apache-2.0 OR MIT +async-executor 1.14.1: Apache-2.0 OR MIT Authors: Stjepan Glavina ; John Nunley -Source: https://crates.io/api/v1/crates/async-executor/1.14.0/download +Source: https://crates.io/api/v1/crates/async-executor/1.14.1/download Repository: https://github.com/smol-rs/async-executor async-fs 2.2.0: Apache-2.0 OR MIT @@ -649,9 +649,9 @@ Authors: Eira Fransham Source: https://crates.io/api/v1/crates/crunchy/0.2.4/download Repository: https://github.com/eira-fransham/crunchy -crypto-bigint 0.7.5: Apache-2.0 OR MIT +crypto-bigint 0.7.11: Apache-2.0 OR MIT Authors: RustCrypto Developers -Source: https://crates.io/api/v1/crates/crypto-bigint/0.7.5/download +Source: https://crates.io/api/v1/crates/crypto-bigint/0.7.11/download Repository: https://github.com/RustCrypto/crypto-bigint crypto-common 0.1.7: MIT OR Apache-2.0 @@ -678,9 +678,9 @@ Authors: RustCrypto Developers Source: https://crates.io/api/v1/crates/ctr/0.10.1/download Repository: https://github.com/RustCrypto/block-modes -ctutils 0.4.2: Apache-2.0 OR MIT +ctutils 0.4.8: Apache-2.0 OR MIT Authors: RustCrypto Developers -Source: https://crates.io/api/v1/crates/ctutils/0.4.2/download +Source: https://crates.io/api/v1/crates/ctutils/0.4.8/download Repository: https://github.com/RustCrypto/utils cursor-icon 1.2.0: MIT OR Apache-2.0 OR Zlib @@ -1008,6 +1008,11 @@ Authors: Joshua Barretto Source: https://crates.io/api/v1/crates/flume/0.12.0/download Repository: https://github.com/zesterer/flume +fnv 1.0.7: Apache-2.0 OR MIT +Authors: Alex Crichton +Source: https://crates.io/api/v1/crates/fnv/1.0.7/download +Repository: https://github.com/servo/rust-fnv + foldhash 0.1.5: Zlib Authors: Orson Peters Source: https://crates.io/api/v1/crates/foldhash/0.1.5/download @@ -1334,6 +1339,11 @@ Authors: Sean Bowe ; Jack Grigg Source: https://crates.io/api/v1/crates/group/0.14.0/download Repository: https://github.com/zkcrypto/group +h2 0.4.19: MIT +Authors: Carl Lerche ; Sean McArthur +Source: https://crates.io/api/v1/crates/h2/0.4.19/download +Repository: https://github.com/hyperium/h2 + half 2.7.1: MIT OR Apache-2.0 Authors: Kathryn Long Source: https://crates.io/api/v1/crates/half/2.7.1/download @@ -1446,6 +1456,11 @@ Authors: Sean McArthur Source: https://crates.io/api/v1/crates/httparse/1.10.1/download Repository: https://github.com/seanmonstar/httparse +httpdate 1.0.3: MIT OR Apache-2.0 +Authors: Pyfisch +Source: https://crates.io/api/v1/crates/httpdate/1.0.3/download +Repository: https://github.com/pyfisch/httpdate + hybrid-array 0.4.15: MIT OR Apache-2.0 Authors: RustCrypto Developers Source: https://crates.io/api/v1/crates/hybrid-array/0.4.15/download @@ -1460,6 +1475,11 @@ hyper-rustls 0.27.10: Apache-2.0 OR ISC OR MIT Source: https://crates.io/api/v1/crates/hyper-rustls/0.27.10/download Repository: https://github.com/rustls/hyper-rustls +hyper-timeout 0.5.2: MIT OR Apache-2.0 +Authors: Herman J. Radtke III +Source: https://crates.io/api/v1/crates/hyper-timeout/0.5.2/download +Repository: https://github.com/hjr3/hyper-timeout + hyper-util 0.1.21: MIT Authors: Sean McArthur Source: https://crates.io/api/v1/crates/hyper-util/0.1.21/download @@ -1729,6 +1749,21 @@ Authors: The Rust Project Developers Source: https://crates.io/api/v1/crates/log/0.4.34/download Repository: https://github.com/rust-lang/log +logos 0.16.1: MIT OR Apache-2.0 +Authors: Maciej Hirsz ; Jérome Eertmans (maintainer) +Source: https://crates.io/api/v1/crates/logos/0.16.1/download +Repository: https://github.com/maciejhirsz/logos + +logos-codegen 0.16.1: MIT OR Apache-2.0 +Authors: Maciej Hirsz ; Jérome Eertmans (maintainer) +Source: https://crates.io/api/v1/crates/logos-codegen/0.16.1/download +Repository: https://github.com/maciejhirsz/logos + +logos-derive 0.16.1: MIT OR Apache-2.0 +Authors: Maciej Hirsz ; Jérome Eertmans (maintainer) +Source: https://crates.io/api/v1/crates/logos-derive/0.16.1/download +Repository: https://github.com/maciejhirsz/logos + lsp-types 0.97.0: MIT Authors: Markus Westerlind ; Bruno Medeiros Source: https://crates.io/api/v1/crates/lsp-types/0.97.0/download @@ -1828,6 +1863,16 @@ Authors: gfx-rs developers Source: https://crates.io/api/v1/crates/metal/0.33.0/download Repository: https://github.com/gfx-rs/metal-rs +miette 7.6.0: Apache-2.0 +Authors: Kat Marchán +Source: https://crates.io/api/v1/crates/miette/7.6.0/download +Repository: https://github.com/zkat/miette + +miette-derive 7.6.0: Apache-2.0 +Authors: Kat Marchán +Source: https://crates.io/api/v1/crates/miette-derive/7.6.0/download +Repository: https://github.com/zkat/miette + mime 0.3.17: MIT OR Apache-2.0 Authors: Sean McArthur Source: https://crates.io/api/v1/crates/mime/0.3.17/download @@ -1876,6 +1921,11 @@ Authors: Radzivon Bartoshyk Source: https://crates.io/api/v1/crates/moxcms/0.8.1/download Repository: https://github.com/awxkee/moxcms.git +multimap 0.10.1: MIT OR Apache-2.0 +Authors: Håvar Nøvik +Source: https://crates.io/api/v1/crates/multimap/0.10.1/download +Repository: https://github.com/havarnov/multimap + multiversion_no_op 1.0.0: Apache-2.0 OR MIT Authors: Henri Sivonen Source: https://crates.io/api/v1/crates/multiversion_no_op/1.0.0/download @@ -2196,6 +2246,11 @@ Authors: The rust-url developers Source: https://crates.io/api/v1/crates/percent-encoding/2.3.2/download Repository: https://github.com/servo/rust-url/ +petgraph 0.8.3: MIT OR Apache-2.0 +Authors: bluss; mitchmindtree +Source: https://crates.io/api/v1/crates/petgraph/0.8.3/download +Repository: https://github.com/petgraph/petgraph + phc 0.6.1: Apache-2.0 OR MIT Authors: RustCrypto Developers Source: https://crates.io/api/v1/crates/phc/0.6.1/download @@ -2397,6 +2452,51 @@ Authors: Philip Degarmo Source: https://crates.io/api/v1/crates/profiling-procmacros/1.0.18/download Repository: https://github.com/aclysma/profiling +prost 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +prost-build 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost-build/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +prost-derive 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost-derive/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +prost-reflect 0.16.5: MIT OR Apache-2.0 +Authors: Andrew Hickman +Source: https://crates.io/api/v1/crates/prost-reflect/0.16.5/download +Repository: https://github.com/andrewhickman/prost-reflect + +prost-types 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost-types/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +protox 0.10.0: MIT OR Apache-2.0 +Authors: Andrew Hickman +Source: https://crates.io/api/v1/crates/protox/0.10.0/download +Repository: https://github.com/andrewhickman/protox + +protox-parse 0.10.0: MIT OR Apache-2.0 +Authors: Andrew Hickman +Source: https://crates.io/api/v1/crates/protox-parse/0.10.0/download +Repository: https://github.com/andrewhickman/protox + +pulldown-cmark 0.13.6: MIT +Authors: Raph Levien ; Marcus Klaas de Vries +Source: https://crates.io/api/v1/crates/pulldown-cmark/0.13.6/download +Repository: https://github.com/raphlinus/pulldown-cmark + +pulldown-cmark-to-cmark 22.0.3: Apache-2.0 +Authors: Sebastian Thiel ; Dylan Owen ; Alessandro Ogier ; Zixian Cai <2891235+caizixian@users.noreply.github.com>; Andrew Lyjak +Source: https://crates.io/api/v1/crates/pulldown-cmark-to-cmark/22.0.3/download +Repository: https://github.com/Byron/pulldown-cmark-to-cmark + pulp 0.22.3: MIT Authors: sarah quiñones Source: https://crates.io/api/v1/crates/pulp/0.22.3/download @@ -2663,8 +2763,8 @@ Authors: Dan Gohman Source: https://crates.io/api/v1/crates/rustix-openpty/0.2.0/download Repository: https://github.com/sunfishcode/rustix-openpty -rustls 0.23.45: Apache-2.0 OR ISC OR MIT -Source: https://crates.io/api/v1/crates/rustls/0.23.45/download +rustls 0.23.46: Apache-2.0 OR ISC OR MIT +Source: https://crates.io/api/v1/crates/rustls/0.23.46/download Repository: https://github.com/rustls/rustls rustls-pemfile 2.2.0: Apache-2.0 OR ISC OR MIT @@ -2675,8 +2775,8 @@ rustls-pki-types 1.15.1: MIT OR Apache-2.0 Source: https://crates.io/api/v1/crates/rustls-pki-types/1.15.1/download Repository: https://github.com/rustls/pki-types -rustls-webpki 0.103.15: ISC -Source: https://crates.io/api/v1/crates/rustls-webpki/0.103.15/download +rustls-webpki 0.103.16: ISC +Source: https://crates.io/api/v1/crates/rustls-webpki/0.103.16/download Repository: https://github.com/rustls/webpki rustversion 1.0.23: MIT OR Apache-2.0 @@ -3209,9 +3309,9 @@ Authors: Lokathor Source: https://crates.io/api/v1/crates/tinyvec/1.13.3/download Repository: https://github.com/Lokathor/tinyvec -tokio 1.53.1: MIT +tokio 1.54.1: MIT Authors: Tokio Contributors -Source: https://crates.io/api/v1/crates/tokio/1.53.1/download +Source: https://crates.io/api/v1/crates/tokio/1.54.1/download Repository: https://github.com/tokio-rs/tokio tokio-macros 2.7.2: MIT @@ -3223,6 +3323,16 @@ tokio-rustls 0.26.6: MIT OR Apache-2.0 Source: https://crates.io/api/v1/crates/tokio-rustls/0.26.6/download Repository: https://github.com/rustls/tokio-rustls +tokio-socks 0.5.3: MIT +Authors: Yilin Chen +Source: https://crates.io/api/v1/crates/tokio-socks/0.5.3/download +Repository: https://github.com/sticnarf/tokio-socks + +tokio-stream 0.1.19: MIT +Authors: Tokio Contributors +Source: https://crates.io/api/v1/crates/tokio-stream/0.1.19/download +Repository: https://github.com/tokio-rs/tokio + tokio-util 0.7.19: MIT Authors: Tokio Contributors Source: https://crates.io/api/v1/crates/tokio-util/0.7.19/download @@ -3264,6 +3374,26 @@ toml_writer 1.1.2+spec-1.1.0: MIT OR Apache-2.0 Source: https://crates.io/api/v1/crates/toml_writer/1.1.2+spec-1.1.0/download Repository: https://github.com/toml-rs/toml +tonic 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + +tonic-build 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic-build/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + +tonic-prost 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic-prost/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + +tonic-prost-build 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic-prost-build/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + tower 0.5.3: MIT Authors: Tower Maintainers Source: https://crates.io/api/v1/crates/tower/0.5.3/download @@ -3438,6 +3568,11 @@ Authors: Yevhenii Reizner Source: https://crates.io/api/v1/crates/unicode-ccc/0.4.0/download Repository: https://github.com/RazrFalcon/unicode-ccc +unicode-general-category 1.0.0: Apache-2.0 +Authors: YesLogic Pty. Ltd. +Source: https://crates.io/api/v1/crates/unicode-general-category/1.0.0/download +Repository: https://github.com/yeslogic/unicode-general-category + unicode-id 0.3.7: MIT OR Apache-2.0 Authors: Boshen ; erick.tryzelaar ; kwantam ; Manish Goregaokar Source: https://crates.io/api/v1/crates/unicode-id/0.3.7/download @@ -3473,6 +3608,11 @@ Authors: Evgeniy Reizner Source: https://crates.io/api/v1/crates/unicode-vo/0.1.0/download Repository: https://github.com/RazrFalcon/unicode-vo +unicode-width 0.1.14: MIT OR Apache-2.0 +Authors: kwantam ; Manish Goregaokar +Source: https://crates.io/api/v1/crates/unicode-width/0.1.14/download +Repository: https://github.com/unicode-rs/unicode-width + unicode-width 0.2.2: MIT OR Apache-2.0 Authors: kwantam ; Manish Goregaokar Source: https://crates.io/api/v1/crates/unicode-width/0.2.2/download @@ -3613,8 +3753,8 @@ webpki-roots 0.26.11: CDLA-Permissive-2.0 Source: https://crates.io/api/v1/crates/webpki-roots/0.26.11/download Repository: https://github.com/rustls/webpki-roots -webpki-roots 1.0.9: CDLA-Permissive-2.0 -Source: https://crates.io/api/v1/crates/webpki-roots/1.0.9/download +webpki-roots 1.0.11: CDLA-Permissive-2.0 +Source: https://crates.io/api/v1/crates/webpki-roots/1.0.11/download Repository: https://github.com/rustls/webpki-roots weezl 0.1.12: MIT OR Apache-2.0 @@ -4197,9 +4337,9 @@ The `cfg_aliases!` macro uses a lot of the code from [`tectonic_cfg_support::tar ======================================================================== License: CDLA-Permissive-2.0 -Used by: webpki-roots 0.26.11, webpki-roots 1.0.9 +Used by: webpki-roots 0.26.11, webpki-roots 1.0.11 Original notice: webpki-roots 0.26.11 / LICENSE -Original notice: webpki-roots 1.0.9 / LICENSE +Original notice: webpki-roots 1.0.11 / LICENSE # Community Data License Agreement - Permissive - Version 2.0 @@ -4660,6 +4800,521 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { return 0; } +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Codegen exports used by `tonic-build`. + +pub use async_trait::async_trait; +pub use tokio_stream; + +pub use std::future::Future; +pub use std::pin::Pin; +pub use std::sync::Arc; +pub use std::task::{Context, Poll}; +pub use tower_service::Service; +pub type StdError = Box; +pub use crate::codec::{CompressionEncoding, EnabledCompressionEncodings}; +pub use crate::extensions::GrpcMethod; +pub use crate::service::interceptor::InterceptedService; +pub use bytes::Bytes; +pub use http; +pub use http_body::Body; + +pub type BoxFuture = self::Pin> + Send + 'static>>; +pub type BoxStream = + self::Pin> + Send + 'static>>; + +======================================================================== +License: MIT +Used by: tonic-prost 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Prost codec implementation for tonic. +//! +//! This crate provides the [`ProstCodec`] for encoding and decoding protobuf +//! messages using the [`prost`] library. +//! +//! # Example +//! +//! ```rust,ignore +//! use tonic_prost::ProstCodec; +//! +//! let codec = ProstCodec::::default(); +//! ``` + +#![warn( + missing_docs, + missing_debug_implementations, + rust_2018_idioms, + unreachable_pub +)] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/tokio-rs/website/master/public/img/icons/tonic.svg" +)] +#![doc(html_root_url = "https://docs.rs/tonic-prost/0.13.1")] +#![doc(issue_tracker_base_url = "https://github.com/grpc/grpc-rust/issues/")] + +mod codec; + +pub use codec::{ProstCodec, ProstDecoder, ProstEncoder}; + +// Re-export prost types that users might need +pub use prost; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Utilities for using Tower services with Tonic. + +pub mod interceptor; +pub(crate) mod layered; +#[cfg(feature = "router")] +pub(crate) mod router; + +#[doc(inline)] +pub use self::interceptor::{Interceptor, InterceptorLayer}; +pub use self::layered::{LayerExt, Layered}; +#[doc(inline)] +#[cfg(feature = "router")] +pub use self::router::{Routes, RoutesBuilder}; +#[cfg(feature = "router")] +pub use axum::{Router as AxumRouter, body::Body as AxumBody}; + +pub mod recover_error; +pub use self::recover_error::{RecoverError, RecoverErrorLayer}; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Various utilities used throughout tonic. + +// some combinations of features might cause things here not to be used +#![allow(dead_code)] + +pub(crate) mod base64 { + use base64::{ + alphabet, + engine::{ + DecodePaddingMode, + general_purpose::{GeneralPurpose, GeneralPurposeConfig}, + }, + }; + + pub(crate) const STANDARD: GeneralPurpose = GeneralPurpose::new( + &alphabet::STANDARD, + GeneralPurposeConfig::new() + .with_encode_padding(true) + .with_decode_padding_mode(DecodePaddingMode::Indifferent), + ); + + pub(crate) const STANDARD_NO_PAD: GeneralPurpose = GeneralPurpose::new( + &alphabet::STANDARD, + GeneralPurposeConfig::new() + .with_encode_padding(false) + .with_decode_padding_mode(DecodePaddingMode::Indifferent), + ); +} + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +/// A gRPC Method info extension. +#[derive(Debug, Clone)] +pub struct GrpcMethod<'a> { + service: &'a str, + method: &'a str, +} + +impl<'a> GrpcMethod<'a> { + /// Create a new `GrpcMethod` extension. + #[doc(hidden)] + pub fn new(service: &'a str, method: &'a str) -> Self { + Self { service, method } + } + + /// gRPC service name + pub fn service(&self) -> &str { + self.service + } + /// gRPC method name + pub fn method(&self) -> &str { + self.method + } +} + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +mod add_origin; +use self::add_origin::AddOrigin; + +mod user_agent; +use self::user_agent::UserAgent; + +mod reconnect; +use self::reconnect::Reconnect; + +mod connection; +pub(super) use self::connection::Connection; + +mod discover; +pub use self::discover::Change; +pub(super) use self::discover::DynamicServiceStream; + +mod io; +use self::io::BoxedIo; + +mod connector; +pub(crate) use self::connector::Connector; + +mod executor; +pub(super) use self::executor::{Executor, SharedExec}; + +#[cfg(feature = "_tls-any")] +mod tls; +#[cfg(feature = "_tls-any")] +pub(super) use self::tls::TlsConnector; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +mod io; +pub(crate) use self::io::{ConnectInfoLayer, ServerIo}; + +#[cfg(feature = "_tls-any")] +mod tls; +#[cfg(feature = "_tls-any")] +pub(crate) use self::tls::TlsAcceptor; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +pub(crate) mod grpc_timeout; +#[cfg(feature = "_tls-any")] +pub(crate) mod tls; + +pub(crate) use self::grpc_timeout::GrpcTimeout; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +use crate::transport::channel::BoxFuture; +use hyper_util::rt::TokioExecutor; +use std::{future::Future, sync::Arc}; + +pub(crate) use hyper::rt::Executor; + +#[derive(Clone)] +pub(crate) struct SharedExec { + inner: Arc> + Send + Sync + 'static>, +} + +impl SharedExec { + pub(crate) fn new(exec: E) -> Self + where + E: Executor> + Send + Sync + 'static, + { + Self { + inner: Arc::new(exec), + } + } + + pub(crate) fn tokio() -> Self { + Self::new(TokioExecutor::new()) + } +} + +impl Executor for SharedExec +where + F: Future + Send + 'static, +{ + fn execute(&self, fut: F) { + self.inner.execute(Box::pin(fut)) + } +} + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +use super::Connected; +use std::sync::Arc; + +/// Connection info for Unix domain socket streams. +/// +/// This type will be accessible through [request extensions][ext] if you're using +/// a unix stream. +/// +/// See [Connected] for more details. +/// +/// [ext]: crate::Request::extensions +#[derive(Clone, Debug)] +pub struct UdsConnectInfo { + /// Peer address. This will be "unnamed" for client unix sockets. + pub peer_addr: Option>, + /// Process credentials for the unix socket. + pub peer_cred: Option, +} + +impl Connected for tokio::net::UnixStream { + type ConnectInfo = UdsConnectInfo; + + fn connect_info(&self) -> Self::ConnectInfo { + UdsConnectInfo { + peer_addr: self.peer_addr().ok().map(Arc::new), + peer_cred: self.peer_cred().ok(), + } + } +} + ======================================================================== License: MIT Used by: freetype-sys 0.20.1 @@ -6504,6 +7159,241 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +======================================================================== +License: Apache-2.0 +Used by: miette-derive 7.6.0 +Original notice: miette-derive 7.6.0 / LICENSE + +Apache License + +Version 2.0, January 2004 + +http://www.apache.org/licenses/ TERMS +AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + + + + "License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + + + + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + + + + "Legal Entity" shall mean the +union of the acting entity and all other entities that control, are controlled +by, or are under common control with that entity. For the purposes of this +definition, "control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or otherwise, or (ii) +ownership of fifty percent (50%) or more of the outstanding shares, or (iii) +beneficial ownership of such entity. + + + + "You" (or "Your") shall mean +an individual or Legal Entity exercising permissions granted by this License. + + + + + "Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation source, and +configuration files. + + + + "Object" form shall mean any form resulting +from mechanical transformation or translation of a Source form, including but not +limited to compiled object code, generated documentation, and conversions to +other media types. + + + + "Work" shall mean the work of authorship, +whether in Source or Object form, made available under the License, as indicated +by a copyright notice that is included in or attached to the work (an example is +provided in the Appendix below). + + + + "Derivative Works" shall mean any +work, whether in Source or Object form, that is based on (or derived from) the +Work and for which the editorial revisions, annotations, elaborations, or other +modifications represent, as a whole, an original work of authorship. For the +purposes of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, the Work +and Derivative Works thereof. + + + + "Contribution" shall mean any work +of authorship, including the original version of the Work and any modifications +or additions to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner or by an +individual or Legal Entity authorized to submit on behalf of the copyright owner. +For the purposes of this definition, "submitted" means any form of electronic, +verbal, or written communication sent to the Licensor or its representatives, +including but not limited to communication on electronic mailing lists, source +code control systems, and issue tracking systems that are managed by, or on +behalf of, the Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise designated in +writing by the copyright owner as "Not a Contribution." + + + + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of +whom a Contribution has been received by Licensor and subsequently incorporated +within the Work. + + 2. Grant of Copyright License. Subject to the terms and +conditions of this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license +to reproduce, prepare Derivative Works of, publicly display, publicly perform, +sublicense, and distribute the Work and such Derivative Works in Source or Object +form. + + 3. Grant of Patent License. Subject to the terms and conditions of this +License, each Contributor hereby grants to You a perpetual, worldwide, +non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this +section) patent license to make, have made, use, offer to sell, sell, import, and +otherwise transfer the Work, where such license applies only to those patent +claims licensable by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) with the Work to +which such Contribution(s) was submitted. If You institute patent litigation +against any entity (including a cross-claim or counterclaim in a lawsuit) +alleging that the Work or a Contribution incorporated within the Work constitutes +direct or contributory patent infringement, then any patent licenses granted to +You under this License for that Work shall terminate as of the date such +litigation is filed. + + 4. Redistribution. You may reproduce and distribute +copies of the Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You meet the following +conditions: + + (a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + + (b) You must cause any +modified files to carry prominent notices stating that You changed the files; +and + + (c) You must retain, in the Source form of any Derivative Works that +You distribute, all copyright, patent, trademark, and attribution notices from +the Source form of the Work, excluding those notices that do not pertain to any +part of the Derivative Works; and + + (d) If the Work includes a "NOTICE" text +file as part of its distribution, then any Derivative Works that You distribute +must include a readable copy of the attribution notices contained within such +NOTICE file, excluding those notices that do not pertain to any part of the +Derivative Works, in at least one of the following places: within a NOTICE text +file distributed as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, within a display +generated by the Derivative Works, if and wherever such third-party notices +normally appear. The contents of the NOTICE file are for informational purposes +only and do not modify the License. You may add Your own attribution notices +within Derivative Works that You distribute, alongside or as an addendum to the +NOTICE text from the Work, provided that such additional attribution notices +cannot be construed as modifying the License. + + You may add Your own +copyright statement to Your modifications and may provide additional or different +license terms and conditions for use, reproduction, or distribution of Your +modifications, or for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with the conditions +stated in this License. + + 5. Submission of Contributions. Unless You explicitly +state otherwise, any Contribution intentionally submitted for inclusion in the +Work by You to the Licensor shall be under the terms and conditions of this +License, without any additional terms or conditions. Notwithstanding the above, +nothing herein shall supersede or modify the terms of any separate license +agreement you may have executed with Licensor regarding such Contributions. + + +6. Trademarks. This License does not grant permission to use the trade names, +trademarks, service marks, or product names of the Licensor, except as required +for reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless +required by applicable law or agreed to in writing, Licensor provides the Work +(and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, +without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, +MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible +for determining the appropriateness of using or redistributing the Work and +assume any risks associated with Your exercise of permissions under this +License. + + 8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, unless required +by applicable law (such as deliberate and grossly negligent acts) or agreed to in +writing, shall any Contributor be liable to You for damages, including any +direct, indirect, special, incidental, or consequential damages of any character +arising as a result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, work stoppage, +computer failure or malfunction, or any and all other commercial damages or +losses), even if such Contributor has been advised of the possibility of such +damages. + + 9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, and charge a fee +for, acceptance of support, warranty, indemnity, or other liability obligations +and/or rights consistent with this License. However, in accepting such +obligations, You may act only on Your own behalf and on Your sole responsibility, +not on behalf of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability incurred by, or +claims asserted against, such Contributor by reason of your accepting any such +warranty or additional liability. END OF TERMS AND CONDITIONS + +APPENDIX: How to +apply the Apache License to your work. + +To apply the Apache License to your work, +attach the following boilerplate notice, with the fields enclosed by brackets +"[]" replaced with your own identifying information. (Don't include the +brackets!) The text should be enclosed in the appropriate comment syntax for the +file format. We also recommend that a file or class name and description of +purpose be included on the same "printed page" as the copyright notice for easier +identification within third-party archives. + +Copyright [yyyy] Kat +Marchán + +Licensed under the Apache License, Version 2.0 (the "License"); + +you may +not use this file except in compliance with the License. + +You may obtain a copy +of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by +applicable law or agreed to in writing, software + +distributed under the License +is distributed on an "AS IS" BASIS, + +WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. + +See the License for the specific language +governing permissions and + +limitations under the License. + ======================================================================== Original notice: fdeflate 0.3.7 / LICENSE-APACHE Original notice: half 2.7.1 / LICENSE-APACHE @@ -6954,6 +7844,213 @@ Apache License See the License for the specific language governing permissions and limitations under the License. +======================================================================== +License: Apache-2.0 +Used by: pulldown-cmark-to-cmark 22.0.3 +Original notice: pulldown-cmark-to-cmark 22.0.3 / LICENSE-APACHE + +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright 2018 "Sebastian Thiel ", "Dylan Owen ", "Alessandro Ogier ", "Zixian Cai <2891235+caizixian@users.noreply.github.com>" + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + ======================================================================== Original notice: xkeysym 0.2.1 / LICENSE-APACHE @@ -8395,7 +9492,7 @@ Apache License ======================================================================== License: Apache-2.0 -Used by: clang-sys 1.9.1, codespan-reporting 0.13.1, self_cell 1.3.0, unicode-linebreak 0.1.5 +Used by: clang-sys 1.9.1, codespan-reporting 0.13.1, miette 7.6.0, self_cell 1.3.0, unicode-linebreak 0.1.5 License: Apache-2.0 Used by: russh-sftp 3.0.1 Original notice: as-raw-xcb-connection 1.0.1 / LICENSE-APACHE @@ -8406,7 +9503,7 @@ Original notice: clang-sys 1.9.1 / LICENSE.txt Original notice: cmov 0.5.4 / LICENSE-APACHE Original notice: codespan-reporting 0.13.1 / LICENSE Original notice: crypto-primes 0.7.2 / LICENSE-APACHE -Original notice: ctutils 0.4.2 / LICENSE-APACHE +Original notice: ctutils 0.4.8 / LICENSE-APACHE Original notice: encoding_rs 0.8.42 / LICENSE-APACHE Original notice: flume 0.12.0 / LICENSE-APACHE Original notice: gl_generator@0.14.0 / upstream/brendanzab-gl-rs-license-cfc7749b96f6.txt @@ -8415,6 +9512,7 @@ Original notice: khronos_api@3.1.0 / upstream/brendanzab-gl-rs-license-cfc7749b9 Source: https://raw.githubusercontent.com/brendanzab/gl-rs/f150967b1c44ae888e6676f93f639ebc82771bdc/LICENSE Original notice: kurbo 0.11.3 / LICENSE-APACHE Original notice: kurbo 0.13.1 / LICENSE-APACHE +Original notice: miette 7.6.0 / LICENSE Original notice: multiversion_no_op 1.0.0 / LICENSE-APACHE Original notice: nohash-hasher 0.2.0 / LICENSE-APACHE Original notice: pageant@0.2.3 / upstream/brendanzab-gl-rs-license-cfc7749b96f6.txt @@ -9768,6 +10866,8 @@ Apache License limitations under the License. ======================================================================== +License: Apache-2.0 +Used by: unicode-general-category 1.0.0 Original notice: annotate-snippets 0.12.16 / LICENSE-APACHE Original notice: anstyle 1.0.14 / LICENSE-APACHE Original notice: bit_field 0.10.3 / LICENSE-APACHE @@ -9796,6 +10896,7 @@ Original notice: toml_edit 0.25.15+spec-1.1.0 / LICENSE-APACHE Original notice: toml_parser 1.1.3+spec-1.1.0 / LICENSE-APACHE Original notice: toml_write 0.1.2 / LICENSE-APACHE Original notice: toml_writer 1.1.2+spec-1.1.0 / LICENSE-APACHE +Original notice: unicode-general-category 1.0.0 / LICENSE Original notice: winapi 0.3.9 / LICENSE-APACHE Original notice: wio 0.2.2 / LICENSE-APACHE @@ -11200,15 +12301,221 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION END OF TERMS AND CONDITIONS ======================================================================== -Original notice: futures 0.3.34 / LICENSE-APACHE -Original notice: futures-channel 0.3.34 / LICENSE-APACHE -Original notice: futures-core 0.3.34 / LICENSE-APACHE -Original notice: futures-executor 0.3.34 / LICENSE-APACHE -Original notice: futures-io 0.3.34 / LICENSE-APACHE -Original notice: futures-macro 0.3.34 / LICENSE-APACHE -Original notice: futures-sink 0.3.34 / LICENSE-APACHE -Original notice: futures-task 0.3.34 / LICENSE-APACHE -Original notice: futures-util 0.3.34 / LICENSE-APACHE +Original notice: futures 0.3.34 / LICENSE-APACHE +Original notice: futures-channel 0.3.34 / LICENSE-APACHE +Original notice: futures-core 0.3.34 / LICENSE-APACHE +Original notice: futures-executor 0.3.34 / LICENSE-APACHE +Original notice: futures-io 0.3.34 / LICENSE-APACHE +Original notice: futures-macro 0.3.34 / LICENSE-APACHE +Original notice: futures-sink 0.3.34 / LICENSE-APACHE +Original notice: futures-task 0.3.34 / LICENSE-APACHE +Original notice: futures-util 0.3.34 / LICENSE-APACHE + +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright (c) 2016 Alex Crichton +Copyright (c) 2017 The Tokio Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +======================================================================== +Original notice: typenum 1.20.1 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -11398,8 +12705,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright (c) 2016 Alex Crichton -Copyright (c) 2017 The Tokio Authors +Copyright 2014 Paho Lurie-Gregg Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11414,7 +12720,7 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: typenum 1.20.1 / LICENSE-APACHE +Original notice: gpui-pre-reqwest 0.12.15 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -11604,7 +12910,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2014 Paho Lurie-Gregg +Copyright 2016 Sean McArthur Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11619,7 +12925,7 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: gpui-pre-reqwest 0.12.15 / LICENSE-APACHE +Original notice: http 1.5.0 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -11809,7 +13115,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2016 Sean McArthur +Copyright 2017 http-rs authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11824,7 +13130,7 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: http 1.5.0 / LICENSE-APACHE +Original notice: tokio-rustls 0.26.6 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -12014,7 +13320,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2017 http-rs authors +Copyright 2017 quininer kel Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -12029,7 +13335,9 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: tokio-rustls 0.26.6 / LICENSE-APACHE +Original notice: logos 0.16.1 / LICENSE-APACHE +Original notice: logos-codegen 0.16.1 / LICENSE-APACHE +Original notice: logos-derive 0.16.1 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -12219,13 +13527,13 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2017 quininer kel +Copyright 2018 Maciej Hirsz Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -14084,14 +15392,14 @@ limitations under the License. ======================================================================== License: Apache-2.0 -Used by: gethostname 1.1.0, ring 0.17.14 +Used by: gethostname 1.1.0, prost 0.14.4, prost-build 0.14.4, prost-derive 0.14.4, prost-types 0.14.4, ring 0.17.14 Original notice: addr2line 0.25.1 / LICENSE-APACHE Original notice: ahash 0.8.12 / LICENSE-APACHE Original notice: arc-swap 1.9.2 / LICENSE-APACHE Original notice: arrayvec 0.7.8 / LICENSE-APACHE Original notice: async-channel 2.5.0 / LICENSE-APACHE Original notice: async-compression 0.4.50 / LICENSE-APACHE -Original notice: async-executor 1.14.0 / LICENSE-APACHE +Original notice: async-executor 1.14.1 / LICENSE-APACHE Original notice: async-fs 2.2.0 / LICENSE-APACHE Original notice: async-io 2.6.0 / LICENSE-APACHE Original notice: async-lock 3.4.2 / LICENSE-APACHE @@ -14151,6 +15459,7 @@ Original notice: filetime 0.2.29 / LICENSE-APACHE Original notice: find-msvc-tools 0.1.14 / LICENSE-APACHE Original notice: fixedbitset 0.5.7 / LICENSE-APACHE Original notice: flate2 1.1.10 / LICENSE-APACHE +Original notice: fnv 1.0.7 / LICENSE-APACHE Original notice: form_urlencoded 1.2.2 / LICENSE-APACHE Original notice: fs2 0.4.3 / LICENSE-APACHE Original notice: futf 0.1.5 / LICENSE-APACHE @@ -14173,6 +15482,7 @@ Original notice: heck 0.5.0 / LICENSE-APACHE Original notice: html5ever 0.27.0 / LICENSE-APACHE Original notice: httparse 1.10.1 / LICENSE-APACHE Original notice: hyper-rustls 0.27.10 / LICENSE-APACHE +Original notice: hyper-timeout 0.5.2 / LICENSE-APACHE Original notice: idna 1.1.0 / LICENSE-APACHE Original notice: idna_adapter 1.2.2 / LICENSE-APACHE Original notice: indexmap 2.14.2 / LICENSE-APACHE @@ -14191,6 +15501,7 @@ Original notice: markup5ever_rcdom 0.3.0 / LICENSE-APACHE Original notice: metal 0.33.0 / LICENSE-APACHE Original notice: mime 0.3.17 / LICENSE-APACHE Original notice: miow 0.6.1 / LICENSE-APACHE +Original notice: multimap 0.10.1 / LICENSE-APACHE Original notice: num 0.4.3 / LICENSE-APACHE Original notice: num-bigint 0.4.8 / LICENSE-APACHE Original notice: num-bigint 0.5.1 / LICENSE-APACHE @@ -14211,6 +15522,7 @@ Original notice: parking_lot_core 0.9.12 / LICENSE-APACHE Original notice: pathfinder_simd@0.5.6 / upstream/servo-pathfinder-license-apache-a60eea817514.txt Source: https://raw.githubusercontent.com/servo/pathfinder/6c3c0466f451c5bd2007087728cd168798cd64e8/LICENSE-APACHE Original notice: percent-encoding 2.3.2 / LICENSE-APACHE +Original notice: petgraph 0.8.3 / LICENSE-APACHE Original notice: piper 0.2.5 / LICENSE-APACHE Original notice: pkg-config 0.3.34 / LICENSE-APACHE Original notice: png 0.17.16 / LICENSE-APACHE @@ -14220,6 +15532,13 @@ Original notice: pollster 0.2.5 / LICENSE-APACHE Original notice: pollster 0.4.0 / LICENSE-APACHE Original notice: profiling-procmacros@1.0.18, profiling@1.0.18 / upstream/aclysma-profiling-license-apache-10d30a673cd5.txt Source: https://raw.githubusercontent.com/aclysma/profiling/8271551172eb6fa4cba47369aedd93790c623df9/LICENSE-APACHE +Original notice: prost 0.14.4 / LICENSE +Original notice: prost-build 0.14.4 / LICENSE +Original notice: prost-derive 0.14.4 / LICENSE +Original notice: prost-reflect 0.16.5 / LICENSE-APACHE +Original notice: prost-types 0.14.4 / LICENSE +Original notice: protox 0.10.0 / LICENSE-APACHE +Original notice: protox-parse 0.10.0 / LICENSE-APACHE Original notice: rayon 1.12.0 / LICENSE-APACHE Original notice: rayon-core 1.13.0 / LICENSE-APACHE Original notice: regex 1.13.1 / LICENSE-APACHE @@ -14236,7 +15555,7 @@ Original notice: rustc_version 0.4.1 / LICENSE-APACHE Original notice: rustix 0.38.44 / LICENSE-APACHE Original notice: rustix 1.1.5 / LICENSE-APACHE Original notice: rustix-openpty 0.2.0 / LICENSE-APACHE -Original notice: rustls 0.23.45 / LICENSE-APACHE +Original notice: rustls 0.23.46 / LICENSE-APACHE Original notice: rustls-pemfile 2.2.0 / LICENSE-APACHE Original notice: scoped-tls 1.0.1 / LICENSE-APACHE Original notice: scopeguard 1.2.0 / LICENSE-APACHE @@ -14273,6 +15592,7 @@ Original notice: unicode-id 0.3.7 / LICENSE-APACHE Original notice: unicode-properties 0.1.4 / LICENSE-APACHE Original notice: unicode-segmentation 1.13.3 / LICENSE-APACHE Original notice: unicode-vo 0.1.0 / LICENSE-APACHE +Original notice: unicode-width 0.1.14 / LICENSE-APACHE Original notice: unicode-width 0.2.2 / LICENSE-APACHE Original notice: unicode-xid 0.2.6 / LICENSE-APACHE Original notice: url 2.5.8 / LICENSE-APACHE @@ -14725,7 +16045,7 @@ Original notice: const-oid 0.10.2 / LICENSE-APACHE Original notice: cpubits 0.1.1 / LICENSE-APACHE Original notice: cpufeatures 0.2.17 / LICENSE-APACHE Original notice: cpufeatures 0.3.1 / LICENSE-APACHE -Original notice: crypto-bigint 0.7.5 / LICENSE-APACHE +Original notice: crypto-bigint 0.7.11 / LICENSE-APACHE Original notice: crypto-common 0.1.7 / LICENSE-APACHE Original notice: crypto-common 0.2.2 / LICENSE-APACHE Original notice: ctr 0.10.1 / LICENSE-APACHE @@ -15966,7 +17286,214 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - https://www.apache.org/licenses/LICENSE-2.0 + https://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + +======================================================================== +Original notice: adler2 2.0.1 / LICENSE-APACHE +Original notice: home 0.5.12 / LICENSE-APACHE +Original notice: proc-macro-crate 3.5.0 / LICENSE-APACHE + +Apache License + Version 2.0, January 2004 + https://www.apache.org/licenses/LICENSE-2.0 + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + https://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -15975,197 +17502,56 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: adler2 2.0.1 / LICENSE-APACHE -Original notice: home 0.5.12 / LICENSE-APACHE -Original notice: proc-macro-crate 3.5.0 / LICENSE-APACHE +Original notice: bytemuck 1.25.2 / LICENSE-APACHE +Original notice: bytemuck_derive 1.12.1 / LICENSE-APACHE Apache License - Version 2.0, January 2004 - https://www.apache.org/licenses/LICENSE-2.0 +Version 2.0, January 2004 +http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION -1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, - and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by - the copyright owner that is granting the License. - - "Legal Entity" shall mean the union of the acting entity and all - other entities that control, are controlled by, or are under common - control with that entity. For the purposes of this definition, - "control" means (i) the power, direct or indirect, to cause the - direction or management of such entity, whether by contract or - otherwise, or (ii) ownership of fifty percent (50%) or more of the - outstanding shares, or (iii) beneficial ownership of such entity. - - "You" (or "Your") shall mean an individual or Legal Entity - exercising permissions granted by this License. - - "Source" form shall mean the preferred form for making modifications, - including but not limited to software source code, documentation - source, and configuration files. - - "Object" form shall mean any form resulting from mechanical - transformation or translation of a Source form, including but - not limited to compiled object code, generated documentation, - and conversions to other media types. - - "Work" shall mean the work of authorship, whether in Source or - Object form, made available under the License, as indicated by a - copyright notice that is included in or attached to the work - (an example is provided in the Appendix below). - - "Derivative Works" shall mean any work, whether in Source or Object - form, that is based on (or derived from) the Work and for which the - editorial revisions, annotations, elaborations, or other modifications - represent, as a whole, an original work of authorship. For the purposes - of this License, Derivative Works shall not include works that remain - separable from, or merely link (or bind by name) to the interfaces of, - the Work and Derivative Works thereof. - - "Contribution" shall mean any work of authorship, including - the original version of the Work and any modifications or additions - to that Work or Derivative Works thereof, that is intentionally - submitted to Licensor for inclusion in the Work by the copyright owner - or by an individual or Legal Entity authorized to submit on behalf of - the copyright owner. For the purposes of this definition, "submitted" - means any form of electronic, verbal, or written communication sent - to the Licensor or its representatives, including but not limited to - communication on electronic mailing lists, source code control systems, - and issue tracking systems that are managed by, or on behalf of, the - Licensor for the purpose of discussing and improving the Work, but - excluding communication that is conspicuously marked or otherwise - designated in writing by the copyright owner as "Not a Contribution." - - "Contributor" shall mean Licensor and any individual or Legal Entity - on behalf of whom a Contribution has been received by Licensor and - subsequently incorporated within the Work. - -2. Grant of Copyright License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - copyright license to reproduce, prepare Derivative Works of, - publicly display, publicly perform, sublicense, and distribute the - Work and such Derivative Works in Source or Object form. - -3. Grant of Patent License. Subject to the terms and conditions of - this License, each Contributor hereby grants to You a perpetual, - worldwide, non-exclusive, no-charge, royalty-free, irrevocable - (except as stated in this section) patent license to make, have made, - use, offer to sell, sell, import, and otherwise transfer the Work, - where such license applies only to those patent claims licensable - by such Contributor that are necessarily infringed by their - Contribution(s) alone or by combination of their Contribution(s) - with the Work to which such Contribution(s) was submitted. If You - institute patent litigation against any entity (including a - cross-claim or counterclaim in a lawsuit) alleging that the Work - or a Contribution incorporated within the Work constitutes direct - or contributory patent infringement, then any patent licenses - granted to You under this License for that Work shall terminate - as of the date such litigation is filed. + 1. Definitions. -4. Redistribution. You may reproduce and distribute copies of the - Work or Derivative Works thereof in any medium, with or without - modifications, and in Source or Object form, provided that You - meet the following conditions: + "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. - (a) You must give any other recipients of the Work or - Derivative Works a copy of this License; and + "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. - (b) You must cause any modified files to carry prominent notices - stating that You changed the files; and + "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. - (c) You must retain, in the Source form of any Derivative Works - that You distribute, all copyright, patent, trademark, and - attribution notices from the Source form of the Work, - excluding those notices that do not pertain to any part of - the Derivative Works; and + "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. - (d) If the Work includes a "NOTICE" text file as part of its - distribution, then any Derivative Works that You distribute must - include a readable copy of the attribution notices contained - within such NOTICE file, excluding those notices that do not - pertain to any part of the Derivative Works, in at least one - of the following places: within a NOTICE text file distributed - as part of the Derivative Works; within the Source form or - documentation, if provided along with the Derivative Works; or, - within a display generated by the Derivative Works, if and - wherever such third-party notices normally appear. The contents - of the NOTICE file are for informational purposes only and - do not modify the License. You may add Your own attribution - notices within Derivative Works that You distribute, alongside - or as an addendum to the NOTICE text from the Work, provided - that such additional attribution notices cannot be construed - as modifying the License. + "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. - You may add Your own copyright statement to Your modifications and - may provide additional or different license terms and conditions - for use, reproduction, or distribution of Your modifications, or - for any such Derivative Works as a whole, provided Your use, - reproduction, and distribution of the Work otherwise complies with - the conditions stated in this License. + "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. -5. Submission of Contributions. Unless You explicitly state otherwise, - any Contribution intentionally submitted for inclusion in the Work - by You to the Licensor shall be under the terms and conditions of - this License, without any additional terms or conditions. - Notwithstanding the above, nothing herein shall supersede or modify - the terms of any separate license agreement you may have executed - with Licensor regarding such Contributions. + "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). -6. Trademarks. This License does not grant permission to use the trade - names, trademarks, service marks, or product names of the Licensor, - except as required for reasonable and customary use in describing the - origin of the Work and reproducing the content of the NOTICE file. + "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. -7. Disclaimer of Warranty. Unless required by applicable law or - agreed to in writing, Licensor provides the Work (and each - Contributor provides its Contributions) on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or - implied, including, without limitation, any warranties or conditions - of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A - PARTICULAR PURPOSE. You are solely responsible for determining the - appropriateness of using or redistributing the Work and assume any - risks associated with Your exercise of permissions under this License. + "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." -8. Limitation of Liability. In no event and under no legal theory, - whether in tort (including negligence), contract, or otherwise, - unless required by applicable law (such as deliberate and grossly - negligent acts) or agreed to in writing, shall any Contributor be - liable to You for damages, including any direct, indirect, special, - incidental, or consequential damages of any character arising as a - result of this License or out of the use or inability to use the - Work (including but not limited to damages for loss of goodwill, - work stoppage, computer failure or malfunction, or any and all - other commercial damages or losses), even if such Contributor - has been advised of the possibility of such damages. + "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. + 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. + 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. + 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: + (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and + (b) You must cause any modified files to carry prominent notices stating that You changed the files; and + (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and + (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. -9. Accepting Warranty or Additional Liability. While redistributing - the Work or Derivative Works thereof, You may choose to offer, - and charge a fee for, acceptance of support, warranty, indemnity, - or other liability obligations and/or rights consistent with this - License. However, in accepting such obligations, You may act only - on Your own behalf and on Your sole responsibility, not on behalf - of any other Contributor, and only if You agree to indemnify, - defend, and hold each Contributor harmless for any liability - incurred by, or claims asserted against, such Contributor by reason - of your accepting any such warranty or additional liability. + You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. + 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. + 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. + 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. + 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. + 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. - To apply the Apache License to your work, attach the following - boilerplate notice, with the fields enclosed by brackets "[]" - replaced with your own identifying information. (Don't include - the brackets!) The text should be enclosed in the appropriate - comment syntax for the file format. We also recommend that a - file or class name and description of purpose be included on the - same "printed page" as the copyright notice for easier - identification within third-party archives. +To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] @@ -16173,7 +17559,7 @@ Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - https://www.apache.org/licenses/LICENSE-2.0 +http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -16182,8 +17568,7 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: bytemuck 1.25.2 / LICENSE-APACHE -Original notice: bytemuck_derive 1.12.1 / LICENSE-APACHE +Original notice: httpdate 1.0.3 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -16191,47 +17576,187 @@ http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION - 1. Definitions. - - "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. - - "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. +1. Definitions. - "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. +"License" shall mean the terms and conditions for use, reproduction, +and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by +the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all +other entities that control, are controlled by, or are under common +control with that entity. For the purposes of this definition, +"control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or +otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity +exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation +source, and configuration files. + +"Object" form shall mean any form resulting from mechanical +transformation or translation of a Source form, including but +not limited to compiled object code, generated documentation, +and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or +Object form, made available under the License, as indicated by a +copyright notice that is included in or attached to the work +(an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object +form, that is based on (or derived from) the Work and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. For the purposes +of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, +the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including +the original version of the Work and any modifications or additions +to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner +or by an individual or Legal Entity authorized to submit on behalf of +the copyright owner. For the purposes of this definition, "submitted" +means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, +and issue tracking systems that are managed by, or on behalf of, the +Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise +designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity +on behalf of whom a Contribution has been received by Licensor and +subsequently incorporated within the Work. - "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. +2. Grant of Copyright License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the +Work and such Derivative Works in Source or Object form. - "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. +3. Grant of Patent License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have made, +use, offer to sell, sell, import, and otherwise transfer the Work, +where such license applies only to those patent claims licensable +by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) +with the Work to which such Contribution(s) was submitted. If You +institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work +or a Contribution incorporated within the Work constitutes direct +or contributory patent infringement, then any patent licenses +granted to You under this License for that Work shall terminate +as of the date such litigation is filed. - "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. +4. Redistribution. You may reproduce and distribute copies of the +Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You +meet the following conditions: + +(a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + +(b) You must cause any modified files to carry prominent notices +stating that You changed the files; and + +(c) You must retain, in the Source form of any Derivative Works +that You distribute, all copyright, patent, trademark, and +attribution notices from the Source form of the Work, +excluding those notices that do not pertain to any part of +the Derivative Works; and + +(d) If the Work includes a "NOTICE" text file as part of its +distribution, then any Derivative Works that You distribute must +include a readable copy of the attribution notices contained +within such NOTICE file, excluding those notices that do not +pertain to any part of the Derivative Works, in at least one +of the following places: within a NOTICE text file distributed +as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, +within a display generated by the Derivative Works, if and +wherever such third-party notices normally appear. The contents +of the NOTICE file are for informational purposes only and +do not modify the License. You may add Your own attribution +notices within Derivative Works that You distribute, alongside +or as an addendum to the NOTICE text from the Work, provided +that such additional attribution notices cannot be construed +as modifying the License. + +You may add Your own copyright statement to Your modifications and +may provide additional or different license terms and conditions +for use, reproduction, or distribution of Your modifications, or +for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with +the conditions stated in this License. - "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). +5. Submission of Contributions. Unless You explicitly state otherwise, +any Contribution intentionally submitted for inclusion in the Work +by You to the Licensor shall be under the terms and conditions of +this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify +the terms of any separate license agreement you may have executed +with Licensor regarding such Contributions. - "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. +6. Trademarks. This License does not grant permission to use the trade +names, trademarks, service marks, or product names of the Licensor, +except as required for reasonable and customary use in describing the +origin of the Work and reproducing the content of the NOTICE file. - "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." +7. Disclaimer of Warranty. Unless required by applicable law or +agreed to in writing, Licensor provides the Work (and each +Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied, including, without limitation, any warranties or conditions +of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A +PARTICULAR PURPOSE. You are solely responsible for determining the +appropriateness of using or redistributing the Work and assume any +risks associated with Your exercise of permissions under this License. - "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. - 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. - 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. - 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: - (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and - (b) You must cause any modified files to carry prominent notices stating that You changed the files; and - (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and - (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. +8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, +unless required by applicable law (such as deliberate and grossly +negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, +incidental, or consequential damages of any character arising as a +result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, +work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses), even if such Contributor +has been advised of the possibility of such damages. - You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. - 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. - 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. - 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. - 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. - 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. +9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, +and charge a fee for, acceptance of support, warranty, indemnity, +or other liability obligations and/or rights consistent with this +License. However, in accepting such obligations, You may act only +on Your own behalf and on Your sole responsibility, not on behalf +of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason +of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. -To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. +To apply the Apache License to your work, attach the following +boilerplate notice, with the fields enclosed by brackets "[]" +replaced with your own identifying information. (Don't include +the brackets!) The text should be enclosed in the appropriate +comment syntax for the file format. We also recommend that a +file or class name and description of purpose be included on the +same "printed page" as the copyright notice for easier +identification within third-party archives. Copyright [yyyy] [name of copyright owner] @@ -17705,11 +19230,12 @@ THE SOFTWARE. ======================================================================== License: MIT -Used by: either 1.18.0, itertools 0.11.0, itertools 0.13.0, itertools 0.14.0, serde_with 3.24.0, serde_with_macros 3.24.0 +Used by: either 1.18.0, itertools 0.11.0, itertools 0.13.0, itertools 0.14.0, petgraph 0.8.3, serde_with 3.24.0, serde_with_macros 3.24.0 Original notice: either 1.18.0 / LICENSE-MIT Original notice: itertools 0.11.0 / LICENSE-MIT Original notice: itertools 0.13.0 / LICENSE-MIT Original notice: itertools 0.14.0 / LICENSE-MIT +Original notice: petgraph 0.8.3 / LICENSE-MIT Original notice: serde_with 3.24.0 / LICENSE-MIT Original notice: serde_with_macros 3.24.0 / LICENSE-MIT @@ -18203,7 +19729,7 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: fs2 0.4.3, gimli 0.32.3, heck 0.4.1, heck 0.5.0, unicode-bidi 0.3.18, unicode-id 0.3.7, unicode-properties 0.1.4, unicode-segmentation 1.13.3, unicode-width 0.2.2, unicode-xid 0.2.6 +Used by: fs2 0.4.3, gimli 0.32.3, heck 0.4.1, heck 0.5.0, unicode-bidi 0.3.18, unicode-id 0.3.7, unicode-properties 0.1.4, unicode-segmentation 1.13.3, unicode-width 0.1.14, unicode-width 0.2.2, unicode-xid 0.2.6 Original notice: fs2 0.4.3 / LICENSE-MIT Original notice: gimli 0.32.3 / LICENSE-MIT Original notice: heck 0.4.1 / LICENSE-MIT @@ -18212,6 +19738,7 @@ Original notice: unicode-bidi 0.3.18 / LICENSE-MIT Original notice: unicode-id 0.3.7 / LICENSE-MIT Original notice: unicode-properties 0.1.4 / LICENSE-MIT Original notice: unicode-segmentation 1.13.3 / LICENSE-MIT +Original notice: unicode-width 0.1.14 / LICENSE-MIT Original notice: unicode-width 0.2.2 / LICENSE-MIT Original notice: unicode-xid 0.2.6 / LICENSE-MIT @@ -18653,10 +20180,41 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: serde_urlencoded 0.7.1 -Original notice: serde_urlencoded 0.7.1 / LICENSE-MIT +Used by: serde_urlencoded 0.7.1 +Original notice: serde_urlencoded 0.7.1 / LICENSE-MIT + +Copyright (c) 2016 Anthony Ramine + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + +======================================================================== +License: MIT +Used by: vte 0.15.0 +Original notice: vte 0.15.0 / LICENSE-MIT -Copyright (c) 2016 Anthony Ramine +Copyright (c) 2016 Joe Wilm Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated @@ -18684,10 +20242,12 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: vte 0.15.0 -Original notice: vte 0.15.0 / LICENSE-MIT +Used by: hyper-rustls 0.27.10, rustls 0.23.46, rustls-pemfile 2.2.0 +Original notice: hyper-rustls 0.27.10 / LICENSE-MIT +Original notice: rustls 0.23.46 / LICENSE-MIT +Original notice: rustls-pemfile 2.2.0 / LICENSE-MIT -Copyright (c) 2016 Joe Wilm +Copyright (c) 2016 Joseph Birr-Pixton Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated @@ -18715,36 +20275,28 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: hyper-rustls 0.27.10, rustls 0.23.45, rustls-pemfile 2.2.0 -Original notice: hyper-rustls 0.27.10 / LICENSE-MIT -Original notice: rustls 0.23.45 / LICENSE-MIT -Original notice: rustls-pemfile 2.2.0 / LICENSE-MIT +Used by: httpdate 1.0.3 +Original notice: httpdate 1.0.3 / LICENSE-MIT -Copyright (c) 2016 Joseph Birr-Pixton +Copyright (c) 2016 Pyfisch -Permission is hereby granted, free of charge, to any -person obtaining a copy of this software and associated -documentation files (the "Software"), to deal in the -Software without restriction, including without -limitation the rights to use, copy, modify, merge, -publish, distribute, sublicense, and/or sell copies of -the Software, and to permit persons to whom the Software -is furnished to do so, subject to the following -conditions: +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: -The above copyright notice and this permission notice -shall be included in all copies or substantial portions -of the Software. +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF -ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED -TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A -PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT -SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY -CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION -OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR -IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER -DEALINGS IN THE SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. ======================================================================== License: MIT @@ -18869,6 +20421,37 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: multimap 0.10.1 +Original notice: multimap 0.10.1 / LICENSE-MIT + +Copyright (c) 2016 multimap developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + ======================================================================== License: MIT Used by: indexmap 2.14.2 @@ -19282,6 +20865,37 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: fnv 1.0.7 +Original notice: fnv 1.0.7 / LICENSE-MIT + +Copyright (c) 2017 Contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + ======================================================================== License: MIT Used by: globwalk 0.8.1, memoffset 0.9.1 @@ -19490,6 +21104,37 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: h2 0.4.19 +Original notice: h2 0.4.19 / LICENSE + +Copyright (c) 2017 h2 authors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + ======================================================================== License: MIT Used by: http 1.5.0 @@ -19869,6 +21514,35 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: logos 0.16.1, logos-codegen 0.16.1, logos-derive 0.16.1 +Original notice: logos 0.16.1 / LICENSE-MIT +Original notice: logos-codegen 0.16.1 / LICENSE-MIT +Original notice: logos-derive 0.16.1 / LICENSE-MIT + +Copyright (c) 2018 Maciej Hirsz + +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ======================================================================== License: MIT Used by: kurbo 0.11.3, kurbo 0.13.1, polycool 0.4.0 @@ -21709,9 +23383,9 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: argon2 0.6.0, crypto-bigint 0.7.5, phc 0.6.1, pkcs5 0.8.1, scrypt 0.12.0, sec1 0.8.1, spki 0.8.0, ssh-encoding 0.3.0, ssh-key 0.7.0-rc.11 +Used by: argon2 0.6.0, crypto-bigint 0.7.11, phc 0.6.1, pkcs5 0.8.1, scrypt 0.12.0, sec1 0.8.1, spki 0.8.0, ssh-encoding 0.3.0, ssh-key 0.7.0-rc.11 Original notice: argon2 0.6.0 / LICENSE-MIT -Original notice: crypto-bigint 0.7.5 / LICENSE-MIT +Original notice: crypto-bigint 0.7.11 / LICENSE-MIT Original notice: phc 0.6.1 / LICENSE-MIT Original notice: pkcs5 0.8.1 / LICENSE-MIT Original notice: scrypt 0.12.0 / LICENSE-MIT @@ -22667,8 +24341,8 @@ SOFTWARE. ======================================================================== License: MIT -Used by: ctutils 0.4.2 -Original notice: ctutils 0.4.2 / LICENSE-MIT +Used by: ctutils 0.4.8 +Original notice: ctutils 0.4.8 / LICENSE-MIT Copyright (c) 2025-2026 The RustCrypto Project Developers @@ -23558,6 +25232,34 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: tonic 0.14.7, tonic-build 0.14.7 +Original notice: tonic 0.14.7 / LICENSE +Original notice: tonic-build 0.14.7 / LICENSE +Original notice: tonic-prost-build@0.14.7 / upstream/grpc-grpc-rust-license-5c686e0f7978.txt +Source: https://raw.githubusercontent.com/grpc/grpc-rust/818532da3abfa6068b0855f483e3c627d9e14640/LICENSE + +Copyright 2019 gRPC authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ======================================================================== License: MIT Used by: x11rb 0.13.2, x11rb-protocol 0.13.2 @@ -24712,8 +26414,8 @@ express Statement of Purpose. ======================================================================== License: ISC -Used by: rustls-webpki 0.103.15 -Original notice: rustls-webpki 0.103.15 / LICENSE +Used by: rustls-webpki 0.103.16 +Original notice: rustls-webpki 0.103.16 / LICENSE Except as otherwise noted, this project is licensed under the following (ISC-style) terms: @@ -26640,6 +28342,17 @@ necessary. Here is a sample; alter the names: That's all there is to it! +======================================================================== +Original notice: petgraph 0.8.3 / assets/images/LICENSE.md + +Graphosaurus (c) by the petgraph project. + +Graphosaurus is licensed under a +Creative Commons Attribution-ShareAlike 4.0 International License. + +You should have received a copy of the license along with this +work. If not, see . + ======================================================================== Original notice: gpui-kit-assets 0.7.0 / LICENSE-LUCIDE @@ -26689,7 +28402,7 @@ SOFTWARE. ======================================================================== Original notice: hyper-rustls 0.27.10 / LICENSE-ISC -Original notice: rustls 0.23.45 / LICENSE-ISC +Original notice: rustls 0.23.46 / LICENSE-ISC Original notice: rustls-pemfile 2.2.0 / LICENSE-ISC ISC License (ISC) @@ -26731,6 +28444,7 @@ Redistributions of this Work must include all inherited and new license notices, Original notice: unicode-id 0.3.7 / COPYRIGHT Original notice: unicode-properties 0.1.4 / COPYRIGHT Original notice: unicode-segmentation 1.13.3 / COPYRIGHT +Original notice: unicode-width 0.1.14 / COPYRIGHT Original notice: unicode-width 0.2.2 / COPYRIGHT Original notice: unicode-xid 0.2.6 / COPYRIGHT @@ -27212,6 +28926,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: tokio-socks 0.5.3 +Original notice: tokio-socks 0.5.3 / LICENSE + +MIT License + +Copyright (c) 2018 Yilin Chen + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ======================================================================== Bundled component: appimage/LICENSE-mimalloc Original notice: / supplemental/appimage/LICENSE-mimalloc @@ -28244,7 +29985,7 @@ SOFTWARE. ======================================================================== SPDX reference license terms: MIT -Used by: accesskit 0.24.1, accesskit_atspi_common 0.19.1, accesskit_consumer 0.38.0, accesskit_macos 0.26.3, accesskit_unix 0.22.1, accesskit_windows 0.34.0, block 0.1.6, block2 0.6.2, chrono 0.4.45, delegate 0.13.5, dispatch2 0.3.1, gpu-descriptor 0.3.2, gpu-descriptor-types 0.2.0, granit-parser 1.3.0, harfrust 0.5.2, leak 0.1.2, leaky-cow 0.1.1, libm 0.2.16, lyon 1.0.19, lyon_algorithms 1.0.21, lyon_geom 1.0.19, lyon_path 1.0.19, lyon_tessellation 1.0.22, mac 0.1.1, mac-notification-sys 0.6.15, malloc_buf 0.0.6, md5 0.8.1, objc-sys 0.3.5, objc2 0.5.2, objc2 0.6.4, objc2-app-kit 0.2.2, objc2-app-kit 0.3.2, objc2-cloud-kit 0.3.2, objc2-core-data 0.3.2, objc2-core-foundation 0.3.2, objc2-core-graphics 0.3.2, objc2-core-image 0.3.2, objc2-core-location 0.3.2, objc2-core-media 0.3.2, objc2-core-text 0.3.2, objc2-core-video 0.3.2, objc2-encode 4.1.0, objc2-foundation 0.2.2, objc2-foundation 0.3.2, objc2-metal 0.3.2, objc2-quartz-core 0.3.2, objc2-screen-capture-kit 0.3.2, objc2-user-notifications 0.3.2, pathfinder_geometry 0.5.1, pathfinder_simd 0.5.6, profiling 1.0.18, profiling-procmacros 1.0.18, pulp-wasm-simd-flag 0.1.1, rust-i18n-macro 4.2.4, rust-i18n-support 4.2.4, seahash 4.1.0, svg_fmt 0.4.5, taffy 0.13.0, tauri-winrt-notification 0.8.1, tree-sitter-bash 0.23.3, tree-sitter-cpp 0.23.4, tree-sitter-css 0.23.2, tree-sitter-diff 0.1.0, tree-sitter-go 0.23.4, tree-sitter-html 0.23.2, tree-sitter-java 0.23.5, tree-sitter-javascript 0.23.1, tree-sitter-json 0.24.8, tree-sitter-python 0.23.6, tree-sitter-toml-ng 0.7.0, tree-sitter-typescript 0.23.2, windows 0.58.0, windows 0.62.2, windows-collections 0.3.2, windows-core 0.58.0, windows-core 0.62.2, windows-future 0.3.2, windows-implement 0.58.0, windows-implement 0.60.2, windows-interface 0.58.0, windows-interface 0.59.3, windows-link 0.1.3, windows-link 0.2.1, windows-numerics 0.3.1, windows-registry 0.4.0, windows-registry 0.6.1, windows-result 0.2.0, windows-result 0.3.4, windows-result 0.4.1, windows-strings 0.1.0, windows-strings 0.3.1, windows-strings 0.5.1, windows-sys 0.48.0, windows-sys 0.52.0, windows-sys 0.59.0, windows-sys 0.61.2, windows-targets 0.48.5, windows-targets 0.52.6, windows-targets 0.53.5, windows-threading 0.2.1, windows-version 0.1.7, windows_x86_64_gnu 0.48.5, windows_x86_64_gnu 0.52.6, windows_x86_64_gnu 0.53.1, windows_x86_64_msvc 0.48.5, windows_x86_64_msvc 0.52.6, windows_x86_64_msvc 0.53.1, xim-ctext 0.3.0, xim-parser 0.2.2, zune-core 0.4.12, zune-inflate 0.2.54, zune-jpeg 0.4.21 +Used by: accesskit 0.24.1, accesskit_atspi_common 0.19.1, accesskit_consumer 0.38.0, accesskit_macos 0.26.3, accesskit_unix 0.22.1, accesskit_windows 0.34.0, block 0.1.6, block2 0.6.2, chrono 0.4.45, delegate 0.13.5, dispatch2 0.3.1, gpu-descriptor 0.3.2, gpu-descriptor-types 0.2.0, granit-parser 1.3.0, harfrust 0.5.2, leak 0.1.2, leaky-cow 0.1.1, libm 0.2.16, lyon 1.0.19, lyon_algorithms 1.0.21, lyon_geom 1.0.19, lyon_path 1.0.19, lyon_tessellation 1.0.22, mac 0.1.1, mac-notification-sys 0.6.15, malloc_buf 0.0.6, md5 0.8.1, objc-sys 0.3.5, objc2 0.5.2, objc2 0.6.4, objc2-app-kit 0.2.2, objc2-app-kit 0.3.2, objc2-cloud-kit 0.3.2, objc2-core-data 0.3.2, objc2-core-foundation 0.3.2, objc2-core-graphics 0.3.2, objc2-core-image 0.3.2, objc2-core-location 0.3.2, objc2-core-media 0.3.2, objc2-core-text 0.3.2, objc2-core-video 0.3.2, objc2-encode 4.1.0, objc2-foundation 0.2.2, objc2-foundation 0.3.2, objc2-metal 0.3.2, objc2-quartz-core 0.3.2, objc2-screen-capture-kit 0.3.2, objc2-user-notifications 0.3.2, pathfinder_geometry 0.5.1, pathfinder_simd 0.5.6, profiling 1.0.18, profiling-procmacros 1.0.18, pulp-wasm-simd-flag 0.1.1, rust-i18n-macro 4.2.4, rust-i18n-support 4.2.4, seahash 4.1.0, svg_fmt 0.4.5, taffy 0.13.0, tauri-winrt-notification 0.8.1, tonic-prost-build 0.14.7, tree-sitter-bash 0.23.3, tree-sitter-cpp 0.23.4, tree-sitter-css 0.23.2, tree-sitter-diff 0.1.0, tree-sitter-go 0.23.4, tree-sitter-html 0.23.2, tree-sitter-java 0.23.5, tree-sitter-javascript 0.23.1, tree-sitter-json 0.24.8, tree-sitter-python 0.23.6, tree-sitter-toml-ng 0.7.0, tree-sitter-typescript 0.23.2, windows 0.58.0, windows 0.62.2, windows-collections 0.3.2, windows-core 0.58.0, windows-core 0.62.2, windows-future 0.3.2, windows-implement 0.58.0, windows-implement 0.60.2, windows-interface 0.58.0, windows-interface 0.59.3, windows-link 0.1.3, windows-link 0.2.1, windows-numerics 0.3.1, windows-registry 0.4.0, windows-registry 0.6.1, windows-result 0.2.0, windows-result 0.3.4, windows-result 0.4.1, windows-strings 0.1.0, windows-strings 0.3.1, windows-strings 0.5.1, windows-sys 0.48.0, windows-sys 0.52.0, windows-sys 0.59.0, windows-sys 0.61.2, windows-targets 0.48.5, windows-targets 0.52.6, windows-targets 0.53.5, windows-threading 0.2.1, windows-version 0.1.7, windows_x86_64_gnu 0.48.5, windows_x86_64_gnu 0.52.6, windows_x86_64_gnu 0.53.1, windows_x86_64_msvc 0.48.5, windows_x86_64_msvc 0.52.6, windows_x86_64_msvc 0.53.1, xim-ctext 0.3.0, xim-parser 0.2.2, zune-core 0.4.12, zune-inflate 0.2.54, zune-jpeg 0.4.21 MIT License @@ -28298,8 +30039,9 @@ SOFTWARE. ======================================================================== License: MIT -Used by: tokio 1.53.1, tokio-util 0.7.19 -Original notice: tokio 1.53.1 / LICENSE +Used by: tokio 1.54.1, tokio-stream 0.1.19, tokio-util 0.7.19 +Original notice: tokio 1.54.1 / LICENSE +Original notice: tokio-stream 0.1.19 / LICENSE Original notice: tokio-util 0.7.19 / LICENSE MIT License @@ -29822,7 +31564,7 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: adler2 2.0.1, anyhow 1.0.104, async-channel 2.5.0, async-executor 1.14.0, async-fs 2.2.0, async-io 2.6.0, async-lock 3.4.2, async-net 2.0.0, async-process 2.5.0, async-recursion 1.1.1, async-signal 0.2.14, async-task 4.7.1, async-trait 0.1.92, atomic-waker 1.1.2, blocking 1.7.0, concurrent-queue 2.5.0, curve25519-dalek-derive 0.1.1, displaydoc 0.2.7, dyn-clone 1.0.20, endi 1.1.1, enumn 0.1.14, erased-serde 0.4.10, event-listener 5.4.2, event-listener-strategy 0.5.4, fastrand 2.5.0, flume 0.12.0, futures-lite 2.6.1, group 0.14.0, home 0.5.12, inventory 0.3.24, itoa 1.0.18, khronos-egl 6.0.0, lazy_static 1.5.1, linux-raw-sys 0.12.1, linux-raw-sys 0.4.15, minimal-lexical 0.2.1, once_cell 1.21.4, ordered-stream 0.2.0, parking 2.2.1, paste 1.0.15, pin-project 1.1.13, pin-project-internal 1.1.13, pin-project-lite 0.2.17, piper 0.2.5, polling 3.11.0, prettyplease 0.2.37, proc-macro-crate 3.5.0, proc-macro2 1.0.107, process-wrap 9.1.1, quote 1.0.47, ref-cast 1.0.27, ref-cast-impl 1.0.27, rsa 0.10.0-rc.18, rustc-hash 1.1.0, rustc-hash 2.1.3, rustix 0.38.44, rustix 1.1.5, rustix-openpty 0.2.0, rustversion 1.0.23, semver 1.0.28, serde 1.0.229, serde-saphyr 1.3.0, serde_bytes 0.11.19, serde_core 1.0.229, serde_derive 1.0.229, serde_derive_internals 0.30.0, serde_json 1.0.151, serde_json_lenient 0.2.4, serde_repr 0.1.21, smol 2.0.2, smol_str 0.3.6, syn 1.0.109, syn 2.0.119, syn 3.0.6, thiserror 1.0.69, thiserror 2.0.21, thiserror-impl 1.0.69, thiserror-impl 2.0.21, typeid 1.0.3, unicode-ident 1.0.26, utf-8 0.7.6, waker-fn 1.2.0, zbus-lockstep 0.5.2, zbus-lockstep-macros 0.5.2, zed-font-kit 0.14.1-zed, zmij 1.0.23, zvariant_utils 4.2.0 +Used by: adler2 2.0.1, anyhow 1.0.104, async-channel 2.5.0, async-executor 1.14.1, async-fs 2.2.0, async-io 2.6.0, async-lock 3.4.2, async-net 2.0.0, async-process 2.5.0, async-recursion 1.1.1, async-signal 0.2.14, async-task 4.7.1, async-trait 0.1.92, atomic-waker 1.1.2, blocking 1.7.0, concurrent-queue 2.5.0, curve25519-dalek-derive 0.1.1, displaydoc 0.2.7, dyn-clone 1.0.20, endi 1.1.1, enumn 0.1.14, erased-serde 0.4.10, event-listener 5.4.2, event-listener-strategy 0.5.4, fastrand 2.5.0, flume 0.12.0, futures-lite 2.6.1, group 0.14.0, home 0.5.12, inventory 0.3.24, itoa 1.0.18, khronos-egl 6.0.0, lazy_static 1.5.1, linux-raw-sys 0.12.1, linux-raw-sys 0.4.15, minimal-lexical 0.2.1, once_cell 1.21.4, ordered-stream 0.2.0, parking 2.2.1, paste 1.0.15, pin-project 1.1.13, pin-project-internal 1.1.13, pin-project-lite 0.2.17, piper 0.2.5, polling 3.11.0, prettyplease 0.2.37, proc-macro-crate 3.5.0, proc-macro2 1.0.107, process-wrap 9.1.1, prost-reflect 0.16.5, protox 0.10.0, protox-parse 0.10.0, quote 1.0.47, ref-cast 1.0.27, ref-cast-impl 1.0.27, rsa 0.10.0-rc.18, rustc-hash 1.1.0, rustc-hash 2.1.3, rustix 0.38.44, rustix 1.1.5, rustix-openpty 0.2.0, rustversion 1.0.23, semver 1.0.28, serde 1.0.229, serde-saphyr 1.3.0, serde_bytes 0.11.19, serde_core 1.0.229, serde_derive 1.0.229, serde_derive_internals 0.30.0, serde_json 1.0.151, serde_json_lenient 0.2.4, serde_repr 0.1.21, smol 2.0.2, smol_str 0.3.6, syn 1.0.109, syn 2.0.119, syn 3.0.6, thiserror 1.0.69, thiserror 2.0.21, thiserror-impl 1.0.69, thiserror-impl 2.0.21, typeid 1.0.3, unicode-ident 1.0.26, utf-8 0.7.6, waker-fn 1.2.0, zbus-lockstep 0.5.2, zbus-lockstep-macros 0.5.2, zed-font-kit 0.14.1-zed, zmij 1.0.23, zvariant_utils 4.2.0 License: MIT Used by: allocator-api2 0.2.21, glow 0.17.0 Original notice: accesskit@0.24.1 / upstream/AccessKit-accesskit-license-mit-23f18e03dc49.txt @@ -29833,7 +31575,7 @@ Original notice: adler2 2.0.1 / LICENSE-MIT Original notice: allocator-api2 0.2.21 / LICENSE-MIT Original notice: anyhow 1.0.104 / LICENSE-MIT Original notice: async-channel 2.5.0 / LICENSE-MIT -Original notice: async-executor 1.14.0 / LICENSE-MIT +Original notice: async-executor 1.14.1 / LICENSE-MIT Original notice: async-fs 2.2.0 / LICENSE-MIT Original notice: async-io 2.6.0 / LICENSE-MIT Original notice: async-lock 3.4.2 / LICENSE-MIT @@ -29880,6 +31622,9 @@ Original notice: prettyplease 0.2.37 / LICENSE-MIT Original notice: proc-macro-crate 3.5.0 / LICENSE-MIT Original notice: proc-macro2 1.0.107 / LICENSE-MIT Original notice: process-wrap 9.1.1 / LICENSE-MIT +Original notice: prost-reflect 0.16.5 / LICENSE-MIT +Original notice: protox 0.10.0 / LICENSE-MIT +Original notice: protox-parse 0.10.0 / LICENSE-MIT Original notice: quote 1.0.47 / LICENSE-MIT Original notice: ref-cast 1.0.27 / LICENSE-MIT Original notice: ref-cast-impl 1.0.27 / LICENSE-MIT @@ -58881,6 +60626,33 @@ Legal Terms --- end of FTL.TXT --- +======================================================================== +License: MIT +Used by: pulldown-cmark 0.13.6 +Original notice: pulldown-cmark 0.13.6 / LICENSE + +The MIT License + +Copyright 2015 Google Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ======================================================================== Original notice: lyon@1.0.19, lyon_geom@1.0.19, lyon_path@1.0.19 / upstream/nical-lyon-license-mit-80f275e90d79.txt Source: https://raw.githubusercontent.com/nical/lyon/56219c95c658d4f39de7b190d98e57d5773ba241/LICENSE-MIT @@ -59671,6 +61443,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: hyper-timeout 0.5.2 +Original notice: hyper-timeout 0.5.2 / LICENSE-MIT + +The MIT License (MIT) + +Copyright (c) 2016 The weldr Project Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ======================================================================== License: MIT Used by: shellexpand 3.1.2 diff --git a/crates/acp/src/bridge.rs b/crates/acp/src/bridge.rs index 9a55a93a..12647bd9 100644 --- a/crates/acp/src/bridge.rs +++ b/crates/acp/src/bridge.rs @@ -14,7 +14,7 @@ use futures::{ future::{Either, select}, }; use nocterm_ai::{ - BridgeCall, BridgeLaunch, BridgeRegistration, BridgeRejection, ToolBridge, + BridgeCall, BridgeLaunch, BridgeRegistration, BridgeRejection, ToolBridge, ToolOffer, mcp::{McpSession, McpStep}, }; use nocterm_core::Paths; @@ -30,9 +30,16 @@ type Listener = std::os::unix::net::UnixListener; #[cfg(not(unix))] type Listener = std::net::TcpListener; +/// One connection's way to write lines, shared by its replies and the notifications sent to it, +/// so a line is never written into the middle of another. +type Writer = Arc>; + struct Registration { token: String, sockets: BTreeMap, + writers: BTreeMap, + /// What the chat offers its agent now: the tools listed and admitted. + offer: ToolOffer, cancel: async_channel::Sender<()>, cancelled: async_channel::Receiver<()>, } @@ -122,11 +129,14 @@ impl ToolBridge for BridgeServer { Registration { token: token.clone(), sockets: BTreeMap::new(), + writers: BTreeMap::new(), + offer: ToolOffer::default(), cancel, cancelled, }, ); let inner = Arc::downgrade(&self.inner); + let offering = inner.clone(); let launch = BridgeLaunch { program: relay.program.clone(), args: relay.args.clone(), @@ -143,7 +153,12 @@ impl ToolBridge for BridgeServer { revoke(&inner, id); } }), - )) + ) + .with_offer(Arc::new(move |id, offer| { + if let Some(inner) = offering.upgrade() { + set_offer(&inner, id, offer); + } + }))) } fn calls(&self) -> async_channel::Receiver { self.inner.calls_rx.clone() @@ -311,6 +326,44 @@ fn close_registration(registration: Registration) { } registration.cancel.close(); } +/// Records what a chat offers and, when it changed, tells its connected agents to list the +/// tools again. The notice is written on a thread of its own: a busy agent never blocks the chat. +fn set_offer(inner: &Inner, id: u64, offer: ToolOffer) { + let writers: Vec = { + let mut state = inner.state.lock().unwrap_or_else(|e| e.into_inner()); + let Some(registration) = state.registrations.get_mut(&id) else { + return; + }; + if registration.offer == offer { + return; + } + registration.offer = offer; + registration.writers.values().cloned().collect() + }; + if writers.is_empty() { + return; + } + let _ = std::thread::Builder::new() + .name("nocterm-bridge-notice".into()) + .spawn(move || { + for writer in writers { + let mut socket = writer.lock().unwrap_or_else(|e| e.into_inner()); + let _ = write_line(&mut socket, &nocterm_ai::mcp::list_changed()); + } + }); +} +fn write_line(socket: &mut Socket, value: &Value) -> io::Result<()> { + let bytes = serde_json::to_vec(value)?; + if bytes.len() > nocterm_ai::MAX_MCP_LINE { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "Bridge response too large", + )); + } + socket.write_all(&bytes)?; + socket.write_all(b"\n")?; + socket.flush() +} fn revoke(inner: &Inner, id: u64) { let registration = inner .state @@ -385,7 +438,7 @@ fn serve(socket: Socket, inner: Arc, epoch: u64, connection_id: u64) -> i .get("token") .and_then(Value::as_str) .unwrap_or_default(); - let (id, cancel) = { + let (id, cancel, writer) = { let mut state = inner.state.lock().unwrap_or_else(|e| e.into_inner()); if inner.generation.load(Ordering::Acquire) != epoch { return Err(io::Error::new( @@ -413,32 +466,44 @@ fn serve(socket: Socket, inner: Arc, epoch: u64, connection_id: u64) -> i "Thread connection limit", )); } + let writer: Writer = Arc::new(Mutex::new(socket.try_clone()?)); registration .sockets .insert(connection_id, socket.try_clone()?); - (*id, registration.cancelled.clone()) + registration.writers.insert(connection_id, writer.clone()); + (*id, registration.cancelled.clone(), writer) }; - let result = serve_authenticated(socket, &mut reader, &inner, id, cancel); + let result = socket + .set_read_timeout(None) + .and_then(|()| serve_authenticated(&writer, &mut reader, &inner, id, cancel)); let mut state = inner.state.lock().unwrap_or_else(|e| e.into_inner()); if let Some(registration) = state.registrations.get_mut(&id) { registration.sockets.remove(&connection_id); + registration.writers.remove(&connection_id); } result } fn serve_authenticated( - mut socket: Socket, + writer: &Writer, reader: &mut BufReader, inner: &Inner, id: u64, cancel: async_channel::Receiver<()>, ) -> io::Result<()> { - socket.set_read_timeout(None)?; let mut session = McpSession::default(); while let Some(line) = bounded_line(reader, nocterm_ai::MAX_MCP_LINE)? { if cancel.is_closed() { break; } - let response = match session.handle(&line) { + let offer = inner + .state + .lock() + .unwrap_or_else(|e| e.into_inner()) + .registrations + .get(&id) + .map(|registration| registration.offer) + .unwrap_or_default(); + let response = match session.handle(&line, offer) { McpStep::Reply(reply) => reply, McpStep::Ignore => continue, McpStep::Rejected { @@ -489,16 +554,10 @@ fn serve_authenticated( nocterm_ai::mcp::tool_result(request_id, result) } }; - let bytes = serde_json::to_vec(&response)?; - if bytes.len() > nocterm_ai::MAX_MCP_LINE { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "Bridge response too large", - )); - } - socket.write_all(&bytes)?; - socket.write_all(b"\n")?; - socket.flush()?; + write_line( + &mut writer.lock().unwrap_or_else(|e| e.into_inner()), + &response, + )?; } Ok(()) } diff --git a/crates/acp/src/bridge/tests.rs b/crates/acp/src/bridge/tests.rs index c8997696..ccf65df4 100644 --- a/crates/acp/src/bridge/tests.rs +++ b/crates/acp/src/bridge/tests.rs @@ -138,6 +138,46 @@ fn authenticates_routes_and_revokes_pending_calls() { } assert!(pending.respond.is_canceled()); } +fn listed(socket: &mut Socket, reader: &mut BufReader, id: u64) -> Vec { + writeln!( + socket, + "{}", + serde_json::json!({"jsonrpc":"2.0","id":id,"method":"tools/list"}) + ) + .unwrap(); + let response: Value = + serde_json::from_str(&bounded_line(reader, 1024 * 1024).unwrap().unwrap()).unwrap(); + response["result"]["tools"] + .as_array() + .unwrap() + .iter() + .map(|tool| tool["name"].as_str().unwrap().to_owned()) + .collect() +} +#[test] +fn a_chat_offering_file_tools_tells_its_agent_to_list_them() { + let dir = tempfile::tempdir().unwrap(); + let server = bridge(&dir); + let registration = server.register().unwrap(); + let (mut socket, mut reader) = authenticated(®istration); + initialize(&mut socket, &mut reader); + assert!(!listed(&mut socket, &mut reader, 2).contains(&"read_file".to_owned())); + registration.offer(ToolOffer::new(true)); + let notice: Value = + serde_json::from_str(&bounded_line(&mut reader, 1024 * 1024).unwrap().unwrap()).unwrap(); + assert_eq!(notice["method"], "notifications/tools/list_changed"); + assert!(notice.get("id").is_none()); + assert!(listed(&mut socket, &mut reader, 3).contains(&"read_file".to_owned())); + registration.offer(ToolOffer::new(true)); + registration.offer(ToolOffer::default()); + let notice: Value = + serde_json::from_str(&bounded_line(&mut reader, 1024 * 1024).unwrap().unwrap()).unwrap(); + assert_eq!( + notice["method"], "notifications/tools/list_changed", + "the same offer again sends nothing; a change does" + ); + assert!(!listed(&mut socket, &mut reader, 4).contains(&"read_file".to_owned())); +} #[test] fn rejects_bad_tokens_and_limits_thread_connections() { let dir = tempfile::tempdir().unwrap(); @@ -214,21 +254,15 @@ fn actual_stdio_relay_roundtrip() { .iter() .map(|tool| tool["name"].as_str().unwrap()) .collect(); + let catalog: std::collections::BTreeSet<_> = + ToolOffer::default().tools().map(|spec| spec.name).collect(); assert_eq!( - names, - [ - "list_terminals", - "open_terminal", - "read_terminal", - "send_input", - "run_command", - "exec_command", - "read_command", - "cancel_command", - ] - .into_iter() - .collect(), + names, catalog, + "a chat offers its terminal tools from the start" ); + for transfer in ["transfer_files", "read_transfer", "cancel_transfer"] { + assert!(names.contains(transfer)); + } } #[test] fn bounds_unauthenticated_connections_and_protocol_lines() { diff --git a/crates/acp/src/client/lifecycle_tests.rs b/crates/acp/src/client/lifecycle_tests.rs index 51acc68d..61ba1b45 100644 --- a/crates/acp/src/client/lifecycle_tests.rs +++ b/crates/acp/src/client/lifecycle_tests.rs @@ -32,6 +32,7 @@ for line in sys.stdin: args: vec!["-c".into(), script.into(), mode.into()], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.into(), terminal_auth: false, diff --git a/crates/acp/src/client/restore_classification.rs b/crates/acp/src/client/restore_classification.rs index b8466a3f..7f4513ea 100644 --- a/crates/acp/src/client/restore_classification.rs +++ b/crates/acp/src/client/restore_classification.rs @@ -45,6 +45,7 @@ for line in sys.stdin: ], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: dir.path().to_owned(), terminal_auth: false, diff --git a/crates/acp/src/client/startup_failures.rs b/crates/acp/src/client/startup_failures.rs index 3dc22f6d..0b5e5638 100644 --- a/crates/acp/src/client/startup_failures.rs +++ b/crates/acp/src/client/startup_failures.rs @@ -26,6 +26,7 @@ fn request( args, env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.to_owned(), terminal_auth: false, diff --git a/crates/acp/src/client/tests.rs b/crates/acp/src/client/tests.rs index bc31a139..25fe9227 100644 --- a/crates/acp/src/client/tests.rs +++ b/crates/acp/src/client/tests.rs @@ -51,6 +51,7 @@ for line in sys.stdin: args: vec!["-c".into(), script.into()], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.path().to_owned(), }) @@ -157,6 +158,7 @@ for line in sys.stdin: args: vec!["-c".into(), script], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.path().to_owned(), }) @@ -306,6 +308,7 @@ fn oversized_subprocess_line_fails_initialization_and_stops_processes() { ], env: Default::default(), inherit_env: Vec::new(), + hub: None, }; let result = AcpConnector::unmanaged().connect(ConnectRequest { terminal_auth: false, @@ -576,6 +579,7 @@ for line in sys.stdin: args: vec!["-u".into(), "-c".into(), script], env: Default::default(), inherit_env: vec!["PATH".into()], + hub: None, }; let connection = AcpConnector::unmanaged() .connect(ConnectRequest { diff --git a/crates/acp/src/lib.rs b/crates/acp/src/lib.rs index 7465087d..e7a87390 100644 --- a/crates/acp/src/lib.rs +++ b/crates/acp/src/lib.rs @@ -8,6 +8,8 @@ //! and, on Linux, the `run_agent_host` entry point with its `AgentHostError`. //! Settings section: none. Globals: none. //! Invariants: SDK types never leave this crate; relay registrations authenticate callers; revocation closes owned sockets. +//! The bridge lists and admits only the tools a registration's chat offers, and tells its +//! connected agents when the offer changes without blocking the chat. //! Native taskkill diagnostics retain the original I/O error through shared typed causes; //! the public process shutdown port retains its existing string diagnostic. mod bridge; diff --git a/crates/acp/src/mapping/content.rs b/crates/acp/src/mapping/content.rs index 5fe28580..fedc62a8 100644 --- a/crates/acp/src/mapping/content.rs +++ b/crates/acp/src/mapping/content.rs @@ -40,7 +40,7 @@ pub(super) fn content(value: wire::ContentBlock) -> Result }), wire::ContentBlock::Image(value) => Content::Image(Image { annotations: annotations(value.annotations)?, - data: value.data, + data: value.data.into(), mime_type: value.mime_type, uri: value.uri, meta: extensions(value.meta), @@ -112,7 +112,7 @@ pub(super) fn wire_content(value: Content) -> wire::ContentBlock { .meta(meta(value.meta)), ), Content::Image(value) => wire::ContentBlock::Image( - wire::ImageContent::new(value.data, value.mime_type) + wire::ImageContent::new(String::from(value.data), value.mime_type) .uri(value.uri) .annotations(wire_annotations(value.annotations)) .meta(meta(value.meta)), diff --git a/crates/acp/src/mapping/info.rs b/crates/acp/src/mapping/info.rs index 014d4bc7..239dbe0b 100644 --- a/crates/acp/src/mapping/info.rs +++ b/crates/acp/src/mapping/info.rs @@ -8,6 +8,8 @@ use nocterm_ai::model::{ pub(crate) fn capabilities(value: &wire::AgentCapabilities) -> AgentCapabilitiesSummary { AgentCapabilitiesSummary { image_prompts: value.prompt_capabilities.image, + keeps_history: false, + own_tools: false, } } /// Authentication methods of an unknown future shape are not offered. diff --git a/crates/acp/src/mapping/requests.rs b/crates/acp/src/mapping/requests.rs index 344b90d7..e0f849d3 100644 --- a/crates/acp/src/mapping/requests.rs +++ b/crates/acp/src/mapping/requests.rs @@ -56,6 +56,7 @@ pub(crate) fn opened(value: wire::NewSessionResponse) -> SessionOpened { session: SessionId::new(value.session_id.to_string()), modes: value.modes.map(modes), configuration: value.config_options.map(options), + history: None, } } pub(crate) fn prompt_outcome(value: wire::PromptResponse) -> Result { diff --git a/crates/acp/src/process.rs b/crates/acp/src/process.rs index 54bab89b..7244332e 100644 --- a/crates/acp/src/process.rs +++ b/crates/acp/src/process.rs @@ -210,6 +210,7 @@ mod tests { args, env: BTreeMap::new(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.to_owned(), } diff --git a/crates/agent-chat/Cargo.toml b/crates/agent-chat/Cargo.toml index 150ff2e3..dbab22f0 100644 --- a/crates/agent-chat/Cargo.toml +++ b/crates/agent-chat/Cargo.toml @@ -21,9 +21,9 @@ nocterm-agent-runtime.workspace = true nocterm-agent-directory.workspace = true nocterm-store.workspace = true nocterm-session.workspace = true +nocterm-transfers.workspace = true nocterm-workspace-ports.workspace = true futures.workspace = true -async-channel.workspace = true serde.workspace = true serde_json.workspace = true tracing.workspace = true @@ -34,6 +34,8 @@ gpui = { workspace = true, features = ["test-support"] } nocterm-session = { workspace = true, features = ["test-support"] } nocterm-ai = { workspace = true, features = ["test-support"] } nocterm-agent-runtime = { workspace = true, features = ["test-support"] } +nocterm-hubs-model.workspace = true +nocterm-transfers = { workspace = true, features = ["test-support"] } tempfile.workspace = true [lints] diff --git a/crates/agent-chat/src/chat_deps.rs b/crates/agent-chat/src/chat_deps.rs index cd7a3e21..7b8a0ce8 100644 --- a/crates/agent-chat/src/chat_deps.rs +++ b/crates/agent-chat/src/chat_deps.rs @@ -10,8 +10,9 @@ use nocterm_agent_directory::AgentsHandle; use nocterm_agent_runtime::{CatalogRow, ChatOpenError, DocumentClaim, DocumentClaimOutcome}; use nocterm_ai::{ history::{ContextId, SavedChat}, + local_environment::ChatScope, metadata::ChatMetadataSource, - storage::{AgentStorageError, require_chat_scope}, + storage::{AgentStorageError, require_local_scope}, }; use nocterm_store::SettingsHandle; use std::{rc::Rc, sync::Arc}; @@ -61,7 +62,7 @@ impl ChatDeps { self.settings().setting::(cx) } pub fn enabled(&self, cx: &App) -> bool { - self.ai(cx).enabled + self.runtime().check_current().is_ok() && self.ai(cx).enabled } /// What chats may do with the window. pub fn seat(&self) -> &Seat { @@ -74,15 +75,31 @@ impl ChatDeps { pub fn timestamp(&self) -> u64 { self.inner.metadata.timestamp() } - pub fn new_chat(&self, agent: String) -> SavedChat { + pub fn new_chat(&self, agent: String) -> Result { + self.runtime().check_current()?; + let scope = self.runtime().chat_scope()?; + Ok(self.frame_chat(agent, scope)) + } + fn frame_chat(&self, agent: String, scope: ChatScope) -> SavedChat { let identity = self.inner.metadata.new_identity(); - SavedChat::for_scope(agent, identity.id, identity.created, self.scope()) + SavedChat::for_environment(agent, identity.id, identity.created, scope) + } + pub(crate) fn new_chat_for_origin( + &self, + agent: String, + origin: &nocterm_ai::execution_origin::ChatKey, + ) -> SavedChat { + self.frame_chat(agent, origin.chat_scope()) } pub fn scope(&self) -> ContextId { self.runtime().scope() } - pub(crate) fn require_scope(&self, actual: ContextId) -> Result<(), AgentStorageError> { - require_chat_scope(self.scope(), actual) + pub fn chat_scope(&self) -> Result { + self.runtime().chat_scope() + } + pub(crate) fn require_local_scope(&self, actual: &ChatScope) -> Result<(), AgentStorageError> { + self.runtime().check_current()?; + require_local_scope(&self.chat_scope()?, actual) } fn reserve( &self, @@ -90,7 +107,7 @@ impl ChatDeps { replacing: Option, cx: &mut App, ) -> Result<(DocumentClaim, Self), ChatOpenError> { - self.require_scope(key.scope())?; + self.require_local_scope(&key.chat_scope())?; // Seat.id is the original root window entity, not an inner terminal surface. let window = self.seat().id(); let outcome = match replacing { @@ -113,7 +130,7 @@ impl ChatDeps { &self, key: &nocterm_ai::execution_origin::ChatKey, ) -> Result { - self.require_scope(key.scope())?; + self.require_local_scope(&key.chat_scope())?; Ok(Self { inner: Rc::new(Deps { runtime: self.inner.runtime.clone(), @@ -130,9 +147,38 @@ impl ChatDeps { claim: DocumentClaim, deps: Self, cx: &mut App, - build: impl FnOnce(Self, &mut Context) -> AgentThread, + build: impl FnOnce( + Self, + crate::thread::local_payloads::LocalPayloadAllocation, + &mut Context, + ) -> AgentThread, ) -> Result, ChatOpenError> { - let thread = cx.new(|cx| build(deps, cx)); + let allocation = self.reserve_payload(cx)?; + self.build_allocated(claim, deps, allocation, cx, build) + } + fn reserve_payload( + &self, + cx: &mut App, + ) -> Result { + let allocations = self.runtime().local_history_allocations(cx); + let resident = allocations + .reserve_resident(None, cx) + .map_err(|error| ChatOpenError::Storage(error.into_storage()))?; + Ok((allocations, resident)) + } + fn build_allocated( + &self, + claim: DocumentClaim, + deps: Self, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, + cx: &mut App, + build: impl FnOnce( + Self, + crate::thread::local_payloads::LocalPayloadAllocation, + &mut Context, + ) -> AgentThread, + ) -> Result, ChatOpenError> { + let thread = cx.new(|cx| build(deps, allocation, cx)); self.runtime() .commit_document(claim, crate::thread::client(&thread), cx)?; Ok(thread) @@ -142,7 +188,11 @@ impl ChatDeps { key: nocterm_ai::execution_origin::ChatKey, replacing: Option, cx: &mut App, - build: impl FnOnce(Self, &mut Context) -> AgentThread, + build: impl FnOnce( + Self, + crate::thread::local_payloads::LocalPayloadAllocation, + &mut Context, + ) -> AgentThread, ) -> Result, ChatOpenError> { let (claim, deps) = self.reserve(key, replacing, cx)?; self.build_committed(claim, deps, cx, build) @@ -152,12 +202,11 @@ impl ChatDeps { agent: String, cx: &mut App, ) -> Result, ChatOpenError> { - let chat = self.new_chat(agent); + let chat = self.new_chat(agent)?; chat.validate_identity().map_err(AgentStorageError::from)?; - let key = - nocterm_ai::execution_origin::ChatKey::new(chat.header.scope, chat.header.id.clone()); - self.build_claimed(key, None, cx, |deps, cx| { - let mut thread = AgentThread::new(chat, deps, cx); + let key = nocterm_ai::execution_origin::ChatKey::from_header(&chat.header); + self.build_claimed(key, None, cx, |deps, allocation, cx| { + let mut thread = AgentThread::new(chat, deps, allocation, cx); thread.preview_config(cx); thread }) @@ -168,9 +217,12 @@ impl ChatDeps { expected: EntityId, cx: &mut App, ) -> Result, ChatOpenError> { - self.build_claimed(data.key().clone(), Some(expected), cx, |deps, cx| { - AgentThread::restarted(data, deps, cx) - }) + self.build_claimed( + data.key().clone(), + Some(expected), + cx, + |deps, allocation, cx| AgentThread::restarted(data, deps, allocation, cx), + ) } pub(crate) fn fork_chat( &self, @@ -179,10 +231,8 @@ impl ChatDeps { ) -> Result, ChatOpenError> { let fork = fork.prepared().map_err(AgentStorageError::from)?; let (claim, deps) = self.reserve(fork.key(), None, cx)?; - self.runtime() - .admit_claimed_history(&claim, fork.resident_bytes(), cx)?; - self.build_committed(claim, deps, cx, |deps, cx| { - AgentThread::forked(fork, deps, cx) + self.build_committed(claim, deps, cx, |deps, allocation, cx| { + AgentThread::forked(fork, deps, allocation, cx) }) } /// Reserve exact ownership before constructing an entity or starting work. @@ -191,16 +241,15 @@ impl ChatDeps { chat: SavedChat, cx: &mut App, ) -> Result, ChatOpenError> { - self.require_scope(chat.header.scope)?; + self.require_local_scope(&chat.header.chat_scope())?; chat.validate_identity().map_err(AgentStorageError::from)?; - let key = - nocterm_ai::execution_origin::ChatKey::new(chat.header.scope, chat.header.id.clone()); - let chat = nocterm_ai::history::prepare(chat).map_err(AgentStorageError::from)?; + let key = nocterm_ai::execution_origin::ChatKey::from_header(&chat.header); + let chat = nocterm_ai::history::prepare_owned(chat).map_err(AgentStorageError::from)?; let (claim, deps) = self.reserve(key, None, cx)?; self.runtime() .admit_claimed_history(&claim, chat.resident_bytes(), cx)?; - self.build_committed(claim, deps, cx, |deps, cx| { - AgentThread::restored(chat, deps, cx) + self.build_committed(claim, deps, cx, |deps, allocation, cx| { + AgentThread::restored(chat, deps, allocation, cx) }) } /// Admit catalog metadata and ownership before exposing a saved chat. @@ -208,17 +257,47 @@ impl ChatDeps { &self, row: CatalogRow, cx: &mut App, + ) -> Result, ChatOpenError> { + self.restore_archive_as(row, None, cx) + } + /// Restores archived chat `row` in place of `expected`, the entity another window held it + /// in, which keeps nothing unsaved. + pub(crate) fn restore_archive_in_place_of( + &self, + row: CatalogRow, + expected: EntityId, + cx: &mut App, + ) -> Result, ChatOpenError> { + self.restore_archive_as(row, Some(expected), cx) + } + fn restore_archive_as( + &self, + row: CatalogRow, + replacing: Option, + cx: &mut App, ) -> Result, ChatOpenError> { let summary = row.summary(); - self.require_scope(summary.scope)?; + self.require_local_scope(&summary.chat_scope())?; summary.validate().map_err(AgentStorageError::from)?; - let key = nocterm_ai::execution_origin::ChatKey::new(summary.scope, summary.id.clone()); - let (claim, deps) = self.reserve(key, None, cx)?; + let key = nocterm_ai::execution_origin::ChatKey::from_summary(summary); + let (claim, deps) = self.reserve(key, replacing, cx)?; self.runtime().admit_claimed_history(&claim, 0, cx)?; - let prepared = self.runtime().take_prepared_chat(&claim, cx)?; - self.build_committed(claim, deps, cx, |deps, cx| match prepared { - Some(chat) => AgentThread::restored(chat, deps, cx), - None => AgentThread::archived(row, deps, cx), - }) + let allocation = self.reserve_payload(cx)?; + let prepared = self + .runtime() + .take_prepared_chat(&claim, allocation.1.clone(), cx)?; + match prepared { + Some(prepared) => { + let built = prepared.map(|chat| { + self.build_allocated(claim, deps, allocation, cx, |deps, allocation, cx| { + AgentThread::restored(chat, deps, allocation, cx) + }) + }); + built.value().clone() + } + None => self.build_allocated(claim, deps, allocation, cx, |deps, allocation, cx| { + AgentThread::archived(row, deps, allocation, cx) + }), + } } } diff --git a/crates/agent-chat/src/lib.rs b/crates/agent-chat/src/lib.rs index ed8da07a..9b89870b 100644 --- a/crates/agent-chat/src/lib.rs +++ b/crates/agent-chat/src/lib.rs @@ -1,7 +1,11 @@ //! Agent chats without views: one [`thread::AgentThread`] per chat and the //! [`roster::ChatRoster`] of a window's selected and retained context chats. -//! Command jobs privately assemble their owned runner; snapshots and sign-in waits expose -//! immutable facts. +//! Command jobs use a supplied session-domain registry; the chat owns scheduling its runner. +//! File transfers go through a supplied `TransferRequests` port; each chat reads and cancels +//! only the transfers it submitted, bound to both endpoint terminals' sessions. +//! A chat offers the file tools while an attached terminal serves files but runs no programs, +//! and runs them only on such terminals, on the filesystem captured with the call's session. +//! Snapshots and sign-in waits expose immutable facts. //! //! Owns: AgentThread coordination, private Composer/ChatConfig/ChatDocument owners, private //! SessionDriver and ToolRuntime owners, the session-scoped ApprovalInbox for provider @@ -9,18 +13,23 @@ //! lifecycle (new, restart, fork, restore and removal admission). //! Runtime owns installation-wide history admission. Catalog archives retain an accounted //! row reference; quiet hidden archives release their entities and references. -//! ChatDeps binds an immutable runtime scope and exact per-chat original seat; every entity factory +//! ChatDeps binds an immutable Local environment lease, context and exact per-chat original seat; every entity factory //! acquires a scoped document claim and binds the seat before constructing an entity. ChatDocument identity retains its ChatKey. //! ThreadNotice owns status/error presentation facts; ThreadBindings retains the //! window/subscriptions. Finite operations return named completions and original-authority //! polling projections. Archive owns lazy hydration generations, typed pending intentions //! and delivery residency. BackgroundOpen owns the native-opening handoff and its original //! workspace receipt. +//! A hub agent's chat is an ordinary chat: an agent that keeps its sessions reopens them +//! without a saved transcript and replaces it with the history it returns, and a turn the +//! agent runs for another device shows as a turn of the chat (ADR 0148). +//! The private Transcript admits live byte/row growth before mutation; capacity failure preserves accepted rows and queued prompts. //! Does not own: views, the workspace shell, ACP transport, connection admission, -//! filesystem storage or image decoding. +//! filesystem storage, native command execution or image decoding. //! Ports used: AgentConnector and ToolBridge through the runtime, terminal ports from //! nocterm-workspace-ports, [`seat::ChatWorkspace`], [`sign_in::SignInTerminal`] and -//! ChatMetadataSource, all supplied with [`chat_deps::ChatDeps`]. +//! ChatMetadataSource, all supplied with [`chat_deps::ChatDeps`]. The retained runtime supplies +//! CommandJobsFactory; each chat creates its own bounded registry without depending on an adapter. //! Ports provided: [`seat::ChatWorkspace`] and [`sign_in::SignInTerminal`] for the window //! shell to implement. //! Settings section: reads ai; its schema belongs to nocterm-ai. @@ -37,7 +46,8 @@ //! old publication failures cannot pause a newer desired document. //! Session operations carry their original ticket, connection, session and registration //! authority. Tool work retains fixed prompt scope, original executor identities and -//! workspace receipts; cancellation preserves readable job outcomes. +//! workspace receipts; cancellation preserves readable job outcomes. Actual tool dispatch and +//! every background runner poll recheck their original account lease. //! Joined reconnect calls retain their original busy guards and dispatch in acceptance //! order after validating original session, registration, workspace and window authority. //! Ordering releases after synchronous launch; direct calls after connection retain diff --git a/crates/agent-chat/src/roster.rs b/crates/agent-chat/src/roster.rs index 9a9eceb4..7517d916 100644 --- a/crates/agent-chat/src/roster.rs +++ b/crates/agent-chat/src/roster.rs @@ -69,7 +69,7 @@ impl ChatRoster { } /// Switches the visible collection; retained threads keep original hosts. pub fn switch_context(&mut self, host: ChatDeps, cx: &mut App) -> bool { - if host.scope() == self.host().scope() { + if host.chat_scope().ok() == self.host().chat_scope().ok() { return self.contexts.select(host); } if let Some(previous) = self.current() { @@ -93,6 +93,35 @@ impl ChatRoster { } true } + /// Retain device-local snapshots before removing an unavailable original source. + /// This never grants a new account lease or redirects the old writer. + pub fn retire_stale_sources(&mut self, cx: &mut App) -> Retained { + let stale = self + .contexts + .all() + .flat_map(|scope| { + scope + .chats() + .iter() + .filter(|thread| !thread.read(cx).source_current()) + .map(|thread| (scope.host().runtime().clone(), thread.entity_id())) + }) + .collect::>(); + let mut removed = Vec::new(); + let mut error = None; + for (runtime, id) in stale { + match runtime.capture_and_detach_document(id, cx) { + Ok(true) => removed.push(id), + Ok(false) => {} + Err(cause) => { + error = Some(RetainError(cause)); + break; + } + } + } + self.contexts.remove_all(&removed); + Retained { removed, error } + } pub fn current(&self) -> Option> { self.contexts.current().chats().current() } @@ -109,12 +138,12 @@ impl ChatRoster { cx: &App, ) -> Result { let thread = slot.thread(); - let scope = thread.read(cx).document().key().scope(); + let scope = thread.read(cx).document().key().chat_scope(); let id = thread.entity_id(); - let visible = scope == self.host().scope(); + let visible = Some(&scope) == self.host().chat_scope().ok().as_ref(); let origin = self .contexts - .scope_mut(scope) + .scope_mut(&scope) .ok_or(nocterm_agent_runtime::ChatOpenError::Closed)?; origin.chats_mut().append(slot); if !visible { @@ -134,6 +163,14 @@ impl ChatRoster { pub fn is_empty(&self) -> bool { self.contexts.current().chats().is_empty() } + /// Leaves the Local roster with no selected document when another captured + /// conversation is presented. Uses the same Local visibility transition. + pub fn deselect(&mut self, cx: &mut App) { + if let Some(previous) = self.current() { + previous.update(cx, |thread, cx| thread.set_shown(false, cx)); + } + self.contexts.current_mut().chats_mut().deselect(); + } /// Opens chat `id`, hiding the chat open before. pub fn select(&mut self, id: EntityId, cx: &mut App) { if let Some(previous) = self.current() { @@ -207,26 +244,77 @@ impl ChatRoster { .origin(source.entity_id()) .ok_or(nocterm_agent_runtime::ChatOpenError::Closed)? .host(); - host.require_scope(source.read(cx).document().key().scope())?; + host.require_local_scope(&source.read(cx).document().key().chat_scope())?; let fork = match last { - Some(last) => source.read(cx).fork_at(last + 1), - None => source.read(cx).fork(), + Some(last) => source.update(cx, |thread, cx| thread.fork_at(last + 1, cx)), + None => source.update(cx, |thread, cx| thread.fork(cx)), }; - let thread = host.fork_chat(fork, cx)?; + let thread = host.fork_chat(fork?, cx)?; thread.update(cx, |thread, cx| thread.persist(cx)); Ok(thread) } + /// The chats of this collection other windows hold, which this window lists without + /// holding them. + pub fn held_elsewhere(&self, cx: &App) -> Vec> { + let host = self.host(); + host.runtime() + .held_elsewhere(host.seat().id(), cx) + .into_iter() + .filter_map(|chat| chat.upgrade()?.downcast::().ok()) + .collect() + } + /// Opens in this window chat `chat`, which another window holds, in its place: while it + /// neither works nor shows there, with what that window had of it. `None` when it stays, + /// or is an archived chat whose row the catalog is reloading. + pub fn take_over( + &self, + chat: &Entity, + cx: &mut App, + ) -> Result>, nocterm_agent_runtime::ChatOpenError> { + let host = self.host(); + let expected = chat.entity_id(); + if !host.runtime().may_move(expected, host.seat().id(), cx) { + return Ok(None); + } + if chat.read(cx).is_archived() { + if !chat.read(cx).can_release_archive() { + return Ok(None); + } + let id = chat.read(cx).chat_id().to_owned(); + let row = host.runtime().catalog_snapshot(cx).and_then(|catalog| { + (0..catalog.rows().len()) + .filter_map(|index| catalog.row(index)) + .find(|row| row.summary().id == id) + }); + return row + .map(|row| host.restore_archive_in_place_of(row, expected, cx)) + .transpose(); + } + chat.update(cx, |thread, cx| thread.release_resources(cx)); + let data = chat + .update(cx, |thread, cx| thread.restart_data(cx)) + .map_err(nocterm_agent_runtime::ChatOpenError::from)?; + host.restart_chat(data, expected, cx).map(Some) + } /// The chats saved in earlier runs and not listed yet, newest first: - /// loaded chats, then archived summaries. + /// loaded chats, then archived summaries. A chat another window holds stays there. pub fn saved(&self, cx: &mut App) -> Vec> { let Some(catalog) = self.host().runtime().catalog_snapshot(cx) else { return Vec::new(); }; + let elsewhere: std::collections::HashSet = self + .held_elsewhere(cx) + .iter() + .map(|thread| thread.read(cx).chat_id().to_owned()) + .collect(); let mut restored = Vec::new(); for index in (0..catalog.rows().len()).rev() { let Some(row) = catalog.row(index) else { continue; }; + if elsewhere.contains(&row.summary().id) { + continue; + } if self .contexts .current() @@ -239,6 +327,8 @@ impl ChatRoster { } match self.host().restore_archive(row, cx) { Ok(thread) => restored.push(thread), + // Another window is opening it now. + Err(nocterm_agent_runtime::ChatOpenError::AlreadyOpen { .. }) => {} Err(error) => tracing::warn!(%error, "could not admit archived chat"), } } @@ -347,7 +437,13 @@ impl ChatRoster { match self.host().runtime().trim_history(cx) { Ok(evicted) => evicted .into_iter() - .filter(|key| key.scope() == self.host().scope() && matches.contains(key.id())) + .filter(|key| { + self.host() + .chat_scope() + .as_ref() + .is_ok_and(|scope| key.chat_scope() == *scope) + && matches.contains(key.id()) + }) .map(|key| key.id().to_owned()) .collect(), Err(error) => { diff --git a/crates/agent-chat/src/roster/chat_list.rs b/crates/agent-chat/src/roster/chat_list.rs index 8fec1b1b..53179d12 100644 --- a/crates/agent-chat/src/roster/chat_list.rs +++ b/crates/agent-chat/src/roster/chat_list.rs @@ -23,6 +23,9 @@ pub struct ChatList { active: Option, } impl ChatList { + pub fn deselect(&mut self) { + self.active = None; + } pub fn current(&self) -> Option> { self.active.and_then(|id| self.find(id)).cloned() } diff --git a/crates/agent-chat/src/roster/contexts.rs b/crates/agent-chat/src/roster/contexts.rs index 7cccaf74..a7c8587e 100644 --- a/crates/agent-chat/src/roster/contexts.rs +++ b/crates/agent-chat/src/roster/contexts.rs @@ -1,7 +1,7 @@ //! Selected and retained chat collections keep immutable original dependencies. use super::{ChatDeps, ChatList}; use gpui::EntityId; -use nocterm_ai::history::ContextId; +use nocterm_ai::local_environment::ChatScope; use std::collections::HashMap; pub(super) struct ScopeChats { @@ -27,7 +27,7 @@ impl ScopeChats { } pub(super) struct ContextChats { selected: ScopeChats, - hidden: HashMap, + hidden: HashMap, } impl ContextChats { pub(super) fn new(host: ChatDeps) -> Self { @@ -61,11 +61,17 @@ impl ContextChats { pub(super) fn origin(&self, id: EntityId) -> Option<&ScopeChats> { self.all().find(|scope| scope.chats.contains(id)) } - pub(super) fn scope_mut(&mut self, scope: ContextId) -> Option<&mut ScopeChats> { - if self.selected.host.scope() == scope { + pub(super) fn scope_mut(&mut self, scope: &ChatScope) -> Option<&mut ScopeChats> { + if self + .selected + .host + .chat_scope() + .as_ref() + .is_ok_and(|current| current == scope) + { Some(&mut self.selected) } else { - self.hidden.get_mut(&scope) + self.hidden.get_mut(scope) } } pub(super) fn all(&self) -> impl Iterator { @@ -79,21 +85,23 @@ impl ContextChats { }); } pub(super) fn select(&mut self, host: ChatDeps) -> bool { - let scope = host.scope(); - if scope == self.selected.host.scope() { + let scope = host.chat_scope().ok(); + if scope == self.selected.host.chat_scope().ok() { self.selected.host = host; return false; } - let mut next = self - .hidden - .remove(&scope) + let mut next = scope + .as_ref() + .and_then(|scope| self.hidden.remove(scope)) .unwrap_or_else(|| ScopeChats::new(host.clone())); // Future factories use the fresh original surface. Existing entities keep // their own previously captured ChatDeps/Seat and are never retargeted. next.host = host; let previous = std::mem::replace(&mut self.selected, next); - if !previous.chats.is_empty() { - self.hidden.insert(previous.host.scope(), previous); + if !previous.chats.is_empty() + && let Ok(scope) = previous.host.chat_scope() + { + self.hidden.insert(scope, previous); } true } diff --git a/crates/agent-chat/src/thread.rs b/crates/agent-chat/src/thread.rs index 12adf225..c54e67c6 100644 --- a/crates/agent-chat/src/thread.rs +++ b/crates/agent-chat/src/thread.rs @@ -34,7 +34,7 @@ mod execution; mod permissions; mod reconnects; mod session_grants; -pub use approval_inbox::ToolApprovalCard; +pub use approval_inbox::{ApprovalToken, ToolApprovalCard}; pub use permissions::PermissionCard; mod queue; mod tool_display; @@ -45,15 +45,17 @@ mod session_driver; mod tool_runtime; pub use restart::RestartData; mod document; +pub(crate) mod local_payloads; mod saved; mod sign_in_wait; +mod title; pub use sign_in_wait::SignInWait; mod session; mod tools; /// A chat's title from its name and transcript, as the history shows it. #[cfg(any(test, feature = "test-support"))] pub fn chat_title(name: Option<&str>, state: &ThreadState) -> String { - saved::chat_title(name, state) + title::chat_title(name, state) } #[derive(Clone, Debug, PartialEq, Eq)] @@ -80,28 +82,8 @@ impl Restore { self.fork } } -/// What a forked chat starts from. -pub struct Fork { - chat: nocterm_ai::history::SavedChat, - restore: Option, - attachments: Vec, -} - -impl Fork { - pub(crate) fn prepared(mut self) -> Result { - self.chat = nocterm_ai::history::prepare(self.chat)?; - Ok(self) - } - pub(crate) fn resident_bytes(&self) -> usize { - self.chat.resident_bytes() - } - pub(crate) fn key(&self) -> nocterm_ai::execution_origin::ChatKey { - nocterm_ai::execution_origin::ChatKey::new( - self.chat.header.scope, - self.chat.header.id.clone(), - ) - } -} +mod fork; +pub use fork::Fork; pub struct AgentThread { session: session_driver::SessionDriver, @@ -113,6 +95,7 @@ pub struct AgentThread { notice: notice::ThreadNotice, deps: crate::chat_deps::ChatDeps, bindings: bindings::ThreadBindings, + payloads: local_payloads::LocalPayloads, } impl AgentThread { @@ -186,6 +169,12 @@ impl AgentThread { .is_none_or(|info| info.at_prompt != Some(true) || info.dirty_input || info.alt_screen) } + pub fn tool_approval_tokens(&self) -> Vec { + self.tools.approval_tool_tokens() + } + pub fn tool_approval_call(&self, token: ApprovalToken) -> Option<&nocterm_ai::TerminalCall> { + self.tools.approval_tool(token) + } pub fn tool_cards(&self) -> Vec { self.tools.approval_tool_cards() } @@ -209,6 +198,7 @@ impl AgentThread { } pub fn context(&mut self, cx: &App) -> String { + self.offer_tools(cx); let input = self::tool_runtime::resolution::Resolution::observe(self.deps.seat(), cx); self.tools .context(&input, self.composer.default_attachments(), cx) @@ -346,11 +336,17 @@ impl AgentThread { fn on_release(&mut self, id: EntityId, cx: &mut gpui::App) { self.cancel_pending(); self.finalize_tool_displays(); - let snapshot = self - .deps - .enabled(cx) - .then(|| self.capture_pending(cx)) - .flatten(); + let snapshot = if self.deps.enabled(cx) { + match self.capture_pending(cx) { + Ok(snapshot) => snapshot, + Err(error) => { + tracing::warn!(%error, "released chat capture failed"); + None + } + } + } else { + None + }; self.cancel_execution(); // The lease releases its connection when it is dropped. self.session.release_lease(); @@ -384,6 +380,7 @@ impl AgentThread { fn from_document( state: document::ChatDocument, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { let this = cx.weak_entity(); @@ -397,7 +394,7 @@ impl AgentThread { let release = cx.on_release(move |this, cx| this.on_release(id, cx)); Self { session: Default::default(), - tools: Default::default(), + tools: tool_runtime::ToolRuntime::attached(deps.runtime(), cx), archive: Default::default(), composer: Default::default(), state, @@ -405,39 +402,44 @@ impl AgentThread { notice: Default::default(), deps, bindings: bindings::ThreadBindings::new(release, authority), + payloads: local_payloads::LocalPayloads::new(allocation.0, allocation.1, id), } } pub(crate) fn new( chat: nocterm_ai::history::SavedChat, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { Self::from_document( document::ChatDocument::new( - chat.header.agent_id, - nocterm_ai::execution_origin::ChatKey::new(chat.header.scope, chat.header.id), + chat.header.agent_id.clone(), + nocterm_ai::execution_origin::ChatKey::from_header(&chat.header), chat.header.updated, ), deps, + allocation, cx, ) } pub(crate) fn archived( row: nocterm_agent_runtime::CatalogRow, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { let summary = row.summary(); let mut state = document::ChatDocument::new( summary.agent_id.clone(), - nocterm_ai::execution_origin::ChatKey::new(summary.scope, summary.id.clone()), + nocterm_ai::execution_origin::ChatKey::from_summary(summary), summary.updated, ); state.archived_clean(summary); - let mut thread = Self::from_document(state, deps, cx); + let mut thread = Self::from_document(state, deps, allocation, cx); thread.config.saved_model(summary.model.clone()); thread.notice.present("Saved chat".into()); thread.archive = archive::Archive::archived(row); + thread.payloads.archive(); thread } pub fn is_archived(&self) -> bool { @@ -455,6 +457,10 @@ impl AgentThread { pub fn take_changed_rows(&mut self) -> std::collections::HashSet { self.state.take_changed_rows() } + /// Whether a row changed since the last [`Self::take_changed_rows`], without taking it. + pub fn has_changed_rows(&self) -> bool { + self.state.has_changed_rows() + } pub fn document(&self) -> &document::ChatDocument { &self.state } @@ -532,7 +538,6 @@ impl AgentThread { pub fn session_accepts_updates(&self) -> bool { self.session.accepts_updates() } - #[cfg(any(test, feature = "test-support"))] pub fn session_stopped(&self) -> bool { self.session.stopped() } diff --git a/crates/agent-chat/src/thread/approval_inbox.rs b/crates/agent-chat/src/thread/approval_inbox.rs index d9a20553..a79951ec 100644 --- a/crates/agent-chat/src/thread/approval_inbox.rs +++ b/crates/agent-chat/src/thread/approval_inbox.rs @@ -33,6 +33,7 @@ pub(super) struct ToolGate { attached: bool, unsafe_input: bool, expected: Option, + source_expected: Option, } impl ToolGate { pub(super) fn from_context( @@ -51,14 +52,20 @@ impl ToolGate { cx: &gpui::App, ) -> Self { let attached = match (call.terminal_id(), call.server_id()) { - (Some(id), _) => resolved.iter().any(|(candidate, _, _)| candidate == id), + (Some(_), _) => call + .terminals() + .all(|id| resolved.iter().any(|(candidate, _, _)| candidate == id)), (None, Some(id)) => offline.iter().any(|(server, _)| server.server_id == id), (None, None) => true, }; - let info = resolved - .iter() - .find(|(id, _, _)| Some(id.as_str()) == call.terminal_id()) - .and_then(|(_, entry, _)| entry.capabilities.state().info(cx)); + let expected_of = |wanted: Option<&str>| { + resolved + .iter() + .find(|(id, _, _)| Some(id.as_str()) == wanted) + .and_then(|(_, entry, _)| entry.capabilities.state().info(cx)) + }; + let info = expected_of(call.terminal_id()); + let source_expected = expected_of(call.source_terminal_id()).map(|info| info.expected); let unsafe_input = matches!(call, TerminalCall::SendInput(_)) && info.as_ref().is_none_or(|info| { info.at_prompt != Some(true) || info.dirty_input || info.alt_screen @@ -70,10 +77,18 @@ impl ToolGate { unsafe_input, info.map(|info| info.expected), ) + .with_source(source_expected) } pub(super) fn expected(&self) -> Option { self.expected } + pub(super) fn source_expected(&self) -> Option { + self.source_expected + } + fn with_source(mut self, source: Option) -> Self { + self.source_expected = source; + self + } pub(super) fn new( available: bool, @@ -88,6 +103,7 @@ impl ToolGate { attached, unsafe_input, expected, + source_expected: None, } } fn accepts(&self, call: &BridgeCall) -> bool { @@ -162,6 +178,8 @@ impl ApprovalInbox { return ToolDecision::Respond(call.into_call(), ToolRejection::Unavailable); } self.grants.observe(&call.call().call, gate.expected); + self.grants + .observe_source(&call.call().call, gate.source_expected); if self.grants.requires_approval(&call.call().call, policy) || gate.unsafe_input { self.generation = self.generation.wrapping_add(1); self.tools.push(PendingTool { @@ -187,15 +205,18 @@ impl ApprovalInbox { if !allow { return ToolDecision::Respond(call.into_call(), ToolRejection::Denied); } - if !gate.accepts(call.call()) || !gate.attached || call.expected() != gate.expected { + if !gate.accepts(call.call()) + || !gate.attached + || call.expected() != gate.expected + || call.source() != gate.source_expected + { return ToolDecision::Respond(call.into_call(), ToolRejection::Detached); } self.grants.observe(&call.call().call, gate.expected); - if grant - && !gate.unsafe_input - && let Some(id) = call.call().call.target() - { - self.grants.grant(id, call.call().call.capability()); + self.grants + .observe_source(&call.call().call, gate.source_expected); + if grant && !gate.unsafe_input { + self.grants.grant_call(&call.call().call); } ToolDecision::Execute(call) } @@ -228,6 +249,9 @@ impl ApprovalInbox { pub(super) fn permission_cards(&self) -> Vec { self.providers.cards() } + pub(super) fn tool_tokens(&self) -> Vec { + self.tools.iter().map(|entry| entry.token).collect() + } pub(super) fn tool_cards(&self) -> Vec { self.tools .iter() diff --git a/crates/agent-chat/src/thread/archive.rs b/crates/agent-chat/src/thread/archive.rs index a3160996..dbee69c6 100644 --- a/crates/agent-chat/src/thread/archive.rs +++ b/crates/agent-chat/src/thread/archive.rs @@ -172,15 +172,28 @@ impl Archive { self.draft_overlay = Some(DraftOverlay::new(text, updated)); true } + pub(super) fn draft_capacity(&self) -> usize { + self.draft_overlay + .as_ref() + .map_or(0, |overlay| overlay.text.len().saturating_mul(2)) + .saturating_add(self.summary().map_or(0, |summary| { + summary + .id + .len() + .saturating_add(summary.agent_id.len()) + .saturating_mul(2) + })) + .saturating_add(64 * 1024) + } pub(super) fn draft_patch(&self) -> Option { let summary = self.summary()?; self.draft_overlay.as_ref().map(|overlay| { - let mut chat = SavedChat::archive_draft_in_scope( + let mut chat = SavedChat::archive_draft_in_environment( summary.id.clone(), summary.agent_id.clone(), overlay.text.clone(), overlay.updated, - summary.scope, + summary.chat_scope(), ); chat.version = summary.transcript_version().wire(); chat @@ -191,7 +204,7 @@ impl Archive { load_id: LoadId, current: bool, result: Result, - permit: HistoryPermit, + permit: Option, ) -> LoadOutcome { let ArchivePhase::Archived { loading, .. } = &mut self.phase else { return LoadOutcome::Stale; @@ -202,7 +215,7 @@ impl Archive { *loading = None; match result { Ok(chat) => { - self.permit = Some(permit); + self.permit = permit; LoadOutcome::Hydrate { chat: Box::new(chat), overlay: self.draft_overlay.take(), diff --git a/crates/agent-chat/src/thread/archive/load_operation.rs b/crates/agent-chat/src/thread/archive/load_operation.rs index a7ae89f5..a2231558 100644 --- a/crates/agent-chat/src/thread/archive/load_operation.rs +++ b/crates/agent-chat/src/thread/archive/load_operation.rs @@ -1,10 +1,14 @@ -//! Hydration returns its original local generation and delivery permit together. +//! Original archive intent observes the shared admitted native-load service. use super::LoadId; +use crate::thread::AgentThread; +use gpui::Context; +use nocterm_agent_runtime::{LoadedHistory, history_admission::HistoryPayloadPermit}; use nocterm_ai::{ history::{HistoryPermit, SavedChat}, session::Ticket, storage::AgentStorageError, }; + pub(in crate::thread) struct LoadOperation { id: LoadId, ticket: Ticket, @@ -25,31 +29,42 @@ impl LoadOperation { reader, } } - pub(in crate::thread) async fn run(self) -> LoadCompletion { - let (result, permit) = self.reader.load(self.chat).await; - LoadCompletion { - id: self.id, - ticket: self.ticket, - result, - permit, - } + pub(in crate::thread) fn start( + self, + admission: nocterm_agent_runtime::history_admission::HistoryAdmissionHandle, + cx: &mut Context, + ) { + let loading = self.reader.load_admitted(self.chat, admission, cx); + cx.spawn(async move |thread, cx| { + let loaded = loading.await; + let completion = LoadCompletion { + id: self.id, + ticket: self.ticket, + loaded, + }; + let _ = thread.update(cx, |thread, cx| thread.on_archive_loaded(completion, cx)); + }) + .detach(); } } pub(in crate::thread) struct LoadCompletion { id: LoadId, ticket: Ticket, - result: Result, - permit: HistoryPermit, + loaded: LoadedHistory, } impl LoadCompletion { - pub(in crate::thread) fn into_parts( + pub(in crate::thread) fn publish( self, - ) -> ( - LoadId, - Ticket, - Result, - HistoryPermit, + publish: impl FnOnce( + LoadId, + Ticket, + Result, + Option, + Option, + ), ) { - (self.id, self.ticket, self.result, self.permit) + self.loaded.publish(|result, permit, payload| { + publish(self.id, self.ticket, result, permit, payload) + }); } } diff --git a/crates/agent-chat/src/thread/archive/tests.rs b/crates/agent-chat/src/thread/archive/tests.rs index e74b8c17..e14bbb21 100644 --- a/crates/agent-chat/src/thread/archive/tests.rs +++ b/crates/agent-chat/src/thread/archive/tests.rs @@ -10,10 +10,12 @@ fn saved() -> SavedChat { 1, ) } -fn permit(gate: &HistoryGate) -> HistoryPermit { - gate.acquire() - .now_or_never() - .expect("available history delivery") +fn permit(gate: &HistoryGate) -> Option { + Some( + gate.acquire() + .now_or_never() + .expect("available history delivery"), + ) } fn begin(archive: &mut Archive, intent: ArchiveIntent) -> LoadId { match archive.request(intent, SessionState::default().ticket()) { diff --git a/crates/agent-chat/src/thread/bound_call.rs b/crates/agent-chat/src/thread/bound_call.rs index 619ee283..d4d7a39d 100644 --- a/crates/agent-chat/src/thread/bound_call.rs +++ b/crates/agent-chat/src/thread/bound_call.rs @@ -4,10 +4,23 @@ use nocterm_workspace_ports::ExpectedSession; pub(super) struct BoundToolCall { call: BridgeCall, expected: Option, + /// The session a transfer reads from; other calls have none. + source: Option, } impl BoundToolCall { pub(super) fn new(call: BridgeCall, expected: Option) -> Self { - Self { call, expected } + Self { + call, + expected, + source: None, + } + } + pub(super) fn with_source(mut self, source: Option) -> Self { + self.source = source; + self + } + pub(super) fn source(&self) -> Option { + self.source } pub(super) fn call(&self) -> &BridgeCall { &self.call diff --git a/crates/agent-chat/src/thread/client.rs b/crates/agent-chat/src/thread/client.rs index 6c16cbbf..618388f2 100644 --- a/crates/agent-chat/src/thread/client.rs +++ b/crates/agent-chat/src/thread/client.rs @@ -4,6 +4,8 @@ use super::{AgentThread, approval_inbox::ApprovalToken, permissions::PermissionG use super::{ approval_inbox::{ToolDecision, ToolRejection}, bound_call::BoundToolCall, + execution::WaitSubject, + tool_runtime::TransferAdmission, }; use crate::runtime::SessionEvent; use gpui::{App, Context}; @@ -22,6 +24,9 @@ use std::path::PathBuf; use std::sync::Arc; impl AgentThread { + pub fn source_current(&self) -> bool { + self.deps.runtime().check_current().is_ok() + } pub(super) fn on_tool_completion( &mut self, call: BridgeCall, @@ -32,29 +37,24 @@ impl AgentThread { cx.notify(); } + /// Offers the agent the file tools while an attached terminal takes them. + pub(super) fn offer_tools(&mut self, cx: &gpui::App) { + let input = super::tool_runtime::resolution::Resolution::observe(self.deps.seat(), cx); + let registration = self.session.registration().as_ref(); + self.tools.offer_tools( + &input, + self.composer.default_attachments(), + registration, + cx, + ); + } + pub fn revoke_executions(&mut self, cx: &gpui::App) { - if self.tools.active_commands().next().is_none() && self.tools.live_empty() { + if !self.tools.has_active_executions() && self.tools.live_empty() { return; } let resolved = self.resolved(cx); - let attached = resolved - .iter() - .map(|(id, _, _)| id.clone()) - .collect::>(); - let current = resolved - .into_iter() - .map(|(id, entry, _)| { - let executor = entry.capabilities.state().info(cx).and_then(|info| { - entry - .capabilities - .execution()? - .executor(info.expected, cx) - .ok() - }); - (id, executor) - }) - .collect::>(); - self.tools.revoke_replaced_executors(¤t, &attached); + self.tools.revoke_replaced(resolved, cx); } pub(super) fn on_execution_watch(&mut self, cx: &gpui::App) -> bool { @@ -63,7 +63,7 @@ impl AgentThread { } else { self.revoke_executions(cx); } - if self.tools.active_commands().next().is_none() { + if !self.tools.has_active_executions() { self.tools.finish_watch(); false } else { @@ -75,6 +75,42 @@ impl AgentThread { self.tools.cancel_execution(); } + pub(super) fn start_transfer(&mut self, admission: TransferAdmission, cx: &mut Context) { + let source = self + .resolved(cx) + .into_iter() + .find(|(id, _, _)| admission.reads_from(id)) + .map(|(_, entry, _)| entry); + match self.tools.start_transfer(admission, source, cx) { + Ok(started) => { + let (call, id, yield_ms) = started.into_parts(); + self.start_execution_watch(cx); + self.wait_tool(WaitSubject::transfer(&id.to_string()), yield_ms, call, cx); + } + Err(rejected) => { + let (call, error) = rejected.into_parts(); + self.finish(call, Err(error)); + } + } + } + + pub(super) fn read_transfer(&mut self, call: BridgeCall, cx: &mut Context) { + let TerminalCall::ReadTransfer(request) = &call.call else { + return; + }; + let subject = WaitSubject::transfer(&request.transfer_id); + self.wait_tool(subject, request.yield_ms.unwrap_or(0), call, cx); + } + + pub(super) fn cancel_transfer(&mut self, call: BridgeCall, cx: &mut Context) { + let TerminalCall::CancelTransfer(request) = &call.call else { + return; + }; + let redact = self.deps.ai(cx).approval.redact_secrets; + let result = self.tools.cancel_transfer(&request.transfer_id, redact); + self.finish(call, result); + } + pub(super) fn command_payload( &self, id: &str, @@ -150,34 +186,13 @@ impl AgentThread { let gate = self.tool_gate(&call.call, cx); let expected = gate.expected(); let decision = self.tools.approval_receive_tool( - BoundToolCall::new(call, expected), + BoundToolCall::new(call, expected).with_source(gate.source_expected()), &gate, &self.deps.ai(cx).approval, ); self.apply_tool_decision(decision, cx); } - fn tool_destination( - &mut self, - request: &TerminalCall, - entry: Option<&TerminalEntry>, - cx: &App, - ) -> Option { - if let Some(entry) = entry { - let info = entry.capabilities.state().info(cx)?; - if info.local { - return Some("This computer".into()); - } - let target = info.target?; - return Some(format!("{} · {target}", entry.title)); - } - let server = request.server_id()?; - self.offline_servers(cx) - .into_iter() - .find(|(descriptor, _)| descriptor.server_id == server) - .map(|(descriptor, summary)| format!("{} · {}", descriptor.name, summary.target)) - } - fn normalize_tool_displays(&mut self) { let patches = self.tools.normalize_displays(self.state.entries()); self.state.patch_tool_rows(patches, self.deps.timestamp()); @@ -227,7 +242,14 @@ impl AgentThread { .into_iter() .find(|(id, _, _)| Some(id.as_str()) == call.call.terminal_id()) .map(|(_, entry, _)| entry); - let destination = self.tool_destination(&call.call, provided_entry.or(entry.as_ref()), cx); + let input = super::tool_runtime::resolution::Resolution::observe(self.deps.seat(), cx); + let (entry, defaults) = ( + provided_entry.or(entry.as_ref()), + self.composer.default_attachments(), + ); + let destination = self + .tools + .destination(&call.call, entry, &input, defaults, cx); let mut display = ToolDisplay::requested(tool.into(), arguments.clone()); display.destination = destination.map(|text| nocterm_ai::redact::redact(&text)); if self.deps.ai(cx).approval.redact_secrets { @@ -243,18 +265,27 @@ impl AgentThread { cx.notify(); } - pub fn apply_presented_update(&mut self, mut update: SessionUpdate) -> ThreadChange { + pub fn apply_presented_update( + &mut self, + mut update: SessionUpdate, + ) -> Result { self.tools .prepare_displays(self.session.turn(), self.state.entries().len()); tool_display::strip_provider_meta(&mut update); + let tools_changed = matches!( + &update, + SessionUpdate::ToolCall(_) | SessionUpdate::ToolCallUpdate(_) + ); let change = if self.config.report_control(&update) { ThreadChange::Controls } else { - self.state.apply_transcript(update, self.deps.timestamp()) + self.state.apply_transcript(update, self.deps.timestamp())? }; - let patches = self.tools.normalize_displays(self.state.entries()); - self.state.patch_tool_rows(patches, self.deps.timestamp()); - change + if tools_changed { + let patches = self.tools.normalize_displays(self.state.entries()); + self.state.patch_tool_rows(patches, self.deps.timestamp()); + } + Ok(change) } pub(super) fn on_prompt_completion( @@ -283,11 +314,6 @@ impl AgentThread { if response.usage.is_some() { self.state.tokens(response.usage); } - // Agents that keep limits in their own files have - // just written this turn's. - let agent = self.state.agent_id().to_owned(); - let runtime = self.deps.runtime().clone(); - cx.defer(move |cx| runtime.refresh_limits(&agent, cx)); self.notice.present(if self.session.stopped() { "Stopped".into() } else { @@ -336,6 +362,12 @@ impl AgentThread { self.config .keep_control(self.session.ticket(), notification); } + if let SessionUpdate::Turn(activity) = ¬ification.update { + if self.session().as_ref() == Some(¬ification.session) { + self.external_turn(activity, cx); + } + return; + } if self.session().as_ref() != Some(¬ification.session) || !self.session.accepts_updates() || matches!(notification.update, SessionUpdate::UserMessageChunk(_)) @@ -354,7 +386,14 @@ impl AgentThread { notification.update, SessionUpdate::ConfigOptionUpdate(_) | SessionUpdate::CurrentModeUpdate(_) ); - match self.apply_presented_update(notification.update.clone()) { + let change = match self.apply_presented_update(notification.update.clone()) { + Ok(change) => change, + Err(error) => { + self.fail(&error.to_string(), cx); + return; + } + }; + match change { ThreadChange::Transcript => { if let Some(index) = index { self.mark_dirty(index); @@ -489,7 +528,14 @@ impl AgentThread { self.session.retain_failed_restore(ticket, pending_restore); } match result { - Ok(response) => { + Ok(mut response) => { + let history = response.history.take().filter(|_| restored == Some(true)); + if let Some(history) = history + && let Err(error) = self.state.replace_history(history, self.deps.timestamp()) + { + self.fail(&error.to_string(), cx); + return true; + } let session = response.session; if !self.session.accept_open( ticket, @@ -544,6 +590,8 @@ impl AgentThread { self.fail("Terminal bridge is unavailable.", cx); return; }; + // An agent that keeps its sessions reopens one even when nothing of it was saved here. + let keeps_history = info.capabilities.keeps_history; if !self .session .begin_open(commands.clone(), info, workdir.clone()) @@ -557,7 +605,9 @@ impl AgentThread { .session .restore() .clone() - .filter(|restore| !self.state.entries().is_empty() && restore.workdir.is_absolute()) + .filter(|restore| { + keeps_history || (!self.state.entries().is_empty() && restore.workdir.is_absolute()) + }) .map(|mut restore| { restore.workdir = workdir.clone(); restore @@ -664,7 +714,10 @@ impl AgentThread { } pub(super) fn runtime_event(&mut self, event: SessionEvent, cx: &mut Context) { match event { - SessionEvent::Leased(lease) => self.begin_lease(lease), + SessionEvent::Leased(lease) => { + self.begin_lease(lease); + self.offer_tools(cx); + } SessionEvent::Connected { connection, commands, diff --git a/crates/agent-chat/src/thread/client_port.rs b/crates/agent-chat/src/thread/client_port.rs index 199f2406..3560f49f 100644 --- a/crates/agent-chat/src/thread/client_port.rs +++ b/crates/agent-chat/src/thread/client_port.rs @@ -14,6 +14,9 @@ impl SessionClient for ThreadClient { fn id(&self) -> EntityId { self.0.entity_id() } + fn chat(&self) -> Option { + Some(self.0.clone().into()) + } fn alive(&self) -> bool { self.0.upgrade().is_some() } @@ -53,17 +56,38 @@ impl SessionClient for ThreadClient { thread.session.shown(), )) } + fn history_payload(&self, cx: &App) -> Option { + let thread = self.0.upgrade()?; + let thread = thread.read(cx); + (!thread.is_archived() && thread.history_eviction_gate().is_permitted()) + .then(|| thread.payloads.order()) + .flatten() + } fn evict_history(&self, cx: &mut App) -> bool { self.0 .upgrade() .is_some_and(|thread| thread.update(cx, |thread, cx| thread.evict_history(cx))) } - fn snapshot(&self, cx: &App) -> Option> { - self.0.upgrade()?.read(cx).flush_snapshot(cx) + fn snapshot( + &self, + cx: &mut App, + ) -> Result>, nocterm_ai::storage::AgentStorageError> + { + let Some(thread) = self.0.upgrade() else { + return Ok(None); + }; + thread.update(cx, |thread, cx| thread.flush_snapshot(cx)) } - fn capture(&self, cx: &mut App) -> Option<(Arc, u64)> { - self.0 - .upgrade()? - .update(cx, |thread, cx| thread.capture_pending(cx)) + fn capture( + &self, + cx: &mut App, + ) -> Result< + Option<(Arc, u64)>, + nocterm_ai::storage::AgentStorageError, + > { + let Some(thread) = self.0.upgrade() else { + return Ok(None); + }; + thread.update(cx, |thread, cx| thread.capture_pending(cx)) } } diff --git a/crates/agent-chat/src/thread/composer.rs b/crates/agent-chat/src/thread/composer.rs index 8e1e037f..1119876a 100644 --- a/crates/agent-chat/src/thread/composer.rs +++ b/crates/agent-chat/src/thread/composer.rs @@ -1,14 +1,16 @@ //! What the user is composing, apart from the conversation it goes into: the -//! unsent draft, the context and images for the next prompt, and the queue of -//! prompts waiting their turn. +//! unsent draft, the context, images and files for the next prompt, and the queue +//! of prompts waiting their turn. pub(in crate::thread) mod prompt_preparation; use super::{Attachment, queue::QueuedPrompt}; use nocterm_ai::images::PromptImage; +use nocterm_ai::prompt_files::{MAX_PROMPT_FILES, PromptFile, PromptFileError}; pub struct Composer { draft: Option>, attachments: Vec, images: Vec, + files: Vec, queue: Vec, queue_paused: bool, /// Composer edits suspend dispatch independently of stops and errors. @@ -21,6 +23,7 @@ pub struct Composer { struct Defaults { attachments: Vec, images: Vec, + files: Vec, } impl Default for Composer { @@ -29,6 +32,7 @@ impl Default for Composer { draft: None, attachments: Vec::new(), images: Vec::new(), + files: Vec::new(), queue: Vec::new(), queue_paused: false, queue_editing: false, @@ -54,10 +58,11 @@ impl Composer { replace: Option, attachments: Vec, ) -> Result { + // Files are small links: they are measured with the prompt itself. let mut saved = nocterm_ai::history::SavedPrompt { id: replace.unwrap_or(self.next_queue_id()), text, - images: Vec::new(), + images: self.files.iter().map(PromptFile::content).collect(), attachments, }; let encoded_len = nocterm_ai::history::prompt_size(&saved)? @@ -66,8 +71,10 @@ impl Composer { .iter() .map(|image| image.encoded_len()) .sum::() - + self.images().len().saturating_sub(1); + + self.images().len(); + let files = std::mem::take(&mut saved.images); saved.images = self.images().iter().map(|image| image.content()).collect(); + saved.images.extend(files); let prompt = QueuedPrompt::prepared(saved, self.attachments().to_vec(), Some(encoded_len)); let candidate = self.prepare_prompt_change(prompt, replace)?; let attachment_count = candidate @@ -94,6 +101,9 @@ impl Composer { pub fn images(&self) -> &[PromptImage] { &self.images } + pub fn files(&self) -> &[PromptFile] { + &self.files + } pub fn queue(&self) -> &[QueuedPrompt] { &self.queue } @@ -137,6 +147,7 @@ impl Composer { pub(super) fn reset_after_disable(&mut self) { self.clear_context(); self.images.clear(); + self.files.clear(); } pub(super) fn remove_image(&mut self, index: usize) -> bool { if index >= self.images.len() { @@ -145,6 +156,27 @@ impl Composer { self.images.remove(index); true } + /// Adds `files` the prompt does not hold yet. + pub(super) fn add_files(&mut self, files: Vec) -> Result<(), PromptFileError> { + let mut next = self.files.clone(); + for file in files { + if !next.contains(&file) { + next.push(file); + } + } + if next.len() > MAX_PROMPT_FILES { + return Err(PromptFileError::TooMany); + } + self.files = next; + Ok(()) + } + pub(super) fn remove_file(&mut self, index: usize) -> bool { + if index >= self.files.len() { + return false; + } + self.files.remove(index); + true + } pub(super) fn replace_images( &mut self, images: Vec, @@ -206,6 +238,7 @@ impl Composer { draft: self.draft.clone(), attachments: self.default_attachments().to_vec(), images: self.images.clone(), + files: self.files.clone(), queue: self.queue.clone(), queue_paused: true, next_queue_id: self.next_queue_id, @@ -241,6 +274,7 @@ impl Composer { self.next_queue_id += 1; } self.images.clear(); + self.files.clear(); } /// Queued prompts wait to be sent and nothing holds them back. @@ -282,12 +316,18 @@ impl Composer { self.defaults.is_some() } - /// Shows a queued prompt's images and context for editing, keeping the + /// Shows a queued prompt's images, files and context for editing, keeping the /// composer's own to return to. - pub fn begin_edit(&mut self, images: Vec, attachments: Vec) { + pub fn begin_edit( + &mut self, + images: Vec, + files: Vec, + attachments: Vec, + ) { debug_assert!(self.defaults.is_none()); self.defaults = Some(Defaults { images: std::mem::replace(&mut self.images, images), + files: std::mem::replace(&mut self.files, files), attachments: std::mem::replace(&mut self.attachments, attachments), }); self.queue_editing = true; @@ -297,6 +337,7 @@ impl Composer { pub fn end_edit(&mut self) { if let Some(defaults) = self.defaults.take() { self.images = defaults.images; + self.files = defaults.files; self.attachments = defaults.attachments; } self.queue_editing = false; @@ -356,7 +397,11 @@ mod tests { vec![prompt(1), prompt(2)], ); composer.resume(); - composer.begin_edit(Vec::new(), vec![Attachment::Group("queued".into())]); + composer.begin_edit( + Vec::new(), + Vec::new(), + vec![Attachment::Group("queued".into())], + ); assert!(composer.editing()); assert_eq!( composer.default_attachments(), @@ -414,6 +459,7 @@ mod tests { composer.replace_context(vec![Attachment::Group("own".into())]); composer.begin_edit( vec![image.clone()], + Vec::new(), vec![Attachment::Group("queued".into())], ); assert!( @@ -441,4 +487,20 @@ mod tests { .collect::>(); assert_eq!(ids, [1, 3]); } + + #[test] + fn files_go_with_the_prompt_after_its_images_and_leave_the_composer() { + let mut composer = Composer::default(); + let file = PromptFile::link(std::path::Path::new("/srv/report.pdf"), 7).unwrap(); + composer + .add_files(vec![file.clone(), file.clone()]) + .unwrap(); + assert_eq!(composer.files().len(), 1, "a file is attached once"); + let candidate = composer + .prepare_submission("read it".into(), None, Vec::new()) + .unwrap(); + assert_eq!(candidate.queue()[0].saved().images, vec![file.content()]); + composer.commit_prompt_change(candidate); + assert!(composer.files().is_empty()); + } } diff --git a/crates/agent-chat/src/thread/config.rs b/crates/agent-chat/src/thread/config.rs index c954f189..3d9ac85d 100644 --- a/crates/agent-chat/src/thread/config.rs +++ b/crates/agent-chat/src/thread/config.rs @@ -95,10 +95,13 @@ impl ChatConfig { pub(super) fn mode_choice(&self) -> Option<&ModeId> { self.mode_choice.as_ref() } - pub fn model(&self) -> Option { + pub(super) fn model_ref(&self) -> Option<&String> { self.last_prompt_model - .clone() - .or_else(|| self.last_saved_model.clone()) + .as_ref() + .or(self.last_saved_model.as_ref()) + } + pub fn model(&self) -> Option { + self.model_ref().cloned() } pub fn config_overrides_modes(&self) -> bool { self.options diff --git a/crates/agent-chat/src/thread/document.rs b/crates/agent-chat/src/thread/document.rs index 239e00ff..b0e02985 100644 --- a/crates/agent-chat/src/thread/document.rs +++ b/crates/agent-chat/src/thread/document.rs @@ -49,7 +49,7 @@ impl ChatDocument { identity: Identity::new( agent, key, - nocterm_ai::history::TranscriptVersion::SCOPED.wire(), + nocterm_ai::history::TranscriptVersion::ENVIRONMENT.wire(), ), metadata: Metadata::new(updated), transcript: Transcript::default(), @@ -123,7 +123,7 @@ impl ChatDocument { pub(super) fn imported_clean(&mut self, chat: &mut SavedChat, pending: bool) { self.identity = Identity::new( chat.header.agent_id.clone(), - ChatKey::new(chat.header.scope, chat.header.id.clone()), + ChatKey::from_header(&chat.header), chat.version, ); self.metadata = Metadata::restored( @@ -158,7 +158,7 @@ impl ChatDocument { pub(super) fn archived_clean(&mut self, summary: &nocterm_ai::history::ChatSummary) { self.identity = Identity::new( summary.agent_id.clone(), - ChatKey::new(summary.scope, summary.id.clone()), + ChatKey::from_summary(summary), summary.transcript_version().wire(), ); self.metadata = @@ -210,18 +210,58 @@ impl ChatDocument { pub(super) fn edited_archived_draft(&mut self, now: u64) { self.changed(now, true); } - pub(super) fn apply_transcript(&mut self, update: SessionUpdate, now: u64) -> ThreadChange { - let (change, semantic) = self.transcript.apply(update, now); + pub(super) fn apply_transcript( + &mut self, + update: SessionUpdate, + now: u64, + ) -> Result { + let (change, semantic) = self.transcript.apply(update, now)?; if semantic { self.changed(now, false); } - change + Ok(change) + } + /// Adds a prompt another device sent as user rows, marked for redraw. + pub(super) fn append_prompt( + &mut self, + prompt: &[Content], + now: u64, + ) -> Result<(), HistoryCodecError> { + let first = self.entries().len(); + for part in prompt.iter().cloned() { + self.apply_transcript(SessionUpdate::UserMessageChunk(part), now)?; + } + for row in first..self.entries().len() { + self.mark_row(row); + } + Ok(()) } - pub(super) fn append_user(&mut self, content: Vec, now: u64, input: &Inputs<'_>) { - self.transcript.append_user(content, now); + /// Shows the agent's own record of the conversation in place of the saved transcript. + pub(super) fn replace_history( + &mut self, + updates: Vec, + now: u64, + ) -> Result<(), HistoryCodecError> { + let next = self.transcript.replaced(updates, now)?; + let rows = self.entries().len().max(next.state().entries.len()); + self.transcript = next; + for row in 0..rows { + self.mark_row(row); + } + self.changed(now, false); + Ok(()) + } + pub(super) fn append_user( + &mut self, + content: Vec, + now: u64, + input: &Inputs<'_>, + ) -> Result<(), HistoryCodecError> { + self.transcript.append_user(content, now)?; self.persistence.remember_inputs(input); self.mark_row(self.entries().len() - 1); self.changed(now, false); + Ok(()) } pub(super) fn tokens(&mut self, usage: Option) { self.transcript.tokens(usage); @@ -254,6 +294,9 @@ impl ChatDocument { pub fn take_changed_rows(&mut self) -> HashSet { std::mem::take(&mut self.changed_rows) } + pub fn has_changed_rows(&self) -> bool { + !self.changed_rows.is_empty() + } pub(super) fn mark_row(&mut self, row: usize) { self.changed_rows.insert(row); self.budget.mark(row); @@ -262,7 +305,9 @@ impl ChatDocument { self.budget.refresh(&self.transcript.state().entries) } pub fn resident_bytes(&self) -> usize { - self.budget.resident_bytes() + self.transcript + .resident_bytes() + .max(self.budget.resident_bytes()) } pub(super) fn validate_candidate( &mut self, @@ -274,11 +319,11 @@ impl ChatDocument { self.budget.validate(metadata, encoded, memory) } pub(super) fn metadata(&self, input: &Inputs<'_>) -> SavedChat { - let mut chat = SavedChat::for_scope( + let mut chat = SavedChat::for_environment( self.agent_id().into(), self.chat_id().into(), self.updated(), - self.key().scope(), + self.key().chat_scope(), ); chat.version = self.identity.wire_version(); chat.header.name = self.name().map(str::to_owned); @@ -289,6 +334,57 @@ impl ChatDocument { input.metadata(&mut chat); chat } + pub(super) fn fork_capacity( + &self, + input: &Inputs<'_>, + len: usize, + ) -> Result { + let end = len.min(self.entries().len()); + if end > nocterm_ai::history::MAX_SAVED_ENTRIES { + return Err(HistoryCodecError::CollectionLimit); + } + Ok(self.snapshot_capacity(input)?.saturating_add( + nocterm_ai::history::retained_value_bytes(&&self.entries()[..end])?, + )) + } + pub(super) fn restart_capacity(&self, input: &Inputs<'_>) -> Result { + let state = self.transcript(); + let plan = state.plan.as_ref().map_or(0, |plan| { + plan.entries.iter().fold(0usize, |total, entry| { + total + .saturating_add(512) + .saturating_add(entry.content.len().saturating_mul(2)) + }) + }); + let meta = state + .usage + .as_ref() + .and_then(|usage| usage.meta.as_ref()) + .map(nocterm_ai::history::retained_value_bytes) + .transpose()? + .unwrap_or(0); + Ok(self + .snapshot_capacity(input)? + .saturating_add(nocterm_ai::history::retained_value_bytes(&state.entries)?) + .saturating_add(plan) + .saturating_add(meta)) + } + pub(super) fn snapshot_capacity(&self, input: &Inputs<'_>) -> Result { + use nocterm_ai::history::retained_value_bytes; + // The transcript-free snapshot uses the same counter as the retained + // snapshot, so header, scope, environment, inputs, queue and times are + // exact. Rows are counted in place, before any transcript is cloned; the + // fixed 64KiB covers the catalog preview drawn from them. + let start = SavedChat::saved_entry_start(self.entries().len()); + let frame = SharedChat { + metadata: self.metadata(input), + queue: input.queue(), + } + .retained_bytes()?; + Ok((64usize * 1024) + .saturating_add(frame) + .saturating_add(retained_value_bytes(&&self.entries()[start..])?)) + } pub(super) fn snapshot(&self, input: &Inputs<'_>) -> Option> { if !self.pinned() && self.name().is_none() @@ -313,6 +409,7 @@ impl ChatDocument { pub(super) fn existing_offer(&self) -> Option<(Arc, u64)> { self.persistence.capture_pending() } + #[cfg(test)] pub(super) fn propose_candidate( &mut self, input: &Inputs<'_>, @@ -323,14 +420,24 @@ impl ChatDocument { if self.persistence.has_current_candidate() { return self.persistence.capture_pending(); } - let snapshot = self.snapshot(input).or_else(|| { + let snapshot = self.candidate_snapshot(input)?; + self.adopt_candidate(input, snapshot) + } + pub(super) fn candidate_snapshot(&self, input: &Inputs<'_>) -> Option> { + self.snapshot(input).or_else(|| { self.history_present().then(|| { Arc::new(SharedChat { metadata: self.metadata(input), queue: Vec::new(), }) }) - })?; + }) + } + pub(super) fn adopt_candidate( + &mut self, + input: &Inputs<'_>, + snapshot: Arc, + ) -> Option<(Arc, u64)> { self.persistence.remember_inputs(input); self.persistence.prepare(snapshot); self.persistence.capture_pending() diff --git a/crates/agent-chat/src/thread/document/tests.rs b/crates/agent-chat/src/thread/document/tests.rs index cb87450b..11ff966b 100644 --- a/crates/agent-chat/src/thread/document/tests.rs +++ b/crates/agent-chat/src/thread/document/tests.rs @@ -91,20 +91,30 @@ fn empty_matching_chunks_identical_tool_patches_and_view_updates_are_clean() { .status(ToolStatus::Pending) .raw_output(json!({"out":"same"})); let mut document = imported(vec![Entry::Tool(call), Entry::Agent("answer".into())]); - document.apply_transcript( - update(json!({"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":""}})), - 80, - ); - document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"call","status":"pending","title":"Tool","rawOutput":{"out":"same"}})),80); - document.apply_transcript(update(json!({"sessionUpdate":"plan","entries":[]})), 80); - document.apply_transcript( - update(json!({"sessionUpdate":"usage_update","used":1,"size":10})), - 80, - ); - document.apply_transcript( - update(json!({"sessionUpdate":"session_info_update","title":"Recorded title"})), - 80, - ); + document + .apply_transcript( + update( + json!({"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":""}}), + ), + 80, + ) + .unwrap(); + document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"call","status":"pending","title":"Tool","rawOutput":{"out":"same"}})),80).unwrap(); + document + .apply_transcript(update(json!({"sessionUpdate":"plan","entries":[]})), 80) + .unwrap(); + document + .apply_transcript( + update(json!({"sessionUpdate":"usage_update","used":1,"size":10})), + 80, + ) + .unwrap(); + document + .apply_transcript( + update(json!({"sessionUpdate":"session_info_update","title":"Recorded title"})), + 80, + ) + .unwrap(); document.tokens(None); assert_eq!(document.desired_revision(), 0); assert_eq!(document.updated(), 40); @@ -115,12 +125,16 @@ fn a_new_empty_row_is_durable_but_a_patch_outside_the_saved_window_is_not() { let mut entries = vec![Entry::Tool(ToolRecord::new("old", "Old"))]; entries.extend((0..2000).map(|_| Entry::Agent("saved".into()))); let mut document = imported(entries); - document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"old","title":"Changed old row"})),80); + document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"old","title":"Changed old row"})),80).unwrap(); assert_eq!(document.desired_revision(), 0); - document.apply_transcript( - update(json!({"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":""}})), - 81, - ); + document + .apply_transcript( + update( + json!({"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":""}}), + ), + 81, + ) + .unwrap(); assert_eq!(document.desired_revision(), 1); assert_eq!(document.updated(), 81); } @@ -129,10 +143,14 @@ fn a_new_empty_row_is_durable_but_a_patch_outside_the_saved_window_is_not() { fn genuine_saved_row_transitions_remain_dirty_even_when_the_text_returns_to_its_old_value() { let mut document = imported(vec![Entry::Tool(ToolRecord::new("call", "A"))]); for (title, time) in [("B", 81), ("A", 82)] { - document.apply_transcript( - update(json!({"sessionUpdate":"tool_call_update","toolCallId":"call","title":title})), - time, - ); + document + .apply_transcript( + update( + json!({"sessionUpdate":"tool_call_update","toolCallId":"call","title":title}), + ), + time, + ) + .unwrap(); } assert_eq!(document.desired_revision(), 2); assert_eq!(document.updated(), 82); @@ -328,3 +346,56 @@ fn disabled_release_retains_unadmitted_or_failed_input_and_an_old_ack_cannot_rel "failure retains the original input after releasing presentation" ); } + +#[test] +fn snapshot_capacity_covers_a_long_transcript_with_entry_times() { + let composer = Composer::default(); + let config = ChatConfig::default(); + let inputs = input(&composer, &config); + let mut saved = SavedChat::for_identity("agent".into(), ID.into(), 40); + saved.entries = (0..400).map(|_| Entry::Agent("a".into())).collect(); + saved.times = (0..400).collect(); + let mut document = ChatDocument::new( + "agent".into(), + nocterm_ai::execution_origin::ChatKey::new( + nocterm_ai::history::ContextId::General, + ID.into(), + ), + 40, + ); + document.imported_clean(&mut saved, false); + let snapshot = document.snapshot(&inputs).expect("non-empty chat"); + assert!(document.snapshot_capacity(&inputs).unwrap() >= snapshot.retained_bytes().unwrap()); +} + +#[test] +fn snapshot_capacity_covers_a_hub_account_header_and_draft() { + use nocterm_ai::local_environment::{ChatScope, LocalEnvironment}; + use nocterm_hubs_model::source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, NamespaceId, + }; + let fingerprint = Fingerprint::from_bytes([7; 32]); + let partition = LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new(NamespaceId::new("hub").unwrap(), fingerprint), + AccountIdentity::new(NamespaceId::new("account").unwrap(), fingerprint), + )); + let scope = ChatScope::new( + LocalEnvironment::HubAccount(partition), + nocterm_ai::history::ContextId::General, + ); + let mut saved = SavedChat::for_environment("agent".into(), ID.into(), 40, scope.clone()); + saved.entries = vec![Entry::Agent("a".into())]; + saved.times = vec![1]; + let mut document = ChatDocument::new( + "agent".into(), + nocterm_ai::execution_origin::ChatKey::in_environment(scope, ID.into()), + 40, + ); + document.imported_clean(&mut saved, false); + let mut composer = Composer::default(); + composer.set_draft("draft".into()); + let config = ChatConfig::default(); + let inputs = input(&composer, &config); + let snapshot = document.snapshot(&inputs).expect("non-empty chat"); + assert!(document.snapshot_capacity(&inputs).unwrap() >= snapshot.retained_bytes().unwrap()); +} diff --git a/crates/agent-chat/src/thread/document/transcript.rs b/crates/agent-chat/src/thread/document/transcript.rs index 9a84d72d..386367d0 100644 --- a/crates/agent-chat/src/thread/document/transcript.rs +++ b/crates/agent-chat/src/thread/document/transcript.rs @@ -6,9 +6,15 @@ use nocterm_ai::model::tools::{ToolChanges, ToolRecord, ToolStatus}; use nocterm_ai::model::usage::TokenTotals; use nocterm_ai::thread::{Entry, ThreadChange, ThreadState}; -#[derive(Clone, Default)] +#[derive(Clone)] pub(super) struct Transcript { state: ThreadState, + live_bytes: usize, +} +impl Default for Transcript { + fn default() -> Self { + Self::restored(Vec::new(), Vec::new(), None) + } } pub(crate) struct ToolMetadataPatch { row: usize, @@ -24,21 +30,28 @@ impl ToolMetadataPatch { } impl Transcript { pub(super) fn restored(entries: Vec, times: Vec, title: Option) -> Self { - Self { - state: ThreadState { - entries, - times, - title, - ..Default::default() - }, - } + let state = ThreadState { + entries, + times, + title, + ..Default::default() + }; + let live_bytes = nocterm_ai::thread::live::resident_bytes(&state).unwrap_or(usize::MAX); + Self { state, live_bytes } } pub(super) fn state(&self) -> &ThreadState { &self.state } - pub(super) fn apply(&mut self, update: SessionUpdate, now: u64) -> (ThreadChange, bool) { + pub(super) fn apply( + &mut self, + update: SessionUpdate, + now: u64, + ) -> Result<(ThreadChange, bool), nocterm_ai::history::HistoryCodecError> { + let next = nocterm_ai::thread::live::admit_update(&self.state, self.live_bytes, &update)?; let semantic = self.changes_saved(&update); - (self.state.apply(update, now), semantic) + let change = self.state.apply(update, now); + self.live_bytes = next; + Ok((change, semantic)) } fn changes_saved(&self, update: &SessionUpdate) -> bool { match update { @@ -69,8 +82,30 @@ impl Transcript { _ => false, } } - pub(super) fn append_user(&mut self, content: Vec, now: u64) { + /// A transcript of the same chat made of `updates` alone, oldest first. + pub(super) fn replaced( + &self, + updates: Vec, + now: u64, + ) -> Result { + let mut next = Self::restored(Vec::new(), Vec::new(), self.state.title.clone()); + for update in updates { + next.apply(update, now)?; + } + Ok(next) + } + pub(super) fn append_user( + &mut self, + content: Vec, + now: u64, + ) -> Result<(), nocterm_ai::history::HistoryCodecError> { + self.live_bytes = + nocterm_ai::thread::live::admit_user(&self.state, self.live_bytes, &content)?; self.state.push_user(content, now); + Ok(()) + } + pub(super) fn resident_bytes(&self) -> usize { + self.live_bytes } pub(super) fn tokens(&mut self, usage: Option) { self.state.tokens = usage; @@ -83,6 +118,23 @@ impl Transcript { if call.meta.as_ref().and_then(|meta| meta.get(key)) == Some(&patch.value) { return false; } + let new_key = call.meta.as_ref().and_then(|meta| meta.get(key)).is_none(); + let removed = call + .meta + .as_ref() + .and_then(|meta| meta.get(key)) + .map_or(Ok(0), nocterm_ai::history::retained_value_bytes) + .unwrap_or(0); + let added = nocterm_ai::history::retained_value_bytes(&patch.value).unwrap_or(usize::MAX); + let next = self + .live_bytes + .saturating_sub(removed) + .saturating_add(added) + .saturating_add(if new_key { 2048 } else { 0 }); + if next > nocterm_ai::history::MAX_RESIDENT_BYTES { + return false; + } + self.live_bytes = next; call.meta .get_or_insert_with(Default::default) .insert(key.into(), patch.value); diff --git a/crates/agent-chat/src/thread/execution.rs b/crates/agent-chat/src/thread/execution.rs index 9e9b61f1..893b6d0a 100644 --- a/crates/agent-chat/src/thread/execution.rs +++ b/crates/agent-chat/src/thread/execution.rs @@ -2,28 +2,40 @@ use super::AgentThread; use nocterm_ai::{BridgeCall, ToolFailure}; use std::time::{Duration, Instant}; -pub(in crate::thread) mod jobs; +/// What a wait reports on; both name only work this chat started. +pub(super) enum WaitSubject { + Command { terminal: String, id: String }, + Transfer { id: String }, +} +impl WaitSubject { + pub(super) fn command(terminal: &str, id: &str) -> Self { + Self::Command { + terminal: terminal.into(), + id: id.into(), + } + } + pub(super) fn transfer(id: &str) -> Self { + Self::Transfer { id: id.into() } + } +} /// Read waits retain the original ticket and never take a replacement executor. -pub(super) struct CommandWaitOperation { - terminal: String, - id: String, +pub(super) struct ToolWaitOperation { + subject: WaitSubject, ticket: nocterm_ai::session::Ticket, yield_time: Duration, until: Instant, _guard: super::session_driver::OperationGuard, } -impl CommandWaitOperation { +impl ToolWaitOperation { pub(super) fn new( - terminal: String, - id: String, + subject: WaitSubject, yield_ms: u64, ticket: nocterm_ai::session::Ticket, guard: super::session_driver::OperationGuard, ) -> Self { Self { - terminal, - id, + subject, ticket, yield_time: Duration::from_millis(yield_ms), until: Instant::now(), @@ -33,23 +45,25 @@ impl CommandWaitOperation { pub(super) fn ticket(&self) -> nocterm_ai::session::Ticket { self.ticket } - pub(super) fn id(&self) -> &str { - &self.id - } - pub(super) fn terminal(&self) -> &str { - &self.terminal - } + /// The current report, or `None` while the work runs and the wait has time left. pub(super) fn frame( &self, - state: super::tool_runtime::CommandSnapshot, + tools: &mut super::tool_runtime::ToolRuntime, redact: bool, ) -> Result, ToolFailure> { - if state.active() && Instant::now() < self.until { - return Ok(None); + use super::tool_runtime::presentation::{command_payload, transfer_payload}; + let waiting = Instant::now() < self.until; + match &self.subject { + WaitSubject::Command { terminal, id } => { + let state = tools.command_snapshot(id, terminal)?; + Ok((!(state.active() && waiting)).then(|| command_payload(id, state, redact))) + } + WaitSubject::Transfer { id } => { + let progress = tools.transfer_progress(id)?; + let running = !progress.state.finished(); + Ok((!(running && waiting)).then(|| transfer_payload(&progress, redact))) + } } - Ok(Some(super::tool_runtime::presentation::command_payload( - &self.id, state, redact, - ))) } pub(super) async fn run( mut self, @@ -59,7 +73,7 @@ impl CommandWaitOperation { ) { self.until = Instant::now() + self.yield_time; loop { - match thread.update(cx, |thread, cx| thread.poll_command_wait(&self, cx)) { + match thread.update(cx, |thread, cx| thread.poll_tool_wait(&self, cx)) { Ok(Ok(None)) => { cx.background_executor() .timer(Duration::from_millis(25)) diff --git a/crates/agent-chat/src/thread/execution/jobs/tests.rs b/crates/agent-chat/src/thread/execution/jobs/tests.rs deleted file mode 100644 index f29600f5..00000000 --- a/crates/agent-chat/src/thread/execution/jobs/tests.rs +++ /dev/null @@ -1,217 +0,0 @@ -use super::*; -use nocterm_session::{ExecError, ExecFuture, ExecOutput}; -use std::sync::atomic::{AtomicUsize, Ordering}; -struct Executor { - calls: AtomicUsize, - output: Mutex>, -} -impl HostExec for Executor { - fn exec(&self, _: ExecRequest) -> ExecFuture { - self.calls.fetch_add(1, Ordering::SeqCst); - let output = self.output.lock().unwrap().take(); - async move { output.ok_or(ExecError::Unsupported) }.boxed() - } -} -fn executor(output: Option) -> Arc { - Arc::new(Executor { - calls: AtomicUsize::new(0), - output: Mutex::new(output), - }) -} -#[test] -fn rejects_a_fifth_active_command_and_foreign_command_ids() { - let mut registry = Jobs::default(); - let mut first = String::new(); - for index in 0..MAX_ACTIVE { - let (id, _, _) = registry.insert("t1".into(), executor(None)).unwrap(); - if index == 0 { - first = id; - } - } - assert_eq!( - registry.insert("t1".into(), executor(None)).err(), - Some(JobError::Busy) - ); - for (id, terminal) in [("c99", "t1"), (first.as_str(), "t2"), ("x", "t1")] { - assert_eq!(registry.get(id, terminal).err(), Some(JobError::Unknown)); - } - assert_eq!( - nocterm_ai::ToolFailure::from(JobError::Busy).message(), - "This chat already has four active commands. Read or cancel one first." - ); -} -#[test] -fn cancelled_or_expired_before_first_poll_never_starts_a_program() { - futures::executor::block_on(async { - for cancelled in [true, false] { - let executor = executor(None); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - if cancelled { - registry.get(&id, "t1").unwrap().cancel(State::Cancelled); - } - run( - executor.clone(), - ExecRequest::new("do-not-run"), - state, - cancel, - futures::future::ready(()), - ) - .await; - assert_eq!(executor.calls.load(Ordering::SeqCst), 0); - assert_eq!( - registry.get(&id, "t1").unwrap().snapshot().state, - if cancelled { - State::Cancelled - } else { - State::TimedOut - } - ); - } - }); -} -#[test] -fn output_is_drained_past_retention_and_preserves_actual_exit() { - futures::executor::block_on(async { - let (sink, output) = ExecOutput::channel(); - let executor = executor(Some(output)); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - let produce = async move { - for _ in 0..40 { - assert!(sink.send(vec![b'x'; 4096]).await); - } - sink.finish(ExecExit { - status: Some(7), - stderr: "failure".into(), - }); - }; - futures::join!( - run( - executor, - ExecRequest::new("test"), - state, - cancel, - futures::future::pending() - ), - produce - ); - let state = registry.get(&id, "t1").unwrap().snapshot(); - assert_eq!(state.state, State::Exited); - assert_eq!(state.stdout.len(), MAX_STDOUT); - assert_eq!(state.total_bytes, 40 * 4096); - assert_eq!(state.exit.unwrap().status, Some(7)); - }); -} -#[test] -fn registry_limits_active_work_retains_finished_jobs_and_checks_ownership() { - let executor = executor(None); - let mut registry = Jobs::default(); - let first = registry.insert("t1".into(), executor.clone()).unwrap().0; - for _ in 1..MAX_ACTIVE { - registry.insert("t1".into(), executor.clone()).unwrap(); - } - assert!(registry.insert("t1".into(), executor.clone()).is_err()); - assert!(registry.get(&first, "t2").is_err()); - assert!(Jobs::default().get(&first, "t1").is_err()); - registry.cancel_all(); - for _ in 0..MAX_RETAINED { - let (id, _, _) = registry.insert("t1".into(), executor.clone()).unwrap(); - registry.get(&id, "t1").unwrap().cancel(State::Cancelled); - } - assert_eq!(registry.jobs.len(), MAX_RETAINED); - assert!(registry.get(&first, "t1").is_err()); -} -#[test] -fn silence_finishes_and_cancellation_drops_transport_output() { - futures::executor::block_on(async { - let (sink, output) = ExecOutput::channel(); - sink.finish(ExecExit { - status: Some(0), - stderr: String::new(), - }); - drop(sink); - let executor = executor(Some(output)); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - run( - executor, - ExecRequest::new("true"), - state, - cancel, - futures::future::pending(), - ) - .await; - assert_eq!( - registry - .get(&id, "t1") - .unwrap() - .snapshot() - .exit - .unwrap() - .status, - Some(0) - ); - }); -} - -#[test] -fn cancelling_running_program_drops_output_and_keeps_exit_unknown() { - futures::executor::block_on(async { - let (sink, output) = ExecOutput::channel(); - let executor = executor(Some(output)); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - let stop = async { - assert!(sink.send(b"running".to_vec()).await); - registry.get(&id, "t1").unwrap().cancel(State::Cancelled); - sink.closed().await; - }; - futures::join!( - run( - executor.clone(), - ExecRequest::new("test"), - state, - cancel, - futures::future::pending() - ), - stop - ); - assert_eq!(executor.calls.load(Ordering::SeqCst), 1); - let state = registry.get(&id, "t1").unwrap().snapshot(); - assert_eq!(state.state, State::Cancelled); - assert!(state.exit.is_none()); - }); -} - -#[test] -fn cancelling_an_owned_start_before_poll_never_requests_native_execution() { - futures::executor::block_on(async { - let executor = Arc::new(nocterm_session::testing::FakeExec::default()); - let mut registry = Jobs::default(); - let start = registry - .start( - "original terminal".into(), - executor.clone(), - ExecRequest::new("do-not-run"), - futures::future::pending(), - ) - .unwrap(); - let (id, owned_run) = start.into_run(); - assert!(executor.requests().is_empty()); - registry - .get(&id, "original terminal") - .unwrap() - .cancel(State::Cancelled); - owned_run.await; - assert!(executor.requests().is_empty()); - assert_eq!( - registry - .get(&id, "original terminal") - .unwrap() - .snapshot() - .state(), - State::Cancelled - ); - }); -} diff --git a/crates/agent-chat/src/thread/fork.rs b/crates/agent-chat/src/thread/fork.rs new file mode 100644 index 00000000..ffaca115 --- /dev/null +++ b/crates/agent-chat/src/thread/fork.rs @@ -0,0 +1,102 @@ +//! A prepared fork owns its copied durable body until the destination admits it. +use super::{Attachment, Composer, Restore, config::ChatConfig, document::ChatDocument}; +/// What a forked chat starts from. +pub struct Fork { + chat: nocterm_ai::history::SavedChat, + restore: Option, + attachments: Vec, + _allocation: nocterm_core::allocation::AllocationLease, +} + +impl Fork { + pub(crate) fn prepared(mut self) -> Result { + self.chat = nocterm_ai::history::prepare_owned(self.chat)?; + Ok(self) + } + pub(crate) fn key(&self) -> nocterm_ai::execution_origin::ChatKey { + nocterm_ai::execution_origin::ChatKey::from_header(&self.chat.header) + } +} + +impl Fork { + pub(super) fn restoration( + session: &Option, + workdir: &Option, + restore: Option<&Restore>, + ) -> Option { + match (session, restore) { + (Some(session), _) => workdir.clone().map(|workdir| Restore { + session: session.clone(), + workdir, + fork: true, + }), + (None, Some(restore)) => Some(Restore { + fork: true, + ..restore.clone() + }), + (None, None) => None, + } + } + pub(super) fn capture( + mut chat: nocterm_ai::history::SavedChat, + source: ForkSource<'_>, + restore: Option, + len: usize, + allocation: nocterm_core::allocation::AllocationLease, + ) -> Self { + let whole = len >= source.document.entries().len(); + let end = len.min(source.document.entries().len()); + chat.entries = source.document.entries()[..end].to_vec(); + chat.times = source + .document + .transcript() + .times + .iter() + .take(end) + .copied() + .collect(); + chat.header.title = source.document.transcript().title.clone(); + chat.header.name = Some(format!("{} (fork)", source.title)); + chat.model = source.config.model(); + chat.config = source.config.choices().clone(); + chat.mode = source.config.mode_choice().map(ToString::to_string); + chat.pending_history = source.document.pending_history() || !whole; + Self { + chat, + restore: restore.filter(|_| whole), + attachments: source.composer.default_attachments().to_vec(), + _allocation: allocation, + } + } + pub(super) fn into_parts( + self, + ) -> ( + nocterm_ai::history::SavedChat, + Option, + Vec, + ) { + (self.chat, self.restore, self.attachments) + } +} + +pub(super) struct ForkSource<'a> { + document: &'a ChatDocument, + composer: &'a Composer, + config: &'a ChatConfig, + title: String, +} +impl<'a> ForkSource<'a> { + pub(super) fn new( + document: &'a ChatDocument, + composer: &'a Composer, + config: &'a ChatConfig, + title: String, + ) -> Self { + Self { + document, + composer, + config, + title, + } + } +} diff --git a/crates/agent-chat/src/thread/live.rs b/crates/agent-chat/src/thread/live.rs index ec8a916a..47934b12 100644 --- a/crates/agent-chat/src/thread/live.rs +++ b/crates/agent-chat/src/thread/live.rs @@ -158,11 +158,12 @@ impl LiveCommandOperation { }, cx, )?; - let (mut payload, bytes) = super::tool_runtime::presentation::text_payload(tail, redact); - payload["completion"] = serde_json::json!(reason); - payload["exit_status"] = serde_json::Value::Null; - payload["completed"] = serde_json::json!(reason == "prompt_returned"); - Ok(Some((payload, bytes))) + let report = super::tool_runtime::presentation::terminal_report(tail, redact); + let bytes = report.text.len(); + Ok(Some(( + serde_json::json!(report.observed(reason.into())), + bytes, + ))) } pub(super) async fn run( mut self, diff --git a/crates/agent-chat/src/thread/local_payloads.rs b/crates/agent-chat/src/thread/local_payloads.rs new file mode 100644 index 00000000..4648f0b2 --- /dev/null +++ b/crates/agent-chat/src/thread/local_payloads.rs @@ -0,0 +1,84 @@ +//! One Local resident body and separately retained immutable snapshot generations. +use gpui::{App, EntityId}; +use nocterm_agent_runtime::history_admission::{ + HistoryPayloadPermit, LocalHistoryAllocations, LocalSnapshotAllocation, +}; +use nocterm_ai::storage::AgentStorageError; + +pub(crate) type LocalPayloadAllocation = (LocalHistoryAllocations, HistoryPayloadPermit); + +pub(super) struct LocalPayloads { + allocations: LocalHistoryAllocations, + resident: Option, + requester: EntityId, +} +impl LocalPayloads { + pub(super) fn new( + allocations: LocalHistoryAllocations, + resident: HistoryPayloadPermit, + requester: EntityId, + ) -> Self { + Self { + allocations, + resident: Some(resident), + requester, + } + } + pub(super) fn order(&self) -> Option { + self.resident.as_ref().map(HistoryPayloadPermit::order) + } + pub(super) fn archive(&mut self) { + self.resident.take(); + } + pub(super) fn ensure_resident(&mut self, cx: &mut App) -> Result<(), AgentStorageError> { + if self.resident.is_none() { + self.resident = Some( + self.allocations + .reserve_resident(Some(self.requester), cx) + .map_err(|error| error.into_storage())?, + ); + } + Ok(()) + } + pub(super) fn adopt_resident(&mut self, payload: HistoryPayloadPermit) { + self.resident = Some(payload); + } + pub(super) fn history_admission( + &self, + ) -> nocterm_agent_runtime::history_admission::HistoryAdmissionHandle { + self.allocations.history_admission() + } + pub(super) fn restart( + &self, + document: &super::document::ChatDocument, + input: &super::document::Inputs<'_>, + existing: usize, + cx: &mut App, + ) -> Result { + Ok(self + .snapshot(existing, document.restart_capacity(input)?, cx)? + .into_lease()) + } + pub(super) fn fork( + &self, + document: &super::document::ChatDocument, + input: &super::document::Inputs<'_>, + existing: usize, + len: usize, + cx: &mut App, + ) -> Result { + Ok(self + .snapshot(existing, document.fork_capacity(input, len)?, cx)? + .into_lease()) + } + pub(super) fn snapshot( + &self, + existing: usize, + capacity: usize, + cx: &mut App, + ) -> Result { + self.allocations + .reserve_snapshot(self.requester, existing, capacity, cx) + .map_err(|error| error.into_storage()) + } +} diff --git a/crates/agent-chat/src/thread/queue.rs b/crates/agent-chat/src/thread/queue.rs index 62c97df5..6069351a 100644 --- a/crates/agent-chat/src/thread/queue.rs +++ b/crates/agent-chat/src/thread/queue.rs @@ -1,22 +1,19 @@ -//! Prompt snapshots and FIFO dispatch. Cancellation completes before dispatch resumes. +//! Prompt snapshots and FIFO dispatch. Cancellation completes before dispatch resumes, and a +//! turn the agent runs for another device holds dispatch until it ends. use super::tools::unreachable; use super::{ background_open::BackgroundOpen, bound_call::BoundToolCall, connection_wait::{ConnectionWaitError, WaitTarget}, - execution::jobs, + execution::WaitSubject, }; use gpui::{App, Context, EntityId, Task}; use nocterm_ai::history::{SavedAttachment, SavedPrompt}; use nocterm_ai::model::content::Content; use nocterm_ai::{BridgeCall, TerminalCall, ToolFailure}; -use nocterm_session::ExecRequest; use nocterm_workspace_ports::{ExpectedSession, TerminalEntry, TerminalStatus}; -use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, -}; +use std::sync::Arc; use super::{AgentThread, Attachment}; @@ -26,7 +23,6 @@ pub(super) use submit_error::SubmitError; #[derive(Clone)] pub struct QueuedPrompt { saved: Arc, - image_keys: Arc>>, attachments: Vec, encoded_len: usize, memory_len: usize, @@ -42,38 +38,19 @@ impl QueuedPrompt { attachments: Vec, encoded_len: Option, ) -> Self { - static NEXT_IMAGE: AtomicUsize = AtomicUsize::new(1); let encoded_len = encoded_len .unwrap_or_else(|| nocterm_ai::history::prompt_size(&saved).unwrap_or(usize::MAX)); - let image_keys = saved - .images - .iter() - .map(|block| match block { - Content::Image(image) => Some(( - NEXT_IMAGE.fetch_add(1, Ordering::Relaxed), - image.data.len(), - true, - )), - _ => None, - }) - .collect(); let memory_len = nocterm_ai::history::prompt_memory(&saved).unwrap_or(usize::MAX); Self { memory_len, saved: Arc::new(saved), attachments, - image_keys: Arc::new(image_keys), encoded_len, } } pub fn saved(&self) -> &Arc { &self.saved } - /// One key per image block: its preview identity, encoded size and - /// whether it still awaits preparation; `None` for other blocks. - pub fn image_keys(&self) -> &[Option<(usize, usize, bool)>] { - &self.image_keys - } /// What the prompt was written about, captured when it was queued. pub fn attachments(&self) -> &[Attachment] { &self.attachments @@ -87,7 +64,7 @@ impl QueuedPrompt { } impl AgentThread { - fn start_execution_watch(&mut self, cx: &mut Context) { + pub(super) fn start_execution_watch(&mut self, cx: &mut Context) { if self.tools.has_watch() { return; } @@ -98,9 +75,9 @@ impl AgentThread { })); } - pub(super) fn poll_command_wait( + pub(super) fn poll_tool_wait( &mut self, - operation: &super::execution::CommandWaitOperation, + operation: &super::execution::ToolWaitOperation, cx: &mut Context, ) -> Result, ToolFailure> { if !self.session.session_current(operation.ticket()) @@ -110,23 +87,18 @@ impl AgentThread { return Err(ToolFailure::from("Chat is unavailable.")); } self.revoke_executions(cx); - let state = self - .tools - .command_snapshot(operation.id(), operation.terminal())?; - operation.frame(state, self.deps.ai(cx).approval.redact_secrets) + operation.frame(&mut self.tools, self.deps.ai(cx).approval.redact_secrets) } - fn wait_command( + pub(super) fn wait_tool( &mut self, - terminal: String, - id: String, + subject: WaitSubject, yield_ms: u64, call: BridgeCall, cx: &mut Context, ) { - let operation = super::execution::CommandWaitOperation::new( - terminal, - id, + let operation = super::execution::ToolWaitOperation::new( + subject, yield_ms, self.session.ticket(), self.hold_operation(), @@ -150,9 +122,8 @@ impl AgentThread { let TerminalCall::ReadCommand(request) = &call.call else { return; }; - let terminal = request.terminal_id.clone(); - let id = request.command_id.clone(); - self.wait_command(terminal, id, request.yield_ms.unwrap_or(0), call, cx); + let subject = WaitSubject::command(&request.terminal_id, &request.command_id); + self.wait_tool(subject, request.yield_ms.unwrap_or(0), call, cx); } pub(super) fn execute_command( @@ -165,48 +136,19 @@ impl AgentThread { let TerminalCall::ExecCommand(request) = &call.call else { return; }; - let executor = match entry - .capabilities - .execution() - .ok_or(nocterm_workspace_ports::TerminalError::Unsupported( - nocterm_workspace_ports::TerminalCapability::StructuredExecution, - )) - .and_then(|port| port.executor(expected, cx)) + let source = + super::tool_runtime::source_execution::SourceExecution::capture(self.deps.runtime()); + let (id, run) = match source + .and_then(|source| source.start(entry, expected, request, &mut self.tools, cx)) { - Ok(executor) => executor, - Err(error) => { - self.finish(call, Err(error.to_string().into())); - return; - } + Ok(started) => started, + Err(error) => return self.finish(call, Err(error)), }; - let program = ExecRequest { - program: request.program.clone(), - args: request.args.clone(), - stdin: request - .stdin - .as_ref() - .map(|stdin| stdin.as_bytes().to_vec()), - }; - let timer = cx - .background_executor() - .timer(jobs::timeout(request.timeout_ms)); - let started = - match self - .tools - .start_command(request.terminal_id.clone(), executor, program, timer) - { - Ok(started) => started, - Err(error) => { - self.finish(call, Err(error)); - return; - } - }; - let (id, run) = started.into_run(); cx.background_executor().spawn(run).detach(); self.start_execution_watch(cx); - let terminal = request.terminal_id.clone(); + let subject = WaitSubject::command(&request.terminal_id, &id); let yield_ms = request.yield_ms.unwrap_or(1000); - self.wait_command(terminal, id, yield_ms, call, cx); + self.wait_tool(subject, yield_ms, call, cx); } fn reconnect_target( @@ -219,6 +161,9 @@ impl AgentThread { .bindings .window() .ok_or(ConnectionWaitError::WorkspaceClosed)?; + if !self.deps.enabled(cx) { + return Err(ConnectionWaitError::Stopped); + } let receipt = self.tools.prepare_reconnect(entry, expected, cx)?; Ok(WaitTarget::new( self.deps.seat().clone(), @@ -363,8 +308,15 @@ impl AgentThread { DispatchAction::Execute(entry, expected, call) => { self.execute_command(entry, expected, call, cx) } + DispatchAction::Files(call, work) => { + let guard = self.hold_operation(); + super::tool_runtime::file_dispatch::complete(call, work, guard, cx); + } DispatchAction::Read(call) => self.read_command(call, cx), DispatchAction::Cancel(call) => self.cancel_command(call, cx), + DispatchAction::Transfer(admission) => self.start_transfer(*admission, cx), + DispatchAction::ReadTransfer(call) => self.read_transfer(call, cx), + DispatchAction::CancelTransfer(call) => self.cancel_transfer(call, cx), DispatchAction::Ignore => {} } } @@ -377,7 +329,9 @@ impl AgentThread { cx: &mut Context, ) { let redact = self.deps.ai(cx).approval.redact_secrets; - let action = self.tools.dispatch(call, entry, expected, redact, cx); + let action = self + .tools + .dispatch(call, entry, expected, redact, self.deps.enabled(cx), cx); self.apply_dispatch_action(action, cx); } @@ -558,6 +512,9 @@ impl AgentThread { call: BridgeCall, cx: &mut Context, ) { + if !self.deps.enabled(cx) { + return self.finish_unreachable(call, "Chat is unavailable."); + } let mut operation = match super::live::LiveCommandOperation::acquire(entry, expected, request, cx) { Ok(operation) => operation, @@ -590,7 +547,11 @@ impl AgentThread { // ACP adapters inspect the first block for slash commands; local // administrative commands may require a single text block. let is_command = nocterm_ai::commands::invocation(&text, self.config.commands()).is_some(); - let context = (!is_command).then(|| self.context(cx)); + // An agent with tools of its own gets only what the person wrote. + let own_tools = self + .agent_info() + .is_some_and(|info| info.capabilities.own_tools); + let context = (!is_command && !own_tools).then(|| self.context(cx)); let history = (self.state.pending_history() && !is_command) .then(|| super::session::history_context(self.state.entries())); let prepared = super::composer::prompt_preparation::PromptPreparation::new( @@ -605,8 +566,15 @@ impl AgentThread { let resume = super::document::Resume::new(Some(session.to_string()), self.session_workdir().clone()); let input = super::document::Inputs::new(&self.composer, &self.config, attachments, resume); - self.state - .append_user(prepared.visible().to_vec(), self.deps.timestamp(), &input); + if let Err(error) = + self.state + .append_user(prepared.visible().to_vec(), self.deps.timestamp(), &input) + { + self.fail(&error.to_string(), cx); + return; + } + // Consume only after live-transcript admission: a refusal preserves the queued body. + let _ = self.composer.take_next(); self.mark_dirty(self.state.entries().len() - 1); self.persist(cx); self.notice.present("Thinking…".into()); @@ -647,6 +615,7 @@ impl AgentThread { self.prune_background(cx); } } + self.offer_tools(cx); self.accept_document_inputs(cx, false); self.save(cx); cx.notify(); @@ -692,7 +661,10 @@ impl AgentThread { if !self.deps.enabled(cx) || self.session.sign_in_required() { return Ok(false); } - if text.trim().is_empty() && self.composer.images().is_empty() { + if text.trim().is_empty() + && self.composer.images().is_empty() + && self.composer.files().is_empty() + { return Ok(false); } let candidate = self.composer.prepare_submission( @@ -726,6 +698,36 @@ impl AgentThread { Ok(true) } + /// A turn the agent runs for another device: its prompt joins the transcript and the chat + /// shows the agent working until the turn ends; then the chat's own queue goes on. + pub(super) fn external_turn( + &mut self, + activity: &nocterm_ai::model::session::TurnActivity, + cx: &mut Context, + ) { + use nocterm_ai::model::session::TurnActivity; + match activity { + TurnActivity::Started { prompt } => { + if let Err(error) = self.state.append_prompt(prompt, self.deps.timestamp()) { + return self.fail(&error.to_string(), cx); + } + if self.session.begin_external_turn() { + self.notice.present("Working…".into()); + } + } + TurnActivity::Ended => { + if !self.session.finish_external_turn() { + return; + } + self.finalize_tool_displays(); + self.notice.present("Ready".into()); + self.persist(cx); + self.dispatch_next(cx); + } + } + cx.notify(); + } + pub fn dispatch_next(&mut self, cx: &mut Context) { if !self.queue_dispatchable() || self.session.generating() @@ -745,7 +747,7 @@ impl AgentThread { if self.session.phase() != nocterm_ai::session::SessionPhase::Ready { return; } - let Some(prompt) = self.composer.take_next() else { + let Some(prompt) = self.composer.queue().first().cloned() else { return; }; // Context is fixed for this turn, including terminal tool access. @@ -770,7 +772,7 @@ pub(super) fn restore_attachments(values: &[SavedAttachment]) -> Vec mod tests { use super::*; #[test] - fn rendering_snapshot_shares_image_payload_and_precomputed_keys() { + fn rendering_snapshot_shares_immutable_image_payload() { let prompt = QueuedPrompt::new( SavedPrompt { id: 1, @@ -784,8 +786,14 @@ mod tests { Vec::new(), ); let copy = prompt.clone(); - assert!(Arc::ptr_eq(&prompt.saved, ©.saved)); - assert!(Arc::ptr_eq(&prompt.image_keys, ©.image_keys)); - assert_eq!(copy.image_keys[0].unwrap().1, 2 * 1024 * 1024); + assert!(Arc::ptr_eq(prompt.saved(), copy.saved())); + let Content::Image(original) = &prompt.saved().images[0] else { + panic!("image"); + }; + let Content::Image(cloned) = ©.saved().images[0] else { + panic!("image"); + }; + assert_eq!(original.data.as_ptr(), cloned.data.as_ptr()); + assert_eq!(cloned.data.len(), 2 * 1024 * 1024); } } diff --git a/crates/agent-chat/src/thread/restart.rs b/crates/agent-chat/src/thread/restart.rs index 119f8fea..9679c332 100644 --- a/crates/agent-chat/src/thread/restart.rs +++ b/crates/agent-chat/src/thread/restart.rs @@ -5,6 +5,7 @@ pub struct RestartData { config: super::config::RestartConfig, composer: Composer, restore: Option, + _allocation: nocterm_core::allocation::AllocationLease, } impl RestartData { pub(super) fn new( @@ -12,12 +13,14 @@ impl RestartData { config: super::config::RestartConfig, composer: Composer, restore: Option, + allocation: nocterm_core::allocation::AllocationLease, ) -> Self { Self { document, config, composer, restore, + _allocation: allocation, } } pub(super) fn into_parts( diff --git a/crates/agent-chat/src/thread/saved.rs b/crates/agent-chat/src/thread/saved.rs index dbe8eae6..be2c6eba 100644 --- a/crates/agent-chat/src/thread/saved.rs +++ b/crates/agent-chat/src/thread/saved.rs @@ -1,9 +1,8 @@ //! A chat as saved in the history: restoring, forking, naming and saving. use gpui::{App, Context}; -use nocterm_ai::model::content::Content; use nocterm_ai::model::controls::ConfigSetting; use nocterm_ai::model::ids::{ConfigId, ModeId, SessionId}; -use nocterm_ai::thread::ThreadState; +use nocterm_ai::{history::SharedChat, storage::AgentStorageError}; use std::{sync::Arc, time::Duration}; use super::{AgentThread, Attachment, Composer, Fork, RestartData, Restore, archive}; @@ -61,10 +60,11 @@ impl AgentThread { pub fn begin_composer_edit( &mut self, images: Vec, + files: Vec, attachments: Vec, cx: &mut Context, ) { - self.composer.begin_edit(images, attachments); + self.composer.begin_edit(images, files, attachments); cx.notify(); } @@ -84,6 +84,23 @@ impl AgentThread { Ok(()) } + /// Adds files to the next prompt; files it holds already are not added twice. + pub fn add_composer_files( + &mut self, + files: Vec, + cx: &mut Context, + ) -> Result<(), nocterm_ai::prompt_files::PromptFileError> { + self.composer.add_files(files)?; + cx.notify(); + Ok(()) + } + + pub fn remove_composer_file(&mut self, index: usize, cx: &mut Context) { + if self.composer.remove_file(index) { + cx.notify(); + } + } + #[cfg(any(test, feature = "test-support"))] pub fn clear_composer_context(&mut self, cx: &mut Context) { self.replace_composer_context(Vec::new(), cx); @@ -221,72 +238,102 @@ impl AgentThread { pub(crate) fn restarted( data: RestartData, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { let (document, config, composer, restore) = data.into_parts(); let state = super::document::ChatDocument::restarted(document); - let mut thread = Self::from_document(state, deps, cx); + let mut thread = Self::from_document(state, deps, allocation, cx); thread.config.hydrate_restart(config); thread.composer = composer; thread.session.import_restore(restore); thread } - pub fn restart_data(&self) -> RestartData { - RestartData::new( + pub fn restart_data(&self, cx: &mut App) -> Result { + let input = super::document::Inputs::new( + &self.composer, + &self.config, + self.stable_attachments(self.composer.default_attachments(), cx), + self.persistent_resume(), + ); + let allocation = + self.payloads + .restart(&self.state, &input, self.resident_history_bytes(), cx)?; + Ok(RestartData::new( self.state.restart_snapshot(), self.config.restart_snapshot(), self.composer.restarted(), self.restore_descriptor(), - ) + allocation, + )) } pub fn capture_pending( &mut self, - cx: &App, - ) -> Option<(Arc, u64)> { + cx: &mut App, + ) -> Result, u64)>, AgentStorageError> { self.state.cancel_debounce(); if !self.state.needs_candidate() { - return self.state.existing_offer(); + return Ok(self.state.existing_offer()); } if self.is_archived() { - let snapshot = self.archive.draft_patch().map(|metadata| { - Arc::new(nocterm_ai::history::SharedChat { - metadata, - queue: Vec::new(), - }) - })?; - return self.state.capture_patch(snapshot); + let allocation = self.payloads.snapshot( + self.resident_history_bytes(), + self.archive.draft_capacity(), + cx, + )?; + let Some(metadata) = self.archive.draft_patch() else { + return Ok(None); + }; + let snapshot = allocation.retain(Arc::new(SharedChat { + metadata, + queue: Vec::new(), + }))?; + return Ok(self.state.capture_patch(snapshot)); } let attachments = self.stable_attachments(self.composer.default_attachments(), cx); let resume = self.persistent_resume(); let input = super::document::Inputs::new(&self.composer, &self.config, attachments, resume); - self.state.propose_candidate(&input) + let capacity = self.state.snapshot_capacity(&input)?; + let allocation = self + .payloads + .snapshot(self.resident_history_bytes(), capacity, cx)?; + let Some(snapshot) = self.state.candidate_snapshot(&input) else { + return Ok(None); + }; + let snapshot = allocation.retain(snapshot)?; + Ok(self.state.adopt_candidate(&input, snapshot)) } - pub fn flush_snapshot(&self, cx: &App) -> Option> { + pub fn flush_snapshot( + &self, + cx: &mut App, + ) -> Result>, AgentStorageError> { if self.state.clean() { - return None; + return Ok(None); } if let Some(snapshot) = self.state.current_snapshot() { - return Some(snapshot); + return Ok(Some(snapshot)); } if self.is_archived() { - return self.archive.draft_patch().map(|metadata| { - Arc::new(nocterm_ai::history::SharedChat { - metadata, - queue: Vec::new(), + let allocation = self.payloads.snapshot( + self.resident_history_bytes(), + self.archive.draft_capacity(), + cx, + )?; + return self + .archive + .draft_patch() + .map(|metadata| { + allocation.retain(Arc::new(SharedChat { + metadata, + queue: Vec::new(), + })) }) - }); + .transpose(); } - self.shared_snapshot(cx).or_else(|| { - self.state.history_present().then(|| { - Arc::new(nocterm_ai::history::SharedChat { - metadata: self.history_metadata(cx), - queue: Vec::new(), - }) - }) - }) + self.shared_snapshot(cx) } pub fn flush_draft(&mut self, cx: &mut Context) { @@ -327,15 +374,18 @@ impl AgentThread { && !self.session.leased() && !self.session_busy() } - pub fn evict_history(&mut self, cx: &mut Context) -> bool { - let gate = archive::EvictionGate::new( + pub(super) fn history_eviction_gate(&self) -> archive::EvictionGate { + archive::EvictionGate::new( !self.session.leased(), !self.session_busy() && !self.session.shown(), self.state.desired_revision() == self.state.published_revision(), !self.state.draft_changed(), self.state.persistence_error().is_none(), self.state.history_present(), - ); + ) + } + pub fn evict_history(&mut self, cx: &mut Context) -> bool { + let gate = self.history_eviction_gate(); let mut summary = nocterm_ai::history::ChatSummary::from_chat(&self.history_metadata(cx)); summary.title = Some(self.title()); summary.has_content = !self.state.entries().is_empty() || !self.composer.is_empty(); @@ -343,6 +393,7 @@ impl AgentThread { return false; } self.state.evict_clean(); + self.payloads.archive(); self.composer = Default::default(); self.tools.clear_displays(); cx.notify(); @@ -373,53 +424,47 @@ impl AgentThread { } } - fn on_archive_loaded( + pub(super) fn on_archive_loaded( &mut self, completion: archive::load_operation::LoadCompletion, cx: &mut Context, ) { - let (load_id, ticket, result, permit) = completion.into_parts(); - - let current = self.session.session_current(ticket) - && self.deps.enabled(cx) - && self - .deps - .runtime() - .document_is_current(self.state.chat_id(), cx.entity_id(), cx); - let result = if current { - result.and_then(|chat| { - self.deps - .runtime() - .admit_history(cx.entity_id(), chat.resident_bytes(), cx)?; - Ok(chat) - }) - } else { - result - }; - match self.archive.accept(load_id, current, result, permit) { - archive::LoadOutcome::Stale => return, - archive::LoadOutcome::Failed(error) => { - self.notice - .present(format!("Could not load saved chat: {error}")); - self.notice.mark_error(); - } - archive::LoadOutcome::Hydrate { - chat, - overlay, - intents, - } => { - self.restore_content_with_overlay(*chat, overlay); - self.preview_config(cx); - if self.state.draft_changed() { - self.save(cx); + completion.publish(|load_id, ticket, result, permit, payload| { + let current = self.session.session_current(ticket) + && self.deps.enabled(cx) + && self.deps.runtime().document_is_current( + self.state.chat_id(), + cx.entity_id(), + cx, + ); + match self.archive.accept(load_id, current, result, permit) { + archive::LoadOutcome::Stale => return, + archive::LoadOutcome::Failed(error) => { + self.notice + .present(format!("Could not load saved chat: {error}")); + self.notice.mark_error(); } - for intent in intents { - self.apply_archive_intent(intent, cx); + archive::LoadOutcome::Hydrate { + chat, + overlay, + intents, + } => { + if let Some(payload) = payload { + self.payloads.adopt_resident(payload); + } + self.restore_content_with_overlay(*chat, overlay); + self.preview_config(cx); + if self.state.draft_changed() { + self.save(cx); + } + for intent in intents { + self.apply_archive_intent(intent, cx); + } + self.warm(cx); } - self.warm(cx); } - } - cx.notify(); + cx.notify(); + }); } pub fn load_archive(&mut self, intent: archive::ArchiveIntent, cx: &mut Context) { @@ -439,12 +484,7 @@ impl AgentThread { let reader = self.deps.runtime().history_reader(cx); let operation = archive::load_operation::LoadOperation::new(load_id, ticket, chat_id, reader); - let loading = cx.background_executor().spawn(operation.run()); - cx.spawn(async move |this, cx| { - let completion = loading.await; - let _ = this.update(cx, |this, cx| this.on_archive_loaded(completion, cx)); - }) - .detach(); + operation.start(self.payloads.history_admission(), cx); cx.notify(); } @@ -456,18 +496,17 @@ impl AgentThread { pub(crate) fn restored( chat: nocterm_ai::history::SavedChat, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { let mut thread = Self::from_document( super::document::ChatDocument::new( chat.header.agent_id.clone(), - nocterm_ai::execution_origin::ChatKey::new( - chat.header.scope, - chat.header.id.clone(), - ), + nocterm_ai::execution_origin::ChatKey::from_header(&chat.header), chat.header.updated, ), deps, + allocation, cx, ); thread.restore_content_with_overlay(chat, None); @@ -478,13 +517,14 @@ impl AgentThread { &mut self, chat: nocterm_ai::history::SavedChat, cx: &mut Context, - ) -> Result<(), nocterm_ai::storage::AgentStorageError> { - self.deps.require_scope(chat.header.scope)?; + ) -> Result<(), AgentStorageError> { + self.deps.require_local_scope(&chat.header.chat_scope())?; chat.validate_identity()?; if chat.header.id != self.state.chat_id() { return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); } - let chat = nocterm_ai::history::prepare(chat)?; + let chat = nocterm_ai::history::prepare_owned(chat)?; + self.payloads.ensure_resident(cx)?; self.deps .runtime() .admit_history(cx.entity_id(), chat.resident_bytes(), cx)?; @@ -542,58 +582,54 @@ impl AgentThread { } /// A copy of this chat, to go on with separately. When opened, the copy /// continues in a copy of this chat's agent session. - pub fn fork(&self) -> Fork { - self.fork_at(self.state.entries().len()) + pub fn fork(&self, cx: &mut App) -> Result { + self.fork_at(self.state.entries().len(), cx) } /// A copy of the chat's first `len` entries. Only a copy of the whole /// chat continues in a copy of the agent's session; an earlier cut /// starts a new session, since the agent cannot forget the rest. - pub fn fork_at(&self, len: usize) -> Fork { - let whole = len >= self.state.entries().len(); - let mut chat = self.deps.new_chat(self.state.agent_id().to_owned()); - let mut state = self.state.transcript().clone(); - state.truncate(len); - chat.entries = state.entries; - chat.times = state.times; - chat.header.title = self.state.transcript().title.clone(); - chat.header.name = Some(format!("{} (fork)", self.title())); - chat.model = self.model(); - chat.config = self.config.choices().clone(); - chat.mode = self.config.mode_choice().map(ToString::to_string); - chat.pending_history = self.state.pending_history() || !whole; - let restore = match (self.session(), self.session.restore()) { - (Some(session), _) => self.session_workdir().clone().map(|workdir| Restore { - session: session.clone(), - workdir, - fork: true, - }), - (None, Some(restore)) => Some(Restore { - fork: true, - ..restore.clone() - }), - (None, None) => None, - } - .filter(|_| whole); - Fork { + pub fn fork_at(&self, len: usize, cx: &mut App) -> Result { + let input = super::document::Inputs::new( + &self.composer, + &self.config, + self.stable_attachments(self.composer.default_attachments(), cx), + self.persistent_resume(), + ); + let allocation = + self.payloads + .fork(&self.state, &input, self.resident_history_bytes(), len, cx)?; + let chat = self + .deps + .new_chat_for_origin(self.state.agent_id().to_owned(), self.state.key()); + let restore = Fork::restoration( + self.session(), + self.session_workdir(), + self.session.restore().as_ref(), + ); + Ok(Fork::capture( chat, + super::fork::ForkSource::new(&self.state, &self.composer, &self.config, self.title()), restore, - attachments: self.composer.default_attachments().to_vec(), - } + len, + allocation, + )) } /// The chat `fork` made, connected only when work is submitted. pub(crate) fn forked( fork: Fork, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { + let (chat, restore, attachments) = fork.into_parts(); let pending_history = - fork.chat.pending_history || (fork.restore.is_none() && !fork.chat.entries.is_empty()); - let mut thread = Self::restored(fork.chat, deps, cx); + chat.pending_history || (restore.is_none() && !chat.entries.is_empty()); + let mut thread = Self::restored(chat, deps, allocation, cx); thread .state .set_pending_history(pending_history, thread.deps.timestamp()); - thread.session.import_restore(fork.restore); - thread.composer.replace_context(fork.attachments); + thread.session.import_restore(restore); + thread.composer.replace_context(attachments); thread.establish_clean_document_inputs(cx); thread.state.fork_created(); thread.notice.present("Forked chat".into()); @@ -603,7 +639,7 @@ impl AgentThread { /// the first message. pub fn title(&self) -> String { self.archive.summary().map_or_else( - || chat_title(self.state.name(), self.state.transcript()), + || super::title::chat_title(self.state.name(), self.state.transcript()), |summary| summary.title().to_owned(), ) } @@ -658,10 +694,10 @@ impl AgentThread { } pub fn shared_snapshot( &self, - cx: &gpui::App, - ) -> Option> { + cx: &mut App, + ) -> Result>, AgentStorageError> { if self.is_archived() { - return None; + return Ok(None); } let input = super::document::Inputs::new( &self.composer, @@ -669,7 +705,14 @@ impl AgentThread { self.stable_attachments(self.composer.default_attachments(), cx), self.persistent_resume(), ); - self.state.snapshot(&input) + let capacity = self.state.snapshot_capacity(&input)?; + let allocation = self + .payloads + .snapshot(self.resident_history_bytes(), capacity, cx)?; + self.state + .candidate_snapshot(&input) + .map(|snapshot| allocation.retain(snapshot)) + .transpose() } pub(super) fn accept_document_inputs(&mut self, cx: &gpui::App, draft: bool) -> bool { let attachments = self.stable_attachments(self.composer.default_attachments(), cx); @@ -686,7 +729,14 @@ impl AgentThread { } #[cfg(any(test, feature = "test-support"))] pub fn snapshot(&self, cx: &gpui::App) -> Option { - let shared = self.shared_snapshot(cx)?; + // Assertion-only export; production persistence uses charged captures. + let input = super::document::Inputs::new( + &self.composer, + &self.config, + self.stable_attachments(self.composer.default_attachments(), cx), + self.persistent_resume(), + ); + let shared = self.state.snapshot(&input)?; let mut chat = shared.metadata.clone(); chat.queue = shared .queue @@ -699,8 +749,13 @@ impl AgentThread { self.save(cx); } pub fn save(&mut self, cx: &mut Context) { - let Some((chat, revision)) = self.capture_pending(cx) else { - return; + let (chat, revision) = match self.capture_pending(cx) { + Ok(Some(value)) => value, + Ok(None) => return, + Err(error) => { + self.document_save_failed(self.state.desired_revision(), error.into(), cx); + return; + } }; if !self.state.mark_normal_offer(revision) { return; @@ -724,33 +779,3 @@ impl AgentThread { }); } } - -/// A chat's title: `name`, else the agent's title, else the start of the -/// first message. A chat with nothing in it is a new one. -pub(super) fn chat_title(name: Option<&str>, state: &ThreadState) -> String { - if let Some(name) = name.filter(|name| !name.trim().is_empty()) { - return name.to_owned(); - } - if state.entries.is_empty() { - return "New Agent".into(); - } - state - .title - .as_ref() - .filter(|title| !title.trim().is_empty()) - .cloned() - .or_else(|| { - state.entries.iter().find_map(|entry| match entry { - nocterm_ai::thread::Entry::User(parts) => { - parts.iter().find_map(|part| match part { - Content::Text(text) if !text.text.trim().is_empty() => { - Some(text.text.trim().chars().take(60).collect()) - } - _ => None, - }) - } - _ => None, - }) - }) - .unwrap_or_else(|| "New Agent".into()) -} diff --git a/crates/agent-chat/src/thread/session_driver.rs b/crates/agent-chat/src/thread/session_driver.rs index c3c8ac05..dc7558b0 100644 --- a/crates/agent-chat/src/thread/session_driver.rs +++ b/crates/agent-chat/src/thread/session_driver.rs @@ -222,6 +222,12 @@ impl SessionDriver { pub(super) fn begin_cancel(&mut self) -> bool { self.phase.stop() } + pub(super) fn begin_external_turn(&mut self) -> bool { + self.phase.start_external_turn() + } + pub(super) fn finish_external_turn(&mut self) -> bool { + self.phase.finish_external_turn() + } pub(super) fn opened(&mut self) { self.phase.opened() } diff --git a/crates/agent-chat/src/thread/session_grants.rs b/crates/agent-chat/src/thread/session_grants.rs index e5bfcda4..a65fb1ca 100644 --- a/crates/agent-chat/src/thread/session_grants.rs +++ b/crates/agent-chat/src/thread/session_grants.rs @@ -1,5 +1,5 @@ //! Reusable target grants expire when the observed session changes. -use nocterm_ai::{ApprovalSettings, TerminalCall, ToolCapability, approval::ApprovalGrants}; +use nocterm_ai::{ApprovalSettings, TerminalCall, approval::ApprovalGrants}; use nocterm_workspace_ports::ExpectedSession; use std::collections::HashMap; #[derive(Default)] @@ -9,9 +9,21 @@ pub(super) struct SessionGrants { } impl SessionGrants { pub(super) fn observe(&mut self, call: &TerminalCall, expected: Option) { - let Some(id) = call.terminal_id() else { - return; - }; + if let Some(id) = call.terminal_id() { + self.observe_session(id, expected); + } + } + /// The source terminal of a transfer expires its grants the same way. + pub(super) fn observe_source( + &mut self, + call: &TerminalCall, + expected: Option, + ) { + if let Some(id) = call.source_terminal_id() { + self.observe_session(id, expected); + } + } + fn observe_session(&mut self, id: &str, expected: Option) { if self.sessions.get(id).copied() != expected { self.grants.revoke(id); self.sessions.remove(id); @@ -23,8 +35,8 @@ impl SessionGrants { pub(super) fn requires_approval(&self, call: &TerminalCall, policy: &ApprovalSettings) -> bool { self.grants.requires_approval(call, policy) } - pub(super) fn grant(&mut self, id: &str, capability: ToolCapability) { - self.grants.grant(id, capability); + pub(super) fn grant_call(&mut self, call: &TerminalCall) { + self.grants.grant_call(call); } pub(super) fn clear(&mut self) { self.grants.clear(); diff --git a/crates/agent-chat/src/thread/title.rs b/crates/agent-chat/src/thread/title.rs new file mode 100644 index 00000000..33230911 --- /dev/null +++ b/crates/agent-chat/src/thread/title.rs @@ -0,0 +1,33 @@ +//! What a chat is called in the history. +use nocterm_ai::model::content::Content; +use nocterm_ai::thread::ThreadState; + +/// A chat's title: `name`, else the agent's title, else the start of the +/// first message. A chat with nothing in it is a new one. +pub(crate) fn chat_title(name: Option<&str>, state: &ThreadState) -> String { + if let Some(name) = name.filter(|name| !name.trim().is_empty()) { + return name.to_owned(); + } + if state.entries.is_empty() { + return "New Agent".into(); + } + state + .title + .as_ref() + .filter(|title| !title.trim().is_empty()) + .cloned() + .or_else(|| { + state.entries.iter().find_map(|entry| match entry { + nocterm_ai::thread::Entry::User(parts) => { + parts.iter().find_map(|part| match part { + Content::Text(text) if !text.text.trim().is_empty() => { + Some(text.text.trim().chars().take(60).collect()) + } + _ => None, + }) + } + _ => None, + }) + }) + .unwrap_or_else(|| "New Agent".into()) +} diff --git a/crates/agent-chat/src/thread/tool_runtime.rs b/crates/agent-chat/src/thread/tool_runtime.rs index b2d89c87..a9140176 100644 --- a/crates/agent-chat/src/thread/tool_runtime.rs +++ b/crates/agent-chat/src/thread/tool_runtime.rs @@ -16,20 +16,25 @@ use nocterm_workspace_ports::{ExpectedSession, LiveCommandLease}; use std::sync::Arc; mod command_jobs; pub(in crate::thread) mod dispatch; +pub(in crate::thread) mod file_dispatch; mod live_observations; pub(in crate::thread) mod presentation; pub(in crate::thread) mod reconnect_dispatch; pub(in crate::thread) mod reconnect_operation; pub(in crate::thread) mod resolution; +pub(in crate::thread) mod source_execution; mod terminal_sessions; +mod transfer_dispatch; +mod transfers; mod turn_context; use command_jobs::CommandJobs; pub(super) use command_jobs::{CommandSnapshot, CommandStart}; use live_observations::LiveObservations; use reconnect_dispatch::{DispatchEffect, DispatchScope, DispatchTicket, ReconnectDispatch}; use terminal_sessions::TerminalSessions; +pub(super) use transfer_dispatch::{RejectedTransfer, StartedTransfer, TransferAdmission}; +use transfers::ChatTransfers; use turn_context::TurnContext; -#[derive(Default)] pub(super) struct ToolRuntime { approvals: ApprovalInbox, turn: TurnContext, @@ -38,8 +43,29 @@ pub(super) struct ToolRuntime { terminals: TerminalSessions, displays: super::tool_display::ToolDisplays, reconnect_dispatch: ReconnectDispatch, + transfers: ChatTransfers, } impl ToolRuntime { + /// A chat's tools over what its installed runtime supplies. + pub(super) fn attached(runtime: &nocterm_agent_runtime::RuntimeHandle, cx: &gpui::App) -> Self { + Self::new(runtime.command_jobs(cx), runtime.transfers(cx)) + } + pub(super) fn new( + jobs: Box, + transfers: Arc, + ) -> Self { + Self { + approvals: Default::default(), + turn: Default::default(), + commands: CommandJobs::new(jobs), + live: Default::default(), + terminals: Default::default(), + displays: Default::default(), + reconnect_dispatch: Default::default(), + transfers: ChatTransfers::new(transfers), + } + } + pub(super) fn prepare_reconnect( &mut self, entry: &nocterm_workspace_ports::TerminalEntry, @@ -78,13 +104,62 @@ impl ToolRuntime { pub(super) fn take_ready_reconnect(&mut self, scope: DispatchScope) -> Option { self.reconnect_dispatch.take_ready(scope) } - pub(super) fn revoke_replaced_executors( + /// Revokes the transfers, live terminals and command executors whose terminals detached or + /// changed session, given the attachments `resolved` now. + pub(super) fn revoke_replaced( &mut self, - current: &[(String, Option>)], - attached: &[String], + resolved: Vec<( + String, + nocterm_workspace_ports::TerminalEntry, + nocterm_ai::context::TerminalDescriptor, + )>, + cx: &gpui::App, ) { - self.live.revoke_detached(attached); - self.commands.revoke_replaced(current); + self.revoke_replaced_transfers(&resolved, cx); + let attached = resolved + .iter() + .map(|(id, _, _)| id.clone()) + .collect::>(); + let current = resolved + .into_iter() + .map(|(id, entry, _)| { + let executor = entry.capabilities.state().info(cx).and_then(|info| { + entry + .capabilities + .execution()? + .executor(info.expected, cx) + .ok() + }); + (id, executor) + }) + .collect::>(); + self.live.revoke_detached(&attached); + self.commands.revoke_replaced(¤t); + } + + /// Where a tool call runs, as its row names it: the attached terminal `entry`, else the + /// offline server the request names. + pub(super) fn destination( + &mut self, + request: &TerminalCall, + entry: Option<&nocterm_workspace_ports::TerminalEntry>, + input: &resolution::Resolution, + defaults: &[Attachment], + cx: &gpui::App, + ) -> Option { + if let Some(entry) = entry { + let info = entry.capabilities.state().info(cx)?; + if info.local { + return Some("This computer".into()); + } + let target = info.target?; + return Some(format!("{} · {target}", entry.title)); + } + let server = request.server_id()?; + self.offline_servers(input, defaults, cx) + .into_iter() + .find(|(descriptor, _)| descriptor.server_id == server) + .map(|(descriptor, summary)| format!("{} · {}", descriptor.name, summary.target)) } pub(super) fn prepare_open( @@ -153,8 +228,12 @@ impl ToolRuntime { entry: nocterm_workspace_ports::TerminalEntry, expected: ExpectedSession, redact: bool, + available: bool, cx: &mut gpui::App, ) -> dispatch::DispatchAction { + if !available { + return dispatch::DispatchAction::Respond(call, Err("Chat is unavailable.".into()), 0); + } let action = dispatch::TerminalDispatch::new(call, entry, expected).prepare(redact, cx); if let dispatch::DispatchAction::Respond(_, _, bytes) = &action { self.turn.account_outcome(*bytes); @@ -195,6 +274,25 @@ impl ToolRuntime { ) -> String { self.turn.context(input, defaults, cx) } + /// Tells the chat's bridge what it offers the agent: the file tools only while an attached + /// terminal takes them. The bridge tells a connected agent when that changes. + pub(super) fn offer_tools( + &mut self, + input: &resolution::Resolution, + defaults: &[Attachment], + registration: Option<&nocterm_ai::BridgeRegistration>, + cx: &gpui::App, + ) { + let Some(registration) = registration else { + return; + }; + let files = self + .turn + .resolved(input, defaults, cx) + .iter() + .any(|(_, _, descriptor)| descriptor.file_tools); + registration.offer(nocterm_ai::ToolOffer::new(files)); + } pub(super) fn describe_target( &mut self, call: &TerminalCall, @@ -206,7 +304,7 @@ impl ToolRuntime { } pub(super) fn busy(&self) -> bool { - self.approvals.pending() || self.commands.active().next().is_some() || self.live.active() + self.approvals.pending() || self.commands.has_active() || self.live.active() } pub(super) fn capture_scope(&mut self, scope: Vec) { self.turn.capture_scope(scope); @@ -295,11 +393,56 @@ impl ToolRuntime { pub(super) fn cancel_execution(&mut self) { self.commands.cancel_all(); self.live.cancel_all(); + self.transfers.cancel_all(); } - pub(super) fn active_commands( - &self, - ) -> impl Iterator)> { - self.commands.active() + /// Commands and transfers still running for this chat. + pub(super) fn has_active_executions(&mut self) -> bool { + self.commands.has_active() || self.transfers.has_active() + } + pub(super) fn start_transfer( + &mut self, + admission: TransferAdmission, + source: Option, + cx: &gpui::App, + ) -> Result> { + transfer_dispatch::start(&mut self.transfers, admission, source, cx) + } + pub(super) fn transfer_progress( + &mut self, + id: &str, + ) -> Result { + self.transfers.progress(id) + } + pub(super) fn cancel_transfer( + &mut self, + id: &str, + redact: bool, + ) -> Result { + let progress = self.transfers.cancel(id)?; + Ok(presentation::transfer_payload(&progress, redact)) + } + /// Cancels transfers whose endpoint terminals detached or changed session. + fn revoke_replaced_transfers( + &mut self, + resolved: &[( + String, + nocterm_workspace_ports::TerminalEntry, + nocterm_ai::context::TerminalDescriptor, + )], + cx: &gpui::App, + ) { + let current = resolved + .iter() + .map(|(id, entry, _)| { + let session = entry + .capabilities + .state() + .info(cx) + .map(|info| info.expected); + (id.clone(), session) + }) + .collect::>(); + self.transfers.revoke_replaced(¤t); } pub(super) fn start_command( &mut self, @@ -431,6 +574,9 @@ impl ToolRuntime { pub(super) fn approval_permission_cards(&self) -> Vec { self.approvals.permission_cards() } + pub(super) fn approval_tool_tokens(&self) -> Vec { + self.approvals.tool_tokens() + } pub(super) fn approval_tool_cards(&self) -> Vec { self.approvals.tool_cards() } @@ -441,7 +587,10 @@ mod tests { use super::*; #[test] fn running_scope_does_not_follow_future_defaults_and_detachment_revokes_only_that_scope() { - let mut tools = ToolRuntime::default(); + let mut tools = ToolRuntime::new( + Box::::default(), + Arc::new(nocterm_transfers::testing::FakeTransfers::default()), + ); let fixed = Attachment::Group("accepted turn".into()); let future = vec![Attachment::Group("next turn".into())]; tools.capture_scope(vec![fixed.clone()]); @@ -457,7 +606,10 @@ mod tests { } #[test] fn clearing_a_turn_preserves_opaque_terminal_identity_and_usage_accounting() { - let mut tools = ToolRuntime::default(); + let mut tools = ToolRuntime::new( + Box::::default(), + Arc::new(nocterm_transfers::testing::FakeTransfers::default()), + ); let id = tools.terminal_id("original item"); let server = tools.server_id("profile"); tools.account_context(40); diff --git a/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs b/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs index de1b928f..6eda6124 100644 --- a/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs +++ b/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs @@ -1,33 +1,21 @@ //! Structured jobs and their watchdog have one bounded owner; consumers receive snapshots only. -use super::super::execution::jobs; use gpui::Task; -pub(in crate::thread) use jobs::Snapshot as CommandSnapshot; +pub(in crate::thread) use jobs::CommandSnapshot; use nocterm_ai::ToolFailure; +use nocterm_session::command_jobs as jobs; use nocterm_session::{ExecRequest, HostExec}; use std::sync::Arc; -#[derive(Default)] pub(super) struct CommandJobs { - jobs: jobs::Jobs, + jobs: Box, watch: Option>, } pub(in crate::thread) use jobs::CommandStart; impl CommandJobs { + pub(super) fn new(jobs: Box) -> Self { + Self { jobs, watch: None } + } pub(super) fn revoke_replaced(&self, current: &[(String, Option>)]) { - let revoked = self - .jobs - .active() - .filter_map(|(id, executor)| { - let replacement = current - .iter() - .find(|(candidate, _)| candidate == id) - .and_then(|(_, executor)| executor.as_ref()); - (!replacement.is_some_and(|replacement| Arc::ptr_eq(replacement, executor))) - .then_some(id.to_owned()) - }) - .collect::>(); - for id in revoked { - self.jobs.cancel_terminal(&id); - } + self.jobs.revoke_replaced(current); } pub(super) fn start( @@ -37,22 +25,26 @@ impl CommandJobs { request: ExecRequest, deadline: impl std::future::Future + Send + 'static, ) -> Result { - Ok(self.jobs.start(terminal, executor, request, deadline)?) + self.jobs + .start(terminal, executor, request, Box::pin(deadline)) + .map_err(|error| error.to_string().into()) } pub(super) fn snapshot( &self, id: &str, terminal: &str, ) -> Result { - Ok(self.jobs.get(id, terminal)?.snapshot()) + self.jobs + .snapshot(id, terminal) + .map_err(|error| error.to_string().into()) } pub(super) fn cancel(&self, id: &str, terminal: &str) -> Result { - let job = self.jobs.get(id, terminal)?; - job.cancel(jobs::State::Cancelled); - Ok(job.snapshot()) + self.jobs + .cancel(id, terminal) + .map_err(|error| error.to_string().into()) } - pub(super) fn active(&self) -> impl Iterator)> { - self.jobs.active() + pub(super) fn has_active(&self) -> bool { + self.jobs.has_active() } pub(super) fn cancel_all(&mut self) { @@ -76,7 +68,8 @@ mod tests { #[test] fn admission_bounds_original_jobs_and_cancellation_retains_readable_snapshots() { let executor = Arc::new(nocterm_session::testing::FakeExec::default()); - let mut jobs = CommandJobs::default(); + let mut jobs = + CommandJobs::new(Box::::default()); let start = |jobs: &mut CommandJobs| { jobs.start( "terminal".into(), @@ -101,9 +94,9 @@ mod tests { jobs.cancel_all(); assert_eq!( jobs.snapshot(&id, "terminal").unwrap().state(), - jobs::State::Cancelled + jobs::CommandState::Cancelled ); - assert!(jobs.active().next().is_none()); + assert!(!jobs.has_active()); assert!(start(&mut jobs).is_ok()); assert!( executor.requests().is_empty(), diff --git a/crates/agent-chat/src/thread/tool_runtime/dispatch.rs b/crates/agent-chat/src/thread/tool_runtime/dispatch.rs index b3f69a6b..970b98b5 100644 --- a/crates/agent-chat/src/thread/tool_runtime/dispatch.rs +++ b/crates/agent-chat/src/thread/tool_runtime/dispatch.rs @@ -18,9 +18,14 @@ pub(in crate::thread) enum DispatchAction { BridgeCall, ), Execute(TerminalEntry, ExpectedSession, BridgeCall), + /// A file call with its work on the terminal's files. + Files(BridgeCall, super::file_dispatch::FileWork), Read(BridgeCall), Cancel(BridgeCall), Ignore, + Transfer(Box), + ReadTransfer(BridgeCall), + CancelTransfer(BridgeCall), ExecuteAdmitted(BridgeCall, TerminalEntry, ExpectedSession), List(BridgeCall), Open(String, BridgeCall), @@ -93,6 +98,23 @@ impl TerminalDispatch { TerminalCall::ReadCommand(_) => DispatchAction::Read(self.call), TerminalCall::CancelCommand(_) => DispatchAction::Cancel(self.call), TerminalCall::ListTerminals | TerminalCall::OpenTerminal(_) => DispatchAction::Ignore, + TerminalCall::ListFiles(_) + | TerminalCall::ReadFile(_) + | TerminalCall::WriteFile(_) + | TerminalCall::EditFile(_) + | TerminalCall::MakeDirectory(_) + | TerminalCall::DeletePath(_) + | TerminalCall::MovePath(_) => { + super::file_dispatch::prepare(self.call, &self.entry, self.expected, redact, cx) + } + // Admission routes transfers before terminal dispatch. + TerminalCall::TransferFiles(_) + | TerminalCall::ReadTransfer(_) + | TerminalCall::CancelTransfer(_) => DispatchAction::Respond( + self.call, + Err("This call does not act on a terminal.".into()), + 0, + ), } } } @@ -125,6 +147,10 @@ impl ExecutionAdmission { TerminalCall::OpenTerminal(request) => { DispatchAction::Open(request.server_id.clone(), bound.into_call()) } + TerminalCall::ReadTransfer(_) => DispatchAction::ReadTransfer(bound.into_call()), + TerminalCall::CancelTransfer(_) => { + DispatchAction::CancelTransfer(bound.into_call()) + } _ => return AdmissionRoute::Resolve(Box::new(bound)), } }; @@ -178,6 +204,9 @@ impl ExecutionAdmission { { return DispatchAction::Reconnect(bound, entry, expected); } + if matches!(bound.call().call, TerminalCall::TransferFiles(_)) { + return super::transfer_dispatch::TransferAdmission::admit(bound, entry, expected); + } // Native read/input execution is a separate admitted step interpreted by the coordinator. DispatchAction::ExecuteAdmitted(bound.into_call(), entry, expected) } diff --git a/crates/agent-chat/src/thread/tool_runtime/file_dispatch.rs b/crates/agent-chat/src/thread/tool_runtime/file_dispatch.rs new file mode 100644 index 00000000..306a5f6a --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/file_dispatch.rs @@ -0,0 +1,93 @@ +//! The file tools of a chat: which attached terminals take them, and one call's work on the +//! filesystem captured under the call's `ExpectedSession`. +//! +//! A terminal takes the file tools when it serves a filesystem of its own but runs no programs: +//! the agent cannot reach its files with `exec_command`, so the chat offers these tools while +//! such a terminal is attached, and they act on nothing else. +use std::sync::Arc; + +use futures::future::BoxFuture; +use gpui::{App, Context}; +use nocterm_ai::{BridgeCall, ToolFailure}; +use nocterm_session::RemoteFs; +use nocterm_workspace_ports::{ + ExpectedSession, TerminalCapability, TerminalEntry, TerminalError, TerminalFilesystem, +}; + +use super::dispatch::DispatchAction; +use crate::thread::{AgentThread, session_driver::OperationGuard}; + +/// One file call's work, answered when it ends. +pub(in crate::thread) type FileWork = BoxFuture<'static, Result>; + +/// Whether the agent reaches this terminal's files only through the file tools. +pub(in crate::thread) fn takes_file_tools( + entry: &TerminalEntry, + expected: ExpectedSession, + cx: &App, +) -> bool { + remote(entry, expected, cx).is_ok() + && entry.capabilities.execution().is_none_or(|execution| { + matches!( + execution.executor(expected, cx), + Err(TerminalError::Unsupported(_)) + ) + }) +} + +fn remote( + entry: &TerminalEntry, + expected: ExpectedSession, + cx: &App, +) -> Result, TerminalError> { + let files = entry + .capabilities + .files() + .ok_or(TerminalError::Unsupported(TerminalCapability::Files))?; + match files.filesystem(expected, cx)? { + TerminalFilesystem::Remote { fs, .. } => Ok(fs), + TerminalFilesystem::Local => Err(TerminalError::Unsupported(TerminalCapability::Files)), + } +} + +/// The work of the file call `call` on its terminal, or why the terminal does not take it. +pub(in crate::thread) fn prepare( + call: BridgeCall, + entry: &TerminalEntry, + expected: ExpectedSession, + redact: bool, + cx: &App, +) -> DispatchAction { + if !takes_file_tools(entry, expected, cx) { + return DispatchAction::Respond( + call, + Err( + "The file tools act only on terminals with file_tools: true. Use exec_command or transfer_files on this terminal." + .into(), + ), + 0, + ); + } + match remote(entry, expected, cx) { + Ok(fs) => { + let work = Box::pin(nocterm_ai::files::run(call.call.clone(), fs, redact)); + DispatchAction::Files(call, work) + } + Err(error) => DispatchAction::Respond(call, Err(error.to_string().into()), 0), + } +} + +/// Answers the file call `call` once its work ends, holding the chat's operation until then. +pub(in crate::thread) fn complete( + call: BridgeCall, + work: FileWork, + guard: OperationGuard, + cx: &mut Context, +) { + cx.spawn(async move |this, cx| { + let _guard = guard; + let result = work.await; + let _ = this.update(cx, |this, cx| this.on_tool_completion(call, result, cx)); + }) + .detach(); +} diff --git a/crates/agent-chat/src/thread/tool_runtime/presentation.rs b/crates/agent-chat/src/thread/tool_runtime/presentation.rs index 110c5baf..c4111f59 100644 --- a/crates/agent-chat/src/thread/tool_runtime/presentation.rs +++ b/crates/agent-chat/src/thread/tool_runtime/presentation.rs @@ -1,18 +1,29 @@ //! Terminal outcome framing receives an explicit redaction policy. use super::CommandSnapshot; +use nocterm_ai::tools::output::{CommandReport, TerminalReport}; pub(in crate::thread) fn text_payload( tail: nocterm_workspace_ports::TerminalText, redact: bool, ) -> (serde_json::Value, usize) { + let report = terminal_report(tail, redact); + let bytes = report.text.len(); + (serde_json::json!(report), bytes) +} +pub(in crate::thread) fn terminal_report( + tail: nocterm_workspace_ports::TerminalText, + redact: bool, +) -> TerminalReport { let text = if redact { nocterm_ai::redact::redact(&tail.text) } else { tail.text }; - let bytes = text.len(); - ( - serde_json::json!({"text":text,"first_line":tail.first_line,"next_line":tail.next_line,"cursor_semantics":"inclusive snapshot; replace overlapping lines","truncated":tail.truncated,"alt_screen":tail.alt_screen}), - bytes, + TerminalReport::new( + text, + tail.first_line, + tail.next_line, + tail.truncated, + tail.alt_screen, ) } pub(in crate::thread) fn command_payload( @@ -20,19 +31,12 @@ pub(in crate::thread) fn command_payload( state: CommandSnapshot, redact: bool, ) -> serde_json::Value { - let text = |text: String| { - if redact { - nocterm_ai::redact::redact(&text) - } else { - text - } - }; - serde_json::json!({ - "command_id": id, "state": state.state().as_str(), - "stdout": text(String::from_utf8_lossy(state.stdout()).into_owned()), - "stderr": text(state.exit().map(|exit| exit.stderr.clone()).unwrap_or_default()), - "exit_status": state.exit().and_then(|exit| exit.status), - "total_bytes": state.total_bytes(), "truncated": state.total_bytes() > state.stdout().len() as u64, - "error": state.error().map(nocterm_ai::redact::redact), - }) + serde_json::json!(CommandReport::new(id, &state, redact)) +} +/// The agent-facing report of one transfer. +pub(in crate::thread) fn transfer_payload( + progress: &nocterm_transfers::Progress, + redact: bool, +) -> serde_json::Value { + nocterm_ai::tools::transfer::report(progress, redact) } diff --git a/crates/agent-chat/src/thread/tool_runtime/source_execution.rs b/crates/agent-chat/src/thread/tool_runtime/source_execution.rs new file mode 100644 index 00000000..74426589 --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/source_execution.rs @@ -0,0 +1,68 @@ +//! A native runner retains its original account lease through every scheduler poll. +use futures::{FutureExt as _, future::BoxFuture}; +use nocterm_ai::{ExecCommand, ToolFailure, local_environment::LocalEnvironmentAccess}; +use nocterm_workspace_ports::{ExpectedSession, TerminalCapability, TerminalEntry, TerminalError}; +use std::{future::poll_fn, task::Poll, time::Duration}; + +pub(in crate::thread) struct SourceExecution { + source: LocalEnvironmentAccess, +} +impl SourceExecution { + pub(in crate::thread) fn capture( + runtime: &nocterm_agent_runtime::RuntimeHandle, + ) -> Result { + Ok(Self { + source: runtime + .captured_source() + .map_err(|error| ToolFailure::from(error.to_string()))?, + }) + } + pub(in crate::thread) fn start( + self, + entry: TerminalEntry, + expected: ExpectedSession, + request: &ExecCommand, + tools: &mut super::ToolRuntime, + cx: &mut gpui::App, + ) -> Result<(String, BoxFuture<'static, ()>), ToolFailure> { + self.source + .check_current() + .map_err(|error| ToolFailure::from(error.to_string()))?; + let executor = entry + .capabilities + .execution() + .ok_or(TerminalError::Unsupported( + TerminalCapability::StructuredExecution, + )) + .and_then(|port| port.executor(expected, cx)) + .map_err(|error| ToolFailure::from(error.to_string()))?; + let program = nocterm_session::ExecRequest { + program: request.program.clone(), + args: request.args.clone(), + stdin: request + .stdin + .as_ref() + .map(|stdin| stdin.as_bytes().to_vec()), + }; + let timer = cx + .background_executor() + .timer(Duration::from_millis(request.timeout_ms.unwrap_or(30_000))); + let (id, run) = tools + .start_command(request.terminal_id.clone(), executor, program, timer)? + .into_run(); + Ok((id, self.guard(run))) + } + /// Each native future poll is admitted against the original owner. Already + /// admitted transport work is cancelled by dropping the runner; no new poll + /// can consume credentials from a replacement account. The chat watchdog + /// records cancellation of the retained command separately. + fn guard(self, mut run: BoxFuture<'static, ()>) -> BoxFuture<'static, ()> { + poll_fn(move |cx| { + if self.source.check_current().is_err() { + return Poll::Ready(()); + } + run.as_mut().poll(cx) + }) + .boxed() + } +} diff --git a/crates/agent-chat/src/thread/tool_runtime/transfer_dispatch.rs b/crates/agent-chat/src/thread/tool_runtime/transfer_dispatch.rs new file mode 100644 index 00000000..cd50d683 --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/transfer_dispatch.rs @@ -0,0 +1,173 @@ +//! A transfer call is admitted against two terminals' exact sessions; their filesystems +//! are captured once, under those sessions, into the request the chat submits. +use super::transfers::{ChatTransfers, TransferBinding}; +use crate::thread::bound_call::BoundToolCall; +use crate::thread::tool_runtime::dispatch::DispatchAction; +use gpui::App; +use nocterm_ai::{BridgeCall, TerminalCall, ToolFailure}; +use nocterm_transfers::{CollisionPolicy, Request, TransferEndpoint, TransferId}; +use nocterm_workspace_ports::{ + ExpectedSession, TerminalCapability, TerminalEntry, TerminalError, TerminalFilesystem, +}; + +const DEFAULT_YIELD_MS: u64 = 1000; + +/// An approved `transfer_files` call whose destination is resolved; the source is not yet. +pub(in crate::thread) struct TransferAdmission { + call: BridgeCall, + destination: TerminalEntry, + expected: ExpectedSession, + source_expected: ExpectedSession, +} +/// The submitted transfer and the call waiting for its report. +pub(in crate::thread) struct StartedTransfer { + call: BridgeCall, + id: TransferId, + yield_ms: u64, +} +impl StartedTransfer { + pub(in crate::thread) fn into_parts(self) -> (BridgeCall, TransferId, u64) { + (self.call, self.id, self.yield_ms) + } +} +/// A transfer call that could not start, with the failure to answer it with. Boxed so the +/// `Err` side of the start path stays small. +pub(in crate::thread) struct RejectedTransfer { + call: BridgeCall, + error: ToolFailure, +} +impl RejectedTransfer { + fn new(call: BridgeCall, error: ToolFailure) -> Box { + Box::new(Self { call, error }) + } + pub(in crate::thread) fn into_parts(self) -> (BridgeCall, ToolFailure) { + (self.call, self.error) + } +} +struct PreparedTransfer { + call: BridgeCall, + request: Request, + binding: TransferBinding, + yield_ms: u64, +} + +impl TransferAdmission { + /// A call without a source session was approved against a terminal that is gone. + pub(in crate::thread) fn admit( + bound: BoundToolCall, + destination: TerminalEntry, + expected: ExpectedSession, + ) -> DispatchAction { + let Some(source_expected) = bound.source() else { + let stale = TerminalError::StaleSession.to_string().into(); + return DispatchAction::Respond(bound.into_call(), Err(stale), 0); + }; + DispatchAction::Transfer(Box::new(Self { + call: bound.into_call(), + destination, + expected, + source_expected, + })) + } + /// Whether the call reads from the terminal with this id. + pub(in crate::thread) fn reads_from(&self, id: &str) -> bool { + self.call.call.source_terminal_id() == Some(id) + } + /// Rechecks both sessions, captures both filesystems and builds the request. + fn prepare( + self, + source: Option, + cx: &App, + ) -> Result> { + match self.endpoints(source, cx) { + Ok((request, binding, yield_ms)) => Ok(PreparedTransfer { + call: self.call, + request, + binding, + yield_ms, + }), + Err(error) => Err(RejectedTransfer::new(self.call, error)), + } + } + fn endpoints( + &self, + source: Option, + cx: &App, + ) -> Result<(Request, TransferBinding, u64), ToolFailure> { + let TerminalCall::TransferFiles(call) = &self.call.call else { + return Err("This is not a file transfer.".into()); + }; + let source = source.ok_or_else(|| { + crate::thread::tools::unreachable( + "Terminal is not attached or was closed. Call list_terminals for the current ones.", + ) + })?; + let from = endpoint(&source, self.source_expected, cx)?; + let to = endpoint(&self.destination, self.expected, cx)?; + let collisions = if call.overwrite { + CollisionPolicy::Replace + } else { + CollisionPolicy::Skip + }; + let request = Request::between( + from, + call.sources.clone(), + to, + call.destination.clone(), + collisions, + ) + .map_err(|error| ToolFailure::from(error.to_string()))?; + let binding = TransferBinding::new( + (call.destination_terminal_id.clone(), self.expected), + (call.source_terminal_id.clone(), self.source_expected), + ); + Ok((request, binding, call.yield_ms.unwrap_or(DEFAULT_YIELD_MS))) + } +} +impl PreparedTransfer { + fn submit(self, book: &mut ChatTransfers) -> Result> { + match book.start(self.request, self.binding) { + Ok(id) => Ok(StartedTransfer { + call: self.call, + id, + yield_ms: self.yield_ms, + }), + Err(error) => Err(RejectedTransfer::new(self.call, error)), + } + } +} +/// Submits an admitted transfer to the chat's book. +pub(super) fn start( + book: &mut ChatTransfers, + admission: TransferAdmission, + source: Option, + cx: &App, +) -> Result> { + admission.prepare(source, cx)?.submit(book) +} + +fn endpoint( + entry: &TerminalEntry, + expected: ExpectedSession, + cx: &App, +) -> Result { + let current = entry + .capabilities + .state() + .info(cx) + .is_some_and(|info| info.expected == expected); + let files = if current { + entry + .capabilities + .files() + .ok_or(TerminalError::Unsupported(TerminalCapability::Files)) + .and_then(|port| port.filesystem(expected, cx)) + } else { + Err(TerminalError::StaleSession) + }; + match files { + Ok(TerminalFilesystem::Local) => Ok(TransferEndpoint::Local), + Ok(TerminalFilesystem::Remote { host, fs }) => Ok(TransferEndpoint::Remote { host, fs }), + Err(error) => Err(error.to_string().into()), + } +} diff --git a/crates/agent-chat/src/thread/tool_runtime/transfers.rs b/crates/agent-chat/src/thread/tool_runtime/transfers.rs new file mode 100644 index 00000000..83f12c8f --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/transfers.rs @@ -0,0 +1,174 @@ +//! The chat's file transfers: it reads and cancels only ids it submitted, and a transfer +//! ends when either endpoint terminal detaches or its session is replaced. +use nocterm_ai::ToolFailure; +use nocterm_transfers::{ + OwnedLimits, OwnedTransferError, OwnedTransfers, Progress, Request, TransferId, + TransferRequests, +}; +use nocterm_workspace_ports::ExpectedSession; +use std::sync::Arc; + +/// The terminals a transfer was admitted for and the sessions it captured from them. +pub(super) struct TransferBinding { + destination: (String, ExpectedSession), + source: (String, ExpectedSession), +} +impl TransferBinding { + pub(super) fn new( + destination: (String, ExpectedSession), + source: (String, ExpectedSession), + ) -> Self { + Self { + destination, + source, + } + } + fn holds(&self, current: &[(String, Option)]) -> bool { + [&self.destination, &self.source] + .into_iter() + .all(|(id, expected)| { + current + .iter() + .any(|(attached, session)| attached == id && *session == Some(*expected)) + }) + } +} + +pub(super) struct ChatTransfers { + book: OwnedTransfers, +} +impl ChatTransfers { + pub(super) fn new(port: Arc) -> Self { + Self { + book: OwnedTransfers::new(port, OwnedLimits::default()), + } + } + pub(super) fn start( + &mut self, + request: Request, + binding: TransferBinding, + ) -> Result { + self.book + .submit(request, binding) + .map_err(|error| error.to_string().into()) + } + pub(super) fn progress(&mut self, id: &str) -> Result { + let id = owned_id(id)?; + self.book + .progress(id) + .map_err(|error| error.to_string().into()) + } + pub(super) fn cancel(&mut self, id: &str) -> Result { + let id = owned_id(id)?; + self.book + .cancel(id) + .map_err(|error| error.to_string().into()) + } + /// `current`: every attached terminal id with its present session. + pub(super) fn revoke_replaced(&mut self, current: &[(String, Option)]) { + self.book.retain_active(|binding| binding.holds(current)); + } + pub(super) fn cancel_all(&mut self) { + self.book.cancel_all(); + } + pub(super) fn has_active(&mut self) -> bool { + self.book.has_active() + } +} +/// A malformed id cannot be one this chat submitted. +fn owned_id(text: &str) -> Result { + text.parse() + .map_err(|_| OwnedTransferError::NotOwned.to_string().into()) +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_session::{HostAddress, Target, testing::InMemoryFs}; + use nocterm_transfers::{ + CollisionPolicy, TransferEndpoint, TransferEvent, TransferState, testing::FakeTransfers, + }; + + fn host(name: &str) -> HostAddress { + HostAddress::direct(Target { + host: name.into(), + port: 22, + user: "user".into(), + }) + } + fn relay() -> Request { + let endpoint = |name: &str| TransferEndpoint::Remote { + host: host(name), + fs: Arc::new(InMemoryFs::default()), + }; + Request::between( + endpoint("source"), + vec!["/srv/a".into()], + endpoint("destination"), + "/srv/in".into(), + CollisionPolicy::Skip, + ) + .unwrap() + } + fn session(item: u64, epoch: u64) -> ExpectedSession { + ExpectedSession::new(gpui::EntityId::from(item), epoch) + } + fn binding(epoch: u64) -> TransferBinding { + TransferBinding::new( + ("destination".into(), session(1, epoch)), + ("source".into(), session(2, 0)), + ) + } + fn attached(destination_epoch: u64) -> Vec<(String, Option)> { + vec![ + ("destination".into(), Some(session(1, destination_epoch))), + ("source".into(), Some(session(2, 0))), + ] + } + + #[test] + fn another_chats_or_malformed_ids_are_not_owned() { + let fake = Arc::new(FakeTransfers::default()); + let mut other = ChatTransfers::new(fake.clone()); + let foreign = other.start(relay(), binding(0)).unwrap(); + let mut mine = ChatTransfers::new(fake); + let refused = mine.progress(&foreign.to_string()).unwrap_err(); + assert_eq!( + refused.to_string(), + OwnedTransferError::NotOwned.to_string() + ); + assert!(mine.cancel(&foreign.to_string()).is_err()); + assert!(mine.progress("not an id").is_err()); + assert!(other.progress(&foreign.to_string()).is_ok()); + } + + #[test] + fn a_replaced_or_detached_endpoint_cancels_only_its_transfer() { + let fake = Arc::new(FakeTransfers::default()); + let mut transfers = ChatTransfers::new(fake.clone()); + let kept = transfers.start(relay(), binding(0)).unwrap(); + let replaced = transfers.start(relay(), binding(1)).unwrap(); + transfers.revoke_replaced(&attached(0)); + let state = |transfers: &mut ChatTransfers, id: TransferId| { + transfers.progress(&id.to_string()).unwrap().state + }; + assert_eq!(state(&mut transfers, kept), TransferState::Queued); + assert_eq!(state(&mut transfers, replaced), TransferState::Cancelled); + transfers.revoke_replaced(&attached(0)[..1]); + assert_eq!(state(&mut transfers, kept), TransferState::Cancelled); + assert!(!transfers.has_active()); + } + + #[test] + fn finished_transfers_keep_their_final_report_after_the_runtime_forgets_them() { + let fake = Arc::new(FakeTransfers::default()); + let mut transfers = ChatTransfers::new(fake.clone()); + let id = transfers.start(relay(), binding(0)).unwrap(); + fake.advance(id, TransferEvent::DiscoveryComplete).unwrap(); + fake.advance(id, TransferEvent::WorkersDrained).unwrap(); + assert!(!transfers.has_active()); + fake.forget(id); + let report = transfers.progress(&id.to_string()).unwrap(); + assert_eq!(report.state, TransferState::Completed); + } +} diff --git a/crates/agent-chat/src/thread/tool_runtime/turn_context.rs b/crates/agent-chat/src/thread/tool_runtime/turn_context.rs index d341f61c..65bf1243 100644 --- a/crates/agent-chat/src/thread/tool_runtime/turn_context.rs +++ b/crates/agent-chat/src/thread/tool_runtime/turn_context.rs @@ -149,6 +149,7 @@ impl TurnContext { connection, cwd: info.cwd.map(|path| path.to_string_lossy().into_owned()), status: format!("{:?}", info.status), + file_tools: super::file_dispatch::takes_file_tools(&entry, info.expected, cx), }; Some((id, entry, descriptor)) }) @@ -231,23 +232,34 @@ impl TurnContext { }) .unwrap_or_else(|| "Unavailable server".into()); } - self.resolved(input, defaults, cx) - .into_iter() - .find(|(id, _, _)| Some(id.as_str()) == call.terminal_id()) - .map(|(_, entry, descriptor)| { - format!( - "{}{}", - entry.title, - descriptor - .connection - .as_ref() - .map(|connection| format!( - " · {}@{}:{}", - connection.user, connection.host, connection.port - )) - .unwrap_or_default() - ) - }) - .unwrap_or_else(|| "Unavailable terminal".into()) + let resolved = self.resolved(input, defaults, cx); + let describe = |wanted: Option<&str>| { + resolved + .iter() + .find(|(id, _, _)| Some(id.as_str()) == wanted) + .map(|(_, entry, descriptor)| { + format!( + "{}{}", + entry.title, + descriptor + .connection + .as_ref() + .map(|connection| format!( + " · {}@{}:{}", + connection.user, connection.host, connection.port + )) + .unwrap_or_default() + ) + }) + .unwrap_or_else(|| "Unavailable terminal".into()) + }; + match call.source_terminal_id() { + Some(source) => format!( + "{} → {}", + describe(Some(source)), + describe(call.terminal_id()) + ), + None => describe(call.terminal_id()), + } } } diff --git a/crates/agent-directory/Cargo.toml b/crates/agent-directory/Cargo.toml index f292161f..bcfdf4d3 100644 --- a/crates/agent-directory/Cargo.toml +++ b/crates/agent-directory/Cargo.toml @@ -15,6 +15,7 @@ nocterm-ai.workspace = true nocterm-store.workspace = true [dev-dependencies] +nocterm-hubs-model.workspace = true gpui = { workspace = true, features = ["test-support"] } [lints] diff --git a/crates/agent-directory/src/lib.rs b/crates/agent-directory/src/lib.rs index ad5eb1b9..72a72a29 100644 --- a/crates/agent-directory/src/lib.rs +++ b/crates/agent-directory/src/lib.rs @@ -1,31 +1,48 @@ //! Which agents the user can start now, and from which source. //! -//! Owns: `AgentsHandle`, the one resolution of the injected `AgentCatalog` -//! and the `ai` settings into `AvailableAgents` that every consumer reads. +//! Owns: `AgentsHandle`, the one resolution of the injected `AgentCatalog`, the `ai` settings +//! and the agents of the signed-in hubs ([`HubAgentLaunches`]) into `AvailableAgents` that +//! every consumer reads. //! Does not own: agent processes (`nocterm-agent-runtime`), profile and //! resolution rules (`nocterm-ai`), the built-in profiles (`nocterm-agent-providers`) or views. -//! Ports used: SettingsHandle. Ports provided: AgentsHandle. +//! Ports used: SettingsHandle, HubAgentLaunches. Ports provided: AgentsHandle. //! Settings section: reads `ai`. Globals: none. //! Invariants: consumers see the agents of the current settings revision; //! nothing outside this crate resolves settings into agents. use gpui::App; use nocterm_ai::{ AgentCatalog, AiSettings, AvailableAgents, - agents::{AgentProfile, AvailableAgent}, + agents::{AgentLaunch, AgentProfile, AvailableAgent}, }; use nocterm_store::SettingsHandle; +use std::rc::Rc; + +/// The agents of the hubs this device is signed in to, as they are now. +pub type HubAgentLaunches = Rc Vec>; /// Explicit access to the available agents, retained by the application. #[derive(Clone)] pub struct AgentsHandle { catalog: AgentCatalog, settings: SettingsHandle, + hubs: Option, } impl AgentsHandle { pub fn new(catalog: AgentCatalog, settings: SettingsHandle, cx: &mut App) -> Self { settings.register::(cx); - Self { catalog, settings } + Self { + catalog, + settings, + hubs: None, + } + } + + /// These agents and the agents of the signed-in hubs. + #[must_use] + pub fn with_hub_agents(mut self, hubs: HubAgentLaunches) -> Self { + self.hubs = Some(hubs); + self } /// The profiles nocterm ships, whether or not settings enable them. @@ -35,7 +52,12 @@ impl AgentsHandle { /// The agents the current settings allow, with skipped configurations. pub fn available(&self, cx: &App) -> AvailableAgents { - AvailableAgents::resolve(&self.catalog, self.settings.setting::(cx)) + let settings = self.settings.setting::(cx); + let agents = AvailableAgents::resolve(&self.catalog, settings); + match &self.hubs { + Some(hubs) if settings.enabled => agents.with_hub_agents(hubs(cx)), + _ => agents, + } } pub fn get(&self, id: &str, cx: &App) -> Option { @@ -44,7 +66,13 @@ impl AgentsHandle { /// The profile of agent `id`: its own when available, else the catalog's. pub fn profile(&self, id: &str, cx: &App) -> Option { - match self.available(cx).get(id) { + self.profile_in(&self.available(cx), id) + } + + /// The profile of agent `id` among agents already resolved by [`Self::available`], + /// for callers that look up many agents at once. + pub fn profile_in(&self, available: &AvailableAgents, id: &str) -> Option { + match available.get(id) { Some(agent) => agent.profile().copied(), None => self.catalog.get(id).copied(), } diff --git a/crates/agent-directory/src/tests.rs b/crates/agent-directory/src/tests.rs index a2130d57..c8eb4e06 100644 --- a/crates/agent-directory/src/tests.rs +++ b/crates/agent-directory/src/tests.rs @@ -58,3 +58,16 @@ async fn available_agents_follow_the_current_settings(cx: &mut TestAppContext) { assert_eq!(agents.catalog().iter().count(), 1); }); } + +#[gpui::test] +async fn the_agents_of_signed_in_hubs_are_available_beside_the_local_ones(cx: &mut TestAppContext) { + use nocterm_hubs_model::{HubAddress, HubId}; + let hub = HubId::from(HubAddress::parse("hub.example:7443").unwrap()); + let launch = nocterm_ai::agents::AgentLaunch::hub_agent(hub, "a1".into(), "Ops".into()); + let id = launch.id.clone(); + let agents = handle(cx).with_hub_agents(std::rc::Rc::new(move |_| vec![launch.clone()])); + cx.update(|cx| { + assert_eq!(agents.get(&id, cx).unwrap().source(), AgentSource::Hub); + assert!(agents.get("alpha", cx).is_some()); + }); +} diff --git a/crates/agent-providers/src/claude.rs b/crates/agent-providers/src/claude.rs index 8a623ff4..6e5589a0 100644 --- a/crates/agent-providers/src/claude.rs +++ b/crates/agent-providers/src/claude.rs @@ -1,7 +1,5 @@ //! Claude Code through Zed's `claude-agent-acp` adapter. -use nocterm_ai::agents::{ - AccountUsage, AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile, -}; +use nocterm_ai::agents::{AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; pub(crate) const PROFILE: AgentProfile = AgentProfile::new( "claude", @@ -19,13 +17,10 @@ pub(crate) const PROFILE: AgentProfile = AgentProfile::new( .recognized_by(&["claude-agent-acp"]) .writes_home(&[".claude", ".claude.json", ".config/claude"]) .with_usage( - UsageProfile::new( - UsageMeta::RateLimitRecord { - key: "_claude/rateLimit", - }, - AccountUsage::OAuthAccount, - ) - .with_empty_hint("Claude plan limits load from your Claude Code sign-in."), + UsageProfile::new(UsageMeta::RateLimitRecord { + key: "_claude/rateLimit", + }) + .with_empty_hint("Claude has not reported plan limits."), ) .with_mark( AgentMark::new( diff --git a/crates/agent-providers/src/codex.rs b/crates/agent-providers/src/codex.rs index d67c2873..abde7d92 100644 --- a/crates/agent-providers/src/codex.rs +++ b/crates/agent-providers/src/codex.rs @@ -1,7 +1,5 @@ //! OpenAI Codex through the `codex-acp` adapter. -use nocterm_ai::agents::{ - AccountUsage, AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile, -}; +use nocterm_ai::agents::{AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; pub(crate) const PROFILE: AgentProfile = AgentProfile::new( "codex", @@ -15,8 +13,7 @@ pub(crate) const PROFILE: AgentProfile = AgentProfile::new( .recognized_by(&["codex"]) .writes_home(&[".codex"]) .with_usage( - UsageProfile::new(UsageMeta::None, AccountUsage::SessionLog) - .with_empty_hint("Codex reports plan limits after a reply."), + UsageProfile::new(UsageMeta::None).with_empty_hint("Codex has not reported plan limits."), ) .with_mark(AgentMark::new( "icons/agents/codex.svg", diff --git a/crates/agent-providers/src/lib.rs b/crates/agent-providers/src/lib.rs index e8edb566..87456c80 100644 --- a/crates/agent-providers/src/lib.rs +++ b/crates/agent-providers/src/lib.rs @@ -3,7 +3,7 @@ //! Owns: one module per built-in agent returning its `AgentProfile` (pinned //! launch, home state, plan-limit reporting, brand mark) and `catalog()`. //! Does not own: dialect and resolution rules (`nocterm-ai`), launching -//! (`nocterm-acp`), native usage reads (`nocterm-agent-usage`) or rendering. +//! (`nocterm-acp`), provider credentials or rendering. //! Ports used: none. Ports provided: none; the application injects `catalog()`. //! Settings section: none. Globals: none. //! Invariants: ids are unique and valid; launches are pinned; every profile diff --git a/crates/agent-runtime/Cargo.toml b/crates/agent-runtime/Cargo.toml index d297a77b..1c89db62 100644 --- a/crates/agent-runtime/Cargo.toml +++ b/crates/agent-runtime/Cargo.toml @@ -13,7 +13,10 @@ layer = "service" test-support = [] [dependencies] +nocterm-hubs-model.workspace = true nocterm-ai.workspace = true +nocterm-session.workspace = true +nocterm-transfers.workspace = true nocterm-agent-directory.workspace = true nocterm-core.workspace = true nocterm-store.workspace = true @@ -24,7 +27,10 @@ tracing.workspace = true thiserror.workspace = true [dev-dependencies] +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } nocterm-agent-providers.workspace = true +nocterm-transfers = { workspace = true, features = ["test-support"] } serde_json.workspace = true gpui = { workspace = true, features = ["test-support"] } tempfile.workspace = true diff --git a/crates/agent-runtime/src/bridge.rs b/crates/agent-runtime/src/bridge.rs index e5ac7fb7..d2b008b7 100644 --- a/crates/agent-runtime/src/bridge.rs +++ b/crates/agent-runtime/src/bridge.rs @@ -61,7 +61,13 @@ impl BridgeRegistry { _ => unreachable!(), }; let client = runtime - .read_with(cx, |runtime, _| runtime.bridge.target(registration)) + .read_with(cx, |runtime, cx| { + runtime.bridge.target(registration).filter(|client| { + client.state(cx).is_some_and(|state| { + runtime.chats.source_current(state.origin().key()) + }) + }) + }) .ok() .flatten(); if let Some(client) = client { diff --git a/crates/agent-runtime/src/catalog.rs b/crates/agent-runtime/src/catalog.rs index eb5aa17a..db338793 100644 --- a/crates/agent-runtime/src/catalog.rs +++ b/crates/agent-runtime/src/catalog.rs @@ -1,18 +1,40 @@ //! Installation-wide catalog payload reservations and immutable scoped snapshots. use super::{AgentStorage, HistoryReader, Runtime}; +use crate::history_admission::{HistoryAdmissionError, HistoryAdmissionHandle, HistoryBytesPermit}; use gpui::{Context, Task}; use nocterm_ai::{ execution_origin::ChatKey, history::{ChatSummary, ContextId, HistoryGate, SavedChat}, - storage::{AgentStorageError, ChatRepository, require_chat_scope}, + local_environment::ChatScope, + storage::{AgentStorageError, ChatRepository, require_local_scope}, }; -use std::sync::{Arc, Weak}; -const MAX_CATALOG_SNAPSHOTS: usize = 32; +use std::sync::Arc; +mod allocation; +pub(crate) use allocation::CatalogAllocation; +#[cfg(test)] +pub(crate) use allocation::WORK_BYTES as CATALOG_WORK_BYTES; +mod prepared; +mod reclamation; +use prepared::PreparedRecords; -#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +#[derive(Clone, Debug, thiserror::Error)] pub enum CatalogLoadError { - #[error("All 32 installation catalog payload slots are retained or loading.")] + #[error("The original account source is unavailable: {0}")] + AccountSource(#[from] nocterm_hubs_model::source_identity::LocalAccountSourceError), + #[error("All normal installation history payload slots are retained or loading.")] Capacity, + #[error( + "Catalog metadata needs {incoming} bytes with {retained} retained; installation limit is {limit}." + )] + ResidentCapacity { + retained: usize, + incoming: usize, + limit: usize, + }, + #[error("Catalog metadata admission failed: {0}")] + Admission(#[source] Arc), + #[error(transparent)] + Storage(#[from] AgentStorageError), #[error("Agent history is shutting down.")] Closed, #[error("Agent history load generation is exhausted.")] @@ -31,14 +53,18 @@ impl PreparedHistory { let mut ids = std::collections::HashSet::new(); let mut prepared = Vec::with_capacity(chats.len()); for chat in chats { - require_chat_scope(ContextId::General, chat.header.scope)?; + require_local_scope(&ContextId::General.into(), &chat.header.chat_scope())?; if !ids.insert(chat.header.id.clone()) { return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); } - let chat = nocterm_ai::history::prepare(chat)?; + let chat = nocterm_ai::history::prepare_owned(chat)?; resident = resident.saturating_add(chat.resident_bytes()); - if resident > nocterm_ai::history::MAX_RESIDENT_BYTES { - return Err(nocterm_ai::history::HistoryCodecError::StructuralLimit.into()); + if resident > crate::history_admission::MAX_NORMAL_HISTORY_BYTES { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: resident, + limit: crate::history_admission::MAX_NORMAL_HISTORY_BYTES, + }); } prepared.push(chat); } @@ -54,14 +80,18 @@ pub struct CatalogSnapshot { payload: Arc, } struct CatalogPayload { - scope: ContextId, + scope: ChatScope, generation: u64, rows: Vec, error: Option, + allocation: CatalogAllocation, } impl CatalogSnapshot { pub fn scope(&self) -> ContextId { - self.payload.scope + self.payload.scope.context() + } + pub fn chat_scope(&self) -> &ChatScope { + &self.payload.scope } pub fn generation(&self) -> u64 { self.payload.generation @@ -92,6 +122,9 @@ impl CatalogRow { pub fn generation(&self) -> u64 { self.snapshot.generation() } + pub fn chat_scope(&self) -> &ChatScope { + self.snapshot.chat_scope() + } pub fn summary(&self) -> &ChatSummary { // Only CatalogSnapshot::row constructs this immutable validated index. &self.snapshot.rows()[self.index] @@ -104,48 +137,57 @@ struct ScopeCatalog { loading: Option>, } pub(super) struct Catalog { - saved: Option>, - scopes: std::collections::HashMap, + saved: PreparedRecords, + scopes: std::collections::HashMap, gate: HistoryGate, repository: Arc, closing: bool, - payloads: Vec>, + admission: HistoryAdmissionHandle, sequence: u64, + _reclamation: crate::history_admission::HistoryRegistration, } impl Catalog { - pub(super) fn new(storage: &mut AgentStorage, cx: &mut Context) -> Self { + pub(super) fn new( + storage: &mut AgentStorage, + admission: HistoryAdmissionHandle, + prepared: Option, + cx: &mut Context, + ) -> Self { + let reclamation = admission + .register(std::rc::Rc::new(reclamation::CatalogReclamation::new( + cx.weak_entity(), + ))) + .expect("a new installation admission is open"); let mut catalog = Self { - saved: storage.take_history().map(|history| history.0), + saved: PreparedRecords::new( + storage.take_history().map(|history| history.0), + &admission, + prepared, + cx.entity_id(), + ), scopes: Default::default(), gate: HistoryGate::default(), repository: storage.chats(), closing: false, - payloads: Vec::new(), + admission, sequence: 0, + _reclamation: reclamation, }; - if let Some(chats) = &catalog.saved { - let payload = Arc::new(CatalogPayload { - scope: ContextId::General, - generation: 1, - rows: chats.iter().map(ChatSummary::from_chat).collect(), - error: None, - }); - catalog.payloads.push(Arc::downgrade(&payload)); - catalog.sequence = 1; - catalog.scopes.insert( - ContextId::General, - ScopeCatalog { - snapshot: Some(CatalogSnapshot { payload }), - generation: 1, - loading: None, - }, - ); + if catalog.saved.prepared_count().is_some() { + if let Err(error) = catalog.install_prepared(cx) { + tracing::warn!(%error, "could not admit prepared history metadata"); + } } else { - let _ = catalog.reload(ContextId::General, cx); + let _ = catalog.reload(ContextId::General.into(), cx); } catalog } - fn reserve(&mut self, scope: ContextId) -> Result { + fn reserve( + &mut self, + scope: ChatScope, + bytes: usize, + cx: &mut Context, + ) -> Result<(u64, CatalogAllocation), CatalogLoadError> { if self.closing { return Err(CatalogLoadError::Closed); } @@ -153,38 +195,98 @@ impl Catalog { .sequence .checked_add(1) .ok_or(CatalogLoadError::GenerationExhausted)?; - self.payloads.retain(|payload| payload.strong_count() > 0); - let pending = self - .scopes - .iter() - .filter(|(key, entry)| **key != scope && entry.loading.is_some()) - .count(); - if self.payloads.len() + pending >= MAX_CATALOG_SNAPSHOTS { - let victim = self - .scopes - .iter() - .filter(|(key, entry)| { - **key != scope - && entry.loading.is_none() - && entry - .snapshot - .as_ref() - .is_some_and(|snapshot| Arc::strong_count(&snapshot.payload) == 1) - }) - .min_by_key(|(_, entry)| entry.generation) - .map(|(key, _)| *key); - let Some(victim) = victim else { - return Err(CatalogLoadError::Capacity); - }; - self.scopes.remove(&victim); - self.payloads.retain(|payload| payload.strong_count() > 0); - } + let permit = match self + .admission + .reserve_payload_reclaiming(Some(cx.entity_id()), cx) + { + Ok(permit) => permit, + Err(HistoryAdmissionError::Closed) => return Err(CatalogLoadError::Closed), + Err(_) => { + let victim = self + .reclaimable_scope(Some(&scope)) + .ok_or(CatalogLoadError::Capacity)?; + self.scopes.remove(&victim); + self.admission + .reserve_payload() + .map_err(|error| match error { + HistoryAdmissionError::Closed => CatalogLoadError::Closed, + _ => CatalogLoadError::Capacity, + })? + } + }; + let bytes = self.reserve_metadata(&scope, bytes, cx)?; self.sequence = generation; - Ok(generation) + Ok((generation, CatalogAllocation::new(permit, bytes))) + } + fn reserve_metadata( + &mut self, + scope: &ChatScope, + bytes: usize, + cx: &mut Context, + ) -> Result { + loop { + match self + .admission + .reserve_entity_bytes(cx.entity_id(), bytes, cx) + { + Ok(permit) => return Ok(permit), + Err(error @ HistoryAdmissionError::ResidentCapacity { .. }) => { + // The borrowed Runtime was excluded from callbacks. Its + // own unpinned cached scopes can be reclaimed directly. + let Some(victim) = self.reclaimable_scope(Some(scope)) else { + return Err(catalog_admission_error(error)); + }; + self.scopes.remove(&victim); + } + Err(error) => return Err(catalog_admission_error(error)), + } + } + } + fn reclaimable_scope(&self, excluding: Option<&ChatScope>) -> Option { + self.scopes + .iter() + .filter(|(scope, entry)| excluding != Some(*scope) && entry.loading.is_none()) + .filter_map(|(scope, entry)| { + entry + .snapshot + .as_ref() + .filter(|snapshot| Arc::strong_count(&snapshot.payload) == 1) + .map(|snapshot| (scope, snapshot.payload.allocation.order())) + }) + .min_by_key(|(_, order)| *order) + .map(|(scope, _)| scope.clone()) + } + fn install_prepared(&mut self, cx: &mut Context) -> Result<(), CatalogLoadError> { + let count = self.saved.prepared_count().unwrap_or(0); + let bytes = (count + 1) + .saturating_mul(allocation::MAX_ROW_BYTES) + .saturating_add(1024 * 1024); + let scope = ContextId::General.into(); + let (generation, allocation) = self.reserve(scope, bytes, cx)?; + let rows = CatalogAllocation::check(Ok(self.saved.rows().unwrap_or_default()))?; + let allocation = allocation + .finish(&self.admission, &rows, rows.capacity()) + .map_err(catalog_admission_error)?; + let payload = Arc::new(CatalogPayload { + scope: ContextId::General.into(), + generation, + rows, + error: None, + allocation, + }); + self.scopes.insert( + ContextId::General.into(), + ScopeCatalog { + snapshot: Some(CatalogSnapshot { payload }), + generation, + loading: None, + }, + ); + Ok(()) } pub(super) fn ensure( &mut self, - scope: ContextId, + scope: ChatScope, cx: &mut Context, ) -> Result<(), CatalogLoadError> { if self.closing { @@ -193,24 +295,36 @@ impl Catalog { if self.scopes.contains_key(&scope) { return Ok(()); } + if scope == ContextId::General.into() && self.saved.prepared_count().is_some() { + return self.install_prepared(cx); + } self.reload(scope, cx) } pub(super) fn reload( &mut self, - scope: ContextId, + scope: ChatScope, cx: &mut Context, ) -> Result<(), CatalogLoadError> { - let generation = self.reserve(scope)?; - let entry = self.scopes.entry(scope).or_default(); + let (generation, allocation) = self.reserve(scope.clone(), allocation::WORK_BYTES, cx)?; + let entry = self.scopes.entry(scope.clone()).or_default(); entry.generation = generation; let repository = self.repository.clone(); - let loading = cx - .background_executor() - .spawn(async move { repository.catalog(scope).await }); + let captured = scope.clone(); + let (complete, loading) = futures::channel::oneshot::channel(); + cx.background_executor() + .spawn(async move { + let rows = CatalogAllocation::check(repository.catalog_in(captured).await); + // The detached bounded read owns its allocation independently of a + // cancelled viewer. An abandoned result drops bytes and proof together. + let _ = complete.send((rows, allocation)); + }) + .detach(); entry.loading = Some(cx.spawn(async move |runtime, cx| { - let rows = loading.await; + let Ok((rows, allocation)) = loading.await else { + return; + }; let _ = runtime.update(cx, |runtime, cx| { - runtime.chats.loaded(scope, generation, rows); + runtime.chats.loaded(scope, generation, rows, allocation); cx.notify(); }); })); @@ -218,9 +332,10 @@ impl Catalog { } pub(super) fn complete( &mut self, - scope: ContextId, + scope: ChatScope, generation: u64, rows: Result, AgentStorageError>, + allocation: CatalogAllocation, ) { if self.closing { return; @@ -232,63 +347,88 @@ impl Catalog { return; } let (mut rows, error): (Vec, _) = match rows { - Ok(rows) => ( - rows.into_iter() - .filter(|row| { - require_chat_scope(scope, row.scope).is_ok() && row.validate().is_ok() - }) - .take(nocterm_ai::history::MAX_RESTORED) - .collect(), - None, - ), + Ok(mut rows) => { + rows.retain(|row| { + require_local_scope(&scope, &row.chat_scope()).is_ok() && row.validate().is_ok() + }); + rows.truncate(nocterm_ai::history::MAX_RESTORED); + (rows, None) + } Err(error) => { tracing::warn!(%error, "could not load agent catalog"); (Vec::new(), Some(error)) } }; - if scope == ContextId::General - && let Some(prepared) = &self.saved + if scope == ContextId::General.into() + && let Some(prepared) = self.saved.rows() { - for chat in prepared { - rows.retain(|row| row.id != chat.header.id); - rows.push(ChatSummary::from_chat(chat)); + for row in prepared { + rows.retain(|known| known.id != row.id); + rows.push(row); } rows.sort_by_key(|row| std::cmp::Reverse((row.pinned, row.updated))); rows.truncate(nocterm_ai::history::MAX_RESTORED); } + let allocation = match allocation.finish(&self.admission, &rows, rows.capacity()) { + Ok(allocation) => allocation, + Err(error) => { + entry.loading.take(); + tracing::warn!(%error, "could not retain catalog metadata"); + return; + } + }; let payload = Arc::new(CatalogPayload { scope, generation, rows, error, + allocation, }); - self.payloads.retain(|payload| payload.strong_count() > 0); - self.payloads.push(Arc::downgrade(&payload)); entry.snapshot = Some(CatalogSnapshot { payload }); entry.loading.take(); } - pub(super) fn invalidate(&mut self, scope: ContextId) { + pub(super) fn invalidate(&mut self, scope: ChatScope) { self.scopes.remove(&scope); } - pub(super) fn snapshot(&self, scope: ContextId) -> Option { + pub(super) fn snapshot(&self, scope: ChatScope) -> Option { self.scopes.get(&scope)?.snapshot.clone() } - pub(super) fn take_prepared(&mut self, key: &ChatKey) -> Option { - if key.scope() != ContextId::General { - return None; - } - let records = self.saved.as_mut()?; - let index = records.iter().position(|chat| chat.header.id == key.id())?; - Some(records.remove(index)) + pub(super) fn oldest_payload(&self) -> Option { + let scope = self.reclaimable_scope(None)?; + Some( + self.scopes + .get(&scope)? + .snapshot + .as_ref()? + .payload + .allocation + .order(), + ) + } + pub(super) fn evict_payload(&mut self) -> bool { + let Some(scope) = self.reclaimable_scope(None) else { + return false; + }; + self.scopes.remove(&scope).is_some() + } + pub(super) fn take_prepared( + &mut self, + key: &ChatKey, + payload: crate::history_admission::HistoryPayloadPermit, + cx: &mut gpui::App, + ) -> Result>, AgentStorageError> { + self.saved.take(key, payload, cx) } - pub(super) fn prepared_bytes(&self) -> usize { - self.saved - .as_ref() - .map_or(0, |saved| saved.iter().map(SavedChat::resident_bytes).sum()) + pub(super) fn discard_prepared(&mut self, key: &ChatKey) { + self.saved.discard(key); } - pub(super) fn reader(&self, scope: ContextId) -> HistoryReader { + + pub(super) fn reader(&self, scope: ChatScope) -> HistoryReader { HistoryReader::new(self.repository.clone(), self.gate.clone(), scope) } + pub(super) fn unavailable_reader(&self) -> HistoryReader { + HistoryReader::unavailable(self.repository.clone(), self.gate.clone()) + } pub(super) fn stop(&mut self) { self.closing = true; for entry in self.scopes.values_mut() { @@ -297,6 +437,24 @@ impl Catalog { } } +fn catalog_admission_error(error: HistoryAdmissionError) -> CatalogLoadError { + match error { + HistoryAdmissionError::Closed => CatalogLoadError::Closed, + HistoryAdmissionError::PayloadCapacity => CatalogLoadError::Capacity, + HistoryAdmissionError::ResidentCapacity { + retained, + incoming, + limit, + } => CatalogLoadError::ResidentCapacity { + retained, + incoming, + limit, + }, + HistoryAdmissionError::Storage(error) => CatalogLoadError::Storage(error), + error => CatalogLoadError::Admission(Arc::new(error)), + } +} + #[cfg(test)] mod tests { use super::*; @@ -344,7 +502,10 @@ mod tests { .collect(); assert!(matches!( PreparedHistory::new(chats), - Err(AgentStorageError::Codec(HistoryCodecError::StructuralLimit)) + Err(AgentStorageError::ResidentCapacity { + limit: crate::history_admission::MAX_NORMAL_HISTORY_BYTES, + .. + }) )); } #[test] diff --git a/crates/agent-runtime/src/catalog/allocation.rs b/crates/agent-runtime/src/catalog/allocation.rs new file mode 100644 index 00000000..360b20d9 --- /dev/null +++ b/crates/agent-runtime/src/catalog/allocation.rs @@ -0,0 +1,66 @@ +//! Pending scans and retained metadata share the installation's byte allocation. +use crate::history_admission::{ + HistoryAdmissionError, HistoryAdmissionHandle, HistoryBytesPermit, HistoryPayloadPermit, +}; +use nocterm_ai::{ + history::{ChatSummary, HistoryCodecError, MAX_RESTORED}, + storage::AgentStorageError, +}; + +pub(super) const MAX_ROW_BYTES: usize = 32 * 1024; +// At most200 returned rows,200 prepared metadata rows and one native scan +// candidate coexist. OneMiB also covers vector reallocation and assembly state. +pub(crate) const WORK_BYTES: usize = (2 * MAX_RESTORED + 1) * MAX_ROW_BYTES + 1024 * 1024; + +pub(crate) struct CatalogAllocation { + slot: HistoryPayloadPermit, + bytes: HistoryBytesPermit, +} +impl CatalogAllocation { + pub(super) fn new(slot: HistoryPayloadPermit, bytes: HistoryBytesPermit) -> Self { + Self { slot, bytes } + } + pub(super) fn order(&self) -> u64 { + self.slot.order() + } + pub(super) fn finish( + self, + admission: &HistoryAdmissionHandle, + rows: &[ChatSummary], + capacity: usize, + ) -> Result { + let retained = rows.iter().fold( + 4096usize.saturating_add(capacity.saturating_mul(std::mem::size_of::())), + |total, row| total.saturating_add(row_bytes(row)), + ); + Ok(Self { + slot: self.slot, + bytes: admission.shrink_buffer(self.bytes, retained)?, + }) + } + pub(super) fn check( + rows: Result, AgentStorageError>, + ) -> Result, AgentStorageError> { + let rows = rows?; + if rows.len() > MAX_RESTORED { + return Err(HistoryCodecError::CollectionLimit.into()); + } + if rows.iter().any(|row| row_bytes(row) > MAX_ROW_BYTES) { + return Err(HistoryCodecError::StructuralLimit.into()); + } + Ok(rows) + } +} +fn row_bytes(row: &ChatSummary) -> usize { + // Fixed storage covers inline IDs/epochs/fingerprints and the captured + // environment's bounded namespace strings (2*128B or a host<=253B). + // Actual string capacity counts the owned allocations, including decoder + // spare capacity. Serializing fixed fingerprint bytes as JSON nodes would + // overstate this metadata's memory rather than measure its retained owners. + 4096usize + .saturating_add(row.id.capacity()) + .saturating_add(row.agent_id.capacity()) + .saturating_add(row.title.as_ref().map_or(0, String::capacity)) + .saturating_add(row.name.as_ref().map_or(0, String::capacity)) + .saturating_add(row.model.as_ref().map_or(0, String::capacity)) +} diff --git a/crates/agent-runtime/src/catalog/prepared.rs b/crates/agent-runtime/src/catalog/prepared.rs new file mode 100644 index 00000000..d40851a1 --- /dev/null +++ b/crates/agent-runtime/src/catalog/prepared.rs @@ -0,0 +1,132 @@ +//! Prepared local bodies remain accounted before any chat entity owns them. +use crate::{ + HistoryUsage, + history_admission::{ + HistoryAdmissionHandle, HistoryParticipant, HistoryPayloadPermit, HistoryRegistration, + }, +}; +use gpui::App; +use nocterm_ai::{ + execution_origin::ChatKey, + history::{ChatSummary, SavedChat}, + storage::AgentStorageError, +}; +use std::{ + cell::RefCell, + rc::{Rc, Weak}, +}; + +pub(super) struct PreparedRecords { + records: Rc>, + registration: HistoryRegistration, + allocations: crate::history_admission::LocalHistoryAllocations, +} +struct PreparedBatch { + values: Option>, + allocation: Option, +} +struct PreparedUsage(Weak>); +impl HistoryParticipant for PreparedUsage { + fn usage(&self, _cx: &App) -> Result, AgentStorageError> { + Ok(self.0.upgrade().map(|records| { + HistoryUsage::new( + records.borrow().values.as_ref().map_or(0, |values| { + values.iter().map(SavedChat::resident_bytes).sum() + }), + 0, + true, + ) + })) + } +} +impl PreparedRecords { + pub(super) fn prepared_count(&self) -> Option { + self.records.borrow().values.as_ref().map(Vec::len) + } + pub(super) fn new( + records: Option>, + admission: &HistoryAdmissionHandle, + allocation: Option, + runtime: gpui::EntityId, + ) -> Self { + let records = Rc::new(RefCell::new(PreparedBatch { + values: records, + allocation, + })); + let registration = admission + .register(Rc::new(PreparedUsage(Rc::downgrade(&records)))) + .expect("a new installation admission is open"); + Self { + records, + registration, + allocations: crate::history_admission::LocalHistoryAllocations::new( + admission.clone(), + runtime, + ), + } + } + pub(super) fn rows(&self) -> Option> { + self.records + .borrow() + .values + .as_ref() + .map(|records| records.iter().map(ChatSummary::from_chat).collect()) + } + pub(super) fn take( + &self, + key: &ChatKey, + payload: HistoryPayloadPermit, + cx: &mut App, + ) -> Result>, AgentStorageError> { + let values = self.records.borrow(); + let Some(records) = &values.values else { + return Ok(None); + }; + let Some(index) = records.iter().position(|chat| chat.header.id == key.id()) else { + return Ok(None); + }; + let bytes = records[index].resident_bytes(); + let remaining = records + .iter() + .enumerate() + .filter(|(position, _)| *position != index) + .fold(0usize, |total, (_, chat)| { + total.saturating_add(chat.resident_bytes()) + }); + drop(values); + // Admit before removing the source body. No callbacks/await occur between + // reserving this immutable destination and moving that exact owned body. + let allocation = self.allocations.transfer_prepared( + &self.registration, + remaining, + bytes, + payload, + cx, + )?; + let mut batch = self.records.borrow_mut(); + let Some(records) = batch.values.as_mut() else { + return Err(AgentStorageError::Interrupted); + }; + let Some(index) = records.iter().position(|chat| chat.header.id == key.id()) else { + return Err(AgentStorageError::Interrupted); + }; + let chat = records.remove(index); + if records.is_empty() { + batch.allocation.take(); + } + Ok(Some(nocterm_core::allocation::Allocated::new( + chat, allocation, + ))) + } + pub(super) fn discard(&self, key: &ChatKey) { + let mut batch = self.records.borrow_mut(); + if let Some(records) = &mut batch.values { + if let Some(index) = records.iter().position(|chat| chat.header.id == key.id()) { + drop(records.remove(index)); + } + if records.is_empty() { + batch.allocation.take(); + } + } + } +} diff --git a/crates/agent-runtime/src/catalog/reclamation.rs b/crates/agent-runtime/src/catalog/reclamation.rs new file mode 100644 index 00000000..5921aafb --- /dev/null +++ b/crates/agent-runtime/src/catalog/reclamation.rs @@ -0,0 +1,35 @@ +//! Weak catalog reclamation participates in the installation's shared payload cap. +use crate::{HistoryUsage, Runtime, history_admission::HistoryParticipant}; +use gpui::{App, EntityId, WeakEntity}; +use nocterm_ai::storage::AgentStorageError; + +pub(super) struct CatalogReclamation(WeakEntity); +impl CatalogReclamation { + pub(super) fn new(runtime: WeakEntity) -> Self { + Self(runtime) + } +} +impl HistoryParticipant for CatalogReclamation { + fn entity_id(&self) -> Option { + Some(self.0.entity_id()) + } + fn usage(&self, _cx: &App) -> Result, AgentStorageError> { + Ok(None) + } + fn oldest_payload(&self, cx: &App) -> Option { + self.0 + .upgrade() + .and_then(|runtime| runtime.read(cx).chats.oldest_catalog_payload()) + } + fn evict_payload(&self, cx: &mut App) -> bool { + self.0.upgrade().is_some_and(|runtime| { + runtime.update(cx, |runtime, cx| { + let evicted = runtime.chats.evict_catalog_payload(); + if evicted { + cx.notify(); + } + evicted + }) + }) + } +} diff --git a/crates/agent-runtime/src/client.rs b/crates/agent-runtime/src/client.rs index d3846c92..c2234f36 100644 --- a/crates/agent-runtime/src/client.rs +++ b/crates/agent-runtime/src/client.rs @@ -160,18 +160,34 @@ impl HistoryUsage { /// A chat as the runtime sees it. Calls on a closed chat do nothing. pub trait SessionClient { fn id(&self) -> EntityId; + /// The chat itself, so that another window can list a chat this one holds. + fn chat(&self) -> Option { + None + } /// Whether the chat is still open. fn alive(&self) -> bool; fn emit(&self, event: SessionEvent, cx: &mut App); fn state(&self, cx: &App) -> Option; /// Resident history only; callers must exclude a currently borrowed requester. fn history_usage(&self, cx: &App) -> Option; + fn history_payload(&self, _cx: &App) -> Option { + None + } /// Evict only through the chat's durability, lease and dirty-input gates. fn evict_history(&self, cx: &mut App) -> bool; /// The chat's unsaved changes, without changing it. - fn snapshot(&self, cx: &App) -> Option>; + fn snapshot( + &self, + cx: &mut App, + ) -> Result>, nocterm_ai::storage::AgentStorageError>; /// The chat's unsaved changes and the revision they become. - fn capture(&self, cx: &mut App) -> Option<(Arc, u64)>; + fn capture( + &self, + cx: &mut App, + ) -> Result< + Option<(Arc, u64)>, + nocterm_ai::storage::AgentStorageError, + >; } pub type Client = Rc; diff --git a/crates/agent-runtime/src/documents.rs b/crates/agent-runtime/src/documents.rs index e4173d7b..1886fc58 100644 --- a/crates/agent-runtime/src/documents.rs +++ b/crates/agent-runtime/src/documents.rs @@ -5,6 +5,7 @@ use gpui::AppContext as _; use nocterm_ai::{ execution_origin::ChatKey, history::{ChatSummary, ContextId, SavedChat, SharedChat}, + local_environment::ChatScope, storage::AgentStorageError, }; use nocterm_store::{KeyedPersisted, KeyedRetention, StoreError}; @@ -28,10 +29,15 @@ pub(super) struct ChatStore { registry: Rc>, writer: Entity>, catalog: crate::catalog::Catalog, + admission: crate::history_admission::LocalHistoryAllocations, } impl ChatStore { pub(super) fn new(storage: &mut AgentStorage, cx: &mut Context) -> Self { - let registry = Rc::new(RefCell::new(DocumentRegistry::default())); + let (admission, prepared) = + crate::history_admission::HistoryAdmissionHandle::with_initial_payload( + storage.has_prepared_history(), + ); + let registry = Rc::new(RefCell::new(DocumentRegistry::new(admission.clone()))); let owner = registry.clone(); let runtime = cx.weak_entity(); let repository = storage.chats(); @@ -44,7 +50,7 @@ impl ChatStore { match intent { ChatIntent::Save(chat) => backend.save(chat).await, ChatIntent::Delete => { - backend.delete(id.scope(), id.id().to_owned()).await + backend.delete_in(id.chat_scope(), id.id().to_owned()).await } } .map_err(StoreError::backend) @@ -63,7 +69,7 @@ impl ChatStore { cx.defer(move |cx| { if succeeded && !retained { let _ = runtime.update(cx, |runtime, cx| { - let _ = runtime.chats.reload(stamp.key().scope(), cx); + let _ = runtime.chats.reload(stamp.key().chat_scope(), cx); cx.notify(); }); } @@ -92,23 +98,31 @@ impl ChatStore { } }) }); + let accounting = crate::history_admission::LocalHistoryAllocations::new( + admission.clone(), + cx.entity_id(), + ); Self { registry, writer, - catalog: crate::catalog::Catalog::new(storage, cx), + catalog: crate::catalog::Catalog::new(storage, admission, prepared, cx), + admission: accounting, } } pub(super) fn require_client_scope( - expected: ContextId, + expected: ChatScope, client: &Client, cx: &mut App, ) -> Result<(), AgentStorageError> { let state = client.state(cx); if let Some(state) = &state { - nocterm_ai::storage::require_chat_scope(expected, state.origin().scope())?; + nocterm_ai::storage::require_local_scope( + &expected, + &state.origin().key().chat_scope(), + )?; } - if let Some((chat, _)) = client.capture(cx) { - nocterm_ai::storage::require_chat_scope(expected, chat.header.scope)?; + if let Some((chat, _)) = client.capture(cx)? { + nocterm_ai::storage::require_local_scope(&expected, &chat.header.chat_scope())?; if state.is_some_and(|state| state.origin().key().id() != chat.header.id) { return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); } @@ -118,21 +132,23 @@ impl ChatStore { pub(super) fn claim( &self, key: ChatKey, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, window: EntityId, replacing: Option, ) -> Result { - DocumentRegistry::claim(&self.registry, key, window, replacing) + DocumentRegistry::claim(&self.registry, key, access, window, replacing) } + /// Commits `claim` to `client`; the owner it replaced in another window, if any. pub(super) fn commit( &mut self, claim: DocumentClaim, client: Client, cx: &mut App, - ) -> Result<(), crate::ChatOpenError> { + ) -> Result, crate::ChatOpenError> { if !claim.belongs_to(&self.registry) { return Err(crate::ChatOpenError::StaleClaim(claim.key().clone())); } - Self::require_client_scope(claim.key().scope(), &client, cx)?; + Self::require_client_scope(claim.key().chat_scope(), &client, cx)?; let state = client.state(cx).ok_or(crate::ChatOpenError::Closed)?; if state.origin().key() != claim.key() { return Err(crate::ChatOpenError::Storage( @@ -147,21 +163,23 @@ impl ChatStore { pub(super) fn take_prepared( &mut self, claim: &DocumentClaim, - ) -> Result, crate::ChatOpenError> { + payload: crate::history_admission::HistoryPayloadPermit, + cx: &mut App, + ) -> Result>, crate::ChatOpenError> { if !claim.belongs_to(&self.registry) { return Err(crate::ChatOpenError::StaleClaim(claim.key().clone())); } self.registry.borrow().require_reservation(claim)?; - Ok(self.catalog.take_prepared(claim.key())) + Ok(self.catalog.take_prepared(claim.key(), payload, cx)?) } pub(super) fn require_owner_scope( &self, - scope: ContextId, + scope: ChatScope, id: EntityId, cx: &mut App, ) -> Result<(), AgentStorageError> { if let Some(actual) = self.registry.borrow().scope_of(id) { - nocterm_ai::storage::require_chat_scope(scope, actual)?; + nocterm_ai::storage::require_local_scope(&scope, &actual)?; } if let Some(client) = self.client(id) { Self::require_client_scope(scope, &client, cx)?; @@ -171,7 +189,7 @@ impl ChatStore { pub(super) fn retire(&mut self, id: EntityId, cx: &mut App) { let ids = self.registry.borrow_mut().retire(id); for key in &ids { - self.catalog.invalidate(key.scope()); + self.catalog.invalidate(key.chat_scope()); } self.writer.update(cx, |writer, _| { for id in ids { @@ -182,6 +200,18 @@ impl ChatStore { pub(super) fn client(&self, id: EntityId) -> Option { self.registry.borrow().client(id) } + pub(super) fn held_elsewhere(&self, scope: &ChatScope, window: EntityId) -> Vec { + self.registry.borrow().held_elsewhere(scope, window) + } + pub(super) fn window_of(&self, entity: EntityId) -> Option { + self.registry.borrow().window_of(entity) + } + pub(super) fn revoke_stale_sources(&self) -> Vec { + self.registry.borrow_mut().revoke_stale_sources() + } + pub(super) fn source_current(&self, key: &ChatKey) -> bool { + key.environment().is_this_computer() || self.registry.borrow().source_current(key) + } pub(super) fn clients(&self) -> Vec { self.registry.borrow().clients() } @@ -190,15 +220,15 @@ impl ChatStore { } pub(super) fn save( &mut self, - scope: ContextId, + scope: ChatScope, chat: Arc, owner: EntityId, revision: u64, cx: &mut App, ) -> Result { - nocterm_ai::storage::require_chat_scope(scope, chat.header.scope)?; - let key = ChatKey::new(scope, chat.header.id.clone()); - if !self.registry.borrow().current(&key, owner) + nocterm_ai::storage::require_local_scope(&scope, &chat.header.chat_scope())?; + let key = ChatKey::in_environment(scope.clone(), chat.header.id.clone()); + if !self.registry.borrow().capture_current(&key, owner) || !self .registry .borrow() @@ -206,6 +236,11 @@ impl ChatStore { { return Ok(false); } + let existing = self + .client(owner) + .and_then(|client| client.history_usage(cx)) + .map_or(0, |usage| usage.bytes()); + let chat = self.admission.retain_external(owner, existing, chat, cx)?; let id = key; self.submit( id.clone(), @@ -275,8 +310,8 @@ impl ChatStore { return; } self.registry.borrow_mut().seal(id.clone()); - self.catalog.take_prepared(&id); - self.catalog.invalidate(id.scope()); + self.catalog.discard_prepared(&id); + self.catalog.invalidate(id.chat_scope()); if let Err(error) = self.submit( id, ChatIntent::Delete, @@ -298,9 +333,9 @@ impl ChatStore { let Some(client) = self.client(id) else { return Ok(false); }; - if let Some((chat, revision)) = client.capture(cx) { - let key = ChatKey::new(chat.header.scope, chat.header.id.clone()); - match self.save(key.scope(), chat, id, revision, cx) { + if let Some((chat, revision)) = client.capture(cx)? { + let key = ChatKey::in_environment(chat.header.chat_scope(), chat.header.id.clone()); + match self.save(key.chat_scope(), chat, id, revision, cx) { Ok(true) => { let target = client.clone(); cx.defer(move |cx| target.emit(SessionEvent::SaveAdmitted { revision }, cx)); @@ -331,37 +366,43 @@ impl ChatStore { }) .collect() } - pub(super) fn snapshot(&self, scope: ContextId) -> Option { + pub(super) fn snapshot(&self, scope: ChatScope) -> Option { self.catalog.snapshot(scope) } + pub(super) fn oldest_catalog_payload(&self) -> Option { + self.catalog.oldest_payload() + } + pub(super) fn evict_catalog_payload(&mut self) -> bool { + self.catalog.evict_payload() + } pub(super) fn loaded( &mut self, - scope: ContextId, + scope: ChatScope, generation: u64, rows: Result, AgentStorageError>, + allocation: crate::catalog::CatalogAllocation, ) { - let rows = rows.map(|rows| { - rows.into_iter() - .filter(|row| { - !self - .registry - .borrow() - .contains_deleted(&ChatKey::new(scope, row.id.clone())) - }) - .collect() + let rows = rows.map(|mut rows| { + rows.retain(|row| { + !self + .registry + .borrow() + .contains_deleted(&ChatKey::in_environment(scope.clone(), row.id.clone())) + }); + rows }); - self.catalog.complete(scope, generation, rows) + self.catalog.complete(scope, generation, rows, allocation) } pub(super) fn ensure( &mut self, - scope: ContextId, + scope: ChatScope, cx: &mut Context, ) -> Result<(), crate::CatalogLoadError> { self.catalog.ensure(scope, cx) } pub(super) fn reload( &mut self, - scope: ContextId, + scope: ChatScope, cx: &mut Context, ) -> Result<(), crate::CatalogLoadError> { self.catalog.reload(scope, cx) @@ -370,19 +411,19 @@ impl ChatStore { &self, claim: &DocumentClaim, cx: &App, - ) -> Result { + ) -> Result { if !claim.belongs_to(&self.registry) { return Err(crate::ChatOpenError::StaleClaim(claim.key().clone())); } self.registry.borrow().require_reservation(claim)?; - Ok(self.history_residency(claim.key().scope(), None, cx)?) + Ok(self.history_residency(claim.key().chat_scope(), None, cx)?) } pub(super) fn history_residency( &self, - scope: ContextId, + scope: ChatScope, requester: Option, - cx: &App, - ) -> Result { + _cx: &App, + ) -> Result { let registry = self.registry.borrow(); if registry.closing() { return Err(AgentStorageError::Interrupted); @@ -391,30 +432,17 @@ impl ChatStore { let actual = registry .scope_of(requester) .ok_or(AgentStorageError::Interrupted)?; - nocterm_ai::storage::require_chat_scope(scope, actual)?; + nocterm_ai::storage::require_local_scope(&scope, &actual)?; } - let mut retained = self.catalog.prepared_bytes(); - let mut seen = std::collections::HashSet::new(); - let mut error = None; - self.writer.read(cx).visit_retained_values(|intent| { - if let ChatIntent::Save(chat) = intent - && seen.insert(Arc::as_ptr(chat)) - { - match chat.retained_bytes() { - Ok(bytes) => retained = retained.saturating_add(bytes), - Err(cause) => error = Some(AgentStorageError::from(cause)), - } - } - }); - if let Some(error) = error { - return Err(error); - } - Ok(crate::residency::HistoryResidency::new( - registry.clients(), - retained, - )) + Ok(self.admission.handle()) } - pub(super) fn reader(&self, scope: ContextId) -> HistoryReader { + pub(super) fn history_admission(&self) -> crate::history_admission::HistoryAdmissionHandle { + self.admission.handle() + } + pub(super) fn unavailable_reader(&self) -> HistoryReader { + self.catalog.unavailable_reader() + } + pub(super) fn reader(&self, scope: ChatScope) -> HistoryReader { self.catalog.reader(scope) } pub(super) fn begin_shutdown(&mut self) { @@ -431,11 +459,14 @@ impl ChatStore { { return Ok(()); } - let Some((chat, revision)) = client.capture(cx) else { + let Some((chat, revision)) = client.capture(cx).map_err(StoreError::backend)? else { return Ok(()); }; - nocterm_ai::storage::require_chat_scope(state.origin().scope(), chat.header.scope) - .map_err(StoreError::backend)?; + nocterm_ai::storage::require_local_scope( + &state.origin().key().chat_scope(), + &chat.header.chat_scope(), + ) + .map_err(StoreError::backend)?; if !self .registry .borrow() @@ -443,6 +474,15 @@ impl ChatStore { { return Ok(()); } + let chat = self + .admission + .retain_external( + client.id(), + client.history_usage(cx).map_or(0, |usage| usage.bytes()), + chat, + cx, + ) + .map_err(StoreError::backend)?; let id = state.origin().key().clone(); self.submit( id.clone(), @@ -468,6 +508,7 @@ impl ChatStore { .filter_map(|client| self.capture_document(&client, cx).err()) .collect::>(); self.catalog.stop(); + self.admission.handle().close(); self.registry.borrow_mut().stop(); let captured = self.writer.update(cx, |writer, _| writer.capture()); Box::pin(async move { @@ -486,29 +527,66 @@ impl ChatStore { } } impl Runtime { + #[cfg(test)] pub(crate) fn claim_document( &self, key: ChatKey, window: EntityId, replacing: Option, ) -> Result { - self.chats.claim(key, window, replacing) + self.chats.claim( + key, + nocterm_ai::local_environment::LocalEnvironmentAccess::this_computer(), + window, + replacing, + ) + } + pub(crate) fn claim_local_document( + &self, + key: ChatKey, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, + window: EntityId, + replacing: Option, + ) -> Result { + self.chats.claim(key, access, window, replacing) } pub(crate) fn commit_document( &mut self, claim: DocumentClaim, client: Client, - cx: &mut App, + cx: &mut Context, ) -> Result<(), crate::ChatOpenError> { - self.chats.commit(claim, client, cx) + if let Some(chat) = self.chats.commit(claim, client, cx)? { + cx.emit(crate::HolderEvent::Moved { chat }); + } + cx.emit(crate::HolderEvent::Changed); + Ok(()) + } + /// The chats of `scope` windows other than `window` hold. + pub(crate) fn held_elsewhere(&self, scope: &ChatScope, window: EntityId) -> Vec { + self.chats.held_elsewhere(scope, window) + } + /// The window that keeps chat `owner` from moving to `window`: its own, while its agent + /// works there or it shows there. A chat moves from one window to another only at rest. + pub(crate) fn kept_by(&self, owner: EntityId, window: EntityId, cx: &App) -> Option { + let held = self.chats.window_of(owner)?; + if held == window { + return None; + } + let state = self.chats.client(owner).and_then(|client| client.state(cx)); + state + .is_none_or(|state| state.busy() || state.shown()) + .then_some(held) } pub(crate) fn take_prepared_chat( &mut self, claim: &DocumentClaim, - ) -> Result, crate::ChatOpenError> { - self.chats.take_prepared(claim) + payload: crate::history_admission::HistoryPayloadPermit, + cx: &mut App, + ) -> Result>, crate::ChatOpenError> { + self.chats.take_prepared(claim, payload, cx) } - pub(crate) fn catalog_snapshot(&self, scope: ContextId) -> Option { + pub(crate) fn catalog_snapshot(&self, scope: ChatScope) -> Option { self.chats.snapshot(scope) } pub fn document_is_current(&self, chat_id: &str, owner: EntityId) -> bool { @@ -533,7 +611,7 @@ impl Runtime { } pub(crate) fn capture_scoped_document( &mut self, - scope: ContextId, + scope: ChatScope, id: EntityId, cx: &mut Context, ) -> Result { @@ -547,11 +625,11 @@ impl Runtime { revision: u64, cx: &mut Context, ) -> Result { - self.scoped_save_chat(ContextId::General, chat, owner, revision, cx) + self.scoped_save_chat(ContextId::General.into(), chat, owner, revision, cx) } pub(crate) fn scoped_save_chat( &mut self, - scope: ContextId, + scope: ChatScope, chat: Arc, owner: EntityId, revision: u64, @@ -563,21 +641,21 @@ impl Runtime { self.scoped_delete_chat(ChatKey::new(ContextId::General, id), cx) } pub(crate) fn scoped_delete_chat(&mut self, key: ChatKey, cx: &mut Context) { - let scope = key.scope(); + let scope = key.chat_scope(); self.chats.delete(key, cx); self.scoped_reload_history(scope, cx); } pub fn reload_history(&mut self, cx: &mut Context) { - self.scoped_reload_history(ContextId::General, cx) + self.scoped_reload_history(ContextId::General.into(), cx) } - pub(crate) fn scoped_reload_history(&mut self, scope: ContextId, cx: &mut Context) { + pub(crate) fn scoped_reload_history(&mut self, scope: ChatScope, cx: &mut Context) { if let Err(error) = self.try_scoped_reload_history(scope, cx) { tracing::warn!(%error, "could not request agent history"); } } pub(crate) fn try_scoped_reload_history( &mut self, - scope: ContextId, + scope: ChatScope, cx: &mut Context, ) -> Result<(), crate::CatalogLoadError> { self.chats.reload(scope, cx) diff --git a/crates/agent-runtime/src/documents/history_tests.rs b/crates/agent-runtime/src/documents/history_tests.rs index 960dcfa2..18871d32 100644 --- a/crates/agent-runtime/src/documents/history_tests.rs +++ b/crates/agent-runtime/src/documents/history_tests.rs @@ -11,6 +11,7 @@ use std::{ }; mod closing; mod contexts; +mod environments; mod local_launch; mod ownership; mod preferences; @@ -101,7 +102,7 @@ impl SessionClient for ProbeClient { } let chat = self.chat.borrow(); let origin = nocterm_ai::execution_origin::ExecutionOrigin::new( - nocterm_ai::execution_origin::ChatKey::new(chat.header.scope, chat.header.id.clone()), + nocterm_ai::execution_origin::ChatKey::from_header(&chat.header), None, ); Some(ClientState::new( @@ -126,11 +127,17 @@ impl SessionClient for ProbeClient { self.history.set(None); true } - fn snapshot(&self, _: &App) -> Option> { - Some(self.chat.borrow().clone()) + fn snapshot( + &self, + _: &mut App, + ) -> Result>, nocterm_ai::storage::AgentStorageError> { + Ok(Some(self.chat.borrow().clone())) } - fn capture(&self, _: &mut App) -> Option<(Arc, u64)> { - Some((self.chat.borrow().clone(), self.revision.get())) + fn capture( + &self, + _: &mut App, + ) -> Result, u64)>, nocterm_ai::storage::AgentStorageError> { + Ok(Some((self.chat.borrow().clone(), self.revision.get()))) } } fn fixture( @@ -166,32 +173,23 @@ fn runtime_from_storage( storage: AgentStorage, directory: &std::path::Path, ) -> Entity { - runtime_with_usage_source( + runtime_with_workspace_resolver( cx, connector, storage, directory, - Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()), Arc::new(nocterm_ai::testing::ScriptedWorkdirResolver::accept_requests()), ) } -fn runtime_with_usage_source( +fn runtime_with_workspace_resolver( cx: &mut TestAppContext, connector: Arc, storage: AgentStorage, directory: &std::path::Path, - usage_source: Arc, workdir_resolver: Arc, ) -> Entity { cx.update(|cx| { - let services = runtime_services( - connector, - storage, - directory, - usage_source, - workdir_resolver, - cx, - ); + let services = runtime_services(connector, storage, directory, workdir_resolver, cx); cx.new(|cx| Runtime::new(services, cx)) }) } @@ -199,7 +197,6 @@ fn runtime_services( connector: Arc, storage: AgentStorage, directory: &std::path::Path, - usage_source: Arc, workdir_resolver: Arc, cx: &mut App, ) -> RuntimeServices { @@ -210,12 +207,13 @@ fn runtime_services( cx, ); RuntimeServices { + command_jobs: Arc::new(nocterm_session::testing::ScriptedCommandJobsFactory::default()), + transfers: Arc::new(nocterm_transfers::testing::FakeTransfers::default()), settings, agents, connector, bridge: Arc::new(UnusedBridge), storage, - usage_source, workdir_resolver, workdir: directory.join("work"), terminal_auth: false, diff --git a/crates/agent-runtime/src/documents/history_tests/closing.rs b/crates/agent-runtime/src/documents/history_tests/closing.rs index b16df14c..5ff6b289 100644 --- a/crates/agent-runtime/src/documents/history_tests/closing.rs +++ b/crates/agent-runtime/src/documents/history_tests/closing.rs @@ -131,6 +131,7 @@ fn connected_client( args: Vec::new(), env: Default::default(), inherit_env: Vec::new(), + hub: None, }, cx, ) diff --git a/crates/agent-runtime/src/documents/history_tests/contexts.rs b/crates/agent-runtime/src/documents/history_tests/contexts.rs index 4ce5eeec..989df9ca 100644 --- a/crates/agent-runtime/src/documents/history_tests/contexts.rs +++ b/crates/agent-runtime/src/documents/history_tests/contexts.rs @@ -5,6 +5,16 @@ use nocterm_ai::{ storage::AgentStorageError, }; +pub(super) fn decoded_allowance( + admission: &crate::history_admission::HistoryAdmissionHandle, + bytes: usize, + cx: &mut App, +) -> nocterm_core::allocation::AllocationLease { + let slot = admission.reserve_payload_reclaiming(None, cx).unwrap(); + let proof = admission.reserve_unowned_bytes(bytes, cx).unwrap(); + nocterm_core::allocation::AllocationLease::new(bytes, 1, (slot, proof)) +} + pub(super) fn project_chat() -> SavedChat { let mut chat = snapshot().metadata.clone(); chat.version = 2; @@ -52,6 +62,7 @@ fn general_registration_and_direct_start_refuse_project_documents_without_native args: Vec::new(), env: Default::default(), inherit_env: Vec::new(), + hub: None, }; assert!(matches!( runtime.connect(client.clone(), chat.header.id.clone(), launch, cx), @@ -83,12 +94,13 @@ fn general_hydration_and_catalog_preserve_other_contexts_in_storage(cx: &mut Tes runtime.update(cx, |runtime, cx| runtime.reload_history(cx)); cx.run_until_parked(); let rows = runtime.update(cx, |runtime, _| { - runtime.catalog_snapshot(ContextId::General).unwrap() + runtime.catalog_snapshot(ContextId::General.into()).unwrap() }); assert_eq!(rows.rows().len(), 1); assert_eq!(rows.rows()[0].id, general.header.id); let reader = cx.update(|cx| runtime.read(cx).history_reader()); - let (loaded, permit) = futures::executor::block_on(reader.load(project.header.id.clone())); + let (loaded, permit) = + futures::executor::block_on(reader.prepare_load(project.header.id.clone())); assert!(matches!(loaded, Err(AgentStorageError::NotFound(_)))); drop(permit); assert_eq!( @@ -134,7 +146,6 @@ pub(super) fn scoped_fixture_with_history( connector.clone(), storage, directory.path(), - Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()), resolver.clone(), cx, ), @@ -196,16 +207,41 @@ async fn readers_keep_their_original_context_after_another_scoped_handle_is_crea repository.seed_chat(&second).unwrap(); let reader = cx.update(|cx| project.history_reader(cx)); let _selected_elsewhere = project.for_scope(ContextId::General); - let (loaded, permit) = reader.load(second.header.id.clone()).await; - let loaded = loaded.unwrap(); - assert_eq!(loaded.header.scope, second.header.scope); - assert_eq!(loaded.header.title, second.header.title); + let (prepared, permit) = reader.prepare_load(second.header.id.clone()).await; + let prepared = prepared.unwrap(); + let allocation = cx.update(|cx| { + decoded_allowance( + &general.history_admission(cx), + prepared.resident_bytes(), + cx, + ) + }); + let loaded = prepared.decode(allocation).await.unwrap(); + assert_eq!(loaded.value().header.scope, second.header.scope); + assert_eq!(loaded.value().header.title, second.header.title); drop(permit); let (loaded, permit) = cx .update(|cx| general.history_reader(cx)) - .load(first.header.id) + .prepare_load(first.header.id) .await; - assert_eq!(loaded.unwrap().header.title, first.header.title); + let prepared = loaded.unwrap(); + let allocation = cx.update(|cx| { + decoded_allowance( + &general.history_admission(cx), + prepared.resident_bytes(), + cx, + ) + }); + assert_eq!( + prepared + .decode(allocation) + .await + .unwrap() + .value() + .header + .title, + first.header.title + ); drop(permit); } #[gpui::test] @@ -285,6 +321,7 @@ fn a_scoped_document_uses_the_ordinary_local_connector_without_changing_its_memb args: Vec::new(), env: Default::default(), inherit_env: Vec::new(), + hub: None, }; let registration = project .register_bridge(&(client.clone() as Client), cx) @@ -319,105 +356,7 @@ fn project_scope(index: u8) -> ContextId { ) .unwrap() } -#[gpui::test] -fn retained_catalog_generations_consume_the_installation_limit_until_dropped( - cx: &mut TestAppContext, -) { - let (general, _directory, repository, _, _) = scoped_fixture(cx); - cx.run_until_parked(); - let mut retained = Vec::new(); - for _ in 0..32 { - retained.push(cx.update(|cx| general.catalog_snapshot(cx).unwrap())); - if retained.len() < 32 { - cx.update(|cx| general.try_reload_history(cx).unwrap()); - cx.run_until_parked(); - } - } - let generation = retained.last().unwrap().generation(); - let attempts = repository.operations().len(); - cx.update(|cx| { - assert!(matches!( - general.try_reload_history(cx), - Err(crate::CatalogLoadError::Capacity) - )); - assert_eq!( - general.catalog_snapshot(cx).unwrap().generation(), - generation - ); - assert!(matches!( - general.for_scope(project_scope(1)).try_reload_history(cx), - Err(crate::CatalogLoadError::Capacity) - )); - }); - cx.run_until_parked(); - assert_eq!(repository.operations().len(), attempts); - drop(retained.remove(0)); - cx.update(|cx| general.try_reload_history(cx).unwrap()); - cx.run_until_parked(); - assert!(cx.update(|cx| general.catalog_snapshot(cx).unwrap().generation()) > generation); -} -#[gpui::test] -fn pending_catalog_requests_share_the_same_installation_capacity(cx: &mut TestAppContext) { - let (general, _directory, repository, _, _) = scoped_fixture(cx); - // General's initial catalog is already reserved, even before its work runs. - cx.update(|cx| { - for index in 1..32 { - general - .for_scope(project_scope(index)) - .try_reload_history(cx) - .unwrap(); - } - assert!(matches!( - general.for_scope(project_scope(32)).try_reload_history(cx), - Err(crate::CatalogLoadError::Capacity) - )); - }); - cx.run_until_parked(); - assert_eq!( - repository - .operations() - .iter() - .filter(|operation| **operation == StorageOperation::Catalog) - .count(), - 32 - ); - // Completed unretained LRU entries can make room without multiplying cache size. - cx.update(|cx| { - general - .for_scope(project_scope(32)) - .try_reload_history(cx) - .unwrap() - }); - cx.run_until_parked(); - assert!( - cx.update(|cx| general.for_scope(project_scope(32)).catalog_snapshot(cx)) - .is_some() - ); -} -#[gpui::test] -fn an_old_catalog_load_cannot_replace_a_newer_scope_generation(cx: &mut TestAppContext) { - let (general, _directory, repository, _, _) = scoped_fixture(cx); - cx.run_until_parked(); - let project = general.for_scope(project_chat().header.scope); - let release = repository.block_next(StorageOperation::Catalog); - cx.update(|cx| project.reload_history(cx)); - cx.run_until_parked(); - let mut chat = project_chat(); - chat.header.title = Some("Current generation".into()); - repository.seed_chat(&chat).unwrap(); - cx.update(|cx| project.reload_history(cx)); - cx.run_until_parked(); - let latest = cx.update(|cx| project.catalog_snapshot(cx).unwrap()); - chat.header.title = Some("Late stale generation".into()); - repository.seed_chat(&chat).unwrap(); - let _ = release.send(Ok(())); - cx.run_until_parked(); - cx.update(|cx| { - let actual = project.catalog_snapshot(cx).unwrap(); - assert_eq!(actual.generation(), latest.generation()); - assert_eq!(actual.rows()[0].title(), "Current generation"); - }); -} +mod catalog_capacity; #[gpui::test] fn closed_clients_and_mismatched_chat_arguments_cannot_reach_native_ports(cx: &mut TestAppContext) { let (general, _directory, _, connector, resolver) = scoped_fixture(cx); @@ -430,6 +369,7 @@ fn closed_clients_and_mismatched_chat_arguments_cannot_reach_native_ports(cx: &m args: Vec::new(), env: Default::default(), inherit_env: Vec::new(), + hub: None, }; cx.update(|cx| { assert!(matches!( @@ -529,7 +469,7 @@ fn retained_catalog_rows_hold_the_original_accounted_payload_without_cloning_sum let generation = row.generation(); drop(first); let mut retained = Vec::new(); - for index in 1..32 { + for index in 1..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS as u8 { let context = general.for_scope(project_scope(index)); cx.update(|cx| context.try_reload_history(cx).unwrap()); cx.run_until_parked(); @@ -566,32 +506,39 @@ fn prepared_payload_handoff_requires_its_original_claim_and_leaves_other_records let window = cx.new(|_| ()); cx.update(|cx| { assert_eq!(general.catalog_snapshot(cx).unwrap().rows().len(), 2); + let payload = general.history_admission(cx).reserve_payload().unwrap(); let claim = ownership::owned_claim( general .claim_document(first.header.id.clone(), window.entity_id(), cx) .unwrap(), ); assert!(matches!( - other_runtime.take_prepared_chat(&claim, cx), + other_runtime.take_prepared_chat(&claim, payload.clone(), cx), Err(crate::ChatOpenError::StaleClaim(_)) )); assert!(matches!( general .for_scope(project_scope(1)) - .take_prepared_chat(&claim, cx), + .take_prepared_chat(&claim, payload.clone(), cx), Err(crate::ChatOpenError::Storage( AgentStorageError::ScopeMismatch { .. } )) )); assert_eq!( general - .take_prepared_chat(&claim, cx) + .take_prepared_chat(&claim, payload.clone(), cx) .unwrap() .unwrap() + .value() .draft, first.draft ); - assert!(general.take_prepared_chat(&claim, cx).unwrap().is_none()); + assert!( + general + .take_prepared_chat(&claim, payload.clone(), cx) + .unwrap() + .is_none() + ); let other = ownership::owned_claim( general .claim_document(second.header.id.clone(), window.entity_id(), cx) @@ -599,9 +546,14 @@ fn prepared_payload_handoff_requires_its_original_claim_and_leaves_other_records ); assert_eq!( general - .take_prepared_chat(&other, cx) + .take_prepared_chat( + &other, + general.history_admission(cx).reserve_payload().unwrap(), + cx + ) .unwrap() .unwrap() + .value() .header .id, second.header.id diff --git a/crates/agent-runtime/src/documents/history_tests/contexts/catalog_capacity.rs b/crates/agent-runtime/src/documents/history_tests/contexts/catalog_capacity.rs new file mode 100644 index 00000000..f42ba662 --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/contexts/catalog_capacity.rs @@ -0,0 +1,291 @@ +//! Catalog generations, shared slots and retained metadata allocation. +use super::*; +use crate::history_admission::HistoryAdmissionError; + +#[gpui::test] +fn retained_catalog_generations_consume_the_installation_limit_until_dropped( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let mut retained = Vec::new(); + for _ in 0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + retained.push(cx.update(|cx| general.catalog_snapshot(cx).unwrap())); + if retained.len() < crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + } + } + let generation = retained.last().unwrap().generation(); + let attempts = repository.operations().len(); + cx.update(|cx| { + assert!(matches!( + general.try_reload_history(cx), + Err(crate::CatalogLoadError::Capacity) + )); + assert_eq!( + general.catalog_snapshot(cx).unwrap().generation(), + generation + ); + assert!(matches!( + general.for_scope(project_scope(1)).try_reload_history(cx), + Err(crate::CatalogLoadError::Capacity) + )); + }); + cx.run_until_parked(); + assert_eq!(repository.operations().len(), attempts); + drop(retained.remove(0)); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx).unwrap().generation()) > generation); +} +#[gpui::test] +fn pending_catalog_requests_share_the_same_installation_byte_capacity(cx: &mut TestAppContext) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + // General's initial scan already owns its worst-case metadata allocation, + // including before the background work's first poll. + let mut admitted = 1; + let mut refused = None; + cx.update(|cx| { + for index in 1..32 { + match general + .for_scope(project_scope(index)) + .try_reload_history(cx) + { + Ok(()) => admitted += 1, + Err(crate::CatalogLoadError::ResidentCapacity { .. }) => { + refused = Some(index); + break; + } + Err(error) => panic!("unexpected catalog refusal: {error}"), + } + } + }); + assert!(admitted > 1 && admitted < 32); + let refused = refused.expect("bounded pending metadata exhausts bytes before32slots"); + cx.run_until_parked(); + assert_eq!( + repository + .operations() + .iter() + .filter(|operation| **operation == StorageOperation::Catalog) + .count(), + admitted + ); + // Each completed result transfers to a smaller immutable retained allocation. + cx.update(|cx| { + general + .for_scope(project_scope(refused)) + .try_reload_history(cx) + .unwrap() + }); + cx.run_until_parked(); + assert!( + cx.update(|cx| general + .for_scope(project_scope(refused)) + .catalog_snapshot(cx)) + .is_some() + ); +} +#[gpui::test] +fn an_old_catalog_load_cannot_replace_a_newer_scope_generation(cx: &mut TestAppContext) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let project = general.for_scope(project_chat().header.scope); + let release = repository.block_next(StorageOperation::Catalog); + cx.update(|cx| project.reload_history(cx)); + cx.run_until_parked(); + let mut chat = project_chat(); + chat.header.title = Some("Current generation".into()); + repository.seed_chat(&chat).unwrap(); + cx.update(|cx| project.reload_history(cx)); + cx.run_until_parked(); + let latest = cx.update(|cx| project.catalog_snapshot(cx).unwrap()); + chat.header.title = Some("Late stale generation".into()); + repository.seed_chat(&chat).unwrap(); + let _ = release.send(Ok(())); + cx.run_until_parked(); + cx.update(|cx| { + let actual = project.catalog_snapshot(cx).unwrap(); + assert_eq!(actual.generation(), latest.generation()); + assert_eq!(actual.rows()[0].title(), "Current generation"); + }); +} + +#[gpui::test] +fn a_retained_catalog_row_keeps_metadata_charged_until_its_last_reference_drops( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let mut saved = snapshot().metadata.clone(); + saved.header.title = Some("界".repeat(512)); + saved.header.name = saved.header.title.clone(); + saved.model = saved.header.title.clone(); + repository.seed_chat(&saved).unwrap(); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + let snapshot = cx.update(|cx| general.catalog_snapshot(cx).unwrap()); + let row = snapshot.row(0).unwrap(); + let second = row.clone(); + drop(snapshot); + cx.update(|cx| general.delete_chat(saved.header.id, cx)); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + let incoming = crate::history_admission::MAX_NORMAL_HISTORY_BYTES - 8 * 1024; + cx.update(|cx| { + assert!(matches!( + admission.reserve_unowned_bytes(incoming, cx), + Err(HistoryAdmissionError::ResidentCapacity { .. }) + )) + }); + drop(row); + cx.update(|cx| assert!(admission.reserve_unowned_bytes(incoming, cx).is_err())); + assert_eq!(second.summary().name.as_ref().unwrap(), &"界".repeat(512)); + drop(second); + cx.update(|cx| drop(admission.reserve_unowned_bytes(incoming, cx).unwrap())); +} + +#[gpui::test] +fn replacing_a_loading_generation_keeps_its_native_read_budget_until_actual_completion( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + let release = repository.block_next(StorageOperation::Catalog); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + let latest = cx.update(|cx| general.catalog_snapshot(cx).unwrap()); + let generation = latest.generation(); + cx.update(|cx| { + assert!(matches!( + admission.reserve_unowned_bytes(56 * 1024 * 1024, cx), + Err(HistoryAdmissionError::ResidentCapacity { .. }) + )) + }); + release.send(Ok(())).unwrap(); + cx.run_until_parked(); + assert_eq!( + cx.update(|cx| general.catalog_snapshot(cx).unwrap().generation()), + generation + ); + cx.update(|cx| { + drop( + admission + .reserve_unowned_bytes(56 * 1024 * 1024, cx) + .unwrap(), + ) + }); +} + +#[gpui::test] +fn catalog_work_observes_the_same_retained_remote_bytes_before_native_start( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + let pending = cx.update(|cx| { + admission + .reserve_unowned_bytes(56 * 1024 * 1024, cx) + .unwrap() + }); + let attempts = repository.operations().len(); + cx.update(|cx| { + assert!(matches!( + general.try_reload_history(cx), + Err(crate::CatalogLoadError::ResidentCapacity { .. }) + )) + }); + cx.run_until_parked(); + assert_eq!(repository.operations().len(), attempts); + drop(pending); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert_eq!(repository.operations().len(), attempts + 1); +} + +#[gpui::test] +fn byte_pressure_reclaims_an_unretained_catalog_payload_before_refusing_shared_history( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_some()); + let incoming = cx.update(|cx| { + admission + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap() + }); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_none()); + drop(incoming); + cx.update(|cx| general.ensure_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_some()); +} + +#[gpui::test] +fn a_catalog_reload_reclaims_its_unpinned_cache_without_reentering_runtime( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let old = general.for_scope(project_scope(1)); + for index in 0..nocterm_ai::history::MAX_RESTORED { + let mut chat = project_chat(); + chat.header.scope = old.scope(); + chat.header.id = format!("00000000-0000-0000-0000-{index:012x}"); + chat.header.title = Some("界".repeat(512)); + chat.header.name = chat.header.title.clone(); + chat.model = chat.header.title.clone(); + repository.seed_chat(&chat).unwrap(); + } + cx.update(|cx| old.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert_eq!( + cx.update(|cx| old.catalog_snapshot(cx).unwrap().rows().len()), + nocterm_ai::history::MAX_RESTORED + ); + let admission = cx.update(|cx| general.history_admission(cx)); + let remote = cx.update(|cx| { + admission + // Leaves room for exactly one worst-case catalog reload, so the + // second scope fits only if the unpinned first scope is reclaimed. + .reserve_unowned_bytes( + crate::history_admission::MAX_NORMAL_HISTORY_BYTES + - crate::catalog::CATALOG_WORK_BYTES, + cx, + ) + .unwrap() + }); + let next = general.for_scope(project_scope(2)); + cx.update(|cx| next.try_reload_history(cx).unwrap()); + assert!(cx.update(|cx| old.catalog_snapshot(cx)).is_none()); + cx.run_until_parked(); + assert!(cx.update(|cx| next.catalog_snapshot(cx)).is_some()); + drop(remote); +} + +#[gpui::test] +fn byte_pressure_reclaims_every_cold_scope_offered_by_one_catalog_owner(cx: &mut TestAppContext) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let project = general.for_scope(project_scope(1)); + cx.update(|cx| project.ensure_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_some()); + assert!(cx.update(|cx| project.catalog_snapshot(cx)).is_some()); + let admission = cx.update(|cx| general.history_admission(cx)); + let incoming = cx.update(|cx| { + admission + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap() + }); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_none()); + assert!(cx.update(|cx| project.catalog_snapshot(cx)).is_none()); + drop(incoming); +} diff --git a/crates/agent-runtime/src/documents/history_tests/environments.rs b/crates/agent-runtime/src/documents/history_tests/environments.rs new file mode 100644 index 00000000..9adfe6b9 --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/environments.rs @@ -0,0 +1,221 @@ +//! Retained Local account sources fence documents, readers and shared budgets. +use super::*; +use nocterm_ai::{local_environment::LocalEnvironmentAccess, storage::AgentStorageError}; +use nocterm_hubs_model::{ + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, + LocalAccountSourceFactory, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, +}; + +fn partition(hub: u8, account: &str) -> LocalAccountPartition { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("same-instance-id").unwrap(), + Fingerprint::from_bytes([hub; 32]), + ), + AccountIdentity::new( + NamespaceId::new(account).unwrap(), + Fingerprint::from_bytes([3; 32]), + ), + )) +} +fn bind(base: &RuntimeHandle, factory: &ScriptedLocalAccountFactory) -> RuntimeHandle { + base.for_environment(LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap()) + .unwrap() +} +fn chat(handle: &RuntimeHandle, title: &str) -> Arc { + let mut metadata = snapshot().metadata.clone(); + metadata.version = 3; + metadata.header.environment = handle.environment().unwrap().clone(); + metadata.header.title = Some(title.into()); + Arc::new(SharedChat { + metadata, + queue: Vec::new(), + }) +} +#[gpui::test] +fn identical_ids_in_different_authenticated_hubs_have_independent_writers_and_catalogs( + cx: &mut TestAppContext, +) { + let (base, _directory, repository, _, _) = contexts::scoped_fixture(cx); + let first = ScriptedLocalAccountFactory::default(); + let second = ScriptedLocalAccountFactory::default(); + first.sign_in(partition(1, "same-account-id")); + second.sign_in(partition(2, "same-account-id")); + let a = bind(&base, &first); + let b = bind(&base, &second); + let a_chat = chat(&a, "Hub A"); + let b_chat = chat(&b, "Hub B"); + let a_client = ProbeClient::new(cx, a_chat.clone()); + let b_client = ProbeClient::new(cx, b_chat.clone()); + cx.update(|cx| { + ownership::register_bound(&a, a_client.clone(), cx).unwrap(); + ownership::register_bound(&b, b_client.clone(), cx).unwrap(); + a.save_chat(a_chat.clone(), a_client.id(), 1, cx).unwrap(); + b.save_chat(b_chat.clone(), b_client.id(), 1, cx).unwrap(); + }); + cx.run_until_parked(); + cx.update(|cx| { + a.reload_history(cx); + b.reload_history(cx); + }); + cx.run_until_parked(); + cx.update(|cx| { + assert_eq!(a.catalog_snapshot(cx).unwrap().rows()[0].title(), "Hub A"); + assert_eq!(b.catalog_snapshot(cx).unwrap().rows()[0].title(), "Hub B"); + a.delete_chat(a_chat.header.id.clone(), cx); + }); + cx.run_until_parked(); + assert!( + repository + .encoded_in_environment(&a.chat_scope().unwrap(), &a_chat.header.id) + .is_none() + ); + assert!( + repository + .encoded_in_environment(&b.chat_scope().unwrap(), &b_chat.header.id) + .is_some() + ); + assert!( + repository.encoded(&a_chat.header.id).is_none(), + "hub Local never writes ThisComputer" + ); +} +#[gpui::test] +async fn deferred_reader_refuses_the_old_owner_after_a_b_a_and_does_not_retarget( + cx: &mut TestAppContext, +) { + let (base, _directory, repository, _, _) = contexts::scoped_fixture(cx); + let factory = ScriptedLocalAccountFactory::default(); + factory.sign_in(partition(1, "alice")); + let a = bind(&base, &factory); + let original = chat(&a, "A original"); + repository.seed_chat(&original.metadata).unwrap(); + let reader = cx.update(|cx| a.history_reader(cx)); + let release = repository.block_next(StorageOperation::Load(original.header.id.clone())); + let pending = cx + .background_executor + .spawn(async move { reader.prepare_load(original.header.id.clone()).await }); + cx.run_until_parked(); + factory.sign_in(partition(1, "bob")); + let b = bind(&base, &factory); + factory.sign_in(partition(1, "alice")); + let new_a = bind(&base, &factory); + release.send(Ok(())).unwrap(); + let (loaded, permit) = pending.await; + assert!(matches!(loaded, Err(AgentStorageError::AccountSource(_)))); + drop(permit); + assert!(a.check_current().is_err()); + assert!(b.check_current().is_err()); + assert!(new_a.check_current().is_ok()); + let attempts = repository.operations().len(); + let (loaded, permit) = cx + .update(|cx| a.history_reader(cx)) + .prepare_load(snapshot().header.id.clone()) + .await; + assert!(loaded.is_err()); + drop(permit); + assert_eq!( + repository.operations().len(), + attempts, + "stale reads fail before repository I/O" + ); +} +#[gpui::test] +fn stale_claims_and_native_startup_cannot_acquire_the_replacement_account(cx: &mut TestAppContext) { + let (base, _directory, _, connector, _) = contexts::scoped_fixture(cx); + let factory = ScriptedLocalAccountFactory::default(); + factory.sign_in(partition(1, "alice")); + let old = bind(&base, &factory); + let client = ProbeClient::new(cx, chat(&old, "original")); + let claim = cx.update(|cx| { + ownership::owned_claim( + old.claim_document(snapshot().header.id.clone(), client.id(), cx) + .unwrap(), + ) + }); + factory.sign_in(partition(1, "bob")); + cx.update(|cx| { + assert!(old.commit_document(claim, client.clone(), cx).is_err()); + assert!(old.request_activation(client.clone(), cx).is_err()); + assert!( + old.register_bridge(&(client.clone() as Client), cx) + .is_err() + ); + }); + cx.run_until_parked(); + assert!(connector.requests().is_empty()); + let current = bind(&base, &factory); + cx.update(|cx| { + let claim = ownership::owned_claim( + current + .claim_document(snapshot().header.id.clone(), client.id(), cx) + .unwrap(), + ); + assert_eq!(claim.key().environment(), current.environment().unwrap()); + }); +} +#[gpui::test] +fn all_account_context_views_consume_one_installation_catalog_budget(cx: &mut TestAppContext) { + let (base, _directory, repository, _, _) = contexts::scoped_fixture(cx); + cx.run_until_parked(); + let mut retained = Vec::new(); + let mut factories = Vec::new(); + for index in 0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS as u8 { + let factory = ScriptedLocalAccountFactory::default(); + factory.sign_in(partition(index, "alice")); + let handle = bind(&base, &factory); + cx.update(|cx| handle.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + retained.push(cx.update(|cx| handle.catalog_snapshot(cx).unwrap())); + factories.push(factory); + } + let attempts = repository.operations().len(); + cx.update(|cx| { + assert!(matches!( + base.try_reload_history(cx), + Err(crate::CatalogLoadError::Capacity) + )) + }); + assert_eq!(repository.operations().len(), attempts); + drop(retained.remove(0)); + cx.update(|cx| base.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| base.catalog_snapshot(cx)).is_some()); +} + +#[gpui::test] +fn resident_history_budget_is_shared_across_accounts_and_windows(cx: &mut TestAppContext) { + const MIB: usize = 1024 * 1024; + let (base, _directory, _, _, _) = contexts::scoped_fixture(cx); + let a_factory = ScriptedLocalAccountFactory::default(); + let b_factory = ScriptedLocalAccountFactory::default(); + a_factory.sign_in(partition(1, "alice")); + b_factory.sign_in(partition(2, "alice")); + let a = bind(&base, &a_factory); + let b = bind(&base, &b_factory); + let first = ProbeClient::new(cx, chat(&a, "A")); + let requester = ProbeClient::new(cx, chat(&b, "B")); + first + .history + .set(Some(crate::HistoryUsage::new(48 * MIB, 1, true))); + first.allow_history_eviction.set(true); + requester + .history + .set(Some(crate::HistoryUsage::new(0, 2, false))); + cx.update(|cx| { + ownership::register_bound(&a, first.clone(), cx).unwrap(); + ownership::register_bound(&b, requester.clone(), cx).unwrap(); + assert!(matches!( + b.admit_history(requester.id(), 24 * MIB, cx), + Err(AgentStorageError::ResidentCapacity { .. }) + )); + first + .history + .set(Some(crate::HistoryUsage::new(48 * MIB, 1, false))); + b.admit_history(requester.id(), 24 * MIB, cx).unwrap(); + assert!(first.history.get().is_none()); + }); +} diff --git a/crates/agent-runtime/src/documents/history_tests/local_launch.rs b/crates/agent-runtime/src/documents/history_tests/local_launch.rs index 418659a3..a9b9070e 100644 --- a/crates/agent-runtime/src/documents/history_tests/local_launch.rs +++ b/crates/agent-runtime/src/documents/history_tests/local_launch.rs @@ -4,7 +4,7 @@ use nocterm_ai::{ AgentConnection, AgentInfo, AgentResourceSettings, AiSettings, SandboxMode, history::ContextId, model::auth::{AuthMethod, TerminalSetup}, - testing::{ScriptedUsageSource, ScriptedWorkdirResolver}, + testing::ScriptedWorkdirResolver, }; struct LocalLaunch { @@ -44,7 +44,6 @@ impl LocalLaunch { connector.clone(), storage, directory.path(), - Arc::new(ScriptedUsageSource::default()), resolver.clone(), cx, ); @@ -126,6 +125,7 @@ fn launch() -> AgentLaunch { args: vec!["--base".into()], env: Default::default(), inherit_env: vec![], + hub: None, } } fn auth_method() -> AuthMethod { diff --git a/crates/agent-runtime/src/documents/history_tests/ownership.rs b/crates/agent-runtime/src/documents/history_tests/ownership.rs index 6b7ab7d1..641a5ca6 100644 --- a/crates/agent-runtime/src/documents/history_tests/ownership.rs +++ b/crates/agent-runtime/src/documents/history_tests/ownership.rs @@ -8,7 +8,7 @@ pub(super) fn register( client: Client, cx: &mut Context, ) -> Result<(), crate::ChatOpenError> { - ChatStore::require_client_scope(ContextId::General, &client, cx)?; + ChatStore::require_client_scope(ContextId::General.into(), &client, cx)?; let key = client .state(cx) .ok_or(crate::ChatOpenError::Closed)? diff --git a/crates/agent-runtime/src/documents/history_tests/residency.rs b/crates/agent-runtime/src/documents/history_tests/residency.rs index 69a4937f..97bee38c 100644 --- a/crates/agent-runtime/src/documents/history_tests/residency.rs +++ b/crates/agent-runtime/src/documents/history_tests/residency.rs @@ -5,6 +5,10 @@ use nocterm_ai::{ storage::AgentStorageError, }; const MIB: usize = 1024 * 1024; +use crate::{ + HistoryUsage, + history_admission::{HistoryAdmissionError, testing::HistoryProbe}, +}; fn client_in(scope: ContextId, cx: &mut TestAppContext) -> Rc { let mut metadata = snapshot().metadata.clone(); @@ -18,6 +22,68 @@ fn client_in(scope: ContextId, cx: &mut TestAppContext) -> Rc { }), ) } + +#[gpui::test] +fn transferring_a_unique_buffer_releases_only_its_unused_reservation(cx: &mut TestAppContext) { + let admission = HistoryProbe::installation(); + let reserved = cx.update(|cx| admission.reserve_unowned_bytes(32 * MIB, cx).unwrap()); + let retained = HistoryProbe::transfer_buffer(&admission, reserved, MIB).unwrap(); + assert_eq!(retained.bytes(), MIB); + cx.update(|cx| drop(admission.reserve_unowned_bytes(60 * MIB, cx).unwrap())); +} + +#[gpui::test] +fn a_shared_buffer_keeps_its_original_charge_until_shared_owners_release_it( + cx: &mut TestAppContext, +) { + let admission = HistoryProbe::installation(); + let reserved = cx.update(|cx| admission.reserve_unowned_bytes(32 * MIB, cx).unwrap()); + let original = reserved.clone(); + let retained = HistoryProbe::transfer_buffer(&admission, reserved, MIB).unwrap(); + assert_eq!(retained.bytes(), 32 * MIB); + cx.update(|cx| { + assert!(matches!( + admission.reserve_unowned_bytes(40 * MIB, cx), + Err(HistoryAdmissionError::ResidentCapacity { .. }) + )) + }); + drop(original); + let retained = HistoryProbe::transfer_buffer(&admission, retained, MIB).unwrap(); + assert_eq!(retained.bytes(), MIB); + cx.update(|cx| drop(admission.reserve_unowned_bytes(40 * MIB, cx).unwrap())); +} + +#[gpui::test] +fn buffer_transfer_refuses_foreign_allocations_and_growth_without_releasing_other_owners( + cx: &mut TestAppContext, +) { + let admission = HistoryProbe::installation(); + let foreign_owner = HistoryProbe::installation(); + let foreign = cx.update(|cx| foreign_owner.reserve_unowned_bytes(MIB, cx).unwrap()); + let retained = foreign.clone(); + assert!(matches!( + HistoryProbe::transfer_buffer(&admission, foreign, MIB / 2), + Err(HistoryAdmissionError::ForeignAllocation) + )); + assert!( + matches!(HistoryProbe::transfer_buffer(&foreign_owner, retained.clone(), MIB * 2), Err(HistoryAdmissionError::AllocationGrowth { reserved, requested }) if reserved==MIB && requested==MIB*2) + ); + cx.update(|cx| { + assert!( + foreign_owner + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .is_err() + ) + }); + drop(retained); + cx.update(|cx| { + drop( + foreign_owner + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap(), + ) + }); +} fn scope(index: u8) -> ContextId { serde_json::from_value(serde_json::json!({"kind":"project", "id": format!("{index:032x}")})) .unwrap() @@ -90,7 +156,7 @@ fn remaining_prepared_payloads_count_until_their_claimed_handoff(cx: &mut TestAp ); cx.update(|cx| { ownership::register_bound(&general, client.clone(), cx).unwrap(); - let incoming = MAX_RESIDENT_BYTES - retained + 1; + let incoming = MAX_RESIDENT_BYTES - retained + 64 * 1024; assert!(matches!( general.admit_history(client.id(), incoming, cx), Err(AgentStorageError::ResidentCapacity { .. }) @@ -100,7 +166,8 @@ fn remaining_prepared_payloads_count_until_their_claimed_handoff(cx: &mut TestAp .claim_document(prepared.header.id, client.id(), cx) .unwrap(), ); - drop(general.take_prepared_chat(&claim, cx).unwrap()); + let slot = general.history_admission(cx).reserve_payload().unwrap(); + drop(general.take_prepared_chat(&claim, slot, cx).unwrap()); general.admit_history(client.id(), incoming, cx).unwrap(); }); } @@ -147,15 +214,291 @@ fn detached_active_and_latest_writer_payloads_remain_in_hydration_admission( .unwrap(); general.unregister_document(writer_client.id(), cx).unwrap(); assert!(matches!( - project.admit_history(requester.id(), MAX_RESIDENT_BYTES - retained + 1, cx), + project.admit_history( + requester.id(), + MAX_RESIDENT_BYTES - retained + 64 * 1024, + cx + ), Err(AgentStorageError::ResidentCapacity { .. }) )); }); + drop(first); + drop(writer_client); release.send(Ok(())).unwrap(); cx.run_until_parked(); cx.update(|cx| { project - .admit_history(requester.id(), MAX_RESIDENT_BYTES - retained + 1, cx) + .admit_history( + requester.id(), + MAX_RESIDENT_BYTES - retained + 64 * 1024, + cx, + ) .unwrap() }); } + +#[gpui::test] +fn remote_admission_evicts_hidden_local_history_and_local_admission_evicts_remote_history( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let protected = client_in(general.scope(), cx); + let hidden_scope = general.for_scope(scope(1)); + let hidden = client_in(hidden_scope.scope(), cx); + protected + .history + .set(Some(HistoryUsage::new(24 * MIB, 1, true))); + hidden + .history + .set(Some(HistoryUsage::new(24 * MIB, 2, false))); + hidden.allow_history_eviction.set(true); + let remote = HistoryProbe::default(); + cx.update(|cx| { + ownership::register_bound(&general, protected.clone(), cx).unwrap(); + ownership::register_bound(&hidden_scope, hidden.clone(), cx).unwrap(); + let admission = general.history_admission(cx); + let registration = admission.register(remote.participant()).unwrap(); + let buffer = admission + .reserve_bytes(®istration, 24 * MIB, cx) + .unwrap(); + assert!(hidden.history.get().is_none()); + assert!(protected.history.get().is_some()); + remote.set_history(HistoryUsage::new(24 * MIB, 3, false), true); + drop(buffer); + hidden_scope + .admit_history(hidden.id(), 24 * MIB, cx) + .unwrap(); + assert!(remote.history().is_none()); + assert_eq!(remote.evictions(), 1); + assert!(protected.history.get().is_some()); + }); +} + +#[gpui::test] +fn pending_remote_buffer_clones_stay_charged_until_the_last_owner_drops(cx: &mut TestAppContext) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let client = client_in(general.scope(), cx); + cx.update(|cx| { + ownership::register_bound(&general, client.clone(), cx).unwrap(); + let admission = general.history_admission(cx); + let buffer = admission.reserve_unowned_bytes(40 * MIB, cx).unwrap(); + let pending = buffer.clone(); + drop(buffer); + assert!(matches!( + general.admit_history(client.id(), 32 * MIB, cx), + Err(AgentStorageError::ResidentCapacity { .. }) + )); + drop(pending); + general.admit_history(client.id(), 32 * MIB, cx).unwrap(); + }); +} + +#[gpui::test] +fn local_and_remote_payloads_reclaim_each_other_through_one_installation_cap( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let remote = HistoryProbe::default(); + remote.set_history(HistoryUsage::new(0, 1, false), true); + cx.update(|cx| { + let admission = general.history_admission(cx); + let _registration = admission.register(remote.participant()).unwrap(); + for _ in 1..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + remote.retain_payload(admission.reserve_payload().unwrap()); + } + let payload = admission.reserve_payload_reclaiming(None, cx).unwrap(); + assert!( + general.catalog_snapshot(cx).is_none(), + "oldest unpinned Local catalog was reclaimed" + ); + remote.retain_payload(payload); + assert_eq!( + remote.payload_count(), + crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS + ); + general.try_reload_history(cx).unwrap(); + assert_eq!( + remote.payload_count(), + crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS - 1, + "Local load reclaimed a remote payload" + ); + }); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx).is_some())); +} + +#[gpui::test] +fn history_eviction_callbacks_run_without_a_budget_borrow_and_nested_admission_refuses( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let remote = HistoryProbe::default(); + remote.set_history(HistoryUsage::new(40 * MIB, 1, false), true); + cx.update(|cx| { + let admission = general.history_admission(cx); + let _registration = admission.register(remote.participant()).unwrap(); + let nested = admission.clone(); + let called = Rc::new(Cell::new(false)); + let observed = called.clone(); + remote.on_evict(Rc::new(move |cx| { + // Registration may mutate budget state, while recursive eviction + // decisions must refuse instead of admitting uncounted work. + let probe = HistoryProbe::default(); + let _registration = nested.register(probe.participant()).unwrap(); + assert!(matches!( + nested.reserve_unowned_bytes(1, cx), + Err(HistoryAdmissionError::Reentrant) + )); + observed.set(true); + })); + let _buffer = admission.reserve_unowned_bytes(40 * MIB, cx).unwrap(); + assert!(called.get()); + }); +} + +#[gpui::test] +fn unchanged_payload_offers_stop_reclamation_even_when_the_callback_reports_success( + cx: &mut TestAppContext, +) { + use crate::history_admission::{HistoryAdmissionError, testing::HistoryProbe}; + let admission = HistoryProbe::installation(); + let neighbor = HistoryProbe::default(); + neighbor.set_history(crate::HistoryUsage::new(0, 0, false), true); + neighbor.report_payload_success_without_release(); + let _registration = admission.register(neighbor.participant()).unwrap(); + for _ in 0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + neighbor.retain_payload(admission.reserve_payload().unwrap()); + } + cx.update(|cx| { + assert!(matches!( + admission.reserve_payload_reclaiming(None, cx), + Err(HistoryAdmissionError::PayloadCapacity) + )) + }); + assert_eq!(neighbor.evictions(), 1); + assert_eq!( + neighbor.payload_count(), + crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS + ); +} + +#[gpui::test] +fn normal_allocations_preserve_urgent_control_capacity_inside_the_hard_installation_bounds( + cx: &mut TestAppContext, +) { + use crate::history_admission::{ + MAX_NORMAL_HISTORY_BYTES, MAX_NORMAL_HISTORY_PAYLOADS, URGENT_HISTORY_BYTES, + testing::HistoryProbe, + }; + let admission = HistoryProbe::installation(); + let neighbor = HistoryProbe::default(); + let registration = admission.register(neighbor.participant()).unwrap(); + let normal = (0..MAX_NORMAL_HISTORY_PAYLOADS) + .map(|_| admission.reserve_payload().unwrap()) + .collect::>(); + let bytes = cx.update(|cx| { + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap() + }); + assert!(admission.reserve_payload().is_err()); + let urgent = cx.update(|cx| { + admission + .reserve_urgent_control(®istration, URGENT_HISTORY_BYTES, 2, cx) + .unwrap() + }); + cx.update(|cx| { + assert!( + admission + .reserve_urgent_control(®istration, 1, 1, cx) + .is_err() + ) + }); + let clone = urgent.clone(); + drop(urgent); + cx.update(|cx| { + assert!( + admission + .reserve_urgent_control(®istration, 1, 1, cx) + .is_err() + ) + }); + drop(clone); + cx.update(|cx| { + drop( + admission + .reserve_urgent_control(®istration, URGENT_HISTORY_BYTES, 2, cx) + .unwrap(), + ) + }); + drop((normal, bytes)); +} + +#[gpui::test] +fn snapshot_arc_clones_share_one_generation_and_release_only_after_the_actual_last_drop( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let requester = client_in(general.scope(), cx); + let value = snapshot(); + let source = cx.update(|cx| general.local_history_allocations(cx)); + let allocation = cx.update(|cx| { + source + .reserve_snapshot(requester.id(), 0, value.retained_bytes().unwrap(), cx) + .unwrap() + }); + let value = allocation.retain(value).unwrap(); + let clone = value.clone(); + let admission = cx.update(|cx| general.history_admission(cx)); + let others = (0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS - 2) + .map(|_| admission.reserve_payload().unwrap()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); // catalog + snapshot +28normal + drop(value); + assert!(admission.reserve_payload().is_err()); + drop(clone); + let freed = admission.reserve_payload().unwrap(); + drop((freed, others)); +} + +#[gpui::test] +fn active_and_latest_writer_generations_use_distinct_shared_slots_until_native_completion( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let first = snapshot(); + let client = ProbeClient::new(cx, first.clone()); + let release = repository.block_next(StorageOperation::Save(first.header.id.clone())); + cx.update(|cx| { + ownership::register_bound(&general, client.clone(), cx).unwrap(); + general + .save_chat(first.clone(), client.id(), 1, cx) + .unwrap(); + }); + cx.run_until_parked(); + let second = snapshot(); + cx.update(|cx| { + *client.chat.borrow_mut() = second.clone(); + client.revision.set(2); + general + .save_chat(second.clone(), client.id(), 2, cx) + .unwrap(); + general.unregister_document(client.id(), cx).unwrap(); + }); + drop((first, second, client)); + let admission = cx.update(|cx| general.history_admission(cx)); + // The catalog is cold and invalidated by retirement; both actual writer + // generations still exist in the common store/native worker. + let retained = (0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS - 2) + .map(|_| admission.reserve_payload().unwrap()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); + release.send(Ok(())).unwrap(); + cx.run_until_parked(); + let freed = admission.reserve_payload().unwrap(); + drop((freed, retained)); +} diff --git a/crates/agent-runtime/src/documents/history_tests/usage.rs b/crates/agent-runtime/src/documents/history_tests/usage.rs index a028c5c8..b3c59cf3 100644 --- a/crates/agent-runtime/src/documents/history_tests/usage.rs +++ b/crates/agent-runtime/src/documents/history_tests/usage.rs @@ -1,104 +1,129 @@ -//! Account refresh effects through public Runtime methods and the shared source fake. +//! Plan limits arrive through the connected agent's event stream. use super::*; use nocterm_ai::{ - testing::ScriptedUsageSource, - usage::{RateLimit, UsageReadError, Window}, + AgentConnection, + model::{session::SessionNotice, usage::ContextUsage}, + usage::Window, }; -fn with_source(cx: &mut TestAppContext, source: Arc) -> Entity { - let repository = Arc::new(InMemoryAgentRepository::default()); - let storage = AgentStorage::prepared(repository.clone(), repository, Ok(Default::default())); - runtime_with_usage_source( - cx, - Arc::new(ScriptedConnector::default()), - storage, - std::path::Path::new("/unused"), - source, - Arc::new(nocterm_ai::testing::ScriptedWorkdirResolver::accept_requests()), - ) +type UsageFixture = ( + Entity, + tempfile::TempDir, + Rc, + async_channel::Sender, +); +fn connected(cx: &mut TestAppContext, agent: &str) -> UsageFixture { + let connector = Arc::new(ScriptedConnector::default()); + let (events, receiver) = async_channel::unbounded(); + connector.push(Ok(AgentConnection { + commands: closing::BlockedCommands::new(false), + events: receiver, + info: AgentInfo { + name: "Supplied agent".into(), + version: "1".into(), + capabilities: Default::default(), + auth_methods: vec![], + }, + })); + let (runtime, directory, _) = fixture_with_connector(cx, connector); + let client = ProbeClient::new(cx, snapshot()); + let launch = nocterm_agent_providers::catalog() + .get(agent) + .map(|profile| profile.default_launch()) + .unwrap_or_else(|| AgentLaunch { + id: agent.into(), + name: agent.into(), + command: "not-executed".into(), + args: vec![], + env: Default::default(), + inherit_env: vec![], + hub: None, + }); + runtime.update(cx, |runtime, cx| { + ownership::register(runtime, client.clone(), cx).unwrap(); + runtime + .connect( + client.clone(), + client.chat.borrow().header.id.clone(), + launch, + cx, + ) + .unwrap(); + }); + cx.run_until_parked(); + assert!( + client + .events + .borrow() + .iter() + .any(|event| matches!(event, SessionEvent::Connected { .. })) + ); + (runtime, directory, client, events) } -fn limit(used: f64) -> RateLimit { - RateLimit { - window: Window::Weekly, - used: Some(used), - resets_at: None, - reached: false, - } +fn usage(meta: serde_json::Value) -> AgentEvent { + AgentEvent::Session(SessionNotice::new( + "usage-session", + SessionUpdate::UsageUpdate( + ContextUsage::new(50, 100).meta( + meta.as_object() + .cloned() + .map(nocterm_ai::model::extensions::Extensions::from), + ), + ), + )) +} +fn report(kind: &str, used: f64) -> AgentEvent { + usage(serde_json::json!({"_claude/rateLimit": { + "rateLimitType": kind, "utilization": used, "status": "allowed" + }})) } #[gpui::test] -fn account_refresh_coalesces_and_retains_last_limits_on_missing_report_or_failure( +fn received_limit_windows_merge_and_missing_or_malformed_reports_retain_them( cx: &mut TestAppContext, ) { - let source = Arc::new(ScriptedUsageSource::default()); - let answer = source.pending(); - let runtime = with_source(cx, source.clone()); - runtime.update(cx, |runtime, cx| { - runtime.refresh_limits("codex", cx); - runtime.refresh_limits("codex", cx); - }); - cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); - assert_eq!(source.requests()[0].id, "codex"); - answer.send(Ok(Some(vec![limit(0.38)]))).unwrap(); - cx.run_until_parked(); - runtime.read_with(cx, |runtime, _| { - assert_eq!( - runtime - .limits("codex") - .unwrap() - .iter() - .cloned() - .collect::>(), - vec![limit(0.38)] - ) - }); - source.push(Ok(None)); - runtime.update(cx, |runtime, cx| runtime.refresh_limits("codex", cx)); - cx.run_until_parked(); - source.push(Err(UsageReadError::read( - "/unused/report", - std::io::Error::from(std::io::ErrorKind::PermissionDenied), - ))); - runtime.update(cx, |runtime, cx| runtime.refresh_limits("codex", cx)); + let (runtime, _directory, _client, events) = connected(cx, "claude"); + runtime.read_with(cx, |runtime, _| assert!(runtime.limits("claude").is_none())); + for event in [ + report("seven_day", 0.38), + report("five_hour", 0.12), + report("seven_day", 0.42), + usage(serde_json::json!({})), + usage(serde_json::json!({"_claude/rateLimit": "invalid"})), + report("unknown_window", 0.99), + ] { + events.try_send(event).unwrap(); + } cx.run_until_parked(); runtime.read_with(cx, |runtime, _| { - assert_eq!( - runtime - .limits("codex") - .unwrap() - .iter() - .cloned() - .collect::>(), - vec![limit(0.38)] - ) + let limits: Vec<_> = runtime.limits("claude").unwrap().iter().collect(); + assert_eq!(limits.len(), 2); + assert_eq!(limits[0].window, Window::FiveHour); + assert_eq!(limits[0].used, Some(0.12)); + assert_eq!(limits[1].window, Window::Weekly); + assert_eq!(limits[1].used, Some(0.42)); + assert!(runtime.limits("codex").is_none()); }); - assert_eq!(source.requests().len(), 3); } #[gpui::test] -fn stopped_runtime_rejects_late_usage_delivery_and_unknown_agent_has_no_request( - cx: &mut TestAppContext, -) { - let source = Arc::new(ScriptedUsageSource::default()); - let answer = source.pending(); - let runtime = with_source(cx, source.clone()); - runtime.update(cx, |runtime, cx| { - runtime.refresh_limits("unknown-agent", cx) - }); - cx.run_until_parked(); - assert!(source.requests().is_empty()); - runtime.update(cx, |runtime, cx| runtime.refresh_limits("codex", cx)); - cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); +fn agents_without_a_limit_metadata_profile_do_not_fabricate_plan_limits(cx: &mut TestAppContext) { + for agent in ["codex", "unknown-agent"] { + let (runtime, _directory, client, events) = connected(cx, agent); + events.try_send(report("seven_day", 0.99)).unwrap(); + cx.run_until_parked(); + runtime.read_with(cx, |runtime, _| assert!(runtime.limits(agent).is_none())); + assert!(client.events.borrow().iter().any(|event| matches!(event, + SessionEvent::Update(notice) if matches!(notice.update, SessionUpdate::UsageUpdate(_)) + ))); + } +} + +#[gpui::test] +fn a_stopped_connection_does_not_accept_late_plan_limit_reports(cx: &mut TestAppContext) { + let (runtime, _directory, _client, events) = connected(cx, "claude"); let _drain = runtime.update(cx, |runtime, cx| runtime.shutdown(cx)); + let _ = events.try_send(report("seven_day", 0.99)); cx.run_until_parked(); - let _ = answer.send(Ok(Some(vec![limit(0.99)]))); - cx.run_until_parked(); - runtime.update(cx, |runtime, cx| { - assert!(runtime.limits("codex").is_none()); - runtime.refresh_limits("codex", cx); - }); - cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); + runtime.read_with(cx, |runtime, _| assert!(runtime.limits("claude").is_none())); } diff --git a/crates/agent-runtime/src/documents/history_tests/workspace.rs b/crates/agent-runtime/src/documents/history_tests/workspace.rs index 902b1da6..ec00a059 100644 --- a/crates/agent-runtime/src/documents/history_tests/workspace.rs +++ b/crates/agent-runtime/src/documents/history_tests/workspace.rs @@ -2,7 +2,7 @@ use super::*; use nocterm_ai::{ AgentError, - testing::{ScriptedUsageSource, ScriptedWorkdirResolver}, + testing::ScriptedWorkdirResolver, workspace::{PreparedWorkspace, WorkdirError}, }; fn candidate() -> std::path::PathBuf { @@ -26,14 +26,7 @@ fn runtime_with_workspace( ) -> Entity { let repository = Arc::new(InMemoryAgentRepository::default()); let storage = AgentStorage::prepared(repository.clone(), repository, Ok(Default::default())); - runtime_with_usage_source( - cx, - connector, - storage, - &candidate(), - Arc::new(ScriptedUsageSource::default()), - resolver, - ) + runtime_with_workspace_resolver(cx, connector, storage, &candidate(), resolver) } fn connect(runtime: &Entity, cx: &mut TestAppContext) { let client = ProbeClient::new(cx, snapshot()); @@ -44,6 +37,7 @@ fn connect(runtime: &Entity, cx: &mut TestAppContext) { args: Vec::new(), env: Default::default(), inherit_env: Vec::new(), + hub: None, }; runtime.update(cx, |runtime, cx| { runtime diff --git a/crates/agent-runtime/src/documents/registry.rs b/crates/agent-runtime/src/documents/registry.rs index 491315e0..4e0256e0 100644 --- a/crates/agent-runtime/src/documents/registry.rs +++ b/crates/agent-runtime/src/documents/registry.rs @@ -2,7 +2,7 @@ use super::{CandidateStamp, Client}; use crate::ChatOpenError; use gpui::EntityId; -use nocterm_ai::execution_origin::ChatKey; +use nocterm_ai::{execution_origin::ChatKey, local_environment::LocalEnvironmentAccess}; use nocterm_store::KeyedStamp; use std::{ cell::RefCell, @@ -24,6 +24,7 @@ pub struct DocumentClaim { key: ChatKey, owner: DocumentOwner, previous: Option, + access: LocalEnvironmentAccess, } impl DocumentClaim { pub fn key(&self) -> &ChatKey { @@ -49,22 +50,44 @@ pub enum DocumentClaimOutcome { Deleted, } -#[derive(Default)] +struct RegisteredClient { + client: Client, + _history: crate::history_admission::HistoryRegistration, +} pub(super) struct DocumentRegistry { - clients: HashMap, + clients: HashMap, + admission: crate::history_admission::HistoryAdmissionHandle, owners: HashMap, deleted: HashSet, stamps: HashMap, closing: bool, sequence: u64, + access: HashMap, } impl DocumentRegistry { + pub(super) fn new(admission: crate::history_admission::HistoryAdmissionHandle) -> Self { + Self { + clients: Default::default(), + admission, + owners: Default::default(), + deleted: Default::default(), + stamps: Default::default(), + closing: false, + sequence: 0, + access: Default::default(), + } + } pub(super) fn claim( registry: &Rc>, key: ChatKey, + access: LocalEnvironmentAccess, window: EntityId, replacing: Option, ) -> Result { + access + .check_current() + .map_err(nocterm_ai::storage::AgentStorageError::from)?; + nocterm_ai::storage::require_local_environment(key.environment(), access.environment())?; let mut this = registry.borrow_mut(); if this.closing { return Err(ChatOpenError::Closed); @@ -80,7 +103,15 @@ impl DocumentRegistry { && this .clients .get(&entity) - .is_none_or(|client| !client.alive()) + .is_none_or(|client| !client.client.alive()) + { + this.retire(entity); + } + if this + .access + .get(&key) + .is_none_or(|access| access.check_current().is_err()) + && let Some(entity) = this.owners.get(&key).and_then(|owner| owner.entity) { this.retire(entity); } @@ -111,6 +142,7 @@ impl DocumentRegistry { key, owner, previous, + access, })) } fn reservation_current(&self, claim: &DocumentClaim) -> bool { @@ -124,9 +156,11 @@ impl DocumentRegistry { } self.owners.remove(&claim.key); if let Some(previous) = claim.previous - && previous - .entity - .is_some_and(|id| self.clients.get(&id).is_some_and(|client| client.alive())) + && previous.entity.is_some_and(|id| { + self.clients + .get(&id) + .is_some_and(|client| client.client.alive()) + }) { self.owners.insert(claim.key.clone(), previous); } @@ -135,6 +169,10 @@ impl DocumentRegistry { if self.closing { return Err(ChatOpenError::Closed); } + claim + .access + .check_current() + .map_err(nocterm_ai::storage::AgentStorageError::from)?; if self.deleted.contains(&claim.key) { return Err(ChatOpenError::Deleted(claim.key.clone())); } @@ -143,12 +181,18 @@ impl DocumentRegistry { } Ok(()) } + /// Makes `client` the owner `claim` reserved; the owner it replaced when that one was in + /// another window, which the chat moved from. pub(super) fn commit( &mut self, claim: &DocumentClaim, client: Client, - ) -> Result<(), ChatOpenError> { + ) -> Result, ChatOpenError> { self.require_reservation(claim)?; + claim + .access + .check_current() + .map_err(nocterm_ai::storage::AgentStorageError::from)?; if self .owners .iter() @@ -156,10 +200,20 @@ impl DocumentRegistry { { return Err(ChatOpenError::StaleClaim(claim.key.clone())); } + let history = self + .admission + .register(std::rc::Rc::new( + crate::history_admission::local::SessionHistory::new(client.clone()), + )) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage)?; if let Some(previous) = claim.previous.and_then(|owner| owner.entity) { self.clients.remove(&previous); self.stamps.remove(&claim.key); } + let moved = claim + .previous + .filter(|previous| previous.window != claim.owner.window) + .and_then(|previous| previous.entity); let owner = client.id(); self.owners.insert( claim.key.clone(), @@ -169,8 +223,36 @@ impl DocumentRegistry { ..claim.owner }, ); - self.clients.insert(owner, client); - Ok(()) + self.access.insert(claim.key.clone(), claim.access.clone()); + self.clients.insert( + owner, + RegisteredClient { + client, + _history: history, + }, + ); + Ok(moved) + } + /// The window that holds chat `entity`. + pub(super) fn window_of(&self, entity: EntityId) -> Option { + self.owners + .values() + .find(|owner| owner.entity == Some(entity)) + .map(|owner| owner.window) + } + /// The chats of `scope` windows other than `window` hold, once built. + pub(super) fn held_elsewhere( + &self, + scope: &nocterm_ai::local_environment::ChatScope, + window: EntityId, + ) -> Vec { + self.owners + .iter() + .filter(|(key, owner)| { + !owner.reserved && owner.window != window && key.chat_scope() == *scope + }) + .filter_map(|(_, owner)| Some(self.clients.get(&owner.entity?)?.client.clone())) + .collect() } pub(super) fn retire(&mut self, id: EntityId) -> Vec { let ids = self @@ -181,14 +263,42 @@ impl DocumentRegistry { .collect(); self.clients.remove(&id); self.owners.retain(|_, owner| owner.entity != Some(id)); + self.access.retain(|key, _| self.owners.contains_key(key)); self.stamps.retain(|_, (owner, _)| *owner != id); ids } pub(super) fn current(&self, id: &ChatKey, owner: EntityId) -> bool { !self.closing + && self.source_current(id) && self.owners.get(id).is_some_and(|current| !current.reserved) && self.capture_current(id, owner) } + pub(super) fn revoke_stale_sources(&mut self) -> Vec { + let keys = self + .access + .iter() + .filter(|(_, access)| access.check_current().is_err()) + .map(|(key, _)| key.clone()) + .collect::>(); + let mut clients = Vec::new(); + for key in keys { + self.access.remove(&key); + if let Some(client) = self + .owners + .get(&key) + .and_then(|owner| owner.entity) + .and_then(|id| self.clients.get(&id)) + { + clients.push(client.client.clone()); + } + } + clients + } + pub(super) fn source_current(&self, key: &ChatKey) -> bool { + self.access + .get(key) + .is_some_and(|access| access.check_current().is_ok()) + } pub(super) fn capture_current(&self, id: &ChatKey, owner: EntityId) -> bool { !self.deleted.contains(id) && self @@ -212,6 +322,7 @@ impl DocumentRegistry { self.deleted.insert(id.clone()); self.stamps.remove(&id); self.owners.remove(&id); + self.access.remove(&id); } pub(super) fn target( &self, @@ -227,18 +338,28 @@ impl DocumentRegistry { if !succeeded && self.stamps.get(stamp.key()) != Some(&(owner, stamp.tag().revision)) { return None; } - self.clients.get(&owner).cloned() + self.clients + .get(&owner) + .map(|registered| registered.client.clone()) } - pub(super) fn scope_of(&self, owner: EntityId) -> Option { + pub(super) fn scope_of( + &self, + owner: EntityId, + ) -> Option { self.owners .iter() - .find_map(|(key, current)| (current.entity == Some(owner)).then_some(key.scope())) + .find_map(|(key, current)| (current.entity == Some(owner)).then_some(key.chat_scope())) } pub(super) fn client(&self, id: EntityId) -> Option { - self.clients.get(&id).cloned() + self.clients + .get(&id) + .map(|registered| registered.client.clone()) } pub(super) fn clients(&self) -> Vec { - self.clients.values().cloned().collect() + self.clients + .values() + .map(|registered| registered.client.clone()) + .collect() } pub(super) fn contains_deleted(&self, id: &ChatKey) -> bool { self.deleted.contains(id) diff --git a/crates/agent-runtime/src/handle.rs b/crates/agent-runtime/src/handle.rs index 197bce9f..358329db 100644 --- a/crates/agent-runtime/src/handle.rs +++ b/crates/agent-runtime/src/handle.rs @@ -10,6 +10,7 @@ use nocterm_ai::{ AgentLaunch, BridgeRegistration, execution_origin::ChatKey, history::{ContextId, SavedChat, SharedChat}, + local_environment::{ChatScope, LocalEnvironment, LocalEnvironmentAccess}, }; use std::sync::Arc; @@ -17,12 +18,14 @@ use std::sync::Arc; pub struct RuntimeHandle { runtime: Entity, scope: ContextId, + access: Option, } impl RuntimeHandle { pub fn new(services: RuntimeServices, cx: &mut App) -> Self { Self { runtime: cx.new(|cx| Runtime::new(services, cx)), scope: ContextId::General, + access: Some(LocalEnvironmentAccess::this_computer()), } } /// An immutable view of the same installation service, captured before work starts. @@ -30,13 +33,81 @@ impl RuntimeHandle { Self { runtime: self.runtime.clone(), scope, + access: self.access.clone(), } } + pub fn for_environment( + &self, + access: LocalEnvironmentAccess, + ) -> Result { + access.check_current()?; + Ok(Self { + runtime: self.runtime.clone(), + scope: self.scope, + access: Some(access), + }) + } + pub fn unavailable_account(&self) -> Self { + Self { + runtime: self.runtime.clone(), + scope: self.scope, + access: None, + } + } + pub fn environment( + &self, + ) -> Result<&LocalEnvironment, nocterm_hubs_model::source_identity::LocalAccountSourceError> + { + self.access + .as_ref() + .map(LocalEnvironmentAccess::environment) + .ok_or(nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable) + } + pub fn chat_scope(&self) -> Result { + Ok(ChatScope::new(self.environment()?.clone(), self.scope)) + } + fn current_source( + &self, + ) -> Result<(), nocterm_hubs_model::source_identity::LocalAccountSourceError> { + self.access + .as_ref() + .ok_or(nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable)? + .check_current() + } + pub fn check_current(&self) -> Result<(), nocterm_ai::storage::AgentStorageError> { + self.current_source().map_err(Into::into) + } + /// Retain the original source for native workers that outlive an App callback. + pub fn captured_source( + &self, + ) -> Result { + self.check_current()?; + self.access.clone().ok_or_else(|| { + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable.into() + }) + } + /// Native source observations trigger revalidation of retained original leases. + /// Notifications are hints; each source checks its own actual retained owner. + pub fn refresh_environment_sources(&self, cx: &mut App) { + self.runtime + .update(cx, |runtime, cx| runtime.refresh_environment_sources(cx)); + } + /// Create a chat-owned registry using this installation’s supplied native adapter. + pub fn command_jobs(&self, cx: &App) -> Box { + self.runtime.read(cx).services.command_jobs.create() + } + /// The transfer port this installation supplies; every chat keeps its own book over it. + pub fn transfers(&self, cx: &App) -> Arc { + self.runtime.read(cx).services.transfers.clone() + } pub fn scope(&self) -> ContextId { self.scope } pub fn catalog_snapshot(&self, cx: &App) -> Option { - self.runtime.read(cx).catalog_snapshot(self.scope) + self.check_current().ok()?; + self.runtime + .read(cx) + .catalog_snapshot(self.chat_scope().ok()?) } pub fn observe( &self, @@ -63,15 +134,31 @@ impl RuntimeHandle { self.runtime.read(cx).favorites_error(cx) } pub fn history_reader(&self, cx: &App) -> HistoryReader { - self.runtime.read(cx).scoped_history_reader(self.scope) + let runtime = self.runtime.read(cx); + match self.chat_scope() { + Ok(scope) => runtime + .scoped_history_reader(scope) + .with_access(self.access.clone()), + Err(_) => runtime.chats.unavailable_reader(), + } } pub fn limits<'a>(&self, agent: &str, cx: &'a App) -> Option<&'a nocterm_ai::usage::Limits> { self.runtime.read(cx).limits(agent) } pub fn document_is_current(&self, chat_id: &str, owner: EntityId, cx: &App) -> bool { - self.runtime - .read(cx) - .scoped_document_is_current(&ChatKey::new(self.scope, chat_id.to_owned()), owner) + if self.check_current().is_err() { + return false; + } + self.runtime.read(cx).scoped_document_is_current( + &ChatKey::in_environment( + match self.chat_scope() { + Ok(scope) => scope, + Err(_) => return false, + }, + chat_id.to_owned(), + ), + owner, + ) } pub fn terminal_auth_request( &self, @@ -79,8 +166,23 @@ impl RuntimeHandle { method: &AuthMethod, cx: &App, ) -> Option { + self.check_current().ok()?; self.runtime.read(cx).terminal_auth_request(key, method) } + /// The same retained installation budget for Local and remote conversation sources. + pub fn history_admission(&self, cx: &App) -> crate::history_admission::HistoryAdmissionHandle { + self.runtime.read(cx).chats.history_admission() + } + /// Capture the installation and Runtime borrower identity before Local work. + pub fn local_history_allocations( + &self, + cx: &App, + ) -> crate::history_admission::LocalHistoryAllocations { + crate::history_admission::LocalHistoryAllocations::new( + self.history_admission(cx), + self.runtime.entity_id(), + ) + } /// Admit before a hydrated body mutates its original registered owner. /// All other contexts/windows and immutable writer generations are included. pub fn admit_history( @@ -89,23 +191,30 @@ impl RuntimeHandle { bytes: usize, cx: &mut App, ) -> Result<(), nocterm_ai::storage::AgentStorageError> { - let residency = - self.runtime - .read(cx) - .chats - .history_residency(self.scope, Some(requester), cx)?; - residency.admit(Some(requester), bytes, cx).map(|_| ()) + self.check_current()?; + let residency = self.runtime.read(cx).chats.history_residency( + self.chat_scope()?, + Some(requester), + cx, + )?; + residency + .admit(Some(requester), bytes, cx) + .map(|_| ()) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage) } pub fn trim_history( &self, cx: &mut App, ) -> Result, nocterm_ai::storage::AgentStorageError> { - let residency = self - .runtime - .read(cx) - .chats - .history_residency(self.scope, None, cx)?; - residency.admit(None, 0, cx) + self.check_current()?; + let residency = + self.runtime + .read(cx) + .chats + .history_residency(self.chat_scope()?, None, cx)?; + residency + .admit(None, 0, cx) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage) } /// Admission for a new decoded document is bound to its exact reservation. pub fn admit_claimed_history( @@ -114,19 +223,25 @@ impl RuntimeHandle { bytes: usize, cx: &mut App, ) -> Result<(), crate::ChatOpenError> { - nocterm_ai::storage::require_chat_scope(self.scope, claim.key().scope())?; + self.check_current()?; + nocterm_ai::storage::require_local_scope(&self.chat_scope()?, &claim.key().chat_scope())?; let residency = self.runtime.read(cx).chats.claim_residency(claim, cx)?; - residency.admit(None, bytes, cx)?; + residency + .admit(None, bytes, cx) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage)?; Ok(()) } pub fn take_prepared_chat( &self, claim: &crate::DocumentClaim, + payload: crate::history_admission::HistoryPayloadPermit, cx: &mut App, - ) -> Result, crate::ChatOpenError> { - nocterm_ai::storage::require_chat_scope(self.scope, claim.key().scope())?; - self.runtime - .update(cx, |runtime, _| runtime.take_prepared_chat(claim)) + ) -> Result>, crate::ChatOpenError> { + self.check_current()?; + nocterm_ai::storage::require_local_scope(&self.chat_scope()?, &claim.key().chat_scope())?; + self.runtime.update(cx, |runtime, cx| { + runtime.take_prepared_chat(claim, payload, cx) + }) } pub fn toggle_favorite(&self, agent: &str, option: &str, value: &str, cx: &mut App) { self.runtime.update(cx, |runtime, cx| { @@ -153,20 +268,24 @@ impl RuntimeHandle { self.runtime .update(cx, |runtime, cx| runtime.set_last_agent(agent, cx)) } - pub fn refresh_limits(&self, agent: &str, cx: &mut App) { - self.runtime - .update(cx, |runtime, cx| runtime.refresh_limits(agent, cx)) - } pub fn claim_document( &self, id: String, window: EntityId, cx: &mut App, ) -> Result { - self.runtime - .read(cx) - .claim_document(ChatKey::new(self.scope, id), window, None) + self.check_current()?; + self.runtime.read(cx).claim_local_document( + ChatKey::in_environment(self.chat_scope()?, id), + self.access + .clone() + .ok_or(nocterm_ai::storage::AgentStorageError::Interrupted)?, + window, + None, + ) } + /// Claims chat `id` in `window` in place of `expected_owner`: a restart in the same window, + /// or a move from another, where the chat must be at rest. pub fn replace_document( &self, id: String, @@ -174,8 +293,15 @@ impl RuntimeHandle { window: EntityId, cx: &mut App, ) -> Result { - self.runtime.read(cx).claim_document( - ChatKey::new(self.scope, id), + self.check_current()?; + if let Some(held) = self.runtime.read(cx).kept_by(expected_owner, window, cx) { + return Ok(crate::DocumentClaimOutcome::AlreadyOpen { window: held }); + } + self.runtime.read(cx).claim_local_document( + ChatKey::in_environment(self.chat_scope()?, id), + self.access + .clone() + .ok_or(nocterm_ai::storage::AgentStorageError::Interrupted)?, window, Some(expected_owner), ) @@ -186,7 +312,8 @@ impl RuntimeHandle { client: Client, cx: &mut App, ) -> Result<(), crate::ChatOpenError> { - nocterm_ai::storage::require_chat_scope(self.scope, claim.key().scope())?; + self.check_current()?; + nocterm_ai::storage::require_local_scope(&self.chat_scope()?, &claim.key().chat_scope())?; self.runtime .update(cx, |runtime, cx| runtime.commit_document(claim, client, cx)) } @@ -196,7 +323,7 @@ impl RuntimeHandle { cx: &mut App, ) -> Result<(), nocterm_ai::storage::AgentStorageError> { self.runtime.update(cx, |runtime, cx| { - runtime.unregister_scoped_document(self.scope, id, cx) + runtime.unregister_scoped_document(self.chat_scope()?, id, cx) }) } pub fn request_activation( @@ -204,8 +331,9 @@ impl RuntimeHandle { client: Client, cx: &mut App, ) -> Result<(), crate::AgentActivationError> { + self.check_current()?; self.runtime.update(cx, |runtime, cx| { - runtime.request_scoped_activation(self.scope, client, cx) + runtime.request_scoped_activation(self.chat_scope()?, client, cx) }) } pub fn connect( @@ -215,8 +343,9 @@ impl RuntimeHandle { launch: AgentLaunch, cx: &mut App, ) -> Result<(), crate::AgentActivationError> { + self.check_current()?; self.runtime.update(cx, |runtime, cx| { - runtime.connect_scoped(self.scope, client, chat_id, launch, cx) + runtime.connect_scoped(self.chat_scope()?, client, chat_id, launch, cx) }) } pub fn register_bridge( @@ -224,8 +353,17 @@ impl RuntimeHandle { client: &Client, cx: &mut App, ) -> Result { + self.current_source()?; self.runtime.update(cx, |runtime, cx| { - runtime.register_scoped_bridge(self.scope, client, cx) + runtime.register_scoped_bridge( + self.chat_scope().map_err(|_| { + nocterm_ai::connection::BridgeRegistrationError::AccountSource( + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable, + ) + })?, + client, + cx, + ) }) } pub fn save_chat( @@ -236,7 +374,7 @@ impl RuntimeHandle { cx: &mut App, ) -> Result { self.runtime.update(cx, |runtime, cx| { - runtime.scoped_save_chat(self.scope, chat, owner, revision, cx) + runtime.scoped_save_chat(self.chat_scope()?, chat, owner, revision, cx) }) } pub fn capture_and_detach_document( @@ -245,28 +383,74 @@ impl RuntimeHandle { cx: &mut App, ) -> Result { self.runtime.update(cx, |runtime, cx| { - runtime.capture_scoped_document(self.scope, id, cx) + runtime.capture_scoped_document(self.chat_scope()?, id, cx) }) } + /// The chats of this collection windows other than `window` hold, which `window` lists + /// without holding them. + pub fn held_elsewhere(&self, window: EntityId, cx: &App) -> Vec { + let (Ok(()), Ok(scope)) = (self.check_current(), self.chat_scope()) else { + return Vec::new(); + }; + self.runtime + .read(cx) + .held_elsewhere(&scope, window) + .iter() + .filter_map(|client| client.chat()) + .collect() + } + /// Asks the window that holds chat `chat` to show it and come forward. + pub fn reveal_chat(&self, chat: EntityId, cx: &mut App) { + self.runtime + .update(cx, |_, cx| cx.emit(crate::HolderEvent::Reveal { chat })); + } + /// Whether chat `chat`, which another window holds, may move to `window`: it neither works + /// nor shows in its own. + pub fn may_move(&self, chat: EntityId, window: EntityId, cx: &App) -> bool { + let runtime = self.runtime.read(cx); + runtime.chats.window_of(chat).is_some() && runtime.kept_by(chat, window, cx).is_none() + } + /// Calls `event` for what the runtime tells every window about the chats windows hold. + pub fn observe_holders( + &self, + cx: &mut App, + mut event: impl FnMut(&crate::HolderEvent, &mut App) + 'static, + ) -> Subscription { + cx.subscribe(&self.runtime, move |_, notice, cx| event(notice, cx)) + } pub fn delete_chat(&self, id: String, cx: &mut App) { + if self.check_current().is_err() { + return; + } + let Ok(scope) = self.chat_scope() else { + return; + }; self.runtime.update(cx, |runtime, cx| { - runtime.scoped_delete_chat(ChatKey::new(self.scope, id), cx) + runtime.scoped_delete_chat(ChatKey::in_environment(scope, id), cx) }) } /// Request the captured context once; an existing load or snapshot is retained. pub fn ensure_history(&self, cx: &mut App) -> Result<(), crate::CatalogLoadError> { - self.runtime - .update(cx, |runtime, cx| runtime.chats.ensure(self.scope, cx)) + self.current_source()?; + self.runtime.update(cx, |runtime, cx| { + runtime.chats.ensure(self.chat_scope()?, cx) + }) } pub fn try_reload_history(&self, cx: &mut App) -> Result<(), crate::CatalogLoadError> { + self.current_source()?; self.runtime.update(cx, |runtime, cx| { - runtime.try_scoped_reload_history(self.scope, cx) + runtime.try_scoped_reload_history(self.chat_scope()?, cx) }) } pub fn reload_history(&self, cx: &mut App) { - self.runtime.update(cx, |runtime, cx| { - runtime.scoped_reload_history(self.scope, cx) - }) + if self.check_current().is_err() { + return; + } + let Ok(scope) = self.chat_scope() else { + return; + }; + self.runtime + .update(cx, |runtime, cx| runtime.scoped_reload_history(scope, cx)) } pub fn settings_changed(&self, cx: &mut App) { self.runtime diff --git a/crates/agent-runtime/src/history_admission.rs b/crates/agent-runtime/src/history_admission.rs new file mode 100644 index 00000000..75e44515 --- /dev/null +++ b/crates/agent-runtime/src/history_admission.rs @@ -0,0 +1,608 @@ +//! One installation owns Local/remote history bytes and retained payload slots. +//! +//! Participants expose only history accounting and optional hidden-history eviction. +//! Registration and allocation tokens never keep an entity alive. No budget state +//! borrow is held while a participant is observed or asked to evict. +use crate::HistoryUsage; +use gpui::{App, EntityId}; +use nocterm_ai::{ + execution_origin::ChatKey, history::MAX_RESIDENT_BYTES, storage::AgentStorageError, +}; +use std::{ + cell::{Cell, RefCell}, + collections::HashSet, + rc::{Rc, Weak}, + sync::{Arc, Weak as SyncWeak}, +}; + +mod eviction; +mod local_allocations; +pub use local_allocations::{LocalHistoryAllocations, LocalSnapshotAllocation}; +pub(crate) mod local; +use eviction::EvictionCandidate; +#[cfg(any(test, feature = "test-support"))] +pub mod testing; + +pub const MAX_HISTORY_PAYLOADS: usize = 32; +/// The final two payloads and 1MiB remain available to original urgent controls. +pub const MAX_NORMAL_HISTORY_PAYLOADS: usize = MAX_HISTORY_PAYLOADS - 2; +// Guarded fresh-head receipts and upload buffers share this reserved lane. +// This repartitions the same installation total; normal capacity decreases. +pub const URGENT_HISTORY_BYTES: usize = 1024 * 1024; +pub const MAX_NORMAL_HISTORY_BYTES: usize = MAX_RESIDENT_BYTES - URGENT_HISTORY_BYTES; + +#[derive(Debug, thiserror::Error)] +pub enum HistoryAdmissionError { + #[error("History admission is closed.")] + Closed, + #[error("History admission is already deciding an eviction.")] + Reentrant, + #[error("The history registration belongs to another installation.")] + ForeignRegistration, + #[error("The history allocation belongs to another installation.")] + ForeignAllocation, + #[error("A retained history allocation cannot grow from {reserved} to {requested} bytes.")] + AllocationGrowth { reserved: usize, requested: usize }, + #[error("All normal installation history payload slots are retained or loading.")] + PayloadCapacity, + #[error( + "History needs {incoming} bytes with {retained} retained; the installation limit is {limit}." + )] + ResidentCapacity { + retained: usize, + incoming: usize, + limit: usize, + }, + #[error(transparent)] + Storage(#[from] AgentStorageError), +} +impl HistoryAdmissionError { + pub fn into_storage(self) -> AgentStorageError { + match self { + Self::Storage(error) => error, + Self::PayloadCapacity => AgentStorageError::PayloadCapacity, + Self::ResidentCapacity { + retained, + incoming, + limit, + } => AgentStorageError::ResidentCapacity { + retained, + incoming, + limit, + }, + _ => AgentStorageError::Interrupted, + } + } +} + +/// The implementation retains a weak entity/data-owner reference, never an App. +pub trait HistoryParticipant { + fn entity_id(&self) -> Option { + None + } + fn usage(&self, cx: &App) -> Result, AgentStorageError>; + fn evict(&self, _cx: &mut App) -> bool { + false + } + /// Oldest reclaimable payload's installation allocation order. Retained + /// caller snapshots, shown documents and pending work are never candidates. + fn oldest_payload(&self, _cx: &App) -> Option { + None + } + fn evict_payload(&self, _cx: &mut App) -> bool { + false + } + fn local_key(&self, _cx: &App) -> Option { + None + } +} +struct RegisteredHistory { + owner: Weak, + participant: Rc, +} +/// Retains the observation adapter, whose entity reference remains weak. +#[derive(Clone)] +pub struct HistoryRegistration(Rc); + +/// Cloneable allocation proof; cloned retained rows consume one slot together. +#[derive(Clone)] +pub struct HistoryPayloadPermit(Arc); +struct PayloadAllocation { + order: u64, +} +impl HistoryPayloadPermit { + pub fn same_allocation(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } + pub fn order(&self) -> u64 { + self.0.order + } +} +/// An admitted pending buffer. Its immutable bytes stay charged until final drop. +#[derive(Clone)] +pub struct HistoryBytesPermit(Arc); +impl HistoryBytesPermit { + pub fn bytes(&self) -> usize { + *self.0 + } +} +#[derive(Default)] +struct AdmissionState { + participants: Vec>, + payloads: Vec>, + buffers: Vec>, + snapshots: Vec, + closed: bool, + payload_order: u64, +} +struct InstallationHistoryAdmission { + state: RefCell, + admitting: Cell, +} +/// An explicit handle to the existing retained installation, shared by every partition. +#[derive(Clone)] +pub struct HistoryAdmissionHandle(Rc); + +enum Requester { + None, + Entity(EntityId), + Entities(Vec), + Observed { + registration: *const RegisteredHistory, + entities: Vec, + }, + Registration(*const RegisteredHistory), +} + +struct AdmissionTurn<'a>(&'a Cell); +impl Drop for AdmissionTurn<'_> { + fn drop(&mut self) { + self.0.set(false); + } +} +impl HistoryAdmissionHandle { + #[cfg(any(test, feature = "test-support"))] + pub(crate) fn new() -> Self { + Self::with_initial_payload(false).0 + } + /// Installation construction can move one already prepared body batch into + /// its very first generation. No existing owner/quota can race this step. + pub(crate) fn with_initial_payload(prepared: bool) -> (Self, Option) { + let allocation = prepared.then(|| Arc::new(PayloadAllocation { order: 1 })); + let state = AdmissionState { + payloads: allocation.iter().map(Arc::downgrade).collect(), + payload_order: u64::from(prepared), + ..AdmissionState::default() + }; + let handle = Self(Rc::new(InstallationHistoryAdmission { + state: RefCell::new(state), + admitting: Cell::new(false), + })); + (handle, allocation.map(HistoryPayloadPermit)) + } + pub fn register( + &self, + participant: Rc, + ) -> Result { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + state.participants.retain(|entry| entry.strong_count() > 0); + let registration = Rc::new(RegisteredHistory { + owner: Rc::downgrade(&self.0), + participant, + }); + state.participants.push(Rc::downgrade(®istration)); + Ok(HistoryRegistration(registration)) + } + pub fn reserve_payload(&self) -> Result { + self.reserve_payload_to(MAX_NORMAL_HISTORY_PAYLOADS) + } + fn reserve_payload_to( + &self, + limit: usize, + ) -> Result { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + state + .snapshots + .retain(local_allocations::TrackedSnapshot::alive); + state.payloads.retain(|entry| entry.strong_count() > 0); + if state.payloads.len() >= limit { + return Err(HistoryAdmissionError::PayloadCapacity); + } + let order = state + .payload_order + .checked_add(1) + .ok_or(HistoryAdmissionError::PayloadCapacity)?; + state.payload_order = order; + let allocation = Arc::new(PayloadAllocation { order }); + state.payloads.push(Arc::downgrade(&allocation)); + Ok(HistoryPayloadPermit(allocation)) + } + /// Reclaim across Local and Cloud using one installation allocation order. + /// Skip the entity currently borrowed by the caller. All callbacks run + /// outside the budget's RefCell borrow, and nested decisions fail closed. + pub fn reserve_payload_reclaiming( + &self, + requester: Option, + cx: &mut App, + ) -> Result { + self.reserve_payload_excluding(&requester.into_iter().collect::>(), cx) + } + fn reserve_payload_excluding( + &self, + excluded: &[EntityId], + cx: &mut App, + ) -> Result { + if self.0.admitting.replace(true) { + return Err(HistoryAdmissionError::Reentrant); + } + let _turn = AdmissionTurn(&self.0.admitting); + let mut attempted = HashSet::new(); + let maximum_attempts = self + .observations()? + .0 + .len() + .saturating_add(MAX_HISTORY_PAYLOADS); + loop { + match self.reserve_payload() { + Ok(permit) => return Ok(permit), + Err(HistoryAdmissionError::PayloadCapacity) => {} + Err(error) => return Err(error), + } + let (participants, _) = self.observations()?; + let victim = participants + .into_iter() + .filter(|entry| { + entry + .participant + .entity_id() + .is_none_or(|id| !excluded.contains(&id)) + }) + .filter_map(|entry| { + entry + .participant + .oldest_payload(cx) + .filter(|order| !attempted.contains(&(Rc::as_ptr(&entry), *order))) + .map(|order| (entry, order)) + }) + .min_by_key(|(_, order)| *order) + .ok_or(HistoryAdmissionError::PayloadCapacity)?; + if attempted.len() >= maximum_attempts { + return Err(HistoryAdmissionError::PayloadCapacity); + } + attempted.insert((Rc::as_ptr(&victim.0), victim.1)); + victim.0.participant.evict_payload(cx); + // Re-observe live allocations, including caller-retained rows. A + // allocation ordinal is tried once, even if a callback reports success + // without releasing it. At most the32live payloads can be reclaimed. + } + } + pub fn reserve_bytes( + &self, + requester: &HistoryRegistration, + bytes: usize, + cx: &mut App, + ) -> Result { + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + self.reserve(Requester::Registration(Rc::as_ptr(&requester.0)), bytes, cx) + } + /// Move a previously observed Local prepared body into a proof-backed + /// buffer. The remaining source plus destination are admitted together. + fn reserve_observed_buffer( + &self, + requester: &HistoryRegistration, + entities: Vec, + remaining: usize, + bytes: usize, + cx: &mut App, + ) -> Result { + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + self.decide( + Requester::Observed { + registration: Rc::as_ptr(&requester.0), + entities, + }, + remaining.saturating_add(bytes), + cx, + )?; + self.charge(bytes) + } + fn require_payload(&self, payload: &HistoryPayloadPermit) -> Result<(), HistoryAdmissionError> { + let identity = Arc::downgrade(&payload.0); + if self + .0 + .state + .borrow() + .payloads + .iter() + .any(|entry| entry.ptr_eq(&identity)) + { + Ok(()) + } else { + Err(HistoryAdmissionError::ForeignAllocation) + } + } + /// Additional temporary work while the requester is mutably borrowed. Its + /// supplied existing bytes stay counted, but only the new buffer is charged + /// by the returned token. No requester callback is invoked. + pub fn reserve_buffer( + &self, + requester: &HistoryRegistration, + existing: usize, + bytes: usize, + cx: &mut App, + ) -> Result { + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + self.decide( + Requester::Registration(Rc::as_ptr(&requester.0)), + existing.saturating_add(bytes), + cx, + )?; + self.charge(bytes) + } + /// Admission before a new entity or retained native result is materialized. + pub fn reserve_unowned_bytes( + &self, + bytes: usize, + cx: &mut App, + ) -> Result { + self.reserve(Requester::None, bytes, cx) + } + /// Native metadata work is reserved while its owning Runtime is borrowed. + /// Other installations/participants remain visible to this same decision. + pub(crate) fn reserve_entity_bytes( + &self, + requester: EntityId, + bytes: usize, + cx: &mut App, + ) -> Result { + self.reserve(Requester::Entity(requester), bytes, cx) + } + /// Transfer a unique reservation to a smaller immutable allocation. Shared + /// reservations retain their conservative original charge until last drop. + pub(crate) fn shrink_buffer( + &self, + allocation: HistoryBytesPermit, + bytes: usize, + ) -> Result { + if bytes > allocation.bytes() { + return Err(HistoryAdmissionError::AllocationGrowth { + reserved: allocation.bytes(), + requested: bytes, + }); + } + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + let original = Arc::downgrade(&allocation.0); + if !state.buffers.iter().any(|entry| entry.ptr_eq(&original)) { + return Err(HistoryAdmissionError::ForeignAllocation); + } + if Arc::strong_count(&allocation.0) > 1 { + return Ok(allocation); + } + let smaller = Arc::new(bytes); + state.buffers.push(Arc::downgrade(&smaller)); + // No callback, await or externally visible decision occurs between the + // replacement and releasing its old proof. Both byte counts are immutable. + drop(state); + drop(allocation); + Ok(HistoryBytesPermit(smaller)) + } + fn reserve( + &self, + requester: Requester, + bytes: usize, + cx: &mut App, + ) -> Result { + self.decide(requester, bytes, cx)?; + self.charge(bytes) + } + fn charge(&self, bytes: usize) -> Result { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + let allocation = Arc::new(bytes); + state.buffers.push(Arc::downgrade(&allocation)); + Ok(HistoryBytesPermit(allocation)) + } + fn observations(&self) -> Result<(Vec>, usize), HistoryAdmissionError> { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + state.participants.retain(|entry| entry.strong_count() > 0); + state + .snapshots + .retain(local_allocations::TrackedSnapshot::alive); + state.buffers.retain(|entry| entry.strong_count() > 0); + let participants = state + .participants + .iter() + .filter_map(Weak::upgrade) + .collect(); + let buffers = state + .buffers + .iter() + .filter_map(SyncWeak::upgrade) + .fold(0usize, |total, bytes| total.saturating_add(*bytes)); + Ok((participants, buffers)) + } + /// Local admissions and Cloud reservations use exactly this same decision. + pub(crate) fn admit( + &self, + requester: Option, + incoming: usize, + cx: &mut App, + ) -> Result, HistoryAdmissionError> { + self.decide( + requester.map_or(Requester::None, Requester::Entity), + incoming, + cx, + ) + } + fn decide( + &self, + requester: Requester, + incoming: usize, + cx: &mut App, + ) -> Result, HistoryAdmissionError> { + self.decide_to(requester, incoming, MAX_NORMAL_HISTORY_BYTES, cx) + } + fn decide_to( + &self, + requester: Requester, + incoming: usize, + limit: usize, + cx: &mut App, + ) -> Result, HistoryAdmissionError> { + if self.0.admitting.replace(true) { + return Err(HistoryAdmissionError::Reentrant); + } + let _turn = AdmissionTurn(&self.0.admitting); + let mut attempted = HashSet::new(); + let mut evicted = Vec::new(); + let maximum_attempts = self + .observations()? + .0 + .len() + .saturating_add(MAX_HISTORY_PAYLOADS); + loop { + let (participants, mut retained) = self.observations()?; + let mut candidates = Vec::new(); + for participant in participants { + let is_requester = match &requester { + Requester::None => false, + Requester::Entity(id) => participant.participant.entity_id() == Some(*id), + Requester::Entities(ids) => participant + .participant + .entity_id() + .is_some_and(|id| ids.contains(&id)), + Requester::Registration(ptr) => Rc::as_ptr(&participant) == *ptr, + Requester::Observed { + registration, + entities, + } => { + Rc::as_ptr(&participant) == *registration + || participant + .participant + .entity_id() + .is_some_and(|id| entities.contains(&id)) + } + }; + if is_requester { + continue; + } + let usage = participant.participant.usage(cx)?; + if let Some(usage) = usage { + retained = retained.saturating_add(usage.bytes()); + } + if let Some(candidate) = EvictionCandidate::new(participant, usage, cx) + && !attempted.contains(&candidate.identity()) + { + candidates.push(candidate); + } + } + if self.0.state.borrow().closed { + return Err(HistoryAdmissionError::Closed); + } + if retained.saturating_add(incoming) <= limit { + return Ok(evicted); + } + candidates.sort_by_key(EvictionCandidate::priority); + let Some(victim) = candidates + .into_iter() + .next() + .filter(|_| attempted.len() < maximum_attempts) + else { + return Err(HistoryAdmissionError::ResidentCapacity { + retained, + incoming, + limit, + }); + }; + attempted.insert(victim.identity()); + if let Some(key) = victim.evict(cx) { + evicted.push(key); + } + // Re-observe actual data and retained buffers after the callback. A + // callback may release registrations or immutable writer generations. + } + } + /// A bounded allowance for an already captured urgent control. The service + /// selects this only for original Stop, never normal prompt/data allocations. + /// It consumes the same hard32slots/64MiB; it cannot extend either bound. + pub fn reserve_urgent_control( + &self, + requester: &HistoryRegistration, + bytes: usize, + payloads: usize, + cx: &mut App, + ) -> Result { + if payloads > 2 { + return Err(HistoryAdmissionError::PayloadCapacity); + } + if bytes > URGENT_HISTORY_BYTES { + return Err(HistoryAdmissionError::ResidentCapacity { + retained: 0, + incoming: bytes, + limit: URGENT_HISTORY_BYTES, + }); + } + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + let mut slots = Vec::with_capacity(payloads); + for _ in 0..payloads { + slots.push(self.reserve_payload_to(MAX_HISTORY_PAYLOADS)?); + } + self.decide_to( + Requester::Registration(Rc::as_ptr(&requester.0)), + bytes, + MAX_RESIDENT_BYTES, + cx, + )?; + let proof = self.charge(bytes)?; + Ok(nocterm_core::allocation::AllocationLease::new( + bytes, + payloads, + (proof, slots), + )) + } + pub(crate) fn close(&self) { + self.0.state.borrow_mut().closed = true; + } +} diff --git a/crates/agent-runtime/src/history_admission/eviction.rs b/crates/agent-runtime/src/history_admission/eviction.rs new file mode 100644 index 00000000..db061c54 --- /dev/null +++ b/crates/agent-runtime/src/history_admission/eviction.rs @@ -0,0 +1,65 @@ +//! One weak participant's offered history or immutable-payload reclamation. +use super::{HistoryUsage, RegisteredHistory}; +use gpui::App; +use nocterm_ai::execution_origin::ChatKey; +use std::rc::Rc; + +enum EvictionKind { + History, + Payload, +} +pub(super) struct EvictionCandidate { + participant: Rc, + age: u64, + kind: EvictionKind, +} +impl EvictionCandidate { + pub(super) fn new( + participant: Rc, + usage: Option, + cx: &App, + ) -> Option { + if let Some(usage) = usage { + if usage.shown() { + return None; + } + if usage.bytes() > 0 { + return Some(Self { + participant, + age: usage.updated(), + kind: EvictionKind::History, + }); + } + } + let age = participant.participant.oldest_payload(cx)?; + Some(Self { + participant, + age, + kind: EvictionKind::Payload, + }) + } + // Reclaim cold metadata before hidden transcript history. Payload ordinals + // and history timestamps are compared only within their own class. + pub(super) fn priority(&self) -> (bool, u64) { + (matches!(self.kind, EvictionKind::History), self.age) + } + pub(super) fn identity(&self) -> (*const RegisteredHistory, (bool, u64)) { + (Rc::as_ptr(&self.participant), self.priority()) + } + pub(super) fn evict(&self, cx: &mut App) -> Option { + match self.kind { + EvictionKind::History => { + let key = self.participant.participant.local_key(cx); + self.participant + .participant + .evict(cx) + .then_some(key) + .flatten() + } + EvictionKind::Payload => { + self.participant.participant.evict_payload(cx); + None + } + } + } +} diff --git a/crates/agent-runtime/src/history_admission/local.rs b/crates/agent-runtime/src/history_admission/local.rs new file mode 100644 index 00000000..b4a0bd45 --- /dev/null +++ b/crates/agent-runtime/src/history_admission/local.rs @@ -0,0 +1,32 @@ +//! Existing local session clients participate through their unchanged history gates. +use super::HistoryParticipant; +use crate::{Client, HistoryUsage}; +use gpui::{App, EntityId}; +use nocterm_ai::{execution_origin::ChatKey, storage::AgentStorageError}; + +pub(crate) struct SessionHistory(Client); +impl SessionHistory { + pub(crate) fn new(client: Client) -> Self { + Self(client) + } +} +impl HistoryParticipant for SessionHistory { + fn entity_id(&self) -> Option { + Some(self.0.id()) + } + fn usage(&self, cx: &App) -> Result, AgentStorageError> { + Ok(self.0.alive().then(|| self.0.history_usage(cx)).flatten()) + } + fn evict(&self, cx: &mut App) -> bool { + self.0.evict_history(cx) + } + fn oldest_payload(&self, cx: &App) -> Option { + self.0.history_payload(cx) + } + fn evict_payload(&self, cx: &mut App) -> bool { + self.0.evict_history(cx) + } + fn local_key(&self, cx: &App) -> Option { + self.0.state(cx).map(|state| state.origin().key().clone()) + } +} diff --git a/crates/agent-runtime/src/history_admission/local_allocations.rs b/crates/agent-runtime/src/history_admission/local_allocations.rs new file mode 100644 index 00000000..de29104a --- /dev/null +++ b/crates/agent-runtime/src/history_admission/local_allocations.rs @@ -0,0 +1,170 @@ +//! Local immutable generations follow the actual Arc through the existing writer. +use super::{ + HistoryAdmissionError, HistoryAdmissionHandle, HistoryBytesPermit, HistoryPayloadPermit, + HistoryRegistration, Requester, +}; +use gpui::{App, EntityId}; +use nocterm_ai::{history::SharedChat, storage::AgentStorageError}; +use std::sync::{Arc, Weak}; + +pub(super) struct TrackedSnapshot { + value: Weak, + _bytes: HistoryBytesPermit, + _payload: HistoryPayloadPermit, +} +impl TrackedSnapshot { + pub(super) fn alive(&self) -> bool { + self.value.strong_count() > 0 + } +} +/// Captured before any Runtime/Thread mutable borrow. It performs no service lookup. +#[derive(Clone)] +pub struct LocalHistoryAllocations { + admission: HistoryAdmissionHandle, + runtime: EntityId, +} +pub struct LocalSnapshotAllocation { + admission: HistoryAdmissionHandle, + bytes: HistoryBytesPermit, + payload: HistoryPayloadPermit, +} +impl LocalHistoryAllocations { + pub(crate) fn new(admission: HistoryAdmissionHandle, runtime: EntityId) -> Self { + Self { admission, runtime } + } + pub(crate) fn handle(&self) -> HistoryAdmissionHandle { + self.admission.clone() + } + pub fn history_admission(&self) -> HistoryAdmissionHandle { + self.admission.clone() + } + pub(crate) fn retain_external( + &self, + requester: EntityId, + existing: usize, + value: Arc, + cx: &mut App, + ) -> Result, AgentStorageError> { + let tracked = self + .admission + .0 + .state + .borrow() + .snapshots + .iter() + .any(|entry| entry.value.ptr_eq(&Arc::downgrade(&value))); + if tracked { + return Ok(value); + } + let capacity = value.retained_bytes()?; + self.reserve_snapshot(requester, existing, capacity, cx) + .map_err(HistoryAdmissionError::into_storage)? + .retain(value) + } + pub(crate) fn transfer_prepared( + &self, + source: &HistoryRegistration, + remaining: usize, + bytes: usize, + payload: HistoryPayloadPermit, + cx: &mut App, + ) -> Result { + self.admission + .require_payload(&payload) + .map_err(HistoryAdmissionError::into_storage)?; + let proof = self + .admission + .reserve_observed_buffer(source, vec![self.runtime], remaining, bytes, cx) + .map_err(HistoryAdmissionError::into_storage)?; + Ok(nocterm_core::allocation::AllocationLease::new( + bytes, + 1, + (proof, payload), + )) + } + pub fn reserve_resident( + &self, + requester: Option, + cx: &mut App, + ) -> Result { + let mut excluded = vec![self.runtime]; + excluded.extend(requester); + self.admission.reserve_payload_excluding(&excluded, cx) + } + /// Snapshot metadata/rows are measured while borrowed, before cloning them. + pub fn reserve_snapshot( + &self, + requester: EntityId, + existing: usize, + capacity: usize, + cx: &mut App, + ) -> Result { + let excluded = vec![self.runtime, requester]; + let payload = self.admission.reserve_payload_excluding(&excluded, cx)?; + self.admission.decide( + Requester::Entities(excluded), + existing.saturating_add(capacity), + cx, + )?; + let bytes = self.admission.charge(capacity)?; + Ok(LocalSnapshotAllocation { + admission: self.admission.clone(), + bytes, + payload, + }) + } +} +impl LocalSnapshotAllocation { + /// The actual decoded body generation moves to its resident owner with this + /// same slot. Bytes remain with the worker/result until atomic publication. + pub fn decode_allocation( + self, + ) -> ( + nocterm_core::allocation::AllocationLease, + HistoryPayloadPermit, + ) { + let slot = self.payload.clone(); + (self.into_lease(), slot) + } + + /// Transfer an already reserved standalone preparation into its actual owner. + pub fn into_lease(self) -> nocterm_core::allocation::AllocationLease { + nocterm_core::allocation::AllocationLease::new( + self.bytes.bytes(), + 1, + (self.bytes, self.payload), + ) + } + + /// Track the actual SharedChat allocation, including active/latest generations, + /// shutdown capture, and a detached native worker after its GUI owner drops. + /// Arc clones/retries keep this one proof, not one proof per observer. + pub fn retain(self, value: Arc) -> Result, AgentStorageError> { + let actual = value.retained_bytes()?; + if actual > self.bytes.bytes() { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: actual, + limit: self.bytes.bytes(), + }); + } + let bytes = self + .admission + .shrink_buffer(self.bytes, actual) + .map_err(HistoryAdmissionError::into_storage)?; + let mut state = self.admission.0.state.borrow_mut(); + state.snapshots.retain(TrackedSnapshot::alive); + if !state + .snapshots + .iter() + .any(|entry| entry.value.ptr_eq(&Arc::downgrade(&value))) + { + state.snapshots.push(TrackedSnapshot { + value: Arc::downgrade(&value), + _bytes: bytes, + _payload: self.payload, + }); + } + Ok(value) + } +} diff --git a/crates/agent-runtime/src/history_admission/testing.rs b/crates/agent-runtime/src/history_admission/testing.rs new file mode 100644 index 00000000..7475a24a --- /dev/null +++ b/crates/agent-runtime/src/history_admission/testing.rs @@ -0,0 +1,120 @@ +//! Shared external history participant for Local/remote admission behavior tests. +use super::{HistoryParticipant, HistoryPayloadPermit}; +use crate::HistoryUsage; +use gpui::App; +use nocterm_ai::storage::AgentStorageError; +use std::{ + cell::RefCell, + rc::{Rc, Weak}, +}; + +#[derive(Default)] +struct ProbeState { + usage: Option, + evictable: bool, + payloads: Vec, + evictions: usize, + on_evict: Option>, + false_payload_success: bool, +} +/// The participant adapter keeps only a weak reference to this external owner. +#[derive(Clone, Default)] +pub struct HistoryProbe(Rc>); +struct ProbeParticipant(Weak>); +impl HistoryProbe { + /// Uses the real installation admission owner without a connector fixture. + pub fn installation() -> super::HistoryAdmissionHandle { + super::HistoryAdmissionHandle::new() + } + /// Drives the real allocation transfer through the shared test collaborator. + pub fn transfer_buffer( + admission: &super::HistoryAdmissionHandle, + allocation: super::HistoryBytesPermit, + bytes: usize, + ) -> Result { + admission.shrink_buffer(allocation, bytes) + } + /// Exercise a dishonest/no-progress collaborator without a second fake. + pub fn report_payload_success_without_release(&self) { + self.0.borrow_mut().false_payload_success = true; + } + pub fn participant(&self) -> Rc { + Rc::new(ProbeParticipant(Rc::downgrade(&self.0))) + } + pub fn set_history(&self, usage: HistoryUsage, evictable: bool) { + let mut state = self.0.borrow_mut(); + state.usage = Some(usage); + state.evictable = evictable; + } + pub fn retain_payload(&self, permit: HistoryPayloadPermit) { + self.0.borrow_mut().payloads.push(permit); + } + pub fn payload_count(&self) -> usize { + self.0.borrow().payloads.len() + } + pub fn history(&self) -> Option { + self.0.borrow().usage + } + pub fn evictions(&self) -> usize { + self.0.borrow().evictions + } + pub fn on_evict(&self, callback: Rc) { + self.0.borrow_mut().on_evict = Some(callback); + } +} +impl HistoryParticipant for ProbeParticipant { + fn usage(&self, _cx: &App) -> Result, AgentStorageError> { + Ok(self.0.upgrade().and_then(|state| state.borrow().usage)) + } + fn evict(&self, cx: &mut App) -> bool { + let Some(owner) = self.0.upgrade() else { + return false; + }; + let callback = { + let mut state = owner.borrow_mut(); + if !state.evictable || state.usage.is_some_and(|usage| usage.shown()) { + return false; + } + state.usage = None; + state.evictions += 1; + state.on_evict.clone() + }; + if let Some(callback) = callback { + callback(cx); + } + true + } + fn oldest_payload(&self, _cx: &App) -> Option { + self.0.upgrade().and_then(|owner| { + let state = owner.borrow(); + if !state.evictable || state.usage.is_some_and(|usage| usage.shown()) { + return None; + } + state.payloads.iter().map(HistoryPayloadPermit::order).min() + }) + } + fn evict_payload(&self, _cx: &mut App) -> bool { + let Some(owner) = self.0.upgrade() else { + return false; + }; + let mut state = owner.borrow_mut(); + if !state.evictable || state.usage.is_some_and(|usage| usage.shown()) { + return false; + } + state.evictions += 1; + if state.false_payload_success { + return true; + } + let Some(index) = state + .payloads + .iter() + .enumerate() + .min_by_key(|(_, permit)| permit.order()) + .map(|(index, _)| index) + else { + return false; + }; + state.payloads.remove(index); + true + } +} diff --git a/crates/agent-runtime/src/holders.rs b/crates/agent-runtime/src/holders.rs new file mode 100644 index 00000000..49798d11 --- /dev/null +++ b/crates/agent-runtime/src/holders.rs @@ -0,0 +1,20 @@ +//! What the windows of one installation tell each other about the chats they hold. +//! +//! A chat's document belongs to the one window that holds it; the other windows list it in +//! their history without holding it. Opening it there asks its window to show it and come +//! forward, or, while it neither works nor shows, moves it: the asking window claims it in +//! place of that window's chat, and that window is told to stop listing it. +use gpui::{EntityId, EventEmitter}; + +/// What the runtime tells every window about the chats windows hold. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HolderEvent { + /// A window began to hold a chat. + Changed, + /// The window holding chat `chat` shows it and comes forward. + Reveal { chat: EntityId }, + /// Chat `chat` moved to another window: its window stops listing it. + Moved { chat: EntityId }, +} + +impl EventEmitter for crate::Runtime {} diff --git a/crates/agent-runtime/src/lib.rs b/crates/agent-runtime/src/lib.rs index 7bc0ad82..f441cc31 100644 --- a/crates/agent-runtime/src/lib.rs +++ b/crates/agent-runtime/src/lib.rs @@ -4,15 +4,19 @@ //! keyed write admission, lazy history hydration and usage projections. //! Does not own: settings storage, history/favorites files, ACP subprocesses or views. //! Ports used: AgentConnector, ToolBridge, AgentStateRepository, ChatRepository, -//! AccountUsageSource, WorkdirResolver, explicit SettingsHandle and host-supplied ShutdownCoordinator +//! WorkdirResolver, CommandJobsFactory, explicit SettingsHandle and host-supplied ShutdownCoordinator //! at installation. -//! Ports provided: SessionClient/SessionEvent, HistoryReader and synchronous quit capture. +//! Ports provided: SessionClient/SessionEvent, HistoryReader, HolderEvent (what windows hold, +//! reveal and move requests) and synchronous quit capture. //! Settings section: ai consumer. Globals: none. //! Invariants: one current document owner per scoped ChatKey; noncloneable reservations -//! precede entity construction, and explicit restart replaces only its expected owner. +//! precede entity construction, and explicit restart replaces only its expected owner. A +//! chat moves to another window only while it neither works nor shows in its own. //! Scope-bound handles/readers and callbacks retain original ownership. Catalog payloads -//! share one installation32-slot limit, including retained snapshots, archived row references -//! and pending work. Hydration admission includes all contexts/windows, remaining prepared +//! share one installation 32-slot limit, including retained snapshots, archived row references +//! and pending work across every Local environment. Handles retain authenticated account +//! leases independently of General/Project context; unavailable accounts have no fallback. +//! Hydration admission includes all contexts/windows/accounts, remaining prepared //! records and immutable active/latest writer values; shown chats cannot be evicted. //! All contexts use the same local agent launcher. Delete seals //! only its scoped key; persistence @@ -30,12 +34,14 @@ pub use error::{AgentActivationError, ChatOpenError}; mod execution; mod handle; pub use handle::RuntimeHandle; +mod holders; +pub use holders::HolderEvent; mod admission; mod bridge; mod catalog; mod client; +pub mod history_admission; mod releases; -mod residency; mod usage; pub use catalog::{CatalogLoadError, CatalogRow, CatalogSnapshot, PreparedHistory}; mod connections; @@ -56,7 +62,9 @@ use nocterm_ai::{ ToolBridge, }; use std::{collections::HashMap, path::PathBuf, sync::Arc}; -pub use storage::{AgentPersistenceError, AgentStorage, HistoryReader}; +pub use storage::{ + AgentPersistenceError, AgentStorage, HistoryReader, LoadedHistory, PreparedHistoryRead, +}; pub struct TerminalAuthRequest { pub program: String, @@ -72,9 +80,10 @@ pub struct RuntimeServices { pub agents: nocterm_agent_directory::AgentsHandle, pub connector: Arc, pub bridge: Arc, + pub command_jobs: Arc, + /// Starts and follows the file transfers chats run for their agents. + pub transfers: Arc, pub storage: AgentStorage, - /// Account usage is read through a host-injected native capability. - pub usage_source: Arc, pub workdir_resolver: Arc, pub workdir: PathBuf, /// Whether the host can run an agent's sign-in command in a terminal. @@ -127,11 +136,10 @@ impl Runtime { let state = state::AgentStateStore::new(&services.storage, cx); let chats = documents::ChatStore::new(&mut services.storage, cx); let releases = releases::LeaseInbox::new(cx); - let usage = usage::UsageLimits::new(services.usage_source.clone()); Self { state, chats, - usage, + usage: Default::default(), services, connections: Default::default(), admission: Default::default(), @@ -266,11 +274,12 @@ impl Runtime { self.state.error(cx) } pub fn history_reader(&self) -> HistoryReader { - self.chats.reader(nocterm_ai::history::ContextId::General) + self.chats + .reader(nocterm_ai::history::ContextId::General.into()) } pub(crate) fn scoped_history_reader( &self, - scope: nocterm_ai::history::ContextId, + scope: nocterm_ai::local_environment::ChatScope, ) -> HistoryReader { self.chats.reader(scope) } @@ -278,18 +287,6 @@ impl Runtime { pub fn limits(&self, agent: &str) -> Option<&nocterm_ai::usage::Limits> { self.usage.get(agent) } - /// Reads `agent`'s plan limits again, from agents that keep them in their - /// own files. Others report them along with usage. - pub fn refresh_limits(&mut self, agent: &str, cx: &mut Context) { - if self.shutting_down { - return; - } - let Some(agent) = self.services.agents.get(agent, cx) else { - return; - }; - self.usage - .refresh(nocterm_ai::usage::UsageAgent::new(&agent), cx); - } pub fn start_bridge(&mut self, cx: &mut Context) { if self.shutting_down { return; @@ -302,18 +299,18 @@ impl Runtime { client: &Client, cx: &mut Context, ) -> Result { - self.register_scoped_bridge(nocterm_ai::history::ContextId::General, client, cx) + self.register_scoped_bridge(nocterm_ai::history::ContextId::General.into(), client, cx) } pub(crate) fn register_scoped_bridge( &mut self, - scope: nocterm_ai::history::ContextId, + scope: nocterm_ai::local_environment::ChatScope, client: &Client, cx: &mut Context, ) -> Result { let state = client .state(cx) .ok_or(nocterm_ai::connection::BridgeRegistrationError::ClientClosed)?; - state.origin().require_scope(scope)?; + state.origin().require_chat_scope(&scope)?; if !self.ai_enabled(cx) { return Err(nocterm_ai::connection::BridgeRegistrationError::AiDisabled); } @@ -362,7 +359,7 @@ impl Runtime { cx: &mut Context, ) -> Result<(), AgentActivationError> { self.connect_scoped( - nocterm_ai::history::ContextId::General, + nocterm_ai::history::ContextId::General.into(), client, chat_id, launch, @@ -371,14 +368,14 @@ impl Runtime { } pub(crate) fn connect_scoped( &mut self, - scope: nocterm_ai::history::ContextId, + scope: nocterm_ai::local_environment::ChatScope, client: Client, chat_id: String, launch: AgentLaunch, cx: &mut Context, ) -> Result<(), AgentActivationError> { let state = client.state(cx).ok_or(AgentActivationError::ClientClosed)?; - documents::ChatStore::require_client_scope(scope, &client, cx)?; + documents::ChatStore::require_client_scope(scope.clone(), &client, cx)?; let origin = execution::admitted_origin(state.origin(), &chat_id)?; if self.shutting_down || !self.ai_enabled(cx) { return Ok(()); @@ -465,6 +462,13 @@ impl Runtime { effective: PathBuf, cx: &mut Context, ) -> bool { + if !self + .connections + .get(key) + .is_some_and(|connection| self.chats.source_current(connection.identity.key())) + { + return false; + } if !self .connections .get(key) diff --git a/crates/agent-runtime/src/lifecycle.rs b/crates/agent-runtime/src/lifecycle.rs index ae33c41c..441551c7 100644 --- a/crates/agent-runtime/src/lifecycle.rs +++ b/crates/agent-runtime/src/lifecycle.rs @@ -34,7 +34,7 @@ impl Runtime { } pub(crate) fn unregister_scoped_document( &mut self, - scope: nocterm_ai::history::ContextId, + scope: nocterm_ai::local_environment::ChatScope, id: EntityId, cx: &mut Context, ) -> Result<(), nocterm_ai::storage::AgentStorageError> { @@ -44,15 +44,15 @@ impl Runtime { } pub(crate) fn request_scoped_activation( &mut self, - scope: nocterm_ai::history::ContextId, + scope: nocterm_ai::local_environment::ChatScope, client: Client, cx: &mut Context, ) -> Result<(), crate::AgentActivationError> { let state = client .state(cx) .ok_or(crate::AgentActivationError::ClientClosed)?; - state.origin().require_scope(scope)?; - documents::ChatStore::require_client_scope(scope, &client, cx)?; + state.origin().require_chat_scope(&scope)?; + documents::ChatStore::require_client_scope(scope.clone(), &client, cx)?; self.request_activation(client, cx); Ok(()) } @@ -70,7 +70,24 @@ impl Runtime { self.start_session_maintenance(cx); self.maintain_sessions(cx); } + pub(crate) fn refresh_environment_sources(&mut self, cx: &mut Context) { + self.revalidate_sources(cx); + // An explicit native notification can make fresh source captures available. + cx.notify(); + } + fn revalidate_sources(&mut self, cx: &mut Context) -> bool { + let revoked = self.chats.revoke_stale_sources(); + let changed = !revoked.is_empty(); + for client in revoked { + self.admission.withdraw(client.id()); + client.emit(SessionEvent::Shutdown, cx); + } + changed + } fn maintain_sessions(&mut self, cx: &mut Context) { + if self.revalidate_sources(cx) { + cx.notify(); + } if self.shutting_down || !self.ai_enabled(cx) { self.admission.clear(); return; @@ -147,7 +164,7 @@ impl Runtime { } if let Some(agent) = self.services.agents.get(state.agent_id(), cx) { if let Err(error) = self.connect_scoped( - state.origin().scope(), + state.origin().key().chat_scope(), client.clone(), state.origin().key().id().to_owned(), agent.launch().clone(), @@ -272,7 +289,6 @@ impl Runtime { let cleanup = self.connections.take_cleanup(); self.bridge.stop(); self.services.bridge.stop(); - self.usage.stop(); let executor = cx.background_executor().clone(); Box::pin(async move { // Independent process cleanup cannot be serialized behind synchronous file I/O. diff --git a/crates/agent-runtime/src/residency.rs b/crates/agent-runtime/src/residency.rs deleted file mode 100644 index 3b413a87..00000000 --- a/crates/agent-runtime/src/residency.rs +++ /dev/null @@ -1,58 +0,0 @@ -//! One installation admission over live histories and retained persistence values. -use crate::{Client, HistoryUsage}; -use gpui::{App, EntityId}; -use nocterm_ai::{ - execution_origin::ChatKey, history::MAX_RESIDENT_BYTES, storage::AgentStorageError, -}; - -pub(crate) struct HistoryResidency { - clients: Vec, - retained: usize, -} -impl HistoryResidency { - pub(crate) fn new(clients: Vec, retained: usize) -> Self { - Self { clients, retained } - } - pub(crate) fn admit( - self, - requester: Option, - incoming: usize, - cx: &mut App, - ) -> Result, AgentStorageError> { - let mut observed: Vec<(Client, HistoryUsage)> = self - .clients - .into_iter() - // The caller may already mutably borrow its entity. Never project it. - .filter(|client| Some(client.id()) != requester && client.alive()) - .filter_map(|client| client.history_usage(cx).map(|usage| (client, usage))) - .collect(); - let mut resident = observed.iter().fold(self.retained, |bytes, (_, usage)| { - bytes.saturating_add(usage.bytes()) - }); - observed.sort_by_key(|(_, usage)| usage.updated()); - let mut evicted = Vec::new(); - for (client, usage) in observed { - if resident.saturating_add(incoming) <= MAX_RESIDENT_BYTES { - break; - } - if usage.shown() || usage.bytes() == 0 { - continue; - } - let key = client.state(cx).map(|state| state.origin().key().clone()); - if client.evict_history(cx) { - resident = resident.saturating_sub(usage.bytes()); - if let Some(key) = key { - evicted.push(key); - } - } - } - if resident.saturating_add(incoming) > MAX_RESIDENT_BYTES { - return Err(AgentStorageError::ResidentCapacity { - retained: resident, - incoming, - limit: MAX_RESIDENT_BYTES, - }); - } - Ok(evicted) - } -} diff --git a/crates/agent-runtime/src/storage.rs b/crates/agent-runtime/src/storage.rs index 2850d4f8..36e6478a 100644 --- a/crates/agent-runtime/src/storage.rs +++ b/crates/agent-runtime/src/storage.rs @@ -4,6 +4,8 @@ use nocterm_ai::{ storage::{AgentStateRepository, AgentStorageError, ChatRepository}, }; use std::sync::Arc; +mod admitted; +pub use admitted::LoadedHistory; pub struct AgentStorage { state: Arc, @@ -34,6 +36,11 @@ impl AgentStorage { self.history = Some(history); self } + pub(super) fn has_prepared_history(&self) -> bool { + self.history + .as_ref() + .is_some_and(|history| !history.records().is_empty()) + } pub(super) fn take_history(&mut self) -> Option { self.history.take() } @@ -63,41 +70,124 @@ impl AgentStorage { pub struct HistoryReader { repository: Arc, gate: nocterm_ai::history::HistoryGate, - scope: nocterm_ai::history::ContextId, + scope: Option, + access: Option, } impl HistoryReader { pub(super) fn new( repository: Arc, gate: nocterm_ai::history::HistoryGate, - scope: nocterm_ai::history::ContextId, + scope: nocterm_ai::local_environment::ChatScope, ) -> Self { Self { repository, gate, - scope, + scope: Some(scope), + access: Some(nocterm_ai::local_environment::LocalEnvironmentAccess::this_computer()), } } - pub async fn load( + pub(super) fn with_access( + mut self, + access: Option, + ) -> Self { + self.access = access; + self + } + pub(super) fn unavailable( + repository: Arc, + gate: nocterm_ai::history::HistoryGate, + ) -> Self { + Self { + repository, + gate, + scope: None, + access: None, + } + } + /// The captured service performs native work and admits its actual decoded + /// generation before allocation. Cancelling this observer never frees work + /// still owned by an independent native worker/result. + pub fn load_admitted( + &self, + id: String, + admission: crate::history_admission::HistoryAdmissionHandle, + cx: &mut gpui::App, + ) -> gpui::Task { + admitted::AdmittedHistoryLoad::new(self.clone(), id, admission).start(cx) + } + /// Preflight the original held source before any decoded body is allocated. + /// The existing gate bounds raw native scratch only; it grants no decoded quota. + pub async fn prepare_load( &self, id: String, ) -> ( - Result, + Result, nocterm_ai::history::HistoryPermit, ) { let permit = self.gate.acquire().await; - let loaded = self - .repository - .load(self.scope, id.clone()) - .await - .and_then(|chat| { - chat.validate_identity()?; - if chat.header.id != id { - return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); - } - nocterm_ai::storage::require_chat_scope(self.scope, chat.header.scope)?; - Ok(chat) + let prepared = + async { + let access = self.access.as_ref().ok_or( + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable, + )?; + let scope = self.scope.as_ref().ok_or( + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable, + )?; + access.check_current()?; + nocterm_ai::storage::require_local_environment( + access.environment(), + scope.environment(), + )?; + let load = self + .repository + .prepare_load_in(scope.clone(), id.clone()) + .await?; + access.check_current()?; + Ok(PreparedHistoryRead { + load, + access: access.clone(), + scope: scope.clone(), + id, + }) + } + .await; + (prepared, permit) + } +} +/// Retains the exact preflight source/bytes; decode cannot reopen a pathname. +pub struct PreparedHistoryRead { + load: Box, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, + scope: nocterm_ai::local_environment::ChatScope, + id: String, +} +impl PreparedHistoryRead { + pub fn resident_bytes(&self) -> usize { + self.load.resident_bytes() + } + pub async fn decode( + self, + allocation: nocterm_core::allocation::AllocationLease, + ) -> Result< + nocterm_core::allocation::Allocated, + AgentStorageError, + > { + self.access.check_current()?; + if allocation.payloads() == 0 || allocation.bytes() < self.load.resident_bytes() { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: self.load.resident_bytes(), + limit: allocation.bytes(), }); - (loaded, permit) + } + let chat = self.load.decode(allocation).await?; + self.access.check_current()?; + chat.value().validate_identity()?; + if chat.value().header.id != self.id { + return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); + } + nocterm_ai::storage::require_local_scope(&self.scope, &chat.value().header.chat_scope())?; + Ok(chat) } } diff --git a/crates/agent-runtime/src/storage/admitted.rs b/crates/agent-runtime/src/storage/admitted.rs new file mode 100644 index 00000000..68574ea8 --- /dev/null +++ b/crates/agent-runtime/src/storage/admitted.rs @@ -0,0 +1,124 @@ +//! One service pipeline owns native preflight, shared admission and actual workers. +use super::HistoryReader; +use crate::history_admission::{HistoryAdmissionHandle, HistoryPayloadPermit}; +use futures::channel::oneshot; +use gpui::{App, BackgroundExecutor, Task}; +use nocterm_ai::{ + history::{HistoryPermit, SavedChat}, + storage::AgentStorageError, +}; +use nocterm_core::allocation::{Allocated, AllocationLease}; + +pub(super) struct AdmittedHistoryLoad { + reader: HistoryReader, + id: String, + admission: HistoryAdmissionHandle, +} +/// Decoded data, its actual allocation, and raw-history delivery all retire together. +/// Consumers may inspect the borrowed body or atomically move it to its new owner. +pub struct LoadedHistory { + result: Result, AgentStorageError>, + payload: Option, + permit: Option, +} +impl LoadedHistory { + pub fn value(&self) -> Result<&SavedChat, &AgentStorageError> { + self.result.as_ref().map(Allocated::value) + } + /// The callback inspects/drops the body or adopts its same payload proof in + /// a registered resident owner before returning. No naked body is returned. + pub fn publish( + self, + publish: impl FnOnce( + Result, + Option, + Option, + ), + ) { + match self.result { + Ok(charged) => { + charged.map(|chat| publish(Ok(chat), self.permit, self.payload)); + } + Err(error) => publish(Err(error), self.permit, self.payload), + } + } + fn failed(error: AgentStorageError, permit: Option) -> Self { + Self { + result: Err(error), + payload: None, + permit, + } + } +} +impl AdmittedHistoryLoad { + pub(super) fn new( + reader: HistoryReader, + id: String, + admission: HistoryAdmissionHandle, + ) -> Self { + Self { + reader, + id, + admission, + } + } + pub(super) fn start(self, cx: &mut App) -> Task { + let prepared = detached(cx.background_executor(), async move { + self.reader.prepare_load(self.id).await + }); + cx.spawn(async move |cx| { + let Ok((prepared, permit)) = prepared.await else { + return LoadedHistory::failed(AgentStorageError::Interrupted, None); + }; + let prepared = match prepared { + Ok(value) => value, + Err(error) => return LoadedHistory::failed(error, Some(permit)), + }; + let allocation = + cx.update(|cx| allocate(&self.admission, prepared.resident_bytes(), cx)); + let (allocation, payload) = match allocation { + Ok(value) => value, + Err(error) => return LoadedHistory::failed(error, Some(permit)), + }; + let decoded = detached(cx.background_executor(), async move { + (prepared.decode(allocation).await, payload, permit) + }); + match decoded.await { + Ok((result, payload, permit)) => LoadedHistory { + result, + payload: Some(payload), + permit: Some(permit), + }, + Err(_) => LoadedHistory::failed(AgentStorageError::Interrupted, None), + } + }) + } +} +fn allocate( + admission: &HistoryAdmissionHandle, + bytes: usize, + cx: &mut App, +) -> Result<(AllocationLease, HistoryPayloadPermit), AgentStorageError> { + let payload = admission + .reserve_payload_reclaiming(None, cx) + .map_err(|error| error.into_storage())?; + let proof = admission + .reserve_unowned_bytes(bytes, cx) + .map_err(|error| error.into_storage())?; + Ok(( + AllocationLease::new(bytes, 1, (proof, payload.clone())), + payload, + )) +} +fn detached( + executor: &BackgroundExecutor, + work: impl Future + Send + 'static, +) -> oneshot::Receiver { + let (send, receive) = oneshot::channel(); + executor + .spawn(async move { + let _ = send.send(work.await); + }) + .detach(); + receive +} diff --git a/crates/agent-runtime/src/usage.rs b/crates/agent-runtime/src/usage.rs index 7cda0375..97aec2f0 100644 --- a/crates/agent-runtime/src/usage.rs +++ b/crates/agent-runtime/src/usage.rs @@ -1,60 +1,17 @@ -//! Retained usage projections and cancellable refresh ownership. -use super::Runtime; -use gpui::{Context, Task}; -use nocterm_ai::usage::{AccountUsageSource, Limits, RateLimit, UsageAgent, UsageReadError}; -use std::{ - collections::{HashMap, HashSet}, - sync::Arc, -}; +//! Plan limits retained from accepted ACP usage events. +use nocterm_ai::usage::{Limits, RateLimit}; +use std::collections::HashMap; + +#[derive(Default)] pub(super) struct UsageLimits { limits: HashMap, - reading: HashSet, - tasks: HashMap>, - source: Arc, } + impl UsageLimits { - pub(super) fn new(source: Arc) -> Self { - Self { - source, - limits: HashMap::new(), - reading: HashSet::new(), - tasks: HashMap::new(), - } - } pub(super) fn get(&self, id: &str) -> Option<&Limits> { self.limits.get(id).filter(|limits| !limits.is_empty()) } pub(super) fn merge(&mut self, id: String, limit: RateLimit) { self.limits.entry(id).or_default().merge(limit) } - fn complete(&mut self, id: String, limits: Result>, UsageReadError>) { - self.reading.remove(&id); - self.tasks.remove(&id); - if let Ok(Some(limits)) = limits { - self.limits.entry(id).or_default().replace(limits) - } - } - pub(super) fn refresh(&mut self, agent: UsageAgent, cx: &mut Context) { - let id = agent.id.clone(); - if !self.reading.insert(id.clone()) { - return; - } - let source = self.source.clone(); - let reading = cx - .background_executor() - .spawn(async move { source.read(agent).await }); - let key = id.clone(); - let task = cx.spawn(async move |runtime, cx| { - let limits = reading.await; - let _ = runtime.update(cx, |runtime, cx| { - runtime.usage.complete(id, limits); - cx.notify(); - }); - }); - self.tasks.insert(key, task); - } - pub(super) fn stop(&mut self) { - self.tasks.clear(); - self.reading.clear(); - } } diff --git a/crates/agent-settings-ui/src/controls.rs b/crates/agent-settings-ui/src/controls.rs index 410b9793..4a54a884 100644 --- a/crates/agent-settings-ui/src/controls.rs +++ b/crates/agent-settings-ui/src/controls.rs @@ -1,8 +1,7 @@ use super::AgentSettingsPage; use gpui_kit::{ - AnyElement, App, Context, SharedString, TestSupportExt as _, + AnyElement, App, Context, SharedString, component::{Disableable as _, switch::Switch}, - prelude::*, }; use nocterm_settings::SettingsSection; use nocterm_ui::form; @@ -16,7 +15,7 @@ pub(crate) fn toggle( Switch::new(id) .checked(checked) .disabled(disabled) - .on_click(cx.listener(move |this, checked: &bool, _, cx| { + .on_change(cx.listener(move |this, checked: &bool, _, cx| { let checked = *checked; this.save(move |settings| set(settings, checked), cx); })) @@ -29,16 +28,5 @@ pub(crate) fn text_row( description: impl Into, cx: &App, ) -> AnyElement { - let field = view.field(key); - gpui_kit::div() - .id(SharedString::from(format!("settings-field-{key}"))) - .test_support() - .child(form::stacked_row( - label, - description, - field.input(), - field.error(cx), - cx, - )) - .into_any_element() + form::text_field(view.field(key), key, label, description, cx) } diff --git a/crates/agent-settings-ui/src/lib.rs b/crates/agent-settings-ui/src/lib.rs index 04ea9f0d..b89afc4d 100644 --- a/crates/agent-settings-ui/src/lib.rs +++ b/crates/agent-settings-ui/src/lib.rs @@ -117,16 +117,11 @@ impl AgentSettingsPage { cx: &mut Context, ) { let key = key.into(); - let field = SettingsTextField::new( + let field = SettingsTextField::section( self.settings().clone(), key.clone(), - move |document| read(document.get::()), - move |document, text| { - let mut section = document.get::().clone(); - write(&mut section, text).map_err(nocterm_store::StoreError::edit)?; - document.set(section); - Ok(()) - }, + read, + write, window, cx, ); diff --git a/crates/agent-settings-ui/src/render.rs b/crates/agent-settings-ui/src/render.rs index ab66daef..88f197b8 100644 --- a/crates/agent-settings-ui/src/render.rs +++ b/crates/agent-settings-ui/src/render.rs @@ -29,6 +29,7 @@ pub(super) fn render( ) -> AnyElement { let ai = view.settings().setting::(cx).clone(); let off = !ai.enabled; + let available = view.agents().available(cx); let mut page = v_flex() .w_full() .child(form::page_header( @@ -43,7 +44,7 @@ pub(super) fn render( .when_some(view.ai.error.clone(), |page, error| page.child(form::error_text(error, cx))); let mut agents: Vec = Vec::new(); for id in view.ai.agents.clone() { - agents.push(agent(view, &id, &ai, off, cx)); + agents.push(agent(view, &id, &ai, &available, off, cx)); } agents.push(add_agent(view, off, cx)); page = page.child(form::section("Agents", agents, cx)); @@ -252,12 +253,13 @@ fn agent( view: &AgentSettingsPage, id: &str, ai: &AiSettings, + available: &nocterm_ai::AvailableAgents, off: bool, cx: &mut Context, ) -> AnyElement { let mut block = v_flex() .w_full() - .child(agent_heading(view, id, ai, off, cx)); + .child(agent_heading(view, id, ai, available, off, cx)); if view.ai.expanded.contains(id) { block = block.child(agent_details(view, id, cx)); } @@ -281,6 +283,7 @@ fn agent_heading( view: &AgentSettingsPage, id: &str, ai: &AiSettings, + available: &nocterm_ai::AvailableAgents, off: bool, cx: &mut Context, ) -> AnyElement { @@ -309,7 +312,7 @@ fn agent_heading( .small() .text_color(cx.theme().muted_foreground), ) - .child(agent_icon(view.agents().profile(id, cx)).small()) + .child(agent_icon(view.agents().profile_in(available, id)).small()) .child(div().text_sm().font_medium().child(name)) .child( div() diff --git a/crates/agent-storage/Cargo.toml b/crates/agent-storage/Cargo.toml index 307051d0..e03a32d0 100644 --- a/crates/agent-storage/Cargo.toml +++ b/crates/agent-storage/Cargo.toml @@ -13,11 +13,12 @@ layer = "adapter" nocterm-ai.workspace = true nocterm-core.workspace = true nocterm-session.workspace = true -serde_json.workspace = true toml.workspace = true uuid.workspace = true [dev-dependencies] +nocterm-ai = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } futures.workspace = true tempfile.workspace = true diff --git a/crates/agent-storage/src/catalog.rs b/crates/agent-storage/src/catalog.rs index 19aed2a7..4778973a 100644 --- a/crates/agent-storage/src/catalog.rs +++ b/crates/agent-storage/src/catalog.rs @@ -1,34 +1,13 @@ -//! Per-scope discovery verifies transcript authority before consulting sidecars. +//! Per-scope discovery reads authoritative transcript headers directly. use crate::{ input::Input, namespace::{Partition, Root}, - publication, }; use nocterm_ai::{ - history::{self, ChatSummary, ContextId, MAX_RESTORED, SavedChat}, - storage::{AgentStorageError, require_chat_scope}, + history::{self, ChatSummary, MAX_RESTORED}, + local_environment::ChatScope, + storage::{AgentStorageError, require_local_scope}, }; -const MAX_METADATA_BYTES: usize = 16 * 1024; - -pub(super) fn update_cache(partition: &Partition, chat: &SavedChat, queued: bool) { - let Ok(input) = Input::open(partition, &chat.header.id, "json") else { - return; - }; - let summary = ChatSummary::from_chat(chat).with_stamp(input.stamp(), queued); - write_cache(partition, &summary); -} -fn write_cache(partition: &Partition, summary: &ChatSummary) { - if let Ok(bytes) = serde_json::to_vec(summary) { - let _ = publication::publish(partition, &summary.id, "meta", &bytes); - } -} -fn cached(partition: &Partition, id: &str) -> Option { - let input = Input::open(partition, id, "meta").ok()?; - if input.stamp().bytes > MAX_METADATA_BYTES as u64 { - return None; - } - serde_json::from_slice(&input.prefix().ok()?).ok() -} fn summary(partition: &Partition, id: &str) -> Result, AgentStorageError> { let input = Input::open(partition, id, "json")?; let stamp = input.stamp(); @@ -39,37 +18,41 @@ fn summary(partition: &Partition, id: &str) -> Result, Agent if bytes.starts_with(b"{\"version\":2,") { return Err(history::HistoryCodecError::InvalidHeader.into()); } - history::decode_summary(&bytes, id, stamp)? + let summary = history::decode_summary(&bytes, id, stamp)?; + if summary + .as_ref() + .is_some_and(|row| row.transcript_version().wire() != 1) + { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + summary } else { let header = history::decode_catalog_header(&input.prefix()?, id)?; - require_chat_scope(partition.scope(), header.scope())?; + require_local_scope(partition.chat_scope(), &header.chat_scope())?; + if header.transcript_version().wire() != partition.version() { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } header.summary(stamp) }; if let Some(summary) = &summary { - require_chat_scope(partition.scope(), summary.scope)?; - if cached(partition, id).as_ref() != Some(summary) { - write_cache(partition, summary); - } + require_local_scope(partition.chat_scope(), &summary.chat_scope())?; } Ok(summary) } -pub(super) fn load(root: &Root, scope: ContextId) -> Result, AgentStorageError> { +pub(super) fn load(root: &Root, scope: &ChatScope) -> Result, AgentStorageError> { let Some(directory) = root.open(false)? else { return Ok(Vec::new()); }; - let scoped = Partition::scoped(&directory, scope, false)?; + let partitions = crate::namespace::readable(&directory, scope)?; let mut rows = Vec::new(); - if let Some(partition) = &scoped { - scan(partition, None, &mut rows)?; - } - if scope == ContextId::General { - scan(&Partition::legacy(directory), scoped.as_ref(), &mut rows)?; + for (index, partition) in partitions.iter().enumerate() { + scan(partition, &partitions[..index], &mut rows)?; } Ok(rows) } fn scan( partition: &Partition, - preferred: Option<&Partition>, + preferred: &[Partition], rows: &mut Vec, ) -> Result<(), AgentStorageError> { let mut entries = partition.directory().entries()?; @@ -77,9 +60,11 @@ fn scan( let Some(id) = Partition::id(&name) else { continue; }; - if let Some(preferred) = preferred - && preferred.exists(id)? - { + let mut shadowed = false; + for preferred in preferred { + shadowed |= preferred.exists(id)?; + } + if shadowed { continue; } let Ok(Some(row)) = summary(partition, id) else { diff --git a/crates/agent-storage/src/chats.rs b/crates/agent-storage/src/chats.rs index cefb3b62..e18ebed1 100644 --- a/crates/agent-storage/src/chats.rs +++ b/crates/agent-storage/src/chats.rs @@ -7,7 +7,11 @@ use crate::{ }; use nocterm_ai::{ history::{self, CatalogHeader, ChatSummary, ContextId, SavedChat, SharedChat}, - storage::{AgentStorageError, ChatRepository, StorageFuture, require_chat_scope}, + local_environment::ChatScope, + storage::{ + AgentStorageError, ChatRepository, PreparedChatBytes, PreparedChatLoad, StorageFuture, + require_local_scope, + }, }; use nocterm_session::local_fs::{LocalDirectory, LocalFs}; use std::{path::PathBuf, sync::Arc}; @@ -26,42 +30,69 @@ impl FileChatRepository { } } impl ChatRepository for FileChatRepository { - fn catalog(&self, scope: ContextId) -> StorageFuture> { + fn prepare_load_in( + &self, + scope: ChatScope, + id: String, + ) -> StorageFuture> { let root = self.root.clone(); - Box::pin(async move { catalog::load(&root, scope) }) + Box::pin(async move { + let (bytes, version) = read(&root, &scope, &id)?; + Ok( + Box::new(PreparedChatBytes::new(bytes, scope, id, Some(version))?) + as Box, + ) + }) + } + fn catalog(&self, scope: ContextId) -> StorageFuture> { + self.catalog_in(scope.into()) } fn load(&self, scope: ContextId, id: String) -> StorageFuture { + self.load_in(scope.into(), id) + } + fn delete(&self, scope: ContextId, id: String) -> StorageFuture<()> { + self.delete_in(scope.into(), id) + } + fn catalog_in(&self, scope: ChatScope) -> StorageFuture> { let root = self.root.clone(); - Box::pin(async move { load(&root, scope, &id) }) + Box::pin(async move { catalog::load(&root, &scope) }) + } + fn load_in(&self, scope: ChatScope, id: String) -> StorageFuture { + let root = self.root.clone(); + Box::pin(async move { load(&root, &scope, &id) }) } fn save(&self, chat: Arc) -> StorageFuture<()> { let root = self.root.clone(); Box::pin(async move { save(&root, &chat) }) } - fn delete(&self, scope: ContextId, id: String) -> StorageFuture<()> { + fn delete_in(&self, scope: ChatScope, id: String) -> StorageFuture<()> { let root = self.root.clone(); - Box::pin(async move { delete(&root, scope, &id) }) + Box::pin(async move { delete(&root, &scope, &id) }) } } + fn requested( root: &Arc, - scope: ContextId, + scope: &ChatScope, id: &str, ) -> Result, AgentStorageError> { - if let Some(partition) = Partition::scoped(root, scope, false)? - && partition.exists(id)? - { - return Ok(Some(partition)); - } - if scope == ContextId::General { - let legacy = Partition::legacy(root.clone()); - if legacy.exists(id)? { - return Ok(Some(legacy)); + for partition in crate::namespace::readable(root, scope)? { + if partition.exists(id)? { + return Ok(Some(partition)); } } Ok(None) } -fn load(root: &Root, scope: ContextId, id: &str) -> Result { +fn load(root: &Root, scope: &ChatScope, id: &str) -> Result { + let (bytes, version) = read(root, scope, id)?; + let chat = history::decode(&bytes, id)?; + require_local_scope(scope, &chat.header.chat_scope())?; + if version != chat.version { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + Ok(chat) +} +fn read(root: &Root, scope: &ChatScope, id: &str) -> Result<(Vec, u32), AgentStorageError> { crate::namespace::validate_id(id)?; let directory = root .open(false)? @@ -74,13 +105,10 @@ fn load(root: &Root, scope: ContextId, id: &str) -> Result Result<(), AgentStorageError> { - if (partition.is_legacy() && version != 1) || (!partition.is_legacy() && version != 2) { + if partition.version() != version { return Err(history::HistoryCodecError::InvalidHeader.into()); } Ok(()) @@ -88,7 +116,8 @@ fn check_version(partition: &Partition, version: u32) -> Result<(), AgentStorage pub(super) fn header(partition: &Partition, id: &str) -> Result { let input = Input::open(partition, id, "json")?; let header = history::decode_catalog_header(&input.prefix()?, id)?; - require_chat_scope(partition.scope(), header.scope())?; + require_local_scope(partition.chat_scope(), &header.chat_scope())?; + check_version(partition, header.transcript_version().wire())?; Ok(header) } fn existing(partition: &Partition, id: &str) -> Result<(), AgentStorageError> { @@ -97,7 +126,7 @@ fn existing(partition: &Partition, id: &str) -> Result<(), AgentStorageError> { } if partition.is_legacy() { let chat = history::decode(&Input::open(partition, id, "json")?.all()?, id)?; - require_chat_scope(partition.scope(), chat.header.scope)?; + require_local_scope(partition.chat_scope(), &chat.header.chat_scope())?; check_version(partition, chat.version) } else { header(partition, id).map(|_| ()) @@ -105,60 +134,43 @@ fn existing(partition: &Partition, id: &str) -> Result<(), AgentStorageError> { } fn save(root: &Root, chat: &SharedChat) -> Result<(), AgentStorageError> { if let Some((draft, updated)) = chat.metadata.draft_patch() { - let mut original = load(root, chat.header.scope, &chat.header.id)?; + let mut original = load(root, &chat.header.chat_scope(), &chat.header.id)?; original.draft = draft; original.header.updated = updated; - return save_record( - root, - &original, - history::encode(&original)?, - !original.queue.is_empty(), - ); - } - save_record( - root, - &chat.metadata, - history::encode_shared(chat)?, - !chat.queue.is_empty(), - ) + return save_record(root, &original, history::encode(&original)?); + } + save_record(root, &chat.metadata, history::encode_shared(chat)?) } -fn save_record( - root: &Root, - chat: &SavedChat, - bytes: Vec, - queued: bool, -) -> Result<(), AgentStorageError> { +fn save_record(root: &Root, chat: &SavedChat, bytes: Vec) -> Result<(), AgentStorageError> { let directory = root .open(true)? .ok_or_else(|| AgentStorageError::NotFound(chat.header.id.clone()))?; - let partition = if chat.version == 1 { - require_chat_scope(ContextId::General, chat.header.scope)?; - Partition::legacy(directory.clone()) - } else { - Partition::scoped(&directory, chat.header.scope, true)? - .ok_or_else(|| AgentStorageError::NotFound(chat.header.id.clone()))? + let scope = chat.header.chat_scope(); + let partition = match chat.version { + 1 => { + require_local_scope(&ContextId::General.into(), &scope)?; + Some(Partition::legacy(directory.clone())) + } + 2 => Partition::scoped(&directory, scope.context(), true)?, + 3 => Partition::environment(&directory, &scope, true)?, + other => return Err(history::HistoryCodecError::UnsupportedVersion(other).into()), }; + let partition = partition.ok_or_else(|| AgentStorageError::NotFound(chat.header.id.clone()))?; existing(&partition, &chat.header.id)?; publication::publish(&partition, &chat.header.id, "json", &bytes)?; - catalog::update_cache(&partition, chat, queued); Ok(()) } -fn delete(root: &Root, scope: ContextId, id: &str) -> Result<(), AgentStorageError> { +fn delete(root: &Root, scope: &ChatScope, id: &str) -> Result<(), AgentStorageError> { crate::namespace::validate_id(id)?; let Some(directory) = root.open(false)? else { return Ok(()); }; - let scoped = Partition::scoped(&directory, scope, false)?; - // Validate both before mutation. Legacy is removed first so an interrupted - // delete cannot reveal its stale fallback after removing the scoped document. - let legacy = (scope == ContextId::General).then(|| Partition::legacy(directory)); - if let Some(legacy) = &legacy { - existing(legacy, id)?; - } - if let Some(scoped) = &scoped { - existing(scoped, id)?; + let partitions = crate::namespace::readable(&directory, scope)?; + for partition in &partitions { + existing(partition, id)?; } - for partition in legacy.into_iter().chain(scoped) { + // Oldest first: interruption cannot expose a previously shadowed fallback. + for partition in partitions.iter().rev() { partition.remove(id, "json")?; let _ = partition.remove(id, "meta"); } diff --git a/crates/agent-storage/src/input.rs b/crates/agent-storage/src/input.rs index bfdcc228..d16a2acb 100644 --- a/crates/agent-storage/src/input.rs +++ b/crates/agent-storage/src/input.rs @@ -1,8 +1,9 @@ //! Bounded reads retain the original opened source and its supplied file facts. use crate::namespace::Partition; use nocterm_ai::{ - history::{self, ContextId, FileStamp, HistoryCodecError, MAX_FILE_BYTES}, - storage::{AgentStorageError, require_chat_scope}, + history::{self, FileStamp, HistoryCodecError, MAX_FILE_BYTES}, + local_environment::ChatScope, + storage::{AgentStorageError, require_local_scope}, }; use nocterm_session::local_fs::{LocalFsError, LocalSource}; @@ -59,11 +60,11 @@ impl Input { pub(super) fn scoped_record( mut self, id: &str, - scope: ContextId, + scope: &ChatScope, ) -> Result, AgentStorageError> { let mut bytes = self.read(history::MAX_HEADER_BYTES)?; let header = history::decode_catalog_header(&bytes, id)?; - require_chat_scope(scope, header.scope())?; + require_local_scope(scope, &header.chat_scope())?; self.fill(&mut bytes, MAX_FILE_BYTES as usize + 1)?; self.finish(bytes) } diff --git a/crates/agent-storage/src/lib.rs b/crates/agent-storage/src/lib.rs index 2ef8e4ec..ab6628d5 100644 --- a/crates/agent-storage/src/lib.rs +++ b/crates/agent-storage/src/lib.rs @@ -1,17 +1,16 @@ //! Bounded filesystem repositories for agent history and preferences. -//! Owns: private files, transcript/catalog discovery, sidecar stamps, draft merges +//! Owns: private files, transcript/catalog discovery, draft merges //! and native chat identity/time facts. //! Does not own: conversation rules, views, scheduling or shutdown deadlines. //! Ports used/provided: injected session LocalFs/LocalDirectory; AI //! AgentStateRepository, ChatRepository and -//! ChatMetadataSource. +//! ChatMetadataSource and bounded PreparedChatLoad using core allocation leases. //! Settings section: none. Globals: none. -//! Invariants: (scope, ID) selects one held namespace; v2 bounded catalog headers -//! are authoritative before sidecars. General v1 retains full bounded scans. The -//! configured private root has trusted ancestry; generated descendants never use -//! absolute operations. Project storage requires held identity; portable General -//! retains the adapter's documented active-ancestor limitation. Cache errors never -//! revoke publication. +//! Invariants: (environment, context, ID) selects one held v3 namespace. Bounded +//! authoritative headers precede full reads; obsolete sidecars are ignored. Old v1/v2 history is +//! ThisComputer only and retains its bytes and paths. The configured private root +//! has trusted ancestry; generated descendants use held operations. Portable +//! identities support only old General history. mod catalog; mod chats; mod input; diff --git a/crates/agent-storage/src/namespace.rs b/crates/agent-storage/src/namespace.rs index 398634cf..33977dd2 100644 --- a/crates/agent-storage/src/namespace.rs +++ b/crates/agent-storage/src/namespace.rs @@ -1,5 +1,5 @@ //! Context paths are selected once, then generated descendants use held operations. -use nocterm_ai::{history::ContextId, storage::AgentStorageError}; +use nocterm_ai::{history::ContextId, local_environment::ChatScope, storage::AgentStorageError}; use nocterm_session::local_fs::{LocalDirectory, LocalFs, LocalFsError}; use std::{ ffi::{OsStr, OsString}, @@ -63,15 +63,15 @@ impl Root { #[derive(Clone)] pub(super) struct Partition { directory: Arc, - scope: ContextId, - legacy: bool, + scope: ChatScope, + version: u32, } impl Partition { pub(super) fn legacy(directory: Arc) -> Self { Self { directory, - scope: ContextId::General, - legacy: true, + scope: ContextId::General.into(), + version: 1, } } pub(super) fn scoped( @@ -82,38 +82,42 @@ impl Partition { if scope != ContextId::General && !root.identity().is_held() { return Err(AgentStorageError::ScopeUnsupported(scope)); } - let mut directory = root.clone(); - let names = match scope { - ContextId::General => vec![OsString::from("scoped-v2"), OsString::from("general")], - ContextId::Project(id) => { - vec!["scoped-v2".into(), "projects".into(), id.to_string().into()] - } - }; - for name in names { - directory = match if create { - directory.create_child(&name) - } else { - directory.child(&name) - } { - Ok(directory) => directory, - Err(LocalFsError::NotFound(_)) => return Ok(None), - Err(error) => return Err(error.into()), - }; - if scope != ContextId::General && !directory.identity().is_held() { - return Err(AgentStorageError::ScopeUnsupported(scope)); - } + let names = context_names(scope); + let names = std::iter::once(OsString::from("scoped-v2")) + .chain(names) + .collect(); + open_partition( + root, + scope.into(), + create, + names, + 2, + scope != ContextId::General, + ) + } + pub(super) fn environment( + root: &Arc, + scope: &ChatScope, + create: bool, + ) -> Result, AgentStorageError> { + if !root.identity().is_held() && !create && scope.environment().is_this_computer() { + return Ok(None); } - Ok(Some(Self { - directory, - scope, - legacy: false, - })) + let mut names = vec![ + OsString::from("scoped-v3"), + scope.environment().storage_key().into(), + ]; + names.extend(context_names(scope.context())); + open_partition(root, scope.clone(), create, names, 3, true) + } + pub(super) fn chat_scope(&self) -> &ChatScope { + &self.scope } - pub(super) fn scope(&self) -> ContextId { - self.scope + pub(super) fn version(&self) -> u32 { + self.version } pub(super) fn is_legacy(&self) -> bool { - self.legacy + self.version == 1 } pub(super) fn directory(&self) -> &Arc { &self.directory @@ -137,3 +141,65 @@ impl Partition { Some(name) } } + +fn context_names(scope: ContextId) -> Vec { + match scope { + ContextId::General => vec!["general".into()], + ContextId::Project(id) => vec!["projects".into(), id.to_string().into()], + } +} +fn open_partition( + root: &Arc, + scope: ChatScope, + create: bool, + names: Vec, + version: u32, + held: bool, +) -> Result, AgentStorageError> { + let mut directory = root.clone(); + if held && !directory.identity().is_held() { + return Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))); + } + for name in names { + directory = match if create { + directory.create_child(&name) + } else { + directory.child(&name) + } { + Ok(directory) => directory, + Err(LocalFsError::NotFound(_)) => return Ok(None), + Err(error) => return Err(error.into()), + }; + if held && !directory.identity().is_held() { + return Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))); + } + } + Ok(Some(Partition { + directory, + scope, + version, + })) +} + +pub(super) fn readable( + root: &Arc, + scope: &ChatScope, +) -> Result, AgentStorageError> { + let mut partitions = Vec::new(); + if let Some(current) = Partition::environment(root, scope, false)? { + partitions.push(current); + } + if scope.environment().is_this_computer() { + if let Some(previous) = Partition::scoped(root, scope.context(), false)? { + partitions.push(previous); + } + if scope.context() == ContextId::General { + partitions.push(Partition::legacy(root.clone())); + } + } + Ok(partitions) +} diff --git a/crates/agent-storage/tests/prepared_history.rs b/crates/agent-storage/tests/prepared_history.rs new file mode 100644 index 00000000..b3127850 --- /dev/null +++ b/crates/agent-storage/tests/prepared_history.rs @@ -0,0 +1,136 @@ +//! Prepared native history keeps its original bytes and decoded allocation together. +use futures::executor::block_on; +use nocterm_agent_storage::FileChatRepository; +use nocterm_ai::{ + history::{self, ContextId, SavedChat, SharedChat}, + storage::{AgentStorageError, ChatRepository, PreparedChatBytes, PreparedChatLoad}, + testing::{InMemoryAgentRepository, StorageOperation}, +}; +use nocterm_core::allocation::AllocationLease; +use nocterm_session::testing::InMemoryFs; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; + +const ID: &str = "00000000-0000-4000-8000-0000000000a1"; + +fn chat(draft: &str) -> SavedChat { + let mut chat = SavedChat::for_identity("agent".into(), ID.into(), 1); + chat.draft = Some(draft.into()); + chat +} + +fn prepared() -> Box { + Box::new( + PreparedChatBytes::new( + history::encode(&chat("original")).unwrap(), + ContextId::General.into(), + ID.into(), + Some(2), + ) + .unwrap(), + ) +} + +#[test] +fn prepared_decode_refuses_insufficient_bytes_and_missing_payload_slot() { + let source = prepared(); + let bytes = source.resident_bytes(); + assert!(bytes > 0); + let result = block_on(source.decode(AllocationLease::new(bytes - 1, 1, ()))); + assert!(matches!( + result, + Err(AgentStorageError::ResidentCapacity { .. }) + )); + let source = prepared(); + let bytes = source.resident_bytes(); + let result = block_on(source.decode(AllocationLease::new(bytes, 0, ()))); + assert!(matches!(result, Err(AgentStorageError::PayloadCapacity))); +} + +struct Release(Arc); +impl Drop for Release { + fn drop(&mut self) { + self.0.fetch_add(1, Ordering::SeqCst); + } +} + +#[test] +fn detached_decode_and_returned_generation_retain_the_same_allocation() { + let source = prepared(); + let released = Arc::new(AtomicUsize::new(0)); + let bytes = source.resident_bytes(); + let decode = source.decode(AllocationLease::new(bytes, 1, Release(released.clone()))); + assert_eq!(released.load(Ordering::SeqCst), 0); + let generation = std::thread::spawn(move || Arc::new(block_on(decode).unwrap())) + .join() + .unwrap(); + assert_eq!(generation.value().draft.as_deref(), Some("original")); + assert_eq!(generation.allocation().bytes(), bytes); + assert_eq!(released.load(Ordering::SeqCst), 0); + drop(generation); + assert_eq!(released.load(Ordering::SeqCst), 1); +} + +#[test] +fn native_prepared_history_survives_delete_and_replacement_without_reopening() { + let directory = tempfile::tempdir().unwrap(); + let filesystem = Arc::new(InMemoryFs::default()); + for ancestor in directory + .path() + .ancestors() + .collect::>() + .into_iter() + .rev() + { + filesystem.seed_native_directory(ancestor).unwrap(); + } + let repository = FileChatRepository::new(directory.path().into(), filesystem); + let original = SharedChat { + metadata: chat("original"), + queue: Vec::new(), + }; + block_on(repository.save(Arc::new(original))).unwrap(); + let source = + block_on(repository.prepare_load_in(ContextId::General.into(), ID.into())).unwrap(); + block_on(repository.delete(ContextId::General, ID.into())).unwrap(); + let replacement = SharedChat { + metadata: chat("replacement"), + queue: Vec::new(), + }; + block_on(repository.save(Arc::new(replacement))).unwrap(); + let bytes = source.resident_bytes(); + let loaded = block_on(source.decode(AllocationLease::new(bytes, 1, ()))).unwrap(); + assert_eq!(loaded.value().draft.as_deref(), Some("original")); + let current = block_on(repository.load(ContextId::General, ID.into())).unwrap(); + assert_eq!(current.draft.as_deref(), Some("replacement")); +} + +#[test] +fn shared_repository_preparation_uses_existing_load_failures_and_frozen_bytes() { + let repository = InMemoryAgentRepository::default(); + repository.seed_chat(&chat("original")).unwrap(); + repository.fail_next( + StorageOperation::Load(ID.into()), + AgentStorageError::Interrupted, + ); + assert!(matches!( + block_on(repository.prepare_load_in(ContextId::General.into(), ID.into())), + Err(AgentStorageError::Interrupted) + )); + let source = + block_on(repository.prepare_load_in(ContextId::General.into(), ID.into())).unwrap(); + repository.seed_chat(&chat("replacement")).unwrap(); + let bytes = source.resident_bytes(); + let loaded = block_on(source.decode(AllocationLease::new(bytes, 1, ()))).unwrap(); + assert_eq!(loaded.value().draft.as_deref(), Some("original")); + assert_eq!( + repository.operations(), + vec![ + StorageOperation::Load(ID.into()), + StorageOperation::Load(ID.into()), + StorageOperation::Decoded(ID.into()), + ] + ); +} diff --git a/crates/agent-tools/Cargo.toml b/crates/agent-tools/Cargo.toml new file mode 100644 index 00000000..16786662 --- /dev/null +++ b/crates/agent-tools/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "nocterm-agent-tools" +description = "Bounded agent command execution behind session contracts." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "adapter" + +[dependencies] +nocterm-session.workspace = true +futures.workspace = true +async-channel.workspace = true + +[dev-dependencies] +nocterm-session = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/agent-chat/src/thread/execution/jobs.rs b/crates/agent-tools/src/jobs.rs similarity index 59% rename from crates/agent-chat/src/thread/execution/jobs.rs rename to crates/agent-tools/src/jobs.rs index 2edb2901..f36d627e 100644 --- a/crates/agent-chat/src/thread/execution/jobs.rs +++ b/crates/agent-tools/src/jobs.rs @@ -1,116 +1,40 @@ //! Bounded command ownership; cancellation lives independently of read waiters. use futures::FutureExt as _; -use nocterm_session::{ExecExit, ExecRequest, HostExec}; +use nocterm_session::command_jobs::{ + CommandJobs, CommandSnapshot as Snapshot, CommandStart, CommandState as State, JobError, +}; +use nocterm_session::{ExecRequest, HostExec}; use std::{ collections::BTreeMap, sync::{Arc, Mutex}, - time::Duration, }; -pub(in crate::thread) const MAX_ACTIVE: usize = 4; +const MAX_ACTIVE: usize = 4; const MAX_RETAINED: usize = 16; const MAX_STDOUT: usize = 64 * 1024; -/// Why a command was not started or found; the agent sees the text. -#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] -pub(in crate::thread) enum JobError { - #[error("This chat already has four active commands. Read or cancel one first.")] - Busy, - #[error("Unknown command for this chat and terminal.")] - Unknown, -} -impl From for nocterm_ai::ToolFailure { - fn from(error: JobError) -> Self { - error.to_string().into() - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(in crate::thread) enum State { - Starting, - Running, - Exited, - TimedOut, - Cancelled, - Failed, -} -impl State { - pub(in crate::thread) fn as_str(self) -> &'static str { - match self { - Self::Starting => "starting", - Self::Running => "running", - Self::Exited => "exited", - Self::TimedOut => "timed_out", - Self::Cancelled => "cancelled", - Self::Failed => "failed", - } - } - fn active(self) -> bool { - matches!(self, Self::Starting | Self::Running) - } -} - -#[derive(Clone, Debug)] -pub(in crate::thread) struct Snapshot { - state: State, - stdout: Vec, - total_bytes: u64, - exit: Option, - error: Option, +fn append_output(snapshot: &mut Snapshot, bytes: &[u8]) { + snapshot.total_bytes = snapshot.total_bytes.saturating_add(bytes.len() as u64); + let keep = bytes + .len() + .min(MAX_STDOUT.saturating_sub(snapshot.stdout.len())); + snapshot.stdout.extend_from_slice(&bytes[..keep]); } -impl Default for Snapshot { - fn default() -> Self { - Self { - state: State::Starting, - stdout: Vec::new(), - total_bytes: 0, - exit: None, - error: None, - } - } -} -impl Snapshot { - pub(in crate::thread) fn state(&self) -> State { - self.state - } - pub(in crate::thread) fn stdout(&self) -> &[u8] { - &self.stdout - } - pub(in crate::thread) fn total_bytes(&self) -> u64 { - self.total_bytes - } - pub(in crate::thread) fn exit(&self) -> Option<&ExecExit> { - self.exit.as_ref() - } - pub(in crate::thread) fn error(&self) -> Option<&str> { - self.error.as_deref() - } - pub(in crate::thread) fn active(&self) -> bool { - self.state.active() - } - fn append(&mut self, bytes: &[u8]) { - self.total_bytes = self.total_bytes.saturating_add(bytes.len() as u64); - let keep = bytes - .len() - .min(MAX_STDOUT.saturating_sub(self.stdout.len())); - self.stdout.extend_from_slice(&bytes[..keep]); - } -} -pub(in crate::thread) struct Job { +struct Job { terminal_id: String, executor: Arc, snapshot: Arc>, cancel: async_channel::Sender<()>, } impl Job { - pub(in crate::thread) fn snapshot(&self) -> Snapshot { + fn snapshot(&self) -> Snapshot { self.snapshot .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .clone() } - pub(in crate::thread) fn cancel(&self, state: State) { + fn cancel(&self, state: State) { let mut snapshot = self .snapshot .lock() @@ -127,21 +51,12 @@ impl Drop for Job { } } #[derive(Default)] -pub(in crate::thread) struct Jobs { +pub(crate) struct Jobs { jobs: BTreeMap, next: u64, } -pub(in crate::thread) struct CommandStart { - id: String, - run: futures::future::BoxFuture<'static, ()>, -} -impl CommandStart { - pub(in crate::thread) fn into_run(self) -> (String, futures::future::BoxFuture<'static, ()>) { - (self.id, self.run) - } -} impl Jobs { - pub(in crate::thread) fn start( + fn start_owned( &mut self, terminal: String, executor: Arc, @@ -149,10 +64,10 @@ impl Jobs { deadline: impl std::future::Future + Send + 'static, ) -> Result { let (id, state, cancel) = self.insert(terminal, executor.clone())?; - Ok(CommandStart { + Ok(CommandStart::new( id, - run: Box::pin(run(executor, request, state, cancel, deadline)), - }) + Box::pin(run(executor, request, state, cancel, deadline)), + )) } fn insert( @@ -195,24 +110,24 @@ impl Jobs { ); Ok((format!("c{}", self.next), snapshot, receiver)) } - pub(in crate::thread) fn get(&self, id: &str, terminal: &str) -> Result<&Job, JobError> { + fn get(&self, id: &str, terminal: &str) -> Result<&Job, JobError> { id.strip_prefix('c') .and_then(|id| id.parse::().ok()) .and_then(|id| self.jobs.get(&id)) .filter(|job| job.terminal_id == terminal) .ok_or(JobError::Unknown) } - pub(in crate::thread) fn cancel_terminal(&self, terminal: &str) { + fn cancel_terminal(&self, terminal: &str) { for job in self.jobs.values().filter(|job| job.terminal_id == terminal) { job.cancel(State::Cancelled); } } - pub(in crate::thread) fn cancel_all(&self) { + fn cancel_all(&self) { for job in self.jobs.values() { job.cancel(State::Cancelled); } } - pub(in crate::thread) fn active(&self) -> impl Iterator)> { + fn active(&self) -> impl Iterator)> { self.jobs .values() .filter(|job| job.snapshot().active()) @@ -244,7 +159,7 @@ async fn run( .unwrap_or_else(std::sync::PoisonError::into_inner); if state.active() { state.state = State::Failed; - state.error = Some(error.to_string()); + state.error = Some(error); } return; } @@ -259,10 +174,12 @@ async fn run( state.state = State::Running; } while let Some(chunk) = output.next().await { - snapshot - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .append(&chunk); + append_output( + &mut snapshot + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner), + &chunk, + ); } let exit = output.exit().await; let mut state = snapshot @@ -299,9 +216,40 @@ async fn run( } } } -pub(in crate::thread) fn timeout(value: Option) -> Duration { - Duration::from_millis(value.unwrap_or(30_000)) -} -#[cfg(test)] -mod tests; +impl CommandJobs for Jobs { + fn start( + &mut self, + terminal: String, + executor: Arc, + request: ExecRequest, + deadline: futures::future::BoxFuture<'static, ()>, + ) -> Result { + self.start_owned(terminal, executor, request, deadline) + } + fn snapshot(&self, id: &str, terminal: &str) -> Result { + Ok(self.get(id, terminal)?.snapshot()) + } + fn cancel(&self, id: &str, terminal: &str) -> Result { + let job = self.get(id, terminal)?; + job.cancel(State::Cancelled); + Ok(job.snapshot()) + } + fn cancel_all(&self) { + Jobs::cancel_all(self); + } + fn has_active(&self) -> bool { + Jobs::active(self).next().is_some() + } + fn revoke_replaced(&self, current: &[(String, Option>)]) { + for (id, executor) in Jobs::active(self) { + let replacement = current + .iter() + .find(|(candidate, _)| candidate == id) + .and_then(|(_, executor)| executor.as_ref()); + if !replacement.is_some_and(|replacement| Arc::ptr_eq(replacement, executor)) { + self.cancel_terminal(id); + } + } + } +} diff --git a/crates/agent-tools/src/lib.rs b/crates/agent-tools/src/lib.rs new file mode 100644 index 00000000..3efe7e22 --- /dev/null +++ b/crates/agent-tools/src/lib.rs @@ -0,0 +1,20 @@ +//! Bounded agent commands without GUI state. +//! +//! Owns: per-chat command admission, continuous stdout draining, retained outcomes, +//! cancellation and absolute deadlines, retaining the original executor for every job. +//! Does not own: approvals, SSH authentication, model execution or views. +//! Ports used: session::HostExec. Ports provided: +//! session::command_jobs::{CommandJobs, CommandJobsFactory}. +//! Settings sections: none. Globals: none. +//! Invariants: at most four active and sixteen retained commands per registry; +//! stdout retains a 64KiB prefix while all bytes are drained and counted; +//! cancelling never invents a successful exit or replaces an accepted executor. +mod jobs; + +#[derive(Default)] +pub struct JobsFactory; +impl nocterm_session::command_jobs::CommandJobsFactory for JobsFactory { + fn create(&self) -> Box { + Box::::default() + } +} diff --git a/crates/agent-tools/tests/command_lifecycle.rs b/crates/agent-tools/tests/command_lifecycle.rs new file mode 100644 index 00000000..ec989532 --- /dev/null +++ b/crates/agent-tools/tests/command_lifecycle.rs @@ -0,0 +1,331 @@ +use futures::{ + FutureExt as _, + future::{BoxFuture, pending, ready}, +}; +use nocterm_agent_tools::JobsFactory; +use nocterm_session::{ + ErrorTail, ExecError, ExecRequest, HostExec, + command_jobs::{CommandJobs, CommandJobsFactory, CommandState, JobError}, + testing::FakeExec, +}; +use std::sync::Arc; + +fn start( + jobs: &mut dyn CommandJobs, + executor: Arc, + terminal: &str, +) -> (String, BoxFuture<'static, ()>) { + jobs.start( + terminal.into(), + executor, + ExecRequest::new("program"), + pending().boxed(), + ) + .unwrap() + .into_run() +} + +#[test] +fn four_active_commands_refuse_fifth_and_terminal_or_chat_foreign_ids() { + let mut jobs = JobsFactory.create(); + assert!(!jobs.has_active()); + let executor = Arc::new(FakeExec::default()); + let mut runs = Vec::new(); + for _ in 0..4 { + runs.push(start(&mut *jobs, executor.clone(), "original")); + } + assert!( + jobs.has_active(), + "unpolled starts must keep their owner active" + ); + assert!(matches!( + jobs.start( + "original".into(), + executor.clone(), + ExecRequest::new("no"), + pending().boxed() + ), + Err(JobError::Busy) + )); + for (id, terminal) in [ + (runs[0].0.as_str(), "foreign"), + ("c99", "original"), + ("bad", "original"), + ] { + assert!(matches!( + jobs.snapshot(id, terminal), + Err(JobError::Unknown) + )); + } + assert!(matches!( + JobsFactory.create().snapshot(&runs[0].0, "original"), + Err(JobError::Unknown) + )); + assert!(executor.requests().is_empty()); +} + +#[test] +fn cancellation_or_elapsed_deadline_before_poll_never_executes() { + futures::executor::block_on(async { + for cancelled in [true, false] { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let (id, run) = jobs + .start( + "t".into(), + executor.clone(), + ExecRequest::new("no"), + ready(()).boxed(), + ) + .unwrap() + .into_run(); + if cancelled { + jobs.cancel(&id, "t").unwrap(); + } + run.await; + assert!(executor.requests().is_empty()); + assert_eq!( + jobs.snapshot(&id, "t").unwrap().state(), + if cancelled { + CommandState::Cancelled + } else { + CommandState::TimedOut + } + ); + } + }); +} + +#[test] +fn output_keeps_prefix_while_draining_every_chunk_and_preserving_stderr_exit_and_arguments() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let request = ExecRequest::new("program") + .args(["a b", "$(literal)", "", "'quote'"]) + .stdin(b"first\nsecond\n".to_vec()); + let (id, run) = jobs + .start( + "t".into(), + executor.clone(), + request.clone(), + pending().boxed(), + ) + .unwrap() + .into_run(); + let produce = async move { + for _ in 0..40 { + assert!(sink.send(vec![b'x'; 4096]).await); + } + let mut errors = ErrorTail::default(); + errors.push(&vec![b'e'; 8192]); + errors.push(b"\nlast error"); + sink.finish(errors.exit(Some(7))); + }; + futures::join!(run, produce); + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Exited); + assert_eq!(snapshot.stdout(), vec![b'x'; 65536]); + assert_eq!(snapshot.total_bytes(), 40 * 4096); + assert_eq!(snapshot.exit().unwrap().status, Some(7)); + assert_eq!(snapshot.exit().unwrap().stderr.len(), 4096); + assert!(snapshot.exit().unwrap().stderr.ends_with("last error")); + assert_eq!(executor.requests(), vec![request]); + }); +} + +#[test] +fn silent_exit_finishes_without_output() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::new(vec![("program", Ok(("", 0, "")))])); + let (id, run) = start(&mut *jobs, executor, "t"); + run.await; + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Exited); + assert_eq!(snapshot.exit().unwrap().status, Some(0)); + assert!(snapshot.stdout().is_empty()); + }); +} + +#[test] +fn cancelling_running_command_drops_output_and_retains_unknown_exit() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let (id, mut run) = start(&mut *jobs, executor.clone(), "t"); + assert!(run.as_mut().now_or_never().is_none()); + assert_eq!(executor.requests().len(), 1); + assert!(sink.send(b"running".to_vec()).await); + assert!(run.as_mut().now_or_never().is_none()); + jobs.cancel(&id, "t").unwrap(); + run.await; + sink.closed().await; + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Cancelled); + assert!(snapshot.exit().is_none()); + assert!(sink.is_closed()); + }); +} + +#[test] +fn retained_commands_evict_oldest_completed_result_and_allow_new_admission() { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let mut ids = Vec::new(); + for _ in 0..17 { + let (id, _run) = start(&mut *jobs, executor.clone(), "t"); + jobs.cancel(&id, "t").unwrap(); + ids.push(id); + } + assert!(matches!( + jobs.snapshot(&ids[0], "t"), + Err(JobError::Unknown) + )); + for id in &ids[1..] { + assert_eq!( + jobs.snapshot(id, "t").unwrap().state(), + CommandState::Cancelled + ); + } + assert!(!jobs.has_active()); +} + +#[test] +fn completed_result_churn_never_evicts_an_older_running_command() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let (running, mut run) = start(&mut *jobs, executor.clone(), "live"); + assert!(run.as_mut().now_or_never().is_none()); + let mut completed = Vec::new(); + for _ in 0..20 { + let (id, _run) = start(&mut *jobs, executor.clone(), "short"); + jobs.cancel(&id, "short").unwrap(); + completed.push(id); + } + assert!(jobs.has_active()); + assert_eq!( + jobs.snapshot(&running, "live").unwrap().state(), + CommandState::Running + ); + assert!(matches!( + jobs.snapshot(&completed[0], "short"), + Err(JobError::Unknown) + )); + assert!(!sink.is_closed()); + assert!(matches!( + jobs.cancel(&running, "short"), + Err(JobError::Unknown) + )); + assert!(jobs.snapshot(&running, "live").unwrap().active()); + jobs.cancel(&running, "live").unwrap(); + assert!(!jobs.has_active()); + run.await; + sink.closed().await; + }); +} + +#[test] +fn replacement_executor_revokes_only_its_original_terminal() { + let mut jobs = JobsFactory.create(); + let original: Arc = Arc::new(FakeExec::default()); + let replacement: Arc = Arc::new(FakeExec::default()); + let (first, _run) = start(&mut *jobs, original.clone(), "first"); + let (other, _run) = start(&mut *jobs, original.clone(), "other"); + jobs.revoke_replaced(&[ + ("first".into(), Some(replacement)), + ("other".into(), Some(original)), + ]); + assert_eq!( + jobs.snapshot(&first, "first").unwrap().state(), + CommandState::Cancelled + ); + assert!(jobs.snapshot(&other, "other").unwrap().active()); + jobs.cancel_all(); + assert!(!jobs.snapshot(&other, "other").unwrap().active()); +} + +#[test] +fn startup_failure_preserves_the_typed_transport_error() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::new(vec![( + "program", + Err(ExecError::Disconnected), + )])); + let (id, run) = start(&mut *jobs, executor, "t"); + run.await; + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Failed); + assert!(matches!(snapshot.error(), Some(ExecError::Disconnected))); + assert!(snapshot.exit().is_none()); + }); +} + +#[test] +fn absolute_deadline_covers_suspended_startup_and_silent_running_output() { + futures::executor::block_on(async { + for suspended in [true, false] { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + if suspended { + executor.suspend(nocterm_session::testing::Gate::pending()); + } + let (expire, deadline) = async_channel::bounded::<()>(1); + let deadline = async move { + let _ = deadline.recv().await; + } + .boxed(); + let (id, mut run) = jobs + .start( + "t".into(), + executor.clone(), + ExecRequest::new("program"), + deadline, + ) + .unwrap() + .into_run(); + assert!(run.as_mut().now_or_never().is_none()); + assert_eq!(executor.requests().len(), 1); + assert_eq!( + jobs.snapshot(&id, "t").unwrap().state(), + if suspended { + CommandState::Starting + } else { + CommandState::Running + } + ); + expire.close(); + run.await; + assert_eq!( + jobs.snapshot(&id, "t").unwrap().state(), + CommandState::TimedOut + ); + assert!(jobs.snapshot(&id, "t").unwrap().exit().is_none()); + sink.closed().await; + } + }); +} + +#[test] +fn dropping_the_registry_cancels_a_runner_even_when_its_read_waiter_is_gone() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let (id, mut run) = start(&mut *jobs, executor, "t"); + assert!(run.as_mut().now_or_never().is_none()); + let observation = jobs.snapshot(&id, "t").unwrap(); + drop(observation); + assert!(!sink.is_closed()); + drop(jobs); + run.await; + sink.closed().await; + assert!(sink.is_closed()); + }); +} diff --git a/crates/agent-usage/Cargo.toml b/crates/agent-usage/Cargo.toml deleted file mode 100644 index 40041774..00000000 --- a/crates/agent-usage/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "nocterm-agent-usage" -description = "Bounded native reads of provider account plan limits." -version = "0.0.0" -edition.workspace = true -license-file.workspace = true -publish = false - -[package.metadata.nocterm] -layer = "adapter" - -[dependencies] -nocterm-ai.workspace = true -futures.workspace = true -reqwest.workspace = true -serde_json.workspace = true - -[dev-dependencies] -tempfile.workspace = true - -[lints] -workspace = true diff --git a/crates/agent-usage/src/claude.rs b/crates/agent-usage/src/claude.rs deleted file mode 100644 index b8bf7a81..00000000 --- a/crates/agent-usage/src/claude.rs +++ /dev/null @@ -1,136 +0,0 @@ -//! Claude's account usage, read with Claude Code's own OAuth token. -use futures::future::BoxFuture; -use nocterm_ai::usage::{AccountUsageSource, RateLimit, UsageAgent, UsageReadError}; -use std::{ - io::Read as _, - path::{Path, PathBuf}, - time::{Duration, SystemTime, UNIX_EPOCH}, -}; - -const USAGE_URL: &str = "https://api.anthropic.com/api/oauth/usage"; -const TIMEOUT: Duration = Duration::from_secs(10); -const MAX_BODY_BYTES: u64 = 1024 * 1024; - -/// Reads Claude's plan limits under a configuration directory supplied by -/// composition, for whichever agent the runtime asks about. -pub struct ClaudeUsageReader { - config: Option, -} - -impl ClaudeUsageReader { - pub fn new(config: Option) -> Self { - Self { config } - } -} - -impl AccountUsageSource for ClaudeUsageReader { - fn read( - &self, - _: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - let config = self.config.clone(); - Box::pin(async move { - match config { - Some(config) => match token(&config.join(".credentials.json"))? { - Some(token) => fetch(&token), - None => Ok(None), - }, - None => Ok(None), - } - }) - } -} - -/// The unexpired access token in Claude Code's credentials, if it keeps one -/// there. -fn token(path: &Path) -> Result, UsageReadError> { - let text = match std::fs::read_to_string(path) { - Ok(text) => text, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), - Err(error) => return Err(UsageReadError::read(path.to_owned(), error)), - }; - let Ok(value) = serde_json::from_str::(&text) else { - return Ok(None); - }; - let oauth = &value["claudeAiOauth"]; - let now = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map_or(0, |now| now.as_millis()); - let expired = oauth["expiresAt"] - .as_u64() - .is_some_and(|expires| u128::from(expires) <= now); - Ok(oauth["accessToken"] - .as_str() - .filter(|token| !token.is_empty() && !expired) - .map(str::to_owned)) -} - -fn fetch(token: &str) -> Result>, UsageReadError> { - let client = reqwest::blocking::Client::builder() - .timeout(TIMEOUT) - .user_agent(concat!("nocterm/", env!("CARGO_PKG_VERSION"))) - .build() - .map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - let response = client - .get(USAGE_URL) - .bearer_auth(token) - .header("anthropic-beta", "oauth-2025-04-20") - .header("Content-Type", "application/json") - .send() - .map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - // A rejected or unsupported account reports nothing rather than failing. - if !response.status().is_success() { - return Ok(None); - } - let mut bytes = Vec::new(); - response - .take(MAX_BODY_BYTES) - .read_to_end(&mut bytes) - .map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - let body: serde_json::Value = - serde_json::from_slice(&bytes).map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - Ok(crate::parser::claude_limits(&body)) -} - -#[cfg(test)] -mod tests { - use super::{ClaudeUsageReader, token}; - use nocterm_ai::usage::{AccountUsageSource as _, UsageAgent}; - - fn agent() -> UsageAgent { - UsageAgent { - id: "claude".into(), - account: nocterm_ai::agents::AccountUsage::OAuthAccount, - } - } - - #[test] - fn missing_credentials_report_nothing() { - let dir = tempfile::tempdir().unwrap(); - let reader = ClaudeUsageReader::new(Some(dir.path().to_owned())); - assert_eq!( - futures::executor::block_on(reader.read(agent())).unwrap(), - None - ); - let unconfigured = ClaudeUsageReader::new(None); - assert_eq!( - futures::executor::block_on(unconfigured.read(agent())).unwrap(), - None - ); - } - - #[test] - fn only_an_unexpired_token_is_used() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join(".credentials.json"); - let write = |json: serde_json::Value| std::fs::write(&path, json.to_string()).unwrap(); - write( - serde_json::json!({"claudeAiOauth": {"accessToken": "fresh", "expiresAt": u64::MAX}}), - ); - assert_eq!(token(&path).unwrap().as_deref(), Some("fresh")); - write(serde_json::json!({"claudeAiOauth": {"accessToken": "stale", "expiresAt": 1}})); - assert_eq!(token(&path).unwrap(), None); - std::fs::write(&path, "not json").unwrap(); - assert_eq!(token(&path).unwrap(), None); - } -} diff --git a/crates/agent-usage/src/lib.rs b/crates/agent-usage/src/lib.rs deleted file mode 100644 index 3dd50f80..00000000 --- a/crates/agent-usage/src/lib.rs +++ /dev/null @@ -1,51 +0,0 @@ -//! Bounded native reads of provider account plan limits. -//! -//! Owns: Claude's account usage request and its response grammar, the read of -//! the OAuth token Claude Code keeps in its configuration directory, Codex's -//! session-log tail reader and grammar, and where both live on this machine -//! (`native_sources`). -//! Does not own: which agent reports limits which way (the agent's -//! `UsageProfile`), scheduling, merging or presenting limits (agent runtime -//! and feature), or token refresh: an expired token yields no report until -//! Claude Code renews it. -//! Ports provided: AI `AccountUsageSource` per `AccountUsage` kind. Ports used: -//! none. -//! Settings section: none. Globals: none. -//! Invariants: readers serve only the agents `UsageSources` routes to them by -//! profile; the token is sent only to Anthropic's API over TLS and never -//! logged; requests are bounded by a timeout, log reads by a tail size, and -//! both run when the runtime polls on its background executor. -mod claude; -mod parser; -mod session_log; - -pub use claude::ClaudeUsageReader; -pub use session_log::CodexUsageReader; - -use nocterm_ai::{agents::AccountUsage, usage::UsageSources}; -use std::{path::PathBuf, sync::Arc}; - -/// The account sources for this machine, with their locations resolved once -/// from `CLAUDE_CONFIG_DIR`, `CODEX_HOME` and `HOME`. -pub fn native_sources() -> UsageSources { - UsageSources::default() - .with( - AccountUsage::OAuthAccount, - Arc::new(ClaudeUsageReader::new(home_dir( - "CLAUDE_CONFIG_DIR", - ".claude", - ))), - ) - .with( - AccountUsage::SessionLog, - Arc::new(CodexUsageReader::new(home_dir("CODEX_HOME", ".codex"))), - ) -} - -/// `variable` when set, otherwise `default` under the home directory. -fn home_dir(variable: &str, default: &str) -> Option { - std::env::var_os(variable) - .filter(|dir| !dir.is_empty()) - .map(PathBuf::from) - .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(default))) -} diff --git a/crates/agent-usage/src/parser.rs b/crates/agent-usage/src/parser.rs deleted file mode 100644 index 4ac39031..00000000 --- a/crates/agent-usage/src/parser.rs +++ /dev/null @@ -1,112 +0,0 @@ -//! Claude's account usage grammar, separate from its native request. -use nocterm_ai::usage::{RateLimit, Window}; -use serde_json::Value; - -/// The plan limits in Claude's account usage response, if it reports any. -pub(crate) fn claude_limits(body: &Value) -> Option> { - let windows = [ - ("five_hour", Window::FiveHour), - ("seven_day", Window::Weekly), - ("seven_day_opus", Window::WeeklyModel("Opus".into())), - ("seven_day_sonnet", Window::WeeklyModel("Sonnet".into())), - ]; - let mut limits: Vec<_> = windows - .into_iter() - .filter_map(|(key, window)| limit(&body[key], window)) - .collect(); - let extra = &body["extra_usage"]; - if extra["is_enabled"].as_bool() == Some(true) { - limits.extend(limit(extra, Window::Extra)); - } - (!limits.is_empty()).then_some(limits) -} - -/// One window, whose utilization is a percentage. -fn limit(window: &Value, kind: Window) -> Option { - let used = (window["utilization"].as_f64()? / 100.).clamp(0., 1.); - Some(RateLimit { - window: kind, - used: Some(used), - resets_at: window["resets_at"].as_str().and_then(unix_seconds), - reached: used >= 1., - }) -} - -/// An RFC 3339 timestamp (`2026-10-20T21:49:59.785225+00:00`) in seconds -/// since the Unix epoch. -fn unix_seconds(text: &str) -> Option { - let (date, rest) = text.split_once('T')?; - let mut date = date.splitn(3, '-').map(str::parse::); - let (year, month, day) = (date.next()?.ok()?, date.next()?.ok()?, date.next()?.ok()?); - let zone = rest.find(['Z', 'z', '+', '-'])?; - let (time, offset) = rest.split_at(zone); - let mut time = time.split(':'); - let hour: i64 = time.next()?.parse().ok()?; - let minute: i64 = time.next()?.parse().ok()?; - let second: i64 = time.next()?.split('.').next()?.parse().ok()?; - let offset = match offset.as_bytes().first()? { - b'Z' | b'z' => 0, - sign => { - let (hours, minutes) = offset[1..].split_once(':')?; - let minutes = hours.parse::().ok()? * 60 + minutes.parse::().ok()?; - if *sign == b'-' { -minutes } else { minutes } - } - }; - let seconds = days_from_civil(year, month, day) * 86_400 + hour * 3_600 + minute * 60 + second - - offset * 60; - u64::try_from(seconds).ok() -} - -/// Days from 1970-01-01 to a proleptic Gregorian date. -fn days_from_civil(year: i64, month: i64, day: i64) -> i64 { - let year = if month <= 2 { year - 1 } else { year }; - let era = year.div_euclid(400); - let year_of_era = year - era * 400; - let day_of_year = (153 * (month + if month > 2 { -3 } else { 9 }) + 2) / 5 + day - 1; - let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year; - era * 146_097 + day_of_era - 719_468 -} - -#[cfg(test)] -mod tests { - use super::{claude_limits, unix_seconds}; - use nocterm_ai::usage::Window; - - #[test] - fn account_usage_reports_each_window_as_a_share() { - let body = serde_json::json!({ - "five_hour": {"utilization": 10.0, "resets_at": "2026-10-20T21:49:59.785225+00:00"}, - "seven_day": {"utilization": 100.0, "resets_at": "2026-10-23T15:59:59.785245+00:00"}, - "seven_day_opus": null, - "seven_day_sonnet": {"utilization": 3.0, "resets_at": null}, - "extra_usage": {"is_enabled": false, "utilization": null}, - }); - let limits = claude_limits(&body).unwrap(); - let windows: Vec<_> = limits.iter().map(|limit| limit.window.clone()).collect(); - assert_eq!( - windows, - [ - Window::FiveHour, - Window::Weekly, - Window::WeeklyModel("Sonnet".into()) - ] - ); - assert_eq!(limits[0].used, Some(0.1)); - assert_eq!(limits[0].resets_at, Some(1_792_532_999)); - assert!(!limits[0].reached); - assert!(limits[1].reached); - assert_eq!(limits[2].resets_at, None); - assert!(claude_limits(&serde_json::json!({"five_hour": null})).is_none()); - } - - #[test] - fn timestamps_honour_their_offset() { - assert_eq!(unix_seconds("1970-01-01T00:00:00Z"), Some(0)); - assert_eq!(unix_seconds("1970-01-01T01:00:00+01:00"), Some(0)); - assert_eq!( - unix_seconds("2000-03-01T00:00:00.5-00:30"), - Some(951_870_600) - ); - assert_eq!(unix_seconds("not a time"), None); - } -} diff --git a/crates/agent-usage/src/session_log.rs b/crates/agent-usage/src/session_log.rs deleted file mode 100644 index b0f79690..00000000 --- a/crates/agent-usage/src/session_log.rs +++ /dev/null @@ -1,91 +0,0 @@ -//! Codex's plan limits, read from the tail of its newest session log. -mod parser; -#[cfg(test)] -mod tests; - -use futures::future::BoxFuture; -use nocterm_ai::usage::{AccountUsageSource, RateLimit, UsageAgent, UsageReadError}; -use parser::codex_limits; -use std::{ - io::{Read as _, Seek as _, SeekFrom}, - path::{Path, PathBuf}, -}; - -const CODEX_TAIL_BYTES: u64 = 256 * 1024; - -/// Reads Codex's latest account report under a home supplied by composition, -/// for whichever agent the runtime asks about. -pub struct CodexUsageReader { - home: Option, -} -impl CodexUsageReader { - pub fn new(home: Option) -> Self { - Self { home } - } -} -impl AccountUsageSource for CodexUsageReader { - fn read( - &self, - _: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - let home = self.home.clone(); - Box::pin(async move { - match home { - Some(home) => read_codex_limits(&home), - None => Ok(None), - } - }) - } -} -fn read_codex_limits(home: &Path) -> Result>, UsageReadError> { - let mut dir = home.join("sessions"); - for _ in 0..3 { - let Some(next) = newest(&dir, |path| path.is_dir())? else { - return Ok(None); - }; - dir = next; - } - let Some(log) = newest(&dir, |path| { - path.extension() - .is_some_and(|extension| extension == "jsonl") - })? - else { - return Ok(None); - }; - let mut file = match std::fs::File::open(&log) { - Ok(file) => file, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), - Err(error) => return Err(read_error(&log, error)), - }; - let length = file - .metadata() - .map_err(|error| read_error(&log, error))? - .len(); - file.seek(SeekFrom::Start(length.saturating_sub(CODEX_TAIL_BYTES))) - .map_err(|source| UsageReadError::seek(log.clone(), source))?; - let mut tail = Vec::new(); - file.take(CODEX_TAIL_BYTES) - .read_to_end(&mut tail) - .map_err(|error| read_error(&log, error))?; - Ok(String::from_utf8_lossy(&tail) - .lines() - .rev() - .find_map(codex_limits)) -} -fn newest(dir: &Path, keep: impl Fn(&Path) -> bool) -> Result, UsageReadError> { - let entries = match std::fs::read_dir(dir) { - Ok(entries) => entries, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), - Err(source) => { - return Err(UsageReadError::enumerate(dir.to_owned(), source)); - } - }; - Ok(entries - .filter_map(Result::ok) - .map(|entry| entry.path()) - .filter(|path| keep(path)) - .max_by(|a, b| a.file_name().cmp(&b.file_name()))) -} -fn read_error(path: &Path, source: std::io::Error) -> UsageReadError { - UsageReadError::read(path.to_owned(), source) -} diff --git a/crates/agent-usage/src/session_log/parser.rs b/crates/agent-usage/src/session_log/parser.rs deleted file mode 100644 index 51048158..00000000 --- a/crates/agent-usage/src/session_log/parser.rs +++ /dev/null @@ -1,28 +0,0 @@ -//! Codex account-log grammar, separate from its native reader. -use nocterm_ai::usage::{RateLimit, Window}; - -/// The plan limits in one line of a Codex session log, if it reports them. -pub(super) fn codex_limits(line: &str) -> Option> { - if !line.contains("\"rate_limits\"") { - return None; - } - let value: serde_json::Value = serde_json::from_str(line).ok()?; - let limits = value.get("payload")?.get("rate_limits")?; - let reached = limits - .get("rate_limit_reached_type") - .is_some_and(|reached| !reached.is_null()); - let parsed: Vec<_> = ["primary", "secondary"] - .into_iter() - .filter_map(|key| { - let window = limits.get(key)?; - let used = window.get("used_percent")?.as_f64()? / 100.; - Some(RateLimit { - window: Window::from_minutes(window.get("window_minutes")?.as_u64()?), - used: Some(used.clamp(0., 1.)), - resets_at: window.get("resets_at").and_then(serde_json::Value::as_u64), - reached: reached && used >= 1., - }) - }) - .collect(); - (!parsed.is_empty()).then_some(parsed) -} diff --git a/crates/agent-usage/src/session_log/tests.rs b/crates/agent-usage/src/session_log/tests.rs deleted file mode 100644 index bbdb8c7a..00000000 --- a/crates/agent-usage/src/session_log/tests.rs +++ /dev/null @@ -1,155 +0,0 @@ -use super::*; -use futures::executor::block_on; -use nocterm_ai::usage::Window; - -const CODEX_LINE: &str = r#"{"timestamp":"2026-10-03T17:42:09Z","type":"event_msg","payload":{"type":"token_count","info":null,"rate_limits":{"limit_id":"codex","primary":{"used_percent":38.0,"window_minutes":10080,"resets_at":1791588105},"secondary":{"used_percent":7.5,"window_minutes":300,"resets_at":1791500000},"rate_limit_reached_type":null}}}"#; - -#[test] -fn codex_limits_come_from_its_session_log() { - let limits = codex_limits(CODEX_LINE).unwrap(); - assert_eq!(limits.len(), 2); - assert_eq!(limits[0].window, Window::Weekly); - assert_eq!(limits[0].used, Some(0.38)); - assert_eq!(limits[0].resets_at, Some(1_791_588_105)); - assert_eq!(limits[1].window, Window::FiveHour); - assert!(codex_limits(r#"{"payload":{"type":"agent_message"}}"#).is_none()); - assert!(codex_limits(r#"{"payload":{"rate_limits":{"primary":null}}}"#).is_none()); -} - -#[test] -fn the_newest_codex_log_is_read() { - let home = tempfile::tempdir().unwrap(); - let day = |path: &str| { - let dir = home.path().join("sessions").join(path); - std::fs::create_dir_all(&dir).unwrap(); - dir - }; - std::fs::write( - day("2026/09/30").join("rollout-a.jsonl"), - CODEX_LINE.replace("38.0", "99.0"), - ) - .unwrap(); - let newest = day("2026/10/03"); - std::fs::write(newest.join("rollout-a.jsonl"), "{}\n").unwrap(); - std::fs::write( - newest.join("rollout-b.jsonl"), - format!("{CODEX_LINE}\n{{\"payload\":{{\"type\":\"agent_message\"}}}}\n"), - ) - .unwrap(); - - let limits = block_on(CodexUsageReader::new(Some(home.path().to_owned())).read(codex())) - .unwrap() - .unwrap(); - - assert_eq!(limits[0].used, Some(0.38)); - assert!( - block_on(CodexUsageReader::new(Some(home.path().join("missing"))).read(codex())) - .unwrap() - .is_none() - ); -} - -fn codex() -> UsageAgent { - UsageAgent { - id: "custom".into(), - account: nocterm_ai::agents::AccountUsage::SessionLog, - } -} - -#[test] -fn an_unset_home_reports_nothing_and_an_unreadable_one_fails() { - let file = tempfile::NamedTempFile::new().unwrap(); - let reader = CodexUsageReader::new(Some(file.path().to_owned())); - assert!( - block_on(CodexUsageReader::new(None).read(codex())) - .unwrap() - .is_none() - ); - let error = block_on(reader.read(codex())).unwrap_err(); - assert!(matches!(error, UsageReadError::Enumerate { .. })); - use std::error::Error as _; - let native = error - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert_eq!(native.kind(), std::io::ErrorKind::NotADirectory); - let clone = error.clone(); - let shared = clone - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - assert_eq!(error.to_string(), clone.to_string()); -} - -#[test] -fn malformed_and_partial_utf8_tail_lines_preserve_the_last_usable_report() { - let home = tempfile::tempdir().unwrap(); - let day = home.path().join("sessions/2026/10/03"); - std::fs::create_dir_all(&day).unwrap(); - let mut bytes = vec![b'x'; CODEX_TAIL_BYTES as usize + 32]; - bytes.extend_from_slice(b"\xff\xfe\n"); - bytes.extend_from_slice(CODEX_LINE.as_bytes()); - bytes.extend_from_slice(b"\n{\"rate_limits\": broken}\n{}\n"); - std::fs::write(day.join("rollout-a.jsonl"), bytes).unwrap(); - let limits = block_on(CodexUsageReader::new(Some(home.path().to_owned())).read(codex())) - .unwrap() - .unwrap(); - assert_eq!(limits[0].used, Some(0.38)); -} - -#[test] -fn records_outside_the_tail_are_not_read_and_corrupt_reports_return_no_limits() { - let home = tempfile::tempdir().unwrap(); - let day = home.path().join("sessions/2026/10/03"); - std::fs::create_dir_all(&day).unwrap(); - let log = day.join("rollout-a.jsonl"); - let bytes = format!("{CODEX_LINE}\n{}", "x".repeat(CODEX_TAIL_BYTES as usize)); - std::fs::write(&log, bytes).unwrap(); - let reader = CodexUsageReader::new(Some(home.path().to_owned())); - assert!(block_on(reader.read(codex())).unwrap().is_none()); - std::fs::write(&log, "{\"rate_limits\": invalid}\n{}\n").unwrap(); - assert!(block_on(reader.read(codex())).unwrap().is_none()); -} - -#[test] -fn native_read_and_seek_refusals_preserve_the_original_error_mapping() { - use std::{ - error::Error, - io::{Seek, SeekFrom}, - }; - let directory = tempfile::tempdir().unwrap(); - let source = std::fs::read(directory.path()).unwrap_err(); - let read_kind = source.kind(); - let read = read_error(directory.path(), source); - let file = tempfile::NamedTempFile::new().unwrap(); - let mut opened = std::fs::File::open(file.path()).unwrap(); - let source = opened.seek(SeekFrom::Current(i64::MIN)).unwrap_err(); - let seek_kind = source.kind(); - let seek = UsageReadError::seek(file.path(), source); - for (error, kind) in [(read, read_kind), (seek, seek_kind)] { - let clone = error.clone(); - let native = error - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert_eq!(native.kind(), kind); - let shared = clone - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - } -} diff --git a/crates/agent/Cargo.toml b/crates/agent/Cargo.toml index ed121fc1..16629604 100644 --- a/crates/agent/Cargo.toml +++ b/crates/agent/Cargo.toml @@ -10,6 +10,8 @@ publish = false layer = "feature" [dependencies] +nocterm-hubs-model.workspace = true +nocterm-hub-agents.workspace = true nocterm-ai.workspace = true nocterm-plugin-model.workspace = true nocterm-project-model.workspace = true @@ -18,6 +20,7 @@ nocterm-agent-runtime.workspace = true nocterm-agent-directory.workspace = true nocterm-store.workspace = true nocterm-session.workspace = true +nocterm-transfers.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true # Fenced code in chat replies: the languages agents write most. @@ -40,7 +43,6 @@ gpui-kit = { workspace = true, features = [ ] } futures.workspace = true async-channel.workspace = true -serde.workspace = true serde_json.workspace = true base64.workspace = true chrono = { version = "0.4", default-features = false, features = ["clock"] } @@ -48,6 +50,7 @@ thiserror.workspace = true tracing.workspace = true [dev-dependencies] +nocterm-hubs-model = { workspace = true, features = ["test-support"] } nocterm-workspace-ports.workspace = true nocterm-workspace = { workspace = true, features = ["test-support"] } nocterm-project-model = { workspace = true, features = ["test-support"] } @@ -57,6 +60,7 @@ nocterm-session = { workspace = true, features = ["test-support"] } gpui-kit = { workspace = true, features = ["test-support"] } nocterm-ai = { workspace = true, features = ["test-support"] } nocterm-agent-runtime = { workspace = true, features = ["test-support"] } +nocterm-transfers = { workspace = true, features = ["test-support"] } tempfile.workspace = true [lints] diff --git a/crates/agent/src/chat_host.rs b/crates/agent/src/chat_host.rs index f6ae5400..24312aba 100644 --- a/crates/agent/src/chat_host.rs +++ b/crates/agent/src/chat_host.rs @@ -12,6 +12,8 @@ pub(crate) struct ChatHost { } #[derive(Debug, thiserror::Error)] pub(crate) enum ChatHostError { + #[error("The account source is not available for a new chat action")] + Account(#[from] nocterm_ai::storage::AgentStorageError), #[error("The originating root window no longer matches this chat host.")] WindowMismatch, } @@ -24,10 +26,14 @@ impl ChatHost { if source.window().workspace_id() != workspace.entity_id() { return Err(ChatHostError::WindowMismatch); } - let seat = Rc::new(crate::workspace_seat::WorkspaceSeat::new( - workspace.clone(), - source, - )); + let runtime = installed + .capture_runtime() + .unwrap_or_else(|_| installed.runtime().unavailable_account()) + .for_scope(source.context()); + let seat = Rc::new( + crate::workspace_seat::WorkspaceSeat::new(workspace.clone(), source) + .with_runtime(runtime.clone()), + ); let sign_in = installed.auth().map(|opener| { Rc::new(crate::workspace_sign_in::WorkspaceSignIn::new( opener, @@ -36,7 +42,7 @@ impl ChatHost { }); Ok(Self { deps: ChatDeps::new( - installed.runtime().for_scope(source.context()), + runtime, installed.agents().clone(), installed.settings().clone(), installed.metadata(), @@ -51,8 +57,18 @@ impl ChatHost { pub(crate) fn source(&self) -> ProjectSurfaceReceipt { self.source } - /// Capture a committed UI origin synchronously. This supplies descriptive - /// membership; terminals remain attached to that original ordinary surface. + /// The hub agents this window offers, if the application has hubs. + pub(crate) fn hub_agents(&self) -> Option<&crate::HubAgentsAccess> { + self.installed.hub_agents() + } + + /// The agents the window's new-chat menu offers: the local ones, and in a hub window also + /// that hub's agents. + pub(crate) fn window_agents(&self, cx: &gpui_kit::App) -> nocterm_ai::AvailableAgents { + let hub = self.hub_agents().and_then(|access| access.hub()); + self.agents().available(cx).offered_in(hub) + } + pub(crate) fn deps_for_source( &self, source: ProjectSurfaceReceipt, @@ -60,15 +76,19 @@ impl ChatHost { if source.window().workspace_id() != self.workspace.entity_id() { return Err(ChatHostError::WindowMismatch); } + let runtime = self + .installed + .capture_runtime()? + .for_scope(source.context()); Ok(ChatDeps::new( - self.installed.runtime().for_scope(source.context()), + runtime.clone(), self.installed.agents().clone(), self.installed.settings().clone(), self.installed.metadata(), - Rc::new(crate::workspace_seat::WorkspaceSeat::new( - self.workspace.clone(), - source, - )), + Rc::new( + crate::workspace_seat::WorkspaceSeat::new(self.workspace.clone(), source) + .with_runtime(runtime), + ), self.deps.sign_in().cloned(), )) } diff --git a/crates/agent/src/hub_access.rs b/crates/agent/src/hub_access.rs new file mode 100644 index 00000000..31d20be4 --- /dev/null +++ b/crates/agent/src/hub_access.rs @@ -0,0 +1,42 @@ +//! The hub agents a window offers: the shared service and the hub of the window. Only a hub +//! window offers hub agents; the window of this computer shows none of a hub's data. +use gpui_kit::Entity; +use nocterm_hub_agents::HubAgents; +use nocterm_hubs_model::HubId; + +/// What the panel of a window needs of the hubs: the agents service, to follow the hub's chats +/// and agents, and the window's hub. Cheap to clone; the entity is the application's own. +#[derive(Clone)] +pub struct HubAgentsAccess { + agents: Entity, + hub: Option, +} + +impl HubAgentsAccess { + /// The installation-wide access; it offers nothing until bound to the window of a hub. + pub fn new(agents: Entity) -> Self { + Self { agents, hub: None } + } + + /// The access of a window of `hub`: it offers that hub's agents. + pub fn for_hub_window(&self, hub: &HubId) -> Self { + Self { + agents: self.agents.clone(), + hub: Some(hub.clone()), + } + } + + /// The shared service, for the other surfaces that manage the same agents. + pub fn service(&self) -> &Entity { + &self.agents + } + + pub(crate) fn agents(&self) -> &Entity { + &self.agents + } + + /// The hub of the window, if it is a hub window. + pub(crate) fn hub(&self) -> Option<&HubId> { + self.hub.as_ref() + } +} diff --git a/crates/agent/src/image_deps.rs b/crates/agent/src/image_deps.rs index 6a886299..ddb44c3f 100644 --- a/crates/agent/src/image_deps.rs +++ b/crates/agent/src/image_deps.rs @@ -5,12 +5,27 @@ use std::sync::Arc; pub struct ImageDeps { inspector: Arc, thumbnailer: Arc, + available: async_channel::Receiver<()>, + returned: async_channel::Sender<()>, +} +/// Retained by the actual background work, including after a panel disappears. +pub(crate) struct ImageWorkPermit(async_channel::Sender<()>); +impl Drop for ImageWorkPermit { + fn drop(&mut self) { + let _ = self.0.try_send(()); + } } impl ImageDeps { pub fn new(inspector: Arc, thumbnailer: Arc) -> Self { + let (returned, available) = async_channel::bounded(4); + for _ in 0..4 { + returned.try_send(()).expect("new image work slots"); + } Self { inspector, thumbnailer, + available, + returned, } } pub(crate) fn inspector(&self) -> Arc { @@ -19,4 +34,52 @@ impl ImageDeps { pub(crate) fn thumbnailer(&self) -> Arc { self.thumbnailer.clone() } + pub(crate) fn admit_work(&self) -> Option { + self.available.try_recv().ok()?; + Some(ImageWorkPermit(self.returned.clone())) + } + pub(crate) fn work(&self) -> futures::future::BoxFuture<'static, ImageWorkPermit> { + let (available, returned) = (self.available.clone(), self.returned.clone()); + Box::pin(async move { + available.recv().await.expect("retained image work slots"); + ImageWorkPermit(returned) + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn installations_share_a_finite_physical_image_work_budget() { + let codec = Arc::new(nocterm_ai::testing::ScriptedImageCodec::default()); + let deps = ImageDeps::new(codec.clone(), codec); + let other = deps.clone(); + let mut permits = (0..4) + .map(|_| deps.admit_work().unwrap()) + .collect::>(); + assert!(other.admit_work().is_none()); + permits.pop(); + assert!(other.admit_work().is_some()); + } + #[test] + fn queued_decoding_wakes_when_physical_work_finishes_and_cancelled_waiters_release() { + let codec = Arc::new(nocterm_ai::testing::ScriptedImageCodec::default()); + let deps = ImageDeps::new(codec.clone(), codec); + let mut permits = (0..4) + .map(|_| deps.admit_work().unwrap()) + .collect::>(); + futures::executor::block_on(async { + let mut cancelled = deps.work(); + assert!(futures::poll!(&mut cancelled).is_pending()); + drop(cancelled); + let mut waiting = deps.work(); + assert!(futures::poll!(&mut waiting).is_pending()); + permits.pop(); + let released = waiting.await; + assert!(deps.admit_work().is_none()); + drop(released); + assert!(deps.admit_work().is_some()); + }); + } } diff --git a/crates/agent/src/installation.rs b/crates/agent/src/installation.rs index 18672bf5..e44d45d6 100644 --- a/crates/agent/src/installation.rs +++ b/crates/agent/src/installation.rs @@ -5,12 +5,16 @@ use nocterm_agent_directory::AgentsHandle; use nocterm_store::{SettingsHandle, ShutdownCoordinator, StoreError}; use nocterm_ui::{LayoutHandle, UiReady}; use std::{rc::Rc, sync::Arc}; +mod source; +use crate::hub_access::HubAgentsAccess; +use source::SourceBinding; #[derive(Clone)] pub struct Installed { inner: Rc, + source: SourceBinding, + hubs: Option, } struct Installation { - runtime: runtime::RuntimeHandle, agents: AgentsHandle, settings: SettingsHandle, metadata: Arc, @@ -21,7 +25,50 @@ struct Installation { } impl Installed { pub fn runtime(&self) -> &runtime::RuntimeHandle { - &self.inner.runtime + self.source.runtime() + } + pub fn for_environment( + &self, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, + ) -> Result { + Ok(Self { + inner: self.inner.clone(), + hubs: self.hubs.clone(), + source: SourceBinding::fixed(self.runtime().for_environment(access)?), + }) + } + /// A hub window without an authenticated capture provider has no Local environment. + pub fn unavailable_account(&self) -> Self { + Self { + inner: self.inner.clone(), + hubs: self.hubs.clone(), + source: SourceBinding::fixed(self.runtime().unavailable_account()), + } + } + pub fn with_account_source( + &self, + factory: Arc, + ) -> Self { + Self { + inner: self.inner.clone(), + hubs: self.hubs.clone(), + source: SourceBinding::account(self.runtime(), factory), + } + } + /// A window of one hub offers that hub's agents beside the local ones, and lists its chats. + pub fn for_hub_window(&self, hub: &nocterm_hubs_model::HubId) -> Self { + Self { + hubs: self.hubs.as_ref().map(|access| access.for_hub_window(hub)), + ..self.clone() + } + } + pub(crate) fn hub_agents(&self) -> Option<&HubAgentsAccess> { + self.hubs.as_ref() + } + pub(crate) fn capture_runtime( + &self, + ) -> Result { + self.source.capture() } /// The agents available to start, for other surfaces such as settings. pub fn agents(&self) -> &AgentsHandle { @@ -49,18 +96,24 @@ pub(crate) fn install( layout: LayoutHandle, ready: &UiReady, shutdown: &ShutdownCoordinator, + hubs: Option, cx: &mut App, ) -> (Installed, Result<(), StoreError>) { let settings = ready.settings().clone(); - let agents = AgentsHandle::new(services.catalog, settings.clone(), cx); + let mut agents = AgentsHandle::new(services.catalog, settings.clone(), cx); + if let Some(hubs) = &hubs { + let service = hubs.agents().clone(); + agents = agents.with_hub_agents(Rc::new(move |cx| service.read(cx).launches())); + } let runtime = runtime::RuntimeHandle::new( runtime::RuntimeServices { settings: settings.clone(), agents: agents.clone(), connector: services.connector, bridge: services.bridge, + command_jobs: services.command_jobs, + transfers: services.transfers, storage: services.storage, - usage_source: services.usage_source, workdir_resolver: services.workdir_resolver, workdir: services.workdir, terminal_auth: services.terminal_auth.is_some(), @@ -74,8 +127,9 @@ pub(crate) fn install( let registered = shutdown.register_capture(move |cx| captured.capture_shutdown(cx)); ( Installed { + hubs, + source: SourceBinding::fixed(runtime.clone()), inner: Rc::new(Installation { - runtime, agents, settings, metadata: services.metadata, diff --git a/crates/agent/src/installation/source.rs b/crates/agent/src/installation/source.rs new file mode 100644 index 00000000..ac9ced8d --- /dev/null +++ b/crates/agent/src/installation/source.rs @@ -0,0 +1,41 @@ +//! New UI actions capture an account source; existing chats retain their old handle. +use crate::runtime::RuntimeHandle; +use nocterm_ai::{local_environment::LocalEnvironmentAccess, storage::AgentStorageError}; +use nocterm_hubs_model::source_identity::LocalAccountSourceFactory; +use std::sync::Arc; + +#[derive(Clone)] +pub(super) struct SourceBinding { + runtime: RuntimeHandle, + capture: Option>, +} +impl SourceBinding { + pub(super) fn fixed(runtime: RuntimeHandle) -> Self { + Self { + runtime, + capture: None, + } + } + pub(super) fn account( + runtime: &RuntimeHandle, + capture: Arc, + ) -> Self { + // Explicitly unavailable, never a ThisComputer substitute before unlock. + Self { + runtime: runtime.unavailable_account(), + capture: Some(capture), + } + } + pub(super) fn runtime(&self) -> &RuntimeHandle { + &self.runtime + } + pub(super) fn capture(&self) -> Result { + if let Some(capture) = &self.capture { + let access = LocalEnvironmentAccess::capture(capture.capture()?)?; + self.runtime.for_environment(access) + } else { + self.runtime.check_current()?; + Ok(self.runtime.clone()) + } + } +} diff --git a/crates/agent/src/lib.rs b/crates/agent/src/lib.rs index fcb9bcc9..c20afd05 100644 --- a/crates/agent/src/lib.rs +++ b/crates/agent/src/lib.rs @@ -1,8 +1,14 @@ //! The agent chat panel: views over the chats of `nocterm-agent-chat`. //! Installed retains the actual runtime, available agents, image, layout, settings, metadata -//! and auth collaborators. +//! and auth collaborators. New hub-window actions capture fresh account sources; +//! existing chats keep their original source and are withdrawn when it becomes stale. //! MessageList, ComposerView, HistoryColumn, PanelLayout and MenuView own controls, -//! disposable presentation transitions and their subscription/task lifetimes. +//! disposable presentation transitions and their subscription/task lifetimes. Transcript rows +//! borrow the open AgentThread's transcript into the EntryRenderer. A hub window's chats are +//! ordinary chats of its history (ADR 0148): its new-chat menu offers the hub's agents beside +//! the local ones, and a change of the hub's chats reads the history again. The history also +//! lists, through weak handles, the chats other windows hold (ADR 0152); opening one moves it +//! here while it rests, otherwise its window shows it. //! SignInCards owns secret controls and prompt tokens. PanelNotices owns errors, approval //! identity and cancellable redraw work; PanelBindings retains installation subscriptions. //! Stateless renderers take immutable projections; finite presentation operations return @@ -11,11 +17,13 @@ //! Owns: panel views and presentation state, the panel's ChatRoster, WorkspaceSeat (the //! ChatWorkspace over an original surface and exact ChatKey) and WorkspaceSignIn (the SignInTerminal over the //! host's TerminalAuthOpener). +//! Image previews retain immutable source identity with a 64 MiB source/PNG/RGBA budget and at most 32 cached entries. +//! Preparation and preview decoding share four physical work permits across windows, retained through actual background work. //! Does not own: chats, sessions and tools (nocterm-agent-chat), ACP transport, connection //! admission, filesystem storage or image decoding. //! Ports used: AgentConnector, ToolBridge, WorkdirResolver, ConnectionDirectory, ChatConnectionOpen, //! ImageInspector and ImageThumbnailer through ImageDeps, and ChatMetadataSource. -//! Ports provided: the required `agent` system plugin (`plugin::PLUGIN`) with its side panel, +//! Ports provided: the required `agent` system plugin (`plugin::PLUGIN`) with its side panel //! and the TerminalAuthOpener composition callback. //! Settings section: reads ai; its schema belongs to nocterm-ai. //! Globals: none. @@ -27,6 +35,8 @@ mod agent_icon; mod chat_host; +mod hub_access; +pub use hub_access::HubAgentsAccess; mod image_deps; mod installation; pub use installation::Installed; @@ -64,9 +74,9 @@ pub struct AgentServices { pub metadata: Arc, pub connector: Arc, pub bridge: Arc, + pub command_jobs: Arc, + pub transfers: Arc, pub storage: runtime::AgentStorage, - /// The host's account usage capability, without native paths in the view. - pub usage_source: Arc, pub workdir_resolver: Arc, pub workdir: PathBuf, pub terminal_auth: Option, @@ -86,10 +96,11 @@ pub fn init( layout: nocterm_ui::LayoutHandle, ready: &nocterm_ui::UiReady, shutdown: &nocterm_store::ShutdownCoordinator, + hubs: Option, cx: &mut App, ) -> (Installed, Result<(), nocterm_store::StoreError>) { panel::commands::init(cx); - installation::install(services, images, layout, ready, shutdown, cx) + installation::install(services, images, layout, ready, shutdown, hubs, cx) } gpui_kit::actions!( @@ -101,7 +112,8 @@ gpui_kit::actions!( ShowHistory, StopGeneration, ToggleModelPicker, - AttachImage + /// Attach files to the next message; images among them are sent as images. + AttachFiles ] ); diff --git a/crates/agent/src/panel.rs b/crates/agent/src/panel.rs index 41fe023d..5a5811b4 100644 --- a/crates/agent/src/panel.rs +++ b/crates/agent/src/panel.rs @@ -1,29 +1,31 @@ +use crate::chat_host::ChatHostError; use crate::thread::AgentThread; +use account_notice::AccountNotice; use gpui_kit::{ AnyElement, App, Context, Entity, EntityId, EventEmitter, FocusHandle, Focusable, SharedString, - Subscription, TestSupportExt as _, Window, - component::{ActiveTheme as _, v_flex}, - div, - prelude::*, + Subscription, TestSupportExt as _, Window, component::v_flex, div, prelude::*, }; use nocterm_ai::model::controls::ConfigCategory; use nocterm_plugin_model::Side; use nocterm_ui::IconName; use nocterm_workspace::{ExpandablePanel, Panel, PanelRequest, WorkspaceEvent}; +mod account_notice; mod approvals; mod attach; +mod attach_files; mod attachments; mod bindings; mod chat; +mod chat_servers; pub(crate) mod commands; mod composer; +mod composer_frame; mod composer_renderer; mod composer_view; mod entries; mod history; mod history_column; -mod image_picker; mod image_pipeline; mod image_preparation; mod images; @@ -33,8 +35,11 @@ mod menu; mod menu_view; mod message_actions; mod message_list; +mod message_markdown; mod navigation; mod notices; +mod other_windows; +mod parts; mod preview_operation; mod queue; mod queue_image_preparation; @@ -91,6 +96,10 @@ impl AgentPanel { let layout = layout::PanelLayout::new(layout, cx); let messages = message_list::MessageList::new(images, cx); let subscriptions = Self::bind_panel(&host, &settings, window, cx); + // Whatever the panel redraws for, its cached regions redraw too; thread changes + // and typing redraw only their regions instead of notifying the panel. + cx.observe_self(|panel, cx| panel.layout.parts().invalidate(cx)) + .detach(); let panel = cx.weak_entity(); window.defer(cx, move |window, cx| { let _ = panel.update(cx, |panel, cx| panel.on_runtime_changed(window, cx)); @@ -135,22 +144,75 @@ impl AgentPanel { let _ = panel.update(cx, |panel, cx| panel.on_runtime_changed(window, cx)); }); }); - [Some(settings), workspace, Some(runtime)] - .into_iter() - .flatten() - .collect() + let panel = cx.weak_entity(); + let holders = host.runtime().observe_holders(cx, move |event, cx| { + let event = *event; + let _ = cx.update_window(originating_window, |_, window, cx| { + let _ = panel.update(cx, |panel, cx| panel.on_holder_event(event, window, cx)); + }); + }); + let hubs = host.hub_agents().map(|access| { + let hub = access.hub().cloned(); + cx.subscribe(access.agents(), move |panel, _, event, cx| { + panel.on_hub_agents_event(hub.as_ref(), event, cx); + }) + }); + [ + Some(settings), + workspace, + Some(runtime), + Some(holders), + hubs, + ] + .into_iter() + .flatten() + .collect() } fn on_runtime_changed(&mut self, window: &mut Window, cx: &mut Context) { + let retired = self.chats.retire_stale_sources(cx); + let removed = !retired.removed().is_empty(); + if let Some(error) = retired.into_error() { + self.notices.set_error(Some(error.to_string())); + } + let deps = self.host.deps_for_source(self.source); + let account_notice = match &deps { + Ok(_) => None, + Err(ChatHostError::Account(error)) => AccountNotice::from_capture(error), + Err(ChatHostError::WindowMismatch) => None, + }; + self.notices.set_account(account_notice); + let switched = deps + .ok() + .is_some_and(|deps| self.chats.switch_context(deps, cx)); + if switched || removed { + self.composer.clear(); + self.history.switch_context(window, cx); + self.sign_ins.clear(); + self.reset_chat_view(cx); + } self.chats.release_quiet_hidden(cx); if !self.settings.setting::(cx).enabled { return; } - if let Err(error) = self.chats.host().runtime().ensure_history(cx) { - self.notices.set_error(Some(error.to_string())); + if account_notice.is_some() { + cx.notify(); + return; } + self.load_history(cx); self.adopt_saved_chats(window, cx); + self.refresh_other_windows(cx); cx.notify(); } + /// Asks for the history of the captured context; a failure of the account becomes the + /// account notice, any other the error line. + fn load_history(&mut self, cx: &mut Context) { + if let Err(error) = self.chats.host().runtime().ensure_history(cx) { + match AccountNotice::from_catalog(&error) { + Some(notice) => self.notices.set_account(Some(notice)), + None => self.notices.set_error(Some(error.to_string())), + } + } + } fn select_context( &mut self, source: nocterm_workspace::ProjectSurfaceReceipt, @@ -163,11 +225,20 @@ impl AgentPanel { let deps = match self.host.deps_for_source(source) { Ok(deps) => deps, Err(error) => { - self.notices.set_error(Some(error.to_string())); + match &error { + ChatHostError::Account(cause) => match AccountNotice::from_capture(cause) { + Some(notice) => self.notices.set_account(Some(notice)), + None => self.notices.set_error(Some(error.to_string())), + }, + ChatHostError::WindowMismatch => { + self.notices.set_error(Some(error.to_string())); + } + } cx.notify(); return; } }; + self.notices.set_account(None); self.leave_composer(cx); self.chats.switch_context(deps, cx); self.source = source; @@ -185,9 +256,7 @@ impl AgentPanel { } else { self.composer.set_text("", window, cx); } - if let Err(error) = self.chats.host().runtime().ensure_history(cx) { - self.notices.set_error(Some(error.to_string())); - } + self.load_history(cx); self.adopt_saved_chats(window, cx); self.sync_approval_attention(window, cx); cx.notify(); @@ -254,7 +323,7 @@ impl AgentPanel { } fn history_is_visible(&self, cx: &App) -> bool { self.layout.history() - && !self.chats.is_empty() + && (!self.chats.is_empty() || !self.history.others().listed(cx).is_empty()) && self .host .workspace() @@ -294,6 +363,23 @@ impl AgentPanel { pub(crate) fn current(&self) -> Option> { self.chats.current() } + /// The hub's agents or chats changed: a hub window lists the hub's chats again and offers + /// the agents it lists now. + fn on_hub_agents_event( + &mut self, + window_hub: Option<&nocterm_hubs_model::HubId>, + event: &nocterm_hub_agents::HubAgentsEvent, + cx: &mut Context, + ) { + use nocterm_hub_agents::HubAgentsEvent; + match event { + HubAgentsEvent::ChatsChanged(hub) if Some(hub) == window_hub => { + self.host.runtime().reload_history(cx); + } + HubAgentsEvent::Changed(hub) if Some(hub) == window_hub => cx.notify(), + _ => {} + } + } #[cfg(test)] pub(crate) fn chat_documents(&self) -> impl ExactSizeIterator> { self.chats.iter() @@ -310,7 +396,7 @@ impl AgentPanel { /// The agent `NewThreadWithLastAgent` starts: the last one used, else /// the default, else the first enabled one. fn last_agent(&self, cx: &App) -> Option { - let agents = self.host.agents().available(cx); + let agents = self.host.window_agents(cx); let known = |id: &&String| agents.get(id).is_some(); self.host .runtime() @@ -328,7 +414,8 @@ impl AgentPanel { .or_else(|| agents.iter().next().map(|agent| agent.id().to_owned())) } pub(super) fn set_history(&mut self, open: bool, cx: &mut Context) { - if let Some(width) = self.layout.set_history(open, self.current().is_some(), cx) { + let chat = self.current().is_some(); + if let Some(width) = self.layout.set_history(open, chat, cx) { cx.emit(PanelRequest::Widen(width)); } cx.notify(); @@ -339,18 +426,52 @@ impl AgentPanel { window: &mut Window, cx: &mut Context, ) -> AnyElement { - let history = history::HistoryProjection::render( - &self.chats, - &self.history, - self.host.agents(), - self.host.timestamp(), - cx, - ); + let history = self.layout.parts().history().into_any_element(); self.layout.render_split(chat, history, window, cx) } + /// Draws one region of the panel for its view in [`parts`]. + fn render_part( + &mut self, + kind: parts::PartKind, + window: &mut Window, + cx: &mut Context, + ) -> AnyElement { + match kind { + parts::PartKind::Transcript => self.render_transcript_part(cx), + parts::PartKind::History => { + let stamp = history::HistoryProjection::stamp(&self.chats, &self.history, cx); + self.layout.parts_mut().drew_history(stamp); + let hub_window = self + .host + .hub_agents() + .is_some_and(|access| access.hub().is_some()); + let history = history::HistoryProjection::render( + &self.chats, + &self.history, + (self.host.agents(), hub_window), + self.host.timestamp(), + cx, + ); + div() + .size_full() + .flex() + .flex_col() + .child(history) + .into_any_element() + } + parts::PartKind::Footer => self.render_footer(window, cx), + } + } fn toggle_history(&mut self, cx: &mut Context) { self.set_history(!self.layout.history(), cx); } + /// Lists the chats of `side` in the history of a hub window. + fn show_history_side(&mut self, side: history_column::HistorySide, cx: &mut Context) { + if self.history.show_side(side) { + self.layout.parts().invalidate(cx); + cx.notify(); + } + } fn new_thread_with_last(&mut self, window: &mut Window, cx: &mut Context) { if let Some(id) = self.last_agent(cx) { self.new_thread(id, window, cx); @@ -377,19 +498,22 @@ impl AgentPanel { impl AgentPanel { pub(super) fn render_menu(&mut self, window: &Window, cx: &mut Context) -> AnyElement { let kind = self.menus.current().cloned(); - let attachment = matches!(kind, Some(MenuKind::Context)).then(|| { - attach::AttachmentMenuRenderer::render_attach_menu( - self.current(), + let attachment = matches!(kind, Some(MenuKind::Context)).then(|| match self.current() { + Some(thread) if chat_servers::takes_servers(thread.read(cx)) => { + chat_servers::ChatServersRenderer::render_menu(&thread, cx) + } + thread => attach::AttachmentMenuRenderer::render_attach_menu( + thread, self.host.workspace().clone(), cx, - ) + ), }); let frame = menu::MenuFrame::new( kind, self.current(), self.menus.query_input(), self.menus.query(cx).to_lowercase(), - self.host.agents().available(cx), + self.host.window_agents(cx), self.last_agent(cx), self.focus.clone(), self.host.runtime().favorites(cx).clone(), @@ -457,20 +581,25 @@ impl Render for AgentPanel { cx, ); let mut body = shell::ShellRenderer::body(&self.focus, cx).child(header); - body = match current { - None => body.child(history::HistoryProjection::render( - &self.chats, - &self.history, - self.host.agents(), - self.host.timestamp(), - cx, - )), - Some(thread) if self.layout.history() => { - let chat = self.render_chat(thread, window, cx); + body = if current.is_none() { + body.child(self.layout.parts().history()) + } else { + let chat = v_flex() + .size_full() + .min_w_0() + .min_h_0() + .child(self.layout.parts().transcript()) + .child(self.layout.parts().footer()) + .into_any_element(); + if self.layout.history() { body.child(self.render_split(chat, window, cx)) + } else { + body.child(chat) } - Some(thread) => body.child(self.render_chat(thread, window, cx)), }; + if let Some(notice) = self.notices.account() { + body = body.child(notice.strip(cx)); + } if let Some(error) = self.notices.error() { body = body.child( shell::ShellRenderer::error(error.to_owned(), cx) diff --git a/crates/agent/src/panel/account_notice.rs b/crates/agent/src/panel/account_notice.rs new file mode 100644 index 00000000..1c22fbaf --- /dev/null +++ b/crates/agent/src/panel/account_notice.rs @@ -0,0 +1,99 @@ +//! What the state of the hub account behind this window's chats lets the person do, and the +//! strip that says why the chats are unavailable. +use gpui_kit::{ + App, ClickEvent, IntoElement, TestSupportExt as _, Window, + component::{ActiveTheme as _, Sizable as _, button::Button, h_flex}, + div, + prelude::*, +}; +use nocterm_ai::storage::AgentStorageError; +use nocterm_hubs_model::HubError; +use nocterm_workspace::{UnlockVault, request_sign_in}; + +use nocterm_agent_runtime::CatalogLoadError; + +/// Why the account of this window cannot serve chats right now. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum AccountNotice { + /// The keys of the account are closed. + Locked, + /// The account is signed out on this device. + SignedOut, + /// The account the window captured is gone or cannot be identified. + Unavailable, +} + +impl AccountNotice { + /// The notice for a failure to capture the account, if the failure is about the account. + pub(super) fn from_capture(error: &AgentStorageError) -> Option { + match error { + AgentStorageError::AccountCapture(HubError::KeysLocked) => Some(Self::Locked), + AgentStorageError::AccountCapture(HubError::NotSignedIn | HubError::Revoked) => { + Some(Self::SignedOut) + } + AgentStorageError::AccountCapture(_) | AgentStorageError::AccountSource(_) => { + Some(Self::Unavailable) + } + _ => None, + } + } + + /// The notice for a failed history load, if the failure is about the account. + pub(super) fn from_catalog(error: &CatalogLoadError) -> Option { + match error { + CatalogLoadError::AccountSource(_) => Some(Self::Unavailable), + CatalogLoadError::Storage(error) => Self::from_capture(error), + _ => None, + } + } + + fn message(self) -> &'static str { + match self { + Self::Locked => "Hub account locked — unlock it to see its chats", + Self::SignedOut => "Signed out of the hub — sign in to see its chats", + Self::Unavailable => "Hub account unavailable — unlock it or sign in again", + } + } + + fn remedy(self) -> &'static str { + match self { + Self::SignedOut => "Sign in", + Self::Locked | Self::Unavailable => "Unlock", + } + } + + fn resolve(self, window: &mut Window, cx: &mut App) { + match self { + Self::Locked | Self::Unavailable => { + window.dispatch_action(Box::new(UnlockVault), cx); + } + Self::SignedOut => { + request_sign_in(window, cx); + } + } + } + + /// The strip shown above the chats. + pub(super) fn strip(self, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + h_flex() + .id("agent-account-notice") + .test_support() + .gap_2() + .mx_2() + .my_1() + .px_2() + .py_1() + .rounded(theme.radius) + .bg(theme.muted) + .text_xs() + .text_color(theme.muted_foreground) + .child(div().flex_1().min_w_0().child(self.message())) + .child( + Button::new("agent-account-action") + .xsmall() + .label(self.remedy()) + .on_click(move |_: &ClickEvent, window, cx| self.resolve(window, cx)), + ) + } +} diff --git a/crates/agent/src/panel/approvals.rs b/crates/agent/src/panel/approvals.rs index bda0992d..eaab029d 100644 --- a/crates/agent/src/panel/approvals.rs +++ b/crates/agent/src/panel/approvals.rs @@ -13,12 +13,20 @@ use gpui_kit::{ prelude::*, px, }; -use nocterm_ai::TerminalCall; +use nocterm_ai::{TerminalCall, TransferFiles}; use nocterm_ui::IconName; use nocterm_workspace::UnlockVault; use super::{AgentPanel, widgets::menu_variant}; -use crate::thread::{AgentThread, PermissionCard, ToolApprovalCard}; +use crate::thread::{AgentThread, PermissionCard}; + +/// What a person answers a terminal tool call that waits for approval. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum ToolApprovalChoice { + Deny, + AllowOnce, + AllowForSession, +} fn sign_in_text(wait: &crate::thread::SignInWait) -> (String, String) { let heading = if wait.vault() { @@ -54,6 +62,45 @@ fn card(cx: &Context) -> gpui_kit::Div { pub(super) struct ApprovalCardsRenderer; impl ApprovalCardsRenderer { + pub(super) fn list( + cards: Vec, + window: &gpui_kit::Window, + cx: &Context, + ) -> Option { + let pending = cards.len(); + if pending == 0 { + return None; + } + Some( + v_flex() + .id("agent-approvals") + .test_support() + .w_full() + .min_w_0() + .flex_shrink_0() + .px_3() + .gap_1() + .child( + div() + .text_xs() + .text_color(cx.theme().warning) + .child(format!("Waiting for you · {pending}")), + ) + .child( + v_flex() + .id("agent-approval-list") + .test_support() + .w_full() + .min_w_0() + .min_h_0() + .max_h(window.viewport_size().height * 0.35) + .overflow_y_scroll() + .gap_2() + .children(cards), + ) + .into_any_element(), + ) + } /// A background session asks for a secret. The user types it here, or /// unlocks the vault when a saved secret answers it; the session never /// becomes a tab. @@ -192,60 +239,72 @@ impl ApprovalCardsRenderer { .child(actions) .into_any_element() } + /// The card of tool call `token` of `thread`, while it waits for approval. pub(super) fn tool_card( thread: &Entity, - approval: ToolApprovalCard, - target: String, - reusable: bool, + token: crate::thread::ApprovalToken, index: usize, cx: &mut Context, - ) -> AnyElement { - let token = approval.token(); - let call = approval.call().clone(); - let owner = thread.downgrade(); + ) -> Option { + let call = thread.read(cx).tool_approval_call(token)?.clone(); + let (target, reusable) = thread.update(cx, |thread, cx| { + ( + thread.describe_target(&call, cx), + !thread.unsafe_live_input(&call, cx), + ) + }); let (heading, exact, grant) = Self::tool_text(&call, &target); let exact = nocterm_ai::redact::redact(&exact); - card(cx) - .child(div().min_w_0().truncate().text_sm().child(heading)) - .child( - h_flex().w_full().min_w_0().flex_wrap().gap_1().children( - [ - (false, false, "Deny"), - (true, false, "Allow once"), - (true, true, grant), - ] - .into_iter() - .filter(|(_, grant, _)| !grant || reusable) - .map(|(allow, grant, label)| { - let owner = owner.clone(); - Button::new((SharedString::from(format!("tool-{label}")), index)) - .custom(menu_variant(cx)) - .small() - .max_w_full() - .label(label) - .on_click(cx.listener(move |_, _, _, cx| { - if let Some(thread) = owner.upgrade() { - thread.update(cx, |thread, cx| { - thread.approve_tool(token, allow, grant, cx) + let weak = thread.downgrade(); + Some( + card(cx) + .child(div().min_w_0().truncate().text_sm().child(heading)) + .child( + h_flex().w_full().min_w_0().flex_wrap().gap_1().children( + [ + (ToolApprovalChoice::Deny, false, "Deny"), + (ToolApprovalChoice::AllowOnce, false, "Allow once"), + (ToolApprovalChoice::AllowForSession, true, grant), + ] + .into_iter() + .filter(|(_, grant, _)| !grant || reusable) + .map(|(choice, _, label)| { + let thread = weak.clone(); + Button::new((SharedString::from(format!("tool-{label}")), index)) + .custom(menu_variant(cx)) + .small() + .max_w_full() + .label(label) + .on_click(cx.listener(move |_, _, _, cx| { + let _ = thread.update(cx, |thread, cx| { + if thread.tool_approval_call(token).is_some() { + thread.approve_tool( + token, + choice != ToolApprovalChoice::Deny, + choice == ToolApprovalChoice::AllowForSession + && reusable, + cx, + ); + } }); - } - })) - }), - ), - ) - .child( - div() - .id(("approval-command", index)) - .test_support() - .w_full() - .min_w_0() - .max_h(px(64.)) - .overflow_x_scroll() - .overflow_y_scroll() - .text_xs() - .child(exact), - ) - .into_any_element() + })) + }), + ), + ) + .child( + div() + .id(("approval-command", index)) + .test_support() + .w_full() + .min_w_0() + .max_h(px(64.)) + .overflow_x_scroll() + .overflow_y_scroll() + .text_xs() + .child(exact), + ) + .into_any_element(), + ) } fn tool_text(call: &TerminalCall, target: &str) -> (&'static str, String, &'static str) { match call { @@ -300,6 +359,109 @@ impl ApprovalCardsRenderer { "Allow for this server", ), TerminalCall::ListTerminals => ("List terminals", String::new(), "Allow"), + TerminalCall::TransferFiles(request) => transfer_text(request, target), + TerminalCall::ReadTransfer(request) => ( + "Read transfer", + request.transfer_id.clone(), + "Allow reading", + ), + TerminalCall::CancelTransfer(request) => ( + "Cancel transfer", + request.transfer_id.clone(), + "Allow cancelling", + ), + TerminalCall::ListFiles(_) + | TerminalCall::ReadFile(_) + | TerminalCall::WriteFile(_) + | TerminalCall::EditFile(_) + | TerminalCall::MakeDirectory(_) + | TerminalCall::DeletePath(_) + | TerminalCall::MovePath(_) => file_text(call, target), + } + } +} + +/// The most of a file's new content or an edit's text a card shows. +const SHOWN: usize = 4000; + +fn shown(text: &str) -> String { + match text.char_indices().nth(SHOWN) { + Some((end, _)) => format!("{}\n…", &text[..end]), + None => text.to_owned(), + } +} + +/// A file call on the terminal `target`: the path, and for a change what it writes. +fn file_text(call: &TerminalCall, target: &str) -> (&'static str, String, &'static str) { + const READ: &str = "Allow reading on this terminal"; + const CHANGE: &str = "Allow file changes on this terminal"; + match call { + TerminalCall::ListFiles(request) => { + ("List files", format!("{target}: {}", request.path), READ) + } + TerminalCall::ReadFile(request) => { + ("Read file", format!("{target}: {}", request.path), READ) } + TerminalCall::WriteFile(request) => ( + if request.overwrite { + "Replace file" + } else { + "Create file" + }, + format!( + "{target}: {}\n{} bytes\n\n{}", + request.path, + request.content.len(), + shown(&request.content) + ), + CHANGE, + ), + TerminalCall::EditFile(request) => ( + "Edit file", + format!( + "{target}: {}{}\n\nReplace:\n{}\n\nWith:\n{}", + request.path, + if request.replace_all { + " (every occurrence)" + } else { + "" + }, + shown(&request.old_text), + shown(&request.new_text) + ), + CHANGE, + ), + TerminalCall::MakeDirectory(request) => ( + "Create folder", + format!("{target}: {}", request.path), + CHANGE, + ), + TerminalCall::DeletePath(request) => { + ("Delete", format!("{target}: {}", request.path), CHANGE) + } + TerminalCall::MovePath(request) => ( + "Move", + format!("{target}: {} → {}", request.from, request.to), + CHANGE, + ), + _ => ("File", target.to_owned(), CHANGE), } } + +/// `target` names the source and destination terminals of the transfer. +fn transfer_text(request: &TransferFiles, target: &str) -> (&'static str, String, &'static str) { + let sources: String = request + .sources + .iter() + .map(|source| format!("\n {source}")) + .collect(); + let existing = if request.overwrite { "replace" } else { "skip" }; + ( + "Transfer files", + format!( + "{target}\nSources:{sources}\nInto: {}\nExisting files: {existing}", + request.destination + ), + "Allow transfers between these terminals", + ) +} diff --git a/crates/agent/src/panel/attach_files.rs b/crates/agent/src/panel/attach_files.rs new file mode 100644 index 00000000..b5aee4f0 --- /dev/null +++ b/crates/agent/src/panel/attach_files.rs @@ -0,0 +1,37 @@ +//! The paperclip: files of any kind for the next message. Images among them are attached as +//! images the agent sees; every other file as a link to where it is on this computer. +use nocterm_ai::prompt_files::{PromptFile, PromptFileError, is_image}; +use std::path::PathBuf; + +/// A pick delivered only to the chat and composer revision it was started for. +pub(super) struct AttachPick { + chat: Option, + revision: u64, +} + +impl AttachPick { + pub(super) fn new(chat: Option, revision: u64) -> Self { + Self { chat, revision } + } + pub(super) fn current(&self, chat: Option, revision: u64) -> bool { + self.chat == chat && self.revision == revision + } +} + +/// `paths` split into the images (when the agent takes images) and the other files. +pub(super) fn split(paths: Vec, images_taken: bool) -> (Vec, Vec) { + paths + .into_iter() + .partition(|path| images_taken && is_image(path)) +} + +/// Links to `files` with their sizes. Reads file metadata: run it off the UI thread. +pub(super) fn link(files: &[PathBuf]) -> Result, PromptFileError> { + files + .iter() + .map(|path| { + let size = std::fs::metadata(path).map_or(0, |meta| meta.len()); + PromptFile::link(path, size) + }) + .collect() +} diff --git a/crates/agent/src/panel/chat.rs b/crates/agent/src/panel/chat.rs index 0c86fb8d..d0863e5f 100644 --- a/crates/agent/src/panel/chat.rs +++ b/crates/agent/src/panel/chat.rs @@ -9,6 +9,32 @@ use gpui_kit::AnyElement; use nocterm_ai::usage::{Breakdown, RateLimit}; impl AgentPanel { + /// The open chat's footer: approvals, status, queue, commands and the composer. + pub(super) fn render_footer( + &mut self, + window: &mut Window, + cx: &mut Context, + ) -> AnyElement { + let Some(thread) = self.current() else { + return div().into_any_element(); + }; + let frame = super::composer_frame::ComposerFrame::read(&thread, cx); + let approvals = self.render_approvals(&thread, window, cx); + let status = self.chat_status(&thread, cx); + let labels = attachments::AttachmentLabels::new(thread.read(cx).workspace_seat(), cx); + let queue = self.render_queue(&thread, &labels, cx); + let commands = self.render_commands(window, cx); + let composer = self.render_composer(&frame, &thread, &labels, cx); + v_flex() + .w_full() + .min_w_0() + .children(approvals) + .children(status) + .children(queue) + .children(commands) + .child(composer) + .into_any_element() + } pub(super) fn sync_approval_attention(&mut self, window: &mut Window, cx: &mut Context) { let attention = self .chats @@ -21,8 +47,12 @@ impl AgentPanel { || self.host.workspace().upgrade().is_none_or(|workspace| { !workspace.read(cx).panel_is_open(&crate::plugin::CHATS.id) })); - // The workspace pulls the attention from `Panel::attention` when the panel notifies. - cx.notify(); + // The workspace pulls the attention from `Panel::attention` when the panel notifies, + // so notify only when it changed: this runs on every chunk a chat streams. + let asks = Panel::attention(self, cx).is_some(); + if self.notices.attention_changed(asks) { + cx.notify(); + } let requests = needs_notice.then(|| { self.chats .iter() @@ -77,78 +107,50 @@ impl AgentPanel { cx.notify(); } } - /// The cards of every pending request of `thread`; none when nothing waits. + /// The cards of every pending request of the chat: its sign-in, permission and tool + /// approval requests; none when nothing waits. pub(super) fn render_approvals( &mut self, thread: &Entity, window: &mut Window, cx: &mut Context, ) -> Option { - let permissions = thread.read(cx).permission_cards(); - let tools = thread.read(cx).tool_cards(); - let pending = permissions.len() + tools.len() + thread.read(cx).sign_in_waits().len(); - let asking: Vec = self - .chats - .iter() - .flat_map(|thread| { - thread - .read(cx) - .sign_in_waits() - .iter() - .map(|wait| wait.item()) - }) - .collect(); - self.sign_ins.reconcile(&asking); - if pending == 0 { - return None; - } - let mut list = v_flex() - .id("agent-approval-list") - .test_support() - .w_full() - .min_w_0() - .min_h_0() - .max_h(window.viewport_size().height * 0.35) - .overflow_y_scroll() - .gap_2(); - for index in 0..thread.read(cx).sign_in_waits().len() { - let wait = thread.read(cx).sign_in_waits()[index].clone(); - let (input, submission) = self.sign_ins.input_for(thread, &wait, window, cx); - list = list.child(approvals::ApprovalCardsRenderer::sign_in_card( - wait, input, submission, index, cx, - )); - } - for (index, permission) in permissions.into_iter().enumerate() { - list = list.child(approvals::ApprovalCardsRenderer::permission_card( - thread, permission, index, cx, - )); + let mut list = Vec::new(); + { + let asking: Vec = self + .chats + .iter() + .flat_map(|thread| { + thread + .read(cx) + .sign_in_waits() + .iter() + .map(|wait| wait.item()) + }) + .collect(); + self.sign_ins.reconcile(&asking); + for index in 0..thread.read(cx).sign_in_waits().len() { + let wait = thread.read(cx).sign_in_waits()[index].clone(); + let (input, submission) = self.sign_ins.input_for(thread, &wait, window, cx); + list.push(approvals::ApprovalCardsRenderer::sign_in_card( + wait, input, submission, index, cx, + )); + } + for (index, permission) in thread.read(cx).permission_cards().into_iter().enumerate() { + list.push(approvals::ApprovalCardsRenderer::permission_card( + thread, permission, index, cx, + )); + } } - for (index, tool) in tools.into_iter().enumerate() { - let call = tool.call(); - let target = thread.update(cx, |thread, cx| thread.describe_target(call, cx)); - let reusable = thread.update(cx, |thread, cx| !thread.unsafe_live_input(call, cx)); - list = list.child(approvals::ApprovalCardsRenderer::tool_card( - thread, tool, target, reusable, index, cx, - )); + let tokens = thread.read(cx).tool_approval_tokens(); + for (index, token) in tokens.into_iter().enumerate() { + if let Some(card) = + approvals::ApprovalCardsRenderer::tool_card(thread, token, index, cx) + { + list.push(card); + } } - Some( - v_flex() - .id("agent-approvals") - .test_support() - .w_full() - .min_w_0() - .flex_shrink_0() - .px_3() - .gap_1() - .child( - div() - .text_xs() - .text_color(cx.theme().warning) - .child(format!("Waiting for you · {pending}")), - ) - .child(list) - .into_any_element(), - ) + approvals::ApprovalCardsRenderer::list(list, window, cx) } pub(super) fn submit_sign_in( &mut self, @@ -169,16 +171,17 @@ impl AgentPanel { binding: message_list::ActiveListBinding, cx: &mut Context, ) { - let Some(thread) = self.current() else { + let Some(thread) = self.messages.thread() else { return; }; let reduced = cx.reduce_motion(); let changed = self.messages - .advance_reveal(&binding, thread.read(cx).history_entries(), reduced); + .advance_reveal(&binding, &thread.read(cx).transcript().entries, reduced); if let Some(changed) = changed { self.messages.apply_reveal(changed, cx); - cx.notify(); + // The reveal animates the transcript's rows only. + self.layout.parts().notify(parts::PartKind::Transcript, cx); } } pub(super) fn on_preview_completed( @@ -186,7 +189,7 @@ impl AgentPanel { binding: message_list::ActiveListBinding, ticket: image_pipeline::PreviewTicket, row: Option, - image: Option>, + image: Option, cx: &mut Context, ) { if self @@ -229,93 +232,114 @@ impl AgentPanel { ) }) } - pub(super) fn render_chat( - &mut self, - thread: Entity, - window: &mut Window, - cx: &mut Context, - ) -> AnyElement { - let mut body = v_flex().size_full().min_w_0().min_h_0(); - let plan = thread.read(cx).transcript().plan.clone(); - let usage = thread.read(cx).transcript().usage.clone(); - let dirty = thread + /// The transcript of the open chat, for its cached view; the rows changed since its last + /// drawing redraw. + pub(super) fn render_transcript_part(&mut self, cx: &mut Context) -> AnyElement { + let Some(thread) = self.current() else { + return div().into_any_element(); + }; + let dirty: Vec = thread .update(cx, |thread, _| thread.take_changed_rows()) .into_iter() - .collect::>(); - self.messages.bind_chat(thread.entity_id(), cx); - let rows = self.messages.observe_rows( - thread.entity_id(), - thread.read(cx).history_entries(), - &dirty, - thread.read(cx).session_generating(), - cx.reduce_motion(), - ); - self.messages.apply_rows(rows, cx); + .collect(); + let stamp = parts::transcript_stamp(thread.read(cx)); + self.layout.parts_mut().drew_transcript(Some(stamp)); + self.observe_rows(&thread, &dirty, cx); let scroller = self.messages.scroller(cx); let navigation = navigation::NavigationRenderer::render(&self.messages, cx); let overlay = self .menus .is_open(&MenuKind::Usage) .then(|| self.render_usage(cx)); - body = body.child(TranscriptView::area(scroller, navigation, overlay, cx)); - if let Some(plan) = &plan { - body = body.child(TranscriptView::plan(plan, cx)); - } - if let Some(approvals) = self.render_approvals(&thread, window, cx) { - body = body.child(approvals); - } - let labels = attachments::AttachmentLabels::new(thread.read(cx).workspace_seat(), cx); - let composer = self.render_composer(&thread, usage.as_ref(), &labels, cx); - let status = self.chat_status(&thread, cx); - body = body - .children(status) - .children(self.render_queue(&thread, &labels, window, cx)) - .children(self.render_commands(window, cx)) - .child(composer); - body.into_any_element() + let plan = thread + .read(cx) + .transcript() + .plan + .as_ref() + .map(|plan| TranscriptView::plan(plan, cx)); + v_flex() + .size_full() + .min_h_0() + .min_w_0() + .child(TranscriptView::area(scroller, navigation, overlay, cx)) + .children(plan) + .into_any_element() } pub(super) fn render_entry( &mut self, index: usize, cx: &mut Context, ) -> gpui_kit::AnyElement { - let Some(thread) = self.current() else { + let Some(thread) = self.messages.thread() else { return div().into_any_element(); }; + let identity = thread.entity_id(); + let markdown = thread + .read(cx) + .transcript() + .entries + .get(index) + .and_then(|entry| { + super::message_markdown::MessageMarkdown::text(entry, index, &self.messages) + }); + if let Some((text, source_len)) = markdown { + self.messages.prepare_markdown(index, text, source_len, cx); + } let state = thread.read(cx); - let Some(entry) = state.history_entries().get(index) else { + let transcript = state.transcript(); + let Some(entry) = transcript.entries.get(index) else { return div().into_any_element(); }; + let generating = state.session_generating(); let frame = RowPresentation::new( - thread.entity_id(), + identity, self.messages.binding(), index, self.messages.expanded(index), entry_is_live( - state.history_entries(), + &transcript.entries, index, - state.session_generating() && state.session_accepts_updates(), + generating && state.session_accepts_updates(), ), - state.session_generating(), - state.transcript().time(index), + generating, + transcript.time(index), self.settings .setting::(cx) .approval .redact_secrets, - ); + ) + .with_fork_available(true); let (row, pending) = EntryRenderer::render(frame, entry, &self.messages, cx); - for (key, data) in pending { + for source in pending { preview_operation::PreviewOperation::cached( &mut self.messages, - key, - || data, - true, + source, Some(index), cx, ); } row } + /// The visible list follows the open chat. Each observer supplies its own dirty rows, so + /// one window never consumes another's change tracking. + pub(super) fn observe_rows( + &mut self, + thread: &Entity, + dirty: &[usize], + cx: &mut Context, + ) { + self.messages.bind_thread(thread, cx); + let reduced = cx.reduce_motion(); + let state = thread.read(cx); + let rows = self.messages.observe_rows( + thread.entity_id(), + &state.transcript().entries, + dirty, + state.session_generating(), + reduced, + ); + self.messages.apply_rows(rows, cx); + } pub(super) fn jump_to_message(&mut self, first: bool, cx: &mut Context) { self.messages.navigate(first, cx); cx.notify(); @@ -341,7 +365,6 @@ impl AgentPanel { if open { self.close_usage(window, cx); } else { - self.refresh_usage(cx); self.menus.open(MenuKind::Usage); // Read-only information must not interrupt composing. if !self.composer.focus(cx).is_focused(window) { @@ -350,12 +373,6 @@ impl AgentPanel { cx.notify(); } } - pub(super) fn refresh_usage(&mut self, cx: &mut Context) { - if let Some(thread) = self.current() { - let agent = thread.read(cx).document().agent_id().to_owned(); - self.host.runtime().refresh_limits(&agent, cx); - } - } pub(super) fn usage_report(&self, cx: &App) -> Option { let thread = self.current()?; let thread = thread.read(cx); @@ -406,34 +423,3 @@ impl AgentPanel { } } } - -impl AgentPanel { - pub(super) fn render_queue( - &mut self, - thread: &Entity, - labels: &attachments::AttachmentLabels, - window: &mut Window, - cx: &mut Context, - ) -> Option { - if thread.read(cx).composer().queue().is_empty() { - return None; - } - let height = queue::QueueRenderer::visible_height(&self.composer, thread, window, cx); - let prompts = if self.composer.queue_open() || height > gpui_kit::px(0.) { - thread.read(cx).composer().queue().to_vec() - } else { - Vec::new() - }; - let previews = - preview_operation::PreviewOperation::queued_previews(&mut self.messages, &prompts, cx); - queue::QueueRenderer::render_queue( - &self.composer, - prompts, - previews, - thread, - labels, - height, - cx, - ) - } -} diff --git a/crates/agent/src/panel/chat_servers.rs b/crates/agent/src/panel/chat_servers.rs new file mode 100644 index 00000000..b7cbcebf --- /dev/null +++ b/crates/agent/src/panel/chat_servers.rs @@ -0,0 +1,173 @@ +//! The servers an agent that runs elsewhere may sign in to, as its chat offers them. +//! +//! Such an agent gets none of this device's terminals: the attach menu lists the servers the +//! chat offers, each checked while the chat has it attached, and the composer shows the +//! attached ones as removable chips. Every server is a flag of the chat's configuration +//! (`ConfigCategory::Server`), so a choice is stored where the agent runs and every device +//! shows it. + +use gpui_kit::{ + AnyElement, Context, Entity, SharedString, + component::{ + ActiveTheme as _, Disableable as _, Selectable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_ai::model::controls::{ConfigCategory, ConfigKind, ConfigOption, ConfigSetting}; +use nocterm_ui::IconName; + +use super::{ + AgentPanel, + widgets::{menu_row, menu_variant}, +}; +use crate::thread::AgentThread; + +/// A server the chat offers, and whether the chat has it attached. +struct Offered { + option: ConfigOption, + attached: bool, +} + +fn offered(thread: &AgentThread) -> Vec { + thread + .config_options() + .into_iter() + .filter(is_server) + .map(|option| Offered { + attached: matches!(option.kind, ConfigKind::Boolean(true)), + option, + }) + .collect() +} + +/// Whether `option` attaches a server rather than configuring the agent. +pub(super) fn is_server(option: &ConfigOption) -> bool { + option.category == Some(ConfigCategory::Server) +} + +/// Whether the chat's agent signs in to the chat's servers instead of using this device's +/// terminals. +pub(super) fn takes_servers(thread: &AgentThread) -> bool { + thread + .agent_info() + .is_some_and(|info| info.capabilities.own_tools) + || thread.config_options().iter().any(is_server) +} + +/// Attaches or detaches the server of `option` through the chat's configuration. +fn choose( + thread: &Entity, + option: &ConfigOption, + attach: bool, + cx: &mut Context, +) { + let id = option.id.clone(); + thread.update(cx, |thread, cx| { + thread.set_config(id, ConfigSetting::Boolean(attach), cx); + }); +} + +pub(super) struct ChatServersRenderer; + +impl ChatServersRenderer { + /// The attach menu of a chat whose agent takes servers. + pub(super) fn render_menu( + thread: &Entity, + cx: &mut Context, + ) -> AnyElement { + let servers = offered(thread.read(cx)); + let busy = thread.read(cx).session_generating(); + let mut menu = v_flex().w_full().gap(px(2.)); + if servers.is_empty() { + menu = menu.child( + div() + .px_2() + .py_1() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child( + "No servers yet. Give agents access to a saved server, or connect one in \ + the hub's web panel.", + ), + ); + } + for (index, server) in servers.into_iter().enumerate() { + menu = menu.child(Self::row(thread, index, server, busy, cx)); + } + menu.into_any_element() + } + + fn row( + thread: &Entity, + index: usize, + server: Offered, + busy: bool, + cx: &mut Context, + ) -> AnyElement { + let Offered { option, attached } = server; + let address = option.description.clone().unwrap_or_default(); + let theme = cx.theme().clone(); + let thread = thread.clone(); + let chosen = option.clone(); + menu_row(("attach-server", index), option.name.clone(), cx) + .icon(if attached { + IconName::Check + } else { + IconName::Server + }) + .child( + div() + .flex_shrink_0() + .max_w(px(180.)) + .truncate() + .text_xs() + .text_color(theme.muted_foreground) + .child(address.clone()), + ) + .tooltip(format!("{} · {address}", option.name)) + .selected(attached) + .disabled(busy) + .on_click(cx.listener(move |_, _, _, cx| { + choose(&thread, &chosen, !attached, cx); + })) + .into_any_element() + } + + /// Removable chips for the servers the chat has attached. + pub(super) fn chips( + thread: &Entity, + cx: &mut Context, + ) -> Vec { + let busy = thread.read(cx).session_generating(); + offered(thread.read(cx)) + .into_iter() + .filter(|server| server.attached) + .enumerate() + .map(|(index, server)| { + let thread = thread.clone(); + let option = server.option; + let name = option.name.clone(); + Button::new(SharedString::from(format!("server-chip-{index}"))) + .custom(menu_variant(cx)) + .small() + .icon(IconName::Server) + .max_w_full() + .label(name.clone()) + .tooltip(format!( + "Detach {name} · {}", + option.description.clone().unwrap_or_default() + )) + .disabled(busy) + .child(gpui_kit::component::Icon::new(IconName::X).xsmall()) + .on_click(cx.listener(move |_, _, _, cx| { + choose(&thread, &option, false, cx); + })) + .into_any_element() + }) + .collect() + } +} diff --git a/crates/agent/src/panel/composer.rs b/crates/agent/src/panel/composer.rs index 2e31f01e..3aee82bb 100644 --- a/crates/agent/src/panel/composer.rs +++ b/crates/agent/src/panel/composer.rs @@ -5,10 +5,9 @@ use super::{ menu::{ConfigurationSelection, FavoriteSelection, ModeSelection}, }; use crate::thread::Attachment; -use gpui_kit::{AnyElement, PathPromptOptions}; +use gpui_kit::AnyElement; use nocterm_ai::images::PromptImage; use nocterm_ai::model::ids::AuthMethodId; -use nocterm_ai::model::usage::ContextUsage; use std::path::PathBuf; impl AgentPanel { @@ -25,7 +24,8 @@ impl AgentPanel { } self.sync_composer_draft(cx); self.sync_command_token(window, cx); - cx.notify(); + // Typing redraws the footer with the composer, not the transcript or history. + self.layout.parts().notify(parts::PartKind::Footer, cx); } pub(super) fn submit_composer(&mut self, window: &mut Window, cx: &mut Context) { self.send(window, cx); @@ -167,10 +167,11 @@ impl AgentPanel { ) { self.composer.select_command_token(range, window, cx); } + /// The composer of the open chat: its field, attachments, controls and notices. pub(super) fn render_composer( &mut self, + frame: &composer_frame::ComposerFrame, thread: &Entity, - usage: Option<&ContextUsage>, labels: &attachments::AttachmentLabels, cx: &mut Context, ) -> AnyElement { @@ -184,16 +185,19 @@ impl AgentPanel { cx, ); let input = composer_renderer::ComposerRenderer::render_input(&self.composer, cx); + let stop = composer_frame::RenderedStop::capture(thread, cx); let footer = composer_renderer::ComposerRenderer::render_footer( + frame, &self.composer, &self.menus, thread, - usage, + stop, cx, ); + let terminal = self.host.sign_in().is_some(); let notices = composer_renderer::ComposerRenderer::render_notices( &self.composer, - self.host.sign_in().is_some(), + terminal, thread, cx, ); @@ -207,6 +211,25 @@ impl AgentPanel { cx, ) } + /// The Local chat's queued prompts above the composer; none while the queue is empty. + pub(super) fn render_queue( + &mut self, + thread: &Entity, + labels: &attachments::AttachmentLabels, + cx: &mut Context, + ) -> Option { + if thread.read(cx).composer().queue().is_empty() { + return None; + } + let prompts = if self.composer.queue_open() { + thread.read(cx).composer().queue().to_vec() + } else { + Vec::new() + }; + let previews = + preview_operation::PreviewOperation::queued_previews(&mut self.messages, &prompts, cx); + queue::QueueRenderer::render_queue(&self.composer, prompts, previews, thread, labels, cx) + } pub(super) fn prepare_images( &mut self, work: impl std::future::Future, ImageAttachError>> @@ -217,7 +240,7 @@ impl AgentPanel { let Some(thread) = self.current() else { return; }; - if self.preparing_queue_edit() + if self.preparing_images() || !thread .read(cx) .agent_info() @@ -225,11 +248,18 @@ impl AgentPanel { { return; } + let Some(permit) = self.messages.admit_image_work() else { + self.notices + .set_error(Some(ImageAttachError::Busy.to_string())); + cx.notify(); + return; + }; let binding = self.composer.prepare_images(thread.entity_id()); cx.notify(); let ticket = thread.read(cx).session_ticket(); let owner = thread.downgrade(); - image_preparation::ImagePreparation::new(binding, owner, ticket, Box::pin(work)).launch(cx); + image_preparation::ImagePreparation::new(binding, owner, ticket, Box::pin(work), permit) + .launch(cx); } pub(super) fn on_composer_images_prepared( &mut self, @@ -269,38 +299,47 @@ impl AgentPanel { cx.notify(); } pub(super) fn add_image_bytes(&mut self, bytes: Vec, cx: &mut Context) { + let Some(thread) = self.current() else { + return; + }; + let budget = nocterm_ai::images::ImageCollectionBudget::remaining( + thread.read(cx).composer().images(), + ); let inspector = self.messages.inspector(); self.prepare_images( - async move { Ok(vec![PromptImage::validate(bytes, inspector.as_ref())?]) }, + async move { + let mut budget = budget?; + budget.admit(bytes.len())?; + Ok(vec![PromptImage::validate(bytes, inspector.as_ref())?]) + }, cx, ); } pub(super) fn add_images(&mut self, paths: Vec, cx: &mut Context) { + let Some(thread) = self.current() else { + return; + }; + let budget = nocterm_ai::images::ImageCollectionBudget::remaining( + thread.read(cx).composer().images(), + ); let inspector = self.messages.inspector(); self.prepare_images( - async move { images::read_images(paths, inspector.as_ref()) }, + async move { images::read_images(paths, inspector.as_ref(), budget?) }, cx, ); } - pub(super) fn on_image_paths_picked( - &mut self, - operation: image_picker::ImagePickerOperation, - paths: Vec, - cx: &mut Context, - ) { - if operation.current( - self.current().map(|thread| thread.entity_id()), - self.composer.revision(), - ) { - self.add_images(paths, cx); - } + /// Whether the composer takes images and files: a chat is open. + pub(super) fn composer_attaches(&self, _cx: &App) -> bool { + self.current().is_some() } - pub(super) fn pick_images(&mut self, cx: &mut Context) { - let operation = image_picker::ImagePickerOperation::new( - self.current().map(|thread| thread.entity_id()), - self.composer.revision(), - ); - let picked = cx.prompt_for_paths(PathPromptOptions { + /// The paperclip: files of any kind, picked for the open chat. + pub(super) fn pick_files(&mut self, cx: &mut Context) { + if !self.composer_attaches(cx) { + return; + } + let chat = self.current().map(|thread| thread.entity_id()); + let pick = attach_files::AttachPick::new(chat, self.composer.revision()); + let picked = cx.prompt_for_paths(gpui_kit::PathPromptOptions { files: true, directories: false, multiple: true, @@ -309,12 +348,52 @@ impl AgentPanel { cx.spawn(async move |this, cx| { if let Ok(Ok(Some(paths))) = picked.await { let _ = this.update(cx, |this, cx| { - this.on_image_paths_picked(operation, paths, cx) + let chat = this.current().map(|thread| thread.entity_id()); + if pick.current(chat, this.composer.revision()) { + this.attach_paths(paths, cx); + } }); } }) .detach(); } + /// Attaches `paths`: images the agent takes as images, every other file as a link. + pub(super) fn attach_paths(&mut self, paths: Vec, cx: &mut Context) { + let Some(thread) = self.current() else { + return; + }; + let images_taken = thread + .read(cx) + .agent_info() + .is_none_or(|info| info.capabilities.image_prompts); + let (images, files) = attach_files::split(paths, images_taken); + if !images.is_empty() { + self.add_images(images, cx); + } + if files.is_empty() { + return; + } + let chat = thread.entity_id(); + let read = cx + .background_executor() + .spawn(async move { attach_files::link(&files) }); + cx.spawn(async move |this, cx| { + let linked = read.await; + let _ = this.update(cx, |this, cx| { + let Some(thread) = this.chats.find(chat).cloned() else { + return; + }; + let added = linked.and_then(|files| { + thread.update(cx, |thread, cx| thread.add_composer_files(files, cx)) + }); + if let Err(error) = added { + this.notices.set_error(Some(error.to_string())); + } + cx.notify(); + }); + }) + .detach(); + } pub(super) fn on_menu_open_changed( &mut self, kind: MenuKind, @@ -362,6 +441,7 @@ impl AgentPanel { pub(super) fn preparing_images(&self) -> bool { self.composer.preparing() } + #[cfg(test)] pub(super) fn preparing_queue_edit(&self) -> bool { self.composer.preparing_edit() } @@ -480,13 +560,23 @@ impl AgentPanel { return; }; let draft = self.original_composer_text(cx); - let ready = prompt.saved().images.is_empty(); + let ready = !prompt + .saved() + .images + .iter() + .any(|content| matches!(content, nocterm_ai::model::content::Content::Image(_))); + let files = prompt + .saved() + .images + .iter() + .filter_map(nocterm_ai::prompt_files::PromptFile::of) + .collect(); let binding = self .composer .begin_edit(thread.entity_id(), id, draft.clone(), ready); thread.update(cx, |thread, cx| { thread.set_draft(draft, cx); - thread.begin_composer_edit(Vec::new(), prompt.attachments().to_vec(), cx); + thread.begin_composer_edit(Vec::new(), files, prompt.attachments().to_vec(), cx); cx.notify(); }); if !ready { @@ -513,7 +603,7 @@ impl AgentPanel { thread.read(cx).session_ticket(), id, saved, - self.messages.inspector(), + self.messages.image_deps(), ) .launch(cx); self.composer.install_edit_preparation(&binding, id, task); @@ -561,16 +651,6 @@ impl AgentPanel { self.composer.set_text(draft, window, cx); } - pub(super) fn on_queue_height_observed( - &mut self, - chat: EntityId, - height: gpui_kit::Pixels, - cx: &mut Context, - ) { - if self.composer.observe_queue_height(chat, height) { - cx.notify(); - } - } pub(super) fn sync_composer_draft(&mut self, cx: &mut Context) { let Some(thread) = self.current() else { return; @@ -580,7 +660,6 @@ impl AgentPanel { thread.update(cx, |thread, cx| thread.set_draft(text, cx)); } } - /// Reads the plan limits again when the card opens. pub(super) fn close_menus(&mut self, cx: &mut Context) { self.menus.reset(); cx.notify(); @@ -593,7 +672,7 @@ impl AgentPanel { return; } self.composer.toggle_queue(); - cx.notify(); + self.layout.parts().notify(parts::PartKind::Footer, cx); } pub(super) fn on_attachment_selected( &mut self, @@ -618,18 +697,28 @@ impl AgentPanel { }); } } + pub(super) fn on_file_removed( + &mut self, + chat: gpui_kit::EntityId, + index: usize, + cx: &mut Context, + ) { + if let Some(thread) = self.chats.find(chat).cloned() { + thread.update(cx, |thread, cx| thread.remove_composer_file(index, cx)); + } + } pub(super) fn on_send_clicked( &mut self, chat: gpui_kit::EntityId, window: &mut Window, cx: &mut Context, ) { - if let Some(thread) = self.current().filter(|thread| thread.entity_id() == chat) { - if composer_renderer::ComposerRenderer::stops(&self.composer, &thread, cx) { - thread.update(cx, |thread, cx| thread.stop(cx)); - } else { - self.send(window, cx); - } + // The send button stops a run through its rendered stop; this sends. + if self + .current() + .is_some_and(|thread| thread.entity_id() == chat) + { + self.send(window, cx); } } pub(super) fn on_restart_clicked( diff --git a/crates/agent/src/panel/composer_frame.rs b/crates/agent/src/panel/composer_frame.rs new file mode 100644 index 00000000..4959e2a9 --- /dev/null +++ b/crates/agent/src/panel/composer_frame.rs @@ -0,0 +1,66 @@ +//! What the composer of the open chat offers, read once per drawing from its thread. +use super::composer_view::ComposerView; +use crate::thread::AgentThread; +use gpui_kit::{App, Entity, EntityId, WeakEntity}; +use nocterm_ai::model::usage::ContextUsage; +use nocterm_ai::session::Ticket; + +pub(super) struct ComposerFrame { + origin: EntityId, + can_prompt: bool, + can_stop: bool, + usage: Option, +} +impl ComposerFrame { + pub(super) fn read(thread: &Entity, cx: &App) -> Self { + let state = thread.read(cx); + Self { + origin: thread.entity_id(), + can_prompt: !state.session_sign_in_required(), + can_stop: state.session_generating() || state.queued_prompt_ids().len() != 0, + usage: state.transcript().usage.clone(), + } + } + /// The chat the composer sends to and its controls act on. + pub(super) fn origin(&self) -> EntityId { + self.origin + } + /// The context usage the usage ring shows. + pub(super) fn usage(&self) -> Option<&ContextUsage> { + self.usage.as_ref() + } + /// Whether the send button stops the run instead: nothing is typed, attached or edited + /// and there is a run to stop. + pub(super) fn stops(&self, composer: &ComposerView, images: bool, cx: &App) -> bool { + self.can_stop && composer.text(cx).trim().is_empty() && !images && !composer.editing() + } + /// Whether sending is unavailable: the chat takes no prompts now or images are being + /// prepared. + pub(super) fn send_disabled(&self, stops: bool, composer: &ComposerView) -> bool { + !stops && (!self.can_prompt || composer.preparing()) + } +} + +/// The stop the send button shows: the chat and the session it was drawn for. A stop drawn +/// for an earlier session of the chat stops nothing. +#[derive(Clone)] +pub(super) struct RenderedStop { + thread: WeakEntity, + ticket: Ticket, +} +impl RenderedStop { + pub(super) fn capture(thread: &Entity, cx: &App) -> Self { + Self { + thread: thread.downgrade(), + ticket: thread.read(cx).session_ticket(), + } + } + pub(super) fn activate(&self, cx: &mut App) { + let Some(thread) = self.thread.upgrade() else { + return; + }; + if thread.read(cx).session_ticket() == self.ticket { + thread.update(cx, |thread, cx| thread.stop(cx)); + } + } +} diff --git a/crates/agent/src/panel/composer_renderer.rs b/crates/agent/src/panel/composer_renderer.rs index c73c1006..f1c1b22f 100644 --- a/crates/agent/src/panel/composer_renderer.rs +++ b/crates/agent/src/panel/composer_renderer.rs @@ -13,7 +13,6 @@ use gpui_kit::{ px, rems, }; use nocterm_ai::model::auth::AuthFlow; -use nocterm_ai::model::usage::ContextUsage; use nocterm_ui::IconName; use super::{ @@ -33,12 +32,19 @@ fn remove_image(origin: gpui_kit::EntityId, index: usize, cx: &mut Context, clipboard: &ClipboardItem, cx: &mut App, ) -> bool { + if !panel + .upgrade() + .is_some_and(|panel| panel.read(cx).composer_attaches(cx)) + { + return false; + } let paths = clipboard .entries() .iter() @@ -49,7 +55,7 @@ pub(super) fn paste( .flatten() .collect::>(); if !paths.is_empty() { - let _ = panel.update(cx, |panel, cx| panel.add_images(paths, cx)); + let _ = panel.update(cx, |panel, cx| panel.attach_paths(paths, cx)); return true; } let image = clipboard.entries().iter().find_map(|entry| match entry { @@ -65,13 +71,15 @@ pub(super) fn paste( pub(super) struct ComposerRenderer; impl ComposerRenderer { - /// Removable chips for the attached terminals and servers. + /// Removable chips for the attached terminals, servers and files. pub(super) fn render_attachment_chips( thread: &Entity, labels: &super::attachments::AttachmentLabels, cx: &mut Context, ) -> Option { - if thread.read(cx).composer().attachments().is_empty() { + let servers = super::chat_servers::ChatServersRenderer::chips(thread, cx); + let composer = thread.read(cx).composer(); + if composer.attachments().is_empty() && composer.files().is_empty() && servers.is_empty() { return None; } let origin = thread.entity_id(); @@ -81,7 +89,8 @@ impl ComposerRenderer { .min_w_0() .gap_1() .flex_wrap() - .items_start(); + .items_start() + .children(servers); for attachment in thread.read(cx).composer().attachments() { let value = attachment.clone(); let label = labels.label(attachment); @@ -104,6 +113,22 @@ impl ComposerRenderer { })), ); } + for (index, file) in thread.read(cx).composer().files().iter().enumerate() { + let name = file.name().to_owned(); + chips = chips.child( + Button::new(("file-chip", index)) + .custom(menu_variant(cx)) + .small() + .icon(IconName::Paperclip) + .max_w_full() + .label(name.clone()) + .tooltip(format!("Remove {name}")) + .child(gpui_kit::component::Icon::new(IconName::X).xsmall()) + .on_click(cx.listener(move |this, _, _, cx| { + this.on_file_removed(origin, index, cx); + })), + ); + } Some(chips.into_any_element()) } /// Thumbnails of the images the next message will carry. @@ -182,7 +207,12 @@ impl ComposerRenderer { ) -> AnyElement { let generating = thread.read(cx).session_generating(); let mut controls = h_flex().w_full().min_w_0().gap_1().flex_wrap(); - for option in &thread.read(cx).config_options() { + let options = thread.read(cx).config_options(); + // Servers are attached from the attach menu, not configured here. + for option in options + .iter() + .filter(|option| !super::chat_servers::is_server(option)) + { let id = option.id.to_string(); controls = controls.child(super::menu::ConfigMenuRenderer::popover( menus, @@ -220,28 +250,60 @@ impl ComposerRenderer { } controls.into_any_element() } - /// Attach buttons, session controls, usage and send. + /// The attach controls, the session's options, usage and send. pub(super) fn render_footer( + frame: &super::composer_frame::ComposerFrame, composer: &super::composer_view::ComposerView, menus: &super::menu_view::MenuView, thread: &Entity, - usage: Option<&ContextUsage>, + stop: super::composer_frame::RenderedStop, cx: &mut Context, ) -> AnyElement { - let context_tooltip = format!( - "{} attached terminals · sent metadata {} B · terminal output {} B", - thread.read(cx).composer().attachments().len(), - thread.read(cx).context_bytes(), - thread.read(cx).tool_bytes() + let images = !thread.read(cx).composer().images().is_empty(); + let stops = frame.stops(composer, images, cx); + let attach = Self::render_attach(composer, menus, thread, cx); + let options = Self::render_config_controls(composer, menus, thread, cx); + let usage = super::usage::UsageCard::usage_button( + menus.is_open(&MenuKind::Usage), + frame.usage(), + cx, + ); + let send = Self::send_button( + frame.origin(), + stops, + frame.send_disabled(stops, composer), + stop, + composer.input_ticket(cx).ok(), + cx, ); - let images_supported = thread - .read(cx) - .agent_info() - .is_some_and(|info| info.capabilities.image_prompts); - let controls = Self::render_config_controls(composer, menus, thread, cx); h_flex() .gap_1() - .child(super::menu::ConfigMenuRenderer::popover( + .children(attach) + .child(div().flex_1().min_w_0().child(options)) + .child(usage) + .child(send) + .into_any_element() + } + /// Attach a terminal or server, and an image. + fn render_attach( + composer: &super::composer_view::ComposerView, + menus: &super::menu_view::MenuView, + thread: &Entity, + cx: &mut Context, + ) -> [AnyElement; 2] { + let context_tooltip = if super::chat_servers::takes_servers(thread.read(cx)) { + "Attach servers the agent may sign in to".to_owned() + } else { + format!( + "Attach terminal or server\n{} attached terminals · sent metadata {} B · terminal \ + output {} B", + thread.read(cx).composer().attachments().len(), + thread.read(cx).context_bytes(), + thread.read(cx).tool_bytes() + ) + }; + [ + super::menu::ConfigMenuRenderer::popover( menus, "context-popover", MenuKind::Context, @@ -249,46 +311,29 @@ impl ComposerRenderer { .ghost() .small() .icon(IconName::Plus) - .tooltip(format!("Attach terminal or server\n{context_tooltip}")), + .tooltip(context_tooltip), Anchor::BottomLeft, cx, - )) - .child( - Button::new("agent-attach-image") - .ghost() - .small() - .icon(IconName::ImagePlus) - .tooltip("Attach image") - .disabled(composer.preparing() || !images_supported) - .on_click(cx.listener(|this, _, _, cx| this.pick_images(cx))), ) - .child(div().flex_1().min_w_0().child(controls)) - .child(super::usage::UsageCard::usage_button( - menus.is_open(&MenuKind::Usage), - usage, - cx, - )) - .child(Self::render_send(composer, thread, cx)) - .into_any_element() - } - /// Whether the send button stops the running turn instead. - pub(super) fn stops( - composer: &super::composer_view::ComposerView, - thread: &Entity, - cx: &App, - ) -> bool { - thread.read(cx).session_generating() - && composer.text(cx).trim().is_empty() - && thread.read(cx).composer().images().is_empty() - && !composer.editing() + .into_any_element(), + Button::new("agent-attach-files") + .ghost() + .small() + .icon(IconName::Paperclip) + .tooltip("Attach files") + .disabled(composer.preparing()) + .on_click(cx.listener(|this, _, _, cx| this.pick_files(cx))) + .into_any_element(), + ] } - pub(super) fn render_send( - composer: &super::composer_view::ComposerView, - thread: &Entity, + fn send_button( + origin: gpui_kit::EntityId, + stop: bool, + disabled: bool, + captured: super::composer_frame::RenderedStop, + input: Option, cx: &mut Context, ) -> AnyElement { - let origin = thread.entity_id(); - let stop = Self::stops(composer, thread, cx); Button::new("agent-send") .primary() .small() @@ -305,10 +350,20 @@ impl ComposerRenderer { }) .flex_shrink_0() .tooltip(if stop { "Stop" } else { "Send" }) - .disabled(thread.read(cx).session_sign_in_required() || composer.preparing()) - .on_click( - cx.listener(move |panel, _, window, cx| panel.on_send_clicked(origin, window, cx)), - ) + .disabled(disabled || input.is_none()) + .on_click(cx.listener(move |panel, _, window, cx| { + if input + .as_ref() + .is_none_or(|input| !panel.composer.input_current(input, cx)) + { + return; + } + if stop { + captured.activate(cx); + } else { + panel.on_send_clicked(origin, window, cx); + } + })) .into_any_element() } /// Image preparation, restart and sign-in rows under the controls. diff --git a/crates/agent/src/panel/composer_view.rs b/crates/agent/src/panel/composer_view.rs index 13eb6957..9caf03ec 100644 --- a/crates/agent/src/panel/composer_view.rs +++ b/crates/agent/src/panel/composer_view.rs @@ -2,12 +2,57 @@ mod commands; use super::{AgentPanel, commands::CommandState}; use gpui_kit::{ - App, AppContext as _, Context, Entity, EntityId, FocusHandle, Focusable as _, Pixels, - SharedString, Subscription, Task, Window, + App, AppContext as _, Context, Entity, EntityId, FocusHandle, Focusable as _, SharedString, + Subscription, Task, Window, component::input::{InputEvent, Textarea, TextareaState}, - px, }; -use std::{collections::HashMap, sync::Arc}; +use std::{cell::RefCell, rc::Rc, sync::Arc}; + +#[derive(Clone)] +pub(super) struct InputTicket { + revision: u64, + reset: Rc<()>, +} +impl PartialEq for InputTicket { + fn eq(&self, other: &Self) -> bool { + self.revision == other.revision && Rc::ptr_eq(&self.reset, &other.reset) + } +} +impl Eq for InputTicket {} +struct InputGeneration { + observed: u64, + reset: Rc<()>, + fenced: bool, +} +impl Default for InputGeneration { + fn default() -> Self { + Self { + observed: 0, + reset: Rc::new(()), + fenced: false, + } + } +} +/// The composer's input was replaced since its text was read. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +#[error("The composer changed meanwhile.")] +pub(super) struct StaleInput; + +impl InputGeneration { + fn ticket(&mut self, revision: u64) -> Result { + // The native counter wraps. Once an observed generation regresses, + // only a new input instance can restore this fence. + self.fenced |= revision < self.observed; + self.observed = self.observed.max(revision); + if self.fenced { + return Err(StaleInput); + } + Ok(InputTicket { + revision, + reset: self.reset.clone(), + }) + } +} #[derive(Clone)] pub(super) struct ComposerBinding { @@ -33,11 +78,6 @@ struct QueueEdit { ready: bool, preparation: Option>, } -#[derive(Default)] -struct QueueView { - open: bool, - heights: HashMap, -} pub(super) struct EditRestoration { chat: EntityId, draft: String, @@ -49,11 +89,12 @@ impl EditRestoration { } pub(super) struct ComposerView { input: Entity, - queue: QueueView, + queue_open: bool, state: ComposerState, commands: CommandState, _bindings: Vec, binding: Option, + input_generation: RefCell, } impl ComposerView { pub(super) fn new(window: &mut Window, cx: &mut Context) -> Self { @@ -73,11 +114,12 @@ impl ComposerView { }); Self { input, - queue: QueueView::default(), + queue_open: false, state: ComposerState::default(), commands: CommandState::default(), _bindings: vec![submit, observe], binding: None, + input_generation: RefCell::new(InputGeneration::default()), } } pub(super) fn text(&self, cx: &App) -> SharedString { @@ -101,6 +143,14 @@ impl ComposerView { pub(super) fn revision(&self) -> u64 { self.state.revision } + pub(super) fn input_ticket(&self, cx: &App) -> Result { + let revision = self.input.read(cx).document_revision(); + self.input_generation.borrow_mut().ticket(revision) + } + pub(super) fn input_current(&self, ticket: &InputTicket, cx: &App) -> bool { + self.input_ticket(cx) + .is_ok_and(|current| ¤t == ticket) + } pub(super) fn editing(&self) -> bool { self.state.edit.is_some() } @@ -140,15 +190,16 @@ impl ComposerView { } } pub(super) fn clear(&mut self) { + self.input_generation.borrow_mut().reset = Rc::new(()); self.commands.reset(); self.state = ComposerState::default(); - self.queue = QueueView::default(); + self.queue_open = false; self.binding = None; } pub(super) fn leave(&mut self) { self.commands.reset(); self.invalidate(); - self.queue.open = false; + self.queue_open = false; } pub(super) fn take_edit(&mut self) -> Option { let edit = self.state.edit.take()?; @@ -273,31 +324,13 @@ impl ComposerView { true } pub(super) fn queue_open(&self) -> bool { - self.queue.open + self.queue_open } pub(super) fn open_queue(&mut self) { - self.queue.open = true; + self.queue_open = true; } pub(super) fn toggle_queue(&mut self) { - self.queue.open = !self.queue.open; - } - pub(super) fn queue_height(&self, chat: EntityId) -> Pixels { - self.queue.heights.get(&chat).copied().unwrap_or(px(0.)) - } - pub(super) fn observe_queue_height(&mut self, chat: EntityId, height: Pixels) -> bool { - if self - .queue - .heights - .get(&chat) - .is_some_and(|old| (*old - height).abs() <= px(0.5)) - { - return false; - } - self.queue.heights.insert(chat, height); - true - } - pub(super) fn removed_chats(&mut self, chats: &[EntityId]) { - self.queue.heights.retain(|id, _| !chats.contains(id)); + self.queue_open = !self.queue_open; } } diff --git a/crates/agent/src/panel/entries.rs b/crates/agent/src/panel/entries.rs index 81cc964f..5dc6bfc7 100644 --- a/crates/agent/src/panel/entries.rs +++ b/crates/agent/src/panel/entries.rs @@ -16,7 +16,7 @@ use super::{ AgentPanel, image_pipeline::PreviewState, message_actions::{MESSAGE_GROUP, escape_markdown, message_text}, - widgets::{chat_markdown, disclosure_header, safe_markdown}, + widgets::{chat_markdown, chat_markdown_in, disclosure_header, safe_markdown}, }; #[derive(Debug, thiserror::Error)] @@ -38,6 +38,7 @@ pub(super) struct RowPresentation { generating: bool, time: Option, redact: bool, + can_fork: bool, } pub(super) struct EntryRenderer; impl EntryRenderer { @@ -60,7 +61,7 @@ impl EntryRenderer { cx: &Context, ) -> ( gpui_kit::AnyElement, - Vec<(super::image_pipeline::ImageKey, Vec)>, + Vec, ) { let row = v_flex() .id(("agent-entry", frame.index)) @@ -82,7 +83,7 @@ impl EntryRenderer { cx, ), Entry::Agent(text) => Self::agent(row, &frame, text, messages, cx), - Entry::Thought(text) => Self::thought(row, &frame, text, cx), + Entry::Thought(text) => Self::thought(row, &frame, text, messages, cx), Entry::Tool(call) => Self::tool(row, &frame, call, cx), Entry::Content(Content::Image(image)) => { let key = Self::pending_image(image, messages, &mut pending); @@ -95,11 +96,12 @@ impl EntryRenderer { fn pending_image( image: &nocterm_ai::model::content::Image, messages: &super::message_list::MessageList, - pending: &mut Vec<(super::image_pipeline::ImageKey, Vec)>, + pending: &mut Vec, ) -> super::image_pipeline::ImageKey { - let key = (image.data.as_ptr() as usize, image.data.len(), true); + let source = super::image_pipeline::PreviewSource::Encoded(image.data.clone()); + let key = source.key(); if !messages.has_preview(&key) { - pending.push((key, image.data.as_bytes().to_vec())); + pending.push(source); } key } @@ -108,7 +110,7 @@ impl EntryRenderer { frame: &RowPresentation, parts: &[Content], messages: &super::message_list::MessageList, - pending: &mut Vec<(super::image_pipeline::ImageKey, Vec)>, + pending: &mut Vec, text: String, cx: &Context, ) -> gpui_kit::Stateful { @@ -125,6 +127,8 @@ impl EntryRenderer { .bg(cx.theme().muted); let mut images = h_flex().flex_wrap().gap_2(); let mut has_images = false; + let mut files = h_flex().flex_wrap().gap_1(); + let mut has_files = false; for (part_index, part) in parts.iter().enumerate() { match part { Content::Text(content) => { @@ -141,15 +145,22 @@ impl EntryRenderer { has_images = true; images = images.child(Self::image(messages.preview_state(&key))); } + Content::ResourceReference(link) => { + has_files = true; + files = files.child(Self::file(&link.name, cx)); + } _ => {} } } if has_images { bubble = bubble.child(images); } + if has_files { + bubble = bubble.child(files); + } row.child(bubble).child( super::message_actions::MessageRowActions::new( - frame.chat, + frame.can_fork.then_some(frame.chat), frame.index, text, frame.time, @@ -158,6 +169,23 @@ impl EntryRenderer { .render(cx), ) } + /// A file the message carried: its name beside a paperclip. + fn file(name: &str, cx: &Context) -> gpui_kit::Div { + h_flex() + .gap_1() + .px_1p5() + .py_0p5() + .rounded(px(6.)) + .border_1() + .border_color(cx.theme().border) + .text_xs() + .child( + gpui_kit::component::Icon::new(nocterm_ui::IconName::Paperclip) + .size_3() + .text_color(cx.theme().muted_foreground), + ) + .child(name.to_owned()) + } fn agent( mut row: gpui_kit::Stateful, frame: &RowPresentation, @@ -165,15 +193,20 @@ impl EntryRenderer { messages: &super::message_list::MessageList, cx: &Context, ) -> gpui_kit::Stateful { - let revealed = messages.revealed_text(frame.index, text); - row = row.group(MESSAGE_GROUP).child( - chat_markdown(("agent-message", frame.index), safe_markdown(&revealed)) - .stream_fade(frame.generating || messages.revealing()), - ); + let markdown = match messages.markdown(frame.index) { + Some(state) => chat_markdown_in(state), + None => chat_markdown( + ("agent-message", frame.index), + safe_markdown(&messages.revealed_text(frame.index, text)), + ), + }; + row = row + .group(MESSAGE_GROUP) + .child(markdown.stream_fade(frame.generating || messages.revealing())); if !frame.live { row = row.child( super::message_actions::MessageRowActions::new( - frame.chat, + frame.can_fork.then_some(frame.chat), frame.index, text.to_owned(), frame.time, @@ -188,6 +221,7 @@ impl EntryRenderer { mut row: gpui_kit::Stateful, frame: &RowPresentation, text: &str, + messages: &super::message_list::MessageList, cx: &Context, ) -> gpui_kit::Stateful { let index = frame.index; @@ -229,10 +263,13 @@ impl EntryRenderer { .max_w_full() .overflow_x_scroll() .child( - chat_markdown(("thought-text", index), safe_markdown(text)) - .text_color(cx.theme().muted_foreground) - .w_full() - .max_w_full(), + match messages.markdown(index) { + Some(state) => chat_markdown_in(state), + None => chat_markdown(("thought-text", index), safe_markdown(text)), + } + .text_color(cx.theme().muted_foreground) + .w_full() + .max_w_full(), ), ); } @@ -302,6 +339,11 @@ impl RowPresentation { generating, time, redact, + can_fork: false, } } + pub(super) fn with_fork_available(mut self, available: bool) -> Self { + self.can_fork = available; + self + } } diff --git a/crates/agent/src/panel/entries/tool_input.rs b/crates/agent/src/panel/entries/tool_input.rs index 66ea2d81..11de88ee 100644 --- a/crates/agent/src/panel/entries/tool_input.rs +++ b/crates/agent/src/panel/entries/tool_input.rs @@ -14,7 +14,7 @@ use gpui_kit::{ }; use nocterm_ai::model::tools::ToolRecord; use nocterm_ai::{ - TerminalCall, + TerminalCall, TransferFiles, tool_display::{self, ToolDisplay}, }; use nocterm_ui::IconName; @@ -217,9 +217,87 @@ fn request_source(call: TerminalCall) -> Source { TerminalCall::ListTerminals => { Source::plain("Scope", "Terminals attached to this chat".into(), None) } + TerminalCall::TransferFiles(v) => transfer_source(v), + TerminalCall::ReadTransfer(v) => transfer_handle(v.transfer_id, v.yield_ms), + TerminalCall::CancelTransfer(v) => transfer_handle(v.transfer_id, None), + call @ (TerminalCall::ListFiles(_) + | TerminalCall::ReadFile(_) + | TerminalCall::WriteFile(_) + | TerminalCall::EditFile(_) + | TerminalCall::MakeDirectory(_) + | TerminalCall::DeletePath(_) + | TerminalCall::MovePath(_)) => file_source(call), } } +/// A file call: its path, with what a change writes. +fn file_source(call: TerminalCall) -> Source { + let terminal = call.terminal_id().unwrap_or_default().to_owned(); + let mut source = match call { + TerminalCall::ListFiles(v) => Source::plain("Folder", v.path, None), + TerminalCall::ReadFile(v) => { + let mut source = Source::plain("File", v.path, None); + source.parameters.push(format!( + "From line {}, {} lines", + v.start_line.unwrap_or(1), + v.max_lines.unwrap_or(2000) + )); + source + } + TerminalCall::WriteFile(v) => { + let mut source = Source::plain("Content", v.content, None); + source.parameters.push(format!("File {}", v.path)); + if v.overwrite { + source.parameters.push("Replaces an existing file".into()); + } + source + } + TerminalCall::EditFile(v) => { + let mut source = Source::plain("Replace", v.old_text, None); + source.extra.push(Section { + label: "With", + text: v.new_text, + language: None, + }); + source.parameters.push(format!("File {}", v.path)); + if v.replace_all { + source.parameters.push("Every occurrence".into()); + } + source + } + TerminalCall::MakeDirectory(v) => Source::plain("New folder", v.path, None), + TerminalCall::DeletePath(v) => Source::plain("Delete", v.path, None), + TerminalCall::MovePath(v) => Source::plain("Move", format!("{}\n→ {}", v.from, v.to), None), + _ => Source::plain("Tool input", String::new(), None), + }; + source.parameters.push(format!("Terminal {terminal}")); + source +} + +fn transfer_source(v: TransferFiles) -> Source { + let mut source = Source::plain("Sources", v.sources.join("\n"), None); + source.parameters = vec![ + format!("From terminal {}", v.source_terminal_id), + format!( + "Into {} on terminal {}", + v.destination, v.destination_terminal_id + ), + format!( + "Existing files: {}", + if v.overwrite { "replace" } else { "skip" } + ), + ]; + source +} + +fn transfer_handle(transfer_id: String, wait_ms: Option) -> Source { + let mut source = Source::plain("Transfer handle", transfer_id, None); + if let Some(wait) = wait_ms { + source.parameters.push(format!("Wait {wait} ms")); + } + source +} + fn shell_script<'a>(program: &str, args: &'a [String]) -> Option<&'a str> { let program = program.rsplit('/').next()?; if !matches!(program, "bash" | "sh") || !matches!(args.first()?.as_str(), "-c" | "-lc") { diff --git a/crates/agent/src/panel/entries/tool_output/decode/payload.rs b/crates/agent/src/panel/entries/tool_output/decode/payload.rs index d6034064..896060d4 100644 --- a/crates/agent/src/panel/entries/tool_output/decode/payload.rs +++ b/crates/agent/src/panel/entries/tool_output/decode/payload.rs @@ -1,7 +1,9 @@ //! Decode the known bridge payload independently of provider envelopes. use super::super::Output; use crate::panel::entries::tool_input::Section; -use serde::Deserialize; +use nocterm_ai::tools::output::{ + CommandReport as Command, ObservationReport as Observation, TerminalReport as Terminal, +}; use serde_json::Value; /// Shape recognition protects payload fields from being mistaken for wrappers. @@ -87,18 +89,6 @@ pub(super) fn known(tool: &str, value: &Value) -> Option { Some(output) } -#[derive(Deserialize)] -struct Command { - command_id: String, - state: String, - stdout: String, - stderr: String, - exit_status: Option, - total_bytes: u64, - truncated: bool, - error: Option, -} - pub(super) fn command(value: &Value) -> Option { let value: Command = serde_json::from_value(value.clone()).ok()?; let state = match value.state.as_str() { @@ -141,18 +131,8 @@ pub(super) fn command(value: &Value) -> Option { Some(output) } -#[derive(Deserialize)] -struct Terminal { - text: String, - first_line: u64, - next_line: u64, - cursor_semantics: String, - truncated: bool, - alt_screen: bool, -} - fn terminal_output(value: Terminal) -> Option { - if value.cursor_semantics != "inclusive snapshot; replace overlapping lines" { + if value.cursor_semantics != Terminal::CURSOR_SEMANTICS { return None; } let mut output = Output::plain("Terminal output", value.text); @@ -172,18 +152,6 @@ pub(super) fn terminal(value: &Value) -> Option { terminal_output(serde_json::from_value(value.clone()).ok()?) } -#[derive(Deserialize)] -struct Observation { - text: String, - first_line: u64, - next_line: u64, - cursor_semantics: String, - truncated: bool, - alt_screen: bool, - completion: String, - completed: bool, - exit_status: Option, -} pub(super) fn observation(value: &Value) -> Option { let value: Observation = serde_json::from_value(value.clone()).ok()?; let status = match (value.completion.as_str(), value.completed) { @@ -195,14 +163,7 @@ pub(super) fn observation(value: &Value) -> Option { if value.exit_status.is_some() { return None; } - let mut output = terminal_output(Terminal { - text: value.text, - first_line: value.first_line, - next_line: value.next_line, - cursor_semantics: value.cursor_semantics, - truncated: value.truncated, - alt_screen: value.alt_screen, - })?; + let mut output = terminal_output(value.terminal)?; output.parameters.push(status.into()); output.parameters.push("Exit status: unknown".into()); Some(output) diff --git a/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs b/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs index 9a6c9576..a77b4f58 100644 --- a/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs +++ b/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs @@ -11,7 +11,7 @@ pub(super) enum Part { } pub(super) fn normalized(tool: Option<&str>, value: &Value, depth: usize) -> Option> { - if serde_json::to_vec(value).ok()?.len() > MAX_DECODE_BYTES { + if nocterm_ai::history::json_size(value).ok()? > MAX_DECODE_BYTES { return None; } let mut parts = Vec::new(); diff --git a/crates/agent/src/panel/history.rs b/crates/agent/src/panel/history.rs index 63f1c53c..94bf78d3 100644 --- a/crates/agent/src/panel/history.rs +++ b/crates/agent/src/panel/history.rs @@ -1,10 +1,13 @@ //! The chat history: search, pinned chats first, and each chat's actions — -//! rename, fork, pin and delete. +//! rename, fork, pin and delete. A hub window lists its hub's chats here too, on their own +//! side: a switch shows either the chats of agents on this computer or those of the hub's +//! agents on the server. The chats other windows hold are listed among them, marked, with +//! no actions: opening one moves it here or brings its window forward. use gpui_kit::{ Anchor, App, Context, Entity, TestSupportExt as _, component::{ - ActiveTheme as _, Sizable as _, + ActiveTheme as _, Selectable as _, Sizable as _, button::{Button, ButtonVariants as _}, h_flex, input::Input, @@ -15,12 +18,13 @@ use gpui_kit::{ px, }; use nocterm_agent_directory::AgentsHandle; -use nocterm_ai::agents::AgentProfile; +use nocterm_ai::agents::{AgentProfile, AvailableAgents}; use nocterm_ui::IconName; use std::time::Duration; use super::{ AgentPanel, + history_column::HistorySide, widgets::{relative_prompt_time, single_line_label}, }; use crate::thread::AgentThread; @@ -40,6 +44,7 @@ pub(super) fn order(chats: &[(bool, u64)]) -> Vec { order } +/// What a history row shows. struct HistoryRow { id: EntityId, title: String, @@ -50,6 +55,8 @@ struct HistoryRow { generating: bool, agent: Option, rename: Option, + /// Another window holds it. + elsewhere: bool, } struct HistoryRowRenderer; impl HistoryRowRenderer { @@ -138,7 +145,12 @@ impl HistoryRowRenderer { let hover = cx.theme().muted.blend(cx.theme().foreground.opacity(0.04)); let name = Self::name(&row.title, row.rename.take(), cx); let details = Self::details(&row, name, cx); - let actions = Self::actions(row.id, row.pinned, cx); + let elsewhere = row.elsewhere; + let actions = if elsewhere { + Self::elsewhere_mark(id, row.generating, cx) + } else { + Self::actions(id, row.pinned, cx) + }; h_flex() .id(("history-thread", id.as_u64())) .test_support() @@ -159,11 +171,41 @@ impl HistoryRowRenderer { cx.new(|_| gpui_kit::component::tooltip::Tooltip::new(title.clone())) .into() }) - .on_click(cx.listener(move |panel, _, _, cx| panel.on_history_selected(id, cx))) + .on_click(cx.listener(move |panel, _, window, cx| { + if elsewhere { + panel.open_from_other_window(id, window, cx); + } else { + panel.on_history_selected(id, cx); + } + })) .child(details) .child(actions) .into_any_element() } + /// Where the actions of a chat another window holds would be: that window has them. + fn elsewhere_mark(id: EntityId, working: bool, cx: &App) -> gpui_kit::AnyElement { + let tip = if working { + "Working in another window: opens there" + } else { + "Open in another window: opens here" + }; + div() + .id(("history-elsewhere", id.as_u64())) + .test_support() + .aria_label(tip) + .flex_shrink_0() + .px_1() + .child( + gpui_kit::component::Icon::new(IconName::ExternalLink) + .xsmall() + .text_color(cx.theme().muted_foreground), + ) + .tooltip(move |_, cx| { + cx.new(|_| gpui_kit::component::tooltip::Tooltip::new(tip)) + .into() + }) + .into_any_element() + } fn actions(id: EntityId, pinned: bool, cx: &mut Context) -> gpui_kit::AnyElement { let panel = cx.weak_entity(); h_flex() @@ -238,41 +280,114 @@ impl HistoryRowRenderer { pub(super) struct HistoryProjection; impl HistoryProjection { - /// The chats to list, in order, matching the search. + /// The chats to list, in order, matching the search, each with whether another window + /// holds it; in a hub window only those of the side shown. fn listed_threads( chats: &crate::roster::ChatRoster, history: &super::history_column::HistoryColumn, + hub_window: bool, cx: &App, - ) -> Vec> { + ) -> Vec<(Entity, bool)> { + let side = history.side(); let query = history.query(cx); - let chats_owner = chats; - let chats: Vec<(bool, u64)> = chats + let threads: Vec<(Entity, bool)> = chats .iter() - .map(|thread| { - ( - thread.read(cx).document().pinned(), - thread.read(cx).document().updated(), - ) + .map(|thread| (thread.clone(), false)) + .chain( + history + .others() + .listed(cx) + .into_iter() + .map(|thread| (thread, true)), + ) + .collect(); + let keys: Vec<(bool, u64)> = threads + .iter() + .map(|(thread, _)| { + let document = thread.read(cx).document(); + (document.pinned(), document.updated()) }) .collect(); - let threads = chats_owner.iter().collect::>(); - order(&chats) + order(&keys) .into_iter() - .map(|index| threads[index]) - .filter(|thread| { + .map(|index| threads[index].clone()) + .filter(|(thread, _)| { + let thread = thread.read(cx); + !hub_window || HistorySide::of(thread.document().agent_id()) == side + }) + .filter(|(thread, _)| { let thread = thread.read(cx); query.is_empty() || thread.title().to_lowercase().contains(&query) || thread.transcript().mentions(&query) || history.matches(thread.document().chat_id()) }) - .cloned() .collect() } + /// What the listed rows show, so a chat's change that none of them shows (a draft + /// keystroke, a streamed chunk) leaves the history as it is. A search matches + /// transcripts, so while one is typed there is no stamp. + pub(super) fn stamp( + chats: &crate::roster::ChatRoster, + history: &super::history_column::HistoryColumn, + cx: &App, + ) -> Option { + use std::hash::{Hash as _, Hasher as _}; + if !history.query(cx).is_empty() { + return None; + } + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + history.side().hash(&mut hasher); + chats + .current() + .map(|current| current.entity_id()) + .hash(&mut hasher); + let others = history.others().listed(cx); + for thread in chats.iter().chain(&others) { + let chat = thread.read(cx); + ( + thread.entity_id(), + chat.title(), + chat.document().pinned(), + chat.document().updated(), + chat.model(), + chat.session_generating(), + chat.document().agent_id(), + chat.draft_text().is_some(), + chat.history_entries().is_empty(), + chat.archive_summary().map(|row| row.has_content), + ) + .hash(&mut hasher); + } + Some(hasher.finish()) + } + /// The switch between the chats of this computer and those of the hub's agents. + fn sides(side: HistorySide, cx: &mut Context) -> gpui_kit::AnyElement { + let button = |id: &'static str, label: &'static str, shown: HistorySide| { + Button::new(id) + .small() + .ghost() + .label(label) + .selected(side == shown) + .on_click(cx.listener(move |panel, _, _, cx| panel.show_history_side(shown, cx))) + }; + h_flex() + .id("agent-history-sides") + .test_support() + .gap_1() + .pb_1() + .child(button("agent-history-local", "Local", HistorySide::Local)) + .child(button( + "agent-history-server", + "Server", + HistorySide::Server, + )) + .into_any_element() + } pub(super) fn render( chats: &crate::roster::ChatRoster, - history_owner: &super::history_column::HistoryColumn, - agents: &AgentsHandle, + column: &super::history_column::HistoryColumn, + (agents, hub_window): (&AgentsHandle, bool), now: u64, cx: &mut Context, ) -> gpui_kit::AnyElement { @@ -284,7 +399,10 @@ impl HistoryProjection { .overflow_y_scroll() .p_2() .gap_1(); - if chats.is_empty() { + if hub_window { + history = history.child(Self::sides(column.side(), cx)); + } + if chats.is_empty() && column.others().listed(cx).is_empty() { return history .child( div() @@ -299,14 +417,19 @@ impl HistoryProjection { } history = history.child( div().pb_1().child( - history_owner + column .query_input() .prefix(gpui_kit::component::Icon::new(IconName::Search).small()) .cleanable(true), ), ); - let listed = Self::listed_threads(chats, history_owner, cx); + let listed = Self::listed_threads(chats, column, hub_window, cx); if listed.is_empty() { + let empty = match (column.query(cx).is_empty(), hub_window, column.side()) { + (false, ..) | (true, false, _) => "No chats match", + (true, true, HistorySide::Local) => "No chats with agents on this computer", + (true, true, HistorySide::Server) => "No chats with the server's agents", + }; history = history.child( div() .id("agent-history-no-match") @@ -314,29 +437,34 @@ impl HistoryProjection { .px_1() .text_xs() .text_color(cx.theme().muted_foreground) - .child("No chats match"), + .child(empty), ); } - for thread in listed { - history = history.child(Self::render_history_row( - &thread, - chats, - history_owner, - agents, - now, - cx, - )); + // Resolved once: resolving builds the whole agent list from the catalog and settings. + let available = agents.available(cx); + // Pinned first, then the most recently changed. + let mut rows: Vec<((bool, u64), HistoryRow)> = Vec::with_capacity(listed.len()); + for (thread, elsewhere) in listed { + let document = thread.read(cx).document(); + let key = (document.pinned(), document.updated()); + let mut row = Self::local_row(&thread, chats, column, (agents, &available), now, cx); + row.elsewhere = elsewhere; + rows.push((key, row)); + } + rows.sort_by_key(|(key, _)| std::cmp::Reverse(*key)); + for (_, row) in rows { + history = history.child(HistoryRowRenderer::render(row, cx)); } history.into_any_element() } - fn render_history_row( + fn local_row( thread: &Entity, chats: &crate::roster::ChatRoster, history: &super::history_column::HistoryColumn, - agents: &AgentsHandle, + (agents, available): (&AgentsHandle, &AvailableAgents), now: u64, - cx: &mut Context, - ) -> gpui_kit::AnyElement { + cx: &App, + ) -> HistoryRow { let id = thread.entity_id(); let chat = thread.read(cx); let when = if chat.archive_summary().is_none_or(|row| !row.has_content) @@ -349,7 +477,7 @@ impl HistoryProjection { now.saturating_sub(chat.updated_at()), ))) }; - let row = HistoryRow { + HistoryRow { id, title: chat.title().to_string(), pinned: chat.document().pinned(), @@ -359,10 +487,10 @@ impl HistoryProjection { when, model: chat.model().unwrap_or_default(), generating: chat.session_generating(), - agent: agents.profile(chat.document().agent_id(), cx), + agent: agents.profile_in(available, chat.document().agent_id()), rename: history.rename_input(id), - }; - HistoryRowRenderer::render(row, cx) + elsewhere: false, + } } } diff --git a/crates/agent/src/panel/history_column.rs b/crates/agent/src/panel/history_column.rs index 2fd9a46b..ea9a2aa3 100644 --- a/crates/agent/src/panel/history_column.rs +++ b/crates/agent/src/panel/history_column.rs @@ -1,4 +1,4 @@ -//! Search, rename and display-clock lifetimes of the history column. +//! Search, rename, the listed side and display-clock lifetimes of the history column. use super::AgentPanel; use crate::runtime::HistoryReader; use gpui_kit::{ @@ -17,6 +17,26 @@ use std::{ time::Duration, }; +/// Which chats a hub window's history lists: those of agents on this computer, or those of +/// the hub's agents, which run on the server. Other windows have no hub chats and list all. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub(super) enum HistorySide { + #[default] + Local, + Server, +} + +impl HistorySide { + /// The side of a chat of `agent`. + pub(super) fn of(agent: &str) -> Self { + if nocterm_ai::agents::is_hub_agent(agent) { + Self::Server + } else { + Self::Local + } + } +} + #[derive(Clone)] pub(super) struct SearchTicket { query: String, @@ -111,9 +131,11 @@ struct HistoryClock { } pub(super) struct HistoryColumn { input: Entity, + side: HistorySide, search: SearchState, rename: Option, tick: Option, + others: super::other_windows::OtherWindowChats, _bindings: Vec, } impl HistoryColumn { @@ -126,9 +148,11 @@ impl HistoryColumn { }); Self { input, + side: HistorySide::default(), search: SearchState::default(), rename: None, tick: None, + others: Default::default(), _bindings: vec![subscription], } } @@ -138,6 +162,20 @@ impl HistoryColumn { self.input .update(cx, |input, cx| input.set_value("", window, cx)); } + pub(super) fn side(&self) -> HistorySide { + self.side + } + /// The chats other windows hold, listed here too. + pub(super) fn others(&self) -> &super::other_windows::OtherWindowChats { + &self.others + } + pub(super) fn others_mut(&mut self) -> &mut super::other_windows::OtherWindowChats { + &mut self.others + } + /// Lists `side`; returns whether that changed what is listed. + pub(super) fn show_side(&mut self, side: HistorySide) -> bool { + std::mem::replace(&mut self.side, side) != side + } pub(super) fn search_requested(&self, cx: &App) -> bool { !self.query(cx).is_empty() || !self.search.query.is_empty() } @@ -157,6 +195,7 @@ impl HistoryColumn { &mut self, ids: Vec, reader: HistoryReader, + admission: nocterm_agent_runtime::history_admission::HistoryAdmissionHandle, catalog: Vec<(EntityId, bool)>, cx: &mut Context, ) { @@ -166,29 +205,31 @@ impl HistoryColumn { let cancellation = Arc::new(AtomicBool::new(false)); let flag = cancellation.clone(); let query = ticket.query.clone(); - let work = cx.background_executor().spawn(async move { + let cancellation = SearchCancellation(cancellation); + self.search.task = Some(cx.spawn(async move |panel, cx| { + let _cancel = cancellation; let mut matches = HashSet::new(); for id in ids { if flag.load(Ordering::Acquire) { break; } - let (loaded, _permit) = reader.load(id.clone()).await; - if let Ok(chat) = loaded { - let transcript = nocterm_ai::thread::ThreadState { - entries: chat.entries, - ..Default::default() - }; - if transcript.mentions(&query) { - matches.insert(id); + let loading = + cx.update(|cx| reader.load_admitted(id.clone(), admission.clone(), cx)); + let loaded = loading.await; + let mut found = false; + loaded.publish(|loaded, _permit, _payload| { + if let Ok(chat) = loaded { + let transcript = nocterm_ai::thread::ThreadState { + entries: chat.entries, + ..Default::default() + }; + found = transcript.mentions(&query); } + }); + if found { + matches.insert(id); } } - matches - }); - let cancellation = SearchCancellation(cancellation); - self.search.task = Some(cx.spawn(async move |panel, cx| { - let _cancel = cancellation; - let matches = work.await; let _ = panel.update(cx, |panel, cx| { panel.on_archive_search_completed(ticket, matches, cx) }); diff --git a/crates/agent/src/panel/image_picker.rs b/crates/agent/src/panel/image_picker.rs deleted file mode 100644 index 76696f7b..00000000 --- a/crates/agent/src/panel/image_picker.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Picker delivery is bound to the original chat and composer revision. -use gpui_kit::EntityId; -pub(super) struct ImagePickerOperation { - chat: Option, - revision: u64, -} -impl ImagePickerOperation { - pub(super) fn new(chat: Option, revision: u64) -> Self { - Self { chat, revision } - } - pub(super) fn current(&self, chat: Option, revision: u64) -> bool { - self.chat == chat && self.revision == revision - } -} diff --git a/crates/agent/src/panel/image_pipeline.rs b/crates/agent/src/panel/image_pipeline.rs index 19914043..fc910f3e 100644 --- a/crates/agent/src/panel/image_pipeline.rs +++ b/crates/agent/src/panel/image_pipeline.rs @@ -1,15 +1,80 @@ -//! Preview requests retain cache generation and identity across background delivery. +//! Bounded previews retain immutable sources, so pointer identities cannot be reused. use crate::ImageDeps; use gpui_kit::{Image, Task}; -use nocterm_ai::images::{ImageInspector, ImageThumbnailer}; -use std::{collections::HashMap, sync::Arc}; +use nocterm_ai::{ + images::{ImageInspector, MAX_PREVIEW_BYTES}, + model::content::ImageData, +}; +use std::{cell::Cell, collections::HashMap, sync::Arc}; pub(super) type ImageKey = (usize, usize, bool); +const MAX_ENTRIES: usize = 32; +const MAX_BYTES: usize = 64 * 1024 * 1024; +const MAX_PREVIEW_RESIDENT: usize = MAX_PREVIEW_BYTES + 640 * 640 * 8; +const MAX_LOADING: usize = 4; + +#[derive(Clone)] +pub(super) enum PreviewSource { + Encoded(ImageData), + Original(Arc<[u8]>), +} +impl PreviewSource { + pub(super) fn retained_bytes(&self) -> usize { + match self { + Self::Encoded(data) => data.retained_bytes(), + Self::Original(data) => data.len().saturating_add( + 2 * std::mem::size_of::() + std::mem::align_of::() - 1, + ), + } + } + pub(super) fn key(&self) -> ImageKey { + match self { + Self::Encoded(data) => (data.as_ptr() as usize, data.len(), true), + Self::Original(data) => (data.as_ptr() as usize, data.len(), false), + } + } + pub(super) fn decode(&self) -> Result, super::entries::PreviewError> { + match self { + Self::Original(data) => Ok(data.clone()), + Self::Encoded(data) => { + use base64::Engine as _; + if data.len() > nocterm_ai::images::MAX_IMAGE_BYTES.div_ceil(3) * 4 { + return Err(nocterm_ai::images::ImageCodecError::SourceSize.into()); + } + Ok(base64::engine::general_purpose::STANDARD + .decode(data)? + .into()) + } + } + } +} #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) struct PreviewTicket { generation: u64, request: u64, key: ImageKey, } +#[derive(Clone)] +pub(super) struct CompletedPreview { + image: Arc, + resident_bytes: usize, +} +impl CompletedPreview { + pub(super) fn from_encoded(preview: nocterm_ai::images::EncodedPreview) -> Self { + let (width, height) = preview.dimensions(); + let resident_bytes = preview.bytes().len() + width as usize * height as usize * 8; + Self { + image: Arc::new(Image::from_bytes( + gpui_kit::ImageFormat::Png, + preview.bytes().to_vec(), + )), + resident_bytes, + } + } + + fn into_ready(self) -> (PreviewState, usize) { + (PreviewState::Ready(self.image), self.resident_bytes) + } +} pub(super) enum PreviewState { Loading, Ready(Arc), @@ -18,17 +83,20 @@ pub(super) enum PreviewState { struct PreviewEntry { ticket: PreviewTicket, state: PreviewState, + source: PreviewSource, + last_used: Cell, task: Option>, + charge: usize, } struct PreviewCache { entries: HashMap, generation: u64, next_request: u64, + clock: Cell, } pub(super) struct ImagePipeline { cache: PreviewCache, - inspector: Arc, - thumbnailer: Arc, + deps: ImageDeps, } impl ImagePipeline { pub(super) fn new(deps: ImageDeps) -> Self { @@ -37,27 +105,60 @@ impl ImagePipeline { entries: HashMap::new(), generation: 0, next_request: 0, + clock: Cell::new(0), }, - inspector: deps.inspector(), - thumbnailer: deps.thumbnailer(), + deps, } } pub(super) fn inspector(&self) -> Arc { - self.inspector.clone() + self.deps.inspector() + } + pub(super) fn image_deps(&self) -> ImageDeps { + self.deps.clone() } - pub(super) fn thumbnailer(&self) -> Arc { - self.thumbnailer.clone() + pub(super) fn admit_work(&self) -> Option { + self.deps.admit_work() } pub(super) fn contains_key(&self, key: &ImageKey) -> bool { - self.cache.entries.contains_key(key) + self.state(key).is_some() } pub(super) fn state(&self, key: &ImageKey) -> Option<&PreviewState> { - self.cache.entries.get(key).map(|entry| &entry.state) + let entry = self.cache.entries.get(key)?; + let next = self.cache.clock.get().saturating_add(1); + self.cache.clock.set(next); + entry.last_used.set(next); + Some(&entry.state) } - pub(super) fn request(&mut self, key: ImageKey) -> Option { + pub(super) fn request(&mut self, source: PreviewSource) -> Option { + let key = source.key(); if self.contains_key(&key) { return None; } + let loading = self + .cache + .entries + .values() + .filter(|entry| matches!(entry.state, PreviewState::Loading)) + .count(); + if loading >= MAX_LOADING { + return None; + } + let charge = source.retained_bytes().saturating_add(MAX_PREVIEW_RESIDENT); + if charge > MAX_BYTES { + return None; + } + while self.cache.entries.len() >= MAX_ENTRIES + || self.charged_bytes().saturating_add(charge) > MAX_BYTES + { + let oldest = self + .cache + .entries + .iter() + .filter(|(_, entry)| !matches!(entry.state, PreviewState::Loading)) + .min_by_key(|(_, entry)| entry.last_used.get()) + .map(|(key, _)| *key)?; + self.cache.entries.remove(&oldest); + } self.cache.next_request = self .cache .next_request @@ -68,16 +169,24 @@ impl ImagePipeline { request: self.cache.next_request, key, }; + let last_used = self.cache.clock.get().saturating_add(1); + self.cache.clock.set(last_used); self.cache.entries.insert( key, PreviewEntry { ticket, state: PreviewState::Loading, + source, + last_used: Cell::new(last_used), task: None, + charge, }, ); Some(ticket) } + fn charged_bytes(&self) -> usize { + self.cache.entries.values().map(|entry| entry.charge).sum() + } pub(super) fn install_task(&mut self, ticket: PreviewTicket, task: Task<()>) { if let Some(entry) = self .cache @@ -88,7 +197,11 @@ impl ImagePipeline { entry.task = Some(task); } } - pub(super) fn complete(&mut self, ticket: PreviewTicket, image: Option>) -> bool { + pub(super) fn complete( + &mut self, + ticket: PreviewTicket, + image: Option, + ) -> bool { let Some(entry) = self .cache .entries @@ -100,7 +213,10 @@ impl ImagePipeline { if !matches!(entry.state, PreviewState::Loading) { return false; } - entry.state = image.map_or(PreviewState::Failed, PreviewState::Ready); + let (state, bytes) = image.map_or((PreviewState::Failed, 0), CompletedPreview::into_ready); + entry.charge = entry.source.retained_bytes().saturating_add(bytes); + entry.state = state; + entry.task = None; true } pub(super) fn clear(&mut self) { diff --git a/crates/agent/src/panel/image_pipeline/tests.rs b/crates/agent/src/panel/image_pipeline/tests.rs index 3db5be13..a6d60a41 100644 --- a/crates/agent/src/panel/image_pipeline/tests.rs +++ b/crates/agent/src/panel/image_pipeline/tests.rs @@ -5,19 +5,86 @@ fn pipeline() -> ImagePipeline { let codec = Arc::new(ScriptedImageCodec::default()); ImagePipeline::new(ImageDeps::new(codec.clone(), codec)) } +fn completed_preview() -> CompletedPreview { + let bytes = include_bytes!("../tests/fixtures/prompt.png").to_vec(); + let bounds = nocterm_ai::images::PreviewBounds::new(640, 640).unwrap(); + let preview = nocterm_ai::images::EncodedPreview::new(bytes.into(), bounds).unwrap(); + CompletedPreview::from_encoded(preview) +} #[test] fn cleared_generation_cannot_install_old_success_or_failure_over_a_reused_key() { let mut pipeline = pipeline(); - let key = (0, 0, false); - let old = pipeline.request(key).unwrap(); + let source = PreviewSource::Original(vec![7].into()); + let key = source.key(); + let old = pipeline.request(source.clone()).unwrap(); pipeline.clear(); - let current = pipeline.request(key).unwrap(); - let image = Arc::new(Image::from_bytes(gpui_kit::ImageFormat::Png, vec![1])); + let current = pipeline.request(source.clone()).unwrap(); + let image = completed_preview(); assert!(!pipeline.complete(old, Some(image.clone()))); assert!(!pipeline.complete(old, None)); assert!(matches!(pipeline.state(&key), Some(PreviewState::Loading))); assert!(pipeline.complete(current, Some(image))); assert!(matches!(pipeline.state(&key), Some(PreviewState::Ready(_)))); - assert!(pipeline.request(key).is_none()); + assert!(pipeline.request(source.clone()).is_none()); assert!(!pipeline.complete(current, None)); } + +#[test] +fn retained_source_prevents_aliasing_and_cloned_image_reuses_its_preview() { + let mut pipeline = pipeline(); + let source = PreviewSource::Encoded("old image".to_owned().into()); + let key = source.key(); + let ticket = pipeline.request(source.clone()).unwrap(); + assert!(pipeline.complete(ticket, None)); + assert!(pipeline.request(source).is_none()); + let other = PreviewSource::Encoded("new image".to_owned().into()); + assert_ne!(key, other.key()); + assert!(pipeline.request(other).is_some()); +} +#[test] +fn preview_admission_bounds_concurrent_decodes_and_reclaims_finished_sources() { + let mut pipeline = pipeline(); + let first = PreviewSource::Original(vec![0; 5 * 1024 * 1024].into()); + let key = first.key(); + let ticket = pipeline.request(first.clone()).unwrap(); + assert!(pipeline.complete(ticket, None)); + for value in 1..=16 { + let source = PreviewSource::Original(vec![value as u8; 5 * 1024 * 1024].into()); + if let Some(ticket) = pipeline.request(source) { + assert!(pipeline.complete(ticket, None)); + } + } + assert!(pipeline.state(&key).is_none()); + pipeline.clear(); + for value in 0..MAX_LOADING { + assert!( + pipeline + .request(PreviewSource::Original(vec![value as u8].into())) + .is_some() + ); + } + assert!( + pipeline + .request(PreviewSource::Original(vec![99].into())) + .is_none() + ); +} + +#[test] +fn small_completed_previews_release_their_decode_reservation() { + let mut pipeline = pipeline(); + let mut sources = Vec::new(); + for value in 0..16u8 { + let source = PreviewSource::Original(vec![value; 1024].into()); + let ticket = pipeline.request(source.clone()).unwrap(); + let image = completed_preview(); + assert!(pipeline.complete(ticket, Some(image))); + sources.push(source); + } + for source in sources { + assert!(matches!( + pipeline.state(&source.key()), + Some(PreviewState::Ready(_)) + )); + } +} diff --git a/crates/agent/src/panel/image_preparation.rs b/crates/agent/src/panel/image_preparation.rs index f42bf872..0cb78ae1 100644 --- a/crates/agent/src/panel/image_preparation.rs +++ b/crates/agent/src/panel/image_preparation.rs @@ -10,6 +10,7 @@ pub(super) struct ImagePreparation { thread: WeakEntity, ticket: Ticket, work: BoxFuture<'static, Result, ImageAttachError>>, + permit: crate::image_deps::ImageWorkPermit, } impl ImagePreparation { pub(super) fn new( @@ -17,12 +18,14 @@ impl ImagePreparation { thread: WeakEntity, ticket: Ticket, work: BoxFuture<'static, Result, ImageAttachError>>, + permit: crate::image_deps::ImageWorkPermit, ) -> Self { Self { binding, thread, ticket, work, + permit, } } pub(super) fn launch(self, cx: &mut Context) { @@ -31,8 +34,12 @@ impl ImagePreparation { thread, ticket, work, + permit, } = self; - let future = cx.background_executor().spawn(work); + let future = cx.background_executor().spawn(async move { + let _permit = permit; + work.await + }); cx.spawn(async move |panel, cx| { let result = future.await; let _ = panel.update(cx, |panel, cx| { diff --git a/crates/agent/src/panel/images.rs b/crates/agent/src/panel/images.rs index fc92eb49..a9cda4ce 100644 --- a/crates/agent/src/panel/images.rs +++ b/crates/agent/src/panel/images.rs @@ -1,5 +1,7 @@ //! Image preparation runs outside the UI and belongs to one composer revision. -use nocterm_ai::images::{ImageCollectionError, MAX_IMAGE_BYTES, PromptImage, PromptImageError}; +use nocterm_ai::images::{ + ImageCollectionBudget, ImageCollectionError, MAX_IMAGE_BYTES, PromptImage, PromptImageError, +}; use std::{io::Read as _, path::PathBuf}; /// Why attaching images to the composer failed. @@ -13,19 +15,57 @@ pub(super) enum ImageAttachError { Invalid(#[from] PromptImageError), #[error(transparent)] Collection(#[from] ImageCollectionError), + #[error("Image processing is busy. Try again when the current images are ready.")] + Busy, } pub(super) fn read_images( paths: Vec, inspector: &dyn nocterm_ai::images::ImageInspector, + mut budget: ImageCollectionBudget, ) -> Result, ImageAttachError> { + budget.check_count(paths.len())?; paths .into_iter() .map(|path| { let mut file = std::fs::File::open(path)?; let mut bytes = Vec::new(); std::io::Read::take(&mut file, (MAX_IMAGE_BYTES + 1) as u64).read_to_end(&mut bytes)?; + budget.admit(bytes.len())?; Ok(PromptImage::validate(bytes, inspector)?) }) .collect() } + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_ai::testing::ScriptedImageCodec; + #[test] + fn excessive_selection_is_refused_before_opening_any_path() { + let codec = ScriptedImageCodec::default(); + let budget = ImageCollectionBudget::remaining(&[]).unwrap(); + let paths = vec![PathBuf::from("missing-image.png"); nocterm_ai::images::MAX_IMAGES + 1]; + assert!(matches!( + read_images(paths, &codec, budget), + Err(ImageAttachError::Collection(_)) + )); + assert!(codec.requests().is_empty()); + } + #[test] + fn aggregate_bytes_are_refused_before_the_next_decode() { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join("image.png"); + std::fs::write(&path, b"uninspected bytes").unwrap(); + let codec = ScriptedImageCodec::default(); + let mut budget = ImageCollectionBudget::remaining(&[]).unwrap(); + budget + .admit(nocterm_ai::images::MAX_TOTAL_IMAGE_BYTES) + .unwrap(); + assert!(matches!( + read_images(vec![path], &codec, budget), + Err(ImageAttachError::Collection(_)) + )); + assert!(codec.requests().is_empty()); + } +} diff --git a/crates/agent/src/panel/layout.rs b/crates/agent/src/panel/layout.rs index 50f4cac9..cb33a11f 100644 --- a/crates/agent/src/panel/layout.rs +++ b/crates/agent/src/panel/layout.rs @@ -1,4 +1,5 @@ -//! The history split retains its shared layout and reversible widening amount. +//! The history split retains its shared layout and reversible widening amount; the +//! panel's regions are views it keeps. use super::AgentPanel; use gpui_kit::{ AnyElement, App, AppContext as _, Context, Entity, Pixels, Subscription, TestSupportExt as _, @@ -87,6 +88,7 @@ pub(super) struct PanelLayout { layout: LayoutHandle, _bindings: Vec, refresh: [SplitRefresh; 2], + parts: super::parts::PanelParts, } impl PanelLayout { pub(super) fn new(layout: LayoutHandle, cx: &mut Context) -> Self { @@ -119,11 +121,19 @@ impl PanelLayout { layout, _bindings: bindings, refresh: std::array::from_fn(|_| SplitRefresh::Idle), + parts: super::parts::PanelParts::new(cx), } } pub(super) fn history(&self) -> bool { self.history } + /// The regions the panel draws as views of their own. + pub(super) fn parts(&self) -> &super::parts::PanelParts { + &self.parts + } + pub(super) fn parts_mut(&mut self) -> &mut super::parts::PanelParts { + &mut self.parts + } pub(super) fn maximized(&self) -> bool { self.maximized } diff --git a/crates/agent/src/panel/lifecycle.rs b/crates/agent/src/panel/lifecycle.rs index 2a786417..88fa18f4 100644 --- a/crates/agent/src/panel/lifecycle.rs +++ b/crates/agent/src/panel/lifecycle.rs @@ -29,7 +29,9 @@ impl AgentPanel { .iter() .map(|thread| (thread.entity_id(), thread.read(cx).is_archived())) .collect(); - self.history.start_search(ids, reader, catalog, cx); + let admission = self.chats.host().runtime().history_admission(cx); + self.history + .start_search(ids, reader, admission, catalog, cx); cx.notify(); } pub(super) fn on_archive_search_completed( @@ -61,7 +63,6 @@ impl AgentPanel { { self.leave_composer_with(true, cx); } - self.composer.removed_chats(&removed); self.chats.remove(&removed); if active.is_some_and(|id| removed.contains(&id)) { self.reset_chat_view(cx); @@ -321,7 +322,6 @@ impl AgentPanel { self.leave_composer_with(true, cx); } let retained = self.chats.clear_all(cx); - self.composer.removed_chats(retained.removed()); if let Some(error) = retained.into_error() { self.notices.set_error(Some(error.to_string())); } @@ -337,6 +337,9 @@ impl AgentPanel { self.sync_approval_attention(window, cx); } pub(super) fn new_thread(&mut self, id: String, window: &mut Window, cx: &mut Context) { + // The new chat is listed on its own side of a hub window's history. + self.history + .show_side(super::history_column::HistorySide::of(&id)); let terminal = self.chats.host().seat().terminals(cx).and_then(|entries| { entries .into_iter() @@ -393,7 +396,14 @@ impl AgentPanel { // Release ACP even if another observer still holds the old document entity. old.update(cx, |thread, cx| thread.release_resources(cx)); // Edits and provider tool statuses are final before taking the restart snapshot. - let data = old.read(cx).restart_data(); + let data = match old.update(cx, |thread, cx| thread.restart_data(cx)) { + Ok(data) => data, + Err(error) => { + self.notices.set_error(Some(error.to_string())); + cx.notify(); + return; + } + }; self.open_started( ChatStart::Restart { data: Box::new(data), @@ -480,6 +490,24 @@ impl AgentPanel { window.defer(cx, move |window, cx| { let _ = panel.update(cx, |panel, cx| panel.sync_approval_attention(window, cx)); }); + // The chat on screen draws its change at once: the transcript only when what it + // shows changed, so typing a draft leaves it. The history waits for the coalesced + // redraw. + if self + .current() + .is_some_and(|current| current.entity_id() == loaded.entity_id()) + { + // The usage card over the transcript shows the chat's usage. + let rows = loaded.read(cx).has_changed_rows() || self.menus.is_open(&MenuKind::Usage); + let stamp = super::parts::transcript_stamp(loaded.read(cx)); + let parts = self.layout.parts(); + parts.transcript_changed(rows, stamp, cx); + parts.notify(super::parts::PartKind::Footer, cx); + } + self.schedule_history_redraw(cx); + } + /// Redraws the history once the changes of the next frames have arrived. + fn schedule_history_redraw(&mut self, cx: &mut Context) { let Some(ticket) = self.notices.request_redraw() else { return; }; @@ -492,13 +520,90 @@ impl AgentPanel { }); self.notices.install_redraw(&ticket, task); } + /// What the runtime tells every window about the chats windows hold. + pub(super) fn on_holder_event( + &mut self, + event: nocterm_agent_runtime::HolderEvent, + window: &mut Window, + cx: &mut Context, + ) { + use nocterm_agent_runtime::HolderEvent; + match event { + HolderEvent::Changed => { + if self.history.others_mut().mark_stale() { + cx.defer_in(window, |panel, _, cx| panel.refresh_other_windows(cx)); + } + } + HolderEvent::Reveal { chat } if self.chats.contains(chat) => { + self.reveal_chat(chat, window, cx); + } + HolderEvent::Moved { chat } if self.chats.contains(chat) => { + self.retain_threads(|thread, _| thread.entity_id() != chat, cx); + self.refresh_other_windows(cx); + } + HolderEvent::Reveal { .. } | HolderEvent::Moved { .. } => {} + } + } + /// Lists the chats other windows hold anew. + pub(super) fn refresh_other_windows(&mut self, cx: &mut Context) { + let chats = self.chats.held_elsewhere(cx); + if self.history.others_mut().set(chats, cx) { + self.schedule_history_redraw(cx); + } + } + /// A chat another window holds changed: its row follows on the coalesced redraw. + pub(super) fn on_other_window_chat_changed(&mut self, cx: &mut Context) { + self.schedule_history_redraw(cx); + } + /// Opens chat `id`, which another window holds: here when it neither works nor shows + /// there, otherwise in that window, which comes forward. + pub(super) fn open_from_other_window( + &mut self, + id: EntityId, + window: &mut Window, + cx: &mut Context, + ) { + let Some(chat) = self.history.others().find(id) else { + return; + }; + match self.chats.take_over(&chat, cx) { + Ok(Some(thread)) => { + let opened = thread.entity_id(); + let slot = self.track(&thread, window, cx); + self.chats.append(slot); + self.refresh_other_windows(cx); + self.on_history_catalog_changed(cx); + self.open_thread(opened, cx); + } + Ok(None) => self.host.runtime().reveal_chat(id, cx), + Err(error) => self.notices.set_error(Some(error.to_string())), + } + cx.notify(); + } + /// Shows chat `chat`, which another window asked for, and brings this window forward. + fn reveal_chat(&mut self, chat: EntityId, window: &mut Window, cx: &mut Context) { + if self + .current() + .is_none_or(|current| current.entity_id() != chat) + { + self.open_thread(chat, cx); + } + if let Some(workspace) = self.host.workspace().upgrade() { + workspace.update(cx, |workspace, cx| { + workspace.open_panel(&crate::plugin::CHATS.id, window, cx) + }); + } + window.activate_window(); + cx.notify(); + } pub(super) fn on_coalesced_redraw( &mut self, ticket: &std::sync::Arc<()>, cx: &mut Context, ) { if self.notices.finish_redraw(ticket) { - cx.notify(); + let stamp = super::history::HistoryProjection::stamp(&self.chats, &self.history, cx); + self.layout.parts().history_changed(stamp, cx); } } } diff --git a/crates/agent/src/panel/menu.rs b/crates/agent/src/panel/menu.rs index 72bde70e..3753c33b 100644 --- a/crates/agent/src/panel/menu.rs +++ b/crates/agent/src/panel/menu.rs @@ -192,7 +192,7 @@ impl ConfigMenuRenderer { &frame.focus, window, cx, - )) + )); } Some(MenuKind::Context) => { menu = menu.child( @@ -237,6 +237,12 @@ impl ConfigMenuRenderer { ) -> String { match kind { Some(MenuKind::Agents) => "New agent".into(), + Some(MenuKind::Context) + if thread + .is_some_and(|thread| super::chat_servers::takes_servers(thread.read(cx))) => + { + "Servers the agent may sign in to".into() + } Some(MenuKind::Context) => "Servers and terminals".into(), Some(MenuKind::Modes) => "Agent mode".into(), Some(MenuKind::Usage) | None => String::new(), diff --git a/crates/agent/src/panel/message_actions.rs b/crates/agent/src/panel/message_actions.rs index d3c24d2a..1a21317b 100644 --- a/crates/agent/src/panel/message_actions.rs +++ b/crates/agent/src/panel/message_actions.rs @@ -65,7 +65,7 @@ pub(super) fn escape_markdown(text: &str) -> String { } pub(super) struct MessageRowActions { - thread: EntityId, + thread: Option, index: usize, text: String, time: Option, @@ -73,7 +73,7 @@ pub(super) struct MessageRowActions { } impl MessageRowActions { pub(super) fn new( - thread: EntityId, + thread: Option, index: usize, text: String, time: Option, @@ -113,16 +113,18 @@ impl MessageRowActions { cx.write_to_clipboard(ClipboardItem::new_string(text.clone())) }), ) - .child( - Button::new(("fork-message", index)) - .ghost() - .xsmall() - .icon(IconName::GitFork) - .tooltip("Fork chat from here") - .on_click(cx.listener(move |this, _, window, cx| { - this.fork_thread_at(thread, Some(index), window, cx) - })), - ) + .when_some(thread, |row, thread| { + row.child( + Button::new(("fork-message", index)) + .ghost() + .xsmall() + .icon(IconName::GitFork) + .tooltip("Fork chat from here") + .on_click(cx.listener(move |this, _, window, cx| { + this.fork_thread_at(thread, Some(index), window, cx) + })), + ) + }) .when_some(time, |row, time| { row.child( div() diff --git a/crates/agent/src/panel/message_list.rs b/crates/agent/src/panel/message_list.rs index 1c48c3c3..79f5815a 100644 --- a/crates/agent/src/panel/message_list.rs +++ b/crates/agent/src/panel/message_list.rs @@ -1,18 +1,21 @@ //! Transcript presentation binds reveal, navigation and previews to one visible chat. use super::{ AgentPanel, - image_pipeline::{ImageKey, ImagePipeline, PreviewState, PreviewTicket}, + image_pipeline::{ + CompletedPreview, ImageKey, ImagePipeline, PreviewSource, PreviewState, PreviewTicket, + }, + message_markdown::MessageMarkdown, navigation::MessageNavigation, stream::StreamReveal, }; use gpui_kit::{ - App, AppContext as _, Context, Entity, EntityId, Image, Task, - component::message_scroller::{MessageScroller, MessageScrollerState}, -}; -use nocterm_ai::{ - images::{ImageInspector, ImageThumbnailer}, - thread::Entry, + App, AppContext as _, Context, Entity, EntityId, Task, WeakEntity, + component::{ + message_scroller::{MessageScroller, MessageScrollerState}, + text::TextViewState, + }, }; +use nocterm_ai::{images::ImageInspector, thread::Entry}; use std::{collections::HashSet, sync::Arc, time::Duration}; #[derive(Clone)] @@ -39,6 +42,8 @@ pub(super) struct MessageList { navigation: MessageNavigation, expanded: HashSet, binding: Option, + thread: Option>, + markdown: MessageMarkdown, } impl MessageList { pub(super) fn new(images: crate::ImageDeps, cx: &mut Context) -> Self { @@ -51,6 +56,8 @@ impl MessageList { navigation: MessageNavigation::default(), expanded: HashSet::new(), binding: None, + thread: None, + markdown: MessageMarkdown::default(), } } pub(super) fn bind_chat(&mut self, chat: EntityId, cx: &mut Context) { @@ -70,6 +77,27 @@ impl MessageList { pub(super) fn binding(&self) -> Option { self.binding.clone() } + pub(super) fn bind_thread( + &mut self, + thread: &Entity, + cx: &mut Context, + ) { + let chat = thread.entity_id(); + if self.bound_to(chat) && self.thread.is_some() { + return; + } + self.bind_chat(chat, cx); + self.thread = Some(thread.downgrade()); + } + pub(super) fn bound_to(&self, chat: EntityId) -> bool { + self.binding + .as_ref() + .is_some_and(|binding| binding.chat == chat) + } + /// The chat the list shows, while it is open. + pub(super) fn thread(&self) -> Option> { + self.thread.as_ref()?.upgrade() + } fn current(&self, binding: &ActiveListBinding) -> bool { self.binding .as_ref() @@ -84,6 +112,8 @@ impl MessageList { self.expanded.clear(); self.pipeline.clear(); self.binding = None; + self.thread = None; + self.markdown.clear(); } pub(super) fn observe_rows( &mut self, @@ -93,10 +123,12 @@ impl MessageList { live: bool, reduced: bool, ) -> MessageRows { + self.markdown.invalidate(dirty); let previous = self.row_count; self.row_count = entries.len(); self.navigation.observe(chat, entries, dirty); let mut changed = dirty.to_vec(); + changed.extend(entries.len()..self.row_count.min(previous)); changed.extend(self.reveal.observe(entries, dirty, live, reduced)); MessageRows { previous, @@ -112,6 +144,7 @@ impl MessageList { } else { self.scroller .update(cx, |list, cx| list.reset(rows.count, cx)); + self.markdown.retain(rows.count); } } self.remeasure_rows(rows.changed, cx); @@ -189,17 +222,36 @@ impl MessageList { pub(super) fn revealed_text(&self, row: usize, text: &str) -> String { self.reveal.text(row, text) } + pub(super) fn revealed_slice<'a>(&self, row: usize, text: &'a str) -> &'a str { + self.reveal.slice(row, text) + } + pub(super) fn markdown_current(&self, row: usize, source_len: usize) -> bool { + self.markdown.current(row, source_len) + } pub(super) fn revealing(&self) -> bool { self.reveal.pending() } + /// Keeps the row's parsed Markdown current with what its entry draws now. + pub(super) fn prepare_markdown( + &mut self, + row: usize, + text: String, + source_len: usize, + cx: &mut Context, + ) { + self.markdown.prepare(row, text, source_len, cx); + } + pub(super) fn markdown(&self, row: usize) -> Option<&Entity> { + self.markdown.state(row) + } pub(super) fn preview_state(&self, key: &ImageKey) -> Option<&PreviewState> { self.pipeline.state(key) } pub(super) fn has_preview(&self, key: &ImageKey) -> bool { self.pipeline.contains_key(key) } - pub(super) fn request_preview(&mut self, key: ImageKey) -> Option { - self.pipeline.request(key) + pub(super) fn request_preview(&mut self, source: PreviewSource) -> Option { + self.pipeline.request(source) } pub(super) fn install_preview_task(&mut self, ticket: PreviewTicket, task: Task<()>) { self.pipeline.install_task(ticket, task); @@ -209,7 +261,7 @@ impl MessageList { binding: &ActiveListBinding, ticket: PreviewTicket, row: Option, - image: Option>, + image: Option, cx: &mut Context, ) -> bool { if !self.current(binding) || !self.pipeline.complete(ticket, image) { @@ -223,8 +275,11 @@ impl MessageList { pub(super) fn inspector(&self) -> Arc { self.pipeline.inspector() } - pub(super) fn thumbnailer(&self) -> Arc { - self.pipeline.thumbnailer() + pub(super) fn image_deps(&self) -> crate::ImageDeps { + self.pipeline.image_deps() + } + pub(super) fn admit_image_work(&self) -> Option { + self.pipeline.admit_work() } pub(super) fn scroller(&self, cx: &mut Context) -> MessageScroller { MessageScroller::new( diff --git a/crates/agent/src/panel/message_markdown.rs b/crates/agent/src/panel/message_markdown.rs new file mode 100644 index 00000000..06ba0e8b --- /dev/null +++ b/crates/agent/src/panel/message_markdown.rs @@ -0,0 +1,97 @@ +//! Parsed Markdown of the visible chat's agent messages and reasoning, kept per row: a +//! row scrolled back into view keeps its parse, and text streamed into a row parses +//! only what was appended instead of the whole message again. +use super::{AgentPanel, message_list::MessageList, parts::PartKind, widgets::safe_markdown}; +use gpui_kit::{AppContext as _, Context, Entity, Subscription, component::text::TextViewState}; +use nocterm_ai::thread::Entry; +use std::collections::HashMap; + +struct RowMarkdown { + state: Entity, + text: String, + source_len: Option, + _parsed: Subscription, +} + +#[derive(Default)] +pub(super) struct MessageMarkdown { + rows: HashMap, +} +impl MessageMarkdown { + /// The Markdown a row draws now: an agent message as revealed so far, or expanded + /// reasoning. Other rows draw no kept Markdown. + pub(super) fn text( + entry: &Entry, + row: usize, + messages: &MessageList, + ) -> Option<(String, usize)> { + let source = match entry { + Entry::Agent(text) => messages.revealed_slice(row, text), + Entry::Thought(text) if messages.expanded(row) => text, + _ => return None, + }; + if messages.markdown_current(row, source.len()) { + return None; + } + Some((safe_markdown(source), source.len())) + } + pub(super) fn current(&self, row: usize, source_len: usize) -> bool { + self.rows + .get(&row) + .is_some_and(|kept| kept.source_len == Some(source_len)) + } + pub(super) fn invalidate(&mut self, dirty: &[usize]) { + for row in dirty { + if let Some(kept) = self.rows.get_mut(row) { + kept.source_len = None; + } + } + } + /// Shows `text` in the row's state, appending when it extends what the row showed. + pub(super) fn prepare( + &mut self, + row: usize, + text: String, + source_len: usize, + cx: &mut Context, + ) { + if let Some(kept) = self.rows.get_mut(&row) { + kept.source_len = Some(source_len); + if kept.text == text { + return; + } + kept.state.update(cx, |state, cx| { + match text.strip_prefix(kept.text.as_str()) { + Some(appended) => state.push_str(appended, cx), + None => state.set_text(&text, cx), + } + }); + kept.text = text; + return; + } + let state = cx.new(|cx| TextViewState::markdown(&text, cx)); + // As an element's keyed state would: a finished parse redraws the transcript. + let parsed = cx.observe(&state, |panel, _, cx| { + panel.layout.parts().notify(PartKind::Transcript, cx); + }); + self.rows.insert( + row, + RowMarkdown { + state, + text, + source_len: Some(source_len), + _parsed: parsed, + }, + ); + } + pub(super) fn state(&self, row: usize) -> Option<&Entity> { + self.rows.get(&row).map(|kept| &kept.state) + } + /// Forgets the rows a shortened transcript no longer has. + pub(super) fn retain(&mut self, count: usize) { + self.rows.retain(|row, _| *row < count); + } + pub(super) fn clear(&mut self) { + self.rows.clear(); + } +} diff --git a/crates/agent/src/panel/notices.rs b/crates/agent/src/panel/notices.rs index 64d63b5a..4ca52e02 100644 --- a/crates/agent/src/panel/notices.rs +++ b/crates/agent/src/panel/notices.rs @@ -2,6 +2,8 @@ use gpui_kit::{EntityId, Task}; use std::sync::Arc; +use super::account_notice::AccountNotice; + struct Redraw { ticket: Arc<()>, task: Option>, @@ -14,16 +16,28 @@ pub(super) enum ApprovalEffect { #[derive(Default)] pub(super) struct PanelNotices { error: Option, + account: Option, redraw: Option, approval: Option>, + attention: bool, } impl PanelNotices { + /// Records whether the panel asks for attention; true when that changed. + pub(super) fn attention_changed(&mut self, attention: bool) -> bool { + std::mem::replace(&mut self.attention, attention) != attention + } pub(super) fn error(&self) -> Option<&str> { self.error.as_deref() } pub(super) fn set_error(&mut self, error: Option) { self.error = error; } + pub(super) fn account(&self) -> Option { + self.account + } + pub(super) fn set_account(&mut self, account: Option) { + self.account = account; + } pub(super) fn approval_changed( &mut self, fingerprint: Option>, diff --git a/crates/agent/src/panel/other_windows.rs b/crates/agent/src/panel/other_windows.rs new file mode 100644 index 00000000..e298abbb --- /dev/null +++ b/crates/agent/src/panel/other_windows.rs @@ -0,0 +1,62 @@ +//! The chats other windows hold, which this window's history lists without holding them. +//! +//! The runtime names them; each is observed so that its row follows it — its title, and the +//! spinner while its agent works. Only weak handles are kept: a chat lives as long as its own +//! window keeps it. A burst of changes (a window adopting its saved chats) refreshes the list +//! once. +use super::AgentPanel; +use crate::thread::AgentThread; +use gpui_kit::{App, Context, Entity, EntityId, Subscription, WeakEntity}; + +#[derive(Default)] +pub(super) struct OtherWindowChats { + chats: Vec>, + stale: bool, + _observations: Vec, +} +impl OtherWindowChats { + /// Lists `chats`, observing each so that its row follows it; whether that changed the list. + pub(super) fn set( + &mut self, + chats: Vec>, + cx: &mut Context, + ) -> bool { + self.stale = false; + let same = chats.len() == self.chats.len() + && chats + .iter() + .zip(&self.chats) + .all(|(chat, known)| chat.entity_id() == known.entity_id()); + if same { + return false; + } + self._observations = chats + .iter() + .map(|chat| cx.observe(chat, |panel, _, cx| panel.on_other_window_chat_changed(cx))) + .collect(); + self.chats = chats.iter().map(Entity::downgrade).collect(); + true + } + /// Marks the list for one refresh; `true` the first time since the last. + pub(super) fn mark_stale(&mut self) -> bool { + !std::mem::replace(&mut self.stale, true) + } + /// The chats with something to list: a chat no one wrote to yet stays in its window. + pub(super) fn listed(&self, cx: &App) -> Vec> { + self.chats + .iter() + .filter_map(WeakEntity::upgrade) + .filter(|chat| { + let chat = chat.read(cx); + !chat.history_entries().is_empty() + || chat.archive_summary().is_some_and(|row| row.has_content) + }) + .collect() + } + pub(super) fn find(&self, id: EntityId) -> Option> { + self.chats + .iter() + .find(|chat| chat.entity_id() == id) + .and_then(WeakEntity::upgrade) + } +} diff --git a/crates/agent/src/panel/parts.rs b/crates/agent/src/panel/parts.rs new file mode 100644 index 00000000..1f26ceca --- /dev/null +++ b/crates/agent/src/panel/parts.rs @@ -0,0 +1,139 @@ +//! The panel's regions as views, so a change redraws only the regions it touches. +//! +//! Every region is cached: the panel's own redraws reuse them unless they are +//! notified. Any notify of the panel invalidates all of them, so state the panel +//! changes stays correct by default; thread changes, typing and queue disclosure can +//! target a single region. +//! The footer (approvals, status, queue, commands and composer) is cached to its +//! content: reused, it keeps the height its content last took. +use super::AgentPanel; +use gpui_kit::{ + App, AppContext as _, Context, Entity, IntoElement, Render, StyleRefinement, Styled, + WeakEntity, Window, div, +}; +use std::hash::{Hash, Hasher}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum PartKind { + Transcript, + History, + Footer, +} + +pub(super) struct PanelPart { + panel: WeakEntity, + kind: PartKind, +} +impl Render for PanelPart { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + let kind = self.kind; + self.panel + .update(cx, |panel, cx| panel.render_part(kind, window, cx)) + .unwrap_or_else(|_| div().into_any_element()) + } +} + +pub(super) struct PanelParts { + transcript: Entity, + history: Entity, + footer: Entity, + transcript_seen: Option, + history_seen: Option, +} +impl PanelParts { + pub(super) fn new(cx: &mut Context) -> Self { + let panel = cx.weak_entity(); + let mut part = |kind| { + let panel = panel.clone(); + cx.new(|_| PanelPart { panel, kind }) + }; + Self { + transcript: part(PartKind::Transcript), + history: part(PartKind::History), + footer: part(PartKind::Footer), + transcript_seen: None, + history_seen: None, + } + } + /// The transcript fills the chat column above the footer. + pub(super) fn transcript(&self) -> impl IntoElement + use<> { + self.transcript.clone().cached( + StyleRefinement::default() + .w_full() + .flex_1() + .min_h_0() + .min_w_0(), + ) + } + /// The history fills its column, or the panel body when no chat is open. + pub(super) fn history(&self) -> impl IntoElement + use<> { + self.history.clone().cached( + StyleRefinement::default() + .w_full() + .flex_1() + .min_h_0() + .min_w_0(), + ) + } + /// The footer spans the chat column below the transcript, as tall as its content. + pub(super) fn footer(&self) -> impl IntoElement + use<> { + self.footer + .clone() + .cached_to_content(StyleRefinement::default().w_full()) + } + /// Redraws the cached regions: the panel's state changed in a way only it knows. + pub(super) fn invalidate(&self, cx: &mut App) { + self.transcript.update(cx, |_, cx| cx.notify()); + self.history.update(cx, |_, cx| cx.notify()); + self.footer.update(cx, |_, cx| cx.notify()); + } + pub(super) fn notify(&self, kind: PartKind, cx: &mut App) { + let part = match kind { + PartKind::Transcript => &self.transcript, + PartKind::History => &self.history, + PartKind::Footer => &self.footer, + }; + part.update(cx, |_, cx| cx.notify()); + } + /// Redraws the transcript when its rows or what they show differ from its last + /// drawing: a draft or a configuration change of the chat leaves it as it is. + pub(super) fn transcript_changed(&self, rows: bool, stamp: u64, cx: &mut App) { + if rows || self.transcript_seen != Some(stamp) { + self.notify(PartKind::Transcript, cx); + } + } + pub(super) fn drew_transcript(&mut self, stamp: Option) { + self.transcript_seen = stamp; + } + /// Redraws the history when the rows it lists differ from its last drawing; without + /// a stamp (a search matches transcripts) it always redraws. + pub(super) fn history_changed(&self, stamp: Option, cx: &mut App) { + if stamp.is_none() || self.history_seen != stamp { + self.notify(PartKind::History, cx); + } + } + pub(super) fn drew_history(&mut self, stamp: Option) { + self.history_seen = stamp; + } +} + +/// What the transcript draws besides its changed rows: the rows' count and liveness, and the +/// plan. +pub(super) fn transcript_stamp(thread: &crate::thread::AgentThread) -> u64 { + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + let transcript = thread.transcript(); + ( + transcript.entries.len(), + thread.session_generating(), + thread.session_accepts_updates(), + ) + .hash(&mut hasher); + if let Some(plan) = &transcript.plan { + for entry in &plan.entries { + entry.content.hash(&mut hasher); + std::mem::discriminant(&entry.priority).hash(&mut hasher); + std::mem::discriminant(&entry.status).hash(&mut hasher); + } + } + hasher.finish() +} diff --git a/crates/agent/src/panel/preview_operation.rs b/crates/agent/src/panel/preview_operation.rs index f07035c8..7f49f972 100644 --- a/crates/agent/src/panel/preview_operation.rs +++ b/crates/agent/src/panel/preview_operation.rs @@ -2,41 +2,35 @@ use super::entries::PreviewError; use super::{ AgentPanel, - image_pipeline::{ImageKey, PreviewTicket}, + image_pipeline::{CompletedPreview, PreviewSource, PreviewTicket}, message_list::{ActiveListBinding, MessageList}, }; -use base64::Engine as _; use gpui_kit::{Context, Task}; -use nocterm_ai::images::ImageThumbnailer; use nocterm_ai::model::content::Content; -use std::sync::Arc; pub(super) struct PreviewOperation { binding: ActiveListBinding, ticket: PreviewTicket, row: Option, - bytes: Vec, - encoded: bool, - thumbnailer: Arc, + source: PreviewSource, + images: crate::ImageDeps, } impl PreviewOperation { pub(super) fn cached( messages: &mut MessageList, - key: ImageKey, - data: impl FnOnce() -> Vec, - encoded: bool, + source: PreviewSource, row: Option, cx: &mut Context, ) -> gpui_kit::AnyElement { + let key = source.key(); if let Some(binding) = messages.binding() - && let Some(ticket) = messages.request_preview(key) + && let Some(ticket) = messages.request_preview(source.clone()) { let task = Self { binding, ticket, row, - bytes: data(), - encoded, - thumbnailer: messages.thumbnailer(), + source, + images: messages.image_deps(), } .launch(cx); messages.install_preview_task(ticket, task); @@ -48,29 +42,17 @@ impl PreviewOperation { binding, ticket, row, - bytes, - encoded, - thumbnailer, + source, + images, } = self; let future = cx.background_executor().spawn(async move { - let bytes = if encoded { - if bytes.len() > nocterm_ai::images::MAX_IMAGE_BYTES.div_ceil(3) * 4 { - return Err(PreviewError::Codec( - nocterm_ai::images::ImageCodecError::SourceSize, - )); - } - base64::engine::general_purpose::STANDARD.decode(bytes)? - } else { - bytes - }; + let _permit = images.work().await; + let bytes = source.decode()?; let bounds = nocterm_ai::images::PreviewBounds::new(640, 640)?; - let preview = thumbnailer.thumbnail(bytes.into(), bounds)?; + let preview = images.thumbnailer().thumbnail(bytes, bounds)?; // Supplied previews retain the same pure size/format contract. let preview = nocterm_ai::images::EncodedPreview::new(preview.bytes().clone(), bounds)?; - Ok::<_, PreviewError>(Arc::new(gpui_kit::Image::from_bytes( - gpui_kit::ImageFormat::Png, - preview.bytes().to_vec(), - ))) + Ok::<_, PreviewError>(CompletedPreview::from_encoded(preview)) }); cx.spawn(async move |panel, cx| { let result = future.await; @@ -92,22 +74,14 @@ impl PreviewOperation { .composer() .images() .iter() - .map(|image| { - let key = (image.bytes().as_ptr() as usize, image.bytes().len(), false); - ( - key, - (!messages.has_preview(&key)).then(|| image.bytes().to_vec()), - ) - }) + .map(|image| PreviewSource::Original(image.shared_bytes())) .collect::>(); if previews.is_empty() { return Vec::new(); } previews .into_iter() - .map(|(key, data)| { - Self::cached(messages, key, || data.unwrap_or_default(), false, None, cx) - }) + .map(|source| Self::cached(messages, source, None, cx)) .collect() } } @@ -125,17 +99,13 @@ impl PreviewOperation { .saved() .images .iter() - .zip(prompt.image_keys().iter()) - .filter_map(|(block, key)| { + .filter_map(|block| { let Content::Image(image) = block else { return None; }; - let key = (*key)?; Some(Self::cached( messages, - key, - || image.data.as_bytes().to_vec(), - true, + PreviewSource::Encoded(image.data.clone()), None, cx, )) diff --git a/crates/agent/src/panel/queue.rs b/crates/agent/src/panel/queue.rs index 0e5117a6..7ca8f2ea 100644 --- a/crates/agent/src/panel/queue.rs +++ b/crates/agent/src/panel/queue.rs @@ -1,11 +1,9 @@ -//! The queue peeks from behind the composer and unfolds above it. +//! The queue peeks from behind the composer and opens at its intrinsic height. use super::{AgentPanel, images::ImageAttachError}; use crate::thread::AgentThread; use base64::Engine as _; use gpui_kit::{ - AnyElement, Context, Entity, EntityId, SharedString, TestSupportExt as _, Window, - base::motion::{Transition, transition}, - canvas, + AnyElement, Context, Entity, EntityId, SharedString, TestSupportExt as _, component::{ ActiveTheme as _, Disableable as _, Sizable as _, button::{Button, ButtonVariants as _}, @@ -16,7 +14,6 @@ use gpui_kit::{ px, rems, }; use nocterm_ui::IconName; -use std::time::Duration; pub(super) fn decode_saved_image(data: &str) -> Result, ImageAttachError> { if data.len() > nocterm_ai::images::MAX_IMAGE_BYTES.div_ceil(3) * 4 { @@ -25,41 +22,8 @@ pub(super) fn decode_saved_image(data: &str) -> Result, ImageAttachError Ok(base64::engine::general_purpose::STANDARD.decode(data)?) } -/// One immutable frame of the queue, without authoritative queued messages. -pub(super) struct QueueFrame { - chat: EntityId, - open: bool, - measured: gpui_kit::Pixels, - editing: bool, -} pub(super) struct QueueRenderer; impl QueueRenderer { - pub(super) fn height( - frame: &QueueFrame, - window: &mut Window, - cx: &mut Context, - ) -> gpui_kit::Pixels { - transition( - ( - SharedString::from(format!("agent-queue-{}", frame.chat)), - "height", - ), - if frame.open { - frame.measured.min(px(240.)) - } else { - px(0.) - }, - Transition::new(Duration::from_millis(220)).ease(|t| { - if t >= 1. { - 1. - } else { - 1. - 2_f32.powf(-10. * t) - } - }), - window, - cx, - ) - } pub(super) fn hood( header: AnyElement, cx: &gpui_kit::App, @@ -76,10 +40,15 @@ impl QueueRenderer { .min_w_0() .child(header) } - fn rows(frame: &QueueFrame, rows: Vec, cx: &mut Context) -> AnyElement { - let owner = cx.weak_entity(); - let chat = frame.chat; - v_flex() + pub(super) fn render( + editing: bool, + header: AnyElement, + rows: Vec, + cx: &mut Context, + ) -> AnyElement { + // Normal layout resolves the current content, width and font in this frame. + // A previous-frame height followed by a transition delays even a warm open. + let rows = v_flex() .id("agent-queue-rows") .test_support() .relative() @@ -88,44 +57,12 @@ impl QueueRenderer { .min_w_0() .gap_2() .p_2() - .children(rows) - .child( - canvas( - move |bounds, _, cx| { - let owner = owner.clone(); - cx.defer(move |cx| { - let _ = owner.update(cx, |panel, cx| { - panel.on_queue_height_observed(chat, bounds.size.height, cx) - }); - }); - }, - |_, _, _, _| {}, - ) - .absolute() - .inset_0(), - ) - .into_any_element() - } - pub(super) fn render( - frame: &QueueFrame, - header: AnyElement, - rows: Vec, - height: gpui_kit::Pixels, - cx: &mut Context, - ) -> AnyElement { - let target = frame.measured.min(px(240.)); - let rows = Self::rows(frame, rows, cx); + .children(rows); let hood = Self::hood(header, cx).child( div() .id("agent-queue-reveal") .test_support() - .h(height) .overflow_hidden() - .opacity(if target > px(0.) { - (height / target).clamp(0., 1.) - } else { - 0. - }) .child( div() .id("agent-queue-scroll") @@ -135,7 +72,7 @@ impl QueueRenderer { .child(rows), ), ); - hood.when(frame.editing, |hood| { + hood.when(editing, |hood| { hood.child( h_flex() .px_2() @@ -314,7 +251,6 @@ impl QueueRenderer { previews: Vec>, thread: &Entity, labels: &super::attachments::AttachmentLabels, - height: gpui_kit::Pixels, cx: &mut Context, ) -> Option { let origin = thread.entity_id(); @@ -323,14 +259,8 @@ impl QueueRenderer { } let count = thread.read(cx).queued_prompt_ids().count(); let first = thread.read(cx).composer().queue()[0].saved().id; - let frame = QueueFrame { - chat: thread.entity_id(), - open: composer.queue_open(), - measured: composer.queue_height(thread.entity_id()), - editing: composer.editing_chat() == Some(thread.entity_id()), - }; let header = Self::queue_header(composer, origin, count, first, cx); - if !frame.open && height <= px(0.) { + if !composer.queue_open() { return Some( QueueRenderer::hood(header, cx) .test_support() @@ -342,23 +272,11 @@ impl QueueRenderer { .zip(previews) .map(|(prompt, images)| Self::queue_row(composer, origin, prompt, images, labels, cx)) .collect(); - Some(Self::render(&frame, header, rows, height, cx)) - } -} - -impl QueueRenderer { - pub(super) fn visible_height( - composer: &super::composer_view::ComposerView, - thread: &Entity, - window: &mut Window, - cx: &mut Context, - ) -> gpui_kit::Pixels { - let frame = QueueFrame { - chat: thread.entity_id(), - open: composer.queue_open(), - measured: composer.queue_height(thread.entity_id()), - editing: composer.editing_chat() == Some(thread.entity_id()), - }; - Self::height(&frame, window, cx) + Some(Self::render( + composer.editing_chat() == Some(origin), + header, + rows, + cx, + )) } } diff --git a/crates/agent/src/panel/queue_image_preparation.rs b/crates/agent/src/panel/queue_image_preparation.rs index ff260ef2..162c4f94 100644 --- a/crates/agent/src/panel/queue_image_preparation.rs +++ b/crates/agent/src/panel/queue_image_preparation.rs @@ -3,10 +3,7 @@ use super::{AgentPanel, composer_view::ComposerBinding, images::ImageAttachError use crate::thread::AgentThread; use gpui_kit::{Context, Task, WeakEntity}; use nocterm_ai::model::content::Content; -use nocterm_ai::{ - images::{ImageInspector, PromptImage}, - session::Ticket, -}; +use nocterm_ai::{images::PromptImage, session::Ticket}; use std::sync::Arc; pub(super) struct QueueImagePreparation { binding: ComposerBinding, @@ -14,7 +11,7 @@ pub(super) struct QueueImagePreparation { ticket: Ticket, prompt_id: u64, saved: Arc, - inspector: Arc, + images: crate::ImageDeps, } impl QueueImagePreparation { pub(super) fn new( @@ -23,7 +20,7 @@ impl QueueImagePreparation { ticket: Ticket, prompt_id: u64, saved: Arc, - inspector: Arc, + images: crate::ImageDeps, ) -> Self { Self { binding, @@ -31,7 +28,7 @@ impl QueueImagePreparation { ticket, prompt_id, saved, - inspector, + images, } } pub(super) fn launch(self, cx: &mut Context) -> Task<()> { @@ -41,15 +38,26 @@ impl QueueImagePreparation { ticket, prompt_id, saved, - inspector, + images, } = self; let future = cx.background_executor().spawn(async move { + let _permit = images.work().await; + let inspector = images.inspector(); + let mut budget = nocterm_ai::images::ImageCollectionBudget::remaining(&[])?; + budget.check_count( + saved + .images + .iter() + .filter(|content| matches!(content, Content::Image(_))) + .count(), + )?; saved .images .iter() .filter_map(|block| match block { Content::Image(image) => Some( super::queue::decode_saved_image(&image.data).and_then(|bytes| { + budget.admit(bytes.len())?; PromptImage::validate(bytes, inspector.as_ref()) .map_err(ImageAttachError::from) }), diff --git a/crates/agent/src/panel/shell.rs b/crates/agent/src/panel/shell.rs index 99c0e54d..e4ff8f18 100644 --- a/crates/agent/src/panel/shell.rs +++ b/crates/agent/src/panel/shell.rs @@ -90,7 +90,7 @@ impl ShellRenderer { .on_action(cx.listener(|this, _: &crate::ToggleModelPicker, _, cx| { this.toggle_model_picker(cx); })) - .on_action(cx.listener(|this, _: &crate::AttachImage, _, cx| this.pick_images(cx))) + .on_action(cx.listener(|this, _: &crate::AttachFiles, _, cx| this.pick_files(cx))) } pub(super) fn header( diff --git a/crates/agent/src/panel/stream.rs b/crates/agent/src/panel/stream.rs index c0f3d8ca..b132e65a 100644 --- a/crates/agent/src/panel/stream.rs +++ b/crates/agent/src/panel/stream.rs @@ -79,7 +79,7 @@ impl StreamReveal { }); changed } - pub(super) fn text(&self, index: usize, text: &str) -> String { + pub(super) fn slice<'a>(&self, index: usize, text: &'a str) -> &'a str { let mut end = self .bytes .get(&index) @@ -89,7 +89,10 @@ impl StreamReveal { while !text.is_char_boundary(end) { end -= 1; } - text[..end].to_owned() + &text[..end] + } + pub(super) fn text(&self, index: usize, text: &str) -> String { + self.slice(index, text).to_owned() } pub(super) fn pending(&self) -> bool { !self.backlog.is_empty() diff --git a/crates/agent/src/panel/tests/approvals.rs b/crates/agent/src/panel/tests/approvals.rs index 23fadacc..bb28b0d1 100644 --- a/crates/agent/src/panel/tests/approvals.rs +++ b/crates/agent/src/panel/tests/approvals.rs @@ -151,7 +151,7 @@ fn live_group_membership_and_revoked_registration_reject_stale_terminal_ids( fn queued_long_terminal_approvals_fit_short_narrow_panel_and_actions_respond( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let responses = cx.update(|cx| { let thread = f.panel.read(cx).current().unwrap(); @@ -257,7 +257,7 @@ fn queued_long_terminal_approvals_fit_short_narrow_panel_and_actions_respond( fn permission_approval_choices_are_clickable_and_cancel_removes_last_request( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let responses = cx.update(|cx| { let thread = f.panel.read(cx).current().unwrap(); diff --git a/crates/agent/src/panel/tests/archive.rs b/crates/agent/src/panel/tests/archive.rs index 3c6683d2..2b95e478 100644 --- a/crates/agent/src/panel/tests/archive.rs +++ b/crates/agent/src/panel/tests/archive.rs @@ -70,7 +70,11 @@ fn catalog_rows_are_idle_and_opening_hydrates_and_connects_with_the_queue_paused cx.update(|cx| { assert!(thread.read(cx).is_archived()); assert!(thread.read(cx).history_entries().is_empty()); - assert!(thread.read(cx).flush_snapshot(cx).is_none()); + assert!( + thread + .update(cx, |thread, cx| thread.flush_snapshot(cx).unwrap()) + .is_none() + ); }); f.panel .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); @@ -137,7 +141,7 @@ fn delete_while_loading_does_not_resurrect_the_document(cx: &mut TestAppContext) #[gpui_kit::test] fn restored_root_controls_process_sandbox_restore_and_fresh_fallback(cx: &mut TestAppContext) { let resolver = workspace_resolver(); - let f = fixture_with_width(cx, 26., usage_source(), resolver.clone(), image_deps()); + let f = fixture_with_width(cx, 26., resolver.clone(), image_deps()); cx.run_until_parked(); let a = f._directory.path().join("A"); let b = f._directory.path().join("B"); @@ -379,7 +383,7 @@ fn native_quit_flushes_pending_archived_draft_with_history_gate_held(cx: &mut Te #[gpui_kit::test] fn invalid_saved_root_fails_safely_and_legacy_missing_root_starts_fresh(cx: &mut TestAppContext) { let resolver = workspace_resolver(); - let f = fixture_with_width(cx, 26., usage_source(), resolver.clone(), image_deps()); + let f = fixture_with_width(cx, 26., resolver.clone(), image_deps()); cx.run_until_parked(); let mut invalid = saved(&f); invalid.session_id = Some("old-session".into()); @@ -701,3 +705,64 @@ fn installation_capacity_refuses_restore_before_mutation_and_keeps_shown_project cx.run_until_parked(); assert_eq!(f.connector.connects.load(Ordering::SeqCst), 0); } + +#[gpui_kit::test] +fn deleting_a_loading_chat_keeps_its_actual_decoded_native_result_charged_until_drop( + cx: &mut TestAppContext, +) { + use nocterm_agent_runtime::history_admission::{ + MAX_NORMAL_HISTORY_BYTES, MAX_NORMAL_HISTORY_PAYLOADS, + }; + let f = fixture(cx); + cx.run_until_parked(); + let mut chat = saved(&f); + chat.entries.push(Entry::Agent("x".repeat(2 * 1024 * 1024))); + let thread = archived(&f, &chat, cx); + let decoded = f + .repository + .block_next(nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone(), + )); + f.panel + .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); + cx.run_until_parked(); + assert!( + f.repository + .operations() + .contains(&nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone() + )) + ); + let admission = cx.update(|cx| f.installation.installed.runtime().history_admission(cx)); + cx.update_window(f.handle, |_, window, cx| { + f.panel.update(cx, |panel, cx| { + panel.delete_thread(thread.entity_id(), window, cx) + }) + }) + .unwrap(); + drop(thread); + cx.run_until_parked(); + let slots = (0..MAX_NORMAL_HISTORY_PAYLOADS) + .filter_map(|_| admission.reserve_payload().ok()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); + cx.update(|cx| { + assert!( + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES - 2 * 1024 * 1024, cx) + .is_err() + ) + }); + decoded.send(Ok(())).unwrap(); + cx.run_until_parked(); + let released = admission.reserve_payload().unwrap(); + cx.update(|cx| { + drop( + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES - 2 * 1024 * 1024, cx) + .unwrap(), + ) + }); + assert!(f.repository.transcript(&chat.header.id).is_err()); + drop((released, slots)); +} diff --git a/crates/agent/src/panel/tests/authentication.rs b/crates/agent/src/panel/tests/authentication.rs index e4ba0b36..20a92291 100644 --- a/crates/agent/src/panel/tests/authentication.rs +++ b/crates/agent/src/panel/tests/authentication.rs @@ -186,7 +186,6 @@ fn terminal_authentication_uses_host_callback_and_retries_after_cancel_or_failur |f| { let mut services = fixture_services(f); services.terminal_auth = Some(opener); - services.usage_source = Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()); services }, cx, diff --git a/crates/agent/src/panel/tests/bridge_outcomes.rs b/crates/agent/src/panel/tests/bridge_outcomes.rs index 472ac994..27a58403 100644 --- a/crates/agent/src/panel/tests/bridge_outcomes.rs +++ b/crates/agent/src/panel/tests/bridge_outcomes.rs @@ -85,9 +85,12 @@ fn check_provider(provider: &str, cx: &mut TestAppContext) { update(&f, &session, SessionUpdate::ToolCall(Box::new(row)), cx); if id == "mcp-rejection" { let mut mcp = nocterm_ai::mcp::McpSession::default(); - mcp.handle(r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#); + mcp.handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + Default::default(), + ); let nocterm_ai::mcp::McpStep::Rejected { tool, arguments, error, .. } = - mcp.handle(&json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":tool,"arguments":args}}).to_string()) + mcp.handle(&json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":tool,"arguments":args}}).to_string(), Default::default()) else { panic!("rejected"); }; f.rejections .try_send(nocterm_ai::BridgeRejection { diff --git a/crates/agent/src/panel/tests/chat_servers.rs b/crates/agent/src/panel/tests/chat_servers.rs new file mode 100644 index 00000000..101a9515 --- /dev/null +++ b/crates/agent/src/panel/tests/chat_servers.rs @@ -0,0 +1,63 @@ +//! A chat whose agent runs elsewhere attaches the servers its agent may sign in to, from the +//! attach menu, instead of this device's terminals; the attached ones show as chips. +use super::*; +use nocterm_ai::model::controls::{ConfigCategory, ConfigChoice, ConfigOption, ConfigSetting}; + +fn server(id: &str, name: &str, attached: bool) -> ConfigOption { + ConfigOption::boolean(format!("server:{id}"), name, attached) + .description(format!("root@{id}.example")) + .category(ConfigCategory::Server) +} + +fn hub_chat(f: &Fixture, cx: &mut TestAppContext) { + f.connector.run_elsewhere(); + f.commands.config.set_options(vec![ + ConfigOption::select("model", "Model", "m1", vec![ConfigChoice::new("m1", "m1")]) + .category(ConfigCategory::Model), + server("web", "Web", false), + server("build", "Build", true), + ]); + new_chat(f, cx); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn the_attach_menu_of_an_agent_that_runs_elsewhere_lists_the_chats_servers( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + hub_chat(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + // Servers are not configured beside the model; one is attached already. + assert!(window.try_find("config-picker-model").is_some()); + assert!(window.try_find("config-picker-server:web").is_none()); + assert!(window.try_find("server-chip-0").is_some()); + assert!(window.try_find("server-chip-1").is_none()); + window.click("agent-attach-context", cx); + window.render_frame(cx); + assert!(window.try_find(("attach-server", 0usize)).is_some()); + assert!(window.try_find(("attach-server", 1usize)).is_some()); + // None of this device's terminals or saved servers are offered. + assert!(window.try_find("attach-group-homelab").is_none()); + window.click(("attach-server", 0usize), cx); + }) + .unwrap(); + cx.run_until_parked(); + let request = f.commands.config.requests().pop().expect("a choice"); + assert_eq!(request.config.as_str(), "server:web"); + assert_eq!(request.setting, ConfigSetting::Boolean(true)); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("server-chip-1").is_some()); + // The menu stays open for more choices; once closed, a chip's cross detaches its server. + assert!(window.try_find(("attach-server", 0usize)).is_some()); + window.click("agent-attach-context", cx); + window.render_frame(cx); + window.click("server-chip-0", cx); + }) + .unwrap(); + cx.run_until_parked(); + let request = f.commands.config.requests().pop().expect("a choice"); + assert_eq!(request.setting, ConfigSetting::Boolean(false)); +} diff --git a/crates/agent/src/panel/tests/commands.rs b/crates/agent/src/panel/tests/commands.rs index 1c1e3437..1404ce53 100644 --- a/crates/agent/src/panel/tests/commands.rs +++ b/crates/agent/src/panel/tests/commands.rs @@ -49,7 +49,7 @@ fn clicked_unicode_command_keeps_focus_and_appends_at_end(cx: &mut TestAppContex #[gpui_kit::test] fn long_description_has_same_row_height_and_bounded_full_details(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let long = "First paragraph\n\n## Instructions\n\n".to_string() + &"Extensive description with examples.\n".repeat(150); diff --git a/crates/agent/src/panel/tests/composer.rs b/crates/agent/src/panel/tests/composer.rs index 7ce01a72..72ccb795 100644 --- a/crates/agent/src/panel/tests/composer.rs +++ b/crates/agent/src/panel/tests/composer.rs @@ -4,79 +4,7 @@ use nocterm_ai::model::controls::{ConfigChoice, ConfigOption}; use nocterm_ai::model::session::{PromptOutcome, SessionNotice, SessionUpdate, StopReason}; use nocterm_ai::model::usage::{ContextUsage, TokenTotals}; -#[gpui_kit::test] -fn usage_card_shows_context_tokens_and_plan_limits(cx: &mut TestAppContext) { - let source = Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()); - source.push(Ok(Some(vec![ - nocterm_ai::usage::RateLimit { - window: nocterm_ai::usage::Window::Weekly, - used: Some(0.38), - resets_at: Some(1_791_588_105), - reached: false, - }, - nocterm_ai::usage::RateLimit { - window: nocterm_ai::usage::Window::FiveHour, - used: Some(0.12), - resets_at: Some(1_791_500_000), - reached: false, - }, - ]))); - let f = fixture_with_width(cx, 26., source, workspace_resolver(), image_deps()); - new_chat(&f, cx); - let session = cx.update(|cx| { - let thread = f.panel.read(cx).current().unwrap(); - thread.update(cx, |thread, cx| thread.send("hello".into(), cx)); - thread.read(cx).session().clone().unwrap() - }); - cx.run_until_parked(); - f.events - .try_send(AgentEvent::Session(SessionNotice::new( - session, - SessionUpdate::UsageUpdate(ContextUsage::new(25_900, 256_000)), - ))) - .unwrap(); - cx.run_until_parked(); - let finish = f.commands.pending.lock().unwrap().take().unwrap(); - finish - .send(PromptOutcome { - usage: Some(TokenTotals { - cache_read: Some(20_000), - ..TokenTotals::new(30_000, 25_000, 5_000) - }), - ..PromptOutcome::new(StopReason::EndTurn) - }) - .unwrap(); - cx.run_until_parked(); - cx.update(|cx| { - let runtime = f.installation.installed.runtime(); - let limits: Vec<_> = runtime - .limits("codex", cx) - .unwrap() - .iter() - .cloned() - .collect(); - assert_eq!(limits.len(), 2); - assert_eq!(limits[0].window, nocterm_ai::usage::Window::FiveHour); - let thread = f.panel.read(cx).current().unwrap(); - assert_eq!( - thread.read(cx).transcript().tokens.as_ref().unwrap().total, - 30_000 - ); - }); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - window.click("agent-context-usage", cx); - window.render_frame(cx); - let card = window.find("agent-usage").bounds(); - let composer = window.find("agent-composer").bounds(); - // Over the chat, above the composer. - assert!(card.bottom() <= composer.origin.y + gpui_kit::px(1.)); - window.click("agent-usage-close", cx); - window.render_frame(cx); - assert!(window.try_find("agent-usage").is_none()); - }) - .unwrap(); -} +mod context_usage; #[gpui_kit::test] fn attach_menu_lists_servers_under_their_folders(cx: &mut TestAppContext) { @@ -152,7 +80,6 @@ fn image_preparation_runs_once_and_renders_a_cached_small_static_preview(cx: &mu let f = fixture_with_width( cx, 26., - usage_source(), workspace_resolver(), crate::ImageDeps::new(codec.clone(), codec.clone()), ); @@ -227,7 +154,7 @@ fn agent_popup_is_anchored_to_header_and_dismisses(cx: &mut TestAppContext) { #[gpui_kit::test] fn composer_popups_select_config_modes_and_context(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); f.commands.config.set_options(vec![ConfigOption::select( "model", "Model", @@ -367,7 +294,7 @@ fn transcript_activity_tracks_only_live_reasoning_and_unfinished_tools() { fn compact_chips_and_live_reasoning_respect_narrow_layout_and_reduced_motion( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); thread.update(cx, |thread, cx| thread.send("Question".into(), cx)); @@ -445,7 +372,7 @@ fn compact_chips_and_live_reasoning_respect_narrow_layout_and_reduced_motion( #[gpui_kit::test] fn expanded_long_mcp_command_json_and_reasoning_stay_within_narrow_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let long = "very_long_unbroken_argument_".repeat(300); cx.update(|cx| { @@ -514,52 +441,30 @@ fn expanded_long_mcp_command_json_and_reasoning_stay_within_narrow_panel(cx: &mu } #[gpui_kit::test] -fn prompt_completion_refreshes_its_original_usage_source_after_another_installation( - cx: &mut TestAppContext, -) { - let source = usage_source(); - source.push(Ok(Some(vec![nocterm_ai::usage::RateLimit { - window: nocterm_ai::usage::Window::Weekly, - used: Some(0.38), - resets_at: None, - reached: false, - }]))); - let first = fixture_with_width(cx, 26., source.clone(), workspace_resolver(), image_deps()); - new_chat(&first, cx); - let thread = cx.update(|cx| first.panel.read(cx).current().unwrap()); - thread.update(cx, |thread, cx| thread.send("original prompt".into(), cx)); - cx.run_until_parked(); - let finish = first.commands.pending.lock().unwrap().take().unwrap(); - let other_source = usage_source(); - let second = fixture_with_width( - cx, - 26., - other_source.clone(), - workspace_resolver(), - image_deps(), - ); - finish - .send(PromptOutcome::new(StopReason::EndTurn)) - .unwrap(); +fn a_file_of_any_kind_goes_with_the_message_as_a_link_and_shows_as_a_chip(cx: &mut TestAppContext) { + let f = fixture(cx); + lazy_start(&f, cx); + new_chat(&f, cx); + let file = f._directory.path().join("report.pdf"); + std::fs::write(&file, b"%PDF").unwrap(); + cx.update(|cx| { + f.panel + .update(cx, |panel, cx| panel.attach_paths(vec![file.clone()], cx)) + }); cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); - assert!(other_source.requests().is_empty()); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find(("file-chip", 0usize)).is_some()); + }) + .unwrap(); + exchange(&f, "read it", "ok", cx); + let prompt = f.commands.prompts.lock().unwrap()[0].clone(); + assert!(prompt.content.iter().any(|part| matches!( + part, + Content::ResourceReference(link) if link.name == "report.pdf" && link.uri.starts_with("file://") + ))); cx.update(|cx| { - assert!( - first - .installation - .installed - .runtime() - .limits("codex", cx) - .is_some() - ); - assert!( - second - .installation - .installed - .runtime() - .limits("codex", cx) - .is_none() - ); + let thread = f.panel.read(cx).current().unwrap(); + assert!(thread.read(cx).composer().files().is_empty()); }); } diff --git a/crates/agent/src/panel/tests/composer/context_usage.rs b/crates/agent/src/panel/tests/composer/context_usage.rs new file mode 100644 index 00000000..81f3b502 --- /dev/null +++ b/crates/agent/src/panel/tests/composer/context_usage.rs @@ -0,0 +1,175 @@ +//! Existing context and plan usage presentation. +use super::*; + +#[gpui_kit::test] +fn usage_card_shows_context_tokens_and_plan_limits(cx: &mut TestAppContext) { + let f = fixture(cx); + panel_actions::new_agent_chat(&f, "claude", cx); + let session = cx.update(|cx| { + let thread = f.panel.read(cx).current().unwrap(); + thread.update(cx, |thread, cx| thread.send("hello".into(), cx)); + thread.read(cx).session().clone().unwrap() + }); + cx.run_until_parked(); + for (kind, used) in [("seven_day", 0.38), ("five_hour", 0.12)] { + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session.clone(), + SessionUpdate::UsageUpdate(plan_usage(kind, used)), + ))) + .unwrap(); + } + cx.run_until_parked(); + let finish = f.commands.pending.lock().unwrap().take().unwrap(); + finish + .send(PromptOutcome { + usage: Some(TokenTotals { + cache_read: Some(20_000), + ..TokenTotals::new(30_000, 25_000, 5_000) + }), + ..PromptOutcome::new(StopReason::EndTurn) + }) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + let runtime = f.installation.installed.runtime(); + let limits: Vec<_> = runtime + .limits("claude", cx) + .unwrap() + .iter() + .cloned() + .collect(); + assert_eq!(limits.len(), 2); + assert_eq!(limits[0].window, nocterm_ai::usage::Window::FiveHour); + let thread = f.panel.read(cx).current().unwrap(); + assert_eq!( + thread.read(cx).transcript().tokens.as_ref().unwrap().total, + 30_000 + ); + }); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + window.click("agent-context-usage", cx); + window.render_frame(cx); + let card = window.find("agent-usage").bounds(); + let composer = window.find("agent-composer").bounds(); + // Over the chat, above the composer. + assert!(card.bottom() <= composer.origin.y + gpui_kit::px(1.)); + window.click("agent-usage-close", cx); + window.render_frame(cx); + assert!(window.try_find("agent-usage").is_none()); + }) + .unwrap(); +} + +#[gpui_kit::test] +fn context_tokens_and_cost_remain_available_without_plan_limits(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("hello".into(), cx)); + cx.run_until_parked(); + let session = thread.read_with(cx, |thread, _| thread.session().clone().unwrap()); + let mut usage = ContextUsage::new(50, 100); + usage.cost = Some(nocterm_ai::model::usage::Cost { + amount: 1.25, + currency: "USD".into(), + }); + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session, + SessionUpdate::UsageUpdate(usage.clone()), + ))) + .unwrap(); + cx.run_until_parked(); + f.commands + .pending + .lock() + .unwrap() + .take() + .unwrap() + .send(PromptOutcome { + usage: Some(TokenTotals::new(30, 20, 10)), + ..PromptOutcome::new(StopReason::EndTurn) + }) + .unwrap(); + cx.run_until_parked(); + thread.read_with(cx, |thread, _| { + assert_eq!(thread.transcript().usage, Some(usage)); + assert_eq!( + thread.transcript().tokens, + Some(TokenTotals::new(30, 20, 10)) + ); + }); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + window.click("agent-context-usage", cx); + window.render_frame(cx); + assert!(window.try_find("agent-usage").is_some()); + assert!( + f.installation + .installed + .runtime() + .limits("codex", cx) + .is_none() + ); + }) + .unwrap(); +} + +fn plan_usage(kind: &str, used: f64) -> ContextUsage { + let meta = serde_json::json!({"_claude/rateLimit": { + "rateLimitType": kind, + "utilization": used, + "resetsAt": 1_791_588_105u64, + "status": "allowed" + }}); + ContextUsage::new(25_900, 256_000).meta( + meta.as_object() + .cloned() + .map(nocterm_ai::model::extensions::Extensions::from), + ) +} + +#[gpui_kit::test] +fn received_plan_limits_stay_with_their_original_installation_after_prompt_completion( + cx: &mut TestAppContext, +) { + let first = fixture(cx); + panel_actions::new_agent_chat(&first, "claude", cx); + let thread = cx.update(|cx| first.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("original prompt".into(), cx)); + cx.run_until_parked(); + let finish = first.commands.pending.lock().unwrap().take().unwrap(); + let second = fixture(cx); + panel_actions::new_agent_chat(&second, "claude", cx); + let session = thread.read_with(cx, |thread, _| thread.session().clone().unwrap()); + first + .events + .try_send(AgentEvent::Session(SessionNotice::new( + session, + SessionUpdate::UsageUpdate(plan_usage("seven_day", 0.38)), + ))) + .unwrap(); + finish + .send(PromptOutcome::new(StopReason::EndTurn)) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + let limits = first + .installation + .installed + .runtime() + .limits("claude", cx) + .unwrap(); + assert_eq!(limits.iter().next().unwrap().used, Some(0.38)); + assert!( + second + .installation + .installed + .runtime() + .limits("claude", cx) + .is_none() + ); + }); +} diff --git a/crates/agent/src/panel/tests/drafts.rs b/crates/agent/src/panel/tests/drafts.rs index fe144607..470bbcfd 100644 --- a/crates/agent/src/panel/tests/drafts.rs +++ b/crates/agent/src/panel/tests/drafts.rs @@ -31,7 +31,7 @@ pub(super) fn disk_value( // tests separately own byte framing and authoritative-envelope validation. let chat = repository.transcript(id).unwrap(); let mut value = serde_json::to_value(chat).unwrap(); - if value["version"] == 2 { + if matches!(value["version"].as_u64(), Some(2 | 3)) { value["chat"].take() } else { value @@ -333,7 +333,12 @@ fn sending_a_large_draft_counts_its_text_once_and_rejection_keeps_it(cx: &mut Te // A second payload must fail without removing its composer draft. let next = "y".repeat(17 * 1024 * 1024); thread.set_draft(next.clone(), cx); - thread.begin_composer_edit(Vec::new(), vec![Attachment::Group("editing".into())], cx); + thread.begin_composer_edit( + Vec::new(), + Vec::new(), + vec![Attachment::Group("editing".into())], + cx, + ); let next_id = thread.composer().next_queue_id(); let queued = thread.composer().queue()[0].saved().clone(); let entries = thread.history_entries().len(); diff --git a/crates/agent/src/panel/tests/execution.rs b/crates/agent/src/panel/tests/execution.rs index ead80a30..36d27708 100644 --- a/crates/agent/src/panel/tests/execution.rs +++ b/crates/agent/src/panel/tests/execution.rs @@ -3,40 +3,33 @@ use super::*; use crate::thread::AgentThread; use nocterm_ai::model::auth::PermissionOutcome; use nocterm_ai::{CancelCommand, ExecCommand, ReadCommand, TerminalCall}; -use nocterm_session::{ExecExit, ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec}; +use nocterm_session::{ExecFuture, ExecOutput, ExecRequest, HostExec}; use serde_json::Value; use std::time::Duration; -#[derive(Default)] struct Programs { - requests: Mutex>, - sinks: Mutex>>>, + jobs: Arc, + offset: usize, } - impl HostExec for Programs { - fn exec(&self, request: ExecRequest) -> ExecFuture { - self.requests.lock().unwrap().push(request); - let (sink, output) = ExecOutput::channel(); - self.sinks.lock().unwrap().push(Some(Arc::new(sink))); - async move { Ok(output) }.boxed() + fn exec(&self, _: ExecRequest) -> ExecFuture { + panic!("the consumer's scripted command port must not perform transport I/O") } } - impl Programs { + fn new(jobs: Arc) -> Self { + let offset = jobs.requests().len(); + Self { jobs, offset } + } + fn requests(&self) -> Vec { + self.jobs.requests()[self.offset..].to_vec() + } fn closed(&self, index: usize) -> bool { - self.sinks.lock().unwrap()[index] - .as_ref() - .unwrap() - .is_closed() + !self.jobs.snapshot(self.offset + index).active() } - fn finish(&self, index: usize, stdout: &[u8], status: u32, stderr: &str) { - let sink = self.sinks.lock().unwrap()[index].take().unwrap(); - assert!(futures::executor::block_on(sink.send(stdout.to_vec()))); - sink.finish(ExecExit { - status: Some(status), - stderr: stderr.into(), - }); + self.jobs + .finish(self.offset + index, stdout, status, stderr); } } @@ -108,7 +101,7 @@ fn request(terminal_id: &str) -> ExecCommand { fn setup(cx: &mut TestAppContext) -> (Fixture, Entity, Arc, String) { let f = fixture(cx); - let programs = Arc::new(Programs::default()); + let programs = Arc::new(Programs::new(f._directory.jobs.clone())); *f.access.executor().borrow_mut() = Some(programs.clone()); new_chat(&f, cx); let thread = current(&f, cx); @@ -134,6 +127,12 @@ fn start( matches!(payload["state"].as_str(), Some("starting" | "running")), "{payload}" ); + let jobs = &f._directory.jobs; + let expected = f.access.executor().borrow().as_ref().unwrap().clone(); + assert!(Arc::ptr_eq( + &jobs.executor(jobs.requests().len() - 1), + &expected + )); payload["command_id"].as_str().unwrap().into() } @@ -158,17 +157,16 @@ fn read( } #[gpui_kit::test] -fn structured_requests_preserve_arguments_and_report_real_exit_with_redaction( +fn structured_requests_preserve_arguments_and_present_port_outcomes_with_redaction( cx: &mut TestAppContext, ) { let (f, thread, programs, terminal) = setup(cx); let id = start(&f, &thread, &terminal, cx); - let requests = programs.requests.lock().unwrap(); + let requests = programs.requests(); assert_eq!(requests.len(), 1); assert_eq!(requests[0].program, request(&terminal).program); assert_eq!(requests[0].args, request(&terminal).args); assert_eq!(requests[0].stdin.as_deref(), Some(&b"first\nsecond\n"[..])); - drop(requests); programs.finish( 0, b"output\nDB_PASSWORD=hunter2\n", @@ -223,7 +221,10 @@ fn a_silent_program_and_an_abandoned_read_remain_running_until_cancelled(cx: &mu .unwrap(); assert_eq!(cancelled["state"], "cancelled"); cx.run_until_parked(); - assert!(programs.closed(0), "cancelling drops the transport output"); + assert!( + programs.closed(0), + "cancelling reaches the original command port" + ); assert_eq!( read(&f, &thread, &terminal, &id, cx).unwrap()["state"], "cancelled" @@ -256,7 +257,7 @@ fn approval_rechecks_authentication_and_unsupported_execution_never_falls_back( .message() .contains("authentication") ); - assert!(programs.requests.lock().unwrap().is_empty()); + assert!(programs.requests().is_empty()); f.access.sign_in().asks().set(false); f.access.executor().borrow_mut().take(); let unsupported = result(submit( @@ -270,7 +271,7 @@ fn approval_rechecks_authentication_and_unsupported_execution_never_falls_back( unsupported.unwrap_err().message(), "This terminal does not support structured execution." ); - assert!(programs.requests.lock().unwrap().is_empty()); + assert!(programs.requests().is_empty()); assert!(f.access.sent().borrow().is_empty()); } @@ -313,7 +314,7 @@ fn active_command_limit_and_cancelled_slot_apply_through_bridge_calls(cx: &mut T fifth.unwrap_err().message(), "This chat already has four active commands. Read or cancel one first." ); - assert_eq!(programs.requests.lock().unwrap().len(), 4); + assert_eq!(programs.requests().len(), 4); result(submit( &f, &thread, @@ -327,7 +328,7 @@ fn active_command_limit_and_cancelled_slot_apply_through_bridge_calls(cx: &mut T .unwrap(); let next = start(&f, &thread, &terminal, cx); assert!(!ids.contains(&next)); - assert_eq!(programs.requests.lock().unwrap().len(), 5); + assert_eq!(programs.requests().len(), 5); assert!(programs.closed(0)); } @@ -428,7 +429,7 @@ fn removing_the_active_attachment_cancels_owned_programs(cx: &mut TestAppContext fn replacing_a_session_executor_revokes_old_commands_and_allows_new_ones(cx: &mut TestAppContext) { let (f, thread, programs, terminal) = setup(cx); let old = start(&f, &thread, &terminal, cx); - let replacement = Arc::new(Programs::default()); + let replacement = Arc::new(Programs::new(f._directory.jobs.clone())); *f.access.executor().borrow_mut() = Some(replacement.clone()); cx.executor().advance_clock(Duration::from_millis(150)); cx.run_until_parked(); @@ -439,7 +440,7 @@ fn replacing_a_session_executor_revokes_old_commands_and_allows_new_ones(cx: &mu ); let new = start(&f, &thread, &terminal, cx); assert_ne!(new, old); - assert_eq!(replacement.requests.lock().unwrap().len(), 1); + assert_eq!(replacement.requests().len(), 1); assert!(!replacement.closed(0)); } @@ -460,7 +461,7 @@ fn disabling_ai_cancels_execution_and_revokes_chat_access(cx: &mut TestAppContex } #[gpui_kit::test] -fn deleting_and_releasing_a_chat_drops_its_program_output(cx: &mut TestAppContext) { +fn deleting_and_releasing_a_chat_cancels_its_command_port(cx: &mut TestAppContext) { let (f, thread, programs, terminal) = setup(cx); start(&f, &thread, &terminal, cx); let id = thread.entity_id(); @@ -529,10 +530,13 @@ fn a_joined_reconnect_command_does_not_hold_input_until_its_process_exits(cx: &m )); assert!(result(input).is_ok()); assert_eq!(*f.access.sent().borrow(), ["following input\r"]); - assert_eq!(programs.requests.lock().unwrap().len(), 1); + assert_eq!(programs.requests().len(), 1); assert!(!programs.closed(0), "the first command is still running"); programs.finish(0, b"finished", 0, ""); cx.run_until_parked(); } +mod file_tools; +mod file_transfers; +mod local_environments; mod screenshot_history; diff --git a/crates/agent/src/panel/tests/execution/file_tools.rs b/crates/agent/src/panel/tests/execution/file_tools.rs new file mode 100644 index 00000000..71d34d85 --- /dev/null +++ b/crates/agent/src/panel/tests/execution/file_tools.rs @@ -0,0 +1,130 @@ +//! File tools: offered while an attached terminal serves files but runs no programs, reading +//! freely and changing files only with approval. +use super::*; +use nocterm_ai::{EditFile, ReadFile, ToolOffer}; +use nocterm_session::{HostAddress, Target, testing::InMemoryFs}; + +fn panel_host() -> HostAddress { + HostAddress::direct(Target { + host: "Game panel".into(), + port: 22, + user: "lobby".into(), + }) +} + +/// A chat whose attached terminal serves `fs` and runs no programs, as a plugin's console tab. +fn console(cx: &mut TestAppContext) -> (Fixture, Entity, Arc, String) { + let f = fixture(cx); + let fs = f.access.serve_remote_files(panel_host()); + fs.seed_file("/server.properties", "motd=Hi\nmax-players=20\n") + .unwrap(); + new_chat(&f, cx); + let thread = current(&f, cx); + let id = terminal_id(&thread, cx); + (f, thread, fs, id) +} + +fn last_offer(f: &Fixture, thread: &Entity, cx: &mut TestAppContext) -> ToolOffer { + let id = cx.update(|cx| thread.read(cx).registration().as_ref().unwrap().id); + f.bridge + .offers + .lock() + .unwrap() + .iter() + .rev() + .find(|(registration, _)| *registration == id) + .map(|(_, offer)| *offer) + .expect("the chat offered its tools") +} + +fn read(id: &str) -> TerminalCall { + TerminalCall::ReadFile(ReadFile { + terminal_id: id.into(), + path: "/server.properties".into(), + start_line: None, + max_lines: None, + }) +} + +fn edit(id: &str, old: &str, new: &str) -> TerminalCall { + TerminalCall::EditFile(EditFile { + terminal_id: id.into(), + path: "/server.properties".into(), + old_text: old.into(), + new_text: new.into(), + replace_all: false, + }) +} + +fn text(fs: &InMemoryFs) -> String { + String::from_utf8(fs.bytes("/server.properties").unwrap()).unwrap() +} + +#[gpui_kit::test] +fn file_tools_are_offered_only_while_such_a_terminal_is_attached(cx: &mut TestAppContext) { + let (f, thread, _, _) = console(cx); + assert_eq!(last_offer(&f, &thread, cx), ToolOffer::new(true)); + let context = cx.update(|cx| thread.update(cx, |thread, cx| thread.context(cx))); + assert!(context.contains(r#""file_tools":true"#), "{context}"); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread.attach(Attachment::Terminal(f.terminal), cx); + }) + }); + assert_eq!( + last_offer(&f, &thread, cx), + ToolOffer::new(false), + "detaching the terminal withdraws the file tools" + ); +} + +#[gpui_kit::test] +fn a_terminal_that_runs_programs_gets_no_file_tools(cx: &mut TestAppContext) { + let f = fixture(cx); + let fs = f.access.serve_remote_files(panel_host()); + fs.seed_file("/server.properties", "motd=Hi\n").unwrap(); + *f.access.executor().borrow_mut() = Some(Arc::new(Programs::new(f._directory.jobs.clone()))); + new_chat(&f, cx); + let thread = current(&f, cx); + assert_eq!(last_offer(&f, &thread, cx), ToolOffer::new(false)); + let id = terminal_id(&thread, cx); + let refused = result(submit(&f, &thread, read(&id), true, cx)).unwrap_err(); + assert!(refused.message().contains("file_tools: true"), "{refused}"); +} + +#[gpui_kit::test] +fn an_agent_reads_freely_and_changes_a_file_after_approval(cx: &mut TestAppContext) { + let (f, thread, fs, id) = console(cx); + let content = result(submit(&f, &thread, read(&id), false, cx)).unwrap(); + assert_eq!(content["content"], "motd=Hi\nmax-players=20\n"); + + let pending = submit( + &f, + &thread, + edit(&id, "max-players=20", "max-players=40"), + false, + cx, + ); + assert_eq!( + text(&fs), + "motd=Hi\nmax-players=20\n", + "nothing changes before the card is answered" + ); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, true, true, cx); + }) + }); + cx.run_until_parked(); + assert_eq!(result(pending).unwrap()["replacements"], 1); + assert_eq!(text(&fs), "motd=Hi\nmax-players=40\n"); + + let granted = submit(&f, &thread, edit(&id, "motd=Hi", "motd=Welcome"), false, cx); + assert!( + cx.update(|cx| thread.read(cx).tool_cards().is_empty()), + "the grant covers later changes on this terminal" + ); + result(granted).unwrap(); + assert_eq!(text(&fs), "motd=Welcome\nmax-players=40\n"); +} diff --git a/crates/agent/src/panel/tests/execution/file_transfers.rs b/crates/agent/src/panel/tests/execution/file_transfers.rs new file mode 100644 index 00000000..3c3f70bd --- /dev/null +++ b/crates/agent/src/panel/tests/execution/file_transfers.rs @@ -0,0 +1,333 @@ +//! Agent file transfers reach the transfer port only through approved, session-bound endpoints. +use super::*; +use gpui_kit::EntityId; +use nocterm_ai::TransferFiles; +use nocterm_session::{HostAddress, Target, testing::InMemoryFs}; +use nocterm_transfers::{ + CollisionPolicy, Request, TransferEndpoint, TransferId, TransferService, TransferState, +}; + +fn host(name: &str) -> HostAddress { + HostAddress::direct(Target { + host: name.into(), + port: 22, + user: "user".into(), + }) +} + +/// A background terminal served by its own remote host, for the chat to attach. +fn add_terminal(f: &Fixture, host_name: &str, cx: &mut TestAppContext) -> (Rc, EntityId) { + let access = Access::new(None, Rc::default()); + access.serve_remote_files(host(host_name)); + let item = cx + .update_window(f.handle, |_, window, cx| { + let terminal = cx.new(|cx| FakeTerminal::new(access.clone(), cx)); + f.workspace.update(cx, |workspace, cx| { + workspace.add_background_item(terminal, window, cx) + }) + }) + .unwrap(); + (access, item) +} +fn id_of(thread: &Entity, item: EntityId, cx: &mut TestAppContext) -> String { + cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread + .resolved(cx) + .into_iter() + .find(|(_, entry, _)| entry.item == item) + .map(|(id, _, _)| id) + .expect("the terminal is attached") + }) + }) +} + +struct Pair { + f: Fixture, + thread: Entity, + source: (Rc, String), + destination: (Rc, String), + third: String, +} +fn pair(cx: &mut TestAppContext) -> Pair { + let f = fixture(cx); + f.access.serve_remote_files(host("source")); + let (destination, destination_item) = add_terminal(&f, "destination", cx); + let (_, third_item) = add_terminal(&f, "third", cx); + new_chat(&f, cx); + let thread = current(&f, cx); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread.attach(Attachment::Terminal(destination_item), cx); + thread.attach(Attachment::Terminal(third_item), cx); + }) + }); + let source = terminal_id(&thread, cx); + Pair { + source: (f.access.clone(), source), + destination: (destination, id_of(&thread, destination_item, cx)), + third: id_of(&thread, third_item, cx), + f, + thread, + } +} + +fn transfer(pair: &Pair, destination: &str) -> TerminalCall { + transfer_between(&pair.source.1, destination) +} +fn transfer_between(source: &str, destination: &str) -> TerminalCall { + TerminalCall::TransferFiles(TransferFiles { + source_terminal_id: source.into(), + sources: vec!["/srv/a".into(), "/srv/dir".into()], + destination_terminal_id: destination.into(), + destination: "/srv/in".into(), + overwrite: true, + yield_ms: Some(0), + }) +} +fn read_transfer( + pair: &Pair, + id: &str, + cx: &mut TestAppContext, +) -> Result { + let call = TerminalCall::ReadTransfer(nocterm_ai::ReadTransfer { + transfer_id: id.into(), + yield_ms: Some(0), + }); + result(submit(&pair.f, &pair.thread, call, true, cx)) +} +fn submitted(pair: &Pair, payload: &Value) -> Request { + let id: TransferId = payload["transfer_id"].as_str().unwrap().parse().unwrap(); + pair.f._directory.transfers.request(id).unwrap() +} +fn states(pair: &Pair) -> Vec { + pair.f + ._directory + .transfers + .snapshot() + .into_iter() + .map(|progress| progress.state) + .collect() +} +fn decide(pair: &Pair, allow: bool, grant: bool, cx: &mut TestAppContext) { + cx.update(|cx| { + pair.thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, allow, grant, cx); + }) + }); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn an_approved_transfer_submits_a_relay_between_the_captured_endpoints(cx: &mut TestAppContext) { + let p = pair(cx); + let payload = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + assert_eq!(payload["state"], "queued"); + assert_eq!(payload["direction"], "relay"); + let Request::Relay(relay) = submitted(&p, &payload) else { + panic!("a remote to remote transfer is a relay"); + }; + assert_eq!(relay.sources, ["/srv/a", "/srv/dir"]); + assert_eq!(relay.destination, "/srv/in"); + assert_eq!(relay.source_host, host("source")); + assert_eq!(relay.host, host("destination")); + assert_eq!(relay.collisions, CollisionPolicy::Replace); +} + +#[gpui_kit::test] +fn a_local_source_uploads_and_a_denied_card_submits_nothing(cx: &mut TestAppContext) { + let p = pair(cx); + p.source.0.serve_local_files(); + let denied = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + assert!( + states(&p).is_empty(), + "nothing runs before the card is answered" + ); + decide(&p, false, false, cx); + assert!(result(denied).unwrap_err().to_string().contains("denied")); + assert!(states(&p).is_empty()); + let payload = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + assert!(matches!(submitted(&p, &payload), Request::Upload(_))); +} + +#[gpui_kit::test] +fn one_grant_covers_both_terminals_but_not_a_new_endpoint(cx: &mut TestAppContext) { + let p = pair(cx); + let third = p.third.clone(); + let first = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + decide(&p, true, true, cx); + assert!(result(first).is_ok()); + let repeated = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + assert!( + cx.update(|cx| p.thread.read(cx).tool_cards().is_empty()), + "the granted pair needs no card" + ); + assert!(result(repeated).is_ok()); + let _pending = submit(&p.f, &p.thread, transfer(&p, &third), false, cx); + assert_eq!(cx.update(|cx| p.thread.read(cx).tool_cards().len()), 1); +} + +#[gpui_kit::test] +fn grants_for_two_pairs_do_not_approve_a_transfer_across_them(cx: &mut TestAppContext) { + let p = pair(cx); + let (source, destination, third) = + (p.source.1.clone(), p.destination.1.clone(), p.third.clone()); + let first = submit( + &p.f, + &p.thread, + transfer_between(&source, &destination), + false, + cx, + ); + decide(&p, true, true, cx); + assert!(result(first).is_ok()); + let second = submit( + &p.f, + &p.thread, + transfer_between(&destination, &third), + false, + cx, + ); + assert_eq!( + cx.update(|cx| p.thread.read(cx).tool_cards().len()), + 1, + "the first pair's grant does not cover the destination's new partner" + ); + decide(&p, true, true, cx); + assert!(result(second).is_ok()); + let crossing = submit( + &p.f, + &p.thread, + transfer_between(&source, &third), + false, + cx, + ); + assert_eq!( + cx.update(|cx| p.thread.read(cx).tool_cards().len()), + 1, + "each terminal holds a grant, but never together" + ); + decide(&p, false, false, cx); + assert!(result(crossing).is_err()); + let reverse = submit( + &p.f, + &p.thread, + transfer_between(&third, &destination), + false, + cx, + ); + assert!(cx.update(|cx| p.thread.read(cx).tool_cards().is_empty())); + assert!(result(reverse).is_ok()); +} + +#[gpui_kit::test] +fn a_source_reconnecting_between_card_and_approval_detaches_the_call(cx: &mut TestAppContext) { + let p = pair(cx); + let reply = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + p.source.0.replace_connection(); + decide(&p, true, false, cx); + assert!(result(reply).unwrap_err().to_string().contains("detached")); + assert!(states(&p).is_empty()); +} + +#[gpui_kit::test] +fn a_chat_reads_only_its_own_transfers(cx: &mut TestAppContext) { + let p = pair(cx); + let endpoint = |name: &str| TransferEndpoint::Remote { + host: host(name), + fs: Arc::new(InMemoryFs::default()), + }; + let foreign = nocterm_transfers::TransferRequests::submit( + &*p.f._directory.transfers, + Request::between( + endpoint("a"), + vec!["/x".into()], + endpoint("b"), + "/y".into(), + CollisionPolicy::Skip, + ) + .unwrap(), + ) + .unwrap(); + let refused = read_transfer(&p, &foreign.to_string(), cx).unwrap_err(); + assert!(refused.to_string().contains("not started by this chat")); + let own = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + let id = own["transfer_id"].as_str().unwrap(); + assert_eq!(read_transfer(&p, id, cx).unwrap()["state"], "queued"); +} + +#[gpui_kit::test] +fn stopping_the_chat_cancels_its_active_transfers(cx: &mut TestAppContext) { + let p = pair(cx); + let _ = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + assert_eq!(states(&p), [TransferState::Queued]); + cx.update(|cx| p.thread.update(cx, |thread, cx| thread.stop(cx))); + cx.run_until_parked(); + assert_eq!(states(&p), [TransferState::Cancelled]); +} + +#[gpui_kit::test] +fn replacing_an_endpoint_session_cancels_the_transfer(cx: &mut TestAppContext) { + let p = pair(cx); + let _ = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + p.source.0.replace_connection(); + cx.update(|cx| { + p.thread + .update(cx, |thread, cx| thread.revoke_executions(cx)) + }); + assert_eq!(states(&p), [TransferState::Cancelled]); +} + +#[gpui_kit::test] +fn cancel_transfer_stops_an_own_transfer_and_reports_it(cx: &mut TestAppContext) { + let p = pair(cx); + let own = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + let call = TerminalCall::CancelTransfer(nocterm_ai::CancelTransfer { + transfer_id: own["transfer_id"].as_str().unwrap().into(), + }); + let report = result(submit(&p.f, &p.thread, call, true, cx)).unwrap(); + assert_eq!(report["state"], "cancelled"); +} diff --git a/crates/agent/src/panel/tests/execution/local_environments.rs b/crates/agent/src/panel/tests/execution/local_environments.rs new file mode 100644 index 00000000..a198b7fb --- /dev/null +++ b/crates/agent/src/panel/tests/execution/local_environments.rs @@ -0,0 +1,243 @@ +//! Original ACP callbacks cannot consume credentials belonging to a later account. +use super::*; +use nocterm_hubs_model::{ + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, +}; +fn account(name: &str) -> LocalAccountPartition { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("hub").unwrap(), + Fingerprint::from_bytes([1; 32]), + ), + AccountIdentity::new( + NamespaceId::new(name).unwrap(), + Fingerprint::from_bytes([2; 32]), + ), + )) +} +fn account_fixture(factory: Arc, cx: &mut TestAppContext) -> Fixture { + let mut f = fixture(cx); + let installed = f + .installation + .installed + .with_account_source(factory.clone()); + if let Ok(runtime) = installed.capture_runtime() { + f.access.bind_source(runtime.captured_source().unwrap()); + } + let (handle, workspace, panel, terminal) = cx.update(|cx| { + open_host( + &f._directory, + &installed, + &f.installation.layout, + &f.access, + cx, + ) + }); + f.installation.installed = installed; + f.handle = handle; + f.workspace = workspace; + f.panel = panel; + f.terminal = terminal; + f +} +#[gpui_kit::test] +fn locked_account_host_can_capture_after_unlock_without_rebuilding_its_window( + cx: &mut TestAppContext, +) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + let f = account_fixture(factory.clone(), cx); + cx.update(|cx| assert!(f.panel.read(cx).current().is_none())); + assert!(f.installation.installed.capture_runtime().is_err()); + factory.sign_in(account("alice")); + cx.update(|cx| { + f.installation + .installed + .runtime() + .refresh_environment_sources(cx) + }); + cx.run_until_parked(); + new_chat(&f, cx); + let original = current(&f, cx); + let original_key = cx.update(|cx| original.read(cx).document().key().clone()); + assert!(!original_key.environment().is_this_computer()); + factory.sign_in(account("bob")); + cx.update(|cx| { + f.installation + .installed + .runtime() + .refresh_environment_sources(cx) + }); + cx.run_until_parked(); + new_chat(&f, cx); + let replacement = current(&f, cx); + let replacement_key = cx.update(|cx| replacement.read(cx).document().key().clone()); + assert_ne!(replacement_key.environment(), original_key.environment()); + cx.update(|cx| { + assert!(!original.read(cx).source_current()); + assert!(replacement.read(cx).source_current()); + }); +} +fn refresh_sources(f: &Fixture, cx: &mut TestAppContext) { + cx.update(|cx| { + f.installation + .installed + .runtime() + .refresh_environment_sources(cx) + }); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| window.render_frame(cx)) + .unwrap(); +} +fn shown(f: &Fixture, id: &'static str, cx: &mut TestAppContext) -> bool { + cx.update_window(f.handle, |_, window, _| window.try_find(id).is_some()) + .unwrap() +} +#[gpui_kit::test] +fn a_locked_hub_account_shows_an_unlock_notice_and_clears_it_after_unlock(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + let f = account_fixture(factory.clone(), cx); + refresh_sources(&f, cx); + assert!(shown(&f, "agent-account-notice", cx)); + assert!( + !shown(&f, "agent-error", cx), + "locking is not an error line" + ); + + factory.sign_in(account("alice")); + refresh_sources(&f, cx); + assert!(!shown(&f, "agent-account-notice", cx)); + assert!(!shown(&f, "agent-error", cx)); +} +#[gpui_kit::test] +fn locking_after_capture_shows_the_notice_again(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + refresh_sources(&f, cx); + assert!(!shown(&f, "agent-account-notice", cx)); + + factory.close(); + refresh_sources(&f, cx); + assert!(shown(&f, "agent-account-notice", cx)); + assert!(!shown(&f, "agent-error", cx)); +} +#[gpui_kit::test] +fn the_unlock_button_asks_to_open_the_unlock_dialog(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + let f = account_fixture(factory, cx); + refresh_sources(&f, cx); + let unlocks = std::rc::Rc::new(std::cell::Cell::new(0)); + let counter = unlocks.clone(); + cx.update_window(f.handle, |_, window, cx| { + cx.on_action(move |_: &nocterm_workspace::UnlockVault, _| counter.set(counter.get() + 1)); + window.click("agent-account-action", cx); + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(unlocks.get(), 1); +} +#[gpui_kit::test] +fn a_signed_out_hub_account_shows_a_notice_with_a_sign_in_button(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + factory.sign_out(); + refresh_sources(&f, cx); + assert!(shown(&f, "agent-account-notice", cx)); + assert!(shown(&f, "agent-account-action", cx)); + assert!(!shown(&f, "agent-error", cx)); +} +#[gpui_kit::test] +fn old_approval_cannot_send_input_after_synchronous_account_replacement(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + new_chat(&f, cx); + let thread = current(&f, cx); + let terminal = terminal_id(&thread, cx); + let reply = submit( + &f, + &thread, + TerminalCall::SendInput(nocterm_ai::SendInput { + terminal_id: terminal, + text: "must not reach B".into(), + press_enter: true, + }), + false, + cx, + ); + factory.sign_in(account("bob")); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, true, false, cx); + }) + }); + cx.run_until_parked(); + assert!(result(reply).is_err()); + assert!(f.access.sent().borrow().is_empty()); + assert_eq!(f.access.link().reconnects().get(), 0); +} +#[gpui_kit::test] +fn exec_accepted_before_replacement_is_denied_before_its_first_native_poll( + cx: &mut TestAppContext, +) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + let programs = Arc::new(Programs::new(f._directory.jobs.clone())); + *f.access.executor().borrow_mut() = Some(programs.clone()); + new_chat(&f, cx); + let thread = current(&f, cx); + let terminal = terminal_id(&thread, cx); + let _reply = submit( + &f, + &thread, + TerminalCall::ExecCommand(request(&terminal)), + false, + cx, + ); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, true, false, cx); + // No background executor turn occurs between admission and revocation. + factory.sign_in(account("bob")); + }) + }); + cx.run_until_parked(); + assert_eq!( + programs.requests().len(), + 1, + "the job was admitted before replacement" + ); + assert_eq!( + f._directory.jobs.snapshot(0).state(), + nocterm_session::command_jobs::CommandState::Starting, + "the owned runner never reached its first transport poll" + ); + cx.background_executor + .advance_clock(Duration::from_millis(100)); + cx.run_until_parked(); + assert_eq!( + f._directory.jobs.snapshot(0).state(), + nocterm_session::command_jobs::CommandState::Cancelled + ); + assert!(f.access.sent().borrow().is_empty()); +} +#[gpui_kit::test] +fn a_message_in_a_hub_account_chat_reaches_the_agent(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + refresh_sources(&f, cx); + new_chat(&f, cx); + let thread = current(&f, cx); + cx.update(|cx| thread.update(cx, |thread, cx| thread.send("hello".into(), cx))); + cx.run_until_parked(); + assert!(f.commands.pending.lock().unwrap().is_some()); + cx.update(|cx| assert!(!thread.read(cx).queue_dispatchable())); +} diff --git a/crates/agent/src/panel/tests/execution/screenshot_history.rs b/crates/agent/src/panel/tests/execution/screenshot_history.rs index 6c7849cd..79b6a106 100644 --- a/crates/agent/src/panel/tests/execution/screenshot_history.rs +++ b/crates/agent/src/panel/tests/execution/screenshot_history.rs @@ -46,10 +46,10 @@ fn screenshot_identity_and_real_mcp_result_survive_late_input_and_history(cx: &m yield_ms: Some(0), }; let payload = bridge(&f, TerminalCall::ExecCommand(request.clone()), cx); - let actual = program.request.lock().unwrap(); - let actual = actual - .as_ref() - .expect("exact attached executor received request"); + let requests = f._directory.jobs.requests(); + let actual = requests.first().expect("command port received request"); + let executor: Arc = program.clone(); + assert!(Arc::ptr_eq(&f._directory.jobs.executor(0), &executor)); assert_eq!(actual.program, "bash"); assert_eq!(actual.args, args); assert_eq!(actual.stdin.as_deref(), Some(&b"input\r\n"[..])); @@ -72,15 +72,9 @@ fn screenshot_identity_and_real_mcp_result_survive_late_input_and_history(cx: &m ); }); let stdout = "line\r\n\t```literal```\n界\n"; - let sink = program.sink.lock().unwrap().take().unwrap(); - assert!(futures::executor::block_on( - sink.send(stdout.as_bytes().to_vec()) - )); - sink.finish(ExecExit { - status: Some(7), - stderr: "warning\n".into(), - }); - drop(sink); + f._directory + .jobs + .finish(0, stdout.as_bytes(), 7, "warning\n"); cx.run_until_parked(); let result = bridge( &f, diff --git a/crates/agent/src/panel/tests/hermes.rs b/crates/agent/src/panel/tests/hermes.rs index a8bda0cf..08a4fc73 100644 --- a/crates/agent/src/panel/tests/hermes.rs +++ b/crates/agent/src/panel/tests/hermes.rs @@ -9,7 +9,6 @@ use nocterm_ai::model::tools::{ToolChanges, ToolContent, ToolPatch, ToolRecord, use nocterm_ai::{ ApprovalPolicy, ExecCommand, ReadCommand, TerminalCall, thread::Entry, tool_display, }; -use nocterm_session::ExecExit; use serde_json::{Value, json}; /// The text Hermes reports for an MCP result, as `tool_dispatch_helpers` builds it. @@ -114,7 +113,7 @@ fn assert_completed_command( thread: &Entity, session: &SessionId, terminal_id: String, - program: &Program, + program: &Arc, cx: &mut TestAppContext, ) { let script = "qm config 9000; echo '--- PVE STORAGE ---'; pvesm status"; @@ -136,15 +135,9 @@ fn assert_completed_command( cx, ); let started = bridge(f, TerminalCall::ExecCommand(request), cx); - let sink = program.sink.lock().unwrap().take().unwrap(); - assert!(futures::executor::block_on( - sink.send(b"boot: order=scsi0\n".to_vec()) - )); - sink.finish(ExecExit { - status: Some(0), - stderr: String::new(), - }); - drop(sink); + let executor: Arc = program.clone(); + assert!(Arc::ptr_eq(&f._directory.jobs.executor(0), &executor)); + f._directory.jobs.finish(0, b"boot: order=scsi0\n", 0, ""); cx.run_until_parked(); let finished = bridge( f, diff --git a/crates/agent/src/panel/tests/history.rs b/crates/agent/src/panel/tests/history.rs index 5cf4be85..055ec91a 100644 --- a/crates/agent/src/panel/tests/history.rs +++ b/crates/agent/src/panel/tests/history.rs @@ -1,3 +1,4 @@ +mod search; use super::*; use crate::thread::AgentThread; use nocterm_ai::model::content::Content; @@ -259,7 +260,12 @@ fn assert_unaccepted_prompts_leave_no_history( assert!(thread.history_entries().is_empty()); thread.send(" ".into(), cx); assert!(thread.history_entries().is_empty()); - thread.begin_composer_edit(Vec::new(), thread.composer().attachments().to_vec(), cx); + thread.begin_composer_edit( + Vec::new(), + Vec::new(), + thread.composer().attachments().to_vec(), + cx, + ); thread.send("Queued before the session is ready".into(), cx); assert_eq!(thread.composer().queue().len(), 1); let ids = thread.queued_prompt_ids().collect::>(); @@ -272,7 +278,7 @@ fn assert_unaccepted_prompts_leave_no_history( #[gpui_kit::test] fn history_records_only_accepted_prompts_and_keeps_model_snapshot(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| { let thread = f.panel.read(cx).current().unwrap(); @@ -408,35 +414,7 @@ fn second_history_window( f: &Fixture, cx: &mut TestAppContext, ) -> (AnyWindowHandle, Entity) { - let mut panel = None; - let (handle, _) = cx.update(|cx| { - gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { - cx.new(|cx| { - let mut workspace = Workspace::new( - crate::panel::tests::deps(f.installation.layout.clone()), - window, - cx, - ); - let owner = cx.entity().downgrade(); - let source = workspace.general_surface_source(cx); - let view = cx.new(|cx| { - AgentPanel::new( - f._directory - .host(owner, f.installation.installed.clone(), source), - window, - cx, - ) - }); - crate::panel::tests::host_panel(&mut workspace, &view, window, cx); - workspace.set_panel_available(&crate::plugin::CHATS.id, true, window, cx); - workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx); - panel = Some(view); - workspace - }) - }) - .unwrap() - }); - let panel = panel.unwrap(); + let (handle, panel) = open_window_panel(f, cx); cx.update_window(handle, |_, window, cx| { window.resize(gpui_kit::size(gpui_kit::px(1800.), gpui_kit::px(900.))); let focus = panel.read(cx).composer_focus(cx); @@ -493,7 +471,7 @@ fn settle_scheduled_history( #[gpui_kit::test] fn shared_history_width_refreshes_painted_windows_without_scalar_echo(cx: &mut TestAppContext) { use gpui_kit::{px, size}; - let f = fixture_with_width(cx, 60., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 60., workspace_resolver(), image_deps()); let layout = &f.installation.layout; cx.update(|cx| cx.set_reduce_motion(true)); new_chat(&f, cx); @@ -573,7 +551,7 @@ fn shared_history_width_refreshes_painted_windows_without_scalar_echo(cx: &mut T #[gpui_kit::test] fn closing_one_window_keeps_the_surviving_history_layout_subscription(cx: &mut TestAppContext) { use gpui_kit::px; - let f = fixture_with_width(cx, 60., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 60., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| { cx.set_reduce_motion(true); @@ -624,7 +602,7 @@ fn shared_history_refresh_during_native_drag_keeps_later_pointer_events_safe( cx: &mut TestAppContext, ) { use gpui_kit::{InputEvent as _, MouseButton, MouseDownEvent, MouseUpEvent, point, px, size}; - let f = fixture_with_width(cx, 60., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 60., workspace_resolver(), image_deps()); let layout = &f.installation.layout; cx.update(|cx| cx.set_reduce_motion(true)); new_chat(&f, cx); diff --git a/crates/agent/src/panel/tests/history/search.rs b/crates/agent/src/panel/tests/history/search.rs new file mode 100644 index 00000000..cff89daf --- /dev/null +++ b/crates/agent/src/panel/tests/history/search.rs @@ -0,0 +1,72 @@ +//! Archived full-text search uses the same admitted native-body service. +use super::*; +use nocterm_agent_runtime::history_admission::{ + MAX_NORMAL_HISTORY_BYTES, MAX_NORMAL_HISTORY_PAYLOADS, +}; +use nocterm_ai::metadata::ChatMetadataSource as _; + +#[gpui_kit::test] +fn changing_an_archive_search_keeps_the_old_decoded_worker_charged_until_actual_drop( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + cx.run_until_parked(); + let identity = f._directory.metadata.new_identity(); + let mut chat = + nocterm_ai::history::SavedChat::for_identity("codex".into(), identity.id, identity.created); + chat.header.name = Some("Neutral saved title".into()); + chat.entries.push(nocterm_ai::thread::Entry::Agent(format!( + "oldnativeone {}", + "x".repeat(2 * 1024 * 1024) + ))); + let thread = super::super::archive::archived(&f, &chat, cx); + let decoded = f + .repository + .block_next(nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone(), + )); + super::super::panel_actions::show_history(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + replace_textbox("agent-history-search", "oldnativeone", window, cx); + }) + .unwrap(); + cx.run_until_parked(); + assert!( + f.repository + .operations() + .contains(&nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone() + )) + ); + let admission = cx.update(|cx| f.installation.installed.runtime().history_admission(cx)); + cx.update_window(f.handle, |_, window, cx| { + replace_textbox("agent-history-search", "replacement_no_match", window, cx); + }) + .unwrap(); + cx.run_until_parked(); + let slots = (0..MAX_NORMAL_HISTORY_PAYLOADS) + .filter_map(|_| admission.reserve_payload().ok()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); + cx.update(|cx| { + assert!( + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES - 2 * 1024 * 1024, cx) + .is_err() + ) + }); + decoded.send(Ok(())).unwrap(); + cx.run_until_parked(); + let released = admission.reserve_payload().unwrap(); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("agent-history-no-match").is_some()); + assert!( + window + .try_find(("history-thread", thread.entity_id().as_u64())) + .is_none() + ); + }) + .unwrap(); + drop((released, slots)); +} diff --git a/crates/agent/src/panel/tests/invalidation.rs b/crates/agent/src/panel/tests/invalidation.rs index 349c058d..ffe06393 100644 --- a/crates/agent/src/panel/tests/invalidation.rs +++ b/crates/agent/src/panel/tests/invalidation.rs @@ -230,6 +230,7 @@ fn refused_shutdown_registration_returns_a_usable_original_installation(cx: &mut f.installation.layout.clone(), &f.installation.ready, &refused, + None, cx, ); assert!(matches!( @@ -393,3 +394,132 @@ async fn ai_off_clears_edit_drafts_stream_and_queue_measurements(cx: &mut TestAp }) .unwrap(); } + +fn count_panel_notifications( + f: &Fixture, + cx: &mut TestAppContext, +) -> (Rc>, gpui_kit::Subscription) { + let notifications = Rc::new(Cell::new(0)); + let seen = notifications.clone(); + let observer = cx.update(|cx| cx.observe(&f.panel, move |_, _| seen.set(seen.get() + 1))); + (notifications, observer) +} + +#[gpui_kit::test] +fn a_streamed_chunk_draws_in_the_open_chat_without_redrawing_the_whole_panel( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "question", "answer", cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("next question".into(), cx)); + cx.run_until_parked(); + let streamed = cx.update(|cx| thread.read(cx).history_entries().len()); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find(("agent-entry", streamed)).is_none()); + }) + .unwrap(); + let (notifications, _observer) = count_panel_notifications(&f, cx); + let session = cx.update(|cx| thread.read(cx).session().clone().unwrap()); + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session, + SessionUpdate::AgentMessageChunk(Content::text("streamed answer")), + ))) + .unwrap(); + cx.run_until_parked(); + cx.executor() + .advance_clock(std::time::Duration::from_millis(100)); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + // A plain draw, which reuses cached views a refresh would redraw. + window.draw(cx).clear(cx); + assert!( + window.try_find(("agent-entry", streamed)).is_some(), + "the streamed answer draws in the open chat" + ); + }) + .unwrap(); + assert_eq!( + notifications.get(), + 0, + "a chunk redraws the chat's regions, not the whole panel" + ); +} + +#[gpui_kit::test] +fn typing_a_draft_saves_it_without_redrawing_the_whole_panel(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "question", "answer", cx); + cx.update_window(f.handle, |_, window, cx| window.render_frame(cx)) + .unwrap(); + let (notifications, _observer) = count_panel_notifications(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + replace_composer_value(f.panel.read(cx).composer_focus(cx), "a draft", window, cx); + window.render_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.executor() + .advance_clock(std::time::Duration::from_millis(100)); + cx.run_until_parked(); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + cx.update(|cx| { + assert_eq!(f.panel.read(cx).composer_text(cx).as_ref(), "a draft"); + assert_eq!(thread.read(cx).draft_text(), Some("a draft")); + }); + assert_eq!( + notifications.get(), + 0, + "typing redraws the composer, not the transcript and history" + ); +} + +#[gpui_kit::test] +fn a_growing_draft_grows_the_cached_composer_below_the_transcript(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "question", "answer", cx); + let draw = |cx: &mut TestAppContext| { + // Plain draws, which reuse cached regions a refresh would redraw. + for _ in 0..2 { + cx.update_window(f.handle, |_, window, cx| window.draw(cx).clear(cx)) + .unwrap(); + cx.run_until_parked(); + } + cx.update_window(f.handle, |_, window, _| { + ( + window.find("agent-composer").bounds(), + window.find("agent-transcript-area").bounds(), + ) + }) + .unwrap() + }; + let (before, _) = draw(cx); + cx.update_window(f.handle, |_, window, cx| { + replace_composer_value( + f.panel.read(cx).composer_focus(cx), + "one\ntwo\nthree\nfour\nfive", + window, + cx, + ); + }) + .unwrap(); + let (after, transcript) = draw(cx); + assert!( + after.size.height > before.size.height, + "the composer grows with its draft: {before:?} -> {after:?}" + ); + assert_eq!( + after.bottom(), + before.bottom(), + "the composer stays at the bottom" + ); + assert!( + transcript.bottom() <= after.top(), + "the transcript gives way: {transcript:?} above {after:?}" + ); +} diff --git a/crates/agent/src/panel/tests/labels.rs b/crates/agent/src/panel/tests/labels.rs index fb66340c..5bd968a8 100644 --- a/crates/agent/src/panel/tests/labels.rs +++ b/crates/agent/src/panel/tests/labels.rs @@ -50,7 +50,7 @@ fn chat_label_heights(f: &Fixture, raw: &str, cx: &mut TestAppContext) -> [gpui_ #[gpui_kit::test] fn chat_header_and_history_keep_multiline_titles_on_one_line(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let prompt = "смотри в чем соль\nтут"; @@ -132,7 +132,7 @@ fn chat_header_and_history_keep_multiline_titles_on_one_line(cx: &mut TestAppCon #[gpui_kit::test] fn completed_and_live_heredoc_tool_headers_keep_one_line_and_raw_command(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); let script = "python3 - <<'PY'\nfrom pathlib import Path\nPath('/tmp/probe.txt').write_text('nocterm-ui-test-artifacts')\nPY"; diff --git a/crates/agent/src/panel/tests/lifecycle.rs b/crates/agent/src/panel/tests/lifecycle.rs index e5310c27..43e52933 100644 --- a/crates/agent/src/panel/tests/lifecycle.rs +++ b/crates/agent/src/panel/tests/lifecycle.rs @@ -101,7 +101,6 @@ async fn disabling_ai_cancels_pending_initialization_immediately(cx: &mut TestAp started: started.clone(), cancelled: cancelled.clone(), }); - services.usage_source = Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()); services.workdir = f._directory.path().join("pending"); services }, diff --git a/crates/agent/src/panel/tests/mod.rs b/crates/agent/src/panel/tests/mod.rs index 7559a5dc..24d20dc2 100644 --- a/crates/agent/src/panel/tests/mod.rs +++ b/crates/agent/src/panel/tests/mod.rs @@ -60,6 +60,8 @@ mod terminal_fixture; use terminal_fixture::{Access, FakeTerminal, SignIn}; struct HostEnvironment { + jobs: Arc, + transfers: Arc, directory: tempfile::TempDir, metadata: Arc, } @@ -115,23 +117,21 @@ struct Fixture { repository: Arc, } fn fixture(cx: &mut TestAppContext) -> Fixture { - fixture_with_width(cx, 26., usage_source(), workspace_resolver(), image_deps()) + fixture_with_width(cx, 26., workspace_resolver(), image_deps()) } fn workspace_resolver() -> Arc { Arc::new(nocterm_ai::testing::ScriptedWorkdirResolver::accept_requests()) } -fn usage_source() -> Arc { - Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()) -} #[expect(clippy::too_many_lines, reason = "predates the limit")] fn fixture_with_width( cx: &mut TestAppContext, width: f32, - usage_source: Arc, workdir_resolver: Arc, images: crate::ImageDeps, ) -> Fixture { let directory = HostEnvironment { + jobs: Arc::new(nocterm_session::testing::ScriptedCommandJobsFactory::default()), + transfers: Arc::default(), directory: tempfile::tempdir().unwrap(), metadata: Arc::new(nocterm_ai::testing::ScriptedChatMetadata::default()), }; @@ -144,6 +144,7 @@ fn fixture_with_width( let bridge = Arc::new(Bridge { next: AtomicU64::new(1), revoked: Default::default(), + offers: Default::default(), stops: AtomicUsize::new(0), calls, rejections, @@ -186,6 +187,8 @@ fn fixture_with_width( let layout = nocterm_ui::LayoutMemory::init(None, &shutdown, cx); let (installed, registration) = crate::init( AgentServices { + command_jobs: directory.jobs.clone(), + transfers: directory.transfers.clone(), catalog: nocterm_agent_providers::catalog(), metadata: directory.metadata.clone(), terminal_auth: None, @@ -198,7 +201,6 @@ fn fixture_with_width( repository.clone(), Ok(Default::default()), ), - usage_source, workdir_resolver, workdir: directory.path().join("work"), }, @@ -206,6 +208,7 @@ fn fixture_with_width( layout.clone(), &ui, &shutdown, + None, cx, ); registration.unwrap(); @@ -242,6 +245,8 @@ fn fixture_with_width( } fn fixture_services(f: &Fixture) -> AgentServices { AgentServices { + command_jobs: f._directory.jobs.clone(), + transfers: f._directory.transfers.clone(), catalog: nocterm_agent_providers::catalog(), metadata: f._directory.metadata.clone(), connector: f.connector.clone(), @@ -251,7 +256,6 @@ fn fixture_services(f: &Fixture) -> AgentServices { f.repository.clone(), Ok(f.repository.state()), ), - usage_source: Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()), workdir_resolver: workspace_resolver(), workdir: f._directory.path().join("work"), terminal_auth: None, @@ -296,6 +300,37 @@ fn open_host( (handle, workspace, panel.unwrap(), terminal.unwrap()) } /// Dependencies whose plugin catalog lists the agent, over `layout`. +/// Another window of the fixture's installation, its chats panel open. +fn open_window_panel( + f: &Fixture, + cx: &mut TestAppContext, +) -> (AnyWindowHandle, Entity) { + let mut panel = None; + let (handle, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let mut workspace = Workspace::new(deps(f.installation.layout.clone()), window, cx); + let owner = cx.entity().downgrade(); + let source = workspace.general_surface_source(cx); + let view = cx.new(|cx| { + AgentPanel::new( + f._directory + .host(owner, f.installation.installed.clone(), source), + window, + cx, + ) + }); + host_panel(&mut workspace, &view, window, cx); + workspace.set_panel_available(&crate::plugin::CHATS.id, true, window, cx); + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx); + panel = Some(view); + workspace + }) + }) + .unwrap() + }); + (handle, panel.unwrap()) +} fn deps(layout: nocterm_ui::LayoutHandle) -> nocterm_workspace::Deps { nocterm_workspace::test_support::deps_over(layout, &[], &[&crate::plugin::PLUGIN]) } @@ -331,6 +366,7 @@ fn install_fixture( f.installation.layout.clone(), &f.installation.ready, &f.installation.shutdown, + None, cx, ); registration.unwrap(); @@ -388,7 +424,9 @@ fn lazy_start(f: &Fixture, cx: &mut TestAppContext) { cx.run_until_parked(); } mod panel_actions; -use panel_actions::{new_chat, open_queue, replace_composer_value, replace_textbox, show_history}; +use panel_actions::{ + new_chat, open_queue, replace_composer_value, replace_textbox, show_history, show_history_in, +}; fn complete_active(f: &Fixture, cx: &mut TestAppContext) { f.commands .pending @@ -453,6 +491,7 @@ impl nocterm_workspace::ConnectionDirectory for Directory { mod approvals; mod authentication; mod bridge_outcomes; +mod chat_servers; mod composer; mod context_rosters; mod durable_config; @@ -462,6 +501,7 @@ mod invalidation; mod labels; mod lifecycle; mod navigation; +mod other_windows; mod persistence; mod provider_fixtures; mod provider_permissions; @@ -486,6 +526,7 @@ mod flow; mod queue_edit; mod queue_layout; +mod queue_timing; mod commands; diff --git a/crates/agent/src/panel/tests/navigation.rs b/crates/agent/src/panel/tests/navigation.rs index 70adbf8d..d7dd12f6 100644 --- a/crates/agent/src/panel/tests/navigation.rs +++ b/crates/agent/src/panel/tests/navigation.rs @@ -5,6 +5,24 @@ use nocterm_ai::model::session::{SessionNotice, SessionUpdate}; use nocterm_ai::model::tools::ToolRecord; use nocterm_ai::thread::Entry; +#[gpui_kit::test] +fn borrowed_local_rows_keep_their_copy_and_fork_actions(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "Local question", "Local answer", cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find(("fork-message", 0usize)).is_some()); + window.hover(("agent-entry", 0usize), cx); + window.click(("copy-message", 0usize), cx); + assert_eq!( + cx.read_from_clipboard().unwrap().text().as_deref(), + Some("Local question") + ); + }) + .unwrap(); +} + fn transcript(f: &Fixture, cx: &mut TestAppContext) -> Entity { new_chat(f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -317,7 +335,7 @@ fn streaming_and_new_messages_preserve_reader_anchor_and_new_chat_clears_navigat #[gpui_kit::test] fn navigation_pill_fits_narrow_chat_in_both_themes_and_keeps_rows_virtual(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 14., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 14., workspace_resolver(), image_deps()); let thread = transcript(&f, cx); thread.update(cx, |thread, cx| { let mut entries = thread.history_entries().to_vec(); @@ -524,6 +542,41 @@ fn streaming_keeps_reader_scroll_until_jump_to_latest(cx: &mut TestAppContext) { complete_active(&f, cx); } +#[gpui_kit::test] +fn a_code_block_streamed_in_chunks_copies_its_whole_code(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + cx.update(|cx| cx.set_reduce_motion(true)); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("Show debounce".into(), cx)); + cx.run_until_parked(); + let session = cx.update(|cx| thread.read(cx).session().clone().unwrap()); + let code = "export function debounce() {\n return null;\n}"; + for chunk in [ + "Here it is:\n\n```typescript\nexport function debounce() {\n", + " return null;\n}\n```\n", + ] { + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session.clone(), + SessionUpdate::AgentMessageChunk(Content::text(chunk)), + ))) + .unwrap(); + cx.run_until_parked(); + for _ in 0..3 { + // Plain draws, which reuse the cached transcript unless it was told to redraw. + cx.update_window(f.handle, |_, window, cx| window.draw(cx).clear(cx)) + .unwrap(); + cx.run_until_parked(); + } + } + cx.update_window(f.handle, |_, window, cx| { + window.click("copy", cx); + assert_eq!(cx.read_from_clipboard().unwrap().text().unwrap(), code); + }) + .unwrap(); +} + #[gpui_kit::test] fn code_blocks_in_replies_copy_their_code(cx: &mut TestAppContext) { let f = fixture(cx); diff --git a/crates/agent/src/panel/tests/other_windows.rs b/crates/agent/src/panel/tests/other_windows.rs new file mode 100644 index 00000000..b4d1e24f --- /dev/null +++ b/crates/agent/src/panel/tests/other_windows.rs @@ -0,0 +1,137 @@ +//! Two windows of one installation: each lists every chat, a chat belongs to one of them. +use super::*; + +/// Renders `handle` and runs `check` on it. +fn in_window( + handle: AnyWindowHandle, + cx: &mut TestAppContext, + check: impl FnOnce(&mut Window, &mut App) -> R, +) -> R { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + check(window, cx) + }) + .unwrap() +} + +#[gpui_kit::test] +fn a_second_window_lists_the_first_windows_chats_and_opens_an_idle_one_here( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "Why is the disk full?", "The logs grew.", cx); + let first = cx.update(|cx| f.panel.read(cx).current().unwrap()); + let chat = cx.update(|cx| first.read(cx).chat_id().to_owned()); + // The first window moves on, so the chat neither works nor shows there. + new_chat(&f, cx); + + let (second, panel) = open_window_panel(&f, cx); + show_history_in(second, cx); + let row = first.entity_id().as_u64(); + in_window(second, cx, |window, cx| { + assert_eq!( + window.find(("history-elsewhere", row)).label(), + Some("Open in another window: opens here") + ); + assert!(window.try_find(("delete-thread", row)).is_none()); + assert_eq!(panel.read(cx).chat_documents().len(), 0, "listed, not held"); + window.click(("history-thread", row), cx); + }); + cx.run_until_parked(); + + let moved = cx.update(|cx| panel.read(cx).current().unwrap()); + cx.update(|cx| { + let thread = moved.read(cx); + assert_eq!(thread.chat_id(), chat); + assert_eq!(thread.transcript().entries.len(), 2, "its whole transcript"); + assert!( + f.panel + .read(cx) + .chat_documents() + .all(|thread| thread.read(cx).chat_id() != chat), + "the first window no longer holds it" + ); + }); + show_history(&f, cx); + in_window(f.handle, cx, |window, _| { + assert!( + window + .try_find(("history-elsewhere", moved.entity_id().as_u64())) + .is_some(), + "the first window lists it where it went" + ); + }); +} + +#[gpui_kit::test] +fn a_chat_working_in_another_window_opens_in_that_window(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + let working = cx.update(|cx| f.panel.read(cx).current().unwrap()); + cx.update(|cx| working.update(cx, |thread, cx| thread.send("Rotate the logs".into(), cx))); + cx.run_until_parked(); + new_chat(&f, cx); + + let (second, panel) = open_window_panel(&f, cx); + show_history_in(second, cx); + let row = working.entity_id().as_u64(); + in_window(second, cx, |window, cx| { + assert_eq!( + window.find(("history-elsewhere", row)).label(), + Some("Working in another window: opens there") + ); + window.click(("history-thread", row), cx); + }); + cx.run_until_parked(); + + cx.update(|cx| { + assert_eq!( + panel.read(cx).chat_documents().len(), + 0, + "it stays where it works" + ); + assert_eq!( + f.panel.read(cx).current().map(|thread| thread.entity_id()), + Some(working.entity_id()), + "its window shows it" + ); + }); + complete_active(&f, cx); +} + +#[gpui_kit::test] +fn a_saved_chat_the_first_window_has_not_opened_moves_with_its_transcript(cx: &mut TestAppContext) { + let f = fixture(cx); + cx.run_until_parked(); + let identity = nocterm_ai::metadata::ChatMetadataSource::new_identity(&*f._directory.metadata); + let mut chat = + nocterm_ai::history::SavedChat::for_identity("codex".into(), identity.id, identity.created); + chat.entries + .push(nocterm_ai::thread::Entry::Agent("Nginx is up.".into())); + let archived = super::archive::archived(&f, &chat, cx); + assert!(cx.update(|cx| archived.read(cx).is_archived())); + + let (second, panel) = open_window_panel(&f, cx); + show_history_in(second, cx); + in_window(second, cx, |window, cx| { + window.click(("history-thread", archived.entity_id().as_u64()), cx); + }); + cx.run_until_parked(); + + let moved = cx.update(|cx| panel.read(cx).current().unwrap()); + cx.update(|cx| { + assert_eq!(moved.read(cx).chat_id(), chat.header.id); + assert_eq!( + moved.read(cx).transcript().entries.len(), + 1, + "loaded where it went" + ); + assert!( + f.panel + .read(cx) + .chat_documents() + .all(|thread| thread.entity_id() != archived.entity_id()) + ); + }); +} diff --git a/crates/agent/src/panel/tests/panel_actions.rs b/crates/agent/src/panel/tests/panel_actions.rs index 78049474..7d332afe 100644 --- a/crates/agent/src/panel/tests/panel_actions.rs +++ b/crates/agent/src/panel/tests/panel_actions.rs @@ -16,13 +16,16 @@ pub(super) fn replace_composer_value( } } pub(super) fn new_chat(fixture: &Fixture, cx: &mut TestAppContext) { + new_agent_chat(fixture, "codex", cx); +} +pub(super) fn new_agent_chat(fixture: &Fixture, agent: &str, cx: &mut TestAppContext) { cx.update_window(fixture.handle, |_, window, cx| { let focus = fixture.panel.read(cx).composer_focus(cx); window.focus(&focus, cx); window.render_frame(cx); window.click("agent-new-thread", cx); window.render_frame(cx); - window.click("new-agent-codex", cx); + window.click(format!("new-agent-{agent}"), cx); window.render_frame(cx); let thread = fixture.panel.read(cx).current().unwrap(); thread.update(cx, |thread, cx| thread.request_activation(cx)); @@ -31,7 +34,10 @@ pub(super) fn new_chat(fixture: &Fixture, cx: &mut TestAppContext) { cx.run_until_parked(); } pub(super) fn show_history(f: &Fixture, cx: &mut TestAppContext) { - cx.update_window(f.handle, |_, window, cx| { + show_history_in(f.handle, cx); +} +pub(super) fn show_history_in(handle: AnyWindowHandle, cx: &mut TestAppContext) { + cx.update_window(handle, |_, window, cx| { window.render_frame(cx); if window.try_find("agent-history-column").is_none() { window.click("agent-history", cx); diff --git a/crates/agent/src/panel/tests/persistence.rs b/crates/agent/src/panel/tests/persistence.rs index 363eb1b1..545859b3 100644 --- a/crates/agent/src/panel/tests/persistence.rs +++ b/crates/agent/src/panel/tests/persistence.rs @@ -238,8 +238,8 @@ fn loaded_browse_noop_actions_detach_and_quit_preserve_the_original_v1_bytes( thread.rename(None, cx); thread.set_pinned(false, cx); thread.set_draft(String::new(), cx); - assert!(thread.shared_snapshot(cx).is_some()); - assert!(thread.flush_snapshot(cx).is_none()); + assert!(thread.shared_snapshot(cx).unwrap().is_some()); + assert!(thread.flush_snapshot(cx).unwrap().is_none()); thread.release_resources(cx); }); cx.run_until_parked(); @@ -320,8 +320,8 @@ fn captures_before_the_draft_timer_keep_one_revision_and_one_immutable_intent( thread.set_draft("before 300 ms".into(), cx) }); let client = crate::thread::client(&thread); - let first = cx.update(|cx| client.capture(cx).unwrap()); - let second = cx.update(|cx| client.capture(cx).unwrap()); + let first = cx.update(|cx| client.capture(cx).unwrap().unwrap()); + let second = cx.update(|cx| client.capture(cx).unwrap().unwrap()); assert_eq!(first.1, second.1); assert!(Arc::ptr_eq(&first.0, &second.0)); shutdown_runtime(&f.installation.installed, cx); @@ -347,8 +347,8 @@ fn shared_snapshots_reuse_queued_payload_and_cache_exact_size(cx: &mut TestAppCo .accept_composer_images(vec![prompt_image()], cx) .unwrap(); thread.send("queued\n\"\\界".into(), cx); - let first = thread.shared_snapshot(cx).unwrap(); - let second = thread.shared_snapshot(cx).unwrap(); + let first = thread.shared_snapshot(cx).unwrap().unwrap(); + let second = thread.shared_snapshot(cx).unwrap().unwrap(); assert!(Arc::ptr_eq(&first.queue[0], &second.queue[0])); assert!(Arc::ptr_eq( &first.queue[0], diff --git a/crates/agent/src/panel/tests/provider_fixtures.rs b/crates/agent/src/panel/tests/provider_fixtures.rs index 98eba400..54e22546 100644 --- a/crates/agent/src/panel/tests/provider_fixtures.rs +++ b/crates/agent/src/panel/tests/provider_fixtures.rs @@ -3,23 +3,8 @@ use super::*; use nocterm_ai::TerminalCall; use nocterm_ai::model::ids::SessionId; use nocterm_ai::model::session::{SessionNotice, SessionUpdate}; -use nocterm_session::{ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec}; use serde_json::Value; - -#[derive(Default)] -pub(super) struct Program { - pub(super) request: Mutex>, - pub(super) sink: Mutex>, -} - -impl HostExec for Program { - fn exec(&self, request: ExecRequest) -> ExecFuture { - *self.request.lock().unwrap() = Some(request); - let (sink, output) = ExecOutput::channel(); - *self.sink.lock().unwrap() = Some(sink); - async move { Ok(output) }.boxed() - } -} +pub(super) type Program = nocterm_session::testing::FakeExec; pub(super) fn bridge(f: &Fixture, call: TerminalCall, cx: &mut TestAppContext) -> Value { let registration_id = cx.update(|cx| { diff --git a/crates/agent/src/panel/tests/provider_permissions.rs b/crates/agent/src/panel/tests/provider_permissions.rs index 0a5eca6f..74f08f18 100644 --- a/crates/agent/src/panel/tests/provider_permissions.rs +++ b/crates/agent/src/panel/tests/provider_permissions.rs @@ -138,7 +138,7 @@ fn changes_handle_pending_requests_in_every_chat_and_ask_again_for_future_reques fn no_one_time_choice_stays_visible_with_an_explanation_and_can_be_cancelled( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); set_policy(&f, cx, ApprovalPolicy::Allow); new_chat(&f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); diff --git a/crates/agent/src/panel/tests/queue_layout.rs b/crates/agent/src/panel/tests/queue_layout.rs index 65e072bc..90404ae4 100644 --- a/crates/agent/src/panel/tests/queue_layout.rs +++ b/crates/agent/src/panel/tests/queue_layout.rs @@ -1,7 +1,7 @@ use super::*; use gpui_kit::px; -fn settle(f: &Fixture, cx: &mut TestAppContext) { +pub(super) fn settle(f: &Fixture, cx: &mut TestAppContext) { for _ in 0..3 { cx.update_window(f.handle, |_, window, cx| window.render_frame(cx)) .unwrap(); @@ -9,9 +9,60 @@ fn settle(f: &Fixture, cx: &mut TestAppContext) { } } +fn assert_first_open_frame(reduced: bool, cx: &mut TestAppContext) { + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); + new_chat(&f, cx); + cx.update(|cx| cx.set_reduce_motion(reduced)); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| { + thread.clear_composer_context(cx); + thread.send("active".into(), cx); + thread.send("short queued message".into(), cx); + }); + settle(&f, cx); + let viewport = cx + .update_window(f.handle, |_, window, cx| { + let height = window.find("agent-transcript-area").bounds().size.height; + window.click("queue-toggle", cx); + height + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.simulate_next_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + // Preserve normal cached-view invalidation: render_frame() forces a refresh. + window.draw(cx).clear(cx); + let reveal = window.find("agent-queue-reveal"); + let rows = window.find("agent-queue-rows"); + assert!( + reveal.visible(), + "the first post-click frame must show the list" + ); + assert!(rows.bounds().size.height > px(20.)); + assert!((reveal.bounds().size.height - rows.bounds().size.height).abs() <= px(1.)); + let remaining = window.find("agent-transcript-area").bounds().size.height; + assert!((viewport - remaining - reveal.bounds().size.height).abs() <= px(1.)); + }) + .unwrap(); +} + +#[gpui_kit::test] +fn the_queue_opens_at_full_height_in_its_first_frame(cx: &mut TestAppContext) { + assert_first_open_frame(false, cx); +} + +#[gpui_kit::test] +fn reduced_motion_opens_the_queue_without_a_measurement_frame(cx: &mut TestAppContext) { + assert_first_open_frame(true, cx); +} + #[gpui_kit::test] fn expanded_queue_fits_one_and_two_short_messages_without_blank_height(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -68,7 +119,7 @@ fn expanded_queue_fits_one_and_two_short_messages_without_blank_height(cx: &mut #[gpui_kit::test] fn wrapped_text_images_and_attachments_are_measured_then_capped(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -106,7 +157,6 @@ fn collapsed_queue_skips_hidden_row_and_image_rendering(cx: &mut TestAppContext) let f = fixture_with_width( cx, 32., - usage_source(), workspace_resolver(), crate::ImageDeps::new(codec.clone(), codec.clone()), ); @@ -145,3 +195,56 @@ fn collapsed_queue_skips_hidden_row_and_image_rendering(cx: &mut TestAppContext) "opening queue must prepare its images" ); } + +#[gpui_kit::test] +fn reopening_the_queue_keeps_its_thumbnail_and_needs_no_animation_ticks(cx: &mut TestAppContext) { + let codec = image_codec(); + let f = fixture_with_width( + cx, + 18., + workspace_resolver(), + crate::ImageDeps::new(codec.clone(), codec.clone()), + ); + new_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| { + thread.send("active".into(), cx); + thread + .accept_composer_images(vec![prompt_image()], cx) + .unwrap(); + thread.send("image in the queue".into(), cx); + }); + settle(&f, cx); + for open in [true, false, true] { + cx.update_window(f.handle, |_, window, cx| window.click("queue-toggle", cx)) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.simulate_next_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.draw(cx).clear(cx); + if open { + let reveal = window.find("agent-queue-reveal"); + let height = window.find("agent-queue-rows").bounds().size.height; + assert!(reveal.visible()); + assert!((reveal.bounds().size.height - height.min(px(240.))).abs() <= px(1.)); + } else { + assert!(window.try_find("agent-queue-rows").is_none()); + } + }) + .unwrap(); + cx.run_until_parked(); + } + let prepared = codec + .requests() + .iter() + .filter(|request| matches!(request, nocterm_ai::testing::ImageRequest::Thumbnail(..))) + .count(); + assert_eq!( + prepared, 1, + "reopening must reuse the existing image preview" + ); +} diff --git a/crates/agent/src/panel/tests/queue_timing.rs b/crates/agent/src/panel/tests/queue_timing.rs new file mode 100644 index 00000000..194035cd --- /dev/null +++ b/crates/agent/src/panel/tests/queue_timing.rs @@ -0,0 +1,154 @@ +//! Manual CPU/layout timing through the real queue button and normal cached draws. +//! Run with `cargo test -p nocterm-agent --lib queue_opening_frame_trace -- --ignored --nocapture`. +//! Optional NOCTERM_QUEUE_PROBE_{BYTES,COUNT,IMAGE,WARM,REDUCE_MOTION} select the workload. +//! Set NOCTERM_QUEUE_PROBE_NATIVE_TEXT=1 for real system font shaping. +//! Logical 16 ms frame steps are not GPU presentation or wall-clock input latency. +use super::*; +use gpui_kit::px; +use std::time::{Duration, Instant}; + +fn option(name: &str, default: usize) -> usize { + std::env::var(format!("NOCTERM_QUEUE_PROBE_{name}")) + .ok() + .map(|value| { + value + .parse() + .expect("queue probe option must be an integer") + }) + .unwrap_or(default) +} + +fn advance_frame(f: &Fixture, cx: &mut TestAppContext, advance: bool) { + if advance { + cx.executor().advance_clock(Duration::from_millis(16)); + } + cx.update_window(f.handle, |_, window, cx| { + window.simulate_next_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +fn settle_motion(f: &Fixture, cx: &mut TestAppContext) { + for _ in 0..20 { + advance_frame(f, cx, true); + cx.update_window(f.handle, |_, window, cx| window.draw(cx).clear(cx)) + .unwrap(); + cx.run_until_parked(); + } +} + +#[gpui_kit::test] +#[ignore = "manual queue opening timing probe"] +fn queue_opening_frame_trace(cx: &mut TestAppContext) { + if option("NATIVE_TEXT", 0) == 0 { + measure_queue(cx); + return; + } + let platform = gpui_kit::platform::current_platform(true); + let mut native = TestAppContext::build_with_text_system( + gpui_kit::TestDispatcher::new(0), + Some("queue_opening_native_text"), + platform.text_system(), + ); + measure_queue(&mut native); +} + +#[expect( + clippy::print_stderr, + reason = "explicitly requested manual queue timing diagnostics" +)] +fn measure_queue(cx: &mut TestAppContext) { + let (bytes, count) = (option("BYTES", 32), option("COUNT", 1)); + let (image, warm, reduced) = ( + option("IMAGE", 0) != 0, + option("WARM", 0) != 0, + option("REDUCE_MOTION", 0) != 0, + ); + let codec = image_codec(); + let f = fixture_with_width( + cx, + 18., + workspace_resolver(), + crate::ImageDeps::new(codec.clone(), codec.clone()), + ); + new_chat(&f, cx); + cx.update(|cx| cx.set_reduce_motion(reduced)); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| { + thread.clear_composer_context(cx); + thread.send("active".into(), cx); + if image { + thread + .accept_composer_images(vec![prompt_image()], cx) + .unwrap(); + } + let mut text = "queued message ".repeat(bytes.div_ceil(15)); + text.truncate(bytes); + for _ in 0..count { + thread.send(text.clone(), cx); + } + }); + super::queue_layout::settle(&f, cx); + if warm { + open_queue(&f, cx); + settle_motion(&f, cx); + cx.update_window(f.handle, |_, window, cx| window.click("queue-toggle", cx)) + .unwrap(); + settle_motion(&f, cx); + } + eprintln!( + "QUEUE_WORKLOAD {}", + serde_json::json!({ + "bytes": bytes, "count": count, "image": image, "warm": warm, "reduced": reduced, + }) + ); + trace_open(&f, codec.as_ref(), cx); +} + +#[expect( + clippy::print_stderr, + reason = "explicitly requested manual queue timing diagnostics" +)] +fn trace_open( + f: &Fixture, + codec: &nocterm_ai::testing::ScriptedImageCodec, + cx: &mut TestAppContext, +) { + cx.update_window(f.handle, |_, window, cx| window.click("queue-toggle", cx)) + .unwrap(); + cx.run_until_parked(); + for frame in 0..20 { + advance_frame(f, cx, frame != 0); + let started = Instant::now(); + let (height, visible, rows) = cx + .update_window(f.handle, |_, window, cx| { + window.draw(cx).clear(cx); + let reveal = window.try_find("agent-queue-reveal"); + ( + reveal + .as_ref() + .map(|node| node.bounds().size.height / px(1.)), + reveal.as_ref().is_some_and(|node| node.visible()), + window + .try_find("agent-queue-rows") + .map(|node| node.bounds().size.height / px(1.)), + ) + }) + .unwrap(); + cx.run_until_parked(); + eprintln!( + "QUEUE_FRAME {}", + serde_json::json!({ + "ms": frame * 16, "frame_us": started.elapsed().as_micros(), + "height": height, "visible": visible, "rows": rows, + }) + ); + } + let thumbnails = codec + .requests() + .iter() + .filter(|request| matches!(request, nocterm_ai::testing::ImageRequest::Thumbnail(..))) + .count(); + eprintln!("QUEUE_THUMBNAILS {thumbnails}"); +} diff --git a/crates/agent/src/panel/tests/reconnect.rs b/crates/agent/src/panel/tests/reconnect.rs index ba615fb6..3d4da12a 100644 --- a/crates/agent/src/panel/tests/reconnect.rs +++ b/crates/agent/src/panel/tests/reconnect.rs @@ -112,3 +112,48 @@ fn identical_secret_prompt_tokens_are_specific_to_the_original_request(cx: &mut }); assert_eq!(*f.access.sign_in().answers().borrow(), ["current"]); } + +#[gpui_kit::test] +fn terminal_files_follow_the_configured_filesystem_and_the_exact_connection( + cx: &mut TestAppContext, +) { + use nocterm_workspace::{TerminalError, TerminalFiles as _, TerminalFilesystem}; + let f = fixture(cx); + new_chat(&f, cx); + let expected = cx.update(|cx| f.access.info(cx).unwrap().expected); + cx.update(|cx| { + assert!(matches!( + f.access.filesystem(expected, cx), + Err(TerminalError::Unsupported( + nocterm_workspace::TerminalCapability::Files + )) + )); + }); + f.access.serve_local_files(); + cx.update(|cx| { + assert!(matches!( + f.access.filesystem(expected, cx), + Ok(TerminalFilesystem::Local) + )); + }); + f.access.link().remote().set(true); + let host = + nocterm_session::HostAddress::direct(nocterm_session::Target::new("agent", "server", 22)); + let remote = f.access.serve_remote_files(host.clone()); + cx.update(|cx| { + let Ok(TerminalFilesystem::Remote { host: served, fs }) = f.access.filesystem(expected, cx) + else { + panic!("a remote terminal serves its configured filesystem"); + }; + assert_eq!(served, host); + let remote: Arc = remote; + assert!(Arc::ptr_eq(&fs, &remote)); + }); + f.access.replace_connection(); + cx.update(|cx| { + assert!(matches!( + f.access.filesystem(expected, cx), + Err(TerminalError::StaleSession) + )); + }); +} diff --git a/crates/agent/src/panel/tests/resources.rs b/crates/agent/src/panel/tests/resources.rs index e4de9b44..c53c1057 100644 --- a/crates/agent/src/panel/tests/resources.rs +++ b/crates/agent/src/panel/tests/resources.rs @@ -109,7 +109,12 @@ async fn idle_guards_and_paused_queue_release_independently_of_terminal_ownershi cx.update(|cx| assert!(thread.read(cx).is_leased())); drop(guard); thread.update(cx, |thread, cx| { - thread.begin_composer_edit(Vec::new(), thread.composer().attachments().to_vec(), cx); + thread.begin_composer_edit( + Vec::new(), + Vec::new(), + thread.composer().attachments().to_vec(), + cx, + ); thread.submit("paused".into(), None, cx).unwrap(); thread.pause_queue(cx); thread.end_composer_edit(false, cx); diff --git a/crates/agent/src/panel/tests/restoration.rs b/crates/agent/src/panel/tests/restoration.rs index 702e3316..cba867f5 100644 --- a/crates/agent/src/panel/tests/restoration.rs +++ b/crates/agent/src/panel/tests/restoration.rs @@ -610,7 +610,7 @@ fn restored_project_data_keeps_its_scope_after_edits_and_refuses_a_general_hydra assert_eq!(f.commands.sessions.load(Ordering::SeqCst), 0); } #[gpui_kit::test] -fn legacy_general_restore_edits_and_restart_keep_wire_version_while_forks_use_v2( +fn legacy_general_restore_edits_and_restart_keep_wire_version_while_forks_use_v3( cx: &mut TestAppContext, ) { let mut f = fixture(cx); @@ -639,5 +639,5 @@ fn legacy_general_restore_edits_and_restart_keep_wire_version_while_forks_use_v2 f.panel.read(cx).current().unwrap() }) .unwrap(); - cx.update(|cx| assert_eq!(fork.read(cx).snapshot(cx).unwrap().version, 2)); + cx.update(|cx| assert_eq!(fork.read(cx).snapshot(cx).unwrap().version, 3)); } diff --git a/crates/agent/src/panel/tests/routing.rs b/crates/agent/src/panel/tests/routing.rs index d09f9949..c2573bfe 100644 --- a/crates/agent/src/panel/tests/routing.rs +++ b/crates/agent/src/panel/tests/routing.rs @@ -170,6 +170,17 @@ fn prompts_tell_the_agent_to_report_unreachable_servers(cx: &mut TestAppContext) assert!(context.text.contains("")); } +#[gpui_kit::test] +fn an_agent_with_tools_of_its_own_gets_only_what_the_person_wrote(cx: &mut TestAppContext) { + let f = fixture(cx); + f.connector.info.lock().unwrap().capabilities.own_tools = true; + new_chat(&f, cx); + exchange(&f, "check the server", "ok", cx); + let prompt = f.commands.prompts.lock().unwrap()[0].clone(); + assert_eq!(prompt.content.len(), 1, "{:?}", prompt.content); + assert!(matches!(&prompt.content[0], Content::Text(text) if text.text == "check the server")); +} + #[gpui_kit::test] fn an_unreachable_terminal_asks_the_agent_to_tell_the_user(cx: &mut TestAppContext) { let f = fixture(cx); diff --git a/crates/agent/src/panel/tests/scripted_agent.rs b/crates/agent/src/panel/tests/scripted_agent.rs index 127390d0..76dd8054 100644 --- a/crates/agent/src/panel/tests/scripted_agent.rs +++ b/crates/agent/src/panel/tests/scripted_agent.rs @@ -167,6 +167,10 @@ impl Connector { pub(super) fn support_images(&self) { self.info.lock().unwrap().capabilities.image_prompts = true; } + /// The agent runs elsewhere with tools of its own, as a hub agent does. + pub(super) fn run_elsewhere(&self) { + self.info.lock().unwrap().capabilities.own_tools = true; + } } impl AgentConnector for Connector { fn connect( @@ -202,6 +206,8 @@ impl AgentConnector for Connector { pub(super) struct Bridge { pub(super) next: AtomicU64, pub(super) revoked: Arc>>, + /// Every tool offer a chat made, with its registration. + pub(super) offers: Arc>>, pub(super) stops: AtomicUsize, pub(super) calls: async_channel::Receiver, pub(super) rejections: async_channel::Receiver, @@ -211,7 +217,7 @@ impl ToolBridge for Bridge { &self, ) -> Result { let id = self.next.fetch_add(1, Ordering::SeqCst); - let revoked = self.revoked.clone(); + let (revoked, offers) = (self.revoked.clone(), self.offers.clone()); Ok(BridgeRegistration::new( id, nocterm_ai::BridgeLaunch { @@ -220,7 +226,10 @@ impl ToolBridge for Bridge { env: vec![("NOCTERM_BRIDGE_TOKEN".into(), "test-token".into())], }, Arc::new(move |id| revoked.lock().unwrap().push(id)), - )) + ) + .with_offer(Arc::new(move |id, offer| { + offers.lock().unwrap().push((id, offer)); + }))) } fn calls(&self) -> async_channel::Receiver { self.calls.clone() diff --git a/crates/agent/src/panel/tests/seat_binding.rs b/crates/agent/src/panel/tests/seat_binding.rs index 1f7a212f..b423ed96 100644 --- a/crates/agent/src/panel/tests/seat_binding.rs +++ b/crates/agent/src/panel/tests/seat_binding.rs @@ -149,3 +149,85 @@ fn chats_keep_their_workspace_terminals_while_another_workspace_is_active(cx: &m }) .unwrap(); } + +#[gpui_kit::test] +fn simultaneous_account_sources_filter_foreign_terminals_and_relogin_cannot_reclaim_old_authority( + cx: &mut TestAppContext, +) { + use nocterm_ai::local_environment::LocalEnvironmentAccess; + use nocterm_hubs_model::{ + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, + LocalAccountSourceFactory, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, + }; + let f = fixture(cx); + let partition = |hub| { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("same-instance-id").unwrap(), + Fingerprint::from_bytes([hub; 32]), + ), + AccountIdentity::new( + NamespaceId::new("same-account-id").unwrap(), + Fingerprint::from_bytes([3; 32]), + ), + )) + }; + let a = Arc::new(ScriptedLocalAccountFactory::default()); + let b = Arc::new(ScriptedLocalAccountFactory::default()); + a.sign_in(partition(1)); + b.sign_in(partition(2)); + let access_a = Access::new(None, Rc::default()); + let access_b = Access::new(None, Rc::default()); + access_a.bind_source(LocalEnvironmentAccess::capture(a.capture().unwrap()).unwrap()); + access_b.bind_source(LocalEnvironmentAccess::capture(b.capture().unwrap()).unwrap()); + let (id_a, id_b) = cx + .update_window(f.handle, |_, window, cx| { + let terminal_a = cx.new(|cx| FakeTerminal::new(access_a, cx)); + let terminal_b = cx.new(|cx| FakeTerminal::new(access_b, cx)); + f.workspace.update(cx, |workspace, cx| { + workspace.add_item(terminal_a.clone(), window, cx); + workspace.add_item(terminal_b.clone(), window, cx); + }); + (terminal_a.entity_id(), terminal_b.entity_id()) + }) + .unwrap(); + let deps = |factory: Arc, cx: &App| { + f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.with_account_source(factory), + cx, + ) + }; + let (a_seat, b_seat) = cx.update(|cx| { + ( + deps(a.clone(), cx).seat().clone(), + deps(b.clone(), cx).seat().clone(), + ) + }); + cx.update(|cx| { + assert_eq!(terminal_ids(&a_seat, cx), [id_a]); + assert_eq!(terminal_ids(&b_seat, cx), [id_b]); + assert!(nocterm_agent_chat::seat::current_session(&b_seat, id_a, cx).is_none()); + }); + a.close(); + cx.update(|cx| { + assert!(a_seat.terminals(cx).is_none()); + assert_eq!(terminal_ids(&b_seat, cx), [id_b]); + }); + a.sign_in(partition(1)); + cx.update(|cx| { + let fresh = deps(a, cx); + assert!( + fresh.seat().terminals(cx).unwrap().is_empty(), + "the old terminal keeps its original login lease" + ); + assert!( + a_seat.terminals(cx).is_none(), + "a new login does not revive an old seat" + ); + assert_eq!(terminal_ids(&b_seat, cx), [id_b]); + }); +} diff --git a/crates/agent/src/panel/tests/terminal_fixture.rs b/crates/agent/src/panel/tests/terminal_fixture.rs index a5e422c7..60ddf134 100644 --- a/crates/agent/src/panel/tests/terminal_fixture.rs +++ b/crates/agent/src/panel/tests/terminal_fixture.rs @@ -3,10 +3,13 @@ use super::*; pub(super) struct Access { item: std::cell::Cell>, executor: std::cell::RefCell>>, + source: std::cell::RefCell>, lease: std::cell::RefCell>>, at_prompt: std::cell::Cell, sent: std::cell::RefCell>, profile: std::cell::RefCell>, + /// What `TerminalFiles` reports; unset means the terminal has no file access. + files: std::cell::RefCell>, /// Shared by the sessions of a test. sign_in: Rc, link: Link, @@ -36,15 +39,23 @@ impl Access { Rc::new(Self { item: Default::default(), executor: Default::default(), + source: Default::default(), lease: Default::default(), at_prompt: std::cell::Cell::new(true), sent: Default::default(), profile: std::cell::RefCell::new(profile), + files: Default::default(), sign_in, link: Default::default(), }) } + pub(super) fn bind_source( + &self, + source: nocterm_ai::local_environment::LocalEnvironmentAccess, + ) { + *self.source.borrow_mut() = Some(source); + } pub(super) fn replace_connection(&self) { self.link.epoch.set(self.link.epoch.get() + 1); self.link.closed.set(false); @@ -66,6 +77,21 @@ impl Access { impl TerminalState for Access { fn info(&self, _: &App) -> Option { Some(TerminalInfo { + source: self + .source + .borrow() + .as_ref() + .and_then(|access| match access.environment() { + nocterm_ai::local_environment::LocalEnvironment::HubAccount(partition) => { + Some(partition.clone()) + } + _ => None, + }), + source_current: self + .source + .borrow() + .as_ref() + .is_none_or(|access| access.check_current().is_ok()), expected: ExpectedSession::new(self.item.get()?, self.link.epoch.get()), title: "Terminal".into(), local: !self.link.remote.get(), @@ -146,6 +172,24 @@ impl TerminalExecution for Access { )) } } +impl nocterm_workspace::TerminalFiles for Access { + fn filesystem( + &self, + expected: ExpectedSession, + _: &App, + ) -> Result { + self.check(expected)?; + if self.link.closed.get() || self.sign_in.asks.get() || self.sign_in.vault_locked.get() { + return Err(nocterm_workspace::TerminalError::Unavailable); + } + self.files + .borrow() + .clone() + .ok_or(nocterm_workspace::TerminalError::Unsupported( + nocterm_workspace::TerminalCapability::Files, + )) + } +} impl LiveCommands for Access { fn begin( &self, @@ -245,7 +289,8 @@ impl Item for FakeTerminal { .with_live(self.access.clone()) .with_execution(self.access.clone()) .with_prompts(self.access.clone()) - .with_reconnect(self.access.clone()), + .with_reconnect(self.access.clone()) + .with_files(self.access.clone()), ) } } @@ -268,6 +313,22 @@ impl Access { pub(super) fn profile(&self) -> &std::cell::RefCell> { &self.profile } + /// This computer is the terminal's filesystem. + pub(super) fn serve_local_files(&self) { + *self.files.borrow_mut() = Some(nocterm_workspace::TerminalFilesystem::Local); + } + /// A remote host with its own in-memory filesystem, returned for seeding and assertions. + pub(super) fn serve_remote_files( + &self, + host: nocterm_session::HostAddress, + ) -> Arc { + let fs = Arc::new(nocterm_session::testing::InMemoryFs::default()); + *self.files.borrow_mut() = Some(nocterm_workspace::TerminalFilesystem::Remote { + host, + fs: fs.clone(), + }); + fs + } pub(super) fn sign_in(&self) -> &Rc { &self.sign_in } diff --git a/crates/agent/src/panel/tests/tool_input.rs b/crates/agent/src/panel/tests/tool_input.rs index 9e9b2882..1d7a38d1 100644 --- a/crates/agent/src/panel/tests/tool_input.rs +++ b/crates/agent/src/panel/tests/tool_input.rs @@ -222,7 +222,7 @@ fn settle(f: &Fixture, cx: &mut TestAppContext) { } fn tool_fixture(cx: &mut TestAppContext, title: &str, input: Option) -> Fixture { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -608,7 +608,9 @@ fn verified_destination_uses_the_connected_host_and_survives_rename_detach_and_r ); cx.update(|cx| { thread.update(cx, |thread, cx| { - thread.apply_presented_update(SessionUpdate::ToolCall(Box::new(call))); + thread + .apply_presented_update(SessionUpdate::ToolCall(Box::new(call))) + .unwrap(); let (respond, _) = oneshot::channel(); let mut request = BridgeCall { arguments: Some(json!({"terminal_id":"t1","command":"echo snapshot"})), @@ -625,12 +627,14 @@ fn verified_destination_uses_the_connected_host_and_survives_rename_detach_and_r *access.target.borrow_mut() = nocterm_session::Target::new("reconnected", "new.example", 22); cx.update(|cx| { thread.update(cx, |thread, _| { - thread.apply_presented_update(SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( - "snapshot", - ToolChanges::default() - .title("Renamed") - .status(ToolStatus::Succeeded), - )))); + thread + .apply_presented_update(SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + "snapshot", + ToolChanges::default() + .title("Renamed") + .status(ToolStatus::Succeeded), + )))) + .unwrap(); let Entry::Tool(call) = &thread.transcript().entries[0] else { unreachable!() }; @@ -654,7 +658,9 @@ fn verified_destination_uses_the_connected_host_and_survives_rename_detach_and_r ); cx.update(|cx| { thread.update(cx, |thread, cx| { - thread.apply_presented_update(SessionUpdate::ToolCall(Box::new(call))); + thread + .apply_presented_update(SessionUpdate::ToolCall(Box::new(call))) + .unwrap(); let (respond, _) = oneshot::channel(); let mut request = BridgeCall { arguments: Some(json!({"terminal_id":"t1","command":"echo ipv6"})), diff --git a/crates/agent/src/panel/tests/tool_output.rs b/crates/agent/src/panel/tests/tool_output.rs index 89f16419..8013e992 100644 --- a/crates/agent/src/panel/tests/tool_output.rs +++ b/crates/agent/src/panel/tests/tool_output.rs @@ -49,7 +49,7 @@ pub(super) fn set_call(f: &Fixture, call: ToolRecord, cx: &mut TestAppContext) { } pub(super) fn expanded_fixture(call: ToolRecord, cx: &mut TestAppContext) -> Fixture { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); set_call(&f, call, cx); diff --git a/crates/agent/src/panel/tests/usage_dismissal.rs b/crates/agent/src/panel/tests/usage_dismissal.rs index df46c456..aaf569da 100644 --- a/crates/agent/src/panel/tests/usage_dismissal.rs +++ b/crates/agent/src/panel/tests/usage_dismissal.rs @@ -182,7 +182,7 @@ fn usage_trigger_toggle_and_outside_menu_action_do_not_reopen_card(cx: &mut Test #[gpui_kit::test] fn usage_stays_above_a_multiline_composer_in_a_narrow_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 14., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 14., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update_window(f.handle, |_, window, cx| { replace_composer_value( @@ -266,7 +266,7 @@ fn assert_original_usage_bounds(window: &Window) { #[gpui_kit::test] fn usage_retains_original_full_chat_width_in_a_wide_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 40., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 40., workspace_resolver(), image_deps()); new_chat(&f, cx); open_usage(&f, cx); cx.update_window(f.handle, |_, window, _| { @@ -277,7 +277,7 @@ fn usage_retains_original_full_chat_width_in_a_wide_panel(cx: &mut TestAppContex #[gpui_kit::test] fn usage_retains_original_full_chat_width_in_a_narrow_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 14., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 14., workspace_resolver(), image_deps()); new_chat(&f, cx); open_usage(&f, cx); cx.update_window(f.handle, |_, window, _| { diff --git a/crates/agent/src/panel/widgets.rs b/crates/agent/src/panel/widgets.rs index 12c8d2d4..5045e9e7 100644 --- a/crates/agent/src/panel/widgets.rs +++ b/crates/agent/src/panel/widgets.rs @@ -174,10 +174,18 @@ pub(super) fn activity_text( } } pub(super) fn chat_markdown(id: impl Into, text: String) -> TextView { + chat_text(TextView::markdown(id, text)) +} +/// [`chat_markdown`] drawing a row's kept, already parsed state. +pub(super) fn chat_markdown_in( + state: &gpui_kit::Entity, +) -> TextView { + chat_text(TextView::new(state)) +} +fn chat_text(view: TextView) -> TextView { let mut code_block = gpui_kit::StyleRefinement::default().text_sm(); code_block.overflow.x = Some(gpui_kit::Overflow::Scroll); - TextView::markdown(id, text) - .selectable(true) + view.selectable(true) .text_sm() .min_w_0() .style(TextViewStyle { diff --git a/crates/agent/src/plugin.rs b/crates/agent/src/plugin.rs index 6a2e5c7d..f877fd61 100644 --- a/crates/agent/src/plugin.rs +++ b/crates/agent/src/plugin.rs @@ -21,6 +21,7 @@ pub const CHATS: SystemContribution = SystemContribution { chrome: PanelChrome::OwnHeader, }), preview: None, + edge: None, pinned: false, }; diff --git a/crates/agent/src/workspace_seat.rs b/crates/agent/src/workspace_seat.rs index 4b2d9c54..2f7d9c95 100644 --- a/crates/agent/src/workspace_seat.rs +++ b/crates/agent/src/workspace_seat.rs @@ -15,6 +15,7 @@ pub(crate) struct WorkspaceSeat { workspace: WeakEntity, source: ProjectSurfaceReceipt, key: Option, + runtime: Option, } impl WorkspaceSeat { pub(crate) fn new(workspace: WeakEntity, source: ProjectSurfaceReceipt) -> Self { @@ -22,8 +23,18 @@ impl WorkspaceSeat { workspace, source, key: None, + runtime: None, } } + pub(crate) fn with_runtime(mut self, runtime: crate::runtime::RuntimeHandle) -> Self { + self.runtime = Some(runtime); + self + } + fn current(&self) -> bool { + self.runtime + .as_ref() + .is_none_or(|runtime| runtime.check_current().is_ok()) + } fn permits(&self, workspace: &Workspace, item: EntityId, cx: &App) -> bool { workspace .context_terminals(self.source, cx) @@ -33,6 +44,14 @@ impl WorkspaceSeat { impl ChatWorkspace for WorkspaceSeat { fn for_chat(&self, key: &ChatKey) -> Result { require_chat_scope(self.source.context(), key.scope())?; + let environment = match &self.runtime { + Some(runtime) => runtime.environment()?.clone(), + None => Default::default(), + }; + nocterm_ai::storage::require_local_environment(&environment, key.environment())?; + if let Some(runtime) = &self.runtime { + runtime.check_current()?; + } if self.key.as_ref().is_some_and(|original| original != key) { return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); } @@ -40,18 +59,51 @@ impl ChatWorkspace for WorkspaceSeat { workspace: self.workspace.clone(), source: self.source, key: Some(key.clone()), + runtime: self.runtime.clone(), })) } fn id(&self) -> EntityId { self.workspace.entity_id() } fn is_open(&self) -> bool { - self.workspace.upgrade().is_some() + self.current() && self.workspace.upgrade().is_some() } fn terminals(&self, cx: &App) -> Option> { + if !self.current() { + return None; + } let workspace = self.workspace.upgrade()?; let workspace = workspace.read(cx); - workspace.context_terminals(self.source, cx).ok() + let expected = self + .runtime + .as_ref() + .map(|runtime| runtime.environment().ok().cloned()) + .unwrap_or(Some(Default::default()))?; + Some( + workspace + .context_terminals(self.source, cx) + .ok()? + .into_iter() + .filter(|entry| { + entry.capabilities.state().info(cx).is_some_and(|info| { + info.source_current + && match (&expected, &info.source) { + ( + nocterm_ai::local_environment::LocalEnvironment::ThisComputer, + None, + ) => true, + ( + nocterm_ai::local_environment::LocalEnvironment::HubAccount( + expected, + ), + Some(actual), + ) => expected == actual, + _ => false, + } + }) + }) + .collect(), + ) } fn connections(&self, cx: &App) -> Vec { if self.terminals(cx).is_none() { @@ -84,6 +136,9 @@ impl ChatWorkspace for WorkspaceSeat { window: &mut Window, cx: &mut App, ) -> Option { + if !self.current() { + return None; + } let owner = self.workspace.upgrade()?; if self.source.context() == ContextId::General { let directory = owner.read(cx).connection_directory()?; diff --git a/crates/ai/Cargo.toml b/crates/ai/Cargo.toml index 756721e2..34db92af 100644 --- a/crates/ai/Cargo.toml +++ b/crates/ai/Cargo.toml @@ -9,8 +9,11 @@ layer = "domain" [features] test-support = [] [dependencies] +sha2.workspace = true +nocterm-hubs-model.workspace = true nocterm-settings.workspace = true nocterm-core.workspace = true +nocterm-transfers.workspace = true nocterm-project-model.workspace = true nocterm-session.workspace = true serde.workspace = true @@ -25,6 +28,8 @@ toml.workspace = true thiserror.workspace = true uuid = { version = "1", default-features = false } [dev-dependencies] +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } tempfile.workspace = true [lints] workspace = true diff --git a/crates/ai/src/agents.rs b/crates/ai/src/agents.rs index a743bee4..7451288a 100644 --- a/crates/ai/src/agents.rs +++ b/crates/ai/src/agents.rs @@ -16,5 +16,5 @@ mod tests; pub use available::{AgentConfigWarning, AgentSource, AvailableAgent, AvailableAgents}; pub use catalog::{AgentCatalog, CatalogError}; -pub use launch::{AgentLaunch, valid_id}; -pub use profile::{AccountUsage, AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; +pub use launch::{AgentLaunch, HUB_AGENT_PREFIX, HubAgentRef, is_hub_agent, valid_id}; +pub use profile::{AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; diff --git a/crates/ai/src/agents/available.rs b/crates/ai/src/agents/available.rs index ccff1ea2..0e33196e 100644 --- a/crates/ai/src/agents/available.rs +++ b/crates/ai/src/agents/available.rs @@ -9,6 +9,8 @@ pub enum AgentSource { Builtin, /// A command the user added in settings. Custom, + /// An agent of a hub the user is signed in to; it runs there. + Hub, } /// An agent configuration that was skipped; the agent is not offered. @@ -45,6 +47,10 @@ impl AvailableAgent { pub fn profile(&self) -> Option<&AgentProfile> { self.profile.as_ref() } + /// The hub whose agent this is; `None` for an agent of this computer. + pub fn hub(&self) -> Option<&nocterm_hubs_model::HubId> { + self.launch.hub.as_ref().map(|agent| &agent.hub) + } } /// The agents that can be started under the current settings, by id. @@ -88,10 +94,35 @@ impl AvailableAgents { Self { agents, warnings } } + /// These agents and the hub agents of `launches` (launches without a hub are ignored). + /// A hub agent never replaces an agent of this computer. + #[must_use] + pub fn with_hub_agents(mut self, launches: impl IntoIterator) -> Self { + for launch in launches.into_iter().filter(|launch| launch.hub.is_some()) { + self.agents + .entry(launch.id.clone()) + .or_insert(AvailableAgent { + launch, + source: AgentSource::Hub, + profile: None, + }); + } + self + } + pub fn get(&self, id: &str) -> Option<&AvailableAgent> { self.agents.get(id) } + /// The agents a window offers in its new-chat menu: every agent of this computer, and + /// the agents of the window's `hub` only. A window of this computer offers no hub agent. + #[must_use] + pub fn offered_in(mut self, hub: Option<&nocterm_hubs_model::HubId>) -> Self { + self.agents + .retain(|_, agent| agent.hub().is_none() || agent.hub() == hub); + self + } + /// Agents in id order. pub fn iter(&self) -> impl Iterator { self.agents.values() @@ -139,6 +170,7 @@ fn apply( args: Vec::new(), env: BTreeMap::new(), inherit_env: Vec::new(), + hub: None, }; (launch, AgentSource::Custom) }); diff --git a/crates/ai/src/agents/launch.rs b/crates/ai/src/agents/launch.rs index 7c565daa..2e268b05 100644 --- a/crates/ai/src/agents/launch.rs +++ b/crates/ai/src/agents/launch.rs @@ -1,6 +1,7 @@ +use nocterm_hubs_model::HubId; use std::collections::BTreeMap; -/// How to start one agent process. +/// How to start one agent: a local process, or an agent of a hub. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct AgentLaunch { pub id: String, @@ -9,9 +10,34 @@ pub struct AgentLaunch { pub args: Vec, pub env: BTreeMap, pub inherit_env: Vec, + /// The hub agent this launch reaches instead of a process; the process fields are empty. + pub hub: Option, +} + +/// An agent that runs on a hub: its chats live there and keep running while no client is +/// open. The hub connector reaches it; no process starts on this computer. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct HubAgentRef { + pub hub: HubId, + /// The hub's identifier of the agent. + pub agent: String, } impl AgentLaunch { + /// The launch of agent `agent` of `hub`, named `name`. Its id is stable for the pair, so a + /// saved chat finds its agent again. + pub fn hub_agent(hub: HubId, agent: String, name: String) -> Self { + Self { + id: hub_agent_id(&hub, &agent), + name, + command: String::new(), + args: Vec::new(), + env: BTreeMap::new(), + inherit_env: Vec::new(), + hub: Some(HubAgentRef { hub, agent }), + } + } + /// Whether the command or an argument contains `marker`, such as the /// package name of an ACP adapter. pub fn mentions(&self, marker: &str) -> bool { @@ -21,6 +47,37 @@ impl AgentLaunch { } } +/// How the ids of hub agents begin. +pub const HUB_AGENT_PREFIX: &str = "hub-"; + +/// Whether `id` names a hub agent ([`AgentLaunch::hub_agent`]): its chats run on the hub. +pub fn is_hub_agent(id: &str) -> bool { + id.strip_prefix(HUB_AGENT_PREFIX).is_some_and(|hash| { + hash.len() == 24 + && hash + .bytes() + .all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f')) + }) +} + +/// [`HUB_AGENT_PREFIX`] and 24 hex digits of a hash of the pair: a valid agent id of at most +/// 64 characters. +fn hub_agent_id(hub: &HubId, agent: &str) -> String { + use sha2::{Digest as _, Sha256}; + let mut hash = Sha256::new(); + for part in [hub.to_string().as_bytes(), agent.as_bytes()] { + hash.update((part.len() as u64).to_be_bytes()); + hash.update(part); + } + let digest = hash.finalize(); + let mut id = String::from(HUB_AGENT_PREFIX); + for byte in &digest[..12] { + use std::fmt::Write as _; + let _ = write!(id, "{byte:02x}"); + } + id +} + /// Agent ids use letters, numbers, underscore or dash, up to 64 characters. pub fn valid_id(id: &str) -> bool { !id.is_empty() diff --git a/crates/ai/src/agents/profile.rs b/crates/ai/src/agents/profile.rs index ffeae8f7..f67d6248 100644 --- a/crates/ai/src/agents/profile.rs +++ b/crates/ai/src/agents/profile.rs @@ -44,31 +44,17 @@ pub enum UsageMeta { RateLimitRecord { key: &'static str }, } -/// How nocterm reads an agent's plan limits outside the protocol. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum AccountUsage { - /// Limits arrive with usage updates only. - #[default] - None, - /// The agent keeps an OAuth sign-in whose account reports limits. - OAuthAccount, - /// The agent logs limits to session files in its home. - SessionLog, -} - /// How an agent reports plan limits, and what to say while none are known. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct UsageProfile { meta: UsageMeta, - account: AccountUsage, empty_hint: Option<&'static str>, } impl UsageProfile { - pub const fn new(meta: UsageMeta, account: AccountUsage) -> Self { + pub const fn new(meta: UsageMeta) -> Self { Self { meta, - account, empty_hint: None, } } @@ -80,9 +66,6 @@ impl UsageProfile { pub fn meta(&self) -> UsageMeta { self.meta } - pub fn account(&self) -> AccountUsage { - self.account - } pub fn empty_hint(&self) -> Option<&'static str> { self.empty_hint } @@ -141,7 +124,7 @@ impl AgentProfile { launch, markers: &[], state: &[], - usage: UsageProfile::new(UsageMeta::None, AccountUsage::None), + usage: UsageProfile::new(UsageMeta::None), mark: None, } } @@ -207,6 +190,7 @@ impl AgentProfile { .iter() .map(|&name| name.into()) .collect(), + hub: None, } } } diff --git a/crates/ai/src/agents/tests.rs b/crates/ai/src/agents/tests.rs index 8c3f8534..30a66cf3 100644 --- a/crates/ai/src/agents/tests.rs +++ b/crates/ai/src/agents/tests.rs @@ -13,13 +13,10 @@ const ALPHA: AgentProfile = AgentProfile::new( .recognized_by(&["alpha-acp"]) .writes_home(&[".alpha", ".config/alpha"]) .with_usage( - UsageProfile::new( - UsageMeta::RateLimitRecord { - key: "_alpha/rateLimit", - }, - AccountUsage::OAuthAccount, - ) - .with_empty_hint("Alpha limits load from your sign-in."), + UsageProfile::new(UsageMeta::RateLimitRecord { + key: "_alpha/rateLimit", + }) + .with_empty_hint("Alpha has not reported plan limits through ACP."), ); const BETA: AgentProfile = AgentProfile::new("beta", "Beta", LaunchDefaults::new("beta", &["acp"], &[])); @@ -195,3 +192,34 @@ fn catalog_finds_profiles_by_id_then_by_marker() { [".alpha", ".config/alpha"] ); } + +#[test] +fn a_hub_window_offers_its_hub_agents_and_a_local_window_none() { + use nocterm_hubs_model::{HubAddress, HubId}; + let hub = |text: &str| HubId::from(HubAddress::parse(text).unwrap()); + let (work, home) = (hub("work.example:7443"), hub("home.example:7443")); + let agents = AvailableAgents::resolve(&catalog(), &AiSettings::default()).with_hub_agents([ + AgentLaunch::hub_agent(work.clone(), "a1".into(), "Ops".into()), + AgentLaunch::hub_agent(home.clone(), "a1".into(), "Ops".into()), + ]); + let names = |hub: Option<&HubId>| { + agents + .clone() + .offered_in(hub) + .iter() + .map(|agent| (agent.name().to_owned(), agent.hub().cloned())) + .collect::>() + }; + let local = names(None).len(); + let offered = names(Some(&work)); + assert_eq!(offered.len(), local + 1); + assert!(offered.contains(&("Ops".into(), Some(work.clone())))); + let offered = agents.clone().offered_in(Some(&work)); + let ops = offered.iter().find(|agent| agent.hub().is_some()).unwrap(); + assert_eq!(ops.source(), AgentSource::Hub); + assert!(super::valid_id(ops.id())); + assert_ne!( + AgentLaunch::hub_agent(work, "a1".into(), "x".into()).id, + AgentLaunch::hub_agent(home, "a1".into(), "x".into()).id + ); +} diff --git a/crates/ai/src/approval.rs b/crates/ai/src/approval.rs index c8d71380..b396fda5 100644 --- a/crates/ai/src/approval.rs +++ b/crates/ai/src/approval.rs @@ -4,27 +4,64 @@ use std::collections::BTreeSet; #[derive(Default)] pub struct ApprovalGrants { grants: BTreeSet<(String, ToolCapability)>, + /// Transfer grants belong to an unordered pair of terminals, so approving + /// `a`/`b` and `c`/`d` does not approve `a`/`d`. A same-terminal transfer is `(a, a)`. + transfer_pairs: BTreeSet<(String, String)>, +} +/// The unordered terminal pair a transfer moves files between. +fn transfer_pair(call: &TerminalCall) -> Option<(String, String)> { + let (source, destination) = (call.source_terminal_id()?, call.terminal_id()?); + let (first, second) = if source <= destination { + (source, destination) + } else { + (destination, source) + }; + Some((first.into(), second.into())) } impl ApprovalGrants { pub fn grant(&mut self, id: &str, capability: ToolCapability) { self.grants.insert((id.into(), capability)); } + /// Records what a card approval of `call` covers: a transfer grants its terminal + /// pair, every other call grants its capability on each terminal it touches. + pub fn grant_call(&mut self, call: &TerminalCall) { + if let Some(pair) = transfer_pair(call) { + self.transfer_pairs.insert(pair); + return; + } + for id in call.terminals().chain(call.server_id()) { + self.grant(id, call.capability()); + } + } pub fn revoke(&mut self, id: &str) { self.grants.retain(|(target, _)| target != id); + self.transfer_pairs + .retain(|(first, second)| first != id && second != id); } pub fn clear(&mut self) { self.grants.clear(); + self.transfer_pairs.clear(); } + /// A call needs a card unless the policy allows it or every terminal it + /// touches already holds the capability it needs. pub fn requires_approval(&self, call: &TerminalCall, settings: &ApprovalSettings) -> bool { - let Some(id) = call.target() else { + let mut targets = call.terminals().peekable(); + let server = call.server_id(); + if targets.peek().is_none() && server.is_none() { return false; - }; - if call.writes() { - settings.terminal_write == ApprovalPolicy::Ask - && !self.grants.contains(&(id.into(), call.capability())) - } else { - settings.terminal_read == ApprovalPolicy::Ask - && !self.grants.contains(&(id.into(), call.capability())) } + let policy = if call.writes() { + settings.terminal_write + } else { + settings.terminal_read + }; + let capability = call.capability(); + let covered = match transfer_pair(call) { + Some(pair) => self.transfer_pairs.contains(&pair), + None => targets + .chain(server) + .all(|id| self.grants.contains(&(id.into(), capability))), + }; + policy == ApprovalPolicy::Ask && !covered } } diff --git a/crates/ai/src/connection.rs b/crates/ai/src/connection.rs index b1b111bf..6bfc707f 100644 --- a/crates/ai/src/connection.rs +++ b/crates/ai/src/connection.rs @@ -198,6 +198,8 @@ pub type ConnectError = AgentError; #[derive(Debug, thiserror::Error)] pub enum BridgeRegistrationError { + #[error("The original account source is unavailable: {0}")] + AccountSource(#[from] nocterm_hubs_model::source_identity::LocalAccountSourceError), #[error("{0}")] Origin(#[from] crate::execution_origin::ExecutionOriginError), #[error("The chat is no longer open.")] @@ -230,6 +232,7 @@ pub struct BridgeRegistration { pub id: u64, launch: BridgeLaunch, revoke: Option>, + offer: Option>, } impl BridgeRegistration { pub fn new(id: u64, launch: BridgeLaunch, revoke: Arc) -> Self { @@ -237,6 +240,19 @@ impl BridgeRegistration { id, launch, revoke: Some(revoke), + offer: None, + } + } + /// A registration whose bridge lists the tools a chat offers, and tells its agent when they + /// change. + pub fn with_offer(mut self, offer: Arc) -> Self { + self.offer = Some(offer); + self + } + /// What the chat offers its agent now. Offering the same again changes nothing. + pub fn offer(&self, offer: crate::ToolOffer) { + if let Some(set) = &self.offer { + set(self.id, offer); } } pub fn launch(&self) -> &BridgeLaunch { diff --git a/crates/ai/src/context.rs b/crates/ai/src/context.rs index 7f2ce641..1c505700 100644 --- a/crates/ai/src/context.rs +++ b/crates/ai/src/context.rs @@ -21,6 +21,9 @@ pub struct TerminalDescriptor { pub connection: Option, pub cwd: Option, pub status: String, + /// The terminal serves files but runs no programs: the file tools reach its files. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub file_tools: bool, } /// A saved server attached to a chat that has no open session. An agent /// opens one in the background with the `open_terminal` tool. @@ -112,14 +115,23 @@ pub fn context_block(terminals: &[TerminalDescriptor], servers: &[ServerDescript server }) .collect(); - let json = if servers.is_empty() { + let files = safe.iter().any(|terminal| terminal.file_tools); + let json = if servers.is_empty() && !files { serde_json::to_string(&safe) } else { - serde_json::to_string(&serde_json::json!({ - "terminals": safe, - "offline_servers": servers, - "note": "Offline servers have no session yet. Call open_terminal with a server_id to connect in the background, then use the returned terminal_id.", - })) + let mut context = serde_json::json!({ "terminals": safe }); + if !servers.is_empty() { + context["offline_servers"] = serde_json::json!(servers); + context["note"] = serde_json::json!( + "Offline servers have no session yet. Call open_terminal with a server_id to connect in the background, then use the returned terminal_id." + ); + } + if files { + context["file_tools_note"] = serde_json::json!( + "Terminals with file_tools: true serve files but run no programs. Read and change their files with list_files, read_file, edit_file, write_file, make_directory, move_path and delete_path; those tools reach no other terminal." + ); + } + serde_json::to_string(&context) } .expect("descriptors serialize"); // Preserve JSON while escaping delimiters embedded in user-editable metadata. diff --git a/crates/ai/src/execution_origin.rs b/crates/ai/src/execution_origin.rs index aa472243..947574af 100644 --- a/crates/ai/src/execution_origin.rs +++ b/crates/ai/src/execution_origin.rs @@ -4,20 +4,50 @@ //! Ports provided: typed immutable scope validation. Settings/globals: none. //! Invariant: conversation membership never grants workspace tool authority. mod error; -use crate::history::ContextId; +use crate::{ + history::ContextId, + local_environment::{ChatScope, LocalEnvironment}, +}; pub use error::ExecutionOriginError; -use std::path::{Path, PathBuf}; +use std::{ + path::{Path, PathBuf}, + sync::Arc, +}; /// Descriptive document identity. Storage validates the ID at its boundary; /// possessing a key never grants access to a native resource or process. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct ChatKey { scope: ContextId, + environment: Arc, id: String, } impl ChatKey { pub fn new(scope: ContextId, id: String) -> Self { - Self { scope, id } + Self { + scope, + id, + environment: Arc::new(LocalEnvironment::ThisComputer), + } + } + pub fn from_header(header: &crate::history::SavedHeader) -> Self { + Self::in_environment(header.chat_scope(), header.id.clone()) + } + pub fn from_summary(summary: &crate::history::ChatSummary) -> Self { + Self::in_environment(summary.chat_scope(), summary.id.clone()) + } + pub fn in_environment(scope: ChatScope, id: String) -> Self { + Self { + scope: scope.context(), + environment: Arc::new(scope.environment().clone()), + id, + } + } + pub fn environment(&self) -> &LocalEnvironment { + &self.environment + } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment().clone(), self.scope) } pub fn scope(&self) -> ContextId { self.scope @@ -50,6 +80,12 @@ impl ExecutionOrigin { pub fn provider_resume(&self) -> Option<&Path> { self.provider_resume.as_deref() } + pub fn require_chat_scope(&self, expected: &ChatScope) -> Result<(), ExecutionOriginError> { + if self.key.environment() != expected.environment() { + return Err(ExecutionOriginError::EnvironmentMismatch); + } + self.require_scope(expected.context()) + } pub fn require_scope(&self, expected: ContextId) -> Result<(), ExecutionOriginError> { if self.scope() == expected { Ok(()) diff --git a/crates/ai/src/execution_origin/error.rs b/crates/ai/src/execution_origin/error.rs index 1d4137ab..2fff6bc8 100644 --- a/crates/ai/src/execution_origin/error.rs +++ b/crates/ai/src/execution_origin/error.rs @@ -1,5 +1,7 @@ #[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] pub enum ExecutionOriginError { + #[error("Execution origin belongs to another original environment")] + EnvironmentMismatch, #[error("Execution origin belongs to {actual:?}, but the consumer expects {expected:?}")] ScopeMismatch { expected: crate::history::ContextId, diff --git a/crates/ai/src/history.rs b/crates/ai/src/history.rs index 0014e0bc..339152b1 100644 --- a/crates/ai/src/history.rs +++ b/crates/ai/src/history.rs @@ -18,7 +18,7 @@ mod budget; mod catalog; mod codec; mod error; -pub use codec::{decode, encode, encode_shared, prepare, structural_bytes}; +pub use codec::{decode, encode, encode_shared, prepare, prepare_owned, structural_bytes}; pub use error::HistoryCodecError; mod decode; mod loading; @@ -39,12 +39,14 @@ pub use nocterm_project_model::ContextId; pub use prefix::{MAX_HEADER_BYTES, decode_catalog_header}; pub use version::TranscriptVersion; const VERSION: u32 = 2; +const ENVIRONMENT_VERSION: u32 = 3; const LEGACY_VERSION: u32 = 1; /// Images larger than this are left out of saved chats. const MAX_SAVED_IMAGE_BYTES: usize = 512 * 1024; const OMITTED_IMAGE_TEXT: &str = "[Image not kept in history]"; /// Only the most recent entries of a very long chat are kept. -const MAX_SAVED_ENTRIES: usize = 2_000; +/// Maximum persisted transcript rows, shared by pre-copy admission. +pub const MAX_SAVED_ENTRIES: usize = 2_000; /// Larger files are not read back. pub const MAX_FILE_BYTES: u64 = 32 * 1024 * 1024; /// At most this many chats are restored, newest first. @@ -143,6 +145,30 @@ impl SavedChat { } } + /// Explicit environment-scoped local documents use version three; no data is uploaded. + pub fn for_environment( + agent_id: String, + id: String, + updated: u64, + scope: crate::local_environment::ChatScope, + ) -> Self { + let mut chat = Self::for_scope(agent_id, id, updated, scope.context()); + chat.version = ENVIRONMENT_VERSION; + chat.header.environment = scope.environment().clone(); + chat + } + pub fn archive_draft_in_environment( + id: String, + agent_id: String, + text: String, + updated: u64, + scope: crate::local_environment::ChatScope, + ) -> Self { + let mut chat = Self::for_environment(agent_id, id, updated, scope); + chat.draft = (!text.is_empty()).then_some(text); + chat.archived_draft = true; + chat + } /// A draft edit made before the archived transcript has been hydrated. pub fn archive_draft(id: String, agent_id: String, text: String, updated: u64) -> Self { Self::archive_draft_in_scope( @@ -176,6 +202,7 @@ impl SavedChat { pub fn validate_identity(&self) -> Result<(), HistoryCodecError> { codec::validate_id(&self.header.id)?; header::resolve_scope(self.version, Some(self.header.scope))?; + header::validate_environment(self.version, &self.header.environment)?; Ok(()) } /// Reject a prompt before accepting it if it could not be read back. @@ -264,6 +291,24 @@ impl std::ops::Deref for SharedChat { &self.metadata } } + +/// Exact compact JSON byte size, without allocating the encoded payload. +/// This counts an already decoded value; it does not parse or authenticate input. +pub fn json_size(value: &impl serde::Serialize) -> Result { + size::encoded_len(value).map_err(HistoryCodecError::from) +} + +/// Conservative retained ownership cost, without allocating an encoded buffer. +/// Consumers must still bound native input before decoding or materializing it. +pub fn retained_value_bytes(value: &impl serde::Serialize) -> Result { + size::structural_size(value).map_err(HistoryCodecError::from) +} + +/// Exact JSON string size, using the same allocation-free codec counter. +pub fn text_size(text: &str) -> Result { + size::encoded_len(&text).map_err(HistoryCodecError::from) +} + pub fn content_size(content: &Content) -> Result { size::encoded_len(content).map_err(HistoryCodecError::from) } diff --git a/crates/ai/src/history/catalog.rs b/crates/ai/src/history/catalog.rs index 3587c558..007df674 100644 --- a/crates/ai/src/history/catalog.rs +++ b/crates/ai/src/history/catalog.rs @@ -16,6 +16,8 @@ struct Legacy { version: u32, #[serde(default)] scope: Option, + #[serde(default)] + environment: Option, id: String, agent_id: String, #[serde(default)] @@ -147,11 +149,13 @@ pub fn decode_summary( ) -> Result, HistoryCodecError> { super::check_size(bytes.len())?; super::preflight::check(bytes)?; - if bytes.starts_with(super::prefix::START) { + if super::prefix::is_scoped(bytes) { let header = super::decode_catalog_header(bytes, id)?; let envelope: ScopedSummary = serde_json::from_slice(bytes)?; - if envelope.version != super::VERSION - || envelope.chat.version != super::VERSION + if !matches!( + envelope.version, + super::VERSION | super::ENVIRONMENT_VERSION + ) || envelope.chat.version != envelope.version || envelope.catalog != header || envelope.chat.catalog(id)? != header { @@ -179,6 +183,17 @@ struct ScopedSummary { impl Legacy { fn catalog(self, id: &str) -> Result { let scope = super::header::resolve_scope(self.version, self.scope)?; + let environment = self + .environment + .ok_or(HistoryCodecError::MissingEnvironment) + .or_else(|error| { + if self.version <= 2 { + Ok(crate::local_environment::LocalEnvironment::ThisComputer) + } else { + Err(error) + } + })?; + super::header::validate_environment(self.version, &environment)?; if self.id != id { return Err(HistoryCodecError::IdentityMismatch); } @@ -189,6 +204,7 @@ impl Legacy { id: self.id, agent_id: self.agent_id, scope, + environment, title: self.title.or(self.entries.title), name: self.name, pinned: self.pinned, @@ -200,6 +216,7 @@ impl Legacy { &super::catalog_header::Preview::for_title(None, content), None, false, + self.version, )) } } diff --git a/crates/ai/src/history/catalog_header.rs b/crates/ai/src/history/catalog_header.rs index 4fbcda20..19c65f02 100644 --- a/crates/ai/src/history/catalog_header.rs +++ b/crates/ai/src/history/catalog_header.rs @@ -1,5 +1,6 @@ //! Bounded authoritative discovery facts live before the transcript payload. use super::{ContextId, FileStamp, SavedChat, SavedHeader, TranscriptVersion}; +use crate::local_environment::{ChatScope, LocalEnvironment}; use crate::{model::content::Content, thread::Entry}; use serde::{Deserialize, Serialize}; @@ -10,6 +11,8 @@ pub struct CatalogHeader { id: String, agent_id: String, scope: ContextId, + #[serde(default, skip_serializing_if = "Option::is_none")] + environment: Option, title: Option, name: Option, pinned: bool, @@ -24,13 +27,37 @@ impl CatalogHeader { pub fn scope(&self) -> ContextId { self.scope } + pub fn environment(&self) -> LocalEnvironment { + self.environment.clone().unwrap_or_default() + } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment(), self.scope) + } + pub(super) fn require_version(&self, version: u32) -> Result<(), super::HistoryCodecError> { + if (version == super::ENVIRONMENT_VERSION) != self.environment.is_some() { + return Err(super::HistoryCodecError::InvalidHeader); + } + super::header::validate_environment(version, &self.environment()) + } + pub fn transcript_version(&self) -> TranscriptVersion { + if self.environment.is_some() { + TranscriptVersion::ENVIRONMENT + } else { + TranscriptVersion::SCOPED + } + } pub fn summary(&self, stamp: FileStamp) -> Option { (self.has_content || self.name.is_some() || self.pinned).then(|| self.to_summary(stamp)) } pub(super) fn to_summary(&self, stamp: FileStamp) -> ChatSummary { ChatSummary { cache_version: 3, - transcript_version: TranscriptVersion::SCOPED, + transcript_version: if self.environment.is_some() { + TranscriptVersion::ENVIRONMENT + } else { + TranscriptVersion::SCOPED + }, + environment: self.environment(), id: self.id.clone(), agent_id: self.agent_id.clone(), scope: self.scope, @@ -52,6 +79,7 @@ impl CatalogHeader { &Preview::new(&chat.entries[start..]), chat.draft.as_deref(), queued || !chat.queue.is_empty(), + chat.version, ) } pub(super) fn for_persistence(chat: &SavedChat, queued: bool) -> Self { @@ -62,6 +90,7 @@ impl CatalogHeader { &Preview::persisted(&chat.entries[start..]), chat.draft.as_deref(), queued || !chat.queue.is_empty(), + chat.version, ) } pub(super) fn from_parts( @@ -70,11 +99,13 @@ impl CatalogHeader { preview: &Preview, draft: Option<&str>, queued: bool, + version: u32, ) -> Self { Self { id: saved.id.clone(), agent_id: bounded(&saved.agent_id), scope: saved.scope, + environment: (version == super::ENVIRONMENT_VERSION).then(|| saved.environment.clone()), title: saved .title .as_deref() @@ -168,6 +199,8 @@ pub struct ChatSummary { pub id: String, pub agent_id: String, pub scope: ContextId, + #[serde(default, skip_serializing_if = "LocalEnvironment::is_this_computer")] + pub environment: LocalEnvironment, pub title: Option, pub name: Option, pub pinned: bool, @@ -182,12 +215,17 @@ impl ChatSummary { /// This does not validate an unread transcript payload or issue authority. pub fn validate(&self) -> Result<(), super::HistoryCodecError> { self.transcript_version.require_scope(self.scope)?; + self.transcript_version + .require_environment(&self.environment)?; validate_metadata( &self.id, &self.agent_id, [&self.title, &self.name, &self.model], ) } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment.clone(), self.scope) + } pub fn transcript_version(&self) -> TranscriptVersion { self.transcript_version } diff --git a/crates/ai/src/history/codec.rs b/crates/ai/src/history/codec.rs index 049b9c52..426efc97 100644 --- a/crates/ai/src/history/codec.rs +++ b/crates/ai/src/history/codec.rs @@ -24,6 +24,7 @@ pub fn decode(bytes: &[u8], id: &str) -> Result { pub fn prepare(chat: SavedChat) -> Result { validate_id(&chat.header.id)?; super::header::resolve_scope(chat.version, Some(chat.header.scope))?; + super::header::validate_environment(chat.version, &chat.header.environment)?; if chat.archived_draft { return Err(HistoryCodecError::ArchivedPatch); } @@ -35,6 +36,36 @@ pub fn prepare(chat: SavedChat) -> Result { let bytes = serde_json::to_vec(&chat)?; decode(&bytes, &chat.header.id) } +/// Validate and prepare the same owned decoded body. This avoids serializing +/// and decoding a second full transcript merely to create its cached budgets. +pub fn prepare_owned(mut chat: SavedChat) -> Result { + validate_id(&chat.header.id)?; + super::header::resolve_scope(chat.version, Some(chat.header.scope))?; + super::header::validate_environment(chat.version, &chat.header.environment)?; + if chat.archived_draft { + return Err(HistoryCodecError::ArchivedPatch); + } + chat.validate_collections()?; + if chat.entries.len() > super::MAX_SAVED_ENTRIES || chat.times.len() > super::MAX_SAVED_ENTRIES + { + return Err(HistoryCodecError::CollectionLimit); + } + let resident = { + let value = wire::Wire::complete(&chat); + // Preserve decode's public refusal ordering: encoded limit before the + // structural limit, using the existing allocation-free counters. + check_size(super::size::encoded_len(&value).map_err(HistoryCodecError::from)?)?; + let resident = super::size::structural_size(&value).map_err(HistoryCodecError::from)?; + super::check_memory(resident)?; + resident + }; + super::CatalogHeader::from_chat(&chat, false).validate(&chat.header.id)?; + let mut prepared = PreparedBudget::new(&chat)?; + prepared.resident_bytes = resident; + chat.prepared = Some(prepared); + Ok(chat) +} + pub fn encode(chat: &SavedChat) -> Result, HistoryCodecError> { if chat.archived_draft { return Err(HistoryCodecError::ArchivedPatch); @@ -70,6 +101,7 @@ fn encode_value( ) -> Result, HistoryCodecError> { validate_id(&chat.header.id)?; super::header::resolve_scope(chat.version, Some(chat.header.scope))?; + super::header::validate_environment(chat.version, &chat.header.environment)?; let bytes = serde_json::to_vec(value)?; check_size(bytes.len())?; preflight::check(&bytes)?; diff --git a/crates/ai/src/history/error.rs b/crates/ai/src/history/error.rs index 5379ea29..a21809da 100644 --- a/crates/ai/src/history/error.rs +++ b/crates/ai/src/history/error.rs @@ -13,6 +13,10 @@ pub enum HistoryCodecError { MissingScope, #[error("Version-one chats cannot claim project ownership")] LegacyScope, + #[error("A version-three local chat requires an explicit environment")] + MissingEnvironment, + #[error("Legacy chats belong only to this computer")] + LegacyEnvironment, #[error("The saved chat requires its authoritative catalog prefix")] MissingCatalog, #[error("The saved catalog prefix is malformed or noncanonical")] diff --git a/crates/ai/src/history/header.rs b/crates/ai/src/history/header.rs index abb97221..8728c991 100644 --- a/crates/ai/src/history/header.rs +++ b/crates/ai/src/history/header.rs @@ -1,4 +1,5 @@ //! Durable identity and catalog metadata, independent of transcript payloads. +use crate::local_environment::{ChatScope, LocalEnvironment}; use nocterm_project_model::ContextId; use serde::{Deserialize, ser::SerializeMap}; @@ -19,6 +20,7 @@ pub struct SavedHeader { pub updated: u64, /// Durable chat membership, independent of names, paths and current selection. pub scope: ContextId, + pub environment: LocalEnvironment, } impl SavedHeader { pub fn new(agent_id: String, id: String, updated: u64, scope: ContextId) -> Self { @@ -30,8 +32,12 @@ impl SavedHeader { pinned: false, updated, scope, + environment: LocalEnvironment::ThisComputer, } } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment.clone(), self.scope) + } pub(super) fn serialize_prefix(&self, map: &mut M) -> Result<(), M::Error> { map.serialize_entry("id", &self.id)?; map.serialize_entry("agent_id", &self.agent_id)?; @@ -61,10 +67,23 @@ pub(super) struct IncomingHeader { updated: u64, #[serde(default)] scope: Option, + #[serde(default)] + environment: Option, } impl IncomingHeader { pub(super) fn finish(self, version: u32) -> Result { let scope = resolve_scope(version, self.scope)?; + let environment = self + .environment + .ok_or(super::HistoryCodecError::MissingEnvironment) + .or_else(|error| { + if version <= 2 { + Ok(LocalEnvironment::ThisComputer) + } else { + Err(error) + } + })?; + validate_environment(version, &environment)?; Ok(SavedHeader { id: self.id, agent_id: self.agent_id, @@ -73,6 +92,7 @@ impl IncomingHeader { pinned: self.pinned, updated: self.updated, scope, + environment, }) } } @@ -84,8 +104,21 @@ pub(super) fn resolve_scope( match (version, scope) { (LEGACY_VERSION, None | Some(ContextId::General)) => Ok(ContextId::General), (LEGACY_VERSION, Some(ContextId::Project(_))) => Err(HistoryCodecError::LegacyScope), - (VERSION, Some(scope)) => Ok(scope), - (VERSION, None) => Err(HistoryCodecError::MissingScope), + (VERSION | super::ENVIRONMENT_VERSION, Some(scope)) => Ok(scope), + (VERSION | super::ENVIRONMENT_VERSION, None) => Err(HistoryCodecError::MissingScope), (version, _) => Err(HistoryCodecError::UnsupportedVersion(version)), } } + +pub(super) fn validate_environment( + version: u32, + environment: &LocalEnvironment, +) -> Result<(), super::HistoryCodecError> { + match version { + 1 | 2 if !environment.is_this_computer() => { + Err(super::HistoryCodecError::LegacyEnvironment) + } + 1..=3 => Ok(()), + other => Err(super::HistoryCodecError::UnsupportedVersion(other)), + } +} diff --git a/crates/ai/src/history/incoming.rs b/crates/ai/src/history/incoming.rs index e86b6346..440595a6 100644 --- a/crates/ai/src/history/incoming.rs +++ b/crates/ai/src/history/incoming.rs @@ -65,7 +65,7 @@ struct IncomingEnvelope { } impl IncomingEnvelope { fn finish(self) -> Result { - if self.version != super::VERSION { + if !matches!(self.version, super::VERSION | super::ENVIRONMENT_VERSION) { return Err(HistoryCodecError::UnsupportedVersion(self.version)); } let chat = self.chat.finish()?; @@ -86,7 +86,7 @@ fn legacy(chat: IncomingChat) -> Result { Ok(chat) } pub(super) fn decode_bytes(bytes: &[u8], id: &str) -> Result { - if bytes.starts_with(super::prefix::START) { + if super::prefix::is_scoped(bytes) { super::decode_catalog_header(bytes, id)?; serde_json::from_slice::(bytes)?.finish() } else { @@ -145,7 +145,7 @@ pub(super) fn deserialize<'de, D: serde::Deserializer<'de>>( let version = (key == "version") .then(|| map.next_value::()) .transpose()?; - let scoped = version == Some(super::VERSION); + let scoped = matches!(version, Some(super::VERSION | super::ENVIRONMENT_VERSION)); let replay = MapAccessDeserializer::new(ReplayMap { key: Some(key), value: version, diff --git a/crates/ai/src/history/prefix.rs b/crates/ai/src/history/prefix.rs index a56cae60..86f5889c 100644 --- a/crates/ai/src/history/prefix.rs +++ b/crates/ai/src/history/prefix.rs @@ -2,6 +2,10 @@ use super::{CatalogHeader, HistoryCodecError}; pub const MAX_HEADER_BYTES: usize = 16 * 1024; pub(super) const START: &[u8] = b"{\"version\":2,\"catalog\":"; +const ENVIRONMENT_START: &[u8] = b"{\"version\":3,\"catalog\":"; +pub(super) fn is_scoped(bytes: &[u8]) -> bool { + bytes.starts_with(START) || bytes.starts_with(ENVIRONMENT_START) +} pub(super) const PAYLOAD: &[u8] = b",\"chat\":"; /// Validate only the authoritative v2 prefix. Full hydration additionally checks @@ -10,9 +14,13 @@ pub fn decode_catalog_header(bytes: &[u8], id: &str) -> Result(); let header = stream .next() @@ -33,5 +41,6 @@ pub fn decode_catalog_header(bytes: &[u8], id: &str) -> Result(changed).is_err()); } + +mod environments; + +#[test] +fn owned_preparation_preserves_the_body_and_matches_existing_format_validation() { + let legacy = decode(LEGACY, ID).unwrap(); + let scoped = SavedChat::for_identity("agent".into(), ID.into(), 10); + let environment = SavedChat::for_environment( + "agent".into(), + ID.into(), + 10, + crate::local_environment::ChatScope::new( + crate::local_environment::LocalEnvironment::ThisComputer, + ContextId::General, + ), + ); + for mut chat in [legacy, scoped, environment] { + chat.entries + .push(crate::thread::Entry::Agent("kept owned body".into())); + let expected = prepare(chat.clone()).unwrap(); + let rows = chat.entries.as_ptr(); + let actual = prepare_owned(chat).unwrap(); + assert_eq!(actual.entries.as_ptr(), rows); + assert_eq!(encode(&actual).unwrap(), encode(&expected).unwrap()); + assert_eq!(actual.resident_bytes(), expected.resident_bytes()); + } + let invalid = SavedChat::for_identity("agent".into(), "invalid".into(), 1); + assert!(matches!( + prepare_owned(invalid), + Err(HistoryCodecError::InvalidId(_)) + )); + let archived = SavedChat::archive_draft(ID.into(), "agent".into(), "draft".into(), 1); + assert!(matches!( + prepare_owned(archived), + Err(HistoryCodecError::ArchivedPatch) + )); + let mut oversized = SavedChat::for_identity("agent".into(), ID.into(), 1); + oversized.entries = vec![crate::thread::Entry::Agent(String::new()); MAX_SAVED_ENTRIES + 1]; + assert!(matches!( + prepare_owned(oversized), + Err(HistoryCodecError::CollectionLimit) + )); +} diff --git a/crates/ai/src/history/scope_tests/environments.rs b/crates/ai/src/history/scope_tests/environments.rs new file mode 100644 index 00000000..4994f66b --- /dev/null +++ b/crates/ai/src/history/scope_tests/environments.rs @@ -0,0 +1,195 @@ +use super::*; +use crate::{ + execution_origin::ChatKey, + local_environment::{ChatScope, LocalEnvironment, LocalEnvironmentAccess}, +}; +use nocterm_hubs_model::{ + HubAddress, HubId, + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LegacyConfiguredAccount, + LocalAccountPartition, LocalAccountSourceFactory, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, +}; +use std::sync::Arc; + +fn partition(authority: u8, account_key: u8) -> LocalAccountPartition { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("same-instance-uuid").unwrap(), + Fingerprint::from_bytes([authority; 32]), + ), + AccountIdentity::new( + NamespaceId::new("same-account-uuid").unwrap(), + Fingerprint::from_bytes([account_key; 32]), + ), + )) +} +fn scope(authority: u8, key: u8) -> ChatScope { + ChatScope::new( + LocalEnvironment::HubAccount(partition(authority, key)), + ContextId::General, + ) +} +fn record(scope: ChatScope) -> SavedChat { + let mut chat = SavedChat::for_environment("agent".into(), ID.into(), 1, scope); + chat.entries + .push(crate::thread::Entry::Agent("saved reply".into())); + chat +} + +#[test] +fn environment_and_project_are_independent_coordinates_of_chat_identity() { + let a = ChatKey::in_environment(scope(1, 2), ID.into()); + let b = ChatKey::in_environment(scope(9, 2), ID.into()); + let c = ChatKey::in_environment(scope(1, 9), ID.into()); + let project: ContextId = serde_json::from_value(project()).unwrap(); + let project = + ChatKey::in_environment(ChatScope::new(a.environment().clone(), project), ID.into()); + assert_ne!(a, b); + assert_ne!(a, c); + assert_ne!(a, project); + assert_eq!(a.scope(), ContextId::General); + assert_ne!(a.environment().storage_key(), b.environment().storage_key()); + assert_ne!(a.environment().storage_key(), c.environment().storage_key()); + assert_eq!(a.environment().storage_key().len(), 64); +} + +#[test] +fn version_three_has_matching_bounded_environment_header_and_full_body() { + for scope in [scope(1, 2), ChatScope::this_computer(ContextId::General)] { + let chat = record(scope.clone()); + let bytes = encode(&chat).unwrap(); + assert!(bytes.starts_with(b"{\"version\":3,\"catalog\":")); + let header = decode_catalog_header(&bytes, ID).unwrap(); + assert_eq!(header.chat_scope(), scope); + assert_eq!(header.transcript_version(), TranscriptVersion::ENVIRONMENT); + let decoded = decode(&bytes, ID).unwrap(); + assert_eq!(decoded.header.chat_scope(), scope); + let summary = decode_summary( + &bytes, + ID, + FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 0, + }, + ) + .unwrap() + .unwrap(); + assert_eq!(summary.chat_scope(), scope); + assert_eq!(summary.transcript_version(), TranscriptVersion::ENVIRONMENT); + let value: Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!( + value["catalog"]["environment"], + value["chat"]["environment"] + ); + assert_eq!(encode(&prepare(decoded).unwrap()).unwrap(), bytes); + } +} + +#[test] +fn version_three_refuses_missing_unknown_moved_or_mismatched_environment() { + let valid = serde_json::to_value(record(scope(1, 2))).unwrap(); + let mut missing = valid.clone(); + missing["catalog"] + .as_object_mut() + .unwrap() + .remove("environment"); + let mut missing_body = valid.clone(); + missing_body["chat"] + .as_object_mut() + .unwrap() + .remove("environment"); + let mut changed = valid.clone(); + changed["chat"]["environment"] = serde_json::to_value(scope(9, 2).environment()).unwrap(); + let mut unknown = valid.clone(); + unknown["catalog"]["environment"] = json!({"kind":"future"}); + let mut malformed = valid.clone(); + malformed["chat"]["environment"] = + json!({"kind":"hub_account","account":{"kind":"stable","identity":{}}}); + for value in [missing, missing_body, changed, unknown, malformed] { + let bytes = serde_json::to_vec(&value).unwrap(); + assert!(decode(&bytes, ID).is_err()); + assert!( + decode_summary( + &bytes, + ID, + FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 0 + } + ) + .is_err() + ); + } +} + +#[test] +fn legacy_versions_never_acquire_hub_account_membership() { + let original = decode(LEGACY, ID).unwrap(); + assert!(original.header.environment.is_this_computer()); + assert_eq!(encode(&original).unwrap(), LEGACY); + for version in [1, 2] { + let mut moved = record(scope(1, 2)); + moved.version = version; + assert!(matches!( + encode(&moved), + Err(HistoryCodecError::LegacyEnvironment) + )); + } + let mut version_two = SavedChat::for_identity("agent".into(), ID.into(), 1); + version_two + .entries + .push(crate::thread::Entry::Agent("local".into())); + let value = serde_json::to_value(&version_two).unwrap(); + assert!(value["catalog"].get("environment").is_none()); + assert_eq!( + decode(&encode(&version_two).unwrap(), ID) + .unwrap() + .header + .environment, + LocalEnvironment::ThisComputer + ); +} + +#[test] +fn new_capture_after_unlock_never_revives_an_old_a_to_b_to_a_lease() { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + assert!(factory.capture().is_err()); + factory.sign_in(partition(1, 2)); + let original = LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap(); + let refreshed = LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap(); + assert_eq!(original.environment(), refreshed.environment()); + assert!(original.check_current().is_ok()); + factory.sign_in(partition(1, 9)); + assert!(original.check_current().is_err()); + factory.sign_in(partition(1, 2)); + let reopened = LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap(); + assert_eq!(original.environment(), reopened.environment()); + assert!(original.check_current().is_err()); + assert!(reopened.check_current().is_ok()); + factory.close(); + assert!(reopened.check_current().is_err()); +} + +#[test] +fn legacy_configured_partition_is_explicit_and_does_not_claim_stable_alias_identity() { + let legacy = |host: &str| { + LocalEnvironment::HubAccount(LocalAccountPartition::LegacyConfigured( + LegacyConfiguredAccount::new( + HubId::from(HubAddress::parse(host).unwrap()), + NamespaceId::new("account").unwrap(), + Fingerprint::from_bytes([8; 32]), + ), + )) + }; + assert_eq!(legacy("https://HUB.home/"), legacy("hub.home:7443")); + assert_ne!(legacy("hub.home"), legacy("alias.home")); + assert_ne!( + legacy("hub.home").storage_key(), + scope(1, 2).environment().storage_key() + ); + let mut value = serde_json::to_value(scope(1, 2).environment()).unwrap(); + value["account"]["identity"]["hub"]["instance"] = json!("x".repeat(129)); + assert!(serde_json::from_value::(value).is_err()); +} diff --git a/crates/ai/src/history/version.rs b/crates/ai/src/history/version.rs index fa30f576..8bc86abe 100644 --- a/crates/ai/src/history/version.rs +++ b/crates/ai/src/history/version.rs @@ -7,6 +7,7 @@ use serde::{Deserialize, Serialize}; pub struct TranscriptVersion(u32); impl TranscriptVersion { pub const LEGACY: Self = Self(super::LEGACY_VERSION); + pub const ENVIRONMENT: Self = Self(super::ENVIRONMENT_VERSION); pub const SCOPED: Self = Self(super::VERSION); pub const fn wire(self) -> u32 { self.0 @@ -14,6 +15,12 @@ impl TranscriptVersion { pub(super) fn observed(version: u32) -> Self { Self(version) } + pub(super) fn require_environment( + self, + environment: &crate::local_environment::LocalEnvironment, + ) -> Result<(), HistoryCodecError> { + super::header::validate_environment(self.0, environment) + } pub(super) fn require_scope(self, scope: ContextId) -> Result<(), HistoryCodecError> { super::header::resolve_scope(self.0, Some(scope)).map(|_| ()) } @@ -22,7 +29,9 @@ impl TryFrom for TranscriptVersion { type Error = HistoryCodecError; fn try_from(version: u32) -> Result { match version { - super::LEGACY_VERSION | super::VERSION => Ok(Self(version)), + super::LEGACY_VERSION | super::VERSION | super::ENVIRONMENT_VERSION => { + Ok(Self(version)) + } other => Err(HistoryCodecError::UnsupportedVersion(other)), } } diff --git a/crates/ai/src/history/wire.rs b/crates/ai/src/history/wire.rs index 64834f4e..cf3bc0e6 100644 --- a/crates/ai/src/history/wire.rs +++ b/crates/ai/src/history/wire.rs @@ -92,6 +92,8 @@ impl Serialize for Wire<'_, Q> { fn serialize(&self, serializer: S) -> Result { super::header::resolve_scope(self.version, Some(self.header.scope)) .map_err(serde::ser::Error::custom)?; + super::header::validate_environment(self.version, &self.header.environment) + .map_err(serde::ser::Error::custom)?; if self.version == super::LEGACY_VERSION { return Flat(self).serialize(serializer); } @@ -101,6 +103,7 @@ impl Serialize for Wire<'_, Q> { &self.entries.preview, self.draft.as_deref(), self.queue.is_some(), + self.version, ); if super::size::encoded_len(&catalog).map_err(serde::ser::Error::custom)? + super::prefix::START.len() @@ -125,6 +128,9 @@ impl Serialize for Flat<'_, '_, Q> { let mut map = serializer.serialize_map(None)?; map.serialize_entry("version", &wire.version)?; wire.header.serialize_prefix(&mut map)?; + if wire.version == super::ENVIRONMENT_VERSION { + map.serialize_entry("environment", &wire.header.environment)?; + } if wire.version != super::LEGACY_VERSION { map.serialize_entry("scope", &wire.header.scope)?; } diff --git a/crates/ai/src/images.rs b/crates/ai/src/images.rs index c057a100..6867f3c5 100644 --- a/crates/ai/src/images.rs +++ b/crates/ai/src/images.rs @@ -28,6 +28,38 @@ pub enum PromptImageError { #[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] #[error("At most 8 images and 20 MiB total")] pub struct ImageCollectionError; +/// Remaining admission for one image operation, captured before native reads/decodes. +#[derive(Clone, Copy, Debug)] +pub struct ImageCollectionBudget { + count: usize, + bytes: usize, +} +impl ImageCollectionBudget { + pub fn remaining(images: &[PromptImage]) -> Result { + validate_collection(images)?; + Ok(Self { + count: images.len(), + bytes: images.iter().map(|image| image.bytes().len()).sum(), + }) + } + pub fn check_count(self, incoming: usize) -> Result<(), ImageCollectionError> { + if incoming > MAX_IMAGES.saturating_sub(self.count) { + Err(ImageCollectionError) + } else { + Ok(()) + } + } + /// Refuses before decoding or encoding the incoming image. + pub fn admit(&mut self, bytes: usize) -> Result<(), ImageCollectionError> { + self.check_count(1)?; + if bytes > MAX_TOTAL_IMAGE_BYTES.saturating_sub(self.bytes) { + return Err(ImageCollectionError); + } + self.count += 1; + self.bytes += bytes; + Ok(()) + } +} #[derive(Clone, Debug)] pub struct PromptImage { mime_type: String, @@ -68,6 +100,10 @@ impl PromptImage { pub fn bytes(&self) -> &[u8] { &self.data } + /// Shared immutable source for retained background decoding and preview identity. + pub fn shared_bytes(&self) -> std::sync::Arc<[u8]> { + self.data.clone() + } pub fn mime_type(&self) -> &str { &self.mime_type } diff --git a/crates/ai/src/lib.rs b/crates/ai/src/lib.rs index 3ba5bf4b..47022ae3 100644 --- a/crates/ai/src/lib.rs +++ b/crates/ai/src/lib.rs @@ -3,17 +3,20 @@ //! Owns: neutral `model` records (the only agent vocabulary other crates see), agent //! profiles, catalog and settings resolution (`agents`), configuration rules, transcript reduction, session/tool rules, pure sandbox policy, //! bounded history codecs, immutable scoped execution origins and capability/storage ports. +//! The terminal tool catalog includes file transfers and shared agent-facing terminal, +//! command, observation and transfer reports; display data never grants execution authority. +//! File tools for terminals that run no programs are pure rules over a captured `RemoteFs`; +//! catalog groups and the chat's `ToolOffer` decide which tools the bridge lists and admits. +//! Image data is immutable and shared across projections; live transcript admission uses incremental pre-mutation costs. //! Does not own: per-chat configuration/composer state, native workspace/sandbox lookup, //! history/favorites files or persistence scheduling; native capabilities and repositories are adapters. //! Ports used: none for history codecs. Ports provided: metadata::ChatMetadataSource, //! AgentConnector, ToolBridge, -//! AgentStateRepository, ChatRepository, usage::AccountUsageSource, +//! AgentStateRepository, ChatRepository and bounded PreparedChatLoad, //! workspace::WorkdirResolver, sandbox::SandboxProbe and //! images::{ImageInspector, ImageThumbnailer}. //! Image preflight and encoded admission are pure; native decoding is injected. //! ImageCodecCause reuses core shared causes without exposing a native codec type. -//! Account usage diagnostics retain typed native I/O causes through foundation SharedError; -//! narrow domain constructors preserve operation category and path. //! Workspace preparation and sandbox spawn diagnostics retain typed shared native I/O; //! adapter constructors preserve classification without changing policy or mount authority. //! Settings section: ai. Globals: none. @@ -22,7 +25,9 @@ //! Native chat allocation and wall-clock facts are provided by ChatMetadataSource. //! No agent protocol SDK type appears in this crate; `nocterm-acp` maps the SDK to neutral records. //! Legacy transcripts preserve their version-one bytes. Newly framed documents use version -//! two with checked explicit context ownership; saved ownership never grants execution access. +//! three with explicit device/account environment and independent context ownership. +//! LocalAccountSource leases are captured from an authenticated native owner; persistence identity +//! never grants execution access. Version-two records remain ThisComputer only. //! Agent launch settings validation is pure; native text parsing and page controls belong to Agent. //! No concrete agent is named here: built-in profiles are injected as an `AgentCatalog`. pub mod agents; @@ -35,8 +40,10 @@ pub mod execution_origin; pub mod favorites; pub mod history; pub mod images; +pub mod local_environment; pub mod mcp; pub mod metadata; +pub mod prompt_files; pub mod redact; pub mod sandbox; pub mod session; diff --git a/crates/ai/src/local_environment.rs b/crates/ai/src/local_environment.rs new file mode 100644 index 00000000..46806c36 --- /dev/null +++ b/crates/ai/src/local_environment.rs @@ -0,0 +1,119 @@ +//! Device-local chat partitions, separate from General/Project membership. +use crate::history::ContextId; +use nocterm_hubs_model::source_identity::{ + LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, +}; +use serde::{Deserialize, Serialize}; +use std::sync::Arc; + +/// Descriptive persistence owner. This does not grant native or Cloud authority. +#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde( + tag = "kind", + content = "account", + rename_all = "snake_case", + deny_unknown_fields +)] +pub enum LocalEnvironment { + #[default] + ThisComputer, + HubAccount(LocalAccountPartition), +} +impl LocalEnvironment { + /// Versioned local filename key only; the authoritative header is checked too. + /// Display addresses are used solely by the explicit legacy configured variant. + pub fn storage_key(&self) -> String { + use sha2::{Digest as _, Sha256}; + let mut hash = Sha256::new(); + let mut part = |value: &[u8]| { + hash.update((value.len() as u64).to_be_bytes()); + hash.update(value); + }; + part(b"nocterm/local-history-partition/v3"); + match self { + Self::ThisComputer => part(b"this-computer"), + Self::HubAccount(LocalAccountPartition::Stable(scope)) => { + part(b"stable"); + part(scope.hub().instance().as_str().as_bytes()); + part(scope.hub().authority().as_bytes()); + part(scope.account().id().as_str().as_bytes()); + part(scope.account().key().as_bytes()); + } + Self::HubAccount(LocalAccountPartition::LegacyConfigured(account)) => { + part(b"legacy-configured"); + part(account.hub().to_string().as_bytes()); + part(account.account().as_str().as_bytes()); + part(account.key().as_bytes()); + } + } + let mut output = String::with_capacity(64); + for byte in hash.finalize() { + use std::fmt::Write as _; + let _ = write!(output, "{byte:02x}"); + } + output + } + pub fn is_this_computer(&self) -> bool { + matches!(self, Self::ThisComputer) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct ChatScope { + environment: LocalEnvironment, + context: ContextId, +} +impl ChatScope { + pub fn new(environment: LocalEnvironment, context: ContextId) -> Self { + Self { + environment, + context, + } + } + pub fn this_computer(context: ContextId) -> Self { + Self::new(LocalEnvironment::ThisComputer, context) + } + pub fn environment(&self) -> &LocalEnvironment { + &self.environment + } + pub fn context(&self) -> ContextId { + self.context + } +} +impl From for ChatScope { + fn from(context: ContextId) -> Self { + Self::this_computer(context) + } +} + +/// A retained source capture. The original lease never becomes valid again +/// merely because a later sign-in selects the same persistent partition. +#[derive(Clone, Default)] +pub struct LocalEnvironmentAccess { + source: Option>, + environment: Arc, +} +impl LocalEnvironmentAccess { + pub fn this_computer() -> Self { + Self::default() + } + pub fn capture(source: Arc) -> Result { + source.check_current()?; + Ok(Self { + environment: Arc::new(LocalEnvironment::HubAccount(source.partition())), + source: Some(source), + }) + } + pub fn environment(&self) -> &LocalEnvironment { + &self.environment + } + pub fn check_current(&self) -> Result<(), LocalAccountSourceError> { + if let Some(source) = &self.source { + source.check_current()?; + if *self.environment != LocalEnvironment::HubAccount(source.partition()) { + return Err(LocalAccountSourceError::Stale); + } + } + Ok(()) + } +} diff --git a/crates/ai/src/mcp.rs b/crates/ai/src/mcp.rs index 664adedd..91b440ed 100644 --- a/crates/ai/src/mcp.rs +++ b/crates/ai/src/mcp.rs @@ -32,8 +32,13 @@ pub fn tool_result(id: Value, result: Result) -> Value { } } } +/// The notification that tells a connected agent to list the tools again. +pub fn list_changed() -> Value { + json!({"jsonrpc":"2.0","method":"notifications/tools/list_changed"}) +} impl McpSession { - pub fn handle(&mut self, line: &str) -> McpStep { + /// Answers one line under `offer`: only the offered tools are listed or admitted. + pub fn handle(&mut self, line: &str, offer: ToolOffer) -> McpStep { if line.len() > MAX_MCP_LINE { return error(Value::Null, -32600, "Request exceeds limit"); } @@ -57,27 +62,36 @@ impl McpSession { "initialize" => { self.initialized = true; McpStep::Reply( - json!({"jsonrpc":"2.0","id":id,"result":{"protocolVersion":"2024-11-05","serverInfo":{"name":"nocterm","version":"1"},"capabilities":{"tools":{}}}}), + json!({"jsonrpc":"2.0","id":id,"result":{"protocolVersion":"2024-11-05","serverInfo":{"name":"nocterm","version":"1"},"capabilities":{"tools":{"listChanged":true}}}}), ) } "ping" => McpStep::Reply(json!({"jsonrpc":"2.0","id":id,"result":{}})), _ if !self.initialized => error(id, -32002, "Initialize first"), "tools/list" => { - let tools: Vec<_> = catalog::TOOLS.iter().map(|spec| { + let tools: Vec<_> = offer.tools().map(|spec| { json!({"name":spec.name,"description":spec.description,"inputSchema":(spec.schema)()}) }).collect(); McpStep::Reply(json!({"jsonrpc":"2.0","id":id,"result":{"tools":tools}})) } - "tools/call" => call(id, &value["params"]), + "tools/call" => call(id, &value["params"], offer), _ => error(id, -32601, "Unknown method"), } } } -fn call(id: Value, params: &Value) -> McpStep { +fn call(id: Value, params: &Value, offer: ToolOffer) -> McpStep { let args = params.get("arguments").cloned().unwrap_or(json!({})); let tool = params["name"].as_str().unwrap_or_default().to_owned(); - let call = catalog::get(&tool).and_then(|spec| (spec.parse)(args.clone())); + let spec = catalog::get(&tool); + if spec.is_some_and(|spec| !offer.offers(spec.group)) { + return McpStep::Rejected { + id, + tool, + arguments: args, + error: "File tools are offered only while a terminal with file_tools: true is attached; call list_terminals".into(), + }; + } + let call = spec.and_then(|spec| (spec.parse)(args.clone())); match call { Some(call) => match call.validate() { Ok(()) => McpStep::Call { diff --git a/crates/ai/src/model/content.rs b/crates/ai/src/model/content.rs index 2113b62f..1a171931 100644 --- a/crates/ai/src/model/content.rs +++ b/crates/ai/src/model/content.rs @@ -3,6 +3,9 @@ use super::extensions::Extensions; use serde::{Deserialize, Serialize}; use serde_with::{DefaultOnError, VecSkipError, serde_as, skip_serializing_none}; +#[path = "image_data.rs"] +mod image_data; +pub use image_data::ImageData; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] @@ -61,7 +64,7 @@ pub struct Image { #[serde_as(deserialize_as = "DefaultOnError")] #[serde(default)] pub annotations: Option, - pub data: String, + pub data: ImageData, pub mime_type: String, #[serde_as(deserialize_as = "DefaultOnError")] #[serde(default)] @@ -74,7 +77,7 @@ impl Image { pub fn new(data: impl Into, mime_type: impl Into) -> Self { Self { annotations: None, - data: data.into(), + data: data.into().into(), mime_type: mime_type.into(), uri: None, meta: None, diff --git a/crates/ai/src/model/controls.rs b/crates/ai/src/model/controls.rs index d3deab87..bae8d3ae 100644 --- a/crates/ai/src/model/controls.rs +++ b/crates/ai/src/model/controls.rs @@ -120,6 +120,9 @@ pub enum ConfigCategory { Model, ModelConfig, ThoughtLevel, + /// A server an agent that runs elsewhere may sign in to during the chat: a flag, on while + /// the server is attached to the chat. The option's description is the server's address. + Server, Other(String), } diff --git a/crates/ai/src/model/image_data.rs b/crates/ai/src/model/image_data.rs new file mode 100644 index 00000000..11db7418 --- /dev/null +++ b/crates/ai/src/model/image_data.rs @@ -0,0 +1,80 @@ +//! Immutable encoded image data: clones share their allocation and wire bytes stay a string. +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use std::{ops::Deref, sync::Arc}; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ImageData(Arc); +impl ImageData { + /// The normalized encoded allocation and its owned String/Arc framing. + pub fn retained_bytes(&self) -> usize { + self.0 + .capacity() + .saturating_add(std::mem::size_of::()) + .saturating_add(2 * std::mem::size_of::()) + } +} +impl Deref for ImageData { + type Target = str; + fn deref(&self) -> &str { + &self.0 + } +} +impl AsRef<[u8]> for ImageData { + fn as_ref(&self) -> &[u8] { + self.as_bytes() + } +} +impl From for ImageData { + fn from(value: String) -> Self { + Self(Arc::new(value.into_boxed_str().into_string())) + } +} +impl From for String { + fn from(value: ImageData) -> Self { + Arc::try_unwrap(value.0).unwrap_or_else(|value| (*value).clone()) + } +} +impl PartialEq for ImageData { + fn eq(&self, other: &String) -> bool { + self.deref() == other + } +} +impl Serialize for ImageData { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(self) + } +} +impl<'de> Deserialize<'de> for ImageData { + fn deserialize>(decoder: D) -> Result { + String::deserialize(decoder).map(Self::from) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn excess_owned_capacity_is_released_before_the_image_can_be_retained() { + let mut input = String::with_capacity(4 * 1024 * 1024); + input.push_str("eA=="); + let data = ImageData::from(input); + assert!(data.retained_bytes() < 1024); + assert!(data.retained_bytes() > data.len()); + assert_eq!(serde_json::to_string(&data).unwrap(), "\"eA==\""); + let owned = String::from(data); + assert_eq!(owned.capacity(), owned.len()); + assert_eq!(owned, "eA=="); + } + #[test] + fn cloning_shares_immutable_bytes_and_preserves_the_string_wire_format() { + let original = ImageData::from("encoded image".to_owned()); + let copy = original.clone(); + assert_eq!(original.as_ptr(), copy.as_ptr()); + assert_eq!(serde_json::to_string(©).unwrap(), "\"encoded image\""); + assert_eq!( + serde_json::from_str::("\"encoded image\"").unwrap(), + original + ); + assert_eq!(String::from(copy), "encoded image"); + } +} diff --git a/crates/ai/src/model/session.rs b/crates/ai/src/model/session.rs index 8ac2a6ee..735c7a21 100644 --- a/crates/ai/src/model/session.rs +++ b/crates/ai/src/model/session.rs @@ -32,6 +32,10 @@ pub struct SessionOpened { pub session: SessionId, pub modes: Option, pub configuration: Option>, + /// The agent's own record of a reopened session, oldest first, from an agent that keeps + /// its sessions ([`AgentCapabilitiesSummary::keeps_history`]). The chat shows it in place + /// of its saved transcript; `None` keeps the saved transcript. + pub history: Option>, } impl SessionOpened { pub fn new(session: impl Into) -> Self { @@ -39,6 +43,7 @@ impl SessionOpened { session: session.into(), modes: None, configuration: None, + history: None, } } } @@ -52,6 +57,10 @@ impl SessionOpened { self.configuration = Some(options); self } + pub fn history(mut self, updates: Vec) -> Self { + self.history = Some(updates); + self + } } /// How the agent starts a local tool server for the session. @@ -133,6 +142,17 @@ pub enum SessionUpdate { ConfigOptionUpdate(Vec), SessionInfoUpdate(SessionInfo), UsageUpdate(ContextUsage), + /// A turn no prompt of this client started, such as one sent from another device. + Turn(TurnActivity), +} + +/// What an agent that runs elsewhere does on its own: it began a turn for a prompt sent from +/// another device, or ended it. A chat shows the prompt and that the agent works, and offers +/// Stop, until the turn ends. +#[derive(Clone, Debug, PartialEq)] +pub enum TurnActivity { + Started { prompt: Vec }, + Ended, } #[derive(Clone, Debug, Default, PartialEq)] @@ -165,4 +185,11 @@ impl std::fmt::Display for RemoteError { #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct AgentCapabilitiesSummary { pub image_prompts: bool, + /// The agent keeps its sessions itself (a hub agent): a chat reopens its session even + /// when it saved nothing of it, and shows the history the agent returns. + pub keeps_history: bool, + /// The agent runs elsewhere with tools of its own (a hub agent on its servers): a prompt + /// carries only what the person wrote, not the context and rules of this device's + /// terminals. + pub own_tools: bool, } diff --git a/crates/ai/src/prompt_files.rs b/crates/ai/src/prompt_files.rs new file mode 100644 index 00000000..62485f4a --- /dev/null +++ b/crates/ai/src/prompt_files.rs @@ -0,0 +1,161 @@ +//! Files a person attaches to a prompt that are not images. A prompt carries a link to the +//! file where it is (`file://`): a local agent opens it there, and a hub chat uploads it. + +use std::path::Path; +use std::sync::Arc; + +use crate::model::content::{Content, ResourceReference}; + +/// The most files one prompt carries besides its images. +pub const MAX_PROMPT_FILES: usize = 16; + +/// The extensions of files attached as images, which the agent sees inline instead. +pub const IMAGE_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp"]; + +/// Whether `path` is attached as an image rather than as a file. +pub fn is_image(path: &Path) -> bool { + path.extension() + .and_then(|extension| extension.to_str()) + .is_some_and(|extension| { + IMAGE_EXTENSIONS + .iter() + .any(|known| known.eq_ignore_ascii_case(extension)) + }) +} + +/// Why a file cannot be attached. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum PromptFileError { + #[error("Only files given by their full path can be attached")] + Relative, + #[error("At most {MAX_PROMPT_FILES} files can be attached to a message")] + TooMany, +} + +/// A file of a prompt: its name and size for the chip, and the link the agent receives. +#[derive(Clone, Debug, PartialEq)] +pub struct PromptFile { + name: String, + size: u64, + content: Arc, +} + +impl PromptFile { + /// The file at the absolute `path`, of `size` bytes. + pub fn link(path: &Path, size: u64) -> Result { + if !path.is_absolute() { + return Err(PromptFileError::Relative); + } + let name = path.file_name().map_or_else( + || path.display().to_string(), + |name| name.to_string_lossy().into(), + ); + let content = Content::ResourceReference(ResourceReference { + annotations: None, + description: None, + mime_type: None, + name: name.clone(), + size: i64::try_from(size).ok(), + title: None, + uri: file_uri(path), + meta: None, + }); + Ok(Self { + name, + size, + content: Arc::new(content), + }) + } + + /// The file a prompt's `content` links to, when it is one. + pub fn of(content: &Content) -> Option { + let Content::ResourceReference(link) = content else { + return None; + }; + link.uri.starts_with("file://").then(|| Self { + name: link.name.clone(), + size: link + .size + .and_then(|size| u64::try_from(size).ok()) + .unwrap_or(0), + content: Arc::new(content.clone()), + }) + } + + pub fn name(&self) -> &str { + &self.name + } + + pub fn size(&self) -> u64 { + self.size + } + + /// The link the prompt carries. + pub fn content(&self) -> Content { + (*self.content).clone() + } +} + +/// The local path a `file://` link names, if it names one. +pub fn path_of(uri: &str) -> Option { + let rest = uri.strip_prefix("file://")?; + let mut bytes = Vec::with_capacity(rest.len()); + let mut chars = rest.bytes(); + while let Some(byte) = chars.next() { + if byte == b'%' { + let high = chars.next()?; + let low = chars.next()?; + let hex = [high, low]; + bytes.push(u8::from_str_radix(std::str::from_utf8(&hex).ok()?, 16).ok()?); + } else { + bytes.push(byte); + } + } + Some(std::path::PathBuf::from(String::from_utf8(bytes).ok()?)) +} + +/// `file://` and the path with every byte outside the unreserved set and `/` escaped. +fn file_uri(path: &Path) -> String { + use std::fmt::Write as _; + let text = path.to_string_lossy().replace('\\', "/"); + let mut uri = String::from("file://"); + if !text.starts_with('/') { + uri.push('/'); + } + for byte in text.bytes() { + if byte.is_ascii_alphanumeric() || b"-._~/:".contains(&byte) { + uri.push(char::from(byte)); + } else { + let _ = write!(uri, "%{byte:02X}"); + } + } + uri +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_file_is_linked_by_its_escaped_path_and_read_back_from_the_link() { + let path = Path::new("/home/ops/notes 2026/план.txt"); + let file = PromptFile::link(path, 42).unwrap(); + assert_eq!(file.name(), "план.txt"); + let Content::ResourceReference(link) = file.content() else { + panic!("a resource link"); + }; + assert!(link.uri.starts_with("file:///home/ops/notes%202026/")); + assert_eq!(path_of(&link.uri).as_deref(), Some(path)); + assert_eq!(PromptFile::of(&file.content()), Some(file)); + } + + #[test] + fn images_are_told_apart_by_extension_and_relative_paths_are_refused() { + assert!(is_image(Path::new("/tmp/Shot.PNG"))); + assert!(!is_image(Path::new("/tmp/report.pdf"))); + assert_eq!( + PromptFile::link(Path::new("notes.txt"), 1), + Err(PromptFileError::Relative) + ); + } +} diff --git a/crates/ai/src/session.rs b/crates/ai/src/session.rs index 9e65da74..0bee37ab 100644 --- a/crates/ai/src/session.rs +++ b/crates/ai/src/session.rs @@ -67,6 +67,8 @@ pub struct SessionState { closing: bool, /// Failures since the last completed turn. failures: u32, + /// The running turn was started by the agent for another device. + external: bool, } impl SessionState { @@ -196,6 +198,28 @@ impl SessionState { self.turn += 1; Some(self.ticket()) } + /// The agent began a turn no prompt of this chat started (one sent from another device): + /// the chat shows it as running. `false` unless the session is ready. + pub fn start_external_turn(&mut self) -> bool { + if self.start_turn().is_none() { + return false; + } + self.external = true; + true + } + /// The agent's own turn ended. Returns whether one was running. + pub fn finish_external_turn(&mut self) -> bool { + if !self.external || !self.generating() { + return false; + } + self.external = false; + self.phase = SessionPhase::Ready; + true + } + /// The running turn is the agent's own, not one of this chat's prompts. + pub fn external_turn(&self) -> bool { + self.external && self.generating() + } /// The user stops the chat. Returns whether a running turn must be /// cancelled. pub fn stop(&mut self) -> bool { @@ -213,6 +237,7 @@ impl SessionState { if !self.turn_current(ticket) || !self.generating() { return false; } + self.external = false; self.phase = SessionPhase::Ready; if completed { self.failures = 0; @@ -245,6 +270,7 @@ impl SessionState { } /// The session ended with an error. Results of its requests are dropped. pub fn fail(&mut self) { + self.external = false; self.phase = SessionPhase::Failed; self.epoch += 1; self.stopped = false; diff --git a/crates/ai/src/session/tests.rs b/crates/ai/src/session/tests.rs index 238a1185..386e1f7c 100644 --- a/crates/ai/src/session/tests.rs +++ b/crates/ai/src/session/tests.rs @@ -149,3 +149,27 @@ fn a_missing_session_or_a_failed_fork_starts_over() { "a transient error keeps the chat's own session for the next try" ); } + +#[test] +fn a_turn_the_agent_runs_for_another_device_shows_and_ends_on_its_own() { + let mut state = SessionState::default(); + assert!(!state.start_external_turn(), "a detached chat runs nothing"); + state.queue(); + state.leased(); + state.opened(); + assert!(state.start_external_turn()); + assert!(state.generating() && state.external_turn()); + assert!( + state.start_turn().is_none(), + "a prompt waits for the agent's own turn" + ); + assert!(state.finish_external_turn()); + assert!(!state.generating()); + assert!(!state.finish_external_turn()); + let ticket = state.start_turn().unwrap(); + assert!( + !state.finish_external_turn(), + "a prompt's turn is not the agent's own" + ); + assert!(state.finish_turn(ticket, true)); +} diff --git a/crates/ai/src/session_config.rs b/crates/ai/src/session_config.rs index b32c7154..aea80085 100644 --- a/crates/ai/src/session_config.rs +++ b/crates/ai/src/session_config.rs @@ -80,6 +80,7 @@ fn category_name(category: &ConfigCategory) -> String { ConfigCategory::Model => "model".into(), ConfigCategory::ModelConfig => "model_config".into(), ConfigCategory::ThoughtLevel => "thought_level".into(), + ConfigCategory::Server => "server".into(), ConfigCategory::Other(name) => name.clone(), } } @@ -89,6 +90,7 @@ fn category(name: &str) -> ConfigCategory { "model" => ConfigCategory::Model, "model_config" => ConfigCategory::ModelConfig, "thought_level" => ConfigCategory::ThoughtLevel, + "server" => ConfigCategory::Server, other => ConfigCategory::Other(other.into()), } } diff --git a/crates/ai/src/session_config/tests.rs b/crates/ai/src/session_config/tests.rs index f545e8c1..46d36171 100644 --- a/crates/ai/src/session_config/tests.rs +++ b/crates/ai/src/session_config/tests.rs @@ -40,6 +40,9 @@ fn options() -> Vec { .category(ConfigCategory::Mode), ConfigOption::boolean("web", "Web search", false) .category(ConfigCategory::Other("_tools".into())), + ConfigOption::boolean("server:1", "Build box", true) + .description("root@build.example") + .category(ConfigCategory::Server), ] } @@ -73,6 +76,8 @@ fn a_choice_shows_at_once_only_when_the_option_offers_it() { value("opus"), value("low"), value("ask"), + ConfigValue::Flag(true), + // The attached server keeps the value the agent reported. ConfigValue::Flag(true) ] ); diff --git a/crates/ai/src/storage.rs b/crates/ai/src/storage.rs index e77a7bf0..d36a70c3 100644 --- a/crates/ai/src/storage.rs +++ b/crates/ai/src/storage.rs @@ -5,9 +5,15 @@ use crate::{ }; use futures::future::BoxFuture; use std::{path::PathBuf, sync::Arc}; +mod prepared; +pub use prepared::{PreparedChatBytes, PreparedChatLoad}; #[derive(Clone, Debug, thiserror::Error)] pub enum AgentStorageError { + #[error("All normal installation history payload slots are retained or loading")] + PayloadCapacity, + #[error("The history repository cannot prepare a charged decoded load")] + AllocationUnsupported, #[error( "Saved history needs {incoming} bytes with {retained} retained; installation limit is {limit} bytes" )] @@ -29,6 +35,17 @@ pub enum AgentStorageError { expected: nocterm_project_model::ContextId, actual: nocterm_project_model::ContextId, }, + #[error("Saved chat belongs to another original environment")] + EnvironmentMismatch { + expected: Arc, + actual: Arc, + }, + #[error("This environment requires held native storage directory capabilities")] + EnvironmentUnsupported(Arc), + #[error("The hub account could not be captured")] + AccountCapture(#[from] nocterm_hubs_model::HubError), + #[error("The captured account source is no longer available")] + AccountSource(#[from] nocterm_hubs_model::source_identity::LocalAccountSourceError), #[error("This context requires held native storage directory capabilities: {0:?}")] ScopeUnsupported(nocterm_project_model::ContextId), #[error("The local history filesystem operation failed: {0}")] @@ -90,6 +107,25 @@ pub fn require_chat_scope( } Ok(()) } +pub fn require_local_environment( + expected: &crate::local_environment::LocalEnvironment, + actual: &crate::local_environment::LocalEnvironment, +) -> Result<(), AgentStorageError> { + if expected != actual { + return Err(AgentStorageError::EnvironmentMismatch { + expected: Arc::new(expected.clone()), + actual: Arc::new(actual.clone()), + }); + } + Ok(()) +} +pub fn require_local_scope( + expected: &crate::local_environment::ChatScope, + actual: &crate::local_environment::ChatScope, +) -> Result<(), AgentStorageError> { + require_local_environment(expected.environment(), actual.environment())?; + require_chat_scope(expected.context(), actual.context()) +} pub type StorageFuture = BoxFuture<'static, Result>; /// Futures perform I/O only when polled by the background storage executor. pub trait AgentStateRepository: Send + Sync { @@ -97,6 +133,61 @@ pub trait AgentStateRepository: Send + Sync { fn save(&self, state: AgentStateFile) -> StorageFuture<()>; } pub trait ChatRepository: Send + Sync { + /// Read bounded original bytes and preflight their decoded ownership cost. + /// Callers must obtain the actual shared allocation before `decode`; an + /// implementation without this capability must refuse rather than hydrate + /// an uncharged value through the older load API. + fn prepare_load_in( + &self, + _scope: crate::local_environment::ChatScope, + _id: String, + ) -> StorageFuture> { + Box::pin(async { Err(AgentStorageError::AllocationUnsupported) }) + } + fn catalog_in( + &self, + scope: crate::local_environment::ChatScope, + ) -> StorageFuture> { + if scope.environment().is_this_computer() { + self.catalog(scope.context()) + } else { + Box::pin(async move { + Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))) + }) + } + } + fn load_in( + &self, + scope: crate::local_environment::ChatScope, + id: String, + ) -> StorageFuture { + if scope.environment().is_this_computer() { + self.load(scope.context(), id) + } else { + Box::pin(async move { + Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))) + }) + } + } + fn delete_in( + &self, + scope: crate::local_environment::ChatScope, + id: String, + ) -> StorageFuture<()> { + if scope.environment().is_this_computer() { + self.delete(scope.context(), id) + } else { + Box::pin(async move { + Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))) + }) + } + } /// Filter authoritative ownership before applying per-context discovery limits. fn catalog(&self, scope: nocterm_project_model::ContextId) -> StorageFuture>; fn load(&self, scope: nocterm_project_model::ContextId, id: String) diff --git a/crates/ai/src/storage/prepared.rs b/crates/ai/src/storage/prepared.rs new file mode 100644 index 00000000..69b99e5f --- /dev/null +++ b/crates/ai/src/storage/prepared.rs @@ -0,0 +1,88 @@ +//! Owned bounded source bytes are decoded only after receiving the caller's lease. +use super::{AgentStorageError, StorageFuture, require_local_scope}; +use crate::{ + history::{self, SavedChat}, + local_environment::ChatScope, +}; +use nocterm_core::allocation::{Allocated, AllocationLease}; +use std::fmt; + +/// An original source prepared by a repository, not a fresh pathname lookup. +/// The estimate covers decoded retained objects; separately bounded native input +/// scratch remains owned by this handle until decoding or cancellation finishes. +pub trait PreparedChatLoad: Send { + fn resident_bytes(&self) -> usize; + fn decode(self: Box, allocation: AllocationLease) -> StorageFuture>; +} + +/// Pure shared implementation for native repositories and their canonical fake. +/// Creation preflights bytes without constructing a decoded SavedChat. Matching +/// descriptive coordinates confer no filesystem or account authority. +pub struct PreparedChatBytes { + bytes: Vec, + scope: ChatScope, + id: String, + expected_version: Option, + resident: usize, +} +impl PreparedChatBytes { + pub fn new( + bytes: Vec, + scope: ChatScope, + id: String, + expected_version: Option, + ) -> Result { + if bytes.len() as u64 > history::MAX_FILE_BYTES { + return Err(history::HistoryCodecError::EncodedLimit.into()); + } + if let Some(version) = expected_version + && !(1..=3).contains(&version) + { + return Err(history::HistoryCodecError::UnsupportedVersion(version).into()); + } + let resident = history::structural_bytes(&bytes)?; + Ok(Self { + bytes, + scope, + id, + expected_version, + resident, + }) + } +} +impl fmt::Debug for PreparedChatBytes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("PreparedChatBytes") + .field("resident_bytes", &self.resident) + .field("expected_version", &self.expected_version) + .finish_non_exhaustive() + } +} +impl PreparedChatLoad for PreparedChatBytes { + fn resident_bytes(&self) -> usize { + self.resident + } + fn decode(self: Box, allocation: AllocationLease) -> StorageFuture> { + Box::pin(async move { + if allocation.payloads() == 0 { + return Err(AgentStorageError::PayloadCapacity); + } + if allocation.bytes() < self.resident { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: self.resident, + limit: allocation.bytes(), + }); + } + let chat = history::decode(&self.bytes, &self.id)?; + require_local_scope(&self.scope, &chat.header.chat_scope())?; + if self + .expected_version + .is_some_and(|version| version != chat.version) + { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + Ok(Allocated::new(chat, allocation)) + }) + } +} diff --git a/crates/ai/src/testing.rs b/crates/ai/src/testing.rs index 7fe7a056..53b4d6e9 100644 --- a/crates/ai/src/testing.rs +++ b/crates/ai/src/testing.rs @@ -40,9 +40,6 @@ impl AgentConnector for ScriptedConnector { mod storage; pub use storage::{InMemoryAgentRepository, StorageOperation}; -mod usage; -pub use usage::ScriptedUsageSource; - mod sandbox; mod workspace; pub use sandbox::ScriptedSandboxProbe; diff --git a/crates/ai/src/testing/storage.rs b/crates/ai/src/testing/storage.rs index 6d6389c2..abdc5a70 100644 --- a/crates/ai/src/testing/storage.rs +++ b/crates/ai/src/testing/storage.rs @@ -1,8 +1,12 @@ //! One deterministic repository shared by runtime, feature and composition tests. +use crate::local_environment::ChatScope; use crate::{ favorites::AgentStateFile, history::{self, ChatSummary, FileStamp, SavedChat, SharedChat}, - storage::{AgentStateRepository, AgentStorageError, ChatRepository, StorageFuture}, + storage::{ + AgentStateRepository, AgentStorageError, ChatRepository, PreparedChatBytes, + PreparedChatLoad, StorageFuture, + }, }; use futures::channel::oneshot; use std::{ @@ -16,6 +20,8 @@ pub enum StorageOperation { SaveState, Catalog, Load(String), + /// Pause after materialization while the actual decoded body retains its lease. + Decoded(String), Save(String), Delete(String), } @@ -23,7 +29,7 @@ type Release = oneshot::Receiver>; #[derive(Default)] struct State { preferences: AgentStateFile, - chats: HashMap>>, + chats: HashMap>>, barriers: HashMap>, operations: Vec, rejected: HashMap, @@ -45,7 +51,7 @@ impl InMemoryAgentRepository { .lock() .unwrap() .chats - .entry(chat.header.scope) + .entry(chat.header.chat_scope()) .or_default() .insert(chat.header.id.clone(), bytes); Ok(()) @@ -70,11 +76,14 @@ impl InMemoryAgentRepository { self.encoded_in_scope(history::ContextId::General, id) } pub fn encoded_in_scope(&self, scope: history::ContextId, id: &str) -> Option> { + self.encoded_in_environment(&scope.into(), id) + } + pub fn encoded_in_environment(&self, scope: &ChatScope, id: &str) -> Option> { self.state .lock() .unwrap() .chats - .get(&scope)? + .get(scope)? .get(id) .cloned() } @@ -156,7 +165,44 @@ impl AgentStateRepository for InMemoryAgentRepository { } } impl ChatRepository for InMemoryAgentRepository { + fn prepare_load_in( + &self, + scope: ChatScope, + id: String, + ) -> StorageFuture> { + let this = self.clone(); + Box::pin(async move { + this.begin(StorageOperation::Load(id.clone())).await?; + let bytes = { + let state = this + .state + .lock() + .map_err(|_| AgentStorageError::Interrupted)?; + state + .chats + .get(&scope) + .and_then(|chats| chats.get(&id)) + .ok_or_else(|| AgentStorageError::NotFound(id.clone()))? + .clone() + }; + let source = PreparedChatBytes::new(bytes, scope, id.clone(), None)?; + Ok(Box::new(PreparedRepositoryLoad { + source: Box::new(source), + repository: this, + id, + }) as Box) + }) + } fn catalog(&self, scope: history::ContextId) -> StorageFuture> { + self.catalog_in(scope.into()) + } + fn load(&self, scope: history::ContextId, id: String) -> StorageFuture { + self.load_in(scope.into(), id) + } + fn delete(&self, scope: history::ContextId, id: String) -> StorageFuture<()> { + self.delete_in(scope.into(), id) + } + fn catalog_in(&self, scope: ChatScope) -> StorageFuture> { let this = self.clone(); Box::pin(async move { this.begin(StorageOperation::Catalog).await?; @@ -178,14 +224,14 @@ impl ChatRepository for InMemoryAgentRepository { .ok() .flatten() }) - .filter(|row| row.scope == scope) + .filter(|row| row.chat_scope() == scope) .collect::>(); rows.sort_by_key(|row| std::cmp::Reverse((row.pinned, row.updated))); rows.truncate(history::MAX_RESTORED); Ok(rows) }) } - fn load(&self, scope: history::ContextId, id: String) -> StorageFuture { + fn load_in(&self, scope: ChatScope, id: String) -> StorageFuture { let this = self.clone(); Box::pin(async move { this.begin(StorageOperation::Load(id.clone())).await?; @@ -196,7 +242,7 @@ impl ChatRepository for InMemoryAgentRepository { .and_then(|chats| chats.get(&id)) .ok_or_else(|| AgentStorageError::NotFound(id.clone()))?; let chat = history::decode(bytes, &id)?; - crate::storage::require_chat_scope(scope, chat.header.scope)?; + crate::storage::require_local_scope(&scope, &chat.header.chat_scope())?; Ok(chat) }) } @@ -210,17 +256,15 @@ impl ChatRepository for InMemoryAgentRepository { let state = this.state.lock().unwrap(); let bytes = state .chats - .get(&chat.header.scope) + .get(&chat.header.chat_scope()) .and_then(|chats| chats.get(&chat.header.id)) .ok_or_else(|| AgentStorageError::NotFound(chat.header.id.clone()))?; history::decode(bytes, &chat.header.id)? }; - if original.header.scope != chat.header.scope { - return Err(AgentStorageError::ScopeMismatch { - expected: chat.header.scope, - actual: original.header.scope, - }); - } + crate::storage::require_local_scope( + &chat.header.chat_scope(), + &original.header.chat_scope(), + )?; original.draft = draft; original.header.updated = updated; history::encode(&original)? @@ -228,16 +272,19 @@ impl ChatRepository for InMemoryAgentRepository { history::encode_shared(&chat)? }; let mut state = this.state.lock().unwrap(); - let chats = state.chats.entry(chat.header.scope).or_default(); + let chats = state.chats.entry(chat.header.chat_scope()).or_default(); if let Some(original) = chats.get(&chat.header.id) { let original = history::decode(original, &chat.header.id)?; - crate::storage::require_chat_scope(chat.header.scope, original.header.scope)?; + crate::storage::require_local_scope( + &chat.header.chat_scope(), + &original.header.chat_scope(), + )?; } chats.insert(chat.header.id.clone(), bytes); Ok(()) }) } - fn delete(&self, scope: history::ContextId, id: String) -> StorageFuture<()> { + fn delete_in(&self, scope: ChatScope, id: String) -> StorageFuture<()> { let this = self.clone(); Box::pin(async move { this.begin(StorageOperation::Delete(id.clone())).await?; @@ -247,7 +294,7 @@ impl ChatRepository for InMemoryAgentRepository { }; if let Some(bytes) = chats.get(&id) { let chat = history::decode(bytes, &id)?; - crate::storage::require_chat_scope(scope, chat.header.scope)?; + crate::storage::require_local_scope(&scope, &chat.header.chat_scope())?; } chats.remove(&id); Ok(()) @@ -255,6 +302,29 @@ impl ChatRepository for InMemoryAgentRepository { } } +struct PreparedRepositoryLoad { + source: Box, + repository: InMemoryAgentRepository, + id: String, +} +impl PreparedChatLoad for PreparedRepositoryLoad { + fn resident_bytes(&self) -> usize { + self.source.resident_bytes() + } + fn decode( + self: Box, + allocation: nocterm_core::allocation::AllocationLease, + ) -> StorageFuture> { + Box::pin(async move { + let decoded = self.source.decode(allocation).await?; + self.repository + .begin(StorageOperation::Decoded(self.id)) + .await?; + Ok(decoded) + }) + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/ai/src/testing/usage.rs b/crates/ai/src/testing/usage.rs deleted file mode 100644 index efb5db64..00000000 --- a/crates/ai/src/testing/usage.rs +++ /dev/null @@ -1,147 +0,0 @@ -//! Shared scripted account usage source with explicitly completed reads. -use crate::usage::{AccountUsageSource, RateLimit, UsageAgent, UsageReadError}; -use futures::{channel::oneshot, future::BoxFuture}; -use std::{ - collections::VecDeque, - sync::{Arc, Mutex}, -}; - -type Answer = Result>, UsageReadError>; -#[derive(Default)] -struct Script { - requests: Vec, - answers: VecDeque>, -} -#[derive(Clone, Default)] -pub struct ScriptedUsageSource(Arc>); -impl ScriptedUsageSource { - pub fn push(&self, result: Answer) { - self.0 - .lock() - .expect("usage script") - .answers - .push_back(Box::pin(async move { result })); - } - pub fn pending(&self) -> oneshot::Sender { - let (sender, receiver) = oneshot::channel(); - self.0 - .lock() - .expect("usage script") - .answers - .push_back(Box::pin(async move { receiver.await.unwrap_or(Ok(None)) })); - sender - } - pub fn requests(&self) -> Vec { - self.0.lock().expect("usage script").requests.clone() - } -} -impl AccountUsageSource for ScriptedUsageSource { - fn read(&self, agent: UsageAgent) -> BoxFuture<'static, Answer> { - let script = self.0.clone(); - Box::pin(async move { - let answer = { - let mut script = script.lock().expect("usage script"); - script.requests.push(agent); - script.answers.pop_front() - }; - match answer { - Some(answer) => answer.await, - None => Ok(None), - } - }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use futures::{FutureExt, executor::block_on}; - use std::{error::Error, path::PathBuf}; - fn agent() -> UsageAgent { - UsageAgent { - id: "account".into(), - account: crate::agents::AccountUsage::SessionLog, - } - } - #[test] - fn held_usage_failures_preserve_read_seek_category_native_kind_and_allocation() { - for error in [ - UsageReadError::read( - "/report", - std::io::Error::from(std::io::ErrorKind::PermissionDenied), - ), - UsageReadError::seek( - "/report", - std::io::Error::from(std::io::ErrorKind::InvalidInput), - ), - ] { - let expected = error.clone(); - let source = ScriptedUsageSource::default(); - let answer = source.pending(); - let mut future = source.read(agent()); - assert!(future.as_mut().now_or_never().is_none()); - assert_eq!(source.requests(), vec![agent()]); - answer.send(Err(error)).unwrap(); - let observed = block_on(future).unwrap_err(); - match (&expected, &observed) { - (UsageReadError::Read { path: a, .. }, UsageReadError::Read { path: b, .. }) - | (UsageReadError::Seek { path: a, .. }, UsageReadError::Seek { path: b, .. }) => { - assert_eq!(a, b); - assert_eq!(a, &PathBuf::from("/report")); - } - _ => panic!("usage operation category must be preserved"), - } - let native = expected - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - let shared = observed - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - assert_eq!(native.kind(), shared.kind()); - assert_eq!(expected.to_string(), observed.to_string()); - } - } - #[test] - fn usage_source_preserves_immediate_native_error_before_its_deeper_cause() { - #[derive(Debug)] - struct NativeContext; - impl std::fmt::Display for NativeContext { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("usage context") - } - } - impl Error for NativeContext { - fn source(&self) -> Option<&(dyn Error + 'static)> { - Some(&std::fmt::Error) - } - } - let error = UsageReadError::read("/report", std::io::Error::other(NativeContext)); - let clone = error.clone(); - let native = error - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - let shared = clone - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - assert_eq!(native.kind(), std::io::ErrorKind::Other); - assert!(native.source().unwrap().is::()); - } -} diff --git a/crates/ai/src/tests.rs b/crates/ai/src/tests.rs index 34a2a3e5..f91707f4 100644 --- a/crates/ai/src/tests.rs +++ b/crates/ai/src/tests.rs @@ -19,6 +19,7 @@ fn environment_blocks_sensitive_and_injection_vars_even_if_requested() { args: Vec::new(), env: Default::default(), inherit_env: vec!["ANTHROPIC_API_KEY".into()], + hub: None, }; for name in [ "SSH_AUTH_SOCK", @@ -136,18 +137,22 @@ fn reducer_handles_transcript_metadata_usage_and_tool_replacement() { fn mcp_requires_initialization_validates_input_and_never_echoes_bad_payload() { let mut m = McpSession::default(); assert!( - matches!(m.handle("marker-secret malformed"),McpStep::Reply(v) if !v.to_string().contains("marker-secret")) + matches!(m.handle("marker-secret malformed", Default::default()),McpStep::Reply(v) if !v.to_string().contains("marker-secret")) ); assert!( - matches!(m.handle(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#),McpStep::Reply(v) if v["error"]["code"]==-32002) + matches!(m.handle(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#, Default::default()),McpStep::Reply(v) if v["error"]["code"]==-32002) + ); + m.handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + Default::default(), ); - m.handle(r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#); assert!( - matches!(m.handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_terminal","arguments":{"terminal_id":"t1","lines":2001}}}"#),McpStep::Rejected { error, .. } if !error.is_empty()) + matches!(m.handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_terminal","arguments":{"terminal_id":"t1","lines":2001}}}"#, Default::default()),McpStep::Rejected { error, .. } if !error.is_empty()) ); assert!(matches!( m.handle( - r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"list_terminals"}}"# + r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"list_terminals"}}"#, + Default::default() ), McpStep::Call { call: TerminalCall::ListTerminals, @@ -155,11 +160,14 @@ fn mcp_requires_initialization_validates_input_and_never_echoes_bad_payload() { } )); assert!(matches!( - m.handle(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#), + m.handle( + r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#, + Default::default() + ), McpStep::Ignore )); assert!(matches!( - m.handle(&"x".repeat(MAX_MCP_LINE + 1)), + m.handle(&"x".repeat(MAX_MCP_LINE + 1), Default::default()), McpStep::Reply(_) )); } @@ -188,6 +196,7 @@ fn explicit_descriptor_payload_filters_user_metadata_and_has_no_credentials() { local: false, cwd: None, status: "connected".into(), + file_tools: false, connection: Some(ConnectionDescriptor { id: "c1".into(), name: "host".into(), @@ -195,7 +204,7 @@ fn explicit_descriptor_payload_filters_user_metadata_and_has_no_credentials() { description: "password=marker-description sk-ant-secretmarker123".into(), host: "example.org".into(), port: 22, - user: "egor".into(), + user: "alice".into(), }), }; let payload = context_block(&[terminal], &[]); @@ -264,6 +273,7 @@ fn descriptor_redaction_preserves_json_escaping_and_blocks_context_delimiter_inj connection: None, cwd: Some(" injected".into()), status: "Connected".into(), + file_tools: false, }; let block = context_block(&[terminal], &[]); let json = block @@ -323,6 +333,66 @@ fn capabilities_do_not_share_execution_input_or_connection_grants() { grants.revoke("t1"); assert!(grants.requires_approval(&execution, &settings)); } +fn transfer_between(source: &str, destination: &str) -> TerminalCall { + TerminalCall::TransferFiles(TransferFiles { + source_terminal_id: source.into(), + sources: vec!["/a".into()], + destination_terminal_id: destination.into(), + destination: "/in".into(), + overwrite: false, + yield_ms: None, + }) +} +#[test] +fn a_transfer_grant_covers_one_unordered_terminal_pair_only() { + let settings = ApprovalSettings::default(); + let mut grants = ApprovalGrants::default(); + let call = transfer_between("t1", "t2"); + assert!(grants.requires_approval(&call, &settings)); + grants.grant("t1", ToolCapability::Transfer); + grants.grant("t2", ToolCapability::Transfer); + assert!( + grants.requires_approval(&call, &settings), + "per-terminal capability grants do not approve a transfer" + ); + grants.grant_call(&call); + assert!(!grants.requires_approval(&call, &settings)); + assert!(!grants.requires_approval(&transfer_between("t2", "t1"), &settings)); + grants.grant_call(&transfer_between("t3", "t4")); + for crossing in [("t1", "t4"), ("t3", "t2"), ("t1", "t3"), ("t1", "t1")] { + assert!( + grants.requires_approval(&transfer_between(crossing.0, crossing.1), &settings), + "{crossing:?}" + ); + } + grants.revoke("t1"); + assert!(grants.requires_approval(&call, &settings)); + assert!(!grants.requires_approval(&transfer_between("t4", "t3"), &settings)); +} +#[test] +fn a_same_terminal_transfer_grant_is_its_own_pair() { + let settings = ApprovalSettings::default(); + let mut grants = ApprovalGrants::default(); + grants.grant_call(&transfer_between("t1", "t1")); + assert!(!grants.requires_approval(&transfer_between("t1", "t1"), &settings)); + assert!(grants.requires_approval(&transfer_between("t1", "t2"), &settings)); +} +#[test] +fn reading_and_cancelling_a_transfer_need_no_terminal_grant() { + let settings = ApprovalSettings::default(); + let grants = ApprovalGrants::default(); + for call in [ + TerminalCall::ReadTransfer(ReadTransfer { + transfer_id: "x".into(), + yield_ms: None, + }), + TerminalCall::CancelTransfer(CancelTransfer { + transfer_id: "x".into(), + }), + ] { + assert!(!grants.requires_approval(&call, &settings)); + } +} #[test] fn structured_execution_bounds_program_arguments_stdin_and_lifetime() { let valid = ExecCommand { diff --git a/crates/ai/src/thread.rs b/crates/ai/src/thread.rs index cc724ae9..c552d4fc 100644 --- a/crates/ai/src/thread.rs +++ b/crates/ai/src/thread.rs @@ -7,6 +7,7 @@ use crate::model::{ usage::{ContextUsage, TokenTotals}, }; use serde::{Deserialize, Serialize}; +pub mod live; #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(tag = "kind", content = "value", rename_all = "snake_case")] pub enum Entry { diff --git a/crates/ai/src/thread/live.rs b/crates/ai/src/thread/live.rs new file mode 100644 index 00000000..4fe60f3c --- /dev/null +++ b/crates/ai/src/thread/live.rs @@ -0,0 +1,236 @@ +//! Pure live-transcript admission. A stream is bounded before it mutates accepted rows. +use super::{Entry, ThreadState}; +use crate::{ + history::{HistoryCodecError, MAX_RESIDENT_BYTES, retained_value_bytes}, + model::{content::Content, controls::Change, session::SessionUpdate}, +}; + +const MAX_LIVE_ENTRIES: usize = 20_000; +pub fn resident_bytes(state: &ThreadState) -> Result { + Ok(retained_value_bytes(&state.entries)? + .saturating_add(plan_bytes(state.plan.as_ref())) + .saturating_add(retained_value_bytes(&state.title)?) + .saturating_add(usage_bytes(state.usage.as_ref())?) + .saturating_add(state.times.len().saturating_mul(8))) +} +fn plan_bytes(value: Option<&crate::model::plan::Plan>) -> usize { + value.map_or(0, |plan| { + plan.entries.iter().fold(1024usize, |bytes, entry| { + bytes + .saturating_add(entry.content.len().saturating_mul(2)) + .saturating_add(1024) + }) + }) +} +fn usage_bytes( + value: Option<&crate::model::usage::ContextUsage>, +) -> Result { + value.map_or(Ok(0), |usage| { + Ok(retained_value_bytes(&usage.meta)? + .saturating_add( + usage + .cost + .as_ref() + .map_or(0, |cost| cost.currency.len().saturating_mul(2)), + ) + .saturating_add(1024)) + }) +} +pub fn admit_user( + state: &ThreadState, + current: usize, + content: &[Content], +) -> Result { + if state.entries.len() >= MAX_LIVE_ENTRIES { + return Err(HistoryCodecError::CollectionLimit); + } + let next = current + .saturating_add(retained_value_bytes(&content)?) + .saturating_add(4096); + if next > MAX_RESIDENT_BYTES { + return Err(HistoryCodecError::StructuralLimit); + } + Ok(next) +} +/// Computes only the changed content; a text append never rescans the existing message. +pub fn admit_update( + state: &ThreadState, + current: usize, + update: &SessionUpdate, +) -> Result { + let Some((removed, added, row)) = update_change(state, update)? else { + return Ok(current); + }; + if state.entries.len().saturating_add(usize::from(row)) > MAX_LIVE_ENTRIES { + return Err(HistoryCodecError::CollectionLimit); + } + let next = current + .saturating_sub(removed) + .saturating_add(added) + .saturating_add(usize::from(row) * 8); + if next > MAX_RESIDENT_BYTES { + return Err(HistoryCodecError::StructuralLimit); + } + Ok(next) +} + +fn update_change( + state: &ThreadState, + update: &SessionUpdate, +) -> Result, HistoryCodecError> { + let change = match update { + SessionUpdate::AgentMessageChunk(content) | SessionUpdate::AgentThoughtChunk(content) => { + let thought = matches!(update, SessionUpdate::AgentThoughtChunk(_)); + match (content, state.entries.last(), thought) { + (Content::Text(text), Some(Entry::Agent(_)), false) + | (Content::Text(text), Some(Entry::Thought(_)), true) => { + (0, text.text.len().saturating_mul(2), false) + } + _ => (0, retained_value_bytes(content)?.saturating_add(4096), true), + } + } + SessionUpdate::UserMessageChunk(content) => ( + 0, + retained_value_bytes(content)?.saturating_add(4096), + !matches!(state.entries.last(), Some(Entry::User(_))), + ), + SessionUpdate::ToolCall(call) => { + (0, retained_value_bytes(call)?.saturating_add(4096), true) + } + SessionUpdate::ToolCallUpdate(patch) => return tool_change(state, patch), + _ => return control_change(state, update), + }; + Ok(Some(change)) +} +fn tool_change( + state: &ThreadState, + patch: &crate::model::tools::ToolPatch, +) -> Result, HistoryCodecError> { + let Some(original) = state.entries.iter().find_map(|entry| match entry { + Entry::Tool(call) if call.id == patch.id => Some(call), + _ => None, + }) else { + return Ok(None); + }; + let mut candidate = original.clone(); + candidate.apply(patch.changes.clone()); + Ok(Some(( + retained_value_bytes(original)?, + retained_value_bytes(&candidate)?, + false, + ))) +} +fn control_change( + state: &ThreadState, + update: &SessionUpdate, +) -> Result, HistoryCodecError> { + let change = match update { + SessionUpdate::Plan(plan) => ( + plan_bytes(state.plan.as_ref()), + plan_bytes(Some(plan)), + false, + ), + SessionUpdate::SessionInfoUpdate(info) => match &info.title { + Change::Set(title) => ( + retained_value_bytes(&state.title)?, + retained_value_bytes(&Some(title))?, + false, + ), + Change::Clear | Change::Keep => return Ok(None), + }, + SessionUpdate::UsageUpdate(usage) => ( + usage_bytes(state.usage.as_ref())?, + usage_bytes(Some(usage))?, + false, + ), + _ => return Ok(None), + }; + Ok(Some(change)) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn repeated_small_chunks_refuse_before_changing_the_accepted_transcript() { + let mut state = ThreadState::default(); + let mut bytes = resident_bytes(&state).unwrap(); + let update = SessionUpdate::AgentMessageChunk(Content::text("x".repeat(1024 * 1024))); + let mut accepted = 0; + while let Ok(next) = admit_update(&state, bytes, &update) { + state.apply(update.clone(), 1); + bytes = next; + assert!(resident_bytes(&state).unwrap() <= bytes); + accepted += 1; + } + assert!(accepted > 1 && accepted < 64); + assert!(matches!( + admit_update(&state, bytes, &update), + Err(HistoryCodecError::StructuralLimit) + )); + assert!( + matches!(&state.entries[0], Entry::Agent(text) if text.len() == accepted * 1024 * 1024) + ); + } + #[test] + fn replacing_a_tool_payload_releases_its_previous_charge() { + use crate::model::tools::{ToolChanges, ToolPatch, ToolRecord}; + let mut state = ThreadState::default(); + state.apply( + SessionUpdate::ToolCall(Box::new(ToolRecord::new("tool", "x".repeat(4096)))), + 1, + ); + let bytes = resident_bytes(&state).unwrap(); + let update = SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + "tool", + ToolChanges::default().title("done"), + ))); + assert!(admit_update(&state, bytes, &update).unwrap() < bytes); + } + #[test] + fn mixed_updates_keep_the_complete_live_state_inside_the_admitted_budget() { + use crate::model::{ + plan::{Plan, PlanEntry, PlanPriority, PlanStatus}, + session::SessionInfo, + tools::{ToolChanges, ToolPatch, ToolRecord}, + usage::ContextUsage, + }; + let mut state = ThreadState::default(); + let mut bytes = resident_bytes(&state).unwrap(); + let updates = vec![ + SessionUpdate::AgentMessageChunk(Content::text("answer")), + SessionUpdate::AgentThoughtChunk(Content::text("thinking")), + SessionUpdate::ToolCall(Box::new(ToolRecord::new("tool", "command"))), + SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + "tool", + ToolChanges::default().raw_output(serde_json::json!({"output": "x".repeat(4096)})), + ))), + SessionUpdate::Plan(Plan { + entries: vec![PlanEntry { + content: "plan".repeat(1024), + priority: PlanPriority::High, + status: PlanStatus::Pending, + }], + }), + SessionUpdate::UsageUpdate(ContextUsage::new(10, 100).meta( + crate::model::extensions::Extensions::from(serde_json::Map::from_iter([( + "detail".into(), + serde_json::json!("x".repeat(2048)), + )])), + )), + SessionUpdate::SessionInfoUpdate(SessionInfo { + title: Change::Set("title".repeat(1024)), + ..Default::default() + }), + ]; + for update in updates { + bytes = admit_update(&state, bytes, &update).unwrap(); + state.apply(update, 1); + assert!(resident_bytes(&state).unwrap() <= bytes); + } + let content = vec![Content::text("user prompt")]; + bytes = admit_user(&state, bytes, &content).unwrap(); + state.push_user(content, 1); + assert!(resident_bytes(&state).unwrap() <= bytes); + } +} diff --git a/crates/ai/src/tool_display.rs b/crates/ai/src/tool_display.rs index ec05e405..37719b02 100644 --- a/crates/ai/src/tool_display.rs +++ b/crates/ai/src/tool_display.rs @@ -63,9 +63,9 @@ impl ToolDisplay { Ok(value) => ToolOutcome::Ok(value), Err(error) => ToolOutcome::Err(crate::redact::redact(error.message())), }; - self.outcome = serde_json::to_vec(&outcome) + self.outcome = crate::history::json_size(&outcome) .ok() - .filter(|bytes| bytes.len() <= 256 * 1024) + .filter(|bytes| *bytes <= 256 * 1024) .map(|_| outcome); } @@ -148,6 +148,16 @@ pub fn request(call: &TerminalCall) -> (&'static str, Value) { TerminalCall::ExecCommand(v) => json!(v), TerminalCall::ReadCommand(v) => json!(v), TerminalCall::CancelCommand(v) => json!(v), + TerminalCall::TransferFiles(v) => json!(v), + TerminalCall::ReadTransfer(v) => json!(v), + TerminalCall::CancelTransfer(v) => json!(v), + TerminalCall::ListFiles(v) => json!(v), + TerminalCall::ReadFile(v) => json!(v), + TerminalCall::WriteFile(v) => json!(v), + TerminalCall::EditFile(v) => json!(v), + TerminalCall::MakeDirectory(v) => json!(v), + TerminalCall::DeletePath(v) => json!(v), + TerminalCall::MovePath(v) => json!(v), }; (call.name(), arguments) } diff --git a/crates/ai/src/tool_display/identity.rs b/crates/ai/src/tool_display/identity.rs index 79fb355c..6b93df69 100644 --- a/crates/ai/src/tool_display/identity.rs +++ b/crates/ai/src/tool_display/identity.rs @@ -73,7 +73,8 @@ fn explicit_identity(call: &ToolRecord) -> Option>> { /// Only exact names or a consistent explicit MCP envelope establish the request /// format. Neither establishes an execution destination. pub fn envelope(call: &ToolRecord, server: &str) -> Option { - envelope_of(call, registration(server)?, parse) + let (tool, arguments) = raw_envelope(call, server)?; + parse(&tool, arguments) } /// Exact provider identity and original arguments, including rejected requests. @@ -102,37 +103,6 @@ pub fn raw_envelope(call: &ToolRecord, server: &str) -> Option<(String, serde_js Some((named?.tool.into(), input.clone())) } -/// `read` turns the tool's arguments into a request: strictly when matching -/// requests the bridge executed, by shape alone when only showing one. -fn envelope_of( - call: &ToolRecord, - registration: &str, - read: fn(&str, serde_json::Value) -> Option, -) -> Option { - let named = explicit_identity(call)?; - if named.is_some_and(|named| named.registration != registration) { - return None; - } - let Some(input) = call.raw_input.as_ref().filter(|input| !input.is_null()) else { - // Hermes omits the input of a call without arguments. - return read(named?.tool, json!({})); - }; - if ["server", "tool", "arguments"] - .iter() - .any(|key| input.get(key).is_some()) - { - let envelope_server = input.get("server")?.as_str()?; - let tool = input.get("tool")?.as_str()?; - if self::registration(envelope_server) != Some(registration) - || named.is_some_and(|named| named.tool != tool) - { - return None; - } - return read(tool, input.get("arguments")?.clone()); - } - read(named?.tool, input.clone()) -} - /// Presentation identity remains useful even when its arguments are malformed. pub fn fallback_tool(call: &ToolRecord) -> Option<&str> { requested_arguments(call).map(|(tool, _)| tool) diff --git a/crates/ai/src/tools.rs b/crates/ai/src/tools.rs index dff6047b..dc8a9ead 100644 --- a/crates/ai/src/tools.rs +++ b/crates/ai/src/tools.rs @@ -1,7 +1,16 @@ pub mod catalog; mod failure; +pub mod files; +pub mod output; +pub mod transfer; -pub use failure::{TerminalCallError, ToolFailure}; +pub use catalog::{ToolGroup, ToolOffer}; +pub use failure::{TerminalCallError, ToolArgumentsError, ToolFailure}; +pub use files::{DeletePath, EditFile, ListFiles, MakeDirectory, MovePath, ReadFile, WriteFile}; +pub use transfer::{ + CancelTransfer, MAX_TRANSFER_FAILURES, MAX_TRANSFER_PATH_BYTES, MAX_TRANSFER_SOURCE_BYTES, + MAX_TRANSFER_SOURCES, ReadTransfer, TransferFiles, +}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -63,12 +72,15 @@ pub enum ToolCapability { LiveInput, Execution, Connection, + Transfer, + /// Changing the files of a terminal that runs no programs. + Files, } -/// Connects to an attached offline server without opening a tab. +/// Opens an allowlisted server or reuses its already connected terminal. #[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] #[serde(deny_unknown_fields)] pub struct OpenTerminal { - /// An `offline_servers` entry of `list_terminals`. + /// A `server_id` from the terminals or offline servers in `list_terminals`. pub server_id: String, } #[derive(Clone, Debug)] @@ -81,19 +93,80 @@ pub enum TerminalCall { ExecCommand(ExecCommand), ReadCommand(ReadCommand), CancelCommand(CancelCommand), + TransferFiles(TransferFiles), + ReadTransfer(ReadTransfer), + CancelTransfer(CancelTransfer), + ListFiles(ListFiles), + ReadFile(ReadFile), + WriteFile(WriteFile), + EditFile(EditFile), + MakeDirectory(MakeDirectory), + DeletePath(DeletePath), + MovePath(MovePath), } impl TerminalCall { + /// Canonical semantic arguments, using the same DTOs as catalog parsing. + /// No execution or provider metadata is included. + pub fn arguments(&self) -> Result { + match self { + Self::ListTerminals => Ok(serde_json::json!({})), + Self::ReadTerminal(v) => serde_json::to_value(v), + Self::SendInput(v) => serde_json::to_value(v), + Self::RunCommand(v) => serde_json::to_value(v), + Self::OpenTerminal(v) => serde_json::to_value(v), + Self::ExecCommand(v) => serde_json::to_value(v), + Self::ReadCommand(v) => serde_json::to_value(v), + Self::CancelCommand(v) => serde_json::to_value(v), + Self::TransferFiles(v) => serde_json::to_value(v), + Self::ReadTransfer(v) => serde_json::to_value(v), + Self::CancelTransfer(v) => serde_json::to_value(v), + Self::ListFiles(v) => serde_json::to_value(v), + Self::ReadFile(v) => serde_json::to_value(v), + Self::WriteFile(v) => serde_json::to_value(v), + Self::EditFile(v) => serde_json::to_value(v), + Self::MakeDirectory(v) => serde_json::to_value(v), + Self::DeletePath(v) => serde_json::to_value(v), + Self::MovePath(v) => serde_json::to_value(v), + } + .map_err(ToolArgumentsError::from) + } pub fn terminal_id(&self) -> Option<&str> { match self { - Self::ListTerminals | Self::OpenTerminal(_) => None, + Self::ListTerminals + | Self::OpenTerminal(_) + | Self::ReadTransfer(_) + | Self::CancelTransfer(_) => None, + Self::TransferFiles(v) => Some(&v.destination_terminal_id), Self::ReadTerminal(v) => Some(&v.terminal_id), Self::SendInput(v) => Some(&v.terminal_id), Self::RunCommand(v) => Some(&v.terminal_id), Self::ExecCommand(v) => Some(&v.terminal_id), Self::ReadCommand(v) => Some(&v.terminal_id), Self::CancelCommand(v) => Some(&v.terminal_id), + Self::ListFiles(v) => Some(&v.terminal_id), + Self::ReadFile(v) => Some(&v.terminal_id), + Self::WriteFile(v) => Some(&v.terminal_id), + Self::EditFile(v) => Some(&v.terminal_id), + Self::MakeDirectory(v) => Some(&v.terminal_id), + Self::DeletePath(v) => Some(&v.terminal_id), + Self::MovePath(v) => Some(&v.terminal_id), + } + } + /// The terminal whose host holds the sources of a transfer. + pub fn source_terminal_id(&self) -> Option<&str> { + match self { + Self::TransferFiles(v) => Some(&v.source_terminal_id), + _ => None, } } + /// Every terminal the call touches: `terminal_id`, then a distinct `source_terminal_id`. + pub fn terminals(&self) -> impl Iterator { + let destination = self.terminal_id(); + let source = self + .source_terminal_id() + .filter(|source| Some(*source) != destination); + destination.into_iter().chain(source) + } pub fn server_id(&self) -> Option<&str> { match self { Self::OpenTerminal(v) => Some(&v.server_id), @@ -104,7 +177,7 @@ impl TerminalCall { pub fn target(&self) -> Option<&str> { self.terminal_id().or_else(|| self.server_id()) } - /// Typing, running and connecting change something; reading does not. + /// Typing, running, connecting and changing files change something; reading does not. pub fn writes(&self) -> bool { matches!( self, @@ -113,16 +186,36 @@ impl TerminalCall { | Self::OpenTerminal(_) | Self::ExecCommand(_) | Self::CancelCommand(_) + | Self::TransferFiles(_) + | Self::CancelTransfer(_) + | Self::WriteFile(_) + | Self::EditFile(_) + | Self::MakeDirectory(_) + | Self::DeletePath(_) + | Self::MovePath(_) ) } + /// A file tool: the call acts on the files of a terminal that runs no programs. + pub fn is_file_tool(&self) -> bool { + self.group() == ToolGroup::Files + } pub fn capability(&self) -> ToolCapability { match self { Self::SendInput(_) | Self::RunCommand(_) => ToolCapability::LiveInput, Self::ExecCommand(_) | Self::CancelCommand(_) => ToolCapability::Execution, Self::OpenTerminal(_) => ToolCapability::Connection, - Self::ListTerminals | Self::ReadTerminal(_) | Self::ReadCommand(_) => { - ToolCapability::Read - } + Self::TransferFiles(_) | Self::CancelTransfer(_) => ToolCapability::Transfer, + Self::WriteFile(_) + | Self::EditFile(_) + | Self::MakeDirectory(_) + | Self::DeletePath(_) + | Self::MovePath(_) => ToolCapability::Files, + Self::ListTerminals + | Self::ReadTerminal(_) + | Self::ReadCommand(_) + | Self::ReadTransfer(_) + | Self::ListFiles(_) + | Self::ReadFile(_) => ToolCapability::Read, } } pub fn validate(&self) -> Result<(), TerminalCallError> { @@ -182,6 +275,16 @@ impl TerminalCall { Self::CancelCommand(v) if v.command_id.is_empty() || v.command_id.len() > 128 => { Err(TerminalCallError::InvalidCommandId) } + Self::TransferFiles(v) => v.validate(), + Self::ReadTransfer(v) => v.validate(), + Self::CancelTransfer(v) => v.validate(), + Self::ListFiles(v) => v.validate(), + Self::ReadFile(v) => v.validate(), + Self::WriteFile(v) => v.validate(), + Self::EditFile(v) => v.validate(), + Self::MakeDirectory(v) => v.validate(), + Self::DeletePath(v) => v.validate(), + Self::MovePath(v) => v.validate(), _ => Ok(()), } } diff --git a/crates/ai/src/tools/catalog.rs b/crates/ai/src/tools/catalog.rs index d592a103..9c1c1db8 100644 --- a/crates/ai/src/tools/catalog.rs +++ b/crates/ai/src/tools/catalog.rs @@ -5,20 +5,58 @@ use serde_json::{Value, json}; pub struct ToolSpec { pub name: &'static str, pub label: &'static str, + /// Terminal tools are always listed; file tools only while a chat offers them. + pub group: ToolGroup, pub description: &'static str, pub schema: fn() -> Value, /// Shape only: execution limits are checked separately by `validate`. pub parse: fn(Value) -> Option, } +/// The tools an agent is given together. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ToolGroup { + /// The terminals, commands and transfers of a chat: always offered. + Terminals, + /// The files of attached terminals that serve files but run no programs. + Files, +} + +/// What a chat offers its agent now beyond the terminal tools. The bridge lists and admits only +/// these, and tells a connected agent when the offer changes. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct ToolOffer { + files: bool, +} + +impl ToolOffer { + /// The terminal tools and, when `files` is true, the file tools. + pub fn new(files: bool) -> Self { + Self { files } + } + + pub fn offers(self, group: ToolGroup) -> bool { + match group { + ToolGroup::Terminals => true, + ToolGroup::Files => self.files, + } + } + + /// The tools listed under this offer, in catalog order. + pub fn tools(self) -> impl Iterator { + TOOLS.iter().filter(move |spec| self.offers(spec.group)) + } +} + // The exhaustive name match makes each TerminalCall variant require a row here. macro_rules! catalog { - ($($variant:ident $(($arguments:ty))? => $name:literal, $label:literal, $description:literal;)*) => { + ($($variant:ident $(($arguments:ty))? => $name:literal, $label:literal, $group:ident, $description:literal;)*) => { enum Index { $($variant,)* } pub static TOOLS: &[ToolSpec] = &[$(ToolSpec { name: $name, label: $label, + group: ToolGroup::$group, description: $description, schema: catalog!(@schema $($arguments)?), parse: catalog!(@parse $variant $($arguments)?), @@ -30,6 +68,11 @@ macro_rules! catalog { $(catalog!(@pattern $variant $($arguments)?) => TOOLS[Index::$variant as usize].name,)* } } + pub fn group(&self) -> ToolGroup { + match self { + $(catalog!(@pattern $variant $($arguments)?) => TOOLS[Index::$variant as usize].group,)* + } + } } }; (@pattern $variant:ident) => { Self::$variant }; @@ -43,22 +86,42 @@ macro_rules! catalog { } catalog! { - ListTerminals => "list_terminals", "List terminals", + ListTerminals => "list_terminals", "List terminals", Terminals, "List terminals attached to this chat, and attached servers that have no session yet (offline_servers). These are the only servers you can reach: nocterm holds their credentials, so never connect on your own with ssh. When the list is empty, tell the user to attach a terminal or server to the chat"; - OpenTerminal(OpenTerminal) => "open_terminal", "Connect", + OpenTerminal(OpenTerminal) => "open_terminal", "Connect", Terminals, "Connect to an attached offline server in the background, without disturbing the user; returns its terminal_id once connected. If it fails, tell the user in one short sentence why and stop"; - ReadTerminal(ReadTerminal) => "read_terminal", "Read terminal", + ReadTerminal(ReadTerminal) => "read_terminal", "Read terminal", Terminals, "Read bounded output of an attached terminal"; - SendInput(SendInput) => "send_input", "Send input", + SendInput(SendInput) => "send_input", "Send input", Terminals, "Send text to an attached terminal with user approval"; - RunCommand(RunCommand) => "run_command", "Run command", + RunCommand(RunCommand) => "run_command", "Run command", Terminals, "Type one command into a known empty live shell prompt. Requires shell integration; an output pause never proves completion. Prefer exec_command for a reliable exit status"; - ExecCommand(ExecCommand) => "exec_command", "Execute program", + ExecCommand(ExecCommand) => "exec_command", "Execute program", Terminals, "Run program with separate args and optional stdin on the attached terminal host over its current connection. Local execution passes args directly; SSH renders a POSIX command line, so arbitrary args require a POSIX-compatible remote command shell and are not guaranteed on Windows SSH. Fresh process: no inherited terminal cwd, environment, aliases or functions. Use an explicit shell for shell syntax. Returns actual exit status or an owned command_id for read_command/cancel_command. Output is bounded and continuously drained"; - ReadCommand(ReadCommand) => "read_command", "Read command", + ReadCommand(ReadCommand) => "read_command", "Read command", Terminals, "Read a bounded full snapshot of a command owned by this chat. Does not start or cancel a command; optional yield_ms waits briefly"; - CancelCommand(CancelCommand) => "cancel_command", "Cancel command", + CancelCommand(CancelCommand) => "cancel_command", "Cancel command", Terminals, "Cancel a command owned by this chat. SSH sends TERM and closes its channel; remote descendant termination is best effort"; + TransferFiles(TransferFiles) => "transfer_files", "Transfer files", Terminals, + "Copy files and folders between the hosts of two attached terminals: this computer (a local terminal) to a server, a server to this computer, or a server to another server. nocterm relays the data over the connections it already holds, so servers need no credentials for each other and you must never use scp, rsync or ssh between them. Every path is absolute (no ~, no .. components). sources are files or folders on the source host; folders copy recursively, and each source is copied into destination under its own name. destination must be an existing folder; it is never created. Existing files are skipped and counted as skipped unless overwrite is true. Symbolic links and special files are skipped. At most 64 sources per call. Two local terminals are refused: use exec_command. Some environments only allow copies within one server. The copy runs in the background: after optional yield_ms (default 1000) it returns the current state and a transfer_id for read_transfer/cancel_transfer. Poll read_transfer until the state is completed, failed or cancelled. Whole files are published atomically; a cancelled or failed file leaves no partial file"; + ReadTransfer(ReadTransfer) => "read_transfer", "Read transfer", Terminals, + "Read a snapshot of a transfer started by this chat: state, file and byte counts and up to 16 failures. Does not start or cancel a transfer; optional yield_ms waits briefly while it is active"; + CancelTransfer(CancelTransfer) => "cancel_transfer", "Cancel transfer", Terminals, + "Cancel a transfer started by this chat. Files already published stay in place; files being copied are discarded"; + ListFiles(ListFiles) => "list_files", "List files", Files, + "List a folder of an attached terminal whose list_terminals entry has file_tools: true. Such a terminal serves files but runs no programs (for example a console tab a plugin opened for a game server panel): exec_command and run_command cannot reach its files, so use the file tools for them. Paths are absolute from / with no ~, . or .. parts. Folders come first; at most 1000 entries. The file tools act only on terminals with file_tools: true, never on other terminals or this computer"; + ReadFile(ReadFile) => "read_file", "Read file", Files, + "Read a UTF-8 text file of an attached terminal with file_tools: true, up to 1 MiB, a range of lines at a time (start_line from 1, max_lines up to 2000, at most 64 KiB per answer; next_start_line says where to continue). The content is exactly the file's text, except where it is shown as [REDACTED]: such text is hidden from you, so change other parts with edit_file and never write the marker back"; + WriteFile(WriteFile) => "write_file", "Write file", Files, + "Write a whole UTF-8 text file (at most 512 KiB) on an attached terminal with file_tools: true, with user approval. An existing file is refused unless overwrite is true; prefer edit_file to change part of a file. The parent folder must exist"; + EditFile(EditFile) => "edit_file", "Edit file", Files, + "Replace exact text in a file of an attached terminal with file_tools: true, with user approval. old_text must occur exactly once, copied exactly from read_file with its whitespace, unless replace_all is true. Send only the part that changes; the file is changed on its host"; + MakeDirectory(MakeDirectory) => "make_directory", "Create folder", Files, + "Create a folder on an attached terminal with file_tools: true, with user approval. Its parent must exist; an existing folder is accepted"; + DeletePath(DeletePath) => "delete_path", "Delete", Files, + "Delete a file or folder on an attached terminal with file_tools: true, with user approval. A folder is removed as the terminal's host removes folders, which may include its contents; it cannot be undone"; + MovePath(MovePath) => "move_path", "Move", Files, + "Move or rename a file or folder within one attached terminal with file_tools: true, with user approval. Both paths are absolute"; } pub fn get(name: &str) -> Option<&'static ToolSpec> { diff --git a/crates/ai/src/tools/catalog/tests.rs b/crates/ai/src/tools/catalog/tests.rs index df4d3a4b..18057d9e 100644 --- a/crates/ai/src/tools/catalog/tests.rs +++ b/crates/ai/src/tools/catalog/tests.rs @@ -10,18 +10,35 @@ fn fixtures() -> Vec { serde_json::from_str(include_str!("../fixtures/calls.json")).unwrap() } +/// Every tool offered, as while a terminal with file tools is attached. +const ALL: ToolOffer = ToolOffer { files: true }; + fn session() -> McpSession { let mut session = McpSession::default(); - session.handle(r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#); + session.handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + ToolOffer::default(), + ); session } fn invoke(session: &mut McpSession, name: &str, arguments: &Value, id: Value) -> McpStep { + invoke_under(session, name, arguments, id, ALL) +} + +fn invoke_under( + session: &mut McpSession, + name: &str, + arguments: &Value, + id: Value, + offer: ToolOffer, +) -> McpStep { session.handle( &json!({"jsonrpc":"2.0","id":id,"method":"tools/call", "params":{ "name":name,"arguments":arguments }}) .to_string(), + offer, ) } @@ -36,6 +53,16 @@ fn correct_variant(name: &str, call: &TerminalCall) -> bool { | ("exec_command", TerminalCall::ExecCommand(_)) | ("read_command", TerminalCall::ReadCommand(_)) | ("cancel_command", TerminalCall::CancelCommand(_)) + | ("transfer_files", TerminalCall::TransferFiles(_)) + | ("read_transfer", TerminalCall::ReadTransfer(_)) + | ("cancel_transfer", TerminalCall::CancelTransfer(_)) + | ("list_files", TerminalCall::ListFiles(_)) + | ("read_file", TerminalCall::ReadFile(_)) + | ("write_file", TerminalCall::WriteFile(_)) + | ("edit_file", TerminalCall::EditFile(_)) + | ("make_directory", TerminalCall::MakeDirectory(_)) + | ("delete_path", TerminalCall::DeletePath(_)) + | ("move_path", TerminalCall::MovePath(_)) ) } @@ -43,19 +70,85 @@ fn correct_variant(name: &str, call: &TerminalCall) -> bool { fn tools_list_preserves_original_order_descriptions_and_schemas() { let expected: Value = serde_json::from_str(include_str!("../fixtures/tools-list.json")).unwrap(); - let McpStep::Reply(actual) = - session().handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/list"}"#) - else { + let McpStep::Reply(actual) = session().handle( + r#"{"jsonrpc":"2.0","id":2,"method":"tools/list"}"#, + ToolOffer::default(), + ) else { panic!("expected tools/list reply"); }; - assert_eq!(actual, expected); + assert_eq!( + actual, expected, + "without file terminals the list is as before" + ); +} + +#[test] +fn file_tools_are_listed_and_admitted_only_while_offered() { + let listed = |offer| { + let McpStep::Reply(reply) = + session().handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/list"}"#, offer) + else { + panic!("expected tools/list reply"); + }; + reply["result"]["tools"] + .as_array() + .unwrap() + .iter() + .map(|tool| tool["name"].as_str().unwrap().to_owned()) + .collect::>() + }; + let without = listed(ToolOffer::default()); + let with = listed(ToolOffer::new(true)); + assert!(!without.iter().any(|name| name == "read_file")); + assert_eq!(&with[..without.len()], without.as_slice()); + assert_eq!( + &with[without.len()..], + [ + "list_files", + "read_file", + "write_file", + "edit_file", + "make_directory", + "delete_path", + "move_path" + ] + ); + let arguments = json!({"terminal_id":"t3","path":"/server.properties"}); + let McpStep::Rejected { error, .. } = invoke_under( + &mut session(), + "read_file", + &arguments, + json!(5), + ToolOffer::default(), + ) else { + panic!("a tool that is not offered is refused"); + }; + assert!(error.contains("file_tools"), "{error}"); + assert!(matches!( + invoke_under(&mut session(), "read_file", &arguments, json!(6), ALL), + McpStep::Call { + call: TerminalCall::ReadFile(_), + .. + } + )); + let McpStep::Reply(initialized) = McpSession::default().handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + ToolOffer::default(), + ) else { + panic!("expected initialize reply"); + }; + assert_eq!( + initialized["result"]["capabilities"]["tools"]["listChanged"], + json!(true), + "the agent is told the list may change" + ); } #[test] fn every_variant_round_trips_through_a_unique_catalogue_entry() { let fixtures = fixtures(); let names: BTreeSet<_> = TOOLS.iter().map(|spec| spec.name).collect(); - assert_eq!(TOOLS.len(), 8); + assert_eq!(TOOLS.len(), 18); assert_eq!(TOOLS.len(), names.len()); assert_eq!(fixtures.len(), names.len()); assert_eq!( @@ -112,7 +205,7 @@ fn mcp_preserves_original_arguments_ids_and_optional_defaults_for_all_tools() { } } assert!( - matches!(session.handle(r#"{"jsonrpc":"2.0","id":42,"method":"tools/call","params":{"name":"list_terminals"}}"#), + matches!(session.handle(r#"{"jsonrpc":"2.0","id":42,"method":"tools/call","params":{"name":"list_terminals"}}"#, ToolOffer::default()), McpStep::Call { call: TerminalCall::ListTerminals, arguments, .. } if arguments == json!({})) ); } @@ -137,6 +230,16 @@ fn catalogue_and_mcp_reject_unknown_tools_bad_shapes_and_extra_fields() { ), ("open_terminal", json!({"server_id":null})), ("read_command", json!({"terminal_id":"t1"})), + ( + "transfer_files", + json!({"source_terminal_id":"t1","sources":"/a","destination_terminal_id":"t2","destination":"/b"}), + ), + ( + "transfer_files", + json!({"source_terminal_id":"t1","sources":["/a"],"destination_terminal_id":"t2","destination":"/b","extra":true}), + ), + ("read_transfer", json!({"transfer_id":7})), + ("cancel_transfer", json!({})), ] { assert!( get(name) @@ -209,3 +312,17 @@ fn both_metadata_versions_use_the_same_shape_parser_without_losing_outcomes() { } } } + +#[test] +fn canonical_tool_arguments_round_trip_through_every_existing_catalog_schema() { + let fixtures: Value = serde_json::from_str(include_str!("../fixtures/calls.json")).unwrap(); + for fixture in fixtures.as_array().unwrap() { + let name = fixture["name"].as_str().unwrap(); + let spec = get(name).unwrap(); + let call = (spec.parse)(fixture["arguments"].clone()).unwrap(); + let arguments = call.arguments().unwrap(); + let restored = (spec.parse)(arguments.clone()).unwrap(); + assert_eq!(restored.arguments().unwrap(), arguments); + assert_eq!(restored.name(), name); + } +} diff --git a/crates/ai/src/tools/failure.rs b/crates/ai/src/tools/failure.rs index 2e408bbb..42658f11 100644 --- a/crates/ai/src/tools/failure.rs +++ b/crates/ai/src/tools/failure.rs @@ -21,6 +21,26 @@ pub enum TerminalCallError { ReadCommandRejected, #[error("Invalid command id")] InvalidCommandId, + #[error( + "Transfer needs 1–64 absolute source paths and one destination folder (each at most 4096 bytes without control characters, sources at most 64 KiB in total) and a yield of at most 10000 ms" + )] + TransferRejected, + #[error("Invalid transfer id or yield interval")] + ReadTransferRejected, + #[error("Invalid transfer id")] + InvalidTransferId, + #[error( + "File paths are absolute, at most 4096 bytes, without control characters and without empty, `.` or `..` parts; a change or a read names something below `/`, and a move does not go into itself" + )] + FilePathRejected, + #[error("start_line is at least 1 and max_lines 1–2000")] + ReadRangeRejected, + #[error("Content is text of at most 512 KiB without NUL")] + FileContentRejected, + #[error( + "old_text is nonempty, old_text and new_text are at most 64 KiB each without NUL, and they differ" + )] + EditRejected, } /// Agent-facing text of a failed tool call. @@ -61,3 +81,18 @@ impl From for ToolFailure { error.to_string().into() } } + +/// Serialization failed without discarding its original concrete cause. +#[derive(Clone, thiserror::Error)] +#[error("Terminal tool arguments could not be encoded")] +pub struct ToolArgumentsError(#[source] nocterm_core::SharedError); +impl From for ToolArgumentsError { + fn from(error: serde_json::Error) -> Self { + Self(nocterm_core::SharedError::from_error(error)) + } +} +impl std::fmt::Debug for ToolArgumentsError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("ToolArgumentsError") + } +} diff --git a/crates/ai/src/tools/files.rs b/crates/ai/src/tools/files.rs new file mode 100644 index 00000000..4d5783dc --- /dev/null +++ b/crates/ai/src/tools/files.rs @@ -0,0 +1,205 @@ +//! File tools of attached terminals that serve files but run no programs: arguments, bounds, +//! and the work over the terminal's filesystem. +//! +//! An agent reaches such a terminal's files only through these tools: it cannot run a program +//! there to read or change them. Paths are absolute without `.` or `..` components; files are +//! read and written whole as UTF-8 text within the bounds below, and an edit replaces exact +//! text, so an agent sends only what changes. +use super::TerminalCallError; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +mod work; + +pub use work::run; + +pub const MAX_FILE_PATH_BYTES: usize = 4096; +/// The most text one `read_file` answer carries. +pub const MAX_FILE_READ_BYTES: usize = 64 * 1024; +/// The largest file read or edited whole. +pub const MAX_FILE_LOAD_BYTES: usize = 1024 * 1024; +/// The largest content `write_file` takes. +pub const MAX_FILE_WRITE_BYTES: usize = 512 * 1024; +/// The largest `old_text` or `new_text` of an edit. +pub const MAX_EDIT_BYTES: usize = 64 * 1024; +pub const MAX_LISTED_ENTRIES: usize = 1000; +pub const MAX_READ_FILE_LINES: usize = 2000; + +/// List a folder of an attached terminal that has `file_tools`. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ListFiles { + pub terminal_id: String, + /// Absolute path of the folder; `/` is the top. + pub path: String, +} + +/// Read a text file, a range of its lines at a time. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ReadFile { + pub terminal_id: String, + pub path: String, + /// First line to read, from 1 (default 1). + pub start_line: Option, + /// Lines to read, 1–2000 (default 2000); an answer also stops at 64 KiB. + pub max_lines: Option, +} + +/// Write a whole text file. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct WriteFile { + pub terminal_id: String, + pub path: String, + /// The whole new content, UTF-8 text of at most 512 KiB. + pub content: String, + /// Replace a file that exists; by default an existing file is refused. + #[serde(default)] + pub overwrite: bool, +} + +/// Replace exact text in a file. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct EditFile { + pub terminal_id: String, + pub path: String, + /// Text that occurs in the file exactly once, unless `replace_all` is true. + pub old_text: String, + /// The text it becomes. + pub new_text: String, + /// Replace every occurrence. + #[serde(default)] + pub replace_all: bool, +} + +/// Create a folder; its parent must exist. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct MakeDirectory { + pub terminal_id: String, + pub path: String, +} + +/// Delete a file, or a folder as the terminal's host removes one. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct DeletePath { + pub terminal_id: String, + pub path: String, +} + +/// Move or rename a file or folder. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct MovePath { + pub terminal_id: String, + pub from: String, + pub to: String, +} + +/// An absolute path with no empty, `.` or `..` component and no control character, so a path +/// names one place and reads plainly on an approval card. +pub fn path_ok(path: &str) -> bool { + path.len() <= MAX_FILE_PATH_BYTES + && !path.chars().any(char::is_control) + && (path == "/" + || path.strip_prefix('/').is_some_and(|rest| { + rest.split('/') + .all(|part| !part.is_empty() && part != "." && part != "..") + })) +} + +fn paths(paths: &[&str]) -> Result<(), TerminalCallError> { + if paths.iter().all(|path| path_ok(path)) { + Ok(()) + } else { + Err(TerminalCallError::FilePathRejected) + } +} + +/// A path below the top, which a change may name. +fn below_top(path: &str) -> Result<(), TerminalCallError> { + paths(&[path])?; + if path == "/" { + Err(TerminalCallError::FilePathRejected) + } else { + Ok(()) + } +} + +impl ListFiles { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + paths(&[&self.path]) + } +} + +impl ReadFile { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path)?; + if self.start_line == Some(0) + || self + .max_lines + .is_some_and(|n| n == 0 || n > MAX_READ_FILE_LINES) + { + return Err(TerminalCallError::ReadRangeRejected); + } + Ok(()) + } +} + +impl WriteFile { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path)?; + if self.content.len() > MAX_FILE_WRITE_BYTES || self.content.contains('\0') { + return Err(TerminalCallError::FileContentRejected); + } + Ok(()) + } +} + +impl EditFile { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path)?; + if self.old_text.is_empty() + || self.old_text.len() > MAX_EDIT_BYTES + || self.new_text.len() > MAX_EDIT_BYTES + || self.old_text == self.new_text + || self.new_text.contains('\0') + { + return Err(TerminalCallError::EditRejected); + } + Ok(()) + } +} + +impl MakeDirectory { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path) + } +} + +impl DeletePath { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path) + } +} + +impl MovePath { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.from)?; + below_top(&self.to)?; + let inside = self + .to + .strip_prefix(&self.from) + .is_some_and(|rest| rest.is_empty() || rest.starts_with('/')); + if inside { + return Err(TerminalCallError::FilePathRejected); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ai/src/tools/files/tests.rs b/crates/ai/src/tools/files/tests.rs new file mode 100644 index 00000000..a994052e --- /dev/null +++ b/crates/ai/src/tools/files/tests.rs @@ -0,0 +1,254 @@ +//! File tools: their bounds, and their work on a terminal's filesystem. +use std::sync::Arc; + +use futures::executor::block_on; +use nocterm_session::{RemoteFs, testing::InMemoryFs}; +use serde_json::{Value, json}; + +use super::*; +use crate::{TerminalCall, TerminalCallError, ToolCapability, ToolFailure, ToolGroup}; + +fn files() -> InMemoryFs { + let fs = InMemoryFs::default(); + fs.seed_file("/server.properties", "motd=Hi\nmax-players=20\npvp=true\n") + .unwrap(); + fs.seed_file("/plugins/a.yml", "a: 1\n").unwrap(); + fs +} + +fn run_on(fs: &InMemoryFs, call: TerminalCall, redact: bool) -> Result { + let shared: Arc = Arc::new(fs.clone()); + block_on(run(call, shared, redact)) +} + +fn text(fs: &InMemoryFs, path: &str) -> String { + String::from_utf8(fs.bytes(path).unwrap()).unwrap() +} + +fn edit(old: &str, new: &str, replace_all: bool) -> TerminalCall { + TerminalCall::EditFile(EditFile { + terminal_id: "t1".into(), + path: "/server.properties".into(), + old_text: old.into(), + new_text: new.into(), + replace_all, + }) +} + +#[test] +fn paths_name_one_place_below_the_top() { + for path in ["/", "/a", "/a/b.txt", "/with space/ü"] { + assert!(path_ok(path), "{path}"); + } + for path in ["", "a", "/a/", "//a", "/a/../b", "/./a", "/a\nb", "~/a"] { + assert!(!path_ok(path), "{path:?}"); + } + let delete = |path: &str| { + TerminalCall::DeletePath(DeletePath { + terminal_id: "t1".into(), + path: path.into(), + }) + .validate() + }; + assert_eq!(delete("/"), Err(TerminalCallError::FilePathRejected)); + assert_eq!(delete("/logs"), Ok(())); + let moved = TerminalCall::MovePath(MovePath { + terminal_id: "t1".into(), + from: "/world".into(), + to: "/world/inside".into(), + }); + assert_eq!(moved.validate(), Err(TerminalCallError::FilePathRejected)); + assert_eq!( + edit("same", "same", false).validate(), + Err(TerminalCallError::EditRejected) + ); +} + +#[test] +fn reading_is_a_read_and_every_change_needs_the_files_capability() { + let read = TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/a".into(), + start_line: None, + max_lines: None, + }); + assert!(!read.writes()); + assert_eq!(read.capability(), ToolCapability::Read); + assert_eq!(read.group(), ToolGroup::Files); + let change = edit("a", "b", false); + assert!(change.writes() && change.is_file_tool()); + assert_eq!(change.capability(), ToolCapability::Files); + assert_eq!(change.terminal_id(), Some("t1")); +} + +#[test] +fn a_listing_shows_folders_first_with_the_sizes_of_files() { + let listed = run_on( + &files(), + TerminalCall::ListFiles(ListFiles { + terminal_id: "t1".into(), + path: "/".into(), + }), + false, + ) + .unwrap(); + assert_eq!( + listed["entries"], + json!([ + {"name": "plugins", "type": "directory"}, + {"name": "server.properties", "type": "file", "size": 32} + ]) + ); + assert_eq!(listed["truncated"], json!(false)); +} + +#[test] +fn a_file_is_read_a_range_of_lines_at_a_time() { + let fs = files(); + let read = |start, max| { + run_on( + &fs, + TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/server.properties".into(), + start_line: start, + max_lines: max, + }), + false, + ) + .unwrap() + }; + let first = read(None, Some(2)); + assert_eq!(first["content"], json!("motd=Hi\nmax-players=20\n")); + assert_eq!(first["total_lines"], json!(3)); + assert_eq!(first["next_start_line"], json!(3)); + let rest = read(Some(3), None); + assert_eq!(rest["content"], json!("pvp=true\n")); + assert!(rest.get("next_start_line").is_none()); +} + +#[test] +fn an_edit_replaces_exact_text_once_or_says_why_not() { + let fs = files(); + let answer = run_on(&fs, edit("max-players=20", "max-players=40", false), false).unwrap(); + assert_eq!(answer["replacements"], json!(1)); + assert_eq!( + text(&fs, "/server.properties"), + "motd=Hi\nmax-players=40\npvp=true\n" + ); + let missing = run_on(&fs, edit("max-players=20", "x", false), false).unwrap_err(); + assert!(missing.message().contains("not found"), "{missing}"); + let twice = run_on(&fs, edit("=", ":", false), false).unwrap_err(); + assert!(twice.message().contains("occurs 3 times"), "{twice}"); + let all = run_on(&fs, edit("=", ":", true), false).unwrap(); + assert_eq!(all["replacements"], json!(3)); + assert_eq!( + text(&fs, "/server.properties"), + "motd:Hi\nmax-players:40\npvp:true\n" + ); +} + +#[test] +fn a_write_creates_a_file_and_replaces_one_only_when_asked() { + let fs = files(); + let write = |path: &str, overwrite| { + TerminalCall::WriteFile(WriteFile { + terminal_id: "t1".into(), + path: path.into(), + content: "[]\n".into(), + overwrite, + }) + }; + run_on(&fs, write("/ops.json", false), false).unwrap(); + assert_eq!(text(&fs, "/ops.json"), "[]\n"); + let refused = run_on(&fs, write("/server.properties", false), false).unwrap_err(); + assert!(refused.message().contains("set overwrite"), "{refused}"); + run_on(&fs, write("/server.properties", true), false).unwrap(); + assert_eq!(text(&fs, "/server.properties"), "[]\n"); +} + +#[test] +fn hidden_secrets_are_never_written_back_but_the_rest_of_the_file_can_change() { + let fs = InMemoryFs::default(); + fs.seed_file("/config.yml", "name: lobby\npassword: hunter2\n") + .unwrap(); + let read = run_on( + &fs, + TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/config.yml".into(), + start_line: None, + max_lines: None, + }), + true, + ) + .unwrap(); + assert_eq!(read["redacted"], json!(true)); + let shown = read["content"].as_str().unwrap(); + assert!(!shown.contains("hunter2"), "{shown}"); + let rewrite = TerminalCall::WriteFile(WriteFile { + terminal_id: "t1".into(), + path: "/config.yml".into(), + content: shown.replace("lobby", "hub"), + overwrite: true, + }); + assert!(run_on(&fs, rewrite, true).is_err()); + let rename = TerminalCall::EditFile(EditFile { + terminal_id: "t1".into(), + path: "/config.yml".into(), + old_text: "name: lobby".into(), + new_text: "name: hub".into(), + replace_all: false, + }); + run_on(&fs, rename, true).unwrap(); + assert_eq!(text(&fs, "/config.yml"), "name: hub\npassword: hunter2\n"); +} + +#[test] +fn folders_are_made_moved_and_deleted() { + let fs = files(); + let call = |call| run_on(&fs, call, false); + call(TerminalCall::MakeDirectory(MakeDirectory { + terminal_id: "t1".into(), + path: "/backups".into(), + })) + .unwrap(); + call(TerminalCall::MovePath(MovePath { + terminal_id: "t1".into(), + from: "/plugins/a.yml".into(), + to: "/backups/a.yml".into(), + })) + .unwrap(); + assert_eq!(text(&fs, "/backups/a.yml"), "a: 1\n"); + let deleted = call(TerminalCall::DeletePath(DeletePath { + terminal_id: "t1".into(), + path: "/plugins".into(), + })) + .unwrap(); + assert_eq!(deleted["deleted"], json!("directory")); + let gone = call(TerminalCall::DeletePath(DeletePath { + terminal_id: "t1".into(), + path: "/plugins".into(), + })) + .unwrap_err(); + assert!(gone.message().contains("no such file"), "{gone}"); +} + +#[test] +fn a_binary_file_is_refused_with_the_way_to_copy_it() { + let fs = InMemoryFs::default(); + fs.seed_file("/world/level.dat", vec![0x1f, 0x8b, 0xff, 0x00]) + .unwrap(); + let refused = run_on( + &fs, + TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/world/level.dat".into(), + start_line: None, + max_lines: None, + }), + false, + ) + .unwrap_err(); + assert!(refused.message().contains("transfer_files"), "{refused}"); +} diff --git a/crates/ai/src/tools/files/work.rs b/crates/ai/src/tools/files/work.rs new file mode 100644 index 00000000..61064367 --- /dev/null +++ b/crates/ai/src/tools/files/work.rs @@ -0,0 +1,243 @@ +//! The work of one file tool call over a terminal's filesystem, and the answer the agent reads. +use std::sync::Arc; + +use nocterm_session::{ + EntryKind, FsError, RemoteFs, + fs::{DownloadReadError, UploadMode, read_bounded}, +}; +use serde_json::{Value, json}; + +use super::{ + DeletePath, EditFile, MAX_FILE_LOAD_BYTES, MAX_FILE_READ_BYTES, MAX_LISTED_ENTRIES, + MAX_READ_FILE_LINES, ReadFile, WriteFile, +}; +use crate::{TerminalCall, ToolFailure}; + +/// What redaction leaves where it hid text. +const REDACTED: &str = "[REDACTED"; +/// The chunk a file is read or written in. +const CHUNK: usize = 32 * 1024; + +/// Runs the file tool call `call` on `fs`. Text read is redacted when `redact` is set, and a +/// write that would put a redaction marker into a file is then refused: hidden text must never +/// be replaced by its marker. +pub async fn run( + call: TerminalCall, + fs: Arc, + redact: bool, +) -> Result { + let fs = fs.as_ref(); + match call { + TerminalCall::ListFiles(request) => list(fs, &request.path).await, + TerminalCall::ReadFile(request) => read(fs, &request, redact).await, + TerminalCall::WriteFile(request) => write(fs, &request, redact).await, + TerminalCall::EditFile(request) => edit(fs, &request).await, + TerminalCall::MakeDirectory(request) => { + fs.create_dir(&request.path).await.map_err(failed)?; + Ok(json!({"path": request.path, "created": true})) + } + TerminalCall::DeletePath(request) => delete(fs, &request).await, + TerminalCall::MovePath(request) => { + fs.rename(&request.from, &request.to) + .await + .map_err(failed)?; + Ok(json!({"from": request.from, "to": request.to})) + } + _ => Err("This call is not a file tool.".into()), + } +} + +fn failed(error: FsError) -> ToolFailure { + error.to_string().into() +} + +fn kind(kind: EntryKind) -> &'static str { + match kind { + EntryKind::Directory => "directory", + EntryKind::File => "file", + EntryKind::Other => "other", + } +} + +/// Folders first, then files, each by name; at most [`MAX_LISTED_ENTRIES`]. +async fn list(fs: &dyn RemoteFs, path: &str) -> Result { + let mut entries = fs.read_dir(path).await.map_err(failed)?; + entries.sort_by(|a, b| { + (a.kind != EntryKind::Directory, &a.name).cmp(&(b.kind != EntryKind::Directory, &b.name)) + }); + let truncated = entries.len() > MAX_LISTED_ENTRIES; + let listed: Vec = entries + .iter() + .take(MAX_LISTED_ENTRIES) + .map( + |entry| match entry.size.filter(|_| entry.kind == EntryKind::File) { + Some(size) => json!({"name": entry.name, "type": kind(entry.kind), "size": size}), + None => json!({"name": entry.name, "type": kind(entry.kind)}), + }, + ) + .collect(); + Ok(json!({"path": path, "entries": listed, "truncated": truncated})) +} + +/// The whole file as text, at most [`MAX_FILE_LOAD_BYTES`]. +async fn load(fs: &dyn RemoteFs, path: &str) -> Result { + let mut download = fs.download(path).await.map_err(failed)?; + let bytes = read_bounded(download.as_mut(), MAX_FILE_LOAD_BYTES, CHUNK).await; + let _ = download.close().await; + let bytes = bytes.map_err(|error| -> ToolFailure { + match error { + DownloadReadError::TooLarge { .. } => format!( + "{path}: larger than {} KiB; copy it with transfer_files instead", + MAX_FILE_LOAD_BYTES / 1024 + ) + .into(), + DownloadReadError::Read(error) => failed(error), + other => other.to_string().into(), + } + })?; + String::from_utf8(bytes) + .map_err(|_| format!("{path}: not UTF-8 text; copy it with transfer_files instead").into()) +} + +/// Lines `start_line..` of the file, as many as fit [`MAX_FILE_READ_BYTES`]; a longer single +/// line is cut and said to be. +async fn read(fs: &dyn RemoteFs, request: &ReadFile, redact: bool) -> Result { + let text = load(fs, &request.path).await?; + let start = request.start_line.unwrap_or(1); + let max = request.max_lines.unwrap_or(MAX_READ_FILE_LINES); + let total = text.split_inclusive('\n').count(); + let (mut content, mut shown, mut cut) = (String::new(), 0, false); + for line in text.split_inclusive('\n').skip(start - 1).take(max) { + if content.len() + line.len() > MAX_FILE_READ_BYTES { + if shown == 0 { + let mut end = MAX_FILE_READ_BYTES; + while !line.is_char_boundary(end) { + end -= 1; + } + content.push_str(&line[..end]); + (shown, cut) = (1, true); + } + break; + } + content.push_str(line); + shown += 1; + } + let next = start + shown; + let mut answer = json!({ + "path": request.path, + "start_line": start, + "lines": shown, + "total_lines": total, + "content": content, + }); + if next <= total { + answer["next_start_line"] = json!(next); + } + if cut { + answer["line_cut"] = json!(true); + } + if redact { + let hidden = crate::redact::redact(&content); + if hidden != content { + answer["content"] = json!(hidden); + answer["redacted"] = json!(true); + answer["note"] = json!( + "Text shown as [REDACTED] is hidden, not the file's text: change other parts with edit_file." + ); + } + } + Ok(answer) +} + +/// Writes `bytes` as the whole file at `path`. +async fn store( + fs: &dyn RemoteFs, + path: &str, + bytes: &[u8], + mode: UploadMode, +) -> Result<(), ToolFailure> { + let refused = |error: FsError| -> ToolFailure { + match error { + FsError::AlreadyExists { .. } if mode == UploadMode::Create => format!( + "{path}: already exists; set overwrite to replace it, or change it with edit_file" + ) + .into(), + error => failed(error), + } + }; + let mut upload = fs.upload(path, mode).await.map_err(refused)?; + for chunk in bytes.chunks(CHUNK) { + if let Err(error) = upload.write(chunk.to_vec()).await { + let _ = upload.cancel().await; + return Err(refused(error)); + } + } + upload.finish().await.map_err(refused)?; + Ok(()) +} + +async fn write(fs: &dyn RemoteFs, request: &WriteFile, redact: bool) -> Result { + if redact && request.content.contains(REDACTED) { + return Err( + "The content holds a [REDACTED] marker, which would replace hidden text: change the file with edit_file instead." + .into(), + ); + } + let mode = if request.overwrite { + UploadMode::Replace + } else { + UploadMode::Create + }; + store(fs, &request.path, request.content.as_bytes(), mode).await?; + Ok(json!({"path": request.path, "bytes": request.content.len()})) +} + +/// Replaces `old_text` in the file's real text, which redaction never touched. +async fn edit(fs: &dyn RemoteFs, request: &EditFile) -> Result { + let path = &request.path; + let text = load(fs, path).await?; + let found = text.matches(request.old_text.as_str()).count(); + if found == 0 { + return Err(format!( + "{path}: old_text was not found. Read the file again and copy the text exactly; text shown as [REDACTED] cannot be matched." + ) + .into()); + } + if found > 1 && !request.replace_all { + return Err(format!( + "{path}: old_text occurs {found} times; include more surrounding text, or set replace_all." + ) + .into()); + } + let (changed, replaced) = if request.replace_all { + (text.replace(&request.old_text, &request.new_text), found) + } else { + (text.replacen(&request.old_text, &request.new_text, 1), 1) + }; + if changed.len() > MAX_FILE_LOAD_BYTES { + return Err(format!( + "{path}: the edited file would be larger than {} KiB", + MAX_FILE_LOAD_BYTES / 1024 + ) + .into()); + } + store(fs, path, changed.as_bytes(), UploadMode::Replace).await?; + Ok(json!({"path": path, "replacements": replaced, "bytes": changed.len()})) +} + +/// Removes a folder as a folder and anything else as a file. +async fn delete(fs: &dyn RemoteFs, request: &DeletePath) -> Result { + let path = &request.path; + let directory = match fs.stat(path).await { + Ok(Some(entry)) => entry.kind == EntryKind::Directory && !entry.is_symlink, + Ok(None) => return Err(failed(FsError::NotFound { path: path.clone() })), + Err(FsError::Unsupported(_)) => false, + Err(error) => return Err(failed(error)), + }; + if directory { + fs.remove_dir(path).await.map_err(failed)?; + } else { + fs.remove_file(path).await.map_err(failed)?; + } + Ok(json!({"path": path, "deleted": if directory { "directory" } else { "file" }})) +} diff --git a/crates/ai/src/tools/fixtures/calls.json b/crates/ai/src/tools/fixtures/calls.json index de660c11..662d2d3e 100644 --- a/crates/ai/src/tools/fixtures/calls.json +++ b/crates/ai/src/tools/fixtures/calls.json @@ -94,5 +94,146 @@ "terminal_id": "t1", "command_id": "cmd1" } + }, + { + "name": "transfer_files", + "label": "Transfer files", + "arguments": { + "source_terminal_id": "t1", + "sources": [ + "/srv/a", + "/srv/dir b" + ], + "destination_terminal_id": "t2", + "destination": "/home/alice/in" + }, + "normalized": { + "source_terminal_id": "t1", + "sources": [ + "/srv/a", + "/srv/dir b" + ], + "destination_terminal_id": "t2", + "destination": "/home/alice/in", + "overwrite": false, + "yield_ms": null + } + }, + { + "name": "read_transfer", + "label": "Read transfer", + "arguments": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819" + }, + "normalized": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819", + "yield_ms": null + } + }, + { + "name": "cancel_transfer", + "label": "Cancel transfer", + "arguments": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819" + }, + "normalized": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819" + } + }, + { + "name": "list_files", + "label": "List files", + "arguments": { + "terminal_id": "t3", + "path": "/plugins" + }, + "normalized": { + "terminal_id": "t3", + "path": "/plugins" + } + }, + { + "name": "read_file", + "label": "Read file", + "arguments": { + "terminal_id": "t3", + "path": "/server.properties" + }, + "normalized": { + "terminal_id": "t3", + "path": "/server.properties", + "start_line": null, + "max_lines": null + } + }, + { + "name": "write_file", + "label": "Write file", + "arguments": { + "terminal_id": "t3", + "path": "/ops.json", + "content": "[]\n" + }, + "normalized": { + "terminal_id": "t3", + "path": "/ops.json", + "content": "[]\n", + "overwrite": false + } + }, + { + "name": "edit_file", + "label": "Edit file", + "arguments": { + "terminal_id": "t3", + "path": "/server.properties", + "old_text": "max-players=20", + "new_text": "max-players=40" + }, + "normalized": { + "terminal_id": "t3", + "path": "/server.properties", + "old_text": "max-players=20", + "new_text": "max-players=40", + "replace_all": false + } + }, + { + "name": "make_directory", + "label": "Create folder", + "arguments": { + "terminal_id": "t3", + "path": "/plugins/Essentials" + }, + "normalized": { + "terminal_id": "t3", + "path": "/plugins/Essentials" + } + }, + { + "name": "delete_path", + "label": "Delete", + "arguments": { + "terminal_id": "t3", + "path": "/logs/old.log" + }, + "normalized": { + "terminal_id": "t3", + "path": "/logs/old.log" + } + }, + { + "name": "move_path", + "label": "Move", + "arguments": { + "terminal_id": "t3", + "from": "/world_old", + "to": "/backups/world_old" + }, + "normalized": { + "terminal_id": "t3", + "from": "/world_old", + "to": "/backups/world_old" + } } ] diff --git a/crates/ai/src/tools/fixtures/tools-list.json b/crates/ai/src/tools/fixtures/tools-list.json index d180aa55..bb5550c2 100644 --- a/crates/ai/src/tools/fixtures/tools-list.json +++ b/crates/ai/src/tools/fixtures/tools-list.json @@ -18,11 +18,11 @@ "inputSchema": { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "OpenTerminal", - "description": "Connects to an attached offline server without opening a tab.", + "description": "Opens an allowlisted server or reuses its already connected terminal.", "type": "object", "properties": { "server_id": { - "description": "An `offline_servers` entry of `list_terminals`.", + "description": "A `server_id` from the terminals or offline servers in `list_terminals`.", "type": "string" } }, @@ -233,6 +233,101 @@ "command_id" ] } + }, + { + "name": "transfer_files", + "description": "Copy files and folders between the hosts of two attached terminals: this computer (a local terminal) to a server, a server to this computer, or a server to another server. nocterm relays the data over the connections it already holds, so servers need no credentials for each other and you must never use scp, rsync or ssh between them. Every path is absolute (no ~, no .. components). sources are files or folders on the source host; folders copy recursively, and each source is copied into destination under its own name. destination must be an existing folder; it is never created. Existing files are skipped and counted as skipped unless overwrite is true. Symbolic links and special files are skipped. At most 64 sources per call. Two local terminals are refused: use exec_command. Some environments only allow copies within one server. The copy runs in the background: after optional yield_ms (default 1000) it returns the current state and a transfer_id for read_transfer/cancel_transfer. Poll read_transfer until the state is completed, failed or cancelled. Whole files are published atomically; a cancelled or failed file leaves no partial file", + "inputSchema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "TransferFiles", + "description": "Copy files and folders between the hosts of two attached terminals.", + "type": "object", + "properties": { + "source_terminal_id": { + "description": "terminal_id whose host holds the sources; a local terminal means this computer.", + "type": "string" + }, + "sources": { + "description": "Absolute paths of files or folders on the source host; folders copy recursively.", + "type": "array", + "items": { + "type": "string" + } + }, + "destination_terminal_id": { + "description": "terminal_id whose host receives the copies.", + "type": "string" + }, + "destination": { + "description": "Absolute path of an existing folder on the destination host.", + "type": "string" + }, + "overwrite": { + "description": "Replace files that already exist at the destination; by default they are skipped.", + "type": "boolean", + "default": false + }, + "yield_ms": { + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 0 + } + }, + "additionalProperties": false, + "required": [ + "source_terminal_id", + "sources", + "destination_terminal_id", + "destination" + ] + } + }, + { + "name": "read_transfer", + "description": "Read a snapshot of a transfer started by this chat: state, file and byte counts and up to 16 failures. Does not start or cancel a transfer; optional yield_ms waits briefly while it is active", + "inputSchema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "ReadTransfer", + "type": "object", + "properties": { + "transfer_id": { + "type": "string" + }, + "yield_ms": { + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 0 + } + }, + "additionalProperties": false, + "required": [ + "transfer_id" + ] + } + }, + { + "name": "cancel_transfer", + "description": "Cancel a transfer started by this chat. Files already published stay in place; files being copied are discarded", + "inputSchema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "CancelTransfer", + "type": "object", + "properties": { + "transfer_id": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "transfer_id" + ] + } } ] } diff --git a/crates/ai/src/tools/output.rs b/crates/ai/src/tools/output.rs new file mode 100644 index 00000000..c89a3d3b --- /dev/null +++ b/crates/ai/src/tools/output.rs @@ -0,0 +1,97 @@ +//! Agent-facing observations are display data, never execution capabilities. +use nocterm_session::command_jobs::CommandSnapshot; +use serde::{Deserialize, Serialize}; + +/// A bounded terminal snapshot; overlapping line numbers replace earlier text. +#[derive(Serialize, Deserialize)] +pub struct TerminalReport { + pub text: String, + pub first_line: u64, + pub next_line: u64, + pub cursor_semantics: String, + pub truncated: bool, + pub alt_screen: bool, +} +impl TerminalReport { + pub const CURSOR_SEMANTICS: &str = "inclusive snapshot; replace overlapping lines"; + + pub fn new( + text: String, + first_line: u64, + next_line: u64, + truncated: bool, + alt_screen: bool, + ) -> Self { + Self { + text, + first_line, + next_line, + cursor_semantics: Self::CURSOR_SEMANTICS.into(), + truncated, + alt_screen, + } + } + + /// An observation timeout or idle output does not establish process completion. + pub fn observed(self, completion: String) -> ObservationReport { + ObservationReport { + completed: completion == "prompt_returned", + terminal: self, + completion, + exit_status: None, + } + } +} + +#[derive(Serialize, Deserialize)] +pub struct ObservationReport { + #[serde(flatten)] + pub terminal: TerminalReport, + pub completion: String, + pub completed: bool, + pub exit_status: Option, +} + +/// A snapshot of an admitted command. Its identifier carries no control authority. +#[derive(Serialize, Deserialize)] +pub struct CommandReport { + pub command_id: String, + pub state: String, + pub stdout: String, + pub stderr: String, + pub exit_status: Option, + pub total_bytes: u64, + pub truncated: bool, + pub error: Option, +} +impl CommandReport { + pub fn new(id: &str, snapshot: &CommandSnapshot, redact: bool) -> Self { + let text = |value: String| { + if redact { + crate::redact::redact(&value) + } else { + value + } + }; + Self { + command_id: id.into(), + state: snapshot.state().as_str().into(), + stdout: text(String::from_utf8_lossy(snapshot.stdout()).into_owned()), + stderr: text( + snapshot + .exit() + .map(|exit| exit.stderr.clone()) + .unwrap_or_default(), + ), + exit_status: snapshot.exit().and_then(|exit| exit.status), + total_bytes: snapshot.total_bytes(), + truncated: snapshot.total_bytes() > snapshot.stdout().len() as u64, + error: snapshot + .error() + .map(|error| crate::redact::redact(&error.to_string())), + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ai/src/tools/output/tests.rs b/crates/ai/src/tools/output/tests.rs new file mode 100644 index 00000000..d3e22509 --- /dev/null +++ b/crates/ai/src/tools/output/tests.rs @@ -0,0 +1,65 @@ +use super::*; +use nocterm_session::{ExecError, ExecExit, command_jobs::CommandState}; +use serde_json::json; + +#[test] +fn command_wire_preserves_lossy_output_exit_counts_and_explicit_redaction_policy() { + let snapshot = CommandSnapshot { + state: CommandState::Exited, + stdout: b"password=secret\xff".to_vec(), + total_bytes: 100, + exit: Some(ExecExit { + status: Some(7), + stderr: "token=hidden".into(), + }), + error: Some(ExecError::Failed("password=private".into())), + }; + let literal = json!(CommandReport::new("c1", &snapshot, false)); + assert_eq!( + literal, + json!({ + "command_id": "c1", "state": "exited", "stdout": "password=secret�", + "stderr": "token=hidden", "exit_status": 7, "total_bytes": 100, + "truncated": true, "error": "could not start the program: password=[REDACTED]", + }) + ); + let redacted = json!(CommandReport::new("c1", &snapshot, true)); + assert_eq!(redacted["stdout"], "password=[REDACTED]"); + assert_eq!(redacted["stderr"], "token=[REDACTED]"); + assert_eq!(redacted["total_bytes"], 100); + assert_eq!(redacted["truncated"], true); +} + +#[test] +fn terminal_wire_keeps_cursor_rules_and_observation_never_invents_an_exit() { + let snapshot = || TerminalReport::new("界".into(), 5, 7, true, false); + let expected = json!({ + "text": "界", "first_line": 5, "next_line": 7, + "cursor_semantics": "inclusive snapshot; replace overlapping lines", + "truncated": true, "alt_screen": false, + }); + assert_eq!(json!(snapshot()), expected); + for reason in ["prompt_returned", "output_idle", "timeout"] { + let mut observation = expected.clone(); + observation["completion"] = json!(reason); + observation["completed"] = json!(reason == "prompt_returned"); + observation["exit_status"] = json!(null); + assert_eq!(json!(snapshot().observed(reason.into())), observation); + observation["future_field"] = json!("kept by display fallback"); + let decoded: ObservationReport = serde_json::from_value(observation).unwrap(); + assert_eq!(decoded.terminal.next_line, 7); + assert_eq!(decoded.completed, reason == "prompt_returned"); + assert!(decoded.exit_status.is_none()); + } +} + +#[test] +fn unfinished_commands_report_unknown_exit_and_do_not_mark_exact_output_truncated() { + assert_eq!( + json!(CommandReport::new("c2", &CommandSnapshot::default(), false)), + json!({ + "command_id": "c2", "state": "starting", "stdout": "", "stderr": "", + "exit_status": null, "total_bytes": 0, "truncated": false, "error": null, + }) + ); +} diff --git a/crates/ai/src/tools/transfer.rs b/crates/ai/src/tools/transfer.rs new file mode 100644 index 00000000..f5605431 --- /dev/null +++ b/crates/ai/src/tools/transfer.rs @@ -0,0 +1,160 @@ +//! File transfer tools: arguments, bounds and the agent-facing transfer report. +//! +//! Absolute-path rules are endpoint specific and belong to the transfer request +//! (`nocterm_transfers::Request::between`); this module only bounds the shape. +use super::TerminalCallError; +use nocterm_transfers::{Progress, TransferDirection, TransferState}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +pub const MAX_TRANSFER_SOURCES: usize = 64; +pub const MAX_TRANSFER_PATH_BYTES: usize = 4096; +pub const MAX_TRANSFER_SOURCE_BYTES: usize = 64 * 1024; +pub const MAX_TRANSFER_FAILURES: usize = 16; +const MAX_ID_BYTES: usize = 128; +const MAX_YIELD_MS: u64 = 10_000; + +/// Copy files and folders between the hosts of two attached terminals. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct TransferFiles { + /// terminal_id whose host holds the sources; a local terminal means this computer. + pub source_terminal_id: String, + /// Absolute paths of files or folders on the source host; folders copy recursively. + pub sources: Vec, + /// terminal_id whose host receives the copies. + pub destination_terminal_id: String, + /// Absolute path of an existing folder on the destination host. + pub destination: String, + /// Replace files that already exist at the destination; by default they are skipped. + #[serde(default)] + pub overwrite: bool, + pub yield_ms: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ReadTransfer { + pub transfer_id: String, + pub yield_ms: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct CancelTransfer { + pub transfer_id: String, +} + +fn id_len_ok(id: &str) -> bool { + !id.is_empty() && id.len() <= MAX_ID_BYTES +} + +/// Control characters (NUL, newlines, escapes) could reshape the approval card text. +fn path_ok(path: &str) -> bool { + !path.is_empty() && path.len() <= MAX_TRANSFER_PATH_BYTES && !path.chars().any(char::is_control) +} + +impl TransferFiles { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + if !id_len_ok(&self.source_terminal_id) || !id_len_ok(&self.destination_terminal_id) { + return Err(TerminalCallError::InvalidTarget); + } + let total: usize = self.sources.iter().map(String::len).sum(); + if self.sources.is_empty() + || self.sources.len() > MAX_TRANSFER_SOURCES + || !self.sources.iter().all(|path| path_ok(path)) + || !path_ok(&self.destination) + || total > MAX_TRANSFER_SOURCE_BYTES + || self.yield_ms.is_some_and(|n| n > MAX_YIELD_MS) + { + return Err(TerminalCallError::TransferRejected); + } + Ok(()) + } +} + +impl ReadTransfer { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + if id_len_ok(&self.transfer_id) && self.yield_ms.is_none_or(|n| n <= MAX_YIELD_MS) { + Ok(()) + } else { + Err(TerminalCallError::ReadTransferRejected) + } + } +} + +impl CancelTransfer { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + if id_len_ok(&self.transfer_id) { + Ok(()) + } else { + Err(TerminalCallError::InvalidTransferId) + } + } +} + +fn state_name(state: TransferState) -> &'static str { + match state { + TransferState::Queued => "queued", + TransferState::Discovering => "discovering", + TransferState::Running => "running", + TransferState::Completed => "completed", + TransferState::Failed => "failed", + TransferState::Cancelled => "cancelled", + } +} + +// Seam: `TransferDirection::Relay` arrives with the transfers domain contract; the +// fallthrough already names it, so this function needs no change when it lands. +fn direction_name(direction: TransferDirection) -> &'static str { + if direction == TransferDirection::Upload { + "upload" + } else if direction == TransferDirection::Download { + "download" + } else { + "relay" + } +} + +/// Agent-facing JSON for one transfer; paths and error text are redacted on request. +pub fn report(progress: &Progress, redact: bool) -> Value { + let text = |text: &str| { + if redact { + crate::redact::redact(text) + } else { + text.to_owned() + } + }; + let failures: Vec = progress + .errors + .iter() + .take(MAX_TRANSFER_FAILURES) + .map(|issue| { + json!({ + "path": issue.path().map(|path| text(&path.to_string())), + "operation": issue.operation().to_string(), + "error": text(&issue.cause().to_string()), + }) + }) + .collect(); + json!({ + "transfer_id": progress.id.to_string(), + "state": state_name(progress.state), + "direction": direction_name(progress.direction), + "destination": text(&progress.destination), + "files": { + "discovered": progress.discovered_files, + "completed": progress.completed_files, + "skipped": progress.skipped_files, + "failed": progress.failed_files, + }, + "bytes": {"total": progress.total_bytes, "transferred": progress.sent_bytes}, + "discovery_complete": progress.discovery_complete, + "failures": failures, + "failures_truncated": progress.errors.len() > MAX_TRANSFER_FAILURES, + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/ai/src/tools/transfer/tests.rs b/crates/ai/src/tools/transfer/tests.rs new file mode 100644 index 00000000..16303fd5 --- /dev/null +++ b/crates/ai/src/tools/transfer/tests.rs @@ -0,0 +1,239 @@ +use super::*; +use crate::TerminalCall; +use nocterm_session::{HostAddress, Target}; +use nocterm_transfers::{ + TransferCause, TransferId, TransferIssue, TransferOperation, TransferPath, +}; + +fn transfer() -> TransferFiles { + TransferFiles { + source_terminal_id: "t1".into(), + sources: vec!["/srv/a".into(), "/srv/dir b".into()], + destination_terminal_id: "t2".into(), + destination: "/home/alice/in".into(), + overwrite: false, + yield_ms: Some(1000), + } +} + +fn rejection(change: impl FnOnce(&mut TransferFiles)) -> TerminalCallError { + let mut call = transfer(); + change(&mut call); + TerminalCall::TransferFiles(call).validate().unwrap_err() +} + +fn progress(errors: Vec) -> Progress { + Progress { + id: TransferId::from_bytes([0xab; 16]).unwrap(), + host: HostAddress::direct(Target { + host: "example.test".into(), + port: 22, + user: "alice".into(), + }), + destination: "/srv/in".into(), + direction: TransferDirection::Download, + state: TransferState::Running, + discovered_files: 12, + completed_files: 10, + prepared_directories: 2, + skipped_files: 1, + failed_files: 1, + total_bytes: 1_048_576, + sent_bytes: 524_288, + discovery_complete: true, + errors, + } +} + +fn issue(path: &str, cause: TransferCause) -> TransferIssue { + TransferIssue::new( + Some(TransferPath::Remote(path.into())), + TransferOperation::Write, + cause, + ) +} + +#[test] +fn a_well_formed_transfer_validates_and_writes_on_both_terminals() { + let call = TerminalCall::TransferFiles(transfer()); + assert!(call.validate().is_ok()); + assert!(call.writes()); + assert_eq!(call.capability(), crate::ToolCapability::Transfer); + assert_eq!(call.terminal_id(), Some("t2")); + assert_eq!(call.source_terminal_id(), Some("t1")); + assert_eq!(call.terminals().collect::>(), ["t2", "t1"]); +} + +#[test] +fn terminals_lists_a_shared_source_and_destination_once() { + let mut same = transfer(); + same.source_terminal_id = "t2".into(); + let call = TerminalCall::TransferFiles(same); + assert_eq!(call.terminals().collect::>(), ["t2"]); +} + +#[test] +fn read_and_cancel_have_no_terminal_and_differ_in_writing() { + let read = TerminalCall::ReadTransfer(ReadTransfer { + transfer_id: "x".into(), + yield_ms: None, + }); + let cancel = TerminalCall::CancelTransfer(CancelTransfer { + transfer_id: "x".into(), + }); + for call in [&read, &cancel] { + assert_eq!(call.terminals().count(), 0); + assert!(call.target().is_none()); + } + assert!(!read.writes()); + assert_eq!(read.capability(), crate::ToolCapability::Read); + assert!(cancel.writes()); + assert_eq!(cancel.capability(), crate::ToolCapability::Transfer); +} + +#[test] +fn transfer_terminal_ids_must_be_one_to_128_bytes() { + for id in ["", &"t".repeat(129)] { + assert_eq!( + rejection(|call| call.source_terminal_id = id.into()), + TerminalCallError::InvalidTarget + ); + assert_eq!( + rejection(|call| call.destination_terminal_id = id.into()), + TerminalCallError::InvalidTarget + ); + } +} + +#[test] +fn transfer_sources_and_destination_are_bounded() { + let rejected = TerminalCallError::TransferRejected; + assert_eq!(rejection(|call| call.sources.clear()), rejected); + assert_eq!( + rejection(|call| call.sources = vec!["/a".into(); MAX_TRANSFER_SOURCES + 1]), + rejected + ); + assert_eq!( + rejection(|call| call.sources = vec![String::new()]), + rejected + ); + assert_eq!( + rejection(|call| call.sources = vec!["/a\0".into()]), + rejected + ); + assert_eq!( + rejection(|call| call.sources = vec!["/".repeat(MAX_TRANSFER_PATH_BYTES + 1)]), + rejected + ); + let long = "/".repeat(MAX_TRANSFER_PATH_BYTES); + assert_eq!( + rejection(|call| call.sources = vec![long.clone(); 17]), + rejected + ); + assert_eq!(rejection(|call| call.destination.clear()), rejected); + assert_eq!(rejection(|call| call.destination = "/\0".into()), rejected); + assert_eq!(rejection(|call| call.yield_ms = Some(10_001)), rejected); + let mut edge = transfer(); + edge.sources = vec!["/a".into(); MAX_TRANSFER_SOURCES]; + edge.yield_ms = Some(10_000); + assert!(TerminalCall::TransferFiles(edge).validate().is_ok()); +} + +#[test] +fn control_characters_cannot_reshape_a_transfer_path() { + let rejected = TerminalCallError::TransferRejected; + for text in [ + "/a\nInto: /tmp", + "/a\rb", + "/a\u{1b}[2J", + "/a\tb", + "/a\u{7f}", + ] { + assert_eq!( + rejection(|call| call.sources = vec![text.into()]), + rejected, + "{text:?}" + ); + assert_eq!( + rejection(|call| call.destination = text.into()), + rejected, + "{text:?}" + ); + } +} + +#[test] +fn read_and_cancel_validation_name_their_own_rejection() { + let read = |transfer_id: &str, yield_ms| { + TerminalCall::ReadTransfer(ReadTransfer { + transfer_id: transfer_id.into(), + yield_ms, + }) + .validate() + }; + assert!(read("abc", Some(10_000)).is_ok()); + for result in [ + read("", None), + read("a", Some(10_001)), + read(&"a".repeat(129), None), + ] { + assert_eq!(result, Err(TerminalCallError::ReadTransferRejected)); + } + let cancel = |transfer_id: &str| { + TerminalCall::CancelTransfer(CancelTransfer { + transfer_id: transfer_id.into(), + }) + .validate() + }; + assert!(cancel("abc").is_ok()); + assert_eq!(cancel(""), Err(TerminalCallError::InvalidTransferId)); + assert_eq!( + cancel(&"a".repeat(129)), + Err(TerminalCallError::InvalidTransferId) + ); +} + +#[test] +fn report_describes_progress_for_the_agent() { + let value = report( + &progress(vec![issue("/srv/a/x", TransferCause::Cancelled)]), + false, + ); + assert_eq!(value["transfer_id"], "abababab-abab-abab-abab-abababababab"); + assert_eq!(value["state"], "running"); + assert_eq!(value["direction"], "download"); + assert_eq!(value["destination"], "/srv/in"); + assert_eq!(value["files"]["completed"], 10); + assert_eq!(value["files"]["skipped"], 1); + assert_eq!(value["bytes"]["transferred"], 524_288); + assert_eq!(value["discovery_complete"], true); + assert_eq!(value["failures"][0]["path"], "/srv/a/x"); + assert_eq!(value["failures"][0]["operation"], "write"); + assert_eq!(value["failures"][0]["error"], "Transfer cancelled."); + assert_eq!(value["failures_truncated"], false); +} + +#[test] +fn report_keeps_the_first_sixteen_failures_and_says_so() { + let issues = (0..MAX_TRANSFER_FAILURES + 4) + .map(|n| issue(&format!("/srv/{n}"), TransferCause::Cancelled)) + .collect(); + let value = report(&progress(issues), false); + let failures = value["failures"].as_array().unwrap(); + assert_eq!(failures.len(), MAX_TRANSFER_FAILURES); + assert_eq!(failures[0]["path"], "/srv/0"); + assert_eq!(value["failures_truncated"], true); +} + +#[test] +fn report_redacts_paths_and_error_text_only_on_request() { + let secret = "/srv/ghp_abcdefghijklmnopqrst/key"; + let mut input = progress(vec![issue(secret, TransferCause::Cancelled)]); + input.destination = secret.into(); + let plain = report(&input, false); + assert_eq!(plain["destination"], secret); + assert_eq!(plain["failures"][0]["path"], secret); + let redacted = report(&input, true).to_string(); + assert!(!redacted.contains("ghp_abcdefghijklmnopqrst")); + assert!(redacted.contains("[REDACTED TOKEN]")); +} diff --git a/crates/ai/src/usage.rs b/crates/ai/src/usage.rs index d2bf6b25..20615191 100644 --- a/crates/ai/src/usage.rs +++ b/crates/ai/src/usage.rs @@ -3,13 +3,10 @@ //! //! ACP reports the context window (`usage_update`) and, at the end of a turn, //! the session's token totals. Plan limits are not part of ACP: an agent's -//! `UsageProfile` says whether it attaches them to `usage_update` metadata and -//! which injected account source reads the full report. +//! `UsageProfile` says whether it attaches them to `usage_update` metadata. +//! Nocterm uses only the reports received from the agent. use crate::model::content::Content; -mod source; -pub use source::{AccountUsageSource, UsageAgent, UsageReadError, UsageSources}; - use crate::{model::usage::ContextUsage, thread::Entry}; /// The span a plan limit counts usage over. @@ -86,14 +83,6 @@ impl Limits { } } } - - /// Replaces every limit: the source reports all of them at once. - pub fn replace(&mut self, limits: Vec) { - self.0.clear(); - for limit in limits { - self.merge(limit); - } - } } /// The plan limit an agent attached to `update` as a rate-limit record under @@ -217,50 +206,6 @@ mod tests { assert!(record(&update(serde_json::json!({"other": 1}))).is_none()); } - #[test] - fn usage_sources_read_each_agent_from_the_source_for_its_account() { - use crate::agents::AccountUsage; - use futures::future::BoxFuture; - use std::sync::Arc; - struct Fixed(Result>, UsageReadError>); - impl AccountUsageSource for Fixed { - fn read( - &self, - _: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - let result = self.0.clone(); - Box::pin(async move { result }) - } - } - let report = vec![RateLimit { - window: Window::FiveHour, - used: Some(0.1), - resets_at: None, - reached: false, - }]; - let failure = - || UsageReadError::fetch("https://example.test", std::io::Error::other("down")); - let sources = UsageSources::default() - .with(AccountUsage::OAuthAccount, Arc::new(Fixed(Err(failure())))) - .with( - AccountUsage::OAuthAccount, - Arc::new(Fixed(Ok(Some(report.clone())))), - ) - .with(AccountUsage::SessionLog, Arc::new(Fixed(Err(failure())))); - let read = |account| { - futures::executor::block_on(sources.read(UsageAgent { - id: "agent".into(), - account, - })) - }; - assert_eq!(read(AccountUsage::OAuthAccount).unwrap(), Some(report)); - assert!(matches!( - read(AccountUsage::SessionLog), - Err(UsageReadError::Fetch { .. }) - )); - assert_eq!(read(AccountUsage::None).unwrap(), None); - } - #[test] fn limits_merge_by_window_in_order() { let limit = |window, used| RateLimit { diff --git a/crates/ai/src/usage/source.rs b/crates/ai/src/usage/source.rs deleted file mode 100644 index 0e57032a..00000000 --- a/crates/ai/src/usage/source.rs +++ /dev/null @@ -1,121 +0,0 @@ -//! Account usage capabilities, without native paths or provider log formats. -use super::RateLimit; -use crate::agents::AccountUsage; -use futures::future::BoxFuture; -use nocterm_core::{SharedCause, SharedError}; -use std::path::PathBuf; -use std::sync::Arc; - -/// The agent whose limits to read and how its account reports them. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct UsageAgent { - pub id: String, - pub account: AccountUsage, -} - -impl UsageAgent { - /// An agent without a profile reports limits with usage updates only. - pub fn new(agent: &crate::agents::AvailableAgent) -> Self { - Self { - id: agent.id().to_owned(), - account: agent - .profile() - .map_or(AccountUsage::None, |profile| profile.usage().account()), - } - } -} - -/// Physical account-source failures retain their native cause. -#[derive(Clone, Debug, thiserror::Error)] -pub enum UsageReadError { - #[error("Could not enumerate account usage at {path}: {source}")] - Enumerate { - path: PathBuf, - #[source] - source: SharedError, - }, - #[error("Could not read account usage at {path}: {source}")] - Read { - path: PathBuf, - #[source] - source: SharedError, - }, - #[error("Could not seek account usage at {path}: {source}")] - Seek { - path: PathBuf, - #[source] - source: SharedError, - }, - #[error("Could not fetch account usage from {url}: {source}")] - Fetch { - url: String, - #[source] - source: SharedCause, - }, -} - -impl UsageReadError { - pub fn enumerate(path: impl Into, source: std::io::Error) -> Self { - Self::Enumerate { - path: path.into(), - source: SharedError::from_error(source), - } - } - pub fn read(path: impl Into, source: std::io::Error) -> Self { - Self::Read { - path: path.into(), - source: SharedError::from_error(source), - } - } - pub fn seek(path: impl Into, source: std::io::Error) -> Self { - Self::Seek { - path: path.into(), - source: SharedError::from_error(source), - } - } - pub fn fetch( - url: impl Into, - source: impl std::error::Error + Send + Sync + 'static, - ) -> Self { - Self::Fetch { - url: url.into(), - source: SharedCause::new(source), - } - } -} - -/// Implementations read only when polled on the host's background executor. -/// Missing or unsupported sources and logs without a usable report return None. -pub trait AccountUsageSource: Send + Sync { - fn read( - &self, - agent: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>>; -} - -/// One account source per way of reporting limits; agents whose way has no -/// source report nothing. -#[derive(Default)] -pub struct UsageSources(Vec<(AccountUsage, Arc)>); - -impl UsageSources { - /// Reads `account` agents' limits from `source`, replacing an earlier one. - #[must_use] - pub fn with(mut self, account: AccountUsage, source: Arc) -> Self { - self.0.retain(|(kind, _)| *kind != account); - self.0.push((account, source)); - self - } -} - -impl AccountUsageSource for UsageSources { - fn read( - &self, - agent: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - match self.0.iter().find(|(kind, _)| *kind == agent.account) { - Some((_, source)) => source.read(agent), - None => Box::pin(async { Ok(None) }), - } - } -} diff --git a/crates/connections-io/src/flags.rs b/crates/connections-io/src/flags.rs index 1bae66ac..62b8a3ec 100644 --- a/crates/connections-io/src/flags.rs +++ b/crates/connections-io/src/flags.rs @@ -1,17 +1,19 @@ //! A downloaded flag is usable even when atomic cache publication fails. -use crate::http::{self, MAX_FLAG_BYTES}; +use crate::http::{HttpClient, MAX_FLAG_BYTES}; use nocterm_connections_model::observation::{CountryCode, FlagRepository, ObservationError}; -use std::{io::Read as _, path::PathBuf}; +use std::{io::Read as _, path::PathBuf, sync::Arc}; pub(crate) struct CachedFlags { directory: PathBuf, endpoint: Option, + client: Arc, } impl CachedFlags { - pub(crate) fn new(directory: PathBuf) -> Self { + pub(crate) fn new(directory: PathBuf, client: Arc) -> Self { Self { directory, endpoint: Some("https://flagcdn.com/".into()), + client, } } #[cfg(test)] @@ -19,6 +21,7 @@ impl CachedFlags { Self { directory, endpoint, + client: Arc::default(), } } } @@ -40,7 +43,9 @@ impl FlagRepository for CachedFlags { let Some(endpoint) = &self.endpoint else { return Ok(None); }; - let bytes = http::get(&format!("{endpoint}{}.svg", code.as_str()), MAX_FLAG_BYTES)?; + let bytes = self + .client + .get(&format!("{endpoint}{}.svg", code.as_str()), MAX_FLAG_BYTES)?; if !is_svg(&bytes) { return Err(ObservationError::Decode(Box::new(std::io::Error::new( std::io::ErrorKind::InvalidData, diff --git a/crates/connections-io/src/http.rs b/crates/connections-io/src/http.rs index 7870bc5f..95014647 100644 --- a/crates/connections-io/src/http.rs +++ b/crates/connections-io/src/http.rs @@ -2,7 +2,12 @@ use nocterm_connections_model::observation::{ CountryCode, CountryLocator, ObservationError, PublicAddress, }; -use std::{io::Read as _, time::Duration}; +use nocterm_core::SharedError; +use std::{ + io::Read as _, + sync::{Arc, OnceLock}, + time::Duration, +}; pub(crate) const MAX_GEOIP_BYTES: u64 = 4 * 1024; pub(crate) use nocterm_connections_model::observation::MAX_FLAG_BYTES; @@ -10,18 +15,29 @@ const TIMEOUT: Duration = Duration::from_secs(8); pub(crate) struct HttpCountry { endpoint: String, + client: Arc, } impl Default for HttpCountry { fn default() -> Self { Self { endpoint: "https://api.country.is/".into(), + client: Arc::default(), } } } impl HttpCountry { + pub(crate) fn with_client(client: Arc) -> Self { + Self { + client, + ..Self::default() + } + } #[cfg(test)] pub(crate) fn at(endpoint: String) -> Self { - Self { endpoint } + Self { + endpoint, + client: Arc::default(), + } } } impl CountryLocator for HttpCountry { @@ -30,7 +46,7 @@ impl CountryLocator for HttpCountry { struct Answer { country: Option, } - let body = get( + let body = self.client.get( &format!("{}{ip}", self.endpoint, ip = ip.as_ip()), MAX_GEOIP_BYTES, )?; @@ -46,31 +62,45 @@ impl CountryLocator for HttpCountry { } } -pub(crate) fn get(url: &str, limit: u64) -> Result, ObservationError> { - let client = reqwest::blocking::Client::builder() - .timeout(TIMEOUT) - .user_agent(concat!("nocterm/", env!("CARGO_PKG_VERSION"))) - .build() - .map_err(|source| ObservationError::Network(Box::new(source.without_url())))?; - let response = client - .get(url) - .send() - .map_err(|source| ObservationError::Network(Box::new(source.without_url())))?; - let response = response - .error_for_status() - .map_err(|source| ObservationError::Status { - status: source.status().map_or(0, |status| status.as_u16()), - source: Box::new(source.without_url()), - })?; - let mut body = Vec::new(); - response - .take(limit + 1) - .read_to_end(&mut body) - .map_err(|source| ObservationError::Body(Box::new(source)))?; - if body.len() as u64 > limit { - return Err(ObservationError::BodyLimit { limit }); +/// One installation shares connection pooling; construction occurs on first background use. +#[derive(Default)] +pub(crate) struct HttpClient { + client: OnceLock>>, +} + +impl HttpClient { + pub(crate) fn get(&self, url: &str, limit: u64) -> Result, ObservationError> { + let client = self + .client + .get_or_init(|| { + reqwest::blocking::Client::builder() + .timeout(TIMEOUT) + .user_agent(concat!("nocterm/", env!("CARGO_PKG_VERSION"))) + .build() + .map_err(|source| SharedError::from_error(source.without_url())) + }) + .as_ref() + .map_err(|source| ObservationError::Network(Box::new(source.clone())))?; + let response = client + .get(url) + .send() + .map_err(|source| ObservationError::Network(Box::new(source.without_url())))?; + let response = response + .error_for_status() + .map_err(|source| ObservationError::Status { + status: source.status().map_or(0, |status| status.as_u16()), + source: Box::new(source.without_url()), + })?; + let mut body = Vec::new(); + response + .take(limit + 1) + .read_to_end(&mut body) + .map_err(|source| ObservationError::Body(Box::new(source)))?; + if body.len() as u64 > limit { + return Err(ObservationError::BodyLimit { limit }); + } + Ok(body) } - Ok(body) } #[cfg(test)] diff --git a/crates/connections-io/src/http/tests.rs b/crates/connections-io/src/http/tests.rs index 022744b6..0d0905a6 100644 --- a/crates/connections-io/src/http/tests.rs +++ b/crates/connections-io/src/http/tests.rs @@ -27,7 +27,9 @@ fn country_requests_use_only_ip_and_decode_checked_optional_codes() { fn status_error_retains_native_cause_without_endpoint() { use std::error::Error as _; let (endpoint, worker) = serve(503, vec![]); - let result = get(&endpoint, MAX_GEOIP_BYTES).unwrap_err(); + let result = HttpClient::default() + .get(&endpoint, MAX_GEOIP_BYTES) + .unwrap_err(); worker.join().unwrap(); assert!(matches!( result, diff --git a/crates/connections-io/src/lib.rs b/crates/connections-io/src/lib.rs index df5b96ce..575db8ab 100644 --- a/crates/connections-io/src/lib.rs +++ b/crates/connections-io/src/lib.rs @@ -1,12 +1,15 @@ //! Native connection files, environment/entropy/clock and network observations. //! -//! Owns: three-document repositories, native defaults/UUID/clock, DNS, bounded remote OS reads and HTTP/flag cache. +//! Owns: three-document repositories, native defaults/UUID/clock, a finite native DNS worker budget, bounded remote OS reads and HTTP/flag cache. //! Does not own: profile rules, GPUI images, observation scheduling or persistence workers. //! Ports used: foundation persistence. Ports provided: ConnectionRepository, ProfileIds, //! ConnectionClock, PublicAddressResolver, CountryLocator, //! FlagRepository, RemoteOsDetector. Settings section: none. Globals: none. //! Invariants: country services receive public IPs only; bodies and cached flags -//! are bounded; failure to cache never discards an acquired flag. Blocking document +//! are bounded; failure to cache never discards an acquired flag. DNS admits four workers +//! and four pending queries with an eight-second caller deadline; uncancellable OS work +//! retains its physical worker until it ends. One lazy HTTP client is shared per installation. +//! Blocking document //! acquisition and writes run off foreground; profiles retain configuration publication semantics. mod os_probe; @@ -35,10 +38,11 @@ pub struct NativeObservations { impl NativeObservations { pub fn new(flags_dir: PathBuf) -> Self { + let client = Arc::new(http::HttpClient::default()); Self { - resolver: Arc::new(NativeResolver), - locator: Arc::new(http::HttpCountry::default()), - flags: Arc::new(flags::CachedFlags::new(flags_dir)), + resolver: Arc::new(NativeResolver::default()), + locator: Arc::new(http::HttpCountry::with_client(client.clone())), + flags: Arc::new(flags::CachedFlags::new(flags_dir, client)), } } diff --git a/crates/connections-io/src/os_probe.rs b/crates/connections-io/src/os_probe.rs index 86af6b6c..cacd6799 100644 --- a/crates/connections-io/src/os_probe.rs +++ b/crates/connections-io/src/os_probe.rs @@ -3,7 +3,10 @@ use nocterm_connections_model::{ os::{self, OsId}, os_observation::{OsFuture, OsObservationError, RemoteOsDetector}, }; -use nocterm_session::{EntryKind, FsError, RemoteFs, fs::RemoteDownload}; +use nocterm_session::{ + EntryKind, FsError, RemoteFs, + fs::{DownloadReadError, read_bounded}, +}; use std::sync::Arc; const MAX_OS_RELEASE_BYTES: usize = 16 * 1024; pub struct NativeOsDetector; @@ -93,14 +96,34 @@ async fn read_small(fs: &dyn RemoteFs, path: &str) -> Result, OsO }); } }; - let result = read_bytes(file.as_mut(), path).await.and_then(|bytes| { - String::from_utf8(bytes) - .map(Some) - .map_err(|source| OsObservationError::InvalidUtf8 { + let result = read_bounded(file.as_mut(), MAX_OS_RELEASE_BYTES, 4096) + .await + .map_err(|error| match error { + DownloadReadError::Read(source) => OsObservationError::Read { path: path.into(), source, - }) - }); + }, + DownloadReadError::TooLarge { limit } => OsObservationError::Oversize { + path: path.into(), + limit, + }, + DownloadReadError::InvalidChunk { + requested, + returned, + } => OsObservationError::InvalidChunk { + path: path.into(), + requested, + returned, + }, + }) + .and_then(|bytes| { + String::from_utf8(bytes) + .map(Some) + .map_err(|source| OsObservationError::InvalidUtf8 { + path: path.into(), + source, + }) + }); if let Err(source) = file.close().await { let error = OsObservationError::Close { path: path.into(), @@ -110,38 +133,5 @@ async fn read_small(fs: &dyn RemoteFs, path: &str) -> Result, OsO } result } -async fn read_bytes( - file: &mut dyn RemoteDownload, - path: &str, -) -> Result, OsObservationError> { - let mut bytes = Vec::new(); - loop { - let requested = (MAX_OS_RELEASE_BYTES + 1 - bytes.len()).min(4096); - let chunk = file - .read(requested) - .await - .map_err(|source| OsObservationError::Read { - path: path.into(), - source, - })?; - if chunk.len() > requested { - return Err(OsObservationError::InvalidChunk { - path: path.into(), - requested, - returned: chunk.len(), - }); - } - if chunk.is_empty() { - return Ok(bytes); - } - if bytes.len() + chunk.len() > MAX_OS_RELEASE_BYTES { - return Err(OsObservationError::Oversize { - path: path.into(), - limit: MAX_OS_RELEASE_BYTES, - }); - } - bytes.extend(chunk); - } -} #[cfg(test)] mod tests; diff --git a/crates/connections-io/src/repository.rs b/crates/connections-io/src/repository.rs index 3997a368..95103f89 100644 --- a/crates/connections-io/src/repository.rs +++ b/crates/connections-io/src/repository.rs @@ -11,14 +11,14 @@ use nocterm_connections_model::{ use nocterm_core::persist; use std::{path::PathBuf, sync::Arc}; pub struct FileConnectionRepository { - profiles: PathBuf, + profiles: persist::RevisionedFile, recents: PathBuf, facts: PathBuf, } impl FileConnectionRepository { pub fn new(profiles: PathBuf, recents: PathBuf, facts: PathBuf) -> Self { Self { - profiles, + profiles: persist::RevisionedFile::new(profiles), recents, facts, } @@ -26,7 +26,7 @@ impl FileConnectionRepository { } impl ConnectionRepository for FileConnectionRepository { fn load(&self) -> LoadedConnections { - let (profiles, read_only) = match persist::load::(&self.profiles) { + let (profiles, read_only) = match self.profiles.load::() { Ok(value) => (value.unwrap_or_default(), None), Err(error) => { tracing::error!(%error, "could not read saved connections"); @@ -60,7 +60,7 @@ impl ConnectionRepository for FileConnectionRepository { } } fn save_profiles(&self, profiles: &Profiles) -> Result<(), ConnectionStorageError> { - persist::save_preserving(&self.profiles, profiles).map_err(|error| { + self.profiles.save_preserving(profiles).map_err(|error| { ConnectionStorageError::new( ConnectionDocument::Profiles, StorageOperation::Write, diff --git a/crates/connections-io/src/repository/tests.rs b/crates/connections-io/src/repository/tests.rs index 06af1eb3..40e0088d 100644 --- a/crates/connections-io/src/repository/tests.rs +++ b/crates/connections-io/src/repository/tests.rs @@ -202,3 +202,29 @@ fn a_document_without_jump_hosts_keeps_its_bytes_after_a_save() { first ); } + +#[test] +fn a_parallel_profile_edit_is_preserved_and_the_stale_writer_reports_its_cause() { + let directory = tempfile::tempdir().unwrap(); + let first = repository(directory.path()); + let second = repository(directory.path()); + let mut a = first.load().profiles; + let mut b = second.load().profiles; + let mut changed = profile(); + changed.name = "Saved by first".into(); + a.upsert(changed.clone()); + first.save_profiles(&a).unwrap(); + b.upsert(profile()); + let error = second.save_profiles(&b).unwrap_err(); + assert!(matches!( + error + .source() + .unwrap() + .downcast_ref::(), + Some(persist::PersistError::Conflict { .. }) + )); + assert_eq!( + first.load().profiles.get(changed.id).unwrap().name, + changed.name + ); +} diff --git a/crates/connections-io/src/resolver.rs b/crates/connections-io/src/resolver.rs index 1acb13cd..77dd1047 100644 --- a/crates/connections-io/src/resolver.rs +++ b/crates/connections-io/src/resolver.rs @@ -2,17 +2,109 @@ use nocterm_connections_model::observation::{ ObservationError, PublicAddress, PublicAddressResolver, }; -use std::net::{IpAddr, ToSocketAddrs as _}; +use nocterm_core::SharedError; +use std::{ + io, + net::{IpAddr, ToSocketAddrs as _}, + sync::{ + Arc, Mutex, OnceLock, + mpsc::{self, SyncSender}, + }, + thread, + time::{Duration, Instant}, +}; + +const DNS_WORKERS: usize = 4; +const DNS_PENDING: usize = 4; +const DNS_DEADLINE: Duration = Duration::from_secs(8); + +struct Request { + host: String, + deadline: Instant, + answer: SyncSender>>, +} + +/// Optional country resolution has its own finite physical budget. An OS lookup +/// that ignores cancellation retains one of these workers, not a shared fact slot. +/// Dropping the last resolver closes idle workers; queued expired work is skipped. +pub struct NativeResolver { + requests: OnceLock, SharedError>>, + deadline: Duration, +} -pub struct NativeResolver; +impl Default for NativeResolver { + fn default() -> Self { + Self { + requests: OnceLock::new(), + deadline: DNS_DEADLINE, + } + } +} + +type Lookup = Arc io::Result> + Send + Sync>; +fn workers(lookup: Lookup) -> io::Result> { + let (sender, receiver) = mpsc::sync_channel::(DNS_PENDING); + let receiver = Arc::new(Mutex::new(receiver)); + for index in 0..DNS_WORKERS { + let receiver = receiver.clone(); + let lookup = lookup.clone(); + thread::Builder::new() + .name(format!("nocterm-dns-{index}")) + .spawn(move || { + loop { + let next = receiver + .lock() + .unwrap_or_else(|error| error.into_inner()) + .recv(); + let Ok(request) = next else { break }; + if Instant::now() >= request.deadline { + continue; + } + let resolved = lookup(&request.host); + let _ = request.answer.send(resolved); + } + })?; + } + Ok(sender) +} + +impl NativeResolver { + fn lookup(&self, host: &str) -> io::Result> { + let sender = self + .requests + .get_or_init(|| { + workers(Arc::new(|host| { + (host, 0) + .to_socket_addrs() + .map(|addresses| addresses.map(|address| address.ip()).collect()) + })) + .map_err(SharedError::from_error) + }) + .as_ref() + .map_err(|error| io::Error::other(error.clone()))?; + let (answer, result) = mpsc::sync_channel(1); + sender + .try_send(Request { + host: host.to_owned(), + deadline: Instant::now() + self.deadline, + answer, + }) + .map_err(|error| match error { + mpsc::TrySendError::Full(_) => io::ErrorKind::WouldBlock, + mpsc::TrySendError::Disconnected(_) => io::ErrorKind::BrokenPipe, + })?; + result + .recv_timeout(self.deadline) + .map_err(|error| match error { + mpsc::RecvTimeoutError::Timeout => io::ErrorKind::TimedOut, + mpsc::RecvTimeoutError::Disconnected => io::ErrorKind::BrokenPipe, + })? + } +} impl PublicAddressResolver for NativeResolver { fn resolve(&self, host: &str) -> Result, ObservationError> { - resolve_with(host, |host| { - (host, 0) - .to_socket_addrs() - .map(|addresses| addresses.map(|address| address.ip())) - }) + resolve_with(host, |host| self.lookup(host)) } } @@ -33,39 +125,4 @@ fn resolve_with>( } #[cfg(test)] -mod tests { - use super::*; - #[test] - fn literals_and_local_names_never_use_dns() { - for host in [ - "192.168.1.10", - "[::1]", - "pi.local", - "localhost", - "", - "8.8.8.8", - ] { - let result = - resolve_with::>(host, |_| panic!("unexpected DNS for {host}")).unwrap(); - assert_eq!(result.is_some(), host == "8.8.8.8"); - } - } - #[test] - fn resolution_selects_first_public_address_only() { - let found = resolve_with("example.com", |_| { - Ok(vec![ - "10.0.0.1".parse().unwrap(), - "8.8.8.8".parse().unwrap(), - ]) - }) - .unwrap() - .unwrap(); - assert_eq!(found.as_ip(), "8.8.8.8".parse::().unwrap()); - assert!(matches!( - resolve_with::>("example.com", |_| Err(std::io::Error::other( - "DNS failed" - ))), - Err(ObservationError::Resolution(_)) - )); - } -} +mod tests; diff --git a/crates/connections-io/src/resolver/tests.rs b/crates/connections-io/src/resolver/tests.rs new file mode 100644 index 00000000..dc6de31b --- /dev/null +++ b/crates/connections-io/src/resolver/tests.rs @@ -0,0 +1,82 @@ +use super::*; +#[test] +fn literals_and_local_names_never_use_dns() { + for host in [ + "192.168.1.10", + "[::1]", + "pi.local", + "localhost", + "", + "8.8.8.8", + ] { + let result = + resolve_with::>(host, |_| panic!("unexpected DNS for {host}")).unwrap(); + assert_eq!(result.is_some(), host == "8.8.8.8"); + } +} +#[test] +fn resolution_selects_first_public_address_only() { + let found = resolve_with("example.com", |_| { + Ok(vec![ + "10.0.0.1".parse().unwrap(), + "8.8.8.8".parse().unwrap(), + ]) + }) + .unwrap() + .unwrap(); + assert_eq!(found.as_ip(), "8.8.8.8".parse::().unwrap()); + assert!(matches!( + resolve_with::>("example.com", |_| Err(std::io::Error::other("DNS failed"))), + Err(ObservationError::Resolution(_)) + )); +} + +#[test] +fn stalled_dns_keeps_only_four_workers_and_expired_queued_queries_are_skipped() { + use std::sync::{ + Condvar, + atomic::{AtomicUsize, Ordering}, + }; + let gate = Arc::new((Mutex::new(false), Condvar::new())); + let started = Arc::new(AtomicUsize::new(0)); + let (began, began_rx) = mpsc::channel(); + let lookup: Lookup = { + let gate = gate.clone(); + let started = started.clone(); + Arc::new(move |_| { + started.fetch_add(1, Ordering::SeqCst); + began.send(()).unwrap(); + let (lock, ready) = &*gate; + let _ = ready + .wait_timeout_while(lock.lock().unwrap(), Duration::from_secs(5), |open| !*open) + .unwrap(); + Ok(vec!["8.8.8.8".parse().unwrap()]) + }) + }; + let resolver = Arc::new(NativeResolver { + requests: OnceLock::from(Ok(workers(lookup).unwrap())), + deadline: Duration::from_millis(100), + }); + let calls: Vec<_> = (0..DNS_WORKERS) + .map(|_| { + let resolver = resolver.clone(); + thread::spawn(move || resolver.resolve("stalled.example")) + }) + .collect(); + for _ in 0..DNS_WORKERS { + began_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + } + for call in calls { + assert!(call.join().unwrap().is_err()); + } + assert!(resolver.resolve("expires.example").is_err()); + assert_eq!(started.load(Ordering::SeqCst), DNS_WORKERS); + { + let (lock, ready) = &*gate; + *lock.lock().unwrap() = true; + ready.notify_all(); + } + let resolved = resolver.resolve("ready.example").unwrap().unwrap(); + assert_eq!(resolved.as_ip(), "8.8.8.8".parse::().unwrap()); + assert_eq!(started.load(Ordering::SeqCst), DNS_WORKERS + 1); +} diff --git a/crates/connections-model/Cargo.toml b/crates/connections-model/Cargo.toml index df024bd9..7df6e6a2 100644 --- a/crates/connections-model/Cargo.toml +++ b/crates/connections-model/Cargo.toml @@ -17,6 +17,7 @@ serde.workspace = true toml.workspace = true uuid = { version = "1", default-features = false, features = ["std", "serde"] } thiserror.workspace = true +futures.workspace = true [features] test-support = [] diff --git a/crates/connections-model/src/draft.rs b/crates/connections-model/src/draft.rs index 038e6ead..7b98c48e 100644 --- a/crates/connections-model/src/draft.rs +++ b/crates/connections-model/src/draft.rs @@ -1,8 +1,8 @@ //! Text drafts and checked values; defaults and identifiers are supplied by callers. use crate::jump_route::JumpHostEntry; use nocterm_session::{ - Auth, CredentialId, CredentialIdParseError, DEFAULT_PORT, LaunchValidationError, - SessionOptions, SessionOptionsError, ShellLaunch, Target, + Auth, CredentialId, DEFAULT_PORT, LaunchValidationError, SessionOptions, SessionOptionsError, + ShellLaunch, Target, }; use std::path::{Path, PathBuf}; @@ -12,6 +12,8 @@ pub enum AuthKind { Auto, Password, Key, + /// A key kept in the vault, chosen in the editor. + VaultKey, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ValidationField { @@ -19,7 +21,6 @@ pub enum ValidationField { Port, User, Key, - Credential, Arguments, Environment, Launch, @@ -42,8 +43,8 @@ pub enum ProfileValidationError { MissingUser, #[error("Choose the private key file to sign in with.")] MissingKey, - #[error(transparent)] - Credential(#[from] CredentialIdParseError), + #[error("Choose a key from the vault, or create or import one.")] + MissingVaultKey, #[error("Arguments must be a JSON array of strings.")] Arguments(#[source] serde_json::Error), #[error("Environment must be a JSON object of string values.")] @@ -65,8 +66,7 @@ impl ProfileValidationError { Self::MissingHost | Self::InvalidHost => ValidationField::Host, Self::InvalidPort(_) => ValidationField::Port, Self::MissingUser => ValidationField::User, - Self::MissingKey => ValidationField::Key, - Self::Credential(_) => ValidationField::Credential, + Self::MissingKey | Self::MissingVaultKey => ValidationField::Key, Self::Arguments(_) => ValidationField::Arguments, Self::Environment(_) => ValidationField::Environment, Self::Launch(_) => ValidationField::Launch, @@ -93,7 +93,16 @@ pub struct EndpointDraft { pub struct AuthenticationDraft { pub kind: AuthKind, pub key_path: String, - pub credential: String, + /// The vault record and canonical public half of the chosen vault key. + pub vault_key: Option, + /// The vault record of the password the connection signs in with, as the editor kept it. + pub credential: Option, +} +/// A key the vault holds, as the profile will name it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct VaultKeyChoice { + pub credential: CredentialId, + pub public_key: String, } #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct LaunchDraft { @@ -168,9 +177,6 @@ impl ProfileDraft { ) -> Result { let target = self.endpoint.validate(defaults.user.as_deref())?; let auth = self.authentication.validate(defaults.home.as_deref())?; - let credential = optional(&self.authentication.credential) - .map(|id| id.parse()) - .transpose()?; let launch = self.launch.validate()?; self.options.validate()?; let appearance = self.appearance.validate()?; @@ -183,7 +189,7 @@ impl ProfileDraft { group: optional(&self.metadata.group), target, auth, - credential, + credential: self.authentication.credential, options: self.options.clone(), launch, icon: appearance.icon, @@ -227,6 +233,16 @@ impl AuthenticationDraft { path: expand_home(&path, home), } } + AuthKind::VaultKey => { + let key = self + .vault_key + .clone() + .ok_or(ProfileValidationError::MissingVaultKey)?; + Auth::VaultKey { + credential: key.credential, + public_key: key.public_key, + } + } }) } } diff --git a/crates/connections-model/src/draft/tests.rs b/crates/connections-model/src/draft/tests.rs index 6019b1fe..53a3c996 100644 --- a/crates/connections-model/src/draft/tests.rs +++ b/crates/connections-model/src/draft/tests.rs @@ -46,7 +46,6 @@ fn endpoint_authentication_and_launch_validation_keep_their_order() { draft.endpoint.host.clear(); draft.endpoint.port = "invalid".into(); draft.authentication.kind = AuthKind::Key; - draft.authentication.credential = "invalid".into(); draft.launch.override_launch = true; draft.launch.args = "invalid".into(); assert!(matches!( @@ -64,13 +63,6 @@ fn endpoint_authentication_and_launch_validation_keep_their_order() { ValidationField::Key ); draft.authentication.key_path = "/supplied/key".into(); - assert!(matches!( - draft.validate(&defaults()), - Err(ProfileValidationError::Credential( - CredentialIdParseError::InvalidShape - )) - )); - draft.authentication.credential.clear(); assert_eq!( draft.validate(&defaults()).unwrap_err().field(), ValidationField::Arguments @@ -177,3 +169,27 @@ fn jump_hosts_flow_from_the_draft_into_the_saved_profile() { let profile = validated.into_profile(crate::test_support::profile_id_for("db")); assert_eq!(profile.jump_hosts, draft.jump_hosts); } +#[test] +fn a_vault_key_draft_becomes_an_auth_that_names_the_record_and_its_public_half() { + let mut draft = draft(); + draft.authentication.kind = AuthKind::VaultKey; + let error = draft.validate(&defaults()).unwrap_err(); + assert!(matches!(error, ProfileValidationError::MissingVaultKey)); + assert_eq!(error.field(), ValidationField::Key); + let credential = CredentialId::from_bytes([4; 16]); + draft.authentication.vault_key = Some(VaultKeyChoice { + credential, + public_key: "ssh-ed25519 AAAA".into(), + }); + // The typed file path and password reference are not part of this method. + draft.authentication.key_path = "~/.ssh/ignored".into(); + let profile = draft.validate(&defaults()).unwrap(); + assert_eq!( + profile.auth, + Auth::VaultKey { + credential, + public_key: "ssh-ed25519 AAAA".into() + } + ); + assert_eq!(profile.credential, None); +} diff --git a/crates/connections-model/src/lib.rs b/crates/connections-model/src/lib.rs index cb6890b2..afbca6ec 100644 --- a/crates/connections-model/src/lib.rs +++ b/crates/connections-model/src/lib.rs @@ -1,8 +1,8 @@ //! Pure saved-connection formats, drafts, transitions and native capability ports. //! -//! Owns: profile/recent/facts formats, jump-host references and route resolution, checked mutations, live-profile authority stamps, recent-use intents, drafts, checked OS identities/release policy, observation values and bounded immutable flag bytes. +//! Owns: profile/recent/facts formats, jump-host references and route resolution, checked mutations, live-profile authority stamps, recent-use intents, drafts, checked OS identities/release policy, observation values, bounded immutable flag bytes, and the replica reconciliation rules with the agreed-state document. //! Does not own: DNS, HTTP, files, clocks, GPUI or observation lifecycle. -//! Ports provided: ConnectionRepository, ProfileIds, ConnectionClock and country/flag/RemoteOsDetector observation ports. +//! Ports provided: ConnectionRepository, ProfileIds, ConnectionClock, ProfileReplica and country/flag/RemoteOsDetector observation ports. //! Ports used: session value types. Settings section: none. Globals: none. //! Invariants: only checked public addresses reach country lookup; country keys //! are two lower-case ASCII letters; supplied raw flag bytes are bounded at 256 KiB. IDs/timestamps/defaults are supplied values; @@ -19,6 +19,7 @@ pub mod os_observation; pub mod profile_authority; pub mod profiles; pub mod recents; +pub mod replica; pub mod repository; pub mod value_ports; diff --git a/crates/connections-model/src/profiles.rs b/crates/connections-model/src/profiles.rs index 381f0f33..9a6e1b85 100644 --- a/crates/connections-model/src/profiles.rs +++ b/crates/connections-model/src/profiles.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; /// Identifies a saved connection across renames and edits. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(transparent)] pub struct ProfileId(Uuid); @@ -195,6 +195,33 @@ impl Profiles { self.forget_group(group); } + /// Moves the `members` that are in folder `from` to folder `to` (`None`: the top + /// level); connections of the folder that are not listed keep it. + pub fn regroup(&mut self, members: &[ProfileId], from: &str, to: Option<&str>) { + if to == Some(from) { + return; + } + for profile in &mut self.list { + if profile.group.as_deref() == Some(from) && members.contains(&profile.id) { + profile.group = to.map(str::to_owned); + } + } + self.forget_group(from); + self.remember_group(to); + } + + /// Deletes the `members` that are in `group`, returning the removed ones. + pub fn remove_members(&mut self, group: &str, members: &[ProfileId]) -> Vec { + let (removed, kept) = std::mem::take(&mut self.list) + .into_iter() + .partition(|profile| { + profile.group.as_deref() == Some(group) && members.contains(&profile.id) + }); + self.list = kept; + self.forget_group(group); + removed + } + /// Deletes the folder and its connections, returning the removed ones. pub fn remove_group(&mut self, group: &str) -> Vec { let (removed, kept) = std::mem::take(&mut self.list) diff --git a/crates/connections-model/src/profiles/mutation.rs b/crates/connections-model/src/profiles/mutation.rs index cd23e31b..c78af154 100644 --- a/crates/connections-model/src/profiles/mutation.rs +++ b/crates/connections-model/src/profiles/mutation.rs @@ -64,6 +64,19 @@ pub enum ProfileMutation { group: String, expected: Vec, }, + /// Moves the listed `members` of folder `from` to folder `to` (`None`: the top level). + /// Connections of the folder that are not listed, such as one that joined it after it + /// was shown, stay where they are. + Regroup { + members: Vec, + from: String, + to: Option, + }, + /// Deletes the listed `members` of `group`, provided each is still in it. + DeleteMembers { + group: String, + members: Vec, + }, Credential { scope: CredentialScope, id: CredentialId, @@ -75,7 +88,12 @@ impl ProfileMutation { let metadata_bytes = match self { Self::Move { group, .. } => group.as_ref().map_or(0, String::len), Self::RenameGroup { from, to } => from.len().saturating_add(to.len()), - Self::Ungroup(group) | Self::DeleteGroup { group, .. } => group.len(), + Self::Regroup { from, to, .. } => from + .len() + .saturating_add(to.as_ref().map_or(0, String::len)), + Self::Ungroup(group) + | Self::DeleteGroup { group, .. } + | Self::DeleteMembers { group, .. } => group.len(), Self::Credential { scope, .. } => scope .target() .host @@ -146,6 +164,26 @@ impl ProfileMutation { } profiles.remove_group(group); } + Self::Regroup { members, from, to } => { + require_group(profiles, from)?; + let to = match to.as_deref().map(str::trim) { + Some("") => return Err(ProfileError::EmptyGroup), + other => other, + }; + profiles.regroup(members, from, to); + } + Self::DeleteMembers { group, members } => { + require_group(profiles, group)?; + let all_in = members.iter().all(|id| { + profiles + .get(*id) + .is_some_and(|profile| profile.group.as_deref() == Some(group)) + }); + if !all_in { + return Err(ProfileError::GroupChanged(group.clone())); + } + profiles.remove_members(group, members); + } Self::Credential { scope, id } => { profiles.associate_credential(scope, *id); } diff --git a/crates/connections-model/src/profiles/tests.rs b/crates/connections-model/src/profiles/tests.rs index 05e4023f..b7742f36 100644 --- a/crates/connections-model/src/profiles/tests.rs +++ b/crates/connections-model/src/profiles/tests.rs @@ -669,3 +669,61 @@ fn a_routed_session_without_a_saved_connection_has_nowhere_to_remember_a_credent Some(CredentialScope::direct(target, Auth::Auto)) ); } + +#[test] +fn regrouping_listed_members_leaves_the_rest_of_the_folder_in_place() { + let (a, b) = ( + profile("a", "a.example", Some("Work")), + profile("b", "b.example", Some("Work")), + ); + let mut profiles = Profiles::default(); + profiles.upsert(a.clone()); + profiles.upsert(b.clone()); + ProfileMutation::Regroup { + members: vec![a.id], + from: "Work".into(), + to: Some(" Team ".into()), + } + .apply(&mut profiles) + .unwrap(); + assert_eq!(profiles.get(a.id).unwrap().group.as_deref(), Some("Team")); + assert_eq!(profiles.get(b.id).unwrap().group.as_deref(), Some("Work")); + assert!(matches!( + ProfileMutation::Regroup { + members: vec![a.id], + from: "Team".into(), + to: Some(" ".into()), + } + .apply(&mut profiles), + Err(ProfileError::EmptyGroup) + )); +} + +#[test] +fn deleting_listed_members_keeps_the_rest_and_refuses_one_that_left() { + let (a, b) = ( + profile("a", "a.example", Some("Work")), + profile("b", "b.example", Some("Work")), + ); + let mut profiles = Profiles::default(); + profiles.upsert(a.clone()); + profiles.upsert(b.clone()); + let delete = |members: Vec| ProfileMutation::DeleteMembers { + group: "Work".into(), + members, + }; + ProfileMutation::Move { + id: b.id, + group: None, + before: None, + } + .apply(&mut profiles) + .unwrap(); + assert!(matches!( + delete(vec![a.id, b.id]).apply(&mut profiles), + Err(ProfileError::GroupChanged(_)) + )); + assert!(profiles.get(a.id).is_some()); + delete(vec![a.id]).apply(&mut profiles).unwrap(); + assert!(profiles.get(a.id).is_none() && profiles.get(b.id).is_some()); +} diff --git a/crates/connections-model/src/replica.rs b/crates/connections-model/src/replica.rs new file mode 100644 index 00000000..a33ff421 --- /dev/null +++ b/crates/connections-model/src/replica.rs @@ -0,0 +1,128 @@ +//! The port a remote copy of the saved profiles implements, and the reconciliation rules. +//! +//! The local document stays the source the application reads. A replica is a second +//! copy, kept by another party (a hub), that changes independently. The rules in +//! [`rules`] decide, per profile, what to do when the local copy, the replica and the +//! last agreed state differ; they never perform I/O. +mod state; + +pub mod rules; + +pub use state::{ReplicaState, SyncedEntry}; + +use crate::profiles::{Profile, ProfileId}; +use futures::{future::BoxFuture, stream::BoxStream}; +use nocterm_core::SharedCause; +use serde::{Deserialize, Serialize}; + +/// An opaque position in the replica's change feed. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct ReplicaCursor(String); + +impl ReplicaCursor { + pub fn new(token: impl Into) -> Self { + Self(token.into()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +/// The replica's revision of one profile, used as the expectation of the next write. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(transparent)] +pub struct ReplicaVersion(u64); + +impl ReplicaVersion { + pub fn new(version: u64) -> Self { + Self(version) + } + + pub fn get(self) -> u64 { + self.0 + } +} + +/// What the replica holds for a profile. +#[derive(Clone, Debug, PartialEq)] +pub enum RemoteBody { + Present(Box), + /// A tombstone: the profile was deleted and the deletion is itself a version. + Deleted, + /// An object this client cannot decode, usually written by a newer client. + Unrecognized, +} + +/// One entry of the change feed. +#[derive(Clone, Debug, PartialEq)] +pub struct RemoteProfile { + pub id: ProfileId, + pub version: ReplicaVersion, + pub body: RemoteBody, +} + +/// Changes after a cursor, in the order the replica made them. A profile's versions +/// grow along the feed, but the rules do not rely on it: a change older than the +/// version already agreed is ignored. +#[derive(Clone, Debug, PartialEq)] +pub struct ReplicaPage { + pub changes: Vec, + /// Where to continue reading after these changes. + pub cursor: ReplicaCursor, + /// More changes are available immediately after `cursor`. + pub more: bool, +} + +#[derive(Clone, Debug, thiserror::Error)] +pub enum ReplicaError { + /// The original account capture no longer belongs to the current login. + #[error("The original profile replica is no longer current.")] + Stale, + /// The replica cannot be reached now; local work continues. + #[error("The profile replica is not reachable.")] + Unavailable, + /// The keys that open the replica are closed, so nothing can be read or written until + /// they are opened again. The server may well be reachable. + #[error("The profile replica is locked.")] + Locked, + /// The expected version is no longer the replica's current one. + #[error("Profile {id} changed on the replica.")] + Conflict { id: ProfileId }, + /// The account may not change this profile. + #[error("Profile {id} cannot be changed on the replica.")] + Forbidden { id: ProfileId }, + #[error("The profile replica failed: {0}")] + Other(#[source] SharedCause), +} + +/// A remote copy of the saved profiles, one sealed object per profile. +pub trait ProfileReplica: Send + Sync { + /// Changes after `after`; `None` reads from the beginning. + fn changes( + &self, + after: Option, + ) -> BoxFuture<'static, Result>; + + /// Writes `profile`. `expected` is the version this client last saw, or `None` + /// to create; a replica holding another version answers `Conflict`. + fn put( + &self, + profile: Profile, + expected: Option, + ) -> BoxFuture<'static, Result>; + + /// Replaces the profile with a tombstone under the same expectation rules. + fn delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + ) -> BoxFuture<'static, Result>; + + /// Yields once whenever the replica may have new changes. + fn watch(&self) -> BoxStream<'static, ()>; +} + +#[cfg(test)] +mod tests; diff --git a/crates/connections-model/src/replica/rules.rs b/crates/connections-model/src/replica/rules.rs new file mode 100644 index 00000000..75f03de1 --- /dev/null +++ b/crates/connections-model/src/replica/rules.rs @@ -0,0 +1,277 @@ +//! Three-way reconciliation of one profile: local `L`, remote `R` and the last agreed `S`. +//! +//! The rules are pure. They name what must happen and leave doing it, and the order +//! of side effects, to the caller. A remote change always wins a conflict; the local +//! edit it replaces is reported so the user can be told. A change older than the +//! agreed version is a late entry of the feed, never a newer state, and is ignored. +use super::{RemoteBody, RemoteProfile, ReplicaState, ReplicaVersion, SyncedEntry}; +use crate::profiles::{Profile, ProfileId, Profiles}; +use std::collections::{HashMap, HashSet}; + +/// One effect the reconciliation asks for. +#[derive(Clone, Debug, PartialEq)] +pub enum ReplicaAction { + /// Write `profile` locally if the local copy still is `expected` (`None`: absent), + /// then record agreement at `version`. + SaveLocal { + version: ReplicaVersion, + profile: Box, + expected: Option>, + }, + /// Delete the local profile if it still is `expected`, then forget it. + DeleteLocal { + id: ProfileId, + expected: Box, + }, + /// Send `profile`; `expected` is the replica's version to replace (`None`: create). + Push { + profile: Box, + expected: Option, + }, + /// Send a deletion replacing the replica's `expected` version. + PushDelete { + id: ProfileId, + expected: ReplicaVersion, + }, + /// Both sides already hold `profile`; record agreement at `version`. + MarkSynced { + version: ReplicaVersion, + profile: Box, + }, + /// Neither side holds the profile any more; drop the agreement. + Forget { id: ProfileId }, + /// A local edit was replaced by the remote version. + Overwritten { + id: ProfileId, + discarded: Box, + }, + /// The remote version cannot be read; leave the local profile alone. + NeedsNewerClient { id: ProfileId }, +} + +impl ReplicaAction { + pub fn id(&self) -> ProfileId { + match self { + Self::SaveLocal { profile, .. } + | Self::Push { profile, .. } + | Self::MarkSynced { profile, .. } => profile.id, + Self::DeleteLocal { id, .. } + | Self::PushDelete { id, .. } + | Self::Forget { id } + | Self::Overwritten { id, .. } + | Self::NeedsNewerClient { id } => *id, + } + } +} + +/// Reconciles every profile known to any of the three sides. +/// +/// `changes` are the remote changes read since the state's cursor; a profile without +/// one, or with only changes older than its agreed version, is unchanged remotely. A +/// profile the replica holds unreadably stays untouched until a readable version of it +/// arrives. +pub fn plan( + local: &Profiles, + changes: &[RemoteProfile], + state: &ReplicaState, +) -> Vec { + let latest = latest_changes(changes, state); + let local_by_id: HashMap<_, _> = local.iter().map(|profile| (profile.id, profile)).collect(); + let mut remembered: Vec = state.entries().map(|(id, _)| id).collect(); + remembered.sort_unstable(); + let mut seen = HashSet::new(); + local + .iter() + .map(|profile| profile.id) + .chain(changes.iter().map(|change| change.id)) + .chain(remembered) + .filter(|id| seen.insert(*id)) + .filter(|id| !state.is_unrecognized(*id) || latest.contains_key(id)) + .flat_map(|id| { + reconcile( + local_by_id.get(&id).copied(), + latest.get(&id).copied(), + state.entry(id), + ) + }) + .collect() +} + +/// The change that stands for each profile: the later one of the feed. Late entries older +/// than the agreed version never stand. +fn latest_changes<'a>( + changes: &'a [RemoteProfile], + state: &ReplicaState, +) -> HashMap { + changes + .iter() + .filter(|change| !is_stale(change, state.entry(change.id))) + .map(|change| (change.id, change)) + .collect() +} + +/// The feed delivered a version older than the one already agreed for the profile. +fn is_stale(change: &RemoteProfile, synced: Option<&SyncedEntry>) -> bool { + synced.is_some_and(|synced| change.version < synced.version) +} + +/// What to do about one profile. At least one of the three must be present. +pub fn reconcile( + local: Option<&Profile>, + remote: Option<&RemoteProfile>, + synced: Option<&SyncedEntry>, +) -> Vec { + let remote = remote.filter(|change| !is_stale(change, synced)); + let Some(id) = local + .map(|profile| profile.id) + .or_else(|| remote.map(|change| change.id)) + .or_else(|| synced.map(|entry| entry.profile.id)) + else { + return Vec::new(); + }; + if remote.is_some_and(|change| change.body == RemoteBody::Unrecognized) { + return vec![ReplicaAction::NeedsNewerClient { id }]; + } + match synced { + Some(synced) => with_history(local, remote, synced), + None => without_history(local, remote), + } +} + +fn boxed(profile: &Profile) -> Box { + Box::new(profile.clone()) +} + +fn save_remote( + change: &RemoteProfile, + profile: &Profile, + expected: Option<&Profile>, +) -> ReplicaAction { + ReplicaAction::SaveLocal { + version: change.version, + profile: boxed(profile), + expected: expected.map(boxed), + } +} + +fn mark_synced(change: &RemoteProfile, profile: &Profile) -> ReplicaAction { + ReplicaAction::MarkSynced { + version: change.version, + profile: boxed(profile), + } +} + +/// Both disagree with a remote profile that has no agreed state: the remote wins. +fn take_remote( + change: &RemoteProfile, + remote: &Profile, + local: Option<&Profile>, +) -> Vec { + match local { + Some(local) if local == remote => vec![mark_synced(change, remote)], + Some(local) => vec![ + save_remote(change, remote, Some(local)), + ReplicaAction::Overwritten { + id: local.id, + discarded: boxed(local), + }, + ], + None => vec![save_remote(change, remote, None)], + } +} + +fn without_history(local: Option<&Profile>, remote: Option<&RemoteProfile>) -> Vec { + match (local, remote) { + (None, None) => Vec::new(), + (Some(local), None) => vec![ReplicaAction::Push { + profile: boxed(local), + expected: None, + }], + (_, Some(change)) => match &change.body { + RemoteBody::Present(remote) => take_remote(change, remote, local), + RemoteBody::Deleted => local + .map(|local| ReplicaAction::Push { + profile: boxed(local), + expected: Some(change.version), + }) + .into_iter() + .collect(), + RemoteBody::Unrecognized => Vec::new(), + }, + } +} + +#[derive(Clone, Copy)] +enum Side<'a> { + Equal, + Changed(&'a Profile), + Deleted, +} + +/// The remote side relative to `S`. A change carrying `S`'s version, or `S`'s content +/// at a newer version, is not a change of content. +fn remote_side<'a>(remote: Option<&'a RemoteProfile>, synced: &SyncedEntry) -> Side<'a> { + let Some(change) = remote else { + return Side::Equal; + }; + match &change.body { + _ if change.version == synced.version => Side::Equal, + RemoteBody::Present(profile) if **profile == synced.profile => Side::Equal, + RemoteBody::Present(profile) => Side::Changed(profile), + RemoteBody::Deleted => Side::Deleted, + RemoteBody::Unrecognized => Side::Equal, + } +} + +fn local_side<'a>(local: Option<&'a Profile>, synced: &SyncedEntry) -> Side<'a> { + match local { + None => Side::Deleted, + Some(local) if *local == synced.profile => Side::Equal, + Some(local) => Side::Changed(local), + } +} + +fn with_history( + local: Option<&Profile>, + remote: Option<&RemoteProfile>, + synced: &SyncedEntry, +) -> Vec { + let version = remote.map_or(synced.version, |change| change.version); + match ( + local_side(local, synced), + remote_side(remote, synced), + local, + remote, + ) { + (Side::Equal, Side::Equal, ..) if version == synced.version => Vec::new(), + (Side::Equal, Side::Equal, ..) => vec![ReplicaAction::MarkSynced { + version, + profile: boxed(&synced.profile), + }], + (Side::Equal, Side::Changed(profile), _, Some(change)) => { + vec![save_remote(change, profile, Some(&synced.profile))] + } + (Side::Equal, Side::Deleted, ..) => vec![ReplicaAction::DeleteLocal { + id: synced.profile.id, + expected: boxed(&synced.profile), + }], + (Side::Changed(local), Side::Equal | Side::Deleted, ..) => vec![ReplicaAction::Push { + profile: boxed(local), + expected: Some(version), + }], + (Side::Changed(local), Side::Changed(profile), _, Some(change)) => { + take_remote(change, profile, Some(local)) + } + (Side::Deleted, Side::Equal, ..) => vec![ReplicaAction::PushDelete { + id: synced.profile.id, + expected: version, + }], + (Side::Deleted, Side::Changed(profile), _, Some(change)) => { + vec![save_remote(change, profile, None)] + } + (Side::Deleted, Side::Deleted, ..) => vec![ReplicaAction::Forget { + id: synced.profile.id, + }], + _ => Vec::new(), + } +} diff --git a/crates/connections-model/src/replica/state.rs b/crates/connections-model/src/replica/state.rs new file mode 100644 index 00000000..5caa9bd8 --- /dev/null +++ b/crates/connections-model/src/replica/state.rs @@ -0,0 +1,68 @@ +//! The last state both sides agreed on, which tells a local change from a remote one. +use super::{ReplicaCursor, ReplicaVersion}; +use crate::profiles::{Profile, ProfileId}; +use serde::{Deserialize, Serialize}; +use std::collections::{HashMap, HashSet}; + +/// A profile as it was when the local copy and the replica last agreed. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct SyncedEntry { + pub version: ReplicaVersion, + pub profile: Profile, +} + +/// What is persisted between runs: the feed position, the agreed profiles and the +/// profiles the replica holds in a form this client cannot read. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct ReplicaState { + #[serde(default, skip_serializing_if = "Option::is_none")] + cursor: Option, + #[serde(default)] + entries: HashMap, + #[serde(default, skip_serializing_if = "HashSet::is_empty")] + unrecognized: HashSet, +} + +impl ReplicaState { + pub fn cursor(&self) -> Option<&ReplicaCursor> { + self.cursor.as_ref() + } + + pub fn entry(&self, id: ProfileId) -> Option<&SyncedEntry> { + self.entries.get(&id) + } + + pub fn entries(&self) -> impl Iterator { + self.entries.iter().map(|(id, entry)| (*id, entry)) + } + + /// The replica holds a version of this profile that this client cannot read. + pub fn is_unrecognized(&self, id: ProfileId) -> bool { + self.unrecognized.contains(&id) + } + + pub fn has_unrecognized(&self) -> bool { + !self.unrecognized.is_empty() + } + + pub fn advance(&mut self, cursor: ReplicaCursor) { + self.cursor = Some(cursor); + } + + /// Records agreement on `entry`, whether this client sent it or took the replica's + /// version; the replica's version is readable again. + pub fn agree(&mut self, id: ProfileId, entry: SyncedEntry) { + self.unrecognized.remove(&id); + self.entries.insert(id, entry); + } + + /// Neither side holds the profile any more: the agreement goes. + pub fn forget(&mut self, id: ProfileId) { + self.unrecognized.remove(&id); + self.entries.remove(&id); + } + + pub fn mark_unrecognized(&mut self, id: ProfileId) { + self.unrecognized.insert(id); + } +} diff --git a/crates/connections-model/src/replica/tests.rs b/crates/connections-model/src/replica/tests.rs new file mode 100644 index 00000000..efe909a3 --- /dev/null +++ b/crates/connections-model/src/replica/tests.rs @@ -0,0 +1,370 @@ +use super::rules::{ReplicaAction, plan, reconcile}; +use super::*; +use crate::{profiles::Profiles, test_support::profile_id_for}; +use nocterm_session::{Auth, Target}; + +fn profile(name: &str) -> Profile { + Profile { + id: profile_id_for("shared"), + name: name.into(), + description: String::new(), + options: Default::default(), + target: Target::new("ci", "host", 22), + auth: Auth::Password, + credential: None, + launch: None, + icon: None, + icon_color: None, + country: None, + group: None, + jump_hosts: Vec::new(), + } +} + +fn id() -> ProfileId { + profile_id_for("shared") +} + +fn version(version: u64) -> ReplicaVersion { + ReplicaVersion::new(version) +} + +fn synced(name: &str, at: u64) -> SyncedEntry { + SyncedEntry { + version: version(at), + profile: profile(name), + } +} + +fn remote(body: RemoteBody, at: u64) -> RemoteProfile { + RemoteProfile { + id: id(), + version: version(at), + body, + } +} + +fn present(name: &str, at: u64) -> RemoteProfile { + remote(RemoteBody::Present(Box::new(profile(name))), at) +} + +fn save(name: &str, at: u64, expected: Option<&str>) -> ReplicaAction { + ReplicaAction::SaveLocal { + version: version(at), + profile: Box::new(profile(name)), + expected: expected.map(|name| Box::new(profile(name))), + } +} + +fn push(name: &str, expected: Option) -> ReplicaAction { + ReplicaAction::Push { + profile: Box::new(profile(name)), + expected: expected.map(version), + } +} + +fn marked(name: &str, at: u64) -> ReplicaAction { + ReplicaAction::MarkSynced { + version: version(at), + profile: Box::new(profile(name)), + } +} + +#[test] +fn nothing_happens_when_local_and_remote_equal_the_synced_profile() { + let local = profile("a"); + let actions = reconcile(Some(&local), Some(&present("a", 3)), Some(&synced("a", 3))); + assert_eq!(actions, []); + assert_eq!(reconcile(Some(&local), None, Some(&synced("a", 3))), []); +} + +#[test] +fn a_newer_version_with_the_synced_content_only_updates_the_agreed_version() { + let local = profile("a"); + let actions = reconcile(Some(&local), Some(&present("a", 4)), Some(&synced("a", 3))); + assert_eq!(actions, [marked("a", 4)]); +} + +#[test] +fn a_remote_change_is_applied_over_an_unchanged_local_profile() { + let local = profile("a"); + let actions = reconcile(Some(&local), Some(&present("b", 4)), Some(&synced("a", 3))); + assert_eq!(actions, [save("b", 4, Some("a"))]); +} + +#[test] +fn a_local_change_is_sent_against_the_synced_version() { + let local = profile("b"); + assert_eq!( + reconcile(Some(&local), None, Some(&synced("a", 3))), + [push("b", Some(3))] + ); + let echo = present("a", 3); + assert_eq!( + reconcile(Some(&local), Some(&echo), Some(&synced("a", 3))), + [push("b", Some(3))] + ); +} + +#[test] +fn equal_edits_on_both_sides_are_marked_synced_without_a_conflict() { + let local = profile("c"); + let actions = reconcile(Some(&local), Some(&present("c", 5)), Some(&synced("a", 3))); + assert_eq!(actions, [marked("c", 5)]); +} + +#[test] +fn different_edits_on_both_sides_apply_the_remote_and_report_the_overwrite() { + let local = profile("b"); + let actions = reconcile(Some(&local), Some(&present("c", 5)), Some(&synced("a", 3))); + assert_eq!( + actions, + [ + save("c", 5, Some("b")), + ReplicaAction::Overwritten { + id: id(), + discarded: Box::new(profile("b")), + }, + ] + ); +} + +#[test] +fn a_new_local_profile_without_history_is_sent_as_new() { + let local = profile("a"); + assert_eq!(reconcile(Some(&local), None, None), [push("a", None)]); +} + +#[test] +fn a_new_remote_profile_without_history_is_applied_as_new() { + let actions = reconcile(None, Some(&present("a", 2)), None); + assert_eq!(actions, [save("a", 2, None)]); +} + +#[test] +fn a_local_and_a_remote_profile_without_history_are_reconciled_like_two_edits() { + let local = profile("a"); + assert_eq!( + reconcile(Some(&local), Some(&present("a", 2)), None), + [marked("a", 2)] + ); + assert_eq!( + reconcile(Some(&profile("b")), Some(&present("a", 2)), None), + [ + save("a", 2, Some("b")), + ReplicaAction::Overwritten { + id: id(), + discarded: Box::new(profile("b")), + }, + ] + ); +} + +#[test] +fn a_local_deletion_is_sent_when_the_remote_is_unchanged() { + let actions = reconcile(None, None, Some(&synced("a", 3))); + assert_eq!( + actions, + [ReplicaAction::PushDelete { + id: id(), + expected: version(3), + }] + ); +} + +#[test] +fn a_remote_edit_restores_a_locally_deleted_profile() { + let actions = reconcile(None, Some(&present("b", 4)), Some(&synced("a", 3))); + assert_eq!(actions, [save("b", 4, None)]); +} + +#[test] +fn a_remote_tombstone_deletes_an_unchanged_local_profile() { + let local = profile("a"); + let tombstone = remote(RemoteBody::Deleted, 4); + let actions = reconcile(Some(&local), Some(&tombstone), Some(&synced("a", 3))); + assert_eq!( + actions, + [ReplicaAction::DeleteLocal { + id: id(), + expected: Box::new(profile("a")), + }] + ); +} + +#[test] +fn a_local_edit_is_sent_over_a_remote_tombstone() { + let local = profile("b"); + let tombstone = remote(RemoteBody::Deleted, 4); + let actions = reconcile(Some(&local), Some(&tombstone), Some(&synced("a", 3))); + assert_eq!(actions, [push("b", Some(4))]); +} + +#[test] +fn deleting_on_both_sides_forgets_the_agreement() { + let tombstone = remote(RemoteBody::Deleted, 4); + let actions = reconcile(None, Some(&tombstone), Some(&synced("a", 3))); + assert_eq!(actions, [ReplicaAction::Forget { id: id() }]); +} + +#[test] +fn an_unrecognized_remote_object_never_touches_the_local_profile() { + let unreadable = remote(RemoteBody::Unrecognized, 4); + let expected = [ReplicaAction::NeedsNewerClient { id: id() }]; + let local = profile("b"); + let history = synced("a", 3); + assert_eq!( + reconcile(Some(&local), Some(&unreadable), Some(&history)), + expected + ); + assert_eq!(reconcile(Some(&local), Some(&unreadable), None), expected); + assert_eq!(reconcile(None, Some(&unreadable), Some(&history)), expected); + assert_eq!(reconcile(None, Some(&unreadable), None), expected); +} + +#[test] +fn a_remote_tombstone_without_history_is_ignored_unless_a_local_profile_exists() { + let tombstone = remote(RemoteBody::Deleted, 4); + assert_eq!(reconcile(None, Some(&tombstone), None), []); + let local = profile("a"); + assert_eq!( + reconcile(Some(&local), Some(&tombstone), None), + [push("a", Some(4))] + ); +} + +fn profiles_of(items: &[Profile]) -> Profiles { + let mut profiles = Profiles::default(); + for item in items { + profiles.upsert(item.clone()); + } + profiles +} + +#[test] +fn a_plan_covers_every_profile_known_to_any_side_once() { + let other = Profile { + id: profile_id_for("other"), + ..profile("other") + }; + let third = Profile { + id: profile_id_for("third"), + ..profile("third") + }; + let local = profiles_of(&[profile("a"), other.clone()]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 3)); + state.agree( + third.id, + SyncedEntry { + version: version(2), + profile: third.clone(), + }, + ); + let change = RemoteProfile { + id: id(), + version: version(4), + body: RemoteBody::Present(Box::new(profile("b"))), + }; + let actions = plan(&local, &[change], &state); + let ids: Vec<_> = actions.iter().map(ReplicaAction::id).collect(); + assert_eq!(ids, [id(), other.id, third.id]); + assert!(matches!(actions[0], ReplicaAction::SaveLocal { .. })); + assert!(matches!( + actions[1], + ReplicaAction::Push { expected: None, .. } + )); + assert!(matches!(actions[2], ReplicaAction::PushDelete { .. })); +} + +#[test] +fn a_plan_leaves_an_unrecognized_profile_alone_until_a_readable_version_arrives() { + let local = profiles_of(&[profile("edited")]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 3)); + state.mark_unrecognized(id()); + assert_eq!(plan(&local, &[], &state), []); + let readable = present("b", 6); + let actions = plan(&local, &[readable], &state); + assert_eq!(actions.len(), 2); + assert!(matches!(actions[1], ReplicaAction::Overwritten { .. })); +} + +#[test] +fn replica_state_survives_serialization_with_its_unrecognized_ids() { + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 3)); + state.advance(ReplicaCursor::new("7")); + state.mark_unrecognized(profile_id_for("newer")); + let text = serde_json::to_string(&state).unwrap(); + let restored: ReplicaState = serde_json::from_str(&text).unwrap(); + assert_eq!(restored, state); + assert!(restored.is_unrecognized(profile_id_for("newer"))); + assert_eq!(restored.cursor().map(ReplicaCursor::as_str), Some("7")); +} + +#[test] +fn agreeing_or_forgetting_a_profile_makes_it_readable_again() { + let mut state = ReplicaState::default(); + state.mark_unrecognized(id()); + assert!(state.has_unrecognized()); + state.agree(id(), synced("a", 3)); + assert!(!state.is_unrecognized(id())); + state.mark_unrecognized(id()); + state.forget(id()); + assert!(!state.has_unrecognized() && state.entry(id()).is_none()); +} + +#[test] +fn a_feed_entry_older_than_the_agreed_version_never_reverts_the_local_profile() { + let synced = synced("current", 5); + let stale = [ + present("old", 3), + remote(RemoteBody::Deleted, 4), + remote(RemoteBody::Unrecognized, 2), + ]; + for change in &stale { + assert_eq!( + reconcile(Some(&profile("current")), Some(change), Some(&synced)), + [] + ); + } +} + +#[test] +fn a_local_edit_is_still_sent_against_the_agreed_version_beside_a_stale_feed_entry() { + assert_eq!( + reconcile( + Some(&profile("edited")), + Some(&present("old", 3)), + Some(&synced("current", 5)), + ), + [push("edited", Some(5))] + ); +} + +#[test] +fn a_plan_ignores_stale_entries_and_keeps_an_unreadable_profile_unreadable() { + let local = profiles_of(&[profile("current")]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("current", 5)); + assert_eq!(plan(&local, &[present("old", 3)], &state), []); + state.mark_unrecognized(id()); + assert_eq!(plan(&local, &[present("old", 3)], &state), []); + assert!(state.is_unrecognized(id())); +} + +#[test] +fn of_two_changes_to_one_profile_the_later_one_stands() { + let local = profiles_of(&[profile("a")]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 2)); + let changes = [present("b", 3), remote(RemoteBody::Deleted, 5)]; + assert_eq!( + plan(&local, &changes, &state), + [ReplicaAction::DeleteLocal { + id: id(), + expected: Box::new(profile("a")), + }] + ); +} diff --git a/crates/connections-model/src/test_support.rs b/crates/connections-model/src/test_support.rs index a15defa7..a22eabd1 100644 --- a/crates/connections-model/src/test_support.rs +++ b/crates/connections-model/src/test_support.rs @@ -11,6 +11,8 @@ mod gates; mod repository; pub use gates::{BlockingGate, BlockingRelease}; pub use repository::{InMemoryConnectionRepository, RepositoryCall, RepositoryGate}; +mod replica; +pub use replica::{MemoryReplica, ReplicaCall}; #[derive(Clone, Debug, PartialEq, Eq)] pub enum ObservationCall { Resolve(String), diff --git a/crates/connections-model/src/test_support/replica.rs b/crates/connections-model/src/test_support/replica.rs new file mode 100644 index 00000000..140b605d --- /dev/null +++ b/crates/connections-model/src/test_support/replica.rs @@ -0,0 +1,415 @@ +//! An in-memory profile replica with scripted remote writers, refusals and outages. +use crate::{ + profiles::{Profile, ProfileId}, + replica::{ + ProfileReplica, RemoteBody, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaPage, + ReplicaVersion, + }, +}; +use futures::{ + FutureExt as _, StreamExt as _, + channel::{mpsc, oneshot}, + future::BoxFuture, + stream::BoxStream, +}; +use std::{ + collections::{HashMap, HashSet, VecDeque}, + sync::Mutex, +}; + +/// A call the replica received, in order. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ReplicaCall { + Changes(Option), + Put { + id: ProfileId, + expected: Option, + }, + Delete { + id: ProfileId, + expected: ReplicaVersion, + }, +} + +struct Stored { + body: RemoteBody, + version: u64, +} + +/// What writes are refused: single connections, or all of them. +#[derive(Default)] +struct Forbidden { + profiles: HashSet, + all: bool, +} + +#[derive(Default)] +struct Inner { + sequence: u64, + objects: HashMap, + feed: Vec<(u64, ProfileId)>, + offline: bool, + /// The account keys are closed: the server is reachable but every call is refused. + locked: bool, + /// Successful writes left before the replica goes offline. + offline_after: Option, + /// The answer of the next write waits for this signal. + held: Option>, + forbidden: Forbidden, + forced_conflicts: usize, + concurrent: Option, + page_size: Option, + calls: Vec, +} + +/// Versions and cursors come from one counter, so a cursor is "after write N". +#[derive(Default)] +pub struct MemoryReplica { + inner: Mutex, + watchers: Mutex>>, + scripted_pages: Mutex>, + held_read: Mutex>>, +} + +impl Inner { + /// Why a call cannot be served now: closed keys are noticed before the network. + fn unserved(&self) -> Option { + if self.locked { + Some(ReplicaError::Locked) + } else if self.offline { + Some(ReplicaError::Unavailable) + } else { + None + } + } + + fn store(&mut self, id: ProfileId, body: RemoteBody) -> ReplicaVersion { + self.sequence += 1; + self.feed.push((self.sequence, id)); + self.objects.insert( + id, + Stored { + body, + version: self.sequence, + }, + ); + ReplicaVersion::new(self.sequence) + } + + fn check_write( + &mut self, + id: ProfileId, + expected: Option, + ) -> Result<(), ReplicaError> { + if let Some(error) = self.unserved() { + return Err(error); + } + if let Some(profile) = self.concurrent.take() { + let body = RemoteBody::Present(Box::new(profile.clone())); + self.store(profile.id, body); + } + if self.forbidden.all || self.forbidden.profiles.contains(&id) { + return Err(ReplicaError::Forbidden { id }); + } + if self.forced_conflicts > 0 { + self.forced_conflicts -= 1; + return Err(ReplicaError::Conflict { id }); + } + let current = self + .objects + .get(&id) + .map(|stored| ReplicaVersion::new(stored.version)); + if current == expected { + Ok(()) + } else { + Err(ReplicaError::Conflict { id }) + } + } + + fn page(&self, after: Option<&ReplicaCursor>) -> ReplicaPage { + let from = after + .and_then(|cursor| cursor.as_str().parse::().ok()) + .unwrap_or(0); + let mut ids = Vec::new(); + let mut last = from; + let mut more = false; + for (sequence, id) in self.feed.iter().filter(|(sequence, _)| *sequence > from) { + if self.page_size.is_some_and(|size| ids.len() >= size) { + more = true; + break; + } + last = *sequence; + ids.retain(|seen| seen != id); + ids.push(*id); + } + let changes = ids + .into_iter() + .filter_map(|id| { + let stored = self.objects.get(&id)?; + Some(RemoteProfile { + id, + version: ReplicaVersion::new(stored.version), + body: stored.body.clone(), + }) + }) + .collect(); + ReplicaPage { + changes, + cursor: ReplicaCursor::new(last.to_string()), + more, + } + } +} + +impl MemoryReplica { + pub fn new() -> Self { + Self::default() + } + + fn with(&self, work: impl FnOnce(&mut Inner) -> R) -> R { + work(&mut self.inner.lock().unwrap_or_else(|e| e.into_inner())) + } + + fn notify(&self) { + self.watchers + .lock() + .unwrap_or_else(|e| e.into_inner()) + .retain(|watcher| watcher.unbounded_send(()).is_ok()); + } + + /// Another device writes `profile`; watchers are told. + pub fn write_remote(&self, profile: Profile) -> ReplicaVersion { + let id = profile.id; + let version = self.with(|inner| inner.store(id, RemoteBody::Present(Box::new(profile)))); + self.notify(); + version + } + + /// Another device deletes the profile, leaving a tombstone. + pub fn delete_remote(&self, id: ProfileId) -> ReplicaVersion { + let version = self.with(|inner| inner.store(id, RemoteBody::Deleted)); + self.notify(); + version + } + + /// A newer client wrote something this client cannot read. + pub fn write_unrecognized(&self, id: ProfileId) -> ReplicaVersion { + let version = self.with(|inner| inner.store(id, RemoteBody::Unrecognized)); + self.notify(); + version + } + + /// Reachability: while offline every call answers `Unavailable`. + pub fn set_offline(&self, offline: bool) { + self.with(|inner| inner.offline = offline); + if !offline { + self.notify(); + } + } + + /// The account keys: while locked every call answers `Locked`, even when reachable. + /// Unlocking tells watchers, as the real replica does when its keys open. + pub fn set_locked(&self, locked: bool) { + self.with(|inner| inner.locked = locked); + if !locked { + self.notify(); + } + } + + /// The link drops once `writes` more writes succeed, as when it breaks between two + /// requests of one operation. + pub fn go_offline_after_writes(&self, writes: usize) { + self.with(|inner| inner.offline_after = Some(writes)); + } + + /// The next write is carried out, but its answer reaches the caller only after the + /// returned sender is used: whatever happens meanwhile happens during the round. + pub fn hold_next_write(&self) -> oneshot::Sender<()> { + let (release, held) = oneshot::channel(); + self.with(|inner| inner.held = Some(held)); + release + } + + /// Writes of this profile are refused as `Forbidden`. + pub fn refuse_writes_of(&self, id: ProfileId) { + self.with(|inner| inner.forbidden.profiles.insert(id)); + } + + /// Every write is refused as `Forbidden`. + pub fn refuse_writes(&self) { + self.with(|inner| inner.forbidden.all = true); + } + + /// The next `count` writes lose a race regardless of their expectation. + pub fn conflict_next_writes(&self, count: usize) { + self.with(|inner| inner.forced_conflicts = count); + } + + /// Another device writes `profile` just before the next write is checked. + pub fn write_concurrently_before_next_write(&self, profile: Profile) { + self.with(|inner| inner.concurrent = Some(profile)); + } + + /// Changes come in pages of at most `size` writes. + pub fn page_size(&self, size: usize) { + self.with(|inner| inner.page_size = Some(size)); + } + + /// Supplies protocol pages without changing the coherent remote object store. + /// Malformed cursors are allowed for consumer validation tests; response counts stay bounded. + pub fn script_pages(&self, pages: Vec) { + assert!(pages.len() <= 512 && pages.iter().all(|page| page.changes.len() <= 20_000)); + *self + .scripted_pages + .lock() + .unwrap_or_else(|error| error.into_inner()) = pages.into(); + } + /// Suspends the next changes response; dropping its consumer closes the receipt. + pub fn hold_next_read(&self) -> oneshot::Sender<()> { + let (send, receive) = oneshot::channel(); + *self + .held_read + .lock() + .unwrap_or_else(|error| error.into_inner()) = Some(receive); + send + } + + pub fn calls(&self) -> Vec { + self.with(|inner| inner.calls.clone()) + } + + pub fn writes(&self) -> Vec { + self.calls() + .into_iter() + .filter(|call| !matches!(call, ReplicaCall::Changes(_))) + .collect() + } + + /// The version and body of the latest write of a profile. + pub fn stored(&self, id: ProfileId) -> Option<(ReplicaVersion, RemoteBody)> { + self.with(|inner| { + let stored = inner.objects.get(&id)?; + Some((ReplicaVersion::new(stored.version), stored.body.clone())) + }) + } + + /// The live profile stored under `id`, if any. + pub fn profile(&self, id: ProfileId) -> Option { + match self.stored(id)?.1 { + RemoteBody::Present(profile) => Some(*profile), + RemoteBody::Deleted | RemoteBody::Unrecognized => None, + } + } + + fn write( + &self, + id: ProfileId, + call: ReplicaCall, + expected: Option, + body: RemoteBody, + ) -> Result { + let result = self.with(|inner| { + inner.calls.push(call); + inner.check_write(id, expected)?; + let version = inner.store(id, body); + if let Some(left) = inner.offline_after.as_mut() { + *left = left.saturating_sub(1); + if *left == 0 { + inner.offline = true; + inner.offline_after = None; + } + } + Ok(version) + }); + if result.is_ok() { + self.notify(); + } + result + } + + /// The answer of a write, after the hold on it is released. + fn answered( + &self, + result: Result, + ) -> BoxFuture<'static, Result> { + match self.with(|inner| inner.held.take()) { + Some(held) => async move { + held.await.ok(); + result + } + .boxed(), + None => futures::future::ready(result).boxed(), + } + } +} + +impl ProfileReplica for MemoryReplica { + fn changes( + &self, + after: Option, + ) -> BoxFuture<'static, Result> { + let result = self.with(|inner| { + inner.calls.push(ReplicaCall::Changes( + after.as_ref().map(|c| c.as_str().to_owned()), + )); + match inner.unserved() { + Some(error) => Err(error), + None => Ok(self + .scripted_pages + .lock() + .unwrap_or_else(|error| error.into_inner()) + .pop_front() + .unwrap_or_else(|| inner.page(after.as_ref()))), + } + }); + let held = self + .held_read + .lock() + .unwrap_or_else(|error| error.into_inner()) + .take(); + async move { + if let Some(held) = held { + let _ = held.await; + } + result + } + .boxed() + } + + fn put( + &self, + profile: Profile, + expected: Option, + ) -> BoxFuture<'static, Result> { + let call = ReplicaCall::Put { + id: profile.id, + expected, + }; + let result = self.write( + profile.id, + call, + expected, + RemoteBody::Present(Box::new(profile)), + ); + self.answered(result) + } + + fn delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + ) -> BoxFuture<'static, Result> { + let call = ReplicaCall::Delete { id, expected }; + let result = self.write(id, call, Some(expected), RemoteBody::Deleted); + self.answered(result) + } + + fn watch(&self) -> BoxStream<'static, ()> { + let (sender, receiver) = mpsc::unbounded(); + self.watchers + .lock() + .unwrap_or_else(|e| e.into_inner()) + .push(sender); + receiver.boxed() + } +} diff --git a/crates/connections-store/Cargo.toml b/crates/connections-store/Cargo.toml index 0609fb47..93e0813f 100644 --- a/crates/connections-store/Cargo.toml +++ b/crates/connections-store/Cargo.toml @@ -7,6 +7,10 @@ license-file.workspace = true publish = false [package.metadata.nocterm] layer = "service" +[features] +default = [] +test-support = [] + [dependencies] nocterm-core.workspace = true gpui.workspace = true @@ -16,8 +20,10 @@ nocterm-store.workspace = true futures.workspace = true tracing.workspace = true thiserror.workspace = true +serde_json.workspace = true [dev-dependencies] nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-store = { workspace = true, features = ["test-support"] } gpui = { workspace = true, features = ["test-support"] } nocterm-connections-model = { workspace = true, features = ["test-support"] } toml.workspace = true diff --git a/crates/connections-store/src/connections.rs b/crates/connections-store/src/connections.rs index 63947a20..b8e55e63 100644 --- a/crates/connections-store/src/connections.rs +++ b/crates/connections-store/src/connections.rs @@ -227,6 +227,44 @@ impl ConnectionStore { ) -> Task> { self.enqueue(Mutation::DeleteGroup { group, expected }, cx) } + /// Renames the folder `from` for the listed `members` only (`to` empty: error); a + /// connection that joined the folder after it was listed keeps the old name. + pub fn rename_group_of( + &mut self, + members: Vec, + from: String, + to: String, + cx: &mut Context, + ) -> Task> { + let to = Some(to); + self.enqueue(Mutation::Regroup { members, from, to }, cx) + } + /// Moves the listed `members` of a folder to the top level; the folder name is forgotten. + pub fn ungroup_of( + &mut self, + members: Vec, + group: String, + cx: &mut Context, + ) -> Task> { + let to = None; + self.enqueue( + Mutation::Regroup { + members, + from: group, + to, + }, + cx, + ) + } + /// Deletes the listed `members` of a folder, provided each is still in it. + pub fn delete_group_of( + &mut self, + group: String, + members: Vec, + cx: &mut Context, + ) -> Task> { + self.enqueue(Mutation::DeleteMembers { group, members }, cx) + } pub fn associate_credential( &mut self, scope: CredentialScope, diff --git a/crates/connections-store/src/connections/tests.rs b/crates/connections-store/src/connections/tests.rs index 59565e89..7d29947d 100644 --- a/crates/connections-store/src/connections/tests.rs +++ b/crates/connections-store/src/connections/tests.rs @@ -7,6 +7,7 @@ use nocterm_session::{Auth, Target}; use nocterm_store::{ShutdownCoordinator, SnapshotWriter, StoreError}; use std::sync::{Arc, Mutex}; +mod folders; mod publication; mod shutdown; diff --git a/crates/connections-store/src/connections/tests/folders.rs b/crates/connections-store/src/connections/tests/folders.rs new file mode 100644 index 00000000..03c206a8 --- /dev/null +++ b/crates/connections-store/src/connections/tests/folders.rs @@ -0,0 +1,83 @@ +use super::*; + +#[gpui::test] +async fn folder_actions_for_listed_members_leave_the_other_members_alone( + cx: &mut gpui::TestAppContext, +) { + let grouped = |name: &str| { + let mut item = profile(name); + item.group = Some("Work".into()); + item + }; + let (mine, theirs) = (grouped("mine"), grouped("theirs")); + let entity = cx.new(|_| Connections::in_memory()); + for item in [&mine, &theirs] { + entity + .update(cx, |e, cx| e.save_profile(item.clone(), cx)) + .await + .unwrap(); + } + let group_of = |id, cx: &mut gpui::TestAppContext| { + entity.read_with(cx, |e, cx| e.profiles(cx).get(id).unwrap().group.clone()) + }; + entity + .update(cx, |e, cx| { + e.rename_group_of(vec![mine.id], "Work".into(), " Team ".into(), cx) + }) + .await + .unwrap(); + assert_eq!(group_of(mine.id, cx), Some("Team".into())); + assert_eq!(group_of(theirs.id, cx), Some("Work".into())); + + entity + .update(cx, |e, cx| e.ungroup_of(vec![mine.id], "Team".into(), cx)) + .await + .unwrap(); + assert_eq!(group_of(mine.id, cx), None); + assert_eq!(group_of(theirs.id, cx), Some("Work".into())); + + entity + .update(cx, |e, cx| { + e.save_profile( + Profile { + group: Some("Work".into()), + ..mine.clone() + }, + cx, + ) + }) + .await + .unwrap(); + entity + .update(cx, |e, cx| { + e.delete_group_of("Work".into(), vec![mine.id], cx) + }) + .await + .unwrap(); + entity.read_with(cx, |e, cx| { + assert!(e.profiles(cx).get(mine.id).is_none()); + assert!(e.profiles(cx).get(theirs.id).is_some()); + }); +} + +#[gpui::test] +async fn deleting_listed_members_is_refused_when_one_left_the_folder( + cx: &mut gpui::TestAppContext, +) { + let mut a = profile("a"); + a.group = Some("Work".into()); + let entity = cx.new(|_| Connections::in_memory()); + entity + .update(cx, |e, cx| e.save_profile(a.clone(), cx)) + .await + .unwrap(); + entity + .update(cx, |e, cx| e.move_profile(a.id, None, cx)) + .await + .unwrap(); + entity + .update(cx, |e, cx| e.delete_group_of("Work".into(), vec![a.id], cx)) + .await + .unwrap_err(); + assert!(entity.read_with(cx, |e, cx| e.profiles(cx).get(a.id).is_some())); +} diff --git a/crates/connections-store/src/facts.rs b/crates/connections-store/src/facts.rs index 169d9aeb..b423ff2a 100644 --- a/crates/connections-store/src/facts.rs +++ b/crates/connections-store/src/facts.rs @@ -91,6 +91,7 @@ pub struct FactsService { probes: admission::ProbeLedger, locations: location::LocationAdmission, flags: flags::RawFlags, + flag_retry: Option<(std::time::Instant, gpui::Task<()>)>, jobs: jobs::ObservationJobs, policy: policy::DetectionPolicy, } @@ -347,6 +348,7 @@ impl FactsService { if self.closing(cx) { return; } + self.flags.retry_due(cx.background_executor().now()); while let Some(code) = self.flags.pending() { let Some(job) = self.jobs.reserve_flag() else { break; @@ -363,7 +365,33 @@ impl FactsService { }) .detach(); } + self.schedule_flag_retry(cx); } + + fn schedule_flag_retry(&mut self, cx: &mut Context) { + let deadline = self.flags.retry_deadline(); + if self.flag_retry.as_ref().map(|(deadline, _)| *deadline) == deadline { + return; + } + self.flag_retry = deadline.map(|deadline| { + let timer = cx + .background_executor() + .timer(deadline.saturating_duration_since(cx.background_executor().now())); + ( + deadline, + cx.spawn(async move |this, cx| { + timer.await; + this.update(cx, |this, cx| { + this.flag_retry = None; + this.drain_flags(cx); + cx.notify(); + }) + .ok(); + }), + ) + }); + } + fn finish_flag( &mut self, code: CountryCode, @@ -376,7 +404,8 @@ impl FactsService { if !released || self.closing(cx) { return; } - self.flags.finish(code, &job, result); + self.flags + .finish(code, &job, result, cx.background_executor().now()); self.progress(cx); } fn progress(&mut self, cx: &mut Context) { diff --git a/crates/connections-store/src/facts/flags.rs b/crates/connections-store/src/facts/flags.rs index 3c9b419f..3a44e875 100644 --- a/crates/connections-store/src/facts/flags.rs +++ b/crates/connections-store/src/facts/flags.rs @@ -1,6 +1,10 @@ use super::tickets::JobKey; use nocterm_connections_model::observation::{CountryCode, FlagBytes, ObservationError}; -use std::{collections::HashMap, sync::Arc}; +use std::{ + collections::HashMap, + sync::Arc, + time::{Duration, Instant}, +}; #[derive(Clone, Debug)] pub enum FlagState { Requested, @@ -11,6 +15,8 @@ pub enum FlagState { struct FlagEntry { state: FlagState, job: Option, + attempts: u8, + retry_at: Option, } pub(super) struct RawFlags { entries: HashMap, @@ -29,6 +35,8 @@ impl RawFlags { FlagState::Unavailable }, job: None, + attempts: 0, + retry_at: None, }); } pub(super) fn pending(&self) -> Option { @@ -38,6 +46,8 @@ impl RawFlags { } pub(super) fn loading(&mut self, code: CountryCode, job: JobKey) { if let Some(entry) = self.entries.get_mut(&code) { + entry.attempts += 1; + entry.retry_at = None; entry.state = FlagState::Loading; entry.job = Some(job); } @@ -47,6 +57,7 @@ impl RawFlags { code: CountryCode, job: &JobKey, result: Result>, ObservationError>, + now: Instant, ) -> bool { let Some(entry) = self.entries.get_mut(&code) else { return false; @@ -70,12 +81,41 @@ impl RawFlags { Ok(None) => FlagState::Unavailable, Err(error) => { tracing::debug!(%error,"flag acquisition failed"); + if entry.attempts < 3 && retryable(&error) { + entry.retry_at = + Some(now + Duration::from_secs(if entry.attempts == 1 { 5 } else { 30 })); + } FlagState::Unavailable } }; true } + pub(super) fn retry_due(&mut self, now: Instant) { + for entry in self.entries.values_mut() { + if entry.retry_at.is_some_and(|deadline| deadline <= now) { + entry.retry_at = None; + entry.state = FlagState::Requested; + } + } + } + pub(super) fn retry_deadline(&self) -> Option { + self.entries + .values() + .filter_map(|entry| entry.retry_at) + .min() + } pub(super) fn state(&self, code: CountryCode) -> Option<&FlagState> { self.entries.get(&code).map(|entry| &entry.state) } } + +fn retryable(error: &ObservationError) -> bool { + match error { + ObservationError::Network(_) + | ObservationError::Resolution(_) + | ObservationError::Body(_) + | ObservationError::Cache(_) => true, + ObservationError::Status { status, .. } => *status >= 500 || matches!(*status, 408 | 429), + ObservationError::BodyLimit { .. } | ObservationError::Decode(_) => false, + } +} diff --git a/crates/connections-store/src/facts/prepared.rs b/crates/connections-store/src/facts/prepared.rs index 33c1cfe7..71639d10 100644 --- a/crates/connections-store/src/facts/prepared.rs +++ b/crates/connections-store/src/facts/prepared.rs @@ -55,6 +55,7 @@ impl FactsPrepared { probes: admission::ProbeLedger::new(), locations, flags: flags::RawFlags::new(), + flag_retry: None, jobs: jobs::ObservationJobs::new(executor), policy, }); diff --git a/crates/connections-store/src/facts/source.rs b/crates/connections-store/src/facts/source.rs index f60d116b..d151cd53 100644 --- a/crates/connections-store/src/facts/source.rs +++ b/crates/connections-store/src/facts/source.rs @@ -37,6 +37,10 @@ impl ProbeSourceLease { pub(super) fn identity(&self) -> ProbeSourceIdentity { self.identity } + /// Equality of the captured lease lineage, independent of descriptive host/profile ids. + pub fn same_origin(&self, other: &Self) -> bool { + Rc::ptr_eq(&self.revoked, &other.revoked) + } pub fn revoke(&self) { self.revoked.set(true); } diff --git a/crates/connections-store/src/facts/tests/flags.rs b/crates/connections-store/src/facts/tests/flags.rs index 5e09076b..2aba3b16 100644 --- a/crates/connections-store/src/facts/tests/flags.rs +++ b/crates/connections-store/src/facts/tests/flags.rs @@ -96,3 +96,121 @@ fn manual_saved_countries_start_without_a_known_row_or_live_session(cx: &mut Tes ); assert_eq!(fixture.os.calls(), 0); } + +fn transient_flag_failure() -> nocterm_connections_model::observation::ObservationError { + nocterm_connections_model::observation::ObservationError::Network(Box::new( + std::io::Error::from(std::io::ErrorKind::NotConnected), + )) +} + +#[gpui::test] +fn transient_flag_failure_retries_after_cooldown_and_publishes_the_new_source( + cx: &mut TestAppContext, +) { + let fixture = Fixture::new( + profile(), + Default::default(), + true, + threaded(cx), + ready_writer(), + cx, + ); + let code = CountryCode::new("de").unwrap(); + fixture.script.flag_next(Err(transient_flag_failure())); + fixture.script.flag_next(Ok(Some(b"".to_vec()))); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Unavailable)) + }) + }); + for _ in 0..20 { + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + } + assert_eq!(fixture.script.calls().len(), 1); + cx.executor().advance_clock(Duration::from_secs(5)); + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Ready(_))) + }) + }); + assert_eq!(fixture.script.calls().len(), 2); +} + +#[gpui::test] +fn an_offline_flag_has_at_most_three_automatic_attempts(cx: &mut TestAppContext) { + let fixture = Fixture::new( + profile(), + Default::default(), + true, + threaded(cx), + ready_writer(), + cx, + ); + let code = CountryCode::new("de").unwrap(); + for _ in 0..3 { + fixture.script.flag_next(Err(transient_flag_failure())); + } + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + for attempt in 1..=3 { + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Unavailable)) + }) && fixture.script.calls().len() == attempt + }); + cx.executor().advance_clock(Duration::from_secs(31)); + cx.run_until_parked(); + } + assert_eq!(fixture.script.calls().len(), 3); + cx.executor().advance_clock(Duration::from_secs(300)); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + cx.run_until_parked(); + assert_eq!(fixture.script.calls().len(), 3); +} + +#[gpui::test] +fn a_missing_flag_is_permanently_negative_without_network_retries(cx: &mut TestAppContext) { + let fixture = Fixture::new( + profile(), + Default::default(), + true, + threaded(cx), + ready_writer(), + cx, + ); + let code = CountryCode::new("xx").unwrap(); + fixture.script.flag_next(Err( + nocterm_connections_model::observation::ObservationError::Status { + status: 404, + source: Box::new(std::io::Error::from(std::io::ErrorKind::NotFound)), + }, + )); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Unavailable)) + }) + }); + cx.executor().advance_clock(Duration::from_secs(300)); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + cx.run_until_parked(); + assert_eq!(fixture.script.calls().len(), 1); +} diff --git a/crates/connections-store/src/lib.rs b/crates/connections-store/src/lib.rs index 38ce58f8..2178f52e 100644 --- a/crates/connections-store/src/lib.rs +++ b/crates/connections-store/src/lib.rs @@ -1,27 +1,38 @@ //! Ordered connection publication and bounded raw server-fact acquisition. //! Owns: checked profiles/recents, committed publication carrier, raw Known/flag -//! state, original-source tickets, policy history, physical reservations and capture. +//! state, original-source tickets, policy history, physical reservations and capture, +//! and the synchronization of profiles with a replica (ProfileReplication), including the rollback of a refused change. //! Does not own: native I/O, views/images, session providers, settings storage, //! credentials, terminal implementation or observation composition. //! Ports used: supplied writers/clock, DNS/country/flag/OS observation contracts, -//! CountryPolicySource, ProbeSourceAuthority, BackgroundExecutor, ShutdownCoordinator. -//! Ports provided: ConnectionStore, ProfilePublications, FactsService. +//! CountryPolicySource, ProbeSourceAuthority, BackgroundExecutor, ShutdownCoordinator, +//! ProfileReplica (supplied by an adapter) and a supplied Persisted replica state. +//! Ports provided: ConnectionStore, ProfilePublications, FactsService, ProfileReplication. //! Settings: supplied enabled/revision policy projection. Globals: none. //! Invariants: committed carrier and current original source/policy are read at //! admission/completion; stale jobs keep their physical slot until actual finish; //! 32 slots are shared across windows and paired reservations are atomic. Facts //! use common optimistic persistence/capture and its existing 180-ms deadline. +//! Replication writes local profiles only through ConnectionStore, never echoes an +//! applied remote change, leaves a profile the replica holds unreadably untouched, +//! reconciles nothing while the local document failed to load and repeats a pass +//! that met a concurrent change at most three times. //! Read-only diagnostics preserve their original allocation and concrete sources through core SharedError. mod connections; mod error; mod prepared; mod publication; +mod replication; pub use connections::ConnectionStore; pub use error::ConnectionsError; pub use nocterm_connections_model::profile_authority::{ ProfileAuthorityRow, ProfilePublication, ProfileStamp, }; pub use publication::projection::ProfilePublications; +pub use replication::{ + ProfileReplication, ReplicationEvent, ReplicationFailure, ReplicationFeedError, + ReplicationStatus, +}; mod facts; mod observation; @@ -31,3 +42,6 @@ pub use facts::{ ProbeSourceAuthority, ProbeSourceIdentity, ProbeSourceLease, }; pub use observation::ObservationDeps; + +#[cfg(feature = "test-support")] +pub mod test_support; diff --git a/crates/connections-store/src/replication.rs b/crates/connections-store/src/replication.rs new file mode 100644 index 00000000..0137dce6 --- /dev/null +++ b/crates/connections-store/src/replication.rs @@ -0,0 +1,240 @@ +//! Keeps the saved profiles and a remote replica of them in agreement. +//! +//! The store stays the one the application reads and writes through +//! [`ConnectionStore`]; for a hub account it is an in-memory mirror that [`ProfileReplication::detach`] +//! empties. This entity watches its committed publications, reads the +//! replica's changes, reconciles the three sides with the pure rules of +//! `connections-model` and applies the result: remote changes through the store's +//! checked writes, local ones through the replica port. What the two sides last +//! agreed on is a [`ReplicaState`] document persisted through `nocterm_store`, so a +//! restart continues where it stopped. +mod feed; +mod round; +pub use feed::ReplicationFeedError; +mod status; + +use crate::ConnectionStore; +use futures::StreamExt as _; +use gpui::{Context, Entity, EventEmitter, Subscription, Task}; +use nocterm_connections_model::{ + profiles::ProfileId, + replica::{ProfileReplica, ReplicaState}, +}; +use nocterm_store::{Persisted, Publication, SnapshotWriter}; +use round::{Outcome, Round}; +use std::{collections::HashSet, sync::Arc}; + +pub use status::{ReplicationEvent, ReplicationFailure, ReplicationStatus}; + +/// Pending writes of the agreed-state document; its edits are coalesced snapshots. +const STATE_WRITES: usize = 8; + +/// A standing refusal that has been reported. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum Announced { + /// The local document refused a remote change. + Skipped(ProfileId), + /// The replica refused a local profile that has no agreed version to go back to. + Refused(ProfileId), +} + +pub struct ProfileReplication { + store: Entity, + state: Entity>, + replica: Option>, + status: ReplicationStatus, + run: Option>, + rerun: bool, + watcher: Option>, + /// Refusals already reported and still standing: a remote change the local document + /// refused (the cursor stays behind it) and a local profile the replica refused and + /// never agreed to. Each pass meets them again but says so once. + refused: HashSet, + _publications: Subscription, +} + +impl EventEmitter for ProfileReplication {} + +impl ProfileReplication { + /// `state` is the document built by [`Self::state_document`] and registered + /// with the shutdown coordinator by the application. + pub fn new( + store: Entity, + state: Entity>, + cx: &mut Context, + ) -> Self { + let publications = store.update(cx, |store, cx| store.publications(cx)); + let subscription = cx.observe(&publications, |this, _, cx| this.request_sync(cx)); + Self { + store, + state, + replica: None, + status: ReplicationStatus::Detached, + run: None, + rerun: false, + watcher: None, + refused: HashSet::new(), + _publications: subscription, + } + } + + /// The agreed-state document. It publishes optimistically: the replication reads + /// its own latest edits back while the disk catches up. + pub fn state_document( + initial: ReplicaState, + writer: SnapshotWriter, + ) -> Persisted { + Persisted::new(initial, writer, Publication::Optimistic, STATE_WRITES) + } + + /// Starts synchronizing with `replica`, replacing a previously attached one. + /// The agreed state is kept: a restart continues with the same replica. + pub fn attach(&mut self, replica: Arc, cx: &mut Context) { + self.stop(cx); + let mut changes = replica.watch(); + self.watcher = Some(cx.spawn(async move |this, cx| { + while changes.next().await.is_some() { + if this.update(cx, |this, cx| this.request_sync(cx)).is_err() { + break; + } + } + })); + self.replica = Some(replica); + self.start(cx); + } + + /// Stops synchronizing, forgets what was agreed with that replica and empties the store: + /// the store is the mirror of one account, so nothing in it may be offered to another + /// account's replica or shown after the account left. The agreement and the feed position + /// mean nothing to another replica either. + pub fn detach(&mut self, cx: &mut Context) { + self.stop(cx); + self.clear_mirror(cx); + self.state.update(cx, |state, cx| { + if let Err(error) = state.update_latest(|state| *state = ReplicaState::default(), cx) { + tracing::error!(%error, "could not reset the profile replica state"); + } + }); + self.set_status(ReplicationStatus::Detached, cx); + } + + /// Removes every profile from the store. With no replica attached nothing is sent. + fn clear_mirror(&mut self, cx: &mut Context) { + let removals: Vec<_> = self.store.update(cx, |store, cx| { + let ids: Vec = store + .profiles(cx) + .iter() + .map(|profile| profile.id) + .collect(); + ids.into_iter() + .map(|id| store.delete_profile(id, cx)) + .collect() + }); + cx.spawn(async move |_, _| { + for removal in removals { + if let Err(error) = removal.await { + tracing::error!(%error, "could not clear a profile of the mirror"); + } + } + }) + .detach(); + } + + pub fn status(&self) -> &ReplicationStatus { + &self.status + } + + fn stop(&mut self, cx: &mut Context) { + self.replica = None; + self.watcher = None; + self.run = None; + self.rerun = false; + cx.notify(); + } + + fn request_sync(&mut self, cx: &mut Context) { + if self.replica.is_none() { + return; + } + if self.run.is_some() { + self.rerun = true; + } else { + self.start(cx); + } + } + + fn start(&mut self, cx: &mut Context) { + let Some(replica) = self.replica.clone() else { + return; + }; + let round = Round::new( + replica, + self.store.clone(), + self.state.clone(), + cx.weak_entity(), + ); + self.set_status(ReplicationStatus::Syncing, cx); + self.run = Some(cx.spawn(async move |this, cx| { + let outcome = round.run(cx).await; + if let Some(outcome) = outcome { + this.update(cx, |this, cx| this.finish(outcome, cx)).ok(); + } + })); + } + + /// Reports a profile the pass skipped. A refused remote change is read again by + /// every pass until it can be applied, but is announced only the first time. + fn announce_skip( + &mut self, + id: ProfileId, + failure: ReplicationFailure, + cx: &mut Context, + ) { + let first = !matches!(failure, ReplicationFailure::Rejected { .. }) + || self.refused.insert(Announced::Skipped(id)); + if first { + cx.emit(ReplicationEvent::Failed(failure)); + } + } + + /// Reports a refusal of the replica. One that restored the agreed version is news + /// once by itself; one that left a never-agreed profile local would repeat with + /// every pass, so it is announced only the first time. + fn announce_refusal(&mut self, id: ProfileId, event: ReplicationEvent, cx: &mut Context) { + let restored = matches!(event, ReplicationEvent::Refused { restored: true, .. }); + if restored || self.refused.insert(Announced::Refused(id)) { + cx.emit(event); + } + } + + /// A pass left nothing refused behind it: later refusals are news again. + fn forget_refusals(&mut self) { + self.refused.clear(); + } + + fn finish(&mut self, outcome: Outcome, cx: &mut Context) { + self.run = None; + let status = match outcome { + Outcome::Synced => ReplicationStatus::Synced, + Outcome::NeedsNewerClient => ReplicationStatus::NeedsNewerClient, + Outcome::Offline => ReplicationStatus::Offline, + Outcome::Locked => ReplicationStatus::Locked, + Outcome::Failed(failure) => { + cx.emit(ReplicationEvent::Failed(failure.clone())); + ReplicationStatus::Failed(failure) + } + }; + self.set_status(status, cx); + if std::mem::take(&mut self.rerun) { + self.start(cx); + } + } + + fn set_status(&mut self, status: ReplicationStatus, cx: &mut Context) { + self.status = status; + cx.notify(); + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/connections-store/src/replication/feed.rs b/crates/connections-store/src/replication/feed.rs new file mode 100644 index 00000000..77b27650 --- /dev/null +++ b/crates/connections-store/src/replication/feed.rs @@ -0,0 +1,145 @@ +//! Bounded acquisition of one complete feed, before any reconciliation effects. +use nocterm_connections_model::{ + profiles::Profile, + replica::{ + ProfileReplica, RemoteBody, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaPage, + }, +}; +use nocterm_core::{SharedError, bounded_io::CountingWriter}; +use std::{collections::HashSet, io, sync::Arc}; + +const MAX_PAGES: usize = 256; +const MAX_CHANGES: usize = 16_384; +const MAX_BYTES: usize = 16 * 1024 * 1024; +const MAX_CURSOR_BYTES: usize = 4096; + +#[derive(Clone, Debug, thiserror::Error)] +pub enum ReplicationFeedError { + #[error("The profile feed exceeds {MAX_PAGES} pages; nothing was synchronized.")] + Pages, + #[error("The profile feed exceeds {MAX_CHANGES} changes; nothing was synchronized.")] + Changes, + #[error("The profile feed exceeds its 16 MiB payload budget; nothing was synchronized.")] + Bytes, + #[error("The profile feed cursor exceeds {MAX_CURSOR_BYTES} bytes.")] + CursorSize, + #[error("The profile feed cursor did not advance.")] + NonProgress, + #[error("The profile feed repeated an earlier cursor.")] + Cycle, + #[error("The profile feed did not finish within two minutes.")] + TimedOut, + #[error("The profile feed could not be encoded for size validation: {0}")] + Encoding(#[source] SharedError), +} +pub(super) enum FetchError { + Remote(ReplicaError), + Invalid(ReplicationFeedError), +} +impl From for FetchError { + fn from(error: ReplicationFeedError) -> Self { + Self::Invalid(error) + } +} + +struct Feed { + changes: Vec, + cursor: Option, + seen: HashSet, + pages: usize, + bytes: usize, +} +impl Feed { + fn new(cursor: Option) -> Result { + let mut seen = HashSet::new(); + if let Some(cursor) = &cursor { + check_cursor(cursor)?; + seen.insert(cursor.as_str().to_owned()); + } + Ok(Self { + changes: Vec::new(), + cursor, + seen, + pages: 0, + bytes: 0, + }) + } + fn accept(&mut self, page: ReplicaPage) -> Result { + check_cursor(&page.cursor)?; + if page.changes.len() > MAX_CHANGES.saturating_sub(self.changes.len()) { + return Err(ReplicationFeedError::Changes); + } + let unchanged = self.cursor.as_ref() == Some(&page.cursor); + if unchanged && (page.more || !page.changes.is_empty()) { + return Err(ReplicationFeedError::NonProgress); + } + if !unchanged && !self.seen.insert(page.cursor.as_str().to_owned()) { + return Err(ReplicationFeedError::Cycle); + } + for change in &page.changes { + self.charge(std::mem::size_of::())?; + if let RemoteBody::Present(profile) = &change.body { + self.charge(std::mem::size_of::())?; + let bytes = profile_bytes(profile, MAX_BYTES - self.bytes)?; + self.charge(bytes)?; + } + } + self.pages += 1; + self.changes.extend(page.changes); + self.cursor = Some(page.cursor); + Ok(page.more) + } + fn charge(&mut self, bytes: usize) -> Result<(), ReplicationFeedError> { + self.bytes = self + .bytes + .checked_add(bytes) + .filter(|bytes| *bytes <= MAX_BYTES) + .ok_or(ReplicationFeedError::Bytes)?; + Ok(()) + } +} +fn check_cursor(cursor: &ReplicaCursor) -> Result<(), ReplicationFeedError> { + if cursor.as_str().len() > MAX_CURSOR_BYTES { + Err(ReplicationFeedError::CursorSize) + } else { + Ok(()) + } +} + +/// Runs on the supplied background executor. A failing/partial feed is discarded; +/// it never becomes a partial plan or an advanced durable cursor. +pub(super) async fn fetch( + replica: Arc, + cursor: Option, +) -> Result<(Vec, ReplicaCursor), FetchError> { + let mut feed = Feed::new(cursor)?; + loop { + if feed.pages == MAX_PAGES { + return Err(ReplicationFeedError::Pages.into()); + } + let page = replica + .changes(feed.cursor.clone()) + .await + .map_err(FetchError::Remote)?; + if !feed.accept(page)? { + break; + } + } + Ok(( + feed.changes, + feed.cursor.unwrap_or_else(|| ReplicaCursor::new("")), + )) +} + +fn profile_bytes(profile: &Profile, limit: usize) -> Result { + let mut writer = CountingWriter::new(limit); + match serde_json::to_writer(&mut writer, profile) { + Ok(()) => Ok(writer.count()), + Err(error) if error.io_error_kind() == Some(io::ErrorKind::OutOfMemory) => { + Err(ReplicationFeedError::Bytes) + } + Err(error) => Err(ReplicationFeedError::Encoding(SharedError::from_error( + error, + ))), + } +} diff --git a/crates/connections-store/src/replication/round.rs b/crates/connections-store/src/replication/round.rs new file mode 100644 index 00000000..7ebc1fdc --- /dev/null +++ b/crates/connections-store/src/replication/round.rs @@ -0,0 +1,425 @@ +//! One synchronization pass: read remote changes, reconcile, apply, advance the cursor. +//! +//! Every action is persisted in the agreed state as soon as it succeeds, and the +//! cursor moves only after a pass in which nothing was left undone: not after a +//! conflict, and not after a remote change the local document refused. A pass that +//! is interrupted, retried or refused re-reads changes it already handled; those +//! reconcile to nothing because the agreed state already holds them, while a refused +//! change is offered again instead of being lost. +use super::{ProfileReplication, ReplicationEvent, ReplicationFailure}; +use crate::{ConnectionStore, ConnectionsError}; +use gpui::{AsyncApp, Entity, WeakEntity}; +use nocterm_connections_model::{ + profiles::{Profile, ProfileError, ProfileId, Profiles}, + replica::{ + ProfileReplica, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaState, ReplicaVersion, + SyncedEntry, + rules::{self, ReplicaAction}, + }, +}; +use nocterm_store::Persisted; +use std::{collections::HashSet, sync::Arc}; + +/// Passes tried before a profile that keeps changing under us is reported. +pub(super) const MAX_ATTEMPTS: usize = 3; + +pub(super) enum Outcome { + Synced, + NeedsNewerClient, + Offline, + Locked, + Failed(ReplicationFailure), +} + +/// What applying the planned actions left behind. +enum Applied { + Done, + /// Nothing is left to retry, but the replica refused a profile that stays local. + Refused, + /// A conflict left something undone: plan again from the same cursor. + Retry, + /// A remote change could not be written locally: it stays ahead of the cursor. + Deferred, +} + +/// The pass cannot continue. +enum Stop { + Halt(Outcome), + /// The application or the owning entity is gone. + Gone, +} + +/// Why one action did not finish. +enum Flow { + /// One side changed under the action; read and reconcile again. + Conflict, + /// This profile cannot be synchronized; the others still can. + Skip(ReplicationFailure), + /// The replica refused the profile and the refusal was already dealt with and + /// reported; leave the rest of its actions undone. + Refused, + Stop(Stop), +} + +impl From for Flow { + fn from(stop: Stop) -> Self { + Self::Stop(stop) + } +} + +fn gone(_: E) -> Stop { + Stop::Gone +} + +fn halt(failure: ReplicationFailure) -> Stop { + Stop::Halt(Outcome::Failed(failure)) +} + +fn remote_stop(error: ReplicaError) -> Stop { + match error { + ReplicaError::Unavailable => Stop::Halt(Outcome::Offline), + ReplicaError::Locked | ReplicaError::Stale => Stop::Halt(Outcome::Locked), + error => halt(error.into()), + } +} + +fn remote_flow(error: ReplicaError) -> Flow { + match error { + ReplicaError::Conflict { .. } => Flow::Conflict, + error => remote_stop(error).into(), + } +} + +fn local_flow(id: ProfileId, error: ConnectionsError) -> Flow { + match &error { + ConnectionsError::Profile(ProfileError::Conflict(_) | ProfileError::MissingProfile(_)) => { + Flow::Conflict + } + ConnectionsError::Profile(_) => { + Flow::Skip(ReplicationFailure::Rejected { id, source: error }) + } + _ => halt(ReplicationFailure::Local(error)).into(), + } +} + +/// The action brings a remote change into the local document. +fn writes_remote_change(action: &ReplicaAction) -> bool { + matches!( + action, + ReplicaAction::SaveLocal { .. } | ReplicaAction::DeleteLocal { .. } + ) +} + +pub(super) struct Round { + replica: Arc, + store: Entity, + state: Entity>, + events: WeakEntity, +} + +impl Round { + pub(super) fn new( + replica: Arc, + store: Entity, + state: Entity>, + events: WeakEntity, + ) -> Self { + Self { + replica, + store, + state, + events, + } + } + + /// `None` when the application went away before the pass finished. + pub(super) async fn run(self, cx: &mut AsyncApp) -> Option { + for _ in 0..MAX_ATTEMPTS { + match self.attempt(cx).await { + Ok(true) => {} + Ok(false) => return Some(self.finished(cx)), + Err(Stop::Halt(outcome)) => return Some(outcome), + Err(Stop::Gone) => return None, + } + } + Some(Outcome::Failed(ReplicationFailure::Conflicts)) + } + + fn finished(&self, cx: &mut AsyncApp) -> Outcome { + let unreadable = self + .state + .read_with(cx, |state, _| state.visible().has_unrecognized()); + if unreadable { + Outcome::NeedsNewerClient + } else { + Outcome::Synced + } + } + + /// Whether the pass must be repeated. + async fn attempt(&self, cx: &mut AsyncApp) -> Result { + self.require_readable_local(cx)?; + let (changes, cursor) = self.fetch(cx).await?; + let actions = self.plan(&changes, cx)?; + match self.apply(actions, cx).await? { + Applied::Retry => return Ok(true), + Applied::Refused => self.record(cx, move |state| state.advance(cursor))?, + Applied::Done => { + self.record(cx, move |state| state.advance(cursor))?; + self.events + .update(cx, |this, _| this.forget_refusals()) + .map_err(gone)?; + } + Applied::Deferred => {} + } + Ok(false) + } + + fn require_readable_local(&self, cx: &mut AsyncApp) -> Result<(), Stop> { + let unreadable = self + .store + .read_with(cx, |store, _| store.load_error().is_some()); + if unreadable { + return Err(halt(ReplicationFailure::LocalUnavailable)); + } + Ok(()) + } + + async fn fetch(&self, cx: &mut AsyncApp) -> Result<(Vec, ReplicaCursor), Stop> { + let after = self + .state + .read_with(cx, |state, _| state.visible().cursor().cloned()); + let replica = self.replica.clone(); + let executor = cx.background_executor().clone(); + let acquisition = executor.spawn(async move { super::feed::fetch(replica, after).await }); + let deadline = executor.timer(std::time::Duration::from_secs(120)); + futures::pin_mut!(acquisition, deadline); + match futures::future::select(acquisition, deadline).await { + futures::future::Either::Left((result, _)) => result.map_err(|error| match error { + super::feed::FetchError::Remote(error) => remote_stop(error), + super::feed::FetchError::Invalid(error) => halt(error.into()), + }), + futures::future::Either::Right(_) => { + Err(halt(super::ReplicationFeedError::TimedOut.into())) + } + } + } + + fn plan( + &self, + changes: &[RemoteProfile], + cx: &mut AsyncApp, + ) -> Result, Stop> { + let local: Profiles = self + .store + .read_with(cx, |store, cx| store.profiles(cx).clone()); + let state = self.state.read_with(cx, |state, _| state.visible().clone()); + Ok(rules::plan(&local, changes, &state)) + } + + async fn apply(&self, actions: Vec, cx: &mut AsyncApp) -> Result { + let mut skipped = HashSet::new(); + let mut applied = Applied::Done; + let mut refused = false; + for action in actions { + let id = action.id(); + if skipped.contains(&id) { + continue; + } + let from_remote = writes_remote_change(&action); + match self.execute(action, cx).await { + Ok(()) => {} + Err(Flow::Conflict) => { + applied = Applied::Retry; + skipped.insert(id); + } + Err(Flow::Refused) => { + refused = true; + skipped.insert(id); + } + Err(Flow::Skip(failure)) => { + skipped.insert(id); + if from_remote && matches!(applied, Applied::Done) { + applied = Applied::Deferred; + } + self.skipped(id, failure, cx)?; + } + Err(Flow::Stop(stop)) => return Err(stop), + } + } + Ok(match applied { + Applied::Done if refused => Applied::Refused, + other => other, + }) + } + + async fn execute(&self, action: ReplicaAction, cx: &mut AsyncApp) -> Result<(), Flow> { + match action { + ReplicaAction::SaveLocal { + version, + profile, + expected, + } => { + self.save_local(version, *profile, expected.map(|p| *p), cx) + .await + } + ReplicaAction::DeleteLocal { id, expected } => { + self.delete_local(id, *expected, cx).await + } + ReplicaAction::Push { profile, expected } => self.push(*profile, expected, cx).await, + ReplicaAction::PushDelete { id, expected } => self.push_delete(id, expected, cx).await, + ReplicaAction::MarkSynced { version, profile } => { + Ok(self.agree(version, *profile, cx)?) + } + ReplicaAction::Forget { id } => Ok(self.record(cx, move |state| state.forget(id))?), + ReplicaAction::Overwritten { id, discarded } => { + Ok(self.emit(ReplicationEvent::Overwritten { id, discarded }, cx)?) + } + ReplicaAction::NeedsNewerClient { id } => { + Ok(self.record(cx, move |state| state.mark_unrecognized(id))?) + } + } + } + + async fn save_local( + &self, + version: ReplicaVersion, + profile: Profile, + expected: Option, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let id = profile.id; + let agreed = profile.clone(); + let saving = self.store.update(cx, |store, cx| { + store.save_profile_checked(profile, expected, cx) + }); + saving.await.map_err(|error| local_flow(id, error))?; + Ok(self.agree(version, agreed, cx)?) + } + + /// The comparison and the deletion happen in one synchronous step, so an edit + /// made after planning is not deleted unseen. + async fn delete_local( + &self, + id: ProfileId, + expected: Profile, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let deleting = self.store.update(cx, |store, cx| { + (store.profiles(cx).get(id) == Some(&expected)).then(|| store.delete_profile(id, cx)) + }); + let Some(deleting) = deleting else { + return Err(Flow::Conflict); + }; + deleting.await.map_err(|error| local_flow(id, error))?; + Ok(self.record(cx, move |state| state.forget(id))?) + } + + async fn push( + &self, + profile: Profile, + expected: Option, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let id = profile.id; + match self.replica.put(profile.clone(), expected).await { + Ok(version) => Ok(self.agree(version, profile, cx)?), + Err(ReplicaError::Forbidden { .. }) => self.refused(id, profile, cx).await, + Err(error) => Err(remote_flow(error)), + } + } + + async fn push_delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + match self.replica.delete(id, expected).await { + Ok(_) => Ok(self.record(cx, move |state| state.forget(id))?), + Err(ReplicaError::Forbidden { .. }) => { + let agreed = self + .state + .read_with(cx, |state, _| state.visible().entry(id).cloned()); + match agreed { + Some(entry) => self.refused(id, entry.profile, cx).await, + None => Err(Flow::Refused), + } + } + Err(error) => Err(remote_flow(error)), + } + } + + /// The account may not write `profile`. The user is told once, and the local profile + /// goes back to what was last agreed, so nothing is sent again. A profile never agreed + /// stays local. + async fn refused( + &self, + id: ProfileId, + profile: Profile, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let agreed = self + .state + .read_with(cx, |state, _| state.visible().entry(id).cloned()); + let restored = agreed.is_some(); + if let Some(entry) = agreed { + let current = self + .store + .read_with(cx, |store, cx| store.profiles(cx).get(id).cloned()); + if current.as_ref() != Some(&entry.profile) { + let restoring = self.store.update(cx, |store, cx| { + store.save_profile_checked(entry.profile, current, cx) + }); + restoring.await.map_err(|error| local_flow(id, error))?; + } + } + let event = ReplicationEvent::Refused { + profile: Box::new(profile), + restored, + }; + self.events + .update(cx, |this, cx| this.announce_refusal(id, event, cx)) + .map_err(gone)?; + Err(Flow::Refused) + } + + /// The replica's version stands, whether this client sent it, wrote it here or both + /// already held it. + fn agree( + &self, + version: ReplicaVersion, + profile: Profile, + cx: &mut AsyncApp, + ) -> Result<(), Stop> { + let id = profile.id; + let entry = SyncedEntry { version, profile }; + self.record(cx, move |state| state.agree(id, entry)) + } + + fn record( + &self, + cx: &mut AsyncApp, + edit: impl FnOnce(&mut ReplicaState) + 'static, + ) -> Result<(), Stop> { + self.state + .update(cx, |state, cx| state.update_latest(edit, cx)) + .map(|_| ()) + .map_err(|error| halt(ReplicationFailure::State(error))) + } + + fn skipped( + &self, + id: ProfileId, + failure: ReplicationFailure, + cx: &mut AsyncApp, + ) -> Result<(), Stop> { + self.events + .update(cx, |this, cx| this.announce_skip(id, failure, cx)) + .map_err(gone) + } + + fn emit(&self, event: ReplicationEvent, cx: &mut AsyncApp) -> Result<(), Stop> { + self.events.update(cx, |_, cx| cx.emit(event)).map_err(gone) + } +} diff --git a/crates/connections-store/src/replication/status.rs b/crates/connections-store/src/replication/status.rs new file mode 100644 index 00000000..be50dfda --- /dev/null +++ b/crates/connections-store/src/replication/status.rs @@ -0,0 +1,71 @@ +//! What the replication reports to its observers. +use crate::ConnectionsError; +use nocterm_connections_model::{ + profiles::{Profile, ProfileId}, + replica::ReplicaError, +}; +use nocterm_store::StoreError; + +/// Where synchronization stands. +#[derive(Clone, Debug)] +pub enum ReplicationStatus { + /// No replica is attached; profiles stay local. + Detached, + Syncing, + /// Local and remote profiles agree. + Synced, + /// The replica could not be reached; local work continues and sync retries on the + /// next local change or remote notification. + Offline, + /// The keys that open the replica are closed; the server may be reachable. Local work + /// continues and sync resumes when the keys open and the replica notifies, or on the + /// next attach. + Locked, + /// The replica holds a profile this client cannot read; the local copy is untouched. + NeedsNewerClient, + Failed(ReplicationFailure), +} + +/// Why a synchronization could not finish, or why one profile was skipped. +#[derive(Clone, Debug, thiserror::Error)] +pub enum ReplicationFailure { + /// Saved connections failed to load, so an empty local copy would look like a + /// deletion of everything. + #[error("Saved connections could not be read, so nothing was synchronized.")] + LocalUnavailable, + /// A profile kept changing under the synchronization. + #[error("Profiles kept changing during synchronization.")] + Conflicts, + #[error(transparent)] + Feed(#[from] super::ReplicationFeedError), + #[error(transparent)] + Replica(#[from] ReplicaError), + /// The local document refused one profile; the others were synchronized. + #[error("Connection {id} could not be saved locally: {source}")] + Rejected { + id: ProfileId, + #[source] + source: ConnectionsError, + }, + #[error("Connections could not be updated: {0}")] + Local(#[source] ConnectionsError), + #[error("Synchronization state could not be kept: {0}")] + State(#[source] StoreError), +} + +#[derive(Clone, Debug)] +pub enum ReplicationEvent { + /// A remote version replaced this unsynchronized local edit. + Overwritten { + id: ProfileId, + discarded: Box, + }, + Failed(ReplicationFailure), + /// The replica refused to change `profile`, because the account may not. When + /// `restored`, the local profile is back to the last agreed version; otherwise it stays + /// local only and is not sent again until it changes. + Refused { + profile: Box, + restored: bool, + }, +} diff --git a/crates/connections-store/src/replication/tests.rs b/crates/connections-store/src/replication/tests.rs new file mode 100644 index 00000000..4afa2c01 --- /dev/null +++ b/crates/connections-store/src/replication/tests.rs @@ -0,0 +1,359 @@ +use super::*; +use gpui::AppContext as _; +use nocterm_connections_model::{ + profiles::{Profile, Profiles, Recents}, + replica::{RemoteBody, ReplicaCursor, ReplicaVersion, SyncedEntry}, + test_support::{MemoryReplica, ReplicaCall, profile_id_for}, +}; +use nocterm_session::{Auth, Target}; +use nocterm_store::test_support::{WriteLog, gated_writer}; +use std::sync::Mutex; + +mod conflicts; +mod feed; +mod lifecycle; + +fn profile(name: &str) -> Profile { + Profile { + id: profile_id_for(name), + name: name.into(), + description: String::new(), + options: Default::default(), + target: Target::new("ci", format!("{name}.example"), 22), + auth: Auth::Password, + credential: None, + launch: None, + icon: None, + icon_color: None, + country: None, + group: None, + jump_hosts: Vec::new(), + } +} + +fn at_host(profile: &Profile, host: &str) -> Profile { + let mut edited = profile.clone(); + edited.target.host = host.into(); + edited +} + +fn profiles_of(items: &[Profile]) -> Profiles { + let mut profiles = Profiles::default(); + for item in items { + profiles.upsert(item.clone()); + } + profiles +} + +/// A store and a replication over it, wired to one in-memory replica. +struct Fixture { + store: Entity, + replication: Entity, + replica: Arc, + events: Arc>>, + writes: WriteLog, +} + +impl Fixture { + fn new(cx: &mut gpui::TestAppContext, local: &[Profile]) -> Self { + Self::resumed( + cx, + local, + ReplicaState::default(), + Arc::new(MemoryReplica::new()), + ) + } + + /// A start with `state` already agreed, as after a restart. + fn resumed( + cx: &mut gpui::TestAppContext, + local: &[Profile], + state: ReplicaState, + replica: Arc, + ) -> Self { + let local = profiles_of(local); + let store = ConnectionStore::with_documents(local, Recents::default()); + Self::over(cx, store, state, replica) + } + + fn over( + cx: &mut gpui::TestAppContext, + store: ConnectionStore, + state: ReplicaState, + replica: Arc, + ) -> Self { + let store = cx.new(|_| store); + let (writer, release, writes) = gated_writer().into_parts(); + release.send(Ok(())).ok(); + let document = cx.new(|_| ProfileReplication::state_document(state, writer)); + let replication = { + let store = store.clone(); + cx.new(|cx| ProfileReplication::new(store, document, cx)) + }; + let events = Arc::new(Mutex::new(Vec::new())); + let seen = events.clone(); + cx.update(|cx| { + cx.subscribe(&replication, move |_, event: &ReplicationEvent, _| { + seen.lock().unwrap().push(event.clone()); + }) + .detach(); + }); + Self { + store, + replication, + replica, + events, + writes, + } + } + + fn attach(&self, cx: &mut gpui::TestAppContext) { + let replica = self.replica.clone(); + self.replication + .update(cx, |replication, cx| replication.attach(replica, cx)); + cx.run_until_parked(); + } + + async fn save(&self, cx: &mut gpui::TestAppContext, profile: Profile) { + self.store + .update(cx, |store, cx| store.save_profile(profile, cx)) + .await + .unwrap(); + cx.run_until_parked(); + } + + async fn delete(&self, cx: &mut gpui::TestAppContext, profile: &Profile) { + let id = profile.id; + self.store + .update(cx, |store, cx| store.delete_profile(id, cx)) + .await + .unwrap(); + cx.run_until_parked(); + } + + fn local(&self, cx: &gpui::TestAppContext, profile: &Profile) -> Option { + let id = profile.id; + self.store + .read_with(cx, |store, cx| store.profiles(cx).get(id).cloned()) + } + + fn status(&self, cx: &gpui::TestAppContext) -> ReplicationStatus { + self.replication + .read_with(cx, |replication, _| replication.status().clone()) + } + + fn events(&self) -> Vec { + self.events.lock().unwrap().clone() + } + + /// The last agreed state handed to the writer, as a restart would load it. + fn persisted_state(&self) -> ReplicaState { + self.writes + .lock() + .unwrap() + .last() + .cloned() + .unwrap_or_default() + } + + fn put_count(&self) -> usize { + self.replica + .writes() + .iter() + .filter(|call| matches!(call, ReplicaCall::Put { .. })) + .count() + } +} + +#[gpui::test] +async fn a_new_remote_profile_is_applied_locally_without_echoing_it_back( + cx: &mut gpui::TestAppContext, +) { + let fixture = Fixture::new(cx, &[]); + let remote = profile("remote"); + fixture.replica.write_remote(remote.clone()); + fixture.attach(cx); + assert_eq!(fixture.local(cx, &remote), Some(remote.clone())); + assert_eq!(fixture.replica.writes(), []); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); +} + +#[gpui::test] +async fn a_new_local_profile_is_sent_once_and_its_echo_is_ignored(cx: &mut gpui::TestAppContext) { + let local = profile("local"); + let fixture = Fixture::new(cx, std::slice::from_ref(&local)); + fixture.attach(cx); + assert_eq!( + fixture.replica.writes(), + [ReplicaCall::Put { + id: local.id, + expected: None + }] + ); + assert_eq!(fixture.replica.profile(local.id), Some(local.clone())); + let created = fixture.replica.stored(local.id).unwrap().0; + fixture.replica.write_remote(profile("other")); + cx.run_until_parked(); + assert_eq!(fixture.put_count(), 1); + assert_eq!( + fixture.persisted_state().entry(local.id).map(|e| e.version), + Some(created) + ); +} + +#[gpui::test] +async fn a_remote_edit_replaces_an_unchanged_local_profile(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let edited = at_host(&original, "moved.example"); + fixture.replica.write_remote(edited.clone()); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(edited)); + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.events().len(), 0); +} + +#[gpui::test] +async fn a_local_edit_is_sent_against_the_agreed_version(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let agreed = fixture.replica.stored(original.id).unwrap().0; + let edited = at_host(&original, "moved.example"); + fixture.save(cx, edited.clone()).await; + assert_eq!( + fixture.replica.writes().last(), + Some(&ReplicaCall::Put { + id: original.id, + expected: Some(agreed) + }) + ); + assert_eq!(fixture.replica.profile(original.id), Some(edited)); +} + +#[gpui::test] +async fn the_remote_wins_when_both_sides_changed_and_the_loss_is_reported( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + cx.run_until_parked(); + let local_edit = at_host(&original, "local.example"); + fixture.save(cx, local_edit.clone()).await; + let remote_edit = at_host(&original, "remote.example"); + fixture.replica.write_remote(remote_edit.clone()); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(remote_edit)); + let events = fixture.events(); + assert!(matches!( + events.as_slice(), + [ReplicationEvent::Overwritten { id, discarded }] + if *id == original.id && **discarded == local_edit + )); +} + +#[gpui::test] +async fn equal_edits_on_both_sides_are_agreed_without_a_write_or_an_event( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + let edited = at_host(&original, "same.example"); + fixture.save(cx, edited.clone()).await; + let version = fixture.replica.write_remote(edited); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.events().len(), 0); + assert_eq!( + fixture + .persisted_state() + .entry(original.id) + .map(|e| e.version), + Some(version) + ); +} + +#[gpui::test] +async fn a_local_deletion_leaves_a_tombstone_on_the_replica(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let agreed = fixture.replica.stored(original.id).unwrap().0; + fixture.delete(cx, &original).await; + assert_eq!( + fixture.replica.writes().last(), + Some(&ReplicaCall::Delete { + id: original.id, + expected: agreed + }) + ); + assert!(matches!( + fixture.replica.stored(original.id).map(|s| s.1), + Some(RemoteBody::Deleted) + )); + assert!(fixture.persisted_state().entry(original.id).is_none()); +} + +#[gpui::test] +async fn a_remote_tombstone_deletes_the_unchanged_local_profile(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.delete_remote(original.id); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), None); + assert_eq!(fixture.put_count(), 1); + assert!(fixture.persisted_state().entry(original.id).is_none()); +} + +#[gpui::test] +async fn a_remote_edit_restores_a_profile_deleted_locally_while_offline( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + fixture.delete(cx, &original).await; + let edited = at_host(&original, "remote.example"); + fixture.replica.write_remote(edited.clone()); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(edited)); + assert!( + fixture + .replica + .writes() + .iter() + .all(|call| !matches!(call, ReplicaCall::Delete { .. })) + ); +} + +#[gpui::test] +async fn a_local_edit_is_sent_over_a_remote_tombstone(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + let edited = at_host(&original, "local.example"); + fixture.save(cx, edited.clone()).await; + let tombstone = fixture.replica.delete_remote(original.id); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!( + fixture.replica.writes().last(), + Some(&ReplicaCall::Put { + id: original.id, + expected: Some(tombstone) + }) + ); + assert_eq!(fixture.replica.profile(original.id), Some(edited.clone())); + assert_eq!(fixture.local(cx, &original), Some(edited)); +} diff --git a/crates/connections-store/src/replication/tests/conflicts.rs b/crates/connections-store/src/replication/tests/conflicts.rs new file mode 100644 index 00000000..4e1f5855 --- /dev/null +++ b/crates/connections-store/src/replication/tests/conflicts.rs @@ -0,0 +1,258 @@ +use super::*; + +fn failed_with(status: ReplicationStatus) -> Option { + match status { + ReplicationStatus::Failed(failure) => Some(failure), + _ => None, + } +} + +#[gpui::test] +async fn a_write_that_lost_a_race_is_reconciled_against_the_change_it_missed( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let remote_edit = at_host(&original, "remote.example"); + fixture + .replica + .write_concurrently_before_next_write(remote_edit.clone()); + let local_edit = at_host(&original, "local.example"); + fixture.save(cx, local_edit.clone()).await; + assert_eq!(fixture.local(cx, &original), Some(remote_edit)); + assert_eq!(fixture.put_count(), 2); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Overwritten { discarded, .. }] if **discarded == local_edit + )); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); +} + +#[gpui::test] +async fn a_write_that_keeps_conflicting_is_reported_after_three_attempts( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.conflict_next_writes(10); + fixture.save(cx, at_host(&original, "local.example")).await; + assert_eq!(fixture.put_count(), 1 + 3); + let failure = failed_with(fixture.status(cx)); + assert!(matches!(failure, Some(ReplicationFailure::Conflicts))); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Failed(ReplicationFailure::Conflicts)] + )); +} + +#[gpui::test] +async fn an_unreadable_remote_profile_never_changes_the_local_one(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.write_unrecognized(original.id); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(original.clone())); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::NeedsNewerClient + )); + let edited = at_host(&original, "local.example"); + fixture.save(cx, edited.clone()).await; + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.local(cx, &original), Some(edited)); +} + +#[gpui::test] +async fn an_unreadable_profile_stays_unreadable_across_a_restart_and_clears_when_readable( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.write_unrecognized(original.id); + cx.run_until_parked(); + let restarted = Fixture::resumed( + cx, + std::slice::from_ref(&original), + fixture.persisted_state(), + fixture.replica.clone(), + ); + restarted.attach(cx); + assert!(matches!( + restarted.status(cx), + ReplicationStatus::NeedsNewerClient + )); + let readable = at_host(&original, "newer.example"); + restarted.replica.write_remote(readable.clone()); + cx.run_until_parked(); + assert!(matches!(restarted.status(cx), ReplicationStatus::Synced)); + assert_eq!(restarted.local(cx, &original), Some(readable)); +} + +#[gpui::test] +async fn an_unreachable_replica_leaves_local_work_alone_and_catches_up_when_back( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.replica.set_offline(true); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Offline)); + let added = profile("b"); + fixture.save(cx, added.clone()).await; + assert_eq!(fixture.local(cx, &added), Some(added.clone())); + assert_eq!(fixture.replica.writes(), []); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); + assert_eq!(fixture.replica.profile(original.id), Some(original)); + assert_eq!(fixture.replica.profile(added.id), Some(added)); + assert_eq!(fixture.events().len(), 0); +} + +#[gpui::test] +async fn a_locked_account_is_reported_apart_from_an_unreachable_server_and_resumes_when_opened( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.replica.set_locked(true); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Locked)); + let added = profile("b"); + fixture.save(cx, added.clone()).await; + assert_eq!(fixture.local(cx, &added), Some(added.clone())); + assert_eq!(fixture.replica.writes(), []); + assert!(matches!(fixture.status(cx), ReplicationStatus::Locked)); + fixture.replica.set_locked(false); + cx.run_until_parked(); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); + assert_eq!(fixture.replica.profile(original.id), Some(original)); + assert_eq!(fixture.replica.profile(added.id), Some(added)); + assert_eq!(fixture.events().len(), 0); +} + +#[gpui::test] +async fn attaching_again_after_the_keys_opened_synchronizes_a_locked_account( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.replica.set_locked(true); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Locked)); + fixture.replica.set_locked(false); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); + assert_eq!(fixture.replica.profile(original.id), Some(original)); +} + +#[gpui::test] +async fn a_forbidden_profile_is_reported_without_blocking_the_others( + cx: &mut gpui::TestAppContext, +) { + let (shared, own) = (profile("shared"), profile("own")); + let fixture = Fixture::new(cx, &[shared.clone(), own.clone()]); + fixture.replica.refuse_writes_of(shared.id); + fixture.attach(cx); + assert_eq!(fixture.replica.profile(own.id), Some(own)); + assert_eq!(fixture.replica.profile(shared.id), None); + assert_eq!(fixture.local(cx, &shared), Some(shared.clone())); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Refused { profile, restored: false }] + if profile.id == shared.id + )); +} + +#[gpui::test] +async fn a_remote_profile_the_local_document_refuses_is_reported_and_skipped( + cx: &mut gpui::TestAppContext, +) { + let fixture = Fixture::new(cx, &[]); + let mut oversized = profile("big"); + oversized.description = "x".repeat(70 * 1024); + let fine = profile("fine"); + fixture.replica.write_remote(oversized.clone()); + fixture.replica.write_remote(fine.clone()); + fixture.attach(cx); + assert_eq!(fixture.local(cx, &oversized), None); + assert_eq!(fixture.local(cx, &fine), Some(fine)); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Failed(ReplicationFailure::Rejected { id, .. })] if *id == oversized.id + )); +} + +/// A remote edit of `original` that the local document refuses for its size. +fn oversized_edit(original: &Profile) -> Profile { + let mut edited = original.clone(); + edited.description = "x".repeat(70 * 1024); + edited +} + +fn rejected_ids(events: &[ReplicationEvent]) -> Vec { + events + .iter() + .filter_map(|event| match event { + ReplicationEvent::Failed(ReplicationFailure::Rejected { id, .. }) => Some(*id), + _ => None, + }) + .collect() +} + +#[gpui::test] +async fn a_remote_change_the_local_document_refuses_keeps_the_cursor_behind_it_and_is_reported_once( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let before = fixture.persisted_state().cursor().cloned(); + assert!(before.is_some(), "the first pass recorded a position"); + fixture.replica.write_remote(oversized_edit(&original)); + cx.run_until_parked(); + assert_eq!(rejected_ids(&fixture.events()), [original.id]); + assert_eq!(fixture.persisted_state().cursor().cloned(), before); + let other = profile("b"); + fixture.replica.write_remote(other.clone()); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &other), Some(other)); + assert_eq!(fixture.local(cx, &original), Some(original.clone())); + assert_eq!( + rejected_ids(&fixture.events()), + [original.id], + "the refused change is read again by every pass but reported once" + ); + assert_eq!(fixture.persisted_state().cursor().cloned(), before); +} + +#[gpui::test] +async fn a_local_edit_after_a_refused_remote_change_does_not_end_in_conflicts( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.write_remote(oversized_edit(&original)); + cx.run_until_parked(); + let puts = fixture.put_count(); + let edited = at_host(&original, "local.example"); + fixture.save(cx, edited.clone()).await; + assert_eq!(fixture.local(cx, &original), Some(edited)); + assert_eq!( + fixture.put_count(), + puts, + "nothing is sent over a newer version" + ); + assert!( + !matches!( + failed_with(fixture.status(cx)), + Some(ReplicationFailure::Conflicts) + ), + "the edit is not stuck retrying against the missed change" + ); +} diff --git a/crates/connections-store/src/replication/tests/feed.rs b/crates/connections-store/src/replication/tests/feed.rs new file mode 100644 index 00000000..103c6998 --- /dev/null +++ b/crates/connections-store/src/replication/tests/feed.rs @@ -0,0 +1,165 @@ +//! Malformed or excessive feeds never produce a partial plan or advance the cursor. +use super::*; +use nocterm_connections_model::replica::{RemoteProfile, ReplicaPage}; +use std::time::Duration; + +fn page(cursor: &str, profiles: Vec, more: bool) -> ReplicaPage { + ReplicaPage { + changes: profiles + .into_iter() + .map(|profile| RemoteProfile { + id: profile.id, + version: ReplicaVersion::new(1), + body: RemoteBody::Present(Box::new(profile)), + }) + .collect(), + cursor: ReplicaCursor::new(cursor), + more, + } +} +fn unchanged(fixture: &Fixture, candidate: &Profile, cx: &gpui::TestAppContext) { + assert!(fixture.local(cx, candidate).is_none()); + assert!(fixture.replica.writes().is_empty()); + assert!(fixture.persisted_state().cursor().is_none()); +} + +#[gpui::test] +fn a_feed_that_stalls_with_more_pages_is_rejected_before_any_profile_changes( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture.replica.script_pages(vec![ + page("first", vec![candidate.clone()], true), + page("first", vec![], true), + ]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::NonProgress)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn a_feed_cycle_is_refused_even_when_its_last_page_claims_completion( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture.replica.script_pages(vec![ + page("first", vec![candidate.clone()], true), + page("second", vec![], true), + page("first", vec![], false), + ]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Cycle)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn excessive_pages_stop_requesting_the_feed_without_advancing_the_cursor( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + let mut pages = vec![page("first", vec![candidate.clone()], true)]; + pages.extend((1..=256).map(|index| page(&format!("cursor-{index}"), vec![], true))); + fixture.replica.script_pages(pages); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Pages)) + )); + assert_eq!(fixture.replica.calls().len(), 256); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn excessive_change_counts_are_refused_before_planning(cx: &mut gpui::TestAppContext) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + let change = RemoteProfile { + id: candidate.id, + version: ReplicaVersion::new(1), + body: RemoteBody::Deleted, + }; + fixture.replica.script_pages(vec![ReplicaPage { + changes: vec![change; 16_385], + cursor: ReplicaCursor::new("end"), + more: false, + }]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Changes)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn the_payload_budget_applies_across_pages_before_any_remote_or_local_effect( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + let mut large = profile("large"); + large.description = "x".repeat(9 * 1024 * 1024); + let mut second = profile("second"); + second.description = "y".repeat(9 * 1024 * 1024); + fixture.replica.script_pages(vec![ + page("first", vec![candidate.clone(), large], true), + page("second", vec![second], false), + ]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Bytes)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn an_oversized_cursor_never_becomes_the_saved_replication_position(cx: &mut gpui::TestAppContext) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture.replica.script_pages(vec![page( + &"x".repeat(4097), + vec![candidate.clone()], + false, + )]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::CursorSize)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn a_stalled_feed_has_a_complete_acquisition_deadline_and_cancels_its_reader( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture + .replica + .script_pages(vec![page("end", vec![candidate.clone()], false)]); + let release = fixture.replica.hold_next_read(); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Syncing)); + cx.executor().advance_clock(Duration::from_secs(121)); + cx.run_until_parked(); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::TimedOut)) + )); + assert!( + release.send(()).is_err(), + "the original request was dropped" + ); + unchanged(&fixture, &candidate, cx); +} diff --git a/crates/connections-store/src/replication/tests/lifecycle.rs b/crates/connections-store/src/replication/tests/lifecycle.rs new file mode 100644 index 00000000..3f8f646c --- /dev/null +++ b/crates/connections-store/src/replication/tests/lifecycle.rs @@ -0,0 +1,155 @@ +use super::*; +use nocterm_connections_model::repository::{ + ConnectionDocument, ConnectionStorageError, StorageOperation, +}; + +#[gpui::test] +async fn a_restart_continues_from_the_persisted_state_without_resending( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let saved = fixture.persisted_state(); + let cursor = saved.cursor().cloned().expect("a cursor was kept"); + let added = profile("added"); + fixture.replica.write_remote(added.clone()); + let restarted = Fixture::resumed( + cx, + std::slice::from_ref(&original), + saved, + fixture.replica.clone(), + ); + restarted.attach(cx); + assert_eq!(restarted.local(cx, &added), Some(added)); + assert_eq!(restarted.put_count(), 1); + assert!( + restarted + .replica + .calls() + .contains(&ReplicaCall::Changes(Some(cursor.as_str().to_owned()))) + ); +} + +impl Fixture { + fn detach(&self, cx: &mut gpui::TestAppContext) { + self.replication + .update(cx, |replication, cx| replication.detach(cx)); + cx.run_until_parked(); + } +} + +#[gpui::test] +async fn detaching_stops_synchronizing_and_forgets_the_agreement(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.detach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Detached)); + let local = profile("local"); + fixture.save(cx, local.clone()).await; + fixture.replica.write_remote(profile("remote")); + cx.run_until_parked(); + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.local(cx, &profile("remote")), None); + assert!(fixture.replica.profile(local.id).is_none()); + assert!(fixture.persisted_state().entry(original.id).is_none()); +} + +#[gpui::test] +async fn detaching_empties_the_mirror_but_not_the_replica(cx: &mut gpui::TestAppContext) { + let (agreed, edited) = (profile("agreed"), profile("edited")); + let fixture = Fixture::new(cx, &[agreed.clone(), edited.clone()]); + fixture.attach(cx); + fixture.replica.set_offline(true); + fixture.save(cx, at_host(&edited, "unsent.example")).await; + fixture.detach(cx); + assert_eq!(fixture.local(cx, &agreed), None); + assert_eq!(fixture.local(cx, &edited), None); + assert_eq!(fixture.replica.profile(agreed.id), Some(agreed)); +} + +#[gpui::test] +async fn attaching_again_after_detaching_shows_the_account_afresh(cx: &mut gpui::TestAppContext) { + let agreed = profile("agreed"); + let fixture = Fixture::new(cx, std::slice::from_ref(&agreed)); + fixture.attach(cx); + fixture.detach(cx); + assert_eq!(fixture.local(cx, &agreed), None); + fixture.attach(cx); + assert_eq!(fixture.local(cx, &agreed), Some(agreed)); +} + +#[gpui::test] +async fn attaching_another_replica_moves_synchronization_to_it(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.detach(cx); + let second = Arc::new(MemoryReplica::new()); + fixture + .replication + .update(cx, |replication, cx| replication.attach(second.clone(), cx)); + cx.run_until_parked(); + // What was agreed with the first replica belongs to it, not to the second. + assert_eq!(second.profile(original.id), None); + assert_eq!(fixture.local(cx, &original), None); + let added = profile("added"); + fixture.save(cx, added.clone()).await; + assert_eq!(second.profile(added.id), Some(added)); + assert_eq!(fixture.put_count(), 1); +} + +#[gpui::test] +async fn changes_are_read_page_by_page_until_none_remain(cx: &mut gpui::TestAppContext) { + let fixture = Fixture::new(cx, &[]); + let remote: Vec = ["a", "b", "c"].iter().map(|name| profile(name)).collect(); + for profile in &remote { + fixture.replica.write_remote(profile.clone()); + } + fixture.replica.page_size(1); + fixture.attach(cx); + for profile in &remote { + assert_eq!(fixture.local(cx, profile), Some(profile.clone())); + } + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); +} + +#[gpui::test] +async fn nothing_is_synchronized_while_the_local_document_failed_to_load( + cx: &mut gpui::TestAppContext, +) { + let kept = profile("kept"); + let mut state = ReplicaState::default(); + state.agree( + kept.id, + SyncedEntry { + version: ReplicaVersion::new(1), + profile: kept.clone(), + }, + ); + state.advance(ReplicaCursor::new("1")); + let replica = Arc::new(MemoryReplica::new()); + replica.write_remote(kept.clone()); + let unreadable = ConnectionStore::prepared( + Profiles::default(), + Recents::default(), + Arc::new(|_| Box::pin(async { Ok(()) })), + Arc::new(|_| Box::pin(async { Ok(()) })), + Some(Arc::new(ConnectionStorageError::new( + ConnectionDocument::Profiles, + StorageOperation::Read, + std::io::Error::from(std::io::ErrorKind::PermissionDenied), + ))), + Arc::new(nocterm_connections_model::value_ports::SuppliedValues::new( + 1, 0, + )), + ); + let fixture = Fixture::over(cx, unreadable, state, replica); + fixture.attach(cx); + assert_eq!(fixture.replica.writes(), []); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::LocalUnavailable) + )); +} diff --git a/crates/connections-store/src/test_support.rs b/crates/connections-store/src/test_support.rs new file mode 100644 index 00000000..feb24dfc --- /dev/null +++ b/crates/connections-store/src/test_support.rs @@ -0,0 +1,31 @@ +//! Controlled foreground source authority for consumers of captured probe leases. +use crate::{ProbeSourceAuthority, ProbeSourceIdentity, ProbeSourceLease}; +use gpui::App; +use std::{cell::Cell, rc::Rc}; +#[derive(Clone)] +pub struct ScriptedProbeSource { + identity: ProbeSourceIdentity, + current: Rc>, +} +impl ScriptedProbeSource { + pub fn new(identity: ProbeSourceIdentity) -> Self { + Self { + identity, + current: Rc::new(Cell::new(true)), + } + } + pub fn lease(&self) -> ProbeSourceLease { + ProbeSourceLease::new(self.identity, Rc::new(self.clone())) + } + pub fn set_current(&self, current: bool) { + self.current.set(current); + } +} +impl ProbeSourceAuthority for ScriptedProbeSource { + fn is_current(&self, _: &App) -> bool { + self.current.get() + } + fn same_session_alive(&self, _: &App) -> bool { + self.current.get() + } +} diff --git a/crates/connections/Cargo.toml b/crates/connections/Cargo.toml index d0090d24..281652a7 100644 --- a/crates/connections/Cargo.toml +++ b/crates/connections/Cargo.toml @@ -16,18 +16,21 @@ nocterm-plugin-model.workspace = true nocterm-project-model.workspace = true nocterm-store.workspace = true nocterm-session.workspace = true +nocterm-ssh-keys.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true nocterm-workspace-ports.workspace = true +futures.workspace = true gpui-kit.workspace = true serde_json.workspace = true +thiserror.workspace = true [dev-dependencies] nocterm-workspace = { workspace = true, features = ["test-support"] } toml.workspace = true -futures.workspace = true nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } nocterm-connections-model = { workspace = true, features = ["test-support"] } gpui-kit = { workspace = true, features = ["test-support"] } tempfile.workspace = true diff --git a/crates/connections/src/agent_access.rs b/crates/connections/src/agent_access.rs new file mode 100644 index 00000000..0220bc2a --- /dev/null +++ b/crates/connections/src/agent_access.rs @@ -0,0 +1,102 @@ +//! The port through which a hub window shows and changes whether the hub's agents may work on a +//! saved server (ADR 0148): the editor's Agent access switch and the state on the server's row. +//! The composition root implements it over the hub agents service. +use gpui_kit::{App, SharedString, Subscription, Task}; +use nocterm_connections_model::profiles::Profile; + +/// The saved server whose agent access is shown and changed. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AgentServer { + /// The id of the saved connection. + pub connection: String, + pub name: String, + pub host: String, + pub port: u16, + pub user: String, +} + +impl AgentServer { + /// The saved connection `profile`, as the hub's agent servers are matched against it. + pub(crate) fn of(profile: &Profile) -> Self { + Self { + connection: profile.id.to_string(), + name: profile.name.clone(), + host: profile.target.host.clone(), + port: profile.target.port, + user: profile.target.user.clone(), + } + } +} + +/// What the hub's agents last found on a server they may sign in to. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AgentAccessState { + /// Recorded on the hub; agents have not signed in yet. + Unchecked, + /// Agents sign in. + Ready, + /// The server refuses the agent key. + NoAccess, + /// The server presents another host key than the recorded one. + HostKeyChanged, + /// The server could not be reached. + Unreachable, + /// The agent key was replaced after its line was written: the server needs the new one. + Reconnect, +} + +impl AgentAccessState { + /// The short text of the server's row. + pub fn label(self) -> &'static str { + match self { + Self::Unchecked => "Agents", + Self::Ready => "Agents ✓", + Self::NoAccess => "No access", + Self::HostKeyChanged => "Host key changed", + Self::Unreachable => "Unreachable", + Self::Reconnect => "Reconnect", + } + } + + /// Whether nothing needs the user's attention. + pub fn is_ready(self) -> bool { + matches!(self, Self::Ready | Self::Unchecked) + } +} + +/// Why agent access did not change, in words the editor shows. +#[derive(Clone, Debug, thiserror::Error)] +#[error("{reason}")] +pub struct AgentAccessRefused { + reason: SharedString, +} + +impl AgentAccessRefused { + pub fn new(reason: impl Into) -> Self { + Self { + reason: reason.into(), + } + } + + pub fn reason(&self) -> &SharedString { + &self.reason + } +} + +/// Whether the agents of the window's hub may sign in to a saved server, and the switch for it. +pub trait ServerAgentAccess: Send + Sync { + /// The state of `server` among the hub's agent servers, as last listed; none while the hub + /// does not record it. + fn state(&self, server: &AgentServer, cx: &App) -> Option; + /// Whether a session to `server` is open, so the agent key's line can be written through it. + fn session_open(&self, server: &AgentServer, cx: &App) -> bool; + /// Grants or withdraws access; the task ends once the hub holds the new state. + fn set( + &self, + server: AgentServer, + granted: bool, + cx: &mut App, + ) -> Task>; + /// Calls `changed` whenever the hub's agent servers may have changed. + fn observe(&self, changed: Box, cx: &mut App) -> Subscription; +} diff --git a/crates/connections/src/directory.rs b/crates/connections/src/directory.rs index c67e6c00..bdaa3857 100644 --- a/crates/connections/src/directory.rs +++ b/crates/connections/src/directory.rs @@ -6,8 +6,7 @@ use std::collections::HashSet; use crate::{ Installed, - session_intents::{open_profile, report_route_error}, - spec_for_profile, + session_intents::{open_background, open_profile}, }; pub(crate) struct Directory { @@ -78,20 +77,7 @@ impl ConnectionDirectory for Directory { cx: &mut App, ) -> Option { let profile = self.profile(id, cx)?; - let spec = spec_for_profile(&profile, self.installed.connections().read(cx).profiles(cx)); - let spec = match spec { - Ok(spec) => spec, - Err(error) => { - report_route_error(&error, window, cx); - return None; - } - }; - workspace - .update(cx, |workspace, cx| { - workspace.open_background_session(spec, window, cx) - }) - .ok() - .flatten() + open_background(&self.installed, workspace, &profile, window, cx) } } diff --git a/crates/connections/src/editor.rs b/crates/connections/src/editor.rs index e429c771..d9df9ee2 100644 --- a/crates/connections/src/editor.rs +++ b/crates/connections/src/editor.rs @@ -1,7 +1,8 @@ //! The form that creates and edits a saved connection. use gpui_kit::{ - App, Context, Entity, FocusHandle, Focusable, SharedString, Subscription, WeakEntity, Window, + App, Context, Entity, FocusHandle, Focusable, ScrollHandle, SharedString, Subscription, + WeakEntity, Window, component::{ ActiveTheme as _, Disableable as _, Sizable as _, StyledExt as _, WindowExt as _, button::{Button, ButtonVariants as _}, @@ -18,7 +19,9 @@ use nocterm_connections_model::{jump_route::resolve_jump_route, profiles::Profil use nocterm_ui::ActiveDesign as _; use nocterm_workspace::Workspace; -use crate::{Installed, session_intents::open_profile}; +use crate::{Installed, PasswordNotKept, session_intents::open_profile}; +use futures::future::BoxFuture; +use nocterm_session::{Auth, CredentialId}; /// Two rows of icon tiles: 28 px tiles with a 4 px gap. const ICON_ROWS_HEIGHT: f32 = 60.; @@ -50,18 +53,23 @@ impl EditorSection { ]; } +use agent_access::AgentAccessField; use appearance::AppearanceFields; use authentication::AuthenticationFields; use connection::ConnectionFields; use jump_hosts::JumpHostsFields; use launch::LaunchFields; -use nocterm_connections_model::draft::{AuthKind, ProfileDraft, ValidationField}; +use nocterm_connections_model::draft::{AuthKind, ProfileDraft, ValidationField, expand_home}; use state::EditState; +use vault_key::VaultKeyEvent; +mod agent_access; mod authentication; mod connection; mod jump_hosts; mod launch; +mod password; mod state; +mod vault_key; #[derive(Debug)] struct FormError { @@ -86,6 +94,10 @@ pub fn open_editor( window: &mut Window, cx: &mut App, ) { + // A change the account does not receive would be lost. + if !crate::panel::account_state::AccountState::of(&installed, cx).editable() { + return; + } let _ = open_editor_view(installed, profile, workspace, window, cx); } fn open_editor_view( @@ -148,7 +160,10 @@ pub struct ConnectionEditor { appearance: AppearanceFields, jump_hosts: JumpHostsFields, options: Entity, + agent_access: AgentAccessField, section: EditorSection, + /// The page's scroll, to bring a row that just appeared into view. + scroll: ScrollHandle, error: Option, _subscriptions: Vec, } @@ -194,7 +209,13 @@ impl ConnectionEditor { .map_or_else(|| inputs.next_id(), |profile| profile.id); let existing = profile.as_ref(); let connection = ConnectionFields::new(existing, inputs.defaults(), window, cx); - let authentication = AuthenticationFields::new(existing, window, cx); + let authentication = AuthenticationFields::new( + existing, + inputs.keys().clone(), + inputs.passwords().clone(), + window, + cx, + ); let launch = LaunchFields::new(existing, window, cx); let appearance = AppearanceFields::new(existing, &installed, window, cx); let choices = JumpHostsFields::choices(id, &installed, cx); @@ -207,8 +228,10 @@ impl ConnectionEditor { cx, ) }); - let subscription = cx.observe(installed.facts(), |_, _, cx| cx.notify()); - Self { + let agent_access = AgentAccessField::new(inputs.agent_access().cloned()); + let mut subscriptions = vec![cx.observe(installed.facts(), |_, _, cx| cx.notify())]; + subscriptions.extend(agent_access.observe(cx)); + let editor = Self { deps: installed, focus: cx.focus_handle(), edit: EditState::new(id, profile), @@ -219,10 +242,16 @@ impl ConnectionEditor { appearance, jump_hosts, options, + agent_access, section: EditorSection::Connection, + scroll: ScrollHandle::new(), error: None, - _subscriptions: vec![subscription], + _subscriptions: subscriptions, + }; + if editor.authentication.kind() == AuthKind::VaultKey { + editor.refresh_vault_keys(window, cx); } + editor } fn fields(&self, cx: &App) -> ProfileDraft { let (metadata, endpoint) = self.connection.snapshot(cx); @@ -261,12 +290,13 @@ impl ConnectionEditor { } } - /// Saves the connection, and opens it when `and_connect` is set. + /// Saves the connection, and opens it when `and_connect` is set. A typed password is kept + /// in the window's vault first, and the connection names the record it went to. fn save(&mut self, and_connect: bool, window: &mut Window, cx: &mut Context) { if !self.edit.try_begin() { return; } - let profile = match self.validated_profile(cx) { + let mut profile = match self.validated_profile(cx) { Ok(profile) => profile, Err(error) => { if self.edit.finish() { @@ -276,13 +306,32 @@ impl ConnectionEditor { return; } }; - - let saved = self.deps.connections().update(cx, |connections, cx| { - connections.save_profile_checked(profile.clone(), self.edit.original().cloned(), cx) - }); + let original = self.edit.original().cloned(); + let kept = self.kept_password(&profile, original.as_ref(), cx); self.error = None; cx.notify(); cx.spawn_in(window, async move |this, cx| { + if let Some(kept) = kept { + match kept.await { + Ok(credential) => profile.credential = Some(credential), + Err(error) => { + let _ = this.update(cx, |this, cx| { + if this.edit.finish() { + this.error = Some(error.to_string().into()); + cx.notify(); + } + }); + return; + } + } + } + let Ok(saved) = this.update(cx, |this, cx| { + this.deps.connections().update(cx, |connections, cx| { + connections.save_profile_checked(profile.clone(), original, cx) + }) + }) else { + return; + }; let result = saved.await; let _ = this.update_in(cx, |this, window, cx| { if !this.edit.finish() { @@ -307,6 +356,33 @@ impl ConnectionEditor { .detach(); } + /// What the vault does with the connection's password before it is saved: a typed one + /// becomes the new password of the chosen record (every server it signs in to takes it) + /// or of a new one; a chosen saved password gains the address when the connection is new + /// to it. Resolves to the record the connection names; none when nothing changes. + fn kept_password( + &self, + profile: &Profile, + original: Option<&Profile>, + cx: &App, + ) -> Option>> { + let passwords = self.deps.inputs().passwords(); + let target = profile.target.clone(); + if let Some(password) = self.authentication.typed_password(cx) { + let kept = passwords.keep(profile.credential, profile.name.clone(), target, password); + return Some(kept); + } + let chosen = profile.credential?; + let unchanged = original.is_some_and(|original| { + original.credential == Some(chosen) && original.target == profile.target + }); + if unchanged || !matches!(profile.auth, Auth::Auto | Auth::Password) { + return None; + } + let attached = passwords.attach(chosen, target); + Some(Box::pin(async move { attached.await.map(|()| chosen) })) + } + fn validated_profile(&self, cx: &App) -> Result { let mut fields = self.fields(cx); fields.options = self @@ -330,17 +406,84 @@ impl ConnectionEditor { fn set_auth(&mut self, auth: AuthKind, window: &mut Window, cx: &mut Context) { self.authentication.select(auth); self.error = None; - if auth == AuthKind::Key { - self.authentication + match auth { + AuthKind::Key => self + .authentication .key_path() - .update(cx, |input, cx| input.focus(window, cx)); + .update(cx, |input, cx| input.focus(window, cx)), + AuthKind::VaultKey => self.refresh_vault_keys(window, cx), + AuthKind::Auto | AuthKind::Password => {} + } + cx.notify(); + } + + fn refresh_vault_keys(&self, window: &mut Window, cx: &mut Context) { + self.authentication + .vault_key() + .update(cx, |field, cx| field.refresh(window, cx)); + } + + /// What a vault key operation came to: a chosen key signs the connection + /// in, a failure is the error the editor shows. + fn on_vault_key(&mut self, event: &VaultKeyEvent, cx: &mut Context) { + match event { + VaultKeyEvent::Chosen => { + self.authentication.select(AuthKind::VaultKey); + self.error = None; + } + VaultKeyEvent::PassphraseAsked => self.scroll.scroll_to_bottom(), + VaultKeyEvent::Failed(message) => self.error = Some(message.clone().into()), + } + cx.notify(); + } + + /// The name a key created or imported here gets when none is typed. + fn key_label(&self, cx: &App) -> String { + let (metadata, endpoint) = self.connection.snapshot(cx); + [metadata.name, endpoint.host] + .into_iter() + .map(|text| text.trim().to_owned()) + .find(|text| !text.is_empty()) + .unwrap_or_else(|| "SSH key".to_owned()) + } + + fn create_vault_key(&mut self, window: &mut Window, cx: &mut Context) { + let label = self.key_label(cx); + self.error = None; + self.authentication + .vault_key() + .update(cx, |field, cx| field.create(&label, window, cx)); + cx.notify(); + } + + /// Stores the key file named in the editor in the vault. Both "Import" + /// and "Move to vault" end with the connection using the stored copy. + fn import_vault_key(&mut self, window: &mut Window, cx: &mut Context) { + let typed = self.authentication.snapshot(cx).key_path; + let typed = typed.trim(); + if typed.is_empty() { + self.show_validation( + FormError::new( + ValidationField::Key, + "Enter the path of the key file to import.", + ), + window, + cx, + ); + return; } + let path = expand_home(typed, self.deps.inputs().defaults().home.as_deref()); + let label = self.key_label(cx); + self.error = None; + self.authentication + .vault_key() + .update(cx, |field, cx| field.import(&label, path, window, cx)); cx.notify(); } fn show_validation(&mut self, error: FormError, window: &mut Window, cx: &mut Context) { let section = match error.field { - ValidationField::Key | ValidationField::Credential => EditorSection::Authentication, + ValidationField::Key => EditorSection::Authentication, ValidationField::Arguments | ValidationField::Environment | ValidationField::Launch => { EditorSection::Launch } @@ -354,7 +497,6 @@ impl ConnectionEditor { ValidationField::Port => Some(self.connection.port()), ValidationField::User => Some(self.connection.user()), ValidationField::Key => Some(self.authentication.key_path()), - ValidationField::Credential => Some(self.authentication.credential()), ValidationField::Arguments => Some(&self.launch.inputs()[1]), ValidationField::Environment => Some(&self.launch.inputs()[3]), ValidationField::Launch => Some(&self.launch.inputs()[0]), @@ -465,9 +607,10 @@ impl ConnectionEditor { self.section = section; let focus = match section { EditorSection::Connection => self.focus_handle(cx), - EditorSection::Authentication => { - self.authentication.credential().read(cx).focus_handle(cx) - } + EditorSection::Authentication => self.authentication.first_input(cx).map_or_else( + || self.focus.clone(), + |input| input.read(cx).focus_handle(cx), + ), EditorSection::JumpHosts => self.jump_hosts.address().read(cx).focus_handle(cx), EditorSection::Session => self.options.read(cx).focus_handle(cx), EditorSection::Launch if self.launch.overridden() => { diff --git a/crates/connections/src/editor/agent_access.rs b/crates/connections/src/editor/agent_access.rs new file mode 100644 index 00000000..b2de9cc3 --- /dev/null +++ b/crates/connections/src/editor/agent_access.rs @@ -0,0 +1,216 @@ +//! Agent access of the saved connection being edited, offered in hub windows only. +//! +//! Turning it on writes the agent key's line through a session to the server: the one the user +//! has open, or else one opened without a tab with the connection's own saved credentials, the +//! way the sidebar opens it. A session that asks the user to sign in is shown in its tab. +mod session; + +use crate::agent_access::{AgentAccessRefused, AgentServer, ServerAgentAccess}; +use gpui_kit::{ + AnyElement, Context, SharedString, Subscription, Task, Window, + base::TestSupportExt as _, + component::{ActiveTheme as _, Disableable as _, switch::Switch}, + div, + prelude::*, +}; +use nocterm_connections_model::profiles::Profile; +use nocterm_ui::form; +use session::{BackgroundSession, POLL, Step}; +use std::sync::Arc; + +use super::ConnectionEditor; + +const DESCRIPTION: &str = "Lets this hub's agents sign in with the account's agent key. Turning it on adds the key to the server's authorized_keys, connecting with the saved credentials when no session is open."; + +/// What a change of agent access is doing. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Progress { + /// A session to the server is being opened with its saved credentials. + Connecting, + /// The opened session waits for the user to sign in, in its tab. + SigningIn, + /// The server and the hub are being changed. + Changing, +} + +impl Progress { + fn note(self) -> Option<&'static str> { + match self { + Self::Connecting => Some("Connecting with the saved credentials…"), + Self::SigningIn => { + Some("The server asks you to sign in: finish in its tab, then access is added.") + } + Self::Changing => None, + } + } +} + +pub(super) struct AgentAccessField { + port: Option>, + /// What a change is doing; none while nothing changes. A change runs to its end even if the + /// editor closes, so a written key line is always recorded on the hub. + progress: Option, + problem: Option, +} + +impl AgentAccessField { + pub(super) fn new(port: Option>) -> Self { + Self { + port, + progress: None, + problem: None, + } + } + + /// Renders the editor again whenever the hub's agent servers change. + pub(super) fn observe(&self, cx: &mut Context) -> Option { + let editor = cx.weak_entity(); + let changed = move |cx: &mut gpui_kit::App| { + let _ = editor.update(cx, |_, cx| cx.notify()); + }; + Some(self.port.as_ref()?.observe(Box::new(changed), cx)) + } + + /// The row for the saved connection `profile`; none outside hub windows or before the + /// connection is saved. + pub(super) fn row( + &self, + profile: Option<&Profile>, + cx: &mut Context, + ) -> Option { + let (port, profile) = (self.port.as_ref()?, profile?.clone()); + let granted = port.state(&AgentServer::of(&profile), cx).is_some(); + let switch = Switch::new("profile-agent-access") + .checked(granted) + .disabled(self.progress.is_some()) + .on_click(cx.listener(move |editor, checked: &bool, window, cx| { + Self::change(editor, &profile, *checked, window, cx); + })); + let row = form::row("Agent access", DESCRIPTION, switch, cx); + let note = match (&self.problem, self.progress.and_then(Progress::note)) { + (Some(problem), _) => Some(("profile-agent-access-problem", problem.clone(), true)), + (None, Some(note)) => Some(("profile-agent-access-progress", note.into(), false)), + (None, None) => None, + }; + Some(match note { + Some((id, text, problem)) => { + let text = if problem { + form::error_text(text, cx).into_any_element() + } else { + div() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(text) + .into_any_element() + }; + div() + .id(id) + .test_support() + .child(row) + .child(text) + .into_any_element() + } + None => row, + }) + } + + /// Grants or withdraws access for `profile`. Granting without an open session first opens + /// one with the connection's saved credentials. + fn change( + editor: &mut ConnectionEditor, + profile: &Profile, + granted: bool, + window: &mut Window, + cx: &mut Context, + ) { + let field = &mut editor.agent_access; + let Some(port) = field.port.clone().filter(|_| field.progress.is_none()) else { + return; + }; + let server = AgentServer::of(profile); + field.problem = None; + if !granted || port.session_open(&server, cx) { + field.progress = Some(Progress::Changing); + let change = port.set(server, granted, cx); + cx.spawn(async move |editor, cx| { + let result = change.await; + let _ = editor.update(cx, |editor, cx| editor.agent_access.finish(result, cx)); + }) + .detach(); + } else { + match BackgroundSession::open(&editor.deps, &editor.workspace, profile, window, cx) { + Some(session) => { + field.progress = Some(Progress::Connecting); + connect_then_grant(port, server, session, window, cx).detach(); + } + None => field.problem = Some("The server could not be opened.".into()), + } + } + cx.notify(); + } + + fn report(&mut self, progress: Progress, cx: &mut Context) { + if self.progress != Some(progress) { + self.progress = Some(progress); + cx.notify(); + } + } + + fn finish( + &mut self, + result: Result<(), AgentAccessRefused>, + cx: &mut Context, + ) { + self.progress = None; + self.problem = result.err().map(|refused| refused.reason().clone()); + cx.notify(); + } +} + +/// Waits for `session` to come up, then grants access through it and closes it, unless the user +/// was shown it to sign in. The editor hears how it goes while it is open. +fn connect_then_grant( + port: Arc, + server: AgentServer, + mut session: BackgroundSession, + window: &mut Window, + cx: &mut Context, +) -> Task<()> { + cx.spawn_in(window, async move |editor, cx| { + let connected = loop { + cx.background_executor().timer(POLL).await; + let step = cx.update(|window, cx| { + let open = port.session_open(&server, cx); + session.poll(open, window, cx) + }); + match step { + Ok(Step::Waiting { signing_in }) => { + let progress = if signing_in { + Progress::SigningIn + } else { + Progress::Connecting + }; + let _ = + editor.update(cx, |editor, cx| editor.agent_access.report(progress, cx)); + } + Ok(Step::Connected) => break Ok(()), + Ok(Step::Failed(reason)) => break Err(AgentAccessRefused::new(reason)), + Err(_) => return, + } + }; + let result = match connected { + Ok(()) => { + let _ = editor.update(cx, |editor, cx| { + editor.agent_access.report(Progress::Changing, cx); + }); + match cx.update(|_, cx| port.set(server, true, cx)) { + Ok(change) => change.await, + Err(_) => return, + } + } + Err(refused) => Err(refused), + }; + let _ = cx.update(|window, cx| session.close(window, cx)); + let _ = editor.update(cx, |editor, cx| editor.agent_access.finish(result, cx)); + }) +} diff --git a/crates/connections/src/editor/agent_access/session.rs b/crates/connections/src/editor/agent_access/session.rs new file mode 100644 index 00000000..e7f0d586 --- /dev/null +++ b/crates/connections/src/editor/agent_access/session.rs @@ -0,0 +1,112 @@ +//! A session opened without a tab so the agent key's line can be written through it, and the +//! wait until it is up. +use crate::Installed; +use gpui_kit::{App, EntityId, SharedString, WeakEntity, Window}; +use nocterm_connections_model::profiles::Profile; +use nocterm_workspace::{TerminalStatus, Workspace}; +use std::time::Duration; + +/// How often the session is looked at while it comes up. +pub(super) const POLL: Duration = Duration::from_millis(250); +/// Looks before an unattended sign-in gives up: one minute. +const CONNECT_POLLS: u32 = 240; +/// Looks before a sign-in the user was asked for gives up: three minutes. +const SIGN_IN_POLLS: u32 = 720; + +/// Where the wait for the session stands. +#[derive(Debug, PartialEq, Eq)] +pub(super) enum Step { + /// Not up yet; `signing_in` once the user was asked to sign in. + Waiting { + signing_in: bool, + }, + /// The server can be written to. + Connected, + Failed(SharedString), +} + +pub(super) struct BackgroundSession { + workspace: WeakEntity, + item: EntityId, + /// The session asked the user to sign in and was shown in its tab; it stays open then. + shown: bool, + polls: u32, +} + +impl BackgroundSession { + /// Opens `profile` without a tab, signing in with its saved credentials. + pub(super) fn open( + installed: &Installed, + workspace: &WeakEntity, + profile: &Profile, + window: &mut Window, + cx: &mut App, + ) -> Option { + let item = + crate::session_intents::open_background(installed, workspace, profile, window, cx)?; + Some(Self { + workspace: workspace.clone(), + item, + shown: false, + polls: 0, + }) + } + + /// One look at the session; `open` tells whether the server can be written to now. A + /// session that asks the user to sign in is shown in its tab, once. + pub(super) fn poll(&mut self, open: bool, window: &mut Window, cx: &mut App) -> Step { + if open { + return Step::Connected; + } + self.polls += 1; + match self.status(cx) { + None | Some(TerminalStatus::Closed) => { + return Step::Failed( + "Could not connect to the server with its saved credentials.".into(), + ); + } + Some(TerminalStatus::AwaitingUser | TerminalStatus::AwaitingVault) if !self.shown => { + self.shown = true; + let item = self.item; + let _ = self.workspace.update(cx, |workspace, cx| { + workspace.show_background_session(item, window, cx) + }); + } + Some(_) => {} + } + let limit = if self.shown { + SIGN_IN_POLLS + } else { + CONNECT_POLLS + }; + if self.polls >= limit { + Step::Failed("Connecting to the server timed out.".into()) + } else { + Step::Waiting { + signing_in: self.shown, + } + } + } + + fn status(&self, cx: &App) -> Option { + let workspace = self.workspace.upgrade()?; + let workspace = workspace.read(cx); + let terminals = workspace + .context_terminals(workspace.general_surface_source(cx), cx) + .ok()?; + let entry = terminals + .into_iter() + .find(|entry| entry.item == self.item)?; + entry.capabilities.state().info(cx).map(|info| info.status) + } + + /// Closes the session, unless the user was shown it. + pub(super) fn close(self, window: &mut Window, cx: &mut App) { + if self.shown { + return; + } + let _ = self.workspace.update(cx, |workspace, cx| { + workspace.close_background_session(self.item, window, cx) + }); + } +} diff --git a/crates/connections/src/editor/appearance.rs b/crates/connections/src/editor/appearance.rs index 53083669..8e2f2924 100644 --- a/crates/connections/src/editor/appearance.rs +++ b/crates/connections/src/editor/appearance.rs @@ -13,7 +13,7 @@ use gpui_kit::{ prelude::*, px, rems, }; -use nocterm_ui::{IconName, form}; +use nocterm_ui::form; use super::{ConnectionEditor, ICON_ROWS_HEIGHT, field, text}; use nocterm_connections_model::draft::AppearanceDraft; @@ -242,41 +242,16 @@ impl AppearanceFields { }) .child(h_flex().flex_wrap().gap_1().children(tiles)) .when(!expanded, |grid| { - grid.child( - div() - .absolute() - .left_0() - .right_0() - .bottom_0() - .h(px(ICON_ROWS_HEIGHT / 2.)) - .bg(gpui_kit::linear_gradient( - 180., - gpui_kit::linear_color_stop(background.opacity(0.), 0.), - gpui_kit::linear_color_stop(background, 1.), - )), - ) + grid.child(form::fade(background, px(ICON_ROWS_HEIGHT / 2.))) }); - let icon = v_flex().gap_1().child(grid).child( - div().child( - Button::new("editor-icons-expand") - .xsmall() - .ghost() - .icon(if expanded { - IconName::ChevronUp - } else { - IconName::ChevronDown - }) - .label(if expanded { - "Show fewer".to_owned() - } else { - format!("Show all {count}") - }) - .on_click(cx.listener(|this, _, _, cx| { - this.appearance.toggle_expanded(); - cx.notify(); - })), - ), - ); + let icon = v_flex().gap_1().child(grid).child(div().child( + form::fold_button("editor-icons-expand", expanded, count).on_click(cx.listener( + |this, _, _, cx| { + this.appearance.toggle_expanded(); + cx.notify(); + }, + )), + )); icon.into_any_element() } fn color_control(&self, cx: &mut Context) -> AnyElement { diff --git a/crates/connections/src/editor/authentication.rs b/crates/connections/src/editor/authentication.rs index a529bd92..365619fc 100644 --- a/crates/connections/src/editor/authentication.rs +++ b/crates/connections/src/editor/authentication.rs @@ -1,44 +1,69 @@ -//! Authentication selection and its native inputs. -use super::{ConnectionEditor, field, text, watch}; +//! Authentication selection and its native inputs, and the password the connection signs in +//! with: typed here and kept in the window's vault, or one the vault already keeps. +use super::{ + ConnectionEditor, field, password::PasswordField, text, vault_key::VaultKeyField, watch, +}; +use crate::{SavedPasswords, VaultPlace}; use gpui_kit::{App, Context, Entity, Subscription, Window, component::input::InputState}; use nocterm_connections_model::draft::{AuthKind, AuthenticationDraft}; use nocterm_connections_model::profiles::Profile; -use nocterm_session::Auth; +use nocterm_session::{Auth, Secret}; +use nocterm_ssh_keys::VaultSshKeys; +use std::sync::Arc; pub(super) struct AuthenticationFields { kind: AuthKind, key_path: Entity, - credential: Entity, + password: Entity, + place: VaultPlace, + /// The connection signs in with a key file, which a hub window offers only to keep it. + key_file: bool, + vault_key: Entity, _subscriptions: Vec, } impl AuthenticationFields { pub(super) fn new( profile: Option<&Profile>, + keys: Arc, + passwords: Arc, window: &mut Window, cx: &mut Context, ) -> Self { let (kind, path) = match profile.map(|p| &p.auth) { Some(Auth::Password) => (AuthKind::Password, String::new()), Some(Auth::Key { path }) => (AuthKind::Key, path.display().to_string()), + Some(Auth::VaultKey { .. }) => (AuthKind::VaultKey, String::new()), _ => (AuthKind::Auto, String::new()), }; let key_path = field("~/.ssh/id_ed25519", path, window, cx); - let credential = field( - "Credential ID copied from Vault (optional)", - profile - .and_then(|p| p.credential) - .map(|id| id.to_string()) - .unwrap_or_default(), - window, - cx, - ); - let subscriptions = [&key_path, &credential] + let place = passwords.place(); + let chosen = profile.and_then(|p| p.credential); + let password = cx.new(|cx| { + let mut field = PasswordField::new(passwords, chosen, window, cx); + field.refresh(cx); + field + }); + let auth = profile.map(|p| &p.auth); + let vault_key = cx.new(|cx| VaultKeyField::new(keys, auth, window, cx)); + let typed = password.read(cx).input().clone(); + let (label, passphrase) = { + let field = vault_key.read(cx); + (field.label().clone(), field.passphrase().clone()) + }; + let mut subscriptions: Vec = [&key_path, &typed, &label, &passphrase] .into_iter() .map(|input| watch(input, window, cx)) .collect(); + subscriptions.push(cx.observe(&password, |_, _, cx| cx.notify())); + subscriptions.push(cx.subscribe(&vault_key, |this, _, event, cx| { + this.on_vault_key(event, cx) + })); Self { kind, key_path, - credential, + password, + place, + key_file: kind == AuthKind::Key, + vault_key, _subscriptions: subscriptions, } } @@ -51,29 +76,50 @@ impl AuthenticationFields { pub(super) fn key_path(&self) -> &Entity { &self.key_path } - pub(super) fn credential(&self) -> &Entity { - &self.credential + pub(super) fn vault_key(&self) -> &Entity { + &self.vault_key + } + pub(super) fn password(&self) -> &Entity { + &self.password + } + /// The field the page of the method starts with. + pub(super) fn first_input(&self, cx: &App) -> Option> { + match self.kind { + AuthKind::Auto | AuthKind::Password => Some(self.password.read(cx).input().clone()), + AuthKind::Key => Some(self.key_path.clone()), + AuthKind::VaultKey => None, + } + } + /// The password typed to keep, for a method that signs in with one. + pub(super) fn typed_password(&self, cx: &App) -> Option { + matches!(self.kind, AuthKind::Auto | AuthKind::Password) + .then(|| self.password.read(cx).typed(cx)) + .flatten() } pub(super) fn snapshot(&self, cx: &App) -> AuthenticationDraft { AuthenticationDraft { kind: self.kind, key_path: text(&self.key_path, cx), - credential: text(&self.credential, cx), + vault_key: self.vault_key.read(cx).chosen(), + credential: self.password.read(cx).chosen(), } } } use super::render::field_row; +use gpui_kit::component::input::Input; use gpui_kit::{ - AnyElement, + AnyElement, ClipboardItem, SharedString, TestSupportExt as _, component::{ - Selectable as _, Sizable as _, + ActiveTheme as _, Icon, Selectable as _, Sizable as _, button::{Button, ButtonVariants as _}, - h_flex, + h_flex, v_flex, }, + div, prelude::*, + rems, }; -use nocterm_ui::form; +use nocterm_ui::{IconName, form}; impl AuthenticationFields { fn render_auth_choice(&self, cx: &mut Context) -> AnyElement { @@ -88,44 +134,340 @@ impl AuthenticationFields { .selected(self.kind == kind) .on_click(cx.listener(move |this, _, window, cx| this.set_auth(kind, window, cx))) }; + // A key file stays on this computer: a hub window keeps keys in the account's vault. + let key_file = self.place == VaultPlace::ThisComputer || self.key_file; h_flex() .gap_1() .child(choice("auth-auto", "Automatic", AuthKind::Auto, cx)) .child(choice("auth-password", "Password", AuthKind::Password, cx)) - .child(choice("auth-key", "Key file", AuthKind::Key, cx)) + .when(key_file, |row| { + row.child(choice("auth-key", "Key file", AuthKind::Key, cx)) + }) + .child(choice( + "auth-vault-key", + "Vault key", + AuthKind::VaultKey, + cx, + )) .into_any_element() } pub(super) fn page(&self, cx: &mut Context) -> Vec { + let account = self.place == VaultPlace::Account; let hint = match self.kind { - AuthKind::Auto => "Like ssh: the agent, then your default keys, then a password.", - AuthKind::Password => "Always ask for the account's password.", + AuthKind::Auto => { + "Like ssh: this computer's agent and default keys, then the password." + } + AuthKind::Password => "Sign in with the account's password.", + AuthKind::Key if account => { + "A key file on this computer only. Move it to the vault to sign in from your other \ + computers too." + } AuthKind::Key => { "Sign in with one private key; its passphrase is asked for if it has one." } + AuthKind::VaultKey if account => { + "Sign in with a key kept in your account's vault on the hub. Every computer of \ + the account signs in with it." + } + AuthKind::VaultKey => { + "Sign in with a key kept in the vault. It follows the vault to your other devices." + } }; let choice = self.render_auth_choice(cx); let mut sign_in = vec![form::stacked_row("Method", hint, choice, None, cx)]; - if self.kind == AuthKind::Key { - sign_in.push(field_row( - "Private key", - "Path to the key file.", - &self.key_path, + match self.kind { + AuthKind::Key => { + sign_in.push(field_row( + "Private key", + "Path to the key file.", + &self.key_path, + cx, + )); + sign_in.push(form::row( + "Move to the vault", + "Stores this key in the vault and signs in with it from there.", + Button::new("vault-key-move") + .small() + .label("Move to vault") + .on_click( + cx.listener(|this, _, window, cx| this.import_vault_key(window, cx)), + ), + cx, + )); + } + AuthKind::VaultKey => sign_in.extend(self.vault_key_rows(cx)), + AuthKind::Auto | AuthKind::Password => { + sign_in.push(self.password_row(cx)); + sign_in.extend(self.saved_passwords_row(cx)); + } + } + vec![form::section("Sign in", sign_in, cx)] + } + + /// The password to keep, and where it is kept. + fn password_row(&self, cx: &mut Context) -> AnyElement { + let account = self.place == VaultPlace::Account; + let field = self.password.read(cx); + let hint: SharedString = match (field.chosen_listed(), field.chosen().is_some(), account) { + (Some(saved), _, _) if saved.servers > 1 => format!( + "Signs in with \u{201c}{}\u{201d}, shared by {} servers. A new password changes \ + it for all of them.", + saved.label, saved.servers + ) + .into(), + (Some(saved), _, _) => format!( + "Signs in with \u{201c}{}\u{201d} from the vault. Type a new one to replace it.", + saved.label + ) + .into(), + (None, true, true) => { + "Saved in your account's vault on the hub. Type a new one to replace it.".into() + } + (None, true, false) => { + "Saved in this computer's vault. Type a new one to replace it.".into() + } + (None, false, true) => "Kept in your account's vault on the hub, encrypted end to \ + end: every computer of the account signs in with it. Empty: \ + asked when connecting." + .into(), + (None, false, false) => { + "Kept in this computer's vault. Empty: asked when connecting.".into() + } + }; + let forget = field.chosen().is_some().then(|| { + Button::new("editor-forget-password") + .small() + .ghost() + .label("Forget") + .tooltip("Ask for the password when connecting") + .on_click(cx.listener(|this, _, _, cx| { + this.authentication + .password() + .update(cx, |field, cx| field.forget(cx)); + })) + }); + let input = field.input().clone(); + form::row( + "Password", + hint, + h_flex() + .gap_1() + .child( + div().w(rems(13.)).child( + Input::new(&input) + .id("editor-field-Password") + .small() + .mask_toggle(), + ), + ) + .children(forget), + cx, + ) + } + + /// The passwords the vault keeps, to sign in with one of them; none when it keeps none. + fn saved_passwords_row(&self, cx: &mut Context) -> Option { + let field = self.password.read(cx); + if field.listed().is_empty() { + return None; + } + let chosen = field.chosen(); + let query = field.query().clone(); + let matching: Vec<(usize, String, usize, bool)> = field + .matching(cx) + .into_iter() + .map(|(index, saved)| { + let picked = chosen == Some(saved.id); + (index, saved.label.clone(), saved.servers, picked) + }) + .collect(); + let empty = matching.is_empty(); + let rows: Vec = matching + .into_iter() + .map(|(index, label, servers, picked)| { + saved_password_row(index, label, servers, picked, cx) + }) + .collect(); + let list = v_flex() + .w_full() + .gap_0p5() + .child( + div().pb_1().child( + Input::new(&query) + .id("saved-passwords-search") + .small() + .prefix(Icon::new(IconName::Search).small()) + .cleanable(true), + ), + ) + .children(rows) + .when(empty, |list| { + list.child(form::note("No saved password has this name.", cx)) + }); + Some(form::stacked_row( + "Saved passwords", + "Sign in with a password the vault keeps. One password can sign in to several \ + servers; add and rename them on the vault's Credentials tab.", + list.into_any_element(), + None, + cx, + )) + } +} + +/// One saved password to sign in with: its name and how many servers use it, left aligned, +/// the chosen one marked. +fn saved_password_row( + index: usize, + label: String, + servers: usize, + picked: bool, + cx: &mut Context, +) -> AnyElement { + let theme = cx.theme(); + let hover = theme.foreground.opacity(0.06); + let servers = match servers { + 0 => "no server yet".to_owned(), + 1 => "1 server".to_owned(), + n => format!("{n} servers"), + }; + h_flex() + .id(SharedString::from(format!("saved-password-{index}"))) + .test_support() + .role(gpui_kit::Role::Button) + .aria_label(label.clone()) + .w_full() + .min_w_0() + .gap_2() + .px_2() + .py_1() + .rounded(theme.radius) + .cursor_pointer() + .text_sm() + .when(picked, |row| row.bg(theme.secondary_active)) + .when(!picked, |row| row.hover(move |style| style.bg(hover))) + .child(Icon::new(IconName::KeyRound).small().text_color(if picked { + theme.primary + } else { + theme.muted_foreground + })) + .child(div().min_w_0().truncate().child(label)) + .child( + div() + .flex_shrink_0() + .text_xs() + .text_color(theme.muted_foreground) + .child(servers), + ) + .child(div().flex_1()) + .when(picked, |row| { + row.child(Icon::new(IconName::Check).small().text_color(theme.primary)) + }) + .on_click(cx.listener(move |this, _, window, cx| { + this.authentication + .password() + .update(cx, |field, cx| field.choose(index, window, cx)); + })) + .into_any_element() +} + +impl AuthenticationFields { + /// The vault's keys to choose from, and ways to add one. + fn vault_key_rows(&self, cx: &mut Context) -> Vec { + let field = self.vault_key.read(cx); + let keys = field.listed().iter().enumerate().map(|(index, key)| { + Button::new(SharedString::from(format!("vault-key-{index}"))) + .small() + .ghost() + .label(format!("{} \u{b7} {}", key.label, key.fingerprint)) + .selected(field.is_chosen(key)) + .on_click(cx.listener(move |this, _, _, cx| { + this.authentication + .vault_key() + .update(cx, |field, cx| field.choose_listed(index, cx)); + })) + }); + let list = v_flex().gap_1().children(keys).children( + field + .note() + .map(|note| div().text_xs().child(note.to_owned())), + ); + let mut rows = vec![form::stacked_row( + "Key", + "The keys the vault holds.", + list.into_any_element(), + None, + cx, + )]; + if let Some(fingerprint) = field.chosen_fingerprint() { + rows.push(form::row( + "Public key", + fingerprint, + Button::new("vault-key-copy") + .small() + .ghost() + .icon(IconName::Copy) + .label("Copy public key") + .on_click(cx.listener(|this, _, _, cx| { + if let Some(chosen) = this.authentication.vault_key().read(cx).chosen() { + cx.write_to_clipboard(ClipboardItem::new_string(chosen.public_key)); + } + })), cx, )); } - vec![ - form::section("Sign in", sign_in, cx), - form::section( - "Saved password", - [field_row( - "Credential ID", - "Saved from the sign-in prompt. Empty: ask each time.", - &self.credential, - cx, - )], + rows.push(field_row( + "Key name", + "How a new or imported key is named in the vault.", + field.label(), + cx, + )); + rows.push(form::row( + "New key", + "Creates an Ed25519 key in the vault.", + Button::new("vault-key-create") + .small() + .label("Create") + .on_click(cx.listener(|this, _, window, cx| this.create_vault_key(window, cx))), + cx, + )); + rows.push(self.import_row(cx)); + if field.passphrase_needed() { + rows.push(field_row( + "Passphrase", + "The key file is protected; the vault keeps it without the passphrase.", + field.passphrase(), cx, - ), - ] + )); + } + rows + } + + /// The key file to import, with its button beside the path so that the + /// button stays where it was when the passphrase row appears below. + fn import_row(&self, cx: &Context) -> AnyElement { + form::row( + "Private key", + "Path to a key file to import into the vault.", + h_flex() + .gap_1() + .child( + div().w(rems(13.)).child( + Input::new(&self.key_path) + .id("editor-field-Private key") + .small(), + ), + ) + .child( + Button::new("vault-key-import") + .small() + .label("Import") + .on_click( + cx.listener(|this, _, window, cx| this.import_vault_key(window, cx)), + ), + ), + cx, + ) } } diff --git a/crates/connections/src/editor/password.rs b/crates/connections/src/editor/password.rs new file mode 100644 index 00000000..595d67fd --- /dev/null +++ b/crates/connections/src/editor/password.rs @@ -0,0 +1,133 @@ +//! The password a connection signs in with: typed here to keep it in the window's vault, or one +//! the vault already keeps, which may sign in to other servers as well. +//! +//! [`PasswordField`] owns the typed text, the chosen record, the vault's list of passwords and +//! the search that narrows it; the editor reads them when it saves and decides what to keep or +//! attach. +use crate::{SavedPassword, SavedPasswords}; +use gpui_kit::{ + AppContext as _, Context, Entity, Subscription, Task, Window, + component::input::{InputEvent, InputState}, +}; +use nocterm_session::{CredentialId, Secret}; +use std::sync::Arc; + +pub(super) struct PasswordField { + passwords: Arc, + /// A new password to keep; empty keeps the chosen one. + input: Entity, + /// The saved password the connection signs in with, until another is chosen or it is + /// forgotten. + chosen: Option, + listed: Vec, + /// What the list is narrowed to. + query: Entity, + /// Bumped by every listing, so that a superseded answer is dropped. + epoch: u64, + listing: Option>, + _query_changed: Subscription, +} + +impl PasswordField { + pub(super) fn new( + passwords: Arc, + chosen: Option, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let input = cx.new(|cx| { + InputState::new(window, cx) + .placeholder("Password") + .masked(true) + }); + let query = cx.new(|cx| InputState::new(window, cx).placeholder("Search saved passwords")); + let query_changed = cx.subscribe(&query, |_, _, event: &InputEvent, cx| { + if matches!(event, InputEvent::Change) { + cx.notify(); + } + }); + Self { + passwords, + input, + chosen, + listed: Vec::new(), + query, + epoch: 0, + listing: None, + _query_changed: query_changed, + } + } + + pub(super) fn input(&self) -> &Entity { + &self.input + } + + pub(super) fn chosen(&self) -> Option { + self.chosen + } + + pub(super) fn listed(&self) -> &[SavedPassword] { + &self.listed + } + + pub(super) fn query(&self) -> &Entity { + &self.query + } + + /// The listed passwords whose name has the searched text, with their place in the list. + pub(super) fn matching(&self, cx: &gpui_kit::App) -> Vec<(usize, &SavedPassword)> { + let query = self.query.read(cx).value().trim().to_lowercase(); + self.listed + .iter() + .enumerate() + .filter(|(_, password)| { + query.is_empty() || password.label.to_lowercase().contains(&query) + }) + .collect() + } + + /// The saved password the connection signs in with, as listed. + pub(super) fn chosen_listed(&self) -> Option<&SavedPassword> { + let chosen = self.chosen?; + self.listed.iter().find(|password| password.id == chosen) + } + + /// The password typed to keep, if any. + pub(super) fn typed(&self, cx: &gpui_kit::App) -> Option { + let typed = self.input.read(cx).value().to_string(); + (!typed.is_empty()).then(|| Secret::new(typed)) + } + + /// Signs in with the saved password at `index` of the list; a typed one is dropped. + pub(super) fn choose(&mut self, index: usize, window: &mut Window, cx: &mut Context) { + let Some(password) = self.listed.get(index) else { + return; + }; + self.chosen = Some(password.id); + self.input + .update(cx, |input, cx| input.set_value("", window, cx)); + cx.notify(); + } + + /// Unlinks the saved password: the connection asks for it again. + pub(super) fn forget(&mut self, cx: &mut Context) { + self.chosen = None; + cx.notify(); + } + + /// Reads the vault's passwords again; a vault that cannot list them offers none. + pub(super) fn refresh(&mut self, cx: &mut Context) { + self.epoch = self.epoch.wrapping_add(1); + let epoch = self.epoch; + let listed = self.passwords.list(); + self.listing = Some(cx.spawn(async move |this, cx| { + let listed = listed.await.unwrap_or_default(); + let _ = this.update(cx, |this, cx| { + if this.epoch == epoch { + this.listed = listed; + cx.notify(); + } + }); + })); + } +} diff --git a/crates/connections/src/editor/render.rs b/crates/connections/src/editor/render.rs index 3f41fdc1..f15dec4a 100644 --- a/crates/connections/src/editor/render.rs +++ b/crates/connections/src/editor/render.rs @@ -55,14 +55,21 @@ impl Render for ConnectionEditor { (window.viewport_size().height - rems(10.).to_pixels(window.rem_size())).max(px(0.)); let nav_width = rems(layout.connection_nav_width); let page = match self.section { - EditorSection::Connection => self.connection.page( - &self.launch, - &self.appearance, - self.edit.id(), - self.edit.editing(), - &self.deps, - cx, - ), + EditorSection::Connection => { + let mut page = self.connection.page( + &self.launch, + &self.appearance, + self.edit.id(), + self.edit.editing(), + &self.deps, + cx, + ); + let agents = self.agent_access.row(self.edit.original(), cx); + if let Some(row) = agents { + page.insert(1, form::section("Agents", [row], cx)); + } + page + } EditorSection::Authentication => self.authentication.page(cx), EditorSection::JumpHosts => { let labels = self.jump_hosts.labels(&self.deps, cx); @@ -103,6 +110,7 @@ impl Render for ConnectionEditor { .min_h_0() .pr_2() .overflow_y_scroll() + .track_scroll(&self.scroll) .children(page), ) } diff --git a/crates/connections/src/editor/tests.rs b/crates/connections/src/editor/tests.rs index c9c98308..8e8c7602 100644 --- a/crates/connections/src/editor/tests.rs +++ b/crates/connections/src/editor/tests.rs @@ -369,7 +369,7 @@ fn hidden_invalid_session_options_open_the_session_section(cx: &mut TestAppConte .unwrap(); } #[gpui_kit::test] -fn hidden_invalid_credential_focuses_credential_and_keeps_draft(cx: &mut TestAppContext) { +fn a_password_with_no_vault_to_keep_it_keeps_the_draft_open(cx: &mut TestAppContext) { let (handle, workspace, _, installed) = crate::test_support::workspace(cx); cx.update_window(handle, |_, window, cx| { open_editor(installed.clone(), None, workspace.downgrade(), window, cx); @@ -381,28 +381,20 @@ fn hidden_invalid_credential_focuses_credential_and_keeps_draft(cx: &mut TestApp cx.run_until_parked(); cx.update_window(handle, |_, window, cx| { window.render_frame(cx); - window.input("not-an-id", cx); - window.click("editor-section-connection", cx); - }) - .unwrap(); - cx.run_until_parked(); - cx.update_window(handle, |_, window, cx| { - window.render_frame(cx); + // The section opens on its password field. + window.input("s3cret", cx); window.click("editor-save", cx); }) .unwrap(); cx.run_until_parked(); cx.update_window(handle, |_, window, cx| { window.render_frame(cx); - assert!(window.try_find("editor-field-Credential ID").is_some()); - assert_eq!( - window.focused_input(cx).unwrap().value(cx).as_ref(), - "not-an-id" - ); + assert!(window.try_find("dialog").is_some()); assert_eq!( installed.connections().read(cx).profiles(cx).iter().count(), 0 ); + window.click("editor-field-Password", cx); window.press("ctrl-a", cx); window.press("backspace", cx); window.click("editor-save", cx); @@ -512,11 +504,11 @@ fn every_field_is_used() { assert_eq!(profile.group.as_deref(), Some("Work")); } #[test] -fn launch_override_and_credential_id_validate_and_preserve_arguments() { +fn launch_override_and_the_saved_password_validate_and_preserve_arguments() { let mut draft = fields("example.com"); assert!(build(&draft, AuthKind::Auto).unwrap().launch.is_none()); let id = nocterm_session::CredentialId::from_bytes([42; 16]); - draft.authentication.credential = id.to_string(); + draft.authentication.credential = Some(id); draft.launch.override_launch = true; draft.launch.program = "/bin/bash".into(); draft.launch.args = r#"["-c","printf '%s' \"a b\""]"#.into(); @@ -530,9 +522,6 @@ fn launch_override_and_credential_id_validate_and_preserve_arguments() { assert!(!launch.integration); draft.launch.env = r#"{"INVALID;KEY":"value"}"#.into(); assert!(build(&draft, AuthKind::Auto).is_err()); - draft.launch.override_launch = false; - draft.authentication.credential = "plain password".into(); - assert!(build(&draft, AuthKind::Auto).is_err()); } #[test] fn starting_directory_sets_launch_cwd_without_override() { @@ -762,5 +751,8 @@ fn unsaved_country_input_loads_flag_when_detection_is_off_and_no_session_exists( ); } +mod agent_access; mod appearance; mod jump_hosts; +mod passwords; +mod vault_keys; diff --git a/crates/connections/src/editor/tests/agent_access.rs b/crates/connections/src/editor/tests/agent_access.rs new file mode 100644 index 00000000..e0f1423c --- /dev/null +++ b/crates/connections/src/editor/tests/agent_access.rs @@ -0,0 +1,164 @@ +//! In a hub window the editor of a saved connection offers Agent access through the injected +//! port, connecting with the connection's saved credentials when no session to it is open; +//! elsewhere, and for a connection not saved yet, it offers none. +use super::*; +use crate::test_support::ScriptedAgentAccess; +use gpui_kit::{Context, EntityId}; +use nocterm_workspace::{SessionFactory, SessionSpec, Workspace}; +use std::{cell::RefCell, rc::Rc, sync::Arc, time::Duration}; + +/// Opens sessions without a tab and records them; with `connects`, the session comes up at once. +struct BackgroundSessions { + opened: Rc>>, + access: Arc, + connects: bool, +} + +impl SessionFactory for BackgroundSessions { + fn open_background( + &self, + _: &mut Workspace, + spec: SessionSpec, + _: &mut Window, + cx: &mut Context, + ) -> Option { + self.opened.borrow_mut().push(spec); + if self.connects { + self.access.open_session(); + } + Some(cx.new(|_| ()).entity_id()) + } +} + +fn saved(installed: &Installed, cx: &mut gpui_kit::App) -> Profile { + use futures::FutureExt as _; + let profile = build(&fields("web.example"), AuthKind::Auto).unwrap(); + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + profile +} + +/// The editor of a saved connection over `access`; `None` is the window of this computer. +/// `connects` is how sessions opened without a tab come up. +fn editor_of_saved( + access: Option>, + connects: bool, + cx: &mut TestAppContext, +) -> ( + gpui_kit::AnyWindowHandle, + Profile, + Rc>>, +) { + let opened = Rc::default(); + let (handle, workspace, _, installed) = match &access { + Some(access) => crate::test_support::workspace_with_agent_access(cx, access.clone()), + None => crate::test_support::workspace(cx), + }; + if let Some(access) = access { + let sessions = BackgroundSessions { + opened: Rc::clone(&opened), + access, + connects, + }; + workspace.update(cx, |workspace, _| workspace.set_session_factory(sessions)); + } + cx.simulate_window_resize(handle, gpui_kit::size(px(1400.), px(1800.))); + let profile = cx + .update_window(handle, |_, window, cx| { + let profile = saved(&installed, cx); + open_editor( + installed, + Some(profile.clone()), + workspace.downgrade(), + window, + cx, + ); + profile + }) + .unwrap(); + cx.run_until_parked(); + (handle, profile, opened) +} + +fn toggle(handle: gpui_kit::AnyWindowHandle, cx: &mut TestAppContext) { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + window.click("profile-agent-access", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.executor().advance_clock(Duration::from_secs(1)); + cx.run_until_parked(); +} + +fn shows(handle: gpui_kit::AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) -> bool { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + window.try_find(id).is_some() + }) + .unwrap() +} + +#[gpui_kit::test] +fn the_switch_grants_and_withdraws_access_of_the_saved_server(cx: &mut TestAppContext) { + let access = Arc::new(ScriptedAgentAccess::with_session()); + let (handle, profile, opened) = editor_of_saved(Some(access.clone()), false, cx); + for _ in 0..2 { + toggle(handle, cx); + } + assert_eq!(access.granted(), vec![true, false]); + assert!(opened.borrow().is_empty(), "the open session was used"); + let server = access.changes.lock().unwrap()[0].0.clone(); + assert_eq!(server.connection, profile.id.to_string()); + assert_eq!(server.host, "web.example"); + assert_eq!(server.user, profile.target.user); +} + +#[gpui_kit::test] +fn without_an_open_session_the_switch_connects_with_the_saved_credentials_first( + cx: &mut TestAppContext, +) { + let access = Arc::new(ScriptedAgentAccess::default()); + let (handle, profile, opened) = editor_of_saved(Some(access.clone()), true, cx); + toggle(handle, cx); + let opened = opened.borrow(); + assert_eq!(opened.len(), 1, "one session was opened without a tab"); + assert_eq!(opened[0].target, profile.target); + assert_eq!(opened[0].profile.as_deref(), Some(&*profile.id.to_string())); + assert_eq!(access.granted(), vec![true]); + assert!(!shows(handle, "profile-agent-access-problem", cx)); +} + +#[gpui_kit::test] +fn a_session_that_does_not_come_up_is_said_under_the_switch_and_changes_nothing( + cx: &mut TestAppContext, +) { + let access = Arc::new(ScriptedAgentAccess::default()); + let (handle, _, opened) = editor_of_saved(Some(access.clone()), false, cx); + toggle(handle, cx); + assert_eq!(opened.borrow().len(), 1); + assert!(access.granted().is_empty()); + assert!(shows(handle, "profile-agent-access-problem", cx)); +} + +#[gpui_kit::test] +fn a_refused_change_is_said_under_the_switch(cx: &mut TestAppContext) { + let access = Arc::new(ScriptedAgentAccess { + refusal: Some("The hub did not accept the change."), + ..ScriptedAgentAccess::with_session() + }); + let (handle, _, _) = editor_of_saved(Some(access), false, cx); + toggle(handle, cx); + assert!(shows(handle, "profile-agent-access-problem", cx)); +} + +#[gpui_kit::test] +fn the_window_of_this_computer_offers_no_agent_access(cx: &mut TestAppContext) { + let (handle, _, _) = editor_of_saved(None, false, cx); + assert!(!shows(handle, "profile-agent-access", cx)); +} diff --git a/crates/connections/src/editor/tests/passwords.rs b/crates/connections/src/editor/tests/passwords.rs new file mode 100644 index 00000000..ee892e7c --- /dev/null +++ b/crates/connections/src/editor/tests/passwords.rs @@ -0,0 +1,240 @@ +//! The password a connection signs in with, typed in the "Authentication" section and kept in +//! the window's vault. +use super::*; +use crate::{VaultPlace, test_support::ScriptedPasswords}; +use nocterm_connections_model::profiles::Profile; +use nocterm_session::CredentialId; +use std::sync::Arc; + +fn saved_profiles(cx: &mut TestAppContext, installed: &crate::Installed) -> Vec { + cx.read(|cx| { + installed + .connections() + .read(cx) + .profiles(cx) + .iter() + .cloned() + .collect() + }) +} + +fn act( + cx: &mut TestAppContext, + handle: gpui_kit::AnyWindowHandle, + steps: impl FnOnce(&mut gpui_kit::Window, &mut gpui_kit::App), +) { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + steps(window, cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +/// Opens the editor of `profile` (a new connection to `prod.example` without one) on its +/// "Authentication" section. +fn open( + cx: &mut TestAppContext, + passwords: &Arc, + profile: Option, +) -> (gpui_kit::AnyWindowHandle, crate::Installed) { + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_passwords(cx, passwords.clone()); + if let Some(profile) = &profile { + cx.update(|cx| { + use futures::FutureExt as _; + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + }); + } + cx.simulate_window_resize(handle, gpui_kit::size(px(900.), px(900.))); + let is_new = profile.is_none(); + act(cx, handle, |window, cx| { + open_editor( + installed.clone(), + profile, + workspace.downgrade(), + window, + cx, + ); + window.render_frame(cx); + if is_new { + window.input("prod.example", cx); + } + window.click("editor-section-authentication", cx); + }); + (handle, installed) +} + +fn type_password(cx: &mut TestAppContext, handle: gpui_kit::AnyWindowHandle, password: &str) { + act(cx, handle, |window, cx| { + window.click("editor-field-Password", cx); + window.input(password, cx); + }); +} + +#[gpui_kit::test] +fn a_typed_password_goes_to_the_windows_vault_and_the_connection_names_its_record( + cx: &mut TestAppContext, +) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + let (handle, installed) = open(cx, &passwords, None); + act(cx, handle, |window, cx| window.click("auth-password", cx)); + type_password(cx, handle, "s3cret"); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + + let kept = passwords.kept.lock().unwrap().clone(); + assert_eq!(kept.len(), 1); + let (replacing, label, target, password) = &kept[0]; + assert_eq!(*replacing, None); + assert_eq!(label, "prod.example"); + assert_eq!(*target, Target::new("tester", "prod.example", 22)); + assert_eq!(password, "s3cret"); + let profiles = saved_profiles(cx, &installed); + assert_eq!(profiles[0].auth, Auth::Password); + assert_eq!( + profiles[0].credential, + Some(CredentialId::from_bytes([1; 16])) + ); +} + +#[gpui_kit::test] +fn a_new_password_replaces_the_saved_one_and_forgetting_unlinks_it(cx: &mut TestAppContext) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + let id = CredentialId::from_bytes([9; 16]); + let mut draft = fields("prod.example"); + draft.endpoint.user = "ops".into(); + let mut profile = build(&draft, AuthKind::Password).unwrap(); + profile.credential = Some(id); + let (handle, installed) = open(cx, &passwords, Some(profile.clone())); + type_password(cx, handle, "n3w"); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + let kept = passwords.kept.lock().unwrap().clone(); + assert_eq!( + kept[0].0, + Some(id), + "the saved record takes the new password" + ); + assert_eq!(saved_profiles(cx, &installed)[0].credential, Some(id)); + + profile = saved_profiles(cx, &installed).remove(0); + let (handle, installed) = open(cx, &passwords, Some(profile)); + act(cx, handle, |window, cx| { + window.click("editor-forget-password", cx) + }); + act(cx, handle, |window, cx| { + assert!(window.try_find("editor-forget-password").is_none()); + window.click("editor-save", cx); + }); + assert_eq!(saved_profiles(cx, &installed)[0].credential, None); + assert_eq!(passwords.kept.lock().unwrap().len(), 1, "nothing new kept"); +} + +#[gpui_kit::test] +fn a_locked_vault_keeps_the_editor_open_and_saves_nothing(cx: &mut TestAppContext) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + passwords + .locked + .store(true, std::sync::atomic::Ordering::SeqCst); + let (handle, installed) = open(cx, &passwords, None); + type_password(cx, handle, "s3cret"); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + act(cx, handle, |window, _| { + assert!( + window.try_find("editor-save").is_some(), + "the editor stays open" + ); + }); + assert!(saved_profiles(cx, &installed).is_empty()); +} + +#[gpui_kit::test] +fn a_hub_window_offers_no_key_file_and_this_computer_does(cx: &mut TestAppContext) { + for (place, offered) in [ + (VaultPlace::Account, false), + (VaultPlace::ThisComputer, true), + ] { + let passwords = Arc::new(ScriptedPasswords::new(place)); + let (handle, _) = open(cx, &passwords, None); + act(cx, handle, |window, _| { + assert_eq!(window.try_find("auth-key").is_some(), offered); + assert!(window.try_find("auth-vault-key").is_some()); + }); + } +} + +#[gpui_kit::test] +fn a_saved_password_is_chosen_from_the_vault_and_signs_in_to_the_new_server_too( + cx: &mut TestAppContext, +) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + let office = CredentialId::from_bytes([5; 16]); + passwords.saved.lock().unwrap().push(crate::SavedPassword { + id: office, + label: "Office".into(), + servers: 2, + }); + let (handle, installed) = open(cx, &passwords, None); + type_password(cx, handle, "typed first"); + act(cx, handle, |window, cx| { + window.click("saved-password-0", cx) + }); + act(cx, handle, |window, cx| { + assert!(window.try_find("editor-forget-password").is_some()); + window.click("editor-save", cx); + }); + assert!( + passwords.kept.lock().unwrap().is_empty(), + "choosing drops the typed one" + ); + assert_eq!( + *passwords.attached.lock().unwrap(), + vec![(office, Target::new("tester", "prod.example", 22))] + ); + let profile = saved_profiles(cx, &installed).remove(0); + assert_eq!(profile.credential, Some(office)); + + // Saving it again without changing the password or the address attaches nothing more. + let (handle, _) = open(cx, &passwords, Some(profile)); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + assert_eq!(passwords.attached.lock().unwrap().len(), 1); +} + +#[gpui_kit::test] +fn the_search_narrows_the_saved_passwords_by_name(cx: &mut TestAppContext) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + for (byte, label) in [(1u8, "Office"), (2, "homelab"), (3, "Office VPN")] { + passwords.saved.lock().unwrap().push(crate::SavedPassword { + id: CredentialId::from_bytes([byte; 16]), + label: label.into(), + servers: 1, + }); + } + let (handle, _) = open(cx, &passwords, None); + let shown = |window: &mut gpui_kit::Window| { + (0..3) + .filter(|index| window.try_find(format!("saved-password-{index}")).is_some()) + .collect::>() + }; + act(cx, handle, |window, cx| { + assert_eq!(shown(window), [0, 1, 2]); + window.click("saved-passwords-search", cx); + window.input("office", cx); + }); + act(cx, handle, |window, cx| { + assert_eq!( + shown(window), + [0, 2], + "each keeps its place in the vault's list" + ); + window.click("saved-password-2", cx); + }); + act(cx, handle, |window, _| { + assert!(window.try_find("editor-forget-password").is_some()); + }); +} diff --git a/crates/connections/src/editor/tests/vault_keys.rs b/crates/connections/src/editor/tests/vault_keys.rs new file mode 100644 index 00000000..0d569d48 --- /dev/null +++ b/crates/connections/src/editor/tests/vault_keys.rs @@ -0,0 +1,286 @@ +//! Choosing, creating and importing vault keys in the "Authentication" section. +use super::*; +use futures::FutureExt as _; +use nocterm_connections_model::profiles::Profile; +use nocterm_ssh_keys::{VaultSshKeys, testing::MemoryVaultSshKeys}; +use std::sync::Arc; + +const ENCRYPTED: &str = include_str!("../../../../ssh-keys/src/tests/fixtures/ed25519-encrypted"); +const PASSPHRASE: &str = "correct horse"; + +fn keys_of(keys: &MemoryVaultSshKeys) -> Vec { + keys.list().now_or_never().unwrap().unwrap() +} + +fn saved_profiles(cx: &mut TestAppContext, installed: &crate::Installed) -> Vec { + cx.read(|cx| { + installed + .connections() + .read(cx) + .profiles(cx) + .iter() + .cloned() + .collect() + }) +} + +fn open_authentication( + cx: &mut TestAppContext, + handle: gpui_kit::AnyWindowHandle, + workspace: &gpui_kit::Entity, + installed: &crate::Installed, + profile: Option, +) { + cx.simulate_window_resize(handle, gpui_kit::size(px(900.), px(900.))); + let is_new = profile.is_none(); + cx.update_window(handle, |_, window, cx| { + open_editor( + installed.clone(), + profile, + workspace.downgrade(), + window, + cx, + ); + window.render_frame(cx); + if is_new { + window.input("vault.test", cx); + } + window.click("editor-section-authentication", cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +fn act( + cx: &mut TestAppContext, + handle: gpui_kit::AnyWindowHandle, + steps: impl FnOnce(&mut gpui_kit::Window, &mut gpui_kit::App), +) { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + steps(window, cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn a_created_key_is_chosen_and_the_connection_saves_with_it(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| window.click("auth-vault-key", cx)); + act(cx, handle, |window, cx| { + assert!(window.try_find("vault-key-0").is_none(), "an empty vault"); + window.click("vault-key-create", cx); + }); + act(cx, handle, |window, cx| { + assert!( + window.try_find("vault-key-0").is_some(), + "the new key is listed" + ); + window.click("editor-save", cx); + }); + let stored = keys_of(&keys); + assert_eq!(stored.len(), 1); + assert_eq!(stored[0].label, "vault.test"); + let profiles = saved_profiles(cx, &installed); + assert_eq!( + profiles[0].auth, + Auth::VaultKey { + credential: stored[0].credential, + public_key: stored[0].public_key.as_str().to_owned(), + } + ); + assert_eq!( + profiles[0].credential, None, + "the key needs no password record" + ); +} + +#[gpui_kit::test] +fn a_key_already_in_the_vault_is_chosen_from_the_list(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + keys.generate("first".into()) + .now_or_never() + .unwrap() + .unwrap(); + let second = keys + .generate("second".into()) + .now_or_never() + .unwrap() + .unwrap(); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| window.click("auth-vault-key", cx)); + act(cx, handle, |window, cx| window.click("vault-key-1", cx)); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + let profiles = saved_profiles(cx, &installed); + assert!(matches!( + &profiles[0].auth, + Auth::VaultKey { credential, .. } if *credential == second.credential + )); +} + +#[gpui_kit::test] +fn saving_without_a_chosen_key_keeps_the_editor_open(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + keys.set_locked(true); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| window.click("auth-vault-key", cx)); + act(cx, handle, |window, cx| { + window.click("vault-key-create", cx) + }); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("dialog").is_some()); + assert!(window.try_find("vault-key-0").is_none()); + }) + .unwrap(); + assert!(saved_profiles(cx, &installed).is_empty()); +} + +#[gpui_kit::test] +fn an_encrypted_key_file_is_imported_after_its_passphrase_is_asked(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default() + .with_file("/keys/id_ed25519", nocterm_session::Secret::new(ENCRYPTED)); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| { + window.click("auth-vault-key", cx); + }); + act(cx, handle, |window, cx| { + window.click("editor-field-Private key", cx); + window.input("/keys/id_ed25519", cx); + assert!(window.try_find("editor-field-Passphrase").is_none()); + window.click("vault-key-import", cx); + }); + act(cx, handle, |window, cx| { + assert!( + window.try_find("vault-key-0").is_none(), + "nothing stored yet" + ); + window.click("editor-field-Passphrase", cx); + window.input(PASSPHRASE, cx); + window.click("vault-key-import", cx); + }); + act(cx, handle, |window, cx| { + assert!(window.try_find("vault-key-0").is_some()); + window.click("editor-save", cx); + }); + let stored = keys_of(&keys); + assert_eq!(stored.len(), 1); + // The vault holds the key without its passphrase. + let text = keys.secret_of(stored[0].credential).unwrap(); + assert!(nocterm_ssh_keys::KeyMaterial::import(&text, None).is_ok()); + assert!(matches!( + &saved_profiles(cx, &installed)[0].auth, + Auth::VaultKey { credential, .. } if *credential == stored[0].credential + )); +} + +#[gpui_kit::test] +fn moving_a_key_file_to_the_vault_turns_the_connection_into_a_vault_key_one( + cx: &mut TestAppContext, +) { + let plain = include_str!("../../../../ssh-keys/src/tests/fixtures/ed25519"); + let keys = + MemoryVaultSshKeys::default().with_file("/keys/id", nocterm_session::Secret::new(plain)); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + let mut draft = fields("moved.test"); + draft.authentication.key_path = "/keys/id".into(); + let mut profile = build(&draft, AuthKind::Key).unwrap(); + profile.name = "Moved".into(); + cx.update(|cx| { + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + }); + open_authentication(cx, handle, &workspace, &installed, Some(profile)); + act(cx, handle, |window, cx| window.click("vault-key-move", cx)); + act(cx, handle, |window, cx| { + assert!( + window.try_find("vault-key-0").is_some(), + "the vault key view is shown" + ); + window.click("editor-save", cx); + }); + let stored = keys_of(&keys); + assert_eq!(stored.len(), 1); + assert_eq!(stored[0].label, "Moved"); + assert!(matches!( + &saved_profiles(cx, &installed)[0].auth, + Auth::VaultKey { credential, .. } if *credential == stored[0].credential + )); +} + +fn events_of( + cx: &mut TestAppContext, + keys: &MemoryVaultSshKeys, + run: impl FnOnce( + &mut crate::editor::vault_key::VaultKeyField, + &mut gpui_kit::Window, + &mut gpui_kit::Context, + ), +) -> Vec { + use crate::editor::vault_key::VaultKeyField; + let (handle, _workspace, _opened, _installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + let events = std::rc::Rc::new(std::cell::RefCell::new(Vec::new())); + let seen = events.clone(); + let field = cx + .update_window(handle, |_, window, cx| { + let field = cx.new(|cx| VaultKeyField::new(Arc::new(keys.clone()), None, window, cx)); + cx.subscribe(&field, move |_, event, _| { + seen.borrow_mut().push(event.clone()) + }) + .detach(); + field.update(cx, |field, cx| run(field, window, cx)); + field + }) + .unwrap(); + cx.run_until_parked(); + drop(field); + events.borrow().clone() +} + +#[gpui_kit::test] +fn the_key_field_reports_a_created_key_as_chosen(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + let events = events_of(cx, &keys, |field, window, cx| { + field.create("made", window, cx) + }); + assert_eq!(events, [crate::editor::vault_key::VaultKeyEvent::Chosen]); +} + +#[gpui_kit::test] +fn the_key_field_reports_a_refusal_and_a_protected_file_as_events(cx: &mut TestAppContext) { + use crate::editor::vault_key::VaultKeyEvent; + let locked = MemoryVaultSshKeys::default(); + locked.set_locked(true); + let events = events_of(cx, &locked, |field, window, cx| { + field.create("made", window, cx) + }); + assert!(matches!(events.as_slice(), [VaultKeyEvent::Failed(_)])); + + let protected = MemoryVaultSshKeys::default() + .with_file("/keys/id", nocterm_session::Secret::new(ENCRYPTED)); + let events = events_of(cx, &protected, |field, window, cx| { + field.import("made", "/keys/id".into(), window, cx) + }); + assert!(matches!( + events.as_slice(), + [VaultKeyEvent::PassphraseAsked, VaultKeyEvent::Failed(_)] + )); +} diff --git a/crates/connections/src/editor/vault_key.rs b/crates/connections/src/editor/vault_key.rs new file mode 100644 index 00000000..d1120e63 --- /dev/null +++ b/crates/connections/src/editor/vault_key.rs @@ -0,0 +1,261 @@ +//! The key kept in the vault that a connection signs in with: choosing one, +//! creating an Ed25519 key and importing a key file. +//! +//! [`VaultKeyField`] owns its state and reports outcomes as [`VaultKeyEvent`]s; +//! the editor decides what they mean for the connection and its error line. +use futures::future::BoxFuture; +use gpui_kit::{ + AppContext as _, Context, Entity, EventEmitter, Task, Window, component::input::InputState, +}; +use nocterm_connections_model::draft::VaultKeyChoice; +use nocterm_session::{Auth, Secret}; +use nocterm_ssh_keys::{ + KeyImportError, KeySource, PublicKeyText, StoredKey, VaultSshKeys, VaultSshKeysError, +}; +use std::{path::PathBuf, sync::Arc}; + +/// Where the list of vault keys stands. +enum Listing { + Loading, + Ready, + Unavailable(String), +} + +/// What an operation of the field came to, for the editor to act on. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) enum VaultKeyEvent { + /// A key is now the chosen one. + Chosen, + /// The key file is protected and its passphrase is asked for now. + PassphraseAsked, + /// An operation failed; the text is what the person reads. + Failed(String), +} + +pub(super) struct VaultKeyField { + keys: Arc, + listed: Vec, + listing: Listing, + chosen: Option, + /// The name a new or imported key is stored under. + label: Entity, + /// Asked for only after the key turned out to be encrypted. + passphrase: Entity, + passphrase_needed: bool, + /// Bumped by every operation so that a superseded answer is dropped. + epoch: u64, + task: Option>, +} + +impl EventEmitter for VaultKeyField {} + +impl VaultKeyField { + pub(super) fn new( + keys: Arc, + auth: Option<&Auth>, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let chosen = match auth { + Some(Auth::VaultKey { + credential, + public_key, + }) => Some(VaultKeyChoice { + credential: *credential, + public_key: public_key.clone(), + }), + _ => None, + }; + let label = cx.new(|cx| InputState::new(window, cx).placeholder("Name of the new key")); + let passphrase = cx.new(|cx| { + InputState::new(window, cx) + .placeholder("Passphrase of the key file") + .masked(true) + }); + Self { + keys, + listed: Vec::new(), + listing: Listing::Loading, + chosen, + label, + passphrase, + passphrase_needed: false, + epoch: 0, + task: None, + } + } + pub(super) fn chosen(&self) -> Option { + self.chosen.clone() + } + pub(super) fn label(&self) -> &Entity { + &self.label + } + pub(super) fn passphrase(&self) -> &Entity { + &self.passphrase + } + pub(super) fn passphrase_needed(&self) -> bool { + self.passphrase_needed + } + pub(super) fn listed(&self) -> &[StoredKey] { + &self.listed + } + pub(super) fn is_chosen(&self, key: &StoredKey) -> bool { + self.chosen + .as_ref() + .is_some_and(|chosen| chosen.credential == key.credential) + } + /// What to tell the person about the list, when it is not simply shown. + pub(super) fn note(&self) -> Option<&str> { + match &self.listing { + Listing::Loading => Some("Loading the vault's keys…"), + Listing::Unavailable(message) => Some(message), + Listing::Ready if self.listed.is_empty() => { + Some("The vault holds no keys yet. Create one or import a key file.") + } + Listing::Ready => None, + } + } + /// The fingerprint of the chosen key, from the list or its public half. + pub(super) fn chosen_fingerprint(&self) -> Option { + let chosen = self.chosen.as_ref()?; + match self + .listed + .iter() + .find(|key| key.credential == chosen.credential) + { + Some(key) => Some(key.fingerprint.clone()), + None => PublicKeyText::parse(&chosen.public_key) + .ok() + .map(|public| public.fingerprint().to_owned()), + } + } + pub(super) fn choose_listed(&mut self, index: usize, cx: &mut Context) { + if let Some(key) = self.listed.get(index).cloned() { + self.choose(&key, cx); + } + } + fn choose(&mut self, key: &StoredKey, cx: &mut Context) { + self.chosen = Some(VaultKeyChoice { + credential: key.credential, + public_key: key.public_key.as_str().to_owned(), + }); + cx.emit(VaultKeyEvent::Chosen); + cx.notify(); + } + + /// Reads the vault's keys again. + pub(super) fn refresh(&mut self, window: &mut Window, cx: &mut Context) { + self.listing = Listing::Loading; + let list = self.keys.list(); + let epoch = self.begin(); + self.task = Some(cx.spawn_in(window, async move |this, cx| { + let result = list.await; + let _ = this.update(cx, |field, cx| { + if field.epoch == epoch { + field.listing = match result { + Ok(keys) => { + field.listed = keys; + Listing::Ready + } + Err(error) => Listing::Unavailable(describe(&error)), + }; + cx.notify(); + } + }); + })); + } + + /// Stores a new Ed25519 key and chooses it. + pub(super) fn create( + &mut self, + fallback_label: &str, + window: &mut Window, + cx: &mut Context, + ) { + let label = self.label_or(fallback_label, cx); + let stored = self.keys.generate(label); + self.store(stored, window, cx); + } + + /// Stores the key file at `path`, asking for its passphrase when it has one. + pub(super) fn import( + &mut self, + fallback_label: &str, + path: PathBuf, + window: &mut Window, + cx: &mut Context, + ) { + let label = self.label_or(fallback_label, cx); + let text = self.passphrase.read(cx).value().to_string(); + let passphrase = (!text.is_empty()).then(|| Secret::new(text)); + let stored = self.keys.import(label, KeySource::File(path), passphrase); + self.store(stored, window, cx); + } + + fn label_or(&self, fallback: &str, cx: &Context) -> String { + let typed = self.label.read(cx).value().trim().to_owned(); + if typed.is_empty() { + fallback.to_owned() + } else { + typed + } + } + + fn begin(&mut self) -> u64 { + self.epoch = self.epoch.wrapping_add(1); + self.epoch + } + + fn store( + &mut self, + stored: BoxFuture<'static, Result>, + window: &mut Window, + cx: &mut Context, + ) { + let epoch = self.begin(); + self.task = Some(cx.spawn_in(window, async move |this, cx| { + let result = stored.await; + let _ = this.update_in(cx, |field, window, cx| { + if field.epoch != epoch { + return; + } + match result { + Ok(key) => { + field.listed.push(key.clone()); + field.listing = Listing::Ready; + field.passphrase_needed = false; + field + .passphrase + .update(cx, |input, cx| input.set_value("", window, cx)); + field.choose(&key, cx); + } + Err(error) => { + if matches!( + error, + VaultSshKeysError::Key(KeyImportError::PassphraseRequired) + | VaultSshKeysError::Key(KeyImportError::WrongPassphrase) + ) { + field.passphrase_needed = true; + cx.emit(VaultKeyEvent::PassphraseAsked); + } + cx.emit(VaultKeyEvent::Failed(describe(&error))); + } + } + cx.notify(); + }); + })); + } +} + +/// What the person reads when the vault could not serve a key operation. +fn describe(error: &VaultSshKeysError) -> String { + match error { + VaultSshKeysError::Locked => "Unlock the vault to use its keys.".to_owned(), + VaultSshKeysError::Offline => "The vault is offline.".to_owned(), + VaultSshKeysError::Unavailable => "No vault is available.".to_owned(), + VaultSshKeysError::Key(KeyImportError::PassphraseRequired) => { + "The key file is protected: enter its passphrase and import again.".to_owned() + } + other => other.to_string(), + } +} diff --git a/crates/connections/src/facts_presentation.rs b/crates/connections/src/facts_presentation.rs index aa88636b..d813e91b 100644 --- a/crates/connections/src/facts_presentation.rs +++ b/crates/connections/src/facts_presentation.rs @@ -1,28 +1,57 @@ //! Images and embedded fallbacks are shared presentation, never raw fact acquisition. use gpui_kit::{Image, ImageFormat}; -use nocterm_connections_model::{observation::CountryCode, profiles::Profile}; +use nocterm_connections_model::{ + observation::{CountryCode, FlagBytes}, + profiles::Profile, +}; use nocterm_connections_store::FlagState; use std::{cell::RefCell, collections::HashMap, sync::Arc}; #[derive(Default)] pub(crate) struct FactsPresentation { - images: RefCell>>>, + images: RefCell>, + icons: RefCell>>, } impl FactsPresentation { pub(crate) fn flag(&self, code: CountryCode, state: Option) -> Option> { - let state = state?; - if matches!(state, FlagState::Requested | FlagState::Loading) { - return None; + let mut images = self.images.borrow_mut(); + let source = match state { + Some(FlagState::Ready(bytes)) => Some(bytes), + Some(FlagState::Unavailable) => None, + // A new attempt must not reuse its previous result or fallback. + None | Some(FlagState::Requested | FlagState::Loading) => { + images.remove(&code); + return None; + } + }; + if let Some(cached) = images.get(&code) + && cached.same_source(source.as_ref()) + { + return cached.image.clone(); } - if let Some(image) = self.images.borrow().get(&code) { - return image.clone(); + let bytes = source.as_ref().map_or_else( + || crate::geo::builtin_flag(code.as_str()), + |bytes| Some(bytes.bytes().to_vec()), + ); + let byte_cost = bytes.as_ref().map_or(0, Vec::len) + + source.as_ref().map_or(0, |bytes| bytes.bytes().len()); + // Installation-scoped, at most 64 entries and 4 MiB of retained SVG bytes. + // Clearing the small cache on saturation also evicts obsolete country choices. + images.remove(&code); + if images.len() >= 64 + || images.values().map(|image| image.byte_cost).sum::() + byte_cost + > 4 * 1024 * 1024 + { + images.clear(); } - let bytes = match state { - FlagState::Ready(bytes) => Some(bytes.bytes().to_vec()), - FlagState::Unavailable => crate::geo::builtin_flag(code.as_str()), - FlagState::Requested | FlagState::Loading => None, - }; let image = bytes.map(|bytes| Arc::new(Image::from_bytes(ImageFormat::Svg, bytes))); - self.images.borrow_mut().insert(code, image.clone()); + images.insert( + code, + FlagImage { + source, + image: image.clone(), + byte_cost, + }, + ); image } pub(crate) fn icon( @@ -40,10 +69,37 @@ impl FactsPresentation { .as_deref() .filter(|color| crate::os::is_valid_color(color)) .unwrap_or(os.color); - Some(Arc::new(Image::from_bytes( + let rgb = crate::os::rgb(color)?; + let mut icons = self.icons.borrow_mut(); + let key = (os.id, rgb); + if let Some(image) = icons.get(&key) { + return Some(image.clone()); + } + // OS catalog identity and validated RGB are the complete rendering inputs. + if icons.len() >= 256 { + icons.clear(); + } + let image = Arc::new(Image::from_bytes( ImageFormat::Svg, crate::os::svg(os, color), - ))) + )); + icons.insert(key, image.clone()); + Some(image) + } +} +struct FlagImage { + // Retaining the source makes pointer equality safe even after raw-cache eviction. + source: Option, + image: Option>, + byte_cost: usize, +} +impl FlagImage { + fn same_source(&self, source: Option<&FlagBytes>) -> bool { + match (self.source.as_ref(), source) { + (Some(old), Some(new)) => std::ptr::eq(old.bytes(), new.bytes()), + (None, None) => true, + _ => false, + } } } #[cfg(test)] diff --git a/crates/connections/src/facts_presentation/tests.rs b/crates/connections/src/facts_presentation/tests.rs index 411638d9..58b16e87 100644 --- a/crates/connections/src/facts_presentation/tests.rs +++ b/crates/connections/src/facts_presentation/tests.rs @@ -25,3 +25,70 @@ fn embedded_fallback_waits_until_raw_negative_result() { .is_none() ); } + +#[test] +fn ready_source_changes_and_retries_replace_the_presented_flag() { + let cache = FactsPresentation::default(); + let code = CountryCode::new("de").unwrap(); + let ready = || FlagState::Ready(FlagBytes::new(b"".to_vec()).unwrap()); + let old = cache.flag(code, Some(ready())).unwrap(); + let changed = cache.flag(code, Some(ready())).unwrap(); + assert!( + !Arc::ptr_eq(&old, &changed), + "replacement raw source refreshes presentation" + ); + assert!(cache.flag(code, Some(FlagState::Loading)).is_none()); + let fallback = cache.flag(code, Some(FlagState::Unavailable)).unwrap(); + assert!(!Arc::ptr_eq(&changed, &fallback)); + let current = cache.flag(code, Some(ready())).unwrap(); + assert!(!Arc::ptr_eq(&fallback, ¤t)); +} + +#[test] +fn projected_flags_evict_old_countries_at_the_byte_budget() { + let cache = FactsPresentation::default(); + let first_code = CountryCode::new("aa").unwrap(); + let raw = FlagBytes::new(vec![b' '; 256 * 1024]).unwrap(); + let first = cache + .flag(first_code, Some(FlagState::Ready(raw.clone()))) + .unwrap(); + for second in b'b'..=b'j' { + let code = CountryCode::new(&format!("a{}", char::from(second))).unwrap(); + cache.flag(code, Some(FlagState::Ready(raw.clone()))); + } + let revisited = cache.flag(first_code, Some(FlagState::Ready(raw))).unwrap(); + assert!(!Arc::ptr_eq(&first, &revisited)); +} + +#[test] +fn projected_icons_share_canonical_color_and_evict_old_choices() { + let cache = FactsPresentation::default(); + let mut profile = nocterm_connections_model::profiles::Profile { + id: nocterm_connections_model::test_support::profile_id_for("icons"), + name: "icons".into(), + target: nocterm_session::Target::new("ci", "icons", 22), + description: String::new(), + options: Default::default(), + auth: Default::default(), + group: None, + credential: None, + launch: None, + icon: Some("ubuntu".into()), + icon_color: Some("#abc".into()), + country: None, + jump_hosts: Vec::new(), + }; + let first = cache.icon(&profile, None).unwrap(); + profile.icon_color = Some("#AABBCC".into()); + assert!(Arc::ptr_eq(&first, &cache.icon(&profile, None).unwrap())); + for color in 0..256 { + profile.icon_color = Some(format!("#{color:06x}")); + cache.icon(&profile, None); + } + profile.icon_color = Some("#abc".into()); + assert!(!Arc::ptr_eq(&first, &cache.icon(&profile, None).unwrap())); + profile.icon_color = Some("invalid".into()); + let default = cache.icon(&profile, None).unwrap(); + profile.icon_color = None; + assert!(Arc::ptr_eq(&default, &cache.icon(&profile, None).unwrap())); +} diff --git a/crates/connections/src/installed.rs b/crates/connections/src/installed.rs index e59ceba7..42da6e24 100644 --- a/crates/connections/src/installed.rs +++ b/crates/connections/src/installed.rs @@ -5,10 +5,10 @@ use nocterm_connections_model::{ observation::CountryCode, profiles::{Profile, ProfileId}, }; -use nocterm_connections_store::{FactsEvent, FactsPrepared, FactsService}; +use nocterm_connections_store::{FactsEvent, FactsPrepared, FactsService, ProfileReplication}; use nocterm_store::{SettingsHandle, ShutdownCoordinator, StoreError}; use nocterm_ui::UiReady; -use std::{rc::Rc, sync::Arc}; +use std::{cell::OnceCell, rc::Rc, sync::Arc}; #[derive(Clone)] pub struct Installed(Rc); struct Installation { @@ -18,6 +18,8 @@ struct Installation { settings: SettingsHandle, presentation: Rc, sources: Rc, + /// Created after this installation, once the application knows the account. + replication: OnceCell>, _subscriptions: Vec, } impl Installed { @@ -84,6 +86,7 @@ impl Installed { settings, presentation: Rc::new(Default::default()), sources, + replication: OnceCell::new(), _subscriptions: subscriptions, }))) } @@ -93,6 +96,20 @@ impl Installed { pub fn facts(&self) -> &Entity { &self.0.facts } + /// Tells the views which replication keeps the saved connections in step with an + /// account, so they show whether the list may be changed. Only the first call has an + /// effect. + pub fn bind_replication(&self, replication: Entity) { + let _ = self.0.replication.set(replication); + } + /// Tells `observer` about every session of a saved connection as it comes up. Only the + /// first call has an effect. + pub fn bind_host_observer(&self, observer: Rc) { + self.0.sources.bind_observer(observer); + } + pub(crate) fn replication(&self) -> Option<&Entity> { + self.0.replication.get() + } pub fn settings(&self) -> &SettingsHandle { &self.0.settings } @@ -163,3 +180,22 @@ pub fn install( cx, ) } +/// Installs connections that live only in memory, for a window whose list something else +/// keeps (the mirror of an account). It registers no settings, no key bindings and nothing +/// for shutdown: the owner of the list saves it, and `install` already did the rest for the +/// application. +pub fn install_volatile( + prepared: PreparedConnections, + ready: &UiReady, + cx: &mut App, +) -> Result { + let (connections, inputs, facts) = prepared.into_owners(); + Installed::from_owners( + connections, + facts, + inputs, + ready.settings().clone(), + &ShutdownCoordinator::default(), + cx, + ) +} diff --git a/crates/connections/src/lib.rs b/crates/connections/src/lib.rs index ac0ab302..dfe4d766 100644 --- a/crates/connections/src/lib.rs +++ b/crates/connections/src/lib.rs @@ -10,7 +10,7 @@ //! Opening a connection asks the workspace for a session //! ([`Workspace::open_session`]); this crate never names the terminal. //! -//! Owns: views, editor inputs, session-intent conversion and detected-server image presentation and weak source projection. +//! Owns: views (the panel and the strip for the account's state, from the bound replication; the list may be changed only while the copy is in step with the account; in a hub window, the editor's Agent access switch of a saved server, which first opens a session without a tab with the connection's saved credentials when none is open, and the agent access state on each server's row), editor inputs and their shared `ConnectionInputs`, the in-memory installation `install_volatile` (no settings, key bindings or shutdown registration), session-intent conversion and detected-server image presentation and weak source projection. //! Does not own: profile/recent publication, committed authority, profile formats/rules, files, native defaults/IDs/clock, session transport, //! credentials, remote OS acquisition, DNS, HTTP, flag files or //! persistence worker lifecycle. @@ -18,19 +18,28 @@ //! ProfileIds, ConnectionClock, FactsService and original terminal capability receipts. //! Ports provided: ConnectionDirectory, checked original-workspace parent //! SessionSpec resolution for app composition and the core `connections.servers` -//! contribution (`plugin::CORE`). Settings section: none. +//! contribution (`plugin::CORE`); `HostObserver`, told once per freshly connected session (the application supplies it through `Installed::bind_host_observer`); `ServerAgentAccess`, supplied through `EditorInputs::with_agent_access` by a hub window; `SavedPasswords`, the window's vault the editor keeps a typed password in (`EditorInputs::with_passwords`). Settings section: none. //! Globals: none. Installed retains exact owners/settings and observer lifetimes. //! Invariants: profiles publish after physical success; independent recents survive //! profile failure; derived recent effects require a successful profile projection. //! Connections aliases the actual service entity; windows/actions use supplied installation. +//! Sidebar projections retain positions until profile, replication or filter +//! changes; only visible rows own short presentation and drag captures. OS/flag images +//! have installation-scoped bounded caches keyed by rendering inputs and retained raw source. //! Raw facts/jobs belong to the supplied actual service entity. Original Item //! capability receipts retain session authority independently of window projection. //! Workspace recovery compares original profile, destination and ordered resolved //! route before opening; it never supplies authentication from project metadata. +mod agent_access; mod installed; mod session_intents; -pub use installed::{Installed, install}; +pub use agent_access::{AgentAccessRefused, AgentAccessState, AgentServer, ServerAgentAccess}; +mod saved_password; +pub use installed::{Installed, install, install_volatile}; +pub use saved_password::{ + NoSavedPasswords, PasswordNotKept, SavedPassword, SavedPasswords, VaultPlace, +}; pub use session_intents::{connect, resolve_parent_spec, spec_for_profile}; mod country_policy; mod directory; @@ -43,10 +52,13 @@ mod panel; pub mod plugin; mod prepared; mod probe_sources; -pub use prepared::{ConnectionWriters, EditorInputs, PreparedConnections}; +pub use prepared::{ConnectionInputs, ConnectionWriters, EditorInputs, PreparedConnections}; +pub use probe_sources::HostObserver; #[cfg(test)] mod test_support; +#[cfg(test)] +mod volatile_tests; use gpui_kit::{Context, Window, prelude::*}; use nocterm_workspace::{PanelRegistration, RegistrationError, Workspace}; diff --git a/crates/connections/src/panel.rs b/crates/connections/src/panel.rs index 44e3fc46..d5e52f55 100644 --- a/crates/connections/src/panel.rs +++ b/crates/connections/src/panel.rs @@ -8,7 +8,7 @@ use gpui_kit::{ MouseButton, SharedString, Subscription, WeakEntity, Window, base::TestSupportExt as _, component::{ - ActiveTheme as _, Icon, Sizable as _, StyledExt as _, WindowExt as _, + ActiveTheme as _, Disableable as _, Icon, Sizable as _, StyledExt as _, WindowExt as _, button::{Button, ButtonVariants as _}, dialog::DialogFooter, h_flex, @@ -26,10 +26,15 @@ use crate::{ os::{self, Os}, session_intents::open_profile, }; +use account_state::AccountState; +pub(crate) mod account_state; +mod folder; mod group_actions; mod row; +mod sections; use group_actions::{GroupAction, run_group_action}; +use sections::ConnectionList; /// A server's icon: `os` in `color` (its brand colour when none), or the /// generic server icon in `color` (`fallback` when none). @@ -96,6 +101,7 @@ pub struct ConnectionsPanel { /// Folders the user folded away. collapsed: HashSet, renaming: Option, + listing: ConnectionList, _subscriptions: Vec, } @@ -107,11 +113,12 @@ impl ConnectionsPanel { cx: &mut Context, ) -> Self { let filter = cx.new(|cx| InputState::new(window, cx).placeholder("Filter")); - let subscriptions = vec![ + let mut subscriptions = vec![ cx.observe_in( installed.connections(), window, |this, connections, window, cx| { + this.listing.invalidate(); // The group may have been deleted or ungrouped while being renamed. if let Some(rename) = &this.renaming && !connections.read(cx).profiles(cx).has_group(&rename.group) @@ -128,12 +135,30 @@ impl ConnectionsPanel { // Detected systems and countries, and flags as they load. cx.observe(installed.facts(), |_, _, cx| cx.notify()), cx.subscribe_in(&filter, window, |this, _, event, window, cx| match event { - InputEvent::Change => cx.notify(), + InputEvent::Change => { + this.listing.invalidate(); + cx.notify(); + } // Enter opens the only match. InputEvent::PressEnter { .. } => this.open_single_match(window, cx), _ => {} }), ]; + // Whether the list may be changed follows the account, not only the profiles. + if let Some(replication) = installed.replication() { + subscriptions.push(cx.observe(replication, |this, _, cx| { + this.listing.invalidate(); + cx.notify(); + })); + } + // The rows show what the hub's agents found on each server. + if let Some(access) = installed.inputs().agent_access() { + let panel = cx.weak_entity(); + let changed = move |cx: &mut App| { + let _ = panel.update(cx, |_, cx| cx.notify()); + }; + subscriptions.push(access.observe(Box::new(changed), cx)); + } Self { installed, workspace, @@ -141,10 +166,16 @@ impl ConnectionsPanel { filter, collapsed: HashSet::new(), renaming: None, + listing: ConnectionList::default(), _subscriptions: subscriptions, } } + /// Whether the list may be changed now: see [`account_state`]. + fn editable(&self, cx: &App) -> bool { + AccountState::of(&self.installed, cx).editable() + } + fn filter_text(&self, cx: &App) -> String { self.filter.read(cx).value().to_string() } @@ -163,6 +194,22 @@ impl ConnectionsPanel { open_profile(&self.installed, &self.workspace, &profile, window, cx); } + /// Opens every connection of `group`, each in its tab. + fn open_group(&mut self, group: &str, window: &mut Window, cx: &mut Context) { + let members: Vec<_> = self + .installed + .connections() + .read(cx) + .profiles(cx) + .iter() + .filter(|profile| profile.group.as_deref() == Some(group)) + .cloned() + .collect(); + for profile in &members { + open_profile(&self.installed, &self.workspace, profile, window, cx); + } + } + fn open_single_match(&mut self, window: &mut Window, cx: &mut Context) { let filter = self.filter_text(cx); let matches: Vec = self @@ -180,6 +227,9 @@ impl ConnectionsPanel { } fn edit(&mut self, id: ProfileId, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } let profile = self .installed .connections() @@ -199,6 +249,9 @@ impl ConnectionsPanel { } fn confirm_delete(&mut self, id: ProfileId, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } let Some(name) = self .installed .connections() @@ -240,17 +293,22 @@ impl ConnectionsPanel { }); } - fn confirm_delete_group(&mut self, group: String, window: &mut Window, cx: &mut Context) { - // The delete only applies while the group still holds these connections. - let members: Vec = self - .installed - .connections() - .read(cx) - .profiles(cx) - .in_group(Some(&group), "") + /// The connections in folder `group`. + fn folder_members(&self, group: &str, cx: &App) -> Vec { + let profiles = self.installed.connections().read(cx).profiles(cx); + profiles + .in_group(Some(group), "") .iter() .map(|profile| profile.id) - .collect(); + .collect() + } + + fn confirm_delete_group(&mut self, group: String, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } + // The delete only applies while the group still holds these connections. + let members = self.folder_members(&group, cx); let count = members.len(); let panel = cx.weak_entity(); let connections = self.installed.connections().clone(); @@ -309,6 +367,9 @@ impl ConnectionsPanel { } fn start_rename(&mut self, group: &str, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } if self.renaming.as_ref().is_some_and(|r| r.group == group) { return; } @@ -325,6 +386,7 @@ impl ConnectionsPanel { this.finish_rename(true, false, window, cx) }), ]; + self.listing.invalidate(); self.renaming = Some(GroupRename { group: group.to_owned(), input, @@ -345,6 +407,7 @@ impl ConnectionsPanel { let Some(GroupRename { group, input, .. }) = self.renaming.take() else { return; }; + self.listing.invalidate(); if restore_focus { window.focus(&self.focus_handle, cx); } @@ -364,8 +427,9 @@ impl ConnectionsPanel { } else { self.collapsed.remove(&to); } + let members = self.folder_members(&group, cx); let renamed = self.installed.connections().update(cx, |connections, cx| { - connections.rename_group(group.clone(), to.clone(), cx) + connections.rename_group_of(members, group.clone(), to.clone(), cx) }); cx.spawn_in(window, async move |this, cx| { let result = renamed.await; @@ -401,6 +465,7 @@ impl ConnectionsPanel { if self.renaming.as_ref().is_some_and(|r| r.group == group) { return; } + self.listing.invalidate(); if !self.collapsed.remove(group) { self.collapsed.insert(group.to_owned()); } @@ -416,6 +481,9 @@ impl ConnectionsPanel { cx: &mut Context, ) { cx.stop_propagation(); + if !self.editable(cx) { + return; + } let moved = self.installed.connections().update(cx, |connections, cx| { connections.place_profile(dragged.0, group, before, cx) }); @@ -435,216 +503,43 @@ impl ConnectionsPanel { .detach(); } - #[expect(clippy::too_many_lines, reason = "predates the limit")] - fn render_group_header( - &self, - group: &str, - collapsed: bool, - cx: &mut Context, - ) -> impl IntoElement { - let theme = cx.theme(); - let name = group.to_owned(); - let drop_group = name.clone(); - let renaming = self.renaming.as_ref().filter(|r| r.group == group); - let label = match renaming { - Some(rename) => { - let group = name.clone(); - div() - .id(SharedString::from(format!("group-rename-{group}"))) - .test_support() - .flex_1() - .min_w_0() - .child(Input::new(&rename.input).small()) - .on_click(|_: &ClickEvent, _, cx| cx.stop_propagation()) - .on_key_down( - cx.listener(|this, event: &gpui_kit::KeyDownEvent, window, cx| { - if event.keystroke.key == "escape" { - cx.stop_propagation(); - this.finish_rename(false, true, window, cx); - } - }), - ) - .into_any_element() - } - None => { - let group = name.clone(); - div() - .id(SharedString::from(format!("group-name-{name}"))) - .test_support() - .flex_1() - .min_w_0() - .truncate() - .child(name.clone()) - // Keep the header from also treating the second click as a toggle. - .on_mouse_down(MouseButton::Left, |event, _, cx| { - if event.click_count == 2 { - cx.stop_propagation(); - } - }) - .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { - if event.click_count() == 2 { - // Undo the first click's toggle, then edit. - cx.stop_propagation(); - this.toggle_group(&group, cx); - this.start_rename(&group, window, cx); - } - })) - .into_any_element() - } - }; - let toggle_group = name.clone(); - let delete_group = name.clone(); - h_flex() - .id(SharedString::from(format!("group-{group}"))) - .test_support() - .group(GROUP_HEADER) - .gap_1() - .mx_1() - .px_1() - .py_1() - .rounded(theme.radius) - .cursor_pointer() - .text_xs() - .font_semibold() - .text_color(theme.muted_foreground) - .hover(|row| row.bg(theme.sidebar_accent)) - .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) - .on_drop( - cx.listener(move |this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, Some(drop_group.clone()), None, window, cx) - }), - ) - .child( - Icon::new(if collapsed { - IconName::ChevronRight - } else { - IconName::ChevronDown - }) - .xsmall(), - ) - .child( - Icon::new(if collapsed { - IconName::FolderClosed - } else { - IconName::FolderOpen - }) - .small(), - ) - .child(label) - .when(renaming.is_none(), |header| { - header.child( - h_flex() - .invisible() - .group_hover(GROUP_HEADER, |buttons| buttons.visible()) - .child( - Button::new(SharedString::from(format!("group-toggle-{group}"))) - .ghost() - .xsmall() - .icon(if collapsed { - IconName::ChevronRight - } else { - IconName::ChevronDown - }) - .tooltip(if collapsed { "Expand" } else { "Collapse" }) - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - cx.stop_propagation(); - this.toggle_group(&toggle_group, cx); - })), - ) - .child( - Button::new(SharedString::from(format!("group-delete-{group}"))) - .ghost() - .xsmall() - .icon(IconName::Trash) - .tooltip("Delete group") - .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { - cx.stop_propagation(); - this.confirm_delete_group(delete_group.clone(), window, cx); - })), - ), - ) - }) - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.toggle_group(&name, cx))) - } - - fn render_list(&self, cx: &mut Context) -> impl IntoElement { + fn render_list(&mut self, cx: &mut Context) -> impl IntoElement { let filter = self.filter_text(cx); - let filtering = !filter.trim().is_empty(); - let connections = self.installed.connections().read(cx); - let profiles = connections.profiles(cx); - - let (top, groups) = profiles.grouped(&filter); - let top: Vec<_> = top.into_iter().cloned().collect(); - let groups: Vec<_> = groups - .into_iter() - .map(|(name, members)| (name, members.into_iter().cloned().collect::>())) - .collect(); - let empty = profiles.is_empty() && groups.is_empty(); - let nothing_matches = top.is_empty() && groups.is_empty(); - - let mut list = v_flex().gap_px().py_1().child( - h_flex() - .id("connections-ungrouped") - .test_support() - .mx_1() - .px_2() - .py_1() - .rounded(cx.theme().radius) - .gap_1() - .text_xs() - .text_color(cx.theme().muted_foreground) - .child(Icon::new(IconName::Server).small()) - .child("Ungrouped") - .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) - .on_drop(cx.listener(|this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, None, None, window, cx) - })), - ); - for profile in &top { - list = list.child(self.render_row(profile, cx)); - } - list = list.child(self.render_append_zone(None, cx)); - for (group, members) in &groups { - // A filter shows every match, folded or not. - let collapsed = !filtering && self.collapsed.contains(group); - list = list.child(self.render_group_header(group, collapsed, cx)); - if !collapsed { - let drop_group = group.clone(); - let mut folder = v_flex() - .id(SharedString::from(format!("group-members-{group}"))) - .test_support() - .pl_3() - .gap_px() - .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) - .on_drop( - cx.listener(move |this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, Some(drop_group.clone()), None, window, cx) - }), - ); - for profile in members { - folder = folder.child(self.render_row(profile, cx)); - } - list = list.child(folder.child(self.render_append_zone(Some(group), cx))); - } + let editable = self.editable(cx); + let profiles = self.installed.connections().read(cx).profiles(cx); + let rename = self.renaming.as_ref().map(|rename| { + ( + rename.group.as_str(), + rename.input.read(cx).focus_handle(cx), + ) + }); + self.listing + .prepare(profiles, editable, &filter, &self.collapsed, rename); + if self.listing.empty() { + return self.render_empty(cx).into_any_element(); } - - if empty { - self.render_empty(cx).into_any_element() - } else if nothing_matches { - div() + if self.listing.nothing_matches() { + return div() .p_3() .text_sm() .text_color(cx.theme().muted_foreground) .child("No connection matches.") - .into_any_element() - } else { - div() - .id("connections-list") - .size_full() - .overflow_y_scroll() - .child(list) - .into_any_element() + .into_any_element(); } + let rows = self.listing.rows(); + div() + .id("connections-list") + .test_support() + .size_full() + .py_1() + .child( + gpui_kit::list( + self.listing.scroll(), + cx.processor(move |this, index: usize, _, cx| rows[index].render(this, cx)), + ) + .size_full(), + ) + .into_any_element() } fn render_empty(&self, cx: &mut Context) -> impl IntoElement { @@ -658,6 +553,7 @@ impl ConnectionsPanel { .child( Button::new("empty-new-connection") .small() + .disabled(!self.editable(cx)) .icon(IconName::ServerPlus) .label("New Connection") .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { @@ -704,6 +600,9 @@ impl Render for ConnectionsPanel { .persistence_error(cx) .map(|error| error.to_string()) }); + let account = AccountState::of(&self.installed, cx); + let editable = account.editable(); + let notice = account.notice(); let theme = cx.theme(); let danger = theme.danger; @@ -716,17 +615,22 @@ impl Render for ConnectionsPanel { .px_2() .pb_1() .child( - div().flex_1().child( - Input::new(&self.filter) - .small() - .cleanable(true) - .prefix(Icon::new(IconName::Search).small()), - ), + div() + .id("connections-filter") + .test_support() + .flex_1() + .child( + Input::new(&self.filter) + .small() + .cleanable(true) + .prefix(Icon::new(IconName::Search).small()), + ), ) .child( Button::new("new-connection") .ghost() .small() + .disabled(!editable) .icon(IconName::ServerPlus) .tooltip("New Connection") .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { @@ -740,6 +644,9 @@ impl Render for ConnectionsPanel { })), ), ) + .when_some(notice, |panel, notice| { + panel.child(account_state::strip(¬ice, cx)) + }) .when_some(load_error, |panel, error| { panel.child( div() diff --git a/crates/connections/src/panel/account_state.rs b/crates/connections/src/panel/account_state.rs new file mode 100644 index 00000000..b8ad0c4d --- /dev/null +++ b/crates/connections/src/panel/account_state.rs @@ -0,0 +1,128 @@ +//! What the state of the account that keeps the list lets the user do, and the strip above +//! the list that says why something is unavailable. +//! +//! Without an account the list is only this computer's and always editable. With one, the +//! list is a copy: a change that is not delivered to the account is lost, so the list may be +//! changed only while the copy is in step with it. +use super::*; +use nocterm_connections_store::{ReplicationFailure, ReplicationStatus}; +use nocterm_workspace::{UnlockVault, request_sign_in}; + +/// Where the copy of the account's list stands, read from the bound replication; none +/// without an account. +pub(crate) struct AccountState(Option); + +impl AccountState { + pub(crate) fn of(installed: &Installed, cx: &App) -> Self { + Self( + installed + .replication() + .map(|replication| replication.read(cx).status().clone()), + ) + } + + /// Whether the list may be changed: always without an account, and with one only while + /// the copy is in step with it, because a change that stays here is lost. + pub(crate) fn editable(&self) -> bool { + self.0.as_ref().is_none_or(editable) + } + + /// What to tell the user about the account above the list. + pub(crate) fn notice(&self) -> Option { + self.0.as_ref().and_then(notice) + } +} + +/// Whether the list may be changed while the copy is in the state `status`. +pub(crate) fn editable(status: &ReplicationStatus) -> bool { + matches!( + status, + ReplicationStatus::Synced | ReplicationStatus::Syncing + ) +} + +/// What the user needs to hear about the account above the list. +#[derive(Clone, Debug)] +pub(crate) enum AccountNotice { + /// The keys of the account are closed. + Locked, + /// The server cannot be reached; the list shown is the last one received. + Offline, + /// The account is signed out, so the list is empty. + SignedOut, + /// A connection was saved by a newer version of the application. + NeedsNewerClient, + Failed(ReplicationFailure), +} + +/// The notice for a copy in the state `status`; none while it is in step. +pub(crate) fn notice(status: &ReplicationStatus) -> Option { + match status { + ReplicationStatus::Synced | ReplicationStatus::Syncing => None, + ReplicationStatus::Detached => Some(AccountNotice::SignedOut), + ReplicationStatus::Offline => Some(AccountNotice::Offline), + ReplicationStatus::Locked => Some(AccountNotice::Locked), + ReplicationStatus::NeedsNewerClient => Some(AccountNotice::NeedsNewerClient), + ReplicationStatus::Failed(failure) => Some(AccountNotice::Failed(failure.clone())), + } +} + +impl AccountNotice { + fn message(&self) -> String { + match self { + Self::Locked => "Account locked".to_owned(), + Self::Offline => "Server unreachable — showing the last list, read-only".to_owned(), + Self::SignedOut => "Signed out".to_owned(), + Self::NeedsNewerClient => { + "Some connections need a newer Nocterm — the list is read-only".to_owned() + } + Self::Failed(failure) => format!("Could not synchronize: {failure}"), + } + } + + /// The label of the button that resolves the notice, if the user can. + fn remedy(&self) -> Option<&'static str> { + match self { + Self::Locked => Some("Unlock"), + Self::SignedOut => Some("Sign in"), + _ => None, + } + } + + fn resolve(&self, window: &mut Window, cx: &mut App) { + match self { + Self::Locked => window.dispatch_action(Box::new(UnlockVault), cx), + Self::SignedOut => { + request_sign_in(window, cx); + } + _ => {} + } + } +} + +/// The strip above the list for `notice`. +pub(super) fn strip(notice: &AccountNotice, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + let remedy = notice.clone(); + h_flex() + .id("connections-account-notice") + .test_support() + .gap_2() + .mx_2() + .mb_1() + .px_2() + .py_1() + .rounded(theme.radius) + .bg(theme.muted) + .text_xs() + .text_color(theme.muted_foreground) + .child(div().flex_1().min_w_0().child(notice.message())) + .when_some(notice.remedy(), |strip, label| { + strip.child( + Button::new("connections-account-action") + .xsmall() + .label(label) + .on_click(move |_: &ClickEvent, window, cx| remedy.resolve(window, cx)), + ) + }) +} diff --git a/crates/connections/src/panel/folder.rs b/crates/connections/src/panel/folder.rs new file mode 100644 index 00000000..7f89dc4d --- /dev/null +++ b/crates/connections/src/panel/folder.rs @@ -0,0 +1,132 @@ +//! The folder rows of the connections list: group header and its members. +use super::*; + +impl ConnectionsPanel { + #[expect(clippy::too_many_lines, reason = "predates the limit")] + pub(super) fn render_group_header( + &self, + group: &str, + collapsed: bool, + cx: &mut Context, + ) -> impl IntoElement { + let theme = cx.theme(); + let name = group.to_owned(); + let drop_group = name.clone(); + let renaming = self.renaming.as_ref().filter(|r| r.group == group); + let label = match renaming { + Some(rename) => { + let group = name.clone(); + div() + .id(SharedString::from(format!("group-rename-{group}"))) + .test_support() + .flex_1() + .min_w_0() + .child(Input::new(&rename.input).small()) + .on_click(|_: &ClickEvent, _, cx| cx.stop_propagation()) + .on_key_down( + cx.listener(|this, event: &gpui_kit::KeyDownEvent, window, cx| { + if event.keystroke.key == "escape" { + cx.stop_propagation(); + this.finish_rename(false, true, window, cx); + } + }), + ) + .into_any_element() + } + None => { + let group = name.clone(); + div() + .id(SharedString::from(format!("group-name-{name}"))) + .test_support() + .flex_1() + .min_w_0() + .truncate() + .child(name.clone()) + // Keep the header from also treating the second click as a toggle. + .on_mouse_down(MouseButton::Left, |event, _, cx| { + if event.click_count == 2 { + cx.stop_propagation(); + } + }) + .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { + if event.click_count() == 2 { + // Undo the first click's toggle, then edit. + cx.stop_propagation(); + this.toggle_group(&group, cx); + this.start_rename(&group, window, cx); + } + })) + .into_any_element() + } + }; + let open_group = name.clone(); + let delete_group = name.clone(); + h_flex() + .id(SharedString::from(format!("group-{group}"))) + .test_support() + .group(GROUP_HEADER) + .gap_1() + .mx_1() + .px_1() + .py_1() + .rounded(theme.radius) + .cursor_pointer() + .text_xs() + .font_semibold() + .text_color(theme.muted_foreground) + .hover(|row| row.bg(theme.sidebar_accent)) + .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) + .on_drop( + cx.listener(move |this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, Some(drop_group.clone()), None, window, cx) + }), + ) + .child( + Icon::new(if collapsed { + IconName::ChevronRight + } else { + IconName::ChevronDown + }) + .xsmall(), + ) + .child( + Icon::new(if collapsed { + IconName::FolderClosed + } else { + IconName::FolderOpen + }) + .small(), + ) + .child(label) + .when(renaming.is_none(), |header| { + header.child( + h_flex() + .invisible() + .group_hover(GROUP_HEADER, |buttons| buttons.visible()) + .child( + Button::new(SharedString::from(format!("group-open-{group}"))) + .ghost() + .xsmall() + .icon(IconName::ArrowUpRight) + .tooltip("Open every connection of the group") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.open_group(&open_group, window, cx); + })), + ) + .child( + Button::new(SharedString::from(format!("group-delete-{group}"))) + .ghost() + .xsmall() + .icon(IconName::Trash) + .tooltip("Delete group") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.confirm_delete_group(delete_group.clone(), window, cx); + })), + ), + ) + }) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.toggle_group(&name, cx))) + } +} diff --git a/crates/connections/src/panel/group_actions.rs b/crates/connections/src/panel/group_actions.rs index dbce155f..a9596fa1 100644 --- a/crates/connections/src/panel/group_actions.rs +++ b/crates/connections/src/panel/group_actions.rs @@ -17,12 +17,12 @@ pub(super) fn run_group_action( ) { let (done, key, title) = connections.update(cx, |connections, cx| match action { GroupAction::Ungroup => ( - connections.ungroup(group.clone(), cx), + connections.ungroup_of(expected, group.clone(), cx), "connections-ungroup", "Could not ungroup connections", ), GroupAction::Delete => ( - connections.delete_group(group.clone(), expected, cx), + connections.delete_group_of(group.clone(), expected, cx), "connections-delete-group", "Could not delete group", ), diff --git a/crates/connections/src/panel/row.rs b/crates/connections/src/panel/row.rs index 5d934c49..d4d07c97 100644 --- a/crates/connections/src/panel/row.rs +++ b/crates/connections/src/panel/row.rs @@ -1,28 +1,70 @@ //! The same passive server row supplies sidebar content and its drag appearance. use super::*; +use crate::agent_access::{AgentAccessState, AgentServer}; use gpui_kit::{Div, ObjectFit, base::ElementExt as _}; use nocterm_ui::{DragSource, IconName}; #[derive(Clone)] -struct RowVisual { - profile: Profile, - os: Option<&'static Os>, +pub(super) struct RowVisual { + id: ProfileId, + name: SharedString, + target: SharedString, + description: Option, + icon: Option>, + color: Option, flag: Option<(String, Arc)>, + group: Option, + tooltip: Option, + /// What the hub's agents found on the server, in a hub window that lets them sign in to it. + agents: Option, } impl RowVisual { - fn new(profile: &Profile, installed: &Installed, cx: &App) -> Self { + pub(super) fn new(profile: &Profile, installed: &Installed, cx: &App) -> Self { Self { - profile: profile.clone(), - os: installed.shown_os(profile, cx), + id: profile.id, + name: profile.name.clone().into(), + target: profile.target.to_string().into(), + description: (!profile.description.is_empty()) + .then(|| description_preview(&profile.description).into()), + icon: installed.icon(profile, cx), + color: profile.icon_color.as_deref().and_then(os::rgb), + group: profile.group.clone(), + tooltip: (!profile.description.is_empty()) + .then(|| description_tooltip(&profile.description)), flag: installed .shown_country(profile, cx) .and_then(|code| Some((code.to_uppercase(), installed.flag(&code, cx)?))), + agents: installed + .inputs() + .agent_access() + .and_then(|access| access.state(&AgentServer::of(profile), cx)), } } + /// The agent access state beside the name: quiet while agents sign in, a warning otherwise. + fn agents_badge(&self, cx: &App) -> Option> { + let state = self.agents?; + let theme = cx.theme(); + let color = if state.is_ready() { + theme.muted_foreground + } else { + theme.warning + }; + Some( + div() + .id(SharedString::from(format!("connection-agents-{}", self.id))) + .test_support() + .aria_label(state.label()) + .flex_shrink_0() + .text_xs() + .text_color(color) + .child(state.label()), + ) + } + fn buttons(&self) -> (Button, Button) { - let id = self.profile.id; + let id = self.id; ( Button::new(SharedString::from(format!("edit-{id}"))) .ghost() @@ -38,7 +80,6 @@ impl RowVisual { #[expect(clippy::too_many_lines, reason = "predates the limit")] fn render(&self, hovered: bool, actions: AnyElement, cx: &App) -> Div { let theme = cx.theme(); - let profile = &self.profile; h_flex() .w_full() .gap_2() @@ -49,20 +90,23 @@ impl RowVisual { .hover(|row| row.bg(theme.sidebar_accent)) .child( div() - .id(SharedString::from(format!( - "connection-icon-{}", - profile.id - ))) + .id(SharedString::from(format!("connection-icon-{}", self.id))) .test_support() .flex_shrink_0() - .child(server_icon( - self.os, - profile - .icon_color - .as_deref() - .filter(|color| os::is_valid_color(color)), - theme.muted_foreground, - )), + .child(match &self.icon { + Some(icon) => img(icon.clone()) + .size_4() + .flex_shrink_0() + .into_any_element(), + None => { + Icon::new(IconName::Server) + .small() + .text_color(self.color.map_or(theme.muted_foreground, |rgb| { + gpui_kit::rgb(rgb).into() + })) + .into_any_element() + } + }), ) .child( v_flex() @@ -74,21 +118,18 @@ impl RowVisual { .min_w_0() .child( div() - .id(SharedString::from(format!( - "connection-name-{}", - profile.id - ))) + .id(SharedString::from(format!("connection-name-{}", self.id))) .test_support() .text_sm() .truncate() - .child(profile.name.clone()), + .child(self.name.clone()), ) .when_some(self.flag.clone(), |line, (country, flag)| { line.child( div() .id(SharedString::from(format!( "connection-flag-{}", - profile.id + self.id ))) .test_support() .flex_shrink_0() @@ -113,32 +154,30 @@ impl RowVisual { }) }), ) - }), + }) + .children(self.agents_badge(cx)), ) .child( div() - .id(SharedString::from(format!( - "connection-target-{}", - profile.id - ))) + .id(SharedString::from(format!("connection-target-{}", self.id))) .test_support() .text_xs() .truncate() .text_color(theme.muted_foreground) - .child(profile.target.to_string()), + .child(self.target.clone()), ) - .when(!profile.description.is_empty(), |column| { + .when_some(self.description.clone(), |column, description| { column.child( div() .id(SharedString::from(format!( "connection-description-{}", - profile.id + self.id ))) .test_support() .text_xs() .truncate() .text_color(theme.muted_foreground) - .child(description_preview(&profile.description)), + .child(description), ) }), ) @@ -199,28 +238,37 @@ impl ConnectionsPanel { ) } - pub(super) fn render_row(&self, profile: &Profile, cx: &mut Context) -> impl IntoElement { - let id = profile.id; - let visual = RowVisual::new(profile, &self.installed, cx); + /// A connection's row. Without `writable` it can only be opened: no buttons, no dragging. + pub(super) fn render_row( + &self, + visual: RowVisual, + writable: bool, + cx: &mut Context, + ) -> impl IntoElement { + let id = visual.id; let source = DragSource::default(); let source_visual = visual.clone(); - let group = profile.group.clone(); + let group = visual.group.clone(); + let tooltip = visual.tooltip.clone(); let (edit, delete) = visual.buttons(); let actions = h_flex() .invisible() .group_hover(ROW_GROUP, |buttons| buttons.visible()) - .child(edit.tooltip("Edit").on_click(cx.listener( - move |this, _: &ClickEvent, window, cx| { - cx.stop_propagation(); - this.edit(id, window, cx); - }, - ))) - .child(delete.tooltip("Delete").on_click(cx.listener( - move |this, _: &ClickEvent, window, cx| { - cx.stop_propagation(); - this.confirm_delete(id, window, cx); - }, - ))) + .when(writable, |actions| { + actions + .child(edit.tooltip("Edit").on_click(cx.listener( + move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.edit(id, window, cx); + }, + ))) + .child(delete.tooltip("Delete").on_click(cx.listener( + move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.confirm_delete(id, window, cx); + }, + ))) + }) .into_any_element(); div() .id(SharedString::from(format!("connection-{id}"))) @@ -233,18 +281,19 @@ impl ConnectionsPanel { let source = source.clone(); move |bounds, window, _| source.capture(bounds, window) }) - .on_drag(DraggedProfile(id), move |_, _, _, cx| { - cx.stop_propagation(); - let visual = visual.clone(); - cx.new(|_| source.preview(move |_, cx| visual.preview(cx))) + .when(writable, |row| { + row.on_drag(DraggedProfile(id), move |_, _, _, cx| { + cx.stop_propagation(); + let visual = visual.clone(); + cx.new(|_| source.preview(move |_, cx| visual.preview(cx))) + }) + .on_drop(cx.listener( + move |this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, group.clone(), Some(id), window, cx) + }, + )) }) - .on_drop( - cx.listener(move |this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, group.clone(), Some(id), window, cx) - }), - ) - .when(!profile.description.is_empty(), |row| { - let description = description_tooltip(&profile.description); + .when_some(tooltip, |row, description| { row.tooltip(move |_, cx| { cx.new(|_| gpui_kit::component::tooltip::Tooltip::new(description.clone())) .into() diff --git a/crates/connections/src/panel/sections.rs b/crates/connections/src/panel/sections.rs new file mode 100644 index 00000000..8ecc7746 --- /dev/null +++ b/crates/connections/src/panel/sections.rs @@ -0,0 +1,228 @@ +//! Prepared sidebar positions. Saved profiles stay in their service. +use super::*; +use gpui_kit::{ListAlignment, ListState, px}; +use nocterm_connections_model::profiles::Profiles; +use std::{collections::HashMap, rc::Rc}; + +pub(super) enum Entry { + Ungrouped, + Profile { + index: usize, + id: ProfileId, + group: Option>, + writable: bool, + }, + Group(Rc, bool), + Append(Option>), +} + +impl Entry { + pub(super) fn render( + &self, + panel: &ConnectionsPanel, + cx: &mut Context, + ) -> AnyElement { + let row = match self { + Self::Ungrouped => ungrouped(cx).into_any_element(), + Self::Profile { + index, + id, + group, + writable, + } => profile_row(*index, *id, group.as_ref(), *writable, panel, cx), + Self::Group(group, collapsed) => panel + .render_group_header(group, *collapsed, cx) + .into_any_element(), + Self::Append(group) => { + let zone = panel + .render_append_zone(group.as_deref(), cx) + .into_any_element(); + match group { + Some(group) => div() + .id(SharedString::from(format!("group-members-{group}"))) + .test_support() + .pl_3() + .child(zone) + .into_any_element(), + None => div().child(zone).into_any_element(), + } + } + }; + div().pb_px().child(row).into_any_element() + } +} +fn ungrouped(cx: &mut Context) -> impl IntoElement { + h_flex() + .id("connections-ungrouped") + .test_support() + .mx_1() + .px_2() + .py_1() + .rounded(cx.theme().radius) + .gap_1() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(Icon::new(IconName::Server).small()) + .child("Ungrouped") + .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) + .on_drop(cx.listener(|this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, None, None, window, cx) + })) +} +fn profile_row( + index: usize, + id: ProfileId, + group: Option<&Rc>, + writable: bool, + panel: &ConnectionsPanel, + cx: &mut Context, +) -> AnyElement { + let profiles = panel.installed.connections().read(cx).profiles(cx); + // A frame prepared before a deferred publication observation cannot display + // a different record that moved into the same position. + let Some(profile) = profiles + .iter() + .nth(index) + .filter(|profile| profile.id == id) + else { + return div().into_any_element(); + }; + let visual = row::RowVisual::new(profile, &panel.installed, cx); + let row = panel.render_row(visual, writable, cx).into_any_element(); + div() + .when(group.is_some(), |row| row.pl_3()) + .when_some(group.cloned(), |row, group| { + row.drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) + .on_drop( + cx.listener(move |this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, Some(group.to_string()), None, window, cx) + }), + ) + }) + .child(row) + .into_any_element() +} + +pub(super) struct ConnectionList { + rows: Rc>, + scroll: ListState, + dirty: bool, + empty: bool, + nothing_matches: bool, +} +impl Default for ConnectionList { + fn default() -> Self { + Self { + rows: Rc::default(), + scroll: ListState::new(0, ListAlignment::Top, px(100.)), + dirty: true, + empty: true, + nothing_matches: false, + } + } +} +impl ConnectionList { + pub(super) fn invalidate(&mut self) { + self.dirty = true; + } + pub(super) fn rows(&self) -> Rc> { + self.rows.clone() + } + pub(super) fn scroll(&self) -> ListState { + self.scroll.clone() + } + pub(super) fn empty(&self) -> bool { + self.empty + } + pub(super) fn nothing_matches(&self) -> bool { + self.nothing_matches + } + /// `editable`: whether the list may be changed now, so its rows can be edited and moved. + pub(super) fn prepare( + &mut self, + profiles: &Profiles, + editable: bool, + filter: &str, + collapsed: &HashSet, + rename: Option<(&str, FocusHandle)>, + ) { + if !self.dirty { + return; + } + let listing = Listing::new(profiles, filter); + self.empty = profiles.is_empty(); + self.nothing_matches = listing.is_empty(); + let rows = listing.into_rows(profiles, editable, filter, collapsed); + let offset = self.scroll.logical_scroll_top(); + self.scroll.splice_focusable( + 0..self.rows.len(), + rows.iter().map(|entry| match (entry, &rename) { + (Entry::Group(group, _), Some((name, focus))) if group.as_ref() == *name => { + Some(focus.clone()) + } + _ => None, + }), + ); + // Unmeasured rows otherwise contribute zero scroll extent. A height hint lets + // native wheel/thumb jumps reach unseen rows without constructing the whole list. + self.scroll = self.scroll.clone().with_uniform_item_height(px(64.)); + self.scroll.scroll_to(offset); + self.rows = Rc::new(rows); + self.dirty = false; + } +} + +/// A temporary borrowed partition, used only when semantic list inputs change. +struct Listing<'a> { + top: Vec<&'a Profile>, + groups: Vec<(String, Vec<&'a Profile>)>, +} +impl<'a> Listing<'a> { + fn new(profiles: &'a Profiles, filter: &str) -> Self { + let (top, groups) = profiles.grouped(filter); + Self { top, groups } + } + fn is_empty(&self) -> bool { + self.top.is_empty() && self.groups.is_empty() + } + fn into_rows( + self, + profiles: &Profiles, + editable: bool, + filter: &str, + collapsed: &HashSet, + ) -> Vec { + let indices: HashMap<_, _> = profiles + .iter() + .enumerate() + .map(|(index, profile)| (profile.id, index)) + .collect(); + let profile_row = |profile: &Profile, group| Entry::Profile { + index: indices[&profile.id], + id: profile.id, + group, + writable: editable, + }; + let mut rows = vec![Entry::Ungrouped]; + rows.extend( + self.top + .into_iter() + .map(|profile| profile_row(profile, None)), + ); + rows.push(Entry::Append(None)); + for (name, members) in self.groups { + let collapsed = filter.trim().is_empty() && collapsed.contains(&name); + let group: Rc = name.into(); + rows.push(Entry::Group(group.clone(), collapsed)); + if !collapsed { + rows.extend( + members + .into_iter() + .map(|profile| profile_row(profile, Some(group.clone()))), + ); + rows.push(Entry::Append(Some(group))); + } + } + rows + } +} diff --git a/crates/connections/src/panel/tests.rs b/crates/connections/src/panel/tests.rs index aab168f9..90d382b4 100644 --- a/crates/connections/src/panel/tests.rs +++ b/crates/connections/src/panel/tests.rs @@ -18,7 +18,10 @@ fn add_servers( .unwrap(); } +mod account_state; +mod agents; mod drag; +mod list; mod rename; mod reorder; @@ -334,17 +337,21 @@ fn set_rename_value( #[gpui_kit::test] fn header_click_toggles_once_and_hover_buttons_do_not_double_fire(cx: &mut TestAppContext) { - let (handle, panel, _, _installed) = setup(cx, &[gp("a", Some("Work"))]); + let (handle, panel, opened, _installed) = setup(cx, &[gp("a", Some("Work"))]); cx.update_window(handle, |_, window, cx| { window.click("group-Work", cx); assert!(panel.read(cx).collapsed.contains("Work")); window.hover("group-Work", cx); window.render_frame(cx); - window.click("group-toggle-Work", cx); - assert!(!panel.read(cx).collapsed.contains("Work")); + window.click("group-open-Work", cx); + assert!( + panel.read(cx).collapsed.contains("Work"), + "opening the group leaves it collapsed" + ); assert!(!window.has_active_dialog(cx)); }) .unwrap(); + assert_eq!(opened.borrow().len(), 1); } fn open_group_dialog(cx: &mut TestAppContext, handle: gpui_kit::AnyWindowHandle) { @@ -512,19 +519,35 @@ fn collapsed_of(cx: &mut TestAppContext, panel: &Entity) -> Ve } #[gpui_kit::test] -fn header_toggle_button_round_trips(cx: &mut TestAppContext) { - let (handle, panel, opened, _installed) = setup(cx, &[gp("a", Some("Work"))]); - for expected in [true, false, true] { - cx.update_window(handle, |_, window, cx| { - window.hover("group-Work", cx); - window.render_frame(cx); - window.click("group-toggle-Work", cx); - window.render_frame(cx); - assert_eq!(panel.read(cx).collapsed.contains("Work"), expected); - }) - .unwrap(); - } - assert!(opened.borrow().is_empty()); +fn the_group_button_opens_every_connection_of_the_group(cx: &mut TestAppContext) { + let (handle, panel, opened, _installed) = setup( + cx, + &[ + gp("web", Some("Work")), + gp("home", Some("Personal")), + gp("db", Some("Work")), + gp("solo", None), + ], + ); + cx.update_window(handle, |_, window, cx| { + window.hover("group-Work", cx); + window.render_frame(cx); + window.click("group-open-Work", cx); + window.render_frame(cx); + assert!(!panel.read(cx).collapsed.contains("Work"), "it stays open"); + }) + .unwrap(); + let mut hosts: Vec = opened + .borrow() + .iter() + .map(|spec| spec.target.host.clone()) + .collect(); + hosts.sort(); + assert_eq!( + hosts, + ["db", "web"], + "every member of the group and nothing else" + ); } #[gpui_kit::test] diff --git a/crates/connections/src/panel/tests/account_state.rs b/crates/connections/src/panel/tests/account_state.rs new file mode 100644 index 00000000..ce1c6538 --- /dev/null +++ b/crates/connections/src/panel/tests/account_state.rs @@ -0,0 +1,170 @@ +use super::*; +use crate::test_support::{replicate, unattached}; +use nocterm_connections_model::test_support::MemoryReplica; +use nocterm_connections_store::{ReplicationFailure, ReplicationStatus}; +use nocterm_workspace::UnlockVault; +use std::{cell::Cell, rc::Rc}; + +/// Stands in for the action of the hubs crate, which this crate cannot name. It is declared in +/// a module so the actions reference does not list it twice. +mod stand_in { + gpui_kit::actions!(hubs, [SignInToServer]); +} +use stand_in::SignInToServer; + +/// How the account stands in a panel over one personal connection. +enum Account { + /// Attached to a replica that answers. + Synced, + /// Attached to a replica that cannot be reached. + Offline, + /// Attached to a replica whose keys are closed. + Locked, + /// Bound to an account that is signed out. + SignedOut, +} + +fn account( + cx: &mut TestAppContext, + state: Account, +) -> (gpui_kit::AnyWindowHandle, Entity, Profile) { + let (handle, workspace, _opened, installed) = crate::test_support::workspace(cx); + let own = gp("own", None); + cx.update(|cx| { + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(own.clone(), cx) + .now_or_never() + .unwrap() + .unwrap() + }); + }); + match state { + Account::SignedOut => { + unattached(&installed, cx); + } + state => { + let replica = Arc::new(MemoryReplica::new()); + match state { + Account::Offline => replica.set_offline(true), + Account::Locked => replica.set_locked(true), + _ => {} + } + replicate(&installed, replica, cx); + } + } + let panel = cx + .update_window(handle, |_, window, cx| { + let panel = cx.new(|cx| { + ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx) + }); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel.clone(), window, cx) + }); + window.render_frame(cx); + panel + }) + .unwrap(); + (handle, panel, own) +} + +fn has(cx: &mut TestAppContext, handle: gpui_kit::AnyWindowHandle, id: &str) -> bool { + cx.update_window(handle, |_, window, _| { + window.try_find(id.to_owned()).is_some() + }) + .unwrap() +} + +#[gpui_kit::test] +fn a_list_in_step_with_the_account_has_no_strip_and_can_be_changed(cx: &mut TestAppContext) { + let (handle, _panel, own) = account(cx, Account::Synced); + assert!(!has(cx, handle, "connections-account-notice")); + assert!(has(cx, handle, &format!("edit-{}", own.id))); + assert!(has(cx, handle, &format!("delete-{}", own.id))); +} + +#[gpui_kit::test] +fn a_locked_account_shows_an_unlock_strip_that_asks_for_the_vault(cx: &mut TestAppContext) { + let (handle, _panel, _own) = account(cx, Account::Locked); + assert!(has(cx, handle, "connections-account-notice")); + let asked = Rc::new(Cell::new(0)); + let counter = asked.clone(); + cx.update(|cx| { + cx.on_action(move |_: &UnlockVault, _| counter.set(counter.get() + 1)); + }); + cx.update_window(handle, |_, window, cx| { + window.click("connections-account-action", cx) + }) + .unwrap(); + assert_eq!(asked.get(), 1); +} + +#[gpui_kit::test] +fn a_signed_out_account_shows_a_sign_in_strip_that_asks_to_sign_in(cx: &mut TestAppContext) { + let (handle, _panel, _own) = account(cx, Account::SignedOut); + assert!(has(cx, handle, "connections-account-notice")); + let asked = Rc::new(Cell::new(0)); + let counter = asked.clone(); + cx.update(|cx| { + cx.on_action(move |_: &SignInToServer, _| counter.set(counter.get() + 1)); + }); + cx.update_window(handle, |_, window, cx| { + window.click("connections-account-action", cx) + }) + .unwrap(); + assert_eq!(asked.get(), 1); +} + +#[gpui_kit::test] +fn an_unreachable_server_is_told_without_a_remedy(cx: &mut TestAppContext) { + let (handle, _panel, _own) = account(cx, Account::Offline); + assert!(has(cx, handle, "connections-account-notice")); + assert!(!has(cx, handle, "connections-account-action")); +} + +#[gpui_kit::test] +fn outside_sync_nothing_is_added_edited_deleted_or_moved(cx: &mut TestAppContext) { + for state in [Account::Offline, Account::Locked, Account::SignedOut] { + let (handle, panel, own) = account(cx, state); + assert!(!has(cx, handle, &format!("edit-{}", own.id))); + assert!(!has(cx, handle, &format!("delete-{}", own.id))); + cx.update_window(handle, |_, window, cx| { + panel.update(cx, |panel, cx| { + panel.edit(own.id, window, cx); + panel.confirm_delete(own.id, window, cx); + panel.start_rename("Work", window, cx); + panel.confirm_delete_group("Work".into(), window, cx); + }); + crate::open_editor( + panel.read(cx).installed.clone(), + None, + panel.read(cx).workspace.clone(), + window, + cx, + ); + window.render_frame(cx); + assert!(window.try_find("dialog".to_owned()).is_none()); + }) + .unwrap(); + } +} + +#[test] +fn only_a_list_in_step_with_the_account_is_editable() { + use crate::panel::account_state::{editable, notice}; + let failed = ReplicationStatus::Failed(ReplicationFailure::Conflicts); + for status in [ReplicationStatus::Synced, ReplicationStatus::Syncing] { + assert!(editable(&status)); + assert!(notice(&status).is_none()); + } + for status in [ + ReplicationStatus::Detached, + ReplicationStatus::Offline, + ReplicationStatus::Locked, + ReplicationStatus::NeedsNewerClient, + failed, + ] { + assert!(!editable(&status), "{status:?}"); + assert!(notice(&status).is_some(), "{status:?}"); + } +} diff --git a/crates/connections/src/panel/tests/agents.rs b/crates/connections/src/panel/tests/agents.rs new file mode 100644 index 00000000..67d1665b --- /dev/null +++ b/crates/connections/src/panel/tests/agents.rs @@ -0,0 +1,72 @@ +//! In a hub window each saved server's row says what the hub's agents found there; a server +//! the agents may not sign in to, and every row of other windows, says nothing. +use super::*; +use crate::{AgentAccessState, test_support::ScriptedAgentAccess}; + +/// A panel listing one saved server, in a hub window when `access` is given. +fn panel_of( + access: Option>, + cx: &mut TestAppContext, +) -> (gpui_kit::AnyWindowHandle, Profile) { + let (handle, workspace, _, installed) = match access { + Some(access) => crate::test_support::workspace_with_agent_access(cx, access), + None => crate::test_support::workspace(cx), + }; + let profile = gp("web", None); + cx.update_window(handle, |_, window, cx| { + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + let panel = cx + .new(|cx| ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx)); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel, window, cx) + }); + }) + .unwrap(); + (handle, profile) +} + +fn badge( + handle: gpui_kit::AnyWindowHandle, + profile: &Profile, + cx: &mut TestAppContext, +) -> Option { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let id = SharedString::from(format!("connection-agents-{}", profile.id)); + window + .try_find(id) + .map(|badge| badge.label().unwrap_or_default().to_owned()) + }) + .unwrap() +} + +#[gpui_kit::test] +fn a_row_says_what_the_agents_found_on_its_server(cx: &mut TestAppContext) { + for (state, label) in [ + (AgentAccessState::Ready, "Agents ✓"), + (AgentAccessState::NoAccess, "No access"), + (AgentAccessState::HostKeyChanged, "Host key changed"), + (AgentAccessState::Reconnect, "Reconnect"), + ] { + let access = Arc::new(ScriptedAgentAccess::default()); + *access.state.lock().unwrap() = Some(state); + let (handle, profile) = panel_of(Some(access), cx); + assert_eq!(badge(handle, &profile, cx).as_deref(), Some(label)); + } +} + +#[gpui_kit::test] +fn a_server_without_agent_access_and_the_window_of_this_computer_show_none( + cx: &mut TestAppContext, +) { + let (handle, profile) = panel_of(Some(Arc::new(ScriptedAgentAccess::default())), cx); + assert_eq!(badge(handle, &profile, cx), None); + let (handle, profile) = panel_of(None, cx); + assert_eq!(badge(handle, &profile, cx), None); +} diff --git a/crates/connections/src/panel/tests/list.rs b/crates/connections/src/panel/tests/list.rs new file mode 100644 index 00000000..e233d722 --- /dev/null +++ b/crates/connections/src/panel/tests/list.rs @@ -0,0 +1,107 @@ +//! Rendering and filtering large connection folders through the public sidebar. +use super::*; + +fn profile(index: usize) -> Profile { + let name = format!("Server {index:03}"); + Profile { + id: nocterm_connections_model::test_support::profile_id_for(&name), + name, + group: Some("Work".into()), + target: nocterm_session::Target::new("ci", "host", 22), + description: "A saved host".into(), + options: Default::default(), + auth: Default::default(), + credential: None, + launch: None, + icon: Some("ubuntu".into()), + icon_color: None, + country: None, + jump_hosts: Vec::new(), + } +} + +fn rendered(window: &Window) -> Vec { + (0..256) + .filter(|index| { + window + .try_find(SharedString::from(format!( + "connection-{}", + profile(*index).id + ))) + .is_some() + }) + .collect() +} + +#[gpui_kit::test] +fn expanded_large_folders_build_visible_rows_and_open_the_filtered_last_server( + cx: &mut TestAppContext, +) { + let (handle, workspace, opened, installed) = crate::test_support::workspace(cx); + cx.update_window(handle, |_, window, cx| { + installed.connections().update(cx, |connections, cx| { + for index in 0..256 { + connections + .save_profile(profile(index), cx) + .now_or_never() + .unwrap() + .unwrap(); + } + }); + let panel = cx + .new(|cx| ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx)); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel, window, cx) + }); + window.render_frame(cx); + let materialized = rendered(window).len(); + assert!( + (1..40).contains(&materialized), + "materialized {materialized}/256 rows" + ); + assert!( + window + .try_find(SharedString::from(format!( + "connection-{}", + profile(255).id + ))) + .is_none() + ); + window.scroll( + "connections-list", + gpui_kit::ScrollDelta::Pixels(gpui_kit::point( + gpui_kit::px(0.), + gpui_kit::px(-100_000.), + )), + cx, + ); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let visible = rendered(window); + assert!( + visible.contains(&255), + "native wheel reaches the last row: {visible:?}" + ); + window.click("connections-filter", cx); + window.input("Server 255", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + assert!( + window + .try_find(SharedString::from(format!("connection-{}", profile(0).id))) + .is_none() + ); + window.click( + SharedString::from(format!("connection-{}", profile(255).id)), + cx, + ); + }) + .unwrap(); + assert_eq!(opened.borrow().len(), 1); +} diff --git a/crates/connections/src/plugin.rs b/crates/connections/src/plugin.rs index 7631c9b9..1b18128f 100644 --- a/crates/connections/src/plugin.rs +++ b/crates/connections/src/plugin.rs @@ -24,6 +24,7 @@ pub const SERVERS: SystemContribution = SystemContribution { chrome: PanelChrome::Titled, }), preview: None, + edge: None, pinned: false, }; diff --git a/crates/connections/src/prepared.rs b/crates/connections/src/prepared.rs index 76fff3be..d1e3fb63 100644 --- a/crates/connections/src/prepared.rs +++ b/crates/connections/src/prepared.rs @@ -1,5 +1,8 @@ //! Prepared documents and injected collaborators, installed without foreground native acquisition. -use crate::{Connections, FactsPrepared, ObservationDeps}; +use crate::{ + Connections, FactsPrepared, NoSavedPasswords, ObservationDeps, SavedPasswords, + ServerAgentAccess, +}; use nocterm_connections_model::{ draft::EditorDefaults, facts::Known, @@ -8,16 +11,51 @@ use nocterm_connections_model::{ repository::LoadedConnections, value_ports::{ConnectionClock, ProfileIds, SuppliedValues}, }; +use nocterm_ssh_keys::{NoVaultSshKeys, VaultSshKeys}; use nocterm_store::SnapshotWriter; use std::sync::Arc; #[derive(Clone)] pub struct EditorInputs { defaults: EditorDefaults, ids: Arc, + keys: Arc, + passwords: Arc, + agent_access: Option>, } impl EditorInputs { + /// Inputs of a window without a vault: its key and password choices report that. pub fn new(defaults: EditorDefaults, ids: Arc) -> Self { - Self { defaults, ids } + Self { + defaults, + ids, + keys: Arc::new(NoVaultSshKeys), + passwords: Arc::new(NoSavedPasswords), + agent_access: None, + } + } + /// A password typed in the editor is kept in `passwords`. + pub fn with_passwords(mut self, passwords: Arc) -> Self { + self.passwords = passwords; + self + } + pub(crate) fn passwords(&self) -> &Arc { + &self.passwords + } + /// The keys the editor offers are those of `keys`. + pub fn with_keys(mut self, keys: Arc) -> Self { + self.keys = keys; + self + } + pub(crate) fn keys(&self) -> &Arc { + &self.keys + } + /// The editor offers Agent access of a saved server through `access`. + pub fn with_agent_access(mut self, access: Arc) -> Self { + self.agent_access = Some(access); + self + } + pub(crate) fn agent_access(&self) -> Option<&Arc> { + self.agent_access.as_ref() } pub fn defaults(&self) -> &EditorDefaults { &self.defaults @@ -44,42 +82,101 @@ impl ConnectionWriters { } } } +/// What the connections need besides documents and writers: the same for the saved list of +/// this computer and for a list kept only in memory, so a window can be given either. +#[derive(Clone)] +pub struct ConnectionInputs { + editor: EditorInputs, + clock: Arc, + observations: ObservationDeps, +} +impl ConnectionInputs { + pub fn new( + editor: EditorInputs, + clock: Arc, + observations: ObservationDeps, + ) -> Self { + Self { + editor, + clock, + observations, + } + } + /// The editor offers the keys of `keys` instead of reporting no vault. + pub fn with_keys(mut self, keys: Arc) -> Self { + self.editor = self.editor.with_keys(keys); + self + } + /// The editor keeps a typed password in `passwords` instead of reporting no vault. + pub fn with_passwords(mut self, passwords: Arc) -> Self { + self.editor = self.editor.with_passwords(passwords); + self + } + /// The editor offers Agent access of a saved server through `access`. + pub fn with_agent_access(mut self, access: Arc) -> Self { + self.editor = self.editor.with_agent_access(access); + self + } +} pub struct PreparedConnections { documents: LoadedConnections, writers: Option, - inputs: EditorInputs, - clock: Arc, - observations: ObservationDeps, + inputs: ConnectionInputs, } impl PreparedConnections { pub fn new( documents: LoadedConnections, writers: ConnectionWriters, - inputs: EditorInputs, - clock: Arc, - observations: ObservationDeps, + inputs: ConnectionInputs, ) -> Self { Self { documents, writers: Some(writers), inputs, - clock, - observations, } } - pub fn memory(defaults: EditorDefaults) -> Self { - let values = Arc::new(SuppliedValues::new(1, 0)); + /// An empty list kept only in memory, for a list that something else keeps: it writes + /// nothing, and new connections get the identifiers `inputs` supplies. + pub fn volatile(inputs: ConnectionInputs) -> Self { Self { documents: LoadedConnections::default(), writers: None, - inputs: EditorInputs::new(defaults, values.clone()), - clock: values, - observations: ObservationDeps::offline(), + inputs, } } + /// The inputs these connections were prepared with, to prepare another list the same way. + pub fn inputs(&self) -> ConnectionInputs { + self.inputs.clone() + } + /// The editor offers the keys of `keys` instead of reporting no vault. + pub fn with_keys(mut self, keys: Arc) -> Self { + self.inputs = self.inputs.with_keys(keys); + self + } + /// The editor keeps a typed password in `passwords` instead of reporting no vault. + pub fn with_passwords(mut self, passwords: Arc) -> Self { + self.inputs = self.inputs.with_passwords(passwords); + self + } + pub fn memory(defaults: EditorDefaults) -> Self { + Self::memory_with_keys(defaults, Arc::new(NoVaultSshKeys)) + } + pub fn memory_with_keys(defaults: EditorDefaults, keys: Arc) -> Self { + let values = Arc::new(SuppliedValues::new(1, 0)); + Self::volatile(ConnectionInputs::new( + EditorInputs::new(defaults, values.clone()).with_keys(keys), + values, + ObservationDeps::offline(), + )) + } pub(crate) fn into_owners(self) -> (Connections, EditorInputs, FactsPrepared) { let profiles = self.documents.profiles; let recents = self.documents.recents; + let ConnectionInputs { + editor, + clock, + observations, + } = self.inputs; let (connections, facts) = match self.writers { Some(writers) => ( Connections::prepared( @@ -88,15 +185,15 @@ impl PreparedConnections { writers.profiles, writers.recents, self.documents.read_only, - self.clock, + clock, ), - FactsPrepared::new(self.documents.known, writers.facts, self.observations), + FactsPrepared::new(self.documents.known, writers.facts, observations), ), None => ( - Connections::memory_with_clock(profiles, recents, self.clock), - FactsPrepared::in_memory(self.observations), + Connections::memory_with_clock(profiles, recents, clock), + FactsPrepared::in_memory(observations), ), }; - (connections, self.inputs, facts) + (connections, editor, facts) } } diff --git a/crates/connections/src/probe_sources.rs b/crates/connections/src/probe_sources.rs index b79cc984..77dfe364 100644 --- a/crates/connections/src/probe_sources.rs +++ b/crates/connections/src/probe_sources.rs @@ -7,6 +7,7 @@ use nocterm_connections_store::{ ProbeSourceIdentity, ProbeSourceLease, }; use nocterm_session::{RemoteFs, Target}; +use nocterm_ssh_keys::authorized_keys::{ConnectedHost, HostIdentity}; use nocterm_workspace::{ConnectionFactSession, ExpectedSession, Workspace}; use std::{ cell::{Cell, RefCell}, @@ -14,6 +15,10 @@ use std::{ rc::Rc, sync::{Arc, Weak}, }; +/// Told once for every session of a saved connection that comes up, with its file system. +pub trait HostObserver { + fn connected(&self, host: ConnectedHost, source: ProbeSourceLease, cx: &mut App); +} struct OriginalSource { original: Rc, expected: ExpectedSession, @@ -100,6 +105,18 @@ impl ProjectedSource { fs: snapshot.session.fs?, }) } + fn announcement(&self) -> ConnectedHost { + ConnectedHost { + connection: self.association.to_string(), + identity: HostIdentity { + user: self.target.user.clone(), + host: self.target.host.clone(), + host_key: String::new(), + }, + port: self.target.port, + fs: self.fs.clone(), + } + } fn fresh_slot(&self) -> SourceSlot { let authority = OriginalSource { original: self.original.clone(), @@ -136,7 +153,12 @@ impl SourceSlot { } } impl ProbeSource { - fn reproject(&mut self, connections: &Entity, cx: &App) -> Vec { + fn reproject( + &mut self, + connections: &Entity, + arrived: &mut Vec<(ConnectedHost, ProbeSourceLease)>, + cx: &App, + ) -> Vec { let Some(workspace) = self.workspace.upgrade() else { return Vec::new(); }; @@ -167,7 +189,9 @@ impl ProbeSource { { old.lease.revoke(); } - self.leases.insert(id, projection.fresh_slot()); + let slot = projection.fresh_slot(); + arrived.push((projection.announcement(), slot.lease.clone())); + self.leases.insert(id, slot); } self.leases .get(&id) @@ -180,8 +204,12 @@ impl ProbeSource { pub(crate) struct ProbeSources { sources: RefCell>, rotation: Cell, + observer: RefCell>>, } impl ProbeSources { + pub(crate) fn bind_observer(&self, observer: Rc) { + self.observer.borrow_mut().get_or_insert(observer); + } pub(crate) fn register(&self, workspace: WeakEntity) { let mut sources = self.sources.borrow_mut(); if !sources @@ -201,6 +229,7 @@ impl ProbeSources { facts: &Entity, cx: &mut App, ) { + let observer = self.observer.borrow().clone(); let mut sources = self.sources.borrow_mut(); sources.retain(|source| source.workspace.upgrade().is_some()); let count = sources.len(); @@ -208,7 +237,13 @@ impl ProbeSources { for offset in 0..count { let index = (start + offset) % count; let source = &mut sources[index]; - let requests = source.reproject(connections, cx); + let mut arrived = Vec::new(); + let requests = source.reproject(connections, &mut arrived, cx); + if let Some(observer) = observer.as_ref() { + for (host, source) in arrived { + observer.connected(host, source, cx); + } + } let length = requests.len(); if length == 0 { source.cursor = 0; diff --git a/crates/connections/src/saved_password.rs b/crates/connections/src/saved_password.rs new file mode 100644 index 00000000..88f9e614 --- /dev/null +++ b/crates/connections/src/saved_password.rs @@ -0,0 +1,96 @@ +//! The port through which the connection editor keeps the password a saved connection signs in +//! with, in the vault of its window: the account's vault on the hub in a hub window, this +//! computer's vault otherwise. A saved password may sign in to several servers; the editor +//! offers the vault's passwords and attaches the one chosen to the connection's address. The +//! composition root implements it over that vault. +use futures::future::BoxFuture; +use nocterm_session::{CredentialId, Secret, Target}; + +/// Which vault a window keeps its passwords and keys in, as the editor names it. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum VaultPlace { + /// The vault of this computer. + #[default] + ThisComputer, + /// The account's vault on the window's hub, sealed end to end: every computer of the + /// account signs in with what it keeps. + Account, +} + +/// Why a password was not kept, in words the editor shows. +#[derive(Debug, thiserror::Error)] +pub enum PasswordNotKept { + #[error("Unlock the vault to keep the password.")] + Locked, + #[error("This window has no vault to keep the password in.")] + Unavailable, + #[error("The saved password is no longer in the vault.")] + Missing, + #[error("The vault could not keep the password.")] + Vault(#[source] Box), +} + +/// A password the vault keeps, as the editor offers it to sign in with. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SavedPassword { + pub id: CredentialId, + pub label: String, + /// How many servers it signs in to. + pub servers: usize, +} + +/// The vault the window keeps passwords in. +pub trait SavedPasswords: Send + Sync { + /// Which vault it is. + fn place(&self) -> VaultPlace; + /// The passwords the vault keeps, in its order. + fn list(&self) -> BoxFuture<'static, Result, PasswordNotKept>>; + /// Keeps `password` for `target`: as the new password of the record `replacing`, which + /// keeps its name and every server it signs in to and gains `target`, or in a new record + /// named `label` that signs in to `target` only. Resolves to the record. + fn keep( + &self, + replacing: Option, + label: String, + target: Target, + password: Secret, + ) -> BoxFuture<'static, Result>; + /// Lets the saved password `id` sign in to `target` as well. + fn attach( + &self, + id: CredentialId, + target: Target, + ) -> BoxFuture<'static, Result<(), PasswordNotKept>>; +} + +/// The port of a window composed without a vault: nothing is kept. +#[derive(Clone, Copy, Debug, Default)] +pub struct NoSavedPasswords; + +impl SavedPasswords for NoSavedPasswords { + fn place(&self) -> VaultPlace { + VaultPlace::ThisComputer + } + + fn list(&self) -> BoxFuture<'static, Result, PasswordNotKept>> { + Box::pin(std::future::ready(Ok(Vec::new()))) + } + + fn keep( + &self, + _: Option, + _: String, + _: Target, + _: Secret, + ) -> BoxFuture<'static, Result> { + Box::pin(std::future::ready(Err(PasswordNotKept::Unavailable))) + } + + fn attach( + &self, + _: CredentialId, + _: Target, + ) -> BoxFuture<'static, Result<(), PasswordNotKept>> { + Box::pin(std::future::ready(Err(PasswordNotKept::Unavailable))) + } +} diff --git a/crates/connections/src/session_intents.rs b/crates/connections/src/session_intents.rs index d72eb4ae..1ce58fd4 100644 --- a/crates/connections/src/session_intents.rs +++ b/crates/connections/src/session_intents.rs @@ -1,6 +1,6 @@ //! Feature-owned conversion and foreground open intent; background routes skip recents. use crate::Installed; -use gpui_kit::{App, SharedString, WeakEntity, Window}; +use gpui_kit::{App, EntityId, SharedString, WeakEntity, Window}; use nocterm_connections_model::{ jump_route::{JumpRouteError, resolve_jump_route}, profiles::{Profile, ProfileId, Profiles, Recent}, @@ -119,6 +119,29 @@ pub(crate) fn open_profile( } } } +/// Opens a saved connection without a tab and returns the session's item, or reports why its +/// route cannot be resolved. It signs in as a tab would and is not recorded among recents. +pub(crate) fn open_background( + installed: &Installed, + workspace: &WeakEntity, + profile: &Profile, + window: &mut Window, + cx: &mut App, +) -> Option { + let spec = match spec_for_profile(profile, installed.connections().read(cx).profiles(cx)) { + Ok(spec) => spec, + Err(error) => { + report_route_error(&error, window, cx); + return None; + } + }; + workspace + .update(cx, |workspace, cx| { + workspace.open_background_session(spec, window, cx) + }) + .ok() + .flatten() +} /// Records the foreground intent before asking the existing workspace session factory. pub fn connect( installed: &Installed, diff --git a/crates/connections/src/test_support.rs b/crates/connections/src/test_support.rs index 1155c231..13f78be5 100644 --- a/crates/connections/src/test_support.rs +++ b/crates/connections/src/test_support.rs @@ -15,6 +15,131 @@ pub(crate) type Opened = Rc>>; pub(crate) fn workspace( cx: &mut TestAppContext, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + workspace_with_keys(cx, std::sync::Arc::new(nocterm_ssh_keys::NoVaultSshKeys)) +} + +/// A workspace whose connection editor offers the keys of `keys`. +pub(crate) fn workspace_with_keys( + cx: &mut TestAppContext, + keys: std::sync::Arc, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + workspace_over(cx, memory(keys)) +} + +/// A workspace of a hub window, whose editor offers Agent access through `access`. +pub(crate) fn workspace_with_agent_access( + cx: &mut TestAppContext, + access: std::sync::Arc, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + let prepared = memory(std::sync::Arc::new(nocterm_ssh_keys::NoVaultSshKeys)); + let inputs = prepared.inputs().with_agent_access(access); + workspace_over(cx, crate::PreparedConnections::volatile(inputs)) +} + +/// A workspace whose connection editor keeps a typed password in `passwords`. +pub(crate) fn workspace_with_passwords( + cx: &mut TestAppContext, + passwords: std::sync::Arc, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + let prepared = memory(std::sync::Arc::new(nocterm_ssh_keys::NoVaultSshKeys)); + let inputs = prepared.inputs().with_passwords(passwords); + workspace_over(cx, crate::PreparedConnections::volatile(inputs)) +} + +/// A vault of `place` that records every password kept in it, or refuses with `locked`. +pub(crate) struct ScriptedPasswords { + place: crate::VaultPlace, + pub(crate) locked: std::sync::atomic::AtomicBool, + /// The passwords the vault offers. + pub(crate) saved: std::sync::Mutex>, + /// `(record, address)` of each saved password attached to an address, in order. + pub(crate) attached: + std::sync::Mutex>, + /// `(replacing, label, target, password)` of each password kept, in order. + pub(crate) kept: std::sync::Mutex< + Vec<( + Option, + String, + nocterm_session::Target, + String, + )>, + >, +} + +impl ScriptedPasswords { + pub(crate) fn new(place: crate::VaultPlace) -> Self { + Self { + place, + locked: Default::default(), + saved: Default::default(), + attached: Default::default(), + kept: Default::default(), + } + } +} + +impl crate::SavedPasswords for ScriptedPasswords { + fn place(&self) -> crate::VaultPlace { + self.place + } + + fn list( + &self, + ) -> futures::future::BoxFuture< + 'static, + Result, crate::PasswordNotKept>, + > { + Box::pin(std::future::ready(Ok(self.saved.lock().unwrap().clone()))) + } + + fn attach( + &self, + id: nocterm_session::CredentialId, + target: nocterm_session::Target, + ) -> futures::future::BoxFuture<'static, Result<(), crate::PasswordNotKept>> { + if self.locked.load(std::sync::atomic::Ordering::SeqCst) { + return Box::pin(std::future::ready(Err(crate::PasswordNotKept::Locked))); + } + self.attached.lock().unwrap().push((id, target)); + Box::pin(std::future::ready(Ok(()))) + } + + fn keep( + &self, + replacing: Option, + label: String, + target: nocterm_session::Target, + password: nocterm_session::Secret, + ) -> futures::future::BoxFuture< + 'static, + Result, + > { + if self.locked.load(std::sync::atomic::Ordering::SeqCst) { + return Box::pin(std::future::ready(Err(crate::PasswordNotKept::Locked))); + } + let mut kept = self.kept.lock().unwrap(); + let id = replacing.unwrap_or_else(|| { + nocterm_session::CredentialId::from_bytes([u8::try_from(kept.len() + 1).unwrap(); 16]) + }); + kept.push((replacing, label, target, password.expose().to_owned())); + Box::pin(std::future::ready(Ok(id))) + } +} + +fn memory(keys: std::sync::Arc) -> crate::PreparedConnections { + crate::PreparedConnections::memory_with_keys( + nocterm_connections_model::draft::EditorDefaults { + user: Some("tester".into()), + home: None, + }, + keys, + ) +} + +fn workspace_over( + cx: &mut TestAppContext, + prepared: crate::PreparedConnections, ) -> (AnyWindowHandle, Entity, Opened, Installed) { let opened = Opened::default(); let calls = opened.clone(); @@ -28,10 +153,7 @@ pub(crate) fn workspace( cx, ); let installed = crate::install( - crate::PreparedConnections::memory(nocterm_connections_model::draft::EditorDefaults { - user: Some("tester".into()), - home: None, - }), + prepared, &ready, &nocterm_store::ShutdownCoordinator::default(), cx, @@ -121,3 +243,98 @@ fn model_inputs(connections: &Connections, cx: &gpui_kit::App) -> crate::EditorI values, ) } + +/// Binds a replication to `installed` that no replica is attached to: the state of an +/// account that is signed out. +pub(crate) fn unattached( + installed: &Installed, + cx: &mut TestAppContext, +) -> Entity { + use nocterm_connections_model::replica::ReplicaState; + use nocterm_connections_store::ProfileReplication; + let replication = cx.update(|cx| { + let store = installed.connections().clone(); + let writer: nocterm_store::SnapshotWriter = + std::sync::Arc::new(|_| Box::pin(async { Ok(()) })); + let document = + cx.new(|_| ProfileReplication::state_document(ReplicaState::default(), writer)); + cx.new(|cx| ProfileReplication::new(store, document, cx)) + }); + installed.bind_replication(replication.clone()); + replication +} + +/// Keeps `installed` in step with `replica`, as the application does for the chosen +/// account, and runs the first synchronization. +pub(crate) fn replicate( + installed: &Installed, + replica: std::sync::Arc, + cx: &mut TestAppContext, +) -> Entity { + let replication = unattached(installed, cx); + replication.update(cx, |replication, cx| replication.attach(replica, cx)); + cx.run_until_parked(); + replication +} + +/// An Agent access port that records each change and grants it, or refuses it with `refusal`. +/// `session` tells whether a session to the server is open; `state` is what the hub records. +#[derive(Default)] +pub(crate) struct ScriptedAgentAccess { + pub(crate) state: std::sync::Mutex>, + pub(crate) session: std::sync::atomic::AtomicBool, + pub(crate) changes: std::sync::Mutex>, + pub(crate) refusal: Option<&'static str>, +} + +impl ScriptedAgentAccess { + pub(crate) fn with_session() -> Self { + let access = Self::default(); + access.open_session(); + access + } + + pub(crate) fn open_session(&self) { + self.session + .store(true, std::sync::atomic::Ordering::SeqCst); + } + + pub(crate) fn granted(&self) -> Vec { + let changes = self.changes.lock().unwrap(); + changes.iter().map(|(_, granted)| *granted).collect() + } +} + +impl crate::ServerAgentAccess for ScriptedAgentAccess { + fn state(&self, _: &crate::AgentServer, _: &gpui_kit::App) -> Option { + *self.state.lock().unwrap() + } + + fn session_open(&self, _: &crate::AgentServer, _: &gpui_kit::App) -> bool { + self.session.load(std::sync::atomic::Ordering::SeqCst) + } + + fn set( + &self, + server: crate::AgentServer, + granted: bool, + _: &mut gpui_kit::App, + ) -> gpui_kit::Task> { + self.changes.lock().unwrap().push((server, granted)); + match self.refusal { + Some(reason) => gpui_kit::Task::ready(Err(crate::AgentAccessRefused::new(reason))), + None => { + *self.state.lock().unwrap() = granted.then_some(crate::AgentAccessState::Unchecked); + gpui_kit::Task::ready(Ok(())) + } + } + } + + fn observe( + &self, + _: Box, + _: &mut gpui_kit::App, + ) -> gpui_kit::Subscription { + gpui_kit::Subscription::new(|| {}) + } +} diff --git a/crates/connections/src/volatile_tests.rs b/crates/connections/src/volatile_tests.rs new file mode 100644 index 00000000..9c374c3e --- /dev/null +++ b/crates/connections/src/volatile_tests.rs @@ -0,0 +1,107 @@ +//! Connections kept only in memory, for a window whose list something else keeps. +use crate::{ConnectionInputs, EditorInputs, Installed, PreparedConnections, open_editor}; +use gpui_kit::{AppContext as _, TestAppContext, WindowOptions, test::TestWindowExt as _}; +use nocterm_connections_model::{ + draft::EditorDefaults, + profiles::ProfileId, + test_support::profile_id_for, + value_ports::{ProfileIds, SuppliedValues}, +}; +use nocterm_store::{SettingsConfig, ShutdownCoordinator}; +use nocterm_ui::DesignTokens; +use nocterm_workspace::Workspace; +use std::sync::Arc; + +/// Hands out one identifier, standing in for the UUIDs of the application. +struct FixedId(ProfileId); +impl ProfileIds for FixedId { + fn next(&self) -> ProfileId { + self.0 + } +} + +fn inputs(id: ProfileId) -> ConnectionInputs { + let values = Arc::new(SuppliedValues::new(1, 0)); + ConnectionInputs::new( + EditorInputs::new( + EditorDefaults { + user: Some("tester".into()), + home: None, + }, + Arc::new(FixedId(id)), + ), + values, + crate::ObservationDeps::offline(), + ) +} + +fn ready(cx: &mut gpui_kit::App) -> nocterm_ui::UiReady { + gpui_kit::init(cx); + nocterm_ui::init( + DesignTokens::builtin(), + SettingsConfig::in_memory(Default::default()).install(cx), + cx, + ) +} + +fn bindings(cx: &gpui_kit::App) -> usize { + cx.key_bindings().borrow().bindings().count() +} + +#[gpui_kit::test] +fn installing_volatile_connections_twice_adds_no_key_bindings(cx: &mut TestAppContext) { + let (before, after) = cx.update(|cx| { + let ready = ready(cx); + crate::install( + PreparedConnections::volatile(inputs(profile_id_for("a"))), + &ready, + &ShutdownCoordinator::default(), + cx, + ) + .unwrap(); + let before = bindings(cx); + for _ in 0..2 { + crate::install_volatile( + PreparedConnections::volatile(inputs(profile_id_for("a"))), + &ready, + cx, + ) + .unwrap(); + } + (before, bindings(cx)) + }); + assert_eq!(before, after); +} + +#[gpui_kit::test] +fn a_new_connection_in_volatile_connections_takes_the_supplied_identifier(cx: &mut TestAppContext) { + let id = profile_id_for("fresh"); + let (handle, workspace, installed): (_, _, Installed) = cx.update(|cx| { + cx.set_reduce_motion(true); + let ready = ready(cx); + let installed = + crate::install_volatile(PreparedConnections::volatile(inputs(id)), &ready, cx).unwrap(); + let (window, workspace) = + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let deps = nocterm_workspace::test_support::deps_for_core(&[], &[], cx); + Workspace::new(deps, window, cx) + }) + }) + .unwrap(); + (window, workspace, installed) + }); + cx.update_window(handle, |_, window, cx| { + open_editor(installed.clone(), None, workspace.downgrade(), window, cx); + window.render_frame(cx); + window.input("host.test", cx); + window.press("enter", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.read(|cx| { + let connections = installed.connections().read(cx); + assert!(connections.profiles(cx).get(id).is_some()); + assert_eq!(connections.profiles(cx).iter().count(), 1); + }); +} diff --git a/crates/containers-service/Cargo.toml b/crates/containers-service/Cargo.toml new file mode 100644 index 00000000..f6cfa752 --- /dev/null +++ b/crates/containers-service/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "nocterm-containers-service" +description = "One shared watch of each host's containers and images, relisted only for events that change them." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +gpui.workspace = true +nocterm-containers.workspace = true +nocterm-session.workspace = true +futures.workspace = true + +[features] +test-support = [] + +[lints] +workspace = true + +[dev-dependencies] +nocterm-session = { workspace = true, features = ["test-support"] } +gpui = { workspace = true, features = ["test-support"] } diff --git a/crates/containers-service/src/host.rs b/crates/containers-service/src/host.rs new file mode 100644 index 00000000..d330ad7b --- /dev/null +++ b/crates/containers-service/src/host.rs @@ -0,0 +1,175 @@ +//! What one host connection's engine last reported, and the watch that keeps it current. + +use std::sync::Arc; + +use futures::channel::mpsc; +use gpui::{Context, Task}; +use nocterm_containers::{Changes, Container, ContainersError, Engine, Image, Snapshot}; +use nocterm_session::HostExec; + +use crate::watch; + +/// Where the listing of a host stands. +#[derive(Debug)] +pub enum ListingStatus { + /// The first listing, or one after a failure, is under way. + Loading, + Ready, + /// The last attempt failed; unless the host has no engine, another follows shortly. + Failed(ContainersError), +} + +impl ListingStatus { + /// Whether no watch follows: the host has no engine, or cannot run programs. + fn is_final(&self) -> bool { + matches!( + self, + Self::Failed(ContainersError::NotInstalled | ContainersError::Unsupported) + ) + } +} + +/// A listing that replaces one or both lists of the snapshot. +pub(crate) enum Listed { + Both(Snapshot), + Containers(Vec), + Images(Vec), +} + +/// The containers and images of one host connection, shared by everyone watching it. +pub struct HostContainers { + exec: Arc, + engine: Option, + snapshot: Snapshot, + status: ListingStatus, + /// What the running watch is asked to list again; closed once it has ended. + requests: mpsc::UnboundedSender, + _watch: Task<()>, +} + +impl HostContainers { + pub(crate) fn new(exec: Arc, cx: &mut Context) -> Self { + let (requests, watch) = Self::spawn(exec.clone(), None, cx); + Self { + exec, + engine: None, + snapshot: Snapshot::default(), + status: ListingStatus::Loading, + requests, + _watch: watch, + } + } + + fn spawn( + exec: Arc, + engine: Option, + cx: &mut Context, + ) -> (mpsc::UnboundedSender, Task<()>) { + let (requests, asked) = mpsc::unbounded(); + let task = + cx.spawn(async move |this, cx| watch::watch(this, exec, engine, asked, cx).await); + (requests, task) + } + + /// How programs run on this host. + pub fn exec(&self) -> &Arc { + &self.exec + } + + /// The engine the host was found to have. + pub fn engine(&self) -> Option { + self.engine + } + + pub fn snapshot(&self) -> &Snapshot { + &self.snapshot + } + + pub fn status(&self) -> &ListingStatus { + &self.status + } + + /// Lists `changes` again soon, as after an action on the host or when the person asks; + /// a watch that has stopped starts again, looking for the engine anew. + pub fn relist(&mut self, changes: Changes, cx: &mut Context) { + if changes.is_empty() { + return; + } + if self.requests.unbounded_send(changes).is_ok() { + return; + } + self.restart(cx); + } + + /// Starts again at once a watch that is waiting out a failure, or has stopped. + pub(crate) fn revive(&mut self, cx: &mut Context) { + if matches!(self.status, ListingStatus::Failed(_)) { + self.relist(Changes::ALL, cx); + } + } + + fn restart(&mut self, cx: &mut Context) { + let engine = if self.status.is_final() { + None + } else { + self.engine + }; + let (requests, watch) = Self::spawn(self.exec.clone(), engine, cx); + self.requests = requests; + self._watch = watch; + self.set_status(ListingStatus::Loading, cx); + } + + pub(crate) fn listed(&mut self, engine: Engine, listed: Listed, cx: &mut Context) { + let changed = self.engine != Some(engine); + self.engine = Some(engine); + let changed = changed + | match listed { + Listed::Both(snapshot) => replace(&mut self.snapshot, snapshot), + Listed::Containers(containers) => { + replace(&mut self.snapshot.containers, containers) + } + Listed::Images(images) => replace(&mut self.snapshot.images, images), + }; + if changed { + cx.notify(); + } + self.set_status(ListingStatus::Ready, cx); + } + + /// The watch's attempt ended with `outcome`; whether to try again. + pub(crate) fn ended( + &mut self, + outcome: Result<(), ContainersError>, + cx: &mut Context, + ) -> bool { + // An ended event stream leaves the listing standing. + let Err(error) = outcome else { return true }; + self.set_status(ListingStatus::Failed(error), cx); + !self.status.is_final() + } + + fn set_status(&mut self, status: ListingStatus, cx: &mut Context) { + // Native error sources have no equality contract. Deduplicate only the + // visible status category and text; recovery always matches typed variants. + let unchanged = match (&self.status, &status) { + (ListingStatus::Failed(previous), ListingStatus::Failed(next)) => { + std::mem::discriminant(previous) == std::mem::discriminant(next) + && previous.to_string() == next.to_string() + } + (previous, next) => std::mem::discriminant(previous) == std::mem::discriminant(next), + }; + if !unchanged { + self.status = status; + cx.notify(); + } + } +} + +fn replace(slot: &mut T, value: T) -> bool { + if *slot == value { + return false; + } + *slot = value; + true +} diff --git a/crates/containers-service/src/lib.rs b/crates/containers-service/src/lib.rs new file mode 100644 index 00000000..eba2a53f --- /dev/null +++ b/crates/containers-service/src/lib.rs @@ -0,0 +1,76 @@ +//! One shared watch of each host's containers and images, shared by every window. +//! +//! Owns: [`ContainerHosts`], the registry that hands every caller asking about the same host +//! connection the same [`HostContainers`]; the engine found there, its last listings and +//! where listing stands; the one event stream followed per connection; and the rules for +//! when to list again: only for events that change a listing, only the listing they change, +//! once per burst, and every [`RETRY`] where there is no event stream. +//! Does not own: command lines, listing and event formats (`nocterm-containers`), host +//! execution or connections (`HostExec`), which host a window looks at, actions and views +//! (`nocterm-containers-ui`). +//! Ports used: `nocterm_session::HostExec`. Ports provided: [`ContainerHosts`] and +//! [`HostContainers`]. Settings section: none. Globals: none. +//! Invariants: a host connection, identified by its executor, has at most one watch, and so +//! one `events` process, however many windows show it; the watch lives while someone holds +//! its entity and stops its programs when the last one lets go; an event that changes no +//! listing runs no program. + +mod host; +#[cfg(any(test, feature = "test-support"))] +pub mod test_support; +mod watch; + +use std::{ + cell::RefCell, + rc::Rc, + sync::{Arc, Weak}, +}; + +use gpui::{App, AppContext as _, Entity, WeakEntity}; +use nocterm_session::HostExec; + +pub use host::{HostContainers, ListingStatus}; +pub use watch::{RETRY, SETTLE}; + +/// The watched hosts, by connection. Cloning shares the registry. +#[derive(Clone, Default)] +pub struct ContainerHosts { + watched: Rc>>, +} + +struct Watched { + exec: Weak, + host: WeakEntity, +} + +impl ContainerHosts { + pub fn new() -> Self { + Self::default() + } + + /// The containers of the host reached through `exec`, watched once for everyone who asks + /// about the same executor. A watch that stopped after a failure starts again. + pub fn watch(&self, exec: &Arc, cx: &mut App) -> Entity { + let found = { + let mut watched = self.watched.borrow_mut(); + watched.retain(|entry| entry.host.upgrade().is_some()); + watched + .iter() + .filter(|entry| std::ptr::addr_eq(entry.exec.as_ptr(), Arc::as_ptr(exec))) + .find_map(|entry| entry.host.upgrade()) + }; + if let Some(host) = found { + host.update(cx, |host, cx| host.revive(cx)); + return host; + } + let host = cx.new(|cx| HostContainers::new(exec.clone(), cx)); + self.watched.borrow_mut().push(Watched { + exec: Arc::downgrade(exec), + host: host.downgrade(), + }); + host + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/containers-service/src/test_support.rs b/crates/containers-service/src/test_support.rs new file mode 100644 index 00000000..32f5b6e8 --- /dev/null +++ b/crates/containers-service/src/test_support.rs @@ -0,0 +1,145 @@ +//! A host with a scripted Docker, for tests of everything that watches containers. + +use futures::future::BoxFuture; +use std::{ + collections::VecDeque, + sync::{Arc, Mutex}, +}; + +use futures::FutureExt as _; +use nocterm_session::{ExecExit, ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec}; + +/// One running container named `web`. +pub const RUNNING: &str = + r#"{"ID":"a1","Image":"nginx","Names":"web","State":"running","Status":"Up 2 hours"}"#; +/// The same container, exited. +pub const EXITED: &str = + r#"{"ID":"a1","Image":"nginx","Names":"web","State":"exited","Status":"Exited (0)"}"#; +/// One image. +pub const IMAGE: &str = r#"{"ID":"sha256:1","Repository":"nginx","Tag":"1","Size":"7MB"}"#; + +/// A host whose `docker` lists what it is told to, keeps `docker events` running until the +/// test prints to it or lets it end, and does every other command it is asked. Without +/// Docker, every command is missing. +pub struct FakeDockerHost { + docker: bool, + containers: Mutex, + images: Mutex, + ran: Mutex>, + events: Mutex>, + listings: Mutex>>, +} + +impl FakeDockerHost { + pub fn new(docker: bool) -> Arc { + Arc::new(Self { + docker, + containers: Mutex::new(RUNNING.into()), + images: Mutex::new(String::new()), + ran: Mutex::default(), + events: Mutex::default(), + listings: Mutex::default(), + }) + } + + /// Suspends the next container listing after capturing its reply. + pub fn suspend_listing(&self, gate: BoxFuture<'static, ()>) { + lock(&self.listings).push_back(gate); + } + + /// Every command line run so far. + pub fn ran(&self) -> Vec { + lock(&self.ran).clone() + } + + /// How many command lines run so far start with `prefix`. + pub fn count(&self, prefix: &str) -> usize { + lock(&self.ran) + .iter() + .filter(|line| line.starts_with(prefix)) + .count() + } + + /// What `docker ps` prints from now on. + pub fn set_containers(&self, listing: &str) { + *lock(&self.containers) = listing.into(); + } + + /// What `docker images` prints from now on. + pub fn set_images(&self, listing: &str) { + *lock(&self.images) = listing.into(); + } + + /// Prints `line` and a newline to the latest event stream. + pub fn announce(&self, line: &str) { + let events = lock(&self.events); + let sink = events.last().expect("events are followed"); + let mut bytes = line.as_bytes().to_vec(); + bytes.push(b'\n'); + assert_eq!(sink.send(bytes).now_or_never(), Some(true)); + } + + /// Ends the latest event stream, as an engine whose events are unavailable does. + pub fn end_events(&self) { + let sink = lock(&self.events).pop().expect("events are followed"); + sink.finish(ExecExit::default()); + } + + /// Whether the event stream started `index`th is still being read. + pub fn following(&self, index: usize) -> bool { + !lock(&self.events)[index].is_closed() + } + + /// How many event streams are still being read. + pub fn followers(&self) -> usize { + lock(&self.events) + .iter() + .filter(|sink| !sink.is_closed()) + .count() + } +} + +/// A Docker event line of `kind` (`container` or `image`) for `action` on `id`. +pub fn docker_event(kind: &str, action: &str, id: &str) -> String { + format!(r#"{{"Type":"{kind}","Action":"{action}","Actor":{{"ID":"{id}"}}}}"#) +} + +impl HostExec for FakeDockerHost { + fn exec(&self, request: ExecRequest) -> ExecFuture { + let line = request.command_line(); + lock(&self.ran).push(line.clone()); + let (sink, output) = ExecOutput::channel(); + if self.docker && line.starts_with("docker events") { + lock(&self.events).push(sink); + return Box::pin(async { Ok(output) }); + } + let (stdout, status) = match () { + _ if !self.docker => (String::new(), 127), + _ if line.starts_with("docker ps") => (lock(&self.containers).clone(), 0), + _ if line.starts_with("docker images") => (lock(&self.images).clone(), 0), + _ if line.starts_with("docker") => (String::new(), 0), + _ => (String::new(), 127), + }; + let gate = line + .starts_with("docker ps") + .then(|| lock(&self.listings).pop_front()) + .flatten(); + Box::pin(async move { + if let Some(gate) = gate { + gate.await; + } + assert!(sink.send(stdout.into_bytes()).await); + sink.finish(ExecExit { + status: Some(status), + stderr: String::new(), + }); + Ok(output) + }) + } +} + +fn lock(mutex: &Mutex) -> std::sync::MutexGuard<'_, T> { + mutex + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) +} diff --git a/crates/containers-service/src/tests.rs b/crates/containers-service/src/tests.rs new file mode 100644 index 00000000..13de6b95 --- /dev/null +++ b/crates/containers-service/src/tests.rs @@ -0,0 +1,232 @@ +use futures::FutureExt as _; +use std::sync::Arc; + +use gpui::{Entity, TestAppContext}; +use nocterm_containers::{Changes, ContainersError, Engine, State}; +use nocterm_session::HostExec; + +use crate::{ + ContainerHosts, HostContainers, ListingStatus, RETRY, SETTLE, + test_support::{EXITED, FakeDockerHost, IMAGE, docker_event}, +}; + +fn watch( + cx: &mut TestAppContext, + hosts: &ContainerHosts, + host: &Arc, +) -> Entity { + let exec: Arc = host.clone(); + let watched = cx.update(|cx| hosts.watch(&exec, cx)); + cx.run_until_parked(); + watched +} + +fn settle(cx: &mut TestAppContext) { + cx.executor().advance_clock(SETTLE * 2); + cx.run_until_parked(); +} + +/// Lets GPUI drop entities nobody holds any more. +fn release(cx: &mut TestAppContext) { + cx.update(|_| {}); + cx.run_until_parked(); +} + +fn listings(host: &FakeDockerHost) -> (usize, usize) { + (host.count("docker ps"), host.count("docker images")) +} + +#[gpui::test] +fn every_window_on_a_host_shares_one_watch(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let first = watch(cx, &hosts, &local); + let second = watch(cx, &hosts, &local); + assert_eq!(first.entity_id(), second.entity_id()); + assert_eq!(local.count("docker events"), 1); + assert_eq!(listings(&local), (1, 1)); + first.read_with(cx, |host, _| { + assert_eq!(host.engine(), Some(Engine::Docker)); + assert!(matches!(host.status(), ListingStatus::Ready)); + assert_eq!(host.snapshot().running(), 1); + }); + + let other = FakeDockerHost::new(true); + let elsewhere = watch(cx, &hosts, &other); + assert_ne!(elsewhere.entity_id(), first.entity_id()); + assert_eq!(other.count("docker events"), 1); +} + +#[gpui::test] +fn health_checks_list_nothing_again(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let _watched = watch(cx, &hosts, &local); + for _ in 0..20 { + for action in [ + "exec_create: pg_isready", + "exec_start: pg_isready", + "exec_die", + ] { + local.announce(&docker_event("container", action, "pg")); + } + settle(cx); + } + local.announce(&docker_event("container", "health_status: healthy", "pg")); + settle(cx); + assert_eq!(listings(&local), (2, 1), "a new health status, once"); + local.announce(&docker_event("container", "health_status: healthy", "pg")); + settle(cx); + assert_eq!(listings(&local), (2, 1), "the same status again is no news"); +} + +#[gpui::test] +fn a_change_lists_only_what_it_changed_once_per_burst(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let watched = watch(cx, &hosts, &local); + + local.set_containers(EXITED); + for action in ["kill", "die", "stop"] { + local.announce(&docker_event("container", action, "a1")); + } + settle(cx); + assert_eq!( + listings(&local), + (2, 1), + "containers once, images not at all" + ); + watched.read_with(cx, |host, _| { + assert_eq!(host.snapshot().containers[0].state, State::Exited); + }); + + local.set_images(IMAGE); + local.announce(&docker_event("image", "pull", "nginx:1")); + settle(cx); + assert_eq!(listings(&local), (2, 2), "images alone"); + watched.read_with(cx, |host, _| { + assert_eq!(host.snapshot().images[0].name(), "nginx:1"); + }); + assert!(local.following(0), "the same stream is still followed"); +} + +#[gpui::test] +fn asking_to_relist_reuses_the_followed_stream(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let watched = watch(cx, &hosts, &local); + watched.update(cx, |host, cx| host.relist(Changes::CONTAINERS, cx)); + settle(cx); + assert_eq!(listings(&local), (2, 1)); + watched.update(cx, |host, cx| host.relist(Changes::ALL, cx)); + settle(cx); + assert_eq!(listings(&local), (3, 2)); + assert_eq!(local.count("docker events"), 1); + assert!(local.following(0)); +} + +#[gpui::test] +fn the_watch_stops_when_nobody_holds_it(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let first = watch(cx, &hosts, &local); + let second = watch(cx, &hosts, &local); + drop(first); + release(cx); + assert!(local.following(0), "one window still shows it"); + drop(second); + release(cx); + assert_eq!(local.followers(), 0, "the last one let go"); + + let _again = watch(cx, &hosts, &local); + assert_eq!( + local.count("docker events"), + 2, + "a new watch, from the start" + ); + assert_eq!(local.followers(), 1); +} + +#[gpui::test] +fn without_events_the_host_is_listed_every_retry(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let _watched = watch(cx, &hosts, &local); + local.end_events(); + cx.run_until_parked(); + assert_eq!(listings(&local), (1, 1)); + cx.executor().advance_clock(RETRY); + cx.run_until_parked(); + assert_eq!(listings(&local), (2, 2)); + assert_eq!(local.count("docker events"), 2); +} + +#[gpui::test] +fn a_host_without_an_engine_is_asked_again_only_on_request(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let bare = FakeDockerHost::new(false); + let watched = watch(cx, &hosts, &bare); + watched.read_with(cx, |host, _| { + assert!(matches!( + host.status(), + ListingStatus::Failed(ContainersError::NotInstalled) + )); + }); + let asked = bare.ran().len(); + cx.executor().advance_clock(RETRY * 6); + cx.run_until_parked(); + assert_eq!(bare.ran().len(), asked, "not asked again by itself"); + + let same = watch(cx, &hosts, &bare); + assert_eq!(same.entity_id(), watched.entity_id()); + assert!( + bare.ran().len() > asked, + "showing it again looks for an engine anew" + ); +} + +#[gpui::test] +fn a_refresh_during_initial_listing_is_not_lost(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let gate = nocterm_session::testing::Gate::pending(); + let waiting = gate.clone(); + local.suspend_listing(async move { waiting.wait().await }.boxed()); + let watched = watch(cx, &hosts, &local); + assert_eq!( + local.count("docker events"), + 1, + "subscribed before snapshot" + ); + local.set_containers(EXITED); + watched.update(cx, |host, cx| host.relist(Changes::CONTAINERS, cx)); + gate.release(); + cx.run_until_parked(); + settle(cx); + watched.read_with(cx, |host, _| { + assert_eq!(host.snapshot().containers[0].state, State::Exited) + }); + assert_eq!(listings(&local), (2, 1)); +} + +#[gpui::test] +fn a_stalled_listing_times_out_and_can_be_retried(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + local.suspend_listing(futures::future::pending().boxed()); + let watched = watch(cx, &hosts, &local); + cx.executor() + .advance_clock(std::time::Duration::from_secs(31)); + cx.run_until_parked(); + watched.read_with(cx, |host, _| { + assert!(matches!( + host.status(), + ListingStatus::Failed(ContainersError::TimedOut) + )) + }); + watched.update(cx, |host, cx| host.relist(Changes::ALL, cx)); + cx.run_until_parked(); + watched.read_with(cx, |host, _| { + assert!(matches!(host.status(), ListingStatus::Ready)) + }); +} diff --git a/crates/containers-service/src/watch.rs b/crates/containers-service/src/watch.rs new file mode 100644 index 00000000..5df27623 --- /dev/null +++ b/crates/containers-service/src/watch.rs @@ -0,0 +1,175 @@ +//! Following one host's engine: a listing, then another only of what its events change. + +use std::{sync::Arc, time::Duration}; + +use futures::{ + FutureExt as _, StreamExt as _, + channel::mpsc, + future::{Either, select}, + pin_mut, +}; +use gpui::{AsyncApp, BackgroundExecutor, WeakEntity}; +use nocterm_containers::{Changes, ContainersError, Engine, EventReader}; +use nocterm_session::{ExecOutput, HostExec}; + +use crate::host::{HostContainers, Listed}; + +/// How long a failed listing, or an ended event stream, waits before the host is listed +/// again, unless someone asks sooner. +pub const RETRY: Duration = Duration::from_secs(10); +/// How long a burst of events (a Compose project starting) may take before the host is +/// listed once for all of it. +pub const SETTLE: Duration = Duration::from_millis(300); + +/// Lists the host and follows its changes, again after failures, until the host has no +/// engine or nobody holds the entity any more. +pub(crate) async fn watch( + this: WeakEntity, + exec: Arc, + mut engine: Option, + mut asked: mpsc::UnboundedReceiver, + cx: &mut AsyncApp, +) { + loop { + let outcome = follow(&this, exec.clone(), &mut engine, &mut asked, cx).await; + let retry = this.update(cx, |this, cx| this.ended(outcome, cx)); + if !matches!(retry, Ok(true)) { + return; + } + let timer = cx.background_executor().timer(RETRY); + let request = asked.next(); + pin_mut!(timer, request); + if let Either::Right((None, _)) = select(timer, request).await { + return; + } + } +} + +/// What woke the watch. +enum Heard { + Output(Vec), + Asked(Changes), + Ended, +} + +/// One attempt: a listing, then another of what changed after each burst of events that +/// change something, until the event stream ends. +async fn follow( + this: &WeakEntity, + exec: Arc, + engine: &mut Option, + asked: &mut mpsc::UnboundedReceiver, + cx: &mut AsyncApp, +) -> Result<(), ContainersError> { + // Only requests preceding this acquisition are covered by its snapshot. + while asked.try_recv().is_ok() {} + let executor = cx.background_executor().clone(); + let known = *engine; + let opening_exec = exec.clone(); + let (found, snapshot, mut events) = bounded(&executor, async move { + open(opening_exec.as_ref(), known).await + }) + .await?; + *engine = Some(found); + publish(this, found, Listed::Both(snapshot), cx)?; + let mut reader = EventReader::default(); + loop { + let mut changes = match next(&mut events, asked).await { + Heard::Output(chunk) => reader.feed(&chunk), + Heard::Asked(changes) => changes, + Heard::Ended => return Ok(()), + }; + if changes.is_empty() { + continue; + } + cx.background_executor().timer(SETTLE).await; + while let Some(Some(chunk)) = events.next().now_or_never() { + changes |= reader.feed(&chunk); + } + while let Ok(more) = asked.try_recv() { + changes |= more; + } + let exec = exec.clone(); + let listed = bounded(&executor, async move { + relist(found, exec.as_ref(), changes).await + }) + .await?; + publish(this, found, listed, cx)?; + } +} + +/// Subscribe before acquisition; changes arriving while listing stay in the stream. +async fn open( + exec: &dyn HostExec, + known: Option, +) -> Result<(Engine, nocterm_containers::Snapshot, ExecOutput), ContainersError> { + let mut failure = ContainersError::NotInstalled; + for engine in Engine::ALL + .into_iter() + .filter(|engine| known.is_none_or(|known| known == *engine)) + { + let events = match engine.events(exec).await { + Ok(events) => events, + Err(ContainersError::NotInstalled) => continue, + Err(error) => return Err(error), + }; + match engine.snapshot(exec).await { + Ok(snapshot) => return Ok((engine, snapshot, events)), + Err(error @ (ContainersError::Disconnected | ContainersError::Unsupported)) => { + return Err(error); + } + Err(ContainersError::NotInstalled) => {} + Err(error) => failure = error, + } + } + Err(failure) +} + +/// Decode and collection never occupy the foreground executor; deadline owns cancellation. +async fn bounded( + executor: &BackgroundExecutor, + operation: impl std::future::Future> + Send + 'static, +) -> Result { + let task = executor.spawn(operation); + let timer = executor.timer(Duration::from_secs(30)); + pin_mut!(task, timer); + match select(task, timer).await { + Either::Left((result, _)) => result, + Either::Right(_) => Err(ContainersError::TimedOut), + } +} + +async fn next(events: &mut ExecOutput, asked: &mut mpsc::UnboundedReceiver) -> Heard { + let output = events.next(); + let request = asked.next(); + pin_mut!(output, request); + match select(output, request).await { + Either::Left((Some(chunk), _)) => Heard::Output(chunk), + Either::Right((Some(changes), _)) => Heard::Asked(changes), + // The program ended, or nobody holds the entity any more. + Either::Left((None, _)) | Either::Right((None, _)) => Heard::Ended, + } +} + +/// Only the listings `changes` names. +async fn relist( + engine: Engine, + exec: &dyn HostExec, + changes: Changes, +) -> Result { + Ok(match (changes.containers(), changes.images()) { + (true, false) => Listed::Containers(engine.containers(exec).await?), + (false, true) => Listed::Images(engine.images(exec).await?), + _ => Listed::Both(engine.snapshot(exec).await?), + }) +} + +fn publish( + this: &WeakEntity, + engine: Engine, + listed: Listed, + cx: &mut AsyncApp, +) -> Result<(), ContainersError> { + this.update(cx, |this, cx| this.listed(engine, listed, cx)) + .map_err(|_| ContainersError::Disconnected) +} diff --git a/crates/containers-ui/Cargo.toml b/crates/containers-ui/Cargo.toml index 32c599a9..a66b2a76 100644 --- a/crates/containers-ui/Cargo.toml +++ b/crates/containers-ui/Cargo.toml @@ -11,6 +11,7 @@ layer = "feature" [dependencies] nocterm-containers.workspace = true +nocterm-containers-service.workspace = true nocterm-plugin-model.workspace = true nocterm-session.workspace = true nocterm-ui.workspace = true @@ -22,6 +23,7 @@ gpui-kit.workspace = true workspace = true [dev-dependencies] +nocterm-containers-service = { workspace = true, features = ["test-support"] } nocterm-store.workspace = true gpui-kit = { workspace = true, features = ["test-support"] } nocterm-settings.workspace = true diff --git a/crates/containers-ui/src/lib.rs b/crates/containers-ui/src/lib.rs index cdfab074..74288f7e 100644 --- a/crates/containers-ui/src/lib.rs +++ b/crates/containers-ui/src/lib.rs @@ -9,9 +9,12 @@ //! already has; the footer's switcher shows how many containers run. //! //! Owns: active-host container presentation, captured operation intents and workspace tabs. -//! Local host execution is supplied explicitly at registration and retained by the model. -//! Does not own: container command formats, host execution or connection establishment. -//! Ports used: workspace active-session/program contracts and session `HostExec`. +//! [`install`] retains the supplied local host execution and the application's +//! `ContainerHosts`; every window's model receives them at registration. +//! Does not own: container command formats, watching a host and deciding when to list it +//! again (`nocterm-containers-service`), host execution or connection establishment. +//! Ports used: workspace active-session/program contracts, session `HostExec` and the +//! shared `ContainerHosts`. //! Ports provided: the `containers` system plugin (`plugin::PLUGIN`) with its sidebar panel. //! Settings section: none. Globals: none. //! Invariants: host changes invalidate confirmations; completion retains its original context. @@ -21,6 +24,7 @@ mod panel; pub mod plugin; use gpui_kit::{AppContext as _, Context, Window}; +use nocterm_containers_service::ContainerHosts; use nocterm_session::HostExec; use nocterm_workspace::{PanelRegistration, RegistrationError, Workspace}; use std::sync::Arc; @@ -36,11 +40,28 @@ gpui_kit::actions!( ] ); +/// What every window's containers panel shares: this computer's executor, and the watch of +/// each host, so a host shown in several windows is watched once. +#[derive(Clone)] +pub struct Installed { + local: Option>, + hosts: ContainerHosts, +} + +/// Prepares the containers panels of every window, with `local` running this computer's +/// programs. +pub fn install(local: Option>) -> Installed { + Installed { + local, + hosts: ContainerHosts::new(), + } +} + /// Adds the containers panel to the sidebar of `workspace`. /// /// The action is registered even when the workspace refuses the panel. pub fn register( - local: Option>, + installed: &Installed, workspace: &mut Workspace, window: &mut Window, cx: &mut Context, @@ -48,12 +69,14 @@ pub fn register( workspace.register_action(|workspace, _: &ToggleContainers, window, cx| { workspace.toggle_panel(&plugin::LIST.id, window, cx); }); + let installed = installed.clone(); workspace.register_panel( PanelRegistration::new(&plugin::LIST, move |workspace, _, cx| { let session = workspace.active_session(cx); let handle = cx.entity(); + let Installed { local, hosts } = installed.clone(); let model = - cx.new(|cx| model::ContainersModel::new(&handle, session, local.clone(), cx)); + cx.new(|cx| model::ContainersModel::new(&handle, session, local, hosts, cx)); cx.new(|cx| ContainersPanel::new(model, handle.downgrade(), cx)) }), window, diff --git a/crates/containers-ui/src/model.rs b/crates/containers-ui/src/model.rs index 22c88dcf..8c280a2a 100644 --- a/crates/containers-ui/src/model.rs +++ b/crates/containers-ui/src/model.rs @@ -1,31 +1,30 @@ //! The containers of the active host, kept current. //! //! The model follows the workspace's active host ([`nocterm_workspace::host`]), -//! like the resource monitor, over the connection that host already has. It -//! keeps watching while the panel is hidden, so the footer's count stays -//! right. The engine's event stream says when to list again; where there is -//! none, or it ends, the listing is repeated every [`RETRY`]. +//! like the resource monitor, over the connection that host already has. Its +//! containers come from the application's [`ContainerHosts`], which watches each +//! host connection once for every window showing it. The model holds that watch +//! while the panel is hidden, so the footer's count stays right. -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; -use futures::FutureExt as _; -use gpui_kit::{AsyncApp, Context, Entity, Subscription, Task, WeakEntity}; -use nocterm_containers::{Action, ContainersError, Engine, Snapshot, detect}; +use gpui_kit::{App, Context, Entity, Subscription, Task}; +use nocterm_containers::{Action, Changes, ContainersError, Engine, Snapshot}; +use nocterm_containers_service::{ContainerHosts, HostContainers, ListingStatus}; use nocterm_session::{ExecRequest, HostExec}; use nocterm_workspace::{ Host, ProgramSpec, SessionContext, Workspace, host::follow_active_session, }; -/// How long a failed listing, or an ended event stream, waits before the -/// host is listed again. -const RETRY: Duration = Duration::from_secs(10); -/// How long a burst of events (a Compose project starting) may take before -/// the host is listed once for all of it. -const SETTLE: Duration = Duration::from_millis(300); +/// What a host shows before its engine has answered. +static NOTHING: Snapshot = Snapshot { + containers: Vec::new(), + images: Vec::new(), +}; /// Where the listing of the current host stands. #[derive(Debug)] -pub(crate) enum Status { +pub(crate) enum Status<'a> { /// There is no host to list. Off, /// The host's session is not connected. @@ -34,7 +33,7 @@ pub(crate) enum Status { Ready, /// The last attempt failed; unless the host has no engine, another /// follows shortly. - Failed(ContainersError), + Failed(&'a ContainersError), } /// A confirmed operation belongs to one observed host connection. Moving @@ -57,30 +56,33 @@ impl ActionIntent { pub(crate) struct ContainersModel { epoch: Arc<()>, local: Option>, + hosts: ContainerHosts, host: Option, - /// The engine the host was found to have. - engine: Option, - snapshot: Snapshot, - status: Status, - task: Option>, + /// The shared watch of a connected host. + watched: Option, _follow: Subscription, } +/// A host's shared watch, redrawn by this model when it changes. +struct Watched { + containers: Entity, + _observe: Subscription, +} + impl ContainersModel { pub(crate) fn new( workspace: &Entity, session: Option, local: Option>, + hosts: ContainerHosts, cx: &mut Context, ) -> Self { let mut this = Self { epoch: Arc::new(()), local, + hosts, host: None, - engine: None, - snapshot: Snapshot::default(), - status: Status::Off, - task: None, + watched: None, _follow: follow_active_session(workspace, cx, Self::follow), }; this.follow(session, cx); @@ -91,16 +93,33 @@ impl ContainersModel { self.host.as_ref() } - pub(crate) fn engine(&self) -> Option { - self.engine + fn containers<'a>(&self, cx: &'a App) -> Option<&'a HostContainers> { + self.watched + .as_ref() + .map(|watched| watched.containers.read(cx)) + } + + pub(crate) fn engine(&self, cx: &App) -> Option { + self.containers(cx)?.engine() } - pub(crate) fn snapshot(&self) -> &Snapshot { - &self.snapshot + pub(crate) fn snapshot<'a>(&self, cx: &'a App) -> &'a Snapshot { + self.containers(cx) + .map_or(&NOTHING, HostContainers::snapshot) } - pub(crate) fn status(&self) -> &Status { - &self.status + pub(crate) fn status<'a>(&self, cx: &'a App) -> Status<'a> { + let Some(host) = &self.host else { + return Status::Off; + }; + if !host.connected { + return Status::Offline; + } + match self.containers(cx).map(HostContainers::status) { + Some(ListingStatus::Ready) => Status::Ready, + Some(ListingStatus::Failed(error)) => Status::Failed(error), + Some(ListingStatus::Loading) | None => Status::Loading, + } } fn follow(&mut self, session: Option, cx: &mut Context) { @@ -113,49 +132,58 @@ impl ContainersModel { if unchanged { return; } - let key = |host: &Option| host.as_ref().map(|host| host.key.clone()); - if key(&self.host) != key(&host) { - self.engine = None; - self.snapshot = Snapshot::default(); - } self.epoch = Arc::new(()); + self.watched = host + .as_ref() + .filter(|host| host.connected) + .map(|host| self.watch(&host.exec, cx)); self.host = host; - self.refresh(cx); + cx.notify(); } - /// Lists the host again, and follows it from there. + fn watch(&self, exec: &Arc, cx: &mut Context) -> Watched { + let containers = self.hosts.watch(exec, cx); + Watched { + _observe: cx.observe(&containers, |_, _, cx| cx.notify()), + containers, + } + } + + /// Lists the host again, as the person asked. pub(crate) fn refresh(&mut self, cx: &mut Context) { - self.task = None; - let Some(host) = self.host.clone() else { - self.set_status(Status::Off, cx); - return; - }; - if !host.connected { - self.set_status(Status::Offline, cx); - return; + self.relist(Changes::ALL, cx); + } + + fn relist(&self, changes: Changes, cx: &mut Context) { + if let Some(watched) = &self.watched { + watched + .containers + .update(cx, |containers, cx| containers.relist(changes, cx)); } - self.set_status(Status::Loading, cx); - let engine = self.engine; - self.task = Some(cx.spawn(async move |this, cx| watch(this, host, engine, cx).await)); } - pub(crate) fn intent(&self, action: Action, ids: Vec) -> Option { + pub(crate) fn intent( + &self, + action: Action, + ids: Vec, + cx: &App, + ) -> Option { let host = self.host.clone()?; if !host.connected { return None; } Some(ActionIntent { host, - engine: self.engine?, + engine: self.engine(cx)?, epoch: self.epoch.clone(), action, ids, }) } - fn accepts(&self, intent: &ActionIntent) -> bool { + fn accepts(&self, intent: &ActionIntent, cx: &App) -> bool { Arc::ptr_eq(&self.epoch, &intent.epoch) - && self.engine == Some(intent.engine) + && self.engine(cx) == Some(intent.engine) && self .host .as_ref() @@ -169,7 +197,7 @@ impl ContainersModel { ids: Vec, cx: &mut Context, ) -> Task> { - let Some(intent) = self.intent(action, ids) else { + let Some(intent) = self.intent(action, ids, cx) else { return Task::ready(Err(ContainersError::Disconnected)); }; self.perform_intent(intent, cx) @@ -183,18 +211,27 @@ impl ContainersModel { cx: &mut Context, ) -> Task> { cx.spawn(async move |this, cx| { - let accepted = this.update(cx, |this, _| this.accepts(&intent)); + let accepted = this.update(cx, |this, cx| this.accepts(&intent, cx)); if !matches!(accepted, Ok(true)) { return Err(ContainersError::Disconnected); } - let done = intent - .engine - .perform(intent.host.exec.as_ref(), intent.action, &intent.ids) - .await; - // Finishing on A must not interrupt B's watcher. + let deadline = cx + .background_executor() + .timer(std::time::Duration::from_secs(75)); + let action = + intent + .engine + .perform(intent.host.exec.as_ref(), intent.action, &intent.ids); + futures::pin_mut!(deadline, action); + let done = match futures::future::select(action, deadline).await { + futures::future::Either::Left((result, _)) => result, + futures::future::Either::Right(_) => Err(ContainersError::TimedOut), + }; + // Its events say so too, but not every engine reports them. Finishing on A + // must not relist B. let _ = this.update(cx, |this, cx| { - if this.accepts(&intent) { - this.refresh(cx); + if this.accepts(&intent, cx) { + this.relist(intent.action.changes(), cx); } }); done @@ -207,94 +244,13 @@ impl ContainersModel { &self, title: String, program: impl FnOnce(Engine) -> ExecRequest, + cx: &App, ) -> Option { Some(ProgramSpec { title: title.into(), host: self.host.clone()?, - program: program(self.engine?), + program: program(self.engine(cx)?), shell_syntax: None, }) } - - fn apply(&mut self, engine: Engine, snapshot: Snapshot, cx: &mut Context) { - self.engine = Some(engine); - if self.snapshot != snapshot { - self.snapshot = snapshot; - cx.notify(); - } - self.set_status(Status::Ready, cx); - } - - fn set_status(&mut self, status: Status, cx: &mut Context) { - // Native error sources have no equality contract. Deduplicate only the - // visible status category and text; recovery always matches typed variants. - let unchanged = match (&self.status, &status) { - (Status::Failed(previous), Status::Failed(next)) => { - std::mem::discriminant(previous) == std::mem::discriminant(next) - && previous.to_string() == next.to_string() - } - (previous, next) => std::mem::discriminant(previous) == std::mem::discriminant(next), - }; - if !unchanged { - self.status = status; - cx.notify(); - } - } -} - -/// Lists the host and follows its changes, again after failures, until the -/// host has no engine or the model moves on. -async fn watch( - this: WeakEntity, - host: Host, - mut engine: Option, - cx: &mut AsyncApp, -) { - loop { - let outcome = follow_engine(&this, &host, &mut engine, cx).await; - let retry = this.update(cx, |this, cx| match outcome { - // The event stream ended; the listing still stands. - Ok(()) => true, - Err(error) => { - let retry = !matches!( - error, - ContainersError::NotInstalled | ContainersError::Unsupported - ); - this.set_status(Status::Failed(error), cx); - retry - } - }); - if !matches!(retry, Ok(true)) { - return; - } - cx.background_executor().timer(RETRY).await; - } -} - -/// One attempt: a listing, then another after each change the engine -/// reports, until its event stream ends. -async fn follow_engine( - this: &WeakEntity, - host: &Host, - engine: &mut Option, - cx: &mut AsyncApp, -) -> Result<(), ContainersError> { - let exec = host.exec.as_ref(); - let (found, snapshot) = match *engine { - Some(known) => (known, known.snapshot(exec).await?), - None => detect(exec).await?, - }; - *engine = Some(found); - let apply = |snapshot, cx: &mut AsyncApp| { - this.update(cx, |this, cx| this.apply(found, snapshot, cx)) - .map_err(|_| ContainersError::Disconnected) - }; - apply(snapshot, cx)?; - let mut events = found.events(exec).await?; - while events.next().await.is_some() { - cx.background_executor().timer(SETTLE).await; - while let Some(Some(_)) = events.next().now_or_never() {} - apply(found.snapshot(exec).await?, cx)?; - } - Ok(()) } diff --git a/crates/containers-ui/src/panel.rs b/crates/containers-ui/src/panel.rs index f74284c4..f7bbd064 100644 --- a/crates/containers-ui/src/panel.rs +++ b/crates/containers-ui/src/panel.rs @@ -18,7 +18,7 @@ use gpui_kit::{ div, prelude::*, }; -use nocterm_containers::{Action, ContainersError}; +use nocterm_containers::{Action, ContainersError, Engine}; use nocterm_ui::IconName; use nocterm_workspace::{Panel, ShellSyntax, Workspace}; @@ -119,14 +119,12 @@ impl ContainersPanel { Op::Logs => ("Logs", None), Op::Act(_) => return, }; - let Some(mut spec) = - self.model - .read(cx) - .program(format!("{label}: {}", subject.name), |engine| match op { - Op::Shell => engine.shell(id), - _ => engine.logs(id), - }) - else { + let title = format!("{label}: {}", subject.name); + let program = |engine: Engine| match op { + Op::Shell => engine.shell(id), + _ => engine.logs(id), + }; + let Some(mut spec) = self.model.read(cx).program(title, program, cx) else { return; }; spec.shell_syntax = shell_syntax; @@ -143,7 +141,7 @@ impl ContainersPanel { cx: &mut Context, ) { let Subject { kind, name, ids } = subject; - let Some(intent) = self.model.read(cx).intent(action, ids) else { + let Some(intent) = self.model.read(cx).intent(action, ids, cx) else { return; }; let panel = cx.entity().downgrade(); @@ -224,7 +222,7 @@ impl ContainersPanel { let host = model .host() .map_or_else(|| "No host".to_owned(), |host| host.key.label()); - let engine = model.engine().map(|engine| engine.name()); + let engine = model.engine(cx).map(|engine| engine.name()); let theme = cx.theme(); h_flex() .gap_1() @@ -250,8 +248,8 @@ impl ContainersPanel { /// A line on why there is nothing, or nothing current, to show. fn render_status(&self, cx: &mut Context) -> Option { let model = self.model.read(cx); - let found = model.engine().is_some(); - let (text, danger) = match model.status() { + let found = model.engine(cx).is_some(); + let (text, danger) = match model.status(cx) { Status::Ready => return None, Status::Loading if found => return None, Status::Loading => ("Looking for Docker or Podman…".to_owned(), false), @@ -292,7 +290,7 @@ impl Panel for ContainersPanel { /// How many containers run; none shows no count. fn badge(&self, cx: &App) -> Option { - let running = self.model.read(cx).snapshot().running(); + let running = self.model.read(cx).snapshot(cx).running(); (running > 0).then(|| running.to_string().into()) } } @@ -305,7 +303,7 @@ impl Focusable for ContainersPanel { impl Render for ContainersPanel { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { - let shown = self.model.read(cx).engine().is_some(); + let shown = self.model.read(cx).engine(cx).is_some(); v_flex() .track_focus(&self.focus_handle) .size_full() diff --git a/crates/containers-ui/src/panel/rows.rs b/crates/containers-ui/src/panel/rows.rs index 531a72ff..9e65a2cf 100644 --- a/crates/containers-ui/src/panel/rows.rs +++ b/crates/containers-ui/src/panel/rows.rs @@ -27,7 +27,7 @@ const PROJECT_GROUP: &str = "container-project"; impl ContainersPanel { pub(super) fn render_tree(&self, cx: &mut Context) -> impl IntoElement { - let snapshot = self.model.read(cx).snapshot().clone(); + let snapshot = self.model.read(cx).snapshot(cx).clone(); let mut tree = v_flex().gap_px().pb_1(); let containers = !self.folded.contains(&Fold::Containers); diff --git a/crates/containers-ui/src/plugin.rs b/crates/containers-ui/src/plugin.rs index 24bf7554..8aea3a3e 100644 --- a/crates/containers-ui/src/plugin.rs +++ b/crates/containers-ui/src/plugin.rs @@ -21,6 +21,7 @@ pub const LIST: SystemContribution = SystemContribution { chrome: PanelChrome::Titled, }), preview: None, + edge: None, pinned: false, }; diff --git a/crates/containers-ui/src/plugin_tests.rs b/crates/containers-ui/src/plugin_tests.rs index 3072549d..87a7de12 100644 --- a/crates/containers-ui/src/plugin_tests.rs +++ b/crates/containers-ui/src/plugin_tests.rs @@ -9,7 +9,7 @@ fn registered(cx: &mut TestAppContext) -> crate::tests::Fixture { let fixture = fixture(cx, None); cx.update_window(fixture.window, |_, window, cx| { fixture.workspace.update(cx, |workspace, cx| { - crate::register(None, workspace, window, cx).unwrap(); + crate::register(&crate::install(None), workspace, window, cx).unwrap(); }); }) .unwrap(); diff --git a/crates/containers-ui/src/tests.rs b/crates/containers-ui/src/tests.rs index d8e40011..ac17b56c 100644 --- a/crates/containers-ui/src/tests.rs +++ b/crates/containers-ui/src/tests.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use futures::FutureExt as _; use gpui_kit::{ @@ -6,7 +6,11 @@ use gpui_kit::{ TestAppContext, Window, WindowOptions, div, prelude::*, }; use nocterm_containers::{Action, ContainersError, Engine}; -use nocterm_session::{ExecExit, ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec, Target}; +use nocterm_containers_service::{ + ContainerHosts, + test_support::{EXITED, FakeDockerHost, RUNNING, docker_event}, +}; +use nocterm_session::{HostExec, Target}; use nocterm_settings::SettingsDocument; use nocterm_store::SettingsConfig; use nocterm_ui::DesignTokens; @@ -17,76 +21,11 @@ use crate::{ model::{ContainersModel, Status}, }; -const RUNNING: &str = - r#"{"ID":"a1","Image":"nginx","Names":"web","State":"running","Status":"Up 2 hours"}"#; -const EXITED: &str = - r#"{"ID":"a1","Image":"nginx","Names":"web","State":"exited","Status":"Exited (0)"}"#; +/// A host with a scripted Docker; see [`FakeDockerHost`]. +pub(crate) type FakeHost = FakeDockerHost; -/// A host whose `docker` lists `containers`, keeps `docker events` running -/// until the test prints to it, and does every other command it is asked. -pub(crate) struct FakeHost { - docker: bool, - containers: Mutex<&'static str>, - ran: Mutex>, - events: Mutex>, -} - -impl FakeHost { - pub(crate) fn new(docker: bool) -> Arc { - Arc::new(Self { - docker, - containers: Mutex::new(RUNNING), - ran: Mutex::default(), - events: Mutex::default(), - }) - } - - pub(crate) fn ran(&self) -> Vec { - self.ran.lock().unwrap().clone() - } - - /// Reports a change from the latest event stream. - fn announce(&self) { - let events = self.events.lock().unwrap(); - let sink = events.last().expect("events are followed"); - assert_eq!( - sink.send(b"container die\n".to_vec()).now_or_never(), - Some(true) - ); - } - - /// Whether the event stream started `index`th is still being read. - fn following(&self, index: usize) -> bool { - !self.events.lock().unwrap()[index].is_closed() - } -} - -impl HostExec for FakeHost { - fn exec(&self, request: ExecRequest) -> ExecFuture { - let line = request.command_line(); - self.ran.lock().unwrap().push(line.clone()); - let (sink, output) = ExecOutput::channel(); - if self.docker && line.starts_with("docker events") { - self.events.lock().unwrap().push(sink); - return Box::pin(async { Ok(output) }); - } - let (stdout, status) = match () { - _ if !self.docker => ("", 127), - _ if line.starts_with("docker ps") => (*self.containers.lock().unwrap(), 0), - _ if line.starts_with("docker images") => ("", 0), - _ if line.starts_with("docker") => ("", 0), - _ => ("", 127), - }; - assert_eq!( - sink.send(stdout.as_bytes().to_vec()).now_or_never(), - Some(true) - ); - sink.finish(ExecExit { - status: Some(status), - stderr: String::new(), - }); - Box::pin(async { Ok(output) }) - } +fn announce_exit(host: &FakeHost) { + host.announce(&docker_event("container", "die", "a1")); } struct SessionItem { @@ -142,7 +81,8 @@ pub(crate) fn fixture(cx: &mut TestAppContext, local: Option>) }) }) .unwrap(); - let model = cx.new(|cx| ContainersModel::new(&workspace, None, local, cx)); + let model = + cx.new(|cx| ContainersModel::new(&workspace, None, local, ContainerHosts::new(), cx)); let panel = cx.new(|cx| ContainersPanel::new(model.clone(), workspace.downgrade(), cx)); Fixture { window, @@ -160,11 +100,11 @@ fn assert_status( fixture: &Fixture, expected: impl FnOnce(&Status) -> bool, ) { - fixture.model.read_with(cx, |model, _| { + fixture.model.read_with(cx, |model, cx| { assert!( - expected(model.status()), + expected(&model.status(cx)), "unexpected status: {:?}", - model.status() + model.status(cx) ); }); } @@ -205,8 +145,8 @@ fn containers_are_listed_again_when_the_engine_reports_a_change(cx: &mut TestApp .any(|line| line.starts_with("docker events")) ); - *local.containers.lock().unwrap() = EXITED; - local.announce(); + local.set_containers(EXITED); + announce_exit(&local); cx.executor() .advance_clock(std::time::Duration::from_secs(1)); cx.run_until_parked(); @@ -225,12 +165,22 @@ fn actions_and_programs_run_on_the_followed_host(cx: &mut TestAppContext) { cx.run_until_parked(); assert!(matches!(done.now_or_never(), Some(Ok(())))); assert!(local.ran().contains(&"docker stop a1".to_owned())); - assert!(!local.following(0), "a refresh follows the engine anew"); + // With the events the action caused, once they settle. + cx.executor() + .advance_clock(nocterm_containers_service::SETTLE); + cx.run_until_parked(); + assert_eq!( + local.count("docker ps"), + 2, + "the containers are listed again" + ); + assert_eq!(local.count("docker images"), 1, "the images are not"); + assert!(local.following(0), "over the same event stream"); let spec = fixture .model - .read_with(cx, |model, _| { - model.program("Logs: web".into(), |engine| engine.logs("a1")) + .read_with(cx, |model, cx| { + model.program("Logs: web".into(), |engine| engine.logs("a1"), cx) }) .unwrap(); assert_eq!(spec.host.key, HostKey::Local); @@ -270,8 +220,11 @@ fn the_panel_follows_the_active_session_without_connections_of_its_own(cx: &mut assert!( fixture .model - .read_with(cx, |model, _| model - .program("Logs".into(), |e| e.logs("a1"))) + .read_with(cx, |model, cx| model.program( + "Logs".into(), + |e| e.logs("a1"), + cx + )) .is_none(), "no engine is known for it" ); @@ -294,9 +247,9 @@ fn nothing_is_listed_without_a_host(cx: &mut TestAppContext) { fn destructive_intents_cannot_move_to_another_host_or_reconnect(cx: &mut TestAppContext) { let local = FakeHost::new(true); let fixture = fixture(cx, Some(local.clone())); - let intent = fixture.model.read_with(cx, |model, _| { + let intent = fixture.model.read_with(cx, |model, cx| { model - .intent(Action::RemoveImage, vec!["same-image".into()]) + .intent(Action::RemoveImage, vec!["same-image".into()], cx) .unwrap() }); let another = FakeHost::new(true); @@ -312,9 +265,9 @@ fn destructive_intents_cannot_move_to_another_host_or_reconnect(cx: &mut TestApp assert!(!local.ran().iter().any(|line| line.contains("same-image"))); assert!(!another.ran().iter().any(|line| line.contains("same-image"))); - let intent = fixture.model.read_with(cx, |model, _| { + let intent = fixture.model.read_with(cx, |model, cx| { model - .intent(Action::RemoveImage, vec!["same-image".into()]) + .intent(Action::RemoveImage, vec!["same-image".into()], cx) .unwrap() }); // Same target, a new connection/executor. @@ -341,15 +294,18 @@ fn disconnecting_invalidates_captured_intent_and_rejects_new_actions(cx: &mut Te let host = FakeHost::new(true); let fixture = fixture(cx, None); open_session(cx, &fixture, remote(&host, true)); - let intent = fixture.model.read_with(cx, |model, _| { - model.intent(Action::Remove, vec!["a1".into()]).unwrap() + let intent = fixture.model.read_with(cx, |model, cx| { + model.intent(Action::Remove, vec!["a1".into()], cx).unwrap() }); open_session(cx, &fixture, remote(&host, false)); assert!( fixture .model - .read_with(cx, |model, _| model - .intent(Action::Remove, vec!["a1".into()])) + .read_with(cx, |model, cx| model.intent( + Action::Remove, + vec!["a1".into()], + cx + )) .is_none() ); let done = fixture @@ -367,7 +323,7 @@ fn disconnecting_invalidates_captured_intent_and_rejects_new_actions(cx: &mut Te #[gpui_kit::test] fn malformed_successful_listing_remains_a_retryable_failure(cx: &mut TestAppContext) { let local = FakeHost::new(true); - *local.containers.lock().unwrap() = "not JSON"; + local.set_containers("not JSON"); let fixture = fixture(cx, Some(local.clone())); assert_status(cx, &fixture, |status| { let Status::Failed(ContainersError::Parse(error)) = status else { @@ -379,7 +335,7 @@ fn malformed_successful_listing_remains_a_retryable_failure(cx: &mut TestAppCont ) }); let attempts = local.ran().len(); - *local.containers.lock().unwrap() = RUNNING; + local.set_containers(RUNNING); cx.executor() .advance_clock(std::time::Duration::from_secs(10)); cx.run_until_parked(); @@ -389,38 +345,45 @@ fn malformed_successful_listing_remains_a_retryable_failure(cx: &mut TestAppCont } #[gpui_kit::test] -fn registered_container_panels_keep_their_supplied_host_across_other_windows( - cx: &mut TestAppContext, -) { +fn windows_on_the_same_host_share_one_watch(cx: &mut TestAppContext) { let first = FakeHost::new(true); let second = FakeHost::new(true); - for host in [first.clone(), second.clone(), first.clone()] { + let shared = crate::install(Some(first.clone())); + let other = crate::install(Some(second.clone())); + for installed in [&shared, &other, &shared] { let fixture = fixture(cx, None); cx.update_window(fixture.window, |_, window, cx| { fixture.workspace.update(cx, |workspace, cx| { - crate::register(Some(host.clone()), workspace, window, cx).unwrap(); + crate::register(installed, workspace, window, cx).unwrap(); }); }) .unwrap(); cx.run_until_parked(); } - assert_eq!( - first - .ran() - .iter() - .filter(|command| command.starts_with("docker events")) - .count(), - 2 - ); - assert_eq!( - second - .ran() - .iter() - .filter(|command| command.starts_with("docker events")) - .count(), - 1 - ); + assert_eq!(first.count("docker events"), 1, "two windows, one watch"); + assert_eq!(first.count("docker ps"), 1); + assert_eq!(second.count("docker events"), 1); assert!(first.following(0)); - assert!(first.following(1)); assert!(second.following(0)); } + +#[gpui_kit::test] +fn health_checks_do_not_list_the_host_again(cx: &mut TestAppContext) { + let local = FakeHost::new(true); + let fixture = fixture(cx, Some(local.clone())); + for _ in 0..10 { + for action in [ + "exec_create: pg_isready", + "exec_start: pg_isready", + "exec_die", + ] { + local.announce(&docker_event("container", action, "pg")); + } + cx.executor() + .advance_clock(std::time::Duration::from_secs(3)); + cx.run_until_parked(); + } + assert_eq!(local.count("docker ps"), 1); + assert_eq!(local.count("docker images"), 1); + assert_eq!(badge(cx, &fixture).as_deref(), Some("1")); +} diff --git a/crates/containers/src/engine.rs b/crates/containers/src/engine.rs index 26602415..e43efd3d 100644 --- a/crates/containers/src/engine.rs +++ b/crates/containers/src/engine.rs @@ -3,7 +3,11 @@ use futures::future::try_join; use nocterm_session::{ExecOutput, ExecRequest, HostExec}; -use crate::{ContainersError, model::Snapshot, parse}; +use crate::{ + Changes, ContainersError, + model::{Container, Image, Snapshot}, + parse, +}; /// The most a listing may print; hosts with more containers than this /// describes are not served. @@ -48,6 +52,14 @@ impl Action { } } + /// The listings doing it changes. + pub fn changes(self) -> Changes { + match self { + Self::RemoveImage => Changes::ALL, + _ => Changes::CONTAINERS, + } + } + fn args(self) -> &'static [&'static str] { match self { Self::Start => &["start"], @@ -110,29 +122,39 @@ impl Engine { /// Every container, running or not, and every image. pub async fn snapshot(self, exec: &dyn HostExec) -> Result { - let list = |what: &str, all: &str| { - let request = self - .command() - .arg(what) - .arg(all) - .arg("--format") - .arg("json"); - run(exec, request) - }; - let (containers, images) = try_join(list("ps", "--all"), list("images", "--all")).await?; - Ok(Snapshot { - containers: parse::containers(&containers).map_err(ContainersError::Parse)?, - images: parse::images(&images).map_err(ContainersError::Parse)?, - }) + let (containers, images) = try_join(self.containers(exec), self.images(exec)).await?; + Ok(Snapshot { containers, images }) + } + + /// Every container, running or not. + pub async fn containers(self, exec: &dyn HostExec) -> Result, ContainersError> { + let output = run(exec, self.listing("ps")).await?; + parse::containers(&output).map_err(ContainersError::Parse) + } + + /// Every image, intermediate ones included. + pub async fn images(self, exec: &dyn HostExec) -> Result, ContainersError> { + let output = run(exec, self.listing("images")).await?; + parse::images(&output).map_err(ContainersError::Parse) + } + + fn listing(self, what: &str) -> ExecRequest { + self.command().args([what, "--all", "--format", "json"]) } - /// A stream that prints whenever a container or image changes, until it - /// is dropped. + /// A stream of what happens to containers and images, one JSON object + /// per line, until it is dropped; [`EventReader`](crate::EventReader) + /// reads it. pub async fn events(self, exec: &dyn HostExec) -> Result { let request = self .command() .args(["events", "--filter", "type=container"]) - .args(["--filter", "type=image"]); + .args(["--filter", "type=image", "--format"]) + .arg(match self { + // A template, which every Docker version reads. + Self::Docker => "{{json .}}", + Self::Podman => "json", + }); Ok(exec.exec(request).await?) } diff --git a/crates/containers/src/engine/tests.rs b/crates/containers/src/engine/tests.rs index 18d01169..98187825 100644 --- a/crates/containers/src/engine/tests.rs +++ b/crates/containers/src/engine/tests.rs @@ -111,7 +111,7 @@ fn logs_and_shells_are_programs_for_a_terminal() { block_on(Engine::Docker.events(&host)).unwrap(); assert_eq!( host.ran(), - ["docker events --filter type=container --filter type=image"] + ["docker events --filter type=container --filter type=image --format '{{json .}}'"] ); } @@ -230,3 +230,23 @@ fn malformed_podman_array_keeps_the_native_location_and_eof_cause() { assert!(source.column() > 0); assert!(source.is_eof()); } + +#[test] +fn containers_and_images_are_listed_on_their_own() { + let host = FakeExec::new(vec![ + ("docker ps --all", Ok((CONTAINER, 0, ""))), + ("docker images --all", Ok((IMAGE, 0, ""))), + ("podman events", Ok(("", 0, ""))), + ]); + let containers = block_on(Engine::Docker.containers(&host)).unwrap(); + assert_eq!(containers[0].name, "box"); + assert_eq!(host.ran(), ["docker ps --all --format json"]); + let images = block_on(Engine::Docker.images(&host)).unwrap(); + assert_eq!(images[0].name(), "alpine:3"); + assert_eq!(host.ran().len(), 2); + block_on(Engine::Podman.events(&host)).unwrap(); + assert_eq!( + host.ran()[2], + "podman events --filter type=container --filter type=image --format json" + ); +} diff --git a/crates/containers/src/error.rs b/crates/containers/src/error.rs index ed07140d..b844237d 100644 --- a/crates/containers/src/error.rs +++ b/crates/containers/src/error.rs @@ -46,6 +46,8 @@ pub enum ContainersError { Disconnected, #[error("This host cannot run programs.")] Unsupported, + #[error("The container engine did not finish the operation in time.")] + TimedOut, #[error("{0}")] Parse(#[from] ListingError), #[error("{0}")] @@ -64,7 +66,7 @@ impl From for ContainersError { ExecError::NotFound(_) => Self::NotInstalled, ExecError::Disconnected => Self::Disconnected, ExecError::Unsupported => Self::Unsupported, - error @ ExecError::Failed(_) => Self::Execution(error), + error @ (ExecError::Failed(_) | ExecError::Source(_)) => Self::Execution(error), } } } diff --git a/crates/containers/src/events.rs b/crates/containers/src/events.rs new file mode 100644 index 00000000..585968d8 --- /dev/null +++ b/crates/containers/src/events.rs @@ -0,0 +1,265 @@ +//! What an engine's event stream means for its listings. +//! +//! An engine reports far more than what changes a listing: a health check +//! inside a container is an `exec_create`, `exec_start` and `exec_die` every +//! few seconds, and Podman repeats an unchanged health status after every +//! check. Relisting for each of them makes the daemon walk every container +//! and image again and again, so [`EventReader`] keeps only the events that +//! change what a listing shows, and says which of the two listings they +//! change. A line it cannot read changes both: being unsure costs a listing, +//! never a stale one. + +use std::collections::HashMap; + +use serde::Deserialize; + +/// The longest event line kept; a longer one is dropped, as unreadable. +const MAX_LINE: usize = 64 * 1024; +/// The most containers whose health status is remembered at once. +const MAX_HEALTH: usize = 4096; + +/// Container events that never change a listing: commands run inside a +/// container (health checks are these), terminals attached to it and files +/// copied from it. +const QUIET_CONTAINER: [&str; 16] = [ + "exec_create", + "exec_start", + "exec_die", + "exec_detach", + "exec", + "exec_died", + "attach", + "detach", + "resize", + "top", + "copy", + "export", + "archive-path", + "extract-to-dir", + "mount", + "unmount", +]; +/// Image events that never change a listing. +const QUIET_IMAGE: [&str; 4] = ["push", "save", "mount", "unmount"]; +/// Image events that rename or remove an image a container may be shown by. +const RENAMING_IMAGE: [&str; 5] = ["tag", "untag", "delete", "remove", "prune"]; +/// The container event that makes a new image of a container. +const COMMIT: &str = "commit"; +const HEALTH: &str = "health_status"; + +/// Which listings an engine's events made stale. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Changes { + containers: bool, + images: bool, +} + +impl Changes { + /// Nothing to list again. + pub const NONE: Self = Self { + containers: false, + images: false, + }; + /// Both listings. + pub const ALL: Self = Self { + containers: true, + images: true, + }; + pub const CONTAINERS: Self = Self { + containers: true, + images: false, + }; + pub const IMAGES: Self = Self { + containers: false, + images: true, + }; + + pub fn containers(self) -> bool { + self.containers + } + + pub fn images(self) -> bool { + self.images + } + + pub fn is_empty(self) -> bool { + self == Self::NONE + } +} + +impl std::ops::BitOr for Changes { + type Output = Self; + + fn bitor(self, other: Self) -> Self { + Self { + containers: self.containers || other.containers, + images: self.images || other.images, + } + } +} + +impl std::ops::BitOrAssign for Changes { + fn bitor_assign(&mut self, other: Self) { + *self = *self | other; + } +} + +/// Reads the output of [`Engine::events`](crate::Engine::events), Docker's +/// or Podman's, in chunks as they arrive. +#[derive(Debug, Default)] +pub struct EventReader { + /// The end of the last chunk, up to its first newline. + partial: Vec, + /// The rest of a line too long to keep is skipped. + skipping: bool, + /// The last health status heard of each container. + health: HashMap, +} + +impl EventReader { + /// The listings the complete lines of `chunk`, with what came before it, + /// make stale. + pub fn feed(&mut self, chunk: &[u8]) -> Changes { + let mut changes = Changes::NONE; + let mut rest = chunk; + while let Some(end) = rest.iter().position(|&byte| byte == b'\n') { + let (line, after) = rest.split_at(end); + rest = &after[1..]; + if std::mem::take(&mut self.skipping) { + self.partial.clear(); + continue; + } + if self.partial.is_empty() { + changes |= self.line(line); + } else { + let mut whole = std::mem::take(&mut self.partial); + whole.extend_from_slice(line); + changes |= self.line(&whole); + } + } + if !self.skipping { + self.partial.extend_from_slice(rest); + if self.partial.len() > MAX_LINE { + self.partial = Vec::new(); + self.skipping = true; + changes = Changes::ALL; + } + } + changes + } + + fn line(&mut self, line: &[u8]) -> Changes { + if line.iter().all(u8::is_ascii_whitespace) { + return Changes::NONE; + } + let Ok(event) = serde_json::from_slice::(line) else { + return Changes::ALL; + }; + let Some(action) = event.action() else { + return Changes::ALL; + }; + let (name, detail) = match action.split_once(':') { + Some((name, detail)) => (name.trim(), Some(detail.trim())), + None => (action.trim(), None), + }; + match event.kind.as_str() { + "container" => self.container(&event, name, detail), + "image" => image(name), + // Only containers and images are asked for. + _ => Changes::NONE, + } + } + + fn container(&mut self, event: &Event, name: &str, detail: Option<&str>) -> Changes { + let id = event.id(); + if name == HEALTH { + let status = event.health.as_deref().or(detail); + return match (id, status) { + (Some(id), Some(status)) => self.health_changed(id, status), + _ => Changes::CONTAINERS, + }; + } + if QUIET_CONTAINER.contains(&name) { + return Changes::NONE; + } + // Whatever else happened to it, its next health status is news. + if let Some(id) = id { + self.health.remove(id); + } + if name == COMMIT { + Changes::ALL + } else { + Changes::CONTAINERS + } + } + + fn health_changed(&mut self, id: &str, status: &str) -> Changes { + if self.health.get(id).is_some_and(|known| known == status) { + return Changes::NONE; + } + if self.health.len() >= MAX_HEALTH && !self.health.contains_key(id) { + self.health.clear(); + } + self.health.insert(id.to_owned(), status.to_owned()); + Changes::CONTAINERS + } +} + +fn image(name: &str) -> Changes { + if QUIET_IMAGE.contains(&name) { + Changes::NONE + } else if RENAMING_IMAGE.contains(&name) { + Changes::ALL + } else { + Changes::IMAGES + } +} + +/// One event, as either engine prints it: Docker names what happened +/// `Action` (with a deprecated `status`) and the object in `Actor`, Podman +/// names it `Status` with the object's `ID` and a separate `HealthStatus`. +#[derive(Deserialize)] +struct Event { + #[serde(rename = "Type", default)] + kind: String, + #[serde(rename = "Action")] + action: Option, + #[serde(rename = "Status")] + podman_status: Option, + #[serde(rename = "status")] + docker_status: Option, + #[serde(rename = "Actor")] + actor: Option, + #[serde(rename = "ID")] + podman_id: Option, + #[serde(rename = "id")] + docker_id: Option, + #[serde(rename = "HealthStatus")] + health: Option, +} + +#[derive(Deserialize)] +struct Actor { + #[serde(rename = "ID")] + id: Option, +} + +impl Event { + fn action(&self) -> Option<&str> { + [&self.action, &self.podman_status, &self.docker_status] + .into_iter() + .find_map(|value| value.as_deref().filter(|value| !value.trim().is_empty())) + } + + fn id(&self) -> Option<&str> { + self.actor + .as_ref() + .and_then(|actor| actor.id.as_deref()) + .or(self.podman_id.as_deref()) + .or(self.docker_id.as_deref()) + .filter(|id| !id.is_empty()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/containers/src/events/tests.rs b/crates/containers/src/events/tests.rs new file mode 100644 index 00000000..9d32b51d --- /dev/null +++ b/crates/containers/src/events/tests.rs @@ -0,0 +1,170 @@ +use super::*; + +/// A Docker event line, as `docker events --format '{{json .}}'` prints it. +fn docker(kind: &str, action: &str, id: &str) -> String { + format!( + r#"{{"status":"{action}","id":"{id}","Type":"{kind}","Action":"{action}","Actor":{{"ID":"{id}","Attributes":{{"name":"pg"}}}},"scope":"local","time":1}}"# + ) + "\n" +} + +/// A Podman event line, as `podman events --format json` prints it. +fn podman(action: &str, id: &str, health: Option<&str>) -> String { + let health = health.map_or(String::new(), |health| { + format!(r#","HealthStatus":"{health}""#) + }); + format!(r#"{{"ID":"{id}","Name":"pg","Status":"{action}","Type":"container"{health}}}"#) + "\n" +} + +fn feed(reader: &mut EventReader, line: &str) -> Changes { + reader.feed(line.as_bytes()) +} + +#[test] +fn health_check_commands_change_no_listing() { + let mut reader = EventReader::default(); + for action in [ + "exec_create: pg_isready -U nocterm", + "exec_start: pg_isready -U nocterm", + "exec_die", + ] { + assert_eq!( + feed(&mut reader, &docker("container", action, "c1")), + Changes::NONE + ); + } + for action in ["exec", "exec_died"] { + assert_eq!( + feed(&mut reader, &podman(action, "c1", None)), + Changes::NONE + ); + } +} + +#[test] +fn container_life_changes_only_the_container_listing() { + let mut reader = EventReader::default(); + for action in [ + "create", "start", "die", "stop", "pause", "destroy", "rename", + ] { + assert_eq!( + feed(&mut reader, &docker("container", action, "c1")), + Changes::CONTAINERS, + "{action}" + ); + } + assert_eq!( + feed(&mut reader, &podman("died", "c1", None)), + Changes::CONTAINERS + ); + assert_eq!( + feed(&mut reader, &docker("container", "commit", "c1")), + Changes::ALL, + "a commit makes an image" + ); +} + +#[test] +fn image_events_change_the_image_listing_and_renames_both() { + let mut reader = EventReader::default(); + assert_eq!( + feed(&mut reader, &docker("image", "pull", "alpine:3")), + Changes::IMAGES + ); + assert_eq!( + feed(&mut reader, &docker("image", "push", "alpine:3")), + Changes::NONE + ); + for action in ["tag", "untag", "delete"] { + assert_eq!( + feed(&mut reader, &docker("image", action, "sha256:1")), + Changes::ALL, + "containers may be shown by the image's name: {action}" + ); + } + assert_eq!( + feed(&mut reader, &docker("network", "connect", "n1")), + Changes::NONE + ); +} + +#[test] +fn only_a_new_health_status_changes_the_listing() { + let mut reader = EventReader::default(); + let healthy = docker("container", "health_status: healthy", "c1"); + assert_eq!(feed(&mut reader, &healthy), Changes::CONTAINERS); + assert_eq!(feed(&mut reader, &healthy), Changes::NONE); + assert_eq!( + feed( + &mut reader, + &docker("container", "health_status: healthy", "c2") + ), + Changes::CONTAINERS, + "another container's status is its own" + ); + assert_eq!( + feed( + &mut reader, + &docker("container", "health_status: unhealthy", "c1") + ), + Changes::CONTAINERS + ); + + // Podman repeats the status after every check. + let repeated = podman("health_status", "p1", Some("healthy")); + assert_eq!(feed(&mut reader, &repeated), Changes::CONTAINERS); + assert_eq!(feed(&mut reader, &repeated), Changes::NONE); + // A restart resets the status the listing shows, so the same one is news again. + assert_eq!( + feed(&mut reader, &podman("restart", "p1", None)), + Changes::CONTAINERS + ); + assert_eq!(feed(&mut reader, &repeated), Changes::CONTAINERS); + // Without a status to compare, the listing is the only way to know. + let unknown = podman("health_status", "p1", None); + assert_eq!(feed(&mut reader, &unknown), Changes::CONTAINERS); +} + +#[test] +fn lines_split_across_chunks_are_read_whole() { + let mut reader = EventReader::default(); + let line = docker("container", "start", "c1"); + let (head, tail) = line.split_at(line.len() / 2); + assert_eq!(feed(&mut reader, head), Changes::NONE); + assert_eq!(feed(&mut reader, tail), Changes::CONTAINERS); + + let quiet = docker("container", "exec_die", "c1"); + let both = format!("{quiet}{}", docker("image", "pull", "alpine")); + assert_eq!(feed(&mut reader, &both), Changes::IMAGES); +} + +#[test] +fn unreadable_lines_relist_everything() { + let mut reader = EventReader::default(); + assert_eq!(feed(&mut reader, "\n \n"), Changes::NONE); + assert_eq!(feed(&mut reader, "container die\n"), Changes::ALL); + assert_eq!( + feed(&mut reader, "{\"Type\":\"container\"}\n"), + Changes::ALL + ); + + let long = "x".repeat(MAX_LINE + 1); + assert_eq!(feed(&mut reader, &long), Changes::ALL); + assert_eq!( + feed(&mut reader, "rest of it\n"), + Changes::NONE, + "the rest of a dropped line is skipped" + ); + assert_eq!( + feed(&mut reader, &docker("container", "start", "c1")), + Changes::CONTAINERS + ); +} + +#[test] +fn changes_combine() { + assert_eq!(Changes::CONTAINERS | Changes::IMAGES, Changes::ALL); + let mut changes = Changes::NONE; + assert!(changes.is_empty()); + changes |= Changes::IMAGES; + assert!(changes.images() && !changes.containers()); +} diff --git a/crates/containers/src/lib.rs b/crates/containers/src/lib.rs index 39b6aa70..ee99e9ab 100644 --- a/crates/containers/src/lib.rs +++ b/crates/containers/src/lib.rs @@ -1,11 +1,12 @@ //! Containers on a host, independent of any UI. //! -//! Owns: engine detection, CLI requests, listing parsing and container actions. +//! Owns: engine detection, CLI requests, listing parsing, which events change a listing +//! and container actions. //! Does not own: session creation, daemon sockets, credential storage or views. //! Ports used: HostExec. Ports provided: none. Settings section: none. Globals: none. //! Invariants: operations use the supplied host executor and its authority; //! unavailable engines remain distinct from typed execution or decoding failures; -//! listing output is bounded. +//! listing output and event lines are bounded; an unreadable event changes every listing. //! //! A host is asked about its containers through a [`HostExec`], with the //! container engine's own command line: Docker's, or Podman's where Docker @@ -14,7 +15,8 @@ //! with the rights of the user signed in. //! //! [`Engine`] builds the commands — listing, the event stream that says -//! when to list again, actions, and the programs a tab runs (a container's +//! when to list again (read by [`EventReader`], which keeps only the events +//! that change a listing and says which), actions, and the programs a tab runs (a container's //! log, a shell inside it) — and [`Snapshot`] is what a listing found, //! grouped by Compose project for display. //! @@ -22,11 +24,13 @@ mod engine; mod error; +mod events; mod model; mod parse; pub use engine::{Action, Engine, detect}; pub use error::{ContainersError, ListingEntity, ListingError}; +pub use events::{Changes, EventReader}; pub use model::{Container, Group, Image, Snapshot, State}; /// Decode a bounded container listing acquired by a host adapter. diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index dd623db2..abb93517 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -13,6 +13,7 @@ layer = "foundation" directories.workspace = true serde.workspace = true thiserror.workspace = true +sha2.workspace = true toml.workspace = true toml_edit.workspace = true tempfile.workspace = true diff --git a/crates/core/src/allocation.rs b/crates/core/src/allocation.rs new file mode 100644 index 00000000..60982fd1 --- /dev/null +++ b/crates/core/src/allocation.rs @@ -0,0 +1,121 @@ +//! Opaque allocation lifetime carriers shared by actual native quota owners. +//! +//! Counts describe the owner's real reservation. This module creates no quota, +//! authenticates no source, and grants no execution or publication authority. +use std::{fmt, sync::Arc}; + +struct RetainedAllocation { + bytes: usize, + payloads: usize, + _keepalive: Box, +} +/// Clones retain one actual reservation until the final lease is dropped. +#[derive(Clone)] +pub struct AllocationLease(Arc); +impl AllocationLease { + /// The supplied owner must retain the actual permits matching these immutable + /// counts. Descriptive counts or an arbitrary keepalive do not prove admission. + pub fn new(bytes: usize, payloads: usize, keepalive: impl Send + Sync + 'static) -> Self { + Self(Arc::new(RetainedAllocation { + bytes, + payloads, + _keepalive: Box::new(keepalive), + })) + } + pub fn bytes(&self) -> usize { + self.0.bytes + } + pub fn payloads(&self) -> usize { + self.0.payloads + } +} +impl fmt::Debug for AllocationLease { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AllocationLease") + .field("bytes", &self.bytes()) + .field("payloads", &self.payloads()) + .finish_non_exhaustive() + } +} +/// Owns the actual value and its reservation together. Share this generation with +/// `Arc>`; a deep clone of T requires a separate real reservation. +/// There is deliberately no Clone or uncharged consuming extraction API. +pub struct Allocated { + // Field order keeps the reservation alive through destruction of the value. + value: T, + allocation: AllocationLease, +} +impl Allocated { + pub fn new(value: T, allocation: AllocationLease) -> Self { + Self { value, allocation } + } + pub fn value(&self) -> &T { + &self.value + } + pub fn allocation(&self) -> &AllocationLease { + &self.allocation + } + /// Retains the same reservation throughout a consuming transformation. + /// The owner must first reserve separately if the transformation can exceed + /// this reservation or retain another independently allocated generation. + pub fn map(self, transform: impl FnOnce(T) -> U) -> Allocated { + let Self { value, allocation } = self; + Allocated { + value: transform(value), + allocation, + } + } +} +impl fmt::Debug for Allocated { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Allocated") + .field("allocation", &self.allocation) + .finish_non_exhaustive() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + struct Guard(Arc); + impl Drop for Guard { + fn drop(&mut self) { + self.0.fetch_add(1, Ordering::SeqCst); + } + } + #[test] + fn shared_generations_keep_one_actual_reservation_until_the_last_reference_drops() { + let released = Arc::new(AtomicUsize::new(0)); + let allocation = AllocationLease::new(8, 1, Guard(released.clone())); + let observer = allocation.clone(); + let value = Arc::new(Allocated::new(7u32, allocation)); + let native_worker = value.clone(); + drop(value); + drop(observer); + assert_eq!(released.load(Ordering::SeqCst), 0); + assert_eq!( + ( + *native_worker.value(), + native_worker.allocation().bytes(), + native_worker.allocation().payloads() + ), + (7, 8, 1) + ); + drop(native_worker); + assert_eq!(released.load(Ordering::SeqCst), 1); + } + #[test] + fn consuming_a_value_transfers_its_allocation_without_an_uncharged_gap() { + let released = Arc::new(AtomicUsize::new(0)); + let value = Allocated::new(3u32, AllocationLease::new(8, 0, Guard(released.clone()))); + let changed = value.map(|value| { + assert_eq!(released.load(Ordering::SeqCst), 0); + u64::from(value) + 1 + }); + assert_eq!(*changed.value(), 4); + assert_eq!(released.load(Ordering::SeqCst), 0); + drop(changed); + assert_eq!(released.load(Ordering::SeqCst), 1); + } +} diff --git a/crates/core/src/bounded_io.rs b/crates/core/src/bounded_io.rs new file mode 100644 index 00000000..ac19722b --- /dev/null +++ b/crates/core/src/bounded_io.rs @@ -0,0 +1,103 @@ +//! In-memory byte sinks with caller-supplied bounds; no filesystem or codec policy. +use std::io::{self, Write}; + +/// Counts bytes without retaining them and refuses writes beyond its limit. +pub struct CountingWriter { + count: usize, + limit: usize, +} +impl CountingWriter { + pub fn new(limit: usize) -> Self { + Self { count: 0, limit } + } + pub fn count(&self) -> usize { + self.count + } +} +impl Write for CountingWriter { + fn write(&mut self, bytes: &[u8]) -> io::Result { + self.count = self + .count + .checked_add(bytes.len()) + .filter(|count| *count <= self.limit) + .ok_or_else(capacity)?; + Ok(bytes.len()) + } + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +/// Retains at most `limit` bytes; a rejected write leaves the buffer untouched. +pub struct BoundedBuffer { + bytes: Vec, + limit: usize, + exceeded: bool, +} +impl BoundedBuffer { + pub fn new(limit: usize) -> Self { + Self::with_capacity(limit, 0) + } + pub fn with_capacity(limit: usize, capacity: usize) -> Self { + Self { + bytes: Vec::with_capacity(capacity.min(limit)), + limit, + exceeded: false, + } + } + pub fn exceeded(&self) -> bool { + self.exceeded + } + pub fn into_inner(self) -> Vec { + self.bytes + } +} +impl Write for BoundedBuffer { + fn write(&mut self, bytes: &[u8]) -> io::Result { + if bytes.len() > self.limit.saturating_sub(self.bytes.len()) { + self.exceeded = true; + return Err(capacity()); + } + self.bytes.extend_from_slice(bytes); + Ok(bytes.len()) + } + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} +fn capacity() -> io::Error { + io::ErrorKind::OutOfMemory.into() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn exact_limit_is_accepted_and_failed_write_retains_prefix() { + let mut output = BoundedBuffer::with_capacity(3, 100); + output.write_all(b"abc").unwrap(); + assert!(!output.exceeded()); + assert_eq!( + output.write_all(b"d").unwrap_err().kind(), + io::ErrorKind::OutOfMemory + ); + assert!(output.exceeded()); + assert_eq!(output.into_inner(), b"abc"); + } + + #[test] + fn counting_refuses_overflow_without_changing_accepted_count() { + let mut output = CountingWriter::new(3); + output.write_all(b"abc").unwrap(); + assert_eq!( + output.write_all(b"d").unwrap_err().kind(), + io::ErrorKind::OutOfMemory + ); + assert_eq!(output.count(), 3); + output.write_all(b"").unwrap(); + let mut empty = CountingWriter::new(0); + assert!(empty.write_all(b"x").is_err()); + assert_eq!(empty.count(), 0); + } +} diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index d0053c15..14f9f43b 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -1,6 +1,21 @@ //! Immutable shared causes preserve the original concrete error in source chains. use std::{error::Error, fmt, sync::Arc}; +/// The error and every cause beneath it as one line, outermost first, for a log record. +/// +/// It prints only what each frame's `Display` shows; callers log chains of typed errors +/// whose messages carry no secrets. +pub fn chain(error: &(dyn Error + 'static)) -> String { + let mut line = error.to_string(); + let mut cause = error.source(); + while let Some(next) = cause { + line.push_str(": "); + line.push_str(&next.to_string()); + cause = next.source(); + } + line +} + /// Cheaply clone an error without requiring the original error to implement Clone. /// /// The holder is one source frame; the next frame is the original error, including diff --git a/crates/core/src/error/tests.rs b/crates/core/src/error/tests.rs index 8e1a1d86..069ef7e0 100644 --- a/crates/core/src/error/tests.rs +++ b/crates/core/src/error/tests.rs @@ -74,3 +74,14 @@ fn construction_from_arc_reuses_the_existing_allocation() { erased.downcast_ref().unwrap() )); } + +#[test] +fn a_chain_lists_the_error_and_every_cause_outermost_first() { + let error = Operation(SharedCause::new(io::Error::new( + io::ErrorKind::PermissionDenied, + "denied", + ))); + assert_eq!(chain(&error), "operation failed: denied: denied: denied"); + let plain = io::Error::new(io::ErrorKind::NotFound, "missing"); + assert_eq!(chain(&plain), "missing"); +} diff --git a/crates/core/src/fs.rs b/crates/core/src/fs.rs index 6022b5dd..0289aeb4 100644 --- a/crates/core/src/fs.rs +++ b/crates/core/src/fs.rs @@ -4,8 +4,10 @@ //! directories must be trusted: this is not a pinned-directory traversal API. mod bounded; +mod private; pub use bounded::{BoundedReadError, read_regular_bounded}; +pub use private::{PrivateFileError, open_private_regular, require_private_regular}; use std::{ fs::{self, File, OpenOptions, Permissions}, @@ -79,6 +81,19 @@ pub struct AtomicPublication { destination: PathBuf, } +/// A completed namespace replacement whose parent directory still needs best-effort sync. +/// Dropping this receipt cannot remove or roll back the published file. +#[must_use = "finish parent sync outside the owner's publication lock"] +pub struct CommittedPublication { + destination: PathBuf, +} + +impl CommittedPublication { + pub fn sync_parent(self) { + sync_parent_best_effort(&self.destination); + } +} + impl AtomicPublication { /// Stages a mode-0600 snapshot in an existing parent directory. pub fn stage_private(path: &Path, bytes: &[u8]) -> Result { @@ -139,15 +154,46 @@ impl AtomicPublication { /// in-memory state. Best-effort directory sync cannot turn a commit into an /// apparent failed write. Ancestors are not pinned against concurrent changes. pub fn publish(self) -> Result<(), FileError> { + self.commit()?.sync_parent(); + Ok(()) + } + + /// Commits only the namespace replacement. The caller can publish matching + /// memory under its authority lock, then sync the parent after releasing it. + pub fn commit(self) -> Result { self.temporary .persist(&self.destination) .map_err(|error| FileError::at(&self.destination, error.error))?; - #[cfg(unix)] - if let Ok(directory) = File::open(parent_directory(&self.destination)) { - let _ = directory.sync_all(); - } - Ok(()) + Ok(CommittedPublication { + destination: self.destination, + }) + } + + /// Publishes only if the final path does not exist, including a dangling link. + /// Returns the actual published descriptor; no destination is overwritten. + /// + /// Success commits the namespace operation. The tempfile fallback can leave + /// a second staging hard link, and parent sync is best effort. A private-key + /// owner must validate this descriptor and confirm held-parent durability + /// before announcing readiness. A postcommit refusal must retain the identity, + /// rather than deleting or silently regenerating the destination. + pub fn publish_new(self) -> Result { + let file = self + .temporary + .persist_noclobber(&self.destination) + .map_err(|error| FileError::at(&self.destination, error.error))?; + sync_parent_best_effort(&self.destination); + Ok(file) + } +} + +fn sync_parent_best_effort(path: &Path) { + #[cfg(unix)] + if let Ok(directory) = File::open(parent_directory(path)) { + let _ = directory.sync_all(); } + #[cfg(not(unix))] + let _ = path; } fn parent_directory(path: &Path) -> &Path { diff --git a/crates/core/src/fs/bounded.rs b/crates/core/src/fs/bounded.rs index 242d2c48..eb950bd5 100644 --- a/crates/core/src/fs/bounded.rs +++ b/crates/core/src/fs/bounded.rs @@ -47,23 +47,7 @@ impl BoundedReadError { /// Parent directories must be trusted: this is not a pinned-directory traversal API. Callers /// that hash the content hash the returned buffer, which is the exact content they parse. pub fn read_regular_bounded(path: &Path, cap: usize) -> Result, BoundedReadError> { - let file = open_without_following(path).map_err(|source| { - if is_link_refusal(&source) { - BoundedReadError::NotRegular { - path: path.to_owned(), - } - } else { - BoundedReadError::io(path, source) - } - })?; - let metadata = file - .metadata() - .map_err(|source| BoundedReadError::io(path, source))?; - if !metadata.is_file() || is_reparse_point(&metadata) { - return Err(BoundedReadError::NotRegular { - path: path.to_owned(), - }); - } + let (file, metadata) = open_regular(path)?; let limit = u64::try_from(cap).unwrap_or(u64::MAX); if metadata.len() > limit { return Err(BoundedReadError::TooLarge { @@ -84,6 +68,27 @@ pub fn read_regular_bounded(path: &Path, cap: usize) -> Result, BoundedR Ok(bytes) } +pub(super) fn open_regular(path: &Path) -> Result<(File, Metadata), BoundedReadError> { + let file = open_without_following(path).map_err(|source| { + if is_link_refusal(&source) { + BoundedReadError::NotRegular { + path: path.to_owned(), + } + } else { + BoundedReadError::io(path, source) + } + })?; + let metadata = file + .metadata() + .map_err(|source| BoundedReadError::io(path, source))?; + if !metadata.is_file() || is_reparse_point(&metadata) { + return Err(BoundedReadError::NotRegular { + path: path.to_owned(), + }); + } + Ok((file, metadata)) +} + #[cfg(unix)] fn open_without_following(path: &Path) -> io::Result { use nix::fcntl::OFlag; diff --git a/crates/core/src/fs/private.rs b/crates/core/src/fs/private.rs new file mode 100644 index 00000000..709fc948 --- /dev/null +++ b/crates/core/src/fs/private.rs @@ -0,0 +1,72 @@ +//! Validate the actual opened descriptor before a private owner reads secrets. +use super::bounded::{BoundedReadError, open_regular}; +use std::{ + fs::{File, Metadata}, + io, + path::Path, +}; + +#[derive(Debug, thiserror::Error)] +pub enum PrivateFileError { + #[error("the private file could not be opened")] + Open(#[source] BoundedReadError), + #[error("the opened private file could not be inspected")] + Inspect(#[source] io::Error), + #[error("the opened file is not an owner-only single-link regular file")] + Policy, + #[error("private file ownership checks are unavailable on this platform")] + Unsupported, +} +impl PrivateFileError { + pub fn io_kind(&self) -> Option { + match self { + Self::Open(error) => error.io_kind(), + Self::Inspect(error) => Some(error.kind()), + Self::Policy | Self::Unsupported => None, + } + } +} + +/// Opens without following the final link and checks the opened inode's policy. +/// On Unix only current effective-UID ownership, one link and exact0400/0600 +/// permissions are accepted. Other platforms refuse before opening the path. +/// +/// No secret bytes are read here. The owner supplies its fixed zeroizing buffer +/// and bounds reads, including one-byte growth/overflow detection. Parents must +/// be trusted; this does not pin directory ancestry or create process isolation. +pub fn open_private_regular(path: &Path) -> Result { + if !cfg!(unix) { + return Err(PrivateFileError::Unsupported); + } + let (file, metadata) = open_regular(path).map_err(PrivateFileError::Open)?; + check(&metadata)?; + Ok(file) +} + +/// Applies the same policy to a held descriptor, including after publication. +/// A refusal after publication does not remove or roll back that published file. +pub fn require_private_regular(file: &File) -> Result<(), PrivateFileError> { + if !cfg!(unix) { + return Err(PrivateFileError::Unsupported); + } + let metadata = file.metadata().map_err(PrivateFileError::Inspect)?; + check(&metadata) +} + +#[cfg(unix)] +fn check(metadata: &Metadata) -> Result<(), PrivateFileError> { + use std::os::unix::fs::MetadataExt as _; + if metadata.is_file() + && metadata.uid() == nix::unistd::Uid::effective().as_raw() + && metadata.nlink() == 1 + && matches!(metadata.mode() & 0o7777, 0o400 | 0o600) + { + Ok(()) + } else { + Err(PrivateFileError::Policy) + } +} +#[cfg(not(unix))] +fn check(_: &Metadata) -> Result<(), PrivateFileError> { + Err(PrivateFileError::Unsupported) +} diff --git a/crates/core/src/fs/tests.rs b/crates/core/src/fs/tests.rs index 445c7ef8..2f02818d 100644 --- a/crates/core/src/fs/tests.rs +++ b/crates/core/src/fs/tests.rs @@ -1,5 +1,26 @@ use super::*; +#[test] +fn namespace_commit_is_visible_before_parent_sync_and_receipt_drop_never_rolls_it_back() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("snapshot"); + fs::write(&path, b"old").unwrap(); + let committed = AtomicPublication::stage_private(&path, b"new") + .unwrap() + .commit() + .unwrap(); + assert_eq!(fs::read(&path).unwrap(), b"new"); + drop(committed); + assert_eq!(fs::read(&path).unwrap(), b"new"); + assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1); + AtomicPublication::stage_private(&path, b"synced") + .unwrap() + .commit() + .unwrap() + .sync_parent(); + assert_eq!(fs::read(path).unwrap(), b"synced"); +} + #[test] fn binary_snapshot_round_trips_and_creates_parents() { let directory = tempfile::tempdir().unwrap(); @@ -34,6 +55,155 @@ fn failed_publication_preserves_destination_and_cleans_staging() { assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1); } +#[test] +fn new_publication_has_one_winner_and_never_overwrites_an_existing_identity() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("identity"); + let first = AtomicPublication::stage_private(&path, b"first identity").unwrap(); + let second = AtomicPublication::stage_private(&path, b"second identity").unwrap(); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(2)); + let ready = barrier.clone(); + let one = std::thread::spawn(move || { + ready.wait(); + first.publish_new() + }); + let two = std::thread::spawn(move || { + barrier.wait(); + second.publish_new() + }); + let outcomes = [one.join().unwrap(), two.join().unwrap()]; + assert_eq!(outcomes.iter().filter(|result| result.is_ok()).count(), 1); + assert_eq!( + outcomes + .iter() + .filter_map(|result| result.as_ref().err()) + .filter(|error| error.kind() == io::ErrorKind::AlreadyExists) + .count(), + 1 + ); + let bytes = fs::read(&path).unwrap(); + assert!(bytes == b"first identity" || bytes == b"second identity"); + assert!( + AtomicPublication::stage_private(&path, b"replacement") + .unwrap() + .publish_new() + .is_err() + ); + assert_eq!(fs::read(&path).unwrap(), bytes); + assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1); +} + +#[cfg(unix)] +#[test] +fn new_publication_preserves_live_and_dangling_links_without_reading_their_target() { + let directory = tempfile::tempdir().unwrap(); + let target = directory.path().join("target"); + let path = directory.path().join("identity"); + fs::write(&target, b"preserve").unwrap(); + std::os::unix::fs::symlink(&target, &path).unwrap(); + for exists in [true, false] { + if !exists { + fs::remove_file(&target).unwrap(); + } + let result = AtomicPublication::stage_private(&path, b"new identity") + .unwrap() + .publish_new(); + assert_eq!(result.unwrap_err().kind(), io::ErrorKind::AlreadyExists); + assert_eq!(fs::read_link(&path).unwrap(), target); + if exists { + assert_eq!(fs::read(&target).unwrap(), b"preserve"); + } else { + assert!(!target.exists()); + } + } +} + +#[cfg(unix)] +#[test] +fn private_open_checks_actual_permissions_and_links_and_retains_original_file() { + use std::{io::Read as _, os::unix::fs::PermissionsExt as _}; + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("identity"); + let published = AtomicPublication::stage_private(&path, b"original") + .unwrap() + .publish_new() + .unwrap(); + require_private_regular(&published).unwrap(); + for mode in [0o600, 0o400] { + fs::set_permissions(&path, Permissions::from_mode(mode)).unwrap(); + let mut original = open_private_regular(&path).unwrap(); + let mut bytes = Vec::new(); + original.read_to_end(&mut bytes).unwrap(); + assert_eq!(bytes, b"original"); + } + for mode in [0o640, 0o604, 0o700, 0o4600] { + fs::set_permissions(&path, Permissions::from_mode(mode)).unwrap(); + assert!(matches!( + open_private_regular(&path), + Err(PrivateFileError::Policy) + )); + } + fs::set_permissions(&path, Permissions::from_mode(0o600)).unwrap(); + let link = directory.path().join("backup-link"); + fs::hard_link(&path, &link).unwrap(); + assert!(matches!( + require_private_regular(&published), + Err(PrivateFileError::Policy) + )); + assert!(matches!( + open_private_regular(&path), + Err(PrivateFileError::Policy) + )); + assert_eq!(fs::read(&path).unwrap(), b"original"); + fs::remove_file(&link).unwrap(); + let mut original = open_private_regular(&path).unwrap(); + let replacement = directory.path().join("replacement"); + fs::write(&replacement, b"replacement").unwrap(); + fs::rename(&replacement, &path).unwrap(); + let mut bytes = Vec::new(); + original.read_to_end(&mut bytes).unwrap(); + assert_eq!(bytes, b"original"); + assert_eq!(fs::read(path).unwrap(), b"replacement"); +} + +#[cfg(unix)] +#[test] +fn private_open_refuses_final_links_directories_and_fifo_without_reading() { + let directory = tempfile::tempdir().unwrap(); + let target = directory.path().join("identity"); + drop( + AtomicPublication::stage_private(&target, b"identity") + .unwrap() + .publish_new() + .unwrap(), + ); + let link = directory.path().join("link"); + std::os::unix::fs::symlink(&target, &link).unwrap(); + assert!(matches!( + open_private_regular(&link), + Err(PrivateFileError::Open(BoundedReadError::NotRegular { .. })) + )); + assert!(matches!( + open_private_regular(directory.path()), + Err(PrivateFileError::Open(BoundedReadError::NotRegular { .. })) + )); + let fifo = directory.path().join("pipe"); + nix::unistd::mkfifo(&fifo, nix::sys::stat::Mode::from_bits_truncate(0o600)).unwrap(); + assert!(matches!( + open_private_regular(&fifo), + Err(PrivateFileError::Open(BoundedReadError::NotRegular { .. })) + )); +} + +#[cfg(not(unix))] +#[test] +fn private_open_refuses_unsupported_platform_before_opening() { + assert!(matches!( + open_private_regular(Path::new("not-opened")), + Err(PrivateFileError::Unsupported) + )); +} + #[test] fn parent_creation_failure_leaves_existing_file_intact() { let directory = tempfile::tempdir().unwrap(); diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 6d407010..611931c6 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -1,8 +1,11 @@ //! Shared error ownership, filesystem primitives and standard application locations. //! -//! Owns: paths, exclusive private creation, atomic publication, bounded no-follow reads of -//! regular files, TOML persistence, -//! ordered write queues, shutdown deadlines, immutable concrete error causes and the wall-clock +//! Owns: paths, exclusive private creation, replacing or no-clobber atomic publication, +//! strict opened private-file policy, bounded no-follow reads of +//! regular files, bounded in-memory byte sinks, TOML persistence with cooperative +//! document locks and observed-revision conflict detection, +//! ordered write queues, shutdown deadlines, opaque allocation lifetime carriers, +//! immutable concrete error causes and the wall-clock //! contract (`clock::{WallClock, WallTime}`; no I/O, no time-zone data). //! Does not own: transport protocols, encryption, storage schemas, views or tasks. //! Ports used: none. Ports provided: `clock::WallClock`, implemented by `nocterm-local`; its @@ -12,6 +15,8 @@ //! opens before it checks, refuses links, pipes and devices, and never exceeds its cap. //! Parent directories must be trusted; traversal capabilities belong to adapters. +pub mod allocation; +pub mod bounded_io; pub mod clock; pub mod error; pub mod fs; diff --git a/crates/core/src/persist.rs b/crates/core/src/persist.rs index 70e05f56..8f2485f3 100644 --- a/crates/core/src/persist.rs +++ b/crates/core/src/persist.rs @@ -13,7 +13,9 @@ use serde::{Serialize, de::DeserializeOwned}; use toml_edit::{DocumentMut, Item, Table}; pub mod queue; +mod revisioned; mod shutdown; +pub use revisioned::RevisionedFile; pub use shutdown::{ShutdownDeadline, WriteGate}; @@ -30,6 +32,10 @@ pub enum PersistError { path: PathBuf, source: Box, }, + /// An observed document changed before this owner published its next edit. + Conflict { + path: PathBuf, + }, Serialize { path: PathBuf, source: Box, @@ -39,6 +45,11 @@ pub enum PersistError { impl fmt::Display for PersistError { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + Self::Conflict { path } => write!( + formatter, + "{} changed outside this owner; reload before saving", + path.display() + ), Self::Io { path, source } => write!(formatter, "{}: {source}", path.display()), Self::Parse { path, source } => write!(formatter, "{}: {source}", path.display()), Self::Serialize { path, source } => write!(formatter, "{}: {source}", path.display()), @@ -48,6 +59,7 @@ impl fmt::Display for PersistError { impl std::error::Error for PersistError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { + Self::Conflict { .. } => None, Self::Io { source, .. } => Some(source), Self::Parse { source, .. } => Some(source.as_ref()), Self::Serialize { source, .. } => Some(source.as_ref()), @@ -101,25 +113,58 @@ pub fn save_text(path: &Path, contents: &str) -> Result<(), PersistError> { /// is first copied to `.bak`, so a hand-edited file is never lost to a /// syntax error. pub fn save_preserving(path: &Path, value: &T) -> Result<(), PersistError> { - let fresh = serialize(path, value)? - .parse::() - .expect("serialised TOML is valid TOML"); - - let mut document = match read(path)? { - None => DocumentMut::new(), - Some(text) => match text.parse::() { - Ok(document) => document, + save_preserving_owned(path, value, |_| true) +} + +/// Replaces owned top-level tables while preserving the current unowned tables. +/// The formatting document is acquired and parsed once for the whole merge. +pub fn save_preserving_owned( + path: &Path, + value: &T, + owns: impl Fn(&str) -> bool, +) -> Result<(), PersistError> { + let fresh = fresh_document(path, value)?; + let original = read(path)?; + let existing = original.is_some(); + let mut document = formatting_document(path, original.as_deref())?; + merge_document(fresh, owns, existing, &mut document); + write_atomic(path, &document.to_string()).map_err(|source| PersistError::io(path, source)) +} + +fn formatting_document(path: &Path, original: Option<&str>) -> Result { + match original { + None => Ok(DocumentMut::new()), + Some(text) => match text.parse() { + Ok(document) => Ok(document), Err(_) => { let backup = backup_path(path); crate::fs::write_private_atomic(&backup, text.as_bytes()) .map_err(|error| PersistError::io(&backup, error.into_io_error()))?; - DocumentMut::new() + Ok(DocumentMut::new()) } }, - }; + } +} + +fn fresh_document(path: &Path, value: &T) -> Result { + Ok(serialize(path, value)? + .parse() + .expect("serialised TOML is valid TOML")) +} +fn merge_document( + mut fresh: DocumentMut, + owns: impl Fn(&str) -> bool, + existing: bool, + document: &mut DocumentMut, +) { + if existing { + fresh.as_table_mut().retain(|key, _| owns(key)); + for (key, item) in document.iter().filter(|(key, _)| !owns(key)) { + fresh.insert(key, item.clone()); + } + } sync_table(document.as_table_mut(), fresh.as_table()); - write_atomic(path, &document.to_string()).map_err(|source| PersistError::io(path, source)) } fn read(path: &Path) -> Result, PersistError> { diff --git a/crates/core/src/persist/queue.rs b/crates/core/src/persist/queue.rs index 8aaa9b83..a89af75f 100644 --- a/crates/core/src/persist/queue.rs +++ b/crates/core/src/persist/queue.rs @@ -28,7 +28,7 @@ pub struct WriteQueue { limit: usize, draining: bool, closing: bool, - writes: Arc, + writes: InFlight, } /// Marks one disk write as in flight until dropped. @@ -36,7 +36,7 @@ pub struct WriteQueue { pub struct Writing(Arc); /// Counts the writes in flight, independently of the queue. -#[derive(Clone)] +#[derive(Clone, Default)] pub struct InFlight(Arc); impl WriteQueue { @@ -47,7 +47,7 @@ impl WriteQueue { limit, draining: false, closing: false, - writes: Arc::default(), + writes: InFlight::default(), } } @@ -99,12 +99,11 @@ impl WriteQueue { /// Marks a disk write as in flight until the guard drops. Writes made /// outside the queue, such as of related files, may use it too. pub fn begin_write(&self) -> Writing { - self.writes.fetch_add(1, Ordering::AcqRel); - Writing(self.writes.clone()) + self.writes.begin_write() } pub fn in_flight(&self) -> InFlight { - InFlight(self.writes.clone()) + self.writes.clone() } } @@ -115,6 +114,13 @@ impl Drop for Writing { } impl InFlight { + /// Tracks native work independently from a pending queue or its caller. + /// The returned guard belongs to the actual task through physical completion. + pub fn begin_write(&self) -> Writing { + self.0.fetch_add(1, Ordering::AcqRel); + Writing(self.0.clone()) + } + pub fn count(&self) -> usize { self.0.load(Ordering::Acquire) } diff --git a/crates/core/src/persist/revisioned.rs b/crates/core/src/persist/revisioned.rs new file mode 100644 index 00000000..5b446938 --- /dev/null +++ b/crates/core/src/persist/revisioned.rs @@ -0,0 +1,201 @@ +//! Cooperative document publication and optimistic conflict detection. +//! Lock files are stable siblings of the resolved configuration target and are never unlinked. +use super::{PersistError, read}; +use serde::{Serialize, de::DeserializeOwned}; +use sha2::{Digest as _, Sha256}; +use std::{ + collections::BTreeMap, + fs::{self, File}, + io, + path::{Path, PathBuf}, + sync::{Arc, Mutex, PoisonError}, +}; +use toml_edit::DocumentMut; + +/// One document owner and the revision it last read or successfully published. +/// Clones share that revision; independently constructed owners detect stale edits. +/// +/// Native callers dispatch load/save on their disk worker. A non-cooperating editor can +/// still race the final rename; the lock coordinates Nocterm writers, not arbitrary programs. +#[derive(Clone, Debug)] +pub struct RevisionedFile { + path: PathBuf, + observed: Arc>>, +} + +#[derive(Debug, PartialEq, Eq)] +struct Revision { + target: PathBuf, + content: Content, +} +#[derive(Debug, PartialEq, Eq)] +enum Content { + Missing, + Tables(BTreeMap), + Invalid([u8; 32]), +} + +impl RevisionedFile { + pub fn new(path: impl Into) -> Self { + Self { + path: path.into(), + observed: Arc::default(), + } + } + pub fn path(&self) -> &Path { + &self.path + } + + /// Reading observes the same bytes used to decode the returned document. + pub fn load(&self) -> Result, PersistError> { + let mut observed = self.observed.lock().unwrap_or_else(PoisonError::into_inner); + let target = resolve(&self.path).map_err(|error| PersistError::io(&self.path, error))?; + let text = read(&target)?; + let parsed = text.as_deref().map(str::parse::); + *observed = Some(revision(target, text.as_deref(), parsed.as_ref())); + text.map(|text| { + toml::from_str(&text).map_err(|source| PersistError::Parse { + path: self.path.clone(), + source: Box::new(source), + }) + }) + .transpose() + } + + pub fn save_preserving(&self, value: &T) -> Result<(), PersistError> { + self.save_preserving_owned(value, |_| true) + } + + /// Unowned top-level tables may change independently and keep their current bytes. + /// Conflicts in owned tables leave the document and observed revision untouched. + pub fn save_preserving_owned( + &self, + value: &T, + owns: impl Fn(&str) -> bool, + ) -> Result<(), PersistError> { + let fresh = super::fresh_document(&self.path, value)?; + let mut observed = self.observed.lock().unwrap_or_else(PoisonError::into_inner); + let target = resolve(&self.path).map_err(|error| PersistError::io(&self.path, error))?; + let parent = target.parent().unwrap_or(Path::new(".")); + fs::create_dir_all(parent).map_err(|error| PersistError::io(parent, error))?; + let _lock = publication_lock(&target).map_err(|error| PersistError::io(&target, error))?; + let text = read(&target)?; + let parsed = text.as_deref().map(str::parse::); + let current = revision(target.clone(), text.as_deref(), parsed.as_ref()); + if observed + .as_ref() + .is_some_and(|prior| conflicts(prior, ¤t, &owns)) + { + return Err(PersistError::Conflict { + path: self.path.clone(), + }); + } + let mut document = match parsed { + Some(Ok(document)) => document, + // Preserve the existing malformed-file backup contract after conflict admission. + _ => super::formatting_document(&target, text.as_deref())?, + }; + super::merge_document(fresh, &owns, text.is_some(), &mut document); + let published = document.to_string(); + super::write_atomic(&target, &published) + .map_err(|error| PersistError::io(&target, error))?; + *observed = Some(Revision { + target, + content: tables(&document), + }); + Ok(()) + } +} + +fn resolve(path: &Path) -> io::Result { + match fs::canonicalize(path) { + Ok(path) => Ok(path), + Err(error) if error.kind() == io::ErrorKind::NotFound => { + let absolute = std::path::absolute(path)?; + let parent = absolute.parent().unwrap_or(Path::new(".")); + Ok(fs::canonicalize(parent).map_or(absolute.clone(), |parent| { + parent.join(absolute.file_name().unwrap_or_default()) + })) + } + Err(error) => Err(error), + } +} + +fn publication_lock(target: &Path) -> io::Result { + let mut name = target.file_name().unwrap_or_default().to_os_string(); + name.push(".lock"); + let path = target.with_file_name(name); + let file = match crate::fs::create_private(&path) { + Ok(file) => file, + Err(error) if error.kind() == io::ErrorKind::AlreadyExists => open_existing_lock(&path)?, + Err(error) => return Err(error.into_io_error()), + }; + file.try_lock().map_err(io::Error::other)?; + Ok(file) +} + +#[cfg(unix)] +fn open_existing_lock(path: &Path) -> io::Result { + crate::fs::open_private_regular(path).map_err(io::Error::other) +} +#[cfg(not(unix))] +fn open_existing_lock(path: &Path) -> io::Result { + let mut options = fs::OpenOptions::new(); + options.read(true).write(true); + #[cfg(windows)] + { + use std::os::windows::fs::OpenOptionsExt as _; + // Open the reparse point itself; it must not select another file's lock. + options.custom_flags(0x0020_0000); + } + if fs::symlink_metadata(path)?.file_type().is_symlink() { + return Err(io::Error::from(io::ErrorKind::InvalidInput)); + } + let file = options.open(path)?; + if !file.metadata()?.is_file() { + return Err(io::Error::from(io::ErrorKind::InvalidInput)); + } + Ok(file) +} + +fn revision( + target: PathBuf, + text: Option<&str>, + parsed: Option<&Result>, +) -> Revision { + let content = match (text, parsed) { + (_, Some(Ok(document))) => tables(document), + (Some(text), _) => Content::Invalid(digest(text)), + _ => Content::Missing, + }; + Revision { target, content } +} +fn digest(text: &str) -> [u8; 32] { + Sha256::digest(text.as_bytes()).into() +} +fn tables(document: &DocumentMut) -> Content { + Content::Tables( + document + .iter() + .map(|(key, item)| (key.to_owned(), digest(&item.to_string()))) + .collect(), + ) +} +fn conflicts(prior: &Revision, current: &Revision, owns: &impl Fn(&str) -> bool) -> bool { + if prior.target != current.target { + return true; + } + match (&prior.content, ¤t.content) { + (Content::Tables(prior), Content::Tables(current)) => prior + .keys() + .chain(current.keys()) + .any(|key| owns(key) && prior.get(key) != current.get(key)), + // The empty document and a missing file are equivalent for owned keys. + (Content::Missing, Content::Tables(current)) => current.keys().any(|key| owns(key)), + (Content::Tables(prior), Content::Missing) => prior.keys().any(|key| owns(key)), + (prior, current) => prior != current, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/core/src/persist/revisioned/tests.rs b/crates/core/src/persist/revisioned/tests.rs new file mode 100644 index 00000000..6caa64e4 --- /dev/null +++ b/crates/core/src/persist/revisioned/tests.rs @@ -0,0 +1,110 @@ +use super::*; + +fn values(text: &str) -> toml::Table { + toml::from_str(text).unwrap() +} + +#[test] +fn independent_owners_cannot_replace_a_newer_owned_document() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + fs::write(&path, "[terminal]\nsize = 14\n").unwrap(); + let first = RevisionedFile::new(&path); + let second = RevisionedFile::new(&path); + let _: toml::Table = first.load().unwrap().unwrap(); + let _: toml::Table = second.load().unwrap().unwrap(); + first + .save_preserving(&values("[terminal]\nsize = 15\n")) + .unwrap(); + assert!(matches!( + second.save_preserving(&values("[terminal]\nsize = 16\n")), + Err(PersistError::Conflict { .. }) + )); + assert!(fs::read_to_string(&path).unwrap().contains("size = 15")); + let _: toml::Table = second.load().unwrap().unwrap(); + second + .save_preserving(&values("[terminal]\nsize = 16\n")) + .unwrap(); +} + +#[test] +fn unowned_external_tables_survive_while_owned_edits_conflict() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + fs::write(&path, "[future]\nflag = true\n").unwrap(); + let file = RevisionedFile::new(&path); + let _: toml::Table = file.load().unwrap().unwrap(); + fs::write(&path, "[future]\n# retain this comment\nflag = false\n").unwrap(); + file.save_preserving_owned(&values("[terminal]\nsize = 15\n"), |key| key == "terminal") + .unwrap(); + assert!( + fs::read_to_string(&path) + .unwrap() + .contains("# retain this comment\nflag = false") + ); + fs::write(&path, "[terminal]\nsize = 18\n").unwrap(); + assert!(matches!( + file.save_preserving_owned(&values("[terminal]\nsize = 16\n"), |key| key == "terminal"), + Err(PersistError::Conflict { .. }) + )); +} + +#[test] +fn clones_share_a_publication_revision_and_missing_file_creation_is_observed() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("new/settings.toml"); + let file = RevisionedFile::new(&path); + assert!(file.load::().unwrap().is_none()); + let clone = file.clone(); + file.save_preserving(&values("a = 1")).unwrap(); + clone.save_preserving(&values("a = 2")).unwrap(); + let other = RevisionedFile::new(&path); + other.load::().unwrap(); + clone.save_preserving(&values("a = 3")).unwrap(); + assert!(matches!( + other.save_preserving(&values("a = 4")), + Err(PersistError::Conflict { .. }) + )); +} + +#[cfg(unix)] +#[test] +fn symlink_aliases_use_the_same_lock_and_lock_symlinks_are_refused() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + let alias = dir.path().join("alias.toml"); + fs::write(&path, "a = 1").unwrap(); + std::os::unix::fs::symlink(&path, &alias).unwrap(); + let file = RevisionedFile::new(&alias); + file.load::().unwrap(); + let lock = publication_lock(&path).unwrap(); + assert!(file.save_preserving(&values("a = 2")).is_err()); + drop(lock); + file.save_preserving(&values("a = 2")).unwrap(); + assert!(fs::symlink_metadata(&alias).unwrap().is_symlink()); + fs::remove_file(path.with_file_name("settings.toml.lock")).unwrap(); + let victim = dir.path().join("victim"); + fs::write(&victim, "untouched").unwrap(); + std::os::unix::fs::symlink(&victim, path.with_file_name("settings.toml.lock")).unwrap(); + assert!(file.save_preserving(&values("a = 3")).is_err()); + assert_eq!(fs::read_to_string(victim).unwrap(), "untouched"); +} + +#[test] +fn serialization_refusal_does_not_touch_an_unreadable_document_or_create_a_lock() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + fs::write(&path, "bad = =").unwrap(); + let file = RevisionedFile::new(&path); + assert!(matches!( + file.save_preserving(&42u32), + Err(PersistError::Serialize { .. }) + )); + assert_eq!(fs::read_to_string(&path).unwrap(), "bad = ="); + assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1); + assert!(matches!( + super::super::save_preserving(&path, &42u32), + Err(PersistError::Serialize { .. }) + )); + assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1); +} diff --git a/crates/core/src/persist/shutdown.rs b/crates/core/src/persist/shutdown.rs index 26b7ae2a..dc88b157 100644 --- a/crates/core/src/persist/shutdown.rs +++ b/crates/core/src/persist/shutdown.rs @@ -14,7 +14,7 @@ pub struct WriteGate(Arc); #[derive(Default)] struct Gate { closing: AtomicBool, - disk: futures::lock::Mutex<()>, + disk: Arc>, } impl WriteGate { @@ -33,6 +33,18 @@ impl WriteGate { (!self.is_frozen()).then_some(guard) } + /// An off-thread publication retains this owned guard until its actual + /// native operation ends, even when the original waiter is cancelled. + pub async fn owned_normal(&self) -> Option> { + let guard = self.0.disk.clone().lock_owned().await; + (!self.is_frozen()).then_some(guard) + } + + /// Final publication uses the same owned gate after normal admission closes. + pub async fn owned_final_write(&self) -> futures::lock::OwnedMutexGuard<()> { + self.0.disk.clone().lock_owned().await + } + /// The final writer runs after active disk work and bypasses frozen admission. pub async fn final_write(&self) -> futures::lock::MutexGuard<'_, ()> { self.0.disk.lock().await @@ -59,6 +71,31 @@ mod tests { use super::*; use futures::{FutureExt as _, executor::block_on}; + #[test] + fn an_owned_publication_outlives_its_caller_and_blocks_final_write() { + let gate = WriteGate::default(); + let writer = block_on(gate.owned_normal()).unwrap(); + let observer = gate.clone(); + let tracked = crate::persist::InFlight::default(); + let writing = tracked.begin_write(); + let (release, wait) = std::sync::mpsc::channel(); + let work = std::thread::spawn(move || { + let _writer = writer; + let _writing = writing; + wait.recv().unwrap(); + }); + drop(gate); + observer.freeze(); + assert_eq!(tracked.count(), 1); + assert!(observer.owned_final_write().now_or_never().is_none()); + release.send(()).unwrap(); + work.join().unwrap(); + assert_eq!(tracked.count(), 0); + assert!(block_on(observer.owned_normal()).is_none()); + assert!(observer.owned_final_write().now_or_never().is_some()); + assert!(observer.final_write().now_or_never().is_some()); + } + #[test] fn freeze_is_nonblocking_and_final_waits_for_active_write() { let gate = WriteGate::default(); diff --git a/crates/design/tokens/default.toml b/crates/design/tokens/default.toml index 30eb9c13..606c0e02 100644 --- a/crates/design/tokens/default.toml +++ b/crates/design/tokens/default.toml @@ -60,7 +60,7 @@ picker_width = 22.0 # the "new tab" picker dialog_width = 30.0 # the connection editor local_terminal_height = 15.0 # initial height of the bottom local shell status_bar_center_max = 40.0 # the centre of the status bar grows no wider than this -settings_width = 45.0 # the settings form and its footer +settings_width = 52.0 # the settings form and its footer connection_editor_width = 45.0 # connection editor, including section navigation connection_editor_height = 32.0 # maximum connection editor form height connection_nav_width = 9.0 # connection editor section navigation diff --git a/crates/files/src/activity.rs b/crates/files/src/activity.rs index bfb8767d..e568769b 100644 --- a/crates/files/src/activity.rs +++ b/crates/files/src/activity.rs @@ -45,12 +45,14 @@ impl Activity { } } -/// Whether a finished upload changed the remote folder `shown` on `target`: -/// it was copied into that folder or into one of its direct subfolders. +/// Whether a finished upload or relay changed the remote folder `shown` on +/// `target`: it was copied into that folder or into one of its direct subfolders. pub(crate) fn changes_remote(job: &Progress, host: &HostAddress, shown: &str) -> bool { let trim = |value: &str| value.trim_end_matches('/').to_owned(); - job.direction == TransferDirection::Upload - && job.host == *host + matches!( + job.direction, + TransferDirection::Upload | TransferDirection::Relay + ) && job.host == *host && (trim(&job.destination) == trim(shown) || trim(path::parent(&job.destination)) == trim(shown)) } @@ -75,6 +77,7 @@ pub(crate) fn strip<'a>( let verb = match job.direction { TransferDirection::Upload => "Uploading", TransferDirection::Download => "Downloading", + TransferDirection::Relay => "Copying", }; let fraction = if job.total_bytes > 0 { job.sent_bytes as f32 / job.total_bytes as f32 @@ -94,6 +97,7 @@ pub(crate) fn strip<'a>( Icon::new(match job.direction { TransferDirection::Upload => IconName::ArrowUp, TransferDirection::Download => IconName::ArrowDown, + TransferDirection::Relay => IconName::ArrowRight, }) .xsmall(), ) @@ -207,5 +211,12 @@ mod tests { assert!(changes_local(&download, Path::new("/tmp/in"))); assert!(changes_local(&download, Path::new("/tmp"))); assert!(!changes_local(&upload, Path::new("/srv/app"))); + let relay = job( + TransferDirection::Relay, + "/srv/app", + TransferState::Completed, + ); + assert!(changes_remote(&relay, &target, "/srv/app")); + assert!(!changes_local(&relay, Path::new("/srv/app"))); } } diff --git a/crates/files/src/host_capabilities.rs b/crates/files/src/host_capabilities.rs index 17a6301e..717f574e 100644 --- a/crates/files/src/host_capabilities.rs +++ b/crates/files/src/host_capabilities.rs @@ -5,7 +5,7 @@ //! poll, and starts, retries or cancels nothing. use std::sync::Arc; -use gpui_kit::App; +use gpui_kit::{App, Entity}; use nocterm_plugin_api::{ HostError, capability::transfers::{Counts, CountsNow}, @@ -18,24 +18,35 @@ use crate::Installed; /// The counts of the transfer queue shared by every window. pub struct TransferCounts { - service: Arc, + source: CountsSource, +} +enum CountsSource { + Projection(Entity), + Direct(Arc), } impl TransferCounts { pub fn new(installed: &Installed) -> Self { - Self::over(installed.transfers().clone()) + Self { + source: CountsSource::Projection(installed.projection().clone()), + } } /// The counts of `service`, for a composition that holds the service without [`Installed`]. pub fn over(service: Arc) -> Self { - Self { service } + Self { + source: CountsSource::Direct(service), + } } } impl Observe for TransferCounts { /// Transfers not yet finished, and transfers in which a file failed. - fn sample(&self, _: &PluginId, _: &App) -> Result { - let snapshot = self.service.snapshot(); + fn sample(&self, _: &PluginId, cx: &App) -> Result { + let snapshot: Arc<[nocterm_transfers::Progress]> = match &self.source { + CountsSource::Projection(projection) => projection.read(cx).entries().clone(), + CountsSource::Direct(service) => service.snapshot().into(), + }; let count = |matching: &dyn Fn(&nocterm_transfers::Progress) -> bool| { u16::try_from(snapshot.iter().filter(|job| matching(job)).count()).unwrap_or(u16::MAX) }; diff --git a/crates/files/src/lib.rs b/crates/files/src/lib.rs index b82a8d6e..de1c010d 100644 --- a/crates/files/src/lib.rs +++ b/crates/files/src/lib.rs @@ -1,5 +1,5 @@ //! Explorer navigation, file dialogs and transfer presentation. -//! Browser generations, scan results and completion cursors remain private transition owners. +//! Browser generations, shared row selections, scan results and completion cursors remain private transition owners. //! //! Owns: independent pane views, acknowledged browser navigation, selection and pinned dialogs. //! Shared path editors and navigation controls belong to nocterm-ui. @@ -22,11 +22,13 @@ mod local_pane; mod open; mod operations; pub mod plugin; +mod projection; use nocterm_ui::directory as path_input; mod registration; mod remote; mod remote_pane; mod row; +mod selection; mod settings; mod settings_page; mod statistics; @@ -60,7 +62,8 @@ use nocterm_workspace::{ }; use operations::FileTarget; use remote::{Browser, listing}; -use std::{collections::BTreeSet, path::PathBuf, rc::Rc, sync::Arc, time::Duration}; +use selection::RowSelection; +use std::{path::PathBuf, rc::Rc, sync::Arc, time::Duration}; #[cfg(feature = "user-plugins")] pub use host_capabilities::TransferCounts; @@ -259,6 +262,13 @@ impl FilesPanel { this.local.update(cx, |_, cx| cx.notify()); } }); + let transfer_events = cx.observe(installed.projection(), |this, _, cx| { + this.transfers_changed(cx) + }); + let weak = cx.entity().downgrade(); + cx.defer(move |cx| { + let _ = weak.update(cx, |this, cx| this.transfers_changed(cx)); + }); let tick = cx.spawn_in(window, async move |this, cx| { loop { cx.background_executor() @@ -281,7 +291,12 @@ impl FilesPanel { collisions: CollisionPolicy::Skip, window: window.window_handle(), _observers: PanelObservers { - _subscriptions: vec![local_events, remote_events, workspace_events], + _subscriptions: vec![ + local_events, + remote_events, + workspace_events, + transfer_events, + ], _tick: tick, }, installed, @@ -381,7 +396,7 @@ impl FilesPanel { }); } fn synchronize(&mut self, finished: &[nocterm_transfers::Progress], cx: &mut Context) { - let jobs = self.installed.transfers().snapshot(); + let jobs = self.installed.projection().read(cx).entries().to_vec(); self.local.update(cx, |local, cx| { local.transfers( jobs.iter() @@ -400,9 +415,14 @@ impl FilesPanel { remote.transfers(jobs, finished, ready, self.collisions, cx) }); } + fn transfers_changed(&mut self, cx: &mut Context) { + let snapshot = self.installed.projection().read(cx).entries().to_vec(); + let (changed, finished) = self.activity.update(snapshot); + if changed { + self.synchronize(&finished, cx); + } + } fn poll(&mut self, window: &mut Window, cx: &mut Context) { - let (_, finished) = self.activity.update(self.installed.transfers().snapshot()); - self.synchronize(&finished, cx); self.local .update(cx, |local, cx| local.poll_statistics(window, cx)); self.remote diff --git a/crates/files/src/local_pane.rs b/crates/files/src/local_pane.rs index 9bddcb43..124b803d 100644 --- a/crates/files/src/local_pane.rs +++ b/crates/files/src/local_pane.rs @@ -20,8 +20,7 @@ struct LocalBrowser { path: Option, requested: Option, entries: Vec, - selected: BTreeSet, - anchor: Option, + selection: RowSelection, loading: bool, error: Option, counter: statistics::Counter, @@ -103,7 +102,7 @@ impl LocalPane { .iter() .map(|entry| entry.name.clone()) .collect(), - selected: self.browser.selected.iter().copied().collect(), + selected: self.browser.selection.indices().collect(), loading: self.browser.loading, error: self.browser.error.clone(), statistics: statistics::summary(self.browser.counter.shown()), @@ -305,8 +304,7 @@ impl LocalPane { }); self.browser.path = Some(directory.to_owned()); self.browser.entries = entries; - self.browser.selected.clear(); - self.browser.anchor = None; + self.browser.selection.clear(); } Err(error) => { self.path.update(cx, |input, cx| { @@ -329,21 +327,11 @@ impl LocalPane { event: &gpui_kit::MouseDownEvent, cx: &mut Context, ) { - if event.modifiers.shift { - let anchor = self.browser.anchor.unwrap_or(ix); - self.browser - .selected - .extend(anchor.min(ix)..=anchor.max(ix)); - } else if event.modifiers.control || event.modifiers.platform { - if !self.browser.selected.insert(ix) { - self.browser.selected.remove(&ix); - } - self.browser.anchor = Some(ix); - } else { - self.browser.selected.clear(); - self.browser.selected.insert(ix); - self.browser.anchor = Some(ix); - } + self.browser.selection.click( + ix, + event.modifiers.shift, + event.modifiers.control || event.modifiers.platform, + ); if event.click_count == 2 && let Some(entry) = self .browser @@ -373,27 +361,16 @@ impl LocalPane { } pub(super) fn paths(&self, index: usize) -> Vec { - if self.browser.selected.contains(&index) { - self.browser - .selected - .iter() - .filter_map(|ix| self.browser.entries.get(*ix)) - .map(|e| e.path.clone()) - .collect() - } else { - self.browser - .entries - .get(index) - .map(|e| vec![e.path.clone()]) - .unwrap_or_default() - } + self.browser + .selection + .for_row(index) + .filter_map(|ix| self.browser.entries.get(ix)) + .map(|entry| entry.path.clone()) + .collect() } fn select_for_menu(&mut self, ix: usize, cx: &mut Context) { - if !self.browser.selected.contains(&ix) { - self.browser.selected.clear(); - self.browser.selected.insert(ix); - self.browser.anchor = Some(ix); + if self.browser.selection.context_click(ix) { cx.notify(); } } @@ -401,7 +378,7 @@ impl LocalPane { pub(super) fn render_local_row(&self, ix: usize, cx: &mut Context) -> AnyElement { let entry = &self.browser.entries[ix]; let paths = self.paths(ix); - let visual = ExplorerRow::local(entry, self.browser.selected.contains(&ix), cx); + let visual = ExplorerRow::local(entry, self.browser.selection.contains(ix), cx); let source = nocterm_ui::DragSource::default(); let destination = entry.path.clone(); let directory = entry.kind == EntryKind::Directory && !entry.symlink; diff --git a/crates/files/src/plugin.rs b/crates/files/src/plugin.rs index d17b2904..09286060 100644 --- a/crates/files/src/plugin.rs +++ b/crates/files/src/plugin.rs @@ -24,6 +24,7 @@ pub const EXPLORER: SystemContribution = SystemContribution { chrome: PanelChrome::Titled, }), preview: None, + edge: None, pinned: false, }; @@ -41,6 +42,7 @@ pub const TRANSFERS: SystemContribution = SystemContribution { }, panel: None, preview: None, + edge: None, pinned: false, }; diff --git a/crates/files/src/plugin_tests.rs b/crates/files/src/plugin_tests.rs index 698cb13a..71a0008d 100644 --- a/crates/files/src/plugin_tests.rs +++ b/crates/files/src/plugin_tests.rs @@ -51,3 +51,35 @@ fn the_toggle_action_hides_the_shown_explorer_and_shows_it_again(cx: &mut TestAp dispatch(cx); assert!(open(cx)); } + +#[gpui_kit::test] +fn explorer_status_and_queue_share_one_periodic_transfer_snapshot(cx: &mut TestAppContext) { + let directory = tempfile::tempdir().unwrap(); + let (deps, _, recording) = transfer_fixture_deps_at(directory.path()); + let (window, workspace, _, _panel, installed) = + panel_fixture_with_installation(cx, Arc::new(FakeFs), deps); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + crate::register(&installed, workspace, window, cx).unwrap() + }); + let queue = cx.new(|cx| { + crate::transfers::TransfersView::new( + installed.transfers().clone(), + installed.projection().clone(), + workspace.downgrade(), + cx, + ) + }); + workspace.update(cx, |workspace, cx| workspace.add_item(queue, window, cx)); + }) + .unwrap(); + cx.run_until_parked(); + let before = recording.snapshot_count(); + cx.executor().advance_clock(Duration::from_millis(150)); + cx.run_until_parked(); + assert_eq!( + recording.snapshot_count(), + before + 1, + "legacy polling is shared by all views" + ); +} diff --git a/crates/files/src/projection.rs b/crates/files/src/projection.rs new file mode 100644 index 00000000..ce86a82e --- /dev/null +++ b/crates/files/src/projection.rs @@ -0,0 +1,62 @@ +//! One installation-scoped, coalesced projection of the shared transfer service. +//! Native snapshot locking/copying is background work; views observe immutable entries. +use gpui_kit::{Context, Task}; +use nocterm_transfers::{Progress, TransferService}; +use std::{sync::Arc, time::Duration}; + +pub(crate) struct TransferProjection { + entries: Arc<[Progress]>, + _poll: Task<()>, +} +impl TransferProjection { + pub(crate) fn new(service: Arc, cx: &mut Context) -> Self { + let poll = cx.spawn(async move |this, cx| { + let mut revision = None; + let mut previous: Arc<[Progress]> = Arc::from([]); + loop { + let poll_service = service.clone(); + let before = previous.clone(); + let result = cx + .background_executor() + .spawn(async move { + let next = poll_service.revision(); + if next.is_some() && next != Some(u64::MAX) && next == revision { + return (revision, None); + } + let snapshot: Arc<[Progress]> = poll_service.snapshot().into(); + let changed = (snapshot != before).then_some(snapshot); + (next, changed) + }) + .await; + revision = result.0; + if let Some(snapshot) = result.1 { + previous = snapshot.clone(); + if this + .update(cx, |this, cx| { + this.entries = snapshot; + cx.notify(); + }) + .is_err() + { + return; + } + } else if this.upgrade().is_none() { + return; + } + if let Some(wait) = revision.and_then(|revision| service.changed_since(revision)) { + wait.await; + } + cx.background_executor() + .timer(Duration::from_millis(150)) + .await; + } + }); + Self { + entries: Arc::from([]), + _poll: poll, + } + } + pub(crate) fn entries(&self) -> &Arc<[Progress]> { + &self.entries + } +} diff --git a/crates/files/src/registration.rs b/crates/files/src/registration.rs index 7dd37e4c..68dcbd6a 100644 --- a/crates/files/src/registration.rs +++ b/crates/files/src/registration.rs @@ -1,6 +1,6 @@ //! The Explorer's place in the workspace: its sidebar panel, the transfer //! status and the commands that show them. -use gpui_kit::{App, AppContext as _, Context, Window}; +use gpui_kit::{App, AppContext as _, Context, Entity, Window}; use nocterm_workspace::{PanelRegistration, RegistrationError, WidgetRegistration, Workspace}; use crate::{Deps, FilesPanel, plugin, transfers}; @@ -23,8 +23,12 @@ gpui_kit::actions!( pub struct Installed { deps: Deps, settings: nocterm_store::SettingsHandle, + projection: Entity, } impl Installed { + pub(crate) fn projection(&self) -> &Entity { + &self.projection + } pub(crate) fn directories( &self, ) -> &std::sync::Arc { @@ -61,7 +65,10 @@ pub fn install(deps: Deps, ready: &nocterm_ui::UiReady, cx: &mut App) -> Install ready .settings() .register::(cx); + let projection = + cx.new(|cx| crate::projection::TransferProjection::new(deps.transfers.clone(), cx)); Installed { + projection, deps, settings: ready.settings().clone(), } @@ -79,12 +86,15 @@ pub fn register( workspace.toggle_panel(&plugin::EXPLORER.id, window, cx); }); let service = installed.deps.transfers.clone(); + let projection = installed.projection.clone(); workspace.register_action(move |workspace, _: &ShowTransfers, window, cx| { if let Some(view) = workspace.find_item::(cx) { workspace.activate_item_by_id(view.entity_id(), window, cx); } else { let handle = cx.entity().downgrade(); - let view = cx.new(|cx| transfers::TransfersView::new(service.clone(), handle, cx)); + let view = cx.new(|cx| { + transfers::TransfersView::new(service.clone(), projection.clone(), handle, cx) + }); workspace.add_item(view, window, cx); } }); @@ -98,7 +108,7 @@ pub fn register( window, cx, ); - let transfers = installed.deps.transfers.clone(); + let transfers = installed.projection.clone(); let status = workspace.register_widget( WidgetRegistration::new(&plugin::TRANSFERS, move |_, window, cx| { let transfers = transfers.clone(); diff --git a/crates/files/src/remote_pane.rs b/crates/files/src/remote_pane.rs index cb0ec05c..9ef14904 100644 --- a/crates/files/src/remote_pane.rs +++ b/crates/files/src/remote_pane.rs @@ -27,8 +27,7 @@ pub(super) struct RemotePane { source: Option, browser: Browser, requested: Option, - selected: BTreeSet, - anchor: Option, + selection: RowSelection, path: Entity, _subscription: Subscription, counter: statistics::Counter, @@ -55,8 +54,7 @@ impl RemotePane { source: None, browser: Browser::default(), requested: None, - selected: BTreeSet::new(), - anchor: None, + selection: RowSelection::default(), path, _subscription: subscription, counter: statistics::Counter::default(), @@ -82,7 +80,7 @@ impl RemotePane { .iter() .map(|entry| entry.name.clone()) .collect(), - selected: self.selected.iter().copied().collect(), + selected: self.selection.indices().collect(), loading: self.browser.loading(), error: self.browser.error().map(ToString::to_string), statistics: statistics::summary(self.counter.shown()), @@ -122,7 +120,11 @@ impl RemotePane { jobs.into_iter() .filter(|job| { Some(&job.host) == session.address().as_ref() - && job.direction == nocterm_transfers::TransferDirection::Upload + && matches!( + job.direction, + nocterm_transfers::TransferDirection::Upload + | nocterm_transfers::TransferDirection::Relay + ) && !job.state.finished() }) .collect() @@ -188,8 +190,7 @@ impl RemotePane { .update(cx, |input, cx| input.replace_reader(reader, cx)); self.requested = None; self.browser.clear(); - self.selected.clear(); - self.anchor = None; + self.selection.clear(); self.task = None; self.counter.set(Default::default()); if self.filesystem().is_some() { @@ -226,8 +227,7 @@ impl RemotePane { self.path .update(cx, |input, cx| input.begin_navigation_with_mode(mode, cx)); let generation = self.browser.begin(); - self.selected.clear(); - self.anchor = None; + self.selection.clear(); self.requested = directory.clone(); cx.notify(); let window = self.presentation.host.window; @@ -344,19 +344,11 @@ impl RemotePane { event: &gpui_kit::MouseDownEvent, cx: &mut Context, ) { - if event.modifiers.shift { - let anchor = self.anchor.unwrap_or(ix); - self.selected.extend(anchor.min(ix)..=anchor.max(ix)); - } else if event.modifiers.control || event.modifiers.platform { - if !self.selected.insert(ix) { - self.selected.remove(&ix); - } - self.anchor = Some(ix); - } else { - self.selected.clear(); - self.selected.insert(ix); - self.anchor = Some(ix); - } + self.selection.click( + ix, + event.modifiers.shift, + event.modifiers.control || event.modifiers.platform, + ); if event.click_count == 2 && let Some(entry) = self .browser @@ -390,32 +382,21 @@ impl RemotePane { } fn select_for_menu(&mut self, ix: usize, cx: &mut Context) { - if !self.selected.contains(&ix) { - self.selected.clear(); - self.selected.insert(ix); - self.anchor = Some(ix); + if self.selection.context_click(ix) { cx.notify(); } } - fn selection_for_row(&self, ix: usize) -> Vec { - if self.selected.contains(&ix) { - self.selected.iter().copied().collect() - } else { - vec![ix] - } - } - pub(super) fn render_remote_row(&self, ix: usize, cx: &mut Context) -> AnyElement { let entry = &self.browser.entries()[ix]; let directory = entry.kind == EntryKind::Directory; - let selected = self.selection_for_row(ix); + let selected = self.selection.for_row(ix).collect::>(); let download = self.paths(selected.iter().copied()); let visual = ExplorerRow { name: entry.name.clone(), directory: entry.kind == EntryKind::Directory, symlink: entry.is_symlink, - selected: self.selected.contains(&ix), + selected: self.selection.contains(ix), font_size: cx.design().typography.explorer_size.unwrap_or(12.0), }; let source = nocterm_ui::DragSource::default(); @@ -546,10 +527,12 @@ impl RemotePane { .icon(IconName::ArrowDown) .tooltip("Download selected items to the local Explorer directory") .disabled( - !enabled || self.selected.is_empty() || !self.presentation.download_enabled, + !enabled + || self.selection.is_empty() + || !self.presentation.download_enabled, ) .on_click(cx.listener(|this, _, _, cx| { - if let Some(files) = this.paths(this.selected.iter().copied()) { + if let Some(files) = this.paths(this.selection.indices()) { cx.emit(PaneIntent::Download { files, destination: None, diff --git a/crates/files/src/selection.rs b/crates/files/src/selection.rs new file mode 100644 index 00000000..2e4b7d1d --- /dev/null +++ b/crates/files/src/selection.rs @@ -0,0 +1,56 @@ +//! Row membership and range-anchor transitions shared by both Explorer panes. +use std::collections::BTreeSet; + +#[derive(Default)] +pub(super) struct RowSelection { + selected: BTreeSet, + anchor: Option, +} +impl RowSelection { + pub(super) fn clear(&mut self) { + self.selected.clear(); + self.anchor = None; + } + pub(super) fn click(&mut self, index: usize, shift: bool, toggle: bool) { + if shift { + let anchor = self.anchor.unwrap_or(index); + self.selected.extend(anchor.min(index)..=anchor.max(index)); + } else if toggle { + if !self.selected.insert(index) { + self.selected.remove(&index); + } + self.anchor = Some(index); + } else { + self.replace(index); + } + } + /// A context click keeps a multi-selection when its row is already a member. + pub(super) fn context_click(&mut self, index: usize) -> bool { + if self.contains(index) { + false + } else { + self.replace(index); + true + } + } + fn replace(&mut self, index: usize) { + self.selected.clear(); + self.selected.insert(index); + self.anchor = Some(index); + } + pub(super) fn contains(&self, index: usize) -> bool { + self.selected.contains(&index) + } + pub(super) fn is_empty(&self) -> bool { + self.selected.is_empty() + } + pub(super) fn indices(&self) -> impl Iterator + '_ { + self.selected.iter().copied() + } + pub(super) fn for_row(&self, index: usize) -> impl Iterator + '_ { + let selected = self.contains(index); + self.indices() + .take(if selected { self.selected.len() } else { 0 }) + .chain((!selected).then_some(index)) + } +} diff --git a/crates/files/src/selection_ui_tests.rs b/crates/files/src/selection_ui_tests.rs new file mode 100644 index 00000000..4f3ee670 --- /dev/null +++ b/crates/files/src/selection_ui_tests.rs @@ -0,0 +1,93 @@ +//! Both panes expose the same selection gestures while keeping separate paths. +use super::*; +use gpui_kit::{Modifiers, test::TestWindowExt as _}; + +#[gpui_kit::test] +fn both_panes_preserve_context_selection_and_reset_the_range_anchor_on_navigation( + cx: &mut TestAppContext, +) { + let directory = tempfile::tempdir().unwrap(); + for name in ["a.txt", "b.txt", "c.txt"] { + std::fs::write(directory.path().join(name), b"local").unwrap(); + } + let (handle, workspace, _, panel) = + panel_fixture_at(cx, transfer_remote(false), directory.path()); + transfer_ui::show_local(cx, handle, &workspace, &panel, directory.path()); + for local in [true, false] { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let group = if local { "local-entry" } else { "remote-entry" }; + let first = window.find((group, 0usize)).bounds().center(); + let third = window.find((group, 2usize)).bounds().center(); + pointer_click(window, first, Default::default(), cx); + pointer_click( + window, + third, + Modifiers { + shift: true, + ..Default::default() + }, + cx, + ); + let middle = window.find((group, 1usize)).bounds().center(); + pointer_click( + window, + middle, + Modifiers { + platform: true, + ..Default::default() + }, + cx, + ); + }) + .unwrap(); + assert_selection(&panel, local, &[0, 2], cx); + operations_ui::context_menu(cx, handle, local, 2); + assert_selection(&panel, local, &[0, 2], cx); + cx.update_window(handle, |_, window, cx| window.press("escape", cx)) + .unwrap(); + operations_ui::context_menu(cx, handle, local, 1); + assert_selection(&panel, local, &[1], cx); + cx.update_window(handle, |_, window, cx| window.press("escape", cx)) + .unwrap(); + panel.update(cx, |panel, cx| { + if local { + panel.navigate_local(directory.path().to_owned(), cx); + } else { + panel.navigate_remote(Some("/home/test".into()), cx); + } + }); + cx.run_until_parked(); + assert_selection(&panel, local, &[], cx); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let group = if local { "local-entry" } else { "remote-entry" }; + let third = window.find((group, 2usize)).bounds().center(); + pointer_click( + window, + third, + Modifiers { + shift: true, + ..Default::default() + }, + cx, + ); + }) + .unwrap(); + assert_selection(&panel, local, &[2], cx); + } +} +fn assert_selection( + panel: &Entity, + local: bool, + expected: &[usize], + cx: &mut TestAppContext, +) { + panel.read_with(cx, |panel, cx| { + if local { + assert_eq!(panel.local_listing(cx).selected(), expected); + } else { + assert_eq!(panel.remote_listing(cx).selected(), expected); + } + }); +} diff --git a/crates/files/src/tests.rs b/crates/files/src/tests.rs index 5f99e050..dd9a51ef 100644 --- a/crates/files/src/tests.rs +++ b/crates/files/src/tests.rs @@ -121,7 +121,9 @@ fn recorded_download( ) -> nocterm_transfers::DownloadRequest { match recording.request(id).unwrap() { nocterm_transfers::Request::Download(request) => request, - nocterm_transfers::Request::Upload(_) => panic!("expected a download request"), + nocterm_transfers::Request::Upload(_) | nocterm_transfers::Request::Relay(_) => { + panic!("expected a download request") + } } } fn recorded_upload( @@ -130,7 +132,9 @@ fn recorded_upload( ) -> nocterm_transfers::UploadRequest { match recording.request(id).unwrap() { nocterm_transfers::Request::Upload(request) => request, - nocterm_transfers::Request::Download(_) => panic!("expected an upload request"), + nocterm_transfers::Request::Download(_) | nocterm_transfers::Request::Relay(_) => { + panic!("expected an upload request") + } } } fn complete_recorded( @@ -710,6 +714,8 @@ mod listing_ui; mod operations_ui; #[path = "plugin_tests.rs"] mod plugin; +#[path = "selection_ui_tests.rs"] +mod selection_ui; #[path = "transfer_ui_tests.rs"] mod transfer_ui; diff --git a/crates/files/src/transfer_ui_tests.rs b/crates/files/src/transfer_ui_tests.rs index 8bff14fe..e33b65bf 100644 --- a/crates/files/src/transfer_ui_tests.rs +++ b/crates/files/src/transfer_ui_tests.rs @@ -94,7 +94,8 @@ fn external_folder_drop_pins_context_internal_drag_queues_and_queue_cancel_works let replacement = transfer_remote(true); let (deps, _, recording) = transfer_fixture_deps_at(directory.path()); let service = deps.transfers.clone(); - let (handle, workspace, item, panel) = panel_fixture_with(cx, original.clone(), deps); + let (handle, workspace, item, panel, installed) = + panel_fixture_with_installation(cx, original.clone(), deps); show_local(cx, handle, &workspace, &panel, directory.path()); cx.update_window(handle, |_, window, cx| { drop_external_on_remote(window, &file, cx); @@ -130,8 +131,14 @@ fn external_folder_drop_pins_context_internal_drag_queues_and_queue_cancel_works "second-host" ); cx.update_window(handle, |_, window, cx| { - let queue = - cx.new(|cx| transfers::TransfersView::new(service.clone(), workspace.downgrade(), cx)); + let queue = cx.new(|cx| { + transfers::TransfersView::new( + service.clone(), + installed.projection().clone(), + workspace.downgrade(), + cx, + ) + }); workspace.update(cx, |workspace, cx| workspace.add_item(queue, window, cx)); window.render_frame(cx); window.click(SharedString::from(format!("cancel-upload-{current}")), cx); diff --git a/crates/files/src/transfers.rs b/crates/files/src/transfers.rs index fbe89a16..071162bd 100644 --- a/crates/files/src/transfers.rs +++ b/crates/files/src/transfers.rs @@ -1,7 +1,9 @@ //! Presentation of the retained transfer service. Available outside Explorer. +use crate::projection::TransferProjection; use crate::{ShowTransfers, local::bytes}; use gpui_kit::{ - App, Context, EventEmitter, FocusHandle, Focusable, SharedString, Task, WeakEntity, Window, + App, Context, Entity, EventEmitter, FocusHandle, Focusable, SharedString, Subscription, + WeakEntity, Window, component::{ ActiveTheme as _, Disableable as _, Icon, Sizable as _, button::{Button, ButtonVariants as _}, @@ -13,78 +15,66 @@ use gpui_kit::{ use nocterm_transfers::{Progress, TransferDirection, TransferService}; use nocterm_ui::IconName; use nocterm_workspace::{Item, ItemEvent, Workspace}; -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; pub(super) struct TransferStatus { - service: Arc, - snapshot: Vec, - _poll: Task<()>, + snapshot: Arc<[Progress]>, + _projection: Entity, + _observation: Subscription, } impl TransferStatus { pub(super) fn new( - service: Arc, + projection: Entity, window: &mut Window, cx: &mut Context, ) -> Self { + let snapshot = projection.read(cx).entries().clone(); + let observation = cx.observe_in(&projection, window, |this, projection, window, cx| { + let snapshot = projection.read(cx).entries().clone(); + this.changed(snapshot, window, cx); + }); Self { - snapshot: service.snapshot(), - service, - _poll: cx.spawn_in(window, async move |this, cx| { - loop { - cx.background_executor() - .timer(Duration::from_millis(150)) - .await; - if this - .update_in(cx, |this, window, cx| { - let snapshot = this.service.snapshot(); - if snapshot != this.snapshot { - for job in &snapshot { - if job.state.finished() - && job.failed_files > 0 - && !this - .snapshot - .iter() - .any(|old| old.id == job.id && old.state.finished()) - { - let error = job - .errors - .first() - .map(ToString::to_string) - .unwrap_or_else(|| { - format!( - "{} files failed; review the transfer queue.", - job.failed_files - ) - }); - nocterm_ui::notice::error_action( - window, - cx, - "files-transfer-failed", - "Transfer failed", - error, - "Open Transfers", - |window, cx| { - window.defer(cx, |window, cx| { - window.dispatch_action( - Box::new(ShowTransfers), - cx, - ) - }); - }, - ); - } - } - this.snapshot = snapshot; - cx.notify(); - } + snapshot, + _projection: projection, + _observation: observation, + } + } + fn changed(&mut self, snapshot: Arc<[Progress]>, window: &mut Window, cx: &mut Context) { + for job in snapshot.iter() { + if job.state.finished() + && job.failed_files > 0 + && !self + .snapshot + .iter() + .any(|old| old.id == job.id && old.state.finished()) + { + let error = job + .errors + .first() + .map(ToString::to_string) + .unwrap_or_else(|| { + format!( + "{} files failed; review the transfer queue.", + job.failed_files + ) + }); + nocterm_ui::notice::error_action( + window, + cx, + "files-transfer-failed", + "Transfer failed", + error, + "Open Transfers", + |window, cx| { + window.defer(cx, |window, cx| { + window.dispatch_action(Box::new(ShowTransfers), cx) }) - .is_err() - { - break; - } - } - }), + }, + ); + } } + self.snapshot = snapshot; + cx.notify(); } } impl Render for TransferStatus { @@ -110,40 +100,31 @@ pub(super) struct TransfersView { service: Arc, focus: FocusHandle, workspace: WeakEntity, - snapshot: Vec, - _poll: Task<()>, + snapshot: Arc<[Progress]>, + _projection: Entity, + _observation: Subscription, } impl TransfersView { pub(super) fn new( service: Arc, + projection: Entity, workspace: WeakEntity, cx: &mut Context, ) -> Self { - let snapshot = service.snapshot(); + // One bounded initial read preserves immediate queue actions when opened. + // Periodic reads and all subsequent fan-out use the background projection. + let snapshot = service.snapshot().into(); + let observation = cx.observe(&projection, |this, projection, cx| { + this.snapshot = projection.read(cx).entries().clone(); + cx.notify(); + }); Self { service, focus: cx.focus_handle(), workspace, snapshot, - _poll: cx.spawn(async move |this, cx| { - loop { - cx.background_executor() - .timer(Duration::from_millis(150)) - .await; - if this - .update(cx, |this, cx| { - let snapshot = this.service.snapshot(); - if this.snapshot != snapshot { - this.snapshot = snapshot; - cx.notify(); - } - }) - .is_err() - { - break; - } - } - }), + _projection: projection, + _observation: observation, } } } @@ -189,6 +170,7 @@ impl Render for TransfersView { let direction = match job.direction { TransferDirection::Upload => "Upload", TransferDirection::Download => "Download", + TransferDirection::Relay => "Copy", }; let description = format!( "{direction} · {} · {} complete / {} files · {} / {}{} · {:?}", diff --git a/crates/hub-agents-model/Cargo.toml b/crates/hub-agents-model/Cargo.toml new file mode 100644 index 00000000..147a5f2a --- /dev/null +++ b/crates/hub-agents-model/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "nocterm-hub-agents-model" +description = "Agents and agent servers of a hub: records, validation and the ports the hub adapter implements." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "domain" + +[features] +test-support = [] + +[dependencies] +nocterm-hubs-model.workspace = true +nocterm-session.workspace = true + +futures.workspace = true +thiserror.workspace = true + +[lints] +workspace = true diff --git a/crates/hub-agents-model/src/agent.rs b/crates/hub-agents-model/src/agent.rs new file mode 100644 index 00000000..13dfdef2 --- /dev/null +++ b/crates/hub-agents-model/src/agent.rs @@ -0,0 +1,110 @@ +//! An agent: a named configuration that costs nothing while no chat runs it. + +use crate::error::DraftError; +use crate::ids::{AgentId, ProviderId, ServerId}; + +/// The longest agent name, in characters. +pub const MAX_NAME_CHARS: usize = 80; +/// The longest instructions, in bytes. +pub const MAX_INSTRUCTIONS_BYTES: usize = 32 * 1024; +/// The longest model name, in bytes. +const MAX_MODEL_BYTES: usize = 200; + +/// When an agent waits for a person before it uses a tool. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ApprovalMode { + /// Every tool runs at once. + #[default] + Auto, + /// Tools that change something wait for "Allow / Allow in this chat / Deny" from any + /// device; read-only tools run at once. + Ask, +} + +/// One agent of the account, as the hub lists it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubAgent { + pub id: AgentId, + pub name: String, + pub instructions: String, + pub provider: ProviderId, + /// The model new chats start with; always one of `models`. + pub model: String, + /// The models a chat may switch to. + pub models: Vec, + pub approval: ApprovalMode, + /// The servers new chats start with. + pub servers: Vec, +} + +impl HubAgent { + /// The form that edits this agent. + pub fn draft(&self) -> AgentDraft { + AgentDraft { + name: self.name.clone(), + instructions: self.instructions.clone(), + provider: Some(self.provider.clone()), + model: self.model.clone(), + models: self.models.clone(), + approval: self.approval, + servers: self.servers.clone(), + } + } +} + +/// What a person typed to create or change an agent. [`AgentDraft::validate`] checks it +/// before it reaches the hub. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct AgentDraft { + pub name: String, + pub instructions: String, + pub provider: Option, + /// The default model; empty takes the first of `models`. + pub model: String, + pub models: Vec, + pub approval: ApprovalMode, + pub servers: Vec, +} + +impl AgentDraft { + /// The draft with trimmed text, duplicate models dropped and the default model among the + /// models, or the first rule it breaks. + pub fn validate(mut self) -> Result { + self.name = self.name.trim().to_owned(); + if self.name.is_empty() { + return Err(DraftError::NameMissing); + } + if self.name.chars().count() > MAX_NAME_CHARS { + return Err(DraftError::NameTooLong); + } + if self.instructions.len() > MAX_INSTRUCTIONS_BYTES { + return Err(DraftError::InstructionsTooLong); + } + if self.provider.is_none() { + return Err(DraftError::ProviderMissing); + } + let mut models: Vec = Vec::with_capacity(self.models.len()); + for model in self.models.iter().map(|model| model.trim()) { + if model.is_empty() || models.iter().any(|known| known == model) { + continue; + } + if model.len() > MAX_MODEL_BYTES { + return Err(DraftError::ModelTooLong); + } + models.push(model.to_owned()); + } + let model = self.model.trim(); + let model = if model.is_empty() { + models.first().cloned().ok_or(DraftError::ModelMissing)? + } else { + model.to_owned() + }; + if !models.contains(&model) { + models.insert(0, model.clone()); + } + self.model = model; + self.models = models; + self.servers.dedup(); + Ok(self) + } +} diff --git a/crates/hub-agents-model/src/error.rs b/crates/hub-agents-model/src/error.rs new file mode 100644 index 00000000..1bdae637 --- /dev/null +++ b/crates/hub-agents-model/src/error.rs @@ -0,0 +1,39 @@ +//! Typed failures of the agent ports and of the forms people fill in. + +use nocterm_hubs_model::HubError; + +/// Why a form cannot be saved as typed. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum DraftError { + #[error("Enter a name.")] + NameMissing, + #[error("The name is too long.")] + NameTooLong, + #[error("The instructions are too long.")] + InstructionsTooLong, + #[error("Choose a provider.")] + ProviderMissing, + #[error("Add at least one model.")] + ModelMissing, + #[error("A model name is too long.")] + ModelTooLong, + #[error("The address must start with https:// or http://.")] + BaseUrl, +} + +/// Why an agent, provider or server call failed. +#[derive(Clone, Debug, thiserror::Error)] +pub enum HubAgentsError { + /// The hub could not be reached or refused the call; the cause tells why. + #[error(transparent)] + Hub(#[from] HubError), + /// The form breaks a rule; nothing was sent. + #[error(transparent)] + Draft(#[from] DraftError), + /// The hub answered with a record this client cannot read. + #[error("the hub sent a record this client cannot read")] + Malformed, + /// A limit set by the hub's administrator is reached. + #[error("a limit of the hub is reached")] + LimitReached, +} diff --git a/crates/hub-agents-model/src/ids.rs b/crates/hub-agents-model/src/ids.rs new file mode 100644 index 00000000..de065999 --- /dev/null +++ b/crates/hub-agents-model/src/ids.rs @@ -0,0 +1,56 @@ +//! Identifiers the hub gives its agents, providers and servers. + +use std::fmt; + +/// The longest identifier this client accepts from a hub. +pub const MAX_ID_BYTES: usize = 128; + +/// An identifier a hub sent was empty, too long or not plain text. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +#[error("a hub identifier holds 1 to 128 visible ASCII characters")] +pub struct IdError; + +macro_rules! hub_id { + ($(#[$meta:meta])* $name:ident) => { + $(#[$meta])* + #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] + pub struct $name(String); + + impl $name { + /// Checks the shape only; the hub decides whether it names anything. + pub fn new(id: impl Into) -> Result { + let id = id.into(); + if id.is_empty() + || id.len() > MAX_ID_BYTES + || !id.bytes().all(|byte| byte.is_ascii_graphic()) + { + return Err(IdError); + } + Ok(Self(id)) + } + + pub fn as_str(&self) -> &str { + &self.0 + } + } + + impl fmt::Display for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } + } + }; +} + +hub_id!( + /// An agent of the account. + AgentId +); +hub_id!( + /// A model provider: one of the hub's or one of the account's own. + ProviderId +); +hub_id!( + /// A server connected to the account's agents. + ServerId +); diff --git a/crates/hub-agents-model/src/lib.rs b/crates/hub-agents-model/src/lib.rs new file mode 100644 index 00000000..59b4f020 --- /dev/null +++ b/crates/hub-agents-model/src/lib.rs @@ -0,0 +1,41 @@ +//! The agents, model providers and agent servers of a hub account, as the desktop shows and +//! edits them. +//! +//! Owns: the records of a listing ([`HubAgent`], [`Provider`], [`HubServer`], [`AgentKey`]), +//! the validated forms people fill in ([`AgentDraft`], [`ProviderDraft`], [`NewServer`]) with +//! their rules ([`DraftError`]), the server states agents report ([`ServerStatus`]), the ports +//! the hub adapter implements ([`HubAgentDirectory`], [`HubServers`], [`HubChanges`]) and +//! their typed error ([`HubAgentsError`]). +//! Does not own: the hub protocol (`nocterm-hub-io` is the adapter), the per-hub lists and their +//! refresh (`nocterm-hub-agents`), chats and their transcript (an ordinary agent chat over the +//! hub's `AgentConnector`), views. +//! Ports provided: `HubAgentDirectory`, `HubServers`, `HubChanges`. Ports used: +//! `nocterm-hubs-model` (`HubId`, `HubError`) and the `Secret` of `nocterm-session`. +//! Settings: none. Globals: none. +//! Invariants: no I/O and no protocol types; a provider key is write-only and never formatted; +//! an agent's default model is one of its models; a draft that breaks a rule never reaches a +//! port. +//! +//! With the `test-support` feature, `testing::FakeHubAgents` implements every port in memory +//! for consumers' tests. + +mod agent; +mod error; +mod ids; +mod ports; +mod provider; +mod server; +#[cfg(any(test, feature = "test-support"))] +pub mod testing; + +pub use agent::{AgentDraft, ApprovalMode, HubAgent, MAX_INSTRUCTIONS_BYTES, MAX_NAME_CHARS}; +pub use error::{DraftError, HubAgentsError}; +pub use ids::{AgentId, IdError, MAX_ID_BYTES, ProviderId, ServerId}; +pub use ports::{ + AgentsFuture, HubAgentDirectory, HubChange, HubChangeHints, HubChanges, HubServers, +}; +pub use provider::{Provider, ProviderDraft, ProviderKind, ProviderScope, ProviderTest}; +pub use server::{AgentKey, HubServer, NewServer, ServerRemoval, ServerStatus}; + +#[cfg(test)] +mod tests; diff --git a/crates/hub-agents-model/src/ports.rs b/crates/hub-agents-model/src/ports.rs new file mode 100644 index 00000000..73ace9a8 --- /dev/null +++ b/crates/hub-agents-model/src/ports.rs @@ -0,0 +1,82 @@ +//! The ports through which the desktop reaches the agents and agent servers of a hub. +//! +//! Each call names the hub; the adapter uses the account this device is signed in to there. +//! None needs the vault: agents, providers, servers and chats never wait for an unlock. + +use futures::future::BoxFuture; +use nocterm_hubs_model::HubId; + +use crate::agent::{AgentDraft, HubAgent}; +use crate::error::HubAgentsError; +use crate::ids::{AgentId, ProviderId, ServerId}; +use crate::provider::{Provider, ProviderDraft, ProviderTest}; +use crate::server::{AgentKey, HubServer, NewServer, ServerRemoval}; + +/// A call of an agents port. +pub type AgentsFuture = BoxFuture<'static, Result>; + +/// The account's agents and the providers they may use. +pub trait HubAgentDirectory: Send + Sync { + fn agents(&self, hub: &HubId) -> AgentsFuture>; + fn create_agent(&self, hub: &HubId, draft: AgentDraft) -> AgentsFuture; + fn update_agent( + &self, + hub: &HubId, + agent: &AgentId, + draft: AgentDraft, + ) -> AgentsFuture; + /// Deletes the agent and its chats. + fn delete_agent(&self, hub: &HubId, agent: &AgentId) -> AgentsFuture<()>; + + /// The hub's providers and the account's own. + fn providers(&self, hub: &HubId) -> AgentsFuture>; + /// Adds a provider of the account's own. + fn create_provider(&self, hub: &HubId, draft: ProviderDraft) -> AgentsFuture; + fn update_provider( + &self, + hub: &HubId, + provider: &ProviderId, + draft: ProviderDraft, + ) -> AgentsFuture; + fn delete_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture<()>; + /// Calls the provider from an agent worker and lists the models it offers. + fn test_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture; +} + +/// The account's agent key and the servers connected to its agents. +pub trait HubServers: Send + Sync { + /// The account's agent key; the hub creates it on first use. + fn agent_key(&self, hub: &HubId) -> AgentsFuture; + /// Replaces the agent key; every server needs its line written again. + fn rotate_agent_key(&self, hub: &HubId) -> AgentsFuture; + fn servers(&self, hub: &HubId) -> AgentsFuture>; + /// Records a server whose `authorized_keys` already holds the agent key's line. + fn add_server(&self, hub: &HubId, server: NewServer) -> AgentsFuture; + /// Signs in with the agent key and updates the status. + fn check_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture; + /// Agents stop using the server at once. The line stays on the server. + fn remove_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture; +} + +/// What changed on a hub, here or on another device. A hint means "list it again". +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HubChange { + /// Chats were created, renamed, ran or deleted. + Chats, + /// Agents, or the providers and models they use. + Agents, + /// Servers connected to agents, the agent key, or what agents last found on a server. + Servers, +} + +/// Lossy hints of what changed on a hub. +pub trait HubChangeHints: Send { + /// Waits for the next hint; `None` when the hub ended the stream (subscribe again). + fn next(&mut self) -> BoxFuture<'_, Result, HubAgentsError>>; +} + +/// Follows the chats, agents and servers of a hub, so every window shows what other devices +/// changed. +pub trait HubChanges: Send + Sync { + fn watch(&self, hub: &HubId) -> AgentsFuture>; +} diff --git a/crates/hub-agents-model/src/provider.rs b/crates/hub-agents-model/src/provider.rs new file mode 100644 index 00000000..72a67a04 --- /dev/null +++ b/crates/hub-agents-model/src/provider.rs @@ -0,0 +1,126 @@ +//! Model providers: an API endpoint with a key, the hub's or the account's own. + +use std::fmt; + +use nocterm_session::Secret; + +use crate::error::DraftError; +use crate::ids::ProviderId; + +/// The longest provider name, in characters. +const MAX_PROVIDER_NAME_CHARS: usize = 80; +/// The longest base URL, in bytes. +const MAX_BASE_URL_BYTES: usize = 2048; + +/// The API a provider speaks. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ProviderKind { + /// OpenAI-compatible chat completions (OpenAI, Gemini, OpenRouter, vLLM, Ollama, ...). + #[default] + OpenAi, + /// The Anthropic messages API. + Anthropic, +} + +impl ProviderKind { + /// The base URL a new provider of this kind starts with. + pub fn default_base_url(self) -> &'static str { + match self { + Self::OpenAi => "https://api.openai.com/v1", + Self::Anthropic => "https://api.anthropic.com", + } + } +} + +/// Who a provider belongs to. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ProviderScope { + /// The account's own; only its owner sees and uses it. + #[default] + Account, + /// The hub's; administrators manage it and every account may use it. + Hub, +} + +/// A provider the account may use, as the hub lists it. Its key is never returned. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Provider { + pub id: ProviderId, + pub scope: ProviderScope, + pub kind: ProviderKind, + pub name: String, + pub base_url: String, + /// The models agents may use through it. + pub models: Vec, + pub has_key: bool, + /// The caller may change or delete it. + pub editable: bool, +} + +/// What a person typed to add or change a provider of their own. +#[derive(Clone, Default, PartialEq)] +pub struct ProviderDraft { + pub kind: ProviderKind, + pub name: String, + pub base_url: String, + pub models: Vec, + /// Write-only. `None` on a change keeps the stored key. + pub api_key: Option, +} + +impl fmt::Debug for ProviderDraft { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("ProviderDraft") + .field("kind", &self.kind) + .field("name", &self.name) + .field("base_url", &self.base_url) + .field("models", &self.models) + .field("api_key", &self.api_key.as_ref().map(|_| "")) + .finish() + } +} + +impl ProviderDraft { + /// The draft with trimmed text and models, or the first rule it breaks. + pub fn validate(mut self) -> Result { + self.name = self.name.trim().to_owned(); + if self.name.is_empty() { + return Err(DraftError::NameMissing); + } + if self.name.chars().count() > MAX_PROVIDER_NAME_CHARS { + return Err(DraftError::NameTooLong); + } + self.base_url = self.base_url.trim().trim_end_matches('/').to_owned(); + if !self.base_url.starts_with("https://") && !self.base_url.starts_with("http://") { + return Err(DraftError::BaseUrl); + } + if self.base_url.len() > MAX_BASE_URL_BYTES { + return Err(DraftError::BaseUrl); + } + let mut models: Vec = Vec::new(); + for model in self.models.iter().map(|model| model.trim()) { + if !model.is_empty() && !models.iter().any(|known| known == model) { + models.push(model.to_owned()); + } + } + self.models = models; + if self + .api_key + .as_ref() + .is_some_and(|key| key.expose().trim().is_empty()) + { + self.api_key = None; + } + Ok(self) + } +} + +/// What testing a provider showed. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ProviderTest { + pub ok: bool, + /// What went wrong, readable by a person; empty when it worked. + pub message: String, + /// The models the endpoint lists, when it lists any. + pub models: Vec, +} diff --git a/crates/hub-agents-model/src/server.rs b/crates/hub-agents-model/src/server.rs new file mode 100644 index 00000000..157ce657 --- /dev/null +++ b/crates/hub-agents-model/src/server.rs @@ -0,0 +1,107 @@ +//! The account's agent key and the servers it is written to. + +use crate::ids::ServerId; + +/// The account's agent key: one Ed25519 key per account, without expiry. A server is +/// connected to agents when its `authorized_keys` holds [`AgentKey::authorized_line`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AgentKey { + /// OpenSSH public key text. + pub public_key: String, + /// The whole `authorized_keys` line with its restrictions. + pub authorized_line: String, + /// `SHA256:…`. + pub fingerprint: String, +} + +impl AgentKey { + /// Whether `line` of an `authorized_keys` file is this key's line, whatever options or + /// comment it carries. + pub fn is_line_of(&self, line: &str) -> bool { + let key = key_blob(&self.public_key); + !key.is_empty() && line.split_whitespace().any(|part| part == key) + } +} + +/// The base64 blob of an OpenSSH public key text (`ssh-ed25519 AAAA… comment`). +fn key_blob(public_key: &str) -> &str { + public_key.split_whitespace().nth(1).unwrap_or_default() +} + +/// What agents last found when they signed in to a server. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ServerStatus { + /// Not checked yet. + #[default] + Unchecked, + /// The agent key signs in. + Ready, + /// The server refuses the agent key. + NoAccess, + /// The server presents another host key than the recorded one; agents do not connect. + HostKeyChanged, + /// The server could not be reached. + Unreachable, + /// The agent key was replaced after its line was written. + KeyRotated, +} + +impl ServerStatus { + /// Short text for the server's row. + pub fn label(self) -> &'static str { + match self { + Self::Unchecked => "Not checked", + Self::Ready => "Agents ✓", + Self::NoAccess => "No access — reconnect", + Self::HostKeyChanged => "Host key changed", + Self::Unreachable => "Unreachable", + Self::KeyRotated => "Reconnect needed", + } + } + + /// Whether agents can sign in now. + pub fn is_ready(self) -> bool { + self == Self::Ready + } +} + +/// A server connected to the account's agents. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubServer { + pub id: ServerId, + pub name: String, + pub host: String, + pub port: u16, + pub user: String, + pub host_key_fingerprint: String, + pub status: ServerStatus, + /// Why the status is not ready, readable by a person; empty otherwise. + pub status_message: String, +} + +impl HubServer { + /// Whether this record is the server `host:port` with `user`. + pub fn is(&self, host: &str, port: u16, user: &str) -> bool { + self.host.eq_ignore_ascii_case(host) && self.port == port && self.user == user + } +} + +/// A server whose `authorized_keys` already holds the agent key's line. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NewServer { + pub name: String, + pub host: String, + pub port: u16, + pub user: String, + /// The OpenSSH public host key this device recorded for the server. + pub host_key: String, +} + +/// What removing a server came to. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ServerRemoval { + /// The agent key's line was removed from the server too. + pub line_removed: bool, + /// Why the line stayed, readable by a person. + pub message: String, +} diff --git a/crates/hub-agents-model/src/testing.rs b/crates/hub-agents-model/src/testing.rs new file mode 100644 index 00000000..6dcf484f --- /dev/null +++ b/crates/hub-agents-model/src/testing.rs @@ -0,0 +1,365 @@ +//! An in-memory hub for consumers' tests: agents, providers, the agent key and servers. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::channel::mpsc; +use futures::future::BoxFuture; +use futures::{FutureExt as _, StreamExt as _}; +use nocterm_hubs_model::{HubError, HubId}; + +use crate::{ + AgentDraft, AgentId, AgentKey, AgentsFuture, HubAgent, HubAgentDirectory, HubAgentsError, + HubChange, HubChangeHints, HubChanges, HubServer, HubServers, NewServer, Provider, + ProviderDraft, ProviderId, ProviderScope, ProviderTest, ServerId, ServerRemoval, ServerStatus, +}; + +#[derive(Default)] +struct Account { + agents: Vec, + providers: Vec, + servers: Vec, + key: u32, + next: u32, +} + +#[derive(Default)] +struct State { + accounts: HashMap, + offline: bool, + hints: Vec>, + calls: Vec, +} + +/// Implements every agents port in memory. Clones share one state. +#[derive(Clone, Default)] +pub struct FakeHubAgents(Arc>); + +fn lock(state: &Mutex) -> MutexGuard<'_, State> { + state.lock().unwrap_or_else(PoisonError::into_inner) +} + +fn ready(value: Result) -> AgentsFuture { + Box::pin(async move { value }) +} + +impl FakeHubAgents { + pub fn new() -> Self { + Self::default() + } + + /// Adds a provider of the hub with `models`. + pub fn with_provider(self, hub: &HubId, name: &str, models: &[&str]) -> Self { + { + let mut state = lock(&self.0); + let account = state.accounts.entry(hub.clone()).or_default(); + account.next += 1; + let id = ProviderId::new(format!("provider-{}", account.next)).expect("id"); + account.providers.push(Provider { + id, + scope: ProviderScope::Hub, + kind: crate::ProviderKind::OpenAi, + name: name.to_owned(), + base_url: "https://models.example/v1".into(), + models: models.iter().map(|model| (*model).to_owned()).collect(), + has_key: true, + editable: false, + }); + } + self + } + + /// Adds an agent with `model` through the first provider of `hub`. + pub fn with_agent(self, hub: &HubId, name: &str, model: &str) -> Self { + let provider = lock(&self.0) + .accounts + .get(hub) + .and_then(|account| account.providers.first()) + .map(|provider| provider.id.clone()) + .unwrap_or_else(|| ProviderId::new("provider-0").expect("id")); + let draft = AgentDraft { + name: name.to_owned(), + provider: Some(provider), + models: vec![model.to_owned()], + ..AgentDraft::default() + }; + futures::executor::block_on(self.create_agent(hub, draft)).expect("agent"); + self + } + + /// Makes every call fail as if the hub could not be reached. + pub fn set_offline(&self, offline: bool) { + lock(&self.0).offline = offline; + } + + /// Sets the status agents report for the server `host`. + pub fn set_status(&self, hub: &HubId, host: &str, status: ServerStatus) { + let mut state = lock(&self.0); + if let Some(account) = state.accounts.get_mut(hub) { + for server in account.servers.iter_mut().filter(|s| s.host == host) { + server.status = status; + } + } + } + + /// Tells every watcher that `change` happened, as another device would. + pub fn changed(&self, change: HubChange) { + lock(&self.0) + .hints + .retain(|hint| hint.unbounded_send(change).is_ok()); + } + + /// The names of the calls made so far, oldest first. + pub fn calls(&self) -> Vec { + lock(&self.0).calls.clone() + } + + fn with( + &self, + call: &str, + hub: &HubId, + change: impl FnOnce(&mut Account) -> Result, + ) -> AgentsFuture { + let mut state = lock(&self.0); + state.calls.push(call.to_owned()); + if state.offline { + return ready(Err(HubError::NotSignedIn.into())); + } + let account = state.accounts.entry(hub.clone()).or_default(); + ready(change(account)) + } +} + +fn next_id(account: &mut Account, prefix: &str) -> String { + account.next += 1; + format!("{prefix}-{}", account.next) +} + +fn agent_of(id: AgentId, draft: AgentDraft) -> HubAgent { + HubAgent { + id, + name: draft.name, + instructions: draft.instructions, + provider: draft.provider.expect("validated"), + model: draft.model, + models: draft.models, + approval: draft.approval, + servers: draft.servers, + } +} + +fn provider_of(id: ProviderId, draft: ProviderDraft) -> Provider { + Provider { + id, + scope: ProviderScope::Account, + kind: draft.kind, + name: draft.name, + base_url: draft.base_url, + models: draft.models, + has_key: draft.api_key.is_some(), + editable: true, + } +} + +fn agent_key(generation: u32) -> AgentKey { + let public_key = format!("ssh-ed25519 AAAAkey{generation} nocterm-agents"); + AgentKey { + authorized_line: format!( + "no-port-forwarding,no-agent-forwarding,no-X11-forwarding {public_key}" + ), + public_key, + fingerprint: format!("SHA256:key{generation}"), + } +} + +impl HubAgentDirectory for FakeHubAgents { + fn agents(&self, hub: &HubId) -> AgentsFuture> { + self.with("agents", hub, |account| Ok(account.agents.clone())) + } + + fn create_agent(&self, hub: &HubId, draft: AgentDraft) -> AgentsFuture { + self.with("create_agent", hub, |account| { + let draft = draft.validate()?; + let id = AgentId::new(next_id(account, "agent")).expect("id"); + let agent = agent_of(id, draft); + account.agents.push(agent.clone()); + Ok(agent) + }) + } + + fn update_agent( + &self, + hub: &HubId, + agent: &AgentId, + draft: AgentDraft, + ) -> AgentsFuture { + let agent = agent.clone(); + self.with("update_agent", hub, move |account| { + let draft = draft.validate()?; + let stored = account + .agents + .iter_mut() + .find(|stored| stored.id == agent) + .ok_or(HubAgentsError::Hub(HubError::NotFound))?; + *stored = agent_of(agent, draft); + Ok(stored.clone()) + }) + } + + fn delete_agent(&self, hub: &HubId, agent: &AgentId) -> AgentsFuture<()> { + let agent = agent.clone(); + self.with("delete_agent", hub, move |account| { + account.agents.retain(|stored| stored.id != agent); + Ok(()) + }) + } + + fn providers(&self, hub: &HubId) -> AgentsFuture> { + self.with("providers", hub, |account| Ok(account.providers.clone())) + } + + fn create_provider(&self, hub: &HubId, draft: ProviderDraft) -> AgentsFuture { + self.with("create_provider", hub, |account| { + let draft = draft.validate()?; + let id = ProviderId::new(next_id(account, "provider")).expect("id"); + let provider = provider_of(id, draft); + account.providers.push(provider.clone()); + Ok(provider) + }) + } + + fn update_provider( + &self, + hub: &HubId, + provider: &ProviderId, + draft: ProviderDraft, + ) -> AgentsFuture { + let provider = provider.clone(); + self.with("update_provider", hub, move |account| { + let draft = draft.validate()?; + let stored = account + .providers + .iter_mut() + .find(|stored| stored.id == provider && stored.editable) + .ok_or(HubAgentsError::Hub(HubError::NotFound))?; + let has_key = stored.has_key || draft.api_key.is_some(); + *stored = Provider { + has_key, + ..provider_of(provider, draft) + }; + Ok(stored.clone()) + }) + } + + fn delete_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture<()> { + let provider = provider.clone(); + self.with("delete_provider", hub, move |account| { + account + .providers + .retain(|stored| stored.id != provider || !stored.editable); + Ok(()) + }) + } + + fn test_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture { + let provider = provider.clone(); + self.with("test_provider", hub, move |account| { + let stored = account + .providers + .iter() + .find(|stored| stored.id == provider) + .ok_or(HubAgentsError::Hub(HubError::NotFound))?; + Ok(ProviderTest { + ok: stored.has_key, + message: if stored.has_key { + String::new() + } else { + "The provider has no key.".into() + }, + models: stored.models.clone(), + }) + }) + } +} + +impl HubServers for FakeHubAgents { + fn agent_key(&self, hub: &HubId) -> AgentsFuture { + self.with("agent_key", hub, |account| Ok(agent_key(account.key))) + } + + fn rotate_agent_key(&self, hub: &HubId) -> AgentsFuture { + self.with("rotate_agent_key", hub, |account| { + account.key += 1; + for server in &mut account.servers { + server.status = ServerStatus::KeyRotated; + } + Ok(agent_key(account.key)) + }) + } + + fn servers(&self, hub: &HubId) -> AgentsFuture> { + self.with("servers", hub, |account| Ok(account.servers.clone())) + } + + fn add_server(&self, hub: &HubId, server: NewServer) -> AgentsFuture { + self.with("add_server", hub, move |account| { + let id = ServerId::new(next_id(account, "server")).expect("id"); + let record = HubServer { + id, + name: server.name, + host: server.host, + port: server.port, + user: server.user, + host_key_fingerprint: server.host_key, + status: ServerStatus::Ready, + status_message: String::new(), + }; + account.servers.push(record.clone()); + Ok(record) + }) + } + + fn check_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let server = server.clone(); + self.with("check_server", hub, move |account| { + account + .servers + .iter() + .find(|stored| stored.id == server) + .cloned() + .ok_or(HubAgentsError::Hub(HubError::NotFound)) + }) + } + + fn remove_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let server = server.clone(); + self.with("remove_server", hub, move |account| { + account.servers.retain(|stored| stored.id != server); + Ok(ServerRemoval { + line_removed: false, + message: String::new(), + }) + }) + } +} + +struct Hints(mpsc::UnboundedReceiver); + +impl HubChangeHints for Hints { + fn next(&mut self) -> BoxFuture<'_, Result, HubAgentsError>> { + self.0.next().map(Ok).boxed() + } +} + +impl HubChanges for FakeHubAgents { + fn watch(&self, hub: &HubId) -> AgentsFuture> { + let (sender, receiver) = mpsc::unbounded(); + let mut state = lock(&self.0); + state.calls.push(format!("watch {hub}")); + if state.offline { + return ready(Err(HubError::NotSignedIn.into())); + } + state.hints.push(sender); + ready(Ok(Box::new(Hints(receiver)) as Box)) + } +} diff --git a/crates/hub-agents-model/src/tests.rs b/crates/hub-agents-model/src/tests.rs new file mode 100644 index 00000000..8415227b --- /dev/null +++ b/crates/hub-agents-model/src/tests.rs @@ -0,0 +1,104 @@ +use nocterm_session::Secret; + +use crate::{ + AgentDraft, AgentKey, DraftError, ProviderDraft, ProviderId, ProviderKind, ServerStatus, +}; + +fn provider() -> Option { + Some(ProviderId::new("p1").unwrap()) +} + +#[test] +fn an_agent_needs_a_name_a_provider_and_a_model() { + let draft = |name: &str, provider, models: &[&str]| AgentDraft { + name: name.into(), + provider, + models: models.iter().map(|m| (*m).to_owned()).collect(), + ..AgentDraft::default() + }; + assert_eq!( + draft(" ", provider(), &["m"]).validate(), + Err(DraftError::NameMissing) + ); + assert_eq!( + draft("ops", None, &["m"]).validate(), + Err(DraftError::ProviderMissing) + ); + assert_eq!( + draft("ops", provider(), &[" "]).validate(), + Err(DraftError::ModelMissing) + ); + let valid = draft(" ops ", provider(), &["a", "b", "a"]) + .validate() + .unwrap(); + assert_eq!(valid.name, "ops"); + assert_eq!(valid.model, "a"); + assert_eq!(valid.models, ["a", "b"]); +} + +#[test] +fn the_default_model_is_always_one_of_the_models() { + let valid = AgentDraft { + name: "ops".into(), + provider: provider(), + model: "big".into(), + models: vec!["small".into()], + ..AgentDraft::default() + } + .validate() + .unwrap(); + assert_eq!(valid.model, "big"); + assert_eq!(valid.models, ["big", "small"]); +} + +#[test] +fn a_provider_needs_a_web_address_and_keeps_its_key_out_of_debug() { + let draft = ProviderDraft { + kind: ProviderKind::Anthropic, + name: "Mine".into(), + base_url: "ftp://x".into(), + models: vec![], + api_key: Some(Secret::new("sk-secret")), + }; + assert_eq!(draft.clone().validate(), Err(DraftError::BaseUrl)); + let valid = ProviderDraft { + base_url: "https://api.anthropic.com/".into(), + ..draft + } + .validate() + .unwrap(); + assert_eq!(valid.base_url, "https://api.anthropic.com"); + assert!(!format!("{valid:?}").contains("sk-secret")); +} + +#[test] +fn an_empty_key_on_a_change_keeps_the_stored_one() { + let valid = ProviderDraft { + name: "Mine".into(), + base_url: "https://x".into(), + api_key: Some(Secret::new(" ")), + ..ProviderDraft::default() + } + .validate() + .unwrap(); + assert!(valid.api_key.is_none()); +} + +#[test] +fn the_agent_key_line_is_recognized_with_any_options_or_comment() { + let key = AgentKey { + public_key: "ssh-ed25519 AAAAblob nocterm".into(), + authorized_line: "no-pty ssh-ed25519 AAAAblob nocterm".into(), + fingerprint: "SHA256:x".into(), + }; + assert!(key.is_line_of("restrict ssh-ed25519 AAAAblob other")); + assert!(!key.is_line_of("ssh-ed25519 AAAAother nocterm")); + assert!(!key.is_line_of("")); +} + +#[test] +fn only_a_ready_server_is_usable() { + assert!(ServerStatus::Ready.is_ready()); + assert!(!ServerStatus::KeyRotated.is_ready()); + assert_eq!(ServerStatus::Ready.label(), "Agents ✓"); +} diff --git a/crates/hub-agents/Cargo.toml b/crates/hub-agents/Cargo.toml new file mode 100644 index 00000000..682eaa09 --- /dev/null +++ b/crates/hub-agents/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "nocterm-hub-agents" +description = "The agents, agent servers and chat lists of the signed-in hubs, kept per hub and refreshed on change." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +nocterm-ai.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-hubs-model.workspace = true +nocterm-ssh-keys.workspace = true + +futures.workspace = true +gpui.workspace = true +thiserror.workspace = true +tracing.workspace = true + +[dev-dependencies] +nocterm-hub-agents-model = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } +gpui = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hub-agents/src/access.rs b/crates/hub-agents/src/access.rs new file mode 100644 index 00000000..e76bc3f7 --- /dev/null +++ b/crates/hub-agents/src/access.rs @@ -0,0 +1,101 @@ +//! Agent access of a saved server: the account's agent key line in the server's +//! `authorized_keys`, written through the session the user opened, and the hub's record of the +//! server (ADR 0148). + +use std::sync::Arc; + +use nocterm_hub_agents_model::{ + HubAgentsError, HubServer, HubServers, NewServer, ServerId, ServerRemoval, +}; +use nocterm_hubs_model::HubId; +use nocterm_ssh_keys::authorized_keys::{AgentHosts, HostAccessError, with_line, without_lines}; + +/// The saved connection whose agent access changes. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AgentTarget { + /// The id of the saved connection, as its session announced it. + pub connection: String, + /// The name the hub shows for the server. + pub name: String, + pub host: String, + pub port: u16, + pub user: String, +} + +/// Why agent access could not be changed. +#[derive(Debug, thiserror::Error)] +pub enum AgentAccessError { + /// No session to the server is open, so its `authorized_keys` cannot be written. + #[error("no session to the server is open")] + NotConnected, + /// This device recorded no host key for the server, so the hub could not check it. + #[error("no host key is recorded for the server")] + UnknownHostKey, + /// The server's `authorized_keys` could not be read or replaced. + #[error("the server's authorized_keys could not be edited")] + Host(#[source] HostAccessError), + /// The hub refused or could not be reached. + #[error(transparent)] + Hub(#[from] HubAgentsError), +} + +impl From for AgentAccessError { + fn from(error: HostAccessError) -> Self { + match error { + HostAccessError::NotConnected => Self::NotConnected, + error => Self::Host(error), + } + } +} + +/// Writes the agent key's line to the server, then records the server on the hub with the host +/// key this device recorded for it. +pub(crate) async fn grant( + servers: Arc, + hosts: Arc, + hub: HubId, + target: AgentTarget, +) -> Result { + let access = hosts.access(&target.connection)?; + if access.identity.host_key.is_empty() { + return Err(AgentAccessError::UnknownHostKey); + } + let key = servers.agent_key(&hub).await?; + let current = access.file.read().await?; + let updated = with_line(¤t, &key.authorized_line); + if updated != current { + access.file.replace(current, updated).await?; + } + let server = NewServer { + name: target.name, + host: target.host, + port: target.port, + user: access.identity.user, + host_key: access.identity.host_key, + }; + Ok(servers.add_server(&hub, server).await?) +} + +/// Removes the agent key's line while a session to the server is open, then the hub's record; +/// without a session the hub is asked to remove the line itself. +pub(crate) async fn withdraw( + servers: Arc, + hosts: Arc, + hub: HubId, + connection: String, + server: ServerId, +) -> Result { + match hosts.access(&connection) { + Ok(access) => { + let key = servers.agent_key(&hub).await?; + let current = access.file.read().await?; + let updated = without_lines(¤t, |line| key.is_line_of(line)); + if updated != current { + access.file.replace(current, updated).await?; + } + } + Err(HostAccessError::NotConnected) => {} + Err(error) => return Err(AgentAccessError::Host(error)), + } + Ok(servers.remove_server(&hub, &server).await?) +} diff --git a/crates/hub-agents/src/installed.rs b/crates/hub-agents/src/installed.rs new file mode 100644 index 00000000..b3fc5d58 --- /dev/null +++ b/crates/hub-agents/src/installed.rs @@ -0,0 +1,38 @@ +//! The ports the service needs and the handle to the installed entity. + +use std::sync::Arc; + +use gpui::{App, AppContext as _, Entity}; +use nocterm_hub_agents_model::{HubAgentDirectory, HubChanges, HubServers}; +use nocterm_ssh_keys::authorized_keys::AgentHosts; + +use crate::service::HubAgents; + +/// The agents ports, supplied by the composition root. +#[derive(Clone)] +pub struct Deps { + pub directory: Arc, + pub servers: Arc, + pub changes: Arc, + /// The sessions of saved connections, through which Agent access edits `authorized_keys`. + pub hosts: Arc, +} + +/// The entity shared by every window, kept alive by whoever composes the application. +#[derive(Clone)] +pub struct Installed { + agents: Entity, +} + +impl Installed { + pub fn agents(&self) -> &Entity { + &self.agents + } +} + +/// Creates the service. It tracks no hub until [`HubAgents::track`] is called for it. +pub fn install(deps: Deps, cx: &mut App) -> Installed { + Installed { + agents: cx.new(|_| HubAgents::new(deps)), + } +} diff --git a/crates/hub-agents/src/lib.rs b/crates/hub-agents/src/lib.rs new file mode 100644 index 00000000..eea2f70c --- /dev/null +++ b/crates/hub-agents/src/lib.rs @@ -0,0 +1,32 @@ +//! The agents, agent servers and chat lists of the signed-in hubs, shared by every window. +//! +//! Owns: one entry per tracked hub with its agents and agent servers (listed when the hub is +//! tracked and again on [`HubAgents::refresh`]), the follow of its chat list (the hub's change +//! hints, re-subscribed with growing delays), and the launches of its agents as ordinary +//! agents of `nocterm-ai`, so a hub window's new-chat menu offers them beside the local ones, +//! and Agent access of a saved server ([`AgentTarget`]): whether a session to it is open, the +//! agent key's line written to or removed from its `authorized_keys` through that session, and +//! the hub's record of it. +//! Does not own: the hub protocol (the injected ports are implemented by `nocterm-hub-io`), +//! the rules of a draft (`nocterm-hub-agents-model`), the chats themselves (an agent chat over +//! the hub connector), views. +//! Ports used: `HubAgentDirectory`, `HubServers` and `HubChanges` of +//! `nocterm-hub-agents-model` and `AgentHosts` of `nocterm-ssh-keys`, passed in [`Deps`]. Provides: [`Installed`] with the +//! [`HubAgents`] entity and its [`HubAgentsEvent`]s. +//! Settings: none. Globals: none. +//! Invariants: a hub that is no longer tracked forgets its lists and stops its follow; a +//! failed listing keeps the last lists shown and is tried again with growing delays, and a +//! re-subscribed follow reads the lists and the chats again (hints sent meanwhile are lost); every change of a hub's lists emits +//! [`HubAgentsEvent::Changed`] once, every hint of its chat list +//! [`HubAgentsEvent::ChatsChanged`], and a hint of its agents or servers lists them again; a server is recorded on the hub only after the agent +//! key's line was written to it, with the host key this device recorded. + +mod access; +mod installed; +mod service; +#[cfg(test)] +mod tests; + +pub use access::{AgentAccessError, AgentTarget}; +pub use installed::{Deps, Installed, install}; +pub use service::{HubAgents, HubAgentsEvent}; diff --git a/crates/hub-agents/src/service.rs b/crates/hub-agents/src/service.rs new file mode 100644 index 00000000..cb2a6a03 --- /dev/null +++ b/crates/hub-agents/src/service.rs @@ -0,0 +1,301 @@ +//! The entity every window reads the agents and agent servers of a hub from. + +use std::collections::HashMap; +use std::sync::Arc; +use std::time::Duration; + +use gpui::{Context, EventEmitter, Task}; +use nocterm_ai::agents::AgentLaunch; +use nocterm_hub_agents_model::{ + HubAgent, HubAgentDirectory, HubAgentsError, HubChange, HubChanges, HubServer, HubServers, + ServerId, ServerRemoval, +}; +use nocterm_hubs_model::HubId; + +use crate::access::{self, AgentAccessError, AgentTarget}; +use crate::installed::Deps; + +const FIRST_RETRY: Duration = Duration::from_secs(2); +const MAX_RETRY: Duration = Duration::from_secs(60); + +/// What changed for the views. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum HubAgentsEvent { + /// The agents or agent servers of the hub changed. + Changed(HubId), + /// The hub's chats changed: a history that lists them reads them again. + ChatsChanged(HubId), +} + +/// The lists of one tracked hub. +#[derive(Default)] +struct Entry { + agents: Vec, + servers: Vec, + /// Why the last listing failed, while it does. + problem: Option, + /// How long the next listing after a failed one waits; none while listings succeed. + retry: Option, + _listing: Option>, + _follow: Option>, +} + +/// The agents and agent servers of every tracked hub. +pub struct HubAgents { + deps: Deps, + hubs: HashMap, +} + +impl EventEmitter for HubAgents {} + +impl HubAgents { + pub(crate) fn new(deps: Deps) -> Self { + Self { + deps, + hubs: HashMap::new(), + } + } + + /// Starts keeping the lists of `hub`. Tracking twice is harmless. + pub fn track(&mut self, hub: &HubId, cx: &mut Context) { + if self.hubs.contains_key(hub) { + return; + } + let follow = follow_changes(self.deps.changes.clone(), hub.clone(), cx); + self.hubs.insert( + hub.clone(), + Entry { + _follow: Some(follow), + ..Entry::default() + }, + ); + self.refresh(hub, cx); + } + + /// Forgets `hub`: its lists are dropped and its chats are no longer followed. + pub fn untrack(&mut self, hub: &HubId, cx: &mut Context) { + if self.hubs.remove(hub).is_some() { + cx.emit(HubAgentsEvent::Changed(hub.clone())); + cx.notify(); + } + } + + /// Lists the agents and agent servers of `hub` again. + pub fn refresh(&mut self, hub: &HubId, cx: &mut Context) { + let Some(entry) = self.hubs.get_mut(hub) else { + return; + }; + let agents = self.deps.directory.agents(hub); + let servers = self.deps.servers.servers(hub); + let hub_id = hub.clone(); + entry._listing = Some(cx.spawn(async move |this, cx| { + let (agents, servers) = futures::join!(agents, servers); + this.update(cx, |this, cx| this.listed(&hub_id, agents, servers, cx)) + .ok(); + })); + } + + fn listed( + &mut self, + hub: &HubId, + agents: Result, HubAgentsError>, + servers: Result, HubAgentsError>, + cx: &mut Context, + ) { + let Some(entry) = self.hubs.get_mut(hub) else { + return; + }; + let mut problem = None; + match agents { + Ok(agents) => entry.agents = agents, + Err(error) => problem = Some(error), + } + match servers { + Ok(servers) => entry.servers = servers, + Err(error) => problem = problem.or(Some(error)), + } + if let Some(error) = &problem { + tracing::debug!(%error, hub = %hub, "the hub's agents could not be listed"); + // A hub that is still connecting answers soon: the lists are read again then. + let delay = entry + .retry + .map_or(FIRST_RETRY, |delay| (delay * 2).min(MAX_RETRY)); + entry.retry = Some(delay); + let hub_id = hub.clone(); + entry._listing = Some(cx.spawn(async move |this, cx| { + cx.background_executor().timer(delay).await; + this.update(cx, |this, cx| this.refresh(&hub_id, cx)).ok(); + })); + } else { + entry.retry = None; + } + entry.problem = problem; + cx.emit(HubAgentsEvent::Changed(hub.clone())); + cx.notify(); + } + + /// The agents of `hub`, as last listed. + pub fn agents(&self, hub: &HubId) -> &[HubAgent] { + self.hubs.get(hub).map_or(&[], |entry| &entry.agents) + } + + /// The servers connected to the agents of `hub`, as last listed. + pub fn servers(&self, hub: &HubId) -> &[HubServer] { + self.hubs.get(hub).map_or(&[], |entry| &entry.servers) + } + + /// Why the last listing of `hub` failed, while it does. + pub fn problem(&self, hub: &HubId) -> Option<&HubAgentsError> { + self.hubs.get(hub)?.problem.as_ref() + } + + /// The record of `target` among the agent servers of `hub`, as last listed. + pub fn server_of(&self, hub: &HubId, target: &AgentTarget) -> Option<&HubServer> { + self.servers(hub) + .iter() + .find(|server| server.is(&target.host, target.port, &target.user)) + } + + /// Whether a session to the saved connection `connection` is open, so the agent key's line + /// can be written through it now. + pub fn session_open(&self, connection: &str) -> bool { + self.deps.hosts.access(connection).is_ok() + } + + /// Lets the agents of `hub` work on `target`: the agent key's line is written through the + /// session the user opened and the server is recorded on the hub, then in the list. + pub fn grant_access( + &mut self, + hub: &HubId, + target: AgentTarget, + cx: &mut Context, + ) -> Task> { + let work = cx.background_executor().spawn(access::grant( + self.deps.servers.clone(), + self.deps.hosts.clone(), + hub.clone(), + target, + )); + let hub = hub.clone(); + cx.spawn(async move |this, cx| { + let server = work.await?; + this.update(cx, |this, cx| this.recorded(&hub, Some(server), None, cx)) + .ok(); + Ok(()) + }) + } + + /// Stops the agents of `hub` working on `target`. `None` when the hub has no record of it. + pub fn withdraw_access( + &mut self, + hub: &HubId, + target: &AgentTarget, + cx: &mut Context, + ) -> Task, AgentAccessError>> { + let Some(server) = self.server_of(hub, target) else { + return Task::ready(Ok(None)); + }; + let work = cx.background_executor().spawn(access::withdraw( + self.deps.servers.clone(), + self.deps.hosts.clone(), + hub.clone(), + target.connection.clone(), + server.id.clone(), + )); + let (hub, id) = (hub.clone(), server.id.clone()); + cx.spawn(async move |this, cx| { + let removal = work.await?; + this.update(cx, |this, cx| this.recorded(&hub, None, Some(&id), cx)) + .ok(); + Ok(Some(removal)) + }) + } + + /// Applies what the hub answered to a change of its servers: `added` replaces a record of + /// the same id, `removed` leaves the list. + fn recorded( + &mut self, + hub: &HubId, + added: Option, + removed: Option<&ServerId>, + cx: &mut Context, + ) { + let Some(entry) = self.hubs.get_mut(hub) else { + return; + }; + let gone = |server: &HubServer| { + added.as_ref().is_some_and(|added| added.id == server.id) || removed == Some(&server.id) + }; + entry.servers.retain(|server| !gone(server)); + entry.servers.extend(added); + cx.emit(HubAgentsEvent::Changed(hub.clone())); + cx.notify(); + } + + /// The agents of every tracked hub as agents a chat can start. + pub fn launches(&self) -> Vec { + self.hubs + .iter() + .flat_map(|(hub, entry)| { + entry.agents.iter().map(|agent| { + AgentLaunch::hub_agent(hub.clone(), agent.id.to_string(), agent.name.clone()) + }) + }) + .collect() + } + + /// The agents port, for the forms that edit agents and providers. + pub fn directory(&self) -> &Arc { + &self.deps.directory + } + + /// The servers port, for the agent key and the server rows. + pub fn server_port(&self) -> &Arc { + &self.deps.servers + } +} + +/// Follows what changes on `hub`, re-subscribing with growing delays, until it is untracked: +/// changed chats are announced, changed agents or servers are listed again. Hints sent while +/// no subscription was open are lost, so after a re-subscription everything is read again. +fn follow_changes( + changes: Arc, + hub: HubId, + cx: &mut Context, +) -> Task<()> { + cx.spawn(async move |this, cx| { + let mut delay = FIRST_RETRY; + let mut subscribed = false; + loop { + if let Ok(mut hints) = changes.watch(&hub).await { + delay = FIRST_RETRY; + if std::mem::replace(&mut subscribed, true) { + let alive = this.update(cx, |this, cx| { + this.refresh(&hub, cx); + cx.emit(HubAgentsEvent::ChatsChanged(hub.clone())); + }); + if alive.is_err() { + return; + } + } + while let Ok(Some(change)) = hints.next().await { + let alive = this.update(cx, |this, cx| match change { + HubChange::Chats => cx.emit(HubAgentsEvent::ChatsChanged(hub.clone())), + HubChange::Agents | HubChange::Servers => this.refresh(&hub, cx), + }); + if alive.is_err() { + return; + } + } + } + cx.background_executor().timer(delay).await; + delay = (delay * 2).min(MAX_RETRY); + if this + .read_with(cx, |this, _| !this.hubs.contains_key(&hub)) + .unwrap_or(true) + { + return; + } + } + }) +} diff --git a/crates/hub-agents/src/tests.rs b/crates/hub-agents/src/tests.rs new file mode 100644 index 00000000..15d06c5b --- /dev/null +++ b/crates/hub-agents/src/tests.rs @@ -0,0 +1,222 @@ +use std::sync::Arc; + +use gpui::TestAppContext; +use nocterm_hub_agents_model::HubChange; +use nocterm_hub_agents_model::testing::FakeHubAgents; +use nocterm_hubs_model::{HubAddress, HubId}; + +use crate::{AgentAccessError, AgentTarget, Deps, HubAgentsEvent, install}; +use nocterm_ssh_keys::testing::{FakeAgentHosts, FakeAuthorizedKeys}; + +fn hub() -> HubId { + HubId::from(HubAddress::parse("hub.example:7443").unwrap()) +} + +fn deps(fake: &FakeHubAgents) -> Deps { + deps_over(fake, &FakeAgentHosts::default()) +} + +fn deps_over(fake: &FakeHubAgents, hosts: &FakeAgentHosts) -> Deps { + Deps { + directory: Arc::new(fake.clone()), + servers: Arc::new(fake.clone()), + changes: Arc::new(fake.clone()), + hosts: Arc::new(hosts.clone()), + } +} + +fn web() -> AgentTarget { + AgentTarget { + connection: "web".into(), + name: "Web".into(), + host: "web.example".into(), + port: 22, + user: "deploy".into(), + } +} + +#[gpui::test] +fn a_tracked_hub_offers_its_agents_as_agents_a_chat_can_start(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new() + .with_provider(&hub(), "Models", &["m1"]) + .with_agent(&hub(), "Ops", "m1"); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert_eq!(agents.agents(&hub()).len(), 1); + let launches = agents.launches(); + assert_eq!(launches.len(), 1); + assert_eq!(launches[0].name, "Ops"); + assert_eq!(launches[0].hub.as_ref().unwrap().hub, hub()); + }); + agents.update(cx, |agents, cx| agents.untrack(&hub(), cx)); + agents.read_with(cx, |agents, _| assert!(agents.launches().is_empty())); +} + +#[gpui::test] +fn a_change_of_the_hubs_chats_is_told_to_the_views(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new(); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let _subscription = cx.update({ + let seen = seen.clone(); + |cx| { + cx.subscribe(&agents, move |_, event: &HubAgentsEvent, _| { + seen.lock().unwrap().push(event.clone()); + }) + } + }); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + fake.changed(HubChange::Chats); + cx.run_until_parked(); + assert!( + seen.lock() + .unwrap() + .contains(&HubAgentsEvent::ChatsChanged(hub())) + ); +} + +#[gpui::test] +fn a_failed_listing_keeps_the_last_lists_and_says_why(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new() + .with_provider(&hub(), "Models", &["m1"]) + .with_agent(&hub(), "Ops", "m1"); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + fake.set_offline(true); + agents.update(cx, |agents, cx| agents.refresh(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert_eq!(agents.agents(&hub()).len(), 1); + assert!(agents.problem(&hub()).is_some()); + }); +} + +#[gpui::test] +fn a_listing_that_failed_is_read_again_once_the_hub_answers(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new() + .with_provider(&hub(), "Models", &["m1"]) + .with_agent(&hub(), "Ops", "m1"); + fake.set_offline(true); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(agents.problem(&hub()).is_some())); + fake.set_offline(false); + cx.executor() + .advance_clock(std::time::Duration::from_secs(2)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert!(agents.problem(&hub()).is_none()); + assert_eq!(agents.agents(&hub()).len(), 1); + }); +} + +#[gpui::test] +fn granting_access_writes_the_agent_key_line_and_records_the_server(cx: &mut TestAppContext) { + let (fake, hosts) = (FakeHubAgents::new(), FakeAgentHosts::default()); + let file = FakeAuthorizedKeys::with_text("ssh-ed25519 AAAAown me\n"); + hosts.connect("web", "deploy", "web.example", file.clone()); + let agents = cx + .update(|cx| install(deps_over(&fake, &hosts), cx)) + .agents() + .clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(agents.session_open("web"))); + + let granted = agents.update(cx, |agents, cx| agents.grant_access(&hub(), web(), cx)); + cx.run_until_parked(); + + assert!(cx.foreground_executor().block_test(granted).is_ok()); + let text = file.text(); + assert!(text.starts_with("ssh-ed25519 AAAAown me\n"), "{text}"); + assert!(text.contains("AAAAkey0 nocterm-agents"), "{text}"); + agents.read_with(cx, |agents, _| { + let server = agents + .server_of(&hub(), &web()) + .expect("the server is recorded"); + assert_eq!(server.host_key_fingerprint, "ssh-ed25519 AAAAfakehostkey"); + }); +} + +#[gpui::test] +fn access_needs_an_open_session_and_records_nothing_without_one(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new(); + let agents = cx.update(|cx| install(deps(&fake), cx)).agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(!agents.session_open("web"))); + + let granted = agents.update(cx, |agents, cx| agents.grant_access(&hub(), web(), cx)); + cx.run_until_parked(); + + assert!(matches!( + cx.foreground_executor().block_test(granted), + Err(AgentAccessError::NotConnected) + )); + agents.read_with(cx, |agents, _| { + assert!(agents.server_of(&hub(), &web()).is_none()) + }); +} + +#[gpui::test] +fn withdrawing_access_removes_only_the_agent_key_line_and_the_record(cx: &mut TestAppContext) { + let (fake, hosts) = (FakeHubAgents::new(), FakeAgentHosts::default()); + let file = FakeAuthorizedKeys::with_text("ssh-ed25519 AAAAown me\n"); + hosts.connect("web", "deploy", "web.example", file.clone()); + let agents = cx + .update(|cx| install(deps_over(&fake, &hosts), cx)) + .agents() + .clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + let granted = agents.update(cx, |agents, cx| agents.grant_access(&hub(), web(), cx)); + cx.run_until_parked(); + assert!(cx.foreground_executor().block_test(granted).is_ok()); + + let withdrawn = agents.update(cx, |agents, cx| agents.withdraw_access(&hub(), &web(), cx)); + cx.run_until_parked(); + + assert!(matches!( + cx.foreground_executor().block_test(withdrawn), + Ok(Some(_)) + )); + assert_eq!(file.text(), "ssh-ed25519 AAAAown me\n"); + agents.read_with(cx, |agents, _| { + assert!(agents.server_of(&hub(), &web()).is_none()) + }); +} + +#[gpui::test] +fn a_server_connected_on_another_device_is_listed_after_its_hint(cx: &mut TestAppContext) { + use nocterm_hub_agents_model::{HubServers as _, NewServer}; + let fake = FakeHubAgents::new(); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(agents.servers(&hub()).is_empty())); + let elsewhere = fake.add_server( + &hub(), + NewServer { + name: "prod".into(), + host: "prod.example".into(), + port: 22, + user: "ops".into(), + host_key: String::new(), + }, + ); + futures::executor::block_on(elsewhere).unwrap(); + fake.changed(HubChange::Servers); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert_eq!(agents.servers(&hub())[0].name, "prod"); + }); +} diff --git a/crates/hub-io/Cargo.toml b/crates/hub-io/Cargo.toml new file mode 100644 index 00000000..48af0330 --- /dev/null +++ b/crates/hub-io/Cargo.toml @@ -0,0 +1,48 @@ +[package] +name = "nocterm-hub-io" +description = "Reaches Nocterm hubs through the hub contract: pinned TLS, device credentials and sign-in." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "adapter" + +[dependencies] +hex.workspace = true +nocterm-core.workspace = true +nocterm-ai.workspace = true +nocterm-hubs-model.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-session.workspace = true +nocterm-hub-client.workspace = true +nocterm-hub-proto.workspace = true +nocterm-keys.workspace = true +nocterm-connections-model.workspace = true +nocterm-vault.workspace = true + +async-channel.workspace = true +base64.workspace = true +futures.workspace = true +serde.workspace = true +serde_json.workspace = true +sha2.workspace = true +thiserror.workspace = true +tokio.workspace = true +tonic.workspace = true +toml.workspace = true +tracing.workspace = true +uuid.workspace = true +zeroize.workspace = true + +[dev-dependencies] +nocterm-core = { workspace = true, features = ["test-support"] } +tokio = { workspace = true, features = ["test-util"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } +tempfile.workspace = true +nocterm-connections-model = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hub-io/src/account.rs b/crates/hub-io/src/account.rs new file mode 100644 index 00000000..8346bd15 --- /dev/null +++ b/crates/hub-io/src/account.rs @@ -0,0 +1,258 @@ +//! The open keys of the account, per hub, kept in memory only while the vault is unlocked. +//! +//! A hub's [`Slot`] holds the keyset opened with the vault password (never the password or +//! the unlock key beyond opening it), the collection keys opened from it, and a copy of the +//! encrypted cache file. Locking drops the keys, which wipes them. One notice is raised for +//! every change so a vault can report its status without polling. +//! Forget releases registry residency but preserves a weak Slot identity while +//! a general factory still uses it. A new login promotes that cleared Slot; +//! original captures remain bound to their retired membership and open keys. + +mod cache_publication; +mod flow; +pub(crate) mod key_admission; +mod keyset_hub; +mod lifecycle; +mod recovery; +mod refresh; +mod slot; +pub(crate) use slot::Slot; +pub(crate) mod operations; +pub(crate) mod source; +#[cfg(test)] +pub(crate) mod testing; + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak}; + +use nocterm_hub_client::Keyset; +use nocterm_hubs_model::{HubError, HubId}; +use nocterm_keys::{CollectionKey, OpenError, UnlockKdf, UnlockKey}; +use tokio::sync::watch; + +use crate::cache::{CacheFile, CacheWork, UnlockMethod}; +use crate::classify::{credentials, remote}; + +pub(crate) use flow::KeyFlow; +pub(crate) use lifecycle::SlotReceipt; + +/// Raises a counter whenever keys are opened, dropped or replaced. +pub(crate) struct Notice(watch::Sender); + +impl Notice { + fn new() -> Self { + Self(watch::channel(0).0) + } + + pub(crate) fn raise(&self) { + self.0.send_modify(|count| *count += 1); + } + + pub(crate) fn count(&self) -> u64 { + *self.0.borrow() + } + + pub(crate) fn subscribe(&self) -> watch::Receiver { + self.0.subscribe() + } +} + +/// The opened keyset and the collection keys opened with it. +pub(crate) struct OpenKeys { + keyset: Keyset, + collections: Mutex>>, +} + +impl OpenKeys { + pub(crate) fn new(keyset: Keyset) -> Self { + Self { + keyset, + collections: Mutex::default(), + } + } + + fn collections(&self) -> MutexGuard<'_, HashMap>> { + self.collections + .lock() + .unwrap_or_else(PoisonError::into_inner) + } + + /// The opened keyset. + pub(crate) fn keyset(&self) -> &Keyset { + &self.keyset + } + + /// The key of a collection of the account: the envelope the hub holds was sealed by the + /// account key. An envelope that does not open is [`OpenError`]: the hub altered it. + pub(crate) fn own_collection( + &self, + collection_id: &str, + envelope: &[u8], + ) -> Result, OpenError> { + self.remembered(collection_id, || { + self.keyset.open_collection_key(collection_id, envelope) + }) + } + + fn remembered( + &self, + collection_id: &str, + open: impl FnOnce() -> Result, + ) -> Result, OpenError> { + if let Some(key) = self.collections().get(collection_id) { + return Ok(Arc::clone(key)); + } + let key = Arc::new(open()?); + self.collections() + .insert(collection_id.to_owned(), Arc::clone(&key)); + Ok(key) + } + + /// A new collection key of its own, with the envelope the hub stores for this account. + pub(crate) fn new_collection( + &self, + collection_id: &str, + ) -> Result<(Arc, Vec), HubError> { + let (key, envelope) = self.generate_collection(collection_id)?; + self.collections() + .insert(collection_id.to_owned(), Arc::clone(&key)); + Ok((key, envelope)) + } + /// Transient owners retain the key themselves rather than populating a cache. + pub(crate) fn generate_collection( + &self, + collection_id: &str, + ) -> Result<(Arc, Vec), HubError> { + let key = CollectionKey::generate().map_err(remote)?; + let envelope = self + .keyset + .seal_collection_key(&key, collection_id) + .map_err(remote)?; + Ok((Arc::new(key), envelope)) + } +} + +/// A keyset the typed password does not open, kept until a recovery key opens it. +pub(crate) struct Recovery { + account_id: String, + unlock: UnlockKey, + kdf: UnlockKdf, + method: UnlockMethod, +} + +/// The slots of the hubs this device knows, and where their caches are kept. +pub(crate) struct Accounts { + root: PathBuf, + slots: Mutex>, + notice: Arc, + work: Arc, + // Serialize first loads with eviction, outside the short map lookup lock. + loading: Mutex<()>, +} + +enum SlotRetention { + Resident(Arc), + Forgotten(Weak), +} +impl SlotRetention { + fn upgrade(&self) -> Option> { + match self { + Self::Resident(slot) => Some(slot.clone()), + Self::Forgotten(slot) => slot.upgrade(), + } + } + fn retain(&mut self) -> Option> { + if let Self::Resident(slot) = self { + return Some(slot.clone()); + } + let slot = self.upgrade()?; + *self = Self::Resident(slot.clone()); + Some(slot) + } + fn alive(&self) -> bool { + match self { + Self::Resident(_) => true, + Self::Forgotten(slot) => slot.strong_count() != 0, + } + } +} + +impl Accounts { + #[cfg(test)] + pub(crate) fn new(root: PathBuf) -> Self { + Self::with_runtime(root, None) + } + + pub(crate) fn with_runtime(root: PathBuf, runtime: Option) -> Self { + Self { + root, + slots: Mutex::default(), + notice: Arc::new(Notice::new()), + work: CacheWork::new(runtime), + loading: Mutex::new(()), + } + } + + pub(crate) fn notice(&self) -> &Arc { + &self.notice + } + + /// Release residency without severing a live general account factory. A + /// failed settings removal can still sign in through that cleared Slot. + /// Captured authority remains retired by the preceding erase transition. + pub(crate) fn evict_if(&self, hub: &HubId, original: &Arc) { + let _loading = self.loading.lock().unwrap_or_else(PoisonError::into_inner); + let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner); + slots.retain(|_, slot| slot.alive()); + if slots + .get(hub) + .and_then(SlotRetention::upgrade) + .is_some_and(|slot| Arc::ptr_eq(&slot, original)) + { + slots.insert( + hub.clone(), + SlotRetention::Forgotten(Arc::downgrade(original)), + ); + } + } + + pub(crate) fn loaded_slot(&self, hub: &HubId) -> Option> { + let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner); + let slot = slots.get(hub).and_then(SlotRetention::upgrade); + if slot.is_none() { + slots.remove(hub); + } + slot + } + + fn resident_slot(&self, hub: &HubId) -> Option> { + self.slots + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get_mut(hub) + .and_then(SlotRetention::retain) + } + + /// The slot of a hub; its cache is read the first time. + pub(crate) fn slot(&self, hub: &HubId) -> Result, HubError> { + if let Some(slot) = self.resident_slot(hub) { + return Ok(slot); + } + let _loading = self.loading.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(slot) = self.resident_slot(hub) { + return Ok(slot); + } + let file = CacheFile::new(&self.root, hub.address()) + .map_err(credentials)? + .with_work(self.work.clone()); + let slot = Arc::new(Slot::new(hub.clone(), file, Arc::clone(&self.notice))); + let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner); + slots.retain(|_, slot| slot.alive()); + slots.insert(hub.clone(), SlotRetention::Resident(slot.clone())); + Ok(slot) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/account/cache_publication.rs b/crates/hub-io/src/account/cache_publication.rs new file mode 100644 index 00000000..ce1c7568 --- /dev/null +++ b/crates/hub-io/src/account/cache_publication.rs @@ -0,0 +1,50 @@ +//! An unpublished cache candidate retains its exact original content generation. +//! Staging has no authority; only Slot can atomically admit its namespace commit. +use std::sync::Arc; + +use nocterm_core::fs::{AtomicPublication, CommittedPublication}; + +use crate::cache::{AccountCache, CacheError}; + +pub(crate) struct CacheDraft { + original: Option>, + next: Arc, + publication: AtomicPublication, +} + +impl CacheDraft { + pub(super) fn new( + original: Option>, + next: AccountCache, + publication: AtomicPublication, + ) -> Self { + Self { + original, + next: Arc::new(next), + publication, + } + } + + pub(super) fn account(&self) -> &str { + &self.next.account_id + } + + pub(super) fn matches(&self, current: &Option>) -> bool { + same_generation(&self.original, current) + } + + pub(super) fn commit(self) -> Result<(Arc, CommittedPublication), CacheError> { + Ok((self.next, self.publication.commit()?)) + } +} + +pub(super) fn same_generation( + original: &Option>, + current: &Option>, +) -> bool { + match (original, current) { + (Some(original), Some(current)) => Arc::ptr_eq(original, current), + (None, None) => true, + _ => false, + } +} diff --git a/crates/hub-io/src/account/flow.rs b/crates/hub-io/src/account/flow.rs new file mode 100644 index 00000000..5ab8f868 --- /dev/null +++ b/crates/hub-io/src/account/flow.rs @@ -0,0 +1,594 @@ +//! Opening, creating and recovering the account keys around a sign-in. +//! +//! A first sign-in creates the keyset (one conditional write: a second device that races it +//! opens the keyset that won, it never overwrites it). A later sign-in opens it with the key +//! derived from the password. A password that does not open it (it was reset) leaves the keys +//! waiting for the recovery key. A keyset that does not authenticate although the key is right +//! was altered by the hub: that is an error and nothing is written. + +use super::{ + key_admission::{KeyAdmission, KeySnapshot}, + source::{AccountSubject, AccountSubjects, CapturedMembership, LocalSourceProvider}, +}; +use crate::pool::captured::CapturedClient; +use std::sync::{Arc, Weak}; +use tokio::runtime::Handle; + +use futures::future::BoxFuture; +use nocterm_core::SharedCause; +use nocterm_hub_client::{Keyset, KeysetError, UnlockMethod as KeysetUnlock}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, HubId, KeyStatus}; +use nocterm_keys::{LoginError, LoginSecrets, RecoveryKey, UnlockKdf, UnlockKey}; +use nocterm_session::Secret; + +use super::keyset_hub::KeysetHub; +use super::{Accounts, OpenKeys, Recovery}; +use crate::cache::{AccountCache, KeysetCopy, UnlockMethod}; +use crate::classify::remote; +use crate::directory::who_am_i; +use crate::pool::Pool; +use nocterm_hubs_model::{RecoveryKeyOffer, RecoveryKeyReissue}; + +/// The key work of a sign-in, for every hub. +pub(crate) struct KeyFlow { + pool: Arc, + accounts: Arc, + handle: Handle, + weak: Weak, +} + +/// How often the replacement of the recovery envelope is tried against a keyset that other +/// devices keep writing. +const RESEAL_ATTEMPTS: u32 = 4; + +/// What a keyset write decided. +enum Creation { + Created(nocterm_hub_client::NewKeyset), + /// Another device created the keyset first. + Raced, +} + +/// A keyset that someone else already created is opened, not replaced. +fn creation( + result: Result, +) -> Result { + match result { + Ok(created) => Ok(Creation::Created(created)), + Err(KeysetError::AlreadyExists) => Ok(Creation::Raced), + Err(error) => Err(error), + } +} + +impl KeyFlow { + pub(crate) fn new(pool: Arc, accounts: Arc, handle: Handle) -> Arc { + Arc::new_cyclic(|weak| Self { + pool, + accounts, + handle, + weak: weak.clone(), + }) + } + + pub(crate) fn capture(&self, hub: &HubId) -> Result, HubError> { + let slot = self + .accounts + .loaded_slot(hub) + .ok_or(HubError::NotSignedIn)?; + let member = LocalSourceProvider::new(slot, self.pool.clone()).membership()?; + KeyAdmission::native( + member, + Arc::new(CapturedClient::current(self.pool.clone(), hub.clone())?), + ) + } + + pub(crate) fn published( + &self, + hub: &HubId, + subject: &Arc, + ) -> Result, HubError> { + let slot = self + .accounts + .loaded_slot(hub) + .ok_or(HubError::NotSignedIn)?; + let subjects: Arc = self.pool.clone(); + let member = CapturedMembership::published(&slot, &subjects, subject)?; + KeyAdmission::native( + member, + Arc::new(CapturedClient::current(self.pool.clone(), hub.clone())?), + ) + } + + pub(crate) fn bind_snapshot( + &self, + snapshot: KeySnapshot, + ) -> Result, HubError> { + snapshot.check()?; + let client = Arc::new(CapturedClient::current( + self.pool.clone(), + snapshot.member().hub().clone(), + )?); + KeyAdmission::bound(snapshot, client) + } + + pub(crate) fn run( + &self, + captured: Result, HubError>, + work: impl FnOnce(Arc) -> F + Send + 'static, + ) -> nocterm_hubs_model::HubFuture + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let owner = match captured { + Ok(owner) => owner, + Err(error) => return Box::pin(async move { Err(error) }), + }; + let returned = owner.clone(); + let task = crate::task::spawn(&self.handle, async move { + owner.during(work(owner.clone())).await? + }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + + pub(crate) fn recovery_offer(&self, hub: &HubId) -> Result { + let owner = self.capture(hub)?; + owner.keys()?; + Ok(self.issuer(owner.snapshot()?)) + } + + pub(super) fn issuer(&self, snapshot: KeySnapshot) -> RecoveryKeyReissue { + super::recovery::issuer(self.weak.clone(), snapshot) + } + + pub(super) fn offer( + &self, + owner: &KeyAdmission, + secret: Secret, + ) -> Result { + Ok(super::recovery::offer(secret, owner.snapshot()?)) + } + + fn unconfirmed(&self, owner: &KeyAdmission) -> Result { + owner.recovery_unconfirmed() + } + + fn opened(&self, owner: &KeyAdmission) -> Result { + if self.unconfirmed(owner)? { + Ok(KeyStatus::RecoveryKeyUnconfirmed( + self.issuer(owner.snapshot()?), + )) + } else { + Ok(KeyStatus::Open) + } + } + + /// The keys after a password sign-in: the login password also derives the unlock key. + pub(crate) async fn after_password( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + username: &str, + secrets: LoginSecrets, + ) -> Result { + owner.check()?; + if owner.member().subject()?.account() != account_id { + return Err(HubError::Stale); + } + let kdf = secrets.kdf().clone(); + let unlock = secrets.into_unlock(); + let method = UnlockMethod::Login { + username: username.trim().to_owned(), + }; + self.settle(owner, client, account_id, method, unlock, kdf) + .await + } + + /// Whether the keyset `record` is opened by something other than the login password. The + /// record says so itself: the client never guesses it from how the user signed in. + fn needs_vault_passphrase( + &self, + owner: &KeyAdmission, + record: &v2::Keyset, + ) -> Result { + match KeysetUnlock::of(record).map_err(|error| self.keyset_failure(owner, error))? { + KeysetUnlock::LoginPassword => Ok(false), + KeysetUnlock::VaultPassword(_) => Ok(true), + } + } + + /// The keys after a browser sign-in: nothing opens them until a passphrase is typed. + pub(crate) fn after_browser(&self, owner: &KeyAdmission) -> Result { + owner.check()?; + match owner.keys() { + Ok(_) => self.opened(owner), + Err(HubError::KeysLocked) => Ok(KeyStatus::PassphraseNeeded), + Err(error) => Err(error), + } + } + + /// Whether the recovery key made on this device was never confirmed as saved. + pub(crate) fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + let Some(slot) = self.accounts.loaded_slot(hub) else { + return false; + }; + slot.open() + .is_some_and(|keys| slot.recovery_unconfirmed(keys.keyset().account_id())) + } + + /// The count of changes to any hub's account keys (opened, dropped, replaced) so far. + pub(crate) fn keys_revision(&self) -> u64 { + self.accounts.notice().count() + } + + /// Resolves with the count once it differs from `seen`. + pub(crate) fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64> { + let mut changes = self.accounts.notice().subscribe(); + Box::pin(async move { + loop { + let now = *changes.borrow_and_update(); + if now != seen || changes.changed().await.is_err() { + return now; + } + } + }) + } + + pub(super) async fn reissue_recovery_key( + &self, + owner: &KeyAdmission, + ) -> Result { + let keys = owner.keys()?; + let client = owner.fresh().await?; + self.reissue_with(owner, &client, keys.keyset()).await + } + + async fn reissue_with( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + keyset: &Keyset, + ) -> Result { + let hub = owner.member().hub(); + let before = owner.cache()?.recovery_unconfirmed; + owner.keys()?; + owner.begin_recovery_issue().await?; + let recovery = RecoveryKey::generate().map_err(remote)?; + let written = self.reseal(owner, client, keyset, &recovery).await; + match written { + Ok(record) => { + keep_recovery_record(owner, &record).await; + Ok(Secret::new(recovery.display().as_str())) + } + Err(error) => { + if let Err(error) = owner + .update_cache(|cache| cache.recovery_unconfirmed = before) + .await + { + tracing::warn!(%error, hub = %hub, "the account cache cannot be written"); + } + Err(error) + } + } + } + + /// Writes the recovery envelope conditional on the keyset version read just before. A + /// device that wrote the keyset in between (another sign-in, a recovery) makes it conflict: + /// the record is read again and the write repeated, a few times. + async fn reseal( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + keyset: &Keyset, + recovery: &RecoveryKey, + ) -> Result { + let mut attempt = 1; + loop { + match owner + .during(client.reseal_recovery(keyset, recovery)) + .await? + { + Ok(record) => return Ok(record), + Err(KeysetError::Hub(error)) + if error.is_conflict() && attempt < RESEAL_ATTEMPTS => + { + attempt += 1; + } + Err(error) => return Err(self.keyset_failure(owner, error)), + } + } + } + + /// Opens, or creates, the keys with the vault passphrase of an account without a password. + pub(crate) async fn passphrase( + &self, + owner: &KeyAdmission, + passphrase: Secret, + ) -> Result { + let client = owner.fresh().await?; + let account = who_am_i(&client, owner).await?; + self.open_with_passphrase(owner, &client, account.id.as_str(), passphrase) + .await + } + + async fn open_with_passphrase( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + passphrase: Secret, + ) -> Result { + owner.check()?; + if owner.member().subject()?.account() != account_id { + return Err(HubError::Stale); + } + let record = match self.record(owner, client).await? { + Some(record) => record, + None => { + let kdf = UnlockKdf::random().map_err(remote)?; + let unlock = derive(&passphrase, &kdf).await?; + let method = UnlockMethod::Passphrase; + if let Some(created) = self + .try_create(owner, client, account_id, &method, &unlock, &kdf) + .await? + { + return Ok(created); + } + // Another device made the keyset first, derived with a salt of its own: the + // passphrase is tried again against the keyset that exists. + self.record(owner, client) + .await? + .ok_or_else(|| damaged(KeysetError::Incomplete))? + } + }; + // The record says which password opens it; a keyset sealed with the login password + // cannot be opened with a passphrase, whatever is typed. + let kdf = + match KeysetUnlock::of(&record).map_err(|error| self.keyset_failure(owner, error))? { + KeysetUnlock::VaultPassword(kdf) => kdf, + KeysetUnlock::LoginPassword => { + return Err(HubError::UnlockSourceMismatch { + needs_login_password: true, + }); + } + }; + let unlock = derive(&passphrase, &kdf).await?; + match Keyset::open(&record, account_id, &unlock) { + Err(KeysetError::WrongUnlockKey) => { + // Nothing says whether the passphrase was mistyped or is a new one: the + // recovery key decides, and then the typed passphrase is kept. + let fresh = UnlockKdf::random().map_err(remote)?; + let unlock = derive(&passphrase, &fresh).await?; + owner.recovering(Recovery { + account_id: account_id.to_owned(), + unlock, + kdf: fresh, + method: UnlockMethod::Passphrase, + })?; + Ok(KeyStatus::RecoveryNeeded) + } + other => { + let keyset = other.map_err(|error| self.keyset_failure(owner, error))?; + let unconfirmed = self.unconfirmed(owner)?; + self.keep(owner, client, keyset, UnlockMethod::Passphrase, unconfirmed) + .await?; + self.opened(owner) + } + } + } + + /// Opens the keys with the recovery key, and makes the password typed at sign-in (or the + /// passphrase) open them from now on. + pub(crate) async fn recover( + &self, + owner: &KeyAdmission, + recovery_key: Secret, + ) -> Result<(), HubError> { + let client = owner.fresh().await?; + self.recover_with(owner, &client, recovery_key).await + } + + async fn recover_with( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + recovery_key: Secret, + ) -> Result<(), HubError> { + let pending = owner.recovery()?; + let key = + RecoveryKey::parse(recovery_key.expose()).map_err(|_| HubError::RecoveryKeyInvalid)?; + let keyset = owner + .during(client.recover(&pending.account_id, &key, &pending.unlock, &pending.kdf)) + .await? + .map_err(|error| match error { + KeysetError::WrongRecoveryKey => HubError::RecoveryKeyInvalid, + other => self.keyset_failure(owner, other), + })?; + // The person typed the recovery key, so it is the one they hold. + self.keep(owner, client, keyset, pending.method.clone(), false) + .await + } + + async fn settle( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + method: UnlockMethod, + unlock: UnlockKey, + kdf: UnlockKdf, + ) -> Result { + let record = match self.record(owner, client).await? { + Some(record) => record, + None => { + let created = self + .try_create(owner, client, account_id, &method, &unlock, &kdf) + .await?; + if let Some(created) = created { + return Ok(created); + } + // Another device made the keyset first: this one opens it, never replaces it. + self.record(owner, client) + .await? + .ok_or_else(|| damaged(KeysetError::Incomplete))? + } + }; + if self.needs_vault_passphrase(owner, &record)? { + // The login password cannot open it, and is not a wrong password either. + return Ok(KeyStatus::PassphraseNeeded); + } + match Keyset::open(&record, account_id, &unlock) { + Ok(keyset) => { + let unconfirmed = self.unconfirmed(owner)?; + self.keep(owner, client, keyset, method, unconfirmed) + .await?; + self.opened(owner) + } + Err(KeysetError::WrongUnlockKey) => { + owner.recovering(Recovery { + account_id: account_id.to_owned(), + unlock, + kdf, + method, + })?; + Ok(KeyStatus::RecoveryNeeded) + } + Err(error) => Err(self.keyset_failure(owner, error)), + } + } + + /// Creates the keyset and opens the vault with it. `None` when another device created the + /// keyset first. + async fn try_create( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + method: &UnlockMethod, + unlock: &UnlockKey, + kdf: &UnlockKdf, + ) -> Result, HubError> { + let made = creation(owner.during(client.create(account_id, unlock, kdf)).await?) + .map_err(|error| self.keyset_failure(owner, error))?; + match made { + Creation::Created(new) => { + // Marked before the key is shown: if it is never confirmed, the next time the + // keys open another is offered. + if let Err(error) = self + .keep(owner, client, new.keyset, method.clone(), true) + .await + { + owner.remember_pending_recovery()?; + return Err(error); + } + Ok(Some(KeyStatus::Created(self.offer( + owner, + Secret::new(new.recovery.display().as_str()), + )?))) + } + Creation::Raced => Ok(None), + } + } + + /// Opens the vault with `keyset` and writes the cache from the record the hub now holds. + async fn keep( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + keyset: Keyset, + method: UnlockMethod, + unconfirmed: bool, + ) -> Result<(), HubError> { + let record = self + .record(owner, client) + .await? + .ok_or_else(|| damaged(KeysetError::Incomplete))?; + let mut cache = AccountCache::new( + client.hub_uid(), + keyset.account_id().to_owned(), + method, + &record, + ); + cache.recovery_unconfirmed = unconfirmed; + let keys = OpenKeys::new(keyset); + owner.install(keys, cache).await + } + + async fn record( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + ) -> Result, HubError> { + owner + .during(client.record()) + .await? + .map_err(|error| owner.failure(error)) + } + + fn keyset_failure(&self, owner: &KeyAdmission, error: KeysetError) -> HubError { + match error { + KeysetError::Hub(error) => owner.failure(error), + other => keyset_error(other), + } + } +} + +/// Keep the hub's accepted recovery envelope in the offline copy. The pending +/// recovery fact was persisted before resealing; a cache failure cannot undo +/// the successful remote replacement or suppress the new recovery key. +async fn keep_recovery_record(owner: &KeyAdmission, record: &v2::Keyset) { + if let Err(error) = owner + .update_cache(|cache| cache.keyset = KeysetCopy::of(record)) + .await + { + tracing::warn!(%error, hub = %owner.member().hub(), "the account cache cannot be written"); + } +} + +/// A keyset failure that is not a hub call failing. +fn keyset_error(error: KeysetError) -> HubError { + match error { + KeysetError::Damaged(_) + | KeysetError::Kdf(_) + | KeysetError::Incomplete + | KeysetError::AlreadyExists + | KeysetError::WrongRecoveryKey + | KeysetError::WrongUnlockKey + | KeysetError::UnsupportedUnlock { .. } => damaged(error), + KeysetError::WrongUnlockSource { stored, .. } => HubError::UnlockSourceMismatch { + needs_login_password: matches!(stored, nocterm_keys::UnlockSource::LoginPassword), + }, + other => remote(other), + } +} + +fn damaged(error: KeysetError) -> HubError { + HubError::KeysetDamaged { + cause: SharedCause::new(error), + } +} + +/// The unlock key of a vault passphrase. Key derivation is slow and memory hard, so it does not +/// run on a thread that serves other hub calls. +async fn derive(passphrase: &Secret, kdf: &UnlockKdf) -> Result { + let (passphrase, kdf) = (passphrase.clone(), kdf.clone()); + let derived = tokio::task::spawn_blocking(move || { + UnlockKey::from_vault_password(passphrase.expose(), &kdf) + }) + .await + .map_err(remote)?; + derived.map_err(|error| match error { + LoginError::PasswordTooShort { .. } | LoginError::EmptyUsername => { + HubError::InvalidVaultPassphrase + } + other => remote(other), + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/account/flow/tests.rs b/crates/hub-io/src/account/flow/tests.rs new file mode 100644 index 00000000..d4792b31 --- /dev/null +++ b/crates/hub-io/src/account/flow/tests.rs @@ -0,0 +1,562 @@ +use nocterm_keys::OpenError; +mod source_binding; + +use super::*; +use crate::account::Slot; + +#[test] +fn a_device_that_loses_the_race_opens_the_keyset_that_exists() { + assert!(matches!( + creation(Err(KeysetError::AlreadyExists)), + Ok(Creation::Raced) + )); +} + +#[test] +fn other_creation_failures_stay_failures() { + assert!(matches!( + creation(Err(KeysetError::Incomplete)), + Err(KeysetError::Incomplete) + )); +} + +#[test] +fn a_keyset_the_hub_altered_is_reported_as_damaged_not_overwritten() { + for error in [ + KeysetError::Damaged(OpenError::Rejected), + KeysetError::Incomplete, + KeysetError::WrongUnlockKey, + ] { + assert!(matches!( + keyset_error(error), + HubError::KeysetDamaged { .. } + )); + } +} + +pub(super) mod race { + use std::path::Path; + + use nocterm_vault::VaultStatus; + use tempfile::TempDir; + + use super::*; + use crate::account::testing::{ACCOUNT_ID, HUB_UID, MemoryKeysets, PASSWORD, hub}; + use crate::cache::KeysetCopy; + + const PASSPHRASE: &str = "a long vault passphrase"; + + /// One device of the account: its own cache directory and its own key flow. + pub(super) struct Device { + pub(super) flow: Arc, + subjects: Arc, + accounts: Arc, + dir: TempDir, + } + + impl Device { + pub(super) fn new() -> Self { + Self::in_dir(tempfile::tempdir().unwrap()) + } + + fn in_dir(dir: TempDir) -> Self { + let accounts = Arc::new(Accounts::new(dir.path().to_path_buf())); + let subjects = Arc::new(crate::account::testing::ScriptedSubjects::for_slot( + &accounts.slot(&hub()).unwrap(), + )); + Self { + flow: KeyFlow::new( + pool(dir.path(), &accounts), + Arc::clone(&accounts), + Handle::current(), + ), + subjects, + accounts, + dir, + } + } + + /// The same device after the client was closed and opened again: what it kept on disk + /// stays, what it held in memory is gone. + pub(super) fn restarted(self) -> Self { + Self::in_dir(self.dir) + } + + pub(super) fn slot(&self) -> Arc { + self.accounts.slot(&hub()).unwrap() + } + + pub(super) fn owner(&self) -> Arc { + if self.subjects.current(&hub()).is_none() { + self.subjects.sign_in(hub(), ACCOUNT_ID, "device"); + } + let provider = LocalSourceProvider::new(self.slot(), self.subjects.clone()); + KeyAdmission::scripted(provider.membership().unwrap()) + } + pub(super) async fn confirm(&self) { + let owner = self.owner(); + self.flow + .offer(&owner, Secret::new("displayed fixture key")) + .unwrap() + .confirm() + .await + .unwrap(); + } + + pub(super) async fn sign_in(&self, hub_keys: &MemoryKeysets, password: &str) -> KeyStatus { + self.subjects.sign_in(hub(), ACCOUNT_ID, "device"); + let owner = self.owner(); + let secrets = LoginSecrets::derive(password, HUB_UID, "ada").unwrap(); + self.flow + .after_password(&owner, hub_keys, ACCOUNT_ID, "ada", secrets) + .await + .unwrap() + } + + async fn with_passphrase(&self, hub_keys: &MemoryKeysets, passphrase: &str) -> KeyStatus { + let passphrase = Secret::new(passphrase); + self.flow + .open_with_passphrase(&self.owner(), hub_keys, ACCOUNT_ID, passphrase) + .await + .unwrap() + } + } + + fn pool(dir: &Path, accounts: &Arc) -> Arc { + Arc::new(Pool::new(dir.to_path_buf(), "test", Arc::clone(accounts))) + } + + #[tokio::test] + async fn remote_first_creation_with_failed_local_installation_requires_recovery_reissue_after_repair() + { + let device = Device::new(); + let hub_keys = MemoryKeysets::default(); + let owner = device.owner(); + let path = device.dir.path().join(format!( + "{}.cache", + crate::credentials::file_stem(hub().address()) + )); + std::fs::create_dir(&path).unwrap(); + let secrets = LoginSecrets::derive(PASSWORD, HUB_UID, "ada").unwrap(); + assert!(matches!( + device + .flow + .after_password(&owner, &hub_keys, ACCOUNT_ID, "ada", secrets) + .await, + Err(HubError::Credentials { .. }) + )); + assert!( + hub_keys.stored().is_some(), + "remote creation already succeeded" + ); + assert!( + device.slot().open().is_none(), + "failed durable installation cannot open keys" + ); + std::fs::remove_dir(path).unwrap(); + let secrets = LoginSecrets::derive(PASSWORD, HUB_UID, "ada").unwrap(); + assert!(matches!( + device + .flow + .after_password(&owner, &hub_keys, ACCOUNT_ID, "ada", secrets) + .await + .unwrap(), + KeyStatus::RecoveryKeyUnconfirmed(_) + )); + let keys = owner.keys().unwrap(); + let shown = device + .flow + .reissue_with(&owner, &hub_keys, keys.keyset()) + .await + .unwrap(); + device + .flow + .offer(&owner, shown) + .unwrap() + .confirm() + .await + .unwrap(); + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } + + /// Both devices read "no keyset" before either creates one. + fn racing_hub() -> MemoryKeysets { + let hub_keys = MemoryKeysets::default(); + hub_keys.meet_before_creating(2); + hub_keys + } + + #[tokio::test] + async fn recovery_acknowledgement_rechecks_its_postcommit_generation_after_parent_sync() { + use crate::account::testing::blocked_cache_job; + use futures::poll; + let device = Device::new(); + let hub_keys = MemoryKeysets::default(); + let KeyStatus::Created(shown) = device.sign_in(&hub_keys, PASSWORD).await else { + panic!("created") + }; + let slot = device.slot(); + let (job, entered, first_release) = blocked_cache_job(&slot); + let first = tokio::spawn(job); + tokio::time::timeout(std::time::Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + let (job, entered, second_release) = blocked_cache_job(&slot); + let second = tokio::spawn(job); + tokio::time::timeout(std::time::Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + let mut confirmation = shown.confirm(); + assert!(poll!(&mut confirmation).is_pending()); + let (mut job, entered, final_release) = blocked_cache_job(&slot); + assert!(poll!(&mut job).is_pending()); + let confirming = tokio::spawn(confirmation); + let final_job = tokio::spawn(job); + first_release.release(); + tokio::time::timeout(std::time::Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + tokio::time::timeout(std::time::Duration::from_secs(5), async { + while slot.recovery_unconfirmed(ACCOUNT_ID) { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + slot.lock(); + second_release.release(); + final_release.release(); + first.await.unwrap().unwrap(); + second.await.unwrap().unwrap(); + final_job.await.unwrap().unwrap(); + assert!(matches!(confirming.await.unwrap(), Err(HubError::Stale))); + } + + fn count(statuses: [&KeyStatus; 2], wanted: impl Fn(&KeyStatus) -> bool) -> usize { + statuses.into_iter().filter(|status| wanted(status)).count() + } + + fn is_created(status: &KeyStatus) -> bool { + matches!(status, KeyStatus::Created(_)) + } + + #[tokio::test] + async fn two_devices_creating_the_keyset_at_once_end_with_one_keyset_and_one_recovery_key() { + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + + let (a, b) = tokio::join!( + first.sign_in(&hub_keys, PASSWORD), + second.sign_in(&hub_keys, PASSWORD) + ); + + assert_eq!(hub_keys.creations(), 2, "both devices tried to create it"); + assert_eq!(count([&a, &b], is_created), 1, "{a:?} {b:?}"); + assert_eq!(count([&a, &b], |s| *s == KeyStatus::Open), 1, "{a:?} {b:?}"); + let stored = hub_keys.stored().unwrap(); + assert_eq!(stored.version, 1, "the loser did not write over the keyset"); + for device in [&first, &second] { + let slot = device.slot(); + assert_eq!(slot.status(), VaultStatus::Unlocked); + let cached = slot.read_cache(|cache| cache.map(|cache| cache.keyset.clone())); + assert_eq!( + cached, + Some(KeysetCopy::of(&stored)), + "both keep the winner's" + ); + } + } + + #[tokio::test] + async fn the_device_that_lost_the_race_holds_the_keys_of_the_one_that_won() { + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + tokio::join!( + first.sign_in(&hub_keys, PASSWORD), + second.sign_in(&hub_keys, PASSWORD) + ); + + let (_, envelope) = first + .slot() + .open() + .unwrap() + .new_collection("shared") + .unwrap(); + let opened = second + .slot() + .open() + .unwrap() + .own_collection("shared", &envelope); + assert!( + opened.is_ok(), + "one account key opens what the other sealed" + ); + } + + #[tokio::test] + async fn a_vault_passphrase_that_loses_the_race_opens_the_keyset_that_won() { + // Each device chose a salt of its own before it knew of the other: the loser must + // derive the key again with the salt of the keyset that exists. + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + + let (a, b) = tokio::join!( + first.with_passphrase(&hub_keys, PASSPHRASE), + second.with_passphrase(&hub_keys, PASSPHRASE) + ); + + assert_eq!(count([&a, &b], is_created), 1, "{a:?} {b:?}"); + assert_eq!(count([&a, &b], |s| *s == KeyStatus::Open), 1, "{a:?} {b:?}"); + for device in [&first, &second] { + assert_eq!(device.slot().status(), VaultStatus::Unlocked); + } + } + + #[tokio::test] + async fn a_keyset_sealed_with_the_login_password_is_not_opened_with_a_passphrase() { + let hub_keys = MemoryKeysets::default(); + let (first, second) = (Device::new(), Device::new()); + assert!(is_created(&first.sign_in(&hub_keys, PASSWORD).await)); + + let refused = second + .flow + .open_with_passphrase( + &second.owner(), + &hub_keys, + ACCOUNT_ID, + Secret::new(PASSPHRASE), + ) + .await; + + assert!(matches!( + refused, + Err(HubError::UnlockSourceMismatch { + needs_login_password: true + }) + )); + assert!(second.slot().open().is_none()); + assert!( + second.slot().recovery().is_none(), + "a wrong source is not a wrong password: no recovery key is asked for" + ); + } + + #[tokio::test] + async fn a_keyset_sealed_with_a_vault_password_asks_for_it_instead_of_the_login_password() { + let hub_keys = MemoryKeysets::default(); + let (first, second) = (Device::new(), Device::new()); + first.with_passphrase(&hub_keys, PASSPHRASE).await; + + let status = second.sign_in(&hub_keys, PASSWORD).await; + + assert_eq!(status, KeyStatus::PassphraseNeeded); + assert!(second.slot().open().is_none()); + assert!(second.slot().recovery().is_none()); + assert_eq!( + second.with_passphrase(&hub_keys, PASSPHRASE).await, + KeyStatus::Open + ); + } + + #[tokio::test] + async fn a_device_with_another_password_loses_the_race_and_waits_for_the_recovery_key() { + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + + let (a, b) = tokio::join!( + first.sign_in(&hub_keys, PASSWORD), + second.sign_in(&hub_keys, "another long password") + ); + + let (winner, loser, created, waiting) = match (&a, &b) { + (KeyStatus::Created(key), KeyStatus::RecoveryNeeded) => (&first, &second, key, &b), + (KeyStatus::RecoveryNeeded, KeyStatus::Created(key)) => (&second, &first, key, &a), + other => panic!("one creates, the other waits for the recovery key: {other:?}"), + }; + assert_eq!(*waiting, KeyStatus::RecoveryNeeded); + assert_eq!(winner.slot().status(), VaultStatus::Unlocked); + assert!( + loser.slot().open().is_none(), + "nothing opens without the key" + ); + + // The recovery key made by the winner opens the loser's keys. + let recovered = loser + .flow + .recover_with(&loser.owner(), &hub_keys, created.key().clone()) + .await; + assert!(recovered.is_ok()); + assert_eq!(loser.slot().status(), VaultStatus::Unlocked); + } +} + +mod reissue { + use nocterm_keys::{RecoveryKey, UnlockKey}; + + use super::race::Device; + use super::*; + use crate::account::testing::{ACCOUNT_ID, HUB_UID, MemoryKeysets, PASSWORD, hub}; + + /// A device that created the account's keys, and the recovery key it was shown. + async fn created() -> (Device, MemoryKeysets, RecoveryKey) { + let (device, hub_keys) = (Device::new(), MemoryKeysets::default()); + let KeyStatus::Created(shown) = device.sign_in(&hub_keys, PASSWORD).await else { + panic!("a first sign-in creates the keys"); + }; + let key = RecoveryKey::parse(shown.key().expose()).unwrap(); + (device, hub_keys, key) + } + + async fn reissue(device: &Device, hub_keys: &MemoryKeysets) -> Result { + let slot = device.slot(); + let keys = slot.open().ok_or(HubError::KeysLocked)?; + let shown = device + .flow + .reissue_with(&device.owner(), hub_keys, keys.keyset()) + .await?; + Ok(RecoveryKey::parse(shown.expose()).unwrap()) + } + + fn opens_with(hub_keys: &MemoryKeysets, key: &RecoveryKey) -> bool { + Keyset::recover(&hub_keys.stored().unwrap(), ACCOUNT_ID, key).is_ok() + } + + fn password_opens(hub_keys: &MemoryKeysets) -> bool { + let secrets = LoginSecrets::derive(PASSWORD, HUB_UID, "ada").unwrap(); + let unlock: UnlockKey = secrets.into_unlock(); + Keyset::open(&hub_keys.stored().unwrap(), ACCOUNT_ID, &unlock).is_ok() + } + + #[tokio::test] + async fn a_recovery_key_is_unconfirmed_from_its_creation_until_the_person_confirms_it() { + let (device, ..) = created().await; + assert!(device.flow.recovery_key_unconfirmed(&hub())); + + device.confirm().await; + + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } + + #[tokio::test] + async fn an_unconfirmed_key_is_offered_again_when_the_keys_open_after_a_restart() { + let (device, hub_keys, _) = created().await; + + let device = device.restarted(); + assert!( + !device.flow.recovery_key_unconfirmed(&hub()), + "nothing is open before the password opens the keys" + ); + let status = device.sign_in(&hub_keys, PASSWORD).await; + assert!(matches!(status, KeyStatus::RecoveryKeyUnconfirmed(_))); + assert!(device.flow.recovery_key_unconfirmed(&hub())); + + device.confirm().await; + let device = device.restarted(); + assert_eq!(device.sign_in(&hub_keys, PASSWORD).await, KeyStatus::Open); + } + + #[tokio::test] + async fn another_device_of_the_account_does_not_inherit_the_marker() { + let (_first, hub_keys, _) = created().await; + + let second = Device::new(); + + assert_eq!(second.sign_in(&hub_keys, PASSWORD).await, KeyStatus::Open); + } + + #[tokio::test] + async fn reissuing_replaces_the_recovery_key_the_hub_can_open_and_keeps_the_password() { + let (device, hub_keys, old) = created().await; + device.confirm().await; + let version = hub_keys.stored().unwrap().version; + + let new = reissue(&device, &hub_keys).await.unwrap(); + + assert!(opens_with(&hub_keys, &new)); + assert!(!opens_with(&hub_keys, &old), "the old key no longer opens"); + assert!(password_opens(&hub_keys), "the password still opens it"); + let stored = hub_keys.stored().unwrap(); + assert_eq!(stored.version, version + 1); + assert_eq!( + device + .slot() + .read_cache(|cache| cache.unwrap().keyset.clone()), + KeysetCopy::of(&stored), + "the cache holds the record the hub now has" + ); + } + + #[tokio::test] + async fn a_reissued_key_is_unconfirmed_until_confirmed_and_survives_a_restart() { + let (device, hub_keys, _) = created().await; + device.confirm().await; + + reissue(&device, &hub_keys).await.unwrap(); + assert!(device.flow.recovery_key_unconfirmed(&hub())); + + let device = device.restarted(); + assert!(matches!( + device.sign_in(&hub_keys, PASSWORD).await, + KeyStatus::RecoveryKeyUnconfirmed(_) + )); + } + + #[tokio::test] + async fn a_write_that_loses_to_another_device_is_repeated_against_the_new_version() { + let (device, hub_keys, _) = created().await; + let version = hub_keys.stored().unwrap().version; + hub_keys.contend_recovery_writes(2); + + let new = reissue(&device, &hub_keys).await.unwrap(); + + assert!(opens_with(&hub_keys, &new)); + assert_eq!( + hub_keys.stored().unwrap().version, + version + 3, + "two writes of the other device and then ours" + ); + } + + #[tokio::test] + async fn a_keyset_that_keeps_changing_leaves_the_old_key_valid_and_the_marker_as_it_was() { + let (device, hub_keys, old) = created().await; + device.confirm().await; + hub_keys.contend_recovery_writes(100); + + let result = reissue(&device, &hub_keys).await; + + assert!(result.is_err()); + assert!(opens_with(&hub_keys, &old), "nothing was replaced"); + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } + + #[tokio::test] + async fn reissuing_with_closed_keys_is_refused() { + let (device, ..) = created().await; + let device = device.restarted(); + + let result = device.flow.reissue_recovery_key(&device.owner()).await; + + assert!(matches!(result, Err(HubError::KeysLocked))); + } + + #[tokio::test] + async fn typing_the_recovery_key_confirms_that_the_person_holds_it() { + let (device, hub_keys, key) = created().await; + let device = device.restarted(); + let other = device.sign_in(&hub_keys, "another long password").await; + assert_eq!(other, KeyStatus::RecoveryNeeded); + + device + .flow + .recover_with( + &device.owner(), + &hub_keys, + Secret::new(key.display().as_str()), + ) + .await + .unwrap(); + + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } +} diff --git a/crates/hub-io/src/account/flow/tests/source_binding.rs b/crates/hub-io/src/account/flow/tests/source_binding.rs new file mode 100644 index 00000000..62f736fc --- /dev/null +++ b/crates/hub-io/src/account/flow/tests/source_binding.rs @@ -0,0 +1,78 @@ +use super::{race::Device, *}; +use crate::account::{ + key_admission::KeyAdmission, + source::LocalSourceProvider, + testing::{ACCOUNT_ID, MemoryKeysets, NativeAccounts, PASSWORD}, +}; +use nocterm_hubs_model::{Account, AccountId}; + +#[tokio::test] +async fn an_old_displayed_recovery_key_cannot_confirm_a_reissued_key() { + let device = Device::new(); + let records = MemoryKeysets::default(); + let KeyStatus::Created(first) = device.sign_in(&records, PASSWORD).await else { + panic!("created") + }; + let owner = device.owner(); + let keys = owner.keys().unwrap(); + let key = device + .flow + .reissue_with(&owner, &records, keys.keyset()) + .await + .unwrap(); + let second = device.flow.offer(&owner, key).unwrap(); + assert!(matches!(first.confirm().await, Err(HubError::Stale))); + assert!( + device + .flow + .recovery_key_unconfirmed(&crate::account::testing::hub()) + ); + second.confirm().await.unwrap(); + assert!( + !device + .flow + .recovery_key_unconfirmed(&crate::account::testing::hub()) + ); + assert!(matches!(second.confirm().await, Err(HubError::Stale))); +} + +#[tokio::test] +async fn same_account_sign_in_never_revives_an_earlier_recovery_card() { + let device = Device::new(); + let records = MemoryKeysets::default(); + let KeyStatus::Created(first) = device.sign_in(&records, PASSWORD).await else { + panic!("created") + }; + device.sign_in(&records, PASSWORD).await; + assert!(matches!(first.confirm().await, Err(HubError::Stale))); + assert!( + device + .flow + .recovery_key_unconfirmed(&crate::account::testing::hub()) + ); +} + +#[tokio::test] +async fn a_display_account_never_replaces_the_authenticated_native_recipient() { + let fixture = NativeAccounts::new(); + let provider = LocalSourceProvider::new(fixture.slot().clone(), fixture.subjects().clone()); + let owner = KeyAdmission::scripted(provider.membership().unwrap()); + let account = |id: &str| Account { + id: AccountId::new(id), + username: "display".into(), + display_name: "Display".into(), + admin: false, + }; + assert!(crate::directory::checked_account(&owner, account(ACCOUNT_ID)).is_ok()); + assert!(matches!( + crate::directory::checked_account(&owner, account("account-2")), + Err(HubError::Stale) + )); + fixture.login(1); + fixture.login(0); + assert!(matches!( + crate::directory::checked_account(&owner, account(ACCOUNT_ID)), + Err(HubError::Stale) + )); + assert!(fixture.source().check_current().is_ok()); +} diff --git a/crates/hub-io/src/account/key_admission.rs b/crates/hub-io/src/account/key_admission.rs new file mode 100644 index 00000000..974558fd --- /dev/null +++ b/crates/hub-io/src/account/key_admission.rs @@ -0,0 +1,270 @@ +//! One bounded key operation; display capabilities keep immutable snapshots, not this owner. +use super::{OpenKeys, Recovery, SlotReceipt, source::CapturedMembership}; +use crate::{cache::AccountCache, pool::captured::CapturedClient}; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hubs_model::HubError; +use std::sync::{Arc, Mutex, PoisonError}; + +#[derive(Clone)] +enum Requester { + Native(Arc), + #[cfg(test)] + Scripted, +} + +/// Unlike a running operation, an offer's immutable snapshot never follows publication. +#[derive(Clone)] +pub(crate) struct KeySnapshot { + member: Arc, + receipt: SlotReceipt, +} +impl KeySnapshot { + pub(crate) fn check(&self) -> Result<(), HubError> { + self.member.with_current(|| { + self.member + .slot()? + .check_receipt(&self.member.subject()?, &self.receipt) + }) + } + pub(crate) fn member(&self) -> &Arc { + &self.member + } + pub(crate) async fn confirm(&self) -> Result<(), HubError> { + let slot = self.member.slot()?; + let _turn = slot.cache_turn().await; + loop { + self.check()?; + let original = slot.cache_snapshot(); + let mut next = original.as_deref().cloned().ok_or(HubError::NotFound)?; + next.recovery_unconfirmed = false; + let draft = slot.stage_cache(original, next).await?; + let committed = self.member.with_current(|| { + slot.commit_member_cache(&self.member.subject()?, &self.receipt, draft, true) + })?; + if let Some((receipt, committed)) = committed { + slot.finish_cache(committed).await; + self.member + .with_current(|| slot.check_receipt(&self.member.subject()?, &receipt))?; + slot.notice().raise(); + return Ok(()); + } + } + } +} + +pub(crate) struct KeyAdmission { + member: Arc, + receipt: Mutex, + requester: Requester, +} +impl KeyAdmission { + pub(crate) fn native( + member: Arc, + client: Arc, + ) -> Result, HubError> { + let receipt = member.with_current(|| member.slot()?.receipt(&member.subject()?))?; + let admission = Arc::new(Self { + member, + receipt: Mutex::new(receipt), + requester: Requester::Native(client), + }); + admission.check()?; + Ok(admission) + } + pub(crate) fn bound( + snapshot: KeySnapshot, + client: Arc, + ) -> Result, HubError> { + snapshot.check()?; + let admission = Arc::new(Self { + member: snapshot.member, + receipt: Mutex::new(snapshot.receipt), + requester: Requester::Native(client), + }); + admission.check()?; + Ok(admission) + } + #[cfg(test)] + pub(crate) fn scripted(member: Arc) -> Arc { + let receipt = member + .with_current(|| member.slot()?.receipt(&member.subject()?)) + .unwrap(); + Arc::new(Self { + member, + receipt: Mutex::new(receipt), + requester: Requester::Scripted, + }) + } + fn receipt(&self) -> SlotReceipt { + self.receipt + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } + fn replaced(&self, receipt: SlotReceipt) { + *self.receipt.lock().unwrap_or_else(PoisonError::into_inner) = receipt; + } + pub(crate) fn snapshot(&self) -> Result { + let snapshot = KeySnapshot { + member: self.member.clone(), + receipt: self.receipt(), + }; + snapshot.check()?; + Ok(snapshot) + } + pub(crate) fn member(&self) -> &Arc { + &self.member + } + pub(crate) fn check(&self) -> Result<(), HubError> { + match &self.requester { + Requester::Native(client) => client.check()?, + #[cfg(test)] + Requester::Scripted => {} + } + self.snapshot().map(|_| ()) + } + pub(crate) fn cache(&self) -> Result { + let receipt = self.receipt(); + self.member.with_current(|| { + self.member + .slot()? + .cache_for(&self.member.subject()?, &receipt) + }) + } + pub(crate) fn recovery_unconfirmed(&self) -> Result { + let receipt = self.receipt(); + self.member.with_current(|| { + self.member + .slot()? + .pending_recovery_for(&self.member.subject()?, &receipt) + }) + } + + pub(crate) fn remember_pending_recovery(&self) -> Result<(), HubError> { + let receipt = self.receipt(); + let next = self.member.with_current(|| { + self.member + .slot()? + .remember_pending_recovery(&self.member.subject()?, &receipt) + })?; + self.replaced(next); + Ok(()) + } + pub(crate) fn keys(&self) -> Result, HubError> { + self.check()?; + let receipt = self.receipt(); + self.member.with_current(|| { + let slot = self.member.slot()?; + let subject = self.member.subject()?; + slot.check_receipt(&subject, &receipt)?; + slot.open_for(&subject) + }) + } + pub(crate) async fn install( + &self, + keys: OpenKeys, + mut cache: AccountCache, + ) -> Result<(), HubError> { + let slot = self.member.slot()?; + let _turn = slot.cache_turn().await; + let receipt = self.receipt(); + self.check()?; + let original = slot.cache_snapshot(); + if let Some(earlier) = original + .as_deref() + .filter(|old| old.account_id == cache.account_id) + { + cache.vault = earlier.vault.clone(); + } + let keys = Arc::new(keys); + let draft = slot.stage_cache(original, cache).await?; + let committed = self + .member + .with_current(|| slot.commit_install(&self.member.subject()?, &receipt, keys, draft))? + .ok_or(HubError::Stale)?; + self.replaced(committed.0); + slot.finish_cache(committed.1).await; + slot.notice().raise(); + Ok(()) + } + pub(crate) fn recovering(&self, recovery: Recovery) -> Result<(), HubError> { + let receipt = self.receipt(); + let next = self.member.with_current(|| { + self.member + .slot()? + .recovering_if(&self.member.subject()?, &receipt, recovery) + })?; + self.replaced(next); + Ok(()) + } + pub(crate) fn recovery(&self) -> Result, HubError> { + let receipt = self.receipt(); + self.member.with_current(|| { + let slot = self.member.slot()?; + slot.check_receipt(&self.member.subject()?, &receipt)?; + slot.recovery().ok_or(HubError::NoRecoveryPending) + }) + } + pub(crate) async fn update_cache( + &self, + change: impl Fn(&mut AccountCache), + ) -> Result<(), HubError> { + self.change_cache(change, false).await + } + + async fn change_cache( + &self, + change: impl Fn(&mut AccountCache), + rotate: bool, + ) -> Result<(), HubError> { + let slot = self.member.slot()?; + let _turn = slot.cache_turn().await; + let receipt = self.receipt(); + loop { + self.check()?; + let original = slot.cache_snapshot(); + let mut next = original.as_deref().cloned().ok_or(HubError::NotFound)?; + change(&mut next); + let draft = slot.stage_cache(original, next).await?; + let committed = self.member.with_current(|| { + slot.commit_member_cache(&self.member.subject()?, &receipt, draft, rotate) + })?; + if let Some((receipt, committed)) = committed { + self.replaced(receipt); + slot.finish_cache(committed).await; + if rotate { + slot.notice().raise(); + } + return Ok(()); + } + } + } + pub(crate) async fn begin_recovery_issue(&self) -> Result<(), HubError> { + self.change_cache(|cache| cache.recovery_unconfirmed = true, true) + .await + } + pub(crate) async fn fresh(&self) -> Result { + self.check()?; + let client = match &self.requester { + Requester::Native(client) => client.fresh().await?, + #[cfg(test)] + Requester::Scripted => return Err(HubError::NotSignedIn), + }; + self.check()?; + Ok(client) + } + pub(crate) fn failure(&self, error: ClientError) -> HubError { + match &self.requester { + Requester::Native(client) => client.failure(error), + #[cfg(test)] + Requester::Scripted => crate::classify::classify(self.member.hub().address(), error), + } + } + + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} diff --git a/crates/hub-io/src/account/keyset_hub.rs b/crates/hub-io/src/account/keyset_hub.rs new file mode 100644 index 00000000..38f01585 --- /dev/null +++ b/crates/hub-io/src/account/keyset_hub.rs @@ -0,0 +1,119 @@ +//! The keyset calls of a sign-in, as one narrow port to a hub. +//! +//! [`KeyFlow`](super::KeyFlow) decides what to do with the account's keys; this port is only +//! what it asks of the hub. [`HubClient`] implements it over the contract; tests implement it +//! over memory, so a race between two devices needs no network. + +use futures::future::BoxFuture; +use nocterm_hub_client::{ClientError, HubClient, Keyset, KeysetError, NewKeyset}; +use nocterm_hub_proto::v2; +use nocterm_keys::{RecoveryKey, UnlockKdf, UnlockKey}; + +/// What a key flow asks of one hub. +pub(crate) trait KeysetHub: Send + Sync { + /// The hub's own identifier, which the login key is derived with. + fn hub_uid(&self) -> String; + + /// The account's keyset record; `None` before a device created it. + fn record(&self) -> BoxFuture<'_, Result, ClientError>>; + + /// Creates the keyset, conditional on there being none: a device that lost the race gets + /// [`KeysetError::AlreadyExists`]. + fn create<'a>( + &'a self, + account_id: &'a str, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result>; + + /// Opens the keyset with the recovery key and seals the account key anew under `unlock`. + fn recover<'a>( + &'a self, + account_id: &'a str, + recovery: &'a RecoveryKey, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result>; + + /// Seals the account key of `keyset`, which is open, under `recovery` in place of the + /// recovery envelope the hub holds. One write conditional on the version just read: a + /// version that moved in between is a conflict ([`ClientError::is_conflict`]) the caller + /// retries. Returns the record the hub now holds. + fn reseal_recovery<'a>( + &'a self, + keyset: &'a Keyset, + recovery: &'a RecoveryKey, + ) -> BoxFuture<'a, Result>; +} + +/// The write that replaces the recovery envelope of `record` and nothing else: the other +/// values are the hub's own, and the write is conditional on the version just read. +pub(crate) fn resealed( + record: &v2::Keyset, + keyset: &Keyset, + recovery: &RecoveryKey, +) -> Result { + Ok(v2::PutKeysetRequest { + expected_version: record.version, + kdf: record.kdf.clone(), + salt: record.salt.clone(), + wrapped_account_key: record.wrapped_account_key.clone(), + recovery_wrapped_account_key: recovery.wrap(keyset.account_key(), keyset.account_id())?, + unlock_kind: record.unlock_kind, + }) +} + +impl KeysetHub for HubClient { + fn hub_uid(&self) -> String { + self.info().hub_id.clone() + } + + fn record(&self) -> BoxFuture<'_, Result, ClientError>> { + Box::pin(self.keyset_record()) + } + + fn create<'a>( + &'a self, + account_id: &'a str, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(self.create_keyset(account_id, unlock, kdf)) + } + + fn recover<'a>( + &'a self, + account_id: &'a str, + recovery: &'a RecoveryKey, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(self.recover_keyset(account_id, recovery, unlock, kdf)) + } + + fn reseal_recovery<'a>( + &'a self, + keyset: &'a Keyset, + recovery: &'a RecoveryKey, + ) -> BoxFuture<'a, Result> { + Box::pin(reseal(self, keyset, recovery)) + } +} + +async fn reseal( + client: &HubClient, + keyset: &Keyset, + recovery: &RecoveryKey, +) -> Result { + let record = client + .keyset_record() + .await? + .ok_or(KeysetError::Incomplete)?; + let request = resealed(&record, keyset, recovery)?; + let stored = client + .vault() + .put_keyset(request) + .await + .map_err(ClientError::from)?; + Ok(stored.into_inner()) +} diff --git a/crates/hub-io/src/account/lifecycle.rs b/crates/hub-io/src/account/lifecycle.rs new file mode 100644 index 00000000..5e7da847 --- /dev/null +++ b/crates/hub-io/src/account/lifecycle.rs @@ -0,0 +1,76 @@ +//! Nonpersistent native membership and key-lifecycle publication receipts. +use super::source::AccountSubject; +use nocterm_hubs_model::HubError; +use std::sync::{Arc, Weak}; + +#[derive(Clone)] +pub(crate) struct SlotReceipt(Arc<()>); + +impl SlotReceipt { + fn fresh() -> Self { + Self(Arc::new(())) + } + fn matches(&self, expected: &Self) -> bool { + Arc::ptr_eq(&self.0, &expected.0) + } +} + +pub(super) struct SlotAuthority { + owner: Option>, + receipt: SlotReceipt, +} + +impl SlotAuthority { + pub(super) fn new() -> Self { + Self { + owner: None, + receipt: SlotReceipt::fresh(), + } + } + pub(super) fn begin(&mut self, owner: &Arc) { + self.owner = Some(Arc::downgrade(owner)); + self.rotate(); + } + pub(super) fn rotate(&mut self) -> SlotReceipt { + self.receipt = SlotReceipt::fresh(); + self.receipt.clone() + } + pub(super) fn receipt(&self, owner: &Arc) -> Result { + self.check_owner(owner)?; + Ok(self.receipt.clone()) + } + pub(super) fn observed(&self) -> SlotReceipt { + self.receipt.clone() + } + pub(super) fn check_observed(&self, receipt: &SlotReceipt) -> Result<(), HubError> { + if self.receipt.matches(receipt) { + Ok(()) + } else { + Err(HubError::Stale) + } + } + pub(super) fn check_owner(&self, expected: &Arc) -> Result<(), HubError> { + if self + .owner + .as_ref() + .and_then(Weak::upgrade) + .is_some_and(|current| Arc::ptr_eq(¤t, expected)) + { + Ok(()) + } else { + Err(HubError::Stale) + } + } + pub(super) fn check( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result<(), HubError> { + self.check_owner(owner)?; + if self.receipt.matches(receipt) { + Ok(()) + } else { + Err(HubError::Stale) + } + } +} diff --git a/crates/hub-io/src/account/operations.rs b/crates/hub-io/src/account/operations.rs new file mode 100644 index 00000000..dbd02d14 --- /dev/null +++ b/crates/hub-io/src/account/operations.rs @@ -0,0 +1,228 @@ +//! Shared original-account capture for native vault, replica and credential workers. +use super::{ + OpenKeys, Slot, SlotReceipt, + source::{CapturedAccount, CapturedMembership, LocalSourceProvider}, +}; +use crate::{ + pool::Pool, + store::{HubStore, StoreOps}, + vault::Ledger, +}; +use nocterm_hubs_model::{ + HubError, + source_identity::{LocalAccountPartition, LocalAccountSource, LocalAccountSourceError}, +}; +use std::sync::{Arc, Weak}; + +/// The single shared store selector. Production uses the original SDK client; +/// MemoryHub implements this seam in the existing native test-support owner. +pub(crate) trait OperationStores: Send + Sync { + fn opened( + &self, + native: &CapturedAccount, + source: Arc, + ) -> Result, HubError>; + fn membership(&self, native: &CapturedMembership) -> Result, HubError>; +} + +struct PooledStores(Weak); +impl OperationStores for PooledStores { + fn opened( + &self, + native: &CapturedAccount, + source: Arc, + ) -> Result, HubError> { + let pool = self.0.upgrade().ok_or(HubError::Stale)?; + Ok(Arc::new(HubStore::captured(pool, native, source)?)) + } + fn membership(&self, native: &CapturedMembership) -> Result, HubError> { + let pool = self.0.upgrade().ok_or(HubError::Stale)?; + Ok(Arc::new(HubStore::membership(pool, native)?)) + } +} + +/// A caller-provided lease only restricts the independently authenticated capture. +pub(crate) struct OpenedSource { + native: Arc, + restriction: Arc, +} +impl OpenedSource { + pub(crate) fn new( + native: Arc, + restriction: Arc, + ) -> Result, HubError> { + if native.partition() != restriction.partition() { + return Err(HubError::Stale); + } + let source = Arc::new(Self { + native, + restriction, + }); + source.check_current().map_err(|_| HubError::Stale)?; + Ok(source) + } + pub(crate) fn native(&self) -> &Arc { + &self.native + } + pub(crate) fn check(&self) -> Result<(), HubError> { + self.check_current().map_err(|_| HubError::Stale) + } + pub(crate) fn publish(&self, publish: impl FnOnce() -> T) -> Result { + self.restriction + .check_current() + .map_err(|_| HubError::Stale)?; + self.native.publish(publish) + } + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} +impl LocalAccountSource for OpenedSource { + fn partition(&self) -> LocalAccountPartition { + self.native.partition() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + self.native.check_current()?; + self.restriction.check_current() + } +} + +/// A bound facade retains weak key authority; only admitted workers hold keys. +#[derive(Clone)] +pub(crate) struct BoundOperations { + source: Arc, + stores: Arc, +} +impl BoundOperations { + pub(crate) fn source(&self) -> &Arc { + &self.source + } + pub(crate) fn capture(&self) -> Result { + self.source.check()?; + let keys = self.source.native.keys()?; + let ops = self + .stores + .opened(&self.source.native, self.source.clone())?; + self.source.check()?; + Ok(OpenedWork { + source: self.source.clone(), + keys, + ops, + }) + } +} + +pub(crate) struct OpenedWork { + source: Arc, + keys: Arc, + ops: Arc, +} +impl OpenedWork { + pub(crate) fn source(&self) -> &Arc { + &self.source + } + pub(crate) fn store(&self) -> crate::store::Store { + crate::store::Store::new(self.ops.clone(), self.keys.clone()) + } + pub(crate) fn ledger(&self) -> Result { + self.source.check()?; + Ok(Ledger::captured( + self.source.native.slot()?, + self.ops.clone(), + self.source.native.clone(), + )) + } + pub(crate) fn ops(&self) -> &Arc { + &self.ops + } +} + +pub(crate) struct MembershipWork { + membership: Arc, + receipt: SlotReceipt, + ops: Arc, +} +impl MembershipWork { + pub(crate) fn membership(&self) -> &Arc { + &self.membership + } + pub(crate) fn receipt(&self) -> &SlotReceipt { + &self.receipt + } + pub(crate) fn ops(&self) -> &Arc { + &self.ops + } + pub(crate) fn slot(&self) -> Result, HubError> { + self.membership.slot() + } + pub(crate) fn check(&self) -> Result<(), HubError> { + self.membership.with_current(|| { + self.slot()? + .check_receipt(&self.membership.subject()?, &self.receipt) + }) + } +} + +pub(crate) struct AccountOperations { + provider: LocalSourceProvider, + stores: Arc, +} +impl AccountOperations { + pub(crate) fn current_opened_source(&self) -> Result, HubError> { + self.provider.capture_native() + } + pub(crate) fn current_membership(&self) -> Result, HubError> { + self.provider.membership() + } + #[cfg(test)] + pub(crate) fn with_stores( + provider: LocalSourceProvider, + stores: Arc, + ) -> Self { + Self { provider, stores } + } + pub(crate) fn new(provider: LocalSourceProvider, pool: &Arc) -> Self { + Self { + provider, + stores: Arc::new(PooledStores(Arc::downgrade(pool))), + } + } + pub(crate) fn stores(&self) -> &Arc { + &self.stores + } + pub(crate) fn bind( + &self, + restriction: Arc, + ) -> Result { + let source = OpenedSource::new(self.provider.capture_native()?, restriction)?; + Ok(BoundOperations { + source, + stores: self.stores.clone(), + }) + } + pub(crate) fn opened(&self) -> Result { + let native = self.provider.capture_native()?; + let source = OpenedSource::new(native.clone(), native)?; + BoundOperations { + source, + stores: self.stores.clone(), + } + .capture() + } + pub(crate) fn membership(&self) -> Result { + let membership = self.provider.membership()?; + let receipt = + membership.with_current(|| membership.slot()?.receipt(&membership.subject()?))?; + let ops = self.stores.membership(&membership)?; + let work = MembershipWork { + membership, + receipt, + ops, + }; + work.check()?; + Ok(work) + } +} diff --git a/crates/hub-io/src/account/recovery.rs b/crates/hub-io/src/account/recovery.rs new file mode 100644 index 00000000..58741ec7 --- /dev/null +++ b/crates/hub-io/src/account/recovery.rs @@ -0,0 +1,54 @@ +//! Display capabilities retain immutable original recipient/action snapshots. +use super::{KeyFlow, key_admission::KeySnapshot}; +use nocterm_hubs_model::{ + HubError, HubFuture, RecoveryKeyConfirmation, RecoveryKeyIssuer, RecoveryKeyOffer, + RecoveryKeyReissue, +}; +use nocterm_session::Secret; +use std::sync::{Arc, Weak}; + +struct Acknowledgement(KeySnapshot); +impl RecoveryKeyConfirmation for Acknowledgement { + fn check_current(&self) -> Result<(), HubError> { + self.0.check() + } + fn confirm(&self) -> HubFuture<()> { + let snapshot = self.0.clone(); + Box::pin(async move { snapshot.confirm().await }) + } +} +pub(super) fn offer(secret: Secret, snapshot: KeySnapshot) -> RecoveryKeyOffer { + RecoveryKeyOffer::new( + snapshot.member().hub().clone(), + secret, + Arc::new(Acknowledgement(snapshot)), + ) +} + +struct Issuer { + flow: Weak, + snapshot: KeySnapshot, +} +impl RecoveryKeyIssuer for Issuer { + fn check_current(&self) -> Result<(), HubError> { + self.snapshot.check()?; + self.flow.upgrade().map(|_| ()).ok_or(HubError::Stale) + } + fn issue(&self) -> HubFuture { + let Some(flow) = self.flow.upgrade() else { + return Box::pin(async { Err(HubError::Stale) }); + }; + let captured = flow.bind_snapshot(self.snapshot.clone()); + let worker = flow.clone(); + flow.run(captured, move |owner| async move { + let shown = worker.reissue_recovery_key(&owner).await?; + worker.offer(&owner, shown) + }) + } +} +pub(super) fn issuer(flow: Weak, snapshot: KeySnapshot) -> RecoveryKeyReissue { + RecoveryKeyReissue::new( + snapshot.member().hub().clone(), + Arc::new(Issuer { flow, snapshot }), + ) +} diff --git a/crates/hub-io/src/account/refresh.rs b/crates/hub-io/src/account/refresh.rs new file mode 100644 index 00000000..86910bce --- /dev/null +++ b/crates/hub-io/src/account/refresh.rs @@ -0,0 +1,93 @@ +//! Only a successful concurrent personal refresh satisfies waiting readers. +use super::OpenKeys; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, PoisonError, Weak}; + +#[derive(Default)] +pub(super) struct Refresh { + serial: tokio::sync::Mutex<()>, + completed: AtomicU64, + completed_keys: Mutex>, +} + +pub(crate) struct RefreshTurn<'a> { + owner: &'a Refresh, + _guard: tokio::sync::MutexGuard<'a, ()>, + needed: bool, + keys: Weak, +} + +impl Refresh { + pub(super) async fn enter(&self, keys: &Arc) -> RefreshTurn<'_> { + let before = self.completed.load(Ordering::Acquire); + let guard = self.serial.lock().await; + let same_keys = self + .completed_keys + .lock() + .unwrap_or_else(PoisonError::into_inner) + .upgrade() + .is_some_and(|completed| Arc::ptr_eq(&completed, keys)); + RefreshTurn { + owner: self, + _guard: guard, + needed: !same_keys || before == self.completed.load(Ordering::Acquire), + keys: Arc::downgrade(keys), + } + } +} + +impl RefreshTurn<'_> { + pub(crate) fn needed(&self) -> bool { + self.needed + } + + pub(crate) fn complete(self) { + if self.needed { + *self + .owner + .completed_keys + .lock() + .unwrap_or_else(PoisonError::into_inner) = self.keys; + self.owner.completed.fetch_add(1, Ordering::Release); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::account::testing::open_keys; + use futures::{FutureExt as _, poll}; + + #[tokio::test] + async fn waiting_readers_reuse_only_a_successful_refresh_of_the_same_open_keys() { + let refresh = Refresh::default(); + let keys = Arc::new(open_keys()); + let leader = refresh.enter(&keys).await; + assert!(leader.needed()); + let mut same = refresh.enter(&keys).boxed(); + let replacement = Arc::new(open_keys()); + let mut next = refresh.enter(&replacement).boxed(); + assert!(poll!(&mut same).is_pending()); + assert!(poll!(&mut next).is_pending()); + leader.complete(); + let follower = same.await; + assert!(!follower.needed()); + follower.complete(); + assert!(next.await.needed()); + } + + #[tokio::test] + async fn cancelled_or_failed_leaders_do_not_satisfy_waiting_readers() { + let refresh = Refresh::default(); + let keys = Arc::new(open_keys()); + let leader = refresh.enter(&keys).await; + let mut waiting = refresh.enter(&keys).boxed(); + assert!(poll!(&mut waiting).is_pending()); + drop(leader); + let retry = waiting.await; + assert!(retry.needed()); + retry.complete(); + assert!(refresh.enter(&keys).await.needed()); + } +} diff --git a/crates/hub-io/src/account/slot.rs b/crates/hub-io/src/account/slot.rs new file mode 100644 index 00000000..37ea02fa --- /dev/null +++ b/crates/hub-io/src/account/slot.rs @@ -0,0 +1,541 @@ +//! One native account Slot owns key lifecycle, original membership and private cache publication. +//! Immutable cache generations separate content contention from lifecycle receipts. +//! A serialized async writer stages on shared bounded workers; admission retains +//! only the final generation check, namespace replacement and memory transition. +use super::cache_publication::CacheDraft; +use super::{ + Notice, OpenKeys, Recovery, SlotReceipt, lifecycle::SlotAuthority, source::AccountSubject, +}; +use crate::{ + cache::{AccountCache, CacheFile}, + classify::credentials, +}; +use nocterm_core::fs::CommittedPublication; +use nocterm_hubs_model::{HubError, HubId}; +use nocterm_vault::VaultStatus; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +pub(crate) struct SlotEnd { + receipt: SlotReceipt, + result: Result<(), HubError>, +} +impl SlotEnd { + pub(crate) fn into_parts(self) -> (SlotReceipt, Result<(), HubError>) { + (self.receipt, self.result) + } +} + +enum KeyState { + Locked, + Open(Arc), + Recovering(Arc), +} + +struct SlotState { + keys: KeyState, + cache: Option>, + authority: SlotAuthority, + pending_recovery: Option, +} + +/// Everything this device knows of one hub's account keys. +pub(crate) struct Slot { + hub: HubId, + file: CacheFile, + notice: Arc, + state: Mutex, + cache_write: tokio::sync::Mutex<()>, + refresh: super::refresh::Refresh, +} + +impl Slot { + pub(crate) fn pending_recovery_for( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result { + let state = self.state(); + state.authority.check(owner, receipt)?; + Ok(state + .cache + .as_ref() + .is_some_and(|cache| cache.account_id == owner.account() && cache.recovery_unconfirmed) + || state + .pending_recovery + .as_ref() + .is_some_and(|pending| state.authority.check_observed(pending).is_ok())) + } + + /// A remote keyset was created, but its first durable local installation + /// failed. Keep only this bounded fact for the original current generation. + pub(crate) fn remember_pending_recovery( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result { + let next = { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + let next = state.authority.rotate(); + state.pending_recovery = Some(next.clone()); + next + }; + self.notice.raise(); + Ok(next) + } + pub(crate) async fn refresh_turn( + &self, + keys: &Arc, + ) -> super::refresh::RefreshTurn<'_> { + self.refresh.enter(keys).await + } + + pub(crate) fn cache_work(&self) -> &Arc { + self.file.work() + } + pub(crate) async fn cache_turn(&self) -> tokio::sync::MutexGuard<'_, ()> { + self.cache_write.lock().await + } + + pub(crate) fn cache_snapshot(&self) -> Option> { + self.state().cache.clone() + } + + pub(crate) async fn stage_cache( + &self, + original: Option>, + next: AccountCache, + ) -> Result { + let file = self.file.clone(); + self.file + .work() + .run(move || { + let publication = file.stage(&next)?; + Ok(CacheDraft::new(original, next, publication)) + }) + .await + .map_err(credentials) + } + + pub(crate) async fn finish_cache(&self, committed: CommittedPublication) { + if let Err(error) = self + .file + .work() + .run(move || { + committed.sync_parent(); + Ok(()) + }) + .await + { + tracing::warn!(%error, "the committed account cache parent cannot be synced"); + } + } + + pub(crate) fn commit_cache_if( + &self, + expected: &Arc, + draft: CacheDraft, + ) -> Result, HubError> { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) + || draft.account() != expected.keyset().account_id() + { + return Err(HubError::Stale); + } + if !draft.matches(&state.cache) { + return Ok(None); + } + let (next, committed) = draft.commit().map_err(credentials)?; + state.cache = Some(next); + Ok(Some(committed)) + } + + /// Ordinary encrypted refresh remains usable when its offline copy cannot + /// be written. Security facts use the durable commit methods exclusively. + pub(crate) fn accept_memory_cache_if( + &self, + expected: &Arc, + original: &Option>, + next: Arc, + ) -> Result { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) + || next.account_id != expected.keyset().account_id() + { + return Err(HubError::Stale); + } + if !super::cache_publication::same_generation(original, &state.cache) { + return Ok(false); + } + state.cache = Some(next); + Ok(true) + } + + pub(crate) fn commit_member_cache( + &self, + owner: &Arc, + receipt: &SlotReceipt, + draft: CacheDraft, + rotate: bool, + ) -> Result, HubError> { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + if draft.account() != owner.account() { + return Err(HubError::Stale); + } + if !draft.matches(&state.cache) { + return Ok(None); + } + let (next, committed) = draft.commit().map_err(credentials)?; + state.cache = Some(next); + state.pending_recovery = None; + let receipt = if rotate { + state.authority.rotate() + } else { + receipt.clone() + }; + Ok(Some((receipt, committed))) + } + + pub(crate) fn commit_install( + &self, + owner: &Arc, + receipt: &SlotReceipt, + keys: Arc, + draft: CacheDraft, + ) -> Result, HubError> { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + if draft.account() != owner.account() || keys.keyset().account_id() != owner.account() { + return Err(HubError::Stale); + } + if !draft.matches(&state.cache) { + return Ok(None); + } + let (next, committed) = draft.commit().map_err(credentials)?; + state.cache = Some(next); + state.keys = KeyState::Open(keys); + state.pending_recovery = None; + Ok(Some((state.authority.rotate(), committed))) + } + + pub(super) fn new(hub: HubId, file: CacheFile, notice: Arc) -> Self { + let cache = match file.load() { + Ok(cache) => cache, + Err(error) => { + tracing::warn!(%error, hub = %hub, "the account cache is not usable; it is read again from the hub"); + None + } + }; + Self { + hub, + file, + notice, + state: Mutex::new(SlotState { + keys: KeyState::Locked, + cache: cache.map(Arc::new), + authority: SlotAuthority::new(), + pending_recovery: None, + }), + cache_write: tokio::sync::Mutex::new(()), + refresh: super::refresh::Refresh::default(), + } + } + + fn state(&self) -> MutexGuard<'_, SlotState> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } + + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + + pub(crate) fn notice(&self) -> &Arc { + &self.notice + } + + /// Called under Pool's clients mutex before a new membership is visible. + pub(crate) fn begin_membership(&self, owner: &Arc) { + { + let mut state = self.state(); + state.keys = KeyState::Locked; + state.authority.begin(owner); + state.pending_recovery = None; + if state + .cache + .as_ref() + .is_some_and(|cache| cache.account_id != owner.account()) + { + state.cache = None; + if let Err(error) = self.file.erase() { + tracing::warn!(%error, "the former account cache cannot be erased"); + } + } + } + self.notice.raise(); + } + + pub(crate) fn receipt(&self, owner: &Arc) -> Result { + self.state().authority.receipt(owner) + } + + pub(crate) fn check_receipt( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result<(), HubError> { + self.state().authority.check(owner, receipt) + } + + pub(crate) fn observed(&self) -> SlotReceipt { + self.state().authority.observed() + } + pub(crate) fn check_observed(&self, receipt: &SlotReceipt) -> Result<(), HubError> { + self.state().authority.check_observed(receipt) + } + pub(crate) fn belongs_to(&self, owner: &Arc) -> Result<(), HubError> { + self.state().authority.check_owner(owner) + } + + pub(crate) fn close_for(&self, owner: &Arc) -> Result { + let next = { + let mut state = self.state(); + state.authority.check_owner(owner)?; + state.keys = KeyState::Locked; + state.authority.rotate() + }; + self.notice.raise(); + Ok(next) + } + + pub(crate) fn end_observed( + &self, + receipt: &SlotReceipt, + erase: bool, + ) -> Result { + let ended = { + let mut state = self.state(); + state.authority.check_observed(receipt)?; + state.keys = KeyState::Locked; + if erase { + state.cache = None; + } + let receipt = state.authority.rotate(); + let result = if erase { + self.file.erase().map_err(credentials) + } else { + Ok(()) + }; + SlotEnd { receipt, result } + }; + self.notice.raise(); + Ok(ended) + } + + pub(crate) fn open_for(&self, owner: &Arc) -> Result, HubError> { + let state = self.state(); + state.authority.check_owner(owner)?; + match &state.keys { + KeyState::Open(keys) if keys.keyset().account_id() == owner.account() => { + Ok(keys.clone()) + } + _ => Err(HubError::KeysLocked), + } + } + + pub(crate) fn cache_for( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result { + let state = self.state(); + state.authority.check(owner, receipt)?; + state.cache.as_deref().cloned().ok_or(HubError::NotFound) + } + + pub(super) fn recovering_if( + &self, + owner: &Arc, + receipt: &SlotReceipt, + recovery: Recovery, + ) -> Result { + let next = { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + if recovery.account_id != owner.account() { + return Err(HubError::Stale); + } + state.keys = KeyState::Recovering(Arc::new(recovery)); + state.authority.rotate() + }; + self.notice.raise(); + Ok(next) + } + + pub(crate) fn lock_keys_if(&self, expected: &Arc) -> bool { + { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) { + return false; + } + state.keys = KeyState::Locked; + state.authority.rotate(); + } + self.notice.raise(); + true + } + + pub(crate) fn while_keys_current( + &self, + expected: &Arc, + publish: impl FnOnce() -> T, + ) -> Result { + let state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) { + return Err(HubError::Stale); + } + Ok(publish()) + } + + pub(crate) fn erase_for(&self, owner: &Arc) -> Result<(), HubError> { + let result = { + let mut state = self.state(); + state.authority.check_owner(owner)?; + state.keys = KeyState::Locked; + state.cache = None; + state.authority.rotate(); + self.file.erase().map_err(credentials) + }; + self.notice.raise(); + result + } + + /// The keys, while the vault is unlocked. + pub(crate) fn open(&self) -> Option> { + match &self.state().keys { + KeyState::Open(keys) => Some(Arc::clone(keys)), + _ => None, + } + } + + pub(crate) fn status(&self) -> VaultStatus { + let state = self.state(); + match (&state.keys, &state.cache) { + (KeyState::Open(_), _) => VaultStatus::Unlocked, + (_, Some(_)) => VaultStatus::Locked, + _ => VaultStatus::Missing, + } + } + + /// Reads the cached account, if the hub has one on this device. + pub(crate) fn read_cache(&self, read: impl FnOnce(Option<&AccountCache>) -> R) -> R { + let snapshot = self.state().cache.clone(); + read(snapshot.as_deref()) + } + + /// Uses only the original open account, under the same mutex as cache access. + pub(crate) fn read_cache_if( + &self, + expected: &Arc, + read: impl FnOnce(&AccountCache) -> R, + ) -> Result { + let state = self.state(); + let current = matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)); + let cache = state + .cache + .as_ref() + .filter(|cache| current && cache.account_id == expected.keyset().account_id()) + .ok_or(nocterm_vault::VaultError::Stale)?; + let cache = cache.clone(); + drop(state); + Ok(read(&cache)) + } + + /// Whether the recovery key of `account_id` was shown on this device and never confirmed. + /// A cache of another account says nothing about this one. + pub(crate) fn recovery_unconfirmed(&self, account_id: &str) -> bool { + self.read_cache(|cache| { + cache.is_some_and(|cache| cache.account_id == account_id && cache.recovery_unconfirmed) + }) + } + + /// Opens the vault with `keys`, remembering `cache` on disk. A cache that cannot be + /// written is logged: the account still works, but cannot unlock without the hub. + #[cfg(test)] + pub(crate) fn install(&self, keys: OpenKeys, cache: AccountCache) { + { + let mut state = self.state(); + if let Err(error) = self.file.save(&cache) { + tracing::warn!(%error, hub = %self.hub, "the account cache cannot be written"); + } + state.cache = Some(Arc::new(cache)); + state.keys = KeyState::Open(Arc::new(keys)); + state.authority.rotate(); + } + self.notice.raise(); + } + + /// Opens the vault with `keys` from a cache that is already kept. + #[cfg(test)] + pub(crate) fn unlocked(&self, keys: OpenKeys) { + { + let mut state = self.state(); + state.keys = KeyState::Open(Arc::new(keys)); + state.authority.rotate(); + } + self.notice.raise(); + } + + pub(super) fn recovery(&self) -> Option> { + match &self.state().keys { + KeyState::Recovering(recovery) => Some(Arc::clone(recovery)), + _ => None, + } + } + + /// Drops the keys: the vault is locked, the cache stays. + pub(crate) fn lock(&self) { + let was_unlocked = { + let mut state = self.state(); + let was = !matches!(state.keys, KeyState::Locked); + state.keys = KeyState::Locked; + state.authority.rotate(); + was + }; + if was_unlocked { + self.notice.raise(); + } + } + + /// A captured revoked operation may erase only its original open account. + pub(crate) fn erase_if(&self, expected: &Arc) -> Result<(), HubError> { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) + || !state + .cache + .as_ref() + .is_some_and(|cache| cache.account_id == expected.keyset().account_id()) + { + return Ok(()); + } + state.keys = KeyState::Locked; + state.cache = None; + state.authority.rotate(); + let result = self.file.erase().map_err(credentials); + drop(state); + self.notice.raise(); + result + } + + /// Drops the keys and erases the cache: the account is signed out of this device. + pub(crate) fn erase(&self) -> Result<(), HubError> { + { + let mut state = self.state(); + state.keys = KeyState::Locked; + state.cache = None; + state.authority.rotate(); + } + self.notice.raise(); + self.file.erase().map_err(credentials) + } +} diff --git a/crates/hub-io/src/account/source.rs b/crates/hub-io/src/account/source.rs new file mode 100644 index 00000000..a0cd2099 --- /dev/null +++ b/crates/hub-io/src/account/source.rs @@ -0,0 +1,160 @@ +//! Captured local-history identity of the signed-in account. +use super::{OpenKeys, Slot}; +use crate::classify::remote; +use nocterm_hubs_model::{ + HubError, HubId, + source_identity::{ + Fingerprint, LegacyConfiguredAccount, LocalAccountPartition, LocalAccountSource, + LocalAccountSourceError, NamespaceId, + }, +}; +use std::sync::{Arc, Weak}; + +mod membership; +pub(crate) use membership::CapturedMembership; + +/// Actual account/device metadata of one installed pool membership. Its Arc +/// identity ends at eviction or a new login, independently from token rotation. +#[derive(PartialEq, Eq)] +pub(crate) struct AccountSubject { + account: String, + device: String, +} +impl AccountSubject { + pub(crate) fn new(account: String, device: String) -> Self { + Self { account, device } + } + pub(crate) fn device(&self) -> &str { + &self.device + } + pub(crate) fn account(&self) -> &str { + &self.account + } +} + +/// Synchronous, non-I/O metadata access to the actual installed client. No device +/// keys, tokens or mutable SDK client cross this consumer-specific boundary. +pub(crate) trait AccountSubjects: Send + Sync { + fn current(&self, hub: &HubId) -> Option>; + fn with_current( + &self, + hub: &HubId, + expected: &Arc, + action: &mut dyn FnMut() -> Result<(), HubError>, + ) -> Result<(), HubError>; +} + +#[derive(Clone)] +pub(crate) struct LocalSourceProvider { + slot: Arc, + subjects: Arc, +} +impl LocalSourceProvider { + pub(crate) fn new(slot: Arc, subjects: Arc) -> Self { + Self { slot, subjects } + } + pub(crate) fn capture(&self) -> Result, HubError> { + self.capture_native() + .map(|source| source as Arc) + } + pub(crate) fn membership(&self) -> Result, HubError> { + CapturedMembership::capture(&self.slot, &self.subjects) + } + pub(crate) fn capture_native(&self) -> Result, HubError> { + self.membership()?.opened() + } +} + +pub(crate) struct CapturedAccount { + slot: Weak, + subjects: Weak, + partition: LocalAccountPartition, + keys: Weak, + subject: Weak, +} +impl CapturedAccount { + /// The callback must not call Slot or Pool; both native ownership locks are held. + pub(crate) fn publish(&self, publish: impl FnOnce() -> T) -> Result { + let membership = self.membership()?; + let expected = self.keys.upgrade().ok_or(HubError::Stale)?; + membership.with_current(|| self.slot()?.while_keys_current(&expected, publish)) + } + + pub(crate) fn membership(&self) -> Result, HubError> { + let slot = self.slot()?; + let subjects = self.subjects.upgrade().ok_or(HubError::Stale)?; + let subject = self.subject()?; + CapturedMembership::published(&slot, &subjects, &subject) + } + + pub(crate) fn with_current( + &self, + action: impl FnOnce(&Arc) -> Result, + ) -> Result { + let membership = self.membership()?; + let expected = self.keys.upgrade().ok_or(HubError::Stale)?; + membership.with_current(|| { + let keys = self.slot()?.open_for(&membership.subject()?)?; + if !Arc::ptr_eq(&keys, &expected) { + return Err(HubError::Stale); + } + action(&keys) + }) + } + pub(crate) fn keys(&self) -> Result, HubError> { + self.check_current().map_err(|_| HubError::NotSignedIn)?; + self.keys.upgrade().ok_or(HubError::KeysLocked) + } + pub(crate) fn slot(&self) -> Result, HubError> { + self.slot.upgrade().ok_or(HubError::NotSignedIn) + } + pub(crate) fn subject(&self) -> Result, HubError> { + self.check_current().map_err(|_| HubError::NotSignedIn)?; + self.subject.upgrade().ok_or(HubError::NotSignedIn) + } +} + +impl LocalAccountSource for CapturedAccount { + fn partition(&self) -> LocalAccountPartition { + self.partition.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + let slot = self.slot.upgrade().ok_or(LocalAccountSourceError::Stale)?; + let subjects = self + .subjects + .upgrade() + .ok_or(LocalAccountSourceError::Stale)?; + let expected_keys = self.keys.upgrade().ok_or(LocalAccountSourceError::Stale)?; + let expected_subject = self + .subject + .upgrade() + .ok_or(LocalAccountSourceError::Stale)?; + let subject = subjects + .current(slot.hub()) + .ok_or(LocalAccountSourceError::Stale)?; + let keys = slot + .open_for(&subject) + .map_err(|_| LocalAccountSourceError::Stale)?; + if !Arc::ptr_eq(&expected_keys, &keys) + || !Arc::ptr_eq(&expected_subject, &subject) + || keys.keyset().account_id() != subject.account() + { + return Err(LocalAccountSourceError::Stale); + } + Ok(()) + } +} + +/// The local history partition of `account` on `hub`. It depends on the account only, never +/// on its keys, so a locked vault keeps the same chats. +pub(crate) fn partition_of(hub: &HubId, account: &str) -> Result { + use sha2::{Digest as _, Sha256}; + let id = NamespaceId::new(account.to_owned()).map_err(remote)?; + let digest: [u8; 32] = Sha256::digest(account.as_bytes()).into(); + Ok(LocalAccountPartition::LegacyConfigured( + LegacyConfiguredAccount::new(hub.clone(), id, Fingerprint::from_bytes(digest)), + )) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/account/source/membership.rs b/crates/hub-io/src/account/source/membership.rs new file mode 100644 index 00000000..d2f4ef72 --- /dev/null +++ b/crates/hub-io/src/account/source/membership.rs @@ -0,0 +1,110 @@ +//! Native login membership remains usable for explicit unlock while keys are locked. +use super::{AccountSubject, AccountSubjects, CapturedAccount, partition_of}; +use crate::account::Slot; +use nocterm_hubs_model::source_identity::{ + LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, +}; +use nocterm_hubs_model::{HubError, HubId}; +use std::sync::{Arc, Weak}; + +pub(crate) struct CapturedMembership { + slot: Weak, + subjects: Weak, + subject: Weak, + hub: HubId, + partition: LocalAccountPartition, +} + +impl CapturedMembership { + pub(super) fn capture( + slot: &Arc, + subjects: &Arc, + ) -> Result, HubError> { + let subject = subjects.current(slot.hub()).ok_or(HubError::NotSignedIn)?; + Self::published(slot, subjects, &subject) + } + + pub(crate) fn published( + slot: &Arc, + subjects: &Arc, + subject: &Arc, + ) -> Result, HubError> { + let captured = Arc::new(Self { + slot: Arc::downgrade(slot), + subjects: Arc::downgrade(subjects), + subject: Arc::downgrade(subject), + hub: slot.hub().clone(), + partition: partition_of(slot.hub(), subject.account())?, + }); + captured.check()?; + Ok(captured) + } + + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + + pub(crate) fn slot(&self) -> Result, HubError> { + self.slot.upgrade().ok_or(HubError::Stale) + } + + pub(crate) fn subject(&self) -> Result, HubError> { + self.subject.upgrade().ok_or(HubError::Stale) + } + + pub(crate) fn check(&self) -> Result<(), HubError> { + self.with_current(|| Ok(())) + } + + pub(crate) fn opened(self: &Arc) -> Result, HubError> { + self.with_current(|| { + let slot = self.slot()?; + let subject = self.subject()?; + let keys = slot.open_for(&subject)?; + let partition = self.partition.clone(); + Ok(Arc::new(CapturedAccount { + slot: Arc::downgrade(&slot), + subjects: self.subjects.clone(), + partition, + keys: Arc::downgrade(&keys), + subject: Arc::downgrade(&subject), + })) + }) + } + + /// The source provider owns admission, including the native Pool mutex. + /// Actions must be synchronous: native ownership cannot change until return. + pub(crate) fn with_current( + &self, + action: impl FnOnce() -> Result, + ) -> Result { + let subjects = self.subjects.upgrade().ok_or(HubError::Stale)?; + let expected = self.subject()?; + let mut action = Some(action); + let mut result = None; + subjects.with_current(&self.hub, &expected, &mut || { + let run = action.take().ok_or(HubError::Stale)?; + result = Some(run()?); + Ok(()) + })?; + result.ok_or(HubError::Stale) + } + + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} + +/// The membership alone is the local history source of a hub window: a locked vault keeps its +/// chats; sign-out or another login makes it stale. +impl LocalAccountSource for CapturedMembership { + fn partition(&self) -> LocalAccountPartition { + self.partition.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + self.check().map_err(|_| LocalAccountSourceError::Stale) + } +} diff --git a/crates/hub-io/src/account/source/tests.rs b/crates/hub-io/src/account/source/tests.rs new file mode 100644 index 00000000..989090f9 --- /dev/null +++ b/crates/hub-io/src/account/source/tests.rs @@ -0,0 +1,269 @@ +use super::*; +use crate::{ + account::{Accounts, testing::*}, + cache::UnlockMethod, +}; +use nocterm_hub_proto::v2; +use nocterm_keys::UnlockKey; + +struct Fixture { + _directory: tempfile::TempDir, + slot: Arc, + subjects: Arc, + provider: LocalSourceProvider, + record: v2::Keyset, + unlock: UnlockKey, +} +impl Fixture { + fn new() -> Self { + let directory = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(directory.path().to_path_buf()); + let slot = accounts.slot(&hub()).unwrap(); + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + let subjects = Arc::new(ScriptedSubjects::for_slot(&slot)); + subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + slot.install( + open(&record, &unlock), + cache(&record, UnlockMethod::Passphrase), + ); + let provider = LocalSourceProvider::new(slot.clone(), subjects.clone()); + Self { + _directory: directory, + slot, + subjects, + provider, + record, + unlock, + } + } + fn capture(&self) -> Arc { + self.provider.capture().unwrap() + } + fn unlock(&self) { + self.slot.unlocked(open(&self.record, &self.unlock)); + } +} + +#[test] +fn local_partition_binds_the_configured_hub_and_the_account_but_not_its_keys() { + let fixture = Fixture::new(); + let source = fixture.capture(); + let LocalAccountPartition::LegacyConfigured(partition) = source.partition() else { + panic!("a hub window's history is partitioned by hub and account") + }; + assert_eq!(partition.hub(), &hub()); + assert_eq!(partition.account().as_str(), ACCOUNT_ID); + assert_eq!(source.check_current(), Ok(())); + let membership = fixture.provider.membership().unwrap(); + fixture.slot.lock(); + assert_eq!( + LocalAccountSource::partition(membership.as_ref()), + source.partition(), + "a locked vault keeps the same chats" + ); + assert_eq!( + LocalAccountSource::check_current(membership.as_ref()), + Ok(()) + ); +} + +#[test] +fn locking_drops_open_keys_despite_retained_history_captures_and_providers() { + let fixture = Fixture::new(); + let keys = Arc::downgrade(&fixture.slot.open().unwrap()); + let source = fixture.capture(); + fixture.slot.lock(); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + assert!( + keys.upgrade().is_none(), + "the source cannot retain the secret owner" + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + let next = fixture.capture(); + assert_eq!(next.partition(), source.partition()); + assert_eq!(next.check_current(), Ok(())); +} + +#[test] +fn a_same_account_relogin_invalidates_capture_even_before_keys_are_reinstalled() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + let next = fixture.capture(); + assert_eq!(next.partition(), original.partition()); + assert_eq!(next.check_current(), Ok(())); +} + +#[test] +fn an_account_round_trip_never_revives_an_old_capture() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture + .subjects + .sign_in(hub(), "another-account", "device-2"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + assert_eq!(fixture.capture().check_current(), Ok(())); +} + +#[test] +fn pool_eviction_invalidates_capture_before_key_erasure_or_remote_work() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture.subjects.evict(&hub()); + assert!( + fixture.slot.open().is_some(), + "sign-out's remote work has not erased keys yet" + ); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::NotSignedIn) + )); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); +} + +#[test] +fn ordinary_refresh_retains_the_capture_but_device_replacement_does_not() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture.subjects.renew(hub(), ACCOUNT_ID, "device-1"); + assert_eq!(original.check_current(), Ok(())); + fixture.subjects.renew(hub(), ACCOUNT_ID, "device-2"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + assert_eq!(fixture.capture().partition(), original.partition()); +} + +#[test] +fn replacing_keys_invalidates_capture_but_keeps_its_local_partition() { + let fixture = Fixture::new(); + let original = fixture.capture(); + let (replacement, _) = record(&fixture.unlock); + fixture.slot.install( + open(&replacement, &fixture.unlock), + cache(&replacement, UnlockMethod::Passphrase), + ); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert_eq!(fixture.capture().partition(), original.partition()); +} + +#[test] +fn a_retained_provider_can_wait_for_the_first_unlock_without_capturing_keys() { + let fixture = Fixture::new(); + fixture.slot.lock(); + let provider = LocalSourceProvider::new(fixture.slot.clone(), fixture.subjects.clone()); + assert!(matches!(provider.capture(), Err(HubError::KeysLocked))); + fixture.unlock(); + assert_eq!(provider.capture().unwrap().check_current(), Ok(())); +} + +#[test] +fn a_capture_alone_keeps_neither_the_account_owner_nor_its_open_keys_alive() { + let (source, keys) = { + let fixture = Fixture::new(); + let keys = Arc::downgrade(&fixture.slot.open().unwrap()); + (fixture.capture(), keys) + }; + assert!(keys.upgrade().is_none()); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); +} + +#[tokio::test] +async fn locked_membership_admits_unlock_but_an_explicit_lock_retires_its_receipt() { + let fixture = Fixture::new(); + fixture.slot.lock(); + let membership = fixture.provider.membership().unwrap(); + let subject = membership.subject().unwrap(); + let receipt = membership + .with_current(|| fixture.slot.receipt(&subject)) + .unwrap(); + assert!(membership.check().is_ok()); + fixture.slot.lock(); + let next = cache(&fixture.record, UnlockMethod::Passphrase); + let keys = Arc::new(open(&fixture.record, &fixture.unlock)); + let draft = fixture + .slot + .stage_cache(fixture.slot.cache_snapshot(), next) + .await + .unwrap(); + let result = + membership.with_current(|| fixture.slot.commit_install(&subject, &receipt, keys, draft)); + assert!(matches!(result, Err(HubError::Stale))); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); +} + +#[tokio::test] +async fn a_late_unlock_never_installs_keys_after_another_login_is_published() { + let fixture = Fixture::new(); + fixture.slot.lock(); + let original = fixture.provider.membership().unwrap(); + let subject = original.subject().unwrap(); + let receipt = fixture.slot.receipt(&subject).unwrap(); + let next = cache(&fixture.record, UnlockMethod::Passphrase); + let keys = Arc::new(open(&fixture.record, &fixture.unlock)); + let draft = fixture + .slot + .stage_cache(fixture.slot.cache_snapshot(), next) + .await + .unwrap(); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + let result = + original.with_current(|| fixture.slot.commit_install(&subject, &receipt, keys, draft)); + assert!(matches!(result, Err(HubError::Stale))); + assert!(fixture.slot.open().is_none()); + fixture.unlock(); + assert!(fixture.provider.capture().is_ok()); + assert!(matches!(original.check(), Err(HubError::Stale))); +} diff --git a/crates/hub-io/src/account/testing.rs b/crates/hub-io/src/account/testing.rs new file mode 100644 index 00000000..5f7bfe94 --- /dev/null +++ b/crates/hub-io/src/account/testing.rs @@ -0,0 +1,155 @@ +//! Keys for tests, made the way a first sign-in makes them but without a hub. + +use std::sync::Arc; + +use nocterm_hub_client::Keyset; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use nocterm_keys::{AccountKey, KdfParams, RecoveryKey, UnlockKdf, UnlockKey, UnlockSource}; + +use super::{Accounts, OpenKeys, Slot}; +use crate::cache::{AccountCache, UnlockMethod}; + +mod accounts; +mod hub; +mod native_operations; +mod subjects; +pub(crate) use accounts::NativeAccounts; +pub(crate) use native_operations::{NativeVault, native_operations}; + +pub(crate) use hub::MemoryKeysets; +pub(crate) use subjects::ScriptedSubjects; + +pub(crate) const ACCOUNT_ID: &str = "account-1"; +pub(crate) const HUB_UID: &str = "hub-uid"; +pub(crate) const PASSWORD: &str = "correct horse battery"; + +/// Occupies the existing physical cache worker until the shared gate is released. +/// Polling its returned future controls FIFO admission without an I/O injection hook. +pub(crate) struct CacheJobRelease(nocterm_session::testing::Gate); +impl CacheJobRelease { + pub(crate) fn release(&self) { + self.0.release(); + } +} +impl Drop for CacheJobRelease { + fn drop(&mut self) { + self.0.release(); + } +} + +pub(crate) fn blocked_cache_job( + slot: &Slot, +) -> ( + futures::future::BoxFuture<'static, Result<(), crate::cache::CacheError>>, + nocterm_session::testing::Gate, + CacheJobRelease, +) { + let entered = nocterm_session::testing::Gate::pending(); + let release = nocterm_session::testing::Gate::pending(); + let worker_entered = entered.clone(); + let worker_release = release.clone(); + let work = slot.cache_work().clone(); + let job = Box::pin(async move { + work.run(move || { + worker_entered.release(); + futures::executor::block_on(worker_release.wait()); + Ok(()) + }) + .await + }); + (job, entered, CacheJobRelease(release)) +} + +pub(crate) fn kdf() -> UnlockKdf { + UnlockKdf::new(KdfParams::MINIMUM, vec![7; 16]).expect("a valid derivation") +} + +pub(crate) fn unlock(password: &str) -> UnlockKey { + UnlockKey::from_vault_password(password, &kdf()).expect("a usable password") +} + +/// A keyset record, as a first sign-in seals it, at version 0. +pub(crate) fn sealed_record( + account_id: &str, + unlock: &UnlockKey, + kdf: &UnlockKdf, + account_key: &AccountKey, + recovery: &RecoveryKey, +) -> v2::Keyset { + v2::Keyset { + version: 0, + kdf: kdf.description(), + salt: kdf.salt().to_vec(), + wrapped_account_key: account_key.seal(unlock, account_id).unwrap(), + updated_at_ms: 0, + recovery_wrapped_account_key: recovery.wrap(account_key, account_id).unwrap(), + unlock_kind: match unlock.source() { + UnlockSource::LoginPassword => v2::UnlockKind::LoginPassword, + UnlockSource::VaultPassword => v2::UnlockKind::VaultPassword, + } as i32, + } +} + +/// A keyset record sealed under `unlock`, and the recovery key that also opens it. +pub(crate) fn record(unlock: &UnlockKey) -> (v2::Keyset, RecoveryKey) { + let account_key = AccountKey::generate().unwrap(); + let recovery = RecoveryKey::generate().unwrap(); + let mut record = sealed_record(ACCOUNT_ID, unlock, &kdf(), &account_key, &recovery); + record.version = 1; + (record, recovery) +} + +pub(crate) fn open(record: &v2::Keyset, unlock: &UnlockKey) -> OpenKeys { + OpenKeys::new(Keyset::open(record, ACCOUNT_ID, unlock).expect("the record opens")) +} + +pub(crate) fn cache(record: &v2::Keyset, method: UnlockMethod) -> AccountCache { + AccountCache::new(HUB_UID.into(), ACCOUNT_ID.into(), method, record) +} + +/// Open keys on their own, for code that only needs to seal and open collections. +pub(crate) fn open_keys() -> Arc { + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + Arc::new(open(&record, &unlock)) +} + +pub(crate) fn hub() -> HubId { + hub_for("hub.test") +} + +pub(crate) fn hub_for(address: &str) -> HubId { + HubId::from(address.parse::().unwrap()) +} + +/// The slot of the test hub in `accounts`, opened as a sign-in with `PASSWORD` opens it. +pub(crate) fn sign_in(accounts: &Accounts) -> (Arc, v2::Keyset) { + let slot = accounts.slot(&hub()).unwrap(); + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + slot.install( + open(&record, &unlock), + cache(&record, UnlockMethod::Passphrase), + ); + (slot, record) +} + +/// A slot whose vault was opened by a sign-in with `PASSWORD`, kept in `dir`. +pub(crate) fn signed_in(dir: &std::path::Path) -> (Accounts, Arc, v2::Keyset) { + let accounts = Accounts::new(dir.to_path_buf()); + let (slot, record) = sign_in(&accounts); + (accounts, slot, record) +} + +/// Another device of the same account: the same keyset, opened with the same password. +pub(crate) fn second_device(dir: &std::path::Path, record: &v2::Keyset) -> (Accounts, Arc) { + let accounts = Accounts::new(dir.to_path_buf()); + let slot = accounts.slot(&hub()).unwrap(); + let unlock = unlock(PASSWORD); + slot.install( + open(record, &unlock), + cache(record, UnlockMethod::Passphrase), + ); + (accounts, slot) +} diff --git a/crates/hub-io/src/account/testing/accounts.rs b/crates/hub-io/src/account/testing/accounts.rs new file mode 100644 index 00000000..331395b4 --- /dev/null +++ b/crates/hub-io/src/account/testing/accounts.rs @@ -0,0 +1,152 @@ +//! Two actual fake native account owners sharing the production capture/ledger path. +use super::{self as testing, ScriptedSubjects}; +use crate::{ + account::{ + Accounts, OpenKeys, Slot, operations::AccountOperations, source::LocalSourceProvider, + }, + cache::{AccountCache, UnlockMethod}, + store::{ + StoreOps as _, VAULT_KIND, + memory::{MemoryAccountStores, MemoryHub}, + }, + vault::{HubVault, credentials::CredentialCapture}, +}; +use nocterm_hub_client::Keyset; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, HubId, source_identity::LocalAccountSource}; +use nocterm_session::{CredentialId, CredentialStore, Secret, SecretRequest, Target}; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct NativeAccounts { + _dir: tempfile::TempDir, + slot: Arc, + subjects: Arc, + provider: LocalSourceProvider, + operations: Arc, + vault: Arc, + hubs: [Arc; 2], + records: [v2::Keyset; 2], + unlock: nocterm_keys::UnlockKey, +} +impl NativeAccounts { + pub(crate) fn new() -> Self { + Self::at(testing::hub()) + } + pub(crate) fn at(hub: HubId) -> Self { + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + let slot = accounts.slot(&hub).unwrap(); + let unlock = testing::unlock(testing::PASSWORD); + let (a, _) = testing::record(&unlock); + let b = testing::sealed_record( + "account-2", + &unlock, + &testing::kdf(), + &nocterm_keys::AccountKey::generate().unwrap(), + &nocterm_keys::RecoveryKey::generate().unwrap(), + ); + let subjects = Arc::new(ScriptedSubjects::for_slot(&slot)); + let provider = LocalSourceProvider::new(slot.clone(), subjects.clone()); + let hubs = [ + Arc::new(MemoryHub::default()), + Arc::new(MemoryHub::default()), + ]; + let stores = Arc::new(MemoryAccountStores::new([ + (testing::ACCOUNT_ID.into(), hubs[0].clone()), + ("account-2".into(), hubs[1].clone()), + ])); + let operations = Arc::new(AccountOperations::with_stores(provider.clone(), stores)); + let vault = Arc::new(HubVault::new( + Handle::current(), + slot.clone(), + operations.clone(), + )); + let fixture = Self { + _dir: dir, + slot, + subjects, + provider, + operations, + vault, + hubs, + records: [a, b], + unlock, + }; + fixture.login(0); + fixture + } + pub(crate) fn slot(&self) -> &Arc { + &self.slot + } + pub(crate) fn subjects(&self) -> &Arc { + &self.subjects + } + pub(crate) fn hub(&self, index: usize) -> &Arc { + &self.hubs[index] + } + pub(crate) fn operations(&self) -> &Arc { + &self.operations + } + pub(crate) fn vault(&self) -> &Arc { + &self.vault + } + pub(crate) fn source(&self) -> Arc { + self.provider.capture().unwrap() + } + pub(crate) fn capture_credentials( + &self, + source: Arc, + ) -> Result, HubError> { + CredentialCapture::new( + Handle::current(), + self.operations.stores().clone(), + &self.vault.idle(), + ) + .capture(&self.provider, source) + } + pub(crate) fn store(&self) -> Arc { + self.capture_credentials(self.source()).unwrap() + } + pub(crate) fn login(&self, index: usize) { + let account = if index == 0 { + testing::ACCOUNT_ID + } else { + "account-2" + }; + self.subjects + .sign_in(self.slot.hub().clone(), account, "device"); + self.hubs[index].set_keyset(Some(self.records[index].clone())); + self.slot.install( + OpenKeys::new(Keyset::open(&self.records[index], account, &self.unlock).unwrap()), + AccountCache::new( + testing::HUB_UID.into(), + account.into(), + UnlockMethod::Passphrase, + &self.records[index], + ), + ); + } + pub(crate) async fn seed(&self) -> CredentialId { + self.store() + .save( + "A".into(), + &SecretRequest::Password { + target: Target::new("ann", "example.test", 22), + retry: false, + }, + Secret::new("secret A"), + ) + .await + .unwrap() + } + pub(crate) async fn count(&self, index: usize) -> usize { + self.hubs[index] + .collections(VAULT_KIND) + .await + .unwrap() + .iter() + .map(|at| self.hubs[index].objects(&at.collection_id).len()) + .sum() + } +} diff --git a/crates/hub-io/src/account/testing/hub.rs b/crates/hub-io/src/account/testing/hub.rs new file mode 100644 index 00000000..f7da6bd1 --- /dev/null +++ b/crates/hub-io/src/account/testing/hub.rs @@ -0,0 +1,158 @@ +//! The keyset of one account on a pretend hub that several devices share. + +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::future::BoxFuture; +use nocterm_hub_client::{ClientError, Keyset, KeysetError, NewKeyset}; +use nocterm_hub_proto::v2; +use nocterm_keys::{AccountKey, RecoveryKey, UnlockKdf, UnlockKey}; +use tokio::sync::Barrier; + +use super::{HUB_UID, sealed_record}; +use crate::account::keyset_hub::{KeysetHub, resealed}; + +#[derive(Default)] +struct Held { + record: Option, + /// Devices that read "no keyset" wait here for each other before they go on. + meet: Option>, + creations: usize, + /// Writes of the recovery envelope that find the version moved, as if another device wrote + /// the keyset between the read and the write. + contended: usize, +} + +/// What the hub answers a conditional write whose version moved. +fn conflict() -> ClientError { + let mut status = tonic::Status::new(tonic::Code::Aborted, "the keyset changed"); + status.metadata_mut().insert( + "x-nocterm-error", + "ERROR_CODE_CONFLICT".parse().expect("a header value"), + ); + ClientError::from(status) +} + +/// The keyset a hub holds for one account. Clones are devices of the same account: they share +/// what the hub holds. +#[derive(Clone, Default)] +pub(crate) struct MemoryKeysets { + held: Arc>, +} + +impl MemoryKeysets { + fn held(&self) -> MutexGuard<'_, Held> { + self.held.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// The record the hub holds. + pub(crate) fn stored(&self) -> Option { + self.held().record.clone() + } + + /// Makes the first `devices` that find no keyset wait for each other before they create it, + /// so all of them try at once. + pub(crate) fn meet_before_creating(&self, devices: usize) { + self.held().meet = Some(Arc::new(Barrier::new(devices))); + } + + /// Makes the next `writes` replacements of the recovery envelope lose to another device that + /// wrote the keyset first. + pub(crate) fn contend_recovery_writes(&self, writes: usize) { + self.held().contended = writes; + } + + /// How many devices tried to create the keyset. + pub(crate) fn creations(&self) -> usize { + self.held().creations + } +} + +impl KeysetHub for MemoryKeysets { + fn hub_uid(&self) -> String { + HUB_UID.to_owned() + } + + fn record(&self) -> BoxFuture<'_, Result, ClientError>> { + Box::pin(async move { + let (seen, meet) = { + let held = self.held(); + (held.record.clone(), held.meet.clone()) + }; + if let (None, Some(meet)) = (&seen, meet) { + meet.wait().await; + } + Ok(seen) + }) + } + + fn create<'a>( + &'a self, + account_id: &'a str, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(async move { + let mut held = self.held(); + held.creations += 1; + if held.record.is_some() { + return Err(KeysetError::AlreadyExists); + } + let account_key = AccountKey::generate()?; + let recovery = RecoveryKey::generate()?; + let mut record = sealed_record(account_id, unlock, kdf, &account_key, &recovery); + record.version = 1; + let keyset = Keyset::open(&record, account_id, unlock)?; + held.record = Some(record); + Ok(NewKeyset { keyset, recovery }) + }) + } + + fn recover<'a>( + &'a self, + account_id: &'a str, + recovery: &'a RecoveryKey, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(async move { + let mut held = self.held(); + let record = held.record.clone().ok_or(KeysetError::Incomplete)?; + let keyset = Keyset::recover(&record, account_id, recovery)?; + let mut next = sealed_record(account_id, unlock, kdf, keyset.account_key(), recovery); + next.version = record.version + 1; + let opened = Keyset::open(&next, account_id, unlock)?; + held.record = Some(next); + Ok(opened) + }) + } + + fn reseal_recovery<'a>( + &'a self, + keyset: &'a Keyset, + recovery: &'a RecoveryKey, + ) -> BoxFuture<'a, Result> { + Box::pin(async move { + let mut held = self.held(); + let record = held.record.clone().ok_or(KeysetError::Incomplete)?; + let request = resealed(&record, keyset, recovery)?; + if held.contended > 0 { + held.contended -= 1; + if let Some(stored) = held.record.as_mut() { + stored.version += 1; + } + return Err(KeysetError::Hub(conflict())); + } + let next = v2::Keyset { + version: record.version + 1, + kdf: request.kdf, + salt: request.salt, + wrapped_account_key: request.wrapped_account_key, + updated_at_ms: 0, + recovery_wrapped_account_key: request.recovery_wrapped_account_key, + unlock_kind: request.unlock_kind, + }; + held.record = Some(next.clone()); + Ok(next) + }) + } +} diff --git a/crates/hub-io/src/account/testing/native_operations.rs b/crates/hub-io/src/account/testing/native_operations.rs new file mode 100644 index 00000000..571d1b9a --- /dev/null +++ b/crates/hub-io/src/account/testing/native_operations.rs @@ -0,0 +1,44 @@ +//! Shared real-key/native-account fixtures over MemoryHub and ScriptedSubjects. +use super::{ACCOUNT_ID, PASSWORD, ScriptedSubjects, unlock}; +use crate::{ + account::{OpenKeys, Slot, operations::AccountOperations, source::LocalSourceProvider}, + store::memory::{MemoryAccountStores, MemoryHub}, +}; +use nocterm_hub_client::Keyset; +use std::sync::Arc; + +pub(crate) fn native_operations(slot: &Arc, hub: Arc) -> Arc { + let cached = slot.read_cache(|cache| cache.cloned()); + let was_open = slot.open().is_some(); + let account = cached + .as_ref() + .map_or(ACCOUNT_ID, |cache| cache.account_id.as_str()); + let subjects = Arc::new(ScriptedSubjects::for_slot(slot)); + subjects.sign_in(slot.hub().clone(), account, "device"); + if was_open && let Some(cache) = &cached { + let keyset = Keyset::open(&cache.keyset.record(), account, &unlock(PASSWORD)).unwrap(); + slot.install(OpenKeys::new(keyset), cache.clone()); + } + let stores = Arc::new(MemoryAccountStores::new([(account.to_owned(), hub)])); + let provider = LocalSourceProvider::new(slot.clone(), subjects); + Arc::new(AccountOperations::with_stores(provider, stores)) +} + +/// Drives the real general-vault and bound-replica ports over one MemoryHub. +pub(crate) struct NativeVault { + vault: crate::vault::HubVault, +} +impl NativeVault { + pub(crate) fn new(slot: Arc, hub: Arc) -> Self { + let operations = native_operations(&slot, hub); + Self { + vault: crate::vault::HubVault::new(tokio::runtime::Handle::current(), slot, operations), + } + } +} +impl std::ops::Deref for NativeVault { + type Target = crate::vault::HubVault; + fn deref(&self) -> &Self::Target { + &self.vault + } +} diff --git a/crates/hub-io/src/account/testing/subjects.rs b/crates/hub-io/src/account/testing/subjects.rs new file mode 100644 index 00000000..f86730b6 --- /dev/null +++ b/crates/hub-io/src/account/testing/subjects.rs @@ -0,0 +1,74 @@ +//! One shared fake of the non-I/O account-subject reader, without a hub service. +use crate::account::Slot; +use crate::account::source::{AccountSubject, AccountSubjects}; +use nocterm_hubs_model::{HubError, HubId}; +use std::{ + collections::HashMap, + sync::{Arc, Mutex, Weak}, +}; + +#[derive(Default)] +pub(crate) struct ScriptedSubjects { + subjects: Mutex>>, + slot: Option>, +} +impl ScriptedSubjects { + pub(crate) fn for_slot(slot: &Arc) -> Self { + Self { + subjects: Mutex::default(), + slot: Some(Arc::downgrade(slot)), + } + } + + fn begin(&self, hub: &HubId, subject: &Arc) { + if let Some(slot) = self.slot.as_ref().and_then(Weak::upgrade) + && slot.hub() == hub + { + slot.begin_membership(subject); + } + } + + pub(crate) fn sign_in(&self, hub: HubId, account: &str, device: &str) { + let mut subjects = self.subjects.lock().unwrap(); + let subject = Arc::new(AccountSubject::new(account.into(), device.into())); + self.begin(&hub, &subject); + subjects.insert(hub, subject); + } + pub(crate) fn renew(&self, hub: HubId, account: &str, device: &str) { + let mut subjects = self.subjects.lock().unwrap(); + let next = AccountSubject::new(account.into(), device.into()); + if subjects + .get(&hub) + .is_some_and(|previous| previous.as_ref() == &next) + { + return; + } + let next = Arc::new(next); + self.begin(&hub, &next); + subjects.insert(hub, next); + } + pub(crate) fn evict(&self, hub: &HubId) { + self.subjects.lock().unwrap().remove(hub); + } +} +impl AccountSubjects for ScriptedSubjects { + fn with_current( + &self, + hub: &HubId, + expected: &Arc, + action: &mut dyn FnMut() -> Result<(), HubError>, + ) -> Result<(), HubError> { + let subjects = self.subjects.lock().unwrap(); + if !subjects + .get(hub) + .is_some_and(|current| Arc::ptr_eq(current, expected)) + { + return Err(HubError::Stale); + } + action() + } + + fn current(&self, hub: &HubId) -> Option> { + self.subjects.lock().unwrap().get(hub).cloned() + } +} diff --git a/crates/hub-io/src/account/tests.rs b/crates/hub-io/src/account/tests.rs new file mode 100644 index 00000000..34e16410 --- /dev/null +++ b/crates/hub-io/src/account/tests.rs @@ -0,0 +1,88 @@ +use super::testing::*; +use super::*; +use crate::cache::CacheFile; +use nocterm_vault::VaultStatus; + +#[test] +fn a_signed_in_account_is_unlocked_and_locking_drops_its_keys() { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, _) = signed_in(dir.path()); + assert_eq!(slot.status(), VaultStatus::Unlocked); + let keys = slot.open().unwrap(); + slot.lock(); + assert_eq!(slot.status(), VaultStatus::Locked); + assert!(slot.open().is_none()); + drop(keys); +} + +#[test] +fn locking_and_signing_in_are_announced() { + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + let slot = accounts.slot(&hub()).unwrap(); + let before = accounts.notice().count(); + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + slot.install( + open(&record, &unlock), + cache(&record, UnlockMethod::Passphrase), + ); + assert!(accounts.notice().count() > before); + let opened = accounts.notice().count(); + slot.lock(); + assert!(accounts.notice().count() > opened); + let locked = accounts.notice().count(); + slot.lock(); + assert_eq!( + accounts.notice().count(), + locked, + "locking twice changes nothing" + ); +} + +#[test] +fn a_new_session_finds_the_account_locked_from_its_cache() { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, _slot, _) = signed_in(dir.path()); + let next = Accounts::new(dir.path().to_path_buf()); + let slot = next.slot(&hub()).unwrap(); + assert_eq!(slot.status(), VaultStatus::Locked); + assert!(slot.read_cache(|cache| cache.is_some())); +} + +#[test] +fn an_account_without_a_cache_is_missing() { + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + assert_eq!( + accounts.slot(&hub()).unwrap().status(), + VaultStatus::Missing + ); +} + +#[test] +fn signing_out_erases_the_cache_and_the_keys() { + let dir = tempfile::tempdir().unwrap(); + let (accounts, slot, _) = signed_in(dir.path()); + slot.erase().unwrap(); + assert_eq!(slot.status(), VaultStatus::Missing); + let file = CacheFile::new(dir.path(), hub().address()).unwrap(); + assert!(file.load().unwrap().is_none()); + assert_eq!( + accounts.slot(&hub()).unwrap().status(), + VaultStatus::Missing + ); +} + +#[test] +fn collection_keys_are_separate_and_open_only_their_own_collection() { + let keys = open_keys(); + let (first, first_envelope) = keys.new_collection("c-1").unwrap(); + let (second, second_envelope) = keys.new_collection("c-2").unwrap(); + let sealed = first.seal_object("c-1", "o", "k", b"secret").unwrap(); + assert!(second.open_object("c-1", "o", "k", &sealed).is_err()); + assert_ne!(first_envelope, second_envelope); + // An envelope belongs to its collection: it does not open as another one. + assert!(keys.own_collection("c-3", &first_envelope).is_err()); + assert!(keys.own_collection("c-1", &first_envelope).is_ok()); +} diff --git a/crates/hub-io/src/account_keys.rs b/crates/hub-io/src/account_keys.rs new file mode 100644 index 00000000..fb86c1ca --- /dev/null +++ b/crates/hub-io/src/account_keys.rs @@ -0,0 +1,105 @@ +//! The account key lock over the hub links: whether the keys of an account are open on this +//! device, opening them with the typed password, and closing them. +//! +//! It acts on the same account slot as the vault, so unlocking here opens the vault and +//! locking closes it. It does not own the vault's idle timer: only vault use restarts that. +//! Status/lock/unlock admission use only an already loaded native Slot. An +//! unloaded Slot has no current native membership or open keys; unlock captures +//! that original membership before its existing background worker starts I/O. + +use std::sync::Arc; + +use futures::FutureExt as _; +use nocterm_core::error::SharedCause; +use nocterm_hubs_model::{AccountKeyError, AccountKeyFuture, AccountKeyLock, HubError, HubId}; +use nocterm_session::Secret; +use nocterm_vault::{VaultAccess as _, VaultError}; + +use crate::links::HubLinks; + +/// Implements [`AccountKeyLock`] over the hub links. +pub struct AccountKeys { + links: Arc, +} + +impl AccountKeys { + pub fn new(links: Arc) -> Self { + Self { links } + } +} + +impl AccountKeyLock for AccountKeys { + fn is_open(&self, hub: &HubId) -> bool { + self.links.account_keys_open(hub) + } + + fn unlock(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()> { + match self.links.loaded_account(hub) { + Some(account) => { + let unlocking = account.vault().unlock(password); + async move { unlocking.await.map_err(failure) }.boxed() + } + None => async move { Err(sign_in_failure(HubError::NotSignedIn)) }.boxed(), + } + } + + fn lock(&self, hub: &HubId) { + if let Some(account) = self.links.loaded_account(hub) { + account.vault().lock(); + } + } +} + +fn failure(error: VaultError) -> AccountKeyError { + match error { + VaultError::Authentication => AccountKeyError::WrongPassword, + VaultError::Offline => AccountKeyError::Offline, + VaultError::Stale | VaultError::Cancelled => AccountKeyError::NotSignedIn, + other => AccountKeyError::Failed { + cause: SharedCause::new(other), + }, + } +} + +fn sign_in_failure(error: HubError) -> AccountKeyError { + match error { + HubError::NotSignedIn | HubError::Stale | HubError::NotFound => { + AccountKeyError::NotSignedIn + } + other => AccountKeyError::Failed { + cause: SharedCause::new(other), + }, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_wrong_password_and_an_unreachable_hub_are_told_apart() { + assert!(matches!( + failure(VaultError::Authentication), + AccountKeyError::WrongPassword + )); + assert!(matches!( + failure(VaultError::Offline), + AccountKeyError::Offline + )); + assert!(matches!( + failure(VaultError::Stale), + AccountKeyError::NotSignedIn + )); + let other = failure(VaultError::Format); + assert!(matches!(other, AccountKeyError::Failed { .. })); + assert!(std::error::Error::source(&other).is_some()); + } + + #[test] + fn a_hub_without_a_sign_in_is_not_signed_in() { + assert!(matches!( + sign_in_failure(HubError::NotSignedIn), + AccountKeyError::NotSignedIn + )); + } +} diff --git a/crates/hub-io/src/agents.rs b/crates/hub-io/src/agents.rs new file mode 100644 index 00000000..42dbf64a --- /dev/null +++ b/crates/hub-io/src/agents.rs @@ -0,0 +1,220 @@ +//! The agents, providers, agent key and agent servers of the account, over `AgentService` and +//! `ServerService`, as the ports of `nocterm-hub-agents-model`. +//! +//! Every call captures the account's original membership before it returns its future and +//! checks it again after every await, like the devices of the account: a result for an +//! account that signed out or was replaced meanwhile is `HubError::Stale`. + +mod convert; +mod servers; + +pub(crate) use convert::server as hub_server; + +use std::sync::Arc; + +use nocterm_hub_agents_model::{ + AgentDraft, AgentId, AgentsFuture, HubAgent, HubAgentDirectory, HubAgentsError, Provider, + ProviderDraft, ProviderId, ProviderTest, +}; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use tokio::runtime::Handle; + +use crate::classify::classify; +use crate::devices::{Captured, run}; +use crate::links::HubLinks; +use crate::pool::Pool; + +/// Implements the agents ports over the clients of the hubs this device is signed in to. +pub struct HubAgentsLink { + handle: Handle, + pool: Arc, + hints: Arc, +} + +impl HubAgentsLink { + pub fn new(links: &HubLinks) -> Self { + let (handle, pool) = links.agent_parts(); + Self { + handle, + pool, + hints: links.chat_hints(), + } + } + + /// Runs `call` with a fresh client of the account on `hub`. + fn call(&self, hub: &HubId, call: F) -> AgentsFuture + where + T: Send + 'static, + F: FnOnce(HubClient) -> Fut + Send + 'static, + Fut: std::future::Future> + Send + 'static, + { + let captured = Captured::new(self.pool.clone(), hub); + let outcome = run(captured, &self.handle, move |captured| async move { + let client = captured.client.clone(); + let address = captured.hub.address().clone(); + let fresh = captured.observed.during(client.ensure_fresh()).await?; + if let Err(error) = fresh { + return Ok(Err(HubAgentsError::Hub(classify(&address, error)))); + } + let result = captured.observed.during(call(client)).await?; + Ok(result.map_err(|error| error.into_agents(&address))) + }); + Box::pin(async move { outcome.await.map_err(HubAgentsError::Hub)? }) + } +} + +/// A failed call before it is told in the port's terms. +pub(crate) enum CallError { + Client(ClientError), + Agents(HubAgentsError), +} + +impl From for CallError { + fn from(status: tonic::Status) -> Self { + Self::Client(ClientError::from(status)) + } +} + +impl From for CallError { + fn from(error: HubAgentsError) -> Self { + Self::Agents(error) + } +} + +impl CallError { + fn into_agents(self, address: &nocterm_hubs_model::HubAddress) -> HubAgentsError { + match self { + Self::Agents(error) => error, + Self::Client(ClientError::Status { + hub_code: Some(v2::ErrorCode::LimitReached), + .. + }) => HubAgentsError::LimitReached, + Self::Client(error) => HubAgentsError::Hub(classify(address, error)), + } + } +} + +impl HubAgentDirectory for HubAgentsLink { + fn agents(&self, hub: &HubId) -> AgentsFuture> { + self.call(hub, |client| async move { + let listed = client + .agents() + .list_agents(v2::Empty {}) + .await? + .into_inner(); + listed + .agents + .into_iter() + .map(convert::agent) + .collect::>() + .map_err(CallError::from) + }) + } + + fn create_agent(&self, hub: &HubId, draft: AgentDraft) -> AgentsFuture { + self.call(hub, |client| async move { + let input = convert::agent_input(draft.validate().map_err(HubAgentsError::from)?); + let made = client.agents().create_agent(input).await?.into_inner(); + Ok(convert::agent(made)?) + }) + } + + fn update_agent( + &self, + hub: &HubId, + agent: &AgentId, + draft: AgentDraft, + ) -> AgentsFuture { + let id = agent.to_string(); + self.call(hub, |client| async move { + let input = convert::agent_input(draft.validate().map_err(HubAgentsError::from)?); + let request = v2::UpdateAgentRequest { + id, + agent: Some(input), + }; + let saved = client.agents().update_agent(request).await?.into_inner(); + Ok(convert::agent(saved)?) + }) + } + + fn delete_agent(&self, hub: &HubId, agent: &AgentId) -> AgentsFuture<()> { + let id = agent.to_string(); + self.call(hub, |client| async move { + client.agents().delete_agent(v2::IdRequest { id }).await?; + Ok(()) + }) + } + + fn providers(&self, hub: &HubId) -> AgentsFuture> { + self.call(hub, |client| async move { + let listed = client + .agents() + .list_providers(v2::Empty {}) + .await? + .into_inner(); + listed + .providers + .into_iter() + .map(convert::provider) + .collect::>() + .map_err(CallError::from) + }) + } + + fn create_provider(&self, hub: &HubId, draft: ProviderDraft) -> AgentsFuture { + self.call(hub, |client| async move { + let input = convert::provider_input(draft.validate().map_err(HubAgentsError::from)?); + let made = client.agents().create_provider(input).await?.into_inner(); + Ok(convert::provider(made)?) + }) + } + + fn update_provider( + &self, + hub: &HubId, + provider: &ProviderId, + draft: ProviderDraft, + ) -> AgentsFuture { + let id = provider.to_string(); + self.call(hub, |client| async move { + let input = convert::provider_input(draft.validate().map_err(HubAgentsError::from)?); + let request = v2::UpdateProviderRequest { + id, + provider: Some(input), + }; + let saved = client.agents().update_provider(request).await?.into_inner(); + Ok(convert::provider(saved)?) + }) + } + + fn delete_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture<()> { + let id = provider.to_string(); + self.call(hub, |client| async move { + client + .agents() + .delete_provider(v2::IdRequest { id }) + .await?; + Ok(()) + }) + } + + fn test_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture { + let id = provider.to_string(); + self.call(hub, |client| async move { + let request = v2::TestProviderRequest { + target: Some(v2::test_provider_request::Target::ProviderId(id)), + }; + let tested = client.agents().test_provider(request).await?.into_inner(); + Ok(ProviderTest { + ok: tested.ok, + message: tested.message, + models: tested.models, + }) + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/agents/convert.rs b/crates/hub-io/src/agents/convert.rs new file mode 100644 index 00000000..67dde92e --- /dev/null +++ b/crates/hub-io/src/agents/convert.rs @@ -0,0 +1,129 @@ +//! Agent, provider and server records of the hub protocol as the records of the model. + +use nocterm_hub_agents_model::{ + AgentDraft, AgentId, AgentKey, ApprovalMode, HubAgent, HubAgentsError, HubServer, Provider, + ProviderDraft, ProviderId, ProviderKind, ProviderScope, ServerId, ServerStatus, +}; +use nocterm_hub_proto::v2; + +fn malformed(_: T) -> HubAgentsError { + HubAgentsError::Malformed +} + +pub(super) fn approval(wire: i32) -> ApprovalMode { + match v2::ApprovalMode::try_from(wire) { + Ok(v2::ApprovalMode::Ask) => ApprovalMode::Ask, + _ => ApprovalMode::Auto, + } +} + +pub(super) fn approval_to_wire(mode: ApprovalMode) -> i32 { + match mode { + ApprovalMode::Auto => v2::ApprovalMode::Auto, + ApprovalMode::Ask => v2::ApprovalMode::Ask, + } + .into() +} + +fn server_ids(ids: Vec) -> Result, HubAgentsError> { + ids.into_iter() + .map(|id| ServerId::new(id).map_err(malformed)) + .collect() +} + +pub(super) fn agent(wire: v2::Agent) -> Result { + Ok(HubAgent { + id: AgentId::new(wire.id).map_err(malformed)?, + name: wire.name, + instructions: wire.instructions, + provider: ProviderId::new(wire.provider_id).map_err(malformed)?, + model: wire.model, + models: wire.models, + approval: approval(wire.approval), + servers: server_ids(wire.server_ids)?, + }) +} + +/// A validated draft as the hub takes it. +pub(super) fn agent_input(draft: AgentDraft) -> v2::AgentInput { + v2::AgentInput { + name: draft.name, + instructions: draft.instructions, + provider_id: draft + .provider + .map(|provider| provider.to_string()) + .unwrap_or_default(), + model: draft.model, + models: draft.models, + approval: approval_to_wire(draft.approval), + server_ids: draft.servers.iter().map(ToString::to_string).collect(), + max_steps: 0, + } +} + +pub(super) fn provider(wire: v2::Provider) -> Result { + Ok(Provider { + id: ProviderId::new(wire.id).map_err(malformed)?, + scope: match v2::Scope::try_from(wire.scope) { + Ok(v2::Scope::Server) => ProviderScope::Hub, + _ => ProviderScope::Account, + }, + kind: match v2::ProviderKind::try_from(wire.kind) { + Ok(v2::ProviderKind::Anthropic) => ProviderKind::Anthropic, + _ => ProviderKind::OpenAi, + }, + name: wire.name, + base_url: wire.base_url, + models: wire.models, + has_key: wire.has_key, + editable: wire.editable, + }) +} + +/// A provider of the account's own, as the hub takes it. The key goes once and is never read +/// back. +pub(super) fn provider_input(draft: ProviderDraft) -> v2::ProviderInput { + v2::ProviderInput { + scope: v2::Scope::Account.into(), + kind: match draft.kind { + ProviderKind::OpenAi => v2::ProviderKind::Openai, + ProviderKind::Anthropic => v2::ProviderKind::Anthropic, + } + .into(), + name: draft.name, + base_url: draft.base_url, + models: draft.models, + api_key: draft + .api_key + .map(|key| key.expose().to_owned()) + .unwrap_or_default(), + } +} + +pub(super) fn agent_key(wire: v2::AgentKey) -> AgentKey { + AgentKey { + public_key: wire.public_key, + authorized_line: wire.authorized_line, + fingerprint: wire.fingerprint, + } +} + +pub(crate) fn server(wire: v2::Server) -> Result { + Ok(HubServer { + id: ServerId::new(wire.id).map_err(malformed)?, + name: wire.name, + host: wire.host, + port: u16::try_from(wire.port).map_err(malformed)?, + user: wire.user, + host_key_fingerprint: wire.host_key_fingerprint, + status: match v2::ServerStatus::try_from(wire.status) { + Ok(v2::ServerStatus::Ready) => ServerStatus::Ready, + Ok(v2::ServerStatus::NoAccess) => ServerStatus::NoAccess, + Ok(v2::ServerStatus::HostKeyChanged) => ServerStatus::HostKeyChanged, + Ok(v2::ServerStatus::Unreachable) => ServerStatus::Unreachable, + Ok(v2::ServerStatus::KeyRotated) => ServerStatus::KeyRotated, + _ => ServerStatus::Unchecked, + }, + status_message: wire.status_message, + }) +} diff --git a/crates/hub-io/src/agents/servers.rs b/crates/hub-io/src/agents/servers.rs new file mode 100644 index 00000000..af84f0a2 --- /dev/null +++ b/crates/hub-io/src/agents/servers.rs @@ -0,0 +1,137 @@ +//! The agent key and agent servers of the account, over `ServerService`. + +use nocterm_hub_agents_model::{ + AgentKey, AgentsFuture, HubAgentsError, HubChange, HubChangeHints, HubChanges, HubServer, + HubServers, NewServer, ServerId, ServerRemoval, +}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; + +use super::{CallError, HubAgentsLink, convert}; + +impl HubServers for HubAgentsLink { + fn agent_key(&self, hub: &HubId) -> AgentsFuture { + self.call(hub, |client| async move { + let key = client.servers().get_agent_key(v2::Empty {}).await?; + Ok(convert::agent_key(key.into_inner())) + }) + } + + fn rotate_agent_key(&self, hub: &HubId) -> AgentsFuture { + self.call(hub, |client| async move { + let key = client.servers().rotate_agent_key(v2::Empty {}).await?; + Ok(convert::agent_key(key.into_inner())) + }) + } + + fn servers(&self, hub: &HubId) -> AgentsFuture> { + self.call(hub, |client| async move { + let listed = client.servers().list_servers(v2::Empty {}).await?; + listed + .into_inner() + .servers + .into_iter() + .map(convert::server) + .collect::>() + .map_err(CallError::from) + }) + } + + fn add_server(&self, hub: &HubId, server: NewServer) -> AgentsFuture { + self.call(hub, move |client| async move { + let request = v2::AddServerRequest { + name: server.name, + host: server.host, + port: server.port.into(), + user: server.user, + host_key: server.host_key, + }; + let added = client.servers().add_server(request).await?; + Ok(convert::server(added.into_inner())?) + }) + } + + fn check_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let id = server.to_string(); + self.call(hub, |client| async move { + let checked = client.servers().check_server(v2::IdRequest { id }).await?; + Ok(convert::server(checked.into_inner())?) + }) + } + + fn remove_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let id = server.to_string(); + self.call(hub, |client| async move { + let request = v2::RemoveServerRequest { + id, + credential: None, + }; + let removed = client.servers().remove_server(request).await?.into_inner(); + Ok(ServerRemoval { + line_removed: removed.line_removed, + message: removed.message, + }) + }) + } +} + +/// Hints from one `Watch` of the account (no open chats) and from the chat lists this device +/// refreshed. +struct ChangeHints { + hub: tonic::Streaming, + here: tokio::sync::watch::Receiver, +} + +/// The hint of an event of the account's stream, if it is one. +fn hint_of(event: v2::watch_event::Event) -> Option { + use v2::watch_event::Event; + match event { + Event::Chat(_) | Event::ChatDeleted(_) | Event::Resync(_) => Some(HubChange::Chats), + Event::Account(changed) => match changed.list() { + v2::AccountList::Agents | v2::AccountList::Providers => Some(HubChange::Agents), + v2::AccountList::Servers => Some(HubChange::Servers), + v2::AccountList::Unspecified => None, + }, + Event::Item(_) | Event::Draft(_) | Event::Ready(_) => None, + } +} + +impl HubChangeHints for ChangeHints { + fn next( + &mut self, + ) -> futures::future::BoxFuture<'_, Result, HubAgentsError>> { + Box::pin(async move { + loop { + tokio::select! { + changed = self.here.changed() => { + if changed.is_ok() { + return Ok(Some(HubChange::Chats)); + } + } + message = self.hub.message() => match message { + Ok(Some(event)) => { + if let Some(hint) = event.event.and_then(hint_of) { + return Ok(Some(hint)); + } + } + Ok(None) | Err(_) => return Ok(None), + }, + } + } + }) + } +} + +impl HubChanges for HubAgentsLink { + fn watch(&self, hub: &HubId) -> AgentsFuture> { + let here = self.hints.subscribe(); + self.call(hub, |client| async move { + let request = v2::WatchRequest { + chats_after_rev: 0, + open: Vec::new(), + }; + let hub = client.chats().watch(request).await?.into_inner(); + Ok(Box::new(ChangeHints { hub, here }) as Box) + }) + } +} diff --git a/crates/hub-io/src/agents/tests.rs b/crates/hub-io/src/agents/tests.rs new file mode 100644 index 00000000..7186ad42 --- /dev/null +++ b/crates/hub-io/src/agents/tests.rs @@ -0,0 +1,61 @@ +use nocterm_hub_agents_model::{AgentDraft, ApprovalMode, ProviderId, ServerStatus}; +use nocterm_hub_proto::v2; + +use super::convert; + +#[test] +fn an_agent_draft_goes_to_the_hub_with_its_mode_and_servers() { + let draft = AgentDraft { + name: "ops".into(), + provider: Some(ProviderId::new("p1").unwrap()), + models: vec!["m1".into()], + approval: ApprovalMode::Ask, + servers: vec![nocterm_hub_agents_model::ServerId::new("s1").unwrap()], + ..AgentDraft::default() + } + .validate() + .unwrap(); + let input = convert::agent_input(draft); + assert_eq!(input.provider_id, "p1"); + assert_eq!(input.model, "m1"); + assert_eq!(input.approval, i32::from(v2::ApprovalMode::Ask)); + assert_eq!(input.server_ids, ["s1"]); +} + +#[test] +fn an_agent_of_the_hub_reads_back_with_its_mode() { + let agent = convert::agent(v2::Agent { + id: "a1".into(), + name: "ops".into(), + provider_id: "p1".into(), + model: "m1".into(), + models: vec!["m1".into()], + approval: v2::ApprovalMode::Ask.into(), + ..Default::default() + }) + .unwrap(); + assert_eq!(agent.approval, ApprovalMode::Ask); + assert!(convert::agent(v2::Agent::default()).is_err()); +} + +#[test] +fn a_server_reads_back_with_its_status() { + let server = convert::server(v2::Server { + id: "s1".into(), + host: "prod".into(), + port: 22, + user: "root".into(), + status: v2::ServerStatus::KeyRotated.into(), + ..Default::default() + }) + .unwrap(); + assert_eq!(server.status, ServerStatus::KeyRotated); + assert!( + convert::server(v2::Server { + id: "s".into(), + port: 70_000, + ..Default::default() + }) + .is_err() + ); +} diff --git a/crates/hub-io/src/cache.rs b/crates/hub-io/src/cache.rs new file mode 100644 index 00000000..bde922ba --- /dev/null +++ b/crates/hub-io/src/cache.rs @@ -0,0 +1,347 @@ +//! The encrypted copy of an account's keyset and vault kept on this device. +//! +//! One mode-0600 file per hub, `_.cache` beside the credential file. It holds only +//! what the hub holds: the sealed keyset, the sealed key envelope of the vault collection and +//! sealed objects, plus the non-secret facts needed to derive the unlock key again (the user +//! name, the way the key is derived and the hub's own identifier). Nothing secret in it opens +//! without the vault password, so it may sit on disk, and it lets the vault unlock while the hub +//! is unreachable. +//! Read and write share a 32 MiB byte limit. Personal-vault refresh publication +//! keeps the newest collection cursor and skips identical snapshots. Vault data +//! is shared immutably across readers; staging and parent sync use two physical +//! installation-owned workers, independent of the Slot's short authority lock. + +use std::collections::BTreeMap; +use std::io; +use std::path::PathBuf; +use std::sync::Arc; + +use nocterm_core::{ + bounded_io::BoundedBuffer, + fs::{AtomicPublication, BoundedReadError, FileError, read_regular_bounded}, +}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubAddress; +use serde::{Deserialize, Serialize}; + +use crate::credentials::{MAX_NAME_BYTES, file_stem, prepare_private_directory}; + +/// The cache holds sealed credentials of one account; anything larger is not ours. +const MAX_FILE_BYTES: usize = 32 * 1024 * 1024; +/// The format the file is written in. A newer file is left alone and not read. +const FORMAT: u32 = 2; + +/// Why the cache of a hub could not be read or kept. +#[derive(Debug, thiserror::Error)] +pub(crate) enum CacheError { + #[error("the account cache cannot be read")] + Read(#[from] BoundedReadError), + #[error("the account cache cannot be written")] + Write(#[from] FileError), + #[error("the account cache directory cannot be prepared")] + Prepare(#[source] io::Error), + #[error("the account cache is not valid")] + Format(#[source] serde_json::Error), + #[error("the account cache was written by a newer version")] + Newer, + #[error("the account cache exceeds its 32 MiB limit")] + Limit, + #[error("the hub address is too long for a file name")] + NameTooLong, + #[error("the account cache worker is unavailable")] + Worker, + #[error("the account cache worker failed")] + Join(#[from] tokio::task::JoinError), +} + +/// How the unlock key of the account's keyset is derived from what the user types. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "snake_case")] +pub(crate) enum UnlockMethod { + /// The login password of the account, bound to the hub and the user name. + Login { username: String }, + /// A vault passphrase of its own (accounts that sign in through a provider). + Passphrase, +} + +/// Bytes as base64 text. +mod bytes { + use base64::{Engine as _, engine::general_purpose::STANDARD}; + use serde::{Deserialize, Deserializer, Serializer}; + + pub(super) fn serialize(value: &[u8], out: S) -> Result { + out.serialize_str(&STANDARD.encode(value)) + } + + pub(super) fn deserialize<'de, D: Deserializer<'de>>(input: D) -> Result, D::Error> { + let text = String::deserialize(input)?; + STANDARD.decode(text).map_err(serde::de::Error::custom) + } +} + +/// The sealed keyset as the hub stores it. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct KeysetCopy { + pub version: u64, + pub kdf: String, + #[serde(with = "bytes")] + pub salt: Vec, + #[serde(with = "bytes")] + pub wrapped_account_key: Vec, + #[serde(with = "bytes")] + pub recovery_wrapped_account_key: Vec, + /// Which password the unlock key comes from, as the hub's `UnlockKind` number. + pub unlock_kind: i32, +} + +impl KeysetCopy { + pub(crate) fn of(record: &v2::Keyset) -> Self { + Self { + version: record.version, + kdf: record.kdf.clone(), + salt: record.salt.clone(), + wrapped_account_key: record.wrapped_account_key.clone(), + recovery_wrapped_account_key: record.recovery_wrapped_account_key.clone(), + unlock_kind: record.unlock_kind, + } + } + + pub(crate) fn record(&self) -> v2::Keyset { + v2::Keyset { + version: self.version, + kdf: self.kdf.clone(), + salt: self.salt.clone(), + wrapped_account_key: self.wrapped_account_key.clone(), + updated_at_ms: 0, + recovery_wrapped_account_key: self.recovery_wrapped_account_key.clone(), + unlock_kind: self.unlock_kind, + } + } +} + +/// One sealed object (or tombstone) of the vault collection. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct ObjectCopy { + pub version: u64, + pub kind: String, + #[serde(with = "bytes")] + pub ciphertext: Vec, + pub deleted: bool, +} + +/// The vault collection: its key envelope and every object read so far. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct VaultCopy { + pub collection_id: String, + #[serde(with = "bytes")] + pub key_envelope: Vec, + /// The stream version up to which `objects` is complete. + pub cursor: u64, + pub objects: BTreeMap, +} + +/// What the cache file holds. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct AccountCache { + pub v: u32, + /// The hub's own identifier, which the login key is derived with. + pub hub_uid: String, + pub account_id: String, + pub method: UnlockMethod, + pub keyset: KeysetCopy, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "shared_vault" + )] + pub vault: Option>, + /// The keys were created on this device and the person has not yet confirmed saving the + /// recovery key shown then. A fact about the device, not a secret: the next time the keys + /// open, a new recovery key is offered. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub recovery_unconfirmed: bool, +} + +impl AccountCache { + /// A delayed refresh never replaces a more recent personal-vault snapshot. + /// Equal snapshots need neither another memory publication nor a disk write. + pub(crate) fn accept_shared_vault(&mut self, next: Arc) -> bool { + if self.vault.as_ref().is_some_and(|held| { + held == &next || (held.collection_id == next.collection_id && held.cursor > next.cursor) + }) { + return false; + } + self.vault = Some(next); + true + } + + pub(crate) fn new( + hub_uid: String, + account_id: String, + method: UnlockMethod, + record: &v2::Keyset, + ) -> Self { + Self { + v: FORMAT, + hub_uid, + account_id, + method, + keyset: KeysetCopy::of(record), + vault: None, + recovery_unconfirmed: false, + } + } +} + +/// Sharing is an in-memory ownership detail; the existing file representation stays plain. +mod shared_vault { + use super::{Arc, VaultCopy}; + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + + pub(super) fn serialize( + value: &Option>, + out: S, + ) -> Result { + value.as_deref().serialize(out) + } + + pub(super) fn deserialize<'de, D: Deserializer<'de>>( + input: D, + ) -> Result>, D::Error> { + Option::::deserialize(input).map(|value| value.map(Arc::new)) + } +} + +/// The cache file of one hub. It keeps no state: callers decide when to read and write. +#[derive(Clone, Debug)] +pub(crate) struct CacheFile { + path: PathBuf, + work: Arc, +} + +/// One installation shares two physical filesystem jobs. A cancelled waiter +/// releases admission; a running job retains its permit until the I/O finishes. +#[derive(Debug)] +pub(crate) struct CacheWork { + runtime: Option, + slots: Arc, + operations: Arc, +} + +impl CacheWork { + pub(crate) fn new(runtime: Option) -> Arc { + Arc::new(Self { + runtime, + slots: Arc::new(tokio::sync::Semaphore::new(2)), + operations: Arc::new(tokio::sync::Semaphore::new(4)), + }) + } + + pub(crate) async fn operation(&self) -> Result { + self.operations + .clone() + .acquire_owned() + .await + .map_err(|_| CacheError::Worker) + } + + pub(crate) async fn run( + &self, + work: impl FnOnce() -> Result + Send + 'static, + ) -> Result { + let permit = self + .slots + .clone() + .acquire_owned() + .await + .map_err(|_| CacheError::Worker)?; + let runtime = self + .runtime + .clone() + .or_else(|| tokio::runtime::Handle::try_current().ok()) + .ok_or(CacheError::Worker)?; + runtime + .spawn_blocking(move || { + let _permit = permit; + work() + }) + .await? + } +} + +impl CacheFile { + pub(crate) fn new(root: &std::path::Path, address: &HubAddress) -> Result { + let stem = file_stem(address); + if stem.len() > MAX_NAME_BYTES { + return Err(CacheError::NameTooLong); + } + Ok(Self { + path: root.join(format!("{stem}.cache")), + work: CacheWork::new(None), + }) + } + + pub(crate) fn with_work(mut self, work: Arc) -> Self { + self.work = work; + self + } + + pub(crate) fn work(&self) -> &Arc { + &self.work + } + + /// The cached account; `None` when nothing was kept for the hub. + pub(crate) fn load(&self) -> Result, CacheError> { + let bytes = match read_regular_bounded(&self.path, MAX_FILE_BYTES) { + Ok(bytes) => bytes, + Err(error) if error.io_kind() == Some(io::ErrorKind::NotFound) => return Ok(None), + Err(error) => return Err(error.into()), + }; + #[derive(Deserialize)] + struct Header { + v: u32, + } + let header: Header = serde_json::from_slice(&bytes).map_err(CacheError::Format)?; + if header.v > FORMAT { + return Err(CacheError::Newer); + } + let cache: AccountCache = serde_json::from_slice(&bytes).map_err(CacheError::Format)?; + Ok(Some(cache)) + } + + /// Replaces the file atomically; it is created readable by the user only. + #[cfg(test)] + pub(crate) fn save(&self, cache: &AccountCache) -> Result<(), CacheError> { + self.stage(cache)?.publish()?; + Ok(()) + } + + pub(crate) fn stage(&self, cache: &AccountCache) -> Result { + if let Some(parent) = self.path.parent() { + prepare_private_directory(parent).map_err(CacheError::Prepare)?; + } + let mut bytes = BoundedBuffer::new(MAX_FILE_BYTES); + if let Err(error) = serde_json::to_writer(&mut bytes, cache) { + return Err(if bytes.exceeded() { + CacheError::Limit + } else { + CacheError::Format(error) + }); + } + Ok(AtomicPublication::stage_private( + &self.path, + &bytes.into_inner(), + )?) + } + + /// Removes the cache; no file is not an error. + pub(crate) fn erase(&self) -> io::Result<()> { + match std::fs::remove_file(&self.path) { + Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error), + _ => Ok(()), + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/cache/tests.rs b/crates/hub-io/src/cache/tests.rs new file mode 100644 index 00000000..ee7abf8b --- /dev/null +++ b/crates/hub-io/src/cache/tests.rs @@ -0,0 +1,106 @@ +use super::*; + +fn address() -> HubAddress { + "hub.example:7443".parse().unwrap() +} + +fn record() -> v2::Keyset { + v2::Keyset { + version: 3, + kdf: "argon2id;m=65536;t=3;p=4".into(), + salt: vec![1; 16], + wrapped_account_key: vec![2; 60], + updated_at_ms: 9, + recovery_wrapped_account_key: vec![5; 60], + unlock_kind: v2::UnlockKind::LoginPassword as i32, + } +} + +fn account() -> AccountCache { + let mut cache = AccountCache::new( + "hub-uid".into(), + "acc-1".into(), + UnlockMethod::Login { + username: "ann".into(), + }, + &record(), + ); + cache.vault = Some(Arc::new(VaultCopy { + collection_id: "c1".into(), + key_envelope: vec![7; 80], + cursor: 2, + objects: BTreeMap::from([( + "o1".into(), + ObjectCopy { + version: 1, + kind: "credential".into(), + ciphertext: vec![9; 40], + deleted: false, + }, + )]), + })); + cache +} + +#[test] +fn a_saved_account_reads_back_unchanged() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + assert!(file.load().unwrap().is_none()); + file.save(&account()).unwrap(); + assert_eq!(file.load().unwrap(), Some(account())); + assert_eq!(account().keyset.record().wrapped_account_key, vec![2; 60]); +} + +#[cfg(unix)] +#[test] +fn the_cache_is_private_and_holds_no_plain_text_of_the_secrets() { + use std::os::unix::fs::PermissionsExt as _; + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + file.save(&account()).unwrap(); + let path = dir.path().join("hub.example_7443.cache"); + let mode = std::fs::metadata(&path).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600); + let text = std::fs::read_to_string(path).unwrap(); + assert!(!text.contains("password") && !text.contains("secret")); +} + +#[test] +fn a_damaged_cache_is_reported_and_left_in_place() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + let path = dir.path().join("hub.example_7443.cache"); + std::fs::write(&path, b"{ not json").unwrap(); + assert!(matches!(file.load(), Err(CacheError::Format(_)))); + assert!(path.exists()); +} + +#[test] +fn a_cache_from_a_newer_version_is_not_read() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + std::fs::write(dir.path().join("hub.example_7443.cache"), br#"{"v":99}"#).unwrap(); + assert!(matches!(file.load(), Err(CacheError::Newer))); +} + +#[test] +fn erasing_a_missing_cache_is_not_an_error() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + file.erase().unwrap(); + file.save(&account()).unwrap(); + file.erase().unwrap(); + assert!(file.load().unwrap().is_none()); +} + +#[test] +fn an_oversized_cache_write_preserves_the_last_readable_account() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + file.save(&account()).unwrap(); + let mut oversized = account(); + oversized.hub_uid = "x".repeat(MAX_FILE_BYTES); + assert!(matches!(file.save(&oversized), Err(CacheError::Limit))); + assert_eq!(file.load().unwrap(), Some(account())); +} diff --git a/crates/hub-io/src/chats.rs b/crates/hub-io/src/chats.rs new file mode 100644 index 00000000..eee0b46c --- /dev/null +++ b/crates/hub-io/src/chats.rs @@ -0,0 +1,28 @@ +//! Hub chats as ordinary agent chats. +//! +//! A hub agent's chat runs on the hub; this module shows it through the neutral agent port of +//! `nocterm-ai`, so the desktop presents it with the same chat views as a local agent. +//! [`HubAgentConnector`] implements `AgentConnector` over `ChatService`: a session is a hub +//! chat (a new one is created by its first message under the session's id, a reopened one +//! reads its `History`), a prompt is `Send` and resolves when the +//! run that answers it ends, cancel is `Stop`, an approval request is a permission request +//! answered with `ResolveApproval`, and the chat's settings are configuration options: the model +//! and approval mode (`SetChatModel`) and one flag per server of the account, on while the chat +//! has it attached (`SetChatServers`; the agent signs in only to attached servers). +//! One `Watch` per open chat follows its items and the text the agent is writing; a turn +//! someone started on another device arrives as `SessionUpdate::Turn`. +//! [`HubChatRepository`] puts the hub's chats beside the local history of a hub window and +//! sends renames, pins and deletions of hub chats to the hub. + +mod connector; +mod hints; +mod items; +mod options; +mod repository; +mod state; +mod upload; +mod watch; + +pub use connector::HubAgentConnector; +pub(crate) use hints::ChatHints; +pub use repository::HubChatRepository; diff --git a/crates/hub-io/src/chats/connector.rs b/crates/hub-io/src/chats/connector.rs new file mode 100644 index 00000000..aa4b0276 --- /dev/null +++ b/crates/hub-io/src/chats/connector.rs @@ -0,0 +1,573 @@ +//! The hub connector: a hub agent's chats as the sessions of an agent connection. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::future::BoxFuture; +use nocterm_ai::model::{ + controls::ConfigOption, + diagnostic::ModelError, + ids::{AuthMethodId, SessionId}, + session::{ + AgentCapabilitiesSummary, PromptOutcome, PromptRequest, RestoreSession, SessionOpen, + SessionOpened, SetConfigOption, SetMode, + }, +}; +use nocterm_ai::{ + AgentCommands, AgentConnection, AgentConnector, AgentError, AgentEvent, AgentInfo, + CloseSessionOutcome, ConnectError, ConnectRequest, +}; +use nocterm_hub_agents_model::HubServer; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubError; +use tokio::runtime::Handle; + +use super::options::{Changed, ChatOptions}; +use super::state::ChatState; +use super::watch::Follow; +use crate::classify::classify; +use crate::devices::Captured; +use crate::links::HubLinks; +use crate::pool::Pool; + +/// The most history items a reopened chat reads at once. +const HISTORY_PAGE: u32 = 200; +/// The most items a reopened chat shows; older ones stay on the hub. +const MAX_HISTORY: usize = nocterm_ai::history::MAX_SAVED_ENTRIES; + +/// Connects chats of hub agents (launches with [`nocterm_ai::agents::HubAgentRef`]). +pub struct HubAgentConnector { + handle: Handle, + pool: Arc, +} + +impl HubAgentConnector { + pub fn new(links: &HubLinks) -> Self { + let (handle, pool) = links.agent_parts(); + Self { handle, pool } + } +} + +/// A failed hub call before it is told in the agent port's terms. +pub(super) enum CallFailure { + Client(ClientError), + Hub(HubError), + Refused(String), +} + +impl From for CallFailure { + fn from(status: tonic::Status) -> Self { + Self::Client(ClientError::from(status)) + } +} + +impl From for CallFailure { + fn from(error: HubError) -> Self { + Self::Hub(error) + } +} + +impl CallFailure { + fn into_agent(self, captured: &Captured) -> AgentError { + let error = match self { + Self::Refused(message) => return AgentError::Io(message), + Self::Client(ClientError::Status { + hub_code: Some(v2::ErrorCode::LimitReached), + message, + .. + }) => return AgentError::Io(format!("A limit of the hub is reached: {message}")), + Self::Client(error) => classify(captured.hub.address(), error), + Self::Hub(error) => error, + }; + AgentError::Io(error.to_string()) + } +} + +fn hub_failure(error: &HubError) -> AgentError { + AgentError::Io(error.to_string()) +} + +/// A client of the captured account with a fresh access token. +pub(super) async fn fresh(captured: &Captured) -> Result { + let client = captured.client.clone(); + captured + .observed + .during(client.ensure_fresh()) + .await? + .map_err(CallFailure::Client)?; + Ok(client) +} + +/// Runs `work` on the adapter's threads with a fresh client of the captured account. +pub(super) fn call( + handle: &Handle, + captured: &Arc, + work: F, +) -> BoxFuture<'static, Result> +where + T: Send + 'static, + F: FnOnce(HubClient) -> Fut + Send + 'static, + Fut: std::future::Future> + Send + 'static, +{ + let captured = captured.clone(); + let task = crate::task::spawn(handle, async move { + let outcome = async { + let client = fresh(&captured).await?; + captured.observed.during(work(client)).await? + } + .await; + outcome.map_err(|failure| failure.into_agent(&captured)) + }); + Box::pin(async move { + task.await + .unwrap_or_else(|| Err(hub_failure(&HubError::Stale))) + }) +} + +impl AgentConnector for HubAgentConnector { + fn connect( + &self, + request: ConnectRequest, + ) -> BoxFuture<'static, Result> { + let Some(agent) = request.launch.hub.clone() else { + return Box::pin(async { + Err(AgentError::Unsupported(ModelError::Unsupported( + "a launch that is not a hub agent", + ))) + }); + }; + let captured = match Captured::new(self.pool.clone(), &agent.hub) { + Ok(captured) => Arc::new(captured), + Err(error) => return Box::pin(async move { Err(hub_failure(&error)) }), + }; + let handle = self.handle.clone(); + let name = request.launch.name.clone(); + let wanted = agent.agent.clone(); + let listed = call(&handle, &captured, |client| async move { + let agents = client.agents().list_agents(v2::Empty {}).await?; + let agent = agents + .into_inner() + .agents + .into_iter() + .find(|agent| agent.id == wanted) + .ok_or_else(|| { + CallFailure::Refused("The agent no longer exists on the hub.".into()) + })?; + // Without the servers a chat still works; it offers none until they are listed. + let servers = servers_of(&client).await.unwrap_or_default(); + Ok((agent, servers)) + }); + Box::pin(async move { + let (record, servers) = listed.await?; + let (events_sender, events) = async_channel::unbounded(); + let commands = Arc::new(HubChatCommands(Arc::new(Inner { + handle, + captured, + agent: record, + events: events_sender, + chats: Mutex::default(), + servers: Mutex::new(servers), + }))); + Ok(AgentConnection { + info: AgentInfo { + name, + version: String::new(), + capabilities: AgentCapabilitiesSummary { + image_prompts: true, + keeps_history: true, + own_tools: true, + }, + auth_methods: Vec::new(), + }, + commands, + events, + }) + }) + } +} + +pub(super) struct Inner { + pub(super) handle: Handle, + pub(super) captured: Arc, + agent: v2::Agent, + pub(super) events: async_channel::Sender, + chats: Mutex>, + /// The account's servers as last listed: a chat offers to attach each of them. + servers: Mutex>, +} + +/// The account's servers; a record the hub sent malformed is left out. +async fn servers_of(client: &HubClient) -> Result, CallFailure> { + let listed = client.servers().list_servers(v2::Empty {}).await?; + Ok(listed + .into_inner() + .servers + .into_iter() + .filter_map(|server| crate::agents::hub_server(server).ok()) + .collect()) +} + +/// A chat this connection shows. A new chat exists only here until its first message creates +/// it on the hub with the same id; then its `Watch` is followed. +struct OpenChat { + state: Arc>, + options: Arc>, + follow: Option, +} + +impl Drop for OpenChat { + fn drop(&mut self) { + if let Some(follow) = &self.follow { + follow.abort(); + } + } +} + +/// The commands of one hub agent's connection; every session is a chat of the agent. +struct HubChatCommands(Arc); + +fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(PoisonError::into_inner) +} + +impl Inner { + fn chats(&self) -> MutexGuard<'_, HashMap> { + lock(&self.chats) + } + + /// What `options` shows as the chat's configuration, with the account's servers. + pub(super) fn config(&self, options: &ChatOptions) -> Vec { + options.config(&lock(&self.servers)) + } + + /// Lists the account's servers again; `false` when the hub did not answer. + pub(super) async fn list_servers(&self) -> bool { + let listed = async { + let client = fresh(&self.captured).await?; + self.captured.observed.during(servers_of(&client)).await? + }; + match listed.await { + Ok(servers) => { + *lock(&self.servers) = servers; + true + } + Err(_) => false, + } + } + + /// Shows `chat` from `state` on, returning its options; it is followed once + /// [`Self::follow`] is called. + fn track(&self, chat: &v2::Chat, state: ChatState) -> Vec { + let options = ChatOptions::of(chat, &self.agent.models); + let shown = self.config(&options); + self.chats().insert( + chat.id.clone(), + OpenChat { + state: Arc::new(Mutex::new(state)), + options: Arc::new(Mutex::new(options)), + follow: None, + }, + ); + shown + } + + /// Follows the hub's chat `id`, telling the person what its state left open. Following + /// twice is harmless. + fn follow(self: &Arc, id: &str) { + let mut chats = self.chats(); + let Some(open) = chats.get_mut(id).filter(|open| open.follow.is_none()) else { + return; + }; + let (opening, running) = { + let state = lock(&open.state); + (state.open_approvals(), state.running()) + }; + let follow = Follow { + inner: Arc::downgrade(self), + chat: id.to_owned(), + state: open.state.clone(), + options: open.options.clone(), + }; + open.follow = Some( + self.handle + .spawn(follow.run(opening, running)) + .abort_handle(), + ); + } + + /// The chat's state, its options, and whether it exists on the hub yet. + fn open_chat( + &self, + chat: &str, + ) -> Option<(Arc>, Arc>, bool)> { + self.chats().get(chat).map(|open| { + ( + open.state.clone(), + open.options.clone(), + open.follow.is_some(), + ) + }) + } +} + +impl Inner { + /// Follows `id` once its first message created it on the hub. + fn follow_if_created(self: &Arc, id: &str, created: bool) { + if created { + self.follow(id); + } + } +} + +/// Creates the chat `id` of `agent` on the hub with the model, mode and servers it was shown +/// with. Creating it again (a retried first message) returns it. +async fn create( + client: &HubClient, + agent: String, + id: &str, + options: ChatOptions, +) -> Result<(), CallFailure> { + let (model, approval) = options.model(); + let chat = client + .chats() + .create_chat(v2::CreateChatRequest { + agent_id: agent, + chat_id: id.to_owned(), + model: model.clone(), + server_ids: options.servers(), + ..Default::default() + }) + .await? + .into_inner(); + if chat.approval != approval { + let request = v2::SetChatModelRequest { + id: id.to_owned(), + model, + approval, + }; + client.chats().set_chat_model(request).await?; + } + Ok(()) +} + +/// Reads the chat's history, oldest first, up to [`MAX_HISTORY`] items. +async fn history(client: &HubClient, chat: &str) -> Result, CallFailure> { + let mut pages = Vec::new(); + let mut before = 0; + let mut count = 0; + loop { + let page = client + .chats() + .history(v2::HistoryRequest { + chat_id: chat.to_owned(), + before_seq: before, + limit: HISTORY_PAGE, + }) + .await? + .into_inner(); + count += page.items.len(); + let first = page.items.first().map(|item| item.seq); + let more = page.more; + pages.push(page.items); + match first { + Some(first) if more && count < MAX_HISTORY => before = first, + _ => break, + } + } + Ok(pages.into_iter().rev().flatten().collect()) +} + +impl AgentCommands for HubChatCommands { + /// A new chat is shown with the agent's model and mode at once; its first message creates + /// it on the hub under the same id, so opening a chat leaves nothing on the hub. + fn new_session( + &self, + _request: SessionOpen, + ) -> BoxFuture<'static, Result> { + let inner = &self.0; + let chat = v2::Chat { + id: uuid::Uuid::new_v4().to_string(), + agent_id: inner.agent.id.clone(), + model: inner.agent.model.clone(), + approval: inner.agent.approval, + ..Default::default() + }; + let options = inner.track(&chat, ChatState::default()); + Box::pin(async move { Ok(SessionOpened::new(chat.id).configuration(options)) }) + } + + fn restore_session( + &self, + request: RestoreSession, + ) -> BoxFuture<'static, Result> { + if request.fork { + return Box::pin(async { + Err(AgentError::RestoreUnavailable( + "A hub chat cannot be copied.".into(), + )) + }); + } + let inner = self.0.clone(); + let id = request.session.to_string(); + let read = call(&inner.handle, &inner.captured, |client| async move { + let chat = client + .chats() + .get_chat(v2::IdRequest { id: id.clone() }) + .await? + .into_inner(); + let items = history(&client, &id).await?; + Ok((chat, items)) + }); + Box::pin(async move { + let (chat, items) = read.await?; + let mut state = ChatState::default(); + let updates = items.iter().flat_map(|item| state.replay(item)).collect(); + let options = inner.track(&chat, state); + inner.follow(&chat.id); + Ok(SessionOpened::new(chat.id) + .configuration(options) + .history(updates)) + }) + } + + fn prompt( + &self, + request: PromptRequest, + ) -> BoxFuture<'static, Result> { + let inner = self.0.clone(); + let chat = request.session.to_string(); + let Some((state, options, created)) = inner.open_chat(&chat) else { + return Box::pin(async { Err(AgentError::Io("The chat is not open.".into())) }); + }; + let client_message = uuid::Uuid::new_v4().to_string(); + let (done, answer) = futures::channel::oneshot::channel(); + lock(&state).expect(client_message.clone(), done); + let creation = (!created).then(|| (inner.agent.id.clone(), lock(&options).clone())); + let id = chat.clone(); + let sent = call(&inner.handle, &inner.captured, move |client| async move { + if let Some((agent, options)) = creation { + create(&client, agent, &chat, options).await?; + } + let (text, attachment_ids) = + super::upload::prepare(&client, &chat, request.content).await?; + let request = v2::SendRequest { + chat_id: chat, + client_message_id: client_message, + text, + attachment_ids, + }; + client.chats().send(request).await?; + Ok(()) + }); + Box::pin(async move { + let outcome = sent.await; + // Once created, the chat is followed even when the message did not go. + inner.follow_if_created(&id, created || outcome.is_ok()); + if let Err(error) = outcome { + lock(&state).abandon(); + return Err(error); + } + answer.await.unwrap_or_else(|_| { + Err(AgentError::Io( + "The chat closed before the agent answered.".into(), + )) + }) + }) + } + + fn cancel(&self, session: SessionId) { + let id = session.to_string(); + let stopping = call(&self.0.handle, &self.0.captured, |client| async move { + client.chats().stop(v2::IdRequest { id }).await?; + Ok(()) + }); + self.0.handle.spawn(async move { + if let Err(error) = stopping.await { + tracing::warn!(%error, "the hub did not stop the chat's agent"); + } + }); + } + + fn set_mode(&self, _request: SetMode) -> BoxFuture<'static, Result<(), AgentError>> { + Box::pin(async { + Err(AgentError::Unsupported(ModelError::Unsupported( + "modes of a hub agent", + ))) + }) + } + + /// Chooses the model or the approval mode, or attaches or detaches a server. A chat its + /// first message has not created keeps the choice until it does. + fn set_config_option( + &self, + request: SetConfigOption, + ) -> BoxFuture<'static, Result, AgentError>> { + let inner = self.0.clone(); + let chat = request.session.to_string(); + let Some((current, created)) = inner + .chats() + .get(&chat) + .map(|open| (open.options.clone(), open.follow.is_some())) + else { + return Box::pin(async { Err(AgentError::Io("The chat is not open.".into())) }); + }; + let mut chosen = lock(¤t).clone(); + let changed = match chosen.choose(request.config.as_str(), &request.setting) { + Ok(changed) => changed, + Err(refused) => return Box::pin(async move { Err(AgentError::Io(refused.0.into())) }), + }; + if !created { + // Not on the hub yet: its first message creates it with these. + let shown = inner.config(&chosen); + *lock(¤t) = chosen; + return Box::pin(async move { Ok(shown) }); + } + let saved = call(&inner.handle, &inner.captured, move |client| async move { + let mut chats = client.chats(); + let stored = match changed { + Changed::Model => { + let (model, approval) = chosen.model(); + let request = v2::SetChatModelRequest { + id: chat, + model, + approval, + }; + chats.set_chat_model(request).await? + } + Changed::Servers => { + let request = v2::SetChatServersRequest { + id: chat, + server_ids: chosen.servers(), + }; + chats.set_chat_servers(request).await? + } + }; + Ok(stored.into_inner()) + }); + Box::pin(async move { + let chat = saved.await?; + let stored = lock(¤t).stored(&chat); + let shown = inner.config(&stored); + *lock(¤t) = stored; + Ok(shown) + }) + } + + fn authenticate(&self, _method: AuthMethodId) -> BoxFuture<'static, Result<(), AgentError>> { + Box::pin(async { Ok(()) }) + } + + fn close_session( + &self, + session: SessionId, + ) -> BoxFuture<'static, Result> { + self.0.chats().remove(session.as_str()); + Box::pin(async { Ok(CloseSessionOutcome::Closed) }) + } + + fn shutdown(&self) { + self.0.chats().clear(); + self.0.events.close(); + } +} diff --git a/crates/hub-io/src/chats/hints.rs b/crates/hub-io/src/chats/hints.rs new file mode 100644 index 00000000..407e6137 --- /dev/null +++ b/crates/hub-io/src/chats/hints.rs @@ -0,0 +1,22 @@ +//! Hints that a hub's chat list changed on this device: a refreshed list that differs from the +//! kept one. Watchers of the chat list hear them beside the hub's own changes. + +use tokio::sync::watch; + +pub(crate) struct ChatHints(watch::Sender); + +impl Default for ChatHints { + fn default() -> Self { + Self(watch::channel(0).0) + } +} + +impl ChatHints { + pub(crate) fn raise(&self) { + self.0.send_modify(|count| *count = count.wrapping_add(1)); + } + + pub(crate) fn subscribe(&self) -> watch::Receiver { + self.0.subscribe() + } +} diff --git a/crates/hub-io/src/chats/items.rs b/crates/hub-io/src/chats/items.rs new file mode 100644 index 00000000..922cdb39 --- /dev/null +++ b/crates/hub-io/src/chats/items.rs @@ -0,0 +1,105 @@ +//! Chat items of the hub protocol as the neutral session updates a chat shows. + +use nocterm_ai::model::{ + content::Content, + ids::ToolId, + session::SessionUpdate, + tools::{ToolChanges, ToolKind, ToolPatch, ToolRecord, ToolStatus}, + usage::TokenTotals, +}; +use nocterm_hub_proto::v2; + +/// The prompt of a person's message: its text, and the names of its attachments. +pub(super) fn prompt(message: &v2::UserMessage) -> Vec { + let mut content = Vec::with_capacity(1 + message.attachments.len()); + if !message.text.is_empty() { + content.push(Content::text(message.text.clone())); + } + content.extend( + message + .attachments + .iter() + .map(|attachment| Content::text(format!("[{}]", attachment.name))), + ); + content +} + +fn kind_of(call: &v2::ToolCall) -> ToolKind { + match call.tool.as_str() { + "server.run" => ToolKind::Execute, + "server.read_file" => ToolKind::Read, + "server.write_file" => ToolKind::Edit, + "server.send_file" => ToolKind::Fetch, + _ if call.read_only => ToolKind::Read, + _ => ToolKind::Other, + } +} + +pub(super) fn tool_call(call: &v2::ToolCall) -> SessionUpdate { + let arguments = serde_json::from_str(&call.arguments_json).ok(); + let title = if call.title.is_empty() { + call.tool.clone() + } else { + call.title.clone() + }; + SessionUpdate::ToolCall(Box::new( + ToolRecord::new(ToolId::new(call.call_id.clone()), title) + .name(call.tool.clone()) + .kind(kind_of(call)) + .status(ToolStatus::Running) + .raw_input(arguments), + )) +} + +pub(super) fn tool_result(result: &v2::ToolResult) -> SessionUpdate { + let mut text = String::new(); + for part in &result.content { + match &part.part { + Some(v2::content_part::Part::Text(value)) => text.push_str(value), + Some(v2::content_part::Part::File(file)) => { + text.push_str(&format!("\n[{}]", file.name)); + } + None => {} + } + } + if result.truncated { + text.push_str("\n… (output cut to the hub's limit)"); + } + if result.unknown_outcome { + text.push_str( + "\nThe run was interrupted while this ran; whether it took effect is unknown.", + ); + } + let failed = result.is_error || result.unknown_outcome; + let output = serde_json::json!({ + "exit_code": result.exit_code, + "truncated": result.truncated, + }); + SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + ToolId::new(result.call_id.clone()), + ToolChanges::default() + .status(if failed { + ToolStatus::Failed + } else { + ToolStatus::Succeeded + }) + .content(vec![text.trim_start().to_owned().into()]) + .raw_output(output), + ))) +} + +/// What the end of a run adds to the transcript: its error, when it has one. +pub(super) fn run_error(ended: &v2::RunEnded) -> Option { + (!ended.error.is_empty()) + .then(|| SessionUpdate::AgentMessageChunk(Content::text(format!("\n\n⚠ {}", ended.error)))) +} + +pub(super) fn usage(ended: &v2::RunEnded) -> Option { + ended.usage.as_ref().map(|usage| { + TokenTotals::new( + usage.input_tokens + usage.output_tokens, + usage.input_tokens, + usage.output_tokens, + ) + }) +} diff --git a/crates/hub-io/src/chats/options.rs b/crates/hub-io/src/chats/options.rs new file mode 100644 index 00000000..81d2485e --- /dev/null +++ b/crates/hub-io/src/chats/options.rs @@ -0,0 +1,289 @@ +//! A hub chat's settings as the configuration options of its session: its model among the +//! agent's models, whether changing tools wait for an approval, and which of the account's +//! servers the chat has attached (one flag per server; the agent signs in only to those). + +use nocterm_ai::model::controls::{ConfigCategory, ConfigChoice, ConfigOption, ConfigSetting}; +use nocterm_hub_agents_model::HubServer; +use nocterm_hub_proto::v2; + +/// The configuration option that chooses the chat's model. +const MODEL: &str = "model"; +/// The configuration option that chooses whether changing tools wait for an approval. +const APPROVAL: &str = "approval"; +/// The prefix of the options that attach one server each: `server:`. +const SERVER: &str = "server:"; +const AUTO: &str = "auto"; +const ASK: &str = "ask"; + +/// What a choice changed, so the hub is told with the call that stores it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Changed { + /// The model or the approval mode (`SetChatModel`). + Model, + /// The attached servers (`SetChatServers`). + Servers, +} + +/// A choice the chat does not take. +#[derive(Debug, PartialEq, Eq)] +pub(super) struct Refused(pub(super) &'static str); + +/// The chat's settings as last stored on the hub, or as chosen for a chat its first message +/// has not created yet. +#[derive(Clone, Debug)] +pub(super) struct ChatOptions { + model: String, + approval: i32, + models: Vec, + title: String, + servers: Vec, +} + +impl ChatOptions { + pub(super) fn of(chat: &v2::Chat, models: &[String]) -> Self { + Self { + model: chat.model.clone(), + approval: chat.approval, + models: models.to_vec(), + title: chat.title.clone(), + servers: chat.server_ids.clone(), + } + } + + /// The same settings after the hub stored `chat`. + pub(super) fn stored(&self, chat: &v2::Chat) -> Self { + Self::of(chat, &self.models) + } + + pub(super) fn title(&self) -> &str { + &self.title + } + + /// The model and approval mode as `SetChatModel` takes them. + pub(super) fn model(&self) -> (String, i32) { + (self.model.clone(), self.approval) + } + + /// The ids of the attached servers, as `SetChatServers` takes them. + pub(super) fn servers(&self) -> Vec { + self.servers.clone() + } + + /// Whether `chat` shows other options than these. + pub(super) fn differs(&self, chat: &v2::Chat) -> bool { + chat.model != self.model + || chat.approval != self.approval + || chat.server_ids != self.servers + } + + /// The options of the chat, with a flag for each server of `offered`, by name. A server + /// attached but no longer offered stays attached and is not shown. + pub(super) fn config(&self, offered: &[HubServer]) -> Vec { + let mut options = self.model_options(); + let mut servers: Vec<&HubServer> = offered.iter().collect(); + servers.sort_by_cached_key(|server| (server.name.to_lowercase(), server.id.to_string())); + options.extend(servers.into_iter().map(|server| { + let id = server.id.to_string(); + let name = if server.name.is_empty() { + server.host.clone() + } else { + server.name.clone() + }; + let attached = self.servers.contains(&id); + ConfigOption::boolean(format!("{SERVER}{id}"), name, attached) + .description(address(server)) + .category(ConfigCategory::Server) + })); + options + } + + fn model_options(&self) -> Vec { + let mut choices: Vec = self + .models + .iter() + .map(|model| ConfigChoice::new(model.clone(), model.clone())) + .collect(); + if !self.model.is_empty() && !self.models.contains(&self.model) { + choices.insert(0, ConfigChoice::new(self.model.clone(), self.model.clone())); + } + let ask = v2::ApprovalMode::try_from(self.approval) == Ok(v2::ApprovalMode::Ask); + vec![ + ConfigOption::select(MODEL, "Model", self.model.clone(), choices) + .category(ConfigCategory::Model), + ConfigOption::select( + APPROVAL, + "Approval", + if ask { ASK } else { AUTO }, + vec![ + ConfigChoice::new(AUTO, "Auto"), + ConfigChoice::new(ASK, "Ask before changes"), + ], + ) + .category(ConfigCategory::Mode), + ] + } + + /// Applies a choice of the option `config`, telling which call stores it. + pub(super) fn choose( + &mut self, + config: &str, + setting: &ConfigSetting, + ) -> Result { + if let Some(server) = config.strip_prefix(SERVER) { + let ConfigSetting::Boolean(attach) = setting else { + return Err(Refused("A server is attached or not.")); + }; + self.servers.retain(|id| id != server); + if *attach { + self.servers.push(server.to_owned()); + } + return Ok(Changed::Servers); + } + let ConfigSetting::Choice(choice) = setting else { + return Err(Refused("The option takes a choice.")); + }; + match config { + MODEL => self.model = choice.to_string(), + APPROVAL => { + self.approval = match choice.as_str() { + AUTO => v2::ApprovalMode::Auto.into(), + ASK => v2::ApprovalMode::Ask.into(), + _ => return Err(Refused("Unknown approval mode.")), + } + } + _ => return Err(Refused("The chat has no such option.")), + } + Ok(Changed::Model) + } +} + +/// `user@host`, the port when it is not 22, and what keeps agents out when something does. +fn address(server: &HubServer) -> String { + let mut text = format!("{}@{}", server.user, server.host); + if server.port != 22 { + text.push_str(&format!(":{}", server.port)); + } + let status = server.status; + if !status.is_ready() && status != nocterm_hub_agents_model::ServerStatus::Unchecked { + text.push_str(" · "); + text.push_str(status.label()); + } + text +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_hub_agents_model::{ServerId, ServerStatus}; + + fn server(id: &str, name: &str, status: ServerStatus) -> HubServer { + HubServer { + id: ServerId::new(id.to_owned()).unwrap(), + name: name.to_owned(), + host: format!("{id}.example"), + port: 22, + user: "root".to_owned(), + host_key_fingerprint: String::new(), + status, + status_message: String::new(), + } + } + + fn chat(servers: &[&str]) -> ChatOptions { + let chat = v2::Chat { + model: "m1".into(), + server_ids: servers.iter().map(ToString::to_string).collect(), + ..Default::default() + }; + ChatOptions::of(&chat, &["m1".into(), "m2".into()]) + } + + fn flags(options: &[ConfigOption]) -> Vec<(String, String, bool)> { + options + .iter() + .filter(|option| option.category == Some(ConfigCategory::Server)) + .map(|option| { + let on = matches!( + option.kind, + nocterm_ai::model::controls::ConfigKind::Boolean(true) + ); + (option.name.clone(), option.description.clone().unwrap(), on) + }) + .collect() + } + + #[test] + fn every_server_of_the_account_is_a_flag_on_while_the_chat_has_it() { + let offered = [ + server("b", "Web", ServerStatus::Ready), + server("a", "build", ServerStatus::NoAccess), + ]; + let options = chat(&["b"]).config(&offered); + assert_eq!(options[0].category, Some(ConfigCategory::Model)); + assert_eq!( + flags(&options), + vec![ + ( + "build".into(), + "root@a.example · No access — reconnect".into(), + false + ), + ("Web".into(), "root@b.example".into(), true), + ] + ); + } + + #[test] + fn attaching_and_detaching_a_server_is_stored_with_set_chat_servers() { + let mut options = chat(&["b"]); + let attach = options.choose("server:a", &ConfigSetting::Boolean(true)); + assert_eq!(attach, Ok(Changed::Servers)); + assert_eq!(options.servers(), vec!["b".to_owned(), "a".to_owned()]); + options + .choose("server:b", &ConfigSetting::Boolean(false)) + .unwrap(); + assert_eq!(options.servers(), vec!["a".to_owned()]); + assert!( + options + .choose("server:a", &ConfigSetting::Choice("x".into())) + .is_err() + ); + } + + #[test] + fn the_model_and_the_approval_mode_are_stored_with_set_chat_model() { + let mut options = chat(&[]); + let model = options.choose("model", &ConfigSetting::Choice("m2".into())); + assert_eq!(model, Ok(Changed::Model)); + options + .choose("approval", &ConfigSetting::Choice("ask".into())) + .unwrap(); + assert_eq!( + options.model(), + ("m2".to_owned(), v2::ApprovalMode::Ask.into()) + ); + assert!( + options + .choose("approval", &ConfigSetting::Choice("never".into())) + .is_err() + ); + assert!( + options + .choose("other", &ConfigSetting::Boolean(true)) + .is_err() + ); + } + + #[test] + fn a_chat_stored_with_other_servers_differs() { + let options = chat(&["a"]); + let mut stored = v2::Chat { + model: "m1".into(), + server_ids: vec!["a".into()], + ..Default::default() + }; + assert!(!options.differs(&stored)); + stored.server_ids.clear(); + assert!(options.differs(&stored)); + } +} diff --git a/crates/hub-io/src/chats/repository.rs b/crates/hub-io/src/chats/repository.rs new file mode 100644 index 00000000..876946b7 --- /dev/null +++ b/crates/hub-io/src/chats/repository.rs @@ -0,0 +1,289 @@ +//! The history of a hub window: the local chats of its account and the hub's chats. +//! +//! Local chats stay in the local repository. Chats of hub agents live on the hub: the list +//! comes from `ListChats` and is kept in a private file per hub, so it shows at once after a +//! restart and while the hub cannot be reached; a hub chat's transcript comes from the hub +//! when the chat opens. Renaming, pinning and deleting a hub chat goes to the hub. A hub chat +//! started on this device keeps the id the chat had here; the file remembers which hub chat +//! that is. + +mod list; + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use nocterm_ai::agents::AgentLaunch; +use nocterm_ai::history::{ChatSummary, ContextId, SavedChat, SharedChat}; +use nocterm_ai::local_environment::{ChatScope, LocalEnvironment}; +use nocterm_ai::storage::{ + AgentStorageError, ChatRepository, PreparedChatBytes, PreparedChatLoad, StorageFuture, +}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use nocterm_hubs_model::source_identity::LocalAccountPartition; +use tokio::runtime::Handle; + +use self::list::{ChatList, Row}; +use super::ChatHints; +use super::connector::call; +use crate::devices::Captured; +use crate::links::HubLinks; +use crate::pool::Pool; + +/// The most chats of a hub the history lists. +const MAX_CHATS: u32 = 500; + +/// Puts the chats of hub agents beside the local history of hub windows. +pub struct HubChatRepository { + local: Arc, + handle: Handle, + pool: Arc, + root: PathBuf, + hints: Arc, + lists: Mutex>>>, +} + +fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// The hub whose account `scope` belongs to. +fn hub_of(scope: &ChatScope) -> Option { + match scope.environment() { + LocalEnvironment::HubAccount(LocalAccountPartition::LegacyConfigured(account)) => { + Some(account.hub().clone()) + } + _ => None, + } +} + +impl HubChatRepository { + /// Hub chats beside `local`; the lists are kept in private files under `root`. + pub fn new(links: &HubLinks, local: Arc, root: PathBuf) -> Self { + let (handle, pool) = links.agent_parts(); + Self { + local, + handle, + pool, + root, + hints: links.chat_hints(), + lists: Mutex::default(), + } + } + + fn list(&self, hub: &HubId) -> Arc> { + lock(&self.lists) + .entry(hub.clone()) + .or_insert_with(|| Arc::new(Mutex::new(ChatList::load(&self.root, hub)))) + .clone() + } + + /// Reads the hub's chats in the background; a changed list is kept and hinted, so the + /// history is read again. + fn refresh(&self, hub: &HubId) { + let Ok(captured) = Captured::new(self.pool.clone(), hub) else { + return; + }; + let captured = Arc::new(captured); + let list = self.list(hub); + let hints = self.hints.clone(); + let root = self.root.clone(); + let hub = hub.clone(); + let read = call(&self.handle, &captured, |client| async move { + let request = v2::ListChatsRequest { + cursor: String::new(), + limit: MAX_CHATS, + }; + Ok(client.chats().list_chats(request).await?.into_inner().chats) + }); + self.handle.spawn(async move { + let Ok(chats) = read.await else { + return; + }; + let changed = lock(&list).replace(chats.iter().map(Row::of).collect()); + if changed { + lock(&list).save(&root, &hub); + hints.raise(); + } + }); + } + + /// A hub chat as a saved chat without transcript: the session reopens on the hub. + fn hub_chat(&self, scope: &ChatScope, hub: &HubId, id: &str) -> Option { + let list = self.list(hub); + let list = lock(&list); + saved_chat(&list, scope, hub, id) + } + + /// Sends what changed of a hub chat's name or pin to the hub. + fn publish(&self, hub: &HubId, chat: &SharedChat) { + let header = &chat.metadata.header; + let list = self.list(hub); + let (id, rename, pin) = { + let mut list = lock(&list); + if let Some(session) = &chat.metadata.session_id { + list.remember(&header.id, session); + } + let Some(row) = list.row_for(&header.id).cloned() else { + return; + }; + let rename = header + .name + .clone() + .filter(|name| !name.is_empty() && *name != row.title); + let pin = (header.pinned != row.pinned).then_some(header.pinned); + list.apply(&row.id, rename.as_deref(), pin); + list.save(&self.root, hub); + (row.id, rename, pin) + }; + if rename.is_none() && pin.is_none() { + return; + } + let Ok(captured) = Captured::new(self.pool.clone(), hub) else { + return; + }; + let captured = Arc::new(captured); + let sent = call(&self.handle, &captured, move |client| async move { + if let Some(title) = rename { + let request = v2::RenameChatRequest { + id: id.clone(), + title, + }; + client.chats().rename_chat(request).await?; + } + if let Some(pinned) = pin { + client + .chats() + .pin_chat(v2::PinChatRequest { id, pinned }) + .await?; + } + Ok(()) + }); + self.handle.spawn(async move { + if let Err(error) = sent.await { + tracing::warn!(%error, "the hub did not take the chat's new name or pin"); + } + }); + } +} + +impl ChatRepository for HubChatRepository { + fn prepare_load_in( + &self, + scope: ChatScope, + id: String, + ) -> StorageFuture> { + let Some(hub) = hub_of(&scope) else { + return self.local.prepare_load_in(scope, id); + }; + let Some(chat) = self.hub_chat(&scope, &hub, &id) else { + return self.local.prepare_load_in(scope, id); + }; + Box::pin(async move { + let bytes = nocterm_ai::history::encode(&chat)?; + let prepared = PreparedChatBytes::new(bytes, scope, id, Some(chat.version))?; + Ok(Box::new(prepared) as Box) + }) + } + + fn catalog_in(&self, scope: ChatScope) -> StorageFuture> { + let local = self.local.catalog_in(scope.clone()); + let Some(hub) = hub_of(&scope) else { + return local; + }; + self.refresh(&hub); + let hub_chats: Vec = { + let list = self.list(&hub); + let list = lock(&list); + list.local_ids() + .filter_map(|id| saved_chat(&list, &scope, &hub, &id)) + .map(|chat| ChatSummary::from_chat(&chat)) + .collect() + }; + Box::pin(async move { + let mut rows = local.await?; + rows.retain(|row| !hub_chats.iter().any(|hub_row| hub_row.id == row.id)); + rows.extend(hub_chats); + Ok(rows) + }) + } + + fn load_in(&self, scope: ChatScope, id: String) -> StorageFuture { + match hub_of(&scope).and_then(|hub| self.hub_chat(&scope, &hub, &id)) { + Some(chat) => Box::pin(async move { Ok(chat) }), + None => self.local.load_in(scope, id), + } + } + + fn delete_in(&self, scope: ChatScope, id: String) -> StorageFuture<()> { + let local = self.local.delete_in(scope.clone(), id.clone()); + let Some(hub) = hub_of(&scope) else { + return local; + }; + let list = self.list(&hub); + let Some(chat) = lock(&list).forget(&id) else { + return local; + }; + lock(&list).save(&self.root, &hub); + let deleted = Captured::new(self.pool.clone(), &hub).map(|captured| { + call(&self.handle, &Arc::new(captured), |client| async move { + client + .chats() + .delete_chat(v2::IdRequest { id: chat }) + .await?; + Ok(()) + }) + }); + Box::pin(async move { + if let Ok(deleted) = deleted + && let Err(error) = deleted.await + { + tracing::warn!(%error, "the hub did not delete the chat"); + } + match local.await { + Ok(()) | Err(AgentStorageError::NotFound(_)) => Ok(()), + Err(error) => Err(error), + } + }) + } + + fn catalog(&self, scope: ContextId) -> StorageFuture> { + self.local.catalog(scope) + } + + fn load(&self, scope: ContextId, id: String) -> StorageFuture { + self.local.load(scope, id) + } + + fn save(&self, chat: Arc) -> StorageFuture<()> { + let scope = chat.metadata.header.chat_scope(); + match hub_of(&scope) { + Some(hub) if nocterm_ai::agents::is_hub_agent(&chat.metadata.header.agent_id) => { + self.publish(&hub, &chat); + Box::pin(async { Ok(()) }) + } + _ => self.local.save(chat), + } + } + + fn delete(&self, scope: ContextId, id: String) -> StorageFuture<()> { + self.local.delete(scope, id) + } +} + +/// The row `id` of `list` as a saved chat without transcript: the session reopens on the hub. +/// Takes the locked list, so listing every row locks it once. +fn saved_chat(list: &ChatList, scope: &ChatScope, hub: &HubId, id: &str) -> Option { + let row = list.row_for(id)?; + let agent = AgentLaunch::hub_agent(hub.clone(), row.agent_id.clone(), row.agent_name.clone()); + let mut chat = + SavedChat::for_environment(agent.id, id.to_owned(), row.updated(), scope.clone()); + chat.header.title = Some(row.title.clone()).filter(|title| !title.is_empty()); + chat.header.pinned = row.pinned; + chat.session_id = Some(row.id.clone()); + Some(chat) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/chats/repository/list.rs b/crates/hub-io/src/chats/repository/list.rs new file mode 100644 index 00000000..4ed1a1ef --- /dev/null +++ b/crates/hub-io/src/chats/repository/list.rs @@ -0,0 +1,144 @@ +//! The chats of one hub as this device last read them, kept in a private file. + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use serde::{Deserialize, Serialize}; + +/// A list larger than this is not ours. +const MAX_FILE_BYTES: usize = 2 * 1024 * 1024; +/// The format the file is written in; another one is read as empty. +const FORMAT: u32 = 1; + +/// One hub chat as the history lists it. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(super) struct Row { + pub(super) id: String, + pub(super) agent_id: String, + pub(super) agent_name: String, + pub(super) title: String, + pub(super) pinned: bool, + updated_at_ms: i64, +} + +impl Row { + pub(super) fn of(chat: &v2::Chat) -> Self { + Self { + id: chat.id.clone(), + agent_id: chat.agent_id.clone(), + agent_name: chat.agent_name.clone(), + title: chat.title.clone(), + pinned: chat.pinned, + updated_at_ms: chat.updated_at_ms, + } + } + + /// Seconds since the Unix epoch, as the history sorts by. + pub(super) fn updated(&self) -> u64 { + u64::try_from(self.updated_at_ms / 1000).unwrap_or(0) + } +} + +/// The hub's chats, and the hub chat behind each chat started here under its own id. +#[derive(Debug, Default, PartialEq, Serialize, Deserialize)] +pub(super) struct ChatList { + v: u32, + rows: Vec, + /// Local id of a chat started on this device → its hub chat. + started_here: BTreeMap, +} + +fn path(root: &Path, hub: &HubId) -> PathBuf { + let stem = crate::credentials::file_stem(hub.address()); + root.join(format!("{stem}.chats.json")) +} + +impl ChatList { + /// The list kept for `hub`, or an empty one when none is kept or it cannot be read. + pub(super) fn load(root: &Path, hub: &HubId) -> Self { + let read = nocterm_core::fs::read_regular_bounded(&path(root, hub), MAX_FILE_BYTES); + let Ok(bytes) = read else { + return Self::default(); + }; + serde_json::from_slice::(&bytes) + .ok() + .filter(|list| list.v == FORMAT) + .unwrap_or_default() + } + + /// Keeps the list; a failure is logged, the list still works in memory. + pub(super) fn save(&self, root: &Path, hub: &HubId) { + let saved = Self { + v: FORMAT, + rows: self.rows.clone(), + started_here: self.started_here.clone(), + }; + let written = serde_json::to_vec(&saved) + .map_err(std::io::Error::other) + .and_then(|bytes| { + nocterm_core::fs::write_private_atomic(&path(root, hub), &bytes) + .map_err(std::io::Error::other) + }); + if let Err(error) = written { + tracing::warn!(%error, "the hub's chat list cannot be kept on this device"); + } + } + + /// Replaces the rows with what the hub listed; `true` when they changed. Chats started + /// here that the hub no longer lists are forgotten. + pub(super) fn replace(&mut self, rows: Vec) -> bool { + if rows == self.rows { + return false; + } + self.started_here + .retain(|_, hub_chat| rows.iter().any(|row| row.id == *hub_chat)); + self.rows = rows; + true + } + + /// Notes that the chat `local` started here is the hub chat `hub_chat`. + pub(super) fn remember(&mut self, local: &str, hub_chat: &str) { + if local != hub_chat && !self.started_here.contains_key(local) { + self.started_here + .insert(local.to_owned(), hub_chat.to_owned()); + } + } + + /// The ids the history lists the hub chats by. + pub(super) fn local_ids(&self) -> impl Iterator + '_ { + self.rows.iter().map(|row| { + self.started_here + .iter() + .find(|(_, hub_chat)| **hub_chat == row.id) + .map_or_else(|| row.id.clone(), |(local, _)| local.clone()) + }) + } + + /// The hub chat a history id stands for. + pub(super) fn row_for(&self, id: &str) -> Option<&Row> { + let hub_chat = self.started_here.get(id).map_or(id, String::as_str); + self.rows.iter().find(|row| row.id == hub_chat) + } + + /// Shows a rename or pin before the hub confirms it. + pub(super) fn apply(&mut self, hub_chat: &str, title: Option<&str>, pinned: Option) { + if let Some(row) = self.rows.iter_mut().find(|row| row.id == hub_chat) { + if let Some(title) = title { + title.clone_into(&mut row.title); + } + if let Some(pinned) = pinned { + row.pinned = pinned; + } + } + } + + /// Forgets the chat a history id stands for and answers its hub chat. + pub(super) fn forget(&mut self, id: &str) -> Option { + let hub_chat = self.row_for(id)?.id.clone(); + self.rows.retain(|row| row.id != hub_chat); + self.started_here.retain(|_, chat| *chat != hub_chat); + Some(hub_chat) + } +} diff --git a/crates/hub-io/src/chats/repository/tests.rs b/crates/hub-io/src/chats/repository/tests.rs new file mode 100644 index 00000000..a33e802a --- /dev/null +++ b/crates/hub-io/src/chats/repository/tests.rs @@ -0,0 +1,85 @@ +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubAddress, HubId}; + +use super::list::{ChatList, Row}; + +fn hub() -> HubId { + HubId::from(HubAddress::parse("hub.example:7443").unwrap()) +} + +fn row(id: &str, title: &str) -> Row { + Row::of(&v2::Chat { + id: id.into(), + agent_id: "a1".into(), + agent_name: "Ops".into(), + title: title.into(), + updated_at_ms: 5_000, + ..Default::default() + }) +} + +#[test] +fn a_chat_started_here_keeps_its_own_id_in_the_history() { + let mut list = ChatList::default(); + assert!(list.replace(vec![row("h1", "one"), row("h2", "two")])); + list.remember("local-1", "h1"); + let ids: Vec<_> = list.local_ids().collect(); + assert_eq!(ids, ["local-1", "h2"]); + assert_eq!(list.row_for("local-1").unwrap().id, "h1"); + assert_eq!(list.row_for("h2").unwrap().updated(), 5); + assert!(!list.replace(vec![row("h1", "one"), row("h2", "two")])); +} + +#[test] +fn a_chat_the_hub_no_longer_lists_is_forgotten() { + let mut list = ChatList::default(); + list.replace(vec![row("h1", "one")]); + list.remember("local-1", "h1"); + list.replace(vec![row("h2", "two")]); + assert!(list.row_for("local-1").is_none()); + assert_eq!(list.forget("h2").as_deref(), Some("h2")); + assert_eq!(list.local_ids().count(), 0); +} + +#[test] +fn a_rename_shows_before_the_hub_confirms_it_and_the_list_survives_a_restart() { + let dir = tempfile::tempdir().unwrap(); + let mut list = ChatList::default(); + list.replace(vec![row("h1", "one")]); + list.apply("h1", Some("renamed"), Some(true)); + list.save(dir.path(), &hub()); + let kept = ChatList::load(dir.path(), &hub()); + let shown = kept.row_for("h1").unwrap(); + assert_eq!(shown.title, "renamed"); + assert!(shown.pinned); + assert_eq!( + ChatList::load( + dir.path(), + &HubId::from(HubAddress::parse("other:1").unwrap()) + ), + ChatList::default() + ); +} + +#[test] +fn every_listed_hub_chat_reads_as_a_saved_chat_of_its_hub_agent() { + use nocterm_ai::local_environment::ChatScope; + let mut list = ChatList::default(); + list.replace(vec![row("h1", "one"), row("h2", "")]); + list.remember("local-1", "h1"); + let scope = ChatScope::this_computer(nocterm_ai::history::ContextId::General); + let chats: Vec<_> = list + .local_ids() + .filter_map(|id| super::saved_chat(&list, &scope, &hub(), &id)) + .collect(); + assert_eq!(chats.len(), 2); + assert_eq!(chats[0].header.title.as_deref(), Some("one")); + assert_eq!(chats[0].session_id.as_deref(), Some("h1")); + assert!(chats[1].header.title.is_none()); + assert!( + chats[0] + .header + .agent_id + .starts_with(nocterm_ai::agents::HUB_AGENT_PREFIX) + ); +} diff --git a/crates/hub-io/src/chats/state.rs b/crates/hub-io/src/chats/state.rs new file mode 100644 index 00000000..3f984e08 --- /dev/null +++ b/crates/hub-io/src/chats/state.rs @@ -0,0 +1,276 @@ +//! What one open hub chat has shown so far, and what each new item or draft adds. +//! +//! The rules are pure: the watch task feeds items and drafts in the order the hub sends them +//! and performs the outputs. A prompt of this client resolves when the first run that started +//! after its message ends; a message from another device starts a turn of its own. + +use std::collections::{HashMap, HashSet}; + +use futures::channel::oneshot; +use nocterm_ai::AgentError; +use nocterm_ai::model::content::Content; +use nocterm_ai::model::session::{PromptOutcome, SessionUpdate, StopReason, TurnActivity}; +use nocterm_hub_proto::v2; + +use super::items; + +/// What the watch task does after an item or draft. +pub(super) enum Output { + Notice(Box), + /// Ask the person whether the tool call `call` may run. + Permission { + approval: String, + call: String, + title: String, + }, + /// The prompt of this client ended. + Resolve( + oneshot::Sender>, + Result, + ), +} + +impl Output { + pub(super) fn notice(update: SessionUpdate) -> Self { + Self::Notice(Box::new(update)) + } +} + +/// A prompt this client sent and waits the end of. +struct Pending { + client_message: String, + /// The seq of its message, once the hub listed it. + after: Option, + /// Runs that were already running when the message arrived: their end is not its end. + earlier: HashSet, + done: oneshot::Sender>, +} + +#[derive(Default)] +pub(super) struct ChatState { + last_seq: u64, + sent: HashSet, + pending: Option, + /// Runs with items and no end yet. + active: HashSet, + /// Text of each run's draft already shown, and how many parts it holds. + drafts: HashMap, + /// Titles of tool calls, for the approval questions about them. + calls: HashMap, + /// Approvals asked for and not resolved yet, with their call. + approvals: HashMap, +} + +impl ChatState { + pub(super) fn last_seq(&self) -> u64 { + self.last_seq + } + + /// Whether a run is under way: the chat shows that the agent works. + pub(super) fn running(&self) -> bool { + !self.active.is_empty() + } + + /// A prompt this client is about to send under `client_message`. + pub(super) fn expect( + &mut self, + client_message: String, + done: oneshot::Sender>, + ) { + self.sent.insert(client_message.clone()); + self.pending = Some(Pending { + client_message, + after: None, + earlier: HashSet::new(), + done, + }); + } + + /// The prompt could not be sent; it waits for nothing. + pub(super) fn abandon(&mut self) -> Option>> { + self.pending.take().map(|pending| pending.done) + } + + /// Questions still open, asked again when a chat reopens. + pub(super) fn open_approvals(&self) -> Vec { + self.approvals + .iter() + .map(|(approval, call)| Output::Permission { + approval: approval.clone(), + call: call.clone(), + title: self.calls.get(call).cloned().unwrap_or_default(), + }) + .collect() + } + + /// Replays a page of history: what the chat shows, without questions or turns. + pub(super) fn replay(&mut self, item: &v2::ChatItem) -> Vec { + self.apply(item, true) + .into_iter() + .filter_map(|output| match output { + Output::Notice(update) => Some(*update), + _ => None, + }) + .collect() + } + + /// One live item of the chat. + pub(super) fn item(&mut self, item: &v2::ChatItem) -> Vec { + if item.seq <= self.last_seq { + return Vec::new(); + } + self.apply(item, false) + } + + fn apply(&mut self, item: &v2::ChatItem, replay: bool) -> Vec { + use v2::chat_item::Body; + self.last_seq = self.last_seq.max(item.seq); + if !item.run_id.is_empty() && !matches!(item.body, Some(Body::RunEnded(_))) { + self.active.insert(item.run_id.clone()); + } + let Some(body) = &item.body else { + return Vec::new(); + }; + match body { + Body::User(message) => self.user(item.seq, message, replay), + Body::Agent(text) => self.agent_text(&item.run_id, &text.text), + Body::ToolCall(call) => { + self.calls.insert(call.call_id.clone(), call.title.clone()); + vec![Output::notice(items::tool_call(call))] + } + Body::ToolResult(result) => vec![Output::notice(items::tool_result(result))], + Body::ApprovalRequest(request) => { + self.approvals + .insert(request.approval_id.clone(), request.call_id.clone()); + if replay { + return Vec::new(); + } + vec![Output::Permission { + approval: request.approval_id.clone(), + call: request.call_id.clone(), + title: self + .calls + .get(&request.call_id) + .cloned() + .unwrap_or_default(), + }] + } + Body::ApprovalResolution(resolution) => { + self.approvals.remove(&resolution.approval_id); + Vec::new() + } + Body::RunEnded(ended) => self.run_ended(item.seq, &item.run_id, ended, replay), + Body::Change(_) => Vec::new(), + } + } + + fn user(&mut self, seq: u64, message: &v2::UserMessage, replay: bool) -> Vec { + let prompt = items::prompt(message); + if replay { + return prompt + .into_iter() + .map(|part| Output::notice(SessionUpdate::UserMessageChunk(part))) + .collect(); + } + if let Some(pending) = self + .pending + .as_mut() + .filter(|pending| pending.client_message == message.client_message_id) + { + pending.after = Some(seq); + pending.earlier = self.active.clone(); + return Vec::new(); + } + if self.sent.contains(&message.client_message_id) { + return Vec::new(); + } + vec![Output::notice(SessionUpdate::Turn(TurnActivity::Started { + prompt, + }))] + } + + fn agent_text(&mut self, run: &str, text: &str) -> Vec { + let shown = self + .drafts + .remove(run) + .map(|(shown, _)| shown) + .unwrap_or_default(); + let rest = text.strip_prefix(shown.as_str()).unwrap_or(text); + if rest.is_empty() { + return Vec::new(); + } + vec![Output::notice(SessionUpdate::AgentMessageChunk( + Content::text(rest.to_owned()), + ))] + } + + /// Text the agent of run `draft.run_id` is writing; only the part not shown yet is told. + pub(super) fn draft(&mut self, draft: &v2::Draft) -> Vec { + let (shown, parts) = self.drafts.entry(draft.run_id.clone()).or_default(); + let whole = if draft.snapshot { + draft.text.clone() + } else if draft.part == *parts + 1 { + format!("{shown}{}", draft.text) + } else { + return Vec::new(); + }; + *parts = draft.part; + let Some(rest) = whole.strip_prefix(shown.as_str()) else { + return Vec::new(); + }; + let rest = rest.to_owned(); + *shown = whole; + if rest.is_empty() { + return Vec::new(); + } + vec![Output::notice(SessionUpdate::AgentMessageChunk( + Content::text(rest), + ))] + } + + fn run_ended( + &mut self, + seq: u64, + run: &str, + ended: &v2::RunEnded, + replay: bool, + ) -> Vec { + let status = v2::RunStatus::try_from(ended.status).unwrap_or(v2::RunStatus::Completed); + self.drafts.remove(run); + if status == v2::RunStatus::Interrupted { + // The run goes on as a new run; nothing ended for the person. + self.active.remove(run); + return Vec::new(); + } + self.active.remove(run); + self.approvals.clear(); + let mut outputs: Vec = items::run_error(ended) + .map(Output::notice) + .into_iter() + .collect(); + if replay { + return outputs; + } + let answers_pending = self.pending.as_ref().is_some_and(|pending| { + pending.after.is_some_and(|after| seq > after) && !pending.earlier.contains(run) + }); + if answers_pending && let Some(pending) = self.pending.take() { + let stop_reason = match status { + v2::RunStatus::Stopped => StopReason::Cancelled, + v2::RunStatus::Limit => StopReason::MaxTurnRequests, + _ => StopReason::EndTurn, + }; + let outcome = PromptOutcome { + stop_reason, + usage: items::usage(ended), + }; + outputs.push(Output::Resolve(pending.done, Ok(outcome))); + } else { + outputs.push(Output::notice(SessionUpdate::Turn(TurnActivity::Ended))); + } + outputs + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/chats/state/tests.rs b/crates/hub-io/src/chats/state/tests.rs new file mode 100644 index 00000000..170cc8a5 --- /dev/null +++ b/crates/hub-io/src/chats/state/tests.rs @@ -0,0 +1,159 @@ +use futures::channel::oneshot; +use nocterm_ai::model::session::{SessionUpdate, StopReason, TurnActivity}; +use nocterm_hub_proto::v2::{self, chat_item::Body}; + +use super::{ChatState, Output}; + +fn item(seq: u64, run: &str, body: Body) -> v2::ChatItem { + v2::ChatItem { + seq, + at_ms: 0, + run_id: run.into(), + body: Some(body), + } +} + +fn user(seq: u64, client_message: &str, text: &str) -> v2::ChatItem { + item( + seq, + "", + Body::User(v2::UserMessage { + text: text.into(), + client_message_id: client_message.into(), + ..Default::default() + }), + ) +} + +fn ended(seq: u64, run: &str, status: v2::RunStatus) -> v2::ChatItem { + item( + seq, + run, + Body::RunEnded(v2::RunEnded { + status: status.into(), + ..Default::default() + }), + ) +} + +fn text(seq: u64, run: &str, text: &str) -> v2::ChatItem { + item(seq, run, Body::Agent(v2::AgentText { text: text.into() })) +} + +fn chunks(outputs: &[Output]) -> Vec { + outputs + .iter() + .filter_map(|output| match output { + Output::Notice(update) => match update.as_ref() { + SessionUpdate::AgentMessageChunk(nocterm_ai::model::content::Content::Text( + text, + )) => Some(text.text.clone()), + _ => None, + }, + _ => None, + }) + .collect() +} + +#[test] +fn a_prompt_ends_with_the_run_that_started_after_it() { + let mut chat = ChatState::default(); + chat.item(&text(1, "r0", "earlier run")); + let (done, mut answer) = oneshot::channel(); + chat.expect("m1".into(), done); + chat.item(&user(2, "m1", "hello")); + let outputs = chat.item(&ended(3, "r0", v2::RunStatus::Completed)); + assert!(matches!( + outputs.last(), + Some(Output::Notice(update)) if matches!(**update, SessionUpdate::Turn(TurnActivity::Ended)) + )); + assert!(answer.try_recv().unwrap().is_none()); + chat.item(&text(4, "r1", "hi")); + let outputs = chat.item(&ended(5, "r1", v2::RunStatus::Stopped)); + let Some(Output::Resolve(sender, result)) = outputs.into_iter().last() else { + panic!("the prompt resolves"); + }; + let _ = sender.send(result); + let outcome = answer.try_recv().unwrap().unwrap().unwrap(); + assert_eq!(outcome.stop_reason, StopReason::Cancelled); +} + +#[test] +fn a_message_from_another_device_starts_and_ends_a_turn() { + let mut chat = ChatState::default(); + let outputs = chat.item(&user(1, "other-device", "deploy")); + assert!(matches!( + outputs.as_slice(), + [Output::Notice(update)] + if matches!(&**update, SessionUpdate::Turn(TurnActivity::Started { prompt }) if prompt.len() == 1) + )); + chat.item(&text(2, "r1", "done")); + assert!(chat.running()); + let outputs = chat.item(&ended(3, "r1", v2::RunStatus::Completed)); + assert!(matches!( + outputs.as_slice(), + [Output::Notice(update)] if matches!(**update, SessionUpdate::Turn(TurnActivity::Ended)) + )); + assert!(!chat.running()); +} + +#[test] +fn a_draft_shows_each_new_part_once_and_the_final_text_adds_only_the_rest() { + let mut chat = ChatState::default(); + let draft = |part, text: &str, snapshot| v2::Draft { + chat_id: "c".into(), + run_id: "r".into(), + part, + text: text.into(), + snapshot, + }; + assert_eq!(chunks(&chat.draft(&draft(1, "Hel", false))), ["Hel"]); + assert!(chat.draft(&draft(3, "skipped", false)).is_empty()); + assert_eq!(chunks(&chat.draft(&draft(2, "lo", false))), ["lo"]); + assert_eq!(chunks(&chat.draft(&draft(2, "Hello wo", true))), [" wo"]); + assert_eq!(chunks(&chat.item(&text(1, "r", "Hello world"))), ["rld"]); +} + +#[test] +fn a_replayed_question_is_not_asked_but_stays_open_until_resolved() { + let mut chat = ChatState::default(); + let call = item( + 1, + "r", + Body::ToolCall(v2::ToolCall { + call_id: "c1".into(), + tool: "server.run".into(), + title: "prod · reboot".into(), + ..Default::default() + }), + ); + let ask = item( + 2, + "r", + Body::ApprovalRequest(v2::ApprovalRequest { + approval_id: "a1".into(), + call_id: "c1".into(), + }), + ); + assert_eq!(chat.replay(&call).len(), 1); + assert!(chat.replay(&ask).is_empty()); + let open = chat.open_approvals(); + assert!(matches!( + open.as_slice(), + [Output::Permission { approval, title, .. }] if approval == "a1" && title == "prod · reboot" + )); + chat.item(&item( + 3, + "r", + Body::ApprovalResolution(v2::ApprovalResolution { + approval_id: "a1".into(), + ..Default::default() + }), + )); + assert!(chat.open_approvals().is_empty()); + assert_eq!(chat.last_seq(), 3); + assert!( + chat.item(&ask).is_empty(), + "an old item is not applied twice" + ); +} diff --git a/crates/hub-io/src/chats/upload.rs b/crates/hub-io/src/chats/upload.rs new file mode 100644 index 00000000..7a2f4358 --- /dev/null +++ b/crates/hub-io/src/chats/upload.rs @@ -0,0 +1,129 @@ +//! Images and files of a prompt, uploaded to the chat in parts before the message is sent. A +//! file is a link to where it is on this device; its bytes are read when the message goes. + +use base64::Engine as _; +use nocterm_ai::model::content::Content; +use nocterm_hub_client::HubClient; +use nocterm_hub_proto::v2; +use sha2::{Digest as _, Sha256}; + +use super::connector::CallFailure; + +/// The largest part sent when the hub names none. +const PART_BYTES: usize = 1024 * 1024; + +/// The text of a prompt and the attachments its other parts became. +pub(super) async fn prepare( + client: &HubClient, + chat: &str, + content: Vec, +) -> Result<(String, Vec), CallFailure> { + let mut text = String::new(); + let mut attachments = Vec::new(); + for (index, part) in content.into_iter().enumerate() { + match part { + Content::Text(value) => { + if !text.is_empty() { + text.push('\n'); + } + text.push_str(&value.text); + } + Content::Image(image) => { + let bytes = base64::engine::general_purpose::STANDARD + .decode(image.data.as_bytes()) + .map_err(|_| CallFailure::Refused("The image could not be read.".into()))?; + let extension = image.mime_type.rsplit('/').next().unwrap_or("png"); + let name = format!("image-{}.{extension}", index + 1); + attachments.push(upload(client, chat, &name, &image.mime_type, &bytes).await?); + } + // A file attached on this device: the hub agent gets its bytes. + Content::ResourceReference(link) => { + let Some(path) = nocterm_ai::prompt_files::path_of(&link.uri) else { + continue; + }; + let bytes = read_file(path).await?; + let mime = link + .mime_type + .clone() + .unwrap_or_else(|| "application/octet-stream".to_owned()); + attachments.push(upload(client, chat, &link.name, &mime, &bytes).await?); + } + // Audio and embedded resources are not something a hub agent reads. + _ => {} + } + } + Ok((text, attachments)) +} + +/// The most bytes of one attached file this device reads; the hub's own limit, usually lower, +/// refuses a larger upload with its reason. +const MAX_FILE_BYTES: u64 = 256 * 1024 * 1024; + +/// The bytes of an attached file, read off the adapter's async threads. +async fn read_file(path: std::path::PathBuf) -> Result, CallFailure> { + let shown = path.display().to_string(); + let read = tokio::task::spawn_blocking(move || { + use std::io::Read as _; + let file = std::fs::File::open(&path)?; + let mut bytes = Vec::new(); + file.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes)?; + Ok::<_, std::io::Error>(bytes) + }) + .await; + match read { + Ok(Ok(bytes)) if bytes.len() as u64 <= MAX_FILE_BYTES => Ok(bytes), + Ok(Ok(_)) => Err(CallFailure::Refused(format!( + "{shown} is larger than {} MiB.", + MAX_FILE_BYTES / (1024 * 1024) + ))), + Ok(Err(_)) | Err(_) => Err(CallFailure::Refused(format!("{shown} could not be read."))), + } +} + +async fn upload( + client: &HubClient, + chat: &str, + name: &str, + mime: &str, + bytes: &[u8], +) -> Result { + let sha256 = hex::encode(Sha256::digest(bytes)); + let begun = client + .chats() + .begin_upload(v2::BeginUploadRequest { + chat_id: chat.to_owned(), + name: name.to_owned(), + mime: mime.to_owned(), + size: bytes.len() as u64, + sha256, + }) + .await? + .into_inner(); + let part = match usize::try_from(begun.part_size) { + Ok(0) | Err(_) => PART_BYTES, + Ok(part) => part.min(PART_BYTES), + }; + let mut offset = usize::try_from(begun.received) + .unwrap_or(0) + .min(bytes.len()); + while offset < bytes.len() { + let end = (offset + part).min(bytes.len()); + client + .chats() + .upload_part(v2::UploadPartRequest { + upload_id: begun.upload_id.clone(), + offset: offset as u64, + data: bytes[offset..end].to_vec(), + }) + .await?; + offset = end; + } + let finished = client + .chats() + .finish_upload(v2::IdRequest { + id: begun.upload_id, + }) + .await? + .into_inner(); + Ok(finished.id) +} diff --git a/crates/hub-io/src/chats/watch.rs b/crates/hub-io/src/chats/watch.rs new file mode 100644 index 00000000..fdd1629b --- /dev/null +++ b/crates/hub-io/src/chats/watch.rs @@ -0,0 +1,247 @@ +//! One `Watch` per open chat: its new items, the text its agent is writing and its settings, +//! told to the chat as session updates until the chat closes. + +use std::sync::{Arc, Mutex, PoisonError, Weak}; +use std::time::Duration; + +use nocterm_ai::model::{ + auth::{PermissionChoice, PermissionEffect, PermissionOutcome, ProviderPermission}, + controls::Change, + ids::ToolId, + session::{SessionInfo, SessionNotice, SessionUpdate, TurnActivity}, + tools::{ToolChanges, ToolPatch}, +}; +use nocterm_ai::{AgentEvent, PermissionResponder}; +use nocterm_hub_proto::v2; + +use super::connector::{Inner, fresh}; +use super::options::ChatOptions; +use super::state::{ChatState, Output}; + +/// How long a broken stream waits before it is opened again. +const RETRY: Duration = Duration::from_secs(3); + +const ALLOW: &str = "allow"; +const ALLOW_IN_CHAT: &str = "allow_chat"; +const DENY: &str = "deny"; + +/// The task that follows one chat. It holds the connection weakly: closing the chat or the +/// connection ends it. +pub(super) struct Follow { + pub(super) inner: Weak, + pub(super) chat: String, + pub(super) state: Arc>, + pub(super) options: Arc>, +} + +impl Follow { + /// Follows the chat; `opening` are the questions a reopened chat left open, and `running` + /// says a run was under way when it was read. + pub(super) async fn run(self, opening: Vec, running: bool) { + let mut first = Some((opening, running)); + loop { + let Some(inner) = self.inner.upgrade() else { + return; + }; + let after = self + .state + .lock() + .unwrap_or_else(PoisonError::into_inner) + .last_seq(); + let stream = async { + let client = fresh(&inner.captured).await.ok()?; + let request = v2::WatchRequest { + chats_after_rev: 0, + open: vec![v2::OpenChat { + chat_id: self.chat.clone(), + after_seq: after, + }], + }; + client + .chats() + .watch(request) + .await + .ok() + .map(|s| s.into_inner()) + } + .await; + drop(inner); + if let Some(mut stream) = stream { + if let Some((opening, running)) = first.take() { + if running { + self.notice(SessionUpdate::Turn(TurnActivity::Started { + prompt: Vec::new(), + })); + } + self.perform(opening); + } + while let Ok(Some(event)) = stream.message().await { + match self.event(event) { + Next::Continue => {} + Next::Reopen => break, + Next::End => return, + } + } + } + tokio::time::sleep(RETRY).await; + } + } + + fn event(&self, event: v2::WatchEvent) -> Next { + use v2::watch_event::Event; + let outputs = match event.event { + Some(Event::Item(event)) if event.chat_id == self.chat => match &event.item { + Some(item) => self.state().item(item), + None => Vec::new(), + }, + Some(Event::Draft(draft)) if draft.chat_id == self.chat => self.state().draft(&draft), + Some(Event::Chat(chat)) if chat.id == self.chat => self.settings(&chat), + Some(Event::ChatDeleted(id)) if id == self.chat => { + if let Some(done) = self.state().abandon() { + let _ = done.send(Err(nocterm_ai::AgentError::Io( + "The chat was deleted.".into(), + ))); + } + return Next::End; + } + Some(Event::Account(changed)) + if changed.list == i32::from(v2::AccountList::Servers) => + { + self.servers_changed(); + Vec::new() + } + Some(Event::Resync(_)) => return Next::Reopen, + _ => Vec::new(), + }; + self.perform(outputs); + Next::Continue + } + + fn state(&self) -> std::sync::MutexGuard<'_, ChatState> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// A changed title, model or set of servers of the chat, told as metadata and options. + fn settings(&self, chat: &v2::Chat) -> Vec { + let mut options = self.options.lock().unwrap_or_else(PoisonError::into_inner); + let mut outputs = Vec::new(); + if chat.title != options.title() && !chat.title.is_empty() { + outputs.push(Output::notice(SessionUpdate::SessionInfoUpdate( + SessionInfo { + title: Change::Set(chat.title.clone()), + meta: None, + }, + ))); + } + let differs = options.differs(chat); + *options = options.stored(chat); + if differs && let Some(inner) = self.inner.upgrade() { + outputs.push(Output::notice(SessionUpdate::ConfigOptionUpdate( + inner.config(&options), + ))); + } + outputs + } + + /// The account's servers changed: they are listed again and the chat shows them. + fn servers_changed(&self) { + let Some(inner) = self.inner.upgrade() else { + return; + }; + let options = self.options.clone(); + let chat = self.chat.clone(); + let weak = self.inner.clone(); + inner.handle.spawn(async move { + let Some(inner) = weak.upgrade() else { + return; + }; + if !inner.list_servers().await { + return; + } + let shown = inner.config(&options.lock().unwrap_or_else(PoisonError::into_inner)); + let notice = SessionNotice::new(chat, SessionUpdate::ConfigOptionUpdate(shown)); + let _ = inner.events.try_send(AgentEvent::Session(notice)); + }); + } + + fn perform(&self, outputs: Vec) { + for output in outputs { + match output { + Output::Notice(update) => self.notice(*update), + Output::Permission { + approval, + call, + title, + } => self.ask(approval, call, title), + Output::Resolve(done, result) => { + let _ = done.send(result); + } + } + } + } + + fn notice(&self, update: SessionUpdate) { + if let Some(inner) = self.inner.upgrade() { + let notice = SessionNotice::new(self.chat.clone(), update); + let _ = inner.events.try_send(AgentEvent::Session(notice)); + } + } + + /// Asks the person about a tool call and sends the answer to the hub. Any device may answer + /// first; a later answer here changes nothing. + fn ask(&self, approval: String, call: String, title: String) { + let Some(inner) = self.inner.upgrade() else { + return; + }; + let (respond, answer) = PermissionResponder::channel(); + let request = ProviderPermission::new( + self.chat.clone(), + ToolPatch::new(ToolId::new(call), ToolChanges::default().title(title)), + vec![ + PermissionChoice::new(ALLOW, "Allow", PermissionEffect::AllowOnce), + PermissionChoice::new( + ALLOW_IN_CHAT, + "Allow in this chat", + PermissionEffect::AllowRemembered, + ), + PermissionChoice::new(DENY, "Deny", PermissionEffect::DenyOnce), + ], + ); + if inner + .events + .try_send(AgentEvent::Permission { request, respond }) + .is_err() + { + return; + } + let chat = self.chat.clone(); + let captured = inner.captured.clone(); + inner.handle.spawn(async move { + let Ok(PermissionOutcome::Selected(choice)) = answer.await else { + return; + }; + let decision = match choice.as_str() { + ALLOW => v2::ApprovalDecision::AllowOnce, + ALLOW_IN_CHAT => v2::ApprovalDecision::AllowChat, + _ => v2::ApprovalDecision::Deny, + }; + let Ok(client) = fresh(&captured).await else { + return; + }; + let request = v2::ResolveApprovalRequest { + chat_id: chat, + approval_id: approval, + decision: decision.into(), + }; + if let Err(status) = client.chats().resolve_approval(request).await { + tracing::debug!(code = ?status.code(), "the approval was not taken"); + } + }); + } +} + +enum Next { + Continue, + Reopen, + End, +} diff --git a/crates/hub-io/src/checked.rs b/crates/hub-io/src/checked.rs new file mode 100644 index 00000000..cff31b5b --- /dev/null +++ b/crates/hub-io/src/checked.rs @@ -0,0 +1,66 @@ +//! Poll admission under a caller's original authority, including completed work. +use std::future::{Future, poll_fn}; +use std::task::Poll; + +pub(crate) async fn during( + check: impl Fn() -> Result<(), E>, + future: impl Future, +) -> Result { + let mut future = std::pin::pin!(future); + poll_fn(|cx| { + check()?; + match future.as_mut().poll(cx) { + Poll::Pending => Poll::Pending, + Poll::Ready(value) => Poll::Ready(check().map(|()| value)), + } + }) + .await +} + +#[cfg(test)] +mod tests { + use super::*; + use std::cell::Cell; + + #[test] + fn invalid_authority_never_polls_the_operation() { + let polled = Cell::new(false); + let result = + futures::executor::block_on(during(|| Err("stale"), async { polled.set(true) })); + assert_eq!(result, Err("stale")); + assert!(!polled.get()); + } + + #[test] + fn resumed_work_rechecks_authority_before_polling_again() { + let valid = Cell::new(true); + let polls = Cell::new(0); + let work = poll_fn(|cx| { + polls.set(polls.get() + 1); + valid.set(false); + cx.waker().wake_by_ref(); + Poll::<()>::Pending + }); + let result = + futures::executor::block_on(during(|| valid.get().then_some(()).ok_or("stale"), work)); + assert_eq!(result, Err("stale")); + assert_eq!(polls.get(), 1); + } + + #[test] + fn completion_rechecks_authority_before_delivering_the_result() { + let valid = Cell::new(true); + let result = futures::executor::block_on(during( + || valid.get().then_some(()).ok_or("stale"), + async { + valid.set(false); + 7 + }, + )); + assert_eq!(result, Err("stale")); + assert_eq!( + futures::executor::block_on(during(|| Ok::<_, ()>(()), async { 7 })), + Ok(7) + ); + } +} diff --git a/crates/hub-io/src/classify.rs b/crates/hub-io/src/classify.rs new file mode 100644 index 00000000..074d33f1 --- /dev/null +++ b/crates/hub-io/src/classify.rs @@ -0,0 +1,170 @@ +//! What went wrong, in the terms of `HubError`. +//! +//! Every function keeps the original failure as the `source()` of the error it returns. + +use std::error::Error; + +use nocterm_core::SharedCause; +use nocterm_hub_client::{ClientError, HubEndpoint, OidcError, ProbeError, StatusCode}; +use nocterm_hubs_model::{Fingerprint, HubAddress, HubEndpointSpec, HubError, TrustPolicy}; +use nocterm_keys::LoginError; + +use crate::credentials::CredentialError; + +pub(crate) fn remote(error: impl Error + Send + Sync + 'static) -> HubError { + HubError::Remote { + cause: SharedCause::new(error), + } +} + +pub(crate) fn credentials(error: impl Error + Send + Sync + 'static) -> HubError { + HubError::Credentials { + cause: SharedCause::new(error), + } +} + +pub(crate) fn credential_file(error: CredentialError) -> HubError { + match error { + CredentialError::StaleLogin => HubError::Stale, + other => credentials(other), + } +} + +/// Whether the hub cannot be asked now: it does not answer, or this device has no (more) +/// credentials for it. Work that needs the hub waits; work that has a local copy goes on. +pub(crate) fn unreachable(error: &HubError) -> bool { + matches!( + error, + HubError::Unreachable { .. } | HubError::NotSignedIn | HubError::Revoked + ) +} + +/// How a call to a hub that was signed in to, or not, is classified. +enum Kind { + Settled(HubError), + Unreachable, + Credentials, + Remote, +} + +fn kind(error: &ClientError) -> Kind { + match error { + ClientError::UpgradeRequired { hub, client } => Kind::Settled(HubError::UpgradeRequired { + hub: *hub, + client: *client, + }), + ClientError::NotSignedIn => Kind::Settled(HubError::NotSignedIn), + ClientError::Revoked => Kind::Settled(HubError::Revoked), + ClientError::Oidc(OidcError::TimedOut) => Kind::Settled(HubError::SignInTimedOut), + ClientError::Oidc(OidcError::Provider { .. } | OidcError::NoCode) => { + Kind::Settled(HubError::SignInCancelled) + } + ClientError::SessionStore(_) | ClientError::Randomness(_) => Kind::Credentials, + ClientError::Connect(_) => Kind::Unreachable, + ClientError::Status { code, .. } => match code { + StatusCode::Unauthenticated => Kind::Settled(HubError::NotSignedIn), + StatusCode::PermissionDenied => Kind::Settled(HubError::PermissionDenied), + StatusCode::NotFound => Kind::Settled(HubError::NotFound), + StatusCode::Unavailable | StatusCode::DeadlineExceeded | StatusCode::Cancelled => { + Kind::Unreachable + } + _ => Kind::Remote, + }, + _ => Kind::Remote, + } +} + +/// A failure of a call on a hub at `address`. +pub(crate) fn classify(address: &HubAddress, error: ClientError) -> HubError { + match kind(&error) { + Kind::Settled(settled) => settled, + Kind::Unreachable => HubError::Unreachable { + address: address.clone(), + cause: SharedCause::new(error), + }, + Kind::Credentials => credentials(error), + Kind::Remote => remote(error), + } +} + +/// Which sign-in method failed. +#[derive(Clone, Copy)] +pub(crate) enum Method { + Password, + Oidc, +} + +/// A failure of a sign-in: a refusal means something else here than on any other call. +pub(crate) fn classify_sign_in( + address: &HubAddress, + method: Method, + error: ClientError, +) -> HubError { + // A password the hub could not have accepted is refused before it is sent. + if let ( + Method::Password, + ClientError::Login(LoginError::PasswordTooShort { .. } | LoginError::EmptyUsername), + ) = (method, &error) + { + return HubError::InvalidCredentials; + } + if let ClientError::Status { code, .. } = &error { + match (method, code) { + ( + Method::Password, + StatusCode::Unauthenticated + | StatusCode::InvalidArgument + | StatusCode::NotFound + | StatusCode::PermissionDenied, + ) => return HubError::InvalidCredentials, + (_, StatusCode::FailedPrecondition | StatusCode::Unimplemented) + | (Method::Oidc, StatusCode::NotFound | StatusCode::InvalidArgument) => { + return HubError::MethodUnavailable; + } + _ => {} + } + } + classify(address, error) +} + +/// A failure to connect. The client only says the connection failed, so the certificate the +/// hub presents is read and compared with the decided trust to tell a changed certificate +/// from a hub that is simply not there. +pub(crate) async fn explain_connect(spec: &HubEndpointSpec, error: ClientError) -> HubError { + if !matches!(error, ClientError::Connect(_)) { + return classify(&spec.address, error); + } + let Ok(probe) = HubEndpoint::probe(&spec.address.to_string()).await else { + return classify(&spec.address, error); + }; + let presented = Fingerprint::from_bytes(probe.fingerprint); + match spec.trust { + TrustPolicy::Pinned(pinned) if pinned != presented => HubError::CertificateChanged { + address: spec.address.clone(), + pinned, + presented, + }, + TrustPolicy::PublicCa if !probe.publicly_trusted => HubError::CertificateUntrusted { + address: spec.address.clone(), + presented, + }, + _ => classify(&spec.address, error), + } +} + +/// A failure to read the certificate of a hub. +pub(crate) fn probe_failed(address: &HubAddress, error: ProbeError) -> HubError { + match error { + ProbeError::Unreachable(_) + | ProbeError::Handshake(_) + | ProbeError::TimedOut + | ProbeError::NoCertificate => HubError::Unreachable { + address: address.clone(), + cause: SharedCause::new(error), + }, + _ => remote(error), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/classify/tests.rs b/crates/hub-io/src/classify/tests.rs new file mode 100644 index 00000000..e4e4286c --- /dev/null +++ b/crates/hub-io/src/classify/tests.rs @@ -0,0 +1,171 @@ +use nocterm_hub_client::{ConnectError, EndpointError, OidcError}; +use tonic::{Code, Status}; + +use super::*; + +fn address() -> HubAddress { + HubAddress::parse("hub.example").unwrap() +} + +fn status(code: Code) -> ClientError { + ClientError::from(Status::new(code, "details the user never sees")) +} + +fn coded(code: Code, hub_code: &str) -> ClientError { + let mut status = Status::new(code, "revoked"); + status + .metadata_mut() + .insert("x-nocterm-error", hub_code.parse().unwrap()); + ClientError::from(status) +} + +#[test] +fn a_revoked_device_is_revoked_however_the_hub_says_it() { + assert!(matches!( + classify(&address(), ClientError::Revoked), + HubError::Revoked + )); + let wire = coded(Code::Unauthenticated, "ERROR_CODE_DEVICE_REVOKED"); + assert!(matches!(classify(&address(), wire), HubError::Revoked)); +} + +#[test] +fn statuses_become_the_errors_a_caller_acts_on() { + assert!(matches!( + classify(&address(), status(Code::Unauthenticated)), + HubError::NotSignedIn + )); + assert!(matches!( + classify(&address(), status(Code::PermissionDenied)), + HubError::PermissionDenied + )); + assert!(matches!( + classify(&address(), status(Code::NotFound)), + HubError::NotFound + )); + for code in [Code::Unavailable, Code::DeadlineExceeded] { + assert!(matches!( + classify(&address(), status(code)), + HubError::Unreachable { .. } + )); + } + assert!(matches!( + classify(&address(), status(Code::Internal)), + HubError::Remote { .. } + )); +} + +#[test] +fn an_old_client_is_told_both_versions() { + let error = ClientError::UpgradeRequired { hub: 3, client: 1 }; + assert!(matches!( + classify(&address(), error), + HubError::UpgradeRequired { hub: 3, client: 1 } + )); +} + +#[test] +fn a_connection_that_failed_is_unreachable_and_keeps_its_cause() { + let error = ClientError::Connect(ConnectError::Endpoint(EndpointError::Address)); + let classified = classify(&address(), error); + let HubError::Unreachable { address: at, .. } = &classified else { + panic!("{classified:?}"); + }; + assert_eq!(at.to_string(), "hub.example:7443"); + let cause = classified.source().unwrap(); + let inner = cause + .source() + .and_then(|source| source.downcast_ref::()) + .or_else(|| cause.downcast_ref::()); + assert!(matches!(inner, Some(ClientError::Connect(_))), "{cause:?}"); +} + +#[test] +fn a_failure_to_keep_tokens_is_a_credentials_error() { + let error = ClientError::SessionStore(std::io::Error::other("disk")); + assert!(matches!( + classify(&address(), error), + HubError::Credentials { .. } + )); +} + +#[test] +fn browser_sign_in_failures_say_why() { + let timed_out = ClientError::Oidc(OidcError::TimedOut); + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, timed_out), + HubError::SignInTimedOut + )); + let refused = ClientError::Oidc(OidcError::Provider { + error: "access_denied".into(), + description: String::new(), + }); + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, refused), + HubError::SignInCancelled + )); + let loopback = ClientError::Oidc(OidcError::Loopback(std::io::Error::other("bind"))); + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, loopback), + HubError::Remote { .. } + )); +} + +#[test] +fn a_refused_password_is_invalid_credentials_only_when_signing_in() { + for code in [ + Code::Unauthenticated, + Code::InvalidArgument, + Code::PermissionDenied, + ] { + assert!(matches!( + classify_sign_in(&address(), Method::Password, status(code)), + HubError::InvalidCredentials + )); + } + assert!(matches!( + classify(&address(), status(Code::InvalidArgument)), + HubError::Remote { .. } + )); +} + +#[test] +fn a_password_too_short_for_any_account_is_invalid_credentials() { + let short = || ClientError::Login(LoginError::PasswordTooShort { min: 8 }); + assert!(matches!( + classify_sign_in(&address(), Method::Password, short()), + HubError::InvalidCredentials + )); +} + +#[test] +fn a_method_the_hub_does_not_offer_is_unavailable() { + for method in [Method::Password, Method::Oidc] { + assert!(matches!( + classify_sign_in(&address(), method, status(Code::Unimplemented)), + HubError::MethodUnavailable + )); + } + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, status(Code::NotFound)), + HubError::MethodUnavailable + )); +} + +#[test] +fn a_hub_that_is_not_there_is_unreachable_not_a_changed_certificate() { + let spec = HubEndpointSpec { + address: HubAddress::parse("127.0.0.1:1").unwrap(), + trust: TrustPolicy::Pinned(Fingerprint::from_bytes([1; 32])), + }; + let error = ClientError::Connect(ConnectError::Endpoint(EndpointError::Address)); + let classified = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(explain_connect(&spec, error)); + assert!( + matches!(classified, HubError::Unreachable { .. }), + "{classified:?}" + ); +} diff --git a/crates/hub-io/src/connect.rs b/crates/hub-io/src/connect.rs new file mode 100644 index 00000000..bb8dc00a --- /dev/null +++ b/crates/hub-io/src/connect.rs @@ -0,0 +1,182 @@ +//! Connecting to a hub and signing in or out. + +use std::sync::Arc; + +use futures::channel::oneshot; +use nocterm_hub_client::{HubClient, Session}; +use nocterm_hubs_model::{ + Account, HubEndpointSpec, HubError, HubId, KeyStatus, SignedIn, TrustPolicy, +}; +use nocterm_keys::DeviceKey; +use nocterm_session::Secret; + +use crate::account::{KeyFlow, key_admission::KeyAdmission}; +use crate::classify::{Method, classify_sign_in, explain_connect}; +use crate::credentials::{CredentialFile, CredentialOrigin, LoginPublication}; +use crate::directory::who_am_i; +use crate::endpoint::endpoint; +use crate::pool::Pool; +use crate::pool::observation::LoginObservation; +use zeroize::Zeroize as _; + +mod login; +mod request; +mod sign_out; +mod start; +pub(crate) use request::Reconnect; +pub(crate) use sign_out::SignOut; +pub(crate) use start::LoginStart; + +/// Connects to hubs and keeps the credentials of the ones the device signs in to. +pub(crate) struct Connector { + pool: Arc, + keys: Arc, +} + +/// A sign-in under way: the client and the file its tokens will be kept in. A file created for +/// a sign-in that did not finish is removed, so a hub never signed in to leaves no key behind. +struct Attempt { + hub: HubId, + file: CredentialFile, + client: HubClient, + trust: TrustPolicy, + created: bool, + finished: bool, + origin: CredentialOrigin, + observed: Arc, +} + +impl Drop for Attempt { + fn drop(&mut self) { + if self.created && !self.finished { + let _ = self.file.erase_unpublished(self.origin.public_key()); + } + } +} + +impl Connector { + pub(crate) fn new(pool: Arc, keys: Arc) -> Self { + Self { pool, keys } + } + + pub(crate) fn reconnect(&self, spec: HubEndpointSpec) -> Result { + Reconnect::capture(self.pool.clone(), spec) + } + pub(crate) fn begin(&self, spec: HubEndpointSpec) -> Result { + LoginStart::capture(self.pool.clone(), spec) + } + pub(crate) fn sign_out(&self, hub: &HubId) -> Result { + SignOut::capture(self.pool.clone(), hub.clone()) + } + + /// Keeps the tokens of a finished sign-in and reads who signed in, with the client that + /// signed in. + async fn complete( + &self, + mut attempt: Attempt, + mut session: Session, + ) -> Result<(Account, HubClient, Arc), HubError> { + let client = attempt.client.clone(); + let published = self.pool.publish_login( + attempt.hub.clone(), + attempt.trust, + client.clone(), + LoginPublication::new(&attempt.file, &session, &attempt.origin), + &attempt.observed.snapshot(), + ); + session.access_token.zeroize(); + session.refresh_token.zeroize(); + let published = published?; + // After this point even a stale result never owns an unpublished file. + attempt.finished = true; + let (_, subject) = published.into_parts(); + attempt.observed.became(&subject)?; + let owner = self.keys.published(&attempt.hub, &subject)?; + let account = who_am_i(&client, &owner).await?; + Ok((account, client, owner)) + } + + pub(crate) async fn password( + &self, + start: LoginStart, + username: String, + password: Secret, + ) -> Result { + let attempt = start.open().await?; + let hub = attempt.hub.clone(); + let signed_in = attempt + .observed + .during( + attempt + .client + .password_login(&username, password.expose(), self.pool.device()), + ) + .await? + .map_err(|error| classify_sign_in(hub.address(), Method::Password, error))?; + let secrets = signed_in.secrets; + let (account, client, owner) = self.complete(attempt, signed_in.session).await?; + // The login password also derives the key that opens the account's keyset. + let keys = + self.keys + .after_password(&owner, &client, account.id.as_str(), &username, secrets); + let keys = owner.during(keys).await??; + Ok(SignedIn { + account, + keys, + login: login::receipt(&self.pool, owner.member().clone()), + }) + } + + /// Signs in through the browser. `browser` receives the address to open. + pub(crate) async fn oidc( + &self, + start: LoginStart, + provider: String, + browser: oneshot::Sender, + ) -> Result { + let attempt = start.open().await?; + let hub = attempt.hub.clone(); + let observed = attempt.observed.clone(); + let session = attempt + .observed + .during( + attempt + .client + .oidc_login(&provider, self.pool.device(), |url| { + if observed.check().is_ok() { + let _ = browser.send(url.to_owned()); + } + }), + ) + .await? + .map_err(|error| classify_sign_in(hub.address(), Method::Oidc, error))?; + let (account, _, owner) = self.complete(attempt, session).await?; + let keys: KeyStatus = self.keys.after_browser(&owner)?; + Ok(SignedIn { + account, + keys, + login: login::receipt(&self.pool, owner.member().clone()), + }) + } + + /// Explicit removal of the configured hub is separate from an account worker. + pub(crate) fn forget(&self, hub: &HubId) -> Result<(), HubError> { + self.pool.forget_hub(hub) + } +} + +/// A client for the hub with the decided trust; a failure says why the hub was not reached. +async fn open( + spec: &HubEndpointSpec, + key: DeviceKey, + session: Option, +) -> Result { + let endpoint = endpoint(spec)?; + match HubClient::connect(&endpoint, key, session).await { + Ok(client) => Ok(client), + Err(error) => Err(explain_connect(spec, error).await), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/connect/login.rs b/crates/hub-io/src/connect/login.rs new file mode 100644 index 00000000..22f2f5d9 --- /dev/null +++ b/crates/hub-io/src/connect/login.rs @@ -0,0 +1,31 @@ +//! A successful sign-in hands its original native cleanup authority to the consumer. +use crate::{account::source::CapturedMembership, pool::Pool}; +use nocterm_hubs_model::{HubError, LoginOwner, LoginReceipt}; +use std::sync::{Arc, Weak}; +struct Owner { + pool: Weak, + member: Arc, +} +impl LoginOwner for Owner { + fn check_current(&self) -> Result<(), HubError> { + self.member.check() + } + fn discard(&self) -> Result<(), HubError> { + self.member.check()?; + let pool = self.pool.upgrade().ok_or(HubError::Stale)?; + let subject = self.member.subject()?; + let (_, result) = pool + .end_membership(self.member.hub(), &subject, false)? + .into_parts(); + result + } +} +pub(super) fn receipt(pool: &Arc, member: Arc) -> LoginReceipt { + LoginReceipt::new( + member.hub().clone(), + Arc::new(Owner { + pool: Arc::downgrade(pool), + member, + }), + ) +} diff --git a/crates/hub-io/src/connect/request.rs b/crates/hub-io/src/connect/request.rs new file mode 100644 index 00000000..fe03bdf6 --- /dev/null +++ b/crates/hub-io/src/connect/request.rs @@ -0,0 +1,163 @@ +//! Reconnect retains original device credentials before it is enqueued. +use super::open; +use crate::{ + classify::{credential_file, remote}, + convert, + credentials::{CredentialFile, CredentialLoginId, Device}, + pool::{Pool, observation::LoginObservation}, + task::spawn, +}; +use nocterm_hub_client::{ClientError, HubClient, StatusCode}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubEndpointSpec, HubError, HubFuture, HubId, HubInfo}; +use nocterm_keys::DeviceKey; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct Reconnect { + pool: Arc, + observed: Arc, + spec: HubEndpointSpec, + file: CredentialFile, + device: Option, + known: Option<(HubClient, Option)>, + file_subject: Option<(String, String)>, +} +impl Reconnect { + pub(crate) fn capture(pool: Arc, spec: HubEndpointSpec) -> Result { + let hub = HubId::from(spec.address.clone()); + let observed = LoginObservation::capture(pool.clone(), hub.clone())?; + let known = pool.connected(&hub, spec.trust); + let file = pool.file(&spec.address)?; + let mut device = match file.load().map_err(credential_file)? { + Some(device) => device, + None => Device { + key: DeviceKey::generate().map_err(remote)?, + session: None, + login: None, + }, + }; + if let Some(subject) = observed.original_subject()? { + let matches = device.session.as_ref().is_some_and(|session| { + session.account_id == subject.account() && session.device_id == subject.device() + }); + if !matches + || known + .as_ref() + .is_some_and(|(_, login)| *login != device.login) + { + device.wipe_session(); + return Err(HubError::Stale); + } + } + if let Err(error) = observed.check() { + device.wipe_session(); + return Err(error); + } + let file_subject = device + .session + .as_ref() + .map(|session| (session.account_id.clone(), session.device_id.clone())); + Ok(Self { + pool, + observed, + spec, + file, + device: Some(device), + known, + file_subject, + }) + } + pub(crate) fn start(self, handle: &Handle) -> HubFuture { + let observed = self.observed.clone(); + let returned = observed.clone(); + let task = spawn(handle, async move { observed.during(self.worker()).await? }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + async fn worker(mut self) -> Result { + if let Some((client, login)) = self.known.take() { + let now = self + .observed + .during(client.auth().get_hub_info(v2::Empty {})) + .await?; + self.observed.check()?; + if let Ok(now) = now { + let now = now.into_inner(); + if now.hub_id == client.info().hub_id + && now.protocol_version == client.info().protocol_version + { + return self.renewed(client, login).await; + } + } + } + let mut device = self.device.take().ok_or(HubError::Stale)?; + let login = device.login; + let store = device + .session + .as_ref() + .map(|session| self.file.clone().bound(session, login)); + let client = self + .observed + .during(open(&self.spec, device.key, device.session.take())) + .await??; + self.observed.check()?; + match store { + Some(store) => client.persist_with(Arc::new(store)), + None => return Ok(convert::hub_info(client.info(), false)), + } + self.renewed(client, login).await + } + async fn renewed( + &self, + client: HubClient, + login: Option, + ) -> Result { + match self.observed.during(client.ensure_fresh()).await? { + Ok(_) => { + self.observed.check()?; + let info = convert::hub_info(client.info(), true); + let next = self.pool.renew_observed( + self.observed.hub().clone(), + self.spec.trust, + client, + login, + &self.observed.snapshot(), + )?; + self.observed.published(next); + Ok(info) + } + Err(error) => { + let expired = matches!( + error, + ClientError::Status { + code: StatusCode::Unauthenticated, + .. + } + ); + let revoked = matches!(error, ClientError::Revoked); + if expired || revoked { + let subject = self + .file_subject + .as_ref() + .map(|(account, device)| (account.as_str(), device.as_str())); + self.observed.retire(&self.file, subject, login, revoked)?; + if expired { + return Ok(convert::hub_info(client.info(), false)); + } + } + Err(crate::classify::classify(&self.spec.address, error)) + } + } + } +} +impl Drop for Reconnect { + fn drop(&mut self) { + if let Some(device) = &mut self.device { + device.wipe_session(); + } + } +} diff --git a/crates/hub-io/src/connect/sign_out.rs b/crates/hub-io/src/connect/sign_out.rs new file mode 100644 index 00000000..c9c823a4 --- /dev/null +++ b/crates/hub-io/src/connect/sign_out.rs @@ -0,0 +1,114 @@ +//! Sign-out retains the original native member or original offline private-file origin. +use crate::{ + classify::credential_file, + credentials::{CredentialFile, CredentialOrigin}, + pool::{Pool, observation::LoginObservation}, + task::spawn, +}; +use nocterm_hub_client::HubClient; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, HubFuture, HubId}; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct SignOut { + observed: Arc, + target: Target, +} +enum Target { + Member { + pool: Arc, + client: HubClient, + subject: Arc, + }, + Offline { + file: CredentialFile, + origin: Option, + }, +} +impl SignOut { + pub(crate) fn capture(pool: Arc, hub: HubId) -> Result { + let observed = LoginObservation::capture(pool.clone(), hub.clone())?; + let target = if let Some(subject) = observed.original_subject()? { + let client = pool.captured(&hub, &subject)?; + Target::Member { + pool, + client, + subject, + } + } else { + let file = pool.file(hub.address())?; + let origin = file.load().map_err(credential_file)?.map(|mut device| { + let origin = device.origin(); + device.wipe_session(); + origin + }); + Target::Offline { file, origin } + }; + observed.check()?; + Ok(Self { observed, target }) + } + pub(crate) fn start(self, handle: &Handle) -> HubFuture<()> { + let observed = self.observed.clone(); + let returned = observed.clone(); + let task = spawn(handle, async move { observed.during(self.worker()).await? }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + async fn worker(self) -> Result<(), HubError> { + match self.target { + Target::Member { + pool, + client, + subject, + } => { + let (next, result) = pool + .end_membership(self.observed.hub(), &subject, false)? + .into_parts(); + self.observed.published(next); + result?; + // Signing out revokes this device on the hub. + if let Err(error) = self + .observed + .during(client.account().logout(v2::Empty {})) + .await? + { + tracing::debug!(%error,"the hub did not end the original device session"); + } + Ok(()) + } + Target::Offline { file, origin } => { + let expected = self.observed.snapshot(); + let mut cleanup = None; + self.observed.with_current(|| { + let retire = || { + cleanup = Some( + match self.observed.slot().end_observed(expected.receipt(), true) { + Ok(ended) => { + let (receipt, result) = ended.into_parts(); + self.observed.ended_absence(receipt); + result + } + Err(error) => Err(error), + }, + ); + }; + let retired = match origin { + Some(ref origin) => file.retire_origin(origin, retire), + None => file.while_absent(retire), + } + .map_err(credential_file)?; + if !retired { + return Err(HubError::Stale); + } + cleanup.take().ok_or(HubError::Stale)??; + Ok(()) + })?; + Ok(()) + } + } + } +} diff --git a/crates/hub-io/src/connect/start.rs b/crates/hub-io/src/connect/start.rs new file mode 100644 index 00000000..ea67ae8d --- /dev/null +++ b/crates/hub-io/src/connect/start.rs @@ -0,0 +1,110 @@ +//! An explicit login owns its original native selection and private device file. +use super::{Attempt, open}; +use crate::{ + classify::{credential_file, remote}, + credentials::{CredentialFile, CredentialOrigin, Device}, + pool::{Pool, observation::LoginObservation}, + task::spawn, +}; +use nocterm_hubs_model::{HubEndpointSpec, HubError, HubFuture, HubId}; +use nocterm_keys::DeviceKey; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct LoginStart { + spec: HubEndpointSpec, + observed: Arc, + file: CredentialFile, + device: Option, + origin: CredentialOrigin, + created: bool, + handed_off: bool, +} +impl LoginStart { + pub(crate) fn capture(pool: Arc, spec: HubEndpointSpec) -> Result { + let observed = LoginObservation::capture(pool.clone(), HubId::from(spec.address.clone()))?; + let file = pool.file(&spec.address)?; + let (mut device, created) = match file.load().map_err(credential_file)? { + Some(device) => (device, false), + None => ( + Device { + key: DeviceKey::generate().map_err(remote)?, + session: None, + login: None, + }, + true, + ), + }; + if let Err(error) = observed.check() { + device.wipe_session(); + return Err(error); + } + let origin = device.origin(); + Ok(Self { + spec, + observed, + file, + device: Some(device), + origin, + created, + handed_off: false, + }) + } + pub(crate) fn observation(&self) -> &Arc { + &self.observed + } + pub(crate) fn start( + self, + handle: &Handle, + work: impl FnOnce(Self) -> F + Send + 'static, + ) -> HubFuture + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let observed = self.observed.clone(); + let returned = observed.clone(); + let task = spawn(handle, async move { observed.during(work(self)).await? }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + pub(super) async fn open(mut self) -> Result { + let device = self.device.as_ref().ok_or(HubError::Stale)?; + self.observed.with_current(|| { + self.file + .prepare_login(device, self.created) + .map_err(credential_file) + })?; + let mut device = self.device.take().ok_or(HubError::Stale)?; + let client = self + .observed + .during(open(&self.spec, device.key, device.session.take())) + .await??; + self.observed.check()?; + let attempt = Attempt { + hub: self.observed.hub().clone(), + file: self.file.clone(), + client, + trust: self.spec.trust, + created: self.created, + finished: false, + origin: self.origin.clone(), + observed: self.observed.clone(), + }; + self.handed_off = true; + Ok(attempt) + } +} +impl Drop for LoginStart { + fn drop(&mut self) { + if let Some(device) = &mut self.device { + device.wipe_session(); + } + if self.created && !self.handed_off { + let _ = self.file.erase_unpublished(self.origin.public_key()); + } + } +} diff --git a/crates/hub-io/src/connect/tests.rs b/crates/hub-io/src/connect/tests.rs new file mode 100644 index 00000000..f081837b --- /dev/null +++ b/crates/hub-io/src/connect/tests.rs @@ -0,0 +1,102 @@ +//! Native deferred admission uses the original private-file generation. +use super::*; +use crate::{account::Accounts, credentials::CredentialDir}; +use nocterm_hub_client::Session; +use nocterm_hubs_model::{Fingerprint, HubAddress}; + +struct Device { + _dir: tempfile::TempDir, + file: CredentialFile, + connector: Connector, + spec: HubEndpointSpec, +} +impl Device { + fn new() -> Self { + let dir = tempfile::tempdir().unwrap(); + let address = HubAddress::parse("127.0.0.1:1").unwrap(); + let accounts = Arc::new(Accounts::new(dir.path().to_owned())); + let pool = Arc::new(Pool::new(dir.path().to_owned(), "test", accounts.clone())); + let keys = KeyFlow::new(pool.clone(), accounts, tokio::runtime::Handle::current()); + let file = CredentialDir::new(dir.path().to_owned()) + .file(&address) + .unwrap(); + Self { + _dir: dir, + file, + connector: Connector::new(pool, keys), + spec: HubEndpointSpec { + address, + trust: TrustPolicy::Pinned(Fingerprint::from_bytes([7; 32])), + }, + } + } + fn login(&self, name: &str) { + self.file.load_or_create().unwrap(); + self.file + .save_session(&Session { + account_id: name.into(), + device_id: "device".into(), + access_token: format!("access-{name}"), + refresh_token: format!("refresh-{name}"), + access_expires_at_ms: 10, + refresh_expires_at_ms: 20, + }) + .unwrap(); + } + fn account(&self) -> String { + self.file + .load() + .unwrap() + .unwrap() + .session + .unwrap() + .account_id + } +} +#[tokio::test] +async fn a_login_queued_without_credentials_never_adopts_a_newer_private_file() { + let device = Device::new(); + let start = device.connector.begin(device.spec.clone()).unwrap(); + device.login("B"); + assert!(matches!( + device + .connector + .password(start, "A".into(), Secret::new("password")) + .await, + Err(HubError::Stale) + )); + assert_eq!(device.account(), "B"); +} +#[tokio::test] +async fn a_login_queued_with_a_session_cannot_replace_another_login_of_the_same_account() { + let device = Device::new(); + device.login("A"); + let start = device.connector.begin(device.spec.clone()).unwrap(); + let original = device.file.load().unwrap().unwrap().login; + device.login("A"); + let current = device.file.load().unwrap().unwrap().login; + assert_ne!(original, current); + assert!(matches!( + device + .connector + .password(start, "A".into(), Secret::new("password")) + .await, + Err(HubError::Stale) + )); + assert_eq!(device.file.load().unwrap().unwrap().login, current); +} +#[tokio::test] +async fn an_offline_sign_out_captured_before_a_new_login_preserves_that_login() { + let device = Device::new(); + device.login("A"); + let sign_out = device + .connector + .sign_out(&HubId::from(device.spec.address.clone())) + .unwrap(); + device.login("B"); + assert!(matches!( + sign_out.start(&tokio::runtime::Handle::current()).await, + Err(HubError::Stale) + )); + assert_eq!(device.account(), "B"); +} diff --git a/crates/hub-io/src/convert.rs b/crates/hub-io/src/convert.rs new file mode 100644 index 00000000..50db23a6 --- /dev/null +++ b/crates/hub-io/src/convert.rs @@ -0,0 +1,33 @@ +//! Records of the hub protocol as the records of the model. + +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{Account, AccountId, HubInfo, OidcProvider}; + +pub(crate) fn hub_info(info: &v2::HubInfo, signed_in: bool) -> HubInfo { + HubInfo { + name: info.name.clone(), + password_login: info.password_login, + oidc_providers: info + .oidc_providers + .iter() + .map(|provider| OidcProvider { + id: provider.id.clone(), + display_name: provider.display_name.clone(), + }) + .collect(), + bootstrapped: !info.needs_setup, + signed_in, + } +} + +pub(crate) fn account(me: v2::Me) -> Account { + Account { + id: AccountId::new(me.account_id), + username: me.username, + display_name: me.display_name, + admin: me.admin, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/convert/tests.rs b/crates/hub-io/src/convert/tests.rs new file mode 100644 index 00000000..d4b3510a --- /dev/null +++ b/crates/hub-io/src/convert/tests.rs @@ -0,0 +1,34 @@ +use super::*; + +#[test] +fn hub_info_keeps_what_a_sign_in_screen_needs() { + let wire = v2::HubInfo { + name: "Team hub".into(), + needs_setup: false, + password_login: true, + oidc_providers: vec![v2::OidcProvider { + id: "corp".into(), + display_name: "Corporate".into(), + }], + ..Default::default() + }; + let info = hub_info(&wire, true); + assert_eq!(info.name, "Team hub"); + assert!(info.password_login && info.bootstrapped && info.signed_in); + assert_eq!(info.oidc_providers[0].id, "corp"); + assert!(!hub_info(&wire, false).signed_in); +} + +#[test] +fn an_account_says_whether_it_administers_the_hub() { + let me = v2::Me { + account_id: "a1".into(), + username: "ada".into(), + display_name: "Ada".into(), + admin: true, + ..Default::default() + }; + let account = account(me); + assert_eq!(account.id.as_str(), "a1"); + assert!(account.admin); +} diff --git a/crates/hub-io/src/credentials.rs b/crates/hub-io/src/credentials.rs new file mode 100644 index 00000000..c30f3cf4 --- /dev/null +++ b/crates/hub-io/src/credentials.rs @@ -0,0 +1,485 @@ +//! The private credential file of this device for each hub. +//! +//! One file per hub, `_.toml` in a mode-0700 directory, holds the device key seed +//! and the tokens. A sibling `.lock` file serializes token rotation between processes. + +use std::any::Any; +use std::fs::{self, File, OpenOptions}; +use std::io; +use std::path::{Path, PathBuf}; + +use nocterm_core::fs::{ + BoundedReadError, create_private, read_regular_bounded, write_private_atomic, +}; +use nocterm_hub_client::{Session, SessionStore}; +use nocterm_hubs_model::HubAddress; +use nocterm_keys::DeviceKey; +use serde::{Deserialize, Serialize}; +use tokio::runtime::{Handle, RuntimeFlavor}; + +mod bound; +mod publication; +mod seed; +use bound::BoundSessionStore; +pub(crate) use publication::{CredentialOrigin, LoginPublication}; + +/// A credential file is a few hundred bytes; anything larger is not ours. +const MAX_FILE_BYTES: usize = 16 * 1024; +/// Keeps `.toml.lock` below the usual 255-byte file name limit. +pub(crate) const MAX_NAME_BYTES: usize = 200; + +/// Why the credentials of a hub could not be read or kept. +#[derive(Debug, thiserror::Error)] +pub(crate) enum CredentialError { + #[error("the credential file cannot be read")] + Read(#[from] BoundedReadError), + #[error("the credential file cannot be written")] + Write(#[from] nocterm_core::fs::FileError), + #[error("the credential directory or lock cannot be prepared")] + Prepare(#[source] io::Error), + #[error("the credential file is not valid")] + Format(#[from] toml::de::Error), + #[error("the credential file cannot be encoded")] + Encode(#[from] toml::ser::Error), + #[error("the credential file holds no valid device key")] + Seed(#[from] seed::SeedError), + #[error("the credential file belongs to another hub")] + OtherHub, + #[error("the local login identity cannot be generated")] + LoginIdentity(#[source] nocterm_keys::KeyError), + #[error("the original local login file was replaced")] + StaleLogin, + #[error("the hub address is too long for a file name")] + NameTooLong, +} + +/// Where every hub's credential file lives. +#[derive(Clone, Debug)] +pub(crate) struct CredentialDir { + root: PathBuf, +} + +impl CredentialDir { + pub(crate) fn new(root: PathBuf) -> Self { + Self { root } + } + + pub(crate) fn file(&self, address: &HubAddress) -> Result { + let name = file_stem(address); + if name.len() > MAX_NAME_BYTES { + return Err(CredentialError::NameTooLong); + } + Ok(CredentialFile { + path: self.root.join(format!("{name}.toml")), + lock_path: self.root.join(format!("{name}.toml.lock")), + address: address.clone(), + }) + } +} + +/// `host_port`; `:` becomes `_`, which no host name contains, so names are unique. +pub(crate) fn file_stem(address: &HubAddress) -> String { + // Also names the account cache beside the credential file. + format!("{}_{}", address.host(), address.port()).replace(':', "_") +} + +/// Identifies one explicit native login, independently of account/device reuse. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct CredentialLoginId([u8; 16]); + +/// What the file holds, as stored. +#[derive(Serialize, Deserialize)] +struct Stored { + address: String, + seed: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + login: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + session: Option, +} + +/// The device key and tokens read from a credential file. +pub(crate) struct Device { + pub(crate) key: DeviceKey, + pub(crate) session: Option, + pub(crate) login: Option, +} +impl Device { + pub(crate) fn origin(&self) -> CredentialOrigin { + CredentialOrigin::of(self) + } + pub(crate) fn wipe_session(&mut self) { + use zeroize::Zeroize as _; + if let Some(session) = &mut self.session { + session.access_token.zeroize(); + session.refresh_token.zeroize(); + } + } +} + +/// The credential file of one hub. Also the [`SessionStore`] its client rotates tokens in. +#[derive(Clone, Debug)] +pub(crate) struct CredentialFile { + path: PathBuf, + lock_path: PathBuf, + address: HubAddress, +} + +impl CredentialFile { + /// Pins token adoption and refresh publication to this original login subject. + pub(crate) fn bound( + self, + session: &Session, + login: Option, + ) -> BoundSessionStore { + BoundSessionStore::new(self, session, login) + } + + /// The stored device; `None` when nothing was ever kept for the hub. + pub(crate) fn load(&self) -> Result, CredentialError> { + let bytes = match read_regular_bounded(&self.path, MAX_FILE_BYTES) { + Ok(bytes) => bytes, + Err(error) if error.io_kind() == Some(io::ErrorKind::NotFound) => return Ok(None), + Err(error) => return Err(error.into()), + }; + let stored = self.parse(&bytes)?; + Ok(Some(Device { + key: DeviceKey::from_seed(&seed::decode(&stored.seed)?), + session: stored.session, + login: stored.login, + })) + } + + fn parse(&self, bytes: &[u8]) -> Result { + let text = String::from_utf8_lossy(bytes); + let stored: Stored = toml::from_str(&text)?; + if stored.address != self.address.to_string() { + return Err(CredentialError::OtherHub); + } + Ok(stored) + } + + /// The stored device, or a new key kept without tokens. The flag says it was created. + #[cfg(test)] + pub(crate) fn load_or_create(&self) -> Result<(Device, bool), CredentialError> { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + if let Some(device) = self.load()? { + return Ok((device, false)); + } + let key = DeviceKey::generate() + .map_err(|error| CredentialError::Prepare(io::Error::other(error)))?; + self.write(&key, None, None)?; + Ok(( + Device { + key, + session: None, + login: None, + }, + true, + )) + } + + /// Publishes a new explicit login, even when the subject is unchanged. + #[cfg(test)] + pub(crate) fn save_session( + &self, + session: &Session, + ) -> Result { + self.save_session_inner(session, None) + } + + fn save_session_expected( + &self, + session: &Session, + origin: &CredentialOrigin, + ) -> Result { + self.save_session_inner(session, Some(origin)) + } + + fn save_session_inner( + &self, + session: &Session, + origin: Option<&CredentialOrigin>, + ) -> Result { + let login = CredentialLoginId( + nocterm_keys::random_bytes().map_err(CredentialError::LoginIdentity)?, + ); + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let mut device = self + .load()? + .ok_or_else(|| CredentialError::Prepare(io::Error::from(io::ErrorKind::NotFound)))?; + let current = origin.is_none_or(|origin| origin.matches(&device)); + device.wipe_session(); + if !current { + return Err(CredentialError::StaleLogin); + } + self.write(&device.key, Some(session), Some(login))?; + Ok(login) + } + + /// Establishes only the captured original device file before native login I/O. + pub(crate) fn prepare_login( + &self, + device: &Device, + absent: bool, + ) -> Result<(), CredentialError> { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + match self.load()? { + None if absent => self.write(&device.key, None, None), + Some(mut current) if !absent => { + let matches = device.origin().matches(¤t); + current.wipe_session(); + if matches { + Ok(()) + } else { + Err(CredentialError::StaleLogin) + } + } + Some(mut current) => { + current.wipe_session(); + Err(CredentialError::StaleLogin) + } + None => Err(CredentialError::StaleLogin), + } + } + + /// Forgets the tokens but keeps the key, so the device signs in again as itself. + #[cfg(test)] + pub(crate) fn clear_session(&self) -> Result<(), CredentialError> { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + self.replace_session(None) + } + + /// Replaces the tokens for a caller that holds the lock already: the client of the + /// contract does while it rotates. A second lock would wait for itself. + fn save_locked(&self, session: &Session) -> Result<(), CredentialError> { + self.replace_session(Some(session)) + } + + fn replace_session(&self, session: Option<&Session>) -> Result<(), CredentialError> { + let device = self + .load()? + .ok_or_else(|| CredentialError::Prepare(io::Error::from(io::ErrorKind::NotFound)))?; + self.write(&device.key, session, device.login) + } + + fn write( + &self, + key: &DeviceKey, + session: Option<&Session>, + login: Option, + ) -> Result<(), CredentialError> { + self.prepare_directory().map_err(CredentialError::Prepare)?; + let stored = Stored { + address: self.address.to_string(), + seed: seed::encode(&key.seed()), + session: session.cloned(), + login, + }; + let text = toml::to_string(&stored)?; + write_private_atomic(&self.path, text.as_bytes())?; + Ok(()) + } + + /// Checks a captured login while excluding token/file replacement. + pub(crate) fn matches_login( + &self, + account: &str, + device: &str, + login: Option, + ) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + self.matches_login_locked(account, device, login) + } + + fn matches_login_locked( + &self, + account: &str, + device: &str, + login: Option, + ) -> Result { + use zeroize::Zeroize as _; + Ok(self.load()?.is_some_and(|stored| { + stored.session.is_some_and(|mut session| { + let matches = stored.login == login + && session.account_id == account + && session.device_id == device; + session.access_token.zeroize(); + session.refresh_token.zeroize(); + matches + }) + })) + } + + /// Retires only the original local login under the shared file lock. + #[cfg(test)] + pub(crate) fn erase_if( + &self, + account: &str, + device: &str, + login: Option, + ) -> Result<(), CredentialError> { + self.retire_if(account, device, login, true, || {}) + .map(|_| ()) + } + + /// The existing file-generation check also fences original cache retirement. + /// Callers hold Pool admission; the action must not reacquire this file lock. + pub(crate) fn retire_if( + &self, + account: &str, + device: &str, + login: Option, + erase: bool, + retire: impl FnOnce(), + ) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let matches = self.matches_login_locked(account, device, login)?; + if matches { + retire(); + if erase { + self.remove_locked().map_err(CredentialError::Prepare)?; + } else { + self.replace_session(None)?; + } + } + Ok(matches) + } + + /// A failed first sign-in owns only its still unpublished original device file. + pub(crate) fn erase_unpublished( + &self, + expected_public_key: &[u8; 32], + ) -> Result<(), CredentialError> { + use zeroize::Zeroize as _; + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let Some(mut stored) = self.load()? else { + return Ok(()); + }; + let unpublished = stored.login.is_none() + && stored.session.is_none() + && stored.key.public_key() == *expected_public_key; + if let Some(session) = &mut stored.session { + session.access_token.zeroize(); + session.refresh_token.zeroize(); + } + if unpublished { + self.remove_locked().map_err(CredentialError::Prepare)?; + } + Ok(()) + } + + pub(crate) fn retire_origin( + &self, + origin: &CredentialOrigin, + retire: impl FnOnce(), + ) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let Some(mut current) = self.load()? else { + return Ok(false); + }; + let matches = origin.matches(¤t); + current.wipe_session(); + if matches { + retire(); + self.remove_locked().map_err(CredentialError::Prepare)?; + } + Ok(matches) + } + + pub(crate) fn while_absent(&self, retire: impl FnOnce()) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + if let Some(mut current) = self.load()? { + current.wipe_session(); + return Ok(false); + } + retire(); + Ok(true) + } + + /// Removes the credentials of the hub; no file is not an error. + pub(crate) fn erase(&self) -> io::Result<()> { + let _lock = self.lock_exclusive()?; + self.remove_locked() + } + + fn remove_locked(&self) -> io::Result<()> { + match fs::remove_file(&self.path) { + Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error), + _ => Ok(()), + } + } + + fn prepare_directory(&self) -> io::Result<()> { + match self.path.parent() { + Some(parent) => prepare_private_directory(parent), + None => Ok(()), + } + } + + /// Opens the lock file, creating it private, and waits for the exclusive lock. Waiting + /// does not hold up the worker of the runtime it runs on. + fn lock_exclusive(&self) -> io::Result { + self.prepare_directory()?; + let file = match create_private(&self.lock_path) { + Ok(file) => file, + Err(error) if error.kind() == io::ErrorKind::AlreadyExists => { + open_existing(&self.lock_path)? + } + Err(error) => return Err(error.into_io_error()), + }; + off_worker(|| file.lock())?; + Ok(file) + } +} + +/// Creates `directory` and its parents, private to the user where the platform has modes. +pub(crate) fn prepare_private_directory(directory: &Path) -> io::Result<()> { + let mut builder = fs::DirBuilder::new(); + builder.recursive(true); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt as _; + builder.mode(0o700); + } + builder.create(directory) +} + +/// Runs a call that waits for another holder of the lock. On a multi-thread runtime the +/// worker hands its other tasks over first, so the holder, which may be one of them awaiting +/// the network, keeps being polled and releases the lock. +fn off_worker(wait: impl FnOnce() -> T) -> T { + match Handle::try_current() { + Ok(handle) if handle.runtime_flavor() == RuntimeFlavor::MultiThread => { + tokio::task::block_in_place(wait) + } + _ => wait(), + } +} + +fn open_existing(path: &Path) -> io::Result { + OpenOptions::new().write(true).open(path) +} + +impl SessionStore for CredentialFile { + fn lock(&self) -> io::Result> { + Ok(Box::new(self.lock_exclusive()?)) + } + + fn load(&self) -> io::Result> { + Ok(CredentialFile::load(self) + .map_err(into_io)? + .and_then(|device| device.session)) + } + + fn save(&self, session: &Session) -> io::Result<()> { + self.save_locked(session).map_err(into_io) + } +} + +fn into_io(error: CredentialError) -> io::Error { + io::Error::other(error) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/credentials/bound.rs b/crates/hub-io/src/credentials/bound.rs new file mode 100644 index 00000000..ca7aa95f --- /dev/null +++ b/crates/hub-io/src/credentials/bound.rs @@ -0,0 +1,88 @@ +//! A client's token store can refresh its original account/device only. +use std::{any::Any, io}; + +use nocterm_hub_client::{Session, SessionStore}; +use zeroize::Zeroize as _; + +use super::{CredentialFile, CredentialLoginId}; + +#[derive(Debug)] +pub(crate) struct BoundSessionStore { + file: CredentialFile, + account: String, + device: String, + login: Option, +} + +#[derive(Debug, thiserror::Error)] +#[error("the stored login no longer belongs to this client")] +struct ReplacedLogin; + +impl BoundSessionStore { + pub(super) fn new( + file: CredentialFile, + session: &Session, + login: Option, + ) -> Self { + Self { + file, + account: session.account_id.clone(), + device: session.device_id.clone(), + login, + } + } + + fn matches(&self, session: &Session) -> bool { + session.account_id == self.account && session.device_id == self.device + } + + fn current(&self) -> io::Result { + let current = self.file.load().map_err(super::into_io)?; + let same_login = current + .as_ref() + .is_some_and(|stored| stored.login == self.login); + let Some(mut session) = current.and_then(|stored| stored.session) else { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + ReplacedLogin, + )); + }; + if !same_login || !self.matches(&session) { + session.access_token.zeroize(); + session.refresh_token.zeroize(); + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + ReplacedLogin, + )); + } + Ok(session) + } +} + +impl SessionStore for BoundSessionStore { + fn lock(&self) -> io::Result> { + SessionStore::lock(&self.file) + } + + fn load(&self) -> io::Result> { + self.current().map(Some) + } + + fn save(&self, session: &Session) -> io::Result<()> { + if !self.matches(session) { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + ReplacedLogin, + )); + } + // SDK owns the file lock through load, remote refresh and save. A new + // login uses that same lock, so this check cannot race replacement. + let mut current = self.current()?; + current.access_token.zeroize(); + current.refresh_token.zeroize(); + SessionStore::save(&self.file, session) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/credentials/bound/tests.rs b/crates/hub-io/src/credentials/bound/tests.rs new file mode 100644 index 00000000..6a31f4ea --- /dev/null +++ b/crates/hub-io/src/credentials/bound/tests.rs @@ -0,0 +1,201 @@ +use super::*; +use crate::credentials::CredentialDir; + +fn session(account: &str, token: &str) -> Session { + Session { + account_id: account.into(), + device_id: "device".into(), + access_token: token.into(), + refresh_token: format!("refresh-{token}"), + access_expires_at_ms: i64::MAX, + refresh_expires_at_ms: i64::MAX, + } +} +fn file(dir: &tempfile::TempDir) -> CredentialFile { + CredentialDir::new(dir.path().to_path_buf()) + .file(&"hub.test".parse().unwrap()) + .unwrap() +} +fn signed_in(dir: &tempfile::TempDir, session: &Session) -> BoundSessionStore { + let file = file(dir); + file.load_or_create().unwrap(); + let login = file.save_session(session).unwrap(); + file.bound(session, Some(login)) +} + +#[test] +fn a_bound_store_adopts_only_a_refresh_of_the_original_subject() { + let dir = tempfile::tempdir().unwrap(); + let store = signed_in(&dir, &session("A", "initial")); + let refreshed = session("A", "new"); + { + let current = file(&dir); + let _lock = current.lock().unwrap(); + SessionStore::save(¤t, &refreshed).unwrap(); + } + let _lock = store.lock().unwrap(); + assert_eq!(store.load().unwrap(), Some(refreshed.clone())); + store.save(&session("A", "next")).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("A", "next")) + ); +} + +#[test] +fn an_old_a_store_cannot_adopt_b_and_a_new_b_store_can_refresh() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + let current = signed_in(&dir, &session("B", "login")); + { + let _lock = old.lock().unwrap(); + let error = old.load().unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); + assert!(error.get_ref().unwrap().is::()); + } + let _lock = current.lock().unwrap(); + assert_eq!(current.load().unwrap(), Some(session("B", "login"))); + current.save(&session("B", "refreshed")).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("B", "refreshed")) + ); +} + +#[test] +fn a_late_a_refresh_cannot_overwrite_the_file_of_b() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + file(&dir).save_session(&session("B", "login")).unwrap(); + let _lock = old.lock().unwrap(); + assert_eq!( + old.save(&session("A", "late")).unwrap_err().kind(), + io::ErrorKind::PermissionDenied + ); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("B", "login")) + ); +} + +#[test] +fn a_cleared_login_cannot_implicitly_refresh_old_tokens() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + file(&dir).clear_session().unwrap(); + let _lock = old.lock().unwrap(); + assert!(old.load().is_err()); + assert!(old.save(&session("A", "late")).is_err()); + assert!(SessionStore::load(&file(&dir)).unwrap().is_none()); +} + +#[test] +fn a_changed_device_is_a_replacement_even_for_the_same_account() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + let mut next = session("A", "login"); + next.device_id = "new-device".into(); + file(&dir).save_session(&next).unwrap(); + let _lock = old.lock().unwrap(); + assert!(old.load().is_err()); + assert!(old.save(&session("A", "late")).is_err()); + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next)); +} + +#[test] +fn captured_revocation_erases_only_the_matching_file_subject() { + let dir = tempfile::tempdir().unwrap(); + let _old = signed_in(&dir, &session("A", "initial")); + file(&dir).save_session(&session("B", "login")).unwrap(); + file(&dir).erase_if("A", "device", _old.login).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("B", "login")) + ); + file(&dir) + .erase_if("B", "device", file(&dir).load().unwrap().unwrap().login) + .unwrap(); + assert!(file(&dir).load().unwrap().is_none()); +} + +#[test] +fn a_same_subject_new_login_rejects_old_adoption_refresh_and_revocation_before_pool_publication() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "old-login")); + // This is the exact formerly vulnerable window: the new file is durable, + // while a retained old membership can still deliver its delayed failure. + let next = session("A", "new-login"); + let new_login = file(&dir).save_session(&next).unwrap(); + assert_ne!(old.login, Some(new_login)); + assert!(!file(&dir).matches_login("A", "device", old.login).unwrap()); + assert!( + file(&dir) + .matches_login("A", "device", Some(new_login)) + .unwrap() + ); + file(&dir).erase_if("A", "device", old.login).unwrap(); + { + let _lock = old.lock().unwrap(); + assert_eq!( + old.load().unwrap_err().kind(), + io::ErrorKind::PermissionDenied + ); + assert_eq!( + old.save(&session("A", "late-refresh")).unwrap_err().kind(), + io::ErrorKind::PermissionDenied + ); + } + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next.clone())); + let current = file(&dir).bound(&next, Some(new_login)); + let _lock = current.lock().unwrap(); + current.save(&session("A", "current-refresh")).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("A", "current-refresh")) + ); + assert_eq!(file(&dir).load().unwrap().unwrap().login, Some(new_login)); +} + +#[test] +fn a_legacy_login_cannot_retire_or_adopt_a_new_explicit_login() { + let dir = tempfile::tempdir().unwrap(); + let original = session("A", "legacy"); + file(&dir).load_or_create().unwrap(); + SessionStore::save(&file(&dir), &original).unwrap(); + let old = file(&dir).bound(&original, None); + let next = session("A", "new-login"); + file(&dir).save_session(&next).unwrap(); + file(&dir).erase_if("A", "device", None).unwrap(); + let _lock = old.lock().unwrap(); + assert!(old.load().is_err()); + assert!(old.save(&session("A", "late")).is_err()); + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next)); +} + +#[test] +fn late_original_revocation_preserves_new_same_account_file_and_open_slot() { + use crate::account::{ + Accounts, + testing::{ACCOUNT_ID, hub, sign_in}, + }; + use std::sync::Arc; + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + let (slot, _) = sign_in(&accounts); + let original_keys = slot.open().unwrap(); + let old = signed_in(&dir, &session(ACCOUNT_ID, "original")); + // Publish the replacement credentials before the replacement membership. + let next = session(ACCOUNT_ID, "new-login"); + file(&dir).save_session(&next).unwrap(); + let (new_slot, _) = sign_in(&accounts); + let current_keys = new_slot.open().unwrap(); + file(&dir) + .erase_if(ACCOUNT_ID, "device", old.login) + .unwrap(); + slot.erase_if(&original_keys).unwrap(); + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next)); + assert!(Arc::ptr_eq( + &accounts.slot(&hub()).unwrap().open().unwrap(), + ¤t_keys + )); +} diff --git a/crates/hub-io/src/credentials/publication.rs b/crates/hub-io/src/credentials/publication.rs new file mode 100644 index 00000000..2a822690 --- /dev/null +++ b/crates/hub-io/src/credentials/publication.rs @@ -0,0 +1,66 @@ +//! A captured private-file origin fences a delayed explicit login publication. +use super::{CredentialError, CredentialFile, CredentialLoginId, Device}; +use nocterm_hub_client::{HubClient, Session}; +use std::sync::Arc; + +#[derive(Clone)] +pub(crate) struct CredentialOrigin { + public_key: [u8; 32], + login: Option, + subject: Option<(String, String)>, +} +impl CredentialOrigin { + pub(super) fn of(device: &Device) -> Self { + Self { + public_key: device.key.public_key(), + login: device.login, + subject: device + .session + .as_ref() + .map(|session| (session.account_id.clone(), session.device_id.clone())), + } + } + pub(super) fn matches(&self, device: &Device) -> bool { + self.public_key == device.key.public_key() + && self.login == device.login + && self + .subject + .as_ref() + .map(|(account, device)| (account.as_str(), device.as_str())) + == device + .session + .as_ref() + .map(|session| (session.account_id.as_str(), session.device_id.as_str())) + } + pub(crate) fn public_key(&self) -> &[u8; 32] { + &self.public_key + } +} + +pub(crate) struct LoginPublication<'a> { + file: &'a CredentialFile, + session: &'a Session, + origin: &'a CredentialOrigin, +} +impl<'a> LoginPublication<'a> { + pub(crate) fn new( + file: &'a CredentialFile, + session: &'a Session, + origin: &'a CredentialOrigin, + ) -> Self { + Self { + file, + session, + origin, + } + } + pub(crate) fn session(&self) -> &Session { + self.session + } + pub(crate) fn save(&self) -> Result { + self.file.save_session_expected(self.session, self.origin) + } + pub(crate) fn bind(&self, client: &HubClient, login: CredentialLoginId) { + client.persist_with(Arc::new(self.file.clone().bound(self.session, Some(login)))); + } +} diff --git a/crates/hub-io/src/credentials/seed.rs b/crates/hub-io/src/credentials/seed.rs new file mode 100644 index 00000000..4af938ff --- /dev/null +++ b/crates/hub-io/src/credentials/seed.rs @@ -0,0 +1,46 @@ +//! The device key seed as hexadecimal text. + +/// The seed is not 64 hexadecimal digits. +#[derive(Debug, thiserror::Error)] +#[error("the key seed is not 64 hexadecimal digits")] +pub(crate) struct SeedError; + +pub(crate) fn encode(seed: &[u8; 32]) -> String { + hex::encode(seed) +} + +pub(crate) fn decode(text: &str) -> Result<[u8; 32], SeedError> { + let mut seed = [0_u8; 32]; + hex::decode_to_slice(text, &mut seed).map_err(|_| SeedError)?; + Ok(seed) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_seed_round_trips_through_hex() { + let mut seed = [0_u8; 32]; + for (index, byte) in seed.iter_mut().enumerate() { + *byte = u8::try_from(index * 8).unwrap(); + } + assert_eq!(decode(&encode(&seed)).unwrap(), seed); + } + + #[test] + fn text_that_is_not_a_seed_is_refused() { + assert!(decode("").is_err()); + assert!(decode(&"a".repeat(63)).is_err()); + assert!(decode(&"g".repeat(64)).is_err()); + assert!(decode(&"é".repeat(32)).is_err()); + assert!(decode(&"+f".repeat(32)).is_err()); + } + + #[test] + fn mixed_case_seeds_keep_the_same_bytes_and_encode_canonically() { + let seed = decode(&"aF".repeat(32)).unwrap(); + assert_eq!(seed, [175; 32]); + assert_eq!(encode(&seed), "af".repeat(32)); + } +} diff --git a/crates/hub-io/src/credentials/tests.rs b/crates/hub-io/src/credentials/tests.rs new file mode 100644 index 00000000..5341189f --- /dev/null +++ b/crates/hub-io/src/credentials/tests.rs @@ -0,0 +1,186 @@ +use std::sync::mpsc; +use std::thread; +use std::time::Duration; + +use super::*; + +fn address(text: &str) -> HubAddress { + HubAddress::parse(text).unwrap() +} + +fn session(token: &str) -> Session { + Session { + account_id: "account".into(), + device_id: "device".into(), + access_token: token.into(), + access_expires_at_ms: 10, + refresh_token: format!("refresh-{token}"), + refresh_expires_at_ms: 20, + } +} + +fn file_in(dir: &tempfile::TempDir, text: &str) -> CredentialFile { + CredentialDir::new(dir.path().join("hubs")) + .file(&address(text)) + .unwrap() +} + +#[test] +fn a_hub_without_a_file_has_no_device() { + let dir = tempfile::tempdir().unwrap(); + assert!(file_in(&dir, "hub.example").load().unwrap().is_none()); +} + +#[test] +fn a_new_device_key_is_kept_and_found_again() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + let (created, was_new) = file.load_or_create().unwrap(); + assert!(was_new); + assert!(created.session.is_none()); + + let (again, was_new) = file.load_or_create().unwrap(); + assert!(!was_new); + assert_eq!(again.key.public_key(), created.key.public_key()); +} + +#[test] +fn tokens_are_saved_beside_the_key_and_cleared_without_losing_it() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + let (device, _) = file.load_or_create().unwrap(); + + SessionStore::save(&file, &session("one")).unwrap(); + let stored = file.load().unwrap().unwrap(); + assert_eq!(stored.session, Some(session("one"))); + assert_eq!(stored.key.public_key(), device.key.public_key()); + assert_eq!(SessionStore::load(&file).unwrap(), Some(session("one"))); + + file.clear_session().unwrap(); + let cleared = file.load().unwrap().unwrap(); + assert!(cleared.session.is_none()); + assert_eq!(cleared.key.public_key(), device.key.public_key()); +} + +#[test] +fn saving_tokens_for_a_hub_without_a_file_fails() { + let dir = tempfile::tempdir().unwrap(); + assert!(SessionStore::save(&file_in(&dir, "hub.example"), &session("one")).is_err()); +} + +#[cfg(unix)] +#[test] +fn the_file_and_its_directory_are_private() { + use std::os::unix::fs::PermissionsExt as _; + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + file.load_or_create().unwrap(); + SessionStore::save(&file, &session("one")).unwrap(); + + let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777; + assert_eq!(mode(&file.path), 0o600); + assert_eq!(mode(&file.lock_path), 0o600); + assert_eq!(mode(file.path.parent().unwrap()), 0o700); +} + +#[test] +fn erasing_removes_the_credentials_and_tolerates_their_absence() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + file.load_or_create().unwrap(); + file.erase().unwrap(); + assert!(file.load().unwrap().is_none()); + file.erase().unwrap(); +} + +#[test] +fn every_hub_address_has_its_own_file() { + let dir = tempfile::tempdir().unwrap(); + let names: Vec<_> = ["a.example", "a.example:9", "b.example", "[::1]", "[::1]:9"] + .iter() + .map(|text| file_in(&dir, text).path) + .collect(); + for (index, name) in names.iter().enumerate() { + assert!(!names[..index].contains(name), "{name:?}"); + } +} + +#[test] +fn a_file_of_another_hub_or_a_damaged_one_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + file.load_or_create().unwrap(); + let other = file_in(&dir, "other.example"); + fs::copy(&file.path, &other.path).unwrap(); + assert!(matches!(other.load(), Err(CredentialError::OtherHub))); + + fs::write(&file.path, "not toml [").unwrap(); + assert!(matches!(file.load(), Err(CredentialError::Format(_)))); + fs::write( + &file.path, + "address = \"hub.example:7443\"\nseed = \"abcd\"\n", + ) + .unwrap(); + assert!(matches!(file.load(), Err(CredentialError::Seed(_)))); +} + +#[test] +fn an_address_too_long_for_a_file_name_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let label = "a".repeat(60); + let host = format!("{label}.{label}.{label}.{label}.example"); + let result = CredentialDir::new(dir.path().to_owned()).file(&address(&host)); + assert!(matches!(result, Err(CredentialError::NameTooLong))); +} + +#[test] +fn a_second_lock_waits_until_the_first_is_released() { + let dir = tempfile::tempdir().unwrap(); + let first = file_in(&dir, "hub.example"); + let guard = SessionStore::lock(&first).unwrap(); + + let second = file_in(&dir, "hub.example"); + let (acquired, signal) = mpsc::channel(); + let waiter = thread::spawn(move || { + let _guard = SessionStore::lock(&second).unwrap(); + acquired.send(()).unwrap(); + }); + assert!(signal.recv_timeout(Duration::from_millis(200)).is_err()); + drop(guard); + signal.recv_timeout(Duration::from_secs(10)).unwrap(); + waiter.join().unwrap(); +} + +#[test] +fn waiting_for_the_lock_does_not_stall_the_workers_of_the_runtime_that_holds_it() { + let dir = tempfile::tempdir().unwrap(); + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build() + .unwrap(); + let (held, holding) = mpsc::channel(); + let holder = file_in(&dir, "hub.example"); + runtime.spawn(async move { + let guard = SessionStore::lock(&holder).unwrap(); + held.send(()).unwrap(); + // The holder awaits the network, as a token rotation does. + tokio::time::sleep(Duration::from_millis(200)).await; + drop(guard); + }); + holding.recv_timeout(Duration::from_secs(10)).unwrap(); + + let (acquired, signal) = mpsc::channel(); + for _ in 0..2 { + let (waiter, acquired) = (file_in(&dir, "hub.example"), acquired.clone()); + runtime.spawn(async move { + let _guard = SessionStore::lock(&waiter).unwrap(); + acquired.send(()).unwrap(); + }); + } + for _ in 0..2 { + signal + .recv_timeout(Duration::from_secs(10)) + .expect("both waiters get the lock once the holder is done"); + } +} diff --git a/crates/hub-io/src/devices.rs b/crates/hub-io/src/devices.rs new file mode 100644 index 00000000..7fcc1526 --- /dev/null +++ b/crates/hub-io/src/devices.rs @@ -0,0 +1,100 @@ +//! The devices of the account, listed and revoked through the original native membership. +//! +//! The membership is captured before the future is returned and rechecked after every await, +//! as in [`crate::connect`]'s sign-out: a result for an account that was signed out, replaced +//! or revoked meanwhile is [`HubError::Stale`] and is never shown for the new account. + +use std::sync::Arc; + +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{DeviceSummary, HubError, HubFuture, HubId}; +use tokio::runtime::Handle; + +use crate::classify::classify; +use crate::pool::{Pool, observation::LoginObservation}; +use crate::task::spawn; + +/// The captured membership and its client. +pub(crate) struct Captured { + pub(crate) observed: Arc, + pub(crate) client: HubClient, + pub(crate) hub: HubId, +} + +impl Captured { + pub(crate) fn new(pool: Arc, hub: &HubId) -> Result { + let observed = LoginObservation::capture(pool.clone(), hub.clone())?; + let subject = observed.original_subject()?.ok_or(HubError::NotSignedIn)?; + let client = pool.captured(hub, &subject)?; + observed.check()?; + Ok(Self { + observed, + client, + hub: hub.clone(), + }) + } +} + +pub(crate) fn list(pool: Arc, handle: &Handle, hub: &HubId) -> HubFuture> { + run(Captured::new(pool, hub), handle, |captured| async move { + let listed = captured + .observed + .during(captured.client.account().list_devices(v2::Empty {})) + .await? + .map_err(|status| classify(captured.hub.address(), ClientError::from(status)))? + .into_inner(); + Ok(listed + .devices + .into_iter() + .filter(|device| !device.revoked) + .map(|device| DeviceSummary { + id: device.device_id, + name: device.name, + platform: device.platform, + created_at_ms: device.created_at_ms, + last_seen_at_ms: device.last_seen_at_ms, + is_current: device.current, + }) + .collect()) + }) +} + +pub(crate) fn revoke(pool: Arc, handle: &Handle, hub: &HubId, device: &str) -> HubFuture<()> { + let request = v2::IdRequest { + id: device.to_owned(), + }; + run(Captured::new(pool, hub), handle, |captured| async move { + captured + .observed + .during(captured.client.account().revoke_device(request)) + .await? + .map_err(|status| classify(captured.hub.address(), ClientError::from(status)))?; + Ok(()) + }) +} + +/// Runs `work` on the adapter's threads once the membership was captured, and checks it again +/// before the result is returned. +pub(crate) fn run( + captured: Result, + handle: &Handle, + work: F, +) -> HubFuture +where + T: Send + 'static, + F: FnOnce(Arc) -> Fut, + Fut: std::future::Future> + Send + 'static, +{ + let captured = match captured { + Ok(captured) => Arc::new(captured), + Err(error) => return Box::pin(async move { Err(error) }), + }; + let observed = captured.observed.clone(); + let task = spawn(handle, work(captured)); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + observed.check()?; + result + }) +} diff --git a/crates/hub-io/src/directory.rs b/crates/hub-io/src/directory.rs new file mode 100644 index 00000000..a39cb653 --- /dev/null +++ b/crates/hub-io/src/directory.rs @@ -0,0 +1,24 @@ +//! Account display values are checked against the original authenticated native subject. +use crate::{account::key_admission::KeyAdmission, convert}; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{Account, HubError}; + +pub(crate) async fn who_am_i( + client: &HubClient, + owner: &KeyAdmission, +) -> Result { + let response = owner + .during(client.account().who_am_i(v2::Empty {})) + .await? + .map_err(|status| owner.failure(ClientError::from(status)))? + .into_inner(); + checked_account(owner, convert::account(response)) +} +pub(crate) fn checked_account(owner: &KeyAdmission, account: Account) -> Result { + owner.check()?; + if account.id.as_str() != owner.member().subject()?.account() { + return Err(HubError::Stale); + } + Ok(account) +} diff --git a/crates/hub-io/src/endpoint.rs b/crates/hub-io/src/endpoint.rs new file mode 100644 index 00000000..4157c46f --- /dev/null +++ b/crates/hub-io/src/endpoint.rs @@ -0,0 +1,48 @@ +//! The contract's endpoint for a hub and the trust decided for it. + +use nocterm_hub_client::{HubEndpoint, Trust}; +use nocterm_hubs_model::{HubEndpointSpec, HubError, TrustPolicy}; + +use crate::classify::remote; + +pub(crate) fn endpoint(spec: &HubEndpointSpec) -> Result { + let trust = match spec.trust { + TrustPolicy::Pinned(fingerprint) => Trust::Pinned(*fingerprint.as_bytes()), + TrustPolicy::PublicCa => Trust::PublicCa, + }; + HubEndpoint::with_trust(&spec.address.to_string(), trust).map_err(remote) +} + +#[cfg(test)] +mod tests { + use nocterm_hubs_model::{Fingerprint, HubAddress}; + + use super::*; + + fn spec(address: &str, trust: TrustPolicy) -> HubEndpointSpec { + HubEndpointSpec { + address: HubAddress::parse(address).unwrap(), + trust, + } + } + + #[test] + fn the_decided_trust_becomes_the_contracts_trust() { + let pinned = Fingerprint::from_bytes([9; 32]); + let endpoint = endpoint(&spec("hub.example:9000", TrustPolicy::Pinned(pinned))).unwrap(); + assert_eq!(endpoint.host(), "hub.example"); + assert_eq!(endpoint.port(), 9000); + assert_eq!(endpoint.trust(), &Trust::Pinned([9; 32])); + + let public = super::endpoint(&spec("hub.example", TrustPolicy::PublicCa)).unwrap(); + assert_eq!(public.port(), 7443); + assert_eq!(public.trust(), &Trust::PublicCa); + } + + #[test] + fn an_ipv6_address_keeps_its_brackets_out_of_the_host() { + let endpoint = endpoint(&spec("[::1]:7443", TrustPolicy::PublicCa)).unwrap(); + assert_eq!(endpoint.host(), "::1"); + assert_eq!(endpoint.port(), 7443); + } +} diff --git a/crates/hub-io/src/hub_account.rs b/crates/hub-io/src/hub_account.rs new file mode 100644 index 00000000..042f8691 --- /dev/null +++ b/crates/hub-io/src/hub_account.rs @@ -0,0 +1,89 @@ +//! The encrypted data of one signed-in account, as the application uses it. + +use std::sync::Arc; + +use tokio::runtime::Handle; + +use crate::account::Slot; +use crate::account::operations::AccountOperations; +use crate::account::source::LocalSourceProvider; +use crate::pool::Pool; +use crate::replica::HubProfileReplica; +use crate::vault::{HubVault, credentials::CredentialCapture}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +/// A hub account's profile replica and vault. They share the keys of one account: unlocking +/// the vault opens both, and locking it (or signing out) closes both. +pub struct HubAccount { + handle: Handle, + vault: Arc, + source: LocalSourceProvider, + credentials: CredentialCapture, + relay: crate::relay::RelayCapture, + operations: Arc, +} + +impl HubAccount { + pub(crate) fn new(handle: Handle, pool: Arc, slot: Arc) -> Self { + let relay = crate::relay::RelayCapture::new(handle.clone(), pool.clone()); + let source = LocalSourceProvider::new(Arc::clone(&slot), pool.clone()); + let operations = Arc::new(AccountOperations::new(source.clone(), &pool)); + let vault = Arc::new(HubVault::new( + handle.clone(), + Arc::clone(&slot), + operations.clone(), + )); + let credentials = + CredentialCapture::new(handle.clone(), operations.stores().clone(), &vault.idle()); + Self { + handle, + vault, + source, + credentials, + relay, + operations, + } + } + + /// The saved profiles of the account. + pub fn replica(&self) -> Result, HubError> { + self.local_replica(self.source.capture()?) + } + + /// An immutable replica for this exact authenticated account/key owner. + pub fn local_replica( + &self, + source: Arc, + ) -> Result, HubError> { + Ok(Arc::new(HubProfileReplica::new( + self.handle.clone(), + self.operations.bind(source)?, + ))) + } + + /// The vault of the account. + pub fn vault(&self) -> Arc { + Arc::clone(&self.vault) + } + /// Captures this configured hub's currently signed-in, unlocked account for + /// device-local history. Sign-out or replacement login makes it stale; lock and + /// ordinary token refresh do not. + pub fn local_source(&self) -> Result, HubError> { + self.source.capture() + } + /// Relay requester tied to the original account, membership and key owner. + pub fn local_relay( + &self, + source: Arc, + ) -> Result, HubError> { + self.relay.account(&self.source, source) + } + /// Credentials restricted to the captured native account and the caller's + /// current source. A stale capture never follows a replacement login. + pub fn local_credentials( + &self, + source: Arc, + ) -> Result, HubError> { + self.credentials.capture(&self.source, source) + } +} diff --git a/crates/hub-io/src/lib.rs b/crates/hub-io/src/lib.rs new file mode 100644 index 00000000..d3cd1a87 --- /dev/null +++ b/crates/hub-io/src/lib.rs @@ -0,0 +1,80 @@ +//! Hub adapter: reaches Nocterm hubs through the external hub contract. +//! +//! This is the only crate that knows the hub protocol exists. It speaks it through +//! `nocterm-hub-client` (pinned TLS, signed requests, token refresh) and shows the rest of the +//! application the neutral ports of `nocterm-hubs-model`, `nocterm-hub-agents-model`, +//! `nocterm-ai`, `nocterm-vault`, `nocterm-connections-model` and `nocterm-session`. Like +//! `nocterm-ssh`, it owns a small Tokio runtime, so callers need none: every call returns a +//! runtime-agnostic future. +//! +//! Owns: connections to hubs (one client per hub), the probe of a hub's certificate, password +//! and browser sign-in and sign-out (which revokes the device on the hub), the private +//! credential file of this device for each hub with its inter-process lock, the mapping of hub +//! failures to `HubError`, the mapping of hub records to the model's records, the relayed byte +//! streams of SSH routes that use a hub as their first step, and the encrypted data of one +//! signed-in account: the account key (opened at sign-in, unlock and recovery, zeroed on lock), +//! the vault collections sealed under it, their encrypted local cache, the vault ([`HubVault`]) +//! and the profile replica ([`HubProfileReplica`]) built on them, handed out together as a +//! [`HubAccount`]. +//! Agents, providers and servers (`agents`, `servers`) implement the ports of +//! `nocterm-hub-agents-model` over `AgentService` and `ServerService`; [`HubAgentConnector`] +//! implements `nocterm_ai::AgentConnector` over `ChatService` (a hub agent's chat is an +//! ordinary agent chat whose session is the hub chat), and [`HubChatRepository`] lists, renames, +//! pins and deletes hub chats beside the local history of a hub window. +//! Does not own: which hubs the user knows or trusts (the `[hubs]` settings section and the +//! trust decision belong to `nocterm-hubs-model` and the callers), connection status, retries, +//! windows, terminal emulation, the transcript of a chat, or any view. +//! Ports provided: `HubTrust`, `HubSignIn` and `AccountDevices` from `nocterm-hubs-model` +//! (by [`HubLinks`]) and `AccountKeyLock` (by [`AccountKeys`]); `HubAgentDirectory`, +//! `HubServers` and `HubChanges` (by [`HubAgentsLink`]); `AgentConnector` and +//! `ChatRepository` of `nocterm-ai`; the `LocalAccountSource` captured by [`HubAccount`], its +//! captured `CredentialStore` of `nocterm-session`, the `VaultAccess` of `nocterm-vault` and +//! the `ProfileReplica` of `nocterm-connections-model`, implemented by [`HubVault`] and +//! [`HubProfileReplica`], and the `RelayDialer` of `nocterm-session`, implemented by +//! [`HubRelay`] over the clients of the hubs this device is signed in to. Ports used: none. +//! Settings section: none; the callers pass the directory for credentials. +//! Globals: none. +//! Invariants: the device key and tokens live only in a mode-0600 file of a mode-0700 +//! directory, published atomically and read under an inter-process lock; a pinned hub is +//! trusted only with the exact pinned certificate; a revoked device erases its credentials, its +//! account cache and its keys; the account key lives only in memory, is zeroed on lock and +//! follows its own lock policy, independent of the local vault's; only ciphertext and non-secret +//! facts are written to disk (mode 0600, atomically); the local history partition of a hub +//! window depends on the signed-in account only, so a locked vault never blocks chats; no tonic +//! or prost type crosses the crate boundary, and no text built from a failure does; a relay +//! finds its hub only among the hubs signed in to and never signs in; every buffer between the +//! transport and the hub's stream is bounded, and dropping either end of a relayed stream ends +//! both directions; a record of the hub that does not authenticate is an error, never a silent +//! skip or overwrite; collections and record kinds this version does not know are never +//! listed, changed or deleted. + +mod account; +mod account_keys; +mod agents; +mod cache; +mod chats; +mod checked; +mod classify; +mod connect; +mod convert; +mod credentials; +mod devices; +mod directory; +mod endpoint; +mod hub_account; +mod links; +mod pool; +mod relay; +mod replica; +mod store; +mod task; +mod vault; + +pub use account_keys::AccountKeys; +pub use agents::HubAgentsLink; +pub use chats::{HubAgentConnector, HubChatRepository}; +pub use hub_account::HubAccount; +pub use links::{HubLinks, HubLinksConfig, HubStartError}; +pub use relay::HubRelay; +pub use replica::HubProfileReplica; +pub use vault::HubVault; diff --git a/crates/hub-io/src/links.rs b/crates/hub-io/src/links.rs new file mode 100644 index 00000000..ec70b4f6 --- /dev/null +++ b/crates/hub-io/src/links.rs @@ -0,0 +1,276 @@ +//! The hub adapter's entry point: the runtime and the three ports. + +use std::future::Future; +use std::path::PathBuf; +use std::sync::Arc; + +use futures::FutureExt as _; +use futures::channel::oneshot; +use futures::future::BoxFuture; +use nocterm_hub_client::HubEndpoint; +use nocterm_hubs_model::{ + AccountDevices, DeviceSummary, Fingerprint, HubAddress, HubEndpointSpec, HubError, HubFuture, + HubId, HubInfo, HubSignIn, HubTrust, KeyStatus, OidcSignIn, ProbedHub, SignedIn, +}; +use nocterm_session::Secret; +use tokio::runtime::{Builder, Handle, Runtime}; + +use crate::account::{Accounts, KeyFlow}; +use crate::classify::{probe_failed, remote}; +use crate::connect::Connector; +use crate::hub_account::HubAccount; +use crate::pool::Pool; + +/// Threads serving every hub connection of the application. +const WORKER_THREADS: usize = 2; + +/// Where the adapter keeps what it needs and how this device is named to hubs. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct HubLinksConfig { + /// The private directory with one credential file per hub. Created when first needed. + pub credentials_dir: PathBuf, + /// The machine as the user knows it, listed by every hub this device signs in to. + pub device_name: String, +} + +/// The runtime of the adapter could not start. +#[derive(Debug, thiserror::Error)] +#[error("the hub connection threads cannot start")] +pub struct HubStartError(#[source] std::io::Error); + +/// Reaches hubs. Implements the hub ports of `nocterm-hubs-model`. +/// +/// Calls run on the adapter's threads; dropping the future of a call abandons it. Dropping the +/// links ends every call under way. +pub struct HubLinks { + // `None` only while the links are being dropped. + runtime: Option, + handle: Handle, + pool: Arc, + connector: Arc, + accounts: Arc, + keys: Arc, + chat_hints: Arc, +} + +/// The adapter's threads have stopped. +#[derive(Debug, thiserror::Error)] +#[error("the hub connection threads have stopped")] +struct Stopped; + +impl HubLinks { + pub fn start(config: HubLinksConfig) -> Result, HubStartError> { + let runtime = Builder::new_multi_thread() + .worker_threads(WORKER_THREADS) + .thread_name("nocterm-hub") + .enable_all() + .build() + .map_err(HubStartError)?; + let accounts = Arc::new(Accounts::with_runtime( + config.credentials_dir.clone(), + Some(runtime.handle().clone()), + )); + let pool = Arc::new(Pool::new( + config.credentials_dir, + &config.device_name, + Arc::clone(&accounts), + )); + let keys = KeyFlow::new( + Arc::clone(&pool), + Arc::clone(&accounts), + runtime.handle().clone(), + ); + Ok(Arc::new(Self { + handle: runtime.handle().clone(), + runtime: Some(runtime), + connector: Arc::new(Connector::new(Arc::clone(&pool), Arc::clone(&keys))), + pool, + accounts, + keys, + chat_hints: Arc::default(), + })) + } + + /// The encrypted data of the account this device is signed in to on `hub`: its profile + /// replica and its vault. Both share the account's keys, which open at sign-in and close + /// when the vault locks or the device signs out. + pub fn open_account(&self, hub: &HubId) -> Result { + let slot = self.accounts.slot(hub)?; + Ok(HubAccount::new( + self.handle.clone(), + Arc::clone(&self.pool), + slot, + )) + } + + pub(crate) fn account_keys_open(&self, hub: &HubId) -> bool { + self.accounts + .loaded_slot(hub) + .is_some_and(|slot| slot.open().is_some()) + } + + /// Native membership publication always creates its Slot first. An absent + /// loaded Slot therefore cannot be unlocked for a current native recipient. + pub(crate) fn loaded_account(&self, hub: &HubId) -> Option { + self.accounts + .loaded_slot(hub) + .map(|slot| HubAccount::new(self.handle.clone(), self.pool.clone(), slot)) + } + + /// What a relay needs of the links: their threads and the hubs they are signed in to. + pub(crate) fn relay_parts(&self) -> (Handle, Arc) { + (self.handle.clone(), Arc::clone(&self.pool)) + } + + /// Hints that a chat list kept on this device changed. + pub(crate) fn chat_hints(&self) -> Arc { + Arc::clone(&self.chat_hints) + } + + /// What the agents and chats adapters need of the links. + pub(crate) fn agent_parts(&self) -> (Handle, Arc) { + (self.handle.clone(), Arc::clone(&self.pool)) + } + + fn run_boxed( + &self, + work: impl Future> + Send + 'static, + ) -> HubFuture { + crate::task::spawn(&self.handle, work) + .map(|result| result.unwrap_or_else(|| Err(remote(Stopped)))) + .boxed() + } +} + +impl Drop for HubLinks { + fn drop(&mut self) { + self.pool.stop(); + // A runtime must not block where it is dropped: that may be a thread that is itself + // running async code. + if let Some(runtime) = self.runtime.take() { + runtime.shutdown_background(); + } + } +} + +impl HubTrust for HubLinks { + fn probe(&self, address: &HubAddress) -> HubFuture { + let address = address.clone(); + self.run_boxed(async move { + let probe = HubEndpoint::probe(&address.to_string()) + .await + .map_err(|error| probe_failed(&address, error))?; + Ok(ProbedHub { + fingerprint: Fingerprint::from_bytes(probe.fingerprint), + publicly_trusted: probe.publicly_trusted, + }) + }) + } +} + +impl HubSignIn for HubLinks { + fn connect(&self, endpoint: HubEndpointSpec) -> HubFuture { + match self.connector.reconnect(endpoint) { + Ok(request) => request.start(&self.handle), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + fn password( + &self, + endpoint: HubEndpointSpec, + username: String, + password: Secret, + ) -> HubFuture { + let connector = Arc::clone(&self.connector); + match connector.begin(endpoint) { + Ok(start) => start.start(&self.handle, move |start| async move { + connector.password(start, username, password).await + }), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + fn oidc(&self, endpoint: HubEndpointSpec, provider: String) -> OidcSignIn { + let connector = Arc::clone(&self.connector); + let (browser_sender, browser_receiver) = oneshot::channel(); + match connector.begin(endpoint) { + Ok(start) => { + let observed = start.observation().clone(); + let done = start.start(&self.handle, move |start| async move { + connector.oidc(start, provider, browser_sender).await + }); + let browser = Box::pin(async move { + observed.check()?; + let result = observed + .during(browser_receiver) + .await? + .map_err(|_| HubError::Stale)?; + observed.check()?; + Ok(result) + }); + OidcSignIn { browser, done } + } + Err(error) => OidcSignIn { + browser: Box::pin(async { Err(HubError::Stale) }), + done: Box::pin(async move { Err(error) }), + }, + } + } + + fn set_vault_passphrase(&self, hub: &HubId, passphrase: Secret) -> HubFuture { + let keys = self.keys.clone(); + self.keys + .run(self.keys.capture(hub), move |owner| async move { + keys.passphrase(&owner, passphrase).await + }) + } + + fn recover(&self, hub: &HubId, recovery_key: Secret) -> HubFuture<()> { + let keys = self.keys.clone(); + self.keys + .run(self.keys.capture(hub), move |owner| async move { + keys.recover(&owner, recovery_key).await + }) + } + + fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + self.keys.recovery_key_unconfirmed(hub) + } + + fn keys_revision(&self) -> u64 { + self.keys.keys_revision() + } + + fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64> { + self.keys.keys_changed(seen) + } + + fn recovery_key_offer( + &self, + hub: &HubId, + ) -> Result { + self.keys.recovery_offer(hub) + } + + fn sign_out(&self, hub: &HubId) -> HubFuture<()> { + match self.connector.sign_out(hub) { + Ok(request) => request.start(&self.handle), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + fn forget(&self, hub: &HubId) -> Result<(), HubError> { + self.connector.forget(hub) + } +} + +impl AccountDevices for HubLinks { + fn list(&self, hub: &HubId) -> HubFuture> { + crate::devices::list(Arc::clone(&self.pool), &self.handle, hub) + } + + fn revoke(&self, hub: &HubId, device: &str) -> HubFuture<()> { + crate::devices::revoke(Arc::clone(&self.pool), &self.handle, hub, device) + } +} diff --git a/crates/hub-io/src/pool.rs b/crates/hub-io/src/pool.rs new file mode 100644 index 00000000..d71e6fc2 --- /dev/null +++ b/crates/hub-io/src/pool.rs @@ -0,0 +1,442 @@ +//! The clients of the hubs this device is signed in to, and where it keeps their credentials. + +pub(crate) mod captured; +pub(crate) mod observation; +use observation::{MembershipEnd, ObservedLogin}; + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::{ + Arc, Mutex, MutexGuard, PoisonError, + atomic::{AtomicBool, Ordering}, +}; + +use nocterm_hub_client::{DeviceInfo, HubClient}; +use nocterm_hubs_model::{HubAddress, HubError, HubId, TrustPolicy}; + +use crate::account::source::{AccountSubject, AccountSubjects}; +use crate::account::{Accounts, OpenKeys, Slot}; +use crate::classify::credential_file; +use crate::credentials::{CredentialDir, CredentialFile, CredentialLoginId, LoginPublication}; + +/// The application name a hub lists beside the device. +const APP_NAME: &str = "Nocterm"; + +/// The client of a hub and the trust it was connected with. +struct Pooled { + trust: TrustPolicy, + client: HubClient, + subject: Option>, + login: Option, +} + +/// The exact native owner created by one successful explicit publication. +pub(crate) struct PublishedLogin { + login: CredentialLoginId, + subject: Arc, +} +impl PublishedLogin { + pub(crate) fn into_parts(self) -> (CredentialLoginId, Arc) { + (self.login, self.subject) + } +} + +/// One client per hub: clients of a hub share the turn in which its tokens are renewed, and +/// two of them would renew the same refresh token against each other. +/// +/// The pool also knows the account keys of the hubs, only to wipe them when a hub revokes this +/// device: whatever the hub no longer trusts leaves nothing of its account behind. +pub(crate) struct Pool { + credentials: CredentialDir, + device: DeviceInfo, + accounts: Arc, + clients: Mutex>, + stopped: AtomicBool, +} + +impl Pool { + pub(crate) fn new( + credentials_dir: PathBuf, + device_name: &str, + accounts: Arc, + ) -> Self { + Self { + credentials: CredentialDir::new(credentials_dir), + device: DeviceInfo::new(device_name, APP_NAME), + accounts, + clients: Mutex::default(), + stopped: AtomicBool::new(false), + } + } + + pub(crate) fn stop(&self) { + self.stopped.store(true, Ordering::Release); + } + pub(crate) fn is_stopped(&self) -> bool { + self.stopped.load(Ordering::Acquire) + } + pub(crate) fn device(&self) -> &DeviceInfo { + &self.device + } + + pub(crate) fn file(&self, address: &HubAddress) -> Result { + self.credentials.file(address).map_err(credential_file) + } + + fn clients(&self) -> MutexGuard<'_, HashMap> { + self.clients.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// Publishes only this captured file origin and original native selection. + /// All paths acquire clients before the credential file lock. + pub(crate) fn publish_login( + &self, + hub: HubId, + trust: TrustPolicy, + client: HubClient, + publication: LoginPublication<'_>, + expected: &ObservedLogin, + ) -> Result { + let subject = Arc::new(subject_of(&client).ok_or(HubError::NotSignedIn)?); + let session = publication.session(); + if subject.account() != session.account_id || subject.device() != session.device_id { + return Err(HubError::Stale); + } + let slot = self.accounts.slot(&hub)?; + let mut clients = self.clients(); + self.validate_observed(&clients, &hub, &slot, expected)?; + let login = publication.save().map_err(credential_file)?; + publication.bind(&client, login); + slot.begin_membership(&subject); + clients.insert( + hub, + Pooled { + trust, + client, + subject: Some(subject.clone()), + login: Some(login), + }, + ); + Ok(PublishedLogin { login, subject }) + } + + /// A reconnect/refresh retains membership only for the same actual subject + /// and trust. Explicit sign-in publishes a new local login even for the same ids. + pub(crate) fn renew_observed( + &self, + hub: HubId, + trust: TrustPolicy, + client: HubClient, + login: Option, + expected: &ObservedLogin, + ) -> Result { + self.renew_inner(hub, trust, client, login, Some(expected)) + } + + fn renew_inner( + &self, + hub: HubId, + trust: TrustPolicy, + client: HubClient, + login: Option, + expected: Option<&ObservedLogin>, + ) -> Result { + let actual = subject_of(&client).ok_or(HubError::NotSignedIn)?; + let slot = self.accounts.slot(&hub)?; + let mut clients = self.clients(); + if let Some(expected) = expected { + self.validate_observed(&clients, &hub, &slot, expected)?; + } + if !self + .file(hub.address())? + .matches_login(actual.account(), actual.device(), login) + .map_err(credential_file)? + { + return Err(HubError::Stale); + } + let retained = clients.get(&hub).and_then(|pooled| { + let subject = pooled.subject.as_ref()?; + (pooled.trust == trust + && pooled.login == login + && pooled.client.info().hub_id == client.info().hub_id + && &actual == subject.as_ref() + && subject_of(&pooled.client).as_ref() == Some(subject.as_ref())) + .then(|| Arc::clone(subject)) + }); + let subject = match retained { + Some(subject) => subject, + None => { + let subject = Arc::new(actual); + slot.begin_membership(&subject); + subject + } + }; + clients.insert( + hub, + Pooled { + trust, + client, + subject: Some(subject.clone()), + login, + }, + ); + Ok(ObservedLogin::new(Some(&subject), slot.observed())) + } + + /// The client of the hub if it was connected with this trust. + pub(crate) fn connected( + &self, + hub: &HubId, + trust: TrustPolicy, + ) -> Option<(HubClient, Option)> { + let clients = self.clients(); + let pooled = clients.get(hub)?; + (pooled.trust == trust).then(|| (pooled.client.clone(), pooled.login)) + } + + /// A direct configured-hub removal serializes against every pending login. + pub(crate) fn forget_hub(&self, hub: &HubId) -> Result<(), HubError> { + let slot = self.accounts.slot(hub)?; + let mut clients = self.clients(); + clients.remove(hub); + let cache = slot.erase(); + let file = self + .file(hub.address())? + .erase() + .map_err(crate::classify::credentials); + if cache.is_ok() { + self.accounts.evict_if(hub, &slot); + } + cache.and(file) + } + + /// Acquires only this original membership, synchronously and without I/O. + pub(crate) fn captured( + &self, + hub: &HubId, + expected: &Arc, + ) -> Result { + self.with_captured(hub, expected, |client| Ok(client.clone())) + } + + /// One admission policy for original native I/O and synchronous publication. + /// The action may lock Slot/cache, but must never await or call back into Pool. + pub(crate) fn with_captured( + &self, + hub: &HubId, + expected: &Arc, + action: impl FnOnce(&HubClient) -> Result, + ) -> Result { + let clients = self.clients(); + let pooled = self.matching(&clients, hub, expected)?; + action(&pooled.client) + } + + fn matching<'a>( + &self, + clients: &'a HashMap, + hub: &HubId, + expected: &Arc, + ) -> Result<&'a Pooled, HubError> { + let pooled = clients.get(hub).ok_or(HubError::Stale)?; + let subject = pooled.subject.as_ref().ok_or(HubError::Stale)?; + if self.is_stopped() + || !Arc::ptr_eq(subject, expected) + || subject_of(&pooled.client).as_ref() != Some(expected.as_ref()) + { + return Err(HubError::Stale); + } + Ok(pooled) + } + + pub(crate) fn observe(&self, hub: &HubId) -> Result<(Arc, ObservedLogin), HubError> { + // Use the existing subject reader to retire an SDK subject that already ended. + // Otherwise a cleared SDK session would leave reconnect stuck on a stale map row. + let _ = self.current(hub); + let slot = self.accounts.slot(hub)?; + let clients = self.clients(); + if self.is_stopped() { + return Err(HubError::Stale); + } + let subject = clients.get(hub).and_then(|pooled| pooled.subject.as_ref()); + if let Some(subject) = subject { + self.matching(&clients, hub, subject)?; + } + let observed = ObservedLogin::new(subject, slot.observed()); + Ok((slot, observed)) + } + + fn validate_observed( + &self, + clients: &HashMap, + hub: &HubId, + slot: &Slot, + expected: &ObservedLogin, + ) -> Result<(), HubError> { + if self.is_stopped() || slot.hub() != hub { + return Err(HubError::Stale); + } + match expected.subject()? { + Some(subject) => { + self.matching(clients, hub, &subject)?; + return slot.belongs_to(&subject); + } + None if clients.contains_key(hub) => return Err(HubError::Stale), + None => {} + } + slot.check_observed(expected.receipt()) + } + + pub(crate) fn with_observed( + &self, + hub: &HubId, + slot: &Slot, + expected: &ObservedLogin, + action: impl FnOnce() -> Result, + ) -> Result { + let clients = self.clients(); + self.validate_observed(&clients, hub, slot, expected)?; + action() + } + + pub(crate) fn observe_original( + &self, + hub: &HubId, + expected: &Arc, + ) -> Result { + let slot = self.accounts.slot(hub)?; + self.with_captured(hub, expected, |_| { + slot.belongs_to(expected)?; + Ok(ObservedLogin::new(Some(expected), slot.observed())) + }) + } + + pub(crate) fn retire_captured( + &self, + hub: &HubId, + expected: &Arc, + slot: &Slot, + keys: &Arc, + ) { + if self.retire_membership(hub, expected) + && let Err(error) = slot.erase_if(keys) + { + tracing::warn!(%error, "cannot erase captured revoked account cache"); + } + } + + /// Retires the exact membership and its local file login; relay callers + /// without unlocked keys use this same conditional credential boundary. + pub(crate) fn retire_membership(&self, hub: &HubId, expected: &Arc) -> bool { + match self.retire_original(hub, expected, false) { + Ok(Some(ended)) => { + let (_, result) = ended.into_parts(); + if let Err(error) = result { + tracing::warn!(%error,"cannot retire original revoked membership"); + } + true + } + Ok(None) => false, + Err(error) => { + tracing::warn!(%error,"cannot retire original revoked membership"); + false + } + } + } + + pub(crate) fn end_membership( + &self, + hub: &HubId, + expected: &Arc, + expire: bool, + ) -> Result { + self.retire_original(hub, expected, expire)? + .ok_or(HubError::Stale) + } + + fn retire_original( + &self, + hub: &HubId, + expected: &Arc, + expire: bool, + ) -> Result, HubError> { + let mut clients = self.clients(); + if !clients + .get(hub) + .and_then(|pooled| pooled.subject.as_ref()) + .is_some_and(|subject| Arc::ptr_eq(subject, expected)) + { + return Ok(None); + } + let slot = self.accounts.slot(hub)?; + let login = clients.remove(hub).and_then(|pooled| pooled.login); + let mut slot_error = None; + let retired = self + .file(hub.address())? + .retire_if( + expected.account(), + expected.device(), + login, + !expire, + || { + let result = if expire { + slot.close_for(expected).map(|_| ()) + } else { + slot.erase_for(expected) + }; + slot_error = result.err(); + }, + ) + .map_err(credential_file); + if !matches!(&retired, Ok(true)) { + // A newer private-file login belongs to somebody else. Drop only our + // original in-memory keys; never remove that login's disk cache. + let _ = slot.close_for(expected); + } + let result = retired.and_then(|owned| { + if let Some(error) = slot_error { + return Err(error); + } + if owned { Ok(()) } else { Err(HubError::Stale) } + }); + Ok(Some(MembershipEnd::new( + ObservedLogin::new(None, slot.observed()), + result, + ))) + } +} + +impl AccountSubjects for Pool { + fn with_current( + &self, + hub: &HubId, + expected: &Arc, + action: &mut dyn FnMut() -> Result<(), HubError>, + ) -> Result<(), HubError> { + self.with_captured(hub, expected, |_| action()) + } + + fn current(&self, hub: &HubId) -> Option> { + let mut clients = self.clients(); + let pooled = clients.get(hub)?; + let subject = pooled.subject.as_ref()?; + if subject_of(&pooled.client).as_ref() == Some(subject.as_ref()) { + return Some(Arc::clone(subject)); + } + // A changed or cleared SDK subject cannot later revive this membership. + let original = Arc::clone(subject); + clients.remove(hub); + if let Some(slot) = self.accounts.loaded_slot(hub) { + let _ = slot.close_for(&original); + } + None + } +} + +pub(crate) fn subject_of(client: &HubClient) -> Option { + let session = client.session()?; + Some(AccountSubject::new(session.account_id, session.device_id)) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/pool/captured.rs b/crates/hub-io/src/pool/captured.rs new file mode 100644 index 00000000..72c43a59 --- /dev/null +++ b/crates/hub-io/src/pool/captured.rs @@ -0,0 +1,156 @@ +//! One original SDK client and native membership, shared by captured I/O consumers. +use super::{Pool, subject_of}; +use crate::{ + account::{ + OpenKeys, Slot, + source::{AccountSubject, AccountSubjects, CapturedAccount}, + }, + classify::classify, +}; +use nocterm_hub_client::{ClientError, HubClient, StatusCode}; +use nocterm_hubs_model::{HubError, HubId, source_identity::LocalAccountSource}; +use std::sync::{Arc, Weak}; + +struct KeyOwner { + slot: Weak, + keys: Weak, +} +pub(crate) struct CapturedClient { + pool: Arc, + hub: HubId, + client: HubClient, + subject: Weak, + source: Option>, + owner: Option, +} +impl CapturedClient { + /// Capture the actual signed-in requester now, before any worker is spawned. + pub(crate) fn current(pool: Arc, hub: HubId) -> Result { + let subject = pool.current(&hub).ok_or(HubError::NotSignedIn)?; + let client = pool.captured(&hub, &subject)?; + let owner = pool.accounts.loaded_slot(&hub).and_then(|slot| { + let keys = slot.open()?; + (keys.keyset().account_id() == subject.account()).then(|| KeyOwner { + slot: Arc::downgrade(&slot), + keys: Arc::downgrade(&keys), + }) + }); + Ok(Self { + pool, + hub, + client, + subject: Arc::downgrade(&subject), + source: None, + owner, + }) + } + pub(crate) fn account( + pool: Arc, + native: &CapturedAccount, + source: Arc, + ) -> Result { + source.check_current().map_err(|_| HubError::NotSignedIn)?; + native.check_current().map_err(|_| HubError::NotSignedIn)?; + if source.partition() != native.partition() { + return Err(HubError::NotSignedIn); + } + let slot = native.slot()?; + let keys = native.keys()?; + let subject = native.subject()?; + let hub = slot.hub().clone(); + let client = pool.captured(&hub, &subject)?; + Ok(Self { + pool, + hub, + client, + subject: Arc::downgrade(&subject), + source: Some(source), + owner: Some(KeyOwner { + slot: Arc::downgrade(&slot), + keys: Arc::downgrade(&keys), + }), + }) + } + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + pub(crate) fn check(&self) -> Result<(), HubError> { + if self.pool.is_stopped() { + return Err(HubError::NotSignedIn); + } + if let Some(source) = &self.source { + source.check_current().map_err(|_| HubError::NotSignedIn)?; + } + let expected = self.subject.upgrade().ok_or(HubError::NotSignedIn)?; + self.pool.captured(&self.hub, &expected)?; + if subject_of(&self.client).as_ref() != Some(expected.as_ref()) { + return Err(HubError::NotSignedIn); + } + if self.source.is_some() + && let Some(owner) = &self.owner + { + let slot = owner.slot.upgrade().ok_or(HubError::NotSignedIn)?; + let keys = owner.keys.upgrade().ok_or(HubError::KeysLocked)?; + if !slot + .open() + .is_some_and(|current| Arc::ptr_eq(¤t, &keys)) + { + return Err(HubError::NotSignedIn); + } + } + Ok(()) + } + /// Actual worker admission, including every resume after an awaited operation. + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } + pub(crate) async fn fresh(&self) -> Result { + self.check()?; + self.during(self.client.ensure_fresh()) + .await? + .map_err(|error| self.failure(error))?; + self.check()?; + Ok(self.client.clone()) + } + pub(crate) fn failure(&self, error: ClientError) -> HubError { + if matches!(error, ClientError::Revoked) + && let Some(subject) = self.subject.upgrade() + { + let original = self + .owner + .as_ref() + .and_then(|owner| Some((owner.slot.upgrade()?, owner.keys.upgrade()?))); + if let Some((slot, keys)) = original { + self.pool.retire_captured(&self.hub, &subject, &slot, &keys); + } else { + self.pool.retire_membership(&self.hub, &subject); + } + } + if matches!( + &error, + ClientError::NotSignedIn + | ClientError::Status { + code: StatusCode::Unauthenticated, + .. + } + ) { + let Some(subject) = self.subject.upgrade() else { + return HubError::Stale; + }; + // Authentication rejection ends only the original local login. Unlike + // an explicit Revoked code, expiry keeps the device key and ciphertext. + let ended = match self.pool.end_membership(&self.hub, &subject, true) { + Ok(ended) => ended, + Err(error) => return error, + }; + let (_, retired) = ended.into_parts(); + if let Err(error) = retired { + return error; + } + } + classify(self.hub.address(), error) + } +} diff --git a/crates/hub-io/src/pool/observation.rs b/crates/hub-io/src/pool/observation.rs new file mode 100644 index 00000000..bad32f14 --- /dev/null +++ b/crates/hub-io/src/pool/observation.rs @@ -0,0 +1,155 @@ +//! A reconnect observes one original native owner, including an initially absent one. +use super::{AccountSubject, Pool}; +use crate::account::{Slot, SlotReceipt}; +use crate::{ + classify::credential_file, + credentials::{CredentialFile, CredentialLoginId}, +}; +use nocterm_hubs_model::{HubError, HubId}; +use std::sync::{Arc, Mutex, PoisonError, Weak}; + +#[derive(Clone)] +pub(crate) struct ObservedLogin { + subject: Option>, + receipt: SlotReceipt, +} +impl ObservedLogin { + pub(super) fn new(subject: Option<&Arc>, receipt: SlotReceipt) -> Self { + Self { + subject: subject.map(Arc::downgrade), + receipt, + } + } + pub(super) fn subject(&self) -> Result>, HubError> { + self.subject + .as_ref() + .map(|subject| subject.upgrade().ok_or(HubError::Stale)) + .transpose() + } + pub(crate) fn receipt(&self) -> &SlotReceipt { + &self.receipt + } +} + +/// Retirement can close the local owner even when a disk cleanup reports an error. +pub(crate) struct MembershipEnd { + observed: ObservedLogin, + result: Result<(), HubError>, +} +impl MembershipEnd { + pub(super) fn new(observed: ObservedLogin, result: Result<(), HubError>) -> Self { + Self { observed, result } + } + pub(crate) fn into_parts(self) -> (ObservedLogin, Result<(), HubError>) { + (self.observed, self.result) + } +} + +pub(crate) struct LoginObservation { + pool: Arc, + hub: HubId, + slot: Arc, + expected: Mutex, +} +impl LoginObservation { + pub(crate) fn capture(pool: Arc, hub: HubId) -> Result, HubError> { + let (slot, expected) = pool.observe(&hub)?; + Ok(Arc::new(Self { + pool, + hub, + slot, + expected: Mutex::new(expected), + })) + } + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + pub(crate) fn slot(&self) -> &Arc { + &self.slot + } + pub(crate) fn snapshot(&self) -> ObservedLogin { + self.expected + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } + pub(crate) fn published(&self, expected: ObservedLogin) { + *self.expected.lock().unwrap_or_else(PoisonError::into_inner) = expected; + } + pub(crate) fn ended_absence(&self, receipt: SlotReceipt) { + self.published(ObservedLogin::new(None, receipt)); + } + pub(crate) fn original_subject(&self) -> Result>, HubError> { + self.snapshot().subject() + } + pub(crate) fn became(&self, subject: &Arc) -> Result<(), HubError> { + self.published(self.pool.observe_original(&self.hub, subject)?); + Ok(()) + } + pub(crate) fn retire( + &self, + file: &CredentialFile, + subject: Option<(&str, &str)>, + login: Option, + erase: bool, + ) -> Result<(), HubError> { + if let Some(subject) = self.original_subject()? { + let (next, result) = self + .pool + .end_membership(&self.hub, &subject, !erase)? + .into_parts(); + self.published(next); + return result; + } + let expected = self.snapshot(); + let next = self.with_current(|| { + let mut slot_error = None; + let owned = match subject { + Some((account, device)) => file + .retire_if(account, device, login, erase, || { + match self.slot.end_observed(expected.receipt(), erase) { + Ok(ended) => { + let (receipt, result) = ended.into_parts(); + self.ended_absence(receipt); + slot_error = result.err(); + } + Err(error) => slot_error = Some(error), + } + }) + .map_err(credential_file)?, + None => false, + }; + if !owned { + let (receipt, _) = self + .slot + .end_observed(expected.receipt(), false)? + .into_parts(); + self.ended_absence(receipt); + return Err(HubError::Stale); + } + if let Some(error) = slot_error { + return Err(error); + } + Ok(ObservedLogin::new(None, self.slot.observed())) + })?; + self.published(next); + Ok(()) + } + pub(crate) fn check(&self) -> Result<(), HubError> { + self.pool + .with_observed(&self.hub, &self.slot, &self.snapshot(), || Ok(())) + } + pub(crate) fn with_current( + &self, + action: impl FnOnce() -> Result, + ) -> Result { + self.pool + .with_observed(&self.hub, &self.slot, &self.snapshot(), action) + } + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} diff --git a/crates/hub-io/src/pool/tests.rs b/crates/hub-io/src/pool/tests.rs new file mode 100644 index 00000000..c8ceb42f --- /dev/null +++ b/crates/hub-io/src/pool/tests.rs @@ -0,0 +1,174 @@ +//! Offline/reconnect failures retire the captured native file and Slot receipt. +use super::*; +use crate::{ + account::testing::{ACCOUNT_ID, hub, sign_in}, + cache::CacheFile, + pool::observation::LoginObservation, +}; +use nocterm_hub_client::Session; +use nocterm_vault::VaultStatus; + +#[test] +fn forgetting_a_hub_releases_its_original_slot_and_fences_old_absent_observations() { + let login = Login::new(); + let original = Arc::downgrade(&login.slot); + login.pool.forget_hub(&hub()).unwrap(); + assert!(matches!(login.observed.check(), Err(HubError::Stale))); + assert_eq!(login.slot.status(), VaultStatus::Missing); + let accounts = login.accounts.clone(); + drop(login); + assert!(original.upgrade().is_none()); + assert_eq!( + accounts.slot(&hub()).unwrap().status(), + VaultStatus::Missing + ); +} + +#[test] +fn a_retained_account_factory_can_sign_in_after_forget_without_reviving_its_old_capture() { + use crate::account::{source::LocalSourceProvider, testing::ScriptedSubjects}; + let login = Login::new(); + let subjects = Arc::new(ScriptedSubjects::for_slot(&login.slot)); + subjects.sign_in(hub(), ACCOUNT_ID, "device"); + sign_in(&login.accounts); + let factory = LocalSourceProvider::new(login.slot.clone(), subjects.clone()); + let original = factory.capture().unwrap(); + login.pool.forget_hub(&hub()).unwrap(); + subjects.evict(&hub()); + assert!(factory.capture().is_err()); + // Settings publication failed: the same configured factory remains installed. + subjects.sign_in(hub(), ACCOUNT_ID, "device"); + sign_in(&login.accounts); + let replacement = factory.capture().unwrap(); + assert!(replacement.check_current().is_ok()); + assert!(original.check_current().is_err()); +} + +struct Login { + directory: tempfile::TempDir, + accounts: Arc, + pool: Arc, + file: CredentialFile, + slot: Arc, + session: Session, + login: CredentialLoginId, + observed: Arc, +} +impl Login { + fn new() -> Self { + let directory = tempfile::tempdir().unwrap(); + let accounts = Arc::new(Accounts::new(directory.path().to_path_buf())); + let (slot, _) = sign_in(&accounts); + let pool = Arc::new(Pool::new( + directory.path().to_path_buf(), + "test", + accounts.clone(), + )); + let file = pool.file(hub().address()).unwrap(); + file.load_or_create().unwrap(); + let session = Session { + account_id: ACCOUNT_ID.into(), + device_id: "device".into(), + access_token: "access-original".into(), + refresh_token: "refresh-original".into(), + access_expires_at_ms: i64::MAX, + refresh_expires_at_ms: i64::MAX, + }; + let login = file.save_session(&session).unwrap(); + let observed = LoginObservation::capture(pool.clone(), hub()).unwrap(); + Self { + directory, + accounts, + pool, + file, + slot, + session, + login, + observed, + } + } + fn cached(&self) -> bool { + CacheFile::new(self.directory.path(), hub().address()) + .unwrap() + .load() + .unwrap() + .is_some() + } + fn retire(&self, erase: bool) -> Result<(), HubError> { + self.observed.retire( + &self.file, + Some((&self.session.account_id, &self.session.device_id)), + Some(self.login), + erase, + ) + } +} + +#[test] +fn a_captured_revocation_erases_only_its_original_native_login_keys_and_cache() { + let login = Login::new(); + assert!(login.cached()); + login.retire(true).unwrap(); + assert!(login.slot.open().is_none()); + assert_eq!(login.slot.status(), VaultStatus::Missing); + assert!(!login.cached()); + assert!(login.file.load().unwrap().is_none()); +} + +#[test] +fn a_captured_revocation_preserves_the_keys_and_cache_of_another_hub() { + let login = Login::new(); + let other = HubId::from(HubAddress::parse("other.test").unwrap()); + let file = login.pool.file(other.address()).unwrap(); + file.load_or_create().unwrap(); + let mut session = login.session.clone(); + session.account_id = "other-account".into(); + let id = file.save_session(&session).unwrap(); + let observed = LoginObservation::capture(login.pool.clone(), other).unwrap(); + observed + .retire( + &file, + Some((&session.account_id, &session.device_id)), + Some(id), + true, + ) + .unwrap(); + assert_eq!(login.slot.status(), VaultStatus::Unlocked); + assert!(login.cached()); + assert_eq!( + login.file.load().unwrap().unwrap().session, + Some(login.session) + ); +} + +#[test] +fn captured_expiry_closes_keys_but_keeps_the_device_key_and_ciphertext_for_sign_in() { + let login = Login::new(); + let public_key = login.file.load().unwrap().unwrap().key.public_key(); + login.retire(false).unwrap(); + assert!(login.slot.open().is_none()); + assert_eq!(login.slot.status(), VaultStatus::Locked); + assert!(login.cached()); + let device = login.file.load().unwrap().unwrap(); + assert_eq!(device.key.public_key(), public_key); + assert!(device.session.is_none()); +} + +#[test] +fn a_late_captured_revocation_cannot_erase_a_new_login_of_the_same_account() { + let login = Login::new(); + let mut replacement = login.session.clone(); + replacement.access_token = "access-replacement".into(); + replacement.refresh_token = "refresh-replacement".into(); + let current = login.file.save_session(&replacement).unwrap(); + assert_ne!(current, login.login); + sign_in(&login.accounts); + let keys = login.slot.open().unwrap(); + assert!(matches!(login.retire(true), Err(HubError::Stale))); + assert_eq!(login.slot.status(), VaultStatus::Unlocked); + assert!(Arc::ptr_eq(&login.slot.open().unwrap(), &keys)); + assert!(login.cached()); + let device = login.file.load().unwrap().unwrap(); + assert_eq!(device.login, Some(current)); + assert_eq!(device.session, Some(replacement)); +} diff --git a/crates/hub-io/src/relay.rs b/crates/hub-io/src/relay.rs new file mode 100644 index 00000000..252e3ba7 --- /dev/null +++ b/crates/hub-io/src/relay.rs @@ -0,0 +1,224 @@ +//! The hub as the first step of an SSH route: [`RelayDialer`] over the hub's relay stream. +//! +//! A dial finds the hub by its address among the hubs this device is signed in to, asks it to +//! open a connection to the destination and hands the transport the two bounded frame channels +//! of a [`RelayStream`]. A task on the adapter's runtime then carries frames between those +//! channels and the hub's stream ([`pump`]); the two runtimes share nothing else. +//! +//! The hub learns the destination address and what it is told to forward, never what is inside +//! the SSH connection: host keys and sign-in are the transport's, checked against the +//! destination. + +mod failure; +mod pump; + +use std::sync::Arc; + +use futures::FutureExt as _; +use futures::future::BoxFuture; +use nocterm_hub_client::RelayTarget; +use nocterm_hubs_model::{HubAddress, HubId}; +use nocterm_session::{RelayDialer, RelayError, RelayStream, Target}; +use tokio::runtime::Handle; + +use crate::account::source::LocalSourceProvider; +use crate::links::HubLinks; +use crate::pool::{Pool, captured::CapturedClient}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +/// Opens relayed streams through the hubs this device is signed in to. +pub struct HubRelay { + handle: Handle, + pool: Arc, +} + +impl HubRelay { + /// A relay that runs on the threads of `links` and uses the hubs `links` signed in to. + /// + /// Once the links are dropped, dials fail with [`RelayError::Closed`]. + pub fn new(links: &HubLinks) -> Self { + let (handle, pool) = links.relay_parts(); + Self { handle, pool } + } +} + +/// Capture capability for an account-bound terminal, composed by the application. +pub(crate) struct RelayCapture { + handle: Handle, + pool: Arc, +} +impl RelayCapture { + pub(crate) fn new(handle: Handle, pool: Arc) -> Self { + Self { handle, pool } + } + pub(crate) fn account( + &self, + provider: &LocalSourceProvider, + source: Arc, + ) -> Result, HubError> { + let native = provider.capture_native()?; + let client = CapturedClient::account(self.pool.clone(), &native, source)?; + Ok(Arc::new(AccountRelay { + handle: self.handle.clone(), + client: Arc::new(client), + })) + } +} +struct AccountRelay { + handle: Handle, + client: Arc, +} +struct CapturedRelay { + handle: Handle, + client: Arc, + hub: String, + target: RelayTarget, + principal: String, +} +impl HubRelay { + fn captured(&self, name: &str, target: &Target) -> Result, RelayError> { + if self.pool.is_stopped() { + return Err(RelayError::Closed); + } + let missing = || RelayError::NotSignedIn { + hub: name.to_owned(), + }; + let hub = HubId::from(HubAddress::parse(name).map_err(|_| missing())?); + let client = CapturedClient::current(self.pool.clone(), hub) + .map_err(|error| failure::without_client(name, error))?; + Ok(Arc::new(CapturedRelay::new( + self.handle.clone(), + Arc::new(client), + name, + target, + ))) + } +} +impl RelayDialer for HubRelay { + fn capture(&self, hub: &str, target: &Target) -> Result, RelayError> { + self.captured(hub, target) + } + fn dial( + &self, + hub: &str, + target: &Target, + ) -> BoxFuture<'static, Result> { + match self.capture(hub, target) { + Ok(captured) => captured.dial(hub, target), + Err(error) => async move { Err(error) }.boxed(), + } + } +} +impl RelayDialer for AccountRelay { + fn capture(&self, hub: &str, target: &Target) -> Result, RelayError> { + let valid = + HubAddress::parse(hub).ok().map(HubId::from).as_ref() == Some(self.client.hub()); + if !valid { + return Err(RelayError::NotSignedIn { + hub: hub.to_owned(), + }); + } + self.client + .check() + .map_err(|error| failure::without_client(hub, error))?; + Ok(Arc::new(CapturedRelay::new( + self.handle.clone(), + self.client.clone(), + hub, + target, + ))) + } + fn dial( + &self, + hub: &str, + target: &Target, + ) -> BoxFuture<'static, Result> { + match self.capture(hub, target) { + Ok(captured) => captured.dial(hub, target), + Err(error) => async move { Err(error) }.boxed(), + } + } +} +impl CapturedRelay { + fn new(handle: Handle, client: Arc, hub: &str, target: &Target) -> Self { + Self { + handle, + client, + hub: hub.to_owned(), + principal: target.user.clone(), + target: RelayTarget { + host: target.host.clone(), + port: target.port, + }, + } + } + fn matches(&self, hub: &str, target: &Target) -> bool { + self.hub == hub + && self.target.host == target.host + && self.target.port == target.port + && self.principal == target.user + } + async fn open(self) -> Result { + let client = self + .client + .fresh() + .await + .map_err(|error| failure::without_client(&self.hub, error))?; + // The SDK and membership are already captured; this never selects B. + self.client + .check() + .map_err(|error| failure::without_client(&self.hub, error))?; + let conn = self + .client + .during(client.relay(&self.target)) + .await + .map_err(|error| failure::without_client(&self.hub, error))? + .map_err(|error| failure::opening(&self.client, &self.hub, error))?; + self.client + .check() + .map_err(|error| failure::without_client(&self.hub, error))?; + let (stream, peer) = RelayStream::pair(); + let (writer, reader) = conn.split(); + let client = self.client.clone(); + self.handle.spawn(async move { + let _ = client.during(pump::carry(peer, writer, reader)).await; + }); + Ok(stream) + } +} +impl RelayDialer for CapturedRelay { + fn capture(&self, hub: &str, target: &Target) -> Result, RelayError> { + if !self.matches(hub, target) { + return Err(RelayError::CaptureUnsupported); + } + self.client + .check() + .map_err(|error| failure::without_client(hub, error))?; + Ok(Arc::new(Self::new( + self.handle.clone(), + self.client.clone(), + hub, + target, + ))) + } + fn dial( + &self, + hub: &str, + target: &Target, + ) -> BoxFuture<'static, Result> { + let captured = match self.capture(hub, target) { + Ok(value) => value, + Err(error) => return async move { Err(error) }.boxed(), + }; + // Recreate only an immutable exact-route view; no account lookup occurs. + drop(captured); + let opening = Self::new(self.handle.clone(), self.client.clone(), hub, target); + // Dropping the dial aborts only opening; an issued stream owns its pump. + crate::task::spawn(&self.handle, async move { opening.open().await }) + .map(|result| result.unwrap_or(Err(RelayError::Closed))) + .boxed() + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/relay/failure.rs b/crates/hub-io/src/relay/failure.rs new file mode 100644 index 00000000..7ad1eb6b --- /dev/null +++ b/crates/hub-io/src/relay/failure.rs @@ -0,0 +1,74 @@ +//! A failed relay in the terms of `nocterm-session`'s `RelayError`. +//! +//! The contract names a refusal by a code and keeps its text for display, so the code decides +//! and the text is never read. The original failure stays the `source()` of what is returned. + +use nocterm_core::SharedCause; +use nocterm_hub_client::{ClientError, RelayError as HubRelayError}; +use nocterm_hub_proto::v2::ErrorCode; +use nocterm_hubs_model::HubError; +use nocterm_session::{RelayError, RelayRefusal}; + +use crate::pool::captured::CapturedClient; + +/// Why the hub named by `name` has no usable client. +pub(super) fn without_client(name: &str, error: HubError) -> RelayError { + match error { + HubError::NotSignedIn | HubError::Revoked => RelayError::NotSignedIn { + hub: name.to_owned(), + }, + other => cannot_reach(other), + } +} + +/// Why the hub did not open a relay to a destination. +/// +/// A device the hub revoked is forgotten on the spot, as for any other call. +pub(super) fn opening(client: &CapturedClient, name: &str, error: HubRelayError) -> RelayError { + classify_opening(name, error, |error| client.failure(error)) +} +fn classify_opening( + name: &str, + error: HubRelayError, + failed: impl FnOnce(ClientError) -> HubError, +) -> RelayError { + if let Some(code) = code_of(&error) + && let Some(refused) = coded(code) + { + return refused; + } + match error { + HubRelayError::Hub(error) => without_client(name, failed(error)), + other => cannot_reach(other), + } +} + +/// The hub's own reason for a failure, when it gave one. +fn code_of(error: &HubRelayError) -> Option { + match error { + HubRelayError::Closed { code, .. } + | HubRelayError::Hub(ClientError::Status { hub_code: code, .. }) => *code, + _ => None, + } +} + +/// What a hub's code says about a relay, if it says anything. +fn coded(code: ErrorCode) -> Option { + match code { + // The contract does not say which rule refused, only that one did. + ErrorCode::RelayDenied => Some(RelayError::Refused { + reason: RelayRefusal::Policy, + }), + ErrorCode::RelayLimit => Some(RelayError::Refused { + reason: RelayRefusal::Limit, + }), + _ => None, + } +} + +fn cannot_reach(cause: impl std::error::Error + Send + Sync + 'static) -> RelayError { + RelayError::Unreachable(SharedCause::new(cause)) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/relay/failure/tests.rs b/crates/hub-io/src/relay/failure/tests.rs new file mode 100644 index 00000000..1775f4b4 --- /dev/null +++ b/crates/hub-io/src/relay/failure/tests.rs @@ -0,0 +1,118 @@ +use crate::classify::classify; +use nocterm_hub_client::{ClientError, RelayError as HubRelayError}; +use nocterm_hubs_model::{HubAddress, HubError}; +use tonic::{Code, Status}; + +use super::*; + +fn open(error: HubRelayError) -> RelayError { + classify_opening("hub.example", error, |error| { + classify(&HubAddress::parse("hub.example").unwrap(), error) + }) +} + +fn closed(code: ErrorCode) -> HubRelayError { + HubRelayError::Closed { + code: Some(code), + message: "text that is never read".into(), + } +} + +#[test] +fn the_hubs_refusals_are_the_sessions_refusals() { + assert_eq!( + open(closed(ErrorCode::RelayDenied)), + RelayError::Refused { + reason: RelayRefusal::Policy + } + ); + assert_eq!( + open(closed(ErrorCode::RelayLimit)), + RelayError::Refused { + reason: RelayRefusal::Limit + } + ); +} + +#[test] +fn a_refusal_in_a_status_is_a_refusal_too() { + let mut status = Status::new(Code::PermissionDenied, "no"); + status.metadata_mut().insert( + "x-nocterm-error", + "ERROR_CODE_RELAY_DENIED".parse().unwrap(), + ); + assert_eq!( + open(HubRelayError::from(status)), + RelayError::Refused { + reason: RelayRefusal::Policy + } + ); +} + +#[test] +fn a_hub_without_relays_is_unreachable_and_keeps_its_cause() { + let RelayError::Unreachable(cause) = open(HubRelayError::Unsupported) else { + panic!("expected Unreachable"); + }; + assert!(matches!( + cause.downcast_ref::(), + Some(HubRelayError::Unsupported) + )); +} + +#[test] +fn a_broken_stream_is_unreachable() { + for error in [ + HubRelayError::Interrupted, + HubRelayError::UnexpectedFrame, + HubRelayError::BadPeerAddress, + closed(ErrorCode::Unspecified), + ] { + assert!(matches!(open(error), RelayError::Unreachable(_))); + } +} + +#[test] +fn tokens_the_hub_no_longer_accepts_mean_not_signed_in() { + let rejected = HubRelayError::from(Status::new(Code::Unauthenticated, "expired")); + assert_eq!( + open(rejected), + RelayError::NotSignedIn { + hub: "hub.example".into() + } + ); + assert_eq!( + open(HubRelayError::Hub(ClientError::NotSignedIn)), + RelayError::NotSignedIn { + hub: "hub.example".into() + } + ); +} + +#[test] +fn a_hub_that_does_not_answer_is_unreachable_and_keeps_its_cause() { + let down = HubRelayError::from(Status::new(Code::Unavailable, "down")); + let RelayError::Unreachable(cause) = open(down) else { + panic!("expected Unreachable"); + }; + assert!(matches!( + cause.downcast_ref::(), + Some(HubError::Unreachable { .. }) + )); +} + +#[test] +fn a_hub_without_a_client_is_not_signed_in_unless_it_failed_otherwise() { + assert_eq!( + without_client("h", HubError::NotSignedIn), + RelayError::NotSignedIn { hub: "h".into() } + ); + assert_eq!( + without_client("h", HubError::Revoked), + RelayError::NotSignedIn { hub: "h".into() } + ); + assert!(matches!( + without_client("h", HubError::NotFound), + RelayError::Unreachable(_) + )); +} diff --git a/crates/hub-io/src/relay/pump.rs b/crates/hub-io/src/relay/pump.rs new file mode 100644 index 00000000..bc7ca553 --- /dev/null +++ b/crates/hub-io/src/relay/pump.rs @@ -0,0 +1,132 @@ +//! Carries frames between the transport's bounded channels and the hub's relay stream. +//! +//! Every link of the chain is bounded, so a side that stops reading stops the other instead +//! of making this side buffer: +//! +//! - towards the hub, the transport's channel queues [`nocterm_session::RELAY_FRAMES`] frames, +//! the pump holds one, the hub client queues its own bounded number and then the HTTP/2 +//! window stops the rest; +//! - towards the transport, the HTTP/2 window stops the hub while the pump holds one frame +//! and the transport's channel queues [`nocterm_session::RELAY_FRAMES`]. +//! +//! The directions end independently. The transport dropping its sender is an end of file for +//! the target, which may still answer; the target closing is an end of file for the transport, +//! which may still send. A failure of the hub's stream or a dropped channel ends both: the +//! pump returns and releases every end it holds, which aborts the hub's stream and tells the +//! transport through its channels. + +use std::future::Future; +use std::pin::pin; + +use nocterm_hub_client::{RelayError, RelayReader, RelayWriter}; +use nocterm_session::{RELAY_FRAME_BYTES, RelayPeer, RelayReceiver, RelaySender}; + +/// The way towards the target: what the transport sends. +pub(super) trait Uplink: Send { + /// Waits while the hub still holds as many frames as it may, then queues `data`. + fn send(&mut self, data: &[u8]) -> impl Future> + Send; + /// Tells the target that nothing more follows; it may still answer. + fn finish(self) -> impl Future + Send; +} + +/// The way back: what the target sends. +pub(super) trait Downlink: Send { + /// The next bytes of the target; `Ok(None)` when it closed cleanly. + fn next(&mut self) -> impl Future>, RelayError>> + Send; +} + +impl Uplink for RelayWriter { + async fn send(&mut self, data: &[u8]) -> Result<(), RelayError> { + RelayWriter::send(self, data).await + } + + async fn finish(self) { + // The stream is going away either way; there is nobody to tell. + let _ = RelayWriter::finish(self).await; + } +} + +impl Downlink for RelayReader { + async fn next(&mut self) -> Result>, RelayError> { + self.recv().await + } +} + +/// How a direction ended. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Ended { + /// The direction reached its end; the other may go on. + Finished, + /// The stream or the other end of this direction is gone, so the whole relay is. + Broken, +} + +/// Moves frames both ways until both directions end or one breaks. +pub(super) async fn carry(peer: RelayPeer, up: U, down: D) { + let (from_transport, to_transport) = peer.into_parts(); + let mut upload = pin!(upload(from_transport, up)); + let mut download = pin!(download(down, to_transport)); + let (mut uploading, mut downloading) = (true, true); + while uploading || downloading { + let ended = tokio::select! { + ended = &mut upload, if uploading => { + uploading = false; + ended + } + ended = &mut download, if downloading => { + downloading = false; + ended + } + }; + if ended == Ended::Broken { + return; + } + } +} + +/// Moves what the transport sends to the hub, one frame at a time. +async fn upload(from_transport: RelayReceiver, mut up: U) -> Ended { + while let Ok(frame) = from_transport.recv().await { + // An empty frame carries nothing, and an empty data frame is not one the hub accepts. + if frame.is_empty() { + continue; + } + if let Err(error) = up.send(&frame).await { + tracing::debug!(%error, "the relay stream refused data"); + return Ended::Broken; + } + } + // The transport dropped its sender: end of file for the target, after what was queued. + up.finish().await; + Ended::Finished +} + +/// Moves what the target sends to the transport, in frames the transport accepts. +/// +/// Ends as soon as the transport has dropped its receiver, even while the target is silent. +async fn download(mut down: D, to_transport: RelaySender) -> Ended { + loop { + let next = tokio::select! { + next = down.next() => next, + () = to_transport.closed() => return Ended::Broken, + }; + match next { + Ok(Some(data)) => { + for frame in data.chunks(RELAY_FRAME_BYTES) { + if to_transport.send(frame.to_vec()).await.is_err() { + return Ended::Broken; + } + } + } + // Dropping the sender is the end of file for the transport. + Ok(None) => return Ended::Finished, + Err(error) => { + tracing::debug!(%error, "the relay stream ended"); + return Ended::Broken; + } + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/relay/pump/tests.rs b/crates/hub-io/src/relay/pump/tests.rs new file mode 100644 index 00000000..b6779400 --- /dev/null +++ b/crates/hub-io/src/relay/pump/tests.rs @@ -0,0 +1,238 @@ +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::Duration; + +use nocterm_hub_client::RelayError; +use nocterm_session::{RELAY_FRAME_BYTES, RELAY_FRAMES, RelayReceiver, RelaySender, RelayStream}; +use tokio::sync::{Notify, Semaphore, mpsc, oneshot}; +use tokio::task::JoinHandle; +use tokio::time::timeout; + +use super::{Downlink, Uplink, carry}; + +const PATIENCE: Duration = Duration::from_secs(5); + +/// What the hub's stream was told, in order. +#[derive(Debug, PartialEq, Eq)] +enum Told { + Data(Vec), + Finished, +} + +/// A hub stream going towards the target. `gate` holds sends back like a slow target does; +/// `gone` resolves when the stream is dropped, which aborts a real one. +struct FakeUp { + told: mpsc::UnboundedSender, + gate: Option>, + entered: Arc, + _gone: oneshot::Sender<()>, +} + +impl Uplink for FakeUp { + async fn send(&mut self, data: &[u8]) -> Result<(), RelayError> { + self.entered.notify_one(); + if let Some(gate) = &self.gate { + gate.acquire().await.unwrap().forget(); + } + self.told + .send(Told::Data(data.to_vec())) + .map_err(|_| RelayError::Interrupted) + } + + async fn finish(self) { + let _ = self.told.send(Told::Finished); + } +} + +/// A hub stream coming back from the target, fed by the test. +struct FakeDown { + script: mpsc::UnboundedReceiver>, RelayError>>, + taken: Arc, +} + +impl Downlink for FakeDown { + async fn next(&mut self) -> Result>, RelayError> { + let next = self + .script + .recv() + .await + .unwrap_or(Err(RelayError::Interrupted)); + self.taken.fetch_add(1, Ordering::SeqCst); + next + } +} + +struct Relay { + /// The transport's ends. + to_hub: RelaySender, + from_hub: RelayReceiver, + told: mpsc::UnboundedReceiver, + entered: Arc, + gone: oneshot::Receiver<()>, + answers: mpsc::UnboundedSender>, RelayError>>, + taken: Arc, + pump: JoinHandle<()>, +} + +fn relay(gate: Option>) -> Relay { + let (stream, peer) = RelayStream::pair(); + let (told, told_rx) = mpsc::unbounded_channel(); + let (answers, script) = mpsc::unbounded_channel(); + let (gone_tx, gone) = oneshot::channel(); + let entered = Arc::new(Notify::new()); + let taken = Arc::new(AtomicUsize::new(0)); + let up = FakeUp { + told, + gate, + entered: entered.clone(), + _gone: gone_tx, + }; + let down = FakeDown { + script, + taken: taken.clone(), + }; + let (to_hub, from_hub) = stream.into_parts(); + Relay { + to_hub, + from_hub, + told: told_rx, + entered, + gone, + answers, + taken, + pump: tokio::spawn(carry(peer, up, down)), + } +} + +async fn ended(pump: JoinHandle<()>) { + timeout(PATIENCE, pump) + .await + .expect("the relay did not end") + .unwrap(); +} + +#[tokio::test] +async fn frames_reach_the_hub_in_order_and_an_end_of_file_is_a_half_close() { + let mut relay = relay(None); + relay.to_hub.send(b"one".to_vec()).await.unwrap(); + relay.to_hub.send(b"two".to_vec()).await.unwrap(); + drop(relay.to_hub); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"one".to_vec()))); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"two".to_vec()))); + assert_eq!(relay.told.recv().await, Some(Told::Finished)); + // The target may still answer after the half-close. + relay.answers.send(Ok(Some(b"late".to_vec()))).unwrap(); + assert_eq!(relay.from_hub.recv().await.unwrap(), b"late"); +} + +#[tokio::test] +async fn an_empty_frame_from_the_transport_is_not_sent_to_the_hub() { + let mut relay = relay(None); + relay.to_hub.send(Vec::new()).await.unwrap(); + relay.to_hub.send(b"one".to_vec()).await.unwrap(); + relay.to_hub.send(Vec::new()).await.unwrap(); + drop(relay.to_hub); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"one".to_vec()))); + assert_eq!(relay.told.recv().await, Some(Told::Finished)); +} + +#[tokio::test] +async fn data_from_the_target_comes_in_frames_the_transport_accepts() { + let relay = relay(None); + let data: Vec = (0..(3 * RELAY_FRAME_BYTES + 1)) + .map(|index| (index % 251) as u8) + .collect(); + relay.answers.send(Ok(Some(data.clone()))).unwrap(); + let mut received = Vec::new(); + while received.len() < data.len() { + let frame = timeout(PATIENCE, relay.from_hub.recv()) + .await + .unwrap() + .unwrap(); + assert!(frame.len() <= RELAY_FRAME_BYTES); + received.extend(frame); + } + assert_eq!(received, data); +} + +#[tokio::test] +async fn a_clean_close_by_the_target_is_end_of_file_after_the_queued_data() { + let mut relay = relay(None); + relay.answers.send(Ok(Some(b"bye".to_vec()))).unwrap(); + relay.answers.send(Ok(None)).unwrap(); + assert_eq!(relay.from_hub.recv().await.unwrap(), b"bye"); + assert!(relay.from_hub.recv().await.is_err(), "end of file"); + // The transport may still send: its direction has not ended. + relay.to_hub.send(b"still".to_vec()).await.unwrap(); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"still".to_vec()))); +} + +#[tokio::test] +async fn a_failed_hub_stream_closes_both_directions() { + let relay = relay(None); + relay.answers.send(Err(RelayError::Interrupted)).unwrap(); + ended(relay.pump).await; + assert!(relay.from_hub.recv().await.is_err()); + assert!(relay.to_hub.send(b"x".to_vec()).await.is_err()); +} + +#[tokio::test] +async fn dropping_both_transport_ends_releases_the_hub_stream_while_the_target_is_silent() { + let relay = relay(None); + drop((relay.to_hub, relay.from_hub)); + ended(relay.pump).await; + // The sender of `gone` was dropped with the stream: this resolves with an error. + assert!(timeout(PATIENCE, relay.gone).await.unwrap().is_err()); +} + +#[tokio::test] +async fn a_transport_that_stops_reading_releases_the_hub_stream_while_the_target_is_silent() { + let relay = relay(None); + drop(relay.from_hub); + ended(relay.pump).await; + assert!(relay.to_hub.send(b"x".to_vec()).await.is_err()); +} + +#[tokio::test] +async fn a_target_that_stops_reading_stalls_the_transport_at_the_bounded_queue() { + let gate = Arc::new(Semaphore::new(0)); + let mut relay = relay(Some(gate.clone())); + relay.to_hub.send(vec![0]).await.unwrap(); + timeout(PATIENCE, relay.entered.notified()).await.unwrap(); + // The pump holds that frame; the channel takes `RELAY_FRAMES` more and no further. + let mut queued = 0; + while relay.to_hub.try_send(vec![1]).is_ok() { + queued += 1; + } + assert_eq!(queued, RELAY_FRAMES); + // Letting the target read again moves everything, in order. + gate.add_permits(RELAY_FRAMES + 1); + assert_eq!(relay.told.recv().await, Some(Told::Data(vec![0]))); + for _ in 0..RELAY_FRAMES { + assert_eq!(relay.told.recv().await, Some(Told::Data(vec![1]))); + } +} + +#[tokio::test] +async fn a_transport_that_stops_reading_stalls_the_hub_at_the_bounded_queue() { + let relay = relay(None); + for _ in 0..(10 * RELAY_FRAMES) { + relay.answers.send(Ok(Some(vec![7]))).unwrap(); + } + // Wait until the pump has taken what it can. + let mut last = 0; + loop { + tokio::time::sleep(Duration::from_millis(50)).await; + let now = relay.taken.load(Ordering::SeqCst); + if now == last { + break; + } + last = now; + } + // `RELAY_FRAMES` in the channel, one held by the pump, and the one it asked for next. + assert!( + last <= RELAY_FRAMES + 2, + "the pump took {last} frames from a hub the transport does not read" + ); + assert!(last >= RELAY_FRAMES, "the channel was not filled: {last}"); +} diff --git a/crates/hub-io/src/relay/tests.rs b/crates/hub-io/src/relay/tests.rs new file mode 100644 index 00000000..3f863bcf --- /dev/null +++ b/crates/hub-io/src/relay/tests.rs @@ -0,0 +1,51 @@ +use futures::executor::block_on; +use nocterm_session::{RelayDialer as _, RelayError, Target}; + +use super::HubRelay; +use crate::{HubLinks, HubLinksConfig}; + +fn target() -> Target { + Target { + host: "db.internal".into(), + port: 22, + user: "deploy".into(), + } +} + +fn links(dir: &tempfile::TempDir) -> std::sync::Arc { + HubLinks::start(HubLinksConfig { + credentials_dir: dir.path().join("hubs"), + device_name: "relay test".into(), + }) + .unwrap() +} + +#[test] +fn a_hub_this_device_is_not_signed_in_to_is_not_dialled() { + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let relay = HubRelay::new(&links); + for hub in [ + "hub.example", + "https://Hub.Example:7443/", + "not a hub address", + "", + ] { + let dialled = block_on(relay.dial(hub, &target())); + assert_eq!( + dialled.unwrap_err(), + RelayError::NotSignedIn { hub: hub.into() }, + "{hub:?}" + ); + } +} + +#[test] +fn dialling_after_the_links_stopped_finds_the_relay_closed() { + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let relay = HubRelay::new(&links); + drop(links); + let dialled = block_on(relay.dial("hub.example", &target())); + assert_eq!(dialled.unwrap_err(), RelayError::Closed); +} diff --git a/crates/hub-io/src/replica.rs b/crates/hub-io/src/replica.rs new file mode 100644 index 00000000..1b996681 --- /dev/null +++ b/crates/hub-io/src/replica.rs @@ -0,0 +1,219 @@ +//! The saved profiles on the account hub, one sealed object per profile. +//! +//! [`HubProfileReplica`] implements the replica port of the connection store over the +//! account's `profiles` collection. It works only while the vault is unlocked, because the keys +//! that seal profiles are the vault's; locked or unreachable is "unavailable", never an error. + +mod watch; +mod wire; + +use futures::StreamExt as _; +use futures::future::BoxFuture; +use futures::stream::BoxStream; +use nocterm_connections_model::profiles::{Profile, ProfileId}; +use nocterm_connections_model::replica::{ + ProfileReplica, ReplicaCursor, ReplicaError, ReplicaPage, ReplicaVersion, +}; +use nocterm_core::SharedCause; +use nocterm_hubs_model::HubError; +use tokio::runtime::Handle; +use tokio::sync::mpsc; + +use self::wire::{KIND, Positions}; +use crate::account::operations::{BoundOperations, OpenedWork}; +use crate::classify::unreachable; +use crate::store::{Located, OpError, PROFILES_KIND, Store}; +use crate::task::spawn; + +pub struct HubProfileReplica { + handle: Handle, + bound: BoundOperations, +} + +/// A failure of a store call as the replica reports it. `id` names the profile a write was +/// about. +fn replica_error(error: OpError, id: Option) -> ReplicaError { + match (error, id) { + (OpError::Hub(HubError::NotSignedIn | HubError::Stale), _) => ReplicaError::Stale, + (OpError::Conflict, Some(id)) => ReplicaError::Conflict { id }, + (OpError::Hub(HubError::NotFound), Some(id)) => ReplicaError::Conflict { id }, + (OpError::Hub(HubError::PermissionDenied), Some(id)) => ReplicaError::Forbidden { id }, + (OpError::Hub(error), _) if unreachable(&error) => ReplicaError::Unavailable, + (error, _) => ReplicaError::Other(SharedCause::new(error)), + } +} + +impl HubProfileReplica { + pub(crate) fn new(handle: Handle, bound: BoundOperations) -> Self { + Self { handle, bound } + } + + fn run( + &self, + work: impl FnOnce(OpenedWork) -> F + Send + 'static, + ) -> BoxFuture<'static, Result> + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let admitted = match self.bound.capture() { + Ok(admitted) => admitted, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let source = admitted.source().clone(); + let returned = source.clone(); + let task = spawn(&self.handle, async move { + source + .during(work(admitted)) + .await + .map_err(native_failure)? + }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(ReplicaError::Unavailable)); + returned.check().map_err(native_failure)?; + result + }) + } +} + +fn native_failure(error: HubError) -> ReplicaError { + match error { + HubError::KeysLocked => ReplicaError::Locked, + HubError::NotSignedIn | HubError::Stale => ReplicaError::Stale, + other => ReplicaError::Other(SharedCause::new(other)), + } +} + +async fn changes( + work: OpenedWork, + after: Option, +) -> Result { + let store = work.store(); + let mut positions = Positions::parse(after.as_ref()); + let Some(at) = store + .find(PROFILES_KIND) + .await + .map_err(|error| replica_error(error, None))? + else { + return Ok(ReplicaPage { + changes: Vec::new(), + cursor: positions.cursor(), + more: false, + }); + }; + let from = positions.get(at.id()); + let batch = store + .changes(&at, from) + .await + .map_err(|error| replica_error(error, None))?; + let mut seen = Vec::new(); + let mut position = from; + for object in &batch.changes { + position = position.max(object.stream_version); + if let Some(profile) = wire::read(at.id(), at.key(), object)? { + seen.push(profile); + } + } + if !batch.more { + position = position.max(batch.head); + } + positions.set(at.id(), position); + Ok(ReplicaPage { + changes: seen, + cursor: positions.cursor(), + more: batch.more, + }) +} + +/// The collection a write goes to: the account's own, created by the first write. +async fn target(store: &Store, id: ProfileId, create: bool) -> Result { + let located = if create { + store.ensure(PROFILES_KIND).await.map(Some) + } else { + store.find(PROFILES_KIND).await + } + .map_err(|error| replica_error(error, None))?; + located.ok_or(ReplicaError::Conflict { id }) +} + +async fn put( + work: OpenedWork, + profile: Profile, + expected: Option, +) -> Result { + let id = profile.id; + let store = work.store(); + let plaintext = + wire::encode(&profile).map_err(|error| ReplicaError::Other(SharedCause::new(error)))?; + let at = target(&store, id, true).await?; + let stored = store + .put( + &at, + KIND, + &id.to_string(), + &plaintext, + expected.map_or(0, ReplicaVersion::get), + ) + .await + .map_err(|error| replica_error(error, Some(id)))?; + Ok(ReplicaVersion::new(stored.version)) +} + +async fn delete( + work: OpenedWork, + id: ProfileId, + expected: ReplicaVersion, +) -> Result { + let store = work.store(); + let at = target(&store, id, false).await?; + let stored = store + .remove(&at, &id.to_string(), expected.get()) + .await + .map_err(|error| replica_error(error, Some(id)))?; + Ok(ReplicaVersion::new(stored.version)) +} + +impl ProfileReplica for HubProfileReplica { + fn changes( + &self, + after: Option, + ) -> BoxFuture<'static, Result> { + self.run(move |work| changes(work, after)) + } + + fn put( + &self, + profile: Profile, + expected: Option, + ) -> BoxFuture<'static, Result> { + self.run(move |work| put(work, profile, expected)) + } + + fn delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + ) -> BoxFuture<'static, Result> { + self.run(move |work| delete(work, id, expected)) + } + + fn watch(&self) -> BoxStream<'static, ()> { + let work = match self.bound.capture() { + Ok(work) => work, + Err(_) => return futures::stream::empty().boxed(), + }; + let (ticks, receiver) = mpsc::channel(1); + self.handle.spawn(watch::follow(work, ticks)); + let source = self.bound.source().clone(); + futures::stream::unfold((receiver, source), |(mut receiver, source)| async move { + source.check().ok()?; + receiver.recv().await?; + source.check().ok()?; + Some(((), (receiver, source))) + }) + .boxed() + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/replica/tests.rs b/crates/hub-io/src/replica/tests.rs new file mode 100644 index 00000000..ce79ed15 --- /dev/null +++ b/crates/hub-io/src/replica/tests.rs @@ -0,0 +1,152 @@ +use std::sync::Arc; +use std::time::Duration; + +use nocterm_connections_model::replica::RemoteBody; +use nocterm_connections_model::test_support::profile_id_for; + +use super::*; +use crate::account::{ + Slot, + operations::AccountOperations, + testing::{native_operations, second_device, signed_in}, +}; +use crate::store::StoreOps as _; +use crate::store::memory::MemoryHub; +mod source_binding; + +fn profile(name: &str) -> Profile { + serde_json::from_value(serde_json::json!({ + "id": profile_id_for(name), + "name": name, + "host": "example.org", + "user": "ann", + })) + .unwrap() +} + +struct Device { + replica: HubProfileReplica, + slot: Arc, + _dir: tempfile::TempDir, + _operations: Arc, +} + +/// A signed-in device of the account. +fn device(hub: &Arc) -> (Device, nocterm_hub_proto::v2::Keyset) { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + let operations = native_operations(&slot, hub.clone()); + let source = operations.current_opened_source().unwrap(); + let replica = HubProfileReplica::new(Handle::current(), operations.bind(source).unwrap()); + ( + Device { + replica, + slot, + _dir: dir, + _operations: operations, + }, + record, + ) +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_profile_put_on_one_device_arrives_on_another() { + let hub = Arc::new(MemoryHub::default()); + let (first, record) = device(&hub); + let web = profile("web"); + let version = first.replica.put(web.clone(), None).await.unwrap(); + assert_eq!(version, ReplicaVersion::new(1)); + + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot) = second_device(dir.path(), &record); + let operations = native_operations(&slot, hub.clone()); + let source = operations.current_opened_source().unwrap(); + let second = HubProfileReplica::new(Handle::current(), operations.bind(source).unwrap()); + let page = second.changes(None).await.unwrap(); + assert!(!page.more); + assert_eq!(page.changes.len(), 1); + assert_eq!(page.changes[0].body, RemoteBody::Present(Box::new(web))); + // Reading again from the returned cursor finds nothing new. + let next = second.changes(Some(page.cursor)).await.unwrap(); + assert!(next.changes.is_empty()); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_stale_write_conflicts_and_a_delete_is_a_tombstone() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let web = profile("web"); + let v1 = device.replica.put(web.clone(), None).await.unwrap(); + let again = device.replica.put(web.clone(), None).await; + assert!(matches!(again, Err(ReplicaError::Conflict { id }) if id == web.id)); + let v2 = device.replica.put(web.clone(), Some(v1)).await.unwrap(); + let v3 = device.replica.delete(web.id, v2).await.unwrap(); + assert!(v3 > v2); + let page = device.replica.changes(None).await.unwrap(); + assert_eq!(page.changes.len(), 1); + assert_eq!(page.changes[0].body, RemoteBody::Deleted); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_unreachable_hub_is_unavailable_not_failed() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + hub.set_offline(true); + assert!(matches!( + device.replica.changes(None).await, + Err(ReplicaError::Unavailable) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn closing_account_keys_makes_the_original_replica_stale() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + device.slot.lock(); + assert!(matches!( + device.replica.changes(None).await, + Err(ReplicaError::Stale) + )); + assert!(matches!( + device.replica.put(profile("web"), None).await, + Err(ReplicaError::Stale) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_profile_the_hub_altered_stops_the_feed_with_an_error() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let web = profile("web"); + device.replica.put(web.clone(), None).await.unwrap(); + let collection = hub.collections(PROFILES_KIND).await.unwrap().remove(0); + hub.tamper(&collection.collection_id, &web.id.to_string(), vec![5; 64]); + assert!(matches!( + device.replica.changes(None).await, + Err(ReplicaError::Other(_)) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_old_watch_ends_after_lock_and_a_fresh_capture_watches_after_unlock() { + let hub = Arc::new(MemoryHub::default()); + let (device, record) = device(&hub); + device.slot.lock(); + let mut old = device.replica.watch(); + assert_eq!(old.next().await, None); + let keys = crate::account::testing::open( + &record, + &crate::account::testing::unlock(crate::account::testing::PASSWORD), + ); + device.slot.unlocked(keys); + assert_eq!(old.next().await, None); + let source = device._operations.current_opened_source().unwrap(); + let fresh = HubProfileReplica::new(Handle::current(), device._operations.bind(source).unwrap()); + let mut ticks = fresh.watch(); + assert_eq!( + tokio::time::timeout(Duration::from_secs(5), ticks.next()) + .await + .unwrap(), + Some(()) + ); +} diff --git a/crates/hub-io/src/replica/tests/source_binding.rs b/crates/hub-io/src/replica/tests/source_binding.rs new file mode 100644 index 00000000..03850c91 --- /dev/null +++ b/crates/hub-io/src/replica/tests/source_binding.rs @@ -0,0 +1,118 @@ +use super::*; +use crate::account::testing::NativeAccounts; +use crate::store::memory::StoreBoundary; + +fn captured(fixture: &NativeAccounts) -> HubProfileReplica { + HubProfileReplica::new( + Handle::current(), + fixture.operations().bind(fixture.source()).unwrap(), + ) +} + +#[tokio::test] +async fn queued_replica_mutations_never_choose_the_replacement_account() { + let fixture = NativeAccounts::new(); + let replica = captured(&fixture); + let item = profile("A"); + let put = replica.put(item.clone(), None); + let delete = replica.delete(item.id, ReplicaVersion::new(1)); + fixture.login(1); + assert!(matches!(put.await, Err(ReplicaError::Stale))); + assert!(matches!(delete.await, Err(ReplicaError::Stale))); + assert!( + fixture + .hub(0) + .collections(PROFILES_KIND) + .await + .unwrap() + .is_empty() + ); + assert!( + fixture + .hub(1) + .collections(PROFILES_KIND) + .await + .unwrap() + .is_empty() + ); +} + +#[tokio::test] +async fn late_replica_changes_publish_no_page_for_b() { + let fixture = NativeAccounts::new(); + let a = captured(&fixture); + a.put(profile("A"), None).await.unwrap(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let old = a.changes(None); + entered.wait().await; + fixture.login(1); + let b = captured(&fixture); + assert!(b.changes(None).await.unwrap().changes.is_empty()); + release.release(); + assert!(matches!(old.await, Err(ReplicaError::Stale))); +} + +#[tokio::test] +async fn a_finished_replica_page_is_rechecked_when_its_caller_first_polls() { + let fixture = NativeAccounts::new(); + let replica = captured(&fixture); + replica.put(profile("A"), None).await.unwrap(); + let calls = fixture.hub(0).change_calls(); + let old = replica.changes(None); + while fixture.hub(0).change_calls() == calls { + tokio::task::yield_now().await; + } + fixture.login(1); + assert!(matches!(old.await, Err(ReplicaError::Stale))); +} + +#[tokio::test] +async fn an_old_replica_stays_stale_after_same_account_login_and_a_b_a() { + let fixture = NativeAccounts::new(); + let old = captured(&fixture); + fixture.login(0); + assert!(matches!(old.changes(None).await, Err(ReplicaError::Stale))); + let next = captured(&fixture); + fixture.login(1); + fixture.login(0); + assert!(matches!( + next.put(profile("old"), None).await, + Err(ReplicaError::Stale) + )); + assert!( + captured(&fixture) + .changes(None) + .await + .unwrap() + .changes + .is_empty() + ); +} + +#[tokio::test] +async fn a_queued_watch_tick_cannot_outlive_its_original_account_capture() { + let fixture = NativeAccounts::new(); + let replica = captured(&fixture); + replica.put(profile("A"), None).await.unwrap(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::WatchReady); + let mut old = replica.watch(); + entered.wait().await; + release.release(); + tokio::task::yield_now().await; + fixture.login(1); + assert_eq!(old.next().await, None); +} + +#[tokio::test] +async fn a_valid_source_from_another_hub_cannot_select_this_accounts_ports() { + let a = NativeAccounts::new(); + let b = NativeAccounts::at(crate::account::testing::hub_for("other.test")); + assert!(a.source().check_current().is_ok()); + assert!(b.source().check_current().is_ok()); + assert!(matches!( + a.operations().bind(b.source()), + Err(HubError::Stale) + )); + captured(&a).put(profile("A"), None).await.unwrap(); + assert!(captured(&b).changes(None).await.unwrap().changes.is_empty()); +} diff --git a/crates/hub-io/src/replica/watch.rs b/crates/hub-io/src/replica/watch.rs new file mode 100644 index 00000000..57f85c3c --- /dev/null +++ b/crates/hub-io/src/replica/watch.rs @@ -0,0 +1,55 @@ +//! Change hints retain one original native source and never reopen dynamic Slot. +use crate::{account::operations::OpenedWork, store::PROFILES_KIND}; +use futures::StreamExt as _; +use std::time::Duration; +use tokio::sync::mpsc; + +const RETRY: Duration = Duration::from_secs(20); + +pub(super) async fn follow(work: OpenedWork, ticks: mpsc::Sender<()>) { + let source = work.source().clone(); + let Ok(slot) = source.native().slot() else { + return; + }; + let mut notice = slot.notice().subscribe(); + loop { + if source.check().is_err() { + return; + } + notice.borrow_and_update(); + tokio::select! { + () = ticks.closed() => return, + changed = notice.changed() => { + if changed.is_err() || source.check().is_err() { return; } + // The keys changed: restart the original feed with them. + }, + result = source.during(once(&work, &ticks)) => { + if result.is_err() { return; } + } + } + } +} + +async fn once(work: &OpenedWork, ticks: &mpsc::Sender<()>) { + let store = work.store(); + let collections: Vec = match store.find(PROFILES_KIND).await { + Ok(found) => found.iter().map(|at| at.id().to_owned()).collect(), + Err(_) => Vec::new(), + }; + if collections.is_empty() { + let _ = work.source().publish(|| ticks.try_send(())); + tokio::time::sleep(RETRY).await; + return; + } + let Ok(mut hints) = work.ops().watch(collections).await else { + tokio::time::sleep(RETRY).await; + return; + }; + let _ = work.source().publish(|| ticks.try_send(())); + while hints.next().await.is_some() { + if work.source().publish(|| ticks.try_send(())).is_err() { + return; + } + } + tokio::time::sleep(RETRY).await; +} diff --git a/crates/hub-io/src/replica/wire.rs b/crates/hub-io/src/replica/wire.rs new file mode 100644 index 00000000..3daf3622 --- /dev/null +++ b/crates/hub-io/src/replica/wire.rs @@ -0,0 +1,112 @@ +//! What a profile object holds and how an object of the feed is read. +//! +//! The plaintext is `{"v":1,"profile":…}`. An object this client cannot read (a newer format +//! version, another kind, or a profile under another id) is reported as unrecognized so the +//! reconciliation leaves it alone. An object that does not authenticate is the hub's +//! forgery: it is an error and the feed does not move past it. + +use std::str::FromStr as _; + +use nocterm_connections_model::profiles::{Profile, ProfileId}; +use nocterm_connections_model::replica::{ + RemoteBody, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaVersion, +}; +use nocterm_core::SharedCause; +use nocterm_hub_proto::v2; +use nocterm_keys::CollectionKey; +use serde::{Deserialize, Serialize}; + +/// The kind of the objects that hold profiles. +pub(super) const KIND: &str = "profile"; +const FORMAT: u32 = 1; + +#[derive(Serialize)] +struct Write<'a> { + v: u32, + profile: &'a Profile, +} + +#[derive(Deserialize)] +struct Head { + v: u32, +} + +#[derive(Deserialize)] +struct Read { + profile: Profile, +} + +pub(super) fn encode(profile: &Profile) -> Result, serde_json::Error> { + serde_json::to_vec(&Write { v: FORMAT, profile }) +} + +/// One object of the feed as the replica reports it; `None` for an object that is not a +/// profile at all (its id is not a profile id), which is neither shown nor touched. +pub(super) fn read( + collection_id: &str, + key: &CollectionKey, + object: &v2::StoredObject, +) -> Result, ReplicaError> { + let Ok(id) = ProfileId::from_str(&object.object_id) else { + return Ok(None); + }; + let body = if object.deleted { + RemoteBody::Deleted + } else if object.kind != KIND { + RemoteBody::Unrecognized + } else { + let plain = key + .open_object( + collection_id, + &object.object_id, + &object.kind, + &object.ciphertext, + ) + .map_err(|error| ReplicaError::Other(SharedCause::new(error)))?; + body_of(id, &plain) + }; + Ok(Some(RemoteProfile { + id, + version: ReplicaVersion::new(object.version), + body, + })) +} + +fn body_of(id: ProfileId, plaintext: &[u8]) -> RemoteBody { + match serde_json::from_slice::(plaintext) { + Ok(Head { v: FORMAT }) => {} + _ => return RemoteBody::Unrecognized, + } + match serde_json::from_slice::(plaintext) { + Ok(Read { profile }) if profile.id == id => RemoteBody::Present(Box::new(profile)), + _ => RemoteBody::Unrecognized, + } +} + +/// The position in the feed of every collection read: JSON, so it stays opaque to the +/// reconciliation and can grow to several collections. +#[derive(Default, Serialize, Deserialize)] +pub(super) struct Positions(std::collections::BTreeMap); + +impl Positions { + pub(super) fn parse(cursor: Option<&ReplicaCursor>) -> Self { + cursor + .and_then(|cursor| serde_json::from_str(cursor.as_str()).ok()) + .unwrap_or_default() + } + + pub(super) fn get(&self, collection_id: &str) -> u64 { + self.0.get(collection_id).copied().unwrap_or(0) + } + + pub(super) fn set(&mut self, collection_id: &str, position: u64) { + self.0.insert(collection_id.to_owned(), position); + } + + pub(super) fn cursor(&self) -> ReplicaCursor { + ReplicaCursor::new(serde_json::to_string(self).unwrap_or_else(|_| "{}".to_owned())) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/replica/wire/tests.rs b/crates/hub-io/src/replica/wire/tests.rs new file mode 100644 index 00000000..5af66bca --- /dev/null +++ b/crates/hub-io/src/replica/wire/tests.rs @@ -0,0 +1,196 @@ +use nocterm_connections_model::test_support::profile_id_for; +use nocterm_keys::OpenError; + +use super::*; + +fn profile(name: &str) -> Profile { + serde_json::from_value(serde_json::json!({ + "id": profile_id_for(name), + "name": name, + "host": "example.org", + "user": "ann", + })) + .unwrap() +} + +fn key() -> CollectionKey { + CollectionKey::generate().unwrap() +} + +fn object( + key: &CollectionKey, + profile_id: ProfileId, + kind: &str, + plain: &[u8], +) -> v2::StoredObject { + let object_id = profile_id.to_string(); + v2::StoredObject { + collection_id: "c".into(), + object_id: object_id.clone(), + version: 3, + kind: kind.into(), + ciphertext: key.seal_object("c", &object_id, kind, plain).unwrap(), + deleted: false, + updated_at_ms: 0, + updated_by_device: String::new(), + stream_version: 7, + } +} + +#[test] +fn a_profile_written_by_this_client_reads_back_with_its_version() { + let key = key(); + let web = profile("web"); + let stored = object(&key, web.id, KIND, &encode(&web).unwrap()); + let read = read("c", &key, &stored).unwrap().unwrap(); + assert_eq!(read.id, web.id); + assert_eq!(read.version, ReplicaVersion::new(3)); + assert_eq!(read.body, RemoteBody::Present(Box::new(web))); +} + +#[test] +fn a_tombstone_is_a_deletion_without_opening_anything() { + let key = key(); + let id = profile_id_for("gone"); + let mut stored = object(&key, id, KIND, b"{}"); + stored.deleted = true; + stored.ciphertext.clear(); + let read = read("c", &key, &stored).unwrap().unwrap(); + assert_eq!(read.body, RemoteBody::Deleted); +} + +#[test] +fn an_object_this_client_cannot_decode_is_unrecognized() { + let key = key(); + let web = profile("web"); + let newer = object(&key, web.id, KIND, br#"{"v":2,"profile":{}}"#); + assert_eq!( + read("c", &key, &newer).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); + let other_kind = object(&key, web.id, "profile-v2", &encode(&web).unwrap()); + assert_eq!( + read("c", &key, &other_kind).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); + let garbage = object(&key, web.id, KIND, b"not json"); + assert_eq!( + read("c", &key, &garbage).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); +} + +#[test] +fn a_profile_stored_under_another_profiles_id_is_unrecognized() { + let key = key(); + let web = profile("web"); + let other = profile("other"); + let moved = object(&key, other.id, KIND, &encode(&web).unwrap()); + assert_eq!( + read("c", &key, &moved).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); +} + +#[test] +fn an_object_that_does_not_authenticate_is_an_error() { + let key = key(); + let web = profile("web"); + let mut forged = object(&key, web.id, KIND, &encode(&web).unwrap()); + forged.ciphertext[30] ^= 0xff; + let error = read("c", &key, &forged).unwrap_err(); + let ReplicaError::Other(cause) = error else { + panic!("a forgery is a failure of the replica"); + }; + assert!(cause.downcast_ref::().is_some()); + // The same bytes under another key do not open either. + let stranger = self::key(); + let sealed = object(&key, web.id, KIND, &encode(&web).unwrap()); + assert!(read("c", &stranger, &sealed).is_err()); +} + +#[test] +fn an_object_that_is_not_a_profile_id_is_skipped() { + let key = key(); + let stored = v2::StoredObject { + object_id: "not-a-profile".into(), + ..object(&key, profile_id_for("x"), KIND, b"{}") + }; + assert!(read("c", &key, &stored).unwrap().is_none()); +} + +#[test] +fn positions_round_trip_and_an_unreadable_cursor_starts_over() { + let mut positions = Positions::default(); + positions.set("a", 4); + let cursor = positions.cursor(); + assert_eq!(Positions::parse(Some(&cursor)).get("a"), 4); + assert_eq!( + Positions::parse(Some(&ReplicaCursor::new("???"))).get("a"), + 0 + ); + assert_eq!(Positions::parse(None).get("a"), 0); +} + +#[test] +fn a_profile_the_web_panel_keeps_reads_as_a_server_that_signs_in_with_its_password() { + // Exactly what the web panel writes for a server connected with a password (`newProfile`). + let id = profile_id_for("prod"); + let credential = "ab".repeat(16); + let plain = serde_json::json!({ + "v": 1, + "profile": { + "id": id, + "name": "prod", + "host": "prod.example.com", + "port": 2222, + "user": "ops", + "auth": {"method": "password"}, + "credential": credential, + } + }); + let RemoteBody::Present(profile) = body_of(id, plain.to_string().as_bytes()) else { + panic!("the web panel's profile reads here"); + }; + assert_eq!(profile.name, "prod"); + assert_eq!( + profile.target, + nocterm_session::Target::new("ops", "prod.example.com", 2222) + ); + assert_eq!(profile.auth, nocterm_session::Auth::Password); + assert_eq!( + profile.credential.map(|id| id.to_string()), + Some(credential) + ); +} + +#[test] +fn a_profile_the_web_panel_keeps_with_a_key_signs_in_with_that_vault_key() { + let id = profile_id_for("prod"); + let key = "cd".repeat(16); + let public_key = + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID7XjK7UMquFonNQf6mOb5ODK/CIOubLkjrBcI6wLh5+"; + let plain = serde_json::json!({ + "v": 1, + "profile": { + "id": id, + "name": "prod", + "host": "prod.example.com", + "user": "ops", + "auth": {"method": "vault_key", "credential": key, "public_key": public_key}, + } + }); + let RemoteBody::Present(profile) = body_of(id, plain.to_string().as_bytes()) else { + panic!("the web panel's profile reads here"); + }; + let nocterm_session::Auth::VaultKey { + credential, + public_key: named, + } = profile.auth + else { + panic!("signs in with a vault key: {:?}", profile.auth); + }; + assert_eq!(credential.to_string(), key); + assert_eq!(named, public_key); + assert_eq!(profile.credential, None); +} diff --git a/crates/hub-io/src/store.rs b/crates/hub-io/src/store.rs new file mode 100644 index 00000000..0303d002 --- /dev/null +++ b/crates/hub-io/src/store.rs @@ -0,0 +1,210 @@ +//! Encrypted collections of one account on a hub. +//! +//! [`StoreOps`] is the narrow seam to the hub's vault service: plain calls with sealed bytes. +//! [`Store`] adds what needs the account key: finding or creating the account's collection of +//! a kind, and sealing what goes into it. Only the kinds this client knows are asked for, so a +//! collection of another kind is never listed, opened, changed or deleted here. + +mod client; +mod grpc; +#[cfg(test)] +pub(crate) mod memory; + +use std::sync::Arc; + +use futures::future::BoxFuture; +use futures::stream::BoxStream; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubError; +use nocterm_keys::{CollectionKey, OpenError, random_bytes}; +use uuid::Builder; + +use crate::account::OpenKeys; +use crate::classify::remote; + +pub(crate) use grpc::HubStore; + +/// The kind of the collection that holds the vault's credentials. +pub(crate) const VAULT_KIND: &str = "vault"; +/// The kind of the collection that holds the saved profiles. +pub(crate) const PROFILES_KIND: &str = "profiles"; + +/// Why a store call failed. +#[derive(Debug, thiserror::Error)] +pub(crate) enum OpError { + /// The version the write expected is not the stored one. + #[error("the object changed on the hub")] + Conflict, + /// The hub returned a key or an object that does not authenticate: it was altered. + #[error("the hub returned data that does not authenticate")] + Damaged(#[source] OpenError), + #[error(transparent)] + Hub(#[from] HubError), +} + +pub(crate) type Call<'a, T> = BoxFuture<'a, Result>; + +/// The calls of the vault service this client makes, with sealed bytes only. +pub(crate) trait StoreOps: Send + Sync { + /// The account's keyset record as the hub stores it; `None` before one was created. + fn keyset(&self) -> Call<'_, Option>; + fn collections(&self, kind: &str) -> Call<'_, Vec>; + fn create_collection(&self, request: v2::CreateCollectionRequest) -> Call<'_, v2::Collection>; + /// The key envelope of a collection, sealed by the account key. + fn collection_key(&self, collection_id: &str) -> Call<'_, v2::CollectionKey>; + fn put_object(&self, request: v2::PutObjectRequest) -> Call<'_, v2::ObjectVersion>; + fn delete_object(&self, request: v2::DeleteObjectRequest) -> Call<'_, v2::ObjectVersion>; + fn changes(&self, collection_id: &str, after: u64) -> Call<'_, v2::ChangeBatch>; + /// Yields whenever a head of one of `collections` moved; ends when the connection does. + fn watch(&self, collections: Vec) -> Call<'_, BoxStream<'static, ()>>; +} + +/// A collection of the account and the key that opens its objects. +#[derive(Clone)] +pub(crate) struct Located { + id: String, + key: Arc, + envelope: Vec, +} + +impl Located { + pub(crate) fn id(&self) -> &str { + &self.id + } + + pub(crate) fn key(&self) -> &CollectionKey { + &self.key + } + + /// The sealed key as the hub holds it, for the cache. + pub(crate) fn envelope(&self) -> &[u8] { + &self.envelope + } +} + +/// Of the live collections of `kind`, the oldest: when two devices made one at +/// the same moment, it is the collection and the others stay unused. +fn oldest<'a>(listed: &'a [v2::Collection], kind: &str) -> Option<&'a v2::Collection> { + listed + .iter() + .filter(|c| !c.deleted && c.kind == kind) + .min_by(|a, b| { + (a.created_at_ms, &a.collection_id).cmp(&(b.created_at_ms, &b.collection_id)) + }) +} + +/// The account's collections on one hub, with the keys to read them. +pub(crate) struct Store { + ops: Arc, + keys: Arc, +} + +impl Store { + pub(crate) fn new(ops: Arc, keys: Arc) -> Self { + Self { ops, keys } + } + + /// The account's collection of `kind`, if the hub has one. When several exist (two + /// devices made one at once) the oldest is the collection; the others stay unused. + pub(crate) async fn find(&self, kind: &str) -> Result, OpError> { + let listed = self.ops.collections(kind).await?; + match oldest(&listed, kind) { + Some(chosen) => Ok(Some(self.open(chosen).await?)), + None => Ok(None), + } + } + + /// The account's own collection: the envelope was sealed by this account's key. + async fn open(&self, chosen: &v2::Collection) -> Result { + let record = self.ops.collection_key(&chosen.collection_id).await?; + if record.collection_id != chosen.collection_id || record.wrapped_key.len() > 256 { + return Err(OpError::Hub(HubError::PermissionDenied)); + } + let key = self + .keys + .own_collection(&chosen.collection_id, &record.wrapped_key) + .map_err(OpError::Damaged)?; + Ok(Located { + id: chosen.collection_id.clone(), + key, + envelope: record.wrapped_key, + }) + } + + /// The personal collection of `kind`, created first if the account has none. + pub(crate) async fn ensure(&self, kind: &str) -> Result { + if let Some(found) = self.find(kind).await? { + return Ok(found); + } + let id = Builder::from_random_bytes(random_bytes::<16>().map_err(remote)?) + .into_uuid() + .to_string(); + let (key, envelope) = self.keys.new_collection(&id)?; + let sealed_name = key.seal_name(&id, kind).map_err(remote)?; + self.ops + .create_collection(v2::CreateCollectionRequest { + kind: kind.to_owned(), + sealed_name, + wrapped_key: envelope, + collection_id: id, + }) + .await?; + // A device that created one at the same moment may have made an older one. + self.find(kind) + .await? + .ok_or(OpError::Hub(HubError::NotFound)) + } + + /// Seals `plaintext` for the object and stores it. `expected` is the stored version, or 0 + /// to create. + pub(crate) async fn put( + &self, + at: &Located, + kind: &str, + object_id: &str, + plaintext: &[u8], + expected: u64, + ) -> Result { + let ciphertext = at + .key + .seal_object(&at.id, object_id, kind, plaintext) + .map_err(remote)?; + self.ops + .put_object(v2::PutObjectRequest { + collection_id: at.id.clone(), + object_id: object_id.to_owned(), + expected_version: expected, + kind: kind.to_owned(), + ciphertext, + }) + .await + } + + /// Replaces the object with a tombstone. + pub(crate) async fn remove( + &self, + at: &Located, + object_id: &str, + expected: u64, + ) -> Result { + self.ops + .delete_object(v2::DeleteObjectRequest { + collection_id: at.id.clone(), + object_id: object_id.to_owned(), + expected_version: expected, + }) + .await + } + + /// The changes of the collection after stream version `after`. + pub(crate) async fn changes( + &self, + at: &Located, + after: u64, + ) -> Result { + self.ops.changes(&at.id, after).await + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/store/client.rs b/crates/hub-io/src/store/client.rs new file mode 100644 index 00000000..678254a1 --- /dev/null +++ b/crates/hub-io/src/store/client.rs @@ -0,0 +1,48 @@ +//! Client selection is separate from the one shared store RPC implementation. +use std::sync::Arc; + +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +use super::OpError; +use crate::{ + account::source::{CapturedAccount, CapturedMembership}, + pool::{Pool, captured::CapturedClient}, +}; + +pub(super) struct StoreClient { + fixed: CapturedClient, +} +impl StoreClient { + pub(super) fn captured( + pool: Arc, + native: &CapturedAccount, + source: Arc, + ) -> Result { + let fixed = CapturedClient::account(pool.clone(), native, source)?; + Ok(Self { fixed }) + } + pub(super) async fn fresh(&self) -> Result { + self.fixed.fresh().await.map_err(OpError::from) + } + + pub(super) fn membership( + pool: Arc, + native: &CapturedMembership, + ) -> Result { + native.check()?; + let fixed = CapturedClient::current(pool.clone(), native.hub().clone())?; + if fixed.hub() != native.hub() { + return Err(HubError::Stale); + } + native.check()?; + Ok(Self { fixed }) + } + + pub(super) fn failure(&self, error: ClientError) -> OpError { + if error.is_conflict() { + return OpError::Conflict; + } + OpError::Hub(self.fixed.failure(error)) + } +} diff --git a/crates/hub-io/src/store/grpc.rs b/crates/hub-io/src/store/grpc.rs new file mode 100644 index 00000000..25936f3e --- /dev/null +++ b/crates/hub-io/src/store/grpc.rs @@ -0,0 +1,157 @@ +//! The store service of a signed-in hub. + +use std::sync::Arc; + +use super::client::StoreClient; +use crate::account::source::{CapturedAccount, CapturedMembership}; +use futures::StreamExt as _; +use nocterm_hub_client::ClientError; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +use super::{Call, OpError, StoreOps}; +use crate::pool::Pool; + +/// Reaches the vault service of one hub with the pooled client. +pub(crate) struct HubStore { + client: StoreClient, +} + +macro_rules! store_rpc { + ($client:expr, $method:ident, $request:expr) => { + $client.vault().$method($request).await + }; +} + +impl HubStore { + pub(crate) fn captured( + pool: Arc, + native: &CapturedAccount, + source: Arc, + ) -> Result { + Ok(Self { + client: StoreClient::captured(pool, native, source)?, + }) + } + + fn failure(&self, error: ClientError) -> OpError { + self.client.failure(error) + } + + pub(crate) fn membership( + pool: Arc, + native: &CapturedMembership, + ) -> Result { + Ok(Self { + client: StoreClient::membership(pool, native)?, + }) + } +} + +impl StoreOps for HubStore { + fn keyset(&self) -> Call<'_, Option> { + Box::pin(async move { + let client = self.client.fresh().await?; + client + .keyset_record() + .await + .map_err(|error| self.failure(error)) + }) + } + + fn collections(&self, kind: &str) -> Call<'_, Vec> { + let kind = kind.to_owned(); + Box::pin(async move { + let client = self.client.fresh().await?; + let list = store_rpc!( + client, + list_collections, + v2::ListCollectionsRequest { kind } + ) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(list.into_inner().collections) + }) + } + + fn create_collection(&self, request: v2::CreateCollectionRequest) -> Call<'_, v2::Collection> { + Box::pin(async move { + let client = self.client.fresh().await?; + let made = store_rpc!(client, create_collection, request) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(made.into_inner()) + }) + } + + fn collection_key(&self, collection_id: &str) -> Call<'_, v2::CollectionKey> { + let collection_id = collection_id.to_owned(); + Box::pin(async move { + let client = self.client.fresh().await?; + let key = store_rpc!( + client, + get_collection_key, + v2::IdRequest { id: collection_id } + ) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(key.into_inner()) + }) + } + + fn put_object(&self, request: v2::PutObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + let client = self.client.fresh().await?; + let stored = store_rpc!(client, put_object, request) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(stored.into_inner()) + }) + } + + fn delete_object(&self, request: v2::DeleteObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + let client = self.client.fresh().await?; + let stored = store_rpc!(client, delete_object, request) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(stored.into_inner()) + }) + } + + fn changes(&self, collection_id: &str, after: u64) -> Call<'_, v2::ChangeBatch> { + let collection_id = collection_id.to_owned(); + Box::pin(async move { + let client = self.client.fresh().await?; + let batch = store_rpc!( + client, + list_changes, + v2::ListChangesRequest { + collection_id, + after, + limit: 0, + } + ) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(batch.into_inner()) + }) + } + + fn watch(&self, collections: Vec) -> Call<'_, futures::stream::BoxStream<'static, ()>> { + Box::pin(async move { + let client = self.client.fresh().await?; + let stream = store_rpc!( + client, + watch_changes, + v2::WatchChangesRequest { + collection_ids: collections, + } + ) + .map_err(|status| self.failure(ClientError::from(status)))? + .into_inner(); + // A hint only says "read again"; the stream ends at the first failure. + let hints = futures::stream::unfold(stream, |mut stream| async move { + match stream.message().await { + Ok(Some(_)) => Some(((), stream)), + _ => None, + } + }); + Ok(hints.boxed()) + }) + } +} diff --git a/crates/hub-io/src/store/memory.rs b/crates/hub-io/src/store/memory.rs new file mode 100644 index 00000000..4b691d49 --- /dev/null +++ b/crates/hub-io/src/store/memory.rs @@ -0,0 +1,379 @@ +//! An in-memory hub store for tests: versions, conflicts, tombstones and change streams as the +//! hub keeps them, and an offline switch. + +use std::collections::{BTreeMap, HashMap, VecDeque}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Mutex, MutexGuard, PoisonError}; + +use futures::StreamExt as _; +use nocterm_core::SharedCause; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubError; +use nocterm_session::testing::Gate; + +use super::{Call, OpError, StoreOps}; +mod accounts; +pub(crate) use accounts::MemoryAccountStores; + +#[derive(Default)] +struct Held { + collections: Vec, + keys: HashMap, + objects: HashMap>, + heads: HashMap, + clock: i64, + keyset: Option, + scripted_changes: VecDeque, + change_calls: usize, +} + +/// One account's collections on a pretend hub. +#[derive(Default)] +pub(crate) struct MemoryHub { + held: Mutex, + offline: AtomicBool, + pauses: Mutex)>>, +} + +/// Deterministic boundaries around actual MemoryHub behavior. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) enum StoreBoundary { + KeysetReady, + WatchReady, + Collections, + CreateCollection, + CreateCollectionReady, + ChangesReady, + PutReady, + Put, +} + +impl MemoryHub { + pub(crate) fn script_changes(&self, pages: Vec) { + self.held().scripted_changes = pages.into(); + } + + pub(crate) fn change_calls(&self) -> usize { + self.held().change_calls + } + /// Returns (entered, release). The next matching operation pauses once. + pub(crate) fn pause_next(&self, at: StoreBoundary) -> (Gate, Gate) { + self.pause_outcome(at, None) + } + pub(crate) fn pause_next_error(&self, at: StoreBoundary, error: HubError) -> (Gate, Gate) { + self.pause_outcome(at, Some(error)) + } + fn pause_outcome(&self, at: StoreBoundary, error: Option) -> (Gate, Gate) { + let entered = Gate::pending(); + let release = Gate::pending(); + self.pauses + .lock() + .unwrap() + .insert(at, (entered.clone(), release.clone(), error)); + (entered, release) + } + + async fn boundary(&self, at: StoreBoundary) -> Result<(), OpError> { + let pause = self.pauses.lock().unwrap().remove(&at); + if let Some((entered, release, error)) = pause { + entered.release(); + release.wait().await; + if let Some(error) = error { + return Err(OpError::Hub(error)); + } + } + Ok(()) + } + + pub(crate) fn set_offline(&self, offline: bool) { + self.offline.store(offline, Ordering::SeqCst); + } + + fn held(&self) -> MutexGuard<'_, Held> { + self.held.lock().unwrap_or_else(PoisonError::into_inner) + } + + fn reach(&self) -> Result<(), OpError> { + if self.offline.load(Ordering::SeqCst) { + let address = "hub.test".parse().expect("a valid address"); + return Err(OpError::Hub(HubError::Unreachable { + address, + cause: SharedCause::new(std::io::Error::other("offline")), + })); + } + Ok(()) + } + + /// The keyset record the hub holds for the account. + pub(crate) fn set_keyset(&self, record: Option) { + self.held().keyset = record; + } + + /// A collection another client made, of any kind. + pub(crate) fn add_foreign(&self, kind: &str) -> String { + let mut held = self.held(); + held.clock += 1; + let id = format!("foreign-{}", held.collections.len()); + let collection = v2::Collection { + collection_id: id.clone(), + kind: kind.to_owned(), + sealed_name: vec![1], + head: 0, + created_at_ms: held.clock, + deleted: false, + }; + held.collections.push(collection); + id + } + + /// The ids of the collections of `kind`. + pub(crate) fn collections_of_kind(&self, kind: &str) -> Vec { + self.held() + .collections + .iter() + .filter(|c| c.kind == kind) + .map(|c| c.collection_id.clone()) + .collect() + } + + /// Every stored object of a collection, tombstones included, as the hub holds them. + pub(crate) fn objects(&self, collection_id: &str) -> Vec { + let held = self.held(); + held.objects + .get(collection_id) + .map(|objects| objects.values().cloned().collect()) + .unwrap_or_default() + } + + /// Replaces an object's ciphertext behind the client's back, as a hostile hub could. + pub(crate) fn tamper(&self, collection_id: &str, object_id: &str, ciphertext: Vec) { + let mut held = self.held(); + if let Some(object) = held + .objects + .get_mut(collection_id) + .and_then(|objects| objects.get_mut(object_id)) + { + object.ciphertext = ciphertext; + } + } + + /// Replaces the sealed key a collection hands out. + pub(crate) fn tamper_key(&self, collection_id: &str, envelope: Vec) { + if let Some(record) = self.held().keys.get_mut(collection_id) { + record.wrapped_key = envelope; + } + } + + fn write( + &self, + collection_id: &str, + object_id: &str, + expected: u64, + content: Option<(String, Vec)>, + ) -> Result { + let mut held = self.held(); + if !held + .collections + .iter() + .any(|c| c.collection_id == collection_id) + { + return Err(OpError::Hub(HubError::NotFound)); + } + let current = held + .objects + .get(collection_id) + .and_then(|objects| objects.get(object_id)) + .map(|object| object.version) + .unwrap_or(0); + if current != expected { + return Err(OpError::Conflict); + } + let head = held.heads.entry(collection_id.to_owned()).or_default(); + *head += 1; + let stream_version = *head; + let (kind, ciphertext, deleted) = match content { + Some((kind, ciphertext)) => (kind, ciphertext, false), + None => { + let kind = held + .objects + .get(collection_id) + .and_then(|objects| objects.get(object_id)) + .map(|object| object.kind.clone()) + .unwrap_or_default(); + (kind, Vec::new(), true) + } + }; + let version = current + 1; + held.objects + .entry(collection_id.to_owned()) + .or_default() + .insert( + object_id.to_owned(), + v2::StoredObject { + collection_id: collection_id.to_owned(), + object_id: object_id.to_owned(), + version, + kind, + ciphertext, + deleted, + updated_at_ms: 0, + updated_by_device: String::new(), + stream_version, + }, + ); + Ok(v2::ObjectVersion { + collection_id: collection_id.to_owned(), + object_id: object_id.to_owned(), + version, + stream_version, + }) + } +} + +impl StoreOps for MemoryHub { + fn keyset(&self) -> Call<'_, Option> { + Box::pin(async move { + self.reach()?; + let record = self.held().keyset.clone(); + self.boundary(StoreBoundary::KeysetReady).await?; + Ok(record) + }) + } + + fn collections(&self, kind: &str) -> Call<'_, Vec> { + let kind = kind.to_owned(); + Box::pin(async move { + self.boundary(StoreBoundary::Collections).await?; + self.reach()?; + Ok(self + .held() + .collections + .iter() + .filter(|c| c.kind == kind) + .cloned() + .collect()) + }) + } + + fn create_collection(&self, request: v2::CreateCollectionRequest) -> Call<'_, v2::Collection> { + Box::pin(async move { + self.boundary(StoreBoundary::CreateCollection).await?; + self.reach()?; + let collection = { + let mut held = self.held(); + if held + .collections + .iter() + .any(|row| row.collection_id == request.collection_id) + { + return Err(OpError::Conflict); + } + held.clock += 1; + let collection = v2::Collection { + collection_id: request.collection_id.clone(), + kind: request.kind, + sealed_name: request.sealed_name, + head: 0, + created_at_ms: held.clock, + deleted: false, + }; + held.keys.insert( + request.collection_id.clone(), + v2::CollectionKey { + collection_id: request.collection_id, + key_version: 1, + wrapped_key: request.wrapped_key, + }, + ); + held.collections.push(collection.clone()); + collection + }; + self.boundary(StoreBoundary::CreateCollectionReady).await?; + Ok(collection) + }) + } + + fn collection_key(&self, collection_id: &str) -> Call<'_, v2::CollectionKey> { + let id = collection_id.to_owned(); + Box::pin(async move { + self.reach()?; + self.held() + .keys + .get(&id) + .cloned() + .ok_or(OpError::Hub(HubError::NotFound)) + }) + } + + fn put_object(&self, request: v2::PutObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + self.boundary(StoreBoundary::Put).await?; + self.reach()?; + let written = self.write( + &request.collection_id, + &request.object_id, + request.expected_version, + Some((request.kind, request.ciphertext)), + )?; + self.boundary(StoreBoundary::PutReady).await?; + Ok(written) + }) + } + + fn delete_object(&self, request: v2::DeleteObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + self.reach()?; + self.write( + &request.collection_id, + &request.object_id, + request.expected_version, + None, + ) + }) + } + + fn changes(&self, collection_id: &str, after: u64) -> Call<'_, v2::ChangeBatch> { + let id = collection_id.to_owned(); + Box::pin(async move { + self.reach()?; + let scripted = { + let mut held = self.held(); + held.change_calls += 1; + held.scripted_changes.pop_front() + }; + let batch = scripted.unwrap_or_else(|| { + let held = self.held(); + let mut changes: Vec<_> = held + .objects + .get(&id) + .map(|objects| { + objects + .values() + .filter(|object| object.stream_version > after) + .cloned() + .collect() + }) + .unwrap_or_default(); + changes.sort_by_key(|object: &v2::StoredObject| object.stream_version); + v2::ChangeBatch { + changes, + head: held.heads.get(&id).copied().unwrap_or(0), + more: false, + } + }); + self.boundary(StoreBoundary::ChangesReady).await?; + Ok(batch) + }) + } + + fn watch( + &self, + _collections: Vec, + ) -> Call<'_, futures::stream::BoxStream<'static, ()>> { + Box::pin(async move { + self.reach()?; + self.boundary(StoreBoundary::WatchReady).await?; + Ok(futures::stream::pending().boxed()) + }) + } +} diff --git a/crates/hub-io/src/store/memory/accounts.rs b/crates/hub-io/src/store/memory/accounts.rs new file mode 100644 index 00000000..8a1bee40 --- /dev/null +++ b/crates/hub-io/src/store/memory/accounts.rs @@ -0,0 +1,39 @@ +//! Native account routing over the shared MemoryHub storage implementation. +use super::MemoryHub; +use crate::{ + account::{ + operations::OperationStores, + source::{CapturedAccount, CapturedMembership}, + }, + store::StoreOps, +}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; +use std::{collections::HashMap, sync::Arc}; + +pub(crate) struct MemoryAccountStores(HashMap>); +impl MemoryAccountStores { + pub(crate) fn new(accounts: impl IntoIterator)>) -> Self { + Self(accounts.into_iter().collect()) + } + fn account(&self, id: &str) -> Result, HubError> { + self.0 + .get(id) + .cloned() + .map(|hub| hub as Arc) + .ok_or(HubError::Stale) + } +} +impl OperationStores for MemoryAccountStores { + fn opened( + &self, + native: &CapturedAccount, + source: Arc, + ) -> Result, HubError> { + source.check_current().map_err(|_| HubError::Stale)?; + self.account(native.subject()?.account()) + } + fn membership(&self, native: &CapturedMembership) -> Result, HubError> { + native.check()?; + self.account(native.subject()?.account()) + } +} diff --git a/crates/hub-io/src/store/tests.rs b/crates/hub-io/src/store/tests.rs new file mode 100644 index 00000000..37017961 --- /dev/null +++ b/crates/hub-io/src/store/tests.rs @@ -0,0 +1,125 @@ +use std::sync::Arc; + +use super::memory::MemoryHub; +use super::*; +use crate::account::testing::open_keys; + +fn store(hub: &Arc) -> Store { + Store::new(hub.clone(), open_keys()) +} + +#[tokio::test] +async fn the_collection_of_a_kind_is_created_once_and_found_again() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + assert!(store.find(VAULT_KIND).await.unwrap().is_none()); + let first = store.ensure(VAULT_KIND).await.unwrap(); + let again = store.ensure(VAULT_KIND).await.unwrap(); + assert_eq!(first.id(), again.id()); +} + +#[tokio::test] +async fn objects_are_stored_sealed_and_a_stale_write_conflicts() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + let at = store.ensure(VAULT_KIND).await.unwrap(); + let v1 = store + .put(&at, "credential", "o1", b"plain text", 0) + .await + .unwrap(); + assert_eq!(v1.version, 1); + let stored = hub.objects(at.id()); + assert_eq!(stored.len(), 1); + assert!(!String::from_utf8_lossy(&stored[0].ciphertext).contains("plain text")); + assert!(matches!( + store.put(&at, "credential", "o1", b"again", 0).await, + Err(OpError::Conflict) + )); + let v2 = store + .put(&at, "credential", "o1", b"again", 1) + .await + .unwrap(); + assert_eq!(v2.version, 2); + let opened = at + .key() + .open_object( + at.id(), + "o1", + "credential", + &hub.objects(at.id())[0].ciphertext, + ) + .unwrap(); + assert_eq!(opened.as_slice(), b"again"); +} + +#[tokio::test] +async fn removing_leaves_a_tombstone_in_the_change_feed() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + let at = store.ensure(PROFILES_KIND).await.unwrap(); + store.put(&at, "profile", "p", b"{}", 0).await.unwrap(); + store.remove(&at, "p", 1).await.unwrap(); + let batch = store.changes(&at, 1).await.unwrap(); + assert_eq!(batch.changes.len(), 1); + assert!(batch.changes[0].deleted); + assert_eq!(batch.head, 2); +} + +#[tokio::test] +async fn collections_of_other_kinds_are_never_picked() { + let hub = Arc::new(MemoryHub::default()); + hub.add_foreign("future-kind"); + let store = store(&hub); + assert!(store.find(VAULT_KIND).await.unwrap().is_none()); + let mine = store.ensure(VAULT_KIND).await.unwrap(); + assert!(!mine.id().starts_with("foreign")); +} + +#[tokio::test] +async fn when_two_devices_each_made_a_collection_the_oldest_is_used() { + let hub = Arc::new(MemoryHub::default()); + let one = store(&hub); + let first = one.ensure(VAULT_KIND).await.unwrap(); + // A second device that listed nothing before the first one's create made its own. + let keys = open_keys(); + let (key, envelope) = keys.new_collection("later").unwrap(); + hub.create_collection(v2::CreateCollectionRequest { + kind: VAULT_KIND.into(), + sealed_name: key.seal_name("later", VAULT_KIND).unwrap(), + wrapped_key: envelope, + collection_id: "later".into(), + }) + .await + .unwrap(); + assert_eq!( + one.find(VAULT_KIND).await.unwrap().unwrap().id(), + first.id() + ); +} + +#[tokio::test] +async fn a_key_envelope_the_hub_altered_is_damage_not_a_new_collection() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + let at = store.ensure(VAULT_KIND).await.unwrap(); + // Another client session has no cached key: it must open the envelope the hub returns. + let fresh = Store::new(hub.clone(), open_keys()); + hub.tamper_key(at.id(), vec![0; 64]); + assert!(matches!( + fresh.find(VAULT_KIND).await, + Err(OpError::Damaged(_)) + )); +} + +#[tokio::test] +async fn an_unreachable_hub_is_a_hub_error_and_creates_nothing() { + let hub = Arc::new(MemoryHub::default()); + hub.set_offline(true); + let store = store(&hub); + assert!(matches!( + store.ensure(VAULT_KIND).await, + Err(OpError::Hub(HubError::Unreachable { .. })) + )); + hub.set_offline(false); + assert!(store.find(VAULT_KIND).await.unwrap().is_none()); +} diff --git a/crates/hub-io/src/task.rs b/crates/hub-io/src/task.rs new file mode 100644 index 00000000..06567df7 --- /dev/null +++ b/crates/hub-io/src/task.rs @@ -0,0 +1,97 @@ +//! Work on the adapter's threads whose result any executor can await. + +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +use futures::FutureExt as _; +use futures::channel::oneshot; +use tokio::runtime::Handle; +use tokio::task::AbortHandle; + +/// The result of a task on the adapter's runtime; abandons the task when dropped. +/// +/// Resolves to `None` when the runtime stopped before the task finished. +pub(crate) struct Spawned { + result: oneshot::Receiver, + task: AbortHandle, +} + +/// Runs `work` on the runtime of `handle`. +pub(crate) fn spawn( + handle: &Handle, + work: impl Future + Send + 'static, +) -> Spawned { + let (sender, result) = oneshot::channel(); + let task = handle + .spawn(async move { + let _ = sender.send(work.await); + }) + .abort_handle(); + Spawned { result, task } +} + +impl Future for Spawned { + type Output = Option; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + self.result.poll_unpin(cx).map(Result::ok) + } +} + +impl Drop for Spawned { + fn drop(&mut self) { + self.task.abort(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + struct Release(Option>); + impl Drop for Release { + fn drop(&mut self) { + if let Some(sender) = self.0.take() { + let _ = sender.send(()); + } + } + } + + #[test] + fn a_result_can_be_awaited_by_an_executor_without_a_tokio_context() { + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .build() + .unwrap(); + assert_eq!( + futures::executor::block_on(spawn(runtime.handle(), async { 7 })), + Some(7) + ); + } + + #[tokio::test] + async fn dropping_an_unpolled_answer_releases_the_task_resources() { + let (sender, released) = oneshot::channel(); + let resource = Release(Some(sender)); + let answer = spawn(&Handle::current(), async move { + let _resource = resource; + std::future::pending::<()>().await; + }); + drop(answer); + tokio::time::timeout(std::time::Duration::from_secs(1), released) + .await + .unwrap() + .unwrap(); + } + + #[test] + fn runtime_shutdown_ends_an_unanswered_task() { + let runtime = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + let answer = spawn(runtime.handle(), std::future::pending::<()>()); + runtime.shutdown_background(); + assert_eq!(futures::executor::block_on(answer), None); + } +} diff --git a/crates/hub-io/src/vault.rs b/crates/hub-io/src/vault.rs new file mode 100644 index 00000000..d5b221f7 --- /dev/null +++ b/crates/hub-io/src/vault.rs @@ -0,0 +1,262 @@ +//! The vault of the account hub: credentials kept as sealed objects on the hub. +//! +//! [`HubVault`] implements the vault port over one hub's `vault` collection. The vault's +//! password is the account's: the login password, or a passphrase of its own for an account +//! that signs in through a provider. Credentials are readable offline from the encrypted cache; +//! writes need the hub. The vault has no password of its own to create or change here, and no +//! device unlock, so those operations are [`VaultError::Unsupported`]. + +mod codec; +pub(crate) mod credentials; + +mod idle; +mod ledger; +mod snapshot_budget; +mod unlock; +pub(crate) use idle::Idle; + +use std::sync::Arc; +use std::time::Duration; + +use futures::channel::mpsc as progress; +use futures::future::BoxFuture; +use nocterm_session::{CredentialId, Secret}; +use nocterm_vault::{ + CredentialBinding, CredentialInfo, DeviceCapability, VaultAccess, VaultError, VaultFuture, + VaultStatus, +}; +use tokio::runtime::Handle; + +pub(crate) use self::ledger::Ledger; +use self::unlock::Unlocker; +use crate::account::{Slot, operations::AccountOperations}; +use crate::task::spawn; + +pub struct HubVault { + handle: Handle, + slot: Arc, + operations: Arc, + idle: Arc, +} + +impl HubVault { + pub(crate) fn new(handle: Handle, slot: Arc, operations: Arc) -> Self { + Self { + idle: Arc::new(Idle::new(handle.clone())), + handle, + slot, + operations, + } + } + + pub(crate) fn idle(&self) -> Arc { + self.idle.clone() + } + + fn run(&self, work: impl FnOnce(Ledger) -> F + Send + 'static) -> VaultFuture + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let opened = match self.operations.opened() { + Ok(opened) => opened, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let source = opened.source().clone(); + let returned = source.clone(); + let idle = self.idle.clone(); + let task = spawn(&self.handle, async move { + let result = source + .during(async { + let ledger = opened.ledger().map_err(native_failure)?; + work(ledger).await + }) + .await + .map_err(native_failure) + .and_then(|result| result); + idle.touch(source.native()); + drop(opened); + result + }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(VaultError::Cancelled)); + returned.check().map_err(native_failure)?; + result + }) + } + + fn unsupported() -> VaultFuture { + Box::pin(async { Err(VaultError::Unsupported) }) + } +} + +impl VaultAccess for HubVault { + fn is_unlocked(&self) -> bool { + self.slot.status() == VaultStatus::Unlocked + } + + fn exists(&self) -> bool { + self.slot.status() != VaultStatus::Missing + } + + fn status(&self) -> VaultStatus { + self.slot.status() + } + + fn revision(&self) -> u64 { + self.slot.notice().count() + } + + fn changed(&self, seen: u64) -> BoxFuture<'static, u64> { + let mut changes = self.slot.notice().subscribe(); + Box::pin(async move { + loop { + let now = *changes.borrow_and_update(); + if now != seen || changes.changed().await.is_err() { + return now; + } + } + }) + } + + fn wait_unlocked(&self) -> BoxFuture<'static, ()> { + let member = self.operations.current_membership(); + let slot = Arc::clone(&self.slot); + let mut changes = slot.notice().subscribe(); + Box::pin(async move { + let Ok(member) = member else { + return; + }; + loop { + match member.with_current(|| Ok(slot.status() == VaultStatus::Unlocked)) { + Ok(false) => {} + Ok(true) | Err(_) => return, + } + if changes.changed().await.is_err() { + return; + } + } + }) + } + + fn set_auto_lock(&self, duration: Duration) { + self.idle + .set(duration, self.operations.current_opened_source().ok()); + } + + fn lock(&self) { + if let Ok(member) = self.operations.current_membership() { + let _ = member.with_current(|| { + self.idle.stop(); + self.slot.lock(); + Ok(()) + }); + } + } + + fn create(&self, _password: Secret) -> VaultFuture<()> { + Self::unsupported() + } + + fn unlock(&self, password: Secret) -> VaultFuture<()> { + let work = match self.operations.membership() { + Ok(work) => work, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let member = work.membership().clone(); + let returned = member.clone(); + let slot = match work.slot() { + Ok(slot) => slot, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let final_slot = slot.clone(); + let idle = self.idle.clone(); + let task = spawn(&self.handle, async move { + let unlocker = Unlocker::new(work); + let receipt = member + .during(unlocker.unlock(password)) + .await + .map_err(native_failure)??; + member + .with_current(|| slot.check_receipt(&member.subject()?, &receipt)) + .map_err(native_failure)?; + if let Ok(native) = member.opened() { + idle.touch(&native); + } + Ok(receipt) + }); + Box::pin(async move { + let receipt = task.await.unwrap_or(Err(VaultError::Cancelled))?; + returned + .with_current(|| final_slot.check_receipt(&returned.subject()?, &receipt)) + .map_err(native_failure) + }) + } + + fn probe_device_unlock(&self) -> VaultFuture { + Self::unsupported() + } + + fn enable_device_unlock(&self) -> VaultFuture<()> { + Self::unsupported() + } + + fn unlock_with_device(&self) -> VaultFuture<()> { + Self::unsupported() + } + + fn unlock_with_device_progress(&self) -> (VaultFuture<()>, progress::UnboundedReceiver) { + let (_, steps) = progress::unbounded(); + (Self::unsupported(), steps) + } + + fn disable_device_unlock(&self) -> VaultFuture<()> { + Self::unsupported() + } + + fn change_password(&self, _password: Secret) -> VaultFuture<()> { + Self::unsupported() + } + + fn list(&self) -> VaultFuture> { + self.run(|ledger| async move { ledger.list().await }) + } + + fn get(&self, id: CredentialId, binding: CredentialBinding) -> VaultFuture { + self.run(move |ledger| async move { ledger.get(id, binding).await }) + } + + fn put( + &self, + id: Option, + label: String, + binding: CredentialBinding, + secret: Secret, + ) -> VaultFuture { + self.run(move |ledger| async move { ledger.put(id, label, binding, secret).await }) + } + + fn update( + &self, + id: CredentialId, + label: String, + binding: CredentialBinding, + ) -> VaultFuture<()> { + self.run(move |ledger| async move { ledger.update(id, label, binding).await }) + } + + fn delete(&self, id: CredentialId) -> VaultFuture<()> { + self.run(move |ledger| async move { ledger.delete(id).await }) + } +} + +fn native_failure(error: nocterm_hubs_model::HubError) -> VaultError { + match error { + nocterm_hubs_model::HubError::KeysLocked => VaultError::Locked, + nocterm_hubs_model::HubError::NotFound => VaultError::NotFound, + other => ledger::vault_error(crate::store::OpError::Hub(other)), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/codec.rs b/crates/hub-io/src/vault/codec.rs new file mode 100644 index 00000000..c61b8acc --- /dev/null +++ b/crates/hub-io/src/vault/codec.rs @@ -0,0 +1,98 @@ +//! What a vault record holds, as sealed into one object of the vault collection. +//! +//! The plaintext is `{"v":1,"label":…,"binding":…,"secret":…}`. A record this client cannot +//! read (a newer format version, or a binding of a kind it does not know) is +//! [`Decoded::Unrecognized`]: the caller hides it, never overwrites it and never deletes it. + +use nocterm_keys::CollectionKey; +use nocterm_session::Secret; +use nocterm_vault::{CredentialBinding, VaultError}; +use serde::{Deserialize, Serialize}; +use zeroize::{Zeroize as _, Zeroizing}; + +use crate::cache::ObjectCopy; + +/// The kind of the objects that hold credentials. +pub(super) const KIND: &str = "credential"; +/// The only format this client writes and reads. +const FORMAT: u32 = 1; + +#[derive(Serialize, Deserialize)] +struct Wire { + v: u32, + label: String, + binding: serde_json::Value, + secret: String, +} + +#[derive(Deserialize)] +struct Head { + v: u32, +} + +/// A credential read from a record. +pub(super) struct Record { + pub(super) label: String, + pub(super) binding: CredentialBinding, + pub(super) secret: Secret, +} + +pub(super) enum Decoded { + Known(Record), + Unrecognized, +} + +/// The sealed text of a credential. The buffer is wiped when dropped. +pub(super) fn encode( + label: &str, + binding: &CredentialBinding, + secret: &Secret, +) -> Result>, serde_json::Error> { + let mut wire = Wire { + v: FORMAT, + label: label.to_owned(), + binding: serde_json::to_value(binding)?, + secret: secret.expose().to_owned(), + }; + let bytes = serde_json::to_vec(&wire); + wire.secret.zeroize(); + bytes.map(Zeroizing::new) +} + +/// Opens a sealed credential object: `Ok(None)` for a tombstone or an object of another kind, +/// and [`VaultError::Authentication`] for one that does not authenticate. +pub(super) fn open_sealed( + key: &CollectionKey, + collection_id: &str, + object_id: &str, + object: &ObjectCopy, +) -> Result, VaultError> { + if object.deleted || object.kind != KIND { + return Ok(None); + } + let plain = key + .open_object(collection_id, object_id, KIND, &object.ciphertext) + .map_err(|_| VaultError::Authentication)?; + Ok(Some(decode(&plain))) +} + +pub(super) fn decode(plaintext: &[u8]) -> Decoded { + match serde_json::from_slice::(plaintext) { + Ok(Head { v: FORMAT }) => {} + _ => return Decoded::Unrecognized, + } + let Ok(wire) = serde_json::from_slice::(plaintext) else { + return Decoded::Unrecognized; + }; + match serde_json::from_value::(wire.binding) { + Ok(binding) => Decoded::Known(Record { + label: wire.label, + binding, + secret: Secret::new(wire.secret), + }), + Err(_) => Decoded::Unrecognized, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/codec/tests.rs b/crates/hub-io/src/vault/codec/tests.rs new file mode 100644 index 00000000..32202401 --- /dev/null +++ b/crates/hub-io/src/vault/codec/tests.rs @@ -0,0 +1,104 @@ +use std::path::PathBuf; + +use nocterm_session::Target; + +use super::*; + +fn round_trip(binding: CredentialBinding) { + let bytes = encode("label", &binding, &Secret::new("s3cret")).unwrap(); + match decode(&bytes) { + Decoded::Known(record) => { + assert_eq!(record.label, "label"); + assert_eq!(record.binding, binding); + assert_eq!(record.secret.expose(), "s3cret"); + } + Decoded::Unrecognized => panic!("a record this client wrote must read back"), + } +} + +#[test] +fn every_binding_kind_round_trips() { + round_trip(CredentialBinding::password([Target::new( + "ann", + "example.org", + 22, + )])); + round_trip(CredentialBinding::KeyPassphrase { + path: PathBuf::from("/home/ann/.ssh/id_ed25519"), + }); + round_trip(CredentialBinding::PrivateKey { + public_key: "ssh-ed25519 AAAAC3Nza".into(), + }); +} + +#[test] +fn a_binding_of_an_unknown_kind_is_unrecognized() { + let text = br#"{"v":1,"label":"x","binding":{"kind":"hardware_token","slot":3},"secret":"s"}"#; + assert!(matches!(decode(text), Decoded::Unrecognized)); +} + +#[test] +fn a_newer_format_version_is_unrecognized() { + let text = br#"{"v":2,"label":"x","binding":{"kind":"private_key","public_key":"k"},"secret":"s","extra":1}"#; + assert!(matches!(decode(text), Decoded::Unrecognized)); +} + +#[test] +fn text_that_is_not_a_record_is_unrecognized() { + assert!(matches!(decode(b"not json"), Decoded::Unrecognized)); + assert!(matches!(decode(br#"{"v":1}"#), Decoded::Unrecognized)); +} + +#[test] +fn a_password_the_web_panel_keeps_reads_here_bound_to_its_address() { + // Exactly what the web panel seals (`sealPassword` of `nocterm-keys-wasm`): the port only + // when it is not 22. + for (target, port) in [ + ( + serde_json::json!({"host": "prod.example.com", "user": "ops"}), + 22, + ), + ( + serde_json::json!({"host": "prod.example.com", "port": 2222, "user": "ops"}), + 2222, + ), + ] { + let plain = serde_json::json!({ + "v": 1, + "label": "ops@prod.example.com", + "binding": {"kind": "password", "target": target}, + "secret": "s3cret", + }); + let Decoded::Known(record) = decode(plain.to_string().as_bytes()) else { + panic!("the web panel's record reads here"); + }; + assert_eq!( + record.binding, + CredentialBinding::password([Target::new("ops", "prod.example.com", port)]) + ); + assert_eq!(record.secret.expose(), "s3cret"); + } +} + +#[test] +fn a_key_the_web_panel_keeps_reads_here_bound_to_its_public_half() { + // What `sealSshKey` of `nocterm-keys-wasm` seals: the key without its passphrase. + let public_key = + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID7XjK7UMquFonNQf6mOb5ODK/CIOubLkjrBcI6wLh5+"; + let plain = serde_json::json!({ + "v": 1, + "label": "prod", + "binding": {"kind": "private_key", "public_key": public_key}, + "secret": "-----BEGIN OPENSSH PRIVATE KEY-----\n…\n-----END OPENSSH PRIVATE KEY-----\n", + }); + let Decoded::Known(record) = decode(plain.to_string().as_bytes()) else { + panic!("the web panel's key reads here"); + }; + assert_eq!( + record.binding, + CredentialBinding::PrivateKey { + public_key: public_key.to_owned(), + } + ); + assert_eq!(record.label, "prod"); +} diff --git a/crates/hub-io/src/vault/credentials.rs b/crates/hub-io/src/vault/credentials.rs new file mode 100644 index 00000000..dfa791ce --- /dev/null +++ b/crates/hub-io/src/vault/credentials.rs @@ -0,0 +1,175 @@ +//! Terminal credentials bound to one native account capture, including worker I/O. +use std::sync::{Arc, Weak}; + +use futures::future::BoxFuture; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; +use nocterm_session::{ + CredentialFuture, CredentialId, CredentialStore, CredentialStoreError, Secret, SecretRequest, +}; +use nocterm_vault::{CredentialBinding, VaultError}; +use tokio::runtime::Handle; + +use super::ledger::Ledger; +use crate::{ + account::{ + operations::{OpenedSource, OperationStores}, + source::LocalSourceProvider, + }, + task::spawn, +}; + +/// Retained by HubAccount so capture can be retried after an ordinary unlock. +pub(crate) struct CredentialCapture { + handle: Handle, + operations: Arc, + idle: Weak, +} + +impl CredentialCapture { + pub(crate) fn new( + handle: Handle, + operations: Arc, + idle: &Arc, + ) -> Self { + Self { + handle, + operations, + idle: Arc::downgrade(idle), + } + } + + pub(crate) fn capture( + &self, + provider: &LocalSourceProvider, + source: Arc, + ) -> Result, HubError> { + source.check_current().map_err(|_| HubError::NotSignedIn)?; + let context = OpenedSource::new(provider.capture_native()?, source)?; + context.check_current().map_err(|_| HubError::NotSignedIn)?; + Ok(Arc::new(CapturedCredentials { + handle: self.handle.clone(), + operations: Arc::downgrade(&self.operations), + context, + idle: self.idle.clone(), + })) + } +} + +struct CapturedCredentials { + handle: Handle, + operations: Weak, + context: Arc, + idle: Weak, +} +impl CapturedCredentials { + fn run( + &self, + work: impl FnOnce(Ledger) -> BoxFuture<'static, Result> + Send + 'static, + ) -> CredentialFuture { + let context = Arc::clone(&self.context); + let returned = Arc::clone(&context); + let operations = self.operations.clone(); + let idle = self.idle.clone(); + let task = spawn(&self.handle, async move { + context + .check_current() + .map_err(|_| CredentialStoreError::Stale)?; + let operations = operations.upgrade().ok_or(CredentialStoreError::Stale)?; + // Only an admitted worker retains keys and the actual original + // client; the facade itself owns weak references to both. + let keys = context + .native() + .keys() + .map_err(|_| CredentialStoreError::Stale)?; + let slot = context + .native() + .slot() + .map_err(|_| CredentialStoreError::Stale)?; + let ops = operations + .opened(context.native(), context.clone()) + .map_err(hub_failure)?; + let ledger = Ledger::captured(slot, ops, context.native().clone()); + let result = work(ledger).await; + context + .check_current() + .map_err(|_| CredentialStoreError::Stale)?; + if let Some(idle) = idle.upgrade() { + idle.touch(context.native()); + } + drop(keys); + result.map_err(failure) + }); + Box::pin(async move { + let result = task.await.ok_or(CredentialStoreError::Stale)?; + // The worker may have finished before this future was first polled. + returned + .check_current() + .map_err(|_| CredentialStoreError::Stale)?; + result + }) + } +} + +fn hub_failure(error: HubError) -> CredentialStoreError { + match error { + HubError::NotSignedIn | HubError::KeysLocked | HubError::Stale => { + CredentialStoreError::Stale + } + other => CredentialStoreError::backend(other), + } +} + +fn failure(error: VaultError) -> CredentialStoreError { + match error { + VaultError::NotFound => CredentialStoreError::NotFound, + VaultError::Locked => CredentialStoreError::Locked, + VaultError::Stale => CredentialStoreError::Stale, + other => CredentialStoreError::backend(other), + } +} + +fn not_storable() -> CredentialFuture { + Box::pin(async { Err(CredentialStoreError::NotStorable) }) +} + +impl CredentialStore for CapturedCredentials { + fn is_available(&self) -> bool { + self.operations.upgrade().is_some() && self.context.check_current().is_ok() + } + + fn wait_available(&self) -> BoxFuture<'static, ()> { + // A capture never revives. A new attempt obtains a new capture after + // unlock/login, instead of waiting on an unrelated future account. + if self.is_available() { + Box::pin(async {}) + } else { + Box::pin(futures::future::pending()) + } + } + + fn get(&self, id: CredentialId, request: &SecretRequest) -> CredentialFuture { + let Some(binding) = CredentialBinding::for_request(request) else { + return not_storable(); + }; + self.run(move |ledger| Box::pin(async move { ledger.get(id, binding).await })) + } + + fn save( + &self, + label: String, + request: &SecretRequest, + secret: Secret, + ) -> CredentialFuture { + let Some(binding) = + CredentialBinding::for_request(request).filter(|_| request.is_storable()) + else { + return not_storable(); + }; + self.run(move |ledger| { + Box::pin(async move { ledger.put(None, label, binding, secret).await }) + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/credentials/tests.rs b/crates/hub-io/src/vault/credentials/tests.rs new file mode 100644 index 00000000..7d2ea2fc --- /dev/null +++ b/crates/hub-io/src/vault/credentials/tests.rs @@ -0,0 +1,348 @@ +use super::*; +use crate::account::testing::{self, NativeAccounts as Fixture}; +use crate::store::memory::StoreBoundary; +use nocterm_hubs_model::source_identity::{LocalAccountPartition, LocalAccountSourceError}; +use nocterm_session::Target; + +fn request() -> SecretRequest { + SecretRequest::Password { + target: Target::new("ann", "example.test", 22), + retry: false, + } +} + +#[tokio::test] +async fn a_captured_store_reads_and_saves_only_its_account() { + let fixture = Fixture::new(); + let store = fixture.store(); + let id = fixture.seed().await; + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret A" + ); + fixture + .subjects() + .renew(fixture.slot().hub().clone(), testing::ACCOUNT_ID, "device"); + assert!(store.is_available()); + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret A" + ); + assert_eq!(fixture.count(0).await, 1); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn a_worker_queued_before_an_account_change_never_looks_up_b() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let pending = store.get(id, &request()); + fixture.login(1); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!(fixture.count(1).await, 0); + assert!(!store.is_available()); +} + +#[tokio::test] +async fn a_queued_save_does_not_follow_a_new_login() { + let fixture = Fixture::new(); + let store = fixture.store(); + let pending = store.save("late".into(), &request(), Secret::new("secret A")); + fixture.login(1); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!(fixture.count(0).await, 0); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn a_late_read_cannot_publish_a_cache_into_b() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let pending = store.get(id, &request()); + entered.wait().await; + fixture.login(1); + let b_id = fixture + .store() + .save("B".into(), &request(), Secret::new("secret B")) + .await + .unwrap(); + release.release(); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!( + fixture + .store() + .get(b_id, &request()) + .await + .unwrap() + .expose(), + "secret B" + ); + fixture.hub(1).set_offline(true); + assert_eq!( + fixture + .store() + .get(b_id, &request()) + .await + .unwrap() + .expose(), + "secret B" + ); +} + +#[tokio::test] +async fn an_admitted_remote_save_stays_on_a_and_cannot_replace_b_cache() { + let fixture = Fixture::new(); + let store = fixture.store(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::Put); + let pending = store.save("late A".into(), &request(), Secret::new("secret A")); + entered.wait().await; + fixture.login(1); + let b_id = fixture + .store() + .save("B".into(), &request(), Secret::new("secret B")) + .await + .unwrap(); + release.release(); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!(fixture.count(0).await, 1); + assert_eq!(fixture.count(1).await, 1); + fixture.hub(1).set_offline(true); + assert_eq!( + fixture + .store() + .get(b_id, &request()) + .await + .unwrap() + .expose(), + "secret B" + ); +} + +#[tokio::test] +async fn lock_drops_keys_even_while_a_captured_store_is_retained() { + let fixture = Fixture::new(); + let store = fixture.store(); + let keys = Arc::downgrade(&fixture.slot().open().unwrap()); + fixture.slot().lock(); + assert!(keys.upgrade().is_none()); + assert!(!store.is_available()); + assert!(matches!( + store.save("A".into(), &request(), Secret::new("A")).await, + Err(CredentialStoreError::Stale) + )); + fixture.login(0); + assert!(!store.is_available()); + assert!(fixture.store().is_available()); +} + +#[tokio::test] +async fn a_to_b_to_a_and_eviction_never_revive_an_old_store() { + let fixture = Fixture::new(); + let store = fixture.store(); + fixture.login(1); + fixture.login(0); + assert!(!store.is_available()); + assert!(matches!( + store + .get(CredentialId::from_bytes([1; 16]), &request()) + .await, + Err(CredentialStoreError::Stale) + )); + let current = fixture.store(); + fixture.subjects().evict(fixture.slot().hub()); + assert!(!current.is_available()); + assert!(matches!( + current.save("A".into(), &request(), Secret::new("A")).await, + Err(CredentialStoreError::Stale) + )); +} + +#[tokio::test] +async fn same_account_new_login_invalidates_before_replacing_keys() { + let fixture = Fixture::new(); + let store = fixture.store(); + fixture + .subjects() + .sign_in(fixture.slot().hub().clone(), testing::ACCOUNT_ID, "device"); + assert!(!store.is_available()); + assert!(matches!( + store.save("A".into(), &request(), Secret::new("A")).await, + Err(CredentialStoreError::Stale) + )); + assert_eq!(fixture.count(0).await, 0); +} + +#[tokio::test] +async fn a_completed_worker_result_is_rechecked_when_the_future_is_polled() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let keys = Arc::downgrade(&fixture.slot().open().unwrap()); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::Collections); + let pending = store.get(id, &request()); + entered.wait().await; + assert!( + keys.strong_count() > 1, + "the entered worker holds its original key lease" + ); + release.release(); + // On this single-thread executor the final key lease drops in the same + // poll that sends the worker result. Wait for that observable release, + // rather than assuming a number of scheduler yields completed the worker. + tokio::time::timeout(std::time::Duration::from_secs(10), async { + while keys.strong_count() != 1 { + tokio::task::yield_now().await; + } + }) + .await + .expect("the worker released its key lease before result polling"); + fixture.login(1); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); +} + +struct RestrictedSource { + source: Arc, + allowed: std::sync::atomic::AtomicBool, +} +impl RestrictedSource { + fn deny(&self) { + self.allowed + .store(false, std::sync::atomic::Ordering::SeqCst); + } +} +impl LocalAccountSource for RestrictedSource { + fn partition(&self) -> LocalAccountPartition { + self.source.partition() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + self.source.check_current()?; + if self.allowed.load(std::sync::atomic::Ordering::SeqCst) { + Ok(()) + } else { + Err(LocalAccountSourceError::Stale) + } + } +} + +struct DescribedSource(LocalAccountPartition); +impl LocalAccountSource for DescribedSource { + fn partition(&self) -> LocalAccountPartition { + self.0.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + Ok(()) + } +} + +#[tokio::test] +async fn a_caller_partition_cannot_select_credentials_of_another_native_account() { + let fixture = Fixture::new(); + let source = fixture.source(); + let described = Arc::new(DescribedSource(source.partition())); + fixture.login(1); + assert!(matches!( + fixture.capture_credentials(source), + Err(HubError::NotSignedIn) + )); + assert!(matches!( + fixture.capture_credentials(described), + Err(HubError::Stale) + )); + assert!(fixture.store().is_available()); +} + +#[tokio::test] +async fn an_app_availability_wrapper_can_only_restrict_native_credentials() { + let fixture = Fixture::new(); + let restriction = Arc::new(RestrictedSource { + source: fixture.source(), + allowed: true.into(), + }); + let store = fixture.capture_credentials(restriction.clone()).unwrap(); + let id = fixture.seed().await; + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret A" + ); + restriction.deny(); + assert!(!store.is_available()); + assert!(matches!( + store + .save("denied".into(), &request(), Secret::new("secret")) + .await, + Err(CredentialStoreError::Stale) + )); + assert_eq!(fixture.count(0).await, 1); +} + +#[tokio::test] +async fn in_flight_keys_are_dropped_after_a_locked_operation_finishes() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let keys = Arc::downgrade(&fixture.slot().open().unwrap()); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let pending = store.get(id, &request()); + entered.wait().await; + fixture.slot().lock(); + assert!( + keys.upgrade().is_some(), + "only the in-flight worker holds the original keys" + ); + release.release(); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert!(keys.upgrade().is_none()); + assert!(!store.is_available()); +} + +#[tokio::test] +async fn interactive_answers_and_private_keys_are_never_saved_from_a_prompt() { + let fixture = Fixture::new(); + let store = fixture.store(); + let requests = [ + SecretRequest::Interactive { + prompt: "MFA".into(), + echo: false, + }, + SecretRequest::PrivateKey { + credential: CredentialId::from_bytes([1; 16]), + public_key: "ssh-ed25519 exact".into(), + }, + ]; + for request in requests { + assert!(matches!( + store + .save("denied".into(), &request, Secret::new("secret")) + .await, + Err(CredentialStoreError::NotStorable) + )); + } + assert_eq!(fixture.count(0).await, 0); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn a_captured_revocation_cannot_erase_replacement_keys_or_cache() { + let fixture = Fixture::new(); + let original = fixture.slot().open().unwrap(); + fixture.login(1); + let store = fixture.store(); + let id = store + .save("B".into(), &request(), Secret::new("secret B")) + .await + .unwrap(); + fixture.slot().erase_if(&original).unwrap(); + fixture.hub(1).set_offline(true); + assert!(store.is_available()); + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret B" + ); + let current = fixture.slot().open().unwrap(); + fixture.slot().erase_if(¤t).unwrap(); + assert!(!store.is_available()); + assert_eq!(fixture.slot().status(), nocterm_vault::VaultStatus::Missing); +} diff --git a/crates/hub-io/src/vault/idle.rs b/crates/hub-io/src/vault/idle.rs new file mode 100644 index 00000000..02a327d7 --- /dev/null +++ b/crates/hub-io/src/vault/idle.rs @@ -0,0 +1,82 @@ +//! Inactivity belongs to the exact native account whose work completed. +use crate::account::source::CapturedAccount; +use std::{ + sync::{Arc, Mutex, PoisonError}, + time::Duration, +}; +use tokio::{runtime::Handle, task::AbortHandle}; + +#[derive(Default)] +struct IdleState { + limit: Option, + timer: Option, +} + +pub(crate) struct Idle { + handle: Handle, + state: Mutex, +} + +impl Idle { + pub(crate) fn new(handle: Handle) -> Self { + Self { + handle, + state: Mutex::default(), + } + } + fn state(&self) -> std::sync::MutexGuard<'_, IdleState> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } + /// Locks after `duration` without use; zero keeps the keys open. + pub(crate) fn set(&self, duration: Duration, original: Option>) { + self.state().limit = (!duration.is_zero()).then_some(duration); + if let Some(original) = original { + self.touch(&original); + } + } + pub(crate) fn touch(&self, original: &Arc) { + // Pool membership stays admitted while the timer state changes. Late A + // cannot abort or restart B's countdown. + let _ = original.publish(|| { + let mut state = self.state(); + if let Some(timer) = state.timer.take() { + timer.abort(); + } + let Some(limit) = state.limit else { + return; + }; + let owner = original.clone(); + state.timer = Some( + self.handle + .spawn(async move { + tokio::time::sleep(limit).await; + let _ = owner.with_current(|keys| { + owner.slot()?.lock_keys_if(keys); + Ok(()) + }); + }) + .abort_handle(), + ); + }); + } + /// Caller holds original native membership admission for explicit lock. + pub(crate) fn stop(&self) { + if let Some(timer) = self.state().timer.take() { + timer.abort(); + } + } +} + +impl Drop for Idle { + fn drop(&mut self) { + if let Some(timer) = self + .state + .get_mut() + .unwrap_or_else(PoisonError::into_inner) + .timer + .take() + { + timer.abort(); + } + } +} diff --git a/crates/hub-io/src/vault/ledger.rs b/crates/hub-io/src/vault/ledger.rs new file mode 100644 index 00000000..b13d6b39 --- /dev/null +++ b/crates/hub-io/src/vault/ledger.rs @@ -0,0 +1,368 @@ +//! The credentials of the vault collection: read, written and deleted as sealed objects. +//! +//! Reads come from the encrypted cache, which every call first tries to bring up to date from +//! the hub; an unreachable hub leaves the cached copy, so credentials stay readable offline. +//! Writes need the hub. A record that does not decode (unknown format or binding) is not +//! listed, cannot be overwritten or deleted, and stays on the hub untouched. A record that +//! does not authenticate is the hub's forgery and is an error, never a silent skip. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use nocterm_hubs_model::HubError; +use nocterm_keys::random_bytes; +use nocterm_session::{CredentialId, Secret}; +use nocterm_vault::{CredentialBinding, CredentialInfo, VaultError}; + +use super::codec::{self, Decoded, KIND}; +use crate::account::{OpenKeys, Slot, source::CapturedAccount}; +mod access; +use super::snapshot_budget::{SnapshotBudget, merge_vault}; +use crate::cache::{ObjectCopy, VaultCopy}; +use crate::classify::unreachable; +use crate::store::{Located, OpError, Store, StoreOps, VAULT_KIND}; +use access::LedgerAccess; + +/// How often a write retries after another device changed the same record. +const WRITE_ATTEMPTS: usize = 2; + +pub(crate) struct Ledger { + access: LedgerAccess, + ops: Arc, +} + +/// A failure of a store call, as the vault reports it. +pub(super) fn vault_error(error: OpError) -> VaultError { + match error { + OpError::Conflict => VaultError::Conflict, + OpError::Hub(HubError::NotSignedIn | HubError::Stale) => VaultError::Stale, + OpError::Damaged(_) => VaultError::Authentication, + OpError::Hub(error) if unreachable(&error) => VaultError::Offline, + OpError::Hub(error) => VaultError::Io(std::io::Error::other(error)), + } +} + +impl Ledger { + #[cfg(test)] + pub(crate) fn new(slot: Arc, ops: Arc) -> Self { + Self { + access: LedgerAccess::live(slot), + ops, + } + } + + pub(crate) fn captured( + slot: Arc, + ops: Arc, + capture: Arc, + ) -> Self { + Self { + access: LedgerAccess::captured(slot, capture), + ops, + } + } + + fn store(&self, keys: &Arc) -> Store { + Store::new(Arc::clone(&self.ops), Arc::clone(keys)) + } + + fn keys(&self) -> Result, VaultError> { + self.access.keys() + } + + /// Brings the cache up to date when the hub is reachable. Only a reachable hub's failure + /// is an error. + async fn sync(&self, keys: &Arc) -> Result<(), VaultError> { + let turn = self.access.slot().refresh_turn(keys).await; + self.keys()?; + if !turn.needed() { + return Ok(()); + } + let store = Store::new(Arc::clone(&self.ops), Arc::clone(keys)); + let result = match store.find(VAULT_KIND).await { + Ok(Some(at)) => self.pull(&store, &at).await, + Ok(None) => Ok(()), + Err(OpError::Hub(error)) if unreachable(&error) => Ok(()), + Err(error) => Err(vault_error(error)), + }; + if result.is_ok() { + turn.complete(); + } + result + } + + /// Reads the changes after the cached position and writes the cache through. + async fn pull(&self, store: &Store, at: &Located) -> Result<(), VaultError> { + let copy = self.access.read(|cache| { + cache + .and_then(|cache| cache.vault.clone()) + .filter(|vault| vault.collection_id == at.id()) + })?; + let mut vault = copy.unwrap_or_else(|| { + Arc::new(VaultCopy { + collection_id: at.id().to_owned(), + key_envelope: at.envelope().to_vec(), + cursor: 0, + objects: BTreeMap::new(), + }) + }); + let mut changed = false; + let mut budget: Option = None; + loop { + let batch = store.changes(at, vault.cursor).await.map_err(vault_error)?; + if !changed + && batch.changes.is_empty() + && !batch.more + && batch.head <= vault.cursor + && vault.key_envelope == at.envelope() + { + return Ok(()); + } + changed = true; + let envelope = at.envelope().to_vec(); + let merged = self + .access + .slot() + .cache_work() + .run(move || Ok(merge_vault(vault, budget, envelope, batch))) + .await + .map_err(|error| VaultError::Io(std::io::Error::other(error)))? + .map_err(vault_error)?; + vault = merged.0; + budget = Some(merged.1); + if !merged.2 { + break; + } + } + self.access.publish_vault(vault).await?; + Ok(()) + } + + /// Opens one cached object: `Ok(None)` when it is a tombstone or another kind, and an + /// error when it does not authenticate. + fn open( + keys: &OpenKeys, + collection: &str, + envelope: &[u8], + object_id: &str, + object: &ObjectCopy, + ) -> Result, VaultError> { + if object.deleted || object.kind != KIND { + return Ok(None); + } + let key = keys + .own_collection(collection, envelope) + .map_err(|_| VaultError::Authentication)?; + codec::open_sealed(&key, collection, object_id, object) + } + + fn cached(&self) -> Result>, VaultError> { + self.access + .read(|cache| cache.and_then(|cache| cache.vault.clone())) + } + + pub(super) async fn list(&self) -> Result, VaultError> { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + self.sync(&keys).await?; + let Some(vault) = self.cached()? else { + return Ok(Vec::new()); + }; + let mut listed = Vec::new(); + for (object_id, object) in &vault.objects { + if let Some(Decoded::Known(record)) = Self::open( + &keys, + &vault.collection_id, + &vault.key_envelope, + object_id, + object, + )? && let Ok(id) = object_id.parse::() + { + listed.push(CredentialInfo { + id, + label: record.label, + binding: record.binding, + }); + } + } + Ok(listed) + } + + pub(super) async fn get( + &self, + id: CredentialId, + binding: CredentialBinding, + ) -> Result { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + self.sync(&keys).await?; + let record = self.record(&keys, &id.to_string())?; + if !record.binding.answers(&binding) { + return Err(VaultError::Binding); + } + Ok(record.secret) + } + + /// Renames the record `id` and binds it as `binding` while its secret stays. + pub(super) async fn update( + &self, + id: CredentialId, + label: String, + binding: CredentialBinding, + ) -> Result<(), VaultError> { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + self.sync(&keys).await?; + let object_id = id.to_string(); + let record = self.record(&keys, &object_id)?; + if !record.binding.may_become(&binding) { + return Err(VaultError::Binding); + } + let plaintext = + codec::encode(&label, &binding, &record.secret).map_err(|_| VaultError::Format)?; + let store = self.store(&keys); + self.write_personal(&keys, &store, &object_id, &plaintext) + .await + } + + /// The readable record `object_id` of the cached vault. + fn record(&self, keys: &Arc, object_id: &str) -> Result { + let personal = self.access.read(|cache| { + let vault = cache?.vault.as_ref()?; + let object = vault + .objects + .get(object_id) + .filter(|object| !object.deleted)?; + Some(( + vault.collection_id.clone(), + vault.key_envelope.clone(), + object.clone(), + )) + })?; + let Some((collection, envelope, object)) = personal else { + return Err(VaultError::NotFound); + }; + match Self::open(keys, &collection, &envelope, object_id, &object)? { + Some(Decoded::Known(record)) => Ok(record), + Some(Decoded::Unrecognized) | None => Err(VaultError::NotFound), + } + } + + pub(super) async fn put( + &self, + id: Option, + label: String, + binding: CredentialBinding, + secret: Secret, + ) -> Result { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + let store = self.store(&keys); + let id = match id { + Some(id) => id, + None => { + CredentialId::from_bytes(random_bytes::<16>().map_err(|_| VaultError::Randomness)?) + } + }; + let object_id = id.to_string(); + let plaintext = codec::encode(&label, &binding, &secret).map_err(|_| VaultError::Format)?; + self.write_personal(&keys, &store, &object_id, &plaintext) + .await?; + Ok(id) + } + + /// Writes a record of the vault. + async fn write_personal( + &self, + keys: &Arc, + store: &Store, + object_id: &str, + plaintext: &[u8], + ) -> Result<(), VaultError> { + for attempt in 1..=WRITE_ATTEMPTS { + let at = store.ensure(VAULT_KIND).await.map_err(vault_error)?; + self.pull(store, &at).await?; + let expected = self.expected(keys, object_id, true)?; + match store.put(&at, KIND, object_id, plaintext, expected).await { + Ok(_) => return self.pull(store, &at).await, + Err(OpError::Conflict) if attempt < WRITE_ATTEMPTS => {} + Err(error) => return Err(vault_error(error)), + } + } + Err(VaultError::Conflict) + } + + pub(super) async fn delete(&self, id: CredentialId) -> Result<(), VaultError> { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + let store = self.store(&keys); + let object_id = id.to_string(); + self.remove_personal(&keys, &store, &object_id).await + } + + /// Deletes a record of the vault. + async fn remove_personal( + &self, + keys: &Arc, + store: &Store, + object_id: &str, + ) -> Result<(), VaultError> { + for attempt in 1..=WRITE_ATTEMPTS { + let at = store + .find(VAULT_KIND) + .await + .map_err(vault_error)? + .ok_or(VaultError::NotFound)?; + self.pull(store, &at).await?; + let expected = self.expected(keys, object_id, false)?; + match store.remove(&at, object_id, expected).await { + Ok(_) => return self.pull(store, &at).await, + Err(OpError::Conflict) if attempt < WRITE_ATTEMPTS => {} + Err(error) => return Err(vault_error(error)), + } + } + Err(VaultError::Conflict) + } + + /// The version a write must expect for `object_id`. A record that is not ours to change + /// (it does not decode) is [`VaultError::Format`] when writing and not found when deleting. + fn expected( + &self, + keys: &OpenKeys, + object_id: &str, + creating: bool, + ) -> Result { + let missing = if creating { + Ok(0) + } else { + Err(VaultError::NotFound) + }; + let Some(vault) = self.cached()? else { + return missing; + }; + let Some(object) = vault.objects.get(object_id) else { + return missing; + }; + if object.deleted { + return if creating { + Ok(object.version) + } else { + Err(VaultError::NotFound) + }; + } + match Self::open( + keys, + &vault.collection_id, + &vault.key_envelope, + object_id, + object, + )? { + Some(Decoded::Known(_)) => Ok(object.version), + _ if creating => Err(VaultError::Format), + _ => Err(VaultError::NotFound), + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/ledger/access.rs b/crates/hub-io/src/vault/ledger/access.rs new file mode 100644 index 00000000..7086b0f2 --- /dev/null +++ b/crates/hub-io/src/vault/ledger/access.rs @@ -0,0 +1,113 @@ +//! The keys and cache a ledger may use; captured operations never follow a new login. +use std::sync::Arc; + +use nocterm_hubs_model::source_identity::LocalAccountSource; +use nocterm_vault::VaultError; + +use crate::account::{OpenKeys, Slot, source::CapturedAccount}; +use crate::cache::{AccountCache, VaultCopy}; + +pub(super) struct LedgerAccess { + slot: Arc, + capture: Option>, +} + +impl LedgerAccess { + pub(super) async fn operation(&self) -> Result { + self.slot + .cache_work() + .operation() + .await + .map_err(|error| VaultError::Io(std::io::Error::other(error))) + } + + pub(super) fn slot(&self) -> &Arc { + &self.slot + } + #[cfg(test)] + pub(super) fn live(slot: Arc) -> Self { + Self { + slot, + capture: None, + } + } + + pub(super) fn captured(slot: Arc, capture: Arc) -> Self { + Self { + slot, + capture: Some(capture), + } + } + + pub(super) fn keys(&self) -> Result, VaultError> { + match &self.capture { + Some(capture) => capture.keys().map_err(|_| VaultError::Stale), + None => self.slot.open().ok_or(VaultError::Locked), + } + } + + pub(super) fn read( + &self, + read: impl FnOnce(Option<&AccountCache>) -> R, + ) -> Result { + match &self.capture { + Some(capture) => { + let keys = self.keys()?; + let result = self.slot.read_cache_if(&keys, |cache| read(Some(cache)))?; + capture.check_current().map_err(|_| VaultError::Stale)?; + Ok(result) + } + None => Ok(self.slot.read_cache(read)), + } + } + + pub(super) async fn publish_vault(&self, vault: Arc) -> Result<(), VaultError> { + let _turn = self.slot.cache_turn().await; + let keys = self.keys()?; + loop { + let original = self.slot.cache_snapshot(); + let Some(mut next) = original.as_deref().cloned() else { + return Err(VaultError::Stale); + }; + if !next.accept_shared_vault(vault.clone()) { + return Ok(()); + } + let next = Arc::new(next); + let draft = self + .slot + .stage_cache(original.clone(), next.as_ref().clone()) + .await; + let result = draft.and_then(|draft| match &self.capture { + Some(capture) => capture.with_current(|_| self.slot.commit_cache_if(&keys, draft)), + None => self.slot.commit_cache_if(&keys, draft), + }); + match result { + Ok(Some(committed)) => { + self.slot.finish_cache(committed).await; + return Ok(()); + } + Ok(None) => {} + Err(error) if cache_write_failed(&error) => { + let accepted = match &self.capture { + Some(capture) => capture.with_current(|_| { + self.slot.accept_memory_cache_if(&keys, &original, next) + }), + None => self.slot.accept_memory_cache_if(&keys, &original, next), + } + .map_err(super::super::native_failure)?; + if accepted { + tracing::warn!(error = %nocterm_core::error::chain(&error), "the vault is current in memory; its offline cache could not be written"); + return Ok(()); + } + } + Err(error) => return Err(super::super::native_failure(error)), + } + } + } +} + +fn cache_write_failed(error: &nocterm_hubs_model::HubError) -> bool { + matches!(error, nocterm_hubs_model::HubError::Credentials { cause } + if matches!(cause.downcast_ref::(), + Some(crate::cache::CacheError::Write(_) | crate::cache::CacheError::Prepare(_)))) +} diff --git a/crates/hub-io/src/vault/ledger/tests.rs b/crates/hub-io/src/vault/ledger/tests.rs new file mode 100644 index 00000000..a01062a0 --- /dev/null +++ b/crates/hub-io/src/vault/ledger/tests.rs @@ -0,0 +1,317 @@ +use std::path::PathBuf; +use std::sync::Arc; + +use nocterm_session::Target; + +use super::codec::KIND; +use super::*; +use crate::account::testing::{second_device, signed_in}; +use crate::store::VAULT_KIND; +use crate::store::memory::MemoryHub; + +struct Device { + ledger: Ledger, + slot: Arc, + _dir: tempfile::TempDir, +} + +fn device(hub: &Arc) -> (Device, nocterm_hub_proto::v2::Keyset) { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + let ledger = Ledger::new(Arc::clone(&slot), hub.clone()); + ( + Device { + ledger, + slot, + _dir: dir, + }, + record, + ) +} + +fn password_binding() -> CredentialBinding { + CredentialBinding::password([Target::new("ann", "example.org", 22)]) +} + +#[tokio::test] +async fn a_saved_credential_is_listed_and_read_back() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let listed = device.ledger.list().await.unwrap(); + assert_eq!(listed.len(), 1); + assert_eq!((listed[0].id, listed[0].label.as_str()), (id, "Work")); + let secret = device.ledger.get(id, password_binding()).await.unwrap(); + assert_eq!(secret.expose(), "pw"); +} + +#[tokio::test] +async fn a_credential_does_not_answer_another_request() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let other = CredentialBinding::KeyPassphrase { + path: PathBuf::from("/k"), + }; + assert!(matches!( + device.ledger.get(id, other).await, + Err(VaultError::Binding) + )); +} + +#[tokio::test] +async fn a_private_key_record_round_trips() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let binding = CredentialBinding::PrivateKey { + public_key: "ssh-ed25519 AAAA".into(), + }; + let id = device + .ledger + .put( + None, + "key".into(), + binding.clone(), + Secret::new("PRIVATE KEY TEXT"), + ) + .await + .unwrap(); + let secret = device.ledger.get(id, binding).await.unwrap(); + assert_eq!(secret.expose(), "PRIVATE KEY TEXT"); +} + +#[tokio::test] +async fn the_hub_holds_only_ciphertext() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + device + .ledger + .put( + None, + "Work label".into(), + password_binding(), + Secret::new("hunter2hunter2"), + ) + .await + .unwrap(); + let collection = hub + .collections(crate::store::VAULT_KIND) + .await + .unwrap() + .remove(0); + let objects = hub.objects(&collection.collection_id); + assert_eq!(objects.len(), 1); + let text = String::from_utf8_lossy(&objects[0].ciphertext).into_owned(); + assert!(!text.contains("hunter2") && !text.contains("Work label")); +} + +#[tokio::test] +async fn credentials_stay_readable_offline_but_cannot_be_written() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + hub.set_offline(true); + assert_eq!(device.ledger.list().await.unwrap().len(), 1); + assert_eq!( + device + .ledger + .get(id, password_binding()) + .await + .unwrap() + .expose(), + "pw" + ); + assert!(matches!( + device + .ledger + .put(None, "New".into(), password_binding(), Secret::new("x")) + .await, + Err(VaultError::Offline) + )); + assert!(matches!( + device.ledger.delete(id).await, + Err(VaultError::Offline) + )); +} + +#[tokio::test] +async fn a_credential_saved_on_one_device_is_listed_on_another() { + let hub = Arc::new(MemoryHub::default()); + let (first, record) = device(&hub); + let id = first + .ledger + .put(None, "Shared".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot) = second_device(dir.path(), &record); + let second = Ledger::new(slot, hub.clone()); + let listed = second.list().await.unwrap(); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0].id, id); + assert_eq!( + second.get(id, password_binding()).await.unwrap().expose(), + "pw" + ); +} + +#[tokio::test] +async fn deleting_leaves_a_tombstone_and_the_list_without_the_credential() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + device.ledger.delete(id).await.unwrap(); + assert!(device.ledger.list().await.unwrap().is_empty()); + assert!(matches!( + device.ledger.get(id, password_binding()).await, + Err(VaultError::NotFound) + )); + assert!(matches!( + device.ledger.delete(id).await, + Err(VaultError::NotFound) + )); +} + +#[tokio::test] +async fn a_locked_vault_reads_and_writes_nothing() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + device.slot.lock(); + assert!(matches!( + device.ledger.list().await, + Err(VaultError::Locked) + )); + assert!(matches!( + device + .ledger + .put(None, "x".into(), password_binding(), Secret::new("x")) + .await, + Err(VaultError::Locked) + )); +} + +#[tokio::test] +async fn a_record_the_hub_altered_is_an_error_not_a_silent_skip() { + let hub = Arc::new(MemoryHub::default()); + let (device, record) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let collection = hub + .collections(crate::store::VAULT_KIND) + .await + .unwrap() + .remove(0); + hub.tamper(&collection.collection_id, &id.to_string(), vec![9; 64]); + // A device that already holds the record keeps what it read; another reads the altered one. + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot) = second_device(dir.path(), &record); + let other = Ledger::new(slot, hub.clone()); + assert!(matches!( + other.list().await, + Err(VaultError::Authentication) + )); +} + +/// Writes an object the way a newer client would: sealed with the right key but holding a +/// binding this client does not know. +async fn write_foreign(device: &Device, hub: &Arc, object_id: &str, text: &str) { + let keys = device.slot.open().unwrap(); + let store = Store::new(hub.clone(), keys); + let at = store.ensure(VAULT_KIND).await.unwrap(); + store + .put(&at, KIND, object_id, text.as_bytes(), 0) + .await + .unwrap(); +} + +#[tokio::test] +async fn a_record_of_an_unknown_kind_is_hidden_and_never_changed() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let foreign: CredentialId = "00000000000000000000000000000042".parse().unwrap(); + let text = + r#"{"v":1,"label":"token","binding":{"kind":"hardware_token","slot":1},"secret":"s"}"#; + write_foreign(&device, &hub, &foreign.to_string(), text).await; + let before = { + let collection = hub.collections(VAULT_KIND).await.unwrap().remove(0); + hub.objects(&collection.collection_id) + }; + + assert!(device.ledger.list().await.unwrap().is_empty()); + assert!(matches!( + device.ledger.get(foreign, password_binding()).await, + Err(VaultError::NotFound) + )); + assert!(matches!( + device.ledger.delete(foreign).await, + Err(VaultError::NotFound) + )); + assert!(matches!( + device + .ledger + .put( + Some(foreign), + "mine".into(), + password_binding(), + Secret::new("x") + ) + .await, + Err(VaultError::Format) + )); + // Other credentials still work beside it. + device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + assert_eq!(device.ledger.list().await.unwrap().len(), 1); + + let collection = hub.collections(VAULT_KIND).await.unwrap().remove(0); + let after = hub.objects(&collection.collection_id); + let kept = after + .iter() + .find(|object| object.object_id == foreign.to_string()) + .unwrap(); + assert_eq!( + kept, + before + .iter() + .find(|o| o.object_id == foreign.to_string()) + .unwrap() + ); + assert!(!kept.deleted); +} + +#[tokio::test] +async fn a_record_of_a_newer_format_is_hidden_and_never_changed() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let foreign: CredentialId = "00000000000000000000000000000043".parse().unwrap(); + let text = + r#"{"v":2,"label":"x","binding":{"kind":"private_key","public_key":"k"},"secret":"s"}"#; + write_foreign(&device, &hub, &foreign.to_string(), text).await; + assert!(device.ledger.list().await.unwrap().is_empty()); + assert!(matches!( + device.ledger.delete(foreign).await, + Err(VaultError::NotFound) + )); +} diff --git a/crates/hub-io/src/vault/snapshot_budget.rs b/crates/hub-io/src/vault/snapshot_budget.rs new file mode 100644 index 00000000..10261d2c --- /dev/null +++ b/crates/hub-io/src/vault/snapshot_budget.rs @@ -0,0 +1,126 @@ +//! Bounds one operation's sealed snapshots before retaining another object or page. +use std::{collections::BTreeMap, sync::Arc}; + +use nocterm_hub_proto::v2; + +use crate::{ + cache::{ObjectCopy, VaultCopy}, + classify::remote, + store::OpError, +}; + +pub(super) fn merge_vault( + mut vault: Arc, + budget: Option, + envelope: Vec, + batch: v2::ChangeBatch, +) -> Result<(Arc, SnapshotBudget, bool), OpError> { + let mut budget = match budget { + Some(budget) => budget, + None => { + let mut budget = SnapshotBudget::default(); + budget.add(&vault.objects)?; + budget + } + }; + let next = Arc::make_mut(&mut vault); + next.key_envelope = envelope; + let more = budget.apply(&mut next.objects, &mut next.cursor, batch)?; + Ok((vault, budget, more)) +} + +const MAX_BYTES: usize = 24 * 1024 * 1024; +const MAX_OBJECTS: usize = 32_768; + +const MAX_PAGES: usize = 1_024; + +#[derive(Debug, thiserror::Error)] +enum SnapshotError { + #[error("the vault snapshot exceeds its memory or page limit")] + Capacity, + #[error("the vault change page does not advance its cursor")] + Progress, +} + +#[derive(Default)] +pub(super) struct SnapshotBudget { + bytes: usize, + objects: usize, + pages: usize, +} + +impl SnapshotBudget { + pub(super) fn add(&mut self, objects: &BTreeMap) -> Result<(), OpError> { + for (id, object) in objects { + self.admit(0, charge(id, object), true)?; + } + Ok(()) + } + + fn admit(&mut self, old: usize, next: usize, fresh: bool) -> Result<(), OpError> { + let bytes = self.bytes.saturating_sub(old).saturating_add(next); + let objects = self.objects.saturating_add(usize::from(fresh)); + if bytes > MAX_BYTES || objects > MAX_OBJECTS { + return Err(OpError::Hub(remote(SnapshotError::Capacity))); + } + self.bytes = bytes; + self.objects = objects; + Ok(()) + } + + pub(super) fn apply( + &mut self, + objects: &mut BTreeMap, + cursor: &mut u64, + batch: v2::ChangeBatch, + ) -> Result { + self.pages += 1; + if self.pages > MAX_PAGES { + return Err(OpError::Hub(remote(SnapshotError::Capacity))); + } + let before = *cursor; + for object in batch.changes { + if object.stream_version <= before || object.stream_version > batch.head { + return Err(OpError::Hub(remote(SnapshotError::Progress))); + } + *cursor = (*cursor).max(object.stream_version); + if objects + .get(&object.object_id) + .is_some_and(|held| held.version > object.version) + { + continue; + } + let next = ObjectCopy { + version: object.version, + kind: object.kind, + ciphertext: object.ciphertext, + deleted: object.deleted, + }; + let old = objects + .get_key_value(&object.object_id) + .map(|(id, old)| charge(id, old)); + self.admit( + old.unwrap_or(0), + charge(&object.object_id, &next), + old.is_none(), + )?; + objects.insert(object.object_id, next); + } + if batch.more && *cursor == before { + return Err(OpError::Hub(remote(SnapshotError::Progress))); + } + if !batch.more { + *cursor = (*cursor).max(batch.head); + } + Ok(batch.more) + } +} + +fn charge(id: &String, object: &ObjectCopy) -> usize { + // Includes tree/node metadata, JSON escaping and base64 expansion, as well + // as retained capacities. This leaves file framing/keyset headroom below 32 MiB. + 256usize + .saturating_add(id.capacity().saturating_mul(6)) + .saturating_add(object.kind.capacity().saturating_mul(6)) + .saturating_add(object.ciphertext.capacity().saturating_mul(4).div_ceil(3)) +} diff --git a/crates/hub-io/src/vault/tests.rs b/crates/hub-io/src/vault/tests.rs new file mode 100644 index 00000000..7d1a06c4 --- /dev/null +++ b/crates/hub-io/src/vault/tests.rs @@ -0,0 +1,135 @@ +use std::path::PathBuf; +use std::sync::Arc; +use std::time::Duration; + +use nocterm_session::Target; + +use super::*; +use crate::account::testing::NativeVault; +use crate::account::testing::{PASSWORD, signed_in}; +use crate::store::memory::MemoryHub; +mod refresh; +mod source_binding; + +fn vault() -> (NativeVault, Arc, tempfile::TempDir) { + let hub = Arc::new(MemoryHub::default()); + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + hub.set_keyset(Some(record)); + (NativeVault::new(slot, hub.clone()), hub, dir) +} + +fn binding() -> CredentialBinding { + CredentialBinding::password([Target::new("ann", "example.org", 22)]) +} + +#[tokio::test(flavor = "multi_thread")] +async fn the_vault_reports_its_status_as_the_account_keys_change() { + let (vault, _hub, _dir) = vault(); + assert_eq!(vault.status(), VaultStatus::Unlocked); + let seen = vault.revision(); + vault.lock(); + assert_eq!(vault.status(), VaultStatus::Locked); + assert!(vault.revision() > seen); + assert!(vault.exists() && !vault.is_unlocked()); + vault.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert_eq!(vault.status(), VaultStatus::Unlocked); +} + +#[tokio::test(flavor = "multi_thread")] +async fn waiting_for_a_change_ends_when_the_vault_locks() { + let (vault, _hub, _dir) = vault(); + let seen = vault.revision(); + let changed = vault.changed(seen); + vault.lock(); + let now = tokio::time::timeout(Duration::from_secs(5), changed) + .await + .unwrap(); + assert!(now > seen); +} + +#[tokio::test(flavor = "multi_thread")] +async fn waiting_for_unlock_ends_when_the_password_is_accepted() { + let (vault, _hub, _dir) = vault(); + vault.lock(); + let waiting = vault.wait_unlocked(); + vault.unlock(Secret::new(PASSWORD)).await.unwrap(); + tokio::time::timeout(Duration::from_secs(5), waiting) + .await + .unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_vault_on_a_hub_has_no_password_of_its_own_to_create_or_change() { + let (vault, _hub, _dir) = vault(); + assert!(matches!( + vault.create(Secret::new("a password")).await, + Err(VaultError::Unsupported) + )); + assert!(matches!( + vault.change_password(Secret::new("a password")).await, + Err(VaultError::Unsupported) + )); + assert!(matches!( + vault.enable_device_unlock().await, + Err(VaultError::Unsupported) + )); + assert!(matches!( + vault.unlock_with_device().await, + Err(VaultError::Unsupported) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn credentials_are_saved_and_read_through_the_port() { + let (vault, _hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + assert_eq!(vault.list().await.unwrap().len(), 1); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); + vault.delete(id).await.unwrap(); + assert!(vault.list().await.unwrap().is_empty()); +} + +#[tokio::test(flavor = "multi_thread")] +async fn offline_a_write_is_refused_as_offline() { + let (vault, hub, _dir) = vault(); + hub.set_offline(true); + assert!(matches!( + vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await, + Err(VaultError::Offline) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_locked_vault_does_not_answer() { + let (vault, _hub, _dir) = vault(); + vault.lock(); + assert!(matches!(vault.list().await, Err(VaultError::Locked))); + let other = CredentialBinding::KeyPassphrase { + path: PathBuf::from("/k"), + }; + assert!(matches!( + vault.get(CredentialId::from_bytes([1; 16]), other).await, + Err(VaultError::Locked) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn the_vault_locks_itself_after_a_quiet_period() { + let (vault, _hub, _dir) = vault(); + vault.set_auto_lock(Duration::from_millis(60)); + assert_eq!(vault.status(), VaultStatus::Unlocked); + tokio::time::timeout(Duration::from_secs(5), async { + while vault.status() == VaultStatus::Unlocked { + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await + .unwrap(); + assert_eq!(vault.status(), VaultStatus::Locked); +} diff --git a/crates/hub-io/src/vault/tests/refresh.rs b/crates/hub-io/src/vault/tests/refresh.rs new file mode 100644 index 00000000..71158368 --- /dev/null +++ b/crates/hub-io/src/vault/tests/refresh.rs @@ -0,0 +1,212 @@ +//! Refresh ordering and unchanged-cache behavior through the actual native vault port. +use super::*; +use crate::store::memory::StoreBoundary; +use nocterm_hub_proto::v2; + +#[tokio::test(flavor = "multi_thread")] +async fn an_unordered_change_page_after_the_requested_cursor_is_accepted() { + let (vault, hub, _dir) = vault(); + let first = vault + .put(None, "First".into(), binding(), Secret::new("one")) + .await + .unwrap(); + let second = vault + .put(None, "Second".into(), binding(), Secret::new("two")) + .await + .unwrap(); + let collection = hub + .collections_of_kind(crate::store::VAULT_KIND) + .pop() + .unwrap(); + let mut changes = hub.objects(&collection); + assert_eq!(changes.len(), 2); + changes[0].stream_version = 4; + changes[1].stream_version = 3; + hub.script_changes(vec![v2::ChangeBatch { + changes, + head: 4, + more: false, + }]); + assert_eq!(vault.list().await.unwrap().len(), 2); + hub.set_offline(true); + assert_eq!(vault.get(first, binding()).await.unwrap().expose(), "one"); + assert_eq!(vault.get(second, binding()).await.unwrap().expose(), "two"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn unordered_changes_for_one_object_keep_its_newest_version() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("old")) + .await + .unwrap(); + let collection = hub + .collections_of_kind(crate::store::VAULT_KIND) + .pop() + .unwrap(); + let mut old = hub.objects(&collection).pop().unwrap(); + vault + .put(Some(id), "Changed".into(), binding(), Secret::new("new")) + .await + .unwrap(); + let mut new = hub.objects(&collection).pop().unwrap(); + new.stream_version = 4; + new.version = 4; + old.stream_version = 3; + old.version = 3; + hub.script_changes(vec![v2::ChangeBatch { + changes: vec![new, old], + head: 4, + more: false, + }]); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "new"); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "new"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_remote_write_remains_successful_when_only_its_offline_copy_cannot_be_published() { + let (vault, hub, dir) = vault(); + let first = vault + .put(None, "First".into(), binding(), Secret::new("one")) + .await + .unwrap(); + let path = std::fs::read_dir(dir.path()) + .unwrap() + .map(|entry| entry.unwrap().path()) + .find(|path| { + path.extension() + .is_some_and(|extension| extension == "cache") + }) + .unwrap(); + std::fs::remove_file(&path).unwrap(); + std::fs::create_dir(&path).unwrap(); + let second = vault + .put(None, "Second".into(), binding(), Secret::new("two")) + .await + .unwrap(); + hub.set_offline(true); + assert_eq!(vault.get(first, binding()).await.unwrap().expose(), "one"); + assert_eq!(vault.get(second, binding()).await.unwrap().expose(), "two"); + assert_eq!(vault.list().await.unwrap().len(), 2); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_nonadvancing_change_page_is_refused_once_and_preserves_offline_credentials() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let before = hub.change_calls(); + hub.script_changes(vec![v2::ChangeBatch { + changes: Vec::new(), + head: 100, + more: true, + }]); + assert!(matches!(vault.list().await, Err(VaultError::Io(_)))); + assert_eq!(hub.change_calls(), before + 1); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn oversized_snapshot_content_is_refused_before_cache_publication() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + hub.script_changes(vec![v2::ChangeBatch { + changes: vec![v2::StoredObject { + object_id: "new-format".into(), + kind: "future".into(), + stream_version: 2, + version: 1, + ciphertext: vec![0; 19 * 1024 * 1024], + ..Default::default() + }], + head: 2, + more: false, + }]); + assert!(matches!(vault.list().await, Err(VaultError::Io(_)))); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_progressing_but_unending_change_stream_has_a_finite_page_budget() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let before = hub.change_calls(); + hub.script_changes( + (2..=1_026) + .map(|cursor| v2::ChangeBatch { + changes: vec![v2::StoredObject { + object_id: "future-object".into(), + kind: "future".into(), + stream_version: cursor, + version: cursor, + deleted: true, + ..Default::default() + }], + head: cursor, + more: true, + }) + .collect(), + ); + assert!(matches!(vault.list().await, Err(VaultError::Io(_)))); + assert_eq!(hub.change_calls(), before + 1_025); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_refresh_started_before_deletion_cannot_restore_the_deleted_offline_credential() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let (entered, release) = hub.pause_next(StoreBoundary::ChangesReady); + let delayed = vault.get(id, binding()); + tokio::time::timeout(Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + vault.delete(id).await.unwrap(); + release.release(); + let _ = delayed.await; + hub.set_offline(true); + assert!(matches!( + vault.get(id, binding()).await, + Err(VaultError::NotFound) + )); + assert!(vault.list().await.unwrap().is_empty()); +} + +#[cfg(unix)] +#[tokio::test(flavor = "multi_thread")] +async fn reading_an_unchanged_vault_does_not_replace_its_cache_file() { + use std::os::unix::fs::MetadataExt as _; + let (vault, _hub, dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let path = std::fs::read_dir(dir.path()) + .unwrap() + .map(|entry| entry.unwrap().path()) + .find(|path| { + path.extension() + .is_some_and(|extension| extension == "cache") + }) + .unwrap(); + let original = std::fs::File::open(&path).unwrap(); + let inode = original.metadata().unwrap().ino(); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); + assert_eq!(vault.list().await.unwrap().len(), 1); + assert_eq!(std::fs::metadata(path).unwrap().ino(), inode); +} diff --git a/crates/hub-io/src/vault/tests/source_binding.rs b/crates/hub-io/src/vault/tests/source_binding.rs new file mode 100644 index 00000000..3d0237d5 --- /dev/null +++ b/crates/hub-io/src/vault/tests/source_binding.rs @@ -0,0 +1,230 @@ +use super::*; +use crate::account::testing::NativeAccounts; +use crate::store::memory::StoreBoundary; + +#[tokio::test] +async fn a_vault_read_queued_before_login_changes_cannot_read_the_replacement_account() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let pending = fixture.vault().get(id, request_binding()); + fixture.login(1); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn queued_vault_writes_and_deletes_refuse_a_replacement_login() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let put = fixture + .vault() + .put(None, "A".into(), request_binding(), Secret::new("A")); + let delete = fixture.vault().delete(id); + fixture.login(1); + assert!(matches!(put.await, Err(VaultError::Stale))); + assert!(matches!(delete.await, Err(VaultError::Stale))); + assert_eq!(fixture.count(0).await, 1); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn late_vault_cache_publication_does_not_install_a_records_in_b() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let pending = fixture.vault().get(id, request_binding()); + entered.wait().await; + fixture.login(1); + let b = fixture.seed().await; + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + fixture.hub(1).set_offline(true); + assert_eq!( + fixture + .vault() + .get(b, request_binding()) + .await + .unwrap() + .expose(), + "secret A" + ); + assert!(matches!( + fixture.vault().get(id, request_binding()).await, + Err(VaultError::NotFound) + )); +} + +#[tokio::test] +async fn completed_vault_work_is_rechecked_before_its_caller_first_polls() { + let fixture = NativeAccounts::new(); + let pending = fixture + .vault() + .put(None, "A".into(), request_binding(), Secret::new("A")); + while fixture.count(0).await == 0 { + tokio::task::yield_now().await; + } + // The native write/pull has no pending boundary, so it finishes on the same poll. + tokio::task::yield_now().await; + fixture.login(1); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn an_unlock_response_cannot_install_keys_after_a_replacement_login() { + let fixture = NativeAccounts::new(); + fixture.vault().lock(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::KeysetReady); + let pending = fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)); + entered.wait().await; + fixture.login(1); + let current = fixture.source(); + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert!(current.check_current().is_ok()); + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); +} + +#[tokio::test] +async fn an_explicit_lock_cancels_an_unlock_already_waiting_for_its_record() { + let fixture = NativeAccounts::new(); + fixture.vault().lock(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::KeysetReady); + let pending = fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)); + entered.wait().await; + fixture.vault().lock(); + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert_eq!(fixture.vault().status(), VaultStatus::Locked); + fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)) + .await + .unwrap(); +} + +#[tokio::test] +async fn an_unlock_completed_before_caller_poll_never_publishes_success_for_b() { + let fixture = NativeAccounts::new(); + fixture.vault().lock(); + let revision = fixture.vault().revision(); + let pending = fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)); + fixture.vault().changed(revision).await; + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); + fixture.login(1); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert!(fixture.source().check_current().is_ok()); +} + +#[tokio::test] +async fn same_account_relogin_and_a_b_a_never_revive_a_pending_vault_read() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let old = fixture.vault().get(id, request_binding()); + fixture.login(0); + assert!(matches!(old.await, Err(VaultError::Stale))); + let old = fixture.vault().get(id, request_binding()); + fixture.login(1); + fixture.login(0); + assert!(matches!(old.await, Err(VaultError::Stale))); + assert_eq!( + fixture + .vault() + .get(id, request_binding()) + .await + .unwrap() + .expose(), + "secret A" + ); +} + +#[tokio::test(start_paused = true)] +async fn an_old_account_idle_timer_cannot_lock_a_new_account_without_activity() { + let fixture = NativeAccounts::new(); + fixture.vault().set_auto_lock(Duration::from_secs(100)); + tokio::task::yield_now().await; + fixture.login(1); + tokio::time::advance(Duration::from_secs(101)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); +} + +#[tokio::test(start_paused = true)] +async fn a_late_a_completion_cannot_extend_or_cancel_b_idle_deadline() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + fixture.vault().set_auto_lock(Duration::from_secs(100)); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let old = fixture.vault().get(id, request_binding()); + entered.wait().await; + fixture.login(1); + fixture.seed().await; + tokio::task::yield_now().await; + tokio::time::advance(Duration::from_secs(40)).await; + release.release(); + assert!(matches!(old.await, Err(VaultError::Stale))); + tokio::time::advance(Duration::from_secs(60)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Locked); +} + +#[tokio::test(start_paused = true)] +async fn captured_terminal_credential_activity_refreshes_only_its_own_idle_timer() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + fixture.vault().set_auto_lock(Duration::from_secs(100)); + tokio::task::yield_now().await; + let store = fixture.store(); + tokio::time::advance(Duration::from_secs(40)).await; + store + .get( + id, + &nocterm_session::SecretRequest::Password { + target: Target::new("ann", "example.test", 22), + retry: false, + }, + ) + .await + .unwrap(); + tokio::task::yield_now().await; + tokio::time::advance(Duration::from_secs(60)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); + tokio::time::advance(Duration::from_secs(40)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Locked); +} + +fn request_binding() -> CredentialBinding { + CredentialBinding::password([Target::new("ann", "example.test", 22)]) +} + +#[tokio::test] +async fn a_delayed_original_revocation_cannot_discard_a_replacement_or_its_idle_owner() { + for replacements in [vec![0], vec![1], vec![1, 0]] { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let (entered, release) = fixture.hub(0).pause_next_error( + StoreBoundary::ChangesReady, + nocterm_hubs_model::HubError::Revoked, + ); + let pending = fixture.vault().get(id, request_binding()); + entered.wait().await; + for index in replacements { + fixture.login(index); + } + let current = fixture.source(); + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert!(current.check_current().is_ok()); + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); + fixture.seed().await; + assert!(current.check_current().is_ok()); + } +} diff --git a/crates/hub-io/src/vault/unlock.rs b/crates/hub-io/src/vault/unlock.rs new file mode 100644 index 00000000..fc2ab700 --- /dev/null +++ b/crates/hub-io/src/vault/unlock.rs @@ -0,0 +1,112 @@ +//! Unlocking the vault: the typed password opens the keyset, from the hub's record when the +//! hub answers and from the cached record when it does not. + +use std::sync::Arc; + +use nocterm_hub_client::{Keyset, KeysetError, UnlockMethod as KeysetUnlock}; +use nocterm_hub_proto::v2; +use nocterm_keys::{LoginError, LoginSecrets, UnlockKey}; +use nocterm_session::Secret; +use nocterm_vault::VaultError; + +use super::ledger::vault_error; +use super::native_failure as failure; +use crate::account::{OpenKeys, SlotReceipt, operations::MembershipWork}; +use crate::cache::{AccountCache, KeysetCopy, UnlockMethod}; +use crate::classify::unreachable; +use crate::store::OpError; + +pub(super) struct Unlocker { + work: MembershipWork, +} + +impl Unlocker { + pub(super) fn new(work: MembershipWork) -> Self { + Self { work } + } + + pub(super) async fn unlock(&self, password: Secret) -> Result { + self.work.check().map_err(failure)?; + let slot = self.work.slot().map_err(failure)?; + let member = self.work.membership(); + let subject = member.subject().map_err(failure)?; + if slot.open_for(&subject).is_ok() { + return Ok(self.work.receipt().clone()); + } + let mut cached = member + .with_current(|| slot.cache_for(&subject, self.work.receipt())) + .map_err(failure)?; + let record = self.record(&cached).await?; + let unlock = derive(&cached, &record, password).await?; + self.work.check().map_err(failure)?; + let keyset = + Keyset::open(&record, &cached.account_id, &unlock).map_err(|error| match error { + KeysetError::WrongUnlockKey | KeysetError::Damaged(_) => VaultError::Authentication, + _ => VaultError::Format, + })?; + cached.keyset = KeysetCopy::of(&record); + let _turn = slot.cache_turn().await; + self.work.check().map_err(failure)?; + let original = slot.cache_snapshot(); + if let Some(latest) = original.as_deref() { + cached.vault = latest.vault.clone(); + cached.recovery_unconfirmed = latest.recovery_unconfirmed; + } + let keys = Arc::new(OpenKeys::new(keyset)); + let draft = slot.stage_cache(original, cached).await.map_err(failure)?; + let (receipt, committed) = member + .with_current(|| slot.commit_install(&subject, self.work.receipt(), keys, draft)) + .map_err(failure)? + .ok_or(VaultError::Stale)?; + slot.finish_cache(committed).await; + slot.notice().raise(); + Ok(receipt) + } + + /// The keyset record to open: the hub's when it answers, else the cached copy. + async fn record(&self, cached: &AccountCache) -> Result { + match self.work.ops().keyset().await { + Ok(Some(record)) => Ok(record), + Ok(None) => Ok(cached.keyset.record()), + Err(OpError::Hub(error)) if unreachable(&error) => Ok(cached.keyset.record()), + Err(error) => Err(vault_error(error)), + } + } +} + +/// The unlock key of the typed password, derived the way the keyset records ([`KeysetUnlock`]), +/// not the way the person happened to sign in. Key derivation is slow and memory hard, so it +/// runs on a thread of its own. +async fn derive( + cached: &AccountCache, + record: &v2::Keyset, + password: Secret, +) -> Result { + let method = KeysetUnlock::of(record).map_err(|_| VaultError::Format)?; + let hub_uid = cached.hub_uid.clone(); + // Only a login password needs the user name, which the cache kept from the sign-in. + let username = match &cached.method { + UnlockMethod::Login { username } => Some(username.clone()), + UnlockMethod::Passphrase => None, + }; + let derived = tokio::task::spawn_blocking(move || match (method, username) { + (KeysetUnlock::LoginPassword, Some(username)) => { + LoginSecrets::derive(password.expose(), &hub_uid, &username) + .map(LoginSecrets::into_unlock) + } + (KeysetUnlock::LoginPassword, None) => Err(LoginError::EmptyUsername), + (KeysetUnlock::VaultPassword(kdf), _) => { + UnlockKey::from_vault_password(password.expose(), &kdf) + } + }) + .await + .map_err(|_| VaultError::Cancelled)?; + derived.map_err(|error| match error { + LoginError::PasswordTooShort { .. } => VaultError::Password, + LoginError::EmptyUsername => VaultError::Format, + LoginError::Key(_) => VaultError::Randomness, + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/unlock/tests.rs b/crates/hub-io/src/vault/unlock/tests.rs new file mode 100644 index 00000000..f423b5b6 --- /dev/null +++ b/crates/hub-io/src/vault/unlock/tests.rs @@ -0,0 +1,88 @@ +use std::sync::Arc; + +use super::*; +use crate::account::{ + Slot, + operations::AccountOperations, + testing::{PASSWORD, native_operations, record, signed_in, unlock}, +}; +use crate::store::memory::MemoryHub; + +fn locked_device( + hub: &Arc, +) -> ( + Unlocker, + Arc, + tempfile::TempDir, + Arc, +) { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + hub.set_keyset(Some(record)); + let operations = native_operations(&slot, hub.clone()); + slot.lock(); + let unlocker = Unlocker::new(operations.membership().unwrap()); + (unlocker, slot, dir, operations) +} + +#[tokio::test] +async fn the_password_unlocks_with_the_hub_reachable() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + unlocker.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert!(slot.open().is_some()); +} + +#[tokio::test] +async fn the_password_unlocks_with_the_hub_unreachable() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + hub.set_offline(true); + unlocker.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert!(slot.open().is_some()); +} + +#[tokio::test] +async fn a_wrong_password_stays_locked_and_says_so() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + let result = unlocker.unlock(Secret::new("another password")).await; + assert!(matches!(result, Err(VaultError::Authentication))); + assert!(slot.open().is_none()); +} + +#[tokio::test] +async fn a_password_that_is_too_short_is_not_tried() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, _slot, _dir, _operations) = locked_device(&hub); + assert!(matches!( + unlocker.unlock(Secret::new("short")).await, + Err(VaultError::Password) + )); +} + +#[tokio::test] +async fn a_keyset_changed_on_another_device_is_taken_from_the_hub() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + // Another device recovered the account: the hub now holds a newer record that the same + // password opens. + let (mut newer, _) = record(&unlock(PASSWORD)); + newer.version = 5; + hub.set_keyset(Some(newer.clone())); + unlocker.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert_eq!(slot.read_cache(|c| c.unwrap().keyset.version), 5); +} + +#[tokio::test] +async fn an_account_without_cached_keys_has_nothing_to_unlock() { + let dir = tempfile::tempdir().unwrap(); + let accounts = crate::account::Accounts::new(dir.path().to_path_buf()); + let slot = accounts.slot(&crate::account::testing::hub()).unwrap(); + let operations = native_operations(&slot, Arc::new(MemoryHub::default())); + let unlocker = Unlocker::new(operations.membership().unwrap()); + assert!(matches!( + unlocker.unlock(Secret::new(PASSWORD)).await, + Err(VaultError::NotFound) + )); +} diff --git a/crates/hub-io/tests/hub.rs b/crates/hub-io/tests/hub.rs new file mode 100644 index 00000000..f5363263 --- /dev/null +++ b/crates/hub-io/tests/hub.rs @@ -0,0 +1,251 @@ +//! End-to-end tests against a real hub. +//! +//! They need a running hub (`docker compose up` in the nocterm-server repository) and these +//! variables: `NOCTERM_HUB_ADDRESS`, `NOCTERM_HUB_FINGERPRINT` (SHA-256 of its certificate), +//! `NOCTERM_HUB_USERNAME` and `NOCTERM_HUB_PASSWORD`. Without `NOCTERM_REQUIRE_HUB` the tests +//! pass vacuously when the variables are missing; with it, missing variables fail the run, so +//! CI cannot silently skip them. +//! +//! The relay test that reaches a destination also needs `NOCTERM_HUB_RELAY_TARGET` (`host:port` +//! of an SSH server the hub is allowed to reach, for example a container of the compose file); +//! without it that test passes vacuously. + +use futures::executor::block_on; +use nocterm_hub_io::{HubLinks, HubLinksConfig, HubRelay}; +use nocterm_hubs_model::{ + Fingerprint, HubAddress, HubEndpointSpec, HubError, HubId, HubSignIn as _, HubTrust as _, + SignedIn, TrustPolicy, +}; +use nocterm_session::{RelayDialer as _, RelayError, RelayRefusal, Secret, Target}; + +#[path = "hub/account.rs"] +mod account; +#[path = "hub/chats.rs"] +mod chats; +#[path = "hub/revocation.rs"] +mod revocation; + +struct Hub { + address: HubAddress, + fingerprint: Fingerprint, + username: String, + password: String, +} + +impl Hub { + fn spec(&self) -> HubEndpointSpec { + HubEndpointSpec { + address: self.address.clone(), + trust: TrustPolicy::Pinned(self.fingerprint), + } + } + + fn id(&self) -> HubId { + HubId::from(self.address.clone()) + } +} + +fn hub() -> Option { + let variable = |name: &str| std::env::var(name).ok(); + let settings = ( + variable("NOCTERM_HUB_ADDRESS"), + variable("NOCTERM_HUB_FINGERPRINT"), + variable("NOCTERM_HUB_USERNAME"), + variable("NOCTERM_HUB_PASSWORD"), + ); + let (Some(address), Some(fingerprint), Some(username), Some(password)) = settings else { + assert!( + std::env::var_os("NOCTERM_REQUIRE_HUB").is_none(), + "NOCTERM_REQUIRE_HUB is set but NOCTERM_HUB_ADDRESS, NOCTERM_HUB_FINGERPRINT, \ + NOCTERM_HUB_USERNAME and NOCTERM_HUB_PASSWORD are not all set" + ); + return None; + }; + Some(Hub { + address: HubAddress::parse(&address).expect("NOCTERM_HUB_ADDRESS"), + fingerprint: Fingerprint::parse(&fingerprint).expect("NOCTERM_HUB_FINGERPRINT"), + username, + password, + }) +} + +fn links(dir: &tempfile::TempDir) -> std::sync::Arc { + HubLinks::start(HubLinksConfig { + credentials_dir: dir.path().join("hubs"), + device_name: "hub-io test".into(), + }) + .unwrap() +} + +/// Signs in with the account's password. The hub limits how often an account signs in and the +/// tests sign in at once, so a refusal for that reason is waited out. +fn sign_in(links: &HubLinks, hub: &Hub) -> Result { + for _ in 0..40 { + let signed = block_on(links.password( + hub.spec(), + hub.username.clone(), + Secret::new(hub.password.clone()), + )); + match signed { + Err(error) if format!("{error:?}").contains("ResourceExhausted") => { + std::thread::sleep(std::time::Duration::from_millis(250)); + } + other => return other, + } + } + panic!("the hub kept refusing to sign {} in", hub.username) +} + +#[test] +fn probe_shows_the_certificate_that_was_pinned() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let probed = block_on(links(&dir).probe(&hub.address)).unwrap(); + assert_eq!(probed.fingerprint, hub.fingerprint); +} + +#[test] +fn another_pinned_certificate_is_a_changed_certificate() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let wrong = Fingerprint::from_bytes([0xAB; 32]); + let spec = HubEndpointSpec { + address: hub.address.clone(), + trust: TrustPolicy::Pinned(wrong), + }; + match block_on(links(&dir).connect(spec)) { + Err(HubError::CertificateChanged { + pinned, presented, .. + }) => { + assert_eq!(pinned, wrong); + assert_eq!(presented, hub.fingerprint); + } + other => panic!("{other:?}"), + } +} + +#[test] +fn sign_in_survives_a_restart_and_sign_out_ends_it() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + + let first = links(&dir); + let info = block_on(first.connect(hub.spec())).unwrap(); + assert!(!info.signed_in); + assert!(info.password_login); + + let refused = block_on(first.password(hub.spec(), hub.username.clone(), Secret::new("wrong"))); + assert!( + matches!(refused, Err(HubError::InvalidCredentials)), + "{refused:?}" + ); + + let account = sign_in(&first, &hub).unwrap().account; + assert_eq!(account.username, hub.username); + drop(first); + + // A new process reads the credentials without a new sign-in. + let second = links(&dir); + let info = block_on(second.connect(hub.spec())).unwrap(); + assert!(info.signed_in); + + block_on(second.sign_out(&hub.id())).unwrap(); + let info = block_on(second.connect(hub.spec())).unwrap(); + assert!(!info.signed_in); +} + +/// Waits for `future` for a while: a relay that never answers must fail the test, not hang it. +fn within(future: impl std::future::Future) -> T { + tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .unwrap() + .block_on(async { + tokio::time::timeout(std::time::Duration::from_secs(20), future) + .await + .expect("no answer in time") + }) +} + +fn signed_in_relay(hub: &Hub, dir: &tempfile::TempDir) -> (std::sync::Arc, HubRelay) { + let links = links(dir); + sign_in(&links, hub).unwrap(); + let relay = HubRelay::new(&links); + (links, relay) +} + +fn target(host: &str, port: u16) -> Target { + Target { + host: host.into(), + port, + user: "relay".into(), + } +} + +/// The `host:port` a variable names, or `None` when it is not set. +fn destination(variable: &str) -> Option<(String, u16)> { + let destination = std::env::var(variable).ok()?; + let (host, port) = destination.rsplit_once(':').expect(variable); + Some((host.to_owned(), port.parse().expect(variable))) +} + +/// Reads the server's banner from a relay, then half-closes it and waits for the server to end. +fn expect_banner_then_end(stream: nocterm_session::RelayStream) { + let (to_server, from_server) = stream.into_parts(); + let banner = within(from_server.recv()).expect("the server sends its banner first"); + assert!( + banner.starts_with(b"SSH-"), + "{:?}", + String::from_utf8_lossy(&banner) + ); + // Ending our direction is a half-close: the server, which waits for our banner, closes. + drop(to_server); + within(async { while from_server.recv().await.is_ok() {} }); +} + +#[test] +fn a_relay_reaches_a_server_carries_its_banner_and_ends_with_it() { + let Some(hub) = hub() else { return }; + let Some((host, port)) = destination("NOCTERM_HUB_RELAY_TARGET") else { + return; + }; + let dir = tempfile::tempdir().unwrap(); + let (_links, relay) = signed_in_relay(&hub, &dir); + + // The address is found as the user may type it, not only as it was signed in with. + let address = format!("https://{}/", hub.address); + let stream = within(relay.dial(&address, &target(&host, port))).unwrap(); + expect_banner_then_end(stream); +} + +#[test] +fn a_relay_refuses_the_hubs_own_network_and_the_metadata_service() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let (_links, relay) = signed_in_relay(&hub, &dir); + for (host, port) in [("127.0.0.1", 22), ("169.254.169.254", 80)] { + let refused = within(relay.dial(&hub.address.to_string(), &target(host, port))); + assert!( + matches!( + refused, + Err(RelayError::Refused { + reason: RelayRefusal::Policy | RelayRefusal::PrivateNetwork + }) + ), + "{host}: {refused:?}" + ); + } +} + +#[test] +fn a_relay_needs_a_hub_that_was_signed_in_to_on_this_device() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let relay = HubRelay::new(&links); + let refused = within(relay.dial(&hub.address.to_string(), &target("example.com", 22))); + assert!( + matches!(refused, Err(RelayError::NotSignedIn { .. })), + "{refused:?}" + ); +} diff --git a/crates/hub-io/tests/hub/account.rs b/crates/hub-io/tests/hub/account.rs new file mode 100644 index 00000000..50bcea2b --- /dev/null +++ b/crates/hub-io/tests/hub/account.rs @@ -0,0 +1,321 @@ +//! The keys of an account on a real hub: what one device keeps, another device reads. +//! +//! Two devices are two credential directories. They sign in with the same account, so the +//! second one opens the keyset the first one created. The records the tests write are removed +//! again, and the account is left as it was found. + +use super::{Hub, hub, links, sign_in}; +use futures::executor::block_on; +use nocterm_connections_model::{ + profiles::Profile, + replica::{ProfileReplica as _, RemoteBody}, + test_support::profile_id_for, +}; +use nocterm_hub_io::HubLinks; +use nocterm_hubs_model::{HubSignIn as _, KeyStatus, source_identity::LocalAccountSourceError}; +use nocterm_session::{Auth, Secret}; +use nocterm_vault::{CredentialBinding, VaultAccess as _}; +use std::sync::Arc; + +/// One device: its own credential directory, signed in with the account's password. +pub(super) struct Device { + links: Arc, + login: nocterm_hubs_model::LoginReceipt, + _dir: tempfile::TempDir, +} + +impl Device { + pub(super) fn sign_in(hub: &Hub) -> (Self, KeyStatus) { + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let signed = sign_in(&links, hub).expect("the account signs in"); + ( + Self { + links, + login: signed.login, + _dir: dir, + }, + signed.keys, + ) + } + + pub(super) fn account(&self, hub: &Hub) -> nocterm_hub_io::HubAccount { + self.links.open_account(&hub.id()).expect("an account") + } +} + +impl Drop for Device { + fn drop(&mut self) { + // The device is not left registered with the account. + if let Some(hub) = hub() { + let _ = block_on(self.links.sign_out(&hub.id())); + } + } +} + +pub(super) fn unique(name: &str) -> String { + format!("{name}-{}", uuid::Uuid::new_v4()) +} + +#[test] +fn the_keys_of_an_account_open_on_a_second_device() { + let Some(hub) = hub() else { return }; + let (_first, keys) = Device::sign_in(&hub); + // The first sign-in of an account makes the keys; any later one opens them. + assert!( + matches!(keys, KeyStatus::Created(_) | KeyStatus::Open), + "{keys:?}" + ); + let (second, keys) = Device::sign_in(&hub); + assert!(matches!(keys, KeyStatus::Open), "{keys:?}"); + assert!(second.account(&hub).vault().is_unlocked()); +} + +#[test] +fn a_retained_account_factory_recovers_after_forget_and_preserves_captured_source_fencing() { + use nocterm_hubs_model::AccountDevices as _; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let original = account.local_source().unwrap(); + let replica = account.local_replica(original.clone()).unwrap(); + let old_device = block_on(device.links.list(&hub.id())) + .unwrap() + .into_iter() + .find(|device| device.is_current) + .unwrap() + .id; + device.links.forget(&hub.id()).unwrap(); + assert!(original.check_current().is_err()); + assert!(account.local_source().is_err()); + // A failed settings save leaves this general factory installed, then the + // same configured hub signs in again through a fresh native membership. + sign_in(&device.links, &hub).unwrap(); + block_on(device.links.revoke(&hub.id(), &old_device)).unwrap(); + let replacement = account.local_source().unwrap(); + assert!(replacement.check_current().is_ok()); + assert_eq!(replacement.partition(), original.partition()); + assert!(account.vault().is_unlocked()); + assert!(original.check_current().is_err()); + assert!(matches!( + block_on(replica.changes(None)), + Err(nocterm_connections_model::replica::ReplicaError::Stale) + )); +} + +#[test] +fn local_history_capture_survives_reconnect_but_not_lock_or_a_replacement_login() { + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let source = account.local_source().unwrap(); + let partition = source.partition(); + block_on(device.links.connect(hub.spec())).unwrap(); + assert_eq!(source.check_current(), Ok(())); + + let vault = account.vault(); + vault.lock(); + assert!( + device.login.check_current().is_ok(), + "a key lock is not a new login" + ); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + block_on(vault.unlock(Secret::new(hub.password.clone()))).unwrap(); + let unlocked = account.local_source().unwrap(); + assert_eq!(unlocked.partition(), partition); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + + sign_in(&device.links, &hub).unwrap(); + assert_eq!( + unlocked.check_current(), + Err(LocalAccountSourceError::Stale) + ); + let next = account.local_source().unwrap(); + assert!(matches!( + device.login.discard(), + Err(nocterm_hubs_model::HubError::Stale) + )); + assert!( + next.check_current().is_ok(), + "old same-account cleanup preserves the replacement" + ); + assert_eq!(next.partition(), partition); + block_on(device.links.sign_out(&hub.id())).unwrap(); + assert_eq!(next.check_current(), Err(LocalAccountSourceError::Stale)); +} + +#[test] +fn a_credential_kept_on_one_device_is_read_on_another() { + let Some(hub) = hub() else { return }; + let (first, _) = Device::sign_in(&hub); + let (second, _) = Device::sign_in(&hub); + let label = unique("live-credential"); + let binding = CredentialBinding::KeyPassphrase { + path: "/live/key".into(), + }; + + let id = block_on(first.account(&hub).vault().put( + None, + label.clone(), + binding.clone(), + Secret::new("hunter2"), + )) + .unwrap(); + let vault = second.account(&hub).vault(); + let listed = block_on(vault.list()).unwrap(); + assert!( + listed + .iter() + .any(|info| info.id == id && info.label == label) + ); + let secret = block_on(vault.get(id, binding)).unwrap(); + assert_eq!(secret.expose(), "hunter2"); + + block_on(first.account(&hub).vault().delete(id)).unwrap(); +} + +#[test] +fn captured_terminal_credentials_keep_the_original_account_and_expire_on_lock_and_login() { + use nocterm_session::{CredentialStoreError, SecretRequest}; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let source = account.local_source().unwrap(); + let store = account.local_credentials(source).unwrap(); + let request = SecretRequest::KeyPassphrase { + path: "/live/captured-key".into(), + retry: false, + }; + let id = block_on(store.save( + unique("captured-secret"), + &request, + Secret::new("original account"), + )) + .unwrap(); + block_on(device.links.connect(hub.spec())).unwrap(); + assert_eq!( + block_on(store.get(id, &request)).unwrap().expose(), + "original account" + ); + account.vault().lock(); + assert!(!store.is_available()); + assert!(matches!( + block_on(store.get(id, &request)), + Err(CredentialStoreError::Stale) + )); + block_on(account.vault().unlock(Secret::new(hub.password.clone()))).unwrap(); + let next = account + .local_credentials(account.local_source().unwrap()) + .unwrap(); + assert_eq!( + block_on(next.get(id, &request)).unwrap().expose(), + "original account" + ); + sign_in(&device.links, &hub).unwrap(); + assert!(matches!( + block_on(next.get(id, &request)), + Err(CredentialStoreError::Stale) + )); + let current = account + .local_credentials(account.local_source().unwrap()) + .unwrap(); + assert_eq!( + block_on(current.get(id, &request)).unwrap().expose(), + "original account" + ); + block_on(account.vault().delete(id)).unwrap(); +} + +#[test] +fn a_profile_saved_on_one_device_is_read_on_another() { + let Some(hub) = hub() else { return }; + let (first, _) = Device::sign_in(&hub); + let (second, _) = Device::sign_in(&hub); + let name = unique("live-profile"); + let profile = Profile { + id: profile_id_for(&name), + name: name.clone(), + description: String::new(), + options: Default::default(), + target: nocterm_session::Target::new("ci", "live.example", 22), + auth: Auth::Password, + credential: None, + launch: None, + icon: None, + icon_color: None, + country: None, + group: None, + jump_hosts: Vec::new(), + }; + + let written = block_on( + first + .account(&hub) + .replica() + .unwrap() + .put(profile.clone(), None), + ) + .unwrap(); + let page = block_on(second.account(&hub).replica().unwrap().changes(None)).unwrap(); + let seen = page + .changes + .iter() + .find(|change| change.id == profile.id) + .expect("the other device sees the profile"); + assert_eq!(seen.body, RemoteBody::Present(Box::new(profile.clone()))); + + block_on( + first + .account(&hub) + .replica() + .unwrap() + .delete(profile.id, written), + ) + .unwrap(); +} + +#[test] +fn captured_relay_cannot_follow_relogin_or_change_its_original_route() { + use nocterm_session::{RelayError, Target}; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let source = account.local_source().unwrap(); + let relay = account.local_relay(source).unwrap(); + let name = hub.address.to_string(); + let target = Target::new("relay", "example.com", 22); + let captured = relay.capture(&name, &target).unwrap(); + let other = Target::new("another-principal", "example.com", 22); + assert!(matches!( + captured.capture(&name, &other), + Err(RelayError::CaptureUnsupported) + )); + sign_in(&device.links, &hub).unwrap(); + let stale = super::within(captured.dial(&name, &target)); + assert!( + matches!(stale, Err(RelayError::NotSignedIn { .. })), + "an old native worker selected replacement membership" + ); + let current = account + .local_relay(account.local_source().unwrap()) + .unwrap(); + current.capture(&name, &target).unwrap(); +} +#[test] +fn an_ordinary_relay_captures_before_its_future_is_polled() { + use nocterm_hub_io::HubRelay; + use nocterm_session::{RelayDialer as _, RelayError, Target}; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let relay = HubRelay::new(&device.links); + let name = hub.address.to_string(); + let target = Target::new("relay", "example.com", 22); + let original = relay.capture(&name, &target).unwrap(); + sign_in(&device.links, &hub).unwrap(); + assert!(matches!( + super::within(original.dial(&name, &target)), + Err(RelayError::NotSignedIn { .. }) + )); + let current = relay.capture(&name, &target).unwrap(); + drop(current); +} diff --git a/crates/hub-io/tests/hub/chats.rs b/crates/hub-io/tests/hub/chats.rs new file mode 100644 index 00000000..128dc11e --- /dev/null +++ b/crates/hub-io/tests/hub/chats.rs @@ -0,0 +1,170 @@ +//! A hub chat's servers on a real hub: a server attached before the first message is attached +//! to the chat the message creates, every device reopens the chat with it, and detaching it +//! is stored at once. The agent, provider and server it records are removed again. + +use super::{Hub, hub, links, sign_in}; +use futures::executor::block_on; +use nocterm_ai::agents::AgentLaunch; +use nocterm_ai::model::content::Content; +use nocterm_ai::model::controls::{ConfigCategory, ConfigKind, ConfigOption, ConfigSetting}; +use nocterm_ai::model::ids::SessionId; +use nocterm_ai::model::session::{PromptRequest, RestoreSession, SessionOpen, SetConfigOption}; +use nocterm_ai::{AgentCommands, AgentConnector as _, ConnectRequest}; +use nocterm_hub_agents_model::{ + AgentDraft, ApprovalMode, HubAgentDirectory as _, HubServers as _, NewServer, ProviderDraft, + ProviderKind, +}; +use nocterm_hub_io::{HubAgentConnector, HubAgentsLink, HubLinks}; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +/// A host key the hub accepts; no server answers with it, and none needs to. +const HOST_KEY: &str = + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl"; + +fn connect(links: &HubLinks, launch: &AgentLaunch) -> Arc { + let request = ConnectRequest { + launch: launch.clone(), + working_directory: std::env::temp_dir(), + terminal_auth: false, + sandbox: None, + resources: Default::default(), + cancellation: Default::default(), + }; + block_on(HubAgentConnector::new(links).connect(request)) + .expect("the hub agent connects") + .commands +} + +/// Whether the options show the server flag `id`, and whether it is on. +fn flag(options: &[ConfigOption], id: &str) -> Option { + options + .iter() + .find(|option| option.id.as_str() == format!("server:{id}")) + .map(|option| { + assert_eq!(option.category, Some(ConfigCategory::Server)); + matches!(option.kind, ConfigKind::Boolean(true)) + }) +} + +/// The chat as another device reopens it, once the hub has created it. +fn reopened(commands: &Arc, chat: &SessionId) -> Vec { + let deadline = Instant::now() + Duration::from_secs(20); + loop { + let restore = RestoreSession { + session: chat.clone(), + working_directory: std::env::temp_dir(), + endpoints: Vec::new(), + fork: false, + }; + match block_on(commands.restore_session(restore)) { + Ok(opened) => return opened.configuration.unwrap_or_default(), + Err(error) if Instant::now() > deadline => panic!("the chat never opened: {error}"), + Err(_) => std::thread::sleep(Duration::from_millis(200)), + } + } +} + +#[test] +fn a_server_attached_before_the_first_message_stays_with_the_chat() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + sign_in(&links, &hub).expect("the account signs in"); + let directory = HubAgentsLink::new(&links); + let (agent, provider, server) = records(&directory, &hub); + let launch = AgentLaunch::hub_agent(hub.id(), agent.id.to_string(), agent.name.clone()); + + let here = connect(&links, &launch); + let open = SessionOpen { + working_directory: std::env::temp_dir(), + endpoints: Vec::new(), + }; + let opened = block_on(here.new_session(open)).expect("a new chat"); + let id = server.id.to_string(); + assert_eq!(flag(&opened.configuration.unwrap(), &id), Some(false)); + let attach = SetConfigOption { + session: opened.session.clone(), + config: format!("server:{id}").into(), + setting: ConfigSetting::Boolean(true), + }; + assert_eq!( + flag(&block_on(here.set_config_option(attach)).unwrap(), &id), + Some(true) + ); + // The first message creates the chat; its answer does not matter here. + let prompt = here.prompt(PromptRequest { + session: opened.session.clone(), + content: vec![Content::text("hello")], + }); + std::thread::spawn(move || drop(block_on(prompt))); + + let elsewhere = connect(&links, &launch); + assert_eq!( + flag(&reopened(&elsewhere, &opened.session), &id), + Some(true) + ); + let detach = SetConfigOption { + session: opened.session.clone(), + config: format!("server:{id}").into(), + setting: ConfigSetting::Boolean(false), + }; + assert_eq!( + flag(&block_on(here.set_config_option(detach)).unwrap(), &id), + Some(false) + ); + let again = connect(&links, &launch); + assert_eq!(flag(&reopened(&again, &opened.session), &id), Some(false)); + + block_on(directory.remove_server(&hub.id(), &server.id)).expect("the server is removed"); + block_on(directory.delete_agent(&hub.id(), &agent.id)).expect("the agent is removed"); + block_on(directory.delete_provider(&hub.id(), &provider)).expect("the provider is removed"); + let _ = block_on(nocterm_hubs_model::HubSignIn::sign_out(&*links, &hub.id())); +} + +/// An agent with a provider, and a server of the account, as the web panel makes them. +fn records( + directory: &HubAgentsLink, + hub: &Hub, +) -> ( + nocterm_hub_agents_model::HubAgent, + nocterm_hub_agents_model::ProviderId, + nocterm_hub_agents_model::HubServer, +) { + let provider = block_on(directory.create_provider( + &hub.id(), + ProviderDraft { + kind: ProviderKind::OpenAi, + name: "Stub".into(), + base_url: "https://models.example/v1".into(), + models: vec!["m1".into()], + api_key: None, + }, + )) + .expect("a provider"); + let agent = block_on(directory.create_agent( + &hub.id(), + AgentDraft { + name: "Ops".into(), + instructions: String::new(), + provider: Some(provider.id.clone()), + model: "m1".into(), + models: vec!["m1".into()], + approval: ApprovalMode::Auto, + servers: Vec::new(), + }, + )) + .expect("an agent"); + let server = block_on(directory.add_server( + &hub.id(), + NewServer { + name: "build".into(), + host: "build.example".into(), + port: 22, + user: "ops".into(), + host_key: HOST_KEY.into(), + }, + )) + .expect("a server"); + (agent, provider.id, server) +} diff --git a/crates/hub-io/tests/hub/revocation.rs b/crates/hub-io/tests/hub/revocation.rs new file mode 100644 index 00000000..e6738eaa --- /dev/null +++ b/crates/hub-io/tests/hub/revocation.rs @@ -0,0 +1,129 @@ +//! Actual server revocation reaches the native conditional cleanup boundary. +use super::{Hub, hub, sign_in}; +use futures::executor::block_on; +use nocterm_connections_model::replica::ProfileReplica as _; +use nocterm_hub_client::{DeviceInfo, HubClient, HubEndpoint, Trust}; +use nocterm_hub_io::{HubLinks, HubLinksConfig}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubSignIn as _; +use nocterm_keys::DeviceKey; +use nocterm_vault::{VaultAccess as _, VaultError, VaultStatus}; +use tokio::runtime::Runtime; + +/// A second real device of the same account, removed again even if an assertion fails. +struct Revoker { + runtime: Runtime, + client: HubClient, + device: String, +} +impl Revoker { + fn sign_in(hub: &Hub) -> Self { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let endpoint = HubEndpoint::with_trust( + &hub.address.to_string(), + Trust::Pinned(*hub.fingerprint.as_bytes()), + ) + .unwrap(); + let client = runtime + .block_on(HubClient::connect( + &endpoint, + DeviceKey::generate().unwrap(), + None, + )) + .unwrap(); + let device = DeviceInfo::new(super::account::unique("revoker"), "Nocterm test"); + let signed = runtime + .block_on(client.password_login(&hub.username, &hub.password, &device)) + .unwrap(); + Self { + runtime, + client, + device: signed.session.device_id, + } + } + fn revoke_named(&self, name: &str) { + self.runtime.block_on(async { + let listed = self + .client + .account() + .list_devices(v2::Empty {}) + .await + .unwrap() + .into_inner(); + let device = listed + .devices + .into_iter() + .find(|device| device.name == name) + .expect("the native device is registered"); + self.client + .account() + .revoke_device(v2::IdRequest { + id: device.device_id.clone(), + }) + .await + .unwrap(); + let listed = self + .client + .account() + .list_devices(v2::Empty {}) + .await + .unwrap() + .into_inner(); + assert!( + listed + .devices + .iter() + .any(|known| known.device_id == device.device_id && known.revoked), + "the server confirms the native device is revoked" + ); + }); + } +} +impl Drop for Revoker { + fn drop(&mut self) { + let _ = self + .runtime + .block_on(self.client.account().revoke_device(v2::IdRequest { + id: self.device.clone(), + })); + } +} + +#[test] +fn current_server_revocation_erases_only_the_original_native_login_and_keys() { + let Some(hub) = hub() else { + return; + }; + let directory = tempfile::tempdir().unwrap(); + let label = super::account::unique("native-revoked"); + let links = HubLinks::start(HubLinksConfig { + credentials_dir: directory.path().join("hubs"), + device_name: label.clone(), + }) + .unwrap(); + let signed = sign_in(&links, &hub).unwrap(); + let account = links.open_account(&hub.id()).unwrap(); + let source = account.local_source().unwrap(); + let vault = account.vault(); + let revoker = Revoker::sign_in(&hub); + revoker.revoke_named(&DeviceInfo::new(label, "Nocterm").display_name()); + let replica = account.replica().unwrap(); + let denied = block_on(vault.list()); + let remote = block_on(replica.changes(None)).map(|page| page.changes.len()); + let source_current = source.check_current().is_ok(); + assert!( + matches!(&denied, Err(VaultError::Stale)), + "native vault outcome: {:?}; remote-only replica outcome: {remote:?}; original source current: {source_current}", + denied.as_ref().map(Vec::len) + ); + assert!( + matches!(vault.status(), VaultStatus::Missing | VaultStatus::Locked), + "confirmed auth rejection closes native keys; untyped expiry may retain ciphertext" + ); + assert!(source.check_current().is_err()); + assert!(signed.login.check_current().is_err()); + assert!(!block_on(links.connect(hub.spec())).unwrap().signed_in); +} diff --git a/crates/hubs-model/Cargo.toml b/crates/hubs-model/Cargo.toml new file mode 100644 index 00000000..ee143e28 --- /dev/null +++ b/crates/hubs-model/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "nocterm-hubs-model" +description = "Hub addresses and trust rules, the hubs settings section and the hub access ports." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "domain" + +[features] +test-support = [] + +[dependencies] +nocterm-core.workspace = true +nocterm-session.workspace = true +nocterm-settings.workspace = true + +futures.workspace = true +schemars.workspace = true +serde.workspace = true +thiserror.workspace = true + +[dev-dependencies] +toml.workspace = true + +[lints] +workspace = true diff --git a/crates/hubs-model/src/account_keys.rs b/crates/hubs-model/src/account_keys.rs new file mode 100644 index 00000000..9884a2f3 --- /dev/null +++ b/crates/hubs-model/src/account_keys.rs @@ -0,0 +1,47 @@ +//! Whether the keys of an account are open on this device, and the ways to open or close them. +//! +//! Everything that reads the account's encrypted records (the agent access records, the +//! vault) needs open keys. A restart leaves them closed until the user types the password, and +//! the user may close them again at any time. + +use futures::future::BoxFuture; +use nocterm_core::error::SharedCause; +use nocterm_session::Secret; + +use crate::address::HubId; + +/// Why the keys of an account could not be opened. +#[derive(Clone, Debug, thiserror::Error)] +pub enum AccountKeyError { + /// The password does not open the keys. + #[error("the password does not open the account keys")] + WrongPassword, + /// The hub cannot be reached and no copy of the keys' record is kept on this device. + #[error("the hub cannot be reached")] + Offline, + /// This device is not signed in to the hub, or the sign-in ended meanwhile. + #[error("this device is not signed in to the hub")] + NotSignedIn, + /// The account keys cannot be opened for another reason. + #[error("the account keys cannot be opened")] + Failed { + #[source] + cause: SharedCause, + }, +} + +/// A result that completes later. +pub type AccountKeyFuture = BoxFuture<'static, Result>; + +/// The keys of the account on a hub: their state, opening with the password, and closing. +pub trait AccountKeyLock: Send + Sync { + /// Whether the keys of `hub`'s account are open now. + fn is_open(&self, hub: &HubId) -> bool; + + /// Opens the keys with `password`: the login password, or the vault passphrase of an + /// account that signs in through a provider. Already open keys stay open. + fn unlock(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()>; + + /// Closes the keys: records that need them are unreadable until they are opened again. + fn lock(&self, hub: &HubId); +} diff --git a/crates/hubs-model/src/address.rs b/crates/hubs-model/src/address.rs new file mode 100644 index 00000000..64a7ad66 --- /dev/null +++ b/crates/hubs-model/src/address.rs @@ -0,0 +1,182 @@ +//! Where a hub listens: the canonical address that also identifies it. + +use std::fmt; +use std::net::Ipv6Addr; +use std::str::FromStr; + +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::error::HubAddressError; + +/// The port a hub listens on unless the address names another. +pub const DEFAULT_PORT: u16 = 7443; + +const MAX_HOST_LEN: usize = 253; +const MAX_LABEL_LEN: usize = 63; + +/// A hub's `host:port`, lower-case, with the default port made explicit. +/// +/// Only TLS addresses exist: `https://` is accepted and dropped, `http://` is refused. +#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct HubAddress { + host: String, + port: u16, +} + +impl HubAddress { + /// Reads what a person types: `hub.example.com`, `hub.example.com:7443`, + /// `[::1]:7443` or `https://hub.example.com/`. + pub fn parse(text: &str) -> Result { + let text = text.trim(); + if text.is_empty() { + return Err(HubAddressError::Empty); + } + let rest = strip_scheme(text)?; + let rest = rest.trim_end_matches('/'); + let (host, port) = split_host_port(rest)?; + let port = match port { + Some(port) => port + .parse::() + .ok() + .filter(|port| *port != 0) + .ok_or(HubAddressError::InvalidPort)?, + None => DEFAULT_PORT, + }; + let host = host.to_ascii_lowercase(); + if !valid_host(&host) { + return Err(HubAddressError::InvalidHost); + } + Ok(Self { host, port }) + } + + /// The host name or IP literal, lower-case and without brackets. + pub fn host(&self) -> &str { + &self.host + } + + pub fn port(&self) -> u16 { + self.port + } +} + +/// Drops `https://`; refuses plaintext and every other scheme. +fn strip_scheme(text: &str) -> Result<&str, HubAddressError> { + let Some((scheme, rest)) = text.split_once("://") else { + return Ok(text); + }; + if scheme.eq_ignore_ascii_case("https") { + Ok(rest) + } else if scheme.eq_ignore_ascii_case("http") { + Err(HubAddressError::PlaintextRefused) + } else { + Err(HubAddressError::UnsupportedScheme) + } +} + +/// `host` and the port text after it, if any; an IPv6 literal is in brackets. +fn split_host_port(text: &str) -> Result<(&str, Option<&str>), HubAddressError> { + if let Some(rest) = text.strip_prefix('[') { + let (host, after) = rest.split_once(']').ok_or(HubAddressError::InvalidHost)?; + if host.parse::().is_err() { + return Err(HubAddressError::InvalidHost); + } + return match after { + "" => Ok((host, None)), + after => after + .strip_prefix(':') + .map(|port| (host, Some(port))) + .ok_or(HubAddressError::InvalidHost), + }; + } + match text.split_once(':') { + None => Ok((text, None)), + Some((host, port)) if !port.contains(':') => Ok((host, Some(port))), + // A second colon: an IPv6 literal without brackets cannot carry a port. + Some(_) => Err(HubAddressError::InvalidHost), + } +} + +fn valid_host(host: &str) -> bool { + if host.contains(':') { + return host.parse::().is_ok(); + } + host.len() <= MAX_HOST_LEN + && host.split('.').all(|label| { + !label.is_empty() + && label.len() <= MAX_LABEL_LEN + && !label.starts_with('-') + && !label.ends_with('-') + && label + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-') + }) +} + +impl fmt::Display for HubAddress { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.host.contains(':') { + write!(f, "[{}]:{}", self.host, self.port) + } else { + write!(f, "{}:{}", self.host, self.port) + } + } +} + +impl fmt::Debug for HubAddress { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "HubAddress({self})") + } +} + +impl FromStr for HubAddress { + type Err = HubAddressError; + + fn from_str(text: &str) -> Result { + Self::parse(text) + } +} + +impl Serialize for HubAddress { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_str(self) + } +} + +impl<'de> Deserialize<'de> for HubAddress { + fn deserialize>(deserializer: D) -> Result { + let text = String::deserialize(deserializer)?; + Self::parse(&text).map_err(serde::de::Error::custom) + } +} + +/// The identity of a hub on this device: its canonical address. +#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(transparent)] +pub struct HubId(HubAddress); + +impl HubId { + pub fn address(&self) -> &HubAddress { + &self.0 + } +} + +impl From for HubId { + fn from(address: HubAddress) -> Self { + Self(address) + } +} + +impl fmt::Display for HubId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Debug for HubId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "HubId({})", self.0) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/address/tests.rs b/crates/hubs-model/src/address/tests.rs new file mode 100644 index 00000000..3f80b5bd --- /dev/null +++ b/crates/hubs-model/src/address/tests.rs @@ -0,0 +1,109 @@ +use super::*; + +fn parsed(text: &str) -> String { + HubAddress::parse(text).unwrap().to_string() +} + +#[test] +fn a_bare_host_gets_the_default_port_and_lower_case() { + assert_eq!(parsed("Hub.Example.COM"), "hub.example.com:7443"); + assert_eq!(parsed(" hub.home "), "hub.home:7443"); +} + +#[test] +fn an_explicit_port_is_kept() { + let address = HubAddress::parse("hub.home:9000").unwrap(); + assert_eq!(address.host(), "hub.home"); + assert_eq!(address.port(), 9000); + assert_eq!(parsed("10.0.0.5:7443"), "10.0.0.5:7443"); +} + +#[test] +fn ipv6_literals_need_brackets_and_keep_them_when_shown() { + assert_eq!(parsed("[::1]:7443"), "[::1]:7443"); + assert_eq!(parsed("[::1]"), "[::1]:7443"); + assert_eq!(parsed("[FE80::1]:9"), "[fe80::1]:9"); + assert_eq!( + HubAddress::parse("::1"), + Err(HubAddressError::InvalidHost), + "a second colon without brackets is ambiguous" + ); + assert_eq!(HubAddress::parse("[::1"), Err(HubAddressError::InvalidHost)); + assert_eq!( + HubAddress::parse("[not-an-address]:1"), + Err(HubAddressError::InvalidHost) + ); +} + +#[test] +fn https_is_dropped_and_http_is_refused() { + assert_eq!(parsed("https://hub.example.com/"), "hub.example.com:7443"); + assert_eq!( + parsed("HTTPS://hub.example.com:8443"), + "hub.example.com:8443" + ); + assert_eq!( + HubAddress::parse("http://hub.example.com"), + Err(HubAddressError::PlaintextRefused) + ); + assert_eq!( + HubAddress::parse("HTTP://127.0.0.1:7443"), + Err(HubAddressError::PlaintextRefused) + ); + assert_eq!( + HubAddress::parse("ssh://hub.example.com"), + Err(HubAddressError::UnsupportedScheme) + ); +} + +#[test] +fn empty_and_garbage_addresses_are_refused() { + assert_eq!(HubAddress::parse(""), Err(HubAddressError::Empty)); + assert_eq!(HubAddress::parse(" "), Err(HubAddressError::Empty)); + for garbage in [ + ":7443", + "hub example", + "hub.example.com/path", + "user@hub.example.com", + "-hub.example.com", + "hub..example.com", + "hub_example.com", + "hub.example.com.", + ] { + assert_eq!( + HubAddress::parse(garbage), + Err(HubAddressError::InvalidHost), + "{garbage}" + ); + } +} + +#[test] +fn ports_outside_the_range_are_refused() { + for port in ["0", "65536", "-1", "abc", ""] { + assert_eq!( + HubAddress::parse(&format!("hub.home:{port}")), + Err(HubAddressError::InvalidPort), + "{port:?}" + ); + } + assert_eq!(parsed("hub.home:65535"), "hub.home:65535"); +} + +#[test] +fn spellings_of_one_hub_are_one_identity() { + let first = HubId::from(HubAddress::parse("HUB.home").unwrap()); + let second = HubId::from(HubAddress::parse("https://hub.home:7443/").unwrap()); + assert_eq!(first, second); + assert_eq!(first.to_string(), "hub.home:7443"); + assert_ne!( + first, + HubId::from(HubAddress::parse("hub.home:7444").unwrap()) + ); +} + +#[test] +fn an_address_round_trips_through_its_text() { + let address = HubAddress::parse("[::1]:9").unwrap(); + assert_eq!(address.to_string().parse::().unwrap(), address); +} diff --git a/crates/hubs-model/src/color.rs b/crates/hubs-model/src/color.rs new file mode 100644 index 00000000..7711c526 --- /dev/null +++ b/crates/hubs-model/src/color.rs @@ -0,0 +1,54 @@ +//! The colour a server is marked with in menus. + +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +/// A named colour. The views map each name to the current theme, so a server keeps its colour +/// across themes; the names, not the shades, are what the settings keep. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "kebab-case")] +pub enum HubColor { + Red, + Yellow, + Green, + Cyan, + Blue, + Magenta, +} + +impl HubColor { + /// Every colour, in the order a chooser lists them. + pub const ALL: [Self; 6] = [ + Self::Red, + Self::Yellow, + Self::Green, + Self::Cyan, + Self::Blue, + Self::Magenta, + ]; + + /// The name the settings and the views use. + pub fn name(self) -> &'static str { + match self { + Self::Red => "red", + Self::Yellow => "yellow", + Self::Green => "green", + Self::Cyan => "cyan", + Self::Blue => "blue", + Self::Magenta => "magenta", + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_name_is_the_one_the_settings_use() { + for color in HubColor::ALL { + let value = toml::Value::try_from(color).unwrap(); + assert_eq!(value, toml::Value::String(color.name().to_owned())); + } + } +} diff --git a/crates/hubs-model/src/devices.rs b/crates/hubs-model/src/devices.rs new file mode 100644 index 00000000..311cac83 --- /dev/null +++ b/crates/hubs-model/src/devices.rs @@ -0,0 +1,36 @@ +//! The devices of an account on a hub, as the Servers page lists and revokes them. + +use crate::address::HubId; +use crate::ports::HubFuture; + +/// One device signed in to the account on a hub. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct DeviceSummary { + /// The hub's identifier of the device. + pub id: String, + /// The name the device gave itself when it signed in. + pub name: String, + /// What kind of client it is, as the hub reports it (`desktop`, `browser`, `hubctl`, ...). + pub platform: String, + /// When the device first signed in, in milliseconds since the Unix epoch. + pub created_at_ms: i64, + /// When the hub last heard from the device, in milliseconds since the Unix epoch. + pub last_seen_at_ms: i64, + /// Whether this is the device asking. + pub is_current: bool, +} + +/// Lists and revokes the devices of the account this device is signed in to on a hub. +/// +/// An implementation captures the original native membership before it returns the future +/// and rechecks it after every await: a result for an account that was signed out, replaced +/// or revoked meanwhile is [`crate::HubError::Stale`], never shown for the new account. +pub trait AccountDevices: Send + Sync { + /// The devices of the account that are not revoked, the current one included. + /// [`crate::HubError::NotSignedIn`] when this device holds no credentials for `hub`. + fn list(&self, hub: &HubId) -> HubFuture>; + + /// Revokes `device`: its tokens stop working. Revoking this device ends its own session; + /// the caller signs out instead. + fn revoke(&self, hub: &HubId, device: &str) -> HubFuture<()>; +} diff --git a/crates/hubs-model/src/environment.rs b/crates/hubs-model/src/environment.rs new file mode 100644 index 00000000..37e843ca --- /dev/null +++ b/crates/hubs-model/src/environment.rs @@ -0,0 +1,43 @@ +//! The environment a window belongs to. + +use crate::address::HubId; + +/// What a window shows: this computer, or the account of one hub. +/// +/// It is chosen when the window is composed and never changes inside a window. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum Environment { + Local, + Hub(HubId), +} + +impl Environment { + pub const fn is_local(&self) -> bool { + matches!(self, Self::Local) + } + + /// The hub of a hub window. + pub const fn hub(&self) -> Option<&HubId> { + match self { + Self::Local => None, + Self::Hub(hub) => Some(hub), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::address::HubAddress; + + #[test] + fn a_local_environment_has_no_hub_and_a_hub_environment_names_its_hub() { + let hub = HubId::from(HubAddress::parse("hub.home").unwrap()); + assert!(Environment::Local.is_local()); + assert_eq!(Environment::Local.hub(), None); + let environment = Environment::Hub(hub.clone()); + assert!(!environment.is_local()); + assert_eq!(environment.hub(), Some(&hub)); + assert_ne!(environment, Environment::Local); + } +} diff --git a/crates/hubs-model/src/error.rs b/crates/hubs-model/src/error.rs new file mode 100644 index 00000000..72172783 --- /dev/null +++ b/crates/hubs-model/src/error.rs @@ -0,0 +1,121 @@ +//! Typed failures of hub addresses, fingerprints and hub access. + +use nocterm_core::error::SharedCause; + +use crate::address::HubAddress; +use crate::trust::Fingerprint; + +/// Why a typed hub address was refused. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum HubAddressError { + #[error("the hub address is empty")] + Empty, + #[error("the hub address has no valid host name")] + InvalidHost, + #[error("the hub address has no valid port")] + InvalidPort, + /// `http://` has no certificate to check, so a hub is never reached over it. + #[error("plain http:// hubs are refused; hubs use TLS")] + PlaintextRefused, + #[error("the hub address has an unsupported scheme; use https:// or no scheme")] + UnsupportedScheme, +} + +/// Why a typed certificate fingerprint was refused. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum FingerprintError { + /// A SHA-256 fingerprint has 64 hexadecimal digits; `found` digits were typed. + #[error("a fingerprint has 64 hexadecimal digits, found {found}")] + Length { found: usize }, + #[error("a fingerprint holds only hexadecimal digits")] + NotHex, +} + +/// What went wrong reaching or using a hub, in terms the caller can act on. +/// +/// The original failure stays reachable through `source()`; no variant carries text. +#[derive(Clone, Debug, thiserror::Error)] +pub enum HubError { + /// The hub did not answer. + #[error("cannot reach the hub at {address}")] + Unreachable { + address: HubAddress, + #[source] + cause: SharedCause, + }, + /// The hub presents another certificate than the pinned one. Never retried and never + /// accepted without the user removing the hub and signing in again. + #[error("the certificate of {address} changed")] + CertificateChanged { + address: HubAddress, + pinned: Fingerprint, + presented: Fingerprint, + }, + /// The certificate is not vouched for by a public authority and nothing is pinned. + #[error("the certificate of {address} is not trusted")] + CertificateUntrusted { + address: HubAddress, + presented: Fingerprint, + }, + /// The hub and this client speak different protocol versions. + #[error("the hub speaks protocol {hub}, this client speaks {client}")] + UpgradeRequired { hub: u32, client: u32 }, + /// There are no usable credentials for this hub on this device. + #[error("not signed in to the hub")] + NotSignedIn, + /// The original authenticated account owner was replaced or closed. + #[error("the original account is no longer current")] + Stale, + /// The hub revoked this device; its credentials are gone. + #[error("this device was revoked by the hub")] + Revoked, + #[error("the user name or password was refused")] + InvalidCredentials, + /// The hub does not offer the requested sign-in method. + #[error("the hub does not offer this way of signing in")] + MethodUnavailable, + #[error("signing in took too long")] + SignInTimedOut, + #[error("signing in was cancelled")] + SignInCancelled, + #[error("the hub refused the request for lack of permission")] + PermissionDenied, + #[error("the hub has no such object")] + NotFound, + /// The recovery key is not one of the account, or is not a recovery key at all. + #[error("the recovery key does not open the account")] + RecoveryKeyInvalid, + /// The vault passphrase cannot be used to derive a key (too short). + #[error("the vault passphrase is not acceptable")] + InvalidVaultPassphrase, + /// The account's key record on the hub does not open although the key is right: the hub + /// altered it, or it is too weak to trust. Nothing is overwritten. + #[error("the account's key record on the hub is damaged")] + KeysetDamaged { + #[source] + cause: SharedCause, + }, + /// The keys are protected by the other password than the one that was offered: the login + /// password where a vault passphrase is needed, or the reverse. Whatever is typed, the + /// other password cannot open them. + #[error("the account keys are protected by another password than the one offered")] + UnlockSourceMismatch { needs_login_password: bool }, + /// No recovery is under way for this hub: sign in first. + #[error("there is nothing to recover for this hub")] + NoRecoveryPending, + /// The account keys are not open on this device: unlock the vault or sign in first. + #[error("the account keys are not open on this device")] + KeysLocked, + /// The credential file of this device could not be read or written. + #[error("the credentials of this device cannot be kept")] + Credentials { + #[source] + cause: SharedCause, + }, + /// Any other failure of the hub client. + #[error("the hub request failed")] + Remote { + #[source] + cause: SharedCause, + }, +} diff --git a/crates/hubs-model/src/lib.rs b/crates/hubs-model/src/lib.rs new file mode 100644 index 00000000..022a9445 --- /dev/null +++ b/crates/hubs-model/src/lib.rs @@ -0,0 +1,66 @@ +//! The hubs this device knows and the neutral ports through which the client reaches a hub. +//! +//! Owns: `Environment` (what a window shows: this computer or one hub), hub addresses and ids, +//! certificate fingerprints and the trust decision, captured descriptive hub/account source +//! identities (`source_identity`, distinct from address and certificate observations), +//! the colour that marks a server +//! (`HubColor`), the `[hubs]` settings section (`HubsSettings`), the hub access +//! ports `HubTrust`, `HubSignIn` with their records and `HubError`, +//! including the status of the account keys after sign-in (`KeyStatus`), opaque +//! recovery issue/acknowledgement capabilities (`RecoveryKeyReissue`, `RecoveryKeyOffer`), +//! and `LoginReceipt` for original-login cleanup after delayed settings publication. +//! Does not own: the hub wire protocol and its SDK types (the hub contract crates, reached +//! only by `nocterm-hub-io`), device keys and tokens (private adapter files), connection +//! state over time (`nocterm-hubs`), views. +//! Ports provided: HubTrust, HubSignIn, AccountKeyLock (the account keys and their lock policy) +//! and AccountDevices (the devices of an account, listed and revoked). Ports used: +//! nocterm-session `Secret`. +//! Settings section: `hubs`. Globals: none. +//! Invariants: no I/O; no protocol types; a hub is identified by its canonical address; +//! captured source identities include hub authority and account-key fingerprints, never display +//! addresses or local project ids; their constructors do not authenticate directory values; +//! displayed confirmations carry injected original-recipient capabilities, never authority +//! reconstructed from display ids; stale recipient/action errors stay typed; +//! a pinned fingerprint never changes silently: a different certificate is an error, not a +//! prompt; plaintext addresses are refused; secrets never appear in `HubsSettings`. +//! +//! With the `test-support` feature, `testing::FakeHub` implements the two access ports in +//! memory for consumers' tests. + +//! LocalAccountSource/LocalAccountSourceFactory are consumer ports for captured native +//! account owners. Identity constructors validate shape; adapters authenticate values, and +//! lease checks prevent old owners from returning after sign-out or key replacement. + +pub mod account_keys; +pub mod address; +pub mod color; +pub mod devices; +pub mod environment; +pub mod error; +mod login; +pub use login::{LoginOwner, LoginReceipt}; +pub mod ports; +pub mod records; +mod recovery; +pub use recovery::{ + RecoveryKeyConfirmation, RecoveryKeyIssuer, RecoveryKeyOffer, RecoveryKeyReissue, +}; +pub mod settings; +pub mod source_identity; +pub mod trust; + +#[cfg(any(test, feature = "test-support"))] +pub mod testing; + +pub use account_keys::{AccountKeyError, AccountKeyFuture, AccountKeyLock}; +pub use address::{DEFAULT_PORT, HubAddress, HubId}; +pub use color::HubColor; +pub use devices::{AccountDevices, DeviceSummary}; +pub use environment::Environment; +pub use error::{FingerprintError, HubAddressError, HubError}; +pub use ports::{HubFuture, HubSignIn, HubTrust, OidcSignIn}; +pub use records::{ + Account, AccountId, HubEndpointSpec, HubInfo, KeyStatus, OidcProvider, SignedIn, +}; +pub use settings::{HubRecord, HubsSettings}; +pub use trust::{Fingerprint, ProbedHub, TrustDecision, TrustPolicy, decide}; diff --git a/crates/hubs-model/src/login.rs b/crates/hubs-model/src/login.rs new file mode 100644 index 00000000..ca6b17fb --- /dev/null +++ b/crates/hubs-model/src/login.rs @@ -0,0 +1,41 @@ +//! Conditional cleanup of the original login after a consumer's delayed publication. +use crate::{HubError, HubId}; +use std::{fmt, sync::Arc}; + +/// The adapter retains native membership and local file-login ownership. +pub trait LoginOwner: Send + Sync { + fn check_current(&self) -> Result<(), HubError>; + fn discard(&self) -> Result<(), HubError>; +} +/// A successful login's original owner, independent from the vault key lifecycle. +#[derive(Clone)] +pub struct LoginReceipt { + hub: HubId, + owner: Arc, +} +impl LoginReceipt { + pub fn new(hub: HubId, owner: Arc) -> Self { + Self { hub, owner } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn check_current(&self) -> Result<(), HubError> { + self.owner.check_current() + } + pub fn discard(&self) -> Result<(), HubError> { + self.owner.discard() + } +} +impl fmt::Debug for LoginReceipt { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("LoginReceipt") + .field("hub", &self.hub) + .finish_non_exhaustive() + } +} +impl PartialEq for LoginReceipt { + fn eq(&self, other: &Self) -> bool { + self.hub == other.hub && Arc::ptr_eq(&self.owner, &other.owner) + } +} diff --git a/crates/hubs-model/src/ports.rs b/crates/hubs-model/src/ports.rs new file mode 100644 index 00000000..1ec60623 --- /dev/null +++ b/crates/hubs-model/src/ports.rs @@ -0,0 +1,94 @@ +//! The hub access ports. The application's hub adapter implements them; services use them. +//! +//! Every method returns a runtime-agnostic future that can be awaited on any executor. + +use futures::future::BoxFuture; +use nocterm_session::Secret; + +use crate::address::{HubAddress, HubId}; +use crate::error::HubError; +use crate::records::{HubEndpointSpec, HubInfo, KeyStatus, SignedIn}; +use crate::trust::ProbedHub; + +/// The outcome of one hub call. +pub type HubFuture = BoxFuture<'static, Result>; + +/// Reads the certificate of an address nobody has decided to trust yet. +pub trait HubTrust: Send + Sync { + /// Completes a TLS handshake that accepts any certificate and reports its fingerprint and + /// whether a public authority vouches for it. Sends no credentials and makes no hub call. + fn probe(&self, address: &HubAddress) -> HubFuture; +} + +/// A single sign-in through an identity provider, in two parts. +pub struct OidcSignIn { + /// Resolves with the address the user must open in a browser, once the sign-in listens. + pub browser: HubFuture, + /// Resolves when the browser sign-in finishes. Dropping it cancels the sign-in and closes + /// its local listener. + pub done: HubFuture, +} + +/// Connecting to a hub and signing in or out, with the trust the caller decided. +pub trait HubSignIn: Send + Sync { + /// Connects with the decided trust and reads what the hub says of itself. The returned + /// `signed_in` says whether this device already holds working credentials; expired tokens + /// are renewed on the way. A certificate that differs from the pinned one is + /// [`HubError::CertificateChanged`]; a revoked device erases its credentials and is + /// [`HubError::Revoked`]. + fn connect(&self, endpoint: HubEndpointSpec) -> HubFuture; + + /// Signs in with a user name and password and keeps the credentials on this device. The + /// password also opens the account's keys, or creates them on a first sign-in: the answer + /// says which ([`SignedIn::keys`]). + fn password( + &self, + endpoint: HubEndpointSpec, + username: String, + password: Secret, + ) -> HubFuture; + + /// Signs in through the identity provider `provider` and keeps the credentials. The keys + /// stay closed until [`HubSignIn::set_vault_passphrase`] opens them. + fn oidc(&self, endpoint: HubEndpointSpec, provider: String) -> OidcSignIn; + + /// Opens (or, on the first time, creates) the account's keys with a vault passphrase, for an + /// account that signed in without a password. A passphrase that does not open the keys + /// leaves them waiting for the recovery key ([`KeyStatus::RecoveryNeeded`]). + fn set_vault_passphrase(&self, hub: &HubId, passphrase: Secret) -> HubFuture; + + /// Opens the keys that wait for the recovery key, and makes the secret typed at sign-in + /// open them from now on. [`HubError::RecoveryKeyInvalid`] when the key is not the one. + fn recover(&self, hub: &HubId, recovery_key: Secret) -> HubFuture<()>; + + /// Whether the recovery key made when the account's keys were created on this device was + /// never confirmed as saved, and the keys are open: a new one can be issued. + fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool; + + /// How many times the account keys of any hub were opened, dropped or replaced so far. Start + /// from it to follow [`HubSignIn::keys_changed`]. + fn keys_revision(&self) -> u64; + + /// Resolves with the new count once it differs from `seen`: the keys of some hub were + /// opened (a vault unlock, a sign-in), dropped or replaced, so what depends on them, such as + /// [`HubSignIn::recovery_key_unconfirmed`], should be read again. + fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64>; + + /// Captures the current recipient and pending recovery generation for a displayed action. + fn recovery_key_offer(&self, hub: &HubId) -> Result; + + /// Issues through a capability captured before the future is returned. + fn reissue_recovery_key(&self, hub: &HubId) -> HubFuture { + match self.recovery_key_offer(hub) { + Ok(offer) => offer.issue(), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + /// Ends this device's session on the hub and erases its credentials, even when the hub + /// cannot be reached. + fn sign_out(&self, hub: &HubId) -> HubFuture<()>; + + /// Erases this device's credentials for the hub without contacting it. + fn forget(&self, hub: &HubId) -> Result<(), HubError>; +} diff --git a/crates/hubs-model/src/records.rs b/crates/hubs-model/src/records.rs new file mode 100644 index 00000000..7ac95eef --- /dev/null +++ b/crates/hubs-model/src/records.rs @@ -0,0 +1,104 @@ +//! Plain data the hub access ports exchange. None of it carries a secret. + +use std::fmt; + +use crate::address::HubAddress; +use crate::trust::TrustPolicy; + +macro_rules! text_id { + ($(#[$meta:meta])* $name:ident) => { + $(#[$meta])* + #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] + pub struct $name(String); + + impl $name { + pub fn new(id: impl Into) -> Self { + Self(id.into()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } + } + + impl fmt::Display for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } + } + }; +} + +text_id!( + /// An account on a hub. + AccountId +); + +/// A hub and the trust decided for it; the adapter refuses a certificate that does not match. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubEndpointSpec { + pub address: HubAddress, + pub trust: TrustPolicy, +} + +/// An identity provider the hub offers for single sign-on. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct OidcProvider { + pub id: String, + pub display_name: String, +} + +/// What a hub says of itself, and whether this device is signed in to it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubInfo { + pub name: String, + /// The hub accepts a user name and password. + pub password_login: bool, + pub oidc_providers: Vec, + /// The hub has an owner. A hub without one cannot be signed in to from this client. + pub bootstrapped: bool, + /// This device holds working credentials for the hub. + pub signed_in: bool, +} + +/// Who this device is signed in as. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Account { + pub id: AccountId, + pub username: String, + pub display_name: String, + /// The account administers the hub (its users, server-wide providers and limits). + pub admin: bool, +} + +/// What a sign-in leaves to do about the account's keys. +/// +/// The keys protect what the account keeps in its vault on the hub (saved servers and their +/// credentials); the hub never holds them. Chats and agents never need them. They are opened with the login password, or with a vault passphrase for an +/// account that signs in through a provider. +#[derive(Clone, Debug, PartialEq)] +pub enum KeyStatus { + /// The keys are open: the account's data on the hub can be read. + Open, + /// The keys are open, but the recovery key made with them was never confirmed as saved + /// (the dialog was closed or the client stopped before): a new one can be issued, because + /// the open keys may replace the recovery envelope. + RecoveryKeyUnconfirmed(crate::RecoveryKeyReissue), + /// The account had no keys; they were made now. The recovery key is the only way back + /// after the password is lost, so it must be shown once and kept nowhere by the client. + Created(crate::RecoveryKeyOffer), + /// A browser sign-in cannot open the keys: the user types the vault passphrase. + PassphraseNeeded, + /// The keys exist but the password does not open them (it was reset): the recovery key + /// does. + RecoveryNeeded, +} + +/// A finished sign-in. +#[derive(Clone, Debug, PartialEq)] +pub struct SignedIn { + pub account: Account, + pub keys: KeyStatus, + /// Original login authority for delayed publication or conditional cleanup. + pub login: crate::LoginReceipt, +} diff --git a/crates/hubs-model/src/recovery.rs b/crates/hubs-model/src/recovery.rs new file mode 100644 index 00000000..4195e190 --- /dev/null +++ b/crates/hubs-model/src/recovery.rs @@ -0,0 +1,85 @@ +//! The displayed recovery key retains its original recipient and acknowledgement. +use crate::{HubError, HubFuture, HubId}; +use nocterm_session::Secret; +use std::{fmt, sync::Arc}; + +/// Native authority for acknowledging exactly one displayed recovery key. +pub trait RecoveryKeyConfirmation: Send + Sync { + fn check_current(&self) -> Result<(), HubError>; + fn confirm(&self) -> HubFuture<()>; +} + +#[derive(Clone)] +pub struct RecoveryKeyOffer { + hub: HubId, + key: Secret, + confirmation: Arc, +} +impl RecoveryKeyOffer { + /// The injected capability establishes authority; this wrapper does not authenticate it. + pub fn new(hub: HubId, key: Secret, confirmation: Arc) -> Self { + Self { + hub, + key, + confirmation, + } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn key(&self) -> &Secret { + &self.key + } + pub fn confirm(&self) -> HubFuture<()> { + self.confirmation.confirm() + } + pub fn check_current(&self) -> Result<(), HubError> { + self.confirmation.check_current() + } +} +impl fmt::Debug for RecoveryKeyOffer { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("RecoveryKeyOffer").finish_non_exhaustive() + } +} +impl PartialEq for RecoveryKeyOffer { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.confirmation, &other.confirmation) + } +} + +/// Native authority to replace the recovery envelope of one original open owner. +pub trait RecoveryKeyIssuer: Send + Sync { + fn check_current(&self) -> Result<(), HubError>; + fn issue(&self) -> HubFuture; +} + +#[derive(Clone)] +pub struct RecoveryKeyReissue { + hub: HubId, + issuer: Arc, +} +impl RecoveryKeyReissue { + pub fn new(hub: HubId, issuer: Arc) -> Self { + Self { hub, issuer } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn issue(&self) -> HubFuture { + self.issuer.issue() + } + pub fn check_current(&self) -> Result<(), HubError> { + self.issuer.check_current() + } +} +impl fmt::Debug for RecoveryKeyReissue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("RecoveryKeyReissue").finish_non_exhaustive() + } +} +impl PartialEq for RecoveryKeyReissue { + fn eq(&self, other: &Self) -> bool { + self.hub == other.hub && Arc::ptr_eq(&self.issuer, &other.issuer) + } +} diff --git a/crates/hubs-model/src/settings.rs b/crates/hubs-model/src/settings.rs new file mode 100644 index 00000000..998bb99f --- /dev/null +++ b/crates/hubs-model/src/settings.rs @@ -0,0 +1,267 @@ +//! The `[hubs]` section: the servers this device knows. It holds no secrets. +//! +//! ```toml +//! [[hubs.servers]] +//! address = "hub.home:7443" +//! fingerprint = "ab:cd:…" # pins a self-signed certificate +//! label = "Home" +//! color = "blue" # optional: red, yellow, green, cyan, blue or magenta +//! +//! [[hubs.servers]] +//! address = "hub.example.com" +//! trust = "public-ca" # any certificate a public authority vouches for +//! +//! [hubs] +//! key_lock_minutes = 0 # the account keys of a hub stay open until locked or signed out +//! ``` + +use std::collections::HashSet; + +use nocterm_settings::SettingsSection; +use schemars::JsonSchema; +use serde::{Deserialize, Deserializer, Serialize}; + +use crate::address::{HubAddress, HubId}; +use crate::color::HubColor; +use crate::trust::{Fingerprint, TrustPolicy}; + +/// Longest label kept, in characters. +const MAX_LABEL_CHARS: usize = 64; + +/// One known server. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[serde(try_from = "RawRecord", into = "RawRecord")] +#[schemars(with = "RawRecord")] +pub struct HubRecord { + pub address: HubAddress, + pub trust: TrustPolicy, + /// How the server is named in menus; empty shows the address. + pub label: String, + /// The colour that marks the server in menus; none leaves it unmarked. + pub color: Option, +} + +impl HubRecord { + pub fn new(address: HubAddress, trust: TrustPolicy, label: impl Into) -> Self { + Self { + address, + trust, + label: clean_label(&label.into()), + color: None, + } + } + + /// The record marked with `color`. + pub fn with_color(mut self, color: Option) -> Self { + self.color = color; + self + } + + pub fn id(&self) -> HubId { + HubId::from(self.address.clone()) + } + + /// The label, or the address when there is none. + pub fn display_label(&self) -> String { + if self.label.is_empty() { + self.address.to_string() + } else { + self.label.clone() + } + } +} + +fn clean_label(label: &str) -> String { + let printable: String = label.chars().filter(|c| !c.is_control()).collect(); + printable.trim().chars().take(MAX_LABEL_CHARS).collect() +} + +/// The shape of one `[[hubs.servers]]` table. +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] +struct RawRecord { + /// The server as `host` or `host:port`; the port defaults to 7443. + address: String, + /// SHA-256 fingerprint of the server's certificate, in hex with or without colons. + #[serde(default, skip_serializing_if = "Option::is_none")] + fingerprint: Option, + /// `public-ca` trusts certificates that public authorities vouch for. + #[serde(default, skip_serializing_if = "Option::is_none")] + trust: Option, + /// How the server is named in menus. + #[serde(default, skip_serializing_if = "String::is_empty")] + label: String, + /// `red`, `yellow`, `green`, `cyan`, `blue` or `magenta`. A name this version does not know + /// leaves the server unmarked. + #[serde( + default, + deserialize_with = "lenient_color", + skip_serializing_if = "Option::is_none" + )] + #[schemars(with = "Option")] + color: Option, +} + +/// Reads a colour this version knows; anything else is no colour, so a newer file does not +/// make the server unusable. +fn lenient_color<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + #[derive(Deserialize)] + #[serde(untagged)] + enum Entry { + Known(HubColor), + Unusable(serde::de::IgnoredAny), + } + Ok(match Entry::deserialize(deserializer)? { + Entry::Known(color) => Some(color), + Entry::Unusable(_) => None, + }) +} + +#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "kebab-case")] +enum RawTrust { + PublicCa, +} + +/// Why a table is not a usable server. +#[derive(Debug, thiserror::Error)] +enum RecordError { + #[error("invalid address")] + Address(#[from] crate::error::HubAddressError), + #[error("invalid fingerprint")] + Fingerprint(#[from] crate::error::FingerprintError), + #[error("neither a fingerprint nor `trust = \"public-ca\"`")] + NoTrust, +} + +impl TryFrom for HubRecord { + type Error = RecordError; + + fn try_from(raw: RawRecord) -> Result { + // A fingerprint is the stricter statement, so it wins over `trust`. + let trust = match (raw.fingerprint, raw.trust) { + (Some(fingerprint), _) => TrustPolicy::Pinned(Fingerprint::parse(&fingerprint)?), + (None, Some(RawTrust::PublicCa)) => TrustPolicy::PublicCa, + (None, None) => return Err(RecordError::NoTrust), + }; + Ok(Self::new(HubAddress::parse(&raw.address)?, trust, raw.label).with_color(raw.color)) + } +} + +impl From for RawRecord { + fn from(record: HubRecord) -> Self { + let (fingerprint, trust) = match record.trust { + TrustPolicy::Pinned(fingerprint) => (Some(fingerprint.to_string()), None), + TrustPolicy::PublicCa => (None, Some(RawTrust::PublicCa)), + }; + Self { + address: record.address.to_string(), + fingerprint, + trust, + label: record.label, + color: record.color, + } + } +} + +/// The servers this device has signed in to. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[serde(default)] +pub struct HubsSettings { + /// Known servers in the order they were added. A table that is not a usable server is + /// ignored without affecting the others. + #[serde(deserialize_with = "lenient_servers")] + pub servers: Vec, + /// Close the account keys of a hub after this many minutes without vault use (at most + /// 1440); 0 keeps them open until the person locks them or signs out. Independent of the + /// local vault's auto-lock: chats and agents never need the keys. + pub key_lock_minutes: u32, +} + +/// The longest wait before the account keys of a hub close. +pub const MAX_KEY_LOCK_MINUTES: u32 = 1440; + +/// Reads every table that is a usable server and skips the rest. +fn lenient_servers<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + #[derive(Deserialize)] + #[serde(untagged)] + enum Entry { + Server(HubRecord), + Unusable(serde::de::IgnoredAny), + } + let entries = Vec::::deserialize(deserializer)?; + Ok(entries + .into_iter() + .filter_map(|entry| match entry { + Entry::Server(record) => Some(record), + Entry::Unusable(_) => None, + }) + .collect()) +} + +impl HubsSettings { + /// How long the account keys stay open without vault use; `None` keeps them open. + pub fn key_lock(&self) -> Option { + (self.key_lock_minutes > 0) + .then(|| std::time::Duration::from_secs(u64::from(self.key_lock_minutes) * 60)) + } + + pub fn find(&self, hub: &HubId) -> Option<&HubRecord> { + self.servers.iter().find(|record| record.id() == *hub) + } + + /// Adds a server, or replaces the one with the same address in place. + pub fn upsert(&mut self, record: HubRecord) { + match self + .servers + .iter_mut() + .find(|existing| existing.address == record.address) + { + Some(existing) => *existing = record, + None => self.servers.push(record), + } + } + + /// Renames a server; empty shows its address. `true` when it was known. + pub fn set_label(&mut self, hub: &HubId, label: &str) -> bool { + self.edit(hub, |record| record.label = clean_label(label)) + } + + /// Marks a server with a colour, or clears the mark. `true` when it was known. + pub fn set_color(&mut self, hub: &HubId, color: Option) -> bool { + self.edit(hub, |record| record.color = color) + } + + fn edit(&mut self, hub: &HubId, change: impl FnOnce(&mut HubRecord)) -> bool { + match self.servers.iter_mut().find(|record| record.id() == *hub) { + Some(record) => { + change(record); + true + } + None => false, + } + } + + /// Forgets a server; `true` when it was known. + pub fn remove(&mut self, hub: &HubId) -> bool { + let before = self.servers.len(); + self.servers.retain(|record| record.id() != *hub); + self.servers.len() != before + } +} + +impl SettingsSection for HubsSettings { + const KEY: &'static str = "hubs"; + + /// Keeps the first server of each address, shortens labels and bounds the key lock. + fn sanitize(&mut self) { + self.key_lock_minutes = self.key_lock_minutes.min(MAX_KEY_LOCK_MINUTES); + let mut seen = HashSet::new(); + self.servers.retain(|record| seen.insert(record.id())); + for record in &mut self.servers { + record.label = clean_label(&record.label); + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/settings/tests.rs b/crates/hubs-model/src/settings/tests.rs new file mode 100644 index 00000000..8e53fa3a --- /dev/null +++ b/crates/hubs-model/src/settings/tests.rs @@ -0,0 +1,247 @@ +use super::*; + +const PRINT: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"; + +fn load(text: &str) -> HubsSettings { + let table: toml::Table = text.parse().unwrap(); + let mut settings = table + .get("hubs") + .map(|hubs| hubs.clone().try_into::().unwrap()) + .unwrap_or_default(); + settings.sanitize(); + settings +} + +fn pinned(address: &str, label: &str) -> HubRecord { + HubRecord::new( + HubAddress::parse(address).unwrap(), + TrustPolicy::Pinned(Fingerprint::parse(PRINT).unwrap()), + label, + ) +} + +#[test] +fn servers_are_read_with_a_pinned_fingerprint_or_public_trust() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "Hub.Home" +fingerprint = "{PRINT}" +label = "Home" + +[[hubs.servers]] +address = "https://hub.example.com:8443/" +trust = "public-ca" +"# + )); + assert_eq!(settings.servers.len(), 2); + assert_eq!(settings.servers[0], pinned("hub.home:7443", "Home")); + assert_eq!(settings.servers[1].trust, TrustPolicy::PublicCa); + assert_eq!( + settings.servers[1].address.to_string(), + "hub.example.com:8443" + ); + assert_eq!(settings.servers[1].display_label(), "hub.example.com:8443"); +} + +#[test] +fn a_broken_server_does_not_drop_the_others() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "http://plain.example.com" +trust = "public-ca" + +[[hubs.servers]] +address = "no trust.example.com" + +[[hubs.servers]] +address = "hub.home" +fingerprint = "not-hex" + +[[hubs.servers]] +address = "hub.home" +trust = "everyone" + +[[hubs.servers]] +address = "kept.home" +fingerprint = "{PRINT}" +"# + )); + assert_eq!(settings.servers, vec![pinned("kept.home", "")]); +} + +#[test] +fn servers_with_the_same_address_collapse_to_the_first() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "hub.home" +fingerprint = "{PRINT}" +label = "First" + +[[hubs.servers]] +address = "HUB.home:7443" +trust = "public-ca" +label = "Second" +"# + )); + assert_eq!(settings.servers, vec![pinned("hub.home", "First")]); +} + +#[test] +fn a_fingerprint_wins_over_public_trust_in_one_table() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "hub.home" +fingerprint = "{PRINT}" +trust = "public-ca" +"# + )); + assert_eq!(settings.servers, vec![pinned("hub.home", "")]); +} + +#[test] +fn saving_writes_only_the_known_fields() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("hub.home", "Home")); + settings.upsert(HubRecord::new( + HubAddress::parse("hub.example.com").unwrap(), + TrustPolicy::PublicCa, + "", + )); + let text = toml::to_string(&toml::Table::from_iter([( + "hubs".to_owned(), + toml::Value::try_from(&settings).unwrap(), + )])) + .unwrap(); + assert!(text.contains("address = \"hub.home:7443\"")); + assert!(text.contains("trust = \"public-ca\"")); + assert!(!text.contains("label = \"\""), "{text}"); + assert!(!text.contains("token") && !text.contains("seed"), "{text}"); + assert_eq!(load(&text), settings); +} + +#[test] +fn upsert_replaces_in_place_and_remove_forgets() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("a.home", "A")); + settings.upsert(pinned("b.home", "B")); + settings.upsert(pinned("a.home", "Renamed")); + assert_eq!( + settings + .servers + .iter() + .map(HubRecord::display_label) + .collect::>(), + ["Renamed", "B"] + ); + let id = HubId::from(HubAddress::parse("a.home").unwrap()); + assert!(settings.find(&id).is_some()); + assert!(settings.remove(&id)); + assert!(!settings.remove(&id)); + assert!(settings.find(&id).is_none()); +} + +#[test] +fn labels_lose_control_characters_and_are_shortened() { + let long = "x".repeat(200); + let record = pinned("hub.home", &format!(" Ho\nme\t{long}")); + assert_eq!(record.label.chars().count(), 64); + assert!(record.label.starts_with("Home")); + assert!(!record.label.contains('\n')); +} + +#[test] +fn a_missing_section_means_no_servers() { + assert_eq!(load(""), HubsSettings::default()); + assert_eq!(HubsSettings::KEY, "hubs"); +} + +#[test] +fn a_colour_is_read_written_and_survives_a_round_trip() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("hub.home", "Home").with_color(Some(HubColor::Blue))); + settings.upsert(pinned("plain.home", "Plain")); + let text = toml::to_string(&toml::Table::from_iter([( + "hubs".to_owned(), + toml::Value::try_from(&settings).unwrap(), + )])) + .unwrap(); + assert!(text.contains("color = \"blue\""), "{text}"); + assert_eq!( + text.matches("color").count(), + 1, + "unmarked servers write none" + ); + assert_eq!(load(&text), settings); +} + +#[test] +fn a_colour_this_version_does_not_know_leaves_the_server_unmarked_but_listed() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "future.home" +fingerprint = "{PRINT}" +color = "chartreuse" + +[[hubs.servers]] +address = "odd.home" +fingerprint = "{PRINT}" +color = 7 +label = "Odd" +unheard_of = true + +[[hubs.servers]] +address = "kept.home" +fingerprint = "{PRINT}" +color = "green" +"# + )); + assert_eq!( + settings.servers, + vec![ + pinned("future.home", ""), + pinned("odd.home", "Odd"), + pinned("kept.home", "").with_color(Some(HubColor::Green)), + ] + ); +} + +#[test] +fn editing_a_server_changes_only_that_server() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("a.home", "A")); + settings.upsert(pinned("b.home", "B")); + let a = HubId::from(HubAddress::parse("a.home").unwrap()); + let unknown = HubId::from(HubAddress::parse("c.home").unwrap()); + assert!(settings.set_label(&a, " \u{7}Renamed ")); + assert!(settings.set_color(&a, Some(HubColor::Red))); + assert!(!settings.set_label(&unknown, "x")); + assert!(!settings.set_color(&unknown, Some(HubColor::Red))); + assert_eq!( + settings.servers, + vec![ + pinned("a.home", "Renamed").with_color(Some(HubColor::Red)), + pinned("b.home", "B") + ] + ); + assert!(settings.set_color(&a, None)); + assert_eq!(settings.find(&a).unwrap().color, None); +} + +#[test] +fn the_hub_keys_stay_open_by_default_and_their_lock_is_bounded() { + let mut settings = HubsSettings::default(); + assert_eq!(settings.key_lock(), None); + settings.key_lock_minutes = 10_000; + settings.sanitize(); + assert_eq!( + settings.key_lock(), + Some(std::time::Duration::from_secs(1440 * 60)) + ); + let read: HubsSettings = toml::from_str("key_lock_minutes = 30").unwrap(); + assert_eq!(read.key_lock(), Some(std::time::Duration::from_secs(1800))); +} diff --git a/crates/hubs-model/src/source_identity.rs b/crates/hubs-model/src/source_identity.rs new file mode 100644 index 00000000..be4210c6 --- /dev/null +++ b/crates/hubs-model/src/source_identity.rs @@ -0,0 +1,121 @@ +//! Captured descriptive source identity shared by chat, storage and cloud owners. +//! Constructors check shape only; trusted adapters authenticate the supplied values. +use serde::{Deserialize, Serialize}; +use std::fmt; +mod local; +pub use local::{ + LegacyConfiguredAccount, LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, + LocalAccountSourceFactory, +}; + +pub const MAX_NAMESPACE_BYTES: usize = 128; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum SourceIdentityError { + #[error("an opaque namespace identifier must contain 1 to 128 UTF-8 bytes")] + NamespaceLength, +} + +/// Supplied digest/fingerprint bytes, not a key validator or signature verifier. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub struct Fingerprint([u8; 32]); +impl Fingerprint { + pub const fn from_bytes(bytes: [u8; 32]) -> Self { + Self(bytes) + } + pub const fn as_bytes(&self) -> &[u8; 32] { + &self.0 + } +} + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(try_from = "String", into = "String")] +pub struct NamespaceId(String); +impl NamespaceId { + pub fn new(value: impl Into) -> Result { + let value = value.into(); + if value.is_empty() || value.len() > MAX_NAMESPACE_BYTES { + return Err(SourceIdentityError::NamespaceLength); + } + Ok(Self(value)) + } + pub fn as_str(&self) -> &str { + &self.0 + } +} +impl TryFrom for NamespaceId { + type Error = SourceIdentityError; + fn try_from(value: String) -> Result { + Self::new(value) + } +} +impl From for String { + fn from(value: NamespaceId) -> Self { + value.0 + } +} +impl fmt::Debug for NamespaceId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("NamespaceId").field(&self.0.len()).finish() + } +} + +/// An instance label bound to its originally authenticated authority, not its address. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct HubIdentity { + instance: NamespaceId, + authority: Fingerprint, +} +impl HubIdentity { + pub fn new(instance: NamespaceId, authority: Fingerprint) -> Self { + Self { + instance, + authority, + } + } + pub fn instance(&self) -> &NamespaceId { + &self.instance + } + pub fn authority(&self) -> Fingerprint { + self.authority + } +} + +/// Captured account ID and expected account-key identity from the original installation. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AccountIdentity { + id: NamespaceId, + key: Fingerprint, +} +impl AccountIdentity { + pub fn new(id: NamespaceId, key: Fingerprint) -> Self { + Self { id, key } + } + pub fn id(&self) -> &NamespaceId { + &self.id + } + pub fn key(&self) -> Fingerprint { + self.key + } +} + +/// A descriptive account partition. Construction is not account authentication. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AccountScope { + hub: HubIdentity, + account: AccountIdentity, +} +impl AccountScope { + pub fn new(hub: HubIdentity, account: AccountIdentity) -> Self { + Self { hub, account } + } + pub fn hub(&self) -> &HubIdentity { + &self.hub + } + pub fn account(&self) -> &AccountIdentity { + &self.account + } +} diff --git a/crates/hubs-model/src/source_identity/local.rs b/crates/hubs-model/src/source_identity/local.rs new file mode 100644 index 00000000..46273736 --- /dev/null +++ b/crates/hubs-model/src/source_identity/local.rs @@ -0,0 +1,63 @@ +//! Captured local-history partition and validity; no Cloud authority is inferred. +use super::{AccountScope, Fingerprint, NamespaceId}; +use crate::HubId; +use serde::{Deserialize, Serialize}; + +/// Old hubs have no authenticated stable instance authority. Their local history +/// is separated by retained configuration, authenticated account and account key. +/// Address aliases may have distinct partitions; this is never a Cloud identity. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct LegacyConfiguredAccount { + hub: HubId, + account: NamespaceId, + key: Fingerprint, +} +impl LegacyConfiguredAccount { + pub fn new(hub: HubId, account: NamespaceId, key: Fingerprint) -> Self { + Self { hub, account, key } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn account(&self) -> &NamespaceId { + &self.account + } + pub fn key(&self) -> Fingerprint { + self.key + } +} + +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde( + tag = "kind", + content = "identity", + rename_all = "snake_case", + deny_unknown_fields +)] +pub enum LocalAccountPartition { + Stable(AccountScope), + LegacyConfigured(LegacyConfiguredAccount), +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum LocalAccountSourceError { + #[error("the captured hub account is no longer signed in or unlocked")] + Stale, + #[error("the captured hub account identity is unavailable")] + Unavailable, +} + +/// Authenticated adapter-owned capture. A plain partition constructor is not +/// evidence of authentication. Ordinary token refresh retains the source; +/// sign-out, replacement login, key rotation and lock invalidate the old capture. +pub trait LocalAccountSource: Send + Sync { + fn partition(&self) -> LocalAccountPartition; + fn check_current(&self) -> Result<(), LocalAccountSourceError>; +} + +/// Captures only a new attempt; existing conversations retain their original source. +/// Unlock/sign-in can make later captures available without rewriting old handles. +pub trait LocalAccountSourceFactory: Send + Sync { + fn capture(&self) -> Result, crate::HubError>; +} diff --git a/crates/hubs-model/src/testing.rs b/crates/hubs-model/src/testing.rs new file mode 100644 index 00000000..eaaf5e85 --- /dev/null +++ b/crates/hubs-model/src/testing.rs @@ -0,0 +1,674 @@ +//! An in-memory hub for tests: one fake behind the access ports. +//! +//! Enable `test-support` in a consumer's dev-dependencies. The fake does no I/O and keeps its +//! "hubs" in memory. A call completes when it is polled; [`FakeHub::hold`] makes the next call +//! of one kind wait until the test releases it, so a test can observe the state in between. + +use std::collections::{BTreeMap, BTreeSet, VecDeque}; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::FutureExt as _; +use futures::channel::oneshot; +use futures::future::BoxFuture; +use nocterm_core::error::SharedCause; +use nocterm_session::Secret; + +use crate::address::{HubAddress, HubId}; +use crate::error::HubError; +use crate::ports::{HubFuture, HubSignIn, HubTrust, OidcSignIn}; +use crate::records::{Account, HubEndpointSpec, HubInfo, KeyStatus, SignedIn}; +use crate::trust::{ProbedHub, TrustPolicy}; + +mod account_keys; +mod devices; +mod local_source; +mod login; +mod recovery; +mod server; +pub use local_source::ScriptedLocalAccountFactory; + +pub use account_keys::FakeAccountKeys; +pub use devices::FakeAccountDevices; +pub use server::{FakeKeyset, FakeServer, FakeUser}; + +/// The protocol version the fake client speaks. +pub const CLIENT_PROTOCOL: u32 = 1; + +/// The kinds of call a test can observe, hold or make fail. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum FakeOp { + Probe, + Connect, + Password, + Oidc, + SetVaultPassphrase, + Recover, + ReissueRecoveryKey, + ConfirmRecoveryKey, + SignOut, + Forget, +} + +/// One call the fake received. Secrets are not recorded. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum FakeCall { + Probe(HubAddress), + Connect(HubId), + Password { hub: HubId, username: String }, + Oidc { hub: HubId, provider: String }, + SetVaultPassphrase(HubId), + Recover(HubId), + ReissueRecoveryKey(HubId), + ConfirmRecoveryKey(HubId), + SignOut(HubId), + Forget(HubId), +} + +/// Why a fake hub did not answer. +#[derive(Debug, thiserror::Error)] +#[error("the fake hub is offline")] +pub struct FakeOffline; + +/// An in-memory hub implementing [`HubTrust`] and [`HubSignIn`]. +/// +/// Clones share one state, so a test keeps a handle while a service owns another. +#[derive(Clone, Default)] +pub struct FakeHub { + state: Arc>, +} + +#[derive(Default)] +struct State { + servers: BTreeMap, + receipts: recovery::Receipts, + /// Hubs this device holds credentials for, and who it is signed in as. + credentials: BTreeMap, + revoked: BTreeSet, + calls: Vec, + failures: VecDeque<(FakeOp, HubError)>, + holds: Vec<(FakeOp, oneshot::Receiver<()>)>, + oidc: Option>>, + oidc_cancelled: usize, + /// Keys a sign-in could not open, waiting for the recovery key, and what should open them + /// afterwards. + recovering: BTreeMap, + /// Hubs whose recovery key was shown on this device and not confirmed as saved. + unconfirmed: BTreeSet, + /// Hubs whose account keys are closed, as after a restart until the vault is unlocked. + locked: BTreeSet, + /// How many times the keys of any hub were opened or dropped, and who waits for the next. + key_changes: u64, + key_waiters: Vec>, +} + +impl State { + fn discard(&mut self, hub: &HubId) -> Result<(), HubError> { + self.calls.push(FakeCall::Forget(hub.clone())); + let injected = self + .failures + .iter() + .position(|(op, _)| *op == FakeOp::Forget) + .and_then(|index| self.failures.remove(index)); + if let Some((_, error)) = injected { + return Err(error); + } + self.credentials.remove(hub); + self.recovering.remove(hub); + self.unconfirmed.remove(hub); + self.receipts.begin(hub); + self.keys_changed(); + Ok(()) + } + fn keys_changed(&mut self) { + self.key_changes += 1; + for waiter in self.key_waiters.drain(..) { + let _ = waiter.send(()); + } + } +} + +fn lock(state: &Mutex) -> MutexGuard<'_, State> { + state.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// Lets the held call continue when [`Release::release`] is called or the value is dropped. +#[derive(Debug)] +pub struct Release(oneshot::Sender<()>); + +impl Release { + pub fn release(self) { + let _ = self.0.send(()); + } +} + +impl FakeHub { + pub fn new() -> Self { + Self::default() + } + + /// Adds a server reachable at `server.address`. + #[must_use] + pub fn with_server(self, server: FakeServer) -> Self { + lock(&self.state) + .servers + .insert(server.address.clone(), server); + self + } + + /// Changes a server through `change`; panics when the address is not served. + pub fn edit_server(&self, address: &HubAddress, change: impl FnOnce(&mut FakeServer)) { + let mut state = lock(&self.state); + change(state.servers.get_mut(address).expect("a served address")); + } + + /// Pretends this device already signed in to `hub` as `username`. + pub fn sign_in_as(&self, hub: &HubId, username: &str) { + let mut state = lock(&self.state); + state.receipts.begin(hub); + state.credentials.insert(hub.clone(), username.to_owned()); + } + + /// Makes the hub revoke this device: its next call finds the device revoked. + /// Ends the sign-in of this device as an expired login does: the credentials are gone, the + /// account keys it holds are not. + pub fn expire_login(&self, hub: &HubId) { + lock(&self.state).credentials.remove(hub); + } + + /// Marks the recovery key shown on this device for `hub` as never confirmed, as a dialog + /// closed on that step leaves it. + pub fn leave_recovery_key_unconfirmed(&self, hub: &HubId) { + lock(&self.state).unconfirmed.insert(hub.clone()); + } + + /// Closes the account keys of `hub`, as a restart does until the vault is unlocked. + pub fn lock_keys(&self, hub: &HubId) { + let mut state = lock(&self.state); + state.receipts.rotate(hub); + if state.locked.insert(hub.clone()) { + state.keys_changed(); + } + } + + /// Opens the account keys of `hub`, as unlocking the vault does. + pub fn unlock_keys(&self, hub: &HubId) { + let mut state = lock(&self.state); + state.receipts.rotate(hub); + if state.locked.remove(hub) { + state.keys_changed(); + } + } + + pub fn revoke_device(&self, hub: &HubId) { + lock(&self.state).revoked.insert(hub.clone()); + } + + /// Whether the account keys of `hub` are open: signed in and not locked. + pub fn keys_open(&self, hub: &HubId) -> bool { + let state = lock(&self.state); + state.credentials.contains_key(hub) && !state.locked.contains(hub) + } + + pub fn is_signed_in(&self, hub: &HubId) -> bool { + lock(&self.state).credentials.contains_key(hub) + } + + /// Every call received, oldest first. + pub fn calls(&self) -> Vec { + lock(&self.state).calls.clone() + } + + /// How many calls of one kind arrived. + pub fn count(&self, op: FakeOp) -> usize { + lock(&self.state) + .calls + .iter() + .filter(|call| call.op() == op) + .count() + } + + /// The next call of kind `op` fails with `error`. + pub fn fail_next(&self, op: FakeOp, error: HubError) { + lock(&self.state).failures.push_back((op, error)); + } + + /// The next call of kind `op` waits until the returned handle releases it. + #[must_use] + pub fn hold(&self, op: FakeOp) -> Release { + let (release, wait) = oneshot::channel(); + lock(&self.state).holds.push((op, wait)); + Release(release) + } + + /// Finishes the pending browser sign-in as `username`. + pub fn complete_oidc(&self, username: &str) { + if let Some(done) = lock(&self.state).oidc.take() { + let _ = done.send(Ok(username.to_owned())); + } + } + + /// Fails the pending browser sign-in. + pub fn fail_oidc(&self, error: HubError) { + if let Some(done) = lock(&self.state).oidc.take() { + let _ = done.send(Err(error)); + } + } + + /// How many browser sign-ins were dropped before they finished. + pub fn cancelled_oidc(&self) -> usize { + lock(&self.state).oidc_cancelled + } + + /// Records the call, then runs `answer` against the state when the future is polled, + /// after any hold on `op` was released. + fn call( + &self, + op: FakeOp, + call: FakeCall, + answer: impl FnOnce(&mut State) -> Result + Send + 'static, + ) -> HubFuture { + let hold = { + let mut state = lock(&self.state); + state.calls.push(call); + let index = state.holds.iter().position(|(held, _)| *held == op); + index.map(|index| state.holds.remove(index).1) + }; + let shared = Arc::clone(&self.state); + async move { + if let Some(hold) = hold { + let _ = hold.await; + } + let mut state = lock(&shared); + let injected = state + .failures + .iter() + .position(|(failing, _)| *failing == op) + .and_then(|index| state.failures.remove(index)); + match injected { + Some((_, error)) => Err(error), + None => answer(&mut state), + } + } + .boxed() + } +} + +impl FakeCall { + fn op(&self) -> FakeOp { + match self { + Self::Probe(_) => FakeOp::Probe, + Self::Connect(_) => FakeOp::Connect, + Self::Password { .. } => FakeOp::Password, + Self::Oidc { .. } => FakeOp::Oidc, + Self::SetVaultPassphrase(_) => FakeOp::SetVaultPassphrase, + Self::Recover(_) => FakeOp::Recover, + Self::ReissueRecoveryKey(_) => FakeOp::ReissueRecoveryKey, + Self::ConfirmRecoveryKey(_) => FakeOp::ConfirmRecoveryKey, + Self::SignOut(_) => FakeOp::SignOut, + Self::Forget(_) => FakeOp::Forget, + } + } +} + +impl State { + fn unreachable(address: &HubAddress) -> HubError { + HubError::Unreachable { + address: address.clone(), + cause: SharedCause::new(FakeOffline), + } + } + + /// The server a call reaches, or why it cannot be reached. + fn reach(&self, address: &HubAddress) -> Result<&FakeServer, HubError> { + self.servers + .get(address) + .filter(|server| server.online) + .ok_or_else(|| Self::unreachable(address)) + } + + /// Checks the certificate against the decided trust, as the real adapter does. + fn verify(&self, endpoint: &HubEndpointSpec) -> Result<&FakeServer, HubError> { + let server = self.reach(&endpoint.address)?; + match endpoint.trust { + TrustPolicy::Pinned(pinned) if pinned != server.certificate => { + Err(HubError::CertificateChanged { + address: endpoint.address.clone(), + pinned, + presented: server.certificate, + }) + } + TrustPolicy::PublicCa if !server.publicly_trusted => { + Err(HubError::CertificateUntrusted { + address: endpoint.address.clone(), + presented: server.certificate, + }) + } + _ => Ok(server), + } + } + + /// The signed-in user of a hub; a revoked device loses its credentials. + fn authenticate(&mut self, hub: &HubId) -> Result<&FakeServer, HubError> { + if self.revoked.remove(hub) { + self.credentials.remove(hub); + return Err(HubError::Revoked); + } + if !self.credentials.contains_key(hub) { + return Err(HubError::NotSignedIn); + } + self.reach(hub.address()) + } + + /// What a sign-in with `secret` does to the keys of `username`: opens them, makes them (the + /// recovery key is `recovery-`) or waits for the recovery key. + fn open_keys( + &mut self, + hub: &HubId, + username: &str, + secret: &str, + shared: std::sync::Weak>, + ) -> Result { + let server = self + .servers + .get_mut(hub.address()) + .filter(|server| server.online) + .ok_or_else(|| Self::unreachable(hub.address()))?; + if !server.keyed { + return Ok(KeyStatus::Open); + } + match server.keysets.get(username) { + None => { + let recovery = format!("recovery-{username}"); + server.keysets.insert( + username.to_owned(), + FakeKeyset { + secret: secret.to_owned(), + recovery: recovery.clone(), + }, + ); + self.unconfirmed.insert(hub.clone()); + Ok(KeyStatus::Created( + recovery::Recipient::capture(shared, self, hub).offer(Secret::new(recovery)), + )) + } + Some(keyset) if keyset.secret == secret => Ok(if self.unconfirmed.contains(hub) { + KeyStatus::RecoveryKeyUnconfirmed( + recovery::Recipient::capture(shared, self, hub).issuer(), + ) + } else { + KeyStatus::Open + }), + Some(_) => { + self.recovering.insert(hub.clone(), secret.to_owned()); + Ok(KeyStatus::RecoveryNeeded) + } + } + } + + fn account_of(&self, hub: &HubId, username: &str) -> Result { + let server = self.reach(hub.address())?; + server + .users + .iter() + .find(|user| user.username == username) + .map(|user| user.account.clone()) + .ok_or(HubError::InvalidCredentials) + } +} + +impl HubTrust for FakeHub { + fn probe(&self, address: &HubAddress) -> HubFuture { + let address = address.clone(); + self.call( + FakeOp::Probe, + FakeCall::Probe(address.clone()), + move |state| { + let server = state.reach(&address)?; + Ok(ProbedHub { + fingerprint: server.certificate, + publicly_trusted: server.publicly_trusted, + }) + }, + ) + } +} + +impl HubSignIn for FakeHub { + fn connect(&self, endpoint: HubEndpointSpec) -> HubFuture { + let hub = HubId::from(endpoint.address.clone()); + self.call( + FakeOp::Connect, + FakeCall::Connect(hub.clone()), + move |state| { + let server = state.verify(&endpoint)?; + if server.protocol != CLIENT_PROTOCOL { + return Err(HubError::UpgradeRequired { + hub: server.protocol, + client: CLIENT_PROTOCOL, + }); + } + if state.revoked.remove(&hub) { + state.credentials.remove(&hub); + return Err(HubError::Revoked); + } + let signed_in = state.credentials.contains_key(&hub); + let server = state.verify(&endpoint)?; + Ok(server.info(signed_in)) + }, + ) + } + + fn password( + &self, + endpoint: HubEndpointSpec, + username: String, + password: Secret, + ) -> HubFuture { + let hub = HubId::from(endpoint.address.clone()); + let call = FakeCall::Password { + hub: hub.clone(), + username: username.clone(), + }; + let shared = Arc::downgrade(&self.state); + self.call(FakeOp::Password, call, move |state| { + let server = state.verify(&endpoint)?; + if !server.password_login { + return Err(HubError::MethodUnavailable); + } + let known = server.users.iter().any(|user| { + user.username == username && user.password.as_str() == password.expose() + }); + if !known { + return Err(HubError::InvalidCredentials); + } + state.revoked.remove(&hub); + state.receipts.begin(&hub); + state.credentials.insert(hub.clone(), username.clone()); + let account = state.account_of(&hub, &username)?; + let keys = state.open_keys(&hub, &username, password.expose(), shared.clone())?; + let login = login::receipt(shared.clone(), state, &hub); + Ok(SignedIn { + account, + keys, + login, + }) + }) + } + + fn oidc(&self, endpoint: HubEndpointSpec, provider: String) -> OidcSignIn { + let hub = HubId::from(endpoint.address.clone()); + let call = FakeCall::Oidc { + hub: hub.clone(), + provider: provider.clone(), + }; + let (browser_tx, browser) = oneshot::channel(); + let (finish, finished) = oneshot::channel(); + let shared = Arc::clone(&self.state); + let ready = { + let provider = provider.clone(); + self.call(FakeOp::Oidc, call, move |state| { + let server = state.verify(&endpoint)?; + if !server + .oidc_providers + .iter() + .any(|known| known.id == provider) + { + return Err(HubError::MethodUnavailable); + } + state.oidc = Some(finish); + let _ = browser_tx.send(format!("https://idp.fake/authorize?provider={provider}")); + Ok(()) + }) + }; + let done = async move { + let mut guard = CancelGuard { + state: Arc::clone(&shared), + finished: false, + }; + ready.await?; + let username = finished.await.map_err(|_| HubError::SignInCancelled)??; + guard.finished = true; + let mut state = lock(&shared); + state.revoked.remove(&hub); + state.receipts.begin(&hub); + state.credentials.insert(hub.clone(), username.clone()); + state.recovering.remove(&hub); + let account = state.account_of(&hub, &username)?; + let keys = if state.reach(hub.address())?.keyed { + KeyStatus::PassphraseNeeded + } else { + KeyStatus::Open + }; + let login = login::receipt(Arc::downgrade(&shared), &mut state, &hub); + Ok(SignedIn { + account, + keys, + login, + }) + } + .boxed(); + OidcSignIn { + browser: Box::pin(async move { browser.await.map_err(|_| HubError::SignInCancelled) }), + done, + } + } + + fn set_vault_passphrase(&self, hub: &HubId, passphrase: Secret) -> HubFuture { + let hub = hub.clone(); + let shared = Arc::downgrade(&self.state); + self.call( + FakeOp::SetVaultPassphrase, + FakeCall::SetVaultPassphrase(hub.clone()), + move |state| { + state.authenticate(&hub)?; + let username = state.credentials[&hub].clone(); + state.open_keys(&hub, &username, passphrase.expose(), shared) + }, + ) + } + + fn recover(&self, hub: &HubId, recovery_key: Secret) -> HubFuture<()> { + let hub = hub.clone(); + self.call( + FakeOp::Recover, + FakeCall::Recover(hub.clone()), + move |state| { + state.authenticate(&hub)?; + let username = state.credentials[&hub].clone(); + let secret = state + .recovering + .get(&hub) + .cloned() + .ok_or(HubError::NoRecoveryPending)?; + let keyset = state + .servers + .get_mut(hub.address()) + .and_then(|server| server.keysets.get_mut(&username)) + .ok_or(HubError::NoRecoveryPending)?; + if keyset.recovery != recovery_key.expose() { + return Err(HubError::RecoveryKeyInvalid); + } + keyset.secret = secret; + state.recovering.remove(&hub); + Ok(()) + }, + ) + } + + fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + let state = lock(&self.state); + state.unconfirmed.contains(hub) + && !state.recovering.contains_key(hub) + && !state.locked.contains(hub) + } + + fn keys_revision(&self) -> u64 { + lock(&self.state).key_changes + } + + fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64> { + let state = Arc::clone(&self.state); + Box::pin(async move { + loop { + let waiting = { + let mut state = lock(&state); + if state.key_changes != seen { + return state.key_changes; + } + let (sender, receiver) = oneshot::channel(); + state.key_waiters.push(sender); + receiver + }; + if waiting.await.is_err() { + return lock(&state).key_changes; + } + } + }) + } + + fn recovery_key_offer(&self, hub: &HubId) -> Result { + let mut state = lock(&self.state); + if state.locked.contains(hub) || state.recovering.contains_key(hub) { + return Err(HubError::KeysLocked); + } + Ok(recovery::Recipient::capture(Arc::downgrade(&self.state), &mut state, hub).issuer()) + } + + fn sign_out(&self, hub: &HubId) -> HubFuture<()> { + let hub = hub.clone(); + self.call( + FakeOp::SignOut, + FakeCall::SignOut(hub.clone()), + move |state| { + state.receipts.begin(&hub); + state.credentials.remove(&hub); + state.recovering.remove(&hub); + state.unconfirmed.remove(&hub); + Ok(()) + }, + ) + } + + fn forget(&self, hub: &HubId) -> Result<(), HubError> { + lock(&self.state).discard(hub) + } +} + +/// Counts a browser sign-in that was dropped before it finished. +struct CancelGuard { + state: Arc>, + finished: bool, +} + +impl Drop for CancelGuard { + fn drop(&mut self) { + if !self.finished { + let mut state = lock(&self.state); + state.oidc = None; + state.oidc_cancelled += 1; + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/testing/account_keys.rs b/crates/hubs-model/src/testing/account_keys.rs new file mode 100644 index 00000000..885c9645 --- /dev/null +++ b/crates/hubs-model/src/testing/account_keys.rs @@ -0,0 +1,68 @@ +//! In-memory control of account keys, for tests of what unlocks and locks them. + +use std::sync::{Arc, Mutex, PoisonError}; + +use futures::FutureExt as _; +use nocterm_session::Secret; + +use crate::account_keys::{AccountKeyError, AccountKeyFuture, AccountKeyLock}; +use crate::address::HubId; +use crate::testing::FakeHub; + +/// An in-memory [`AccountKeyLock`] over the key state of a [`FakeHub`], which also closes what +/// the hub's other fakes read ([`FakeHub::lock_keys`]). Opening needs `password`. +#[derive(Clone)] +pub struct FakeAccountKeys { + hub: FakeHub, + password: String, + attempts: Arc>>, +} + +impl FakeAccountKeys { + /// Keys of `hub` that open with `password`. + pub fn alongside(hub: &FakeHub, password: &str) -> Self { + Self { + hub: hub.clone(), + password: password.to_owned(), + attempts: Arc::default(), + } + } + + /// Whether each unlock attempt so far was accepted, in order. + pub fn attempts(&self) -> Vec { + self.attempts + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } +} + +impl AccountKeyLock for FakeAccountKeys { + fn is_open(&self, hub: &HubId) -> bool { + self.hub.keys_open(hub) + } + + fn unlock(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()> { + let (keys, hub) = (self.clone(), hub.clone()); + async move { + let accepted = password.expose() == keys.password; + keys.attempts + .lock() + .unwrap_or_else(PoisonError::into_inner) + .push(accepted); + if !keys.hub.is_signed_in(&hub) { + return Err(AccountKeyError::NotSignedIn); + } + if !accepted { + return Err(AccountKeyError::WrongPassword); + } + keys.hub.unlock_keys(&hub); + Ok(()) + } + .boxed() + } + + fn lock(&self, hub: &HubId) { + self.hub.lock_keys(hub); + } +} diff --git a/crates/hubs-model/src/testing/devices.rs b/crates/hubs-model/src/testing/devices.rs new file mode 100644 index 00000000..32123153 --- /dev/null +++ b/crates/hubs-model/src/testing/devices.rs @@ -0,0 +1,104 @@ +//! The devices of fake hub accounts, for tests of what lists and revokes them. + +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::FutureExt as _; +use futures::channel::oneshot; + +use crate::address::HubId; +use crate::devices::{AccountDevices, DeviceSummary}; +use crate::error::HubError; +use crate::ports::HubFuture; + +/// An in-memory [`AccountDevices`]. Clones share one state, so a test keeps a handle while a +/// service owns another. +#[derive(Clone, Default)] +pub struct FakeAccountDevices { + state: Arc>, +} + +#[derive(Default)] +struct State { + devices: Vec<(HubId, DeviceSummary)>, + revoked: Vec<(HubId, String)>, + failure: Option, + held: Option>, +} + +fn lock(state: &Mutex) -> MutexGuard<'_, State> { + state.lock().unwrap_or_else(PoisonError::into_inner) +} + +impl FakeAccountDevices { + /// The account on `hub` has these devices from now on. + pub fn set(&self, hub: &HubId, devices: Vec) { + let mut state = lock(&self.state); + state.devices.retain(|(known, _)| known != hub); + state + .devices + .extend(devices.into_iter().map(|device| (hub.clone(), device))); + } + + /// Every call fails with `error` until [`FakeAccountDevices::recover`]. + pub fn fail_with(&self, error: HubError) { + lock(&self.state).failure = Some(error); + } + + /// Calls work again. + pub fn recover(&self) { + lock(&self.state).failure = None; + } + + /// The next listing waits until the returned sender is used or dropped. + pub fn hold_next_list(&self) -> oneshot::Sender<()> { + let (release, held) = oneshot::channel(); + lock(&self.state).held = Some(held); + release + } + + /// The devices revoked so far, in order. + pub fn revoked(&self) -> Vec<(HubId, String)> { + lock(&self.state).revoked.clone() + } +} + +impl AccountDevices for FakeAccountDevices { + fn list(&self, hub: &HubId) -> HubFuture> { + let hub = hub.clone(); + let state = self.state.clone(); + let held = lock(&state).held.take(); + async move { + if let Some(held) = held { + let _ = held.await; + } + let state = lock(&state); + if let Some(error) = &state.failure { + return Err(error.clone()); + } + Ok(state + .devices + .iter() + .filter(|(known, _)| *known == hub) + .map(|(_, device)| device.clone()) + .collect()) + } + .boxed() + } + + fn revoke(&self, hub: &HubId, device: &str) -> HubFuture<()> { + let (hub, device) = (hub.clone(), device.to_owned()); + let state = self.state.clone(); + async move { + let mut state = lock(&state); + if let Some(error) = &state.failure { + return Err(error.clone()); + } + state + .devices + .retain(|(known, summary)| *known != hub || summary.id != device); + state.revoked.push((hub, device)); + Ok(()) + } + .boxed() + } +} diff --git a/crates/hubs-model/src/testing/local_source.rs b/crates/hubs-model/src/testing/local_source.rs new file mode 100644 index 00000000..b0cf1320 --- /dev/null +++ b/crates/hubs-model/src/testing/local_source.rs @@ -0,0 +1,72 @@ +//! Per-capture source lifetimes for consumer tests; no authentication is performed. +use crate::{ + HubError, + source_identity::{ + LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, + LocalAccountSourceFactory, + }, +}; +use std::sync::{ + Arc, Mutex, PoisonError, + atomic::{AtomicBool, Ordering}, +}; + +struct SourceOwner { + partition: LocalAccountPartition, + current: AtomicBool, +} +impl LocalAccountSource for SourceOwner { + fn partition(&self) -> LocalAccountPartition { + self.partition.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + if self.current.load(Ordering::Acquire) { + Ok(()) + } else { + Err(LocalAccountSourceError::Stale) + } + } +} + +#[derive(Clone, Default)] +pub struct ScriptedLocalAccountFactory(Arc>>>, Arc); +impl ScriptedLocalAccountFactory { + /// A new login or key owner, even for the same persistent partition. + pub fn sign_in(&self, partition: LocalAccountPartition) { + self.1.store(false, Ordering::Release); + let mut slot = self.0.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(previous) = slot.take() { + previous.current.store(false, Ordering::Release); + } + *slot = Some(Arc::new(SourceOwner { + partition, + current: AtomicBool::new(true), + })); + } + /// The account is signed out: later captures fail as not signed in until a sign in. + pub fn sign_out(&self) { + self.close(); + self.1.store(true, Ordering::Release); + } + /// The keys lock: later captures fail as locked until a sign in. + pub fn close(&self) { + if let Some(previous) = self.0.lock().unwrap_or_else(PoisonError::into_inner).take() { + previous.current.store(false, Ordering::Release); + } + } +} +impl LocalAccountSourceFactory for ScriptedLocalAccountFactory { + fn capture(&self) -> Result, HubError> { + let signed_out = self.1.load(Ordering::Acquire); + self.0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .as_ref() + .map(|source| source.clone() as Arc) + .ok_or(if signed_out { + HubError::NotSignedIn + } else { + HubError::KeysLocked + }) + } +} diff --git a/crates/hubs-model/src/testing/login.rs b/crates/hubs-model/src/testing/login.rs new file mode 100644 index 00000000..733d6e2b --- /dev/null +++ b/crates/hubs-model/src/testing/login.rs @@ -0,0 +1,41 @@ +//! Membership ownership is independent from the fake's displayed key action receipts. +use super::{State, lock}; +use crate::{HubError, HubId, LoginOwner, LoginReceipt}; +use std::sync::{Arc, Mutex, Weak}; +struct Owner { + state: Weak>, + hub: HubId, + member: Arc<()>, +} +impl Owner { + fn with_current( + &self, + action: impl FnOnce(&mut State) -> Result, + ) -> Result { + let shared = self.state.upgrade().ok_or(HubError::Stale)?; + let mut state = lock(&shared); + state.receipts.check_member(&self.hub, &self.member)?; + if !state.credentials.contains_key(&self.hub) { + return Err(HubError::Stale); + } + action(&mut state) + } +} +impl LoginOwner for Owner { + fn check_current(&self) -> Result<(), HubError> { + self.with_current(|_| Ok(())) + } + fn discard(&self) -> Result<(), HubError> { + self.with_current(|state| state.discard(&self.hub)) + } +} +pub(super) fn receipt(shared: Weak>, state: &mut State, hub: &HubId) -> LoginReceipt { + LoginReceipt::new( + hub.clone(), + Arc::new(Owner { + state: shared, + hub: hub.clone(), + member: state.receipts.member(hub), + }), + ) +} diff --git a/crates/hubs-model/src/testing/recovery.rs b/crates/hubs-model/src/testing/recovery.rs new file mode 100644 index 00000000..861f7ba0 --- /dev/null +++ b/crates/hubs-model/src/testing/recovery.rs @@ -0,0 +1,157 @@ +//! Original recipient and action identities for the shared fake hub. +use super::{FakeCall, FakeHub, FakeOp, State, lock}; +use crate::{ + HubError, HubFuture, HubId, RecoveryKeyConfirmation, RecoveryKeyIssuer, RecoveryKeyOffer, + RecoveryKeyReissue, +}; +use nocterm_session::Secret; +use std::{ + collections::BTreeMap, + sync::{Arc, Mutex, Weak}, +}; + +#[derive(Default)] +pub(super) struct Receipts { + actions: BTreeMap>, + members: BTreeMap>, +} +impl Receipts { + pub(super) fn begin(&mut self, hub: &HubId) { + self.members.insert(hub.clone(), Arc::new(())); + self.rotate(hub); + } + pub(super) fn member(&mut self, hub: &HubId) -> Arc<()> { + self.members.entry(hub.clone()).or_default().clone() + } + pub(super) fn check_member(&self, hub: &HubId, expected: &Arc<()>) -> Result<(), HubError> { + if self + .members + .get(hub) + .is_some_and(|now| Arc::ptr_eq(now, expected)) + { + Ok(()) + } else { + Err(HubError::Stale) + } + } + pub(super) fn rotate(&mut self, hub: &HubId) { + self.actions.insert(hub.clone(), Arc::new(())); + } + pub(super) fn current(&mut self, hub: &HubId) -> Arc<()> { + self.actions.entry(hub.clone()).or_default().clone() + } + fn check(&self, hub: &HubId, expected: &Arc<()>) -> Result<(), HubError> { + if self + .actions + .get(hub) + .is_some_and(|now| Arc::ptr_eq(now, expected)) + { + Ok(()) + } else { + Err(HubError::Stale) + } + } +} +#[derive(Clone)] +pub(super) struct Recipient { + shared: Weak>, + hub: HubId, + receipt: Arc<()>, +} +impl Recipient { + pub(super) fn capture(shared: Weak>, state: &mut State, hub: &HubId) -> Self { + Self { + shared, + hub: hub.clone(), + receipt: state.receipts.current(hub), + } + } + pub(super) fn check(&self, state: &State) -> Result<(), HubError> { + state.receipts.check(&self.hub, &self.receipt)?; + if state.locked.contains(&self.hub) { + return Err(HubError::KeysLocked); + } + Ok(()) + } + pub(super) fn with_current( + &self, + action: impl FnOnce(&mut State) -> Result, + ) -> Result { + let shared = self.shared.upgrade().ok_or(HubError::Stale)?; + let mut state = lock(&shared); + self.check(&state)?; + action(&mut state) + } + pub(super) fn issuer(&self) -> RecoveryKeyReissue { + RecoveryKeyReissue::new(self.hub.clone(), Arc::new(self.clone())) + } + pub(super) fn offer(&self, key: Secret) -> RecoveryKeyOffer { + RecoveryKeyOffer::new(self.hub.clone(), key, Arc::new(self.clone())) + } +} +impl RecoveryKeyConfirmation for Recipient { + fn check_current(&self) -> Result<(), HubError> { + self.with_current(|_| Ok(())) + } + fn confirm(&self) -> HubFuture<()> { + let Some(shared) = self.shared.upgrade() else { + return Box::pin(async { Err(HubError::Stale) }); + }; + let original = self.clone(); + FakeHub { state: shared }.call( + FakeOp::ConfirmRecoveryKey, + FakeCall::ConfirmRecoveryKey(self.hub.clone()), + move |state| { + original.check(state)?; + state.unconfirmed.remove(&original.hub); + state.receipts.rotate(&original.hub); + state.keys_changed(); + Ok(()) + }, + ) + } +} +impl RecoveryKeyIssuer for Recipient { + fn check_current(&self) -> Result<(), HubError> { + RecoveryKeyConfirmation::check_current(self) + } + fn issue(&self) -> HubFuture { + let Some(shared) = self.shared.upgrade() else { + return Box::pin(async { Err(HubError::Stale) }); + }; + if let Err(error) = self.with_current(|_| Ok(())) { + return Box::pin(async move { Err(error) }); + } + let original = self.clone(); + let fake = FakeHub { + state: shared.clone(), + }; + let action = fake.call( + FakeOp::ReissueRecoveryKey, + FakeCall::ReissueRecoveryKey(self.hub.clone()), + move |state| { + original.check(state)?; + state.authenticate(&original.hub)?; + let username = state.credentials[&original.hub].clone(); + if state.recovering.contains_key(&original.hub) { + return Err(HubError::KeysLocked); + } + let keyset = state + .servers + .get_mut(original.hub.address()) + .and_then(|server| server.keysets.get_mut(&username)) + .ok_or(HubError::KeysLocked)?; + keyset.recovery.push('+'); + let key = Secret::new(keyset.recovery.clone()); + state.unconfirmed.insert(original.hub.clone()); + state.receipts.rotate(&original.hub); + Ok(Recipient::capture(Arc::downgrade(&shared), state, &original.hub).offer(key)) + }, + ); + Box::pin(async move { + let shown = action.await?; + shown.check_current()?; + Ok(shown) + }) + } +} diff --git a/crates/hubs-model/src/testing/server.rs b/crates/hubs-model/src/testing/server.rs new file mode 100644 index 00000000..43482dbd --- /dev/null +++ b/crates/hubs-model/src/testing/server.rs @@ -0,0 +1,155 @@ +//! The data of one fake hub server. + +use std::collections::BTreeMap; + +use crate::address::HubAddress; +use crate::records::{Account, AccountId, HubInfo, OidcProvider}; +use crate::settings::HubRecord; +use crate::testing::CLIENT_PROTOCOL; +use crate::trust::{Fingerprint, TrustPolicy}; + +/// A user who can sign in to a fake server. +#[derive(Clone, Debug)] +pub struct FakeUser { + pub username: String, + pub password: String, + pub account: Account, +} + +/// The account keys a fake server keeps for one user: what opens them and the recovery key. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct FakeKeyset { + /// The password (or vault passphrase) that opens the keys. + pub secret: String, + pub recovery: String, +} + +/// A server a [`crate::testing::FakeHub`] can reach. The fields are plain data a test edits. +#[derive(Clone, Debug)] +pub struct FakeServer { + pub address: HubAddress, + pub name: String, + pub certificate: Fingerprint, + /// A public authority vouches for the certificate. + pub publicly_trusted: bool, + /// The server answers; `false` makes every call unreachable. + pub online: bool, + /// The protocol version the server speaks. + pub protocol: u32, + pub bootstrapped: bool, + pub password_login: bool, + pub oidc_providers: Vec, + pub users: Vec, + /// The server keeps account keys: a sign-in opens, creates or waits for the recovery of + /// the keys of its user. Off, every sign-in finds the keys open. + pub keyed: bool, + /// The keys of the users that made them, by user name. + pub keysets: BTreeMap, +} + +impl FakeServer { + /// A self-signed server named "Fake hub" whose certificate is `[seed; 32]`. + pub fn new(address: &str, seed: u8) -> Self { + Self { + address: HubAddress::parse(address).expect("a valid fake address"), + name: "Fake hub".into(), + certificate: Fingerprint::from_bytes([seed; 32]), + publicly_trusted: false, + online: true, + protocol: CLIENT_PROTOCOL, + bootstrapped: true, + password_login: true, + oidc_providers: Vec::new(), + users: Vec::new(), + keyed: false, + keysets: BTreeMap::new(), + } + } + + /// Makes the server keep account keys; users without keys get them on their first sign-in. + #[must_use] + pub fn keyed(mut self) -> Self { + self.keyed = true; + self + } + + /// Gives `username` account keys that `secret` opens and `recovery` recovers. + #[must_use] + pub fn with_keyset(mut self, username: &str, secret: &str, recovery: &str) -> Self { + self.keyed = true; + self.keysets.insert( + username.to_owned(), + FakeKeyset { + secret: secret.into(), + recovery: recovery.into(), + }, + ); + self + } + + #[must_use] + pub fn named(mut self, name: &str) -> Self { + self.name = name.into(); + self + } + + /// The settings record of this server as a sign-in leaves it: pinned to its certificate. + pub fn pinned_record(&self, label: &str) -> HubRecord { + HubRecord::new( + self.address.clone(), + TrustPolicy::Pinned(self.certificate), + label, + ) + } + + #[must_use] + pub fn publicly_trusted(mut self) -> Self { + self.publicly_trusted = true; + self + } + + #[must_use] + pub fn with_provider(mut self, id: &str, display_name: &str) -> Self { + self.oidc_providers.push(OidcProvider { + id: id.into(), + display_name: display_name.into(), + }); + self + } + + /// A user of the hub. + #[must_use] + pub fn with_user(mut self, username: &str, password: &str) -> Self { + self.users.push(FakeUser { + username: username.into(), + password: password.into(), + account: Account { + id: AccountId::new(format!("account-{username}")), + username: username.into(), + display_name: username.to_uppercase(), + admin: false, + }, + }); + self + } + + /// Makes the existing user `username` an administrator of the hub; a user that does not + /// exist is left alone. + #[must_use] + pub fn with_admin(mut self, username: &str) -> Self { + if let Some(user) = self.users.iter_mut().find(|user| user.username == username) { + user.account.admin = true; + } + self + } + + pub(super) fn info(&self, signed_in: bool) -> HubInfo { + HubInfo { + name: self.name.clone(), + password_login: self.password_login, + oidc_providers: self.oidc_providers.clone(), + bootstrapped: self.bootstrapped, + signed_in, + } + } +} diff --git a/crates/hubs-model/src/testing/tests.rs b/crates/hubs-model/src/testing/tests.rs new file mode 100644 index 00000000..73fe3716 --- /dev/null +++ b/crates/hubs-model/src/testing/tests.rs @@ -0,0 +1,367 @@ +use futures::executor::block_on; + +use super::*; +use crate::Fingerprint; + +const ADDRESS: &str = "hub.example:7443"; + +fn hub_id() -> HubId { + HubId::from(HubAddress::parse(ADDRESS).unwrap()) +} + +fn server() -> FakeServer { + FakeServer::new(ADDRESS, 7) + .with_user("ada", "secret") + .with_provider("corp", "Corporate SSO") +} + +fn endpoint(trust: TrustPolicy) -> HubEndpointSpec { + HubEndpointSpec { + address: HubAddress::parse(ADDRESS).unwrap(), + trust, + } +} + +fn pinned() -> TrustPolicy { + TrustPolicy::Pinned(Fingerprint::from_bytes([7; 32])) +} + +fn signed_in() -> FakeHub { + let hub = FakeHub::new().with_server(server()); + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))).unwrap(); + hub +} + +#[test] +fn probe_shows_the_certificate_without_trusting_it() { + let hub = FakeHub::new().with_server(server()); + let probed = block_on(hub.probe(&HubAddress::parse(ADDRESS).unwrap())).unwrap(); + assert_eq!(probed.fingerprint, Fingerprint::from_bytes([7; 32])); + assert!(!probed.publicly_trusted); +} + +#[test] +fn an_unknown_or_offline_address_is_unreachable() { + let hub = FakeHub::new().with_server(server()); + let other = HubAddress::parse("nowhere.example").unwrap(); + assert!(matches!( + block_on(hub.probe(&other)), + Err(HubError::Unreachable { .. }) + )); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| s.online = false); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::Unreachable { .. }) + )); +} + +#[test] +fn connect_reports_the_certificate_that_changed_since_it_was_pinned() { + let hub = FakeHub::new().with_server(server()); + let old = TrustPolicy::Pinned(Fingerprint::from_bytes([1; 32])); + match block_on(hub.connect(endpoint(old))) { + Err(HubError::CertificateChanged { + pinned, presented, .. + }) => { + assert_eq!(pinned, Fingerprint::from_bytes([1; 32])); + assert_eq!(presented, Fingerprint::from_bytes([7; 32])); + } + other => panic!("{other:?}"), + } +} + +#[test] +fn public_trust_needs_a_certificate_a_public_authority_vouches_for() { + let hub = FakeHub::new().with_server(server()); + assert!(matches!( + block_on(hub.connect(endpoint(TrustPolicy::PublicCa))), + Err(HubError::CertificateUntrusted { .. }) + )); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| { + s.publicly_trusted = true; + }); + assert!(block_on(hub.connect(endpoint(TrustPolicy::PublicCa))).is_ok()); +} + +#[test] +fn connect_tells_whether_this_device_is_signed_in() { + let hub = FakeHub::new().with_server(server()); + let info = block_on(hub.connect(endpoint(pinned()))).unwrap(); + assert!(!info.signed_in); + assert_eq!(info.oidc_providers.len(), 1); + hub.sign_in_as(&hub_id(), "ada"); + assert!(block_on(hub.connect(endpoint(pinned()))).unwrap().signed_in); +} + +#[test] +fn connect_refuses_a_hub_that_needs_a_newer_client() { + let hub = FakeHub::new().with_server(server()); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| s.protocol = 9); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::UpgradeRequired { hub: 9, .. }) + )); +} + +#[test] +fn password_sign_in_returns_the_account_and_remembers_the_device() { + let hub = FakeHub::new().with_server(server()); + let wrong = block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("nope"))); + assert!(matches!(wrong, Err(HubError::InvalidCredentials))); + assert!(!hub.is_signed_in(&hub_id())); + + let signed_in = + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))).unwrap(); + assert_eq!(signed_in.account.username, "ada"); + assert!(!signed_in.account.admin); + assert_eq!(signed_in.keys, KeyStatus::Open); + assert!(hub.is_signed_in(&hub_id())); +} + +#[test] +fn password_sign_in_is_refused_when_the_hub_offers_only_single_sign_on() { + let hub = FakeHub::new().with_server(server()); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| { + s.password_login = false; + }); + let result = block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))); + assert!(matches!(result, Err(HubError::MethodUnavailable))); +} + +#[test] +fn calls_never_record_the_password() { + let hub = FakeHub::new().with_server(server()); + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))).unwrap(); + let text = format!("{:?}", hub.calls()); + assert!(text.contains("ada")); + assert!(!text.contains("secret")); +} + +#[test] +fn browser_sign_in_hands_out_the_address_and_finishes_when_the_browser_does() { + let hub = FakeHub::new().with_server(server()); + let OidcSignIn { browser, done } = hub.oidc(endpoint(pinned()), "corp".into()); + let browser_side = async { + let url = browser.await.unwrap(); + assert!(url.contains("provider=corp")); + hub.complete_oidc("ada"); + }; + let (result, ()) = block_on(futures::future::join(done, browser_side)); + assert_eq!(result.unwrap().account.username, "ada"); + assert!(hub.is_signed_in(&hub_id())); + assert_eq!(hub.cancelled_oidc(), 0); +} + +#[test] +fn dropping_a_browser_sign_in_counts_as_cancelling_it() { + let hub = FakeHub::new().with_server(server()); + let OidcSignIn { browser, mut done } = hub.oidc(endpoint(pinned()), "corp".into()); + block_on(async { + assert!(futures::poll!(&mut done).is_pending()); + browser.await.unwrap(); + }); + drop(done); + assert_eq!(hub.cancelled_oidc(), 1); + assert!(!hub.is_signed_in(&hub_id())); +} + +#[test] +fn a_failed_browser_sign_in_reports_why() { + let hub = FakeHub::new().with_server(server()); + let OidcSignIn { browser, done } = hub.oidc(endpoint(pinned()), "corp".into()); + let browser_side = async { + browser.await.unwrap(); + hub.fail_oidc(HubError::SignInTimedOut); + }; + let (result, ()) = block_on(futures::future::join(done, browser_side)); + assert!(matches!(result, Err(HubError::SignInTimedOut))); + assert!(!hub.is_signed_in(&hub_id())); +} + +#[test] +fn an_unknown_provider_is_unavailable() { + let hub = FakeHub::new().with_server(server()); + let sign_in = hub.oidc(endpoint(pinned()), "other".into()); + assert!(matches!( + block_on(sign_in.done), + Err(HubError::MethodUnavailable) + )); +} + +#[test] +fn a_revoked_device_loses_its_sign_in() { + let hub = signed_in(); + hub.revoke_device(&hub_id()); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::Revoked) + )); + assert!(!hub.is_signed_in(&hub_id())); + assert!(!block_on(hub.connect(endpoint(pinned()))).unwrap().signed_in); +} + +#[test] +fn sign_out_and_forget_remove_the_credentials() { + let hub = signed_in(); + block_on(hub.sign_out(&hub_id())).unwrap(); + assert!(!hub.is_signed_in(&hub_id())); + hub.sign_in_as(&hub_id(), "ada"); + hub.forget(&hub_id()).unwrap(); + assert!(!hub.is_signed_in(&hub_id())); + assert!(hub.calls().contains(&FakeCall::Forget(hub_id()))); +} + +#[test] +fn an_injected_failure_hits_one_call_of_that_kind_only() { + let hub = signed_in(); + hub.fail_next(FakeOp::Connect, HubError::PermissionDenied); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::PermissionDenied) + )); + assert!(block_on(hub.connect(endpoint(pinned()))).is_ok()); + assert_eq!(hub.count(FakeOp::Connect), 2); +} + +#[test] +fn a_held_call_waits_until_released() { + let hub = signed_in(); + let release = hub.hold(FakeOp::Connect); + let mut pending = hub.connect(endpoint(pinned())); + block_on(async { assert!(futures::poll!(&mut pending).is_pending()) }); + release.release(); + assert!(block_on(pending).is_ok()); +} + +fn keyed_hub() -> FakeHub { + FakeHub::new().with_server(server().keyed()) +} + +fn sign_in(hub: &FakeHub, password: &str) -> SignedIn { + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new(password))).unwrap() +} + +#[test] +fn a_first_sign_in_creates_the_keys_and_hands_out_the_recovery_key_once() { + let hub = keyed_hub(); + let KeyStatus::Created(first) = sign_in(&hub, "secret").keys else { + panic!("created"); + }; + assert_eq!(first.key(), &Secret::new("recovery-ada")); + assert!(hub.recovery_key_unconfirmed(&hub_id())); + assert!(matches!( + sign_in(&hub, "secret").keys, + KeyStatus::RecoveryKeyUnconfirmed(_) + )); + assert!(matches!(block_on(first.confirm()), Err(HubError::Stale))); + let current = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + block_on(current.confirm()).unwrap(); + assert!(!hub.recovery_key_unconfirmed(&hub_id())); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::Open); +} + +#[test] +fn reissuing_replaces_the_recovery_key_until_it_is_confirmed() { + let hub = keyed_hub(); + let KeyStatus::Created(first) = sign_in(&hub, "secret").keys else { + panic!("created"); + }; + block_on(first.confirm()).unwrap(); + + let key = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + assert_eq!(key.key(), &Secret::new("recovery-ada+")); + assert!(hub.recovery_key_unconfirmed(&hub_id())); + block_on(key.confirm()).unwrap(); + + let again = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + assert_ne!(again, key); +} + +#[test] +fn reissuing_needs_open_keys() { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "old", "recovery-key")); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::RecoveryNeeded); + let result = block_on(hub.reissue_recovery_key(&hub_id())); + assert!(matches!(result, Err(HubError::KeysLocked))); + assert!(!hub.recovery_key_unconfirmed(&hub_id())); +} + +#[test] +fn a_password_that_does_not_open_the_keys_waits_for_the_recovery_key() { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "old", "recovery-key")); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::RecoveryNeeded); + + let wrong = block_on(hub.recover(&hub_id(), Secret::new("not it"))); + assert!(matches!(wrong, Err(HubError::RecoveryKeyInvalid))); + block_on(hub.recover(&hub_id(), Secret::new("recovery-key"))).unwrap(); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::Open); +} + +#[test] +fn recovering_without_a_waiting_sign_in_is_refused() { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "secret", "recovery-key")); + sign_in(&hub, "secret"); + let result = block_on(hub.recover(&hub_id(), Secret::new("recovery-key"))); + assert!(matches!(result, Err(HubError::NoRecoveryPending))); +} + +#[test] +fn a_browser_sign_in_needs_the_vault_passphrase_to_open_or_make_the_keys() { + let hub = keyed_hub(); + let OidcSignIn { browser, done } = hub.oidc(endpoint(pinned()), "corp".into()); + let browser_side = async { + browser.await.unwrap(); + hub.complete_oidc("ada"); + }; + let (result, ()) = block_on(futures::future::join(done, browser_side)); + assert_eq!(result.unwrap().keys, KeyStatus::PassphraseNeeded); + + let made = block_on(hub.set_vault_passphrase(&hub_id(), Secret::new("a long passphrase"))); + let KeyStatus::Created(key) = made.unwrap() else { + panic!("created"); + }; + assert_eq!(key.key(), &Secret::new("recovery-ada")); + block_on(key.confirm()).unwrap(); + let again = block_on(hub.set_vault_passphrase(&hub_id(), Secret::new("a long passphrase"))); + assert_eq!(again.unwrap(), KeyStatus::Open); + let other = block_on(hub.set_vault_passphrase(&hub_id(), Secret::new("another passphrase"))); + assert_eq!(other.unwrap(), KeyStatus::RecoveryNeeded); +} + +#[test] +fn an_old_recovery_acknowledgement_cannot_confirm_a_new_reissue() { + let hub = keyed_hub(); + let KeyStatus::Created(first) = sign_in(&hub, "secret").keys else { + panic!("created"); + }; + let second = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + assert!(matches!(block_on(first.confirm()), Err(HubError::Stale))); + assert!(hub.recovery_key_unconfirmed(&hub_id())); + block_on(second.confirm()).unwrap(); + assert!(!hub.recovery_key_unconfirmed(&hub_id())); +} + +#[test] +fn a_held_recovery_issue_does_not_choose_a_replacement_account() { + let hub = keyed_hub(); + sign_in(&hub, "secret"); + let action = hub.recovery_key_offer(&hub_id()).unwrap(); + let held = hub.hold(FakeOp::ReissueRecoveryKey); + let pending = action.issue(); + hub.sign_in_as(&hub_id(), "bob"); + held.release(); + assert!(matches!(block_on(pending), Err(HubError::Stale))); +} + +#[test] +fn a_login_receipt_survives_key_lock_and_ordinary_refresh_but_not_a_new_login() { + let hub = keyed_hub(); + let signed = sign_in(&hub, "secret"); + hub.lock_keys(&hub_id()); + hub.unlock_keys(&hub_id()); + block_on(hub.connect(endpoint(pinned()))).unwrap(); + assert!(signed.login.check_current().is_ok()); + hub.sign_in_as(&hub_id(), "ada"); + assert!(matches!(signed.login.discard(), Err(HubError::Stale))); + assert!(hub.is_signed_in(&hub_id())); +} diff --git a/crates/hubs-model/src/trust.rs b/crates/hubs-model/src/trust.rs new file mode 100644 index 00000000..51e2ffaf --- /dev/null +++ b/crates/hubs-model/src/trust.rs @@ -0,0 +1,170 @@ +//! Certificate fingerprints and the decision whether to trust a hub's certificate. + +use std::fmt; +use std::str::FromStr; + +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::error::FingerprintError; + +/// SHA-256 of a hub's leaf certificate. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct Fingerprint([u8; 32]); + +impl Fingerprint { + pub const fn from_bytes(bytes: [u8; 32]) -> Self { + Self(bytes) + } + + pub const fn as_bytes(&self) -> &[u8; 32] { + &self.0 + } + + /// Reads the digits a hub prints or a person pastes: hex, any case, with or without + /// colons and spaces. + pub fn parse(text: &str) -> Result { + let digits: Vec = text + .chars() + .filter(|c| *c != ':' && !c.is_whitespace()) + .collect(); + if digits.iter().any(|c| !c.is_ascii_hexdigit()) { + return Err(FingerprintError::NotHex); + } + if digits.len() != 64 { + return Err(FingerprintError::Length { + found: digits.len(), + }); + } + let mut bytes = [0_u8; 32]; + for (byte, pair) in bytes.iter_mut().zip(digits.chunks(2)) { + let high = pair[0].to_digit(16).unwrap_or(0); + let low = pair[1].to_digit(16).unwrap_or(0); + *byte = u8::try_from(high * 16 + low).unwrap_or(0); + } + Ok(Self(bytes)) + } + + /// Eight groups of four digits for reading aloud and comparing by eye. + pub fn groups(&self) -> [String; 8] { + std::array::from_fn(|group| { + self.0[group * 4..group * 4 + 4] + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() + }) + } +} + +impl fmt::Display for Fingerprint { + /// Lower-case pairs separated by colons, the form stored in settings. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for (index, byte) in self.0.iter().enumerate() { + if index > 0 { + f.write_str(":")?; + } + write!(f, "{byte:02x}")?; + } + Ok(()) + } +} + +impl fmt::Debug for Fingerprint { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Fingerprint({self})") + } +} + +impl FromStr for Fingerprint { + type Err = FingerprintError; + + fn from_str(text: &str) -> Result { + Self::parse(text) + } +} + +impl Serialize for Fingerprint { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_str(self) + } +} + +impl<'de> Deserialize<'de> for Fingerprint { + fn deserialize>(deserializer: D) -> Result { + let text = String::deserialize(deserializer)?; + Self::parse(&text).map_err(serde::de::Error::custom) + } +} + +/// How a hub's certificate is checked on every connection. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum TrustPolicy { + /// Exactly this certificate; a different one is an error, never a prompt. + Pinned(Fingerprint), + /// Any certificate a public authority vouches for under the hub's name. + PublicCa, +} + +/// What a TLS handshake without trust showed of a hub. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ProbedHub { + pub fingerprint: Fingerprint, + /// A public authority vouches for the certificate under the probed name. + pub publicly_trusted: bool, +} + +/// What to do about the certificate a hub presented. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum TrustDecision { + /// The fingerprint the user typed matches: pin it, no question to ask. + Pinned(Fingerprint), + /// A public authority vouches for the certificate and nothing was typed. + PublicCa, + /// A self-signed certificate and nothing typed: show the fingerprint and let the user + /// confirm it against the server's output before pinning. + ConfirmFirstUse(Fingerprint), + /// The typed fingerprint is not the presented one. There is nothing to accept. + Mismatch { + typed: Fingerprint, + presented: Fingerprint, + }, +} + +impl TrustDecision { + /// The policy that needs no further answer, if there is one. + pub const fn settled(&self) -> Option { + match self { + Self::Pinned(fingerprint) => Some(TrustPolicy::Pinned(*fingerprint)), + Self::PublicCa => Some(TrustPolicy::PublicCa), + Self::ConfirmFirstUse(_) | Self::Mismatch { .. } => None, + } + } + + /// The policy after the user confirmed the shown fingerprint; only a first-use + /// decision can be confirmed. + pub const fn confirmed(&self) -> Option { + match self { + Self::ConfirmFirstUse(fingerprint) => Some(TrustPolicy::Pinned(*fingerprint)), + Self::Pinned(_) | Self::PublicCa | Self::Mismatch { .. } => None, + } + } +} + +/// Decides how to treat a probed certificate given the fingerprint the user typed, if any. +/// +/// A typed fingerprint always wins: it must match, even for a certificate a public authority +/// vouches for. Without one, a publicly trusted chain needs no pinning, so certificate +/// rotation stays silent, and a self-signed certificate must be confirmed once. +pub fn decide(probe: &ProbedHub, typed: Option<&Fingerprint>) -> TrustDecision { + match typed { + Some(typed) if *typed == probe.fingerprint => TrustDecision::Pinned(probe.fingerprint), + Some(typed) => TrustDecision::Mismatch { + typed: *typed, + presented: probe.fingerprint, + }, + None if probe.publicly_trusted => TrustDecision::PublicCa, + None => TrustDecision::ConfirmFirstUse(probe.fingerprint), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/trust/tests.rs b/crates/hubs-model/src/trust/tests.rs new file mode 100644 index 00000000..19723ca8 --- /dev/null +++ b/crates/hubs-model/src/trust/tests.rs @@ -0,0 +1,111 @@ +use super::*; + +const PLAIN: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"; + +fn print() -> Fingerprint { + Fingerprint::parse(PLAIN).unwrap() +} + +#[test] +fn a_fingerprint_reads_hex_with_or_without_colons_in_any_case() { + let colons = PLAIN + .as_bytes() + .chunks(2) + .map(|pair| std::str::from_utf8(pair).unwrap()) + .collect::>() + .join(":"); + assert_eq!(Fingerprint::parse(&colons).unwrap(), print()); + assert_eq!(Fingerprint::parse(&PLAIN.to_uppercase()).unwrap(), print()); + assert_eq!( + Fingerprint::parse(&format!(" {PLAIN}\n")).unwrap(), + print() + ); + assert_eq!(print().as_bytes()[1], 0x11); +} + +#[test] +fn a_fingerprint_of_the_wrong_length_or_with_other_characters_is_refused() { + assert_eq!( + Fingerprint::parse(&PLAIN[..62]), + Err(FingerprintError::Length { found: 62 }) + ); + assert_eq!( + Fingerprint::parse(&format!("{PLAIN}ab")), + Err(FingerprintError::Length { found: 66 }) + ); + assert_eq!( + Fingerprint::parse(&format!("{}zz", &PLAIN[..62])), + Err(FingerprintError::NotHex) + ); + assert_eq!( + Fingerprint::parse(""), + Err(FingerprintError::Length { found: 0 }) + ); +} + +#[test] +fn a_fingerprint_is_shown_with_colons_and_in_groups_of_four_digits() { + let shown = print().to_string(); + assert!(shown.starts_with("00:11:22:33:44")); + assert_eq!(shown.len(), 95); + assert_eq!(Fingerprint::parse(&shown).unwrap(), print()); + let groups = print().groups(); + assert_eq!(groups[0], "00112233"); + assert_eq!(groups[7], "ccddeeff"); +} + +#[test] +fn a_typed_fingerprint_that_matches_pins_the_certificate() { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted: false, + }; + let decision = decide(&probe, Some(&print())); + assert_eq!(decision, TrustDecision::Pinned(print())); + assert_eq!(decision.settled(), Some(TrustPolicy::Pinned(print()))); + assert_eq!(decision.confirmed(), None); +} + +#[test] +fn a_typed_fingerprint_that_differs_is_a_mismatch_even_for_a_public_certificate() { + let other = Fingerprint::from_bytes([9; 32]); + for publicly_trusted in [false, true] { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted, + }; + let decision = decide(&probe, Some(&other)); + assert_eq!( + decision, + TrustDecision::Mismatch { + typed: other, + presented: print() + } + ); + assert_eq!(decision.settled(), None); + assert_eq!(decision.confirmed(), None, "a mismatch cannot be accepted"); + } +} + +#[test] +fn a_public_certificate_without_a_typed_fingerprint_is_not_pinned() { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted: true, + }; + let decision = decide(&probe, None); + assert_eq!(decision, TrustDecision::PublicCa); + assert_eq!(decision.settled(), Some(TrustPolicy::PublicCa)); +} + +#[test] +fn a_self_signed_certificate_needs_one_confirmation_before_it_is_pinned() { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted: false, + }; + let decision = decide(&probe, None); + assert_eq!(decision, TrustDecision::ConfirmFirstUse(print())); + assert_eq!(decision.settled(), None); + assert_eq!(decision.confirmed(), Some(TrustPolicy::Pinned(print()))); +} diff --git a/crates/hubs-ui/Cargo.toml b/crates/hubs-ui/Cargo.toml new file mode 100644 index 00000000..ce36f280 --- /dev/null +++ b/crates/hubs-ui/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "nocterm-hubs-ui" +description = "The environment menu, the sign-in dialog and the Servers page of nocterm hubs." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "feature" + +[dependencies] +nocterm-core.workspace = true +nocterm-hubs.workspace = true +nocterm-hub-agents.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-hubs-model.workspace = true +nocterm-session.workspace = true +nocterm-ui.workspace = true +nocterm-workspace.workspace = true +gpui-kit.workspace = true +thiserror.workspace = true +futures.workspace = true + +[dev-dependencies] +nocterm-hub-agents-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-settings.workspace = true +nocterm-store.workspace = true +gpui-kit = { workspace = true, features = ["test-support"] } +nocterm-workspace = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hubs-ui/src/color.rs b/crates/hubs-ui/src/color.rs new file mode 100644 index 00000000..0e805594 --- /dev/null +++ b/crates/hubs-ui/src/color.rs @@ -0,0 +1,49 @@ +//! The colours that mark a server, drawn in the current theme. + +use gpui_kit::{App, Hsla, component::ActiveTheme as _}; +use nocterm_hubs_model::HubColor; + +/// The shade of `color` in the current theme. +pub(crate) fn tint(color: HubColor, cx: &App) -> Hsla { + let theme = cx.theme(); + match color { + HubColor::Red => theme.red, + HubColor::Yellow => theme.yellow, + HubColor::Green => theme.green, + HubColor::Cyan => theme.cyan, + HubColor::Blue => theme.blue, + HubColor::Magenta => theme.magenta, + } +} + +#[cfg(test)] +mod tests { + use super::tint; + use crate::test_support::{fixture, server}; + use gpui_kit::{Hsla, TestAppContext}; + use nocterm_hubs_model::{Environment, HubColor, testing::FakeHub}; + + #[gpui_kit::test] + fn every_colour_has_its_own_shade_in_the_theme(cx: &mut TestAppContext) { + let _window = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + Vec::new(), + None, + ); + let shades: Vec = cx.update(|cx| { + HubColor::ALL + .into_iter() + .map(|color| tint(color, cx)) + .collect() + }); + for (index, shade) in shades.iter().enumerate() { + assert!( + !shades[..index].contains(shade), + "{} repeats an earlier colour", + HubColor::ALL[index].name() + ); + } + } +} diff --git a/crates/hubs-ui/src/commands.rs b/crates/hubs-ui/src/commands.rs new file mode 100644 index 00000000..43e66ca0 --- /dev/null +++ b/crates/hubs-ui/src/commands.rs @@ -0,0 +1,128 @@ +//! The "Hubs" commands of the command palette: open a server in a new window and sign out of +//! one. Each asks which server in a picker, then does what the menu and the Servers page do. + +use gpui_kit::{App, Entity, Window}; +use nocterm_hubs::{HubStatus, HubSummary, Hubs}; +use nocterm_hubs_model::{Environment, HubId}; +use nocterm_ui::{PickerOption, open_choice_picker}; + +use crate::ports::Placement; +use crate::status::status_chip; +use crate::switch::Switch; +use crate::{manage, problems, sign_in}; + +/// Opens `summary` the way its status allows: as an environment when the server can be +/// reached, in the sign-in dialog when it needs a sign-in, or as an explanation when it is in +/// trouble that the user has to deal with. +pub(crate) fn open_server( + hubs: Entity, + summary: HubSummary, + placement: Placement, + switch: Switch, + window: &mut Window, + cx: &mut App, +) { + match summary.status.clone() { + HubStatus::Online | HubStatus::Connecting | HubStatus::Offline { .. } => { + switch.open(Environment::Hub(summary.id), placement, window, cx); + } + HubStatus::SignedOut | HubStatus::Revoked => { + sign_in::start(&hubs, Some(&summary.id), Some(switch), window, cx); + } + HubStatus::TrustFailed { pinned, presented } => { + problems::open_trust_failed(hubs, summary, pinned, presented, window, cx); + } + HubStatus::UpgradeRequired { hub, client } => { + problems::open_upgrade_required(&summary, hub, client, window, cx); + } + } +} + +/// Whether the server holds credentials of this device that signing out would erase. A server +/// whose certificate changed has none to offer here: removing it is the way out, and signing +/// out would hide the warning until the next connection attempt. +pub(crate) fn can_sign_out(summary: &HubSummary) -> bool { + !matches!( + summary.status, + HubStatus::SignedOut | HubStatus::Revoked | HubStatus::TrustFailed { .. } + ) +} + +fn options(servers: Vec, cx: &App) -> Vec> { + servers + .into_iter() + .map(|summary| { + let (status, _) = status_chip(&summary.status, cx); + let label = format!("{} · {} · {status}", summary.label, summary.address); + PickerOption::new(label, false, summary.id) + }) + .collect() +} + +/// Asks which known server to open in a new window. +pub(crate) fn pick_server_to_open( + hubs: &Entity, + switch: Switch, + window: &mut Window, + cx: &mut App, +) { + let servers = hubs.read(cx).list(); + if servers.is_empty() { + sign_in::start(hubs, None, Some(switch), window, cx); + return; + } + let picks = options(servers, cx); + let hubs = hubs.clone(); + open_choice_picker( + "Open a server in a new window", + picks, + move |id, window, cx| { + let Some(summary) = hubs.read(cx).summary(&id) else { + return; + }; + open_server( + hubs.clone(), + summary, + Placement::NewWindow, + switch.clone(), + window, + cx, + ); + }, + window, + cx, + ); +} + +/// Asks which signed-in server to sign out of. +pub(crate) fn pick_server_to_sign_out(hubs: &Entity, window: &mut Window, cx: &mut App) { + let servers: Vec = hubs + .read(cx) + .list() + .into_iter() + .filter(can_sign_out) + .collect(); + if servers.is_empty() { + nocterm_ui::notice::info( + window, + cx, + "hub-manage", + "No server to sign out of", + "This device is not signed in to any server.".to_owned(), + ); + return; + } + let picks = options(servers, cx); + let hubs = hubs.clone(); + open_choice_picker( + "Sign out of a server", + picks, + move |id, window, cx| manage::sign_out(&hubs, &id, window, cx), + window, + cx, + ); +} + +#[cfg(test)] +#[path = "commands_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/commands_tests.rs b/crates/hubs-ui/src/commands_tests.rs new file mode 100644 index 00000000..646a478e --- /dev/null +++ b/crates/hubs-ui/src/commands_tests.rs @@ -0,0 +1,108 @@ +//! The Hubs commands of the palette pick a known server and act on it. +use crate::test_support::{Fixture, fixture, hub_id, pinned_record, server}; +use crate::{OpenServerInNewWindow, Placement, SignInToServer, SignOutOfServer}; +use gpui_kit::{AppContext as _, TestAppContext}; +use nocterm_hubs::HubStatus; +use nocterm_hubs_model::{Environment, Fingerprint, HubRecord, TrustPolicy, testing::FakeHub}; + +fn window(cx: &mut TestAppContext, known: Vec, signed_in: Option<&str>) -> Fixture { + fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + known, + signed_in, + ) +} + +fn status(window: &Fixture, cx: &mut TestAppContext) -> Option { + cx.update(|cx| window.installed.hubs().read(cx).status(&hub_id())) +} + +fn notices(window: &Fixture, cx: &mut TestAppContext) -> usize { + cx.update_window(window.window, |_, window, cx| { + nocterm_ui::notice::count(window, cx) + }) + .unwrap() +} + +#[gpui_kit::test] +fn the_chosen_server_opens_in_a_new_window(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], Some("ada")); + window.dispatch(OpenServerInNewWindow, cx); + assert!(window.has_dialog(cx), "the servers are offered"); + window.press("enter", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn dismissing_the_picker_opens_nothing(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], Some("ada")); + window.dispatch(OpenServerInNewWindow, cx); + window.press("escape", cx); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn a_server_that_needs_signing_in_opens_the_sign_in_dialog_instead(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], None); + window.dispatch(OpenServerInNewWindow, cx); + window.press("enter", cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn a_changed_certificate_is_explained_and_never_opened(cx: &mut TestAppContext) { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let window = window(cx, vec![changed], Some("ada")); + window.dispatch(OpenServerInNewWindow, cx); + window.press("enter", cx); + window.settle_dialog(cx); + assert!(window.shows("trust-failed-remove", cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn without_known_servers_opening_one_starts_signing_in(cx: &mut TestAppContext) { + let window = window(cx, Vec::new(), None); + window.dispatch(OpenServerInNewWindow, cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn signing_out_of_the_chosen_server_keeps_it_listed(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], Some("ada")); + window.dispatch(SignOutOfServer, cx); + assert!(window.has_dialog(cx), "the signed-in servers are offered"); + window.press("enter", cx); + cx.run_until_parked(); + assert_eq!(status(&window, cx), Some(HubStatus::SignedOut)); + assert!(!window.hub.is_signed_in(&hub_id())); +} + +#[gpui_kit::test] +fn with_no_signed_in_server_signing_out_says_so(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], None); + window.dispatch(SignOutOfServer, cx); + assert!(!window.has_dialog(cx), "nothing to choose from"); + assert_eq!(notices(&window, cx), 1); +} + +#[gpui_kit::test] +fn the_sign_in_command_still_opens_the_sign_in_dialog(cx: &mut TestAppContext) { + let window = window(cx, Vec::new(), None); + window.dispatch(SignInToServer, cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); +} diff --git a/crates/hubs-ui/src/installation.rs b/crates/hubs-ui/src/installation.rs new file mode 100644 index 00000000..54e15d1f --- /dev/null +++ b/crates/hubs-ui/src/installation.rs @@ -0,0 +1,45 @@ +//! The hubs installation windows are composed from. + +use gpui_kit::{App, Entity}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::HubId; + +/// The registry of known hubs, kept by the application and given to each window. +#[derive(Clone)] +pub struct Installed { + hubs: nocterm_hubs::Installed, + hub_agents: Option>, +} + +impl Installed { + /// The name the user gave the server, or `None` when it is not in the list. + pub fn label(&self, hub: &HubId, cx: &App) -> Option { + self.hubs() + .read(cx) + .summary(hub) + .map(|summary| summary.label) + } + + /// The Servers page also manages the account's agents, providers and agent servers + /// through `agents`. + pub fn with_hub_agents(mut self, agents: Entity) -> Self { + self.hub_agents = Some(agents); + self + } + + pub(crate) fn hub_agents(&self) -> Option<&Entity> { + self.hub_agents.as_ref() + } + + pub(crate) fn hubs(&self) -> &Entity { + self.hubs.hubs() + } +} + +/// Keeps the service installation for the views of every window. +pub fn install(hubs: nocterm_hubs::Installed) -> Installed { + Installed { + hubs, + hub_agents: None, + } +} diff --git a/crates/hubs-ui/src/layout.rs b/crates/hubs-ui/src/layout.rs new file mode 100644 index 00000000..949dbd01 --- /dev/null +++ b/crates/hubs-ui/src/layout.rs @@ -0,0 +1,61 @@ +//! Small pieces the steps of the sign-in dialog are built from. + +use gpui_kit::{ + App, IntoElement, SharedString, + component::{ActiveTheme as _, StyledExt as _, v_flex}, + div, + prelude::*, +}; +use nocterm_hubs_model::Fingerprint; + +/// A labelled field. +pub(crate) fn field(label: &'static str, input: impl IntoElement) -> impl IntoElement { + v_flex() + .gap_1() + .child(div().text_xs().font_semibold().child(label)) + .child(input) +} + +pub(crate) fn title(text: String, _: &App) -> impl IntoElement { + div().font_semibold().child(text) +} + +/// A quiet sentence. +pub(crate) fn note(text: impl Into, cx: &App) -> impl IntoElement { + div() + .text_sm() + .text_color(cx.theme().muted_foreground) + .child(text.into()) +} + +/// A sentence about what went wrong. +pub(crate) fn problem(text: String, cx: &App) -> impl IntoElement { + div().text_sm().text_color(cx.theme().danger).child(text) +} + +/// A fingerprint in groups of four bytes, which are easier to compare by eye. +pub(crate) fn fingerprint_block( + label: &'static str, + fingerprint: &Fingerprint, + cx: &App, +) -> impl IntoElement { + let groups = fingerprint.groups(); + v_flex() + .gap_1() + .child( + div() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(label), + ) + .child( + div() + .font_family(cx.theme().mono_font_family.clone()) + .text_sm() + .child(format!( + "{}\n{}", + groups[..4].join(" "), + groups[4..].join(" ") + )), + ) +} diff --git a/crates/hubs-ui/src/lib.rs b/crates/hubs-ui/src/lib.rs new file mode 100644 index 00000000..008369a3 --- /dev/null +++ b/crates/hubs-ui/src/lib.rs @@ -0,0 +1,86 @@ +//! The environment menu, the sign-in dialog and the Servers page of nocterm hubs. +//! +//! A window shows one environment: this computer, or the account of one hub. This crate gives +//! the window the title-bar menu that switches between them, the dialog that signs in to a +//! server, the commands that sign in to, open and sign out of one, and the Servers page of the +//! settings. +//! +//! Owns: the presentation of the known hubs and their status ([`register`] installs the menu); +//! the Servers page of the settings ([`settings_page`]), a list of servers and, for the chosen +//! one, the tabs Overview, Devices (listed and revoked through the service), Keys (the vault +//! keys of the account, opened and closed on this device, and a new recovery key), Agents (the +//! account's agents and model providers, through `nocterm-hub-agents`) and Agent access (the +//! agent key and the servers connected to agents, checked and disconnected), and the palette +//! commands; the steps of the sign-in dialog as +//! the service orders them; the confirmation before a window with tabs switches. +//! +//! Does not own: the registry of hubs and the order of sign-in (`nocterm-hubs`); hub I/O and +//! trust rules (`nocterm-hubs-model` and the injected adapter); composing windows (the +//! application, through [`EnvironmentOpener`]); terminal tabs. +//! +//! Ports: uses [`EnvironmentOpener`], implemented by the application; it contributes no panel +//! and no plugin. +//! +//! Settings: none of its own; the service owns the `[hubs]` section and the Servers page edits +//! it through the service. +//! +//! Globals: none. +//! +//! Invariants: a certificate that is not the trusted one is never offered for acceptance, on +//! the Servers page as in the menu: removing the server is the way out; the trust step is never +//! confirmed by Enter alone; a password is cleared from its field when it is sent; switching a +//! window that has tabs asks first; closing the sign-in dialog cancels a sign-in in flight. + +mod color; +mod commands; +mod installation; +mod layout; +mod manage; +mod menu; +mod messages; +mod ports; +mod problems; +mod servers; +mod sign_in; +mod status; +mod switch; + +pub use installation::{Installed, install}; +pub use menu::register; +pub use ports::{EnvironmentOpenError, EnvironmentOpener, Placement}; +pub use servers::settings_page; +pub use sign_in::sign_in_again; + +gpui_kit::actions!( + hubs, + [ + /// Open the environment menu in the title bar. + OpenEnvironmentMenu, + /// Sign in to a server that is not in the list yet. + SignInToServer, + /// Open the Servers page of the settings, where known servers are renamed, coloured, signed out of or removed. + ManageServers, + /// Choose a known server and open it in a new window. + OpenServerInNewWindow, + /// Choose a signed-in server and sign this device out of it. + SignOutOfServer, + ] +); + +#[cfg(test)] +mod test_support; + +#[cfg(test)] +mod sign_in_action_tests { + #[gpui_kit::test] + fn the_sign_in_action_other_features_dispatch_by_name_exists( + cx: &mut gpui_kit::TestAppContext, + ) { + cx.update(|cx| { + let action = cx + .build_action(nocterm_workspace::SIGN_IN_TO_SERVER_ACTION, None) + .expect("registered"); + assert!(action.partial_eq(&crate::SignInToServer)); + }); + } +} diff --git a/crates/hubs-ui/src/manage.rs b/crates/hubs-ui/src/manage.rs new file mode 100644 index 00000000..58611c3f --- /dev/null +++ b/crates/hubs-ui/src/manage.rs @@ -0,0 +1,100 @@ +//! What the user does to a server they already know: sign out, remove, rename, colour. Each +//! starts the service call and tells the user when it did not work; the service keeps the +//! rules. + +use gpui_kit::{ + App, ClickEvent, Entity, Task, Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + }, + prelude::*, +}; +use nocterm_hubs::{HubSummary, Hubs, HubsError}; +use nocterm_hubs_model::{HubColor, HubId}; + +use crate::messages::hubs_problem; + +/// Shows a notice titled `title` when `task` fails, once it has finished. +pub(crate) fn report( + task: Task>, + title: &'static str, + window: &mut Window, + cx: &mut App, +) { + window + .spawn(cx, async move |cx| { + if let Err(error) = task.await { + cx.update(|window, cx| { + nocterm_ui::notice::error( + window, + cx, + "hub-manage", + title, + hubs_problem(&error), + ); + }) + .ok(); + } + }) + .detach(); +} + +/// Ends this device's session on the server. The server stays in the list, signed out. +pub(crate) fn sign_out(hubs: &Entity, hub: &HubId, window: &mut Window, cx: &mut App) { + let signing_out = hubs.update(cx, |hubs, cx| hubs.sign_out(hub, cx)); + report(signing_out, "Could not sign out", window, cx); +} + +/// Forgets the server and its credentials. +pub(crate) fn remove(hubs: &Entity, hub: &HubId, window: &mut Window, cx: &mut App) { + let removing = hubs.update(cx, |hubs, cx| hubs.remove(hub, cx)); + report(removing, "Could not remove the server", window, cx); +} + +/// Marks the server with `color`, or clears the mark. +pub(crate) fn recolor( + hubs: &Entity, + hub: &HubId, + color: Option, + window: &mut Window, + cx: &mut App, +) { + let marking = hubs.update(cx, |hubs, cx| hubs.set_color(hub, color, cx)); + report(marking, "Could not change the colour", window, cx); +} + +/// Asks before removing a server: it leaves this device's list and its credentials are +/// erased. Sessions on the server itself are not touched. +pub(crate) fn confirm_remove( + hubs: Entity, + hub: HubSummary, + window: &mut Window, + cx: &mut App, +) { + window.open_alert_dialog(cx, move |alert, _, _| { + let (hubs, id) = (hubs.clone(), hub.id.clone()); + let confirm = Button::new("remove-server-confirm") + .danger() + .label("Remove server") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + remove(&hubs, &id, window, cx); + }); + let keep = Button::new("remove-server-keep") + .primary() + .label("Keep it") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + }); + alert + .title(format!("Remove {}?", hub.label)) + .description(format!( + "{} leaves the list and this device is signed out of it. Its sessions keep running on the server.", + hub.address + )) + .on_ok(|_, _, _| true) + .footer(DialogFooter::new().child(confirm).child(keep)) + }); +} diff --git a/crates/hubs-ui/src/menu.rs b/crates/hubs-ui/src/menu.rs new file mode 100644 index 00000000..d16762c0 --- /dev/null +++ b/crates/hubs-ui/src/menu.rs @@ -0,0 +1,400 @@ +//! The environment menu in the title bar: this computer, every known server with its +//! status, and a way to sign in to another one. + +use std::rc::Rc; +use std::time::Duration; + +use gpui_kit::{ + Anchor, App, AppContext as _, ClickEvent, Context, Entity, EventEmitter, FocusHandle, + Focusable, Hsla, KeyDownEvent, SharedString, Subscription, Task, TestSupportExt as _, Window, + base::actions::Confirm, + component::{ + ActiveTheme as _, Icon, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, + popover::Popover, + v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::{HubSummary, Hubs, HubsEvent}; +use nocterm_hubs_model::Environment; +use nocterm_ui::IconName; +use nocterm_workspace::Workspace; + +use crate::color::tint; +use crate::commands::{open_server, pick_server_to_open, pick_server_to_sign_out}; +use crate::installation::Installed; +use crate::ports::{EnvironmentOpener, Placement}; +use crate::status::{status_chip, status_icon}; +use crate::switch::Switch; +use crate::{OpenEnvironmentMenu, OpenServerInNewWindow, SignInToServer, SignOutOfServer, sign_in}; + +/// How often the countdown of an offline server is redrawn while the menu shows. +const TICK: Duration = Duration::from_secs(1); + +/// Puts the environment menu in the title bar of `workspace` and handles +/// [`OpenEnvironmentMenu`], [`SignInToServer`], [`OpenServerInNewWindow`] and +/// [`SignOutOfServer`] there. `environment` is what the window shows; opening it again does +/// nothing. +pub fn register( + installed: &Installed, + environment: Environment, + opener: Rc, + workspace: &mut Workspace, + window: &mut Window, + cx: &mut gpui_kit::Context, +) { + let switch = Switch::new(opener, cx.weak_entity(), environment); + let hubs = installed.hubs().clone(); + let menu = cx.new(|cx| EnvironmentMenu::new(hubs.clone(), switch.clone(), window, cx)); + workspace.set_environment_menu(menu.clone(), cx); + let opening = menu.downgrade(); + workspace.register_action(move |_, _: &OpenEnvironmentMenu, _, cx| { + let _ = opening.update(cx, |menu, cx| menu.set_open(true, cx)); + }); + let (signing_in, opening, signing_out) = (hubs.clone(), hubs.clone(), hubs); + let (sign_in_switch, open_switch) = (switch.clone(), switch); + workspace.register_action(move |_, _: &SignInToServer, window, cx| { + sign_in::start(&signing_in, None, Some(sign_in_switch.clone()), window, cx); + }); + workspace.register_action(move |_, _: &OpenServerInNewWindow, window, cx| { + let (hubs, switch) = (opening.clone(), open_switch.clone()); + window.defer(cx, move |window, cx| { + pick_server_to_open(&hubs, switch, window, cx); + }); + }); + workspace.register_action(move |_, _: &SignOutOfServer, window, cx| { + let hubs = signing_out.clone(); + window.defer(cx, move |window, cx| { + pick_server_to_sign_out(&hubs, window, cx); + }); + }); +} + +/// The title-bar button and the list it opens. +struct EnvironmentMenu { + hubs: Entity, + environment: Environment, + list: Entity, + open: bool, + _subscriptions: Vec, +} + +impl EnvironmentMenu { + fn new(hubs: Entity, switch: Switch, _: &mut Window, cx: &mut Context) -> Self { + let environment = switch.current().clone(); + let list = cx.new(|cx| EnvironmentList::new(hubs.clone(), switch, cx)); + let subscriptions = vec![ + cx.observe(&hubs, |_, _, cx| cx.notify()), + cx.subscribe(&list, |menu, _, _: &ListDone, cx| menu.set_open(false, cx)), + ]; + Self { + hubs, + environment, + list, + open: false, + _subscriptions: subscriptions, + } + } + + fn set_open(&mut self, open: bool, cx: &mut Context) { + if self.open == open { + return; + } + self.open = open; + self.list.update(cx, |list, cx| list.shown(open, cx)); + cx.notify(); + } + + /// The colour of the server this window shows, when the owner chose one. + fn mark(&self, cx: &App) -> Option { + let Environment::Hub(id) = &self.environment else { + return None; + }; + let color = self.hubs.read(cx).summary(id)?.color?; + Some(tint(color, cx)) + } + + fn label(&self, cx: &App) -> SharedString { + match &self.environment { + Environment::Local => "This computer".into(), + Environment::Hub(id) => self + .hubs + .read(cx) + .summary(id) + .map_or_else(|| id.address().to_string(), |summary| summary.label) + .into(), + } + } +} + +impl Render for EnvironmentMenu { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let icon = match self.environment { + Environment::Local => IconName::Terminal, + Environment::Hub(_) => IconName::Server, + }; + let mark = self.mark(cx); + let trigger = Button::new("environment-menu") + .ghost() + .small() + .icon(match mark { + Some(color) => Icon::new(icon).text_color(color), + None => Icon::new(icon), + }) + .label(self.label(cx)); + let list = self.list.clone(); + let focus = list.read(cx).focus_handle(cx); + let menu = cx.weak_entity(); + div().flex().items_center().h_full().child( + Popover::new("environment-popover") + .anchor(Anchor::BottomLeft) + .trigger(trigger) + .track_focus(&focus) + .open(self.open) + .on_open_change(move |open, _, cx| { + let _ = menu.update(cx, |menu, cx| menu.set_open(*open, cx)); + }) + .content(move |_, _, _| list.clone()), + ) + } +} + +/// The list asks its menu to close. +struct ListDone; + +/// One line of the list. +#[derive(Clone)] +enum Row { + Local, + Hub(HubSummary), + SignIn, +} + +impl Row { + fn environment(&self) -> Option { + match self { + Self::Local => Some(Environment::Local), + Self::Hub(summary) => Some(Environment::Hub(summary.id.clone())), + Self::SignIn => None, + } + } +} + +struct EnvironmentList { + hubs: Entity, + switch: Switch, + selected: usize, + focus: FocusHandle, + /// Redraws the countdown of offline servers while the menu is open. + tick: Option>, + _subscriptions: Vec, +} + +impl EventEmitter for EnvironmentList {} + +impl EnvironmentList { + fn new(hubs: Entity, switch: Switch, cx: &mut Context) -> Self { + let subscriptions = vec![cx.subscribe(&hubs, |_, _, _: &HubsEvent, cx| cx.notify())]; + Self { + hubs, + switch, + selected: 0, + focus: cx.focus_handle(), + tick: None, + _subscriptions: subscriptions, + } + } + + fn rows(&self, cx: &App) -> Vec { + std::iter::once(Row::Local) + .chain(self.hubs.read(cx).list().into_iter().map(Row::Hub)) + .chain(std::iter::once(Row::SignIn)) + .collect() + } + + /// The menu opened or closed. An open menu starts on the row of the environment the + /// window shows and counts down the retries of offline servers. + fn shown(&mut self, open: bool, cx: &mut Context) { + if !open { + self.tick = None; + return; + } + let current = self.switch.current().clone(); + self.selected = self + .rows(cx) + .iter() + .position(|row| row.environment().as_ref() == Some(¤t)) + .unwrap_or(0); + self.tick = Some(cx.spawn(async move |this, cx| { + loop { + cx.background_executor().timer(TICK).await; + if this.update(cx, |_, cx| cx.notify()).is_err() { + break; + } + } + })); + cx.notify(); + } + + fn move_selection(&mut self, down: bool, cx: &mut Context) { + let rows = self.rows(cx).len(); + self.selected = if down { + (self.selected + 1) % rows + } else { + (self.selected + rows - 1) % rows + }; + cx.notify(); + } + + /// Does what a row stands for: opens the environment, or deals with the trouble the + /// server is in. + fn activate( + &mut self, + row: Row, + placement: Placement, + window: &mut Window, + cx: &mut Context, + ) { + cx.emit(ListDone); + let (hubs, switch) = (self.hubs.clone(), self.switch.clone()); + // The dialogs open once the popover has let go of the focus. + window.defer(cx, move |window, cx| match row { + Row::Local => switch.open(Environment::Local, placement, window, cx), + Row::SignIn => sign_in::start(&hubs, None, Some(switch), window, cx), + Row::Hub(summary) => open_server(hubs, summary, placement, switch, window, cx), + }); + } + + fn on_key(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context) { + let key = event.keystroke.key.as_str(); + match key { + "up" | "down" => { + self.move_selection(key == "down", cx); + cx.stop_propagation(); + } + "enter" if event.keystroke.modifiers.secondary() => { + self.activate_selected(Placement::NewWindow, window, cx); + cx.stop_propagation(); + } + _ => {} + } + } + + fn activate_selected( + &mut self, + placement: Placement, + window: &mut Window, + cx: &mut Context, + ) { + if let Some(row) = self.rows(cx).get(self.selected).cloned() { + self.activate(row, placement, window, cx); + } + } + + fn render_row(&self, index: usize, row: &Row, cx: &mut Context) -> gpui_kit::AnyElement { + let theme = cx.theme(); + let (muted, hover, accent) = (theme.muted_foreground, theme.accent, theme.accent); + let icon_color = match row { + Row::Hub(summary) => summary.color.map_or(muted, |color| tint(color, cx)), + _ => muted, + }; + let (icon, label, chip) = match row { + Row::Local => (IconName::Terminal, "This computer".to_owned(), None), + Row::Hub(summary) => ( + status_icon(&summary.status), + summary.label.clone(), + Some(status_chip(&summary.status, cx)), + ), + Row::SignIn => ( + IconName::ServerPlus, + "Sign in to a server…".to_owned(), + None, + ), + }; + let current = row.environment().as_ref() == Some(self.switch.current()); + let opens_elsewhere = row.environment().is_some(); + let (row_click, row_new) = (row.clone(), row.clone()); + h_flex() + .id(("environment-row", index)) + .test_support() + .gap_2() + .px_2() + .py_1() + .items_center() + .rounded(theme.radius) + .cursor_pointer() + .hover(|style| style.bg(hover)) + .when(index == self.selected, |style| style.bg(accent)) + .child(Icon::new(icon).small().text_color(icon_color)) + .child(div().flex_1().min_w_0().truncate().text_sm().child(label)) + .when(current, |style| { + style.child(Icon::new(IconName::Check).small().text_color(muted)) + }) + .when_some(chip, |style, (text, color)| { + style.child(div().text_xs().text_color(color).child(text)) + }) + .when(opens_elsewhere, |style| { + style.child( + Button::new(("environment-new-window", index)) + .ghost() + .xsmall() + .icon(IconName::ArrowUpRight) + .tooltip("Open in a new window") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.activate(row_new.clone(), Placement::NewWindow, window, cx); + })), + ) + }) + .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { + let placement = if event.modifiers().secondary() { + Placement::NewWindow + } else { + Placement::ThisWindow + }; + this.activate(row_click.clone(), placement, window, cx); + })) + .into_any_element() + } +} + +impl Focusable for EnvironmentList { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +impl Render for EnvironmentList { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let rows = self.rows(cx); + let last = rows.len().saturating_sub(1); + let border = cx.theme().border; + v_flex() + .id("environment-list") + .track_focus(&self.focus) + .key_context("EnvironmentMenu") + .w(px(340.)) + .gap_px() + .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| { + this.on_key(event, window, cx); + })) + .on_action(cx.listener(|this, _: &Confirm, window, cx| { + this.activate_selected(Placement::ThisWindow, window, cx); + })) + .children(rows.iter().enumerate().flat_map(|(index, row)| { + let separator = + (index == last).then(|| div().my_1().h_px().bg(border).into_any_element()); + separator + .into_iter() + .chain(std::iter::once(self.render_row(index, row, cx))) + })) + } +} + +#[cfg(test)] +#[path = "menu_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/menu_tests.rs b/crates/hubs-ui/src/menu_tests.rs new file mode 100644 index 00000000..821118fb --- /dev/null +++ b/crates/hubs-ui/src/menu_tests.rs @@ -0,0 +1,238 @@ +//! The environment menu lists this computer and the known servers, and opens what is chosen. +use crate::status::chip; +use crate::test_support::{Fixture, fixture, fixture_over, hub_id, pinned_record, server}; +use crate::{OpenEnvironmentMenu, Placement}; +use gpui_kit::{AppContext as _, Hsla, TestAppContext, hsla}; +use nocterm_hubs::HubStatus; +use nocterm_hubs_model::{Environment, Fingerprint, HubRecord, TrustPolicy, testing::FakeHub}; +use nocterm_settings::SettingsDocument; +use nocterm_store::{SettingsConfig, SnapshotWriter, StoreError}; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +fn local_window(cx: &mut TestAppContext, known: Vec) -> Fixture { + fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + known, + Some("ada"), + ) +} + +fn open_menu(window: &Fixture, cx: &mut TestAppContext) { + window.dispatch(OpenEnvironmentMenu, cx); +} + +#[gpui_kit::test] +fn without_servers_the_menu_offers_this_computer_and_signing_in(cx: &mut TestAppContext) { + let window = local_window(cx, Vec::new()); + open_menu(&window, cx); + assert!( + window.shows(("environment-row", 0usize), cx), + "this computer" + ); + assert!(window.shows(("environment-row", 1usize), cx), "sign in"); + assert!(!window.shows(("environment-row", 2usize), cx)); +} + +#[gpui_kit::test] +fn each_known_server_gets_a_row_between_this_computer_and_signing_in(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + assert!(window.shows(("environment-row", 2usize), cx)); + assert!(!window.shows(("environment-row", 3usize), cx)); +} + +#[gpui_kit::test] +fn clicking_a_signed_in_server_opens_it_in_this_window(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::ThisWindow)] + ); +} + +#[gpui_kit::test] +fn the_new_window_button_of_a_row_opens_it_beside_this_window(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + window.click(("environment-new-window", 1usize), cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn enter_opens_the_selected_row_and_ctrl_enter_opens_it_in_a_new_window(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + window.press("down", cx); + window.press("enter", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::ThisWindow)] + ); + open_menu(&window, cx); + window.press("down", cx); + window.press("ctrl-enter", cx); + assert_eq!( + window.environments.requests().last(), + Some(&(Environment::Hub(hub_id()), Placement::NewWindow)) + ); +} + +#[gpui_kit::test] +fn a_server_that_needs_a_new_certificate_decision_opens_no_window(cx: &mut TestAppContext) { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let window = local_window(cx, vec![changed]); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); + assert!( + window.has_dialog(cx), + "the certificate problem is explained" + ); + assert!(window.shows("trust-failed-close", cx)); + assert!(window.shows("trust-failed-remove", cx)); + assert!( + window.environments.requests().is_empty(), + "a changed certificate is never opened, let alone accepted" + ); +} + +fn changed_certificate_window(cx: &mut TestAppContext, config: SettingsConfig) -> Fixture { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let window = fixture_over( + cx, + config, + Environment::Local, + FakeHub::new().with_server(server()), + vec![changed], + Some("ada"), + ); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); + window +} + +#[gpui_kit::test] +fn removing_a_server_whose_certificate_changed_drops_its_row(cx: &mut TestAppContext) { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let window = changed_certificate_window(cx, config); + window.click("trust-failed-remove", cx); + cx.run_until_parked(); + assert!(!window.has_dialog(cx)); + open_menu(&window, cx); + assert!(!window.shows(("environment-row", 2usize), cx)); +} + +#[gpui_kit::test] +fn a_server_that_cannot_be_removed_says_so_instead_of_staying_listed_silently( + cx: &mut TestAppContext, +) { + let writer: SnapshotWriter = Arc::new(|_| { + Box::pin(async { + Err(StoreError::backend(std::io::Error::from( + std::io::ErrorKind::PermissionDenied, + ))) + }) + }); + let config = SettingsConfig::with_writer(SettingsDocument::default(), writer); + let window = changed_certificate_window(cx, config); + window.click("trust-failed-remove", cx); + cx.run_until_parked(); + window.render(cx); + let notices = cx + .update_window(window.window, |_, window, cx| { + nocterm_ui::notice::count(window, cx) + }) + .unwrap(); + assert_eq!(notices, 1, "the failure is reported"); +} + +#[gpui_kit::test] +fn a_signed_out_server_opens_the_sign_in_dialog(cx: &mut TestAppContext) { + let window = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + vec![pinned_record()], + None, + ); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn this_computer_row_opens_nothing_in_a_local_window(cx: &mut TestAppContext) { + let window = local_window(cx, Vec::new()); + open_menu(&window, cx); + window.click(("environment-row", 0usize), cx); + assert!(window.environments.requests().is_empty()); +} + +fn colors() -> [Hsla; 4] { + [ + hsla(0.3, 1., 0.5, 1.), + hsla(0., 0., 0.5, 1.), + hsla(0.1, 1., 0.5, 1.), + hsla(0., 1., 0.5, 1.), + ] +} + +fn text(status: &HubStatus, now: Instant) -> String { + let [good, quiet, warn, bad] = colors(); + chip(status, now, good, quiet, warn, bad).0.to_string() +} + +#[test] +fn every_status_has_a_chip_and_an_offline_server_counts_down() { + let now = Instant::now(); + let fingerprint = Fingerprint::from_bytes([1; 32]); + assert_eq!(text(&HubStatus::Online, now), "Online"); + assert_eq!(text(&HubStatus::Connecting, now), "Connecting…"); + assert_eq!(text(&HubStatus::SignedOut, now), "Sign-in required"); + assert_eq!(text(&HubStatus::Revoked, now), "Device revoked"); + assert_eq!( + text( + &HubStatus::TrustFailed { + pinned: Some(fingerprint), + presented: fingerprint + }, + now + ), + "Certificate changed" + ); + assert_eq!( + text(&HubStatus::UpgradeRequired { hub: 2, client: 1 }, now), + "Update Nocterm" + ); + let offline = HubStatus::Offline { + retry_at: now + Duration::from_secs(5), + }; + assert_eq!(text(&offline, now), "Offline · retry in 5 s"); + assert_eq!( + text(&offline, now + Duration::from_millis(1500)), + "Offline · retry in 4 s" + ); + assert_eq!( + text(&offline, now + Duration::from_secs(9)), + "Offline · retry in 0 s" + ); +} diff --git a/crates/hubs-ui/src/messages.rs b/crates/hubs-ui/src/messages.rs new file mode 100644 index 00000000..71453656 --- /dev/null +++ b/crates/hubs-ui/src/messages.rs @@ -0,0 +1,140 @@ +//! The words the views use for the failures of the hub service. The service reports typed +//! errors; this is the only place that turns them into text. + +use nocterm_hub_agents_model::HubAgentsError; +use nocterm_hubs::{HubsError, SignInError}; +use nocterm_hubs_model::HubError; + +/// Why a sign-in step did not work, for the dialog. +pub(crate) fn sign_in_problem(error: &SignInError) -> String { + match error { + SignInError::Address(error) => capitalize(&error.to_string()), + SignInError::Fingerprint(error) => capitalize(&error.to_string()), + SignInError::Hub(error) => hub_problem(error), + SignInError::AlreadyKnown(hub) => format!( + "{hub} is in the list already. Use its row to sign in again, or remove it first." + ), + SignInError::NotInitialized => { + "This server has no owner yet. Set it up first, then sign in here.".to_owned() + } + SignInError::Save(_) => { + "The server could not be saved to the settings, so this device was signed out of it again." + .to_owned() + } + } +} + +/// Why forgetting a server or signing out of it did not work. +pub(crate) fn hubs_problem(error: &HubsError) -> String { + match error { + HubsError::Unknown(hub) => format!("{hub} is not in the list."), + HubsError::Hub(error) => hub_problem(error), + HubsError::Save(_) => { + "The settings could not be saved, so the server stays listed.".to_owned() + } + } +} + +/// What a failed call of the agents or servers of a hub means to the person. +pub(crate) fn agents_problem(error: &HubAgentsError) -> String { + match error { + HubAgentsError::Hub(error) => hub_problem(error), + HubAgentsError::Draft(error) => error.to_string(), + HubAgentsError::Malformed => { + "The server answered with something this version cannot read. Update Nocterm." + .to_owned() + } + HubAgentsError::LimitReached => { + "A limit set by the server's administrator is reached.".to_owned() + } + } +} + +/// What a failed call on a hub means to the person using it. +pub(crate) fn hub_problem(error: &HubError) -> String { + match error { + HubError::Unreachable { address, .. } => { + format!("Cannot reach {address}. Check the address and your network.") + } + HubError::CertificateChanged { address, .. } => { + format!("The certificate of {address} is not the one that was pinned.") + } + HubError::CertificateUntrusted { address, .. } => { + format!("The certificate of {address} is not trusted.") + } + HubError::UpgradeRequired { hub, client } => format!( + "This server speaks protocol {hub} and Nocterm speaks {client}. Update the older one." + ), + HubError::InvalidCredentials => "The user name or password was not accepted.".to_owned(), + HubError::Stale => "The account changed. Start this action again for the current account.".to_owned(), + HubError::RecoveryKeyInvalid => { + "That is not the recovery key of this account. Check it and try again.".to_owned() + } + HubError::InvalidVaultPassphrase => { + "The vault passphrase is too short. Use at least eight characters.".to_owned() + } + HubError::KeysLocked => { + "The account keys are closed on this device. Unlock the vault or sign in with your password first." + .to_owned() + } + HubError::UnlockSourceMismatch { + needs_login_password: true, + } => "These account keys are opened with your login password, not with a vault passphrase. Sign in with the password instead." + .to_owned(), + HubError::UnlockSourceMismatch { + needs_login_password: false, + } => "These account keys are opened with a vault passphrase, not with your login password. Type the passphrase." + .to_owned(), + HubError::KeysetDamaged { .. } => { + "The account keys on the server do not verify. The server may have been tampered with; nothing was changed." + .to_owned() + } + other => capitalize(&other.to_string()), + } +} + +fn capitalize(text: &str) -> String { + let mut chars = text.chars(); + match chars.next() { + Some(first) => first.to_uppercase().chain(chars).collect::() + ".", + None => String::new(), + } +} + +/// The sentence for a failed attempt to open the account keys. +pub(crate) fn account_key_problem(error: &nocterm_hubs_model::AccountKeyError) -> String { + use nocterm_hubs_model::AccountKeyError as Error; + match error { + Error::WrongPassword => { + "That password does not open the account keys. Check it and try again.".to_owned() + } + Error::Offline => { + "The server cannot be reached, and this device keeps no copy of the keys to open offline." + .to_owned() + } + Error::NotSignedIn => { + "This device is not signed in to the server. Sign in first.".to_owned() + } + Error::Failed { .. } => "The account keys could not be opened.".to_owned(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_hubs_model::HubAddressError; + + #[test] + fn a_refused_address_reads_as_a_sentence() { + let error = SignInError::Address(HubAddressError::Empty); + assert_eq!(sign_in_problem(&error), "The hub address is empty."); + } + + #[test] + fn refused_credentials_name_the_user_name_and_password() { + assert_eq!( + hub_problem(&HubError::InvalidCredentials), + "The user name or password was not accepted." + ); + } +} diff --git a/crates/hubs-ui/src/ports.rs b/crates/hubs-ui/src/ports.rs new file mode 100644 index 00000000..52499671 --- /dev/null +++ b/crates/hubs-ui/src/ports.rs @@ -0,0 +1,39 @@ +//! What the views of this crate need from the application, named for the consumer. + +use gpui_kit::{App, Window}; +use nocterm_core::error::SharedCause; +use nocterm_hubs_model::{Environment, HubError, HubId}; + +/// Where an environment is opened. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Placement { + /// Instead of what the window shows now. + ThisWindow, + /// In a window of its own; the window asking is left alone. + NewWindow, +} + +/// Why an environment could not be opened. +#[derive(Debug, thiserror::Error)] +pub enum EnvironmentOpenError { + #[error("no such server: {0}")] + UnknownHub(HubId), + #[error("the account of the server cannot be opened")] + Account(#[source] HubError), + #[error("the window could not be opened")] + Window(#[source] SharedCause), +} + +/// Opens an environment in a window. The application implements it: only it can compose a +/// window. +pub trait EnvironmentOpener { + /// Opens `environment` in `placement`. [`Placement::ThisWindow`] replaces the window `window` + /// shows; the replacement may happen after this call returns. + fn open( + &self, + environment: Environment, + placement: Placement, + window: &mut Window, + cx: &mut App, + ) -> Result<(), EnvironmentOpenError>; +} diff --git a/crates/hubs-ui/src/problems.rs b/crates/hubs-ui/src/problems.rs new file mode 100644 index 00000000..0b735088 --- /dev/null +++ b/crates/hubs-ui/src/problems.rs @@ -0,0 +1,97 @@ +//! The dialogs for hubs that cannot be signed in to: a certificate that is not the trusted +//! one, and a protocol this Nocterm does not speak. Neither offers a way to accept what the +//! hub presents. + +use gpui_kit::{ + App, ClickEvent, Entity, Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + v_flex, + }, + prelude::*, + px, +}; +use nocterm_hubs::{HubSummary, Hubs}; +use nocterm_hubs_model::Fingerprint; + +use crate::layout::{fingerprint_block, note, problem}; + +/// Tells that the certificate of `hub` is not the trusted one, shows both fingerprints, and +/// offers to forget the server. Signing in again to the same server then asks anew. +pub(crate) fn open_trust_failed( + hubs: Entity, + hub: HubSummary, + pinned: Option, + presented: Fingerprint, + window: &mut Window, + cx: &mut App, +) { + window.open_alert_dialog(cx, move |alert, _, cx| { + let (summary, hubs) = (hub.clone(), hubs.clone()); + let remove = Button::new("trust-failed-remove") + .label("Remove this server") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + crate::manage::remove(&hubs, &summary.id, window, cx); + window.close_dialog(cx); + }); + let close = Button::new("trust-failed-close") + .primary() + .label("Close") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + }); + let (heading, reason) = match &pinned { + Some(_) => ( + "The certificate changed", + "The server presents another certificate than the one pinned when it was added. Nocterm will not connect until you have checked why.", + ), + None => ( + "The certificate is not trusted", + "The server's certificate is not issued by a public authority and none is pinned. Nocterm will not connect.", + ), + }; + let mut details = v_flex().gap_3().child(problem(reason.to_owned(), cx)); + if let Some(pinned) = &pinned { + details = details.child(fingerprint_block("Pinned", pinned, cx)); + } + details = details + .child(fingerprint_block("Presented now", &presented, cx)) + .child(note( + "Remove the server and sign in again only if you can confirm the new fingerprint with whoever runs it.", + cx, + )); + alert + .title(format!("{}: {heading}", hub.label)) + .width(px(480.)) + .description(details) + .on_ok(|_, _, _| true) + .footer(DialogFooter::new().child(remove).child(close)) + }); +} + +/// Tells that `hub` and this client speak different protocol versions. +pub(crate) fn open_upgrade_required( + hub: &HubSummary, + hub_version: u32, + client_version: u32, + window: &mut Window, + cx: &mut App, +) { + let label = hub.label.clone(); + window.open_alert_dialog(cx, move |alert, _, _| { + let newer = if hub_version > client_version { + "Update Nocterm to connect." + } else { + "The server needs an update before this Nocterm can connect." + }; + alert + .title(format!("{label}: update needed")) + .description(format!( + "The server speaks protocol {hub_version} and this Nocterm speaks {client_version}. {newer}" + )) + .ok_text("Close") + .show_cancel(false) + }); +} diff --git a/crates/hubs-ui/src/servers.rs b/crates/hubs-ui/src/servers.rs new file mode 100644 index 00000000..90dbb502 --- /dev/null +++ b/crates/hubs-ui/src/servers.rs @@ -0,0 +1,242 @@ +//! The Servers page of the settings: the known servers in a list, and the selected one in +//! tabs: its name, colour, status and certificate with the ways to sign out of it or remove it, +//! the devices of the account, its vault keys, its agents and providers, and the agent key +//! with the servers connected to the agents. +//! +//! The page edits through the service and never decides who may be trusted: a certificate is +//! shown, never accepted, and the only way out of a changed one is to remove the server. The +//! page owns only which server and which tab are selected; each tab owns its own state. + +mod detail; +mod list; +mod tabs; + +use std::time::Duration; + +use gpui_kit::{ + App, Context, ElementId, Entity, FocusHandle, Focusable, Subscription, Task, + TestSupportExt as _, Window, + component::{ActiveTheme as _, StyledExt as _, h_flex, v_flex}, + div, + prelude::*, +}; +use nocterm_hubs::HubSummary; +use nocterm_hubs::{HubStatus, Hubs}; +use nocterm_hubs_model::HubId; +use nocterm_ui::{IconName, SettingsPage, SettingsPageSpec}; + +use crate::installation::Installed; +use crate::layout::note; +use detail::{Access, Tab, TabViews, detail}; +use list::server_list; +use tabs::{ + account_keys::AccountKeys, agent_access::AgentAccessTab, agents::AgentsTab, devices::Devices, + names::NameFields, +}; + +/// The Servers page of the settings, listed with the other pages by the application. +pub fn settings_page(installed: &Installed) -> SettingsPageSpec { + let hubs = installed.hubs().clone(); + let agents = installed.hub_agents().cloned(); + SettingsPageSpec::new("servers", "Servers", move |_, cx| { + let (hubs, agents) = (hubs.clone(), agents.clone()); + cx.new(|cx| ServersPage::new(hubs, agents, cx)) + }) + .with_icon(IconName::Server) +} + +/// How often the countdown of an offline server is redrawn. +const TICK: Duration = Duration::from_secs(1); + +/// The id of the element of kind `kind` of the server at `address`. +pub(crate) fn item(kind: &str, address: &str) -> ElementId { + ElementId::Name(format!("{kind}-{address}").into()) +} + +/// The settings page that lists the known servers and shows one of them. +pub(crate) struct ServersPage { + hubs: Entity, + /// The server the person chose; the first one while it is none or no longer listed. + selected: Option, + tab: Tab, + names: NameFields, + devices: Entity, + keys: Entity, + agents: Option>, + access: Option>, + focus: FocusHandle, + _redraw: Task<()>, + _hubs: Subscription, +} + +impl ServersPage { + pub(crate) fn new( + hubs: Entity, + agents: Option>, + cx: &mut Context, + ) -> Self { + let redraw = cx.spawn(async move |this, cx| { + loop { + cx.background_executor().timer(TICK).await; + let alive = this.update(cx, |page, cx| { + if page.counts_down(cx) { + cx.notify(); + } + }); + if alive.is_err() { + break; + } + } + }); + Self { + selected: None, + tab: Tab::Overview, + names: NameFields::new(hubs.clone()), + devices: cx.new(|_| Devices::new(hubs.clone())), + keys: cx.new(|_| AccountKeys::new(hubs.clone())), + access: agents + .clone() + .map(|service| cx.new(|cx| AgentAccessTab::new(service, cx))), + agents: agents.map(|service| cx.new(|cx| AgentsTab::new(service, cx))), + focus: cx.focus_handle(), + _redraw: redraw, + _hubs: cx.observe(&hubs, |_, _, cx| cx.notify()), + hubs, + } + } + + /// Whether a server shows a countdown that must be redrawn. + fn counts_down(&self, cx: &App) -> bool { + self.hubs + .read(cx) + .list() + .iter() + .any(|summary| matches!(summary.status, HubStatus::Offline { .. })) + } + + /// Shows `hub`, from its first tab. + fn select_hub(&mut self, hub: HubId, cx: &mut Context) { + if self.selected.as_ref() != Some(&hub) { + self.tab = Tab::Overview; + } + self.selected = Some(hub); + cx.notify(); + } + + fn select_tab(&mut self, tab: Tab, cx: &mut Context) { + self.tab = tab; + cx.notify(); + } +} + +impl SettingsPage for ServersPage { + fn flush(&mut self, window: &mut Window, cx: &mut Context) { + self.names.flush(window, cx); + } +} + +impl Focusable for ServersPage { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +/// The server whose `wanted` tab is open, while this device can reach its account. +fn on_show(shown: Option<&HubSummary>, current: Tab, wanted: Tab) -> Option { + shown + .filter(|summary| current == wanted && Access::of(&summary.status) == Access::SignedIn) + .map(|summary| summary.id.clone()) +} + +impl Render for ServersPage { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.names.sync(window, cx); + let summaries = self.hubs.read(cx).list(); + let shown = self + .selected + .as_ref() + .and_then(|hub| summaries.iter().find(|summary| &summary.id == hub)) + .or(summaries.first()); + // The devices are read only while their tab is on show, and again each time it is. + let devices_on_show = on_show(shown, self.tab, Tab::Devices); + self.devices + .update(cx, |devices, cx| match &devices_on_show { + Some(hub) => devices.show(hub, cx), + None => devices.hide(), + }); + if let Some(hub) = on_show(shown, self.tab, Tab::Keys) { + self.keys.update(cx, |keys, cx| keys.show(&hub, cx)); + } + // The agents and the agent servers are read again each time their tab is on show. + let agents_on_show = on_show(shown, self.tab, Tab::Agents); + if let Some(tab) = &self.agents { + tab.update(cx, |tab, cx| match &agents_on_show { + Some(hub) => tab.show(hub, cx), + None => tab.hide(), + }); + } + let access_on_show = on_show(shown, self.tab, Tab::AgentAccess); + if let Some(tab) = &self.access { + tab.update(cx, |tab, cx| match &access_on_show { + Some(hub) => tab.show(hub, cx), + None => tab.hide(), + }); + } + let body = shown.and_then(|summary| { + let name = self.names.get(&summary.id)?; + Some(detail( + summary, + &name, + self.tab, + TabViews { + devices: &self.devices, + keys: &self.keys, + agents: self.agents.as_ref(), + access: self.access.as_ref(), + }, + cx.weak_entity(), + &self.hubs, + cx, + )) + }); + let list = server_list(&summaries, shown.map(|summary| &summary.id), cx); + v_flex() + .id("servers-page") + .test_support() + .track_focus(&self.focus) + .size_full() + .p_4() + .gap_4() + .child( + v_flex() + .gap_1() + .child(div().text_lg().font_semibold().child("Servers")) + .child(note( + "Nocterm hubs this device is signed in to. Credentials stay in a private file on this device.", + cx, + )), + ) + .child( + h_flex() + .gap_4() + .items_start() + .child(list) + .children(body) + .when(summaries.is_empty(), |page| { + page.child(div().id("servers-empty").test_support().child(note( + "No servers yet. Sign in to one to use your hub from this computer.", + cx, + ))) + }), + ) + .text_color(cx.theme().foreground) + } +} + +#[cfg(test)] +#[path = "servers_tests.rs"] +mod tests; + +#[cfg(test)] +#[path = "servers_agents_tests.rs"] +mod agent_tests; diff --git a/crates/hubs-ui/src/servers/detail.rs b/crates/hubs-ui/src/servers/detail.rs new file mode 100644 index 00000000..d11818f1 --- /dev/null +++ b/crates/hubs-ui/src/servers/detail.rs @@ -0,0 +1,198 @@ +//! The selected server on the right of the Servers page: its name and the tabs. + +use gpui_kit::{ + App, ClickEvent, Entity, TestSupportExt as _, WeakEntity, Window, + component::{ + ActiveTheme as _, Icon, StyledExt as _, + button::{Button, ButtonVariants as _}, + h_flex, + tab::{Tab as TabButton, TabBar}, + v_flex, + }, + div, + prelude::*, +}; +use nocterm_hubs::{HubStatus, HubSummary, Hubs}; +use nocterm_ui::SettingsTextField; + +use super::tabs::{ + account_keys::AccountKeys, agent_access::AgentAccessTab, agents::AgentsTab, devices::Devices, + overview::overview, trust::trust, +}; +use super::{ServersPage, item}; +use crate::color::tint; +use crate::layout::note; +use crate::status::status_icon; + +/// The tabs of a server. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Tab { + Overview, + Devices, + Keys, + Agents, + AgentAccess, +} + +impl Tab { + const ALL: [Self; 5] = [ + Self::Overview, + Self::Devices, + Self::Keys, + Self::Agents, + Self::AgentAccess, + ]; + + fn title(self) -> &'static str { + match self { + Self::Overview => "Overview", + Self::Devices => "Devices", + Self::Keys => "Keys", + Self::Agents => "Agents", + Self::AgentAccess => "Agent access", + } + } +} + +/// The views of the tabs that keep their own state. +pub(super) struct TabViews<'a> { + pub(super) devices: &'a Entity, + pub(super) keys: &'a Entity, + pub(super) agents: Option<&'a Entity>, + pub(super) access: Option<&'a Entity>, +} + +/// Whether the account of a server can be reached from this device. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Access { + /// This device holds credentials. + SignedIn, + /// Signing in comes first. + SignedOut, + /// The certificate or the protocol stands in the way; the Overview says which. + Blocked, +} + +impl Access { + pub(super) fn of(status: &HubStatus) -> Self { + match status { + HubStatus::SignedOut | HubStatus::Revoked => Self::SignedOut, + HubStatus::TrustFailed { .. } | HubStatus::UpgradeRequired { .. } => Self::Blocked, + HubStatus::Online | HubStatus::Connecting | HubStatus::Offline { .. } => Self::SignedIn, + } + } +} + +/// The server's name and the tab `tab` of it. +pub(super) fn detail( + summary: &HubSummary, + name: &SettingsTextField, + tab: Tab, + views: TabViews<'_>, + page: WeakEntity, + hubs: &Entity, + cx: &App, +) -> gpui_kit::AnyElement { + let key = summary.address.to_string(); + let theme = cx.theme(); + let mark = summary + .color + .map_or(theme.muted_foreground, |color| tint(color, cx)); + let selected = Tab::ALL.iter().position(|each| *each == tab).unwrap_or(0); + let tabs = TabBar::new("servers-tabs") + .underline() + .selected_index(selected) + .children(Tab::ALL.map(|tab| TabButton::new().label(tab.title()))) + .on_click(move |index, _, cx| { + let tab = Tab::ALL[*index]; + let _ = page.update(cx, |page, cx| page.select_tab(tab, cx)); + }); + let Some(record) = hubs.read(cx).record(&summary.id).cloned() else { + return div().into_any_element(); + }; + let body = match (tab, Access::of(&summary.status)) { + (Tab::Overview, _) => overview(summary, &record, name, hubs, cx), + (_, Access::SignedOut) => signed_out(summary, hubs, &key, cx), + (_, Access::Blocked) => blocked(&key, cx), + (Tab::Devices, Access::SignedIn) => views.devices.clone().into_any_element(), + (Tab::Keys, Access::SignedIn) => trust(summary, hubs, views.keys, cx), + (Tab::Agents, Access::SignedIn) => match views.agents { + Some(tab) => tab.clone().into_any_element(), + None => without_agents(cx), + }, + (Tab::AgentAccess, Access::SignedIn) => match views.access { + Some(tab) => tab.clone().into_any_element(), + None => without_agents(cx), + }, + }; + v_flex() + .id(item("server", &key)) + .test_support() + .flex_1() + .min_w_0() + .gap_3() + .child( + h_flex() + .gap_2() + .items_center() + .child(Icon::new(status_icon(&summary.status)).text_color(mark)) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .font_semibold() + .child(summary.label.clone()), + ), + ) + .child(tabs) + .child(body) + .into_any_element() +} + +/// The empty state of a tab that needs the account, with the way to sign in. +fn signed_out( + summary: &HubSummary, + hubs: &Entity, + key: &str, + cx: &App, +) -> gpui_kit::AnyElement { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + v_flex() + .id(item("server-tab-signed-out", key)) + .test_support() + .gap_2() + .items_start() + .child(note( + "This device is not signed in to this server. Sign in to see this.", + cx, + )) + .child( + Button::new(item("server-tab-sign-in", key)) + .primary() + .label("Sign in") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + crate::sign_in::start(&hubs, Some(&id), None, window, cx); + }), + ) + .into_any_element() +} + +/// The agent tabs of an application composed without the agents service. +fn without_agents(cx: &App) -> gpui_kit::AnyElement { + div() + .child(note("This build has no hub agents.", cx)) + .into_any_element() +} + +/// The empty state of a tab while the server cannot be trusted or spoken to. +fn blocked(key: &str, cx: &App) -> gpui_kit::AnyElement { + div() + .id(item("server-tab-blocked", key)) + .test_support() + .child(note( + "This is not available while the server cannot be reached safely. The Overview tab says why.", + cx, + )) + .into_any_element() +} diff --git a/crates/hubs-ui/src/servers/list.rs b/crates/hubs-ui/src/servers/list.rs new file mode 100644 index 00000000..0d99c832 --- /dev/null +++ b/crates/hubs-ui/src/servers/list.rs @@ -0,0 +1,95 @@ +//! The compact list of known servers on the left of the Servers page. + +use gpui_kit::{ + App, ClickEvent, Context, TestSupportExt as _, WeakEntity, Window, + component::{ + ActiveTheme as _, Icon, + button::{Button, ButtonVariants as _}, + h_flex, v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::HubSummary; +use nocterm_hubs_model::HubId; + +use super::{ServersPage, item}; +use crate::SignInToServer; +use crate::color::tint; +use crate::status::{status_chip, status_icon}; + +/// The servers, the one in `selected` marked, and the button that signs in to another. +pub(super) fn server_list( + summaries: &[HubSummary], + selected: Option<&HubId>, + cx: &mut Context, +) -> gpui_kit::AnyElement { + let page = cx.weak_entity(); + let rows: Vec<_> = summaries + .iter() + .map(|summary| entry(summary, selected == Some(&summary.id), page.clone(), cx)) + .collect(); + v_flex() + .id("servers-list") + .test_support() + .w(px(240.)) + .flex_none() + .gap_2() + .child(v_flex().gap_1().children(rows)) + .child( + Button::new("servers-add") + .primary() + .label("Sign in to a server…") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.dispatch_action(Box::new(SignInToServer), cx); + }), + ) + .into_any_element() +} + +/// One server: its colour mark, its name and a dot in the colour of its status. +fn entry( + summary: &HubSummary, + selected: bool, + page: WeakEntity, + cx: &App, +) -> impl IntoElement { + let key = summary.address.to_string(); + let theme = cx.theme(); + let (_, status_color) = status_chip(&summary.status, cx); + let mark = summary + .color + .map_or(theme.muted_foreground, |color| tint(color, cx)); + let id = summary.id.clone(); + h_flex() + .id(item("server-select", &key)) + .test_support() + .gap_2() + .px_2() + .py_1p5() + .items_center() + .rounded(theme.radius) + .cursor_pointer() + .when(selected, |entry| entry.bg(theme.accent)) + .child(Icon::new(status_icon(&summary.status)).text_color(mark)) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .child(summary.label.clone()), + ) + .child( + div() + .id(item("server-dot", &key)) + .test_support() + .size_2() + .flex_none() + .rounded_full() + .bg(status_color), + ) + .on_click(move |_, _, cx| { + let _ = page.update(cx, |page, cx| page.select_hub(id.clone(), cx)); + }) +} diff --git a/crates/hubs-ui/src/servers/tabs.rs b/crates/hubs-ui/src/servers/tabs.rs new file mode 100644 index 00000000..7cdd37d5 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs.rs @@ -0,0 +1,9 @@ +//! The tabs of one server on the Servers page, each an owner of its own state. + +pub(super) mod account_keys; +pub(super) mod agent_access; +pub(super) mod agents; +pub(super) mod devices; +pub(super) mod names; +pub(super) mod overview; +pub(super) mod trust; diff --git a/crates/hubs-ui/src/servers/tabs/account_keys.rs b/crates/hubs-ui/src/servers/tabs/account_keys.rs new file mode 100644 index 00000000..b3d6b2cf --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/account_keys.rs @@ -0,0 +1,165 @@ +//! The state of the account keys on this device, with the password prompt that opens them and +//! the button that closes them. +//! +//! The keys open the account's vault (saved servers and their credentials); chats and agents +//! never need them. They follow their own lock policy (`[hubs] key_lock_minutes`), not the +//! local vault's. This view never decides what the password opens: it hands the typed secret +//! to the service and reports the outcome. + +use gpui_kit::{ + ClickEvent, Context, Entity, Focusable as _, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Disableable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, + input::{Input, InputState}, + v_flex, + }, + div, + prelude::*, +}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::HubId; +use nocterm_session::Secret; + +use crate::layout::{field, note, problem}; +use crate::messages::account_key_problem; + +/// The lock of the account keys of the server that is shown. +pub(in crate::servers) struct AccountKeys { + hubs: Entity, + hub: Option, + /// Created with the first frame, because a field needs a window. + password: Option>, + /// Why the last attempt to unlock failed, as a sentence. + refusal: Option, + unlocking: Option>, +} + +impl AccountKeys { + pub(in crate::servers) fn new(hubs: Entity) -> Self { + Self { + hubs, + hub: None, + password: None, + refusal: None, + unlocking: None, + } + } + + /// Shows the keys of `hub`; another server forgets the last refusal. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() != Some(hub) { + self.hub = Some(hub.clone()); + self.refusal = None; + self.unlocking = None; + cx.notify(); + } + } + + fn lock(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + self.hubs.read(cx).lock_account_keys(&hub); + cx.notify(); + } + + fn unlock(&mut self, window: &mut Window, cx: &mut Context) { + let (Some(hub), Some(input)) = (self.hub.clone(), self.password.clone()) else { + return; + }; + let typed = input.read(cx).value().to_string(); + input.update(cx, |input, cx| input.set_value("", window, cx)); + let unlocking = self + .hubs + .read(cx) + .unlock_account_keys(&hub, Secret::new(typed)); + self.refusal = None; + self.unlocking = Some(cx.spawn(async move |this, cx| { + let result = unlocking.await; + this.update(cx, |keys, cx| { + keys.refusal = result.err().map(|error| account_key_problem(&error)); + cx.notify(); + }) + .ok(); + })); + cx.notify(); + } + + fn prompt(&mut self, window: &mut Window, cx: &mut Context) -> gpui_kit::AnyElement { + if self.password.is_none() { + let input = cx.new(|cx| { + InputState::new(window, cx) + .masked(true) + .placeholder("Password") + }); + // The prompt appears because the user needs it: type at once. + window.focus(&input.focus_handle(cx), cx); + self.password = Some(input); + } + let input = self.password.clone().expect("created above"); + let busy = self.unlocking.is_some(); + v_flex() + .id("account-keys-locked") + .test_support() + .gap_2() + .child(note( + "The account keys are closed on this device. Type your password (or the vault passphrase of an account that signs in through a provider) to open them.", + cx, + )) + .child(field("Password", Input::new(&input))) + .children( + self.refusal + .clone() + .map(|text| div().id("account-keys-refused").test_support().child(problem(text, cx))), + ) + .child( + h_flex().child( + Button::new("account-keys-unlock") + .primary() + .label("Unlock") + .disabled(busy) + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.unlock(window, cx); + })), + ), + ) + .into_any_element() + } + + fn open(&self, cx: &mut Context) -> gpui_kit::AnyElement { + let theme = cx.theme(); + h_flex() + .id("account-keys-open") + .test_support() + .gap_3() + .items_center() + .child( + div() + .flex_1() + .text_xs() + .text_color(theme.muted_foreground) + .child("Account keys are open on this device."), + ) + .child( + Button::new("account-keys-lock") + .small() + .label("Lock account keys") + .on_click(cx.listener(|this, _: &ClickEvent, _: &mut Window, cx| { + this.lock(cx); + })), + ) + .into_any_element() + } +} + +impl Render for AccountKeys { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + match self.hub.clone() { + None => div().into_any_element(), + Some(hub) if self.hubs.read(cx).account_keys_open(&hub) => self.open(cx), + Some(_) => self.prompt(window, cx), + } + } +} diff --git a/crates/hubs-ui/src/servers/tabs/agent_access.rs b/crates/hubs-ui/src/servers/tabs/agent_access.rs new file mode 100644 index 00000000..9ed7d57d --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agent_access.rs @@ -0,0 +1,287 @@ +//! The Agent access tab of a server: the account's agent key, and the servers connected to the +//! agents with what agents last found there. +//! +//! A server is connected from the connection editor of a saved server ("Agent access"), which +//! writes the key's line to its `authorized_keys`. Here the person checks a server again, +//! disconnects it (agents stop using it at once; the line stays until it is removed on the +//! server) and replaces the key (every server then needs its line written again). + +use gpui_kit::{ + ClickEvent, ClipboardItem, Context, Entity, Subscription, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Sizable as _, StyledExt as _, WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + h_flex, v_flex, + }, + div, + prelude::*, +}; +use nocterm_hub_agents::HubAgents; +use nocterm_hub_agents_model::{AgentKey, HubServer, ServerId}; +use nocterm_hubs_model::HubId; + +use crate::layout::{field, note, problem}; +use crate::messages::agents_problem; +use crate::servers::item; + +/// The agent key and servers of the server that is shown. +pub(in crate::servers) struct AgentAccessTab { + service: Entity, + hub: Option, + key: Option, + problem: Option, + working: Option>, + _service: Subscription, +} + +impl AgentAccessTab { + pub(in crate::servers) fn new(service: Entity, cx: &mut Context) -> Self { + Self { + _service: cx.observe(&service, |_, _, cx| cx.notify()), + service, + hub: None, + key: None, + problem: None, + working: None, + } + } + + /// Shows the agent key and servers of `hub`, read again when another server was shown. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() == Some(hub) { + return; + } + self.hub = Some(hub.clone()); + self.key = None; + self.problem = None; + self.service.update(cx, |service, cx| { + service.track(hub, cx); + service.refresh(hub, cx); + }); + let reading = self.service.read(cx).server_port().agent_key(hub); + self.key_from(reading, cx); + } + + pub(in crate::servers) fn hide(&mut self) { + self.hub = None; + self.working = None; + } + + fn key_from( + &mut self, + reading: nocterm_hub_agents_model::AgentsFuture, + cx: &mut Context, + ) { + self.working = Some(cx.spawn(async move |this, cx| { + let result = reading.await; + this.update(cx, |tab, cx| { + match result { + Ok(key) => tab.key = Some(key), + Err(error) => tab.problem = Some(agents_problem(&error)), + } + if let Some(hub) = tab.hub.clone() { + tab.service + .update(cx, |service, cx| service.refresh(&hub, cx)); + } + cx.notify(); + }) + .ok(); + })); + } + + fn rotate(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let rotating = self.service.read(cx).server_port().rotate_agent_key(&hub); + self.key_from(rotating, cx); + } + + /// Runs a call on one server, then lists the servers again. + fn on_server( + &mut self, + call: nocterm_hub_agents_model::AgentsFuture, + cx: &mut Context, + ) { + self.problem = None; + self.working = Some(cx.spawn(async move |this, cx| { + let result = call.await; + this.update(cx, |tab, cx| { + if let Err(error) = result { + tab.problem = Some(agents_problem(&error)); + } + if let Some(hub) = tab.hub.clone() { + tab.service + .update(cx, |service, cx| service.refresh(&hub, cx)); + } + cx.notify(); + }) + .ok(); + })); + cx.notify(); + } + + fn check(&mut self, server: &ServerId, cx: &mut Context) { + if let Some(hub) = self.hub.clone() { + let checking = self + .service + .read(cx) + .server_port() + .check_server(&hub, server); + self.on_server(checking, cx); + } + } + + fn disconnect(&mut self, server: &ServerId, cx: &mut Context) { + if let Some(hub) = self.hub.clone() { + let removing = self + .service + .read(cx) + .server_port() + .remove_server(&hub, server); + self.on_server(removing, cx); + } + } + + /// Asks before replacing the key: every server needs its line written again. + fn confirm_rotate(view: Entity, window: &mut Window, cx: &mut gpui_kit::App) { + window.open_alert_dialog(cx, move |alert, _, _| { + let view = view.clone(); + let confirm = Button::new("agent-key-rotate-confirm") + .danger() + .label("Replace the key") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut gpui_kit::App| { + window.close_dialog(cx); + view.update(cx, |tab, cx| tab.rotate(cx)); + }); + let keep = Button::new("agent-key-rotate-keep") + .primary() + .label("Keep it") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut gpui_kit::App| { + window.close_dialog(cx); + }); + alert + .title("Replace the agent key?") + .description( + "Agents stop signing in to every connected server until its line is written again: turn Agent access off and on in each server's editor.", + ) + .footer(DialogFooter::new().child(keep).child(confirm)) + }); + } + + fn render_key(&self, key: &str, cx: &mut Context) -> gpui_kit::AnyElement { + let Some(agent_key) = &self.key else { + return note("Reading the agent key…", cx).into_any_element(); + }; + let line = agent_key.authorized_line.clone(); + field( + "Agent key", + v_flex() + .id(item("agent-key", key)) + .test_support() + .gap_1() + .child( + div() + .font_family(cx.theme().mono_font_family.clone()) + .text_sm() + .child(agent_key.fingerprint.clone()), + ) + .child(note( + "Agents sign in to your servers with this key. Its line goes into authorized_keys with port, agent and X11 forwarding off.", + cx, + )) + .child( + h_flex() + .gap_2() + .child( + Button::new(item("agent-key-copy", key)) + .small() + .label("Copy line") + .on_click(move |_: &ClickEvent, _, cx: &mut gpui_kit::App| { + cx.write_to_clipboard(ClipboardItem::new_string(line.clone())); + }), + ) + .child( + Button::new(item("agent-key-rotate", key)) + .small() + .ghost() + .label("Replace key") + .on_click(cx.listener(|_, _: &ClickEvent, window, cx| { + Self::confirm_rotate(cx.entity(), window, cx); + })), + ), + ), + ) + .into_any_element() + } + + fn render_server(server: &HubServer, cx: &mut Context) -> gpui_kit::AnyElement { + let (check, remove) = (server.id.clone(), server.id.clone()); + let status = if server.status_message.is_empty() { + server.status.label().to_owned() + } else { + format!("{} — {}", server.status.label(), server.status_message) + }; + h_flex() + .id(item("agent-server", server.id.as_str())) + .test_support() + .gap_2() + .child( + v_flex() + .flex_1() + .min_w_0() + .child(div().text_sm().font_semibold().child(server.name.clone())) + .child(note( + format!("{}@{}:{} · {status}", server.user, server.host, server.port), + cx, + )), + ) + .child( + Button::new(item("agent-server-check", server.id.as_str())) + .small() + .ghost() + .label("Check") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| tab.check(&check, cx))), + ) + .child( + Button::new(item("agent-server-disconnect", server.id.as_str())) + .small() + .ghost() + .label("Disconnect") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + tab.disconnect(&remove, cx); + })), + ) + .into_any_element() + } +} + +impl Render for AgentAccessTab { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + let Some(hub) = self.hub.clone() else { + return div().into_any_element(); + }; + let key = hub.to_string(); + let servers = self.service.read(cx).servers(&hub).to_vec(); + let rows: Vec<_> = servers + .iter() + .map(|server| Self::render_server(server, cx)) + .collect(); + v_flex() + .id(item("agent-access-tab", &key)) + .test_support() + .gap_3() + .children(self.problem.clone().map(|text| problem(text, cx))) + .child(self.render_key(&key, cx)) + .child(div().font_semibold().child("Servers agents may use")) + .when(rows.is_empty(), |tab| { + tab.child(note( + "None yet. Open a saved server in this window's connection editor and turn on Agent access.", + cx, + )) + }) + .children(rows) + .into_any_element() + } +} diff --git a/crates/hubs-ui/src/servers/tabs/agents.rs b/crates/hubs-ui/src/servers/tabs/agents.rs new file mode 100644 index 00000000..43431b42 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agents.rs @@ -0,0 +1,258 @@ +//! The Agents tab of a server: the account's agents, created, edited and deleted in one form +//! (name, instructions, provider and models, default servers, approval mode), and the +//! providers the agents use, with the account's own ones added, changed, tested and removed. +//! +//! Every write goes to the hub through the agents port; the tab then lists the hub again. A +//! provider key is typed once and never shown again: the hub keeps it. + +mod form; +mod providers; + +use gpui_kit::{ + ClickEvent, Context, Entity, Subscription, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Sizable as _, StyledExt as _, + button::{Button, ButtonVariants as _}, + h_flex, v_flex, + }, + div, + prelude::*, +}; +use nocterm_hub_agents::HubAgents; +use nocterm_hub_agents_model::{AgentId, HubAgent, HubAgentsError, Provider}; +use nocterm_hubs_model::HubId; + +use crate::layout::{note, problem}; +use crate::messages::agents_problem; +use crate::servers::item; +use form::AgentForm; +use providers::ProviderForm; + +/// The agents and providers of the server that is shown, and the form open on one of them. +pub(in crate::servers) struct AgentsTab { + service: Entity, + hub: Option, + providers: Vec, + agent_form: Option, + provider_form: Option, + problem: Option, + working: Option>, + _service: Subscription, +} + +impl AgentsTab { + pub(in crate::servers) fn new(service: Entity, cx: &mut Context) -> Self { + Self { + _service: cx.observe(&service, |_, _, cx| cx.notify()), + service, + hub: None, + providers: Vec::new(), + agent_form: None, + provider_form: None, + problem: None, + working: None, + } + } + + /// Shows the agents of `hub`; another server closes the forms. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() == Some(hub) { + return; + } + self.hub = Some(hub.clone()); + self.agent_form = None; + self.provider_form = None; + self.problem = None; + self.service.update(cx, |service, cx| { + service.track(hub, cx); + service.refresh(hub, cx); + }); + self.reload_providers(cx); + } + + pub(in crate::servers) fn hide(&mut self) { + self.hub = None; + self.working = None; + self.agent_form = None; + self.provider_form = None; + } + + fn reload_providers(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let listing = self.service.read(cx).directory().providers(&hub); + self.working = Some(cx.spawn(async move |this, cx| { + let result = listing.await; + this.update(cx, |tab, cx| { + match result { + Ok(providers) => tab.providers = providers, + Err(error) => tab.problem = Some(agents_problem(&error)), + } + cx.notify(); + }) + .ok(); + })); + } + + /// Runs a write against the hub, then lists the agents and providers again. + fn write(&mut self, call: nocterm_hub_agents_model::AgentsFuture<()>, cx: &mut Context) { + self.problem = None; + self.working = Some(cx.spawn(async move |this, cx| { + let result = call.await; + this.update(cx, |tab, cx| tab.written(result, cx)).ok(); + })); + cx.notify(); + } + + fn written(&mut self, result: Result<(), HubAgentsError>, cx: &mut Context) { + match result { + Ok(()) => { + self.agent_form = None; + self.provider_form = None; + } + Err(error) => self.problem = Some(agents_problem(&error)), + } + if let Some(hub) = self.hub.clone() { + self.service + .update(cx, |service, cx| service.refresh(&hub, cx)); + } + self.reload_providers(cx); + cx.notify(); + } + + fn save_agent(&mut self, cx: &mut Context) { + let (Some(hub), Some(form)) = (self.hub.clone(), &self.agent_form) else { + return; + }; + let draft = form.draft(cx); + let directory = self.service.read(cx).directory().clone(); + let call: nocterm_hub_agents_model::AgentsFuture<()> = match form.editing().cloned() { + Some(agent) => { + let saving = directory.update_agent(&hub, &agent, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + None => { + let saving = directory.create_agent(&hub, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + }; + self.write(call, cx); + } + + fn delete_agent(&mut self, agent: &AgentId, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let deleting = self.service.read(cx).directory().delete_agent(&hub, agent); + self.write(deleting, cx); + } + + fn open_agent( + &mut self, + agent: Option<&HubAgent>, + window: &mut Window, + cx: &mut Context, + ) { + self.agent_form = Some(AgentForm::new(agent, window, cx)); + self.provider_form = None; + cx.notify(); + } + + fn render_agents(&self, hub: &HubId, cx: &mut Context) -> gpui_kit::AnyElement { + let key = hub.to_string(); + let agents = self.service.read(cx).agents(hub).to_vec(); + let mut list = v_flex() + .id(item("server-agents", &key)) + .test_support() + .gap_1(); + if agents.is_empty() { + list = list.child(note( + "No agents yet. An agent is a name, instructions and a model; it costs nothing while no chat runs it.", + cx, + )); + } + for agent in agents { + let (edit, remove) = (agent.clone(), agent.id.clone()); + list = list.child( + h_flex() + .id(item("server-agent", agent.id.as_str())) + .test_support() + .gap_2() + .child( + v_flex() + .flex_1() + .min_w_0() + .child(div().text_sm().font_semibold().child(agent.name.clone())) + .child(note(format!("{} · {}", agent.model, approval(&agent)), cx)), + ) + .child( + Button::new(item("server-agent-edit", agent.id.as_str())) + .small() + .ghost() + .label("Edit") + .on_click(cx.listener(move |tab, _: &ClickEvent, window, cx| { + tab.open_agent(Some(&edit), window, cx); + })), + ) + .child( + Button::new(item("server-agent-delete", agent.id.as_str())) + .small() + .ghost() + .label("Delete") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + tab.delete_agent(&remove, cx); + })), + ), + ); + } + list.into_any_element() + } +} + +fn approval(agent: &HubAgent) -> &'static str { + match agent.approval { + nocterm_hub_agents_model::ApprovalMode::Auto => "Auto", + nocterm_hub_agents_model::ApprovalMode::Ask => "Ask before changes", + } +} + +impl Render for AgentsTab { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + let Some(hub) = self.hub.clone() else { + return div().into_any_element(); + }; + let key = hub.to_string(); + let servers = self.service.read(cx).servers(&hub).to_vec(); + let agents = self.render_agents(&hub, cx); + let agent_form = self + .agent_form + .as_ref() + .map(|form| form.render(&self.providers, &servers, cx)); + let providers = providers::render(&self.providers, self.provider_form.as_ref(), cx); + v_flex() + .id(item("server-agents-tab", &key)) + .test_support() + .gap_3() + .children(self.problem.clone().map(|text| problem(text, cx))) + .child( + h_flex() + .justify_between() + .child(div().font_semibold().child("Agents")) + .child( + Button::new(item("server-agent-new", &key)) + .small() + .primary() + .label("New agent") + .on_click(cx.listener(|tab, _: &ClickEvent, window, cx| { + tab.open_agent(None, window, cx); + })), + ), + ) + .child(agents) + .children(agent_form) + .child(div().h_px().bg(cx.theme().border)) + .child(providers) + .into_any_element() + } +} diff --git a/crates/hubs-ui/src/servers/tabs/agents/form.rs b/crates/hubs-ui/src/servers/tabs/agents/form.rs new file mode 100644 index 00000000..b5f90307 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agents/form.rs @@ -0,0 +1,237 @@ +//! The form of one agent: name, instructions, provider and models, default servers and whether +//! changing tools wait for an approval. + +use std::collections::BTreeSet; + +use gpui_kit::{ + AppContext as _, ClickEvent, Context, Entity, TestSupportExt as _, Window, + component::{ + Selectable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + checkbox::Checkbox, + h_flex, + input::{Input, InputState, Textarea, TextareaState}, + v_flex, + }, + prelude::*, +}; +use nocterm_hub_agents_model::{ + AgentDraft, AgentId, ApprovalMode, HubAgent, HubServer, Provider, ProviderId, ServerId, +}; + +use super::AgentsTab; +use crate::layout::{field, note}; +use crate::servers::item; + +/// What the person has typed and chosen for an agent. +pub(super) struct AgentForm { + editing: Option, + name: Entity, + instructions: Entity, + models: Entity, + model: Entity, + provider: Option, + approval: ApprovalMode, + servers: BTreeSet, +} + +fn input( + placeholder: &'static str, + text: &str, + window: &mut Window, + cx: &mut Context, +) -> Entity { + let text = text.to_owned(); + cx.new(|cx| { + let mut input = InputState::new(window, cx).placeholder(placeholder); + input.set_value(text, window, cx); + input + }) +} + +impl AgentForm { + /// A form for a new agent, or one that edits `agent`. + pub(super) fn new( + agent: Option<&HubAgent>, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let draft = agent.map(HubAgent::draft).unwrap_or_default(); + let instructions = draft.instructions.clone(); + Self { + editing: agent.map(|agent| agent.id.clone()), + name: input("Name", &draft.name, window, cx), + instructions: cx.new(|cx| { + let mut state = + TextareaState::new(window, cx).placeholder("How the agent should behave"); + state.set_value(instructions, window, cx); + state + }), + models: input( + "Models, separated by commas", + &draft.models.join(", "), + window, + cx, + ), + model: input("Default model", &draft.model, window, cx), + provider: draft.provider, + approval: draft.approval, + servers: draft.servers.into_iter().collect(), + } + } + + pub(super) fn editing(&self) -> Option<&AgentId> { + self.editing.as_ref() + } + + /// The form as a draft; its rules are checked where it is saved. + pub(super) fn draft(&self, cx: &gpui_kit::App) -> AgentDraft { + let text = |input: &Entity| input.read(cx).value().to_string(); + AgentDraft { + name: text(&self.name), + instructions: self.instructions.read(cx).value().to_string(), + provider: self.provider.clone(), + model: text(&self.model), + models: text(&self.models) + .split(',') + .map(|model| model.trim().to_owned()) + .collect(), + approval: self.approval, + servers: self.servers.iter().cloned().collect(), + } + } + + fn choose_provider( + &mut self, + provider: &Provider, + window: &mut Window, + cx: &mut gpui_kit::App, + ) { + self.provider = Some(provider.id.clone()); + // The provider's models are what the agent may use unless the person names others. + if self.models.read(cx).value().trim().is_empty() { + let models = provider.models.join(", "); + self.models + .update(cx, |input, cx| input.set_value(models, window, cx)); + } + } + + pub(super) fn render( + &self, + providers: &[Provider], + servers: &[HubServer], + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let defaults = if servers.is_empty() { + note( + "No servers are connected to agents yet: turn on Agent access of a saved server.", + cx, + ) + .into_any_element() + } else { + self.server_choices(servers, cx).into_any_element() + }; + v_flex() + .id("server-agent-form") + .test_support() + .gap_2() + .child(field("Name", Input::new(&self.name))) + .child(field("Instructions", Textarea::new(&self.instructions))) + .child(field("Provider", self.provider_choices(providers, cx))) + .child(field("Models", Input::new(&self.models))) + .child(field("Default model", Input::new(&self.model))) + .child(self.approval_choice(cx)) + .child(field("Default servers", defaults)) + .child(actions(cx)) + .into_any_element() + } + + fn provider_choices( + &self, + providers: &[Provider], + cx: &mut Context, + ) -> impl IntoElement { + h_flex() + .flex_wrap() + .gap_1() + .children(providers.iter().map(|provider| { + let chosen = provider.clone(); + Button::new(item("server-agent-provider", provider.id.as_str())) + .small() + .ghost() + .label(provider.name.clone()) + .selected(self.provider.as_ref() == Some(&provider.id)) + .on_click(cx.listener(move |tab, _: &ClickEvent, window, cx| { + if let Some(form) = tab.agent_form.as_mut() { + form.choose_provider(&chosen, window, cx); + } + cx.notify(); + })) + })) + } + + fn approval_choice(&self, cx: &mut Context) -> impl IntoElement { + let ask = self.approval == ApprovalMode::Ask; + Checkbox::new("server-agent-ask") + .label("Ask before changes: tools that change something wait for Allow") + .checked(ask) + .on_click(cx.listener(move |tab, _: &bool, _, cx| { + if let Some(form) = tab.agent_form.as_mut() { + form.approval = if ask { + ApprovalMode::Auto + } else { + ApprovalMode::Ask + }; + } + cx.notify(); + })) + } + + fn server_choices( + &self, + servers: &[HubServer], + cx: &mut Context, + ) -> impl IntoElement { + v_flex().gap_1().children(servers.iter().map(|server| { + let id = server.id.clone(); + let on = self.servers.contains(&id); + Checkbox::new(item("server-agent-server", server.id.as_str())) + .label(format!("{} ({}@{})", server.name, server.user, server.host)) + .checked(on) + .on_click(cx.listener(move |tab, _: &bool, _, cx| { + if let Some(form) = tab.agent_form.as_mut() { + if on { + form.servers.remove(&id); + } else { + form.servers.insert(id.clone()); + } + } + cx.notify(); + })) + })) + } +} + +/// Cancel and Save under the form. +fn actions(cx: &mut Context) -> impl IntoElement { + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("server-agent-cancel") + .small() + .ghost() + .label("Cancel") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| { + tab.agent_form = None; + cx.notify(); + })), + ) + .child( + Button::new("server-agent-save") + .small() + .primary() + .label("Save") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| tab.save_agent(cx))), + ) +} diff --git a/crates/hubs-ui/src/servers/tabs/agents/providers.rs b/crates/hubs-ui/src/servers/tabs/agents/providers.rs new file mode 100644 index 00000000..8bd2c006 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agents/providers.rs @@ -0,0 +1,319 @@ +//! The model providers an agent may use: the hub's, listed, and the account's own, added, +//! changed, tested and removed. A key is typed once; the hub keeps it and never shows it. + +use gpui_kit::{ + AppContext as _, ClickEvent, Context, Entity, TestSupportExt as _, Window, + component::{ + Selectable as _, Sizable as _, StyledExt as _, + button::{Button, ButtonVariants as _}, + h_flex, + input::{Input, InputState}, + v_flex, + }, + div, + prelude::*, +}; +use nocterm_hub_agents_model::{Provider, ProviderDraft, ProviderId, ProviderKind, ProviderScope}; +use nocterm_session::Secret; + +use super::AgentsTab; +use crate::layout::{field, note}; +use crate::messages::agents_problem; +use crate::servers::item; + +/// What the person has typed for a provider of their own. +pub(super) struct ProviderForm { + editing: Option, + kind: ProviderKind, + name: Entity, + base_url: Entity, + models: Entity, + key: Entity, +} + +impl ProviderForm { + fn new(provider: Option<&Provider>, window: &mut Window, cx: &mut Context) -> Self { + let kind = provider.map_or(ProviderKind::default(), |provider| provider.kind); + let mut input = |placeholder: &'static str, text: String, masked: bool| { + cx.new(|cx| { + let mut input = InputState::new(window, cx) + .placeholder(placeholder) + .masked(masked); + input.set_value(text, window, cx); + input + }) + }; + Self { + editing: provider.map(|provider| provider.id.clone()), + kind, + name: input( + "Name", + provider.map(|p| p.name.clone()).unwrap_or_default(), + false, + ), + base_url: input( + "Base URL", + provider.map_or_else( + || kind.default_base_url().to_owned(), + |p| p.base_url.clone(), + ), + false, + ), + models: input( + "Models, separated by commas", + provider.map(|p| p.models.join(", ")).unwrap_or_default(), + false, + ), + key: input( + if provider.is_some() { + "API key (empty keeps the stored one)" + } else { + "API key" + }, + String::new(), + true, + ), + } + } + + fn draft(&self, cx: &gpui_kit::App) -> ProviderDraft { + let text = |input: &Entity| input.read(cx).value().to_string(); + let key = text(&self.key); + ProviderDraft { + kind: self.kind, + name: text(&self.name), + base_url: text(&self.base_url), + models: text(&self.models) + .split(',') + .map(|model| model.trim().to_owned()) + .collect(), + api_key: (!key.trim().is_empty()).then(|| Secret::new(key)), + } + } +} + +impl AgentsTab { + fn open_provider( + &mut self, + provider: Option<&Provider>, + window: &mut Window, + cx: &mut Context, + ) { + self.provider_form = Some(ProviderForm::new(provider, window, cx)); + self.agent_form = None; + cx.notify(); + } + + fn save_provider(&mut self, cx: &mut Context) { + let (Some(hub), Some(form)) = (self.hub.clone(), &self.provider_form) else { + return; + }; + let draft = form.draft(cx); + let directory = self.service.read(cx).directory().clone(); + let call: nocterm_hub_agents_model::AgentsFuture<()> = match form.editing.clone() { + Some(provider) => { + let saving = directory.update_provider(&hub, &provider, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + None => { + let saving = directory.create_provider(&hub, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + }; + self.write(call, cx); + } + + fn delete_provider(&mut self, provider: &ProviderId, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let deleting = self + .service + .read(cx) + .directory() + .delete_provider(&hub, provider); + self.write(deleting, cx); + } + + /// Calls the provider from an agent worker and says what came of it. + fn test_provider(&mut self, provider: &ProviderId, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let testing = self + .service + .read(cx) + .directory() + .test_provider(&hub, provider); + self.working = Some(cx.spawn(async move |this, cx| { + let result = testing.await; + this.update(cx, |tab, cx| { + tab.problem = Some(match result { + Ok(test) if test.ok => format!( + "The provider answers. Models: {}", + if test.models.is_empty() { + "none listed".to_owned() + } else { + test.models.join(", ") + } + ), + Ok(test) => format!("The provider did not answer: {}", test.message), + Err(error) => agents_problem(&error), + }); + cx.notify(); + }) + .ok(); + })); + } +} + +fn row(provider: &Provider, cx: &mut Context) -> gpui_kit::AnyElement { + let scope = match provider.scope { + ProviderScope::Hub => "the hub's", + ProviderScope::Account => "your own", + }; + let id = provider.id.clone(); + let mut actions = h_flex().gap_1().child( + Button::new(item("server-provider-test", provider.id.as_str())) + .small() + .ghost() + .label("Test") + .on_click(cx.listener({ + let id = id.clone(); + move |tab, _: &ClickEvent, _, cx| tab.test_provider(&id, cx) + })), + ); + if provider.editable { + let edit = provider.clone(); + actions = actions + .child( + Button::new(item("server-provider-edit", provider.id.as_str())) + .small() + .ghost() + .label("Edit") + .on_click(cx.listener(move |tab, _: &ClickEvent, window, cx| { + tab.open_provider(Some(&edit), window, cx); + })), + ) + .child( + Button::new(item("server-provider-delete", provider.id.as_str())) + .small() + .ghost() + .label("Delete") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + tab.delete_provider(&id, cx); + })), + ); + } + h_flex() + .id(item("server-provider", provider.id.as_str())) + .test_support() + .gap_2() + .child( + v_flex() + .flex_1() + .min_w_0() + .child(div().text_sm().font_semibold().child(provider.name.clone())) + .child(note( + format!( + "{scope} · {} · {}", + provider.base_url, + provider.models.join(", ") + ), + cx, + )), + ) + .child(actions) + .into_any_element() +} + +fn form(form: &ProviderForm, cx: &mut Context) -> gpui_kit::AnyElement { + let kinds = h_flex().gap_1().children( + [ + (ProviderKind::OpenAi, "OpenAI-compatible"), + (ProviderKind::Anthropic, "Anthropic"), + ] + .map(|(kind, label)| { + Button::new(gpui_kit::ElementId::Name( + format!("server-provider-kind-{label}").into(), + )) + .small() + .ghost() + .label(label) + .selected(form.kind == kind) + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + if let Some(form) = tab.provider_form.as_mut() { + form.kind = kind; + } + cx.notify(); + })) + }), + ); + v_flex() + .id("server-provider-form") + .test_support() + .gap_2() + .child(field("Kind", kinds)) + .child(field("Name", Input::new(&form.name))) + .child(field("Base URL", Input::new(&form.base_url))) + .child(field("Models", Input::new(&form.models))) + .child(field("API key", Input::new(&form.key))) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("server-provider-cancel") + .small() + .ghost() + .label("Cancel") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| { + tab.provider_form = None; + cx.notify(); + })), + ) + .child( + Button::new("server-provider-save") + .small() + .primary() + .label("Save") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| tab.save_provider(cx))), + ), + ) + .into_any_element() +} + +/// The providers section: the providers, the form open on one, and adding one. +pub(super) fn render( + providers: &[Provider], + open: Option<&ProviderForm>, + cx: &mut Context, +) -> gpui_kit::AnyElement { + let rows: Vec<_> = providers.iter().map(|provider| row(provider, cx)).collect(); + v_flex() + .id("server-providers") + .test_support() + .gap_1() + .child( + h_flex() + .justify_between() + .child(div().font_semibold().child("Model providers")) + .child( + Button::new("server-provider-new") + .small() + .label("Add your own") + .on_click(cx.listener(|tab, _: &ClickEvent, window, cx| { + tab.open_provider(None, window, cx); + })), + ), + ) + .when(rows.is_empty(), |list| { + list.child(note( + "No providers yet. Add your own, or ask the hub's administrator for one.", + cx, + )) + }) + .children(rows) + .children(open.map(|open| form(open, cx))) + .into_any_element() +} diff --git a/crates/hubs-ui/src/servers/tabs/devices.rs b/crates/hubs-ui/src/servers/tabs/devices.rs new file mode 100644 index 00000000..6123fa6b --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/devices.rs @@ -0,0 +1,343 @@ +//! The Devices tab of a server: the devices signed in to the account, and revoking them. +//! +//! The list is read from the hub through the service whenever the tab is shown for a server, +//! and again after a device was revoked. Revoking this device is signing out of the server. + +use gpui_kit::{ + App, ClickEvent, Context, Entity, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Sizable as _, StyledExt as _, WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + h_flex, v_flex, + }, + div, + prelude::*, +}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::{DeviceSummary, HubError, HubId}; + +use crate::layout::{note, problem}; +use crate::manage; +use crate::messages::hub_problem; +use crate::servers::item; + +/// What the list of devices shows. +enum Load { + Loading, + Ready(Vec), + Failed(HubError), +} + +/// The devices of the account on the server that is shown. +pub(in crate::servers) struct Devices { + hubs: Entity, + hub: Option, + load: Load, + loading: Option>, +} + +impl Devices { + pub(in crate::servers) fn new(hubs: Entity) -> Self { + Self { + hubs, + hub: None, + load: Load::Loading, + loading: None, + } + } + + /// Shows the devices of `hub`, reading them when another server was shown before. Showing + /// the same server again changes nothing. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() == Some(hub) { + return; + } + self.hub = Some(hub.clone()); + self.reload(cx); + } + + /// Forgets what was read: the next [`Devices::show`] reads again. + pub(in crate::servers) fn hide(&mut self) { + self.hub = None; + self.loading = None; + } + + fn reload(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + self.load = Load::Loading; + let listing = self.hubs.read(cx).list_devices(&hub); + self.loading = Some(cx.spawn(async move |this, cx| { + let result = listing.await; + this.update(cx, |devices, cx| { + if devices.hub.as_ref() != Some(&hub) { + return; + } + devices.load = match result { + Ok(listed) => Load::Ready(sorted(listed)), + Err(error) => Load::Failed(error), + }; + cx.notify(); + }) + .ok(); + })); + cx.notify(); + } + + /// Revokes `device`; this device signs out instead. A failure is reported, and the list is + /// read again either way. + fn revoke(&mut self, device: &DeviceSummary, window: &mut Window, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + if device.is_current { + manage::sign_out(&self.hubs, &hub, window, cx); + return; + } + let revoking = self.hubs.read(cx).revoke_device(&hub, &device.id); + let view = cx.weak_entity(); + window + .spawn(cx, async move |cx| { + let result = revoking.await; + cx.update(|window, cx| { + if let Err(error) = &result { + nocterm_ui::notice::error( + window, + cx, + "hub-devices", + "Could not revoke the device", + hub_problem(error), + ); + } + let _ = view.update(cx, |devices, cx| devices.reload(cx)); + }) + .ok(); + }) + .detach(); + } + + /// Asks before revoking: the device is signed out and has to sign in again. + fn confirm_revoke( + view: Entity, + device: DeviceSummary, + window: &mut Window, + cx: &mut App, + ) { + window.open_alert_dialog(cx, move |alert, _, _| { + let (view, chosen) = (view.clone(), device.clone()); + let confirm = Button::new("revoke-device-confirm") + .danger() + .label("Revoke") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + view.update(cx, |devices, cx| devices.revoke(&chosen, window, cx)); + }); + let keep = Button::new("revoke-device-keep") + .primary() + .label("Keep it") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + }); + let consequence = if device.is_current { + "This is this device: revoking it signs you out of the server." + } else { + "It is signed out of the account and has to sign in again." + }; + alert + .title(format!("Revoke {}?", device.name)) + .description(consequence) + .on_ok(|_, _, _| true) + .footer(DialogFooter::new().child(confirm).child(keep)) + }); + } +} + +/// This device first, then the others by when they were last seen, newest first. +fn sorted(mut devices: Vec) -> Vec { + devices.sort_by_key(|device| { + ( + !device.is_current, + std::cmp::Reverse(device.last_seen_at_ms), + ) + }); + devices +} + +impl Render for Devices { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let view = cx.entity(); + let body = match &self.load { + Load::Loading => div() + .id("devices-loading") + .test_support() + .child(note("Reading the devices of your account…", cx)) + .into_any_element(), + Load::Failed(error) => self.failed(error, cx), + Load::Ready(devices) if devices.is_empty() => div() + .id("devices-empty") + .test_support() + .child(note("The server lists no devices.", cx)) + .into_any_element(), + Load::Ready(devices) => v_flex() + .gap_2() + .children(devices.iter().map(|device| row(device, &view, cx))) + .into_any_element(), + }; + v_flex() + .id("devices-tab") + .test_support() + .gap_3() + .child(note( + "The devices signed in to your account on this server. Revoking one signs it out.", + cx, + )) + .child(body) + } +} + +impl Devices { + fn failed(&self, error: &HubError, cx: &mut Context) -> gpui_kit::AnyElement { + let view = cx.entity(); + v_flex() + .id("devices-error") + .test_support() + .gap_2() + .child(problem(hub_problem(error), cx)) + .child( + h_flex().child(Button::new("devices-retry").label("Try again").on_click( + move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + view.update(cx, |devices, cx| devices.reload(cx)); + }, + )), + ) + .into_any_element() + } +} + +/// One device: its name, kind and last sign-in, and the button that revokes it. +fn row(device: &DeviceSummary, view: &Entity, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + let (view, chosen) = (view.clone(), device.clone()); + let seen = if device.last_seen_at_ms > 0 { + format!("Last seen {}", utc(device.last_seen_at_ms)) + } else { + "Never seen".to_owned() + }; + h_flex() + .id(item("device", &device.id)) + .test_support() + .gap_3() + .p_3() + .items_center() + .rounded(theme.radius) + .border_1() + .border_color(theme.border) + .child( + v_flex() + .flex_1() + .min_w_0() + .gap_1() + .child( + h_flex() + .gap_2() + .items_center() + .child(div().font_semibold().truncate().child(device.name.clone())) + .when(device.is_current, |line| { + line.child( + div() + .id(item("device-current", &device.id)) + .test_support() + .text_xs() + .text_color(theme.success) + .child("This device"), + ) + }), + ) + .child( + div() + .id(item("device-seen", &device.id)) + .test_support() + .text_xs() + .text_color(theme.muted_foreground) + .child(format!("{} · {seen}", kind(&device.platform))), + ), + ) + .child( + Button::new(item("device-revoke", &device.id)) + .danger() + .small() + .label("Revoke") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + Devices::confirm_revoke(view.clone(), chosen.clone(), window, cx); + }), + ) +} + +/// The kind of client as a person reads it; an empty report is an unknown kind. +fn kind(platform: &str) -> String { + let mut chars = platform.chars(); + match chars.next() { + Some(first) => first.to_uppercase().chain(chars).collect(), + None => "Unknown kind".to_owned(), + } +} + +/// A moment as `2023-11-14 22:13 UTC`, so a list reads the same wherever it is shown. +pub(in crate::servers) fn utc(ms: i64) -> String { + let seconds = ms.div_euclid(1000); + let (days, rest) = (seconds.div_euclid(86_400), seconds.rem_euclid(86_400)); + let z = days + 719_468; + let era = z.div_euclid(146_097); + let day_of_era = z.rem_euclid(146_097); + let year_of_era = + (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365; + let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); + let shifted_month = (5 * day_of_year + 2) / 153; + let day = day_of_year - (153 * shifted_month + 2) / 5 + 1; + let month = if shifted_month < 10 { + shifted_month + 3 + } else { + shifted_month - 9 + }; + let year = year_of_era + era * 400 + i64::from(month <= 2); + format!( + "{year:04}-{month:02}-{day:02} {:02}:{:02} UTC", + rest / 3_600, + rest % 3_600 / 60 + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_moment_reads_as_a_utc_date_and_time() { + assert_eq!(utc(0), "1970-01-01 00:00 UTC"); + assert_eq!(utc(1_700_000_000_000), "2023-11-14 22:13 UTC"); + assert_eq!(utc(951_782_400_000), "2000-02-29 00:00 UTC"); + } + + #[test] + fn this_device_is_listed_first_and_the_others_by_recency() { + let device = |id: &str, seen, current| DeviceSummary { + id: id.into(), + name: id.into(), + platform: "desktop".into(), + created_at_ms: 0, + last_seen_at_ms: seen, + is_current: current, + }; + let order: Vec<_> = sorted(vec![ + device("old", 1, false), + device("new", 9, false), + device("here", 0, true), + ]) + .into_iter() + .map(|device| device.id) + .collect(); + assert_eq!(order, ["here", "new", "old"]); + } +} diff --git a/crates/hubs-ui/src/servers/tabs/names.rs b/crates/hubs-ui/src/servers/tabs/names.rs new file mode 100644 index 00000000..5635729f --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/names.rs @@ -0,0 +1,104 @@ +//! The name fields of the known servers. + +use std::collections::HashMap; + +use gpui_kit::{App, Context, Entity, Subscription, Window}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::{HubId, HubsSettings}; +use nocterm_ui::SettingsTextField; + +/// The name field of one server: the shared text field of the settings, which keeps what the +/// user typed until its save is settled, saves a distinct text once and shows the stored name +/// again when the settings change elsewhere. +struct NameField { + field: SettingsTextField, + _observed: Subscription, +} + +/// The name fields of the known servers, one each. +pub(in crate::servers) struct NameFields { + hubs: Entity, + fields: HashMap, +} + +impl NameFields { + pub(in crate::servers) fn new(hubs: Entity) -> Self { + Self { + hubs, + fields: HashMap::new(), + } + } + + /// The field of `hub`, once [`NameFields::sync`] made it. + pub(in crate::servers) fn get(&self, hub: &HubId) -> Option { + self.fields.get(hub).map(|name| name.field.clone()) + } + + /// Saves what is typed in every field that has unsaved text. + pub(in crate::servers) fn flush(&self, window: &mut Window, cx: &mut App) { + for name in self.fields.values() { + name.field.flush(window, cx); + } + } + + /// Gives each known server a name field and drops the fields of forgotten ones. A field + /// that exists keeps what the user is typing. The context owner is redrawn when a field changes. + pub(in crate::servers) fn sync( + &mut self, + window: &mut Window, + cx: &mut Context, + ) { + let known: Vec = self + .hubs + .read(cx) + .list() + .into_iter() + .map(|s| s.id) + .collect(); + self.fields.retain(|hub, _| known.contains(hub)); + for hub in known { + if self.fields.contains_key(&hub) { + continue; + } + let Some(address) = self + .hubs + .read(cx) + .record(&hub) + .map(|record| record.address.to_string()) + else { + continue; + }; + let (reading, writing) = (hub.clone(), hub.clone()); + let field = SettingsTextField::new( + self.hubs.read(cx).settings(), + format!("server-name-input-{address}"), + move |document| { + document + .get::() + .find(&reading) + .map(|record| record.label.clone()) + .unwrap_or_default() + }, + move |document, text| { + document.update::(|settings| { + settings.set_label(&writing, text); + }); + Ok(()) + }, + window, + cx, + ); + let page = cx.weak_entity(); + let observed = field.observe(cx, move |cx| { + let _ = page.update(cx, |_, cx| cx.notify()); + }); + self.fields.insert( + hub, + NameField { + field, + _observed: observed, + }, + ); + } + } +} diff --git a/crates/hubs-ui/src/servers/tabs/overview.rs b/crates/hubs-ui/src/servers/tabs/overview.rs new file mode 100644 index 00000000..fc62a8f7 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/overview.rs @@ -0,0 +1,200 @@ +//! The Overview tab of a server on the Servers page. + +use gpui_kit::{ + App, ClickEvent, Entity, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Icon, Selectable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::{HubStatus, HubSummary, Hubs}; +use nocterm_hubs_model::{HubColor, HubRecord, TrustPolicy}; +use nocterm_ui::{IconName, SettingsTextField}; + +use crate::color::tint; +use crate::commands::can_sign_out; +use crate::layout::{field, fingerprint_block, note, problem}; +use crate::manage; +use crate::servers::item; +use crate::status::status_chip; + +/// Everything about the server itself: status, address, name, colour, certificate and what can +/// be done to it. +pub(in crate::servers) fn overview( + summary: &HubSummary, + record: &HubRecord, + name: &SettingsTextField, + hubs: &Entity, + cx: &App, +) -> gpui_kit::AnyElement { + let key = summary.address.to_string(); + let (status, status_color) = status_chip(&summary.status, cx); + v_flex() + .id(item("server-overview", &key)) + .test_support() + .gap_3() + .child( + h_flex() + .gap_2() + .items_center() + .child( + div() + .id(item("server-status", &key)) + .test_support() + .text_xs() + .text_color(status_color) + .child(status), + ) + .children(retry(summary, hubs, &key)), + ) + .child(note(summary.address.to_string(), cx)) + .child(field( + "Name", + div() + .id(item("server-name", &key)) + .test_support() + .child(name.input()), + )) + .children(name.error(cx).map(|error| problem(error.to_string(), cx))) + .child(colors(summary, hubs, &key, cx)) + .child(certificate(summary, record, &key, cx)) + .child(actions(summary, hubs, &key)) + .into_any_element() +} + +/// The button that connects at once while the server waits for the next attempt. +fn retry(summary: &HubSummary, hubs: &Entity, key: &str) -> Option { + if !matches!(summary.status, HubStatus::Offline { .. }) { + return None; + } + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Some( + Button::new(item("server-retry", key)) + .xsmall() + .label("Try again") + .on_click(move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + hubs.update(cx, |hubs, cx| hubs.retry(&id, cx)); + }), + ) +} + +/// The chooser of the colour that marks the server. +fn colors(summary: &HubSummary, hubs: &Entity, key: &str, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + let (border, foreground) = (theme.border, theme.foreground); + let none = { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Button::new(item("server-color-none", key)) + .ghost() + .xsmall() + .label("None") + .selected(summary.color.is_none()) + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + manage::recolor(&hubs, &id, None, window, cx); + }) + }; + let swatches = HubColor::ALL.into_iter().map(|color| { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + let chosen = summary.color == Some(color); + div() + .id(item(&format!("server-color-{}", color.name()), key)) + .test_support() + .size_5() + .rounded_full() + .flex() + .items_center() + .justify_center() + .cursor_pointer() + .bg(tint(color, cx)) + .border_2() + .border_color(if chosen { foreground } else { border }) + .when(chosen, |swatch| { + swatch.child(Icon::new(IconName::Check).xsmall().text_color(foreground)) + }) + .on_click(move |_, window, cx| { + manage::recolor(&hubs, &id, Some(color), window, cx); + }) + }); + field( + "Colour", + h_flex() + .gap_2() + .items_center() + .child(none) + .children(swatches), + ) +} + +/// How the server's certificate is trusted. It is only shown: a certificate that changed is +/// never accepted here, and removing the server is the way to sign in to the new one. +fn certificate(summary: &HubSummary, record: &HubRecord, key: &str, cx: &App) -> impl IntoElement { + let mut section = v_flex() + .id(item("server-trust", key)) + .test_support() + .gap_2(); + match record.trust { + TrustPolicy::Pinned(fingerprint) => { + section = section.child(div().id(item("server-pinned", key)).test_support().child( + fingerprint_block("Pinned certificate (SHA-256)", &fingerprint, cx), + )); + } + TrustPolicy::PublicCa => { + section = section.child( + div() + .id(item("server-public-ca", key)) + .test_support() + .child(note( + "Trusted through a public certificate authority. No certificate is pinned.", + cx, + )), + ); + } + } + if let HubStatus::TrustFailed { presented, .. } = &summary.status { + section = section + .child(problem( + "The server presents another certificate than the one trusted. Nocterm will not connect. Remove the server and sign in again only if you can confirm the new fingerprint with whoever runs it." + .to_owned(), + cx, + )) + .child( + div() + .id(item("server-presented", key)) + .test_support() + .child(fingerprint_block("Presented now", presented, cx)), + ); + } + section +} + +/// Sign out and remove. +fn actions(summary: &HubSummary, hubs: &Entity, key: &str) -> impl IntoElement { + let signed_in = can_sign_out(summary); + let sign_out = signed_in.then(|| { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Button::new(item("server-sign-out", key)) + .label("Sign out") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + manage::sign_out(&hubs, &id, window, cx); + }) + }); + let remove = { + let (hubs, summary) = (hubs.clone(), summary.clone()); + Button::new(item("server-remove", key)) + .danger() + .label("Remove…") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + manage::confirm_remove(hubs.clone(), summary.clone(), window, cx); + }) + }; + h_flex() + .gap_2() + .justify_end() + .mt(px(2.)) + .children(sign_out) + .child(remove) +} diff --git a/crates/hubs-ui/src/servers/tabs/trust.rs b/crates/hubs-ui/src/servers/tabs/trust.rs new file mode 100644 index 00000000..cab5f818 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/trust.rs @@ -0,0 +1,70 @@ +//! The Keys tab of a server: the account keys, opened and closed on this device, and a +//! recovery key that was never confirmed as saved. + +use gpui_kit::{ + App, ClickEvent, Entity, TestSupportExt as _, Window, + component::{button::Button, h_flex, v_flex}, + prelude::*, +}; +use nocterm_hubs::{HubSummary, Hubs}; + +use super::account_keys::AccountKeys; +use crate::layout::{field, note}; +use crate::servers::item; + +/// The account keys of the server and, while one was never saved, the offer of a new recovery +/// key. +pub(in crate::servers) fn trust( + summary: &HubSummary, + hubs: &Entity, + keys: &Entity, + cx: &App, +) -> gpui_kit::AnyElement { + let key = summary.address.to_string(); + let recovery = recovery_key(summary, hubs, &key, cx); + v_flex() + .id(item("server-trust-tab", &key)) + .test_support() + .gap_3() + .child(field("Vault keys", keys.clone())) + .children(recovery) + .into_any_element() +} + +/// The offer of a new recovery key, while the one made with the account keys on this device was +/// never confirmed as saved and the keys are open. +fn recovery_key( + summary: &HubSummary, + hubs: &Entity, + key: &str, + cx: &App, +) -> Option { + if !hubs.read(cx).recovery_key_unconfirmed(&summary.id) { + return None; + } + let offer = hubs.read(cx).recovery_key_offer(&summary.id).ok()?; + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Some( + field( + "Recovery key", + v_flex() + .id(item("server-recovery", key)) + .test_support() + .gap_2() + .child(note( + "The recovery key made with your account keys was never confirmed as saved. Issue a new one so you can still open them if you lose your password.", + cx, + )) + .child( + h_flex().justify_end().child( + Button::new(item("server-recovery-issue", key)) + .label("Issue a new recovery key") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + crate::sign_in::start_reissue(&hubs, &id, offer.clone(), window, cx); + }), + ), + ), + ) + .into_any_element(), + ) +} diff --git a/crates/hubs-ui/src/servers_agents_tests.rs b/crates/hubs-ui/src/servers_agents_tests.rs new file mode 100644 index 00000000..933eb2ba --- /dev/null +++ b/crates/hubs-ui/src/servers_agents_tests.rs @@ -0,0 +1,123 @@ +//! The Agents and Agent access tabs of a server read and write the account's agents through the +//! agents service. +use super::{ServersPage, item}; +use crate::test_support::{Fixture, fixture_over, hub_id, pinned_record, server}; +use gpui_kit::{AppContext as _, TestAppContext, WindowOptions, test::TestWindowExt as _}; +use nocterm_hub_agents_model::{HubServers as _, NewServer}; +use nocterm_hubs_model::Environment; +use nocterm_hubs_model::testing::FakeHub; +use nocterm_settings::SettingsDocument; +use nocterm_store::SettingsConfig; + +const AGENTS: usize = 3; +const ACCESS: usize = 4; + +fn page(cx: &mut TestAppContext) -> Fixture { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let setup = fixture_over( + cx, + config, + Environment::Local, + FakeHub::new().with_server(server()), + vec![pinned_record()], + Some("ada"), + ); + let hubs = setup.installed.hubs().clone(); + let agents = setup.installed.hub_agents().cloned(); + let (window, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |_, cx| { + cx.new(|cx| ServersPage::new(hubs, agents, cx)) + }) + .unwrap() + }); + let page = Fixture { window, ..setup }; + page.render(cx); + page +} + +fn open_tab(page: &Fixture, index: usize, cx: &mut TestAppContext) { + page.render(cx); + cx.update_window(page.window, |_, window, cx| { + window.within("servers-tabs").click(index, cx); + }) + .unwrap(); + cx.run_until_parked(); + page.render(cx); + page.render(cx); +} + +#[gpui_kit::test] +fn the_agents_tab_lists_the_accounts_agents_and_providers(cx: &mut TestAppContext) { + let page = page(cx); + let _ = page + .agents + .clone() + .with_provider(&hub_id(), "Models", &["m1"]) + .with_agent(&hub_id(), "Ops", "m1"); + open_tab(&page, AGENTS, cx); + assert!(page.shows(item("server-agents-tab", &hub_id().to_string()), cx)); + assert!(page.shows(item("server-agent", "agent-2"), cx)); + assert!(page.shows(item("server-provider", "provider-1"), cx)); +} + +#[gpui_kit::test] +fn deleting_an_agent_removes_it_on_the_hub_and_from_the_list(cx: &mut TestAppContext) { + let page = page(cx); + let _ = page + .agents + .clone() + .with_provider(&hub_id(), "Models", &["m1"]) + .with_agent(&hub_id(), "Ops", "m1"); + open_tab(&page, AGENTS, cx); + page.click(item("server-agent-delete", "agent-2"), cx); + cx.run_until_parked(); + page.render(cx); + assert!(page.agents.calls().contains(&"delete_agent".to_owned())); + assert!(!page.shows(item("server-agent", "agent-2"), cx)); +} + +#[gpui_kit::test] +fn the_new_agent_form_opens_and_closes(cx: &mut TestAppContext) { + let page = page(cx); + open_tab(&page, AGENTS, cx); + page.click(item("server-agent-new", &hub_id().to_string()), cx); + assert!(page.shows("server-agent-form", cx)); + page.click("server-agent-cancel", cx); + assert!(!page.shows("server-agent-form", cx)); +} + +#[gpui_kit::test] +fn the_agent_access_tab_shows_the_key_and_disconnects_a_server(cx: &mut TestAppContext) { + let page = page(cx); + let added = futures::executor::block_on(page.agents.add_server( + &hub_id(), + NewServer { + name: "prod".into(), + host: "prod.example".into(), + port: 22, + user: "root".into(), + host_key: "ssh-ed25519 AAAAhost".into(), + }, + )) + .unwrap(); + open_tab(&page, ACCESS, cx); + assert!(page.shows(item("agent-key", &hub_id().to_string()), cx)); + assert!(page.shows(item("agent-server", added.id.as_str()), cx)); + page.click(item("agent-server-disconnect", added.id.as_str()), cx); + cx.run_until_parked(); + page.render(cx); + assert!(!page.shows(item("agent-server", added.id.as_str()), cx)); +} + +#[gpui_kit::test] +fn replacing_the_agent_key_asks_first(cx: &mut TestAppContext) { + let page = page(cx); + open_tab(&page, ACCESS, cx); + page.click(item("agent-key-rotate", &hub_id().to_string()), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx)); + assert!(!page.agents.calls().contains(&"rotate_agent_key".to_owned())); + page.click("agent-key-rotate-confirm", cx); + cx.run_until_parked(); + assert!(page.agents.calls().contains(&"rotate_agent_key".to_owned())); +} diff --git a/crates/hubs-ui/src/servers_tests.rs b/crates/hubs-ui/src/servers_tests.rs new file mode 100644 index 00000000..660e09bd --- /dev/null +++ b/crates/hubs-ui/src/servers_tests.rs @@ -0,0 +1,595 @@ +//! The Servers page lists the known servers and manages them through the service. +use std::cell::Cell; +use std::rc::Rc; +use std::sync::Arc; + +use super::{ServersPage, item, settings_page}; +use crate::test_support::{Fixture, fixture_over, hub_id, pinned_record, server}; +use gpui_kit::{AppContext as _, TestAppContext, WindowOptions, test::TestWindowExt as _}; +use nocterm_hubs::{HubStatus, HubSummary}; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp, FakeServer}; +use nocterm_hubs_model::{ + DeviceSummary, Environment, Fingerprint, HubColor, HubError, HubRecord, TrustPolicy, +}; +use nocterm_settings::SettingsDocument; +use nocterm_store::{SettingsConfig, SnapshotWriter, StoreError}; + +const KEY: &str = "hub.home:7443"; +const OTHER: &str = "hub.work:7443"; + +const OVERVIEW: usize = 0; +const DEVICES: usize = 1; +const KEYS: usize = 2; + +/// Opens the tab `index` of the selected server. +fn open_tab(page: &Fixture, index: usize, cx: &mut TestAppContext) { + page.render(cx); + cx.update_window(page.window, |_, window, cx| { + window.within("servers-tabs").click(index, cx); + }) + .unwrap(); + cx.run_until_parked(); + page.render(cx); +} + +/// The page in a window of its own, over a hub that `signed_in` names a user of. +fn page_over( + cx: &mut TestAppContext, + config: SettingsConfig, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + page_on( + cx, + config, + FakeHub::new().with_server(server()), + known, + signed_in, + ) +} + +fn page_on( + cx: &mut TestAppContext, + config: SettingsConfig, + hub: FakeHub, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + let setup = fixture_over(cx, config, Environment::Local, hub, known, signed_in); + let hubs = setup.installed.hubs().clone(); + let (window, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |_, cx| { + cx.new(|cx| ServersPage::new(hubs, None, cx)) + }) + .unwrap() + }); + let page = Fixture { window, ..setup }; + page.render(cx); + page +} + +fn page(cx: &mut TestAppContext, known: Vec) -> Fixture { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + page_over(cx, config, known, Some("ada")) +} + +fn servers(page: &Fixture, cx: &mut TestAppContext) -> Vec { + cx.update(|cx| page.installed.hubs().read(cx).list()) +} + +/// How many times the settings were published. +fn revision(page: &Fixture, cx: &mut TestAppContext) -> u64 { + cx.update(|cx| page.installed.hubs().read(cx).settings().revision(cx)) +} + +fn failing_config() -> SettingsConfig { + let writer: SnapshotWriter = Arc::new(|_| { + Box::pin(async { + Err(StoreError::backend(std::io::Error::from( + std::io::ErrorKind::PermissionDenied, + ))) + }) + }); + SettingsConfig::with_writer(SettingsDocument::default(), writer) +} + +fn notices(page: &Fixture, cx: &mut TestAppContext) -> usize { + cx.update_window(page.window, |_, window, cx| { + nocterm_ui::notice::count(window, cx) + }) + .unwrap() +} + +#[gpui_kit::test] +fn without_servers_the_page_invites_signing_in(cx: &mut TestAppContext) { + let page = page(cx, Vec::new()); + assert!(page.shows("servers-empty", cx)); + assert!(page.shows("servers-add", cx)); +} + +#[gpui_kit::test] +fn the_page_is_the_servers_page_of_the_settings(cx: &mut TestAppContext) { + let page = page(cx, Vec::new()); + let spec = settings_page(&page.installed); + assert_eq!(spec.id, "servers"); + assert_eq!(spec.title.as_ref(), "Servers"); +} + +#[gpui_kit::test] +fn a_pinned_server_shows_its_fingerprint(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + assert!(page.shows(item("server", KEY), cx)); + assert!(page.shows(item("server-pinned", KEY), cx)); + assert!(!page.shows(item("server-public-ca", KEY), cx)); + assert!(!page.shows(item("server-presented", KEY), cx)); +} + +#[gpui_kit::test] +fn a_server_trusted_through_a_public_authority_says_nothing_is_pinned(cx: &mut TestAppContext) { + let record = HubRecord::new(pinned_record().address, TrustPolicy::PublicCa, "Home"); + let page = page(cx, vec![record]); + assert!(page.shows(item("server-public-ca", KEY), cx)); + assert!(!page.shows(item("server-pinned", KEY), cx)); +} + +#[gpui_kit::test] +fn a_changed_certificate_is_shown_and_removing_is_the_only_way_out(cx: &mut TestAppContext) { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let page = page(cx, vec![changed.clone()]); + assert!(matches!( + servers(&page, cx)[0].status, + HubStatus::TrustFailed { .. } + )); + assert!(page.shows(item("server-pinned", KEY), cx)); + assert!(page.shows(item("server-presented", KEY), cx)); + assert!(page.shows(item("server-remove", KEY), cx)); + assert!(!page.shows(item("server-sign-out", KEY), cx)); + assert!(!page.shows("sign-in-trust", cx), "nothing to accept"); + assert_eq!( + cx.update(|cx| page.installed.hubs().read(cx).record(&hub_id()).cloned()), + Some(changed), + "the record is as it was" + ); +} + +#[gpui_kit::test] +fn a_name_typed_in_the_row_is_saved_with_enter(cx: &mut TestAppContext) { + let page = page(cx, vec![server().pinned_record("")]); + page.click(item("server-name", KEY), cx); + page.type_text("Office", cx); + page.press("enter", cx); + cx.run_until_parked(); + let label = cx.update(|cx| page.installed.label(&hub_id(), cx)); + assert_eq!(label.as_deref(), Some("Office")); +} + +#[gpui_kit::test] +fn a_name_that_was_not_edited_never_overwrites_a_newer_one(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.render(cx); + // The name changes elsewhere, for instance in another window. + page.installed + .hubs() + .update(cx, |hubs, cx| hubs.set_label(&hub_id(), "Elsewhere", cx)) + .detach(); + cx.run_until_parked(); + + page.click(item("server-name", KEY), cx); + page.press("tab", cx); + cx.run_until_parked(); + + let label = cx.update(|cx| page.installed.label(&hub_id(), cx)); + assert_eq!(label.as_deref(), Some("Elsewhere")); +} + +#[gpui_kit::test] +fn a_name_is_saved_once_when_enter_is_followed_by_leaving_the_field(cx: &mut TestAppContext) { + let page = page(cx, vec![server().pinned_record("")]); + page.click(item("server-name", KEY), cx); + page.type_text("Office", cx); + let before = revision(&page, cx); + page.press("enter", cx); + page.press("tab", cx); + cx.run_until_parked(); + + assert_eq!(revision(&page, cx), before + 1); +} + +#[gpui_kit::test] +fn a_cleared_name_shows_the_address_again(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-name", KEY), cx); + page.press("ctrl-a", cx); + page.press("backspace", cx); + page.press("enter", cx); + cx.run_until_parked(); + let label = cx.update(|cx| page.installed.label(&hub_id(), cx)); + assert_eq!(label.as_deref(), Some(KEY)); +} + +#[gpui_kit::test] +fn choosing_a_colour_marks_the_server_and_none_clears_the_mark(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-color-blue", KEY), cx); + cx.run_until_parked(); + assert_eq!(servers(&page, cx)[0].color, Some(HubColor::Blue)); + page.click(item("server-color-none", KEY), cx); + cx.run_until_parked(); + assert_eq!(servers(&page, cx)[0].color, None); +} + +#[gpui_kit::test] +fn signing_out_keeps_the_server_listed_and_offers_no_second_sign_out(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + assert!(page.shows(item("server-sign-out", KEY), cx)); + page.click(item("server-sign-out", KEY), cx); + cx.run_until_parked(); + page.render(cx); + let listed = servers(&page, cx); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0].status, HubStatus::SignedOut); + assert!(!page.hub.is_signed_in(&hub_id())); + assert!(!page.shows(item("server-sign-out", KEY), cx)); + assert!(page.shows(item("server-remove", KEY), cx)); +} + +#[gpui_kit::test] +fn a_server_that_is_not_signed_in_has_no_sign_out(cx: &mut TestAppContext) { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_over(cx, config, vec![pinned_record()], None); + assert!(!page.shows(item("server-sign-out", KEY), cx)); +} + +#[gpui_kit::test] +fn keeping_a_server_at_the_confirmation_changes_nothing(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-remove", KEY), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx), "removal asks first"); + assert!(servers(&page, cx).len() == 1); + page.click("remove-server-keep", cx); + cx.run_until_parked(); + assert_eq!(servers(&page, cx).len(), 1); + assert!(page.hub.is_signed_in(&hub_id())); +} + +#[gpui_kit::test] +fn confirming_removes_the_server_and_erases_its_credentials(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-remove", KEY), cx); + page.settle_dialog(cx); + page.click("remove-server-confirm", cx); + cx.run_until_parked(); + page.render(cx); + assert!(servers(&page, cx).is_empty()); + assert!(!page.hub.is_signed_in(&hub_id())); + assert!(page.hub.calls().contains(&FakeCall::Forget(hub_id()))); + assert!(!page.shows(item("server", KEY), cx)); + assert!(page.shows("servers-empty", cx)); +} + +#[gpui_kit::test] +fn a_change_that_cannot_be_saved_is_reported(cx: &mut TestAppContext) { + let page = page_over(cx, failing_config(), vec![pinned_record()], Some("ada")); + page.click(item("server-color-red", KEY), cx); + cx.run_until_parked(); + page.render(cx); + assert_eq!(notices(&page, cx), 1); + assert_eq!(servers(&page, cx)[0].color, None, "the colour was not kept"); +} + +#[gpui_kit::test] +fn a_server_whose_recovery_key_was_never_confirmed_offers_a_new_one(cx: &mut TestAppContext) { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "secret", "recovery-ada")); + hub.leave_recovery_key_unconfirmed(&hub_id()); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub, vec![pinned_record()], Some("ada")); + open_tab(&page, KEYS, cx); + assert!(page.shows(item("server-recovery-issue", KEY), cx)); + + page.click(item("server-recovery-issue", KEY), cx); + page.settle_dialog(cx); + cx.run_until_parked(); + page.render(cx); + assert!(page.shows("sign-in-key-saved", cx)); + assert_eq!(page.hub.count(FakeOp::ReissueRecoveryKey), 1); + + page.click("sign-in-key-saved", cx); + cx.run_until_parked(); + page.render(cx); + assert!( + !page.shows(item("server-recovery-issue", KEY), cx), + "a saved key is not offered again" + ); +} + +#[gpui_kit::test] +fn a_server_with_a_confirmed_recovery_key_offers_no_new_one(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + open_tab(&page, KEYS, cx); + assert!(!page.shows(item("server-recovery-issue", KEY), cx)); +} + +/// Counts how often the page asks to be drawn again. +fn count_redraws(page: &Fixture, cx: &mut TestAppContext) -> Rc> { + let redraws = Rc::new(Cell::new(0)); + let counted = Rc::clone(&redraws); + cx.update_window(page.window, |root, _, cx| { + let servers = root + .downcast::() + .unwrap() + .read(cx) + .view() + .clone() + .downcast::() + .unwrap(); + cx.observe(&servers, move |_, _| counted.set(counted.get() + 1)) + .detach(); + }) + .unwrap(); + redraws +} + +#[gpui_kit::test] +fn the_offer_of_a_new_recovery_key_appears_as_soon_as_the_vault_is_unlocked( + cx: &mut TestAppContext, +) { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "secret", "recovery-ada")); + hub.leave_recovery_key_unconfirmed(&hub_id()); + hub.lock_keys(&hub_id()); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub.clone(), vec![pinned_record()], Some("ada")); + open_tab(&page, KEYS, cx); + assert!( + !page.shows(item("server-recovery-issue", KEY), cx), + "a locked vault offers nothing" + ); + let redraws = count_redraws(&page, cx); + + hub.unlock_keys(&hub_id()); + cx.run_until_parked(); + + assert!(redraws.get() > 0, "the page is redrawn by the unlock alone"); + page.render(cx); + assert!(page.shows(item("server-recovery-issue", KEY), cx)); +} + +fn two_servers() -> (FakeHub, Vec) { + let other = FakeServer::new(OTHER, 7) + .named("Work hub") + .with_user("ada", "secret"); + let records = vec![pinned_record(), other.pinned_record("Work")]; + ( + FakeHub::new().with_server(server()).with_server(other), + records, + ) +} + +fn device(id: &str, name: &str, seen: i64, current: bool) -> DeviceSummary { + DeviceSummary { + id: id.into(), + name: name.into(), + platform: "desktop".into(), + created_at_ms: 1_000, + last_seen_at_ms: seen, + is_current: current, + } +} + +/// The page over a signed-in server whose account has a laptop (this device) and a phone. +fn page_with_devices(cx: &mut TestAppContext) -> Fixture { + let page = page(cx, vec![pinned_record()]); + page.devices.set( + &hub_id(), + vec![ + device("phone", "Pixel", 1_700_000_000_000, false), + device("laptop", "Laptop", 1_700_000_500_000, true), + ], + ); + page +} + +#[gpui_kit::test] +fn the_first_server_is_selected_and_choosing_another_shows_its_overview(cx: &mut TestAppContext) { + let (hub, records) = two_servers(); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub, records, Some("ada")); + assert!(page.shows(item("server-select", KEY), cx)); + assert!(page.shows(item("server-select", OTHER), cx)); + assert!(page.shows(item("server-overview", KEY), cx)); + assert!(!page.shows(item("server-overview", OTHER), cx)); + + page.click(item("server-select", OTHER), cx); + + assert!(page.shows(item("server-overview", OTHER), cx)); + assert!(!page.shows(item("server-overview", KEY), cx)); +} + +#[gpui_kit::test] +fn the_tabs_switch_between_the_overview_the_devices_and_the_keys(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + assert!(page.shows(item("server-overview", KEY), cx)); + + open_tab(&page, DEVICES, cx); + assert!(page.shows("devices-tab", cx)); + assert!(!page.shows(item("server-overview", KEY), cx)); + + open_tab(&page, KEYS, cx); + assert!(page.shows(item("server-trust-tab", KEY), cx)); + assert!(!page.shows("devices-tab", cx)); + + open_tab(&page, OVERVIEW, cx); + assert!(page.shows(item("server-overview", KEY), cx)); +} + +#[gpui_kit::test] +fn choosing_another_server_starts_at_its_overview(cx: &mut TestAppContext) { + let (hub, records) = two_servers(); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub, records, Some("ada")); + open_tab(&page, KEYS, cx); + + page.click(item("server-select", OTHER), cx); + + assert!(page.shows(item("server-overview", OTHER), cx)); +} + +#[gpui_kit::test] +fn the_devices_tab_lists_the_account_devices_and_marks_this_one(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + assert!(page.shows(item("device", "phone"), cx)); + assert!(page.shows(item("device", "laptop"), cx)); + assert!(page.shows(item("device-current", "laptop"), cx)); + assert!(!page.shows(item("device-current", "phone"), cx)); + assert!(page.shows(item("device-seen", "phone"), cx)); +} + +#[gpui_kit::test] +fn the_devices_tab_says_so_while_the_list_is_read_and_when_there_is_none(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + let release = page.devices.hold_next_list(); + open_tab(&page, DEVICES, cx); + assert!(page.shows("devices-loading", cx)); + + drop(release); + cx.run_until_parked(); + page.render(cx); + assert!(!page.shows("devices-loading", cx)); + assert!(page.shows("devices-empty", cx)); +} + +#[gpui_kit::test] +fn a_device_list_that_cannot_be_read_is_reported_and_read_again_on_request( + cx: &mut TestAppContext, +) { + let page = page_with_devices(cx); + page.devices.fail_with(HubError::PermissionDenied); + open_tab(&page, DEVICES, cx); + assert!(page.shows("devices-error", cx)); + assert!(!page.shows(item("device", "phone"), cx)); + + page.devices.recover(); + page.click("devices-retry", cx); + cx.run_until_parked(); + page.render(cx); + + assert!(!page.shows("devices-error", cx)); + assert!(page.shows(item("device", "phone"), cx)); +} + +#[gpui_kit::test] +fn revoking_a_device_asks_first_and_keeping_it_changes_nothing(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + page.click(item("device-revoke", "phone"), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx), "revoking asks first"); + assert!(page.devices.revoked().is_empty()); + page.click("revoke-device-keep", cx); + cx.run_until_parked(); + + assert!(page.devices.revoked().is_empty()); + assert!(page.shows(item("device", "phone"), cx)); +} + +#[gpui_kit::test] +fn confirming_revokes_the_device_and_the_list_is_read_again(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + page.click(item("device-revoke", "phone"), cx); + page.settle_dialog(cx); + page.click("revoke-device-confirm", cx); + cx.run_until_parked(); + page.render(cx); + + assert_eq!(page.devices.revoked(), vec![(hub_id(), "phone".to_owned())]); + assert!(!page.shows(item("device", "phone"), cx)); + assert!(page.shows(item("device", "laptop"), cx)); +} + +#[gpui_kit::test] +fn revoking_this_device_signs_out_of_the_server(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + page.click(item("device-revoke", "laptop"), cx); + page.settle_dialog(cx); + page.click("revoke-device-confirm", cx); + cx.run_until_parked(); + page.render(cx); + + assert!( + page.devices.revoked().is_empty(), + "the sign-out path ends the session" + ); + assert_eq!(page.hub.count(FakeOp::SignOut), 1); + assert_eq!(servers(&page, cx)[0].status, HubStatus::SignedOut); + assert!(page.shows(item("server-tab-signed-out", KEY), cx)); +} + +#[gpui_kit::test] +fn a_server_that_is_not_signed_in_offers_signing_in_instead_of_devices_and_keys( + cx: &mut TestAppContext, +) { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_over(cx, config, vec![pinned_record()], None); + + open_tab(&page, DEVICES, cx); + assert!(page.shows(item("server-tab-signed-out", KEY), cx)); + assert!(page.shows(item("server-tab-sign-in", KEY), cx)); + assert!(!page.shows("devices-tab", cx)); + + open_tab(&page, KEYS, cx); + assert!(page.shows(item("server-tab-signed-out", KEY), cx)); + assert!(!page.shows(item("server-trust-tab", KEY), cx)); + + page.click(item("server-tab-sign-in", KEY), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx), "the sign-in dialog opens"); +} + +/// The Keys tab of a server whose account keys are closed. +fn locked_keys(cx: &mut TestAppContext) -> Fixture { + let page = page(cx, vec![pinned_record()]); + page.hub.lock_keys(&hub_id()); + open_tab(&page, KEYS, cx); + page +} + +fn submit_password(page: &Fixture, password: &str, cx: &mut TestAppContext) { + page.type_text(password, cx); + page.click("account-keys-unlock", cx); + cx.run_until_parked(); + page.render(cx); +} + +#[gpui_kit::test] +fn the_right_password_opens_the_account_keys(cx: &mut TestAppContext) { + let page = locked_keys(cx); + assert!(page.shows("account-keys-locked", cx)); + + submit_password(&page, crate::test_support::PASSWORD, cx); + + assert!(page.shows("account-keys-open", cx)); + assert!(!page.shows("account-keys-locked", cx)); + assert_eq!(page.account_keys.attempts(), vec![true]); +} + +#[gpui_kit::test] +fn a_wrong_password_is_refused_and_the_account_keys_stay_closed(cx: &mut TestAppContext) { + let page = locked_keys(cx); + + submit_password(&page, "not it", cx); + + assert!(page.shows("account-keys-refused", cx)); + assert!(page.shows("account-keys-locked", cx)); + assert_eq!(page.account_keys.attempts(), vec![false]); +} diff --git a/crates/hubs-ui/src/sign_in.rs b/crates/hubs-ui/src/sign_in.rs new file mode 100644 index 00000000..4fa92240 --- /dev/null +++ b/crates/hubs-ui/src/sign_in.rs @@ -0,0 +1,786 @@ +//! The dialog that signs in to a server: address and fingerprint, the trust decision, then a +//! password or the browser. The order of the steps belongs to [`SignInSession`]; this view +//! shows the current step and passes the user's choices on. + +mod form; + +use gpui_kit::{ + App, AppContext as _, ClickEvent, Context, Entity, FocusHandle, Focusable, Subscription, + Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + h_flex, + input::Input, + v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::{Hubs, SignInError, SignInEvent, SignInSession, SignInStep, TrustNote}; +use nocterm_hubs_model::{HubId, OidcProvider}; +use nocterm_session::Secret; + +use crate::layout::{field, fingerprint_block, note, problem, title}; +use crate::messages::sign_in_problem; +use crate::switch::Switch; +use form::SignInForm; + +/// Starts signing in to a server not known yet, or again to `hub`, and shows the dialog. A hub +/// that is not known any more shows nothing. With a `switch`, finishing the sign-in opens the +/// server's window. +pub(crate) fn start( + hubs: &Entity, + hub: Option<&HubId>, + switch: Option, + window: &mut Window, + cx: &mut App, +) { + let session = hubs.update(cx, |hubs, cx| match hub { + Some(hub) => hubs.start_sign_in_for(hub, cx), + None => Some(hubs.start_sign_in(cx)), + }); + if let Some(session) = session { + open(session, switch, window, cx); + } +} + +/// Signs in again to the known server `hub` from its own window, such as when this device +/// holds no keys for its account. Nothing is switched: the window stays where it is. +pub fn sign_in_again(hubs: &Entity, hub: &HubId, window: &mut Window, cx: &mut App) { + start(hubs, Some(hub), None, window, cx); +} + +/// Shows the dialog that issues a new recovery key for the listed, signed-in server `hub`. +pub(crate) fn start_reissue( + hubs: &Entity, + hub: &HubId, + offer: nocterm_hubs_model::RecoveryKeyReissue, + window: &mut Window, + cx: &mut App, +) { + let session = hubs.update(cx, |hubs, cx| { + hubs.start_recovery_key_reissue(hub, offer, cx) + }); + if let Some(session) = session { + open(session, None, window, cx); + } +} + +/// Opens the dialog over `session`. Closing the dialog ends the sign-in: a browser sign-in +/// in flight is cancelled with the session. While a recovery key is being issued or shown the +/// dialog cannot be closed (button, backdrop or Escape): the key exists on the server and is +/// shown only here. +pub(crate) fn open( + session: Entity, + switch: Option, + window: &mut Window, + cx: &mut App, +) { + let dialog = cx.new(|cx| SignInDialog::new(session, switch, window, cx)); + let first = dialog.clone(); + window.open_dialog(cx, move |shell, _, cx| { + let submit = dialog.clone(); + let guard = dialog.downgrade(); + let (held, title) = { + let dialog = dialog.read(cx); + (dialog.holds_the_key(cx), dialog.title(cx)) + }; + shell + .title(title) + .w(px(480.)) + .close_button(!held) + .overlay_closable(!held) + .keyboard(!held) + .on_cancel(move |_, _, cx| { + guard + .upgrade() + .is_none_or(|dialog| !dialog.read(cx).holds_the_key(cx)) + }) + // Enter reaches the dialog as the confirm action. The step decides what it means + // and the dialog closes itself when it is done. + .on_ok(move |_, window, cx| { + submit.update(cx, |dialog, cx| dialog.primary(window, cx)); + false + }) + .footer(div()) + .child(dialog.clone()) + }); + first.update(cx, |dialog, cx| dialog.follow_step(window, cx)); +} + +struct SignInDialog { + session: Entity, + /// Opens the server's window once the sign-in is finished. + switch: Option, + form: SignInForm, + focus: FocusHandle, + /// The kind of step the focus was last placed for. + placed: Option>, + _subscriptions: Vec, +} + +impl SignInDialog { + fn new( + session: Entity, + switch: Option, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let form = SignInForm::new(window, cx); + let subscriptions = vec![ + cx.observe_in(&session, window, |this, _, window, cx| { + this.follow_step(window, cx); + }), + cx.subscribe(&session, |_, _, event, cx| { + if let SignInEvent::OpenBrowser(url) = event { + cx.open_url(url); + } + }), + ]; + Self { + session, + switch, + form, + focus: cx.focus_handle(), + placed: None, + _subscriptions: subscriptions, + } + } + + /// Puts the cursor where the step asks for input when the step changes. + fn follow_step(&mut self, window: &mut Window, cx: &mut Context) { + let step = self.step(cx); + let kind = std::mem::discriminant(&step); + if self.placed != Some(kind) { + self.placed = Some(kind); + let target = match step { + SignInStep::Address { .. } if self.session.read(cx).address_editable() => { + self.form.address().read(cx).focus_handle(cx) + } + SignInStep::Method { password: true, .. } => { + self.form.username().read(cx).focus_handle(cx) + } + SignInStep::Passphrase { .. } => self.form.passphrase().read(cx).focus_handle(cx), + SignInStep::Recovery { .. } => self.form.recovery_key().read(cx).focus_handle(cx), + _ => self.focus.clone(), + }; + window.focus(&target, cx); + } + cx.notify(); + } + + fn step(&self, cx: &App) -> SignInStep { + self.session.read(cx).step().clone() + } + + /// A recovery key is being issued or shown: leaving now would lose it. + fn holds_the_key(&self, cx: &App) -> bool { + matches!( + self.session.read(cx).step(), + SignInStep::RecoveryKey { .. } | SignInStep::Issuing + ) + } + + fn title(&self, cx: &App) -> &'static str { + if self.session.read(cx).reissue_only() { + "Recovery key" + } else { + "Sign in to a server" + } + } + + /// What Enter does on this step. Never a choice that cannot be taken back: on the trust + /// step it goes back. + fn primary(&mut self, window: &mut Window, cx: &mut Context) { + match self.step(cx) { + SignInStep::Address { .. } if self.session.read(cx).address_editable() => { + self.submit_address(cx); + } + SignInStep::Address { .. } | SignInStep::Failed(_) => self.close(window, cx), + SignInStep::ConfirmFingerprint { .. } | SignInStep::Mismatch { .. } => { + self.session.update(cx, |session, cx| session.back(cx)); + } + SignInStep::Method { password: true, .. } => self.sign_in_with_password(window, cx), + SignInStep::Finished(hub) => self.finish(&hub, window, cx), + SignInStep::Passphrase { .. } => self.set_vault_passphrase(window, cx), + SignInStep::Recovery { .. } => self.recover(window, cx), + // Enter must not skip past the one showing of the recovery key, nor replace a key + // the person may still hold. + SignInStep::RecoveryKey { .. } + | SignInStep::RecoveryOffer { .. } + | SignInStep::Issuing + | SignInStep::Method { .. } + | SignInStep::Checking + | SignInStep::Authenticating + | SignInStep::Browser { .. } + | SignInStep::Saving => {} + } + } + + fn close(&self, window: &mut Window, cx: &mut Context) { + window.close_dialog(cx); + } + + /// Closes the dialog and shows the server that was signed in to, when the dialog was asked to. + fn finish(&self, hub: &HubId, window: &mut Window, cx: &mut Context) { + self.close(window, cx); + if let Some(switch) = &self.switch { + switch.open_signed_in(hub, window, cx); + } + } + + fn submit_address(&mut self, cx: &mut Context) { + let address = self.form.address_text(cx); + let fingerprint = self.form.fingerprint_text(cx); + self.session.update(cx, |session, cx| { + session.submit_address(&address, &fingerprint, cx) + }); + } + + fn sign_in_with_password(&mut self, window: &mut Window, cx: &mut Context) { + let username = self.form.username_text(cx); + let password = self.form.take_password(window, cx); + self.session.update(cx, |session, cx| { + session.sign_in_with_password(&username, password, cx); + }); + } + + fn set_vault_passphrase(&mut self, window: &mut Window, cx: &mut Context) { + let passphrase = self.form.take_passphrase(window, cx); + self.session.update(cx, |session, cx| { + session.set_vault_passphrase(passphrase, cx); + }); + } + + fn recover(&mut self, window: &mut Window, cx: &mut Context) { + let recovery_key = self.form.take_recovery_key(window, cx); + self.session.update(cx, |session, cx| { + session.recover(recovery_key, cx); + }); + } + + fn sign_in_with_provider(&mut self, provider: &str, cx: &mut Context) { + self.session.update(cx, |session, cx| { + session.sign_in_with_provider(provider, cx) + }); + } + + fn render_step(&mut self, step: SignInStep, cx: &mut Context) -> impl IntoElement { + match step { + SignInStep::Address { problem } => self.render_address(problem.as_ref(), cx), + SignInStep::Checking => self.render_busy("Checking the server…", true, cx), + SignInStep::Authenticating => self.render_busy("Signing in…", false, cx), + SignInStep::Saving => self.render_busy("Saving the server…", false, cx), + SignInStep::ConfirmFingerprint { + address, + fingerprint, + } => self.render_confirm(&address.to_string(), &fingerprint, cx), + SignInStep::Mismatch { typed, presented } => { + self.render_mismatch(&typed, &presented, cx) + } + SignInStep::Method { + hub_name, + password, + providers, + note, + problem, + } => self.render_method(&hub_name, password, &providers, note, problem.as_ref(), cx), + SignInStep::Browser { provider, url } => self.render_browser(&provider, url, cx), + SignInStep::RecoveryKey { key } => self.render_recovery_key(key.key(), cx), + SignInStep::RecoveryOffer { problem, .. } => { + self.render_recovery_offer(problem.as_ref(), cx) + } + SignInStep::Issuing => v_flex() + .gap_3() + .child(note("Issuing a new recovery key…", cx)) + .into_any_element(), + SignInStep::Passphrase { problem } => self.render_passphrase(problem.as_ref(), cx), + SignInStep::Recovery { problem } => self.render_recovery(problem.as_ref(), cx), + SignInStep::Finished(hub) => self.render_finished(hub, cx), + SignInStep::Failed(error) => self.render_failed(&error, cx), + } + } + + fn render_address( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let editable = self.session.read(cx).address_editable(); + v_flex() + .gap_3() + .when(editable, |step| { + step.child(field("Server", Input::new(self.form.address()))) + .child(field( + "Certificate fingerprint", + Input::new(self.form.fingerprint()), + )) + .child(note( + "Leave the fingerprint empty to see the certificate first. A server with a certificate from a public authority needs none.", + cx, + )) + }) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-cancel") + .label("Cancel") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.close(window, cx); + })), + ) + .when(editable, |buttons| { + buttons.child( + Button::new("sign-in-continue") + .primary() + .label("Continue") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.submit_address(cx); + })), + ) + }), + ) + .into_any_element() + } + + fn render_busy( + &mut self, + what: &'static str, + cancels_dialog: bool, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(note(what, cx)) + .child( + h_flex() + .justify_end() + .child( + Button::new("sign-in-stop") + .label("Cancel") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + if cancels_dialog { + this.close(window, cx); + } else { + this.session.update(cx, |session, cx| session.back(cx)); + } + })), + ), + ) + .into_any_element() + } + + fn render_confirm( + &mut self, + address: &str, + fingerprint: &nocterm_hubs_model::Fingerprint, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(title(format!("Trust {address}?"), cx)) + .child(note( + "This server's certificate is not from a public authority. Compare its fingerprint with the one shown by the server, then trust and pin it.", + cx, + )) + .child(fingerprint_block("Fingerprint", fingerprint, cx)) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-back") + .primary() + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ) + .child( + Button::new("sign-in-trust") + .label("Trust and pin this certificate") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session + .update(cx, |session, cx| session.confirm_fingerprint(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_mismatch( + &mut self, + typed: &nocterm_hubs_model::Fingerprint, + presented: &nocterm_hubs_model::Fingerprint, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(problem( + "The server's certificate is not the one you typed. Do not continue unless you know why it changed." + .to_owned(), + cx, + )) + .child(fingerprint_block("You typed", typed, cx)) + .child(fingerprint_block("The server presented", presented, cx)) + .child( + h_flex().justify_end().child( + Button::new("sign-in-back") + .primary() + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_method( + &mut self, + hub_name: &str, + password: bool, + providers: &[OidcProvider], + trust: TrustNote, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let editable = self.session.read(cx).address_editable(); + let trust = match trust { + TrustNote::Pinned => "The certificate matches the fingerprint.", + TrustNote::PublicCa => "The certificate is vouched for by a public authority.", + TrustNote::Confirmed => "The certificate was pinned after you confirmed it.", + }; + v_flex() + .gap_3() + .child(title(format!("Sign in to {hub_name}"), cx)) + .child(note(trust, cx)) + .when(password, |step| { + step.child(field("User name", Input::new(self.form.username()))) + .child(field("Password", Input::new(self.form.password()))) + }) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child(v_flex().gap_2().children(providers.iter().map(|provider| { + let id = provider.id.clone(); + Button::new(provider_key(&provider.id)) + .w_full() + .label(format!("Continue with {}", provider.display_name)) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.sign_in_with_provider(&id, cx); + })) + }))) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-cancel") + .label(if editable { "Back" } else { "Cancel" }) + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + if editable { + this.session.update(cx, |session, cx| session.back(cx)); + } else { + this.close(window, cx); + } + })), + ) + .when(password, |buttons| { + buttons.child( + Button::new("sign-in-password") + .primary() + .label("Sign in") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.sign_in_with_password(window, cx); + })), + ) + }), + ) + .into_any_element() + } + + fn render_browser( + &mut self, + provider: &str, + url: Option, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(note( + format!("Finish signing in with {provider} in your browser. This window continues by itself."), + cx, + )) + .when_some(url.clone(), |step, url| step.child(note(url, cx))) + .child( + h_flex() + .gap_2() + .justify_end() + .when_some(url, |buttons, url| { + buttons.child(Button::new("sign-in-copy-link").label("Copy link").on_click( + move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + cx.write_to_clipboard(gpui_kit::ClipboardItem::new_string( + url.clone(), + )); + }, + )) + }) + .child( + Button::new("sign-in-stop") + .label("Cancel") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_recovery_key( + &mut self, + key: &Secret, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let text = key.expose().to_owned(); + v_flex() + .gap_3() + .child(title("Write down your recovery key".to_owned(), cx)) + .child(note( + "Your account keys were created. This key is the only way to open them if you lose your password, and it is not shown again or kept on this device.", + cx, + )) + .child(note(text.clone(), cx)) + .child( + h_flex() + .gap_2() + .justify_end() + .child(Button::new("sign-in-copy-key").label("Copy key").on_click( + move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + cx.write_to_clipboard(gpui_kit::ClipboardItem::new_string( + text.clone(), + )); + }, + )) + .child( + Button::new("sign-in-key-saved") + .primary() + .label("I have saved it") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| { + session.acknowledge_recovery_key(cx); + }); + })), + ), + ) + .into_any_element() + } + + fn render_recovery_offer( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let standalone = self.session.read(cx).reissue_only(); + v_flex() + .gap_3() + .child(title("Your recovery key was not confirmed".to_owned(), cx)) + .child(note( + "The recovery key made with your account keys was never confirmed as saved, so it may be lost. Issue a new one now: the old key stops working, and the new one is shown once.", + cx, + )) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-offer-later") + .label("Not now") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + if standalone { + this.close(window, cx); + } else { + this.session + .update(cx, |session, cx| session.skip_recovery_offer(cx)); + } + })), + ) + .child( + Button::new("sign-in-offer-issue") + .primary() + .label("Issue a new recovery key") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session + .update(cx, |session, cx| session.issue_recovery_key(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_passphrase( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(title("Vault passphrase".to_owned(), cx)) + .child(note( + "Signing in through a provider does not open your account keys. Type the passphrase that protects them; the first time, this sets it.", + cx, + )) + .child(field("Passphrase", Input::new(self.form.passphrase()))) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-passphrase-back") + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ) + .child( + Button::new("sign-in-passphrase") + .primary() + .label("Continue") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.set_vault_passphrase(window, cx); + })), + ), + ) + .into_any_element() + } + + fn render_recovery( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(title("Enter your recovery key".to_owned(), cx)) + .child(note( + "This password does not open your account keys; it was probably reset. The recovery key you saved when the account was created opens them, and this password opens them from now on.", + cx, + )) + .child(field("Recovery key", Input::new(self.form.recovery_key()))) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-recovery-back") + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ) + .child( + Button::new("sign-in-recovery") + .primary() + .label("Recover") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.recover(window, cx); + })), + ), + ) + .into_any_element() + } + + fn render_finished(&mut self, hub: HubId, cx: &mut Context) -> gpui_kit::AnyElement { + let (heading, text) = if self.session.read(cx).reissue_only() { + ( + "Recovery key saved", + "The new recovery key opens your account keys. The old one no longer does.", + ) + } else { + let text = if self.switch.is_some() { + "The server is saved. Its connections and vault open in its own window." + } else { + "The server is saved." + }; + ("Signed in", text) + }; + v_flex() + .gap_3() + .child(title(heading.to_owned(), cx)) + .child(note(text, cx)) + .child( + h_flex().justify_end().child( + Button::new("sign-in-done") + .primary() + .label("Done") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + this.finish(&hub, window, cx); + })), + ), + ) + .into_any_element() + } + + fn render_failed( + &mut self, + error: &SignInError, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(problem(sign_in_problem(error), cx)) + .child( + h_flex().justify_end().child( + Button::new("sign-in-close") + .primary() + .label("Close") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.close(window, cx); + })), + ), + ) + .into_any_element() + } +} + +/// A stable element id part for a provider. +fn provider_key(id: &str) -> gpui_kit::SharedString { + format!("sign-in-provider-{id}").into() +} + +impl Focusable for SignInDialog { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +impl Render for SignInDialog { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let step = self.step(cx); + v_flex() + .id("sign-in-dialog") + .track_focus(&self.focus) + .w_full() + .child(self.render_step(step, cx)) + } +} + +#[cfg(test)] +#[path = "sign_in_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/sign_in/form.rs b/crates/hubs-ui/src/sign_in/form.rs new file mode 100644 index 00000000..64fb3c49 --- /dev/null +++ b/crates/hubs-ui/src/sign_in/form.rs @@ -0,0 +1,111 @@ +//! The text fields of the sign-in dialog. + +use gpui_kit::{AppContext as _, Context, Entity, Window, component::input::InputState}; +use nocterm_session::Secret; + +/// The address, fingerprint, user name, password, vault passphrase and recovery key fields. A +/// secret is read out of its field once and the field is emptied, so a failed attempt starts +/// from a blank one. +pub(crate) struct SignInForm { + address: Entity, + fingerprint: Entity, + username: Entity, + password: Entity, + passphrase: Entity, + recovery_key: Entity, +} + +impl SignInForm { + pub(crate) fn new(window: &mut Window, cx: &mut Context) -> Self { + Self { + address: cx.new(|cx| InputState::new(window, cx).placeholder("hub.example.com:7443")), + fingerprint: cx + .new(|cx| InputState::new(window, cx).placeholder("SHA-256 fingerprint, optional")), + username: cx.new(|cx| InputState::new(window, cx).placeholder("User name")), + password: cx.new(|cx| { + InputState::new(window, cx) + .masked(true) + .placeholder("Password") + }), + passphrase: cx.new(|cx| { + InputState::new(window, cx) + .masked(true) + .placeholder("Vault passphrase") + }), + recovery_key: cx.new(|cx| InputState::new(window, cx).placeholder("Recovery key")), + } + } + + pub(crate) fn address(&self) -> &Entity { + &self.address + } + + pub(crate) fn fingerprint(&self) -> &Entity { + &self.fingerprint + } + + pub(crate) fn username(&self) -> &Entity { + &self.username + } + + pub(crate) fn password(&self) -> &Entity { + &self.password + } + + pub(crate) fn passphrase(&self) -> &Entity { + &self.passphrase + } + + pub(crate) fn recovery_key(&self) -> &Entity { + &self.recovery_key + } + + pub(crate) fn address_text(&self, cx: &Context) -> String { + self.address.read(cx).value().to_string() + } + + pub(crate) fn fingerprint_text(&self, cx: &Context) -> String { + self.fingerprint.read(cx).value().to_string() + } + + pub(crate) fn username_text(&self, cx: &Context) -> String { + self.username.read(cx).value().to_string() + } + + /// The typed password; the field is emptied. + pub(crate) fn take_password( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Secret { + take(&self.password, window, cx) + } + + /// The typed vault passphrase; the field is emptied. + pub(crate) fn take_passphrase( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Secret { + take(&self.passphrase, window, cx) + } + + /// The typed recovery key; the field is emptied. + pub(crate) fn take_recovery_key( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Secret { + take(&self.recovery_key, window, cx) + } +} + +fn take( + field: &Entity, + window: &mut Window, + cx: &mut Context, +) -> Secret { + let secret = Secret::new(field.read(cx).value().to_string()); + field.update(cx, |input, cx| input.set_value("", window, cx)); + secret +} diff --git a/crates/hubs-ui/src/sign_in_tests.rs b/crates/hubs-ui/src/sign_in_tests.rs new file mode 100644 index 00000000..db3e9938 --- /dev/null +++ b/crates/hubs-ui/src/sign_in_tests.rs @@ -0,0 +1,437 @@ +//! Signing in to a server through the dialog, step by step, over a fake hub. +use crate::Placement; +use crate::SignInToServer; +use crate::test_support::{Fixture, fixture, server}; +use gpui_kit::TestAppContext; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp, FakeServer}; +use nocterm_hubs_model::{Environment, Fingerprint, HubError}; + +fn dialog_over(server: FakeServer, cx: &mut TestAppContext) -> Fixture { + dialog_over_tabs(server, false, cx) +} + +fn dialog_over_tabs(server: FakeServer, tabs: bool, cx: &mut TestAppContext) -> Fixture { + let window = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server), + Vec::new(), + None, + ); + if tabs { + window.add_tab(cx); + } + window.dispatch(SignInToServer, cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + window +} + +/// Types the address (and a fingerprint) and moves on. +fn submit( + window: &Fixture, + address: &str, + fingerprint: Option<&Fingerprint>, + cx: &mut TestAppContext, +) { + window.type_text(address, cx); + if let Some(fingerprint) = fingerprint { + window.press("tab", cx); + window.type_text(&fingerprint.to_string(), cx); + } + window.click("sign-in-continue", cx); + cx.run_until_parked(); + window.render(cx); +} + +fn sign_in_with_password(window: &Fixture, user: &str, password: &str, cx: &mut TestAppContext) { + window.type_text(user, cx); + window.press("tab", cx); + window.type_text(password, cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); +} + +fn saved(window: &Fixture, cx: &mut TestAppContext) -> usize { + cx.update(|cx| window.installed.hubs().read(cx).list().len()) +} + +#[gpui_kit::test] +fn a_matching_fingerprint_goes_straight_to_the_password(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit( + &window, + "hub.home:7443", + Some(&Fingerprint::from_bytes([7; 32])), + cx, + ); + assert!(window.shows("sign-in-password", cx)); + assert!(!window.shows("sign-in-trust", cx)); +} + +#[gpui_kit::test] +fn a_fingerprint_that_does_not_match_offers_only_going_back(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit( + &window, + "hub.home:7443", + Some(&Fingerprint::from_bytes([8; 32])), + cx, + ); + assert!(window.shows("sign-in-back", cx)); + assert!(!window.shows("sign-in-trust", cx)); + assert!(!window.shows("sign-in-password", cx)); + assert!(!window.shows("sign-in-continue", cx)); + assert_eq!( + window.hub.count(FakeOp::Connect), + 0, + "the hub is not contacted again" + ); + assert_eq!(saved(&window, cx), 0); +} + +#[gpui_kit::test] +fn a_self_signed_server_asks_to_trust_and_pin_its_certificate(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-trust", cx)); + assert!(window.shows("sign-in-back", cx)); + window.click("sign-in-trust", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-password", cx)); +} + +#[gpui_kit::test] +fn enter_on_the_trust_step_goes_back_instead_of_trusting(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit(&window, "hub.home:7443", None, cx); + window.press("enter", cx); + assert!(window.shows("sign-in-continue", cx), "back at the address"); + assert!(!window.shows("sign-in-password", cx)); + assert_eq!(window.hub.count(FakeOp::Connect), 0); +} + +#[gpui_kit::test] +fn a_certificate_from_a_public_authority_needs_no_decision(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-password", cx)); + assert!(!window.shows("sign-in-trust", cx)); +} + +#[gpui_kit::test] +fn a_wrong_password_empties_the_field_and_the_right_one_signs_in(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "wrong", cx); + assert!(window.shows("sign-in-password", cx), "back at the methods"); + assert!(!window.shows("sign-in-done", cx)); + // Had the wrong password stayed in its field, this would be "wrongsecret". + window.press("tab", cx); + window.type_text("secret", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1, "the server is saved"); + let attempts = window + .hub + .calls() + .iter() + .filter(|call| matches!(call, FakeCall::Password { .. })) + .count(); + assert_eq!(attempts, 2); +} + +#[gpui_kit::test] +fn done_closes_the_dialog_and_the_server_is_listed(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + window.click("sign-in-done", cx); + assert!(!window.has_dialog(cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn finishing_a_sign_in_from_an_empty_window_opens_the_server_in_it(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + window.click("sign-in-done", cx); + assert_eq!( + window.environments.requests(), + vec![( + Environment::Hub(crate::test_support::hub_id()), + Placement::ThisWindow + )] + ); +} + +#[gpui_kit::test] +fn finishing_a_sign_in_from_a_window_with_tabs_opens_the_server_beside_it(cx: &mut TestAppContext) { + let window = dialog_over_tabs(server().publicly_trusted(), true, cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + window.click("sign-in-done", cx); + assert_eq!( + window.environments.requests(), + vec![( + Environment::Hub(crate::test_support::hub_id()), + Placement::NewWindow + )] + ); +} + +#[gpui_kit::test] +fn browser_sign_in_opens_the_provider_page_and_finishes_when_the_browser_does( + cx: &mut TestAppContext, +) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.render(cx); + assert_eq!( + cx.opened_url().as_deref(), + Some("https://idp.fake/authorize?provider=corp") + ); + assert!(window.shows("sign-in-copy-link", cx)); + window.hub.complete_oidc("ada"); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); +} + +#[gpui_kit::test] +fn cancelling_the_browser_sign_in_stops_waiting_for_it(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.render(cx); + window.click("sign-in-stop", cx); + cx.run_until_parked(); + assert_eq!(window.hub.cancelled_oidc(), 1); + assert!(window.shows("sign-in-password", cx), "back at the methods"); + assert_eq!(saved(&window, cx), 0); +} + +#[gpui_kit::test] +fn closing_the_dialog_during_a_browser_sign_in_cancels_it(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.render(cx); + window.press("escape", cx); + cx.run_until_parked(); + assert!(!window.has_dialog(cx)); + assert_eq!(window.hub.cancelled_oidc(), 1); +} + +#[gpui_kit::test] +fn a_server_that_speaks_another_protocol_is_refused_at_the_address(cx: &mut TestAppContext) { + let mut outdated = server().publicly_trusted(); + outdated.protocol = 9; + let window = dialog_over(outdated, cx); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-continue", cx), "still at the address"); + assert!(!window.shows("sign-in-password", cx)); + assert_eq!(saved(&window, cx), 0); +} + +#[gpui_kit::test] +fn a_server_that_does_not_answer_leaves_the_address_to_correct(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + window.hub.fail_next( + FakeOp::Probe, + HubError::Unreachable { + address: nocterm_hubs_model::HubAddress::parse("hub.home:7443").unwrap(), + cause: nocterm_core::error::SharedCause::new(std::io::Error::from( + std::io::ErrorKind::ConnectionRefused, + )), + }, + ); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-continue", cx)); +} + +#[gpui_kit::test] +fn the_address_is_validated_before_the_hub_is_asked(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit(&window, "http://hub.home", None, cx); + assert!(window.shows("sign-in-continue", cx)); + assert_eq!(window.hub.count(FakeOp::Probe), 0); +} + +#[gpui_kit::test] +fn a_new_account_shows_the_recovery_key_and_lists_the_server_once_it_is_saved( + cx: &mut TestAppContext, +) { + let window = dialog_over(server().publicly_trusted().keyed(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert_eq!(saved(&window, cx), 0, "not listed before the key is saved"); + + window.press("enter", cx); + window.render(cx); + assert!( + window.shows("sign-in-key-saved", cx), + "Enter does not skip the one showing of the key" + ); + + window.click("sign-in-key-saved", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn a_reset_password_asks_for_the_recovery_key_and_a_wrong_one_is_reported(cx: &mut TestAppContext) { + let window = dialog_over( + server() + .publicly_trusted() + .with_keyset("ada", "old", "recovery-key"), + cx, + ); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + assert!(window.shows("sign-in-recovery", cx)); + + window.type_text("not it", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-recovery", cx)); + assert_eq!(saved(&window, cx), 0); + + window.type_text("recovery-key", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn a_provider_sign_in_asks_for_the_vault_passphrase(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted().keyed(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.hub.complete_oidc("ada"); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-passphrase", cx)); + + window.type_text("a long passphrase", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert_eq!(window.hub.count(FakeOp::SetVaultPassphrase), 1); +} + +#[gpui_kit::test] +fn a_new_recovery_key_cannot_be_left_behind_by_closing_the_dialog(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted().keyed(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert!( + !window.shows("close", cx), + "the close button is not offered" + ); + + window.press("escape", cx); + cx.run_until_parked(); + window.render(cx); + + assert!(window.has_dialog(cx)); + assert!( + window.shows("sign-in-key-saved", cx), + "Escape does not close it" + ); + + window.click("sign-in-key-saved", cx); + cx.run_until_parked(); + window.render(cx); + window.press("escape", cx); + cx.run_until_parked(); + window.render(cx); + assert!( + !window.has_dialog(cx), + "once the key is saved Escape closes it" + ); +} + +/// A dialog over a server this device is signed in to whose recovery key was never confirmed. +fn unconfirmed_key_dialog(cx: &mut TestAppContext) -> Fixture { + let hub = FakeHub::new().with_server(server().publicly_trusted().with_keyset( + "ada", + "secret", + "recovery-ada", + )); + let id = crate::test_support::hub_id(); + hub.leave_recovery_key_unconfirmed(&id); + let window = fixture(cx, Environment::Local, hub, Vec::new(), Some("ada")); + window.dispatch(SignInToServer, cx); + window.settle_dialog(cx); + submit(&window, "hub.home:7443", None, cx); + window +} + +#[gpui_kit::test] +fn an_unconfirmed_recovery_key_is_offered_again_and_the_new_one_must_be_saved( + cx: &mut TestAppContext, +) { + let window = unconfirmed_key_dialog(cx); + assert!(window.shows("sign-in-offer-issue", cx)); + assert_eq!(saved(&window, cx), 0); + + window.click("sign-in-offer-issue", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert_eq!(window.hub.count(FakeOp::ReissueRecoveryKey), 1); + assert!(!window.shows("close", cx), "the new key is shown once"); + + window.click("sign-in-key-saved", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn the_offer_of_a_new_recovery_key_can_wait(cx: &mut TestAppContext) { + let window = unconfirmed_key_dialog(cx); + + window.click("sign-in-offer-later", cx); + cx.run_until_parked(); + window.render(cx); + + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); + assert_eq!(window.hub.count(FakeOp::ReissueRecoveryKey), 0); +} + +#[gpui_kit::test] +fn a_failed_reissue_says_why_and_offers_it_again(cx: &mut TestAppContext) { + let window = unconfirmed_key_dialog(cx); + window + .hub + .fail_next(FakeOp::ReissueRecoveryKey, HubError::KeysLocked); + + window.click("sign-in-offer-issue", cx); + cx.run_until_parked(); + window.render(cx); + + assert!(window.shows("sign-in-offer-issue", cx)); + assert!(!window.shows("sign-in-key-saved", cx)); +} diff --git a/crates/hubs-ui/src/status.rs b/crates/hubs-ui/src/status.rs new file mode 100644 index 00000000..47e6c1d4 --- /dev/null +++ b/crates/hubs-ui/src/status.rs @@ -0,0 +1,59 @@ +//! How the status of a server reads, and in what colour, wherever a server is listed. + +use gpui_kit::{App, Hsla, SharedString, component::ActiveTheme as _}; +use nocterm_hubs::HubStatus; +use nocterm_ui::IconName; + +/// The status as a short phrase with the colour it is drawn in. +pub(crate) fn status_chip(status: &HubStatus, cx: &App) -> (SharedString, Hsla) { + let theme = cx.theme(); + let now = cx.background_executor().now(); + chip( + status, + now, + theme.success, + theme.muted_foreground, + theme.warning, + theme.danger, + ) +} + +/// How a server's status reads in the list, and in what colour. +pub(crate) fn chip( + status: &HubStatus, + now: std::time::Instant, + good: Hsla, + quiet: Hsla, + warn: Hsla, + bad: Hsla, +) -> (SharedString, Hsla) { + match status { + HubStatus::Online => ("Online".into(), good), + HubStatus::Connecting => ("Connecting…".into(), quiet), + HubStatus::Offline { retry_at } => { + let seconds = retry_at.saturating_duration_since(now).as_secs_f64().ceil(); + (format!("Offline · retry in {seconds:.0} s").into(), warn) + } + HubStatus::SignedOut => ("Sign-in required".into(), warn), + HubStatus::Revoked => ("Device revoked".into(), bad), + HubStatus::TrustFailed { .. } => ("Certificate changed".into(), bad), + HubStatus::UpgradeRequired { hub, client } if hub < client => { + ("Update the server".into(), warn) + } + HubStatus::UpgradeRequired { .. } => ("Update Nocterm".into(), warn), + } +} + +pub(crate) fn status_icon(status: &HubStatus) -> IconName { + match status { + HubStatus::Online | HubStatus::Connecting | HubStatus::SignedOut => IconName::Server, + HubStatus::Offline { .. } => IconName::ServerOff, + HubStatus::Revoked | HubStatus::TrustFailed { .. } | HubStatus::UpgradeRequired { .. } => { + IconName::TriangleAlert + } + } +} + +#[cfg(test)] +#[path = "status_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/status_tests.rs b/crates/hubs-ui/src/status_tests.rs new file mode 100644 index 00000000..9777acdb --- /dev/null +++ b/crates/hubs-ui/src/status_tests.rs @@ -0,0 +1,31 @@ +//! The chip of a server that speaks another protocol says which side needs the update. + +use super::status_chip; +use crate::test_support::{fixture, server}; +use gpui_kit::TestAppContext; +use nocterm_hubs::HubStatus; +use nocterm_hubs_model::Environment; +use nocterm_hubs_model::testing::FakeHub; + +fn label(status: HubStatus, cx: &mut TestAppContext) -> String { + let _page = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + Vec::new(), + None, + ); + cx.update(|cx| status_chip(&status, cx).0.to_string()) +} + +#[gpui_kit::test] +fn a_server_behind_this_nocterm_is_the_one_to_update(cx: &mut TestAppContext) { + let status = HubStatus::UpgradeRequired { hub: 1, client: 2 }; + assert_eq!(label(status, cx), "Update the server"); +} + +#[gpui_kit::test] +fn a_server_ahead_of_this_nocterm_asks_to_update_nocterm(cx: &mut TestAppContext) { + let status = HubStatus::UpgradeRequired { hub: 3, client: 2 }; + assert_eq!(label(status, cx), "Update Nocterm"); +} diff --git a/crates/hubs-ui/src/switch.rs b/crates/hubs-ui/src/switch.rs new file mode 100644 index 00000000..89865aba --- /dev/null +++ b/crates/hubs-ui/src/switch.rs @@ -0,0 +1,166 @@ +//! Moving the window from one environment to another, or opening one beside it. + +use std::rc::Rc; + +use gpui_kit::{ + App, ClickEvent, WeakEntity, Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + }, + prelude::*, +}; +use nocterm_hubs_model::{Environment, HubId}; +use nocterm_workspace::Workspace; + +use crate::ports::{EnvironmentOpener, Placement}; + +/// Opens environments for the window it belongs to, asking first when switching would end +/// tabs that cannot be taken back. +#[derive(Clone)] +pub(crate) struct Switch { + opener: Rc, + workspace: WeakEntity, + current: Environment, +} + +impl Switch { + pub(crate) fn new( + opener: Rc, + workspace: WeakEntity, + current: Environment, + ) -> Self { + Self { + opener, + workspace, + current, + } + } + + pub(crate) fn current(&self) -> &Environment { + &self.current + } + + /// Opens `target`. Switching this window while it has tabs asks first: their sessions end + /// with the window's contents. + pub(crate) fn open( + &self, + target: Environment, + placement: Placement, + window: &mut Window, + cx: &mut App, + ) { + if placement == Placement::ThisWindow && target == self.current { + return; + } + let tabs = self.tabs(cx); + if placement == Placement::ThisWindow && tabs > 0 { + self.confirm(target, tabs, window, cx); + } else { + self.perform(&target, placement, window, cx); + } + } + + /// Shows the server just signed in to: in this window when it has no tabs to lose, else in + /// a window of its own. + pub(crate) fn open_signed_in(&self, hub: &HubId, window: &mut Window, cx: &mut App) { + let placement = if self.tabs(cx) == 0 { + Placement::ThisWindow + } else { + Placement::NewWindow + }; + self.open(Environment::Hub(hub.clone()), placement, window, cx); + } + + fn tabs(&self, cx: &App) -> usize { + self.workspace + .read_with(cx, |workspace, cx| workspace.items(cx).count()) + .unwrap_or(0) + } + + fn perform( + &self, + target: &Environment, + placement: Placement, + window: &mut Window, + cx: &mut App, + ) { + if let Err(error) = self.opener.open(target.clone(), placement, window, cx) { + nocterm_ui::notice::error( + window, + cx, + "environment-open", + "Could not open the window", + error.to_string(), + ); + } + } + + /// "N tabs are open in this window. Their sessions will end." + fn confirm(&self, target: Environment, tabs: usize, window: &mut Window, cx: &mut App) { + let switch = self.clone(); + window.open_alert_dialog(cx, move |alert, _, _| { + let button = |id: &'static str, label: &'static str, placement, close: bool| { + let (switch, target) = (switch.clone(), target.clone()); + Button::new(id).label(label).on_click( + move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + if close { + switch.perform(&target, placement, window, cx); + } + }, + ) + }; + let new_window = { + let (switch, target) = (switch.clone(), target.clone()); + Button::new("switch-new-window") + .primary() + .label("Open in a new window") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + switch.perform(&target, Placement::NewWindow, window, cx); + }) + }; + let ok = { + let (switch, target) = (switch.clone(), target.clone()); + move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + switch.perform(&target, Placement::NewWindow, window, cx); + true + } + }; + alert + .title("Switch this window?") + .description(format!( + "{} open in this window. Their sessions end if the window switches.", + if tabs == 1 { + "1 tab is".to_owned() + } else { + format!("{tabs} tabs are") + } + )) + // Enter takes the safe choice, which keeps the tabs. + .on_ok(ok) + .footer( + DialogFooter::new() + .child(button( + "switch-cancel", + "Cancel", + Placement::ThisWindow, + false, + )) + .child(button( + "switch-here", + "Switch and close tabs", + Placement::ThisWindow, + true, + )) + .child(new_window), + ) + }); + } +} + +#[cfg(test)] +#[path = "switch_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/switch_tests.rs b/crates/hubs-ui/src/switch_tests.rs new file mode 100644 index 00000000..a4a36959 --- /dev/null +++ b/crates/hubs-ui/src/switch_tests.rs @@ -0,0 +1,109 @@ +//! Switching a window asks first when it would end its tabs. +use crate::test_support::{Fixture, fixture, hub_id, pinned_record, server}; +use crate::{OpenEnvironmentMenu, Placement}; +use gpui_kit::TestAppContext; +use nocterm_hubs_model::{Environment, testing::FakeHub}; + +fn window_with_a_tab(environment: Environment, cx: &mut TestAppContext) -> Fixture { + let window = fixture( + cx, + environment, + FakeHub::new().with_server(server()), + vec![pinned_record()], + Some("ada"), + ); + window.add_tab(cx); + window +} + +/// Chooses the server in the menu of a local window with a tab. +fn choose_the_server(window: &Fixture, cx: &mut TestAppContext) { + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); +} + +#[gpui_kit::test] +fn a_local_window_with_tabs_asks_before_it_switches(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + assert!(window.has_dialog(cx)); + assert!(window.shows("switch-here", cx)); + assert!(window.shows("switch-new-window", cx)); + assert!(window.shows("switch-cancel", cx)); + assert!( + window.environments.requests().is_empty(), + "nothing opens before the answer" + ); +} + +#[gpui_kit::test] +fn cancelling_the_question_changes_nothing(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + window.click("switch-cancel", cx); + assert!(!window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn enter_takes_the_choice_that_keeps_the_tabs(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + window.press("enter", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn switch_and_close_tabs_replaces_this_window(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + window.click("switch-here", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::ThisWindow)] + ); +} + +#[gpui_kit::test] +fn opening_in_a_new_window_asks_nothing(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-new-window", 1usize), cx); + assert!(!window.has_dialog(cx)); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn a_hub_window_with_tabs_asks_before_it_switches_back(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Hub(hub_id()), cx); + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-row", 0usize), cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn a_window_without_tabs_switches_without_asking(cx: &mut TestAppContext) { + let window = fixture( + cx, + Environment::Hub(hub_id()), + FakeHub::new().with_server(server()), + vec![pinned_record()], + Some("ada"), + ); + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-row", 0usize), cx); + assert!(!window.has_dialog(cx)); + assert_eq!( + window.environments.requests(), + vec![(Environment::Local, Placement::ThisWindow)] + ); +} diff --git a/crates/hubs-ui/src/test_support.rs b/crates/hubs-ui/src/test_support.rs new file mode 100644 index 00000000..4256a3fa --- /dev/null +++ b/crates/hubs-ui/src/test_support.rs @@ -0,0 +1,261 @@ +//! Fixtures the tests of this crate share: a window over a [`FakeHub`], and a recording stand-in +//! for the port the application implements. + +use std::cell::RefCell; +use std::rc::Rc; +use std::sync::Arc; + +use gpui_kit::{ + AnyWindowHandle, AppContext as _, ElementId, Entity, Focusable as _, TestAppContext, + WindowOptions, component::WindowExt as _, test::TestWindowExt as _, +}; +use nocterm_hubs_model::testing::{FakeAccountDevices, FakeAccountKeys, FakeHub, FakeServer}; +use nocterm_hubs_model::{Environment, HubAddress, HubId, HubRecord, HubsSettings}; +use nocterm_settings::SettingsDocument; +use nocterm_store::SettingsConfig; +use nocterm_ui::DesignTokens; +use nocterm_workspace::{Workspace, test_support::deps_for}; + +use crate::{EnvironmentOpenError, EnvironmentOpener, Installed, Placement, install, register}; + +pub(crate) const ADDRESS: &str = "hub.home:7443"; +/// The password that opens the account keys in the fixture. +pub(crate) const PASSWORD: &str = "correct horse"; + +pub(crate) fn hub_id() -> HubId { + HubId::from(HubAddress::parse(ADDRESS).unwrap()) +} + +/// The server most tests talk to: certificate `[7; 32]`, user `ada`. +pub(crate) fn server() -> FakeServer { + FakeServer::new(ADDRESS, 7) + .named("Home hub") + .with_user("ada", "secret") + .with_provider("corp", "Corporate SSO") +} + +/// The record of [`server`] as the settings keep it after a sign-in. +pub(crate) fn pinned_record() -> HubRecord { + server().pinned_record("Home") +} + +/// Remembers which environments the views asked to open. +#[derive(Default)] +pub(crate) struct RecordingEnvironments { + requests: RefCell>, +} + +impl RecordingEnvironments { + pub(crate) fn requests(&self) -> Vec<(Environment, Placement)> { + self.requests.borrow().clone() + } +} + +impl EnvironmentOpener for RecordingEnvironments { + fn open( + &self, + environment: Environment, + placement: Placement, + _: &mut gpui_kit::Window, + _: &mut gpui_kit::App, + ) -> Result<(), EnvironmentOpenError> { + self.requests.borrow_mut().push((environment, placement)); + Ok(()) + } +} + +pub(crate) struct Fixture { + pub(crate) window: AnyWindowHandle, + pub(crate) workspace: Entity, + pub(crate) hub: FakeHub, + pub(crate) devices: FakeAccountDevices, + pub(crate) agents: nocterm_hub_agents_model::testing::FakeHubAgents, + pub(crate) account_keys: FakeAccountKeys, + pub(crate) installed: Installed, + pub(crate) environments: Rc, +} + +/// A window over `hub`, with `known` servers in the settings. The hub is signed in to when +/// `signed_in` names a user of the server. +pub(crate) fn fixture( + cx: &mut TestAppContext, + environment: Environment, + hub: FakeHub, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + fixture_over(cx, config, environment, hub, known, signed_in) +} + +/// As [`fixture`], but the settings are kept by `config`, which may fail to save. +pub(crate) fn fixture_over( + cx: &mut TestAppContext, + config: SettingsConfig, + environment: Environment, + hub: FakeHub, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + if let Some(user) = signed_in { + hub.sign_in_as(&hub_id(), user); + } + let devices = FakeAccountDevices::default(); + let agents = nocterm_hub_agents_model::testing::FakeHubAgents::new(); + let account_keys = FakeAccountKeys::alongside(&hub, PASSWORD); + let environments = Rc::new(RecordingEnvironments::default()); + let (window, workspace, installed) = cx.update(|cx| { + gpui_kit::init(cx); + let settings = config + .with(HubsSettings { + servers: known, + ..HubsSettings::default() + }) + .install(cx); + nocterm_ui::init(DesignTokens::builtin(), settings.clone(), cx); + let installed = install(nocterm_hubs::install( + nocterm_hubs::Deps { + trust: Arc::new(hub.clone()), + sign_in: Arc::new(hub.clone()), + devices: Arc::new(devices.clone()), + account_keys: Arc::new(account_keys.clone()), + }, + settings, + cx, + )) + .with_hub_agents( + nocterm_hub_agents::install( + nocterm_hub_agents::Deps { + directory: Arc::new(agents.clone()), + servers: Arc::new(agents.clone()), + changes: Arc::new(agents.clone()), + hosts: Arc::new(nocterm_ssh_keys::testing::FakeAgentHosts::default()), + }, + cx, + ) + .agents() + .clone(), + ); + let (window, workspace) = + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| Workspace::new(deps_for(&[], cx), window, cx)) + }) + .unwrap(); + (window, workspace, installed) + }); + cx.run_until_parked(); + let setup = Fixture { + window, + workspace, + hub, + devices, + agents, + account_keys, + installed, + environments, + }; + setup.compose(cx, environment); + cx.run_until_parked(); + setup +} + +impl Fixture { + /// Adds a tab of a remote session, so the window holds something to lose. + pub(crate) fn add_tab(&self, cx: &mut TestAppContext) { + use nocterm_session::{Auth, Target}; + use nocterm_workspace::{SessionSpec, test_support::SessionProbe}; + cx.update_window(self.window, |_, window, cx| { + self.workspace.update(cx, |workspace, cx| { + let spec = SessionSpec { + options: Default::default(), + title: "shell".into(), + profile: None, + target: Target::new("me", "host.example", 22), + auth: Auth::Auto, + launch: None, + credential: None, + jumps: Vec::new(), + }; + let tab = SessionProbe::new(spec, true, cx); + workspace.add_item(tab, window, cx); + }); + }) + .unwrap(); + cx.run_until_parked(); + } + + fn compose(&self, cx: &mut TestAppContext, environment: Environment) { + cx.update_window(self.window, |_, window, cx| { + self.workspace.update(cx, |workspace, cx| { + register( + &self.installed, + environment, + self.environments.clone(), + workspace, + window, + cx, + ); + }); + }) + .unwrap(); + } + + /// Draws the window. + pub(crate) fn render(&self, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.render_frame(cx)) + .unwrap(); + cx.run_until_parked(); + } + + /// Runs `action` as if the focused workspace received it. + pub(crate) fn dispatch(&self, action: impl gpui_kit::Action, cx: &mut TestAppContext) { + // Focus belongs to a drawn tree. + self.render(cx); + cx.update_window(self.window, |_, window, cx| { + window.focus(&self.workspace.read(cx).focus_handle(cx), cx); + window.dispatch_action(Box::new(action), cx); + }) + .unwrap(); + self.render(cx); + self.render(cx); + } + + pub(crate) fn click(&self, id: impl Into, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.click(id, cx)) + .unwrap(); + self.render(cx); + } + + pub(crate) fn press(&self, key: &str, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.press(key, cx)) + .unwrap(); + self.render(cx); + } + + pub(crate) fn type_text(&self, text: &str, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.input(text, cx)) + .unwrap(); + self.render(cx); + } + + /// Whether an element with `id` was drawn. + pub(crate) fn shows(&self, id: impl Into, cx: &mut TestAppContext) -> bool { + let id = id.into(); + cx.update_window(self.window, |_, window, _| window.try_find(id).is_some()) + .unwrap() + } + + pub(crate) fn has_dialog(&self, cx: &mut TestAppContext) -> bool { + cx.update_window(self.window, |_, window, cx| window.has_active_dialog(cx)) + .unwrap() + } + + /// Waits for the entrance animation of a dialog, so its buttons can be clicked. + pub(crate) fn settle_dialog(&self, cx: &mut TestAppContext) { + self.render(cx); + std::thread::sleep( + *gpui_kit::component::dialog::ANIMATION_DURATION + std::time::Duration::from_millis(50), + ); + self.render(cx); + } +} diff --git a/crates/hubs/Cargo.toml b/crates/hubs/Cargo.toml new file mode 100644 index 00000000..c9200f41 --- /dev/null +++ b/crates/hubs/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "nocterm-hubs" +description = "The hubs this device knows: connection status shared by every window, and sign-in." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +nocterm-hubs-model.workspace = true +nocterm-session.workspace = true +nocterm-store.workspace = true + +gpui.workspace = true +futures.workspace = true +thiserror.workspace = true +tracing.workspace = true + +[dev-dependencies] +nocterm-store = { workspace = true, features = ["test-support"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-core.workspace = true +nocterm-settings.workspace = true +gpui = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hubs/src/error.rs b/crates/hubs/src/error.rs new file mode 100644 index 00000000..9472a62e --- /dev/null +++ b/crates/hubs/src/error.rs @@ -0,0 +1,37 @@ +//! Typed failures of signing in to a hub and of managing known hubs. + +use nocterm_hubs_model::{FingerprintError, HubAddressError, HubError, HubId}; +use nocterm_store::StoreError; + +/// Why signing in to a hub did not complete. +#[derive(Clone, Debug, thiserror::Error)] +pub enum SignInError { + #[error("the server address is not valid")] + Address(#[from] HubAddressError), + #[error("the fingerprint is not valid")] + Fingerprint(#[from] FingerprintError), + #[error("the hub could not be used")] + Hub(#[from] HubError), + /// The address is a server that is listed already. Signing in again to it goes through its + /// row, and a server whose certificate changed has to be removed first. + #[error("the server {0} is listed already")] + AlreadyKnown(HubId), + /// The hub has no owner yet; this client cannot set it up. + #[error("the hub is not set up yet")] + NotInitialized, + /// The sign-in worked but the server could not be added to the settings, so the + /// credentials of this device were erased again. + #[error("the server could not be added to the settings")] + Save(#[source] StoreError), +} + +/// Why forgetting or signing out of a known hub failed. +#[derive(Clone, Debug, thiserror::Error)] +pub enum HubsError { + #[error("no such hub: {0}")] + Unknown(HubId), + #[error("the hub could not be used")] + Hub(#[from] HubError), + #[error("the settings could not be saved")] + Save(#[source] StoreError), +} diff --git a/crates/hubs/src/fixture.rs b/crates/hubs/src/fixture.rs new file mode 100644 index 00000000..5b58340f --- /dev/null +++ b/crates/hubs/src/fixture.rs @@ -0,0 +1,120 @@ +//! A registry over a [`FakeHub`] and in-memory settings, shared by the tests of this crate. + +use std::sync::Arc; + +use gpui::{Entity, TestAppContext}; +use nocterm_hubs_model::testing::{FakeHub, FakeServer}; +use nocterm_hubs_model::{HubAddress, HubId, HubRecord, HubsSettings}; +use nocterm_settings::SettingsDocument; +use nocterm_store::{SettingsConfig, SettingsHandle, StoreError}; + +use crate::{Deps, Hubs, install}; + +pub(crate) const ADDRESS: &str = "hub.home:7443"; + +pub(crate) struct Fixture { + pub hub: FakeHub, + pub hubs: Entity, + pub settings: SettingsHandle, +} + +pub(crate) fn address() -> HubAddress { + HubAddress::parse(ADDRESS).unwrap() +} + +pub(crate) fn id() -> HubId { + HubId::from(address()) +} + +/// The server most tests talk to: certificate `[7; 32]`, user `ada`. +pub(crate) fn server() -> FakeServer { + FakeServer::new(ADDRESS, 7) + .named("Home hub") + .with_user("ada", "secret") + .with_provider("corp", "Corporate SSO") +} + +pub(crate) fn pinned_record() -> HubRecord { + server().pinned_record("Home") +} + +/// Installs the registry over `hub` with `known` servers in the settings. +pub(crate) fn fixture(cx: &mut TestAppContext, hub: FakeHub, known: Vec) -> Fixture { + build(cx, hub, known, None) +} + +/// As [`fixture`], but every write of the settings fails. +pub(crate) fn fixture_with_failing_settings(cx: &mut TestAppContext, hub: FakeHub) -> Fixture { + fixture_with_known_failing_settings(cx, hub, Vec::new()) +} + +/// As [`fixture_with_failing_settings`], with `known` servers already listed. +pub(crate) fn fixture_with_known_failing_settings( + cx: &mut TestAppContext, + hub: FakeHub, + known: Vec, +) -> Fixture { + let writer: nocterm_store::SnapshotWriter = Arc::new(|_| { + Box::pin(async { + Err(StoreError::backend(std::io::Error::from( + std::io::ErrorKind::PermissionDenied, + ))) + }) + }); + build(cx, hub, known, Some(writer)) +} + +/// Installs the ordinary shared snapshot writer supplied by a behavior test. +pub(crate) fn fixture_with_writer( + cx: &mut TestAppContext, + hub: FakeHub, + known: Vec, + writer: nocterm_store::SnapshotWriter, +) -> Fixture { + build(cx, hub, known, Some(writer)) +} + +fn build( + cx: &mut TestAppContext, + hub: FakeHub, + known: Vec, + writer: Option>, +) -> Fixture { + cx.update(|cx| { + let document = SettingsDocument::default(); + let config = match writer { + Some(writer) => SettingsConfig::with_writer(document, writer), + None => SettingsConfig::in_memory(document), + }; + let settings = config + .with(HubsSettings { + servers: known, + ..HubsSettings::default() + }) + .install(cx); + let deps = Deps { + trust: Arc::new(hub.clone()), + sign_in: Arc::new(hub.clone()), + devices: Arc::new(nocterm_hubs_model::testing::FakeAccountDevices::default()), + account_keys: Arc::new(nocterm_hubs_model::testing::FakeAccountKeys::alongside( + &hub, "password", + )), + }; + let installed = install(deps, settings.clone(), cx); + Fixture { + hub, + hubs: installed.hubs().clone(), + settings, + } + }) +} + +impl Fixture { + pub(crate) fn status(&self, cx: &mut TestAppContext) -> Option { + self.hubs.read_with(cx, |hubs, _| hubs.status(&id())) + } + + pub(crate) fn servers(&self, cx: &mut TestAppContext) -> Vec { + cx.update(|cx| self.settings.setting::(cx).servers.clone()) + } +} diff --git a/crates/hubs/src/installed.rs b/crates/hubs/src/installed.rs new file mode 100644 index 00000000..bab8f863 --- /dev/null +++ b/crates/hubs/src/installed.rs @@ -0,0 +1,37 @@ +//! The ports the service needs and the handle to the installed registry. + +use std::sync::Arc; + +use gpui::{App, AppContext as _, Entity}; +use nocterm_hubs_model::{AccountDevices, AccountKeyLock, HubSignIn, HubTrust}; +use nocterm_store::SettingsHandle; + +use crate::registry::Hubs; + +/// The hub access ports, supplied by the composition root. +#[derive(Clone)] +pub struct Deps { + pub trust: Arc, + pub sign_in: Arc, + pub devices: Arc, + pub account_keys: Arc, +} + +/// The registry of known hubs, kept alive by whoever composes the application. +#[derive(Clone)] +pub struct Installed { + hubs: Entity, +} + +impl Installed { + pub fn hubs(&self) -> &Entity { + &self.hubs + } +} + +/// Registers the `[hubs]` settings section and starts connecting to every known hub. +pub fn install(deps: Deps, settings: SettingsHandle, cx: &mut App) -> Installed { + Installed { + hubs: cx.new(|cx| Hubs::new(deps, settings, cx)), + } +} diff --git a/crates/hubs/src/lib.rs b/crates/hubs/src/lib.rs new file mode 100644 index 00000000..3bc88c2e --- /dev/null +++ b/crates/hubs/src/lib.rs @@ -0,0 +1,39 @@ +//! The hubs this device knows: the status of each and sign-in. +//! +//! Owns: the registry of known hubs ([`Hubs`]) and the connection status every window shares +//! ([`HubStatus`]); the order of the sign-in steps ([`SignInSession`]); publishing a signed-in +//! server to `[hubs]`, and renaming, colouring, signing out of and removing a known one. +//! +//! Does not own: any I/O or protocol (the injected hub ports do, see `nocterm-hubs-model`); +//! the commands and dialog that show this state (`nocterm-hubs-ui`). +//! +//! Ports used: [`nocterm_hubs_model::HubTrust`], [`nocterm_hubs_model::HubSignIn`], +//! [`nocterm_hubs_model::AccountDevices`] (the devices of the account, listed and revoked +//! through [`Hubs`]) and [`nocterm_hubs_model::AccountKeyLock`] (the account keys, opened and +//! closed through [`Hubs`]), passed in [`Deps`]; `nocterm_store::SettingsHandle`. +//! Provides: [`Installed`] with the [`Hubs`] entity. +//! +//! Settings: the `[hubs]` section ([`nocterm_hubs_model::HubsSettings`]), registered by +//! [`install`]. +//! +//! Globals: none. +//! +//! Invariants: a certificate that is not the trusted one is never retried and never +//! accepted; an unreachable hub is retried with growing delays; a server enters the settings +//! displayed recovery offers pass unchanged to their original capabilities; +//! only after a sign-in worked, in one edit, and credentials made for a server that could not +//! be saved are erased again; every window shares one status and one connection per hub. + +mod error; +#[cfg(test)] +mod fixture; +mod installed; +mod registry; +mod sign_in; +mod status; + +pub use error::{HubsError, SignInError}; +pub use installed::{Deps, Installed, install}; +pub use registry::{HubSummary, Hubs, HubsEvent}; +pub use sign_in::{SignInEvent, SignInSession, SignInStep, TrustNote}; +pub use status::HubStatus; diff --git a/crates/hubs/src/registry.rs b/crates/hubs/src/registry.rs new file mode 100644 index 00000000..913d0b5c --- /dev/null +++ b/crates/hubs/src/registry.rs @@ -0,0 +1,454 @@ +//! Every hub this device knows, with the connection status every window shares. + +use std::sync::Arc; + +use gpui::{AppContext as _, Context, Entity, EventEmitter, Subscription, Task}; +use nocterm_hubs_model::{ + AccountKeyFuture, DeviceSummary, HubAddress, HubColor, HubEndpointSpec, HubError, HubFuture, + HubId, HubInfo, HubRecord, HubsSettings, +}; +use nocterm_session::Secret; +use nocterm_store::SettingsHandle; + +use crate::error::HubsError; +use crate::installed::Deps; +use crate::sign_in::{SignInEvent, SignInSession}; +use crate::status::{HubStatus, Settled, retry_delay, settle_connect}; + +/// What changes in the registry. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HubsEvent { + /// A hub was added or removed, or a status changed. + Changed, +} + +/// A known hub as a menu shows it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubSummary { + pub id: HubId, + pub label: String, + pub address: HubAddress, + pub color: Option, + pub status: HubStatus, +} + +struct HubEntry { + record: HubRecord, + status: HubStatus, + /// Unsuccessful attempts to reach the hub in a row. + failures: u32, + /// The connection being made, or the wait before the next attempt. + attempt: Option>, +} + +/// The known hubs, in the order of the settings, and the status of each. +pub struct Hubs { + settings: SettingsHandle, + deps: Deps, + entries: Vec, + _settings: Subscription, + _keys: Task<()>, +} + +impl EventEmitter for Hubs {} + +impl Hubs { + pub(crate) fn new(deps: Deps, settings: SettingsHandle, cx: &mut Context) -> Self { + settings.register::(cx); + let weak = cx.weak_entity(); + let observed = settings.observe(cx, move |cx| { + weak.update(cx, |hubs, cx| hubs.sync(cx)).ok(); + }); + let keys = Self::redraw_on_key_changes(&deps, cx); + let mut hubs = Self { + settings, + deps, + entries: Vec::new(), + _settings: observed, + _keys: keys, + }; + hubs.sync(cx); + hubs + } + + /// Tells the observers of the registry whenever account keys open or close, so a view of + /// what depends on them (the offer of a new recovery key) follows a vault unlock at once. + fn redraw_on_key_changes(deps: &Deps, cx: &mut Context) -> Task<()> { + let sign_in = Arc::clone(&deps.sign_in); + let mut seen = sign_in.keys_revision(); + cx.spawn(async move |this, cx| { + loop { + seen = sign_in.keys_changed(seen).await; + if this.update(cx, |_, cx| cx.notify()).is_err() { + break; + } + } + }) + } + + /// The known hubs in the order of the settings. + pub fn list(&self) -> Vec { + self.entries + .iter() + .map(|entry| HubSummary { + id: entry.record.id(), + label: entry.record.display_label(), + address: entry.record.address.clone(), + color: entry.record.color, + status: entry.status.clone(), + }) + .collect() + } + + pub fn summary(&self, hub: &HubId) -> Option { + self.entry(hub).map(|entry| HubSummary { + id: entry.record.id(), + label: entry.record.display_label(), + address: entry.record.address.clone(), + color: entry.record.color, + status: entry.status.clone(), + }) + } + + pub fn status(&self, hub: &HubId) -> Option { + self.entry(hub).map(|entry| entry.status.clone()) + } + + pub fn record(&self, hub: &HubId) -> Option<&HubRecord> { + self.entry(hub).map(|entry| &entry.record) + } + + fn entry(&self, hub: &HubId) -> Option<&HubEntry> { + self.entries.iter().find(|entry| entry.record.id() == *hub) + } + + fn entry_mut(&mut self, hub: &HubId) -> Option<&mut HubEntry> { + self.entries + .iter_mut() + .find(|entry| entry.record.id() == *hub) + } + + /// Brings the entries in line with the published settings: new servers start + /// connecting, removed ones disappear, a changed trust connects again. + fn sync(&mut self, cx: &mut Context) { + let records = &self.settings.setting::(cx).servers; + if records + .iter() + .eq(self.entries.iter().map(|entry| &entry.record)) + { + return; + } + let records = records.clone(); + let mut previous = std::mem::take(&mut self.entries); + let mut reconnect = Vec::new(); + for record in records { + let id = record.id(); + let kept = previous.iter().position(|entry| entry.record.id() == id); + let entry = match kept { + Some(index) => { + let mut entry = previous.remove(index); + if entry.record.trust != record.trust { + reconnect.push(id); + } + entry.record = record; + entry + } + None => { + reconnect.push(id); + HubEntry { + record, + status: HubStatus::Connecting, + failures: 0, + attempt: None, + } + } + }; + self.entries.push(entry); + } + for hub in reconnect { + self.connect(&hub, cx); + } + cx.emit(HubsEvent::Changed); + cx.notify(); + } + + /// Connects to a hub with the trust the settings state. + fn connect(&mut self, hub: &HubId, cx: &mut Context) { + let Some(index) = self.entries.iter().position(|e| e.record.id() == *hub) else { + return; + }; + let entry = &mut self.entries[index]; + entry.status = HubStatus::Connecting; + let connecting = self.deps.sign_in.connect(HubEndpointSpec { + address: entry.record.address.clone(), + trust: entry.record.trust, + }); + let hub = hub.clone(); + entry.attempt = Some(cx.spawn(async move |this, cx| { + let result = connecting.await; + this.update(cx, |hubs, cx| hubs.settled(&hub, result, cx)) + .ok(); + })); + cx.emit(HubsEvent::Changed); + cx.notify(); + } + + fn settled(&mut self, hub: &HubId, result: Result, cx: &mut Context) { + let settled = settle_connect(&result); + self.rest(hub, settled, cx); + } + + /// Records where a hub ended up and, for a hub that did not answer, when to try again. + fn rest(&mut self, hub: &HubId, settled: Settled, cx: &mut Context) { + let now = cx.background_executor().now(); + let Some(entry) = self.entry_mut(hub) else { + return; + }; + entry.attempt = None; + let mut retry = None; + match settled { + Settled::Status(status) => { + entry.failures = 0; + entry.status = status; + } + Settled::Retry => { + entry.failures += 1; + let delay = retry_delay(entry.failures); + entry.status = HubStatus::Offline { + retry_at: now + delay, + }; + retry = Some(delay); + } + } + if let Some(delay) = retry { + let again = hub.clone(); + let waiting = cx.spawn(async move |this, cx| { + cx.background_executor().timer(delay).await; + this.update(cx, |hubs, cx| hubs.connect(&again, cx)).ok(); + }); + if let Some(entry) = self.entry_mut(hub) { + entry.attempt = Some(waiting); + } + } + cx.emit(HubsEvent::Changed); + cx.notify(); + } + + /// Tries a hub again now, instead of at the scheduled time. + pub fn retry(&mut self, hub: &HubId, cx: &mut Context) { + if self + .status(hub) + .is_some_and(|status| !matches!(status, HubStatus::Connecting | HubStatus::Online)) + { + self.connect(hub, cx); + } + } + + /// Starts a sign-in to a server that is not known yet. + pub fn start_sign_in(&mut self, cx: &mut Context) -> Entity { + let (deps, settings) = (self.deps.clone(), self.settings.clone()); + let session = cx.new(|_| SignInSession::new(&deps, settings)); + self.connect_when_signed_in(&session, cx); + session + } + + /// A finished sign-in connects the hub again, so a hub that was signed out or revoked + /// comes back without a change of the settings. A new server is already connecting. + fn connect_when_signed_in(&self, session: &Entity, cx: &mut Context) { + cx.subscribe(session, |hubs, _, event, cx| { + if let SignInEvent::Finished(hub) = event + && hubs.status(hub).is_some_and(|s| s != HubStatus::Connecting) + { + hubs.connect(hub, cx); + } + }) + .detach(); + } + + /// Starts signing in again to a known hub, with the trust its record states. + pub fn start_sign_in_for( + &mut self, + hub: &HubId, + cx: &mut Context, + ) -> Option> { + let record = self.record(hub)?.clone(); + let (deps, settings) = (self.deps.clone(), self.settings.clone()); + let session = cx.new(|cx| SignInSession::resume(&deps, settings, record, cx)); + self.connect_when_signed_in(&session, cx); + Some(session) + } + + /// Whether the account keys of `hub` are open and the recovery key made with them was never + /// confirmed as saved: a new one can be issued with [`Hubs::start_recovery_key_reissue`]. + pub fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + self.entry(hub).is_some() && self.deps.sign_in.recovery_key_unconfirmed(hub) + } + + /// Captures the original recipient of a displayed recovery action. + pub fn recovery_key_offer( + &self, + hub: &HubId, + ) -> Result { + if self.entry(hub).is_none() { + return Err(nocterm_hubs_model::HubError::Stale); + } + self.deps.sign_in.recovery_key_offer(hub) + } + + /// Starts issuing a new recovery key for `hub`, which replaces the unconfirmed one. The + /// dialog shows it once; `None` when `hub` is not listed. + pub fn start_recovery_key_reissue( + &mut self, + hub: &HubId, + offer: nocterm_hubs_model::RecoveryKeyReissue, + cx: &mut Context, + ) -> Option> { + self.entry(hub)?; + if offer.hub() != hub || offer.check_current().is_err() { + return None; + } + let (deps, settings) = (self.deps.clone(), self.settings.clone()); + let session = cx.new(|cx| SignInSession::reissue(&deps, settings, hub, offer, cx)); + // Saving the new key ends the offer: whoever shows it is told to look again. + cx.subscribe(&session, |_, _, event, cx| { + if matches!(event, SignInEvent::Finished(_)) { + cx.notify(); + } + }) + .detach(); + Some(session) + } + + /// The devices of the account on `hub`. The call captures the original membership before it + /// returns, so a result never belongs to an account that replaced it; a hub that is not + /// listed is [`HubError::Stale`]. + pub fn list_devices(&self, hub: &HubId) -> HubFuture> { + if self.entry(hub).is_none() { + return Box::pin(async { Err(HubError::Stale) }); + } + self.deps.devices.list(hub) + } + + /// Revokes `device` of the account on `hub`. Revoking this device is [`Hubs::sign_out`]. + pub fn revoke_device(&self, hub: &HubId, device: &str) -> HubFuture<()> { + if self.entry(hub).is_none() { + return Box::pin(async { Err(HubError::Stale) }); + } + self.deps.devices.revoke(hub, device) + } + + /// Whether the keys of the account on `hub` are open on this device. + pub fn account_keys_open(&self, hub: &HubId) -> bool { + self.deps.account_keys.is_open(hub) + } + + /// Opens the keys of the account on `hub` with `password`. + pub fn unlock_account_keys(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()> { + self.deps.account_keys.unlock(hub, password) + } + + /// Closes the keys of the account on `hub`. + pub fn lock_account_keys(&self, hub: &HubId) { + self.deps.account_keys.lock(hub); + } + + /// Ends this device's session on the hub and erases its credentials. The server stays + /// in the list, signed out. + pub fn sign_out(&mut self, hub: &HubId, cx: &mut Context) -> Task> { + if self.entry(hub).is_none() { + return Task::ready(Err(HubsError::Unknown(hub.clone()))); + } + let signing_out = self.deps.sign_in.sign_out(hub); + let hub = hub.clone(); + cx.spawn(async move |this, cx| { + let result = signing_out.await; + this.update(cx, |hubs, cx| { + if result.is_ok() { + hubs.rest(&hub, Settled::Status(HubStatus::SignedOut), cx); + } + }) + .ok(); + result.map_err(HubsError::from) + }) + } + + /// The settings the registry edits, for a text field that saves a server's name through + /// them in the same one-edit way as [`Hubs::set_label`]. + pub fn settings(&self) -> SettingsHandle { + self.settings.clone() + } + + /// Renames a server in the settings, in one edit. An empty name shows the address. + pub fn set_label( + &mut self, + hub: &HubId, + label: &str, + cx: &mut Context, + ) -> Task> { + let label = label.to_owned(); + self.edit_record(hub, cx, move |settings, hub| { + settings.set_label(hub, &label); + }) + } + + /// Marks a server with a colour, or clears the mark, in one edit of the settings. + pub fn set_color( + &mut self, + hub: &HubId, + color: Option, + cx: &mut Context, + ) -> Task> { + self.edit_record(hub, cx, move |settings, hub| { + settings.set_color(hub, color); + }) + } + + fn edit_record( + &mut self, + hub: &HubId, + cx: &mut Context, + edit: impl FnOnce(&mut HubsSettings, &HubId) + 'static, + ) -> Task> { + if self.entry(hub).is_none() { + return Task::ready(Err(HubsError::Unknown(hub.clone()))); + } + let id = hub.clone(); + let saving = self + .settings + .update_checked::(cx, move |settings| { + edit(settings, &id); + Ok(()) + }); + cx.spawn(async move |_, _| saving.await.map(|_| ()).map_err(HubsError::Save)) + } + + /// Removes a server from the settings and erases the credentials kept for it. When the + /// settings cannot be saved the server stays listed without credentials, and the hub + /// connects again to show where it stands. + pub fn remove(&mut self, hub: &HubId, cx: &mut Context) -> Task> { + if self.entry(hub).is_none() { + return Task::ready(Err(HubsError::Unknown(hub.clone()))); + } + if let Err(error) = self.deps.sign_in.forget(hub) { + return Task::ready(Err(error.into())); + } + let id = hub.clone(); + let removed = hub.clone(); + let saving = self + .settings + .update_checked::(cx, move |settings| { + settings.remove(&removed); + Ok(()) + }); + cx.spawn(async move |this, cx| { + let saved = saving.await; + if saved.is_err() { + this.update(cx, |hubs, cx| hubs.connect(&id, cx)).ok(); + } + saved.map(|_| ()).map_err(HubsError::Save) + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs/src/registry/tests.rs b/crates/hubs/src/registry/tests.rs new file mode 100644 index 00000000..5a26cd91 --- /dev/null +++ b/crates/hubs/src/registry/tests.rs @@ -0,0 +1,351 @@ +use std::time::Duration; + +use gpui::TestAppContext; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp}; +use nocterm_hubs_model::{Fingerprint, HubColor, HubError, HubRecord, TrustPolicy}; + +use crate::fixture::{ + address, fixture, fixture_with_known_failing_settings, id, pinned_record, server, +}; +use crate::{HubStatus, HubsError}; + +fn signed_in_hub() -> FakeHub { + let hub = FakeHub::new().with_server(server()); + hub.sign_in_as(&id(), "ada"); + hub +} + +#[gpui::test] +async fn a_known_hub_connects_and_goes_online(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + assert_eq!(fx.status(cx), Some(HubStatus::Connecting)); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); +} + +#[gpui::test] +async fn a_hub_without_credentials_is_signed_out(cx: &mut TestAppContext) { + let hub = FakeHub::new().with_server(server()); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::SignedOut)); +} + +#[gpui::test] +async fn an_unreachable_hub_is_retried_with_growing_delays_until_it_answers( + cx: &mut TestAppContext, +) { + let hub = signed_in_hub(); + hub.edit_server(&address(), |server| server.online = false); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert!(matches!(fx.status(cx), Some(HubStatus::Offline { .. }))); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); + + cx.executor().advance_clock(Duration::from_secs(2)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 2); + + // The second wait is five seconds, so two more do not reach it. + cx.executor().advance_clock(Duration::from_secs(2)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 2); + + fx.hub + .edit_server(&address(), |server| server.online = true); + cx.executor().advance_clock(Duration::from_secs(3)); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn a_changed_certificate_is_a_trust_failure_that_is_never_retried(cx: &mut TestAppContext) { + let hub = signed_in_hub(); + hub.edit_server(&address(), |server| { + server.certificate = Fingerprint::from_bytes([9; 32]); + }); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!( + fx.status(cx), + Some(HubStatus::TrustFailed { + pinned: Some(Fingerprint::from_bytes([7; 32])), + presented: Fingerprint::from_bytes([9; 32]), + }) + ); + cx.executor().advance_clock(Duration::from_secs(300)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); +} + +#[gpui::test] +async fn a_revoked_device_loses_its_credentials_and_is_not_retried(cx: &mut TestAppContext) { + let hub = signed_in_hub(); + hub.revoke_device(&id()); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Revoked)); + assert!(!fx.hub.is_signed_in(&id())); + cx.executor().advance_clock(Duration::from_secs(300)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); +} + +#[gpui::test] +async fn a_hub_with_another_protocol_asks_for_an_upgrade(cx: &mut TestAppContext) { + let hub = signed_in_hub(); + hub.edit_server(&address(), |server| server.protocol = 2); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!( + fx.status(cx), + Some(HubStatus::UpgradeRequired { hub: 2, client: 1 }) + ); +} + +#[gpui::test] +async fn an_unknown_hub_has_no_status_or_removal(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), Vec::new()); + assert_eq!(fx.status(cx), None); + let removed = fx.hubs.update(cx, |hubs, cx| hubs.remove(&id(), cx)); + assert!(removed.await.is_err()); +} + +#[gpui::test] +async fn a_server_added_to_the_settings_starts_connecting(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), Vec::new()); + cx.update(|cx| { + fx.settings + .update::(cx, |settings| { + settings.upsert(pinned_record()); + }) + }) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn removing_a_server_forgets_its_credentials_and_drops_it_from_the_list( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.remove(&id(), cx)) + .await + .unwrap(); + cx.run_until_parked(); + assert!(fx.servers(cx).is_empty()); + assert_eq!(fx.status(cx), None); + assert!(fx.hub.calls().contains(&FakeCall::Forget(id()))); + assert!(!fx.hub.is_signed_in(&id())); +} + +#[gpui::test] +async fn signing_out_keeps_the_server_listed_but_signed_out(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.sign_out(&id(), cx)) + .await + .unwrap(); + assert_eq!(fx.status(cx), Some(HubStatus::SignedOut)); + assert_eq!(fx.servers(cx).len(), 1); + assert!(!fx.hub.is_signed_in(&id())); +} + +#[gpui::test] +async fn a_changed_trust_in_the_settings_connects_again(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let changed = HubRecord::new(address(), TrustPolicy::PublicCa, "Home"); + cx.update(|cx| { + fx.settings + .update::(cx, move |s| s.upsert(changed)) + }) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 2); + // The fake server is not publicly trusted, so the new trust fails and stays failed. + assert!(matches!( + fx.status(cx), + Some(HubStatus::TrustFailed { pinned: None, .. }) + )); +} + +fn revision(fx: &crate::fixture::Fixture, cx: &mut TestAppContext) -> u64 { + cx.read(|cx| fx.settings.revision(cx)) +} + +#[gpui::test] +async fn renaming_a_server_is_one_settings_edit_and_shows_in_the_list(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let before = revision(&fx, cx); + + fx.hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), " Office\n", cx)) + .await + .unwrap(); + cx.run_until_parked(); + + assert_eq!(revision(&fx, cx), before + 1); + assert_eq!(fx.servers(cx)[0].label, "Office"); + let label = fx + .hubs + .read_with(cx, |hubs, _| hubs.summary(&id()).unwrap().label); + assert_eq!(label, "Office"); + assert_eq!( + fx.hub.count(FakeOp::Connect), + 1, + "no new connection for a name" + ); +} + +#[gpui::test] +async fn an_empty_name_shows_the_address_again(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), " ", cx)) + .await + .unwrap(); + cx.run_until_parked(); + let label = fx + .hubs + .read_with(cx, |hubs, _| hubs.summary(&id()).unwrap().label); + assert_eq!(label, address().to_string()); +} + +#[gpui::test] +async fn colouring_a_server_is_one_settings_edit_and_can_be_undone(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let before = revision(&fx, cx); + + fx.hubs + .update(cx, |hubs, cx| { + hubs.set_color(&id(), Some(HubColor::Green), cx) + }) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(revision(&fx, cx), before + 1); + let color = fx + .hubs + .read_with(cx, |hubs, _| hubs.summary(&id()).unwrap().color); + assert_eq!(color, Some(HubColor::Green)); + assert_eq!(fx.status(cx), Some(HubStatus::Online), "still connected"); + + fx.hubs + .update(cx, |hubs, cx| hubs.set_color(&id(), None, cx)) + .await + .unwrap(); + assert_eq!(fx.servers(cx)[0].color, None); +} + +#[gpui::test] +async fn editing_an_unknown_server_fails_and_changes_nothing(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), Vec::new()); + let before = revision(&fx, cx); + let renamed = fx + .hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), "x", cx)); + let colored = fx.hubs.update(cx, |hubs, cx| { + hubs.set_color(&id(), Some(HubColor::Red), cx) + }); + assert!(matches!(renamed.await, Err(HubsError::Unknown(_)))); + assert!(matches!(colored.await, Err(HubsError::Unknown(_)))); + assert_eq!(revision(&fx, cx), before); +} + +#[gpui::test] +async fn a_failed_save_leaves_the_name_and_colour_as_they_were(cx: &mut TestAppContext) { + let fx = fixture_with_known_failing_settings(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let renamed = fx + .hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), "Office", cx)); + assert!(matches!(renamed.await, Err(HubsError::Save(_)))); + let colored = fx.hubs.update(cx, |hubs, cx| { + hubs.set_color(&id(), Some(HubColor::Cyan), cx) + }); + assert!(matches!(colored.await, Err(HubsError::Save(_)))); + cx.run_until_parked(); + assert_eq!(fx.servers(cx), vec![pinned_record()]); +} + +#[gpui::test] +async fn signing_out_erases_the_credentials_and_keeps_name_and_colour(cx: &mut TestAppContext) { + let marked = pinned_record().with_color(Some(HubColor::Magenta)); + let fx = fixture(cx, signed_in_hub(), vec![marked.clone()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.sign_out(&id(), cx)) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::SignedOut)); + assert_eq!(fx.servers(cx), vec![marked]); + assert!(fx.hub.calls().contains(&FakeCall::SignOut(id()))); + assert!(!fx.hub.is_signed_in(&id())); +} + +#[gpui::test] +async fn a_server_that_cannot_be_removed_from_the_settings_shows_as_signed_out( + cx: &mut TestAppContext, +) { + let fx = fixture_with_known_failing_settings(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); + + let removed = fx.hubs.update(cx, |hubs, cx| hubs.remove(&id(), cx)); + assert!(matches!(removed.await, Err(HubsError::Save(_)))); + cx.run_until_parked(); + + assert_eq!(fx.servers(cx), vec![pinned_record()], "still listed"); + assert!(!fx.hub.is_signed_in(&id()), "the credentials are gone"); + assert_eq!( + fx.status(cx), + Some(HubStatus::SignedOut), + "the list does not claim a connection that cannot work" + ); +} + +#[gpui::test] +async fn credentials_that_cannot_be_erased_keep_the_server_listed(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hub.fail_next(FakeOp::Forget, HubError::NotSignedIn); + let removed = fx.hubs.update(cx, |hubs, cx| hubs.remove(&id(), cx)); + assert!(matches!(removed.await, Err(HubsError::Hub(_)))); + cx.run_until_parked(); + assert_eq!(fx.servers(cx), vec![pinned_record()]); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn an_unrelated_settings_edit_does_not_publish_a_hub_change(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.update(|cx| fx.settings.register::(cx)); + cx.run_until_parked(); + let redraws = std::rc::Rc::new(std::cell::Cell::new(0)); + let changed = redraws.clone(); + let _subscription = + cx.update(|cx| cx.observe(&fx.hubs, move |_, _| changed.set(changed.get() + 1))); + let update = cx.update(|cx| { + fx.settings + .update::(cx, |settings| { + settings.keepalive_interval_secs = 43 + }) + }); + update.await.unwrap(); + cx.run_until_parked(); + assert_eq!(redraws.get(), 0); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} diff --git a/crates/hubs/src/sign_in.rs b/crates/hubs/src/sign_in.rs new file mode 100644 index 00000000..1db2f499 --- /dev/null +++ b/crates/hubs/src/sign_in.rs @@ -0,0 +1,748 @@ +//! Signing in to a server: address, certificate trust, then a password or the browser. +//! +//! One [`SignInSession`] is one run of the dialog. It owns the order of the steps and +//! publishes the server to the settings in a single edit once the sign-in worked. + +use std::sync::Arc; + +use futures::FutureExt as _; +use gpui::{Context, EventEmitter, Task}; +use nocterm_hubs_model::{ + Fingerprint, HubAddress, HubEndpointSpec, HubError, HubId, HubInfo, HubRecord, HubSignIn, + HubTrust, HubsSettings, KeyStatus, OidcProvider, OidcSignIn, ProbedHub, RecoveryKeyOffer, + RecoveryKeyReissue, SignedIn, TrustDecision, TrustPolicy, decide, +}; +use nocterm_session::Secret; +use nocterm_store::SettingsHandle; + +use crate::error::SignInError; +use crate::installed::Deps; + +/// How the certificate of the server came to be trusted. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum TrustNote { + /// It matches the fingerprint the user typed (or the record states). + Pinned, + /// A public certificate authority vouches for it. + PublicCa, + /// The user confirmed its fingerprint on first use. + Confirmed, +} + +/// Where the dialog is. +#[derive(Clone, Debug)] +pub enum SignInStep { + /// Waiting for an address and an optional fingerprint. + Address { + problem: Option, + }, + /// Reading the certificate or connecting. + Checking, + /// The certificate is not publicly trusted and no fingerprint was typed: the user + /// decides whether to trust and pin it. + ConfirmFingerprint { + address: HubAddress, + fingerprint: Fingerprint, + }, + /// The certificate is not the one the user typed. + Mismatch { + typed: Fingerprint, + presented: Fingerprint, + }, + /// The certificate is trusted; waiting for a password or a provider. + Method { + hub_name: String, + password: bool, + providers: Vec, + note: TrustNote, + problem: Option, + }, + /// A password was sent. + Authenticating, + /// Waiting for the user to finish in the browser. + Browser { + provider: String, + /// The address to open; absent until the sign-in listens. + url: Option, + }, + /// The account had no keys and they were made. The recovery key is shown here once and kept + /// nowhere: it is the only way back after the password is lost. + RecoveryKey { + key: RecoveryKeyOffer, + }, + /// The keys are open, but the recovery key shown when they were made was never confirmed as + /// saved (the dialog was closed or the client stopped on that step). A new one can be + /// issued; the old one stays valid until then. + RecoveryOffer { + offer: RecoveryKeyReissue, + problem: Option, + }, + /// Replacing the recovery key. The new key exists on the server before it is shown, so this + /// step cannot be left: stopping now would lose it. + Issuing, + /// A browser sign-in cannot open the account's keys: waiting for the vault passphrase. + Passphrase { + problem: Option, + }, + /// The password does not open the account's keys (it was reset): waiting for the + /// recovery key. + Recovery { + problem: Option, + }, + /// Adding the server to the settings. + Saving, + Finished(HubId), + /// The server could not be added. Final. + Failed(SignInError), +} + +/// What the dialog asks its host to do. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum SignInEvent { + /// Open this address in the system browser. + OpenBrowser(String), + /// The server is signed in to and saved. + Finished(HubId), +} + +/// What is known of the server being signed in to. +#[derive(Default)] +struct Attempt { + address: Option, + /// The fingerprint the user typed. + expected: Option, + trust: Option, + note: Option, + info: Option, + /// The label of a server that is already known. + label: Option, + /// Authority supplied with this successful login, independent of recovery key actions. + login: Option, + /// The address cannot be changed: signing in again to a known server. + fixed: bool, + /// The run only issues a new recovery key for a server that is signed in to and listed. + reissue_only: bool, +} + +/// One run of the sign-in dialog. +pub struct SignInSession { + trust: Arc, + sign_in: Arc, + settings: SettingsHandle, + step: SignInStep, + attempt: Attempt, + /// The call in flight; dropping it cancels a browser sign-in. + work: Option>, +} + +impl EventEmitter for SignInSession {} + +impl SignInSession { + pub(crate) fn new(deps: &Deps, settings: SettingsHandle) -> Self { + Self { + trust: Arc::clone(&deps.trust), + sign_in: Arc::clone(&deps.sign_in), + settings, + step: SignInStep::Address { problem: None }, + attempt: Attempt::default(), + work: None, + } + } + + /// Signing in again to a known server: its trust is decided, so the dialog goes straight + /// to connecting. + pub(crate) fn resume( + deps: &Deps, + settings: SettingsHandle, + record: HubRecord, + cx: &mut Context, + ) -> Self { + let mut session = Self::new(deps, settings); + let note = match record.trust { + TrustPolicy::Pinned(_) => TrustNote::Pinned, + TrustPolicy::PublicCa => TrustNote::PublicCa, + }; + session.attempt = Attempt { + address: Some(record.address.clone()), + label: Some(record.label.clone()), + fixed: true, + ..Attempt::default() + }; + session.connect(record.trust, note, cx); + session + } + + /// A run that issues a new recovery key for the signed-in server `hub`, without signing in. + pub(crate) fn reissue( + deps: &Deps, + settings: SettingsHandle, + hub: &HubId, + offer: RecoveryKeyReissue, + cx: &mut Context, + ) -> Self { + let mut session = Self::new(deps, settings); + session.attempt = Attempt { + address: Some(hub.address().clone()), + fixed: true, + reissue_only: true, + ..Attempt::default() + }; + session.issue(offer, cx); + session + } + + /// The run only issues a new recovery key: nothing is signed in to or saved. + pub fn reissue_only(&self) -> bool { + self.attempt.reissue_only + } + + pub fn step(&self) -> &SignInStep { + &self.step + } + + /// The server may be edited: the dialog was not opened for a known one. + pub fn address_editable(&self) -> bool { + !self.attempt.fixed + } + + fn move_to(&mut self, step: SignInStep, cx: &mut Context) { + self.step = step; + cx.notify(); + } + + /// Reads the certificate of the typed server. `fingerprint` may be empty. + pub fn submit_address(&mut self, address: &str, fingerprint: &str, cx: &mut Context) { + if !matches!(self.step, SignInStep::Address { .. }) || self.attempt.fixed { + return; + } + let parsed = parse_inputs(address, fingerprint); + let (address, expected) = match parsed { + Ok(parsed) => parsed, + Err(problem) => { + return self.move_to( + SignInStep::Address { + problem: Some(problem), + }, + cx, + ); + } + }; + // A known server keeps the certificate it was pinned with: a new sign-in must not + // replace it, so a changed certificate is dealt with by removing the server first. + let known = HubId::from(address.clone()); + if self + .settings + .setting::(cx) + .find(&known) + .is_some() + { + return self.back_to_address(SignInError::AlreadyKnown(known), cx); + } + self.attempt.address = Some(address.clone()); + self.attempt.expected = expected; + self.move_to(SignInStep::Checking, cx); + let probing = self.trust.probe(&address); + self.work = Some(cx.spawn(async move |this, cx| { + let probed = probing.await; + this.update(cx, |session, cx| session.probed(probed, cx)) + .ok(); + })); + } + + fn probed(&mut self, probed: Result, cx: &mut Context) { + self.work = None; + let probe = match probed { + Ok(probe) => probe, + Err(error) => return self.back_to_address(error.into(), cx), + }; + let decision = decide(&probe, self.attempt.expected.as_ref()); + match decision { + TrustDecision::Pinned(_) | TrustDecision::PublicCa => { + let note = if matches!(decision, TrustDecision::PublicCa) { + TrustNote::PublicCa + } else { + TrustNote::Pinned + }; + if let Some(trust) = decision.settled() { + self.connect(trust, note, cx); + } + } + TrustDecision::ConfirmFirstUse(fingerprint) => { + if let Some(address) = self.attempt.address.clone() { + self.move_to( + SignInStep::ConfirmFingerprint { + address, + fingerprint, + }, + cx, + ); + } + } + TrustDecision::Mismatch { typed, presented } => { + self.move_to(SignInStep::Mismatch { typed, presented }, cx); + } + } + } + + /// The user trusts the certificate that was shown and wants it pinned. + pub fn confirm_fingerprint(&mut self, cx: &mut Context) { + let SignInStep::ConfirmFingerprint { fingerprint, .. } = self.step else { + return; + }; + self.connect(TrustPolicy::Pinned(fingerprint), TrustNote::Confirmed, cx); + } + + fn back_to_address(&mut self, problem: SignInError, cx: &mut Context) { + self.move_to( + SignInStep::Address { + problem: Some(problem), + }, + cx, + ); + } + + /// Connects with the decided trust and learns which ways of signing in the server offers. + fn connect(&mut self, trust: TrustPolicy, note: TrustNote, cx: &mut Context) { + let Some(address) = self.attempt.address.clone() else { + return; + }; + self.attempt.trust = Some(trust); + self.attempt.note = Some(note); + self.move_to(SignInStep::Checking, cx); + let connecting = self.sign_in.connect(HubEndpointSpec { address, trust }); + self.work = Some(cx.spawn(async move |this, cx| { + let result = connecting.await; + this.update(cx, |session, cx| session.connected(result, cx)) + .ok(); + })); + } + + fn connected(&mut self, result: Result, cx: &mut Context) { + self.work = None; + let info = match result { + Ok(info) => info, + Err(error) => return self.back_to_address(error.into(), cx), + }; + if !info.bootstrapped { + return self.back_to_address(SignInError::NotInitialized, cx); + } + let signed_in = info.signed_in; + self.attempt.info = Some(info); + if signed_in { + // Keys that are open already, from a run of this dialog that was closed on the + // recovery key, still wait for it to be confirmed. + let unconfirmed = self + .hub() + .is_some_and(|hub| self.sign_in.recovery_key_unconfirmed(&hub)); + if unconfirmed { + let offer = self + .hub() + .ok_or(HubError::Stale) + .and_then(|hub| self.sign_in.recovery_key_offer(&hub)); + match offer { + Ok(offer) => self.move_to( + SignInStep::RecoveryOffer { + offer, + problem: None, + }, + cx, + ), + Err(error) => self.move_to(SignInStep::Failed(error.into()), cx), + } + } else { + self.publish(cx); + } + } else { + self.show_methods(None, cx); + } + } + + fn show_methods(&mut self, problem: Option, cx: &mut Context) { + let Some(info) = &self.attempt.info else { + return; + }; + let step = SignInStep::Method { + hub_name: info.name.clone(), + password: info.password_login, + providers: info.oidc_providers.clone(), + note: self.attempt.note.unwrap_or(TrustNote::Pinned), + problem, + }; + self.move_to(step, cx); + } + + fn endpoint(&self) -> Option { + Some(HubEndpointSpec { + address: self.attempt.address.clone()?, + trust: self.attempt.trust?, + }) + } + + /// Signs in with a user name and password. + pub fn sign_in_with_password( + &mut self, + username: &str, + password: Secret, + cx: &mut Context, + ) { + let (SignInStep::Method { .. }, Some(endpoint)) = (&self.step, self.endpoint()) else { + return; + }; + self.move_to(SignInStep::Authenticating, cx); + let signing_in = self + .sign_in + .password(endpoint, username.trim().to_owned(), password); + self.work = Some(cx.spawn(async move |this, cx| { + let result = signing_in.await; + this.update(cx, |session, cx| session.signed(result, cx)) + .ok(); + })); + } + + /// Signs in through the identity provider `provider` in the system browser. + pub fn sign_in_with_provider(&mut self, provider: &str, cx: &mut Context) { + let (SignInStep::Method { .. }, Some(endpoint)) = (&self.step, self.endpoint()) else { + return; + }; + self.move_to( + SignInStep::Browser { + provider: provider.to_owned(), + url: None, + }, + cx, + ); + let OidcSignIn { browser, done } = self.sign_in.oidc(endpoint, provider.to_owned()); + self.work = Some(cx.spawn(async move |this, cx| { + let result = await_browser_sign_in(browser, done, |url| { + this.update(cx, |session, cx| session.browser_ready(url, cx)) + .ok(); + }) + .await; + this.update(cx, |session, cx| session.signed(result, cx)) + .ok(); + })); + } + + fn browser_ready(&mut self, url: String, cx: &mut Context) { + if let SignInStep::Browser { url: shown, .. } = &mut self.step { + *shown = Some(url.clone()); + cx.emit(SignInEvent::OpenBrowser(url)); + cx.notify(); + } + } + + fn signed(&mut self, result: Result, cx: &mut Context) { + self.work = None; + match result { + Ok(signed) => { + self.attempt.login = Some(signed.login); + self.keys_settled(signed.keys, cx); + } + Err(error) => self.show_methods(Some(error.into()), cx), + } + } + + /// Goes on from what a sign-in (or a passphrase) did to the account's keys. + fn keys_settled(&mut self, keys: KeyStatus, cx: &mut Context) { + let displayed = match &keys { + KeyStatus::Created(key) => Some(key.hub()), + KeyStatus::RecoveryKeyUnconfirmed(offer) => Some(offer.hub()), + _ => None, + }; + if displayed.is_some_and(|hub| Some(hub) != self.hub().as_ref()) { + return self.move_to(SignInStep::Failed(HubError::Stale.into()), cx); + } + match keys { + KeyStatus::Open => self.publish(cx), + KeyStatus::RecoveryKeyUnconfirmed(offer) => self.move_to( + SignInStep::RecoveryOffer { + offer, + problem: None, + }, + cx, + ), + KeyStatus::Created(key) => self.move_to(SignInStep::RecoveryKey { key }, cx), + KeyStatus::PassphraseNeeded => { + self.move_to(SignInStep::Passphrase { problem: None }, cx) + } + KeyStatus::RecoveryNeeded => self.move_to(SignInStep::Recovery { problem: None }, cx), + } + } + + fn hub(&self) -> Option { + self.attempt.address.clone().map(HubId::from) + } + + /// The user wrote the recovery key down: it is no longer offered again, and the sign-in + /// goes on. + pub fn acknowledge_recovery_key(&mut self, cx: &mut Context) { + let SignInStep::RecoveryKey { key } = &self.step else { + return; + }; + let confirming = key.confirm(); + self.move_to(SignInStep::Saving, cx); + self.work = Some(cx.spawn(async move |this, cx| { + let result = confirming.await; + this.update(cx, |session, cx| session.recovery_acknowledged(result, cx)) + .ok(); + })); + } + + fn recovery_acknowledged(&mut self, result: Result<(), HubError>, cx: &mut Context) { + self.work = None; + if let Err(error) = result { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + let hub = self.hub(); + match hub { + Some(hub) if self.attempt.reissue_only => { + self.move_to(SignInStep::Finished(hub.clone()), cx); + cx.emit(SignInEvent::Finished(hub)); + } + _ => self.publish(cx), + } + } + + /// Issues a new recovery key, because the one made with the keys was never confirmed. + pub fn issue_recovery_key(&mut self, cx: &mut Context) { + let SignInStep::RecoveryOffer { offer, .. } = &self.step else { + return; + }; + self.issue(offer.clone(), cx); + } + + /// Keeps the current recovery envelope without replacing it. + pub fn skip_recovery_offer(&mut self, cx: &mut Context) { + let SignInStep::RecoveryOffer { offer, .. } = &self.step else { + return; + }; + if let Err(error) = offer.check_current() { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + if !self.attempt.reissue_only { + self.publish(cx); + } + } + + fn issue(&mut self, offer: RecoveryKeyReissue, cx: &mut Context) { + let issuing = offer.issue(); + self.move_to(SignInStep::Issuing, cx); + self.work = Some(cx.spawn(async move |this, cx| { + let result = issuing.await; + this.update(cx, |session, cx| session.issued(offer, result, cx)) + .ok(); + })); + } + + fn issued( + &mut self, + offer: RecoveryKeyReissue, + result: Result, + cx: &mut Context, + ) { + self.work = None; + match result { + Ok(key) => self.move_to(SignInStep::RecoveryKey { key }, cx), + Err(error) => self.move_to( + SignInStep::RecoveryOffer { + offer, + problem: Some(error.into()), + }, + cx, + ), + } + } + + /// Opens (or makes) the account's keys with the vault passphrase of a browser sign-in. + pub fn set_vault_passphrase(&mut self, passphrase: Secret, cx: &mut Context) { + let (SignInStep::Passphrase { .. }, Some(hub)) = (&self.step, self.hub()) else { + return; + }; + self.move_to(SignInStep::Authenticating, cx); + let opening = self.sign_in.set_vault_passphrase(&hub, passphrase); + self.work = Some(cx.spawn(async move |this, cx| { + let result = opening.await; + this.update(cx, |session, cx| session.passphrase_set(result, cx)) + .ok(); + })); + } + + fn passphrase_set(&mut self, result: Result, cx: &mut Context) { + self.work = None; + match result { + Ok(keys) => self.keys_settled(keys, cx), + Err(error) => self.move_to( + SignInStep::Passphrase { + problem: Some(error.into()), + }, + cx, + ), + } + } + + /// Opens the account's keys with the recovery key, after the password did not. + pub fn recover(&mut self, recovery_key: Secret, cx: &mut Context) { + let (SignInStep::Recovery { .. }, Some(hub)) = (&self.step, self.hub()) else { + return; + }; + self.move_to(SignInStep::Authenticating, cx); + let recovering = self.sign_in.recover(&hub, recovery_key); + self.work = Some(cx.spawn(async move |this, cx| { + let result = recovering.await; + this.update(cx, |session, cx| session.recovered(result, cx)) + .ok(); + })); + } + + fn recovered(&mut self, result: Result<(), HubError>, cx: &mut Context) { + self.work = None; + match result { + Ok(()) => self.publish(cx), + Err(error) => self.move_to( + SignInStep::Recovery { + problem: Some(error.into()), + }, + cx, + ), + } + } + + /// Goes one step back; a browser sign-in in flight is cancelled. + pub fn back(&mut self, cx: &mut Context) { + let target = match &self.step { + SignInStep::Browser { .. } + | SignInStep::Authenticating + | SignInStep::Passphrase { .. } + | SignInStep::Recovery { .. } => { + self.work = None; + self.show_methods(None, cx); + return; + } + SignInStep::Method { .. } if self.attempt.fixed => return, + SignInStep::Checking => { + self.work = None; + SignInStep::Address { problem: None } + } + SignInStep::ConfirmFingerprint { .. } + | SignInStep::Mismatch { .. } + | SignInStep::Method { .. } => SignInStep::Address { problem: None }, + SignInStep::Address { .. } + | SignInStep::RecoveryKey { .. } + | SignInStep::RecoveryOffer { .. } + | SignInStep::Issuing + | SignInStep::Saving + | SignInStep::Finished(_) + | SignInStep::Failed(_) => return, + }; + self.move_to(target, cx); + } + + /// Adds the server to the settings in one edit. + fn publish(&mut self, cx: &mut Context) { + if let Some(login) = &self.attempt.login + && let Err(error) = login.check_current() + { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + let (Some(address), Some(trust), Some(info)) = ( + self.attempt.address.clone(), + self.attempt.trust, + self.attempt.info.as_ref(), + ) else { + return; + }; + let label = self + .attempt + .label + .clone() + .unwrap_or_else(|| info.name.clone()); + let record = HubRecord::new(address, trust, label); + let hub = record.id(); + self.move_to(SignInStep::Saving, cx); + let original = self.attempt.login.clone(); + let saving = self + .settings + .update_checked::(cx, move |settings| { + if let Some(original) = &original { + original + .check_current() + .map_err(nocterm_store::StoreError::backend)?; + } + // Signing in again to a known server keeps the colour its owner chose. + let color = settings.find(&record.id()).and_then(|known| known.color); + settings.upsert(record.with_color(color)); + Ok(()) + }); + self.work = Some(cx.spawn(async move |this, cx| { + let saved = saving.await; + this.update(cx, |session, cx| session.saved(&hub, saved.map(|_| ()), cx)) + .ok(); + })); + } + + fn saved( + &mut self, + hub: &HubId, + saved: Result<(), nocterm_store::StoreError>, + cx: &mut Context, + ) { + self.work = None; + if let Some(login) = &self.attempt.login + && let Err(error) = login.check_current() + { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + match saved { + Ok(()) => { + self.move_to(SignInStep::Finished(hub.clone()), cx); + cx.emit(SignInEvent::Finished(hub.clone())); + } + Err(error) => { + // Only the original login may be discarded after the awaited save. + if let Some(login) = &self.attempt.login + && let Err(forget) = login.discard() + { + tracing::warn!(%hub,error=%forget,"credentials of the original unlisted login were kept"); + } + self.move_to(SignInStep::Failed(SignInError::Save(error)), cx); + } + } + } +} + +fn parse_inputs( + address: &str, + fingerprint: &str, +) -> Result<(HubAddress, Option), SignInError> { + let address = HubAddress::parse(address)?; + let fingerprint = fingerprint.trim(); + let expected = if fingerprint.is_empty() { + None + } else { + Some(Fingerprint::parse(fingerprint)?) + }; + Ok((address, expected)) +} + +/// Waits for a browser sign-in, reporting the address to open as soon as it is known. +async fn await_browser_sign_in( + browser: nocterm_hubs_model::HubFuture, + done: nocterm_hubs_model::HubFuture, + mut open: impl FnMut(String), +) -> Result { + let mut browser = browser.fuse(); + let mut done = done.fuse(); + loop { + futures::select! { + url = browser => { + if let Ok(url) = url { + open(url); + } + } + result = done => return result, + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs/src/sign_in/tests.rs b/crates/hubs/src/sign_in/tests.rs new file mode 100644 index 00000000..405a77c4 --- /dev/null +++ b/crates/hubs/src/sign_in/tests.rs @@ -0,0 +1,780 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use gpui::{Entity, TestAppContext}; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp}; +use nocterm_hubs_model::{Fingerprint, HubColor, HubError, HubSignIn as _, TrustPolicy}; +use nocterm_session::Secret; + +use crate::fixture::{ADDRESS, Fixture, fixture, fixture_with_failing_settings, id, server}; +use crate::{HubStatus, SignInError, SignInEvent, SignInSession, SignInStep, TrustNote}; + +fn hub() -> FakeHub { + FakeHub::new().with_server(server()) +} + +fn start(cx: &mut TestAppContext, fx: &Fixture) -> Entity { + fx.hubs.update(cx, |hubs, cx| hubs.start_sign_in(cx)) +} + +fn step(cx: &mut TestAppContext, session: &Entity) -> SignInStep { + session.read_with(cx, |session, _| session.step().clone()) +} + +fn submit(cx: &mut TestAppContext, session: &Entity, address: &str, print: &str) { + session.update(cx, |session, cx| session.submit_address(address, print, cx)); + cx.run_until_parked(); +} + +fn events( + cx: &mut TestAppContext, + session: &Entity, +) -> (Rc>>, gpui::Subscription) { + let seen = Rc::new(RefCell::new(Vec::new())); + let sink = Rc::clone(&seen); + let subscription = cx.update(|cx| { + cx.subscribe(session, move |_, event: &SignInEvent, _| { + sink.borrow_mut().push(event.clone()) + }) + }); + (seen, subscription) +} + +fn password(cx: &mut TestAppContext, session: &Entity, user: &str, secret: &str) { + session.update(cx, |session, cx| { + session.sign_in_with_password(user, Secret::new(secret), cx); + }); + cx.run_until_parked(); +} + +#[gpui::test] +async fn an_unknown_certificate_waits_for_the_user_then_pins_and_signs_in(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + let revision = cx.read(|cx| fx.settings.revision(cx)); + let (seen, _keep) = events(cx, &session); + + submit(cx, &session, ADDRESS, ""); + let SignInStep::ConfirmFingerprint { fingerprint, .. } = step(cx, &session) else { + panic!("expected the fingerprint to be confirmed"); + }; + assert_eq!(fingerprint, Fingerprint::from_bytes([7; 32])); + assert_eq!( + fx.hub.count(FakeOp::Connect), + 0, + "nothing is sent before the user trusts it" + ); + + session.update(cx, |session, cx| session.confirm_fingerprint(cx)); + cx.run_until_parked(); + let SignInStep::Method { + note, + password: offered, + providers, + hub_name, + .. + } = step(cx, &session) + else { + panic!("expected the sign-in methods"); + }; + assert_eq!(note, TrustNote::Confirmed); + assert!(offered); + assert_eq!(providers.len(), 1); + assert_eq!(hub_name, "Home hub"); + assert!( + fx.servers(cx).is_empty(), + "the server is not listed before signing in" + ); + + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::Finished(hub) if hub == id())); + assert_eq!(*seen.borrow(), [SignInEvent::Finished(id())]); + + let servers = fx.servers(cx); + assert_eq!(servers.len(), 1); + assert_eq!( + servers[0].trust, + TrustPolicy::Pinned(Fingerprint::from_bytes([7; 32])) + ); + assert_eq!(servers[0].label, "Home hub"); + assert_eq!( + cx.read(|cx| fx.settings.revision(cx)), + revision + 1, + "one edit" + ); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn a_typed_fingerprint_that_matches_skips_the_confirmation(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + &format!("https://{ADDRESS}"), + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + note: TrustNote::Pinned, + .. + } + )); +} + +#[gpui::test] +async fn a_typed_fingerprint_that_differs_is_refused_before_any_credentials_are_sent( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([1; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::Mismatch { typed, presented } + if typed == Fingerprint::from_bytes([1; 32]) && presented == Fingerprint::from_bytes([7; 32]) + )); + assert_eq!(fx.hub.count(FakeOp::Connect), 0); + session.update(cx, |session, cx| session.back(cx)); + assert!(matches!( + step(cx, &session), + SignInStep::Address { problem: None } + )); +} + +#[gpui::test] +async fn a_publicly_trusted_certificate_needs_no_confirmation(cx: &mut TestAppContext) { + let hub = FakeHub::new().with_server(server().publicly_trusted()); + let fx = fixture(cx, hub, Vec::new()); + let session = start(cx, &fx); + submit(cx, &session, ADDRESS, ""); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + note: TrustNote::PublicCa, + .. + } + )); + password(cx, &session, "ada", "secret"); + assert_eq!(fx.servers(cx)[0].trust, TrustPolicy::PublicCa); +} + +#[gpui::test] +async fn a_malformed_address_or_fingerprint_is_reported_on_the_address_step( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit(cx, &session, "http://hub.home", ""); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::Address(_)) + } + )); + submit(cx, &session, ADDRESS, "xyz"); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::Fingerprint(_)) + } + )); + assert_eq!(fx.hub.calls(), []); +} + +#[gpui::test] +async fn an_unreachable_server_returns_to_the_address_step(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit(cx, &session, "nowhere.example", ""); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::Hub(HubError::Unreachable { .. })) + } + )); +} + +#[gpui::test] +async fn a_server_without_an_owner_cannot_be_signed_in_to(cx: &mut TestAppContext) { + let hub = hub(); + hub.edit_server(&crate::fixture::address(), |server| { + server.bootstrapped = false + }); + let fx = fixture(cx, hub, Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::NotInitialized) + } + )); +} + +#[gpui::test] +async fn a_wrong_password_returns_to_the_methods_and_changes_nothing(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + let revision = cx.read(|cx| fx.settings.revision(cx)); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + password(cx, &session, "ada", "wrong"); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + problem: Some(SignInError::Hub(HubError::InvalidCredentials)), + .. + } + )); + assert!(fx.servers(cx).is_empty()); + assert_eq!(cx.read(|cx| fx.settings.revision(cx)), revision); + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); +} + +#[gpui::test] +async fn a_device_that_is_already_signed_in_is_listed_without_asking_for_a_password( + cx: &mut TestAppContext, +) { + let hub = hub(); + hub.sign_in_as(&id(), "ada"); + let fx = fixture(cx, hub, Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.hub.count(FakeOp::Password), 0); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn the_browser_sign_in_opens_the_address_the_hub_gives_and_finishes_in_the_browser( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + let (seen, _keep) = events(cx, &session); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + let opened = seen.borrow().clone(); + assert_eq!(opened.len(), 1); + let SignInEvent::OpenBrowser(url) = &opened[0] else { + panic!("expected the browser to be opened"); + }; + assert!(url.contains("corp")); + assert!(matches!( + step(cx, &session), + SignInStep::Browser { url: Some(shown), .. } if shown == *url + )); + + fx.hub.complete_oidc("ada"); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn going_back_from_the_browser_cancels_the_sign_in(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + session.update(cx, |session, cx| session.back(cx)); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Method { problem: None, .. } + )); + assert_eq!(fx.hub.cancelled_oidc(), 1); + assert!(fx.servers(cx).is_empty()); +} + +#[gpui::test] +async fn a_browser_sign_in_that_fails_returns_to_the_methods(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + fx.hub.fail_oidc(HubError::SignInTimedOut); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + problem: Some(SignInError::Hub(HubError::SignInTimedOut)), + .. + } + )); +} + +#[gpui::test] +async fn credentials_made_for_a_server_that_could_not_be_saved_are_erased(cx: &mut TestAppContext) { + let fx = fixture_with_failing_settings(cx, hub()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + password(cx, &session, "ada", "secret"); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Save(_)) + )); + assert!(!fx.hub.is_signed_in(&id())); + assert!(fx.hub.calls().contains(&FakeCall::Forget(id()))); + assert!(fx.servers(cx).is_empty()); +} + +#[gpui::test] +async fn signing_in_again_to_a_revoked_server_starts_at_the_methods(cx: &mut TestAppContext) { + let hub = hub(); + hub.sign_in_as(&id(), "ada"); + hub.revoke_device(&id()); + let fx = fixture(cx, hub, vec![crate::fixture::pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Revoked)); + + let session = fx + .hubs + .update(cx, |hubs, cx| hubs.start_sign_in_for(&id(), cx)) + .expect("a known hub"); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + note: TrustNote::Pinned, + .. + } + )); + assert!(!session.read_with(cx, |session, _| session.address_editable())); + session.update(cx, |session, cx| session.back(cx)); + assert!( + matches!(step(cx, &session), SignInStep::Method { .. }), + "the address stays fixed" + ); + + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx)[0].label, "Home", "a known label is kept"); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn a_known_address_is_refused_so_a_changed_certificate_is_never_pinned_again( + cx: &mut TestAppContext, +) { + let hub = hub(); + let fx = fixture(cx, hub, vec![crate::fixture::pinned_record()]); + cx.run_until_parked(); + let pinned = fx.servers(cx); + fx.hub.edit_server(&crate::fixture::address(), |server| { + server.certificate = Fingerprint::from_bytes([9; 32]); + }); + let probes = fx.hub.count(FakeOp::Probe); + + let session = start(cx, &fx); + submit(cx, &session, ADDRESS, ""); + + assert!( + matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::AlreadyKnown(hub)) + } if hub == id() + ), + "the dialog does not offer to trust the new certificate" + ); + assert_eq!(fx.hub.count(FakeOp::Probe), probes, "nothing was asked"); + assert_eq!(fx.servers(cx), pinned, "the pinned record is unchanged"); +} + +#[gpui::test] +async fn a_resumed_sign_in_cannot_be_pointed_at_another_server(cx: &mut TestAppContext) { + let hub = hub(); + let fx = fixture(cx, hub, vec![crate::fixture::pinned_record()]); + cx.run_until_parked(); + fx.hub + .edit_server(&crate::fixture::address(), |server| server.online = false); + let session = fx + .hubs + .update(cx, |hubs, cx| hubs.start_sign_in_for(&id(), cx)) + .expect("a known hub"); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Address { .. })); + let (probes, connects) = (fx.hub.count(FakeOp::Probe), fx.hub.count(FakeOp::Connect)); + + submit(cx, &session, "other.example:7443", ""); + + assert!(matches!(step(cx, &session), SignInStep::Address { .. })); + assert_eq!(fx.hub.count(FakeOp::Probe), probes); + assert_eq!(fx.hub.count(FakeOp::Connect), connects); +} + +#[gpui::test] +async fn signing_in_again_keeps_the_colour_chosen_for_the_server(cx: &mut TestAppContext) { + let hub = hub(); + hub.sign_in_as(&id(), "ada"); + hub.revoke_device(&id()); + let marked = crate::fixture::pinned_record().with_color(Some(HubColor::Blue)); + let fx = fixture(cx, hub, vec![marked]); + cx.run_until_parked(); + + let session = fx + .hubs + .update(cx, |hubs, cx| hubs.start_sign_in_for(&id(), cx)) + .expect("a known hub"); + cx.run_until_parked(); + password(cx, &session, "ada", "secret"); + + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx)[0].color, Some(HubColor::Blue)); +} + +fn keyed_hub() -> FakeHub { + FakeHub::new().with_server(server().keyed()) +} + +fn at_methods(cx: &mut TestAppContext, fx: &Fixture) -> Entity { + let session = start(cx, fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session +} + +#[gpui::test] +async fn a_first_sign_in_shows_the_recovery_key_once_and_lists_the_server_when_it_is_acknowledged( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + + let SignInStep::RecoveryKey { key } = step(cx, &session) else { + panic!("expected the recovery key"); + }; + assert_eq!(key.key(), &Secret::new("recovery-ada")); + assert!( + fx.servers(cx).is_empty(), + "the server waits for the key to be written down" + ); + + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn a_reset_password_asks_for_the_recovery_key_and_refuses_a_wrong_one( + cx: &mut TestAppContext, +) { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "old", "recovery-key")); + let fx = fixture(cx, hub, Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + assert!(matches!( + step(cx, &session), + SignInStep::Recovery { problem: None } + )); + + session.update(cx, |session, cx| { + session.recover(Secret::new("not it"), cx); + }); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Recovery { + problem: Some(SignInError::Hub(HubError::RecoveryKeyInvalid)) + } + )); + assert!(fx.servers(cx).is_empty()); + + session.update(cx, |session, cx| { + session.recover(Secret::new("recovery-key"), cx); + }); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn a_browser_sign_in_asks_for_a_vault_passphrase_before_listing_the_server( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + fx.hub.complete_oidc("ada"); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Passphrase { problem: None } + )); + + session.update(cx, |session, cx| { + session.set_vault_passphrase(Secret::new("a long passphrase"), cx); + }); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); +} + +#[gpui::test] +async fn the_key_steps_do_nothing_out_of_turn(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + session.update(cx, |session, cx| { + session.acknowledge_recovery_key(cx); + session.issue_recovery_key(cx); + session.skip_recovery_offer(cx); + session.recover(Secret::new("recovery-ada"), cx); + session.set_vault_passphrase(Secret::new("a long passphrase"), cx); + }); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Method { .. })); + assert_eq!(fx.hub.count(FakeOp::Recover), 0); + assert_eq!(fx.hub.count(FakeOp::ReissueRecoveryKey), 0); + assert_eq!(fx.hub.count(FakeOp::SetVaultPassphrase), 0); +} + +/// The sign-in of a first account, closed on the recovery-key step: the server is not listed and +/// the key was shown but never confirmed. +fn closed_on_the_recovery_key(cx: &mut TestAppContext, fx: &Fixture) { + let session = at_methods(cx, fx); + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); + drop(session); + cx.run_until_parked(); +} + +fn issue(cx: &mut TestAppContext, session: &Entity) { + session.update(cx, |session, cx| session.issue_recovery_key(cx)); + cx.run_until_parked(); +} + +#[gpui::test] +async fn acknowledging_the_recovery_key_confirms_it_so_it_is_not_offered_again( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + assert!(fx.hub.recovery_key_unconfirmed(&id())); + + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + + assert!(!fx.hub.recovery_key_unconfirmed(&id())); +} + +#[gpui::test] +async fn a_sign_in_closed_on_the_recovery_key_offers_a_new_one_next_time(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + assert!(fx.servers(cx).is_empty()); + + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::RecoveryOffer { problem: None, .. } + )); + assert!( + fx.servers(cx).is_empty(), + "nothing is listed before it is settled" + ); + + issue(cx, &session); + let SignInStep::RecoveryKey { key } = step(cx, &session) else { + panic!("expected the new recovery key"); + }; + assert_eq!( + key.key(), + &Secret::new("recovery-ada+"), + "the old key was replaced" + ); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); + assert!(!fx.hub.recovery_key_unconfirmed(&id())); +} + +#[gpui::test] +async fn a_password_sign_in_that_finds_the_key_unconfirmed_offers_a_new_one( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + fx.hub.expire_login(&id()); + + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + + assert!(matches!( + step(cx, &session), + SignInStep::RecoveryOffer { problem: None, .. } + )); +} + +#[gpui::test] +async fn leaving_the_offer_for_later_lists_the_server_and_keeps_the_key_unconfirmed( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + + session.update(cx, |session, cx| session.skip_recovery_offer(cx)); + cx.run_until_parked(); + + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); + assert!( + fx.hubs + .read_with(cx, |hubs, _| hubs.recovery_key_unconfirmed(&id())) + ); +} + +#[gpui::test] +async fn a_failed_reissue_returns_to_the_offer_and_can_be_tried_again(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + fx.hub + .fail_next(FakeOp::ReissueRecoveryKey, HubError::KeysLocked); + + issue(cx, &session); + assert!(matches!( + step(cx, &session), + SignInStep::RecoveryOffer { + problem: Some(SignInError::Hub(HubError::KeysLocked)), + .. + } + )); + + issue(cx, &session); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); +} + +#[gpui::test] +async fn the_servers_page_can_issue_a_new_key_for_a_listed_server(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + let first = start(cx, &fx); + submit( + cx, + &first, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + // Listed, with the key shown but not confirmed. + first.update(cx, |session, cx| session.skip_recovery_offer(cx)); + cx.run_until_parked(); + assert_eq!(fx.servers(cx).len(), 1); + assert!( + fx.hubs + .read_with(cx, |hubs, _| hubs.recovery_key_unconfirmed(&id())) + ); + + let session = fx + .hubs + .update(cx, |hubs, cx| { + hubs.start_recovery_key_reissue(&id(), fx.hub.recovery_key_offer(&id()).unwrap(), cx) + }) + .expect("a listed server"); + cx.run_until_parked(); + assert!(session.read_with(cx, |session, _| session.reissue_only())); + let (seen, _keep) = events(cx, &session); + let SignInStep::RecoveryKey { key } = step(cx, &session) else { + panic!("expected the new recovery key"); + }; + assert_eq!(key.key(), &Secret::new("recovery-ada+")); + + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(*seen.borrow(), vec![SignInEvent::Finished(id())]); + assert!( + !fx.hubs + .read_with(cx, |hubs, _| hubs.recovery_key_unconfirmed(&id())) + ); +} + +#[gpui::test] +async fn no_key_is_issued_for_a_server_that_is_not_listed(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = fx.hubs.update(cx, |hubs, cx| { + hubs.start_recovery_key_reissue(&id(), fx.hub.recovery_key_offer(&id()).unwrap(), cx) + }); + assert!(session.is_none()); + assert_eq!(fx.hub.count(FakeOp::ReissueRecoveryKey), 0); +} + +mod source_binding; diff --git a/crates/hubs/src/sign_in/tests/source_binding.rs b/crates/hubs/src/sign_in/tests/source_binding.rs new file mode 100644 index 00000000..630d607e --- /dev/null +++ b/crates/hubs/src/sign_in/tests/source_binding.rs @@ -0,0 +1,78 @@ +use super::*; + +#[gpui::test] +async fn an_earlier_recovery_card_cannot_acknowledge_the_replacement_accounts_key( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); + fx.hub.sign_in_as(&id(), "another account"); + fx.hub.leave_recovery_key_unconfirmed(&id()); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Hub(HubError::Stale)) + )); + assert!(fx.servers(cx).is_empty()); + assert!(fx.hub.recovery_key_unconfirmed(&id())); +} + +#[gpui::test] +async fn a_failed_delayed_settings_write_never_discards_a_replacement_login( + cx: &mut TestAppContext, +) { + for replacements in [vec!["ada"], vec!["bob", "ada"]] { + let (writer, release, writes) = nocterm_store::test_support::gated_writer().into_parts(); + let fx = crate::fixture::fixture_with_writer(cx, hub(), Vec::new(), writer); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Saving)); + assert_eq!(writes.lock().unwrap().len(), 1); + for account in replacements { + fx.hub.sign_in_as(&id(), account); + } + release + .send(Err(nocterm_store::StoreError::backend( + std::io::Error::from(std::io::ErrorKind::PermissionDenied), + ))) + .unwrap(); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Hub(HubError::Stale)) + )); + assert!(fx.hub.is_signed_in(&id())); + assert!(!fx.hub.calls().contains(&FakeCall::Forget(id()))); + assert!(fx.servers(cx).is_empty()); + } +} + +#[gpui::test] +async fn a_recovery_acknowledgement_waits_for_durable_completion_and_rechecks_its_recipient( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + let release = fx.hub.hold(FakeOp::ConfirmRecoveryKey); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Saving)); + assert!(fx.servers(cx).is_empty()); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + assert_eq!(fx.hub.count(FakeOp::ConfirmRecoveryKey), 1); + fx.hub.sign_in_as(&id(), "replacement"); + fx.hub.leave_recovery_key_unconfirmed(&id()); + release.release(); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Hub(HubError::Stale)) + )); + assert!(fx.servers(cx).is_empty()); + assert!(fx.hub.recovery_key_unconfirmed(&id())); +} diff --git a/crates/hubs/src/status.rs b/crates/hubs/src/status.rs new file mode 100644 index 00000000..ae52815d --- /dev/null +++ b/crates/hubs/src/status.rs @@ -0,0 +1,190 @@ +//! What a known hub is doing, and what to make of the outcome of reaching it. + +use std::time::{Duration, Instant}; + +use nocterm_hubs_model::{Fingerprint, HubError, HubInfo}; + +/// The state of one known hub, shared by every window. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum HubStatus { + /// Credentials are missing, so the user has to sign in. + SignedOut, + /// A connection is being made. + Connecting, + /// Connected with working credentials. + Online, + /// The hub did not answer; the next attempt starts at `retry_at`. + Offline { retry_at: Instant }, + /// The hub revoked this device; its credentials are gone. + Revoked, + /// The hub's certificate is not the trusted one. Never retried, never accepted. + TrustFailed { + /// The pinned fingerprint; `None` when a public authority was trusted instead. + pinned: Option, + presented: Fingerprint, + }, + /// The hub and this client speak different protocol versions. + UpgradeRequired { hub: u32, client: u32 }, +} + +impl HubStatus { + pub const fn is_online(&self) -> bool { + matches!(self, Self::Online) + } + + /// Signing in again is the way forward. + pub const fn needs_sign_in(&self) -> bool { + matches!(self, Self::SignedOut | Self::Revoked) + } +} + +/// What reaching a hub leads to. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Settled { + /// A resting status. + Status(HubStatus), + /// The hub could not be reached for now; try again later. + Retry, +} + +/// Reads the outcome of connecting to a hub. +pub(crate) fn settle_connect(result: &Result) -> Settled { + match result { + Ok(info) if info.signed_in => Settled::Status(HubStatus::Online), + Ok(_) => Settled::Status(HubStatus::SignedOut), + Err(error) => settle_failure(error).unwrap_or(Settled::Retry), + } +} + +/// Reads a failure of a call on a hub that was online; `None` leaves the status alone. +pub(crate) fn settle_failure(error: &HubError) -> Option { + Some(match error { + HubError::Unreachable { .. } => Settled::Retry, + HubError::CertificateChanged { + pinned, presented, .. + } => Settled::Status(HubStatus::TrustFailed { + pinned: Some(*pinned), + presented: *presented, + }), + HubError::CertificateUntrusted { presented, .. } => { + Settled::Status(HubStatus::TrustFailed { + pinned: None, + presented: *presented, + }) + } + HubError::UpgradeRequired { hub, client } => Settled::Status(HubStatus::UpgradeRequired { + hub: *hub, + client: *client, + }), + HubError::NotSignedIn => Settled::Status(HubStatus::SignedOut), + HubError::Revoked => Settled::Status(HubStatus::Revoked), + // Credentials that cannot be kept or any other trouble of the hub client: the hub is + // not usable now, and trying again later is the harmless answer. + HubError::Credentials { .. } | HubError::Remote { .. } => Settled::Retry, + HubError::InvalidCredentials + | HubError::MethodUnavailable + | HubError::SignInTimedOut + | HubError::SignInCancelled + | HubError::PermissionDenied + | HubError::NotFound + | HubError::Stale + | HubError::RecoveryKeyInvalid + | HubError::InvalidVaultPassphrase + | HubError::KeysetDamaged { .. } + | HubError::UnlockSourceMismatch { .. } + | HubError::KeysLocked + | HubError::NoRecoveryPending => return None, + }) +} + +/// Waits between attempts to reach a hub that does not answer; the last one repeats. +const RETRY_DELAYS: [Duration; 5] = [ + Duration::from_secs(2), + Duration::from_secs(5), + Duration::from_secs(10), + Duration::from_secs(30), + Duration::from_secs(60), +]; + +/// How long to wait after `failures` unsuccessful attempts in a row (at least one). +pub(crate) fn retry_delay(failures: u32) -> Duration { + let index = usize::try_from(failures.saturating_sub(1)).unwrap_or(usize::MAX); + RETRY_DELAYS[index.min(RETRY_DELAYS.len() - 1)] +} + +#[cfg(test)] +mod tests { + use nocterm_core::error::SharedCause; + use nocterm_hubs_model::HubAddress; + + use super::*; + + fn info(signed_in: bool) -> HubInfo { + HubInfo { + name: "Home".into(), + password_login: true, + oidc_providers: Vec::new(), + bootstrapped: true, + signed_in, + } + } + + fn unreachable() -> HubError { + #[derive(Debug, thiserror::Error)] + #[error("down")] + struct Down; + HubError::Unreachable { + address: HubAddress::parse("hub.home").unwrap(), + cause: SharedCause::new(Down), + } + } + + #[test] + fn a_signed_in_device_is_online_and_an_unknown_one_is_signed_out() { + assert_eq!( + settle_connect(&Ok(info(true))), + Settled::Status(HubStatus::Online) + ); + assert_eq!( + settle_connect(&Ok(info(false))), + Settled::Status(HubStatus::SignedOut) + ); + } + + #[test] + fn an_unreachable_hub_is_retried_but_a_changed_certificate_never_is() { + assert_eq!(settle_connect(&Err(unreachable())), Settled::Retry); + let changed = HubError::CertificateChanged { + address: HubAddress::parse("hub.home").unwrap(), + pinned: Fingerprint::from_bytes([1; 32]), + presented: Fingerprint::from_bytes([2; 32]), + }; + assert_eq!( + settle_connect(&Err(changed)), + Settled::Status(HubStatus::TrustFailed { + pinned: Some(Fingerprint::from_bytes([1; 32])), + presented: Fingerprint::from_bytes([2; 32]), + }) + ); + } + + #[test] + fn revoked_and_outdated_hubs_rest_without_retries() { + assert_eq!( + settle_connect(&Err(HubError::Revoked)), + Settled::Status(HubStatus::Revoked) + ); + assert_eq!( + settle_connect(&Err(HubError::UpgradeRequired { hub: 2, client: 1 })), + Settled::Status(HubStatus::UpgradeRequired { hub: 2, client: 1 }) + ); + assert!(settle_failure(&HubError::PermissionDenied).is_none()); + } + + #[test] + fn retries_back_off_and_the_last_delay_repeats() { + let delays: Vec = (1..=7).map(|n| retry_delay(n).as_secs()).collect(); + assert_eq!(delays, [2, 5, 10, 30, 60, 60, 60]); + assert_eq!(retry_delay(0).as_secs(), 2); + } +} diff --git a/crates/keymap-ui/src/render.rs b/crates/keymap-ui/src/render.rs index 6387a02e..d9b6ce21 100644 --- a/crates/keymap-ui/src/render.rs +++ b/crates/keymap-ui/src/render.rs @@ -17,7 +17,7 @@ use nocterm_keymap::Source; use nocterm_ui::{IconName, form}; use crate::{ - rows::{Row, matches, rows}, + rows::{Query, Row}, view::{KeymapView, Recording}, }; @@ -230,13 +230,26 @@ impl KeymapView { impl Render for KeymapView { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { - let query = self.search.read(cx).value().to_string(); - let listed: Vec = rows(&self.keymap, cx) - .into_iter() - .filter(|row| matches(row, &query)) + // Local actions may render before deferred owner observations are delivered. + // Read the semantic key here; unchanged frames still reuse all prepared rows. + let revision = self.keymap.revision(cx); + if revision != self.rows_revision { + self.rows = std::rc::Rc::new(crate::rows::rows(&self.keymap, cx)); + self.rows_revision = revision; + } + let query = Query::new(&self.search.read(cx).value()); + let listed: Vec = self + .rows + .iter() + .filter(|row| query.matches(row)) + .cloned() .collect(); let count = listed.len(); - let file_error = self.keymap.error(cx).map(ToString::to_string); + let file_error = self + .keymap + .error(cx) + .map(ToString::to_string) + .or_else(|| self.keymap.publication_error(cx).map(ToString::to_string)); let mut table = v_flex().id("keymap-table").test_support().w_full(); table = table.child(Self::column_titles(cx)); for (index, row) in listed.into_iter().enumerate() { diff --git a/crates/keymap-ui/src/rows.rs b/crates/keymap-ui/src/rows.rs index dcb3f883..313f3204 100644 --- a/crates/keymap-ui/src/rows.rs +++ b/crates/keymap-ui/src/rows.rs @@ -35,6 +35,7 @@ pub(crate) fn default_context(action: &str, defaults: &[Binding]) -> Option Vec { let docs = cx.action_documentation(); + let customized = keymap.customized_actions(cx); let row = |action: &str, binding: Option<&Binding>| Row { action: action.to_owned(), label: humanize(action).into(), @@ -46,9 +47,13 @@ pub(crate) fn rows(keymap: &KeymapHandle, cx: &App) -> Vec { keystrokes: binding.map(|binding| binding.keystrokes.clone()), context: binding.and_then(|binding| binding.context.clone()), source: binding.map(|binding| binding.source), - customized: keymap.is_customized(action, cx), + customized: customized.contains(action), }; let bindings = keymap.bindings(cx); + let bound: std::collections::BTreeSet<_> = bindings + .iter() + .map(|binding| binding.action.as_str()) + .collect(); let mut rows: Vec = bindings .iter() .filter(|binding| offered(&binding.action)) @@ -62,7 +67,7 @@ pub(crate) fn rows(keymap: &KeymapHandle, cx: &App) -> Vec { // A command that cannot be built without arguments is sent by a menu; a binding to it // would be skipped when the keymap loads. .filter(|name| cx.build_action(name, None).is_ok()) - .filter(|name| !bindings.iter().any(|binding| binding.action == *name)) + .filter(|name| !bound.contains(name)) .collect(); unbound.sort_unstable(); unbound.dedup(); @@ -75,32 +80,50 @@ pub(crate) fn rows(keymap: &KeymapHandle, cx: &App) -> Vec { /// name, description, shortcut or context, ignoring case. A query that reads /// as a shortcut (`ctrl-shift-p`, `shift-ctrl-p`) also matches the shortcut /// however its modifiers are ordered. -pub(crate) fn matches(row: &Row, query: &str) -> bool { - let query = query.trim().to_lowercase(); - if query.is_empty() { - return true; +pub(crate) struct Query { + text: String, + keys: Option, +} +impl Query { + pub(crate) fn new(query: &str) -> Self { + let text = query.trim().to_lowercase(); + let keys = text + .contains('-') + .then(|| normalize(&text).ok()) + .flatten() + .map(|keys| keys.to_lowercase()); + Self { text, keys } } - if query.contains('-') - && let (Ok(wanted), Some(keys)) = (normalize(&query), &row.keystrokes) - && normalize(keys).is_ok_and(|keys| keys.to_lowercase() == wanted.to_lowercase()) - { - return true; + pub(crate) fn matches(&self, row: &Row) -> bool { + if self.text.is_empty() { + return true; + } + if let (Some(wanted), Some(keys)) = (&self.keys, &row.keystrokes) + && normalize(keys).is_ok_and(|keys| keys.to_lowercase() == *wanted) + { + return true; + } + let haystack = format!( + "{} {} {} {} {}", + row.label, + row.action, + row.description, + row.keystrokes.as_deref().unwrap_or("unbound"), + row.context.as_deref().unwrap_or_default(), + ) + .to_lowercase(); + self.text + .split_whitespace() + .all(|word| haystack.contains(word)) } - let haystack = format!( - "{} {} {} {} {}", - row.label, - row.action, - row.description, - row.keystrokes.as_deref().unwrap_or("unbound"), - row.context.as_deref().unwrap_or_default(), - ) - .to_lowercase(); - query.split_whitespace().all(|word| haystack.contains(word)) } #[cfg(test)] mod tests { use super::*; + fn matches(row: &Row, query: &str) -> bool { + Query::new(query).matches(row) + } fn row(keys: Option<&str>) -> Row { Row { diff --git a/crates/keymap-ui/src/view.rs b/crates/keymap-ui/src/view.rs index f53fb2ae..08313dae 100644 --- a/crates/keymap-ui/src/view.rs +++ b/crates/keymap-ui/src/view.rs @@ -35,6 +35,8 @@ pub struct KeymapView { _subscription: Subscription, keymap: KeymapHandle, _keymap_subscription: Subscription, + rows: std::rc::Rc>, + rows_revision: u64, } impl KeymapView { @@ -51,6 +53,8 @@ impl KeymapView { let keymap_subscription = keymap.observe(cx, move |cx| { let _ = view.update(cx, |_, cx| cx.notify()); }); + let rows = std::rc::Rc::new(crate::rows::rows(&keymap, cx)); + let rows_revision = keymap.revision(cx); Self { keymap, _keymap_subscription: keymap_subscription, @@ -60,6 +64,8 @@ impl KeymapView { message: None, interceptor: None, _subscription: subscription, + rows, + rows_revision, } } diff --git a/crates/keymap/Cargo.toml b/crates/keymap/Cargo.toml index 4f92ee59..30da18e7 100644 --- a/crates/keymap/Cargo.toml +++ b/crates/keymap/Cargo.toml @@ -11,6 +11,7 @@ layer = "ui" [dependencies] nocterm-core.workspace = true +nocterm-store.workspace = true gpui-kit.workspace = true serde.workspace = true toml.workspace = true diff --git a/crates/keymap/src/error.rs b/crates/keymap/src/error.rs index 5d853469..d092cbc0 100644 --- a/crates/keymap/src/error.rs +++ b/crates/keymap/src/error.rs @@ -14,4 +14,6 @@ pub enum KeymapError { }, #[error(transparent)] Persistence(#[from] nocterm_core::SharedError), + #[error(transparent)] + Store(#[from] nocterm_store::StoreError), } diff --git a/crates/keymap/src/file.rs b/crates/keymap/src/file.rs index 0270534a..1055d14d 100644 --- a/crates/keymap/src/file.rs +++ b/crates/keymap/src/file.rs @@ -80,7 +80,7 @@ fn key(context: &Option, keystrokes: &str) -> (Option, String) { impl KeymapFile { /// The bindings for this platform, in file order. - fn here(&self) -> impl Iterator, &String, &String)> { + pub(crate) fn here(&self) -> impl Iterator, &String, &String)> { self.sections .iter() .filter(|section| section.applies_here()) diff --git a/crates/keymap/src/lib.rs b/crates/keymap/src/lib.rs index eb8559c9..18c2b7a0 100644 --- a/crates/keymap/src/lib.rs +++ b/crates/keymap/src/lib.rs @@ -2,8 +2,9 @@ //! //! Owns: keymap format, default/user merge, override persistence and GPUI installation. //! Does not own: action handlers, component bindings or the Keymap settings view. -//! Ports used/provided: explicit `KeymapHandle`. Settings section: none. Globals: none. -//! Native loading and synchronous desired-file publication remain existing UI-layer debt. +//! Ports used: shared Persisted snapshot execution and ShutdownCoordinator. Provided: explicit +//! KeymapHandle with desired binding admission and publication receipt. Settings: none. Globals: none. +//! Native loading remains startup acquisition; accepted writes use the shared bounded snapshot worker. //! Invariants: installation replaces only this crate's marked bindings; explicit //! unbindings remove defaults without modifying unrelated toolkit bindings; //! desired edits remain installed when typed persistence errors are reported. @@ -21,76 +22,90 @@ mod error; mod file; mod names; -use std::{path::PathBuf, rc::Rc}; - use gpui_kit::{ App, AppContext as _, DummyKeyboardMapper, Entity, KeyBinding, KeyBindingContextPredicate, - KeyBindingMetaIndex, NoAction, Subscription, + KeyBindingMetaIndex, NoAction, Subscription, Task, }; +use nocterm_core::{SharedError, persist::PersistError}; +use nocterm_store::{Persisted, Publication, ShutdownCoordinator, SnapshotWriter, StoreError}; +use std::{path::PathBuf, rc::Rc, sync::Arc}; pub use error::KeymapError; pub use file::{Binding, KeymapFile, Section, Source, UNBOUND, merge, normalize}; pub use names::{OFFERED_NAMESPACES, humanize, offered}; - -/// Marks the bindings this crate installs, so a reload replaces only them. const DEFAULT_META: KeyBindingMetaIndex = KeyBindingMetaIndex(0x6e6f_6301); const USER_META: KeyBindingMetaIndex = KeyBindingMetaIndex(0x6e6f_6302); -/// The defaults, the user's changes and the file they are kept in. +enum Failure { + Load(SharedError), + Write(StoreError), +} struct Keymap { defaults: KeymapFile, - user: KeymapFile, - file: Option, - /// Why the user's file could not be read or written, if it could not. - error: Option>, + user: Entity>, + failure: Option, + revision: u64, + generation: u64, + publication: Option>>, } -/// Explicit shared binding owner retained by the composition root and its pages. +/// Original binding owner; its Persisted document is the only authoritative user keymap. #[derive(Clone)] pub struct KeymapHandle { state: Entity, } impl KeymapHandle { - /// Reads the defaults from `defaults` and the user's changes from - /// `file`, then binds the merge. A malformed user file is reported in - /// [`KeymapHandle::error`] and ignored, rather than costing every binding. pub fn install(defaults: &str, file: Option, cx: &mut App) -> Self { let defaults = toml::from_str(defaults).unwrap_or_else(|error| { tracing::error!(%error, "the default keymap is malformed"); KeymapFile::default() }); - let (user, error) = match file.as_deref().map(nocterm_core::persist::load) { + let (initial, failure) = match file.as_deref().map(nocterm_core::persist::load) { None | Some(Ok(None)) => (KeymapFile::default(), None), Some(Ok(Some(user))) => (user, None), - Some(Err(error)) => { - tracing::error!(%error, "the user keymap is malformed"); - ( - KeymapFile::default(), - Some(nocterm_core::SharedError::from_error(error)), - ) - } + Some(Err(error)) => ( + KeymapFile::default(), + Some(Failure::Load(SharedError::from_error(error))), + ), }; + let user = cx.new(|_| Persisted::new(initial, writer(file), Publication::Optimistic, 8)); let state = cx.new(|_| Keymap { defaults, user, - file, - error, + failure, + revision: 0, + generation: 0, + publication: None, }); let handle = Self { state }; handle.replace_bindings(cx); handle } - - /// Last native load/publication failure, retained without reconstructing its cause. - pub fn error<'a>( + /// Registers the existing snapshot owner, including accepted writes, with application quit. + pub fn register_shutdown( &self, - cx: &'a App, - ) -> Option<&'a nocterm_core::SharedError> { - self.state.read(cx).error.as_ref() + shutdown: &ShutdownCoordinator, + cx: &App, + ) -> Result<(), StoreError> { + shutdown.register(&self.state.read(cx).user) + } + /// Last original native load/write cause, preserving its concrete allocation. + pub fn error<'a>(&self, cx: &'a App) -> Option<&'a SharedError> { + match self.state.read(cx).failure.as_ref()? { + Failure::Load(error) => Some(error), + Failure::Write(error) => native_error(error), + } + } + pub fn publication_error<'a>(&self, cx: &'a App) -> Option<&'a StoreError> { + match self.state.read(cx).failure.as_ref()? { + Failure::Write(error) => Some(error), + _ => None, + } + } + pub fn revision(&self, cx: &App) -> u64 { + self.state.read(cx).revision } - - /// Observe completed installation/error transitions; callers retain the subscription. pub fn observe( &self, cx: &mut App, @@ -98,15 +113,31 @@ impl KeymapHandle { ) -> Subscription { cx.observe(&self.state, move |_, cx| on_change(cx)) } - pub fn bindings(&self, cx: &App) -> Vec { - self.state.read(cx).bindings() + let state = self.state.read(cx); + merge(&state.defaults, state.user.read(cx).visible()) } pub fn defaults(&self, cx: &App) -> Vec { - self.state.read(cx).defaults() + merge(&self.state.read(cx).defaults, &KeymapFile::default()) + } + pub fn customized_actions(&self, cx: &App) -> std::collections::BTreeSet { + let state = self.state.read(cx); + state + .user + .read(cx) + .visible() + .here() + .filter_map(|(context, keys, action)| { + if action == UNBOUND { + state.defaults.action_for(context, keys).map(str::to_owned) + } else { + Some(action.clone()) + } + }) + .collect() } pub fn is_customized(&self, action: &str, cx: &App) -> bool { - self.state.read(cx).is_customized(action) + self.customized_actions(cx).contains(action) } pub fn conflicts( &self, @@ -115,11 +146,20 @@ impl KeymapHandle { keys: &str, cx: &App, ) -> Vec { - self.state.read(cx).conflicts(action, context, keys) + let Ok(keys) = normalize(keys) else { + return Vec::new(); + }; + self.bindings(cx) + .into_iter() + .filter(|binding| { + binding.context == *context + && binding.keystrokes == keys + && binding.action != action + }) + .map(|binding| binding.action) + .collect() } - - /// Binds `keystrokes` to `action` in `context`. With `replacing`, the - /// binding being edited goes away. + /// Installs the desired edit immediately; the returned result reports admission. pub fn bind( &self, action: &str, @@ -128,61 +168,85 @@ impl KeymapHandle { keystrokes: &str, cx: &mut App, ) -> Result<(), KeymapError> { - let keystrokes = normalize(keystrokes)?; - self.change(cx, |keymap| { + let keys = normalize(keystrokes)?; + self.change(cx, |user, defaults| { if let Some(old) = replacing { - keymap.unbind_entry(action, &context, old); + unbind_entry(user, defaults, action, &context, old); } - keymap.user.set(&context, &keystrokes, action); + user.set(&context, &keys, action); }) } - - /// Removes the binding of `keystrokes` to `action` in `context`. pub fn unbind( &self, action: &str, context: Option, - keystrokes: &str, + keys: &str, cx: &mut App, ) -> Result<(), KeymapError> { - self.change(cx, |keymap| { - keymap.unbind_entry(action, &context, keystrokes) + self.change(cx, |user, defaults| { + unbind_entry(user, defaults, action, &context, keys) }) } - - /// Returns `action` to its default bindings. pub fn reset(&self, action: &str, cx: &mut App) -> Result<(), KeymapError> { - self.change(cx, |keymap| { - let defaults = keymap.defaults.clone(); - keymap.user.retain(|context, keys, bound| { + self.change(cx, |user, defaults| { + user.retain(|context, keys, bound| { bound != action && !(bound == UNBOUND && defaults.action_for(context, keys) == Some(action)) - }); + }) }) } - - /// Publish the desired edit even when its synchronous native save fails. - fn change(&self, cx: &mut App, edit: impl FnOnce(&mut Keymap)) -> Result<(), KeymapError> { - let result = self.state.update(cx, |keymap, _| { - edit(keymap); - let result = match &keymap.file { - Some(file) => nocterm_core::persist::save(file, &keymap.user) - .map_err(nocterm_core::SharedError::from_error), - None => Ok(()), - }; - keymap.error = result.as_ref().err().cloned(); - result.map_err(KeymapError::Persistence) + /// The latest accepted publication; dropping this waiter leaves the store's work accepted. + pub fn flush(&self, cx: &mut App) -> Task> { + self.state + .update(cx, |state, _| state.publication.take()) + .unwrap_or_else(|| Task::ready(Ok(()))) + } + fn change( + &self, + cx: &mut App, + edit: impl FnOnce(&mut KeymapFile, &KeymapFile), + ) -> Result<(), KeymapError> { + let state = self.state.read(cx); + let user = state.user.clone(); + let mut next = user.read(cx).visible().clone(); + edit(&mut next, &state.defaults); + let changed = next != *user.read(cx).visible(); + let receipt = user.update(cx, |user, cx| { + user.update(move |current| *current = next, cx) + })?; + self.state.update(cx, |state, cx| { + state.revision = state.revision.wrapping_add(u64::from(changed)); + state.generation = state.generation.wrapping_add(1); + let generation = state.generation; + state.publication = Some(cx.spawn(async move |state, cx| { + let result = receipt.wait().await; + let _ = state.update(cx, |state, cx| { + if state.generation == generation { + let next = result.as_ref().err().cloned().map(Failure::Write); + let changed = state.failure.is_some() || next.is_some(); + state.failure = next; + if changed { + cx.notify(); + } + } + }); + result.map(drop).map_err(publication_error) + })); + cx.notify(); }); - self.replace_bindings(cx); - self.state.update(cx, |_, cx| cx.notify()); - result + if changed { + self.replace_bindings(cx); + } + Ok(()) } - fn replace_bindings(&self, cx: &mut App) { let ours = { - let keymap = self.state.read(cx); + let state = self.state.read(cx); let mut ours = Vec::new(); - for (file, meta) in [(&keymap.defaults, DEFAULT_META), (&keymap.user, USER_META)] { + for (file, meta) in [ + (&state.defaults, DEFAULT_META), + (state.user.read(cx).visible(), USER_META), + ] { ours.extend( bindings_of(file, cx) .into_iter() @@ -191,7 +255,7 @@ impl KeymapHandle { } ours }; - let others: Vec = cx + let others: Vec<_> = cx .key_bindings() .borrow() .bindings() @@ -202,54 +266,45 @@ impl KeymapHandle { cx.bind_keys(others.into_iter().chain(ours)); } } - -impl Keymap { - /// Every binding in effect on this platform. - fn bindings(&self) -> Vec { - merge(&self.defaults, &self.user) - } - - /// The default bindings of this platform. - fn defaults(&self) -> Vec { - merge(&self.defaults, &KeymapFile::default()) - } - - /// Whether the user changed how `action` is bound. - fn is_customized(&self, action: &str) -> bool { - let mut customized = false; - let mut user = self.user.clone(); - user.retain(|context, keys, bound| { - customized |= bound == action - || (bound == UNBOUND && self.defaults.action_for(context, keys) == Some(action)); - true - }); - customized - } - - /// The actions other than `action` that `keystrokes` runs in `context`. - fn conflicts(&self, action: &str, context: &Option, keystrokes: &str) -> Vec { - let Ok(keystrokes) = normalize(keystrokes) else { - return Vec::new(); - }; - self.bindings() - .into_iter() - .filter(|binding| { - binding.context == *context - && binding.keystrokes == keystrokes - && binding.action != action - }) - .map(|binding| binding.action) - .collect() +fn unbind_entry( + user: &mut KeymapFile, + defaults: &KeymapFile, + action: &str, + context: &Option, + keys: &str, +) { + if defaults.action_for(context, keys) == Some(action) { + user.set(context, keys, UNBOUND); + } else { + user.remove(context, keys); } - - fn unbind_entry(&mut self, action: &str, context: &Option, keystrokes: &str) { - if self.defaults.action_for(context, keystrokes) == Some(action) { - self.user.set(context, keystrokes, UNBOUND); - } else { - self.user.remove(context, keystrokes); - } +} +fn writer(file: Option) -> SnapshotWriter { + Arc::new(move |user| { + let file = file.clone(); + Box::pin(async move { + if let Some(file) = file { + nocterm_core::persist::save(&file, &user) + .map_err(SharedError::from_error) + .map_err(StoreError::backend)?; + } + Ok(()) + }) + }) +} +fn native_error(error: &StoreError) -> Option<&SharedError> { + if let StoreError::Backend(cause) = error { + cause.downcast_ref() + } else { + None } } +fn publication_error(error: StoreError) -> KeymapError { + native_error(&error) + .cloned() + .map(KeymapError::Persistence) + .unwrap_or(KeymapError::Store(error)) +} /// `file`'s bindings for this platform as GPUI bindings. Entries that do /// not resolve are logged and skipped: one stale binding must not cost the diff --git a/crates/keymap/src/names.rs b/crates/keymap/src/names.rs index 351b3797..97f3abb9 100644 --- a/crates/keymap/src/names.rs +++ b/crates/keymap/src/names.rs @@ -10,6 +10,7 @@ pub const OFFERED_NAMESPACES: &[&str] = &[ "files", "agent", "vault", + "hubs", ]; /// Whether `name` is a command a person runs: in nocterm's namespaces, and @@ -64,6 +65,7 @@ mod tests { assert!(offered("terminal::Copy")); assert!(offered("snippets::ToggleSnippets")); assert!(offered("snippets::NewSnippet")); + assert!(offered("hubs::SignInToServer")); assert!(!offered("input::Backspace")); assert!(!offered("workspace::ToggleCommandPalette")); } diff --git a/crates/keymap/src/tests.rs b/crates/keymap/src/tests.rs index 0b79b158..381886e2 100644 --- a/crates/keymap/src/tests.rs +++ b/crates/keymap/src/tests.rs @@ -22,7 +22,7 @@ fn bound(keymap: &KeymapHandle, cx: &App, keys: &str) -> Vec { } #[gpui_kit::test] -fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestAppContext) { +async fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestAppContext) { let directory = tempfile::tempdir().unwrap(); let file = directory.path().join("keymap.toml"); let keymap = cx.update(|cx| { @@ -56,6 +56,7 @@ fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestA .any(|binding| binding.action().name() == "test::Library") ); }); + cx.update(|cx| keymap.flush(cx)).await.unwrap(); let saved: KeymapFile = toml::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); let workspace = Some("Workspace".to_owned()); assert_eq!(saved.action_for(&workspace, "ctrl-e"), Some(UNBOUND)); @@ -71,6 +72,9 @@ fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestA keymap .unbind("test::Second", Some("Workspace".into()), "ctrl-j", cx) .unwrap(); + }); + cx.update(|cx| keymap.flush(cx)).await.unwrap(); + cx.update(|cx| { let keymap = KeymapHandle::install(DEFAULTS, Some(file.clone()), cx); assert!(bound(&keymap, cx, "ctrl-j").is_empty()); }); @@ -169,7 +173,9 @@ fn assert_native_failure( } #[gpui_kit::test] -fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mut TestAppContext) { +async fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it( + cx: &mut TestAppContext, +) { let directory = tempfile::tempdir().unwrap(); let file = directory.path().join("keymap.toml"); // A directory at the destination deterministically refuses atomic file publication. @@ -179,7 +185,7 @@ fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mu KeymapHandle::install(DEFAULTS, Some(file.clone()), cx) }); cx.update(|cx| { - let error = keymap + keymap .bind( "test::First", Some("Workspace".into()), @@ -187,7 +193,10 @@ fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mu "alt-e", cx, ) - .unwrap_err(); + .unwrap(); + }); + let error = cx.update(|cx| keymap.flush(cx)).await.unwrap_err(); + cx.update(|cx| { assert_native_failure(&keymap, &file, &error, cx); assert!(bound(&keymap, cx, "ctrl-e").is_empty()); assert_eq!(bound(&keymap, cx, "alt-e"), ["test::First"]); @@ -225,9 +234,10 @@ fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mu cx, ) .unwrap(); - assert!(keymap.error(cx).is_none()); assert_eq!(bound(&keymap, cx, "alt-e"), ["test::First"]); }); + cx.update(|cx| keymap.flush(cx)).await.unwrap(); + cx.update(|cx| assert!(keymap.error(cx).is_none())); let saved = nocterm_core::persist::load::(&file) .unwrap() .unwrap(); diff --git a/crates/layout-ui/src/about.rs b/crates/layout-ui/src/about.rs new file mode 100644 index 00000000..985ee62f --- /dev/null +++ b/crates/layout-ui/src/about.rs @@ -0,0 +1,56 @@ +//! What the tooltip of a chip of the bar preview says, made when it shows: the chip's name, +//! whether the panel opens with the layout and, for an item of a user plugin, what the host +//! knows of the plugin, in the host's own words. + +use std::sync::Arc; + +use gpui_kit::{AnyView, App, AppContext as _, SharedString, Window, component::tooltip::Tooltip}; +use nocterm_plugin_model::{ContributionId, PluginSet, PluginTier}; + +use crate::user_facts::SharedFacts; + +/// The tooltip of the chip of `id`, named `name`. +pub(crate) fn tooltip( + catalog: &Arc, + facts: Option<&SharedFacts>, + id: &ContributionId, + name: SharedString, + open: bool, +) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static { + let (catalog, facts, id) = (catalog.clone(), facts.cloned(), id.clone()); + move |_, cx| { + let text = text(&catalog, facts.as_ref(), &id, &name, open, cx); + cx.new(|_| Tooltip::new(text)).into() + } +} + +fn text( + catalog: &PluginSet, + facts: Option<&SharedFacts>, + id: &ContributionId, + name: &str, + open: bool, + cx: &App, +) -> SharedString { + let mut lines = vec![name.to_owned()]; + if open { + lines.push("Opens with the layout".to_owned()); + } + let Some(plugin) = catalog + .plugin_of(id) + .filter(|plugin| plugin.tier() == PluginTier::User) + else { + return lines.join("\n").into(); + }; + lines.push(format!("Plugin \u{b7} {} ({})", plugin.name(), plugin.id())); + if let Some(facts) = facts.and_then(|facts| facts.facts(plugin.id(), cx)) { + lines.push(format!("Publisher: {} (unverified)", facts.publisher)); + lines.push(format!("Fingerprint: {}", facts.fingerprint)); + lines.push(if facts.capabilities.is_empty() { + "Allowed: nothing".to_owned() + } else { + format!("Allowed: {}", facts.capabilities.join("; ")) + }); + } + lines.join("\n").into() +} diff --git a/crates/layout-ui/src/dnd.rs b/crates/layout-ui/src/dnd.rs index 25d87d8b..228818da 100644 --- a/crates/layout-ui/src/dnd.rs +++ b/crates/layout-ui/src/dnd.rs @@ -1,38 +1,22 @@ -//! Dragging the items of the bar: what a drag carries, where a drop on an element lands, and how -//! a target looks while something is over it. +//! Dragging the items of the bar: what a drag carries, what a drop asks for, how an item starts +//! a drag, and the tray, which takes what is dropped on it out of the bar. //! //! The rules stay in `nocterm-window-layout`: a landing only names the list and the place, and -//! the section sends the resulting [`EditOp`] like any other edit. - -use std::any::Any; +//! the section sends the resulting [`EditOp`] like any other edit. Where a drop over the bar +//! preview lands is worked out by `drops`. use gpui_kit::{ - AnyElement, App, ElementId, InteractiveElement, IntoElement, ParentElement, SharedString, - StatefulInteractiveElement, Styled, TestSupportExt as _, - base::ElementExt as _, - component::{ActiveTheme as _, Icon, Sizable as _, h_flex}, - div, - prelude::*, - px, + App, Bounds, InteractiveElement, ParentElement, Pixels, StatefulInteractiveElement, Styled, + Window, canvas, component::ActiveTheme as _, prelude::*, px, }; -use nocterm_plugin_model::{ContributionId, ContributionKind, ContributionRef, GroupId, Zone}; -use nocterm_ui::{DragSource, IconName, contribution_icon}; +use nocterm_plugin_model::{ContributionId, ContributionKind, ContributionRef, GroupId}; +use nocterm_ui::DragSource; use nocterm_window_layout::{EditOp, Slot, Subject}; -use crate::{commands::Commands, snapshot::Snapshot}; - -/// The hover group of a chip of the bar preview. -pub(crate) const CHIP: &str = "layout-drop-chip"; -/// The hover group of a group of the bar preview. -pub(crate) const PILL: &str = "layout-drop-pill"; -/// The hover group of the gap in front of an entry of a zone. -pub(crate) const GAP: &str = "layout-drop-gap"; -/// How wide the gap between the entries of a zone is. -pub(crate) const GAP_WIDTH: f32 = 6.; -/// How thick the line that marks a place is. -const MARK: f32 = 2.; -/// How tall a chip of the bar preview is. -pub(crate) const CHIP_HEIGHT: f32 = 22.; +use crate::{ + commands::Commands, + face::{self, Look}, +}; /// What kind of thing is dragged, which decides where it may land. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -45,44 +29,56 @@ pub(crate) enum Kind { Group, } -/// The payload of a drag: the item, enough to draw its ghost, and where it came from. +/// The payload of a drag: the item and where it came from. #[derive(Clone, Debug)] pub(crate) struct Dragged { subject: Subject, kind: Kind, - title: SharedString, - icon: Option, pinned: bool, /// The list and place it leaves, when it is in the bar. from: Option<(Slot, usize)>, } impl Dragged { - pub(crate) fn contribution(contribution: ContributionRef<'_>, snapshot: &Snapshot) -> Self { - let subject = Subject::Contribution(contribution.id().clone()); + pub(crate) fn new( + subject: Subject, + kind: Kind, + pinned: bool, + from: Option<(Slot, usize)>, + ) -> Self { Self { - kind: match contribution.kind() { - ContributionKind::Widget => Kind::Widget, - ContributionKind::Panel => Kind::Panel, - }, - title: contribution.title().to_owned().into(), - icon: Some(contribution_icon(contribution.icon())), - pinned: contribution.pinned(), - from: snapshot.before(&subject), subject, + kind, + pinned, + from, } } - pub(crate) fn group(id: &GroupId, title: SharedString, snapshot: &Snapshot) -> Self { - let subject = Subject::Group(id.clone()); - Self { - kind: Kind::Group, - title, - icon: None, - pinned: false, - from: snapshot.before(&subject), - subject, - } + /// A widget or a panel, at `from` in the bar or waiting in the tray. + pub(crate) fn contribution( + contribution: ContributionRef<'_>, + from: Option<(Slot, usize)>, + ) -> Self { + // The editor lists no toolbar; one would move like a widget. + let kind = match contribution.kind() { + ContributionKind::Widget | ContributionKind::Toolbar => Kind::Widget, + ContributionKind::Panel => Kind::Panel, + }; + let subject = Subject::Contribution(contribution.id().clone()); + Self::new(subject, kind, contribution.pinned(), from) + } + + /// The group `id`, at `from` in the bar. + pub(crate) fn group(id: &GroupId, from: (Slot, usize)) -> Self { + Self::new(Subject::Group(id.clone()), Kind::Group, false, Some(from)) + } + + pub(crate) fn subject(&self) -> &Subject { + &self.subject + } + + pub(crate) fn kind(&self) -> Kind { + self.kind } fn in_bar(&self) -> bool { @@ -96,279 +92,131 @@ impl Dragged { } } -/// Where a drop on one element puts the dragged item. An element names a place for what goes -/// into a zone, one for a panel that goes into a group, a panel on its own that a dropped panel -/// makes a group with, or the tray. -#[derive(Clone, Debug, Default)] -pub(crate) struct Landing { - zone: Option<(Zone, usize)>, - group: Option<(GroupId, usize)>, - beside: Option, - tray: bool, - /// The element is the whole zone, so it fills while a widget or a group is over it. - zone_fills: bool, - /// The element is the whole group, so it fills while a panel is over it. - group_fills: bool, +/// Where a drop puts the dragged item. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Landing { + /// In front of the entry at `index` of a zone or a group, or last when `index` is its + /// length. + At(Slot, usize), + /// Into a group with the panel that stands on its own here. + With(ContributionId), + /// Out of the bar. + Tray, } impl Landing { - /// In front of the entry at `index` of a zone, or last when `index` is its length. - pub(crate) fn in_zone(zone: Zone, index: usize) -> Self { - Self { - zone: Some((zone, index)), - ..Self::default() - } - } - - /// In front of the member at `index` of a group, or last when `index` is its length. - pub(crate) fn in_group(group: GroupId, index: usize) -> Self { - Self { - group: Some((group, index)), - ..Self::default() - } - } - - /// A panel dropped here makes a group with `panel`, which is on its own; anything else lands - /// at `zone` in front of it. - pub(crate) fn beside(panel: ContributionId, zone: Zone, index: usize) -> Self { - Self { - beside: Some(panel), - ..Self::in_zone(zone, index) - } - } - - /// Out of the bar. - pub(crate) fn tray() -> Self { - Self { - tray: true, - ..Self::default() - } - } - - /// The end of a zone, as the whole area of the zone. - pub(crate) fn zone_area(zone: Zone, len: usize) -> Self { - Self { - zone_fills: true, - ..Self::in_zone(zone, len) - } - } - - /// A panel dropped here goes to the end of `group`, whose `members` it has. - pub(crate) fn and_group_end(mut self, group: GroupId, members: usize) -> Self { - self.group = Some((group, members)); - self.group_fills = true; - self - } - - /// The edit a drop of `dragged` here asks for; `None` when it does not fit this place or - /// would leave it where it is. + /// The edit a drop of `dragged` here asks for; `None` when it changes nothing, because the + /// item is already there or does not go there. pub(crate) fn op(&self, dragged: &Dragged) -> Option { - if let (Kind::Panel, Some(target), Subject::Contribution(panel)) = - (dragged.kind, &self.beside, &dragged.subject) - { - return (panel != target).then(|| EditOp::GroupWith { - panel: panel.clone(), - target: target.clone(), - }); - } - let in_zone = self.zone.map(|(zone, index)| (Slot::Zone(zone), index)); - let wanted = match dragged.kind { - Kind::Widget | Kind::Group => in_zone, - Kind::Panel => self - .group - .clone() - .map(|(group, index)| (Slot::Group(group), index)) - .or(in_zone), - }; - if let Some((place, index)) = wanted { - return (!dragged.stays(&place, index)).then(|| EditOp::MoveTo { - subject: dragged.subject.clone(), - place, - index, - }); - } - let Subject::Contribution(id) = &dragged.subject else { - return None; - }; - (self.tray && dragged.in_bar()).then(|| EditOp::RemoveToTray { id: id.clone() }) - } - - fn accepts(&self, value: &dyn Any) -> bool { - value - .downcast_ref::() - .is_some_and(|dragged| self.op(dragged).is_some()) - } - - /// Whether a drop of `value` here goes in front of the element, which a line shows: it - /// fits, and the element as a whole does not take it. - fn marks(&self, value: &dyn Any) -> bool { - value - .downcast_ref::() - .is_some_and(|dragged| self.op(dragged).is_some() && !self.fills(dragged)) - } - - /// Whether the element as a whole is where `dragged` would go, and so fills. - fn fills(&self, dragged: &Dragged) -> bool { - match dragged.kind { - Kind::Panel => { - self.group_fills || self.tray || self.zone_fills || self.beside.is_some() + match (self, &dragged.subject) { + (Self::At(Slot::Group(_), _), _) if dragged.kind != Kind::Panel => None, + (Self::At(place, index), subject) => { + (!dragged.stays(place, *index)).then(|| EditOp::MoveTo { + subject: subject.clone(), + place: place.clone(), + index: *index, + }) + } + (Self::With(target), Subject::Contribution(panel)) + if dragged.kind == Kind::Panel && panel != target => + { + Some(EditOp::GroupWith { + panel: panel.clone(), + target: target.clone(), + }) + } + (Self::Tray, Subject::Contribution(id)) if dragged.in_bar() => { + Some(EditOp::RemoveToTray { id: id.clone() }) } - Kind::Widget => self.zone_fills || self.tray, - Kind::Group => self.zone_fills, + _ => None, } } -} -/// Makes `element` start a drag of `dragged`, with a ghost that looks like the item. -pub(crate) fn source(element: E, dragged: Dragged) -> E -where - E: StatefulInteractiveElement + ParentElement, -{ - let measured = DragSource::default(); - let capture = measured.clone(); - element - .on_prepaint(move |bounds, window, _| capture.capture(bounds, window)) - .on_drag(dragged.clone(), move |_, _, _, cx| { - cx.stop_propagation(); - let dragged = dragged.clone(); - cx.new(|_| measured.preview(move |_, cx| ghost(&dragged, cx))) - }) + /// Whether the drop goes in front of the entry at `index` of `place`. + pub(crate) fn is_at(&self, place: &Slot, index: usize) -> bool { + matches!(self, Self::At(at, before) if at == place && *before == index) + } } -/// Makes `element` take a drop at `landing`. With `fill`, it is tinted while something that -/// goes into it as a whole is over it; a pinned item over the tray shows as refused. -pub(crate) fn target( +/// Calls `painted` with the box of `element` each time it is painted. `border` is the width +/// of the element's border, which the measuring canvas, filling the padding box, leaves out. +pub(crate) fn measured( element: E, - landing: Landing, - fill: bool, - commands: Commands, + border: f32, + painted: impl FnOnce(Bounds, &mut Window, &mut App) + 'static, ) -> E { - let (fits, shown, dropped) = (landing.clone(), landing.clone(), landing); - let element = element - .can_drop(move |value, _, _| fits.accepts(value)) - .on_drop(move |dragged: &Dragged, window, cx| { - let (dragged, landing) = (dragged.clone(), dropped.clone()); - commands.run(window, cx, move |section, _, cx| { - section.dropped(&dragged, &landing, cx); - }); - }); - if !fill { - return element; - } - element.drag_over::(move |style, dragged, _, cx| { - if !shown.fills(dragged) { - return style; - } - let theme = cx.theme(); - let refused = shown.tray && dragged.pinned; - let color = if refused { theme.danger } else { theme.primary }; - style.bg(color.opacity(0.1)).border_color(color) - }) -} - -/// A line in front of an element that shows where a drop would land. It follows the hover group -/// `group`, which the element it is placed in must carry, and shows only for a drag that -/// `landing` puts in front of the element rather than into it. -pub(crate) fn marker( - id: impl Into, - group: &'static str, - landing: Landing, - cx: &App, -) -> AnyElement { - let color = cx.theme().primary; - div() - .id(id) - .test_support() + element.child( + canvas( + move |bounds, window, cx| painted(bounds.dilate(px(border)), window, cx), + |_, _, _, _| {}, + ) .absolute() - .invisible() - .cursor_default() .top_0() - .bottom_0() - .left(px(-(GAP_WIDTH + MARK) / 2.)) - .w(px(MARK)) - .rounded_full() - .can_drop(move |value, _, _| landing.marks(value)) - .group_drag_over::(group, move |style| style.visible().bg(color)) - .into_any_element() + .left_0() + .size_full(), + ) } -/// The room in front of an entry of a zone, or after its last one, that takes a drop at -/// `landing`: a panel dropped here stands on its own rather than joining its neighbour. -pub(crate) fn gap( - id: impl Into, - landing: Landing, - commands: Commands, - cx: &App, -) -> AnyElement { - let color = cx.theme().primary; - let fits = landing.clone(); - let line = div() - .absolute() - .invisible() - .top_0() - .bottom_0() - .left(px((GAP_WIDTH - MARK) / 2.)) - .w(px(MARK)) - .rounded_full() - .can_drop(move |value, _, _| fits.accepts(value)) - .group_drag_over::(GAP, move |style| style.visible().bg(color)); - let gap = div() - .id(id) - .test_support() - .group(GAP) - .relative() - .flex_none() - .w(px(GAP_WIDTH)) - .h(px(CHIP_HEIGHT)) - .child(line); - target(gap, landing, false, commands).into_any_element() +/// Makes `element` start a drag of `dragged`. The ghost draws what `look` makes when the drag +/// starts, lifted, in the box `measured` took when `element` was last painted. +pub(crate) fn source( + element: E, + dragged: Dragged, + measured: DragSource, + look: impl Fn() -> Look + 'static, +) -> E +where + E: StatefulInteractiveElement, +{ + element.on_drag(dragged, move |_, _, _, cx| { + cx.stop_propagation(); + let look = look(); + cx.new(|_| measured.preview(move |_, cx| face::ghost(&look, cx))) + }) } -/// The ghost that follows the pointer: the item as a small chip. -fn ghost(dragged: &Dragged, cx: &App) -> AnyElement { - let theme = cx.theme(); - h_flex() - .id("layout-drag-ghost") - .test_support() - .gap_1p5() - .px_2() - .py_1() - .items_center() - .rounded(theme.radius) - .border_1() - .border_color(theme.primary) - .bg(theme.popover) - .shadow_md() - .when_some(dragged.icon, |chip, icon| { - chip.child(Icon::new(icon).small()) +/// Makes the tray take what is dropped on it out of the bar. It is tinted while that would +/// happen, in the colour of a refusal for an item that is always shown. A drop that changes +/// nothing, like an item of the tray put back on it, is taken all the same and ends the drag. +pub(crate) fn tray(element: E, commands: Commands) -> E { + element + .on_drop(move |dragged: &Dragged, window, cx| { + let dragged = dragged.clone(); + commands.run(window, cx, move |section, _, cx| { + section.dropped(&dragged, &Landing::Tray, cx); + }); + }) + .drag_over::(|style, dragged, _, cx| { + if Landing::Tray.op(dragged).is_none() { + return style; + } + let theme = cx.theme(); + let color = if dragged.pinned { + theme.danger + } else { + theme.primary + }; + style.bg(color.opacity(0.1)).border_color(color) }) - .child(div().text_sm().child(dragged.title.clone())) - .into_any_element() } #[cfg(test)] mod tests { - use nocterm_plugin_model::ContributionId; + use nocterm_plugin_model::{ContributionId, Zone}; use super::*; fn dragged(kind: Kind, from: Option<(Slot, usize)>) -> Dragged { - Dragged { - subject: Subject::Contribution(ContributionId::new("a.b")), - kind, - title: "Item".into(), - icon: None, - pinned: false, - from, - } + let subject = Subject::Contribution(ContributionId::new("a.b")); + Dragged::new(subject, kind, false, from) } #[test] fn a_widget_lands_in_a_zone_and_a_panel_in_a_group_or_on_its_own_in_a_zone() { let widget = dragged(Kind::Widget, Some((Slot::Zone(Zone::Left), 0))); let panel = dragged(Kind::Panel, Some((Slot::Group(GroupId::new("g")), 0))); - let zone = Landing::in_zone(Zone::Right, 2); - let member = Landing::in_group(GroupId::new("h"), 1); + let zone = Landing::At(Slot::Zone(Zone::Right), 2); + let member = Landing::At(Slot::Group(GroupId::new("h")), 1); assert!(zone.op(&widget).is_some()); assert!(matches!( @@ -387,36 +235,34 @@ mod tests { .. }) )); - assert!( - member.op(&widget).is_none(), - "the group around it takes a widget" - ); + assert!(member.op(&widget).is_none(), "a widget never joins a group"); } #[test] - fn a_panel_dropped_on_a_panel_of_its_own_groups_with_it_and_a_widget_lands_in_front() { + fn a_panel_dropped_on_a_panel_of_its_own_groups_with_it_and_nothing_else_does() { let panel = dragged(Kind::Panel, Some((Slot::Zone(Zone::Left), 0))); let widget = dragged(Kind::Widget, Some((Slot::Zone(Zone::Left), 0))); - let alone = Landing::beside(ContributionId::new("c.d"), Zone::Left, 3); + let alone = Landing::With(ContributionId::new("c.d")); assert!(matches!(alone.op(&panel), Some(EditOp::GroupWith { .. }))); - assert!(matches!( - alone.op(&widget), - Some(EditOp::MoveTo { index: 3, .. }) - )); - let itself = Landing::beside(ContributionId::new("a.b"), Zone::Left, 0); - assert!(itself.op(&panel).is_none(), "not on itself"); + assert!( + alone.op(&widget).is_none(), + "a widget does not make a group" + ); + let itself = Landing::With(ContributionId::new("a.b")); + assert!(itself.op(&panel).is_none(), "not with itself"); } #[test] fn dropping_on_its_own_place_or_the_next_one_changes_nothing() { let widget = dragged(Kind::Widget, Some((Slot::Zone(Zone::Left), 2))); + let at = |zone, index| Landing::At(Slot::Zone(zone), index); - assert!(Landing::in_zone(Zone::Left, 2).op(&widget).is_none()); - assert!(Landing::in_zone(Zone::Left, 3).op(&widget).is_none()); - assert!(Landing::in_zone(Zone::Left, 1).op(&widget).is_some()); - assert!(Landing::in_zone(Zone::Left, 4).op(&widget).is_some()); - assert!(Landing::in_zone(Zone::Right, 2).op(&widget).is_some()); + assert!(at(Zone::Left, 2).op(&widget).is_none()); + assert!(at(Zone::Left, 3).op(&widget).is_none()); + assert!(at(Zone::Left, 1).op(&widget).is_some()); + assert!(at(Zone::Left, 4).op(&widget).is_some()); + assert!(at(Zone::Right, 2).op(&widget).is_some()); } #[test] @@ -425,10 +271,10 @@ mod tests { let waiting = dragged(Kind::Widget, None); assert!(matches!( - Landing::tray().op(&placed), + Landing::Tray.op(&placed), Some(EditOp::RemoveToTray { .. }) )); - assert!(Landing::tray().op(&waiting).is_none()); + assert!(Landing::Tray.op(&waiting).is_none()); } #[test] @@ -436,7 +282,7 @@ mod tests { let waiting = dragged(Kind::Panel, None); assert!(matches!( - Landing::zone_area(Zone::Center, 0).op(&waiting), + Landing::At(Slot::Zone(Zone::Center), 0).op(&waiting), Some(EditOp::MoveTo { place: Slot::Zone(Zone::Center), index: 0, diff --git a/crates/layout-ui/src/drops.rs b/crates/layout-ui/src/drops.rs new file mode 100644 index 00000000..0212896c --- /dev/null +++ b/crates/layout-ui/src/drops.rs @@ -0,0 +1,353 @@ +//! Where a drop over the bar preview lands, worked out from where the pointer is against what +//! the preview painted last. +//! +//! The half of an entry the pointer is over puts the drop in front of the entry or after it. +//! A panel over a group goes among its members, so the outer halves of the group's first and +//! last members are its start and its end; a widget or a group over a group goes beside the +//! group. A panel over the middle of a panel on its own makes a group with it. Away from every +//! entry the nearest zone takes the drop, so a drop over the preview always lands somewhere, +//! and the preview marks that place while the item is held over it. + +use gpui_kit::{Bounds, Context, Entity, InteractiveElement, Pixels, Point}; +use nocterm_plugin_model::{ContributionId, GroupId, Zone}; +use nocterm_window_layout::{Slot, Subject}; + +use crate::{ + commands::Commands, + dnd::{Dragged, Kind, Landing}, +}; + +/// What an entry of the preview holds, as far as a drop over it goes. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Holds { + /// A widget, a separator or a member of a group. + Item, + /// A panel that stands on its own. + Alone(ContributionId), + /// A group, with the members recorded under its own slot. + Group(GroupId), +} + +/// One entry of the preview: the entry at `index` of a zone, or a member of a group. +#[derive(Clone, Debug)] +pub(crate) struct Spot { + place: Slot, + index: usize, + holds: Holds, +} + +impl Spot { + pub(crate) fn new(place: Slot, index: usize, holds: Holds) -> Self { + Self { + place, + index, + holds, + } + } + + /// The list and place a drag of this entry leaves. + pub(crate) fn from(&self) -> (Slot, usize) { + (self.place.clone(), self.index) + } +} + +/// The preview as it was last painted, and the drag held over it. +#[derive(Debug, Default)] +pub(crate) struct Drops { + zones: Vec<(Zone, Bounds)>, + spots: Vec<(Spot, Bounds)>, + /// Where the item held over the preview would land, when that changes something. + aimed: Option, + /// The item being dragged, which its place in the preview shows as moving. + moving: Option, +} + +impl Drops { + /// Forgets what was painted, before the preview is drawn again. + pub(crate) fn repaint(&mut self) { + self.zones.clear(); + self.spots.clear(); + } + + pub(crate) fn zone(&mut self, zone: Zone, bounds: Bounds) { + self.zones.push((zone, bounds)); + } + + pub(crate) fn spot(&mut self, spot: Spot, bounds: Bounds) { + self.spots.push((spot, bounds)); + } + + pub(crate) fn aimed(&self) -> Option<&Landing> { + self.aimed.as_ref() + } + + pub(crate) fn moving(&self) -> Option<&Subject> { + self.moving.as_ref() + } + + /// Where a drop of `dragged` at `at` lands; `None` before the preview was painted. + pub(crate) fn landing(&self, at: Point, dragged: &Dragged) -> Option { + let x = at.x; + let (zone, _) = self + .zones + .iter() + .min_by(|(_, a), (_, b)| distance(a, x).total_cmp(&distance(b, x)))?; + let place = Slot::Zone(*zone); + let under = self + .spots + .iter() + .find(|(spot, bounds)| spot.place == place && bounds.left() <= x && x < bounds.right()); + if let (Some((spot, bounds)), Kind::Panel) = (under, dragged.kind()) { + match &spot.holds { + Holds::Group(group) => { + let members = Slot::Group(group.clone()); + let index = self.before(&members, x); + return Some(Landing::At(members, index)); + } + Holds::Alone(panel) if middle(bounds, x) => { + return Some(Landing::With(panel.clone())); + } + _ => {} + } + } + let index = self.before(&place, x); + Some(Landing::At(place, index)) + } + + /// Follows the pointer of a drag of `dragged`; `at` is `None` while it is away from the + /// preview. Returns whether what the preview shows changed. + pub(crate) fn aim(&mut self, at: Option>, dragged: &Dragged) -> bool { + let aimed = at + .and_then(|at| self.landing(at, dragged)) + .filter(|landing| landing.op(dragged).is_some()); + let moving = Some(dragged.subject().clone()); + let changed = aimed != self.aimed || moving != self.moving; + self.aimed = aimed; + self.moving = moving; + changed + } + + /// Forgets the drag, which ended. Returns whether what the preview shows changed. + pub(crate) fn settle(&mut self) -> bool { + let changed = self.aimed.is_some() || self.moving.is_some(); + self.aimed = None; + self.moving = None; + changed + } + + /// How many entries of `place` are left of `x` by their middle: the index a drop at `x` + /// takes. + fn before(&self, place: &Slot, x: Pixels) -> usize { + self.spots + .iter() + .filter(|(spot, bounds)| spot.place == *place && bounds.center().x < x) + .count() + } +} + +/// How far `x` is from `bounds`, sideways. +fn distance(bounds: &Bounds, x: Pixels) -> f32 { + let (left, right, x) = ( + f32::from(bounds.left()), + f32::from(bounds.right()), + f32::from(x), + ); + (left - x).max(x - right).max(0.) +} + +/// Whether `x` is in the middle half of `bounds`. +fn middle(bounds: &Bounds, x: Pixels) -> bool { + let offset = f32::from(x - bounds.center().x).abs(); + offset <= f32::from(bounds.size.width) / 4. +} + +/// Makes the whole preview take every drop of a dragged item, where `drops` says it lands, and +/// follow the pointer while one is held over it. A drop that changes nothing ends the drag all +/// the same, so no drop over the preview is refused. +pub(crate) fn target( + element: E, + drops: Entity, + commands: Commands, +) -> E { + let held = drops.clone(); + element + .on_drag_move::(move |event, _, cx| { + let at = event.event.position; + let over = event.bounds.contains(&at).then_some(at); + let dragged = event.drag(cx).clone(); + held.update(cx, |drops, cx| notify(drops.aim(over, &dragged), cx)); + }) + .on_drop(move |dragged: &Dragged, window, cx| { + let at = window.mouse_position(); + let landing = drops.update(cx, |drops, cx| { + let landing = drops.landing(at, dragged); + notify(drops.settle(), cx); + landing + }); + let Some(landing) = landing else { + return; + }; + let dragged = dragged.clone(); + commands.run(window, cx, move |section, _, cx| { + section.dropped(&dragged, &landing, cx); + }); + }) +} + +fn notify(changed: bool, cx: &mut Context) { + if changed { + cx.notify(); + } +} + +#[cfg(test)] +mod tests { + use gpui_kit::{point, px, size}; + + use super::*; + + const LEFT: Slot = Slot::Zone(Zone::Left); + + fn dragged(kind: Kind, id: &'static str) -> Dragged { + let subject = Subject::Contribution(ContributionId::new(id)); + Dragged::new(subject, kind, false, None) + } + + fn widget() -> Dragged { + dragged(Kind::Widget, "a.widget") + } + + fn panel() -> Dragged { + dragged(Kind::Panel, "a.panel") + } + + fn span(left: f32, width: f32) -> Bounds { + Bounds::new(point(px(left), px(0.)), size(px(width), px(28.))) + } + + fn at(x: f32) -> Point { + point(px(x), px(14.)) + } + + /// A left zone from 0 to 200 with a widget at 10..40, a group "g" at 50..130 holding two + /// panels at 56..86 and 92..122, and a panel "p.alone" at 140..180; a right zone from 400 + /// to 500 with a widget at 410..440. + fn drops() -> Drops { + let mut drops = Drops::default(); + let group = || Slot::Group(GroupId::new("g")); + drops.zone(Zone::Left, span(0., 200.)); + drops.zone(Zone::Right, span(400., 100.)); + drops.spot(Spot::new(LEFT, 0, Holds::Item), span(10., 30.)); + let pill = Holds::Group(GroupId::new("g")); + drops.spot(Spot::new(LEFT, 1, pill), span(50., 80.)); + drops.spot(Spot::new(group(), 0, Holds::Item), span(56., 30.)); + drops.spot(Spot::new(group(), 1, Holds::Item), span(92., 30.)); + let alone = Holds::Alone(ContributionId::new("p.alone")); + drops.spot(Spot::new(LEFT, 2, alone), span(140., 40.)); + let right = Slot::Zone(Zone::Right); + drops.spot(Spot::new(right, 0, Holds::Item), span(410., 30.)); + drops + } + + #[test] + fn the_half_of_an_entry_under_the_pointer_puts_the_drop_in_front_of_it_or_after_it() { + let drops = drops(); + + assert_eq!( + drops.landing(at(15.), &widget()), + Some(Landing::At(LEFT, 0)) + ); + assert_eq!( + drops.landing(at(35.), &widget()), + Some(Landing::At(LEFT, 1)) + ); + assert_eq!( + drops.landing(at(45.), &widget()), + Some(Landing::At(LEFT, 1)) + ); + } + + #[test] + fn a_panel_over_a_group_lands_among_its_members_from_its_start_to_its_end() { + let drops = drops(); + let group = |index| Some(Landing::At(Slot::Group(GroupId::new("g")), index)); + + assert_eq!(drops.landing(at(52.), &panel()), group(0), "the start edge"); + assert_eq!(drops.landing(at(60.), &panel()), group(0)); + assert_eq!(drops.landing(at(80.), &panel()), group(1)); + assert_eq!(drops.landing(at(118.), &panel()), group(2), "the end"); + assert_eq!(drops.landing(at(128.), &panel()), group(2), "the end edge"); + } + + #[test] + fn a_widget_over_a_group_lands_beside_the_group() { + let drops = drops(); + + assert_eq!( + drops.landing(at(60.), &widget()), + Some(Landing::At(LEFT, 1)) + ); + assert_eq!( + drops.landing(at(118.), &widget()), + Some(Landing::At(LEFT, 2)) + ); + let group = dragged(Kind::Group, "unused"); + assert_eq!(drops.landing(at(118.), &group), Some(Landing::At(LEFT, 2))); + } + + #[test] + fn a_panel_over_the_middle_of_a_panel_on_its_own_groups_with_it_and_its_edges_go_beside() { + let drops = drops(); + let with = Some(Landing::With(ContributionId::new("p.alone"))); + + assert_eq!(drops.landing(at(160.), &panel()), with); + assert_eq!( + drops.landing(at(142.), &panel()), + Some(Landing::At(LEFT, 2)) + ); + assert_eq!( + drops.landing(at(178.), &panel()), + Some(Landing::At(LEFT, 3)) + ); + assert_eq!( + drops.landing(at(160.), &widget()), + Some(Landing::At(LEFT, 2)) + ); + } + + #[test] + fn away_from_every_entry_the_nearest_zone_takes_the_drop() { + let drops = drops(); + let right = |index| Some(Landing::At(Slot::Zone(Zone::Right), index)); + + assert_eq!( + drops.landing(at(195.), &widget()), + Some(Landing::At(LEFT, 3)) + ); + assert_eq!( + drops.landing(at(250.), &widget()), + Some(Landing::At(LEFT, 3)) + ); + assert_eq!(drops.landing(at(380.), &widget()), right(0)); + assert_eq!(drops.landing(at(560.), &widget()), right(1)); + assert_eq!(Drops::default().landing(at(10.), &widget()), None); + } + + #[test] + fn the_aim_shows_only_a_drop_that_changes_something_and_ends_with_the_drag() { + let mut drops = drops(); + let placed = Subject::Contribution(ContributionId::new("a.widget")); + let held = Dragged::new(placed.clone(), Kind::Widget, false, Some((LEFT, 0))); + + assert!(drops.aim(Some(at(15.)), &held), "the drag starts"); + assert_eq!(drops.aimed(), None, "in front of itself changes nothing"); + assert_eq!(drops.moving(), Some(&placed)); + assert!(drops.aim(Some(at(160.)), &held)); + assert_eq!(drops.aimed(), Some(&Landing::At(LEFT, 2))); + assert!(!drops.aim(Some(at(158.)), &held), "nothing new to show"); + assert!(drops.aim(None, &held), "away from the preview"); + assert_eq!(drops.aimed(), None); + assert!(drops.settle()); + assert_eq!(drops.moving(), None); + } +} diff --git a/crates/layout-ui/src/face.rs b/crates/layout-ui/src/face.rs new file mode 100644 index 00000000..bba12745 --- /dev/null +++ b/crates/layout-ui/src/face.rs @@ -0,0 +1,194 @@ +//! How an item of the bar looks: one chip, with its icon and, for a widget while there is +//! room, its text; a group of chips on a background of its own; a separator; and the marks of +//! where a drop would land. The preview, the tray and the ghost that follows a drag draw the +//! same boxes, so that a lifted item looks like itself and fills the box it was measured in. + +use gpui_kit::{ + AnyElement, App, Div, IntoElement, ParentElement, SharedString, Styled, TestSupportExt as _, + component::{ActiveTheme as _, Icon, Sizable as _, Size, h_flex}, + div, + prelude::*, + px, +}; +use nocterm_ui::IconName; + +/// How tall a chip is. +pub(crate) const CHIP_HEIGHT: f32 = 28.; +/// The border of a chip, a group and a zone, transparent unless it shows something. +pub(crate) const BORDER: f32 = 1.; +/// The padding of a group around its panels, border included. +pub(crate) const PILL_PADDING: f32 = 2.; +/// How wide the gap between the entries of a zone, or the members of a group, is. +pub(crate) const GAP_WIDTH: f32 = 6.; +/// How thick the line that marks where a drop lands is. +const MARK: f32 = 2.; +/// The id of the one mark of where a drop would land. +const DROP_MARK: &str = "layout-drop-marker"; + +/// What one chip shows. +#[derive(Clone, Debug)] +pub(crate) struct Face { + icon: IconName, + text: Option, + /// A panel that opens with the layout, which the accent marks. + open: bool, +} + +impl Face { + pub(crate) fn new(icon: IconName, text: Option, open: bool) -> Self { + Self { icon, text, open } + } + + /// `chip` drawn as this face: the box every chip has, and its content. + pub(crate) fn fill(self, chip: E, cx: &App) -> E + where + E: ParentElement + Styled + IntoElement, + { + let theme = cx.theme(); + let icon = Icon::new(self.icon) + .with_size(Size::Medium) + .when(self.open, |icon| icon.text_color(theme.primary)); + chip.relative() + .flex_none() + .h(px(CHIP_HEIGHT)) + .gap_1() + .px_1p5() + .items_center() + .rounded(theme.radius) + .border_1() + .border_color(gpui_kit::transparent_black()) + .when(self.open, |chip| { + chip.bg(theme.primary.opacity(0.14)).child( + div() + .absolute() + .bottom(px(1.)) + .left(px(6.)) + .right(px(6.)) + .h(px(2.)) + .rounded_full() + .bg(theme.primary), + ) + }) + .child(icon) + .children(self.text.map(|text| div().text_xs().child(text))) + } +} + +/// The box of a group around its members. An `empty` group shows its title instead. +pub(crate) fn pill(empty: bool, cx: &App) -> Div { + h_flex() + .relative() + .flex_none() + .h(px(CHIP_HEIGHT + 2. * PILL_PADDING)) + .py(px(PILL_PADDING - BORDER)) + .when(empty, |pill| pill.px(px(PILL_PADDING - BORDER))) + .items_center() + .rounded(cx.theme().radius) + .border_1() + .border_color(gpui_kit::transparent_black()) +} + +/// The title an empty group shows. +pub(crate) fn title(title: SharedString, cx: &App) -> Div { + div() + .px_1p5() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(title) +} + +/// A separator between two entries: a short line with a little room on both sides. +pub(crate) fn separator(cx: &App) -> Div { + div() + .flex_none() + .h(px(CHIP_HEIGHT)) + .px(px(2.)) + .flex() + .items_center() + .child(div().w(px(1.)).h(px(14.)).bg(cx.theme().border)) +} + +/// The room between two entries. +pub(crate) fn gap() -> Div { + div() + .relative() + .flex_none() + .w(px(GAP_WIDTH)) + .h(px(CHIP_HEIGHT)) +} + +/// The accent line, in a gap, where the item held over the bar would land. +pub(crate) fn line(cx: &App) -> AnyElement { + div() + .id(DROP_MARK) + .test_support() + .absolute() + .top(px(2.)) + .bottom(px(2.)) + .left(px((GAP_WIDTH - MARK) / 2.)) + .w(px(MARK)) + .rounded_full() + .bg(cx.theme().primary) + .into_any_element() +} + +/// The tint on what the item held over the bar would go into: a panel on its own that it would +/// make a group with, or a group without members. +pub(crate) fn tint(cx: &App) -> AnyElement { + let theme = cx.theme(); + div() + .id(DROP_MARK) + .test_support() + .absolute() + .top_0() + .bottom_0() + .left_0() + .right_0() + .rounded(theme.radius) + .bg(theme.primary.opacity(0.12)) + .border_1() + .border_color(theme.primary) + .into_any_element() +} + +/// What a dragged item looks like. +#[derive(Clone, Debug)] +pub(crate) enum Look { + Chip(Face), + /// A group: its members, `None` for a separator, or the title of an empty one. + Group { + members: Vec>, + title: SharedString, + }, +} + +/// The ghost that follows the pointer: the item as the bar draws it, lifted, filling the box +/// the item was measured in. +pub(crate) fn ghost(look: &Look, cx: &App) -> AnyElement { + let theme = cx.theme(); + let body = match look { + Look::Chip(face) => face.clone().fill(h_flex(), cx), + Look::Group { members, title } if members.is_empty() => { + pill(true, cx).child(self::title(title.clone(), cx)) + } + Look::Group { members, .. } => { + let mut entries = Vec::with_capacity(2 * members.len() + 1); + for member in members { + entries.push(gap().into_any_element()); + entries.push(match member { + Some(face) => face.clone().fill(h_flex(), cx).into_any_element(), + None => separator(cx).into_any_element(), + }); + } + entries.push(gap().into_any_element()); + pill(false, cx).children(entries) + } + }; + body.id("layout-drag-ghost") + .test_support() + .size_full() + .border_color(theme.primary) + .bg(theme.popover) + .shadow_md() + .into_any_element() +} diff --git a/crates/layout-ui/src/fit.rs b/crates/layout-ui/src/fit.rs index 3f5efe5a..06af970d 100644 --- a/crates/layout-ui/src/fit.rs +++ b/crates/layout-ui/src/fit.rs @@ -5,13 +5,20 @@ //! that fits; then with its zones packed side by side; and compact chips scroll sideways when //! even those do not fit. -use std::{cell::Cell, rc::Rc}; +use std::{ + cell::{Cell, RefCell}, + rc::Rc, + sync::Arc, +}; use gpui_kit::{Pixels, px}; use nocterm_plugin_model::{PluginSet, Zone}; use nocterm_window_layout::{ResolvedItem, ResolvedMember}; -use crate::{dnd, snapshot::Snapshot}; +use crate::{ + face::{GAP_WIDTH, PILL_PADDING}, + snapshot::Snapshot, +}; /// What a chip shows. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -54,12 +61,21 @@ impl Plan { } } -/// The width the preview's box was drawn with last time. +/// The width the preview's box was drawn with last time, and the plan made for it. /// /// The estimate of a chip is generous, so that a bar that is said to fit does. #[derive(Clone, Default)] pub(crate) struct Fit { width: Rc>>, + /// The last plan, which holds while the layout, the plugins and the width are the same. + planned: Rc>>, +} + +struct Planned { + snapshot: Rc, + catalog: Arc, + width: Option, + plan: Plan, } /// An average advance of the small text of a chip. @@ -70,8 +86,6 @@ const ICON: f32 = 16.; const CHIP_FRAME: f32 = 14.; /// The gap between the icon and the text of a chip. const INNER_GAP: f32 = 4.; -/// The padding of a group around its panels, border included, and the gap between them. -pub(crate) const PILL_PADDING: f32 = 2.; /// A separator and the room on both sides of it. const SEPARATOR: f32 = 5.; /// The gap between zones. @@ -95,30 +109,49 @@ impl Fit { } /// How to draw the bar in a box as wide as the last one seen. Before any is seen, in full. - pub(crate) fn plan(&self, snapshot: &Snapshot, catalog: &PluginSet) -> Plan { - let Some(width) = self.width.get() else { - return Plan { - density: Density::Full, - spread: Spread::Centred, - scroll: None, - }; - }; - let room = f32::from(width); - let fits = |density, spread| needed(snapshot, catalog, density, spread) <= room; - let (density, spread) = [ - (Density::Full, Spread::Centred), - (Density::Compact, Spread::Centred), - (Density::Compact, Spread::Packed), - ] - .into_iter() - .find(|(density, spread)| fits(*density, *spread)) - .unwrap_or((Density::Compact, Spread::Packed)); - let need = needed(snapshot, catalog, density, spread); - Plan { - density, - spread, - scroll: (need > room).then(|| px(need)), + pub(crate) fn plan(&self, snapshot: &Rc, catalog: &Arc) -> Plan { + let width = self.width.get(); + if let Some(planned) = self.planned.borrow().as_ref() + && Rc::ptr_eq(&planned.snapshot, snapshot) + && Arc::ptr_eq(&planned.catalog, catalog) + && planned.width == width + { + return planned.plan; } + let plan = plan_for(width, snapshot, catalog); + self.planned.replace(Some(Planned { + snapshot: snapshot.clone(), + catalog: catalog.clone(), + width, + plan, + })); + plan + } +} + +fn plan_for(width: Option, snapshot: &Snapshot, catalog: &PluginSet) -> Plan { + let Some(width) = width else { + return Plan { + density: Density::Full, + spread: Spread::Centred, + scroll: None, + }; + }; + let room = f32::from(width); + let fits = |density, spread| needed(snapshot, catalog, density, spread) <= room; + let (density, spread) = [ + (Density::Full, Spread::Centred), + (Density::Compact, Spread::Centred), + (Density::Compact, Spread::Packed), + ] + .into_iter() + .find(|(density, spread)| fits(*density, *spread)) + .unwrap_or((Density::Compact, Spread::Packed)); + let need = needed(snapshot, catalog, density, spread); + Plan { + density, + spread, + scroll: (need > room).then(|| px(need)), } } @@ -160,13 +193,13 @@ fn zone_width(snapshot: &Snapshot, catalog: &PluginSet, zone: Zone, density: Den let title = snapshot.group_title(group).chars().count() as f32 * CHAR; 2. * PILL_PADDING + title + CHIP_FRAME } else { - // The border, and a gap that takes a drop around every member. - 2. + members.iter().sum::() + (members.len() + 1) as f32 * dnd::GAP_WIDTH + // The border, and a gap around every member. + 2. + members.iter().sum::() + (members.len() + 1) as f32 * GAP_WIDTH } } }) .sum(); - let gaps = (items.len() + 1) as f32 * dnd::GAP_WIDTH; + let gaps = (items.len() + 1) as f32 * GAP_WIDTH; (ZONE_BORDER + entries + gaps).max(EMPTY) } diff --git a/crates/layout-ui/src/history.rs b/crates/layout-ui/src/history.rs index 61ad3cbf..63253b44 100644 --- a/crates/layout-ui/src/history.rs +++ b/crates/layout-ui/src/history.rs @@ -1,11 +1,12 @@ -//! One linear undo history of the layout edits made here. +//! Linear snapshot undo with both step and retained-byte budgets. -use nocterm_window_layout::{EditOutcome, LayoutPreset}; +use nocterm_window_layout::{BarItem, EditOutcome, GroupEntry, LayoutPreset}; +use std::collections::VecDeque; -/// The most steps kept either way. const DEPTH: usize = 100; +const MAX_BYTES: usize = 8 * 1024 * 1024; +type Snapshot = (Vec, usize); -/// How an outcome came about, which decides where its `before` goes. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) enum Step { Edit, @@ -13,81 +14,177 @@ pub(crate) enum Step { Redo, } -/// The lists of presets to go back to and to go forward to, and the list this editor last -/// wrote. A list in the settings that is not the one last written was written by someone else, -/// and the steps no longer fit it. +/// History contains complete snapshots only while their aggregate bytes fit the budget. +/// The current document is compared by reference before another copy is retained. #[derive(Debug)] pub(crate) struct History { - undo: Vec>, - redo: Vec>, + undo: VecDeque, + redo: VecDeque, written: Vec, + bytes: usize, } impl History { - /// An empty history over the presets as they are now. pub(crate) fn new(presets: Vec) -> Self { Self { - undo: Vec::new(), - redo: Vec::new(), + undo: VecDeque::new(), + redo: VecDeque::new(), written: presets, + bytes: 0, } } - pub(crate) fn can_undo(&self) -> bool { !self.undo.is_empty() } - pub(crate) fn can_redo(&self) -> bool { !self.redo.is_empty() } - - /// The presets an undo restores. pub(crate) fn previous(&self) -> Option<&Vec> { - self.undo.last() + self.undo.back().map(|(value, _)| value) } - - /// The presets a redo restores. pub(crate) fn next(&self) -> Option<&Vec> { - self.redo.last() + self.redo.back().map(|(value, _)| value) } - /// Records a write of this editor that was stored. pub(crate) fn record(&mut self, step: Step, outcome: &EditOutcome) { match step { Step::Edit => { if outcome.before != outcome.after { - push(&mut self.undo, outcome.before.clone()); - self.redo.clear(); + for (_, bytes) in self.redo.drain(..) { + self.bytes -= bytes; + } + push(&mut self.undo, &mut self.bytes, &outcome.before); } } Step::Undo => { - self.undo.pop(); - push(&mut self.redo, outcome.before.clone()); + pop(&mut self.undo, &mut self.bytes); + push(&mut self.redo, &mut self.bytes, &outcome.before); } Step::Redo => { - self.redo.pop(); - push(&mut self.undo, outcome.before.clone()); + pop(&mut self.redo, &mut self.bytes); + push(&mut self.undo, &mut self.bytes, &outcome.before); + } + } + while self.bytes > MAX_BYTES { + let removed = self.undo.pop_front().or_else(|| self.redo.pop_front()); + if let Some((_, bytes)) = removed { + self.bytes -= bytes; + } else { + break; } } self.written = outcome.after.clone(); } - - /// Forgets every step if `stored` is not what this editor last wrote. Returns whether it - /// did. pub(crate) fn observe(&mut self, stored: &[LayoutPreset]) -> bool { if self.written == stored { return false; } self.undo.clear(); self.redo.clear(); + self.bytes = 0; self.written = stored.to_vec(); true } } +fn pop(steps: &mut VecDeque, bytes: &mut usize) { + if let Some((_, removed)) = steps.pop_back() { + *bytes -= removed; + } +} +fn push(steps: &mut VecDeque, bytes: &mut usize, presets: &[LayoutPreset]) { + let size = snapshot_bytes(presets); + if size > MAX_BYTES { + for (_, removed) in steps.drain(..) { + *bytes -= removed; + } + return; + } + steps.push_back((presets.to_vec(), size)); + *bytes += size; + if steps.len() > DEPTH + && let Some((_, removed)) = steps.pop_front() + { + *bytes -= removed; + } +} +/// Counts allocations made by cloning this document, including all nested owned strings. +fn snapshot_bytes(presets: &[LayoutPreset]) -> usize { + let mut bytes = std::mem::size_of_val(presets); + for preset in presets { + bytes += preset.id.as_str().len() + preset.name.len(); + for ids in [&preset.tray, &preset.open] { + bytes += std::mem::size_of_val(ids.as_slice()); + bytes += ids.iter().map(|id| id.as_str().len()).sum::(); + } + for zone in [&preset.bar.left, &preset.bar.center, &preset.bar.right] { + bytes += std::mem::size_of_val(zone.as_slice()); + for item in zone { + bytes += match item { + BarItem::Item { id } | BarItem::Panel { id, .. } => id.as_str().len(), + BarItem::Separator => 0, + BarItem::Group { + id, name, members, .. + } => { + id.as_str().len() + + name.as_ref().map_or(0, String::len) + + std::mem::size_of_val(members.as_slice()) + + members + .iter() + .map(|member| match member { + GroupEntry::Panel(id) => id.as_str().len(), + GroupEntry::Separator => 0, + }) + .sum::() + } + }; + } + } + } + bytes +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_window_layout::PresetId; + + #[test] + fn a_snapshot_larger_than_the_byte_budget_is_not_offered_for_undo() { + let before = vec![LayoutPreset { + name: "large".repeat(MAX_BYTES / 5 + 1), + ..Default::default() + }]; + let mut history = History::new(before.clone()); + history.record( + Step::Edit, + &EditOutcome { + before, + after: Vec::new(), + selected: PresetId::new("default"), + }, + ); + assert!(!history.can_undo()); + assert!(history.previous().is_none()); + } -fn push(steps: &mut Vec>, presets: Vec) { - steps.push(presets); - if steps.len() > DEPTH { - steps.remove(0); + #[test] + fn an_unrelated_publication_keeps_the_same_undo_step() { + let before = vec![LayoutPreset { + name: "Original".into(), + ..Default::default() + }]; + let mut after = before.clone(); + after[0].name = "Changed".into(); + let mut history = History::new(before.clone()); + history.record( + Step::Edit, + &EditOutcome { + before: before.clone(), + after: after.clone(), + selected: PresetId::new("default"), + }, + ); + assert!(!history.observe(&after)); + assert_eq!(history.previous(), Some(&before)); } } diff --git a/crates/layout-ui/src/lib.rs b/crates/layout-ui/src/lib.rs index 942e9b8c..a0bc0d43 100644 --- a/crates/layout-ui/src/lib.rs +++ b/crates/layout-ui/src/lib.rs @@ -4,9 +4,9 @@ //! //! Owns: the section's view and its state (the layout shown, the selected item, undo and redo, //! renaming a layout or a group and confirming a delete), the drags, menus and keys that edit -//! the bar (a drop names a place; the rules of the move stay in `nocterm-window-layout`), how -//! the bar preview fits its one line, and what a sent edit led to (the notice, the selection -//! that follows it, the history step). +//! the bar (a drop names a place, found from the pointer against what the preview painted; the +//! rules of the move stay in `nocterm-window-layout`), how the bar preview fits its one line, +//! and what a sent edit led to (the notice, the selection that follows it, the history step). //! Does not own: the rules of layouts and their edits (`nocterm-window-layout`), the write of //! an edit (`nocterm_workspace::edit_layouts`, which refuses a `[layouts]` table that does not //! read), what the plugins are or whether they are on (`nocterm-plugin-model`, @@ -23,9 +23,15 @@ //! the write, never a guess; editing the layout in use changes the footer at once; editing a //! built-in layout in use saves a custom copy that stays in use; history is dropped when //! anything else changes the layouts. +//! Undo is bounded to 100 steps and 8 MiB of cloned snapshots per editor. The layout shown is +//! resolved once for each settings revision, set of plugins and layout picked, and its bar is +//! fitted once for each width, so a drag, which draws the window on every move, redraws cheaply. +mod about; mod commands; mod dnd; +mod drops; +mod face; mod fit; mod history; mod keys; diff --git a/crates/layout-ui/src/menu.rs b/crates/layout-ui/src/menu.rs index 939d02b9..7febb5d2 100644 --- a/crates/layout-ui/src/menu.rs +++ b/crates/layout-ui/src/menu.rs @@ -1,8 +1,8 @@ //! What one item of the bar offers in its menu: move it, group it, separate it, hide it or add -//! it to the bar. The list is plain data, so the preview and the tray build the same menu and a -//! test reads it without opening one. +//! it to the bar. The list is plain data, built when a menu opens, so the preview and the tray +//! build the same menu and a test reads it without opening one. -use std::{cell::Cell, rc::Rc}; +use std::{cell::RefCell, rc::Rc, sync::Arc}; use gpui_kit::{ App, Context, SharedString, Window, @@ -76,34 +76,44 @@ impl Ask { } } -/// The menu of one subject, drawn from the layout as it is now. -pub(crate) struct ItemMenu { - entries: Vec, - commands: Commands, +/// What a menu is opened on. +#[derive(Clone, Debug)] +pub(crate) enum MenuOf { + Item(Subject), + /// The separator at `index` of a list. + Separator(Slot, usize), } -impl ItemMenu { - pub(crate) fn new( - subject: &Subject, - snapshot: &Snapshot, - catalog: &PluginSet, - commands: Commands, - ) -> Rc { - Self::of(entries(subject, snapshot, catalog), commands) - } +/// The menus of the items of the layout as it was drawn, each built when it opens. +#[derive(Clone)] +pub(crate) struct Menus { + snapshot: Rc, + catalog: Arc, + commands: Commands, +} - pub(crate) fn of(entries: Vec, commands: Commands) -> Rc { - Rc::new(Self { entries, commands }) +impl Menus { + pub(crate) fn new(snapshot: Rc, catalog: Arc, commands: Commands) -> Self { + Self { + snapshot, + catalog, + commands, + } } - /// Fills a popup with the entries; a click sends the operation to the section. + /// Fills a popup with the entries of `of`; a click sends the operation to the section. pub(crate) fn popup( &self, + of: &MenuOf, menu: PopupMenu, window: &mut Window, cx: &mut Context, ) -> PopupMenu { - fill(menu, &self.entries, &self.commands, window, cx) + let entries = match of { + MenuOf::Item(subject) => entries(subject, &self.snapshot, &self.catalog), + MenuOf::Separator(place, index) => separator(place.clone(), *index, &self.snapshot), + }; + fill(menu, &entries, &self.commands, window, cx) } } @@ -111,15 +121,15 @@ impl ItemMenu { /// every right click within it; a panel or separator of the group claims that click on the way /// down, and its own menu opens instead. #[derive(Clone, Default)] -pub(crate) struct Claim(Rc>>>); +pub(crate) struct Claim(Rc>>); impl Claim { - pub(crate) fn claim(&self, menu: Rc) { - self.0.set(Some(menu)); + pub(crate) fn claim(&self, of: MenuOf) { + self.0.replace(Some(of)); } - /// The menu a member claimed for this click, or the group's `own`. - pub(crate) fn take_or(&self, own: &Rc) -> Rc { + /// What a member claimed for this click, or the group's `own`. + pub(crate) fn take_or(&self, own: &MenuOf) -> MenuOf { self.0.take().unwrap_or_else(|| own.clone()) } } diff --git a/crates/layout-ui/src/preview.rs b/crates/layout-ui/src/preview.rs index e546ce99..d009aa61 100644 --- a/crates/layout-ui/src/preview.rs +++ b/crates/layout-ui/src/preview.rs @@ -1,41 +1,48 @@ //! The status bar as it will look, on one line, where it is edited: the left zone flush left, //! the centre in the middle and the right zone flush right, drawn from the preview text of each -//! widget. +//! widget. A group has a background of its own, and the panel that opens with the layout is +//! marked with the accent. //! -//! Every chip is a drag source and a drop target, and opens its menu on a right click. Between -//! the entries of a zone is a gap that takes a drop as well, so a panel can stand on its own in -//! front of another rather than join it. The line never wraps: when it is too wide the chips -//! lose their text and keep their icons, and when even those do not fit it scrolls sideways. +//! Every chip and group is a drag source and opens its menu, built when it opens, on a right +//! click. The whole preview takes a drop, where `drops` says it lands; while an item is held +//! over it, one accent line in a gap shows where it would go, or a tint the panel it would make +//! a group with. The line never wraps: when it is too wide the chips lose their text and keep +//! their icons, and when even those do not fit it scrolls sideways. use std::{rc::Rc, sync::Arc}; use gpui_kit::{ - AnyElement, App, FocusHandle, InteractiveElement, IntoElement, MouseButton, ParentElement, - RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, TestSupportExt as _, Window, + AnyElement, App, Entity, FocusHandle, InteractiveElement, IntoElement, MouseButton, + ParentElement, Pixels, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, + TestSupportExt as _, Toggled, Window, base::ElementExt as _, - component::{ - ActiveTheme as _, Icon, Sizable as _, h_flex, menu::ContextMenuExt as _, tooltip::Tooltip, - }, + component::{ActiveTheme as _, h_flex, menu::ContextMenuExt as _, tooltip::Tooltip}, div, prelude::*, px, }; -use nocterm_plugin_model::{ - ContributionId, ContributionKind, ContributionRef, PluginSet, PluginTier, Zone, -}; -use nocterm_ui::contribution_icon; +use nocterm_plugin_model::{ContributionId, ContributionKind, ContributionRef, PluginSet, Zone}; +use nocterm_ui::{DragSource, contribution_icon}; use nocterm_window_layout::{ResolvedGroup, ResolvedItem, ResolvedMember, Slot, Subject}; use crate::{ + about, commands::Commands, - dnd::{self, CHIP, CHIP_HEIGHT, Dragged, GAP_WIDTH, Landing, PILL}, - fit::{Density, EMPTY, Fit, PILL_PADDING, Spread}, + dnd::{self, Dragged, Landing}, + drops::{self, Drops, Holds, Spot}, + face::{self, BORDER, CHIP_HEIGHT, Face, Look, PILL_PADDING}, + fit::{Density, EMPTY, Fit, Spread}, keys, - menu::{self, Claim, ItemMenu}, + menu::{Claim, MenuOf, Menus}, snapshot::Snapshot, user_facts::SharedFacts, }; +/// How visible the item being dragged stays in its place. +const MOVING: f32 = 0.4; +/// How much a chip under the pointer darkens, or lightens in a dark theme. +const HOVER: f32 = 0.07; + #[derive(IntoElement)] pub(crate) struct Preview { snapshot: Rc, @@ -47,15 +54,22 @@ pub(crate) struct Preview { commands: Commands, } -/// How a chip is drawn and what a drop on it does. -struct Setting { +/// One drawing of the preview, and the drag held over it. +struct Bar<'a> { + preview: &'a Preview, density: Density, - /// Where a drop on the chip lands. - landing: Landing, - /// Whether a drop on the chip as a whole tints it: a panel on its own, which a dropped - /// panel makes a group with. - fills: bool, - /// The group the chip is drawn in, which opens the chip's menu for it. + menus: Menus, + /// Where the entries painted are recorded; `None` when the layout shown takes no drops. + drops: Option>, + /// Where the item held over the preview would land. + aimed: Option, + /// The item being dragged. + moving: Option, +} + +/// Where a chip is, and the group it is drawn in, which opens the chip's menu for it. +struct Setting { + spot: Spot, host: Option, } @@ -79,16 +93,24 @@ impl Preview { commands, } } +} - fn zone(&self, zone: Zone, density: Density, spread: Spread, cx: &App) -> AnyElement { - let items = self.snapshot.layout().zone(zone); +impl Bar<'_> { + fn snapshot(&self) -> &Snapshot { + &self.preview.snapshot + } + + fn zone(&self, zone: Zone, spread: Spread, cx: &App) -> AnyElement { + let items = self.snapshot().layout().zone(zone); let name = name(zone); + let place = Slot::Zone(zone); + let gap_id = |index| move || format!("layout-preview-gap:{name}:{index}"); let mut entries = Vec::with_capacity(2 * items.len() + 1); for (index, item) in items.iter().enumerate() { - entries.push(self.gap(zone, index, cx)); - entries.push(self.item(zone, index, item, density, cx)); + entries.push(self.gap(gap_id(index), &place, index, cx)); + entries.push(self.item(zone, index, item, cx)); } - entries.push(self.gap(zone, items.len(), cx)); + entries.push(self.gap(gap_id(items.len()), &place, items.len(), cx)); let row = h_flex() .id(SharedString::from(format!("layout-preview-zone:{name}"))) .test_support() @@ -99,15 +121,14 @@ impl Preview { .border_1() .border_color(gpui_kit::transparent_black()) .children(entries); - let row = if self.snapshot.editable() { - dnd::target( - row, - Landing::zone_area(zone, items.len()), - true, - self.commands.clone(), - ) - } else { - row + let row = match &self.drops { + Some(drops) => { + let drops = drops.clone(); + dnd::measured(row, BORDER, move |bounds, _, cx| { + drops.update(cx, |drops, _| drops.zone(zone, bounds)); + }) + } + None => row, }; match (zone, spread) { (Zone::Left, Spread::Centred) => row.flex_1().justify_start(), @@ -117,41 +138,36 @@ impl Preview { .into_any_element() } - /// The room in front of the entry at `index` of `zone`, or after the last one. - fn gap(&self, zone: Zone, index: usize, cx: &App) -> AnyElement { - if !self.snapshot.editable() { - return div().flex_none().w(px(GAP_WIDTH)).into_any_element(); + /// The room in front of the entry at `index` of `place`, or after the last one, which + /// shows the line where a drop would land. + fn gap(&self, id: impl FnOnce() -> String, place: &Slot, index: usize, cx: &App) -> AnyElement { + let marked = self + .aimed + .as_ref() + .is_some_and(|aimed| aimed.is_at(place, index)); + let gap = face::gap().when(marked, |gap| gap.child(face::line(cx))); + if self.drops.is_some() { + gap.id(SharedString::from(id())) + .test_support() + .into_any_element() + } else { + gap.into_any_element() } - dnd::gap( - SharedString::from(format!("layout-preview-gap:{}:{index}", name(zone))), - Landing::in_zone(zone, index), - self.commands.clone(), - cx, - ) } - fn item( - &self, - zone: Zone, - index: usize, - item: &ResolvedItem, - density: Density, - cx: &App, - ) -> AnyElement { + fn item(&self, zone: Zone, index: usize, item: &ResolvedItem, cx: &App) -> AnyElement { + let spot = |holds| Spot::new(Slot::Zone(zone), index, holds); match item { ResolvedItem::Widget(id) => { let setting = Setting { - density, - landing: Landing::in_zone(zone, index), - fills: false, + spot: spot(Holds::Item), host: None, }; self.contribution(id, setting, cx) } ResolvedItem::Separator => self.separator( SharedString::from(format!("layout-preview-separator:{}:{index}", name(zone))), - Slot::Zone(zone), - index, + spot(Holds::Item), None, cx, ), @@ -160,49 +176,40 @@ impl Preview { return div().into_any_element(); }; let setting = Setting { - density, - landing: Landing::beside(panel.clone(), zone, index), - fills: true, + spot: spot(Holds::Alone(panel.clone())), host: None, }; self.contribution(panel, setting, cx) } - ResolvedItem::Group(group) => self.group(group, zone, index, density, cx), + ResolvedItem::Group(group) => { + self.group(group, spot(Holds::Group(group.id().clone())), cx) + } } } fn contribution(&self, id: &ContributionId, setting: Setting, cx: &App) -> AnyElement { - match self.catalog.contribution(id) { + match self.preview.catalog.contribution(id) { Some(contribution) => self.chip(contribution, setting, cx), None => div().into_any_element(), } } - /// A group: its panels on one filled background, without a title. - fn group( - &self, - group: &ResolvedGroup, - zone: Zone, - index: usize, - density: Density, - cx: &App, - ) -> AnyElement { + /// A group: its panels on a background of their own, without a title. + fn group(&self, group: &ResolvedGroup, spot: Spot, cx: &App) -> AnyElement { let subject = Subject::Group(group.id().clone()); - let selected = self.selected.as_ref() == Some(&subject); - let title: SharedString = self.snapshot.group_title(group).into(); + let selected = self.preview.selected.as_ref() == Some(&subject); + let title: SharedString = self.snapshot().group_title(group).into(); let claim = Claim::default(); - let members = self.members(group, density, &claim, cx); + let members = self.members(group, &claim, cx); let empty = members.is_empty(); - let menu = ItemMenu::new( - &subject, - &self.snapshot, - &self.catalog, - self.commands.clone(), - ); - let commands = self.commands.clone(); + let joined = empty + && self + .aimed + .as_ref() + .is_some_and(|aimed| aimed.is_at(&Slot::Group(group.id().clone()), 0)); + let (commands, picked) = (self.preview.commands.clone(), subject.clone()); let theme = cx.theme(); - let tooltip = title.clone(); - let pill = h_flex() + let pill = face::pill(empty, cx) .id(SharedString::from(format!( "layout-preview-group:{}", group.id() @@ -210,256 +217,231 @@ impl Preview { .aria_label(title.clone()) .aria_selected(selected) .test_support() - .group(PILL) - .relative() - .flex_none() - .h(px(CHIP_HEIGHT + 2. * PILL_PADDING)) - .py(px(PILL_PADDING - 1.)) - .when(empty, |pill| pill.px(px(PILL_PADDING - 1.))) - .items_center() - .rounded(theme.radius) - .bg(theme.secondary) - .border_1() - .border_color(if selected { - theme.primary - } else { - gpui_kit::transparent_black() + .bg(theme.secondary_active) + .when(selected, |pill| pill.border_color(theme.primary)) + .when(self.moving.as_ref() == Some(&subject), |pill| { + pill.opacity(MOVING) }) .cursor_pointer() .on_click(move |_, window, cx| { - let subject = subject.clone(); - commands.run(window, cx, move |section, window, cx| { - section.pick(Some(subject), window, cx); - }); + // A release that ends a drag started on a member is the drop, not a click. + if !cx.has_active_drag() { + pick(&commands, picked.clone(), window, cx); + } }) .when(empty, |pill| { - pill.child( - div() - .px_1p5() - .text_xs() - .text_color(theme.muted_foreground) - .child(title.clone()), - ) - .tooltip(move |_, cx| cx.new(|_| Tooltip::new(tooltip.clone())).into()) + let tooltip = title.clone(); + pill.child(face::title(title, cx)) + .tooltip(move |_, cx| cx.new(|_| Tooltip::new(tooltip.clone())).into()) }) - .children(members); - let pill = if self.snapshot.editable() { - let landing = Landing::in_zone(zone, index) - .and_group_end(group.id().clone(), group.members().len()); - let pill = pill.child(dnd::marker( - SharedString::from(format!("layout-preview-marker:group:{}", group.id())), - PILL, - landing.clone(), - cx, - )); - let pill = dnd::target(pill, landing, true, self.commands.clone()); - dnd::source(pill, Dragged::group(group.id(), title, &self.snapshot)) - } else { - pill + .children(members) + .when(joined, |pill| pill.child(face::tint(cx))); + let pill = match &self.drops { + Some(drops) => { + let source = DragSource::default(); + let dragged = Dragged::group(group.id(), spot.from()); + let pill = painted(pill, spot, Some(source.clone()), drops, BORDER); + let (snapshot, catalog) = + (self.preview.snapshot.clone(), self.preview.catalog.clone()); + let id = group.id().clone(); + dnd::source(pill, dragged, source, move || { + group_look(&snapshot, &catalog, &id) + }) + } + None => pill, }; - pill.context_menu(move |popup, window, cx| claim.take_or(&menu).popup(popup, window, cx)) - .into_any_element() + let (menus, own) = (self.menus.clone(), MenuOf::Item(subject)); + pill.context_menu(move |popup, window, cx| { + menus.popup(&claim.take_or(&own), popup, window, cx) + }) + .into_any_element() } - /// The panels and separators of a group. - fn members( - &self, - group: &ResolvedGroup, - density: Density, - claim: &Claim, - cx: &App, - ) -> Vec { + /// The panels and separators of a group, with the gaps around them. + fn members(&self, group: &ResolvedGroup, claim: &Claim, cx: &App) -> Vec { let members = group.members(); if members.is_empty() { return Vec::new(); } + let place = Slot::Group(group.id().clone()); + let gap_id = |index| move || format!("layout-preview-member-gap:{}:{index}", group.id()); let mut entries = Vec::with_capacity(2 * members.len() + 1); for (member, entry) in members.iter().enumerate() { - entries.push(self.member_gap(group, member, cx)); - entries.push(self.member(group, member, entry, density, claim, cx)); + entries.push(self.gap(gap_id(member), &place, member, cx)); + let spot = Spot::new(place.clone(), member, Holds::Item); + entries.push(match entry { + ResolvedMember::Panel(id) => { + let host = Some(claim.clone()); + self.contribution(id, Setting { spot, host }, cx) + } + ResolvedMember::Separator => self.separator( + SharedString::from(format!( + "layout-preview-member-separator:{}:{member}", + group.id() + )), + spot, + Some(claim), + cx, + ), + }); } - entries.push(self.member_gap(group, members.len(), cx)); + entries.push(self.gap(gap_id(members.len()), &place, members.len(), cx)); entries } - /// The room in front of the member at `index` of `group`, or after the last one. - fn member_gap(&self, group: &ResolvedGroup, index: usize, cx: &App) -> AnyElement { - if !self.snapshot.editable() { - return div().flex_none().w(px(GAP_WIDTH)).into_any_element(); - } - dnd::gap( - SharedString::from(format!("layout-preview-member-gap:{}:{index}", group.id())), - Landing::in_group(group.id().clone(), index), - self.commands.clone(), - cx, - ) - } - - fn member( - &self, - group: &ResolvedGroup, - member: usize, - entry: &ResolvedMember, - density: Density, - claim: &Claim, - cx: &App, - ) -> AnyElement { - match entry { - ResolvedMember::Panel(id) => { - let setting = Setting { - density, - landing: Landing::in_group(group.id().clone(), member), - fills: false, - host: Some(claim.clone()), - }; - self.contribution(id, setting, cx) - } - ResolvedMember::Separator => self.separator( - SharedString::from(format!( - "layout-preview-member-separator:{}:{member}", - group.id() - )), - Slot::Group(group.id().clone()), - member, - Some(claim), - cx, - ), - } - } - fn separator( &self, id: SharedString, - place: Slot, - index: usize, + spot: Spot, host: Option<&Claim>, cx: &App, ) -> AnyElement { - let menu = ItemMenu::of( - menu::separator(place, index, &self.snapshot), - self.commands.clone(), - ); - let line = div() - .id(id) - .test_support() - .flex_none() - .h(px(CHIP_HEIGHT)) - .px(px(2.)) - .flex() - .items_center() - .child(div().w(px(1.)).h(px(12.)).bg(cx.theme().border)); - with_menu(line, menu, host) + let (place, index) = spot.from(); + let line = face::separator(cx).id(id).test_support(); + let line = match &self.drops { + Some(drops) => painted(line, spot, None, drops, 0.), + None => line, + }; + with_menu(line, &self.menus, MenuOf::Separator(place, index), host) } /// A widget or a panel toggle. A widget shows its sample text while the density allows; /// otherwise the chip is its icon and the tooltip names it. fn chip(&self, contribution: ContributionRef<'_>, setting: Setting, cx: &App) -> AnyElement { + let snapshot = self.snapshot(); let id = contribution.id().clone(); let subject = Subject::Contribution(id.clone()); - let selected = self.selected.as_ref() == Some(&subject); - let disabled = self.snapshot.is_disabled(&id, &self.catalog); - let text = contribution - .preview() - .unwrap_or(contribution.title()) - .to_owned(); - let shown = (setting.density == Density::Full - && contribution.kind() == ContributionKind::Widget) - .then_some(text); - let commands = self.commands.clone(); - let menu = ItemMenu::new(&subject, &self.snapshot, &self.catalog, commands.clone()); - let theme = cx.theme(); + let selected = self.preview.selected.as_ref() == Some(&subject); + let disabled = snapshot.is_disabled(&id, &self.preview.catalog); + let panel = contribution.kind() == ContributionKind::Panel; + let open = panel && snapshot.opens_with_layout(&id); + let text = (self.density == Density::Full && !panel).then(|| { + SharedString::from( + contribution + .preview() + .unwrap_or(contribution.title()) + .to_owned(), + ) + }); + let face = Face::new(contribution_icon(contribution.icon()), text, open); let name: SharedString = if disabled { format!("{} (disabled)", contribution.title()).into() } else { contribution.title().to_owned().into() }; - let about = self.about(contribution, name.clone(), cx); - let chip = h_flex() + let grouped = matches!(&self.aimed, Some(Landing::With(target)) if *target == id); + let (commands, picked) = (self.preview.commands.clone(), subject.clone()); + let theme = cx.theme(); + let chip = face + .clone() + .fill(h_flex(), cx) .id(SharedString::from(format!("layout-preview:{id}"))) - .aria_label(name) + .aria_label(name.clone()) .aria_selected(selected) + .when(panel, |chip| { + chip.aria_toggled(if open { Toggled::True } else { Toggled::False }) + }) .test_support() - .group(CHIP) - .relative() - .flex_none() - .h(px(CHIP_HEIGHT)) - .gap_1() - .px_1p5() - .items_center() - .rounded(theme.radius) - .border_1() - .border_color(if selected { - theme.primary - } else { - gpui_kit::transparent_black() + .when(selected, |chip| chip.border_color(theme.primary)) + .when(!open, |chip| { + chip.hover(|style| style.bg(theme.foreground.opacity(HOVER))) }) - .hover(|style| style.bg(theme.secondary_hover)) .when(disabled, |chip| chip.opacity(0.5)) + .when(self.moving.as_ref() == Some(&subject), |chip| { + chip.opacity(MOVING) + }) .cursor_pointer() .on_click(move |_, window, cx| { - // The group around a panel is not picked by the same click. - cx.stop_propagation(); - let subject = subject.clone(); - commands.run(window, cx, move |section, window, cx| { - section.pick(Some(subject), window, cx); - }); + // A release that ends a drag is the drop; the group around a panel is not + // picked by the same click. + if !cx.has_active_drag() { + cx.stop_propagation(); + pick(&commands, picked.clone(), window, cx); + } }) - .child(Icon::new(contribution_icon(contribution.icon())).small()) - .children(shown.map(|text| div().text_xs().child(text))) - .tooltip(move |_, cx| cx.new(|_| Tooltip::new(about.clone())).into()); - let chip = if self.snapshot.editable() { - let chip = chip.child(dnd::marker( - SharedString::from(format!("layout-preview-marker:{id}")), - CHIP, - setting.landing.clone(), - cx, - )); - let chip = dnd::target(chip, setting.landing, setting.fills, self.commands.clone()); - dnd::source(chip, Dragged::contribution(contribution, &self.snapshot)) - } else { - chip + .tooltip(about::tooltip( + &self.preview.catalog, + self.preview.facts.as_ref(), + &id, + name, + open, + )) + .when(grouped, |chip| chip.child(face::tint(cx))); + let chip = match &self.drops { + Some(drops) => { + let source = DragSource::default(); + let dragged = Dragged::contribution(contribution, Some(setting.spot.from())); + let chip = painted(chip, setting.spot, Some(source.clone()), drops, BORDER); + dnd::source(chip, dragged, source, move || Look::Chip(face.clone())) + } + None => chip, }; - with_menu(chip, menu, setting.host.as_ref()) + with_menu( + chip, + &self.menus, + MenuOf::Item(subject), + setting.host.as_ref(), + ) } +} - /// The tooltip of a chip: its name and, for an item of a user plugin, what the host knows - /// of the plugin, in the host's own words. - fn about( - &self, - contribution: ContributionRef<'_>, - name: SharedString, - cx: &App, - ) -> SharedString { - let Some(plugin) = self - .catalog - .plugin_of(contribution.id()) - .filter(|plugin| plugin.tier() == PluginTier::User) - else { - return name; - }; - let mut lines = vec![ - name.to_string(), - format!("Plugin \u{b7} {} ({})", plugin.name(), plugin.id()), - ]; - if let Some(facts) = self - .facts - .as_ref() - .and_then(|facts| facts.facts(plugin.id(), cx)) - { - lines.push(format!("Publisher: {} (unverified)", facts.publisher)); - lines.push(format!("Fingerprint: {}", facts.fingerprint)); - lines.push(if facts.capabilities.is_empty() { - "Allowed: nothing".to_owned() - } else { - format!("Allowed: {}", facts.capabilities.join("; ")) - }); - } - lines.join("\n").into() +/// What the ghost of the group `id` shows: its panels, or its title while it has none. +fn group_look( + snapshot: &Snapshot, + catalog: &PluginSet, + id: &nocterm_plugin_model::GroupId, +) -> Look { + let group = snapshot.group(id); + let members = group.map_or_else(Vec::new, |group| { + group + .members() + .iter() + .map(|member| match member { + ResolvedMember::Panel(panel) => catalog.contribution(panel).map(|contribution| { + let open = snapshot.opens_with_layout(panel); + Face::new(contribution_icon(contribution.icon()), None, open) + }), + ResolvedMember::Separator => None, + }) + .collect() + }); + let title = group + .map(|group| snapshot.group_title(group)) + .unwrap_or_default(); + Look::Group { + members, + title: title.into(), } } -/// `element` opening `menu` on a right click: by itself, or through the group it is drawn in. -fn with_menu(element: E, menu: Rc, host: Option<&Claim>) -> AnyElement +/// `element` recording, each time it is painted, where it is as `spot` in `drops`, and +/// measuring itself for the ghost of `source`. +fn painted( + element: E, + spot: Spot, + source: Option, + drops: &Entity, + border: f32, +) -> E { + let drops = drops.clone(); + dnd::measured(element, border, move |bounds, window, cx| { + if let Some(source) = &source { + source.capture(bounds, window); + } + drops.update(cx, |drops, _| drops.spot(spot, bounds)); + }) +} + +/// Selects `subject` for the keys of the editor. +fn pick(commands: &Commands, subject: Subject, window: &mut Window, cx: &mut App) { + commands.run(window, cx, move |section, window, cx| { + section.pick(Some(subject), window, cx); + }); +} + +/// `element` opening the menu `of` on a right click: by itself, or through the group it is +/// drawn in. +fn with_menu(element: E, menus: &Menus, of: MenuOf, host: Option<&Claim>) -> AnyElement where E: InteractiveElement + ParentElement + Styled + IntoElement + 'static, { @@ -467,12 +449,15 @@ where Some(host) => { let host = host.clone(); element - .on_mouse_down(MouseButton::Right, move |_, _, _| host.claim(menu.clone())) + .on_mouse_down(MouseButton::Right, move |_, _, _| host.claim(of.clone())) + .into_any_element() + } + None => { + let menus = menus.clone(); + element + .context_menu(move |popup, window, cx| menus.popup(&of, popup, window, cx)) .into_any_element() } - None => element - .context_menu(move |popup, window, cx| menu.popup(popup, window, cx)) - .into_any_element(), } } @@ -484,30 +469,71 @@ fn name(zone: Zone) -> &'static str { } } -impl RenderOnce for Preview { - fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { - let plan = self.fit.plan(&self.snapshot, &self.catalog); - let (density, spread, scroll) = (plan.density(), plan.spread(), plan.scroll()); - let (fit, commands, keys) = ( - self.fit.clone(), - self.commands.clone(), - self.commands.clone(), +impl<'a> Bar<'a> { + /// A drawing of `preview` at `density`, with the drag held over it when the layout takes + /// drops. A drag that ended since the last drawing is forgotten. + fn new(preview: &'a Preview, density: Density, window: &mut Window, cx: &mut App) -> Self { + let drops = preview + .snapshot + .editable() + .then(|| window.use_keyed_state("layout-preview-drops", cx, |_, _| Drops::default())); + let held = cx.has_active_drag(); + let (aimed, moving) = drops.as_ref().map_or((None, None), |drops| { + drops.update(cx, |drops, _| { + if !held { + drops.settle(); + } + drops.repaint(); + (drops.aimed().cloned(), drops.moving().cloned()) + }) + }); + let menus = Menus::new( + preview.snapshot.clone(), + preview.catalog.clone(), + preview.commands.clone(), ); - let theme = cx.theme(); + Self { + preview, + density, + menus, + drops, + aimed, + moving, + } + } + + /// The three zones on one line, as wide as the box, or as the bar needs when it scrolls. + fn line(&self, spread: Spread, scroll: Option, cx: &App) -> AnyElement { let line = h_flex() .id("layout-preview-line") .test_support() .items_center() .gap_x_3() .when(spread == Spread::Packed, |line| line.justify_between()) - .child(self.zone(Zone::Left, density, spread, cx)) - .child(self.zone(Zone::Center, density, spread, cx)) - .child(self.zone(Zone::Right, density, spread, cx)); - let line = match scroll { + .child(self.zone(Zone::Left, spread, cx)) + .child(self.zone(Zone::Center, spread, cx)) + .child(self.zone(Zone::Right, spread, cx)); + match scroll { Some(width) => line.w(width).flex_none(), None => line.w_full(), - }; - div() + } + .into_any_element() + } +} + +impl RenderOnce for Preview { + fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { + let plan = self.fit.plan(&self.snapshot, &self.catalog); + let scroll = plan.scroll(); + let bar = Bar::new(&self, plan.density(), window, cx); + let (line, drops) = (bar.line(plan.spread(), scroll, cx), bar.drops.clone()); + let (fit, commands, keys) = ( + self.fit.clone(), + self.commands.clone(), + self.commands.clone(), + ); + let theme = cx.theme(); + let preview = div() .id("layout-preview") .test_support() .track_focus(&self.focus) @@ -538,6 +564,10 @@ impl RenderOnce for Preview { commands.run(window, cx, |_, _, cx| cx.notify()); }); } - }) + }); + match drops { + Some(drops) => drops::target(preview, drops, self.commands.clone()), + None => preview, + } } } diff --git a/crates/layout-ui/src/section.rs b/crates/layout-ui/src/section.rs index 1a0a4a9c..2488b645 100644 --- a/crates/layout-ui/src/section.rs +++ b/crates/layout-ui/src/section.rs @@ -26,7 +26,7 @@ use crate::{ preview::Preview, rename::{RenameDraft, Renamed}, selection::Selection, - snapshot::Snapshot, + snapshot::{Snapshot, Snapshots}, tray::Tray, user_facts::SharedFacts, }; @@ -68,7 +68,10 @@ pub struct LayoutEditorSection { focus: FocusHandle, /// The width the bar preview was drawn with, which decides how much of it is shown. fit: Fit, + /// What is drawn of the layout shown, until the settings, the plugins or the choice change. + snapshots: Snapshots, _subscriptions: Vec, + observed: (Option, PluginEnablement), } impl LayoutEditorSection { @@ -94,6 +97,10 @@ impl LayoutEditorSection { }), cx, ); + let observed = ( + settings.setting::(cx).active.clone(), + settings.setting::(cx).clone(), + ); Self { settings, source, @@ -107,18 +114,26 @@ impl LayoutEditorSection { notice: None, focus: cx.focus_handle(), fit: Fit::default(), + snapshots: Snapshots::default(), _subscriptions: vec![changed, plugins], + observed, } } - pub(crate) fn snapshot(&self, cx: &App) -> Snapshot { - Snapshot::new( - self.settings.setting::(cx), - self.settings.setting::(cx), - self.legacy_swapped, - self.selection.shown(), - &self.catalog, - ) + /// What the editor shows of the layout shown, built once for each state of the settings, + /// the plugins and the layout picked. + pub(crate) fn snapshot(&mut self, cx: &App) -> Rc { + let (settings, catalog, shown) = (&self.settings, &self.catalog, self.selection.shown()); + self.snapshots + .get(settings.revision(cx), catalog, shown, || { + Snapshot::new( + settings.setting::(cx), + settings.setting::(cx), + self.legacy_swapped, + shown, + catalog, + ) + }) } pub(crate) fn mode(&self) -> &Mode { @@ -306,13 +321,22 @@ impl LayoutEditorSection { /// Forgets the history when the layouts changed by someone else, once no write of ours is /// on its way. fn stored_changed(&mut self, cx: &mut Context) { + let active = self.settings.setting::(cx).active.clone(); + let enablement = self.settings.setting::(cx); + let mut changed = self.observed.0 != active || self.observed.1 != *enablement; + if changed { + self.observed = (active, enablement.clone()); + } if self.pending == 0 { - let presets = self.settings.setting::(cx).presets.clone(); - if self.history.observe(&presets) { + let presets = &self.settings.setting::(cx).presets; + if self.history.observe(presets) { self.notice = None; + changed = true; } } - cx.notify(); + if changed { + cx.notify(); + } } pub(crate) fn dismiss_notice(&mut self, cx: &mut Context) { @@ -479,6 +503,7 @@ impl LayoutEditorSection { Subject::Group(_) => Some(subject.clone()), Subject::Contribution(id) => match self.catalog.contribution(id)?.kind() { ContributionKind::Widget => Some(subject.clone()), + ContributionKind::Toolbar => None, ContributionKind::Panel => snapshot.group_of(id).map(|group| { if group.is_solo() { subject.clone() @@ -554,7 +579,7 @@ impl Focusable for LayoutEditorSection { impl Render for LayoutEditorSection { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { - let snapshot = Rc::new(self.snapshot(cx)); + let snapshot = self.snapshot(cx); let commands = Commands::new(cx.weak_entity()); v_flex() .id("layout-editor") diff --git a/crates/layout-ui/src/snapshot.rs b/crates/layout-ui/src/snapshot.rs index 4472497d..b78f1287 100644 --- a/crates/layout-ui/src/snapshot.rs +++ b/crates/layout-ui/src/snapshot.rs @@ -1,9 +1,11 @@ -//! What the editor shows of one layout, derived from the settings on each draw. +//! What the editor shows of one layout, derived from the settings, and kept until they change. -use std::collections::BTreeSet; +use std::{collections::BTreeSet, rc::Rc, sync::Arc}; use nocterm_plugin_enablement::PluginEnablement; -use nocterm_plugin_model::{ContributionId, GroupId, GroupSpec, PluginId, PluginSet, Zone}; +use nocterm_plugin_model::{ + ContributionId, ContributionKind, GroupId, GroupSpec, PluginId, PluginSet, Zone, +}; use nocterm_window_layout::{ BuiltIn, LayoutSettings, PresetId, ResolvedGroup, ResolvedItem, ResolvedLayout, ResolvedMember, Slot, Subject, select, @@ -74,8 +76,10 @@ impl Snapshot { .unwrap_or_default(), }; let layout = settings.resolve(&shown, catalog, &BTreeSet::new()); + // Toolbars are shown and hidden from the status bar's menu, not arranged here. let tray = catalog .entries() + .filter(|contribution| contribution.kind() != ContributionKind::Toolbar) .filter(|contribution| !layout.is_placed(contribution.id())) .map(|contribution| contribution.id().clone()) .collect(); @@ -311,6 +315,49 @@ impl Snapshot { } } +/// The snapshot of the layout shown, built again only when what it is drawn from changed: the +/// settings, by their revision, the set of plugins, or the layout picked. An editor never +/// changes its `legacy_swapped`. It holds the one snapshot drawn last. +#[derive(Default)] +pub(crate) struct Snapshots { + last: Option<(Drawn, Rc)>, +} + +/// What a snapshot was built from. +struct Drawn { + revision: u64, + catalog: Arc, + shown: Option, +} + +impl Snapshots { + /// The snapshot for these inputs: the last one when they are the same, else what `build` + /// makes of them. + pub(crate) fn get( + &mut self, + revision: u64, + catalog: &Arc, + shown: Option<&PresetId>, + build: impl FnOnce() -> Snapshot, + ) -> Rc { + if let Some((drawn, snapshot)) = &self.last + && drawn.revision == revision + && Arc::ptr_eq(&drawn.catalog, catalog) + && drawn.shown.as_ref() == shown + { + return snapshot.clone(); + } + let snapshot = Rc::new(build()); + let drawn = Drawn { + revision, + catalog: catalog.clone(), + shown: shown.cloned(), + }; + self.last = Some((drawn, snapshot.clone())); + snapshot + } +} + /// Where `find` met a subject. struct Found { zone: Zone, diff --git a/crates/layout-ui/src/tests.rs b/crates/layout-ui/src/tests.rs index 7388c156..99759c3a 100644 --- a/crates/layout-ui/src/tests.rs +++ b/crates/layout-ui/src/tests.rs @@ -4,7 +4,9 @@ use std::{borrow::Cow, rc::Rc, sync::Arc}; use gpui_kit::{ - AnyWindowHandle, AppContext as _, TestAppContext, WindowOptions, test::TestWindowExt as _, + AnyWindowHandle, AppContext as _, Bounds, InputEvent as _, Modifiers, MouseButton, + MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, PlatformInput, Point, TestAppContext, + WindowOptions, test::TestWindowExt as _, }; use nocterm_plugin_enablement::PluginEnablement; use nocterm_plugin_model::{ @@ -19,6 +21,7 @@ use nocterm_window_layout::LayoutSettings; use crate::section::LayoutEditorSection; +mod aim; mod dnd; mod editing; mod geometry; @@ -46,6 +49,7 @@ const fn widget( }, panel: None, preview: None, + edge: None, pinned: false, } } @@ -249,6 +253,68 @@ impl Fixture { cx.run_until_parked(); } + /// Drags the element `from` to the point `across` of the way over the element `to`, from + /// its left edge, and lets the writes it caused settle. + pub(super) fn drag_across(&self, from: &str, to: &str, across: f32, cx: &mut TestAppContext) { + let target = self.bounds(to, cx); + let at = gpui_kit::point( + target.left() + target.size.width * across, + target.center().y, + ); + let from = from.to_owned(); + self.frame(cx, |window, cx| { + let start = window.find(from).bounds().center(); + window.drag(start, at, cx); + }); + cx.run_until_parked(); + } + + /// Presses the pointer on the middle of the element `from` and moves it, held, to `to`: a + /// drag in progress, drawn after every move as a real one is. + pub(super) fn hold(&self, from: &str, to: Point, cx: &mut TestAppContext) { + let from = from.to_owned(); + self.frame(cx, |window, cx| { + let start = window.find(from).bounds().center(); + pointer(window, moved(start, None), cx); + pointer(window, pressed(start), cx); + for step in 1..=8 { + let at = start + (to - start) * (step as f32 / 8.); + pointer(window, moved(at, Some(MouseButton::Left)), cx); + } + }); + } + + /// Releases the held pointer at `at` and draws nothing itself: the frame that follows is + /// the one the release asked for, if any. + pub(super) fn release(&self, at: Point, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| { + window.dispatch_event(released(at), cx); + }) + .unwrap(); + cx.run_until_parked(); + } + + /// Where the last frame drawn shows the element `id`, without drawing another one. + pub(super) fn shown(&self, id: &str, cx: &mut TestAppContext) -> Option> { + cx.update_window(self.window, |_, window, _| { + window + .try_find(id.to_owned()) + .map(|element| element.bounds()) + }) + .unwrap() + } + + /// The bounds of the element `id` in the frame drawn now. + pub(super) fn bounds(&self, id: &str, cx: &mut TestAppContext) -> Bounds { + let mut found = None; + self.frame(cx, |window, _| { + found = window + .try_find(id.to_owned()) + .map(|element| element.bounds()); + }); + found.unwrap_or_else(|| panic!("{id} is not in the frame")) + } + /// Clicks the element `id` in the frame drawn now and lets the writes it caused settle. pub(super) fn click(&self, id: &str, cx: &mut TestAppContext) { let id = id.to_owned(); @@ -340,6 +406,18 @@ impl Fixture { selected } + /// Whether the preview marks the panel `id` as opening with the layout; `None` for a chip + /// that is no panel. + pub(super) fn opens(&self, id: &str, cx: &mut TestAppContext) -> Option { + let mut opens = None; + self.frame(cx, |window, _| { + opens = window + .try_find(preview_id(id)) + .and_then(|element| element.checked()); + }); + opens + } + /// Opens the menu of the element `id` and chooses the entry `label` on its first level, /// then lets the writes it caused settle. pub(super) fn choose(&self, id: &str, label: &str, cx: &mut TestAppContext) { @@ -361,6 +439,42 @@ impl Fixture { } } +/// Dispatches one pointer event and draws the frame it leads to, as the platform would. +fn pointer(window: &mut gpui_kit::Window, event: PlatformInput, cx: &mut gpui_kit::App) { + window.dispatch_event(event, cx); + window.render_frame(cx); +} + +fn moved(position: Point, pressed_button: Option) -> PlatformInput { + MouseMoveEvent { + position, + pressed_button, + modifiers: Modifiers::default(), + } + .to_platform_input() +} + +fn pressed(position: Point) -> PlatformInput { + MouseDownEvent { + button: MouseButton::Left, + position, + modifiers: Modifiers::default(), + click_count: 1, + first_mouse: false, + } + .to_platform_input() +} + +fn released(position: Point) -> PlatformInput { + MouseUpEvent { + button: MouseButton::Left, + position, + modifiers: Modifiers::default(), + click_count: 1, + } + .to_platform_input() +} + /// The element of the bar preview that draws `id`, or the group `g` for `group:g`. pub(super) fn preview_id(id: &str) -> String { match id.strip_prefix("group:") { diff --git a/crates/layout-ui/src/tests/aim.rs b/crates/layout-ui/src/tests/aim.rs new file mode 100644 index 00000000..a5ab02f0 --- /dev/null +++ b/crates/layout-ui/src/tests/aim.rs @@ -0,0 +1,154 @@ +//! While an item is held over the bar preview, one mark shows where it would land and the ghost +//! looks like the item; a release anywhere ends the drag, and the frame after it shows neither. + +use gpui_kit::{Bounds, Pixels, TestAppContext, point, px}; +use nocterm_window_layout::LayoutSettings; + +use super::{Fixture, open}; + +const MARKER: &str = "layout-drop-marker"; +const GHOST: &str = "layout-drag-ghost"; + +fn inside(inner: Bounds, outer: Bounds) -> bool { + inner.left() >= outer.left() + && inner.right() <= outer.right() + && inner.top() >= outer.top() + && inner.bottom() <= outer.bottom() +} + +/// The point `across` of the way over the element `id`, on its middle line. +fn across( + fixture: &Fixture, + id: &str, + across: f32, + cx: &mut TestAppContext, +) -> gpui_kit::Point { + let bounds = fixture.bounds(id, cx); + point( + bounds.left() + bounds.size.width * across, + bounds.center().y, + ) +} + +/// Whether the drag ended and the frame drawn after it shows no ghost and no mark. +fn ended(fixture: &Fixture, cx: &mut TestAppContext) -> bool { + !cx.update(|cx| cx.has_active_drag()) + && fixture.shown(GHOST, cx).is_none() + && fixture.shown(MARKER, cx).is_none() +} + +#[gpui_kit::test] +fn an_item_held_over_a_chip_shows_one_line_where_it_would_land(cx: &mut TestAppContext) { + let fixture = open(cx); + let at = across(&fixture, "layout-preview:shell.transfers", 0.25, cx); + fixture.hold("layout-preview:shell.terminal", at, cx); + + let marker = fixture.shown(MARKER, cx).expect("a line marks the place"); + let gap = fixture.bounds("layout-preview-gap:right:1", cx); + assert!( + inside(marker, gap), + "{marker:?} in front of transfers, in {gap:?}" + ); + + fixture.release(at, cx); + assert!(ended(&fixture, cx)); +} + +#[gpui_kit::test] +fn a_widget_released_over_a_panel_of_a_group_ends_the_drag_and_lands_beside_the_group( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + let files = fixture.bounds("layout-preview:shell.files", cx); + fixture.hold("layout-preview:tools.clock", files.center(), cx); + assert!( + fixture.shown(GHOST, cx).is_some(), + "the ghost follows the pointer" + ); + + fixture.release(files.center(), cx); + + assert!(ended(&fixture, cx), "the frame after the release is drawn"); + assert_eq!( + fixture.left_to_right(&["group:navigation", "tools.clock", "group:assistant"], cx), + ["group:navigation", "tools.clock", "group:assistant"] + ); +} + +#[gpui_kit::test] +fn a_release_that_changes_nothing_ends_the_drag_and_leaves_no_ghost_or_mark( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + for target in [ + "layout-preview:shell.transfers", + "layout-preview:tools.clock", + ] { + let at = across(&fixture, target, 0.25, cx); + fixture.hold("layout-preview:shell.transfers", at, cx); + assert!( + fixture.shown(MARKER, cx).is_none(), + "{target}: no place to mark" + ); + + fixture.release(at, cx); + assert!(ended(&fixture, cx), "{target}"); + } + assert_eq!(fixture.stored(cx), LayoutSettings::default()); +} + +#[gpui_kit::test] +fn a_release_away_from_every_chip_lands_in_the_nearest_zone(cx: &mut TestAppContext) { + let fixture = open(cx); + let (preview, zone) = ( + fixture.bounds("layout-preview", cx), + fixture.bounds("layout-preview-zone:right", cx), + ); + let below_the_end = point(zone.right() - px(1.), preview.bottom() - px(1.)); + fixture.hold("layout-preview:shell.terminal", below_the_end, cx); + fixture.release(below_the_end, cx); + + assert!(ended(&fixture, cx)); + assert_eq!( + fixture.left_to_right(&["shell.terminal", "off.item"], cx), + ["off.item", "shell.terminal"], + "last in the right zone" + ); +} + +#[gpui_kit::test] +fn a_panel_held_over_the_middle_of_a_panel_on_its_own_tints_that_panel(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.files", "Take out of group", cx); + let files = fixture.bounds("layout-preview:shell.files", cx); + fixture.hold("layout-preview:shell.agent", files.center(), cx); + + let tint = fixture.shown(MARKER, cx).expect("the panel is tinted"); + assert!(inside(tint, files), "{tint:?} on {files:?}"); + + fixture.release(files.center(), cx); + assert!(ended(&fixture, cx)); + assert!( + fixture.has("layout-preview-group:group-1", cx), + "the two make a group" + ); +} + +#[gpui_kit::test] +fn the_ghost_of_a_drag_is_as_large_as_the_chip_or_the_group_it_lifts(cx: &mut TestAppContext) { + let fixture = open(cx); + for id in [ + "layout-preview:shell.transfers", + "layout-preview-group:navigation", + ] { + let item = fixture.bounds(id, cx); + let away = item.center() + point(px(0.), px(80.)); + fixture.hold(id, away, cx); + let ghost = fixture.shown(GHOST, cx).expect("a ghost"); + let root = fixture.shown("drag-preview", cx).expect("the drag root"); + fixture.release(away, cx); + + assert_eq!(root.size, item.size, "{id}: the box it was measured in"); + assert_eq!(ghost.size, item.size, "{id}: the ghost fills it"); + } +} diff --git a/crates/layout-ui/src/tests/dnd.rs b/crates/layout-ui/src/tests/dnd.rs index 44fe8d24..adc8fac5 100644 --- a/crates/layout-ui/src/tests/dnd.rs +++ b/crates/layout-ui/src/tests/dnd.rs @@ -14,12 +14,19 @@ fn x_of(fixture: &Fixture, id: &'static str, cx: &mut TestAppContext) -> f32 { x.unwrap_or_else(|| panic!("{id} is not in the frame")) } +/// How far across a chip its left half and its right half are. +const LEFT_HALF: f32 = 0.25; +const RIGHT_HALF: f32 = 0.75; + #[gpui_kit::test] -fn a_chip_dropped_on_another_chip_takes_the_place_in_front_of_it(cx: &mut TestAppContext) { +fn a_chip_dropped_on_the_left_half_of_another_takes_the_place_in_front_of_it( + cx: &mut TestAppContext, +) { let fixture = open(cx); - fixture.drag( + fixture.drag_across( "layout-preview:shell.terminal", "layout-preview:shell.transfers", + LEFT_HALF, cx, ); @@ -40,6 +47,27 @@ fn a_chip_dropped_on_another_chip_takes_the_place_in_front_of_it(cx: &mut TestAp ); } +#[gpui_kit::test] +fn a_chip_dropped_on_the_right_half_of_another_takes_the_place_after_it(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag_across( + "layout-preview:shell.terminal", + "layout-preview:shell.notices", + RIGHT_HALF, + cx, + ); + + assert_eq!( + fixture.left_to_right(&RIGHT, cx), + [ + "shell.notices", + "shell.terminal", + "shell.transfers", + "tools.clock" + ] + ); +} + #[gpui_kit::test] fn a_chip_dropped_on_a_gap_takes_that_place(cx: &mut TestAppContext) { let fixture = open(cx); @@ -97,11 +125,14 @@ fn a_panel_dropped_on_another_group_joins_it_at_the_end(cx: &mut TestAppContext) } #[gpui_kit::test] -fn a_panel_dropped_on_a_panel_of_another_group_lands_in_front_of_it(cx: &mut TestAppContext) { +fn a_panel_dropped_on_the_left_half_of_a_panel_of_another_group_lands_in_front_of_it( + cx: &mut TestAppContext, +) { let fixture = open(cx); - fixture.drag( + fixture.drag_across( "layout-preview:shell.files", "layout-preview:shell.agent", + LEFT_HALF, cx, ); @@ -116,6 +147,58 @@ fn a_panel_dropped_on_a_panel_of_another_group_lands_in_front_of_it(cx: &mut Tes ); } +#[gpui_kit::test] +fn a_panel_dropped_at_the_start_or_the_end_edge_of_a_group_is_its_first_or_last_member( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + let members = ["shell.servers", "shell.files", "shell.agent"]; + let in_sidebar = |fixture: &Fixture, cx: &mut TestAppContext| { + let pill = fixture.bounds("layout-preview-group:navigation", cx); + let chip = fixture.bounds("layout-preview:shell.agent", cx); + chip.left() >= pill.left() && chip.right() <= pill.right() + }; + // The outer half of the first member, then of the last one. + for (edge, across, order) in [ + ( + "shell.servers", + LEFT_HALF, + ["shell.agent", "shell.servers", "shell.files"], + ), + ( + "shell.files", + RIGHT_HALF, + ["shell.servers", "shell.files", "shell.agent"], + ), + ] { + let edge = format!("layout-preview:{edge}"); + fixture.drag_across("layout-preview:shell.agent", &edge, across, cx); + + assert_eq!(fixture.left_to_right(&members, cx), order, "{edge}"); + assert!(in_sidebar(&fixture, cx), "{edge}: inside the group"); + } +} + +#[gpui_kit::test] +fn a_panel_moved_inside_its_group_leaves_the_group_unselected(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag_across( + "layout-preview:shell.servers", + "layout-preview:shell.files", + RIGHT_HALF, + cx, + ); + + assert_eq!( + fixture.left_to_right(&["shell.servers", "shell.files"], cx), + ["shell.files", "shell.servers"] + ); + assert!( + !fixture.is_selected("group:navigation", cx), + "the release over the group was the drop, not a click on it" + ); +} + #[gpui_kit::test] fn a_group_dropped_on_a_zone_moves_whole(cx: &mut TestAppContext) { let fixture = open(cx); @@ -149,9 +232,10 @@ fn an_item_dragged_out_of_the_tray_returns_in_front_of_the_chip_it_is_dropped_on ) { let fixture = open(cx); fixture.drag("layout-preview:tools.clock", "layout-tray", cx); - fixture.drag( + fixture.drag_across( "layout-tray:tools.clock", "layout-preview:shell.notices", + LEFT_HALF, cx, ); @@ -193,9 +277,10 @@ fn the_pinned_notice_cannot_be_dropped_in_the_tray_and_the_editor_says_why( #[gpui_kit::test] fn a_drop_is_one_step_of_the_history(cx: &mut TestAppContext) { let fixture = open(cx); - fixture.drag( + fixture.drag_across( "layout-preview:shell.terminal", "layout-preview:shell.transfers", + LEFT_HALF, cx, ); fixture.click("layout-undo", cx); @@ -206,16 +291,13 @@ fn a_drop_is_one_step_of_the_history(cx: &mut TestAppContext) { #[gpui_kit::test] fn nothing_is_written_when_an_item_is_dropped_where_it_already_is(cx: &mut TestAppContext) { let fixture = open(cx); - fixture.drag( - "layout-preview:shell.transfers", - "layout-preview:shell.transfers", - cx, - ); - fixture.drag( - "layout-preview:shell.transfers", - "layout-preview:tools.clock", - cx, - ); + for (to, across) in [ + ("layout-preview:shell.transfers", LEFT_HALF), + ("layout-preview:shell.transfers", RIGHT_HALF), + ("layout-preview:tools.clock", LEFT_HALF), + ] { + fixture.drag_across("layout-preview:shell.transfers", to, across, cx); + } assert_eq!(fixture.stored(cx), LayoutSettings::default()); } diff --git a/crates/layout-ui/src/tests/geometry.rs b/crates/layout-ui/src/tests/geometry.rs index e57ca05f..a107a61f 100644 --- a/crates/layout-ui/src/tests/geometry.rs +++ b/crates/layout-ui/src/tests/geometry.rs @@ -1,7 +1,7 @@ //! The editor lays itself out inside the width it is given: the bar stays one line inside its //! box and the tray stays below it, at a Settings page's width and at a narrow one. -use gpui_kit::{Bounds, Pixels, TestAppContext, test::TestWindowExt as _}; +use gpui_kit::{Bounds, Pixels, TestAppContext}; use super::{Fixture, open}; @@ -28,13 +28,7 @@ fn inside(inner: Bounds, outer: Bounds) -> bool { } fn bounds(fixture: &Fixture, id: &str, cx: &mut TestAppContext) -> Bounds { - let mut found = None; - fixture.frame(cx, |window, _| { - found = window - .try_find(id.to_owned()) - .map(|element| element.bounds()); - }); - found.unwrap_or_else(|| panic!("{id} is not in the frame")) + fixture.bounds(id, cx) } /// The chips of the bar preview that the default layout places. diff --git a/crates/layout-ui/src/tests/grouping.rs b/crates/layout-ui/src/tests/grouping.rs index 1f8585b7..67b2534d 100644 --- a/crates/layout-ui/src/tests/grouping.rs +++ b/crates/layout-ui/src/tests/grouping.rs @@ -1,18 +1,12 @@ //! Panels stand alone in the bar or share a group: dragging and the menus take a panel out of //! its group, put two panels together, start, rename and undo a group. -use gpui_kit::{Bounds, Pixels, TestAppContext, test::TestWindowExt as _}; +use gpui_kit::{Bounds, Pixels, TestAppContext}; use super::{Fixture, open, preview_id}; fn bounds(fixture: &Fixture, id: &str, cx: &mut TestAppContext) -> Bounds { - let mut found = None; - fixture.frame(cx, |window, _| { - found = window - .try_find(preview_id(id)) - .map(|element| element.bounds()); - }); - found.unwrap_or_else(|| panic!("{id} is not in the frame")) + fixture.bounds(&preview_id(id), cx) } /// Whether the chip of `panel` is drawn inside the pill of `group`. @@ -128,3 +122,23 @@ fn a_right_click_on_a_panel_of_a_group_opens_the_menu_of_the_panel(cx: &mut Test assert!(in_group(&fixture, "shell.agent", "group-1", cx)); } + +#[gpui_kit::test] +fn the_panel_that_opens_with_the_layout_is_marked_in_its_group(cx: &mut TestAppContext) { + let fixture = open(cx); + assert_eq!(fixture.opens("shell.files", cx), Some(false)); + + fixture.choose("layout-preview:shell.files", "Open with the layout", cx); + + assert_eq!(fixture.opens("shell.files", cx), Some(true)); + assert_eq!( + fixture.opens("shell.servers", cx), + Some(false), + "one per group" + ); + assert_eq!( + fixture.opens("shell.transfers", cx), + None, + "a widget opens nothing" + ); +} diff --git a/crates/layout-ui/src/tray.rs b/crates/layout-ui/src/tray.rs index 6340fd65..f6348865 100644 --- a/crates/layout-ui/src/tray.rs +++ b/crates/layout-ui/src/tray.rs @@ -6,7 +6,7 @@ use std::{rc::Rc, sync::Arc}; use gpui_kit::{ AnyElement, App, IntoElement, RenderOnce, SharedString, TestSupportExt as _, Window, component::{ - ActiveTheme as _, Disableable as _, Icon, Sizable as _, + ActiveTheme as _, Disableable as _, Sizable as _, button::{Button, ButtonVariants as _}, h_flex, menu::ContextMenuExt as _, @@ -16,13 +16,14 @@ use gpui_kit::{ px, }; use nocterm_plugin_model::{ContributionId, ContributionRef, PluginSet}; -use nocterm_ui::{IconName, contribution_icon}; +use nocterm_ui::{DragSource, IconName, contribution_icon}; use nocterm_window_layout::{EditOp, Subject}; use crate::{ commands::Commands, - dnd::{self, CHIP_HEIGHT, Dragged, Landing}, - menu::ItemMenu, + dnd::{self, Dragged}, + face::{BORDER, CHIP_HEIGHT, Face, Look}, + menu::{MenuOf, Menus}, snapshot::Snapshot, }; @@ -44,54 +45,55 @@ impl Tray { /// A contribution that waits out of the bar: a click puts it back where its hints say, a /// drag puts it anywhere. - fn entry(&self, contribution: ContributionRef<'_>, cx: &App) -> AnyElement { + fn entry(&self, contribution: ContributionRef<'_>, menus: &Menus, cx: &App) -> AnyElement { let id = contribution.id().clone(); let subject = Subject::Contribution(id.clone()); let disabled = self.snapshot.is_disabled(&id, &self.catalog); let editable = self.snapshot.editable(); let add = self.commands.clone(); - let menu = ItemMenu::new( - &subject, - &self.snapshot, - &self.catalog, - self.commands.clone(), - ); let theme = cx.theme(); let name: SharedString = if disabled { format!("{} (disabled)", contribution.title()).into() } else { contribution.title().to_owned().into() }; - let chip = h_flex() + let title = SharedString::from(contribution.title().to_owned()); + let face = Face::new(contribution_icon(contribution.icon()), Some(title), false); + let chip = face + .clone() + .fill(h_flex(), cx) .id(SharedString::from(format!("layout-tray:{id}"))) .aria_label(name) .test_support() - .flex_none() - .h(px(CHIP_HEIGHT)) - .gap_1() - .px_1p5() - .items_center() - .rounded(theme.radius) .bg(theme.secondary) .when(editable, |chip| { chip.cursor_pointer() - .hover(|style| style.bg(theme.secondary_hover)) + .hover(|style| style.bg(theme.secondary_active)) .on_click(move |_, window, cx| { + // A release that ends a drag is the drop, not a click. + if cx.has_active_drag() { + return; + } let id = id.clone(); add.run(window, cx, move |section, _, cx| { section.edit(EditOp::Restore { id }, cx); }); }) }) - .when(disabled, |chip| chip.opacity(0.5)) - .child(Icon::new(contribution_icon(contribution.icon())).small()) - .child(div().text_xs().child(contribution.title().to_owned())); + .when(disabled, |chip| chip.opacity(0.5)); let chip = if editable { - dnd::source(chip, Dragged::contribution(contribution, &self.snapshot)) + let source = DragSource::default(); + let measure = source.clone(); + let chip = dnd::measured(chip, BORDER, move |bounds, window, _| { + measure.capture(bounds, window); + }); + let dragged = Dragged::contribution(contribution, None); + dnd::source(chip, dragged, source, move || Look::Chip(face.clone())) } else { chip }; - chip.context_menu(move |popup, window, cx| menu.popup(popup, window, cx)) + let (menus, of) = (menus.clone(), MenuOf::Item(subject)); + chip.context_menu(move |popup, window, cx| menus.popup(&of, popup, window, cx)) .into_any_element() } @@ -135,12 +137,17 @@ impl Tray { impl RenderOnce for Tray { fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { + let menus = Menus::new( + self.snapshot.clone(), + self.catalog.clone(), + self.commands.clone(), + ); let entries: Vec = self .snapshot .tray() .iter() .filter_map(|id| self.catalog.contribution(id)) - .map(|contribution| self.entry(contribution, cx)) + .map(|contribution| self.entry(contribution, &menus, cx)) .chain( self.snapshot .unknown() @@ -171,7 +178,7 @@ impl RenderOnce for Tray { ) .children(entries); if self.snapshot.editable() { - dnd::target(tray, Landing::tray(), true, self.commands).into_any_element() + dnd::tray(tray, self.commands).into_any_element() } else { tray.into_any_element() } diff --git a/crates/local/Cargo.toml b/crates/local/Cargo.toml index 287e6332..4a800e52 100644 --- a/crates/local/Cargo.toml +++ b/crates/local/Cargo.toml @@ -22,10 +22,17 @@ thiserror.workspace = true chrono = { version = "0.4", default-features = false, features = ["clock"] } [target.'cfg(unix)'.dependencies] -nix = { version = "0.28", features = ["signal", "fs", "event"] } +nix = { version = "0.28", features = ["signal", "fs", "event", "poll"] } + +[target.'cfg(target_os = "linux")'.dependencies] +filedescriptor = "0.8.3" +rustix = { version = "1.1.5", features = ["process"] } [target.'cfg(windows)'.dependencies] process-wrap = { version = "9.1.0", default-features = false, features = ["std", "job-object", "creation-flags"] } +[dev-dependencies] +nocterm-session = {workspace=true,features=["test-support"]} + [lints] workspace = true diff --git a/crates/local/src/exec.rs b/crates/local/src/exec.rs index 082c1879..55f5e3ae 100644 --- a/crates/local/src/exec.rs +++ b/crates/local/src/exec.rs @@ -17,68 +17,126 @@ use nocterm_session::{ ShellLaunch, TerminalRequest, }; +mod admission; mod process; +use admission::{Admission, Permit}; #[cfg(all(test, unix))] mod tests; const READ_BUFFER: usize = 16 * 1024; -const EXIT_POLL: Duration = Duration::from_millis(10); +#[cfg(not(unix))] +const EXIT_POLL: Duration = Duration::from_millis(100); const ERROR_GRACE: Duration = Duration::from_millis(200); -#[derive(Clone, Copy, Debug, Default)] -pub struct LocalExec; +/// One explicitly shared native-command admission owner. +#[derive(Clone, Debug, Default)] +pub struct LocalExec { + admission: Arc, +} +pub(crate) struct ScopedLocalExec { + source: nocterm_session::ConnectionSource, + executor: LocalExec, +} +impl ScopedLocalExec { + pub(crate) fn new(source: nocterm_session::ConnectionSource) -> Self { + Self { + source, + executor: LocalExec::default(), + } + } +} impl HostExec for LocalExec { fn exec(&self, request: ExecRequest) -> ExecFuture { - Box::pin(async move { - let (ready, started) = oneshot::channel(); - spawn("nocterm-exec-start", move || { - if !ready.is_canceled() { - // If the future was dropped during spawn, sending drops - // the output and the supervisor immediately cancels it. - let _ = ready.send(start(request)); - } - })?; - started - .await - .map_err(|error| ExecError::Failed(error.to_string()))? - }) + execute(request, Default::default(), self.admission.clone()) } - fn terminal(&self, request: TerminalRequest) -> Session { - let launch = ShellLaunch { - program: Some(request.program.program), - args: request.program.args, - integration: false, - ..ShellLaunch::default() - }; - crate::open(launch, false, request.term, request.size) + terminal(request, Default::default()) } } +impl HostExec for ScopedLocalExec { + fn exec(&self, request: ExecRequest) -> ExecFuture { + execute( + request, + self.source.clone(), + self.executor.admission.clone(), + ) + } + fn terminal(&self, request: TerminalRequest) -> Session { + terminal(request, self.source.clone()) + } +} +fn execute( + request: ExecRequest, + source: nocterm_session::ConnectionSource, + admission: Arc, +) -> ExecFuture { + source.clone().guard(async move { + let permit = admission.reserve()?; + let (ready, started) = oneshot::channel(); + spawn("nocterm-exec-start", move || { + if !ready.is_canceled() { + let _ = ready.send(start(request, source, permit)); + } + })?; + started + .await + .map_err(|error| ExecError::Failed(error.to_string()))? + }) +} +fn terminal(request: TerminalRequest, source: nocterm_session::ConnectionSource) -> Session { + let launch = ShellLaunch { + program: Some(request.program.program), + args: request.program.args, + integration: false, + ..ShellLaunch::default() + }; + crate::open_scoped(launch, false, request.term, request.size, source) +} -fn start(request: ExecRequest) -> Result { - let mut process = process::Process::spawn(&request)?; +fn start( + request: ExecRequest, + source: nocterm_session::ConnectionSource, + permit: Arc, +) -> Result { + source.check()?; + let mut process = process::Process::spawn(&request, &source)?; let (Some(stdout), Some(stderr)) = (process.stdout(), process.stderr()) else { return Err(ExecError::Failed("no output pipe".into())); }; + let stopped = Arc::new(AtomicBool::new(false)); if let (Some(input), Some(stdin)) = (request.stdin, process.stdin()) { - write_input(stdin, input)?; + write_input( + stdin, + input, + source.clone(), + stopped.clone(), + permit.clone(), + ) + .inspect_err(|_| stopped.store(true, Ordering::Release))?; } - let (sink, output) = ExecOutput::channel(); + let (sink, output) = ExecOutput::channel_with_source(source.clone()); let sink = Arc::new(sink); - let stopped = Arc::new(AtomicBool::new(false)); - let drained = read_output(stdout, sink.clone(), stopped.clone())?; - let errors = read_errors(stderr, stopped.clone())?; + let drained = read_output(stdout, sink.clone(), stopped.clone(), permit.clone()) + .inspect_err(|_| stopped.store(true, Ordering::Release))?; + let errors = read_errors(stderr, stopped.clone(), permit.clone()) + .inspect_err(|_| stopped.store(true, Ordering::Release))?; + let failed = stopped.clone(); spawn("nocterm-exec-wait", move || { // Observe the leader independently from pipe EOF: descendants may // retain both output pipes after the leader has exited. + let _permit = permit; + let mut pause = Duration::from_millis(1); let natural = loop { - if sink.is_closed() { + if sink.is_closed() || source.check().is_err() { break false; } match process.exited() { Ok(true) => break true, - Ok(false) => thread::sleep(EXIT_POLL), + Ok(false) => { + thread::sleep(pause); + pause = (pause * 2).min(Duration::from_millis(100)); + } Err(_) => break false, } }; @@ -92,13 +150,34 @@ fn start(request: ExecRequest) -> Result { }); let tail = errors.recv_timeout(ERROR_GRACE).unwrap_or_default(); sink.finish(tail.exit(status)); - })?; + }) + .inspect_err(|_| failed.store(true, Ordering::Release))?; Ok(output) } -fn write_input(mut stdin: ChildStdin, input: Vec) -> Result<(), ExecError> { +fn write_input( + mut stdin: ChildStdin, + input: Vec, + source: nocterm_session::ConnectionSource, + stopped: Arc, + permit: Arc, +) -> Result<(), ExecError> { + #[cfg(unix)] + process::nonblocking(&stdin)?; spawn("nocterm-exec-in", move || { - let _ = stdin.write_all(&input); + let _permit = permit; + let mut remaining = input.as_slice(); + while !remaining.is_empty() && source.check().is_ok() && !stopped.load(Ordering::Acquire) { + match stdin.write(&remaining[..remaining.len().min(1024)]) { + Ok(0) => break, + Ok(n) => remaining = &remaining[n..], + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { + wait_writable(&stdin) + } + Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {} + Err(_) => break, + } + } }) } @@ -106,11 +185,13 @@ fn read_output( mut stdout: ChildStdout, sink: Arc, stopped: Arc, + permit: Arc, ) -> Result, ExecError> { #[cfg(unix)] process::nonblocking(&stdout)?; let (finished, drained) = oneshot::channel(); spawn("nocterm-exec-out", move || { + let _permit = permit; let mut buffer = vec![0; READ_BUFFER]; loop { match stdout.read(&mut buffer) { @@ -125,7 +206,7 @@ fn read_output( if stopped.load(Ordering::Acquire) || sink.is_closed() { break; } - thread::sleep(EXIT_POLL); + wait_readable(&stdout); } Err(_) => break, } @@ -138,11 +219,13 @@ fn read_output( fn read_errors( mut stderr: ChildStderr, stopped: Arc, + permit: Arc, ) -> Result, ExecError> { #[cfg(unix)] process::nonblocking(&stderr)?; let (send, errors) = std::sync::mpsc::channel(); spawn("nocterm-exec-err", move || { + let _permit = permit; let mut tail = ErrorTail::default(); let mut buffer = [0; 1024]; loop { @@ -154,7 +237,7 @@ fn read_errors( if stopped.load(Ordering::Acquire) { break; } - thread::sleep(EXIT_POLL); + wait_readable(&stderr); } Err(_) => break, } @@ -171,3 +254,25 @@ fn spawn(name: &str, body: impl FnOnce() + Send + 'static) -> Result<(), ExecErr .map(drop) .map_err(|error| ExecError::Failed(error.to_string())) } + +#[cfg(unix)] +fn wait_readable(pipe: &impl std::os::fd::AsFd) { + wait_pipe(pipe, nix::poll::PollFlags::POLLIN); +} +#[cfg(unix)] +fn wait_writable(pipe: &impl std::os::fd::AsFd) { + wait_pipe(pipe, nix::poll::PollFlags::POLLOUT); +} +#[cfg(unix)] +fn wait_pipe(pipe: &impl std::os::fd::AsFd, events: nix::poll::PollFlags) { + let mut fds = [nix::poll::PollFd::new(pipe.as_fd(), events)]; + let _ = nix::poll::poll(&mut fds, 100u16); +} +#[cfg(not(unix))] +fn wait_readable(_: &T) { + thread::sleep(EXIT_POLL); +} +#[cfg(not(unix))] +fn wait_writable(_: &T) { + thread::sleep(EXIT_POLL); +} diff --git a/crates/local/src/exec/admission.rs b/crates/local/src/exec/admission.rs new file mode 100644 index 00000000..8b989beb --- /dev/null +++ b/crates/local/src/exec/admission.rs @@ -0,0 +1,32 @@ +//! The original executor owns a finite native command budget through actual cleanup. +use nocterm_session::ExecError; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; + +const MAX_COMMANDS: usize = 32; +#[derive(Debug, Default)] +pub(super) struct Admission { + active: AtomicUsize, +} +impl Admission { + pub(super) fn reserve(self: &Arc) -> Result, ExecError> { + self.active + .fetch_update(Ordering::AcqRel, Ordering::Acquire, |active| { + (active < MAX_COMMANDS).then_some(active + 1) + }) + .map_err(|_| { + ExecError::Failed( + "Local command capacity is full (32). Wait for a command to finish.".into(), + ) + })?; + Ok(Arc::new(Permit(self.clone()))) + } +} +pub(super) struct Permit(Arc); +impl Drop for Permit { + fn drop(&mut self) { + self.0.active.fetch_sub(1, Ordering::AcqRel); + } +} diff --git a/crates/local/src/exec/process.rs b/crates/local/src/exec/process.rs index 8f7c76f9..bdadcbfa 100644 --- a/crates/local/src/exec/process.rs +++ b/crates/local/src/exec/process.rs @@ -19,7 +19,10 @@ pub(super) struct Process { } impl Process { - pub(super) fn spawn(request: &ExecRequest) -> Result { + pub(super) fn spawn( + request: &ExecRequest, + source: &nocterm_session::ConnectionSource, + ) -> Result { let mut command = Command::new(&request.program); command .args(&request.args) @@ -30,6 +33,7 @@ impl Process { }) .stdout(Stdio::piped()) .stderr(Stdio::piped()); + source.check()?; #[cfg(unix)] let spawned = { use std::os::unix::process::CommandExt as _; diff --git a/crates/local/src/exec/tests.rs b/crates/local/src/exec/tests.rs index e06c1d52..63ac7a42 100644 --- a/crates/local/src/exec/tests.rs +++ b/crates/local/src/exec/tests.rs @@ -17,7 +17,7 @@ fn runs_a_script_from_its_input() { let request = ExecRequest::new("sh") .arg("-s") .stdin("echo one; echo \"$((40 + 2))\"\n"); - let output = block_on(LocalExec.exec(request)).unwrap(); + let output = block_on(LocalExec::default().exec(request)).unwrap(); assert_eq!(collect(output), "one\n42\n"); } @@ -30,7 +30,7 @@ fn dropping_the_output_stops_the_program_and_its_children() { file.display() ); let request = ExecRequest::new("sh").arg("-c").arg(script); - let mut output = block_on(LocalExec.exec(request)).unwrap(); + let mut output = block_on(LocalExec::default().exec(request)).unwrap(); assert_eq!(block_on(output.next()).as_deref(), Some(&b"started\n"[..])); drop(output); thread::sleep(Duration::from_millis(600)); @@ -42,18 +42,22 @@ fn programs_report_their_exit_and_errors() { let request = ExecRequest::new("sh") .arg("-c") .arg("echo out; echo 'no such container' >&2; exit 3"); - let collected = block_on(block_on(LocalExec.exec(request)).unwrap().collect(1024)); + let collected = block_on( + block_on(LocalExec::default().exec(request)) + .unwrap() + .collect(1024), + ); assert_eq!(collected.text(), "out\n"); assert_eq!(collected.exit.status, Some(3)); assert_eq!(collected.exit.error(), "no such container"); - let fine = block_on(LocalExec.exec(ExecRequest::new("true"))).unwrap(); + let fine = block_on(LocalExec::default().exec(ExecRequest::new("true"))).unwrap(); assert!(block_on(fine.exit()).success()); } #[test] fn programs_run_on_a_terminal_of_their_own() { use nocterm_session::{CloseReason, Event, PtySize}; - let session = LocalExec.terminal(TerminalRequest { + let session = LocalExec::default().terminal(TerminalRequest { program: ExecRequest::new("sh") .arg("-c") .arg("test -t 1 && echo on-a-tty; exit 4"), @@ -78,7 +82,8 @@ fn programs_run_on_a_terminal_of_their_own() { #[test] fn a_missing_program_is_an_error() { let started = Instant::now(); - let error = block_on(LocalExec.exec(ExecRequest::new("/nonexistent/nocterm"))).unwrap_err(); + let error = + block_on(LocalExec::default().exec(ExecRequest::new("/nonexistent/nocterm"))).unwrap_err(); assert_eq!(error, ExecError::NotFound("/nonexistent/nocterm".into())); assert!(started.elapsed() < Duration::from_secs(5)); } @@ -88,7 +93,7 @@ fn constructing_or_dropping_an_unpolled_exec_never_spawns() { let directory = tempfile::tempdir().unwrap(); let marker = directory.path().join("unexpected"); let request = ExecRequest::new("touch").arg(marker.to_string_lossy().to_string()); - let future = LocalExec.exec(request); + let future = LocalExec::default().exec(request); thread::sleep(Duration::from_millis(50)); assert!(!marker.exists()); drop(future); @@ -105,7 +110,11 @@ fn exited_leader_does_not_leave_descendants_or_hold_stdout_open() { marker.display() )); let started = Instant::now(); - let collected = block_on(block_on(LocalExec.exec(request)).unwrap().collect(1024)); + let collected = block_on( + block_on(LocalExec::default().exec(request)) + .unwrap() + .collect(1024), + ); assert_eq!(collected.text(), "started\n"); assert_eq!(collected.exit.status, Some(7)); assert!(started.elapsed() < Duration::from_millis(900)); @@ -121,7 +130,7 @@ fn cancellation_after_parent_exit_kills_a_child_that_closed_its_output() { "echo started; (exec >/dev/null 2>&1; sleep 0.3; touch '{}') & exit", marker.display() )); - let output = block_on(LocalExec.exec(request)).unwrap(); + let output = block_on(LocalExec::default().exec(request)).unwrap(); thread::sleep(Duration::from_millis(100)); drop(output); thread::sleep(Duration::from_millis(500)); @@ -134,7 +143,7 @@ fn exit_observation_does_not_discard_buffered_output() { .arg("-c") .arg("i=0; while [ $i -lt 4096 ]; do printf '1234567890\\n'; i=$((i+1)); done; exit 9"); let collected = block_on( - block_on(LocalExec.exec(request)) + block_on(LocalExec::default().exec(request)) .unwrap() .collect(128 * 1024), ); @@ -149,7 +158,7 @@ fn dropping_the_startup_process_guard_stops_the_whole_group() { let request = ExecRequest::new("sh") .arg("-c") .arg(format!("(sleep 0.3; touch '{}') & wait", marker.display())); - let process = process::Process::spawn(&request).unwrap(); + let process = process::Process::spawn(&request, &Default::default()).unwrap(); thread::sleep(Duration::from_millis(50)); // This is also the cleanup path when pipe/worker setup fails. drop(process); @@ -165,7 +174,7 @@ fn cancellation_interrupts_a_backpressured_output_worker_and_stops_descendants() "(sleep 1; touch '{}') & while :; do printf '%01024d\\n' 0; done", marker.display() )); - let mut output = block_on(LocalExec.exec(request)).unwrap(); + let mut output = block_on(LocalExec::default().exec(request)).unwrap(); assert!(block_on(output.next()).is_some()); // Leave stdout unread long enough to fill the bounded ExecOutput queue. thread::sleep(Duration::from_millis(100)); @@ -176,3 +185,31 @@ fn cancellation_interrupts_a_backpressured_output_worker_and_stops_descendants() "cancellation must bypass output backpressure" ); } + +#[test] +fn cloned_executors_share_the_native_command_capacity_and_release_it_after_cleanup() { + let exec = LocalExec::default(); + let clone = exec.clone(); + let mut outputs = Vec::new(); + for _ in 0..32 { + let mut output = + block_on(exec.exec(ExecRequest::new("sh").args(["-c", "echo ready; sleep 60"]))) + .unwrap(); + assert!(block_on(output.next()).is_some()); + outputs.push(output); + } + assert!(matches!( + block_on(clone.exec(ExecRequest::new("true"))), + Err(ExecError::Failed(_)) + )); + drop(outputs.pop()); + let deadline = Instant::now() + Duration::from_secs(5); + let output = loop { + match block_on(clone.exec(ExecRequest::new("true"))) { + Ok(output) => break output, + Err(_) if Instant::now() < deadline => thread::sleep(Duration::from_millis(10)), + error => panic!("command capacity did not recover: {error:?}"), + } + }; + assert!(block_on(output.exit()).success()); +} diff --git a/crates/local/src/integration.rs b/crates/local/src/integration.rs new file mode 100644 index 00000000..95bcb9bd --- /dev/null +++ b/crates/local/src/integration.rs @@ -0,0 +1,22 @@ +//! The same bundled integration programs are used by ordinary and contained shells. +pub(super) fn bundled(stem: &str) -> Option<(&'static str, &'static str)> { + Some(match stem { + "bash" => ( + "bashrc", + include_str!("../../../assets/shell-integration/bash.bash"), + ), + "zsh" => ( + ".zshrc", + include_str!("../../../assets/shell-integration/zsh.zsh"), + ), + "fish" => ( + "integration.fish", + include_str!("../../../assets/shell-integration/fish.fish"), + ), + "pwsh" | "powershell" => ( + "integration.ps1", + include_str!("../../../assets/shell-integration/powershell.ps1"), + ), + _ => return None, + }) +} diff --git a/crates/local/src/lib.rs b/crates/local/src/lib.rs index 92b6d8a4..cc295fa5 100644 --- a/crates/local/src/lib.rs +++ b/crates/local/src/lib.rs @@ -6,6 +6,8 @@ //! recording::OutputLogFactory, nocterm_core::clock::WallClock (`SystemClock`). Output-log //! operations run only on their caller executor. //! Settings section: local options are composed by the app. Globals: none. +//! Linux PTY reads wait for descriptor readiness, owner closure or the owned leader's pidfd; +//! kernels denying pidfds and other platforms retain bounded process-status polling. //! Invariants: PTY/exec cancellation owns process cleanup; external editors instead //! retain a bounded independent wait owner and survive receipt/application exit. @@ -23,16 +25,21 @@ use std::{ use futures::{FutureExt, executor::block_on, select}; use nocterm_session::{ CloseReason, Command, ConnectRequest, Event, PtySize, Session, SessionError, ShellLaunch, - Transport, channel, + Transport, }; use portable_pty::{CommandBuilder, PtySize as NativeSize, native_pty_system}; mod clock; +#[cfg(target_os = "linux")] mod exec; +mod integration; mod program_opener; +#[cfg(target_os = "linux")] +mod readiness; mod recording; pub use clock::SystemClock; +#[cfg(target_os = "linux")] pub use exec::LocalExec; pub use program_opener::{LocalProgramOpener, MAX_OPEN_PROGRAMS}; pub use recording::LocalOutputLogFactory; @@ -41,10 +48,59 @@ pub use recording::LocalOutputLogFactory; #[derive(Clone, Debug)] pub struct LocalTransport(pub ShellLaunch); +impl LocalTransport { + /// Carries native launch preparation to the existing startup worker. + pub fn prepared( + launch: ShellLaunch, + preparation: Option>, + ) -> Arc { + Arc::new(PreparedLocalTransport { + launch, + preparation, + }) + } +} +struct PreparedLocalTransport { + launch: ShellLaunch, + preparation: Option>, +} +impl Transport for PreparedLocalTransport { + fn open(&self, request: ConnectRequest) -> Session { + self.open_with(request, Default::default()) + } + fn open_with( + &self, + request: ConnectRequest, + options: nocterm_session::ConnectionOpenOptions, + ) -> Session { + open_prepared( + self.launch.clone(), + false, + request.term, + request.size, + options.source().clone(), + self.preparation.clone(), + ) + } +} + impl Transport for LocalTransport { fn open(&self, request: ConnectRequest) -> Session { open(self.0.clone(), false, request.term, request.size) } + fn open_with( + &self, + request: ConnectRequest, + options: nocterm_session::ConnectionOpenOptions, + ) -> Session { + open_scoped( + self.0.clone(), + false, + request.term, + request.size, + options.source().clone(), + ) + } } /// A command PTY with the host environment and platform PTY metadata only. @@ -55,22 +111,83 @@ impl Transport for IsolatedLocalTransport { fn open(&self, request: ConnectRequest) -> Session { open(self.0.clone(), true, request.term, request.size) } + fn open_with( + &self, + request: ConnectRequest, + options: nocterm_session::ConnectionOpenOptions, + ) -> Session { + open_scoped( + self.0.clone(), + true, + request.term, + request.size, + options.source().clone(), + ) + } } /// Starts `launch` on a new PTY, on a thread of its own. fn open(launch: ShellLaunch, isolated: bool, term: String, pty: PtySize) -> Session { + open_scoped(launch, isolated, term, pty, Default::default()) +} +fn open_scoped( + launch: ShellLaunch, + isolated: bool, + term: String, + pty: PtySize, + source: nocterm_session::ConnectionSource, +) -> Session { + open_prepared(launch, isolated, term, pty, source, None) +} + +fn open_prepared( + launch: ShellLaunch, + isolated: bool, + term: String, + pty: PtySize, + source: nocterm_session::ConnectionSource, + preparation: Option>, +) -> Session { + if let Err(error) = source.check() { + return Session::failed(error.into()); + } if let Err(error) = launch.validate() { return Session::failed(SessionError::InvalidLaunch(error)); } - let (session, driver) = channel(None); - let session = session.with_exec(Arc::new(LocalExec)); + let (session, driver) = nocterm_session::channel_with_source(None, source.clone()); + let session = session.with_exec(Arc::new(exec::ScopedLocalExec::new(source))); thread::spawn(move || { + if driver.closed().now_or_never().is_some() { + block_on(driver.emit(Event::Closed(CloseReason::ClosedByUser))); + return; + } + let prepared = driver + .source() + .check() + .map_err(SessionError::from) + .and_then(|()| { + let launch = match preparation { + Some(preparation) => preparation + .prepare(launch) + .map_err(SessionError::InvalidLaunch)?, + None => launch, + }; + launch.validate().map_err(SessionError::InvalidLaunch)?; + Ok(launch) + }); + let launch = match prepared { + Ok(launch) => launch, + Err(error) => { + block_on(driver.emit(Event::Closed(CloseReason::Failed(error)))); + return; + } + }; if let Err(error) = start(launch, isolated, term, pty, driver.clone()) { - block_on( - driver.emit(Event::Closed(CloseReason::Failed(SessionError::Other( - error.to_string(), - )))), - ); + let error = match error { + LaunchError::Source(error) => SessionError::Source(error), + other => SessionError::Other(other.to_string()), + }; + block_on(driver.emit(Event::Closed(CloseReason::Failed(error)))); } }); session @@ -87,6 +204,8 @@ fn size(s: PtySize) -> NativeSize { #[derive(Debug, thiserror::Error)] enum LaunchError { + #[error(transparent)] + Source(#[from] nocterm_session::ConnectionSourceError), #[error("native PTY error: {0}")] Pty(String), #[error("native PTY has no file descriptor")] @@ -95,6 +214,9 @@ enum LaunchError { Fcntl(String), #[error("I/O error: {0}")] Io(#[from] std::io::Error), + #[cfg(target_os = "linux")] + #[error(transparent)] + Descriptor(#[from] filedescriptor::Error), } #[expect(clippy::too_many_lines, reason = "predates the limit")] @@ -105,6 +227,7 @@ fn start( pty: PtySize, driver: nocterm_session::SessionDriver, ) -> Result<(), LaunchError> { + driver.source().check()?; let pair = native_pty_system() .openpty(size(pty)) .map_err(|e| LaunchError::Pty(e.to_string()))?; @@ -138,6 +261,13 @@ fn start( .master .take_writer() .map_err(|e| LaunchError::Pty(e.to_string()))?; + #[cfg(target_os = "linux")] + let (mut readiness, close_wake) = readiness::Readiness::new(pair.master.as_ref())?; + driver.source().check()?; + if driver.closed().now_or_never().is_some() { + block_on(driver.emit(Event::Closed(CloseReason::ClosedByUser))); + return Ok(()); + } let mut child = pair .slave .spawn_command(command) @@ -152,6 +282,13 @@ fn start( let shell_group = child_pid.map(|pid| pid as i32); #[cfg(not(unix))] let shell_group = None; + #[cfg(target_os = "linux")] + if let Err(error) = readiness.observe_exit(child.process_id()) { + let mut failed_killer = child.clone_killer(); + kill_process(failed_killer.as_mut(), &master, shell_group); + let _ = child.wait(); + return Err(error.into()); + } let (done_tx, done_rx) = async_channel::bounded::<()>(1); let command_driver = driver.clone(); let closed_by_user = Arc::new(AtomicBool::new(false)); @@ -162,6 +299,10 @@ fn start( let close_master = master.clone(); let command_master = master.clone(); thread::spawn(move || { + // Dropping this pipe wakes a readiness wait even when a descendant + // still holds the slave and killing the shell produces no PTY EOF. + #[cfg(target_os = "linux")] + let _wake = close_wake; let requested = block_on(async { select! { _ = close_driver.closed().fuse() => true, @@ -194,6 +335,9 @@ fn start( } } Some(Command::Resize(s)) => { + if command_driver.source().check().is_err() { + break; + } let _ = command_master .lock() .unwrap_or_else(|e| e.into_inner()) @@ -212,6 +356,8 @@ fn start( kill_process(disconnected_killer.as_mut(), &master, shell_group); } let mut buffer = [0; 8192]; + #[cfg(target_os = "linux")] + let mut read_failure = None; loop { // Closing is independent of slave EOF: a disowned/HUP-ignoring // background job can legitimately retain the terminal descriptor. @@ -229,6 +375,14 @@ fn start( done_tx.close(); break; } + #[cfg(target_os = "linux")] + if let Err(error) = readiness.wait() { + read_failure = Some(error); + let mut failed_killer = child.clone_killer(); + kill_process(failed_killer.as_mut(), &master, shell_group); + break; + } + #[cfg(not(target_os = "linux"))] thread::sleep(Duration::from_millis(10)); } Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {} @@ -255,6 +409,10 @@ fn start( done_tx.close(); // Keep ephemeral scripts alive until the shell has exited. drop(integration); + #[cfg(target_os = "linux")] + if let Some(error) = read_failure { + return Err(error.into()); + } block_on( driver.emit(Event::Closed(if closed_by_user.load(Ordering::Acquire) { CloseReason::ClosedByUser @@ -292,6 +450,7 @@ fn write_input( "PTY closed", )); } + driver.source().check().map_err(std::io::Error::other)?; match writer.write(&bytes[offset..]) { Ok(0) => return Err(std::io::ErrorKind::WriteZero.into()), Ok(count) => offset += count, @@ -309,6 +468,7 @@ fn write_input( "PTY closed", )); } + driver.source().check().map_err(std::io::Error::other)?; match writer.flush() { Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { thread::sleep(Duration::from_millis(10)) @@ -369,24 +529,8 @@ fn prepare_integration( .file_stem() .and_then(|s| s.to_str()) .unwrap_or(""); - let (name, script) = match stem { - "bash" => ( - "bashrc", - include_str!("../../../assets/shell-integration/bash.bash"), - ), - "zsh" => ( - ".zshrc", - include_str!("../../../assets/shell-integration/zsh.zsh"), - ), - "fish" => ( - "integration.fish", - include_str!("../../../assets/shell-integration/fish.fish"), - ), - "pwsh" | "powershell" => ( - "integration.ps1", - include_str!("../../../assets/shell-integration/powershell.ps1"), - ), - _ => return Ok(None), + let Some((name, script)) = integration::bundled(stem) else { + return Ok(None); }; let directory = tempfile::tempdir()?; let path = directory.path().join(name); diff --git a/crates/local/src/readiness.rs b/crates/local/src/readiness.rs new file mode 100644 index 00000000..91f82732 --- /dev/null +++ b/crates/local/src/readiness.rs @@ -0,0 +1,86 @@ +//! Native PTY readiness, with separate wakeups for owner close and leader exit. +//! +//! Linux pidfds let an idle terminal sleep without missing a shell that exits +//! while a detached descendant retains its slave. Older kernels and sandboxes +//! denying pidfds retain the bounded process-status fallback. +use crate::LaunchError; +use filedescriptor::FileDescriptor; +use nix::{ + errno::Errno, + fcntl::OFlag, + poll::{PollFd, PollFlags, PollTimeout, poll}, + unistd::pipe2, +}; +use std::{ + io, + os::fd::{AsFd as _, AsRawFd, OwnedFd}, +}; + +pub(super) struct Readiness { + pty: FileDescriptor, + closing: OwnedFd, + leader: Option, +} + +/// The borrow keeps the native master alive through safe descriptor duplication. +struct Master<'a>(&'a dyn portable_pty::MasterPty); +impl AsRawFd for Master<'_> { + fn as_raw_fd(&self) -> std::os::fd::RawFd { + self.0.as_raw_fd().unwrap_or(-1) + } +} + +impl Readiness { + pub(super) fn new( + master: &dyn portable_pty::MasterPty, + ) -> Result<(Self, OwnedFd), LaunchError> { + let pty = FileDescriptor::dup(&Master(master))?; + let (closing, wake) = pipe2(OFlag::O_CLOEXEC).map_err(io::Error::from)?; + Ok(( + Self { + pty, + closing, + leader: None, + }, + wake, + )) + } + + pub(super) fn observe_exit(&mut self, pid: Option) -> io::Result<()> { + use rustix::{ + io::Errno, + process::{Pid, PidfdFlags, pidfd_open}, + }; + let Some(pid) = pid + .and_then(|pid| i32::try_from(pid).ok()) + .and_then(Pid::from_raw) + else { + return Ok(()); + }; + self.leader = match pidfd_open(pid, PidfdFlags::empty()) { + Ok(fd) => Some(fd), + Err(Errno::NOSYS | Errno::PERM | Errno::ACCESS) => None, + Err(error) => return Err(error.into()), + }; + Ok(()) + } + + pub(super) fn wait(&self) -> io::Result<()> { + let mut descriptors = vec![ + PollFd::new(self.pty.as_fd(), PollFlags::POLLIN), + PollFd::new(self.closing.as_fd(), PollFlags::POLLIN), + ]; + if let Some(leader) = &self.leader { + descriptors.push(PollFd::new(leader.as_fd(), PollFlags::POLLIN)); + } + let timeout = if self.leader.is_some() { + PollTimeout::NONE + } else { + PollTimeout::from(10u16) + }; + match poll(&mut descriptors, timeout) { + Ok(_) | Err(Errno::EINTR) => Ok(()), + Err(error) => Err(error.into()), + } + } +} diff --git a/crates/local/src/tests.rs b/crates/local/src/tests.rs index a3d978b9..83c6c18c 100644 --- a/crates/local/src/tests.rs +++ b/crates/local/src/tests.rs @@ -2,7 +2,7 @@ #![cfg(unix)] use super::*; -fn request(launch: ShellLaunch) -> ConnectRequest { +pub(super) fn request(launch: ShellLaunch) -> ConnectRequest { ConnectRequest { launch, target: nocterm_session::Target::new("user", "localhost", 22), @@ -15,7 +15,7 @@ fn request(launch: ShellLaunch) -> ConnectRequest { jumps: Vec::new(), } } -fn events(session: &Session) -> std::sync::mpsc::Receiver { +pub(super) fn events(session: &Session) -> std::sync::mpsc::Receiver { let session = session.clone(); let (tx, rx) = std::sync::mpsc::channel(); thread::spawn(move || { @@ -30,10 +30,14 @@ fn events(session: &Session) -> std::sync::mpsc::Receiver { }); rx } -fn next(rx: &std::sync::mpsc::Receiver) -> Event { +pub(super) fn next(rx: &std::sync::mpsc::Receiver) -> Event { rx.recv_timeout(std::time::Duration::from_secs(10)) .expect("PTY event timed out") } + +#[cfg(target_os = "linux")] +mod leader_exit; + #[test] fn invalid_launch_reports_typed_failure_before_starting_a_pty() { let launch = ShellLaunch { @@ -382,22 +386,33 @@ fn close_interrupts_full_input_and_kills_shell_process_group() { ); // The direct child is reaped by this adapter. A killed grandchild is // adopted/reaped by init; Linux containers can leave an init-owned - // zombie briefly, but no descendant may remain executing. - let descendant_alive = - nix::sys::signal::kill(nix::unistd::Pid::from_raw(descendant), None).is_ok(); - #[cfg(target_os = "linux")] - if descendant_alive { - let stat = std::fs::read_to_string(format!("/proc/{descendant}/stat")).unwrap_or_default(); - assert!( - stat.is_empty() - || stat - .rsplit_once(") ") - .is_some_and(|(_, fields)| fields.starts_with('Z')), - "descendant still running: {stat}" - ); + // zombie or a dead (X) task briefly, but no descendant may remain executing. + // Close sends SIGKILL to the whole group; on a loaded machine the grandchild + // may not be scheduled to die yet, so it gets a bounded moment to do so. + let executing = || -> Option { + nix::sys::signal::kill(nix::unistd::Pid::from_raw(descendant), None).ok()?; + #[cfg(target_os = "linux")] + { + let stat = + std::fs::read_to_string(format!("/proc/{descendant}/stat")).unwrap_or_default(); + let ended = stat.is_empty() + || stat.rsplit_once(") ").is_some_and(|(_, fields)| { + matches!(fields.split_ascii_whitespace().next(), Some("Z" | "X")) + }); + (!ended).then_some(stat) + } + #[cfg(not(target_os = "linux"))] + Some(String::new()) + }; + let settle = Instant::now() + Duration::from_secs(2); + let mut running = executing(); + while running.is_some() && Instant::now() < settle { + thread::sleep(Duration::from_millis(10)); + running = executing(); + } + if let Some(stat) = running { + panic!("descendant still running: {stat}"); } - #[cfg(not(target_os = "linux"))] - assert!(!descendant_alive); } #[cfg(unix)] @@ -705,3 +720,44 @@ fn dropping_the_last_session_owner_reaps_shell_without_event_consumer() { thread::sleep(Duration::from_millis(5)); } } + +#[test] +fn a_revoked_source_never_starts_a_new_local_pty_or_exec() { + use nocterm_session::{ + ConnectionOpenOptions, ConnectionSourceError, testing::ScriptedConnectionSource, + }; + let source = ScriptedConnectionSource::default(); + source.revoke(); + let launch = ShellLaunch { + program: Some("/bin/sh".into()), + ..Default::default() + }; + let session = LocalTransport(launch.clone()) + .open_with(request(launch), ConnectionOpenOptions::new(source.source())); + assert!(matches!( + block_on(session.next_event()), + Some(Event::Closed(CloseReason::Failed(SessionError::Source( + ConnectionSourceError::Revoked + )))) + )); +} +#[test] +fn a_retained_local_executor_cannot_start_after_its_account_is_revoked() { + use nocterm_session::{ + ConnectionOpenOptions, ExecError, ExecRequest, testing::ScriptedConnectionSource, + }; + let source = ScriptedConnectionSource::default(); + let launch = ShellLaunch { + program: Some("/bin/sh".into()), + integration: false, + ..Default::default() + }; + let session = LocalTransport(launch.clone()) + .open_with(request(launch), ConnectionOpenOptions::new(source.source())); + let executor = session.exec().unwrap(); + let pending = executor.exec(ExecRequest::new("/bin/echo").arg("never")); + source.revoke(); + assert!(matches!(block_on(pending), Err(ExecError::Source(_)))); + assert!(session.exec().is_none()); + session.close(); +} diff --git a/crates/local/src/tests/leader_exit.rs b/crates/local/src/tests/leader_exit.rs new file mode 100644 index 00000000..fadb8828 --- /dev/null +++ b/crates/local/src/tests/leader_exit.rs @@ -0,0 +1,44 @@ +//! A child-exit wake is independent from slave EOF and preserves the leader's tail. +use super::*; +use std::time::Instant; + +#[test] +fn an_exited_leader_closes_after_its_tail_even_when_a_child_holds_the_slave() { + let directory = tempfile::tempdir().unwrap(); + let child_file = directory.path().join("child"); + let ready_file = directory.path().join("ready"); + let launch = ShellLaunch { + program: Some("/bin/sh".into()), + args: vec![ + "-c".into(), + "(trap '' HUP; printf ready > \"$2\"; exec sleep 30) & printf '%s\\n' \"$!\" > \"$1\"; while [ ! -e \"$2\" ]; do sleep 0.001; done; printf leader-tail; exit 7" + .into(), + "leader-test".into(), + child_file.to_string_lossy().into_owned(), + ready_file.to_string_lossy().into_owned(), + ], + integration: false, + ..Default::default() + }; + let session = LocalTransport(launch.clone()).open(request(launch)); + let events = events(&session); + let deadline = Instant::now() + Duration::from_secs(2); + let mut output = Vec::new(); + let reason = loop { + match events.recv_timeout(deadline.saturating_duration_since(Instant::now())) { + Ok(Event::Output(bytes)) => output.extend(bytes), + Ok(Event::Closed(reason)) => break Some(reason), + Ok(_) => {} + Err(_) => break None, + } + }; + // This intentionally orphaned descendant is owned by the fixture. The + // transport must finish before it is removed, without waiting for slave EOF. + if let Ok(pid) = std::fs::read_to_string(child_file) { + let pid = nix::unistd::Pid::from_raw(pid.trim().parse().unwrap()); + let _ = nix::sys::signal::kill(pid, nix::sys::signal::Signal::SIGKILL); + } + session.close(); + assert_eq!(reason, Some(CloseReason::Exited(Some(7)))); + assert_eq!(output, b"leader-tail"); +} diff --git a/crates/monitor-service/Cargo.toml b/crates/monitor-service/Cargo.toml new file mode 100644 index 00000000..ad1166fe --- /dev/null +++ b/crates/monitor-service/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "nocterm-monitor-service" +description = "Shared resource monitoring over captured host execution." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +gpui.workspace = true +nocterm-monitor.workspace = true +nocterm-session.workspace = true + +[dev-dependencies] +gpui = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } +futures.workspace = true + +[lints] +workspace = true diff --git a/crates/monitor-service/src/lib.rs b/crates/monitor-service/src/lib.rs new file mode 100644 index 00000000..2e266fa8 --- /dev/null +++ b/crates/monitor-service/src/lib.rs @@ -0,0 +1,195 @@ +//! Raw resource collection shared by consumers of the same captured executor. +//! Owns: one watch per executor/scope, the metric union and fastest requested cadence, +//! typed watch status, one latest reading, and cancellation when the last consumer leaves. +//! Does not own: selected windows/surfaces, presentation histories, views or native processes. +//! Ports used: HostExec. Ports provided: MonitorWatches, MonitorWatch. Settings: none. Globals: none. +//! Invariants: descriptive host names never merge authority; subscriptions retain their original +//! executor; obsolete plans/results cannot update the current watch generation. +mod watch; +use gpui::{App, AppContext as _, Context, Entity, EntityId, Task, WeakEntity}; +use nocterm_monitor::{MetricSet, Platform, Reading}; +use nocterm_session::{ExecError, HostExec}; +use std::{ + cell::RefCell, + rc::Rc, + sync::{Arc, Weak}, + time::Duration, +}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct WatchPlan { + pub metrics: MetricSet, + pub interval: Duration, +} +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum WatchStatus { + Off, + Detecting, + Starting, + Running, + Unsupported(String), + Failed(ExecError), +} +#[derive(Clone, Default)] +pub struct MonitorWatches { + watched: Rc>>, +} +struct Watched { + exec: Weak, + container: bool, + watch: WeakEntity, +} +impl MonitorWatches { + pub fn watch( + &self, + exec: Arc, + container: bool, + platform: Option, + cx: &mut App, + ) -> Entity { + let mut watched = self.watched.borrow_mut(); + watched.retain(|entry| entry.watch.upgrade().is_some()); + if let Some(watch) = watched + .iter() + .filter(|entry| { + entry.container == container + && std::ptr::addr_eq(entry.exec.as_ptr(), Arc::as_ptr(&exec)) + }) + .find_map(|entry| entry.watch.upgrade()) + { + return watch; + } + let watch = cx.new(|_| MonitorWatch { + exec: exec.clone(), + container, + platform: platform.map(|platform| scoped_platform(platform, container)), + plans: Default::default(), + plan: None, + status: WatchStatus::Off, + reading: None, + sequence: 0, + generation: 0, + task: None, + }); + watched.push(Watched { + exec: Arc::downgrade(&exec), + container, + watch: watch.downgrade(), + }); + watch + } +} +pub struct MonitorWatch { + exec: Arc, + container: bool, + platform: Option, + plans: std::collections::BTreeMap, + plan: Option, + status: WatchStatus, + reading: Option, + sequence: u64, + generation: u64, + task: Option>, +} +impl MonitorWatch { + pub fn platform(&self) -> Option<&Platform> { + self.platform.as_ref() + } + pub fn status(&self) -> &WatchStatus { + &self.status + } + pub fn reading(&self) -> Option<(u64, &Reading)> { + self.reading + .as_ref() + .map(|reading| (self.sequence, reading)) + } + pub fn subscribe(&mut self, consumer: EntityId, plan: WatchPlan, cx: &mut Context) { + if plan.metrics.is_empty() { + self.plans.remove(&consumer); + } else { + self.plans.insert( + consumer, + WatchPlan { + interval: plan.interval.max(Duration::from_secs(1)), + ..plan + }, + ); + } + self.refresh(cx); + } + pub fn unsubscribe(&mut self, consumer: EntityId, cx: &mut Context) { + self.plans.remove(&consumer); + self.refresh(cx); + } + fn refresh(&mut self, cx: &mut Context) { + let plan = self + .plans + .values() + .copied() + .reduce(|left, right| WatchPlan { + metrics: left.metrics.union(right.metrics), + interval: left.interval.min(right.interval), + }); + if self.plan == plan { + return; + } + self.task = None; + self.generation = self.generation.saturating_add(1); + self.plan = plan; + let Some(plan) = plan else { + self.set_status(WatchStatus::Off, cx); + return; + }; + if self.generation == u64::MAX { + self.set_status( + WatchStatus::Failed(ExecError::Failed("Monitor generation exhausted".into())), + cx, + ); + return; + } + let generation = self.generation; + let exec = self.exec.clone(); + let container = self.container; + let platform = self.platform.clone(); + self.set_status( + if platform.is_some() { + WatchStatus::Starting + } else { + WatchStatus::Detecting + }, + cx, + ); + self.task = Some(cx.spawn(async move |this, cx| { + watch::watch(this, exec, container, platform, plan, generation, cx).await + })); + } + fn set_status(&mut self, status: WatchStatus, cx: &mut Context) { + if self.status != status { + self.status = status; + cx.notify(); + } + } + fn accept(&mut self, generation: u64, reading: Reading, cx: &mut Context) -> bool { + if generation != self.generation || self.plan.is_none() { + return false; + } + self.sequence = self.sequence.saturating_add(1); + self.reading = Some(reading); + self.status = WatchStatus::Running; + cx.notify(); + true + } +} + +fn scoped_platform(platform: Platform, container: bool) -> Platform { + if !container { + platform + } else if platform == Platform::Linux || platform == Platform::LinuxContainer { + Platform::LinuxContainer + } else { + Platform::Unsupported("container accounting requires Linux".into()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/monitor-service/src/tests.rs b/crates/monitor-service/src/tests.rs new file mode 100644 index 00000000..6b79312c --- /dev/null +++ b/crates/monitor-service/src/tests.rs @@ -0,0 +1,136 @@ +//! Public captured-watch lifecycle and shared work, using the session execution fake. +use crate::{MonitorWatches, WatchPlan, WatchStatus}; +use gpui::{AppContext as _, TestAppContext}; +use nocterm_monitor::{MetricSet, MonitorMetric, Platform}; +use nocterm_session::{ExecExit, HostExec, testing::FakeExec}; +use std::{sync::Arc, time::Duration}; + +fn plan(metric: MonitorMetric, seconds: u64) -> WatchPlan { + WatchPlan { + metrics: [metric].into_iter().collect(), + interval: Duration::from_secs(seconds), + } +} + +#[gpui::test] +fn consumers_share_one_captured_watch_and_the_last_one_stops_it(cx: &mut TestAppContext) { + let exec = Arc::new(FakeExec::default()); + let sink = exec.queue_stream("sh"); + let host: Arc = exec.clone(); + let registry = MonitorWatches::default(); + let (watch, again, first, second) = cx.update(|cx| { + let first = cx.new(|_| ()); + let second = cx.new(|_| ()); + let watch = registry.watch(host.clone(), false, Some(Platform::Linux), cx); + let again = registry.watch(host, false, Some(Platform::Linux), cx); + watch.update(cx, |watch, cx| { + watch.subscribe(first.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + again.update(cx, |watch, cx| { + watch.subscribe(second.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + (watch, again, first, second) + }); + cx.run_until_parked(); + assert_eq!(watch.entity_id(), again.entity_id()); + assert_eq!(exec.requests().len(), 1); + assert!(!sink.is_closed()); + watch.update(cx, |watch, cx| watch.unsubscribe(first.entity_id(), cx)); + cx.run_until_parked(); + assert!(!sink.is_closed()); + again.update(cx, |watch, cx| watch.unsubscribe(second.entity_id(), cx)); + cx.run_until_parked(); + assert!(sink.is_closed()); + watch.read_with(cx, |watch, _| assert_eq!(watch.status(), &WatchStatus::Off)); +} + +#[gpui::test] +fn details_expand_one_collection_and_releasing_them_restores_the_remaining_plan( + cx: &mut TestAppContext, +) { + let exec = Arc::new(FakeExec::default()); + let slow = exec.queue_stream("sh"); + let registry = MonitorWatches::default(); + let (watch, summary, details) = cx.update(|cx| { + let summary = cx.new(|_| ()); + let details = cx.new(|_| ()); + let watch = registry.watch(exec.clone(), false, Some(Platform::Linux), cx); + watch.update(cx, |watch, cx| { + watch.subscribe(summary.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + (watch, summary, details) + }); + cx.run_until_parked(); + let fast = exec.queue_stream("sh"); + watch.update(cx, |watch, cx| { + watch.subscribe( + details.entity_id(), + WatchPlan { + metrics: MetricSet::all(), + interval: Duration::from_secs(2), + }, + cx, + ) + }); + cx.run_until_parked(); + assert!(slow.is_closed()); + assert!(!fast.is_closed()); + let request = exec.requests().last().unwrap().stdin.clone().unwrap(); + assert!(String::from_utf8(request).unwrap().contains("@@netdev")); + let restored = exec.queue_stream("sh"); + watch.update(cx, |watch, cx| watch.unsubscribe(details.entity_id(), cx)); + cx.run_until_parked(); + assert!(fast.is_closed()); + assert!(!restored.is_closed()); + let request = exec.requests().last().unwrap().stdin.clone().unwrap(); + assert!(!String::from_utf8(request).unwrap().contains("@@netdev")); + watch.update(cx, |watch, cx| watch.unsubscribe(summary.entity_id(), cx)); +} + +#[gpui::test] +fn different_executor_authorities_never_share_a_collection(cx: &mut TestAppContext) { + let registry = MonitorWatches::default(); + let first = Arc::new(FakeExec::default()); + let second = Arc::new(FakeExec::default()); + let (a, b, c) = cx.update(|cx| { + ( + registry.watch(first.clone(), false, None, cx), + registry.watch(second, false, None, cx), + registry.watch(first, true, Some(Platform::Linux), cx), + ) + }); + assert_ne!(a.entity_id(), b.entity_id()); + assert_ne!(a.entity_id(), c.entity_id()); + c.read_with(cx, |watch, _| { + assert_eq!(watch.platform(), Some(&Platform::LinuxContainer)) + }); +} + +#[gpui::test] +fn one_latest_reading_is_shared_as_data_without_a_second_sampler(cx: &mut TestAppContext) { + let exec = Arc::new(FakeExec::default()); + let sink = exec.queue_stream("sh"); + let registry = MonitorWatches::default(); + let (watch, consumer) = cx.update(|cx| { + let consumer = cx.new(|_| ()); + let watch = registry.watch(exec, false, Some(Platform::Linux), cx); + watch.update(cx, |watch, cx| { + watch.subscribe(consumer.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + (watch, consumer) + }); + cx.run_until_parked(); + futures::executor::block_on( + sink.send(b"@@meminfo\nMemTotal: 1000\nMemAvailable: 250\n@@end\n".to_vec()), + ); + cx.run_until_parked(); + watch.read_with(cx, |watch, _| { + assert_eq!(watch.status(), &WatchStatus::Running); + assert_eq!( + watch.reading().unwrap().1.memory, + Some((1000 * 1024, 250 * 1024)) + ); + }); + watch.update(cx, |watch, cx| watch.unsubscribe(consumer.entity_id(), cx)); + sink.finish(ExecExit::default()); +} diff --git a/crates/monitor-service/src/watch.rs b/crates/monitor-service/src/watch.rs new file mode 100644 index 00000000..b11517ef --- /dev/null +++ b/crates/monitor-service/src/watch.rs @@ -0,0 +1,110 @@ +//! One generation-bound collection attempt and bounded retry cadence. +use crate::{MonitorWatch, WatchPlan, WatchStatus}; +use gpui::{AsyncApp, WeakEntity}; +use nocterm_monitor::{Platform, Watch, detect}; +use nocterm_session::{ExecError, HostExec}; +use std::{sync::Arc, time::Duration}; + +pub(super) async fn watch( + this: WeakEntity, + exec: Arc, + container: bool, + mut platform: Option, + plan: WatchPlan, + generation: u64, + cx: &mut AsyncApp, +) { + loop { + let outcome = run( + &this, + exec.as_ref(), + container, + &mut platform, + plan, + generation, + cx, + ) + .await; + let retry = this.update(cx, |this, cx| { + if this.generation != generation || this.plan.is_none() { + return false; + } + let status = match outcome { + Err(ExecError::Unsupported) => { + let name = match &platform { + Some(Platform::Unsupported(name)) => name.clone(), + Some(Platform::LinuxContainer) => "container cgroup accounting".into(), + _ => "unknown".into(), + }; + this.set_status(WatchStatus::Unsupported(name), cx); + return false; + } + Err(error @ ExecError::Source(_)) => { + this.set_status(WatchStatus::Failed(error), cx); + return false; + } + Err(error) => WatchStatus::Failed(error), + Ok(()) => { + WatchStatus::Failed(ExecError::Failed("The monitor script stopped.".into())) + } + }; + this.set_status(status, cx); + true + }); + if !matches!(retry, Ok(true)) { + return; + } + cx.background_executor() + .timer(Duration::from_secs(10)) + .await; + } +} +async fn run( + this: &WeakEntity, + exec: &dyn HostExec, + container: bool, + platform: &mut Option, + plan: WatchPlan, + generation: u64, + cx: &mut AsyncApp, +) -> Result<(), ExecError> { + let detected = match platform.clone() { + Some(platform) => platform, + None => { + let detected = detect(exec).await?; + let detected = crate::scoped_platform(detected, container); + *platform = Some(detected.clone()); + let accepted = this + .update(cx, |this, cx| { + if this.generation != generation { + return false; + } + this.platform = Some(detected.clone()); + this.set_status(WatchStatus::Starting, cx); + true + }) + .map_err(|_| ExecError::Disconnected)?; + if !accepted { + return Err(ExecError::Disconnected); + } + detected + } + }; + let mut watch = Watch::start(exec, detected, plan.metrics, plan.interval).await?; + while let Some(reading) = watch.next().await { + if container + && reading.container.as_ref().is_none_or(|counters| { + counters.cpu_nanoseconds.is_none() && counters.memory_current.is_none() + }) + { + return Err(ExecError::Unsupported); + } + if !matches!( + this.update(cx, |this, cx| this.accept(generation, reading, cx)), + Ok(true) + ) { + return Ok(()); + } + } + Ok(()) +} diff --git a/crates/monitor-ui/Cargo.toml b/crates/monitor-ui/Cargo.toml index abfdd407..c2f761f0 100644 --- a/crates/monitor-ui/Cargo.toml +++ b/crates/monitor-ui/Cargo.toml @@ -11,6 +11,7 @@ layer = "feature" [dependencies] nocterm-monitor.workspace = true +nocterm-monitor-service.workspace = true nocterm-plugin-model.workspace = true nocterm-project-model.workspace = true nocterm-project-providers.workspace = true diff --git a/crates/monitor-ui/src/installed.rs b/crates/monitor-ui/src/installed.rs index ba6c85fc..f6ecc107 100644 --- a/crates/monitor-ui/src/installed.rs +++ b/crates/monitor-ui/src/installed.rs @@ -8,8 +8,12 @@ use std::sync::Arc; pub struct Installed { settings: SettingsHandle, local: Option>, + collectors: nocterm_monitor_service::MonitorWatches, } impl Installed { + pub(crate) fn collectors(&self) -> &nocterm_monitor_service::MonitorWatches { + &self.collectors + } pub(crate) fn settings(&self) -> &SettingsHandle { &self.settings } @@ -26,5 +30,6 @@ pub fn install( Installed { settings: ready.settings().clone(), local, + collectors: Default::default(), } } diff --git a/crates/monitor-ui/src/model.rs b/crates/monitor-ui/src/model.rs index d09c1834..e11392d9 100644 --- a/crates/monitor-ui/src/model.rs +++ b/crates/monitor-ui/src/model.rs @@ -4,19 +4,18 @@ //! observe only their original inner session; unavailable resources never fall //! back to an outer or local executor. History is bounded per surface/resource. +use nocterm_monitor_service::{MonitorWatch, MonitorWatches, WatchPlan, WatchStatus}; use std::{sync::Arc, time::Duration}; use crate::MonitorSettings; #[cfg(test)] pub(crate) use crate::source::watched_host; use crate::source::{MonitorKey, WatchHost, watched_source}; -use gpui_kit::{App, AsyncApp, Context, Entity, EventEmitter, Subscription, Task, WeakEntity}; -use nocterm_monitor::{MetricSet, Platform, Sampler, Watch, detect, local_platform}; -use nocterm_session::{ExecError, HostExec}; +use gpui_kit::{App, Context, Entity, EventEmitter, Subscription}; +use nocterm_monitor::{MetricSet, Platform, Sampler, local_platform}; +use nocterm_session::HostExec; use nocterm_workspace::{HostKey, MonitorSource, Workspace, WorkspaceEvent}; -/// How long a failed or ended watch waits before it is tried again. -const RETRY: Duration = Duration::from_secs(10); /// Hosts whose platform and history are remembered for switching back. const REMEMBERED_HOSTS: usize = 8; @@ -65,11 +64,12 @@ pub(crate) enum Status { /// A remembered host: what it is and what it looked like. struct Record { key: MonitorKey, - platform: Option, sampler: Sampler, + platform: Option<(std::sync::Weak, Platform)>, } pub(crate) struct HostMonitor { + consumer: gpui_kit::EntityId, settings: nocterm_store::SettingsHandle, local: Option>, source: MonitorSource, @@ -80,9 +80,17 @@ pub(crate) struct HostMonitor { records: Vec, /// The host and plan the task was started for. wanted: Option<(WatchHost, Plan)>, - task: Option>, + collectors: MonitorWatches, + watch: Option, generation: u64, - _subscriptions: [Subscription; 2], + _subscriptions: Vec, +} + +/// The model's retained subscription and last applied raw reading. +struct CollectedWatch { + collection: Entity, + sequence: u64, + _observation: Subscription, } /// Fired whenever new readings or a new status are available. @@ -120,6 +128,7 @@ impl HostMonitor { let _ = weak.update(cx, |this, cx| this.refresh(cx)); }); let mut this = Self { + consumer: cx.entity_id(), local: installed.local(), settings: installed.settings().clone(), source, @@ -128,9 +137,14 @@ impl HostMonitor { status: Status::Off, records: Vec::new(), wanted: None, - task: None, + collectors: installed.collectors().clone(), + watch: None, generation: 0, - _subscriptions: [follow, observation], + _subscriptions: vec![ + follow, + observation, + cx.on_release(|this, cx| this.stop_watch(cx)), + ], }; this.refresh(cx); this @@ -200,11 +214,11 @@ impl HostMonitor { if unchanged { return; } - self.task = None; self.generation = self.generation.saturating_add(1); self.wanted = wanted; self.host = host; let Some((host, plan)) = self.wanted.clone() else { + self.stop_watch(cx); self.set_status( if settings.enabled && plan.is_some() @@ -220,29 +234,119 @@ impl HostMonitor { }; self.remember(host.key(), history); if !host.host().connected { + self.stop_watch(cx); self.set_status(Status::Offline, cx); return; } - let platform = match host.host().key { - HostKey::Local => Some(local_platform()), - HostKey::Remote { .. } => self.records[0].platform.clone(), - }; - self.set_status( - if platform.is_some() { - Status::Starting - } else { - Status::Detecting - }, - cx, - ); if self.generation == u64::MAX { + self.stop_watch(cx); self.set_status(Status::Failed("Monitor generation exhausted".into()), cx); return; } + self.attach(host, plan, cx); + } + + fn attach(&mut self, host: WatchHost, plan: Plan, cx: &mut Context) { + let platform = if matches!(host.host().key, HostKey::Local) { + Some(local_platform()) + } else { + self.records[0] + .platform + .as_ref() + .filter(|(exec, _)| { + std::ptr::addr_eq(exec.as_ptr(), Arc::as_ptr(&host.host().exec)) + }) + .map(|(_, platform)| platform.clone()) + }; + let collection = + self.collectors + .watch(host.host().exec.clone(), host.container(), platform, cx); + let old = self.watch.take(); + let sequence = old + .as_ref() + .filter(|old| old.collection == collection) + .map_or(0, |old| old.sequence); + if let Some(old) = old + && old.collection != collection + { + old.collection + .update(cx, |watch, cx| watch.unsubscribe(self.consumer, cx)); + } let generation = self.generation; - self.task = Some(cx.spawn(async move |this, cx| { - watch(this, host, platform, plan, generation, cx).await; - })); + let observation = cx.observe(&collection, move |this, collection, cx| { + this.apply_collection(&collection, generation, cx) + }); + let consumer = cx.entity_id(); + collection.update(cx, |watch, cx| { + watch.subscribe( + consumer, + WatchPlan { + metrics: plan.metrics, + interval: plan.interval, + }, + cx, + ) + }); + self.watch = Some(CollectedWatch { + collection: collection.clone(), + sequence, + _observation: observation, + }); + self.apply_collection(&collection, generation, cx); + } + + fn stop_watch(&mut self, cx: &mut App) { + if let Some(watch) = self.watch.take() { + let consumer = self.consumer; + watch + .collection + .update(cx, |watch, cx| watch.unsubscribe(consumer, cx)); + } + } + + fn apply_collection( + &mut self, + collection: &Entity, + generation: u64, + cx: &mut Context, + ) { + if self.generation != generation { + return; + } + let Some(watch) = self + .watch + .as_mut() + .filter(|watch| watch.collection == *collection) + else { + return; + }; + let collected = collection.read(cx); + if let Some(platform) = collected.platform() + && let (Some(host), Some(record)) = (&self.host, self.records.first_mut()) + { + record.platform = Some((Arc::downgrade(&host.host().exec), platform.clone())); + } + let status = match collected.status() { + WatchStatus::Off => Status::Off, + WatchStatus::Detecting => Status::Detecting, + WatchStatus::Starting => Status::Starting, + WatchStatus::Running => Status::Running, + WatchStatus::Unsupported(name) => Status::Unsupported(name.clone()), + WatchStatus::Failed(error) => Status::Failed(error.to_string()), + }; + let reading = collected + .reading() + .filter(|(sequence, _)| *sequence != watch.sequence) + .map(|(sequence, reading)| (sequence, reading.clone())); + if let Some((sequence, reading)) = reading { + watch.sequence = sequence; + if let Some(record) = self.records.first_mut() { + record.sampler.apply(reading); + cx.emit(Changed); + cx.notify(); + } + } + self.set_status(status, cx); } /// Moves the record of `key` to the front, creating it if need be. @@ -251,22 +355,14 @@ impl HostMonitor { Some(index) => self.records.remove(index), None => Record { key: key.clone(), - platform: None, sampler: Sampler::new(history), + platform: None, }, }; self.records.insert(0, record); self.records.truncate(REMEMBERED_HOSTS); } - /// The record of the watched host, if it is still `key`. - fn current(&mut self, key: &MonitorKey, generation: u64) -> Option<&mut Record> { - if self.generation != generation { - return None; - } - self.records.first_mut().filter(|record| record.key == *key) - } - fn set_status(&mut self, status: Status, cx: &mut Context) { if self.status != status { self.status = status; @@ -275,119 +371,3 @@ impl HostMonitor { } } } - -/// Detects the host if need be, then runs its script, again after failures. -async fn watch( - this: WeakEntity, - host: WatchHost, - mut platform: Option, - plan: Plan, - generation: u64, - cx: &mut AsyncApp, -) { - let key = host.key().clone(); - loop { - let outcome = run(&this, &host, &mut platform, plan, generation, cx).await; - let retry = this.update(cx, |this, cx| { - if this.current(&key, generation).is_none() { - return false; - } - let status = match outcome { - Err(ExecError::Unsupported) => { - let name = match &platform { - Some(Platform::Unsupported(name)) => name.clone(), - Some(Platform::LinuxContainer) => "container cgroup accounting".into(), - _ => "unknown".into(), - }; - this.set_status(Status::Unsupported(name), cx); - return false; - } - Err(ExecError::Disconnected) => "The connection closed.".into(), - Err(ExecError::Failed(error)) => error, - Err(error @ ExecError::NotFound(_)) => error.to_string(), - Ok(()) => "The monitor script stopped.".into(), - }; - if this.current(&key, generation).is_some() { - this.set_status(Status::Failed(status), cx); - } - true - }); - if !matches!(retry, Ok(true)) { - return; - } - cx.background_executor().timer(RETRY).await; - } -} - -/// One attempt: detection, then readings until the script ends. -async fn run( - this: &WeakEntity, - host: &WatchHost, - platform: &mut Option, - plan: Plan, - generation: u64, - cx: &mut AsyncApp, -) -> Result<(), ExecError> { - let detected = match platform.clone() { - Some(platform) => platform, - None => { - let detected = detect(host.host().exec.as_ref()).await?; - let detected = if host.container() && detected == Platform::Linux { - Platform::LinuxContainer - } else if host.container() { - Platform::Unsupported("container accounting requires Linux".into()) - } else { - detected - }; - *platform = Some(detected.clone()); - let key = host.key().clone(); - let remembered = detected.clone(); - let accepted = this - .update(cx, |this, cx| { - if let Some(record) = this.current(&key, generation) { - record.platform = Some(remembered); - this.set_status(Status::Starting, cx); - true - } else { - false - } - }) - .map_err(|_| ExecError::Disconnected)?; - if !accepted { - return Err(ExecError::Disconnected); - } - detected - } - }; - let mut watch = Watch::start( - host.host().exec.as_ref(), - detected, - plan.metrics, - plan.interval, - ) - .await?; - while let Some(reading) = watch.next().await { - if host.container() - && reading.container.as_ref().is_none_or(|counters| { - counters.cpu_nanoseconds.is_none() && counters.memory_current.is_none() - }) - { - return Err(ExecError::Unsupported); - } - let key = host.key().clone(); - let applied = this.update(cx, |this, cx| { - let Some(record) = this.current(&key, generation) else { - return false; - }; - record.sampler.apply(reading); - this.status = Status::Running; - cx.emit(Changed); - cx.notify(); - true - }); - if !matches!(applied, Ok(true)) { - return Ok(()); - } - } - Ok(()) -} diff --git a/crates/monitor-ui/src/plugin.rs b/crates/monitor-ui/src/plugin.rs index d9a84a65..1410bad6 100644 --- a/crates/monitor-ui/src/plugin.rs +++ b/crates/monitor-ui/src/plugin.rs @@ -20,6 +20,7 @@ pub const SUMMARY: SystemContribution = SystemContribution { }, panel: None, preview: None, + edge: None, pinned: false, }; diff --git a/crates/monitor-ui/src/tests/dependencies.rs b/crates/monitor-ui/src/tests/dependencies.rs index 11d6e3a4..f709f45f 100644 --- a/crates/monitor-ui/src/tests/dependencies.rs +++ b/crates/monitor-ui/src/tests/dependencies.rs @@ -59,19 +59,25 @@ fn independent_policy_cannot_redirect_an_existing_watch_and_closed_model_is_rele let weak = monitor.downgrade(); cx.update_window(window, |_, window, _| window.remove_window()) .unwrap(); - drop(workspace); - drop(monitor); + // Flush entity release before admitting a new consumer. Merely parking + // the executor does not run GPUI's deferred release callbacks. + cx.update(|_| { + drop(workspace); + drop(monitor); + }); cx.run_until_parked(); assert!(weak.upgrade().is_none()); + assert!( + !host.reading(1), + "the last subscriber releases its native watch" + ); let (_, new_workspace, new_monitor) = cx.update(|cx| { let (window, workspace) = gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { - Workspace::new( - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)), - window, - cx, - ) + let deps = + nocterm_workspace::test_support::deps_for(&[&crate::plugin::PLUGIN], cx); + Workspace::new(deps, window, cx) }) }) .unwrap(); @@ -86,8 +92,11 @@ fn independent_policy_cannot_redirect_an_existing_watch_and_closed_model_is_rele (window, workspace, monitor) }); cx.run_until_parked(); - assert!(host.reading(2)); - assert_ne!(status(cx, &new_monitor), Status::Off); + assert_eq!(status(cx, &new_monitor), Status::Starting); + assert!( + host.reading(2), + "a new subscriber gets a fresh native watch" + ); drop(new_workspace); } diff --git a/crates/plugin-abi/abi-golden.json b/crates/plugin-abi/abi-golden.json index 876c4630..605685f1 100644 --- a/crates/plugin-abi/abi-golden.json +++ b/crates/plugin-abi/abi-golden.json @@ -22,6 +22,27 @@ "call-render-workspace-kinds": "00036261720410776f726b73706163652e6b696e642e300001000110776f726b73706163652e6b696e642e310001010010776f726b73706163652e6b696e642e320001020110776f726b73706163652e6b696e642e33000103000000010000", "call-render-zone-left": "0003626172000000000000", "call-render-zone-center": "0003626172000000000100", - "call-render-zone-right": "0003626172000000000200" + "call-render-zone-right": "0003626172000000000200", + "call-event-response": "010570616e656c040773657276657273c8010e7b2273657276657273223a7b7d7d000000010000", + "call-event-timer": "010570616e656c0504706f6c6c000000010000", + "call-event-terminal-line": "010570616e656c060373727606736179206869000000010000", + "call-event-terminal-closed": "010570616e656c0703737276000000010000", + "reply-effects-sessions": "01070102723005000a2f6170692f6974656d7300000102723105010a2f6170692f6974656d7301127b22616374696f6e223a227374617274227d000102723205020a2f6170692f6974656d7300000102723305030a2f6170692f6974656d7300000104706f6c6c06d00f00070570616e656c03737276054275696c64000008037372760f1b5b33326d72656164791b5b306d0a00", + "reply-view-code": "000100000b07610a2020202062010000", + "reply-view-glyphs": "004200000000010000030000010000030100010000030200010000030300010000030400010000030500010000030600010000030700010000030800010000030900010000030a00010000030b00010000030c00010000030d00010000030e00010000030f00010000031000010000031100010000031200010000031300010000031400010000031500010000031600010000031700010000031800010000031900010000031a00010000031b00010000031c00010000031d00010000031e00010000031f00010000032000010000032100010000032200010000032300010000032400010000032500010000032600010000032700010000032800010000032900010000032a00010000032b00010000032c00010000032d00010000032e00010000032f00010000033000010000033100010000033200010000033300010000033400010000033500010000033600010000033700010000033800010000033900010000033a00010000033b00010000033c00010000033d00010000033e00010000033f000100000340000000", + "reply-view-components": "0005000107736572766572730c07536572766572730101320000010001046f70656e0d054275696c64010c6f6e20c2b7206e6f64652031013201020101010473746f700e360453746f70000100000f0a6f6e65206973206f666601010000100343505503333825030000", + "call-event-file-list": "010570616e656c080373727600000e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-stat": "010570616e656c080373727601010e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-read": "010570616e656c080373727602020e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-write": "010570616e656c080373727603030e2f706c7567696e732f612e796d6c045457467501000000010000", + "call-event-file-create-dir": "010570616e656c080373727604040e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-remove": "010570616e656c080373727605050e2f706c7567696e732f612e796d6c00000000010000", + "call-event-file-rename": "010570616e656c080373727606060e2f706c7567696e732f612e796d6c062f622e796d6c000000010000", + "reply-effects-files": "010800090373727600000105612e796d6c00010c0180e2cfaa0600090373727601010105612e796d6c00010c0180e2cfaa0600090373727602020454574675000903737276030300090373727604040000090373727605040404627573790104726561640501062f77726974650104545746750100070570616e656c03737276054275696c640100", + "call-render-toolbar": "0003626172000000020100", + "call-render-terminal-bar": "020570616e656c03737276000000020100", + "call-event-terminal-bar": "030570616e656c03737276000772657374617274000000020100", + "reply-effects-terminal-bar": "0105000a037372760100000a037372760101000a037372760102000a037372760103000a037372760000", + "reply-view-charts": "0003000001000100001101030c1e240164013c00010000110203040907030203050000030000" } } diff --git a/crates/plugin-abi/src/lib.rs b/crates/plugin-abi/src/lib.rs index 0732a951..95cdcd92 100644 --- a/crates/plugin-abi/src/lib.rs +++ b/crates/plugin-abi/src/lib.rs @@ -2,7 +2,8 @@ //! their postcard encoding. Shared by the wasm runtime adapter and the guest SDK. //! //! Owns: ABI_VERSION, export names, Call/Reply/Wire* and their conversions from plugin-api -//! values, size bounds, canonical samples for the golden file. +//! values (`Call::render`/`event`/`into_parts` carry a terminal bar's terminal in its own +//! call variants), size bounds, canonical samples for the golden file. //! Does not own: running modules (nocterm-plugin-wasm), package files (nocterm-plugin-packages), //! manifests and views (nocterm-plugin-api). //! Ports: none. Settings: none. Globals: none. diff --git a/crates/plugin-abi/src/samples.rs b/crates/plugin-abi/src/samples.rs index 005c10d1..7915cf57 100644 --- a/crates/plugin-abi/src/samples.rs +++ b/crates/plugin-abi/src/samples.rs @@ -4,14 +4,17 @@ use alloc::{string::String, vec, vec::Vec}; use nocterm_plugin_api::{ - ContributionContext, Effect, EffectOutcome, EffectRequest, Effects, Event, HostError, Kind, - LocalId, Node, NodeKind, PluginIcon, Refresh, Tone, View, Zone, + ContributionContext, Effect, EffectOutcome, EffectRequest, Effects, Event, Glyph, HostError, + Kind, LocalId, Node, NodeKind, PluginIcon, Refresh, Tone, View, Zone, capability::{ clock::ClockNow, monitor::Usage, + net::{Method, Request}, notifications::{NoticeTone, OwnNotice, OwnNotices, Post}, panels::{SetBadge, SetOpen}, storage::{Remove, Set}, + terminals::{Open, Write}, + timers::SetTimer, transfers::Counts, workspace::{WorkspaceKind, WorkspaceState}, }, @@ -28,6 +31,10 @@ pub struct Sample { pub bytes: Vec, } +mod bars; +mod charts; +mod files; + fn local(name: &str) -> LocalId { LocalId::parse(name).expect("a sample local name is valid") } @@ -149,12 +156,7 @@ fn full_view(refresh: Refresh) -> View { nodes: vec![ node(None, None, NodeKind::Column, Tone::Default), node(Some(0), None, NodeKind::Row, Tone::Muted), - node( - Some(1), - None, - NodeKind::Icon(PluginIcon::Puzzle), - Tone::Success, - ), + node(Some(1), None, NodeKind::Icon(Glyph::Puzzle), Tone::Success), node( Some(1), None, @@ -227,13 +229,95 @@ fn full_effects() -> Effects { ]) } +/// The glyphs and the components of a view. +fn component_samples() -> [Sample; 2] { + [ + sample( + "reply-view-glyphs", + encode_reply(&reply(Answer::View(glyphs_view()), &[])), + ), + sample( + "reply-view-components", + encode_reply(&reply(Answer::View(components_view()), &[])), + ), + ] +} + +/// A view with one icon node for every glyph, so a reordered glyph changes a sample. +fn glyphs_view() -> View { + let mut nodes = vec![node(None, None, NodeKind::Row, Tone::Default)]; + nodes.extend( + Glyph::ALL + .into_iter() + .map(|glyph| node(Some(0), None, NodeKind::Icon(glyph), Tone::Default)), + ); + View { + nodes, + refresh: None, + } +} + +/// A section of one item with an icon button, a caption and a field. +fn components_view() -> View { + View { + nodes: vec![ + node( + None, + Some("servers"), + NodeKind::Section { + title: String::from("Servers"), + count: Some(String::from("2")), + folded: false, + }, + Tone::Default, + ), + node( + Some(0), + Some("open"), + NodeKind::Item { + title: String::from("Build"), + detail: Some(String::from("on \u{b7} node 1")), + icon: Some(Glyph::Server), + selected: true, + }, + Tone::Success, + ), + node( + Some(1), + Some("stop"), + NodeKind::IconButton { + icon: Glyph::Stop, + label: String::from("Stop"), + }, + Tone::Default, + ), + node( + Some(0), + None, + NodeKind::Caption(String::from("one is off")), + Tone::Muted, + ), + node( + Some(0), + None, + NodeKind::Field { + label: String::from("CPU"), + value: String::from("38%"), + }, + Tone::Caution, + ), + ], + refresh: None, + } +} + /// A view with one icon node for every icon, so a reordered icon changes a sample. fn icons_view() -> View { let mut nodes = vec![node(None, None, NodeKind::Row, Tone::Default)]; nodes.extend( PluginIcon::ALL .into_iter() - .map(|icon| node(Some(0), None, NodeKind::Icon(icon), Tone::Default)), + .map(|icon| node(Some(0), None, NodeKind::Icon(icon.into()), Tone::Default)), ); View { nodes, @@ -387,5 +471,128 @@ pub fn samples() -> Vec { }), ) })); + samples.extend(session_samples()); + samples +} + +/// The samples of the requests, timers, terminals, code blocks and toolbars, appended after +/// the first set so its bytes stay as they were. +fn session_samples() -> Vec { + let events = [ + ( + "call-event-response", + Event::Response { + key: String::from("servers"), + status: 200, + body: String::from("{\"servers\":{}}"), + }, + ), + ( + "call-event-timer", + Event::Timer { + key: String::from("poll"), + }, + ), + ( + "call-event-terminal-line", + Event::TerminalLine { + terminal: String::from("srv"), + line: String::from("say hi"), + }, + ), + ( + "call-event-terminal-closed", + Event::TerminalClosed { + terminal: String::from("srv"), + }, + ), + ]; + let mut samples: Vec = events + .into_iter() + .map(|(name, event)| { + sample( + name, + encode_call(&Call::Event { + local: local("panel"), + event, + snapshot: empty_snapshot(), + }), + ) + }) + .collect(); + samples.push(sample( + "reply-effects-sessions", + encode_reply(&reply(Answer::Effects(session_effects()), &[])), + )); + samples.push(sample( + "reply-view-code", + encode_reply(&reply( + Answer::View(View { + nodes: vec![node( + None, + None, + NodeKind::Code(String::from("a\n b")), + Tone::Muted, + )], + refresh: None, + }), + &[], + )), + )); + samples.extend(component_samples()); + samples.extend(files::samples()); + samples.push(sample( + "call-render-toolbar", + encode_call(&Call::Render { + local: local("bar"), + snapshot: WireSnapshot { + context: ContributionContext { + kind: Kind::Toolbar, + zone: Zone::Center, + }, + ..empty_snapshot() + }, + }), + )); + samples.extend(bars::samples()); + samples.extend(charts::samples()); samples } + +/// A request of every method, a timer and both terminal effects. +fn session_effects() -> Effects { + let mut effects: Vec = Method::ALL + .into_iter() + .enumerate() + .map(|(index, method)| EffectRequest { + key: Some(alloc::format!("r{index}")), + effect: Effect::NetRequest(Request { + method, + path: String::from("/api/items"), + body: (method == Method::Post).then(|| String::from("{\"action\":\"start\"}")), + bytes: false, + }), + }) + .collect(); + effects.push(EffectRequest { + key: Some(String::from("poll")), + effect: Effect::TimersSet(SetTimer { after_ms: 2000 }), + }); + effects.push(EffectRequest { + key: None, + effect: Effect::TerminalsOpen(Open { + local: local("panel"), + terminal: String::from("srv"), + title: String::from("Build"), + files: false, + }), + }); + effects.push(EffectRequest { + key: None, + effect: Effect::TerminalsWrite(Write { + terminal: String::from("srv"), + text: String::from("\u{1b}[32mready\u{1b}[0m\n"), + }), + }); + Effects(effects) +} diff --git a/crates/plugin-abi/src/samples/bars.rs b/crates/plugin-abi/src/samples/bars.rs new file mode 100644 index 00000000..63a87ad8 --- /dev/null +++ b/crates/plugin-abi/src/samples/bars.rs @@ -0,0 +1,62 @@ +//! Samples of the bar of a plugin's terminal: the call that draws it, an event of it, and the +//! effects that show, move and take it away. + +use alloc::{string::String, vec, vec::Vec}; + +use nocterm_plugin_api::{ + ContributionContext, Edge, Effect, EffectRequest, Effects, Event, Kind, Zone, + capability::terminals::Bar, +}; + +use super::{Sample, empty_snapshot, local, reply, sample}; +use crate::{Answer, Call, WireSnapshot, encode_call, encode_reply}; + +fn bar_snapshot() -> WireSnapshot { + WireSnapshot { + context: ContributionContext { + kind: Kind::Toolbar, + zone: Zone::Center, + }, + ..empty_snapshot() + } +} + +pub(super) fn samples() -> Vec { + let effects = Edge::ALL + .into_iter() + .map(Some) + .chain([None]) + .map(|edge| EffectRequest { + key: None, + effect: Effect::TerminalsBar(Bar { + terminal: String::from("srv"), + edge, + }), + }) + .collect(); + vec![ + sample( + "call-render-terminal-bar", + encode_call(&Call::RenderTerminalBar { + local: local("panel"), + terminal: String::from("srv"), + snapshot: bar_snapshot(), + }), + ), + sample( + "call-event-terminal-bar", + encode_call(&Call::TerminalBarEvent { + local: local("panel"), + terminal: String::from("srv"), + event: Event::Activated { + key: String::from("restart"), + }, + snapshot: bar_snapshot(), + }), + ), + sample( + "reply-effects-terminal-bar", + encode_reply(&reply(Answer::Effects(Effects(effects)), &[])), + ), + ] +} diff --git a/crates/plugin-abi/src/samples/charts.rs b/crates/plugin-abi/src/samples/charts.rs new file mode 100644 index 00000000..c7e8c0fb --- /dev/null +++ b/crates/plugin-abi/src/samples/charts.rs @@ -0,0 +1,42 @@ +//! A sample of a view with charts: one series against a top value and a span, and two series +//! scaled to their largest value. + +use alloc::{vec, vec::Vec}; + +use nocterm_plugin_api::{NodeKind, Tone, View}; + +use super::{Sample, node, reply, sample}; +use crate::{Answer, encode_reply}; + +pub(super) fn samples() -> Vec { + let view = View { + nodes: vec![ + node(None, None, NodeKind::Column, Tone::Default), + node( + Some(0), + None, + NodeKind::Chart { + series: vec![vec![12, 30, 36]], + max: Some(100), + span: Some(60), + }, + Tone::Default, + ), + node( + Some(0), + None, + NodeKind::Chart { + series: vec![vec![4, 9, 7], vec![2, 3, 5]], + max: None, + span: None, + }, + Tone::Caution, + ), + ], + refresh: None, + }; + vec![sample( + "reply-view-charts", + encode_reply(&reply(Answer::View(view), &[])), + )] +} diff --git a/crates/plugin-abi/src/samples/files.rs b/crates/plugin-abi/src/samples/files.rs new file mode 100644 index 00000000..0a5fdddf --- /dev/null +++ b/crates/plugin-abi/src/samples/files.rs @@ -0,0 +1,126 @@ +//! Samples of the files of a plugin's terminal: a request of every kind, the answers to them, +//! and a tab and a request with bytes. + +use alloc::{string::String, vec, vec::Vec}; + +use nocterm_plugin_api::{ + Effect, EffectRequest, Effects, Event, + capability::{ + files::{FileAnswer, FileEntry, FileFailure, FileOp, FileReply}, + net::{Method, Request}, + terminals::Open, + }, +}; + +use super::{Sample, empty_snapshot, local, reply, sample}; +use crate::{Answer, Call, encode_call, encode_reply}; + +pub(super) fn samples() -> Vec { + let mut samples = requests(); + samples.push(sample( + "reply-effects-files", + encode_reply(&reply(Answer::Effects(answers()), &[])), + )); + samples +} + +/// One `file-request` event of every kind. +fn requests() -> Vec { + const NAMES: [&str; 7] = [ + "call-event-file-list", + "call-event-file-stat", + "call-event-file-read", + "call-event-file-write", + "call-event-file-create-dir", + "call-event-file-remove", + "call-event-file-rename", + ]; + let path = || String::from("/plugins/a.yml"); + let ops = [ + FileOp::List { path: path() }, + FileOp::Stat { path: path() }, + FileOp::Read { path: path() }, + FileOp::Write { + path: path(), + data: String::from("TWFu"), + create: true, + }, + FileOp::CreateDir { path: path() }, + FileOp::Remove { + path: path(), + directory: false, + }, + FileOp::Rename { + from: path(), + to: String::from("/b.yml"), + }, + ]; + ops.into_iter() + .zip(NAMES) + .enumerate() + .map(|(index, (op, name))| { + let event = Event::FileRequest { + terminal: String::from("srv"), + request: u32::try_from(index).unwrap_or(0), + op, + }; + sample( + name, + encode_call(&Call::Event { + local: local("panel"), + event, + snapshot: empty_snapshot(), + }), + ) + }) + .collect() +} + +/// An answer of every kind, a request with bytes and a tab that serves files. +fn answers() -> Effects { + let entry = FileEntry { + name: String::from("a.yml"), + directory: false, + size: Some(12), + modified: Some(1_700_000_000), + }; + let answers = [ + FileAnswer::Listed(vec![entry.clone()]), + FileAnswer::Found(Some(entry)), + FileAnswer::Content(String::from("TWFu")), + FileAnswer::Done, + FileAnswer::Failed(FileFailure::NotFound), + FileAnswer::Failed(FileFailure::Other(String::from("busy"))), + ]; + let mut effects: Vec = answers + .into_iter() + .enumerate() + .map(|(index, answer)| EffectRequest { + key: None, + effect: Effect::TerminalsFiles(FileReply { + terminal: String::from("srv"), + request: u32::try_from(index).unwrap_or(0), + answer, + }), + }) + .collect(); + effects.push(EffectRequest { + key: Some(String::from("read")), + effect: Effect::NetRequest(Request { + method: Method::Post, + path: String::from("/write"), + body: Some(String::from("TWFu")), + bytes: true, + }), + }); + effects.push(EffectRequest { + key: None, + effect: Effect::TerminalsOpen(Open { + local: local("panel"), + terminal: String::from("srv"), + title: String::from("Build"), + files: true, + }), + }); + Effects(effects) +} diff --git a/crates/plugin-abi/src/tests.rs b/crates/plugin-abi/src/tests.rs index 0118d8ea..4fb3ab68 100644 --- a/crates/plugin-abi/src/tests.rs +++ b/crates/plugin-abi/src/tests.rs @@ -3,8 +3,8 @@ use alloc::{collections::BTreeMap, string::String, vec, vec::Vec}; use nocterm_plugin_api::{ - ContributionContext, EffectOutcome, HostError, Kind, LocalId, NodeKind, Observed, PluginIcon, - Snapshot, Zone, + ContributionContext, EffectOutcome, Event, HostError, Kind, LocalId, NodeKind, Observed, + PluginIcon, Snapshot, Zone, capability::{ clock::ClockNow, monitor::Usage, @@ -76,6 +76,7 @@ fn snapshot() -> Snapshot { key: Some("a".into()), result: Err(HostError::Denied), }], + terminal: None, } } @@ -125,7 +126,7 @@ fn the_samples_pin_every_icon_workspace_kind_and_zone() { let all = samples(); assert_eq!( all.len(), - 13 + Zone::ALL.len(), + 13 + Zone::ALL.len() + 21, "append a sample, never drop one" ); let bytes = |name: &str| { @@ -146,7 +147,7 @@ fn the_samples_pin_every_icon_workspace_kind_and_zone() { _ => None, }) .collect(); - assert_eq!(icons, PluginIcon::ALL); + assert_eq!(icons, PluginIcon::ALL.map(nocterm_plugin_api::Glyph::from)); let Call::Render { snapshot, .. } = decode_call(bytes("call-render-workspace-kinds")).unwrap() else { @@ -165,9 +166,13 @@ fn the_samples_pin_every_icon_workspace_kind_and_zone() { let zones: Vec<_> = all .iter() .filter(|sample| sample.name.starts_with("call-render-zone-")) - .map(|sample| match decode_call(&sample.bytes).unwrap() { - Call::Render { snapshot, .. } => snapshot.context.zone, - Call::Event { snapshot, .. } => snapshot.context.zone, + .map(|sample| { + decode_call(&sample.bytes) + .unwrap() + .into_parts() + .2 + .context + .zone }) .collect(); assert_eq!(zones, Zone::ALL); @@ -302,3 +307,26 @@ fn a_pointer_and_a_length_pack_into_one_value_and_back() { } assert_eq!(ABI_VERSION, 1); } + +#[test] +fn a_bar_call_names_its_terminal_and_any_other_call_none() { + let local = LocalId::parse("panel").unwrap(); + let mut snapshot = Snapshot::new(ContributionContext { + kind: Kind::Toolbar, + zone: Zone::Center, + }); + let plain = Call::render(local.clone(), &snapshot); + assert!(matches!(plain, Call::Render { .. })); + assert_eq!(plain.into_parts().2.terminal, None); + snapshot.terminal = Some("srv".into()); + let drawn = + decode_call(&encode_call(&Call::render(local.clone(), &snapshot)).unwrap()).unwrap(); + assert!(matches!(drawn, Call::RenderTerminalBar { .. })); + let (_, event, back) = drawn.into_parts(); + assert_eq!((event, back.terminal.as_deref()), (None, Some("srv"))); + let used = Event::Activated { + key: "restart".into(), + }; + let (_, event, back) = Call::event(local, used.clone(), &snapshot).into_parts(); + assert_eq!((event, back), (Some(used), snapshot)); +} diff --git a/crates/plugin-abi/src/wire.rs b/crates/plugin-abi/src/wire.rs index a7bfa75a..e01746e4 100644 --- a/crates/plugin-abi/src/wire.rs +++ b/crates/plugin-abi/src/wire.rs @@ -30,6 +30,85 @@ pub enum Call { event: Event, snapshot: WireSnapshot, }, + /// Draw the bar of the plugin's terminal `terminal` (`terminals.bar`), by the contribution + /// the terminal reports to. + RenderTerminalBar { + local: LocalId, + terminal: String, + snapshot: WireSnapshot, + }, + /// Answer an event of the bar of the plugin's terminal `terminal`. + TerminalBarEvent { + local: LocalId, + terminal: String, + event: Event, + snapshot: WireSnapshot, + }, +} + +impl Call { + /// The call that asks `local` for its view: the bar of a terminal when the snapshot names + /// one. + pub fn render(local: LocalId, snapshot: &Snapshot) -> Self { + let wire = WireSnapshot::from(snapshot); + match &snapshot.terminal { + Some(terminal) => Self::RenderTerminalBar { + local, + terminal: terminal.clone(), + snapshot: wire, + }, + None => Self::Render { + local, + snapshot: wire, + }, + } + } + + /// The call that reports `event` to `local`: from the bar of a terminal when the snapshot + /// names one. + pub fn event(local: LocalId, event: Event, snapshot: &Snapshot) -> Self { + let wire = WireSnapshot::from(snapshot); + match &snapshot.terminal { + Some(terminal) => Self::TerminalBarEvent { + local, + terminal: terminal.clone(), + event, + snapshot: wire, + }, + None => Self::Event { + local, + event, + snapshot: wire, + }, + } + } + + /// The contribution, the event (none for a view) and the snapshot as the guest reads them, + /// with the terminal of a bar's call. + pub fn into_parts(self) -> (LocalId, Option, Snapshot) { + let (local, event, snapshot, terminal) = match self { + Self::Render { local, snapshot } => (local, None, snapshot, None), + Self::Event { + local, + event, + snapshot, + } => (local, Some(event), snapshot, None), + Self::RenderTerminalBar { + local, + terminal, + snapshot, + } => (local, None, snapshot, Some(terminal)), + Self::TerminalBarEvent { + local, + terminal, + event, + snapshot, + } => (local, Some(event), snapshot, Some(terminal)), + }; + let mut snapshot = Snapshot::from(snapshot); + snapshot.terminal = terminal; + (local, event, snapshot) + } } /// What the guest answers. @@ -173,6 +252,7 @@ impl From for Snapshot { storage: wire.storage.into_iter().collect(), context: wire.context, outcomes: wire.outcomes, + terminal: None, } } } diff --git a/crates/plugin-api/api-surface.json b/crates/plugin-api/api-surface.json index 4c2fe622..57e2c572 100644 --- a/crates/plugin-api/api-surface.json +++ b/crates/plugin-api/api-surface.json @@ -165,9 +165,139 @@ "session_derived": false, "since": "0.1", "target": "own-data" + }, + { + "consent": "Send requests to the web address you enter in its settings, with the key you give it.", + "deprecated": null, + "doc": "Sends a request to the web address the user entered in the setting the `[network]` table names, with the credential the user entered, and answers with a `response` event under the request's key.", + "excluded": "Other addresses, cookies, redirects and reading the key.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "net.request", + "live_cap": 4, + "rate": { + "per_ms": 1000, + "uses": 8 + }, + "session_derived": false, + "since": "0.1", + "target": "user-origin" + }, + { + "consent": "Wake itself up after a delay to refresh what it shows.", + "deprecated": null, + "doc": "Wakes the plugin with a `timer` event under the effect's key after one second to one hour; setting a key that waits replaces it.", + "excluded": "Delays shorter than a second.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "timers.set", + "live_cap": 8, + "rate": { + "per_ms": 1000, + "uses": 8 + }, + "session_derived": false, + "since": "0.1", + "target": "own-timer" + }, + { + "consent": "Open terminal tabs that show what it writes and pass on what you type.", + "deprecated": null, + "doc": "Opens a terminal tab, or shows it, after a click. Lines typed into it, by the user or an AI chat it is attached to, arrive as `terminal-line` events.", + "excluded": "Your other terminals and running anything on this computer.", + "family": "effect", + "fields": null, + "gesture": true, + "id": "terminals.open", + "live_cap": 4, + "rate": null, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" + }, + { + "consent": "Write text to the terminal tabs it opened.", + "deprecated": null, + "doc": "Writes text with colours to one of the plugin's open terminal tabs.", + "excluded": "Other terminals, and control sequences other than colours.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "terminals.write", + "live_cap": null, + "rate": { + "per_ms": 1000, + "uses": 50 + }, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" + }, + { + "consent": "Show the files of the servers it opens as terminal tabs in your file browser, and let AI chats you attach those tabs to read and change them.", + "deprecated": null, + "doc": "Serves the files of a terminal tab the plugin opened with `files`: the file browser shows them while the tab is active and an AI chat it is attached to reads and changes them with its file tools. The host asks with `file-request` events and the plugin answers with this effect under the request's number.", + "excluded": "Files on this computer, and the files of your other terminals.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "terminals.files", + "live_cap": null, + "rate": { + "per_ms": 1000, + "uses": 50 + }, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" + }, + { + "consent": "Take its toolbar off one of the terminal tabs it opened, or put it back.", + "deprecated": null, + "doc": "Takes the plugin's toolbar for its own tabs off one terminal tab it opened, or puts it back. The window's layout places the toolbar (the manifest suggests, the user moves or hides it), so the edge named is not read.", + "excluded": "Other tabs, and anything outside the tabs it opened.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "terminals.bar", + "live_cap": null, + "rate": { + "per_ms": 1000, + "uses": 10 + }, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" } ], + "edges": [ + "top", + "bottom", + "left", + "right" + ], "effect_requests": { + "net.request": { + "body": { + "required": false, + "type": "text" + }, + "bytes": { + "required": false, + "type": "bool" + }, + "method": { + "required": true, + "type": "choice", + "vocabulary": "http_methods" + }, + "path": { + "required": true, + "type": "text" + } + }, "notifications.post": { "notice_key": { "required": false, @@ -220,13 +350,81 @@ "required": true, "type": "text" } + }, + "terminals.bar": { + "edge": { + "required": false, + "type": "choice", + "vocabulary": "edges" + }, + "terminal": { + "required": true, + "type": "text" + } + }, + "terminals.files": { + "answer": { + "required": true, + "type": "any" + }, + "request": { + "max": 4294967295, + "min": 0, + "required": true, + "type": "integer" + }, + "terminal": { + "required": true, + "type": "text" + } + }, + "terminals.open": { + "files": { + "required": false, + "type": "bool" + }, + "local": { + "required": true, + "type": "text" + }, + "terminal": { + "required": true, + "type": "text" + }, + "title": { + "required": true, + "type": "text" + } + }, + "terminals.write": { + "terminal": { + "required": true, + "type": "text" + }, + "text": { + "required": true, + "type": "text" + } + }, + "timers.set": { + "after_ms": { + "max": 4294967295, + "min": 0, + "required": true, + "type": "integer" + } } }, "events": [ "activated", "toggled", "visibility-changed", - "settings-changed" + "settings-changed", + "response", + "timer", + "terminal-line", + "terminal-closed", + "file-request" ], "filters": [ "round(n)", @@ -234,12 +432,85 @@ "hh-mm", "threshold(low, high)" ], + "glyphs": [ + "box", + "gem", + "hexagon", + "plug", + "puzzle", + "shapes", + "activity", + "arrow-down", + "arrow-left", + "arrow-up", + "bell", + "cable", + "calendar", + "check", + "chevron-down", + "chevron-left", + "chevron-right", + "circle-alert", + "circle-check", + "circle-x", + "clock", + "container", + "copy", + "cpu", + "ellipsis", + "external-link", + "eye", + "file", + "file-text", + "folder", + "folder-open", + "gauge", + "globe", + "hard-drive", + "inbox", + "info", + "key", + "loader", + "lock", + "memory", + "network", + "pause", + "pencil", + "play", + "plus", + "minus", + "refresh", + "rotate", + "search", + "send", + "server", + "server-off", + "settings", + "square", + "stop", + "star", + "terminal", + "thermometer", + "trash", + "triangle-alert", + "upload", + "user", + "wrench", + "x", + "zap" + ], "host_errors": [ "not-granted", "denied", "rate-limited", "unavailable" ], + "http_methods": [ + "get", + "post", + "put", + "delete" + ], "icons": [ "box", "gem", @@ -250,19 +521,31 @@ ], "kinds": [ "widget", - "panel" + "panel", + "toolbar" ], "limits": { "badge-chars": 8, "button-chars": 48, "capabilities-per-package": 16, + "chart-points": 120, + "chart-series": 2, + "code-chars": 16384, "contributions-per-package": 8, "description-chars": 160, "effect-key-chars": 32, "effects-per-call": 8, + "file-bytes": 163840, + "file-entries": 1000, + "file-name-bytes": 255, + "file-path-bytes": 1024, + "file-reason-chars": 200, + "file-requests-waiting": 8, + "file-timeout-ms": 30000, "list-items": 100, "live-notices": 3, "min-refresh-ms": 250, + "min-timer-ms": 1000, "name-chars": 48, "node-key-chars": 32, "nodes-per-view": 256, @@ -270,6 +553,12 @@ "package-bytes": 262144, "placeholders-per-view": 64, "reason-chars": 160, + "request-body-bytes": 229376, + "request-path-bytes": 2048, + "request-timeout-ms": 15000, + "requests-in-flight": 4, + "response-body-bytes": 262144, + "response-bytes-mode-bytes": 163840, "round-decimals": 2, "setting-fields": 32, "setting-groups": 8, @@ -282,11 +571,23 @@ "storage-total-bytes": 65536, "storage-value-bytes": 4096, "template-chars": 512, + "terminal-key-chars": 32, + "terminal-line-chars": 1024, + "terminal-title-chars": 48, + "terminal-write-bytes": 65536, + "terminals-open": 4, "text-chars": 200, + "timer-max-ms": 3600000, + "timers-waiting": 8, "view-depth": 8 }, "manifest": { "contribution": { + "edge": { + "required": false, + "type": "choice", + "vocabulary": "edges" + }, "icon": { "required": false, "type": "choice", @@ -307,6 +608,15 @@ "required": false, "type": "integer" }, + "shown": { + "required": false, + "type": "bool" + }, + "tabs": { + "required": false, + "type": "choice", + "vocabulary": "tabs" + }, "title": { "required": true, "type": "text" @@ -332,14 +642,22 @@ } }, "contribution.view.nodes": { + "detail": { + "required": false, + "type": "text" + }, "each": { "required": false, "type": "text" }, + "folded": { + "required": false, + "type": "bool" + }, "icon": { "required": false, "type": "choice", - "vocabulary": "icons" + "vocabulary": "glyphs" }, "key": { "required": false, @@ -399,6 +717,10 @@ "required": true, "type": "text" }, + "network": { + "required": false, + "type": "table" + }, "optional_capabilities": { "required": false, "type": "text-list" @@ -421,6 +743,24 @@ "type": "table" } }, + "network": { + "credential_header": { + "required": false, + "type": "text" + }, + "credential_prefix": { + "required": false, + "type": "text" + }, + "credential_setting": { + "required": false, + "type": "text" + }, + "origin_setting": { + "required": true, + "type": "text" + } + }, "settings": { "group": { "required": false, @@ -546,7 +886,14 @@ "toggle", "list", "divider", - "spacer" + "spacer", + "code", + "section", + "item", + "icon-button", + "caption", + "field", + "chart" ], "notice_tones": [ "info", @@ -566,7 +913,12 @@ "choice", "multi-choice", "number", - "text" + "text", + "secret" + ], + "tabs": [ + "own", + "all" ], "tones": [ "default", diff --git a/crates/plugin-api/src/base64.rs b/crates/plugin-api/src/base64.rs new file mode 100644 index 00000000..fd3a0bb5 --- /dev/null +++ b/crates/plugin-api/src/base64.rs @@ -0,0 +1,77 @@ +//! Standard base64 (RFC 4648, with padding), for the bytes of files and request bodies that a +//! plugin passes through as text. + +use alloc::{string::String, vec::Vec}; + +const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/// The base64 text of `bytes`. +pub fn encode(bytes: &[u8]) -> String { + let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4); + for chunk in bytes.chunks(3) { + let b = [ + chunk[0], + chunk.get(1).copied().unwrap_or(0), + chunk.get(2).copied().unwrap_or(0), + ]; + let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); + for (i, shift) in [18, 12, 6, 0].into_iter().enumerate() { + if i <= chunk.len() { + out.push(char::from(ALPHABET[((n >> shift) & 63) as usize])); + } else { + out.push('='); + } + } + } + out +} + +fn value(byte: u8) -> Option { + match byte { + b'A'..=b'Z' => Some(u32::from(byte - b'A')), + b'a'..=b'z' => Some(u32::from(byte - b'a') + 26), + b'0'..=b'9' => Some(u32::from(byte - b'0') + 52), + b'+' => Some(62), + b'/' => Some(63), + _ => None, + } +} + +/// The bytes of base64 `text`, or `None` when it is not valid padded base64. +pub fn decode(text: &str) -> Option> { + let bytes = text.as_bytes(); + if !bytes.len().is_multiple_of(4) { + return None; + } + let mut out = Vec::with_capacity(bytes.len() / 4 * 3); + let quads = bytes.len() / 4; + for (index, quad) in bytes.chunks(4).enumerate() { + let last = index + 1 == quads; + let padding = quad.iter().rev().take_while(|&&byte| byte == b'=').count(); + if padding > 2 || (padding > 0 && !last) { + return None; + } + let mut n = 0u32; + for &byte in &quad[..4 - padding] { + n = (n << 6) | value(byte)?; + } + n <<= 6 * padding as u32; + let decoded = [(n >> 16) as u8, (n >> 8) as u8, n as u8]; + out.extend_from_slice(&decoded[..3 - padding]); + } + Some(out) +} + +/// How many bytes `text` decodes to, without decoding it; `None` when its length or padding +/// cannot be base64. Characters are checked by [`decode`]. +pub fn decoded_len(text: &str) -> Option { + let bytes = text.as_bytes(); + if !bytes.len().is_multiple_of(4) { + return None; + } + let padding = bytes.iter().rev().take_while(|&&byte| byte == b'=').count(); + if padding > 2 { + return None; + } + Some(bytes.len() / 4 * 3 - padding) +} diff --git a/crates/plugin-api/src/capability.rs b/crates/plugin-api/src/capability.rs index c8f80a5f..faf87a57 100644 --- a/crates/plugin-api/src/capability.rs +++ b/crates/plugin-api/src/capability.rs @@ -6,11 +6,15 @@ pub mod clock; mod fields; +pub mod files; pub mod monitor; +pub mod net; pub mod notifications; pub mod panels; pub mod storage; mod table; +pub mod terminals; +pub mod timers; pub mod transfers; pub mod workspace; diff --git a/crates/plugin-api/src/capability/files.rs b/crates/plugin-api/src/capability/files.rs new file mode 100644 index 00000000..f78697ad --- /dev/null +++ b/crates/plugin-api/src/capability/files.rs @@ -0,0 +1,227 @@ +//! `terminals.files`: the files of a plugin's terminal tab. +//! +//! A plugin that opens a terminal tab with `files` set serves that tab's files: the +//! application's file browser shows them while the tab is active, and an AI chat the tab is +//! attached to reads and changes them with its usual file tools. The host asks the plugin with +//! a `file-request` event and the plugin answers with this capability's effect, under the +//! request's number. Paths are absolute POSIX paths under `/`; nothing the plugin answers is +//! ever a file of this computer. File contents travel as base64, so binary files keep their +//! bytes, and are at most [`MAX_FILE_BYTES`] long. + +use alloc::{string::String, vec::Vec}; +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed, terminals::MAX_TERMINAL_KEY}; +use crate::{LimitError, error::key_syntax}; + +/// Most bytes of one file read or written through a plugin. +pub const MAX_FILE_BYTES: usize = 160 * 1024; +/// Most entries of one directory listing. +pub const MAX_FILE_ENTRIES: usize = 1000; +/// Longest path the host asks about, in bytes. +pub const MAX_FILE_PATH: usize = 1024; +/// Longest entry name, in bytes. +pub const MAX_FILE_NAME: usize = 255; +/// Longest failure reason, in characters. +pub const MAX_FILE_REASON: usize = 200; +/// Most file requests of one tab waiting for the plugin's answer. +pub const MAX_FILE_REQUESTS: usize = 8; +/// How long the host waits for an answer before the request fails. +pub const FILE_TIMEOUT_MS: u32 = 30_000; + +/// What the host asks of a plugin's file system. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub enum FileOp { + /// The entries of a directory, answered with [`FileAnswer::Listed`]. + List { path: String }, + /// One entry, answered with [`FileAnswer::Found`]. + Stat { path: String }, + /// A file's contents, answered with [`FileAnswer::Content`]. + Read { path: String }, + /// Writes a file. With `create` it must not exist yet. `data` is base64. + Write { + path: String, + data: String, + create: bool, + }, + /// Creates a directory, answered with [`FileAnswer::Done`]. + CreateDir { path: String }, + /// Removes a file, or an empty directory when `directory` is set. + Remove { path: String, directory: bool }, + /// Renames an entry; the destination must not exist. + Rename { from: String, to: String }, +} + +impl FileOp { + /// The paths the request names. + pub fn paths(&self) -> [Option<&str>; 2] { + match self { + Self::List { path } + | Self::Stat { path } + | Self::Read { path } + | Self::Write { path, .. } + | Self::CreateDir { path } + | Self::Remove { path, .. } => [Some(path), None], + Self::Rename { from, to } => [Some(from), Some(to)], + } + } + + /// Paths are absolute, without `.` or `..` steps, NUL or doubled slashes, and bounded; + /// written data is base64 of at most [`MAX_FILE_BYTES`]. + pub fn check(&self) -> Result<(), LimitError> { + for path in self.paths().into_iter().flatten() { + check_path(path)?; + } + if let Self::Write { data, .. } = self + && crate::base64::decoded_len(data).is_none_or(|len| len > MAX_FILE_BYTES) + { + return Err(LimitError::TooLong { + field: "data", + max: MAX_FILE_BYTES, + }); + } + Ok(()) + } +} + +/// An absolute POSIX path the host may ask about. +pub fn check_path(path: &str) -> Result<(), LimitError> { + let unusual = || LimitError::Syntax { field: "path" }; + if path.len() > MAX_FILE_PATH { + return Err(LimitError::TooLong { + field: "path", + max: MAX_FILE_PATH, + }); + } + if !path.starts_with('/') || path.contains('\0') { + return Err(unusual()); + } + if path == "/" { + return Ok(()); + } + let steps = path.trim_end_matches('/').split('/').skip(1); + for step in steps { + if step.is_empty() || step == "." || step == ".." { + return Err(unusual()); + } + } + Ok(()) +} + +/// One entry of a plugin's file system. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct FileEntry { + pub name: String, + pub directory: bool, + /// Size in bytes, for a file. + pub size: Option, + /// Last change, in seconds since the Unix epoch. + pub modified: Option, +} + +impl FileEntry { + /// A name of one path step: not empty, not `.` or `..`, without `/`, NUL, control, + /// invisible or direction characters, at most [`MAX_FILE_NAME`] bytes. + pub fn check(&self) -> Result<(), LimitError> { + let name = &self.name; + if name.len() > MAX_FILE_NAME { + return Err(LimitError::TooLong { + field: "name", + max: MAX_FILE_NAME, + }); + } + if name.is_empty() + || name == "." + || name == ".." + || name.chars().any(|c| c == '/' || c.is_control()) + { + return Err(LimitError::Syntax { field: "name" }); + } + // Invisible and direction characters could disguise a name, as they could view text. + if crate::sanitize_display(name, MAX_FILE_NAME) != *name { + return Err(LimitError::Unsanitized { field: "name" }); + } + Ok(()) + } +} + +/// Why a file request failed. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub enum FileFailure { + NotFound, + PermissionDenied, + AlreadyExists, + /// The plugin's server does not offer this operation. + Unsupported, + /// Anything else, in at most [`MAX_FILE_REASON`] characters. + Other(String), +} + +/// A plugin's answer to one file request. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub enum FileAnswer { + Listed(Vec), + Found(Option), + /// A file's contents as base64. + Content(String), + Done, + Failed(FileFailure), +} + +/// The effect of `terminals.files`: the answer to the file request `request` of `terminal`. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct FileReply { + pub terminal: String, + pub request: u32, + pub answer: FileAnswer, +} + +impl FileReply { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal")?; + match &self.answer { + FileAnswer::Listed(entries) => { + if entries.len() > MAX_FILE_ENTRIES { + return Err(LimitError::TooLong { + field: "entries", + max: MAX_FILE_ENTRIES, + }); + } + entries.iter().try_for_each(FileEntry::check) + } + FileAnswer::Found(entry) => entry.as_ref().map_or(Ok(()), FileEntry::check), + FileAnswer::Content(data) => match crate::base64::decoded_len(data) { + Some(len) if len <= MAX_FILE_BYTES => Ok(()), + Some(_) => Err(LimitError::TooLong { + field: "content", + max: MAX_FILE_BYTES, + }), + None => Err(LimitError::Syntax { field: "content" }), + }, + FileAnswer::Failed(FileFailure::Other(reason)) + if reason.chars().count() > MAX_FILE_REASON => + { + Err(LimitError::TooLong { + field: "reason", + max: MAX_FILE_REASON, + }) + } + FileAnswer::Done | FileAnswer::Failed(_) => Ok(()), + } + } +} + +/// Marker of `terminals.files`. +pub struct ServeFiles; + +impl sealed::Sealed for ServeFiles {} + +impl Capability for ServeFiles { + const ID: &'static str = "terminals.files"; + type Value = FileReply; +} diff --git a/crates/plugin-api/src/capability/net.rs b/crates/plugin-api/src/capability/net.rs new file mode 100644 index 00000000..aee525c0 --- /dev/null +++ b/crates/plugin-api/src/capability/net.rs @@ -0,0 +1,265 @@ +//! `net.request`: a request to the web address the user entered in the plugin's settings. +//! +//! The plugin names a method, a path and a body; the host joins the path to the origin the user +//! typed into the setting the manifest's `[network]` table names, adds the credential the user +//! entered (which the plugin never reads), sends it without cookies and without following +//! redirects, and answers with a `response` event under the request's key. + +use alloc::string::String; +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::LimitError; + +/// Longest request path, query included, in bytes. +pub const MAX_PATH: usize = 2048; +/// Longest request body, in bytes. +pub const MAX_BODY: usize = 224 * 1024; +/// Longest response body a plugin receives, in bytes; a longer one is cut there. +pub const MAX_RESPONSE: usize = 256 * 1024; +/// Most bytes of an answer to a `bytes` request, which comes back as base64 (a third longer) +/// and must still fit in the call that hands it to the plugin; a longer answer is refused, not +/// cut. +pub const MAX_BYTES_RESPONSE: usize = 160 * 1024; +/// Most requests of one plugin in flight at once. +pub const MAX_IN_FLIGHT: usize = 4; +/// How long the host waits for an answer, in milliseconds. +pub const TIMEOUT_MS: u32 = 15_000; + +/// The method of a request. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Method { + Get, + Post, + Put, + Delete, +} + +impl Method { + /// Every method, in declaration order. + pub const ALL: [Self; 4] = [Self::Get, Self::Post, Self::Put, Self::Delete]; + + pub const fn name(self) -> &'static str { + match self { + Self::Get => "get", + Self::Post => "post", + Self::Put => "put", + Self::Delete => "delete", + } + } +} + +/// A request to the plugin's origin. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Request { + pub method: Method, + /// The path and query below the origin: starts with one `/`, holds visible ASCII only. + pub path: String, + /// A JSON body, sent as `application/json`; with `bytes`, base64 of the raw bytes, sent as + /// `application/octet-stream`. + #[serde(default)] + pub body: Option, + /// The body is base64 of bytes, and the answer's body comes back as base64 of the bytes + /// the server sent, so a binary file keeps every byte. + #[serde(default)] + pub bytes: bool, +} + +impl Request { + pub fn check(&self) -> Result<(), LimitError> { + check_path(&self.path)?; + if self.body.as_ref().is_some_and(|body| body.len() > MAX_BODY) { + return Err(LimitError::TooLong { + field: "body", + max: MAX_BODY, + }); + } + if self.bytes + && self + .body + .as_deref() + .is_some_and(|body| crate::base64::decode(body).is_none()) + { + return Err(LimitError::Syntax { field: "body" }); + } + Ok(()) + } +} + +/// A path the host can join to an origin without leaving it: one leading `/` (never `//`, which +/// would name another host), visible ASCII without `\`, `#` or spaces, and no `..` segment. +pub fn check_path(path: &str) -> Result<(), LimitError> { + if path.len() > MAX_PATH { + return Err(LimitError::TooLong { + field: "path", + max: MAX_PATH, + }); + } + let syntax = LimitError::Syntax { field: "path" }; + let visible = path + .bytes() + .all(|byte| byte.is_ascii_graphic() && !matches!(byte, b'\\' | b'#')); + if !path.starts_with('/') || path.starts_with("//") || !visible { + return Err(syntax); + } + let route = path.split('?').next().unwrap_or_default(); + if route + .split('/') + .any(|segment| segment == ".." || segment == ".") + { + return Err(syntax); + } + Ok(()) +} + +/// Marker of `net.request`. +pub struct NetRequest; + +impl sealed::Sealed for NetRequest {} + +impl Capability for NetRequest { + const ID: &'static str = "net.request"; + type Value = Request; +} + +/// The web address a user typed into the setting that names a plugin's origin: `http` or +/// `https`, a host with an optional port, and an optional base path every request goes below. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Origin { + secure: bool, + authority: String, + base: String, +} + +/// A typed address the host does not send requests to. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum OriginError { + #[error("no address is set")] + Empty, + #[error("the address does not start with http:// or https://")] + Scheme, + #[error("the address has no host, or one with a user name, a space or a bad port")] + Host, + #[error("the address has a query, a fragment or an unusual path")] + Path, +} + +impl Origin { + pub fn parse(text: &str) -> Result { + let text = text.trim(); + if text.is_empty() { + return Err(OriginError::Empty); + } + let lower = text.to_ascii_lowercase(); + let (secure, rest) = if lower.starts_with("https://") { + (true, &text[8..]) + } else if lower.starts_with("http://") { + (false, &text[7..]) + } else { + return Err(OriginError::Scheme); + }; + let split = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + let (authority, path) = rest.split_at(split); + if !authority_allowed(authority) { + return Err(OriginError::Host); + } + let base = path.trim_end_matches('/'); + if !base.is_empty() && check_path(base).is_err() { + return Err(OriginError::Path); + } + Ok(Self { + secure, + authority: authority.to_ascii_lowercase(), + base: String::from(base), + }) + } + + pub fn is_secure(&self) -> bool { + self.secure + } + + /// `host` or `host:port`, lower case. + pub fn authority(&self) -> &str { + &self.authority + } + + /// The address of `path` below the origin. + pub fn url(&self, path: &str) -> Result { + check_path(path)?; + let scheme = if self.secure { "https" } else { "http" }; + Ok(alloc::format!( + "{scheme}://{}{}{path}", + self.authority, + self.base + )) + } +} + +/// A host name, an IPv4 address or a bracketed IPv6 address, with an optional port. +fn authority_allowed(authority: &str) -> bool { + let (host, port) = match authority.rsplit_once(':') { + // An IPv6 address outside brackets: refused below by the host characters. + Some((host, _)) if !host.ends_with(']') && host.contains(':') => (authority, None), + Some((host, port)) => (host, Some(port)), + None => (authority, None), + }; + let port_fine = port.is_none_or(|port| { + !port.is_empty() + && port.len() <= 5 + && port.bytes().all(|byte| byte.is_ascii_digit()) + && port + .parse::() + .is_ok_and(|port| (1..=65535).contains(&port)) + }); + let bracketed = host.starts_with('[') && host.ends_with(']'); + let host_fine = if bracketed { + host.len() > 2 + && host[1..host.len() - 1] + .bytes() + .all(|byte| byte.is_ascii_hexdigit() || byte == b':' || byte == b'.') + } else { + !host.is_empty() + && host.len() <= 253 + && !host.starts_with(['.', '-']) + && host + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'.' || byte == b'-') + }; + port_fine && host_fine +} + +/// One request, ready to be sent: the whole address and the credential header. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NetCall { + pub method: Method, + /// The address below `origin`, built by [`Origin::url`]. + pub url: String, + /// The origin the address must stay at; the adapter checks it again after parsing. + pub origin: Origin, + /// The body's bytes: JSON text, or raw bytes when `bytes` is set. + pub body: Option>, + /// The body is raw bytes, and the answer comes back as base64. + pub bytes: bool, + /// The credential header and its value. Never logged. + pub credential: Option<(String, String)>, +} + +/// What came back: the status and the body (cut to [`MAX_RESPONSE`], lossily decoded), or +/// status 0 and the host's words for why nothing came. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NetAnswer { + pub status: u16, + pub body: String, +} + +impl NetAnswer { + /// No answer came, for `reason`. + pub fn failed(reason: impl Into) -> Self { + Self { + status: 0, + body: reason.into(), + } + } +} diff --git a/crates/plugin-api/src/capability/table.rs b/crates/plugin-api/src/capability/table.rs index 3ba69a2a..51cf7253 100644 --- a/crates/plugin-api/src/capability/table.rs +++ b/crates/plugin-api/src/capability/table.rs @@ -23,6 +23,12 @@ pub enum EffectTarget { OwnData, /// How the host presents something the plugin said, such as a notice. HostPresentation, + /// The web address the user entered in the plugin's settings. + UserOrigin, + /// Terminal tabs the plugin opened. + OwnTerminal, + /// The plugin's own timers. + OwnTimer, } /// A capability that still works but has a successor. @@ -204,6 +210,90 @@ pub const CAPABILITIES: &[CapabilityEntry] = &[ since: V1, deprecated: None, }, + CapabilityEntry { + id: "net.request", + family: Family::Effect, + doc: "Sends a request to the web address the user entered in the setting the `[network]` table names, with the credential the user entered, and answers with a `response` event under the request's key.", + consent: "Send requests to the web address you enter in its settings, with the key you give it.", + excluded: "Other addresses, cookies, redirects and reading the key.", + session_derived: false, + target: Some(EffectTarget::UserOrigin), + gesture: false, + rate: Some((8, Duration::from_secs(1))), + live_cap: Some(4), + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "timers.set", + family: Family::Effect, + doc: "Wakes the plugin with a `timer` event under the effect's key after one second to one hour; setting a key that waits replaces it.", + consent: "Wake itself up after a delay to refresh what it shows.", + excluded: "Delays shorter than a second.", + session_derived: false, + target: Some(EffectTarget::OwnTimer), + gesture: false, + rate: Some((8, Duration::from_secs(1))), + live_cap: Some(8), + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.open", + family: Family::Effect, + doc: "Opens a terminal tab, or shows it, after a click. Lines typed into it, by the user or an AI chat it is attached to, arrive as `terminal-line` events.", + consent: "Open terminal tabs that show what it writes and pass on what you type.", + excluded: "Your other terminals and running anything on this computer.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: true, + rate: None, + live_cap: Some(4), + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.write", + family: Family::Effect, + doc: "Writes text with colours to one of the plugin's open terminal tabs.", + consent: "Write text to the terminal tabs it opened.", + excluded: "Other terminals, and control sequences other than colours.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: false, + rate: Some((50, Duration::from_secs(1))), + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.files", + family: Family::Effect, + doc: "Serves the files of a terminal tab the plugin opened with `files`: the file browser shows them while the tab is active and an AI chat it is attached to reads and changes them with its file tools. The host asks with `file-request` events and the plugin answers with this effect under the request's number.", + consent: "Show the files of the servers it opens as terminal tabs in your file browser, and let AI chats you attach those tabs to read and change them.", + excluded: "Files on this computer, and the files of your other terminals.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: false, + rate: Some((50, Duration::from_secs(1))), + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.bar", + family: Family::Effect, + doc: "Takes the plugin's toolbar for its own tabs off one terminal tab it opened, or puts it back. The window's layout places the toolbar (the manifest suggests, the user moves or hides it), so the edge named is not read.", + consent: "Take its toolbar off one of the terminal tabs it opened, or put it back.", + excluded: "Other tabs, and anything outside the tabs it opened.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: false, + rate: Some((10, Duration::from_secs(1))), + live_cap: None, + since: V1, + deprecated: None, + }, ]; /// The entry with `id`. diff --git a/crates/plugin-api/src/capability/terminals.rs b/crates/plugin-api/src/capability/terminals.rs new file mode 100644 index 00000000..1541a951 --- /dev/null +++ b/crates/plugin-api/src/capability/terminals.rs @@ -0,0 +1,205 @@ +//! `terminals.open`, `terminals.write` and `terminals.bar`: terminal tabs a plugin drives. +//! +//! A plugin terminal is a tab like any other terminal of the window: the user reads and selects +//! its text, and attaches it to an AI chat, whose agent reads it and types into it. The host +//! echoes what is typed and hands each finished line to the plugin as a `terminal-line` event; +//! the plugin writes text back. Nothing runs on this computer. A tab may carry a bar of the +//! plugin along one of its edges: the contribution the tab reports to draws it, told which +//! terminal it draws for. + +use alloc::string::String; +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::{Edge, LimitError, LocalId, error::key_syntax, sanitize_display}; +use nocterm_plugin_model::sanitize_text; + +/// Longest terminal key. +pub const MAX_TERMINAL_KEY: usize = 32; +/// Longest terminal title, in characters. +pub const MAX_TERMINAL_TITLE: usize = 48; +/// Most bytes one write carries. +pub const MAX_WRITE: usize = 64 * 1024; +/// Most terminals one plugin keeps open. +pub const MAX_TERMINALS: usize = 4; +/// Longest line the host hands the plugin, in characters; typing stops there. +pub const MAX_LINE: usize = 1024; + +/// Opens a terminal tab, or shows it if it is open. Lines typed into it are reported to `local`, +/// one of the plugin's own contributions. With `files` the plugin serves the tab's files, which +/// needs `terminals.files` (see [`super::files`]). +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Open { + pub local: LocalId, + pub terminal: String, + pub title: String, + #[serde(default)] + pub files: bool, +} + +impl Open { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal")?; + if self.title.chars().count() > MAX_TERMINAL_TITLE { + return Err(LimitError::TooLong { + field: "title", + max: MAX_TERMINAL_TITLE, + }); + } + if sanitize_display(&self.title, MAX_TERMINAL_TITLE) != self.title { + return Err(LimitError::Unsanitized { field: "title" }); + } + Ok(()) + } +} + +/// Writes text to an open terminal of the plugin. The host keeps printable text, line breaks, +/// tabs and colours, and drops every other control sequence. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Write { + pub terminal: String, + pub text: String, +} + +impl Write { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal")?; + if self.text.len() > MAX_WRITE { + return Err(LimitError::TooLong { + field: "text", + max: MAX_WRITE, + }); + } + Ok(()) + } +} + +/// Takes the plugin's toolbar for its own tabs off one of its open terminal tabs (`edge: None`) +/// or puts it back (any edge). Where the toolbar runs is the window layout's: the manifest's +/// `edge` and `shown` suggest it and the user moves or hides it, so the edge named here is not +/// read. The toolbar draws the bar: the host asks it for a view with the snapshot's `terminal` +/// set, and reports what the user does in the bar to it the same way. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Bar { + pub terminal: String, + #[serde(default)] + pub edge: Option, +} + +impl Bar { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal") + } +} + +/// Marker of `terminals.open`. +pub struct OpenTerminal; + +impl sealed::Sealed for OpenTerminal {} + +impl Capability for OpenTerminal { + const ID: &'static str = "terminals.open"; + type Value = Open; +} + +/// Marker of `terminals.write`. +pub struct WriteTerminal; + +impl sealed::Sealed for WriteTerminal {} + +impl Capability for WriteTerminal { + const ID: &'static str = "terminals.write"; + type Value = Write; +} + +/// Marker of `terminals.bar`. +pub struct ShowTerminalBar; + +impl sealed::Sealed for ShowTerminalBar {} + +impl Capability for ShowTerminalBar { + const ID: &'static str = "terminals.bar"; + type Value = Bar; +} + +/// The bytes a terminal shows for `text`: printable characters, `\n` as a line break, `\t`, +/// and SGR colour sequences (`ESC [ digits ; … m`). Every other control character and escape +/// sequence is dropped whole, so a plugin cannot set the title, move the cursor, write the +/// clipboard, open a link or answer queries through the screen. +pub fn terminal_output(text: &str) -> String { + let mut out = String::with_capacity(text.len() + text.len() / 16); + let mut run = String::new(); + let mut chars = text.chars().peekable(); + while let Some(c) = chars.next() { + if !c.is_control() { + run.push(c); + continue; + } + // A run of plain text loses invisible and direction characters as view text does. + out.push_str(&sanitize_text(&run)); + run.clear(); + match c { + '\n' => out.push_str("\r\n"), + '\t' => out.push('\t'), + '\u{1b}' => match chars.next() { + Some('[') => keep_colour(&mut chars, &mut out), + Some(']' | 'P' | '_' | '^' | 'X') => skip_string(&mut chars), + _ => {} + }, + '\u{9b}' => { + let _ = control_sequence(&mut chars); + } + '\u{90}' | '\u{98}' | '\u{9d}' | '\u{9e}' | '\u{9f}' => skip_string(&mut chars), + _ => {} + } + } + out.push_str(&sanitize_text(&run)); + out +} + +/// Reads a control sequence after its introducer: parameters, intermediates and the final byte. +/// Returns the parameters and the final byte when it is well formed. +fn control_sequence( + chars: &mut core::iter::Peekable>, +) -> Option<(String, char)> { + let mut params = String::new(); + let mut plain = true; + for next in chars.by_ref() { + match next { + '0'..='9' | ';' if plain => { + if params.len() < 32 { + params.push(next); + } + } + '\u{30}'..='\u{3f}' | '\u{20}'..='\u{2f}' => plain = false, + '\u{40}'..='\u{7e}' => return plain.then_some((params, next)), + _ => return None, + } + } + None +} + +fn keep_colour(chars: &mut core::iter::Peekable>, out: &mut String) { + if let Some((params, 'm')) = control_sequence(chars) { + out.push_str("\u{1b}["); + out.push_str(¶ms); + out.push('m'); + } +} + +/// Skips a string sequence (OSC, DCS, APC, PM, SOS) up to its BEL or string terminator. +fn skip_string(chars: &mut core::iter::Peekable>) { + while let Some(next) = chars.next() { + match next { + '\u{7}' | '\u{9c}' => return, + '\u{1b}' if chars.peek() == Some(&'\\') => { + chars.next(); + return; + } + _ => {} + } + } +} diff --git a/crates/plugin-api/src/capability/timers.rs b/crates/plugin-api/src/capability/timers.rs new file mode 100644 index 00000000..efa4d970 --- /dev/null +++ b/crates/plugin-api/src/capability/timers.rs @@ -0,0 +1,45 @@ +//! `timers.set`: a `timer` event after a delay, under the effect's key. + +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::LimitError; + +/// The shortest delay, in milliseconds. +pub const MIN_TIMER_MS: u32 = 1000; +/// The longest delay, in milliseconds. +pub const MAX_TIMER_MS: u32 = 3_600_000; +/// Most timers one plugin has waiting; setting a key that waits replaces it. +pub const MAX_TIMERS: usize = 8; + +/// Wakes the plugin with a `timer` event after `after_ms`. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct SetTimer { + pub after_ms: u32, +} + +impl SetTimer { + pub fn check(&self) -> Result<(), LimitError> { + if self.after_ms < MIN_TIMER_MS { + return Err(LimitError::Syntax { field: "after_ms" }); + } + if self.after_ms > MAX_TIMER_MS { + return Err(LimitError::Range { + field: "after_ms", + max: MAX_TIMER_MS, + }); + } + Ok(()) + } +} + +/// Marker of `timers.set`. +pub struct Timer; + +impl sealed::Sealed for Timer {} + +impl Capability for Timer { + const ID: &'static str = "timers.set"; + type Value = SetTimer; +} diff --git a/crates/plugin-api/src/contribution.rs b/crates/plugin-api/src/contribution.rs index 108cd8bc..36e531f5 100644 --- a/crates/plugin-api/src/contribution.rs +++ b/crates/plugin-api/src/contribution.rs @@ -1,6 +1,6 @@ //! The author-facing words for where and what a contribution is. -use nocterm_plugin_model::{ContributionKind, Zone as ModelZone}; +use nocterm_plugin_model::{ContributionKind, Edge as ModelEdge, ToolbarTabs, Zone as ModelZone}; use serde::{Deserialize, Serialize}; /// A zone of the status bar, as an author names it. @@ -39,16 +39,19 @@ impl Zone { pub enum Kind { Widget, Panel, + /// A strip along an edge of the tab area. + Toolbar, } impl Kind { /// Every kind, in declaration order. - pub const ALL: [Self; 2] = [Self::Widget, Self::Panel]; + pub const ALL: [Self; 3] = [Self::Widget, Self::Panel, Self::Toolbar]; pub const fn name(self) -> &'static str { match self { Self::Widget => "widget", Self::Panel => "panel", + Self::Toolbar => "toolbar", } } @@ -56,6 +59,80 @@ impl Kind { match self { Self::Widget => ContributionKind::Widget, Self::Panel => ContributionKind::Panel, + Self::Toolbar => ContributionKind::Toolbar, + } + } + + /// The author's word for a kind of the shell. + pub const fn of_model(kind: ContributionKind) -> Self { + match kind { + ContributionKind::Widget => Self::Widget, + ContributionKind::Panel => Self::Panel, + ContributionKind::Toolbar => Self::Toolbar, + } + } +} + +/// The edge of the tab area a toolbar suggests, as an author names it. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Edge { + /// Under the tab bar. + Top, + Bottom, + Left, + Right, +} + +impl Edge { + /// Every edge, in declaration order. + pub const ALL: [Self; 4] = [Self::Top, Self::Bottom, Self::Left, Self::Right]; + + pub const fn name(self) -> &'static str { + match self { + Self::Top => "top", + Self::Bottom => "bottom", + Self::Left => "left", + Self::Right => "right", + } + } + + pub const fn to_model(self) -> ModelEdge { + match self { + Self::Top => ModelEdge::Top, + Self::Bottom => ModelEdge::Bottom, + Self::Left => ModelEdge::Left, + Self::Right => ModelEdge::Right, + } + } +} + +/// Which tabs a toolbar runs along, as an author names it. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Tabs { + /// Only the tabs the plugin opened, inside each of them. + #[default] + Own, + /// Every tab, along the window's tab area. + All, +} + +impl Tabs { + /// Every choice, in declaration order. + pub const ALL: [Self; 2] = [Self::Own, Self::All]; + + pub const fn name(self) -> &'static str { + match self { + Self::Own => "own", + Self::All => "all", + } + } + + pub const fn to_model(self) -> ToolbarTabs { + match self { + Self::Own => ToolbarTabs::Own, + Self::All => ToolbarTabs::All, } } } @@ -65,5 +142,7 @@ impl Kind { #[serde(rename_all = "snake_case")] pub struct ContributionContext { pub kind: Kind, + /// The status bar zone of a widget or panel toggle. For a toolbar, its axis: `center` + /// across the top or the bottom, `left` or `right` along that side, as a column. pub zone: Zone, } diff --git a/crates/plugin-api/src/effects.rs b/crates/plugin-api/src/effects.rs index 7a9b3f72..8576f2aa 100644 --- a/crates/plugin-api/src/effects.rs +++ b/crates/plugin-api/src/effects.rs @@ -9,9 +9,13 @@ use crate::{ HostError, LimitError, LocalId, capability::{ Capability, + files::{FileOp, FileReply, ServeFiles}, + net::{NetRequest, Request}, notifications::{Post, PostNotice}, panels::{SetBadge, SetOpen, SetPanelBadge, SetPanelOpen}, storage::{Remove, Set, StoreRemove, StoreSet}, + terminals::{Bar, Open, OpenTerminal, ShowTerminalBar, Write, WriteTerminal}, + timers::{SetTimer, Timer}, }, error::key_syntax, }; @@ -39,6 +43,34 @@ pub enum Event { visible: bool, }, SettingsChanged, + /// The answer to the `net.request` effect with this key. `status` is the HTTP status, or 0 + /// when no answer came (the body then says why, in the host's words). A body longer than + /// the response limit is cut there; bytes that are not UTF-8 are replaced. + Response { + key: String, + status: u16, + body: String, + }, + /// The `timers.set` effect with this key ran out. + Timer { + key: String, + }, + /// A line was entered in one of the plugin's terminals, by the user or an AI chat. + TerminalLine { + terminal: String, + line: String, + }, + /// The user closed one of the plugin's terminals. + TerminalClosed { + terminal: String, + }, + /// The file browser or an AI chat asks the files of one of the plugin's terminals, opened + /// with `files`; the plugin answers with a `terminals.files` effect under `request`. + FileRequest { + terminal: String, + request: u32, + op: FileOp, + }, } /// One thing a plugin asks for. Each variant is one effect capability. @@ -54,6 +86,18 @@ pub enum Effect { StorageSet(Set), #[serde(rename = "storage.remove")] StorageRemove(Remove), + #[serde(rename = "net.request")] + NetRequest(Request), + #[serde(rename = "timers.set")] + TimersSet(SetTimer), + #[serde(rename = "terminals.open")] + TerminalsOpen(Open), + #[serde(rename = "terminals.write")] + TerminalsWrite(Write), + #[serde(rename = "terminals.files")] + TerminalsFiles(FileReply), + #[serde(rename = "terminals.bar")] + TerminalsBar(Bar), } impl Effect { @@ -65,14 +109,26 @@ impl Effect { Self::PanelsSetBadge(_) => SetPanelBadge::ID, Self::StorageSet(_) => StoreSet::ID, Self::StorageRemove(_) => StoreRemove::ID, + Self::NetRequest(_) => NetRequest::ID, + Self::TimersSet(_) => Timer::ID, + Self::TerminalsOpen(_) => OpenTerminal::ID, + Self::TerminalsWrite(_) => WriteTerminal::ID, + Self::TerminalsFiles(_) => ServeFiles::ID, + Self::TerminalsBar(_) => ShowTerminalBar::ID, } } + /// Whether the outcome comes back as an event under the request's key, which it then needs. + pub fn needs_key(&self) -> bool { + matches!(self, Self::NetRequest(_) | Self::TimersSet(_)) + } + /// The own contribution the effect names, if it names one. pub fn local(&self) -> Option<&LocalId> { match self { Self::PanelsSetOpen(request) => Some(&request.local), Self::PanelsSetBadge(request) => Some(&request.local), + Self::TerminalsOpen(request) => Some(&request.local), _ => None, } } @@ -82,6 +138,12 @@ impl Effect { Self::NotificationsPost(request) => request.check(), Self::StorageSet(request) => request.check(), Self::StorageRemove(request) => request.check(), + Self::NetRequest(request) => request.check(), + Self::TimersSet(request) => request.check(), + Self::TerminalsOpen(request) => request.check(), + Self::TerminalsWrite(request) => request.check(), + Self::TerminalsFiles(request) => request.check(), + Self::TerminalsBar(request) => request.check(), Self::PanelsSetOpen(_) | Self::PanelsSetBadge(_) => Ok(()), } } @@ -108,6 +170,8 @@ pub enum EffectsError { TooMany, #[error("key `{key}` is used by two effects")] DuplicateKey { key: String }, + #[error("effect {index} answers with an event and needs a key")] + MissingKey { index: usize }, #[error("effect {index}: {source}")] Limit { index: usize, @@ -131,6 +195,9 @@ impl Effects { return Err(EffectsError::DuplicateKey { key: key.clone() }); } } + if request.key.is_none() && request.effect.needs_key() { + return Err(EffectsError::MissingKey { index }); + } request.effect.check().map_err(limit)?; } Ok(()) diff --git a/crates/plugin-api/src/icon.rs b/crates/plugin-api/src/icon.rs index fcb996a8..ac04d9c2 100644 --- a/crates/plugin-api/src/icon.rs +++ b/crates/plugin-api/src/icon.rs @@ -1,4 +1,4 @@ -//! The icons a plugin view may draw. +//! The icons a plugin names: [`PluginIcon`] for its contributions, [`Glyph`] inside its views. use nocterm_plugin_model::Icon; use serde::{Deserialize, Serialize}; @@ -54,3 +54,235 @@ impl PluginIcon { } } } + +/// An icon inside a plugin's own content: a row's leading icon, an icon button, an icon node. +/// +/// A plugin may draw any of these in the frame the host gives it, where none can stand for a +/// function of the application; what a plugin shows outside that frame (the switcher of its +/// panel) is a [`PluginIcon`]. Names are frozen and the list is append-only; the first six are +/// the plugin icons, in the same order. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +#[non_exhaustive] +pub enum Glyph { + Box, + Gem, + Hexagon, + Plug, + Puzzle, + Shapes, + Activity, + ArrowDown, + ArrowLeft, + ArrowUp, + Bell, + Cable, + Calendar, + Check, + ChevronDown, + ChevronLeft, + ChevronRight, + CircleAlert, + CircleCheck, + CircleX, + Clock, + Container, + Copy, + Cpu, + Ellipsis, + ExternalLink, + Eye, + File, + FileText, + Folder, + FolderOpen, + Gauge, + Globe, + HardDrive, + Inbox, + Info, + Key, + Loader, + Lock, + Memory, + Network, + Pause, + Pencil, + Play, + Plus, + Minus, + Refresh, + Rotate, + Search, + Send, + Server, + ServerOff, + Settings, + Square, + Stop, + Star, + Terminal, + Thermometer, + Trash, + TriangleAlert, + Upload, + User, + Wrench, + X, + Zap, +} + +impl Glyph { + /// Every glyph, in declaration order. + pub const ALL: [Self; 65] = [ + Self::Box, + Self::Gem, + Self::Hexagon, + Self::Plug, + Self::Puzzle, + Self::Shapes, + Self::Activity, + Self::ArrowDown, + Self::ArrowLeft, + Self::ArrowUp, + Self::Bell, + Self::Cable, + Self::Calendar, + Self::Check, + Self::ChevronDown, + Self::ChevronLeft, + Self::ChevronRight, + Self::CircleAlert, + Self::CircleCheck, + Self::CircleX, + Self::Clock, + Self::Container, + Self::Copy, + Self::Cpu, + Self::Ellipsis, + Self::ExternalLink, + Self::Eye, + Self::File, + Self::FileText, + Self::Folder, + Self::FolderOpen, + Self::Gauge, + Self::Globe, + Self::HardDrive, + Self::Inbox, + Self::Info, + Self::Key, + Self::Loader, + Self::Lock, + Self::Memory, + Self::Network, + Self::Pause, + Self::Pencil, + Self::Play, + Self::Plus, + Self::Minus, + Self::Refresh, + Self::Rotate, + Self::Search, + Self::Send, + Self::Server, + Self::ServerOff, + Self::Settings, + Self::Square, + Self::Stop, + Self::Star, + Self::Terminal, + Self::Thermometer, + Self::Trash, + Self::TriangleAlert, + Self::Upload, + Self::User, + Self::Wrench, + Self::X, + Self::Zap, + ]; + + /// The frozen kebab-case name. + pub const fn name(self) -> &'static str { + match self { + Self::Box => "box", + Self::Gem => "gem", + Self::Hexagon => "hexagon", + Self::Plug => "plug", + Self::Puzzle => "puzzle", + Self::Shapes => "shapes", + Self::Activity => "activity", + Self::ArrowDown => "arrow-down", + Self::ArrowLeft => "arrow-left", + Self::ArrowUp => "arrow-up", + Self::Bell => "bell", + Self::Cable => "cable", + Self::Calendar => "calendar", + Self::Check => "check", + Self::ChevronDown => "chevron-down", + Self::ChevronLeft => "chevron-left", + Self::ChevronRight => "chevron-right", + Self::CircleAlert => "circle-alert", + Self::CircleCheck => "circle-check", + Self::CircleX => "circle-x", + Self::Clock => "clock", + Self::Container => "container", + Self::Copy => "copy", + Self::Cpu => "cpu", + Self::Ellipsis => "ellipsis", + Self::ExternalLink => "external-link", + Self::Eye => "eye", + Self::File => "file", + Self::FileText => "file-text", + Self::Folder => "folder", + Self::FolderOpen => "folder-open", + Self::Gauge => "gauge", + Self::Globe => "globe", + Self::HardDrive => "hard-drive", + Self::Inbox => "inbox", + Self::Info => "info", + Self::Key => "key", + Self::Loader => "loader", + Self::Lock => "lock", + Self::Memory => "memory", + Self::Network => "network", + Self::Pause => "pause", + Self::Pencil => "pencil", + Self::Play => "play", + Self::Plus => "plus", + Self::Minus => "minus", + Self::Refresh => "refresh", + Self::Rotate => "rotate", + Self::Search => "search", + Self::Send => "send", + Self::Server => "server", + Self::ServerOff => "server-off", + Self::Settings => "settings", + Self::Square => "square", + Self::Stop => "stop", + Self::Star => "star", + Self::Terminal => "terminal", + Self::Thermometer => "thermometer", + Self::Trash => "trash", + Self::TriangleAlert => "triangle-alert", + Self::Upload => "upload", + Self::User => "user", + Self::Wrench => "wrench", + Self::X => "x", + Self::Zap => "zap", + } + } +} + +impl From for Glyph { + fn from(icon: PluginIcon) -> Self { + match icon { + PluginIcon::Box => Self::Box, + PluginIcon::Gem => Self::Gem, + PluginIcon::Hexagon => Self::Hexagon, + PluginIcon::Plug => Self::Plug, + PluginIcon::Puzzle => Self::Puzzle, + PluginIcon::Shapes => Self::Shapes, + } + } +} diff --git a/crates/plugin-api/src/lib.rs b/crates/plugin-api/src/lib.rs index 87936aec..b17ea44f 100644 --- a/crates/plugin-api/src/lib.rs +++ b/crates/plugin-api/src/lib.rs @@ -11,8 +11,10 @@ //! bound to `(PluginId, sha256)`, the declarative template language and its interpreter //! (`DeclarativeRuntime`), the code package format (`package_format`: kind detection and the //! bounded scan for the manifest section of a WebAssembly module), `Runtimes` (which picks the -//! declarative or the injected code runtime by the manifest), and the `PluginPackages` and -//! `PluginRuntime` ports with their errors. +//! declarative or the injected code runtime by the manifest), the requests and answers of a +//! terminal tab's files (`capability::files`) and the base64 their bytes travel in, the bar of a +//! terminal tab (`terminals.bar`) and the snapshot's `terminal` its calls name, and the +//! `PluginPackages`, `PluginRuntime` and `PluginNet` ports with their errors. //! Does not own: file access, TOML parsing, hashing, the broker, rate limits, providers, views //! or any GPUI type. //! Ports used/provided: provides `PluginPackages` (implemented by `nocterm-plugin-packages`) @@ -31,6 +33,7 @@ extern crate alloc; #[cfg(test)] extern crate std; +pub mod base64; pub mod capability; mod contribution; mod declarative; @@ -54,28 +57,29 @@ pub use capability::{ CAPABILITIES, Capability, CapabilityEntry, Deprecation, EffectTarget, Family, TableError, validate_table, }; -pub use contribution::{ContributionContext, Kind, Zone}; +pub use contribution::{ContributionContext, Edge, Kind, Tabs, Zone}; pub use declarative::DeclarativeRuntime; pub use effects::{ Effect, EffectOutcome, EffectRequest, Effects, EffectsError, Event, MAX_EFFECT_KEY, MAX_EFFECTS, }; pub use error::{HostError, LimitError}; pub use grant::{Additions, Grant, GrantError}; -pub use icon::PluginIcon; +pub use icon::{Glyph, PluginIcon}; pub use identity::{ContentHash, HashError, LocalId}; pub use manifest::{ - ChoiceOptionDto, ContributionDto, ContributionError, DeclareError, DefaultDto, FieldDto, - FieldKindName, GroupDto, HostContext, MAX_CONTRIBUTIONS, MAX_DECLARED, MAX_DESCRIPTION, - MAX_NAME, MAX_REASON, Manifest, ManifestDto, ManifestError, ManifestHeader, OptionalCapability, - Runtime, SettingsDto, SettingsError, + ChoiceOptionDto, ContributionDto, ContributionError, Credential, DeclareError, DefaultDto, + FieldDto, FieldKindName, GroupDto, HostContext, MAX_CONTRIBUTIONS, MAX_DECLARED, + MAX_DESCRIPTION, MAX_NAME, MAX_REASON, Manifest, ManifestDto, ManifestError, ManifestHeader, + Network, NetworkDto, NetworkError, NetworkTableError, OptionalCapability, Runtime, SettingsDto, + SettingsError, }; pub use package_format::{MANIFEST_SECTION, PackageKind, SectionError, kind, manifest_section}; pub use ports::{ Cause, MAX_MODULE_BYTES, MAX_PACKAGE_BYTES, ModuleError, Package, PackageError, PluginInstance, - PluginPackages, PluginRuntime, ProtocolError, RuntimeError, + PluginNet, PluginPackages, PluginRuntime, ProtocolError, RuntimeError, }; pub use runtimes::Runtimes; -pub use sanitize::sanitize_display; +pub use sanitize::{sanitize_code, sanitize_display}; pub use snapshot::{Observed, Snapshot}; pub use surface::{ EFFECT_REQUESTS, EVENTS, FILTERS, HOST_ERRORS, Key, KeyType, LIMITS, MANIFEST_KEYS, REFRESHES, @@ -83,8 +87,9 @@ pub use surface::{ }; pub use version::{API_VERSION, ApiVersion, VersionError}; pub use view::{ - MAX_BADGE, MAX_BUTTON, MAX_DEPTH, MAX_LIST_ITEMS, MAX_NODE_KEY, MAX_NODES, MAX_TEXT, - MIN_REFRESH_MS, Node, NodeKind, Refresh, Tone, View, ViewError, + MAX_BADGE, MAX_BUTTON, MAX_CHART_POINTS, MAX_CHART_SERIES, MAX_CODE, MAX_DEPTH, MAX_LIST_ITEMS, + MAX_NODE_KEY, MAX_NODES, MAX_TEXT, MIN_REFRESH_MS, Node, NodeKind, Refresh, Tone, View, + ViewError, }; #[cfg(test)] diff --git a/crates/plugin-api/src/manifest.rs b/crates/plugin-api/src/manifest.rs index aca3fb9f..ddeee6cb 100644 --- a/crates/plugin-api/src/manifest.rs +++ b/crates/plugin-api/src/manifest.rs @@ -24,10 +24,12 @@ use crate::{ mod contribution; mod declare; +mod network; mod settings; pub use contribution::{ContributionDto, ContributionError}; pub use declare::{DeclareError, MAX_DECLARED, OptionalCapability}; +pub use network::{Credential, Network, NetworkDto, NetworkError, NetworkTableError}; pub use settings::{ ChoiceOptionDto, DefaultDto, FieldDto, FieldKindName, GroupDto, SettingsDto, SettingsError, }; @@ -97,6 +99,9 @@ pub struct ManifestDto { pub contributions: Vec, #[serde(default)] pub settings: Option, + /// Where `net.request` goes and what it carries: the keys of [`NetworkDto`]. + #[serde(default)] + pub network: Option>, } /// What the host knows that a manifest is checked against. @@ -152,6 +157,8 @@ pub enum ManifestError { }, #[error("settings")] Settings(#[from] SettingsError), + #[error("network")] + Network(#[from] NetworkError), #[error("the plugin is invalid")] Plugin(#[from] UserPluginError), } @@ -167,6 +174,7 @@ pub struct Manifest { optional: Vec, reasons: BTreeMap, views: BTreeMap, + network: Option, } impl Manifest { @@ -191,6 +199,14 @@ impl Manifest { Runtime::Wasm => {} } let schema = settings::schema(&id, dto.settings.as_ref())?; + let requests = network::declares_requests( + declaration + .required + .iter() + .map(|entry| entry.id) + .chain(declaration.optional.iter().map(OptionalCapability::id)), + ); + let network = network::network(dto.network.as_ref(), requests, &schema)?; let built = contribution::build( &id, dto.runtime, @@ -210,9 +226,16 @@ impl Manifest { optional: declaration.optional, reasons: declaration.reasons, views: built.views, + network, }) } + /// Where the package's requests go and the credential they carry, when it declares + /// `net.request`. + pub fn network(&self) -> Option<&Network> { + self.network.as_ref() + } + pub fn api(&self) -> ApiVersion { self.api } diff --git a/crates/plugin-api/src/manifest/contribution.rs b/crates/plugin-api/src/manifest/contribution.rs index b23d16c3..92aad233 100644 --- a/crates/plugin-api/src/manifest/contribution.rs +++ b/crates/plugin-api/src/manifest/contribution.rs @@ -12,7 +12,7 @@ use thiserror::Error; use super::{HostContext, ManifestError, Runtime, declare::Declaration, is_plain, is_reserved}; use crate::{ - Kind, LimitError, LocalId, PluginIcon, Zone, + Edge, Kind, LimitError, LocalId, PluginIcon, Tabs, Zone, template::{ViewDto, ViewTemplate, ViewTemplateError}, }; @@ -31,6 +31,17 @@ pub struct ContributionDto { pub zone: Option, #[serde(default)] pub order: u16, + /// The edge of the tab area a toolbar suggests. Default: top. Toolbars only. + #[serde(default)] + pub edge: Option, + /// Whether a toolbar is shown along its `edge` once the plugin is turned on, before the user + /// hides or moves it. Default: it waits until the user shows it. Toolbars only. + #[serde(default)] + pub shown: bool, + /// Which tabs a toolbar runs along: `own`, the tabs the plugin opened (the default), or + /// `all`. Toolbars only. + #[serde(default)] + pub tabs: Option, /// The template of a declarative contribution. A code package draws in code and has none. #[serde(default)] pub view: Option, @@ -43,8 +54,16 @@ pub enum ContributionError { Local(#[from] LimitError), #[error("invalid id")] Id(#[from] IdError), - #[error("it needs a `kind` of widget or panel")] + #[error("it needs a `kind` of widget, panel or toolbar")] NoKind, + #[error("only a toolbar runs along an `edge`")] + EdgeNotToolbar, + #[error("only a toolbar can be `shown` from the start; panels and widgets wait in the tray")] + ShownNotToolbar, + #[error("only a toolbar runs along `tabs`")] + TabsNotToolbar, + #[error("a plugin has one toolbar for its own tabs")] + SecondOwnTabsToolbar, #[error( "its title must be 1 to 48 letters, digits, spaces, `.`, `_` or `-`, and not a name the app reserves" )] @@ -76,6 +95,7 @@ pub(super) fn build( let mut contributions = Vec::with_capacity(dtos.len()); let mut views = BTreeMap::new(); let mut locals = BTreeSet::new(); + let mut own_tabs = BTreeSet::new(); for dto in dtos { let fail = |source: ContributionError| ManifestError::Contribution { local: dto.local.chars().take(32).collect(), @@ -83,6 +103,19 @@ pub(super) fn build( }; let local = LocalId::parse(&dto.local).map_err(|e| fail(e.into()))?; let kind = dto.kind.ok_or_else(|| fail(ContributionError::NoKind))?; + if dto.edge.is_some() && kind != Kind::Toolbar { + return Err(fail(ContributionError::EdgeNotToolbar)); + } + if dto.shown && kind != Kind::Toolbar { + return Err(fail(ContributionError::ShownNotToolbar)); + } + if dto.tabs.is_some() && kind != Kind::Toolbar { + return Err(fail(ContributionError::TabsNotToolbar)); + } + let tabs = dto.tabs.unwrap_or_default(); + if kind == Kind::Toolbar && tabs == Tabs::Own && !own_tabs.insert(()) { + return Err(fail(ContributionError::SecondOwnTabsToolbar)); + } if !is_plain(&dto.title) { return Err(fail(ContributionError::Title)); } @@ -103,7 +136,10 @@ pub(super) fn build( dto.icon.unwrap_or(PluginIcon::Plug).to_icon(), dto.zone.unwrap_or(Zone::Right).to_model(), dto.order, - )?; + )? + .along(dto.edge.unwrap_or(Edge::Top).to_model()) + .shown_at_first(dto.shown) + .on_tabs(tabs.to_model()); // The check that matters runs on the title the shell will show. if is_reserved(clamped.title(), host) { return Err(fail(ContributionError::Title)); diff --git a/crates/plugin-api/src/manifest/network.rs b/crates/plugin-api/src/manifest/network.rs new file mode 100644 index 00000000..ccadfe13 --- /dev/null +++ b/crates/plugin-api/src/manifest/network.rs @@ -0,0 +1,229 @@ +//! The `[network]` table: which settings hold the web address `net.request` reaches and the +//! credential the host sends with it. +//! +//! The origin is whatever the user types into the named text setting, so a plugin reaches only +//! the address its user chose. The credential is a secret setting the plugin never reads: the +//! host puts it into one request header, for that origin only. + +use alloc::{collections::BTreeMap, string::String}; + +use nocterm_plugin_model::{FieldKind, SettingsSchema}; +use serde::Serialize; +use thiserror::Error; + +use crate::capability::{Capability as _, net::NetRequest}; + +/// Longest credential header name. +const MAX_HEADER: usize = 64; +/// Longest credential prefix, in characters. +const MAX_PREFIX: usize = 32; + +/// The table as written. The manifest reads it as a table of text values, which +/// [`NetworkDto::try_from`] turns into this, refusing unknown keys. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub struct NetworkDto { + /// The key of a text setting that holds the origin, such as `https://service.example.com`. + pub origin_setting: String, + /// The key of a secret text setting the host sends in `credential_header`. + #[serde(default)] + pub credential_setting: Option, + /// Default `Authorization`. Otherwise a header named `X-…`. + #[serde(default)] + pub credential_header: Option, + /// What comes before the credential in the header. Default `Bearer `. + #[serde(default)] + pub credential_prefix: Option, +} + +/// A `[network]` table that does not read. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum NetworkTableError { + #[error("missing field `origin_setting`")] + NoOrigin, + #[error("unknown field `{0}`")] + Unknown(String), +} + +impl TryFrom> for NetworkDto { + type Error = NetworkTableError; + + fn try_from(mut table: BTreeMap) -> Result { + let dto = Self { + origin_setting: table + .remove("origin_setting") + .ok_or(NetworkTableError::NoOrigin)?, + credential_setting: table.remove("credential_setting"), + credential_header: table.remove("credential_header"), + credential_prefix: table.remove("credential_prefix"), + }; + match table.into_keys().next() { + Some(key) => Err(NetworkTableError::Unknown(key)), + None => Ok(dto), + } + } +} + +/// Where a package's requests go and what they carry, checked against its settings. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Network { + origin_setting: String, + credential: Option, +} + +/// The secret setting a request carries, and how. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Credential { + setting: String, + header: String, + prefix: String, +} + +/// A `[network]` table, or its absence, the host refuses. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum NetworkError { + #[error("the `[network]` table does not read")] + Table(#[from] NetworkTableError), + #[error("a package that declares `net.request` needs a `[network]` table")] + Missing, + #[error("a `[network]` table needs the `net.request` capability")] + Unused, + #[error("`origin_setting` names `{0}`, which is not a text setting that is not secret")] + Origin(String), + #[error("`credential_setting` names `{0}`, which is not a secret text setting")] + Credential(String), + #[error("the secret setting `{0}` is not the credential of the `[network]` table")] + StraySecret(String), + #[error("`credential_header` must be `Authorization` or a header named `X-…`")] + Header, + #[error("`credential_prefix` holds at most {MAX_PREFIX} visible characters and spaces")] + Prefix, +} + +impl Network { + /// The key of the setting whose value is the origin of every request. + pub fn origin_setting(&self) -> &str { + &self.origin_setting + } + + pub fn credential(&self) -> Option<&Credential> { + self.credential.as_ref() + } +} + +impl Credential { + /// The key of the secret setting. + pub fn setting(&self) -> &str { + &self.setting + } + + pub fn header(&self) -> &str { + &self.header + } + + pub fn prefix(&self) -> &str { + &self.prefix + } +} + +/// Checks the table against the declared capabilities and the settings schema. +pub(super) fn network( + table: Option<&BTreeMap>, + requests: bool, + schema: &SettingsSchema, +) -> Result, NetworkError> { + let dto = table.cloned().map(NetworkDto::try_from).transpose()?; + let Some(dto) = dto.as_ref() else { + if requests { + return Err(NetworkError::Missing); + } + return match stray_secret(schema, None) { + Some(key) => Err(NetworkError::StraySecret(key)), + None => Ok(None), + }; + }; + if !requests { + return Err(NetworkError::Unused); + } + let text = |key: &str| { + schema.field(key).and_then(|field| match field.kind { + FieldKind::Text { secret, .. } => Some(secret), + _ => None, + }) + }; + if text(&dto.origin_setting) != Some(false) { + return Err(NetworkError::Origin(dto.origin_setting.clone())); + } + let credential = match &dto.credential_setting { + None => { + if dto.credential_header.is_some() || dto.credential_prefix.is_some() { + return Err(NetworkError::Credential(String::new())); + } + None + } + Some(key) => { + if text(key) != Some(true) { + return Err(NetworkError::Credential(key.clone())); + } + let header = dto + .credential_header + .clone() + .unwrap_or_else(|| String::from("Authorization")); + if !header_allowed(&header) { + return Err(NetworkError::Header); + } + let prefix = dto + .credential_prefix + .clone() + .unwrap_or_else(|| String::from("Bearer ")); + if prefix.chars().count() > MAX_PREFIX + || !prefix + .bytes() + .all(|byte| byte.is_ascii_graphic() || byte == b' ') + { + return Err(NetworkError::Prefix); + } + Some(Credential { + setting: key.clone(), + header, + prefix, + }) + } + }; + if let Some(key) = stray_secret(schema, credential.as_ref()) { + return Err(NetworkError::StraySecret(key)); + } + Ok(Some(Network { + origin_setting: dto.origin_setting.clone(), + credential, + })) +} + +/// Whether a package declares `net.request`, required or optional. +pub(super) fn declares_requests<'a>(mut ids: impl Iterator) -> bool { + ids.any(|id| id == NetRequest::ID) +} + +/// A secret setting that is not the credential: a package may not collect secrets it never +/// sends. +fn stray_secret(schema: &SettingsSchema, credential: Option<&Credential>) -> Option { + schema + .fields() + .filter(|field| field.kind.is_secret()) + .find(|field| credential.is_none_or(|credential| credential.setting != field.key)) + .map(|field| String::from(&*field.key)) +} + +/// `Authorization`, or a header of the `X-` family, in token characters. +fn header_allowed(header: &str) -> bool { + let token = !header.is_empty() + && header.len() <= MAX_HEADER + && header + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-'); + let named = header.eq_ignore_ascii_case("authorization") + || header + .get(..2) + .is_some_and(|start| start.eq_ignore_ascii_case("x-")); + token && named +} diff --git a/crates/plugin-api/src/manifest/settings.rs b/crates/plugin-api/src/manifest/settings.rs index d464cf80..befe75d5 100644 --- a/crates/plugin-api/src/manifest/settings.rs +++ b/crates/plugin-api/src/manifest/settings.rs @@ -1,6 +1,7 @@ //! The `[settings]` table of a package, mapped onto the shared settings schema. //! -//! The kinds are `toggle`, `choice`, `multi-choice`, `number` and `text`; the limits and the +//! The kinds are `toggle`, `choice`, `multi-choice`, `number`, `text` and `secret` (masked +//! text the plugin never reads, which a `[network]` table sends); the limits and the //! validation are the schema's. Labels, help and option labels are sanitized, at most 48 and 160 //! characters. @@ -26,6 +27,8 @@ pub enum FieldKindName { MultiChoice, Number, Text, + /// Masked single-line text the plugin never reads: the credential a `[network]` table sends. + Secret, } /// The default of a field. @@ -195,10 +198,12 @@ fn field(dto: &FieldDto) -> Result { })?, }, FieldKindName::Number => number(dto)?, - FieldKindName::Text => FieldKind::Text { + FieldKindName::Text | FieldKindName::Secret => FieldKind::Text { default: match &dto.default { None => Cow::Borrowed(""), - Some(DefaultDto::Text(value)) => Cow::Owned(value.clone()), + Some(DefaultDto::Text(value)) if kind_name == FieldKindName::Text => { + Cow::Owned(value.clone()) + } Some(_) => return Err(SettingsError::Default { key }), }, max_len: dto.max_len.ok_or(SettingsError::Missing { @@ -209,6 +214,7 @@ fn field(dto: &FieldDto) -> Result { .placeholder .as_deref() .map(|text| Cow::Owned(sanitize_display(text, MAX_HELP))), + secret: kind_name == FieldKindName::Secret, }, }; Ok(FieldSpec { @@ -268,8 +274,9 @@ fn options(dto: &FieldDto) -> Cow<'static, [ChoiceOption]> { /// Refuses a key that belongs to another kind, so a typo is an error and not a silent no-op. fn stray(dto: &FieldDto, kind: FieldKindName) -> Result<(), SettingsError> { - use FieldKindName::{Choice, MultiChoice, Number, Text}; + use FieldKindName::{Choice, MultiChoice, Number, Secret, Text}; let choosing = matches!(kind, Choice | MultiChoice); + let typing = matches!(kind, Text | Secret); let found = [ ("options", !dto.options.is_empty() && !choosing), ("ordered", dto.ordered.is_some() && kind != MultiChoice), @@ -281,8 +288,8 @@ fn stray(dto: &FieldDto, kind: FieldKindName) -> Result<(), SettingsError> { ("max", dto.max.is_some() && kind != Number), ("step", dto.step.is_some() && kind != Number), ("unit", dto.unit.is_some() && kind != Number), - ("max_len", dto.max_len.is_some() && kind != Text), - ("placeholder", dto.placeholder.is_some() && kind != Text), + ("max_len", dto.max_len.is_some() && !typing), + ("placeholder", dto.placeholder.is_some() && !typing), ]; match found.into_iter().find(|(_, stray)| *stray) { Some((field, _)) => Err(SettingsError::Stray { diff --git a/crates/plugin-api/src/ports.rs b/crates/plugin-api/src/ports.rs index eb70ed76..8bec0a6b 100644 --- a/crates/plugin-api/src/ports.rs +++ b/crates/plugin-api/src/ports.rs @@ -121,6 +121,10 @@ pub enum PackageError { Unwritable(#[source] Cause), #[error("`{0}` is not the id of a user plugin, so it names no package file")] NotUserId(PluginId), + #[error( + "the plugin has {max} retained package versions; finish or retry its pending publication first" + )] + PendingVersions { max: usize }, } /// Reads, stores and removes packages. Implemented by an adapter. @@ -129,8 +133,8 @@ pub trait PluginPackages: Send + Sync { /// parses the manifest. It reads only; nothing is stored. fn read(&self, location: &str) -> Result; - /// Stores the exact bytes of a read package privately and atomically. A package with the - /// same plugin id is a duplicate unless `replace` says it is an update. + /// Stores the exact bytes privately under their hash without removing earlier consented + /// versions. A duplicate id is refused unless `replace` permits an update or staged retry. fn publish(&self, package: &Package, replace: bool) -> Result<(), PackageError>; /// Loads an installed package and checks that its bytes still hash to `expected`. @@ -138,6 +142,12 @@ pub trait PluginPackages: Send + Sync { /// Removes an installed package. Removing one that is not installed succeeds. fn remove(&self, plugin: &PluginId) -> Result<(), PackageError>; + + /// Removes superseded package versions after the matching records document is durable. + /// The caller serializes package management until this operation finishes. + fn retain(&self, _plugin: &PluginId, _expected: &ContentHash) -> Result<(), PackageError> { + Ok(()) + } } /// Why a code module cannot be run at all. @@ -213,6 +223,13 @@ pub trait PluginInstance: Send + Sync { ) -> Result; } +/// Sends a plugin's requests. An adapter implements it over an HTTP client: no cookies, no +/// redirects followed, a bounded body, a timeout, and the address checked again against the +/// origin. The host calls it on a background thread. +pub trait PluginNet: Send + Sync { + fn send(&self, call: &crate::capability::net::NetCall) -> crate::capability::net::NetAnswer; +} + /// Turns a package into an instance: the declarative interpreter or a code runtime. pub trait PluginRuntime: Send + Sync { fn instantiate(&self, package: &Package) -> Result, RuntimeError>; diff --git a/crates/plugin-api/src/sanitize.rs b/crates/plugin-api/src/sanitize.rs index 397b9bf6..37f19b16 100644 --- a/crates/plugin-api/src/sanitize.rs +++ b/crates/plugin-api/src/sanitize.rs @@ -12,3 +12,16 @@ use nocterm_plugin_model::sanitize_text; pub fn sanitize_display(text: &str, cap: usize) -> String { sanitize_text(text).chars().take(cap).collect() } + +/// `text` as inert lines of at most `cap` characters in all: each line as +/// [`sanitize_display`] keeps it, tabs as four spaces, and `\n` between the lines. +pub fn sanitize_code(text: &str, cap: usize) -> String { + let mut out = String::with_capacity(text.len().min(cap)); + for (index, line) in text.split('\n').enumerate() { + if index > 0 { + out.push('\n'); + } + out.push_str(&sanitize_text(&line.replace('\t', " "))); + } + out.chars().take(cap).collect() +} diff --git a/crates/plugin-api/src/snapshot.rs b/crates/plugin-api/src/snapshot.rs index 07962487..777b32bb 100644 --- a/crates/plugin-api/src/snapshot.rs +++ b/crates/plugin-api/src/snapshot.rs @@ -72,6 +72,9 @@ pub struct Snapshot { pub context: ContributionContext, /// What happened to the effects of the previous call, by their keys. pub outcomes: Vec, + /// The plugin's terminal whose bar this call draws, or whose bar the user used + /// (`terminals.bar`); `None` for every other call. + pub terminal: Option, } impl Snapshot { @@ -83,6 +86,7 @@ impl Snapshot { storage: BTreeMap::new(), context, outcomes: Vec::new(), + terminal: None, } } diff --git a/crates/plugin-api/src/surface.rs b/crates/plugin-api/src/surface.rs index 07c4c05f..6a22bb6b 100644 --- a/crates/plugin-api/src/surface.rs +++ b/crates/plugin-api/src/surface.rs @@ -10,12 +10,19 @@ use nocterm_plugin_model::{ }; use crate::{ - MAX_BADGE, MAX_BUTTON, MAX_CONTRIBUTIONS, MAX_DECLARED, MAX_DEPTH, MAX_DESCRIPTION, - MAX_EFFECT_KEY, MAX_EFFECTS, MAX_LIST_ITEMS, MAX_NAME, MAX_NODE_KEY, MAX_NODES, - MAX_PACKAGE_BYTES, MAX_REASON, MAX_TEXT, MIN_REFRESH_MS, + MAX_BADGE, MAX_BUTTON, MAX_CHART_POINTS, MAX_CHART_SERIES, MAX_CODE, MAX_CONTRIBUTIONS, + MAX_DECLARED, MAX_DEPTH, MAX_DESCRIPTION, MAX_EFFECT_KEY, MAX_EFFECTS, MAX_LIST_ITEMS, + MAX_NAME, MAX_NODE_KEY, MAX_NODES, MAX_PACKAGE_BYTES, MAX_REASON, MAX_TEXT, MIN_REFRESH_MS, capability::{ + files::{ + FILE_TIMEOUT_MS, MAX_FILE_BYTES, MAX_FILE_ENTRIES, MAX_FILE_NAME, MAX_FILE_PATH, + MAX_FILE_REASON, MAX_FILE_REQUESTS, + }, + net::{MAX_BODY, MAX_BYTES_RESPONSE, MAX_IN_FLIGHT, MAX_PATH, MAX_RESPONSE, TIMEOUT_MS}, notifications::{MAX_LIVE_NOTICES, MAX_NOTICE_TEXT}, storage::{MAX_KEY as MAX_STORAGE_KEY, MAX_TOTAL, MAX_VALUE}, + terminals::{MAX_LINE, MAX_TERMINAL_KEY, MAX_TERMINAL_TITLE, MAX_TERMINALS, MAX_WRITE}, + timers::{MAX_TIMER_MS, MAX_TIMERS, MIN_TIMER_MS}, }, template::{MAX_PLACEHOLDERS, MAX_ROUND, MAX_TEMPLATE_LEN}, }; @@ -121,6 +128,7 @@ pub const MANIFEST_KEYS: &[(&str, &[Key])] = &[ optional("reasons", TextMap), optional("contribution", TableList), optional("settings", Table), + optional("network", Table), ], ), ( @@ -132,6 +140,9 @@ pub const MANIFEST_KEYS: &[(&str, &[Key])] = &[ optional("icon", Choice("icons")), optional("zone", Choice("zones")), optional("order", U16), + optional("edge", Choice("edges")), + optional("shown", Bool), + optional("tabs", Choice("tabs")), optional("view", Table), ], ), @@ -148,7 +159,9 @@ pub const MANIFEST_KEYS: &[(&str, &[Key])] = &[ optional("tone", Choice("tones")), optional("tone_when", Text), optional("text", Text), - optional("icon", Choice("icons")), + optional("detail", Text), + optional("folded", Bool), + optional("icon", Choice("glyphs")), optional("value", Text), optional("each", Text), ], @@ -193,6 +206,15 @@ pub const MANIFEST_KEYS: &[(&str, &[Key])] = &[ optional("help", Text), ], ), + ( + "network", + &[ + required("origin_setting", Text), + optional("credential_setting", Text), + optional("credential_header", Text), + optional("credential_prefix", Text), + ], + ), ]; /// The request fields of each effect capability, with the type each holds and whether a guest @@ -220,10 +242,64 @@ pub const EFFECT_REQUESTS: &[(&str, &[Key])] = &[ &[required("key", Text), required("value", Text)], ), ("storage.remove", &[required("key", Text)]), + ( + "net.request", + &[ + required("method", Choice("http_methods")), + required("path", Text), + optional("body", Text), + optional("bytes", Bool), + ], + ), + ( + "timers.set", + &[required("after_ms", integer(0, u32::MAX as i64))], + ), + ( + "terminals.open", + &[ + required("local", Text), + required("terminal", Text), + required("title", Text), + optional("files", Bool), + ], + ), + ( + "terminals.write", + &[required("terminal", Text), required("text", Text)], + ), + ( + "terminals.files", + &[ + required("terminal", Text), + required( + "request", + KeyType::Integer { + min: 0, + max: u32::MAX as i64, + }, + ), + required("answer", Any), + ], + ), + ( + "terminals.bar", + &[ + required("terminal", Text), + optional("edge", Choice("edges")), + ], + ), ]; /// The kinds of setting a package may declare. -pub const SETTINGS_KINDS: [&str; 5] = ["toggle", "choice", "multi-choice", "number", "text"]; +pub const SETTINGS_KINDS: [&str; 6] = [ + "toggle", + "choice", + "multi-choice", + "number", + "text", + "secret", +]; /// The runtimes a package may name. pub const RUNTIMES: [&str; 2] = ["declarative", "wasm"]; @@ -232,11 +308,16 @@ pub const RUNTIMES: [&str; 2] = ["declarative", "wasm"]; pub const FILTERS: [&str; 4] = ["round(n)", "percent", "hh-mm", "threshold(low, high)"]; /// The events a plugin receives. -pub const EVENTS: [&str; 4] = [ +pub const EVENTS: [&str; 9] = [ "activated", "toggled", "visibility-changed", "settings-changed", + "response", + "timer", + "terminal-line", + "terminal-closed", + "file-request", ]; /// The errors the host reports to a plugin. @@ -250,13 +331,24 @@ pub const LIMITS: &[(&str, u64)] = &[ ("badge-chars", MAX_BADGE as u64), ("button-chars", MAX_BUTTON as u64), ("capabilities-per-package", MAX_DECLARED as u64), + ("chart-points", MAX_CHART_POINTS as u64), + ("chart-series", MAX_CHART_SERIES as u64), + ("code-chars", MAX_CODE as u64), ("contributions-per-package", MAX_CONTRIBUTIONS as u64), ("description-chars", MAX_DESCRIPTION as u64), ("effect-key-chars", MAX_EFFECT_KEY as u64), ("effects-per-call", MAX_EFFECTS as u64), + ("file-bytes", MAX_FILE_BYTES as u64), + ("file-entries", MAX_FILE_ENTRIES as u64), + ("file-name-bytes", MAX_FILE_NAME as u64), + ("file-path-bytes", MAX_FILE_PATH as u64), + ("file-reason-chars", MAX_FILE_REASON as u64), + ("file-requests-waiting", MAX_FILE_REQUESTS as u64), + ("file-timeout-ms", FILE_TIMEOUT_MS as u64), ("list-items", MAX_LIST_ITEMS as u64), ("live-notices", MAX_LIVE_NOTICES as u64), ("min-refresh-ms", MIN_REFRESH_MS as u64), + ("min-timer-ms", MIN_TIMER_MS as u64), ("name-chars", MAX_NAME as u64), ("node-key-chars", MAX_NODE_KEY as u64), ("nodes-per-view", MAX_NODES as u64), @@ -264,6 +356,12 @@ pub const LIMITS: &[(&str, u64)] = &[ ("package-bytes", MAX_PACKAGE_BYTES), ("placeholders-per-view", MAX_PLACEHOLDERS as u64), ("reason-chars", MAX_REASON as u64), + ("request-body-bytes", MAX_BODY as u64), + ("request-path-bytes", MAX_PATH as u64), + ("request-timeout-ms", TIMEOUT_MS as u64), + ("requests-in-flight", MAX_IN_FLIGHT as u64), + ("response-body-bytes", MAX_RESPONSE as u64), + ("response-bytes-mode-bytes", MAX_BYTES_RESPONSE as u64), ("round-decimals", MAX_ROUND as u64), ("setting-fields", MAX_FIELDS as u64), ("setting-groups", MAX_GROUPS as u64), @@ -276,6 +374,13 @@ pub const LIMITS: &[(&str, u64)] = &[ ("storage-total-bytes", MAX_TOTAL as u64), ("storage-value-bytes", MAX_VALUE as u64), ("template-chars", MAX_TEMPLATE_LEN as u64), + ("terminal-key-chars", MAX_TERMINAL_KEY as u64), + ("terminal-line-chars", MAX_LINE as u64), + ("terminal-title-chars", MAX_TERMINAL_TITLE as u64), + ("terminal-write-bytes", MAX_WRITE as u64), + ("terminals-open", MAX_TERMINALS as u64), ("text-chars", MAX_TEXT as u64), + ("timer-max-ms", MAX_TIMER_MS as u64), + ("timers-waiting", MAX_TIMERS as u64), ("view-depth", MAX_DEPTH as u64), ]; diff --git a/crates/plugin-api/src/template/check.rs b/crates/plugin-api/src/template/check.rs index 0f14298b..c34bfac8 100644 --- a/crates/plugin-api/src/template/check.rs +++ b/crates/plugin-api/src/template/check.rs @@ -130,6 +130,8 @@ fn source_type(source: &Source, scope: &Scope<'_>) -> Result Some(ValueType::Bool), + // A template never shows a credential: it reads as an unknown setting. + FieldKind::Text { secret: true, .. } => None, FieldKind::Choice { .. } | FieldKind::Text { .. } => Some(ValueType::Text), FieldKind::Number { .. } => Some(ValueType::Number), FieldKind::MultiChoice { .. } => None, diff --git a/crates/plugin-api/src/template/view.rs b/crates/plugin-api/src/template/view.rs index 3bebe397..ecc8762d 100644 --- a/crates/plugin-api/src/template/view.rs +++ b/crates/plugin-api/src/template/view.rs @@ -12,8 +12,8 @@ use thiserror::Error; use super::{Context, MAX_PLACEHOLDERS, Output, Placeholder, Scope, Template, TemplateError}; use crate::{ - LimitError, MAX_BADGE, MAX_BUTTON, MAX_DEPTH, MAX_LIST_ITEMS, MAX_NODE_KEY, MAX_NODES, - MAX_TEXT, MIN_REFRESH_MS, Node, NodeKind, PluginIcon, Refresh, Snapshot, Tone, View, ViewError, + Glyph, LimitError, MAX_BADGE, MAX_BUTTON, MAX_DEPTH, MAX_LIST_ITEMS, MAX_NODE_KEY, MAX_NODES, + MAX_TEXT, MIN_REFRESH_MS, Node, NodeKind, Refresh, Snapshot, Tone, View, ViewError, capability::Capability, capability::notifications::Own, error::key_syntax, sanitize_display, }; @@ -32,6 +32,16 @@ pub enum NodeKindName { List, Divider, Spacer, + /// A titled group: `text` is the title, `detail` an optional count, `folded` how it starts. + Section, + /// A row of a list: `text` is the title, `detail` the muted line, `icon` leads it. + Item, + /// A small button with an `icon`; `text` names its action in a tooltip. Needs a key. + IconButton, + /// A line of small muted `text`. + Caption, + /// A `text` label and its `detail` value on one line. + Field, } /// A node as written in a package. @@ -49,11 +59,18 @@ pub struct NodeDto { /// An expression ending in `threshold(low, high)` that sets the tone. #[serde(default)] pub tone_when: Option, - /// A template, for text, badge and button. + /// A template: the text of a text, badge, button or caption, the title of a section or an + /// item, the label of an icon button or a field. #[serde(default)] pub text: Option, + /// A template: the count of a section, the detail line of an item, the value of a field. #[serde(default)] - pub icon: Option, + pub detail: Option, + /// Whether a section starts folded; a section folds only when it has a key. + #[serde(default)] + pub folded: Option, + #[serde(default)] + pub icon: Option, /// An expression: a number for a meter, a switch value for a toggle. #[serde(default)] pub value: Option, @@ -128,14 +145,35 @@ enum Content { Row, Column, Text(Template), - Icon(PluginIcon), + Icon(Glyph), Meter(Placeholder), Badge(Template), Button(Template), Toggle(Placeholder), - List { each: bool }, + List { + each: bool, + }, Divider, Spacer, + Section { + title: Template, + count: Option