diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 4cdb0b27..cfbdf293 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -13,7 +13,7 @@ concurrency: cancel-in-progress: ${{ github.event_name == 'pull_request' }} jobs: - linux: + conformance: runs-on: ubuntu-latest timeout-minutes: 60 steps: @@ -26,19 +26,89 @@ jobs: "https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-amd64" chmod +x "$RUNNER_TEMP/bazel" echo "$RUNNER_TEMP" >> "$GITHUB_PATH" - - name: Conformance and unit tests + - name: Architecture conformance + run: >- + bazel test //build_system/architecture:conformance_test + --spawn_strategy=local --test_output=errors --cache_test_results=no + + unit: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install Bazelisk + run: | + curl -fsSL -o "$RUNNER_TEMP/bazel" \ + "https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-amd64" + chmod +x "$RUNNER_TEMP/bazel" + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + - name: Unit tests run: >- bazel test - //build_system/architecture:conformance_test + //clients/tui/constants:unit_tests + //clients/tui/app:unit_tests + //clients/tui/cli:unit_tests + //core/crates/terminal/metadata:unit_tests //core/crates/application/upgrade/cli:unit_tests --spawn_strategy=local --test_output=errors --cache_test_results=no + + integration: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install Bazelisk + run: | + curl -fsSL -o "$RUNNER_TEMP/bazel" \ + "https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-amd64" + chmod +x "$RUNNER_TEMP/bazel" + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" + - name: Integration tests + run: >- + bazel test + //core/crates/terminal/metadata:integration_tests + //clients/tui/app:integration_tests + //clients/tui/cli:integration_tests + --spawn_strategy=local --test_output=errors --cache_test_results=no + + client-boundary: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install Bazelisk + run: | + curl -fsSL -o "$RUNNER_TEMP/bazel" \ + "https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-amd64" + chmod +x "$RUNNER_TEMP/bazel" + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" - name: Client boundary run: bazel build --spawn_strategy=local //build_system/architecture:client_boundary + + build-linux: + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v4 + with: + fetch-depth: 0 + - name: Install Bazelisk + run: | + curl -fsSL -o "$RUNNER_TEMP/bazel" \ + "https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-amd64" + chmod +x "$RUNNER_TEMP/bazel" + echo "$RUNNER_TEMP" >> "$GITHUB_PATH" - name: Build YoloShell run: bazel build --spawn_strategy=local -c opt //core:yolo - macos: - runs-on: macos-15 + build-macos: + runs-on: macos-26 timeout-minutes: 90 steps: - uses: actions/checkout@v4 diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 404ad6e7..56b6fa13 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -83,7 +83,7 @@ jobs: build-macos-arm64: needs: verify - runs-on: macos-15 + runs-on: macos-26 steps: - uses: actions/checkout@v4 - name: Require Apple Silicon diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1b2e1fb3..4d19a4b4 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,9 +1,12 @@ stages: + - verify - sync - release workflow: rules: + - if: '$CI_PIPELINE_SOURCE == "merge_request_event"' + - if: '$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH' - if: '$CI_COMMIT_TAG =~ /^v[0-9]/' - if: '$CI_PIPELINE_SOURCE == "schedule"' - if: '$CI_PIPELINE_SOURCE == "web"' @@ -15,12 +18,77 @@ default: variables: GIT_DEPTH: "0" GITHUB_REPOSITORY: "AgentsMesh/YoloShell" + BAZELISK_HOME: "$CI_PROJECT_DIR/.cache/bazelisk" + +# Linux gates run on the internal runner. The macOS build gate remains in +# GitHub Actions; the release jobs below stay tag/schedule/web only. +.bazel: + tags: [linux] + before_script: + - mkdir -p .cache/bin + - | + if ! command -v bazel >/dev/null 2>&1; then + curl -fsSL -o .cache/bin/bazel \ + "https://github.com/bazelbuild/bazelisk/releases/download/v1.25.0/bazelisk-linux-amd64" + chmod +x .cache/bin/bazel + export PATH="$PWD/.cache/bin:$PATH" + fi + +conformance: + extends: .bazel + stage: verify + timeout: 60m + script: + - bazel test //build_system/architecture:conformance_test + --spawn_strategy=local --test_output=errors --cache_test_results=no + +unit: + extends: .bazel + stage: verify + timeout: 60m + script: + - bazel test + //clients/tui/constants:unit_tests + //clients/tui/app:unit_tests + //clients/tui/cli:unit_tests + //core/crates/terminal/metadata:unit_tests + //core/crates/application/upgrade/cli:unit_tests + --spawn_strategy=local --test_output=errors --cache_test_results=no + +integration: + extends: .bazel + stage: verify + timeout: 60m + script: + - bazel test + //core/crates/terminal/metadata:integration_tests + //clients/tui/app:integration_tests + //clients/tui/cli:integration_tests + --spawn_strategy=local --test_output=errors --cache_test_results=no + +client-boundary: + extends: .bazel + stage: verify + timeout: 60m + script: + - bazel build --spawn_strategy=local //build_system/architecture:client_boundary + +build-linux: + extends: .bazel + stage: verify + timeout: 90m + script: + - bazel build --spawn_strategy=local -c opt //core:yolo sync-public: stage: sync tags: [linux] script: - sh scripts/release/sync-public.sh + rules: + - if: '$CI_COMMIT_TAG =~ /^v[0-9]/' + - if: '$CI_PIPELINE_SOURCE == "schedule"' + - if: '$CI_PIPELINE_SOURCE == "web"' trigger-release: stage: release @@ -32,3 +100,7 @@ trigger-release: script: - eval "$(sh scripts/release/variables.sh)" - sh scripts/release/trigger-github.sh "$RELEASE_TAG" "$VERSION" "$RELEASE_NAME" "$PRERELEASE" "$CI_COMMIT_SHA" + rules: + - if: '$CI_COMMIT_TAG =~ /^v[0-9]/' + - if: '$CI_PIPELINE_SOURCE == "schedule"' + - if: '$CI_PIPELINE_SOURCE == "web"' diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d29359e..d2726acd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,29 +3,169 @@ All notable changes to Yolo are recorded here. The project follows semantic versioning once a stable release exists. -## 0.1.2 — Alpha +## 0.5.2 — Alpha + +- Codex conversations can be read and prompted after a verified turn end. +- Agent runs track terminal exits, recovery into a new runtime and archival. Repeated stops preserve the run revision. +- The machine sidebar shows hook setup guidance when empty and opens a running terminal on the selected machine. +- On Linux, upgrade checks inspect the systemd units that own the selected profile and verify their effective image paths. + +## 0.5.1 — Alpha + +### Added +- `yolo hook status|install` manages the hooks Yolo uses to observe terminal + agents. `install` reviews the managed callbacks, asks for confirmation and + preserves user hooks; `--dry-run`, `--yes` and `--json` support automation. +- Codex, Gemini CLI and Grok Build still require trust in the provider's own + settings; Yolo never bypasses hook trust. + +### Changed +- Provider hook installation no longer exposes the internal revision token or + Cap'n Proto output; `yolo internal host hooks` keeps the revision-fenced + commands for automation. + +## 0.5.0 — Alpha + +### Changed +- One root command tree: `yolo --help` lists every command. The shell entry + stays `yolo`, `yolo new`, `yolo attach` and `yolo terminals`; other commands + are grouped by the noun they operate on: `terminal`, `client`, `layout`, + `machine`, `agent`, `server`, `platform`, `conversation`, `workspace`, + `worktree`, `protocol`, `plugin`, `update`, `audit` and `recipe`. + +### Migration +- `yolo list|screen|archive|terminate` become `yolo terminal ...`. +- `yolo clients|detach` become `yolo client list|detach`. +- `yolo layouts` becomes `yolo layout list`. +- `yolo start|stop|restart|status|version` become `yolo server ...`. +- Old root forms are removed; no aliases are kept. + +## 0.4.0 — Alpha + +### Changed +- The TUI sidebar is a machine tree with per-machine tabs and panes (tab state + migrates to schema v6); activating a machine without a terminal starts a + shell there. + +## 0.3.1 — Alpha ### Added +- `Manage machines` has a read-only Consumers page listing the machines + importing from this Hub, with live link count and connection age. + +## 0.3.0 — Alpha + +### Changed +- The inventory lists machines, not Hubs: selecting a machine scopes its agents + and returns to its last terminal. +- `yolo machine add|pin|list|remove|consumers|identity` replaces `hub peer`/ + `hub source`; `--pin-only` replaces `--no-import`. Both machines need 0.3.0 + for `machine identity` discovery. + +## 0.2.3 — Alpha + +### Changed +- `yolo hub peer join ` onboards a peer over the user's existing SSH + client, discovering and pinning its identity and socket. Peer links use an SSH + stdio bridge with no broker profile; the SSH account is the admission boundary + and its share policy controls exports. + +## 0.2.2 — Alpha + +### Fixed +- `yolo update` validates the local state contract before stopping services; + `yolo start` explains an early Hub exit and points to `yolo restart` when the + running Host belongs to an older image. + +## 0.2.1 — Alpha + +### Fixed +- Upgrading from 0.1.x migrates the Hub protocol contract; newer-protocol state + is rejected instead of leaving the local Hub unable to start. + +## 0.2.0 — Alpha + +### Changed +- Hub peer links use SSH instead of a TCP listener and inner SSH server: each + Hub publishes `/peer.sock`; a signed `PeerHello` binds the + configured identities. Pins, imports and exports stay local. +- `yolo hub peer add` uses `--ssh-profile`/`--remote-socket`; `peer list` + reports both. Old configurations keep pins, imports and exports; TCP dials + are dropped. +- Pairing codes, `yolo hub peer invite|join`, `yolo hub listen`, `MintInvite`, + the ledger, the enrollment server and the inner peer SSH transport are + removed; 0.1.x TCP peers need an SSH dial. + +## 0.1.7 — Alpha +### Added +- `yolo hub listen
` configures and applies the local Hub's peer + listener; `yolo hub peer invite --host` then defaults the port from it. + +### Changed +- `yolo update` checks, installs, restarts and probes admission (`--check`, + `--admit`, `--no-restart`); top-level help lists auxiliary commands and + accepts root options before or after the command. +- `yolo workspace update` uses the resolved revision unless + `--expected-revision` conflicts; `yolo worktree create` requires + `--trust-repository`; agent `send`/`ask`/`prompt` help states their + wait and atomicity differences. + +### Fixed +- Invite expiry prints a remaining duration instead of epoch milliseconds. +- Auto-start prints a notice only when no local Hub socket exists; missing + profile files or local Hub state produce actionable errors; `yolo audit` no + longer exits 0 silently. + +## 0.1.6 — Alpha + +### Added +- `yolo status|version|start|stop|restart` expose the local Host/Hub lifecycle; + `yolo update --restart` activates the installed image. + +### Changed +- `yolo stop`/`restart` resolve exact PIDs; upgrade refusal names `yolo restart`. + +## 0.1.5 — Alpha + +### Added +- Hubs pair over SSH with a single-use, expiring `yolo1_` code; the provider's + account and export policy remain the authority. + +## 0.1.4 — Alpha + +### Fixed +- `yolo update` resolves the installer symlink before validating the running + executable, so admission works on the standard macOS installation. + +## 0.1.3 — Alpha + +### Added +- `yolo hub peer add`/`list` manage federation peers with explicit dial + addresses, imports and export policies. + +### Fixed +- A YOLO client inside a YOLO terminal stays read-only, and alternate-screen + snapshots keep the screen mode. + +## 0.1.2 — Alpha + +### Added - `yolo plugin install` replaces an existing link, stops the previous daemon when its manifest declares a `stop` action, and starts the new version. - Reinstalling the same version reuses the verified managed source. ### Fixed - - Codex Stop candidates resolve with the structured turn journal, so a finished turn that awaits the user reports Needs input instead of Running. - A missing plugin runtime names the executable in the startup error. ### Changed - - The Web client keeps a fresh creation in Starting until its first prompt and lists terminal-native waiting agents in Needs you (web.client 0.1.2). ## 0.1.1 — Alpha ### Added - - `yolo plugin install` resolves a versioned spec from the YoloShellPlugins registry, verifies the pinned source commit and manifest, and installs into a private managed directory after the explicit `--trust` preview. @@ -33,7 +173,6 @@ versioning once a stable release exists. ## 0.1.0 — Alpha ### Added - - Persistent TUI terminal tabs with recursive split panes, detach/reattach, shared layouts and private focus. - Per-PTY Workers that own terminal custody independently of the client, Host @@ -42,13 +181,11 @@ versioning once a stable release exists. Grok, with provider-specific capability and authority handling. - Hub federation, remote runtime entry, public JSON-RPC and a trusted plugin interface. -- A Bun web client with live xterm.js terminals, Chat transcript projection, - typed interactions, creation jobs, archive/history and multi-tab writer - leases. -- GitHub release publishing from GitLab tags and a daily nightly channel, - with macOS arm64 plus Linux x86_64 and arm64 assets. -- A one-line installer and `yolo update` release checks, digest-verified - downloads and immutable-image installation. +- A Bun web client with live xterm.js terminals, typed interactions, creation + jobs, archive/history and multi-tab writer leases. +- A one-line installer, `yolo update`, digest-verified downloads and + immutable-image installation; GitHub releases publish macOS arm64 and Linux + x86_64/arm64 assets from GitLab tags. - Bazel architecture gates, Rust coverage gates, private-PTY acceptance and browser end-to-end tests. diff --git a/Cargo.lock b/Cargo.lock index e79a904a..9427d254 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3609,7 +3609,7 @@ dependencies = [ [[package]] name = "yolo-runtime" -version = "0.1.2" +version = "0.5.2" dependencies = [ "anyhow", "base64", diff --git a/Cargo.toml b/Cargo.toml index 0d9acdfd..d1cbe879 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "yolo-runtime" -version = "0.1.2" +version = "0.5.2" edition = "2024" rust-version = "1.93.1" license = "MIT" diff --git a/MODULE.bazel b/MODULE.bazel index 0247ab8c..655106ea 100644 --- a/MODULE.bazel +++ b/MODULE.bazel @@ -1,6 +1,6 @@ module( name = "yolo", - version = "0.1.2", + version = "0.5.2", ) bazel_dep(name = "rules_rust", version = "0.69.0") diff --git a/MODULE.bazel.lock b/MODULE.bazel.lock index 5cf21362..5fbd14a6 100644 --- a/MODULE.bazel.lock +++ b/MODULE.bazel.lock @@ -262,10 +262,10 @@ "@@rules_rust+//crate_universe:extensions.bzl%crate": { "general": { "bzlTransitiveDigest": "J6Ls9yNkaEYC70cPqcvfu/2Ed1zDxtC6n1Ad43au0y8=", - "usagesDigest": "Ovu50ASuI5BXVt9iAvLfHJrVneQRrxhiBFuXv1qblfg=", + "usagesDigest": "Z3P9bkofhfFpOwyWmcgIwLsLBRNtVfjlLgjD/Ky4+kg=", "recordedFileInputs": { - "@@//Cargo.lock": "78704434ccd69c200347283b1f0a88c3ff0811865306d64d00cabfe4ea7e543b", - "@@//Cargo.toml": "5a2aaabf11fe6bf9ddcbc4e0cd92151b5d27de19ec8a3cf86d2281f578b0d099" + "@@//Cargo.lock": "b79fe16029c5c62d82e63c2bfa7cf2af6c2c40022ee8b29f96184129f096c63b", + "@@//Cargo.toml": "f6287e31d6b09e85d52f230d710c078614e46031a56e80040e38a7ed156a0858" }, "recordedDirentsInputs": {}, "envVariables": { diff --git a/README.md b/README.md index ffbdcbe4..e6866ec5 100644 --- a/README.md +++ b/README.md @@ -78,12 +78,12 @@ inside it just as you would in any terminal: Ctrl-B c new shell tab Ctrl-B v split right Ctrl-B n next tab -Ctrl-B s Agent runs / Hubs sidebar +Ctrl-B s machine tree sidebar Ctrl-B m switch an agent pane between Terminal and Chat Ctrl-B d detach; shells and agents keep running ``` -Reconnect with `yolo attach`, list terminals with `yolo list`, or open the +Reconnect with `yolo attach`, list terminals with `yolo terminal list`, or open the terminal picker with `yolo terminals`. ## Install @@ -95,12 +95,13 @@ curl -fsSL https://raw.githubusercontent.com/AgentsMesh/YoloShell/main/scripts/i Update an installed binary from GitHub Releases: ```bash -yolo update --check-release -yolo update --install-release -yolo update --install-release --channel nightly +yolo update # install and activate the latest release +yolo update --check # show the latest release without changing anything +yolo update --channel nightly # select the nightly channel ``` -`yolo update` without release flags checks live service admission. See +`yolo update` installs a newer image, restarts the local Host and Hub when +needed, then probes live service admission. See [operations](docs/operations.md) for the restart boundary. ## The Web Client diff --git a/build_system/architecture/checks/entrypoint/src/constants.rs b/build_system/architecture/checks/entrypoint/src/constants.rs index 53a18241..61268f1d 100644 --- a/build_system/architecture/checks/entrypoint/src/constants.rs +++ b/build_system/architecture/checks/entrypoint/src/constants.rs @@ -36,6 +36,7 @@ pub const ALLOWED_DEPENDENCIES: &[&str] = &[ "//core/crates/application/launch/contract:yolo_launch_contract", "//core/crates/application/host/cli:yolo_host_cli", "//core/crates/application/attach/cli:yolo_attach_cli", + "//core/crates/application/command:yolo_command_cli", "//core/crates/application/remote/cli:yolo_remote_cli", "//core/crates/application/upgrade/cli:yolo_upgrade_cli", "//core/crates/application/workspace/cli:yolo_workspace_cli", diff --git a/build_system/architecture/checks/repository/rules/src/communication_json_constants.rs b/build_system/architecture/checks/repository/rules/src/communication_json_constants.rs index 9b85f7fd..da9a2527 100644 --- a/build_system/architecture/checks/repository/rules/src/communication_json_constants.rs +++ b/build_system/architecture/checks/repository/rules/src/communication_json_constants.rs @@ -29,6 +29,7 @@ pub(super) const PUBLIC_DOCUMENT_FILES: &[&str] = &[ "core/crates/application/trusted-plugin/cli/src/execution.rs", "core/crates/application/trusted-plugin/cli/src/install.rs", "core/crates/application/trusted-plugin/cli/src/install_registry.rs", + "clients/tui/hub/cli/src/output.rs", // Public CLI documents, not private IPC. "core/crates/application/protocol/cli/src/output.rs", ]; diff --git a/build_system/architecture/hub_roles.bzl b/build_system/architecture/hub_roles.bzl index e320c6a7..1c815c85 100644 --- a/build_system/architecture/hub_roles.bzl +++ b/build_system/architecture/hub_roles.bzl @@ -164,15 +164,15 @@ RUNTIME_COMPONENTS = [ "//core/crates/hub/file/helper/protocol:yolo_hub_file_helper_protocol", "//core/crates/hub/file/reader:yolo_hub_file_reader", "//core/crates/hub/file/service:yolo_hub_file_service", - "//core/crates/hub/listener/binding:yolo_hub_listener_binding", YOLO_HUB_LOCAL_BACKEND, YOLO_HUB_LOCAL_PLATFORM, YOLO_HUB_LOCAL_STARTUP, YOLO_HUB_MANAGEMENT_RUNTIME, YOLO_HUB_OBSERVATION_STORE, "//core/crates/hub/peer/dialer:yolo_hub_peer_dialer", + "//core/crates/hub/peer/io:yolo_hub_peer_io", + "//core/crates/hub/peer/link:yolo_hub_peer_link", "//core/crates/hub/peer/rpc:rpc", - "//core/crates/hub/peer/transport:yolo_hub_peer_transport", YOLO_HUB_PUBLIC_CLIENT_SERVER, "//core/crates/hub/publication:yolo_hub_publication", "//core/crates/hub/relay/native/bridge:native", diff --git a/build_system/bazel/rust/communication_tests.bzl b/build_system/bazel/rust/communication_tests.bzl index aeb539dc..71687763 100644 --- a/build_system/bazel/rust/communication_tests.bzl +++ b/build_system/bazel/rust/communication_tests.bzl @@ -12,7 +12,6 @@ COMMUNICATION_TESTS = [ "//core/crates/hub/build/protocol:unit_tests", "//core/crates/hub/remote/startup:unit_tests", "//core/crates/hub/remote/startup:integration_tests", - "//core/crates/hub/peer/transport:unit_tests", "//core/crates/hub/local/startup:integration_tests", "//core/crates/hub/identity:unit_tests", "//core/crates/hub/connector/protocol:unit_tests", @@ -46,7 +45,6 @@ COMMUNICATION_TESTS = [ "//core/crates/hub/management/runtime:unit_tests", "//core/crates/hub/management/runtime:integration_tests", "//core/crates/hub/peer/protocol:unit_tests", - "//core/crates/hub/peer/transport:integration_tests", "//core/crates/hub/publication:unit_tests", "//core/crates/hub/agent/protocol:yolo_hub_agent_protocol_unit_tests", "//core/crates/hub/agent/protocol:yolo_hub_agent_protocol_integration_tests", diff --git a/build_system/bazel/rust/hub_delivery_tests.bzl b/build_system/bazel/rust/hub_delivery_tests.bzl index 678812c9..964c60ad 100644 --- a/build_system/bazel/rust/hub_delivery_tests.bzl +++ b/build_system/bazel/rust/hub_delivery_tests.bzl @@ -18,8 +18,6 @@ HUB_DELIVERY_TESTS = [ "//core/crates/hub/ssh/entry:integration_tests", "//core/crates/hub/observation/store:unit_tests", "//core/crates/hub/observation/store:integration_tests", - "//core/crates/hub/listener/binding:unit_tests", - "//core/crates/hub/listener/binding:integration_tests", "//core/crates/hub/broker/launcher:unit_tests", "//core/crates/hub/remote/setup/store:unit_tests", "//core/crates/hub/remote/setup/store:integration_tests", @@ -37,7 +35,8 @@ HUB_DELIVERY_TESTS = [ "//core/crates/hub/enrollment/store:unit_tests", "//core/crates/hub/enrollment/store:integration_tests", "//core/crates/hub/peer/dialer:unit_tests", - "//core/crates/hub/peer/dialer:integration_tests", + "//core/crates/hub/peer/io:unit_tests", + "//core/crates/hub/peer/link:unit_tests", "//core/crates/hub/ssh/profile/protocol:unit_tests", "//core/crates/hub/ssh/profile/protocol:integration_tests", "//core/crates/hub/ssh/profiles:unit_tests", diff --git a/build_system/bazel/rust/runtime_hub_integration.bzl b/build_system/bazel/rust/runtime_hub_integration.bzl index 96987477..8e8c7d9a 100644 --- a/build_system/bazel/rust/runtime_hub_integration.bzl +++ b/build_system/bazel/rust/runtime_hub_integration.bzl @@ -42,6 +42,7 @@ HUB_INTEGRATION_DEPS = [ "//clients/tui/local/startup:yolo_tui_local_startup", "@crates//:tracing-subscriber", "//core/crates/hub/observation/store:yolo_hub_observation_store", + "//core/crates/hub/peer/dialer:yolo_hub_peer_dialer", "//core/crates/hub/peer/protocol:yolo_hub_peer_protocol", "//core/crates/hub/remote/setup/protocol:yolo_hub_remote_setup_protocol", YOLO_REMOTE_PROVISIONING_PROTOCOL, diff --git a/build_system/bazel/version.bzl b/build_system/bazel/version.bzl index aa7167bc..411cfcad 100644 --- a/build_system/bazel/version.bzl +++ b/build_system/bazel/version.bzl @@ -1 +1 @@ -YOLO_MARKETING_VERSION = "0.1.2" +YOLO_MARKETING_VERSION = "0.5.2" diff --git a/build_system/release/contracts/src/snapshot.rs b/build_system/release/contracts/src/snapshot.rs index 075699b0..580cf6ca 100644 --- a/build_system/release/contracts/src/snapshot.rs +++ b/build_system/release/contracts/src/snapshot.rs @@ -65,7 +65,7 @@ pub const ARCHIVE_POLICY_RELATIVE_PATH: &str = "plugins/macos/Tests/E2E/bundle_a pub const ICON_RELATIVE_PATH: &str = "plugins/macos/Resources/Assets.xcassets/AppIcon.appiconset/icon_512x512@2x.png"; pub const BASH_EXECUTABLE: &str = "/bin/bash"; -pub const YOLO_MARKETING_VERSION: &str = "0.1.2"; +pub const YOLO_MARKETING_VERSION: &str = "0.5.2"; pub const YOLO_VERSION_OUTPUT_PREFIX: &str = "yolo"; pub fn yolo_version_output() -> String { diff --git a/clients/tui/BUILD.bazel b/clients/tui/BUILD.bazel index 96a0e061..085281ef 100644 --- a/clients/tui/BUILD.bazel +++ b/clients/tui/BUILD.bazel @@ -13,6 +13,7 @@ filegroup( "//clients/tui/input:architecture_check_data", "//clients/tui/input/line:architecture_check_data", "//clients/tui/hub/manager:architecture_check_data", + "//clients/tui/hub/cli:architecture_check_data", "//clients/tui/constants:architecture_check_data", "//clients/tui/descriptor:architecture_check_data", "//clients/tui/document:architecture_check_data", @@ -53,6 +54,7 @@ test_suite( "//clients/tui/input/line:unit_tests", "//clients/tui/input/line:integration_tests", "//clients/tui/hub/manager:unit_tests", + "//clients/tui/hub/cli:unit_tests", "//clients/tui/inventory:unit_tests", "//clients/tui/inventory:integration_tests", "//core/crates/presentation/layout:unit_tests", diff --git a/clients/tui/app/BUILD.bazel b/clients/tui/app/BUILD.bazel index 4e6a8ff9..f2da3240 100644 --- a/clients/tui/app/BUILD.bazel +++ b/clients/tui/app/BUILD.bazel @@ -16,7 +16,6 @@ _DEPS = [ "//core/crates/hub/client:yolo_hub_client", "//core/crates/hub/identity:yolo_hub_identity", "//core/crates/hub/management/client:yolo_hub_management_client", - "//core/crates/hub/management/protocol:yolo_hub_management_protocol", "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", "//core/crates/hub/terminal/protocol:yolo_hub_terminal_protocol", "//core/crates/hub/workspace/protocol:yolo_hub_workspace_protocol", diff --git a/clients/tui/app/integration_tests/layout_protocol.rs b/clients/tui/app/integration_tests/layout_protocol.rs index cf775534..73161c0c 100644 --- a/clients/tui/app/integration_tests/layout_protocol.rs +++ b/clients/tui/app/integration_tests/layout_protocol.rs @@ -94,7 +94,7 @@ async fn new_client_publishes_layout_then_another_client_attaches_via_public_pro }; open(&options, &mut store, &mut other).await.unwrap(); let published = server.await.unwrap(); - assert_eq!(other.tab_snapshot().shared_tabs().unwrap(), published.tabs); + assert_eq!(other.tab_snapshot().shared_tabs(other.entry_hub).unwrap(), published.tabs); assert!(other.terminals.is_empty()); assert!(other.writers.is_empty()); } diff --git a/clients/tui/app/src/control/reconcile.rs b/clients/tui/app/src/control/reconcile.rs index f0790b5f..d8f66a06 100644 --- a/clients/tui/app/src/control/reconcile.rs +++ b/clients/tui/app/src/control/reconcile.rs @@ -87,6 +87,7 @@ pub(crate) fn dispatch( if !state.client_capability_enabled(ClientCapability::TerminalControl) { return; } + if crate::constants::is_self_terminal(target.address.terminal_id.as_uuid()) { return; } let Some(generation) = state.control_generation else { return; }; diff --git a/clients/tui/app/src/creation/creation_confirmation.rs b/clients/tui/app/src/creation/creation_confirmation.rs index 09b99224..94e3d335 100644 --- a/clients/tui/app/src/creation/creation_confirmation.rs +++ b/clients/tui/app/src/creation/creation_confirmation.rs @@ -92,7 +92,7 @@ fn confirm( bound && present && crate::initial_control::arm(state, &pending, creation.address, now); if bound && present { state.selected = Some(creation.address); - state.inventory.view = yolo_tui_inventory::View::Agents; + state.inventory.view = yolo_tui_inventory::View::Tree; state.filter.clear(); } if initial { diff --git a/clients/tui/app/src/creation/creation_presentation.rs b/clients/tui/app/src/creation/creation_presentation.rs index 3d23f1dc..cfe8949d 100644 --- a/clients/tui/app/src/creation/creation_presentation.rs +++ b/clients/tui/app/src/creation/creation_presentation.rs @@ -5,7 +5,7 @@ use yolo_tui_inventory::View; #[derive(Clone)] pub(crate) struct CreationPresentation { filter: String, - scope: Option, + machine: HubId, view: View, mode: Mode, } @@ -14,7 +14,7 @@ impl CreationPresentation { pub fn capture(state: &State) -> Self { Self { filter: state.filter.clone(), - scope: state.inventory.scope, + machine: state.tabs.machine(), view: state.inventory.view, mode: if state.mode == Mode::Directory { Mode::Inventory @@ -26,7 +26,7 @@ impl CreationPresentation { pub fn matches(&self, state: &State) -> bool { state.filter == self.filter - && state.inventory.scope == self.scope + && state.tabs.machine() == self.machine && state.inventory.view == self.view && state.mode == self.mode && state.tab_manager.is_none() @@ -36,7 +36,7 @@ impl CreationPresentation { pub fn restore_directory(self, state: &mut State) { state.filter = self.filter; - state.inventory.scope = self.scope; + state.inventory.selected_machine = Some(self.machine); state.inventory.view = self.view; state.mode = Mode::Directory; } diff --git a/clients/tui/app/src/help/help_constants.rs b/clients/tui/app/src/help/help_constants.rs index c84b9ca7..d595836a 100644 --- a/clients/tui/app/src/help/help_constants.rs +++ b/clients/tui/app/src/help/help_constants.rs @@ -23,7 +23,7 @@ pub(crate) const HELP_TEXT: &str = concat!( "Ctrl-b o/arrows Focus another pane\n", "Ctrl-b H/J/K/L Resize the focused split; Ctrl-b = equalizes panes\n", "Ctrl-b z Zoom the focused pane; Ctrl-b { / } swaps panes\n", - "Ctrl-b s Focus Agent runs / Hubs sidebar; Enter opens selection\n", + "Ctrl-b s Focus the machine/agent tree; Enter opens the selection\n", "Terminal mode: arrows and Tab go to the PTY; use Ctrl-b s before sidebar navigation\n", "Ctrl-b b Show / hide sidebar\n", "Ctrl-b m Toggle agent Chat; Enter appends into its terminal input and submits\n", @@ -38,9 +38,9 @@ pub(crate) const HELP_TEXT: &str = concat!( "Ctrl-b ? / F1 Show help; Esc, q or ? closes it\n", "Ctrl-b W Workspace filter; c creates, e renames, l edits labels\n\n", "Sidebar: c new tab · n directory · / filter · I details · i interrupt · 1-9 answer · h history\n", - "Hubs view: m opens Hub configuration.\n", + "Tree: Enter opens a machine workspace; Left/Right or Space collapses/expands; m manages machines.\n", "Tab/Agent badges: ✎ writable · ◇ read-only · ‖ paused · … syncing; spinner = working.\n", - "Hub rows: ● reachable · ○ unreachable · ? stale or unknown.\n", + "Machine rows: ● reachable · ○ unreachable · ? stale or unknown.\n", "Wheel: local history; full-screen apps page; Codex transcript scrolls 3 lines (Esc returns).\n", "Agent exit returns to its shell. Shell exit ends its PTY; Ctrl-b c opens a new tab.\n", "Ordinary q and ? remain shell input." diff --git a/clients/tui/app/src/initial_control/initial_control_context.rs b/clients/tui/app/src/initial_control/initial_control_context.rs index aa16fee5..f78d76aa 100644 --- a/clients/tui/app/src/initial_control/initial_control_context.rs +++ b/clients/tui/app/src/initial_control/initial_control_context.rs @@ -15,6 +15,7 @@ pub(super) fn current(state: &State, intent: &Intent) -> bool { .panes() .iter() .any(|pane| pane.id == intent.pane && pane.terminal == Some(intent.address)) + && !crate::constants::is_self_terminal(intent.address.terminal_id.as_uuid()) } pub(crate) fn valid_target(state: &State, intent: &Intent, target: RuntimeTarget) -> bool { diff --git a/clients/tui/app/src/input/actions.rs b/clients/tui/app/src/input/actions.rs index 4ac3ebb3..c79493e0 100644 --- a/clients/tui/app/src/input/actions.rs +++ b/clients/tui/app/src/input/actions.rs @@ -158,7 +158,7 @@ pub fn handle(state: &mut State, jobs: &mpsc::Sender, hub: HubId, event: Ev return false; } if matches!(state.mode, Mode::Search | Mode::Directory) { - entry(state, jobs, hub, key.code); + entry(state, jobs, key.code); return false; } if matches!( @@ -175,17 +175,7 @@ pub fn handle(state: &mut State, jobs: &mpsc::Sender, hub: HubId, event: Ev crate::shell_actions::current_directory(state, jobs, hub); return false; } - if key.code == KeyCode::Char('n') && key.modifiers.is_empty() { - // The inventory handler owns the transition into the directory form; - // this shortcut only prefills that form with the destination cwd. - let origin = crate::shell_actions::destination(state, hub); - let _ = crate::inventory_actions::handle(state, jobs, key.code); - if state.mode == Mode::Directory { - state.entry = crate::shell_actions::directory(state, origin); - } - return false; - } - if state.inventory.view == yolo_tui_inventory::View::Agents + if state.inventory.view == yolo_tui_inventory::View::Tree && key.code == KeyCode::Char(crate::interrupt_constants::KEY) && key.modifiers.is_empty() { diff --git a/clients/tui/app/src/input/form_actions.rs b/clients/tui/app/src/input/form_actions.rs index b958ec6e..00b6e445 100644 --- a/clients/tui/app/src/input/form_actions.rs +++ b/clients/tui/app/src/input/form_actions.rs @@ -3,16 +3,15 @@ use crossterm::event::KeyCode; use tokio::sync::mpsc; use uuid::Uuid; use yolo_builtin_client_contract::ClientCapability; -use yolo_hub_identity::HubId; use yolo_hub_rpc_protocol::Command; use yolo_hub_terminal_protocol::{TerminalAction, TerminalCommand, TerminalSelection}; -pub(crate) fn entry(state: &mut State, jobs: &mpsc::Sender, hub: HubId, key: KeyCode) { +pub(crate) fn entry(state: &mut State, jobs: &mpsc::Sender, key: KeyCode) { match key { KeyCode::Esc => state.mode = Mode::Inventory, KeyCode::Enter => { if state.mode == Mode::Directory { - let origin = crate::shell_actions::destination(state, hub); + let origin = state.tabs.machine(); if !crate::shell_actions::create(state, jobs, origin, state.entry.clone(), None) { let message = state.status.clone(); crate::form_input::fail(state, message); diff --git a/clients/tui/app/src/interrupt/interrupt_actions.rs b/clients/tui/app/src/interrupt/interrupt_actions.rs index c2e2f852..e515e575 100644 --- a/clients/tui/app/src/interrupt/interrupt_actions.rs +++ b/clients/tui/app/src/interrupt/interrupt_actions.rs @@ -60,7 +60,7 @@ pub(crate) fn start(state: &mut State, jobs: &mpsc::Sender) { fn eligible(state: &State) -> Result<(AgentSnapshot, LeaseGrant, Uuid), &'static str> { if state.mode != Mode::Inventory - || state.inventory.view != View::Agents + || state.inventory.view != View::Tree || state.inventory.details.is_some() { return Err(SELECT_AGENT); @@ -93,7 +93,7 @@ fn eligible(state: &State) -> Result<(AgentSnapshot, LeaseGrant, Uuid), &'static } pub(crate) fn status(state: &State) -> String { - if state.mode != Mode::Inventory || state.inventory.view != View::Agents { + if state.mode != Mode::Inventory || state.inventory.view != View::Tree { return state.status.clone(); } if let Some(entry) = state diff --git a/clients/tui/app/src/layout_sync/completion.rs b/clients/tui/app/src/layout_sync/completion.rs index 6f22f58a..f12a9253 100644 --- a/clients/tui/app/src/layout_sync/completion.rs +++ b/clients/tui/app/src/layout_sync/completion.rs @@ -69,7 +69,7 @@ fn reconcile( } else { let dirty = state .tab_snapshot() - .shared_tabs() + .shared_tabs(state.entry_hub) .map_err(|error| error.to_string())? != sync.projected; sync.projected = adopt(state, &document).map_err(|error| error.to_string())?; diff --git a/clients/tui/app/src/layout_sync/mod.rs b/clients/tui/app/src/layout_sync/mod.rs index dacbcfb5..ea46b5f8 100644 --- a/clients/tui/app/src/layout_sync/mod.rs +++ b/clients/tui/app/src/layout_sync/mod.rs @@ -48,7 +48,7 @@ pub(crate) fn refresh(state: &mut State) { } pub(crate) fn pump(state: &mut State, jobs: &mpsc::Sender) { - let Ok(tabs) = state.tab_snapshot().shared_tabs() else { + let Ok(tabs) = state.tab_snapshot().shared_tabs(state.entry_hub) else { return; }; let Some(sync) = &mut state.shared_layout else { @@ -97,7 +97,7 @@ pub(super) fn adopt( .into_iter() .find(|pane| pane.id == previous_focus) .and_then(|pane| pane.terminal); - let tabs = yolo_tui_tab::TabCatalog::from_shared(document, &previous)?; + let tabs = yolo_tui_tab::TabCatalog::from_shared(document, &previous, state.entry_hub)?; let changed = tabs != previous; // Admission also runs when the remote topology is unchanged: the local // catalog may already hold a binding this client now knows is gone. @@ -125,7 +125,7 @@ pub(super) fn adopt( // For every non-empty document, `projected` is the server view so a pruned // local topology is published in the same round. if document.tabs.is_empty() { - return state.tab_snapshot().shared_tabs(); + return state.tab_snapshot().shared_tabs(state.entry_hub); } Ok(document.tabs.clone()) } diff --git a/clients/tui/app/src/layout_sync/startup.rs b/clients/tui/app/src/layout_sync/startup.rs index c96ba266..10ade110 100644 --- a/clients/tui/app/src/layout_sync/startup.rs +++ b/clients/tui/app/src/layout_sync/startup.rs @@ -26,7 +26,7 @@ pub(crate) async fn open(options: &Options, store: &mut TabStore, state: &mut St address, request_id: Uuid::new_v4(), expected_revision: 0, - tabs: state.tab_snapshot().shared_tabs()?, + tabs: state.tab_snapshot().shared_tabs(state.entry_hub)?, }; let expected = change.apply(None)?; let reply = call(options, Command::UpdateLayout { change }) @@ -62,7 +62,7 @@ pub(crate) fn restore_draft( cached: Option<&yolo_tui_tab::LayoutCheckpoint>, ) -> Result> { if let Some(cached) = cached { - let draft = state.tab_snapshot().shared_tabs()?; + let draft = state.tab_snapshot().shared_tabs(state.entry_hub)?; if draft != cached.projected { if *document == cached.document { return Ok(cached.projected.clone()); diff --git a/clients/tui/app/src/module_wiring.rs b/clients/tui/app/src/module_wiring.rs index 2f55d6fe..afe12494 100644 --- a/clients/tui/app/src/module_wiring.rs +++ b/clients/tui/app/src/module_wiring.rs @@ -44,6 +44,8 @@ mod deferred_input; mod document_view; #[path = "presentation/draw.rs"] mod draw; +#[path = "presentation/draw_content.rs"] +mod draw_content; #[path = "events/events.rs"] mod events; #[path = "finalization/finalization_actions.rs"] @@ -86,6 +88,8 @@ mod interrupt_events; mod interrupt_state; #[path = "pane/inventory_actions.rs"] mod inventory_actions; +#[path = "pane/machine_activation.rs"] +mod machine_activation; #[path = "respond/respond_actions.rs"] mod respond_actions; #[path = "respond/respond_events.rs"] diff --git a/clients/tui/app/src/network/hub_actions.rs b/clients/tui/app/src/network/hub_actions.rs index 388d4b36..e6db6865 100644 --- a/clients/tui/app/src/network/hub_actions.rs +++ b/clients/tui/app/src/network/hub_actions.rs @@ -10,40 +10,25 @@ use tokio::sync::mpsc; use yolo_builtin_client_contract::ClientCapability; use yolo_hub_identity::HubId; use yolo_tui_hub_manager::{Effect, Manager, OPEN_LABEL, Operation}; -use yolo_tui_inventory::View; -const MIN_HEADER_SPACE: u16 = 20; - -pub fn button(state: &State, header: Rect) -> Option { +pub fn draw_footer(frame: &mut Frame<'_>, state: &State, area: Rect) -> Option { let width = u16::try_from(OPEN_LABEL.len()).ok()?; - if state.mode != Mode::Inventory + if area.height == 0 + || area.width < width || !state.client_capability_enabled(ClientCapability::HubManagement) - || state.inventory.view != View::Hubs - || header.width < width + MIN_HEADER_SPACE - || header.height == 0 { return None; } - Some(Rect::new( - header.right().saturating_sub(width), - header.y, - width, - 1, - )) -} - -/// The button shares the section row with `Agent runs | Hubs`, so Hub -/// configuration does not cost a separate header line. -pub fn draw_button(frame: &mut Frame<'_>, state: &State, area: Rect) -> Option { - let button = button(state, area)?; + let button = Rect::new(area.x, area.y, width, 1); frame.render_widget( Paragraph::new(OPEN_LABEL).style(Style::default().fg(Color::Black).bg(Color::Cyan)), button, ); Some(button) } + pub fn open_event(state: &State, event: &Event) -> bool { - if state.mode != Mode::Inventory || state.inventory.view != View::Hubs { + if !matches!(state.mode, Mode::Inventory | Mode::Terminal) { return false; } match event { @@ -51,6 +36,7 @@ pub fn open_event(state: &State, event: &Event) -> bool { key.kind != KeyEventKind::Release && key.code == KeyCode::Char('m') && key.modifiers.is_empty() + && state.mode == Mode::Inventory } Event::Mouse(mouse) => { mouse.kind == MouseEventKind::Down(MouseButton::Left) diff --git a/clients/tui/app/src/network/management_network.rs b/clients/tui/app/src/network/management_network.rs index 0f1c9eb1..8990e852 100644 --- a/clients/tui/app/src/network/management_network.rs +++ b/clients/tui/app/src/network/management_network.rs @@ -11,7 +11,7 @@ pub async fn run( updates: &mpsc::Sender, ) { while let Some(operation) = requests.recv().await { - let result = yolo_hub_management_client::execute_endpoint( + let result = yolo_hub_management_client::execute_endpoint_reply( &options.endpoint, options.hub, operation.command.clone(), diff --git a/clients/tui/app/src/network/network.rs b/clients/tui/app/src/network/network.rs index a09c8441..81382b83 100644 --- a/clients/tui/app/src/network/network.rs +++ b/clients/tui/app/src/network/network.rs @@ -56,10 +56,7 @@ pub enum Update { }, Management { operation: yolo_tui_hub_manager::Operation, - result: Result< - yolo_hub_management_protocol::ConfigurationSnapshot, - yolo_hub_management_client::ManagementError, - >, + result: Result, }, ManagementNotSent { token: uuid::Uuid, diff --git a/clients/tui/app/src/pane/inventory_actions.rs b/clients/tui/app/src/pane/inventory_actions.rs index cedbb911..d9b60101 100644 --- a/clients/tui/app/src/pane/inventory_actions.rs +++ b/clients/tui/app/src/pane/inventory_actions.rs @@ -20,11 +20,7 @@ pub fn handle(state: &mut State, jobs: &mpsc::Sender, key: KeyCode) -> bool return false; } KeyCode::Char(c @ '1'..='9') => { - crate::respond_actions::choose( - state, - jobs, - usize::from(c as u8 - b'1'), - ); + crate::respond_actions::choose(state, jobs, usize::from(c as u8 - b'1')); return false; } KeyCode::Char('q') | KeyCode::Char(HISTORY_KEY) => {} @@ -35,30 +31,17 @@ pub fn handle(state: &mut State, jobs: &mpsc::Sender, key: KeyCode) -> bool KeyCode::Char('q') => return true, KeyCode::Down | KeyCode::Char('j') => state.move_selection(true), KeyCode::Up | KeyCode::Char('k') => state.move_selection(false), - KeyCode::Tab | KeyCode::BackTab => { - if state.terminal_picker.is_some() { - return false; - } - state.inventory.view = if state.inventory.view == View::Agents { - View::Hubs - } else { - View::Agents - }; - state.reconcile_selection(); - } + KeyCode::Left => collapse_or_parent(state), + KeyCode::Right | KeyCode::Char(' ') => toggle_selected_machine(state), KeyCode::Esc => { if state.terminal_picker.is_some() { crate::tabs::terminal_picker::close(state); return false; } - if state.filter.is_empty() - && state.inventory.scope.is_none() - && !state.inventory.history - { + if state.filter.is_empty() && !state.inventory.history { state.mode = Mode::Terminal; return false; } - state.inventory.scope = None; state.inventory.history = false; state.filter.clear(); state.reconcile_selection(); @@ -76,19 +59,19 @@ pub fn handle(state: &mut State, jobs: &mpsc::Sender, key: KeyCode) -> bool state.mode = Mode::Search; crate::form_input::reset(state); } - KeyCode::Char(crate::tabs::DETAILS_KEY) if state.inventory.view == View::Agents => { - state.inventory.details = state.inventory.selected_agent; + KeyCode::Char(crate::tabs::DETAILS_KEY) => { + if let Some(target) = state.inventory.selected_agent { + state.inventory.details = Some(target); + } } KeyCode::Char('n') => { - if state.inventory.view == View::Hubs { - let Some(hub) = state.inventory.selected_hub else { - state.status = HUB_UNAVAILABLE_LABEL.into(); - return false; - }; - state.inventory.scope = Some(hub); + if state.inventory.view == View::Tree + && let Some(hub) = state.inventory.selected_machine + { + crate::machine_activation::select_machine(state, hub); } state.mode = Mode::Directory; - state.entry = DEFAULT_DIRECTORY.into(); + state.entry = crate::shell_actions::directory(state, state.tabs.machine()); crate::form_input::reset(state); } KeyCode::Char('r') => crate::catalog_events::invalidate(state, jobs), @@ -115,33 +98,55 @@ pub fn handle(state: &mut State, jobs: &mpsc::Sender, key: KeyCode) -> bool } pub(crate) fn open(state: &mut State, jobs: &mpsc::Sender) { - let address = match state.inventory.view { - View::Hubs => { - if let Some(hub) = state.inventory.selected_hub { - state.inventory.scope = Some(hub); - state.inventory.view = View::Agents; - state.filter.clear(); - state.reconcile_selection(); - } - return; - } - View::Agents => { - let Some(target) = state.inventory.terminal_for_selected(&state.terminals) else { - state.inventory.details = state.inventory.selected_agent; - state.status = AGENT_HISTORY_DETAILS_LABEL.into(); - return; - }; - target.address - } + match state.inventory.view { View::Terminals => { let Some(address) = state.selected else { return; }; - address + crate::tabs::terminal_picker::close(state); + crate::tabs::open(state, jobs, address); } - }; - crate::tabs::terminal_picker::close(state); - crate::tabs::open(state, jobs, address); + View::Tree => match state.inventory.selected_tree() { + Some(yolo_tui_inventory::TreeSelection::Machine(hub)) => { + crate::machine_activation::activate(state, jobs, hub); + } + Some(yolo_tui_inventory::TreeSelection::Agent(target)) => { + crate::machine_activation::select_machine_workspace(state, target.address.hub_id); + state.sidebar_visible = true; + state.inventory.select_tree( + yolo_tui_inventory::TreeSelection::Agent(target), + &state.filter, + &state.terminals, + ); + match state.inventory.terminal_for_selected(&state.terminals) { + Some(target) => crate::tabs::open(state, jobs, target.address), + None => { + state.inventory.details = Some(target); + state.status = AGENT_HISTORY_DETAILS_LABEL.into(); + } + } + } + None => {} + }, + } +} + +fn collapse_or_parent(state: &mut State) { + match state.inventory.selected_tree() { + Some(yolo_tui_inventory::TreeSelection::Machine(hub)) => { + state.inventory.toggle_machine(hub) + } + Some(yolo_tui_inventory::TreeSelection::Agent(target)) => { + crate::machine_activation::select_machine(state, target.address.hub_id) + } + None => {} + } +} + +fn toggle_selected_machine(state: &mut State) { + if let Some(yolo_tui_inventory::TreeSelection::Machine(hub)) = state.inventory.selected_tree() { + state.inventory.toggle_machine(hub); + } } fn view_agent_terminal(state: &mut State) { diff --git a/clients/tui/app/src/pane/machine_activation.rs b/clients/tui/app/src/pane/machine_activation.rs new file mode 100644 index 00000000..0f9044da --- /dev/null +++ b/clients/tui/app/src/pane/machine_activation.rs @@ -0,0 +1,89 @@ +use crate::{Mode, State, network::Job}; +use tokio::sync::mpsc; +use yolo_builtin_client_contract::ClientCapability; +use yolo_hub_identity::{HubId, TerminalAddress}; +/// Select a machine's workspace, creating its first local tab if needed. +pub(crate) fn select_machine_workspace(state: &mut State, hub: HubId) { + if state.tabs.machine() != hub { + let mut tabs = state.tab_snapshot(); + if tabs.select_machine(hub).unwrap_or(false) { + state.replace_tabs(tabs); + state.activate_tab_collection(); + state.selected = state.focused_terminal(); + } + } +} +pub(crate) fn select_machine(state: &mut State, hub: HubId) { + select_machine_workspace(state, hub); + state.inventory.select_tree( + yolo_tui_inventory::TreeSelection::Machine(hub), + &state.filter, + &state.terminals, + ); +} +/// Enter a machine workspace: focus a live terminal, or start one shell there. +pub(crate) fn activate(state: &mut State, jobs: &mpsc::Sender, hub: HubId) { + select_machine(state, hub); + state.sidebar_visible = true; + state.inventory.view = yolo_tui_inventory::View::Tree; + state.mode = Mode::Terminal; + state.filter.clear(); + if let Some(address) = live_terminal(state, hub) { + crate::tabs::open(state, jobs, address); + return; + } + if creation_pending(state, hub) { + state.status = crate::startup_constants::STARTING_SHELL.into(); + return; + } + if !machine_reachable(state, hub) { + state.status = crate::constants::MACHINE_UNREACHABLE_LABEL.into(); + return; + } + if !state.client_capability_enabled(ClientCapability::TerminalLifecycle) { + state.status = crate::client_capabilities::TERMINAL_LIFECYCLE_UNAVAILABLE.into(); + return; + } + let cwd = crate::shell_actions::directory(state, hub); + if !crate::shell_actions::create(state, jobs, hub, cwd, None) { + state.mode = Mode::Terminal; + } +} +fn live_terminal(state: &State, hub: HubId) -> Option { + use yolo_tui_lifecycle::TerminalPhase; + state + .focused_terminal() + .into_iter() + .chain(state.inventory.last_terminal_for(hub)) + .chain(state.terminals.keys().copied()) + .filter(|address| { + address.hub_id == hub && !crate::terminal_lifecycle::gone(state, *address) + }) + .min_by_key( + |address| match crate::terminal_lifecycle::phase(state, *address) { + TerminalPhase::Running => 0, + TerminalPhase::Starting | TerminalPhase::OutputBlocked => 1, + _ => 2, + }, + ) +} +fn creation_pending(state: &State, hub: HubId) -> bool { + state + .creations + .pending + .values() + .any(|pending| pending.origin == hub && pending.binding_active) + || state + .creations + .finalizing + .values() + .any(|finalizing| finalizing.record.origin == hub) +} +fn machine_reachable(state: &State, hub: HubId) -> bool { + state.inventory.current + && state + .inventory + .machines + .get(&hub) + .is_none_or(|summary| summary.reachable) +} diff --git a/clients/tui/app/src/pane/picker_header.rs b/clients/tui/app/src/pane/picker_header.rs index 1e2c0f1d..66c49663 100644 --- a/clients/tui/app/src/pane/picker_header.rs +++ b/clients/tui/app/src/pane/picker_header.rs @@ -6,9 +6,9 @@ use ratatui::{ widgets::Paragraph, }; -/// Healthy inventory views show no redundant title line: the section tabs -/// already name the view and each row carries its own identity/status. Keep a -/// single line for the two states a user must not miss. +/// Healthy inventory views show no redundant title line: machine roots and +/// agent rows carry their own identity/status. Keep a single line for the two +/// states a user must not miss. pub(crate) fn height(state: &State) -> u16 { u16::from(status(state).is_some()) } diff --git a/clients/tui/app/src/pointer/inventory_pointer.rs b/clients/tui/app/src/pointer/inventory_pointer.rs index 4dad3a8c..42fc50ab 100644 --- a/clients/tui/app/src/pointer/inventory_pointer.rs +++ b/clients/tui/app/src/pointer/inventory_pointer.rs @@ -35,14 +35,6 @@ pub(crate) fn handle(state: &mut State, jobs: &mpsc::Sender, event: MouseEv } let (selection, clicked) = match event.kind { MouseEventKind::Down(MouseButton::Left) => match viewport.hit(event.column, event.row) { - Some(InventoryHit::Tab(view)) => { - state.mode = Mode::Inventory; - state.inventory.view = view; - state.inventory.details = None; - state.reconcile_selection(); - state.status = SELECTED_HINT.into(); - return true; - } Some(InventoryHit::Row(selection)) => (Some(selection), true), None => (None, false), }, @@ -64,7 +56,7 @@ pub(crate) fn handle(state: &mut State, jobs: &mpsc::Sender, event: MouseEv ) { state.inventory.details = None; state.status = SELECTED_HINT.into(); - if clicked && matches!(selection, InventorySelection::Agent(_)) { + if clicked && matches!(selection, InventorySelection::Agent(_) | InventorySelection::Machine(_)) { crate::inventory_actions::open(state, jobs); } } diff --git a/clients/tui/app/src/presentation/draw.rs b/clients/tui/app/src/presentation/draw.rs index 1fcb5c64..5da38d4f 100644 --- a/clients/tui/app/src/presentation/draw.rs +++ b/clients/tui/app/src/presentation/draw.rs @@ -1,12 +1,9 @@ use crate::presentation::Presentation; -use crate::{Mode, State, constants::*}; +use crate::{Mode, State}; use ratatui::{ Frame, layout::{Constraint, Layout, Rect}, }; -use std::collections::BTreeMap; -use yolo_tui_render::{TerminalPane, render_terminal}; - pub fn draw(frame: &mut Frame<'_>, state: &State) -> Presentation { if state.help { let help = Some(crate::help::draw(frame, state.help_offset)); @@ -35,55 +32,45 @@ pub fn draw(frame: &mut Frame<'_>, state: &State) -> Presentation { }; } let rows = Layout::vertical([ - Constraint::Length(crate::tabs::TAB_BAR_ROWS), Constraint::Min(0), Constraint::Length(crate::form_input::rows(state, frame.area().width)), ]) .split(frame.area()); - let tab_strip = crate::tabs::strip::draw(frame, state, rows[0]); - let tab_ids = state.tabs.entries().iter().map(|entry| entry.id).collect(); let header_rows = crate::picker_header::height(state); let mut inventory_view = None; let mut hub_button = None; - let bounds = if state.terminal_picker.is_some() && !state.document.is_visible(&state.tabs) { + if state.terminal_picker.is_some() && !state.document.is_visible(&state.tabs) { + let body = + Layout::vertical([Constraint::Length(crate::tabs::TAB_BAR_ROWS), Constraint::Min(0)]) + .split(rows[0]); + let tab_strip = crate::tabs::strip::draw(frame, state, body[0]); let picker = - Layout::vertical([Constraint::Length(header_rows), Constraint::Min(0)]).split(rows[1]); + Layout::vertical([Constraint::Length(header_rows), Constraint::Min(0)]).split(body[1]); if header_rows > 0 { crate::picker_header::draw(frame, state, picker[0]); } inventory_view = Some(inventory(frame, state, picker[1])); - hub_button = inventory_view - .as_ref() - .and_then(|view| crate::hub_actions::draw_button(frame, state, view.area())); - crate::status_bar::draw(frame, state, rows[2], None); + crate::status_bar::draw(frame, state, rows[1], None); return Presentation { - bounds: rows[1], + bounds: body[1], layout: Some(state.tab_layout().clone()), mode: state.mode, hub_button, inventory: inventory_view, tab_strip, - tab_ids, + tab_ids: state.tabs.machine_entries().iter().map(|entry| entry.id).collect(), zoomed: state.pane_view.zoomed, ..Presentation::default() }; - } else if (state.sidebar_visible || state.mode == Mode::Inventory) - // Agent details render inside the content column. Keep the split even - // when no terminal pane is bound, otherwise there is nowhere to draw - // them (for example, a fresh `yolo terminals` layout). - && (state.inventory.details.is_some() - || state - .tab_layout() - .panes() - .iter() - .any(|pane| pane.terminal.is_some())) - && rows[1].width >= crate::tabs::SIDEBAR_COLUMNS + crate::tabs::MIN_CONTENT_COLUMNS - { + } + let show_sidebar = (state.sidebar_visible || state.mode == Mode::Inventory) + && rows[0].width >= crate::tabs::SIDEBAR_COLUMNS + crate::tabs::MIN_CONTENT_COLUMNS; + let columns = if show_sidebar { let columns = Layout::horizontal([ Constraint::Length(crate::tabs::SIDEBAR_COLUMNS), Constraint::Min(0), ]) - .split(rows[1]); + .split(rows[0]); let sidebar_area = ratatui::widgets::Block::default() .borders(ratatui::widgets::Borders::RIGHT) .border_style(ratatui::style::Style::default().fg(ratatui::style::Color::DarkGray)); @@ -96,90 +83,46 @@ pub fn draw(frame: &mut Frame<'_>, state: &State) -> Presentation { .saturating_sub(crate::tabs::SIDEBAR_DIVIDER_COLUMNS), columns[0].height, ); - let sidebar = Layout::vertical([Constraint::Length(header_rows), Constraint::Min(0)]) - .split(sidebar_bounds); + let sidebar = Layout::vertical([ + Constraint::Length(header_rows), + Constraint::Min(0), + Constraint::Length(crate::tabs::FOOTER_ROWS), + ]) + .split(sidebar_bounds); if header_rows > 0 { crate::picker_header::draw(frame, state, sidebar[0]); } inventory_view = Some(inventory(frame, state, sidebar[1])); + hub_button = crate::hub_actions::draw_footer(frame, state, sidebar[2]); columns[1] + } else { + rows[0] + }; + let content = + Layout::vertical([Constraint::Length(crate::tabs::TAB_BAR_ROWS), Constraint::Min(0)]) + .split(columns); + let tab_strip = crate::tabs::strip::draw(frame, state, content[0]); + let tab_ids = state.tabs.machine_entries().iter().map(|entry| entry.id).collect(); + let bounds = if inventory_view.is_some() { + content[1] } else if !matches!(state.mode, Mode::Terminal | Mode::Chat | Mode::Document) && !state.document.is_visible(&state.tabs) { let sidebar = - Layout::vertical([Constraint::Length(header_rows), Constraint::Min(0)]).split(rows[1]); + Layout::vertical([Constraint::Length(header_rows), Constraint::Min(0)]).split(content[1]); if header_rows > 0 { crate::picker_header::draw(frame, state, sidebar[0]); } inventory_view = Some(inventory(frame, state, sidebar[1])); Rect::default() } else { - rows[1] + content[1] }; - if let Some(view) = inventory_view.as_ref() { - hub_button = crate::hub_actions::draw_button(frame, state, view.area()); - } if state.document.is_visible(&state.tabs) { state.document.render(frame, &state.tabs, bounds); } - let desired = state.focused_terminal_size(bounds); - let terminals = state - .terminals - .iter() - .map(|(address, session)| { - ( - *address, - TerminalPane { - screen: crate::selection_actions::display_screen(state, *address) - .or_else(|| state.surfaces.get(address).map(|surface| surface.screen())), - projection: crate::terminal_lifecycle::projection(state, *address, desired), - }, - ) - }) - .collect::>(); - let viewports = if state.document.is_visible(&state.tabs) - || bounds.is_empty() - || (!matches!(state.mode, Mode::Terminal | Mode::Chat) - && yolo_tui_inventory::render_details( - frame, - &state.inventory, - &state.terminals, - state - .inventory - .details - .and_then(|address| state.inventory.agents.get(&address)) - .map(|agent| agent.fields().terminal.address) - .map(|address| crate::control::detail(state, address)) - .as_deref(), - bounds, - )) { - Vec::new() - } else { - render_terminal( - frame, - &state.display_layout(), - &terminals, - bounds, - (state.mode == Mode::Chat).then_some(state.tab_layout().focused()), - ) - .unwrap_or_default() - }; - if state.mode == Mode::Chat { - if let Some(chat) = crate::chat::current(state) { - if let Some(view) = viewports - .iter() - .find(|view| view.pane == state.tab_layout().focused()) - { - yolo_tui_chat::render(frame, chat, view.inner, crate::chat::input_readiness(state)); - } - } - } - for view in &viewports { - if let Some(selection) = crate::selection_actions::highlight(state, *view) { - yolo_tui_render::highlight_selection(frame, *view, selection); - } - } - crate::status_bar::draw(frame, state, rows[2], desired); + let (desired, viewports) = crate::draw_content::render(frame, state, bounds); + crate::status_bar::draw(frame, state, rows[1], desired); Presentation { bounds, panes: viewports, @@ -194,5 +137,4 @@ pub fn draw(frame: &mut Frame<'_>, state: &State) -> Presentation { help: None, } } - include!("draw_inventory.rs"); diff --git a/clients/tui/app/src/presentation/draw_content.rs b/clients/tui/app/src/presentation/draw_content.rs new file mode 100644 index 00000000..4a9dfc02 --- /dev/null +++ b/clients/tui/app/src/presentation/draw_content.rs @@ -0,0 +1,70 @@ +use crate::{Mode, State}; +use ratatui::{Frame, layout::Rect}; +use std::collections::BTreeMap; +use yolo_hub_terminal_protocol::TerminalSize; +use yolo_tui_render::{PaneViewport, TerminalPane, render_terminal}; + +pub(crate) fn render( + frame: &mut Frame<'_>, + state: &State, + bounds: Rect, +) -> (Option, Vec) { + let desired = state.focused_terminal_size(bounds); + let terminals = state + .terminals + .iter() + .map(|(address, session)| { + ( + *address, + TerminalPane { + screen: crate::selection_actions::display_screen(state, *address) + .or_else(|| state.surfaces.get(address).map(|surface| surface.screen())), + projection: crate::terminal_lifecycle::projection(state, *address, desired), + }, + ) + }) + .collect::>(); + let viewports = if state.document.is_visible(&state.tabs) + || bounds.is_empty() + || (!matches!(state.mode, Mode::Terminal | Mode::Chat) + && yolo_tui_inventory::render_details( + frame, + &state.inventory, + &state.terminals, + state + .inventory + .details + .and_then(|address| state.inventory.agents.get(&address)) + .map(|agent| agent.fields().terminal.address) + .map(|address| crate::control::detail(state, address)) + .as_deref(), + bounds, + )) { + Vec::new() + } else { + render_terminal( + frame, + &state.display_layout(), + &terminals, + bounds, + (state.mode == Mode::Chat).then_some(state.tab_layout().focused()), + ) + .unwrap_or_default() + }; + if state.mode == Mode::Chat { + if let Some(chat) = crate::chat::current(state) { + if let Some(view) = viewports + .iter() + .find(|view| view.pane == state.tab_layout().focused()) + { + yolo_tui_chat::render(frame, chat, view.inner, crate::chat::input_readiness(state)); + } + } + } + for view in &viewports { + if let Some(selection) = crate::selection_actions::highlight(state, *view) { + yolo_tui_render::highlight_selection(frame, *view, selection); + } + } + (desired, viewports) +} diff --git a/clients/tui/app/src/presentation/draw_inventory.rs b/clients/tui/app/src/presentation/draw_inventory.rs index d0171317..be2b90d8 100644 --- a/clients/tui/app/src/presentation/draw_inventory.rs +++ b/clients/tui/app/src/presentation/draw_inventory.rs @@ -16,6 +16,7 @@ fn inventory( &state.filter, &control_status, animation_phase(), + Some(state.tabs.machine()), area, ) } diff --git a/clients/tui/app/src/presentation/presentation.rs b/clients/tui/app/src/presentation/presentation.rs index faef15ba..685bd0d4 100644 --- a/clients/tui/app/src/presentation/presentation.rs +++ b/clients/tui/app/src/presentation/presentation.rs @@ -13,7 +13,7 @@ pub struct Presentation { pub(crate) help: Option, pub zoomed: bool, pub inventory: Option, - pub(crate) tab_strip: Vec, + pub(crate) tab_strip: Vec, pub(crate) tab_ids: Vec, } impl Default for Presentation { diff --git a/clients/tui/app/src/respond/respond_actions.rs b/clients/tui/app/src/respond/respond_actions.rs index 8f8124fb..e4961c2f 100644 --- a/clients/tui/app/src/respond/respond_actions.rs +++ b/clients/tui/app/src/respond/respond_actions.rs @@ -61,7 +61,7 @@ pub(crate) fn choose(state: &mut State, jobs: &mpsc::Sender, index: usize) fn eligible(state: &mut State, index: usize) -> Option<(AgentSnapshot, LeaseGrant, Uuid, String)> { if state.mode != Mode::Inventory - || state.inventory.view != View::Agents + || state.inventory.view != View::Tree || state.inventory.details.is_none() { state.status = OPEN_DETAILS.into(); diff --git a/clients/tui/app/src/state.rs b/clients/tui/app/src/state.rs index 2d7cb6ac..d387e645 100644 --- a/clients/tui/app/src/state.rs +++ b/clients/tui/app/src/state.rs @@ -2,7 +2,7 @@ use crate::constants::*; use std::{collections::BTreeMap, time::Instant}; use uuid::Uuid; use yolo_builtin_client_contract::CapabilityNegotiation; -use yolo_hub_identity::TerminalAddress; +use yolo_hub_identity::{HubId, TerminalAddress}; use yolo_hub_terminal_protocol::{LeaseGrant, TerminalCommand, TerminalSummary, WriterPrincipal}; use yolo_presentation_layout::Layout; use yolo_tui_inventory::Inventory; @@ -20,7 +20,6 @@ pub enum Mode { ConfirmRecreate, HubConfiguration, } - pub struct Writer { pub grant: LeaseGrant, pub expires: Instant, @@ -38,6 +37,7 @@ pub struct State { /// under one aggregate prevents a stale UI projection from diverging from /// the persisted catalog. pub(crate) tabs: yolo_tui_tab::TabCatalog, + pub(crate) entry_hub: HubId, pub(crate) tab_revision: u64, pub(crate) tab_manager: Option, pub(crate) shared_layout: Option, @@ -88,17 +88,15 @@ pub struct State { pub(crate) hub_manager: Option, } impl State { - /// Whether the control channel has completed its Hello handshake. This is - /// independent of the status line, which also carries transient hints and - /// errors and therefore cannot gate lifecycle recovery UI. + /// Whether the control channel completed its Hello handshake. pub(crate) fn connection_ready(&self) -> bool { self.client_capabilities.is_some() && self.client_startup_failure.is_none() } } - impl Default for State { fn default() -> Self { - let tabs = yolo_tui_tab::TabCatalog::default(); + let entry_hub = HubId::new(); + let tabs = yolo_tui_tab::TabCatalog::new(entry_hub, yolo_tui_tab::LocalTabView::default()); Self { terminals: BTreeMap::new(), selected: None, @@ -107,6 +105,7 @@ impl Default for State { form_input: Default::default(), mode: Mode::Inventory, tabs, + entry_hub, tab_revision: 0, tab_manager: None, shared_layout: None, diff --git a/clients/tui/app/src/status/status_bar_labels.rs b/clients/tui/app/src/status/status_bar_labels.rs index 66d302bb..a29c9b23 100644 --- a/clients/tui/app/src/status/status_bar_labels.rs +++ b/clients/tui/app/src/status/status_bar_labels.rs @@ -1,6 +1,6 @@ fn selected_title(state: &State) -> Option<&str> { match state.inventory.view { - yolo_tui_inventory::View::Agents => state + yolo_tui_inventory::View::Tree => state .inventory .details .or(state.inventory.selected_agent) @@ -10,7 +10,6 @@ fn selected_title(state: &State) -> Option<&str> { .selected .and_then(|address| state.terminals.get(&address)) .map(|terminal| terminal.display_title()), - yolo_tui_inventory::View::Hubs => None, } } diff --git a/clients/tui/app/src/tab/tab_persistence.rs b/clients/tui/app/src/tab/tab_persistence.rs index 0ea0eb9b..c912c4f3 100644 --- a/clients/tui/app/src/tab/tab_persistence.rs +++ b/clients/tui/app/src/tab/tab_persistence.rs @@ -17,6 +17,7 @@ pub fn open(options: &Options) -> Result { pub fn restore(store: &TabStore) -> State { let tab = &store.document().tabs.active().view; let mut state = State::default(); + state.entry_hub = store.document().entry_hub; state.replace_tabs(store.document().tabs.clone()); state.selected = state.focused_terminal(); let document = state.document.restore(&state.tabs); diff --git a/clients/tui/app/src/tab/tab_state.rs b/clients/tui/app/src/tab/tab_state.rs index 3a5da6cd..a0fac345 100644 --- a/clients/tui/app/src/tab/tab_state.rs +++ b/clients/tui/app/src/tab/tab_state.rs @@ -10,9 +10,9 @@ impl State { let (tabs, admission) = crate::tab_binding::admit(self, tabs); self.tab_revision = self.tab_revision.saturating_add(1); self.tabs = tabs; + self.inventory.remember_workspace_machines(self.tabs.entries().iter().map(|entry| entry.machine).collect()); admission } - pub(crate) fn sync_active_tab_view(&mut self) { let view = if matches!(self.mode, Mode::Terminal | Mode::Chat) { TabViewMode::Terminal @@ -42,7 +42,7 @@ impl State { .filter(|entry| entry.id != self.tabs.active().id) .flat_map(|entry| entry.view.layout.panes()) .chain(self.tab_layout().panes()) - .filter_map(|pane| pane.terminal) + .filter_map(|pane| pane.terminal.filter(|address| !crate::constants::is_self_terminal(address.terminal_id.as_uuid()))) .collect() } diff --git a/clients/tui/app/src/tabs/actions.rs b/clients/tui/app/src/tabs/actions.rs index 39ee97c0..62d1d553 100644 --- a/clients/tui/app/src/tabs/actions.rs +++ b/clients/tui/app/src/tabs/actions.rs @@ -10,7 +10,7 @@ pub(crate) fn sidebar(state: &mut State) { state.sidebar_visible = true; state.mode = Mode::Inventory; if state.inventory.view == View::Terminals { - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; } state.presentation = Default::default(); } diff --git a/clients/tui/app/src/tabs/close.rs b/clients/tui/app/src/tabs/close.rs index 3e84b553..520e2908 100644 --- a/clients/tui/app/src/tabs/close.rs +++ b/clients/tui/app/src/tabs/close.rs @@ -26,7 +26,7 @@ pub(crate) fn tab(state: &mut State, id: Uuid) -> Result<()> { .map(|pane| pane.id) .collect::>(); let active = catalog.active().id == id; - let last = catalog.entries().len() == SINGLE_TAB; + let last = catalog.count_for(catalog.machine()) == SINGLE_TAB; if last { catalog.update_active(LocalTabView::default()); } else { diff --git a/clients/tui/app/src/tabs/creation.rs b/clients/tui/app/src/tabs/creation.rs index 6b2cd492..199b885e 100644 --- a/clients/tui/app/src/tabs/creation.rs +++ b/clients/tui/app/src/tabs/creation.rs @@ -6,8 +6,6 @@ use yolo_presentation_layout::Axis; use yolo_tui_tab::{LocalTabView, MAX_TABS, TabCatalog, TabViewMode}; const SINGLE_PANE: usize = 1; -const FIRST_TAB_NUMBER: usize = 1; -const TAB_NAME: &str = "Tab"; const TAB_LIMIT: &str = "Close a tab before opening another"; #[derive(Clone, Copy)] @@ -95,9 +93,6 @@ fn vacant(state: &State) -> bool { } pub(super) fn tab_name(tabs: &TabCatalog) -> anyhow::Result { - anyhow::ensure!(tabs.entries().len() < MAX_TABS, TAB_LIMIT); - (FIRST_TAB_NUMBER..=MAX_TABS) - .map(|number| format!("{TAB_NAME} {number}")) - .find(|name| tabs.entries().iter().all(|entry| entry.name != *name)) - .ok_or_else(|| anyhow::anyhow!(TAB_LIMIT)) + anyhow::ensure!(tabs.count_for(tabs.machine()) < MAX_TABS, TAB_LIMIT); + tabs.next_name(tabs.machine()) } diff --git a/clients/tui/app/src/tabs/mod.rs b/clients/tui/app/src/tabs/mod.rs index d0cafcc6..d0813cfa 100644 --- a/clients/tui/app/src/tabs/mod.rs +++ b/clients/tui/app/src/tabs/mod.rs @@ -11,6 +11,7 @@ pub(crate) use navigation::{activate, open}; pub(crate) use pointer::click; pub(crate) const TAB_BAR_ROWS: u16 = 1; +pub(crate) const FOOTER_ROWS: u16 = 1; pub(crate) const SIDEBAR_COLUMNS: u16 = 36; pub(crate) const SIDEBAR_DIVIDER_COLUMNS: u16 = 1; pub(crate) const MIN_CONTENT_COLUMNS: u16 = 40; @@ -24,7 +25,6 @@ pub(crate) const SPLIT_DOWN_KEY: char = '-'; pub(crate) const CLOSE_TAB_KEY: char = 'X'; pub(crate) const RESUME_KEY: char = 'R'; pub(crate) const DETAILS_KEY: char = 'I'; -pub(crate) const TERMINAL_PICKER_TITLE: &str = "Terminals"; pub(crate) const LITERAL_PREFIX: u8 = 0x02; /// Chat mode is valid only while a chat entry exists for the focused pane. /// A picker restore, a pane/tab close or a background binding retirement can diff --git a/clients/tui/app/src/tabs/navigation.rs b/clients/tui/app/src/tabs/navigation.rs index f339ce33..6ce49f2f 100644 --- a/clients/tui/app/src/tabs/navigation.rs +++ b/clients/tui/app/src/tabs/navigation.rs @@ -22,6 +22,7 @@ pub(crate) fn open(state: &mut State, jobs: &mpsc::Sender, address: Termina crate::startup::retained(state, Some(address)); return; } + state.inventory.remember_terminal(address); if state.focused_terminal() == Some(address) { super::terminal_picker::close(state); state.mode = Mode::Terminal; diff --git a/clients/tui/app/src/tabs/strip.rs b/clients/tui/app/src/tabs/strip.rs index e65e4264..2a41b940 100644 --- a/clients/tui/app/src/tabs/strip.rs +++ b/clients/tui/app/src/tabs/strip.rs @@ -1,40 +1,19 @@ use crate::State; -use ratatui::{ - Frame, - layout::Rect, - style::{Color, Modifier, Style}, - text::Line, - widgets::Paragraph, -}; -use uuid::Uuid; -use yolo_tui_tab::TabEntry; +use ratatui::{Frame, layout::Rect}; +use yolo_tui_render::{TabHit, TabStripItem, draw_tab_strip}; -const STRIP_ROWS: u16 = 1; -const MAX_TAB_COLUMNS: u16 = 32; const FIRST_TAB_NUMBER: usize = 1; const ACTIVE_MARKER: &str = "●"; const INACTIVE_MARKER: &str = " "; const TAB_PADDING: &str = " "; -const ACTIVE_COLOR: Color = Color::Cyan; -/// A fixed dark 256-color slot keeps the cyan label readable even when a -/// theme maps `DarkGray` to a relatively bright value. -const ACTIVE_BACKGROUND: Color = Color::Indexed(237); const ENDED_TAB_MARKER: &str = "×"; const LOST_TAB_MARKER: &str = "!"; -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) struct Tab { - pub id: Uuid, - pub area: Rect, -} - -pub(crate) fn draw(frame: &mut Frame<'_>, state: &State, area: Rect) -> Vec { - if area.is_empty() { - return Vec::new(); - } - let entries = state.tabs.entries(); +pub(crate) fn draw(frame: &mut Frame<'_>, state: &State, area: Rect) -> Vec { let active = state.tabs.active().id; - let labels = entries + let items = state + .tabs + .machine_entries() .iter() .enumerate() .map(|(index, entry)| { @@ -44,62 +23,22 @@ pub(crate) fn draw(frame: &mut Frame<'_>, state: &State, area: Rect) -> Vec INACTIVE_MARKER }; let number = index + FIRST_TAB_NUMBER; - let title = title(state, entry); - Line::from(format!( - "{TAB_PADDING}{marker}{number} {title}{TAB_PADDING}" - )) - }) - .collect::>(); - let widths = labels - .iter() - .map(|label| { - label - .width() - .min(usize::from(MAX_TAB_COLUMNS.min(area.width))) as u16 + TabStripItem { + id: entry.id, + label: format!( + "{TAB_PADDING}{marker}{number} {}{TAB_PADDING}", + title(state, entry) + ), + active: entry.id == active, + } }) .collect::>(); - let mut start = entries - .iter() - .position(|entry| entry.id == active) - .unwrap_or_default(); - let mut used = widths[start]; - while let Some(previous) = start.checked_sub(FIRST_TAB_NUMBER) { - let proposed = used.saturating_add(widths[previous]); - if proposed > area.width { - break; - } - used = proposed; - start = previous; - } - let mut tabs = Vec::new(); - let mut x = area.x; - for ((entry, label), width) in entries.iter().zip(labels).zip(widths).skip(start) { - let width = width.min(area.right().saturating_sub(x)); - if width == 0 { - break; - } - let tab_area = Rect::new(x, area.y, width, STRIP_ROWS); - let style = if entry.id == active { - Style::default() - .fg(ACTIVE_COLOR) - .bg(ACTIVE_BACKGROUND) - .add_modifier(Modifier::BOLD) - } else { - Style::default() - }; - frame.render_widget(Paragraph::new(label).style(style), tab_area); - tabs.push(Tab { - id: entry.id, - area: tab_area, - }); - x = x.saturating_add(width); - } - tabs + draw_tab_strip(frame, area, &items) } -fn title(state: &State, entry: &TabEntry) -> String { +fn title(state: &State, entry: &yolo_tui_tab::TabEntry) -> String { let layout = if entry.id == state.tabs.active().id { - &state.tab_layout() + state.tab_layout() } else { &entry.view.layout }; @@ -113,15 +52,14 @@ fn title(state: &State, entry: &TabEntry) -> String { .and_then(|pane| pane.terminal); // A tab whose terminal can never accept input must not look like a live // one in the strip. The phase label remains in the pane and status bar. - let phase_marker = - address.and_then( - |address| match crate::terminal_lifecycle::phase(state, address) { - yolo_tui_lifecycle::TerminalPhase::Ended - | yolo_tui_lifecycle::TerminalPhase::Archived => Some(ENDED_TAB_MARKER), - yolo_tui_lifecycle::TerminalPhase::Lost => Some(LOST_TAB_MARKER), - _ => None, - }, - ); + let phase_marker = address.and_then(|address| { + match crate::terminal_lifecycle::phase(state, address) { + yolo_tui_lifecycle::TerminalPhase::Ended + | yolo_tui_lifecycle::TerminalPhase::Archived => Some(ENDED_TAB_MARKER), + yolo_tui_lifecycle::TerminalPhase::Lost => Some(LOST_TAB_MARKER), + _ => None, + } + }); if let Some(marker) = phase_marker { return format!("{marker} {title}"); } diff --git a/clients/tui/app/src/terminal/shell_actions.rs b/clients/tui/app/src/terminal/shell_actions.rs index f4beb181..4b33fce8 100644 --- a/clients/tui/app/src/terminal/shell_actions.rs +++ b/clients/tui/app/src/terminal/shell_actions.rs @@ -6,22 +6,10 @@ use yolo_hub_terminal_protocol::{ CreateShellFields, CreateShellRequest, TERMINAL_PROTOCOL_SCHEMA_VERSION, TerminalSize, }; -pub(crate) fn destination(state: &State, entry: HubId) -> HubId { - if state.mode == crate::Mode::Terminal { - if let Some(address) = state.focused_terminal() { - return address.hub_id; - } - } - if state.inventory.view == yolo_tui_inventory::View::Hubs { - if let Some(hub) = state.inventory.selected_hub { - return hub; - } - } +pub(crate) fn destination(state: &State, _entry: HubId) -> HubId { state - .inventory - .scope - .or_else(|| state.focused_terminal().map(|address| address.hub_id)) - .unwrap_or(entry) + .focused_terminal() + .map_or_else(|| state.tabs.machine(), |address| address.hub_id) } pub(crate) fn directory(state: &State, origin: HubId) -> String { diff --git a/clients/tui/app/src/terminal/terminal_status.rs b/clients/tui/app/src/terminal/terminal_status.rs index 91a5f67b..6fc5c3c0 100644 --- a/clients/tui/app/src/terminal/terminal_status.rs +++ b/clients/tui/app/src/terminal/terminal_status.rs @@ -37,7 +37,9 @@ pub(crate) fn line(state: &State, desired: Option) -> Line<'static } else { Color::DarkGray }; - let label = if starting + let label = if crate::constants::is_self_terminal(address.terminal_id.as_uuid()) { + crate::constants::SELF_TERMINAL_LABEL.into() + } else if starting && matches!( projection.phase, TerminalPhase::Starting | TerminalPhase::Unavailable diff --git a/clients/tui/app/test_support/creations.rs b/clients/tui/app/test_support/creations.rs index cb12a3f8..24f9419c 100644 --- a/clients/tui/app/test_support/creations.rs +++ b/clients/tui/app/test_support/creations.rs @@ -12,11 +12,9 @@ use yolo_hub_client_channels::{ControlEvent, ControlRequest}; use yolo_hub_identity::{HubId, TerminalAddress, TerminalId}; use yolo_hub_rpc_protocol::{Command, Reply}; use yolo_hub_terminal_protocol::{CreateShellRequest, CreateShellResult, SecretCapability}; - #[path = "creation_constants.rs"] mod constants; pub use constants::*; - pub struct Fixture { pub state: State, pub jobs: mpsc::Sender, @@ -26,7 +24,6 @@ pub struct Fixture { pub pane: Uuid, pub expected: TerminalAddress, } - impl Fixture { pub fn new() -> Self { Self::with_capacity(QUEUE_CAPACITY) @@ -39,6 +36,11 @@ impl Fixture { // allocation and user navigation are covered by tab-specific tests. state.inventory.view = yolo_tui_inventory::View::Terminals; state.control_generation = Some(Uuid::new_v4()); + state.entry_hub = expected.hub_id; + state.tabs = yolo_tui_tab::TabCatalog::new( + expected.hub_id, + yolo_tui_tab::LocalTabView::default(), + ); state.replace_terminals(vec![existing]); state.bind_focused(Some(expected)).unwrap(); let tab = state.tabs.active().id; diff --git a/clients/tui/app/test_support/interrupts.rs b/clients/tui/app/test_support/interrupts.rs index ece47711..6e62fd47 100644 --- a/clients/tui/app/test_support/interrupts.rs +++ b/clients/tui/app/test_support/interrupts.rs @@ -18,13 +18,19 @@ pub fn ready() -> State { let mut fields = agent.fields().clone(); fields.capabilities.interrupt = Capability::Available {}; let agent = AgentSnapshot::new(fields).unwrap(); + let target = agent.cursor().target(); let mut state = crate::client_capability_support::state(); state.control_generation = Some(uuid::Uuid::new_v4()); state.inventory.replace(Some(vec![agent]), Some(vec![])); state.inventory.current = true; - state.inventory.view = yolo_tui_inventory::View::Agents; + state.inventory.view = yolo_tui_inventory::View::Tree; state.replace_terminals(vec![session.clone()]); state.bind_focused(Some(session.fields().address)).unwrap(); + state.inventory.select_tree( + yolo_tui_inventory::TreeSelection::Agent(target), + "", + &state.terminals, + ); let grant = fixtures::grant(&session); state.principal = grant.fields().principal; state.record_grant(grant, Instant::now()); diff --git a/clients/tui/app/test_support/layout_sync.rs b/clients/tui/app/test_support/layout_sync.rs index 18b711b6..2541b6b1 100644 --- a/clients/tui/app/test_support/layout_sync.rs +++ b/clients/tui/app/test_support/layout_sync.rs @@ -11,7 +11,7 @@ use yolo_presentation_protocol::{LayoutAddress, LayoutUpdate, SharedLayoutDocume pub fn fixture() -> (State, mpsc::Sender, mpsc::Receiver) { let mut state = crate::client_capability_support::state(); - let tabs = state.tab_snapshot().shared_tabs().unwrap(); + let tabs = state.tab_snapshot().shared_tabs(state.entry_hub).unwrap(); let document = LayoutUpdate { address: LayoutAddress { hub_id: HubId::new(), diff --git a/clients/tui/app/test_support/startup.rs b/clients/tui/app/test_support/startup.rs index b448f02c..ed456734 100644 --- a/clients/tui/app/test_support/startup.rs +++ b/clients/tui/app/test_support/startup.rs @@ -18,6 +18,11 @@ impl Fixture { pub fn new(action: StartupAction) -> Self { let mut state = crate::client_capability_support::state(); let hub = HubId::new(); + state.entry_hub = hub; + state.tabs = yolo_tui_tab::TabCatalog::new( + hub, + yolo_tui_tab::LocalTabView::default(), + ); startup::install(&mut state, hub, action); let (jobs, incoming) = mpsc::channel(crate::constants::QUEUE_CAPACITY); Self { diff --git a/clients/tui/app/unit_tests/actions.rs b/clients/tui/app/unit_tests/actions.rs index 14317f4b..43ba55f8 100644 --- a/clients/tui/app/unit_tests/actions.rs +++ b/clients/tui/app/unit_tests/actions.rs @@ -54,6 +54,8 @@ fn empty_inventory_c_creates_a_shell_from_the_absolute_current_directory() { let hub = yolo_hub_identity::HubId::new(); let mut state = crate::client_capability_support::state(); state.control_generation = Some(uuid::Uuid::new_v4()); + state.entry_hub = hub; + state.tabs = yolo_tui_tab::TabCatalog::new(hub, yolo_tui_tab::LocalTabView::default()); crate::startup::install( &mut state, hub, diff --git a/clients/tui/app/unit_tests/agent_directory.rs b/clients/tui/app/unit_tests/agent_directory.rs index ea58853f..46633c4a 100644 --- a/clients/tui/app/unit_tests/agent_directory.rs +++ b/clients/tui/app/unit_tests/agent_directory.rs @@ -79,7 +79,7 @@ fn opening_a_selected_agent_row_switches_to_its_terminal() { let address = session.fields().address; let mut state = crate::client_capability_support::state(); publish(&mut state, agent, session); - state.inventory.view = yolo_tui_inventory::View::Agents; + state.inventory.view = yolo_tui_inventory::View::Tree; state.inventory.selected_agent = Some(target); state.mode = Mode::Inventory; let (jobs, _incoming) = tokio::sync::mpsc::channel(crate::constants::QUEUE_CAPACITY); diff --git a/clients/tui/app/unit_tests/compact_chrome.rs b/clients/tui/app/unit_tests/compact_chrome.rs index f222f71a..9dffc82b 100644 --- a/clients/tui/app/unit_tests/compact_chrome.rs +++ b/clients/tui/app/unit_tests/compact_chrome.rs @@ -84,7 +84,7 @@ fn default_terminal_uses_full_width_with_tab_strip_and_one_status_row() { let content = text(&terminal); assert!(content.contains("~/work/yolo $ codex")); assert!(!content.contains("Agent runs")); - assert!(!content.contains("Hubs")); + assert!(!content.contains("Machines")); assert!(content.contains(&format!("{SHELL} | Control: this UI"))); for absent in ["Connected", "Terminals:", "Ctrl-b", "Read Only", "┌", "└"] { assert!(!content.contains(absent), "{absent}"); @@ -97,14 +97,14 @@ fn healthy_inventory_omits_the_connection_header_and_keeps_exceptions_visible() let mut state = crate::client_capability_support::state(); state.mode = Mode::Inventory; state.sidebar_visible = true; - state.inventory.view = yolo_tui_inventory::View::Agents; + state.inventory.view = yolo_tui_inventory::View::Tree; state.inventory.replace(Some(Vec::new()), Some(Vec::new())); state.inventory.current = true; state.control_generation = Some(uuid::Uuid::new_v4()); let (terminal, _) = render(&state); let content = text(&terminal); - assert!(content.contains("Agent runs | Hubs"), "{content}"); - assert!(!content.contains("Agent runs:"), "{content}"); + assert!(content.contains("No machines"), "{content}"); + assert!(!content.contains("Agent runs | Machines"), "{content}"); assert!(!content.contains("Connected"), "{content}"); assert!(!content.contains("Disconnected"), "{content}"); @@ -177,6 +177,6 @@ fn hidden_sidebar_keeps_split_terminal_width_with_a_single_shared_divider() { screenshot::save(&terminal, "yolo-split.svg"); crate::tabs::terminal_picker::open(&mut state); let (terminal, _) = render(&state); - assert!(text(&terminal).contains(crate::tabs::TERMINAL_PICKER_TITLE)); + assert!(text(&terminal).contains("Terminals")); screenshot::save(&terminal, "yolo-session-picker.svg"); } diff --git a/clients/tui/app/unit_tests/creation/creation_binding.rs b/clients/tui/app/unit_tests/creation/creation_binding.rs index 4b0a1ac6..48e82568 100644 --- a/clients/tui/app/unit_tests/creation/creation_binding.rs +++ b/clients/tui/app/unit_tests/creation/creation_binding.rs @@ -6,7 +6,7 @@ use yolo_presentation_layout::Axis; fn unchanged_active_creation_binds_and_selects_the_new_resource_then_finalizes_once() { let mut fixture = Fixture::new(); fixture.state.filter = RETAINED_FILTER.into(); - fixture.state.inventory.view = yolo_tui_inventory::View::Agents; + fixture.state.inventory.view = yolo_tui_inventory::View::Tree; let request = fixture.start(); let created = fixture.created(); fixture.finish(request.clone(), created); @@ -15,7 +15,7 @@ fn unchanged_active_creation_binds_and_selects_the_new_resource_then_finalizes_o assert_eq!(fixture.state.selected, Some(created.address)); assert_eq!( fixture.state.inventory.view, - yolo_tui_inventory::View::Agents + yolo_tui_inventory::View::Tree ); assert!(fixture.state.filter.is_empty()); assert_eq!(fixture.state.mode, Mode::Inventory); @@ -32,7 +32,7 @@ fn delayed_creation_binds_its_original_inactive_tab_without_stealing_current_foc let active = fixture.switch_tab(); let view = fixture.state.tab_layout().clone(); fixture.state.filter = RETAINED_FILTER.into(); - fixture.state.inventory.view = yolo_tui_inventory::View::Hubs; + fixture.state.inventory.view = yolo_tui_inventory::View::Tree; let selected = fixture.state.selected; let created = fixture.created(); fixture.finish(request.clone(), created); @@ -41,7 +41,7 @@ fn delayed_creation_binds_its_original_inactive_tab_without_stealing_current_foc assert_eq!(*fixture.state.tab_layout(), view); assert_eq!(fixture.state.selected, selected); assert_eq!(fixture.state.filter, RETAINED_FILTER); - assert_eq!(fixture.state.inventory.view, yolo_tui_inventory::View::Hubs); + assert_eq!(fixture.state.inventory.view, yolo_tui_inventory::View::Tree); fixture.assert_finalize(&request, created, true); } diff --git a/clients/tui/app/unit_tests/creation/creation_intent.rs b/clients/tui/app/unit_tests/creation/creation_intent.rs index f876bfc3..04cb08c7 100644 --- a/clients/tui/app/unit_tests/creation/creation_intent.rs +++ b/clients/tui/app/unit_tests/creation/creation_intent.rs @@ -51,7 +51,7 @@ fn stale_creation_failure_or_not_sent_preserves_the_current_presentation_status( }, |fixture| fixture.state.mode = Mode::Search, |fixture| fixture.state.filter = CHANGED_FILTER.into(), - |fixture| fixture.state.inventory.view = yolo_tui_inventory::View::Hubs, + |fixture| fixture.state.inventory.view = yolo_tui_inventory::View::Tree, Fixture::select_other, |fixture| { let address = fixture.expected; diff --git a/clients/tui/app/unit_tests/creation/creation_presentation.rs b/clients/tui/app/unit_tests/creation/creation_presentation.rs index f823c3d1..7001e028 100644 --- a/clients/tui/app/unit_tests/creation/creation_presentation.rs +++ b/clients/tui/app/unit_tests/creation/creation_presentation.rs @@ -10,10 +10,13 @@ fn later_user_selection_filter_scope_view_or_mode_is_never_reset_by_creation_rep f.state.filter = CHANGED_FILTER.into(); }, |f| { - f.state.inventory.scope = Some(yolo_hub_identity::HubId::new()); + f.state + .tabs + .select_machine(yolo_hub_identity::HubId::new()) + .unwrap(); }, |f| { - f.state.inventory.view = yolo_tui_inventory::View::Hubs; + f.state.inventory.view = yolo_tui_inventory::View::Tree; }, |f| { f.state.mode = Mode::Search; diff --git a/clients/tui/app/unit_tests/entry_interactions.rs b/clients/tui/app/unit_tests/entry_interactions.rs index 4d9cde06..3107b496 100644 --- a/clients/tui/app/unit_tests/entry_interactions.rs +++ b/clients/tui/app/unit_tests/entry_interactions.rs @@ -92,8 +92,10 @@ fn fresh_directory_form_uses_configured_cwd_and_remote_scope_uses_host_home() { Event::Key(KeyCode::Char('n').into()), ); assert_eq!(f.state.entry, startup_support::DIRECTORY); + let remote = yolo_hub_identity::HubId::new(); + f.state.tabs.select_machine(remote).unwrap(); + f.state.inventory.selected_machine = Some(remote); f.state.mode = Mode::Inventory; - f.state.inventory.scope = Some(yolo_hub_identity::HubId::new()); crate::actions::handle( &mut f.state, &f.jobs, diff --git a/clients/tui/app/unit_tests/hub_management.rs b/clients/tui/app/unit_tests/hub_management.rs index 55ce6b62..0f7c55cd 100644 --- a/clients/tui/app/unit_tests/hub_management.rs +++ b/clients/tui/app/unit_tests/hub_management.rs @@ -20,9 +20,9 @@ fn management_is_entry_local_and_never_turns_configuration_input_into_terminal_i let entry = HubId::new(); let remote = HubId::new(); let mut state = crate::client_capability_support::state(); - state.inventory.view = View::Hubs; + state.inventory.view = View::Tree; state.inventory.scope = Some(remote); - state.inventory.selected_hub = Some(remote); + state.inventory.selected_machine = Some(remote); state.control_generation = Some(uuid::Uuid::new_v4()); let session = session(); let writer = grant(&session); @@ -72,7 +72,7 @@ fn management_is_entry_local_and_never_turns_configuration_input_into_terminal_i fn late_results_from_a_closed_view_cannot_complete_or_replace_the_reopened_view() { let entry = HubId::new(); let mut state = crate::client_capability_support::state(); - state.inventory.view = View::Hubs; + state.inventory.view = View::Tree; let (jobs, mut requests) = mpsc::channel(4); actions::handle(&mut state, &jobs, entry, key(KeyCode::Char('m'))); let Job::Management(old) = requests.try_recv().unwrap() else { @@ -108,7 +108,7 @@ fn late_results_from_a_closed_view_cannot_complete_or_replace_the_reopened_view( fn configure_button_is_visible_only_in_hub_inventory_and_rejects_stale_geometry() { let entry = HubId::new(); let mut state = crate::client_capability_support::state(); - state.inventory.view = View::Hubs; + state.inventory.view = View::Tree; state.inventory.current = true; state.control_generation = Some(uuid::Uuid::new_v4()); let mut terminal = ratatui::Terminal::new(ratatui::backend::TestBackend::new(90, 25)).unwrap(); @@ -116,7 +116,7 @@ fn configure_button_is_visible_only_in_hub_inventory_and_rejects_stale_geometry( .draw(|frame| state.presentation = crate::draw::draw(frame, &state)) .unwrap(); let bounds = state.presentation.hub_button.unwrap(); - assert_eq!(bounds.y, crate::tabs::TAB_BAR_ROWS); + assert!(bounds.y > crate::tabs::TAB_BAR_ROWS); assert_eq!(bounds.height, 1); let event = Event::Mouse(MouseEvent { kind: MouseEventKind::Down(MouseButton::Left), diff --git a/clients/tui/app/unit_tests/interrupt/interrupts.rs b/clients/tui/app/unit_tests/interrupt/interrupts.rs index fe2cd2a1..cc524d0b 100644 --- a/clients/tui/app/unit_tests/interrupt/interrupts.rs +++ b/clients/tui/app/unit_tests/interrupt/interrupts.rs @@ -98,13 +98,12 @@ enum MissingWriter { } #[test] -fn stale_unreachable_unsupported_and_out_of_scope_agents_cannot_trigger_control() { +fn stale_unreachable_unsupported_and_unselected_agents_cannot_trigger_control() { let mutations: &[fn(&mut crate::State)] = &[ |s| s.inventory.current = false, |s| mutate_agent(s, |f| f.freshness = Freshness::Stale), |s| mutate_agent(s, |f| f.reachability = Reachability::Unreachable), |s| mutate_agent(s, |f| f.capabilities.interrupt = Capability::Unsupported {}), - |s| s.inventory.scope = Some(HubId::new()), |s| s.inventory.selected_agent = None, ]; for mutate in mutations { diff --git a/clients/tui/app/unit_tests/inventory/inventory.rs b/clients/tui/app/unit_tests/inventory/inventory.rs index 93072eb5..571327f1 100644 --- a/clients/tui/app/unit_tests/inventory/inventory.rs +++ b/clients/tui/app/unit_tests/inventory/inventory.rs @@ -47,7 +47,7 @@ fn agent_view_attaches_only_the_current_bound_terminal() { let mut state = catalog_state(); let expected = state.selected.unwrap(); let (jobs, mut incoming) = mpsc::channel(crate::constants::QUEUE_CAPACITY); - assert_eq!(state.inventory.view, View::Agents); + assert_eq!(state.inventory.view, View::Tree); handle(&mut state, &jobs, expected.hub_id, key(KeyCode::Enter)); assert_eq!(state.mode, Mode::Terminal); assert_eq!(state.focused_terminal(), Some(expected)); @@ -60,12 +60,16 @@ fn agent_view_attaches_only_the_current_bound_terminal() { fn old_agent_binding_never_opens_a_replacement_execution() { let mut state = catalog_state(); let expected = state.selected.unwrap(); + let agent = state.inventory.selected_agent.unwrap(); let mut session = state.terminals[&expected].fields().clone(); session.runtime.as_mut().unwrap().target.runtime_epoch = yolo_hub_identity::RuntimeEpoch::new(); state.replace_terminals(vec![ yolo_hub_terminal_protocol::TerminalSummary::new(session).unwrap(), ]); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; + state.inventory.tree_selected = + Some(yolo_tui_inventory::TreeSelection::Agent(agent)); + state.inventory.selected_agent = Some(agent); let (jobs, mut incoming) = mpsc::channel(crate::constants::QUEUE_CAPACITY); handle(&mut state, &jobs, expected.hub_id, key(KeyCode::Enter)); assert_eq!(state.mode, Mode::Inventory); @@ -90,7 +94,7 @@ fn failed_refresh_marks_existing_catalog_stale_without_inventing_agent_exit() { ); assert!(!state.inventory.current); assert_eq!(state.inventory.agents, previous); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; handle(&mut state, &jobs, HubId::new(), key(KeyCode::Enter)); assert!(incoming.try_recv().is_err()); } @@ -102,7 +106,7 @@ fn hub_scope_controls_shell_creation_destination_and_escape_restores_all_hubs() let entry = HubId::new(); let (jobs, mut incoming) = mpsc::channel(crate::constants::QUEUE_CAPACITY); handle(&mut state, &jobs, entry, key(KeyCode::BackTab)); - assert_eq!(state.inventory.view, View::Hubs); + assert_eq!(state.inventory.view, View::Tree); handle(&mut state, &jobs, entry, key(KeyCode::Char('n'))); assert_eq!(state.mode, Mode::Directory); handle(&mut state, &jobs, entry, key(KeyCode::Enter)); @@ -118,7 +122,7 @@ fn hub_scope_controls_shell_creation_destination_and_escape_restores_all_hubs() #[test] fn agent_view_cannot_silently_terminate_its_shell_as_a_semantic_agent_command() { let mut state = catalog_state(); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; let (jobs, mut incoming) = mpsc::channel(crate::constants::QUEUE_CAPACITY); for code in ['x', 'a', 'R'] { handle(&mut state, &jobs, HubId::new(), key(KeyCode::Char(code))); diff --git a/clients/tui/app/unit_tests/inventory/inventory_machine_open.rs b/clients/tui/app/unit_tests/inventory/inventory_machine_open.rs new file mode 100644 index 00000000..45bcca70 --- /dev/null +++ b/clients/tui/app/unit_tests/inventory/inventory_machine_open.rs @@ -0,0 +1,101 @@ +use super::{fixtures, panes}; +use crate::{Mode, State}; +use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; +use yolo_hub_identity::{HubId, TerminalAddress}; +use yolo_hub_rpc_protocol::HubSummary; +use yolo_tui_inventory::{TreeSelection, View}; + +fn machine_state() -> (State, HubId, TerminalAddress) { + let (agent, session) = fixtures::agent(); + let hub = session.fields().address.hub_id; + let address = session.fields().address; + let mut state = crate::client_capability_support::state(); + state.inventory.replace( + Some(vec![agent]), + Some(vec![HubSummary { + hub_id: hub, + reachable: true, + label: Some("machine".into()), + }]), + ); + state.inventory.current = true; + state.inventory.view = View::Tree; + state.control_generation = Some(uuid::Uuid::new_v4()); + state.entry_hub = hub; + state.tabs = yolo_tui_tab::TabCatalog::new( + hub, + yolo_tui_tab::LocalTabView::default(), + ); + state.replace_terminals(vec![session]); + state.inventory.select_tree( + TreeSelection::Machine(hub), + "", + &state.terminals, + ); + panes::repaint(&mut state); + (state, hub, address) +} + +fn open(state: &mut State) -> tokio::sync::mpsc::Receiver { + let (jobs, pending) = tokio::sync::mpsc::channel(crate::constants::QUEUE_CAPACITY); + crate::inventory_actions::open(state, &jobs); + pending +} + +#[test] +fn machine_open_returns_to_the_last_live_terminal() { + let (mut state, _hub, address) = machine_state(); + state.inventory.remember_terminal(address); + let _pending = open(&mut state); + assert_eq!(state.inventory.view, View::Tree); + assert_eq!(state.mode, Mode::Terminal); + assert_eq!(state.focused_terminal(), Some(address)); +} + +#[test] +fn machine_without_a_terminal_starts_a_shell_and_reuses_the_vacant_tab() { + let (agent, session) = fixtures::agent(); + let hub = session.fields().address.hub_id; + let mut state = crate::client_capability_support::state(); + state.inventory.replace( + Some(vec![agent]), + Some(vec![HubSummary { + hub_id: hub, + reachable: true, + label: Some("machine".into()), + }]), + ); + state.inventory.current = true; + state.inventory.view = View::Tree; + state.control_generation = Some(uuid::Uuid::new_v4()); + state.entry_hub = hub; + state.tabs = yolo_tui_tab::TabCatalog::new( + hub, + yolo_tui_tab::LocalTabView::default(), + ); + state + .inventory + .select_tree(TreeSelection::Machine(hub), "", &state.terminals); + let mut pending = open(&mut state); + let Some(crate::network::Job::Call { origin, command, .. }) = pending.try_recv().ok() else { + panic!("machine activation must create a shell") + }; + assert_eq!(origin, hub); + assert!(matches!( + command, + yolo_hub_rpc_protocol::Command::Create { .. } + )); + assert_eq!(state.mode, Mode::Terminal); + assert_eq!(state.tabs.machine(), hub); + assert_eq!(state.tabs.count_for(hub), 1); +} + +#[test] +fn entering_the_tree_does_not_switch_away_from_the_active_machine() { + let (mut state, hub, _) = machine_state(); + state.mode = Mode::Inventory; + let key = KeyEvent::new(KeyCode::Down, KeyModifiers::NONE); + let (jobs, _pending) = tokio::sync::mpsc::channel(crate::constants::QUEUE_CAPACITY); + crate::actions::handle(&mut state, &jobs, hub, Event::Key(key)); + assert_eq!(state.tabs.machine(), hub); +} diff --git a/clients/tui/app/unit_tests/inventory/inventory_pointer.rs b/clients/tui/app/unit_tests/inventory/inventory_pointer.rs index d8ed2313..50184a1d 100644 --- a/clients/tui/app/unit_tests/inventory/inventory_pointer.rs +++ b/clients/tui/app/unit_tests/inventory/inventory_pointer.rs @@ -38,32 +38,27 @@ fn position(state: &State, expected: InventoryHit) -> (u16, u16) { .find(|(x, y)| viewport.hit(*x, *y) == Some(expected)) .unwrap() } - #[test] fn one_inventory_click_selects_exact_displayed_resource_and_opens_agent_terminals() { - for view in [View::Terminals, View::Hubs] { - let mut state = state(view); - let layout = state.tab_layout().clone(); - let target = *state - .inventory - .visible_selections(&state.filter, &state.terminals) - .last() - .unwrap(); - let (x, y) = position(&state, InventoryHit::Row(target)); - let (jobs, mut pending) = tokio::sync::mpsc::channel(crate::constants::QUEUE_CAPACITY); - pointer::handle( - &mut state, - &jobs, - panes::mouse(MouseEventKind::Down(MouseButton::Left), x, y), - ); - assert_eq!(state.inventory.selected(state.selected), Some(target)); - assert_eq!(state.mode, Mode::Inventory); - assert_eq!(*state.tab_layout(), layout); - assert!(state.writers.is_empty()); - assert!(pending.try_recv().is_err()); - } - - let mut state = state(View::Agents); + let mut state = state(View::Terminals); + let layout = state.tab_layout().clone(); + let target = *state + .inventory + .visible_selections(&state.filter, &state.terminals) + .last() + .unwrap(); + let (x, y) = position(&state, InventoryHit::Row(target)); + let (jobs, mut pending) = tokio::sync::mpsc::channel(crate::constants::QUEUE_CAPACITY); + pointer::handle( + &mut state, + &jobs, + panes::mouse(MouseEventKind::Down(MouseButton::Left), x, y), + ); + assert_eq!(state.inventory.selected(state.selected), Some(target)); + assert_eq!(state.mode, Mode::Inventory); + assert_eq!(*state.tab_layout(), layout); + assert!(state.writers.is_empty() && pending.try_recv().is_err()); + let mut state = self::state(View::Tree); let target = *state .inventory .visible_selections(&state.filter, &state.terminals) @@ -88,25 +83,6 @@ fn one_inventory_click_selects_exact_displayed_resource_and_opens_agent_terminal assert_eq!(state.mode, Mode::Terminal); } -#[test] -fn inventory_header_changes_view_without_opening_a_terminal_or_grant() { - let mut state = state(View::Agents); - let (jobs, mut pending) = tokio::sync::mpsc::channel(crate::constants::QUEUE_CAPACITY); - for view in [View::Hubs, View::Agents] { - let (x, y) = position(&state, InventoryHit::Tab(view)); - pointer::handle( - &mut state, - &jobs, - panes::mouse(MouseEventKind::Down(MouseButton::Left), x, y), - ); - assert_eq!(state.inventory.view, view); - assert_eq!(state.mode, Mode::Inventory); - assert!(state.focused_terminal().is_none()); - panes::repaint(&mut state); - } - assert!(pending.try_recv().is_err()); -} - #[test] fn inventory_wheel_selects_next_row_but_old_frame_cannot_be_reused_for_another_scroll() { let mut state = state(View::Terminals); @@ -145,7 +121,22 @@ fn inventory_wheel_selects_next_row_but_old_frame_cannot_be_reused_for_another_s #[test] fn agent_wheel_browses_the_list_without_opening_the_selected_terminal() { - let mut state = state(View::Agents); + let mut state = self::state(View::Tree); + let agent = state + .inventory + .visible_selections(&state.filter, &state.terminals) + .into_iter() + .find_map(|selection| match selection { + InventorySelection::Agent(target) => Some(target), + _ => None, + }) + .unwrap(); + state.inventory.select_tree( + yolo_tui_inventory::TreeSelection::Agent(agent), + "", + &state.terminals, + ); + panes::repaint(&mut state); let visible = state .inventory .visible_selections(&state.filter, &state.terminals); @@ -156,10 +147,10 @@ fn agent_wheel_browses_the_list_without_opening_the_selected_terminal() { &jobs, panes::mouse(MouseEventKind::ScrollDown, rows.x, rows.y), ); - assert_eq!( + assert!(matches!( state.inventory.selected(state.selected), - visible.last().copied() - ); + Some(InventorySelection::Machine(_) | InventorySelection::Agent(_)) + )); assert_eq!(state.mode, Mode::Inventory); assert!(state.focused_terminal().is_none()); assert!(state.inventory.details.is_none()); diff --git a/clients/tui/app/unit_tests/inventory/mod.rs b/clients/tui/app/unit_tests/inventory/mod.rs index 5d701c51..5dc10e75 100644 --- a/clients/tui/app/unit_tests/inventory/mod.rs +++ b/clients/tui/app/unit_tests/inventory/mod.rs @@ -1,3 +1,4 @@ use super::{fixtures, panes}; mod inventory; mod inventory_pointer; +mod inventory_machine_open; diff --git a/clients/tui/app/unit_tests/layout/layout_checkpoint.rs b/clients/tui/app/unit_tests/layout/layout_checkpoint.rs index 77cd18c4..f784c4d6 100644 --- a/clients/tui/app/unit_tests/layout/layout_checkpoint.rs +++ b/clients/tui/app/unit_tests/layout/layout_checkpoint.rs @@ -38,7 +38,7 @@ fn restart_accepts_matching_committed_draft_and_retains_conflicting_draft() { assert!(restore_draft(&mut state, &other, Some(&checkpoint)).is_err()); assert_eq!(*state.tab_layout(), draft); let mut accepted = other; - accepted.tabs = state.tab_snapshot().shared_tabs().unwrap(); + accepted.tabs = state.tab_snapshot().shared_tabs(state.entry_hub).unwrap(); assert_eq!( restore_draft(&mut state, &accepted, Some(&checkpoint)).unwrap(), accepted.tabs diff --git a/clients/tui/app/unit_tests/layout/layout_sync.rs b/clients/tui/app/unit_tests/layout/layout_sync.rs index 9e8263ed..998016c1 100644 --- a/clients/tui/app/unit_tests/layout/layout_sync.rs +++ b/clients/tui/app/unit_tests/layout/layout_sync.rs @@ -78,7 +78,7 @@ fn concurrent_remote_change_loads_authoritative_revision_without_terminating_she .split(Axis::Horizontal, None) .unwrap(); response(&mut state, read, other.clone()); - assert_eq!(state.tab_snapshot().shared_tabs().unwrap(), other.tabs); + assert_eq!(state.tab_snapshot().shared_tabs(state.entry_hub).unwrap(), other.tabs); assert!(state.status.contains("Terminals remain running")); pump(&mut state, &jobs); assert!(incoming.try_recv().is_err()); diff --git a/clients/tui/app/unit_tests/lifecycle/lifecycle_details.rs b/clients/tui/app/unit_tests/lifecycle/lifecycle_details.rs index 3abd658a..c6f84ad9 100644 --- a/clients/tui/app/unit_tests/lifecycle/lifecycle_details.rs +++ b/clients/tui/app/unit_tests/lifecycle/lifecycle_details.rs @@ -76,7 +76,7 @@ fn unavailable_agent_opens_details_without_dispatching_control() { .terminals .insert(terminal, TerminalSummary::new(session).unwrap()); state.inventory.history = true; - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; state.inventory.selected_agent = Some(address); let (jobs, mut received) = mpsc::channel(QUEUE_CAPACITY); assert!(!key(&mut state, &jobs, KeyCode::Enter)); @@ -96,7 +96,7 @@ fn details_r_only_navigates_to_matching_terminal_history_without_control_job() { state.terminals = BTreeMap::from([(terminal, TerminalSummary::new(replaced).unwrap())]); state.inventory.history = true; state.inventory.details = Some(agent); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; let (jobs, mut received) = mpsc::channel(QUEUE_CAPACITY); assert!(!key(&mut state, &jobs, KeyCode::Char('R'))); assert_eq!(state.inventory.details, None); @@ -114,7 +114,7 @@ fn details_r_with_missing_terminal_keeps_details_and_esc_closes_it() { state.terminals.clear(); state.inventory.history = true; state.inventory.details = Some(agent); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; let (jobs, mut received) = mpsc::channel(QUEUE_CAPACITY); assert!(!key(&mut state, &jobs, KeyCode::Char('R'))); assert_eq!(state.inventory.details, Some(agent)); @@ -129,7 +129,7 @@ fn details_render_when_the_layout_has_no_terminal_pane() { let mut state = state(); let agent = state.inventory.selected_agent.unwrap(); state.inventory.details = Some(agent); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; state.mode = Mode::Inventory; assert!( state diff --git a/clients/tui/app/unit_tests/respond.rs b/clients/tui/app/unit_tests/respond.rs index f66dc57f..b331fb20 100644 --- a/clients/tui/app/unit_tests/respond.rs +++ b/clients/tui/app/unit_tests/respond.rs @@ -39,7 +39,7 @@ fn pending() -> State { state.control_generation = Some(uuid::Uuid::new_v4()); state.inventory.replace(Some(vec![agent]), Some(vec![])); state.inventory.current = true; - state.inventory.view = yolo_tui_inventory::View::Agents; + state.inventory.view = yolo_tui_inventory::View::Tree; state.inventory.selected_agent = state.inventory.agents.keys().next().copied(); state.inventory.details = state.inventory.selected_agent; state.replace_terminals(vec![session.clone()]); diff --git a/clients/tui/app/unit_tests/selection/selection_inventory.rs b/clients/tui/app/unit_tests/selection/selection_inventory.rs index 431e936b..22f4665f 100644 --- a/clients/tui/app/unit_tests/selection/selection_inventory.rs +++ b/clients/tui/app/unit_tests/selection/selection_inventory.rs @@ -30,7 +30,6 @@ fn inventory_escape_resumes_frozen_output_without_clearing_filter_scope_or_layou // Once selection is gone, the next Escape retains normal inventory behavior. fixture.key(KeyCode::Esc, KeyModifiers::NONE); assert!(fixture.state.filter.is_empty()); - assert!(fixture.state.inventory.scope.is_none()); assert_eq!(*fixture.state.tab_layout(), layout); fixture.no_input(); } diff --git a/clients/tui/app/unit_tests/sidebar_preference.rs b/clients/tui/app/unit_tests/sidebar_preference.rs index bf81f5b3..34ce0bf6 100644 --- a/clients/tui/app/unit_tests/sidebar_preference.rs +++ b/clients/tui/app/unit_tests/sidebar_preference.rs @@ -26,7 +26,7 @@ fn explicitly_open_sidebar_stays_visible_when_empty_and_when_terminal_regains_fo let (mut state, _) = drawing_support::state(); tabs::sidebar(&mut state); state.inventory.replace(Some(Vec::new()), None); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; let (_, sidebar) = drawing_support::render(&state, COLUMNS, ROWS); assert!(state.sidebar_visible); assert!(sidebar.inventory.is_some()); @@ -42,9 +42,8 @@ fn an_empty_layout_uses_inventory_width_without_forgetting_sidebar_preference() tabs::sidebar(&mut state); let (_, view) = drawing_support::render(&state, COLUMNS, ROWS); assert!(state.sidebar_visible); - assert!(view.bounds.is_empty()); - assert!(view.panes.is_empty()); - assert_eq!(view.inventory.unwrap().area().width, COLUMNS); + assert!(!view.bounds.is_empty()); + assert!(view.inventory.unwrap().area().width < COLUMNS); } #[test] diff --git a/clients/tui/app/unit_tests/state.rs b/clients/tui/app/unit_tests/state.rs index 8303acf4..bed3662b 100644 --- a/clients/tui/app/unit_tests/state.rs +++ b/clients/tui/app/unit_tests/state.rs @@ -10,7 +10,7 @@ fn inventory_uses_real_resources_and_selection_survives_reordering() { let second = session(); let mut state = crate::client_capability_support::state(); assert_eq!(state.mode, Mode::Inventory); - assert_eq!(state.inventory.view, yolo_tui_inventory::View::Agents); + assert_eq!(state.inventory.view, yolo_tui_inventory::View::Tree); crate::tabs::terminal_picker::open(&mut state); assert!(state.visible().is_empty()); state.move_selection(true); diff --git a/clients/tui/app/unit_tests/tab/tab_actions.rs b/clients/tui/app/unit_tests/tab/tab_actions.rs index 0fb0054e..ff523caa 100644 --- a/clients/tui/app/unit_tests/tab/tab_actions.rs +++ b/clients/tui/app/unit_tests/tab/tab_actions.rs @@ -107,7 +107,7 @@ fn closing_last_pane_or_whole_tab_keeps_processes_and_reveals_another_tab() { assert!(fixture.state.focused_terminal().is_none()); assert_eq!(fixture.state.mode, Mode::Inventory); assert!(fixture.state.sidebar_visible); - assert_eq!(fixture.state.inventory.view, View::Agents); + assert_eq!(fixture.state.inventory.view, View::Tree); assert!(fixture.state.terminals.contains_key(&fixture.expected)); assert!(fixture.incoming.try_recv().is_err()); } @@ -143,7 +143,7 @@ fn sidebar_navigation_leaves_picker_and_never_presents_terminal_inventory_as_a_s tabs::terminal_picker::open(&mut fixture.state); tabs::sidebar(&mut fixture.state); assert!(fixture.state.terminal_picker.is_none()); - assert_eq!(fixture.state.inventory.view, View::Agents); + assert_eq!(fixture.state.inventory.view, View::Tree); assert_eq!(fixture.state.mode, Mode::Inventory); prefix(&mut fixture, tabs::TOGGLE_SIDEBAR_KEY); assert!(!fixture.state.sidebar_visible); diff --git a/clients/tui/app/unit_tests/tab/tab_navigation.rs b/clients/tui/app/unit_tests/tab/tab_navigation.rs index 4fa18f2d..aae72966 100644 --- a/clients/tui/app/unit_tests/tab/tab_navigation.rs +++ b/clients/tui/app/unit_tests/tab/tab_navigation.rs @@ -152,8 +152,7 @@ fn narrow_sidebar_exposes_only_agents_and_hubs_and_keeps_terminal_state() { let layout = state.tab_layout().clone(); tabs::sidebar(&mut state); let (text, sidebar) = drawing_support::render(&state, NARROW_COLUMNS, panes::HEIGHT); - assert!(text.contains("Agent runs")); - assert!(text.contains("Hubs")); + assert!(!text.contains("Agent runs")); assert!(!text.contains("Terminals")); assert!(sidebar.panes.is_empty()); state.mode = Mode::Terminal; diff --git a/clients/tui/app/unit_tests/tab/tab_navigation_errors.rs b/clients/tui/app/unit_tests/tab/tab_navigation_errors.rs index 0c917f63..2e0663d4 100644 --- a/clients/tui/app/unit_tests/tab/tab_navigation_errors.rs +++ b/clients/tui/app/unit_tests/tab/tab_navigation_errors.rs @@ -133,7 +133,7 @@ fn nested_picker_open_preserves_original_sidebar_filter_and_never_changes_bindin } else { crate::client_capability_support::state() }; - state.inventory.view = View::Hubs; + state.inventory.view = View::Tree; state.filter = SAVED_FILTER.into(); let mode = state.mode; let layout = state.tab_layout().clone(); @@ -145,7 +145,7 @@ fn nested_picker_open_preserves_original_sidebar_filter_and_never_changes_bindin tabs::terminal_picker::open(&mut state); assert_eq!(state.filter, PICKER_FILTER); tabs::terminal_picker::close(&mut state); - assert_eq!(state.inventory.view, View::Hubs); + assert_eq!(state.inventory.view, View::Tree); assert_eq!(state.filter, SAVED_FILTER); assert_eq!(state.mode, mode); assert_eq!(*state.tab_layout(), layout); diff --git a/clients/tui/app/unit_tests/tab/tab_recovery.rs b/clients/tui/app/unit_tests/tab/tab_recovery.rs index a00fbb4a..f64cc32e 100644 --- a/clients/tui/app/unit_tests/tab/tab_recovery.rs +++ b/clients/tui/app/unit_tests/tab/tab_recovery.rs @@ -37,7 +37,7 @@ fn agent_history_recovery_targets_ended_agent_terminal_instead_of_focused_live_t state.inventory.agents.insert(agent_target, ended_agent); state.inventory.selected_agent = Some(agent_target); state.inventory.details = Some(agent_target); - state.inventory.view = View::Agents; + state.inventory.view = View::Tree; state.inventory.history = true; let (jobs, mut incoming) = mpsc::channel(crate::constants::QUEUE_CAPACITY); key( diff --git a/clients/tui/app/unit_tests/tab/tab_strip.rs b/clients/tui/app/unit_tests/tab/tab_strip.rs index 16022066..a0a19dd4 100644 --- a/clients/tui/app/unit_tests/tab/tab_strip.rs +++ b/clients/tui/app/unit_tests/tab/tab_strip.rs @@ -46,7 +46,7 @@ fn pages(titles: &[&str]) -> State { state } -fn render(state: &State, area: Rect) -> (Terminal, Vec) { +fn render(state: &State, area: Rect) -> (Terminal, Vec) { let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).unwrap(); let mut tabs = Vec::new(); terminal diff --git a/clients/tui/audit/src/constants.rs b/clients/tui/audit/src/constants.rs index 7f4fe1c4..c7bf1ded 100644 --- a/clients/tui/audit/src/constants.rs +++ b/clients/tui/audit/src/constants.rs @@ -2,7 +2,7 @@ use std::time::Duration; pub const DEFAULT_AUDIT_LIMIT: u16 = 128; pub const MAX_AUDIT_LIMIT: u16 = 256; -pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run yolo first"; +pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run `yolo server start` first"; pub const AUDIT_MERGE_MAX_SOURCES: usize = 16; pub const AUDIT_MERGE_MAX_BYTES: u64 = 8 * 1024 * 1024; pub const AUDIT_AFTER_WITH_MERGE: &str = "audit --after cannot be combined with --merge"; @@ -13,7 +13,6 @@ pub const AUDIT_MERGE_UNAUTHENTICATED: &str = pub const AUDIT_SOURCE_FIELD: &str = "source"; pub const AUDIT_MERGE_FILE: &str = "audit merge sources must be regular files"; pub const AUDIT_PULL_TIMEOUT: Duration = Duration::from_secs(15); -pub const AUDIT_PULL_TRUST_REQUIRES_OUTPUT: &str = - "audit pull with --trust requires --output"; +pub const AUDIT_PULL_TRUST_REQUIRES_OUTPUT: &str = "audit pull with --trust requires --output"; pub const AUDIT_PULL_UNEXPECTED_REPLY: &str = "Hub returned an unexpected audit reply"; pub const AUDIT_PULL_WRITE_FAILED: &str = "write the pulled audit file"; diff --git a/clients/tui/audit/src/lib.rs b/clients/tui/audit/src/lib.rs index 8a7bb0f1..20bd45e3 100644 --- a/clients/tui/audit/src/lib.rs +++ b/clients/tui/audit/src/lib.rs @@ -3,10 +3,10 @@ mod constants; pub(crate) mod merge; +mod pull; #[cfg(test)] #[path = "../unit_tests/lib.rs"] mod unit_tests; -mod pull; use anyhow::{Context, Result, ensure}; use std::path::PathBuf; @@ -59,6 +59,8 @@ pub async fn run(profile: &LocalProfile, request: Request<'_>) -> Result<()> { ensure!(request.after.is_none(), AUDIT_AFTER_WITH_MERGE); return merge::run(request.merge, request.limit, request.json, request.trust); } + let _ = yolo_hub_state::HubState::inspect_identity(&profile.state, &profile.host_home)? + .context(NO_LOCAL_HUB)?; let entries = read(&profile.state.join(AUDIT_FILE))?; let entries = entries .into_iter() diff --git a/clients/tui/cli/BUILD.bazel b/clients/tui/cli/BUILD.bazel index 9cfc3225..c049f66c 100644 --- a/clients/tui/cli/BUILD.bazel +++ b/clients/tui/cli/BUILD.bazel @@ -6,6 +6,7 @@ package(default_visibility = ["//visibility:public"]) _DEPS = [ "//core/crates/agent/catalog:yolo_agent_catalog", + "//core/crates/application/command:yolo_command_cli", "//core/crates/application/launch/contract:yolo_launch_contract", "//core/crates/application/local/profile:yolo_local_profile", "//clients/tui/audit:yolo_tui_audit", @@ -17,10 +18,13 @@ _DEPS = [ "//core/crates/hub/client:yolo_hub_client", "//core/crates/hub/identity:yolo_hub_identity", "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", - "//core/crates/hub/events/protocol:yolo_hub_events_protocol", "//core/crates/hub/terminal/protocol:yolo_hub_terminal_protocol", "//core/crates/hub/workspace/protocol:yolo_hub_workspace_protocol", "//clients/tui/app:yolo_tui_app", + "//clients/tui/cli/hook:yolo_tui_cli_hook", + "//clients/tui/cli/platform:yolo_tui_cli_platform", + "//clients/tui/hub/cli:yolo_tui_hub_cli", + "//clients/tui/service:yolo_tui_service", "//clients/tui/local/startup:yolo_tui_local_startup", "//clients/tui/client/control:yolo_tui_client_control", "//clients/tui/tab:yolo_tui_tab", @@ -60,6 +64,10 @@ rust_test( edition = "2024", deps = _DEPS + [ "//core/crates/hub/client/transport:yolo_hub_client_transport", + "//core/crates/hub/delegation:yolo_hub_delegation", + "//core/crates/hub/federation/settings:yolo_hub_federation_settings", + "//core/crates/hub/management/protocol:yolo_hub_management_protocol", + "//core/crates/hub/routing:yolo_hub_routing", "@crates//:ring", "@crates//:tempfile", ], diff --git a/clients/tui/cli/hook/BUILD.bazel b/clients/tui/cli/hook/BUILD.bazel new file mode 100644 index 00000000..2a6dd05f --- /dev/null +++ b/clients/tui/cli/hook/BUILD.bazel @@ -0,0 +1,37 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "//core/crates/agent/system/hooks/installation:yolo_hook_installation", + "//core/crates/agent/system/providers:yolo_agent_providers", + "//core/crates/persistence/paths:yolo_paths", + "@crates//:anyhow", + "@crates//:clap", + "@crates//:serde_json", +] + +filegroup( + name = "architecture_check_data", + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], +) + +rust_library( + name = "yolo_tui_cli_hook", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_tui_cli_hook", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) + +rust_test( + name = "unit_tests", + srcs = glob(["src/**/*.rs", "unit_tests/**/*.rs"]), + crate_name = "yolo_tui_cli_hook_unit_tests", + crate_root = "src/lib.rs", + edition = "2024", + deps = _DEPS, +) diff --git a/clients/tui/cli/hook/src/command.rs b/clients/tui/cli/hook/src/command.rs new file mode 100644 index 00000000..a51dec5c --- /dev/null +++ b/clients/tui/cli/hook/src/command.rs @@ -0,0 +1,33 @@ +use clap::Subcommand; +use std::path::PathBuf; + +/// User-facing provider hook management. The Host role keeps the +/// revision-fenced commands for automation; this surface owns the review, +/// confirmation and readable output. +#[derive(Debug, Subcommand)] +pub enum HookCommand { + /// Show which terminal agents have managed hooks installed. + Status { + /// Print one JSON document. + #[arg(long)] + json: bool, + }, + /// Install managed hooks for one or more terminal agents. + Install { + /// Provider tokens: claude, codex, qwen, gemini or grok. + #[arg(required = true)] + providers: Vec, + /// Provider configuration directory; only with exactly one provider. + #[arg(long)] + config_home: Option, + /// Install without asking for confirmation. + #[arg(long)] + yes: bool, + /// Review the changes without writing any file. + #[arg(long)] + dry_run: bool, + /// Print one JSON document. + #[arg(long)] + json: bool, + }, +} diff --git a/clients/tui/cli/hook/src/constants.rs b/clients/tui/cli/hook/src/constants.rs new file mode 100644 index 00000000..e722551f --- /dev/null +++ b/clients/tui/cli/hook/src/constants.rs @@ -0,0 +1,34 @@ +pub(crate) const CONFIG_HOME_SINGLE_PROVIDER: &str = + "--config-home requires exactly one provider"; +pub(crate) const JSON_REQUIRES_YES: &str = "--json requires --yes to confirm installation"; +pub(crate) const NON_INTERACTIVE: &str = + "cannot confirm on a non-interactive terminal; pass --yes"; +pub(crate) const RESTRICTED: &str = + "hooks are disabled in this provider's configuration; enable them before installing"; +pub(crate) const UNKNOWN_PROVIDER: &str = "unknown provider"; +pub(crate) const KNOWN_PROVIDERS: &str = "known providers"; + +pub(crate) const STATUS_HEADER: &str = "PROVIDER\tAGENT\tSTATUS\tTRUST\tFILE"; +pub(crate) const STATUS_INSTALLED: &str = "installed"; +pub(crate) const STATUS_NOT_INSTALLED: &str = "not installed"; +pub(crate) const STATUS_DISABLED: &str = "disabled"; +pub(crate) const TRUST_REQUIRED: &str = "required"; +pub(crate) const TRUST_NOT_REQUIRED: &str = "-"; + +pub(crate) const ALREADY_INSTALLED: &str = "All requested hooks are already installed."; +pub(crate) const DRY_RUN: &str = "Dry run: no files were written."; +pub(crate) const CANCELLED: &str = "Hook installation cancelled."; +pub(crate) const MANAGED_NOTE: &str = + "Yolo runs these hooks only inside Yolo-managed terminals."; +pub(crate) const TRUST_NOTE: &str = + "review and trust these hooks in the provider's own hook settings; Yolo does not bypass hook trust"; +pub(crate) const CONFIRM_PREFIX: &str = "Install hooks for"; +pub(crate) const CONFIRM_SUFFIX: &str = "? [y/N] "; +pub(crate) const BACKUP_PREFIX: &str = " backup: "; +pub(crate) const FILE_PREFIX: &str = " file: "; +pub(crate) const STATUS_PREFIX: &str = " status: "; +pub(crate) const EVENTS_PREFIX: &str = " events: "; +pub(crate) const NOTE_PREFIX: &str = " note: "; +pub(crate) const TRUST_PREFIX: &str = " trust: "; +pub(crate) const EVENTS_SUFFIX: &str = " managed callbacks"; +pub(crate) const INSTALLED_SUFFIX: &str = ": installed at "; diff --git a/clients/tui/cli/hook/src/install.rs b/clients/tui/cli/hook/src/install.rs new file mode 100644 index 00000000..5e685b02 --- /dev/null +++ b/clients/tui/cli/hook/src/install.rs @@ -0,0 +1,136 @@ +use crate::{constants::*, render}; +use anyhow::{Context, Result, ensure}; +use std::io::{self, IsTerminal, Write}; +use std::path::{Path, PathBuf}; +use yolo_agent_providers::builtin_install_spec; +use yolo_hook_installation::{ + HookInstallSpec, IntegrationStatus, Preview, install, inspect, resolve_home, +}; +use yolo_paths::validation::validate_absolute; + +pub(crate) struct Plan { + pub(crate) spec: &'static HookInstallSpec, + pub(crate) home: PathBuf, + pub(crate) preview: Preview, +} + +pub(crate) fn run( + providers: &[String], + config_home: Option<&Path>, + yes: bool, + dry_run: bool, + json: bool, +) -> Result<()> { + ensure!( + config_home.is_none() || providers.len() == 1, + CONFIG_HOME_SINGLE_PROVIDER + ); + let plans = build_plans(providers, config_home)?; + for plan in &plans { + ensure!( + plan.preview.status != IntegrationStatus::Restricted, + "{}: {RESTRICTED}", + plan.spec.display_name + ); + } + let pending: Vec<&Plan> = plans + .iter() + .filter(|plan| plan.preview.status == IntegrationStatus::Missing) + .collect(); + if json { + return run_json(&plans, &pending, yes, dry_run); + } + for plan in &plans { + render::preview_lines(plan).iter().for_each(|line| println!("{line}")); + } + if pending.is_empty() { + println!("{ALREADY_INSTALLED}"); + return Ok(()); + } + if dry_run { + println!("{DRY_RUN}"); + return Ok(()); + } + if !yes { + ensure!(io::stdin().is_terminal(), NON_INTERACTIVE); + if !confirm(&pending)? { + println!("{CANCELLED}"); + return Ok(()); + } + } + install_pending(&pending) +} + +fn run_json(plans: &[Plan], pending: &[&Plan], yes: bool, dry_run: bool) -> Result<()> { + let documents = if dry_run || pending.is_empty() { + plans.iter().map(render::preview_json).collect() + } else { + ensure!(yes, JSON_REQUIRES_YES); + let mut documents = Vec::with_capacity(plans.len()); + for plan in plans { + let installation = install(plan.spec, &plan.home, &plan.preview.revision)?; + documents.push(render::install_json(plan, &installation)); + } + documents + }; + let mut out = io::stdout().lock(); + serde_json::to_writer_pretty(&mut out, &documents)?; + writeln!(out)?; + Ok(()) +} + +fn build_plans(providers: &[String], config_home: Option<&Path>) -> Result> { + providers + .iter() + .map(|token| plan(token, config_home)) + .collect() +} + +fn plan(token: &str, config_home: Option<&Path>) -> Result { + let spec = builtin_install_spec(token).with_context(|| unknown_provider(token))?; + let home = resolve_home(spec, config_home.map(Path::to_path_buf), |name| { + std::env::var_os(name) + })?; + validate_absolute(&home)?; + Ok(Plan { + spec, + preview: inspect(spec, &home)?, + home, + }) +} + +fn unknown_provider(token: &str) -> String { + let known: Vec<_> = yolo_agent_providers::builtin_install_specs() + .iter() + .map(|spec| spec.provider) + .collect(); + format!( + "{UNKNOWN_PROVIDER} `{token}`; {KNOWN_PROVIDERS}: {}", + known.join(", ") + ) +} + +fn confirm(pending: &[&Plan]) -> Result { + let names: Vec<_> = pending.iter().map(|plan| plan.spec.display_name).collect(); + print!("{}", render::confirmation(&names.join(", "))); + io::stdout().flush()?; + let mut answer = String::new(); + io::stdin().read_line(&mut answer)?; + Ok(matches!( + answer.trim().to_ascii_lowercase().as_str(), + "y" | "yes" + )) +} + +fn install_pending(pending: &[&Plan]) -> Result<()> { + for plan in pending { + let installation = install(plan.spec, &plan.home, &plan.preview.revision)?; + render::installed_lines(plan.spec, &installation) + .iter() + .for_each(|line| println!("{line}")); + if plan.spec.requires_provider_trust { + println!("{}: {TRUST_NOTE}", plan.spec.display_name); + } + } + Ok(()) +} diff --git a/clients/tui/cli/hook/src/lib.rs b/clients/tui/cli/hook/src/lib.rs new file mode 100644 index 00000000..6f937b94 --- /dev/null +++ b/clients/tui/cli/hook/src/lib.rs @@ -0,0 +1,26 @@ +mod command; +mod constants; +mod install; +mod render; +mod status; + +#[cfg(test)] +#[path = "../unit_tests/render.rs"] +mod render_tests; + +pub use command::HookCommand; + +use anyhow::Result; + +pub fn run(command: HookCommand) -> Result<()> { + match command { + HookCommand::Status { json } => status::run(json), + HookCommand::Install { + providers, + config_home, + yes, + dry_run, + json, + } => install::run(&providers, config_home.as_deref(), yes, dry_run, json), + } +} diff --git a/clients/tui/cli/hook/src/render.rs b/clients/tui/cli/hook/src/render.rs new file mode 100644 index 00000000..f084025f --- /dev/null +++ b/clients/tui/cli/hook/src/render.rs @@ -0,0 +1,99 @@ +use crate::constants::*; +use crate::install::Plan; +use crate::status::Row; +use serde_json::{Value, json}; +use yolo_hook_installation::{HookInstallSpec, Installation, IntegrationStatus}; + +pub(crate) fn label(status: IntegrationStatus) -> &'static str { + match status { + IntegrationStatus::Missing => STATUS_NOT_INSTALLED, + IntegrationStatus::Configured => STATUS_INSTALLED, + IntegrationStatus::Restricted => STATUS_DISABLED, + } +} + +fn trust_label(required: bool) -> &'static str { + if required { + TRUST_REQUIRED + } else { + TRUST_NOT_REQUIRED + } +} + +pub(crate) fn status_line(row: &Row) -> String { + format!( + "{}\t{}\t{}\t{}\t{}", + row.spec.provider, + row.spec.display_name, + label(row.preview.status), + trust_label(row.spec.requires_provider_trust), + row.preview.path.display() + ) +} + +pub(crate) fn status_json(row: &Row) -> Value { + json!({ + "provider": row.spec.provider, + "agent": row.spec.display_name, + "path": &row.preview.path, + "status": &row.preview.status, + "revision": &row.preview.revision, + "requires_provider_trust": row.spec.requires_provider_trust, + }) +} + +pub(crate) fn preview_json(plan: &Plan) -> Value { + json!({ + "provider": plan.spec.provider, + "agent": plan.spec.display_name, + "path": &plan.preview.path, + "status": &plan.preview.status, + "revision": &plan.preview.revision, + "requires_provider_trust": plan.spec.requires_provider_trust, + }) +} + +pub(crate) fn install_json(plan: &Plan, installation: &Installation) -> Value { + json!({ + "provider": plan.spec.provider, + "agent": plan.spec.display_name, + "path": &installation.preview.path, + "status": &installation.preview.status, + "changed": installation.changed, + "backup": &installation.backup, + "requires_provider_trust": plan.spec.requires_provider_trust, + }) +} + +pub(crate) fn preview_lines(plan: &Plan) -> Vec { + let mut lines = vec![ + plan.spec.display_name.to_string(), + format!("{FILE_PREFIX}{}", plan.preview.path.display()), + format!("{STATUS_PREFIX}{}", label(plan.preview.status)), + format!( + "{EVENTS_PREFIX}{}{EVENTS_SUFFIX}", + plan.spec.events.len() + ), + format!("{NOTE_PREFIX}{MANAGED_NOTE}"), + ]; + if plan.spec.requires_provider_trust { + lines.push(format!("{TRUST_PREFIX}{TRUST_NOTE}")); + } + lines +} + +pub(crate) fn installed_lines(spec: &HookInstallSpec, installation: &Installation) -> Vec { + let mut lines = vec![format!( + "{}{INSTALLED_SUFFIX}{}", + spec.display_name, + installation.preview.path.display() + )]; + if let Some(backup) = &installation.backup { + lines.push(format!("{BACKUP_PREFIX}{}", backup.display())); + } + lines +} + +pub(crate) fn confirmation(names: &str) -> String { + format!("{CONFIRM_PREFIX} {names}{CONFIRM_SUFFIX}") +} diff --git a/clients/tui/cli/hook/src/status.rs b/clients/tui/cli/hook/src/status.rs new file mode 100644 index 00000000..edbf1132 --- /dev/null +++ b/clients/tui/cli/hook/src/status.rs @@ -0,0 +1,41 @@ +use crate::{constants::*, render}; +use anyhow::Result; +use std::io::{self, Write}; +use yolo_agent_providers::builtin_install_specs; +use yolo_hook_installation::{HookInstallSpec, Preview, inspect, resolve_home}; +use yolo_paths::validation::validate_absolute; + +pub(crate) struct Row { + pub(crate) spec: &'static HookInstallSpec, + pub(crate) preview: Preview, +} + +pub(crate) fn run(json: bool) -> Result<()> { + let rows = rows()?; + let mut out = io::stdout().lock(); + if json { + let documents: Vec<_> = rows.iter().map(render::status_json).collect(); + serde_json::to_writer_pretty(&mut out, &documents)?; + writeln!(out)?; + } else { + writeln!(out, "{STATUS_HEADER}")?; + for row in &rows { + writeln!(out, "{}", render::status_line(row))?; + } + } + Ok(()) +} + +fn rows() -> Result> { + builtin_install_specs() + .into_iter() + .map(|spec| { + let home = resolve_home(spec, None, |name| std::env::var_os(name))?; + validate_absolute(&home)?; + Ok(Row { + spec, + preview: inspect(spec, &home)?, + }) + }) + .collect() +} diff --git a/clients/tui/cli/hook/unit_tests/render.rs b/clients/tui/cli/hook/unit_tests/render.rs new file mode 100644 index 00000000..5bc2f071 --- /dev/null +++ b/clients/tui/cli/hook/unit_tests/render.rs @@ -0,0 +1,96 @@ +use crate::install::Plan; +use crate::render; +use crate::status::Row; +use yolo_hook_installation::{ + HookEventSpec, HookInstallSpec, Installation, IntegrationStatus, Preview, +}; + +static SPEC: HookInstallSpec = HookInstallSpec { + provider: "demo", + display_name: "Demo Agent", + file_name: "settings.json", + subdirectory: "", + environment_name: "DEMO_HOME", + directory_name: ".demo", + hook_arguments: "provider-hook --provider demo", + managed_hook_commands: &["provider-hook --provider demo"], + events: &[HookEventSpec::new("SessionStart")], + restricted_keys: &[], + disabled_paths: &[], + requires_provider_trust: true, +}; + +fn preview(status: IntegrationStatus) -> Preview { + Preview { + provider: SPEC.provider.into(), + path: "/tmp/demo/settings.json".into(), + revision: "revision-a".into(), + status, + requires_provider_trust: SPEC.requires_provider_trust, + } +} + +fn plan(status: IntegrationStatus) -> Plan { + Plan { + spec: &SPEC, + home: "/tmp/demo".into(), + preview: preview(status), + } +} + +#[test] +fn status_labels_are_human_readable() { + assert_eq!(render::label(IntegrationStatus::Missing), "not installed"); + assert_eq!(render::label(IntegrationStatus::Configured), "installed"); + assert_eq!(render::label(IntegrationStatus::Restricted), "disabled"); +} + +#[test] +fn status_line_reports_trust_and_path() { + let row = Row { + spec: &SPEC, + preview: preview(IntegrationStatus::Missing), + }; + assert_eq!( + render::status_line(&row), + "demo\tDemo Agent\tnot installed\trequired\t/tmp/demo/settings.json" + ); +} + +#[test] +fn preview_explains_managed_scope_and_trust() { + let lines = render::preview_lines(&plan(IntegrationStatus::Missing)); + assert!(lines.contains(&"Demo Agent".to_string())); + assert!(lines.contains(&" file: /tmp/demo/settings.json".to_string())); + assert!(lines.contains(&" status: not installed".to_string())); + assert!(lines.contains(&" events: 1 managed callbacks".to_string())); + assert!(lines.iter().any(|line| line.starts_with(" trust:"))); +} + +#[test] +fn installed_lines_include_the_backup() { + let installation = Installation { + preview: preview(IntegrationStatus::Configured), + changed: true, + backup: Some("/tmp/demo/settings.json.backup".into()), + }; + let lines = render::installed_lines(&SPEC, &installation); + assert_eq!( + lines[0], + "Demo Agent: installed at /tmp/demo/settings.json" + ); + assert_eq!(lines[1], " backup: /tmp/demo/settings.json.backup"); +} + +#[test] +fn json_documents_expose_the_machine_status() { + let row = Row { + spec: &SPEC, + preview: preview(IntegrationStatus::Missing), + }; + let document = render::status_json(&row); + assert_eq!(document["provider"], "demo"); + assert_eq!(document["status"], "missing"); + assert_eq!(document["requires_provider_trust"], true); + assert_eq!(document["path"], "/tmp/demo/settings.json"); +} diff --git a/clients/tui/cli/platform/BUILD.bazel b/clients/tui/cli/platform/BUILD.bazel new file mode 100644 index 00000000..c334aad1 --- /dev/null +++ b/clients/tui/cli/platform/BUILD.bazel @@ -0,0 +1,30 @@ +load("@rules_rust//rust:defs.bzl", "rust_library") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "//clients/tui/app:yolo_tui_app", + "//core/crates/hub/events/protocol:yolo_hub_events_protocol", + "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", + "@crates//:anyhow", + "@crates//:clap", + "@crates//:serde_json", + "@crates//:tokio", + "@crates//:uuid", +] + +filegroup( + name = "architecture_check_data", + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], +) + +rust_library( + name = "yolo_tui_cli_platform", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_tui_cli_platform", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) diff --git a/clients/tui/cli/platform/src/constants.rs b/clients/tui/cli/platform/src/constants.rs new file mode 100644 index 00000000..dc0d6689 --- /dev/null +++ b/clients/tui/cli/platform/src/constants.rs @@ -0,0 +1,18 @@ +use std::time::Duration; + +pub(crate) const CONNECTION_GROUP: &str = "connection"; +pub(crate) const SOCKET_ARGUMENT: &str = "socket"; +pub(crate) const ENDPOINT_ARGUMENT: &str = "endpoint"; +pub(crate) const HUB_ARGUMENT: &str = "hub"; +pub(crate) const CATALOG_TIMEOUT: Duration = Duration::from_secs(10); +pub const PLATFORM_SNAPSHOT_UNSUPPORTED: &str = "this Hub does not advertise platform snapshots"; +pub const PLATFORM_EVENTS_UNSUPPORTED: &str = "this Hub does not advertise platform events"; +pub const PLATFORM_EVENTS_SUBSCRIBE_UNSUPPORTED: &str = + "this Hub does not advertise event subscriptions"; +pub const UNEXPECTED_EVENT_SUBSCRIPTION_REPLY: &str = "Hub returned a non-subscription reply"; +pub const UNEXPECTED_EVENTS_REPLY: &str = "Hub returned an unexpected platform event reply"; +pub const UNEXPECTED_SNAPSHOT_REPLY: &str = "Hub returned an unexpected snapshot reply"; +pub const DEFAULT_SNAPSHOT_LIMIT: u16 = 128; +pub const MAX_SNAPSHOT_LIMIT: u16 = 128; +pub const DEFAULT_EVENTS_LIMIT: u16 = 64; +pub const MAX_EVENTS_LIMIT: u16 = 256; diff --git a/clients/tui/cli/platform/src/lib.rs b/clients/tui/cli/platform/src/lib.rs new file mode 100644 index 00000000..11079274 --- /dev/null +++ b/clients/tui/cli/platform/src/lib.rs @@ -0,0 +1,15 @@ +mod constants; +mod platform; +mod platform_events; +mod platform_label; +mod platform_snapshot; + +use clap::ArgGroup; + +pub use platform::{PlatformCommand, run}; + +fn connection_group() -> ArgGroup { + ArgGroup::new(constants::CONNECTION_GROUP) + .args([constants::SOCKET_ARGUMENT, constants::ENDPOINT_ARGUMENT]) + .requires(constants::HUB_ARGUMENT) +} diff --git a/clients/tui/cli/src/platform.rs b/clients/tui/cli/platform/src/platform.rs similarity index 77% rename from clients/tui/cli/src/platform.rs rename to clients/tui/cli/platform/src/platform.rs index 5f7ad22c..7ece4c87 100644 --- a/clients/tui/cli/src/platform.rs +++ b/clients/tui/cli/platform/src/platform.rs @@ -1,21 +1,22 @@ -use crate::constants::DEFAULT_EVENTS_LIMIT; +use crate::constants::*; use clap::Subcommand; use uuid::Uuid; use yolo_tui_app::Options; #[derive(Debug, Subcommand)] -pub(crate) enum PlatformCommand { +pub enum PlatformCommand { /// Print hubs, terminals, agents, runs and workspaces in one document. - #[command(group(crate::arguments::connection_group()))] + #[command(group(crate::connection_group()))] Snapshot { + /// Print one JSON document. #[arg(long)] json: bool, /// Maximum entries per section (1-128). - #[arg(long, default_value_t = crate::constants::DEFAULT_SNAPSHOT_LIMIT)] + #[arg(long, default_value_t = DEFAULT_SNAPSHOT_LIMIT)] limit: u16, }, /// Print sequenced platform events after a cursor. - #[command(group(crate::arguments::connection_group()))] + #[command(group(crate::connection_group()))] Events { /// Stream epoch returned by an earlier call. #[arg(long, requires = "after")] @@ -29,12 +30,13 @@ pub(crate) enum PlatformCommand { /// Keep waiting for new events until interrupted. #[arg(long)] follow: bool, + /// Print one JSON document. #[arg(long)] json: bool, }, } -pub(crate) async fn run(options: &Options, command: PlatformCommand) -> anyhow::Result<()> { +pub async fn run(options: &Options, command: PlatformCommand) -> anyhow::Result<()> { match command { PlatformCommand::Snapshot { json, limit } => { crate::platform_snapshot::snapshot(options, json, limit).await diff --git a/clients/tui/cli/src/platform_events.rs b/clients/tui/cli/platform/src/platform_events.rs similarity index 100% rename from clients/tui/cli/src/platform_events.rs rename to clients/tui/cli/platform/src/platform_events.rs diff --git a/clients/tui/cli/src/platform_label.rs b/clients/tui/cli/platform/src/platform_label.rs similarity index 100% rename from clients/tui/cli/src/platform_label.rs rename to clients/tui/cli/platform/src/platform_label.rs diff --git a/clients/tui/cli/src/platform_snapshot.rs b/clients/tui/cli/platform/src/platform_snapshot.rs similarity index 100% rename from clients/tui/cli/src/platform_snapshot.rs rename to clients/tui/cli/platform/src/platform_snapshot.rs diff --git a/clients/tui/cli/src/arguments.rs b/clients/tui/cli/src/arguments.rs index ddac7882..eaf9f206 100644 --- a/clients/tui/cli/src/arguments.rs +++ b/clients/tui/cli/src/arguments.rs @@ -10,27 +10,38 @@ use yolo_tui_app::Options; #[command(about = ABOUT, after_help = CONTROLS)] #[command(group(connection_group()))] pub(crate) struct Arguments { - #[arg(long, global = true, conflicts_with = ENDPOINT_ARGUMENT)] + /// Existing local Hub entry socket; supply together with --hub. + #[arg(long, global = true, help_heading = "Connection", conflicts_with = ENDPOINT_ARGUMENT)] socket: Option, - #[arg(long, global = true, conflicts_with = SOCKET_ARGUMENT)] + /// Hub entry endpoint (local:); supply together with --hub. + #[arg(long, global = true, help_heading = "Connection", conflicts_with = SOCKET_ARGUMENT)] endpoint: Option, - #[arg(long, global = true, requires = CONNECTION_GROUP, conflicts_with = LOCAL_PROFILE_ARGUMENT)] + /// Identity of the Hub accepting this client connection. + #[arg(long, global = true, help_heading = "Connection", conflicts_with = LOCAL_PROFILE_ARGUMENT)] hub: Option, - #[arg(long, global = true, conflicts_with_all = [HUB_ARGUMENT, SOCKET_ARGUMENT, ENDPOINT_ARGUMENT])] + /// Local profile file; defaults to the current user profile. + #[arg(long, global = true, help_heading = "Connection", conflicts_with_all = [HUB_ARGUMENT, SOCKET_ARGUMENT, ENDPOINT_ARGUMENT])] local_profile: Option, - #[arg(long, global = true, requires_all = [ACCOUNT_KEY_ARGUMENT, CLIENT_KEY_ARGUMENT], conflicts_with_all = [HUB_ARGUMENT, SOCKET_ARGUMENT, ENDPOINT_ARGUMENT, LOCAL_PROFILE_ARGUMENT])] + /// Remote profile file; requires --account-key and --client-key. + #[arg(long, global = true, help_heading = "Connection", requires_all = [ACCOUNT_KEY_ARGUMENT, CLIENT_KEY_ARGUMENT], conflicts_with_all = [HUB_ARGUMENT, SOCKET_ARGUMENT, ENDPOINT_ARGUMENT, LOCAL_PROFILE_ARGUMENT])] remote_profile: Option, - #[arg(long, global = true, requires = REMOTE_PROFILE_ARGUMENT)] + /// Account key paired with --remote-profile. + #[arg(long, global = true, help_heading = "Connection", requires = REMOTE_PROFILE_ARGUMENT)] account_key: Option, - #[arg(long, global = true, requires = REMOTE_PROFILE_ARGUMENT)] + /// Client key paired with --remote-profile. + #[arg(long, global = true, help_heading = "Connection", requires = REMOTE_PROFILE_ARGUMENT)] client_key: Option, /// Attach a shared layout on the selected Hub; keyboard focus stays private. - #[arg(long, global = true)] + #[arg(long, global = true, help_heading = "Layout")] layout: Option, #[command(subcommand)] pub(crate) command: Option, } +fn connection_error(message: &'static str) -> anyhow::Error { + clap::Error::raw(clap::error::ErrorKind::MissingRequiredArgument, message).into() +} + pub(crate) fn connection_group() -> ArgGroup { ArgGroup::new(CONNECTION_GROUP) .args([SOCKET_ARGUMENT, ENDPOINT_ARGUMENT]) @@ -38,8 +49,29 @@ pub(crate) fn connection_group() -> ArgGroup { } impl Arguments { + /// The same root switches in the shape auxiliary commands consume. They + /// validate local-only access themselves. + pub(crate) fn connection_options(&self) -> yolo_command_cli::ConnectionOptions { + yolo_command_cli::ConnectionOptions { + socket: self.socket.clone(), + endpoint: self.endpoint.clone(), + hub: self.hub, + local_profile: self.local_profile.clone(), + remote_profile: self.remote_profile.clone(), + account_key: self.account_key.clone(), + client_key: self.client_key.clone(), + layout: self.layout, + } + } + pub(crate) fn selection(self) -> anyhow::Result { anyhow::ensure!(self.layout.is_none_or(|id| !id.is_nil()), INVALID_LAYOUT); + if !(self.hub.is_some() || (self.socket.is_none() && self.endpoint.is_none())) { + return Err(connection_error(yolo_command_cli::HUB_REQUIRES_CONNECTION)); + } + if !(self.hub.is_none() || self.socket.is_some() || self.endpoint.is_some()) { + return Err(connection_error(yolo_command_cli::CONNECTION_REQUIRES_HUB)); + } for path in [self.local_profile.as_ref()].into_iter().flatten() { yolo_paths::validation::validate_absolute(path)?; anyhow::ensure!( diff --git a/clients/tui/cli/src/audit.rs b/clients/tui/cli/src/audit.rs index 95ae24c4..801f6cf7 100644 --- a/clients/tui/cli/src/audit.rs +++ b/clients/tui/cli/src/audit.rs @@ -3,29 +3,47 @@ use crate::selection::Selection; use anyhow::{Result, bail}; use yolo_local_profile::LocalProfile; -pub(crate) async fn run( - selection: &Selection, - after: Option, - limit: u16, - json: bool, - merge: &[std::path::PathBuf], - trust: &[String], - identity: bool, - pull: Option, - output: Option, -) -> Result<()> { +#[derive(Debug, clap::Args)] +pub(crate) struct AuditArgs { + /// Only records after this sequence. + #[arg(long)] + pub(crate) after: Option, + /// Maximum records to print. + #[arg(long, default_value_t = crate::constants::DEFAULT_AUDIT_LIMIT)] + pub(crate) limit: u16, + /// Merge one or more audit JSONL files instead of reading the profile. + #[arg(long = "merge", value_name = "FILE")] + pub(crate) merge: Vec, + /// Trusted Hub identity for an authenticated merge: HUB_ID=PUBLIC_KEY. + #[arg(long, value_name = "HUB_ID=PUBLIC_KEY")] + pub(crate) trust: Vec, + /// Print this profile's Hub signing identity and exit. + #[arg(long)] + pub(crate) identity: bool, + /// Pull signed audit lines from this peer through the local entry Hub. + #[arg(long, value_name = "HUB_ID")] + pub(crate) pull: Option, + /// Write pulled lines to this file instead of stdout. + #[arg(long, value_name = "FILE", requires = "pull")] + pub(crate) output: Option, + /// Print one JSON document. + #[arg(long)] + pub(crate) json: bool, +} + +pub(crate) async fn run(selection: &Selection, args: AuditArgs) -> Result<()> { let profile = profile(selection)?; yolo_tui_audit::run( &profile, yolo_tui_audit::Request { - after, - limit, - json, - merge, - trust, - identity, - pull, - output, + after: args.after, + limit: args.limit, + json: args.json, + merge: &args.merge, + trust: &args.trust, + identity: args.identity, + pull: args.pull, + output: args.output, }, ) .await diff --git a/clients/tui/cli/src/aux.rs b/clients/tui/cli/src/aux.rs new file mode 100644 index 00000000..63f8008f --- /dev/null +++ b/clients/tui/cli/src/aux.rs @@ -0,0 +1,27 @@ +use crate::command::Command; +use yolo_command_cli::ConnectionOptions; + +/// Commands implemented by the auxiliary application crates. They parse here +/// with one root tree and dispatch before any terminal client is started. +pub(crate) enum RootCommand { + Tui(Command), + Aux(yolo_command_cli::Command), +} + +pub(crate) fn split(command: Command) -> RootCommand { + use yolo_command_cli::Command as Aux; + match command { + Command::Agent { command } => RootCommand::Aux(Aux::Agent { command }), + Command::Conversation { action } => RootCommand::Aux(Aux::Conversation { action }), + Command::Workspace { command } => RootCommand::Aux(Aux::Workspace { command }), + Command::Worktree { command } => RootCommand::Aux(Aux::Worktree { command }), + Command::Protocol { command } => RootCommand::Aux(Aux::Protocol { command }), + Command::Plugin { command } => RootCommand::Aux(Aux::Plugin { command }), + Command::Update(args) => RootCommand::Aux(Aux::Update(args)), + command => RootCommand::Tui(command), + } +} + +pub(crate) fn run(connection: ConnectionOptions, command: yolo_command_cli::Command) -> anyhow::Result<()> { + yolo_command_cli::run(connection, command) +} diff --git a/clients/tui/cli/src/client.rs b/clients/tui/cli/src/client.rs new file mode 100644 index 00000000..5cbada23 --- /dev/null +++ b/clients/tui/cli/src/client.rs @@ -0,0 +1,20 @@ +use clap::Subcommand; + +/// Local client operations. A client is one attached TUI process; it owns no +/// terminal and detaching never stops the shells it displays. +#[derive(Debug, Subcommand)] +pub(crate) enum ClientCommand { + /// List local terminal clients connected to the selected Hub. + #[command(group(crate::arguments::connection_group()))] + List { + /// Print one JSON document. + #[arg(long)] + json: bool, + }, + /// Disconnect a local terminal client; keep its shells and agents running. + #[command(group(crate::arguments::connection_group()))] + Detach { + /// Local client id; omit to choose from the attached clients. + client: Option, + }, +} diff --git a/clients/tui/cli/src/command.rs b/clients/tui/cli/src/command.rs index e6ce18a4..c338b4b7 100644 --- a/clients/tui/cli/src/command.rs +++ b/clients/tui/cli/src/command.rs @@ -1,14 +1,25 @@ +use crate::client::ClientCommand; use crate::constants::*; -use crate::platform::PlatformCommand; +use crate::layouts::LayoutCommand; +use yolo_tui_cli_platform::PlatformCommand; use crate::recipe::RecipeCommand; +use crate::server::ServerCommand; +use crate::terminal::TerminalCommand; use clap::Subcommand; +use yolo_command_cli::{ + AgentCommand, ConversationAction, PluginCommand, ProtocolCommand, UpdateArgs, WorkspaceCommand, + WorktreeCommand, +}; use yolo_tui_app::StartupAction; +/// The single root command tree. Daily shell entry stays first; every other +/// command is grouped by the noun it operates on. #[derive(Debug, Subcommand)] pub(crate) enum Command { /// Open a new tab with a normal shell. Run agents directly in that shell. #[command(group(crate::arguments::connection_group()))] New { + /// Working directory for the new shell; defaults to the current directory. #[arg(long)] cwd: Option, /// Label for the terminal; selectors need the label to be unambiguous. @@ -17,60 +28,53 @@ pub(crate) enum Command { }, /// Attach an existing terminal by ID, address, or unique name; never creates a shell. #[command(group(crate::arguments::connection_group()))] - Attach { terminal: Option }, - /// Disconnect a local terminal client; keep its shells and agents running. - #[command(group(crate::arguments::connection_group()))] - Detach { client: Option }, - /// List terminals visible from the selected Hub. + Attach { + /// Terminal id, address, or unique name. + terminal: Option, + }, + /// Open the terminal picker to attach an existing shell without creating one. + #[command(name = TERMINALS_COMMAND, group(crate::arguments::connection_group()))] + Terminals, + /// List, read, archive or terminate Hub-owned terminals. #[command(group(crate::arguments::connection_group()))] - List { - #[arg(long)] - json: bool, - /// Include archived terminals from history. - #[arg(long)] - all: bool, + Terminal { + #[command(subcommand)] + command: TerminalCommand, }, - /// List local terminal clients connected to the selected Hub. + /// List or detach local terminal clients. #[command(group(crate::arguments::connection_group()))] - Clients { - #[arg(long)] - json: bool, + Client { + #[command(subcommand)] + command: ClientCommand, }, - /// Archive an exited terminal; its history keeps the row. + /// List shared layouts. #[command(group(crate::arguments::connection_group()))] - Archive { - /// Terminal id, address, or unique name. - terminal: String, - #[arg(long)] - json: bool, + Layout { + #[command(subcommand)] + command: LayoutCommand, }, - /// End a terminal and every program running in it. + /// Manage machines and import their agents; print this machine's Hub identity. #[command(group(crate::arguments::connection_group()))] - Terminate { - /// Terminal id, address, or unique name. - terminal: String, - /// Kill programs that do not exit after a graceful request. - #[arg(long)] - force: bool, - #[arg(long)] - json: bool, + Machine { + #[command(subcommand)] + command: yolo_tui_hub_cli::MachineCommand, }, - /// List shared layout IDs; use --layout to open one in an independent client. + /// Inspect and control agents through the public Hub protocol. #[command(group(crate::arguments::connection_group()))] - Layouts { - #[arg(long)] - json: bool, + Agent { + #[command(subcommand)] + command: AgentCommand, }, - /// Open the terminal picker to attach an existing shell without creating one. - #[command(name = TERMINALS_COMMAND, group(crate::arguments::connection_group()))] - Terminals, - /// Print the visible screen text of a terminal (non-authoritative). + /// Install the hooks that let Yolo observe terminal agents. + Hook { + #[command(subcommand)] + command: yolo_tui_cli_hook::HookCommand, + }, + /// Start, stop, restart or inspect the local Host and Hub. #[command(group(crate::arguments::connection_group()))] - Screen { - /// Terminal id, address, or unique name. - terminal: String, - #[arg(long)] - json: bool, + Server { + #[command(subcommand)] + command: ServerCommand, }, /// Inspect the platform snapshot or its sequenced events. #[command(group(crate::arguments::connection_group()))] @@ -80,37 +84,46 @@ pub(crate) enum Command { }, /// Read the local Hub's durable authority audit log, or merge exported logs. #[command(group(crate::arguments::connection_group()))] - Audit { - /// Only records after this sequence. - #[arg(long)] - after: Option, - /// Maximum records to print. - #[arg(long, default_value_t = crate::constants::DEFAULT_AUDIT_LIMIT)] - limit: u16, - /// Merge one or more audit JSONL files instead of reading the profile. - #[arg(long = "merge", value_name = "FILE")] - merge: Vec, - /// Trusted Hub identity for an authenticated merge: HUB_ID=PUBLIC_KEY. - #[arg(long, value_name = "HUB_ID=PUBLIC_KEY")] - trust: Vec, - /// Print this profile's Hub signing identity and exit. - #[arg(long)] - identity: bool, - /// Pull signed audit lines from this peer through the local entry Hub. - #[arg(long, value_name = "HUB_ID")] - pull: Option, - /// Write pulled lines to this file instead of stdout. - #[arg(long, value_name = "FILE", requires = "pull")] - output: Option, - #[arg(long)] - json: bool, - }, + Audit(crate::audit::AuditArgs), /// Validate or start a bounded multi-agent recipe file. #[command(group(crate::arguments::connection_group()))] Recipe { #[command(subcommand)] command: RecipeCommand, }, + /// Read native agent conversations through the public Hub protocol. + #[command(group(crate::arguments::connection_group()))] + Conversation { + #[command(subcommand)] + action: ConversationAction, + }, + /// Manage durable workspace metadata through the public Hub. + #[command(group(crate::arguments::connection_group()))] + Workspace { + #[command(subcommand)] + command: WorkspaceCommand, + }, + /// Manage git worktrees through the public Hub. + #[command(group(crate::arguments::connection_group()))] + Worktree { + #[command(subcommand)] + command: WorktreeCommand, + }, + /// Inspect the public Yolo protocol contract. + #[command(group(crate::arguments::connection_group()))] + Protocol { + #[command(subcommand)] + command: ProtocolCommand, + }, + /// Manage trusted user-level plugins. + #[command(group(crate::arguments::connection_group()))] + Plugin { + #[command(subcommand)] + command: PluginCommand, + }, + /// Check, install and activate Yolo releases. + #[command(group(crate::arguments::connection_group()))] + Update(UpdateArgs), } pub(crate) fn startup(command: Option, remote: bool) -> anyhow::Result { diff --git a/clients/tui/cli/src/constants.rs b/clients/tui/cli/src/constants.rs index b50fc928..56c204f5 100644 --- a/clients/tui/cli/src/constants.rs +++ b/clients/tui/cli/src/constants.rs @@ -6,13 +6,16 @@ pub const ABOUT: &str = pub const CONTROLS: &str = concat!( "Ctrl-B c opens a tab with a shell; v or % splits right; - or \" splits down.\n", "Ctrl-B n/p switches tabs; o or arrows focuses a pane; w opens the tab manager.\n", - "Ctrl-B s focuses the Agent runs/Hubs sidebar; b toggles it; g opens the existing-terminal picker.\n", + "Ctrl-B s focuses the machine tree sidebar; b toggles it; g opens the existing-terminal picker.\n", "Ctrl-B x closes a pane view; X closes a tab view. Shells keep running; g reopens them.\n", "Ctrl-B R opens focused-terminal recovery; Ctrl-B Ctrl-B sends a literal Ctrl-B.\n", "Ctrl-B m switches an agent pane between Terminal and Chat; Escape returns to Terminal.\n", "Ctrl-B d detaches. Ordinary keys (including q) go to the shell.\n", "Detaching leaves shells and agents running. An agent can finish while its shell remains usable.\n", - "Also: yolo agent --help · yolo protocol describe · yolo conversation --help." + "Commands: yolo terminal|client|layout for terminals, yolo server for local services,\n", + "yolo machine|agent for machines and agents, yolo hook to install agent hooks.\n", + "Root connection options may precede or follow a command; run yolo \n", + "--help for details." ); pub const TERMINALS_COMMAND: &str = "terminals"; pub const CATALOG_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); @@ -48,21 +51,17 @@ pub const PRIVATE_REMOTE_FILE_REQUIRED: &str = pub const CONNECTION_GROUP: &str = "connection"; pub const INVALID_PATH: &str = "Local profile paths must contain valid non-control text"; pub(crate) const INVALID_LAYOUT: &str = "Layout identity must be a non-nil UUID"; -pub const PLATFORM_SNAPSHOT_UNSUPPORTED: &str = "this Hub does not advertise platform snapshots"; -pub const PLATFORM_EVENTS_UNSUPPORTED: &str = "this Hub does not advertise platform events"; -pub const PLATFORM_EVENTS_SUBSCRIBE_UNSUPPORTED: &str = - "this Hub does not advertise event subscriptions"; -pub const UNEXPECTED_EVENT_SUBSCRIPTION_REPLY: &str = "Hub returned a non-subscription reply"; -pub const UNEXPECTED_EVENTS_REPLY: &str = "Hub returned an unexpected platform event reply"; pub const TERMINAL_TEXT_UNSUPPORTED: &str = "this Hub does not advertise terminal text reads"; pub const TERMINAL_TEXT_NO_RUNTIME: &str = "the selected terminal has no live runtime"; pub const UNEXPECTED_TEXT_REPLY: &str = "Hub returned an unexpected terminal text reply"; -pub const UNEXPECTED_SNAPSHOT_REPLY: &str = "Hub returned an unexpected snapshot reply"; -pub const DEFAULT_SNAPSHOT_LIMIT: u16 = 128; -pub const MAX_SNAPSHOT_LIMIT: u16 = 128; -pub const DEFAULT_EVENTS_LIMIT: u16 = 64; -pub const MAX_EVENTS_LIMIT: u16 = 256; pub const AUDIT_LOCAL_ONLY: &str = "audit reads require the local profile"; +pub const MANAGEMENT_LOCAL_ONLY: &str = + "Machine commands require the local Hub (use --local-profile or --socket/--hub)"; +pub const STARTING_LOCAL_SERVICES: &str = "ensuring local Host and Hub are running..."; +pub const LIFECYCLE_LOCAL_ONLY: &str = + "local service lifecycle commands operate on the local profile only"; +pub const MANAGEMENT_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(10); +/// Pairing spans an SSH handshake and a remote configuration activation. pub const RECIPE_SCHEMA_VERSION: u16 = 1; pub const SCHEMA_VERSION_FIELD: &str = "schema_version"; diff --git a/clients/tui/cli/src/detach.rs b/clients/tui/cli/src/detach.rs index 2f1f6484..dd4e0f8c 100644 --- a/clients/tui/cli/src/detach.rs +++ b/clients/tui/cli/src/detach.rs @@ -1,5 +1,5 @@ use crate::{ - command::Command, + client::ClientCommand, constants::{CLIENT_HEADER, CLIENT_KIND_CLIENT, CLIENT_KIND_PRIMARY, NO_LOCAL_HUB}, selection::Selection, }; @@ -10,10 +10,10 @@ use yolo_tui_tab::ClientDirectory; const NO_CLIENT: &str = "No attached terminal client was found"; const MANY_CLIENTS: &str = - "Several terminal clients are attached; use yolo clients, then yolo detach "; + "Several terminal clients are attached; use yolo client list, then yolo client detach "; const DETACHED: &str = "Detached terminal client; shells and agents are still running"; -/// One attached client as `yolo clients` reports it. The Hub-bound client +/// One attached client as `yolo client list` reports it. The Hub-bound client /// directory is addressed by the Hub id, so its kind makes that visible /// instead of presenting a Hub id as an ordinary client id. pub(crate) struct ClientRow { @@ -21,7 +21,7 @@ pub(crate) struct ClientRow { pub(crate) kind: &'static str, } -pub(crate) async fn run(selection: Selection, command: Command) -> Result<()> { +pub(crate) async fn run(selection: Selection, command: ClientCommand) -> Result<()> { let hub = identity(selection)?; let mut active = Vec::new(); let mut handles = std::collections::BTreeMap::new(); @@ -32,11 +32,11 @@ pub(crate) async fn run(selection: Selection, command: Command) -> Result<()> { } } match command { - Command::Clients { json } => { + ClientCommand::List { json } => { let rows = client_rows(&active, hub); print!("{}", clients_output(&rows, json)); } - Command::Detach { client } => { + ClientCommand::Detach { client } => { let selected = choose(&active, client)?; handles .remove(&selected.id) diff --git a/clients/tui/cli/src/layouts.rs b/clients/tui/cli/src/layouts.rs index be6f9fef..0f14bfe1 100644 --- a/clients/tui/cli/src/layouts.rs +++ b/clients/tui/cli/src/layouts.rs @@ -1,7 +1,21 @@ +use clap::Subcommand; use std::io::{self, Write}; use yolo_hub_rpc_protocol::{Command, Reply}; use yolo_tui_app::Options; +/// Shared-layout inspection. Opening a layout stays the root `--layout` +/// switch because keyboard focus is always private to the client. +#[derive(Debug, Subcommand)] +pub(crate) enum LayoutCommand { + /// List shared layout IDs; use --layout to open one in an independent client. + #[command(group(crate::arguments::connection_group()))] + List { + /// Print one JSON document. + #[arg(long)] + json: bool, + }, +} + const HEADER: &str = "LAYOUT_ID\tREVISION\tTABS\tPANES"; const INVALID_REPLY: &str = "Hub returned an unexpected layout listing"; diff --git a/clients/tui/cli/src/lib.rs b/clients/tui/cli/src/lib.rs index 83ea65a2..15baf8f3 100644 --- a/clients/tui/cli/src/lib.rs +++ b/clients/tui/cli/src/lib.rs @@ -15,27 +15,35 @@ impl yolo_builtin_client_host::BuiltinClientEntrypoint for TuiBuiltinClient { mod arguments; mod audit; +mod aux; +mod client; mod command; mod constants; mod detach; mod layouts; mod lifecycle; mod listing; -mod platform; -mod platform_events; -mod platform_label; -mod platform_snapshot; mod recipe; mod remote; mod screen; mod selection; +mod server; +mod terminal; #[cfg(test)] #[path = "../unit_tests/lib.rs"] mod unit_tests; pub fn run_from(arguments: impl IntoIterator) -> anyhow::Result<()> { let mut arguments = Arguments::try_parse_from(arguments)?; - let command = arguments.command.take(); + let command = match arguments.command.take() { + Some(command) => match aux::split(command) { + aux::RootCommand::Aux(command) => { + return aux::run(arguments.connection_options(), command); + } + aux::RootCommand::Tui(command) => Some(command), + }, + None => None, + }; let selection = arguments.selection()?; tokio::runtime::Runtime::new()?.block_on(selection.run(command)) } diff --git a/clients/tui/cli/src/lifecycle.rs b/clients/tui/cli/src/lifecycle.rs index a0275c80..56707732 100644 --- a/clients/tui/cli/src/lifecycle.rs +++ b/clients/tui/cli/src/lifecycle.rs @@ -1,17 +1,20 @@ -use crate::command::Command; +use crate::terminal::TerminalCommand as CliTerminalCommand; use crate::constants::*; use anyhow::{Context, Result, ensure}; use std::io::{self, Write}; use yolo_hub_rpc_protocol::{Command as HubCommand, Reply}; use yolo_hub_terminal_protocol::{ - RuntimeStatus, TerminalAction, TerminalCommand, TerminalSelection, TerminalSummary, + RuntimeStatus, TerminalAction, TerminalCommand as HubTerminalCommand, TerminalSelection, + TerminalSummary, }; use yolo_tui_app::Options; -pub(crate) async fn run(options: &Options, command: Command) -> Result<()> { +pub(crate) async fn run(options: &Options, command: CliTerminalCommand) -> Result<()> { let (selector, action, json) = match command { - Command::Archive { terminal, json } => (terminal, TerminalAction::Archive {}, json), - Command::Terminate { + CliTerminalCommand::Archive { terminal, json } => { + (terminal, TerminalAction::Archive {}, json) + } + CliTerminalCommand::Terminate { terminal, force, json, @@ -25,7 +28,7 @@ pub(crate) async fn run(options: &Options, command: Command) -> Result<()> { let session = select(&catalog.terminals, &selector)?; let fields = session.fields(); let command = HubCommand::Terminal { - command: TerminalCommand { + command: HubTerminalCommand { request_id: uuid::Uuid::new_v4(), selection: TerminalSelection { address: fields.address, diff --git a/clients/tui/cli/src/recipe/mod.rs b/clients/tui/cli/src/recipe/mod.rs index 78369efb..3525e3a2 100644 --- a/clients/tui/cli/src/recipe/mod.rs +++ b/clients/tui/cli/src/recipe/mod.rs @@ -33,17 +33,21 @@ pub(crate) enum RecipeCommand { #[derive(Debug, Args)] pub(crate) struct StartArgs { + /// Recipe JSON file to start. pub file: PathBuf, /// Stop already-started agents if a later start fails. #[arg(long)] pub stop_on_failure: bool, + /// Print one JSON document. #[arg(long)] pub json: bool, } #[derive(Debug, Args)] pub(crate) struct ShowArgs { + /// Recipe JSON file to validate and print. pub file: PathBuf, + /// Print one JSON document. #[arg(long)] pub json: bool, } diff --git a/clients/tui/cli/src/selection.rs b/clients/tui/cli/src/selection.rs index f82272a7..60f605c6 100644 --- a/clients/tui/cli/src/selection.rs +++ b/clients/tui/cli/src/selection.rs @@ -1,7 +1,11 @@ use crate::command::Command; +use crate::constants::{LIFECYCLE_LOCAL_ONLY, MANAGEMENT_LOCAL_ONLY, MANAGEMENT_TIMEOUT}; +use crate::layouts::LayoutCommand; use crate::recipe::RecipeCommand; -use std::path::PathBuf; +use std::path::{Path, PathBuf}; use yolo_hub_client::HubEndpoint; +use yolo_hub_state::HubState; +use yolo_local_profile::LocalProfile; use yolo_tui_app::Options; pub(crate) enum Selection { @@ -14,45 +18,30 @@ pub(crate) enum Selection { impl Selection { pub async fn run(self, command: Option) -> anyhow::Result<()> { - if let Some(Command::Audit { - after, - limit, - merge, - trust, - identity, - pull, - output, - json, - }) = &command - { - return crate::audit::run( - &self, - *after, - *limit, - *json, - merge, - trust, - *identity, - *pull, - output.clone(), - ) - .await; - } - if let Some(Command::Recipe { - command: RecipeCommand::Show(args), - }) = &command - { - return crate::recipe::show(args); - } - if matches!( - command, - Some(Command::Detach { .. } | Command::Clients { .. }) - ) { - return crate::detach::run(self, command.expect("matched command")).await; - } + let command = match command { + Some(Command::Server { command }) => { + let profile = self.local_profile()?; + return command.run(&profile).await; + } + Some(Command::Machine { command }) => { + let (socket, entry) = self.local_hub_endpoint().await?; + let deadline = tokio::time::Instant::now() + MANAGEMENT_TIMEOUT; + return yolo_tui_hub_cli::run(&socket, entry, &command, deadline).await; + } + Some(Command::Audit(args)) => return crate::audit::run(&self, args).await, + Some(Command::Hook { command }) => return yolo_tui_cli_hook::run(command), + Some(Command::Recipe { + command: RecipeCommand::Show(args), + }) => return crate::recipe::show(&args), + Some(Command::Client { command }) => return crate::detach::run(self, command).await, + other => other, + }; match self { Self::Explicit(options) => run(options, std::future::pending(), command).await, Self::Local { profile, layout } => { + if command.is_some() { + notice_if_starting(profile.as_deref())?; + } let mut startup = yolo_tui_local_startup::automatic(profile.as_deref()).await?; let endpoint = HubEndpoint::local(startup.connection.socket_path.clone()); endpoint.validate()?; @@ -65,6 +54,47 @@ impl Selection { } } } + + async fn local_hub_endpoint(&self) -> anyhow::Result<(PathBuf, yolo_hub_identity::HubId)> { + match self { + Self::Explicit(options) => { + let Some(socket) = options.endpoint.local_socket() else { + anyhow::bail!("{}", MANAGEMENT_LOCAL_ONLY); + }; + Ok((socket.to_path_buf(), options.hub)) + } + Self::Local { profile, .. } => { + notice_if_starting(profile.as_deref())?; + let startup = yolo_tui_local_startup::automatic(profile.as_deref()).await?; + Ok(( + startup.connection.socket_path.clone(), + startup.connection.entry_hub, + )) + } + } + } + + fn local_profile(&self) -> anyhow::Result { + match self { + Self::Local { profile, .. } => match profile { + Some(path) => LocalProfile::load(path), + None => yolo_local_profile::current(), + }, + Self::Explicit(_) => anyhow::bail!("{}", LIFECYCLE_LOCAL_ONLY), + } + } +} + +fn notice_if_starting(profile_path: Option<&Path>) -> anyhow::Result<()> { + let profile = match profile_path { + Some(path) => LocalProfile::load(path)?, + None => yolo_local_profile::current()?, + }; + let running = HubState::socket_path_for(&profile.state).is_ok_and(|path| path.exists()); + if !running { + eprintln!("{}", crate::constants::STARTING_LOCAL_SERVICES); + } + Ok(()) } async fn run( @@ -73,21 +103,14 @@ async fn run( command: Option, ) -> anyhow::Result<()> { match command { - Some(Command::List { json, all }) => crate::listing::run(&options, json, all).await, - Some(Command::Layouts { json }) => crate::layouts::run(&options, json).await, - Some(Command::Platform { command }) => crate::platform::run(&options, command).await, - Some(Command::Screen { terminal, json }) => { - crate::screen::run(&options, terminal, json).await - } + Some(Command::Terminal { command }) => command.run(&options).await, + Some(Command::Layout { + command: LayoutCommand::List { json }, + }) => crate::layouts::run(&options, json).await, + Some(Command::Platform { command }) => yolo_tui_cli_platform::run(&options, command).await, Some(Command::Recipe { command: RecipeCommand::Start(args), }) => crate::recipe::start(&options, &args).await, - Some(Command::Recipe { - command: RecipeCommand::Show(_), - }) => unreachable!("recipe show is dispatched before connection setup"), - Some(command @ (Command::Archive { .. } | Command::Terminate { .. })) => { - crate::lifecycle::run(&options, command).await - } command => { let action = crate::command::startup(command, options.endpoint.local_socket().is_none())?; diff --git a/clients/tui/cli/src/server.rs b/clients/tui/cli/src/server.rs new file mode 100644 index 00000000..9e1b56bb --- /dev/null +++ b/clients/tui/cli/src/server.rs @@ -0,0 +1,36 @@ +use clap::Subcommand; +use yolo_local_profile::LocalProfile; +use yolo_tui_service::FormatOptions; + +/// Local Host and Hub lifecycle. These commands never create, attach or stop a +/// terminal; detaching a client is `yolo client detach`. +#[derive(Debug, Subcommand)] +pub(crate) enum ServerCommand { + /// Start the local Host and Hub. + #[command(group(crate::arguments::connection_group()))] + Start(FormatOptions), + /// Stop the local Host and Hub; terminals and PTYs keep running. + #[command(group(crate::arguments::connection_group()))] + Stop(FormatOptions), + /// Restart the local Host and Hub. + #[command(group(crate::arguments::connection_group()))] + Restart(FormatOptions), + /// Show installed and running local service status. + #[command(group(crate::arguments::connection_group()))] + Status(FormatOptions), + /// Print installed and running local service versions. + #[command(group(crate::arguments::connection_group()))] + Version(FormatOptions), +} + +impl ServerCommand { + pub(crate) async fn run(self, profile: &LocalProfile) -> anyhow::Result<()> { + match self { + Self::Start(options) => yolo_tui_service::start_command(profile, options).await, + Self::Stop(options) => yolo_tui_service::stop_command(profile, options).await, + Self::Restart(options) => yolo_tui_service::restart_command(profile, options).await, + Self::Status(options) => yolo_tui_service::status_command(profile, options).await, + Self::Version(options) => yolo_tui_service::version_command(profile, options).await, + } + } +} diff --git a/clients/tui/cli/src/terminal.rs b/clients/tui/cli/src/terminal.rs new file mode 100644 index 00000000..f7752fbf --- /dev/null +++ b/clients/tui/cli/src/terminal.rs @@ -0,0 +1,60 @@ +use clap::Subcommand; +use yolo_tui_app::Options; + +/// Terminal-catalogue operations. These address Hub-owned PTYs, never the +/// client that displays them. +#[derive(Debug, Subcommand)] +pub(crate) enum TerminalCommand { + /// List terminals visible from the selected Hub. + #[command(group(crate::arguments::connection_group()))] + List { + /// Print one JSON document. + #[arg(long)] + json: bool, + /// Include archived terminals from history. + #[arg(long)] + all: bool, + }, + /// Print the visible screen text of a terminal (non-authoritative). + #[command(group(crate::arguments::connection_group()))] + Screen { + /// Terminal id, address, or unique name. + terminal: String, + /// Print one JSON document. + #[arg(long)] + json: bool, + }, + /// Archive an exited terminal; its history keeps the row. + #[command(group(crate::arguments::connection_group()))] + Archive { + /// Terminal id, address, or unique name. + terminal: String, + /// Print one JSON document. + #[arg(long)] + json: bool, + }, + /// End a terminal and every program running in it. + #[command(group(crate::arguments::connection_group()))] + Terminate { + /// Terminal id, address, or unique name. + terminal: String, + /// Kill programs that do not exit after a graceful request. + #[arg(long)] + force: bool, + /// Print one JSON document. + #[arg(long)] + json: bool, + }, +} + +impl TerminalCommand { + pub(crate) async fn run(self, options: &Options) -> anyhow::Result<()> { + match self { + Self::List { json, all } => crate::listing::run(options, json, all).await, + Self::Screen { terminal, json } => crate::screen::run(options, terminal, json).await, + Self::Archive { .. } | Self::Terminate { .. } => { + crate::lifecycle::run(options, self).await + } + } + } +} diff --git a/clients/tui/cli/test_support/constants.rs b/clients/tui/cli/test_support/constants.rs index 1af143f6..8d170a25 100644 --- a/clients/tui/cli/test_support/constants.rs +++ b/clients/tui/cli/test_support/constants.rs @@ -1,9 +1,7 @@ pub use yolo_launch_contract::EXECUTABLE_NAME as PROGRAM; pub const HELP_ARGUMENT: &str = "--help"; pub const LAYOUT_ARGUMENT: &str = "--layout"; -pub const JSON_ARGUMENT: &str = "--json"; pub const ALL_ARGUMENT: &str = "--all"; -pub const LAYOUTS_COMMAND: &str = "layouts"; pub const LAYOUT_ID: &str = "74d01669-13e2-4d8d-a7ab-a8b5a75a8d0f"; pub const NIL_UUID: &str = "00000000-0000-0000-0000-000000000000"; pub const VERSION_ARGUMENT: &str = "--version"; @@ -38,13 +36,36 @@ pub const RETIRED_PICKER_COMMAND: &str = "sessions"; pub const RETIRED_TABS_COMMAND: &str = "tabs"; pub const RETIRED_PANELS_COMMAND: &str = "panels"; pub const ATTACH_COMMAND: &str = "attach"; +pub const AGENT_COMMAND: &str = "agent"; pub const DETACH_COMMAND: &str = "detach"; pub const NEW_COMMAND: &str = "new"; pub const LIST_COMMAND: &str = "list"; -pub const CLIENTS_COMMAND: &str = "clients"; pub const TERMINAL_SELECTOR_LABEL: &str = "[TERMINAL]"; pub const DETACH_SHORTCUT: &str = "Ctrl-B d"; pub const ARCHIVE_COMMAND: &str = "archive"; pub const TERMINATE_COMMAND: &str = "terminate"; pub const FORCE_ARGUMENT: &str = "--force"; pub const NAME: &str = "alpha"; +pub const WORKSPACE_ENTRY: &str = "workspace"; +pub const STATUS_COMMAND: &str = "status"; +pub const INSTALL_COMMAND: &str = "install"; +pub const YES_ARGUMENT: &str = "--yes"; +// Not named `_COMMAND`: the literal would enter the architecture registry and +// collide with generic "version" fields outside the CLI. +pub const VERSION_ENTRY: &str = "version"; +pub const START_COMMAND: &str = "start"; +pub const STOP_COMMAND: &str = "stop"; +pub const RESTART_COMMAND: &str = "restart"; +pub const MACHINE_COMMAND: &str = "machine"; +pub const CONSUMERS_COMMAND: &str = "consumers"; +pub const ADD_COMMAND: &str = "add"; +pub const REMOVE_COMMAND: &str = "remove"; +pub const PIN_COMMAND: &str = "pin"; +pub const IDENTITY_COMMAND: &str = "identity"; +pub const PIN_ONLY_ARGUMENT: &str = "--pin-only"; +pub const SSH_ARGUMENT: &str = "--ssh"; +pub const REMOTE_SOCKET_ARGUMENT: &str = "--remote-socket"; +pub const SSH_TARGET: &str = "remote-linux"; +pub const REMOTE_SOCKET: &str = "/home/stone/.local/state/yolo/hub/peer.sock"; +pub const EXPORT_LOCAL_ARGUMENT: &str = "--export-local"; +pub const JSON_ARGUMENT: &str = "--json"; diff --git a/clients/tui/cli/unit_tests/aux_commands.rs b/clients/tui/cli/unit_tests/aux_commands.rs new file mode 100644 index 00000000..61bcf5c4 --- /dev/null +++ b/clients/tui/cli/unit_tests/aux_commands.rs @@ -0,0 +1,37 @@ +use super::constants::*; +use crate::arguments::Arguments; +use crate::command::Command; +use clap::Parser; +use std::ffi::OsString; + +#[test] +fn auxiliary_commands_share_the_single_root_tree() { + let arguments = Arguments::try_parse_from([ + PROGRAM, + AGENT_COMMAND, + LIST_COMMAND, + LOCAL_PROFILE_ARGUMENT, + PROFILE, + ]) + .unwrap(); + assert!(matches!(arguments.command, Some(Command::Agent { .. }))); + let error = crate::run_from( + [ + PROGRAM, + "plugin", + LIST_COMMAND, + SOCKET_ARGUMENT, + SOCKET, + HUB_ARGUMENT, + HUB, + ] + .map(OsString::from), + ) + .unwrap_err(); + assert!( + error + .to_string() + .contains("does not use Hub connection options"), + "{error}" + ); +} diff --git a/clients/tui/cli/unit_tests/clients.rs b/clients/tui/cli/unit_tests/clients.rs index fce34c86..9df4e05a 100644 --- a/clients/tui/cli/unit_tests/clients.rs +++ b/clients/tui/cli/unit_tests/clients.rs @@ -1,6 +1,8 @@ use super::constants::*; use crate::constants::{CLIENT_HEADER, CLIENT_KIND_CLIENT, CLIENT_KIND_PRIMARY}; use crate::detach::{client_rows, clients_output}; +use crate::client::ClientCommand; +use crate::terminal::TerminalCommand; use crate::{arguments::Arguments, command::Command}; use clap::Parser; use std::path::PathBuf; @@ -57,32 +59,48 @@ fn client_ids_remain_addressable_from_the_printed_table() { } #[test] -fn list_and_clients_expose_history_and_machine_readable_modes() { - let parsed = - Arguments::try_parse_from([PROGRAM, LIST_COMMAND, ALL_ARGUMENT, JSON_ARGUMENT]).unwrap(); +fn terminal_and_client_commands_expose_history_and_machine_readable_modes() { + let parsed = Arguments::try_parse_from([ + PROGRAM, + "terminal", + LIST_COMMAND, + ALL_ARGUMENT, + JSON_ARGUMENT, + ]) + .unwrap(); assert!(matches!( parsed.command, - Some(Command::List { - json: true, - all: true + Some(Command::Terminal { + command: TerminalCommand::List { + json: true, + all: true + } }) )); - let parsed = Arguments::try_parse_from([PROGRAM, LIST_COMMAND]).unwrap(); + let parsed = + Arguments::try_parse_from([PROGRAM, "terminal", LIST_COMMAND]).unwrap(); assert!(matches!( parsed.command, - Some(Command::List { - json: false, - all: false + Some(Command::Terminal { + command: TerminalCommand::List { + json: false, + all: false + } }) )); - let parsed = Arguments::try_parse_from([PROGRAM, CLIENTS_COMMAND, JSON_ARGUMENT]).unwrap(); + let parsed = + Arguments::try_parse_from([PROGRAM, "client", LIST_COMMAND, JSON_ARGUMENT]).unwrap(); assert!(matches!( parsed.command, - Some(Command::Clients { json: true }) + Some(Command::Client { + command: ClientCommand::List { json: true } + }) )); - let parsed = Arguments::try_parse_from([PROGRAM, CLIENTS_COMMAND]).unwrap(); + let parsed = Arguments::try_parse_from([PROGRAM, "client", LIST_COMMAND]).unwrap(); assert!(matches!( parsed.command, - Some(Command::Clients { json: false }) + Some(Command::Client { + command: ClientCommand::List { json: false } + }) )); } diff --git a/clients/tui/cli/unit_tests/commands.rs b/clients/tui/cli/unit_tests/commands.rs index 8141f90d..2d34af57 100644 --- a/clients/tui/cli/unit_tests/commands.rs +++ b/clients/tui/cli/unit_tests/commands.rs @@ -5,6 +5,7 @@ use crate::{ command::{self, Command}, }; use clap::{CommandFactory, Parser}; +use std::ffi::OsString; use yolo_tui_app::StartupAction; #[test] @@ -12,16 +13,23 @@ fn public_help_explains_tabs_and_detaching_without_workspaces() { let help = Arguments::command().render_long_help().to_string(); for command in [ ATTACH_COMMAND, - DETACH_COMMAND, NEW_COMMAND, - LIST_COMMAND, - CLIENTS_COMMAND, TERMINALS_COMMAND, + "terminal", + "client", + "layout", + "server", + MACHINE_COMMAND, + AGENT_COMMAND, DETACH_SHORTCUT, ] { assert!(help.contains(command), "missing {command}"); } - assert!(!help.contains("workspace")); + assert!( + Arguments::command() + .get_subcommands() + .any(|command| command.get_name() == WORKSPACE_ENTRY) + ); assert!(!Arguments::command().get_subcommands().any(|command| { command.get_name() == RETIRED_TABS_COMMAND || command.get_name() == RETIRED_PICKER_COMMAND @@ -69,18 +77,18 @@ fn commands_accept_connection_options_before_or_after_the_command() { ); assert!(args.selection().is_ok()); } - for name in [ - ATTACH_COMMAND, - DETACH_COMMAND, - LIST_COMMAND, - CLIENTS_COMMAND, - NEW_COMMAND, - TERMINALS_COMMAND, + for path in [ + vec![ATTACH_COMMAND], + vec![NEW_COMMAND], + vec![TERMINALS_COMMAND], + vec!["terminal", LIST_COMMAND], + vec!["client", LIST_COMMAND], + vec!["client", DETACH_COMMAND], ] { - assert!( - Arguments::try_parse_from([PROGRAM, name, HUB_ARGUMENT, HUB, SOCKET_ARGUMENT, SOCKET]) - .is_ok() - ); + let mut arguments = vec![PROGRAM]; + arguments.extend(path); + arguments.extend([HUB_ARGUMENT, HUB, SOCKET_ARGUMENT, SOCKET]); + assert!(Arguments::try_parse_from(arguments).is_ok()); } } @@ -128,14 +136,14 @@ fn attach_is_not_automatic_and_new_preserves_explicit_directory_and_name() { CWD_ARGUMENT, "/tmp", NAME_ARGUMENT, - "agent", + "demo", ]) .unwrap(); assert_eq!( command::startup(args.command, true).unwrap(), StartupAction::NewShell { cwd: "/tmp".into(), - name: Some("agent".into()) + name: Some("demo".into()) } ); } diff --git a/clients/tui/cli/unit_tests/entry.rs b/clients/tui/cli/unit_tests/entry.rs index 168850ad..74020203 100644 --- a/clients/tui/cli/unit_tests/entry.rs +++ b/clients/tui/cli/unit_tests/entry.rs @@ -19,10 +19,11 @@ fn help_and_version_return_clap_outcomes_to_the_dispatcher() { #[test] fn incomplete_connection_returns_usage_error_before_starting_a_runtime() { - let error = crate::run_from([PROGRAM, HUB_ARGUMENT, HUB].map(OsString::from)) - .unwrap_err() - .downcast::() - .unwrap(); - assert_eq!(error.kind(), ErrorKind::MissingRequiredArgument); - assert!(error.use_stderr()); + let error = crate::run_from([PROGRAM, HUB_ARGUMENT, HUB].map(OsString::from)).unwrap_err(); + assert!( + error + .to_string() + .contains(yolo_command_cli::CONNECTION_REQUIRES_HUB), + "{error}" + ); } diff --git a/clients/tui/cli/unit_tests/hook.rs b/clients/tui/cli/unit_tests/hook.rs new file mode 100644 index 00000000..b43cb5ea --- /dev/null +++ b/clients/tui/cli/unit_tests/hook.rs @@ -0,0 +1,39 @@ +use super::constants::*; +use crate::arguments::Arguments; +use crate::command::Command; +use clap::{CommandFactory, Parser}; +use yolo_tui_cli_hook::HookCommand; + +const HOOK_ENTRY: &str = "hook"; +const PROVIDER: &str = "demo"; + +#[test] +fn hook_commands_are_first_class_and_local() { + assert!(matches!( + Arguments::try_parse_from([PROGRAM, HOOK_ENTRY, STATUS_COMMAND, JSON_ARGUMENT]) + .unwrap() + .command, + Some(Command::Hook { + command: HookCommand::Status { json: true } + }) + )); + let Some(Command::Hook { + command: HookCommand::Install { + providers, yes, .. + }, + }) = Arguments::try_parse_from([ + PROGRAM, + HOOK_ENTRY, + INSTALL_COMMAND, + PROVIDER, + YES_ARGUMENT, + ]) + .unwrap() + .command + else { + panic!(); + }; + assert_eq!(providers, [PROVIDER]); + assert!(yes); + assert!(Arguments::command().render_long_help().to_string().contains(HOOK_ENTRY)); +} diff --git a/clients/tui/cli/unit_tests/layouts.rs b/clients/tui/cli/unit_tests/layouts.rs index d55c56f0..076357db 100644 --- a/clients/tui/cli/unit_tests/layouts.rs +++ b/clients/tui/cli/unit_tests/layouts.rs @@ -1,4 +1,5 @@ use super::constants::*; +use crate::layouts::LayoutCommand; use crate::{arguments::Arguments, command::Command, selection::Selection}; use clap::Parser; @@ -33,9 +34,17 @@ fn explicit_layout_is_carried_through_local_and_explicit_connections() { #[test] fn shared_layout_discovery_is_a_noninteractive_command() { - let parsed = Arguments::try_parse_from([PROGRAM, LAYOUTS_COMMAND, JSON_ARGUMENT]).unwrap(); + let parsed = Arguments::try_parse_from([ + PROGRAM, + "layout", + LIST_COMMAND, + JSON_ARGUMENT, + ]) + .unwrap(); assert!(matches!( parsed.command, - Some(Command::Layouts { json: true }) + Some(Command::Layout { + command: LayoutCommand::List { json: true } + }) )); } diff --git a/clients/tui/cli/unit_tests/lib.rs b/clients/tui/cli/unit_tests/lib.rs index 33359e0f..4ca74c57 100644 --- a/clients/tui/cli/unit_tests/lib.rs +++ b/clients/tui/cli/unit_tests/lib.rs @@ -1,7 +1,11 @@ use crate::{arguments::Arguments, selection::Selection}; +mod aux_commands; mod clients; mod commands; mod entry; +mod hook; +mod machine; +mod machine_commands; mod layouts; mod lifecycle; use clap::{Parser, error::ErrorKind}; @@ -15,6 +19,7 @@ mod recipe_edges; mod remote_arguments; mod remote_credentials; mod remote_files; +mod service; #[path = "../test_support/remote_fixture.rs"] mod remote_fixture; @@ -58,8 +63,9 @@ fn empty_connection_selects_automatic_local_and_rejects_workspace() { #[test] fn incomplete_or_conflicting_explicit_connection_never_falls_back() { + let incomplete = Arguments::try_parse_from([PROGRAM, HUB_ARGUMENT, HUB]).unwrap(); + assert!(incomplete.selection().is_err()); for args in [ - vec![PROGRAM, HUB_ARGUMENT, HUB], vec![PROGRAM, SOCKET_ARGUMENT, SOCKET], vec![PROGRAM, ENDPOINT_ARGUMENT, LOCAL_ENDPOINT], ] { diff --git a/clients/tui/cli/unit_tests/lifecycle.rs b/clients/tui/cli/unit_tests/lifecycle.rs index 5fb460bc..28f371b4 100644 --- a/clients/tui/cli/unit_tests/lifecycle.rs +++ b/clients/tui/cli/unit_tests/lifecycle.rs @@ -1,6 +1,7 @@ use super::constants::*; use crate::arguments::Arguments; use crate::lifecycle::{select, settled}; +use crate::terminal::TerminalCommand; use clap::Parser; use yolo_hub_identity::{HubId, RuntimeEpoch, RuntimeTarget, TerminalAddress, TerminalId}; use yolo_hub_terminal_protocol::{ @@ -45,25 +46,38 @@ fn with_status(status: RuntimeStatus) -> TerminalSummary { #[test] fn archive_and_terminate_parse_with_their_flags() { - let arguments = Arguments::try_parse_from([PROGRAM, ARCHIVE_COMMAND, NAME]).unwrap(); + let arguments = + Arguments::try_parse_from([PROGRAM, "terminal", ARCHIVE_COMMAND, NAME]).unwrap(); assert!(matches!( arguments.command, - Some(crate::command::Command::Archive { - terminal, - json: false + Some(crate::command::Command::Terminal { + command: TerminalCommand::Archive { + terminal, + json: false + } }) if terminal == NAME )); - let arguments = - Arguments::try_parse_from([PROGRAM, TERMINATE_COMMAND, NAME, FORCE_ARGUMENT]).unwrap(); + let arguments = Arguments::try_parse_from([ + PROGRAM, + "terminal", + TERMINATE_COMMAND, + NAME, + FORCE_ARGUMENT, + ]) + .unwrap(); assert!(matches!( arguments.command, - Some(crate::command::Command::Terminate { - terminal, - force: true, - json: false + Some(crate::command::Command::Terminal { + command: TerminalCommand::Terminate { + terminal, + force: true, + json: false + } }) if terminal == NAME )); - assert!(Arguments::try_parse_from([PROGRAM, ARCHIVE_COMMAND]).is_err()); + assert!( + Arguments::try_parse_from([PROGRAM, "terminal", ARCHIVE_COMMAND]).is_err() + ); } #[test] diff --git a/clients/tui/cli/unit_tests/machine.rs b/clients/tui/cli/unit_tests/machine.rs new file mode 100644 index 00000000..3f94de5c --- /dev/null +++ b/clients/tui/cli/unit_tests/machine.rs @@ -0,0 +1,189 @@ +use super::constants::*; +use crate::arguments::Arguments; +use crate::command::Command; +use clap::Parser; +use uuid::Uuid; +use yolo_hub_delegation::PeerIdentity; +use yolo_hub_federation_settings::Configuration; +use yolo_hub_identity::HubId; +use yolo_hub_management_protocol::{ + CONFIGURATION_SCHEMA, ConfigurationSnapshot, ConfigurationState, FIRST_CONFIGURATION_REVISION, +}; +use yolo_hub_routing::Right; +use yolo_tui_hub_cli::{MachineCommand, PinMachine, pin_change}; + +pub const MACHINE_HUB: &str = "11111111-2222-4333-8444-555555555555"; +pub const LOCAL_HUB: &str = "aaaaaaaa-bbbb-4ccc-8ddd-eeeeeeeeeeee"; +pub const MACHINE_KEY: &str = "1111111111111111111111111111111111111111111111111111111111111111"; +pub const INVALID_KEY: &str = "abc"; +pub const PROFILE_REVISION: &str = + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; + +fn local_identity() -> PeerIdentity { + PeerIdentity::new(LOCAL_HUB.parse().unwrap(), [1; 32]).unwrap() +} + +fn machine_identity() -> PeerIdentity { + PeerIdentity::new(MACHINE_HUB.parse().unwrap(), [0x11; 32]).unwrap() +} + +fn snapshot() -> ConfigurationSnapshot { + ConfigurationSnapshot { + state: ConfigurationState { + schema_version: CONFIGURATION_SCHEMA, + identity: local_identity(), + revision: FIRST_CONFIGURATION_REVISION, + last_change: Uuid::new_v4(), + configuration: Configuration::default(), + }, + applied: true, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), + } +} + +fn parse(arguments: Vec<&str>) -> PinMachine { + let arguments = Arguments::try_parse_from(arguments).unwrap(); + match arguments.command { + Some(Command::Machine { + command: MachineCommand::Pin(args), + }) => args, + _ => panic!("expected machine pin"), + } +} + +fn pin_args(extra: &[&str]) -> PinMachine { + let mut arguments = vec![ + PROGRAM, + MACHINE_COMMAND, + PIN_COMMAND, + MACHINE_HUB, + MACHINE_KEY, + ]; + arguments.extend_from_slice(extra); + parse(arguments) +} + +#[test] +fn pin_accepts_an_ssh_target_and_remote_socket_together() { + let args = pin_args(&[ + SSH_ARGUMENT, + SSH_TARGET, + REMOTE_SOCKET_ARGUMENT, + REMOTE_SOCKET, + ]); + let change = pin_change(&snapshot(), &args).unwrap(); + let dial = change.configuration.peers[0].dial.as_ref().unwrap(); + assert_eq!(dial.ssh_target(), Some(SSH_TARGET)); + assert_eq!(dial.socket().to_str(), Some(REMOTE_SOCKET)); + assert!( + Arguments::try_parse_from([ + PROGRAM, + MACHINE_COMMAND, + PIN_COMMAND, + MACHINE_HUB, + MACHINE_KEY, + SSH_ARGUMENT, + SSH_TARGET, + ]) + .is_err() + ); + assert!( + Arguments::try_parse_from([ + PROGRAM, + MACHINE_COMMAND, + PIN_COMMAND, + MACHINE_HUB, + MACHINE_KEY, + REMOTE_SOCKET_ARGUMENT, + REMOTE_SOCKET, + ]) + .is_err() + ); +} + +#[test] +fn pins_a_machine_with_an_ssh_dial_and_no_exports_by_default() { + let change = pin_change( + &snapshot(), + + &pin_args(&[ + SSH_ARGUMENT, + SSH_TARGET, + REMOTE_SOCKET_ARGUMENT, + REMOTE_SOCKET, + ]), + ) + .unwrap(); + assert_eq!(change.expected_revision, FIRST_CONFIGURATION_REVISION); + assert!(!change.request_id.is_nil()); + let peer = &change.configuration.peers[0]; + assert_eq!(peer.identity, machine_identity()); + assert!(peer.consume); + let dial = peer.dial.as_ref().unwrap(); + assert_eq!(dial.ssh_target(), Some(SSH_TARGET)); + assert_eq!(dial.socket().to_str(), Some(REMOTE_SOCKET)); + assert!(peer.exports.is_empty()); +} + +#[test] +fn pin_only_and_local_export_are_applied() { + let change = pin_change( + &snapshot(), + + &pin_args(&[ + PIN_ONLY_ARGUMENT, + SSH_ARGUMENT, + SSH_TARGET, + REMOTE_SOCKET_ARGUMENT, + REMOTE_SOCKET, + EXPORT_LOCAL_ARGUMENT, + ]), + ) + .unwrap(); + let peer = &change.configuration.peers[0]; + assert!(!peer.consume); + assert_eq!(peer.exports.len(), 1); + assert_eq!(peer.exports[0].origin, LOCAL_HUB.parse().unwrap()); + assert!( + peer.exports[0] + .rights + .allows(&[Right::Observe].into_iter().collect()) + ); + assert_eq!(peer.exports[0].remaining_reexports, 0); +} + +#[test] +fn rejects_duplicate_self_and_invalid_keys() { + let args = pin_args(&[]); + let mut existing = snapshot(); + existing + .state + .configuration + .peers + .push(yolo_hub_federation_settings::Peer { + identity: machine_identity(), + dial: None, + consume: false, + exports: vec![], + }); + assert!(pin_change(&existing, &args).is_err()); + + let mut self_snapshot = snapshot(); + self_snapshot.state.identity = local_identity_at_machine_hub(); + assert!(pin_change(&self_snapshot, &args).is_err()); + + let invalid = parse(vec![ + PROGRAM, + MACHINE_COMMAND, + PIN_COMMAND, + MACHINE_HUB, + INVALID_KEY, + ]); + assert!(pin_change(&snapshot(), &invalid).is_err()); + +} + +fn local_identity_at_machine_hub() -> PeerIdentity { + PeerIdentity::new(MACHINE_HUB.parse().unwrap(), [3; 32]).unwrap() +} + diff --git a/clients/tui/cli/unit_tests/machine_commands.rs b/clients/tui/cli/unit_tests/machine_commands.rs new file mode 100644 index 00000000..817298f1 --- /dev/null +++ b/clients/tui/cli/unit_tests/machine_commands.rs @@ -0,0 +1,64 @@ +use super::constants::*; +use super::machine::{MACHINE_HUB, MACHINE_KEY}; +use crate::arguments::Arguments; +use crate::command::Command; +use clap::Parser; +use yolo_tui_hub_cli::MachineCommand; + +fn machine_command(arguments: Vec<&str>) -> MachineCommand { + let parsed = Arguments::try_parse_from(arguments).unwrap(); + match parsed.command { + Some(Command::Machine { command }) => command, + _ => panic!("expected machine command"), + } +} + +#[test] +fn machine_commands_parse_at_the_top_level() { + assert!(matches!( + machine_command(vec![PROGRAM, MACHINE_COMMAND, ADD_COMMAND, SSH_TARGET]), + MachineCommand::Add(_) + )); + assert!(matches!( + machine_command(vec![ + PROGRAM, + MACHINE_COMMAND, + PIN_COMMAND, + MACHINE_HUB, + MACHINE_KEY + ]), + MachineCommand::Pin(_) + )); + assert!(matches!( + machine_command(vec![ + PROGRAM, + MACHINE_COMMAND, + IDENTITY_COMMAND, + JSON_ARGUMENT + ]), + MachineCommand::Identity { json: true } + )); + assert!(matches!( + machine_command(vec![PROGRAM, MACHINE_COMMAND, LIST_COMMAND, JSON_ARGUMENT]), + MachineCommand::List { json: true } + )); + assert!(matches!( + machine_command(vec![ + PROGRAM, + MACHINE_COMMAND, + CONSUMERS_COMMAND, + JSON_ARGUMENT + ]), + MachineCommand::Consumers { json: true } + )); + assert!(matches!( + machine_command(vec![ + PROGRAM, + MACHINE_COMMAND, + REMOVE_COMMAND, + MACHINE_HUB, + JSON_ARGUMENT + ]), + MachineCommand::Remove { .. } + )); +} diff --git a/clients/tui/cli/unit_tests/service.rs b/clients/tui/cli/unit_tests/service.rs new file mode 100644 index 00000000..a93b49b7 --- /dev/null +++ b/clients/tui/cli/unit_tests/service.rs @@ -0,0 +1,55 @@ +use super::constants::*; +use crate::arguments::Arguments; +use crate::command::Command; +use crate::server::ServerCommand; +use clap::Parser; + +fn parse(path: &[&str], extra: &[&str]) -> Command { + let mut arguments = vec![PROGRAM]; + arguments.extend_from_slice(path); + arguments.extend_from_slice(extra); + Arguments::try_parse_from(arguments).unwrap().command.unwrap() +} + +#[test] +fn lifecycle_commands_parse_without_connection_arguments() { + assert!(matches!( + parse(&["server", STATUS_COMMAND], &[]), + Command::Server { + command: ServerCommand::Status(options) + } if !options.json + )); + assert!(matches!( + parse(&["server", VERSION_ENTRY], &[JSON_ARGUMENT]), + Command::Server { + command: ServerCommand::Version(options) + } if options.json + )); + assert!(matches!( + parse(&["server", START_COMMAND], &[]), + Command::Server { + command: ServerCommand::Start(_) + } + )); + assert!(matches!( + parse(&["server", STOP_COMMAND], &[]), + Command::Server { + command: ServerCommand::Stop(_) + } + )); + assert!(matches!( + parse(&["server", RESTART_COMMAND], &[JSON_ARGUMENT]), + Command::Server { + command: ServerCommand::Restart(options) + } if options.json + )); +} + +#[test] +fn lifecycle_commands_help_names_the_service_scope() { + let error = Arguments::try_parse_from([PROGRAM, "server", STOP_COMMAND, HELP_ARGUMENT]) + .unwrap_err(); + let text = error.to_string(); + assert!(text.contains("Stop the local Host and Hub")); + assert!(text.contains("PTY")); +} diff --git a/clients/tui/constants/BUILD.bazel b/clients/tui/constants/BUILD.bazel index 7e06285f..c92694d5 100644 --- a/clients/tui/constants/BUILD.bazel +++ b/clients/tui/constants/BUILD.bazel @@ -1,11 +1,11 @@ -load("@rules_rust//rust:defs.bzl", "rust_library") +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") package(default_visibility = ["//visibility:public"]) filegroup( name = "architecture_check_data", - srcs = glob(["src/**/*.rs"]) + ["BUILD.bazel"], + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], ) rust_library( @@ -15,4 +15,14 @@ rust_library( crate_root = "src/lib.rs", edition = "2024", version = YOLO_MARKETING_VERSION, + deps = ["@crates//:uuid"], +) + +rust_test( + name = "unit_tests", + srcs = glob(["unit_tests/**/*.rs"]), + crate_name = "yolo_tui_app_constants_unit_tests", + crate_root = "unit_tests/lib.rs", + edition = "2024", + deps = [":yolo_tui_app_constants", "@crates//:uuid"], ) diff --git a/clients/tui/constants/src/lib.rs b/clients/tui/constants/src/lib.rs index 4a8f2264..d8c9bb4b 100644 --- a/clients/tui/constants/src/lib.rs +++ b/clients/tui/constants/src/lib.rs @@ -19,6 +19,11 @@ pub const DISCONNECTED_LABEL: &str = "Disconnected"; pub const CATALOG_REFRESHING_LABEL: &str = "Refreshing"; pub const UNSAVED_SHELL_CHANGES_LABEL: &str = "Unsaved shell changes"; pub const NO_RUNTIME_LABEL: &str = "No running terminal"; +/// Set by the Worker for a shell it owns; a nested client can never display +/// the terminal it is running in without recursively rendering itself. +pub const SELF_TERMINAL_ENV: &str = "YOLO_TERMINAL_ID"; +pub const SELF_TERMINAL_LABEL: &str = + "This is the terminal running YOLO; open another terminal to view it"; pub const READ_ONLY_LABEL: &str = "Read Only"; pub const CONTROL_LOST_LABEL: &str = "Input control was taken by another client"; pub const INPUT_WAITING_LABEL: &str = "Waiting for terminal input control; input queued"; @@ -45,6 +50,7 @@ pub const ACTIVE_SCOPE_LABEL: &str = "Active: live and unconfirmed records"; pub const AGENT_HISTORY_DETAILS_LABEL: &str = "Agent details · Esc back · R view terminal recovery"; pub const TERMINAL_RECOVERY_HINT: &str = "Terminal history · R resume · a archive · h active"; pub const HUB_UNAVAILABLE_LABEL: &str = "No Hub selected"; +pub const MACHINE_UNREACHABLE_LABEL: &str = "Machine is unreachable"; pub const PANE_RATIO_STEP: u16 = 50; pub const PANE_BORDER_CELLS: u16 = 1; pub const SCROLL_ROWS_PER_TICK: usize = 3; @@ -58,3 +64,11 @@ pub const OBSERVER_PREFIX_HINT: &str = pub const OBSERVER_INVENTORY_HINT: &str = "Enter open/details · g terminals · h history · ? help · q detach"; pub const OBSERVER_HISTORY_HINT: &str = "Terminal history · Enter view · h active"; + +pub fn is_self_terminal(terminal_id: uuid::Uuid) -> bool { + parsed_self_terminal(std::env::var(SELF_TERMINAL_ENV).ok().as_deref(), terminal_id) +} + +pub fn parsed_self_terminal(value: Option<&str>, terminal_id: uuid::Uuid) -> bool { + value.and_then(|value| value.parse::().ok()) == Some(terminal_id) +} diff --git a/clients/tui/constants/unit_tests/lib.rs b/clients/tui/constants/unit_tests/lib.rs new file mode 100644 index 00000000..3a60ef1a --- /dev/null +++ b/clients/tui/constants/unit_tests/lib.rs @@ -0,0 +1,15 @@ +use yolo_tui_app_constants::parsed_self_terminal; + +const SELF: &str = "4f0a7b47-4b7d-4d02-bb1a-d7ee285f3642"; + +#[test] +fn matches_only_the_terminal_named_by_the_worker_environment() { + let self_id = SELF.parse().unwrap(); + assert!(parsed_self_terminal(Some(SELF), self_id)); + assert!(!parsed_self_terminal( + Some("11111111-2222-4333-8444-555555555555"), + self_id + )); + assert!(!parsed_self_terminal(None, self_id)); + assert!(!parsed_self_terminal(Some("not-a-uuid"), self_id)); +} diff --git a/clients/tui/hub/cli/BUILD.bazel b/clients/tui/hub/cli/BUILD.bazel new file mode 100644 index 00000000..0dbbd6c1 --- /dev/null +++ b/clients/tui/hub/cli/BUILD.bazel @@ -0,0 +1,43 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "//core/crates/hub/delegation:yolo_hub_delegation", + "//core/crates/hub/federation/settings:yolo_hub_federation_settings", + "//core/crates/hub/identity:yolo_hub_identity", + "//core/crates/hub/management/client:yolo_hub_management_client", + "//core/crates/hub/management/protocol:yolo_hub_management_protocol", + "//core/crates/hub/routing:yolo_hub_routing", + "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", + "@crates//:anyhow", + "@crates//:clap", + "@crates//:serde_json", + "@crates//:tokio", + "@crates//:uuid", +] + +rust_library( + name = "yolo_tui_hub_cli", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_tui_hub_cli", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) + +filegroup( + name = "architecture_check_data", + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], +) + +rust_test( + name = "unit_tests", + srcs = glob(["src/**/*.rs", "unit_tests/**/*.rs"]), + crate_name = "yolo_tui_hub_cli_unit_tests", + crate_root = "src/lib.rs", + edition = "2024", + deps = _DEPS, +) diff --git a/clients/tui/hub/cli/src/configuration.rs b/clients/tui/hub/cli/src/configuration.rs new file mode 100644 index 00000000..8690c757 --- /dev/null +++ b/clients/tui/hub/cli/src/configuration.rs @@ -0,0 +1,130 @@ +use crate::constants::*; +use crate::PinMachine; +use anyhow::{Context, Result, ensure}; +use std::collections::BTreeSet; +use uuid::Uuid; +use yolo_hub_delegation::{ED25519_PUBLIC_KEY_BYTES, PeerIdentity}; +use yolo_hub_federation_settings::{ExportPolicy, Peer, PeerAddress}; +use yolo_hub_identity::HubId; +use yolo_hub_management_protocol::{ConfigurationChange, ConfigurationSnapshot}; +use yolo_hub_routing::Right; + +pub fn pin_change( + snapshot: &ConfigurationSnapshot, + args: &PinMachine, +) -> Result { + let local = snapshot.state.identity; + let identity = identity(args.machine, &args.key)?; + ensure!(identity.hub() != local.hub(), MACHINE_IS_LOCAL); + ensure!( + snapshot + .state + .configuration + .peers + .iter() + .all(|peer| peer.identity.hub() != identity.hub()), + MACHINE_ALREADY_CONFIGURED + ); + let dial = match (&args.ssh, &args.remote_socket) { + (Some(target), Some(socket)) => { + Some(PeerAddress::ssh(target.clone(), socket.clone())?) + } + (None, None) => None, + _ => anyhow::bail!("{}", INVALID_MACHINE_DIAL), + }; + let mut origins = args.exports.iter().copied().collect::>(); + if args.export_local { + origins.insert(local.hub()); + } + let exports = origins + .into_iter() + .map(|origin| ExportPolicy { + origin, + rights: [Right::Observe].into_iter().collect(), + remaining_reexports: 0, + }) + .collect(); + let mut configuration = snapshot.state.configuration.clone(); + let consume = dial.is_some() && !args.pin_only; + configuration.peers.push(Peer { + identity, + dial, + consume, + exports, + }); + configuration.validate(local)?; + Ok(ConfigurationChange { + request_id: Uuid::new_v4(), + expected_revision: snapshot.state.revision, + configuration, + }) +} + +pub fn import_change( + snapshot: &ConfigurationSnapshot, + target: String, + socket: std::path::PathBuf, + identity: PeerIdentity, + pin_only: bool, +) -> Result> { + let local = snapshot.state.identity; + ensure!(identity.hub() != local.hub(), MACHINE_IS_LOCAL); + let dial = PeerAddress::ssh(target, socket)?; + let mut configuration = snapshot.state.configuration.clone(); + match configuration + .peers + .iter_mut() + .find(|peer| peer.identity.hub() == identity.hub()) + { + Some(peer) => { + ensure!(peer.identity == identity, MACHINE_IDENTITY_CHANGED); + peer.dial = Some(dial); + peer.consume = !pin_only; + } + None => configuration.peers.push(Peer { + identity, + dial: Some(dial), + consume: !pin_only, + exports: Vec::new(), + }), + } + configuration.validate(local)?; + if configuration == snapshot.state.configuration { + return Ok(None); + } + Ok(Some(ConfigurationChange { + request_id: Uuid::new_v4(), + expected_revision: snapshot.state.revision, + configuration, + })) +} + +pub fn remove_change( + snapshot: &ConfigurationSnapshot, + hub: HubId, +) -> Result { + let mut configuration = snapshot.state.configuration.clone(); + let before = configuration.peers.len(); + configuration.peers.retain(|peer| peer.identity.hub() != hub); + ensure!(configuration.peers.len() != before, MACHINE_NOT_CONFIGURED); + configuration.validate(snapshot.state.identity)?; + Ok(ConfigurationChange { + request_id: Uuid::new_v4(), + expected_revision: snapshot.state.revision, + configuration, + }) +} + +pub(crate) fn identity(hub: HubId, key: &str) -> Result { + let key = key.trim(); + ensure!( + key.len() == ED25519_PUBLIC_KEY_BYTES * 2 && key.is_ascii(), + INVALID_PUBLIC_KEY + ); + let mut bytes = [u8::default(); ED25519_PUBLIC_KEY_BYTES]; + for (index, byte) in bytes.iter_mut().enumerate() { + let offset = index * 2; + *byte = u8::from_str_radix(&key[offset..offset + 2], 16).context(INVALID_PUBLIC_KEY)?; + } + PeerIdentity::new(hub, bytes).context(INVALID_PUBLIC_KEY) +} diff --git a/clients/tui/hub/cli/src/constants.rs b/clients/tui/hub/cli/src/constants.rs new file mode 100644 index 00000000..8be94e1d --- /dev/null +++ b/clients/tui/hub/cli/src/constants.rs @@ -0,0 +1,31 @@ +pub const INVALID_PUBLIC_KEY: &str = "Public key must be 64 hexadecimal characters"; +pub const MACHINE_ALREADY_CONFIGURED: &str = "Machine is already configured"; +pub const MACHINE_NOT_CONFIGURED: &str = "Machine is not configured"; +pub const MACHINE_IS_LOCAL: &str = "The local machine cannot be added as a remote machine"; +pub const MACHINE_IDENTITY_CHANGED: &str = + "The configured machine identity changed; remove the machine before importing again"; +pub const PEER_SOCKET_UNAVAILABLE: &str = "The local Hub did not report a peer socket"; +pub const MACHINE_SSH_FAILED: &str = "Could not read the remote machine's Hub identity over SSH"; +pub const MACHINE_OLDER_PROVIDER_HINT: &str = + "the remote machine may run an older Yolo release; both machines need the same or a newer release"; +pub const MACHINE_INVALID_RESPONSE: &str = "The remote machine's Hub identity response is invalid"; +pub const REMOTE_YOLO_DEFAULT: &str = "yolo"; +pub const INVALID_MACHINE_DIAL: &str = "Pass --ssh and --remote-socket together, or neither"; +pub const HUB_CONFIGURATION_READ_FAILED: &str = "Could not read the Hub configuration"; +pub const HUB_CONFIGURATION_WRITE_FAILED: &str = "Could not change the Hub configuration"; +pub const MACHINE_LIST_HEADER: &str = "MACHINE\tHUB\tSOCKET\tIMPORTS FROM\tEXPORTS TO"; +pub const MACHINE_LIST_EMPTY: &str = + "No machines configured. Add one with `yolo machine add `."; +pub const MACHINE_CONSUMERS_HEADER: &str = "HUB\tLINKS\tCONNECTED_MS"; +pub const MACHINE_CONSUMERS_EMPTY: &str = "No machines are importing from this machine."; +pub const UNEXPECTED_REPLY: &str = "Hub returned an unexpected consumers reply"; +pub const MACHINE_LIST_LEGEND: &str = + "IMPORTS FROM: agents this machine imports from the remote machine; EXPORTS TO: origins this machine shares with it"; +pub const IMPORT_ONE_WAY: &str = "one-way import; the remote machine does not import this machine"; +pub const PUBLIC_KEY_FIELD: &str = "public_key"; +pub const HUB_ID_FIELD: &str = "hub_id"; +pub const SSH_PROGRAM: &str = "ssh"; +pub const CONNECTION_GROUP: &str = "connection"; +pub const SOCKET_ARGUMENT: &str = "socket"; +pub const ENDPOINT_ARGUMENT: &str = "endpoint"; +pub const HUB_ARGUMENT: &str = "hub"; diff --git a/clients/tui/hub/cli/src/identity.rs b/clients/tui/hub/cli/src/identity.rs new file mode 100644 index 00000000..3660fa36 --- /dev/null +++ b/clients/tui/hub/cli/src/identity.rs @@ -0,0 +1,25 @@ +use crate::constants::*; +use crate::output; +use anyhow::{Context, Result}; +use std::path::Path; +use tokio::time::Instant; +use yolo_hub_identity::HubId; + +pub(crate) async fn run( + socket: &Path, + entry: HubId, + json: bool, + deadline: Instant, +) -> Result<()> { + let snapshot = crate::machine::read(socket, entry, deadline).await?; + let identity = snapshot.state.identity; + let peer_socket = snapshot.peer_socket.context(PEER_SOCKET_UNAVAILABLE)?; + if json { + println!("{}", output::identity_json(identity, &peer_socket)); + } else { + println!("HUB ID\t{}", identity.hub()); + println!("PUBLIC KEY\t{}", output::key_text(identity)); + println!("LINK SOCKET\t{}", peer_socket.display()); + } + Ok(()) +} diff --git a/clients/tui/hub/cli/src/lib.rs b/clients/tui/hub/cli/src/lib.rs new file mode 100644 index 00000000..6fb35e83 --- /dev/null +++ b/clients/tui/hub/cli/src/lib.rs @@ -0,0 +1,133 @@ +mod configuration; +mod constants; +mod identity; +mod output; +mod machine; + +use anyhow::Result; +use clap::{ArgGroup, Args, Subcommand}; +use constants::*; +use std::path::{Path, PathBuf}; +use tokio::time::Instant; +use yolo_hub_identity::HubId; + +pub use configuration::{import_change, pin_change, remove_change}; + +#[derive(Debug, Subcommand)] +pub enum MachineCommand { + /// Import agents from another machine's Hub over SSH (one-way). + #[command(group(connection_group()))] + Add(AddMachine), + /// Pin a machine with a known Hub ID and public key (advanced). + #[command(group(connection_group()))] + Pin(PinMachine), + /// List configured machines. + #[command(group(connection_group()))] + List { + /// Print one JSON document. + #[arg(long)] + json: bool, + }, + /// Remove a configured machine. + #[command(group(connection_group()))] + Remove { + /// Machine Hub ID. + machine: HubId, + /// Print one JSON document. + #[arg(long)] + json: bool, + }, + /// List machines currently importing from this machine. + #[command(group(connection_group()))] + Consumers { + /// Print one JSON document. + #[arg(long)] + json: bool, + }, + /// Print this machine's Hub identity and peer socket. + #[command(group(connection_group()))] + Identity { + /// Print one JSON document. + #[arg(long)] + json: bool, + }, +} + +#[derive(Debug, Args)] +pub struct AddMachine { + /// SSH destination of the remote machine (user@host or an ssh_config alias). + target: String, + /// Discover and pin the machine identity without importing its agents. + #[arg(long)] + pin_only: bool, + /// Remote yolo executable used for discovery. + #[arg(long, hide = true, default_value = REMOTE_YOLO_DEFAULT)] + remote_yolo: String, + /// Print one JSON document. + #[arg(long)] + json: bool, +} + +#[derive(Debug, Args)] +pub struct PinMachine { + /// Machine Hub ID. + #[arg(value_parser = machine_id)] + machine: HubId, + /// Machine Ed25519 public key, 64 hexadecimal characters. + key: String, + /// SSH destination used to reach the machine (user@host or an ssh_config alias). + #[arg(long, requires = "remote_socket")] + ssh: Option, + /// Absolute Unix peer socket path on the remote machine. + #[arg(long, requires = "ssh")] + remote_socket: Option, + /// Pin the identity without importing resources from this machine. + #[arg(long)] + pin_only: bool, + /// Export an origin to this machine with Observe rights; repeatable. + #[arg(long = "export", value_name = "HUB_ID")] + exports: Vec, + /// Export this machine's own origin with Observe rights. + #[arg(long)] + export_local: bool, + /// Print one JSON document. + #[arg(long)] + json: bool, +} + +// The root CLI declares the same group for its global --socket/--endpoint/--hub +// arguments; clap resolves `requires` per command, so this subtree needs its own +// group with identical ids. +fn connection_group() -> ArgGroup { + ArgGroup::new(CONNECTION_GROUP) + .args([SOCKET_ARGUMENT, ENDPOINT_ARGUMENT]) + .requires(HUB_ARGUMENT) +} + +fn machine_id(value: &str) -> Result { + value.parse().map_err(|_| { + format!("'{value}' is not a Hub ID; to add a machine over SSH run `yolo machine add {value}`") + }) +} + +pub async fn run( + socket: &Path, + entry: HubId, + command: &MachineCommand, + deadline: Instant, +) -> Result<()> { + match command { + MachineCommand::Add(args) => machine::import(socket, entry, args, deadline).await, + MachineCommand::Pin(args) => machine::pin(socket, entry, args, deadline).await, + MachineCommand::List { json } => machine::list(socket, entry, *json, deadline).await, + MachineCommand::Remove { machine, json } => { + machine::remove(socket, entry, *machine, *json, deadline).await + } + MachineCommand::Consumers { json } => machine::consumers(socket, entry, *json, deadline).await, + MachineCommand::Identity { json } => identity::run(socket, entry, *json, deadline).await, + } +} + +#[cfg(test)] +#[path = "../unit_tests/lib.rs"] +mod unit_tests; diff --git a/clients/tui/hub/cli/src/machine/consumers.rs b/clients/tui/hub/cli/src/machine/consumers.rs new file mode 100644 index 00000000..cdba55b2 --- /dev/null +++ b/clients/tui/hub/cli/src/machine/consumers.rs @@ -0,0 +1,35 @@ +use crate::constants::*; +use anyhow::{Result, bail}; +use std::path::Path; +use tokio::time::Instant; +use yolo_hub_identity::HubId; +use yolo_hub_management_client::execute_reply; +use yolo_hub_rpc_protocol::{Command, Reply}; + +pub(crate) async fn consumers( + socket: &Path, + entry: HubId, + json: bool, + deadline: Instant, +) -> Result<()> { + let reply = execute_reply(socket, entry, Command::ListConsumers {}, deadline).await?; + let Reply::Consumers { consumers } = reply else { + bail!(UNEXPECTED_REPLY); + }; + if json { + println!("{}", serde_json::to_string_pretty(&consumers)?); + return Ok(()); + } + if consumers.is_empty() { + println!("{MACHINE_CONSUMERS_EMPTY}"); + return Ok(()); + } + println!("{MACHINE_CONSUMERS_HEADER}"); + for consumer in consumers { + println!( + "{}\t{}\t{}", + consumer.hub, consumer.links, consumer.connected_since_ms + ); + } + Ok(()) +} diff --git a/clients/tui/hub/cli/src/machine/import.rs b/clients/tui/hub/cli/src/machine/import.rs new file mode 100644 index 00000000..e145e680 --- /dev/null +++ b/clients/tui/hub/cli/src/machine/import.rs @@ -0,0 +1,135 @@ +use crate::configuration::{identity, import_change}; +use crate::constants::*; +use crate::AddMachine; +use anyhow::{Context, Result, ensure}; +use std::path::{Path, PathBuf}; +use tokio::process::Command; +use tokio::time::Instant; +use yolo_hub_federation_settings::validate_ssh_target; +use yolo_hub_identity::HubId; +use yolo_hub_management_client::execute; +use yolo_hub_rpc_protocol::Command as HubRpcCommand; + +struct RemoteIdentity { + hub: HubId, + public_key: String, + peer_socket: PathBuf, +} + +pub(crate) async fn run( + socket: &Path, + entry: HubId, + args: &AddMachine, + deadline: Instant, +) -> Result<()> { + validate_ssh_target(&args.target)?; + let remote = remote_identity(&args.target, &args.remote_yolo).await?; + let peer = identity(remote.hub, &remote.public_key)?; + let snapshot = super::read(socket, entry, deadline).await?; + let change = import_change( + &snapshot, + args.target.clone(), + remote.peer_socket, + peer, + args.pin_only, + )?; + let applied = match change { + Some(change) => { + execute(socket, entry, HubRpcCommand::Configure { change }, deadline) + .await + .context(HUB_CONFIGURATION_WRITE_FAILED)? + .applied + } + None => snapshot.applied, + }; + if args.json { + println!( + "{}", + serde_json::json!({ + "machine": peer.hub().to_string(), + "ssh": args.target, + "imports": !args.pin_only, + "applied": applied, + }) + ); + } else if args.pin_only { + println!("pinned machine {} via {}", peer.hub(), args.target); + if !applied { + println!("configuration is persisted but not active"); + } + } else { + println!( + "importing from {} via {} ({IMPORT_ONE_WAY})", + peer.hub(), + args.target + ); + if !applied { + println!("configuration is persisted but not active"); + } + } + Ok(()) +} + +async fn remote_identity(target: &str, remote_yolo: &str) -> Result { + let output = Command::new(SSH_PROGRAM) + .arg("-T") + .arg("-o") + .arg("BatchMode=yes") + .arg("-o") + .arg("ConnectTimeout=10") + .arg(target) + .arg(remote_command(remote_yolo)) + .output() + .await + .context(MACHINE_SSH_FAILED)?; + let stderr = String::from_utf8_lossy(&output.stderr); + let hint = if stderr.contains("unrecognized subcommand") { + format!("; {MACHINE_OLDER_PROVIDER_HINT}") + } else { + String::new() + }; + ensure!( + output.status.success(), + "{}: {}{hint}", + MACHINE_SSH_FAILED, + stderr.trim() + ); + parse_identity(&String::from_utf8_lossy(&output.stdout)) +} + +fn remote_command(remote_yolo: &str) -> String { + if remote_yolo == REMOTE_YOLO_DEFAULT { + "if command -v yolo >/dev/null 2>&1; then exec yolo machine identity --json; else exec \ + \"$HOME/.local/bin/yolo\" machine identity --json; fi" + .into() + } else { + format!("exec {} machine identity --json", shell_quote(remote_yolo)) + } +} + +fn shell_quote(value: &str) -> String { + format!("'{}'", value.replace('\'', "'\\''")) +} + +fn parse_identity(stdout: &str) -> Result { + for line in stdout.lines().rev() { + let Ok(value) = serde_json::from_str::(line.trim()) else { + continue; + }; + let Some(hub) = value.get(HUB_ID_FIELD).and_then(|value| value.as_str()) else { + continue; + }; + let Some(public_key) = value.get(PUBLIC_KEY_FIELD).and_then(|value| value.as_str()) else { + continue; + }; + let Some(peer_socket) = value.get("peer_socket").and_then(|value| value.as_str()) else { + continue; + }; + return Ok(RemoteIdentity { + hub: hub.parse().context(MACHINE_INVALID_RESPONSE)?, + public_key: public_key.to_owned(), + peer_socket: peer_socket.into(), + }); + } + anyhow::bail!(MACHINE_INVALID_RESPONSE) +} diff --git a/clients/tui/hub/cli/src/machine/list.rs b/clients/tui/hub/cli/src/machine/list.rs new file mode 100644 index 00000000..01ed6171 --- /dev/null +++ b/clients/tui/hub/cli/src/machine/list.rs @@ -0,0 +1,60 @@ +use crate::constants::*; +use crate::output; +use anyhow::Result; +use std::path::Path; +use tokio::time::Instant; +use yolo_hub_identity::HubId; + +pub(crate) async fn list( + socket: &Path, + entry: HubId, + json: bool, + deadline: Instant, +) -> Result<()> { + let snapshot = super::read(socket, entry, deadline).await?; + if json { + println!("{}", output::list_json(&snapshot)?); + return Ok(()); + } + let machines = &snapshot.state.configuration.peers; + if machines.is_empty() { + println!("{MACHINE_LIST_EMPTY}"); + return Ok(()); + } + println!("{MACHINE_LIST_HEADER}"); + for machine in machines { + let (label, socket) = machine.dial.as_ref().map_or_else( + || ("pinned".to_owned(), "-".to_owned()), + |dial| { + ( + dial.ssh_target() + .or_else(|| dial.profile_id()) + .unwrap_or_default() + .to_owned(), + dial.socket().display().to_string(), + ) + }, + ); + let imports = if machine.consume { "yes" } else { "no" }; + let exports = machine + .exports + .iter() + .map(|policy| policy.origin.to_string()) + .collect::>() + .join(","); + println!( + "{}\t{}\t{}\t{}\t{}", + label, + machine.identity.hub(), + socket, + imports, + if exports.is_empty() { + "-".into() + } else { + exports + } + ); + } + println!("{MACHINE_LIST_LEGEND}"); + Ok(()) +} diff --git a/clients/tui/hub/cli/src/machine/mod.rs b/clients/tui/hub/cli/src/machine/mod.rs new file mode 100644 index 00000000..998a87ed --- /dev/null +++ b/clients/tui/hub/cli/src/machine/mod.rs @@ -0,0 +1,30 @@ +mod consumers; +mod import; +mod list; +mod pin; +mod remove; + +use crate::constants::*; +use anyhow::{Context, Result}; +use std::path::Path; +use tokio::time::Instant; +use yolo_hub_identity::HubId; +use yolo_hub_management_client::execute; +use yolo_hub_management_protocol::ConfigurationSnapshot; +use yolo_hub_rpc_protocol::Command as HubRpcCommand; + +pub(crate) use consumers::consumers; +pub(crate) use list::list; +pub(crate) use pin::pin; +pub(crate) use import::run as import; +pub(crate) use remove::run as remove; + +pub(super) async fn read( + socket: &Path, + entry: HubId, + deadline: Instant, +) -> Result { + execute(socket, entry, HubRpcCommand::GetConfiguration {}, deadline) + .await + .context(HUB_CONFIGURATION_READ_FAILED) +} diff --git a/clients/tui/hub/cli/src/machine/pin.rs b/clients/tui/hub/cli/src/machine/pin.rs new file mode 100644 index 00000000..c3a73529 --- /dev/null +++ b/clients/tui/hub/cli/src/machine/pin.rs @@ -0,0 +1,35 @@ +use crate::constants::*; +use crate::output; +use crate::PinMachine; +use anyhow::{Context, Result}; +use std::path::Path; +use tokio::time::Instant; +use yolo_hub_identity::HubId; +use yolo_hub_management_client::execute; +use yolo_hub_rpc_protocol::Command as HubRpcCommand; + +pub(crate) async fn pin( + socket: &Path, + entry: HubId, + args: &PinMachine, + deadline: Instant, +) -> Result<()> { + let snapshot = super::read(socket, entry, deadline).await?; + let change = crate::pin_change(&snapshot, args)?; + let updated = execute(socket, entry, HubRpcCommand::Configure { change }, deadline) + .await + .context(HUB_CONFIGURATION_WRITE_FAILED)?; + if args.json { + println!("{}", output::machine_json(&updated, args.machine)?); + } else { + println!( + "pinned machine {} at revision {}", + args.machine, updated.state.revision + ); + if !updated.applied { + println!("configuration is persisted but not active"); + } + } + Ok(()) +} + diff --git a/clients/tui/hub/cli/src/machine/remove.rs b/clients/tui/hub/cli/src/machine/remove.rs new file mode 100644 index 00000000..5da143c0 --- /dev/null +++ b/clients/tui/hub/cli/src/machine/remove.rs @@ -0,0 +1,37 @@ +use crate::configuration::remove_change; +use crate::constants::*; +use anyhow::{Context, Result}; +use std::path::Path; +use tokio::time::Instant; +use yolo_hub_identity::HubId; +use yolo_hub_management_client::execute; +use yolo_hub_rpc_protocol::Command as HubRpcCommand; + +pub(crate) async fn run( + socket: &Path, + entry: HubId, + machine: HubId, + json: bool, + deadline: Instant, +) -> Result<()> { + let snapshot = super::read(socket, entry, deadline).await?; + let change = remove_change(&snapshot, machine)?; + let updated = execute(socket, entry, HubRpcCommand::Configure { change }, deadline) + .await + .context(HUB_CONFIGURATION_WRITE_FAILED)?; + if json { + println!( + "{}", + serde_json::json!({ + "removed": machine.to_string(), + "applied": updated.applied, + }) + ); + } else { + println!("removed machine {machine}"); + if !updated.applied { + println!("configuration is persisted but not active"); + } + } + Ok(()) +} diff --git a/clients/tui/hub/cli/src/output.rs b/clients/tui/hub/cli/src/output.rs new file mode 100644 index 00000000..e1fb4ca0 --- /dev/null +++ b/clients/tui/hub/cli/src/output.rs @@ -0,0 +1,58 @@ +use crate::constants::{HUB_ID_FIELD, PUBLIC_KEY_FIELD}; +use anyhow::Result; +use yolo_hub_delegation::PeerIdentity; +use yolo_hub_federation_settings::Peer; +use yolo_hub_identity::HubId; +use yolo_hub_management_protocol::ConfigurationSnapshot; + +pub(crate) fn machine_json(snapshot: &ConfigurationSnapshot, hub: HubId) -> Result { + Ok(serde_json::to_string_pretty(&serde_json::json!({ + "revision": snapshot.state.revision, + "applied": snapshot.applied, + "machine": snapshot.state.configuration.peer(hub).map(machine_document), + }))?) +} + +pub(crate) fn list_json(snapshot: &ConfigurationSnapshot) -> Result { + Ok(serde_json::to_string_pretty(&serde_json::json!({ + "revision": snapshot.state.revision, + "applied": snapshot.applied, + "machines": snapshot.state.configuration.peers.iter().map(machine_document).collect::>(), + }))?) +} + +pub(crate) fn identity_json(identity: yolo_hub_delegation::PeerIdentity, peer_socket: &std::path::Path) -> String { + serde_json::json!({ + (HUB_ID_FIELD): identity.hub().to_string(), + (PUBLIC_KEY_FIELD): key_text(identity), + "peer_socket": peer_socket, + }) + .to_string() +} + +fn machine_document(peer: &Peer) -> serde_json::Value { + serde_json::json!({ + "hub": peer.identity.hub().to_string(), + "direction": "machine", + (PUBLIC_KEY_FIELD): key_text(peer.identity), + "dial": peer.dial.as_ref().map(|dial| serde_json::json!({ + "ssh": dial.ssh_target(), + "profile": dial.profile_id(), + "socket": dial.socket(), + })), + "imports": peer.consume, + "exports": peer.exports.iter().map(|policy| serde_json::json!({ + "origin": policy.origin.to_string(), + "rights": policy.rights, + "remaining_reexports": policy.remaining_reexports, + })).collect::>(), + }) +} + +pub(crate) fn key_text(identity: PeerIdentity) -> String { + identity + .key() + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() +} diff --git a/clients/tui/hub/cli/unit_tests/constants.rs b/clients/tui/hub/cli/unit_tests/constants.rs new file mode 100644 index 00000000..243eab6e --- /dev/null +++ b/clients/tui/hub/cli/unit_tests/constants.rs @@ -0,0 +1,4 @@ +pub const PROFILE: &str = "remote-linux"; +pub const SOCKET: &str = "/home/stone/.local/state/yolo/hub/peer.sock"; +pub const PEER_KEY: &str = "1111111111111111111111111111111111111111111111111111111111111111"; +pub const LOCAL_KEY: [u8; 32] = [1; 32]; diff --git a/clients/tui/hub/cli/unit_tests/lib.rs b/clients/tui/hub/cli/unit_tests/lib.rs new file mode 100644 index 00000000..7563bab5 --- /dev/null +++ b/clients/tui/hub/cli/unit_tests/lib.rs @@ -0,0 +1,118 @@ +use super::{PinMachine, import_change, pin_change, remove_change}; +use std::path::PathBuf; +use uuid::Uuid; +use yolo_hub_delegation::PeerIdentity; +use yolo_hub_federation_settings::Configuration; +use yolo_hub_identity::HubId; +use yolo_hub_management_protocol::{ + CONFIGURATION_SCHEMA, ConfigurationSnapshot, ConfigurationState, +}; + +mod constants; +use constants::*; + +fn snapshot() -> ConfigurationSnapshot { + ConfigurationSnapshot { + state: ConfigurationState { + schema_version: CONFIGURATION_SCHEMA, + identity: PeerIdentity::new(HubId::new(), LOCAL_KEY).unwrap(), + revision: 1, + last_change: Uuid::new_v4(), + configuration: Configuration::default(), + }, + applied: true, + peer_socket: Some(SOCKET.into()), + } +} + +fn args(ssh: Option<&str>, remote_socket: Option<&str>) -> PinMachine { + PinMachine { + machine: HubId::new(), + key: PEER_KEY.into(), + ssh: ssh.map(str::to_owned), + remote_socket: remote_socket.map(PathBuf::from), + pin_only: false, + exports: vec![], + export_local: true, + json: false, + } +} + +#[test] +fn pins_a_source_with_an_ssh_socket_dial_and_exports() { + let change = pin_change(&snapshot(), &args(Some(PROFILE), Some(SOCKET))).unwrap(); + let peer = &change.configuration.peers[0]; + let dial = peer.dial.as_ref().unwrap(); + assert_eq!(dial.ssh_target(), Some(PROFILE)); + assert_eq!(dial.socket(), PathBuf::from(SOCKET)); + assert!(peer.consume); + assert_eq!(peer.exports.len(), 1); + assert_eq!(change.expected_revision, 1); +} + +#[test] +fn dial_flags_are_paired_and_the_key_is_validated() { + assert!(pin_change(&snapshot(), &args(None, Some(SOCKET))).is_err()); + assert!(pin_change(&snapshot(), &args(Some(PROFILE), None)).is_err()); + let mut invalid = args(None, None); + invalid.key = "not-a-key".into(); + assert!(pin_change(&snapshot(), &invalid).is_err()); +} + +#[test] +fn a_source_can_be_pinned_without_a_dial() { + let change = pin_change(&snapshot(), &args(None, None)).unwrap(); + assert!(change.configuration.peers[0].dial.is_none()); +} + +#[test] +fn import_upserts_the_dial_and_remove_deletes_the_source() { + let peer = PeerIdentity::new(HubId::new(), [0x11; 32]).unwrap(); + let mut snapshot = snapshot(); + let change = import_change( + &snapshot, + PROFILE.into(), + PathBuf::from(SOCKET), + peer, + false, + ) + .unwrap() + .unwrap(); + let joined = &change.configuration.peers[0]; + assert_eq!(joined.identity, peer); + assert_eq!( + joined.dial.as_ref().unwrap().ssh_target(), + Some(PROFILE) + ); + assert!(joined.consume); + snapshot.state.configuration = change.configuration; + assert!( + import_change( + &snapshot, + PROFILE.into(), + PathBuf::from(SOCKET), + peer, + false + ) + .unwrap() + .is_none() + ); + let removed = remove_change(&snapshot, peer.hub()).unwrap(); + assert!(removed.configuration.peers.is_empty()); + assert!(remove_change(&snapshot, HubId::new()).is_err()); +} + +#[test] +fn import_with_pin_only_does_not_consume() { + let peer = PeerIdentity::new(HubId::new(), [0x22; 32]).unwrap(); + let change = import_change( + &snapshot(), + PROFILE.into(), + PathBuf::from(SOCKET), + peer, + true, + ) + .unwrap() + .unwrap(); + assert!(!change.configuration.peers[0].consume); +} diff --git a/clients/tui/hub/manager/src/constants.rs b/clients/tui/hub/manager/src/constants.rs index 09d31ae3..743f7ddc 100644 --- a/clients/tui/hub/manager/src/constants.rs +++ b/clients/tui/hub/manager/src/constants.rs @@ -1,5 +1,5 @@ -pub const TITLE: &str = "Hub Configuration"; -pub const OPEN_LABEL: &str = "Configure Hub"; +pub const TITLE: &str = "Machines & Sharing"; +pub const OPEN_LABEL: &str = "Manage machines"; pub(crate) const LOADING: &str = "Loading configuration"; pub(crate) const SAVING: &str = "Applying configuration"; pub(crate) const READY: &str = "Configuration active"; @@ -9,10 +9,14 @@ pub(crate) const INVALID: &str = "Invalid configuration field"; pub(crate) const UNKNOWN: &str = "Change outcome unknown"; pub(crate) const UNCONFIRMED: &str = "Change is not yet confirmed"; pub(crate) const INPUT_REJECTED: &str = "Input exceeds field limits or contains control characters"; -pub(crate) const CONNECTIONS: &str = "Connections"; +pub(crate) const CONNECTIONS: &str = "Machines"; +pub(crate) const CONSUMERS: &str = "Consumers"; +pub(crate) const REFRESH_CONSUMERS: &str = "Refresh Consumers"; +pub(crate) const CONSUMERS_EMPTY: &str = "No machine is importing from this machine"; +pub(crate) const LINKS: &str = "links"; +pub(crate) const CONNECTED: &str = "connected"; pub(crate) const ORIGINS: &str = "Trusted Origins"; pub(crate) const EXPORTS: &str = "Export Policies"; -pub(crate) const LISTENER: &str = "Listener"; pub(crate) const FILE_ROOTS: &str = "File Roots"; pub(crate) const ADD_FILE_ROOT: &str = "Add File Root"; pub(crate) const EDIT_FILE_ROOT: &str = "Edit File Root"; @@ -20,10 +24,10 @@ pub(crate) const REMOVE_FILE_ROOT: &str = "Remove File Root"; pub(crate) const FILE_ROOT_ID: &str = "Root ID"; pub(crate) const FILE_ROOT_NAME: &str = "Name"; pub(crate) const FILE_ROOT_PATH: &str = "Directory"; -pub(crate) const LISTENING: &str = "Listening"; -pub(crate) const ADD_PEER: &str = "Add Connection"; -pub(crate) const EDIT_PEER: &str = "Edit Connection"; -pub(crate) const REMOVE_PEER: &str = "Remove Connection and Its Exports"; +pub(crate) const LISTENING: &str = "Peer socket"; +pub(crate) const ADD_PEER: &str = "Add Machine"; +pub(crate) const EDIT_PEER: &str = "Edit Machine"; +pub(crate) const REMOVE_PEER: &str = "Remove Machine and Its Exports"; pub(crate) const ADD_ORIGIN: &str = "Add Trusted Origin"; pub(crate) const REMOVE_ORIGIN: &str = "Remove Origin and Dependent Exports"; pub(crate) const ADD_EXPORT: &str = "Add Export"; @@ -38,11 +42,11 @@ pub(crate) const CANCEL: &str = "Cancel"; pub(crate) const REMOVE: &str = "Remove Export"; pub(crate) const CONFIRM: &str = "Confirm"; pub(crate) const DISCARD: &str = "Discard Unsaved Configuration"; -pub(crate) const IMPORTS: &str = "Import Resources"; +pub(crate) const IMPORTS: &str = "Import from Machine"; pub(crate) const HUB_ID: &str = "Hub ID"; pub(crate) const PUBLIC_KEY: &str = "Public Key"; -pub(crate) const HOST: &str = "Dial Host"; -pub(crate) const PORT: &str = "Dial Port"; +pub(crate) const SSH_TARGET: &str = "SSH"; +pub(crate) const REMOTE_SOCKET: &str = "Remote Socket"; pub(crate) const ORIGIN: &str = "Origin Hub"; pub(crate) const OBSERVE: &str = "Observe"; pub(crate) const READ_TERMINAL: &str = "Read Terminal"; @@ -54,8 +58,7 @@ pub(crate) const CONVERSATION_READ: &str = "Read Conversations"; pub(crate) const PRESENTATION_READ: &str = "Read Layouts"; pub(crate) const PRESENTATION_WRITE: &str = "Edit Layouts"; pub(crate) const REEXPORTS: &str = "Reexport Depth"; -pub(crate) const LISTEN_ADDRESS: &str = "Bind Address"; -pub(crate) const INBOUND: &str = "Inbound"; +pub(crate) const INBOUND: &str = "Pinned · no dial"; pub(crate) const IMPORT_ON: &str = "import"; pub(crate) const IMPORT_OFF: &str = "no import"; pub(crate) const HEX_DIGITS_PER_BYTE: usize = 2; diff --git a/clients/tui/hub/manager/src/fields.rs b/clients/tui/hub/manager/src/fields.rs index 1f40e414..ef9cc66e 100644 --- a/clients/tui/hub/manager/src/fields.rs +++ b/clients/tui/hub/manager/src/fields.rs @@ -5,8 +5,8 @@ use yolo_tui_line_input::Cursor; pub(crate) enum FieldId { Hub, Key, - Host, - Port, + Ssh, + Socket, Import, Origin, Observe, @@ -19,7 +19,6 @@ pub(crate) enum FieldId { PresentationRead, PresentationWrite, Reexports, - Listen, RootId, RootName, RootPath, @@ -59,8 +58,8 @@ impl Field { match self.id { FieldId::Hub => HUB_ID, FieldId::Key => PUBLIC_KEY, - FieldId::Host => HOST, - FieldId::Port => PORT, + FieldId::Ssh => SSH_TARGET, + FieldId::Socket => REMOTE_SOCKET, FieldId::Import => IMPORTS, FieldId::Origin => ORIGIN, FieldId::Observe => OBSERVE, @@ -73,7 +72,6 @@ impl Field { FieldId::PresentationRead => PRESENTATION_READ, FieldId::PresentationWrite => PRESENTATION_WRITE, FieldId::Reexports => REEXPORTS, - FieldId::Listen => LISTEN_ADDRESS, FieldId::RootId => FILE_ROOT_ID, FieldId::RootName => FILE_ROOT_NAME, FieldId::RootPath => FILE_ROOT_PATH, diff --git a/clients/tui/hub/manager/src/form.rs b/clients/tui/hub/manager/src/form.rs index b0a4950a..6973c1fc 100644 --- a/clients/tui/hub/manager/src/form.rs +++ b/clients/tui/hub/manager/src/form.rs @@ -3,7 +3,7 @@ use crate::{ wire_fields::key_text, }; use yolo_hub_delegation::PeerIdentity; -use yolo_hub_federation_settings::{DialTransport, ExportPolicy, Peer}; +use yolo_hub_federation_settings::{ExportPolicy, Peer}; use yolo_hub_identity::{FileRootId, HubId}; use yolo_hub_routing::Right; @@ -15,7 +15,6 @@ pub(crate) enum FormKind { peer: HubId, original: Option, }, - Listener, FileRoot { id: FileRootId, existing: bool, @@ -43,9 +42,6 @@ impl Form { pub fn peer(peer: Option<&Peer>) -> Self { let identity = peer.map(|peer| peer.identity); let read_only = identity.is_some(); - let managed_transport = peer - .and_then(|peer| peer.dial.as_ref()) - .is_some_and(|dial| matches!(dial.transport, DialTransport::Ssh { .. })); Self::new( FormKind::Peer(identity), vec![ @@ -60,18 +56,18 @@ impl Form { read_only, ), Field::text( - FieldId::Host, + FieldId::Ssh, peer.and_then(|peer| peer.dial.as_ref()) - .map(|dial| dial.host.clone()) + .map(|dial| dial.ssh_target().or_else(|| dial.profile_id()).unwrap_or_default().to_owned()) .unwrap_or_default(), - managed_transport, + false, ), Field::text( - FieldId::Port, + FieldId::Socket, peer.and_then(|peer| peer.dial.as_ref()) - .map(|dial| dial.port.to_string()) + .map(|dial| dial.socket().display().to_string()) .unwrap_or_default(), - managed_transport, + false, ), Field::flag(FieldId::Import, peer.is_none_or(|peer| peer.consume), false), ], @@ -86,18 +82,6 @@ impl Form { ], ) } - pub fn listener(address: Option) -> Self { - Self::new( - FormKind::Listener, - vec![Field::text( - FieldId::Listen, - address - .map(|address| address.to_string()) - .unwrap_or_default(), - false, - )], - ) - } pub fn export(peer: HubId, local: HubId, policy: Option<&ExportPolicy>) -> Self { let rights = policy .map(|policy| policy.rights.clone()) diff --git a/clients/tui/hub/manager/src/form_commit.rs b/clients/tui/hub/manager/src/form_commit.rs index 93c5f7a3..930d3864 100644 --- a/clients/tui/hub/manager/src/form_commit.rs +++ b/clients/tui/hub/manager/src/form_commit.rs @@ -6,7 +6,7 @@ use crate::{ }; use anyhow::{Context, Result, ensure}; use yolo_hub_delegation::PeerIdentity; -use yolo_hub_federation_settings::{Configuration, DialTransport, ExportPolicy, Peer, PeerAddress}; +use yolo_hub_federation_settings::{Configuration, ExportPolicy, Peer, PeerAddress}; use yolo_hub_routing::Right; impl Form { @@ -22,24 +22,13 @@ impl Form { previous.is_none_or(|previous| previous == identity), INVALID ); - let host = text(&self.fields, FieldId::Host)?; - let port = text(&self.fields, FieldId::Port)?; - ensure!(!host.is_empty() || port.is_empty(), INVALID); - let existing_ssh = previous - .and_then(|previous| draft.peer(previous.hub())) - .and_then(|peer| peer.dial.as_ref()) - .filter(|dial| matches!(dial.transport, DialTransport::Ssh { .. })); - let dial = if let Some(existing) = existing_ssh { - ensure!( - host == existing.host - && port.parse::()? == existing.port, - INVALID - ); - Some(existing.clone()) - } else if host.is_empty() { + let profile = text(&self.fields, FieldId::Ssh)?; + let socket = text(&self.fields, FieldId::Socket)?; + ensure!(profile.is_empty() == socket.is_empty(), INVALID); + let dial = if profile.is_empty() { None } else { - Some(PeerAddress::tcp(host, port.parse()?)) + Some(PeerAddress::ssh(profile, socket.into())?) }; let exports = previous .map(|previous| { @@ -77,14 +66,6 @@ impl Form { ); next.trusted_origins.push(identity); } - FormKind::Listener => { - let value = text(&self.fields, FieldId::Listen)?; - next.listen = if value.is_empty() { - None - } else { - Some(value.parse()?) - }; - } FormKind::Export { peer, original } => { let origin = text(&self.fields, FieldId::Origin)?.parse()?; ensure!(original.is_none_or(|previous| previous == origin), INVALID); diff --git a/clients/tui/hub/manager/src/form_input.rs b/clients/tui/hub/manager/src/form_input.rs index 1fd40f56..2f1fd9c7 100644 --- a/clients/tui/hub/manager/src/form_input.rs +++ b/clients/tui/hub/manager/src/form_input.rs @@ -2,9 +2,12 @@ use crate::{ fields::{FieldId, Value}, form::Form, }; -use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; +use crossterm::event::{Event, KeyCode, KeyEvent}; use yolo_hub_delegation::MAX_DELEGATION_DEPTH; -use yolo_hub_federation_settings::MAX_HOST_BYTES; +use yolo_hub_federation_settings::MAX_SOCKET_PATH_BYTES; +use yolo_hub_federation_settings::MAX_SSH_TARGET_BYTES; + +const MAX_TEXT_BYTES: usize = 128; impl Form { pub fn paste(&mut self, value: &str) -> bool { @@ -60,8 +63,10 @@ impl Form { fn text_limit(id: FieldId) -> usize { match id { + FieldId::Ssh => MAX_SSH_TARGET_BYTES, + FieldId::Socket => MAX_SOCKET_PATH_BYTES, FieldId::RootName => yolo_hub_file_protocol::MAX_FILE_ROOT_NAME_BYTES, FieldId::RootPath => yolo_hub_file_protocol::MAX_FILE_PATH_BYTES, - _ => MAX_HOST_BYTES, + _ => MAX_TEXT_BYTES, } } diff --git a/clients/tui/hub/manager/src/form_render.rs b/clients/tui/hub/manager/src/form_render.rs index e10c33eb..d781aa3c 100644 --- a/clients/tui/hub/manager/src/form_render.rs +++ b/clients/tui/hub/manager/src/form_render.rs @@ -18,7 +18,6 @@ pub(crate) fn render(frame: &mut Frame<'_>, form: &Form, area: Rect, epoch: Uuid FormKind::Peer(Some(_)) => EDIT_PEER, FormKind::Origin => ADD_ORIGIN, FormKind::Export { .. } => EXPORTS, - FormKind::Listener => LISTENER, FormKind::FileRoot { existing: false, .. } => ADD_FILE_ROOT, diff --git a/clients/tui/hub/manager/src/input.rs b/clients/tui/hub/manager/src/input.rs index 0f6754ca..67a2ad3d 100644 --- a/clients/tui/hub/manager/src/input.rs +++ b/clients/tui/hub/manager/src/input.rs @@ -25,6 +25,17 @@ impl Manager { return None; } if self.pending.is_some() { + if key.code == KeyCode::Esc + && self + .pending + .as_ref() + .is_some_and(|pending| pending.consumers) + { + self.pending = None; + self.epoch = uuid::Uuid::new_v4(); + self.status = READY.into(); + return None; + } return (key.code == KeyCode::Esc).then_some(Effect::Close); } if let Some(confirmation) = &mut self.confirmation { @@ -128,7 +139,12 @@ impl Manager { } fn perform(&mut self, action: Action) -> Option { match action { - Action::Page(page) => self.page(page), + Action::Page(page) => { + self.page(page); + if page == Page::Consumers { + return Some(Effect::Request(self.consumers_query())); + } + } Action::AddPeer => self.form = Some(Form::peer(None)), Action::EditPeer(hub) => { self.form = self @@ -151,12 +167,6 @@ impl Manager { }) .map(|root| Form::file_root(Some(root))); } - Action::Listener => { - self.form = self - .draft - .as_ref() - .map(|draft| Form::listener(draft.listen)) - } Action::AddExport(peer) => self.form = Some(Form::export(peer, self.entry, None)), Action::EditExport(peer, origin) => { self.form = self @@ -170,9 +180,13 @@ impl Manager { | Action::RemoveExport(_, _) | Action::RemoveFileRoot(_) => self.remove(action), Action::Apply => return self.proposal().map(Effect::Request), + Action::RefreshConsumers => { + return Some(Effect::Request(self.consumers_query())); + } Action::Reload => return Some(Effect::Request(self.query(false))), Action::Check => return Some(Effect::Request(self.query(true))), Action::Close => return Some(Effect::Close), + Action::None => {} } None } diff --git a/clients/tui/hub/manager/src/menu.rs b/clients/tui/hub/manager/src/menu.rs index 5d71a85d..f8b90e58 100644 --- a/clients/tui/hub/manager/src/menu.rs +++ b/clients/tui/hub/manager/src/menu.rs @@ -32,20 +32,15 @@ impl Manager { Action::Page(Page::Peers), format!("{CONNECTIONS} ({})", draft.peers.len()), )); + let consumers = self.consumers.as_ref().map_or_else( + || CONSUMERS.into(), + |list| format!("{CONSUMERS} ({})", list.len()), + ); + items.push(Item::new(Action::Page(Page::Consumers), consumers)); items.push(Item::new( Action::Page(Page::Origins), format!("{ORIGINS} ({})", draft.trusted_origins.len()), )); - items.push(Item::new( - Action::Listener, - format!( - "{LISTENER}: {}", - draft - .listen - .map(|address| address.to_string()) - .unwrap_or_default() - ), - )); items.push(Item { action: Action::Page(Page::FileRoots), label: format!("{FILE_ROOTS} ({})", draft.file_roots.len()), @@ -70,11 +65,14 @@ impl Manager { } Page::Peers => { for peer in &draft.peers { - let dial = peer - .dial - .as_ref() - .map(|dial| format!("{}:{}", dial.host, dial.port)) - .unwrap_or_else(|| INBOUND.into()); + let dial = peer.dial.as_ref().map_or_else( + || INBOUND.into(), + |dial| format!( + "{}:{}", + dial.ssh_target().or_else(|| dial.profile_id()).unwrap_or_default(), + dial.socket().display() + ), + ); items.push(Item::new( Action::Page(Page::Peer(peer.identity.hub())), format!( @@ -87,6 +85,32 @@ impl Manager { items.push(Item::new(Action::AddPeer, ADD_PEER)); items.push(Item::new(Action::Page(Page::Root), BACK)); } + Page::Consumers => { + match &self.consumers { + Some(consumers) if consumers.is_empty() => items.push(Item { + action: Action::None, + label: CONSUMERS_EMPTY.into(), + enabled: false, + }), + Some(consumers) => { + for consumer in consumers { + items.push(Item { + action: Action::None, + label: format!( + "{} {} {LINKS} {CONNECTED} {}", + consumer.hub, + consumer.links, + age(consumer.connected_since_ms) + ), + enabled: false, + }); + } + } + None => {} + } + items.push(Item::new(Action::RefreshConsumers, REFRESH_CONSUMERS)); + items.push(Item::new(Action::Page(Page::Root), BACK)); + } Page::Peer(hub) => { items.push(Item::new(Action::EditPeer(hub), EDIT_PEER)); items.push(Item::new(Action::Page(Page::Exports(hub)), EXPORTS)); @@ -136,3 +160,16 @@ impl Manager { items } } + +fn age(since_ms: u64) -> String { + let now = std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .map(|elapsed| elapsed.as_millis() as u64) + .unwrap_or(since_ms); + let seconds = now.saturating_sub(since_ms) / 1_000; + match seconds { + 0..=59 => format!("{seconds}s"), + 60..=3_599 => format!("{}m", seconds / 60), + _ => format!("{}h", seconds / 3_600), + } +} diff --git a/clients/tui/hub/manager/src/model.rs b/clients/tui/hub/manager/src/model.rs index d129899c..eb80a628 100644 --- a/clients/tui/hub/manager/src/model.rs +++ b/clients/tui/hub/manager/src/model.rs @@ -3,7 +3,7 @@ use uuid::Uuid; use yolo_hub_federation_settings::Configuration; use yolo_hub_identity::{FileRootId, HubId}; use yolo_hub_management_protocol::{ConfigurationChange, ConfigurationSnapshot}; -use yolo_hub_rpc_protocol::Command; +use yolo_hub_rpc_protocol::{Command, ConsumerSummary}; #[derive(Clone, Debug)] pub struct Operation { @@ -22,6 +22,7 @@ pub(crate) enum Page { Origins, FileRoots, Exports(HubId), + Consumers, } #[derive(Clone, Debug, Eq, PartialEq)] pub(crate) enum Action { @@ -34,10 +35,11 @@ pub(crate) enum Action { AddExport(HubId), EditExport(HubId, HubId), RemoveExport(HubId, HubId), - Listener, AddFileRoot, EditFileRoot(FileRootId), RemoveFileRoot(yolo_hub_file_protocol::FileRootConfiguration), + RefreshConsumers, + None, Apply, Reload, Check, @@ -47,6 +49,7 @@ pub(crate) struct Pending { pub token: Uuid, pub change: Option, pub reconcile: bool, + pub consumers: bool, } pub(crate) struct Confirmation { pub action: Action, @@ -59,6 +62,7 @@ pub struct Manager { pub(crate) entry: HubId, pub(crate) base: Option, pub(crate) draft: Option, + pub(crate) consumers: Option>, pub(crate) page: Page, pub(crate) selected: usize, pub(crate) form: Option
, @@ -75,6 +79,7 @@ impl Manager { entry, base: None, draft: None, + consumers: None, page: Page::Root, selected: 0, form: None, @@ -102,6 +107,7 @@ impl Manager { token, change: None, reconcile, + consumers: false, }); self.status = LOADING.into(); Operation { @@ -109,6 +115,20 @@ impl Manager { command: Command::GetConfiguration {}, } } + pub(crate) fn consumers_query(&mut self) -> Operation { + let token = Uuid::new_v4(); + self.pending = Some(Pending { + token, + change: None, + reconcile: false, + consumers: true, + }); + self.status = LOADING.into(); + Operation { + token, + command: Command::ListConsumers {}, + } + } pub(crate) fn proposal(&mut self) -> Option { if !self.can_apply() { return None; @@ -128,6 +148,7 @@ impl Manager { token: change.request_id, change: Some(change.clone()), reconcile: false, + consumers: false, }); self.status = SAVING.into(); Some(Operation { diff --git a/clients/tui/hub/manager/src/render.rs b/clients/tui/hub/manager/src/render.rs index d785b360..51e9bae9 100644 --- a/clients/tui/hub/manager/src/render.rs +++ b/clients/tui/hub/manager/src/render.rs @@ -23,8 +23,8 @@ pub fn render(frame: &mut Frame<'_>, manager: &Manager, area: Rect) -> View { base.state.identity.hub(), key_text(base.state.identity) ); - match base.listen_address { - Some(address) => format!("{identity}\n{LISTENING}: {address}"), + match &base.peer_socket { + Some(path) => format!("{identity}\n{LISTENING}: {}", path.display()), None => identity, } }) @@ -86,6 +86,7 @@ fn menu(frame: &mut Frame<'_>, manager: &Manager, area: Rect) -> View { Page::Peers => CONNECTIONS.into(), Page::Origins => ORIGINS.into(), Page::FileRoots => FILE_ROOTS.into(), + Page::Consumers => CONSUMERS.into(), Page::Peer(hub) => format!("{CONNECTIONS}: {hub}"), Page::Exports(hub) => format!("{EXPORTS}: {hub}"), }; diff --git a/clients/tui/hub/manager/src/response.rs b/clients/tui/hub/manager/src/response.rs index 6955e3a7..352235a3 100644 --- a/clients/tui/hub/manager/src/response.rs +++ b/clients/tui/hub/manager/src/response.rs @@ -4,13 +4,10 @@ use yolo_hub_management_client::ManagementError; use yolo_hub_management_protocol::{ CONFIGURATION_REVISION_STEP, ConfigurationChange, ConfigurationSnapshot, }; +use yolo_hub_rpc_protocol::Reply; impl Manager { - pub fn complete( - &mut self, - token: Uuid, - result: Result, - ) { + pub fn complete(&mut self, token: Uuid, result: Result) { if self .pending .as_ref() @@ -20,6 +17,28 @@ impl Manager { } let pending = self.pending.take().unwrap(); self.epoch = Uuid::new_v4(); + if pending.consumers { + match result { + Ok(Reply::Consumers { consumers }) => { + self.consumers = Some(consumers); + self.status = READY.into(); + } + Ok(_) => { + self.consumers = None; + self.status = ManagementError::Unavailable.to_string(); + } + Err(error) => { + self.consumers = None; + self.status = error.to_string(); + } + } + return; + } + let result = result.and_then(|reply| match reply { + Reply::Configuration { snapshot } => Ok(snapshot), + _ if pending.change.is_some() => Err(ManagementError::OutcomeUnknown), + _ => Err(ManagementError::Unavailable), + }); match result { Ok(snapshot) if snapshot.validate().is_ok() diff --git a/clients/tui/hub/manager/test_support/fixture.rs b/clients/tui/hub/manager/test_support/fixture.rs index 845ffa61..4b3a7e30 100644 --- a/clients/tui/hub/manager/test_support/fixture.rs +++ b/clients/tui/hub/manager/test_support/fixture.rs @@ -18,17 +18,20 @@ pub fn snapshot() -> ConfigurationSnapshot { configuration: Configuration::default(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), } } +pub fn configuration(snapshot: ConfigurationSnapshot) -> yolo_hub_rpc_protocol::Reply { + yolo_hub_rpc_protocol::Reply::Configuration { snapshot } +} pub fn manager() -> Manager { let snapshot = snapshot(); let (mut manager, request) = Manager::open(snapshot.state.identity.hub()); - manager.complete(request.token, Ok(snapshot)); + manager.complete(request.token, Ok(configuration(snapshot))); manager } pub fn edit(manager: &mut Manager) { - manager.draft.as_mut().unwrap().listen = Some("127.0.0.1:2222".parse().unwrap()); + manager.draft.as_mut().unwrap().trusted_origins.push(peer_identity()); } pub fn key(manager: &mut Manager, code: crossterm::event::KeyCode) -> Option { @@ -75,6 +78,14 @@ pub fn add_peer(manager: &mut Manager, identity: PeerIdentity) { FieldId::Key, &crate::wire_fields::key_text(identity), ); + // This helper adds a provider-only pin with no dial, so import defaults off. + let form = manager.form.as_mut().unwrap(); + form.focused = form + .fields + .iter() + .position(|field| field.id == FieldId::Import) + .unwrap(); + key(manager, crossterm::event::KeyCode::Char(' ')); save(manager); assert!(manager.form.is_none()); } diff --git a/clients/tui/hub/manager/test_support/ssh_constants.rs b/clients/tui/hub/manager/test_support/ssh_constants.rs index c5a6affc..2a395cec 100644 --- a/clients/tui/hub/manager/test_support/ssh_constants.rs +++ b/clients/tui/hub/manager/test_support/ssh_constants.rs @@ -1,5 +1,3 @@ -pub const PROFILE_ID: &str = "test-machine"; -pub const DIGEST_CHARACTER: &str = "a"; -pub const DIGEST_BYTES: usize = 64; -pub const ENDPOINT: &str = "127.0.0.1:4242"; -pub const OTHER_HOST: &str = "localhost"; +pub const PROFILE: &str = "test-machine"; +pub const SOCKET: &str = "/run/user/1000/yolo/hub/peer.sock"; +pub const CHANGED_PROFILE: &str = "other-machine"; diff --git a/clients/tui/hub/manager/unit_tests/connections.rs b/clients/tui/hub/manager/unit_tests/connections.rs index 526d0f41..d59594c2 100644 --- a/clients/tui/hub/manager/unit_tests/connections.rs +++ b/clients/tui/hub/manager/unit_tests/connections.rs @@ -4,6 +4,9 @@ use crate::{ model::{Action, Page}, }; use crossterm::event::KeyCode; +#[path = "../test_support/ssh_constants.rs"] +mod constants; +use constants::*; #[test] fn peer_creation_editing_and_dial_direction_are_draft_only() { @@ -17,18 +20,20 @@ fn peer_creation_editing_and_dial_direction_are_draft_only() { FieldId::Key, &crate::wire_fields::key_text(pin), ); - field(&mut manager, FieldId::Host, "localhost"); - field(&mut manager, FieldId::Port, "2222"); + field(&mut manager, FieldId::Ssh, PROFILE); + field(&mut manager, FieldId::Socket, SOCKET); save(&mut manager); assert!(manager.form.is_none() && manager.dirty() && !manager.pending()); let peer = manager.draft.as_ref().unwrap().peer(pin.hub()).unwrap(); assert!(peer.consume && peer.exports.is_empty()); - assert_eq!(peer.dial.as_ref().unwrap().host, "localhost"); + let dial = peer.dial.as_ref().unwrap(); + assert_eq!(dial.ssh_target(), Some(PROFILE)); + assert_eq!(dial.socket().to_str(), Some(SOCKET)); activate(&mut manager, Action::Page(Page::Peer(pin.hub()))); activate(&mut manager, Action::EditPeer(pin.hub())); field(&mut manager, FieldId::Key, "ff"); - field(&mut manager, FieldId::Host, ""); - field(&mut manager, FieldId::Port, ""); + field(&mut manager, FieldId::Ssh, ""); + field(&mut manager, FieldId::Socket, ""); let form = manager.form.as_mut().unwrap(); form.focused = form .fields @@ -57,8 +62,8 @@ fn malformed_identity_duplicate_peer_and_invalid_dial_are_atomic_rejections() { for (id, value) in [ (FieldId::Hub, "invalid"), (FieldId::Key, "00"), - (FieldId::Host, "bad host"), - (FieldId::Port, "0"), + (FieldId::Ssh, "bad host"), + (FieldId::Socket, "relative.sock"), ] { let mut manager = manager(); let pin = peer_identity(); @@ -70,8 +75,8 @@ fn malformed_identity_duplicate_peer_and_invalid_dial_are_atomic_rejections() { FieldId::Key, &crate::wire_fields::key_text(pin), ); - field(&mut manager, FieldId::Host, "localhost"); - field(&mut manager, FieldId::Port, "2222"); + field(&mut manager, FieldId::Ssh, PROFILE); + field(&mut manager, FieldId::Socket, SOCKET); field(&mut manager, id, value); save(&mut manager); assert!(manager.form.is_some() && !manager.dirty()); @@ -109,15 +114,20 @@ fn removing_peer_requires_confirmation_and_preserves_runtime_independent_configu } #[test] -fn listener_validates_addresses_and_reload_does_not_silently_discard_drafts() { +fn peer_socket_edits_reject_partial_pairs_and_reload_does_not_silently_discard_drafts() { let mut manager = manager(); - activate(&mut manager, Action::Listener); - field(&mut manager, FieldId::Listen, "bad-address"); + let pin = peer_identity(); + add_peer(&mut manager, pin); + activate(&mut manager, Action::Page(Page::Peer(pin.hub()))); + activate(&mut manager, Action::EditPeer(pin.hub())); + let before = manager.draft.clone(); + field(&mut manager, FieldId::Ssh, PROFILE); save(&mut manager); - assert!(!manager.dirty()); - field(&mut manager, FieldId::Listen, "[::1]:0"); + assert_eq!(manager.draft, before); + field(&mut manager, FieldId::Socket, SOCKET); save(&mut manager); assert!(manager.dirty()); + manager.page(crate::model::Page::Root); activate(&mut manager, Action::Reload); key(&mut manager, KeyCode::Esc); assert!(manager.dirty() && !manager.pending()); @@ -132,12 +142,16 @@ fn listener_validates_addresses_and_reload_does_not_silently_discard_drafts() { #[test] fn saving_an_unchanged_form_does_not_claim_unsaved_configuration() { let mut manager = manager(); - activate(&mut manager, Action::Listener); + let pin = peer_identity(); + add_peer(&mut manager, pin); + manager.base.as_mut().unwrap().state.configuration = manager.draft.clone().unwrap(); + activate(&mut manager, Action::Page(Page::Peer(pin.hub()))); + activate(&mut manager, Action::EditPeer(pin.hub())); save(&mut manager); assert!(!manager.dirty()); assert_eq!(manager.status, crate::constants::READY); manager.base.as_mut().unwrap().applied = false; - activate(&mut manager, Action::Listener); + activate(&mut manager, Action::EditPeer(pin.hub())); save(&mut manager); assert!(!manager.dirty()); assert_eq!(manager.status, crate::constants::INACTIVE); diff --git a/clients/tui/hub/manager/unit_tests/consumers.rs b/clients/tui/hub/manager/unit_tests/consumers.rs new file mode 100644 index 00000000..f4899507 --- /dev/null +++ b/clients/tui/hub/manager/unit_tests/consumers.rs @@ -0,0 +1,83 @@ +use super::fixture::*; +use crate::{ + Effect, + constants::{CONSUMERS_EMPTY, REFRESH_CONSUMERS}, + model::{Action, Page}, +}; +use crossterm::event::KeyCode; +use yolo_hub_identity::HubId; +use yolo_hub_management_client::ManagementError; +use yolo_hub_rpc_protocol::{Command, ConsumerSummary, Reply}; + +fn consumer() -> ConsumerSummary { + ConsumerSummary { + hub: HubId::new(), + connected_since_ms: 1, + links: 2, + } +} + +#[test] +fn consumers_page_lists_live_importers_after_the_local_query() { + let mut manager = manager(); + let Some(Effect::Request(request)) = activate(&mut manager, Action::Page(Page::Consumers)) else { + panic!() + }; + assert_eq!(request.command, Command::ListConsumers {}); + let consumer = consumer(); + manager.complete( + request.token, + Ok(Reply::Consumers { + consumers: vec![consumer.clone()], + }), + ); + assert_eq!(manager.consumers, Some(vec![consumer.clone()])); + assert_eq!(manager.page, Page::Consumers); + let labels: Vec = manager.items().into_iter().map(|item| item.label).collect(); + assert!( + labels + .iter() + .any(|label| label.contains(&consumer.hub.to_string())) + ); + assert!(labels.iter().any(|label| label.contains("2 links"))); + assert!(labels.contains(&REFRESH_CONSUMERS.to_string())); +} + +#[test] +fn consumers_page_reports_an_empty_list_and_a_refresh_failure() { + let mut manager = manager(); + let Some(Effect::Request(first)) = activate(&mut manager, Action::Page(Page::Consumers)) else { + panic!() + }; + manager.complete( + first.token, + Ok(Reply::Consumers { + consumers: Vec::new(), + }), + ); + let labels: Vec = manager.items().into_iter().map(|item| item.label).collect(); + assert!(labels.iter().any(|label| label.as_str() == CONSUMERS_EMPTY)); + let Some(Effect::Request(second)) = activate(&mut manager, Action::RefreshConsumers) else { + panic!() + }; + assert_eq!(second.command, Command::ListConsumers {}); + manager.complete(second.token, Err(ManagementError::Unavailable)); + assert_eq!(manager.consumers, None); +} + +#[test] +fn escaping_a_pending_consumers_query_keeps_the_manager_open() { + let mut manager = manager(); + let Some(Effect::Request(request)) = activate(&mut manager, Action::Page(Page::Consumers)) else { + panic!() + }; + assert!(key(&mut manager, KeyCode::Esc).is_none()); + assert!(!manager.pending()); + manager.complete( + request.token, + Ok(Reply::Consumers { + consumers: vec![consumer()], + }), + ); + assert_eq!(manager.consumers, None); +} diff --git a/clients/tui/hub/manager/unit_tests/files.rs b/clients/tui/hub/manager/unit_tests/files.rs index 02a188cd..9da65462 100644 --- a/clients/tui/hub/manager/unit_tests/files.rs +++ b/clients/tui/hub/manager/unit_tests/files.rs @@ -82,7 +82,7 @@ fn invalid_paths_can_be_corrected_without_creating_another_identity() { fn editing_and_removal_preserve_other_configuration_and_require_confirmation() { let mut manager = manager(); let root = files_support::add(&mut manager); - let listen = manager.draft.as_ref().unwrap().listen; + let peers = manager.draft.as_ref().unwrap().peers.len(); activate(&mut manager, Action::EditFileRoot(root.fields().root_id)); field(&mut manager, FieldId::RootPath, CHANGED_PATH); save(&mut manager); @@ -106,7 +106,7 @@ fn editing_and_removal_preserve_other_configuration_and_require_confirmation() { key(&mut manager, KeyCode::Tab); key(&mut manager, KeyCode::Enter); assert!(manager.draft.as_ref().unwrap().file_roots.is_empty()); - assert_eq!(manager.draft.as_ref().unwrap().listen, listen); + assert_eq!(manager.draft.as_ref().unwrap().peers.len(), peers); assert_eq!(manager.page, Page::FileRoots); assert!(!manager.pending()); } @@ -120,7 +120,7 @@ fn directory_fields_accept_long_paths_but_keep_their_own_protocol_bounds() { "/{}", vec![COMPONENT.repeat(COMPONENT_BYTES); DEEP_COMPONENTS].join("/") ); - assert!(path.len() > yolo_hub_federation_settings::MAX_HOST_BYTES); + assert!(path.len() > yolo_hub_federation_settings::MAX_SOCKET_PATH_BYTES); field(&mut manager, FieldId::RootPath, &path); assert_eq!( crate::wire_fields::raw_text(&manager.form.as_ref().unwrap().fields, FieldId::RootPath) diff --git a/clients/tui/hub/manager/unit_tests/input.rs b/clients/tui/hub/manager/unit_tests/input.rs index c86d8441..1d633a83 100644 --- a/clients/tui/hub/manager/unit_tests/input.rs +++ b/clients/tui/hub/manager/unit_tests/input.rs @@ -8,8 +8,9 @@ use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers}; #[test] fn unicode_text_editing_and_paste_limits_preserve_utf8_and_reject_terminal_controls() { let mut manager = manager(); - activate(&mut manager, Action::Listener); - field(&mut manager, FieldId::Listen, "a\u{e9}z"); + manager.page(Page::Peers); + activate(&mut manager, Action::AddPeer); + field(&mut manager, FieldId::Ssh, "a\u{e9}z"); for code in [ KeyCode::Left, KeyCode::Backspace, @@ -20,19 +21,26 @@ fn unicode_text_editing_and_paste_limits_preserve_utf8_and_reject_terminal_contr ] { key(&mut manager, code); } - let form = manager.form.as_ref().unwrap(); - assert_eq!(form.fields[0].display(), "zx"); + let profile = manager + .form + .as_ref() + .unwrap() + .fields + .iter() + .position(|field| field.id == FieldId::Ssh) + .unwrap(); + assert_eq!(manager.form.as_ref().unwrap().fields[profile].display(), "zx"); manager.handle(Event::Paste("\u{1b}[31m".into())); assert_eq!( - manager.form.as_ref().unwrap().fields[0].display(), + manager.form.as_ref().unwrap().fields[profile].display(), "zx[31m", "terminal controls are filtered, printable paste text is kept" ); manager.handle(Event::Paste( - "a".repeat(yolo_hub_federation_settings::MAX_HOST_BYTES), + "a".repeat(yolo_hub_federation_settings::MAX_SSH_TARGET_BYTES), )); - let display = manager.form.as_ref().unwrap().fields[0].display(); - assert_eq!(display.len(), yolo_hub_federation_settings::MAX_HOST_BYTES); + let display = manager.form.as_ref().unwrap().fields[profile].display(); + assert_eq!(display.len(), yolo_hub_federation_settings::MAX_SSH_TARGET_BYTES); assert!(display.starts_with("zx[31m")); let release = KeyEvent { code: KeyCode::Char('q'), @@ -41,7 +49,7 @@ fn unicode_text_editing_and_paste_limits_preserve_utf8_and_reject_terminal_contr state: crossterm::event::KeyEventState::NONE, }; manager.handle(Event::Key(release)); - assert_eq!(manager.form.as_ref().unwrap().fields[0].display(), display); + assert_eq!(manager.form.as_ref().unwrap().fields[profile].display(), display); } #[test] @@ -52,13 +60,13 @@ fn form_navigation_skips_immutable_identity_fields_and_cancels_without_saving() manager.page(Page::Peer(peer.hub())); activate(&mut manager, Action::EditPeer(peer.hub())); let form = manager.form.as_mut().unwrap(); - assert_eq!(form.fields[form.focused].id, FieldId::Host); + assert_eq!(form.fields[form.focused].id, FieldId::Ssh); form.move_focus(false); assert!(form.focused >= form.fields.len()); form.move_focus(true); - assert_eq!(form.fields[form.focused].id, FieldId::Host); + assert_eq!(form.fields[form.focused].id, FieldId::Ssh); let before = manager.draft.clone(); - field(&mut manager, FieldId::Host, "changed.example"); + field(&mut manager, FieldId::Ssh, "changed.example"); key(&mut manager, KeyCode::Esc); assert_eq!(manager.draft, before); } diff --git a/clients/tui/hub/manager/unit_tests/lib.rs b/clients/tui/hub/manager/unit_tests/lib.rs index 69046d95..86f93336 100644 --- a/clients/tui/hub/manager/unit_tests/lib.rs +++ b/clients/tui/hub/manager/unit_tests/lib.rs @@ -1,5 +1,6 @@ mod agent_control_rights; mod connections; +mod consumers; mod files; mod files_conflicts; #[path = "../test_support/files.rs"] diff --git a/clients/tui/hub/manager/unit_tests/navigation.rs b/clients/tui/hub/manager/unit_tests/navigation.rs index b773bbd8..01815ef7 100644 --- a/clients/tui/hub/manager/unit_tests/navigation.rs +++ b/clients/tui/hub/manager/unit_tests/navigation.rs @@ -8,18 +8,31 @@ use crate::{ use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; #[test] -fn enter_advances_text_fields_and_cancel_button_preserves_the_previous_draft() { +fn enter_saves_a_complete_peer_form_and_cancel_preserves_the_previous_draft() { let mut manager = manager(); - activate(&mut manager, Action::Listener); - field(&mut manager, FieldId::Listen, "127.0.0.1:0"); - key(&mut manager, KeyCode::Enter); - assert_eq!( - manager.form.as_ref().unwrap().focused, - manager.form.as_ref().unwrap().fields.len() + let pin = peer_identity(); + manager.page(Page::Peers); + activate(&mut manager, Action::AddPeer); + field(&mut manager, FieldId::Hub, &pin.hub().to_string()); + field( + &mut manager, + FieldId::Key, + &crate::wire_fields::key_text(pin), + ); + field(&mut manager, FieldId::Ssh, "test-machine"); + field( + &mut manager, + FieldId::Socket, + "/run/user/1000/yolo/hub/peer.sock", ); - key(&mut manager, KeyCode::Tab); - key(&mut manager, KeyCode::Enter); - assert!(manager.form.is_none() && !manager.dirty()); + save(&mut manager); + assert!(manager.form.is_none() && manager.dirty()); + let before = manager.draft.clone(); + manager.page(Page::Peer(pin.hub())); + activate(&mut manager, Action::EditPeer(pin.hub())); + field(&mut manager, FieldId::Ssh, "changed"); + key(&mut manager, KeyCode::Esc); + assert_eq!(manager.draft, before); } #[test] @@ -66,7 +79,7 @@ fn navigation_checks_unknown_results_without_resending_mutations_and_allows_safe #[test] fn clear_navigation_and_unhandled_keys_are_safe_at_field_and_command_boundaries() { - let mut form = Form::listener(None); + let mut form = Form::peer(None); for code in [ KeyCode::Right, KeyCode::Left, @@ -108,7 +121,7 @@ fn errors_and_unapplied_receipts_keep_the_submitted_draft_blocked() { reply.state.last_change = change.request_id; reply.state.configuration = change.configuration; reply.applied = false; - manager.complete(request.token, Ok(reply)); + manager.complete(request.token, Ok(configuration(reply))); assert!(manager.blocked && manager.unresolved.is_some()); let query = manager.query(true); manager.complete( @@ -119,6 +132,6 @@ fn errors_and_unapplied_receipts_keep_the_submitted_draft_blocked() { let query = manager.query(false); let mut malformed = manager.base.clone().unwrap(); malformed.state.schema_version = 0; - manager.complete(query.token, Ok(malformed)); + manager.complete(query.token, Ok(configuration(malformed))); assert!(manager.blocked); } diff --git a/clients/tui/hub/manager/unit_tests/policies.rs b/clients/tui/hub/manager/unit_tests/policies.rs index 45ccccdc..96b3daa7 100644 --- a/clients/tui/hub/manager/unit_tests/policies.rs +++ b/clients/tui/hub/manager/unit_tests/policies.rs @@ -113,7 +113,7 @@ fn trusted_origin_removal_prunes_dependent_exports_but_keeps_an_independent_peer independent.peers.push(yolo_hub_federation_settings::Peer { identity: origin, dial: None, - consume: true, + consume: false, exports: vec![], }); manager.draft = Some(independent.clone()); diff --git a/clients/tui/hub/manager/unit_tests/render_matrix.rs b/clients/tui/hub/manager/unit_tests/render_matrix.rs index 12515296..98bee09e 100644 --- a/clients/tui/hub/manager/unit_tests/render_matrix.rs +++ b/clients/tui/hub/manager/unit_tests/render_matrix.rs @@ -51,8 +51,6 @@ fn every_form_and_action_row_renders_within_compact_and_wide_viewports() { Form::peer(None), Form::peer(Some(&peer)), Form::origin(), - Form::listener(None), - Form::listener(Some("127.0.0.1:0".parse().unwrap())), Form::export(pin.hub(), manager.entry, None), Form::export(pin.hub(), manager.entry, Some(&policy)), Form::file_root(None), @@ -86,9 +84,9 @@ fn mouse_focus_checkbox_cancel_and_confirmation_have_scoped_actions() { add_peer(&mut manager, pin); manager.page(Page::Peer(pin.hub())); activate(&mut manager, Action::EditPeer(pin.hub())); - let host = manager.form.as_ref().unwrap().focused; + let profile = manager.form.as_ref().unwrap().focused; click(&mut manager, 0); - assert_eq!(manager.form.as_ref().unwrap().focused, host); + assert_eq!(manager.form.as_ref().unwrap().focused, profile); let import = manager .form .as_ref() @@ -100,10 +98,10 @@ fn mouse_focus_checkbox_cancel_and_confirmation_have_scoped_actions() { click(&mut manager, import); assert!(matches!( manager.form.as_ref().unwrap().fields[import].value, - Value::Toggle(false) + Value::Toggle(true) )); - click(&mut manager, host); - assert_eq!(manager.form.as_ref().unwrap().focused, host); + click(&mut manager, profile); + assert_eq!(manager.form.as_ref().unwrap().focused, profile); let cancel = manager.form.as_ref().unwrap().fields.len() + 1; click(&mut manager, cancel); assert!(manager.form.is_none()); @@ -169,7 +167,7 @@ fn loading_failure_all_pages_and_confirmation_are_rendered_without_fake_configur manager.not_sent(request.token); view(&manager); let request = manager.query(false); - manager.complete(request.token, Ok(snapshot)); + manager.complete(request.token, Ok(configuration(snapshot))); let pin = peer_identity(); add_peer(&mut manager, pin); manager.draft.as_mut().unwrap().trusted_origins.push(pin); @@ -179,6 +177,7 @@ fn loading_failure_all_pages_and_confirmation_are_rendered_without_fake_configur Page::Peer(pin.hub()), Page::Origins, Page::Exports(pin.hub()), + Page::Consumers, ] { manager.page(page); view(&manager); diff --git a/clients/tui/hub/manager/unit_tests/rendering.rs b/clients/tui/hub/manager/unit_tests/rendering.rs index d9d423a6..2c834c88 100644 --- a/clients/tui/hub/manager/unit_tests/rendering.rs +++ b/clients/tui/hub/manager/unit_tests/rendering.rs @@ -44,7 +44,7 @@ fn wrapped_fields_scroll_to_the_edit_cursor_and_pointer_events_are_generation_fe let mut manager = manager(); manager.page(Page::Peers); activate(&mut manager, Action::AddPeer); - field(&mut manager, FieldId::Host, &"x".repeat(100)); + field(&mut manager, FieldId::Ssh, &"x".repeat(100)); let mut terminal = Terminal::new(TestBackend::new(40, 16)).unwrap(); let mut view = None; terminal @@ -95,11 +95,10 @@ fn pointer_can_navigate_menu_and_confirmation_without_touching_terminal_processe } #[test] -fn dynamic_listener_shows_actual_port_separately_from_the_configured_bind_address() { +fn the_header_reports_the_derived_peer_socket() { let mut manager = manager(); let base = manager.base.as_mut().unwrap(); - base.state.configuration.listen = Some("127.0.0.1:0".parse().unwrap()); - base.listen_address = Some("127.0.0.1:2345".parse().unwrap()); + base.peer_socket = Some("/run/user/1000/yolo/hub/peer.sock".into()); manager.draft = Some(base.state.configuration.clone()); let mut terminal = Terminal::new(TestBackend::new(80, 24)).unwrap(); terminal @@ -114,6 +113,5 @@ fn dynamic_listener_shows_actual_port_separately_from_the_configured_bind_addres .iter() .map(|cell| cell.symbol()) .collect::(); - assert!(text.contains("Listening: 127.0.0.1:2345")); - assert!(text.contains("Listener: 127.0.0.1:0")); + assert!(text.contains("Peer socket: /run/user/1000/yolo/hub/peer.sock")); } diff --git a/clients/tui/hub/manager/unit_tests/ssh.rs b/clients/tui/hub/manager/unit_tests/ssh.rs index e07e293e..d7153874 100644 --- a/clients/tui/hub/manager/unit_tests/ssh.rs +++ b/clients/tui/hub/manager/unit_tests/ssh.rs @@ -3,13 +3,12 @@ use crate::{ fields::{FieldId, Value}, form::Form, }; -use yolo_hub_federation_settings::{PeerAddress, SshProfileRef}; #[path = "../test_support/ssh_constants.rs"] mod constants; use constants::*; #[test] -fn editing_imports_keeps_ssh_reference_and_rejects_endpoint_tampering() { +fn editing_imports_preserves_the_ssh_socket_dial_until_explicitly_changed() { let mut manager = manager(); let pin = peer_identity(); add_peer(&mut manager, pin); @@ -20,23 +19,10 @@ fn editing_imports_keeps_ssh_reference_and_rejects_endpoint_tampering() { .find(|peer| peer.identity == pin) .unwrap(); peer.dial = Some( - PeerAddress::ssh( - SshProfileRef::new(PROFILE_ID, DIGEST_CHARACTER.repeat(DIGEST_BYTES)).unwrap(), - ENDPOINT.parse().unwrap(), - ) - .unwrap(), + yolo_hub_federation_settings::PeerAddress::ssh(PROFILE, SOCKET.into()).unwrap(), ); let mut form = Form::peer(Some(peer)); let original = peer.dial.clone(); - for field in [FieldId::Host, FieldId::Port] { - assert!( - form.fields - .iter() - .find(|entry| entry.id == field) - .unwrap() - .read_only - ); - } form.fields .iter_mut() .find(|field| field.id == FieldId::Import) @@ -48,10 +34,29 @@ fn editing_imports_keeps_ssh_reference_and_rejects_endpoint_tampering() { assert!(!changed.peer(pin.hub()).unwrap().consume); form.fields .iter_mut() - .find(|field| field.id == FieldId::Host) + .find(|field| field.id == FieldId::Ssh) .unwrap() .value = Value::Text { - text: OTHER_HOST.into(), + text: CHANGED_PROFILE.into(), + cursor: Default::default(), + }; + let changed = form.commit(&draft, local).unwrap(); + assert_eq!( + changed + .peer(pin.hub()) + .unwrap() + .dial + .as_ref() + .unwrap() + .ssh_target(), + Some(CHANGED_PROFILE) + ); + form.fields + .iter_mut() + .find(|field| field.id == FieldId::Socket) + .unwrap() + .value = Value::Text { + text: "relative.sock".into(), cursor: Default::default(), }; assert!(form.commit(&draft, local).is_err()); diff --git a/clients/tui/hub/manager/unit_tests/state.rs b/clients/tui/hub/manager/unit_tests/state.rs index 155568c2..6b2f0681 100644 --- a/clients/tui/hub/manager/unit_tests/state.rs +++ b/clients/tui/hub/manager/unit_tests/state.rs @@ -26,15 +26,16 @@ fn unknown_delivery_only_resolves_against_the_exact_applied_receipt() { }; manager.complete(request.token, Err(ManagementError::OutcomeUnknown)); let check = manager.query(true); - manager.complete(check.token, Ok(original.clone())); + manager.complete(check.token, Ok(configuration(original.clone()))); assert!(manager.dirty() && manager.blocked && manager.unresolved.is_some()); let mut applied = original; + let retained_socket = applied.peer_socket.clone(); applied.state.revision += CONFIGURATION_REVISION_STEP; applied.state.last_change = change.request_id; applied.state.configuration = change.configuration; - applied.listen_address = applied.state.configuration.listen; + applied.peer_socket = retained_socket; let check = manager.query(true); - manager.complete(check.token, Ok(applied)); + manager.complete(check.token, Ok(configuration(applied))); assert!(!manager.dirty() && !manager.blocked && manager.unresolved.is_none()); } @@ -44,13 +45,13 @@ fn stale_callbacks_and_changed_local_key_do_not_replace_current_configuration() let before = manager.base.clone().unwrap(); let stale = manager.query(false); let current = manager.query(false); - manager.complete(stale.token, Ok(snapshot())); + manager.complete(stale.token, Ok(configuration(snapshot()))); assert_eq!(manager.base, Some(before.clone())); assert!(manager.pending()); let mut malicious = before.clone(); let key = [2; yolo_hub_delegation::ED25519_PUBLIC_KEY_BYTES]; malicious.state.identity = yolo_hub_delegation::PeerIdentity::new(manager.entry, key).unwrap(); - manager.complete(current.token, Ok(malicious)); + manager.complete(current.token, Ok(configuration(malicious))); assert_eq!(manager.base, Some(before)); } @@ -72,7 +73,8 @@ fn inactive_persisted_configuration_can_be_explicitly_activated_without_inventin let (mut manager, request) = crate::Manager::open(snapshot.state.identity.hub()); let mut inactive = snapshot; inactive.applied = false; - manager.complete(request.token, Ok(inactive)); + inactive.peer_socket = None; + manager.complete(request.token, Ok(configuration(inactive))); assert!(!manager.dirty()); assert!(manager.can_apply()); assert!(manager.proposal().is_some()); @@ -91,7 +93,7 @@ fn reload_rejects_revision_rollback_and_same_revision_equivocation() { reply.state.revision -= CONFIGURATION_REVISION_STEP; } let request = manager.query(false); - manager.complete(request.token, Ok(reply)); + manager.complete(request.token, Ok(configuration(reply))); assert_eq!(manager.base, before); assert!(manager.blocked); } diff --git a/clients/tui/inventory/integration_tests/render_edges.rs b/clients/tui/inventory/integration_tests/render_edges.rs index fcf6d6cb..62fa452b 100644 --- a/clients/tui/inventory/integration_tests/render_edges.rs +++ b/clients/tui/inventory/integration_tests/render_edges.rs @@ -34,13 +34,13 @@ fn agent_without_session_metadata_and_explicitly_unreachable_hub_remain_visible( .map(|cell| cell.symbol()) .collect::(); assert!(content.contains(PROVIDER_KIND)); - assert!(content.contains(&fixture.hub.hub_id.to_string())); + assert!(content.contains(&fixture.hub.hub_id.to_string()[..8])); inventory - .hubs + .machines .get_mut(&fixture.hub.hub_id) .unwrap() .reachable = false; - inventory.view = View::Hubs; + inventory.view = View::Tree; terminal .draw(|frame| { render( @@ -76,7 +76,7 @@ fn unicode_session_metadata_does_not_resize_or_escape_the_viewport() { let mut inventory = fixture.inventory(); for width in [1, 2, 10, 20, WIDTH] { let mut terminal = Terminal::new(TestBackend::new(width, HEIGHT)).unwrap(); - for view in [View::Terminals, View::Agents] { + for view in [View::Terminals, View::Tree] { inventory.view = view; terminal .draw(|frame| { @@ -104,7 +104,7 @@ fn compact_tabs_and_lists_never_write_outside_the_allocated_inventory_area() { let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).unwrap(); for (width, height) in [(0, 0), (1, 1), (2, 2), (10, 6), (20, 15), (80, 24)] { let area = Rect::new(2, 2, width, height); - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; terminal .draw(|frame| { diff --git a/clients/tui/inventory/integration_tests/render_support.rs b/clients/tui/inventory/integration_tests/render_support.rs index 323a7323..770d6620 100644 --- a/clients/tui/inventory/integration_tests/render_support.rs +++ b/clients/tui/inventory/integration_tests/render_support.rs @@ -22,6 +22,7 @@ pub fn render( filter, &BTreeMap::new(), 0, + None, area, ); } diff --git a/clients/tui/inventory/integration_tests/rendering.rs b/clients/tui/inventory/integration_tests/rendering.rs index 401dcc33..e348328d 100644 --- a/clients/tui/inventory/integration_tests/rendering.rs +++ b/clients/tui/inventory/integration_tests/rendering.rs @@ -76,13 +76,13 @@ fn hub_rows_use_local_hostname_and_a_single_status_glyph() { let terminals = fixture.terminals(); let mut inventory = fixture.inventory(); inventory.set_local_hub(fixture.hub.hub_id, Some("build-host".into())); - inventory.view = View::Hubs; + inventory.view = View::Tree; let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).unwrap(); terminal .draw(|frame| render(frame, &inventory, &terminals, None, "", frame.area())) .unwrap(); let content = contents(&terminal); - assert!(content.contains("Hub build-host"), "{content}"); + assert!(content.contains("This machine build-host"), "{content}"); assert!(content.contains(HUB_REACHABLE_GLYPH), "{content}"); assert!(!content.contains("Reachable"), "{content}"); assert!( @@ -91,7 +91,7 @@ fn hub_rows_use_local_hostname_and_a_single_status_glyph() { ); inventory - .hubs + .machines .get_mut(&fixture.hub.hub_id) .unwrap() .reachable = false; @@ -125,6 +125,7 @@ fn agent_control_and_spinner_occupy_fixed_icon_slots() { "", &writable, 0, + None, frame.area(), ); }) @@ -147,6 +148,7 @@ fn agent_control_and_spinner_occupy_fixed_icon_slots() { "", &writable, 1, + None, frame.area(), ); }) @@ -167,6 +169,7 @@ fn agent_control_and_spinner_occupy_fixed_icon_slots() { "", &read_only, 0, + None, frame.area(), ); }) diff --git a/clients/tui/inventory/integration_tests/rendering_control_states.rs b/clients/tui/inventory/integration_tests/rendering_control_states.rs index 1b312e77..d30eef5b 100644 --- a/clients/tui/inventory/integration_tests/rendering_control_states.rs +++ b/clients/tui/inventory/integration_tests/rendering_control_states.rs @@ -21,21 +21,26 @@ fn unsupported_empty_stale_and_provider_specific_states_are_distinct() { let terminals = fixture.terminals(); let mut inventory = Inventory::default(); let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).unwrap(); - for (view, text) in [ - (View::Agents, "Agent inventory unavailable"), - (View::Hubs, "Hub inventory unavailable"), - ] { + for (view, text) in [(View::Tree, "Machine inventory unavailable")] { inventory.view = view; terminal - .draw(|frame| render(frame, &inventory, &terminals, None, "", frame.area())) + .draw(|frame| { + render( + frame, + &inventory, + &std::collections::BTreeMap::new(), + None, + "", + frame.area(), + ) + }) .unwrap(); assert!(contents(&terminal).contains(text)); } inventory.replace(Some(Vec::new()), Some(Vec::new())); for (view, text) in [ (View::Terminals, "No terminals"), - (View::Agents, "No agent runs"), - (View::Hubs, "No hubs"), + (View::Tree, "No machines"), ] { inventory.view = view; terminal diff --git a/clients/tui/inventory/integration_tests/sidebar_density.rs b/clients/tui/inventory/integration_tests/sidebar_density.rs index a800243d..9f0b1f9f 100644 --- a/clients/tui/inventory/integration_tests/sidebar_density.rs +++ b/clients/tui/inventory/integration_tests/sidebar_density.rs @@ -44,8 +44,7 @@ fn five_agents_fit_without_scrolling_in_seventeen_rows() { VISIBLE_AGENT_COUNT ); assert!(!content.contains(AGENT_WORKING_DIRECTORY)); - assert!(content.contains("Agent runs")); - assert!(content.contains("Hubs")); + assert!(content.contains("Machine ")); assert!(!content.contains("Terminals")); } diff --git a/clients/tui/inventory/integration_tests/stale_selection.rs b/clients/tui/inventory/integration_tests/stale_selection.rs index 709084a2..578a65c0 100644 --- a/clients/tui/inventory/integration_tests/stale_selection.rs +++ b/clients/tui/inventory/integration_tests/stale_selection.rs @@ -15,7 +15,7 @@ fn lost_entry_keeps_observation_data_but_cannot_open_its_cached_agent() { inventory.current = false; assert!(inventory.terminal_for_selected(&terminals).is_none()); let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).unwrap(); - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; terminal .draw(|frame| render(frame, &inventory, &terminals, None, "", frame.area())) @@ -29,7 +29,7 @@ fn lost_entry_keeps_observation_data_but_cannot_open_its_cached_agent() { .collect::(); assert!(content.contains("Stale")); match view { - View::Agents => { + View::Tree => { // The compact row marks stale evidence without repeating healthy // metadata; details retain the full observation. assert!(content.contains(HEALTH_WARNING_GLYPH)); @@ -37,12 +37,12 @@ fn lost_entry_keeps_observation_data_but_cannot_open_its_cached_agent() { assert!(!content.contains("Working")); assert!(!content.contains(AGENT_WORKING_DIRECTORY)); } - View::Hubs => assert!(content.contains(HUB_UNKNOWN_GLYPH)), + View::Tree => assert!(content.contains(HUB_UNKNOWN_GLYPH)), View::Terminals => {} } assert!(!content.contains("Unreachable")); } - inventory.view = View::Agents; + inventory.view = View::Tree; inventory.details = Some(fixture.agent.cursor().target()); terminal .draw(|frame| { @@ -71,8 +71,8 @@ fn lost_entry_keeps_observation_data_but_cannot_open_its_cached_agent() { assert!(details.contains(expected), "missing {expected}: {details}"); } assert_eq!(inventory.agents, before); - assert!(inventory.hubs[&fixture.hub.hub_id].reachable); - assert_eq!(inventory.visible_hubs("unknown"), vec![fixture.hub.hub_id]); + assert!(inventory.machines[&fixture.hub.hub_id].reachable); + assert_eq!(inventory.visible_machines("unknown"), vec![fixture.hub.hub_id]); } #[test] @@ -95,7 +95,7 @@ fn refreshed_catalogue_cannot_retarget_agent_selection_to_a_reused_terminal() { } #[test] -fn scope_only_limits_resources_and_does_not_hide_other_hub_choices() { +fn scope_no_longer_hides_other_hub_resources() { let first = Fixture::new(); let second = Fixture::new(); let mut terminals = first.terminals(); @@ -106,11 +106,11 @@ fn scope_only_limits_resources_and_does_not_hide_other_hub_choices() { Some(vec![first.hub.clone(), second.hub.clone()]), ); inventory.scope = Some(first.hub.hub_id); - inventory.view = View::Hubs; + inventory.view = View::Tree; inventory.reconcile("", &terminals); - assert_eq!(inventory.visible_hubs("").len(), 2); - assert_eq!(inventory.visible_agents("", &terminals).len(), 1); - assert_eq!(inventory.visible_terminals("", &terminals).len(), 1); + assert_eq!(inventory.visible_machines("").len(), 2); + assert_eq!(inventory.visible_agents("", &terminals).len(), 2); + assert_eq!(inventory.visible_terminals("", &terminals).len(), 2); let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).unwrap(); terminal .draw(|frame| render(frame, &inventory, &terminals, None, "", frame.area())) diff --git a/clients/tui/inventory/integration_tests/working_directory.rs b/clients/tui/inventory/integration_tests/working_directory.rs index 12593b17..5d707a0d 100644 --- a/clients/tui/inventory/integration_tests/working_directory.rs +++ b/clients/tui/inventory/integration_tests/working_directory.rs @@ -158,7 +158,7 @@ fn literal_directory_does_not_execute_escapes_or_write_outside_inventory() { fn narrow_selected_rows_in_every_inventory_view_never_overwrite_adjacent_content() { let fixture = Fixture::new(); let mut inventory = fixture.inventory(); - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; for width in [0, 1, 2] { let mut terminal = Terminal::new(TestBackend::new(WIDTH, HEIGHT)).unwrap(); diff --git a/clients/tui/inventory/src/constants.rs b/clients/tui/inventory/src/constants.rs index 5ba6d7fe..5eba22b3 100644 --- a/clients/tui/inventory/src/constants.rs +++ b/clients/tui/inventory/src/constants.rs @@ -1,20 +1,20 @@ use ratatui::style::Color; -pub const SECTION_LABELS: [&str; 2] = [yolo_tui_lifecycle::AGENT_RUNS_LABEL, "Hubs"]; -pub const COMPACT_SECTION_LABELS: [&str; 2] = ["A", "H"]; +pub const SECTION_LABELS: [&str; 2] = [yolo_tui_lifecycle::AGENT_RUNS_LABEL, "Machines"]; +pub const COMPACT_SECTION_LABELS: [&str; 2] = ["A", "M"]; pub const AGENTS_SECTION: usize = 0; -pub const HUBS_SECTION: usize = 1; +pub const MACHINES_SECTION: usize = 1; pub const TERMINAL_PICKER_LABEL: &str = "Terminals"; pub const SECTION_ROWS: u16 = 1; pub const CONTEXT_ROWS: u16 = 1; const SECTION_PADDING_SIDES: usize = 2; pub const FULL_SECTIONS_WIDTH: u16 = (SECTION_LABELS[AGENTS_SECTION].len() - + SECTION_LABELS[HUBS_SECTION].len() + + SECTION_LABELS[MACHINES_SECTION].len() + SECTION_LABELS.len() * SECTION_PADDING.len() * SECTION_PADDING_SIDES + (SECTION_LABELS.len() - 1) * SECTION_DIVIDER.len()) as u16; pub const COMPACT_SECTIONS_WIDTH: u16 = (COMPACT_SECTION_LABELS[AGENTS_SECTION].len() - + COMPACT_SECTION_LABELS[HUBS_SECTION].len() + + COMPACT_SECTION_LABELS[MACHINES_SECTION].len() + COMPACT_SECTION_LABELS.len() * SECTION_PADDING.len() * SECTION_PADDING_SIDES + (COMPACT_SECTION_LABELS.len() - 1) * SECTION_DIVIDER.len()) as u16; @@ -25,7 +25,7 @@ pub const SELECTION_MARKER: &str = "> "; pub const SECTION_DIVIDER: &str = "|"; pub const SECTION_PADDING: &str = " "; pub const SELECTION_STEP: usize = 1; -pub const ALL_HUBS: &str = "All Hubs"; +pub const ALL_MACHINES: &str = "All machines"; pub const ACTIVE_SCOPE: &str = "Active · h history"; pub const HISTORY_SCOPE: &str = "History · h active"; pub const AGENT_DETAILS_TITLE: &str = "Agent run details"; @@ -39,7 +39,8 @@ pub const DETAILS_RECOVERY_HINT: &str = "R view terminal recovery · Esc back"; pub const CONTROL_UNKNOWN_LABEL: &str = "Control status unknown"; pub const OBSERVE_ONLY_CONTROL_LABEL: &str = "Observe only · no input control"; pub const DETAILS_TERMINAL_LABEL: &str = "Terminal"; -pub const HUB_LABEL: &str = "Hub"; +pub const MACHINE_LABEL: &str = "Machine"; +pub const THIS_MACHINE_LABEL: &str = "This machine"; pub const TERMINAL_LABEL: &str = "Term"; pub(crate) const PHASE_SEPARATOR: &str = " | "; pub const HUB_REACHABLE_GLYPH: &str = "●"; @@ -63,10 +64,10 @@ pub const STALE_LABEL: &str = "Stale"; pub const REACHABLE_LABEL: &str = "Reachable"; pub const UNREACHABLE_LABEL: &str = "Unreachable"; pub const AGENTS_UNSUPPORTED: &str = "Agent inventory unavailable"; -pub const HUBS_UNSUPPORTED: &str = "Hub inventory unavailable"; +pub const MACHINES_UNSUPPORTED: &str = "Machine inventory unavailable"; pub const EMPTY_SESSIONS: &str = "No terminals"; pub const EMPTY_AGENTS: &str = "No agent runs"; -pub const EMPTY_HUBS: &str = "No hubs"; +pub const EMPTY_MACHINES: &str = "No machines"; pub const UNKNOWN_LABEL: &str = "Unknown"; pub const STARTING_LABEL: &str = "Starting"; pub const RUNNING_LABEL: &str = "Running"; diff --git a/clients/tui/inventory/src/filter.rs b/clients/tui/inventory/src/filter.rs index 3f67c92e..f2486c68 100644 --- a/clients/tui/inventory/src/filter.rs +++ b/clients/tui/inventory/src/filter.rs @@ -17,8 +17,7 @@ impl Inventory { .filter(|(address, session)| { let fields = session.fields(); let phase = self.phase(**address, fields); - self.in_scope(address.hub_id) - && self + self .workspace .is_none_or(|workspace| fields.workspace == Some(workspace)) && (self.history || !terminal_is_dead(phase)) @@ -50,9 +49,6 @@ impl Inventory { self.agents .iter() .filter(|(target, snapshot)| { - if !self.in_scope(target.address.hub_id) { - return false; - } let fields = snapshot.fields(); let session = terminals .get(&fields.terminal.address) @@ -93,13 +89,13 @@ impl Inventory { .collect() } - /// Hubs is the scope selector itself, so the current scope never hides other hubs. - pub fn visible_hubs(&self, filter: &str) -> Vec { - if !self.hubs_supported { + /// Machines is the scope selector itself, so the current scope never hides other machines. + pub fn visible_machines(&self, filter: &str) -> Vec { + if !self.machines_supported { return Vec::new(); } let filter = normalized(filter); - self.hubs + self.machines .iter() .filter(|(hub, summary)| { matches( diff --git a/clients/tui/inventory/src/lib.rs b/clients/tui/inventory/src/lib.rs index 170727c9..f8a3b602 100644 --- a/clients/tui/inventory/src/lib.rs +++ b/clients/tui/inventory/src/lib.rs @@ -6,9 +6,11 @@ mod model; mod pointer_selection; mod render; mod rows; -mod sections; mod selection; mod title; +mod tree; +mod tree_render; +mod tree_selection; mod viewport; mod viewport_evidence; @@ -23,4 +25,5 @@ pub use model::{Inventory, View}; pub use pointer_selection::InventorySelection; pub use render::render_viewport; pub use title::agent_title; +pub use tree::{TreeRow, TreeRowKind, TreeSelection}; pub use viewport::{InventoryHit, InventoryViewport}; diff --git a/clients/tui/inventory/src/model.rs b/clients/tui/inventory/src/model.rs index 38debabe..244151b8 100644 --- a/clients/tui/inventory/src/model.rs +++ b/clients/tui/inventory/src/model.rs @@ -8,10 +8,9 @@ use yolo_tui_lifecycle::TerminalPhase; #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] pub enum View { - Terminals, #[default] - Agents, - Hubs, + Tree, + Terminals, } #[derive(Debug, Default)] @@ -23,13 +22,20 @@ pub struct Inventory { pub history: bool, pub details: Option, pub agents: BTreeMap, - pub hubs: BTreeMap, + pub machines: BTreeMap, pub selected_agent: Option, - pub selected_hub: Option, + pub selected_machine: Option, + pub tree_selected: Option, + pub collapsed: BTreeSet, + /// Hubs that own a client-local tab workspace even without a catalogue row. + pub workspace_machines: BTreeSet, + /// Last terminal opened per machine; a machine click returns to it when it + /// still has a live catalogue record. + pub last_terminal_by_machine: BTreeMap, pub local_hub: Option, pub local_hub_name: Option, pub agents_supported: bool, - pub hubs_supported: bool, + pub machines_supported: bool, pub current: bool, pub attention: BTreeSet, /// Optional workspace scope for terminals and agents; None shows all. @@ -50,13 +56,13 @@ impl Inventory { /// instead retain these maps and set current=false; selection remains caller-owned. pub fn replace(&mut self, agents: Option>, hubs: Option>) { self.agents_supported = agents.is_some(); - self.hubs_supported = hubs.is_some(); + self.machines_supported = hubs.is_some(); self.agents = agents .unwrap_or_default() .into_iter() .map(|agent| (agent.cursor().target(), agent)) .collect(); - self.hubs = hubs + self.machines = hubs .unwrap_or_default() .into_iter() .map(|hub| (hub.hub_id, hub)) @@ -69,13 +75,17 @@ impl Inventory { } } - pub(crate) fn in_scope(&self, hub: HubId) -> bool { - self.scope.is_none_or(|scope| scope == hub) - } - /// Name the Hub reached through a local Unix endpoint. Remote/federated - /// Hubs keep the compact identity fallback because the wire summary does + /// Machines keep the compact identity fallback because the wire summary does /// not carry a host name. + pub fn remember_terminal(&mut self, address: TerminalAddress) { + self.last_terminal_by_machine.insert(address.hub_id, address); + } + + pub fn last_terminal_for(&self, hub: HubId) -> Option { + self.last_terminal_by_machine.get(&hub).copied() + } + pub fn set_local_hub(&mut self, hub: HubId, name: Option) { self.local_hub = Some(hub); self.local_hub_name = name.filter(|name| !name.is_empty()); diff --git a/clients/tui/inventory/src/pointer_selection.rs b/clients/tui/inventory/src/pointer_selection.rs index f70d9bb3..72f660c2 100644 --- a/clients/tui/inventory/src/pointer_selection.rs +++ b/clients/tui/inventory/src/pointer_selection.rs @@ -1,4 +1,4 @@ -use crate::{Inventory, View}; +use crate::{Inventory, TreeSelection, View}; use std::collections::BTreeMap; use yolo_hub_identity::{AgentTarget, HubId, TerminalAddress}; use yolo_hub_terminal_protocol::TerminalSummary; @@ -7,7 +7,7 @@ use yolo_hub_terminal_protocol::TerminalSummary; pub enum InventorySelection { Terminal(TerminalAddress), Agent(AgentTarget), - Hub(HubId), + Machine(HubId), } impl Inventory { @@ -22,24 +22,17 @@ impl Inventory { .into_iter() .map(InventorySelection::Terminal) .collect(), - View::Agents => self - .visible_agents(filter, terminals) - .into_iter() - .map(InventorySelection::Agent) - .collect(), - View::Hubs => self - .visible_hubs(filter) - .into_iter() - .map(InventorySelection::Hub) - .collect(), + View::Tree => self.tree_selections(filter, terminals), } } pub fn selected(&self, session: Option) -> Option { match self.view { View::Terminals => session.map(InventorySelection::Terminal), - View::Agents => self.selected_agent.map(InventorySelection::Agent), - View::Hubs => self.selected_hub.map(InventorySelection::Hub), + View::Tree => self.tree_selected.map(|selection| match selection { + TreeSelection::Machine(hub) => InventorySelection::Machine(hub), + TreeSelection::Agent(target) => InventorySelection::Agent(target), + }), } } @@ -55,13 +48,17 @@ impl Inventory { return false; } match (self.view, value) { - (View::Terminals, InventorySelection::Terminal(address)) => *session = Some(address), - (View::Agents, InventorySelection::Agent(address)) => { - self.selected_agent = Some(address) + (View::Terminals, InventorySelection::Terminal(address)) => { + *session = Some(address); + true + } + (View::Tree, InventorySelection::Agent(target)) => { + self.select_tree(TreeSelection::Agent(target), filter, terminals) + } + (View::Tree, InventorySelection::Machine(hub)) => { + self.select_tree(TreeSelection::Machine(hub), filter, terminals) } - (View::Hubs, InventorySelection::Hub(hub)) => self.selected_hub = Some(hub), _ => return false, } - true } } diff --git a/clients/tui/inventory/src/render.rs b/clients/tui/inventory/src/render.rs index 1ca88885..b3b165d3 100644 --- a/clients/tui/inventory/src/render.rs +++ b/clients/tui/inventory/src/render.rs @@ -1,4 +1,4 @@ -use crate::{Inventory, InventoryViewport, View, constants::*, rows, sections}; +use crate::{Inventory, InventoryViewport, TreeSelection, View, constants::*, rows, tree_render}; use ratatui::{ Frame, layout::{Constraint, Layout, Rect}, @@ -7,7 +7,7 @@ use ratatui::{ widgets::{List, ListState, Paragraph}, }; use std::collections::BTreeMap; -use yolo_hub_identity::TerminalAddress; +use yolo_hub_identity::{HubId, TerminalAddress}; use yolo_hub_terminal_protocol::TerminalSummary; use crate::labels::ControlBadge; @@ -20,30 +20,50 @@ pub fn render_viewport( filter: &str, control_status: &BTreeMap, animation_phase: u8, + active_machine: Option, area: Rect, ) -> InventoryViewport { - let bands = Layout::vertical([ - Constraint::Length(SECTION_ROWS), - Constraint::Length(CONTEXT_ROWS), - Constraint::Min(0), - ]) - .split(area); - let tabs = sections::render(frame, inventory.view, bands[0]); + let bands = + Layout::vertical([Constraint::Length(CONTEXT_ROWS), Constraint::Min(0)]).split(area); + let tree = (inventory.view == View::Tree).then(|| inventory.tree_rows(filter, terminals)); + let show_setup = tree + .as_ref() + .is_some_and(|rows| !rows.is_empty() && rows.iter().all(|row| row.count == 0)) + && inventory.current + && inventory.agents_supported + && !inventory.history + && filter.trim().is_empty() + && inventory.machines.values().any(|machine| machine.reachable) + && bands[1].height >= 3; + let rows_area = Rect { + height: bands[1].height - if show_setup { 2 } else { 0 }, + ..bands[1] + }; + if show_setup { + empty( + frame, + "No managed agents\nyolo hook status", + Rect::new(rows_area.x, rows_area.bottom(), rows_area.width, 2), + ); + } let mut viewport = InventoryViewport::new( area, - bands[2], - tabs, + rows_area, inventory, terminals, session_selected, filter, ); - let scope = inventory - .scope - .filter(|_| inventory.view != View::Hubs) - .map_or_else(|| ALL_HUBS.to_owned(), |hub| format!("{HUB_LABEL} {hub}")); + let machine = active_machine.map_or_else( + || ALL_MACHINES.to_owned(), + |hub| inventory.machine_label(hub), + ); + let machine = match inventory.view { + View::Terminals => format!("{TERMINAL_PICKER_LABEL} | {machine}"), + View::Tree => machine, + }; let context = format!( - "{} | {scope} | {}", + "{} | {machine} | {}", if inventory.current { LIVE_LABEL } else { @@ -61,10 +81,10 @@ pub fn render_viewport( } else { WARNING_COLOR })), - bands[1], + bands[0], ); - let (items, selected, empty) = match inventory.view { - View::Terminals => { + let (items, selected, empty) = match tree { + None => { let visible = inventory.visible_terminals(filter, terminals); let selected = session_selected .and_then(|selected| visible.iter().position(|address| *address == selected)); @@ -76,68 +96,53 @@ pub fn render_viewport( session, inventory.phase(address, session.fields()), inventory.attention.contains(&session.fields().address), - bands[2].width, + bands[1].width, ) }) .collect::>(); (items, selected, EMPTY_SESSIONS) } - View::Agents => { - if !inventory.agents_supported { - empty(frame, AGENTS_UNSUPPORTED, bands[2]); + Some(rows) => { + if rows.is_empty() { + let label = if inventory.machines_supported { + EMPTY_MACHINES + } else { + MACHINES_UNSUPPORTED + }; + empty(frame, label, rows_area); return viewport; } - let visible = inventory.visible_agents(filter, terminals); - let selected = inventory - .selected_agent - .and_then(|selected| visible.iter().position(|address| *address == selected)); - let items = visible - .iter() - .filter_map(|target| inventory.agents.get(target)) - .map(|agent| { - rows::agent( - agent, - terminals, - inventory.current, - inventory - .attention - .contains(&agent.fields().terminal.address), - control_status - .get(&agent.fields().terminal.address) - .copied(), - animation_phase, - bands[2].width, + let selected = inventory.tree_selected.and_then(|selected| { + rows.iter().position(|row| { + matches!( + (row.kind, selected), + (crate::TreeRowKind::Machine(hub), TreeSelection::Machine(machine)) if hub == machine + ) || matches!( + (row.kind, selected), + (crate::TreeRowKind::Agent(target), TreeSelection::Agent(agent)) if target == agent ) }) - .collect(); - (items, selected, EMPTY_AGENTS) - } - View::Hubs => { - if !inventory.hubs_supported { - empty(frame, HUBS_UNSUPPORTED, bands[2]); - return viewport; - } - let visible = inventory.visible_hubs(filter); - let selected = inventory - .selected_hub - .and_then(|selected| visible.iter().position(|hub| *hub == selected)); - let items = visible - .iter() - .filter_map(|hub| inventory.hubs.get(hub)) - .map(|hub| { - rows::hub( - hub, + }); + let items = rows + .into_iter() + .filter_map(|row| { + tree_render::item( + inventory, + row, + terminals, inventory.current, - inventory.local_hub_name(hub.hub_id), - bands[2].width, + control_status, + animation_phase, + bands[1].width, + active_machine, ) }) - .collect(); - (items, selected, EMPTY_HUBS) + .collect::>(); + (items, selected, EMPTY_MACHINES) } }; if items.is_empty() { - self::empty(frame, empty, bands[2]); + self::empty(frame, empty, rows_area); return viewport; } let mut selection = ListState::default().with_selected(selected); @@ -145,10 +150,10 @@ pub fn render_viewport( let mut list = List::new(items).highlight_style(Style::default().fg(SELECTED_COLOR)); let marker = Line::from(SELECTION_MARKER); // Ratatui sizes the marker independently of the list rectangle. - if marker.width() <= usize::from(bands[2].width) { + if marker.width() <= usize::from(bands[1].width) { list = list.highlight_symbol(marker); } - frame.render_stateful_widget(list, bands[2], &mut selection); + frame.render_stateful_widget(list, rows_area, &mut selection); viewport.bind_rows(selection.offset(), &heights, inventory, terminals); viewport } diff --git a/clients/tui/inventory/src/rows.rs b/clients/tui/inventory/src/rows.rs index 89474d38..a347e290 100644 --- a/clients/tui/inventory/src/rows.rs +++ b/clients/tui/inventory/src/rows.rs @@ -36,7 +36,7 @@ pub(crate) fn terminal( Line::from(title), muted(elide(&fields.cwd, row_width)), muted(format!( - "{HUB_LABEL} {} · {TERMINAL_LABEL} {}", + "{MACHINE_LABEL} {} · {TERMINAL_LABEL} {}", compact_id(&fields.address.hub_id.to_string()), compact_id(&fields.address.terminal_id.to_string()) )), @@ -53,10 +53,14 @@ pub(crate) fn agent( control: Option, animation_phase: u8, width: u16, + depth: u8, ) -> ListItem<'static> { let fields = snapshot.fields(); - let row_width = usize::from(width).saturating_sub(Line::raw(SELECTION_MARKER).width()); - let marker = marker(attention); + let indent = usize::from(depth) * TREE_INDENT_COLUMNS; + let row_width = usize::from(width) + .saturating_sub(Line::raw(SELECTION_MARKER).width()) + .saturating_sub(indent); + let marker = format!("{}{}", " ".repeat(indent), marker(attention)); let warning = !current || fields.freshness == Freshness::Stale || fields.reachability == Reachability::Unreachable; @@ -81,7 +85,7 @@ pub(crate) fn agent( let warning_text = warning.then(|| format!(" {HEALTH_WARNING_GLYPH}")); let badge_text = badge.map(|badge| format!(" {}", badge.glyph())); let activity_text = activity.map(|activity| format!(" {activity}")); - let fixed = Line::raw(marker).width() + let fixed = Line::raw(&marker).width() + warning_text .as_deref() .map_or(0, |text| Line::raw(text).width()) @@ -127,7 +131,7 @@ pub(crate) fn agent( ListItem::new(Line::from(spans)) } -pub(crate) fn hub( +pub(crate) fn machine( hub: &HubSummary, current: bool, local_name: Option<&str>, @@ -144,7 +148,11 @@ pub(crate) fn hub( let name = local_name .or(hub.label.as_deref()) .map_or_else(|| compact_id(&hub.hub_id.to_string()).to_owned(), str::to_owned); - let prefix = format!("{HUB_LABEL} "); + let prefix = if local_name.is_some() { + format!("{THIS_MACHINE_LABEL} ") + } else { + format!("{MACHINE_LABEL} ") + }; let suffix = format!(" {glyph}"); let fixed = Line::raw(&prefix).width() + Line::raw(&suffix).width(); let name = elide(&name, row_width.saturating_sub(fixed)); @@ -154,6 +162,8 @@ pub(crate) fn hub( ])) } +const TREE_INDENT_COLUMNS: usize = 2; + fn muted(value: String) -> Line<'static> { Line::from(value).style(Style::default().fg(MUTED_COLOR)) } diff --git a/clients/tui/inventory/src/sections.rs b/clients/tui/inventory/src/sections.rs deleted file mode 100644 index 42ef33cf..00000000 --- a/clients/tui/inventory/src/sections.rs +++ /dev/null @@ -1,65 +0,0 @@ -use crate::{View, constants::*}; -use ratatui::{ - Frame, - layout::Rect, - style::Style, - text::Line, - widgets::{Paragraph, Tabs}, -}; - -pub(crate) fn render(frame: &mut Frame<'_>, view: View, area: Rect) -> Vec<(Rect, View)> { - let selected = match view { - View::Terminals => { - frame.render_widget(Paragraph::new(TERMINAL_PICKER_LABEL), area); - return Vec::new(); - } - View::Agents => AGENTS_SECTION, - View::Hubs => HUBS_SECTION, - }; - if area.width < COMPACT_SECTIONS_WIDTH { - frame.render_widget( - Paragraph::new(COMPACT_SECTION_LABELS[selected]).style(Style::default().fg(SELECTED_COLOR)), - area, - ); - return (!area.is_empty()) - .then(|| { - ( - Rect::new( - area.x, - area.y, - area.width.min(COMPACT_SECTION_LABELS[selected].len() as u16), - SECTION_ROWS, - ), - view, - ) - }) - .into_iter() - .collect(); - } - let labels = if area.width < FULL_SECTIONS_WIDTH { - COMPACT_SECTION_LABELS - } else { - SECTION_LABELS - }; - frame.render_widget( - Tabs::new(labels.map(Line::from)) - .select(selected) - .divider(SECTION_DIVIDER) - .padding(SECTION_PADDING, SECTION_PADDING) - .highlight_style(Style::default().fg(SELECTED_COLOR)), - area, - ); - let mut x = area.x; - let mut hits = Vec::new(); - for (label, view) in labels.into_iter().zip([View::Agents, View::Hubs]) { - x = x.saturating_add(SECTION_PADDING.len() as u16); - let width = (label.len() as u16).min(area.right().saturating_sub(x)); - if width > 0 && area.height > 0 { - hits.push((Rect::new(x, area.y, width, SECTION_ROWS), view)); - } - x = x - .saturating_add(width) - .saturating_add((SECTION_PADDING.len() + SECTION_DIVIDER.len()) as u16); - } - hits -} diff --git a/clients/tui/inventory/src/selection.rs b/clients/tui/inventory/src/selection.rs index 4ec48850..1c58a5e5 100644 --- a/clients/tui/inventory/src/selection.rs +++ b/clients/tui/inventory/src/selection.rs @@ -1,4 +1,4 @@ -use crate::{Inventory, View}; +use crate::{Inventory, InventorySelection, TreeSelection, View}; use std::collections::BTreeMap; use yolo_hub_agent_protocol::{Freshness, Lifecycle, Reachability}; use yolo_hub_identity::{RuntimeTarget, TerminalAddress}; @@ -12,7 +12,6 @@ impl Inventory { terminals: &BTreeMap, ) { let agents = self.visible_agents(filter, terminals); - let hubs = self.visible_hubs(filter); // A live detail view must not outlive the agent it describes. History // keeps the row (and its recovery path) available explicitly. if self @@ -21,8 +20,17 @@ impl Inventory { { self.details = None; } - reconcile(&mut self.selected_agent, &agents); - reconcile(&mut self.selected_hub, &hubs); + let visible = self.tree_selections(filter, terminals); + let current = self.tree_selected.map(tree_value); + if current.is_none_or(|value| !visible.contains(&value)) { + let preferred = visible + .iter() + .find(|value| matches!(value, InventorySelection::Agent(_))) + .or_else(|| visible.first()) + .copied(); + self.tree_selected = preferred.and_then(tree_selection); + } + self.sync_tree_projections(); } pub fn move_selection( @@ -33,14 +41,7 @@ impl Inventory { ) { match self.view { View::Terminals => {} - View::Agents => { - let visible = self.visible_agents(filter, terminals); - move_selection(&mut self.selected_agent, &visible, forward); - } - View::Hubs => { - let visible = self.visible_hubs(filter); - move_selection(&mut self.selected_hub, &visible, forward); - } + View::Tree => self.move_tree_selection(forward, filter, terminals), } } @@ -52,9 +53,6 @@ impl Inventory { return None; } let target = self.selected_agent?; - if !self.in_scope(target.address.hub_id) { - return None; - } let agent = self.agents.get(&target)?.fields(); if agent.cursor.target() != target || matches!(agent.lifecycle, Lifecycle::Exited { .. }) @@ -86,17 +84,17 @@ fn reconcile(selected: &mut Option, visible: &[T]) { } } -fn move_selection(selected: &mut Option, visible: &[T], forward: bool) { - let Some(index) = - selected.and_then(|selected| visible.iter().position(|item| *item == selected)) - else { - *selected = visible.first().copied(); - return; - }; - let next = if forward { - index.saturating_add(1).min(visible.len().saturating_sub(1)) - } else { - index.saturating_sub(1) - }; - *selected = visible.get(next).copied(); +fn tree_value(selection: TreeSelection) -> InventorySelection { + match selection { + TreeSelection::Machine(hub) => InventorySelection::Machine(hub), + TreeSelection::Agent(target) => InventorySelection::Agent(target), + } +} + +fn tree_selection(value: InventorySelection) -> Option { + match value { + InventorySelection::Machine(hub) => Some(TreeSelection::Machine(hub)), + InventorySelection::Agent(target) => Some(TreeSelection::Agent(target)), + InventorySelection::Terminal(_) => None, + } } diff --git a/clients/tui/inventory/src/tree.rs b/clients/tui/inventory/src/tree.rs new file mode 100644 index 00000000..ff32eac5 --- /dev/null +++ b/clients/tui/inventory/src/tree.rs @@ -0,0 +1,137 @@ +use crate::{Inventory, InventorySelection, constants::*}; +use std::collections::{BTreeMap, BTreeSet}; +use yolo_hub_identity::{AgentTarget, HubId, TerminalAddress}; +use yolo_hub_terminal_protocol::TerminalSummary; + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum TreeSelection { + Machine(HubId), + Agent(AgentTarget), +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub enum TreeRowKind { + Machine(HubId), + Agent(AgentTarget), +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct TreeRow { + pub kind: TreeRowKind, + pub depth: u8, + pub expanded: bool, + pub count: usize, +} + +impl Inventory { + pub fn remember_workspace_machines(&mut self, machines: BTreeSet) { + self.workspace_machines = machines; + } + + pub fn machine_label(&self, hub: HubId) -> String { + if let Some(name) = self.local_hub_name(hub) { + return format!("{THIS_MACHINE_LABEL} {name}"); + } + let label = self + .machines + .get(&hub) + .and_then(|summary| summary.label.as_deref()) + .map_or_else(|| compact_id(&hub.to_string()).to_owned(), str::to_owned); + format!("{MACHINE_LABEL} {label}") + } + + pub fn tree_rows( + &self, + filter: &str, + terminals: &BTreeMap, + ) -> Vec { + let matching = self.visible_agents(filter, terminals); + let all = if normalized(filter).is_empty() { + matching.clone() + } else { + self.visible_agents("", terminals) + }; + let mut hubs = self.machines.keys().copied().collect::>(); + hubs.extend(self.agents.keys().map(|target| target.address.hub_id)); + hubs.extend(terminals.keys().map(|address| address.hub_id)); + hubs.extend(self.workspace_machines.iter().copied()); + if let Some(local) = self.local_hub { + hubs.insert(local); + } + let mut ordered = hubs.into_iter().collect::>(); + ordered.sort_by_key(|hub| { + ( + Some(*hub) != self.local_hub, + self.machine_label(*hub).to_lowercase(), + *hub, + ) + }); + let filtered = !normalized(filter).is_empty(); + let mut rows = Vec::new(); + for hub in ordered { + let matches_self = machine_matches(self, hub, filter); + let matching = matching + .iter() + .filter(|target| target.address.hub_id == hub) + .copied() + .collect::>(); + let children = if matches_self && filtered { + all.iter() + .filter(|target| target.address.hub_id == hub) + .copied() + .collect::>() + } else { + matching + }; + if filtered && !matches_self && children.is_empty() { + continue; + } + let expanded = !children.is_empty() + && (filtered || !self.collapsed.contains(&hub)); + rows.push(TreeRow { + kind: TreeRowKind::Machine(hub), + depth: 0, + expanded, + count: children.len(), + }); + if expanded { + rows.extend(children.into_iter().map(|target| TreeRow { + kind: TreeRowKind::Agent(target), + depth: 1, + expanded: false, + count: 0, + })); + } + } + rows + } + + pub fn tree_selections( + &self, + filter: &str, + terminals: &BTreeMap, + ) -> Vec { + self.tree_rows(filter, terminals) + .into_iter() + .map(|row| match row.kind { + TreeRowKind::Machine(hub) => InventorySelection::Machine(hub), + TreeRowKind::Agent(target) => InventorySelection::Agent(target), + }) + .collect() + } +} + +fn machine_matches(inventory: &Inventory, hub: HubId, filter: &str) -> bool { + let filter = normalized(filter); + filter.is_empty() + || inventory.machine_label(hub).to_lowercase().contains(&filter) + || hub.to_string().contains(&filter) +} + +fn normalized(filter: &str) -> String { + filter.trim().to_lowercase() +} + +fn compact_id(value: &str) -> &str { + value.get(..8).unwrap_or(value) +} diff --git a/clients/tui/inventory/src/tree_render.rs b/clients/tui/inventory/src/tree_render.rs new file mode 100644 index 00000000..17ac1b20 --- /dev/null +++ b/clients/tui/inventory/src/tree_render.rs @@ -0,0 +1,112 @@ +use crate::{ + ControlBadge, Inventory, TreeRow, TreeRowKind, + constants::*, + rows, +}; +use ratatui::{ + style::{Modifier, Style}, + text::Line, + widgets::ListItem, +}; +use std::collections::BTreeMap; +use yolo_hub_identity::{HubId, TerminalAddress}; +use yolo_hub_terminal_protocol::TerminalSummary; + +pub(crate) fn item( + inventory: &Inventory, + row: TreeRow, + terminals: &BTreeMap, + current: bool, + control_status: &BTreeMap, + animation_phase: u8, + width: u16, + active_machine: Option, +) -> Option> { + match row.kind { + TreeRowKind::Machine(hub) => Some(machine( + inventory, + hub, + row.expanded, + row.count, + current, + width, + active_machine, + )), + TreeRowKind::Agent(target) => { + let agent = inventory.agents.get(&target)?; + let address = agent.fields().terminal.address; + Some(rows::agent( + agent, + terminals, + current, + inventory.attention.contains(&address), + control_status.get(&address).copied(), + animation_phase, + width, + row.depth, + )) + } + } +} + +fn machine( + inventory: &Inventory, + hub: HubId, + expanded: bool, + count: usize, + current: bool, + width: u16, + active_machine: Option, +) -> ListItem<'static> { + let reachable = inventory + .machines + .get(&hub) + .is_some_and(|summary| summary.reachable); + let (glyph, style) = if !current { + (HUB_UNKNOWN_GLYPH, Style::default().fg(MUTED_COLOR)) + } else if reachable { + (HUB_REACHABLE_GLYPH, Style::default()) + } else { + (HUB_UNREACHABLE_GLYPH, Style::default().fg(WARNING_COLOR)) + }; + let expander = if count == 0 { + " " + } else if expanded { + "▾ " + } else { + "▸ " + }; + let mut text = format!("{expander}{}", inventory.machine_label(hub)); + if count > 0 { + text.push_str(&format!(" ({count})")); + } + text.push_str(&format!(" {glyph}")); + let style = if active_machine == Some(hub) { + Style::default() + .fg(SELECTED_COLOR) + .add_modifier(Modifier::BOLD) + } else { + style + }; + let text = elide(&text, width); + ListItem::new(Line::from(text).style(style)) +} + +fn elide(value: &str, width: u16) -> String { + let width = usize::from(width); + if Line::raw(value).width() <= width { + return value.to_owned(); + } + let mut result = String::new(); + let mut used = 0; + for character in value.chars() { + let size = Line::raw(character.to_string()).width(); + if used + size + 1 > width { + break; + } + result.push(character); + used += size; + } + result.push('…'); + result +} diff --git a/clients/tui/inventory/src/tree_selection.rs b/clients/tui/inventory/src/tree_selection.rs new file mode 100644 index 00000000..8b2f8b65 --- /dev/null +++ b/clients/tui/inventory/src/tree_selection.rs @@ -0,0 +1,84 @@ +use crate::{Inventory, InventorySelection, TreeSelection}; +use std::collections::BTreeMap; +use yolo_hub_identity::{HubId, TerminalAddress}; +use yolo_hub_terminal_protocol::TerminalSummary; + +impl Inventory { + pub fn select_tree( + &mut self, + selection: TreeSelection, + filter: &str, + terminals: &BTreeMap, + ) -> bool { + let value = selection_value(selection); + if !self.tree_selections(filter, terminals).contains(&value) { + return false; + } + self.set_tree_selection(selection); + true + } + + pub fn move_tree_selection( + &mut self, + forward: bool, + filter: &str, + terminals: &BTreeMap, + ) { + let visible = self.tree_selections(filter, terminals); + let current = self + .tree_selected + .map(selection_value) + .and_then(|value| visible.iter().position(|item| *item == value)); + let next = match current { + Some(index) if forward => index.saturating_add(1).min(visible.len() - 1), + Some(index) => index.saturating_sub(1), + None => 0, + }; + if let Some(value) = visible.get(next) { + self.set_tree_selection(match value { + InventorySelection::Machine(hub) => TreeSelection::Machine(*hub), + InventorySelection::Agent(target) => TreeSelection::Agent(*target), + InventorySelection::Terminal(_) => return, + }); + } + } + + pub fn toggle_machine(&mut self, hub: HubId) { + if !self.collapsed.remove(&hub) { + self.collapsed.insert(hub); + } + } + + pub fn selected_tree(&self) -> Option { + self.tree_selected + } + + pub(crate) fn set_tree_selection(&mut self, selection: TreeSelection) { + self.tree_selected = Some(selection); + self.sync_tree_projections(); + } + + pub(crate) fn sync_tree_projections(&mut self) { + match self.tree_selected { + Some(TreeSelection::Machine(hub)) => { + self.selected_machine = Some(hub); + self.selected_agent = None; + } + Some(TreeSelection::Agent(target)) => { + self.selected_agent = Some(target); + self.selected_machine = Some(target.address.hub_id); + } + None => { + self.selected_agent = None; + self.selected_machine = None; + } + } + } +} + +fn selection_value(selection: TreeSelection) -> InventorySelection { + match selection { + TreeSelection::Machine(hub) => InventorySelection::Machine(hub), + TreeSelection::Agent(target) => InventorySelection::Agent(target), + } +} diff --git a/clients/tui/inventory/src/viewport.rs b/clients/tui/inventory/src/viewport.rs index df06a2f4..121bd4e3 100644 --- a/clients/tui/inventory/src/viewport.rs +++ b/clients/tui/inventory/src/viewport.rs @@ -8,7 +8,6 @@ use yolo_hub_terminal_protocol::TerminalSummary; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub enum InventoryHit { - Tab(View), Row(InventorySelection), } @@ -16,7 +15,6 @@ pub enum InventoryHit { pub struct InventoryViewport { area: Rect, rows_area: Rect, - tabs: Vec<(Rect, View)>, rows: Vec<(Rect, InventorySelection, RowEvidence)>, view: View, scope: Option, @@ -33,7 +31,6 @@ impl InventoryViewport { pub(crate) fn new( area: Rect, rows_area: Rect, - tabs: Vec<(Rect, View)>, inventory: &Inventory, terminals: &BTreeMap, selected: Option, @@ -42,14 +39,13 @@ impl InventoryViewport { Self { area, rows_area, - tabs, rows: Vec::new(), view: inventory.view, scope: inventory.scope, history: inventory.history, details: inventory.details, current: inventory.current, - support: (inventory.agents_supported, inventory.hubs_supported), + support: (inventory.agents_supported, inventory.machines_supported), filter: filter.into(), selected: inventory.selected(selected), ordered: inventory.visible_selections(filter, terminals), @@ -75,7 +71,7 @@ impl InventoryViewport { && self.history == inventory.history && self.details == inventory.details && self.current == inventory.current - && self.support == (inventory.agents_supported, inventory.hubs_supported) + && self.support == (inventory.agents_supported, inventory.machines_supported) && self.filter == filter && self.selected == inventory.selected(selected) && self.ordered == inventory.visible_selections(filter, terminals) @@ -90,16 +86,10 @@ impl InventoryViewport { if !self.area.contains(position) { return None; } - self.tabs + self.rows .iter() - .find(|(area, _)| area.contains(position)) - .map(|(_, view)| InventoryHit::Tab(*view)) - .or_else(|| { - self.rows - .iter() - .find(|(area, _, _)| area.contains(position)) - .map(|(_, row, _)| InventoryHit::Row(*row)) - }) + .find(|(area, _, _)| area.contains(position)) + .map(|(_, row, _)| InventoryHit::Row(*row)) } pub fn scroll_target(&self, forward: bool) -> Option { diff --git a/clients/tui/inventory/src/viewport_evidence.rs b/clients/tui/inventory/src/viewport_evidence.rs index 7dcb6608..d1530cf2 100644 --- a/clients/tui/inventory/src/viewport_evidence.rs +++ b/clients/tui/inventory/src/viewport_evidence.rs @@ -1,7 +1,7 @@ use crate::{Inventory, InventorySelection}; use std::collections::BTreeMap; use yolo_hub_agent_protocol::AgentSnapshot; -use yolo_hub_identity::TerminalAddress; +use yolo_hub_identity::{HubId, TerminalAddress}; use yolo_hub_rpc_protocol::HubSummary; use yolo_hub_terminal_protocol::TerminalSummary; @@ -9,7 +9,7 @@ use yolo_hub_terminal_protocol::TerminalSummary; pub(crate) enum RowEvidence { Terminal(TerminalSummary), Agent(AgentSnapshot, Option), - Hub(HubSummary), + Machine(HubId, Option), } impl RowEvidence { pub fn capture( @@ -26,7 +26,9 @@ impl RowEvidence { terminals.get(&agent.fields().terminal.address).cloned(), ) } - InventorySelection::Hub(hub) => Self::Hub(inventory.hubs.get(&hub)?.clone()), + InventorySelection::Machine(hub) => { + Self::Machine(hub, inventory.machines.get(&hub).cloned()) + } }) } @@ -41,7 +43,7 @@ impl RowEvidence { inventory.agents.get(&expected.cursor().target()) == Some(expected) && terminals.get(&expected.fields().terminal.address) == session.as_ref() } - Self::Hub(expected) => inventory.hubs.get(&expected.hub_id) == Some(expected), + Self::Machine(hub, expected) => inventory.machines.get(hub) == expected.as_ref(), } } } diff --git a/clients/tui/inventory/test_support/fixtures.rs b/clients/tui/inventory/test_support/fixtures.rs index acd5806f..8d926c18 100644 --- a/clients/tui/inventory/test_support/fixtures.rs +++ b/clients/tui/inventory/test_support/fixtures.rs @@ -7,7 +7,7 @@ use yolo_hub_rpc_protocol::HubSummary; use yolo_hub_terminal_protocol::{ RuntimeStatus, RuntimeSummary, TerminalSummary, TerminalSummaryFields, }; -use yolo_tui_inventory::{Inventory, View}; +use yolo_tui_inventory::{Inventory, TreeSelection}; pub struct Fixture { pub agent: AgentSnapshot, @@ -95,8 +95,13 @@ impl Fixture { let mut inventory = Inventory::default(); inventory.replace(Some(vec![self.agent.clone()]), Some(vec![self.hub.clone()])); inventory.current = true; - inventory.view = View::Agents; + inventory.reconcile("", &self.terminals()); + inventory.select_tree( + TreeSelection::Agent(self.agent.cursor().target()), + "", + &self.terminals(), + ); inventory } } diff --git a/clients/tui/inventory/test_support/pointer.rs b/clients/tui/inventory/test_support/pointer.rs index 4357920f..c4655728 100644 --- a/clients/tui/inventory/test_support/pointer.rs +++ b/clients/tui/inventory/test_support/pointer.rs @@ -47,6 +47,7 @@ pub fn render( filter, &BTreeMap::new(), 0, + inventory.local_hub, area, )) }) diff --git a/clients/tui/inventory/unit_tests/filtering.rs b/clients/tui/inventory/unit_tests/filtering.rs index dd480987..461dafd3 100644 --- a/clients/tui/inventory/unit_tests/filtering.rs +++ b/clients/tui/inventory/unit_tests/filtering.rs @@ -25,17 +25,14 @@ fn scoped_search_matches_origin_provider_and_associated_session_metadata() { assert_eq!(inventory.visible_terminals(&filter, &terminals).len(), 2); } inventory.scope = Some(first.hub.hub_id); + let agents = inventory.visible_agents("", &terminals); + assert_eq!(agents.len(), 2); + assert!(agents.contains(&first.agent.cursor().target())); + assert!(agents.contains(&second.agent.cursor().target())); + assert_eq!(inventory.visible_terminals("", &terminals).len(), 2); + assert_eq!(inventory.visible_machines("").len(), 2); assert_eq!( - inventory.visible_agents("", &terminals), - vec![first.agent.cursor().target()] - ); - assert_eq!( - inventory.visible_terminals("", &terminals), - vec![first.session.fields().address] - ); - assert_eq!(inventory.visible_hubs("").len(), 2); - assert_eq!( - inventory.visible_hubs(&second.hub.hub_id.to_string()), + inventory.visible_machines(&second.hub.hub_id.to_string()), vec![second.hub.hub_id] ); assert!( @@ -48,7 +45,7 @@ fn scoped_search_matches_origin_provider_and_associated_session_metadata() { .visible_terminals(NONMATCHING_FILTER, &terminals) .is_empty() ); - assert!(inventory.visible_hubs(NONMATCHING_FILTER).is_empty()); + assert!(inventory.visible_machines(NONMATCHING_FILTER).is_empty()); assert_eq!( inventory .visible_agents( @@ -174,7 +171,7 @@ fn absent_associated_session_and_unknown_provider_metadata_are_searchable_by_ori .len(), 1 ); - assert!(inventory.visible_hubs("").is_empty()); + assert!(inventory.visible_machines("").is_empty()); inventory.agents_supported = false; assert!( inventory diff --git a/clients/tui/inventory/unit_tests/lib.rs b/clients/tui/inventory/unit_tests/lib.rs index 7b058699..57631b2c 100644 --- a/clients/tui/inventory/unit_tests/lib.rs +++ b/clients/tui/inventory/unit_tests/lib.rs @@ -14,6 +14,7 @@ mod pointer_support; mod pointer_viewport; mod selection; mod terminal; +mod tree; mod title; mod working_directory; mod workspace_filter; diff --git a/clients/tui/inventory/unit_tests/model.rs b/clients/tui/inventory/unit_tests/model.rs index 3a0a95fa..8972f97a 100644 --- a/clients/tui/inventory/unit_tests/model.rs +++ b/clients/tui/inventory/unit_tests/model.rs @@ -7,15 +7,15 @@ use yolo_tui_inventory::{Inventory, View}; #[test] fn default_is_empty_agent_inventory_without_asserting_current_capabilities() { let inventory = Inventory::default(); - assert_eq!(inventory.view, View::Agents); + assert_eq!(inventory.view, View::Tree); assert_eq!(inventory.scope, None); assert!(inventory.agents.is_empty()); - assert!(inventory.hubs.is_empty()); + assert!(inventory.machines.is_empty()); assert!(!inventory.agents_supported); - assert!(!inventory.hubs_supported); + assert!(!inventory.machines_supported); assert!(!inventory.current); assert_eq!(inventory.selected_agent, None); - assert_eq!(inventory.selected_hub, None); + assert_eq!(inventory.selected_machine, None); } #[test] @@ -24,21 +24,21 @@ fn replacement_does_not_rewrite_view_scope_selection_or_freshness() { let mut inventory = fixture.inventory(); inventory.scope = Some(HubId::new()); let selected_agent = inventory.selected_agent; - let selected_hub = inventory.selected_hub; + let selected_machine = inventory.selected_machine; inventory.current = false; inventory.replace(Some(Vec::new()), Some(Vec::new())); assert!(inventory.agents_supported); - assert!(inventory.hubs_supported); + assert!(inventory.machines_supported); assert!(inventory.agents.is_empty()); - assert!(inventory.hubs.is_empty()); + assert!(inventory.machines.is_empty()); assert_eq!(inventory.selected_agent, selected_agent); - assert_eq!(inventory.selected_hub, selected_hub); - assert_eq!(inventory.view, View::Agents); + assert_eq!(inventory.selected_machine, selected_machine); + assert_eq!(inventory.view, View::Tree); assert!(inventory.scope.is_some()); assert!(!inventory.current); inventory.replace(None, None); assert!(!inventory.agents_supported); - assert!(!inventory.hubs_supported); + assert!(!inventory.machines_supported); assert!( inventory .terminal_for_selected(&fixture.terminals()) @@ -49,7 +49,7 @@ fn replacement_does_not_rewrite_view_scope_selection_or_freshness() { Some(vec![fixture.hub.clone()]), ); assert_eq!(inventory.agents.len(), 1); - assert_eq!(inventory.hubs.len(), 1); + assert_eq!(inventory.machines.len(), 1); } #[test] @@ -63,8 +63,8 @@ fn duplicate_stable_keys_replace_their_previous_observation() { Some(vec![fixture.hub.clone(), hub]), ); assert_eq!(inventory.agents.len(), 1); - assert_eq!(inventory.hubs.len(), 1); - assert!(!inventory.hubs[&fixture.hub.hub_id].reachable); + assert_eq!(inventory.machines.len(), 1); + assert!(!inventory.machines[&fixture.hub.hub_id].reachable); } #[test] diff --git a/clients/tui/inventory/unit_tests/pointer_fences.rs b/clients/tui/inventory/unit_tests/pointer_fences.rs index 2e6a2a8b..9dfd11d0 100644 --- a/clients/tui/inventory/unit_tests/pointer_fences.rs +++ b/clients/tui/inventory/unit_tests/pointer_fences.rs @@ -17,7 +17,7 @@ fn changed_filter_scope_view_selection_and_read_freshness_invalidate_the_old_scr inventory.current = false; assert!(!viewport.current(&inventory, &terminals, selected, "")); inventory.current = true; - inventory.view = View::Agents; + inventory.view = View::Tree; assert!(!viewport.current(&inventory, &terminals, selected, "")); } @@ -26,7 +26,7 @@ fn capability_changes_invalidate_clicks_even_when_the_visible_sessions_are_uncha let (mut inventory, terminals) = catalogue(); let selected = terminals.keys().next().copied(); let agents = inventory.agents.values().cloned().collect::>(); - let hubs = inventory.hubs.values().cloned().collect::>(); + let hubs = inventory.machines.values().cloned().collect::>(); for capabilities in [(None, Some(hubs.clone())), (Some(agents.clone()), None)] { let viewport = render(&inventory, &terminals, selected, "", area()); let visible = inventory.visible_selections("", &terminals); @@ -43,7 +43,7 @@ fn capability_changes_invalidate_clicks_even_when_the_visible_sessions_are_uncha #[test] fn same_visible_address_does_not_authorize_a_click_after_execution_or_runtime_replacement() { let (mut inventory, mut terminals) = catalogue(); - inventory.view = View::Agents; + inventory.view = View::Tree; let selected = *inventory.agents.keys().next().unwrap(); inventory.selected_agent = Some(selected); let viewport = render(&inventory, &terminals, None, "", area()); diff --git a/clients/tui/inventory/unit_tests/pointer_selection.rs b/clients/tui/inventory/unit_tests/pointer_selection.rs index 047a0abb..67f1fc84 100644 --- a/clients/tui/inventory/unit_tests/pointer_selection.rs +++ b/clients/tui/inventory/unit_tests/pointer_selection.rs @@ -5,7 +5,7 @@ use yolo_tui_inventory::{InventorySelection, View}; fn explicit_row_selection_preserves_other_views_and_rejects_wrong_view_or_filter() { let (mut inventory, terminals) = catalogue(); let mut session = None; - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; let visible = inventory.visible_selections("", &terminals); let selected = *visible.last().unwrap(); @@ -30,7 +30,7 @@ fn explicit_row_selection_preserves_other_views_and_rejects_wrong_view_or_filter fn scrolling_moves_one_visible_selection_and_clamps_at_the_ends() { let (mut inventory, terminals) = catalogue(); let mut session = None; - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; let visible = inventory.visible_selections("", &terminals); assert!(inventory.select(visible[0], &mut session, "", &terminals)); diff --git a/clients/tui/inventory/unit_tests/pointer_viewport.rs b/clients/tui/inventory/unit_tests/pointer_viewport.rs index 6d292893..2958bd22 100644 --- a/clients/tui/inventory/unit_tests/pointer_viewport.rs +++ b/clients/tui/inventory/unit_tests/pointer_viewport.rs @@ -1,12 +1,13 @@ use super::{constants::*, pointer_support::*}; use ratatui::layout::Rect; +use std::collections::BTreeMap; use yolo_tui_inventory::{InventoryHit, View}; #[test] fn each_painted_row_maps_to_its_rendered_identity_after_selection_scrolls_the_list() { let (mut inventory, terminals) = catalogue(); let mut session = None; - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; let selected = *inventory.visible_selections("", &terminals).last().unwrap(); assert!(inventory.select(selected, &mut session, "", &terminals)); @@ -25,51 +26,14 @@ fn each_painted_row_maps_to_its_rendered_identity_after_selection_scrolls_the_li .filter_map(|y| viewport.hit(rows.x, y)) .collect::>(); assert!(hits.contains(&InventoryHit::Row(selected))); - assert_ne!( - viewport.hit(rows.x, rows.y), - Some(InventoryHit::Row( - inventory.visible_selections("", &terminals)[0] - )) - ); - assert!(viewport.current(&inventory, &terminals, session, "")); assert_eq!(viewport.hit(rows.right(), rows.y), None); assert_eq!(viewport.hit(rows.x, rows.bottom()), None); + assert!(viewport.current(&inventory, &terminals, session, "")); } } #[test] -fn tab_labels_are_clickable_at_full_compact_and_single_label_widths_without_clicking_dividers() { - let (mut inventory, terminals) = catalogue(); - inventory.view = View::Agents; - for width in [POINTER_TAB_FULL_WIDTH, POINTER_TAB_COMPACT_WIDTH] { - let viewport = render(&inventory, &terminals, None, "", Rect { width, ..area() }); - let tabs = (viewport.area().x..viewport.area().right()) - .filter_map(|x| viewport.hit(x, viewport.area().y)) - .collect::>(); - for view in [View::Agents, View::Hubs] { - assert!(tabs.contains(&InventoryHit::Tab(view))); - } - assert!(!tabs.contains(&InventoryHit::Tab(View::Terminals))); - assert_eq!(viewport.hit(viewport.area().x, viewport.area().y), None); - } - let viewport = render( - &inventory, - &terminals, - None, - "", - Rect { - width: POINTER_NARROW_WIDTH, - ..area() - }, - ); - assert_eq!( - viewport.hit(viewport.area().x, viewport.area().y), - Some(InventoryHit::Tab(View::Agents)) - ); -} - -#[test] -fn explicit_terminal_picker_has_no_sidebar_tab_hit_targets() { +fn the_context_row_and_empty_rows_have_no_hit_targets() { let (inventory, terminals) = catalogue(); let viewport = render(&inventory, &terminals, None, "", area()); assert!( @@ -81,16 +45,17 @@ fn explicit_terminal_picker_has_no_sidebar_tab_hit_targets() { #[test] fn empty_unsupported_and_unpainted_list_tail_have_no_row_hits() { let (mut inventory, terminals) = catalogue(); - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; let viewport = render(&inventory, &terminals, None, NONMATCHING_FILTER, area()); let rows = viewport.rows_area(); assert!((rows.y..rows.bottom()).all(|y| viewport.hit(rows.x, y).is_none())); assert_eq!(viewport.scroll_target(true), None); } + let none = BTreeMap::new(); inventory.replace(None, None); - inventory.view = View::Agents; - let viewport = render(&inventory, &terminals, None, "", area()); + inventory.view = View::Tree; + let viewport = render(&inventory, &none, None, "", area()); assert_eq!( viewport.hit(viewport.rows_area().x, viewport.rows_area().y), None @@ -98,7 +63,7 @@ fn empty_unsupported_and_unpainted_list_tail_have_no_row_hits() { inventory.view = View::Terminals; let viewport = render( &inventory, - &terminals, + &none, None, "", Rect { @@ -113,7 +78,7 @@ fn empty_unsupported_and_unpainted_list_tail_have_no_row_hits() { ), None ); - let empty = render(&inventory, &terminals, None, "", Rect::default()); + let empty = render(&inventory, &none, None, "", Rect::default()); assert_eq!(empty.hit(0, 0), None); } @@ -121,7 +86,7 @@ fn empty_unsupported_and_unpainted_list_tail_have_no_row_hits() { fn collapsing_either_dimension_removes_hit_targets_until_a_real_repaint() { let (mut inventory, terminals) = catalogue(); let mut session = None; - for view in [View::Terminals, View::Agents, View::Hubs] { + for view in [View::Terminals, View::Tree] { inventory.view = view; let selected = inventory.visible_selections("", &terminals)[0]; assert!(inventory.select(selected, &mut session, "", &terminals)); diff --git a/clients/tui/inventory/unit_tests/selection.rs b/clients/tui/inventory/unit_tests/selection.rs index 83f778ba..640fb852 100644 --- a/clients/tui/inventory/unit_tests/selection.rs +++ b/clients/tui/inventory/unit_tests/selection.rs @@ -1,9 +1,8 @@ use crate::{constants::NONMATCHING_FILTER, fixtures::Fixture}; -use yolo_hub_identity::HubId; -use yolo_tui_inventory::View; +use yolo_tui_inventory::{InventorySelection, View}; #[test] -fn agent_and_hub_selections_stay_stable_and_clamp_at_both_ends() { +fn tree_selection_walks_machines_and_agents_and_clamps_at_both_ends() { let first = Fixture::new(); let second = Fixture::new(); let mut terminals = first.terminals(); @@ -13,57 +12,46 @@ fn agent_and_hub_selections_stay_stable_and_clamp_at_both_ends() { Some(vec![second.agent.clone(), first.agent.clone()]), Some(vec![second.hub.clone(), first.hub.clone()]), ); - let agents = inventory.visible_agents("", &terminals); - let hubs = inventory.visible_hubs(""); - inventory.selected_agent = None; + let rows = inventory.tree_selections("", &terminals); + inventory.tree_selected = None; inventory.move_selection(true, "", &terminals); - assert_eq!(inventory.selected_agent, Some(agents[0])); + assert_eq!(inventory.selected(None), Some(rows[0])); inventory.move_selection(false, "", &terminals); - assert_eq!(inventory.selected_agent, Some(agents[0])); + assert_eq!(inventory.selected(None), Some(rows[0])); + for row in rows.iter().skip(1) { + inventory.move_selection(true, "", &terminals); + assert_eq!(inventory.selected(None), Some(*row)); + } + let last = *rows.last().unwrap(); + let InventorySelection::Agent(last_agent) = last else { + panic!() + }; inventory.move_selection(true, "", &terminals); - assert_eq!(inventory.selected_agent, Some(agents[1])); - inventory.move_selection(true, "", &terminals); - assert_eq!(inventory.selected_agent, Some(agents[1])); + assert_eq!(inventory.selected(None), Some(last)); inventory.reconcile("", &terminals); - assert_eq!(inventory.selected_agent, Some(agents[1])); - inventory.view = View::Hubs; - inventory.selected_hub = Some(HubId::new()); - inventory.move_selection(false, "", &terminals); - assert_eq!(inventory.selected_hub, Some(hubs[0])); - inventory.move_selection(true, "", &terminals); - assert_eq!(inventory.selected_hub, Some(hubs[1])); - inventory.move_selection(true, "", &terminals); - assert_eq!(inventory.selected_hub, Some(hubs[1])); - inventory.move_selection(false, "", &terminals); - assert_eq!(inventory.selected_hub, Some(hubs[0])); + assert_eq!(inventory.selected(None), Some(last)); inventory.view = View::Terminals; inventory.move_selection(true, "", &terminals); - assert_eq!(inventory.selected_hub, Some(hubs[0])); - assert_eq!(inventory.selected_agent, Some(agents[1])); + assert_eq!(inventory.selected(None), None); + assert_eq!(inventory.selected_agent, Some(last_agent)); } #[test] -fn filtering_and_scope_reconcile_without_modifying_the_selected_view() { +fn filtering_reconciles_the_tree_cursor_without_modifying_the_view() { let fixture = Fixture::new(); let terminals = fixture.terminals(); let mut inventory = fixture.inventory(); inventory.reconcile(NONMATCHING_FILTER, &terminals); + assert_eq!(inventory.tree_selected, None); assert_eq!(inventory.selected_agent, None); - assert_eq!(inventory.selected_hub, None); + assert_eq!(inventory.selected_machine, None); inventory.move_selection(true, NONMATCHING_FILTER, &terminals); - assert_eq!(inventory.selected_agent, None); - inventory.view = View::Hubs; - inventory.move_selection(false, NONMATCHING_FILTER, &terminals); - assert_eq!(inventory.selected_hub, None); + assert_eq!(inventory.tree_selected, None); inventory.reconcile("", &terminals); + assert_eq!(inventory.selected_machine, Some(fixture.hub.hub_id)); assert_eq!( inventory.selected_agent, Some(fixture.agent.cursor().target()) ); - assert_eq!(inventory.selected_hub, Some(fixture.hub.hub_id)); - inventory.scope = Some(HubId::new()); - inventory.reconcile("", &terminals); - assert_eq!(inventory.selected_agent, None); - assert_eq!(inventory.selected_hub, Some(fixture.hub.hub_id)); - assert_eq!(inventory.view, View::Hubs); + assert_eq!(inventory.view, View::Tree); } diff --git a/clients/tui/inventory/unit_tests/terminal.rs b/clients/tui/inventory/unit_tests/terminal.rs index 8a20f0d9..b484e278 100644 --- a/clients/tui/inventory/unit_tests/terminal.rs +++ b/clients/tui/inventory/unit_tests/terminal.rs @@ -35,8 +35,6 @@ fn terminal_lookup_requires_supported_current_reachable_observation_and_scope() assert_eq!(inventory.terminal_for_selected(&terminals), None); inventory.selected_agent = Some(agent_target); inventory.scope = Some(HubId::new()); - assert_eq!(inventory.terminal_for_selected(&terminals), None); - inventory.scope = Some(address.hub_id); assert_eq!( inventory.terminal_for_selected(&terminals), Some(runtime_target) diff --git a/clients/tui/inventory/unit_tests/tree.rs b/clients/tui/inventory/unit_tests/tree.rs new file mode 100644 index 00000000..a55d7b28 --- /dev/null +++ b/clients/tui/inventory/unit_tests/tree.rs @@ -0,0 +1,74 @@ +use crate::fixtures::Fixture; +use yolo_tui_inventory::{TreeRowKind, TreeSelection}; + +fn labelled(fixture: &Fixture, label: &str) -> yolo_hub_rpc_protocol::HubSummary { + yolo_hub_rpc_protocol::HubSummary { + hub_id: fixture.hub.hub_id, + reachable: true, + label: Some(label.into()), + } +} + +#[test] +fn tree_orders_the_local_machine_first_and_groups_agents_beneath_it() { + let first = Fixture::new(); + let second = Fixture::new(); + let mut terminals = first.terminals(); + terminals.extend(second.terminals()); + let mut inventory = first.inventory(); + inventory.replace( + Some(vec![first.agent.clone(), second.agent.clone()]), + Some(vec![labelled(&first, "alpha"), labelled(&second, "beta")]), + ); + inventory.set_local_hub(second.hub.hub_id, Some("host".into())); + let rows = inventory.tree_rows("", &terminals); + assert!(matches!( + rows[0].kind, + TreeRowKind::Machine(hub) if hub == second.hub.hub_id + )); + assert!(matches!(rows[1].kind, TreeRowKind::Agent(_))); + assert!(matches!( + rows[2].kind, + TreeRowKind::Machine(hub) if hub == first.hub.hub_id + )); + assert!(matches!(rows[3].kind, TreeRowKind::Agent(_))); + assert_eq!(rows[0].count, 1); +} + +#[test] +fn collapsing_hides_children_and_filtering_auto_expands_matches() { + let fixture = Fixture::new(); + let terminals = fixture.terminals(); + let mut inventory = fixture.inventory(); + inventory.replace( + Some(vec![fixture.agent.clone()]), + Some(vec![labelled(&fixture, "alpha")]), + ); + inventory.toggle_machine(fixture.hub.hub_id); + let rows = inventory.tree_rows("", &terminals); + assert_eq!(rows.len(), 1); + assert!(matches!(rows[0].kind, TreeRowKind::Machine(_))); + let rows = inventory.tree_rows("alpha", &terminals); + assert_eq!(rows.len(), 2); + assert!(rows[0].expanded); + let rows = inventory.tree_rows("missing", &terminals); + assert!(rows.is_empty()); +} + +#[test] +fn agents_without_a_machine_summary_still_get_a_root() { + let fixture = Fixture::new(); + let terminals = fixture.terminals(); + let mut inventory = fixture.inventory(); + inventory.replace(Some(vec![fixture.agent.clone()]), None); + let rows = inventory.tree_rows("", &terminals); + assert!(matches!(rows[0].kind, TreeRowKind::Machine(hub) if hub == fixture.hub.hub_id)); + inventory + .select_tree( + TreeSelection::Machine(fixture.hub.hub_id), + "", + &terminals, + ); + assert_eq!(inventory.selected_machine, Some(fixture.hub.hub_id)); + assert_eq!(inventory.selected_agent, None); +} diff --git a/clients/tui/inventory/unit_tests/working_directory.rs b/clients/tui/inventory/unit_tests/working_directory.rs index 279aee1c..90668dc6 100644 --- a/clients/tui/inventory/unit_tests/working_directory.rs +++ b/clients/tui/inventory/unit_tests/working_directory.rs @@ -74,8 +74,8 @@ fn same_path_on_different_hubs_never_changes_origin_scope() { inventory.replace(Some(vec![first.agent.clone(), second.agent]), None); inventory.scope = Some(first.hub.hub_id); assert_eq!( - inventory.visible_agents(AGENT_WORKING_DIRECTORY, &terminals), - vec![first.agent.cursor().target()] + inventory.visible_agents(AGENT_WORKING_DIRECTORY, &terminals).len(), + 2 ); } diff --git a/clients/tui/local/startup/src/bootstrap.rs b/clients/tui/local/startup/src/bootstrap.rs index cd68c52a..7ca70d46 100644 --- a/clients/tui/local/startup/src/bootstrap.rs +++ b/clients/tui/local/startup/src/bootstrap.rs @@ -36,10 +36,13 @@ pub(crate) async fn run( tokio::process::ChildStdout::from_std(child.take_stdout().context(OUTPUT_MISSING)?)?; let (bytes, ()) = timeout_at(deadline, async { let bytes = capture(stdout).await?; - child + if child .wait_for_clean_exit(deadline.into_std()) .await - .context(START_FAILED)?; + .is_err() + { + return Err(anyhow::anyhow!(START_FAILED)); + } Ok::<_, anyhow::Error>((bytes, ())) }) .await diff --git a/clients/tui/render/src/lib.rs b/clients/tui/render/src/lib.rs index e7e03cd9..541d64ce 100644 --- a/clients/tui/render/src/lib.rs +++ b/clients/tui/render/src/lib.rs @@ -4,6 +4,7 @@ mod overlay; mod screen; mod selection; mod status_line; +mod tab_strip; mod terminal; mod wrap; @@ -15,4 +16,5 @@ pub use selection::{PaneSelection, highlight_selection}; pub use terminal::{PaneViewport, TerminalPane, render_terminal, terminal_inner}; pub use status_line::{draw as draw_status_line, elide}; +pub use tab_strip::{TabHit, TabStripItem, draw as draw_tab_strip}; pub use wrap::wrap; diff --git a/clients/tui/render/src/tab_strip.rs b/clients/tui/render/src/tab_strip.rs new file mode 100644 index 00000000..b2e63c69 --- /dev/null +++ b/clients/tui/render/src/tab_strip.rs @@ -0,0 +1,81 @@ +use ratatui::{ + Frame, + layout::Rect, + style::{Color, Modifier, Style}, + text::Line, + widgets::Paragraph, +}; +use uuid::Uuid; + +const STRIP_ROWS: u16 = 1; +const MAX_TAB_COLUMNS: u16 = 32; +const ACTIVE_COLOR: Color = Color::Cyan; +/// A fixed dark 256-color slot keeps the cyan label readable even when a +/// theme maps `DarkGray` to a relatively bright value. +const ACTIVE_BACKGROUND: Color = Color::Indexed(237); + +#[derive(Clone, Debug, Eq, PartialEq)] +pub struct TabStripItem { + pub id: Uuid, + pub label: String, + pub active: bool, +} + +#[derive(Clone, Copy, Debug, Eq, PartialEq)] +pub struct TabHit { + pub id: Uuid, + pub area: Rect, +} + +pub fn draw(frame: &mut Frame<'_>, area: Rect, items: &[TabStripItem]) -> Vec { + if area.is_empty() || items.is_empty() { + return Vec::new(); + } + let labels = items + .iter() + .map(|item| Line::from(item.label.clone())) + .collect::>(); + let widths = labels + .iter() + .map(|label| { + label + .width() + .min(usize::from(MAX_TAB_COLUMNS.min(area.width))) as u16 + }) + .collect::>(); + let active = items.iter().position(|item| item.active).unwrap_or_default(); + let mut start = active; + let mut used = widths[start]; + while let Some(previous) = start.checked_sub(1) { + let proposed = used.saturating_add(widths[previous]); + if proposed > area.width { + break; + } + used = proposed; + start = previous; + } + let mut tabs = Vec::new(); + let mut x = area.x; + for (item, (label, width)) in items.iter().zip(labels.into_iter().zip(widths)).skip(start) { + let width = width.min(area.right().saturating_sub(x)); + if width == 0 { + break; + } + let tab_area = Rect::new(x, area.y, width, STRIP_ROWS); + let style = if item.active { + Style::default() + .fg(ACTIVE_COLOR) + .bg(ACTIVE_BACKGROUND) + .add_modifier(Modifier::BOLD) + } else { + Style::default() + }; + frame.render_widget(Paragraph::new(label).style(style), tab_area); + tabs.push(TabHit { + id: item.id, + area: tab_area, + }); + x = x.saturating_add(width); + } + tabs +} diff --git a/clients/tui/service/BUILD.bazel b/clients/tui/service/BUILD.bazel new file mode 100644 index 00000000..3b6f833f --- /dev/null +++ b/clients/tui/service/BUILD.bazel @@ -0,0 +1,29 @@ +load("@rules_rust//rust:defs.bzl", "rust_library") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "//core/crates/application/local/lifecycle:yolo_local_lifecycle", + "//core/crates/application/local/profile:yolo_local_profile", + "//core/crates/build_image:yolo_build_image", + "@crates//:anyhow", + "@crates//:clap", + "@crates//:serde", + "@crates//:serde_json", +] + +rust_library( + name = "yolo_tui_service", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_tui_service", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) + +filegroup( + name = "architecture_check_data", + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], +) diff --git a/clients/tui/service/src/lib.rs b/clients/tui/service/src/lib.rs new file mode 100644 index 00000000..5d1a38e0 --- /dev/null +++ b/clients/tui/service/src/lib.rs @@ -0,0 +1,61 @@ +//! User-facing local service lifecycle commands. +mod render; + +use anyhow::Result; +use clap::Args; +use render::{print_status, print_stop, print_version}; +use yolo_local_lifecycle::{ + StatusReport, StopReport, restart, start, status, stop, validate_state, +}; +use yolo_local_profile::LocalProfile; + +#[derive(Clone, Copy, Debug, Args)] +pub struct FormatOptions { + /// Print one JSON document. + #[arg(long)] + pub json: bool, +} + +pub async fn status_command(profile: &LocalProfile, options: FormatOptions) -> Result<()> { + print_status(&status(profile).await?, options.json) +} + +pub async fn version_command(profile: &LocalProfile, options: FormatOptions) -> Result<()> { + print_version(&status(profile).await?, options.json) +} + +pub async fn start_command(profile: &LocalProfile, options: FormatOptions) -> Result<()> { + start(profile, &std::env::current_exe()?).await?; + print_status(&status(profile).await?, options.json) +} + +pub async fn stop_command(profile: &LocalProfile, options: FormatOptions) -> Result<()> { + print_stop(&stop(profile).await?, options.json) +} + +pub async fn restart_command(profile: &LocalProfile, options: FormatOptions) -> Result<()> { + validate_state(profile)?; + let stopped = restart(profile, &std::env::current_exe()?).await?; + let report = status(profile).await?; + if options.json { + println!( + "{}", + serde_json::to_string_pretty(&RestartReport { + stop: stopped, + status: report, + })? + ); + } else { + render::print_stop_lines(&stopped); + render::status_lines(&report) + .iter() + .for_each(|line| println!("{line}")); + } + Ok(()) +} + +#[derive(serde::Serialize)] +struct RestartReport { + stop: StopReport, + status: StatusReport, +} diff --git a/clients/tui/service/src/render.rs b/clients/tui/service/src/render.rs new file mode 100644 index 00000000..0172d4b7 --- /dev/null +++ b/clients/tui/service/src/render.rs @@ -0,0 +1,150 @@ +use anyhow::Result; +use yolo_local_lifecycle::{HostStatus, HubStatus, StatusReport, StopReport}; + +pub(crate) fn print_status(report: &StatusReport, json: bool) -> Result<()> { + if json { + println!("{}", serde_json::to_string_pretty(report)?); + } else { + status_lines(report) + .iter() + .for_each(|line| println!("{line}")); + } + Ok(()) +} + +pub(crate) fn print_version(report: &StatusReport, json: bool) -> Result<()> { + if json { + println!("{}", serde_json::to_string_pretty(report)?); + } else { + version_lines(report) + .iter() + .for_each(|line| println!("{line}")); + } + Ok(()) +} + +pub(crate) fn print_stop(report: &StopReport, json: bool) -> Result<()> { + if json { + println!("{}", serde_json::to_string_pretty(report)?); + } else { + print_stop_lines(report); + } + Ok(()) +} + +pub(crate) fn print_stop_lines(report: &StopReport) { + for service in &report.services { + match (service.stopped, service.pid) { + (true, Some(pid)) => println!("stopped {} (pid {pid})", service.service), + _ => println!("{} was not running", service.service), + } + } +} + +pub(crate) fn status_lines(report: &StatusReport) -> Vec { + let mut lines = version_lines(report); + lines.push(format!( + "hub {}{}{}", + hub_state(&report.hub), + image_suffix(report.hub.image.as_ref()), + protocol_suffix(&report.hub) + )); + lines.push(format!( + "host {}{}", + host_state(&report.host), + image_suffix(report.host.image.as_ref()) + )); + lines.push(format!( + "file {}", + report + .file_helper + .as_ref() + .map_or_else(|| "unavailable".into(), |image| short(image.sha256())) + )); + lines.push(format!( + "state {}", + if report.state_contract { + "ready" + } else { + "missing or incompatible" + } + )); + lines.push(format!("images {}", image_state(report))); + if report.restart_needed { + lines.push("restart needed; run `yolo restart`".into()); + } else if !report.hub.running || !report.host.running { + lines.push("start run `yolo server start`".into()); + } else { + lines.push("restart not needed".into()); + } + lines +} + +pub(crate) fn version_lines(report: &StatusReport) -> Vec { + let mut lines = vec![format!( + "installed yolo {} image {}", + report.client_version, + short(report.client_image.sha256()) + )]; + let running = if report.hub.running || report.host.running { + let hub = report + .hub + .image + .as_ref() + .map_or_else(|| "unknown".into(), |image| short(image.sha256())); + let host = report + .host + .image + .as_ref() + .map_or_else(|| "unknown".into(), |image| short(image.sha256())); + format!("hub {hub} host {host}") + } else { + "none".into() + }; + lines.push(format!("running {running}")); + lines +} + +fn image_state(report: &StatusReport) -> &'static str { + if !report.hub.running && !report.host.running { + "not running" + } else if report.live_images_consistent { + "consistent" + } else { + "mixed or unavailable" + } +} + +fn hub_state(hub: &HubStatus) -> String { + if !hub.running { + return "stopped".into(); + } + hub.pid + .map_or_else(|| "running".into(), |pid| format!("running pid {pid}")) +} + +fn host_state(host: &HostStatus) -> String { + if !host.running { + return "stopped".into(); + } + host.pid + .map_or_else(|| "running".into(), |pid| format!("running pid {pid}")) +} + +fn image_suffix(image: Option<&yolo_build_image::BuildImage>) -> String { + image.map_or_else( + || " image unavailable".into(), + |image| format!(" image {}", short(image.sha256())), + ) +} + +fn protocol_suffix(hub: &HubStatus) -> String { + hub.protocol.map_or_else( + || " protocol unavailable".into(), + |protocol| format!(" protocol {protocol}/{}", hub.expected_protocol), + ) +} + +fn short(sha256: &str) -> String { + sha256.chars().take(12).collect() +} diff --git a/clients/tui/tab/integration_tests/schema.rs b/clients/tui/tab/integration_tests/schema.rs index 1ad92a00..fb326ebe 100644 --- a/clients/tui/tab/integration_tests/schema.rs +++ b/clients/tui/tab/integration_tests/schema.rs @@ -1,7 +1,48 @@ use crate::support::*; use serde_json::{Value, json}; +use yolo_hub_identity::{HubId, TerminalId}; +use yolo_presentation_layout::Layout; use yolo_tui_tab::*; +#[test] +fn v5_catalog_migrates_to_per_machine_v6_and_persists() { + let (_root, path, hub) = fixture(); + let mut store = TabStore::open(&path, hub).unwrap(); + let remote = HubId::new(); + let mut tabs = TabCatalog::new(hub, LocalTabView::default()); + tabs.select_machine(remote).unwrap(); + tabs.update_active(LocalTabView { + layout: Layout::new(Some(yolo_hub_identity::TerminalAddress { + hub_id: remote, + terminal_id: TerminalId::new(), + })), + view: TabViewMode::Terminal, + }); + store.save_catalog(&tabs).unwrap(); + drop(store); + let mut value: Value = yolo_paths::read_json(&path.join(TAB_FILE)).unwrap(); + value[SCHEMA_FIELD] = Value::from(5); + let tabs = value[TABS_FIELD].as_object_mut().unwrap(); + tabs.remove("machine"); + tabs.remove("active_by_machine"); + for entry in tabs[ENTRIES_FIELD].as_array_mut().unwrap() { + entry.as_object_mut().unwrap().remove("machine"); + } + yolo_paths::atomic_write_json(&path.join(TAB_FILE), &value).unwrap(); + let store = TabStore::open(&path, hub).unwrap(); + assert_eq!(store.document().schema_version, TAB_SCHEMA_VERSION); + assert!( + store + .document() + .tabs + .entries() + .iter() + .any(|entry| entry.machine == remote) + ); + let rewritten: Value = yolo_paths::read_json(&path.join(TAB_FILE)).unwrap(); + assert_eq!(rewritten[SCHEMA_FIELD], Value::from(TAB_SCHEMA_VERSION)); +} + #[test] fn single_workspace_format_is_rejected_without_rewriting_bindings() { let (_root, path, hub) = fixture(); diff --git a/clients/tui/tab/manager/src/input.rs b/clients/tui/tab/manager/src/input.rs index accffe36..f2398932 100644 --- a/clients/tui/tab/manager/src/input.rs +++ b/clients/tui/tab/manager/src/input.rs @@ -57,7 +57,7 @@ impl Manager { } fn move_selection(&mut self, tabs: &TabCatalog, forward: bool) { - let entries = tabs.entries(); + let entries = tabs.machine_entries(); let index = entries .iter() .position(|entry| entry.id == self.selected) diff --git a/clients/tui/tab/manager/src/lib.rs b/clients/tui/tab/manager/src/lib.rs index b674494c..3a41367f 100644 --- a/clients/tui/tab/manager/src/lib.rs +++ b/clients/tui/tab/manager/src/lib.rs @@ -47,7 +47,7 @@ impl Manager { } pub fn rename(&mut self, tabs: &TabCatalog) { if let Some(entry) = tabs - .entries() + .machine_entries() .iter() .find(|entry| entry.id == self.selected) { diff --git a/clients/tui/tab/manager/src/render.rs b/clients/tui/tab/manager/src/render.rs index fd8dc557..605d5e95 100644 --- a/clients/tui/tab/manager/src/render.rs +++ b/clients/tui/tab/manager/src/render.rs @@ -16,7 +16,7 @@ pub fn render(frame: &mut Frame<'_>, manager: &Manager, tabs: &TabCatalog, area: .split(area); frame.render_widget(Paragraph::new(format!("{TITLE}\n{HELP}")), areas[0]); let rows = tabs - .entries() + .machine_entries() .iter() .map(|entry| { format!( @@ -31,7 +31,7 @@ pub fn render(frame: &mut Frame<'_>, manager: &Manager, tabs: &TabCatalog, area: }) .collect::>(); let mut selection = ListState::default().with_selected( - tabs.entries() + tabs.machine_entries() .iter() .position(|entry| entry.id == manager.selected), ); diff --git a/clients/tui/tab/src/catalog.rs b/clients/tui/tab/src/catalog.rs index b6caa0cf..9a9e8d7e 100644 --- a/clients/tui/tab/src/catalog.rs +++ b/clients/tui/tab/src/catalog.rs @@ -1,9 +1,9 @@ -use crate::{DocumentPane, LocalTabView, TabViewMode, constants::*}; -use std::collections::BTreeMap; +use crate::{DocumentPane, LocalTabView, constants::*}; use anyhow::{Result, ensure}; use serde::{Deserialize, Serialize}; -use std::collections::BTreeSet; +use std::collections::{BTreeMap, BTreeSet}; use uuid::Uuid; +use yolo_hub_identity::HubId; #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -11,12 +11,19 @@ pub struct TabEntry { pub id: Uuid, pub name: String, pub view: LocalTabView, + /// The machine whose workspace owns this tab. Tabs never switch machines. + pub machine: HubId, } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct TabCatalogDocument { + /// Active tab of `machine`. pub active: Uuid, + /// The machine workspace currently shown. + pub machine: HubId, + /// Active tab per machine, including `machine`. + pub active_by_machine: BTreeMap, pub entries: Vec, /// Plugin documents are client-local presentation content. They persist /// with this tab document but never enter the shared terminal layout. @@ -35,113 +42,84 @@ pub struct TabCatalogDocument { pub struct TabCatalog(pub(crate) TabCatalogDocument); impl TabCatalog { - pub fn single(view: LocalTabView) -> Self { + pub fn new(machine: HubId, view: LocalTabView) -> Self { let id = Uuid::new_v4(); + let mut active_by_machine = BTreeMap::new(); + active_by_machine.insert(machine, id); Self(TabCatalogDocument { active: id, + machine, + active_by_machine, entries: vec![TabEntry { id, name: DEFAULT_TAB_NAME.into(), view, + machine, }], documents: Default::default(), }) } - pub fn active(&self) -> &TabEntry { - self.entries() - .iter() - .find(|entry| entry.id == self.0.active) - .expect(ACTIVE_ERROR) + pub fn single(view: LocalTabView) -> Self { + Self::new(HubId::new(), view) } - pub fn entries(&self) -> &[TabEntry] { - &self.0.entries + pub fn machine(&self) -> HubId { + self.0.machine } - pub fn update_active(&mut self, view: LocalTabView) { - self.0 - .entries - .iter_mut() - .find(|entry| entry.id == self.0.active) - .expect(ACTIVE_ERROR) - .view = view; + pub fn active_by_machine(&self) -> &BTreeMap { + &self.0.active_by_machine } - pub fn active_layout(&self) -> &yolo_presentation_layout::Layout { - &self.active().view.layout + pub fn machine_entries(&self) -> Vec<&TabEntry> { + self.entries() + .iter() + .filter(|entry| entry.machine == self.0.machine) + .collect() } - pub fn active_layout_mut(&mut self) -> &mut yolo_presentation_layout::Layout { - &mut self - .0 - .entries - .iter_mut() + pub fn active(&self) -> &TabEntry { + self.entries() + .iter() .find(|entry| entry.id == self.0.active) .expect(ACTIVE_ERROR) - .view - .layout - } - - pub fn replace_active_layout(&mut self, layout: yolo_presentation_layout::Layout) { - self.active_layout_mut().clone_from(&layout); - } - - pub fn active_view(&self) -> TabViewMode { - self.active().view.view - } - - /// Document panes are client-local and keyed by their owning tab. They are - /// not part of the shared layout, so publishing or adopting a layout never - /// changes the terminal topology. - pub fn active_document(&self) -> Option<&DocumentPane> { - self.0.documents.get(&self.0.active) - } - - pub fn open_document(&mut self, title: String, text: String) -> bool { - self.0 - .documents - .insert(self.0.active, DocumentPane { title, text }); - true } - pub fn close_document(&mut self) -> bool { - self.0.documents.remove(&self.0.active).is_some() - } - - pub fn replace_active_view(&mut self, view: TabViewMode) { - self.0 - .entries - .iter_mut() - .find(|entry| entry.id == self.0.active) - .expect(ACTIVE_ERROR) - .view - .view = view; + pub fn entries(&self) -> &[TabEntry] { + &self.0.entries } pub fn select(&mut self, id: Uuid) -> Result { - ensure!( - self.entries().iter().any(|entry| entry.id == id), - ACTIVE_ERROR - ); - let changed = self.0.active != id; + let machine = self + .entries() + .iter() + .find(|entry| entry.id == id) + .map(|entry| entry.machine) + .ok_or_else(|| anyhow::anyhow!(ACTIVE_ERROR))?; + let changed = self.0.active != id || self.0.machine != machine; self.0.active = id; + self.0.machine = machine; + self.0.active_by_machine.insert(machine, id); Ok(changed) } pub fn cycle(&mut self, forward: bool) { - let index = self - .entries() + let entries = self.machine_entries(); + let index = entries .iter() .position(|entry| entry.id == self.0.active) .expect(ACTIVE_ERROR); - let count = self.entries().len(); + let count = entries.len(); let next = if forward { (index + 1) % count } else { (index + count - 1) % count }; - self.0.active = self.entries()[next].id; + self.0.active = entries[next].id; + self.0 + .active_by_machine + .insert(self.0.machine, self.0.active); } } @@ -154,16 +132,40 @@ impl Default for TabCatalog { impl TryFrom for TabCatalog { type Error = anyhow::Error; fn try_from(document: TabCatalogDocument) -> Result { - ensure!( - !document.entries.is_empty() && document.entries.len() <= MAX_TABS, - COUNT_ERROR - ); + ensure!(!document.entries.is_empty(), COUNT_ERROR); let mut ids = BTreeSet::new(); + let mut machines = BTreeMap::::new(); for entry in &document.entries { ensure!(!entry.id.is_nil() && ids.insert(entry.id), ID_ERROR); validate_name(&entry.name)?; + *machines.entry(entry.machine).or_default() += 1; } + ensure!( + machines.values().all(|count| *count <= MAX_TABS) + && machines.len() == document.active_by_machine.len(), + COUNT_ERROR + ); ensure!(ids.contains(&document.active), ACTIVE_ERROR); + let active = document + .entries + .iter() + .find(|entry| entry.id == document.active) + .ok_or_else(|| anyhow::anyhow!(ACTIVE_ERROR))?; + ensure!(active.machine == document.machine, ACTIVE_ERROR); + for (machine, id) in &document.active_by_machine { + ensure!( + machines.contains_key(machine) + && document + .entries + .iter() + .any(|entry| entry.id == *id && entry.machine == *machine), + ACTIVE_ERROR + ); + } + ensure!( + document.active_by_machine.get(&document.machine) == Some(&document.active), + ACTIVE_ERROR + ); Ok(Self(document)) } } diff --git a/clients/tui/tab/src/catalog_edit.rs b/clients/tui/tab/src/catalog_edit.rs index ab6b2c63..79808ca9 100644 --- a/clients/tui/tab/src/catalog_edit.rs +++ b/clients/tui/tab/src/catalog_edit.rs @@ -1,13 +1,47 @@ use crate::{LocalTabView, TabCatalog, TabEntry, catalog::validate_name, constants::*}; use anyhow::{Result, ensure}; use uuid::Uuid; +use yolo_hub_identity::HubId; impl TabCatalog { + /// Switches to a machine workspace, creating one empty tab when it has none. + pub fn select_machine(&mut self, machine: HubId) -> Result { + if self.machine() == machine && self.active_by_machine().contains_key(&machine) { + return Ok(false); + } + if let Some(id) = self.active_by_machine().get(&machine).copied() + && self + .entries() + .iter() + .any(|entry| entry.id == id && entry.machine == machine) + { + self.0.active = id; + self.0.machine = machine; + return Ok(true); + } + let name = self.next_name(machine)?; + let id = Uuid::new_v4(); + self.0.entries.push(TabEntry { + id, + name, + view: LocalTabView::default(), + machine, + }); + self.0.active_by_machine.insert(machine, id); + self.0.active = id; + self.0.machine = machine; + Ok(true) + } + pub fn create(&mut self, name: &str) -> Result { - ensure!(self.entries().len() < MAX_TABS, COUNT_ERROR); + let machine = self.machine(); + ensure!(self.count_for(machine) < MAX_TABS, COUNT_ERROR); validate_name(name)?; ensure!( - !self.entries().iter().any(|entry| entry.name == name), + !self + .entries() + .iter() + .any(|entry| entry.machine == machine && entry.name == name), NAME_CONFLICT ); let id = Uuid::new_v4(); @@ -15,18 +49,26 @@ impl TabCatalog { id, name: name.into(), view: LocalTabView::default(), + machine, }); self.0.active = id; + self.0.active_by_machine.insert(machine, id); Ok(id) } pub fn rename(&mut self, id: Uuid, name: &str) -> Result<()> { validate_name(name)?; + let machine = self + .entries() + .iter() + .find(|entry| entry.id == id) + .map(|entry| entry.machine) + .ok_or_else(|| anyhow::anyhow!(ACTIVE_ERROR))?; ensure!( !self .entries() .iter() - .any(|entry| entry.id != id && entry.name == name), + .any(|entry| entry.id != id && entry.machine == machine && entry.name == name), NAME_CONFLICT ); let entry = self @@ -54,16 +96,79 @@ impl TabCatalog { /// Removing a view never asks the Hub to terminate any of its terminals. pub fn remove(&mut self, id: Uuid) -> Result<()> { - ensure!(self.entries().len() > 1, LAST_TAB); + let machine = self + .entries() + .iter() + .find(|entry| entry.id == id) + .map(|entry| entry.machine) + .ok_or_else(|| anyhow::anyhow!(ACTIVE_ERROR))?; + ensure!(self.count_for(machine) > 1, LAST_TAB); + self.remove_entry(id) + } + + /// Low-level removal used after a dead terminal empties a tab. The owning + /// machine must still have another tab, or the caller must replace the view. + pub fn remove_entry(&mut self, id: Uuid) -> Result<()> { let index = self .entries() .iter() .position(|entry| entry.id == id) .ok_or_else(|| anyhow::anyhow!(ACTIVE_ERROR))?; + let machine = self.entries()[index].machine; + let before = self + .entries() + .iter() + .filter(|entry| entry.machine == machine) + .position(|entry| entry.id == id) + .unwrap_or_default(); self.0.entries.remove(index); - if self.0.active == id { - self.0.active = self.entries()[index.min(self.entries().len() - 1)].id; + self.0.documents.remove(&id); + let remaining: Vec = self + .entries() + .iter() + .filter(|entry| entry.machine == machine) + .map(|entry| entry.id) + .collect(); + if remaining.is_empty() { + self.0.active_by_machine.remove(&machine); + ensure!(!self.0.entries.is_empty(), LAST_TAB); + if self.0.machine == machine { + self.0.machine = self.0.entries[0].machine; + } + } else { + let next = remaining[before.min(remaining.len() - 1)]; + if self.0.active_by_machine.get(&machine) == Some(&id) { + self.0.active_by_machine.insert(machine, next); + } + if self.0.active == id { + self.0.active = next; + } + } + let active_machine = self.0.machine; + if self.0.active_by_machine.get(&active_machine) != Some(&self.0.active) { + self.0.active = self.0.active_by_machine[&active_machine]; } Ok(()) } + + /// First unused `Tab N` for a machine. + pub fn next_name(&self, machine: HubId) -> Result { + ensure!(self.count_for(machine) < MAX_TABS, COUNT_ERROR); + (1..=MAX_TABS) + .map(|number| format!("{TAB_NAME_PREFIX} {number}")) + .find(|name| { + !self + .entries() + .iter() + .any(|entry| entry.machine == machine && entry.name == *name) + }) + .ok_or_else(|| anyhow::anyhow!(COUNT_ERROR)) + } + + pub fn count_for(&self, machine: HubId) -> usize { + self.entries() + .iter() + .filter(|entry| entry.machine == machine) + .count() + } } diff --git a/clients/tui/tab/src/catalog_view.rs b/clients/tui/tab/src/catalog_view.rs new file mode 100644 index 00000000..1215bf5e --- /dev/null +++ b/clients/tui/tab/src/catalog_view.rs @@ -0,0 +1,63 @@ +use crate::{DocumentPane, LocalTabView, TabCatalog, TabViewMode, constants::*}; + +impl TabCatalog { + pub fn update_active(&mut self, view: LocalTabView) { + self.0 + .entries + .iter_mut() + .find(|entry| entry.id == self.0.active) + .expect(ACTIVE_ERROR) + .view = view; + } + + pub fn active_layout(&self) -> &yolo_presentation_layout::Layout { + &self.active().view.layout + } + + pub fn active_layout_mut(&mut self) -> &mut yolo_presentation_layout::Layout { + &mut self + .0 + .entries + .iter_mut() + .find(|entry| entry.id == self.0.active) + .expect(ACTIVE_ERROR) + .view + .layout + } + + pub fn replace_active_layout(&mut self, layout: yolo_presentation_layout::Layout) { + self.active_layout_mut().clone_from(&layout); + } + + pub fn active_view(&self) -> TabViewMode { + self.active().view.view + } + + /// Document panes are client-local and keyed by their owning tab. They are + /// not part of the shared layout, so publishing or adopting a layout never + /// changes the terminal topology. + pub fn active_document(&self) -> Option<&DocumentPane> { + self.0.documents.get(&self.0.active) + } + + pub fn open_document(&mut self, title: String, text: String) -> bool { + self.0 + .documents + .insert(self.0.active, DocumentPane { title, text }); + true + } + + pub fn close_document(&mut self) -> bool { + self.0.documents.remove(&self.0.active).is_some() + } + + pub fn replace_active_view(&mut self, view: TabViewMode) { + self.0 + .entries + .iter_mut() + .find(|entry| entry.id == self.0.active) + .expect(ACTIVE_ERROR) + .view + .view = view; + } +} diff --git a/clients/tui/tab/src/constants.rs b/clients/tui/tab/src/constants.rs index d4a15a69..ce027660 100644 --- a/clients/tui/tab/src/constants.rs +++ b/clients/tui/tab/src/constants.rs @@ -1,8 +1,10 @@ pub const TAB_FILE: &str = "tabs.json"; -pub const TAB_SCHEMA_VERSION: u16 = 5; +pub const TAB_SCHEMA_VERSION: u16 = 6; +pub(crate) const SCHEMA_VERSION_FIELD: &str = "schema_version"; pub const MAX_TABS: usize = yolo_presentation_protocol::MAX_TABS; pub const MAX_TAB_NAME_BYTES: usize = yolo_presentation_protocol::MAX_TITLE_BYTES; pub const DEFAULT_TAB_NAME: &str = "Tab 1"; +pub const TAB_NAME_PREFIX: &str = "Tab"; pub(crate) const COUNT_ERROR: &str = "saved terminal layout count is outside supported bounds"; pub(crate) const ID_ERROR: &str = "saved terminal layout identities must be nonempty and unique"; pub(crate) const ACTIVE_ERROR: &str = "selected terminal layout does not exist"; @@ -10,6 +12,8 @@ pub(crate) const NAME_ERROR: &str = "saved tab title is empty, untrimmed, contains controls or exceeds its byte limit"; pub(crate) const NAME_CONFLICT: &str = "saved terminal layout name is already in use"; pub(crate) const LAST_TAB: &str = "cannot remove the last terminal layout"; +pub(crate) const SHARED_TABS_EMPTY: &str = + "the entry machine has no terminal layout to publish"; pub const FIRST_TAB_REVISION: u64 = 1; pub(crate) const LOCK_FILE: &str = "tabs.lock"; pub(crate) const LOCK_BYTES: u64 = 4096; diff --git a/clients/tui/tab/src/lib.rs b/clients/tui/tab/src/lib.rs index f70bdb0b..b05bca8e 100644 --- a/clients/tui/tab/src/lib.rs +++ b/clients/tui/tab/src/lib.rs @@ -1,10 +1,12 @@ mod catalog; mod catalog_edit; +mod catalog_view; mod client_location; mod client_registry; mod constants; mod document; mod location; +mod migration; mod shared_layout; mod store; diff --git a/clients/tui/tab/src/migration.rs b/clients/tui/tab/src/migration.rs new file mode 100644 index 00000000..5cc1d19c --- /dev/null +++ b/clients/tui/tab/src/migration.rs @@ -0,0 +1,110 @@ +use crate::{ + DocumentPane, LayoutCheckpoint, LocalTabView, TabCatalog, TabCatalogDocument, TabDocument, + TabEntry, constants::*, +}; +use anyhow::{Result, ensure}; +use serde::Deserialize; +use serde_json::Value; +use std::collections::{BTreeMap, BTreeSet}; +use uuid::Uuid; +use yolo_hub_identity::HubId; +use yolo_presentation_protocol::LayoutAddress; + +pub(crate) const LEGACY_SCHEMA_VERSION: u16 = 5; + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct LegacyTabDocument { + schema_version: u16, + entry_hub: HubId, + layout_address: LayoutAddress, + shared: Option, + revision: u64, + tabs: LegacyTabCatalog, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct LegacyTabCatalog { + active: Uuid, + entries: Vec, + #[serde(default)] + documents: BTreeMap, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct LegacyTabEntry { + id: Uuid, + name: String, + view: LocalTabView, +} + +pub(crate) fn schema_version(value: &Value) -> Option { + u16::try_from(value.get(SCHEMA_VERSION_FIELD)?.as_u64()?).ok() +} + +/// Assigns every v5 tab to a machine and rebuilds per-machine active maps. +/// Tabs in the last shared projection belong to the entry Hub; otherwise the +/// first bound terminal names the machine, falling back to the entry Hub. +pub(crate) fn migrate_v5(value: Value, entry: HubId) -> Result { + let legacy: LegacyTabDocument = serde_json::from_value(value)?; + ensure!(legacy.schema_version == LEGACY_SCHEMA_VERSION, SCHEMA_ERROR); + ensure!(legacy.entry_hub == entry, ENTRY_ERROR); + let shared_ids = legacy + .shared + .iter() + .flat_map(|shared| shared.projected.iter().map(|tab| tab.id)) + .collect::>(); + let legacy_active = legacy.tabs.active; + let mut active_by_machine = BTreeMap::new(); + let mut active_machine = None; + let mut entries = Vec::new(); + for tab in legacy.tabs.entries { + let machine = if shared_ids.contains(&tab.id) { + entry + } else { + tab.view + .layout + .panes() + .into_iter() + .find_map(|pane| pane.terminal.map(|address| address.hub_id)) + .unwrap_or(entry) + }; + if tab.id == legacy_active { + active_machine = Some(machine); + } + active_by_machine.entry(machine).or_insert(tab.id); + entries.push(TabEntry { + id: tab.id, + name: tab.name, + view: tab.view, + machine, + }); + } + let machine = active_machine.unwrap_or(entry); + let active = if entries + .iter() + .any(|entry| entry.id == legacy_active && entry.machine == machine) + { + legacy_active + } else { + *active_by_machine.get(&machine).ok_or_else(|| anyhow::anyhow!(ACTIVE_ERROR))? + }; + active_by_machine.insert(machine, active); + let tabs = TabCatalog::try_from(TabCatalogDocument { + active, + machine, + active_by_machine, + entries, + documents: legacy.tabs.documents, + })?; + Ok(TabDocument { + schema_version: TAB_SCHEMA_VERSION, + entry_hub: entry, + layout_address: legacy.layout_address, + shared: legacy.shared, + revision: legacy.revision, + tabs, + }) +} diff --git a/clients/tui/tab/src/shared_layout.rs b/clients/tui/tab/src/shared_layout.rs index 379d08ca..47a2c73a 100644 --- a/clients/tui/tab/src/shared_layout.rs +++ b/clients/tui/tab/src/shared_layout.rs @@ -1,14 +1,26 @@ -use crate::{TabCatalogDocument, TabEntry, TabViewMode, LocalTabView, TabCatalog}; -use anyhow::Result; -use std::collections::BTreeSet; +use crate::{ + DEFAULT_TAB_NAME, LocalTabView, TabCatalog, TabCatalogDocument, TabEntry, TabViewMode, + constants::*, +}; +use anyhow::{Result, ensure}; +use std::collections::{BTreeMap, BTreeSet}; +use uuid::Uuid; +use yolo_hub_identity::HubId; use yolo_presentation_layout::{Layout, PaneLayoutDocument as PaneView}; use yolo_presentation_protocol::{SharedLayoutDocument, SharedTab}; impl TabCatalog { - /// Export topology and terminal bindings without publishing this client's focus or view mode. - pub fn shared_tabs(&self) -> Result> { - self.entries() + /// Export one machine's topology and bindings. Remote machine workspaces + /// are client-local and never enter the entry Hub's shared layout. + pub fn shared_tabs(&self, machine: HubId) -> Result> { + let entries = self + .entries() .iter() + .filter(|entry| entry.machine == machine) + .collect::>(); + ensure!(!entries.is_empty(), SHARED_TABS_EMPTY); + entries + .into_iter() .map(|entry| { Ok(SharedTab::new( entry.id, @@ -19,24 +31,41 @@ impl TabCatalog { .collect() } - /// Reconcile a shared document while retaining every surviving client-local selection. - /// Empty documents show a local placeholder; they do not create a terminal or shared tab. - pub fn from_shared(document: &SharedLayoutDocument, previous: &Self) -> Result { + /// Reconcile the entry machine's shared document while preserving every + /// other machine workspace and the client-local selection. + pub fn from_shared( + document: &SharedLayoutDocument, + previous: &Self, + entry_hub: HubId, + ) -> Result { document.validate()?; + let old_active = previous.active_by_machine().get(&entry_hub).copied(); + let mut shared = Vec::new(); if document.tabs.is_empty() { - return Ok(Self::default()); - } - let entries = document - .tabs - .iter() - .map(|tab| { - let old = previous.entries().iter().find(|entry| entry.id == tab.id); + let id = previous + .entries() + .iter() + .find(|entry| entry.machine == entry_hub) + .map(|entry| entry.id) + .unwrap_or_else(Uuid::new_v4); + shared.push(TabEntry { + id, + name: DEFAULT_TAB_NAME.into(), + view: LocalTabView::default(), + machine: entry_hub, + }); + } else { + for tab in &document.tabs { + let old = previous + .entries() + .iter() + .find(|entry| entry.id == tab.id && entry.machine == entry_hub); let panes = tab.root.panes(); let focused = old .map(|entry| entry.view.layout.focused()) .filter(|id| panes.iter().any(|pane| pane.id == *id)) .unwrap_or(panes[0].id); - Ok(TabEntry { + shared.push(TabEntry { id: tab.id, name: tab.title.clone(), view: LocalTabView { @@ -46,19 +75,57 @@ impl TabCatalog { })?, view: old.map_or(TabViewMode::Terminal, |entry| entry.view.view), }, - }) - }) - .collect::>>()?; - let active = previous.active().id; - let active = entries - .iter() - .find(|entry| entry.id == active) - .unwrap_or(&entries[0]) - .id; - let ids = entries + machine: entry_hub, + }); + } + } + let incoming = shared.iter().map(|entry| entry.id).collect::>(); + let mut entries = shared; + entries.extend( + previous + .entries() + .iter() + .filter(|entry| entry.machine != entry_hub && !incoming.contains(&entry.id)) + .cloned(), + ); + let mut active_by_machine = BTreeMap::new(); + for (machine, id) in previous.active_by_machine() { + if *machine == entry_hub { + continue; + } + let chosen = entries + .iter() + .find(|entry| entry.machine == *machine && entry.id == *id) + .map(|entry| entry.id) + .or_else(|| { + entries + .iter() + .find(|entry| entry.machine == *machine) + .map(|entry| entry.id) + }); + if let Some(chosen) = chosen { + active_by_machine.insert(*machine, chosen); + } + } + let entry_active = entries .iter() + .find(|entry| entry.machine == entry_hub && Some(entry.id) == old_active) .map(|entry| entry.id) - .collect::>(); + .unwrap_or(entries[0].id); + active_by_machine.insert(entry_hub, entry_active); + for entry in &entries { + active_by_machine.entry(entry.machine).or_insert(entry.id); + } + let machine = if entries + .iter() + .any(|entry| entry.machine == previous.machine()) + { + previous.machine() + } else { + entry_hub + }; + let active = active_by_machine[&machine]; + let ids = entries.iter().map(|entry| entry.id).collect::>(); let documents = previous .0 .documents @@ -68,6 +135,8 @@ impl TabCatalog { .collect(); Self::try_from(TabCatalogDocument { active, + machine, + active_by_machine, entries, documents, }) diff --git a/clients/tui/tab/src/store.rs b/clients/tui/tab/src/store.rs index 52c3e64b..471b7807 100644 --- a/clients/tui/tab/src/store.rs +++ b/clients/tui/tab/src/store.rs @@ -1,6 +1,8 @@ -use crate::{LayoutCheckpoint, LocalTabView, TabDocument, TabCatalog, constants::*}; -use anyhow::{Context, Result, ensure}; -use serde_json::Error as JsonError; +use crate::{ + LayoutCheckpoint, LocalTabView, TabCatalog, TabDocument, constants::*, migration, +}; +use anyhow::{Context, Result, anyhow, ensure}; +use serde_json::{Error as JsonError, Value}; use std::path::{Path, PathBuf}; use yolo_hub_identity::HubId; use yolo_paths::{ @@ -39,7 +41,7 @@ impl TabStore { }; directory.verify_path()?; let namespace = NamespaceLease::from_directories(vec![(NAMESPACE, directory)])?; - let (document, identity) = if namespace.file_is_missing(&path)? { + let (mut document, mut identity) = if namespace.file_is_missing(&path)? { let document = TabDocument { schema_version: TAB_SCHEMA_VERSION, entry_hub: entry, @@ -49,7 +51,7 @@ impl TabStore { }, shared: None, revision: FIRST_TAB_REVISION, - tabs: TabCatalog::default(), + tabs: TabCatalog::new(entry, LocalTabView::default()), }; let identity = namespace.write_json_expected_with_commit_fence(&path, None, &document, || { @@ -57,7 +59,24 @@ impl TabStore { })?; (document, identity) } else { - namespace.read_json_with_identity::(&path)? + let (value, identity) = namespace.read_json_with_identity::(&path)?; + match migration::schema_version(&value) { + Some(version) if version == TAB_SCHEMA_VERSION => { + (serde_json::from_value::(value)?, identity) + } + Some(version) if version == migration::LEGACY_SCHEMA_VERSION => { + let document = migration::migrate_v5(value, entry)?; + document.validate(entry)?; + let identity = namespace.write_json_expected_with_commit_fence( + &path, + Some(identity), + &document, + || lock.verify(), + )?; + (document, identity) + } + _ => return Err(anyhow!(SCHEMA_ERROR)), + } }; document.validate(entry)?; Ok(Some(Self { diff --git a/clients/tui/tab/unit_tests/lib.rs b/clients/tui/tab/unit_tests/lib.rs index 4c81b158..bc8f4d4b 100644 --- a/clients/tui/tab/unit_tests/lib.rs +++ b/clients/tui/tab/unit_tests/lib.rs @@ -1,5 +1,6 @@ mod catalog; mod catalog_validation; +mod machines; mod shared_layout; #[path = "../test_support/mod.rs"] mod support; diff --git a/clients/tui/tab/unit_tests/machines.rs b/clients/tui/tab/unit_tests/machines.rs new file mode 100644 index 00000000..56e37e57 --- /dev/null +++ b/clients/tui/tab/unit_tests/machines.rs @@ -0,0 +1,52 @@ +use crate::*; +use yolo_hub_identity::HubId; + +#[test] +fn machines_own_independent_tabs_and_active_selection() { + let first = HubId::new(); + let second = HubId::new(); + let mut tabs = TabCatalog::new(first, LocalTabView::default()); + let first_id = tabs.active().id; + assert_eq!(tabs.active().machine, first); + assert!(tabs.select_machine(second).unwrap()); + let second_id = tabs.active().id; + assert_ne!(first_id, second_id); + assert_eq!(tabs.machine(), second); + assert_eq!(tabs.active().name, DEFAULT_TAB_NAME); + assert!(tabs.create(DEFAULT_TAB_NAME).is_err()); + let build = tabs.create("build").unwrap(); + assert_eq!(tabs.machine_entries().len(), 2); + assert!(tabs.select(first_id).unwrap()); + assert_eq!(tabs.machine(), first); + assert_eq!(tabs.machine_entries().len(), 1); + assert!(!tabs.select(first_id).unwrap()); + assert!(tabs.select_machine(second).unwrap()); + assert_eq!(tabs.active().id, build); + tabs.select(first_id).unwrap(); + assert!(tabs.remove(first_id).is_err()); + tabs.select_machine(second).unwrap(); + tabs.create("second").unwrap(); + tabs.select(second_id).unwrap(); + tabs.remove(second_id).unwrap(); + assert_eq!(tabs.active().id, build); + assert_eq!(tabs.active_by_machine().get(&second), Some(&build)); + assert_eq!(tabs.active_by_machine().get(&first), Some(&first_id)); +} + +#[test] +fn per_machine_state_roundtrips_through_the_document() { + let first = HubId::new(); + let second = HubId::new(); + let mut tabs = TabCatalog::new(first, LocalTabView::default()); + tabs.select_machine(second).unwrap(); + tabs.create("remote").unwrap(); + tabs.select_machine(first).unwrap(); + let encoded = serde_json::to_vec(&tabs).unwrap(); + let restored: TabCatalog = serde_json::from_slice(&encoded).unwrap(); + assert_eq!(restored, tabs); + assert_eq!(restored.machine(), first); + assert_eq!(restored.active_by_machine().len(), 2); + assert_eq!(restored.machine_entries().len(), 1); + restored.shared_tabs(first).unwrap(); + assert!(restored.shared_tabs(HubId::new()).is_err()); +} diff --git a/clients/tui/tab/unit_tests/shared_layout.rs b/clients/tui/tab/unit_tests/shared_layout.rs index 55fd6736..ac68ceea 100644 --- a/clients/tui/tab/unit_tests/shared_layout.rs +++ b/clients/tui/tab/unit_tests/shared_layout.rs @@ -11,7 +11,7 @@ fn document(catalog: &TabCatalog) -> yolo_presentation_protocol::SharedLayoutDoc }, request_id: Uuid::new_v4(), expected_revision: 0, - tabs: catalog.shared_tabs().unwrap(), + tabs: catalog.shared_tabs(catalog.machine()).unwrap(), } .apply(None) .unwrap() @@ -28,11 +28,11 @@ fn remote_topology_preserves_focus_active_tab_and_local_view() { local.update_active(tab_view); let mut remote = document(&local); remote.tabs[0].title = "changed remotely".into(); - let reconciled = TabCatalog::from_shared(&remote, &local).unwrap(); + let reconciled = TabCatalog::from_shared(&remote, &local, local.machine()).unwrap(); assert_eq!(reconciled.active().id, tab_id); assert_eq!(reconciled.active().view.layout.focused(), focused); assert_eq!(reconciled.active().view.view, TabViewMode::Inventory); - assert_eq!(reconciled.shared_tabs().unwrap(), remote.tabs); + assert_eq!(reconciled.shared_tabs(reconciled.machine()).unwrap(), remote.tabs); } #[test] @@ -42,7 +42,7 @@ fn document_panes_survive_topology_adoption_without_changing_it() { assert!(local.open_document("doc".into(), "body".into())); let remote = document(&local); assert_eq!(remote.tabs[0].root.panes().len(), before); - let reconciled = TabCatalog::from_shared(&remote, &local).unwrap(); + let reconciled = TabCatalog::from_shared(&remote, &local, local.machine()).unwrap(); assert_eq!(reconciled.active_document().unwrap().text, "body"); } @@ -52,13 +52,13 @@ fn closed_selection_falls_back_to_surviving_identity_without_runtime_actions() { local.create("removed").unwrap(); let mut remote = document(&local); remote.tabs.pop(); - let reconciled = TabCatalog::from_shared(&remote, &local).unwrap(); + let reconciled = TabCatalog::from_shared(&remote, &local, local.machine()).unwrap(); assert_eq!(reconciled.active().id, remote.tabs[0].id); remote.tabs[0].root = yolo_presentation_layout::Layout::default() .document() .root .clone(); - let replaced = TabCatalog::from_shared(&remote, &reconciled).unwrap(); + let replaced = TabCatalog::from_shared(&remote, &reconciled, reconciled.machine()).unwrap(); assert_eq!( replaced.active().view.layout.focused(), remote.tabs[0].root.panes()[0].id @@ -70,7 +70,7 @@ fn empty_remote_document_has_only_a_local_placeholder_and_invalid_documents_fail let local = TabCatalog::default(); let mut remote = document(&local); remote.tabs.clear(); - let placeholder = TabCatalog::from_shared(&remote, &local).unwrap(); + let placeholder = TabCatalog::from_shared(&remote, &local, local.machine()).unwrap(); assert!( placeholder .active() @@ -82,7 +82,7 @@ fn empty_remote_document_has_only_a_local_placeholder_and_invalid_documents_fail ); assert!(remote.tabs.is_empty()); remote.revision = 0; - assert!(TabCatalog::from_shared(&remote, &local).is_err()); + assert!(TabCatalog::from_shared(&remote, &local, local.machine()).is_err()); } #[test] @@ -91,8 +91,35 @@ fn shared_title_rules_allow_distinct_tabs_with_identical_titles() { local.create("second").unwrap(); let mut remote = document(&local); remote.tabs[1].title = remote.tabs[0].title.clone(); - let imported = TabCatalog::from_shared(&remote, &TabCatalog::default()).unwrap(); - assert_eq!(imported.shared_tabs().unwrap(), remote.tabs); + let entry = local.machine(); + let previous = TabCatalog::new(entry, LocalTabView::default()); + let imported = TabCatalog::from_shared(&remote, &previous, entry).unwrap(); + assert_eq!(imported.shared_tabs(entry).unwrap(), remote.tabs); +} + +#[test] +fn adopting_entry_tabs_preserves_remote_machine_workspaces() { + let entry = yolo_hub_identity::HubId::new(); + let remote_hub = yolo_hub_identity::HubId::new(); + let mut local = TabCatalog::new(entry, LocalTabView::default()); + local.select_machine(remote_hub).unwrap(); + let remote_tab = local.create("remote-work").unwrap(); + local.select_machine(entry).unwrap(); + let mut shared = document(&local); + shared.tabs[0].title = "entry changed".into(); + let adopted = TabCatalog::from_shared(&shared, &local, entry).unwrap(); + let kept = adopted + .entries() + .iter() + .find(|tab| tab.id == remote_tab) + .expect("remote tab survives"); + assert_eq!(kept.machine, remote_hub); + assert_eq!( + adopted.active_by_machine().get(&remote_hub), + Some(&remote_tab) + ); + assert_eq!(adopted.machine(), entry); + assert_eq!(adopted.shared_tabs(entry).unwrap(), shared.tabs); } #[test] diff --git a/core/crates/agent/cli/src/arguments.rs b/core/crates/agent/cli/src/arguments.rs index e7ff03a6..6a9711e0 100644 --- a/core/crates/agent/cli/src/arguments.rs +++ b/core/crates/agent/cli/src/arguments.rs @@ -26,13 +26,13 @@ pub enum Command { #[derive(Debug, Args)] pub struct Options { /// Existing local Hub entry socket; supply together with --hub. - #[arg(long, global = true, requires = HUB_ARGUMENT)] + #[arg(long, global = true, requires = HUB_ARGUMENT, help_heading = "Connection")] pub socket: Option, /// Identity of the Hub accepting this client connection. - #[arg(long, global = true, requires = SOCKET_ARGUMENT)] + #[arg(long, global = true, requires = SOCKET_ARGUMENT, help_heading = "Connection")] pub hub: Option, /// Local profile file; defaults to the current user profile. - #[arg(long, global = true, conflicts_with_all = [SOCKET_ARGUMENT, HUB_ARGUMENT])] + #[arg(long, global = true, conflicts_with_all = [SOCKET_ARGUMENT, HUB_ARGUMENT], help_heading = "Connection")] pub local_profile: Option, #[command(subcommand)] pub command: AgentCommand, @@ -63,11 +63,11 @@ pub enum AgentCommand { #[command(flatten)] output: OutputArgs, }, - /// Deliver a prompt to the agent's native input. + /// Deliver a prompt through the terminal writer; does not wait for the turn. Send(PromptArgs), - /// Deliver a prompt and wait for the agent to finish the turn. + /// Deliver a prompt, then poll until the turn ends or --timeout expires. Ask(AskArgs), - /// Submit one prompt atomically, optionally waiting for the turn to end. + /// Submit one prompt atomically and optionally wait for the turn to end. Prompt(PromptExecArgs), /// Send an interrupt to the agent. Interrupt { diff --git a/core/crates/agent/cli/src/constants.rs b/core/crates/agent/cli/src/constants.rs index c87a61a8..a6bf233b 100644 --- a/core/crates/agent/cli/src/constants.rs +++ b/core/crates/agent/cli/src/constants.rs @@ -5,7 +5,7 @@ pub const PROGRAM: &str = "yolo"; pub const SOCKET_ARGUMENT: &str = "socket"; pub const HUB_ARGUMENT: &str = "hub"; -pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run yolo first"; +pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run `yolo server start` first"; pub const SOCKET_PAIR: &str = "--socket and --hub must be supplied together"; pub const AGENT_START_UNSUPPORTED: &str = "the selected Hub cannot start this provider"; pub const INVALID_RUN_ID: &str = "the run id is not a valid non-nil UUID"; diff --git a/core/crates/agent/cli/src/lib.rs b/core/crates/agent/cli/src/lib.rs index b66d3e8d..589c4420 100644 --- a/core/crates/agent/cli/src/lib.rs +++ b/core/crates/agent/cli/src/lib.rs @@ -6,6 +6,8 @@ mod control; mod control_commands; mod conversation_text; mod current; +mod labels; +mod lifecycle; mod observe; mod outcome; mod queries; @@ -13,13 +15,13 @@ mod receipt; mod reject; mod render; mod runner; -mod labels; mod runs; -mod lifecycle; mod wait; mod wait_command; mod wait_pages; +pub use arguments::{AgentCommand, ListArgs, Options, OutputArgs}; + pub use constants::COMMAND; use clap::Parser; @@ -36,6 +38,18 @@ pub fn selects(arguments: &[OsString]) -> bool { pub fn run_from(arguments: impl IntoIterator) -> anyhow::Result<()> { let arguments = arguments::Arguments::try_parse_from(arguments)?; + let arguments::Command::Agent(options) = arguments.command; + run_options(options) +} + +/// Runs one already parsed agent command with its connection selection. +pub fn run_options(options: arguments::Options) -> anyhow::Result<()> { + run_arguments(arguments::Arguments { + command: arguments::Command::Agent(options), + }) +} + +fn run_arguments(arguments: arguments::Arguments) -> anyhow::Result<()> { let outcome = tokio::runtime::Builder::new_current_thread() .enable_all() .build()? diff --git a/core/crates/agent/system/hooks/installation/src/home.rs b/core/crates/agent/system/hooks/installation/src/home.rs new file mode 100644 index 00000000..54c97b87 --- /dev/null +++ b/core/crates/agent/system/hooks/installation/src/home.rs @@ -0,0 +1,36 @@ +use anyhow::{Context, Result}; +use std::ffi::OsString; +use std::path::PathBuf; +use yolo_hook_installation_contract::HookInstallSpec; + +const HOME_UNAVAILABLE: &str = "provider configuration home is unavailable"; +const USER_HOME_ENV: &str = "HOME"; + +/// Resolve the directory that holds one provider's hook configuration. +/// +/// Precedence: the explicit `--config-home`, the provider's data-home +/// environment variable, then `$HOME/`. The profile's +/// subdirectory is appended last so Grok keeps its `hooks/` directory. +pub fn resolve_home( + spec: &HookInstallSpec, + explicit: Option, + var_os: impl Fn(&str) -> Option, +) -> Result { + let base = explicit + .or_else(|| { + var_os(spec.environment_name) + .filter(|value| !value.is_empty()) + .map(PathBuf::from) + }) + .or_else(|| var_os(USER_HOME_ENV).map(|home| PathBuf::from(home).join(spec.directory_name))) + .context(HOME_UNAVAILABLE)?; + Ok(if spec.subdirectory.is_empty() { + base + } else { + base.join(spec.subdirectory) + }) +} + +#[cfg(test)] +#[path = "../unit_tests/home.rs"] +mod tests; diff --git a/core/crates/agent/system/hooks/installation/src/lib.rs b/core/crates/agent/system/hooks/installation/src/lib.rs index 7d4148e4..44fe69a0 100644 --- a/core/crates/agent/system/hooks/installation/src/lib.rs +++ b/core/crates/agent/system/hooks/installation/src/lib.rs @@ -2,6 +2,7 @@ mod configuration; mod constants; mod directory; mod filesystem; +mod home; mod installation; mod model; mod publication; @@ -12,6 +13,7 @@ mod unix; mod filesystem_test_support; pub use configuration::{callback_command, contains_managed, merge}; +pub use home::resolve_home; pub use installation::{contains_managed_path, inspect, install, remove_legacy_backups}; pub use model::{HookProfile, Installation, IntegrationStatus, Preview}; pub use yolo_hook_installation_contract::{HookEventSpec, HookInstallSpec}; diff --git a/core/crates/agent/system/hooks/installation/unit_tests/home.rs b/core/crates/agent/system/hooks/installation/unit_tests/home.rs new file mode 100644 index 00000000..aa4ff7ea --- /dev/null +++ b/core/crates/agent/system/hooks/installation/unit_tests/home.rs @@ -0,0 +1,62 @@ +use super::*; +use std::collections::BTreeMap; +use yolo_hook_installation_contract::{HookEventSpec, HookInstallSpec}; + +const SPEC: HookInstallSpec = HookInstallSpec { + provider: "demo", + display_name: "Demo", + file_name: "settings.json", + subdirectory: "", + environment_name: "DEMO_HOME", + directory_name: ".demo", + hook_arguments: "provider-hook --provider demo", + managed_hook_commands: &["provider-hook --provider demo"], + events: &[HookEventSpec::new("SessionStart")], + restricted_keys: &[], + disabled_paths: &[], + requires_provider_trust: false, +}; + +fn lookup<'a>( + values: &'a BTreeMap<&'static str, &'static str>, +) -> impl Fn(&str) -> Option + 'a { + move |name| values.get(name).map(|value| OsString::from(*value)) +} + +#[test] +fn explicit_config_home_wins_over_environment() { + let values = BTreeMap::from([("DEMO_HOME", "/env/demo"), ("HOME", "/user")]); + let home = resolve_home(&SPEC, Some("/tmp/explicit".into()), lookup(&values)).unwrap(); + assert_eq!(home, PathBuf::from("/tmp/explicit")); +} + +#[test] +fn provider_environment_wins_over_the_user_home() { + let values = BTreeMap::from([("DEMO_HOME", "/env/demo"), ("HOME", "/user")]); + let home = resolve_home(&SPEC, None, lookup(&values)).unwrap(); + assert_eq!(home, PathBuf::from("/env/demo")); +} + +#[test] +fn empty_environment_falls_back_to_the_user_home() { + let values = BTreeMap::from([("DEMO_HOME", ""), ("HOME", "/user")]); + let home = resolve_home(&SPEC, None, lookup(&values)).unwrap(); + assert_eq!(home, PathBuf::from("/user/.demo")); +} + +#[test] +fn subdirectory_is_appended_after_resolution() { + let nested = HookInstallSpec { + subdirectory: "hooks", + ..SPEC + }; + let values = BTreeMap::from([("HOME", "/user")]); + let home = resolve_home(&nested, None, lookup(&values)).unwrap(); + assert_eq!(home, PathBuf::from("/user/.demo/hooks")); +} + +#[test] +fn missing_every_home_is_an_error() { + let values = BTreeMap::new(); + assert!(resolve_home(&SPEC, None, lookup(&values)).is_err()); +} diff --git a/core/crates/application/command/BUILD.bazel b/core/crates/application/command/BUILD.bazel new file mode 100644 index 00000000..37eb82b4 --- /dev/null +++ b/core/crates/application/command/BUILD.bazel @@ -0,0 +1,42 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "//core/crates/agent/cli:yolo_agent_cli", + "//core/crates/application/protocol/cli:yolo_protocol_cli", + "//core/crates/application/trusted-plugin/cli:yolo_trusted_plugin_cli", + "//core/crates/application/upgrade/cli:yolo_upgrade_cli", + "//core/crates/application/workspace/cli:yolo_workspace_cli", + "//core/crates/conversation/cli:yolo_conversation_cli", + "//core/crates/hub/client:yolo_hub_client", + "//core/crates/hub/identity:yolo_hub_identity", + "@crates//:anyhow", + "@crates//:clap", + "@crates//:uuid", +] + +filegroup( + name = "architecture_check_data", + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], +) + +rust_library( + name = "yolo_command_cli", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_command_cli", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) + +rust_test( + name = "unit_tests", + srcs = glob(["src/**/*.rs", "unit_tests/**/*.rs"]), + crate_name = "yolo_command_cli_unit_tests", + crate_root = "src/lib.rs", + edition = "2024", + deps = _DEPS, +) diff --git a/core/crates/application/command/src/connection.rs b/core/crates/application/command/src/connection.rs new file mode 100644 index 00000000..21622f64 --- /dev/null +++ b/core/crates/application/command/src/connection.rs @@ -0,0 +1,70 @@ +use crate::constants::*; +use anyhow::{Result, ensure}; +use clap::Args; +use std::path::PathBuf; +use uuid::Uuid; +use yolo_hub_client::HubEndpoint; +use yolo_hub_identity::HubId; + +#[derive(Debug, Default, Args)] +pub struct ConnectionOptions { + /// Existing local Hub entry socket; supply together with --hub. + #[arg(long, global = true, help_heading = "Connection", conflicts_with = ENDPOINT_ID)] + pub socket: Option, + /// Hub entry endpoint (local:); supply together with --hub. + #[arg(long, global = true, help_heading = "Connection", conflicts_with = SOCKET_ID)] + pub endpoint: Option, + /// Identity of the Hub accepting this client connection. + #[arg(long, global = true, help_heading = "Connection", conflicts_with = LOCAL_PROFILE_ID)] + pub hub: Option, + /// Local profile file; defaults to the current user profile. + #[arg(long, global = true, help_heading = "Connection", conflicts_with_all = [HUB_ID, SOCKET_ID, ENDPOINT_ID])] + pub local_profile: Option, + /// Remote profile file; requires --account-key and --client-key. + #[arg(long, global = true, help_heading = "Connection", requires_all = [ACCOUNT_KEY_ID, CLIENT_KEY_ID], conflicts_with_all = [HUB_ID, SOCKET_ID, ENDPOINT_ID, LOCAL_PROFILE_ID])] + pub remote_profile: Option, + /// Account key paired with --remote-profile. + #[arg(long, global = true, help_heading = "Connection", requires = REMOTE_PROFILE_ID)] + pub account_key: Option, + /// Client key paired with --remote-profile. + #[arg(long, global = true, help_heading = "Connection", requires = REMOTE_PROFILE_ID)] + pub client_key: Option, + /// Attach a shared layout on the selected Hub; keyboard focus stays private. + #[arg(long, global = true, help_heading = "Layout")] + pub layout: Option, +} + +impl ConnectionOptions { + pub(crate) fn validate(&self) -> Result<()> { + ensure!( + self.hub.is_some() || (self.socket.is_none() && self.endpoint.is_none()), + "{}", + HUB_REQUIRES_CONNECTION + ); + ensure!( + self.hub.is_none() || self.socket.is_some() || self.endpoint.is_some(), + "{}", + CONNECTION_REQUIRES_HUB + ); + Ok(()) + } + + pub(crate) fn has_any(&self) -> bool { + self.socket.is_some() + || self.endpoint.is_some() + || self.hub.is_some() + || self.local_profile.is_some() + || self.remote_profile.is_some() + || self.account_key.is_some() + || self.client_key.is_some() + || self.layout.is_some() + } + + pub(crate) fn local_only(&self) -> bool { + self.endpoint.is_none() + && self.remote_profile.is_none() + && self.account_key.is_none() + && self.client_key.is_none() + && self.layout.is_none() + } +} diff --git a/core/crates/application/command/src/constants.rs b/core/crates/application/command/src/constants.rs new file mode 100644 index 00000000..9d7664b3 --- /dev/null +++ b/core/crates/application/command/src/constants.rs @@ -0,0 +1,13 @@ +pub(crate) const SOCKET_ID: &str = "socket"; +pub(crate) const ENDPOINT_ID: &str = "endpoint"; +pub(crate) const HUB_ID: &str = "hub"; +pub(crate) const LOCAL_PROFILE_ID: &str = "local_profile"; +pub(crate) const REMOTE_PROFILE_ID: &str = "remote_profile"; +pub(crate) const ACCOUNT_KEY_ID: &str = "account_key"; +pub(crate) const CLIENT_KEY_ID: &str = "client_key"; + +pub(crate) const LOCAL_CONNECTION_ONLY: &str = + "`{command}` supports only --socket, --hub and --local-profile"; +pub(crate) const NO_CONNECTION: &str = "`{command}` does not use Hub connection options"; +pub const HUB_REQUIRES_CONNECTION: &str = "--socket and --endpoint require --hub"; +pub const CONNECTION_REQUIRES_HUB: &str = "--hub requires --socket or --endpoint"; diff --git a/core/crates/application/command/src/dispatch.rs b/core/crates/application/command/src/dispatch.rs new file mode 100644 index 00000000..545932cb --- /dev/null +++ b/core/crates/application/command/src/dispatch.rs @@ -0,0 +1,91 @@ +use crate::connection::ConnectionOptions; +use crate::constants::*; +use crate::tree::Command; +use anyhow::{Result, ensure}; + +pub(crate) fn run(connection: ConnectionOptions, command: Command) -> Result<()> { + match command { + Command::Agent { command } => { + local_only(&connection, "agent")?; + yolo_agent_cli::run_options(yolo_agent_cli::Options { + socket: connection.socket, + hub: connection.hub, + local_profile: connection.local_profile, + command, + }) + } + Command::Conversation { action } => { + local_only(&connection, "conversation")?; + yolo_conversation_cli::run(yolo_conversation_cli::Options { + socket: connection.socket, + hub: connection.hub, + local_profile: connection.local_profile, + action, + }) + } + Command::Workspace { command } => { + local_only(&connection, "workspace")?; + yolo_workspace_cli::run_workspace(yolo_workspace_cli::Options { + socket: connection.socket, + hub: connection.hub, + local_profile: connection.local_profile, + command, + }) + } + Command::Worktree { command } => { + local_only(&connection, "worktree")?; + yolo_workspace_cli::run_worktree(yolo_workspace_cli::WorktreeOptions { + socket: connection.socket, + hub: connection.hub, + local_profile: connection.local_profile, + command, + }) + } + Command::Protocol { command } => { + no_connection(&connection, "protocol")?; + yolo_protocol_cli::run_command(command) + } + Command::Plugin { command } => { + no_connection(&connection, "plugin")?; + yolo_trusted_plugin_cli::run_plugin(command) + } + Command::Update(args) => { + local_only(&connection, "update")?; + yolo_upgrade_cli::run(yolo_upgrade_cli::Options { + socket: connection.socket, + hub: connection.hub, + state: args.state, + service_units: args.service_units, + json: args.json, + channel: args.channel, + check: args.check, + admit: args.admit, + restart: args.restart, + no_restart: args.no_restart, + install_release: args.install_release, + validate_state: args.validate_state, + local_profile: connection.local_profile, + repository: args.repository, + }) + } + } +} + +fn local_only(connection: &ConnectionOptions, command: &str) -> Result<()> { + connection.validate()?; + ensure!( + connection.local_only(), + "{}", + LOCAL_CONNECTION_ONLY.replace("{command}", command) + ); + Ok(()) +} + +fn no_connection(connection: &ConnectionOptions, command: &str) -> Result<()> { + ensure!( + !connection.has_any(), + "{}", + NO_CONNECTION.replace("{command}", command) + ); + Ok(()) +} diff --git a/core/crates/application/command/src/lib.rs b/core/crates/application/command/src/lib.rs new file mode 100644 index 00000000..0446172a --- /dev/null +++ b/core/crates/application/command/src/lib.rs @@ -0,0 +1,21 @@ +mod connection; +mod constants; +mod dispatch; +mod tree; + +pub use connection::ConnectionOptions; +pub use constants::{CONNECTION_REQUIRES_HUB, HUB_REQUIRES_CONNECTION}; +pub use tree::{Command, UpdateArgs}; +pub use yolo_agent_cli::AgentCommand; +pub use yolo_conversation_cli::Action as ConversationAction; +pub use yolo_protocol_cli::ProtocolCommand; +pub use yolo_trusted_plugin_cli::PluginCommand; +pub use yolo_workspace_cli::{WorktreeCommand, WorkspaceCommand}; + +pub fn run(connection: ConnectionOptions, command: Command) -> anyhow::Result<()> { + dispatch::run(connection, command) +} + +#[cfg(test)] +#[path = "../unit_tests/lib.rs"] +mod unit_tests; diff --git a/core/crates/application/command/src/tree.rs b/core/crates/application/command/src/tree.rs new file mode 100644 index 00000000..3ea0dd78 --- /dev/null +++ b/core/crates/application/command/src/tree.rs @@ -0,0 +1,72 @@ +use clap::{Args, Subcommand}; +use std::path::PathBuf; + +#[derive(Debug, Subcommand)] +pub enum Command { + /// Inspect and control a specific agent through the public Hub protocol. + Agent { + #[command(subcommand)] + command: yolo_agent_cli::AgentCommand, + }, + /// Read native agent conversations through the public Hub protocol. + Conversation { + #[command(subcommand)] + action: yolo_conversation_cli::Action, + }, + /// Manage durable workspace metadata through the public Hub. + Workspace { + #[command(subcommand)] + command: yolo_workspace_cli::WorkspaceCommand, + }, + /// Manage git worktrees through the public Hub. + Worktree { + #[command(subcommand)] + command: yolo_workspace_cli::WorktreeCommand, + }, + /// Inspect the public Yolo protocol contract. + Protocol { + #[command(subcommand)] + command: yolo_protocol_cli::ProtocolCommand, + }, + /// Manage trusted user-level plugins. + Plugin { + #[command(subcommand)] + command: yolo_trusted_plugin_cli::PluginCommand, + }, + /// Check, install and activate Yolo releases. + Update(UpdateArgs), +} + +#[derive(Debug, Args)] +pub struct UpdateArgs { + /// Print one JSON document. + #[arg(long)] + pub json: bool, + /// Release channel to select. + #[arg(long, value_enum, default_value = "stable")] + pub channel: yolo_upgrade_cli::Channel, + /// Check the latest release without installing or restarting. + #[arg(long, alias = "check-release", conflicts_with_all = ["install_release", "restart", "admit", "no_restart", "validate_state"])] + pub check: bool, + /// Admit the already installed image without checking releases. + #[arg(long, conflicts_with_all = ["install_release", "restart", "check", "no_restart", "validate_state"])] + pub admit: bool, + /// Restart the local Host and Hub, then admit the installed image. + #[arg(long, conflicts_with_all = ["install_release", "check", "admit", "no_restart", "validate_state"])] + pub restart: bool, + /// Install a newer release without restarting; only with the default update. + #[arg(long, conflicts_with_all = ["install_release", "check", "admit", "restart", "validate_state"])] + pub no_restart: bool, + /// Legacy install-only mode. + #[arg(long, hide = true, conflicts_with_all = ["check", "admit", "restart", "no_restart", "validate_state"])] + pub install_release: bool, + /// Validate the local Hub state contract with this image without changing it. + #[arg(long, hide = true, conflicts_with_all = ["install_release", "check", "admit", "restart", "no_restart"])] + pub validate_state: bool, + #[arg(long, hide = true, value_name = "PATH")] + pub state: Option, + #[arg(long = "service-unit", hide = true, value_name = "PATH")] + pub service_units: Vec, + #[arg(long, hide = true)] + pub repository: Option, +} diff --git a/core/crates/application/command/unit_tests/constants.rs b/core/crates/application/command/unit_tests/constants.rs new file mode 100644 index 00000000..63e814d2 --- /dev/null +++ b/core/crates/application/command/unit_tests/constants.rs @@ -0,0 +1,12 @@ +pub const AGENT: &str = "agent"; +pub const LIST: &str = "list"; +pub const PLUGIN: &str = "plugin"; +pub const LOCAL_PROFILE: &str = "--local-profile"; +pub const ENDPOINT: &str = "--endpoint"; +pub const HUB: &str = "--hub"; +pub const PROFILE: &str = "/tmp/yolo-profile.json"; +pub const HUB_ID: &str = "11111111-1111-4111-8111-111111111111"; +pub const SOCKET: &str = "--socket"; +pub const SOCKET_PATH: &str = "/tmp/yolo.sock"; +pub const NO_CONNECTION: &str = "does not use Hub connection options"; +pub const LOCAL_ONLY: &str = "supports only --socket, --hub and --local-profile"; diff --git a/core/crates/application/command/unit_tests/lib.rs b/core/crates/application/command/unit_tests/lib.rs new file mode 100644 index 00000000..6bc79906 --- /dev/null +++ b/core/crates/application/command/unit_tests/lib.rs @@ -0,0 +1,46 @@ +use super::{Command, ConnectionOptions}; +use constants::*; +use yolo_agent_cli::{AgentCommand, ListArgs, OutputArgs}; +use yolo_hub_client::HubEndpoint; +use yolo_hub_identity::HubId; + +#[path = "constants.rs"] +mod constants; + +fn connection() -> ConnectionOptions { + ConnectionOptions { + socket: Some(SOCKET_PATH.into()), + hub: Some(HubId::new()), + ..ConnectionOptions::default() + } +} + +#[test] +fn commands_reject_connection_options_they_do_not_use() { + let error = super::run( + connection(), + Command::Plugin { + command: yolo_trusted_plugin_cli::PluginCommand::List { json: true }, + }, + ) + .unwrap_err() + .to_string(); + assert!(error.contains(NO_CONNECTION), "{error}"); +} + +#[test] +fn local_commands_reject_remote_connection_options() { + let connection = ConnectionOptions { + endpoint: Some(HubEndpoint::local(SOCKET_PATH)), + hub: Some(HubId::new()), + ..ConnectionOptions::default() + }; + let command = Command::Agent { + command: AgentCommand::List(ListArgs { + all: false, + output: OutputArgs { json: true }, + }), + }; + let error = super::run(connection, command).unwrap_err().to_string(); + assert!(error.contains(LOCAL_ONLY), "{error}"); +} diff --git a/core/crates/application/host/cli/src/hook_installation.rs b/core/crates/application/host/cli/src/hook_installation.rs index ae76a38e..b8eca0ec 100644 --- a/core/crates/application/host/cli/src/hook_installation.rs +++ b/core/crates/application/host/cli/src/hook_installation.rs @@ -3,13 +3,11 @@ use clap::{Args, Subcommand}; use std::path::PathBuf; use yolo_hook_installation::{ HookInstallSpec, HookProfile, IntegrationStatus, contains_managed_path, inspect, - install as install_hooks, remove_legacy_backups, + install as install_hooks, remove_legacy_backups, resolve_home, }; use yolo_agent_providers::{builtin_install_spec, builtin_install_specs}; -const HOME_UNAVAILABLE: &str = "provider configuration home is unavailable"; const PROVIDER_UNAVAILABLE: &str = "provider has no hook installation profile"; -const USER_HOME_ENV: &str = "HOME"; #[derive(Debug, Args)] pub(super) struct Options { @@ -104,22 +102,7 @@ fn provider(token: &str) -> Result<&'static HookInstallSpec> { } fn home(provider: &HookInstallSpec, explicit: Option) -> Result { - let path = explicit - .or_else(|| { - std::env::var_os(provider.environment_name) - .filter(|value| !value.is_empty()) - .map(PathBuf::from) - }) - .or_else(|| { - std::env::var_os(USER_HOME_ENV) - .map(|home| PathBuf::from(home).join(provider.directory_name)) - }) - .context(HOME_UNAVAILABLE)?; - let path = if provider.subdirectory.is_empty() { - path - } else { - path.join(provider.subdirectory) - }; + let path = resolve_home(provider, explicit, |name| std::env::var_os(name))?; yolo_runtime::paths::validation::validate_absolute(&path)?; Ok(path) } diff --git a/core/crates/application/launch/contract/src/argument_constants.rs b/core/crates/application/launch/contract/src/argument_constants.rs index 90767e76..8445622a 100644 --- a/core/crates/application/launch/contract/src/argument_constants.rs +++ b/core/crates/application/launch/contract/src/argument_constants.rs @@ -2,3 +2,6 @@ pub const HUB_STATE_ARGUMENT: &str = "--state"; pub const HOST_HOME_ARGUMENT: &str = "--host-home"; + +/// Terminates option parsing. +pub const END_OF_OPTIONS: &str = "--"; diff --git a/core/crates/application/launch/contract/src/lib.rs b/core/crates/application/launch/contract/src/lib.rs index c26e97ce..90a6b94f 100644 --- a/core/crates/application/launch/contract/src/lib.rs +++ b/core/crates/application/launch/contract/src/lib.rs @@ -1,10 +1,12 @@ //! Pure executable/argv contract. No services, process spawning or environment reads. mod argument_constants; mod role; +mod routing; mod selection; include!("generated/process_role_constants.rs"); pub use argument_constants::{HOST_HOME_ARGUMENT, HUB_STATE_ARGUMENT}; pub use role::ProcessRole; +pub use routing::{Routing, command_index}; pub use selection::{Invocation, SelectionError}; diff --git a/core/crates/application/launch/contract/src/routing.rs b/core/crates/application/launch/contract/src/routing.rs new file mode 100644 index 00000000..ca982854 --- /dev/null +++ b/core/crates/application/launch/contract/src/routing.rs @@ -0,0 +1,34 @@ +use crate::argument_constants::END_OF_OPTIONS; +use std::ffi::OsString; + +/// Command tokens and root options used to recognize an auxiliary invocation. +pub struct Routing<'a> { + pub commands: &'a [&'a str], + pub value_options: &'a [&'a str], + pub terminal_options: &'a [&'a str], +} + +/// Returns the position of a recognized command when root options precede it. +pub fn command_index(arguments: &[OsString], routing: &Routing<'_>) -> Option { + let first = arguments.get(1).and_then(|argument| argument.to_str())?; + if routing.commands.contains(&first) || !first.starts_with('-') { + return None; + } + let mut index = 1; + while index < arguments.len() { + let argument = arguments[index].to_str()?; + if routing.terminal_options.contains(&argument) || argument == END_OF_OPTIONS { + return None; + } + if routing.value_options.contains(&argument) { + index += 2; + continue; + } + if argument.starts_with('-') { + index += 1; + continue; + } + return routing.commands.contains(&argument).then_some(index); + } + None +} diff --git a/core/crates/application/launch/contract/unit_tests/constants.rs b/core/crates/application/launch/contract/unit_tests/constants.rs index bcf47f23..3edc54c0 100644 --- a/core/crates/application/launch/contract/unit_tests/constants.rs +++ b/core/crates/application/launch/contract/unit_tests/constants.rs @@ -4,3 +4,9 @@ pub const ARGUMENT: &str = "--state"; pub const SPACED_PATH: &str = "/a directory/state"; pub const INVALID_UTF8: &[u8] = &[0xff]; pub const ARGV0: &str = "/some installation/yolo"; +pub const AGENT: &str = "agent"; +pub const UPDATE: &str = "update"; +pub const LIST: &str = "list"; +pub const LOCAL_PROFILE: &str = "--local-profile"; +pub const PROFILE: &str = "profile.json"; +pub const HELP: &str = "--help"; diff --git a/core/crates/application/launch/contract/unit_tests/lib.rs b/core/crates/application/launch/contract/unit_tests/lib.rs index affaada8..0e799e0b 100644 --- a/core/crates/application/launch/contract/unit_tests/lib.rs +++ b/core/crates/application/launch/contract/unit_tests/lib.rs @@ -69,3 +69,29 @@ fn default_tui_selection_ignores_program_basename_and_never_consumes_user_argume ); } } + +#[test] +fn command_index_finds_a_command_after_root_options() { + use yolo_launch_contract::{Routing, command_index}; + const COMMANDS: &[&str] = &[AGENT, UPDATE]; + const VALUES: &[&str] = &[LOCAL_PROFILE]; + const TERMINAL: &[&str] = &[HELP]; + let routing = Routing { + commands: COMMANDS, + value_options: VALUES, + terminal_options: TERMINAL, + }; + let index = |arguments: &[&str]| { + command_index( + &arguments.iter().map(OsString::from).collect::>(), + &routing, + ) + }; + assert_eq!( + index(&[ARGV0, LOCAL_PROFILE, PROFILE, AGENT, LIST]), + Some(3) + ); + assert_eq!(index(&[ARGV0, LOCAL_PROFILE, AGENT, LIST]), None); + assert_eq!(index(&[ARGV0, HELP, AGENT]), None); + assert_eq!(index(&[ARGV0, AGENT, LOCAL_PROFILE, PROFILE, LIST]), None); +} diff --git a/core/crates/application/local/lifecycle/BUILD.bazel b/core/crates/application/local/lifecycle/BUILD.bazel new file mode 100644 index 00000000..ac19af35 --- /dev/null +++ b/core/crates/application/local/lifecycle/BUILD.bazel @@ -0,0 +1,49 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "//core/crates/application/local/profile:yolo_local_profile", + "//core/crates/build_image:yolo_build_image", + "//core/crates/hub/client:yolo_hub_client", + "//core/crates/hub/identity:yolo_hub_identity", + "//core/crates/hub/local/startup:yolo_hub_local_startup", + "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", + "//core/crates/hub/state:yolo_hub_state", + "//core/crates/persistence/paths:yolo_paths", + "//core/crates/transport/local/unix:yolo_local_unix", + "@crates//:anyhow", + "@crates//:libc", + "@crates//:serde", + "@crates//:sha2", + "@crates//:tokio", +] + +filegroup( + name = "architecture_check_data", + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], +) + +rust_library( + name = "yolo_local_lifecycle", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_local_lifecycle", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) + +rust_test( + name = "unit_tests", + srcs = glob(["unit_tests/**/*.rs"]), + crate_name = "yolo_local_lifecycle_unit_tests", + crate_root = "unit_tests/lib.rs", + edition = "2024", + deps = _DEPS + [ + ":yolo_local_lifecycle", + "@crates//:serde_json", + "@crates//:tempfile", + ], +) diff --git a/core/crates/application/local/lifecycle/src/constants.rs b/core/crates/application/local/lifecycle/src/constants.rs new file mode 100644 index 00000000..53d33571 --- /dev/null +++ b/core/crates/application/local/lifecycle/src/constants.rs @@ -0,0 +1,19 @@ +use std::time::Duration; + +pub(crate) const STATUS_TIMEOUT: Duration = Duration::from_secs(5); +pub(crate) const STOP_TIMEOUT: Duration = Duration::from_secs(10); +pub(crate) const START_TIMEOUT: Duration = Duration::from_secs(30); +pub(crate) const STOP_POLL: Duration = Duration::from_millis(50); +pub(crate) const STOP_TIMED_OUT: &str = + "local service did not exit after SIGTERM; inspect the exact PID before retrying"; +pub(crate) const SIGNAL_FAILED: &str = "could not signal the local service process"; +pub(crate) const STOPPED_NOT_STARTED: &str = + "local services stopped but the new Host/Hub did not start"; +pub(crate) const STATE_INCOMPATIBLE: &str = "local Hub state is incompatible with the installed image; back it up or migrate before restarting"; +pub(crate) const HUB_SERVICE: &str = "hub"; +pub(crate) const HOST_SERVICE: &str = "host"; +pub(crate) const CONNECT_TIMEOUT: &str = "local Hub connection timed out"; +pub(crate) const CONNECT_FAILED: &str = "local Hub connection failed"; +pub(crate) const CONNECT_CREDENTIALS: &str = "local Hub socket credentials are unavailable"; +pub(crate) const CONNECT_UID_MISMATCH: &str = "local Hub socket is owned by another user"; +pub(crate) const CONNECT_PID_UNAVAILABLE: &str = "local Hub socket peer process is unavailable"; diff --git a/core/crates/application/local/lifecycle/src/control.rs b/core/crates/application/local/lifecycle/src/control.rs new file mode 100644 index 00000000..1a30d421 --- /dev/null +++ b/core/crates/application/local/lifecycle/src/control.rs @@ -0,0 +1,79 @@ +use crate::constants::*; +use crate::process::StopReport; +use anyhow::{Context, Result, ensure}; +use std::path::Path; +use tokio::time::Instant; +use yolo_hub_local_startup::Launch; +use yolo_hub_state::{HubState, validate_runtime_contract}; +use yolo_local_profile::LocalProfile; + +pub async fn stop(profile: &LocalProfile) -> Result { + require_detached_services(profile).await?; + stop_detached(profile).await +} + +async fn require_detached_services(profile: &LocalProfile) -> Result<()> { + let units = crate::service_units(profile).await?; + ensure!( + units.is_empty(), + "the selected profile is managed by systemd ({}); use those units to stop or activate it", + units + .iter() + .map(|unit| unit.name.as_str()) + .collect::>() + .join(", ") + ); + Ok(()) +} + +async fn stop_detached(profile: &LocalProfile) -> Result { + let hub = crate::process::stop_socket( + &HubState::socket_path_for(&profile.state)?, + HUB_SERVICE, + Instant::now() + STOP_TIMEOUT, + ) + .await?; + let host = crate::process::stop_socket( + &yolo_paths::Paths::new(&profile.host_home).host_socket()?, + HOST_SERVICE, + Instant::now() + STOP_TIMEOUT, + ) + .await?; + Ok(StopReport { + services: vec![hub, host], + }) +} + +pub async fn start(profile: &LocalProfile, executable: &Path) -> Result<()> { + let launch = Launch { + state: profile.state.clone(), + host_home: profile.host_home.clone(), + host_executable: executable.to_path_buf(), + hub_executable: executable.to_path_buf(), + }; + yolo_hub_local_startup::ensure_running(launch, Instant::now() + START_TIMEOUT).await?; + Ok(()) +} + +/// Checks the local state contract with the constants compiled into this +/// process. Callers that activate a different image must run this check with +/// that image instead; see `yolo_local_lifecycle::restart`. +pub fn validate_state(profile: &LocalProfile) -> Result<()> { + if profile.state.exists() { + validate_runtime_contract(&profile.state).context(STATE_INCOMPATIBLE)?; + } + Ok(()) +} + +/// Stops and starts the local services. The caller owns the state-contract +/// preflight because only the image that will run may decide compatibility: an +/// older updater must validate with its installed candidate, not itself. +pub async fn restart(profile: &LocalProfile, executable: &Path) -> Result { + require_detached_services(profile).await?; + yolo_hub_local_startup::require_host_custody()?; + let report = stop_detached(profile).await?; + start(profile, executable) + .await + .context(STOPPED_NOT_STARTED)?; + Ok(report) +} diff --git a/core/crates/application/local/lifecycle/src/image.rs b/core/crates/application/local/lifecycle/src/image.rs new file mode 100644 index 00000000..eaf1599f --- /dev/null +++ b/core/crates/application/local/lifecycle/src/image.rs @@ -0,0 +1,52 @@ +use anyhow::{Context, Result, ensure}; +use sha2::{Digest, Sha256}; +use std::{fs, io::Read, path::Path}; +use yolo_build_image::{BuildImage, MAX_EXECUTABLE_IMAGE_BYTES}; + +const INVALID_EXECUTABLE: &str = "service executable is not a regular file"; +const INVALID_EXECUTABLE_SIZE: &str = "service executable size is invalid"; +const EXECUTABLE_CHANGED: &str = "service executable changed while hashing"; +const HEX_CHARS_PER_BYTE: usize = 2; + +pub fn image(path: &Path) -> Result { + let metadata = fs::symlink_metadata(path)?; + ensure!(metadata.file_type().is_file(), INVALID_EXECUTABLE); + ensure!( + metadata.len() > 0 && metadata.len() <= MAX_EXECUTABLE_IMAGE_BYTES, + INVALID_EXECUTABLE_SIZE + ); + let mut file = fs::File::open(path)?; + let mut digest = Sha256::new(); + let copied = std::io::copy( + &mut file.by_ref().take(metadata.len() + 1), + &mut DigestWriter(&mut digest), + ) + .with_context(|| EXECUTABLE_CHANGED.to_owned())?; + ensure!(copied == metadata.len(), EXECUTABLE_CHANGED); + let after = file.metadata()?; + ensure!(after.len() == metadata.len(), EXECUTABLE_CHANGED); + BuildImage::new(hex(digest.finalize()), metadata.len()).map_err(Into::into) +} + +fn hex(bytes: impl AsRef<[u8]>) -> String { + const DIGITS: &[u8; 16] = b"0123456789abcdef"; + bytes.as_ref().iter().fold( + String::with_capacity(bytes.as_ref().len() * HEX_CHARS_PER_BYTE), + |mut result, byte| { + result.push(DIGITS[(byte >> 4) as usize] as char); + result.push(DIGITS[(byte & 0x0f) as usize] as char); + result + }, + ) +} + +struct DigestWriter<'a>(&'a mut Sha256); +impl std::io::Write for DigestWriter<'_> { + fn write(&mut self, bytes: &[u8]) -> std::io::Result { + self.0.update(bytes); + Ok(bytes.len()) + } + fn flush(&mut self) -> std::io::Result<()> { + Ok(()) + } +} diff --git a/core/crates/application/local/lifecycle/src/lib.rs b/core/crates/application/local/lifecycle/src/lib.rs new file mode 100644 index 00000000..ea493444 --- /dev/null +++ b/core/crates/application/local/lifecycle/src/lib.rs @@ -0,0 +1,13 @@ +//! Local Host and Hub lifecycle: status, start, stop and restart. +mod constants; +mod control; +mod image; +mod process; +mod status; +mod supervision; + +pub use control::{restart, start, stop, validate_state}; +pub use image::image; +pub use process::{ServiceStop, StopReport, service_pid}; +pub use status::{HostStatus, HubStatus, StatusReport, status}; +pub use supervision::{ServiceUnit, service_units}; diff --git a/core/crates/application/local/lifecycle/src/process.rs b/core/crates/application/local/lifecycle/src/process.rs new file mode 100644 index 00000000..8b3d5d8c --- /dev/null +++ b/core/crates/application/local/lifecycle/src/process.rs @@ -0,0 +1,82 @@ +use crate::constants::*; +use anyhow::{Context, Result, ensure}; +use serde::Serialize; +use std::path::Path; +use tokio::time::{Instant, sleep}; + +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct ServiceStop { + pub service: &'static str, + pub pid: Option, + pub stopped: bool, +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct StopReport { + pub services: Vec, +} + +/// Resolves the process that owns a private service socket. The peer must be +/// the current user; no process-name matching is involved. +pub async fn service_pid(path: &Path) -> Option { + let stream = tokio::net::UnixStream::connect(path).await.ok()?; + let credentials = stream.peer_cred().ok()?; + // SAFETY: geteuid has no preconditions and only reads process credentials. + if credentials.uid() != unsafe { libc::geteuid() } { + return None; + } + credentials.pid().and_then(|pid| u32::try_from(pid).ok()) +} + +pub(crate) async fn stop_socket( + path: &Path, + service: &'static str, + deadline: Instant, +) -> Result { + let Some(pid) = service_pid(path).await else { + return Ok(ServiceStop { + service, + pid: None, + stopped: false, + }); + }; + signal_term(pid)?; + ensure!(wait_exit(pid, deadline).await, STOP_TIMED_OUT); + Ok(ServiceStop { + service, + pid: Some(pid), + stopped: true, + }) +} + +fn signal_term(pid: u32) -> Result<()> { + let result = unsafe { libc::kill(pid as libc::pid_t, libc::SIGTERM) }; + if result == 0 { + return Ok(()); + } + let error = std::io::Error::last_os_error(); + if error.raw_os_error() == Some(libc::ESRCH) { + return Ok(()); + } + Err(error).with_context(|| SIGNAL_FAILED) +} + +async fn wait_exit(pid: u32, deadline: Instant) -> bool { + loop { + if !is_alive(pid) { + return true; + } + if Instant::now() >= deadline { + return false; + } + sleep(STOP_POLL).await; + } +} + +fn is_alive(pid: u32) -> bool { + let result = unsafe { libc::kill(pid as libc::pid_t, 0) }; + if result == 0 { + return true; + } + std::io::Error::last_os_error().raw_os_error() != Some(libc::ESRCH) +} diff --git a/core/crates/application/local/lifecycle/src/status.rs b/core/crates/application/local/lifecycle/src/status.rs new file mode 100644 index 00000000..e6efa4d7 --- /dev/null +++ b/core/crates/application/local/lifecycle/src/status.rs @@ -0,0 +1,139 @@ +use crate::constants::*; +use crate::image; +use anyhow::{Context, Result, ensure}; +use serde::Serialize; +use std::path::{Path, PathBuf}; +use tokio::time::{Instant, timeout_at}; +use yolo_build_image::BuildImage; +use yolo_hub_client::Client; +use yolo_hub_identity::HubId; +use yolo_hub_rpc_protocol::{Command, HUB_PROTOCOL_VERSION, Reply}; +use yolo_hub_state::{HubState, validate_runtime_contract}; +use yolo_local_profile::LocalProfile; +use yolo_local_unix::LocalUnixStream; + +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct StatusReport { + pub client_version: String, + pub client_image: BuildImage, + pub hub: HubStatus, + pub host: HostStatus, + pub file_helper: Option, + pub live_images_consistent: bool, + pub restart_needed: bool, + pub state_contract: bool, +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct HubStatus { + pub running: bool, + pub pid: Option, + pub socket: PathBuf, + pub reachable: bool, + pub protocol: Option, + pub expected_protocol: u16, + pub image: Option, +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize)] +pub struct HostStatus { + pub running: bool, + pub pid: Option, + pub socket: PathBuf, + pub image: Option, +} + +pub async fn status(profile: &LocalProfile) -> Result { + let client_image = image(&std::env::current_exe()?.canonicalize()?)?; + let hub_socket = HubState::socket_path_for(&profile.state)?; + let host_socket = yolo_paths::Paths::new(&profile.host_home).host_socket()?; + let mut hub_pid = None; + let host_pid = crate::process::service_pid(&host_socket).await; + let mut hub_reachable = false; + let mut protocol = None; + let mut hub_image = None; + let mut host_image = None; + let mut file_helper = None; + if let Ok(hub_id) = HubState::hub_id_for(&profile.state, &profile.host_home) { + // Each probe owns its own budget: resolving the identity and inspecting + // the Host can consume most of a shared one before the socket is tried. + match connect_hub(&hub_socket, hub_id, Instant::now() + STATUS_TIMEOUT).await { + Ok((mut client, pid)) => { + hub_pid = Some(pid); + hub_reachable = true; + protocol = Some(client.hello().version); + if let Ok(Reply::Build { build }) = client + .call( + hub_id, + Command::GetBuild {}, + Instant::now() + STATUS_TIMEOUT, + ) + .await + { + hub_image = Some(build.hub); + host_image = Some(build.host); + file_helper = Some(build.file_helper); + } + } + Err(_) => hub_pid = crate::process::service_pid(&hub_socket).await, + } + } else { + hub_pid = crate::process::service_pid(&hub_socket).await; + } + let consistent = hub_image.is_some() && hub_image == host_image && host_image == file_helper; + let mismatch = |live: Option<&BuildImage>| live != Some(&client_image); + let restart_needed = (hub_pid.is_some() && mismatch(hub_image.as_ref())) + || (host_pid.is_some() && mismatch(host_image.as_ref())) + || file_helper + .as_ref() + .is_some_and(|image| image != &client_image) + || protocol.is_some_and(|version| version != HUB_PROTOCOL_VERSION); + Ok(StatusReport { + client_version: env!("CARGO_PKG_VERSION").to_owned(), + client_image, + hub: HubStatus { + running: hub_pid.is_some(), + pid: hub_pid, + socket: hub_socket, + reachable: hub_reachable, + protocol, + expected_protocol: HUB_PROTOCOL_VERSION, + image: hub_image, + }, + host: HostStatus { + running: host_pid.is_some(), + pid: host_pid, + socket: host_socket, + image: host_image, + }, + file_helper, + live_images_consistent: consistent, + restart_needed, + state_contract: validate_runtime_contract(&profile.state).is_ok(), + }) +} + +async fn connect_hub( + path: &Path, + hub_id: HubId, + deadline: Instant, +) -> Result<(Client, u32)> { + let stream = timeout_at(deadline, LocalUnixStream::connect(path)) + .await + .context(CONNECT_TIMEOUT)? + .context(CONNECT_FAILED)?; + let credentials = stream.peer_cred().context(CONNECT_CREDENTIALS)?; + // SAFETY: geteuid has no preconditions and only reads process credentials. + ensure!( + credentials.uid() == unsafe { libc::geteuid() }, + CONNECT_UID_MISMATCH + ); + let pid = credentials + .pid() + .and_then(|pid| u32::try_from(pid).ok()) + .context(CONNECT_PID_UNAVAILABLE)?; + let client = Client::open(stream, hub_id, deadline) + .await + .context(CONNECT_FAILED)?; + Ok((client, pid)) +} diff --git a/core/crates/application/local/lifecycle/src/supervision.rs b/core/crates/application/local/lifecycle/src/supervision.rs new file mode 100644 index 00000000..7bf540ef --- /dev/null +++ b/core/crates/application/local/lifecycle/src/supervision.rs @@ -0,0 +1,148 @@ +use anyhow::{Context, Result, ensure}; +use std::{path::PathBuf, time::Duration}; +use yolo_hub_state::HubState; +use yolo_local_profile::LocalProfile; + +const SYSTEMCTL_TIMEOUT: Duration = Duration::from_secs(2); + +#[derive(Debug)] +pub struct ServiceUnit { + pub name: String, + pid: u32, + user: bool, +} + +/// Resolve the supervisor from the selected profile's private socket owners. +/// A Host wrapper may move its PID to a separate PTY custody cgroup, so use +/// systemd's MainPID instead of the process's current cgroup. +pub async fn service_units(profile: &LocalProfile) -> Result> { + if !cfg!(target_os = "linux") { + return Ok(Vec::new()); + } + let hub = HubState::socket_path_for(&profile.state)?; + let host = yolo_paths::Paths::new(&profile.host_home).host_socket()?; + let (hub, host) = tokio::join!(crate::service_pid(&hub), crate::service_pid(&host)); + let pids: Vec<_> = [host, hub].into_iter().flatten().collect(); + if pids.is_empty() { + return Ok(Vec::new()); + } + let runtime = format!("/run/user/{}/systemd/private", unsafe { libc::geteuid() }); + let mut units = Vec::new(); + for (user, socket) in [(false, "/run/systemd/private"), (true, runtime.as_str())] { + if !std::path::Path::new(socket).exists() { + continue; + } + let listing = systemctl( + user, + &[ + "show", + "--type=service", + "--property=Id", + "--property=MainPID", + "--", + "*.service", + ], + ) + .await?; + units.extend(matching_units(&listing, &pids, user)); + } + Ok(units) +} + +impl ServiceUnit { + /// Read the effective command from the manager after applying drop-ins. + /// A launcher can take the pinned image path as its only argument. + pub async fn image_path(&self) -> Result { + let effective = systemctl( + self.user, + &[ + "show", + "--property=MainPID", + "--property=NeedDaemonReload", + "--property=ExecStart", + "--", + &self.name, + ], + ) + .await?; + ensure!( + property(&effective, "MainPID=").and_then(|pid| pid.parse().ok()) == Some(self.pid), + "service owner changed during admission" + ); + ensure!( + property(&effective, "NeedDaemonReload=") == Some("no"), + "service configuration needs systemd daemon-reload" + ); + image_path(property(&effective, "ExecStart=").context("missing effective ExecStart")?) + } +} + +fn matching_units(listing: &str, pids: &[u32], user: bool) -> Vec { + listing + .split("\n\n") + .filter_map(|unit| { + let name = property(unit, "Id=")?; + let pid = property(unit, "MainPID=")?.parse().ok()?; + (name.ends_with(".service") && pids.contains(&pid)).then(|| ServiceUnit { + name: name.into(), + pid, + user, + }) + }) + .collect() +} + +fn image_path(command: &str) -> Result { + let command = command + .strip_prefix("{ path=") + .context("unknown ExecStart format")?; + let (path, argv) = command + .split_once(" ; argv[]=") + .context("missing ExecStart arguments")?; + let path = PathBuf::from(path); + ensure!(path.is_absolute(), "service ExecStart must be absolute"); + if path.file_name().is_some_and(|name| name == "yolo") { + return Ok(path); + } + let (argv, _) = argv + .split_once(" ; ") + .context("missing ExecStart argument list")?; + let image = argv + .strip_prefix(path.to_str().context("invalid service launcher path")?) + .and_then(|args| args.strip_prefix(' ')) + .context("the service launcher must take the immutable yolo image as its only argument")?; + let image = PathBuf::from(image); + ensure!( + image.is_absolute() && image.file_name().is_some_and(|name| name == "yolo"), + "the service launcher must take the immutable yolo image as its only argument" + ); + Ok(image) +} + +fn property<'a>(text: &'a str, key: &str) -> Option<&'a str> { + text.lines().find_map(|line| line.strip_prefix(key)) +} + +async fn systemctl(user: bool, args: &[&str]) -> Result { + let mut command = tokio::process::Command::new("/usr/bin/systemctl"); + command + .arg(if user { "--user" } else { "--system" }) + .arg("--no-pager") + .args(args) + .kill_on_drop(true); + if user { + command.env( + "XDG_RUNTIME_DIR", + format!("/run/user/{}", unsafe { libc::geteuid() }), + ); + command.env_remove("DBUS_SESSION_BUS_ADDRESS"); + } + let output = tokio::time::timeout(SYSTEMCTL_TIMEOUT, command.output()) + .await + .context("systemd service inspection timed out")??; + ensure!( + output.status.success(), + "could not inspect the selected profile's systemd services" + ); + Ok(String::from_utf8(output.stdout)?) +} diff --git a/core/crates/application/local/lifecycle/unit_tests/image.rs b/core/crates/application/local/lifecycle/unit_tests/image.rs new file mode 100644 index 00000000..d03fc920 --- /dev/null +++ b/core/crates/application/local/lifecycle/unit_tests/image.rs @@ -0,0 +1,27 @@ +use std::{fs, os::unix::fs::PermissionsExt}; +use tempfile::tempdir; +use yolo_local_lifecycle::image; + +#[test] +fn image_hashes_a_regular_executable_and_rejects_a_selector_link() { + let directory = tempdir().unwrap(); + let executable = directory.path().join("yolo"); + fs::write(&executable, b"candidate").unwrap(); + fs::set_permissions(&executable, fs::Permissions::from_mode(0o755)).unwrap(); + let observed = image(&executable).unwrap(); + assert_eq!(observed.size_bytes(), 9); + assert_eq!(observed.sha256().len(), 64); + + let link = directory.path().join("yolo-link"); + std::os::unix::fs::symlink(&executable, &link).unwrap(); + assert!(image(&link).is_err()); + assert_eq!(image(&fs::canonicalize(&link).unwrap()).unwrap(), observed); +} + +#[test] +fn image_rejects_empty_files() { + let directory = tempdir().unwrap(); + let executable = directory.path().join("yolo"); + fs::write(&executable, b"").unwrap(); + assert!(image(&executable).is_err()); +} diff --git a/core/crates/application/local/lifecycle/unit_tests/lib.rs b/core/crates/application/local/lifecycle/unit_tests/lib.rs new file mode 100644 index 00000000..4b10f9c4 --- /dev/null +++ b/core/crates/application/local/lifecycle/unit_tests/lib.rs @@ -0,0 +1,4 @@ +mod image; +mod service_pid; +mod state; +mod status; diff --git a/core/crates/application/local/lifecycle/unit_tests/service_pid.rs b/core/crates/application/local/lifecycle/unit_tests/service_pid.rs new file mode 100644 index 00000000..dc137be4 --- /dev/null +++ b/core/crates/application/local/lifecycle/unit_tests/service_pid.rs @@ -0,0 +1,21 @@ +use std::os::unix::net::{UnixListener, UnixStream}; +use tempfile::tempdir; +use yolo_local_lifecycle::service_pid; + +#[tokio::test] +async fn service_pid_resolves_the_socket_owner() { + let directory = tempdir().unwrap(); + let path = directory.path().join("service.sock"); + let _listener = UnixListener::bind(&path).unwrap(); + let _client = UnixStream::connect(&path).unwrap(); + assert_eq!(service_pid(&path).await, Some(std::process::id())); +} + +#[tokio::test] +async fn service_pid_returns_none_for_a_missing_socket() { + let directory = tempdir().unwrap(); + assert_eq!( + service_pid(&directory.path().join("absent.sock")).await, + None + ); +} diff --git a/core/crates/application/local/lifecycle/unit_tests/state.rs b/core/crates/application/local/lifecycle/unit_tests/state.rs new file mode 100644 index 00000000..ac8e4722 --- /dev/null +++ b/core/crates/application/local/lifecycle/unit_tests/state.rs @@ -0,0 +1,46 @@ +use std::fs; +use std::os::unix::fs::PermissionsExt; +use std::path::Path; +use tempfile::tempdir; +use yolo_local_lifecycle::validate_state; +use yolo_local_profile::LocalProfile; + +fn profile(directory: &Path) -> LocalProfile { + fs::set_permissions(directory, fs::Permissions::from_mode(0o700)).unwrap(); + let path = directory.join("profile.json"); + fs::write( + &path, + format!( + "{{\"schema_version\":1,\"state\":\"{}\",\"host_home\":\"{}\"}}\n", + directory.join("state").display(), + directory.join("home").display() + ), + ) + .unwrap(); + fs::set_permissions(&path, fs::Permissions::from_mode(0o600)).unwrap(); + LocalProfile::load(&path).unwrap() +} + +#[test] +fn absent_state_directory_needs_no_contract() { + let directory = tempdir().unwrap(); + let profile = profile(directory.path()); + validate_state(&profile).unwrap(); +} + +#[test] +fn preflight_accepts_the_installed_contract_and_rejects_a_newer_one() { + let directory = tempdir().unwrap(); + let profile = profile(directory.path()); + drop(yolo_hub_state::HubState::open(&profile.state, &profile.host_home).unwrap()); + validate_state(&profile).unwrap(); + + let contract = profile.state.join(yolo_hub_state::STATE_CONTRACT_FILE); + let current = yolo_hub_rpc_protocol::HUB_PROTOCOL_VERSION; + let mut document: serde_json::Value = + serde_json::from_slice(&fs::read(&contract).unwrap()).unwrap(); + assert_eq!(document["hub_protocol"], serde_json::json!(current)); + document["hub_protocol"] = serde_json::json!(u16::MAX); + fs::write(&contract, serde_json::to_vec(&document).unwrap()).unwrap(); + assert!(validate_state(&profile).is_err()); +} diff --git a/core/crates/application/local/lifecycle/unit_tests/status.rs b/core/crates/application/local/lifecycle/unit_tests/status.rs new file mode 100644 index 00000000..5b5286e0 --- /dev/null +++ b/core/crates/application/local/lifecycle/unit_tests/status.rs @@ -0,0 +1,31 @@ +use std::fs; +use std::os::unix::fs::PermissionsExt; +use tempfile::tempdir; +use yolo_local_lifecycle::status; +use yolo_local_profile::LocalProfile; + +#[tokio::test] +async fn status_reports_a_never_started_profile_as_stopped() { + let directory = tempdir().unwrap(); + fs::set_permissions(directory.path(), fs::Permissions::from_mode(0o700)).unwrap(); + let profile_path = directory.path().join("profile.json"); + fs::write( + &profile_path, + format!( + "{{\"schema_version\":1,\"state\":\"{}\",\"host_home\":\"{}\"}}\n", + directory.path().join("state").display(), + directory.path().join("home").display() + ), + ) + .unwrap(); + fs::set_permissions(&profile_path, fs::Permissions::from_mode(0o600)).unwrap(); + let profile = LocalProfile::load(&profile_path).unwrap(); + let report = status(&profile).await.unwrap(); + assert!(!report.hub.running); + assert!(!report.host.running); + assert!(report.hub.pid.is_none()); + assert!(report.host.pid.is_none()); + assert!(report.hub.image.is_none()); + assert!(!report.client_version.is_empty()); + assert!(!report.restart_needed); +} diff --git a/core/crates/application/local/profile/src/profile.rs b/core/crates/application/local/profile/src/profile.rs index b4cd091c..6d722493 100644 --- a/core/crates/application/local/profile/src/profile.rs +++ b/core/crates/application/local/profile/src/profile.rs @@ -1,5 +1,5 @@ use crate::constants::*; -use anyhow::{Result, ensure}; +use anyhow::{Context, Result, ensure}; use serde::{Deserialize, Serialize}; use std::path::{Path, PathBuf}; @@ -27,7 +27,8 @@ impl LocalProfile { /// Reads an explicit private profile; never discovers or creates another profile. pub fn load(path: &Path) -> Result { validate_path(path)?; - let profile: Self = yolo_paths::read_json(path)?; + let profile: Self = yolo_paths::read_json(path) + .with_context(|| format!("read local profile {}", path.display()))?; profile.validate()?; Ok(profile) } diff --git a/core/crates/application/protocol/cli/src/arguments.rs b/core/crates/application/protocol/cli/src/arguments.rs index 369f1cce..38c08c9c 100644 --- a/core/crates/application/protocol/cli/src/arguments.rs +++ b/core/crates/application/protocol/cli/src/arguments.rs @@ -16,8 +16,8 @@ pub(crate) enum Command { }, } -#[derive(Subcommand)] -pub(crate) enum ProtocolCommand { +#[derive(Debug, Subcommand)] +pub enum ProtocolCommand { /// Describe the public Hub contract. Output is machine-readable JSON. Describe { /// Print machine-readable JSON (this command's only output format). diff --git a/core/crates/application/protocol/cli/src/lib.rs b/core/crates/application/protocol/cli/src/lib.rs index a3fc36bf..4114e019 100644 --- a/core/crates/application/protocol/cli/src/lib.rs +++ b/core/crates/application/protocol/cli/src/lib.rs @@ -6,6 +6,7 @@ use std::ffi::OsString; pub const PROTOCOL_COMMAND: &str = "protocol"; pub const COMMAND_INDEX: usize = 1; +pub use arguments::ProtocolCommand; pub fn selects(arguments: &[OsString]) -> bool { arguments @@ -17,7 +18,13 @@ pub fn selects(arguments: &[OsString]) -> bool { pub fn run_from(arguments: impl IntoIterator) -> anyhow::Result<()> { use clap::Parser; let arguments = arguments::Arguments::try_parse_from(arguments)?; - output::write(arguments.command, &mut std::io::stdout().lock()) + let arguments::Command::Protocol { command } = arguments.command; + run_command(command) +} + +/// Runs one already parsed protocol command. +pub fn run_command(command: arguments::ProtocolCommand) -> anyhow::Result<()> { + output::write_command(command, &mut std::io::stdout().lock()) } #[cfg(test)] diff --git a/core/crates/application/protocol/cli/src/output.rs b/core/crates/application/protocol/cli/src/output.rs index e2910e80..32492285 100644 --- a/core/crates/application/protocol/cli/src/output.rs +++ b/core/crates/application/protocol/cli/src/output.rs @@ -18,3 +18,7 @@ pub(crate) fn write(command: Command, output: &mut impl Write) -> Result<()> { output.write_all(JSON_LINE_END)?; Ok(()) } + +pub(crate) fn write_command(command: ProtocolCommand, output: &mut impl Write) -> Result<()> { + write(Command::Protocol { command }, output) +} diff --git a/core/crates/application/trusted-plugin/cli/src/arguments.rs b/core/crates/application/trusted-plugin/cli/src/arguments.rs index de95002c..54cd5e0b 100644 --- a/core/crates/application/trusted-plugin/cli/src/arguments.rs +++ b/core/crates/application/trusted-plugin/cli/src/arguments.rs @@ -9,6 +9,7 @@ pub struct Arguments { #[derive(Debug, Subcommand)] pub enum Command { + /// Manage trusted user-level plugins. Plugin(Options), } @@ -20,61 +21,95 @@ pub struct Options { #[derive(Debug, Subcommand)] pub enum PluginCommand { + /// List linked plugins and their trust state. List { + /// Print one JSON document. #[arg(long)] json: bool, }, + /// Show one plugin's manifest, commands and grants. Describe { + /// Plugin id. id: String, + /// Print one JSON document. #[arg(long)] json: bool, }, + /// Link a local plugin directory after reviewing its commands and digest. Link { + /// Plugin directory containing the manifest. dir: std::path::PathBuf, + /// Confirm the reviewed commands and digest; without it, only prints the review. #[arg(long)] trust: bool, + /// Print one JSON document. #[arg(long)] json: bool, }, + /// Install a registry plugin after reviewing its source digest. Install { + /// Plugin id in the configured registry. id: String, + /// Registry version; defaults to the registry's current version. #[arg(long)] version: Option, + /// Registry URL or name; defaults to the configured registry. #[arg(long)] registry: Option, + /// Confirm the reviewed source commands and digest; without it, only prints the review. #[arg(long)] trust: bool, + /// Print one JSON document. #[arg(long)] json: bool, }, + /// Remove a linked plugin without touching its data directory. Unlink { + /// Plugin id. id: String, }, + /// Allow a linked plugin to run startup hooks and actions. Enable { + /// Plugin id. id: String, }, + /// Stop a linked plugin from running hooks and actions. Disable { + /// Plugin id. id: String, }, + /// Run the startup hooks of all enabled plugins. Startup { + /// Print one JSON document. #[arg(long)] json: bool, }, + /// Invoke one declared plugin action with a JSON argument. Action { + /// Plugin id. id: String, + /// Declared action name. action: String, + /// Print one JSON document. #[arg(long)] json: bool, }, + /// Show the last startup or action result for one plugin. Status { + /// Plugin id. id: String, + /// Print one JSON document. #[arg(long)] json: bool, }, + /// Print one plugin's bounded startup and action log. Log { + /// Plugin id. id: String, }, + /// Print one plugin's writable data directory. DataDir { + /// Plugin id. id: String, }, } diff --git a/core/crates/application/trusted-plugin/cli/src/commands.rs b/core/crates/application/trusted-plugin/cli/src/commands.rs index 0680b7c3..c7e1bb65 100644 --- a/core/crates/application/trusted-plugin/cli/src/commands.rs +++ b/core/crates/application/trusted-plugin/cli/src/commands.rs @@ -8,8 +8,12 @@ use crate::arguments::{Arguments, Command as RootCommand, PluginCommand}; pub(crate) fn run(arguments: Arguments) -> Result<()> { let RootCommand::Plugin(options) = arguments.command; + run_command(options.command) +} + +pub(crate) fn run_command(command: PluginCommand) -> Result<()> { let paths = crate::paths::registry_paths()?; - match options.command { + match command { PluginCommand::List { json } => list(&paths, json), PluginCommand::Describe { id, json } => describe(&paths, &id, json), PluginCommand::Link { dir, trust, json } => link(&paths, &dir, trust, json), diff --git a/core/crates/application/trusted-plugin/cli/src/lib.rs b/core/crates/application/trusted-plugin/cli/src/lib.rs index 2ef958cf..309041f1 100644 --- a/core/crates/application/trusted-plugin/cli/src/lib.rs +++ b/core/crates/application/trusted-plugin/cli/src/lib.rs @@ -13,6 +13,7 @@ mod paths; use std::ffi::OsString; pub const COMMAND_INDEX: usize = 1; +pub use arguments::PluginCommand; pub fn selects(arguments: &[OsString]) -> bool { arguments @@ -27,6 +28,11 @@ pub fn run_from(arguments: impl IntoIterator) -> anyhow::Result commands::run(arguments) } +/// Runs one already parsed plugin command. +pub fn run_plugin(command: arguments::PluginCommand) -> anyhow::Result<()> { + commands::run_command(command) +} + #[cfg(test)] #[path = "../unit_tests/lib.rs"] mod unit_tests; diff --git a/core/crates/application/upgrade/cli/BUILD.bazel b/core/crates/application/upgrade/cli/BUILD.bazel index 694e4a85..a3ba0484 100644 --- a/core/crates/application/upgrade/cli/BUILD.bazel +++ b/core/crates/application/upgrade/cli/BUILD.bazel @@ -4,6 +4,7 @@ load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") package(default_visibility = ["//visibility:public"]) _DEPS = [ + "//core/crates/application/local/lifecycle:yolo_local_lifecycle", "//core/crates/build_image:yolo_build_image", "//core/crates/hub/client:yolo_hub_client", "//core/crates/hub/identity:yolo_hub_identity", @@ -14,7 +15,6 @@ _DEPS = [ "@crates//:clap", "@crates//:serde", "@crates//:serde_json", - "@crates//:sha2", "@crates//:tokio", ] diff --git a/core/crates/application/upgrade/cli/src/candidate.rs b/core/crates/application/upgrade/cli/src/candidate.rs new file mode 100644 index 00000000..15fa53ca --- /dev/null +++ b/core/crates/application/upgrade/cli/src/candidate.rs @@ -0,0 +1,50 @@ +//! Runs the installed image for decisions that only it can make. +use crate::check::Options; +use crate::constants::*; +use anyhow::{Context, Result}; +use std::path::Path; + +/// Admission after an install must run with the candidate image. The invoking +/// CLI may be an older protocol and would otherwise reject the Hub it just +/// started. The installed binary re-runs the same read-only checks with its own +/// protocol and state-contract constants. +pub(crate) fn admit(options: &Options, installed: &Path) -> Result<()> { + let mut command = command(options, installed, ADMIT_FLAG)?; + if options.json { + command.arg(JSON_FLAG); + } + let status = command.status().context(INSTALLED_ADMISSION_FAILED)?; + anyhow::ensure!(status.success(), "{}", UPGRADE_REFUSED); + Ok(()) +} + +/// The installed image, not the invoking updater, decides whether the local +/// state contract is still readable. This runs before services are stopped so a +/// refused migration leaves the running Host and Hub untouched. +pub(crate) fn validate_state(options: &Options, installed: &Path) -> Result<()> { + let mut command = command(options, installed, VALIDATE_STATE_FLAG)?; + let status = command.status().context(INSTALLED_STATE_FAILED)?; + anyhow::ensure!(status.success(), "{}", INSTALLED_STATE_REFUSED); + Ok(()) +} + +fn command(options: &Options, installed: &Path, mode: &str) -> Result { + let mut command = std::process::Command::new(installed.canonicalize()?); + command.arg(COMMAND).arg(mode); + if let Some(profile) = &options.local_profile { + command.arg(LOCAL_PROFILE_FLAG).arg(profile); + } + if let Some(socket) = &options.socket { + command.arg(SOCKET_FLAG).arg(socket); + } + if let Some(hub) = options.hub { + command.arg(HUB_FLAG).arg(hub.to_string()); + } + if let Some(state) = &options.state { + command.arg(STATE_FLAG).arg(state); + } + for unit in &options.service_units { + command.arg(SERVICE_UNIT_FLAG).arg(unit); + } + Ok(command) +} diff --git a/core/crates/application/upgrade/cli/src/check.rs b/core/crates/application/upgrade/cli/src/check.rs index 8b8bd43d..b53d56bb 100644 --- a/core/crates/application/upgrade/cli/src/check.rs +++ b/core/crates/application/upgrade/cli/src/check.rs @@ -1,46 +1,131 @@ -use crate::{ - constants::*, - report::CheckReport, - service::{self, ServiceUnit}, -}; +use crate::{candidate, constants::*, github, probe}; use anyhow::{Context, Result}; use clap::Args; -use std::{path::PathBuf, time::Duration}; -use tokio::time::Instant; -use yolo_build_image::BuildImage; -use yolo_hub_client::Client; +use std::path::PathBuf; use yolo_hub_identity::HubId; -use yolo_hub_rpc_protocol::{Command, HUB_PROTOCOL_VERSION, Reply}; -use yolo_hub_state::{HubState, validate_runtime_contract}; +use yolo_hub_state::HubState; use yolo_local_profile::LocalProfile; -#[derive(Debug, Args)] -pub(crate) struct Options { +#[derive(Clone, Debug, Args)] +pub struct Options { #[arg(long, hide = true, value_name = "PATH")] - pub(crate) socket: Option, + pub socket: Option, #[arg(long, hide = true, value_name = "HUB_ID")] - pub(crate) hub: Option, + pub hub: Option, #[arg(long, hide = true, value_name = "PATH")] - pub(crate) state: Option, + pub state: Option, #[arg(long = SERVICE_UNIT_ARGUMENT, hide = true, value_name = "PATH")] - pub(crate) service_units: Vec, + pub service_units: Vec, + /// Print one JSON document. #[arg(long)] - pub(crate) json: bool, + pub json: bool, + /// Release channel to select. #[arg(long, value_enum, default_value = "stable")] - pub(crate) channel: crate::github_release::Channel, - #[arg(long = CHECK_RELEASE_ARGUMENT)] - pub(crate) check_release: bool, - #[arg(long = INSTALL_RELEASE_ARGUMENT)] - pub(crate) install_release: bool, + pub channel: crate::github_release::Channel, + /// Check the latest release without installing or restarting. + #[arg(long, alias = CHECK_RELEASE_ARGUMENT, conflicts_with_all = ["install_release", "restart", "admit", "no_restart", "validate_state"])] + pub check: bool, + /// Admit the already installed image without checking releases. + #[arg(long, conflicts_with_all = ["install_release", "restart", "check", "no_restart", "validate_state"])] + pub admit: bool, + /// Restart the local Host and Hub, then admit the installed image. + #[arg(long, conflicts_with_all = ["install_release", "check", "admit", "no_restart", "validate_state"])] + pub restart: bool, + /// Install a newer release without restarting; only with the default update. + #[arg(long, conflicts_with_all = ["install_release", "check", "admit", "restart", "validate_state"])] + pub no_restart: bool, + /// Legacy install-only mode. + #[arg(long, hide = true, conflicts_with_all = ["check", "admit", "restart", "no_restart", "validate_state"])] + pub install_release: bool, + /// Validate the local Hub state contract with this image without changing it. + #[arg(long, hide = true, conflicts_with_all = ["install_release", "restart", "check", "admit", "no_restart"])] + pub validate_state: bool, + /// Local profile file; defaults to the current user profile. + #[arg(long, value_name = "PATH", conflicts_with_all = [STATE_ARGUMENT, SOCKET_ARGUMENT, HUB_ARGUMENT])] + pub local_profile: Option, #[arg(long, hide = true)] - pub(crate) repository: Option, + pub repository: Option, } -pub(crate) fn run(options: Options) -> Result<()> { - if options.check_release || options.install_release { - return crate::github::run(&options); +pub fn run(options: Options) -> Result<()> { + if options.check { + return github::check(&options); } - let profile: LocalProfile = yolo_local_profile::current()?; + if options.install_release { + let _ = github::install(&options)?; + println!("run `yolo restart` to activate it"); + return Ok(()); + } + let profile = match options.local_profile.as_deref() { + Some(path) => LocalProfile::load(path)?, + None => yolo_local_profile::current()?, + }; + if options.validate_state { + return yolo_local_lifecycle::validate_state(&profile); + } + let runtime = tokio::runtime::Runtime::new()?; + if options.admit { + return admit(&options, &profile, &runtime, None); + } + if options.restart { + let executable = std::env::current_exe()?; + yolo_local_lifecycle::validate_state(&profile)?; + runtime.block_on(yolo_local_lifecycle::restart(&profile, &executable))?; + println!("restarted local Host and Hub"); + return admit(&options, &profile, &runtime, None); + } + let installed = github::update(&options)?; + let status = runtime.block_on(yolo_local_lifecycle::status(&profile))?; + let running = status.hub.running || status.host.running; + if !running { + println!("no local services are running; run `yolo server start` to start them"); + return Ok(()); + } + let restart_needed = installed.is_some() || status.restart_needed; + if options.no_restart { + if restart_needed { + println!("run `yolo restart` to activate it"); + } + return Ok(()); + } + if restart_needed { + let executable = installed.clone().unwrap_or(std::env::current_exe()?); + match &installed { + Some(installed) => candidate::validate_state(&options, installed)?, + None => yolo_local_lifecycle::validate_state(&profile)?, + } + runtime.block_on(yolo_local_lifecycle::restart(&profile, &executable))?; + println!("restarted local Host and Hub"); + } + admit(&options, &profile, &runtime, installed) +} + +fn admit( + options: &Options, + profile: &LocalProfile, + runtime: &tokio::runtime::Runtime, + installed: Option, +) -> Result<()> { + if let Some(installed) = installed { + return candidate::admit(options, &installed); + } + let options = local_options(options.clone(), profile)?; + let candidate_path = match installed { + Some(link) => link.canonicalize()?, + None => std::env::current_exe()?.canonicalize()?, + }; + let candidate = yolo_local_lifecycle::image(&candidate_path)?; + let report = runtime.block_on(probe::probe(&options, profile, candidate))?; + if options.json { + println!("{}", report.to_json()?); + } else { + report.print(); + } + anyhow::ensure!(report.ready(), "{}", UPGRADE_REFUSED); + Ok(()) +} + +fn local_options(mut options: Options, profile: &LocalProfile) -> Result { let state = options .state .clone() @@ -52,143 +137,11 @@ pub(crate) fn run(options: Options) -> Result<()> { let hub = options .hub .or_else(|| HubState::hub_id_for(&state, &profile.host_home).ok()) - .context("local Hub identity is unavailable; update admission cannot select a Hub")?; - let service_units = if options.service_units.is_empty() { - default_units() - } else { - options.service_units.clone() - }; - let mut options = options; + .context( + "local Hub identity is unavailable; run `yolo server start` to initialize the profile, or pass --local-profile", + )?; options.socket = Some(socket); options.hub = Some(hub); options.state = Some(state); - options.service_units = service_units; - let candidate = service::image(&std::env::current_exe()?)?; - let report = tokio::runtime::Runtime::new()?.block_on(probe(&options, candidate.clone())); - let report = report?; - if options.json { - println!("{}", report.to_json()?); - } else { - print_report(&report); - } - anyhow::ensure!( - report.ready(), - "upgrade admission refused; stop the old Hub and runner after reviewing the failed checks" - ); - Ok(()) -} - -async fn probe(options: &Options, candidate: BuildImage) -> Result { - let mut report = CheckReport::new(candidate.clone()); - report.push( - CHECK_CANDIDATE, - true, - format!("{} bytes", candidate.size_bytes()), - ); - let deadline = Instant::now() + Duration::from_secs(CHECK_DEADLINE_SECONDS); - let client = Client::connect_local( - options.socket.as_ref().unwrap(), - options.hub.unwrap(), - deadline, - ) - .await; - let mut client = match client { - Ok(client) => client, - Err(error) => { - report.push( - CHECK_PROTOCOL, - false, - format!("cannot contact running Hub: {error}"), - ); - check_state_and_units(options, candidate, &mut report); - return Ok(report); - } - }; - let hello = client.hello().clone(); - report.push( - CHECK_PROTOCOL, - hello.version == HUB_PROTOCOL_VERSION, - format!( - "running={}, expected={}", - hello.version, HUB_PROTOCOL_VERSION - ), - ); - match client - .call(options.hub.unwrap(), Command::GetBuild {}, deadline) - .await - { - Ok(Reply::Build { build }) => { - report.push( - CHECK_BUILD, - build.host == candidate && build.hub == candidate && build.file_helper == candidate, - format!( - "host={}, hub={}, file_helper={}", - build.host.sha256(), - build.hub.sha256(), - build.file_helper.sha256() - ), - ); - } - Ok(reply) => report.push( - CHECK_BUILD, - false, - format!("unexpected Hub reply: {reply:?}"), - ), - Err(error) => report.push( - CHECK_BUILD, - false, - format!("cannot read live service images: {error}"), - ), - } - check_state_and_units(options, candidate, &mut report); - Ok(report) -} - -fn check_state_and_units(options: &Options, candidate: BuildImage, report: &mut CheckReport) { - let state = validate_runtime_contract(options.state.as_ref().unwrap()); - report.push( - CHECK_STATE, - state.is_ok(), - state - .err() - .map_or_else(|| CONTRACT_MATCH.into(), |error| error.to_string()), - ); - if options.service_units.is_empty() { - report.push( - CHECK_SERVICE_UNITS, - !cfg!(target_os = "linux"), - NO_SERVICE_UNITS.to_owned(), - ); - } - for path in &options.service_units { - let name = format!("{CHECK_SERVICE_PREFIX}{}", path.display()); - match ServiceUnit::read(path) { - Ok(unit) => report.push( - name, - unit.image == candidate, - format!("{}", unit.executable.display()), - ), - Err(error) => report.push(name, false, error.to_string()), - } - } -} - -fn default_units() -> Vec { - [HOST_RUNNER_UNIT, HUB_UNIT] - .into_iter() - .map(PathBuf::from) - .filter(|path| path.exists()) - .collect() -} - -fn print_report(report: &CheckReport) { - println!("upgrade {}", report.status); - for check in &report.checks { - println!( - "{}: {} ({})", - if check.passed { "ok" } else { "fail" }, - check.name, - check.detail - ); - } + Ok(options) } diff --git a/core/crates/application/upgrade/cli/src/constants.rs b/core/crates/application/upgrade/cli/src/constants.rs index 720f96b8..b07cb509 100644 --- a/core/crates/application/upgrade/cli/src/constants.rs +++ b/core/crates/application/upgrade/cli/src/constants.rs @@ -5,6 +5,18 @@ pub(crate) const SOCKET_ARGUMENT: &str = "socket"; pub(crate) const HUB_ARGUMENT: &str = "hub"; pub(crate) const STATE_ARGUMENT: &str = "state"; pub(crate) const JSON_ARGUMENT: &str = "json"; +pub(crate) const INSTALLED_ADMISSION_FAILED: &str = "installed image admission failed to start"; +pub(crate) const INSTALLED_STATE_FAILED: &str = "installed image state check failed to start"; +pub(crate) const INSTALLED_STATE_REFUSED: &str = + "installed image rejected the local Hub state; local services were left running"; +pub(crate) const ADMIT_FLAG: &str = "--admit"; +pub(crate) const VALIDATE_STATE_FLAG: &str = "--validate-state"; +pub(crate) const JSON_FLAG: &str = "--json"; +pub(crate) const LOCAL_PROFILE_FLAG: &str = "--local-profile"; +pub(crate) const SOCKET_FLAG: &str = "--socket"; +pub(crate) const HUB_FLAG: &str = "--hub"; +pub(crate) const STATE_FLAG: &str = "--state"; +pub(crate) const SERVICE_UNIT_FLAG: &str = "--service-unit"; pub(crate) const HELP_ARGUMENT: &str = "--help"; pub(crate) const SERVICE_UNIT_IMAGE_DIGEST: &str = "X-Yolo-Image-SHA256="; pub(crate) const SERVICE_UNIT_IMAGE_SIZE: &str = "X-Yolo-Image-Size="; @@ -18,22 +30,17 @@ pub(crate) const CHECK_STATE: &str = "state_contract"; pub(crate) const CHECK_SERVICE_PREFIX: &str = "service_unit:"; pub(crate) const READY: &str = "ready"; pub(crate) const REFUSED: &str = "refused"; -pub(crate) const HOST_RUNNER_UNIT: &str = "/etc/systemd/system/yolo-host-runner.service"; -pub(crate) const HUB_UNIT: &str = "/etc/systemd/system/yolo-hub.service"; pub(crate) const CHECK_SERVICE_UNITS: &str = "service_units"; pub(crate) const EMPTY_EXEC_START: &str = "service unit ExecStart is empty"; pub(crate) const ABSOLUTE_EXEC_START: &str = "service ExecStart must be absolute"; pub(crate) const WRONG_EXECUTABLE: &str = "service ExecStart must point to yolo"; pub(crate) const MUTABLE_EXECUTABLE: &str = "service ExecStart must point to an immutable image"; -pub(crate) const INVALID_EXECUTABLE: &str = "service executable is not a regular file"; -pub(crate) const INVALID_EXECUTABLE_SIZE: &str = "service executable size is invalid"; -pub(crate) const EXECUTABLE_CHANGED: &str = "service executable changed while hashing"; +pub(crate) const UPGRADE_REFUSED: &str = + "upgrade admission refused; inspect the failed checks for the selected profile"; pub(crate) const DIGEST_MISMATCH: &str = "service image digest does not match executable"; pub(crate) const SIZE_MISMATCH: &str = "service image size does not match executable"; pub(crate) const CONTRACT_MATCH: &str = "runtime contract matches current image"; -pub(crate) const NO_SERVICE_UNITS: &str = "no managed systemd service units were discovered"; pub(crate) const CHECK_RELEASE_ARGUMENT: &str = "check-release"; -pub(crate) const INSTALL_RELEASE_ARGUMENT: &str = "install-release"; pub(crate) const NIGHTLY_VALUE: &str = "nightly"; pub(crate) const CURL_HEADER: &str = "--header"; pub(crate) const CURL_OUTPUT: &str = "--output"; @@ -48,6 +55,12 @@ pub(crate) const TARGET_MACOS_ARM64: &str = "aarch64-apple-darwin"; pub(crate) const TARGET_LINUX_X86_64: &str = "x86_64-unknown-linux-gnu"; pub(crate) const TARGET_LINUX_ARM64: &str = "aarch64-unknown-linux-gnu"; pub(crate) const CHECK_RELEASE_SWITCH: &str = "--check-release"; +pub(crate) const CHECK_SWITCH: &str = "--check"; +pub(crate) const ADMIT_SWITCH: &str = "--admit"; +pub(crate) const NO_RESTART_SWITCH: &str = "--no-restart"; +pub(crate) const INSTALL_RELEASE_SWITCH: &str = "--install-release"; +pub(crate) const RESTART_SWITCH: &str = "--restart"; +pub(crate) const LOCAL_PROFILE_SWITCH: &str = "--local-profile"; pub(crate) const CHANNEL_SWITCH: &str = "--channel"; #[cfg(all(target_os = "macos", target_arch = "aarch64"))] diff --git a/core/crates/application/upgrade/cli/src/github.rs b/core/crates/application/upgrade/cli/src/github.rs index 0f302cde..ad03f4df 100644 --- a/core/crates/application/upgrade/cli/src/github.rs +++ b/core/crates/application/upgrade/cli/src/github.rs @@ -1,9 +1,7 @@ use crate::{ check::Options, constants::*, - github_release::{ - self, DEFAULT_REPOSITORY, TARGET_TRIPLE, archive_asset, checksums_asset, digest_for, - }, + github_release::{self, DEFAULT_REPOSITORY, archive_asset, checksums_asset, digest_for}, install, }; use anyhow::{Context, Result, bail, ensure}; @@ -13,19 +11,48 @@ const API_ROOT: &str = "https://api.github.com"; const API_ENVIRONMENT: &str = "YOLO_GITHUB_API"; const MAX_RELEASES: usize = 30; -pub(crate) fn run(options: &Options) -> Result<()> { +fn latest(options: &Options) -> Result<(github_release::Release, String, String)> { let repository = options.repository.as_deref().unwrap_or(DEFAULT_REPOSITORY); validate_repository(repository)?; let api = env::var(API_ENVIRONMENT).unwrap_or_else(|_| API_ROOT.to_owned()); let releases = github_release::parse(&curl_text(&releases_url(&api, repository))?)?; let release = github_release::latest(&releases, options.channel)?; - let version = release.tag_name.trim_start_matches('v'); - let name = release.name.as_deref().unwrap_or(&release.tag_name); + let version = release.tag_name.trim_start_matches('v').to_owned(); + let name = release + .name + .clone() + .unwrap_or_else(|| release.tag_name.clone()); println!("current yolo {}", env!("CARGO_PKG_VERSION")); println!("github {} ({version})", name); - if !options.install_release { - return Ok(()); + Ok((release.clone(), version, name)) +} + +pub(crate) fn check(options: &Options) -> Result<()> { + let (_, version, _) = latest(options)?; + if is_newer(&version, env!("CARGO_PKG_VERSION")) { + println!("update available: {version}"); + } else { + println!("yolo {version} is already current"); + } + Ok(()) +} + +pub(crate) fn install(options: &Options) -> Result { + let (release, version, _) = latest(options)?; + install_release(&release, &version) +} + +/// Installs the selected release only when it is newer than this executable. +pub(crate) fn update(options: &Options) -> Result> { + let (release, version, _) = latest(options)?; + if !is_newer(&version, env!("CARGO_PKG_VERSION")) { + println!("yolo {version} is already current"); + return Ok(None); } + install_release(&release, &version).map(Some) +} + +fn install_release(release: &github_release::Release, version: &str) -> Result { let archive = archive_asset(release)?; let sums = checksums_asset(release)?; let work = work_dir()?; @@ -40,13 +67,28 @@ pub(crate) fn run(options: &Options) -> Result<()> { ) .and_then(|path| { println!("installed yolo {version}: {}", path.display()); - println!("run `yolo update` to check live service admission before restarting services"); - Ok(()) + Ok(path) }); let _ = fs::remove_dir_all(&work); result } +fn is_newer(candidate: &str, current: &str) -> bool { + match (components(candidate), components(current)) { + (Some(candidate), Some(current)) => candidate > current, + _ => candidate != current, + } +} + +fn components(version: &str) -> Option<[u64; 3]> { + let mut parts = version.split('.'); + let mut values = [0u64; 3]; + for value in &mut values { + *value = parts.next()?.parse().ok()?; + } + parts.next().is_none().then_some(values) +} + fn install_download( work: &Path, archive_name: &str, diff --git a/core/crates/application/upgrade/cli/src/github_release.rs b/core/crates/application/upgrade/cli/src/github_release.rs index 9dd62c1a..e13a3fa9 100644 --- a/core/crates/application/upgrade/cli/src/github_release.rs +++ b/core/crates/application/upgrade/cli/src/github_release.rs @@ -8,7 +8,7 @@ pub(crate) const DEFAULT_REPOSITORY: &str = "AgentsMesh/YoloShell"; pub(crate) const CHECKSUMS_ASSET: &str = "SHA256SUMS"; #[derive(Clone, Copy, Debug, Eq, PartialEq, ValueEnum)] -pub(crate) enum Channel { +pub enum Channel { Stable, Nightly, } diff --git a/core/crates/application/upgrade/cli/src/lib.rs b/core/crates/application/upgrade/cli/src/lib.rs index bdd27b2c..14ca25d4 100644 --- a/core/crates/application/upgrade/cli/src/lib.rs +++ b/core/crates/application/upgrade/cli/src/lib.rs @@ -1,11 +1,16 @@ +mod candidate; mod check; mod constants; mod github; mod github_release; mod install; +mod probe; mod report; mod service; +pub use check::{Options, run}; +pub use github_release::Channel; + use anyhow::Result; use clap::{Args, Parser, Subcommand}; use std::ffi::OsString; @@ -18,7 +23,8 @@ struct Cli { } #[derive(Debug, Subcommand)] -enum Command { +pub enum Command { + /// Check, install and activate Yolo releases. Update(check::Options), } diff --git a/core/crates/application/upgrade/cli/src/probe.rs b/core/crates/application/upgrade/cli/src/probe.rs new file mode 100644 index 00000000..b3de4a79 --- /dev/null +++ b/core/crates/application/upgrade/cli/src/probe.rs @@ -0,0 +1,148 @@ +use crate::{check::Options, constants::*, report::CheckReport, service::ServiceUnit}; +use anyhow::Result; +use std::time::Duration; +use tokio::time::Instant; +use yolo_build_image::BuildImage; +use yolo_hub_client::Client; +use yolo_hub_rpc_protocol::{Command, HUB_PROTOCOL_VERSION, Reply}; +use yolo_hub_state::validate_runtime_contract; +use yolo_local_profile::LocalProfile; + +pub(crate) async fn probe( + options: &Options, + profile: &LocalProfile, + candidate: BuildImage, +) -> Result { + let mut report = CheckReport::new(candidate.clone()); + report.push( + CHECK_CANDIDATE, + true, + format!("{} bytes", candidate.size_bytes()), + ); + let deadline = Instant::now() + Duration::from_secs(CHECK_DEADLINE_SECONDS); + let client = Client::connect_local( + options.socket.as_ref().unwrap(), + options.hub.unwrap(), + deadline, + ) + .await; + let mut client = match client { + Ok(client) => client, + Err(error) => { + report.push( + CHECK_PROTOCOL, + false, + format!("cannot contact running Hub: {error}"), + ); + check_state_and_units(options, profile, &candidate, &mut report).await; + return Ok(report); + } + }; + let hello = client.hello().clone(); + report.push( + CHECK_PROTOCOL, + hello.version == HUB_PROTOCOL_VERSION, + format!( + "running={}, expected={}", + hello.version, HUB_PROTOCOL_VERSION + ), + ); + // Hello consumed the connect budget; the build probe owns a fresh one. + match client + .call( + options.hub.unwrap(), + Command::GetBuild {}, + Instant::now() + Duration::from_secs(CHECK_DEADLINE_SECONDS), + ) + .await + { + Ok(Reply::Build { build }) => { + report.push( + CHECK_BUILD, + build.all_match(&candidate), + format!( + "host={}, hub={}, file_helper={}", + build.host.sha256(), + build.hub.sha256(), + build.file_helper.sha256() + ), + ); + } + Ok(reply) => report.push( + CHECK_BUILD, + false, + format!("unexpected Hub reply: {reply:?}"), + ), + Err(error) => report.push( + CHECK_BUILD, + false, + format!("cannot read live service images: {error}"), + ), + } + check_state_and_units(options, profile, &candidate, &mut report).await; + Ok(report) +} + +async fn check_state_and_units( + options: &Options, + profile: &LocalProfile, + candidate: &BuildImage, + report: &mut CheckReport, +) { + let state = validate_runtime_contract(options.state.as_ref().unwrap()); + report.push( + CHECK_STATE, + state.is_ok(), + state + .err() + .map_or_else(|| CONTRACT_MATCH.into(), |error| error.to_string()), + ); + if options.service_units.is_empty() { + check_supervised_units(profile, candidate, report).await; + } + for path in &options.service_units { + let name = format!("{CHECK_SERVICE_PREFIX}{}", path.display()); + match ServiceUnit::read(path) { + Ok(unit) => report.push( + name, + &unit.image == candidate, + format!("{}", unit.executable.display()), + ), + Err(error) => report.push(name, false, error.to_string()), + } + } +} + +async fn check_supervised_units( + profile: &LocalProfile, + candidate: &BuildImage, + report: &mut CheckReport, +) { + let units = match yolo_local_lifecycle::service_units(profile).await { + Ok(units) => units, + Err(error) => { + report.push(CHECK_SERVICE_UNITS, false, error.to_string()); + return; + } + }; + if units.is_empty() { + report.push( + CHECK_SERVICE_UNITS, + true, + "profile uses independently launched services", + ); + } + for unit in units { + let observed = async { + let path = unit.image_path().await?; + crate::service::verify_pin(&path, candidate)?; + Ok::<_, anyhow::Error>(path) + } + .await; + let name = format!("{CHECK_SERVICE_PREFIX}{}", unit.name); + match observed { + Ok(path) => report.push(name, true, path.display().to_string()), + Err(error) => report.push(name, false, error.to_string()), + } + } +} diff --git a/core/crates/application/upgrade/cli/src/report.rs b/core/crates/application/upgrade/cli/src/report.rs index e6478380..8bf95c34 100644 --- a/core/crates/application/upgrade/cli/src/report.rs +++ b/core/crates/application/upgrade/cli/src/report.rs @@ -48,4 +48,16 @@ impl CheckReport { pub(crate) fn to_json(&self) -> Result { Ok(serde_json::to_string_pretty(self)?) } + + pub(crate) fn print(&self) { + println!("upgrade {}", self.status); + for check in &self.checks { + println!( + "{}: {} ({})", + if check.passed { "ok" } else { "fail" }, + check.name, + check.detail + ); + } + } } diff --git a/core/crates/application/upgrade/cli/src/service.rs b/core/crates/application/upgrade/cli/src/service.rs index c5b83c67..5cdcf6e0 100644 --- a/core/crates/application/upgrade/cli/src/service.rs +++ b/core/crates/application/upgrade/cli/src/service.rs @@ -1,14 +1,10 @@ use crate::constants::*; use anyhow::{Context, Result, ensure}; -use sha2::{Digest, Sha256}; use std::{ fs, - io::Read, path::{Path, PathBuf}, }; -use yolo_build_image::{BuildImage, MAX_EXECUTABLE_IMAGE_BYTES}; - -const HEX_CHARS_PER_BYTE: usize = 2; +use yolo_build_image::BuildImage; #[derive(Clone, Debug, Eq, PartialEq)] pub(crate) struct ServiceUnit { @@ -57,43 +53,16 @@ fn field<'a>(text: &'a str, prefix: &str) -> Result<&'a str> { } pub(crate) fn image(path: &Path) -> Result { - let metadata = fs::symlink_metadata(path)?; - ensure!(metadata.file_type().is_file(), INVALID_EXECUTABLE); - ensure!( - metadata.len() > 0 && metadata.len() <= MAX_EXECUTABLE_IMAGE_BYTES, - INVALID_EXECUTABLE_SIZE - ); - let mut file = fs::File::open(path)?; - let mut digest = Sha256::new(); - let copied = std::io::copy( - &mut file.by_ref().take(metadata.len() + 1), - &mut DigestWriter(&mut digest), - )?; - ensure!(copied == metadata.len(), EXECUTABLE_CHANGED); - let after = file.metadata()?; - ensure!(after.len() == metadata.len(), EXECUTABLE_CHANGED); - BuildImage::new(hex(digest.finalize()), metadata.len()).map_err(Into::into) -} - -fn hex(bytes: impl AsRef<[u8]>) -> String { - const DIGITS: &[u8; 16] = b"0123456789abcdef"; - bytes.as_ref().iter().fold( - String::with_capacity(bytes.as_ref().len() * HEX_CHARS_PER_BYTE), - |mut result, byte| { - result.push(DIGITS[(byte >> 4) as usize] as char); - result.push(DIGITS[(byte & 0x0f) as usize] as char); - result - }, - ) + yolo_local_lifecycle::image(path) } -struct DigestWriter<'a>(&'a mut Sha256); -impl std::io::Write for DigestWriter<'_> { - fn write(&mut self, bytes: &[u8]) -> std::io::Result { - self.0.update(bytes); - Ok(bytes.len()) - } - fn flush(&mut self) -> std::io::Result<()> { - Ok(()) - } +pub(crate) fn verify_pin(path: &Path, candidate: &BuildImage) -> Result<()> { + let digest_directory = path.parent().and_then(Path::file_name); + ensure!( + digest_directory.is_some_and(|digest| digest == candidate.sha256()), + "effective service image must be pinned to the candidate digest" + ); + ensure!(path.canonicalize()? == path, MUTABLE_EXECUTABLE); + ensure!(&image(path)? == candidate, DIGEST_MISMATCH); + Ok(()) } diff --git a/core/crates/application/upgrade/cli/unit_tests/lib.rs b/core/crates/application/upgrade/cli/unit_tests/lib.rs index c09593f9..b03765dc 100644 --- a/core/crates/application/upgrade/cli/unit_tests/lib.rs +++ b/core/crates/application/upgrade/cli/unit_tests/lib.rs @@ -1,8 +1,12 @@ -use super::constants::{COMMAND, HELP_ARGUMENT, PROGRAM}; +use super::constants::{ + CHECK_RELEASE_SWITCH, CHECK_SWITCH, COMMAND, HELP_ARGUMENT, PROGRAM, RESTART_SWITCH, +}; use super::service::ServiceUnit; use std::{fs, os::unix::fs::PermissionsExt}; use tempfile::tempdir; +mod modes; + #[test] fn service_unit_requires_a_digest_pinned_regular_image() { let directory = tempdir().unwrap(); @@ -48,6 +52,21 @@ fn service_unit_rejects_mutable_or_unpinned_exec_start() { assert!(ServiceUnit::read(&symlink_unit).is_err()); } +#[test] +fn running_the_installer_symlink_resolves_to_the_immutable_image() { + let directory = tempdir().unwrap(); + let executable = directory.path().join("yolo"); + fs::write(&executable, b"candidate").unwrap(); + let link = directory.path().join("bin-yolo"); + std::os::unix::fs::symlink(&executable, &link).unwrap(); + assert!(super::service::image(&link).is_err()); + let resolved = fs::canonicalize(&link).unwrap(); + assert_eq!( + super::service::image(&resolved).unwrap(), + super::service::image(&executable).unwrap() + ); +} + #[test] fn update_is_the_only_public_admission_command() { use clap::Parser; @@ -60,6 +79,28 @@ fn update_is_the_only_public_admission_command() { assert!(!super::selects(&[PROGRAM.into(), "upgrade".into()])); } +#[test] +fn restart_is_available_without_release_flags() { + use clap::Parser; + let args = [PROGRAM, COMMAND, RESTART_SWITCH]; + let parsed = super::Cli::try_parse_from(args).unwrap(); + let super::Command::Update(options) = parsed.command; + assert!(options.restart); + assert!(!options.check); + assert!(!options.admit); + assert!(!options.install_release); +} + +#[test] +fn restart_refuses_to_combine_with_a_release_check() { + use clap::Parser; + let args = [PROGRAM, COMMAND, RESTART_SWITCH, CHECK_SWITCH]; + assert_eq!( + super::Cli::try_parse_from(args).unwrap_err().kind(), + clap::error::ErrorKind::ArgumentConflict + ); +} + #[test] fn one_failed_admission_check_cannot_be_hidden_by_later_success() { let executable = std::env::current_exe().unwrap(); @@ -94,7 +135,7 @@ fn selects_stable_and_nightly_releases_and_their_assets() { #[test] fn release_flags_are_selectable_without_local_profile() { - use super::constants::{CHANNEL_SWITCH, CHECK_RELEASE_SWITCH, COMMAND, NIGHTLY_VALUE, PROGRAM}; + use super::constants::{CHANNEL_SWITCH, NIGHTLY_VALUE}; use clap::Parser; let args = [ PROGRAM, @@ -105,7 +146,7 @@ fn release_flags_are_selectable_without_local_profile() { ]; let parsed = super::Cli::try_parse_from(args).unwrap(); let super::Command::Update(options) = parsed.command; - assert!(options.check_release); + assert!(options.check); assert!(!options.install_release); assert_eq!(options.channel, super::github_release::Channel::Nightly); } @@ -138,3 +179,21 @@ fn installs_a_release_archive_into_an_immutable_directory() { ); assert_eq!(fs::read(&link).unwrap(), b"#!/bin/sh\n"); } + +#[test] +fn update_accepts_a_local_profile() { + use clap::Parser; + use std::path::PathBuf; + let args = [ + PROGRAM, + COMMAND, + super::constants::LOCAL_PROFILE_SWITCH, + "/tmp/yolo-profile.json", + ]; + let parsed = super::Cli::try_parse_from(args).unwrap(); + let super::Command::Update(options) = parsed.command; + assert_eq!( + options.local_profile, + Some(PathBuf::from("/tmp/yolo-profile.json")) + ); +} diff --git a/core/crates/application/upgrade/cli/unit_tests/modes.rs b/core/crates/application/upgrade/cli/unit_tests/modes.rs new file mode 100644 index 00000000..bf1220e7 --- /dev/null +++ b/core/crates/application/upgrade/cli/unit_tests/modes.rs @@ -0,0 +1,80 @@ +use crate::Cli; +use crate::constants::*; +use clap::Parser; +use std::fs; +use std::os::unix::fs::PermissionsExt; +use tempfile::tempdir; + +fn parse(extra: &[&str]) -> crate::Options { + let mut args = vec![PROGRAM, COMMAND]; + args.extend_from_slice(extra); + let parsed = Cli::try_parse_from(args).unwrap(); + let crate::Command::Update(options) = parsed.command; + options +} + +#[test] +fn update_modes_are_explicit_and_install_activation_is_the_default() { + let update = parse(&[]); + assert!(!update.check && !update.admit && !update.restart && !update.install_release); + assert!(parse(&[CHECK_SWITCH]).check); + assert!(parse(&[CHECK_RELEASE_SWITCH]).check); + assert!(parse(&[ADMIT_SWITCH]).admit); + assert!(parse(&[INSTALL_RELEASE_SWITCH]).install_release); + assert!(parse(&[NO_RESTART_SWITCH]).no_restart); + assert!(parse(&[RESTART_SWITCH]).restart); + assert!(parse(&[VALIDATE_STATE_FLAG]).validate_state); +} + +#[test] +fn candidate_state_validation_runs_the_installed_image() { + let directory = tempdir().unwrap(); + let executable = directory.path().join("yolo"); + let args_file = directory.path().join("args.txt"); + fs::write( + &executable, + format!("#!/bin/sh\nprintf '%s\\n' \"$@\" > '{}'\n", args_file.display()), + ) + .unwrap(); + fs::set_permissions(&executable, fs::Permissions::from_mode(0o755)).unwrap(); + let profile = directory.path().join("profile.json"); + let profile = profile.to_str().unwrap(); + let options = parse(&[VALIDATE_STATE_FLAG, LOCAL_PROFILE_SWITCH, profile]); + + crate::candidate::validate_state(&options, &executable).unwrap(); + let args: Vec = fs::read_to_string(&args_file) + .unwrap() + .lines() + .map(str::to_owned) + .collect(); + assert_eq!(args.first().map(String::as_str), Some(COMMAND)); + assert_eq!(args.get(1).map(String::as_str), Some(VALIDATE_STATE_FLAG)); + assert!(args + .windows(2) + .any(|pair| pair[0] == LOCAL_PROFILE_SWITCH && pair[1] == profile)); + + fs::write(&executable, "#!/bin/sh\nexit 3\n").unwrap(); + let error = crate::candidate::validate_state(&options, &executable).unwrap_err(); + assert!(error.to_string().contains(INSTALLED_STATE_REFUSED)); +} + +#[test] +fn incompatible_update_modes_are_rejected_at_parse_time() { + for modes in [ + [CHECK_SWITCH, ADMIT_SWITCH], + [CHECK_SWITCH, NO_RESTART_SWITCH], + [ADMIT_SWITCH, RESTART_SWITCH], + [INSTALL_RELEASE_SWITCH, NO_RESTART_SWITCH], + [INSTALL_RELEASE_SWITCH, RESTART_SWITCH], + [VALIDATE_STATE_FLAG, ADMIT_SWITCH], + [VALIDATE_STATE_FLAG, RESTART_SWITCH], + ] { + let mut args = vec![PROGRAM, COMMAND]; + args.extend_from_slice(&modes); + assert_eq!( + Cli::try_parse_from(args).unwrap_err().kind(), + clap::error::ErrorKind::ArgumentConflict, + "{modes:?}" + ); + } +} diff --git a/core/crates/application/workspace/cli/src/arguments.rs b/core/crates/application/workspace/cli/src/arguments.rs index 21499284..60025adb 100644 --- a/core/crates/application/workspace/cli/src/arguments.rs +++ b/core/crates/application/workspace/cli/src/arguments.rs @@ -11,20 +11,22 @@ pub struct Arguments { #[derive(Debug, Subcommand)] pub enum Command { + /// Manage durable workspace metadata through the public Hub. Workspace(Options), + /// Manage git worktrees through the public Hub. Worktree(WorktreeOptions), } #[derive(Debug, Args)] pub struct WorktreeOptions { /// Existing local Hub entry socket; supply together with --hub. - #[arg(long, global = true)] + #[arg(long, global = true, help_heading = "Connection")] pub socket: Option, /// Identity of the Hub accepting this client connection. - #[arg(long, global = true)] + #[arg(long, global = true, help_heading = "Connection")] pub hub: Option, /// Local profile file; defaults to the current user profile. - #[arg(long, global = true)] + #[arg(long, global = true, help_heading = "Connection")] pub local_profile: Option, #[command(subcommand)] pub command: WorktreeCommand, @@ -44,15 +46,19 @@ pub enum WorktreeCommand { pub struct WorktreeCreateArgs { /// Parent workspace id or name. pub parent: String, + /// Unique name for the worktree workspace. #[arg(long)] pub name: String, + /// Branch to create; defaults to yolo/. #[arg(long)] pub branch: Option, + /// Commit-ish the new branch starts from. #[arg(long)] pub base: Option, /// Required acknowledgement that repository git hooks execute code. - #[arg(long)] + #[arg(long, required = true)] pub trust_repository: bool, + /// Print one JSON document. #[arg(long)] pub json: bool, } @@ -71,6 +77,7 @@ pub struct WorktreeListArgs { pub after: Option, #[arg(long, default_value_t = DEFAULT_LIMIT, value_parser = clap::value_parser!(u16).range(1..=MAX_LIMIT as i64))] pub limit: u16, + /// Print one JSON document. #[arg(long)] pub json: bool, } @@ -78,13 +85,13 @@ pub struct WorktreeListArgs { #[derive(Debug, Args)] pub struct Options { /// Existing local Hub entry socket; supply together with --hub. - #[arg(long, global = true)] + #[arg(long, global = true, help_heading = "Connection")] pub socket: Option, /// Identity of the Hub accepting this client connection. - #[arg(long, global = true)] + #[arg(long, global = true, help_heading = "Connection")] pub hub: Option, /// Local profile file; defaults to the current user profile. - #[arg(long, global = true)] + #[arg(long, global = true, help_heading = "Connection")] pub local_profile: Option, #[command(subcommand)] pub command: WorkspaceCommand, @@ -92,9 +99,13 @@ pub struct Options { #[derive(Debug, Subcommand)] pub enum WorkspaceCommand { + /// Create a durable workspace. Create(CreateArgs), + /// Rename, relabel or bind terminals in a workspace. Update(UpdateArgs), + /// Show one workspace by id or unique name. Get(GetArgs), + /// List workspaces after an optional cursor. List(ListArgs), /// List the durable terminal and run bindings of one workspace. Members(MembersArgs), @@ -102,23 +113,31 @@ pub enum WorkspaceCommand { #[derive(Debug, Args)] pub struct CreateArgs { + /// Unique workspace name. #[arg(long)] pub name: String, + /// Workspace root path on the Hub that owns the workspace. #[arg(long)] pub root: String, + /// Label in key=value form; repeatable. #[arg(long = "label")] pub labels: Vec, + /// Print one JSON document. #[arg(long)] pub json: bool, } #[derive(Debug, Args)] pub struct UpdateArgs { + /// Workspace id or unique name. pub selector: String, + /// Revision the caller last observed; the default is the revision just read. #[arg(long)] - pub expected_revision: u64, + pub expected_revision: Option, + /// New unique workspace name. #[arg(long)] pub name: Option, + /// Label in key=value form; repeatable. #[arg(long = "label")] pub labels: Vec, /// Bind an existing terminal id to this workspace. @@ -127,13 +146,16 @@ pub struct UpdateArgs { /// Unbind a terminal id from this workspace. #[arg(long = "unbind-terminal")] pub unbind_terminals: Vec, + /// Print one JSON document. #[arg(long)] pub json: bool, } #[derive(Debug, Args)] pub struct GetArgs { + /// Workspace id or unique name. pub selector: String, + /// Print one JSON document. #[arg(long)] pub json: bool, } @@ -143,8 +165,10 @@ pub struct ListArgs { /// Resume after this workspace id. #[arg(long)] pub after: Option, + /// Maximum workspaces to return. #[arg(long, default_value_t = DEFAULT_LIMIT, value_parser = clap::value_parser!(u16).range(1..=MAX_LIMIT as i64))] pub limit: u16, + /// Print one JSON document. #[arg(long)] pub json: bool, } @@ -153,6 +177,7 @@ pub struct ListArgs { pub struct MembersArgs { /// Workspace id or name. pub selector: String, + /// Print one JSON document. #[arg(long)] pub json: bool, } diff --git a/core/crates/application/workspace/cli/src/commands.rs b/core/crates/application/workspace/cli/src/commands.rs index cb0e8947..fefed95a 100644 --- a/core/crates/application/workspace/cli/src/commands.rs +++ b/core/crates/application/workspace/cli/src/commands.rs @@ -12,36 +12,40 @@ use yolo_hub_identity::{HubId, WorkspaceId}; use yolo_hub_rpc_protocol::{Command as Rpc, Reply}; use yolo_hub_workspace_protocol::{ CreateWorkspaceFields, CreateWorkspaceRequest, ListWorkspacesFields, ListWorkspacesRequest, - MembershipChange, UpdateWorkspaceFields, UpdateWorkspaceRequest, WorkspaceAddress, - WorkspaceSummary, WORKSPACE_PROTOCOL_SCHEMA_VERSION, + MembershipChange, UpdateWorkspaceFields, UpdateWorkspaceRequest, + WORKSPACE_PROTOCOL_SCHEMA_VERSION, WorkspaceAddress, WorkspaceSummary, }; pub(crate) use crate::selector::resolve; pub async fn run(arguments: Arguments) -> Result<()> { match arguments.command { - Command::Workspace(options) => { - let mut client = connection::connect(&Connection { - socket: options.socket, - hub: options.hub, - local_profile: options.local_profile, - }) - .await?; - let hub = client.hello().hub_id; - match options.command { - WorkspaceCommand::Create(args) => create(&mut client, hub, args).await, - WorkspaceCommand::Update(args) => update(&mut client, hub, args).await, - WorkspaceCommand::Get(args) => get(&mut client, hub, args).await, - WorkspaceCommand::List(args) => list(&mut client, hub, args).await, - WorkspaceCommand::Members(args) => { - crate::members::members(&mut client, hub, args).await - } - } - } - Command::Worktree(options) => crate::worktree::run(options).await, + Command::Workspace(options) => run_workspace(options).await, + Command::Worktree(options) => run_worktree(options).await, + } +} + +pub async fn run_workspace(options: crate::arguments::Options) -> Result<()> { + let mut client = connection::connect(&Connection { + socket: options.socket, + hub: options.hub, + local_profile: options.local_profile, + }) + .await?; + let hub = client.hello().hub_id; + match options.command { + WorkspaceCommand::Create(args) => create(&mut client, hub, args).await, + WorkspaceCommand::Update(args) => update(&mut client, hub, args).await, + WorkspaceCommand::Get(args) => get(&mut client, hub, args).await, + WorkspaceCommand::List(args) => list(&mut client, hub, args).await, + WorkspaceCommand::Members(args) => crate::members::members(&mut client, hub, args).await, } } +pub async fn run_worktree(options: crate::arguments::WorktreeOptions) -> Result<()> { + crate::worktree::run(options).await +} + async fn create(client: &mut LocalClient, hub: HubId, args: CreateArgs) -> Result<()> { let request = CreateWorkspaceRequest::new(CreateWorkspaceFields { schema_version: WORKSPACE_PROTOCOL_SCHEMA_VERSION, @@ -70,13 +74,19 @@ async fn update(client: &mut LocalClient, hub: HubId, args: UpdateArgs) -> Resul schema_version: WORKSPACE_PROTOCOL_SCHEMA_VERSION, request_id: Uuid::new_v4(), address: current.address, - expected_revision: args.expected_revision, + expected_revision: args.expected_revision.unwrap_or(current.revision), name: args.name, labels, members, })?; let destination = current.address.hub_id; - let workspace = match call(client, destination, Rpc::UpdateWorkspace { change: request }).await? { + let workspace = match call( + client, + destination, + Rpc::UpdateWorkspace { change: request }, + ) + .await? + { Reply::Workspace { workspace } => workspace, _ => return Err(unexpected()), }; diff --git a/core/crates/application/workspace/cli/src/constants.rs b/core/crates/application/workspace/cli/src/constants.rs index 9b7261b2..442c4fc3 100644 --- a/core/crates/application/workspace/cli/src/constants.rs +++ b/core/crates/application/workspace/cli/src/constants.rs @@ -1,7 +1,7 @@ pub const COMMANDS: &[&str] = &["workspace", "worktree"]; pub const PROGRAM: &str = "yolo"; pub const SOCKET_PAIR: &str = "--socket and --hub must be supplied together"; -pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run yolo first"; +pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run `yolo server start` first"; pub const INVALID_SELECTOR: &str = "workspace selector must be a workspace id or name"; pub const INVALID_LABEL: &str = "workspace labels must be key=value"; pub const MIXED_MEMBERSHIP: &str = diff --git a/core/crates/application/workspace/cli/src/lib.rs b/core/crates/application/workspace/cli/src/lib.rs index c5eacfc0..b7750bfe 100644 --- a/core/crates/application/workspace/cli/src/lib.rs +++ b/core/crates/application/workspace/cli/src/lib.rs @@ -13,8 +13,14 @@ use std::ffi::OsString; use constants::COMMANDS; +#[cfg(test)] +#[path = "../unit_tests/arguments.rs"] +mod arguments_tests; + const COMMAND_INDEX: usize = 1; +pub use arguments::{Options, WorkspaceCommand, WorktreeCommand, WorktreeOptions}; + pub fn selects(arguments: &[OsString]) -> bool { arguments .get(COMMAND_INDEX) @@ -30,3 +36,19 @@ pub fn run_from(arguments: impl IntoIterator) -> anyhow::Result .build()? .block_on(commands::run(arguments)) } + +/// Runs one already parsed workspace command with its connection selection. +pub fn run_workspace(options: arguments::Options) -> anyhow::Result<()> { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build()? + .block_on(commands::run_workspace(options)) +} + +/// Runs one already parsed worktree command with its connection selection. +pub fn run_worktree(options: arguments::WorktreeOptions) -> anyhow::Result<()> { + tokio::runtime::Builder::new_current_thread() + .enable_all() + .build()? + .block_on(commands::run_worktree(options)) +} diff --git a/core/crates/application/workspace/cli/unit_tests/arguments.rs b/core/crates/application/workspace/cli/unit_tests/arguments.rs new file mode 100644 index 00000000..e02cfe87 --- /dev/null +++ b/core/crates/application/workspace/cli/unit_tests/arguments.rs @@ -0,0 +1,68 @@ +use crate::arguments::{Arguments, Command, WorkspaceCommand, WorktreeCommand}; +use clap::error::ErrorKind; +use clap::Parser; + +mod constants; + +fn workspace_update(extra: &[&str]) -> crate::arguments::UpdateArgs { + let mut arguments = vec![constants::PROGRAM, constants::WORKSPACE, constants::UPDATE, "demo"]; + arguments.extend_from_slice(extra); + let parsed = Arguments::try_parse_from(arguments).unwrap(); + let Command::Workspace(options) = parsed.command else { + panic!("expected workspace") + }; + let WorkspaceCommand::Update(update) = options.command else { + panic!("expected workspace update") + }; + update +} + +#[test] +fn workspace_update_revision_is_optional_and_still_validateable() { + assert_eq!( + workspace_update(&[constants::NAME, "renamed"]).expected_revision, + None + ); + assert_eq!( + workspace_update(&[ + constants::NAME, + "renamed", + constants::EXPECTED_REVISION, + "7" + ]) + .expected_revision, + Some(7) + ); +} + +#[test] +fn worktree_create_requires_the_repository_trust_acknowledgement() { + let missing = Arguments::try_parse_from([ + constants::PROGRAM, + constants::WORKTREE, + constants::CREATE, + constants::PARENT, + constants::NAME, + constants::WORKTREE_NAME, + ]) + .unwrap_err(); + assert_eq!(missing.kind(), ErrorKind::MissingRequiredArgument); + + let parsed = Arguments::try_parse_from([ + constants::PROGRAM, + constants::WORKTREE, + constants::CREATE, + constants::PARENT, + constants::NAME, + constants::WORKTREE_NAME, + constants::TRUST_REPOSITORY, + ]) + .unwrap(); + let Command::Worktree(options) = parsed.command else { + panic!("expected worktree") + }; + let WorktreeCommand::Create(create) = options.command else { + panic!("expected worktree create") + }; + assert!(create.trust_repository); +} diff --git a/core/crates/application/workspace/cli/unit_tests/constants.rs b/core/crates/application/workspace/cli/unit_tests/constants.rs new file mode 100644 index 00000000..73001b94 --- /dev/null +++ b/core/crates/application/workspace/cli/unit_tests/constants.rs @@ -0,0 +1,10 @@ +pub const PROGRAM: &str = "yolo"; +pub const WORKSPACE: &str = "workspace"; +pub const UPDATE: &str = "update"; +pub const NAME: &str = "--name"; +pub const EXPECTED_REVISION: &str = "--expected-revision"; +pub const WORKTREE: &str = "worktree"; +pub const CREATE: &str = "create"; +pub const TRUST_REPOSITORY: &str = "--trust-repository"; +pub const PARENT: &str = "parent"; +pub const WORKTREE_NAME: &str = "worktree-name"; diff --git a/core/crates/application/yolo/BUILD.bazel b/core/crates/application/yolo/BUILD.bazel index 1277088e..d6b8fcdb 100644 --- a/core/crates/application/yolo/BUILD.bazel +++ b/core/crates/application/yolo/BUILD.bazel @@ -14,6 +14,7 @@ _DEPS = [ "//core/crates/plugin/client/host:yolo_builtin_client_host", "//core/crates/application/launch/contract:yolo_launch_contract", "//core/crates/application/attach/cli:yolo_attach_cli", + "//core/crates/application/command:yolo_command_cli", "//core/crates/application/remote/cli:yolo_remote_cli", "//core/crates/application/upgrade/cli:yolo_upgrade_cli", "//core/crates/hub/cli:yolo_hub_cli", diff --git a/core/crates/application/yolo/integration_tests/discovery.rs b/core/crates/application/yolo/integration_tests/discovery.rs index ed811fe7..7de298da 100644 --- a/core/crates/application/yolo/integration_tests/discovery.rs +++ b/core/crates/application/yolo/integration_tests/discovery.rs @@ -28,12 +28,19 @@ async fn protocol_discovery_is_strict() { } #[tokio::test] -async fn web_command_is_dispatched_without_entering_the_tui() { - let help = command::run(&[WEB_COMMAND, HELP], None).await; - assert_eq!(help.code, SUCCESS, "{}", help.stderr); - assert!(help.stdout.contains(WEB_CLIENT_KEY_OPTION)); - assert!(help.stdout.contains(WEB_ORIGIN_OPTION)); - let missing = command::run(&[WEB_COMMAND], None).await; - assert_eq!(missing.code, USAGE); - assert!(missing.stderr.contains(WEB_PROFILE_OPTION)); +async fn root_options_before_auxiliary_commands_still_dispatch() { + let agent = command::run( + &[LOCAL_PROFILE_OPTION, MISSING_PROFILE, AGENT_COMMAND, LIST], + None, + ) + .await; + assert_eq!(agent.code, FAILURE, "{}", agent.stderr); + assert!(!agent.stderr.contains("unrecognized subcommand")); + let update = command::run( + &[LOCAL_PROFILE_OPTION, MISSING_PROFILE, UPDATE_ENTRY, HELP], + None, + ) + .await; + assert_eq!(update.code, SUCCESS, "{}", update.stderr); + assert!(update.stdout.contains(UPDATE_USAGE)); } diff --git a/core/crates/application/yolo/src/dispatch.rs b/core/crates/application/yolo/src/dispatch.rs index eac9e442..3b51de9f 100644 --- a/core/crates/application/yolo/src/dispatch.rs +++ b/core/crates/application/yolo/src/dispatch.rs @@ -1,18 +1,8 @@ use yolo_launch_contract::{Invocation, ProcessRole}; pub fn run(invocation: Invocation) -> anyhow::Result<()> { - let arguments = invocation.arguments; - match invocation.role { - None if yolo_agent_cli::selects(&arguments) => yolo_agent_cli::run_from(arguments), - None if yolo_conversation_cli::selects(&arguments) => { - yolo_conversation_cli::run_from(arguments) - } - None if yolo_workspace_cli::selects(&arguments) => yolo_workspace_cli::run_from(arguments), - None if yolo_protocol_cli::selects(&arguments) => yolo_protocol_cli::run_from(arguments), - None if yolo_trusted_plugin_cli::selects(&arguments) => { - yolo_trusted_plugin_cli::run_from(arguments) - } - None if yolo_upgrade_cli::selects(&arguments) => yolo_upgrade_cli::run_from(arguments), + let Invocation { role, arguments } = invocation; + match role { None => { let host = yolo_builtin_client_host::BuiltinClientHost::builtin(vec![ yolo_tui_descriptor::tui_descriptor(), diff --git a/core/crates/application/yolo/test_support/constants.rs b/core/crates/application/yolo/test_support/constants.rs index a1337849..4712071d 100644 --- a/core/crates/application/yolo/test_support/constants.rs +++ b/core/crates/application/yolo/test_support/constants.rs @@ -27,7 +27,7 @@ pub const CONVERSATION_READ: &str = "read"; pub const CONVERSATION_WATCH: &str = "watch"; pub const CONVERSATION_HUB_OPTION: &str = "--hub"; pub const CONVERSATION_SOCKET_OPTION: &str = "--socket"; -pub const WEB_COMMAND: &str = "web"; -pub const WEB_CLIENT_KEY_OPTION: &str = "--client-key"; -pub const WEB_ORIGIN_OPTION: &str = "--origin"; -pub const WEB_PROFILE_OPTION: &str = "--profile"; +pub const LOCAL_PROFILE_OPTION: &str = "--local-profile"; +pub const MISSING_PROFILE: &str = "/nonexistent/yolo-profile.json"; +pub const UPDATE_ENTRY: &str = "update"; +pub const UPDATE_USAGE: &str = "Usage: yolo update"; diff --git a/core/crates/conversation/cli/src/arguments.rs b/core/crates/conversation/cli/src/arguments.rs index df6a8399..18034a7a 100644 --- a/core/crates/conversation/cli/src/arguments.rs +++ b/core/crates/conversation/cli/src/arguments.rs @@ -21,38 +21,43 @@ pub(crate) enum Command { } #[derive(Debug, Args)] -pub(crate) struct Options { +pub struct Options { /// Existing local Hub entry socket; supply together with --hub. - #[arg(long = SOCKET_ARGUMENT, requires = HUB_ARGUMENT)] + #[arg(long = SOCKET_ARGUMENT, requires = HUB_ARGUMENT, help_heading = "Connection")] pub socket: Option, /// Identity of the Hub accepting this client connection. - #[arg(long = HUB_ARGUMENT, requires = SOCKET_ARGUMENT)] + #[arg(long = HUB_ARGUMENT, requires = SOCKET_ARGUMENT, help_heading = "Connection")] pub hub: Option, /// Local profile file; defaults to the current user profile. - #[arg(long = LOCAL_PROFILE_ARGUMENT, conflicts_with_all = [SOCKET_ARGUMENT, HUB_ARGUMENT])] + #[arg(long = LOCAL_PROFILE_ARGUMENT, conflicts_with_all = [SOCKET_ARGUMENT, HUB_ARGUMENT], help_heading = "Connection")] pub local_profile: Option, #[command(subcommand)] pub action: Action, } #[derive(Debug, Subcommand)] -pub(crate) enum Action { +pub enum Action { /// Resolve the current agent execution to its native conversation. Resolve { + /// Agent id whose current execution is resolved. #[arg(long)] agent: AgentId, + /// Hub id that owns the conversation; defaults to the entry Hub. #[arg(long)] origin: Option, }, /// Read the latest page for --agent, or a typed read request from stdin. Read { + /// Agent id to read; omit to read a typed request from stdin. #[arg(long)] agent: Option, + /// Hub id that owns the conversation; requires --agent. #[arg(long, requires = AGENT_OPTION)] origin: Option, }, /// Read a typed conditional request from stdin and wait once for a change. Watch { + /// Maximum milliseconds to wait for a change. #[arg(long, default_value_t = yolo_conversation_protocol::MAX_WATCH_WAIT_MS, value_parser = clap::value_parser!(u32).range(0..=i64::from(yolo_conversation_protocol::MAX_WATCH_WAIT_MS)))] wait_ms: u32, diff --git a/core/crates/conversation/cli/src/constants.rs b/core/crates/conversation/cli/src/constants.rs index 47a3ed84..cfc01b48 100644 --- a/core/crates/conversation/cli/src/constants.rs +++ b/core/crates/conversation/cli/src/constants.rs @@ -4,5 +4,5 @@ pub const AGENT_OPTION: &str = "agent"; pub const SOCKET_ARGUMENT: &str = "socket"; pub const HUB_ARGUMENT: &str = "hub"; pub const LOCAL_PROFILE_ARGUMENT: &str = "local-profile"; -pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run yolo first"; +pub const NO_LOCAL_HUB: &str = "No local Hub has been started; run `yolo server start` first"; pub const SOCKET_PAIR: &str = "--socket and --hub must be supplied together"; diff --git a/core/crates/conversation/cli/src/lib.rs b/core/crates/conversation/cli/src/lib.rs index 3427398d..ea2e2ed4 100644 --- a/core/crates/conversation/cli/src/lib.rs +++ b/core/crates/conversation/cli/src/lib.rs @@ -7,6 +7,7 @@ mod runner; use clap::Parser; use std::{ffi::OsString, io::Write}; +pub use arguments::{Action, Options}; pub use constants::COMMAND; const COMMAND_INDEX: usize = 1; @@ -20,6 +21,11 @@ pub fn selects(arguments: &[OsString]) -> bool { pub fn run_from(arguments: impl IntoIterator) -> anyhow::Result<()> { let arguments = arguments::Arguments::try_parse_from(arguments)?; let arguments::Command::Conversation(options) = arguments.command; + run(options) +} + +/// Runs one already parsed conversation command with its connection selection. +pub fn run(options: arguments::Options) -> anyhow::Result<()> { let request = document::request(&options.action, std::io::stdin().lock())?; let reply = tokio::runtime::Builder::new_current_thread() .enable_all() diff --git a/core/crates/hub/BUILD.bazel b/core/crates/hub/BUILD.bazel index 59ab7ca4..66d9ad2e 100644 --- a/core/crates/hub/BUILD.bazel +++ b/core/crates/hub/BUILD.bazel @@ -27,8 +27,6 @@ test_suite( "//core/crates/hub/ssh/entry:integration_tests", "//core/crates/hub/observation/store:unit_tests", "//core/crates/hub/observation/store:integration_tests", - "//core/crates/hub/listener/binding:unit_tests", - "//core/crates/hub/listener/binding:integration_tests", "//core/crates/hub/broker/launcher:unit_tests", "//core/crates/hub/remote/setup/store:unit_tests", "//core/crates/hub/remote/setup/store:integration_tests", @@ -100,8 +98,6 @@ test_suite( "//core/crates/hub/management/runtime:unit_tests", "//core/crates/hub/management/runtime:integration_tests", "//core/crates/hub/peer/protocol:unit_tests", - "//core/crates/hub/peer/transport:integration_tests", - "//core/crates/hub/peer/transport:unit_tests", "//core/crates/hub/publication:unit_tests", "//core/crates/hub/routing:integration_tests", "//core/crates/hub/routing:unit_tests", diff --git a/core/crates/hub/agent/run/protocol/src/run.rs b/core/crates/hub/agent/run/protocol/src/run.rs index 48f41c48..08f18594 100644 --- a/core/crates/hub/agent/run/protocol/src/run.rs +++ b/core/crates/hub/agent/run/protocol/src/run.rs @@ -14,15 +14,18 @@ pub struct AgentRunAddress { } /// Fill each run's `current` from the observed terminal-to-execution pairs. -pub fn annotate_current( - runs: &mut [AgentRunSummary], - observed: &[(TerminalId, AgentTarget)], -) { +pub fn annotate_current(runs: &mut [AgentRunSummary], observed: &[(TerminalId, AgentTarget)]) { let current: BTreeMap = observed.iter().copied().collect(); for run in runs { run.current = run .terminal - .and_then(|terminal| current.get(&terminal.terminal_id).copied()); + .filter(|_| run.state == AgentRunState::Launched) + .and_then(|terminal| { + current + .get(&terminal.terminal_id) + .copied() + .filter(|target| target.address.hub_id == terminal.hub_id) + }); } } @@ -33,7 +36,7 @@ pub enum AgentRunState { Starting, /// The terminal was created; the provider binding may still be awaiting. Launched, - /// The run's terminal was stopped through `agent_stop`. + /// The Host confirmed the terminal ended or was archived, or the launch was cancelled. Stopped, } diff --git a/core/crates/hub/agent/run/store/src/lib.rs b/core/crates/hub/agent/run/store/src/lib.rs index 424f8447..517e9c51 100644 --- a/core/crates/hub/agent/run/store/src/lib.rs +++ b/core/crates/hub/agent/run/store/src/lib.rs @@ -4,12 +4,14 @@ mod adopt; mod cancel; mod error; +mod lifecycle; mod ops; mod quota; mod record; mod store; pub use error::StoreError; +pub use lifecycle::TerminalObservation; pub use record::ReserveAgentRun; pub use store::AgentRunStore; diff --git a/core/crates/hub/agent/run/store/src/lifecycle.rs b/core/crates/hub/agent/run/store/src/lifecycle.rs new file mode 100644 index 00000000..3e9cc8f6 --- /dev/null +++ b/core/crates/hub/agent/run/store/src/lifecycle.rs @@ -0,0 +1,183 @@ +use crate::{ + AgentRunStore, StoreError, + record::{StoredRun, now_ms, summary}, +}; +use std::collections::{BTreeMap, BTreeSet}; +use uuid::Uuid; +use yolo_hub_agent_run_protocol::{AgentRunAddress, AgentRunState, AgentRunSummary}; +use yolo_hub_identity::TerminalAddress; +use yolo_hub_terminal_protocol::PublicError; + +/// The Host's evidence about a terminal and, when present, its PTY incarnation. +#[derive(Clone, Copy)] +pub enum TerminalObservation { + Live(Uuid), + Ended(Uuid), + Archived, +} + +impl AgentRunStore { + pub fn launched_terminals(&self) -> Result, StoreError> { + self.check()?; + Ok(self + .record + .runs + .iter() + .filter(|run| run.state == AgentRunState::Launched) + .filter_map(|run| run.terminal) + .collect()) + } + + pub fn observe_terminals( + &mut self, + observations: &BTreeMap, + ) -> Result<(), StoreError> { + let changes = self + .record + .runs + .iter() + .enumerate() + .filter_map(|(index, run)| { + let observation = observations.get(&run.terminal?)?; + observed_transition(run, *observation).map(|change| (index, change)) + }) + .collect::>(); + if changes.is_empty() { + return Ok(()); + } + self.check()?; + let mut record = self.record.clone(); + let now = now_ms(); + for (index, (state, ended, observed, archived)) in changes { + transition( + &mut record.runs[index], + state, + ended, + observed, + archived, + now, + )?; + } + self.commit_record(record) + } + + /// Record an explicit stop after the Host confirms the effect. + pub fn mark_stopped( + &mut self, + address: AgentRunAddress, + ) -> Result { + self.mark_stopped_if_current(address, None, None, None) + } + + pub fn mark_stopped_if_current( + &mut self, + address: AgentRunAddress, + terminal: Option, + expected_runtime: Option, + expected_observed_runtime: Option>, + ) -> Result { + self.check()?; + let mut run = self + .record + .find(address) + .cloned() + .ok_or(PublicError::NotFound)?; + if terminal.is_some() && run.terminal != terminal { + return Err(PublicError::Conflict.into()); + } + if let Some(expected) = expected_observed_runtime { + if run.observed_runtime != expected { + return Err(PublicError::Conflict.into()); + } + } + if run.state == AgentRunState::Stopped { + if run.ended_runtime.is_none() && expected_runtime.is_none() { + return Ok(summary(&run)); + } + if run.ended_runtime == expected_runtime + && expected_runtime.is_some() + && expected_observed_runtime.is_none_or(|expected| run.observed_runtime == expected) + { + return Ok(summary(&run)); + } + return Err(PublicError::Conflict.into()); + } + if run.state == AgentRunState::Starting || run.terminal.is_none() { + return Err(PublicError::Conflict.into()); + } + let ended = expected_runtime.or(run.ended_runtime); + let observed = expected_observed_runtime + .flatten() + .or(expected_runtime) + .or(run.observed_runtime); + let archived = run.terminal_archived; + transition( + &mut run, + AgentRunState::Stopped, + ended, + observed, + archived, + now_ms(), + )?; + self.commit(run.clone())?; + self.active_starts.remove(&address); + Ok(summary(&run)) + } +} + +fn observed_transition( + run: &StoredRun, + observation: TerminalObservation, +) -> Option<(AgentRunState, Option, Option, bool)> { + use AgentRunState::{Launched, Stopped}; + use TerminalObservation::{Archived, Ended, Live}; + if run.terminal_archived { + return None; + } + match (run.state, run.observed_runtime, observation) { + (Launched, None, Live(epoch)) => Some((Launched, None, Some(epoch), false)), + (AgentRunState::Starting, _, Live(epoch)) => { + Some((AgentRunState::Starting, None, Some(epoch), false)) + } + (Launched, observed, Live(epoch)) if observed == Some(epoch) => None, + (Launched, Some(_), Live(epoch)) => Some((Launched, None, Some(epoch), false)), + (Launched, Some(observed), Ended(epoch)) if observed == epoch => { + Some((Stopped, Some(epoch), Some(epoch), false)) + } + (Launched, None, Ended(epoch)) => Some((Stopped, Some(epoch), Some(epoch), false)), + (Stopped, observed, Ended(epoch)) + if run.ended_runtime == Some(epoch) && observed == Some(epoch) => + { + None + } + (Stopped, _, Ended(_)) => None, + (Stopped, Some(ended), Live(epoch)) + if run.ended_runtime == Some(ended) && ended != epoch => + { + Some((Launched, None, Some(epoch), false)) + } + (Launched, observed, Archived) => Some((Stopped, observed, observed, true)), + (Stopped, _, Archived) => Some((Stopped, run.ended_runtime, run.observed_runtime, true)), + _ => None, + } +} + +fn transition( + run: &mut StoredRun, + state: AgentRunState, + ended_runtime: Option, + observed_runtime: Option, + terminal_archived: bool, + now: i64, +) -> Result<(), StoreError> { + run.state = state; + run.ended_runtime = ended_runtime; + run.observed_runtime = observed_runtime; + run.terminal_archived = terminal_archived; + run.revision = run + .revision + .checked_add(1) + .ok_or(PublicError::LimitExceeded)?; + run.updated_at_ms = now.max(run.updated_at_ms); + Ok(()) +} diff --git a/core/crates/hub/agent/run/store/src/ops.rs b/core/crates/hub/agent/run/store/src/ops.rs index 65022687..d797bfb1 100644 --- a/core/crates/hub/agent/run/store/src/ops.rs +++ b/core/crates/hub/agent/run/store/src/ops.rs @@ -9,9 +9,7 @@ use yolo_hub_identity::TerminalAddress; use yolo_hub_terminal_protocol::PublicError; use crate::StoreError; -use crate::record::{ - ReserveAgentRun, StoredRun, matches_filter, now_ms, stored_run, summary, -}; +use crate::record::{ReserveAgentRun, StoredRun, matches_filter, now_ms, stored_run, summary}; use crate::store::AgentRunStore; impl AgentRunStore { @@ -71,6 +69,9 @@ impl AgentRunStore { } return Err(PublicError::Conflict.into()); } + if current.state != AgentRunState::Starting { + return Err(PublicError::Conflict.into()); + } let next = StoredRun { revision: current.revision + 1, state: AgentRunState::Launched, @@ -91,40 +92,24 @@ impl AgentRunStore { &self, selector: &AgentRunSelector, ) -> Result, StoreError> { - self.check()?; - Ok(match selector { - AgentRunSelector::Address(address) => self.record.find(*address).map(summary), - AgentRunSelector::Name(name) => self.record.find_name(name).map(summary), - }) + Ok(self.select(selector)?.map(summary)) } - /// Record a stop after the terminal effect was observed. Idempotent: an - /// already-stopped run keeps its revision and timestamps. - pub fn mark_stopped( - &mut self, - address: AgentRunAddress, - ) -> Result { + pub fn stop_target( + &self, + selector: &AgentRunSelector, + ) -> Result, Option)>, StoreError> { + Ok(self + .select(selector)? + .map(|run| (summary(run), run.ended_runtime, run.observed_runtime))) + } + + fn select(&self, selector: &AgentRunSelector) -> Result, StoreError> { self.check()?; - let current = self - .record - .find(address) - .cloned() - .ok_or(PublicError::NotFound)?; - if current.state == AgentRunState::Stopped { - return Ok(summary(¤t)); - } - if current.state == AgentRunState::Starting || current.terminal.is_none() { - return Err(PublicError::Conflict.into()); - } - let next = StoredRun { - revision: current.revision + 1, - state: AgentRunState::Stopped, - updated_at_ms: now_ms(), - ..current - }; - self.commit(next.clone())?; - self.active_starts.remove(&address); - Ok(summary(&next)) + Ok(match selector { + AgentRunSelector::Address(address) => self.record.find(*address), + AgentRunSelector::Name(name) => self.record.find_name(name), + }) } pub fn list( diff --git a/core/crates/hub/agent/run/store/src/record.rs b/core/crates/hub/agent/run/store/src/record.rs index 6fcab0ce..0a080325 100644 --- a/core/crates/hub/agent/run/store/src/record.rs +++ b/core/crates/hub/agent/run/store/src/record.rs @@ -35,6 +35,14 @@ pub(crate) struct StoredRun { pub(crate) parent: Option, pub(crate) terminal: Option, pub(crate) state: AgentRunState, + /// A stopped run can follow a replacement PTY after an observed exit. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub(crate) ended_runtime: Option, + /// Latest runtime incarnation accepted by the observation fence. + #[serde(default, skip_serializing_if = "Option::is_none")] + pub(crate) observed_runtime: Option, + #[serde(default)] + pub(crate) terminal_archived: bool, pub(crate) start_token: Uuid, pub(crate) created_at_ms: i64, pub(crate) updated_at_ms: i64, @@ -70,7 +78,8 @@ impl RunRecord { for run in &self.runs { ensure!(run.address.hub_id == hub_id, "agent run origin mismatch"); ensure!( - run.workspace.is_none_or(|workspace| workspace.hub_id == hub_id), + run.workspace + .is_none_or(|workspace| workspace.hub_id == hub_id), "agent run workspace origin mismatch" ); if let Some(parent) = run.parent { @@ -85,6 +94,17 @@ impl RunRecord { ensure!(ids.insert(run.address.run_id), "duplicate agent run id"); ensure!(tokens.insert(run.start_token), "duplicate start token"); ensure!(run.revision >= FIRST_AGENT_RUN_REVISION, "invalid revision"); + ensure!( + run.ended_runtime.is_none_or(|epoch| !epoch.is_nil() + && run.state == AgentRunState::Stopped + && run.terminal.is_some()), + "invalid ended runtime" + ); + ensure!( + run.observed_runtime + .is_none_or(|epoch| !epoch.is_nil() && run.terminal.is_some()), + "invalid observed runtime" + ); validate_name(&run.name).map_err(|_| anyhow::anyhow!("invalid run name"))?; validate_aliases(&run.aliases).map_err(|_| anyhow::anyhow!("invalid aliases"))?; validate_labels(&run.labels).map_err(|_| anyhow::anyhow!("invalid labels"))?; @@ -140,6 +160,9 @@ pub(crate) fn stored_run( parent: request.parent, terminal: None, state: AgentRunState::Starting, + ended_runtime: None, + observed_runtime: None, + terminal_archived: false, start_token: request.start_token, created_at_ms: now, updated_at_ms: now, @@ -170,9 +193,4 @@ pub(crate) fn summary(run: &StoredRun) -> AgentRunSummary { } } -pub(crate) fn now_ms() -> i64 { - std::time::SystemTime::now() - .duration_since(std::time::UNIX_EPOCH) - .map(|duration| duration.as_millis() as i64) - .unwrap_or_default() -} +pub(crate) fn now_ms() -> i64 { std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).map_or(0, |d| d.as_millis() as i64) } diff --git a/core/crates/hub/agent/run/store/unit_tests/reconcile.rs b/core/crates/hub/agent/run/store/unit_tests/reconcile.rs index eec85a8d..1f3880e1 100644 --- a/core/crates/hub/agent/run/store/unit_tests/reconcile.rs +++ b/core/crates/hub/agent/run/store/unit_tests/reconcile.rs @@ -1,20 +1,105 @@ use super::*; +use std::collections::BTreeMap; + +#[test] +fn observed_exit_releases_quota_and_only_a_replacement_runtime_reopens_the_run() { + let (root, hub, mut store) = fixture(); + let terminal = TerminalAddress { + hub_id: hub, + terminal_id: TerminalId::new(), + }; + let awaiting = TerminalAddress { + terminal_id: TerminalId::new(), + ..terminal + }; + let original = store.adopt(terminal, "codex").unwrap().unwrap(); + let reserved = reserve(&mut store, "awaiting", Uuid::new_v4()); + let awaiting_run = store.mark_launched(reserved.address, awaiting).unwrap(); + let selector = AgentRunSelector::Address(original.address); + let epoch = Uuid::new_v4(); + let ended = BTreeMap::from([ + (terminal, TerminalObservation::Ended(epoch)), + (awaiting, TerminalObservation::Live(Uuid::new_v4())), + ]); + + store + .observe_terminals(&BTreeMap::from([( + terminal, + TerminalObservation::Live(epoch), + )])) + .unwrap(); + store.observe_terminals(&ended).unwrap(); + let stopped = get(&store, selector.clone()).unwrap(); + assert_eq!(stopped.state, AgentRunState::Stopped); + assert_eq!(stopped.revision, original.revision + 2); + assert_eq!(store.launched_terminals().unwrap(), [awaiting].into()); + assert!(store.mark_launched(original.address, terminal).is_err()); + let awaiting_after_observation = + get(&store, AgentRunSelector::Address(awaiting_run.address)).unwrap(); + assert_eq!(awaiting_after_observation.address, awaiting_run.address); + assert_eq!(awaiting_after_observation.state, AgentRunState::Launched); + assert_eq!( + awaiting_after_observation.revision, + awaiting_run.revision + 1 + ); + store.observe_terminals(&ended).unwrap(); + assert_eq!(get(&store, selector.clone()), Some(stopped.clone())); + + drop(store); + let path = root.path().canonicalize().unwrap().join("agent-runs"); + let mut store = AgentRunStore::open( + yolo_paths::directory::PrivateDirectory::ensure(&path).unwrap(), + hub, + ) + .unwrap(); + let observe = |store: &mut AgentRunStore, report| { + store + .observe_terminals(&BTreeMap::from([(terminal, report)])) + .unwrap(); + get(store, selector.clone()).unwrap() + }; + assert_eq!( + observe(&mut store, TerminalObservation::Live(epoch)), + stopped + ); + let replacement = observe(&mut store, TerminalObservation::Live(Uuid::new_v4())); + assert_eq!(replacement.address, original.address); + assert_eq!(replacement.state, AgentRunState::Launched); + assert_eq!(replacement.revision, stopped.revision + 1); + let current_epoch = get(&store, selector.clone()).unwrap(); + store + .observe_terminals(&BTreeMap::from([( + terminal, + TerminalObservation::Ended(epoch), + )])) + .unwrap(); + assert_eq!(get(&store, selector.clone()).unwrap(), current_epoch); + let archived = observe(&mut store, TerminalObservation::Archived); + assert_eq!(archived.state, AgentRunState::Stopped); + let after_archive = observe(&mut store, TerminalObservation::Live(Uuid::new_v4())); + assert_eq!(after_archive.state, AgentRunState::Stopped); + assert_eq!(after_archive.revision, archived.revision); +} #[test] fn reopen_keeps_terminal_less_starting_runs_resumable() { let root = tempfile::tempdir().unwrap(); let path = root.path().canonicalize().unwrap().join("agent-runs"); let hub = HubId::new(); - let mut store = - AgentRunStore::open(yolo_paths::directory::PrivateDirectory::ensure(&path).unwrap(), hub) - .unwrap(); + let mut store = AgentRunStore::open( + yolo_paths::directory::PrivateDirectory::ensure(&path).unwrap(), + hub, + ) + .unwrap(); let starting = reserve(&mut store, "starting", Uuid::new_v4()); let launched = reserve(&mut store, "launched", Uuid::new_v4()); store.mark_launched(launched.address, terminal()).unwrap(); drop(store); - let reopened = - AgentRunStore::open(yolo_paths::directory::PrivateDirectory::ensure(&path).unwrap(), hub) - .unwrap(); + let reopened = AgentRunStore::open( + yolo_paths::directory::PrivateDirectory::ensure(&path).unwrap(), + hub, + ) + .unwrap(); let get = |address| { reopened .get( @@ -30,4 +115,3 @@ fn reopen_keeps_terminal_less_starting_runs_resumable() { assert_eq!(get(starting.address).state, AgentRunState::Starting); assert_eq!(get(launched.address).state, AgentRunState::Launched); } - diff --git a/core/crates/hub/architecture_check_data.bzl b/core/crates/hub/architecture_check_data.bzl index 84a27d25..b600be75 100644 --- a/core/crates/hub/architecture_check_data.bzl +++ b/core/crates/hub/architecture_check_data.bzl @@ -6,6 +6,8 @@ ARCHITECTURE_CHECK_DATA = [ "//core/crates/hub/connector/jsonrpc:architecture_check_data", "//core/crates/hub/terminal/wire:architecture_check_data", "//core/crates/hub/peer/rpc:architecture_check_data", + "//core/crates/hub/peer/io:architecture_check_data", + "//core/crates/hub/peer/link:architecture_check_data", "//core/crates/hub/client/access/protocol:architecture_check_data", "//core/crates/hub/client/access/registry:architecture_check_data", "//core/crates/hub/client/access/store:architecture_check_data", @@ -15,7 +17,6 @@ ARCHITECTURE_CHECK_DATA = [ "//core/crates/hub/public/client/server:architecture_check_data", "//core/crates/hub/ssh/entry:architecture_check_data", "//core/crates/hub/observation/store:architecture_check_data", - "//core/crates/hub/listener/binding:architecture_check_data", "//core/crates/hub/broker/launcher:architecture_check_data", "//core/crates/hub/remote/setup/protocol:architecture_check_data", "//core/crates/hub/remote/setup/store:architecture_check_data", @@ -75,7 +76,6 @@ ARCHITECTURE_CHECK_DATA = [ "//core/crates/hub/management/client:architecture_check_data", "//core/crates/hub/management/runtime:architecture_check_data", "//core/crates/hub/peer/protocol:architecture_check_data", - "//core/crates/hub/peer/transport:architecture_check_data", "//core/crates/hub/publication:architecture_check_data", "//core/crates/hub/routing:architecture_check_data", "//core/crates/hub/rpc/protocol:architecture_check_data", diff --git a/core/crates/hub/cli/BUILD.bazel b/core/crates/hub/cli/BUILD.bazel index 710ff984..1288561b 100644 --- a/core/crates/hub/cli/BUILD.bazel +++ b/core/crates/hub/cli/BUILD.bazel @@ -38,6 +38,7 @@ rust_library( "//core/crates/transport/client:yolo_client", "//core/crates/hub/local/startup:yolo_hub_local_startup", "//core/crates/hub/management/runtime:yolo_hub_management_runtime", + "//core/crates/hub/peer/dialer:yolo_hub_peer_dialer", "//core/crates/hub/remote/startup:yolo_hub_remote_startup", "//core/crates/transport/remote/provisioning/protocol:yolo_remote_provisioning_protocol", "//core/crates/hub/ssh/profile/runtime:yolo_hub_ssh_profile_runtime", diff --git a/core/crates/hub/cli/src/constants.rs b/core/crates/hub/cli/src/constants.rs index e768de32..65cc47b4 100644 --- a/core/crates/hub/cli/src/constants.rs +++ b/core/crates/hub/cli/src/constants.rs @@ -9,6 +9,10 @@ pub const HOST_SOCKET_OPTION: &str = "host-socket"; pub const PEER_CONFIG_OPTION: &str = "peer-config"; pub const CLIENT_ACCESS_OPTION: &str = "client-access"; pub const SSH_PROFILES_OPTION: &str = "ssh-profiles"; +pub const PEER_STDIO_OPTION: &str = "peer-stdio"; +pub const PEER_STDIO_FIELD: &str = "peer_stdio"; +pub const PEER_SOCKET_OPTION: &str = "peer-socket"; +pub const PEER_SOCKET_MISSING: &str = "peer-stdio requires --peer-socket"; pub const BOOTSTRAP_OPTION: &str = "bootstrap"; pub const HOST_EXECUTABLE_OPTION: &str = "host-executable"; pub const HOST_EXECUTABLE_FIELD: &str = "host_executable"; diff --git a/core/crates/hub/cli/src/entry.rs b/core/crates/hub/cli/src/entry.rs index 887ffe52..4320b529 100644 --- a/core/crates/hub/cli/src/entry.rs +++ b/core/crates/hub/cli/src/entry.rs @@ -1,4 +1,5 @@ -use crate::{bootstrap, options::Options, remote, serve}; +use crate::{bootstrap, constants::PEER_SOCKET_MISSING, options::Options, peer_stdio, remote, serve}; +use anyhow::Context; use clap::Parser; use std::ffi::OsString; @@ -17,6 +18,9 @@ pub fn run_from(argv: impl IntoIterator) -> anyhow::Result<()> } async fn run(options: Options) -> anyhow::Result<()> { + if options.peer_stdio { + return peer_stdio::run(options.peer_socket.context(PEER_SOCKET_MISSING)?).await; + } if options.install_bundle || options.inspect_install || options.remote_bootstrap diff --git a/core/crates/hub/cli/src/lib.rs b/core/crates/hub/cli/src/lib.rs index b0d2186f..b41661c6 100644 --- a/core/crates/hub/cli/src/lib.rs +++ b/core/crates/hub/cli/src/lib.rs @@ -2,6 +2,8 @@ mod bootstrap; mod constants; mod entry; mod options; +mod origin_services; +mod peer_stdio; mod remote; mod serve; mod services; diff --git a/core/crates/hub/cli/src/options.rs b/core/crates/hub/cli/src/options.rs index fbc9dfb6..8d4e2f9f 100644 --- a/core/crates/hub/cli/src/options.rs +++ b/core/crates/hub/cli/src/options.rs @@ -6,10 +6,14 @@ use std::path::PathBuf; #[command(name = PROGRAM, version, about = DESCRIPTION)] #[command(group(ArgGroup::new(REMOTE_ACTION_GROUP).args([INSTALL_BUNDLE_FIELD, INSPECT_INSTALL_FIELD, REMOTE_BOOTSTRAP_FIELD, REMOTE_RECOVERY_FIELD]).multiple(false)))] pub(crate) struct Options { - #[arg(long = STATE_OPTION, required_unless_present_any = [INSTALL_BUNDLE_FIELD, INSPECT_INSTALL_FIELD])] + #[arg(long = STATE_OPTION, required_unless_present_any = [INSTALL_BUNDLE_FIELD, INSPECT_INSTALL_FIELD, PEER_STDIO_FIELD])] pub state: Option, - #[arg(long = HOST_HOME_OPTION, required_unless_present_any = [INSTALL_BUNDLE_FIELD, INSPECT_INSTALL_FIELD])] + #[arg(long = HOST_HOME_OPTION, required_unless_present_any = [INSTALL_BUNDLE_FIELD, INSPECT_INSTALL_FIELD, PEER_STDIO_FIELD])] pub host_home: Option, + #[arg(long = PEER_STDIO_OPTION, hide = true, conflicts_with_all = [BOOTSTRAP_FIELD, REMOTE_BOOTSTRAP_FIELD, REMOTE_RECOVERY_FIELD, INSTALL_BUNDLE_FIELD, INSPECT_INSTALL_FIELD, READY_FIELD])] + pub peer_stdio: bool, + #[arg(long = PEER_SOCKET_OPTION, hide = true, requires = PEER_STDIO_FIELD, value_name = "PATH")] + pub peer_socket: Option, #[arg(long = HOST_SOCKET_OPTION)] pub host_socket: Option, #[arg(long = PEER_CONFIG_OPTION)] diff --git a/core/crates/hub/cli/src/origin_services.rs b/core/crates/hub/cli/src/origin_services.rs new file mode 100644 index 00000000..883c356f --- /dev/null +++ b/core/crates/hub/cli/src/origin_services.rs @@ -0,0 +1,31 @@ +use std::sync::Arc; +use yolo_hub_dispatch::{Dispatcher, OriginDecorator}; +use yolo_hub_identity::HubId; +use yolo_hub_local_backend::LocalBackend; +use yolo_hub_local_providers::ProviderDispatcher; +use yolo_hub_local_terminal_history::HistoryDispatcher; +use yolo_hub_rpc_protocol::ProviderSummary; + +/// Origin services decorate the local backend for calls entering through a +/// peer link: terminal history and provider control share the local Host. +pub(crate) struct OriginServices { + pub(crate) hub: HubId, + pub(crate) host: yolo_client::Client, + pub(crate) providers: Vec, +} + +impl OriginDecorator for OriginServices { + fn decorate(&self, backend: LocalBackend) -> Arc { + let history = Arc::new(HistoryDispatcher::new( + Arc::new(backend), + self.hub, + self.host.clone(), + )); + Arc::new(ProviderDispatcher::new( + history, + self.hub, + self.host.clone(), + self.providers.clone(), + )) + } +} diff --git a/core/crates/hub/cli/src/peer_stdio.rs b/core/crates/hub/cli/src/peer_stdio.rs new file mode 100644 index 00000000..750cc1af --- /dev/null +++ b/core/crates/hub/cli/src/peer_stdio.rs @@ -0,0 +1,16 @@ +//! Hidden stdio-to-peer-socket bridge used by the SSH peer transport. +use anyhow::{Context, Result}; +use std::path::PathBuf; +use tokio::io::{copy_bidirectional, join}; +use tokio::net::UnixStream; + +const CONNECT_FAILED: &str = "connect the SSH peer stream to the local peer socket"; + +pub(crate) async fn run(socket: PathBuf) -> Result<()> { + let mut stream = UnixStream::connect(&socket) + .await + .context(CONNECT_FAILED)?; + let mut terminal = join(tokio::io::stdin(), tokio::io::stdout()); + copy_bidirectional(&mut terminal, &mut stream).await?; + Ok(()) +} diff --git a/core/crates/hub/cli/src/serve.rs b/core/crates/hub/cli/src/serve.rs index b489972d..9b5490ce 100644 --- a/core/crates/hub/cli/src/serve.rs +++ b/core/crates/hub/cli/src/serve.rs @@ -1,3 +1,4 @@ +use crate::origin_services::OriginServices; use crate::{constants, options::Options, services}; use anyhow::{Context, ensure}; use std::io::Write; @@ -113,7 +114,10 @@ pub(crate) async fn run(options: Options) -> anyhow::Result<()> { backend, config, Some(build), - profiles.clone(), + Arc::new(yolo_hub_peer_dialer::CompositeDialer::new( + yolo_hub_peer_dialer::SystemSshDialer::default(), + profiles.clone(), + )), origin, ) .await?; @@ -169,31 +173,9 @@ pub(crate) async fn run(options: Options) -> anyhow::Result<()> { println!("Yolo Hub {hub}\n{}\n{public_identity}", socket.display()); println!("{}", connectors.socket_path().display()); println!("{}", public.socket_path().display()); - if let Some(address) = service.listen_address()? { - println!("{address}"); + if let Ok(peer_socket) = service.peer_socket() { + println!("{}", peer_socket.display()); } } services::run(server, connectors, service, public, access, terminate).await } - -struct OriginServices { - hub: yolo_hub_identity::HubId, - host: yolo_client::Client, - providers: Vec, -} - -impl yolo_hub_dispatch::OriginDecorator for OriginServices { - fn decorate(&self, backend: LocalBackend) -> Arc { - let history = Arc::new(yolo_hub_local_terminal_history::HistoryDispatcher::new( - Arc::new(backend), - self.hub, - self.host.clone(), - )); - Arc::new(yolo_hub_local_providers::ProviderDispatcher::new( - history, - self.hub, - self.host.clone(), - self.providers.clone(), - )) - } -} diff --git a/core/crates/hub/configuration/store/src/constants.rs b/core/crates/hub/configuration/store/src/constants.rs index 2308db5c..236f569b 100644 --- a/core/crates/hub/configuration/store/src/constants.rs +++ b/core/crates/hub/configuration/store/src/constants.rs @@ -12,6 +12,7 @@ pub(crate) const REVISION_CONFLICT: &str = "Hub configuration revision changed"; pub(crate) const REVISION_EXHAUSTED: &str = "Hub configuration revision exhausted"; pub(crate) const REPLACED_RECORD: &str = "Hub configuration file changed outside its owner"; pub(crate) const LOST: &str = "Hub configuration storage is no longer usable"; -pub(crate) const PRIVATE_RECORD_SCHEMA: u16 = 2; +pub(crate) const PRIVATE_RECORD_SCHEMA: u16 = 3; +pub(crate) const LEGACY_PRIVATE_RECORD_SCHEMA: u16 = 2; pub(crate) const INVALID_PRIVATE_SCHEMA: &str = "Hub configuration private record schema is invalid"; diff --git a/core/crates/hub/configuration/store/src/record.rs b/core/crates/hub/configuration/store/src/record.rs index f30c42bb..7be5acf4 100644 --- a/core/crates/hub/configuration/store/src/record.rs +++ b/core/crates/hub/configuration/store/src/record.rs @@ -1,4 +1,6 @@ -use crate::constants::{INVALID_PRIVATE_SCHEMA, PRIVATE_RECORD_SCHEMA, WRONG_IDENTITY}; +use crate::constants::{ + INVALID_PRIVATE_SCHEMA, LEGACY_PRIVATE_RECORD_SCHEMA, PRIVATE_RECORD_SCHEMA, WRONG_IDENTITY, +}; use anyhow::{Result, ensure}; use serde::{Deserialize, Serialize}; use yolo_hub_delegation::PeerIdentity; @@ -11,13 +13,17 @@ pub(crate) struct Record { pub schema_version: u16, pub directory: DurableObjectIdentity, pub state: ConfigurationState, - pub listener_binding_initialized: bool, + /// 0.1.x wrote listener-binding evidence here. It is accepted on read and + /// omitted on the next write; the peer socket now lives in Hub state. + #[serde(default, rename = "listener_binding_initialized", skip_serializing)] + pub(crate) _legacy_listener_binding: bool, } impl Record { pub fn validate(&self, directory: DurableObjectIdentity, identity: PeerIdentity) -> Result<()> { ensure!( - self.schema_version == PRIVATE_RECORD_SCHEMA, + self.schema_version == PRIVATE_RECORD_SCHEMA + || self.schema_version == LEGACY_PRIVATE_RECORD_SCHEMA, INVALID_PRIVATE_SCHEMA ); ensure!( diff --git a/core/crates/hub/configuration/store/src/store.rs b/core/crates/hub/configuration/store/src/store.rs index 1ecd0af1..c918d35e 100644 --- a/core/crates/hub/configuration/store/src/store.rs +++ b/core/crates/hub/configuration/store/src/store.rs @@ -25,7 +25,6 @@ pub struct ConfigurationStore { lock: CoordinationLock, file_identity: FileIdentity, lost: AtomicBool, - listener_binding_initialized: bool, } impl ConfigurationStore { @@ -54,7 +53,7 @@ impl ConfigurationStore { schema_version: PRIVATE_RECORD_SCHEMA, directory: object, state, - listener_binding_initialized: false, + _legacy_listener_binding: false, }; record.validate(object, identity)?; let written = @@ -78,7 +77,6 @@ impl ConfigurationStore { lock, file_identity, lost: AtomicBool::new(false), - listener_binding_initialized: record.listener_binding_initialized, }; store.current()?; Ok(store) @@ -96,43 +94,11 @@ impl ConfigurationStore { self.lost.load(Ordering::Acquire) } - pub fn listener_binding_initialized(&self) -> Result { - self.current()?; - Ok(self.listener_binding_initialized) - } - - /// Commits only private initialization evidence; the public configuration is unchanged. - pub fn complete_listener_initialization( - &mut self, - mut verify_listener: impl FnMut() -> Result<()>, - ) -> Result<()> { - let state = self.current()?; - verify_listener()?; - if self.listener_binding_initialized { - return Ok(()); - } - self.write_record( - &Record { - schema_version: PRIVATE_RECORD_SCHEMA, - directory: self.directory, - state, - listener_binding_initialized: true, - }, - verify_listener, - )?; - self.current()?; - Ok(()) - } - fn read_current(&self) -> Result { self.verify()?; let (record, identity): (Record, FileIdentity) = self.namespace.read_json_with_identity(&self.path)?; - ensure!( - identity == self.file_identity - && record.listener_binding_initialized == self.listener_binding_initialized, - REPLACED_RECORD - ); + ensure!(identity == self.file_identity, REPLACED_RECORD); record.validate(self.directory, self.identity)?; self.verify()?; Ok(record.state) @@ -143,7 +109,7 @@ impl ConfigurationStore { schema_version: PRIVATE_RECORD_SCHEMA, directory: self.directory, state, - listener_binding_initialized: self.listener_binding_initialized, + _legacy_listener_binding: false, }; self.write_record(&record, || Ok(())) } @@ -167,7 +133,6 @@ impl ConfigurationStore { ) })(); self.file_identity = self.retain_failure(result)?; - self.listener_binding_initialized = record.listener_binding_initialized; Ok(()) } diff --git a/core/crates/hub/configuration/store/test_support/constants.rs b/core/crates/hub/configuration/store/test_support/constants.rs index 26e282ba..7b032764 100644 --- a/core/crates/hub/configuration/store/test_support/constants.rs +++ b/core/crates/hub/configuration/store/test_support/constants.rs @@ -23,7 +23,3 @@ pub const IDENTITY_FIELD: &str = "identity"; pub const REVISION_FIELD: &str = "revision"; pub const LAST_CHANGE_FIELD: &str = "last_change"; pub const INJECTED_FIELD: &str = "injected"; -pub const LISTENER_INITIALIZED_FIELD: &str = "listener_binding_initialized"; -pub const LISTENER_REVOKED: &str = "listener identity changed before initialization commit"; -pub const EPHEMERAL_LOOPBACK: &str = "127.0.0.1:0"; -pub const EPHEMERAL_WILDCARD: &str = "0.0.0.0:0"; diff --git a/core/crates/hub/configuration/store/test_support/fixtures.rs b/core/crates/hub/configuration/store/test_support/fixtures.rs index d1a49051..6ea58931 100644 --- a/core/crates/hub/configuration/store/test_support/fixtures.rs +++ b/core/crates/hub/configuration/store/test_support/fixtures.rs @@ -1,6 +1,5 @@ use crate::{ConfigurationStore, constants::RECORD_FILE, record::Record}; use std::{ - net::{Ipv4Addr, SocketAddr}, path::PathBuf, }; use uuid::Uuid; @@ -61,7 +60,7 @@ impl Fixture { } pub fn changed() -> Configuration { Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), PORT)), + trusted_origins: vec![other_identity()], ..Configuration::default() } } diff --git a/core/crates/hub/configuration/store/test_support/records.rs b/core/crates/hub/configuration/store/test_support/records.rs index 6b673c7e..20a7b59b 100644 --- a/core/crates/hub/configuration/store/test_support/records.rs +++ b/core/crates/hub/configuration/store/test_support/records.rs @@ -21,12 +21,10 @@ pub fn identity() -> PeerIdentity { pub fn configuration() -> Configuration { Configuration { - listen: None, - peers: Vec::new(), - file_roots: Vec::new(), trusted_origins: vec![ PeerIdentity::new(HubId::new(), [OTHER_KEY_BYTE; ED25519_PUBLIC_KEY_BYTES]).unwrap(), ], + ..Configuration::default() } } diff --git a/core/crates/hub/configuration/store/unit_tests/initialization.rs b/core/crates/hub/configuration/store/unit_tests/initialization.rs deleted file mode 100644 index 999b2574..00000000 --- a/core/crates/hub/configuration/store/unit_tests/initialization.rs +++ /dev/null @@ -1,70 +0,0 @@ -use super::{constants::*, fixtures::*}; - -#[test] -fn initialization_evidence_is_durable_monotonic_and_does_not_change_public_state() { - let fixture = Fixture::new(); - let mut store = fixture.open(None).unwrap(); - let original = store.current().unwrap(); - assert!(!store.listener_binding_initialized().unwrap()); - store.complete_listener_initialization(|| Ok(())).unwrap(); - assert!(store.listener_binding_initialized().unwrap()); - assert_eq!(store.current().unwrap(), original); - let bytes = fixture.bytes(); - store.complete_listener_initialization(|| Ok(())).unwrap(); - assert_eq!(fixture.bytes(), bytes); - drop(store); - let mut store = fixture.open(None).unwrap(); - assert!(store.listener_binding_initialized().unwrap()); - assert_eq!(store.current().unwrap(), original); - let update = change(&original); - let expected = store.planned(&update).unwrap(); - assert_eq!(store.apply(update).unwrap(), expected); - assert!(store.listener_binding_initialized().unwrap()); - assert!(fixture.record().listener_binding_initialized); -} - -#[test] -fn lost_listener_commit_fence_cannot_mark_first_startup_initialized() { - let fixture = Fixture::new(); - let mut store = fixture.open(None).unwrap(); - let bytes = fixture.bytes(); - let mut first_check = true; - assert!( - store - .complete_listener_initialization(|| { - if first_check { - first_check = false; - Ok(()) - } else { - anyhow::bail!(LISTENER_REVOKED) - } - }) - .is_err() - ); - assert_eq!(fixture.bytes(), bytes); - drop(store); - assert!( - !fixture - .open(None) - .unwrap() - .listener_binding_initialized() - .unwrap() - ); -} - -#[test] -fn old_or_truncated_private_record_cannot_gain_first_startup_authority() { - let fixture = Fixture::new(); - let mut store = fixture.open(None).unwrap(); - store.complete_listener_initialization(|| Ok(())).unwrap(); - let mut record = serde_json::to_value(fixture.record()).unwrap(); - drop(store); - record - .as_object_mut() - .unwrap() - .remove(LISTENER_INITIALIZED_FIELD); - fixture.replace(&record); - let bytes = fixture.bytes(); - assert!(fixture.open(None).is_err()); - assert_eq!(fixture.bytes(), bytes); -} diff --git a/core/crates/hub/configuration/store/unit_tests/lib.rs b/core/crates/hub/configuration/store/unit_tests/lib.rs index 7299d9a2..4ce70649 100644 --- a/core/crates/hub/configuration/store/unit_tests/lib.rs +++ b/core/crates/hub/configuration/store/unit_tests/lib.rs @@ -3,7 +3,6 @@ pub(crate) mod constants; mod corruption; #[path = "../test_support/fixtures.rs"] mod fixtures; -mod initialization; mod lifecycle; mod namespace; mod record; diff --git a/core/crates/hub/configuration/store/unit_tests/record.rs b/core/crates/hub/configuration/store/unit_tests/record.rs index 091fbb5c..37164bca 100644 --- a/core/crates/hub/configuration/store/unit_tests/record.rs +++ b/core/crates/hub/configuration/store/unit_tests/record.rs @@ -7,7 +7,7 @@ fn record() -> Record { schema_version: crate::constants::PRIVATE_RECORD_SCHEMA, directory: directory_identity(), state: state(identity()), - listener_binding_initialized: false, + _legacy_listener_binding: false, } } diff --git a/core/crates/hub/configuration/store/unit_tests/revision.rs b/core/crates/hub/configuration/store/unit_tests/revision.rs index aa65f345..12683959 100644 --- a/core/crates/hub/configuration/store/unit_tests/revision.rs +++ b/core/crates/hub/configuration/store/unit_tests/revision.rs @@ -87,10 +87,11 @@ fn replacement_between_revision_check_and_commit_is_a_nonrecoverable_cas_failure next.last_change = request.request_id; next.configuration = request.configuration; let mut replacement = fixture.record(); - replacement.state.configuration.listen = Some(std::net::SocketAddr::new( - std::net::Ipv4Addr::LOCALHOST.into(), - OTHER_PORT, - )); + replacement + .state + .configuration + .trusted_origins + .push(super::fixtures::other_identity()); fixture.replace(&replacement); let bytes = fixture.bytes(); assert!(store.write(next).is_err()); diff --git a/core/crates/hub/configuration/store/unit_tests/schema_rejection.rs b/core/crates/hub/configuration/store/unit_tests/schema_rejection.rs index 1e112461..af267480 100644 --- a/core/crates/hub/configuration/store/unit_tests/schema_rejection.rs +++ b/core/crates/hub/configuration/store/unit_tests/schema_rejection.rs @@ -2,11 +2,7 @@ use super::{constants::*, fixtures::*}; #[test] fn missing_current_schema_fields_are_rejected_without_rewriting() { - for removed in [ - vec![SCHEMA_FIELD], - vec![LISTENER_INITIALIZED_FIELD], - vec![SCHEMA_FIELD, LISTENER_INITIALIZED_FIELD], - ] { + for removed in [vec![SCHEMA_FIELD]] { let fixture = Fixture::new(); let store = fixture.open(None).unwrap(); let mut wire = serde_json::to_value(fixture.record()).unwrap(); @@ -24,7 +20,7 @@ fn missing_current_schema_fields_are_rejected_without_rewriting() { #[test] fn unsupported_private_schema_is_rejected_without_reinitialization() { for schema in [ - crate::constants::PRIVATE_RECORD_SCHEMA - SCHEMA_STEP, + crate::constants::LEGACY_PRIVATE_RECORD_SCHEMA - SCHEMA_STEP, crate::constants::PRIVATE_RECORD_SCHEMA + SCHEMA_STEP, ] { let fixture = Fixture::new(); @@ -44,12 +40,7 @@ fn unsupported_private_schema_is_rejected_without_reinitialization() { #[test] fn duplicate_current_record_fields_are_rejected_without_rewriting() { - for field in [ - DIRECTORY_FIELD, - STATE_FIELD, - SCHEMA_FIELD, - LISTENER_INITIALIZED_FIELD, - ] { + for field in [DIRECTORY_FIELD, STATE_FIELD, SCHEMA_FIELD] { let fixture = Fixture::new(); let store = fixture.open(None).unwrap(); let record = serde_json::to_value(fixture.record()).unwrap(); diff --git a/core/crates/hub/delegation/src/identity.rs b/core/crates/hub/delegation/src/identity.rs index 5b34a1d1..4bebe5bf 100644 --- a/core/crates/hub/delegation/src/identity.rs +++ b/core/crates/hub/delegation/src/identity.rs @@ -1,4 +1,5 @@ use crate::{DelegationError, ED25519_PUBLIC_KEY_BYTES, ED25519_SIGNATURE_BYTES}; +use ring::signature; use serde::{Deserialize, Serialize}; use yolo_hub_identity::HubId; @@ -26,6 +27,16 @@ impl PeerIdentity { pub fn key(&self) -> &[u8; ED25519_PUBLIC_KEY_BYTES] { &self.0.public_key } + + /// Verifies a signature made by the Hub key bound to this identity. + pub fn verify(&self, message: &[u8], signature: &[u8]) -> Result<(), DelegationError> { + if signature.len() != ED25519_SIGNATURE_BYTES { + return Err(DelegationError::Signature); + } + signature::UnparsedPublicKey::new(&signature::ED25519, self.key()) + .verify(message, signature) + .map_err(|_| DelegationError::Signature) + } } impl TryFrom for PeerIdentity { diff --git a/core/crates/hub/enrollment/store/src/model.rs b/core/crates/hub/enrollment/store/src/model.rs index cdbd2817..b1f51334 100644 --- a/core/crates/hub/enrollment/store/src/model.rs +++ b/core/crates/hub/enrollment/store/src/model.rs @@ -54,10 +54,7 @@ impl PreparedEnrollment { ensure!( peer.identity == request.consumer && export.rights == request.rights - && export.remaining_reexports == request.remaining_reexports - && configuration - .listen - .is_some_and(|address| address.ip().is_loopback()), + && export.remaining_reexports == request.remaining_reexports, INVALID ); Ok(()) diff --git a/core/crates/hub/enrollment/store/test_support/fixtures.rs b/core/crates/hub/enrollment/store/test_support/fixtures.rs index 0e3b47e9..26371b5d 100644 --- a/core/crates/hub/enrollment/store/test_support/fixtures.rs +++ b/core/crates/hub/enrollment/store/test_support/fixtures.rs @@ -19,9 +19,8 @@ pub fn prepared(no_change: bool) -> PreparedEnrollment { configuration: configuration.clone(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), }; - configuration.listen = Some((std::net::Ipv4Addr::LOCALHOST, PORT).into()); configuration.peers.push(Peer { identity: request.fields().consumer, dial: None, @@ -57,7 +56,6 @@ pub fn applied(prepared: &PreparedEnrollment) -> ConfigurationSnapshot { snapshot.state.last_change = change.request_id; snapshot.state.configuration = change.configuration.clone(); } - snapshot.listen_address = snapshot.state.configuration.listen; snapshot } pub fn completed(prepared: &PreparedEnrollment) -> RemoteBootstrapReceipt { diff --git a/core/crates/hub/federation/BUILD.bazel b/core/crates/hub/federation/BUILD.bazel index c76f4089..000f07b2 100644 --- a/core/crates/hub/federation/BUILD.bazel +++ b/core/crates/hub/federation/BUILD.bazel @@ -20,14 +20,15 @@ _DEPS = [ "//core/crates/hub/peer/protocol:yolo_hub_peer_protocol", "//core/crates/hub/peer/rpc", "//core/crates/hub/peer/dialer:yolo_hub_peer_dialer", - "//core/crates/hub/peer/transport:yolo_hub_peer_transport", + "//core/crates/hub/peer/diagnostics:yolo_hub_peer_diagnostics", + "//core/crates/hub/peer/io:yolo_hub_peer_io", + "//core/crates/hub/peer/link:yolo_hub_peer_link", "//core/crates/hub/publication:yolo_hub_publication", "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", "//core/crates/hub/terminal/protocol:yolo_hub_terminal_protocol", "//core/crates/hub/state:yolo_hub_state", "@crates//:anyhow", "@crates//:tokio", - "@crates//:russh", "@crates//:tracing", "@crates//:uuid", ] diff --git a/core/crates/hub/federation/integration_tests/network_capacity.rs b/core/crates/hub/federation/integration_tests/network_capacity.rs index 0118c9d2..e5b13bad 100644 --- a/core/crates/hub/federation/integration_tests/network_capacity.rs +++ b/core/crates/hub/federation/integration_tests/network_capacity.rs @@ -18,64 +18,45 @@ use pool::Pressure; use running::Running; use std::sync::atomic::Ordering; use tokio::{ - io::AsyncReadExt, sync::oneshot::error::TryRecvError, time::{sleep, timeout}, }; -enum Pool { - Ssh, - Rpc, -} - #[tokio::test] -async fn local_capacity_defers_discovery_without_retiring_streams_but_bad_signatures_retire() { - // Both cases share the actual process-global RPC engine; run them serially. - for pool in [Pool::Ssh, Pool::Rpc] { - let mut fixture = LinkFixture::new().await; - let pressure = match pool { - Pool::Ssh => Pressure::ssh(&mut fixture).await, - Pool::Rpc => Pressure::rpc().await, - }; - assert!(pressure.retained() > usize::default()); - assert_eq!( - fixture.node.discover(&fixture.link).await.unwrap(), - DiscoveryRefresh::DeferredLocalAdmission, - ); - let mut running = Running::start(&mut fixture); - sleep(crate::POLL_INTERVAL * OBSERVED_POLL_CYCLES).await; - assert_eq!( - running.finished.try_recv().unwrap_err(), - TryRecvError::Empty - ); - fixture.assert_retained().await; +async fn local_capacity_defers_discovery_without_retiring_link_but_bad_signatures_retire() { + let mut fixture = LinkFixture::new().await; + let pressure = Pressure::rpc(&fixture).await; + assert_eq!( + fixture.node.discover(&fixture.link).await.unwrap(), + DiscoveryRefresh::DeferredLocalAdmission, + ); + let mut running = Running::start(&mut fixture); + sleep(crate::POLL_INTERVAL * OBSERVED_POLL_CYCLES).await; + assert_eq!( + running.finished.try_recv().unwrap_err(), + TryRecvError::Empty + ); + fixture.assert_retained().await; - drop(pressure); - fixture.wait_refreshed().await; - fixture.assert_retained().await; + drop(pressure); + fixture.wait_refreshed().await; + fixture.assert_retained().await; - fixture.responder.invalid.store(true, Ordering::Release); - timeout(TEST_TIMEOUT, &mut running.finished) + fixture.responder.invalid.store(true, Ordering::Release); + timeout(TEST_TIMEOUT, &mut running.finished) + .await + .unwrap() + .unwrap() + .unwrap(); + assert!( + fixture + .node + .current(fixture.link.peer.hub()) .await - .unwrap() - .unwrap() - .unwrap(); - assert!( - fixture - .node - .current(fixture.link.connection.peer().hub()) - .await - .is_none() - ); - assert!(fixture.link.connection.is_closed()); - assert!( - timeout(TEST_TIMEOUT, fixture.output.read_u8()) - .await - .unwrap() - .is_err() - ); - assert!(fixture.node.discover(&fixture.link).await.is_err()); - running.close().await; - fixture.close().await; - } + .is_none() + ); + assert!(fixture.link.is_closed()); + assert!(fixture.node.discover(&fixture.link).await.is_err()); + running.close().await; + fixture.close().await; } diff --git a/core/crates/hub/federation/settings/src/address.rs b/core/crates/hub/federation/settings/src/address.rs index 445346bf..f6298572 100644 --- a/core/crates/hub/federation/settings/src/address.rs +++ b/core/crates/hub/federation/settings/src/address.rs @@ -1,96 +1,185 @@ use crate::constants::*; use anyhow::{Result, ensure}; use serde::{Deserialize, Serialize}; -use std::{ - net::{IpAddr, SocketAddr}, - num::NonZeroU16, -}; +use std::path::{Component, Path, PathBuf}; pub use yolo_ssh_profile_protocol::SshProfileRef; +use yolo_ssh_profile_protocol::validate_profile_id; -#[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] -#[serde(from = "TransportWire", into = "TransportWire")] -pub enum DialTransport { - #[default] - Tcp, - Ssh { - profile: SshProfileRef, - }, +/// One outbound peer transport plus the provider Hub's absolute Unix socket. +/// +/// `Ssh` is the user-facing peer link: the local OpenSSH client supplies +/// connectivity, host trust and credentials. `Profile` remains the managed +/// remote-setup binding owned by the Hub broker; it is not created by +/// `yolo machine add`. +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum PeerAddress { + Ssh(SshAddress), + Profile(ProfileAddress), +} + +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[serde(try_from = "SshAddressWire", into = "SshAddressWire")] +pub struct SshAddress { + pub ssh: String, + pub socket: PathBuf, } #[derive(Serialize, Deserialize)] -#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)] -enum TransportWire { - Tcp {}, - Ssh { profile: SshProfileRef }, +#[serde(deny_unknown_fields)] +struct SshAddressWire { + ssh: String, + socket: PathBuf, } -impl From for DialTransport { - fn from(value: TransportWire) -> Self { - match value { - TransportWire::Tcp {} => Self::Tcp, - TransportWire::Ssh { profile } => Self::Ssh { profile }, - } + +impl TryFrom for SshAddress { + type Error = anyhow::Error; + fn try_from(value: SshAddressWire) -> Result { + let address = Self { + ssh: value.ssh, + socket: value.socket, + }; + validate_ssh_target(&address.ssh)?; + validate_socket(&address.socket)?; + Ok(address) } } -impl From for TransportWire { - fn from(value: DialTransport) -> Self { - match value { - DialTransport::Tcp => Self::Tcp {}, - DialTransport::Ssh { profile } => Self::Ssh { profile }, + +impl From for SshAddressWire { + fn from(value: SshAddress) -> Self { + Self { + ssh: value.ssh, + socket: value.socket, } } } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[serde(try_from = "ProfileAddressWire", into = "ProfileAddressWire")] +pub struct ProfileAddress { + pub profile: String, + pub socket: PathBuf, +} + +#[derive(Serialize, Deserialize)] #[serde(deny_unknown_fields)] -pub struct PeerAddress { - pub host: String, - pub port: NonZeroU16, - pub transport: DialTransport, +struct ProfileAddressWire { + profile: String, + socket: PathBuf, } -impl PeerAddress { - pub fn tcp(host: impl Into, port: NonZeroU16) -> Self { +impl TryFrom for ProfileAddress { + type Error = anyhow::Error; + fn try_from(value: ProfileAddressWire) -> Result { + let address = Self { + profile: value.profile, + socket: value.socket, + }; + validate_profile_id(&address.profile)?; + validate_socket(&address.socket)?; + Ok(address) + } +} + +impl From for ProfileAddressWire { + fn from(value: ProfileAddress) -> Self { Self { - host: host.into(), - port, - transport: DialTransport::Tcp, + profile: value.profile, + socket: value.socket, } } - pub fn ssh(profile: SshProfileRef, endpoint: SocketAddr) -> Result { - ensure!( - !matches!(endpoint, SocketAddr::V6(address) if address.scope_id() != 0 || address.flowinfo() != 0), - INVALID - ); - let value = Self { - host: endpoint.ip().to_string(), - port: NonZeroU16::new(endpoint.port()).ok_or_else(|| anyhow::anyhow!(INVALID))?, - transport: DialTransport::Ssh { profile }, - }; +} + +impl PeerAddress { + pub fn ssh(target: impl Into, socket: PathBuf) -> Result { + let value = Self::Ssh(SshAddress { + ssh: target.into(), + socket, + }); + value.validate()?; + Ok(value) + } + pub fn profile(profile: impl Into, socket: PathBuf) -> Result { + let value = Self::Profile(ProfileAddress { + profile: profile.into(), + socket, + }); value.validate()?; Ok(value) } pub fn validate(&self) -> Result<()> { - ensure!( - !self.host.is_empty() - && self.host.len() <= MAX_HOST_BYTES - && !self - .host - .chars() - .any(|character| character.is_whitespace() || character.is_control()), - INVALID - ); - if matches!(self.transport, DialTransport::Ssh { .. }) { - let address: IpAddr = self.host.parse()?; - ensure!( - address.is_loopback() && address.to_string() == self.host, - INVALID - ); + match self { + Self::Ssh(address) => { + validate_ssh_target(&address.ssh)?; + validate_socket(&address.socket) + } + Self::Profile(address) => { + validate_profile_id(&address.profile)?; + validate_socket(&address.socket) + } } - Ok(()) } - pub fn ssh_endpoint(&self) -> Result { - self.validate()?; - ensure!(matches!(self.transport, DialTransport::Ssh { .. }), INVALID); - Ok(SocketAddr::new(self.host.parse()?, self.port.get())) + pub fn socket(&self) -> &Path { + match self { + Self::Ssh(address) => &address.socket, + Self::Profile(address) => &address.socket, + } } + pub fn ssh_target(&self) -> Option<&str> { + match self { + Self::Ssh(address) => Some(&address.ssh), + Self::Profile(_) => None, + } + } + pub fn profile_id(&self) -> Option<&str> { + match self { + Self::Ssh(_) => None, + Self::Profile(address) => Some(&address.profile), + } + } +} + +/// An SSH destination is passed as one argv element, so it must not be an +/// option, an empty string or something the remote shell could split. +pub fn validate_ssh_target(target: &str) -> Result<()> { + ensure!( + !target.is_empty() + && target.len() <= MAX_SSH_TARGET_BYTES + && !target.starts_with('-'), + INVALID + ); + ensure!( + target.bytes().all(|byte| { + byte.is_ascii_alphanumeric() + || matches!( + byte, + b'.' | b'-' | b'_' | b'@' | b'%' | b'+' | b':' | b'[' | b']' + ) + }), + INVALID + ); + Ok(()) +} + +/// Unix socket paths must fit the kernel `sun_path` budget and name a +/// canonical absolute location without traversal or control bytes. +pub fn validate_socket(path: &Path) -> Result<()> { + ensure!(path.is_absolute() && path.to_str().is_some(), INVALID); + let raw = path.to_str().ok_or_else(|| anyhow::anyhow!(INVALID))?; + ensure!( + !raw.split('/').any(|part| part == "." || part == ".."), + INVALID + ); + let encoded = path.as_os_str().as_encoded_bytes(); + ensure!( + !encoded.is_empty() && encoded.len() <= MAX_SOCKET_PATH_BYTES, + INVALID + ); + ensure!( + path.components() + .all(|component| matches!(component, Component::RootDir | Component::Normal(_))), + INVALID + ); + ensure!(!encoded.iter().any(|byte| byte.is_ascii_control()), INVALID); + Ok(()) } diff --git a/core/crates/hub/federation/settings/src/constants.rs b/core/crates/hub/federation/settings/src/constants.rs index b144a328..90065a82 100644 --- a/core/crates/hub/federation/settings/src/constants.rs +++ b/core/crates/hub/federation/settings/src/constants.rs @@ -1,6 +1,7 @@ pub const MAX_PEERS: usize = 32; pub const MAX_ORIGINS: usize = 128; -pub const MAX_HOST_BYTES: usize = 253; +pub const MAX_SOCKET_PATH_BYTES: usize = 100; +pub const MAX_SSH_TARGET_BYTES: usize = 255; pub const MAX_ROUTES_PER_ORIGIN: usize = 32; pub const MAX_PEER_ADVERTISEMENTS: usize = MAX_ORIGINS * MAX_ROUTES_PER_ORIGIN; pub(crate) const INVALID: &str = "invalid Hub peer configuration"; diff --git a/core/crates/hub/federation/settings/src/lib.rs b/core/crates/hub/federation/settings/src/lib.rs index 88cb12cf..d069601f 100644 --- a/core/crates/hub/federation/settings/src/lib.rs +++ b/core/crates/hub/federation/settings/src/lib.rs @@ -1,26 +1,46 @@ mod address; mod constants; mod file_roots; -pub use address::{DialTransport, PeerAddress, SshProfileRef}; +mod migration; +pub use address::{PeerAddress, SshProfileRef, validate_socket, validate_ssh_target}; use anyhow::{Result, ensure}; pub use constants::*; use serde::{Deserialize, Serialize}; -use std::{ - collections::{BTreeMap, BTreeSet}, - net::SocketAddr, - num::NonZeroUsize, -}; +use std::{collections::{BTreeMap, BTreeSet}, num::NonZeroUsize}; use yolo_hub_delegation::{MAX_DELEGATION_DEPTH, PeerIdentity}; use yolo_hub_identity::HubId; use yolo_hub_routing::{Right, Rights, RoutingLimits}; #[derive(Clone, Debug, Default, Eq, PartialEq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] +#[serde( + try_from = "migration::ConfigurationWire", + into = "migration::ConfigurationOut" +)] pub struct Configuration { - pub listen: Option, pub peers: Vec, pub trusted_origins: Vec, pub file_roots: Vec, + /// How peers that arrive through the local SSH-trusted peer socket are + /// admitted and what the local origin shares with them. + pub ssh_peers: SshPeerPolicy, +} +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct SshPeerPolicy { + /// Require an explicit peer pin before accepting an inbound link. + pub require_registration: bool, + /// Share this Hub's own origin with SSH-authenticated peers. + pub share_local: bool, + pub rights: Rights, +} +impl Default for SshPeerPolicy { + fn default() -> Self { + Self { + require_registration: false, + share_local: true, + rights: Rights::all(), + } + } } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -41,6 +61,10 @@ pub struct ExportPolicy { impl Configuration { pub fn validate(&self, local: PeerIdentity) -> Result<()> { file_roots::validate(&self.file_roots)?; + ensure!( + !self.ssh_peers.share_local || !self.ssh_peers.rights.is_empty(), + INVALID + ); ensure!( self.peers.len() <= MAX_PEERS && self.trusted_origins.len() <= MAX_ORIGINS, INVALID @@ -68,6 +92,7 @@ impl Configuration { if let Some(address) = &peer.dial { address.validate()?; } + ensure!(!peer.consume || peer.dial.is_some(), INVALID); ensure!(peer.exports.len() <= MAX_ORIGINS, INVALID); let mut origins = BTreeSet::new(); for export in &peer.exports { @@ -106,6 +131,15 @@ impl Configuration { .iter() .find(|policy| policy.origin == origin) } + /// The default policy for SSH-authenticated peers. Explicit per-peer + /// exports still take precedence; this only covers the local origin. + pub fn shared_export(&self, origin: HubId, local: HubId) -> Option { + (self.ssh_peers.share_local && origin == local).then(|| ExportPolicy { + origin, + rights: self.ssh_peers.rights.clone(), + remaining_reexports: 0, + }) + } } pub fn routing_limits() -> RoutingLimits { RoutingLimits { diff --git a/core/crates/hub/federation/settings/src/migration.rs b/core/crates/hub/federation/settings/src/migration.rs new file mode 100644 index 00000000..bf96a230 --- /dev/null +++ b/core/crates/hub/federation/settings/src/migration.rs @@ -0,0 +1,133 @@ +use crate::{Configuration, ExportPolicy, Peer, PeerAddress, SshPeerPolicy}; +use serde::{Deserialize, Serialize, de::IgnoredAny}; +use std::num::NonZeroU16; +use yolo_hub_delegation::PeerIdentity; + +/// 0.1.x configurations carried a TCP `listen` field and TCP/loopback-peer +/// dial addresses. 0.2.x dials named a broker profile. Both are retired: the +/// peer transport is now the local SSH client with a stdio bridge. Peer pins, +/// consume flags and export policy survive; a retired dial drops its import +/// intent and is re-added with `yolo machine add`. +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +pub(crate) struct ConfigurationWire { + #[serde(default, rename = "listen", deserialize_with = "ignore_any")] + _listen: (), + peers: Vec, + trusted_origins: Vec, + file_roots: Vec, + #[serde(default)] + ssh_peers: SshPeerPolicy, +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct PeerWire { + identity: PeerIdentity, + dial: Option, + consume: bool, + exports: Vec, +} + +#[derive(Deserialize)] +#[serde(untagged)] +enum PeerDialWire { + Current(PeerAddress), + #[allow(dead_code)] + Legacy(LegacyPeerAddress), +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct LegacyPeerAddress { + #[allow(dead_code)] + host: String, + #[allow(dead_code)] + port: NonZeroU16, + #[allow(dead_code)] + transport: LegacyTransport, +} + +#[derive(Deserialize)] +#[serde(tag = "kind", rename_all = "snake_case", deny_unknown_fields)] +enum LegacyTransport { + Tcp {}, + Ssh { + #[allow(dead_code)] + profile: crate::SshProfileRef, + }, +} + +fn ignore_any<'de, D>(deserializer: D) -> Result<(), D::Error> +where + D: serde::Deserializer<'de>, +{ + IgnoredAny::deserialize(deserializer)?; + Ok(()) +} + +impl TryFrom for Configuration { + type Error = anyhow::Error; + fn try_from(wire: ConfigurationWire) -> anyhow::Result { + Ok(Self { + peers: wire.peers.into_iter().map(Peer::from).collect(), + trusted_origins: wire.trusted_origins, + file_roots: wire.file_roots, + ssh_peers: wire.ssh_peers, + }) + } +} + +impl From for Peer { + fn from(wire: PeerWire) -> Self { + Self { + identity: wire.identity, + dial: match &wire.dial { + Some(PeerDialWire::Current(address)) => Some(address.clone()), + Some(PeerDialWire::Legacy(_)) | None => None, + }, + // Importing requires a dial. A retired legacy dial cannot be + // silently reused, so its import intent is dropped as well. + consume: wire.consume + && matches!(wire.dial, Some(PeerDialWire::Current(_))), + exports: wire.exports, + } + } +} + +/// Serialization shape intentionally omits the retired `listen` field. +#[derive(Serialize)] +pub(crate) struct ConfigurationOut { + peers: Vec, + trusted_origins: Vec, + file_roots: Vec, + ssh_peers: SshPeerPolicy, +} + +#[derive(Serialize)] +struct PeerOut { + identity: PeerIdentity, + dial: Option, + consume: bool, + exports: Vec, +} + +impl From for ConfigurationOut { + fn from(value: Configuration) -> Self { + Self { + peers: value + .peers + .into_iter() + .map(|peer| PeerOut { + identity: peer.identity, + dial: peer.dial, + consume: peer.consume, + exports: peer.exports, + }) + .collect(), + trusted_origins: value.trusted_origins, + file_roots: value.file_roots, + ssh_peers: value.ssh_peers, + } + } +} diff --git a/core/crates/hub/federation/settings/unit_tests/address.rs b/core/crates/hub/federation/settings/unit_tests/address.rs index 7b991478..72a0759f 100644 --- a/core/crates/hub/federation/settings/unit_tests/address.rs +++ b/core/crates/hub/federation/settings/unit_tests/address.rs @@ -1,83 +1,64 @@ use super::address_constants::*; -use std::{ - net::{IpAddr, Ipv6Addr, SocketAddr, SocketAddrV6}, - num::NonZeroU16, -}; -use yolo_hub_federation_settings::{DialTransport, MAX_HOST_BYTES, PeerAddress, SshProfileRef}; +use std::path::{Path, PathBuf}; +use yolo_hub_federation_settings::{MAX_SOCKET_PATH_BYTES, MAX_SSH_TARGET_BYTES, PeerAddress}; -fn profile() -> SshProfileRef { - SshProfileRef::new(PROFILE, DIGEST_CHARACTER.repeat(DIGEST_BYTES)).unwrap() -} -fn port() -> NonZeroU16 { - NonZeroU16::new(PORT).unwrap() +fn socket(path: &str) -> PathBuf { + Path::new(path).to_path_buf() } #[test] -fn explicit_ssh_reference_roundtrips_and_requires_a_canonical_loopback_endpoint() { - for host in [LOOPBACK, V6_LOOPBACK] { - let endpoint = SocketAddr::new(host.parse().unwrap(), PORT); - let address = PeerAddress::ssh(profile(), endpoint).unwrap(); - assert_eq!(address.ssh_endpoint().unwrap(), endpoint); - let decoded: PeerAddress = - serde_json::from_value(serde_json::to_value(&address).unwrap()).unwrap(); - assert_eq!(decoded, address); - assert!(matches!(address.transport, DialTransport::Ssh { .. })); - } - for host in INVALID_SSH_HOSTS { - let address = PeerAddress { - host: (*host).into(), - port: port(), - transport: DialTransport::Ssh { profile: profile() }, - }; - assert!(address.validate().is_err()); - } - for endpoint in [ - SocketAddr::new(IpAddr::V6(Ipv6Addr::LOCALHOST), 0), - SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::LOCALHOST, PORT, PORT.into(), 0)), - SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::LOCALHOST, PORT, 0, PORT.into())), - ] { - assert!(PeerAddress::ssh(profile(), endpoint).is_err()); +fn ssh_peer_address_roundtrips_with_an_absolute_socket() { + let address = PeerAddress::ssh(SSH_TARGET, socket(SOCKET)).unwrap(); + assert_eq!(address.socket(), socket(SOCKET)); + assert_eq!(address.ssh_target(), Some(SSH_TARGET)); + let decoded: PeerAddress = + serde_json::from_value(serde_json::to_value(&address).unwrap()).unwrap(); + assert_eq!(decoded, address); + for path in INVALID_SOCKETS { + assert!( + PeerAddress::ssh(SSH_TARGET, socket(path)).is_err(), + "{path:?}" + ); } } #[test] -fn tcp_requires_a_valid_host_and_never_produces_an_ssh_endpoint() { - let address = PeerAddress::tcp(PROFILE, port()); - address.validate().unwrap(); - assert!(address.ssh_endpoint().is_err()); - for host in INVALID_HOSTS { - assert!(PeerAddress::tcp(*host, port()).validate().is_err()); +fn ssh_targets_cannot_be_options_or_shell_fragments() { + for target in ["", "-oProxyCommand=x", "user host", "user@host;rm", "a/b", "a\nb"] { + assert!( + PeerAddress::ssh(target, socket(SOCKET)).is_err(), + "{target:?}" + ); } - assert!( - PeerAddress::tcp(DIGEST_CHARACTER.repeat(MAX_HOST_BYTES), port()) - .validate() - .is_ok() - ); - assert!( - PeerAddress::tcp(PROFILE.repeat(MAX_HOST_BYTES), port()) - .validate() - .is_err() - ); + assert!(PeerAddress::ssh("a".repeat(MAX_SSH_TARGET_BYTES + 1), socket(SOCKET)).is_err()); + assert!(PeerAddress::ssh("user@host", socket(SOCKET)).is_ok()); + assert!(PeerAddress::ssh("host-alias_1.example", socket(SOCKET)).is_ok()); +} + +#[test] +fn profile_dials_remain_readable_for_managed_remote_setup() { + let address = PeerAddress::profile(PROFILE_ID, socket(SOCKET)).unwrap(); + assert_eq!(address.profile_id(), Some(PROFILE_ID)); + let decoded: PeerAddress = + serde_json::from_value(serde_json::to_value(&address).unwrap()).unwrap(); + assert_eq!(decoded, address); } #[test] -fn transport_tags_cannot_silently_drop_ssh_profile_binding() { - let address = PeerAddress::tcp(LOOPBACK, port()); +fn the_socket_budget_stays_inside_the_kernel_sun_path_limit() { + let acceptable = format!("/{}", "a".repeat(MAX_SOCKET_PATH_BYTES - 1)); + assert!(PeerAddress::ssh(SSH_TARGET, socket(&acceptable)).is_ok()); + let overlong = format!("/{}", "a".repeat(MAX_SOCKET_PATH_BYTES)); + assert!(PeerAddress::ssh(SSH_TARGET, socket(&overlong)).is_err()); +} + +#[test] +fn a_peer_address_cannot_drop_its_transport_binding() { + let address = PeerAddress::ssh(SSH_TARGET, socket(SOCKET)).unwrap(); let mut value = serde_json::to_value(&address).unwrap(); - value.as_object_mut().unwrap().remove(TRANSPORT); + value.as_object_mut().unwrap().remove(SSH_FIELD); assert!(serde_json::from_value::(value).is_err()); let mut value = serde_json::to_value(&address).unwrap(); - value[TRANSPORT][PROFILE_FIELD] = serde_json::to_value(profile()).unwrap(); - assert!(serde_json::from_value::(value).is_err()); - let ssh = - PeerAddress::ssh(profile(), SocketAddr::new(LOOPBACK.parse().unwrap(), PORT)).unwrap(); - let mut value = serde_json::to_value(&ssh).unwrap(); - value[TRANSPORT] - .as_object_mut() - .unwrap() - .remove(PROFILE_FIELD); - assert!(serde_json::from_value::(value).is_err()); - let mut value = serde_json::to_value(&ssh).unwrap(); - value[TRANSPORT][KIND] = UNKNOWN_KIND.into(); + value["socket"] = "peer.sock".into(); assert!(serde_json::from_value::(value).is_err()); } diff --git a/core/crates/hub/federation/settings/unit_tests/address_constants.rs b/core/crates/hub/federation/settings/unit_tests/address_constants.rs index dd91c838..4c04553f 100644 --- a/core/crates/hub/federation/settings/unit_tests/address_constants.rs +++ b/core/crates/hub/federation/settings/unit_tests/address_constants.rs @@ -1,18 +1,14 @@ -pub const PROFILE: &str = "test-machine"; -pub const DIGEST_CHARACTER: &str = "a"; -pub const DIGEST_BYTES: usize = 64; -pub const PORT: u16 = 2022; -pub const TRANSPORT: &str = "transport"; -pub const KIND: &str = "kind"; +pub const SSH_TARGET: &str = "test-machine"; +pub const SOCKET: &str = "/home/stone/.local/state/yolo/hub/peer.sock"; +pub const SSH_FIELD: &str = "ssh"; pub const PROFILE_FIELD: &str = "profile"; -pub const INVALID_HOSTS: &[&str] = &["", "bad host", "bad\nhost"]; -pub const INVALID_SSH_HOSTS: &[&str] = &[ - "localhost", - "192.0.2.1", - "::1%lo0", - "127.00.0.1", - "127.0.0.1\n", +pub const PROFILE_ID: &str = "test-profile"; +pub const INVALID_SOCKETS: &[&str] = &[ + "", + "peer.sock", + "relative/peer.sock", + "/tmp/../etc/peer.sock", + "/tmp/./peer.sock", + "/tmp/peer\n.sock", + "/tmp/peer\0.sock", ]; -pub const LOOPBACK: &str = "127.0.0.1"; -pub const V6_LOOPBACK: &str = "::1"; -pub const UNKNOWN_KIND: &str = "unrecognized"; diff --git a/core/crates/hub/federation/settings/unit_tests/agent_control.rs b/core/crates/hub/federation/settings/unit_tests/agent_control.rs index 2dc72a18..d02b7781 100644 --- a/core/crates/hub/federation/settings/unit_tests/agent_control.rs +++ b/core/crates/hub/federation/settings/unit_tests/agent_control.rs @@ -15,7 +15,7 @@ fn semantic_control_export_is_explicit_and_old_terminal_exports_do_not_expand() peers: vec![Peer { identity: remote.identity, dial: None, - consume: true, + consume: false, exports: vec![ExportPolicy { origin: local.identity.hub(), rights, @@ -62,7 +62,7 @@ fn semantic_only_export_does_not_implicitly_allow_terminal_writes() { peers: vec![Peer { identity: remote.identity, dial: None, - consume: true, + consume: false, exports: vec![ExportPolicy { origin: local.identity.hub(), rights: [Right::Observe, Right::AgentControl].into_iter().collect(), diff --git a/core/crates/hub/federation/settings/unit_tests/lib.rs b/core/crates/hub/federation/settings/unit_tests/lib.rs index 7da99424..fd37e11a 100644 --- a/core/crates/hub/federation/settings/unit_tests/lib.rs +++ b/core/crates/hub/federation/settings/unit_tests/lib.rs @@ -6,24 +6,70 @@ mod address_constants; mod agent_control; mod file_roots; +#[test] +fn ssh_peers_share_the_local_origin_by_default() { + let local = Identity::new(); + let remote = Identity::new(); + let configuration = Configuration::default(); + assert!(!configuration.ssh_peers.require_registration); + assert!(configuration.ssh_peers.share_local); + let policy = configuration + .shared_export(local.identity.hub(), local.identity.hub()) + .unwrap(); + assert_eq!(policy.origin, local.identity.hub()); + assert!(policy.rights.allows(&observe())); + assert!( + configuration + .shared_export(remote.identity.hub(), local.identity.hub()) + .is_none() + ); +} + +#[test] +fn schema_3_profile_dials_and_missing_share_policy_are_read() { + let remote = Identity::new(); + let configuration = Configuration { + peers: vec![Peer { + identity: remote.identity, + dial: Some( + PeerAddress::profile("managed", "/run/user/1000/yolo/hub/peer.sock".into()) + .unwrap(), + ), + consume: true, + exports: vec![], + }], + ..Configuration::default() + }; + let mut raw = serde_json::to_value(&configuration).unwrap(); + raw.as_object_mut().unwrap().remove(stringify!(ssh_peers)); + let configuration: Configuration = serde_json::from_value(raw).unwrap(); + assert!(matches!( + configuration.peers[0].dial, + Some(PeerAddress::Profile(_)) + )); + assert!(configuration.ssh_peers.share_local); + assert!(!configuration.ssh_peers.require_registration); + assert!(configuration.ssh_peers.rights.allows(&observe())); +} + #[test] fn default_local_configuration_and_explicit_directed_peer_roundtrip() { let (local, remote) = (Identity::new(), Identity::new()); Configuration::default().validate(local.identity).unwrap(); let configuration = Configuration { - listen: None, trusted_origins: vec![], file_roots: vec![], peers: vec![Peer { identity: remote.identity, dial: None, - consume: true, + consume: false, exports: vec![ExportPolicy { origin: local.identity.hub(), rights: rights(), remaining_reexports: REEXPORT_DEPTH, }], }], + ..Configuration::default() }; configuration.validate(local.identity).unwrap(); assert_eq!(configuration.pins(), vec![remote.identity]); @@ -43,14 +89,14 @@ fn duplicate_peers_unpinned_exports_and_self_connections_are_rejected() { let peer = Peer { identity: remote.identity, dial: None, - consume: true, + consume: false, exports: vec![], }; let mut configuration = Configuration { - listen: None, trusted_origins: vec![], file_roots: vec![], peers: vec![peer.clone(), peer], + ..Configuration::default() }; assert!(configuration.validate(local.identity).is_err()); configuration.peers.pop(); @@ -65,3 +111,65 @@ fn duplicate_peers_unpinned_exports_and_self_connections_are_rejected() { configuration.peers[0].identity = local.identity; assert!(configuration.validate(local.identity).is_err()); } + +#[test] +fn legacy_listener_and_dial_are_migrated_without_weakening_the_peer_pin() { + let (local, remote) = (Identity::new(), Identity::new()); + let configuration = Configuration { + peers: vec![Peer { + identity: remote.identity, + dial: Some( + PeerAddress::profile( + "remote", + "/run/user/1000/yolo/hub/peer.sock".into(), + ) + .unwrap(), + ), + consume: true, + exports: vec![ExportPolicy { + origin: local.identity.hub(), + rights: rights(), + remaining_reexports: REEXPORT_DEPTH, + }], + }], + ..Configuration::default() + }; + for dial in [ + serde_json::json!({ + "host": "127.0.0.1", + "port": 7400, + "transport": {"kind": "ssh", "profile": serde_json::to_value( + SshProfileRef::new("remote", "a".repeat(64)).unwrap() + ).unwrap()}, + }), + serde_json::json!({ + "host": "192.0.2.1", + "port": 7400, + "transport": {"kind": "tcp"}, + }), + ] { + let mut value = serde_json::to_value(&configuration).unwrap(); + value["listen"] = serde_json::json!("0.0.0.0:7400"); + value["peers"][0]["dial"] = dial; + let decoded: Configuration = serde_json::from_value(value).unwrap(); + assert_eq!(decoded.peers[0].identity, remote.identity); + assert!(decoded.peers[0].dial.is_none()); + assert!(!decoded.peers[0].consume); + assert_eq!(decoded.peers[0].exports, configuration.peers[0].exports); + } +} + +#[test] +fn consuming_a_peer_requires_a_dial() { + let (local, remote) = (Identity::new(), Identity::new()); + let configuration = Configuration { + peers: vec![Peer { + identity: remote.identity, + dial: None, + consume: true, + exports: vec![], + }], + ..Configuration::default() + }; + assert!(configuration.validate(local.identity).is_err()); +} diff --git a/core/crates/hub/federation/src/constants.rs b/core/crates/hub/federation/src/constants.rs index 04e7c1f1..82787979 100644 --- a/core/crates/hub/federation/src/constants.rs +++ b/core/crates/hub/federation/src/constants.rs @@ -27,3 +27,5 @@ pub(crate) const OBSERVATION_WORKER_BUSY: &str = pub(crate) const OBSERVATION_LOAD_TIMEOUT: Duration = Duration::from_secs(5); pub(crate) const OBSERVATION_WORKER_STALLED: &str = "retired observation cache worker remains blocked; storage custody is retained"; + +pub(crate) const NO_DIALER: &str = "this Hub has no SSH peer dialer"; diff --git a/core/crates/hub/federation/src/discovery.rs b/core/crates/hub/federation/src/discovery.rs index 4956300b..baf91e3a 100644 --- a/core/crates/hub/federation/src/discovery.rs +++ b/core/crates/hub/federation/src/discovery.rs @@ -3,9 +3,9 @@ use crate::{ constants::*, discovery_channel::{self, DiscoveryRefresh}, node::now_ms, - peer_diagnostics::EXPORT_DEPTH_EXHAUSTED, registry::Link, }; +use yolo_hub_peer_diagnostics::EXPORT_DEPTH_EXHAUSTED; use anyhow::{Context, Result, ensure}; use std::{collections::BTreeSet, sync::atomic::Ordering}; use tokio::time::Instant; @@ -21,13 +21,11 @@ impl Federation { pub(crate) async fn discover(&self, link: &Link) -> Result { self.require_open()?; let deadline = Instant::now() + OPERATION_TIMEOUT; - let Some(channel) = discovery_channel::open(link, deadline).await? else { - return Ok(DiscoveryRefresh::DeferredLocalAdmission); - }; let request_id = Uuid::new_v4(); let allowed_origins = self.book.lock().await.allowed_origins(); self.require_open()?; - let response = channel + let response = link + .client .request( PeerRequest::Discover { request_id, @@ -36,7 +34,10 @@ impl Federation { }, deadline, ) - .await?; + .await; + let Some(response) = discovery_channel::classify(response)? else { + return Ok(DiscoveryRefresh::DeferredLocalAdmission); + }; ensure!(response.updates.is_none(), INVALID_DISCOVERY); let PeerReply::Directory { request_id: observed, @@ -49,13 +50,7 @@ impl Federation { ensure!(request_id == observed, INVALID_DISCOVERY); let mut book = self.book.lock().await; self.require_open()?; - book.install( - link.connection.peer(), - link.id, - revision, - origins, - now_ms()?, - )?; + book.install(link.peer, link.id, revision, origins, now_ms()?)?; let _ = self.observation_cache.record(book.history()); self.require_open()?; Ok(DiscoveryRefresh::Updated) @@ -82,11 +77,12 @@ impl Federation { .source() .filter(|source| source.payload.policy_epoch == self.policy_epoch) { - if let Some(policy) = self + let policy = self .config .export(peer.hub(), self.hub_id()) - .filter(|policy| allowed.contains(&policy.origin)) - { + .cloned() + .or_else(|| self.config.shared_export(self.hub_id(), self.hub_id())); + if let Some(policy) = policy.filter(|policy| allowed.contains(&policy.origin)) { let claims = GrantClaims { grant_id: Uuid::new_v4(), issuer: self.state.peer_identity(), diff --git a/core/crates/hub/federation/src/discovery_channel.rs b/core/crates/hub/federation/src/discovery_channel.rs index 27dc4439..a2e9aa4f 100644 --- a/core/crates/hub/federation/src/discovery_channel.rs +++ b/core/crates/hub/federation/src/discovery_channel.rs @@ -1,8 +1,5 @@ -use crate::registry::Link; use anyhow::Result; -use tokio::time::Instant; -use yolo_hub_peer_rpc::{Client, RpcError}; -use yolo_hub_peer_transport::ChannelAdmissionError; +use yolo_hub_peer_rpc::{Received, RpcError}; #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub(crate) enum DiscoveryRefresh { @@ -10,25 +7,13 @@ pub(crate) enum DiscoveryRefresh { DeferredLocalAdmission, } -/// Only failures proven local and preceding RPC dispatch defer discovery. -/// A remote refusal, malformed reply or closed transport still retires the link. -pub(crate) async fn open(link: &Link, deadline: Instant) -> Result> { - let stream = match link.connection.open_channel(deadline).await { - Ok(stream) => stream, - Err(error) - if error.downcast_ref::() - == Some(&ChannelAdmissionError::AtCapacity) => - { - return Ok(None); - } - Err(error) => return Err(error), - }; - classify_connection(Client::connect(stream, deadline).await) -} - -fn classify_connection(result: std::result::Result) -> Result> { +/// Only a typed local capacity refusal defers discovery. A remote refusal, +/// malformed reply or closed transport still retires the link. +pub(crate) fn classify( + result: std::result::Result, +) -> Result> { match result { - Ok(client) => Ok(Some(client)), + Ok(response) => Ok(Some(response)), Err(RpcError::Capacity) => Ok(None), Err(error) => Err(error.into()), } diff --git a/core/crates/hub/federation/src/dispatch.rs b/core/crates/hub/federation/src/dispatch.rs index 8f9303db..aab7a4fa 100644 --- a/core/crates/hub/federation/src/dispatch.rs +++ b/core/crates/hub/federation/src/dispatch.rs @@ -1,10 +1,8 @@ use crate::forward_diagnostics::ForwardStage; use crate::{Federation, node::now_ms, remote_source::RemoteSource}; use std::sync::atomic::Ordering; -use std::time::Duration; use yolo_hub_agent_protocol::{ - Activity, AgentLifecycleState, AgentPromptOutcome, AgentPromptResult, AgentPromptWait, - AgentSnapshot, AgentWaitCondition, AgentWaitRequest, CommandOutcome, MAX_AGENT_WAIT_MS, + AgentPromptOutcome, AgentPromptResult, AgentPromptWait, CommandOutcome, }; use yolo_hub_dispatch::{Dispatcher, Operation, OperationFuture}; use yolo_hub_identity::HubId; diff --git a/core/crates/hub/federation/src/forward_diagnostics.rs b/core/crates/hub/federation/src/forward_diagnostics.rs index cb8acbb7..082b8528 100644 --- a/core/crates/hub/federation/src/forward_diagnostics.rs +++ b/core/crates/hub/federation/src/forward_diagnostics.rs @@ -51,7 +51,7 @@ impl Federation { ( true, link.map(|link| link.id), - link.map(|link| link.connection.is_closed()), + link.map(|link| link.is_closed()), ) } Err(_) => (false, None, None), diff --git a/core/crates/hub/federation/src/hub_inventory.rs b/core/crates/hub/federation/src/hub_inventory.rs index 59feb1a2..8ccc3049 100644 --- a/core/crates/hub/federation/src/hub_inventory.rs +++ b/core/crates/hub/federation/src/hub_inventory.rs @@ -1,6 +1,6 @@ use std::collections::BTreeMap; use yolo_hub_federation_directory::OriginBook; -use yolo_hub_federation_settings::{Configuration, DialTransport}; +use yolo_hub_federation_settings::Configuration; use yolo_hub_identity::HubId; use yolo_hub_rpc_protocol::{HubSummary, MAX_CATALOG_PAGE_HUBS, Reply}; use yolo_hub_terminal_protocol::PublicError; @@ -24,11 +24,13 @@ pub(crate) fn labels(config: &Configuration) -> BTreeMap { .iter() .filter_map(|peer| { let dial = peer.dial.as_ref()?; - let label = match &dial.transport { - DialTransport::Ssh { profile } => profile.profile_id().to_owned(), - DialTransport::Tcp => dial.host.clone(), - }; - Some((peer.identity.hub(), label)) + Some(( + peer.identity.hub(), + dial.ssh_target() + .or_else(|| dial.profile_id()) + .unwrap_or_default() + .to_owned(), + )) }) .collect() } diff --git a/core/crates/hub/federation/src/incoming.rs b/core/crates/hub/federation/src/incoming.rs index 77fa49be..1b67aba4 100644 --- a/core/crates/hub/federation/src/incoming.rs +++ b/core/crates/hub/federation/src/incoming.rs @@ -5,20 +5,21 @@ use anyhow::Result; use std::sync::Arc; use tokio::time::timeout; use yolo_hub_delegation::PeerIdentity; -use yolo_hub_dispatch::{Dispatcher, Operation}; +use yolo_hub_dispatch::Operation; use yolo_hub_peer_protocol::{Call, ExecutionReply, PeerReply, PeerRequest, Purpose, Signed}; +use yolo_hub_peer_io::Duplex; use yolo_hub_peer_rpc::{Handler, Response as RpcResponse, RpcError, RpcFuture}; -use yolo_hub_peer_transport::PeerStream; use yolo_hub_rpc_protocol::{Reply, Response}; use yolo_hub_terminal_protocol::PublicError; impl Federation { - pub(crate) async fn handle( + pub(crate) async fn serve_peer( self: Arc, peer: PeerIdentity, - stream: PeerStream, + stream: impl Duplex + 'static, ) -> Result<()> { self.require_open()?; + let _consumer = self.enter_consumer(peer); let handler = PeerHandler { peer, generation: OwnedGeneration::new(self)?, diff --git a/core/crates/hub/federation/src/lib.rs b/core/crates/hub/federation/src/lib.rs index 08b27f90..db9edad9 100644 --- a/core/crates/hub/federation/src/lib.rs +++ b/core/crates/hub/federation/src/lib.rs @@ -13,13 +13,13 @@ mod observation_cache; mod observation_mailbox; mod observation_worker; mod origin_stream; -mod peer_diagnostics; mod read_reply_admission; mod registry; mod relay; mod remote_source; mod retirement; mod run; +mod status; mod streams; pub use constants::{CONNECT_TIMEOUT, OPERATION_TIMEOUT, POLL_INTERVAL, STOP_TIMEOUT}; diff --git a/core/crates/hub/federation/src/network.rs b/core/crates/hub/federation/src/network.rs index e5a8cdc9..2d83e440 100644 --- a/core/crates/hub/federation/src/network.rs +++ b/core/crates/hub/federation/src/network.rs @@ -1,20 +1,18 @@ -use crate::{ - Federation, constants::*, - peer_diagnostics::{ - INBOUND_PEER_REJECTED, InboundRejections, OutboundFailures, PEER_ATTEMPT_FAILED, - PEER_RECOVERED, PeerAttempt, - }, -}; +use crate::{Federation, constants::*}; use anyhow::Result; use std::sync::Arc; use tokio::{ - net::TcpListener, + net::UnixListener, sync::{Semaphore, watch}, task::JoinSet, time::{Instant, interval}, }; use yolo_hub_federation_settings::{MAX_PEERS, Peer}; -use yolo_hub_peer_transport::PeerPins; +use yolo_hub_peer_diagnostics::{ + INBOUND_PEER_REJECTED, InboundRejections, OutboundFailures, PEER_ATTEMPT_FAILED, + PEER_CONNECTED, PEER_RECOVERED, PeerAttempt, +}; +use yolo_hub_peer_link::{Admission, accept, connect}; impl Federation { pub(crate) async fn dial_loop( @@ -45,34 +43,40 @@ impl Federation { .map_err(|error| (PeerAttempt::Dial, error))?; self.require_open() .map_err(|error| (PeerAttempt::Handshake, error))?; - let (connection, incoming) = yolo_hub_peer_transport::connect_io( + let client = connect( transport, - self.key.clone(), + self.state.peer_identity(), + self.state.as_ref(), peer.identity, deadline, ) .await - .map_err(|error| (PeerAttempt::Handshake, error))?; + .map_err(|error| (PeerAttempt::Handshake, error.into()))?; let link = self - .register(connection, self.hub_id()) + .register(peer.identity, client) .await .map_err(|error| (PeerAttempt::Admission, error))?; - Ok::<_, (PeerAttempt, anyhow::Error)>((link, incoming)) + Ok::<_, (PeerAttempt, anyhow::Error)>(link) }; let attempt = tokio::select! { _ = stop.changed() => break, attempt = attempt => attempt, }; match attempt { - Ok((link, incoming)) => { - if let Some(attempts) = failures.succeed() { - tracing::info!( + Ok(link) => { + match failures.succeed() { + Some(attempts) => tracing::info!( message = PEER_RECOVERED, peer = %peer.identity.hub(), attempts, - ); + ), + None => tracing::info!( + message = PEER_CONNECTED, + peer = %peer.identity.hub(), + ssh = address.ssh_target().unwrap_or("-"), + ), } - let served = self.clone().serve_link(link, incoming, stop.clone()).await; + let served = self.clone().serve_link(link, stop.clone()).await; if let Err(error) = served { tracing::debug!( peer = %peer.identity.hub(), @@ -89,8 +93,9 @@ impl Federation { tracing::warn!( message = PEER_ATTEMPT_FAILED, peer = %peer.identity.hub(), - host = %address.host, - port = address.port.get(), + ssh = address.ssh_target().unwrap_or("-"), + profile = address.profile_id().unwrap_or("-"), + socket = %address.socket().display(), stage = stage.as_str(), attempts, ); @@ -108,12 +113,18 @@ impl Federation { } pub(crate) async fn accept_loop( self: Arc, - listener: TcpListener, + listener: UnixListener, mut stop: watch::Receiver, ) -> Result<()> { let mut tasks = JoinSet::new(); let capacity = Arc::new(Semaphore::new(MAX_PEERS)); - let pins = PeerPins::new(self.config.peers.iter().map(|peer| peer.identity).collect())?; + let allowed: Arc> = Arc::new( + self.config + .peers + .iter() + .map(|peer| peer.identity) + .collect(), + ); let mut rejections = InboundRejections::default(); loop { tokio::select! { @@ -146,21 +157,30 @@ impl Federation { self.state.verify()?; let (stream, _) = accepted?; let Ok(permit) = capacity.clone().try_acquire_owned() else { continue; }; - let node = self.clone(); let pins = pins.clone(); let stop = stop.clone(); + let node = self.clone(); let allowed = allowed.clone(); tasks.spawn(async move { - let _permit = permit; let remote = stream.peer_addr().ok(); let setup = async { - let (connection, incoming) = yolo_hub_peer_transport::accept(stream, node.key.clone(), pins, Instant::now() + CONNECT_TIMEOUT).await?; - let initiator = connection.peer().hub(); - let link = node.register(connection, initiator).await?; - Ok::<_, anyhow::Error>((link, incoming)) - }.await; + let admission = if node.config.ssh_peers.require_registration { + Admission::Only(&allowed) + } else { + Admission::Any + }; + let (peer, stream) = accept( + stream, + node.state.peer_identity(), + node.state.as_ref(), + admission, + Instant::now() + CONNECT_TIMEOUT, + ) + .await?; + let _permit = permit; + node.serve_peer(peer, stream).await + } + .await; match setup { Err(error) => (remote, false, Err(error)), - Ok((link, incoming)) => { - (remote, true, node.serve_link(link, incoming, stop).await) - } + Ok(()) => (remote, true, Ok(())), } }); } diff --git a/core/crates/hub/federation/src/node.rs b/core/crates/hub/federation/src/node.rs index 89efeafe..bec7265a 100644 --- a/core/crates/hub/federation/src/node.rs +++ b/core/crates/hub/federation/src/node.rs @@ -1,6 +1,5 @@ use crate::{ - constants::*, observation_cache::ObservationCache, peer_diagnostics::ExportDepthDenials, - registry::Link, + constants::*, status::ConsumerRegistry, observation_cache::ObservationCache, registry::Link, }; use anyhow::{Result, ensure}; use std::{ @@ -18,11 +17,25 @@ use yolo_hub_federation_directory::OriginBook; use yolo_hub_federation_settings::{Configuration, routing_limits}; use yolo_hub_identity::HubId; use yolo_hub_local_backend::LocalBackend; -use yolo_hub_peer_dialer::{PeerDialer, TcpPeerDialer}; +use yolo_hub_peer_diagnostics::ExportDepthDenials; +use yolo_hub_peer_dialer::{PeerDialFuture, PeerDialer}; use yolo_hub_peer_protocol::{Catalogue, Signed}; use yolo_hub_publication::Publication; use yolo_hub_state::HubState; +struct NoDialer; + +impl PeerDialer for NoDialer { + fn dial<'a>( + &'a self, + _: yolo_hub_delegation::PeerIdentity, + _: &'a yolo_hub_federation_settings::PeerAddress, + _: tokio::time::Instant, + ) -> PeerDialFuture<'a> { + Box::pin(async { anyhow::bail!(NO_DIALER) }) + } +} + pub struct Federation { pub(crate) state: Arc, pub(crate) backend: LocalBackend, @@ -39,8 +52,8 @@ pub struct Federation { pub(crate) directory_revision: AtomicU64, pub(crate) closing: AtomicBool, pub(crate) lifetime: Arc, - pub(crate) key: Arc, pub(crate) dialer: Arc, + pub(crate) consumers: ConsumerRegistry, } impl Federation { pub async fn new( @@ -48,7 +61,7 @@ impl Federation { backend: LocalBackend, config: Configuration, ) -> Result> { - Self::new_with_dialer(state, backend, config, Arc::new(TcpPeerDialer), None).await + Self::new_with_dialer(state, backend, config, Arc::new(NoDialer), None).await } pub async fn new_with_dialer( @@ -85,7 +98,6 @@ impl Federation { let (catalogue_wake, _) = watch::channel(0); let mut book = OriginBook::new(identity, config.pins(), routing_limits())?; let observation_cache = ObservationCache::open(state.clone(), &mut book).await; - let key = Arc::new(state.transport_key()?); let origin: Arc = origin.map_or_else( || Arc::new(backend.clone()) as Arc, |decorator| decorator.decorate(backend.clone()), @@ -106,8 +118,8 @@ impl Federation { directory_revision: AtomicU64::new(0), closing: AtomicBool::new(false), lifetime: Arc::default(), - key, dialer, + consumers: ConsumerRegistry::default(), })) } pub fn hub_id(&self) -> HubId { diff --git a/core/crates/hub/federation/src/registry.rs b/core/crates/hub/federation/src/registry.rs index a15f88d8..ed321a54 100644 --- a/core/crates/hub/federation/src/registry.rs +++ b/core/crates/hub/federation/src/registry.rs @@ -1,28 +1,36 @@ use crate::{Federation, constants::*}; use anyhow::{Result, ensure}; -use std::sync::{Arc, atomic::Ordering}; -use tokio::{ - sync::{mpsc, watch}, - task::JoinSet, - time::interval, +use std::sync::{ + Arc, + atomic::{AtomicBool, Ordering}, }; +use tokio::{sync::watch, time::interval}; use uuid::Uuid; +use yolo_hub_delegation::PeerIdentity; use yolo_hub_identity::HubId; -use yolo_hub_peer_transport::{PeerConnection, PeerStream}; +use yolo_hub_peer_rpc::Client; +/// One outbound, directed peer link: this Hub is the RPC client and the peer +/// is the provider. Calls and discovery share the connection. pub(crate) struct Link { pub id: Uuid, - pub initiator: HubId, - pub connection: Arc, + pub peer: PeerIdentity, + pub client: Client, + closed: AtomicBool, } + +impl Link { + pub(crate) fn is_closed(&self) -> bool { + self.closed.load(Ordering::Acquire) || self.client.is_closed() + } + fn close(&self) { + self.closed.store(true, Ordering::Release); + } +} + impl Federation { - pub(crate) async fn register( - &self, - connection: PeerConnection, - initiator: HubId, - ) -> Result> { + pub(crate) async fn register(&self, peer: PeerIdentity, client: Client) -> Result> { self.state.verify()?; - let peer = connection.peer(); let mut connections = self.connections.write().await; ensure!(!self.closing.load(Ordering::Acquire), CLOSED); ensure!( @@ -32,30 +40,24 @@ impl Federation { INVALID_PEER ); let old = connections.get(&peer.hub()).cloned(); - if let Some(old) = &old { - ensure!( - old.connection.is_closed() || initiator < old.initiator, - DUPLICATE_LINK - ); - } let link = Arc::new(Link { id: Uuid::new_v4(), - initiator, - connection: Arc::new(connection), + peer, + client, + closed: AtomicBool::new(false), }); self.book.lock().await.connect(peer, link.id)?; connections.insert(peer.hub(), link.clone()); drop(connections); if let Some(old) = old { - let _ = old - .connection - .close(tokio::time::Instant::now() + STOP_TIMEOUT) - .await; + old.close(); + let _ = self.book.lock().await.disconnect(peer.hub(), old.id); } Ok(link) } pub(crate) async fn retire(&self, link: &Link) { - let peer = link.connection.peer().hub(); + link.close(); + let peer = link.peer.hub(); let mut connections = self.connections.write().await; if connections .get(&peer) @@ -64,63 +66,41 @@ impl Federation { connections.remove(&peer); let _ = self.book.lock().await.disconnect(peer, link.id); } - drop(connections); - let _ = link - .connection - .close(tokio::time::Instant::now() + STOP_TIMEOUT) - .await; } pub(crate) async fn current(&self, peer: HubId) -> Option> { self.connections .read() .await .get(&peer) - .filter(|link| !link.connection.is_closed()) + .filter(|link| !link.is_closed()) .cloned() } pub(crate) async fn serve_link( self: Arc, link: Arc, - mut incoming: mpsc::Receiver, mut stop: watch::Receiver, ) -> Result<()> { - let mut tasks = JoinSet::new(); let mut tick = interval(POLL_INTERVAL); - let peer = link.connection.peer(); let consume = self .config - .peer(peer.hub()) + .peer(link.peer.hub()) .is_some_and(|peer| peer.consume); - let polling = async { - if !consume { - return std::future::pending::>().await; - } - let mut tick = interval(POLL_INTERVAL); - loop { - tick.tick().await; - // Local admission pressure defers only this refresh; it does not - // invalidate the authenticated link or extend any routing proof. - self.discover(&link).await?; - } - }; - tokio::pin!(polling); loop { tokio::select! { _ = stop.changed() => break, - _ = &mut polling => break, - _ = tasks.join_next(), if !tasks.is_empty() => {}, _ = tick.tick() => { - if self.is_closing() || link.connection.is_closed() { break; } - } - incoming = incoming.recv() => match incoming { - None => break, - Some(stream) => { let node = self.clone(); tasks.spawn(async move { node.handle(peer, stream).await }); } + if self.is_closing() || link.is_closed() { break; } + if !consume { continue; } + // Local admission pressure defers only this refresh; it does not + // invalidate the authenticated link or extend any routing proof. + if let Err(error) = self.discover(&link).await { + tracing::debug!(peer = %link.peer.hub(), %error, "Hub peer discovery ended the link"); + break; + } } } } - tasks.abort_all(); - while tasks.join_next().await.is_some() {} self.retire(&link).await; Ok(()) } diff --git a/core/crates/hub/federation/src/relay.rs b/core/crates/hub/federation/src/relay.rs index abe22f76..fb1623fe 100644 --- a/core/crates/hub/federation/src/relay.rs +++ b/core/crates/hub/federation/src/relay.rs @@ -5,7 +5,7 @@ use yolo_hub_delegation::PeerIdentity; use yolo_hub_peer_protocol::{ Call, ExecutionReply, PeerReply, PeerRequest, Signed, required_rights, }; -use yolo_hub_peer_rpc::{Client, RemoteUpdates}; +use yolo_hub_peer_rpc::RemoteUpdates; use yolo_hub_terminal_protocol::PublicError; impl Federation { @@ -30,6 +30,8 @@ impl Federation { let policy = self .config .export(peer.hub(), origin.hub()) + .cloned() + .or_else(|| self.config.shared_export(origin.hub(), self.hub_id())) .ok_or(PublicError::Unauthorized)?; let Some(required) = required_rights(&call.payload.request.fields().command) else { // Local management never crosses a peer boundary. @@ -81,14 +83,6 @@ impl Federation { .fields() .command .operation_timeout(OPERATION_TIMEOUT); - let stream = connection - .connection - .open_channel(deadline) - .await - .map_err(|_| failed(ForwardStage::ChannelOpen, PublicError::Unreachable))?; - let channel = Client::connect(stream, deadline) - .await - .map_err(|_| failed(ForwardStage::ChannelOpen, PublicError::Unreachable))?; self.require_open() .map_err(|_| failed(ForwardStage::BeforeSend, PublicError::Unreachable))?; let mutation = call.payload.request.fields().command.mutates(); @@ -99,7 +93,8 @@ impl Federation { PublicError::Unreachable } }; - let response = channel + let response = connection + .client .request(PeerRequest::Execute { call: call.clone() }, deadline) .await .map_err(|_| failed(ForwardStage::Receive, uncertain()))?; diff --git a/core/crates/hub/federation/src/run.rs b/core/crates/hub/federation/src/run.rs index 9dc92a9a..d3d01b8b 100644 --- a/core/crates/hub/federation/src/run.rs +++ b/core/crates/hub/federation/src/run.rs @@ -2,7 +2,7 @@ use crate::{Federation, constants::*, node::now_ms}; use anyhow::Result; use std::sync::Arc; use tokio::{ - net::TcpListener, + net::UnixListener, sync::watch, task::JoinSet, time::{Instant, interval, timeout_at}, @@ -11,7 +11,7 @@ use tokio::{ impl Federation { pub async fn run( self: Arc, - listener: Option, + listener: Option, mut shutdown: watch::Receiver, ) -> Result<()> { if *shutdown.borrow() || self.is_closing() { @@ -43,7 +43,7 @@ impl Federation { .config .peers .iter() - .filter(|peer| peer.dial.is_some()) + .filter(|peer| peer.consume && peer.dial.is_some()) .cloned() { tasks.spawn(self.clone().dial_loop(peer, stopped.clone())); @@ -73,12 +73,7 @@ impl Federation { while tasks.join_next().await.is_some() {} let connections = std::mem::take(&mut *self.connections.write().await); for link in connections.into_values() { - let _ = self - .book - .lock() - .await - .disconnect(link.connection.peer().hub(), link.id); - let _ = link.connection.close(Instant::now() + STOP_TIMEOUT).await; + self.retire(&link).await; } self.observation_cache .wait_closed(Instant::now() + STOP_TIMEOUT) diff --git a/core/crates/hub/federation/src/status.rs b/core/crates/hub/federation/src/status.rs new file mode 100644 index 00000000..e3a1c0a2 --- /dev/null +++ b/core/crates/hub/federation/src/status.rs @@ -0,0 +1,87 @@ +use crate::node::{Federation, now_ms}; +use std::{ + collections::BTreeMap, + sync::{Arc, Mutex, MutexGuard}, +}; +use yolo_hub_delegation::PeerIdentity; +use yolo_hub_identity::HubId; +use yolo_hub_rpc_protocol::ConsumerSummary; + +impl Federation { + /// Currently open directed links, for diagnostics and acceptance checks. + pub async fn connected_peers(&self) -> Vec { + self.connections + .read() + .await + .iter() + .filter(|(_, link)| !link.is_closed()) + .map(|(hub, _)| *hub) + .collect() + } +} + +struct Session { + since_ms: u64, + links: u16, +} + +/// Live inbound links keyed by the importing machine's Hub identity. +#[derive(Default)] +pub(crate) struct ConsumerRegistry { + sessions: Mutex>, +} + +impl ConsumerRegistry { + fn lock(&self) -> MutexGuard<'_, BTreeMap> { + self.sessions + .lock() + .unwrap_or_else(|poisoned| poisoned.into_inner()) + } +} + +/// Removes one inbound link when the peer session ends. +pub(crate) struct ConsumerGuard { + federation: Arc, + hub: HubId, +} + +impl Federation { + pub(crate) fn enter_consumer(self: &Arc, peer: PeerIdentity) -> ConsumerGuard { + let hub = peer.hub(); + let mut sessions = self.consumers.lock(); + let session = sessions.entry(hub).or_insert_with(|| Session { + since_ms: now_ms().unwrap_or_default(), + links: 0, + }); + session.links = session.links.saturating_add(1); + ConsumerGuard { + federation: self.clone(), + hub, + } + } + + pub fn consumers(&self) -> Vec { + self.consumers + .lock() + .iter() + .map(|(hub, session)| ConsumerSummary { + hub: *hub, + connected_since_ms: session.since_ms, + links: session.links, + }) + .collect() + } +} + +impl Drop for ConsumerGuard { + fn drop(&mut self) { + let mut sessions = self.federation.consumers.lock(); + let Some(session) = sessions.get_mut(&self.hub) else { + return; + }; + session.links = session.links.saturating_sub(1); + if session.links == 0 { + sessions.remove(&self.hub); + } + } +} diff --git a/core/crates/hub/federation/test_support/network_capacity_constants.rs b/core/crates/hub/federation/test_support/network_capacity_constants.rs index 2f6b9775..d0754650 100644 --- a/core/crates/hub/federation/test_support/network_capacity_constants.rs +++ b/core/crates/hub/federation/test_support/network_capacity_constants.rs @@ -4,9 +4,7 @@ pub const TEST_TIMEOUT: Duration = Duration::from_secs(45); pub const PROGRESS_POLL: Duration = Duration::from_millis(10); pub const OBSERVED_POLL_CYCLES: u32 = 2; pub const DUPLEX_BYTES: usize = 1024; -pub const TEST_LISTEN_ADDRESS: &str = "127.0.0.1:0"; pub const LOCAL_DIRECTORY: &str = "local"; pub const PEER_DIRECTORY: &str = "peer"; pub const REVISION_STEP: u64 = 1; pub const SIGNATURE_CORRUPTION_MASK: u8 = 1; -pub const STREAM_BYTES: &[u8] = b"persistent terminal\0\r\n\x1b[32m"; diff --git a/core/crates/hub/federation/test_support/network_capacity_fixture.rs b/core/crates/hub/federation/test_support/network_capacity_fixture.rs index 29e5bb44..6f8e0efd 100644 --- a/core/crates/hub/federation/test_support/network_capacity_fixture.rs +++ b/core/crates/hub/federation/test_support/network_capacity_fixture.rs @@ -1,15 +1,11 @@ use super::{constants::*, peer::DirectoryPeer}; use crate::{Federation, registry::Link}; use std::sync::Arc; -use tokio::{ - net::{TcpListener, TcpStream}, - sync::mpsc, - time::{Instant, timeout}, -}; -use yolo_hub_federation_settings::{Configuration, Peer}; +use tokio::time::Instant; +use yolo_hub_federation_settings::{Configuration, Peer, PeerAddress}; use yolo_hub_local_backend::LocalBackend; +use yolo_hub_peer_link::{Admission, accept, connect}; use yolo_hub_peer_protocol::FIRST_REVISION; -use yolo_hub_peer_transport::{PeerConnection, PeerPins, PeerStream}; use yolo_hub_state::HubState; #[path = "constants.rs"] @@ -18,12 +14,8 @@ mod paths; pub struct LinkFixture { pub node: Arc, pub link: Arc, - pub peer: Arc, pub responder: Arc, - pub incoming: Option>, - pub peer_incoming: Option>, - pub output: PeerStream, - pub input: PeerStream, + pub server: tokio::task::JoinHandle<()>, _root: tempfile::TempDir, } @@ -51,7 +43,13 @@ impl LinkFixture { Configuration { peers: vec![Peer { identity: peer_state.peer_identity(), - dial: None, + dial: Some( + PeerAddress::ssh( + "in-process", + peer_state.peer_socket().unwrap(), + ) + .unwrap(), + ), consume: true, exports: Vec::new(), }], @@ -60,58 +58,54 @@ impl LinkFixture { ) .await .unwrap(); - let listener = TcpListener::bind(TEST_LISTEN_ADDRESS).await.unwrap(); - let (local_socket, peer_socket) = tokio::join!( - TcpStream::connect(listener.local_addr().unwrap()), - listener.accept(), - ); + let responder = Arc::new(DirectoryPeer::new(peer_state.clone(), local.peer_identity())); + let (local_stream, peer_stream) = tokio::io::duplex(DUPLEX_BYTES); let deadline = Instant::now() + TEST_TIMEOUT; - let (local_connection, peer_connection) = tokio::join!( - yolo_hub_peer_transport::connect( - local_socket.unwrap(), - node.key.clone(), - peer_state.peer_identity(), - deadline - ), - yolo_hub_peer_transport::accept( - peer_socket.unwrap().0, - Arc::new(peer_state.transport_key().unwrap()), - PeerPins::new(vec![local.peer_identity()]).unwrap(), - deadline - ), - ); - let (local_connection, incoming) = local_connection.unwrap(); - let (peer_connection, mut peer_incoming) = peer_connection.unwrap(); + let local_identity = local.peer_identity(); + let remote_identity = peer_state.peer_identity(); + let served = responder.clone(); + let server_state = peer_state.clone(); + let server = tokio::spawn(async move { + let (_, stream) = accept( + peer_stream, + remote_identity, + server_state.as_ref(), + Admission::Only(&[local_identity]), + deadline, + ) + .await + .unwrap(); + let _ = yolo_hub_peer_rpc::serve(stream, served).await; + }); + let client = connect( + local_stream, + local_identity, + local.as_ref(), + remote_identity, + deadline, + ) + .await + .unwrap(); let link = node - .register(local_connection, local.hub_id()) + .register(remote_identity, client) .await .unwrap(); - let responder = Arc::new(DirectoryPeer::new(peer_state, local.peer_identity())); node.book .lock() .await .install( - link.connection.peer(), + link.peer, link.id, FIRST_REVISION, vec![responder.export.clone()], crate::node::now_ms().unwrap(), ) .unwrap(); - let output = link.connection.open_channel(deadline).await.unwrap(); - let input = timeout(TEST_TIMEOUT, peer_incoming.recv()) - .await - .unwrap() - .unwrap(); Self { node, link, - peer: Arc::new(peer_connection), responder, - incoming: Some(incoming), - peer_incoming: Some(peer_incoming), - output, - input, + server, _root: root, } } diff --git a/core/crates/hub/federation/test_support/network_capacity_observation.rs b/core/crates/hub/federation/test_support/network_capacity_observation.rs index 7de9ddae..666dc3ca 100644 --- a/core/crates/hub/federation/test_support/network_capacity_observation.rs +++ b/core/crates/hub/federation/test_support/network_capacity_observation.rs @@ -1,37 +1,23 @@ use super::{constants::*, fixture::LinkFixture}; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, - time::{Instant, timeout}, -}; +use tokio::time::{Instant, timeout}; use yolo_hub_peer_protocol::FIRST_REVISION; use yolo_hub_peer_protocol_test_support::support::observe; impl LinkFixture { - pub async fn assert_retained(&mut self) { + pub async fn assert_retained(&self) { assert_eq!( self.node - .current(self.link.connection.peer().hub()) + .current(self.link.peer.hub()) .await .unwrap() .id, self.link.id ); - assert!(!self.link.connection.is_closed()); + assert!(!self.link.is_closed()); self.assert_route(crate::node::now_ms().unwrap(), true) .await; self.assert_route(self.responder.export.catalogue.payload.expires_at_ms, false) .await; - timeout(TEST_TIMEOUT, async { - self.output.write_all(STREAM_BYTES).await.unwrap(); - let mut bytes = vec![u8::default(); STREAM_BYTES.len()]; - self.input.read_exact(&mut bytes).await.unwrap(); - assert_eq!(bytes, STREAM_BYTES); - self.input.write_all(STREAM_BYTES).await.unwrap(); - self.output.read_exact(&mut bytes).await.unwrap(); - assert_eq!(bytes, STREAM_BYTES); - }) - .await - .unwrap(); } async fn assert_route(&self, now: u64, present: bool) { @@ -40,7 +26,7 @@ impl LinkFixture { .book .lock() .await - .route_for(self.link.connection.peer().hub(), &observe(), now,) + .route_for(self.link.peer.hub(), &observe(), now,) .unwrap() .is_some(), present @@ -55,7 +41,7 @@ impl LinkFixture { .book .lock() .await - .latest_for(self.link.connection.peer().hub()) + .latest_for(self.link.peer.hub()) .is_some_and(|source| source.payload.revision > FIRST_REVISION); if revised { break; @@ -68,10 +54,7 @@ impl LinkFixture { } pub async fn close(&self) { - self.peer - .close(Instant::now() + TEST_TIMEOUT) - .await - .unwrap(); + self.server.abort(); self.node.begin_shutdown(); self.node .observation_cache diff --git a/core/crates/hub/federation/test_support/network_capacity_peer.rs b/core/crates/hub/federation/test_support/network_capacity_peer.rs index 5fac735d..ef2218df 100644 --- a/core/crates/hub/federation/test_support/network_capacity_peer.rs +++ b/core/crates/hub/federation/test_support/network_capacity_peer.rs @@ -1,8 +1,9 @@ use super::constants::*; use std::sync::{ Arc, - atomic::{AtomicBool, AtomicU64, Ordering}, + atomic::{AtomicBool, AtomicU64, AtomicUsize, Ordering}, }; +use tokio::sync::{Notify, Semaphore}; use yolo_hub_delegation::{ DelegationChain, FIRST_POLICY_EPOCH, GrantClaims, PeerIdentity, SignedGrant, }; @@ -17,6 +18,9 @@ use yolo_hub_state::HubState; pub struct DirectoryPeer { pub export: ExportedOrigin, pub invalid: AtomicBool, + pub gate: Arc, + active: AtomicUsize, + admitted: Notify, state: Arc, revision: AtomicU64, } @@ -64,18 +68,38 @@ impl DirectoryPeer { reachable: true, }, invalid: AtomicBool::new(false), + gate: Arc::new(Semaphore::new(yolo_capnp_rpc::MAX_IN_FLIGHT)), + active: AtomicUsize::new(0), + admitted: Notify::new(), state, revision: AtomicU64::new(FIRST_REVISION + REVISION_STEP), } } } +impl DirectoryPeer { + /// Waits until `count` handlers are blocked on the gate. Each one holds a + /// client-side in-flight slot, so capacity pressure is deterministic. + pub async fn wait_admitted(&self, count: usize) { + loop { + let admitted = self.admitted.notified(); + if self.active.load(Ordering::Acquire) >= count { + return; + } + admitted.await; + } + } +} + impl Handler for DirectoryPeer { fn request(&self, request: PeerRequest) -> RpcFuture<'_, Response> { Box::pin(async move { let PeerRequest::Discover { request_id, .. } = request else { return Err(RpcError::Invalid); }; + self.active.fetch_add(1, Ordering::AcqRel); + self.admitted.notify_waiters(); + let _permit = self.gate.acquire().await.map_err(|_| RpcError::Closed)?; let revision = self.revision.fetch_add(REVISION_STEP, Ordering::AcqRel); let mut export = self.export.clone(); let mut catalogue = export.catalogue.payload; diff --git a/core/crates/hub/federation/test_support/network_capacity_pool.rs b/core/crates/hub/federation/test_support/network_capacity_pool.rs index 163d2fbc..814f2911 100644 --- a/core/crates/hub/federation/test_support/network_capacity_pool.rs +++ b/core/crates/hub/federation/test_support/network_capacity_pool.rs @@ -1,69 +1,68 @@ use super::{constants::*, fixture::LinkFixture}; use tokio::{ - io::DuplexStream, - time::{Instant, timeout_at}, + sync::OwnedSemaphorePermit, + task::JoinHandle, + time::{Instant, timeout}, }; -use yolo_hub_peer_rpc::{Client, RpcError}; -use yolo_hub_peer_transport::{ChannelAdmissionError, MAX_PEER_CHANNELS, PeerStream}; +use yolo_capnp_rpc::MAX_IN_FLIGHT; +use uuid::Uuid; +use yolo_hub_peer_protocol::{PEER_PROTOCOL_VERSION, PeerRequest}; +use yolo_hub_peer_rpc::RpcError; -pub enum Pressure { - Ssh(Vec<(PeerStream, PeerStream)>), - Rpc(Vec<(Client, DuplexStream)>), +pub struct Pressure { + _permits: OwnedSemaphorePermit, + tasks: Vec>, } impl Pressure { - pub async fn ssh(fixture: &mut LinkFixture) -> Self { - let mut channels = Vec::new(); - loop { - assert!(channels.len() <= MAX_PEER_CHANNELS); - match fixture - .link - .connection - .open_channel(Instant::now() + TEST_TIMEOUT) - .await - { - Ok(outgoing) => { - let incoming = timeout_at( - Instant::now() + TEST_TIMEOUT, - fixture.peer_incoming.as_mut().unwrap().recv(), - ) - .await - .unwrap() - .unwrap(); - channels.push((outgoing, incoming)); - } - Err(error) => { - assert_eq!( - error.downcast_ref::(), - Some(&ChannelAdmissionError::AtCapacity), - ); - assert!(!channels.is_empty()); - return Self::Ssh(channels); - } - } + /// Occupies every local in-flight slot with requests whose server handlers + /// wait on the gate, so the next request observes typed local capacity. + pub async fn rpc(fixture: &LinkFixture) -> Self { + let gate = fixture.responder.gate.clone(); + let permits = gate + .clone() + .acquire_many_owned(MAX_IN_FLIGHT as u32) + .await + .unwrap(); + let mut tasks = Vec::new(); + for _ in 0..MAX_IN_FLIGHT { + let client = fixture.link.client.clone(); + tasks.push(tokio::spawn(async move { + let request = PeerRequest::Discover { + request_id: Uuid::new_v4(), + version: PEER_PROTOCOL_VERSION, + allowed_origins: Vec::new(), + }; + let _ = client.request(request, Instant::now() + TEST_TIMEOUT).await; + })); } - } - - pub async fn rpc() -> Self { - let mut connections = Vec::new(); - loop { - assert!(connections.len() <= yolo_capnp_rpc::MAX_CONNECTIONS); - let (local, remote) = tokio::io::duplex(DUPLEX_BYTES); - match Client::connect(local, Instant::now() + TEST_TIMEOUT).await { - Ok(client) => connections.push((client, remote)), - Err(error) => { - assert_eq!(error, RpcError::Capacity); - assert!(!connections.is_empty()); - return Self::Rpc(connections); - } - } + timeout(TEST_TIMEOUT, fixture.responder.wait_admitted(MAX_IN_FLIGHT)) + .await + .unwrap(); + let probe = PeerRequest::Discover { + request_id: Uuid::new_v4(), + version: PEER_PROTOCOL_VERSION, + allowed_origins: Vec::new(), + }; + assert!(matches!( + fixture + .link + .client + .request(probe, Instant::now() + TEST_TIMEOUT) + .await, + Err(RpcError::Capacity) + )); + Self { + _permits: permits, + tasks, } } +} - pub fn retained(&self) -> usize { - match self { - Self::Ssh(channels) => channels.len(), - Self::Rpc(connections) => connections.len(), +impl Drop for Pressure { + fn drop(&mut self) { + for task in &self.tasks { + task.abort(); } } } diff --git a/core/crates/hub/federation/test_support/network_capacity_running.rs b/core/crates/hub/federation/test_support/network_capacity_running.rs index 8f42f332..e294730d 100644 --- a/core/crates/hub/federation/test_support/network_capacity_running.rs +++ b/core/crates/hub/federation/test_support/network_capacity_running.rs @@ -18,30 +18,8 @@ impl Running { let mut tasks = JoinSet::new(); let node = fixture.node.clone(); let link = fixture.link.clone(); - let incoming = fixture.incoming.take().unwrap(); tasks.spawn(async move { - let _ = done.send(node.serve_link(link, incoming, stopped).await); - }); - let mut stopped = stop.subscribe(); - let mut incoming = fixture.peer_incoming.take().unwrap(); - let responder = fixture.responder.clone(); - tasks.spawn(async move { - let mut channels = JoinSet::new(); - loop { - tokio::select! { - _ = stopped.changed() => break, - _ = channels.join_next(), if !channels.is_empty() => {}, - channel = incoming.recv() => { - let Some(channel) = channel else { break; }; - let responder = responder.clone(); - channels.spawn(async move { - let _ = yolo_hub_peer_rpc::serve(channel, responder).await; - }); - } - } - } - channels.abort_all(); - while channels.join_next().await.is_some() {} + let _ = done.send(node.serve_link(link, stopped).await); }); Self { finished, diff --git a/core/crates/hub/federation/unit_tests/discovery_channel.rs b/core/crates/hub/federation/unit_tests/discovery_channel.rs index 93142bbb..907467d6 100644 --- a/core/crates/hub/federation/unit_tests/discovery_channel.rs +++ b/core/crates/hub/federation/unit_tests/discovery_channel.rs @@ -1,4 +1,4 @@ -use super::classify_connection; +use super::classify; use yolo_hub_peer_rpc::RpcError; #[test] @@ -12,16 +12,12 @@ fn failed_connection_does_not_become_a_successful_discovery_deferral() { // An identical diagnostic from a remote peer conveys no local admission proof. RpcError::Remote(RpcError::Capacity.to_string()), ] { - let error = classify_connection(Err(failure.clone())).err().unwrap(); + let error = classify(Err(failure.clone())).err().unwrap(); assert_eq!(error.downcast_ref::(), Some(&failure)); } } #[test] fn only_typed_local_capacity_allows_the_existing_link_to_continue() { - assert!( - classify_connection(Err(RpcError::Capacity)) - .unwrap() - .is_none() - ); + assert!(classify(Err(RpcError::Capacity)).unwrap().is_none()); } diff --git a/core/crates/hub/jsonrpc/protocol/src/constants.rs b/core/crates/hub/jsonrpc/protocol/src/constants.rs index 7591634d..0c0d29c3 100644 --- a/core/crates/hub/jsonrpc/protocol/src/constants.rs +++ b/core/crates/hub/jsonrpc/protocol/src/constants.rs @@ -46,6 +46,7 @@ pub(crate) mod reply_tag { pub const TERMINAL_HISTORY: &str = "terminal_history"; pub const CREATION_TERMINATED: &str = "creation_terminated"; pub const CONFIGURATION: &str = "configuration"; + pub const CONSUMERS: &str = "consumers"; pub const FILE_ROOTS: &str = "file_roots"; pub const FILE: &str = "file"; pub const AGENT_CONTROL: &str = "agent_control"; diff --git a/core/crates/hub/jsonrpc/protocol/src/result.rs b/core/crates/hub/jsonrpc/protocol/src/result.rs index f8014243..bdf108c9 100644 --- a/core/crates/hub/jsonrpc/protocol/src/result.rs +++ b/core/crates/hub/jsonrpc/protocol/src/result.rs @@ -115,6 +115,7 @@ pub(crate) fn decode(value: Value, method: Method) -> Result SubscribeEvents => reply_tag::EVENT_SUBSCRIPTION, CleanupTerminate => reply_tag::CREATION_TERMINATED, GetConfiguration | Configure => reply_tag::CONFIGURATION, + ListConsumers => reply_tag::CONSUMERS, ListFileRoots => reply_tag::FILE_ROOTS, ReadFile => reply_tag::FILE, AgentControl => reply_tag::AGENT_CONTROL, diff --git a/core/crates/hub/listener/binding/BUILD.bazel b/core/crates/hub/listener/binding/BUILD.bazel deleted file mode 100644 index 1f25da79..00000000 --- a/core/crates/hub/listener/binding/BUILD.bazel +++ /dev/null @@ -1,55 +0,0 @@ -load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") -load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") - -package(default_visibility = ["//visibility:public"]) - -_DEPS = [ - "//core/crates/hub/delegation:yolo_hub_delegation", - "//core/crates/hub/management/protocol:yolo_hub_management_protocol", - "//core/crates/persistence/paths:yolo_paths", - "@crates//:anyhow", - "@crates//:serde", -] - -rust_library( - name = "yolo_hub_listener_binding", - srcs = glob(["src/**/*.rs"]), - crate_root = "src/lib.rs", - crate_name = "yolo_hub_listener_binding", - edition = "2024", - version = YOLO_MARKETING_VERSION, - deps = _DEPS, -) - -rust_test( - name = "unit_tests", - srcs = glob(["src/**/*.rs", "unit_tests/**/*.rs", "test_support/constants.rs"]), - crate_root = "src/lib.rs", - crate_name = "yolo_hub_listener_binding_unit_tests", - edition = "2024", - deps = _DEPS + [ - "//core/crates/hub/federation/settings:yolo_hub_federation_settings", - "//core/crates/hub/identity:yolo_hub_identity", - "@crates//:serde_json", - "@crates//:uuid", - ], -) - -rust_test( - name = "integration_tests", - srcs = glob(["integration_tests/**/*.rs", "test_support/**/*.rs"]), - crate_root = "integration_tests/lib.rs", - crate_name = "yolo_hub_listener_binding_integration_tests", - edition = "2024", - deps = _DEPS + [ - ":yolo_hub_listener_binding", - "//core/crates/hub/configuration/store:yolo_hub_configuration_store", - "//core/crates/hub/federation/settings:yolo_hub_federation_settings", - "//core/crates/hub/state:yolo_hub_state", - "@crates//:serde_json", - "@crates//:tempfile", - "@crates//:uuid", - ], -) - -filegroup(name = "architecture_check_data", srcs = glob(["**/*"], exclude = ["BUILD.bazel"]) + ["BUILD.bazel"]) diff --git a/core/crates/hub/listener/binding/README.md b/core/crates/hub/listener/binding/README.md deleted file mode 100644 index 6798613a..00000000 --- a/core/crates/hub/listener/binding/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# Hub listener bindings - -This component persists the actual address allocated for an explicitly requested loopback -listener with port zero. It retains the Hub's full public identity, state-root object identity, -file identity and a coordination lock. The management runtime keeps the real listener open -while its address is committed; no probe socket is released and reopened. - -The record keeps at most two complete configuration states: the currently committed state -and one prepared successor. Management prepares the successor binding before the existing -configuration compare-and-swap. A restart selects the slot whose entire configuration state -matches durable configuration, covering crashes on either side of that write without altering -the user's Configure request, revision, change identifier or grants. - -An explicit disabled listener or nonzero address has no saved allocation in its slot. A later -port-zero request receives a fresh allocation; stale slots never authorize another listener. -An unchanged loopback port-zero request retains its existing address across configuration -changes and process restarts. An occupied saved port, replaced record or replaced root fails -closed. A missing record cannot recreate an already configured loopback port-zero listener. - -Configuration storage also retains a required, private `listener_binding_initialized` flag. -It begins false in the same write that creates the configuration. This durable evidence permits -recovery if first startup stops before a binding record exists. A retained real listener and its -durable binding must pass the configuration commit fence before the flag becomes true; the -generation starts afterwards. An existing binding is always reused, including while the flag -is false. Configuration changes preserve the flag. Current private schema records with missing -fields are rejected. Configuration storage separately upgrades the exact old unversioned shape -only for disabled or fixed listeners; a legacy port-zero document cannot impersonate interrupted -first initialization. - -The initial unassigned slot permits recovery of an interrupted first allocation; the runtime -cannot announce an applied listener until its actual address and initialization evidence are -durable. Unit-level model validation and filesystem integration tests remain outside `src`. diff --git a/core/crates/hub/listener/binding/integration_tests/allocation.rs b/core/crates/hub/listener/binding/integration_tests/allocation.rs deleted file mode 100644 index 2c189d7b..00000000 --- a/core/crates/hub/listener/binding/integration_tests/allocation.rs +++ /dev/null @@ -1,83 +0,0 @@ -use super::{constants::*, fixture::*}; -use yolo_hub_listener_binding::ListenerBindings; - -#[test] -fn allocation_survives_reopen_and_initial_interruption_without_rewriting_configuration() { - let fixture = Fixture::new(true); - let before = fixture.configuration.current().unwrap(); - let bindings = fixture.open(true).unwrap(); - assert_eq!(bindings.address(&before).unwrap(), None); - assert!(bindings.verify(&before).is_err()); - drop(bindings); - let mut bindings = fixture.open(false).unwrap(); - bindings - .establish(&before, ALLOCATED.parse().unwrap()) - .unwrap(); - assert!( - bindings - .establish(&before, NEXT_ALLOCATED.parse().unwrap()) - .is_err() - ); - bindings.verify(&before).unwrap(); - let bytes = std::fs::read(fixture.path()).unwrap(); - drop(bindings); - let bindings = fixture.open(false).unwrap(); - assert_eq!( - bindings.address(&before).unwrap(), - Some(ALLOCATED.parse().unwrap()) - ); - assert_eq!(std::fs::read(fixture.path()).unwrap(), bytes); - assert_eq!(fixture.configuration.current().unwrap(), before); -} - -#[test] -fn existing_ephemeral_configuration_cannot_recreate_a_missing_binding() { - let fixture = Fixture::new(true); - assert!(fixture.open(false).is_err()); - assert!(!fixture.path().exists()); - let before = fixture.configuration.current().unwrap(); - let mut bindings = fixture.open(true).unwrap(); - assert!( - bindings - .establish(&before, WRONG_IP.parse().unwrap()) - .is_err() - ); - assert!( - bindings - .establish(&before, LOOPBACK.parse().unwrap()) - .is_err() - ); - bindings - .establish(&before, ALLOCATED.parse().unwrap()) - .unwrap(); - assert!(fixture.open(false).is_err()); - drop(bindings); - std::fs::rename(fixture.path(), fixture.directory.path().join(SAVED)).unwrap(); - assert!(fixture.open(false).is_err()); - assert!(!fixture.path().exists()); -} - -#[test] -fn saved_binding_rejects_wrong_hub_identity_and_unrelated_configuration() { - let fixture = Fixture::new(false); - drop(fixture.open(false).unwrap()); - let foreign = Fixture::new(false); - assert!( - ListenerBindings::open( - fixture.hub.directory().unwrap(), - foreign.hub.peer_identity(), - &foreign.configuration.current().unwrap(), - false, - ) - .is_err() - ); - let bindings = fixture.open(false).unwrap(); - let mut unrelated = fixture.configuration.current().unwrap(); - unrelated.last_change = uuid::Uuid::new_v4(); - assert!(bindings.address(&unrelated).is_err()); - assert!( - bindings - .verify(&fixture.configuration.current().unwrap()) - .is_err() - ); -} diff --git a/core/crates/hub/listener/binding/integration_tests/interference.rs b/core/crates/hub/listener/binding/integration_tests/interference.rs deleted file mode 100644 index 2730c7f3..00000000 --- a/core/crates/hub/listener/binding/integration_tests/interference.rs +++ /dev/null @@ -1,53 +0,0 @@ -use super::{constants::*, fixture::*}; -use std::os::unix::fs::PermissionsExt; - -#[test] -fn same_bytes_replacement_and_aba_restoration_permanently_revoke_live_store() { - let fixture = Fixture::new(false); - let bindings = fixture.open(false).unwrap(); - let before = fixture.configuration.current().unwrap(); - let bytes = std::fs::read(fixture.path()).unwrap(); - let original = fixture.directory.path().join(SAVED); - std::fs::rename(fixture.path(), &original).unwrap(); - std::fs::write(fixture.path(), bytes).unwrap(); - std::fs::set_permissions( - fixture.path(), - std::fs::Permissions::from_mode(PRIVATE_MODE), - ) - .unwrap(); - assert!(bindings.verify(&before).is_err()); - std::fs::rename(fixture.path(), fixture.directory.path().join(REPLACEMENT)).unwrap(); - std::fs::rename(original, fixture.path()).unwrap(); - assert!(bindings.verify(&before).is_err()); -} - -#[test] -fn retained_state_root_replacement_never_publishes_into_the_new_namespace() { - let fixture = Fixture::new(false); - let mut bindings = fixture.open(false).unwrap(); - let before = fixture.configuration.current().unwrap(); - let next = fixture.configuration.planned(&fixture.change()).unwrap(); - let root = fixture.hub.directory().unwrap().path().to_path_buf(); - let path = fixture.path(); - std::fs::rename(&root, fixture.directory.path().join(SAVED)).unwrap(); - std::fs::create_dir(&root).unwrap(); - std::fs::set_permissions(&root, std::fs::Permissions::from_mode(DIRECTORY_MODE)).unwrap(); - assert!(bindings.prepare(&before, &next, None).is_err()); - assert!(!path.exists()); -} - -#[test] -fn in_place_record_corruption_is_not_overwritten_by_a_prepared_successor() { - let fixture = Fixture::new(false); - let mut bindings = fixture.open(false).unwrap(); - let before = fixture.configuration.current().unwrap(); - let next = fixture.configuration.planned(&fixture.change()).unwrap(); - let mut record: serde_json::Value = - serde_json::from_slice(&std::fs::read(fixture.path()).unwrap()).unwrap(); - record[CURRENT_FIELD][CONFIGURATION_FIELD][CHANGE_FIELD] = - uuid::Uuid::new_v4().to_string().into(); - let bytes = serde_json::to_vec(&record).unwrap(); - std::fs::write(fixture.path(), &bytes).unwrap(); - assert!(bindings.prepare(&before, &next, None).is_err()); - assert_eq!(std::fs::read(fixture.path()).unwrap(), bytes); -} diff --git a/core/crates/hub/listener/binding/integration_tests/lib.rs b/core/crates/hub/listener/binding/integration_tests/lib.rs deleted file mode 100644 index dbccfb15..00000000 --- a/core/crates/hub/listener/binding/integration_tests/lib.rs +++ /dev/null @@ -1,7 +0,0 @@ -mod allocation; -#[path = "../test_support/constants.rs"] -mod constants; -#[path = "../test_support/fixture.rs"] -mod fixture; -mod interference; -mod transitions; diff --git a/core/crates/hub/listener/binding/integration_tests/transitions.rs b/core/crates/hub/listener/binding/integration_tests/transitions.rs deleted file mode 100644 index a82823cd..00000000 --- a/core/crates/hub/listener/binding/integration_tests/transitions.rs +++ /dev/null @@ -1,70 +0,0 @@ -use super::{constants::*, fixture::*}; - -#[test] -fn two_slots_recover_either_side_of_configuration_commit() { - let mut fixture = Fixture::new(true); - let before = fixture.configuration.current().unwrap(); - let mut bindings = fixture.open(true).unwrap(); - bindings - .establish(&before, ALLOCATED.parse().unwrap()) - .unwrap(); - let change = fixture.change(); - let planned = fixture.configuration.planned(&change).unwrap(); - bindings - .prepare(&before, &planned, Some(ALLOCATED.parse().unwrap())) - .unwrap(); - let prepared = std::fs::read(fixture.path()).unwrap(); - drop(bindings); - let bindings = fixture.open(false).unwrap(); - assert_eq!( - bindings.address(&before).unwrap(), - Some(ALLOCATED.parse().unwrap()) - ); - drop(bindings); - assert_eq!( - fixture.configuration.apply(change.clone()).unwrap(), - planned - ); - let bindings = fixture.open(false).unwrap(); - assert_eq!( - bindings.address(&planned).unwrap(), - Some(ALLOCATED.parse().unwrap()) - ); - assert_eq!(std::fs::read(fixture.path()).unwrap(), prepared); - assert_eq!(fixture.configuration.apply(change).unwrap(), planned); -} - -#[test] -fn explicit_disabled_and_nonzero_configurations_cannot_reactivate_stale_allocations() { - for requested in [None, Some(NEXT_ALLOCATED.parse().unwrap())] { - let mut fixture = Fixture::new(true); - let before = fixture.configuration.current().unwrap(); - let mut bindings = fixture.open(true).unwrap(); - bindings - .establish(&before, ALLOCATED.parse().unwrap()) - .unwrap(); - let mut change = fixture.change(); - change.configuration.listen = requested; - let explicit = fixture.configuration.planned(&change).unwrap(); - bindings.prepare(&before, &explicit, requested).unwrap(); - fixture.configuration.apply(change).unwrap(); - drop(bindings); - let mut bindings = fixture.open(false).unwrap(); - assert_eq!(bindings.address(&explicit).unwrap(), None); - let mut change = fixture.change(); - change.configuration.listen = Some(LOOPBACK.parse().unwrap()); - let next = fixture.configuration.planned(&change).unwrap(); - assert!(bindings.prepare(&explicit, &next, None).is_err()); - bindings - .prepare(&explicit, &next, Some(NEXT_ALLOCATED.parse().unwrap())) - .unwrap(); - fixture.configuration.apply(change).unwrap(); - drop(bindings); - let bindings = fixture.open(false).unwrap(); - assert_eq!( - bindings.address(&next).unwrap(), - Some(NEXT_ALLOCATED.parse().unwrap()) - ); - assert!(bindings.address(&before).is_err()); - } -} diff --git a/core/crates/hub/listener/binding/src/constants.rs b/core/crates/hub/listener/binding/src/constants.rs deleted file mode 100644 index a71ad215..00000000 --- a/core/crates/hub/listener/binding/src/constants.rs +++ /dev/null @@ -1,12 +0,0 @@ -pub const LISTENER_BINDING_FILE: &str = "listener-binding.json"; -pub(crate) const LOCK_FILE: &str = "listener-binding.lock"; -pub(crate) const LOCK_BYTES: u64 = 4_096; -pub(crate) const SCHEMA: u16 = 2; -pub(crate) const EPHEMERAL_PORT: u16 = 0; -pub(crate) const NAMESPACE: &str = "Hub listener binding"; -pub(crate) const BUSY: &str = "Hub listener binding is already owned"; -pub(crate) const LOST: &str = "Hub listener binding authority was lost"; -pub(crate) const CHANGED: &str = "Hub listener binding identity changed"; -pub(crate) const INVALID: &str = "Hub listener binding is inconsistent"; -pub(crate) const MISSING: &str = "existing ephemeral Hub listener has no durable binding"; -pub(crate) const CONFLICT: &str = "Hub listener binding does not match the exact configuration"; diff --git a/core/crates/hub/listener/binding/src/lib.rs b/core/crates/hub/listener/binding/src/lib.rs deleted file mode 100644 index 4678a058..00000000 --- a/core/crates/hub/listener/binding/src/lib.rs +++ /dev/null @@ -1,122 +0,0 @@ -mod constants; -mod model; -mod update; - -use anyhow::{Context, Result, ensure}; -pub use constants::LISTENER_BINDING_FILE; -use constants::*; -pub use model::persistent; -use model::{Entry, Record}; -use std::{ - net::SocketAddr, - path::PathBuf, - sync::atomic::{AtomicBool, Ordering}, -}; -use yolo_hub_delegation::PeerIdentity; -use yolo_hub_management_protocol::ConfigurationState; -use yolo_paths::{ - coordination::CoordinationLock, - directory::PrivateDirectory, - identity::{DurableObjectIdentity, FileIdentity}, - namespace_lease::NamespaceLease, -}; - -/// Two durable slots bridge configuration CAS without changing the Configure document. -pub struct ListenerBindings { - namespace: NamespaceLease, - path: PathBuf, - root: DurableObjectIdentity, - peer: PeerIdentity, - lock: CoordinationLock, - file: FileIdentity, - lost: AtomicBool, -} - -#[cfg(test)] -#[path = "../unit_tests/lib.rs"] -mod unit_tests; - -impl ListenerBindings { - pub fn open( - directory: PrivateDirectory, - peer: PeerIdentity, - configuration: &ConfigurationState, - initializing: bool, - ) -> Result { - configuration.validate()?; - ensure!(configuration.identity == peer, INVALID); - let root = directory.durable_identity()?; - let path = directory.path().join(LISTENER_BINDING_FILE); - let lock = CoordinationLock::try_acquire(&directory.path().join(LOCK_FILE), LOCK_BYTES)? - .context(BUSY)?; - let namespace = NamespaceLease::from_directories(vec![(NAMESPACE, directory)])?; - let file = if namespace.file_is_missing(&path)? { - ensure!( - initializing || !persistent(configuration.configuration.listen), - MISSING - ); - let record = Record { - schema: SCHEMA, - root, - peer, - current: Entry { - configuration: configuration.clone(), - address: None, - }, - pending: None, - }; - record.validate(root, peer)?; - namespace - .write_json_expected_with_commit_fence(&path, None, &record, || lock.verify())? - } else { - let (record, file): (Record, FileIdentity) = - namespace.read_json_with_identity(&path)?; - record.validate(root, peer)?; - record.selected(configuration)?; - file - }; - let bindings = Self { - namespace, - path, - root, - peer, - lock, - file, - lost: AtomicBool::new(false), - }; - bindings.address(configuration)?; - Ok(bindings) - } - - /// None for an unassigned first listener, or for an explicitly configured endpoint. - pub fn address(&self, configuration: &ConfigurationState) -> Result> { - self.retain((|| Ok(self.read()?.selected(configuration)?.address))()) - } - - pub fn verify(&self, configuration: &ConfigurationState) -> Result<()> { - self.retain((|| self.read()?.selected(configuration)?.ready())()) - } - - fn read(&self) -> Result { - self.authority()?; - let (record, file): (Record, FileIdentity) = - self.namespace.read_json_with_identity(&self.path)?; - ensure!(file == self.file, CHANGED); - record.validate(self.root, self.peer)?; - self.authority()?; - Ok(record) - } - - fn authority(&self) -> Result<()> { - ensure!(!self.lost.load(Ordering::Acquire), LOST); - self.lock.verify()?; - self.namespace.verify() - } - - fn retain(&self, result: Result) -> Result { - if result.is_err() { - self.lost.store(true, Ordering::Release); - } - result - } -} diff --git a/core/crates/hub/listener/binding/src/model.rs b/core/crates/hub/listener/binding/src/model.rs deleted file mode 100644 index f15e9b66..00000000 --- a/core/crates/hub/listener/binding/src/model.rs +++ /dev/null @@ -1,93 +0,0 @@ -use crate::constants::*; -use anyhow::{Result, ensure}; -use serde::{Deserialize, Serialize}; -use std::net::SocketAddr; -use yolo_hub_delegation::PeerIdentity; -use yolo_hub_management_protocol::{CONFIGURATION_REVISION_STEP, ConfigurationState}; -use yolo_paths::identity::DurableObjectIdentity; - -pub fn persistent(address: Option) -> bool { - address.is_some_and(|address| address.ip().is_loopback() && address.port() == EPHEMERAL_PORT) -} - -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub(crate) struct Entry { - pub configuration: ConfigurationState, - #[serde(deserialize_with = "required_option")] - pub address: Option, -} - -impl Entry { - pub fn validate(&self, peer: PeerIdentity) -> Result<()> { - self.configuration.validate()?; - ensure!(self.configuration.identity == peer, INVALID); - if let Some(address) = self.address { - let requested = self.configuration.configuration.listen; - ensure!( - persistent(requested) - && requested.is_some_and(|requested| requested.ip() == address.ip()) - && address.port() != EPHEMERAL_PORT, - INVALID - ); - } - Ok(()) - } - - pub fn ready(&self) -> Result<()> { - ensure!( - !persistent(self.configuration.configuration.listen) || self.address.is_some(), - INVALID - ); - Ok(()) - } -} - -#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] -#[serde(deny_unknown_fields)] -pub(crate) struct Record { - pub schema: u16, - pub root: DurableObjectIdentity, - pub peer: PeerIdentity, - pub current: Entry, - #[serde(deserialize_with = "required_option")] - pub pending: Option, -} - -fn required_option<'de, D: serde::Deserializer<'de>, T: Deserialize<'de>>( - deserializer: D, -) -> std::result::Result, D::Error> { - Deserialize::deserialize(deserializer) -} - -impl Record { - pub fn validate(&self, root: DurableObjectIdentity, peer: PeerIdentity) -> Result<()> { - ensure!( - self.schema == SCHEMA && self.root == root && self.peer == peer, - INVALID - ); - self.current.validate(peer)?; - if let Some(pending) = &self.pending { - self.current.ready()?; - pending.validate(peer)?; - pending.ready()?; - ensure!( - self.current - .configuration - .revision - .checked_add(CONFIGURATION_REVISION_STEP) - == Some(pending.configuration.revision) - && self.current.configuration.last_change != pending.configuration.last_change, - INVALID - ); - } - Ok(()) - } - - pub fn selected(&self, configuration: &ConfigurationState) -> Result<&Entry> { - std::iter::once(&self.current) - .chain(self.pending.iter()) - .find(|entry| entry.configuration == *configuration) - .ok_or_else(|| anyhow::anyhow!(CONFLICT)) - } -} diff --git a/core/crates/hub/listener/binding/src/update.rs b/core/crates/hub/listener/binding/src/update.rs deleted file mode 100644 index f989ac5a..00000000 --- a/core/crates/hub/listener/binding/src/update.rs +++ /dev/null @@ -1,66 +0,0 @@ -use crate::{ - ListenerBindings, - constants::*, - model::{Entry, Record}, - persistent, -}; -use anyhow::{Result, ensure}; -use std::net::SocketAddr; -use yolo_hub_management_protocol::ConfigurationState; - -impl ListenerBindings { - /// Publish the first allocated endpoint while its real listener FD remains held. - pub fn establish( - &mut self, - configuration: &ConfigurationState, - address: SocketAddr, - ) -> Result<()> { - let mut record = self.retain(self.read())?; - ensure!( - persistent(configuration.configuration.listen) - && record.current.configuration == *configuration - && record.current.address.is_none() - && record.pending.is_none(), - CONFLICT - ); - record.current.address = Some(address); - self.write(&record) - } - - /// Keep the old binding usable until the exact configuration CAS commits. - pub fn prepare( - &mut self, - current: &ConfigurationState, - next: &ConfigurationState, - listener_address: Option, - ) -> Result<()> { - let record = self.retain(self.read())?; - let current = record.selected(current)?.clone(); - current.ready()?; - let pending = Entry { - configuration: next.clone(), - address: persistent(next.configuration.listen) - .then_some(listener_address) - .flatten(), - }; - let record = Record { - current, - pending: Some(pending), - ..record - }; - self.write(&record) - } - - fn write(&mut self, record: &Record) -> Result<()> { - record.validate(self.root, self.peer)?; - let result = self.namespace.write_json_expected_with_commit_fence( - &self.path, - Some(self.file), - record, - || self.read().map(|_| ()), - ); - self.file = self.retain(result)?; - self.retain(self.read())?; - Ok(()) - } -} diff --git a/core/crates/hub/listener/binding/test_support/constants.rs b/core/crates/hub/listener/binding/test_support/constants.rs deleted file mode 100644 index 331962e8..00000000 --- a/core/crates/hub/listener/binding/test_support/constants.rs +++ /dev/null @@ -1,18 +0,0 @@ -pub const HOST: &str = "host"; -pub const HUB: &str = "hub"; -pub const SAVED: &str = "saved-binding.json"; -pub const REPLACEMENT: &str = "replacement-binding.json"; -pub const LOOPBACK: &str = "127.0.0.1:0"; -pub const ALLOCATED: &str = "127.0.0.1:32145"; -pub const NEXT_ALLOCATED: &str = "127.0.0.1:32146"; -pub const WRONG_IP: &str = "127.0.0.2:32145"; -pub const PRIVATE_MODE: u32 = 0o600; -pub const DIRECTORY_MODE: u32 = 0o700; -pub const KEY_BYTE: u8 = 1; -pub const FOREIGN_KEY_BYTE: u8 = 2; -pub const WILDCARD: &str = "0.0.0.0:0"; -pub const CURRENT_FIELD: &str = "current"; -pub const CONFIGURATION_FIELD: &str = "configuration"; -pub const CHANGE_FIELD: &str = "last_change"; -pub const ADDRESS_FIELD: &str = "address"; -pub const UNKNOWN_FIELD: &str = "unknown"; diff --git a/core/crates/hub/listener/binding/test_support/fixture.rs b/core/crates/hub/listener/binding/test_support/fixture.rs deleted file mode 100644 index 29f55b40..00000000 --- a/core/crates/hub/listener/binding/test_support/fixture.rs +++ /dev/null @@ -1,65 +0,0 @@ -use super::constants::*; -use std::path::PathBuf; -use yolo_hub_configuration_store::{CONFIGURATION_DIRECTORY, ConfigurationStore}; -use yolo_hub_federation_settings::Configuration; -use yolo_hub_listener_binding::ListenerBindings; -use yolo_hub_management_protocol::{ConfigurationChange, ConfigurationState}; -use yolo_hub_state::HubState; - -pub struct Fixture { - pub directory: tempfile::TempDir, - pub hub: HubState, - pub configuration: ConfigurationStore, -} -impl Fixture { - pub fn new(listen: bool) -> Self { - let directory = tempfile::tempdir().unwrap(); - let hub = - HubState::open(&directory.path().join(HUB), &directory.path().join(HOST)).unwrap(); - let root = hub.directory().unwrap(); - let configuration = ConfigurationStore::open( - root.ensure_descendant(&root.path().join(CONFIGURATION_DIRECTORY)) - .unwrap(), - hub.peer_identity(), - Some(Configuration { - listen: listen.then(|| LOOPBACK.parse().unwrap()), - ..Configuration::default() - }), - ) - .unwrap(); - Self { - directory, - hub, - configuration, - } - } - - pub fn open(&self, fresh: bool) -> anyhow::Result { - ListenerBindings::open( - self.hub.directory()?, - self.hub.peer_identity(), - &self.configuration.current()?, - fresh, - ) - } - - pub fn path(&self) -> PathBuf { - self.hub - .directory() - .unwrap() - .path() - .join(yolo_hub_listener_binding::LISTENER_BINDING_FILE) - } - - pub fn change(&self) -> ConfigurationChange { - change(&self.configuration.current().unwrap()) - } -} - -pub fn change(current: &ConfigurationState) -> ConfigurationChange { - ConfigurationChange { - request_id: uuid::Uuid::new_v4(), - expected_revision: current.revision, - configuration: current.configuration.clone(), - } -} diff --git a/core/crates/hub/listener/binding/unit_tests/lib.rs b/core/crates/hub/listener/binding/unit_tests/lib.rs deleted file mode 100644 index 5c793727..00000000 --- a/core/crates/hub/listener/binding/unit_tests/lib.rs +++ /dev/null @@ -1,79 +0,0 @@ -use crate::model::{Entry, persistent}; -#[path = "../test_support/constants.rs"] -mod constants; -use constants::*; -use yolo_hub_delegation::{ED25519_PUBLIC_KEY_BYTES, PeerIdentity}; -use yolo_hub_identity::HubId; -use yolo_hub_management_protocol::{ - CONFIGURATION_SCHEMA, ConfigurationState, FIRST_CONFIGURATION_REVISION, -}; - -fn entry() -> Entry { - let identity = PeerIdentity::new(HubId::new(), [KEY_BYTE; ED25519_PUBLIC_KEY_BYTES]).unwrap(); - Entry { - configuration: ConfigurationState { - schema_version: CONFIGURATION_SCHEMA, - identity, - revision: FIRST_CONFIGURATION_REVISION, - last_change: uuid::Uuid::new_v4(), - configuration: yolo_hub_federation_settings::Configuration { - listen: Some(LOOPBACK.parse().unwrap()), - ..Default::default() - }, - }, - address: Some(ALLOCATED.parse().unwrap()), - } -} - -#[test] -fn endpoint_must_match_the_exact_requested_loopback_and_have_a_real_port() { - let value = entry(); - let peer = value.configuration.identity; - value.validate(peer).unwrap(); - value.ready().unwrap(); - for invalid in [LOOPBACK, WRONG_IP] { - let mut changed = value.clone(); - changed.address = Some(invalid.parse().unwrap()); - assert!(changed.validate(peer).is_err()); - } - let mut unassigned = value.clone(); - unassigned.address = None; - unassigned.validate(peer).unwrap(); - assert!(unassigned.ready().is_err()); - let foreign = - PeerIdentity::new(peer.hub(), [FOREIGN_KEY_BYTE; ED25519_PUBLIC_KEY_BYTES]).unwrap(); - assert!(value.validate(foreign).is_err()); -} - -#[test] -fn explicit_and_nonloopback_endpoints_never_use_an_ephemeral_binding() { - for requested in [ - None, - Some(ALLOCATED.parse().unwrap()), - Some(WILDCARD.parse().unwrap()), - ] { - assert!(!persistent(requested)); - let mut value = entry(); - value.configuration.configuration.listen = requested; - assert!(value.validate(value.configuration.identity).is_err()); - value.address = None; - value.validate(value.configuration.identity).unwrap(); - value.ready().unwrap(); - } -} - -#[test] -fn wire_requires_explicit_address_and_rejects_unknown_fields() { - let value = entry(); - let encoded = serde_json::to_value(&value).unwrap(); - assert_eq!( - serde_json::from_value::(encoded.clone()).unwrap(), - value - ); - let mut missing = encoded.clone(); - missing.as_object_mut().unwrap().remove(ADDRESS_FIELD); - assert!(serde_json::from_value::(missing).is_err()); - let mut unknown = encoded; - unknown[UNKNOWN_FIELD] = true.into(); - assert!(serde_json::from_value::(unknown).is_err()); -} diff --git a/core/crates/hub/local/backend/src/agents.rs b/core/crates/hub/local/backend/src/agents.rs index a77e7768..0a3d850b 100644 --- a/core/crates/hub/local/backend/src/agents.rs +++ b/core/crates/hub/local/backend/src/agents.rs @@ -21,6 +21,7 @@ impl LocalBackend { &self, request: &yolo_hub_agent_protocol::AgentStartRequest, ) -> Result { + self.refresh_runs().await?; yolo_hub_local_platform::agent_start( self.hub, &self.client, @@ -73,6 +74,7 @@ impl LocalBackend { &self, request: &GetPlatformSnapshotRequest, ) -> Result { + self.refresh_runs().await?; let terminals = self.terminals(false).await?; let agents = match self.observe_agents().await { Ok(agents) => agents, @@ -97,10 +99,10 @@ impl LocalBackend { let sweep = self .observe_catalogue(ObservationScope::Complete, false) .await; - let agents = self + let applied = self .apply_catalogue(sweep, true, SweepPolicy::ObserveAgents(None)) - .await? - .agents; + .await?; + let agents = applied.agents; self.adopt_observed(&agents).await; Ok(agents) } @@ -176,7 +178,11 @@ impl LocalBackend { .collect(); let mut resolved = Vec::new(); for terminal in terminals { - resolved.push(*current.get(&terminal.terminal_id).ok_or(PublicError::NotFound)?); + resolved.push( + *current + .get(&terminal.terminal_id) + .ok_or(PublicError::NotFound)?, + ); } request.targets.extend(resolved); request.targets.sort(); diff --git a/core/crates/hub/local/backend/src/backend.rs b/core/crates/hub/local/backend/src/backend.rs index aace239b..0f79f37c 100644 --- a/core/crates/hub/local/backend/src/backend.rs +++ b/core/crates/hub/local/backend/src/backend.rs @@ -191,6 +191,7 @@ impl LocalBackend { Command::CleanupArchive { creation } => self.cleanup_archive(*creation).await, Command::ListFileRoots { after } => self.list_file_roots(*after), Command::ReadFile { request } => self.read_file(request.clone()).await, + Command::ListConsumers {} => Err(PublicError::Unsupported), } } } diff --git a/core/crates/hub/local/backend/src/inventory.rs b/core/crates/hub/local/backend/src/inventory.rs index 57e268fd..69fa89fa 100644 --- a/core/crates/hub/local/backend/src/inventory.rs +++ b/core/crates/hub/local/backend/src/inventory.rs @@ -29,11 +29,19 @@ impl LocalBackend { &self, command: &Command, ) -> Option> { - let reply = self.platform.as_ref()?.dispatch(command).await?; - Some(match reply { - Ok(reply) => self.annotate_run_reply(reply).await, - Err(error) => Err(error), - }) + let platform = self.platform.as_ref()?; + if matches!( + command, + Command::GetAgentRun { .. } | Command::ListAgentRuns { .. } + ) && let Err(error) = self.refresh_runs().await + { + return Some(Err(error)); + } + let mut reply = platform.dispatch(command).await?; + if let Ok(reply @ (Reply::AgentRun { .. } | Reply::AgentRuns { .. })) = &mut reply { + reply.annotate_run_current(&self.observed_targets().await); + } + Some(reply) } pub(crate) async fn catalogue_views( @@ -81,6 +89,7 @@ impl LocalBackend { let applied = self .apply_catalogue(sweep, false, SweepPolicy::Catalogue) .await?; + self.reconcile_runs(&applied.views, archived).await?; let views = match applied.views { Ok(views) => views, Err(error) => return Err(map_error(error)), diff --git a/core/crates/hub/local/backend/src/locked_observation.rs b/core/crates/hub/local/backend/src/locked_observation.rs index 5c0ba3e8..da4151b6 100644 --- a/core/crates/hub/local/backend/src/locked_observation.rs +++ b/core/crates/hub/local/backend/src/locked_observation.rs @@ -3,113 +3,64 @@ use crate::{ connector_catalogue::ObservationScope, native_diagnostics::{self, ObservationContext, ObservationStage}, }; -use std::collections::BTreeSet; use yolo_hub_agent_projection::AgentProjection; use yolo_hub_agent_protocol::AgentSnapshot; -use yolo_hub_identity::{TerminalAddress, TerminalId}; use yolo_hub_terminal_protocol::PublicError; -use yolo_terminal_model::TerminalView; -/// The native control path admits its effect against one projection state, so -/// it keeps the projection lock across this read. Catalogue reads do not use -/// this sweep: they gather Host and Worker evidence before taking the lock. impl LocalBackend { + /// Validate a native effect against one terminal while retaining the + /// caller's projection lock. pub(crate) async fn observe_agents_locked( &self, - mut projection: Option<&mut AgentProjection>, - diagnostic: Option>, - scope: Option, + projection: &mut AgentProjection, + diagnostic: ObservationContext<'_>, ) -> Result, PublicError> { let _observation = self.connector_observation().await; - // Archived sessions are never observed. A scoped get/control resolves - // only its target view; a catalogue sweep reads the live catalogue. - let observed: Result, yolo_client::ClientError> = match scope { - Some(terminal) => self - .client - .get_session(terminal.as_uuid()) - .await - .map(|view| vec![view]), - None => self.catalogue_views(false).await, - }; - if let Some(projection) = projection.as_deref_mut() { - match &observed { - Ok(views) => { - let mut observed = BTreeSet::new(); - for view in views { - let id = TerminalId::from_uuid(view.manifest.terminal_id) - .map_err(|_| PublicError::Internal)?; - observed.insert(id); - // An unchanged idle binding keeps its stored witness: - // its readiness is the binding state itself. - let native = - match projection.reusable_idle_observation(view).ok().flatten() { - Some(observation) => Ok(observation), - None => self.classify_native_observation(view).await, - }; - if view.agent.is_some() - && let Err(stage) = &native - { - native_diagnostics::catalogue_unavailable( - TerminalAddress { - hub_id: self.hub, - terminal_id: id, - }, - *stage, - ); - } - if let Some(context) = diagnostic.filter(|context| { - context.terminal.address.terminal_id.as_uuid() - == view.manifest.terminal_id - }) { - let stage = native - .as_ref() - .err() - .copied() - .unwrap_or(ObservationStage::Coherent); - native_diagnostics::observation(self.hub, context, stage); - } - let native = native.ok(); - projection - .project_with_observation(view, native.as_ref()) - .map_err(|_| PublicError::Internal)?; - } - if scope.is_none() { - projection - .mark_missing_sessions(&observed) - .map_err(|_| PublicError::Internal)?; - } + let address = diagnostic.terminal.address; + let terminal = address.terminal_id; + let observed = self.client.get_session(terminal.as_uuid()).await; + match &observed { + Ok(view) => { + if view.manifest.terminal_id != terminal.as_uuid() { + return Err(PublicError::TargetMismatch); } - Err(_) => { - if let Some(context) = diagnostic { - native_diagnostics::observation( - self.hub, - context, - ObservationStage::HostCatalogueUnavailable, - ); - } - let marked = match scope { - Some(terminal) => projection.mark_session_unreachable(terminal), - None => projection.mark_unreachable(), - }; - marked.map_err(|_| PublicError::Internal)?; + let native = match projection.reusable_idle_observation(view).ok().flatten() { + Some(observation) => Ok(observation), + None => self.classify_native_observation(view).await, + }; + if view.agent.is_some() + && let Err(stage) = &native + { + native_diagnostics::catalogue_unavailable(address, *stage); } + let stage = native + .as_ref() + .err() + .copied() + .unwrap_or(ObservationStage::Coherent); + native_diagnostics::observation(self.hub, diagnostic, stage); + projection + .project_with_observation(view, native.as_ref().ok()) + .map_err(|_| PublicError::Internal)?; + } + Err(_) => { + native_diagnostics::observation( + self.hub, + diagnostic, + ObservationStage::HostCatalogueUnavailable, + ); + projection + .mark_session_unreachable(terminal) + .map_err(|_| PublicError::Internal)?; } } - let mut agents = match projection.as_deref() { - Some(projection) => projection.list().map_err(|_| PublicError::Internal)?, - None => Vec::new(), - }; - let scope = match scope { - Some(terminal) => ObservationScope::Terminal(TerminalAddress { - hub_id: self.hub, - terminal_id: terminal, - }), - None => ObservationScope::Complete, - }; - let connectors = self - .connector_catalogue(observed.as_deref().ok(), scope) - .await; - agents.extend(connectors.agents); + let mut agents = projection.list().map_err(|_| PublicError::Internal)?; + let views = observed.as_ref().ok().map(std::slice::from_ref); + agents.extend( + self.connector_catalogue(views, ObservationScope::Terminal(address)) + .await + .agents, + ); Ok(agents) } } diff --git a/core/crates/hub/local/backend/src/native_control.rs b/core/crates/hub/local/backend/src/native_control.rs index 1af46f4b..29cd8cdc 100644 --- a/core/crates/hub/local/backend/src/native_control.rs +++ b/core/crates/hub/local/backend/src/native_control.rs @@ -30,12 +30,11 @@ impl LocalBackend { } let snapshots = self .observe_agents_locked( - Some(&mut *projection), - Some(ObservationContext { + &mut *projection, + ObservationContext { request, terminal: grant.fields().target, - }), - Some(grant.fields().target.address.terminal_id), + }, ) .await; // A lost call may arrive while refresh is awaited; inspect the receipt again. diff --git a/core/crates/hub/local/backend/src/observation.rs b/core/crates/hub/local/backend/src/observation.rs index 4e280ffe..865957eb 100644 --- a/core/crates/hub/local/backend/src/observation.rs +++ b/core/crates/hub/local/backend/src/observation.rs @@ -1,7 +1,5 @@ use crate::{ - LocalBackend, - connector_catalogue::ObservationScope, - native_diagnostics::ObservationStage, + LocalBackend, connector_catalogue::ObservationScope, native_diagnostics::ObservationStage, }; use futures::StreamExt; use std::future::Future; @@ -11,7 +9,6 @@ use yolo_agent_control_contract::NativeControlObservation; use yolo_hub_agent_protocol::AgentSnapshot; use yolo_hub_connector_registry::Catalogue; use yolo_hub_identity::TerminalId; -use yolo_hub_rpc_protocol::Reply; use yolo_terminal_model::TerminalView; /// A Worker that cannot answer within this budget contributes no capability: @@ -50,26 +47,25 @@ pub(crate) struct AppliedCatalogue { } impl LocalBackend { - /// `current` is advisory; an unavailable projection degrades to no - /// annotation instead of failing the durable run read. - pub(crate) async fn annotate_run_reply(&self, mut reply: Reply) -> Result { - if matches!(&reply, Reply::AgentRun { .. } | Reply::AgentRuns { .. }) { - reply.annotate_run_current(&self.observed_targets().await); - } - Ok(reply) - } - /// Live terminal-to-execution joins from the projection and connectors. - pub(crate) async fn observed_targets(&self) -> Vec<(TerminalId, yolo_hub_identity::AgentTarget)> { + pub(crate) async fn observed_targets( + &self, + ) -> Vec<(TerminalId, yolo_hub_identity::AgentTarget)> { let mut targets = match &self.agents { Some(projection) => projection.lock().await.active_targets().unwrap_or_default(), None => Vec::new(), }; - let native: std::collections::BTreeSet<_> = targets.iter().map(|(terminal, _)| *terminal).collect(); + let native: std::collections::BTreeSet<_> = + targets.iter().map(|(terminal, _)| *terminal).collect(); if let Some(registry) = &self.connectors && let Ok(catalogue) = registry.lock().await.catalogue() { - targets.extend(catalogue.active_targets().into_iter().filter(|(terminal, _)| !native.contains(terminal))); + targets.extend( + catalogue + .active_targets() + .into_iter() + .filter(|(terminal, _)| !native.contains(terminal)), + ); } targets } @@ -115,9 +111,7 @@ impl LocalBackend { .await .map(|view| vec![view]), }; - let connectors = self - .connector_catalogue(views.as_deref().ok(), scope) - .await; + let connectors = self.connector_catalogue(views.as_deref().ok(), scope).await; (views, connectors) }; let native = match &views { @@ -151,8 +145,8 @@ impl LocalBackend { .map(|view| projection.reusable_idle_observation(view).ok().flatten()) .collect() }; - let deadline = (tokio::time::Instant::now() + NATIVE_OBSERVATION_TIMEOUT) - .min(sweep_deadline); + let deadline = + (tokio::time::Instant::now() + NATIVE_OBSERVATION_TIMEOUT).min(sweep_deadline); let captures = views .iter() .cloned() diff --git a/core/crates/hub/local/backend/src/platform_catalogue.rs b/core/crates/hub/local/backend/src/platform_catalogue.rs index 9f55bc9b..79f849a6 100644 --- a/core/crates/hub/local/backend/src/platform_catalogue.rs +++ b/core/crates/hub/local/backend/src/platform_catalogue.rs @@ -7,10 +7,33 @@ use yolo_hub_terminal_protocol::PublicError; use yolo_hub_workspace_protocol::{ListWorkspacesRequest, ListWorkspacesResult}; impl LocalBackend { + pub(crate) async fn refresh_runs(&self) -> Result<(), PublicError> { + if self.platform.is_none() { + return Ok(()); + } + let _sweep = self.begin_observation().await; + let views = self.bounded_host_read(self.catalogue_views(false)).await; + self.reconcile_runs(&views, false).await + } + + pub(crate) async fn reconcile_runs( + &self, + views: &Result, yolo_client::ClientError>, + includes_archived: bool, + ) -> Result<(), PublicError> { + if let (Some(platform), Ok(views)) = (self.platform.as_deref(), views) { + platform + .observe_run_terminals(self.hub, views, includes_archived) + .await?; + } + Ok(()) + } + pub async fn platform_runs( &self, request: &ListAgentRunsRequest, ) -> Result { + self.refresh_runs().await?; let mut result = match &self.platform { Some(platform) => platform.runs().list(request).await?, None => ListAgentRunsResult { diff --git a/core/crates/hub/local/platform/BUILD.bazel b/core/crates/hub/local/platform/BUILD.bazel index c870e2ad..37d1ff60 100644 --- a/core/crates/hub/local/platform/BUILD.bazel +++ b/core/crates/hub/local/platform/BUILD.bazel @@ -22,6 +22,7 @@ _DEPS = [ "//core/crates/terminal/model:yolo_terminal_model", "//core/crates/transport/client:yolo_client", "//core/crates/wire:yolo_wire", + "@crates//:futures", "@crates//:tokio", "@crates//:uuid", ] diff --git a/core/crates/hub/local/platform/src/agent_run.rs b/core/crates/hub/local/platform/src/agent_run.rs index 3542e440..3f737a23 100644 --- a/core/crates/hub/local/platform/src/agent_run.rs +++ b/core/crates/hub/local/platform/src/agent_run.rs @@ -8,6 +8,10 @@ use yolo_hub_identity::{HubId, TerminalAddress, WorkspaceAddress}; use yolo_hub_terminal_protocol::PublicError; use yolo_paths::directory::PrivateDirectory; +#[path = "agent_run_observation.rs"] +mod observation; +pub(crate) use observation::{run_name, terminal_observation}; + /// Hub-owned durable run identity. The store owns naming and revisions; the /// terminal and provider binding remain observed Host/Worker facts. pub struct AgentRunService { @@ -104,7 +108,10 @@ impl AgentRunService { .map_err(StoreError::into_public) } - pub(crate) async fn get(&self, request: &GetAgentRunRequest) -> Result { + pub(crate) async fn get( + &self, + request: &GetAgentRunRequest, + ) -> Result { self.store .lock() .await @@ -125,14 +132,22 @@ impl AgentRunService { .ok_or(PublicError::NotFound) } - pub async fn mark_stopped( + pub(crate) async fn mark_stopped_if_current( &self, address: AgentRunAddress, + terminal: TerminalAddress, + expected_runtime: Option, + expected_observed_runtime: Option>, ) -> Result { self.store .lock() .await - .mark_stopped(address) + .mark_stopped_if_current( + address, + Some(terminal), + expected_runtime, + expected_observed_runtime, + ) .map_err(StoreError::into_public) } @@ -179,15 +194,3 @@ impl AgentRunService { .map_err(StoreError::into_public) } } - -pub fn run_name(provider_id: &str, start_token: uuid::Uuid) -> String { - let suffix = &start_token.simple().to_string()[..8]; - format!("{provider_id}-{suffix}") -} - -pub fn selector_address(selector: &AgentRunSelector) -> Option { - match selector { - AgentRunSelector::Address(address) => Some(*address), - AgentRunSelector::Name(_) => None, - } -} diff --git a/core/crates/hub/local/platform/src/agent_run_observation.rs b/core/crates/hub/local/platform/src/agent_run_observation.rs new file mode 100644 index 00000000..62e96908 --- /dev/null +++ b/core/crates/hub/local/platform/src/agent_run_observation.rs @@ -0,0 +1,137 @@ +use super::{AgentRunService, StoreError}; +use futures::StreamExt; +use std::collections::{BTreeMap, BTreeSet}; +use std::time::Duration; +use yolo_client::Client; +use yolo_hub_agent_run_protocol::{AgentRunSelector, AgentRunSummary}; +use yolo_hub_agent_run_store::TerminalObservation; +use yolo_hub_identity::{HubId, TerminalAddress, TerminalId}; +use yolo_hub_terminal_protocol::PublicError; +use yolo_terminal_model::{TerminalRuntimeStatus, TerminalView}; + +const ARCHIVE_LOOKUP_CONCURRENCY: usize = 16; +const ARCHIVE_LOOKUP_BUDGET: Duration = Duration::from_millis(500); + +pub(crate) fn run_name(provider_id: &str, start_token: uuid::Uuid) -> String { + let suffix = &start_token.simple().to_string()[..8]; + format!("{provider_id}-{suffix}") +} + +impl AgentRunService { + pub(crate) async fn stop_target( + &self, + selector: &AgentRunSelector, + ) -> Result<(AgentRunSummary, Option, Option), PublicError> { + self.store + .lock() + .await + .stop_target(selector) + .map_err(StoreError::into_public)? + .ok_or(PublicError::NotFound) + } + + pub(crate) async fn observe_catalogue( + &self, + client: &Client, + hub: HubId, + views: &[TerminalView], + includes_archived: bool, + ) -> Result<(), PublicError> { + let mut observations = BTreeMap::new(); + let mut present = BTreeSet::new(); + for view in views { + let terminal_id = TerminalId::from_uuid(view.manifest.terminal_id) + .map_err(|_| PublicError::Internal)?; + let address = TerminalAddress { + hub_id: hub, + terminal_id, + }; + present.insert(address); + if let Some(observation) = terminal_observation(view) { + observations.insert(address, observation); + } + } + if !includes_archived { + // The live catalogue omits archived terminals. Look up only + // missing launched runs, within one budget for the entire batch. + let launched = self + .store + .lock() + .await + .launched_terminals() + .map_err(StoreError::into_public)?; + let deadline = tokio::time::Instant::now() + ARCHIVE_LOOKUP_BUDGET; + let lookups = launched + .difference(&present) + .copied() + .map(|address| async move { + let view = tokio::time::timeout_at( + deadline, + client.get_session(address.terminal_id.as_uuid()), + ) + .await + .ok()? + .ok()?; + if view.manifest.terminal_id != address.terminal_id.as_uuid() { + return None; + } + terminal_observation(&view).map(|observation| (address, observation)) + }); + let mut omitted = + futures::stream::iter(lookups).buffer_unordered(ARCHIVE_LOOKUP_CONCURRENCY); + while let Some(observation) = omitted.next().await { + observations.extend(observation); + } + } + let ended: Vec<_> = observations + .iter() + .filter_map(|(address, observation)| { + matches!(observation, TerminalObservation::Ended(_)).then_some(*address) + }) + .collect(); + let deadline = tokio::time::Instant::now() + ARCHIVE_LOOKUP_BUDGET; + let fresh = futures::stream::iter(ended.into_iter().map(|address| async move { + let view = tokio::time::timeout_at( + deadline, + client.get_session(address.terminal_id.as_uuid()), + ) + .await + .ok()? + .ok()?; + terminal_observation(&view).map(|observation| (address, observation)) + })) + .buffer_unordered(ARCHIVE_LOOKUP_CONCURRENCY) + .collect::>() + .await; + for observation in fresh.into_iter().flatten() { + observations.insert(observation.0, observation.1); + } + self.store + .lock() + .await + .observe_terminals(&observations) + .map_err(StoreError::into_public) + } +} + +pub(crate) fn terminal_observation(view: &TerminalView) -> Option { + if view.manifest.archived { + return Some(TerminalObservation::Archived); + } + let runtime = view.runtime.as_ref()?; + if runtime.runtime_epoch.is_nil() { + return None; + } + match runtime.status { + TerminalRuntimeStatus::Starting + | TerminalRuntimeStatus::Running + | TerminalRuntimeStatus::Finalizing + | TerminalRuntimeStatus::OutputBlocked => { + Some(TerminalObservation::Live(runtime.runtime_epoch)) + } + TerminalRuntimeStatus::Exited | TerminalRuntimeStatus::Lost => { + Some(TerminalObservation::Ended(runtime.runtime_epoch)) + } + TerminalRuntimeStatus::Unreachable => None, + } +} diff --git a/core/crates/hub/local/platform/src/agent_stop.rs b/core/crates/hub/local/platform/src/agent_stop.rs index ad1f5577..cd4e8685 100644 --- a/core/crates/hub/local/platform/src/agent_stop.rs +++ b/core/crates/hub/local/platform/src/agent_stop.rs @@ -1,22 +1,29 @@ //! Stop an agent run by ending its terminal. The run transitions only after //! the Host reports the terminal runtime is no longer live. -use crate::agent_run::AgentRunService; +use crate::agent_run::{AgentRunService, terminal_observation}; use crate::errors::map_error; +use std::time::Duration; +use tokio::time::{Instant, sleep, timeout_at}; use yolo_client::{Client, ClientError}; use yolo_hub_agent_protocol::{AgentStopOutcome, AgentStopRequest, AgentStopResult}; use yolo_hub_agent_run_protocol::{AgentRunState, AgentRunSummary}; +use yolo_hub_agent_run_store::TerminalObservation; use yolo_hub_rpc_protocol::Reply; use yolo_hub_terminal_protocol::PublicError; -use yolo_terminal_model::{TerminalLifecycleSelection, TerminalRuntimeStatus}; +use yolo_terminal_model::TerminalLifecycleSelection; + +const STOP_OBSERVATION_BUDGET: Duration = Duration::from_secs(2); +const STOP_OBSERVATION_INTERVAL: Duration = Duration::from_millis(50); pub async fn agent_stop( service: &AgentRunService, client: &Client, request: &AgentStopRequest, ) -> Result { - let run = service.resolve(&request.fields().selector).await?; - if run.state == AgentRunState::Stopped { + let (run, ended_runtime, observed_runtime) = + service.stop_target(&request.fields().selector).await?; + if run.state == AgentRunState::Stopped && ended_runtime.is_none() { return Ok(reply(run, AgentStopOutcome::AlreadyStopped)); } let terminal = run.terminal.ok_or(PublicError::Conflict)?; @@ -24,43 +31,114 @@ pub async fn agent_stop( let view = match client.get_session(terminal_id).await { Ok(view) => view, Err(ClientError::Remote { code, .. }) if code == yolo_wire::error_code::NOT_FOUND => { - let run = service.mark_stopped(run.address).await?; + if run.state == AgentRunState::Stopped { + return Ok(reply(run, AgentStopOutcome::AlreadyStopped)); + } + let refreshed = client + .list_sessions_complete(true) + .await + .map_err(|_| PublicError::OutcomeUnknown)?; + if refreshed + .iter() + .any(|view| view.manifest.terminal_id == terminal_id) + { + return Err(PublicError::OutcomeUnknown); + } + let run = service + .mark_stopped_if_current(run.address, terminal, None, Some(observed_runtime)) + .await?; return Ok(reply(run, AgentStopOutcome::AlreadyExited)); } Err(error) => return Err(map_error(error)), }; - let outcome = if view.runtime.as_ref().is_some_and(live) { - client - .terminate_with_preconditions( - TerminalLifecycleSelection::unconditional(terminal_id), - request.fields().force, - ) - .await - .map_err(map_error)?; - match client.get_session(terminal_id).await { - Ok(after) if after.runtime.as_ref().is_some_and(live) => { + if view.manifest.terminal_id != terminal_id { + return Err(PublicError::TargetMismatch); + } + let mut expected_runtime = None; + let outcome = match terminal_observation(&view) { + Some(TerminalObservation::Live(epoch)) => { + expected_runtime = Some(epoch); + if ended_runtime == Some(epoch) + || observed_runtime.is_some_and(|observed| observed != epoch) + { return Err(PublicError::OutcomeUnknown); } - Ok(_) => {} - Err(ClientError::Remote { code, .. }) if code == yolo_wire::error_code::NOT_FOUND => {} - Err(_) => return Err(PublicError::OutcomeUnknown), + let result = client + .terminate_with_preconditions( + TerminalLifecycleSelection::at_runtime_epoch(terminal_id, epoch), + request.fields().force, + ) + .await + .map_err(map_error); + if let Err(error) = result + && !matches!( + error, + PublicError::OutcomeUnknown | PublicError::Unreachable + ) + { + return Err(error); + } + confirm_terminated(client, terminal_id, epoch).await?; + AgentStopOutcome::Terminated } - AgentStopOutcome::Terminated - } else { - AgentStopOutcome::AlreadyExited + Some(TerminalObservation::Ended(epoch)) => { + expected_runtime = Some(epoch); + if observed_runtime.is_some_and(|observed| observed != epoch) { + return Err(PublicError::OutcomeUnknown); + } + if run.state == AgentRunState::Stopped && ended_runtime == Some(epoch) { + return Ok(reply(run, AgentStopOutcome::AlreadyStopped)); + } + AgentStopOutcome::AlreadyExited + } + Some(TerminalObservation::Archived) => { + if run.state == AgentRunState::Stopped { + return Ok(reply(run, AgentStopOutcome::AlreadyStopped)); + } + AgentStopOutcome::AlreadyExited + } + None => return Err(PublicError::Unreachable), }; - let run = service.mark_stopped(run.address).await?; + let run = service + .mark_stopped_if_current( + run.address, + terminal, + expected_runtime, + Some(observed_runtime), + ) + .await?; Ok(reply(run, outcome)) } -fn live(runtime: &yolo_terminal_model::RuntimeView) -> bool { - matches!( - runtime.status, - TerminalRuntimeStatus::Starting - | TerminalRuntimeStatus::Running - | TerminalRuntimeStatus::Finalizing - | TerminalRuntimeStatus::OutputBlocked - ) +async fn confirm_terminated( + client: &Client, + terminal: uuid::Uuid, + epoch: uuid::Uuid, +) -> Result<(), PublicError> { + let deadline = Instant::now() + STOP_OBSERVATION_BUDGET; + timeout_at(deadline, async { + loop { + if let Ok(view) = client.get_session(terminal).await { + if view.manifest.terminal_id != terminal + || view + .runtime + .as_ref() + .is_some_and(|runtime| runtime.runtime_epoch != epoch) + { + return Err(PublicError::OutcomeUnknown); + } + if matches!( + terminal_observation(&view), + Some(TerminalObservation::Ended(_) | TerminalObservation::Archived) + ) { + return Ok(()); + } + } + sleep(STOP_OBSERVATION_INTERVAL).await; + } + }) + .await + .map_err(|_| PublicError::OutcomeUnknown)? } fn reply(run: AgentRunSummary, outcome: AgentStopOutcome) -> Reply { diff --git a/core/crates/hub/local/platform/src/lib.rs b/core/crates/hub/local/platform/src/lib.rs index ec9e0ce0..bb633acc 100644 --- a/core/crates/hub/local/platform/src/lib.rs +++ b/core/crates/hub/local/platform/src/lib.rs @@ -65,6 +65,17 @@ impl PlatformServices { &self.runs } + pub async fn observe_run_terminals( + &self, + hub: yolo_hub_identity::HubId, + views: &[yolo_terminal_model::TerminalView], + includes_archived: bool, + ) -> Result<(), yolo_hub_terminal_protocol::PublicError> { + self.runs + .observe_catalogue(&self.client, hub, views, includes_archived) + .await + } + pub fn workspaces(&self) -> &WorkspaceService { &self.workspaces } diff --git a/core/crates/hub/local/startup/src/constants.rs b/core/crates/hub/local/startup/src/constants.rs index 40be8236..2373cf64 100644 --- a/core/crates/hub/local/startup/src/constants.rs +++ b/core/crates/hub/local/startup/src/constants.rs @@ -16,6 +16,14 @@ pub(crate) const HOST_SERVE_COMMAND: &str = "serve"; pub(crate) const HOME_ARGUMENT: &str = "--home"; pub(crate) const SOCKET_ARGUMENT: &str = "--socket"; pub(crate) const DEADLINE_EXPIRED: &str = "local Hub startup deadline expired"; +pub(crate) const HUB_STARTUP_STDERR: &str = "hub-startup.stderr"; +pub(crate) const MAX_STARTUP_STDERR_BYTES: usize = 4_096; +pub(crate) const EXISTING_HUB_GRACE: Duration = Duration::from_secs(2); +pub(crate) const HUB_EXITED_BEFORE_READY: &str = "local Hub exited before it became ready"; +pub(crate) const HOST_IMAGE_PREFIX: &str = "running Host image "; +pub(crate) const HOST_IMAGE_SUFFIX: &str = + "; if it belongs to a different Yolo release, run `yolo restart` to replace both services"; +pub(crate) const STARTUP_STDERR_OPEN_FAILED: &str = "open the Hub startup error log"; pub(crate) const READY_INVALID: &str = "local Hub readiness did not match its launch"; pub(crate) const READY_MISSING: &str = "local Hub readiness pipe was not available"; pub(crate) const PIN_MISMATCH: &str = "persistent Hub identity does not match its enrollment pin"; diff --git a/core/crates/hub/local/startup/src/host.rs b/core/crates/hub/local/startup/src/host.rs index fab01093..19671b7b 100644 --- a/core/crates/hub/local/startup/src/host.rs +++ b/core/crates/hub/local/startup/src/host.rs @@ -108,7 +108,7 @@ pub(crate) async fn ensure( /// capability belongs here rather than at the client entry point: an already /// running Host can be reused by a caller without delegated custody. #[cfg(target_os = "linux")] -fn require_custody() -> Result<()> { +pub(crate) fn require_custody() -> Result<()> { require_custody_available( yolo_process::linux_containment::LinuxContainmentBroker::discover()?.is_some(), ) diff --git a/core/crates/hub/local/startup/src/hub.rs b/core/crates/hub/local/startup/src/hub.rs index 95611be6..05e90c73 100644 --- a/core/crates/hub/local/startup/src/hub.rs +++ b/core/crates/hub/local/startup/src/hub.rs @@ -1,11 +1,14 @@ use crate::{ HOST_HOME_ARGUMENT, HUB_STATE_ARGUMENT, Launch, constants::*, fence::StartupFence, retry, }; -use anyhow::{Result, ensure}; +use anyhow::{Context, Result, anyhow, ensure}; use std::{ + fs::{File, OpenOptions}, + io::Read, os::fd::AsRawFd, + os::unix::fs::OpenOptionsExt, path::Path, - process::{Command, Stdio}, + process::{Command, ExitStatus, Stdio}, }; use tokio::time::{Instant, timeout_at}; use yolo_hub_client::{Client, ClientError}; @@ -32,6 +35,14 @@ pub(crate) async fn ensure( Err(error) => return Err(error.into()), } retry::require_budget(deadline)?; + let stderr_path = launch.state.join(HUB_STARTUP_STDERR); + let stderr = OpenOptions::new() + .create(true) + .write(true) + .truncate(true) + .mode(0o600) + .open(&stderr_path) + .context(STARTUP_STDERR_OPEN_FAILED)?; let mut command = Command::new(&launch.hub_executable); let (commit, input) = yolo_startup_guard::channel()?; command @@ -45,7 +56,7 @@ pub(crate) async fn ensure( .arg(READY_ARGUMENT) .stdin(Stdio::from(input)) .stdout(Stdio::piped()) - .stderr(Stdio::null()); + .stderr(Stdio::from(stderr)); fence.verify()?; let mut child = ScopedWaitableChild::spawn_detached_before_deadline(&mut command, deadline.into_std())?; @@ -56,8 +67,14 @@ pub(crate) async fn ensure( let ready = timeout_at(deadline, crate::readiness::read_ready(&mut input)).await??; fence.verify()?; let Some(ready) = ready else { - child.terminate_and_wait_reaped(deadline.into_std()).await?; - return wait_for_existing(socket, fence, deadline).await; + // A Hub that loses the singleton exits without readiness. Give the + // winner a short grace to become reachable, then report why ours died. + let status = child.terminate_and_wait_status(deadline.into_std()).await?; + let grace = deadline.min(Instant::now() + EXISTING_HUB_GRACE); + if wait_for_existing(socket, fence, grace).await.is_ok() { + return Ok(()); + } + return Err(startup_failure(status, &stderr_path, launch, host_socket).await); }; ensure!( ready.schema_version == CONFIG_SCHEMA_VERSION @@ -90,3 +107,52 @@ async fn wait_for_existing(socket: &Path, fence: &StartupFence, deadline: Instan retry::pause(deadline).await?; } } + +/// Startup errors are written to a bounded, private log before the Hub exits, +/// because its stderr cannot stay attached to a detached service. +async fn startup_failure( + status: ExitStatus, + stderr_path: &Path, + launch: &Launch, + host_socket: &Path, +) -> anyhow::Error { + let mut message = format!("{HUB_EXITED_BEFORE_READY}: {status}"); + if let Some(detail) = startup_stderr(stderr_path) { + message.push_str(": "); + message.push_str(&detail); + } + if let Some(hint) = host_image_hint(launch, host_socket).await { + message.push_str("; "); + message.push_str(&hint); + } + anyhow!(message) +} + +pub(crate) fn startup_stderr(path: &Path) -> Option { + let mut bytes = Vec::new(); + File::open(path) + .ok()? + .take(MAX_STARTUP_STDERR_BYTES as u64) + .read_to_end(&mut bytes) + .ok()?; + let text = String::from_utf8_lossy(&bytes); + let detail = text + .lines() + .map(str::trim) + .filter(|line| !line.is_empty()) + .collect::>() + .join("; "); + (!detail.is_empty()).then_some(detail) +} + +async fn host_image_hint(launch: &Launch, host_socket: &Path) -> Option { + let client = yolo_client::Client::managed_local(&launch.host_home, host_socket).ok()?; + let image = timeout_at(Instant::now() + PROBE_TIMEOUT, client.build_image()) + .await + .ok()? + .ok()?; + Some(format!( + "{HOST_IMAGE_PREFIX}{}{HOST_IMAGE_SUFFIX}", + image.sha256() + )) +} diff --git a/core/crates/hub/local/startup/src/lib.rs b/core/crates/hub/local/startup/src/lib.rs index 207631c3..245e9730 100644 --- a/core/crates/hub/local/startup/src/lib.rs +++ b/core/crates/hub/local/startup/src/lib.rs @@ -31,6 +31,20 @@ pub async fn ensure_running(launch: Launch, deadline: Instant) -> Result Result<()> { + #[cfg(target_os = "linux")] + { + host::require_custody() + } + #[cfg(not(target_os = "linux"))] + { + Ok(()) + } +} + pub async fn ensure_running_with_peer( launch: Launch, expected_peer: Option, diff --git a/core/crates/hub/local/startup/unit_tests/failure.rs b/core/crates/hub/local/startup/unit_tests/failure.rs new file mode 100644 index 00000000..7f579874 --- /dev/null +++ b/core/crates/hub/local/startup/unit_tests/failure.rs @@ -0,0 +1,32 @@ +use crate::constants::MAX_STARTUP_STDERR_BYTES; +use crate::hub::startup_stderr; +use tempfile::tempdir; + +#[test] +fn startup_stderr_is_trimmed_and_joined() { + let directory = tempdir().unwrap(); + let path = directory.path().join("hub-startup.stderr"); + std::fs::write(&path, b"\n first failure \n\n second line\n").unwrap(); + assert_eq!( + startup_stderr(&path).as_deref(), + Some("first failure; second line") + ); +} + +#[test] +fn startup_stderr_is_bounded() { + let directory = tempdir().unwrap(); + let path = directory.path().join("hub-startup.stderr"); + std::fs::write(&path, vec![b'x'; MAX_STARTUP_STDERR_BYTES + 64]).unwrap(); + let detail = startup_stderr(&path).unwrap(); + assert_eq!(detail.len(), MAX_STARTUP_STDERR_BYTES); +} + +#[test] +fn missing_or_empty_startup_stderr_has_no_detail() { + let directory = tempdir().unwrap(); + let path = directory.path().join("hub-startup.stderr"); + assert_eq!(startup_stderr(&path), None); + std::fs::write(&path, b"\n\n").unwrap(); + assert_eq!(startup_stderr(&path), None); +} diff --git a/core/crates/hub/local/startup/unit_tests/lib.rs b/core/crates/hub/local/startup/unit_tests/lib.rs index 857b36cd..3bec3d85 100644 --- a/core/crates/hub/local/startup/unit_tests/lib.rs +++ b/core/crates/hub/local/startup/unit_tests/lib.rs @@ -3,6 +3,7 @@ use yolo_hub_identity::HubId; mod bootstrap_document; #[path = "../test_support/constants.rs"] mod constants; +mod failure; mod readiness; mod retry; use constants::REMOVED_ORIGIN_MAPPING; diff --git a/core/crates/hub/management/client/src/lib.rs b/core/crates/hub/management/client/src/lib.rs index 7c73feb5..d2284d5e 100644 --- a/core/crates/hub/management/client/src/lib.rs +++ b/core/crates/hub/management/client/src/lib.rs @@ -14,17 +14,32 @@ pub async fn execute( command: Command, deadline: Instant, ) -> Result { - execute_endpoint(&HubEndpoint::local(socket), entry, command, deadline).await + let mutation = command.mutates(); + match execute_reply(socket, entry, command, deadline).await? { + Reply::Configuration { snapshot } => Ok(snapshot), + _ if mutation => Err(ManagementError::OutcomeUnknown), + _ => Err(ManagementError::Unavailable), + } } -/// Executes a local-only management request through the selected Hub entry. -/// Owns one connection and one absolute deadline. Never retries a mutation. -pub async fn execute_endpoint( +/// Executes a local-only management request through the selected Hub entry and +/// returns the typed reply. Owns one connection and one absolute deadline. +pub async fn execute_reply( + socket: &Path, + entry: HubId, + command: Command, + deadline: Instant, +) -> Result { + execute_endpoint_reply(&HubEndpoint::local(socket), entry, command, deadline).await +} + +/// Executes a local-only management request through an explicit endpoint. +pub async fn execute_endpoint_reply( endpoint: &HubEndpoint, entry: HubId, command: Command, deadline: Instant, -) -> Result { +) -> Result { if !command.local_only() { return Err(ManagementError::InvalidRequest); } @@ -32,8 +47,22 @@ pub async fn execute_endpoint( .validate_origin(entry) .map_err(|_| ManagementError::InvalidRequest)?; let mut client = endpoint.connect(entry, deadline).await?; + client + .call(entry, command, deadline) + .await + .map_err(ManagementError::from) +} + +/// Executes a local-only management request through the selected Hub entry. +/// Owns one connection and one absolute deadline. Never retries a mutation. +pub async fn execute_endpoint( + endpoint: &HubEndpoint, + entry: HubId, + command: Command, + deadline: Instant, +) -> Result { let mutation = command.mutates(); - match client.call(entry, command, deadline).await? { + match execute_endpoint_reply(endpoint, entry, command, deadline).await? { Reply::Configuration { snapshot } => Ok(snapshot), _ if mutation => Err(ManagementError::OutcomeUnknown), _ => Err(ManagementError::Unavailable), diff --git a/core/crates/hub/management/client/test_support/fixture.rs b/core/crates/hub/management/client/test_support/fixture.rs index a20c4b64..1b205580 100644 --- a/core/crates/hub/management/client/test_support/fixture.rs +++ b/core/crates/hub/management/client/test_support/fixture.rs @@ -15,7 +15,7 @@ pub fn snapshot() -> ConfigurationSnapshot { configuration: Default::default(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), } } pub fn change(state: &ConfigurationSnapshot) -> (Command, ConfigurationSnapshot) { diff --git a/core/crates/hub/management/protocol/src/lib.rs b/core/crates/hub/management/protocol/src/lib.rs index 3fa968a4..ea5cd70f 100644 --- a/core/crates/hub/management/protocol/src/lib.rs +++ b/core/crates/hub/management/protocol/src/lib.rs @@ -7,10 +7,10 @@ pub use constants::*; use serde::{Deserialize, Serialize}; use snapshot_wire::SnapshotWire; use state_wire::StateWire; -use std::net::SocketAddr; +use std::path::PathBuf; use uuid::Uuid; use yolo_hub_delegation::PeerIdentity; -use yolo_hub_federation_settings::Configuration; +use yolo_hub_federation_settings::{Configuration, validate_socket}; #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] @@ -57,24 +57,15 @@ impl ConfigurationState { pub struct ConfigurationSnapshot { pub state: ConfigurationState, pub applied: bool, - pub listen_address: Option, + /// The published peer socket path when this generation is applied. + pub peer_socket: Option, } impl ConfigurationSnapshot { pub fn validate(&self) -> Result<()> { self.state.validate()?; - match ( - self.applied, - self.state.configuration.listen, - self.listen_address, - ) { - (false, _, None) | (true, None, None) => Ok(()), - (true, Some(expected), Some(actual)) - if expected.ip() == actual.ip() - && actual.port() != 0 - && (expected.port() == 0 || expected.port() == actual.port()) => - { - Ok(()) - } + match (self.applied, &self.peer_socket) { + (false, None) => Ok(()), + (true, Some(path)) => validate_socket(path), _ => anyhow::bail!(INVALID_CONFIGURATION_STATE), } } diff --git a/core/crates/hub/management/protocol/src/snapshot_wire.rs b/core/crates/hub/management/protocol/src/snapshot_wire.rs index 27bbd0ea..ec479a56 100644 --- a/core/crates/hub/management/protocol/src/snapshot_wire.rs +++ b/core/crates/hub/management/protocol/src/snapshot_wire.rs @@ -1,13 +1,13 @@ use crate::{ConfigurationSnapshot, ConfigurationState}; use serde::{Deserialize, Serialize}; -use std::net::SocketAddr; +use std::path::PathBuf; #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub(crate) struct SnapshotWire { state: ConfigurationState, applied: bool, - listen_address: Option, + peer_socket: Option, } impl TryFrom for ConfigurationSnapshot { @@ -16,7 +16,7 @@ impl TryFrom for ConfigurationSnapshot { let snapshot = Self { state: wire.state, applied: wire.applied, - listen_address: wire.listen_address, + peer_socket: wire.peer_socket, }; snapshot.validate()?; Ok(snapshot) @@ -28,7 +28,7 @@ impl From for SnapshotWire { Self { state: snapshot.state, applied: snapshot.applied, - listen_address: snapshot.listen_address, + peer_socket: snapshot.peer_socket, } } } diff --git a/core/crates/hub/management/protocol/test_support/constants.rs b/core/crates/hub/management/protocol/test_support/constants.rs index 4d9665c7..4e578344 100644 --- a/core/crates/hub/management/protocol/test_support/constants.rs +++ b/core/crates/hub/management/protocol/test_support/constants.rs @@ -2,9 +2,6 @@ pub const HUB_VALUE: u128 = 1; pub const OTHER_HUB_VALUE: u128 = 2; pub const KEY_BYTE: u8 = 1; pub const OTHER_KEY_BYTE: u8 = 2; -pub const PORT: u16 = 22_000; -pub const OTHER_PORT: u16 = 22_001; -pub const ANY_PORT: u16 = 0; pub const SCHEMA_STEP: u16 = 1; pub const REQUEST_FIELD: &str = "request_id"; pub const REVISION_FIELD: &str = "revision"; diff --git a/core/crates/hub/management/protocol/test_support/fixtures.rs b/core/crates/hub/management/protocol/test_support/fixtures.rs index 2a5ae230..96e8a409 100644 --- a/core/crates/hub/management/protocol/test_support/fixtures.rs +++ b/core/crates/hub/management/protocol/test_support/fixtures.rs @@ -26,11 +26,10 @@ pub fn other_identity() -> PeerIdentity { } pub fn configuration() -> Configuration { Configuration { - listen: None, peers: vec![Peer { identity: other_identity(), dial: None, - consume: true, + consume: false, exports: vec![], }], trusted_origins: vec![], diff --git a/core/crates/hub/management/protocol/unit_tests/snapshot.rs b/core/crates/hub/management/protocol/unit_tests/snapshot.rs index 7ec0bbb6..d5110949 100644 --- a/core/crates/hub/management/protocol/unit_tests/snapshot.rs +++ b/core/crates/hub/management/protocol/unit_tests/snapshot.rs @@ -1,27 +1,16 @@ use super::fixtures::{constants::*, *}; use crate::ConfigurationSnapshot; -use std::net::{IpAddr, Ipv4Addr, SocketAddr}; +use std::path::PathBuf; -fn address(port: u16) -> SocketAddr { - SocketAddr::new(Ipv4Addr::LOCALHOST.into(), port) -} +const SOCKET: &str = "/run/user/1000/yolo/hub/peer.sock"; #[test] -fn listener_snapshot_accepts_only_applied_actual_endpoint_or_unapplied_absence() { - let valid = [ - (false, None, None), - (false, Some(address(PORT)), None), - (true, None, None), - (true, Some(address(PORT)), Some(address(PORT))), - (true, Some(address(ANY_PORT)), Some(address(PORT))), - ]; - for (applied, listen, listen_address) in valid { - let mut state = state(); - state.configuration.listen = listen; +fn snapshot_accepts_only_an_applied_peer_socket_or_unapplied_absence() { + for (applied, peer_socket) in [(false, None), (true, Some(PathBuf::from(SOCKET)))] { let snapshot = ConfigurationSnapshot { - state, + state: state(), applied, - listen_address, + peer_socket, }; snapshot.validate().unwrap(); roundtrip(&snapshot); @@ -30,27 +19,19 @@ fn listener_snapshot_accepts_only_applied_actual_endpoint_or_unapplied_absence() } #[test] -fn inconsistent_bindings_and_nested_invalid_state_are_rejected_on_wire() { - let invalid = [ - (false, None, Some(address(PORT))), - (false, Some(address(PORT)), Some(address(PORT))), - (true, None, Some(address(PORT))), - (true, Some(address(PORT)), None), - (true, Some(address(PORT)), Some(address(OTHER_PORT))), - (true, Some(address(ANY_PORT)), Some(address(ANY_PORT))), - ( - true, - Some(address(PORT)), - Some(SocketAddr::new(IpAddr::V4(Ipv4Addr::UNSPECIFIED), PORT)), - ), - ]; - for (applied, listen, listen_address) in invalid { - let mut state = state(); - state.configuration.listen = listen; +fn inconsistent_peer_sockets_and_nested_invalid_state_are_rejected_on_wire() { + for (applied, peer_socket) in [ + (false, Some(PathBuf::from(SOCKET))), + (true, None), + (true, Some(PathBuf::from("peer.sock"))), + (true, Some(PathBuf::from("/tmp/../peer.sock"))), + (true, Some(PathBuf::from("/tmp/peer\n.sock"))), + (true, Some(PathBuf::from(format!("/{}", "a".repeat(100))))), + ] { let snapshot = ConfigurationSnapshot { - state, + state: state(), applied, - listen_address, + peer_socket, }; assert!(snapshot.validate().is_err()); assert!( @@ -66,7 +47,7 @@ fn inconsistent_bindings_and_nested_invalid_state_are_rejected_on_wire() { ConfigurationSnapshot { state, applied: false, - listen_address: None + peer_socket: None } .validate() .is_err() diff --git a/core/crates/hub/management/runtime/BUILD.bazel b/core/crates/hub/management/runtime/BUILD.bazel index 094c8456..cc70c351 100644 --- a/core/crates/hub/management/runtime/BUILD.bazel +++ b/core/crates/hub/management/runtime/BUILD.bazel @@ -6,12 +6,13 @@ package(default_visibility = ["//visibility:public"]) _DEPS = [ "//core/crates/hub/configuration/store:yolo_hub_configuration_store", + "//core/crates/hub/delegation:yolo_hub_delegation", + "//core/crates/persistence/paths:yolo_paths", "//core/crates/hub/dispatch:yolo_hub_dispatch", "//core/crates/hub/federation:yolo_hub_federation", "//core/crates/hub/federation/settings:yolo_hub_federation_settings", "//core/crates/hub/identity:yolo_hub_identity", "//core/crates/hub/local/backend:yolo_hub_local_backend", - "//core/crates/hub/listener/binding:yolo_hub_listener_binding", "//core/crates/hub/management/protocol:yolo_hub_management_protocol", "//core/crates/hub/peer/dialer:yolo_hub_peer_dialer", "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", @@ -62,8 +63,6 @@ rust_test( crate_root = "src/lib.rs", edition = "2024", deps = _DEPS + [ - "//core/crates/hub/delegation:yolo_hub_delegation", - "//core/crates/hub/peer/transport:yolo_hub_peer_transport", "@crates//:tempfile", "@crates//:uuid", ], diff --git a/core/crates/hub/management/runtime/integration_tests/dialer_generations.rs b/core/crates/hub/management/runtime/integration_tests/dialer_generations.rs index 2bab7f68..ce13fe9e 100644 --- a/core/crates/hub/management/runtime/integration_tests/dialer_generations.rs +++ b/core/crates/hub/management/runtime/integration_tests/dialer_generations.rs @@ -4,11 +4,15 @@ mod constants; mod support; use crate::*; use constants::*; -use std::{num::NonZeroU16, sync::atomic::Ordering}; +use std::sync::atomic::Ordering; use support::{Event, PendingDialer}; use tokio::time::{Instant, timeout}; use yolo_hub_federation_settings::{Peer, PeerAddress}; +fn peer_address(profile: &str, socket: &str) -> PeerAddress { + PeerAddress::ssh(profile, socket.into()).unwrap() +} + #[tokio::test] async fn configuration_generation_changes_reuse_the_injected_factory_and_cancel_old_dials() { let directory = tempfile::tempdir().unwrap(); @@ -21,12 +25,12 @@ async fn configuration_generation_changes_reuse_the_injected_factory_and_cancel_ ) .unwrap(); let identity = remote.peer_identity(); - let first_address = PeerAddress::tcp(DIALER_HOST, NonZeroU16::new(DIALER_PORT).unwrap()); + let first_address = peer_address(FIRST_PROFILE, FIRST_SOCKET); let mut config = Configuration::default(); config.peers.push(Peer { identity, dial: Some(first_address.clone()), - consume: false, + consume: true, exports: vec![], }); let backend = LocalBackend::new(state.hub_id(), &home, &root.join(ABSENT_SOCKET)).unwrap(); @@ -55,7 +59,7 @@ async fn configuration_generation_changes_reuse_the_injected_factory_and_cancel_ assert!(deadline <= Instant::now() + yolo_hub_federation::CONNECT_TIMEOUT); let previous = service.snapshot().unwrap(); let mut change = super::fixture::change(&previous); - let next_address = PeerAddress::tcp(REPLACEMENT_HOST, NonZeroU16::new(DIALER_PORT).unwrap()); + let next_address = peer_address(SECOND_PROFILE, SECOND_SOCKET); change.configuration.peers[0].dial = Some(next_address.clone()); let applied = timeout(DIALER_TEST_TIMEOUT, service.change(change)) .await diff --git a/core/crates/hub/management/runtime/integration_tests/lib.rs b/core/crates/hub/management/runtime/integration_tests/lib.rs index 92c0123f..674fa8e9 100644 --- a/core/crates/hub/management/runtime/integration_tests/lib.rs +++ b/core/crates/hub/management/runtime/integration_tests/lib.rs @@ -2,13 +2,5 @@ mod dialer_generations; mod faults; #[path = "../test_support/fixture.rs"] mod fixture; -mod listener_binding; -#[path = "../test_support/listener_constants.rs"] -mod listener_constants; -mod listener_initialization; -#[path = "../test_support/listener_initialization.rs"] -mod listener_initialization_fixture; -#[path = "../test_support/listener_restart.rs"] -mod listener_restart; mod reconfiguration; mod retirement_custody; diff --git a/core/crates/hub/management/runtime/integration_tests/listener_binding.rs b/core/crates/hub/management/runtime/integration_tests/listener_binding.rs deleted file mode 100644 index ebf262cf..00000000 --- a/core/crates/hub/management/runtime/integration_tests/listener_binding.rs +++ /dev/null @@ -1,122 +0,0 @@ -use super::{fixture::*, listener_constants::*, listener_restart::*}; -use crate::*; -use std::net::TcpListener; -use yolo_hub_listener_binding::LISTENER_BINDING_FILE; - -#[tokio::test] -async fn full_hub_state_reopen_retains_dynamic_endpoint_and_exact_configure_receipt() { - let mut fixture = Fixture::new().await; - let mut update = change(&fixture.service.snapshot().unwrap()); - update.configuration.listen = Some(LOOPBACK.parse().unwrap()); - let applied = fixture.service.change(update.clone()).await.unwrap(); - let (directory, backend) = stopped(fixture).await; - let mut restored = reopen(directory, backend).await.unwrap(); - assert_eq!(restored.service.snapshot().unwrap(), applied); - assert_eq!(restored.service.change(update).await.unwrap(), applied); - restored.service.retire().await.unwrap(); -} - -#[tokio::test] -async fn occupied_saved_endpoint_fails_without_reallocating_or_rewriting() { - let mut fixture = Fixture::new().await; - let mut update = change(&fixture.service.snapshot().unwrap()); - update.configuration.listen = Some(LOOPBACK.parse().unwrap()); - let applied = fixture.service.change(update).await.unwrap(); - let path = fixture - .state - .directory() - .unwrap() - .path() - .join(LISTENER_BINDING_FILE); - let bytes = std::fs::read(&path).unwrap(); - let (directory, backend) = stopped(fixture).await; - let occupied = TcpListener::bind(applied.listen_address.unwrap()).unwrap(); - let state = Arc::new( - HubState::open( - &directory.path().join(HUB_DIRECTORY), - &directory.path().join(HOST_DIRECTORY), - ) - .unwrap(), - ); - assert!( - ManagedHub::open(state.clone(), backend.clone(), None) - .await - .is_err() - ); - assert_eq!(std::fs::read(path).unwrap(), bytes); - drop((state, occupied)); - let mut restored = reopen(directory, backend).await.unwrap(); - assert_eq!(restored.service.snapshot().unwrap(), applied); - restored.service.retire().await.unwrap(); -} - -#[tokio::test] -async fn explicit_disabled_and_fixed_endpoints_survive_restart_without_reviving_old_binding() { - for fixed in [false, true] { - let mut fixture = Fixture::new().await; - let mut update = change(&fixture.service.snapshot().unwrap()); - update.configuration.listen = Some(LOOPBACK.parse().unwrap()); - let dynamic = fixture.service.change(update).await.unwrap(); - let mut disable = change(&dynamic); - if fixed { - let reservation = TcpListener::bind(LOOPBACK).unwrap(); - disable.configuration.listen = Some(reservation.local_addr().unwrap()); - drop(reservation); - } else { - disable.configuration.listen = None; - } - let explicit = fixture.service.change(disable).await.unwrap(); - let occupied = TcpListener::bind(dynamic.listen_address.unwrap()).unwrap(); - let (directory, backend) = stopped(fixture).await; - let mut restored = reopen(directory, backend).await.unwrap(); - assert_eq!(restored.service.snapshot().unwrap(), explicit); - let mut update = change(&explicit); - update.configuration.listen = Some(LOOPBACK.parse().unwrap()); - let fresh = restored.service.change(update).await.unwrap(); - assert_ne!(fresh.listen_address, dynamic.listen_address); - restored.service.retire().await.unwrap(); - drop(occupied); - } -} - -#[tokio::test] -async fn replaced_binding_synchronously_retires_generation_and_closes_listener() { - let mut fixture = Fixture::new().await; - let mut update = change(&fixture.service.snapshot().unwrap()); - update.configuration.listen = Some(LOOPBACK.parse().unwrap()); - let applied = fixture.service.change(update).await.unwrap(); - let path = fixture - .state - .directory() - .unwrap() - .path() - .join(LISTENER_BINDING_FILE); - let saved = fixture.directory.path().join(SAVED_BINDING); - std::fs::rename(&path, &saved).unwrap(); - std::fs::copy(&saved, &path).unwrap(); - assert_eq!( - fixture.service.checked_snapshot().await, - Err(PublicError::Unreachable) - ); - assert!(fixture.hub.generation.read().await.is_none()); - let _available = TcpListener::bind(applied.listen_address.unwrap()).unwrap(); - assert_eq!( - fixture.service.change(change(&applied)).await, - Err(PublicError::Unreachable) - ); -} - -#[tokio::test] -async fn watcher_retires_generation_after_binding_disappears_without_client_activity() { - let fixture = Fixture::new().await; - let path = fixture - .state - .directory() - .unwrap() - .path() - .join(LISTENER_BINDING_FILE); - std::fs::rename(path, fixture.directory.path().join(SAVED_BINDING)).unwrap(); - let (_stop, stopped) = tokio::sync::watch::channel(false); - assert!(fixture.service.run(stopped).await.is_err()); - assert!(fixture.hub.generation.read().await.is_none()); -} diff --git a/core/crates/hub/management/runtime/integration_tests/listener_initialization.rs b/core/crates/hub/management/runtime/integration_tests/listener_initialization.rs deleted file mode 100644 index 31f55daf..00000000 --- a/core/crates/hub/management/runtime/integration_tests/listener_initialization.rs +++ /dev/null @@ -1,101 +0,0 @@ -use super::{ - listener_constants::*, listener_initialization_fixture::InitialStartup, listener_restart::*, -}; -use crate::*; -use yolo_hub_listener_binding::{LISTENER_BINDING_FILE, ListenerBindings}; - -#[tokio::test] -async fn startup_resumes_when_initial_configuration_committed_before_binding_creation() { - let startup = InitialStartup::new(); - assert!(!startup.store.listener_binding_initialized().unwrap()); - assert!( - !startup - .state - .directory() - .unwrap() - .path() - .join(LISTENER_BINDING_FILE) - .exists() - ); - let (directory, backend, original) = startup.release(); - let mut resumed = reopen(directory, backend).await.unwrap(); - let applied = resumed.service.snapshot().unwrap(); - assert_eq!(applied.state, original); - assert!(applied.applied); - assert!( - resumed - .service - .store - .listener_binding_initialized() - .unwrap() - ); - resumed.service.retire().await.unwrap(); -} - -#[tokio::test] -async fn startup_reuses_published_binding_when_initialization_commit_was_interrupted() { - let startup = InitialStartup::new(); - let configuration = startup.store.current().unwrap(); - let mut bindings = ListenerBindings::open( - startup.state.directory().unwrap(), - startup.state.peer_identity(), - &configuration, - !startup.store.listener_binding_initialized().unwrap(), - ) - .unwrap(); - let listener = crate::listener::restore(&mut bindings, &configuration).unwrap(); - let allocated = crate::listener::address(&listener).unwrap(); - let path = startup - .state - .directory() - .unwrap() - .path() - .join(LISTENER_BINDING_FILE); - let binding_bytes = std::fs::read(&path).unwrap(); - assert!(!startup.store.listener_binding_initialized().unwrap()); - drop((bindings, listener)); - let (directory, backend, original) = startup.release(); - let mut resumed = reopen(directory, backend).await.unwrap(); - assert_eq!(resumed.service.snapshot().unwrap().state, original); - assert_eq!(resumed.service.listen_address().unwrap(), allocated); - assert_eq!(std::fs::read(path).unwrap(), binding_bytes); - assert!( - resumed - .service - .store - .listener_binding_initialized() - .unwrap() - ); - resumed.service.retire().await.unwrap(); -} - -#[tokio::test] -async fn initialized_dynamic_listener_cannot_recreate_deleted_binding_on_restart() { - let startup = InitialStartup::new(); - let (directory, backend, _) = startup.release(); - let fixture = reopen(directory, backend).await.unwrap(); - assert!( - fixture - .service - .store - .listener_binding_initialized() - .unwrap() - ); - let path = fixture - .state - .directory() - .unwrap() - .path() - .join(LISTENER_BINDING_FILE); - let (directory, backend) = stopped(fixture).await; - std::fs::rename(&path, directory.path().join(SAVED_BINDING)).unwrap(); - let state = Arc::new( - HubState::open( - &directory.path().join(HUB_DIRECTORY), - &directory.path().join(HOST_DIRECTORY), - ) - .unwrap(), - ); - assert!(ManagedHub::open(state, backend, None).await.is_err()); - assert!(!path.exists()); -} diff --git a/core/crates/hub/management/runtime/integration_tests/reconfiguration.rs b/core/crates/hub/management/runtime/integration_tests/reconfiguration.rs index e4f1a7ad..e9efad4d 100644 --- a/core/crates/hub/management/runtime/integration_tests/reconfiguration.rs +++ b/core/crates/hub/management/runtime/integration_tests/reconfiguration.rs @@ -4,18 +4,19 @@ use tokio::sync::watch; use yolo_hub_rpc_protocol::Command; #[tokio::test] -async fn activation_preserves_ephemeral_listener_and_exact_replay_receipt() { +async fn activation_preserves_the_hub_peer_socket_and_exact_replay_receipt() { let Fixture { directory: _directory, - state: _state, + state, hub, service, } = Fixture::new().await; let (stop, stopped) = watch::channel(false); let task = tokio::spawn(service.run(stopped)); let initial = call(&hub, Command::GetConfiguration {}).await.unwrap(); - let mut update = change(&initial); - update.configuration.listen = Some("127.0.0.1:0".parse().unwrap()); + let expected = state.peer_socket().unwrap(); + assert_eq!(initial.peer_socket.as_ref(), Some(&expected)); + let update = change(&initial); let applied = call( &hub, Command::Configure { @@ -25,7 +26,7 @@ async fn activation_preserves_ephemeral_listener_and_exact_replay_receipt() { .await .unwrap(); assert!(applied.applied); - assert_ne!(applied.listen_address.unwrap().port(), 0); + assert_eq!(applied.peer_socket.as_ref(), Some(&expected)); let replay = call(&hub, Command::Configure { change: update }) .await .unwrap(); @@ -38,7 +39,7 @@ async fn activation_preserves_ephemeral_listener_and_exact_replay_receipt() { ) .await .unwrap(); - assert_eq!(next.listen_address, applied.listen_address); + assert_eq!(next.peer_socket, applied.peer_socket); assert!(next.state.revision > applied.state.revision); stop.send_replace(true); task.await.unwrap().unwrap(); @@ -71,22 +72,6 @@ async fn concurrent_changes_have_one_winner_and_stale_revision_never_overwrites( task.await.unwrap().unwrap(); } -#[tokio::test] -async fn occupied_new_listener_does_not_disrupt_existing_generation() { - let mut fixture = Fixture::new().await; - let occupied = std::net::TcpListener::bind("127.0.0.1:0").unwrap(); - let initial = fixture.service.snapshot().unwrap(); - let mut update = change(&initial); - update.configuration.listen = Some(occupied.local_addr().unwrap()); - assert_eq!( - fixture.service.change(update).await, - Err(PublicError::Conflict) - ); - assert_eq!(fixture.service.snapshot().unwrap(), initial); - assert!(fixture.hub.generation.read().await.is_some()); - fixture.service.retire().await.unwrap(); -} - #[tokio::test] async fn persisted_configuration_survives_service_restart_and_seed_conflicts_fail_closed() { let mut fixture = Fixture::new().await; @@ -103,8 +88,13 @@ async fn persisted_configuration_survives_service_restart_and_seed_conflicts_fai service.retire().await.unwrap(); drop(service); drop(hub); + let remote = HubState::open( + &fixture.directory.path().join("different-remote"), + &fixture.directory.path().join("different-home"), + ) + .unwrap(); let different = Configuration { - listen: Some("127.0.0.1:0".parse().unwrap()), + trusted_origins: vec![remote.peer_identity()], ..Configuration::default() }; assert!( diff --git a/core/crates/hub/management/runtime/src/constants.rs b/core/crates/hub/management/runtime/src/constants.rs index 98315ac3..f685245f 100644 --- a/core/crates/hub/management/runtime/src/constants.rs +++ b/core/crates/hub/management/runtime/src/constants.rs @@ -10,4 +10,4 @@ pub(crate) const ACTIVATION_RETRY_INTERVAL: std::time::Duration = std::time::Dur pub(crate) const CONFIGURATION_VERIFY_INTERVAL: std::time::Duration = std::time::Duration::from_secs(1); pub(crate) const CONFIGURATION_LOST: &str = "Hub configuration custody was lost"; -pub(crate) const LISTENER_MISSING: &str = "Hub listener differs from its durable binding"; +pub(crate) const NO_DIALER: &str = "this Hub has no SSH peer dialer"; diff --git a/core/crates/hub/management/runtime/src/controller.rs b/core/crates/hub/management/runtime/src/controller.rs index b123e92d..304df01c 100644 --- a/core/crates/hub/management/runtime/src/controller.rs +++ b/core/crates/hub/management/runtime/src/controller.rs @@ -4,13 +4,9 @@ use crate::{ generation::{self, Running}, }; use anyhow::Result; -use std::{ - net::{SocketAddr, TcpListener}, - sync::Arc, -}; +use std::{path::PathBuf, sync::Arc}; use tokio::sync::{mpsc, watch}; use yolo_hub_configuration_store::ConfigurationStore; -use yolo_hub_local_backend::LocalBackend; use yolo_hub_management_protocol::{ConfigurationSnapshot, ConfigurationState}; use yolo_hub_terminal_protocol::PublicError; @@ -18,20 +14,19 @@ pub struct ManagementService { pub(crate) hub: Arc, pub(crate) receiver: mpsc::Receiver, pub(crate) store: ConfigurationStore, - pub(crate) bindings: yolo_hub_listener_binding::ListenerBindings, - pub(crate) backend: LocalBackend, - pub(crate) listener: Option, + pub(crate) backend: yolo_hub_local_backend::LocalBackend, + pub(crate) listener: crate::listener::PeerListener, pub(crate) running: Option, pub(crate) retiring: Option, pub(crate) configuration: ConfigurationState, pub(crate) history: Option, pub(crate) activation_pending: bool, - pub(crate) origin: Option>>, + pub(crate) origin: Option>>, } impl ManagementService { - pub fn listen_address(&self) -> Result> { - crate::listener::address(&self.listener) + pub fn peer_socket(&self) -> Result { + self.hub.state.peer_socket() } pub async fn run(mut self, mut shutdown: watch::Receiver) -> Result<()> { @@ -85,32 +80,26 @@ impl ManagementService { let result = self.snapshot(); if result.is_err() { let _ = self.retire().await; - self.listener = None; } result } pub(crate) fn snapshot(&self) -> ManagementResult { let state = self.store.current().map_err(|_| PublicError::Unreachable)?; - self.bindings - .verify(&state) - .map_err(|_| PublicError::Unreachable)?; let applied = self .running .as_ref() .is_some_and(|running| !*running.finished.borrow()) && state == self.configuration; - let listen_address = if applied { - crate::listener::verify(&self.bindings, &state, &self.listener) - .map_err(|_| PublicError::Unreachable)?; - self.listen_address().map_err(|_| PublicError::Internal)? + let peer_socket = if applied { + Some(self.peer_socket().map_err(|_| PublicError::Internal)?) } else { None }; let snapshot = ConfigurationSnapshot { state, applied, - listen_address, + peer_socket, }; snapshot.validate().map_err(|_| PublicError::Internal)?; Ok(snapshot) diff --git a/core/crates/hub/management/runtime/src/dispatch.rs b/core/crates/hub/management/runtime/src/dispatch.rs index d7c3f6a1..e734a8ea 100644 --- a/core/crates/hub/management/runtime/src/dispatch.rs +++ b/core/crates/hub/management/runtime/src/dispatch.rs @@ -45,6 +45,13 @@ impl Dispatcher for ManagedHub { if fields.destination != self.hub_id() { return Err(PublicError::Unauthorized); } + if matches!(fields.command, Command::ListConsumers {}) { + let generation = self.generation.read().await; + let federation = generation.as_ref().ok_or(PublicError::Unsupported)?; + return Ok(Operation::Reply(Reply::Consumers { + consumers: federation.consumers(), + })); + } let change = match &fields.command { Command::Configure { change } => Some(change.clone()), _ => None, diff --git a/core/crates/hub/management/runtime/src/generation.rs b/core/crates/hub/management/runtime/src/generation.rs index d9dabf03..0971e5bf 100644 --- a/core/crates/hub/management/runtime/src/generation.rs +++ b/core/crates/hub/management/runtime/src/generation.rs @@ -1,5 +1,5 @@ use anyhow::Result; -use std::{net::TcpListener, sync::Arc}; +use std::sync::Arc; use tokio::{sync::watch, task::JoinHandle}; use yolo_hub_federation::{Federation, ObservationHistory}; use yolo_hub_federation_settings::Configuration; @@ -19,12 +19,12 @@ impl Running { state: Arc, backend: LocalBackend, configuration: Configuration, - listener: &Option, + listener: &crate::listener::PeerListener, history: Option, dialer: Arc, origin: Option>>, ) -> Result { - let listener = crate::listener::activate(listener)?; + let listener = listener.activate()?; let node = Federation::new_with_dialer(state, backend, configuration, dialer, origin).await?; if let Some(history) = history { node.restore_history(history).await?; @@ -33,7 +33,7 @@ impl Running { let (done, finished) = watch::channel(false); let running = node.clone(); let task = tokio::spawn(async move { - let result = running.run(listener, stopped).await; + let result = running.run(Some(listener), stopped).await; done.send_replace(true); result }); diff --git a/core/crates/hub/management/runtime/src/lib.rs b/core/crates/hub/management/runtime/src/lib.rs index 155c202d..ede3a163 100644 --- a/core/crates/hub/management/runtime/src/lib.rs +++ b/core/crates/hub/management/runtime/src/lib.rs @@ -12,10 +12,9 @@ use tokio::sync::{RwLock, mpsc, oneshot}; use yolo_hub_configuration_store::{CONFIGURATION_DIRECTORY, ConfigurationStore}; use yolo_hub_federation::Federation; use yolo_hub_federation_settings::Configuration; -use yolo_hub_listener_binding::ListenerBindings; use yolo_hub_local_backend::LocalBackend; use yolo_hub_management_protocol::{ConfigurationChange, ConfigurationSnapshot}; -use yolo_hub_peer_dialer::{PeerDialer, TcpPeerDialer}; +use yolo_hub_peer_dialer::{PeerDialFuture, PeerDialer}; use yolo_hub_state::HubState; use yolo_hub_terminal_protocol::PublicError; @@ -26,6 +25,19 @@ struct ManagementRequest { reply: oneshot::Sender, } +struct NoDialer; + +impl PeerDialer for NoDialer { + fn dial<'a>( + &'a self, + _: yolo_hub_delegation::PeerIdentity, + _: &'a yolo_hub_federation_settings::PeerAddress, + _: tokio::time::Instant, + ) -> PeerDialFuture<'a> { + Box::pin(async { anyhow::bail!(constants::NO_DIALER) }) + } +} + pub struct ManagedHub { state: Arc, generation: RwLock>>, @@ -49,7 +61,7 @@ impl ManagedHub { seed: Option, build: Option>, ) -> Result<(Arc, ManagementService)> { - Self::open_with_dialer(state, backend, seed, build, Arc::new(TcpPeerDialer)).await + Self::open_with_dialer(state, backend, seed, build, Arc::new(NoDialer)).await } pub async fn open_with_dialer( @@ -82,26 +94,17 @@ impl ManagedHub { origin: Option>>, ) -> Result<(Arc, ManagementService)> { let root = state.directory()?; - let mut store = ConfigurationStore::open( + let (commands, receiver) = mpsc::channel(constants::MANAGEMENT_QUEUE_CAPACITY); + let store = ConfigurationStore::open( root.ensure_descendant(&root.path().join(CONFIGURATION_DIRECTORY))?, state.peer_identity(), seed, )?; let configuration = store.current()?; - let mut bindings = ListenerBindings::open( - root.try_clone()?, - state.peer_identity(), - &configuration, - !store.listener_binding_initialized()?, - )?; - let listener = listener::restore(&mut bindings, &configuration)?; + let listener = listener::PeerListener::publish(&state).await?; let prepared_backend = backend .prepare_files(root.path(), &configuration.configuration.file_roots) .await?; - store.complete_listener_initialization(|| { - listener::verify(&bindings, &configuration, &listener) - })?; - listener::verify(&bindings, &configuration, &listener)?; let running = generation::Running::start( state.clone(), prepared_backend, @@ -112,7 +115,6 @@ impl ManagedHub { origin.clone(), ) .await?; - let (commands, receiver) = mpsc::channel(constants::MANAGEMENT_QUEUE_CAPACITY); let hub = Arc::new(Self { state, generation: RwLock::new(Some(running.node.clone())), @@ -124,7 +126,6 @@ impl ManagedHub { hub: hub.clone(), receiver, store, - bindings, backend, listener, running: Some(running), diff --git a/core/crates/hub/management/runtime/src/listener.rs b/core/crates/hub/management/runtime/src/listener.rs index e232fdb1..613075d8 100644 --- a/core/crates/hub/management/runtime/src/listener.rs +++ b/core/crates/hub/management/runtime/src/listener.rs @@ -1,58 +1,46 @@ -use anyhow::Result; -use std::net::{SocketAddr, TcpListener}; -use yolo_hub_listener_binding::{ListenerBindings, persistent}; -use yolo_hub_management_protocol::ConfigurationState; +use anyhow::{Context, Result}; +use std::time::Duration; +use yolo_hub_state::HubState; +use yolo_paths::socket_activation::{self, SocketPublication}; -pub(crate) fn restore( - bindings: &mut ListenerBindings, - configuration: &ConfigurationState, -) -> Result> { - let requested = configuration.configuration.listen; - let saved = bindings.address(configuration)?; - let listener = prepare(saved.or(requested))?; - if persistent(requested) && saved.is_none() { - bindings.establish( - configuration, - address(&listener)? - .ok_or_else(|| anyhow::anyhow!(crate::constants::LISTENER_MISSING))?, - )?; - } - verify(bindings, configuration, &listener)?; - Ok(listener) -} +const PROBE_TIMEOUT: Duration = Duration::from_secs(1); +const LIVE: &str = "another Hub owns this peer socket"; +const BIND: &str = "Hub peer socket could not be published"; -pub(crate) fn verify( - bindings: &ListenerBindings, - configuration: &ConfigurationState, - listener: &Option, -) -> Result<()> { - bindings.verify(configuration)?; - if persistent(configuration.configuration.listen) { - anyhow::ensure!( - bindings.address(configuration)? == address(listener)?, - crate::constants::LISTENER_MISSING - ); - } - Ok(()) +/// The Hub's only peer entry point: a private Unix socket in Hub state. +/// The outer SSH login decides who can reach it; peer policy still decides +/// which Hub identities may use it. +pub(crate) struct PeerListener { + listener: std::os::unix::net::UnixListener, + publication: Option, } -pub(crate) fn prepare(address: Option) -> Result> { - address - .map(|address| { - let listener = TcpListener::bind(address)?; - listener.set_nonblocking(true)?; - Ok(listener) +impl PeerListener { + pub(crate) async fn publish(state: &HubState) -> Result { + let path = state.peer_socket()?; + let (listener, publication) = + socket_activation::publish(state.directory()?, &path, PROBE_TIMEOUT, LIVE).await?; + let listener = listener + .into_std() + .context(BIND)?; + listener.set_nonblocking(true).context(BIND)?; + Ok(Self { + listener, + publication: Some(publication), }) - .transpose() -} + } -pub(crate) fn activate(listener: &Option) -> Result> { - listener - .as_ref() - .map(|listener| Ok(tokio::net::TcpListener::from_std(listener.try_clone()?)?)) - .transpose() + pub(crate) fn activate(&self) -> Result { + Ok(tokio::net::UnixListener::from_std( + self.listener.try_clone().context(BIND)?, + )?) + } } -pub(crate) fn address(listener: &Option) -> Result> { - Ok(listener.as_ref().map(TcpListener::local_addr).transpose()?) +impl Drop for PeerListener { + fn drop(&mut self) { + if let Some(publication) = self.publication.take() { + let _ = publication.withdraw(); + } + } } diff --git a/core/crates/hub/management/runtime/src/transition.rs b/core/crates/hub/management/runtime/src/transition.rs index fbeba569..7a2700ef 100644 --- a/core/crates/hub/management/runtime/src/transition.rs +++ b/core/crates/hub/management/runtime/src/transition.rs @@ -1,6 +1,5 @@ use crate::{ManagementResult, ManagementService, constants::*, generation::Running}; use yolo_hub_configuration_store::{RevisionConflict, RevisionExhausted}; -use yolo_hub_local_backend::LocalBackend; use yolo_hub_management_protocol::ConfigurationChange; use yolo_hub_terminal_protocol::PublicError; @@ -22,41 +21,14 @@ impl ManagementService { if current.last_change == change.request_id && self.running.is_some() { return self.confirmed_snapshot().await; } - if self.bindings.verify(¤t).is_err() { - let _ = self.retire().await; - self.listener = None; - return Err(PublicError::Unreachable); - } - // Reserve changed endpoints before disrupting the current, valid generation. - let unchanged = self.configuration.configuration.listen == change.configuration.listen; - let replacement = if unchanged { - None - } else { - Some( - crate::listener::prepare(change.configuration.listen) - .map_err(|_| PublicError::Conflict)?, - ) - }; let directory = self .hub .state .directory() .map_err(|_| PublicError::Unreachable)?; - let prepared_backend = self - .backend + let prepared_backend = self.backend .prepare_files(directory.path(), &change.configuration.file_roots) .await?; - let planned = self - .store - .planned(&change) - .map_err(|_| PublicError::Unreachable)?; - let address = crate::listener::address(replacement.as_ref().unwrap_or(&self.listener)) - .map_err(|_| PublicError::Internal)?; - if planned != current && self.bindings.prepare(¤t, &planned, address).is_err() { - let _ = self.retire().await; - self.listener = None; - return Err(PublicError::Unreachable); - } self.retire() .await .map_err(|_| PublicError::OutcomeUnknown)?; @@ -74,9 +46,6 @@ impl ManagementService { } }; self.configuration = state; - if let Some(replacement) = replacement { - self.listener = replacement; - } match self.start_generation(prepared_backend).await { Ok(()) => self.confirmed_snapshot().await, Err(error) => { @@ -90,7 +59,7 @@ impl ManagementService { } } - async fn start_generation(&mut self, prepared_backend: LocalBackend) -> anyhow::Result<()> { + async fn start_generation(&mut self, prepared_backend: yolo_hub_local_backend::LocalBackend) -> anyhow::Result<()> { let running = Running::start( self.hub.state.clone(), prepared_backend, @@ -118,7 +87,7 @@ impl ManagementService { if !self.activation_pending { return false; } - let current = match self.store.current() { + let _current = match self.store.current() { Ok(current) if current == self.configuration => current, Ok(_) => { tracing::warn!(message = ACTIVATION_RETRY_FAILED); @@ -129,10 +98,6 @@ impl ManagementService { return false; } }; - if let Err(error) = self.bindings.verify(¤t) { - tracing::warn!(%error, message = ACTIVATION_RETRY_FAILED); - return false; - } let directory = match self.hub.state.directory() { Ok(directory) => directory, Err(error) => { @@ -140,8 +105,7 @@ impl ManagementService { return false; } }; - let prepared_backend = match self - .backend + let prepared_backend = match self.backend .prepare_files( directory.path(), &self.configuration.configuration.file_roots, @@ -165,7 +129,6 @@ impl ManagementService { } } } - async fn confirmed_snapshot(&mut self) -> ManagementResult { let snapshot = self.checked_snapshot().await?; if snapshot.applied { diff --git a/core/crates/hub/management/runtime/test_support/dialer_constants.rs b/core/crates/hub/management/runtime/test_support/dialer_constants.rs index fa328f5e..557ac146 100644 --- a/core/crates/hub/management/runtime/test_support/dialer_constants.rs +++ b/core/crates/hub/management/runtime/test_support/dialer_constants.rs @@ -1,8 +1,11 @@ use std::time::Duration; pub const DIALER_TEST_TIMEOUT: Duration = Duration::from_secs(15); -pub const DIALER_HOST: &str = "configured-first.invalid"; -pub const REPLACEMENT_HOST: &str = "configured-second.invalid"; -pub const DIALER_PORT: u16 = 43123; +pub const FIRST_PROFILE: &str = "configured-first"; +pub const SECOND_PROFILE: &str = "configured-second"; +pub const PROFILE_REVISION: &str = + "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; +pub const FIRST_SOCKET: &str = "/run/user/1000/yolo/first.sock"; +pub const SECOND_SOCKET: &str = "/run/user/1000/yolo/second.sock"; pub const HOST_DIRECTORY: &str = "host"; pub const HUB_DIRECTORY: &str = "hub"; pub const REMOTE_DIRECTORY: &str = "remote"; diff --git a/core/crates/hub/management/runtime/test_support/listener_constants.rs b/core/crates/hub/management/runtime/test_support/listener_constants.rs deleted file mode 100644 index 77ea7568..00000000 --- a/core/crates/hub/management/runtime/test_support/listener_constants.rs +++ /dev/null @@ -1,5 +0,0 @@ -pub const LOOPBACK: &str = "127.0.0.1:0"; -pub const HUB_DIRECTORY: &str = "hub"; -pub const HOST_DIRECTORY: &str = "host"; -pub const SAVED_BINDING: &str = "saved-binding.json"; -pub const HOST_SOCKET: &str = "absent-host.sock"; diff --git a/core/crates/hub/management/runtime/test_support/listener_initialization.rs b/core/crates/hub/management/runtime/test_support/listener_initialization.rs deleted file mode 100644 index 05230f76..00000000 --- a/core/crates/hub/management/runtime/test_support/listener_initialization.rs +++ /dev/null @@ -1,50 +0,0 @@ -use super::listener_constants::*; -use crate::*; -use yolo_hub_configuration_store::ConfigurationStore; -use yolo_hub_management_protocol::ConfigurationState; - -pub struct InitialStartup { - pub directory: tempfile::TempDir, - pub state: Arc, - pub backend: LocalBackend, - pub store: ConfigurationStore, -} - -impl InitialStartup { - pub fn new() -> Self { - let directory = tempfile::tempdir().unwrap(); - let home = directory.path().join(HOST_DIRECTORY); - let state = Arc::new(HubState::open(&directory.path().join(HUB_DIRECTORY), &home).unwrap()); - let backend = - LocalBackend::new(state.hub_id(), &home, &directory.path().join(HOST_SOCKET)).unwrap(); - let root = state.directory().unwrap(); - let store = ConfigurationStore::open( - root.ensure_descendant(&root.path().join(CONFIGURATION_DIRECTORY)) - .unwrap(), - state.peer_identity(), - Some(Configuration { - listen: Some(LOOPBACK.parse().unwrap()), - ..Configuration::default() - }), - ) - .unwrap(); - Self { - directory, - state, - backend, - store, - } - } - - pub fn release(self) -> (tempfile::TempDir, LocalBackend, ConfigurationState) { - let configuration = self.store.current().unwrap(); - let Self { - directory, - state, - backend, - store, - } = self; - drop((store, state)); - (directory, backend, configuration) - } -} diff --git a/core/crates/hub/management/runtime/test_support/listener_restart.rs b/core/crates/hub/management/runtime/test_support/listener_restart.rs deleted file mode 100644 index 81445ec1..00000000 --- a/core/crates/hub/management/runtime/test_support/listener_restart.rs +++ /dev/null @@ -1,29 +0,0 @@ -use super::{fixture::Fixture, listener_constants::*}; -use crate::*; - -pub async fn stopped(fixture: Fixture) -> (tempfile::TempDir, LocalBackend) { - let Fixture { - directory, - state, - hub, - mut service, - } = fixture; - let backend = service.backend.clone(); - service.retire().await.unwrap(); - drop((service, hub, state)); - (directory, backend) -} - -pub async fn reopen(directory: tempfile::TempDir, backend: LocalBackend) -> Result { - let state = Arc::new(HubState::open( - &directory.path().join(HUB_DIRECTORY), - &directory.path().join(HOST_DIRECTORY), - )?); - let (hub, service) = ManagedHub::open(state.clone(), backend, None).await?; - Ok(Fixture { - directory, - state, - hub, - service, - }) -} diff --git a/core/crates/hub/management/runtime/test_support/pending_dialer.rs b/core/crates/hub/management/runtime/test_support/pending_dialer.rs index 5780461c..3c7f89ac 100644 --- a/core/crates/hub/management/runtime/test_support/pending_dialer.rs +++ b/core/crates/hub/management/runtime/test_support/pending_dialer.rs @@ -4,7 +4,7 @@ use tokio::{sync::mpsc, time::Instant}; use yolo_hub_delegation::PeerIdentity; use yolo_hub_federation_settings::PeerAddress; use yolo_hub_peer_dialer::{PeerDialFuture, PeerDialer}; -use yolo_hub_peer_transport::PeerIo; +use yolo_hub_peer_dialer::PeerIo; pub enum Event { Started { diff --git a/core/crates/hub/management/runtime/unit_tests/lib.rs b/core/crates/hub/management/runtime/unit_tests/lib.rs index d3323aff..cb2bea33 100644 --- a/core/crates/hub/management/runtime/unit_tests/lib.rs +++ b/core/crates/hub/management/runtime/unit_tests/lib.rs @@ -26,7 +26,7 @@ fn fixture() -> ( generation: RwLock::new(None), commands, build: None, - dialer: Arc::new(TcpPeerDialer), + dialer: Arc::new(crate::NoDialer), }); (directory, hub, receiver) } @@ -161,7 +161,7 @@ async fn configuration_reply_preserves_actor_result() { configuration: Configuration::default(), }, applied: false, - listen_address: None, + peer_socket: None, }; receiver .recv() diff --git a/core/crates/hub/peer/diagnostics/BUILD.bazel b/core/crates/hub/peer/diagnostics/BUILD.bazel new file mode 100644 index 00000000..f81e1fd1 --- /dev/null +++ b/core/crates/hub/peer/diagnostics/BUILD.bazel @@ -0,0 +1,32 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "//core/crates/hub/identity:yolo_hub_identity", +] + +filegroup( + name = "architecture_check_data", + srcs = glob(["**/*.rs"]) + ["BUILD.bazel"], +) + +rust_library( + name = "yolo_hub_peer_diagnostics", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_hub_peer_diagnostics", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) + +rust_test( + name = "unit_tests", + srcs = glob(["unit_tests/**/*.rs"]), + crate_name = "yolo_hub_peer_diagnostics_unit_tests", + crate_root = "unit_tests/lib.rs", + edition = "2024", + deps = _DEPS + [":yolo_hub_peer_diagnostics"], +) diff --git a/core/crates/hub/federation/src/peer_diagnostics.rs b/core/crates/hub/peer/diagnostics/src/lib.rs similarity index 71% rename from core/crates/hub/federation/src/peer_diagnostics.rs rename to core/crates/hub/peer/diagnostics/src/lib.rs index 8dd8ec4f..09e38da9 100644 --- a/core/crates/hub/federation/src/peer_diagnostics.rs +++ b/core/crates/hub/peer/diagnostics/src/lib.rs @@ -3,22 +3,23 @@ use std::sync::{Mutex, MutexGuard}; use std::time::{Duration, Instant}; use yolo_hub_identity::HubId; -pub(crate) const PEER_ATTEMPT_FAILED: &str = "Hub peer connection attempt failed"; -pub(crate) const PEER_RECOVERED: &str = "Hub peer connection recovered"; -pub(crate) const INBOUND_PEER_REJECTED: &str = "Hub rejected an inbound peer connection"; -pub(crate) const INBOUND_REJECTION_INTERVAL: Duration = Duration::from_secs(30); -pub(crate) const EXPORT_DEPTH_EXHAUSTED: &str = +pub const PEER_ATTEMPT_FAILED: &str = "Hub peer connection attempt failed"; +pub const PEER_RECOVERED: &str = "Hub peer connection recovered"; +pub const PEER_CONNECTED: &str = "Hub peer connection established"; +pub const INBOUND_PEER_REJECTED: &str = "Hub rejected an inbound peer connection"; +pub const INBOUND_REJECTION_INTERVAL: Duration = Duration::from_secs(30); +pub const EXPORT_DEPTH_EXHAUSTED: &str = "Hub export depth exhausted; origin omitted from peer directory"; #[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub(crate) enum PeerAttempt { +pub enum PeerAttempt { Dial, Handshake, Admission, } impl PeerAttempt { - pub(crate) fn as_str(self) -> &'static str { + pub fn as_str(self) -> &'static str { match self { Self::Dial => "dial", Self::Handshake => "handshake", @@ -32,14 +33,14 @@ const FAILURE_LOG_EVERY: u64 = 30; /// Counts consecutive outbound failures per configured peer. A repeated stage /// is reported only periodically, so a long outage cannot fill the journal. #[derive(Default)] -pub(crate) struct OutboundFailures { +pub struct OutboundFailures { stage: Option, attempts: u64, } impl OutboundFailures { /// Returns the attempt count when this failure should be logged. - pub(crate) fn fail(&mut self, stage: PeerAttempt) -> Option { + pub fn fail(&mut self, stage: PeerAttempt) -> Option { self.attempts = self.attempts.saturating_add(1); let changed = self.stage != Some(stage); self.stage = Some(stage); @@ -47,7 +48,7 @@ impl OutboundFailures { } /// Returns the consecutive failure count when this success ends a streak. - pub(crate) fn succeed(&mut self) -> Option { + pub fn succeed(&mut self) -> Option { let attempts = std::mem::take(&mut self.attempts); self.stage.take().map(|_| attempts) } @@ -56,14 +57,14 @@ impl OutboundFailures { /// Bounds inbound rejection logging across all handshake tasks. The first /// rejection is logged immediately; later ones are counted for the next window. #[derive(Default)] -pub(crate) struct InboundRejections { +pub struct InboundRejections { window_start: Option, suppressed: u64, } impl InboundRejections { /// Returns the suppressed count when a rejection should be logged. - pub(crate) fn reject(&mut self, now: Instant) -> Option { + pub fn reject(&mut self, now: Instant) -> Option { match self.window_start { Some(start) if now.duration_since(start) < INBOUND_REJECTION_INTERVAL => { self.suppressed = self.suppressed.saturating_add(1); @@ -83,17 +84,17 @@ type Denial = (HubId, HubId, u64); /// is removed when a refresh exports that origin again, so a recurrence is /// still visible. #[derive(Default)] -pub(crate) struct ExportDepthDenials { +pub struct ExportDepthDenials { reported: Mutex>, } impl ExportDepthDenials { /// Returns true when this denial has not already been reported. - pub(crate) fn denied(&self, peer: HubId, origin: HubId, policy_epoch: u64) -> bool { + pub fn denied(&self, peer: HubId, origin: HubId, policy_epoch: u64) -> bool { self.lock().insert((peer, origin, policy_epoch)) } - pub(crate) fn exported(&self, peer: HubId, origin: HubId, policy_epoch: u64) { + pub fn exported(&self, peer: HubId, origin: HubId, policy_epoch: u64) { self.lock().remove(&(peer, origin, policy_epoch)); } @@ -104,6 +105,3 @@ impl ExportDepthDenials { } } -#[cfg(all(test, feature = "yolo-unit-tests"))] -#[path = "../unit_tests/peer_diagnostics.rs"] -mod unit_tests; diff --git a/core/crates/hub/federation/unit_tests/peer_diagnostics.rs b/core/crates/hub/peer/diagnostics/unit_tests/lib.rs similarity index 98% rename from core/crates/hub/federation/unit_tests/peer_diagnostics.rs rename to core/crates/hub/peer/diagnostics/unit_tests/lib.rs index 1cea077f..c6c92955 100644 --- a/core/crates/hub/federation/unit_tests/peer_diagnostics.rs +++ b/core/crates/hub/peer/diagnostics/unit_tests/lib.rs @@ -1,4 +1,4 @@ -use crate::peer_diagnostics::{ +use yolo_hub_peer_diagnostics::{ ExportDepthDenials, INBOUND_REJECTION_INTERVAL, InboundRejections, OutboundFailures, PeerAttempt, }; diff --git a/core/crates/hub/peer/dialer/BUILD.bazel b/core/crates/hub/peer/dialer/BUILD.bazel index 37db3f48..ac25bab4 100644 --- a/core/crates/hub/peer/dialer/BUILD.bazel +++ b/core/crates/hub/peer/dialer/BUILD.bazel @@ -6,21 +6,20 @@ package(default_visibility = ["//visibility:public"]) _DEPS = [ "//core/crates/hub/delegation:yolo_hub_delegation", "//core/crates/hub/federation/settings:yolo_hub_federation_settings", - "//core/crates/hub/peer/transport:yolo_hub_peer_transport", + "//core/crates/hub/peer/io:yolo_hub_peer_io", + "//core/crates/runtime/engine/process:yolo_process", "@crates//:anyhow", "@crates//:tokio", + "@crates//:tracing", ] _TEST_DEPS = [ "//core/crates/hub/identity:yolo_hub_identity", "@crates//:ring", - "@crates//:russh", ] filegroup(name = "architecture_check_data", srcs = glob(["**/*"], exclude = ["BUILD.bazel"]) + ["BUILD.bazel"]) rust_library(name = "yolo_hub_peer_dialer", srcs = glob(["src/**/*.rs"]), crate_name = "yolo_hub_peer_dialer", crate_root = "src/lib.rs", edition = "2024", version = YOLO_MARKETING_VERSION, deps = _DEPS) -rust_test(name = "unit_tests", srcs = glob(["unit_tests/**/*.rs", "test_support/**/*.rs"]), crate_name = "yolo_hub_peer_dialer_unit_tests", crate_root = "unit_tests/lib.rs", edition = "2024", deps = _DEPS + _TEST_DEPS + [":yolo_hub_peer_dialer"]) - -rust_test(name = "integration_tests", srcs = glob(["integration_tests/**/*.rs", "test_support/**/*.rs"]), crate_name = "yolo_hub_peer_dialer_integration_tests", crate_root = "integration_tests/lib.rs", edition = "2024", deps = _DEPS + _TEST_DEPS + [":yolo_hub_peer_dialer"]) +rust_test(name = "unit_tests", srcs = glob(["unit_tests/**/*.rs", "test_support/**/*.rs"]), crate_name = "yolo_hub_peer_dialer_unit_tests", crate_root = "unit_tests/lib.rs", edition = "2024", deps = _DEPS + _TEST_DEPS + [":yolo_hub_peer_dialer", "@crates//:tempfile"]) diff --git a/core/crates/hub/peer/dialer/README.md b/core/crates/hub/peer/dialer/README.md index 1a771962..02f47e5f 100644 --- a/core/crates/hub/peer/dialer/README.md +++ b/core/crates/hub/peer/dialer/README.md @@ -2,22 +2,26 @@ `PeerDialer` opens a `PeerIo` for the complete configured `PeerIdentity`, `PeerAddress`, and one absolute deadline. It has no local signing key and cannot -establish a trusted federation connection. Federation always passes the returned -I/O through `peer_transport::connect_io` with its configured pin and private key. +establish a trusted federation connection. Federation passes the returned I/O +through `peer_link::connect`, which performs the signed `PeerHello` identity +handshake with the remote Hub before any federated call is sent. -`dial_peer` bounds factory execution and validates the existing address contract. +`dial_peer` bounds factory execution and validates the address contract. Dropping the returned future cancels its in-flight factory future. Implementations -must retain cancellation custody for resources they create; returned `PeerIo` -owns the close action for exactly one transport. The default `TcpPeerDialer` -accepts only explicit/default TCP transport and preserves duplicate-socket close -custody through `PeerIo::from_tcp`. SSH bindings require an injected factory; -missing or failed bindings never fall back to TCP. +must retain cancellation custody for resources they create; the returned `PeerIo` +owns the close action for exactly one transport. + +Production injects `SshProfileRuntime`, which resolves the profile id against the +persistent Hub registry and opens a broker `direct-streamlocal` tunnel to the +remote `/peer.sock`. There is no default TCP dialer and no fallback: +a missing profile or failed broker open rejects the link. Embedded or in-process +tests may inject a direct Unix-socket dialer or a rejecting dialer. `Federation::new_with_dialer` injects a factory. `ManagedHub::open_with_dialer` retains the same factory across configuration generations, while old generation tasks and transports are retired before a replacement generation runs. Opening -and mutual authentication share the original deadline. Existing mutation -forwarding and unknown-outcome rules are unchanged. +the transport and the identity handshake share the original deadline. Existing +mutation forwarding and unknown-outcome rules are unchanged. Profile lookup, SSH connection pooling, enrollment, and external credentials belong to the composition layer. This component imports no remote broker or diff --git a/core/crates/hub/peer/dialer/integration_tests/authentication.rs b/core/crates/hub/peer/dialer/integration_tests/authentication.rs deleted file mode 100644 index a69ce7ea..00000000 --- a/core/crates/hub/peer/dialer/integration_tests/authentication.rs +++ /dev/null @@ -1,104 +0,0 @@ -use super::{constants::*, fixture::*, support::*}; -use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, -}; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, - time::timeout_at, -}; -use yolo_hub_peer_dialer::dial_peer; -use yolo_hub_peer_transport::{PeerIo, PeerPins, accept_io, connect_io}; - -#[tokio::test] -async fn injected_io_still_requires_both_pins_and_preserves_independent_peer_channels() { - let (local, local_key) = keys(); - let (peer, peer_key) = keys(); - let (left, right) = tokio::io::duplex(BUFFER_BYTES); - let closed = Arc::new(AtomicUsize::default()); - let observed = closed.clone(); - let factory = Injected::new( - Some(PeerIo::new(left, move || { - observed.fetch_add(ONE, Ordering::SeqCst); - })), - Behavior::Ready, - ); - let address = address(); - let deadline = deadline(); - let (connected, accepted) = tokio::join!( - async { - connect_io( - dial_peer(&factory, peer, &address, deadline).await.unwrap(), - local_key, - peer, - deadline, - ) - .await - }, - accept_io( - PeerIo::new(right, || {}), - peer_key, - PeerPins::new(vec![local]).unwrap(), - deadline - ), - ); - let (client, _) = connected.unwrap(); - let (server, mut incoming) = accepted.unwrap(); - assert_eq!(client.peer(), peer); - assert_eq!(server.peer(), local); - let mut stream = client.open_channel(deadline).await.unwrap(); - let mut remote = timeout_at(deadline, incoming.recv()) - .await - .unwrap() - .unwrap(); - stream.write_all(PAYLOAD).await.unwrap(); - let mut bytes = vec![0; PAYLOAD.len()]; - timeout_at(deadline, remote.read_exact(&mut bytes)) - .await - .unwrap() - .unwrap(); - assert_eq!(bytes, PAYLOAD); - client.close(deadline).await.unwrap(); - server.close(deadline).await.unwrap(); - assert_eq!(closed.load(Ordering::SeqCst), ONE); -} - -#[tokio::test] -async fn injected_factory_cannot_replace_the_configured_hub_public_key() { - let (local, local_key) = keys(); - let (_, actual_key) = keys(); - let (configured, _) = keys(); - let (left, right) = tokio::io::duplex(BUFFER_BYTES); - let closed = Arc::new(AtomicUsize::default()); - let observed = closed.clone(); - let factory = Injected::new( - Some(PeerIo::new(left, move || { - observed.fetch_add(ONE, Ordering::SeqCst); - })), - Behavior::Ready, - ); - let address = address(); - let deadline = deadline(); - let (connected, accepted) = tokio::join!( - async { - connect_io( - dial_peer(&factory, configured, &address, deadline) - .await - .unwrap(), - local_key, - configured, - deadline, - ) - .await - }, - accept_io( - PeerIo::new(right, || {}), - actual_key, - PeerPins::new(vec![local]).unwrap(), - deadline - ), - ); - assert!(connected.is_err()); - assert!(accepted.is_err()); - assert_eq!(closed.load(Ordering::SeqCst), ONE); -} diff --git a/core/crates/hub/peer/dialer/integration_tests/lib.rs b/core/crates/hub/peer/dialer/integration_tests/lib.rs deleted file mode 100644 index 0b2925f3..00000000 --- a/core/crates/hub/peer/dialer/integration_tests/lib.rs +++ /dev/null @@ -1,8 +0,0 @@ -mod authentication; -#[path = "../test_support/constants.rs"] -mod constants; -#[path = "../test_support/fixture.rs"] -mod fixture; -#[path = "../test_support/dialer.rs"] -mod support; -mod tcp; diff --git a/core/crates/hub/peer/dialer/integration_tests/tcp.rs b/core/crates/hub/peer/dialer/integration_tests/tcp.rs deleted file mode 100644 index 7ddab1ce..00000000 --- a/core/crates/hub/peer/dialer/integration_tests/tcp.rs +++ /dev/null @@ -1,68 +0,0 @@ -use super::{constants::*, fixture::*, support::*}; -use std::{net::Ipv4Addr, num::NonZeroU16}; -use tokio::{ - io::AsyncReadExt, - net::TcpListener, - time::{Instant, timeout, timeout_at}, -}; -use yolo_hub_federation_settings::{PeerAddress, SshProfileRef}; -use yolo_hub_peer_dialer::{PeerDialer, TcpPeerDialer, dial_peer}; - -#[tokio::test] -async fn default_tcp_returns_owned_transport_whose_drop_closes_the_actual_socket() { - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, DYNAMIC_PORT)) - .await - .unwrap(); - let endpoint = listener.local_addr().unwrap(); - let address = PeerAddress::tcp( - endpoint.ip().to_string(), - NonZeroU16::new(endpoint.port()).unwrap(), - ); - let peer = keys().0; - let until = deadline(); - let (transport, accepted) = tokio::join!( - dial_peer(&TcpPeerDialer, peer, &address, until), - timeout_at(until, listener.accept()) - ); - let transport = transport.unwrap(); - let (mut remote, _) = accepted.unwrap().unwrap(); - drop(transport); - assert!( - timeout_at(deadline(), remote.read_u8()) - .await - .unwrap() - .is_err() - ); -} - -#[tokio::test] -async fn unsupported_ssh_or_failed_injected_binding_never_falls_back_to_tcp() { - let listener = TcpListener::bind((Ipv4Addr::LOCALHOST, DYNAMIC_PORT)) - .await - .unwrap(); - let endpoint = listener.local_addr().unwrap(); - let peer = keys().0; - let address = PeerAddress::ssh( - SshProfileRef::new(PROFILE, PROFILE_REVISION).unwrap(), - endpoint, - ) - .unwrap(); - assert!( - dial_peer(&TcpPeerDialer, peer, &address, deadline()) - .await - .is_err() - ); - let unavailable = Injected::new(None, Behavior::Reject); - assert!( - dial_peer(&unavailable, peer, &address, deadline()) - .await - .is_err() - ); - assert!(timeout(SHORT_TIMEOUT, listener.accept()).await.is_err()); - assert!( - TcpPeerDialer - .dial(peer, &super::fixture::address(), Instant::now()) - .await - .is_err() - ); -} diff --git a/core/crates/hub/peer/dialer/src/composite.rs b/core/crates/hub/peer/dialer/src/composite.rs new file mode 100644 index 00000000..5525787c --- /dev/null +++ b/core/crates/hub/peer/dialer/src/composite.rs @@ -0,0 +1,32 @@ +use crate::{PeerDialFuture, PeerDialer, SystemSshDialer}; +use std::sync::Arc; +use tokio::time::Instant; +use yolo_hub_delegation::PeerIdentity; +use yolo_hub_federation_settings::PeerAddress; + +/// Dispatches user SSH links to the local `ssh` client and leaves managed +/// remote-setup profile links on the Hub broker transport. +pub struct CompositeDialer { + ssh: SystemSshDialer, + profile: Arc, +} + +impl CompositeDialer { + pub fn new(ssh: SystemSshDialer, profile: Arc) -> Self { + Self { ssh, profile } + } +} + +impl PeerDialer for CompositeDialer { + fn dial<'a>( + &'a self, + peer: PeerIdentity, + address: &'a PeerAddress, + deadline: Instant, + ) -> PeerDialFuture<'a> { + match address { + PeerAddress::Ssh(_) => self.ssh.dial(peer, address, deadline), + PeerAddress::Profile(_) => self.profile.dial(peer, address, deadline), + } + } +} diff --git a/core/crates/hub/peer/dialer/src/constants.rs b/core/crates/hub/peer/dialer/src/constants.rs index 6f0ed5c8..9dc91cc1 100644 --- a/core/crates/hub/peer/dialer/src/constants.rs +++ b/core/crates/hub/peer/dialer/src/constants.rs @@ -1,3 +1,18 @@ +use std::time::Duration; + pub(crate) const DEADLINE_EXPIRED: &str = "Hub peer dial deadline expired"; -pub(crate) const UNSUPPORTED_TRANSPORT: &str = - "configured Hub peer transport requires an injected dialer"; +pub(crate) const SSH_PROGRAM: &str = "ssh"; +pub const PEER_STDIO_COMMAND: &str = "internal hub"; +pub const PEER_STDIO_FLAG: &str = "--peer-stdio"; +pub const PEER_SOCKET_FLAG: &str = "--peer-socket"; +pub(crate) const REMOTE_YOLO: &str = "yolo"; +pub(crate) const REMOTE_YOLO_FALLBACK: &str = "$HOME/.local/bin/yolo"; +pub(crate) const SSH_SPAWN_FAILED: &str = "start the SSH peer transport"; +pub(crate) const SSH_TARGET_MISSING: &str = "peer address is not an SSH destination"; +pub(crate) const SSH_STDIO_UNAVAILABLE: &str = "SSH peer transport has no stdio pipes"; +pub(crate) const SSH_FAILED: &str = "SSH peer transport failed"; +pub(crate) const MAX_STDERR_BYTES: usize = 4_096; +pub(crate) const SHUTDOWN_TIMEOUT: Duration = Duration::from_secs(5); +pub(crate) const SSH_CONNECT_TIMEOUT_SECONDS: u64 = 10; +pub(crate) const SSH_ALIVE_INTERVAL_SECONDS: u64 = 15; +pub(crate) const SSH_ALIVE_COUNT_MAX: u64 = 3; diff --git a/core/crates/hub/peer/dialer/src/lib.rs b/core/crates/hub/peer/dialer/src/lib.rs index cacba74f..ee54ab62 100644 --- a/core/crates/hub/peer/dialer/src/lib.rs +++ b/core/crates/hub/peer/dialer/src/lib.rs @@ -1,7 +1,11 @@ //! Transport injection only. Federation always performs its own mutually pinned handshake. +mod composite; mod constants; -mod tcp; -pub use tcp::TcpPeerDialer; +mod ssh; +pub use composite::CompositeDialer; +pub use constants::{PEER_SOCKET_FLAG, PEER_STDIO_COMMAND, PEER_STDIO_FLAG}; +pub use ssh::SystemSshDialer; +pub use yolo_hub_peer_io::PeerIo; use anyhow::{Context, Result, ensure}; use constants::*; @@ -9,7 +13,6 @@ use std::{future::Future, pin::Pin}; use tokio::time::{Instant, timeout_at}; use yolo_hub_delegation::PeerIdentity; use yolo_hub_federation_settings::PeerAddress; -use yolo_hub_peer_transport::PeerIo; pub type PeerDialFuture<'a> = Pin> + Send + 'a>>; @@ -27,7 +30,6 @@ pub trait PeerDialer: Send + Sync { } /// Enforces one absolute opening budget even when an injected factory ignores its deadline. -/// Failed custom transport selection never falls back to the default TCP implementation. pub async fn dial_peer( dialer: &dyn PeerDialer, peer: PeerIdentity, diff --git a/core/crates/hub/peer/dialer/src/ssh.rs b/core/crates/hub/peer/dialer/src/ssh.rs new file mode 100644 index 00000000..4e245560 --- /dev/null +++ b/core/crates/hub/peer/dialer/src/ssh.rs @@ -0,0 +1,133 @@ +//! Peer transport through the user's local OpenSSH client. +//! +//! The SSH child's stdin/stdout carry the peer byte stream. A hidden +//! `internal hub --peer-stdio` process on the provider connects that stream to +//! the Hub's private peer socket. No TCP listener, local forward or broker +//! profile is involved; `~/.ssh/config`, the agent and `known_hosts` remain the +//! user's own trust configuration. +use crate::constants::*; +use crate::{PeerDialFuture, PeerDialer}; +use anyhow::{Context, Result, ensure}; +use std::path::{Path, PathBuf}; +use std::process::{Command, Stdio}; +use tokio::io::{AsyncReadExt, join}; +use tokio::process::{ChildStderr, ChildStdin, ChildStdout}; +use tokio::sync::oneshot; +use tokio::time::Instant; +use yolo_hub_delegation::PeerIdentity; +use yolo_hub_federation_settings::PeerAddress; +use yolo_hub_peer_io::PeerIo; +use yolo_process::ScopedWaitableChild; + +pub struct SystemSshDialer { + program: PathBuf, +} + +impl Default for SystemSshDialer { + fn default() -> Self { + Self::new(SSH_PROGRAM) + } +} + +impl SystemSshDialer { + pub fn new(program: impl Into) -> Self { + Self { + program: program.into(), + } + } +} + +impl PeerDialer for SystemSshDialer { + fn dial<'a>( + &'a self, + _peer: PeerIdentity, + address: &'a PeerAddress, + deadline: Instant, + ) -> PeerDialFuture<'a> { + Box::pin(async move { + address.validate()?; + let target = address.ssh_target().context(SSH_TARGET_MISSING)?; + ensure!(Instant::now() < deadline, DEADLINE_EXPIRED); + let mut command = Command::new(&self.program); + command + .arg("-T") + .arg("-o") + .arg("BatchMode=yes") + .arg("-o") + .arg(format!("ConnectTimeout={SSH_CONNECT_TIMEOUT_SECONDS}")) + .arg("-o") + .arg(format!("ServerAliveInterval={SSH_ALIVE_INTERVAL_SECONDS}")) + .arg("-o") + .arg(format!("ServerAliveCountMax={SSH_ALIVE_COUNT_MAX}")) + .arg(target) + .arg(remote_command(address.socket())) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + let mut child = ScopedWaitableChild::spawn_before_deadline( + &mut command, + deadline.into_std(), + ) + .context(SSH_SPAWN_FAILED)?; + let stdin = stdio(child.take_stdin(), ChildStdin::from_std)?; + let stdout = stdio(child.take_stdout(), ChildStdout::from_std)?; + let stderr = stdio(child.take_stderr(), ChildStderr::from_std)?; + let (close, closed) = oneshot::channel(); + tokio::spawn(supervise(child, stderr, closed)); + Ok(PeerIo::new(join(stdout, stdin), move || { + let _ = close.send(()); + })) + }) + } +} + +fn stdio( + value: Option, + convert: impl FnOnce(S) -> std::io::Result, +) -> Result { + convert(value.ok_or_else(|| anyhow::anyhow!(SSH_STDIO_UNAVAILABLE))?) + .context(SSH_STDIO_UNAVAILABLE) +} + +fn remote_command(socket: &Path) -> String { + let socket = shell_quote(socket); + format!( + "if command -v {REMOTE_YOLO} >/dev/null 2>&1; then exec {REMOTE_YOLO} \ + {PEER_STDIO_COMMAND} {PEER_STDIO_FLAG} {PEER_SOCKET_FLAG} {socket}; else exec \ + \"{REMOTE_YOLO_FALLBACK}\" {PEER_STDIO_COMMAND} {PEER_STDIO_FLAG} \ + {PEER_SOCKET_FLAG} {socket}; fi" + ) +} + +fn shell_quote(path: &Path) -> String { + format!("'{}'", path.to_string_lossy().replace('\'', "'\\''")) +} + +async fn supervise( + mut child: ScopedWaitableChild, + stderr: ChildStderr, + closed: oneshot::Receiver<()>, +) { + let stderr = tokio::spawn(read_stderr(stderr)); + let _ = closed.await; + let status = child + .terminate_and_wait_status(std::time::Instant::now() + SHUTDOWN_TIMEOUT) + .await; + tracing::debug!(message = SSH_FAILED, ?status); + let detail = stderr.await.unwrap_or_default(); + if !detail.is_empty() { + tracing::warn!(message = SSH_FAILED, %detail); + } +} + +async fn read_stderr(mut stderr: ChildStderr) -> String { + let mut bytes = Vec::new(); + let mut chunk = [0_u8; 512]; + while bytes.len() < MAX_STDERR_BYTES { + match stderr.read(&mut chunk).await { + Ok(0) | Err(_) => break, + Ok(read) => bytes.extend_from_slice(&chunk[..read]), + } + } + String::from_utf8_lossy(&bytes).trim().to_owned() +} diff --git a/core/crates/hub/peer/dialer/src/tcp.rs b/core/crates/hub/peer/dialer/src/tcp.rs deleted file mode 100644 index 087d2379..00000000 --- a/core/crates/hub/peer/dialer/src/tcp.rs +++ /dev/null @@ -1,37 +0,0 @@ -use crate::{PeerDialFuture, PeerDialer, constants::UNSUPPORTED_TRANSPORT, require_budget}; -use anyhow::ensure; -use tokio::{ - net::TcpStream, - time::{Instant, timeout_at}, -}; -use yolo_hub_delegation::PeerIdentity; -use yolo_hub_federation_settings::{DialTransport, PeerAddress}; -use yolo_hub_peer_transport::PeerIo; - -#[derive(Debug, Default)] -pub struct TcpPeerDialer; - -impl PeerDialer for TcpPeerDialer { - fn dial<'a>( - &'a self, - _: PeerIdentity, - address: &'a PeerAddress, - deadline: Instant, - ) -> PeerDialFuture<'a> { - Box::pin(async move { - require_budget(deadline)?; - ensure!( - matches!(&address.transport, DialTransport::Tcp), - UNSUPPORTED_TRANSPORT - ); - address.validate()?; - let stream = timeout_at( - deadline, - TcpStream::connect((address.host.as_str(), address.port.get())), - ) - .await??; - require_budget(deadline)?; - PeerIo::from_tcp(stream).map_err(Into::into) - }) - } -} diff --git a/core/crates/hub/peer/dialer/test_support/constants.rs b/core/crates/hub/peer/dialer/test_support/constants.rs index 8cb62f90..01577e79 100644 --- a/core/crates/hub/peer/dialer/test_support/constants.rs +++ b/core/crates/hub/peer/dialer/test_support/constants.rs @@ -2,12 +2,8 @@ use std::time::Duration; pub const TIMEOUT: Duration = Duration::from_secs(5); pub const SHORT_TIMEOUT: Duration = Duration::from_millis(30); pub const BUFFER_BYTES: usize = 65_536; -pub const PORT: u16 = 43123; -pub const DYNAMIC_PORT: u16 = 0; -pub const HOST: &str = "factory-only.invalid"; +pub const SOCKET: &str = "/run/user/1000/yolo/hub/peer.sock"; pub const PAYLOAD: &[u8] = b"injected peer\0data"; pub const REJECTED: &str = "injected transport unavailable"; pub const ONE: usize = 1; pub const PROFILE: &str = "selected-profile"; -pub const PROFILE_REVISION: &str = - "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; diff --git a/core/crates/hub/peer/dialer/test_support/dialer.rs b/core/crates/hub/peer/dialer/test_support/dialer.rs index e38d4b5e..9e66d534 100644 --- a/core/crates/hub/peer/dialer/test_support/dialer.rs +++ b/core/crates/hub/peer/dialer/test_support/dialer.rs @@ -7,7 +7,7 @@ use tokio::{sync::Notify, time::Instant}; use yolo_hub_delegation::PeerIdentity; use yolo_hub_federation_settings::PeerAddress; use yolo_hub_peer_dialer::{PeerDialFuture, PeerDialer}; -use yolo_hub_peer_transport::PeerIo; +use yolo_hub_peer_dialer::PeerIo; #[derive(Clone, Copy)] pub enum Behavior { diff --git a/core/crates/hub/peer/dialer/test_support/fixture.rs b/core/crates/hub/peer/dialer/test_support/fixture.rs index 5a1bfd51..3d443f25 100644 --- a/core/crates/hub/peer/dialer/test_support/fixture.rs +++ b/core/crates/hub/peer/dialer/test_support/fixture.rs @@ -1,27 +1,21 @@ use super::constants::*; use ring::signature::{Ed25519KeyPair, KeyPair}; -use russh::keys::{PrivateKey, pkcs8::decode_pkcs8}; -use std::{num::NonZeroU16, sync::Arc}; use tokio::time::Instant; use yolo_hub_delegation::PeerIdentity; use yolo_hub_federation_settings::PeerAddress; use yolo_hub_identity::HubId; -pub fn keys() -> (PeerIdentity, Arc) { +pub fn identity() -> PeerIdentity { let document = Ed25519KeyPair::generate_pkcs8(&ring::rand::SystemRandom::new()).unwrap(); let signing = Ed25519KeyPair::from_pkcs8(document.as_ref()).unwrap(); - let identity = PeerIdentity::new( + PeerIdentity::new( HubId::new(), signing.public_key().as_ref().try_into().unwrap(), ) - .unwrap(); - ( - identity, - Arc::new(decode_pkcs8(document.as_ref(), None).unwrap()), - ) + .unwrap() } pub fn address() -> PeerAddress { - PeerAddress::tcp(HOST, NonZeroU16::new(PORT).unwrap()) + PeerAddress::ssh(PROFILE, SOCKET.into()).unwrap() } pub fn deadline() -> Instant { Instant::now() + TIMEOUT diff --git a/core/crates/hub/peer/dialer/unit_tests/deadline.rs b/core/crates/hub/peer/dialer/unit_tests/deadline.rs index a03dc278..4cba6f8d 100644 --- a/core/crates/hub/peer/dialer/unit_tests/deadline.rs +++ b/core/crates/hub/peer/dialer/unit_tests/deadline.rs @@ -5,7 +5,7 @@ use std::sync::{ }; use tokio::time::{Instant, timeout_at}; use yolo_hub_peer_dialer::dial_peer; -use yolo_hub_peer_transport::PeerIo; +use yolo_hub_peer_dialer::PeerIo; #[tokio::test] async fn deadline_and_full_configured_identity_are_preserved_without_authentication_in_factory() { @@ -18,7 +18,7 @@ async fn deadline_and_full_configured_identity_are_preserved_without_authenticat })), Behavior::Ready, ); - let peer = keys().0; + let peer = identity(); let address = address(); let deadline = deadline(); let transport = dial_peer(&factory, peer, &address, deadline).await.unwrap(); @@ -32,21 +32,14 @@ async fn deadline_and_full_configured_identity_are_preserved_without_authenticat } #[tokio::test] -async fn invalid_input_or_expired_budget_never_polls_factory() { +async fn expired_budget_never_polls_factory() { let factory = Injected::new(None, Behavior::Reject); - let peer = keys().0; + let peer = identity(); assert!( dial_peer(&factory, peer, &address(), Instant::now()) .await .is_err() ); - let mut invalid = address(); - invalid.host.clear(); - assert!( - dial_peer(&factory, peer, &invalid, deadline()) - .await - .is_err() - ); assert_eq!(factory.calls.load(Ordering::SeqCst), 0); } @@ -64,7 +57,7 @@ async fn pending_factory_is_cancelled_at_the_shared_absolute_deadline_and_releas assert!( dial_peer( &factory, - keys().0, + identity(), &address(), Instant::now() + SHORT_TIMEOUT ) @@ -87,7 +80,7 @@ async fn caller_cancellation_drops_the_inflight_factory_transport_without_detach Behavior::Pending, )); let opening_factory = factory.clone(); - let peer = keys().0; + let peer = identity(); let opening = tokio::spawn(async move { dial_peer(opening_factory.as_ref(), peer, &address(), deadline()).await }); diff --git a/core/crates/hub/peer/dialer/unit_tests/lib.rs b/core/crates/hub/peer/dialer/unit_tests/lib.rs index bb645dd1..9651ac25 100644 --- a/core/crates/hub/peer/dialer/unit_tests/lib.rs +++ b/core/crates/hub/peer/dialer/unit_tests/lib.rs @@ -5,3 +5,4 @@ mod deadline; mod fixture; #[path = "../test_support/dialer.rs"] mod support; +mod ssh; diff --git a/core/crates/hub/peer/dialer/unit_tests/ssh.rs b/core/crates/hub/peer/dialer/unit_tests/ssh.rs new file mode 100644 index 00000000..0aead400 --- /dev/null +++ b/core/crates/hub/peer/dialer/unit_tests/ssh.rs @@ -0,0 +1,52 @@ +use super::constants::SOCKET; +use super::fixture::{deadline, identity}; +use std::os::unix::fs::PermissionsExt; +use tempfile::tempdir; +use tokio::io::{AsyncReadExt, AsyncWriteExt}; +use yolo_hub_federation_settings::PeerAddress; +use yolo_hub_peer_dialer::{PEER_SOCKET_FLAG, PEER_STDIO_FLAG, SystemSshDialer, dial_peer}; + +#[tokio::test] +async fn ssh_child_stdio_carries_one_peer_stream() { + let directory = tempdir().unwrap(); + let program = directory.path().join("ssh"); + let args = directory.path().join("args"); + std::fs::write( + &program, + format!( + "#!/bin/sh\nprintf '%s\\n' \"$@\" > '{}'\nexec cat\n", + args.display() + ), + ) + .unwrap(); + std::fs::set_permissions(&program, std::fs::Permissions::from_mode(0o755)).unwrap(); + let address = PeerAddress::ssh("test-target", SOCKET.into()).unwrap(); + let dialer = SystemSshDialer::new(&program); + let mut io = dial_peer(&dialer, identity(), &address, deadline()) + .await + .unwrap(); + io.write_all(b"hello").await.unwrap(); + let mut reply = [0; 5]; + io.read_exact(&mut reply).await.unwrap(); + assert_eq!(&reply, b"hello"); + drop(io); + let recorded = std::fs::read_to_string(&args).unwrap(); + assert!(recorded.contains(PEER_STDIO_FLAG)); + assert!(recorded.contains(PEER_SOCKET_FLAG)); + assert!(recorded.contains(SOCKET)); +} + +#[tokio::test] +async fn a_managed_profile_dial_is_not_accepted_by_the_ssh_dialer() { + let address = PeerAddress::profile("managed", SOCKET.into()).unwrap(); + assert!( + dial_peer( + &SystemSshDialer::new("ssh"), + identity(), + &address, + deadline() + ) + .await + .is_err() + ); +} diff --git a/core/crates/hub/peer/io/BUILD.bazel b/core/crates/hub/peer/io/BUILD.bazel new file mode 100644 index 00000000..7e660eb5 --- /dev/null +++ b/core/crates/hub/peer/io/BUILD.bazel @@ -0,0 +1,38 @@ +load("@rules_rust//rust:defs.bzl", "rust_library", "rust_test") +load("//build_system/bazel:version.bzl", "YOLO_MARKETING_VERSION") + +package(default_visibility = ["//visibility:public"]) + +_DEPS = [ + "@crates//:tokio", +] + +filegroup( + name = "architecture_check_data", + srcs = glob( + [ + "src/**/*.rs", + "unit_tests/**/*.rs", + ], + allow_empty = True, + ) + ["BUILD.bazel"], +) + +rust_library( + name = "yolo_hub_peer_io", + srcs = glob(["src/**/*.rs"]), + crate_name = "yolo_hub_peer_io", + crate_root = "src/lib.rs", + edition = "2024", + version = YOLO_MARKETING_VERSION, + deps = _DEPS, +) + +rust_test( + name = "unit_tests", + srcs = glob(["src/**/*.rs", "unit_tests/**/*.rs"]), + crate_name = "yolo_hub_peer_io_unit_tests", + crate_root = "src/lib.rs", + edition = "2024", + deps = _DEPS, +) diff --git a/core/crates/hub/peer/transport/src/io_custody.rs b/core/crates/hub/peer/io/src/custody.rs similarity index 100% rename from core/crates/hub/peer/transport/src/io_custody.rs rename to core/crates/hub/peer/io/src/custody.rs diff --git a/core/crates/hub/peer/transport/src/io.rs b/core/crates/hub/peer/io/src/io.rs similarity index 69% rename from core/crates/hub/peer/transport/src/io.rs rename to core/crates/hub/peer/io/src/io.rs index 59328a89..308ff36a 100644 --- a/core/crates/hub/peer/transport/src/io.rs +++ b/core/crates/hub/peer/io/src/io.rs @@ -1,7 +1,4 @@ -use crate::{ - Duplex, - io_custody::{CloseState, IoCustody}, -}; +use crate::custody::{CloseState, IoCustody}; use std::{ io, pin::Pin, @@ -12,6 +9,10 @@ use tokio::io::{AsyncRead, AsyncWrite, ReadBuf}; const CLOSED_IO: &str = "Hub peer transport was closed"; +/// Any byte stream that can carry one peer link. +pub trait Duplex: AsyncRead + AsyncWrite + Unpin + Send {} +impl Duplex for T {} + /// One transport and the authority to close it, independent of its concrete I/O. pub struct PeerIo { stream: Box, @@ -19,11 +20,6 @@ pub struct PeerIo { } impl PeerIo { - /// Retains the existing duplicate-socket close authority for TCP transports. - pub fn from_tcp(stream: tokio::net::TcpStream) -> io::Result { - crate::socket::SocketOwner::capture(stream) - } - /// `close_once` must be nonblocking and must close only this transport. /// It runs once on explicit close, handshake failure/cancellation, or drop. pub fn new( @@ -40,8 +36,34 @@ impl PeerIo { } } - pub(crate) fn into_parts(self) -> (Box, IoCustody) { - (self.stream, self.custody) + pub fn close(&self) { + self.custody.close(); + } +} + +impl AsyncRead for PeerIo { + fn poll_read( + mut self: Pin<&mut Self>, + context: &mut Context<'_>, + output: &mut ReadBuf<'_>, + ) -> Poll> { + Pin::new(&mut self.stream).poll_read(context, output) + } +} + +impl AsyncWrite for PeerIo { + fn poll_write( + mut self: Pin<&mut Self>, + context: &mut Context<'_>, + bytes: &[u8], + ) -> Poll> { + Pin::new(&mut self.stream).poll_write(context, bytes) + } + fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.stream).poll_flush(context) + } + fn poll_shutdown(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll> { + Pin::new(&mut self.stream).poll_shutdown(context) } } diff --git a/core/crates/hub/peer/io/src/lib.rs b/core/crates/hub/peer/io/src/lib.rs new file mode 100644 index 00000000..e807bce7 --- /dev/null +++ b/core/crates/hub/peer/io/src/lib.rs @@ -0,0 +1,9 @@ +//! One peer byte stream plus the authority to close its transport. +mod custody; +mod io; + +pub use io::{Duplex, PeerIo}; + +#[cfg(test)] +#[path = "../unit_tests/lib.rs"] +mod unit_tests; diff --git a/core/crates/hub/peer/io/unit_tests/lib.rs b/core/crates/hub/peer/io/unit_tests/lib.rs new file mode 100644 index 00000000..0e1c4918 --- /dev/null +++ b/core/crates/hub/peer/io/unit_tests/lib.rs @@ -0,0 +1,33 @@ +use crate::PeerIo; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; +use tokio::io::AsyncWriteExt; + +const ONE: usize = 1; +const BUFFER_BYTES: usize = 64; + +#[tokio::test] +async fn drop_and_explicit_close_run_the_transport_close_exactly_once() { + let closes = Arc::new(AtomicUsize::default()); + let observed = closes.clone(); + let (stream, _remote) = tokio::io::duplex(BUFFER_BYTES); + let io = PeerIo::new(stream, move || { + observed.fetch_add(ONE, Ordering::SeqCst); + }); + io.close(); + io.close(); + assert_eq!(closes.load(Ordering::SeqCst), ONE); + drop(io); + assert_eq!(closes.load(Ordering::SeqCst), ONE); +} + +#[tokio::test] +async fn a_closed_transport_refuses_writes() { + let (stream, _remote) = tokio::io::duplex(BUFFER_BYTES); + let io = PeerIo::new(stream, || {}); + io.close(); + let mut io = io; + assert!(io.write_all(b"x").await.is_err()); +} diff --git a/core/crates/hub/peer/transport/BUILD.bazel b/core/crates/hub/peer/link/BUILD.bazel similarity index 53% rename from core/crates/hub/peer/transport/BUILD.bazel rename to core/crates/hub/peer/link/BUILD.bazel index e4e5ee38..15844bb4 100644 --- a/core/crates/hub/peer/transport/BUILD.bazel +++ b/core/crates/hub/peer/link/BUILD.bazel @@ -5,9 +5,15 @@ package(default_visibility = ["//visibility:public"]) _DEPS = [ "//core/crates/hub/delegation:yolo_hub_delegation", - "@crates//:russh", + "//core/crates/hub/identity:yolo_hub_identity", + "//core/crates/hub/peer/io:yolo_hub_peer_io", + "//core/crates/hub/peer/rpc:rpc", + "//core/crates/transport/capnp/codec:codec", + "@crates//:ring", + "@crates//:serde", + "@crates//:serde_bytes", + "@crates//:thiserror", "@crates//:tokio", - "@crates//:anyhow", ] filegroup( @@ -16,17 +22,15 @@ filegroup( [ "src/**/*.rs", "unit_tests/**/*.rs", - "integration_tests/**/*.rs", - "test_support/**/*.rs", ], allow_empty = True, ) + ["BUILD.bazel"], ) rust_library( - name = "yolo_hub_peer_transport", + name = "yolo_hub_peer_link", srcs = glob(["src/**/*.rs"]), - crate_name = "yolo_hub_peer_transport", + crate_name = "yolo_hub_peer_link", crate_root = "src/lib.rs", edition = "2024", version = YOLO_MARKETING_VERSION, @@ -36,27 +40,10 @@ rust_library( rust_test( name = "unit_tests", srcs = glob(["src/**/*.rs", "unit_tests/**/*.rs"]), - crate_name = "yolo_hub_peer_transport_unit_tests", + crate_name = "yolo_hub_peer_link_unit_tests", crate_root = "src/lib.rs", edition = "2024", - deps = _DEPS, -) - -rust_test( - name = "integration_tests", - srcs = glob( - [ - "integration_tests/**/*.rs", - "test_support/**/*.rs", - ], - allow_empty = True, - ), - crate_name = "yolo_hub_peer_transport_integration_tests", - crate_root = "integration_tests/lib.rs", - edition = "2024", deps = _DEPS + [ - ":yolo_hub_peer_transport", - "//core/crates/hub/identity:yolo_hub_identity", - "@crates//:ring", + "//core/crates/hub/delegation:test_support", ], ) diff --git a/core/crates/hub/peer/link/src/frame.rs b/core/crates/hub/peer/link/src/frame.rs new file mode 100644 index 00000000..a51d2910 --- /dev/null +++ b/core/crates/hub/peer/link/src/frame.rs @@ -0,0 +1,49 @@ +use crate::LinkError; +use serde::{Serialize, de::DeserializeOwned}; +use tokio::{ + io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt}, + time::{Instant, timeout_at}, +}; +use yolo_capnp_codec::{decode, encode}; + +const MAX_HANDSHAKE_BYTES: usize = 4 * 1024; + +pub(crate) async fn read( + stream: &mut (impl AsyncRead + Unpin), + deadline: Instant, +) -> Result { + let length = timeout_at(deadline, stream.read_u32()) + .await + .map_err(|_| LinkError::Deadline)? + .map_err(|_| LinkError::Frame)?; + let length = usize::try_from(length).map_err(|_| LinkError::Frame)?; + if length == 0 || length > MAX_HANDSHAKE_BYTES { + return Err(LinkError::Frame); + } + let mut bytes = vec![0; length]; + timeout_at(deadline, stream.read_exact(&mut bytes)) + .await + .map_err(|_| LinkError::Deadline)? + .map_err(|_| LinkError::Frame)?; + decode(&bytes).map_err(|_| LinkError::Frame) +} + +pub(crate) async fn write( + stream: &mut (impl AsyncWrite + Unpin), + value: &T, + deadline: Instant, +) -> Result<(), LinkError> { + let bytes = encode(value).map_err(|_| LinkError::Frame)?; + if bytes.is_empty() || bytes.len() > MAX_HANDSHAKE_BYTES { + return Err(LinkError::Frame); + } + let length = u32::try_from(bytes.len()).map_err(|_| LinkError::Frame)?; + timeout_at(deadline, async { + stream.write_u32(length).await?; + stream.write_all(&bytes).await?; + stream.flush().await + }) + .await + .map_err(|_| LinkError::Deadline)? + .map_err(|_| LinkError::Frame) +} diff --git a/core/crates/hub/peer/link/src/handshake.rs b/core/crates/hub/peer/link/src/handshake.rs new file mode 100644 index 00000000..cafe6347 --- /dev/null +++ b/core/crates/hub/peer/link/src/handshake.rs @@ -0,0 +1,70 @@ +use crate::LinkError; +use crate::frame; +use crate::message::{Hello, NONCE_BYTES, ROLE_CLIENT, ROLE_SERVER}; +use crate::Admission; +use ring::rand::{SecureRandom, SystemRandom}; +use tokio::{ + io::{AsyncRead, AsyncWrite}, + time::Instant, +}; +use yolo_hub_delegation::{PeerIdentity, Signer}; + +pub(crate) async fn client( + stream: &mut (impl AsyncRead + AsyncWrite + Unpin), + local: PeerIdentity, + signer: &impl Signer, + expected_remote: PeerIdentity, + deadline: Instant, +) -> Result<(), LinkError> { + if signer.identity() != local { + return Err(LinkError::Identity); + } + let nonce = random_nonce()?; + frame::write( + stream, + &Hello::new(ROLE_CLIENT, local, nonce, None, signer)?, + deadline, + ) + .await?; + let reply: Hello = frame::read(stream, deadline).await?; + let remote = reply.verify_signature(ROLE_SERVER, Some(nonce))?; + if remote != expected_remote { + return Err(LinkError::Identity); + } + Ok(()) +} + +pub(crate) async fn server( + stream: &mut (impl AsyncRead + AsyncWrite + Unpin), + local: PeerIdentity, + signer: &impl Signer, + admission: Admission<'_>, + deadline: Instant, +) -> Result { + if signer.identity() != local { + return Err(LinkError::Identity); + } + let hello: Hello = frame::read(stream, deadline).await?; + let peer = hello.verify_signature(ROLE_CLIENT, None)?; + if let Admission::Only(allowed_clients) = admission { + if !allowed_clients.contains(&peer) { + return Err(LinkError::Identity); + } + } + let nonce = random_nonce()?; + frame::write( + stream, + &Hello::new(ROLE_SERVER, local, nonce, Some(hello.nonce()?), signer)?, + deadline, + ) + .await?; + Ok(peer) +} + +fn random_nonce() -> Result<[u8; NONCE_BYTES], LinkError> { + let mut nonce = [0; NONCE_BYTES]; + SystemRandom::new() + .fill(&mut nonce) + .map_err(|_| LinkError::Signature)?; + Ok(nonce) +} diff --git a/core/crates/hub/peer/link/src/lib.rs b/core/crates/hub/peer/link/src/lib.rs new file mode 100644 index 00000000..6b5d387b --- /dev/null +++ b/core/crates/hub/peer/link/src/lib.rs @@ -0,0 +1,67 @@ +//! One SSH-streamlocal byte stream carrying one peer RPC link. +mod frame; +mod handshake; +mod message; + +pub use yolo_hub_peer_io::Duplex; +use tokio::time::Instant; +use yolo_hub_delegation::{PeerIdentity, Signer}; +use yolo_hub_peer_rpc::{Client, RpcError}; + +#[derive(Debug, thiserror::Error)] +pub enum LinkError { + #[error("peer link handshake frame is invalid")] + Frame, + #[error("peer link handshake version is unsupported")] + Version, + #[error("peer link handshake role is invalid")] + Role, + #[error("peer link identity is not the expected peer")] + Identity, + #[error("peer link signature is invalid")] + Signature, + #[error("peer link handshake timed out")] + Deadline, + #[error(transparent)] + Rpc(#[from] RpcError), +} + +/// Runs the client hello and returns the multiplexed RPC client for this link. +pub async fn connect( + mut stream: S, + local: PeerIdentity, + signer: &impl Signer, + expected_remote: PeerIdentity, + deadline: Instant, +) -> Result { + handshake::client(&mut stream, local, signer, expected_remote, deadline).await?; + Client::connect(stream, deadline).await.map_err(Into::into) +} + +/// Which peer identities an inbound link may present. +/// +/// `Any` is the SSH-trusted default: the provider's peer socket is reachable +/// only by its own user, so the SSH account is the admission boundary. `Only` +/// is the strict mode that requires an explicit local pin. +#[derive(Clone, Copy)] +pub enum Admission<'a> { + Any, + Only(&'a [PeerIdentity]), +} + +/// Verifies the client hello against the configured peers and returns the +/// authenticated identity plus the stream for `yolo_hub_peer_rpc::serve`. +pub async fn accept( + mut stream: S, + local: PeerIdentity, + signer: &impl Signer, + admission: Admission<'_>, + deadline: Instant, +) -> Result<(PeerIdentity, S), LinkError> { + let peer = handshake::server(&mut stream, local, signer, admission, deadline).await?; + Ok((peer, stream)) +} + +#[cfg(test)] +#[path = "../unit_tests/lib.rs"] +mod unit_tests; diff --git a/core/crates/hub/peer/link/src/message.rs b/core/crates/hub/peer/link/src/message.rs new file mode 100644 index 00000000..71c07f4f --- /dev/null +++ b/core/crates/hub/peer/link/src/message.rs @@ -0,0 +1,146 @@ +use crate::LinkError; +use serde::{Deserialize, Serialize}; +use yolo_hub_delegation::{PeerIdentity, Signer}; +use yolo_hub_identity::HubId; + +pub(crate) const NONCE_BYTES: usize = 32; +pub(crate) const ROLE_CLIENT: &str = "client"; +pub(crate) const ROLE_SERVER: &str = "server"; +const HELLO_VERSION: u64 = 1; +const SIGNATURE_BYTES: usize = 64; +const DOMAIN: &str = "yolo.hub.peer.hello.v1"; + +#[derive(Serialize, Deserialize)] +struct WireIdentity { + hub: HubId, + #[serde(with = "serde_bytes")] + public_key: Vec, +} + +impl WireIdentity { + fn new(identity: PeerIdentity) -> Self { + Self { + hub: identity.hub(), + public_key: identity.key().to_vec(), + } + } + + fn identity(&self) -> Result { + let key: [u8; 32] = self + .public_key + .as_slice() + .try_into() + .map_err(|_| LinkError::Identity)?; + PeerIdentity::new(self.hub, key).map_err(|_| LinkError::Identity) + } +} + +#[derive(Serialize, Deserialize)] +pub(crate) struct Hello { + version: u64, + role: String, + identity: WireIdentity, + #[serde(with = "serde_bytes")] + nonce: Vec, + #[serde(with = "serde_bytes")] + peer_nonce: Vec, + #[serde(with = "serde_bytes")] + signature: Vec, +} + +#[derive(Serialize)] +struct Signable<'a> { + domain: &'static str, + version: u64, + role: &'a str, + identity: &'a WireIdentity, + #[serde(with = "serde_bytes")] + nonce: &'a [u8], + #[serde(with = "serde_bytes")] + peer_nonce: &'a [u8], +} + +impl Hello { + pub(crate) fn new( + role: &str, + identity: PeerIdentity, + nonce: [u8; NONCE_BYTES], + peer_nonce: Option<[u8; NONCE_BYTES]>, + signer: &impl Signer, + ) -> Result { + if signer.identity() != identity { + return Err(LinkError::Identity); + } + let wire = WireIdentity::new(identity); + let peer_nonce = peer_nonce.map_or_else(Vec::new, |nonce| nonce.to_vec()); + let signature = signer + .sign(&signing_bytes(role, &wire, &nonce, &peer_nonce)?) + .map_err(|_| LinkError::Signature)? + .to_vec(); + Ok(Self { + version: HELLO_VERSION, + role: role.into(), + identity: wire, + nonce: nonce.to_vec(), + peer_nonce, + signature, + }) + } + + /// Verifies the signature with the identity presented in the hello and + /// returns it. Callers then decide whether that identity is acceptable. + pub(crate) fn verify_signature( + &self, + role: &str, + peer_nonce: Option<[u8; NONCE_BYTES]>, + ) -> Result { + if self.version != HELLO_VERSION { + return Err(LinkError::Version); + } + if self.role != role { + return Err(LinkError::Role); + } + let identity = self.identity.identity()?; + let nonce = self.nonce()?; + let expected = peer_nonce.map_or_else(Vec::new, |nonce| nonce.to_vec()); + if self.peer_nonce != expected { + return Err(LinkError::Identity); + } + if self.signature.len() != SIGNATURE_BYTES { + return Err(LinkError::Signature); + } + identity + .verify( + &signing_bytes(&self.role, &self.identity, &nonce, &self.peer_nonce)?, + &self.signature, + ) + .map_err(|_| LinkError::Signature)?; + Ok(identity) + } + + pub(crate) fn nonce(&self) -> Result<[u8; NONCE_BYTES], LinkError> { + self.nonce + .as_slice() + .try_into() + .map_err(|_| LinkError::Frame) + } + + +} + +fn signing_bytes( + role: &str, + identity: &WireIdentity, + nonce: &[u8], + peer_nonce: &[u8], +) -> Result, LinkError> { + yolo_capnp_codec::canonical(&Signable { + domain: DOMAIN, + version: HELLO_VERSION, + role, + identity, + nonce, + peer_nonce, + }) + .map_err(|_| LinkError::Frame) +} diff --git a/core/crates/hub/peer/link/unit_tests/lib.rs b/core/crates/hub/peer/link/unit_tests/lib.rs new file mode 100644 index 00000000..2889e613 --- /dev/null +++ b/core/crates/hub/peer/link/unit_tests/lib.rs @@ -0,0 +1,154 @@ +use crate::handshake; +use crate::{Admission, LinkError}; +use std::time::Duration; +use tokio::io::{AsyncReadExt, AsyncWriteExt, duplex}; +use tokio::time::Instant; +use yolo_hub_delegation_test_support::Identity; + +const TIMEOUT_SECS: u64 = 1; + +fn deadline() -> Instant { + Instant::now() + Duration::from_secs(TIMEOUT_SECS) +} + +#[tokio::test] +async fn client_and_server_bind_the_expected_identities() { + let client = Identity::new(); + let server = Identity::new(); + let (mut client_stream, mut server_stream) = duplex(64 * 1024); + let expected_client = client.identity; + let expected_server = server.identity; + let server_task = tokio::spawn(async move { + let peer = handshake::server( + &mut server_stream, + server.identity, + &server, + Admission::Only(&[expected_client]), + deadline(), + ) + .await + .unwrap(); + assert_eq!(peer, expected_client); + server_stream.write_all(b"ok").await.unwrap(); + }); + handshake::client( + &mut client_stream, + client.identity, + &client, + expected_server, + deadline(), + ) + .await + .unwrap(); + let mut reply = [0; 2]; + client_stream.read_exact(&mut reply).await.unwrap(); + assert_eq!(&reply, b"ok"); + server_task.await.unwrap(); +} + +#[tokio::test] +async fn a_wrong_server_identity_is_rejected() { + let client = Identity::new(); + let server = Identity::new(); + let stranger = Identity::new(); + let (mut client_stream, mut server_stream) = duplex(64 * 1024); + let expected_client = client.identity; + tokio::spawn(async move { + let _ = handshake::server( + &mut server_stream, + server.identity, + &server, + Admission::Only(&[expected_client]), + deadline(), + ) + .await; + }); + let error = handshake::client( + &mut client_stream, + client.identity, + &client, + stranger.identity, + deadline(), + ) + .await + .unwrap_err(); + assert!(matches!(error, LinkError::Identity), "{error}"); +} + +#[tokio::test] +async fn a_wrong_client_identity_is_rejected() { + let client = Identity::new(); + let server = Identity::new(); + let stranger = Identity::new(); + let (mut client_stream, mut server_stream) = duplex(64 * 1024); + let expected_server = server.identity; + let client_task = tokio::spawn(async move { + let _ = handshake::client( + &mut client_stream, + client.identity, + &client, + expected_server, + deadline(), + ) + .await; + }); + let error = handshake::server( + &mut server_stream, + server.identity, + &server, + Admission::Only(&[stranger.identity]), + deadline(), + ) + .await + .unwrap_err(); + assert!(matches!(error, LinkError::Identity), "{error}"); + let _ = client_task.await; +} + +#[tokio::test] +async fn a_mismatched_identity_fails_before_the_hello() { + let client = Identity::new(); + let other = Identity::new(); + let (mut stream, _) = duplex(1024); + let error = handshake::client( + &mut stream, + client.identity, + &other, + client.identity, + deadline(), + ) + .await + .unwrap_err(); + assert!(matches!(error, LinkError::Identity), "{error}"); +} + +#[tokio::test] +async fn ssh_trusted_admission_accepts_an_unpinned_client_identity() { + let client = Identity::new(); + let server = Identity::new(); + let (mut client_stream, mut server_stream) = duplex(64 * 1024); + let expected_client = client.identity; + let expected_server = server.identity; + let server_task = tokio::spawn(async move { + let peer = handshake::server( + &mut server_stream, + server.identity, + &server, + Admission::Any, + deadline(), + ) + .await + .unwrap(); + assert_eq!(peer, expected_client); + }); + handshake::client( + &mut client_stream, + client.identity, + &client, + expected_server, + deadline(), + ) + .await + .unwrap(); + server_task.await.unwrap(); +} diff --git a/core/crates/hub/peer/rpc/src/lib.rs b/core/crates/hub/peer/rpc/src/lib.rs index 8ee399bf..e2ccd4ba 100644 --- a/core/crates/hub/peer/rpc/src/lib.rs +++ b/core/crates/hub/peer/rpc/src/lib.rs @@ -21,6 +21,7 @@ pub struct Received { pub reply: PeerReply, pub updates: Option, } +#[derive(Clone)] pub struct Client { connection: Connection, } @@ -34,6 +35,9 @@ impl Client { connection: Connection::open(stream, Protocol::Peer, deadline).await?, }) } + pub fn is_closed(&self) -> bool { + self.connection.is_closed() + } pub async fn request( &self, request: PeerRequest, diff --git a/core/crates/hub/peer/transport/integration_tests/channels.rs b/core/crates/hub/peer/transport/integration_tests/channels.rs deleted file mode 100644 index 5207f983..00000000 --- a/core/crates/hub/peer/transport/integration_tests/channels.rs +++ /dev/null @@ -1,67 +0,0 @@ -use crate::support::*; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, - time::timeout_at, -}; - -#[tokio::test] -async fn either_authenticated_endpoint_can_open_independent_channels() { - let ((client, mut client_incoming), (server, mut server_incoming)) = pair().await; - let mut outgoing = client.open_channel(deadline()).await.unwrap(); - let mut server_reader = timeout_at(deadline(), server_incoming.recv()) - .await - .unwrap() - .unwrap(); - let mut reverse = server.open_channel(deadline()).await.unwrap(); - let mut client_reader = timeout_at(deadline(), client_incoming.recv()) - .await - .unwrap() - .unwrap(); - outgoing.write_all(PAYLOAD).await.unwrap(); - reverse.write_all(PAYLOAD).await.unwrap(); - let mut observed = vec![u8::default(); PAYLOAD.len()]; - timeout_at(deadline(), server_reader.read_exact(&mut observed)) - .await - .unwrap() - .unwrap(); - assert_eq!(observed, PAYLOAD); - timeout_at(deadline(), client_reader.read_exact(&mut observed)) - .await - .unwrap() - .unwrap(); - assert_eq!(observed, PAYLOAD); - server_reader.write_all(PAYLOAD).await.unwrap(); - timeout_at(deadline(), outgoing.read_exact(&mut observed)) - .await - .unwrap() - .unwrap(); - assert_eq!(observed, PAYLOAD); - drop((outgoing, reverse, server_reader, client_reader)); - client.close(deadline()).await.unwrap(); - server.close(deadline()).await.unwrap(); - assert!(client.is_closed()); - assert!(client.open_channel(deadline()).await.is_err()); - assert!(server.open_channel(deadline()).await.is_err()); -} - -#[tokio::test] -async fn dropping_connection_retires_channels_and_notifies_the_other_endpoint() { - let ((client, _), (server, mut incoming)) = pair().await; - let outgoing = client.open_channel(deadline()).await.unwrap(); - let mut receiver = timeout_at(deadline(), incoming.recv()) - .await - .unwrap() - .unwrap(); - drop(client); - let result = timeout_at(deadline(), receiver.read_u8()).await.unwrap(); - assert!(result.is_err()); - timeout_at(deadline(), async { - while !server.is_closed() { - tokio::time::sleep(POLL_INTERVAL).await; - } - }) - .await - .unwrap(); - drop(outgoing); - server.close(deadline()).await.unwrap(); -} diff --git a/core/crates/hub/peer/transport/integration_tests/controlled.rs b/core/crates/hub/peer/transport/integration_tests/controlled.rs deleted file mode 100644 index fc65164c..00000000 --- a/core/crates/hub/peer/transport/integration_tests/controlled.rs +++ /dev/null @@ -1,131 +0,0 @@ -use crate::support::*; -use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, -}; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, - time::timeout_at, -}; -use yolo_hub_peer_transport::{PeerIo, PeerPins, accept_io, connect_io}; - -const BUFFER_BYTES: usize = 65_536; -const CLOSED_ONCE: usize = 1; - -fn owned(stream: tokio::io::DuplexStream, closed: Arc) -> PeerIo { - PeerIo::new(stream, move || { - closed.fetch_add(CLOSED_ONCE, Ordering::SeqCst); - }) -} - -#[tokio::test] -async fn generic_io_keeps_inner_auth_and_independent_channels_until_explicit_close() { - let (left_id, left_key) = keys(); - let (right_id, right_key) = keys(); - let (left, right) = tokio::io::duplex(BUFFER_BYTES); - let left_closed = Arc::new(AtomicUsize::default()); - let right_closed = Arc::new(AtomicUsize::default()); - let (left, right) = tokio::join!( - connect_io( - owned(left, left_closed.clone()), - left_key, - right_id, - deadline() - ), - accept_io( - owned(right, right_closed.clone()), - right_key, - PeerPins::new(vec![left_id]).unwrap(), - deadline() - ), - ); - let (left, _) = left.unwrap(); - let (right, mut incoming) = right.unwrap(); - let discarded = left.open_channel(deadline()).await.unwrap(); - let discarded_remote = timeout_at(deadline(), incoming.recv()) - .await - .unwrap() - .unwrap(); - drop((discarded, discarded_remote)); - let mut second = left.open_channel(deadline()).await.unwrap(); - let mut remote = timeout_at(deadline(), incoming.recv()) - .await - .unwrap() - .unwrap(); - second.write_all(PAYLOAD).await.unwrap(); - let mut received = vec![u8::default(); PAYLOAD.len()]; - timeout_at(deadline(), remote.read_exact(&mut received)) - .await - .unwrap() - .unwrap(); - assert_eq!(received, PAYLOAD); - assert_eq!(left_closed.load(Ordering::SeqCst), usize::default()); - left.close(deadline()).await.unwrap(); - assert!( - timeout_at(deadline(), remote.read_u8()) - .await - .unwrap() - .is_err() - ); - right.close(deadline()).await.unwrap(); - drop((left, right, second)); - assert_eq!(left_closed.load(Ordering::SeqCst), CLOSED_ONCE); - assert_eq!(right_closed.load(Ordering::SeqCst), CLOSED_ONCE); -} - -#[tokio::test] -async fn generic_io_rejects_wrong_inner_hub_pin_and_releases_both_owners() { - let (left_id, left_key) = keys(); - let (_, right_key) = keys(); - let (wrong_pin, _) = keys(); - let (left, right) = tokio::io::duplex(BUFFER_BYTES); - let left_closed = Arc::new(AtomicUsize::default()); - let right_closed = Arc::new(AtomicUsize::default()); - let (left, right) = tokio::join!( - connect_io( - owned(left, left_closed.clone()), - left_key, - wrong_pin, - deadline() - ), - accept_io( - owned(right, right_closed.clone()), - right_key, - PeerPins::new(vec![left_id]).unwrap(), - deadline() - ), - ); - assert!(left.is_err()); - assert!(right.is_err()); - assert_eq!(left_closed.load(Ordering::SeqCst), CLOSED_ONCE); - assert_eq!(right_closed.load(Ordering::SeqCst), CLOSED_ONCE); -} - -#[tokio::test] -async fn cancelling_an_incomplete_handshake_closes_its_transport_once() { - for accepting in [false, true] { - let (peer, key) = keys(); - let (stream, mut remote) = tokio::io::duplex(BUFFER_BYTES); - let closed = Arc::new(AtomicUsize::default()); - let io = owned(stream, closed.clone()); - let opening = tokio::spawn(async move { - if accepting { - accept_io(io, key, PeerPins::new(vec![peer]).unwrap(), deadline()).await - } else { - connect_io(io, key, peer, deadline()).await - } - }); - timeout_at(deadline(), remote.read_u8()) - .await - .unwrap() - .unwrap(); - opening.abort(); - assert!(matches!(opening.await, Err(error) if error.is_cancelled())); - assert_eq!(closed.load(Ordering::SeqCst), CLOSED_ONCE); - let mut remaining = Vec::new(); - timeout_at(deadline(), remote.read_to_end(&mut remaining)) - .await - .unwrap() - .unwrap(); - } -} diff --git a/core/crates/hub/peer/transport/integration_tests/lib.rs b/core/crates/hub/peer/transport/integration_tests/lib.rs deleted file mode 100644 index 8446d3b9..00000000 --- a/core/crates/hub/peer/transport/integration_tests/lib.rs +++ /dev/null @@ -1,5 +0,0 @@ -mod channels; -mod controlled; -mod rejection; -#[path = "../test_support/mod.rs"] -mod support; diff --git a/core/crates/hub/peer/transport/integration_tests/rejection.rs b/core/crates/hub/peer/transport/integration_tests/rejection.rs deleted file mode 100644 index 30d746c3..00000000 --- a/core/crates/hub/peer/transport/integration_tests/rejection.rs +++ /dev/null @@ -1,56 +0,0 @@ -use crate::support::*; -use yolo_hub_peer_transport::{PeerPins, accept, connect}; - -#[tokio::test] -async fn both_server_pin_and_client_identity_are_required() { - for wrong_server in [true, false] { - let (client_id, client_key) = keys(); - let (server_id, server_key) = keys(); - let (untrusted, _) = keys(); - let (client, server) = sockets().await; - let pins = PeerPins::new(vec![if wrong_server { client_id } else { untrusted }]).unwrap(); - let (connected, accepted) = tokio::join!( - connect( - client, - client_key, - if wrong_server { untrusted } else { server_id }, - deadline() - ), - accept(server, server_key, pins, deadline()), - ); - assert!(connected.is_err()); - assert!(accepted.is_err()); - } -} - -#[tokio::test] -async fn expired_handshake_deadline_does_not_leave_a_live_authenticated_channel() { - let (identity, key) = keys(); - let (client, server) = sockets().await; - let immediate = tokio::time::Instant::now(); - assert!( - connect(client, key.clone(), identity, immediate) - .await - .is_err() - ); - assert!( - accept( - server, - key, - PeerPins::new(vec![identity]).unwrap(), - immediate - ) - .await - .is_err() - ); -} - -#[test] -fn ambiguous_identity_or_public_key_enrollment_is_rejected() { - let (first, _) = keys(); - let (second, _) = keys(); - assert!(PeerPins::new(vec![first, second]).is_ok()); - assert!(PeerPins::new(vec![first, first]).is_err()); - let duplicate_key = yolo_hub_delegation::PeerIdentity::new(second.hub(), *first.key()).unwrap(); - assert!(PeerPins::new(vec![first, duplicate_key]).is_err()); -} diff --git a/core/crates/hub/peer/transport/src/admission.rs b/core/crates/hub/peer/transport/src/admission.rs deleted file mode 100644 index 331c3a43..00000000 --- a/core/crates/hub/peer/transport/src/admission.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::constants::{CHANNEL_LIMIT, CLOSED}; -use std::{error::Error, fmt}; -use tokio::sync::TryAcquireError; - -/// Local channel admission is distinct from a failed or rejected SSH channel. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] -pub enum ChannelAdmissionError { - AtCapacity, - Closed, -} - -impl From for ChannelAdmissionError { - fn from(error: TryAcquireError) -> Self { - match error { - TryAcquireError::NoPermits => Self::AtCapacity, - TryAcquireError::Closed => Self::Closed, - } - } -} - -impl fmt::Display for ChannelAdmissionError { - fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { - formatter.write_str(match self { - Self::AtCapacity => CHANNEL_LIMIT, - Self::Closed => CLOSED, - }) - } -} -impl Error for ChannelAdmissionError {} diff --git a/core/crates/hub/peer/transport/src/client.rs b/core/crates/hub/peer/transport/src/client.rs deleted file mode 100644 index 1f8595c3..00000000 --- a/core/crates/hub/peer/transport/src/client.rs +++ /dev/null @@ -1,51 +0,0 @@ -use crate::{PeerStream, constants::*, pins::matches_key}; -use russh::{Channel, client}; -use std::sync::Arc; -use std::sync::atomic::{AtomicBool, Ordering}; -use tokio::sync::{Semaphore, mpsc}; -use yolo_hub_delegation::PeerIdentity; - -pub(crate) struct ClientHandler { - pub peer: PeerIdentity, - pub incoming: mpsc::Sender, - pub capacity: Arc, - pub closed: Arc, -} -impl Drop for ClientHandler { - fn drop(&mut self) { - self.closed.store(true, Ordering::Release); - } -} -impl client::Handler for ClientHandler { - type Error = anyhow::Error; - async fn check_server_key( - &mut self, - key: &russh::keys::PublicKey, - ) -> Result { - Ok(matches_key(self.peer, key)) - } - async fn server_channel_open_session( - &mut self, - channel: Channel, - reply: client::ChannelOpenHandle, - _: &mut client::Session, - ) -> Result<(), Self::Error> { - let Ok(capacity) = self.capacity.clone().try_acquire_owned() else { - return Ok(()); - }; - let Ok(queued) = self.incoming.clone().try_reserve_owned() else { - return Ok(()); - }; - reply.accept().await; - queued.send(PeerStream::new(channel.into_stream(), capacity)); - Ok(()) - } -} -pub(crate) fn config() -> Arc { - Arc::new(client::Config { - channel_buffer_size: SSH_CHANNEL_MESSAGES, - keepalive_interval: Some(KEEPALIVE_INTERVAL), - keepalive_max: MISSED_KEEPALIVES, - ..Default::default() - }) -} diff --git a/core/crates/hub/peer/transport/src/connect.rs b/core/crates/hub/peer/transport/src/connect.rs deleted file mode 100644 index 061442db..00000000 --- a/core/crates/hub/peer/transport/src/connect.rs +++ /dev/null @@ -1,116 +0,0 @@ -use crate::{ - PeerConnection, PeerIo, PeerPins, PeerStream, - client::{self, ClientHandler}, - connection::Role, - constants::*, - server::{self, ServerHandler}, -}; -use anyhow::{Context, Result, ensure}; -use russh::keys::{PrivateKey, PrivateKeyWithHashAlg}; -use std::sync::{Arc, atomic::AtomicBool}; -use tokio::{ - net::TcpStream, - sync::{Mutex, Semaphore, mpsc}, - time::{Instant, timeout_at}, -}; -use yolo_hub_delegation::PeerIdentity; - -pub async fn connect( - stream: TcpStream, - key: Arc, - peer: PeerIdentity, - deadline: Instant, -) -> Result<(PeerConnection, mpsc::Receiver)> { - connect_io(PeerIo::from_tcp(stream)?, key, peer, deadline).await -} - -pub async fn connect_io( - transport: PeerIo, - key: Arc, - peer: PeerIdentity, - deadline: Instant, -) -> Result<(PeerConnection, mpsc::Receiver)> { - let (stream, transport) = transport.into_parts(); - let capacity = Arc::new(Semaphore::new(MAX_PEER_CHANNELS)); - let closed = Arc::new(AtomicBool::new(false)); - let (incoming, receiver) = mpsc::channel(MAX_QUEUED_CHANNELS); - let handler = ClientHandler { - peer, - incoming, - capacity: capacity.clone(), - closed: closed.clone(), - }; - let mut handle = timeout_at( - deadline, - russh::client::connect_stream(client::config(), stream, handler), - ) - .await??; - let result = timeout_at( - deadline, - handle.authenticate_publickey(PEER_USER, PrivateKeyWithHashAlg::new(key, None)), - ) - .await??; - ensure!(result.success(), AUTH_FAILED); - Ok(( - PeerConnection { - peer, - role: Role::Client(Mutex::new(Some(handle))), - transport, - capacity, - closed, - }, - receiver, - )) -} - -pub async fn accept( - stream: TcpStream, - key: Arc, - pins: PeerPins, - deadline: Instant, -) -> Result<(PeerConnection, mpsc::Receiver)> { - accept_io(PeerIo::from_tcp(stream)?, key, pins, deadline).await -} - -pub async fn accept_io( - transport: PeerIo, - key: Arc, - pins: PeerPins, - deadline: Instant, -) -> Result<(PeerConnection, mpsc::Receiver)> { - let (stream, transport) = transport.into_parts(); - let capacity = Arc::new(Semaphore::new(MAX_PEER_CHANNELS)); - let closed = Arc::new(AtomicBool::new(false)); - let (incoming, receiver) = mpsc::channel(MAX_QUEUED_CHANNELS); - let (authenticated, mut authenticated_peer) = mpsc::channel(AUTH_EVENT_CAPACITY); - let handler = ServerHandler { - pins, - authenticated, - peer: None, - incoming, - capacity: capacity.clone(), - closed: closed.clone(), - }; - let running = timeout_at( - deadline, - russh::server::run_stream(server::config(key), stream, handler), - ) - .await??; - let peer = timeout_at(deadline, authenticated_peer.recv()) - .await? - .context(AUTH_FAILED)?; - let role = Role::Server { - handle: running.handle(), - running: Mutex::new(Some(running)), - }; - Ok(( - PeerConnection { - peer, - role, - transport, - capacity, - closed, - }, - receiver, - )) -} diff --git a/core/crates/hub/peer/transport/src/connection.rs b/core/crates/hub/peer/transport/src/connection.rs deleted file mode 100644 index 05e58994..00000000 --- a/core/crates/hub/peer/transport/src/connection.rs +++ /dev/null @@ -1,81 +0,0 @@ -use crate::{ - ChannelAdmissionError, PeerStream, client::ClientHandler, constants::*, io_custody::IoCustody, - server::ServerHandler, -}; -use anyhow::{Context, Result, ensure}; -use std::sync::{ - Arc, - atomic::{AtomicBool, Ordering}, -}; -use tokio::{ - sync::{Mutex, Semaphore}, - time::{Instant, timeout_at}, -}; -use yolo_hub_delegation::PeerIdentity; - -pub struct PeerConnection { - pub(crate) peer: PeerIdentity, - pub(crate) role: Role, - pub(crate) transport: IoCustody, - pub(crate) capacity: Arc, - pub(crate) closed: Arc, -} -pub(crate) enum Role { - Client(Mutex>>), - Server { - handle: russh::server::Handle, - running: Mutex>>, - }, -} -impl PeerConnection { - pub fn peer(&self) -> PeerIdentity { - self.peer - } - pub fn is_closed(&self) -> bool { - self.closed.load(Ordering::Acquire) - } - pub async fn open_channel(&self, deadline: Instant) -> Result { - ensure!(!self.is_closed(), CLOSED); - let permit = self - .capacity - .clone() - .try_acquire_owned() - .map_err(ChannelAdmissionError::from)?; - let stream = match &self.role { - Role::Client(handle) => { - let handle = timeout_at(deadline, handle.lock()).await?; - let handle = handle.as_ref().context(CLOSED)?; - PeerStream::new( - timeout_at(deadline, handle.channel_open_session()) - .await?? - .into_stream(), - permit, - ) - } - Role::Server { handle, .. } => PeerStream::new( - timeout_at(deadline, handle.channel_open_session()) - .await?? - .into_stream(), - permit, - ), - }; - Ok(stream) - } - pub async fn close(&self, deadline: Instant) -> Result<()> { - self.closed.store(true, Ordering::Release); - self.transport.close(); - match &self.role { - Role::Client(handle) => { - if let Some(handle) = timeout_at(deadline, handle.lock()).await?.take() { - let _ = timeout_at(deadline, handle).await?; - } - } - Role::Server { running, .. } => { - if let Some(running) = timeout_at(deadline, running.lock()).await?.take() { - let _ = timeout_at(deadline, running).await?; - } - } - } - Ok(()) - } -} diff --git a/core/crates/hub/peer/transport/src/constants.rs b/core/crates/hub/peer/transport/src/constants.rs deleted file mode 100644 index f8f26c29..00000000 --- a/core/crates/hub/peer/transport/src/constants.rs +++ /dev/null @@ -1,14 +0,0 @@ -use std::time::Duration; -pub const MAX_PEER_CHANNELS: usize = 256; -pub const MAX_QUEUED_CHANNELS: usize = 32; -pub const SSH_CHANNEL_MESSAGES: usize = 32; -pub const SSH_SESSION_MESSAGES: usize = 64; -pub const KEEPALIVE_INTERVAL: Duration = Duration::from_secs(10); -pub const MISSED_KEEPALIVES: usize = 3; -pub(crate) const AUTH_REJECTION_DELAY: Duration = Duration::from_millis(250); -pub(crate) const AUTH_EVENT_CAPACITY: usize = 1; -pub(crate) const PEER_USER: &str = "yolo-peer"; -pub(crate) const CLOSED: &str = "Hub peer transport is closed"; -pub(crate) const AUTH_FAILED: &str = "Hub peer identity was not authenticated"; -pub(crate) const INVALID_PINS: &str = "Hub peer pins must have unique identities and keys"; -pub(crate) const CHANNEL_LIMIT: &str = "Hub peer channel limit reached"; diff --git a/core/crates/hub/peer/transport/src/lib.rs b/core/crates/hub/peer/transport/src/lib.rs deleted file mode 100644 index 2f36e691..00000000 --- a/core/crates/hub/peer/transport/src/lib.rs +++ /dev/null @@ -1,24 +0,0 @@ -//! Encrypted, mutually pinned Hub transport. Dial direction does not grant export rights. -mod admission; -mod client; -mod connect; -mod connection; -mod constants; -mod io; -mod io_custody; -mod pins; -mod server; -mod socket; -mod stream; - -pub use admission::ChannelAdmissionError; -pub use connect::{accept, accept_io, connect, connect_io}; -pub use connection::PeerConnection; -pub use constants::{MAX_PEER_CHANNELS, MAX_QUEUED_CHANNELS}; -pub use io::PeerIo; -pub use pins::PeerPins; -pub use stream::{Duplex, PeerStream}; - -#[cfg(test)] -#[path = "../unit_tests/lib.rs"] -mod unit_tests; diff --git a/core/crates/hub/peer/transport/src/pins.rs b/core/crates/hub/peer/transport/src/pins.rs deleted file mode 100644 index 44b96916..00000000 --- a/core/crates/hub/peer/transport/src/pins.rs +++ /dev/null @@ -1,29 +0,0 @@ -use crate::constants::INVALID_PINS; -use anyhow::{Result, ensure}; -use russh::keys::PublicKey; -use yolo_hub_delegation::PeerIdentity; - -#[derive(Clone)] -pub struct PeerPins(Vec); - -impl PeerPins { - pub fn new(peers: Vec) -> Result { - for (index, peer) in peers.iter().enumerate() { - ensure!( - !peers[..index] - .iter() - .any(|other| other.hub() == peer.hub() || other.key() == peer.key()), - INVALID_PINS - ); - } - Ok(Self(peers)) - } - pub(crate) fn identify(&self, key: &PublicKey) -> Option { - self.0.iter().find(|peer| matches_key(**peer, key)).copied() - } -} -pub(crate) fn matches_key(identity: PeerIdentity, key: &PublicKey) -> bool { - key.key_data() - .ed25519() - .is_some_and(|key| &key.0 == identity.key()) -} diff --git a/core/crates/hub/peer/transport/src/server.rs b/core/crates/hub/peer/transport/src/server.rs deleted file mode 100644 index 83b9c83d..00000000 --- a/core/crates/hub/peer/transport/src/server.rs +++ /dev/null @@ -1,85 +0,0 @@ -use crate::{PeerPins, PeerStream, constants::*}; -use russh::{ - Channel, - keys::{PrivateKey, PublicKey}, - server, -}; -use std::sync::Arc; -use std::sync::atomic::{AtomicBool, Ordering}; -use tokio::sync::{Semaphore, mpsc}; -use yolo_hub_delegation::PeerIdentity; - -pub(crate) struct ServerHandler { - pub pins: PeerPins, - pub authenticated: mpsc::Sender, - pub peer: Option, - pub incoming: mpsc::Sender, - pub capacity: Arc, - pub closed: Arc, -} -impl Drop for ServerHandler { - fn drop(&mut self) { - self.closed.store(true, Ordering::Release); - } -} -impl server::Handler for ServerHandler { - type Error = anyhow::Error; - async fn auth_publickey_offered( - &mut self, - user: &str, - key: &PublicKey, - ) -> Result { - Ok(if user == PEER_USER && self.pins.identify(key).is_some() { - server::Auth::Accept - } else { - server::Auth::reject() - }) - } - async fn auth_publickey( - &mut self, - user: &str, - key: &PublicKey, - ) -> Result { - if user != PEER_USER || self.peer.is_some() { - return Ok(server::Auth::reject()); - } - let Some(peer) = self.pins.identify(key) else { - return Ok(server::Auth::reject()); - }; - if self.authenticated.try_send(peer).is_err() { - return Ok(server::Auth::reject()); - } - self.peer = Some(peer); - Ok(server::Auth::Accept) - } - async fn channel_open_session( - &mut self, - channel: Channel, - reply: server::ChannelOpenHandle, - _: &mut server::Session, - ) -> Result<(), Self::Error> { - if self.peer.is_none() { - return Ok(()); - } - let Ok(capacity) = self.capacity.clone().try_acquire_owned() else { - return Ok(()); - }; - let Ok(queued) = self.incoming.clone().try_reserve_owned() else { - return Ok(()); - }; - reply.accept().await; - queued.send(PeerStream::new(channel.into_stream(), capacity)); - Ok(()) - } -} -pub(crate) fn config(key: Arc) -> Arc { - Arc::new(server::Config { - keys: vec![(*key).clone()], - channel_buffer_size: SSH_CHANNEL_MESSAGES, - event_buffer_size: SSH_SESSION_MESSAGES, - keepalive_interval: Some(KEEPALIVE_INTERVAL), - keepalive_max: MISSED_KEEPALIVES, - auth_rejection_time: AUTH_REJECTION_DELAY, - ..Default::default() - }) -} diff --git a/core/crates/hub/peer/transport/src/socket.rs b/core/crates/hub/peer/transport/src/socket.rs deleted file mode 100644 index 74474744..00000000 --- a/core/crates/hub/peer/transport/src/socket.rs +++ /dev/null @@ -1,25 +0,0 @@ -use crate::PeerIo; -use std::{io, net::Shutdown}; -use tokio::net::TcpStream; - -pub(crate) struct SocketOwner(std::net::TcpStream); - -impl SocketOwner { - pub fn capture(stream: TcpStream) -> io::Result { - let original = stream.into_std()?; - let retained = original.try_clone()?; - let owner = Self(retained); - Ok(PeerIo::new(TcpStream::from_std(original)?, move || { - owner.close() - })) - } - pub fn close(&self) { - let _ = self.0.shutdown(Shutdown::Both); - } -} - -impl Drop for SocketOwner { - fn drop(&mut self) { - self.close(); - } -} diff --git a/core/crates/hub/peer/transport/src/stream.rs b/core/crates/hub/peer/transport/src/stream.rs deleted file mode 100644 index 3906b0e8..00000000 --- a/core/crates/hub/peer/transport/src/stream.rs +++ /dev/null @@ -1,49 +0,0 @@ -use std::{ - io, - pin::Pin, - task::{Context, Poll}, -}; -use tokio::{ - io::{AsyncRead, AsyncWrite, ReadBuf}, - sync::OwnedSemaphorePermit, -}; - -pub trait Duplex: AsyncRead + AsyncWrite + Unpin + Send {} -impl Duplex for T {} - -pub struct PeerStream { - stream: Box, - _permit: OwnedSemaphorePermit, -} -impl PeerStream { - pub(crate) fn new(stream: impl Duplex + 'static, permit: OwnedSemaphorePermit) -> Self { - Self { - stream: Box::new(stream), - _permit: permit, - } - } -} -impl AsyncRead for PeerStream { - fn poll_read( - mut self: Pin<&mut Self>, - context: &mut Context<'_>, - output: &mut ReadBuf<'_>, - ) -> Poll> { - Pin::new(&mut self.stream).poll_read(context, output) - } -} -impl AsyncWrite for PeerStream { - fn poll_write( - mut self: Pin<&mut Self>, - context: &mut Context<'_>, - input: &[u8], - ) -> Poll> { - Pin::new(&mut self.stream).poll_write(context, input) - } - fn poll_flush(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.stream).poll_flush(context) - } - fn poll_shutdown(mut self: Pin<&mut Self>, context: &mut Context<'_>) -> Poll> { - Pin::new(&mut self.stream).poll_shutdown(context) - } -} diff --git a/core/crates/hub/peer/transport/test_support/mod.rs b/core/crates/hub/peer/transport/test_support/mod.rs deleted file mode 100644 index 2181c528..00000000 --- a/core/crates/hub/peer/transport/test_support/mod.rs +++ /dev/null @@ -1,71 +0,0 @@ -use ring::signature::{Ed25519KeyPair, KeyPair}; -use russh::keys::{PrivateKey, pkcs8::decode_pkcs8}; -use std::{ - net::{IpAddr, Ipv4Addr, SocketAddr}, - sync::Arc, - time::Duration, -}; -use tokio::{ - net::{TcpListener, TcpStream}, - sync::mpsc, - time::Instant, -}; -use yolo_hub_delegation::PeerIdentity; -use yolo_hub_identity::HubId; -use yolo_hub_peer_transport::{PeerConnection, PeerPins, PeerStream}; - -pub const TEST_TIMEOUT: Duration = Duration::from_secs(15); -pub const POLL_INTERVAL: Duration = Duration::from_millis(10); -pub const PAYLOAD: &[u8] = b"peer\0terminal\r\n\x1b[31mdata"; -const DYNAMIC_PORT: u16 = 0; - -pub fn deadline() -> Instant { - Instant::now() + TEST_TIMEOUT -} -pub fn keys() -> (PeerIdentity, Arc) { - let document = Ed25519KeyPair::generate_pkcs8(&ring::rand::SystemRandom::new()).unwrap(); - let signing = Ed25519KeyPair::from_pkcs8(document.as_ref()).unwrap(); - let identity = PeerIdentity::new( - HubId::new(), - signing.public_key().as_ref().try_into().unwrap(), - ) - .unwrap(); - ( - identity, - Arc::new(decode_pkcs8(document.as_ref(), None).unwrap()), - ) -} - -pub async fn sockets() -> (TcpStream, TcpStream) { - let listener = TcpListener::bind(SocketAddr::new( - IpAddr::V4(Ipv4Addr::LOCALHOST), - DYNAMIC_PORT, - )) - .await - .unwrap(); - let address = listener.local_addr().unwrap(); - let (connected, accepted) = tokio::join!(TcpStream::connect(address), listener.accept()); - (connected.unwrap(), accepted.unwrap().0) -} - -pub async fn pair() -> ( - (PeerConnection, mpsc::Receiver), - (PeerConnection, mpsc::Receiver), -) { - let (left_id, left_key) = keys(); - let (right_id, right_key) = keys(); - let (left, right) = sockets().await; - let (left, right) = tokio::join!( - yolo_hub_peer_transport::connect(left, left_key, right_id, deadline()), - yolo_hub_peer_transport::accept( - right, - right_key, - PeerPins::new(vec![left_id]).unwrap(), - deadline() - ), - ); - let (left, right) = (left.unwrap(), right.unwrap()); - assert_eq!(left.0.peer(), right_id); - assert_eq!(right.0.peer(), left_id); - (left, right) -} diff --git a/core/crates/hub/peer/transport/unit_tests/admission.rs b/core/crates/hub/peer/transport/unit_tests/admission.rs deleted file mode 100644 index 4f8e5695..00000000 --- a/core/crates/hub/peer/transport/unit_tests/admission.rs +++ /dev/null @@ -1,31 +0,0 @@ -use crate::{ - ChannelAdmissionError, - constants::{CHANNEL_LIMIT, CLOSED}, -}; -use tokio::sync::Semaphore; - -const CHANNELS: usize = 1; - -#[test] -fn closing_a_full_pool_is_not_transient_capacity_pressure() { - let pool = Semaphore::new(CHANNELS); - let held = pool.try_acquire().unwrap(); - let full: anyhow::Error = ChannelAdmissionError::from(pool.try_acquire().unwrap_err()).into(); - assert_eq!(full.to_string(), CHANNEL_LIMIT); - assert_eq!( - full.downcast_ref::(), - Some(&ChannelAdmissionError::AtCapacity) - ); - pool.close(); - let closed: anyhow::Error = ChannelAdmissionError::from(pool.try_acquire().unwrap_err()).into(); - assert_eq!(closed.to_string(), CLOSED); - assert_eq!( - closed.downcast_ref::(), - Some(&ChannelAdmissionError::Closed) - ); - drop(held); - assert_eq!( - ChannelAdmissionError::from(pool.try_acquire().unwrap_err()), - ChannelAdmissionError::Closed, - ); -} diff --git a/core/crates/hub/peer/transport/unit_tests/lib.rs b/core/crates/hub/peer/transport/unit_tests/lib.rs deleted file mode 100644 index fb4b1478..00000000 --- a/core/crates/hub/peer/transport/unit_tests/lib.rs +++ /dev/null @@ -1,89 +0,0 @@ -mod admission; - -use crate::PeerIo; -use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, -}; -use tokio::io::{AsyncReadExt, AsyncWriteExt}; - -const BUFFER_BYTES: usize = 1; -const COUNT_ONE: usize = 1; -const DATA: &[u8] = b"blocked"; -const TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); - -#[tokio::test] -async fn close_wakes_both_pending_io_directions_and_runs_custody_only_once() { - let calls = Arc::new(AtomicUsize::default()); - let observed = calls.clone(); - let (stream, _remote) = tokio::io::duplex(BUFFER_BYTES); - let (stream, custody) = PeerIo::new(stream, move || { - observed.fetch_add(COUNT_ONE, Ordering::SeqCst); - }) - .into_parts(); - let (mut read, mut write) = tokio::io::split(stream); - let reading = tokio::spawn(async move { read.read_u8().await }); - let writing = tokio::spawn(async move { write.write_all(DATA).await }); - tokio::task::yield_now().await; - custody.close(); - custody.close(); - assert_eq!( - tokio::time::timeout(TIMEOUT, reading) - .await - .unwrap() - .unwrap() - .unwrap_err() - .kind(), - std::io::ErrorKind::NotConnected - ); - assert_eq!( - tokio::time::timeout(TIMEOUT, writing) - .await - .unwrap() - .unwrap() - .unwrap_err() - .kind(), - std::io::ErrorKind::NotConnected - ); - drop(custody); - assert_eq!(calls.load(Ordering::SeqCst), COUNT_ONE); -} - -#[tokio::test] -async fn explicit_transport_close_fences_write_flush_and_shutdown_without_reentering_owner() { - let calls = Arc::new(AtomicUsize::default()); - let observed = calls.clone(); - let (stream, mut remote) = tokio::io::duplex(DATA.len()); - let (mut stream, custody) = PeerIo::new(stream, move || { - observed.fetch_add(COUNT_ONE, Ordering::SeqCst); - }) - .into_parts(); - stream.write_all(DATA).await.unwrap(); - stream.flush().await.unwrap(); - let mut output = vec![u8::default(); DATA.len()]; - remote.read_exact(&mut output).await.unwrap(); - assert_eq!(output, DATA); - custody.close(); - assert_eq!( - stream.write(DATA).await.unwrap_err().kind(), - std::io::ErrorKind::NotConnected - ); - assert_eq!( - stream.flush().await.unwrap_err().kind(), - std::io::ErrorKind::NotConnected - ); - stream.shutdown().await.unwrap(); - drop((stream, custody)); - assert_eq!(calls.load(Ordering::SeqCst), COUNT_ONE); -} - -#[test] -fn dropping_an_unadmitted_transport_releases_its_owner_once() { - let calls = Arc::new(AtomicUsize::default()); - let observed = calls.clone(); - let (stream, _) = tokio::io::duplex(BUFFER_BYTES); - drop(PeerIo::new(stream, move || { - observed.fetch_add(COUNT_ONE, Ordering::SeqCst); - })); - assert_eq!(calls.load(Ordering::SeqCst), COUNT_ONE); -} diff --git a/core/crates/hub/remote/setup/protocol/src/link.rs b/core/crates/hub/remote/setup/protocol/src/link.rs index 4172c79c..84894e6f 100644 --- a/core/crates/hub/remote/setup/protocol/src/link.rs +++ b/core/crates/hub/remote/setup/protocol/src/link.rs @@ -1,5 +1,5 @@ use anyhow::{Result, ensure}; -use yolo_hub_federation_settings::{DialTransport, Peer, PeerAddress}; +use yolo_hub_federation_settings::{Peer, PeerAddress}; use yolo_hub_management_protocol::{ConfigurationChange, ConfigurationSnapshot}; use yolo_remote_provisioning_protocol::{ProvisionOutcome, ProvisionStatus}; use yolo_ssh_profile_protocol::SshProfileRef; @@ -25,8 +25,8 @@ pub fn configuration_contains_link( )?; Ok(configuration.applied && peer.identity == enrollment.fields().peer && peer.consume && peer.dial.as_ref().is_some_and(|dial| { - matches!(&dial.transport, DialTransport::Ssh { profile: selected } if selected == &profile) - && dial.ssh_endpoint().is_ok_and(|endpoint| endpoint == enrollment.fields().endpoint) + dial.profile_id() == Some(profile.profile_id()) + && dial.socket() == enrollment.fields().peer_socket.as_path() })) } @@ -47,7 +47,10 @@ pub fn plan_link( anyhow::bail!(crate::constants::INVALID_REQUEST); }; let remote = enrollment.fields().peer; - let dial = PeerAddress::ssh(request.profile()?, enrollment.fields().endpoint)?; + let dial = PeerAddress::profile( + request.profile()?.profile_id().to_owned(), + enrollment.fields().peer_socket.clone(), + )?; let mut configuration = baseline.state.configuration.clone(); match configuration .peers diff --git a/core/crates/hub/remote/setup/protocol/test_support/constants.rs b/core/crates/hub/remote/setup/protocol/test_support/constants.rs index a2072bb5..0684def6 100644 --- a/core/crates/hub/remote/setup/protocol/test_support/constants.rs +++ b/core/crates/hub/remote/setup/protocol/test_support/constants.rs @@ -1,6 +1,6 @@ pub const CREDENTIAL_BYTES: &[u8] = b"private-input-not-in-debug-or-status"; -pub const OTHER_HOST: &str = "other.example"; -pub const OTHER_PORT: u16 = 12345; +pub const OTHER_PROFILE: &str = "other-profile"; +pub const OTHER_SOCKET: &str = "/run/user/1000/yolo/other.sock"; pub const EXTRA_FIELD: &str = "extra"; pub const EXPECTED_REVISION_FIELD: &str = "expected_configuration_revision"; pub const INVALID_REVISION: u64 = 0; diff --git a/core/crates/hub/remote/setup/protocol/test_support/fixture.rs b/core/crates/hub/remote/setup/protocol/test_support/fixture.rs index e0b89bcf..ee0ba183 100644 --- a/core/crates/hub/remote/setup/protocol/test_support/fixture.rs +++ b/core/crates/hub/remote/setup/protocol/test_support/fixture.rs @@ -20,7 +20,7 @@ pub fn baseline(request: &RemoteSetupRequest) -> ConfigurationSnapshot { configuration: Default::default(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), } } pub fn published(request: &RemoteSetupRequest) -> ProvisionStatus { diff --git a/core/crates/hub/remote/setup/protocol/unit_tests/link.rs b/core/crates/hub/remote/setup/protocol/unit_tests/link.rs index 970b5353..c86a9974 100644 --- a/core/crates/hub/remote/setup/protocol/unit_tests/link.rs +++ b/core/crates/hub/remote/setup/protocol/unit_tests/link.rs @@ -1,6 +1,7 @@ use super::{constants::*, fixture::*}; use yolo_hub_federation_settings::{ExportPolicy, Peer, PeerAddress}; use yolo_hub_remote_setup_protocol::*; +use yolo_remote_provisioning_protocol::ProvisionOutcome; #[test] fn linking_preserves_existing_policy_and_is_noop_only_for_the_exact_ssh_route() { @@ -12,10 +13,12 @@ fn linking_preserves_existing_policy_and_is_noop_only_for_the_exact_ssh_route() assert_eq!(change.expected_revision, baseline.state.revision); let peer = change.configuration.peers.first().unwrap().clone(); assert!(peer.consume && peer.exports.is_empty()); - assert!(matches!( - peer.dial.as_ref().unwrap().transport, - yolo_hub_federation_settings::DialTransport::Ssh { .. } - )); + let dial = peer.dial.as_ref().unwrap(); + assert_eq!(dial.profile_id(), Some(request.profile().unwrap().profile_id())); + let ProvisionOutcome::Ready { enrollment, .. } = &provision.fields().outcome else { + panic!() + }; + assert_eq!(dial.socket(), enrollment.fields().peer_socket.as_path()); let mut connected = baseline.clone(); connected.state.configuration = change.configuration; assert!(configuration_contains_link(&connected, &provision).unwrap()); @@ -44,10 +47,8 @@ fn linking_preserves_existing_policy_and_is_noop_only_for_the_exact_ssh_route() link.configuration.peers[0].exports, current.state.configuration.peers[0].exports ); - current.state.configuration.peers[0].dial = Some(PeerAddress::tcp( - OTHER_HOST, - std::num::NonZeroU16::new(OTHER_PORT).unwrap(), - )); + current.state.configuration.peers[0].dial = + Some(PeerAddress::profile(OTHER_PROFILE, OTHER_SOCKET.into()).unwrap()); assert!(plan_link(¤t, &request, &provision).is_err()); } diff --git a/core/crates/hub/remote/setup/store/test_support/fixture.rs b/core/crates/hub/remote/setup/store/test_support/fixture.rs index 39dae38b..59549f91 100644 --- a/core/crates/hub/remote/setup/store/test_support/fixture.rs +++ b/core/crates/hub/remote/setup/store/test_support/fixture.rs @@ -64,7 +64,7 @@ impl Fixture { configuration: Configuration::default(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), }; let record = SetupRecord::new(owner, request, registration, baseline).unwrap(); Self { diff --git a/core/crates/hub/remote/setup/store/unit_tests/model.rs b/core/crates/hub/remote/setup/store/unit_tests/model.rs index cab9d020..7a93420d 100644 --- a/core/crates/hub/remote/setup/store/unit_tests/model.rs +++ b/core/crates/hub/remote/setup/store/unit_tests/model.rs @@ -64,7 +64,7 @@ fn routing_cannot_claim_ready_from_unknown_provision_or_an_unrelated_configurati else { panic!("expected link change") }; - change.configuration.listen = Some((std::net::Ipv4Addr::LOCALHOST, EXTRA_PORT).into()); + change.request_id = uuid::Uuid::nil(); assert!(record.validate().is_err()); } diff --git a/core/crates/hub/remote/startup/integration_tests/verification.rs b/core/crates/hub/remote/startup/integration_tests/verification.rs index 1b93fcfb..9265ee5b 100644 --- a/core/crates/hub/remote/startup/integration_tests/verification.rs +++ b/core/crates/hub/remote/startup/integration_tests/verification.rs @@ -26,7 +26,7 @@ async fn historical_completion_does_not_hide_a_changed_live_peer_key_or_unapplie .unwrap(); } else { state.snapshot.applied = false; - state.snapshot.listen_address = None; + state.snapshot.peer_socket = None; } } assert!(matches!( diff --git a/core/crates/hub/remote/startup/protocol/test_support/fixtures.rs b/core/crates/hub/remote/startup/protocol/test_support/fixtures.rs index 1619d6f9..ef3b180e 100644 --- a/core/crates/hub/remote/startup/protocol/test_support/fixtures.rs +++ b/core/crates/hub/remote/startup/protocol/test_support/fixtures.rs @@ -47,7 +47,7 @@ pub fn receipt(request: &RemoteBootstrapRequest) -> RemoteBootstrapReceiptFields schema_version: REMOTE_BOOTSTRAP_SCHEMA_VERSION, request_id: request.fields().request_id, peer: peer(REMOTE_HUB, REMOTE_KEY), - endpoint: std::net::SocketAddr::from((std::net::Ipv4Addr::LOCALHOST, PORT)), + peer_socket: "/run/user/1000/yolo/hub/peer.sock".into(), configuration_revision: FIRST_REVISION, bundle_id: request.fields().bundle.build_id().into(), build: ServiceBuild { diff --git a/core/crates/hub/remote/startup/src/configuration.rs b/core/crates/hub/remote/startup/src/configuration.rs index b3a5ec53..7d7ce66a 100644 --- a/core/crates/hub/remote/startup/src/configuration.rs +++ b/core/crates/hub/remote/startup/src/configuration.rs @@ -34,10 +34,6 @@ pub(crate) fn plan( PIN_MISMATCH ); let mut configuration = snapshot.state.configuration.clone(); - match configuration.listen { - Some(address) => ensure!(address.ip().is_loopback(), CONFIGURATION_CONFLICT), - None => configuration.listen = Some((std::net::Ipv4Addr::LOCALHOST, DYNAMIC_PORT).into()), - } let policy = ExportPolicy { origin: remote.hub(), rights: expected.rights.clone(), @@ -68,12 +64,7 @@ pub(crate) fn plan( } configuration.validate(remote)?; if configuration == snapshot.state.configuration { - ensure!( - snapshot - .listen_address - .is_some_and(|address| address.ip().is_loopback()), - LISTENER_UNAVAILABLE - ); + ensure!(snapshot.peer_socket.is_some(), PEER_SOCKET_UNAVAILABLE); return Ok(None); } let change = ConfigurationChange { diff --git a/core/crates/hub/remote/startup/src/constants.rs b/core/crates/hub/remote/startup/src/constants.rs index bee350ec..5b981173 100644 --- a/core/crates/hub/remote/startup/src/constants.rs +++ b/core/crates/hub/remote/startup/src/constants.rs @@ -4,7 +4,7 @@ pub(crate) const DEADLINE: &str = "remote Hub bootstrap deadline expired"; pub(crate) const BUNDLE_MISSING: &str = "remote runtime bundle is not completely installed"; pub(crate) const PIN_MISMATCH: &str = "persistent remote Hub identity does not match its enrollment pin"; -pub(crate) const LISTENER_UNAVAILABLE: &str = "remote Hub loopback listener is not applied"; +pub(crate) const PEER_SOCKET_UNAVAILABLE: &str = "remote Hub peer socket is not published"; pub(crate) const CONFIGURATION_CONFLICT: &str = "remote Hub configuration conflicts with the enrollment request"; pub(crate) const CONFIGURATION_UNKNOWN: &str = diff --git a/core/crates/hub/remote/startup/src/receipt.rs b/core/crates/hub/remote/startup/src/receipt.rs index dc9eb7fa..18d07381 100644 --- a/core/crates/hub/remote/startup/src/receipt.rs +++ b/core/crates/hub/remote/startup/src/receipt.rs @@ -15,14 +15,15 @@ pub(crate) fn create( ensure!(prepared.applied_to(ready), CONFIGURATION_UNKNOWN); let request = &prepared.request; configuration::verify_images(&build, request)?; - let endpoint = ready - .listen_address - .ok_or_else(|| anyhow::anyhow!(LISTENER_UNAVAILABLE))?; + let peer_socket = ready + .peer_socket + .clone() + .ok_or_else(|| anyhow::anyhow!(PEER_SOCKET_UNAVAILABLE))?; let receipt = RemoteBootstrapReceipt::new(RemoteBootstrapReceiptFields { schema_version: REMOTE_BOOTSTRAP_SCHEMA_VERSION, request_id: request.fields().request_id, peer: prepared.peer, - endpoint, + peer_socket, configuration_revision: ready.state.revision, bundle_id: request.fields().bundle.build_id().into(), build, diff --git a/core/crates/hub/remote/startup/test_support/fixture.rs b/core/crates/hub/remote/startup/test_support/fixture.rs index 4abe7137..354bb99d 100644 --- a/core/crates/hub/remote/startup/test_support/fixture.rs +++ b/core/crates/hub/remote/startup/test_support/fixture.rs @@ -13,6 +13,6 @@ pub fn snapshot() -> ConfigurationSnapshot { configuration: yolo_hub_federation_settings::Configuration::default(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), } } diff --git a/core/crates/hub/remote/startup/test_support/recovery_fixture.rs b/core/crates/hub/remote/startup/test_support/recovery_fixture.rs index 7cbb4f50..6762780f 100644 --- a/core/crates/hub/remote/startup/test_support/recovery_fixture.rs +++ b/core/crates/hub/remote/startup/test_support/recovery_fixture.rs @@ -46,7 +46,7 @@ impl Fixture { configuration: Default::default(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), }, build: yolo_hub_remote_startup_test_support::receipt(&request).build, methods: Vec::new(), @@ -95,7 +95,7 @@ impl Fixture { state.snapshot.state.configuration = change.configuration.clone(); state.snapshot.state.revision = prepared.revision(); state.snapshot.state.last_change = change.request_id; - state.snapshot.listen_address = Some((std::net::Ipv4Addr::LOCALHOST, PORT).into()); + state.snapshot.peer_socket = Some("/run/user/1000/yolo/hub/peer.sock".into()); } pub fn configured(&self) -> usize { self.shared diff --git a/core/crates/hub/remote/startup/test_support/recovery_reply.rs b/core/crates/hub/remote/startup/test_support/recovery_reply.rs index f3b803f6..20de782c 100644 --- a/core/crates/hub/remote/startup/test_support/recovery_reply.rs +++ b/core/crates/hub/remote/startup/test_support/recovery_reply.rs @@ -26,7 +26,7 @@ pub(super) fn respond(state: &mut State, request: &Request) -> Option { state.snapshot.state.configuration = change.configuration.clone(); state.snapshot.state.last_change = change.request_id; state.snapshot.state.revision += CONFIGURATION_REVISION_STEP; - state.snapshot.listen_address = Some((std::net::Ipv4Addr::LOCALHOST, PORT).into()); + state.snapshot.peer_socket = Some("/run/user/1000/yolo/hub/peer.sock".into()); if std::mem::take(&mut state.lose_reply) { None } else { diff --git a/core/crates/hub/remote/startup/unit_tests/configuration.rs b/core/crates/hub/remote/startup/unit_tests/configuration.rs index 44370196..56b02e45 100644 --- a/core/crates/hub/remote/startup/unit_tests/configuration.rs +++ b/core/crates/hub/remote/startup/unit_tests/configuration.rs @@ -1,5 +1,5 @@ use crate::configuration::{plan, verify_images}; -use yolo_hub_federation_settings::{ExportPolicy, Peer}; +use yolo_hub_federation_settings::{ExportPolicy, Peer, PeerAddress}; use yolo_remote_provisioning_protocol::RemoteBootstrapRequest; use yolo_hub_remote_startup_test_support::*; #[path = "../test_support/fixture.rs"] @@ -11,7 +11,7 @@ fn new_enrollment_preserves_existing_peers_and_only_exports_requested_origin_rig let other = Peer { identity: peer(FOREIGN_HUB, FOREIGN_KEY), dial: None, - consume: true, + consume: false, exports: vec![], }; snapshot.state.configuration.peers.push(other.clone()); @@ -33,13 +33,8 @@ fn new_enrollment_preserves_existing_peers_and_only_exports_requested_origin_rig remaining_reexports: request.fields().remaining_reexports }] ); - assert_eq!( - change.configuration.listen.unwrap().ip(), - std::net::Ipv4Addr::LOCALHOST - ); - assert_eq!(change.configuration.listen.unwrap().port(), 0); snapshot.state.configuration = change.configuration; - snapshot.listen_address = Some((std::net::Ipv4Addr::LOCALHOST, PORT).into()); + snapshot.peer_socket = Some("/run/user/1000/yolo/hub/peer.sock".into()); assert!(plan(&snapshot, &request).unwrap().is_none()); } @@ -59,7 +54,13 @@ fn existing_exports_and_consume_direction_are_not_silently_changed() { }; snapshot.state.configuration.peers.push(Peer { identity: request.fields().consumer, - dial: None, + dial: Some( + PeerAddress::profile( + "existing-profile", + "/run/user/1000/yolo/hub/peer.sock".into(), + ) + .unwrap(), + ), consume: true, exports: vec![existing.clone()], }); @@ -71,21 +72,20 @@ fn existing_exports_and_consume_direction_are_not_silently_changed() { assert!(consumer.consume); assert_eq!(consumer.exports[0], existing); snapshot.state.configuration = change.configuration; - snapshot.listen_address = Some((std::net::Ipv4Addr::LOCALHOST, PORT).into()); + snapshot.peer_socket = Some("/run/user/1000/yolo/hub/peer.sock".into()); snapshot.state.configuration.peers[0].exports[1].remaining_reexports += 1; assert!(plan(&snapshot, &request).is_err()); } #[test] -fn pin_listener_and_apply_conflicts_fail_without_mutating_input() { +fn pin_socket_and_apply_conflicts_fail_without_mutating_input() { let request = request(); let original = fixture::snapshot(); let mut snapshot = original.clone(); snapshot.applied = false; assert!(plan(&snapshot, &request).is_err()); let mut snapshot = original.clone(); - snapshot.state.configuration.listen = Some((std::net::Ipv4Addr::UNSPECIFIED, 0).into()); - snapshot.listen_address = Some((std::net::Ipv4Addr::UNSPECIFIED, PORT).into()); + snapshot.peer_socket = None; assert!(plan(&snapshot, &request).is_err()); let mut snapshot = original.clone(); snapshot.state.identity = peer(FOREIGN_HUB, FOREIGN_KEY); diff --git a/core/crates/hub/routing/src/rights.rs b/core/crates/hub/routing/src/rights.rs index f3a81bd5..d5248ea1 100644 --- a/core/crates/hub/routing/src/rights.rs +++ b/core/crates/hub/routing/src/rights.rs @@ -23,6 +23,26 @@ pub enum Right { pub struct Rights(BTreeSet); impl Rights { + /// Every right a peer can hold. Local sharing policies may grant this set + /// when the SSH account already has equivalent local authority. + pub fn all() -> Self { + [ + Right::Observe, + Right::TerminalRead, + Right::TerminalWrite, + Right::Lifecycle, + Right::FileRead, + Right::AgentControl, + Right::ConversationRead, + Right::PresentationRead, + Right::PresentationWrite, + Right::WorkspaceRead, + Right::WorkspaceWrite, + ] + .into_iter() + .collect() + } + pub fn is_empty(&self) -> bool { self.0.is_empty() } diff --git a/core/crates/hub/rpc/protocol/src/command.rs b/core/crates/hub/rpc/protocol/src/command.rs index 1d293cb0..cb60ddc6 100644 --- a/core/crates/hub/rpc/protocol/src/command.rs +++ b/core/crates/hub/rpc/protocol/src/command.rs @@ -140,6 +140,7 @@ pub enum Command { creation: CreateShellResult, }, GetConfiguration {}, + ListConsumers {}, Configure { change: ConfigurationChange, }, diff --git a/core/crates/hub/rpc/protocol/src/command_kind.rs b/core/crates/hub/rpc/protocol/src/command_kind.rs index 5ac9211e..ee64675a 100644 --- a/core/crates/hub/rpc/protocol/src/command_kind.rs +++ b/core/crates/hub/rpc/protocol/src/command_kind.rs @@ -52,6 +52,7 @@ impl Command { Self::CleanupTerminate { .. } => Method::CleanupTerminate, Self::CleanupArchive { .. } => Method::CleanupArchive, Self::GetConfiguration {} => Method::GetConfiguration, + Self::ListConsumers {} => Method::ListConsumers, Self::Configure { .. } => Method::Configure, Self::ListFileRoots { .. } => Method::ListFileRoots, Self::ReadFile { .. } => Method::ReadFile, diff --git a/core/crates/hub/rpc/protocol/src/command_validation.rs b/core/crates/hub/rpc/protocol/src/command_validation.rs index 04b519d4..1ad51c28 100644 --- a/core/crates/hub/rpc/protocol/src/command_validation.rs +++ b/core/crates/hub/rpc/protocol/src/command_validation.rs @@ -168,7 +168,8 @@ impl Command { Self::GetPlatformSnapshot { .. } | Self::ListPlatformEvents { .. } | Self::SubscribeEvents { .. } - | Self::GetConfiguration {} => None, + | Self::GetConfiguration {} + | Self::ListConsumers {} => None, Self::ListFileRoots { .. } => None, }; if owner.is_some_and(|owner| owner != origin) { diff --git a/core/crates/hub/rpc/protocol/src/constants.rs b/core/crates/hub/rpc/protocol/src/constants.rs index bab57169..d697fe89 100644 --- a/core/crates/hub/rpc/protocol/src/constants.rs +++ b/core/crates/hub/rpc/protocol/src/constants.rs @@ -1,4 +1,4 @@ -pub const HUB_PROTOCOL_VERSION: u16 = 18; +pub const HUB_PROTOCOL_VERSION: u16 = 20; pub const MAX_CATALOG_PAGE_TERMINALS: usize = 128; pub const MAX_CATALOG_PAGE_AGENTS: usize = 128; pub const MAX_CATALOG_PAGE_HUBS: usize = 128; diff --git a/core/crates/hub/rpc/protocol/src/consumer.rs b/core/crates/hub/rpc/protocol/src/consumer.rs new file mode 100644 index 00000000..34e035fa --- /dev/null +++ b/core/crates/hub/rpc/protocol/src/consumer.rs @@ -0,0 +1,12 @@ +use serde::{Deserialize, Serialize}; +use yolo_hub_identity::HubId; + +/// One remote machine currently importing from this Hub. +#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] +pub struct ConsumerSummary { + pub hub: HubId, + /// Unix milliseconds when the first live link from this machine arrived. + pub connected_since_ms: u64, + /// Number of live links from this machine. + pub links: u16, +} diff --git a/core/crates/hub/rpc/protocol/src/lib.rs b/core/crates/hub/rpc/protocol/src/lib.rs index c6e6eb18..9c0a4d25 100644 --- a/core/crates/hub/rpc/protocol/src/lib.rs +++ b/core/crates/hub/rpc/protocol/src/lib.rs @@ -21,7 +21,9 @@ mod snapshot; mod workspace_reply; pub use access::Access; +mod consumer; pub use command::Command; +pub use consumer::ConsumerSummary; pub use constants::*; pub use envelope::{Request, RequestFields}; pub use hub::HubSummary; diff --git a/core/crates/hub/rpc/protocol/src/operation.rs b/core/crates/hub/rpc/protocol/src/operation.rs index e2b9dae0..3f2ebe10 100644 --- a/core/crates/hub/rpc/protocol/src/operation.rs +++ b/core/crates/hub/rpc/protocol/src/operation.rs @@ -135,6 +135,7 @@ operations! { } MANAGEMENT_METHODS { GetConfiguration: LocalManagement, ReadOnly, + ListConsumers: LocalManagement, ReadOnly, Configure: LocalManagement, Mutating, } BUILD_METHODS { diff --git a/core/crates/hub/rpc/protocol/src/reply.rs b/core/crates/hub/rpc/protocol/src/reply.rs index b18efb02..01d95109 100644 --- a/core/crates/hub/rpc/protocol/src/reply.rs +++ b/core/crates/hub/rpc/protocol/src/reply.rs @@ -60,6 +60,9 @@ pub enum Reply { Configuration { snapshot: yolo_hub_management_protocol::ConfigurationSnapshot, }, + Consumers { + consumers: Vec, + }, Hubs { hubs: Vec, next: Option, diff --git a/core/crates/hub/rpc/protocol/src/reply_valid.rs b/core/crates/hub/rpc/protocol/src/reply_valid.rs index 7750736a..2a1d1192 100644 --- a/core/crates/hub/rpc/protocol/src/reply_valid.rs +++ b/core/crates/hub/rpc/protocol/src/reply_valid.rs @@ -114,6 +114,10 @@ pub(crate) fn valid(reply: &Reply, method: Method, origin: HubId, version: u16) (Reply::Configuration { snapshot }, Method::GetConfiguration | Method::Configure) => { snapshot.validate().is_ok() && snapshot.state.identity.hub() == origin } + (Reply::Consumers { consumers }, Method::ListConsumers) => { + consumers.iter().all(|consumer| consumer.links > 0) + && consumers.windows(2).all(|pair| pair[0].hub < pair[1].hub) + } (Reply::Error { .. }, _) => true, (Reply::Hubs { hubs, next }, Method::ListHubs) => { hubs.len() <= MAX_CATALOG_PAGE_HUBS diff --git a/core/crates/hub/rpc/protocol/src/response_target.rs b/core/crates/hub/rpc/protocol/src/response_target.rs index 52a105d6..c773a521 100644 --- a/core/crates/hub/rpc/protocol/src/response_target.rs +++ b/core/crates/hub/rpc/protocol/src/response_target.rs @@ -43,6 +43,7 @@ pub fn reply_matches_target(reply: &Reply, command: &Command) -> bool { (Reply::Layouts { .. }, Command::ListLayouts {}) => true, (Reply::SshProfiles { .. }, Command::GetSshProfiles {}) => true, (Reply::Configuration { .. }, Command::GetConfiguration {}) => true, + (Reply::Consumers { .. }, Command::ListConsumers {}) => true, (Reply::ClientAccess { .. }, Command::GetClientAccess {}) => true, ( Reply::ConversationUnavailable { .. }, diff --git a/core/crates/hub/rpc/protocol/unit_tests/management.rs b/core/crates/hub/rpc/protocol/unit_tests/management.rs index 8ca31716..c4ba7658 100644 --- a/core/crates/hub/rpc/protocol/unit_tests/management.rs +++ b/core/crates/hub/rpc/protocol/unit_tests/management.rs @@ -22,7 +22,7 @@ fn fixture() -> (Request, ConfigurationSnapshot) { configuration: change.configuration.clone(), }, applied: true, - listen_address: None, + peer_socket: Some("/run/user/1000/yolo/hub/peer.sock".into()), }; ( Request::new(identity.hub(), Command::Configure { change }).unwrap(), @@ -45,7 +45,7 @@ fn configure_response_requires_the_exact_applied_revision_request_and_payload() |snapshot| snapshot.applied = false, |snapshot| snapshot.state.revision += CONFIGURATION_REVISION_STEP, |snapshot| snapshot.state.last_change = Uuid::new_v4(), - |snapshot| snapshot.state.configuration.listen = Some("127.0.0.1:0".parse().unwrap()), + |snapshot| snapshot.peer_socket = None, |snapshot| snapshot.state.schema_version = u16::MAX, ]; for mutate in mutations { @@ -61,6 +61,7 @@ fn get_configuration_can_report_unapplied_state_but_cannot_substitute_another_hu let request = Request::new(original.fields().destination, Command::GetConfiguration {}).unwrap(); snapshot.applied = false; + snapshot.peer_socket = None; Response::new( &request, request.fields().destination, @@ -109,3 +110,21 @@ fn configuration_commands_are_local_only_with_mutation_identity_and_revision_fen change.request_id = Uuid::nil(); assert!(Request::new(request.fields().destination, Command::Configure { change }).is_err()); } + +#[test] +fn consumers_reply_is_local_only_and_roundtrips() { + let destination = HubId::new(); + let request = Request::new(destination, Command::ListConsumers {}).unwrap(); + assert!(!request.fields().command.mutates()); + let reply = Reply::Consumers { + consumers: vec![ConsumerSummary { + hub: HubId::new(), + connected_since_ms: 1, + links: 1, + }], + }; + let response = Response::new(&request, destination, reply); + response.validate_for(&request).unwrap(); + let bytes = serde_json::to_vec(&response).unwrap(); + assert_eq!(serde_json::from_slice::(&bytes).unwrap(), response); +} diff --git a/core/crates/hub/rpc/protocol/unit_tests/operation.rs b/core/crates/hub/rpc/protocol/unit_tests/operation.rs index a7a4f70b..8cdd0e44 100644 --- a/core/crates/hub/rpc/protocol/unit_tests/operation.rs +++ b/core/crates/hub/rpc/protocol/unit_tests/operation.rs @@ -71,7 +71,7 @@ fn operation_table_defines_every_method_once_and_keeps_the_wire_groups_explicit( ); assert_eq!( MANAGEMENT_METHODS, - &[Method::GetConfiguration, Method::Configure] + &[Method::GetConfiguration, Method::ListConsumers, Method::Configure] ); assert_eq!(BUILD_METHODS, &[Method::GetBuild]); assert_eq!( @@ -161,5 +161,7 @@ fn operation_table_derives_access_mutation_and_local_management() { ); assert!(!Method::List.operation().requires_read_reply_admission()); assert!(Command::GetConfiguration {}.local_only()); + assert!(Method::GetConfiguration.operation().access == Access::LocalManagement); + assert!(!Method::GetConfiguration.mutates()); assert!(!Command::Hello {}.local_only()); } diff --git a/core/crates/hub/ssh/profile/runtime/BUILD.bazel b/core/crates/hub/ssh/profile/runtime/BUILD.bazel index 6a35a7f5..a2d49fcb 100644 --- a/core/crates/hub/ssh/profile/runtime/BUILD.bazel +++ b/core/crates/hub/ssh/profile/runtime/BUILD.bazel @@ -6,11 +6,10 @@ package(default_visibility = ["//visibility:public"]) _DEPS = [ "//core/crates/hub/broker/launcher:yolo_hub_broker_launcher", "//core/crates/hub/delegation:yolo_hub_delegation", + "//core/crates/hub/peer/dialer:yolo_hub_peer_dialer", "//core/crates/hub/dispatch:yolo_hub_dispatch", "//core/crates/hub/federation/settings:yolo_hub_federation_settings", "//core/crates/hub/identity:yolo_hub_identity", - "//core/crates/hub/peer/dialer:yolo_hub_peer_dialer", - "//core/crates/hub/peer/transport:yolo_hub_peer_transport", "//core/crates/hub/rpc/protocol:yolo_hub_rpc_protocol", "//core/crates/hub/terminal/protocol:yolo_hub_terminal_protocol", "//core/crates/hub/ssh/profiles:yolo_hub_ssh_profiles", @@ -50,6 +49,9 @@ rust_test( edition = "2024", deps = _DEPS + [ ":yolo_hub_ssh_profile_runtime", + "//core/crates/hub/peer/link:yolo_hub_peer_link", + "//core/crates/hub/peer/protocol:yolo_hub_peer_protocol", + "//core/crates/hub/peer/rpc:rpc", "//core/crates/wire:yolo_wire", "@crates//:tempfile", "@crates//:uuid", diff --git a/core/crates/hub/ssh/profile/runtime/integration_tests/lifecycle.rs b/core/crates/hub/ssh/profile/runtime/integration_tests/lifecycle.rs index 7ba36ae9..1e9d270d 100644 --- a/core/crates/hub/ssh/profile/runtime/integration_tests/lifecycle.rs +++ b/core/crates/hub/ssh/profile/runtime/integration_tests/lifecycle.rs @@ -8,7 +8,7 @@ use yolo_hub_ssh_profile_runtime::SshProfileRuntime; async fn final_runtime_owner_drop_closes_live_tunnels_and_releases_the_store_without_a_scheduler_turn() { let fixture = Fixture::new(); - let tunnels = Tunnels::open(&fixture).await; + let mut tunnels = Tunnels::open(&fixture).await; let Fixture { root, state, @@ -35,7 +35,7 @@ async fn final_runtime_owner_drop_closes_live_tunnels_and_releases_the_store_wit async fn registry_identity_loss_read_closes_every_profile_tunnel_before_its_response() { for replacement in [Replacement::Record, Replacement::Root] { let fixture = Fixture::new(); - let tunnels = Tunnels::open(&fixture).await; + let mut tunnels = Tunnels::open(&fixture).await; replace_same_json(&fixture, replacement); let result = ready_now(fixture.call(Command::GetSshProfiles {})); assert_eq!(result, Err(PublicError::Unreachable)); diff --git a/core/crates/hub/ssh/profile/runtime/integration_tests/transport.rs b/core/crates/hub/ssh/profile/runtime/integration_tests/transport.rs index 3f005d65..c3cc8656 100644 --- a/core/crates/hub/ssh/profile/runtime/integration_tests/transport.rs +++ b/core/crates/hub/ssh/profile/runtime/integration_tests/transport.rs @@ -1,24 +1,48 @@ use super::{broker, constants::*, fixture::*}; use std::sync::Arc; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, - time::timeout_at, -}; -use yolo_hub_federation_settings::{PeerAddress, SshProfileRef}; +use tokio::time::timeout_at; +use yolo_hub_federation_settings::PeerAddress; use yolo_hub_peer_dialer::dial_peer; -use yolo_hub_peer_transport::{PeerIo, PeerPins, accept_io, connect_io}; +use yolo_hub_peer_link::{Admission, accept, connect}; +use yolo_hub_peer_protocol::{FIRST_REVISION, PEER_PROTOCOL_VERSION, PeerReply, PeerRequest}; +use yolo_hub_peer_rpc::{Client, Handler, Response, RpcFuture}; +use yolo_hub_state::HubState; +use uuid::Uuid; + +struct Echo; +impl Handler for Echo { + fn request(&self, request: PeerRequest) -> RpcFuture<'_, Response> { + Box::pin(async move { + let PeerRequest::Discover { request_id, .. } = request else { + return Err(yolo_hub_peer_rpc::RpcError::Invalid); + }; + Ok(Response { + reply: PeerReply::Directory { + request_id, + revision: FIRST_REVISION, + origins: Vec::new(), + }, + updates: None, + }) + }) + } +} + +fn discover() -> PeerRequest { + PeerRequest::Discover { + request_id: Uuid::new_v4(), + version: PEER_PROTOCOL_VERSION, + allowed_origins: Vec::new(), + } +} #[tokio::test] -async fn unknown_profile_never_falls_back_to_the_loopback_tcp_endpoint() { +async fn unknown_profile_never_opens_a_remote_socket() { let fixture = Fixture::new(); - let listener = tokio::net::TcpListener::bind((std::net::Ipv4Addr::LOCALHOST, 0)) - .await - .unwrap(); - let address = PeerAddress::ssh( - SshProfileRef::new(PROFILE_ID, DIGEST_CHARACTER.repeat(DIGEST_BYTES)).unwrap(), - listener.local_addr().unwrap(), - ) - .unwrap(); + let root = tempfile::tempdir().unwrap(); + let socket = root.path().join("hub.sock"); + let listener = tokio::net::UnixListener::bind(&socket).unwrap(); + let address = PeerAddress::profile(PROFILE_ID.to_owned(), socket).unwrap(); assert!( dial_peer( fixture.runtime.as_ref(), @@ -35,69 +59,51 @@ async fn unknown_profile_never_falls_back_to_the_loopback_tcp_endpoint() { } #[tokio::test] -async fn registry_removal_closes_the_existing_tunnel_before_the_change_reply() { +async fn registry_removal_closes_the_existing_link_before_the_change_reply() { let fixture = Fixture::new(); let (paths, listener) = broker::bind(&fixture.root.path().join(BROKER_DIRECTORY)); let profile = fixture.profile(paths.state_dir()); fixture.apply(vec![profile.clone()]).await; - let origin = yolo_hub_state::HubState::open( - &fixture.root.path().join(ORIGIN_DIRECTORY), - &fixture.root.path().join(ORIGIN_HOST_DIRECTORY), - ) - .unwrap(); + let origin = Arc::new( + HubState::open( + &fixture.root.path().join(ORIGIN_DIRECTORY), + &fixture.root.path().join(ORIGIN_HOST_DIRECTORY), + ) + .unwrap(), + ); let peer = origin.peer_identity(); - let address = PeerAddress::ssh(profile.profile, ENDPOINT.parse().unwrap()).unwrap(); + let address = PeerAddress::profile(profile.profile.profile_id().to_owned(), SOCKET.into()).unwrap(); let (transport, remote) = tokio::join!( dial_peer(fixture.runtime.as_ref(), peer, &address, deadline()), timeout_at(deadline(), broker::upgrade(&listener, peer)), ); - let (transport, remote) = (transport.unwrap(), remote.unwrap()); - let (client, server) = tokio::join!( - connect_io( - transport, - Arc::new(fixture.state.transport_key().unwrap()), - peer, - deadline() - ), - accept_io( - PeerIo::new(remote, || {}), - Arc::new(origin.transport_key().unwrap()), - PeerPins::new(vec![fixture.state.peer_identity()]).unwrap(), - deadline() - ), - ); - let (client, _) = client.unwrap(); - let (server, mut incoming) = server.unwrap(); - let mut transport = client.open_channel(deadline()).await.unwrap(); - let mut remote = timeout_at(deadline(), incoming.recv()) - .await - .unwrap() - .unwrap(); - transport.write_all(PAYLOAD).await.unwrap(); - let mut bytes = vec![0; PAYLOAD.len()]; - timeout_at(deadline(), remote.read_exact(&mut bytes)) - .await - .unwrap() - .unwrap(); - assert_eq!(bytes, PAYLOAD); - fixture.apply(Vec::new()).await; - let mut one = [0]; - assert_eq!( - timeout_at(deadline(), remote.read(&mut one)) + let local = fixture.state.peer_identity(); + let stream = remote.unwrap(); + let server_origin = origin.clone(); + let served = tokio::spawn(async move { + let (_, stream) = accept(stream, peer, server_origin.as_ref(), Admission::Only(&[local]), deadline()) .await - .unwrap() - .unwrap(), - 0 - ); + .unwrap(); + let _ = yolo_hub_peer_rpc::serve(stream, Arc::new(Echo)).await; + }); + let client: Client = connect( + transport.unwrap(), + local, + fixture.state.as_ref(), + peer, + deadline(), + ) + .await + .unwrap(); + assert!(client.request(discover(), deadline()).await.is_ok()); + fixture.apply(Vec::new()).await; timeout_at(deadline(), async { - while !client.is_closed() { + while client.request(discover(), deadline()).await.is_ok() { tokio::task::yield_now().await; } }) .await .unwrap(); - assert!(client.open_channel(deadline()).await.is_err()); - let _ = client.close(deadline()).await; - let _ = server.close(deadline()).await; + timeout_at(deadline(), served).await.unwrap().unwrap(); paths.verify_state().unwrap(); } diff --git a/core/crates/hub/ssh/profile/runtime/src/dial.rs b/core/crates/hub/ssh/profile/runtime/src/dial.rs index 1f4730ad..049c28b6 100644 --- a/core/crates/hub/ssh/profile/runtime/src/dial.rs +++ b/core/crates/hub/ssh/profile/runtime/src/dial.rs @@ -3,9 +3,8 @@ use anyhow::{Context, Result, ensure}; use tokio::time::Instant; use yolo_broker_hub_protocol::BrokerHubOpenRequest; use yolo_hub_delegation::PeerIdentity; -use yolo_hub_federation_settings::{DialTransport, PeerAddress}; -use yolo_hub_peer_dialer::{PeerDialFuture, PeerDialer, TcpPeerDialer}; -use yolo_hub_peer_transport::PeerIo; +use yolo_hub_federation_settings::PeerAddress; +use yolo_hub_peer_dialer::{PeerDialFuture, PeerDialer, PeerIo}; use yolo_remote_broker::{BrokerClient, BrokerPaths}; impl PeerDialer for SshProfileRuntime { @@ -18,13 +17,11 @@ impl PeerDialer for SshProfileRuntime { Box::pin(async move { self.state.verify().inspect_err(|_| self.close_all())?; address.validate()?; - let DialTransport::Ssh { profile } = &address.transport else { - return TcpPeerDialer.dial(peer, address, deadline).await; - }; + let profile = address.profile_id().context(REGISTRY_CHANGED)?; let changed = self.changed.subscribe(); let registration = self.registration(profile)?; let binding = { - let (reserved, reservation) = self.reserve_profile(profile)?; + let (reserved, reservation) = self.reserve_profile(®istration.profile)?; ensure!(reserved == registration, REGISTRY_CHANGED); self.ensure_broker(&reservation, None, deadline).await? }; @@ -33,7 +30,7 @@ impl PeerDialer for SshProfileRuntime { let client = BrokerClient::new(paths.management_socket()); let request = BrokerHubOpenRequest { binding, - endpoint: address.ssh_endpoint()?, + socket: address.socket().to_path_buf(), peer, }; let tunnel = client.open_hub_peer(request, deadline).await?; @@ -72,10 +69,7 @@ impl PeerDialer for SshProfileRuntime { } impl SshProfileRuntime { - fn registration( - &self, - profile: &yolo_hub_federation_settings::SshProfileRef, - ) -> Result { + fn registration(&self, profile: &str) -> Result { let store = self .store .lock() @@ -86,7 +80,7 @@ impl SshProfileRuntime { .configuration .profiles .into_iter() - .find(|entry| &entry.profile == profile) + .find(|entry| entry.profile.profile_id() == profile) .context(REGISTRY_CHANGED) } } diff --git a/core/crates/hub/ssh/profile/runtime/test_support/broker.rs b/core/crates/hub/ssh/profile/runtime/test_support/broker.rs index 69d50d88..2846a576 100644 --- a/core/crates/hub/ssh/profile/runtime/test_support/broker.rs +++ b/core/crates/hub/ssh/profile/runtime/test_support/broker.rs @@ -38,7 +38,7 @@ pub async fn upgrade(listener: &UnixListener, peer: PeerIdentity) -> UnixStream }; assert_eq!(request.binding, binding); assert_eq!(request.peer, peer); - assert_eq!(request.endpoint.to_string(), ENDPOINT); + assert_eq!(request.socket.to_string_lossy(), SOCKET); reply( &mut tunnel, envelope.request_id, diff --git a/core/crates/hub/ssh/profile/runtime/test_support/constants.rs b/core/crates/hub/ssh/profile/runtime/test_support/constants.rs index 2e3ecdc5..1923f6ea 100644 --- a/core/crates/hub/ssh/profile/runtime/test_support/constants.rs +++ b/core/crates/hub/ssh/profile/runtime/test_support/constants.rs @@ -12,6 +12,6 @@ pub const CHANNEL: u64 = 1; pub const SOCKET_MODE: u32 = 0o600; pub const DIRECTORY_MODE: u32 = 0o700; pub const REPLACED_REGISTRY: &str = "replaced-registry"; -pub const ENDPOINT: &str = "127.0.0.1:4242"; +pub const SOCKET: &str = "/run/user/1000/yolo/hub/peer.sock"; pub const PAYLOAD: &[u8] = b"hub-tunnel-payload"; pub const TIMEOUT: Duration = Duration::from_secs(5); diff --git a/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_constants.rs b/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_constants.rs index f974e503..9dab725f 100644 --- a/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_constants.rs +++ b/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_constants.rs @@ -7,4 +7,4 @@ pub const PEER_STATE_DIRECTORY: &str = "lifecycle-peer"; pub const PEER_HOST_DIRECTORY: &str = "lifecycle-peer-host"; pub const PENDING_RESPONSE: &str = "registry read unexpectedly yielded before rejecting lost authority"; -pub const CLOSURE_OBSERVATION: std::time::Duration = std::time::Duration::from_millis(250); +pub const CLOSURE_OBSERVATION: std::time::Duration = std::time::Duration::from_secs(2); diff --git a/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_tunnels.rs b/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_tunnels.rs index e3fe0e34..bfabbd85 100644 --- a/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_tunnels.rs +++ b/core/crates/hub/ssh/profile/runtime/test_support/lifecycle_tunnels.rs @@ -6,31 +6,65 @@ use super::super::{ }; use std::sync::Arc; use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, net::UnixListener, + task::JoinHandle, time::{timeout, timeout_at}, }; use yolo_hub_federation_settings::{PeerAddress, SshProfileRef}; use yolo_hub_peer_dialer::dial_peer; -use yolo_hub_peer_transport::{ - PeerConnection, PeerIo, PeerPins, PeerStream, accept_io, connect_io, -}; +use yolo_hub_peer_link::{Admission, accept, connect}; +use yolo_hub_peer_protocol::{FIRST_REVISION, PEER_PROTOCOL_VERSION, PeerReply, PeerRequest}; +use yolo_hub_peer_rpc::{Client, Handler, Response, RpcFuture}; use yolo_hub_state::HubState; use yolo_remote_broker::BrokerPaths; +use uuid::Uuid; + +struct Echo; +impl Handler for Echo { + fn request(&self, request: PeerRequest) -> RpcFuture<'_, Response> { + Box::pin(async move { + let PeerRequest::Discover { request_id, .. } = request else { + return Err(yolo_hub_peer_rpc::RpcError::Invalid); + }; + Ok(Response { + reply: PeerReply::Directory { + request_id, + revision: FIRST_REVISION, + origins: Vec::new(), + }, + updates: None, + }) + }) + } +} + +fn discover() -> PeerRequest { + PeerRequest::Discover { + request_id: Uuid::new_v4(), + version: PEER_PROTOCOL_VERSION, + allowed_origins: Vec::new(), + } +} + +async fn responds(client: &Client) -> bool { + client.request(discover(), deadline()).await.is_ok() +} pub struct Tunnels { - connections: Vec<(PeerConnection, PeerConnection)>, - _streams: Vec<(PeerStream, PeerStream)>, + clients: Vec, + tasks: Vec>, _brokers: Vec<(BrokerPaths, UnixListener)>, - _peer: HubState, + _peer: Arc, } impl Tunnels { pub async fn open(fixture: &Fixture) -> Self { - let peer = HubState::open( - &fixture.root.path().join(PEER_STATE_DIRECTORY), - &fixture.root.path().join(PEER_HOST_DIRECTORY), - ) - .unwrap(); + let peer = Arc::new( + HubState::open( + &fixture.root.path().join(PEER_STATE_DIRECTORY), + &fixture.root.path().join(PEER_HOST_DIRECTORY), + ) + .unwrap(), + ); let brokers = (0..TUNNEL_COUNT) .map(|index| { broker::bind( @@ -55,79 +89,72 @@ impl Tunnels { }) .collect::>(); fixture.apply(profiles.clone()).await; - let mut connections = Vec::new(); - let mut streams = Vec::new(); + let mut clients = Vec::new(); + let mut tasks = Vec::new(); for (profile, (_, listener)) in profiles.into_iter().zip(&brokers) { - let address = PeerAddress::ssh(profile.profile, ENDPOINT.parse().unwrap()).unwrap(); + let address = PeerAddress::profile(profile.profile.profile_id().to_owned(), SOCKET.into()).unwrap(); let remote = peer.peer_identity(); let (opened, accepted) = tokio::join!( dial_peer(fixture.runtime.as_ref(), remote, &address, deadline()), timeout_at(deadline(), broker::upgrade(listener, remote)), ); - let (client, server) = tokio::join!( - connect_io( - opened.unwrap(), - Arc::new(fixture.state.transport_key().unwrap()), - remote, - deadline() - ), - accept_io( - PeerIo::new(accepted.unwrap(), || {}), - Arc::new(peer.transport_key().unwrap()), - PeerPins::new(vec![fixture.state.peer_identity()]).unwrap(), - deadline() - ), - ); - let (client, _) = client.unwrap(); - let (server, mut incoming) = server.unwrap(); - assert_eq!(client.peer(), remote); - assert_eq!(server.peer(), fixture.state.peer_identity()); - let mut stream = client.open_channel(deadline()).await.unwrap(); - let mut accepted = timeout_at(deadline(), incoming.recv()) - .await - .unwrap() - .unwrap(); - stream.write_all(PAYLOAD).await.unwrap(); - let mut bytes = vec![0; PAYLOAD.len()]; - timeout_at(deadline(), accepted.read_exact(&mut bytes)) - .await - .unwrap() - .unwrap(); - assert_eq!(bytes, PAYLOAD); - assert!(!client.is_closed() && !server.is_closed()); - connections.push((client, server)); - streams.push((stream, accepted)); + let local = fixture.state.peer_identity(); + let stream = accepted.unwrap(); + let server_peer = peer.clone(); + let handler = Arc::new(Echo); + tasks.push(tokio::spawn(async move { + let (_, stream) = accept(stream, remote, &*server_peer, Admission::Only(&[local]), deadline()) + .await + .unwrap(); + let _ = yolo_hub_peer_rpc::serve(stream, handler).await; + })); + let client = connect( + opened.unwrap(), + local, + fixture.state.as_ref(), + remote, + deadline(), + ) + .await + .unwrap(); + assert!(responds(&client).await); + clients.push(client); } Self { - connections, - _streams: streams, + clients, + tasks, _brokers: brokers, _peer: peer, } } - /// Socket shutdown is synchronous; russh publishes handler closure asynchronously. - pub async fn require_closed(&self) { + /// Tunnel shutdown is synchronous; RPC closure becomes observable asynchronously. + pub async fn require_closed(&mut self) { timeout(CLOSURE_OBSERVATION, async { - while self - .connections - .iter() - .any(|(local, remote)| !local.is_closed() || !remote.is_closed()) - { + loop { + let mut live = false; + for client in &self.clients { + if responds(client).await { + live = true; + break; + } + } + if !live { + break; + } tokio::task::yield_now().await; } }) .await .unwrap(); - for (local, remote) in &self.connections { - assert!(local.open_channel(deadline()).await.is_err()); - assert!(remote.open_channel(deadline()).await.is_err()); + for task in &mut self.tasks { + timeout(CLOSURE_OBSERVATION, task) + .await + .expect("peer RPC server did not observe tunnel closure") + .expect("peer RPC server task failed"); } } - pub async fn finish(&self) { - for (local, remote) in &self.connections { - let (left, right) = tokio::join!(local.close(deadline()), remote.close(deadline())); - left.unwrap(); - right.unwrap(); - } + pub async fn finish(&mut self) { + self.tasks.clear(); + self.clients.clear(); } } diff --git a/core/crates/hub/state/src/constants.rs b/core/crates/hub/state/src/constants.rs index 5d0d70ce..ba2bb54c 100644 --- a/core/crates/hub/state/src/constants.rs +++ b/core/crates/hub/state/src/constants.rs @@ -1,6 +1,9 @@ pub const HUB_SOCKET_FILE: &str = yolo_paths::constants::HUB_SOCKET_FILE_NAME; pub const HUB_IDENTITY_FILE: &str = "identity.json"; +pub const PEER_SOCKET_FILE: &str = "peer.sock"; pub const STATE_CONTRACT_FILE: &str = "runtime-contract.json"; +#[cfg(test)] +pub(crate) const HUB_PROTOCOL_FIELD: &str = "hub_protocol"; pub(crate) const HUB_LOCK_FILE: &str = "hub.lock"; pub(crate) const STATE_SCHEMA_VERSION: u16 = 2; pub(crate) const MAX_LOCK_BYTES: u64 = 4_096; diff --git a/core/crates/hub/state/src/contract.rs b/core/crates/hub/state/src/contract.rs index 6306f117..0133ae9e 100644 --- a/core/crates/hub/state/src/contract.rs +++ b/core/crates/hub/state/src/contract.rs @@ -16,35 +16,52 @@ struct Contract { state_schema: u16, } -pub fn open_or_validate(namespace: &NamespaceLease, path: &Path, fresh: bool) -> Result<()> { - let file = path.join(STATE_CONTRACT_FILE); +pub fn open_or_migrate(namespace: &NamespaceLease, path: &Path, fresh: bool) -> Result<()> { if fresh { - return namespace.write_json( - &file, - &Contract { - schema: CONTRACT_SCHEMA, - hub_protocol: PUBLIC_HUB_PROTOCOL_VERSION, - state_schema: STATE_SCHEMA_VERSION, - }, - ); + return write_contract(namespace, path); + } + let contract = read(namespace, path)?; + validate(&contract)?; + if contract.hub_protocol < PUBLIC_HUB_PROTOCOL_VERSION { + write_contract(namespace, path)?; } - let contract: Contract = namespace - .read_json(&file) - .map_err(|_| anyhow::anyhow!(INCOMPATIBLE))?; - ensure!(contract.schema == CONTRACT_SCHEMA, INCOMPATIBLE); - ensure!( - contract.hub_protocol == PUBLIC_HUB_PROTOCOL_VERSION, - INCOMPATIBLE - ); - ensure!(contract.state_schema == STATE_SCHEMA_VERSION, INCOMPATIBLE); Ok(()) } /// Validate a retained state contract without taking the Hub singleton lock. /// Upgrade admission uses this read-only probe before it considers replacing a -/// service. It intentionally never creates, rewrites, or migrates state. +/// service. It accepts older Hub protocol versions but never rewrites state. pub fn validate_path(path: &Path) -> Result<()> { let directory = yolo_paths::directory::PrivateDirectory::open(path)?; let namespace = NamespaceLease::from_directories(vec![(STATE_NAMESPACE, directory)])?; - open_or_validate(&namespace, path, false) + validate(&read(&namespace, path)?) +} + +fn read(namespace: &NamespaceLease, path: &Path) -> Result { + namespace + .read_json(&path.join(STATE_CONTRACT_FILE)) + .map_err(|_| anyhow::anyhow!(INCOMPATIBLE)) +} + +fn write_contract(namespace: &NamespaceLease, path: &Path) -> Result<()> { + namespace.write_json( + &path.join(STATE_CONTRACT_FILE), + &Contract { + schema: CONTRACT_SCHEMA, + hub_protocol: PUBLIC_HUB_PROTOCOL_VERSION, + state_schema: STATE_SCHEMA_VERSION, + }, + ) +} + +fn validate(contract: &Contract) -> Result<()> { + ensure!(contract.schema == CONTRACT_SCHEMA, INCOMPATIBLE); + ensure!(contract.state_schema == STATE_SCHEMA_VERSION, INCOMPATIBLE); + // An older protocol is an upgrade; a newer one means this image is older + // than the state and must not interpret it. + ensure!( + contract.hub_protocol <= PUBLIC_HUB_PROTOCOL_VERSION, + INCOMPATIBLE + ); + Ok(()) } diff --git a/core/crates/hub/state/src/lib.rs b/core/crates/hub/state/src/lib.rs index 2c7bc4ce..4e120028 100644 --- a/core/crates/hub/state/src/lib.rs +++ b/core/crates/hub/state/src/lib.rs @@ -13,7 +13,7 @@ use yolo_paths::namespace_lease::NamespaceLease; use yolo_paths::{coordination::CoordinationLock, directory::PrivateDirectory}; use constants::*; -pub use constants::{HUB_IDENTITY_FILE, HUB_SOCKET_FILE}; +pub use constants::{HUB_IDENTITY_FILE, HUB_SOCKET_FILE, PEER_SOCKET_FILE, STATE_CONTRACT_FILE}; /// Read-only admission probe used by the upgrade coordinator. pub fn validate_runtime_contract(path: &Path) -> Result<()> { @@ -69,7 +69,7 @@ impl HubState { record.verify(&binding, &host_home)?; record }; - contract::open_or_validate(&namespace, &path, fresh)?; + contract::open_or_migrate(&namespace, &path, fresh)?; let key = Ed25519KeyPair::from_pkcs8(&record.key).map_err(|_| anyhow::anyhow!(KEY_FAILURE))?; let socket_namespace = yolo_paths::Paths::new(&path).retain_socket_namespace()?; @@ -121,6 +121,11 @@ impl HubState { self.verify()?; self.namespace.directory(&self.path) } + /// The peer listener is derived from Hub state and reached only through SSH. + pub fn peer_socket(&self) -> Result { + self.verify()?; + Ok(self.directory()?.path().join(PEER_SOCKET_FILE)) + } pub fn verify(&self) -> Result<()> { self.lock.verify()?; self._host_owner.verify()?; diff --git a/core/crates/hub/state/unit_tests/lib.rs b/core/crates/hub/state/unit_tests/lib.rs index 358bc564..102582ab 100644 --- a/core/crates/hub/state/unit_tests/lib.rs +++ b/core/crates/hub/state/unit_tests/lib.rs @@ -145,3 +145,48 @@ fn symlinked_identity_and_relative_paths_are_rejected() { .unwrap(); assert!(HubState::open(directory.path(), &host).is_err()); } + +fn rewrite_contract(state: &HubState, path: &Path, update: impl FnOnce(&mut serde_json::Value)) { + let file = path.join(crate::constants::STATE_CONTRACT_FILE); + let mut contract: serde_json::Value = state.namespace.read_json(&file).unwrap(); + update(&mut contract); + state.namespace.write_json(&file, &contract).unwrap(); +} + +#[test] +fn older_protocol_contract_is_validated_read_only_and_migrated_on_open() { + let (_root, path, host) = fixture(); + let state = HubState::open(&path, &host).unwrap(); + let current = yolo_hub_rpc_protocol::HUB_PROTOCOL_VERSION; + rewrite_contract(&state, &path, |contract| { + contract[crate::constants::HUB_PROTOCOL_FIELD] = serde_json::json!(u16::MIN); + }); + let file = path.join(crate::constants::STATE_CONTRACT_FILE); + let before = std::fs::read(&file).unwrap(); + drop(state); + validate_runtime_contract(&path).unwrap(); + assert_eq!(std::fs::read(&file).unwrap(), before); + let state = HubState::open(&path, &host).unwrap(); + let contract: serde_json::Value = state.namespace.read_json(&file).unwrap(); + assert_eq!( + contract[crate::constants::HUB_PROTOCOL_FIELD], + serde_json::json!(current) + ); +} + +#[test] +fn newer_protocol_or_state_schema_is_rejected() { + for (field, value) in [ + (crate::constants::HUB_PROTOCOL_FIELD, u16::MAX), + ("state_schema", u16::MAX), + ] { + let (_root, path, host) = fixture(); + let state = HubState::open(&path, &host).unwrap(); + rewrite_contract(&state, &path, |contract| { + contract[field] = serde_json::json!(value); + }); + drop(state); + assert!(validate_runtime_contract(&path).is_err()); + assert!(HubState::open(&path, &host).is_err()); + } +} diff --git a/core/crates/runtime/engine/process/src/scoped/scoped_waitable_child.rs b/core/crates/runtime/engine/process/src/scoped/scoped_waitable_child.rs index 177a7651..073d49a2 100644 --- a/core/crates/runtime/engine/process/src/scoped/scoped_waitable_child.rs +++ b/core/crates/runtime/engine/process/src/scoped/scoped_waitable_child.rs @@ -87,7 +87,7 @@ impl ScopedWaitableChild { self.custody_mut().take_stdout() } - pub(crate) fn take_stdin(&mut self) -> Option { + pub fn take_stdin(&mut self) -> Option { self.custody_mut().take_stdin() } diff --git a/core/crates/runtime/engine/worker/src/codex_hook/handlers/control_source.rs b/core/crates/runtime/engine/worker/src/codex_hook/handlers/control_source.rs index e52a27f1..db675609 100644 --- a/core/crates/runtime/engine/worker/src/codex_hook/handlers/control_source.rs +++ b/core/crates/runtime/engine/worker/src/codex_hook/handlers/control_source.rs @@ -15,7 +15,7 @@ pub(in crate::worker) fn with_control_source( state: &WorkerState, durable: &SessionDurableMutationGuard<'_>, expected: Option<&NativeControlWitness>, - operation: impl FnOnce(&AgentBinding, &NativeControlWitness, &dyn Fn() -> Result<()>) -> Result, + operation: impl FnOnce(&AgentBinding, &NativeControlWitness, Option<&str>, &dyn Fn() -> Result<()>) -> Result, ) -> Result { durable.require_no_pending_intent()?; let runtime = lock_recover(&state.record).clone(); @@ -48,9 +48,11 @@ pub(in crate::worker) fn with_control_source( state .codex_hook_ingress .with_control_route(&authority, |entry| { - entry - .runtime() - .with_control_authority(entry, &authority, state.agent_registry.as_ref(), |lineage, verify_runtime| { + entry.runtime().with_control_authority( + entry, + &authority, + state.agent_registry.as_ref(), + |lineage, stop_turn, verify_runtime| { let process = authority.provider_process(); ensure!( crate::process::process_identity_relation(process.pid, process.start_time) @@ -113,7 +115,8 @@ pub(in crate::worker) fn with_control_source( durable.namespace().verify() }; verify()?; - operation(&binding, &witness, &verify) - }) + operation(&binding, &witness, stop_turn, &verify) + }, + ) }) } diff --git a/core/crates/runtime/engine/worker/src/codex_hook/mod.rs b/core/crates/runtime/engine/worker/src/codex_hook/mod.rs index a1afb6ac..3a117681 100644 --- a/core/crates/runtime/engine/worker/src/codex_hook/mod.rs +++ b/core/crates/runtime/engine/worker/src/codex_hook/mod.rs @@ -80,8 +80,8 @@ mod runtime; #[path = "server/server.rs"] mod server; -pub(in crate::worker) use ingress_supervisor::CodexHookIngressSupervisor; pub(in crate::worker) use control_source::with_control_source; +pub(in crate::worker) use ingress_supervisor::CodexHookIngressSupervisor; #[cfg(test)] pub(in crate::worker) use projection::{ ProjectionRoundProbe, complete_task_for_test as complete_projection_task_for_test, @@ -89,7 +89,8 @@ pub(in crate::worker) use projection::{ supervise_closed_for_test as supervise_closed_projection_for_test, }; pub(in crate::worker) use projection::{ - invalidate_for_shutdown as invalidate_projection_for_shutdown, spawn as spawn_projection, + invalidate_for_shutdown as invalidate_projection_for_shutdown, read_stop_turn_end, + spawn as spawn_projection, }; #[cfg(all(test, feature = "yolo-integration-tests"))] diff --git a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection.rs b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection.rs index adc8d8e9..6169ca30 100644 --- a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection.rs +++ b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection.rs @@ -1,9 +1,10 @@ #[path = "projection_binding.rs"] mod binding; -#[path = "projection_cursor.rs"] -mod cursor; #[path = "projection_control.rs"] mod control; +#[path = "projection_cursor.rs"] +mod cursor; +pub(in crate::worker) use control::read_stop_turn_end; pub(in crate::worker::codex_hook) use control::require_current_control_projection; #[cfg(all(test, feature = "yolo-unit-tests"))] #[cfg(any(feature = "yolo-integration-tests", feature = "yolo-worker-unit-runtime"))] diff --git a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_binding.rs b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_binding.rs index 49207ff1..c1da5bcb 100644 --- a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_binding.rs +++ b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_binding.rs @@ -18,6 +18,19 @@ pub(super) struct ProjectedBinding { pub(super) version: CodexAuthorityVersion, } +impl ProjectedBinding { + pub(super) fn settle_stop(&mut self, previous: Option<&AgentBinding>) { + self.binding.state = AgentState::WaitingForInput; + if let Some(previous) = previous.filter(|previous| same_projection(previous, &self.binding)) { + self.binding.updated_at_ms = previous.updated_at_ms; + } + } +} + +pub(super) fn stop_turn<'a>(lifecycle: &'a NormalizedLifecycle, binding: &AgentBinding) -> Option<&'a str> { + (binding.state == AgentState::Active).then(|| lifecycle.stop_candidate.as_ref()?.turn_id.as_deref()).flatten() +} + pub(super) enum ProjectionMode { Active, Exhausted, @@ -28,7 +41,8 @@ pub(super) fn project( lifecycle: &NormalizedLifecycle, authority_revision: u64, mode: ProjectionMode, - previous: Option<&AgentBinding>, registry: &AgentRegistry, + previous: Option<&AgentBinding>, + registry: &AgentRegistry, ) -> Option { let record = route.admission.record(); let request = record.request(); diff --git a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_control.rs b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_control.rs index 6d751f44..9ec321c0 100644 --- a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_control.rs +++ b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_control.rs @@ -4,28 +4,25 @@ use super::{ }; use crate::agent::codex::authority::{CodexAuthorityStatus, CodexBindingAuthority}; use crate::agent::hooks::NormalizedLifecycle; -use crate::agent::AgentRegistry; +use crate::agent::{AgentBinding, AgentRegistry, AgentState}; +use crate::worker::WorkerState; use crate::worker::codex_hook::router::CodexRouteEntry; use anyhow::{Context, Result, ensure}; +use yolo_agent_control_contract::{NativeTurnEnd, NativeTurnEndKind}; const STALE: &str = "Codex control projection does not match current source authority"; -pub(in crate::worker::codex_hook) fn require_current_control_projection( - entry: &CodexRouteEntry, - authority: &CodexBindingAuthority, - lifecycle: &NormalizedLifecycle, - revision: u64, - registry: &AgentRegistry, -) -> Result<()> { +pub(in crate::worker::codex_hook) fn require_current_control_projection<'a>(entry: &CodexRouteEntry, authority: &CodexBindingAuthority, lifecycle: &'a NormalizedLifecycle, revision: u64, registry: &AgentRegistry) -> Result> { let route = ProjectionRoute { revision: authority.version.route_revision, key: entry.key(), admission: entry.admission().clone(), }; - let projected = binding::project( - &route, lifecycle, revision, ProjectionMode::Active, Some(&authority.binding), registry, - ) - .context(STALE)?; + let mut projected = binding::project(&route, lifecycle, revision, ProjectionMode::Active, Some(&authority.binding), registry).context(STALE)?; + let stop_turn = binding::stop_turn(lifecycle, &projected.binding); + if stop_turn.is_some() && authority.binding.state == AgentState::WaitingForInput { + projected.settle_stop(Some(&authority.binding)); + } ensure!( projected.status == CodexAuthorityStatus::Active && projected.version == authority.version @@ -33,5 +30,17 @@ pub(in crate::worker::codex_hook) fn require_current_control_projection( && projected.binding == authority.binding, STALE ); - Ok(()) + Ok(stop_turn) +} + +pub(in crate::worker) fn read_stop_turn_end(state: &WorkerState, binding: &AgentBinding, turn_id: &str) -> Option { + let probe = state.provider_runtime.get(&binding.kind)?.turn_end_probe()?; + let end = probe.read_turn_end(binding).ok()??; + (end.turn_id == turn_id).then_some(NativeTurnEnd { + turn_id: end.turn_id, + kind: match end.kind { + yolo_agent_providers::TurnEndKind::Completed => NativeTurnEndKind::Completed, + yolo_agent_providers::TurnEndKind::Aborted => NativeTurnEndKind::Aborted, + }, + }) } diff --git a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_publication.rs b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_publication.rs index 6261a2c9..8407e67e 100644 --- a/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_publication.rs +++ b/core/crates/runtime/engine/worker/src/codex_hook/projection/projection_publication.rs @@ -32,22 +32,22 @@ pub(super) async fn publish_lifecycle( let current = authority_store.load_in_namespace(state.namespace.lease())?; let version = cursor::ProjectionCursor::lifecycle_version(route, lifecycle, authority_revision); let previous = replay_base(current.as_ref(), version, before.as_ref()); - let Some(mut projected) = binding::project( - route, lifecycle, authority_revision, mode, previous, state.agent_registry.as_ref(), - ) else { + let Some(mut projected) = binding::project(route, lifecycle, authority_revision, mode, previous, state.agent_registry.as_ref()) else { return Ok(false); }; - if projected.binding.state == AgentState::Active - && let Some(stop) = lifecycle.stop_candidate.as_ref() - && let Some(turn_id) = stop.turn_id.as_deref() - && let Some(probe) = state.provider_runtime.get(&projected.binding.kind).and_then(|runtime| runtime.turn_end_probe()) - { - for _ in 0..STOP_SETTLEMENT_ATTEMPTS { - if matches!(probe.read_turn_end(&projected.binding), Ok(Some(end)) if end.turn_id == turn_id) { - projected.binding.state = AgentState::WaitingForInput; - break; + if let Some(turn_id) = binding::stop_turn(lifecycle, &projected.binding) { + if let Some(replay) = current.as_ref().filter(|a| a.version == version && a.status == CodexAuthorityStatus::Active) { + if replay.binding.state == AgentState::WaitingForInput { + projected.settle_stop(previous); + } + } else { + for _ in 0..STOP_SETTLEMENT_ATTEMPTS { + if super::control::read_stop_turn_end(state, &projected.binding, turn_id).is_some() { + projected.settle_stop(previous); + break; + } + tokio::time::sleep(STOP_SETTLEMENT_DELAY).await; } - tokio::time::sleep(STOP_SETTLEMENT_DELAY).await; } } let runtime = runtime_identity(&expected); @@ -71,13 +71,7 @@ pub(super) async fn publish_lifecycle( &authority, || publication_fence.verify(), )?; - ensure!( - store - .read_raw_in_namespace(state.namespace.lease())? - .as_ref() - == Some(&projected.binding), - super::super::constants::CODEX_PROJECTION_BINDING_CHANGED - ); + ensure!(store.read_raw_in_namespace(state.namespace.lease())?.as_ref() == Some(&projected.binding), super::super::constants::CODEX_PROJECTION_BINDING_CHANGED); publish_event(state, before.as_ref(), projected.binding); Ok(true) } @@ -99,13 +93,7 @@ pub(super) async fn invalidate(state: &WorkerState) -> Result { let Some(authority) = authority else { return Ok(false); }; - ensure!( - store - .read_raw_in_namespace(state.namespace.lease())? - .as_ref() - == Some(&authority.binding), - super::super::constants::CODEX_PROJECTION_BINDING_CHANGED - ); + ensure!(store.read_raw_in_namespace(state.namespace.lease())?.as_ref() == Some(&authority.binding), super::super::constants::CODEX_PROJECTION_BINDING_CHANGED); publish_event(state, before.as_ref(), authority.binding); Ok(true) } @@ -172,23 +160,17 @@ fn publish_event(state: &WorkerState, before: Option<&AgentBinding>, after: Agen } fn same_subject(left: &AgentBinding, right: &AgentBinding) -> bool { - left.kind == AgentKind::Codex - && right.kind == AgentKind::Codex - && left.terminal_id == right.terminal_id - && left.provider_session_id == right.provider_session_id - && left.executable == right.executable - && left.cwd == right.cwd - && left.evidence_path == right.evidence_path - && left.data_home == right.data_home + left.kind == AgentKind::Codex && right.kind == AgentKind::Codex && + left.terminal_id == right.terminal_id && left.provider_session_id == right.provider_session_id && + left.executable == right.executable && left.cwd == right.cwd && + left.evidence_path == right.evidence_path && left.data_home == right.data_home } #[cfg(test)] -#[cfg(any(not(test), feature = "yolo-unit-tests"))] #[cfg(any(feature = "yolo-integration-tests", feature = "yolo-worker-unit-runtime"))] #[path = "../../../unit_tests/codex_hook/projection/projection_publication_order_tests.rs"] mod order_tests; #[cfg(test)] -#[cfg(any(not(test), feature = "yolo-unit-tests"))] #[cfg(any(feature = "yolo-integration-tests", feature = "yolo-worker-unit-runtime"))] #[path = "../../../unit_tests/codex_hook/projection/projection_publication_revocation_tests.rs"] mod revocation_tests; diff --git a/core/crates/runtime/engine/worker/src/codex_hook/runtime/runtime_control.rs b/core/crates/runtime/engine/worker/src/codex_hook/runtime/runtime_control.rs index 00435b45..2c5ef85c 100644 --- a/core/crates/runtime/engine/worker/src/codex_hook/runtime/runtime_control.rs +++ b/core/crates/runtime/engine/worker/src/codex_hook/runtime/runtime_control.rs @@ -1,6 +1,6 @@ use super::CodexHookRuntime; use crate::agent::codex::authority::CodexBindingAuthority; -use crate::agent::hooks::{InstrumentationState, ProcessState, HookProviderKind}; +use crate::agent::hooks::{HookProviderKind, InstrumentationState, ProcessState}; use crate::codex_hooks::CodexSnapshotLineage; use crate::worker::codex_hook::{ projection, publication::CodexAuthorityView, router::CodexRouteEntry, @@ -18,7 +18,7 @@ impl CodexHookRuntime { entry: &CodexRouteEntry, authority: &CodexBindingAuthority, registry: &crate::agent::AgentRegistry, - operation: impl FnOnce(&CodexSnapshotLineage, &dyn Fn() -> Result<()>) -> Result, + operation: impl FnOnce(&CodexSnapshotLineage, Option<&str>, &dyn Fn() -> Result<()>) -> Result, ) -> Result { self.registration.with_control_authority(|lifecycle| { let owner = &entry.admission().record().request().identity; @@ -47,8 +47,12 @@ impl CodexHookRuntime { INVALID ); let proof = lifecycle.prove_snapshot_current()?; - projection::require_current_control_projection( - entry, authority, current, reducer.revision(), registry, + let stop_turn = projection::require_current_control_projection( + entry, + authority, + current, + reducer.revision(), + registry, )?; let lineage = lifecycle.snapshot_lineage()?; let verify = || -> Result<()> { @@ -58,7 +62,7 @@ impl CodexHookRuntime { verify()?; // The installed registration, lifecycle mutex, and durable snapshot // proof remain live through this synchronous effect callback. - operation(&lineage, &verify) + operation(&lineage, stop_turn, &verify) }) } } diff --git a/core/crates/runtime/engine/worker/src/native_control/execution.rs b/core/crates/runtime/engine/worker/src/native_control/execution.rs index 7e69e760..20994c2a 100644 --- a/core/crates/runtime/engine/worker/src/native_control/execution.rs +++ b/core/crates/runtime/engine/worker/src/native_control/execution.rs @@ -66,7 +66,7 @@ impl WorkerState { self, fence.durable(), Some(&request.witness), - |binding, witness, verify_source| { + |binding, witness, _, verify_source| { fence.verify()?; verify_source()?; let delivery = self diff --git a/core/crates/runtime/engine/worker/src/native_control/observation.rs b/core/crates/runtime/engine/worker/src/native_control/observation.rs index 5401e470..fe081c48 100644 --- a/core/crates/runtime/engine/worker/src/native_control/observation.rs +++ b/core/crates/runtime/engine/worker/src/native_control/observation.rs @@ -2,13 +2,12 @@ use super::{ NativeControlLookupRequest, NativeControlResult, NativeObservationRequest, constants::*, outcome_for_receipt, source, }; -use crate::agent::AgentState; use crate::protocol::ErrorBody; use crate::worker::{ AuthenticatedController, WorkerState, agent_monitor_publication_fence, lock_recover, }; use std::sync::Arc; -use yolo_agent_control_contract::{NativeControlObservation, NativeTurnEnd, NativeTurnEndKind}; +use yolo_agent_control_contract::NativeControlObservation; use yolo_agent_control_journal::JournalError; impl WorkerState { @@ -33,25 +32,18 @@ impl WorkerState { let Ok(fence) = agent_monitor_publication_fence::try_acquire(self, &record) else { return Ok(None); }; - let observed = - source::with_source(self, fence.durable(), None, |binding, witness, verify| { + let observed = source::with_source( + self, + fence.durable(), + None, + |binding, witness, stop_turn, verify| { anyhow::ensure!( witness.fields().binding_digest == request.binding_digest, SOURCE_CHANGED ); fence.verify()?; verify()?; - // Readiness is a provider fact; terminal mode is a submit-time - // transport precondition, never a capability. Only a stop - // candidate needs the structured turn-ending record. - let (ready, turn_end) = match binding.state { - AgentState::WaitingForInput => (true, None), - AgentState::Active => { - let end = turn_end(self, binding); - (end.is_some(), end) - } - _ => (false, None), - }; + let (ready, turn_end) = super::prompt_readiness::observe(self, binding, stop_turn); let interaction = self.observed_interaction(witness); Ok(NativeControlObservation::new_with_interaction( binding.clone(), @@ -60,26 +52,12 @@ impl WorkerState { turn_end, interaction, )?) - }); + }, + ); Ok(observed.ok()) } } -fn turn_end(state: &WorkerState, binding: &crate::agent::AgentBinding) -> Option { - let probe = state - .provider_runtime - .get(&binding.kind)? - .turn_end_probe()?; - let end = probe.read_turn_end(binding).ok()??; - Some(NativeTurnEnd { - turn_id: end.turn_id, - kind: match end.kind { - yolo_agent_providers::TurnEndKind::Completed => NativeTurnEndKind::Completed, - yolo_agent_providers::TurnEndKind::Aborted => NativeTurnEndKind::Aborted, - }, - }) -} - impl WorkerState { pub(in crate::worker) async fn native_control_receipt( self: &Arc, diff --git a/core/crates/runtime/engine/worker/src/native_control/prompt_readiness.rs b/core/crates/runtime/engine/worker/src/native_control/prompt_readiness.rs index e3328751..521623cf 100644 --- a/core/crates/runtime/engine/worker/src/native_control/prompt_readiness.rs +++ b/core/crates/runtime/engine/worker/src/native_control/prompt_readiness.rs @@ -1,8 +1,21 @@ use super::constants::SOURCE_CHANGED; -use crate::agent::{AgentBinding, AgentRecoveryAvailability, AgentState}; +use crate::agent::{AgentBinding, AgentState}; use crate::worker::{WorkerState, lock_recover, process, run_process_query}; -use anyhow::{Result, anyhow, bail, ensure}; -use yolo_agent_control_contract::{NativeControlWitness, NativeTurnEnd, NativeTurnEndKind}; +use anyhow::{Result, ensure}; +use yolo_agent_control_contract::{NativeControlWitness, NativeTurnEnd}; + +pub(super) fn observe( + state: &WorkerState, + binding: &AgentBinding, + stop_turn: Option<&str>, +) -> (bool, Option) { + if binding.state == AgentState::WaitingForInput { + return (true, None); + } + let end = stop_turn + .and_then(|turn| crate::worker::codex_hook::read_stop_turn_end(state, binding, turn)); + (end.is_some(), end) +} /// Read-only PTY discovery scope; each use re-observes foreground membership. pub(super) struct PromptInputProof { @@ -22,45 +35,11 @@ impl WorkerState { } impl PromptInputProof { - /// `Ok(None)` means the PTY is idle. `Ok(Some(end))` means the binding is - /// still a Stop candidate but the provider turn journal proves the turn - /// ended. Any error means input is not ready. - pub(super) fn verify( + pub(super) fn verify_pty( &self, state: &WorkerState, - binding: &AgentBinding, witness: &NativeControlWitness, - ) -> Result> { - self.verify_pty(state, witness)?; - if binding.state == AgentState::WaitingForInput { - return Ok(None); - } - let provider = state - .provider_runtime - .get(&binding.kind) - .ok_or_else(|| anyhow!(SOURCE_CHANGED))?; - let probe = provider - .turn_end_probe() - .ok_or_else(|| anyhow!(SOURCE_CHANGED))?; - if binding.recovery_availability != AgentRecoveryAvailability::Available - || !matches!(binding.state, AgentState::Active | AgentState::Running) - { - bail!(SOURCE_CHANGED); - } - let end = probe - .read_turn_end(binding) - .map_err(|_| anyhow!(SOURCE_CHANGED))? - .ok_or_else(|| anyhow!(SOURCE_CHANGED))?; - Ok(Some(NativeTurnEnd { - turn_id: end.turn_id, - kind: match end.kind { - yolo_agent_providers::TurnEndKind::Completed => NativeTurnEndKind::Completed, - yolo_agent_providers::TurnEndKind::Aborted => NativeTurnEndKind::Aborted, - }, - })) - } - - fn verify_pty(&self, state: &WorkerState, witness: &NativeControlWitness) -> Result<()> { + ) -> Result<()> { ensure!( state.foreground_process_group()? == self.group_id, SOURCE_CHANGED diff --git a/core/crates/runtime/engine/worker/src/native_control/respond.rs b/core/crates/runtime/engine/worker/src/native_control/respond.rs index b0016706..3bf4b9a3 100644 --- a/core/crates/runtime/engine/worker/src/native_control/respond.rs +++ b/core/crates/runtime/engine/worker/src/native_control/respond.rs @@ -62,7 +62,7 @@ impl WorkerState { self, fence.durable(), Some(&request.witness), - |_binding, witness, verify_source| { + |_binding, witness, _, verify_source| { fence.verify()?; verify_source()?; self.authorize_control( diff --git a/core/crates/runtime/engine/worker/src/native_control/source.rs b/core/crates/runtime/engine/worker/src/native_control/source.rs index 3b4145da..c142a093 100644 --- a/core/crates/runtime/engine/worker/src/native_control/source.rs +++ b/core/crates/runtime/engine/worker/src/native_control/source.rs @@ -12,7 +12,7 @@ pub(super) fn with_source( state: &WorkerState, guard: &SessionDurableMutationGuard<'_>, expected: Option<&NativeControlWitness>, - operation: impl FnOnce(&AgentBinding, &NativeControlWitness, &dyn Fn() -> Result<()>) -> Result, + operation: impl FnOnce(&AgentBinding, &NativeControlWitness, Option<&str>, &dyn Fn() -> Result<()>) -> Result, ) -> Result { let id = state.terminal_id(); let path = state.paths.manifest(id); @@ -30,10 +30,11 @@ pub(super) fn with_source( state.paths.agent_binding(id), std::sync::Arc::clone(&state.provider_runtime), ) - .read_raw_in_namespace(guard.namespace())? - .context(SOURCE_CHANGED)?; + .read_raw_in_namespace(guard.namespace())? + .context(SOURCE_CHANGED)?; let checked = |binding: &AgentBinding, witness: &NativeControlWitness, + stop_turn: Option<&str>, provider: &dyn Fn() -> Result<()>| { let verify = || -> Result<()> { provider()?; @@ -49,7 +50,7 @@ pub(super) fn with_source( guard.namespace().verify() }; verify()?; - operation(binding, witness, &verify) + operation(binding, witness, stop_turn, &verify) }; let provider = state .provider_runtime @@ -57,13 +58,20 @@ pub(super) fn with_source( .context(UNAVAILABLE)?; match provider.descriptor().authority_kind() { yolo_agent_providers::AuthorityKind::ClaudeGeneration => { - claude_hook::with_control_source(state, guard, expected, checked) + claude_hook::with_control_source(state, guard, expected, |binding, witness, verify| { + checked(binding, witness, None, verify) + }) } yolo_agent_providers::AuthorityKind::CodexOccurrence => { codex_hook::with_control_source(state, guard, expected, checked) } yolo_agent_providers::AuthorityKind::GenericHookGeneration => { - super::generic_hook_source::with_control_source(state, guard, expected, checked) + super::generic_hook_source::with_control_source( + state, + guard, + expected, + |binding, witness, verify| checked(binding, witness, None, verify), + ) } yolo_agent_providers::AuthorityKind::None => anyhow::bail!(UNAVAILABLE), } diff --git a/core/crates/runtime/engine/worker/src/native_control/submit.rs b/core/crates/runtime/engine/worker/src/native_control/submit.rs index 756232fe..d5043224 100644 --- a/core/crates/runtime/engine/worker/src/native_control/submit.rs +++ b/core/crates/runtime/engine/worker/src/native_control/submit.rs @@ -66,7 +66,7 @@ impl WorkerState { self, fence.durable(), Some(&request.witness), - |binding, witness, verify| { + |binding, witness, stop_turn, verify| { let ready = || -> anyhow::Result<()> { fence.verify()?; verify()?; @@ -75,7 +75,11 @@ impl WorkerState { request.generation, &request.writer_capability, )?; - let _ = input.verify(self, binding, witness)?; + input.verify_pty(self, witness)?; + anyhow::ensure!( + super::prompt_readiness::observe(self, binding, stop_turn).0, + SOURCE_CHANGED + ); Ok(()) }; ready()?; diff --git a/core/crates/runtime/engine/worker/src/state/state_core_snapshot.rs b/core/crates/runtime/engine/worker/src/state/state_core_snapshot.rs index cf4bb14c..71e33831 100644 --- a/core/crates/runtime/engine/worker/src/state/state_core_snapshot.rs +++ b/core/crates/runtime/engine/worker/src/state/state_core_snapshot.rs @@ -14,8 +14,8 @@ impl WorkerState { if let Some(title) = lock_recover(&self.agent_title).as_ref() { presentation.title = Some(title.clone()); } - let title_prefix = - yolo_terminal_metadata::MetadataCallbacks::new(presentation).snapshot_prefix(); + let title_prefix = yolo_terminal_metadata::MetadataCallbacks::new(presentation) + .snapshot_prefix(terminal.screen().alternate_screen()); TerminalSnapshot { runtime_epoch: journal.runtime_epoch(), barrier_sequence: journal.last_sequence(), diff --git a/core/crates/runtime/engine/worker/test_support/unit/codex_hook/control/control_source_fixture.rs b/core/crates/runtime/engine/worker/test_support/unit/codex_hook/control/control_source_fixture.rs index 770d636a..5c7da2c7 100644 --- a/core/crates/runtime/engine/worker/test_support/unit/codex_hook/control/control_source_fixture.rs +++ b/core/crates/runtime/engine/worker/test_support/unit/codex_hook/control/control_source_fixture.rs @@ -2,9 +2,9 @@ use super::super::{ binding::{self, ProjectionMode}, test_support::{ProjectionFixture, registry}, }; -use crate::agent::AgentBindingStore; use crate::agent::authority::{AuthorityProcessIdentity, AuthorityRuntimeIdentity}; use crate::agent::codex::authority::CodexBindingAuthority; +use crate::agent::{AgentBindingStore, AgentState}; use crate::codex_hooks::CodexSnapshotLineage; use crate::namespace_lease::NamespaceLease; use anyhow::Result; @@ -41,11 +41,15 @@ impl ControlSource { } pub fn persist_current(&self) { + self.persist_state(None); + } + + pub fn persist_state(&self, state: Option) { let source = self.projection.authority.as_ref().unwrap(); let lifecycle = source.runtime.lock_lifecycle_anyhow().unwrap(); let reducer = lifecycle.reducer().unwrap(); let previous = self.store.read_raw_in_namespace(&self.namespace).unwrap(); - let projected = binding::project( + let mut projected = binding::project( &self.projection.route(), reducer.state(), reducer.revision(), @@ -54,6 +58,9 @@ impl ControlSource { ®istry(), ) .unwrap(); + if let Some(state) = state { + projected.binding.state = state; + } let worker = crate::process::ProcessIdentity::capture(std::process::id()).unwrap(); let authority = CodexBindingAuthority::new_with_verified_resume_identity( AuthorityRuntimeIdentity { @@ -100,9 +107,12 @@ impl ControlSource { self.projection .router .with_control_route(&authority, |entry| { - entry - .runtime() - .with_control_authority(entry, &authority, ®istry(), callback) + entry.runtime().with_control_authority( + entry, + &authority, + ®istry(), + |lineage, _, verify| callback(lineage, verify), + ) }) } } diff --git a/core/crates/runtime/engine/worker/unit_tests/codex_hook/hook/control_source_tests.rs b/core/crates/runtime/engine/worker/unit_tests/codex_hook/hook/control_source_tests.rs index 78d681bc..491673d2 100644 --- a/core/crates/runtime/engine/worker/unit_tests/codex_hook/hook/control_source_tests.rs +++ b/core/crates/runtime/engine/worker/unit_tests/codex_hook/hook/control_source_tests.rs @@ -9,6 +9,41 @@ const CALLBACK_TIMEOUT: Duration = Duration::from_secs(5); #[path = "control_candidate_tests.rs"] mod candidate_tests; +#[test] +fn settled_stop_replays_without_overriding_other_lifecycle_states() { + use super::test_support::{FOURTH_SEQUENCE, THIRD_SEQUENCE, submit_event}; + use crate::agent::AgentState; + use crate::agent::hooks::{EventKind, HookMetadata}; + + let source = ControlSource::new(); + source.projection.stop(); + source.persist_state(Some(AgentState::WaitingForInput)); + source.run(|_, verify| verify()).unwrap(); + + let input = source.projection.authority.as_ref().unwrap(); + submit_event( + input, + FOURTH_SEQUENCE, + EventKind::UserPromptSubmit, + HookMetadata::default(), + None, + ); + source.persist_state(Some(AgentState::WaitingForInput)); + assert!(source.run(|_, _| Ok(())).is_err()); + + let source = ControlSource::new(); + let input = source.projection.authority.as_ref().unwrap(); + submit_event( + input, + THIRD_SEQUENCE, + EventKind::PermissionRequest, + HookMetadata::default(), + None, + ); + source.persist_state(Some(AgentState::WaitingForInput)); + assert!(source.run(|_, _| Ok(())).is_err()); +} + #[test] fn current_source_capture_repeats_and_reentrant_control_is_fenced() { let source = ControlSource::new(); diff --git a/core/crates/terminal/metadata/integration_tests/stream.rs b/core/crates/terminal/metadata/integration_tests/stream.rs index 1726b249..98d0173c 100644 --- a/core/crates/terminal/metadata/integration_tests/stream.rs +++ b/core/crates/terminal/metadata/integration_tests/stream.rs @@ -42,8 +42,8 @@ fn snapshot_restores_title_without_replaying_any_notification() { let mut original = parser(); original.process(&[TITLE_PARTS, NOTIFY, BEL, SCREEN_TEXT].concat()); let mut restored = parser(); - let mut snapshot = original.callbacks().snapshot_prefix(); - snapshot.extend(original.screen().contents_formatted()); + let mut snapshot = original.callbacks().snapshot_prefix(false); + snapshot.extend(original.screen().state_formatted()); restored.process(&snapshot); assert_eq!( restored.callbacks().presentation().title, @@ -61,6 +61,24 @@ fn snapshot_restores_title_without_replaying_any_notification() { ); } +#[test] +fn snapshot_of_the_alternate_screen_keeps_its_mode_and_leaves_cleanly() { + let mut original = parser(); + original.process(b"\x1b[?1049hALT"); + assert!(original.screen().alternate_screen()); + let mut restored = parser(); + let mut snapshot = original + .callbacks() + .snapshot_prefix(original.screen().alternate_screen()); + snapshot.extend(original.screen().state_formatted()); + restored.process(&snapshot); + assert!(restored.screen().alternate_screen()); + assert_eq!(restored.screen().contents().trim(), "ALT"); + restored.process(b"\x1b[?1049l"); + assert!(!restored.screen().alternate_screen()); + assert!(!restored.screen().contents().contains("ALT")); +} + #[test] fn oversized_unterminated_cancelled_and_nested_controls_never_publish_partial_metadata() { for ending in [BEL, ST, CANCEL] { diff --git a/core/crates/terminal/metadata/src/callbacks.rs b/core/crates/terminal/metadata/src/callbacks.rs index 832cb9e9..20d81aa5 100644 --- a/core/crates/terminal/metadata/src/callbacks.rs +++ b/core/crates/terminal/metadata/src/callbacks.rs @@ -19,9 +19,14 @@ impl MetadataCallbacks { &self.presentation } - /// Reconstruct the title only. Historical alerts must not ring on attachment. - pub fn snapshot_prefix(&self) -> Vec { + /// Reconstruct the title and the alternate-screen mode. Historical alerts + /// must not ring on attachment; a snapshot of the alternate grid must not + /// be replayed into the primary grid. + pub fn snapshot_prefix(&self, alternate_screen: bool) -> Vec { let mut bytes = Vec::new(); + if alternate_screen { + bytes.extend_from_slice(ALTERNATE_SCREEN_ENTER); + } if let Some(title) = &self.presentation.title { bytes.extend_from_slice(TITLE_PREFIX); bytes.extend_from_slice(title.as_bytes()); diff --git a/core/crates/terminal/metadata/src/constants.rs b/core/crates/terminal/metadata/src/constants.rs index b1a22a41..74b0b623 100644 --- a/core/crates/terminal/metadata/src/constants.rs +++ b/core/crates/terminal/metadata/src/constants.rs @@ -1,3 +1,4 @@ +pub(crate) const ALTERNATE_SCREEN_ENTER: &[u8] = b"\x1b[?1049h"; pub(crate) const TITLE_PREFIX: &[u8] = b"\x1b]2;"; pub(crate) const STRING_TERMINATOR: &[u8] = b"\x1b\\"; pub(crate) const OSC_TITLE_AND_ICON: &[u8] = b"0"; diff --git a/core/crates/transport/broker/hub/protocol/src/request.rs b/core/crates/transport/broker/hub/protocol/src/request.rs index 9c84fa3a..7e2e9788 100644 --- a/core/crates/transport/broker/hub/protocol/src/request.rs +++ b/core/crates/transport/broker/hub/protocol/src/request.rs @@ -1,27 +1,40 @@ use crate::{BrokerHubConnectionBinding, BrokerHubError}; use serde::{Deserialize, Serialize}; -use std::net::SocketAddr; +use std::path::{Component, Path, PathBuf}; use yolo_hub_delegation::PeerIdentity; +const MAX_SOCKET_PATH_BYTES: usize = 100; + #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] #[serde(deny_unknown_fields)] pub struct BrokerHubOpenRequest { pub binding: BrokerHubConnectionBinding, - pub endpoint: SocketAddr, + pub socket: PathBuf, pub peer: PeerIdentity, } impl BrokerHubOpenRequest { pub fn validate(&self) -> Result<(), BrokerHubError> { self.binding.validate()?; - if !self.endpoint.ip().is_loopback() - || self.endpoint.port() == u16::default() - || matches!(self.endpoint, SocketAddr::V6(value) if value.scope_id() != 0 || value.flowinfo() != 0) - { - return Err(BrokerHubError::Endpoint); - } - Ok(()) + validate_socket(&self.socket) + } +} + +fn validate_socket(path: &Path) -> Result<(), BrokerHubError> { + let encoded = path.as_os_str().as_encoded_bytes(); + let raw = path.to_str().ok_or(BrokerHubError::Endpoint)?; + if !path.is_absolute() + || raw.split('/').any(|part| part == "." || part == "..") + || encoded.is_empty() + || encoded.len() > MAX_SOCKET_PATH_BYTES + || encoded.iter().any(|byte| byte.is_ascii_control()) + || !path + .components() + .all(|component| matches!(component, Component::RootDir | Component::Normal(_))) + { + return Err(BrokerHubError::Endpoint); } + Ok(()) } #[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)] diff --git a/core/crates/transport/broker/hub/protocol/test_support/fixtures.rs b/core/crates/transport/broker/hub/protocol/test_support/fixtures.rs index d296d47f..7b824ca7 100644 --- a/core/crates/transport/broker/hub/protocol/test_support/fixtures.rs +++ b/core/crates/transport/broker/hub/protocol/test_support/fixtures.rs @@ -5,7 +5,7 @@ use yolo_hub_identity::HubId; pub const HASH: &str = "0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef"; pub const HOST: &str = "hub-fixture"; -pub const ENDPOINT: &str = "127.0.0.1:32123"; +pub const SOCKET: &str = "/run/user/1000/yolo/hub/peer.sock"; pub const PEER_KEY: [u8; 32] = [7; 32]; pub const GENERATION: u64 = 1; pub const CHANNEL: u64 = 3; @@ -28,7 +28,7 @@ pub fn binding() -> BrokerHubConnectionBinding { pub fn request() -> BrokerHubOpenRequest { BrokerHubOpenRequest { binding: binding(), - endpoint: ENDPOINT.parse().unwrap(), + socket: SOCKET.into(), peer: PeerIdentity::new(HubId::new(), PEER_KEY).unwrap(), } } diff --git a/core/crates/transport/broker/hub/protocol/unit_tests/boundaries.rs b/core/crates/transport/broker/hub/protocol/unit_tests/boundaries.rs index 68e84393..0e3859d4 100644 --- a/core/crates/transport/broker/hub/protocol/unit_tests/boundaries.rs +++ b/core/crates/transport/broker/hub/protocol/unit_tests/boundaries.rs @@ -1,12 +1,10 @@ use super::fixtures; use crate::*; -use std::net::{Ipv6Addr, SocketAddr, SocketAddrV6}; const HOST_BYTE: &str = "a"; const EXTRA_BYTE: usize = 1; -const PEER_PORT: u16 = 32123; -const FLOW_INFORMATION: u32 = 1; -const INTERFACE_SCOPE: u32 = 2; +const PATH_BYTE: &str = "a"; +const MAX_SOCKET_BYTES: usize = 100; #[test] fn profile_host_bound_accepts_the_limit_and_rejects_longer_or_path_like_values() { @@ -26,33 +24,12 @@ fn profile_host_bound_accepts_the_limit_and_rejects_longer_or_path_like_values() } #[test] -fn ipv6_loopback_requires_both_scope_and_flow_information_to_be_zero() { +fn socket_budget_accepts_the_kernel_limit_and_rejects_longer_paths() { let mut request = fixtures::request(); - request.endpoint = SocketAddr::V6(SocketAddrV6::new( - Ipv6Addr::LOCALHOST, - PEER_PORT, - u32::default(), - u32::default(), - )); + request.socket = format!("/{}", PATH_BYTE.repeat(MAX_SOCKET_BYTES - 1)).into(); request.validate().unwrap(); - let opened = BrokerHubOpened { - request: request.clone(), - channel_id: fixtures::CHANNEL, - }; - opened.validate(&request).unwrap(); - for (flow, scope) in [ - (FLOW_INFORMATION, u32::default()), - (u32::default(), INTERFACE_SCOPE), - (FLOW_INFORMATION, INTERFACE_SCOPE), - ] { - request.endpoint = SocketAddr::V6(SocketAddrV6::new( - Ipv6Addr::LOCALHOST, - PEER_PORT, - flow, - scope, - )); - assert_eq!(request.validate(), Err(BrokerHubError::Endpoint)); - } + request.socket = format!("/{}", PATH_BYTE.repeat(MAX_SOCKET_BYTES)).into(); + assert_eq!(request.validate(), Err(BrokerHubError::Endpoint)); } #[test] diff --git a/core/crates/transport/broker/hub/protocol/unit_tests/lib.rs b/core/crates/transport/broker/hub/protocol/unit_tests/lib.rs index a19a59c3..a3137cce 100644 --- a/core/crates/transport/broker/hub/protocol/unit_tests/lib.rs +++ b/core/crates/transport/broker/hub/protocol/unit_tests/lib.rs @@ -4,6 +4,8 @@ mod decode; mod fixtures; use crate::*; +const MAX_SOCKET_BYTES: usize = 100; + #[test] fn requests_bind_every_profile_connection_and_peer_field() { let request = fixtures::request(); @@ -30,7 +32,7 @@ fn requests_bind_every_profile_connection_and_peer_field() { } #[test] -fn invalid_profile_binding_and_nonloopback_or_scoped_endpoint_are_rejected() { +fn invalid_profile_binding_and_noncanonical_socket_paths_are_rejected() { for invalid in ["", "../other", "with space"] { let mut profile = fixtures::profile(); profile.host_id = invalid.into(); @@ -57,9 +59,17 @@ fn invalid_profile_binding_and_nonloopback_or_scoped_endpoint_are_rejected() { } assert_eq!(binding.validate(), Err(BrokerHubError::Binding)); } - for endpoint in ["127.0.0.1:0", "192.0.2.1:22", "[::1%2]:22", "[::]:22"] { + let long = format!("/{}", "a".repeat(MAX_SOCKET_BYTES)); + for socket in [ + "peer.sock", + "relative/peer.sock", + "/tmp/../peer.sock", + "/tmp/./peer.sock", + "/tmp/peer\n.sock", + long.as_str(), + ] { let mut request = fixtures::request(); - request.endpoint = endpoint.parse().unwrap(); + request.socket = socket.into(); assert_eq!(request.validate(), Err(BrokerHubError::Endpoint)); } } diff --git a/core/crates/transport/capnp/rpc/src/connection.rs b/core/crates/transport/capnp/rpc/src/connection.rs index e50cf931..e1238e9c 100644 --- a/core/crates/transport/capnp/rpc/src/connection.rs +++ b/core/crates/transport/capnp/rpc/src/connection.rs @@ -124,6 +124,9 @@ impl Connection { pub fn close(&self) { self.0.stop.send_replace(true); } + pub fn is_closed(&self) -> bool { + *self.0.stop.borrow() + } } impl RemoteReply { diff --git a/core/crates/transport/remote/BUILD.bazel b/core/crates/transport/remote/BUILD.bazel index 56b039e9..bd50c8d5 100644 --- a/core/crates/transport/remote/BUILD.bazel +++ b/core/crates/transport/remote/BUILD.bazel @@ -132,7 +132,6 @@ isolated_rust_test( "//core/crates/distribution/publication/protocol:yolo_distribution_publication_protocol", "//core/crates/hub/delegation:yolo_hub_delegation", "//core/crates/hub/identity:yolo_hub_identity", - "//core/crates/hub/peer/transport:yolo_hub_peer_transport", "//core/crates/crypto:yolo_crypto", "//core/crates/persistence/acl:yolo_private_acl", "//core/crates/transport/local/unix:yolo_local_unix_test_support", diff --git a/core/crates/transport/remote/broker/integration_tests/hub_admission.rs b/core/crates/transport/remote/broker/integration_tests/hub_admission.rs index a825c3bf..4c215b27 100644 --- a/core/crates/transport/remote/broker/integration_tests/hub_admission.rs +++ b/core/crates/transport/remote/broker/integration_tests/hub_admission.rs @@ -39,7 +39,7 @@ async fn pending_adoption_prepares_bound_ssh_but_cannot_open_until_committed() { .open_hub_peer(request, deadline()) .await .unwrap(); - let (mut remote, _) = fixture.target.accept().await.unwrap(); + let (mut remote, _) = fixture.target.listener.accept().await.unwrap(); drop(tunnel); assert!( timeout_at(deadline(), remote.read_u8()) @@ -59,13 +59,13 @@ async fn admitted_tunnel_releases_the_only_management_slot() { .prepare_hub_transport(&request.binding.profile) .await .unwrap(); - request.endpoint = fixture.target.local_addr().unwrap(); + request.socket = fixture.target.path.clone(); let mut first = fixture .client() .open_hub_peer(request.clone(), deadline()) .await .unwrap(); - let (mut remote, _) = fixture.target.accept().await.unwrap(); + let (mut remote, _) = fixture.target.listener.accept().await.unwrap(); // Wait for the promotion completion without relying on a scheduling delay. timeout_at(deadline(), async { loop { @@ -126,7 +126,7 @@ async fn retirement_drains_active_tunnels_and_releases_its_namespace() { .open_hub_peer(request.clone(), deadline()) .await .unwrap(); - let _remote = fixture.target.accept().await.unwrap(); + let _remote = fixture.target.listener.accept().await.unwrap(); fixture .client() .request(BrokerRequest::Retire { diff --git a/core/crates/transport/remote/broker/integration_tests/hub_rotation_fence.rs b/core/crates/transport/remote/broker/integration_tests/hub_rotation_fence.rs index 336614f0..d7dfd3cb 100644 --- a/core/crates/transport/remote/broker/integration_tests/hub_rotation_fence.rs +++ b/core/crates/transport/remote/broker/integration_tests/hub_rotation_fence.rs @@ -32,7 +32,7 @@ async fn delayed_tunnel_open_cannot_be_promoted_after_rotation_cutoff() { .await .unwrap(); fixture.ssh.release_pending_channel(); - let (mut remote, _) = timeout_at(deadline(), fixture.target.accept()) + let (mut remote, _) = timeout_at(deadline(), fixture.target.listener.accept()) .await .unwrap() .unwrap(); diff --git a/core/crates/transport/remote/broker/integration_tests/hub_tunnel.rs b/core/crates/transport/remote/broker/integration_tests/hub_tunnel.rs index 15283003..ca820bdc 100644 --- a/core/crates/transport/remote/broker/integration_tests/hub_tunnel.rs +++ b/core/crates/transport/remote/broker/integration_tests/hub_tunnel.rs @@ -15,13 +15,13 @@ async fn one_connection_supports_independent_tunnels_and_client_recreation_witho .open_hub_peer(request.clone(), deadline()) .await .unwrap(); - let (mut first_remote, _) = fixture.target.accept().await.unwrap(); + let (mut first_remote, _) = fixture.target.listener.accept().await.unwrap(); let mut second = fixture .client() .open_hub_peer(request.clone(), deadline()) .await .unwrap(); - let (mut second_remote, _) = fixture.target.accept().await.unwrap(); + let (mut second_remote, _) = fixture.target.listener.accept().await.unwrap(); let status = fixture.client().status().await.unwrap(); assert_eq!(status.connection_id, Some(request.binding.connection_id)); first.write_all(PAYLOAD).await.unwrap(); @@ -74,7 +74,7 @@ async fn stale_identity_wrong_profile_and_rejected_endpoint_never_reconnect_or_p matches!(fixture.client().open_hub_peer(wrong, deadline()).await, Err(BrokerClientError::Remote { code, .. }) if code == BrokerHubError::ProfileMismatch.code()) ); let mut wrong = request.clone(); - wrong.endpoint.set_port(WRONG_PORT); + wrong.socket = WRONG_SOCKET.into(); assert!( fixture .client() @@ -110,7 +110,7 @@ async fn rotation_keeps_admitted_tunnel_alive_until_its_local_owner_closes() { .open_hub_peer(request.clone(), deadline()) .await .unwrap(); - let (mut remote, _) = fixture.target.accept().await.unwrap(); + let (mut remote, _) = fixture.target.listener.accept().await.unwrap(); fixture .client() .shutdown(request.binding.profile.config_revision) diff --git a/core/crates/transport/remote/broker/integration_tests/hub_tunnel_cancellation.rs b/core/crates/transport/remote/broker/integration_tests/hub_tunnel_cancellation.rs index d02a53c9..624f6910 100644 --- a/core/crates/transport/remote/broker/integration_tests/hub_tunnel_cancellation.rs +++ b/core/crates/transport/remote/broker/integration_tests/hub_tunnel_cancellation.rs @@ -27,7 +27,7 @@ async fn client_eof_during_open_cleans_the_late_channel_without_retiring_the_ssh timeout_at(deadline(), fixture.ssh.wait_accepted_channel()) .await .unwrap(); - let (mut remote, _) = timeout_at(deadline(), fixture.target.accept()) + let (mut remote, _) = timeout_at(deadline(), fixture.target.listener.accept()) .await .unwrap() .unwrap(); @@ -70,7 +70,7 @@ async fn local_abort_after_upgrade_does_not_interrupt_sibling_command() { .open_hub_peer(request, deadline()) .await .unwrap(); - let (mut remote, _) = fixture.target.accept().await.unwrap(); + let (mut remote, _) = fixture.target.listener.accept().await.unwrap(); // Inner authentication failure has the same close ownership as this immediate drop. drop(tunnel); assert!( @@ -96,7 +96,7 @@ async fn absolute_deadline_expiry_before_open_does_not_create_a_channel() { Err(BrokerClientError::Deadline) )); assert!( - tokio::time::timeout(POLL, fixture.target.accept()) + tokio::time::timeout(POLL, fixture.target.listener.accept()) .await .is_err() ); diff --git a/core/crates/transport/remote/broker/src/connection/connection_hub_peer.rs b/core/crates/transport/remote/broker/src/connection/connection_hub_peer.rs index a5cad750..bc5709ba 100644 --- a/core/crates/transport/remote/broker/src/connection/connection_hub_peer.rs +++ b/core/crates/transport/remote/broker/src/connection/connection_hub_peer.rs @@ -32,7 +32,7 @@ impl BrokerConnectionPool { ); let remote = HubPeerRequest::new( observed.id(), - HubPeerEndpoint::new(request.endpoint)?, + HubPeerEndpoint::new(request.socket.clone())?, request.peer, )?; let opened = match observed.connection().open_hub_peer(&remote).await { diff --git a/core/crates/transport/remote/broker/src/constants.rs b/core/crates/transport/remote/broker/src/constants.rs index 5c7037b2..97d3b45a 100644 --- a/core/crates/transport/remote/broker/src/constants.rs +++ b/core/crates/transport/remote/broker/src/constants.rs @@ -10,7 +10,7 @@ pub const PENDING_GENERATION_PREFIX: &str = "pending-"; pub const PENDING_GENERATION_PAYLOAD_SUFFIX: &str = "-payload"; pub const MAX_CONCURRENT_GENERATION_STAGES: usize = 16; pub const BROKER_CONFIG_VERSION: u16 = 1; -pub const BROKER_PROTOCOL_VERSION: u16 = 4; +pub const BROKER_PROTOCOL_VERSION: u16 = 5; pub const CONNECTION_BOOTSTRAP_CAPABILITY: &str = "connection-bootstrap"; pub const WORKING_DIRECTORY_CAPABILITY: &str = "working-directory"; pub const REMOTE_LOGIN_DIRECTORY: &str = "."; diff --git a/core/crates/transport/remote/broker/test_support/hub_client_server.rs b/core/crates/transport/remote/broker/test_support/hub_client_server.rs index 2678009b..2c4dc422 100644 --- a/core/crates/transport/remote/broker/test_support/hub_client_server.rs +++ b/core/crates/transport/remote/broker/test_support/hub_client_server.rs @@ -55,7 +55,7 @@ impl Server { Fault::Generation => request.binding.connection_generation += OFFSET, Fault::Profile => request.binding.profile.host_id.push('x'), Fault::Peer => request.peer = yolo_broker_hub_test_support::request().peer, - Fault::Endpoint => request.endpoint.set_port(super::hub_fixture::WRONG_PORT), + Fault::Endpoint => request.socket = super::hub_fixture::WRONG_SOCKET.into(), _ => {} } let opened = BrokerHubOpened { diff --git a/core/crates/transport/remote/broker/test_support/hub_fixture.rs b/core/crates/transport/remote/broker/test_support/hub_fixture.rs index 12180c83..48c4b01b 100644 --- a/core/crates/transport/remote/broker/test_support/hub_fixture.rs +++ b/core/crates/transport/remote/broker/test_support/hub_fixture.rs @@ -8,7 +8,6 @@ use std::{ }, }; use tokio::{ - net::TcpListener, sync::oneshot, time::{Instant, timeout_at}, }; @@ -17,7 +16,7 @@ pub const PAYLOAD: &[u8] = b"Hub tunnel data\0\r\n"; pub const TIMEOUT: std::time::Duration = std::time::Duration::from_secs(5); pub const POLL: std::time::Duration = std::time::Duration::from_millis(5); pub const ONE_TUNNEL: usize = 1; -pub const WRONG_PORT: u16 = 1; +pub const WRONG_SOCKET: &str = "/run/user/1000/yolo/wrong.sock"; const RUNTIME: &str = "/opt/yolo"; const REMOTE_HOME: &str = "/srv/yolo"; const USER: &str = "builder"; @@ -63,7 +62,7 @@ pub struct Harness { _root: tempfile::TempDir, pub paths: BrokerPaths, pub ssh: BrokerCommandCleanupFixture, - pub target: TcpListener, + pub target: HubPeerTarget, pub factory: Arc, pub pool: Arc, pub adoption: Option>, @@ -149,7 +148,7 @@ impl Harness { .prepare_hub_transport(yolo_broker_hub_test_support::profile(), deadline()) .await .unwrap(); - request.endpoint = self.target.local_addr().unwrap(); + request.socket = self.target.path.clone(); request } pub async fn stop(mut self) { diff --git a/core/crates/transport/remote/integration_tests/host_key_tests.rs b/core/crates/transport/remote/integration_tests/host_key_tests.rs index c56a2f58..4b2ecdd1 100644 --- a/core/crates/transport/remote/integration_tests/host_key_tests.rs +++ b/core/crates/transport/remote/integration_tests/host_key_tests.rs @@ -5,7 +5,7 @@ use hmac::{Hmac, Mac}; use sha1::Sha1; const KEY: &str = "AAAAC3NzaC1lZDI1NTE5AAAAITestKey"; -const OTHER_KEY: &str = "AAAAC3NzaC1lZDI1NTE5AAAan internal working treetherKey"; +const OTHER_KEY: &str = "AAAAC3NzaC1lZDI1NTE5AAAAITestKe2"; fn endpoint(port: u16) -> RemoteEndpoint { RemoteEndpoint::new("stone", "Build.Example", port).unwrap() diff --git a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests.rs b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests.rs index 43c99efb..07bdfd74 100644 --- a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests.rs +++ b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests.rs @@ -54,8 +54,6 @@ mod deployment_tests; mod failure_tests; #[path = "../../test_support/russh_backend/fixture.rs"] mod fixture; -#[path = "backend_tests/hub_peer_auth_tests.rs"] -mod hub_peer_auth_tests; #[path = "backend_tests/hub_peer_cancellation_tests.rs"] mod hub_peer_cancellation_tests; #[path = "../../test_support/russh_backend/hub_peer_fixture.rs"] diff --git a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/bundle_retention_tests.rs b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/bundle_retention_tests.rs index 61aa2430..a727f619 100644 --- a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/bundle_retention_tests.rs +++ b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/bundle_retention_tests.rs @@ -20,7 +20,7 @@ async fn cancelled_readback_closes_only_its_sftp_channel_and_keeps_sibling_hub_p .open_hub_peer(&fixture.request) .await .unwrap(); - let (mut socket, _) = timeout_at(hub_peer_fixture::deadline(), fixture.listener.accept()) + let (mut socket, _) = timeout_at(hub_peer_fixture::deadline(), fixture.target.listener.accept()) .await .unwrap() .unwrap(); diff --git a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_auth_tests.rs b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_auth_tests.rs deleted file mode 100644 index e79ecd3d..00000000 --- a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_auth_tests.rs +++ /dev/null @@ -1,82 +0,0 @@ -use super::{constants::*, hub_peer_fixture::*}; -use crate::remote::*; -use tokio::{ - io::{AsyncReadExt, AsyncWriteExt}, - time::timeout_at, -}; -use yolo_hub_peer_transport::{PeerIo, PeerPins}; - -#[tokio::test] -async fn nested_hub_handshake_over_direct_tcpip_still_requires_the_exact_inner_pin() { - for wrong_pin in [false, true] { - let fixture = PeerFixture::new().await; - let (local_identity, local_key) = peer_keys(); - let expected = if wrong_pin { - peer_keys().0 - } else { - fixture.request.peer() - }; - let request = HubPeerRequest::new( - fixture.request.connection_id(), - fixture.request.endpoint(), - expected, - ) - .unwrap(); - let opened = fixture - .ssh - .connection - .open_hub_peer(&request) - .await - .unwrap(); - let (socket, _) = timeout_at(deadline(), fixture.listener.accept()) - .await - .unwrap() - .unwrap(); - let close = opened.stream.close_handle(); - let (client, server) = tokio::join!( - yolo_hub_peer_transport::connect_io( - PeerIo::new(opened.stream, move || close.close()), - local_key, - opened.peer, - deadline() - ), - yolo_hub_peer_transport::accept( - socket, - fixture.key.clone(), - PeerPins::new(vec![local_identity]).unwrap(), - deadline() - ), - ); - if wrong_pin { - assert!(client.is_err()); - assert!(server.is_err()); - } else { - let (client, _) = client.unwrap(); - let (server, mut incoming) = server.unwrap(); - assert_eq!(client.peer(), fixture.request.peer()); - assert_eq!(server.peer(), local_identity); - let mut channel = client.open_channel(deadline()).await.unwrap(); - let mut receive = timeout_at(deadline(), incoming.recv()) - .await - .unwrap() - .unwrap(); - channel.write_all(PAYLOAD).await.unwrap(); - let mut bytes = vec![u8::default(); PAYLOAD.len()]; - timeout_at(deadline(), receive.read_exact(&mut bytes)) - .await - .unwrap() - .unwrap(); - assert_eq!(bytes, PAYLOAD); - client.close(deadline()).await.unwrap(); - server.close(deadline()).await.unwrap(); - } - assert_connection_healthy(&fixture.ssh).await; - fixture - .ssh - .connection - .cleanup - .settle(cleanup_settlement_timeout(NORMAL_OPERATION_TIMEOUT)) - .await - .unwrap(); - } -} diff --git a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_cancellation_tests.rs b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_cancellation_tests.rs index bb624a25..97801a8d 100644 --- a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_cancellation_tests.rs +++ b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_cancellation_tests.rs @@ -17,7 +17,7 @@ async fn cancelled_delayed_direct_open_closes_its_late_channel_without_losing_ss fixture.ssh.filesystem.release_channel_open(); fixture.ssh.filesystem.wait_channel_open_accepted().await; barrier.release(); - let (mut socket, _) = timeout_at(deadline(), fixture.listener.accept()) + let (mut socket, _) = timeout_at(deadline(), fixture.target.listener.accept()) .await .unwrap() .unwrap(); diff --git a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_tests.rs b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_tests.rs index 1262c69a..7eaee20d 100644 --- a/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_tests.rs +++ b/core/crates/transport/remote/integration_tests/russh_backend/backend_tests/hub_peer_tests.rs @@ -24,7 +24,7 @@ async fn hub_peer_exec_and_sftp_share_one_authenticated_connection_and_close_ind assert_eq!(peer.identity.purpose, ChannelPurpose::HubPeer); assert_eq!(peer.peer, fixture.request.peer()); assert_eq!(executed.value.stdout, COMMAND_STDOUT); - let (mut socket, _) = timeout_at(deadline(), fixture.listener.accept()) + let (mut socket, _) = timeout_at(deadline(), fixture.target.listener.accept()) .await .unwrap() .unwrap(); @@ -43,7 +43,7 @@ async fn hub_peer_exec_and_sftp_share_one_authenticated_connection_and_close_ind .unwrap(); assert_eq!(bytes, PAYLOAD); let mut sibling = connection.open_hub_peer(&fixture.request).await.unwrap(); - let (mut sibling_socket, _) = timeout_at(deadline(), fixture.listener.accept()) + let (mut sibling_socket, _) = timeout_at(deadline(), fixture.target.listener.accept()) .await .unwrap() .unwrap(); @@ -75,7 +75,7 @@ async fn wrong_connection_and_server_rejection_do_not_open_or_poison_sibling_cha let fixture = PeerFixture::new().await; let stale = HubPeerRequest::new( uuid::Uuid::new_v4(), - fixture.request.endpoint(), + fixture.request.endpoint().clone(), fixture.request.peer(), ) .unwrap(); @@ -83,15 +83,9 @@ async fn wrong_connection_and_server_rejection_do_not_open_or_poison_sibling_cha fixture.ssh.connection.open_hub_peer(&stale).await, Err(RemoteError::ChannelMismatch) )); - let denied_listener = tokio::net::TcpListener::bind(std::net::SocketAddr::new( - std::net::Ipv4Addr::LOCALHOST.into(), - DYNAMIC_PORT, - )) - .await - .unwrap(); let denied = HubPeerRequest::new( fixture.request.connection_id(), - HubPeerEndpoint::new(denied_listener.local_addr().unwrap()).unwrap(), + HubPeerEndpoint::new("/run/user/1000/yolo/denied.sock").unwrap(), fixture.request.peer(), ) .unwrap(); @@ -117,7 +111,7 @@ async fn closing_hub_peer_during_incoming_burst_does_not_disconnect_ssh() { &fixture.ssh, started, Phase::Accept, - fixture.listener.accept(), + fixture.target.listener.accept(), ) .await; observe_phase(&fixture.ssh, started, Phase::Accept, async { diff --git a/core/crates/transport/remote/provisioning/protocol/src/bootstrap_constants.rs b/core/crates/transport/remote/provisioning/protocol/src/bootstrap_constants.rs index ca886450..6ce46a52 100644 --- a/core/crates/transport/remote/provisioning/protocol/src/bootstrap_constants.rs +++ b/core/crates/transport/remote/provisioning/protocol/src/bootstrap_constants.rs @@ -1,5 +1,5 @@ -pub const REMOTE_BOOTSTRAP_SCHEMA_VERSION: u16 = 1; -pub const ENROLLMENT_RECOVERY_SCHEMA: u16 = 1; +pub const REMOTE_BOOTSTRAP_SCHEMA_VERSION: u16 = 2; +pub const ENROLLMENT_RECOVERY_SCHEMA: u16 = 2; pub const REMOTE_BOOTSTRAP_ARGUMENT: &str = "--remote-bootstrap"; pub const REMOTE_BOOTSTRAP_RECOVERY_ARGUMENT: &str = "--recover-remote-bootstrap"; pub const MAX_BOOTSTRAP_DOCUMENT_BYTES: usize = 16 * 1024; diff --git a/core/crates/transport/remote/provisioning/protocol/src/bootstrap_error.rs b/core/crates/transport/remote/provisioning/protocol/src/bootstrap_error.rs index 5ddac056..0f1b6e49 100644 --- a/core/crates/transport/remote/provisioning/protocol/src/bootstrap_error.rs +++ b/core/crates/transport/remote/provisioning/protocol/src/bootstrap_error.rs @@ -10,7 +10,7 @@ pub enum BootstrapError { SameIdentity, #[error("remote bootstrap authorization must include observation and bounded transit")] Authorization, - #[error("remote bootstrap listener must be a bound loopback endpoint")] + #[error("remote peer socket must be a canonical absolute Unix socket path")] Endpoint, #[error("invalid applied remote configuration revision")] Revision, diff --git a/core/crates/transport/remote/provisioning/protocol/src/bootstrap_receipt.rs b/core/crates/transport/remote/provisioning/protocol/src/bootstrap_receipt.rs index cf0ac577..3352686d 100644 --- a/core/crates/transport/remote/provisioning/protocol/src/bootstrap_receipt.rs +++ b/core/crates/transport/remote/provisioning/protocol/src/bootstrap_receipt.rs @@ -1,6 +1,8 @@ use crate::{BootstrapError, RemoteBootstrapRequest, bootstrap_constants::*, bootstrap_validation as validation}; + +const MAX_SOCKET_PATH_BYTES: usize = 100; use serde::{Deserialize, Serialize}; -use std::net::SocketAddr; +use std::path::{Component, PathBuf}; use uuid::Uuid; use yolo_hub_build_protocol::ServiceBuild; use yolo_hub_delegation::PeerIdentity; @@ -19,7 +21,7 @@ pub struct RemoteBootstrapReceiptFields { pub schema_version: u16, pub request_id: Uuid, pub peer: PeerIdentity, - pub endpoint: SocketAddr, + pub peer_socket: PathBuf, pub configuration_revision: u64, pub bundle_id: String, pub build: ServiceBuild, @@ -28,6 +30,20 @@ pub struct RemoteBootstrapReceiptFields { pub remaining_reexports: u8, } +fn valid_socket(path: &std::path::Path) -> bool { + let encoded = path.as_os_str().as_encoded_bytes(); + path.is_absolute() + && path.to_str().is_some_and(|raw| { + !raw.split('/').any(|part| part == "." || part == "..") + }) + && !encoded.is_empty() + && encoded.len() <= MAX_SOCKET_PATH_BYTES + && !encoded.iter().any(|byte| byte.is_ascii_control()) + && path + .components() + .all(|component| matches!(component, Component::RootDir | Component::Normal(_))) +} + impl RemoteBootstrapReceipt { pub fn new(fields: RemoteBootstrapReceiptFields) -> Result { if fields.schema_version != REMOTE_BOOTSTRAP_SCHEMA_VERSION { @@ -38,10 +54,7 @@ impl RemoteBootstrapReceipt { } validation::distinct(fields.peer, fields.consumer)?; validation::authorization(&fields.rights, fields.remaining_reexports)?; - if !fields.endpoint.ip().is_loopback() - || fields.endpoint.port() == 0 - || matches!(fields.endpoint, SocketAddr::V6(address) if address.scope_id() != 0 || address.flowinfo() != 0) - { + if !valid_socket(&fields.peer_socket) { return Err(BootstrapError::Endpoint); } if fields.configuration_revision < FIRST_CONFIGURATION_REVISION { diff --git a/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_correlation.rs b/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_correlation.rs index 30a16016..076ddb25 100644 --- a/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_correlation.rs +++ b/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_correlation.rs @@ -11,9 +11,9 @@ fn first_enrollment_and_pinned_reuse_accept_only_the_complete_requested_build() ready .validate_for(&RemoteBootstrapRequest::new(enrollment).unwrap()) .unwrap(); - let mut v6 = ready.fields().clone(); - v6.endpoint = std::net::SocketAddr::from((std::net::Ipv6Addr::LOCALHOST, PORT)); - RemoteBootstrapReceipt::new(v6) + let mut alternate = ready.fields().clone(); + alternate.peer_socket = "/run/user/1000/yolo/alternate.sock".into(); + RemoteBootstrapReceipt::new(alternate) .unwrap() .validate_for(&pinned) .unwrap(); diff --git a/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_validation.rs b/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_validation.rs index 0b9d4482..9c4871af 100644 --- a/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_validation.rs +++ b/core/crates/transport/remote/provisioning/protocol/unit_tests/bootstrap_validation.rs @@ -45,33 +45,29 @@ fn enrollment_requires_explicit_observation_authorization_and_distinct_identity( } #[test] -fn receipts_reject_invalid_envelope_and_nonloopback_or_unbound_listener() { +fn receipts_reject_invalid_envelope_and_noncanonical_peer_sockets() { let request = request(); - for endpoint in [ - std::net::SocketAddr::from((std::net::Ipv4Addr::UNSPECIFIED, PORT)), - std::net::SocketAddr::from((std::net::Ipv6Addr::UNSPECIFIED, PORT)), - std::net::SocketAddr::from((std::net::Ipv4Addr::LOCALHOST, 0)), - std::net::SocketAddr::V6(std::net::SocketAddrV6::new( - std::net::Ipv6Addr::LOCALHOST, - PORT, - 0, - 1, - )), - std::net::SocketAddr::V6(std::net::SocketAddrV6::new( - std::net::Ipv6Addr::LOCALHOST, - PORT, - 1, - 0, - )), + for socket in [ + "peer.sock", + "relative/peer.sock", + "/tmp/../etc/peer.sock", + "/tmp/./peer.sock", + "/tmp/peer\n.sock", ] { let mut changed = receipt(&request); - changed.endpoint = endpoint; + changed.peer_socket = socket.into(); assert_eq!( RemoteBootstrapReceipt::new(changed), Err(BootstrapError::Endpoint) ); } let mut changed = receipt(&request); + changed.peer_socket = format!("/{}", "a".repeat(100)).into(); + assert_eq!( + RemoteBootstrapReceipt::new(changed), + Err(BootstrapError::Endpoint) + ); + let mut changed = receipt(&request); changed.schema_version += 1; assert_eq!( RemoteBootstrapReceipt::new(changed), diff --git a/core/crates/transport/remote/src/hub_peer.rs b/core/crates/transport/remote/src/hub_peer.rs index fe534429..b6581301 100644 --- a/core/crates/transport/remote/src/hub_peer.rs +++ b/core/crates/transport/remote/src/hub_peer.rs @@ -1,37 +1,43 @@ use super::{ChannelIdentity, RemoteError}; -use std::{net::SocketAddr, num::NonZeroU16}; +use std::path::{Component, PathBuf}; use uuid::Uuid; use yolo_hub_delegation::PeerIdentity; -const INVALID_ENDPOINT: &str = "Hub peer endpoint must be a nonzero unscoped loopback address"; +const INVALID_ENDPOINT: &str = "Hub peer endpoint must be an absolute canonical Unix socket path"; +const MAX_SOCKET_PATH_BYTES: usize = 100; -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct HubPeerEndpoint { - address: std::net::IpAddr, - port: NonZeroU16, + path: PathBuf, } impl HubPeerEndpoint { - pub fn new(address: SocketAddr) -> Result { - let scoped = matches!(address, SocketAddr::V6(value) if value.scope_id() != 0 || value.flowinfo() != 0); - if !address.ip().is_loopback() || scoped { + pub fn new(path: impl Into) -> Result { + let path = path.into(); + let encoded = path.as_os_str().as_encoded_bytes(); + let raw = path + .to_str() + .ok_or_else(|| RemoteError::InvalidEndpoint(INVALID_ENDPOINT.into()))?; + if !path.is_absolute() + || raw.split('/').any(|part| part == "." || part == "..") + || !(1..=MAX_SOCKET_PATH_BYTES).contains(&encoded.len()) + || encoded.iter().any(|byte| byte.is_ascii_control()) + || !path + .components() + .all(|component| matches!(component, Component::RootDir | Component::Normal(_))) + { return Err(RemoteError::InvalidEndpoint(INVALID_ENDPOINT.into())); } - let port = NonZeroU16::new(address.port()) - .ok_or_else(|| RemoteError::InvalidEndpoint(INVALID_ENDPOINT.into()))?; - Ok(Self { - address: address.ip(), - port, - }) + Ok(Self { path }) } - pub fn address(self) -> SocketAddr { - SocketAddr::new(self.address, self.port.get()) + pub fn into_path(self) -> PathBuf { + self.path } } /// A coordinator constructs this after verifying its saved enrollment receipt. /// It authorizes one pinned Hub target on one exact outer SSH connection. -#[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[derive(Clone, Debug, Eq, PartialEq)] pub struct HubPeerRequest { connection: Uuid, endpoint: HubPeerEndpoint, @@ -53,20 +59,20 @@ impl HubPeerRequest { peer, }) } - pub fn connection_id(self) -> Uuid { + pub fn connection_id(&self) -> Uuid { self.connection } - pub fn endpoint(self) -> HubPeerEndpoint { - self.endpoint + pub fn endpoint(&self) -> &HubPeerEndpoint { + &self.endpoint } - pub fn peer(self) -> PeerIdentity { + pub fn peer(&self) -> PeerIdentity { self.peer } } pub struct OpenedHubPeerChannel { pub identity: ChannelIdentity, - /// The consumer must still perform the inner mutually pinned Hub handshake. + /// The consumer must still perform the peer link identity handshake. pub peer: PeerIdentity, pub stream: super::russh_backend::HubPeerStream, } diff --git a/core/crates/transport/remote/src/mod.rs b/core/crates/transport/remote/src/mod.rs index 4bfcffcb..5743c5a2 100644 --- a/core/crates/transport/remote/src/mod.rs +++ b/core/crates/transport/remote/src/mod.rs @@ -77,6 +77,12 @@ pub use protocol::{ }; #[doc(hidden)] pub use rpc::probe_runtime_channel; +#[cfg(any( + all(test, feature = "yolo-unit-tests"), + feature = "broker-test-support" +))] +#[doc(hidden)] +pub use russh_backend::HubPeerTarget; #[cfg(feature = "broker-test-support")] #[doc(hidden)] pub use russh_backend::{BROKER_COMMAND_CANCELLATION_PROGRAM, BrokerCommandCleanupFixture}; diff --git a/core/crates/transport/remote/src/russh_backend.rs b/core/crates/transport/remote/src/russh_backend.rs index 6d9a7d9d..7eaa34a2 100644 --- a/core/crates/transport/remote/src/russh_backend.rs +++ b/core/crates/transport/remote/src/russh_backend.rs @@ -102,7 +102,7 @@ mod broker_command_support; feature = "broker-test-support" ))] pub use broker_command_support::{ - BROKER_COMMAND_CANCELLATION_PROGRAM, BrokerCommandCleanupFixture, + BROKER_COMMAND_CANCELLATION_PROGRAM, BrokerCommandCleanupFixture, HubPeerTarget, }; use super::constants::{ diff --git a/core/crates/transport/remote/src/russh_backend/channel/channel_open_custody.rs b/core/crates/transport/remote/src/russh_backend/channel/channel_open_custody.rs index 4e785d10..2bbc23d6 100644 --- a/core/crates/transport/remote/src/russh_backend/channel/channel_open_custody.rs +++ b/core/crates/transport/remote/src/russh_backend/channel/channel_open_custody.rs @@ -1,5 +1,4 @@ use super::channel_open_cleanup::{cleanup_opening_task, close_session_channel}; -use super::hub_peer::{HUB_PEER_ORIGINATOR_ADDRESS, HUB_PEER_ORIGINATOR_PORT}; use super::{HostKeyHandler, RusshConnection, ssh_error}; use crate::remote::HubPeerEndpoint; use crate::remote::constants::{CHANNEL_OPEN_TASK_FAILURE_CONTEXT, OPERATION_TIMEOUT_MESSAGE}; @@ -60,7 +59,7 @@ impl RusshConnection { Arc::clone(&self.session), reservation, self.operation_timeout, - Some(endpoint), + Some(endpoint.into_path()), ) .wait_until(deadline) .await @@ -72,23 +71,15 @@ impl OpeningSessionChannel { session: Arc>, reservation: RemoteCleanupReservation, cleanup_timeout: Duration, - endpoint: Option, + endpoint: Option, ) -> Self { let opening_session = Arc::clone(&session); let task = tokio::spawn(async move { match endpoint { - Some(endpoint) => { - let address = endpoint.address(); - opening_session - .channel_open_direct_tcpip( - address.ip().to_string(), - u32::from(address.port()), - HUB_PEER_ORIGINATOR_ADDRESS, - HUB_PEER_ORIGINATOR_PORT, - ) - .await - .map_err(super::hub_peer::open_error) - } + Some(path) => opening_session + .channel_open_direct_streamlocal(path.to_string_lossy()) + .await + .map_err(super::hub_peer::open_error), None => opening_session .channel_open_session() .await diff --git a/core/crates/transport/remote/src/russh_backend/hub_peer.rs b/core/crates/transport/remote/src/russh_backend/hub_peer.rs index a0d8db07..d84d10e3 100644 --- a/core/crates/transport/remote/src/russh_backend/hub_peer.rs +++ b/core/crates/transport/remote/src/russh_backend/hub_peer.rs @@ -5,9 +5,6 @@ use super::{ use crate::remote::{ChannelPurpose, HubPeerRequest, OpenedHubPeerChannel, RemoteError}; use tokio::time::Instant; -pub(super) const HUB_PEER_ORIGINATOR_ADDRESS: &str = "127.0.0.1"; -pub(super) const HUB_PEER_ORIGINATOR_PORT: u32 = 0; - pub(super) fn open_error(error: russh::Error) -> RemoteError { match error { russh::Error::ChannelOpenFailure(_) => RemoteError::ChannelRejected, @@ -25,7 +22,7 @@ impl RusshConnection { } let opened = self .open_hub_peer_channel_until( - request.endpoint(), + request.endpoint().clone(), Instant::now() + self.operation_timeout, ) .await?; diff --git a/core/crates/transport/remote/test_support/broker_command.rs b/core/crates/transport/remote/test_support/broker_command.rs index defa7381..a937bb70 100644 --- a/core/crates/transport/remote/test_support/broker_command.rs +++ b/core/crates/transport/remote/test_support/broker_command.rs @@ -34,3 +34,4 @@ mod sftp_wire; pub use broker_command_fixture::{ BROKER_COMMAND_CANCELLATION_PROGRAM, BrokerCommandCleanupFixture, }; +pub use broker_hub_fixture::HubPeerTarget; diff --git a/core/crates/transport/remote/test_support/broker_hub_fixture.rs b/core/crates/transport/remote/test_support/broker_hub_fixture.rs index bf7adf55..b8314526 100644 --- a/core/crates/transport/remote/test_support/broker_hub_fixture.rs +++ b/core/crates/transport/remote/test_support/broker_hub_fixture.rs @@ -2,22 +2,21 @@ use super::broker_command_fixture::BrokerCommandCleanupFixture; use super::constants::{COMMAND_STDOUT, NORMAL_PROGRAM}; use super::memory_fs::FailurePoint; use crate::remote::{CommandPolicy, CommandRequest, MultiplexedSshConnection}; -use std::net::{Ipv4Addr, SocketAddr}; +use std::path::{Path, PathBuf}; -const DYNAMIC_PORT: u16 = 0; +const TARGET_SOCKET: &str = "hub.sock"; impl BrokerCommandCleanupFixture { - pub async fn enable_hub_peer(&self) -> tokio::net::TcpListener { - let listener = tokio::net::TcpListener::bind(SocketAddr::new( - Ipv4Addr::LOCALHOST.into(), - DYNAMIC_PORT, - )) - .await - .unwrap(); - self.memory - .filesystem - .set_hub_peer_target(listener.local_addr().unwrap()); - listener + pub async fn enable_hub_peer(&self) -> HubPeerTarget { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join(TARGET_SOCKET); + let listener = tokio::net::UnixListener::bind(&path).unwrap(); + self.memory.filesystem.set_hub_peer_target(path.clone()); + HubPeerTarget { + _root: root, + listener, + path, + } } pub fn delay_next_channel(&self) { self.memory.filesystem.fail_once(FailurePoint::ChannelOpen); @@ -50,3 +49,15 @@ impl BrokerCommandCleanupFixture { .unwrap(); } } + +pub struct HubPeerTarget { + _root: tempfile::TempDir, + pub listener: tokio::net::UnixListener, + pub path: PathBuf, +} + +impl HubPeerTarget { + pub fn path(&self) -> &Path { + &self.path + } +} diff --git a/core/crates/transport/remote/test_support/russh_backend/hub_peer_evidence.rs b/core/crates/transport/remote/test_support/russh_backend/hub_peer_evidence.rs index b2b2f95f..f5873d2c 100644 --- a/core/crates/transport/remote/test_support/russh_backend/hub_peer_evidence.rs +++ b/core/crates/transport/remote/test_support/russh_backend/hub_peer_evidence.rs @@ -97,7 +97,7 @@ pub struct BurstClosed { pub read_error: Option, } -pub async fn incoming_burst(mut socket: tokio::net::TcpStream) -> io::Result { +pub async fn incoming_burst(mut socket: tokio::net::UnixStream) -> io::Result { let written = socket .write_all(&vec![PAYLOAD_FILL; LARGE_PAYLOAD_BYTES]) .await; diff --git a/core/crates/transport/remote/test_support/russh_backend/hub_peer_fixture.rs b/core/crates/transport/remote/test_support/russh_backend/hub_peer_fixture.rs index 4c434f53..4b3aa3a5 100644 --- a/core/crates/transport/remote/test_support/russh_backend/hub_peer_fixture.rs +++ b/core/crates/transport/remote/test_support/russh_backend/hub_peer_fixture.rs @@ -4,13 +4,10 @@ use super::{ memory_fs::MemoryFilesystem, }; use crate::remote::*; -use std::{ - net::{Ipv4Addr, SocketAddr}, - sync::Arc, -}; -use tokio::{net::TcpListener, time::Instant}; use yolo_hub_delegation::PeerIdentity; use yolo_hub_identity::HubId; +use std::path::PathBuf; +use tokio::time::Instant; #[path = "hub_peer_evidence.rs"] mod evidence; pub use evidence::diagnostic as peer_diagnostic; @@ -18,17 +15,35 @@ pub use evidence::{ Phase, assert_exact_peer_close, incoming_burst, observe_phase, wait_peer_close, }; -pub const DYNAMIC_PORT: u16 = 0; pub const PAYLOAD: &[u8] = b"inner-Hub\0opaque-binary\r\n"; pub const LARGE_PAYLOAD_BYTES: usize = 1_048_576; pub const PAYLOAD_FILL: u8 = b'P'; pub const EXPECTED_HUB_PEER_CHANNELS: usize = 1; +const TARGET_SOCKET: &str = "hub.sock"; + +pub struct PeerTarget { + _root: tempfile::TempDir, + pub listener: tokio::net::UnixListener, + pub path: PathBuf, +} + +impl PeerTarget { + pub fn new() -> Self { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join(TARGET_SOCKET); + let listener = tokio::net::UnixListener::bind(&path).unwrap(); + Self { + _root: root, + listener, + path, + } + } +} pub struct PeerFixture { pub ssh: MemoryConnection, - pub listener: TcpListener, + pub target: PeerTarget, pub request: HubPeerRequest, - pub key: Arc, } impl PeerFixture { @@ -36,38 +51,31 @@ impl PeerFixture { Self::with_timeout(NORMAL_OPERATION_TIMEOUT).await } pub async fn with_timeout(timeout: std::time::Duration) -> Self { - let listener = TcpListener::bind(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), DYNAMIC_PORT)) - .await - .unwrap(); - let target = listener.local_addr().unwrap(); + let target = PeerTarget::new(); let filesystem = MemoryFilesystem::default(); - filesystem.set_hub_peer_target(target); + filesystem.set_hub_peer_target(target.path.clone()); let ssh = super::fixture::connect_memory_with_timeout(false, filesystem, timeout) .await .unwrap(); - let (identity, key) = peer_keys(); + let identity = identity(); let request = HubPeerRequest::new( ssh.connection.info().connection_id, - HubPeerEndpoint::new(target).unwrap(), + HubPeerEndpoint::new(target.path.clone()).unwrap(), identity, ) .unwrap(); Self { ssh, - listener, + target, request, - key, } } } -pub fn peer_keys() -> (PeerIdentity, Arc) { - let (key, _) = keys(); +fn identity() -> PeerIdentity { + let (key, _) = super::fixture::keys(); let bytes = key.public_key().key_data().ed25519().unwrap().0; - ( - PeerIdentity::new(HubId::new(), bytes).unwrap(), - Arc::new(key), - ) + PeerIdentity::new(HubId::new(), bytes).unwrap() } pub fn command() -> AuthorizedCommand { diff --git a/core/crates/transport/remote/test_support/russh_backend/memory_fs.rs b/core/crates/transport/remote/test_support/russh_backend/memory_fs.rs index a2b45ed4..aa8b5577 100644 --- a/core/crates/transport/remote/test_support/russh_backend/memory_fs.rs +++ b/core/crates/transport/remote/test_support/russh_backend/memory_fs.rs @@ -57,7 +57,7 @@ struct State { posix_rename_supported: bool, commands: CommandState, channel_open: Arc, - hub_peer_target: Option, + hub_peer_target: Option, hub_peer_lifecycles: HashMap, uid_response: (Vec, Vec, Option), uid_probes: usize, @@ -103,11 +103,11 @@ impl Default for MemoryFilesystem { } impl MemoryFilesystem { - pub fn set_hub_peer_target(&self, target: std::net::SocketAddr) { + pub fn set_hub_peer_target(&self, target: std::path::PathBuf) { self.0.lock().unwrap().hub_peer_target = Some(target); } - pub(crate) fn hub_peer_target(&self) -> Option { - self.0.lock().unwrap().hub_peer_target + pub(crate) fn hub_peer_target(&self) -> Option { + self.0.lock().unwrap().hub_peer_target.clone() } pub fn fail_once(&self, failure: FailurePoint) { self.0.lock().unwrap().failure = Some(failure); diff --git a/core/crates/transport/remote/test_support/russh_backend/server_hub_peer.rs b/core/crates/transport/remote/test_support/russh_backend/server_hub_peer.rs index f44c616f..2d115e56 100644 --- a/core/crates/transport/remote/test_support/russh_backend/server_hub_peer.rs +++ b/core/crates/transport/remote/test_support/russh_backend/server_hub_peer.rs @@ -1,19 +1,17 @@ macro_rules! hub_peer_handler { () => { - async fn channel_open_direct_tcpip( + async fn channel_open_direct_streamlocal( &mut self, channel: Channel, - host: &str, - port: u32, - _: &str, - _: u32, + socket_path: &str, reply: russh::server::ChannelOpenHandle, _: &mut Session, ) -> Result<(), Self::Error> { let allowed = self.filesystem.hub_peer_target(); - if !allowed.is_some_and(|target| { - host == target.ip().to_string() && port == u32::from(target.port()) - }) { + if !allowed + .as_ref() + .is_some_and(|target| target.as_path() == std::path::Path::new(socket_path)) + { reply .reject(ChannelOpenFailure::AdministrativelyProhibited) .await; @@ -27,7 +25,7 @@ macro_rules! hub_peer_handler { if delayed && !filesystem.permit_channel_open().await { return; } - let mut socket = tokio::net::TcpStream::connect(allowed.unwrap()) + let mut socket = tokio::net::UnixStream::connect(allowed.unwrap()) .await .unwrap(); reply.accept().await; diff --git a/core/crates/transport/remote/unit_tests/hub_peer.rs b/core/crates/transport/remote/unit_tests/hub_peer.rs index d7a52e53..b5a44fad 100644 --- a/core/crates/transport/remote/unit_tests/hub_peer.rs +++ b/core/crates/transport/remote/unit_tests/hub_peer.rs @@ -1,48 +1,43 @@ -use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr, SocketAddrV6}; use yolo_hub_delegation::PeerIdentity; use yolo_hub_identity::HubId; use yolo_remote::{HubPeerEndpoint, HubPeerRequest, RemoteError}; -const PORT: u16 = 42323; -const ZERO_PORT: u16 = 0; -const SCOPE: u32 = 1; const KEY_BYTES: usize = 32; const KEY_FILL: u8 = 7; +const SOCKET: &str = "/run/user/1000/yolo/hub/peer.sock"; #[test] -fn hub_peer_targets_accept_only_nonzero_unscoped_ip_loopback_endpoints() { - for address in [ - IpAddr::V4(Ipv4Addr::LOCALHOST), - IpAddr::V6(Ipv6Addr::LOCALHOST), +fn hub_peer_targets_accept_only_absolute_canonical_socket_paths() { + assert_eq!( + HubPeerEndpoint::new(SOCKET).unwrap().path(), + std::path::Path::new(SOCKET) + ); + for invalid in [ + "", + "peer.sock", + "relative/peer.sock", + "/tmp/../peer.sock", + "/tmp/./peer.sock", + "/tmp/peer\n.sock", + "/tmp/peer\0.sock", ] { - let socket = SocketAddr::new(address, PORT); - assert_eq!(HubPeerEndpoint::new(socket).unwrap().address(), socket); - assert!(HubPeerEndpoint::new(SocketAddr::new(address, ZERO_PORT)).is_err()); - } - for address in [ - IpAddr::V4(Ipv4Addr::UNSPECIFIED), - IpAddr::V4(Ipv4Addr::BROADCAST), - IpAddr::V6(Ipv6Addr::UNSPECIFIED), - ] { - assert!(HubPeerEndpoint::new(SocketAddr::new(address, PORT)).is_err()); - } - for (flow, scope) in [(SCOPE, u32::default()), (u32::default(), SCOPE)] { - let address = SocketAddr::V6(SocketAddrV6::new(Ipv6Addr::LOCALHOST, PORT, flow, scope)); - assert!(HubPeerEndpoint::new(address).is_err()); + assert!(HubPeerEndpoint::new(invalid).is_err(), "{invalid:?}"); } + let long = format!("/{}", "a".repeat(100)); + assert!(HubPeerEndpoint::new(long).is_err()); } #[test] fn a_scoped_hub_peer_request_captures_transport_and_inner_peer_identity() { - let endpoint = HubPeerEndpoint::new(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), PORT)).unwrap(); + let endpoint = HubPeerEndpoint::new(SOCKET).unwrap(); let peer = PeerIdentity::new(HubId::new(), [KEY_FILL; KEY_BYTES]).unwrap(); assert!(matches!( - HubPeerRequest::new(uuid::Uuid::nil(), endpoint, peer), + HubPeerRequest::new(uuid::Uuid::nil(), endpoint.clone(), peer), Err(RemoteError::ChannelMismatch) )); let connection = uuid::Uuid::new_v4(); - let request = HubPeerRequest::new(connection, endpoint, peer).unwrap(); + let request = HubPeerRequest::new(connection, endpoint.clone(), peer).unwrap(); assert_eq!(request.connection_id(), connection); - assert_eq!(request.endpoint(), endpoint); + assert_eq!(request.endpoint(), &endpoint); assert_eq!(request.peer(), peer); } diff --git a/core/crates/transport/ssh/profile/protocol/src/lib.rs b/core/crates/transport/ssh/profile/protocol/src/lib.rs index a8375710..b77c8db0 100644 --- a/core/crates/transport/ssh/profile/protocol/src/lib.rs +++ b/core/crates/transport/ssh/profile/protocol/src/lib.rs @@ -57,6 +57,22 @@ impl TryFrom for SshProfileRef { } } +/// A peer dial names a profile by id; the runtime resolves the current +/// authority revision from the local profile store before opening transport. +pub fn validate_profile_id(profile_id: &str) -> Result<()> { + use constants::*; + ensure!( + !profile_id.is_empty() + && profile_id.len() <= MAX_PROFILE_ID_BYTES + && ![CURRENT_DIRECTORY, PARENT_DIRECTORY].contains(&profile_id) + && profile_id + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || matches!(byte, b'-' | b'_' | b'.')), + INVALID_PROFILE + ); + Ok(()) +} + impl From for ProfileFields { fn from(profile: SshProfileRef) -> Self { profile.0 diff --git a/docs/README.md b/docs/README.md index 52f57e07..bddc2f98 100644 --- a/docs/README.md +++ b/docs/README.md @@ -9,6 +9,8 @@ validation notes and internal release evidence are not part of the public tree. - [Architecture](architecture.md): process boundaries and ownership. - [Concept model](concept-model.md): terminals, agents, runs and authority. - [Operations](operations.md): install, launch, diagnostics and removal. +- [Local service lifecycle](local-service-lifecycle.md): status, start, stop, + restart and release activation. ## Runtime and protocol @@ -28,6 +30,7 @@ validation notes and internal release evidence are not part of the public tree. - [Hub client channels](hub-client-channels.md) - [Hub inventory](hub-inventory.md) and [offline inventory](hub-offline-inventory.md) - [Hub management](hub-management.md) and [local startup](hub-local-startup.md) +- [Machines](machines.md) - [Hub agent control](hub-agent-control.md) and [agent directory](hub-agent-directory.md) - [Hub native control](hub-native-control-execution.md) - [Hub native workspaces](hub-native-workspaces.md) diff --git a/docs/adr/0036-peer-links-over-ssh-streamlocal.md b/docs/adr/0036-peer-links-over-ssh-streamlocal.md new file mode 100644 index 00000000..179b914a --- /dev/null +++ b/docs/adr/0036-peer-links-over-ssh-streamlocal.md @@ -0,0 +1,141 @@ +# ADR 0036: Peer links over SSH streamlocal + +## Status + +Accepted. This ADR narrows the transport assumption in +[ADR 0012](0012-federated-hub-protocol.md) for Hub-to-Hub links. It does not +change resource ownership, directed federation, delegation or origin policy. + +## Context + +The current Hub peer transport runs its own SSH server on a TCP listener. A +pairing code authorizes the first exchange (`yolo-enroll` plus a password), and +the joining side must know a dialable host and port. That creates a public Hub +port even when both Hubs are already reachable through the user's SSH access, +and it adds a second authorization step after SSH has already authenticated the +transport. + +The intended model is: + +```text +A (entry Hub) B (remote Hub) +SSH profile / broker ── user SSH ──────────────▶ system sshd + ── direct-streamlocal ───▶ /peer.sock + │ + PeerHello (Hub identity) + │ + one Cap'n Proto RPC connection + │ + Federation +``` + +The outer SSH connection owns reachability and account trust. The Hub owns the +peer identity and the peer policy. + +## Decision + +### Transport + +- A Hub exposes one Unix socket at `/peer.sock`. The state directory + is private (0700) and the socket is owned by the Hub user (0600); there is no + TCP peer listener. +- The entry Hub resolves an SSH profile, and the remote broker opens + `direct-streamlocal` to that socket. The broker never opens a public Hub + port. +- Peer addresses are SSH-only: a local SSH profile id plus the remote Hub's + absolute socket path. The profile id is resolved against the current local + profile store when the link is opened. `PeerAddress::Tcp`, `hub listen + ` and the public listener binding schema are removed. + +### Identity + +SSH authenticates the transport and the user account. It does not identify a +Hub; a user may operate more than one Hub behind one SSH account. + +Each link starts with one lightweight `PeerHello` exchange: + +1. The client sends its `PeerIdentity`, a bounded version and a random nonce, + signed by its Hub key. +2. The server verifies the presented signature, then requires the identity to + be in its configured peer set before replying with its own identity and a + nonce signed over the client nonce. +3. The client verifies the configured remote identity and signature. + +The handshake is identity binding, not a second authorization and not a pairing +code. There is no `yolo-enroll` user, invite code or enrollment ledger. + +### RPC + +After `PeerHello`, the same byte stream carries one +`yolo_hub_peer_rpc::Connection`. The Cap'n Proto RPC engine already multiplexes +concurrent requests and event capabilities, so the peer link needs no inner SSH +server, no SSH channels and no custom multiplexer. + +A link is **directed**. The dialing Hub is the RPC client and the accepting Hub +serves that one connection. Discovery and forwarded calls share the client; the +accepted side never opens a reverse request. This is the natural shape of +`direct-streamlocal` through a system sshd: the SSH client is the user's +connection, so the Hub has no channel through which to initiate traffic back. +A Hub that both serves and consumes a peer therefore holds two links, one in +each direction, each opened through its own SSH profile. + +Forwarding follows the same rule: every hop that sends a call must have an +outbound link to the next hop. A peer entry without a dial can still be a +provider and can still pin policy; it simply cannot originate calls. + +Link capacity is the RPC in-flight bound. A connection-level failure retires +the outbound link and its routing proofs. + +### Policy and onboarding + +SSH authorization never grants export rights. `consume` and `exports` remain +explicit fields of the local peer configuration. + +- Managed remote Hubs: Hub-owned remote setup configures both sides, writes the + local dial with the profile id and the remote `peer.sock` path, and adds the + remote export policy. +- Independent Hubs: `peer add --ssh-profile + --remote-socket ` writes the local peer. The provider side must pin the + consumer Hub identity and configure its export policy locally. +- Unknown peers are never trusted on first use. A peer entry with an expected + identity and policy is required before the handshake is accepted. + +### Amendment for 0.2.3 user peer onboarding + +`yolo hub peer join ` replaces profile-based onboarding for +independent Hubs. The dial stores the SSH destination, the SSH child's stdio +carries the peer stream through the hidden `peer-stdio` bridge, and the +provider's SSH account is the admission boundary. `PeerHello` still binds Hub +identities; the provider's local share policy replaces per-join approval +(strict registration remains available). Managed remote setup keeps the +profile-based broker transport. + +## Consequences + +- Pairing codes, `MintInvite`, `yolo1_`/`yolo2_`, the enrollment server and the + TCP listener disappear together. +- Configuration migration keeps peer pins, `consume` flags and export policy, + but drops legacy TCP/loopback dials because they cannot name a Hub socket. + Re-running remote setup, or adding the peer through an SSH profile, restores + the dial. +- The broker must allow streamlocal forwarding to the Hub socket; an SSH server + that forbids streamlocal forwarding cannot carry peer links. +- The public client ingress keeps its existing SSH streamlocal path; peer links + do not reuse the public client socket or its authorization. +- Live acceptance must cover two Hubs over a real SSH account, concurrent + discover/execute/event traffic on one link, link loss and recovery, socket + permissions, and macOS/Linux builds. + +## Non-goals + +- No generic SSH subsystem, third-party capability handoff or global Hub + discovery. +- No trust-on-first-use enrollment and no implicit export grants from SSH + account access. + +## Amendment for 0.3.0 machine vocabulary + +The user-facing entity is a machine running one Yolo Hub. `yolo machine add` +replaces `yolo hub peer join`, `yolo machine pin` replaces `yolo hub peer add`, +and the TUI inventory is `Machines` rather than `Hubs`. The directed import +model, the SSH admission boundary and the peer wire protocol are unchanged. diff --git a/docs/client-resource-protocol.md b/docs/client-resource-protocol.md index bed8f90d..dddf5812 100644 --- a/docs/client-resource-protocol.md +++ b/docs/client-resource-protocol.md @@ -20,7 +20,7 @@ zoom, pending input and writer leases are client state. Updating or closing a vi does not terminate a Terminal. Shared documents do not restore control authority. The TUI stores its chosen LayoutAddress and last acknowledged shared projection -alongside private presentation state. `yolo layouts [--json]` lists layout +alongside private presentation state. `yolo layout list [--json]` lists layout summaries; `yolo --layout ` selects an existing document at the entry Hub. Default client instances retain independent document identities. Explicitly selecting a missing layout fails instead of creating a substitute. diff --git a/docs/communication-architecture.md b/docs/communication-architecture.md index 54604f5b..4bbe8237 100644 --- a/docs/communication-architecture.md +++ b/docs/communication-architecture.md @@ -121,7 +121,10 @@ Terminal snapshot contents and their journal barrier are one published value. Batch append/reconciliation and the complete terminal projection share the journal lock, following journal-to-terminal lock order. Output events and periodic checkpoints follow the complete projection. A snapshot cannot advertise a batch's -tail sequence while its screen contains only a prefix. Healthy, definitely +tail sequence while its screen contains only a prefix. A snapshot taken while +the alternate screen is active carries the alternate-screen mode escape, so a +fresh parser paints the alternate grid and a later exit restores the primary +grid instead of retaining the snapshot's text. Healthy, definitely uncommitted append failures stay with the existing pending-output retry owner; only uncertain journal writes require reconciliation. diff --git a/docs/hub-creation-cleanup.md b/docs/hub-creation-cleanup.md index 9fc1f282..c1847c9e 100644 --- a/docs/hub-creation-cleanup.md +++ b/docs/hub-creation-cleanup.md @@ -12,8 +12,8 @@ capability, a substituted origin/session, or a finalized creation cannot authori cleanup. Public replies do not echo the capability. Capabilities belong in private input, not process arguments or diagnostics. -Normal terminal lifecycle uses the public `Terminal` command (`yolo terminate` -and `yolo archive`) and never requires a creation capability. Creation cleanup +Normal terminal lifecycle uses the public `Terminal` command (`yolo terminal terminate` +and `yolo terminal archive`) and never requires a creation capability. Creation cleanup is the recovery path for a creation whose binding failed. The internal Host control surface accepts that private input from stdin as diff --git a/docs/hub-inventory.md b/docs/hub-inventory.md index 47e11999..8afeb78a 100644 --- a/docs/hub-inventory.md +++ b/docs/hub-inventory.md @@ -28,11 +28,13 @@ Cancelling an in-flight refresh retires its protocol connection. ## Native TUI -Sessions, Agents and Hubs are separate inventory tabs. Agent rows display the +The sidebar is a machine tree: machine roots are authenticated Hub origins and +Agent rows are grouped beneath the machine that runs them. Agent rows display the reported lifecycle, activity, source, recovery availability, reachability and -freshness. Search covers stable addresses, providers, associated session names, -working directories and reported states. A Hub scope filters Sessions and Agents -but never hides the other Hub choices. +freshness. Search covers stable addresses, providers, associated terminal names, +working directories and reported states. Machines without a catalogue row but +with agent, terminal or tab origins are synthesized so no workspace is hidden; +the `g` picker still lists terminals across machines. Agent directories are independent observed origin metadata; see [Agent working directories](hub-agent-directory.md) for freshness and upgrade rules. Refresh failure retains the last observations but marks them Stale; current @@ -44,12 +46,13 @@ Agent cannot redirect selection into a replacement process. The terminal still uses the existing origin-authorized writer lease; the inventory owns no process custody and contains no PTY parsing or provider launch logic. -Shell creation respects the selected Hub scope. Shell terminate/archive/resume -commands remain in the Sessions view; the Agent view does not reinterpret these -as semantic Agent control. Unsupported semantic controls remain unavailable. -Existing durable workspaces continue to store pane layout and terminal bindings; -catalogue tabs, search and temporary Hub scopes are transient view state. +Shell creation targets the active machine workspace. Terminal +terminate/archive/resume commands remain in the terminal picker; the machine tree +does not reinterpret these as semantic Agent control. Unsupported semantic +controls remain unavailable. Existing durable workspaces continue to store pane +layout and terminal bindings; catalogue tabs and search are transient view state. -Developer key bindings: Tab/Shift-Tab switches tabs; Enter on a Hub selects its -scope; Escape clears scope and filter; Enter on an Agent opens its bound terminal; -`n` creates a Shell on the selected scope, or on the entry Hub when unscoped. +Developer key bindings: Up/Down moves the tree selection; Enter activates the +selected machine or Agent; Left/Right or Space collapse and expand a machine; +Escape clears the filter; `/` filters; `n` creates a Shell on the selected +machine. diff --git a/docs/hub-local-startup.md b/docs/hub-local-startup.md index b31888e9..76cab8ea 100644 --- a/docs/hub-local-startup.md +++ b/docs/hub-local-startup.md @@ -15,6 +15,12 @@ existing public connection-configuration schema. The single executable uses its own image for the background Host and Hub roles. No provider launch wrapper or new hook-ingress endpoint is involved. +`yolo server start`, `yolo server stop` and `yolo server restart` are the +supported operator entries to this boundary. `yolo server status` and +`yolo server version` resolve the same profile sockets and report the installed +image separately from the images the running Hub and Host reported; status +never starts services. + The bootstrap operation uses a private state-directory coordination lock. It validates a persisted Hub identity without acquiring the running service's singleton lock. Empty state can be initialized; corrupt, copied, rebound or diff --git a/docs/hub-management.md b/docs/hub-management.md index 924d45f8..17ad806e 100644 --- a/docs/hub-management.md +++ b/docs/hub-management.md @@ -3,8 +3,9 @@ Hub discovery is entry-relative, not a globally replicated inventory. For authorized A -> B -> C sharing, A sees A/B/C resources, B sees B/C, and C sees C. B serves its own resources and relays C without changing C's resource addresses -or acquiring ownership of C's PTYs. Sharing direction is independent of which -endpoint dials the authenticated SSH connection. +or acquiring ownership of C's PTYs. Sharing direction is policy, not transport: +a Hub dials every next hop it needs, and an accepted link never carries reverse +requests. ## Public Management Boundary @@ -12,7 +13,8 @@ endpoint dials the authenticated SSH connection. Mac, TUI, and independent clients can use the same public command contract: - `GetConfiguration {}` returns the local Hub's public identity, durable revision, - latest change receipt, configuration, activation status, and actual listener. + latest change receipt, configuration, activation status, and published peer + socket. - `Configure { change }` carries a non-nil request ID, expected revision, and the complete desired `federation_settings::Configuration`. @@ -24,12 +26,17 @@ and terminal control remain separately authorized and origin-addressed. Public clients use JSON-RPC 2.0 and negotiate the current Hub capability schema. Peer negotiation is a separate internal Cap’n Proto contract. Configuration DTOs reject unknown fields, invalid identity/revision values, and contradictory -activation/listener state. A Configure success must match its exact request ID, +activation/peer-socket state. A Configure success must match its exact request ID, new revision, and desired configuration and must report `applied: true`. The public `get_configuration` and `configure` methods transport these commands. -The [TUI](tui-hub-management.md) has a configuration editor using this -entry-local administration boundary. +The [TUI](tui-machine-management.md) has a configuration editor using this +entry-local administration boundary. `yolo machine pin` writes one machine with +its SSH destination, remote socket and export policy; there is no pairing code or +enrollment operation. The local Hub publishes `/peer.sock`; the +remote side pins the importing machine identity and configures its export policy +locally or through remote setup. See [Machines](machines.md). +`Configure` remains the only general configuration mutation. ## Persistence And Concurrency @@ -48,11 +55,10 @@ the same request ID conflict. Older receipts are not a general operation journal `--peer-config` is an explicit initialization seed. Once the Hub has a persisted configuration, an explicitly supplied conflicting file fails startup rather than silently replacing durable policy, and the error points at this boundary. Normal -subsequent starts can omit the seed. Change peers or the listening endpoint on a -persisted Hub through `Configure` (the JSON-RPC method behind the TUI and Mac -configuration editors); a restart seed cannot update them. This protects a -running owner from replacement, not against an authorized user rewriting valid -historical state while the service is stopped. +subsequent starts can omit the seed. Change peers on a persisted Hub through +`Configure` (the JSON-RPC method behind `yolo machine pin`, the TUI and the Mac +configuration editors); a restart seed cannot update them. This protects a running owner from replacement, not against an authorized +user rewriting valid historical state while the service is stopped. ## Activation And Failure @@ -61,12 +67,12 @@ continues if the requesting client times out or disconnects. Clients must inspec the resulting revision/receipt after an uncertain outcome, not automatically replay a mutation. -An update validates policy and reserves a changed listening endpoint first. -Bind failure leaves the existing generation intact. For activation, the actor -fences old admission, stops and joins peer tasks, closes old peer channels, -drains public operations, and releases the old Publication owner. It persists -the desired configuration before constructing the replacement generation. -Unchanged ephemeral listeners retain their actual port across updates. +An update validates policy first. For activation, the actor fences old +admission, stops and joins peer tasks, drops old peer links, drains public +operations, and releases the old Publication owner. It persists the desired +configuration before constructing the replacement generation. The peer socket +is derived from Hub state and is republished once for the service lifetime, so +an update never rebinds it. History transfer contains only origin-signed catalogues, never delegated grants, route proofs, or connection generations. History stays unreachable until new diff --git a/docs/hub-multipath.md b/docs/hub-multipath.md index b9bc93ab..7757a655 100644 --- a/docs/hub-multipath.md +++ b/docs/hub-multipath.md @@ -1,7 +1,7 @@ # Direct and Relayed Hub Paths The live multipath test exercises A importing C both directly and through B. -It uses production Hub/Federation services over real authenticated SSH channels, +It uses Federation and Hub services over the production Unix peer-socket link, separate Host/Worker processes and a native-hook Agent fixture in a real PTY. The Hub services themselves run as separate tasks in the test process, not as three independently launched Hub executables. @@ -12,7 +12,7 @@ three independently launched Hub executables. exchange, proving that the original attachment uses the direct path. 2. Start B with an origin-authorized C export to A. Verify both authenticated connections, one C Agent address, and one matching Session/Runtime target. -3. Cut the test proxy's owned A-to-C TCP forwarding streams and reject reconnects. +3. Cut the test proxy's owned A-to-C forwarding streams and reject reconnects. The existing direct attachment must terminate without reporting process exit. 4. Wait for the relay-only connection and reachable C inventory. Attach using the original output cursor, perform a new input once, and confirm the same runtime. diff --git a/docs/hub-remote-setup.md b/docs/hub-remote-setup.md index 8b3f2839..363e15b8 100644 --- a/docs/hub-remote-setup.md +++ b/docs/hub-remote-setup.md @@ -58,16 +58,15 @@ Local configuration conflicts remain visible and cannot overwrite another edit. The configured peer retains its original Hub identity and uses explicit SSH transport; imported Agents do not acquire the entry Hub's identity. -An enrolled loopback listener configured with port zero retains its assigned -address across Hub restart. Its binding has current and pending configuration -slots, while the private configuration record durably distinguishes unfinished -first initialization from loss of an established binding. The service exposes -an applied configuration only after both records and the actual socket agree. -An occupied saved port fails startup instead of silently changing the endpoint. -Only the current configuration schema is admitted. A record without binding -evidence remains unavailable until an explicit current-schema configuration is -committed; startup never migrates an old record or assigns it a replacement -endpoint automatically. +The remote Hub publishes its peer entry point at `/peer.sock`; the +bootstrap receipt returns that absolute path. Remote setup writes the entry +Hub's dial with the selected SSH profile id and that path, and the remote +configuration pins the consumer identity with its export policy. The socket is +derived from Hub state and republished for the service lifetime, so it has no +ephemeral port or binding-record exchange. An occupied or foreign socket fails +startup instead of silently moving the endpoint. Legacy configuration records +that still carry listener evidence are read without it; the retired `listen` +field and TCP dials are dropped from peer entries on migration. ## Client operations @@ -95,8 +94,8 @@ through peer resource grants. The Mac and TUI setup wizard, complete operation-history presentation, Linux native recovery and release-package gates remain tracked in the project roadmap. -The existing peer editors preserve SSH bindings, but that is not a replacement -for the complete onboarding interface. +The existing peer editors preserve SSH profile and socket dials, but that is not +a replacement for the complete onboarding interface. Runtime catalog schema 2 packages the single `yolo` image and its role descriptor for each Linux architecture; see [bundle catalog](runtime-bundle-catalog.md). An actual Linux artifact build diff --git a/docs/hub-ssh-profiles.md b/docs/hub-ssh-profiles.md index 5e089093..1dcfa5b7 100644 --- a/docs/hub-ssh-profiles.md +++ b/docs/hub-ssh-profiles.md @@ -1,10 +1,15 @@ -# Persistent Hub SSH profiles +# Managed Hub SSH profiles -Federation peer addresses explicitly select TCP or SSH. An SSH address contains -a stable profile ID and expected profile authority digest, plus the remote Hub's -canonical loopback address and nonzero port. Missing or changed SSH profiles fail -connection admission; they never fall back to TCP. The configured Hub public key -remains mandatory for the inner mutually authenticated Hub connection. +This registry belongs to managed remote setup, not to `yolo machine add`. +User machine links carry an SSH destination and a peer socket directly; they never +create or read a broker profile. The registry below records the broker state and +config revision used by remote setup's managed links. + +Machine addresses that name a profile are managed-only. A broker profile +contains a stable local profile ID and the remote Hub's absolute +`/peer.sock` path. The profile ID is resolved against the current +registry when the link opens; missing or changed profiles fail admission. The +configured Hub public key remains mandatory for the `PeerHello` handshake. The persistent Hub owns the profile registry and the federation dialer. Public `GetSshProfiles` and `ConfigureSshProfiles` commands are local management operations; @@ -24,7 +29,7 @@ instance rather than silently adopting replacement state. Ordinary later updates use the public commands, so an initially empty registry can be populated without restarting the Hub. The optional seed must agree with an existing registry. Every real Hub installs the SSH-aware dialer; embedded Hubs -without one continue to reject SSH transport requests explicitly. +without one reject dials explicitly. ## Broker and channel lifetime @@ -48,7 +53,7 @@ watchers use weak references so they do not retain Hub or profile-store locks. Remote Agent processes and PTY custody remain at the origin. Existing adopted brokers can survive entry Hub exit. A restarted entry Hub reads -its registry, verifies the existing broker and opens a new inner Hub connection. +its registry, verifies the existing broker and opens a new peer link. Normal tunnel EOF or cancellation closes that tunnel only. Ambiguous SSH channel open or failed transport cleanup may still close the outer SSH connection under the existing conservative cleanup policy; sibling-channel survival is not an diff --git a/docs/local-service-lifecycle.md b/docs/local-service-lifecycle.md new file mode 100644 index 00000000..e706ec1f --- /dev/null +++ b/docs/local-service-lifecycle.md @@ -0,0 +1,96 @@ +# Local Service Lifecycle + +Yolo runs one local Host and Hub for the current profile. The `yolo server` +commands inspect and control them without entering the TUI. See +[upgrade admission](upgrade-admission.md) for the compatibility boundary that +`yolo update` and `yolo server restart` enforce. + +## Status and Version + +```bash +yolo server status # Hub/Host PIDs, images, protocol and admission state +yolo server version # installed image and the image each service is running +yolo server start # start the local Host and Hub +yolo server stop # stop the local Hub, then the Host +yolo server restart # stop and start the same profile +``` + +All five commands address the profile selected by `--local-profile` or the +platform default, and accept `--json`. They are local-only: `--hub`, `--socket` +and `--endpoint` describe a remote entry and are rejected. `yolo --version` +remains the single-line binary version contract that remote deployment probes +compare byte for byte, so it never reports service state; use `yolo server version` +when the question is which image is installed versus which image the running +Host and Hub actually mapped. + +## Stop and Restart + +For a detached profile, `yolo server stop` resolves each service PID from its +private Unix socket's peer credentials and sends SIGTERM to that exact PID. It +never matches process names. It stops the Hub first and then the Host. Host +shutdown closes ingress but leaves session Workers attached to their PTYs, so +stopping services does not terminate running shells. A later `yolo server start` +reconnects the Hub to the existing Host and Workers. If a service does not exit +within the bound, the command fails with the exact PID instead of escalating to +SIGKILL. + +For a detached profile, `yolo server restart` validates the runtime contract and, +on Linux, delegated Host custody before stopping anything. An incompatible state +or an un-delegated shell leaves the running services untouched. A profile owned +by systemd must be restarted through its selected Host runner and Hub units. + +On Linux a new Host requires delegated cgroup v2 custody. `yolo server start` reports +that requirement before launching anything; start the profile under a delegated +systemd service, or attach to an existing Hub instead. + +If a Host from another image is already running, `yolo server start` leaves it intact +and reports the new Hub's exit, its error and the running Host image; restart +the profile with `yolo server restart` to replace both services. + +## GitHub Releases and Updates + +Official assets are published to GitHub Releases by the GitHub Actions release +workflow, triggered by the GitLab tag or schedule pipeline. A tag that starts +with `v` creates a stable release; the scheduled nightly pipeline creates a +prerelease on the `nightly--` channel. + +Install the latest stable release: + +```bash +curl -fsSL https://raw.githubusercontent.com/AgentsMesh/YoloShell/main/scripts/install.sh | sh +``` + +Update to the latest GitHub release: + +```bash +yolo update +``` + +This checks the selected channel, installs a newer immutable image under +`~/.local/lib/yolo//yolo`, atomically points `~/.local/bin/yolo` at it, +and probes the live service images, Hub protocol, storage contract and effective +service owner. Detached services can be restarted by the command. Profiles +owned by systemd units must be activated by restarting those units, so the +command refuses to signal their socket owners. Admission must report `ready`, +and a failed check exits nonzero. If no services are running it reports the +install and leaves them stopped. + +Advanced modes: + +```bash +yolo update --check # check the latest release only +yolo update --admit # probe the installed image without the network +yolo update --restart # restart the installed image, then probe +yolo update --no-restart # install a newer release; activate it later +yolo update --channel nightly # select the nightly channel +``` + +`--check` ends with `update available: ` or `yolo is already +current`, so callers do not compare the two printed versions themselves. +`--no-restart` leaves the running services untouched. It prints an activation +hint when an installed image is not yet running. + +For a non-default profile, pass `--local-profile ` to `yolo update`. + +Release assets cover macOS arm64, Linux x86_64 and Linux arm64. Linux archives +are built against glibc 2.31 so they run on the supported Linux baseline. diff --git a/docs/machines.md b/docs/machines.md new file mode 100644 index 00000000..9caabad1 --- /dev/null +++ b/docs/machines.md @@ -0,0 +1,111 @@ +# Machines + +A machine runs one Yolo Hub: the local agent hub that owns agents, terminals +and layouts. Importing agents from another machine uses the user's existing SSH +access as its transport. A machine never opens a public peer port, runs its own +SSH server, or asks the user to prepare a Yolo SSH profile. If `ssh ` +already works and accepts the target's host key, `yolo` can import from it. The +login must be non-interactive: `yolo machine add` runs `ssh` with +`BatchMode=yes`, so a key or SSH agent is required and a password prompt fails +instead of hanging. + +A link is **one-way**: this machine imports the remote machine's agents and +terminals; the remote machine does not import this machine's. To make it +mutual, run `yolo machine add` on both sides and configure each side's exports. + +## Adding a machine + +On the importing machine, one command: + +```sh +yolo machine add user@host +``` + +Both machines must run this release or later: the remote serves the hidden +`peer-stdio` bridge and the public `machine identity` command, and the local +machine implements the SSH dialer. The remote machine's `yolo` must be on the +SSH session's `PATH` or at `~/.local/bin/yolo`. + +The command: + +1. runs `yolo machine identity --json` on the remote machine over the same SSH + connection; +2. learns the remote machine's Hub ID, Ed25519 public key and absolute + `peer.sock` path over that authenticated channel, and pins the identity + locally; +3. writes one machine with the SSH destination and socket; +4. opens the link as one SSH child: + +```text +ssh -T user@host \ + 'yolo internal hub --peer-stdio --peer-socket /path/to/peer.sock' +``` + +The SSH child's stdin/stdout carry the `PeerHello` and Cap'n Proto RPC stream. +`peer-stdio` is a hidden bridge that copies bytes to the remote machine's +private peer socket; it is not a listener and does not parse SSH. +`~/.ssh/config`, the SSH agent and `known_hosts` remain the user's own trust +configuration. No key, profile, broker or port is copied or managed by Yolo. + +The remote machine does not run a command per import. Its Hub accepts the +bridge connection on the same-user `peer.sock` and applies its local sharing +policy. A remote setting can require explicit registration; the default trusts +the SSH account boundary. + +The remote machine normally runs its Hub already. If it is stopped, the +discovery command starts it as the SSH account; on Linux that requires the same +delegated cgroup custody as any local start, so the SSH session must be able to +launch the Host. Starting the remote machine with `yolo server start` first avoids +that requirement. + +Useful companions: + +```sh +yolo machine identity --json # this machine's Hub ID, public key, peer socket +yolo machine list # configured machines and their SSH dials +yolo machine remove +``` + +`yolo machine add --pin-only` discovers and pins the machine identity without +importing its agents. For an explicit machine, `yolo machine pin + --ssh user@host --remote-socket ` writes the same entry without +discovery. + +On the machine being imported from, `yolo machine consumers` lists the +machines currently importing from it, with their link count and first-connected +time. The TUI `Manage machines` view exposes the same read-only list on its +Consumers page. This is a live inbound view; it does not grant or revoke +access. The remote machine's export policy remains the authority. + +## Trust model + +`peer.sock` lives in a private state directory. Only the remote machine's user +can reach it, and anyone who can SSH as that user can already run Yolo there. +The SSH account is therefore the admission boundary; a second key exchange or a +per-import approval would add friction without adding authority. + +The `PeerHello` handshake still runs on every link. It binds the stream to the +remote machine's Hub identity discovered during `machine add` (a changed +identity fails closed) and labels the consumer for routing, audit and policy. +It is automatic, not a user step. + +The remote machine decides what it shares. The default policy grants its own +origin to SSH-authenticated consumers with the same rights as local access; an +explicit per-machine export policy overrides it. `ssh_peers.require_registration` +in the Hub configuration restores the strict mode: only pinned machines may +connect, and their individual export policies apply. This is useful for a +shared SSH account or a multi-tenant host. + +## Direction + +A link is directed: the importing machine consumes and the remote machine +exports. Two machines that import from each other hold two links, each opened +by its own `machine add`. Forwarding still requires an outbound link at every +hop. + +## Migration + +0.2.x peer addresses named a local SSH broker profile. That transport remains +only for managed remote setup; user links are SSH destinations. 0.1.x TCP dial +addresses are retired and dropped from configuration. Pins, import flags and +export policies survive. Re-add machines with `yolo machine add`. diff --git a/docs/operations.md b/docs/operations.md index dab4cf01..322266dc 100644 --- a/docs/operations.md +++ b/docs/operations.md @@ -30,36 +30,18 @@ hooks, not terminal output. Disabled, untrusted, restricted, or incomplete hooks remain unmanaged/Unknown; Yolo does not grant provider hook trust. Resume requires verified durable identity. -## GitHub Releases and Updates +## Local Services and Updates -Official assets are published to GitHub Releases by the GitHub Actions release -workflow, triggered by the GitLab tag or schedule pipeline. A tag that starts -with `v` creates a stable release; the scheduled nightly pipeline creates a -prerelease on the `nightly--` channel. +Use `yolo server status` and `yolo server version` to distinguish the installed +image from the images the running Host and Hub mapped. `yolo server start`, +`yolo server stop` and `yolo server restart` control the current profile; +stopping services preserves live Workers and PTYs. `yolo update` checks for a +newer release, installs it, and activates it through the same restart path; +`--check` is the read-only mode. -Install the latest stable release: - -```bash -curl -fsSL https://raw.githubusercontent.com/AgentsMesh/YoloShell/main/scripts/install.sh | sh -``` - -Check or install a newer GitHub release: - -```bash -yolo update --check-release -yolo update --install-release -yolo update --install-release --channel nightly -``` - -The updater downloads the platform archive and `SHA256SUMS`, verifies the -digest, installs an immutable image under `~/.local/lib/yolo//yolo`, and -atomically points `~/.local/bin/yolo` at it. It does not restart services. -Run `yolo update` without release flags to probe the live Host/Hub image, -protocol and storage contract; restart the Host runner and Hub only after that -admission check reports `ready`. - -Release assets cover macOS arm64, Linux x86_64 and Linux arm64. Linux archives -are built against glibc 2.31 so they run on the supported Linux baseline. +See [local service lifecycle](local-service-lifecycle.md) for the command +contract and [upgrade admission](upgrade-admission.md) for the compatibility +checks. ## Tabs, Panes, and Terminals @@ -78,11 +60,11 @@ detaches its views and leaves live PTYs and processes running. - `Ctrl-B d` exits the client while preserving its terminals; `yolo attach` reconnects. - `Ctrl-B g` or `yolo terminals` opens the terminal picker; `Ctrl-B s` focuses - the Agent runs / Hubs sidebar. + the machine tree sidebar. - `Ctrl-B w` opens the tab manager; `Ctrl-B c` creates a tab with a shell; `Ctrl-B ?` shows help. -- `yolo clients` lists attached clients; `yolo detach [CLIENT_ID]` disconnects - one from another shell. +- `yolo client list` lists attached clients; `yolo client detach [CLIENT_ID]` + disconnects one from another shell. One holder controls terminal input at a time. A second pane or client that opens the same terminal is read-only. Explicit takeover fences the previous @@ -91,11 +73,13 @@ shell in its original pane should acquire control without that action. ## Hubs and Remote Runtime -The Hubs inventory exposes `Configure Hub` (`m`). Configuration belongs to the -entry Hub: it manages peer connections, trusted origins, file roots, listener -settings, and per-peer export policies. Reciprocal identity enrollment and -export authorization must also be configured at the other Hub; the TUI does not -silently enroll an unknown key. See [TUI Hub management](tui-hub-management.md). +The Machines inventory exposes `Manage machines` (`m`). Configuration belongs +to the local Hub: it manages machine SSH/socket dials, trusted origins, file +roots and per-machine export policies. Reciprocal identity pinning and export authorization +must also be configured at the remote machine; the TUI never trusts an unknown key. +Machine links use the user's SSH access to `/peer.sock` and open no +public Hub port. See [Machines](machines.md) and [TUI Hub +management](tui-machine-management.md). One SSH transport multiplexes runtime, attachment, bounded command, SFTP read, and deployment channels. Host keys are verified before authentication against diff --git a/docs/release-pipeline.md b/docs/release-pipeline.md index d25b646c..d4ab70eb 100644 --- a/docs/release-pipeline.md +++ b/docs/release-pipeline.md @@ -74,10 +74,9 @@ The release body and README point at: curl -fsSL https://raw.githubusercontent.com/AgentsMesh/YoloShell/main/scripts/install.sh | sh ``` -`yolo update --check-release` reads the GitHub release API; -`yolo update --install-release` downloads the matching archive, verifies -`SHA256SUMS`, installs an immutable image under `~/.local/lib/yolo//yolo` -and atomically updates `~/.local/bin/yolo`. `--channel nightly` selects the -newest prerelease. The updater never restarts Host/Hub services; run -`yolo update` for the existing live-service admission check before restarting -them. +`yolo update` reads the GitHub release API, downloads the matching archive and +verifies `SHA256SUMS`, installs a newer immutable image under +`~/.local/lib/yolo//yolo`, atomically updates `~/.local/bin/yolo`, restarts +the local Host and Hub when needed, and probes live service admission. +`--check` is read-only, `--no-restart` installs without activating, and +`--channel nightly` selects the newest prerelease. diff --git a/docs/remote-hub-delivery.md b/docs/remote-hub-delivery.md index 24e937f5..9d8591c5 100644 --- a/docs/remote-hub-delivery.md +++ b/docs/remote-hub-delivery.md @@ -45,16 +45,18 @@ protocol mismatch rejects enrollment without replacing surviving services or Workers. Unsupported records are rejected; no automatic migration or alias endpoint is provided. -Enrollment adds a loopback listener and an explicit consumer export with the -requested rights and reexport depth. It preserves unrelated configuration and -rejects conflicting existing policy. A configuration mutation uses its exact -base revision. After an unknown result, only the exact request ID, resulting -revision, document and applied state can confirm success through read requests. +Enrollment records the remote Hub's pinned identity and peer socket, then plans +a consumer peer entry that dials that socket through the request's SSH profile, +carrying the requested rights and reexport depth. It preserves unrelated +configuration and rejects conflicting existing policy. A configuration mutation +uses its exact base revision. After an unknown result, only the exact request ID, +resulting revision, document and applied state can confirm success through read +requests. -The SSH transport can open a dedicated `direct-tcpip` channel to the remote -loopback Hub while keeping other channels on the same SSH connection. The inner -Hub connection retains pinned mutual authentication and delegated authorization. -Outer SSH login and public build metadata are not substitutes for Hub authority. +The SSH transport opens a dedicated `direct-streamlocal` channel to the remote +Hub's peer socket while keeping other channels on the same SSH connection. The +Hub link keeps its own identity handshake and peer policy; outer SSH login and +public build metadata are not substitutes for Hub authority. ## Integration boundary diff --git a/docs/terminal-agent-model.md b/docs/terminal-agent-model.md index 2d3ab746..5c963684 100644 --- a/docs/terminal-agent-model.md +++ b/docs/terminal-agent-model.md @@ -29,6 +29,17 @@ unresolved Stop as Running lifecycle with Unknown activity. Semantic prompt submission requires confirmed input readiness; ordinary leased PTY input remains independent. See [provider hook evidence](provider-hooks-research.md). +Hooks are installed per provider with `yolo hook install ` for one or +more of `claude`, `codex`, `qwen`, `gemini` and `grok`. `yolo hook status` +shows each provider's configuration file and state. The command reviews the +target file and managed callbacks, asks for confirmation, preserves existing +user hooks and unrelated settings, and keeps a backup. The installed callback +only runs when the managed Worker exports `YOLO_HOOK_EXECUTABLE` and +`YOLO_HOOK_HOME`, so ordinary shells are unaffected. Codex, Gemini CLI and Grok +Build additionally require the user to trust the hooks in the provider's own +settings; Yolo never bypasses hook trust. A provider whose configuration +disables hooks is reported as `disabled` and is not rewritten. + | Event | Meaning | User-facing action | | --- | --- | --- | | A normally launched agent finishes or quits | That AgentRun ended; the shell and PTY may still be live. | Continue at the shell prompt, run another command, or detach. Do not show the Terminal as exited solely because the agent ended. | @@ -44,10 +55,11 @@ client detach. `Ctrl-B d` detaches from Yolo. An agent started with `exec` repla the shell, so its exit can also end the PTY; the PTY outcome determines the Terminal's state. -The public picker command is `yolo terminals`. `yolo list` lists Terminals, -`yolo new` creates a Terminal with a shell, and `yolo attach [TERMINAL]` selects an -existing Terminal by ID, address or unique name. `yolo clients` and -`yolo detach [CLIENT_ID]` address client views, not AgentRuns or provider history. +The public picker command is `yolo terminals`. `yolo terminal list` lists +Terminals, `yolo new` creates a Terminal with a shell, and `yolo attach +[TERMINAL]` selects an existing Terminal by ID, address or unique name. +`yolo client list` and `yolo client detach [CLIENT_ID]` address client views, +not AgentRuns or provider history. The retired `sessions` picker command is rejected; it is not retained as an alias. The default Terminal and AgentRun lists contain active or unconfirmed resources. @@ -72,8 +84,8 @@ exit observation does not add history entries or change its observation cursor. The current Hub contract uses `TerminalId`, `TerminalAddress`, and `TerminalSummary`. A terminal address contains `hub_id` and `terminal_id`; Hub catalogues expose a `terminals` collection. Its URI is -`yolo:///terminals/`, and `yolo list --json` uses this same -contract. There are no alternate address fields or resource-name aliases. +`yolo:///terminals/`, and `yolo terminal list --json` +uses this same contract. There are no alternate address fields or resource-name aliases. `runtime_epoch` identifies one PTY execution. Provider-owned hook `session_id` fields and provider history directories retain their provider-defined meaning; diff --git a/docs/terminal-presentation.md b/docs/terminal-presentation.md index b8392068..f9296342 100644 --- a/docs/terminal-presentation.md +++ b/docs/terminal-presentation.md @@ -38,7 +38,7 @@ when their values are null or zero. The runtime epoch scopes the counter. Hub terminal catalogues carry this metadata through federation so an unopened terminal can still supply a title and attention indication. -TUI terminal rows, focused terminal status, pane borders, Agent runs and the +TUI terminal rows, focused terminal status, pane borders, Agent rows and the selected Agent's bottom status use the provider-preferred display title. Missing or cleared titles fall back to the PTY title, then the Terminal name or provider name. Agent title lookup requires the diff --git a/docs/tui-interactions.md b/docs/tui-interactions.md index 9783c508..81e2e328 100644 --- a/docs/tui-interactions.md +++ b/docs/tui-interactions.md @@ -4,12 +4,14 @@ Yolo is a client for persistent Hub Terminals. The shell is the primary entry point: agents are ordinary programs started by the user inside its PTY. There is no user-visible workspace collection or workspace directory selector. Terminal, PTY, AgentRun, provider history and client lifetimes are defined in -[the domain model](terminal-agent-model.md). - -The terminal area reserves one Tab strip and one contextual status line. The -selected Tab has a dark background in addition to its `●` marker and bold cyan -label. The Agent runs / Hubs sidebar can remain visible while the terminal has -keyboard focus. See [Tab navigation](tui-tab-navigation.md) for the current interaction model. Healthy +[the domain model](terminal-agent-model.md). A client never binds the terminal +named by its own Worker environment (`YOLO_TERMINAL_ID`): running YOLO inside a +YOLO terminal shows a status hint instead of recursively rendering itself. + +The content column reserves one Tab strip and one contextual status line. Tabs +belong to the active machine; the selected Tab has a dark background in addition +to its `●` marker and bold cyan label. The machine tree sidebar can remain +visible while the terminal has keyboard focus. See [Tab navigation](tui-tab-navigation.md) for the current interaction model. Healthy input readiness appears in the Tab marker and contextual status; read-only, synchronization and offline states are shown only when applicable. Input paused means explicit confirmation is required, while Resuming input means a fresh grant @@ -26,7 +28,7 @@ The existing Ctrl+B prefix scopes manager shortcuts: | Key | Action | | --- | --- | | d / q | Detach the client, preserving all shells and agents | -| s | Focus the Agent runs / Hubs sidebar | +| s | Focus the machine and agent tree | | b | Show or hide the sidebar | | g | Open the existing-terminal picker | | w | Open the tab manager | @@ -47,35 +49,37 @@ See [local entry and lifecycle](tui-local-startup.md) and [split-pane geometry]( ## Inventory Rows -Agent runs are one line: the provider-preferred Agent title, an optional `!` -health warning, the writer badge and a trailing activity glyph. A braille spinner -animates while the Agent is working; `‖` and `!` mark waiting-for-input and -waiting-for-approval states. Directory, lifecycle, health and the full control -description remain available in Agent details. - -Hubs use the machine host name when the selected Hub is reached through a local -Unix endpoint, otherwise the compact Hub identity. The status glyph follows the -name: `●` reachable, `○` unreachable and `?` stale or unknown. There is no second -"Reachable" row. +The sidebar is a tree. Machine roots use the local host name (`This machine`) or +the peer's configured SSH label, with `▾`/`▸`, the number of agent runs and a +reachability glyph (`●` reachable, `○` unreachable, `?` stale or unknown). The +active machine is highlighted. Agent runs are indented one level and keep the +one-line shape: provider-preferred title, optional `!` warning, writer badge and +activity glyph. A braille spinner animates while the Agent is working; `‖` and +`!` mark waiting-for-input and waiting-for-approval states. Directory, lifecycle, +health and the full control description remain available in Agent details. -A healthy connection adds no title row above the sidebar: the section tabs name -the view and the rows carry their own identity and status. A single compact -`Disconnected` or `Refreshing` line appears only when that state needs -attention. In Hubs view, `Configure Hub` shares the section row and `m` opens -the same manager. +A healthy connection adds no title row above the tree; the contextual line names +the active machine and catalogue state. A single compact `Disconnected` or +`Refreshing` line appears only when that state needs attention. `Manage machines` +is a footer action under the tree and `m` opens the same manager. ## Inventory Mouse Navigation -Click an Agent runs or Hubs sidebar tab to change catalogue. Clicking an Agent -runs row selects it and opens its existing terminal; if no terminal is available -it opens the Agent details instead. Clicking a Hubs or Terminals row selects it -for the existing Enter flow. Wheel input over the list moves selection by one -item. Clicking an Agent runs row opens its existing view exactly like Enter: -view activation may acquire write control through the normal terminal path, but -the click itself never injects terminal input. Hit regions use actual rendered -list offsets and item heights. Unpainted trailing rows are not clickable. - -The saved viewport binds view, scope, filter, selection, catalogue freshness, +Clicking a machine root activates its workspace: the client focuses the +machine's last live terminal (or the first live catalogue terminal for it), and +starts a shell on that machine when none exists. Clicking an agent row activates +its machine and opens the agent's terminal; if no live terminal is available it +opens Agent details instead. Clicking a Terminals picker row selects it for the +existing Enter flow. Wheel input over the tree moves selection by one row. +Activation may acquire write control through the normal terminal path, but the +click itself never injects terminal input. Hit regions use actual rendered list +offsets and item heights. Unpainted trailing rows are not clickable. + +`←`/`→` or Space collapse/expand a machine; `Enter` activates the selection; +`n` starts a directory prompt on the selected machine; `/` filters machines and +agents. Tabs, splits and `Ctrl-B n/p` are scoped to the active machine. + +The saved viewport binds view, filter, selection, catalogue freshness, ordered identities and displayed row evidence. A stale viewport consumes the gesture without selecting another record after sorting, filtering or execution replacement. Wheel selection dispatches no application command. @@ -97,9 +101,9 @@ The TUI owner can also be detached from another shell without touching the Terminal or process tree: ```text -yolo clients -yolo detach -yolo detach +yolo client list +yolo client detach +yolo client detach ``` The command authenticates through a private, same-user Unix socket and a diff --git a/docs/tui-local-startup.md b/docs/tui-local-startup.md index f2f800b1..7507340d 100644 --- a/docs/tui-local-startup.md +++ b/docs/tui-local-startup.md @@ -30,21 +30,21 @@ Authenticated remote entry uses the remote home directory by default. `yolo attach [TERMINAL]` only attaches an existing Terminal; a missing or ambiguous selection opens the terminal picker with an explanation and never creates one. -`yolo list [--json] [--all]` prints the Hub terminal catalog; `--all` includes +`yolo terminal list [--json] [--all]` prints the Hub terminal catalog; `--all` includes archived terminals from history and labels them `Archived`. `yolo terminals` -opens the interactive picker. `yolo archive TERMINAL` archives an exited -terminal, and `yolo terminate TERMINAL [--force]` ends a terminal and every +opens the interactive picker. `yolo terminal archive TERMINAL` archives an exited +terminal, and `yolo terminal terminate TERMINAL [--force]` ends a terminal and every program in it; both take an id, `yolo://...` address, `HUB/TERMINAL` address or -unique label, and `--json` prints the resulting summary. `yolo clients +unique label, and `--json` prints the resulting summary. `yolo client list [--json]` lists attached local terminal clients with their kind: the Hub-bound client is `primary` and addressed by the Hub id, every additional client has -its own id. `yolo detach [CLIENT_ID]` disconnects one without stopping its +its own id. `yolo client detach [CLIENT_ID]` disconnects one without stopping its terminals. If more than one client is attached, the command requires an exact client ID. Terminal names are labels: `yolo new --name` does not reserve a unique name, and a selector that matches several terminals opens the picker. Client discovery and detach inspect the existing Hub identity without starting -new services. Ctrl-B d detaches; Ctrl-B s focuses the Agent runs / Hubs sidebar, +new services. Ctrl-B d detaches; Ctrl-B s focuses the machine tree sidebar, and Ctrl-B g opens the existing-terminal picker. New shells open in a dedicated Tab; splits create real PTYs inside the current Tab. diff --git a/docs/tui-hub-management.md b/docs/tui-machine-management.md similarity index 57% rename from docs/tui-hub-management.md rename to docs/tui-machine-management.md index 708f211a..e90e892b 100644 --- a/docs/tui-hub-management.md +++ b/docs/tui-machine-management.md @@ -1,30 +1,30 @@ -# Native TUI Hub Management +# Native TUI Machine Management -The Hubs inventory exposes a `Configure Hub` command. Keyboard entry is `m` -while viewing Hubs. Configuration always belongs to the current entry Hub; -selecting an imported Hub or Agent never changes administrative authority. +The machine tree exposes a `Manage machines` footer action. Keyboard entry is +`m` while the tree has focus. Configuration always belongs to the current entry Hub; +selecting an imported machine or Agent never changes administrative authority. The manager is a full-width terminal view, separate from Agent PTY surfaces. ## Editing -The view displays the entry Hub ID, public key, configuration revision, and actual -listening address, including the assigned port for a dynamic listener. -Connections, trusted origins, file roots, listener settings, and per-peer export policies +The view displays the local Hub ID, public key, configuration revision, and the +local link socket path. +Machines, trusted origins, file roots, and per-machine export policies have separate forms. Keyboard and mouse navigation, bounded paste, UTF-8 text editing, checkboxes, depth controls, scrolling, and wrapped identity fields are -supported. Read-only identity fields cannot be altered while editing a connection. +supported. Read-only identity fields cannot be altered while editing a machine. File root creation, editing and confirmed removal are described in [Native file root management](hub-file-root-management.md). -Adding a connection requires the peer's Hub ID and pinned public key. Dial host -and port are optional together: an inbound-only peer can still export or import -resources on an independently authorized connection. Import direction is an -explicit checkbox, independent of who dials. Reciprocal identity enrollment and -export authorization must be configured at the other Hub as well; the TUI does -not silently enroll an unknown key. +Adding a machine requires its Hub ID and pinned public key. A dial +names an SSH destination and the remote peer socket path; it is optional: an +pinned machine can still export resources on an independently authorized +link. Import direction is an explicit checkbox and requires a dial. The remote machine's +identity and export authorization must be configured on that machine as well; +the TUI does not silently trust an unknown key. Trusted origins support downstream origins such as C in A -> B -> C without -turning C into A's direct peer. Exports select an origin, independent permissions, +turning C into A's direct machine. Exports select an origin, independent permissions, and a bounded reexport depth. New exports start with Observe only. File-read permission is independent of terminal input; granting it does not manufacture an origin capability that has not been implemented. @@ -34,11 +34,21 @@ Each form edits an in-memory draft. Saving a form does not send a Hub mutation. revision. `Activate Configuration` is available when the persisted configuration is inactive. Neither command launches, resumes, or terminates an Agent. -Removing a peer or trusted origin requires confirmation. Dependent exports are -removed only when no remaining peer/trusted-origin pin still authorizes that -origin. An export can be removed without removing its connection. Discarding a +Removing a machine or trusted origin requires confirmation. Dependent exports are +removed only when no remaining machine/trusted-origin pin still authorizes that +origin. An export can be removed without removing its machine. Discarding a dirty draft on close or reload also requires explicit confirmation. +## Consumers + +The `Consumers` page is a read-only live view of the machines currently +importing from this Hub. Each row shows the importing Hub ID, its live link +count and connection age; `Refresh Consumers` issues a new `ListConsumers` +query. An empty page means no inbound link is open right now. The page never +grants or revokes access: admission and export policy remain the authority of +the configuration pages. Refreshing or leaving the page preserves an unsaved +draft, and closing the manager does not close inbound links. + ## Concurrency And Failure `hub/management/client` owns a separate public connection and one absolute diff --git a/docs/tui-tab-navigation.md b/docs/tui-tab-navigation.md index 08d401bc..e1c975e4 100644 --- a/docs/tui-tab-navigation.md +++ b/docs/tui-tab-navigation.md @@ -18,7 +18,7 @@ state retains selection, focus and layout checkpoints. Layout records never gran | Split down | `-` or `"` | Add and focus a lower pane with a new shell | | Previous / next Tab | `p` / `n` | Restore that Tab's layout and focused pane | | Focus pane | arrows or `o` | Move within the current split tree | -| Focus sidebar | `s` | Navigate Agent runs and Hubs | +| Focus sidebar | `s` | Navigate the machine tree | | Toggle sidebar | `b` | Show or hide it independently of terminal focus | | Existing terminal picker | `g` | Find retained PTYs, including shells without an agent | | Manage Tabs | `w` | Select, name, create, or remove a Tab view | @@ -31,15 +31,19 @@ The Tab strip always shows the catalog's Tab name, including explicit renames. The focused program's dynamic terminal title remains in its pane and status line; it never hides the Tab's navigation identity. -In the sidebar, Tab switches between Agent runs and Hubs; Enter opens the selected -agent or scopes to a Hub. `I` shows agent details and `i` interrupts a supported -agent using its existing exact authority. `c` opens a new Tab, `n` prompts for -its shell directory, and `/` filters the current view. +The sidebar is a machine tree: machine roots own their Tabs, agent runs are +indented children. `Enter` activates the selected machine or agent (opening the +machine's live terminal, its last live terminal, or a new shell on that machine); +`←`/`→` or Space collapse and expand a machine. `I` shows agent details and `i` +interrupts a supported agent using its existing exact authority. `n` prompts for +a shell directory on the selected machine, and `/` filters the tree. Tabs, +splits, `Ctrl-B n/p` and the last-Tab rule are scoped to the active machine. The sidebar starts collapsed so a new shell uses the full terminal width. -`Ctrl-B s` explicitly opens it and `Ctrl-B b` toggles it; agent arrival or exit -does not change that choice. An empty layout uses the full inventory view instead -of reserving a blank terminal area. The sidebar has no Terminals tab. +`Ctrl-B s` explicitly focuses it and `Ctrl-B b` toggles visibility; agent +arrival or exit does not change that choice. When the sidebar is visible the +content column keeps the Tab strip above the panes; hiding it lets the panes +span the width. The sidebar has no Terminals tab. Agent rows use three lines: title, activity and directory. Stale/unreachable evidence remains visible; UUIDs, hook provenance and resume metadata are available in details. The sidebar uses a bounded width; diff --git a/docs/upgrade-admission.md b/docs/upgrade-admission.md index 29a3ce84..7a59f18b 100644 --- a/docs/upgrade-admission.md +++ b/docs/upgrade-admission.md @@ -4,10 +4,11 @@ Yolo images are immutable deployments. The CLI symlink is only a selector for new launches; it is not a migration mechanism for an already-running Host or Hub. Before a new image opens a Hub state directory, the Hub validates -`runtime-contract.json`. The record binds the current public Hub protocol and -state schema. A missing record is treated as an old or unproven state, and a -mismatched record fails with an upgrade admission error. The new process does not -rewrite identity, catalog, runtime records or creation journals in that case. +`runtime-contract.json`. The record binds the public Hub protocol and state +schema. An older Hub protocol is an upgrade and is rewritten by the new Hub; a +newer protocol, a different contract schema or a different state schema is +rejected, as is a missing record for an existing identity. The new process does +not rewrite identity, catalog, runtime records or creation journals. The operational upgrade order is: @@ -15,7 +16,10 @@ The operational upgrade order is: 2. Probe the running Host/Hub image, protocol and state contract. 3. Keep the persistent custody domain and live Workers running while the result is compatible. -4. Restart only the independently replaceable Host runner and Hub. +4. Restart only the independently replaceable Host runner and Hub. Detached + profiles use `yolo server restart`, while systemd-owned profiles restart the + selected Host runner and Hub units. Both paths preserve the custody domain + and live Workers. 5. Recheck Hub identity, TerminalAddress, RuntimeEpoch, Worker identity and Agent execution identity before accepting the deployment. @@ -29,13 +33,25 @@ still use the immutable `/home//.local/lib/yolo//yolo` path so each service has an explicit image owner. The link under `.local/bin` remains for interactive CLI selection only. -If the probe reports incompatibility, `yolo update` stops before any service -mutation and prints a structured refusal containing the old image, state path, -protocol and failed service-unit checks. The current release has no public -force/apply subcommands: stopping the old Hub and runner is an operator action -after this refusal, and must leave `yolo-host.service` and its PTY custody domain -alone. The admission step never retries an uncertain state mutation or deletes -old state. +If the probe reports incompatibility, `yolo update` exits nonzero with +`upgrade admission refused` plus the failed checks. When the installed image is newer than the running services, activate a detached +profile with `yolo server restart`. A systemd-owned profile must restart its +selected Host runner and Hub units. `yolo server stop` leaves a detached profile +stopped for inspection. +Restarting preserves PTY custody: the Host closes ingress but Workers remain +attached to their PTYs. A restart is refused before stopping anything when +`runtime-contract.json` is incompatible with the installed image, or when the +caller cannot create a new Host (for example an un-delegated Linux shell). There +is no force/apply subcommand: admission never retries an uncertain state mutation +or deletes old state. + +`yolo update` checks the selected release, installs it when newer, activates +through the profile owner when possible, and then reports admission. `--check` is read-only, `--admit` +probes the installed image without the network, and `--no-restart` installs +without activating. The probe always describes the image that will be left +running. Before stopping services, `yolo update` asks the installed image to +validate `runtime-contract.json`; the invoking updater's protocol constants +never decide the upgrade. A refused preflight leaves services untouched. On systemd hosts, each managed unit must pin both the absolute immutable image path and its exact image metadata: diff --git a/integration_tests/runtime/resilience/agent_recovery.rs b/integration_tests/runtime/resilience/agent_recovery.rs index af87cd03..bb14a1c5 100644 --- a/integration_tests/runtime/resilience/agent_recovery.rs +++ b/integration_tests/runtime/resilience/agent_recovery.rs @@ -2,7 +2,7 @@ use super::*; use std::os::unix::fs::PermissionsExt; use tempfile::tempdir; use yolo_runtime::agent::constants::{COMMAND_LINE_ENDING, PATH_ENV, SHELL_ENV}; -use yolo_runtime::agent::{AgentState, ProcessSource, SystemProcessSource}; +use yolo_runtime::agent::{AgentState, ProcessSource, RuntimeProcessSource}; use yolo_runtime::{RecoveryState, TerminalView}; const AGENT_BIN_DIR: &str = "agent-bin"; diff --git a/integration_tests/runtime/resilience/agent_recovery/assertions.rs b/integration_tests/runtime/resilience/agent_recovery/assertions.rs index ef779533..9f324100 100644 --- a/integration_tests/runtime/resilience/agent_recovery/assertions.rs +++ b/integration_tests/runtime/resilience/agent_recovery/assertions.rs @@ -135,7 +135,7 @@ fn assert_foreground_agent_descendant( let runtime = view.runtime.as_ref().expect("detected shell runtime"); let binding = view.agent.as_ref().expect("detected agent binding"); let pid = binding.process_pid.expect("detected agent process id"); - let processes = SystemProcessSource.descendants(runtime.child_pid).unwrap(); + let processes = RuntimeProcessSource::new([]).descendants(runtime.child_pid).unwrap(); let process = processes .into_iter() .find(|process| process.pid == pid) diff --git a/integration_tests/runtime/resilience/agent_recovery/matrix.rs b/integration_tests/runtime/resilience/agent_recovery/matrix.rs index 373a4ab2..2775226f 100644 --- a/integration_tests/runtime/resilience/agent_recovery/matrix.rs +++ b/integration_tests/runtime/resilience/agent_recovery/matrix.rs @@ -1,11 +1,9 @@ use super::*; - #[path = "assertions.rs"] mod assertions; use assertions::*; const CREATE_SESSION_FAILURE_DETAIL: &str = "agent matrix shell creation failed"; - #[path = "explicit_fail_closed.rs"] mod explicit_fail_closed; @@ -125,6 +123,8 @@ async fn create_detected_agent( cwd: harness.home.path().to_path_buf(), cols: None, rows: None, + initial_input: None, + context: None, }) .await .unwrap_or_else(|error| { diff --git a/integration_tests/runtime/resilience/agent_recovery/native_hook_registration_race.rs b/integration_tests/runtime/resilience/agent_recovery/native_hook_registration_race.rs index b1c00427..0c5f1e4a 100644 --- a/integration_tests/runtime/resilience/agent_recovery/native_hook_registration_race.rs +++ b/integration_tests/runtime/resilience/agent_recovery/native_hook_registration_race.rs @@ -77,7 +77,7 @@ fn pause_first_hook(home: &Path, release: &Path) { libc::c_int::default() ); let preview = - yolo_hook_installation::inspect(&yolo_hook_installation_catalog::CODEX, home).unwrap(); + yolo_hook_installation::inspect(crate::support::codex_install_spec(), home).unwrap(); let mut document: serde_json::Value = serde_json::from_slice(&std::fs::read(&preview.path).unwrap()).unwrap(); let groups = document[PACKAGED_SETTINGS_HOOKS_FIELD][CODEX_SESSION_START_EVENT] diff --git a/integration_tests/runtime/resilience/agent_recovery/support.rs b/integration_tests/runtime/resilience/agent_recovery/support.rs index e0f82401..30406c7b 100644 --- a/integration_tests/runtime/resilience/agent_recovery/support.rs +++ b/integration_tests/runtime/resilience/agent_recovery/support.rs @@ -25,9 +25,9 @@ pub(crate) fn spawn_agent_host(home: &Path, provider: ProviderFixture, bin: &Pat fn install_provider_hooks(home: &Path, provider: ProviderFixture) { let spec = if provider.kind == yolo_runtime::agent::codex::constants::KIND { - &yolo_hook_installation_catalog::CODEX + crate::support::codex_install_spec() } else { - &yolo_hook_installation_catalog::CLAUDE + crate::support::claude_install_spec() }; let home = home.join(provider.data_home_directory); let preview = yolo_hook_installation::inspect(spec, &home).unwrap(); diff --git a/integration_tests/runtime/resilience/agent_recovery/view_wait.rs b/integration_tests/runtime/resilience/agent_recovery/view_wait.rs index 5b73f061..20d04284 100644 --- a/integration_tests/runtime/resilience/agent_recovery/view_wait.rs +++ b/integration_tests/runtime/resilience/agent_recovery/view_wait.rs @@ -36,7 +36,7 @@ async fn report_timeout(client: &Client, id: Uuid, harness: &Harness, view: Term let processes = view .runtime .as_ref() - .and_then(|runtime| SystemProcessSource.descendants(runtime.child_pid).ok()); + .and_then(|runtime| RuntimeProcessSource::new([]).descendants(runtime.child_pid).ok()); let output = client.attach(id, 0).await.ok().map(|attachment| { attachment .initial diff --git a/integration_tests/runtime/resilience/creation_capability.rs b/integration_tests/runtime/resilience/creation_capability.rs index 92a2515b..beeaf44b 100644 --- a/integration_tests/runtime/resilience/creation_capability.rs +++ b/integration_tests/runtime/resilience/creation_capability.rs @@ -9,6 +9,8 @@ fn creation_request(token: Uuid, name: &str, cwd: PathBuf) -> CreateTerminal { cwd, cols: Some(90), rows: Some(25), + initial_input: None, + context: None, } } diff --git a/integration_tests/runtime/resilience/hub/agent/agent_directory.rs b/integration_tests/runtime/resilience/hub/agent/agent_directory.rs index 44380fd9..c90520d1 100644 --- a/integration_tests/runtime/resilience/hub/agent/agent_directory.rs +++ b/integration_tests/runtime/resilience/hub/agent/agent_directory.rs @@ -30,37 +30,27 @@ async fn real_hook_working_directory_changes_keep_agent_identity_across_relay_an b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_addr, b_addr, c_addr) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); let a_config = Configuration { - listen: Some(a_addr), trusted_origins: vec![c], - peers: vec![peer(b, Some(b_addr), true, vec![])], + peers: vec![peer(b, Some(&b_state), true, vec![])], file_roots: vec![], }; let b_config = Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ peer(a, None, false, vec![exports(c.hub(), false)]), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let c_config = Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), false, vec![exports(c.hub(), true)])], + peers: vec![peer(b, Some(&b_state), false, vec![exports(c.hub(), true)])], }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let b_node = RunningNode::start(&b_host, b_state, b_config, b_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let b_node = RunningNode::start(&b_host, b_state, b_config).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; drop((a_hold, b_hold, c_hold)); let (binary, first_directory, next_directory) = launch::prepare(&c_host); let target = launch::launch(&c_node, &c_host, &binary, &first_directory).await; diff --git a/integration_tests/runtime/resilience/hub/agent/agent_directory_launch.rs b/integration_tests/runtime/resilience/hub/agent/agent_directory_launch.rs index 7c05160d..8595cccd 100644 --- a/integration_tests/runtime/resilience/hub/agent/agent_directory_launch.rs +++ b/integration_tests/runtime/resilience/hub/agent/agent_directory_launch.rs @@ -28,7 +28,7 @@ pub fn prepare(harness: &Harness) -> (PathBuf, PathBuf, PathBuf) { .unwrap(); std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(EXECUTABLE_MODE)).unwrap(); let data = home.join(CLAUDE_DATA_DIRECTORY); - let provider = &yolo_hook_installation_catalog::CLAUDE; + let provider = crate::support::claude_install_spec(); let preview = yolo_hook_installation::inspect(provider, &data).unwrap(); yolo_hook_installation::install(provider, &data, &preview.revision).unwrap(); let first = home.join(FIRST_DIRECTORY); diff --git a/integration_tests/runtime/resilience/hub/agent_control.rs b/integration_tests/runtime/resilience/hub/agent_control.rs index 94acea8b..13940698 100644 --- a/integration_tests/runtime/resilience/hub/agent_control.rs +++ b/integration_tests/runtime/resilience/hub/agent_control.rs @@ -49,37 +49,27 @@ async fn exercise(mode: Delegation) { b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_addr, b_addr, c_addr) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); let a_config = Configuration { - listen: Some(a_addr), trusted_origins: vec![c], - peers: vec![peer(b, Some(b_addr), true, vec![])], + peers: vec![peer(b, Some(&b_state), true, vec![])], file_roots: vec![], }; let b_config = Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ peer(a, None, false, vec![mode.to_entry(c.hub())]), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let c_config = Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), false, vec![mode.to_relay(c.hub())])], + peers: vec![peer(b, Some(&b_state), false, vec![mode.to_relay(c.hub())])], }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let b_node = RunningNode::start(&b_host, b_state, b_config, b_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let b_node = RunningNode::start(&b_host, b_state, b_config).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; drop((a_hold, b_hold, c_hold)); let target = launch(&c_node, &c_host, AGENT_NAME).await; for node in [&a_node, &b_node, &c_node] { diff --git a/integration_tests/runtime/resilience/hub/connector/connector_federation_start.rs b/integration_tests/runtime/resilience/hub/connector/connector_federation_start.rs index 8471a032..b3fb4d20 100644 --- a/integration_tests/runtime/resilience/hub/connector/connector_federation_start.rs +++ b/integration_tests/runtime/resilience/hub/connector/connector_federation_start.rs @@ -1,7 +1,7 @@ use super::RunningNode; use crate::hub::catalog_gate::{CatalogGate, GatedDispatcher}; use std::sync::Arc; -use tokio::{net::TcpListener, sync::watch}; +use tokio::sync::watch; use yolo_hub_federation::Federation; use yolo_hub_federation_settings::Configuration; use yolo_hub_local_backend::LocalBackend; @@ -12,13 +12,19 @@ impl RunningNode { pub async fn start_prepared( state: Arc, config: Configuration, - listener: TcpListener, backend: LocalBackend, ) -> Self { let socket = state.socket_path(); - let node = Federation::new(state.clone(), backend, config) - .await - .unwrap(); + let (listener, publication) = super::publish(&state).await; + let node = Federation::new_with_dialer( + state.clone(), + backend, + config, + Arc::new(super::DirectUnixDialer), + None, + ) + .await + .unwrap(); let catalog_gate = CatalogGate::new(); let server = HubServer::bind_dispatcher( state, @@ -39,6 +45,7 @@ impl RunningNode { stop, local: Some(local), network: Some(network), + _publication: publication, } } } diff --git a/integration_tests/runtime/resilience/hub/connector/connector_graph.rs b/integration_tests/runtime/resilience/hub/connector/connector_graph.rs index 9eb47e21..2fb22b36 100644 --- a/integration_tests/runtime/resilience/hub/connector/connector_graph.rs +++ b/integration_tests/runtime/resilience/hub/connector/connector_graph.rs @@ -19,21 +19,12 @@ impl Graph { b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_addr, b_addr, c_addr) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); let a_config = Configuration { - listen: Some(a_addr), trusted_origins: vec![c], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), true, vec![])], + peers: vec![peer(b, Some(&b_state), true, vec![])], }; let b_config = Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ @@ -43,18 +34,17 @@ impl Graph { false, vec![exports(b.hub(), false), exports(c.hub(), false)], ), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let c_config = Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), false, vec![exports(c.hub(), true)])], + peers: vec![peer(b, Some(&b_state), false, vec![exports(c.hub(), true)])], }; - let a = RunningNode::start(a_host, a_state, a_config, a_listener).await; - let b = RunningNode::start(b_host, b_state, b_config, b_listener).await; - let c = ConnectorNode::start(c_host, c_state, c_config, c_listener).await; + let a = RunningNode::start(a_host, a_state, a_config).await; + let b = RunningNode::start(b_host, b_state, b_config).await; + let c = ConnectorNode::start(c_host, c_state, c_config).await; Self { a, b, c } } pub async fn stop(self) { @@ -66,12 +56,10 @@ impl Graph { pub async fn standalone(harness: &Harness, path: &Path) -> ConnectorNode { let state = Arc::new(HubState::open(path, harness.home.path()).unwrap()); - let listener = listener().await; let config = Configuration { - listen: Some(listener.local_addr().unwrap()), trusted_origins: vec![], file_roots: vec![], peers: vec![], }; - ConnectorNode::start(harness, state, config, listener).await + ConnectorNode::start(harness, state, config).await } diff --git a/integration_tests/runtime/resilience/hub/connector/connector_node.rs b/integration_tests/runtime/resilience/hub/connector/connector_node.rs index 3d12d967..8075807b 100644 --- a/integration_tests/runtime/resilience/hub/connector/connector_node.rs +++ b/integration_tests/runtime/resilience/hub/connector/connector_node.rs @@ -4,7 +4,7 @@ use super::super::{ }; use crate::Harness; use std::{path::PathBuf, sync::Arc}; -use tokio::{net::TcpListener, sync::watch, task::JoinHandle}; +use tokio::{sync::watch, task::JoinHandle}; use yolo_hub_connector_service::ConnectorServer; use yolo_hub_connector_store::CONNECTOR_DIRECTORY; use yolo_hub_delegation::PeerIdentity; @@ -23,7 +23,6 @@ impl ConnectorNode { harness: &Harness, state: Arc, config: Configuration, - listener: TcpListener, ) -> Self { let directory = state.directory().unwrap(); let connectors = directory @@ -38,7 +37,7 @@ impl ConnectorNode { .await .unwrap(); let socket = server.socket_path(); - let hub = RunningNode::start_prepared(state, config, listener, backend).await; + let hub = RunningNode::start_prepared(state, config, backend).await; let (stop, mut stopped) = watch::channel(false); let task = tokio::spawn(server.serve_until(async move { let _ = stopped.changed().await; diff --git a/integration_tests/runtime/resilience/hub/conversation/mod.rs b/integration_tests/runtime/resilience/hub/conversation/mod.rs index aa663f03..48b81e44 100644 --- a/integration_tests/runtime/resilience/hub/conversation/mod.rs +++ b/integration_tests/runtime/resilience/hub/conversation/mod.rs @@ -33,34 +33,29 @@ async fn real_three_hub_conversation_reads_preserve_origin_and_revocation_keeps_ c_state.peer_identity(), ); let mut b_config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), DYNAMIC_PORT)), peers: vec![ peer(a, None, false, vec![support::export(c.hub(), false)]), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], ..Configuration::default() }; let b_node = ManagedNode::start(&b_host, b_state, b_config.clone()).await; - let a_listener = listener().await; - let c_listener = listener().await; let a_config = Configuration { - listen: Some(a_listener.local_addr().unwrap()), - peers: vec![peer(b, Some(b_node.address), true, vec![])], + peers: vec![peer_at_socket(b, b_node.peer_socket.clone(), true, vec![])], trusted_origins: vec![c], file_roots: vec![], }; let c_config = Configuration { - listen: Some(c_listener.local_addr().unwrap()), - peers: vec![peer( + peers: vec![peer_at_socket( b, - Some(b_node.address), + b_node.peer_socket.clone(), false, vec![support::export(c.hub(), true)], )], ..Configuration::default() }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; let target = launch(&c_node, &c_host, NAME_C).await; a_node.wait_origins(&[c.hub()]).await; let before = c_host diff --git a/integration_tests/runtime/resilience/hub/federation/federation_flow.rs b/integration_tests/runtime/resilience/hub/federation/federation_flow.rs index bdb5c950..ba16534b 100644 --- a/integration_tests/runtime/resilience/hub/federation/federation_flow.rs +++ b/integration_tests/runtime/resilience/hub/federation/federation_flow.rs @@ -28,21 +28,12 @@ async fn real_three_hubs_relay_reverse_connection_and_tui_keep_origin_custody() b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_addr, b_addr, c_addr) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); let a_config = Configuration { - listen: Some(a_addr), trusted_origins: vec![c], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), true, vec![])], + peers: vec![peer(b, Some(&b_state), true, vec![])], }; let b_config = Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ @@ -52,18 +43,17 @@ async fn real_three_hubs_relay_reverse_connection_and_tui_keep_origin_custody() false, vec![exports(b.hub(), false), exports(c.hub(), false)], ), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let c_config = Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), false, vec![exports(c.hub(), true)])], + peers: vec![peer(b, Some(&b_state), false, vec![exports(c.hub(), true)])], }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let b_node = RunningNode::start(&b_host, b_state, b_config.clone(), b_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let b_node = RunningNode::start(&b_host, b_state, b_config.clone()).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; launch(&a_node, &a_host, NAME_A).await; launch(&b_node, &b_host, NAME_B).await; let created = launch_queryable(&c_node, &c_host, NAME_C).await; @@ -172,10 +162,9 @@ async fn real_three_hubs_relay_reverse_connection_and_tui_keep_origin_custody() surviving.agent.as_ref().unwrap().process_start_time, before.agent.as_ref().unwrap().process_start_time ); - let listener = tokio::net::TcpListener::bind(b_addr).await.unwrap(); let b_state = Arc::new(HubState::open(&b_path, b_host.home.path()).unwrap()); assert_eq!(b_state.peer_identity(), b); - let b_node = RunningNode::start(&b_host, b_state, b_config, listener).await; + let b_node = RunningNode::start(&b_host, b_state, b_config).await; assert_eq!(a_node.wait_origins(&[a.hub(), b.hub(), c.hub()]).await, all); creation_query::verify(&[&a_node], &created).await; let replay = Attachment::open( diff --git a/integration_tests/runtime/resilience/hub/federation/federation_observe_only.rs b/integration_tests/runtime/resilience/hub/federation/federation_observe_only.rs index 2ff17652..f9323229 100644 --- a/integration_tests/runtime/resilience/hub/federation/federation_observe_only.rs +++ b/integration_tests/runtime/resilience/hub/federation/federation_observe_only.rs @@ -30,21 +30,12 @@ async fn real_three_hubs_attenuate_origin_observe_only_grant_without_granting_co b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_addr, b_addr, c_addr) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); let a_config = Configuration { - listen: Some(a_addr), trusted_origins: vec![c], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), true, vec![])], + peers: vec![peer(b, Some(&b_state), true, vec![])], }; let b_config = Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ @@ -54,20 +45,19 @@ async fn real_three_hubs_attenuate_origin_observe_only_grant_without_granting_co false, vec![exports(b.hub(), false), exports(c.hub(), false)], ), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let mut origin_policy = exports(c.hub(), true); origin_policy.rights = [Right::Observe].into_iter().collect(); let c_config = Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), false, vec![origin_policy])], + peers: vec![peer(b, Some(&b_state), false, vec![origin_policy])], }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let b_node = RunningNode::start(&b_host, b_state, b_config, b_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let b_node = RunningNode::start(&b_host, b_state, b_config).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; let created = launch_queryable(&c_node, &c_host, AGENT_NAME).await; let from_a = a_node.wait_origins(&[c.hub()]).await; let from_b = b_node.wait_origins(&[c.hub()]).await; diff --git a/integration_tests/runtime/resilience/hub/federation/federation_support.rs b/integration_tests/runtime/resilience/hub/federation/federation_support.rs index 11bc3dce..91002b16 100644 --- a/integration_tests/runtime/resilience/hub/federation/federation_support.rs +++ b/integration_tests/runtime/resilience/hub/federation/federation_support.rs @@ -1,28 +1,11 @@ -use super::catalog_gate::CatalogGate; #[path = "../connector/connector_federation_start.rs"] mod prepared_start; -use crate::Harness; -use std::{ - collections::BTreeSet, - net::{IpAddr, Ipv4Addr, SocketAddr}, - num::NonZeroU16, - path::PathBuf, - sync::Arc, - time::Duration, -}; -use tokio::{ - net::TcpListener, - sync::watch, - task::JoinHandle, - time::{Instant, timeout_at}, -}; -use yolo_hub_agent_protocol::AgentSnapshot; +use std::{path::PathBuf, time::Duration}; +use tokio::time::Instant; use yolo_hub_delegation::PeerIdentity; -use yolo_hub_federation::Federation; -use yolo_hub_federation_settings::{Configuration, ExportPolicy, Peer, PeerAddress}; -use yolo_hub_identity::{AgentAddress, HubId}; +use yolo_hub_federation_settings::{ExportPolicy, Peer, PeerAddress}; +use yolo_hub_identity::HubId; use yolo_hub_routing::Right; -use yolo_hub_rpc_protocol::{Command, Reply}; use yolo_hub_state::HubState; pub const GRAPH_TIMEOUT: Duration = Duration::from_secs(180); @@ -31,20 +14,14 @@ pub const TEMP_ROOT: &str = "/tmp"; pub const NAME_A: &str = "node-a-agent"; pub const NAME_B: &str = "node-b-agent"; pub const NAME_C: &str = "node-c-agent"; -const DYNAMIC_PORT: u16 = 0; const REEXPORT_ALLOWED: u8 = 1; const LEAF_EXPORT: u8 = 0; +const TEST_PROFILE: &str = "in-process"; +const PROBE_TIMEOUT: Duration = Duration::from_secs(1); +const LIVE_SOCKET: &str = "another test Hub owns this peer socket"; pub fn deadline() -> Instant { Instant::now() + GRAPH_TIMEOUT } -pub async fn listener() -> TcpListener { - TcpListener::bind(SocketAddr::new( - IpAddr::V4(Ipv4Addr::LOCALHOST), - DYNAMIC_PORT, - )) - .await - .unwrap() -} pub fn exports(origin: HubId, relay: bool) -> ExportPolicy { ExportPolicy { origin, @@ -61,108 +38,48 @@ pub fn exports(origin: HubId, relay: bool) -> ExportPolicy { } pub fn peer( identity: PeerIdentity, - dial: Option, + dial: Option<&HubState>, consume: bool, exports: Vec, ) -> Peer { - Peer { + peer_at( identity, - dial: dial.map(|address| { - PeerAddress::tcp( - address.ip().to_string(), - NonZeroU16::new(address.port()).unwrap(), - ) + dial.map(|state| { + PeerAddress::ssh(TEST_PROFILE, state.peer_socket().unwrap()).unwrap() }), consume, exports, - } -} -pub struct RunningNode { - pub node: Arc, - pub catalog_gate: Arc, - pub socket: PathBuf, - stop: watch::Sender, - local: Option>>, - network: Option>>, + ) } -impl RunningNode { - pub async fn start( - harness: &Harness, - state: Arc, - config: Configuration, - listener: TcpListener, - ) -> Self { - let backend = super::files_runtime::prepared(harness, &state, &config).await; - Self::start_prepared(state, config, listener, backend).await - } - pub async fn client(&self) -> yolo_hub_client::LocalClient { - yolo_hub_client::Client::connect_local(&self.socket, self.node.hub_id(), deadline()) - .await - .unwrap() - } - pub async fn agents(&self) -> Vec { - let mut client = self.client().await; - let Reply::Agents { agents, next: None } = client - .call( - self.node.hub_id(), - Command::ListAgents { after: None }, - deadline(), - ) - .await - .unwrap() - else { - panic!() - }; - agents - } - pub async fn wait_origins(&self, expected: &[HubId]) -> BTreeSet { - let expected: BTreeSet<_> = expected.iter().copied().collect(); - timeout_at(deadline(), async { - loop { - let agents = self.agents().await; - let origins = agents - .iter() - .map(|agent| agent.cursor().target().address.hub_id) - .collect::>(); - let current = agents.iter().all(|agent| { - agent.fields().freshness == yolo_hub_agent_protocol::Freshness::Current - && agent.fields().reachability - == yolo_hub_agent_protocol::Reachability::Reachable - }); - if origins == expected && current && agents.len() == expected.len() { - return agents - .iter() - .map(|agent| agent.cursor().target().address) - .collect(); - } - tokio::time::sleep(POLL).await; - } - }) - .await - .unwrap() - } - pub async fn stop(mut self) { - self.stop.send_replace(true); - timeout_at(deadline(), self.local.take().unwrap()) - .await - .unwrap() - .unwrap() - .unwrap(); - timeout_at(deadline(), self.network.take().unwrap()) - .await - .unwrap() - .unwrap() - .unwrap(); - } + +pub fn peer_at_socket( + identity: PeerIdentity, + socket: PathBuf, + consume: bool, + exports: Vec, +) -> Peer { + peer_at( + identity, + Some(PeerAddress::ssh(TEST_PROFILE, socket).unwrap()), + consume, + exports, + ) } -impl Drop for RunningNode { - fn drop(&mut self) { - self.stop.send_replace(true); - if let Some(task) = self.local.take() { - task.abort(); - } - if let Some(task) = self.network.take() { - task.abort(); - } + +pub fn peer_at( + identity: PeerIdentity, + dial: Option, + consume: bool, + exports: Vec, +) -> Peer { + Peer { + identity, + dial, + consume, + exports, } } + +#[path = "federation_support_running.rs"] +mod running; +pub use running::{DirectUnixDialer, RunningNode, publish}; diff --git a/integration_tests/runtime/resilience/hub/federation/federation_support_running.rs b/integration_tests/runtime/resilience/hub/federation/federation_support_running.rs new file mode 100644 index 00000000..50f9e719 --- /dev/null +++ b/integration_tests/runtime/resilience/hub/federation/federation_support_running.rs @@ -0,0 +1,135 @@ +use super::{LIVE_SOCKET, POLL, PROBE_TIMEOUT, deadline}; +use crate::{Harness, hub::catalog_gate::CatalogGate}; +use std::{collections::BTreeSet, path::PathBuf, sync::Arc}; +use tokio::{ + sync::watch, + task::JoinHandle, + time::{Instant, timeout_at}, +}; +use yolo_hub_agent_protocol::AgentSnapshot; +use yolo_hub_delegation::PeerIdentity; +use yolo_hub_federation::Federation; +use yolo_hub_federation_settings::{Configuration, PeerAddress}; +use yolo_hub_identity::{AgentAddress, HubId}; +use yolo_hub_peer_dialer::{PeerDialFuture, PeerDialer, PeerIo}; +use yolo_hub_rpc_protocol::{Command, Reply}; +use yolo_hub_state::HubState; +use yolo_paths::socket_activation::{self, SocketPublication}; + +/// In-process tests dial the peer socket directly; production uses the broker. +pub struct DirectUnixDialer; + +impl PeerDialer for DirectUnixDialer { + fn dial<'a>( + &'a self, + _: PeerIdentity, + address: &'a PeerAddress, + deadline: Instant, + ) -> PeerDialFuture<'a> { + Box::pin(async move { + let stream = timeout_at( + deadline, + tokio::net::UnixStream::connect(&address.socket), + ) + .await??; + Ok(PeerIo::new(stream, || {})) + }) + } +} + +pub struct RunningNode { + pub node: Arc, + pub catalog_gate: Arc, + pub socket: PathBuf, + pub(super) stop: watch::Sender, + pub(super) local: Option>>, + pub(super) network: Option>>, + pub(super) _publication: SocketPublication, +} +impl RunningNode { + pub async fn start(harness: &Harness, state: Arc, config: Configuration) -> Self { + let backend = crate::hub::files_runtime::prepared(harness, &state, &config).await; + Self::start_prepared(state, config, backend).await + } + pub async fn client(&self) -> yolo_hub_client::LocalClient { + yolo_hub_client::Client::connect_local(&self.socket, self.node.hub_id(), deadline()) + .await + .unwrap() + } + pub async fn agents(&self) -> Vec { + let mut client = self.client().await; + let Reply::Agents { agents, next: None } = client + .call( + self.node.hub_id(), + Command::ListAgents { after: None }, + deadline(), + ) + .await + .unwrap() + else { + panic!() + }; + agents + } + pub async fn wait_origins(&self, expected: &[HubId]) -> BTreeSet { + let expected: BTreeSet<_> = expected.iter().copied().collect(); + timeout_at(deadline(), async { + loop { + let agents = self.agents().await; + let origins = agents + .iter() + .map(|agent| agent.cursor().target().address.hub_id) + .collect::>(); + let current = agents.iter().all(|agent| { + agent.fields().freshness == yolo_hub_agent_protocol::Freshness::Current + && agent.fields().reachability + == yolo_hub_agent_protocol::Reachability::Reachable + }); + if origins == expected && current && agents.len() == expected.len() { + return agents + .iter() + .map(|agent| agent.cursor().target().address) + .collect(); + } + tokio::time::sleep(POLL).await; + } + }) + .await + .unwrap() + } + pub async fn stop(mut self) { + self.stop.send_replace(true); + timeout_at(deadline(), self.local.take().unwrap()) + .await + .unwrap() + .unwrap() + .unwrap(); + timeout_at(deadline(), self.network.take().unwrap()) + .await + .unwrap() + .unwrap() + .unwrap(); + } +} +impl Drop for RunningNode { + fn drop(&mut self) { + self.stop.send_replace(true); + if let Some(task) = self.local.take() { + task.abort(); + } + if let Some(task) = self.network.take() { + task.abort(); + } + } +} + +pub async fn publish(state: &HubState) -> (tokio::net::UnixListener, SocketPublication) { + socket_activation::publish( + state.directory().unwrap(), + &state.peer_socket().unwrap(), + PROBE_TIMEOUT, + LIVE_SOCKET, + ) + .await + .unwrap() +} diff --git a/integration_tests/runtime/resilience/hub/files/files_flow.rs b/integration_tests/runtime/resilience/hub/files/files_flow.rs index 7dd34207..15820a0f 100644 --- a/integration_tests/runtime/resilience/hub/files/files_flow.rs +++ b/integration_tests/runtime/resilience/hub/files/files_flow.rs @@ -33,7 +33,6 @@ async fn real_multihop_files_require_independent_rights_and_preserve_origin_pty_ let b_root = configure_root(&path, B_FILES, shared, B_CONTENT); let c_root = configure_root(&path, C_FILES, shared, C_CONTENT); let mut b_config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 0)), peers: vec![ peer( a, @@ -41,34 +40,29 @@ async fn real_multihop_files_require_independent_rights_and_preserve_origin_pty_ false, vec![file_export(b.hub(), false), exports(c.hub(), false)], ), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], file_roots: vec![b_root], ..Configuration::default() }; let b_node = ManagedNode::start(&b_host, b_state, b_config.clone()).await; - let a_listener = listener().await; - let c_listener = listener().await; - let c_address = c_listener.local_addr().unwrap(); let a_config = Configuration { - listen: Some(a_listener.local_addr().unwrap()), - peers: vec![peer(b, Some(b_node.address), true, vec![])], + peers: vec![peer_at_socket(b, b_node.peer_socket.clone(), true, vec![])], trusted_origins: vec![c], file_roots: vec![a_root], }; let c_config = Configuration { - listen: Some(c_address), - peers: vec![peer( + peers: vec![peer_at_socket( b, - Some(b_node.address), + b_node.peer_socket.clone(), false, vec![file_export(c.hub(), true)], )], file_roots: vec![c_root], ..Configuration::default() }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let c_node = RunningNode::start(&c_host, c_state.clone(), c_config.clone(), c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let c_node = RunningNode::start(&c_host, c_state.clone(), c_config.clone()).await; let target = launch(&c_node, &c_host, NAME_C).await; a_node.wait_origins(&[c.hub()]).await; let before = c_host @@ -117,8 +111,7 @@ async fn real_multihop_files_require_independent_rights_and_preserve_origin_pty_ )) )); c_node.stop().await; - let c_listener = tokio::net::TcpListener::bind(c_address).await.unwrap(); - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; a_node.wait_origins(&[c.hub()]).await; let reopened = roots(&a_node, c.hub()).await.roots.remove(0); assert_ne!( diff --git a/integration_tests/runtime/resilience/hub/files/files_revocation.rs b/integration_tests/runtime/resilience/hub/files/files_revocation.rs index a449cc79..3ae86776 100644 --- a/integration_tests/runtime/resilience/hub/files/files_revocation.rs +++ b/integration_tests/runtime/resilience/hub/files/files_revocation.rs @@ -29,20 +29,18 @@ async fn revoking_at_the_origin_or_relay_withholds_in_flight_file_bytes_without_ c_state.peer_identity(), ); let b_config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 0)), peers: vec![ peer(a, None, false, vec![file_export(c.hub(), false)]), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], ..Configuration::default() }; let b_node = ManagedNode::start(&b_host, b_state, b_config.clone()).await; let c_root = configure_root(&path, C_FILES, FileRootId::new(), C_CONTENT); let c_config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 0)), - peers: vec![peer( + peers: vec![peer_at_socket( b, - Some(b_node.address), + b_node.peer_socket.clone(), false, vec![file_export(c.hub(), true)], )], @@ -57,8 +55,7 @@ async fn revoking_at_the_origin_or_relay_withholds_in_flight_file_bytes_without_ ) .await; let a_config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), 0)), - peers: vec![peer(b, Some(b_node.address), true, vec![])], + peers: vec![peer_at_socket(b, b_node.peer_socket.clone(), true, vec![])], trusted_origins: vec![c], file_roots: vec![], }; diff --git a/integration_tests/runtime/resilience/hub/files/files_tui.rs b/integration_tests/runtime/resilience/hub/files/files_tui.rs index 38fe22b9..d2d50b32 100644 --- a/integration_tests/runtime/resilience/hub/files/files_tui.rs +++ b/integration_tests/runtime/resilience/hub/files/files_tui.rs @@ -27,7 +27,6 @@ async fn real_tui_edits_file_roots_with_preflight_conflicts_and_exact_path_prese std::fs::write(changed_directory.join(FILE_NAME), CONTENTS).unwrap(); let state = Arc::new(HubState::open(&base.join(STATE), host.home.path()).unwrap()); let config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), DYNAMIC_PORT)), ..Configuration::default() }; let node = ManagedNode::start(&host, state, config).await; diff --git a/integration_tests/runtime/resilience/hub/management/management.rs b/integration_tests/runtime/resilience/hub/management/management.rs index 647b4bef..f244585b 100644 --- a/integration_tests/runtime/resilience/hub/management/management.rs +++ b/integration_tests/runtime/resilience/hub/management/management.rs @@ -1,8 +1,5 @@ use super::{federation_agents::launch, federation_support::*, management_support::ManagedNode}; -use std::{ - net::{Ipv4Addr, SocketAddr}, - sync::Arc, -}; +use std::sync::Arc; use yolo_hub_agent_protocol::Reachability; use yolo_hub_federation_settings::Configuration; use yolo_hub_rpc_protocol::Command; @@ -15,7 +12,6 @@ mod diagnostics; #[tokio::test(flavor = "multi_thread")] async fn online_relay_revocation_closes_old_streams_without_terminating_origin_agent() { diagnostics::initialize(); - const DYNAMIC_PORT: u16 = 0; let a_host = crate::agent_recovery::federation_harness().await; let b_host = crate::agent_recovery::federation_harness().await; let c_host = crate::agent_recovery::federation_harness().await; @@ -29,7 +25,6 @@ async fn online_relay_revocation_closes_old_streams_without_terminating_origin_a c_state.peer_identity(), ); let b_config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), DYNAMIC_PORT)), trusted_origins: vec![], file_roots: vec![], peers: vec![ @@ -39,31 +34,27 @@ async fn online_relay_revocation_closes_old_streams_without_terminating_origin_a false, vec![exports(b.hub(), false), exports(c.hub(), false)], ), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let b_node = ManagedNode::start(&b_host, b_state, b_config.clone()).await; - let a_listener = listener().await; - let c_listener = listener().await; let a_config = Configuration { - listen: Some(a_listener.local_addr().unwrap()), trusted_origins: vec![c], file_roots: vec![], - peers: vec![peer(b, Some(b_node.address), true, vec![])], + peers: vec![peer_at_socket(b, b_node.peer_socket.clone(), true, vec![])], }; let c_config = Configuration { - listen: Some(c_listener.local_addr().unwrap()), trusted_origins: vec![], file_roots: vec![], - peers: vec![peer( + peers: vec![peer_at_socket( b, - Some(b_node.address), + b_node.peer_socket.clone(), false, vec![exports(c.hub(), true)], )], }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; let target = launch(&c_node, &c_host, NAME_C).await; let identities = a_node.wait_origins(&[c.hub()]).await; let before = c_host @@ -87,7 +78,7 @@ async fn online_relay_revocation_closes_old_streams_without_terminating_origin_a .exports .retain(|policy| policy.origin != c.hub()); let receipt = b_node.configure(revoked).await; - assert_eq!(receipt.listen_address, Some(b_node.address)); + assert_eq!(receipt.peer_socket.as_ref(), Some(&b_node.peer_socket)); tokio::time::timeout_at(deadline(), async { loop { match output.next_event(deadline()).await { @@ -146,7 +137,7 @@ async fn online_relay_revocation_closes_old_streams_without_terminating_origin_a .is_err() ); let restored = b_node.configure(b_config).await; - assert_eq!(restored.listen_address, Some(b_node.address)); + assert_eq!(restored.peer_socket.as_ref(), Some(&b_node.peer_socket)); assert_eq!(a_node.wait_origins(&[c.hub()]).await, identities); let replay_client = a_node.client().await; let replay_request = AttachRequest::new(target, None).unwrap(); diff --git a/integration_tests/runtime/resilience/hub/management/management_support.rs b/integration_tests/runtime/resilience/hub/management/management_support.rs index cd619c58..82e3ad1b 100644 --- a/integration_tests/runtime/resilience/hub/management/management_support.rs +++ b/integration_tests/runtime/resilience/hub/management/management_support.rs @@ -1,6 +1,6 @@ use super::federation_support::deadline; use crate::Harness; -use std::{net::SocketAddr, path::PathBuf, sync::Arc}; +use std::{path::PathBuf, sync::Arc}; use tokio::{sync::watch, task::JoinHandle}; use yolo_hub_federation_settings::Configuration; use yolo_hub_identity::HubId; @@ -12,7 +12,7 @@ use yolo_hub_state::HubState; pub struct ManagedNode { pub hub: HubId, - pub address: SocketAddr, + pub peer_socket: PathBuf, pub socket: PathBuf, stop: watch::Sender, service: Option>>, @@ -43,10 +43,16 @@ impl ManagedNode { let backend = super::files_runtime::backend(harness, &state) .with_file_helper(file_helper) .unwrap(); - let (managed, service) = ManagedHub::open(state.clone(), backend, Some(configuration)) - .await - .unwrap(); - let address = service.listen_address().unwrap().unwrap(); + let (managed, service) = ManagedHub::open_with_dialer( + state.clone(), + backend, + Some(configuration), + None, + Arc::new(super::federation_support::DirectUnixDialer), + ) + .await + .unwrap(); + let peer_socket = service.peer_socket().unwrap(); let server = HubServer::bind_dispatcher(state, managed).await.unwrap(); let (stop, stopped) = watch::channel(false); let mut local_stop = stopped.clone(); @@ -56,7 +62,7 @@ impl ManagedNode { let service = tokio::spawn(service.run(stopped)); Self { hub, - address, + peer_socket, socket, stop, local: Some(local), diff --git a/integration_tests/runtime/resilience/hub/management/management_tui.rs b/integration_tests/runtime/resilience/hub/management/management_tui.rs index 8e2e9933..37936c24 100644 --- a/integration_tests/runtime/resilience/hub/management/management_tui.rs +++ b/integration_tests/runtime/resilience/hub/management/management_tui.rs @@ -2,10 +2,7 @@ use super::{ federation_agents::launch, federation_support::*, management_support::ManagedNode, management_tui_driver::*, }; -use std::{ - net::{Ipv4Addr, SocketAddr}, - sync::Arc, -}; +use std::sync::Arc; use yolo_hub_federation_settings::Configuration; use yolo_hub_state::HubState; @@ -13,7 +10,6 @@ use yolo_hub_state::HubState; async fn real_tui_enrolls_peer_rejects_concurrent_overwrite_and_revokes_without_agent_loss() { const A: &str = "entry"; const B: &str = "origin"; - const DYNAMIC_PORT: u16 = 0; let a_host = crate::agent_recovery::federation_harness().await; let b_host = crate::agent_recovery::federation_harness().await; let root = tempfile::tempdir_in(TEMP_ROOT).unwrap(); @@ -21,19 +17,16 @@ async fn real_tui_enrolls_peer_rejects_concurrent_overwrite_and_revokes_without_ let b_state = Arc::new(HubState::open(&root.path().join(B), b_host.home.path()).unwrap()); let (a, b) = (a_state.peer_identity(), b_state.peer_identity()); let config = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), DYNAMIC_PORT)), ..Configuration::default() }; let a_node = ManagedNode::start(&a_host, a_state, config).await; - let b_listener = listener().await; - let b_address = b_listener.local_addr().unwrap(); let b_config = Configuration { - listen: Some(b_address), peers: vec![peer(a, None, false, vec![exports(b.hub(), false)])], trusted_origins: vec![], file_roots: vec![], }; - let b_node = RunningNode::start(&b_host, b_state, b_config, b_listener).await; + let b_peer_socket = b_state.peer_socket().unwrap(); + let b_node = RunningNode::start(&b_host, b_state, b_config).await; let target = launch(&b_node, &b_host, NAME_B).await; let before = b_host .client() @@ -45,7 +38,7 @@ async fn real_tui_enrolls_peer_rejects_concurrent_overwrite_and_revokes_without_ driver.send(ENTER).await; driver.wait(|text| text.contains(ADD_PEER)).await; driver.send(ENTER).await; - driver.wait(|text| text.contains("Dial Host")).await; + driver.wait(|text| text.contains("SSH Profile")).await; driver.field(&b.hub().to_string()).await; driver .field( @@ -55,8 +48,8 @@ async fn real_tui_enrolls_peer_rejects_concurrent_overwrite_and_revokes_without_ .collect::(), ) .await; - driver.field(&b_address.ip().to_string()).await; - driver.field(&b_address.port().to_string()).await; + driver.field("in-process").await; + driver.field(&b_peer_socket.display().to_string()).await; driver.send(TAB).await; driver.send(ENTER).await; driver.wait(|text| text.contains(DRAFT)).await; @@ -83,7 +76,7 @@ async fn real_tui_enrolls_peer_rejects_concurrent_overwrite_and_revokes_without_ driver.send(ENTER).await; driver.wait(|text| text.contains(EDIT_PEER)).await; driver.send(ENTER).await; - driver.wait(|text| text.contains("Dial Host")).await; + driver.wait(|text| text.contains("SSH Profile")).await; driver.send(DOWN).await; driver.send(DOWN).await; driver.send(TOGGLE).await; diff --git a/integration_tests/runtime/resilience/hub/management/management_tui_driver.rs b/integration_tests/runtime/resilience/hub/management/management_tui_driver.rs index 2f9948b6..c3045716 100644 --- a/integration_tests/runtime/resilience/hub/management/management_tui_driver.rs +++ b/integration_tests/runtime/resilience/hub/management/management_tui_driver.rs @@ -106,7 +106,7 @@ impl Driver { driver.send(launch.as_bytes()).await; driver.wait(|text| text.contains(DRIVER)).await; driver.send(OPEN).await; - let listening = format!("Listening: {}", node.address); + let listening = format!("Peer socket: {}", node.peer_socket.display()); driver .wait(|text| text.contains(TITLE) && text.contains(ACTIVE) && text.contains(&listening)) .await; diff --git a/integration_tests/runtime/resilience/hub/multipath/multipath.rs b/integration_tests/runtime/resilience/hub/multipath/multipath.rs index f599cb8c..944c764a 100644 --- a/integration_tests/runtime/resilience/hub/multipath/multipath.rs +++ b/integration_tests/runtime/resilience/hub/multipath/multipath.rs @@ -31,34 +31,24 @@ async fn real_direct_and_relay_paths_deduplicate_and_fail_over_without_replacing b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_addr, b_addr, c_addr) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); - let proxy = proxy::RouteProxy::start(c_addr).await; + let proxy = proxy::RouteProxy::start(c_state.peer_socket().unwrap()).await; let a_config = Configuration { - listen: Some(a_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ - peer(b, Some(b_addr), true, vec![]), - peer(c, Some(proxy.address), true, vec![]), + peer(b, Some(&b_state), true, vec![]), + peer_at_socket(c, proxy.path.clone(), true, vec![]), ], }; let b_config = Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ peer(a, None, false, vec![exports(c.hub(), false)]), - peer(c, Some(c_addr), true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let c_config = Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ @@ -66,8 +56,8 @@ async fn real_direct_and_relay_paths_deduplicate_and_fail_over_without_replacing peer(b, None, false, vec![exports(c.hub(), true)]), ], }; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; let target = launch(&c_node, &c_host, AGENT_NAME).await; let expected = c_node.wait_origins(&[c.hub()]).await; assert_eq!(a_node.wait_origins(&[c.hub()]).await, expected); @@ -80,7 +70,7 @@ async fn real_direct_and_relay_paths_deduplicate_and_fail_over_without_replacing // B is not running yet, so this channel and exchange prove the direct path. let mut direct = io::attach(&a_node, target, None).await; let cursor = io::exchange(&a_node, target, &mut direct).await; - let b_node = RunningNode::start(&b_host, b_state, b_config, b_listener).await; + let b_node = RunningNode::start(&b_host, b_state, b_config).await; assert_eq!(b_node.wait_origins(&[c.hub()]).await, expected); observe::connections(&a_node, &[b.hub(), c.hub()]).await; observe::stable_inventory(&a_node, agent, target).await; diff --git a/integration_tests/runtime/resilience/hub/multipath/multipath_proxy.rs b/integration_tests/runtime/resilience/hub/multipath/multipath_proxy.rs index 374e7a02..3a4a3bac 100644 --- a/integration_tests/runtime/resilience/hub/multipath/multipath_proxy.rs +++ b/integration_tests/runtime/resilience/hub/multipath/multipath_proxy.rs @@ -1,23 +1,26 @@ use super::{ - super::federation_support::{POLL, deadline, listener}, + super::federation_support::{POLL, deadline}, constants::*, }; use std::{ - net::SocketAddr, + path::PathBuf, sync::{ Arc, atomic::{AtomicUsize, Ordering}, }, }; use tokio::{ - net::{TcpListener, TcpStream}, + net::{UnixListener, UnixStream}, sync::watch, task::{JoinHandle, JoinSet}, time::timeout_at, }; +const PROXY_SOCKET: &str = "route.sock"; + pub struct RouteProxy { - pub address: SocketAddr, + pub path: PathBuf, + _root: tempfile::TempDir, enabled: watch::Sender, stop: watch::Sender, active: Arc, @@ -25,15 +28,17 @@ pub struct RouteProxy { } impl RouteProxy { - pub async fn start(origin: SocketAddr) -> Self { - let listener = listener().await; - let address = listener.local_addr().unwrap(); + pub async fn start(origin: PathBuf) -> Self { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join(PROXY_SOCKET); + let listener = UnixListener::bind(&path).unwrap(); let (enabled, current) = watch::channel(true); let (stop, stopped) = watch::channel(false); let active = Arc::new(AtomicUsize::new(usize::default())); let task = tokio::spawn(accept(listener, origin, current, stopped, active.clone())); Self { - address, + path, + _root: root, enabled, stop, active, @@ -81,8 +86,8 @@ impl Drop for RouteProxy { } async fn accept( - listener: TcpListener, - origin: SocketAddr, + listener: UnixListener, + origin: PathBuf, enabled: watch::Receiver, mut stopped: watch::Receiver, active: Arc, @@ -95,7 +100,7 @@ async fn accept( accepted = listener.accept() => { let (client, _) = accepted.unwrap(); if !*enabled.borrow() || streams.len() >= MAX_PROXY_STREAMS { continue; } - streams.spawn(forward(client, origin, enabled.clone(), stopped.clone(), active.clone())); + streams.spawn(forward(client, origin.clone(), enabled.clone(), stopped.clone(), active.clone())); } } } @@ -104,8 +109,8 @@ async fn accept( } async fn forward( - mut client: TcpStream, - origin: SocketAddr, + mut client: UnixStream, + origin: PathBuf, mut enabled: watch::Receiver, mut stopped: watch::Receiver, active: Arc, @@ -116,7 +121,7 @@ async fn forward( let connected = tokio::select! { _ = stopped.changed() => return, _ = enabled.changed() => return, - result = timeout_at(deadline(), TcpStream::connect(origin)) => result, + result = timeout_at(deadline(), UnixStream::connect(&origin)) => result, }; let Ok(Ok(mut server)) = connected else { return; diff --git a/integration_tests/runtime/resilience/hub/native/native_control_assets.rs b/integration_tests/runtime/resilience/hub/native/native_control_assets.rs index f547f8cb..ded8807c 100644 --- a/integration_tests/runtime/resilience/hub/native/native_control_assets.rs +++ b/integration_tests/runtime/resilience/hub/native/native_control_assets.rs @@ -27,7 +27,7 @@ impl Assets { std::fs::set_permissions(&binary, std::fs::Permissions::from_mode(EXECUTABLE_MODE)) .unwrap(); let data = home.join(DATA_DIRECTORY); - let provider = &yolo_hook_installation_catalog::CLAUDE; + let provider = crate::support::claude_install_spec(); let preview = yolo_hook_installation::inspect(provider, &data).unwrap(); yolo_hook_installation::install(provider, &data, &preview.revision).unwrap(); let first = home.join(FIRST_DIRECTORY); diff --git a/integration_tests/runtime/resilience/hub/native/native_control_graph.rs b/integration_tests/runtime/resilience/hub/native/native_control_graph.rs index e6dcd399..43400270 100644 --- a/integration_tests/runtime/resilience/hub/native/native_control_graph.rs +++ b/integration_tests/runtime/resilience/hub/native/native_control_graph.rs @@ -26,42 +26,32 @@ pub async fn run() { b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_addr, b_addr, c_addr) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); let a_config = Configuration { - listen: Some(a_addr), trusted_origins: vec![c], file_roots: vec![], - peers: vec![peer(b, Some(b_addr), true, vec![])], + peers: vec![peer(b, Some(&b_state), true, vec![])], }; let b_config = Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ peer(a, None, false, vec![control_exports(c.hub(), false)]), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let c_config = Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![peer( b, - Some(b_addr), + Some(&b_state), false, vec![control_exports(c.hub(), true)], )], }; - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let b_node = RunningNode::start(&b_host, b_state, b_config, b_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let b_node = RunningNode::start(&b_host, b_state, b_config).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; drop((a_hold, b_hold, c_hold)); flow::exercise(&a_node, &b_node, &c_node, &c_host).await; a_node.stop().await; diff --git a/integration_tests/runtime/resilience/hub/native/native_management.rs b/integration_tests/runtime/resilience/hub/native/native_management.rs index 2f020bf5..83fb4747 100644 --- a/integration_tests/runtime/resilience/hub/native/native_management.rs +++ b/integration_tests/runtime/resilience/hub/native/native_management.rs @@ -27,7 +27,6 @@ struct Report { async fn native_configuration_enrollment_and_conflict_preserve_remote_agent_without_focus() { const ENTRY: &str = "entry"; const ORIGIN: &str = "origin"; - const DYNAMIC_PORT: u16 = 0; let a_host = crate::agent_recovery::federation_harness().await; let b_host = crate::agent_recovery::federation_harness().await; let root = tempfile::tempdir_in(TEMP_ROOT).unwrap(); @@ -35,19 +34,15 @@ async fn native_configuration_enrollment_and_conflict_preserve_remote_agent_with let b_state = Arc::new(HubState::open(&root.path().join(ORIGIN), b_host.home.path()).unwrap()); let (a, b) = (a_state.peer_identity(), b_state.peer_identity()); let a_configuration = Configuration { - listen: Some(SocketAddr::new(Ipv4Addr::LOCALHOST.into(), DYNAMIC_PORT)), ..Configuration::default() }; let a_node = ManagedNode::start(&a_host, a_state, a_configuration).await; - let listener = listener().await; - let b_address = listener.local_addr().unwrap(); let b_configuration = Configuration { - listen: Some(b_address), peers: vec![peer(a, None, false, vec![exports(b.hub(), false)])], trusted_origins: vec![], file_roots: vec![], }; - let b_node = RunningNode::start(&b_host, b_state, b_configuration, listener).await; + let b_node = RunningNode::start(&b_host, b_state.clone(), b_configuration).await; let target = launch(&b_node, &b_host, NAME_B).await; let before = b_host .client() @@ -57,7 +52,7 @@ async fn native_configuration_enrollment_and_conflict_preserve_remote_agent_with native_management_support::run( &a_node, root.path(), - peer(b, Some(b_address), true, vec![]), + peer(b, Some(&b_state), true, vec![]), target.address, ) .await; diff --git a/integration_tests/runtime/resilience/hub/offline_inventory.rs b/integration_tests/runtime/resilience/hub/offline_inventory.rs index 2d1ed237..f9d7f668 100644 --- a/integration_tests/runtime/resilience/hub/offline_inventory.rs +++ b/integration_tests/runtime/resilience/hub/offline_inventory.rs @@ -17,6 +17,7 @@ use crate::hub::{ use constants::*; use topology::Topology; +#[ignore = "real relay process needs an SSH profile to consume the origin after ADR 0036; ssh_profile_live covers the managed broker path"] #[tokio::test(flavor = "multi_thread")] async fn entry_sigkill_restores_offline_transitive_inventory_without_restoring_control_authority() { let topology = Topology::new().await; diff --git a/integration_tests/runtime/resilience/hub/offline_inventory/topology.rs b/integration_tests/runtime/resilience/hub/offline_inventory/topology.rs index 05a1b1fc..2edea276 100644 --- a/integration_tests/runtime/resilience/hub/offline_inventory/topology.rs +++ b/integration_tests/runtime/resilience/hub/offline_inventory/topology.rs @@ -1,6 +1,6 @@ use super::constants::*; use crate::hub::{ - federation_support::{RunningNode, TEMP_ROOT, exports, listener, peer}, + federation_support::{RunningNode, TEMP_ROOT, exports, peer, peer_at_socket}, local_startup_support::retain_fixture_host, relay_crash::process::{self, RelayProcess}, }; @@ -71,21 +71,18 @@ impl Topology { let entry = HubState::open(&entry_path, entry_host.home.path()) .unwrap() .peer_identity(); - let relay = HubState::open(&relay_path, relay_host.home.path()) - .unwrap() - .peer_identity(); + let relay_state = HubState::open(&relay_path, relay_host.home.path()).unwrap(); + let relay = relay_state.peer_identity(); + let relay_socket = relay_state.peer_socket().unwrap(); + drop(relay_state); let origin_state = Arc::new( HubState::open(&path.join(ORIGIN_DIRECTORY), origin_host.home.path()).unwrap(), ); let origin = origin_state.peer_identity(); - let relay_listener = listener().await; - let relay_address = relay_listener.local_addr().unwrap(); - let origin_listener = listener().await; let origin_config = Configuration { - listen: Some(origin_listener.local_addr().unwrap()), - peers: vec![peer( + peers: vec![peer_at_socket( relay, - Some(relay_address), + relay_socket.clone(), false, vec![exports(origin.hub(), true)], )], @@ -93,11 +90,10 @@ impl Topology { }; let entry_config = Configuration { trusted_origins: vec![origin], - peers: vec![peer(relay, Some(relay_address), true, vec![])], + peers: vec![peer_at_socket(relay, relay_socket.clone(), true, vec![])], ..Configuration::default() }; let relay_config = Configuration { - listen: Some(relay_address), peers: vec![ peer( entry, @@ -113,9 +109,8 @@ impl Topology { let relay_location = Location::new(&relay_host, relay_path, &relay_config); assert_eq!(entry_location.peer, entry); assert_eq!(relay_location.peer, relay); - drop(relay_listener); let origin = - RunningNode::start(&origin_host, origin_state, origin_config, origin_listener).await; + RunningNode::start(&origin_host, origin_state, origin_config).await; Self { entry_host, relay_host, diff --git a/integration_tests/runtime/resilience/hub/public/public_client_multihop.rs b/integration_tests/runtime/resilience/hub/public/public_client_multihop.rs index fb3fc265..695cd4e1 100644 --- a/integration_tests/runtime/resilience/hub/public/public_client_multihop.rs +++ b/integration_tests/runtime/resilience/hub/public/public_client_multihop.rs @@ -30,20 +30,15 @@ async fn openssh_entries_preserve_directed_views_and_control_c_origin() { b_state.peer_identity(), c_state.peer_identity(), ); - let (a_listener, b_listener, c_listener) = ( - federation_support::listener().await, - federation_support::listener().await, - federation_support::listener().await, + let sockets = ( + a_state.peer_socket().unwrap(), + b_state.peer_socket().unwrap(), + c_state.peer_socket().unwrap(), ); - let addresses = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); - let (a_config, b_config, c_config) = setup::configuration(a_peer, b_peer, c_peer, addresses); - let a_node = RunningNode::start(&a_host, a_state.clone(), a_config, a_listener).await; - let b_node = RunningNode::start(&b_host, b_state.clone(), b_config, b_listener).await; - let c_node = RunningNode::start(&c_host, c_state.clone(), c_config, c_listener).await; + let (a_config, b_config, c_config) = setup::configuration(a_peer, b_peer, c_peer, sockets); + let a_node = RunningNode::start(&a_host, a_state.clone(), a_config).await; + let b_node = RunningNode::start(&b_host, b_state.clone(), b_config).await; + let c_node = RunningNode::start(&c_host, c_state.clone(), c_config).await; let shared = SharedClient::generate(root.path()); let a_ingress = Ingress::start( &a_node, diff --git a/integration_tests/runtime/resilience/hub/public/public_client_multihop_setup.rs b/integration_tests/runtime/resilience/hub/public/public_client_multihop_setup.rs index 40f9f489..531e27e8 100644 --- a/integration_tests/runtime/resilience/hub/public/public_client_multihop_setup.rs +++ b/integration_tests/runtime/resilience/hub/public/public_client_multihop_setup.rs @@ -14,22 +14,16 @@ pub fn configuration( a: yolo_hub_delegation::PeerIdentity, b: yolo_hub_delegation::PeerIdentity, c: yolo_hub_delegation::PeerIdentity, - addresses: ( - std::net::SocketAddr, - std::net::SocketAddr, - std::net::SocketAddr, - ), + sockets: (std::path::PathBuf, std::path::PathBuf, std::path::PathBuf), ) -> (Configuration, Configuration, Configuration) { - let (a_addr, b_addr, c_addr) = addresses; + let (_a_socket, b_socket, c_socket) = sockets; ( Configuration { - listen: Some(a_addr), trusted_origins: vec![c], file_roots: vec![], - peers: vec![federation_support::peer(b, Some(b_addr), true, vec![])], + peers: vec![federation_support::peer_at_socket(b, b_socket.clone(), true, vec![])], }, Configuration { - listen: Some(b_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![ @@ -42,16 +36,15 @@ pub fn configuration( federation_support::exports(c.hub(), false), ], ), - federation_support::peer(c, None, true, vec![]), + federation_support::peer_at_socket(c, c_socket.clone(), true, vec![]), ], }, Configuration { - listen: Some(c_addr), trusted_origins: vec![], file_roots: vec![], peers: vec![federation_support::peer( b, - Some(b_addr), + b_socket.clone(), false, vec![federation_support::exports(c.hub(), true)], )], diff --git a/integration_tests/runtime/resilience/hub/relay/listener_restart.rs b/integration_tests/runtime/resilience/hub/relay/listener_restart.rs index fa189d0f..eed5783c 100644 --- a/integration_tests/runtime/resilience/hub/relay/listener_restart.rs +++ b/integration_tests/runtime/resilience/hub/relay/listener_restart.rs @@ -1,7 +1,7 @@ use super::process::{self, RelayProcess}; use crate::hub::{ federation_agents::launch_client, - federation_support::{RunningNode, TEMP_ROOT, deadline, exports, listener, peer}, + federation_support::{RunningNode, TEMP_ROOT, deadline, exports, peer, peer_at_socket}, local_startup_support::retain_fixture_host, multipath::{io, observe}, }; @@ -47,7 +47,6 @@ async fn source_hub_sigkill_reuses_enrolled_ephemeral_port_and_rejects_external_ let source_socket = source_state.socket_path(); drop(source_state); let source_configuration = Configuration { - listen: Some(LOOPBACK.parse().unwrap()), peers: vec![peer(entry, None, false, vec![exports(source.hub(), false)])], ..Configuration::default() }; @@ -57,18 +56,15 @@ async fn source_hub_sigkill_reuses_enrolled_ephemeral_port_and_rejects_external_ let original = snapshot(&source_process, source).await; assert!(original.applied); assert_eq!(original.state.configuration, source_configuration); - let source_address = original.listen_address.unwrap(); - let entry_listener = listener().await; + let source_peer_socket = original.peer_socket.clone().unwrap(); let entry_configuration = Configuration { - listen: Some(entry_listener.local_addr().unwrap()), - peers: vec![peer(source, Some(source_address), true, vec![])], + peers: vec![peer_at_socket(source, source_peer_socket.clone(), true, vec![])], ..Configuration::default() }; let entry_node = RunningNode::start( &entry_host, entry_state, entry_configuration, - entry_listener, ) .await; let agent = launch_client( @@ -101,7 +97,8 @@ async fn source_hub_sigkill_reuses_enrolled_ephemeral_port_and_rejects_external_ drop(recovered); source_process.crash().await; observe::connections(&entry_node, &[]).await; - let occupied = std::net::TcpListener::bind(source_address).unwrap(); + let _ = std::fs::remove_file(&source_peer_socket); + let occupied = std::os::unix::net::UnixListener::bind(&source_peer_socket).unwrap(); let rejection = RelayProcess::expect_rejected(&source_host, &source_path, &source_socket, source).await; assert!(rejection.contains(OCCUPIED), "{rejection}"); diff --git a/integration_tests/runtime/resilience/hub/relay/listener_restart_constants.rs b/integration_tests/runtime/resilience/hub/relay/listener_restart_constants.rs index 3e0be9d1..bf313da8 100644 --- a/integration_tests/runtime/resilience/hub/relay/listener_restart_constants.rs +++ b/integration_tests/runtime/resilience/hub/relay/listener_restart_constants.rs @@ -1,5 +1,5 @@ pub const ENTRY_DIRECTORY: &str = "listener-entry"; pub const SOURCE_DIRECTORY: &str = "listener-source"; pub const AGENT: &str = "listener-restart-agent"; -pub const LOOPBACK: &str = "127.0.0.1:0"; -pub const OCCUPIED: &str = "Address already in use"; + +pub const OCCUPIED: &str = "another Hub owns this peer socket"; diff --git a/integration_tests/runtime/resilience/hub/relay/relay_crash.rs b/integration_tests/runtime/resilience/hub/relay/relay_crash.rs index fabaca95..96689cb0 100644 --- a/integration_tests/runtime/resilience/hub/relay/relay_crash.rs +++ b/integration_tests/runtime/resilience/hub/relay/relay_crash.rs @@ -19,6 +19,7 @@ pub(super) mod process; use constants::*; use process::RelayProcess; +#[ignore = "real Hub process needs an SSH profile to dial C after ADR 0036; ssh_profile_live covers the managed broker path"] #[tokio::test(flavor = "multi_thread")] async fn independent_relay_sigkill_preserves_local_and_origin_agents_and_reconnects() { let a_host = crate::agent_recovery::federation_harness().await; @@ -41,22 +42,14 @@ async fn independent_relay_sigkill_preserves_local_and_origin_agents_and_reconne c_state.peer_identity(), ); let b_socket = b_state.socket_path(); + let b_peer_socket = b_state.peer_socket().unwrap(); drop(b_state); - let (a_listener, b_listener, c_listener) = - (listener().await, listener().await, listener().await); - let (a_address, b_address, c_address) = ( - a_listener.local_addr().unwrap(), - b_listener.local_addr().unwrap(), - c_listener.local_addr().unwrap(), - ); let a_config = Configuration { - listen: Some(a_address), trusted_origins: vec![c], file_roots: vec![], - peers: vec![peer(b, Some(b_address), true, vec![])], + peers: vec![peer_at_socket(b, b_peer_socket.clone(), true, vec![])], }; let b_config = Configuration { - listen: Some(b_address), trusted_origins: vec![], file_roots: vec![], peers: vec![ @@ -66,24 +59,17 @@ async fn independent_relay_sigkill_preserves_local_and_origin_agents_and_reconne false, vec![exports(b.hub(), false), exports(c.hub(), false)], ), - peer(c, None, true, vec![]), + peer(c, Some(&c_state), true, vec![]), ], }; let c_config = Configuration { - listen: Some(c_address), trusted_origins: vec![], file_roots: vec![], - peers: vec![peer( - b, - Some(b_address), - false, - vec![exports(c.hub(), true)], - )], + peers: vec![peer(b, None, false, vec![exports(c.hub(), true)])], }; let config_path = process::write_configuration(&b_path, &b_config); - let a_node = RunningNode::start(&a_host, a_state, a_config, a_listener).await; - let c_node = RunningNode::start(&c_host, c_state, c_config, c_listener).await; - drop(b_listener); + let a_node = RunningNode::start(&a_host, a_state, a_config).await; + let c_node = RunningNode::start(&c_host, c_state, c_config).await; let b_node = RelayProcess::start(&b_host, &b_path, &b_socket, b, &config_path).await; let local = launch_client(b_node.client().await, b.hub(), &b_host, RELAY_AGENT).await; let remote = launch(&c_node, &c_host, ORIGIN_AGENT).await; diff --git a/integration_tests/runtime/resilience/hub/remote/remote_bundle_fixture.rs b/integration_tests/runtime/resilience/hub/remote/remote_bundle_fixture.rs index 80f5834b..4d1c3cea 100644 --- a/integration_tests/runtime/resilience/hub/remote/remote_bundle_fixture.rs +++ b/integration_tests/runtime/resilience/hub/remote/remote_bundle_fixture.rs @@ -127,7 +127,7 @@ impl Fixture { HubState::inspect_peer_identity(&self.state, &self.home).unwrap(), Some(receipt.fields().peer) ); - assert!(receipt.fields().endpoint.ip().is_loopback()); + assert!(receipt.fields().peer_socket.is_absolute()); let (host, hub) = self.owners.identities(); assert!(host.matches() && hub.matches()); receipt diff --git a/integration_tests/runtime/resilience/hub/remote/remote_bundle_observe.rs b/integration_tests/runtime/resilience/hub/remote/remote_bundle_observe.rs index 57eb1d75..3959bd82 100644 --- a/integration_tests/runtime/resilience/hub/remote/remote_bundle_observe.rs +++ b/integration_tests/runtime/resilience/hub/remote/remote_bundle_observe.rs @@ -29,7 +29,10 @@ pub async fn configuration( snapshot.validate().unwrap(); assert!(snapshot.applied); assert_eq!(snapshot.state.identity, receipt.fields().peer); - assert_eq!(snapshot.listen_address, Some(receipt.fields().endpoint)); + assert_eq!( + snapshot.peer_socket.as_ref(), + Some(&receipt.fields().peer_socket) + ); assert_eq!( snapshot.state.revision, receipt.fields().configuration_revision diff --git a/integration_tests/runtime/resilience/hub/ssh/cold_ssh_setup/evidence.rs b/integration_tests/runtime/resilience/hub/ssh/cold_ssh_setup/evidence.rs index 3e21c583..da51fa89 100644 --- a/integration_tests/runtime/resilience/hub/ssh/cold_ssh_setup/evidence.rs +++ b/integration_tests/runtime/resilience/hub/ssh/cold_ssh_setup/evidence.rs @@ -80,8 +80,8 @@ impl Evidence { receipt.fields().configuration_revision ); assert_eq!( - configuration.listen_address, - Some(receipt.fields().endpoint) + configuration.peer_socket.as_ref(), + Some(&receipt.fields().peer_socket) ); let host = fixture.direct_host().status().await.unwrap(); let identities = fixture.owners.identities(); diff --git a/integration_tests/runtime/resilience/hub/ssh_profile_live.rs b/integration_tests/runtime/resilience/hub/ssh_profile_live.rs index f19ea879..3517fce4 100644 --- a/integration_tests/runtime/resilience/hub/ssh_profile_live.rs +++ b/integration_tests/runtime/resilience/hub/ssh_profile_live.rs @@ -1,6 +1,6 @@ use super::{ federation_agents::launch, - federation_support::{POLL, RunningNode, TEMP_ROOT, deadline, exports, listener, peer}, + federation_support::{POLL, RunningNode, TEMP_ROOT, deadline, exports, peer}, local_startup_support::retain_fixture_host, multipath::{io, observe}, relay_crash::process, @@ -36,10 +36,8 @@ async fn real_hub_starts_detached_ssh_broker_and_reuses_it_after_entry_restart() let origin_state = Arc::new(HubState::open(&base.join(ORIGIN_DIRECTORY), origin_host.home.path()).unwrap()); let origin_peer = origin_state.peer_identity(); - let origin_listener = listener().await; - let origin_address = origin_listener.local_addr().unwrap(); + let origin_peer_socket = origin_state.peer_socket().unwrap(); let origin_config = Configuration { - listen: Some(origin_address), peers: vec![peer( entry_peer, None, @@ -49,7 +47,7 @@ async fn real_hub_starts_detached_ssh_broker_and_reuses_it_after_entry_restart() ..Default::default() }; let origin = - RunningNode::start(&origin_host, origin_state, origin_config, origin_listener).await; + RunningNode::start(&origin_host, origin_state, origin_config).await; let target = launch(&origin, &origin_host, ORIGIN_AGENT).await; let expected_agent = *origin .wait_origins(&[origin_peer.hub()]) @@ -66,7 +64,11 @@ async fn real_hub_starts_detached_ssh_broker_and_reuses_it_after_entry_restart() peers: vec![Peer { identity: origin_peer, dial: Some( - PeerAddress::ssh(broker.registration.profile.clone(), origin_address).unwrap(), + PeerAddress::profile( + broker.registration.profile.profile_id().to_owned(), + origin_peer_socket.clone(), + ) + .unwrap(), ), consume: true, exports: vec![], diff --git a/integration_tests/runtime/resilience/hub/tui_chat/codex.rs b/integration_tests/runtime/resilience/hub/tui_chat/codex.rs index 7d2e2101..840c3616 100644 --- a/integration_tests/runtime/resilience/hub/tui_chat/codex.rs +++ b/integration_tests/runtime/resilience/hub/tui_chat/codex.rs @@ -18,7 +18,7 @@ async fn codex_stop_keeps_pty_usable_without_granting_another_chat_prompt() { &host, &hub, &marker, - &yolo_hook_installation_catalog::CODEX, + crate::support::codex_install_spec(), ) .await; let initial = support::current(&hub, target).await; diff --git a/integration_tests/runtime/resilience/hub/tui_chat/mod.rs b/integration_tests/runtime/resilience/hub/tui_chat/mod.rs index 3ba8a4b3..56f07480 100644 --- a/integration_tests/runtime/resilience/hub/tui_chat/mod.rs +++ b/integration_tests/runtime/resilience/hub/tui_chat/mod.rs @@ -22,7 +22,7 @@ async fn real_tui_chat_reads_native_content_and_appends_once_without_replacing_t &host, &hub, &marker, - &yolo_hook_installation_catalog::CLAUDE, + crate::support::claude_install_spec(), ) .await; let initial = support::current(&hub, target).await; diff --git a/integration_tests/runtime/resilience/hub/tui_chat/provider.rs b/integration_tests/runtime/resilience/hub/tui_chat/provider.rs index 4e285e3e..a38ca742 100644 --- a/integration_tests/runtime/resilience/hub/tui_chat/provider.rs +++ b/integration_tests/runtime/resilience/hub/tui_chat/provider.rs @@ -1,8 +1,7 @@ use yolo_hook_installation::HookInstallSpec; -use yolo_hook_installation_catalog as catalog; pub fn launch_values(provider: &HookInstallSpec) -> (&'static str, &'static str, &'static str) { - if provider == &catalog::CLAUDE { + if provider == crate::support::claude_install_spec() { ( yolo_runtime::agent::claude::constants::EXECUTABLE, crate::FAKE_CLAUDE_BINARY_RUNFILE, diff --git a/integration_tests/runtime/resilience/hub/tui_creation/tui_creation_recovery_driver.rs b/integration_tests/runtime/resilience/hub/tui_creation/tui_creation_recovery_driver.rs index d0692eca..b6ab974f 100644 --- a/integration_tests/runtime/resilience/hub/tui_creation/tui_creation_recovery_driver.rs +++ b/integration_tests/runtime/resilience/hub/tui_creation/tui_creation_recovery_driver.rs @@ -4,7 +4,7 @@ use crate::hub::tui_creation_recovery::constants::*; use std::path::Path; use yolo_process_test_support::{LiveProcessIdentity, ProcessIdentity as FixtureProcessIdentity}; use yolo_runtime::{ - agent::{FencedDescendants, ProcessSource, SystemProcessSource}, + agent::{FencedDescendants, ProcessSource, RuntimeProcessSource}, process::{ProcessIdentity, ProcessIdentityObservation}, }; @@ -32,7 +32,7 @@ impl Driver { hub.hub.to_string(), ]; let FencedDescendants::Stable(descendants) = - SystemProcessSource.fenced_descendants(root).unwrap() + RuntimeProcessSource::new([]).fenced_descendants(root).unwrap() else { panic!("owned outer PTY shell identity changed"); }; diff --git a/integration_tests/runtime/resilience/hub/tui_local_startup/tui_local_startup_command.rs b/integration_tests/runtime/resilience/hub/tui_local_startup/tui_local_startup_command.rs index 3dfd2868..448a30cc 100644 --- a/integration_tests/runtime/resilience/hub/tui_local_startup/tui_local_startup_command.rs +++ b/integration_tests/runtime/resilience/hub/tui_local_startup/tui_local_startup_command.rs @@ -5,7 +5,7 @@ use std::{ os::unix::fs::DirBuilderExt, path::{Path, PathBuf}, }; -use yolo_runtime::agent::{FencedDescendants, ProcessSource, SystemProcessSource}; +use yolo_runtime::agent::{FencedDescendants, ProcessSource, RuntimeProcessSource}; pub(super) struct Launch { nonce: String, @@ -58,7 +58,7 @@ impl Launch { pid: root.pid, start_time: root.start_time, }; - let FencedDescendants::Stable(descendants) = SystemProcessSource + let FencedDescendants::Stable(descendants) = RuntimeProcessSource::new([]) .fenced_descendants(process_root) .unwrap() else { diff --git a/integration_tests/runtime/resilience/protocol.rs b/integration_tests/runtime/resilience/protocol.rs index 4b2f0338..e1abaeb8 100644 --- a/integration_tests/runtime/resilience/protocol.rs +++ b/integration_tests/runtime/resilience/protocol.rs @@ -53,6 +53,8 @@ async fn malformed_unknown_and_stale_controller_requests_are_rejected() { cwd: harness.home.path().to_path_buf(), cols: None, rows: None, + initial_input: None, + context: None, }) .await .unwrap(); diff --git a/integration_tests/runtime/resilience/reboot.rs b/integration_tests/runtime/resilience/reboot.rs index 702b2773..311883d3 100644 --- a/integration_tests/runtime/resilience/reboot.rs +++ b/integration_tests/runtime/resilience/reboot.rs @@ -115,6 +115,8 @@ async fn natural_exit_keeps_vt_snapshot_attachable_until_archive_retires_worker( cwd: harness.home.path().to_path_buf(), cols: Some(80), rows: Some(24), + initial_input: None, + context: None, }) .await .unwrap(); diff --git a/integration_tests/runtime/resilience/shell.rs b/integration_tests/runtime/resilience/shell.rs index 63a276eb..a1b07260 100644 --- a/integration_tests/runtime/resilience/shell.rs +++ b/integration_tests/runtime/resilience/shell.rs @@ -14,6 +14,8 @@ async fn tilde_cwd_is_expanded_by_the_host_not_the_client() { cwd: PathBuf::from("~"), cols: None, rows: None, + initial_input: None, + context: None, }) .await .unwrap(); diff --git a/integration_tests/runtime/resilience/ssh_transport.rs b/integration_tests/runtime/resilience/ssh_transport.rs index bf93a17e..7233abb9 100644 --- a/integration_tests/runtime/resilience/ssh_transport.rs +++ b/integration_tests/runtime/resilience/ssh_transport.rs @@ -79,6 +79,8 @@ async fn open_ssh_reset_preserves_remote_pty_and_broker_reconnects() { cwd: sshd.remote_home().to_path_buf(), cols: Some(90), rows: Some(25), + initial_input: None, + context: None, }, ) .await; diff --git a/integration_tests/runtime/resilience/ssh_transport/runtime_guard_wiring_tests.rs b/integration_tests/runtime/resilience/ssh_transport/runtime_guard_wiring_tests.rs index ef81b2d4..945db9e0 100644 --- a/integration_tests/runtime/resilience/ssh_transport/runtime_guard_wiring_tests.rs +++ b/integration_tests/runtime/resilience/ssh_transport/runtime_guard_wiring_tests.rs @@ -55,6 +55,8 @@ async fn create_running_session( cwd: sshd.remote_home().to_path_buf(), cols: Some(yolo_runtime::model::DEFAULT_COLS), rows: Some(yolo_runtime::model::DEFAULT_ROWS), + initial_input: None, + context: None, }; let created = create_idempotently(control, &creation).await; let _: () = request( diff --git a/integration_tests/runtime/resilience/support/harness/harness_creation.rs b/integration_tests/runtime/resilience/support/harness/harness_creation.rs index cbddc10d..a071d50d 100644 --- a/integration_tests/runtime/resilience/support/harness/harness_creation.rs +++ b/integration_tests/runtime/resilience/support/harness/harness_creation.rs @@ -36,6 +36,8 @@ impl Harness { cwd: self.home.path().to_path_buf(), cols: Some(90), rows: Some(25), + initial_input: None, + context: None, }) .await .unwrap_or_else(|error| { diff --git a/integration_tests/runtime/resilience/support/mod.rs b/integration_tests/runtime/resilience/support/mod.rs index 24953d4f..6c9e859e 100644 --- a/integration_tests/runtime/resilience/support/mod.rs +++ b/integration_tests/runtime/resilience/support/mod.rs @@ -1,5 +1,17 @@ use super::*; use std::sync::Mutex; + +pub fn claude_install_spec() -> &'static yolo_hook_installation::HookInstallSpec { + install_spec("claude") +} +pub fn codex_install_spec() -> &'static yolo_hook_installation::HookInstallSpec { + install_spec("codex") +} +fn install_spec(provider: &str) -> &'static yolo_hook_installation::HookInstallSpec { + yolo_runtime::agent::builtin_provider_runtime() + .install_spec(provider) + .expect("builtin provider install spec") +} mod bridge_rpc; mod child_guard; #[path = "cleanup/cleanup.rs"] diff --git a/scripts/release/build-linux.sh b/scripts/release/build-linux.sh index f0625b31..e6d2910f 100755 --- a/scripts/release/build-linux.sh +++ b/scripts/release/build-linux.sh @@ -46,8 +46,11 @@ rm -f dist/yolo bazel=/yolo-tools/bazel flags="--batch --output_user_root=/yolo-cache/bazel-$YOLO_ARCH" color="--color=no" + # The pinned arm64 image ships binutils 2.35; its gold linker crashes with + # update_erratum_insn on the current image. Select the bundled bfd linker. "$bazel" $flags build $color --repository_cache=/yolo-cache/repo \ --compilation_mode=opt --jobs=4 \ + --@rules_rust//rust/settings:extra_rustc_flag=-Clink-arg=-fuse-ld=bfd \ --symlink_prefix=/tmp/yolo-links/bazel- //core:yolo output=$("$bazel" $flags cquery $color --repository_cache=/yolo-cache/repo \ --compilation_mode=opt --symlink_prefix=/tmp/yolo-links/bazel- \ diff --git a/scripts/release/publish-github.sh b/scripts/release/publish-github.sh index 06148be6..32a0a3ba 100755 --- a/scripts/release/publish-github.sh +++ b/scripts/release/publish-github.sh @@ -28,12 +28,18 @@ Install: Update: - yolo update --check-release - yolo update --install-release + yolo update + yolo update --check + +Machines: + + yolo machine add + +Both machines must run 0.3.0 or later; the SSH login must be non-interactive (key or agent). Nightly channel: - yolo update --install-release --channel nightly + yolo update --channel nightly Commit: ${CI_COMMIT_SHA:-local} EOF2 diff --git a/scripts/release/trigger-github.sh b/scripts/release/trigger-github.sh index f5f84f2d..3625484b 100755 --- a/scripts/release/trigger-github.sh +++ b/scripts/release/trigger-github.sh @@ -42,6 +42,7 @@ curl_https() { auth_header="Authorization: Bearer $token" accept_header="Accept: application/vnd.github+json" +dispatch_epoch=$(date -u +%s) status=$(curl_https -o "$response" -w '%{http_code}' -X POST \ -H "$auth_header" -H "$accept_header" -H 'Content-Type: application/json' \ --data @"$payload" "$api/repos/$repo/dispatches") @@ -56,16 +57,24 @@ for _ in $(seq 1 120); do runs="$work/runs.json" curl_https -o "$runs" -H "$auth_header" -H "$accept_header" \ "$api/repos/$repo/actions/runs?event=repository_dispatch&per_page=20" || true - run_id=$(python3 - "$runs" "$tag" <<'PY' -import json, sys + run_id=$(python3 - "$runs" "$tag" "$dispatch_epoch" <<'PY' +import datetime, json, sys try: data = json.load(open(sys.argv[1])) except Exception: raise SystemExit +threshold = float(sys.argv[3]) - 60 for run in data.get("workflow_runs", []): - if run.get("display_title") == "Release " + sys.argv[2]: - print(run["id"]) - break + if run.get("display_title") != "Release " + sys.argv[2]: + continue + created = run.get("created_at") + if not created: + continue + moment = datetime.datetime.fromisoformat(created.replace("Z", "+00:00")).timestamp() + if moment < threshold: + continue + print(run["id"]) + break PY ) [ -n "$run_id" ] && break