From d7fd5cb6f113bd6ad7bfc1456b6005ed2fdb8e1e Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 13:27:23 +0000 Subject: [PATCH 1/5] perf(vendor): cache gpui views and keep list estimates vendor/ as the integrated work needs it, kept apart from the application changes that use it. The entries below are the vendored changes. feat(vendor): expose native input document revisions perf(vendor): reuse cached views nested in a redrawn cached view perf(vendor): draw the window content as a cached view perf(vendor): cache views to their content and cache dock tab groups fix(vendor): draw the window content uncached again fix: preserve virtual list height estimates across layout changes Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WAgRyZqRCp4XuUHFDB1w3k --- vendor/gpui-base/NOCTERM.md | 16 + vendor/gpui-base/src/dock/dock_area.rs | 20 +- vendor/gpui-base/src/dock/tab_group.rs | 17 +- vendor/gpui-base/src/input/base/state.rs | 7 + vendor/gpui-pre/Cargo.toml | 5 + vendor/gpui-pre/NOCTERM_PATCH.md | 38 ++ vendor/gpui-pre/src/elements/list.rs | 6 +- .../gpui-pre/src/text_system/line_layout.rs | 27 ++ vendor/gpui-pre/src/view.rs | 325 +++++++++++++++-- vendor/gpui-pre/src/window.rs | 327 ++++++++++++++++++ .../gpui-pre/tests/nocterm_list_estimates.rs | 82 +++++ .../0002-input-document-revision.patch | 30 ++ .../gpui-base/0003-cached-root-content.patch | 26 ++ .../gpui-base/0004-cached-tab-groups.patch | 49 +++ .../0005-uncached-root-content.patch | 29 ++ .../gpui-pre/0002-nested-view-cache.patch | 137 ++++++++ .../gpui-pre/0003-content-sized-cache.patch | 108 ++++++ .../gpui-pre/0004-list-width-estimates.patch | 73 ++++ vendor/patches/sources.json | 11 +- 19 files changed, 1298 insertions(+), 35 deletions(-) create mode 100644 vendor/gpui-pre/tests/nocterm_list_estimates.rs create mode 100644 vendor/patches/gpui-base/0002-input-document-revision.patch create mode 100644 vendor/patches/gpui-base/0003-cached-root-content.patch create mode 100644 vendor/patches/gpui-base/0004-cached-tab-groups.patch create mode 100644 vendor/patches/gpui-base/0005-uncached-root-content.patch create mode 100644 vendor/patches/gpui-pre/0002-nested-view-cache.patch create mode 100644 vendor/patches/gpui-pre/0003-content-sized-cache.patch create mode 100644 vendor/patches/gpui-pre/0004-list-width-estimates.patch diff --git a/vendor/gpui-base/NOCTERM.md b/vendor/gpui-base/NOCTERM.md index f61dab67..bc4c02e2 100644 --- a/vendor/gpui-base/NOCTERM.md +++ b/vendor/gpui-base/NOCTERM.md @@ -1,5 +1,11 @@ # Nocterm's GPUI Base zoom patch +The input engine exposes its existing synchronous document revision through a +read-only getter. Programmatic value replacement advances it even when Change +events are suppressed. The existing counter wraps at u64::MAX; consumers that +need stale-operation fencing must bind the input instance and reject observed +revision regression. No input state, event timing or edit semantics change. + This vendored fork, including Nocterm's local modifications, is distributed under Apache-2.0, as declared by its original Cargo manifest. Upstream copyright notices and LICENSE-APACHE remain in force; the application's PolyForm Perimeter @@ -53,3 +59,13 @@ close guards and explicit locks. The former zoom-lock test now asserts the reque zoomed drag/drop behavior; explicit lock and last-visible regressions remain intact. New-panel registration can target a group directly, avoiding a transient cross-group move that would otherwise clear zoom when adding into a nonfirst split pane. + +`root.rs` draws the window's content view uncached again. Drawing it as a cached view +(patch 0003) moved the focus a click gives away from a text input in the content, so the +input registered no input handler and typed text never reached it; patch 0005 undoes it. + +`dock/dock_area.rs` draws each tab group as a cached view, so a change in one group, +such as its active panel's output, draws neither the other groups nor their tab bars. +The area's own change draws all its groups again. `dock/tab_group.rs` observes the +views of the panels it holds through Nocterm's `AnyView::observe` in the vendored +`gpui-pre`, because its tab bar draws every panel's title, including inactive ones. diff --git a/vendor/gpui-base/src/dock/dock_area.rs b/vendor/gpui-base/src/dock/dock_area.rs index f1c17c7e..7ff4c783 100644 --- a/vendor/gpui-base/src/dock/dock_area.rs +++ b/vendor/gpui-base/src/dock/dock_area.rs @@ -13,8 +13,8 @@ use anyhow::Result; use gpui::{ AnyElement, AnyView, App, AppContext as _, Axis, Bounds, Context, Div, Empty, Entity, EventEmitter, FocusHandle, Focusable, InteractiveElement as _, IntoElement, ParentElement, - Pixels, Point, Render, SharedString, Stateful, Styled as _, Subscription, WeakEntity, Window, - div, prelude::FluentBuilder as _, px, + Pixels, Point, Render, SharedString, Stateful, StyleRefinement, Styled as _, Subscription, + WeakEntity, Window, div, prelude::FluentBuilder as _, px, }; use crate::{ @@ -133,6 +133,14 @@ impl DockArea { cx: &mut Context, ) -> Self { PanelRegistry::init(cx); + // Nocterm: the groups are drawn as cached views; the area's own change draws + // them again. + cx.observe_self(|area, cx| { + for group in area.groups.values() { + group.entity.update(cx, |_, cx| cx.notify()); + } + }) + .detach(); Self { id: id.into(), @@ -1007,8 +1015,14 @@ impl DockArea { .child(group) .into_any_element() } + // Nocterm: cached, so a change in one group draws only that group; any + // change of the area draws them all. PaneRef::Tabs { .. } => match self.groups.get(&node.id()) { - Some(cached) => cached.entity.clone().into_any_element(), + Some(cached) => cached + .entity + .clone() + .cached(StyleRefinement::default().size_full()) + .into_any_element(), None => Empty.into_any_element(), }, } diff --git a/vendor/gpui-base/src/dock/tab_group.rs b/vendor/gpui-base/src/dock/tab_group.rs index fec13329..899349c1 100644 --- a/vendor/gpui-base/src/dock/tab_group.rs +++ b/vendor/gpui-base/src/dock/tab_group.rs @@ -7,7 +7,8 @@ use std::{rc::Rc, sync::Arc}; use gpui::{ AnyElement, AnyView, App, Bounds, Context, Div, DragMoveEvent, Empty, EventEmitter, FocusHandle, Focusable, InteractiveElement as _, IntoElement, ParentElement as _, Pixels, - Render, Stateful, Styled as _, WeakEntity, Window, div, prelude::FluentBuilder as _, px, + Render, Stateful, Styled as _, Subscription, WeakEntity, Window, div, + prelude::FluentBuilder as _, px, }; use crate::Placement; @@ -161,6 +162,9 @@ pub struct TabGroup { active: ActiveTracker, drop_indicator: Option, renderer: Rc, + /// Nocterm: the area draws the group as a cached view, and the tab bar draws + /// every panel's title, so any panel's change draws the group again. + _panel_changes: Vec, } impl TabGroup { @@ -181,6 +185,7 @@ impl TabGroup { active: ActiveTracker::default(), drop_indicator: None, renderer: Rc::new(BareTabGroup), + _panel_changes: Vec::new(), } } @@ -349,6 +354,16 @@ impl TabGroup { } self.panels = panels; + self._panel_changes = self + .panels + .iter() + .map(|panel| { + let group = group.clone(); + panel.view().observe(cx, move |cx| { + _ = group.update(cx, |_, cx| cx.notify()); + }) + }) + .collect(); self.active_ix = active_ix.min(self.panels.len().saturating_sub(1)); self.schedule_active_sync(window, cx); cx.notify(); diff --git a/vendor/gpui-base/src/input/base/state.rs b/vendor/gpui-base/src/input/base/state.rs index b7085414..4b8651ea 100644 --- a/vendor/gpui-base/src/input/base/state.rs +++ b/vendor/gpui-base/src/input/base/state.rs @@ -1273,6 +1273,13 @@ impl InputBaseState { &self.text } + /// Synchronous document edit generation, including programmatic value + /// replacement. This is independent of deferred `InputEvent::Change`. + /// The existing counter wraps at `u64::MAX`; it is not an unbounded clock. + pub fn document_revision(&self) -> u64 { + self.document_revision + } + /// Return the (0-based) [`Position`] of the cursor. pub fn cursor_position(&self) -> Position { let offset = self.cursor(); diff --git a/vendor/gpui-pre/Cargo.toml b/vendor/gpui-pre/Cargo.toml index 8e64b0eb..a0bceb78 100644 --- a/vendor/gpui-pre/Cargo.toml +++ b/vendor/gpui-pre/Cargo.toml @@ -678,3 +678,8 @@ priority = 0 [[test]] name = "nocterm_scroll" path = "tests/nocterm_scroll.rs" + +[[test]] +name = "nocterm_list_estimates" +path = "tests/nocterm_list_estimates.rs" +required-features = ["test-support"] diff --git a/vendor/gpui-pre/NOCTERM_PATCH.md b/vendor/gpui-pre/NOCTERM_PATCH.md index a12c3d24..9516bdf4 100644 --- a/vendor/gpui-pre/NOCTERM_PATCH.md +++ b/vendor/gpui-pre/NOCTERM_PATCH.md @@ -139,3 +139,41 @@ the last old record index and its exact equality result, including false results Viewport shape changes or capacity failure release storage. Reserved-byte limits derive from these capacities and Rust type sizes, including both snapshots and the memo; storage cannot grow indefinitely as content moves between tiles. + +Nested view caching changes `src/view.rs`, `src/window.rs` and +`src/text_system/line_layout.rs`. Upstream draws every cached view nested in a cached +view that draws again, so one notified view redraws all cached views under each of its +cached ancestors. Here a cached view drawn again keeps the window's refresh state, and +the cached views nested in it are reused unless they, a view inside them or the +window's refresh asks for a draw. Each cached view records the cached views prepainted +inside it. When it is reused, its output is copied whole without visiting them, and +their recorded prepaint and paint ranges move by the distance the copy moved. A cached +view is reused only if the draw before painted it, so a range from an older frame is +never replayed. A view still draws again when it is notified, when a view inside it is +notified, when its bounds, content mask or text style change, or on a window refresh, +which focus, activation and theme changes request. Reading another entity while +rendering does not redraw a cached view, as upstream. The regression test +`nested_cached_views_survive_their_ancestors_reuse` covers reuse after a copied frame +and input on the moved output. + +Content-sized caching adds `cached_to_content` to `Entity` and `AnyView` in +`src/view.rs`. Upstream lays a cached view out from its style alone, so a view whose +size follows its content (a composer that grows with its text, a status-bar widget) +cannot be cached. A view cached to its content lays its content out, as an uncached +view does, whenever it draws again; a frame that reuses it lays it out at its style +with the size its content last took along the axes the style leaves auto. A reused +size that the content no longer fits, because the bounds changed, lays the content +out again the next frame. `AnyView::observe` calls back on the notifications of a view +held without its type, for an owner that draws part of it from a cached view of its +own. The regression test `views_cached_to_content_keep_their_contents_size` covers +the measured size, its reuse and a notified size change. + +## Virtual list height estimates + +Nocterm preserves `ListItem::size_hint()` when a new viewport width invalidates +measured rows in `src/elements/list.rs`. The rows remain `Unmeasured`, so visible +content is laid out at the actual new width; the previous size is only an estimate +for scroll extent, matching explicit `remeasure_items` behavior. Initial uniform +hints now survive the first prepaint, and native wheel jumps can reach unseen rows +without eagerly constructing the whole list. The public integration regression +`nocterm_list_estimates` covers first paint, a width change and bounded row rendering. diff --git a/vendor/gpui-pre/src/elements/list.rs b/vendor/gpui-pre/src/elements/list.rs index fa21d107..36ce9a44 100644 --- a/vendor/gpui-pre/src/elements/list.rs +++ b/vendor/gpui-pre/src/elements/list.rs @@ -1540,14 +1540,16 @@ impl Element for List { let hitbox = window.insert_hitbox(bounds, HitboxBehavior::Normal); - // If the width of the list has changed, invalidate all cached item heights + // Nocterm: width changes require remeasurement, but prior sizes remain useful + // estimates. Dropping them also erases explicit hints on the first paint and + // clamps native scrolling to the few rows already measured in the viewport. if state .last_layout_bounds .is_none_or(|last_bounds| last_bounds.size.width != bounds.size.width) { let new_items = SumTree::from_iter( state.items.iter().map(|item| ListItem::Unmeasured { - size_hint: None, + size_hint: item.size_hint(), focus_handle: item.focus_handle(), }), (), diff --git a/vendor/gpui-pre/src/text_system/line_layout.rs b/vendor/gpui-pre/src/text_system/line_layout.rs index f0bf1211..4fd987f5 100644 --- a/vendor/gpui-pre/src/text_system/line_layout.rs +++ b/vendor/gpui-pre/src/text_system/line_layout.rs @@ -493,6 +493,33 @@ pub(crate) struct LineLayoutIndex { wrapped_lines_by_hash_index: usize, } +impl LineLayoutIndex { + /// Nocterm: this index moved by the distance from `from` to `to`, for a range copied + /// as part of an enclosing reused range. + pub(crate) fn shifted(&self, from: &Self, to: &Self) -> Self { + let shift = |index: usize, from: usize, to: usize| index - from + to; + Self { + font_generation: to.font_generation, + lines_index: shift(self.lines_index, from.lines_index, to.lines_index), + wrapped_lines_index: shift( + self.wrapped_lines_index, + from.wrapped_lines_index, + to.wrapped_lines_index, + ), + lines_by_hash_index: shift( + self.lines_by_hash_index, + from.lines_by_hash_index, + to.lines_by_hash_index, + ), + wrapped_lines_by_hash_index: shift( + self.wrapped_lines_by_hash_index, + from.wrapped_lines_by_hash_index, + to.wrapped_lines_by_hash_index, + ), + } + } +} + impl LineLayoutCache { pub fn new( platform_text_system: Arc, diff --git a/vendor/gpui-pre/src/view.rs b/vendor/gpui-pre/src/view.rs index f24a4da9..f453c3c0 100644 --- a/vendor/gpui-pre/src/view.rs +++ b/vendor/gpui-pre/src/view.rs @@ -45,6 +45,35 @@ impl AnyView { ViewElement::new(self).cached(style) } + /// Nocterm: [`Self::cached`] for a view sized by its content along the axes `style` + /// leaves auto. A notified view lays its content out; a frame that reuses it keeps + /// the size its content last laid out at. + pub fn cached_to_content(self, style: StyleRefinement) -> ViewElement { + ViewElement::new(self).cached_to_content(style) + } + + /// Nocterm: calls `on_notify` whenever the view's entity notifies, while it lives, + /// for an owner that draws something of the view without its type, such as a + /// title, from a cached view of its own. + pub fn observe( + &self, + cx: &mut App, + mut on_notify: impl FnMut(&mut App) + 'static, + ) -> crate::Subscription { + let entity = self.entity.downgrade(); + cx.new_observer( + self.entity.entity_id(), + Box::new(move |cx| { + if entity.upgrade().is_some() { + on_notify(cx); + true + } else { + false + } + }), + ) + } + /// Convert this to a weak handle. pub fn downgrade(&self) -> AnyWeakView { AnyWeakView { @@ -242,6 +271,14 @@ impl Entity { pub fn cached(self, style: StyleRefinement) -> ViewElement> { ViewElement::new(self).cached(style) } + + /// Nocterm: [`Self::cached`] for a view sized by its content along the axes `style` + /// leaves auto. A notified view lays its content out; a frame that reuses it keeps + /// the size its content last laid out at. + #[track_caller] + pub fn cached_to_content(self, style: StyleRefinement) -> ViewElement> { + ViewElement::new(self).cached_to_content(style) + } } /// The element type for [`View`] implementations. Wraps a `View` and hooks it @@ -251,6 +288,8 @@ pub struct ViewElement { view: Option, entity_id: Option, cached_style: Option, + /// Nocterm: the cached view is sized by its content along the auto axes of its style. + to_content: bool, #[cfg(debug_assertions)] source: &'static core::panic::Location<'static>, } @@ -263,6 +302,7 @@ impl ViewElement { ViewElement { entity_id, cached_style: None, + to_content: false, view: Some(view), #[cfg(debug_assertions)] source: core::panic::Location::caller(), @@ -282,6 +322,31 @@ impl ViewElement { self.cached_style = Some(style); self } + + /// Nocterm: [`Self::cached`], sized by the content along the auto axes of `style`. + pub(crate) fn cached_to_content(mut self, style: StyleRefinement) -> Self { + self.cached_style = Some(style); + self.to_content = true; + self + } + + /// Nocterm: the axes a view cached to its content takes from its content. + fn content_axes(&self) -> Option> { + let style = self.cached_style.as_ref().filter(|_| self.to_content)?; + Some(Size { + width: style.size.width.is_none(), + height: style.size.height.is_none(), + }) + } +} + +/// The layout state of a [`ViewElement`]. +#[doc(hidden)] +pub struct ViewLayout { + element: Option, + /// Nocterm: set when a view cached to its content laid its content out this frame, + /// with the entities its render read. + fresh: Option>, } impl IntoElement for ViewElement { @@ -293,6 +358,16 @@ impl IntoElement for ViewElement { } struct ViewElementState { + /// Nocterm: a view cached to its content was drawn at a size its content does not + /// fit, so the next frame lays its content out again. + misfit: bool, + /// Nocterm: the draw that last painted this view. Its ranges index the frame + /// drawn last only if that draw painted it; a reused ancestor copies a nested + /// view's output without visiting it, which leaves its ranges stale. + painted_in: u64, + /// Nocterm: the cached views prepainted inside this one, at any depth. Reusing this + /// view copies their output without visiting them, so their ranges move with it. + nested: Vec, prepaint_range: Range, paint_range: Range, cache_key: ViewElementCacheKey, @@ -306,7 +381,7 @@ struct ViewElementCacheKey { } impl Element for ViewElement { - type RequestLayoutState = Option; + type RequestLayoutState = ViewLayout; type PrepaintState = Option; fn id(&self) -> Option { @@ -323,7 +398,7 @@ impl Element for ViewElement { fn request_layout( &mut self, - _id: Option<&GlobalElementId>, + id: Option<&GlobalElementId>, _inspector_id: Option<&InspectorElementId>, window: &mut Window, cx: &mut App, @@ -333,20 +408,30 @@ impl Element for ViewElement { let view = &mut self.view; request_layout_view( entity_id, + id, self.cached_style.as_ref(), + self.to_content, window, cx, &mut |window, cx| view.take().unwrap().render(window, cx).into_any_element(), ) } else { // Stateless path: isolate subtree via type name (no entity identity). - request_layout_component(type_name::(), window, cx, &mut |window, cx| { - self.view - .take() - .unwrap() - .render(window, cx) - .into_any_element() - }) + let (layout_id, element) = + request_layout_component(type_name::(), window, cx, &mut |window, cx| { + self.view + .take() + .unwrap() + .render(window, cx) + .into_any_element() + }); + ( + layout_id, + ViewLayout { + element, + fresh: None, + }, + ) } } @@ -355,17 +440,18 @@ impl Element for ViewElement { global_id: Option<&GlobalElementId>, _inspector_id: Option<&InspectorElementId>, bounds: Bounds, - element: &mut Self::RequestLayoutState, + layout: &mut Self::RequestLayoutState, window: &mut Window, cx: &mut App, ) -> Option { + let content_axes = self.content_axes(); if let Some(entity_id) = self.entity_id { // Stateful path. prepaint_view( entity_id, global_id, bounds, - element, + (&mut layout.element, layout.fresh.take(), content_axes), window, cx, &mut |window, cx| { @@ -378,7 +464,7 @@ impl Element for ViewElement { ) } else { // Stateless path: just prepaint the element. - prepaint_component(type_name::(), element, window, cx) + prepaint_component(type_name::(), &mut layout.element, window, cx) } } @@ -421,29 +507,91 @@ impl Render for EmptyView { #[inline(never)] fn request_layout_view( entity_id: EntityId, + global_id: Option<&GlobalElementId>, cached_style: Option<&StyleRefinement>, + to_content: bool, window: &mut Window, cx: &mut App, render: &mut dyn FnMut(&mut Window, &mut App) -> AnyElement, -) -> (LayoutId, Option) { +) -> (LayoutId, ViewLayout) { window.with_rendered_view(entity_id, |window| { let caching_disabled = window.is_inspector_picking(cx); match cached_style { Some(style) if !caching_disabled => { let mut root_style = Style::default(); root_style.refine(style); + if to_content { + match reusable_size(entity_id, global_id, window) { + // Reused: the auto axes keep the size the content last took. + Some(size) => { + if style.size.width.is_none() { + root_style.size.width = size.width.into(); + } + if style.size.height.is_none() { + root_style.size.height = size.height.into(); + } + } + // Drawn again: the content lays itself out, as an uncached + // view's does, and its size is what later frames keep. + None => { + let (mut element, read) = + cx.detect_accessed_entities(|cx| render(window, cx)); + let layout_id = element.request_layout(window, cx); + return ( + layout_id, + ViewLayout { + element: Some(element), + fresh: Some(read), + }, + ); + } + } + } let layout_id = window.request_layout(root_style, None, cx); - (layout_id, None) + ( + layout_id, + ViewLayout { + element: None, + fresh: None, + }, + ) } _ => { let mut element = render(window, cx); let layout_id = element.request_layout(window, cx); - (layout_id, Some(element)) + ( + layout_id, + ViewLayout { + element: Some(element), + fresh: None, + }, + ) } } }) } +/// Nocterm: the size a view cached to its content keeps in a frame that can reuse it: +/// the last draw painted it at a size its content fit, nothing notified it since and +/// the window is not refreshing. +fn reusable_size( + entity_id: EntityId, + global_id: Option<&GlobalElementId>, + window: &mut Window, +) -> Option> { + if window.dirty_views.contains(&entity_id) || window.refreshing { + return None; + } + let generation = window.draw_generation; + let mut size = None; + window.update_unvisited_element_state(global_id?, |state: &mut ViewElementState| { + if state.painted_in + 1 == generation && !state.misfit { + size = Some(state.cache_key.bounds.size); + } + }); + size +} + #[inline(never)] fn request_layout_component( name: &'static str, @@ -463,16 +611,26 @@ fn prepaint_view( entity_id: EntityId, global_id: Option<&GlobalElementId>, bounds: Bounds, - element: &mut Option, + (element, fresh, content_axes): ( + &mut Option, + Option>, + Option>, + ), window: &mut Window, cx: &mut App, render: &mut dyn FnMut(&mut Window, &mut App) -> AnyElement, ) -> Option { window.set_view_id(entity_id); window.with_rendered_view(entity_id, |window| { - if let Some(mut element) = element.take() { - element.prepaint(window, cx); - return Some(element); + if let Some(element) = element.take() { + return match fresh { + Some(read) => Some(prepaint_laid_out(element, read, global_id, bounds, window, cx)), + None => { + let mut element = element; + element.prepaint(window, cx); + Some(element) + } + }; } window.with_element_state::( @@ -487,32 +645,74 @@ fn prepaint_view( && element_state.cache_key.text_style == text_style && !window.dirty_views.contains(&entity_id) && !window.refreshing + && element_state.painted_in + 1 == window.draw_generation { let prepaint_start = window.prepaint_index(); window.reuse_prepaint(element_state.prepaint_range.clone()); cx.entities .extend_accessed(&element_state.accessed_entities); let prepaint_end = window.prepaint_index(); - element_state.prepaint_range = prepaint_start..prepaint_end; + let previous = mem::replace( + &mut element_state.prepaint_range, + prepaint_start..prepaint_end, + ); + for nested in &element_state.nested { + window.update_unvisited_element_state( + nested, + |state: &mut ViewElementState| { + state.prepaint_range = state + .prepaint_range + .start + .shifted(&previous.start, &element_state.prepaint_range.start) + ..state.prepaint_range.end.shifted( + &previous.start, + &element_state.prepaint_range.start, + ); + }, + ); + } + report_nested(window, global_id.unwrap(), &element_state.nested); return (None, element_state); } - let refreshing = mem::replace(&mut window.refreshing, true); + // Nocterm: a cached view drawn again keeps the window's refresh state, so + // the cached views nested in it are reused unless they or what they read + // changed. Forcing them to draw (upstream) made one notified leaf redraw + // every cached view under each of its cached ancestors. let prepaint_start = window.prepaint_index(); + window.cached_view_scopes.push(Vec::new()); let (element, accessed_entities) = cx.detect_accessed_entities(|cx| { let mut element = render(window, cx); - element.layout_as_root(Size::::from(bounds.size), window, cx); + let size = element.layout_as_root( + Size::::from(bounds.size), + window, + cx, + ); element.prepaint_at(bounds.origin, window, cx); - element + (element, size) }); + let (element, size) = element; + // Nocterm: a view cached to its content drawn at a kept size its content + // no longer fits lays its content out again next frame. + let misfit = content_axes.is_some_and(|axes| { + (axes.width && size.width != bounds.size.width) + || (axes.height && size.height != bounds.size.height) + }); + if misfit { + window.request_animation_frame(); + } + let nested = window.cached_view_scopes.pop().unwrap_or_default(); + report_nested(window, global_id.unwrap(), &nested); let prepaint_end = window.prepaint_index(); - window.refreshing = refreshing; ( Some(element), ViewElementState { + misfit, + painted_in: 0, + nested, accessed_entities, prepaint_range: prepaint_start..prepaint_end, paint_range: PaintIndex::default()..PaintIndex::default(), @@ -528,6 +728,57 @@ fn prepaint_view( }) } +/// Nocterm: prepaints a view cached to its content that laid its content out this +/// frame, recording what the next frames reuse. +fn prepaint_laid_out( + mut element: AnyElement, + read: FxHashSet, + global_id: Option<&GlobalElementId>, + bounds: Bounds, + window: &mut Window, + cx: &mut App, +) -> AnyElement { + let global_id = global_id.unwrap(); + window.with_element_state::(global_id, |_, window| { + let content_mask = window.content_mask(); + let text_style = window.text_style(); + let prepaint_start = window.prepaint_index(); + window.cached_view_scopes.push(Vec::new()); + let ((), mut accessed_entities) = cx.detect_accessed_entities(|cx| { + element.prepaint(window, cx); + }); + accessed_entities.extend(read); + let nested = window.cached_view_scopes.pop().unwrap_or_default(); + report_nested(window, global_id, &nested); + let prepaint_end = window.prepaint_index(); + ( + element, + ViewElementState { + misfit: false, + painted_in: 0, + nested, + accessed_entities, + prepaint_range: prepaint_start..prepaint_end, + paint_range: PaintIndex::default()..PaintIndex::default(), + cache_key: ViewElementCacheKey { + bounds, + content_mask, + text_style, + }, + }, + ) + }) +} + +/// Nocterm: records a prepainted cached view and those nested in it in the enclosing +/// cached view's scope, if any. +fn report_nested(window: &mut Window, global_id: &GlobalElementId, nested: &[GlobalElementId]) { + if let Some(scope) = window.cached_view_scopes.last_mut() { + scope.push(global_id.clone()); + scope.extend(nested.iter().cloned()); + } +} + #[inline(never)] fn prepaint_component( name: &'static str, @@ -560,16 +811,36 @@ fn paint_view( let paint_start = window.paint_index(); + let reused = element.is_none(); if let Some(element) = element { - let refreshing = mem::replace(&mut window.refreshing, true); element.paint(window, cx); - window.refreshing = refreshing; } else { window.reuse_paint(element_state.paint_range.clone()); } let paint_end = window.paint_index(); - element_state.paint_range = paint_start..paint_end; + let previous = + mem::replace(&mut element_state.paint_range, paint_start..paint_end); + element_state.painted_in = window.draw_generation; + if reused { + let generation = window.draw_generation; + for nested in &element_state.nested { + window.update_unvisited_element_state( + nested, + |state: &mut ViewElementState| { + state.paint_range = state + .paint_range + .start + .shifted(&previous.start, &element_state.paint_range.start) + ..state.paint_range.end.shifted( + &previous.start, + &element_state.paint_range.start, + ); + state.painted_in = generation; + }, + ); + } + } ((), element_state) }, diff --git a/vendor/gpui-pre/src/window.rs b/vendor/gpui-pre/src/window.rs index 17e971b2..cb40bd27 100644 --- a/vendor/gpui-pre/src/window.rs +++ b/vendor/gpui-pre/src/window.rs @@ -1026,6 +1026,90 @@ pub(crate) struct PaintIndex { line_layout_index: LineLayoutIndex, } +/// Nocterm: moves a nested index by the distance its enclosing reused range moved. +fn shift_index(index: usize, from: usize, to: usize) -> usize { + index - from + to +} + +impl PrepaintStateIndex { + /// Nocterm: this index moved with an enclosing range copied from `from` to `to`. + pub(crate) fn shifted(&self, from: &Self, to: &Self) -> Self { + Self { + hitboxes_index: shift_index( + self.hitboxes_index, + from.hitboxes_index, + to.hitboxes_index, + ), + tooltips_index: shift_index( + self.tooltips_index, + from.tooltips_index, + to.tooltips_index, + ), + deferred_draws_index: shift_index( + self.deferred_draws_index, + from.deferred_draws_index, + to.deferred_draws_index, + ), + dispatch_tree_index: shift_index( + self.dispatch_tree_index, + from.dispatch_tree_index, + to.dispatch_tree_index, + ), + accessed_element_states_index: shift_index( + self.accessed_element_states_index, + from.accessed_element_states_index, + to.accessed_element_states_index, + ), + line_layout_index: self + .line_layout_index + .shifted(&from.line_layout_index, &to.line_layout_index), + } + } +} + +impl PaintIndex { + /// Nocterm: this index moved with an enclosing range copied from `from` to `to`. + pub(crate) fn shifted(&self, from: &Self, to: &Self) -> Self { + Self { + scene_index: shift_index(self.scene_index, from.scene_index, to.scene_index), + #[cfg(any(test, feature = "test-support"))] + debug_bounds_index: shift_index( + self.debug_bounds_index, + from.debug_bounds_index, + to.debug_bounds_index, + ), + mouse_listeners_index: shift_index( + self.mouse_listeners_index, + from.mouse_listeners_index, + to.mouse_listeners_index, + ), + input_handlers_index: shift_index( + self.input_handlers_index, + from.input_handlers_index, + to.input_handlers_index, + ), + cursor_styles_index: shift_index( + self.cursor_styles_index, + from.cursor_styles_index, + to.cursor_styles_index, + ), + accessed_element_states_index: shift_index( + self.accessed_element_states_index, + from.accessed_element_states_index, + to.accessed_element_states_index, + ), + tab_handle_index: shift_index( + self.tab_handle_index, + from.tab_handle_index, + to.tab_handle_index, + ), + line_layout_index: self + .line_layout_index + .shifted(&from.line_layout_index, &to.line_layout_index), + } + } +} + impl Frame { #[cfg(any(test, feature = "test-support"))] pub(crate) fn record_debug_bounds(&mut self, selector: String, bounds: Bounds) { @@ -1227,6 +1311,11 @@ pub struct Window { long_press_timer: Option>, long_press_capture: Option, pub(crate) refreshing: bool, + /// Nocterm: counts draws, so a cached view can tell whether its recorded ranges + /// belong to the frame drawn last. + pub(crate) draw_generation: u64, + /// Nocterm: the cached views prepainted inside each cached view being prepainted. + pub(crate) cached_view_scopes: Vec>, pub(crate) activation_observers: SubscriberSet<(), AnyObserver>, pub(crate) focus: Option, focus_enabled: bool, @@ -2116,6 +2205,8 @@ impl Window { long_press_timer: None, long_press_capture: None, refreshing: false, + draw_generation: 0, + cached_view_scopes: Vec::new(), activation_observers: SubscriberSet::new(), focus: None, focus_enabled: true, @@ -3300,6 +3391,7 @@ impl Window { // Set up the per-App arena for element allocation during this draw. // This ensures that multiple test Apps have isolated arenas. let arena_scope = ElementArenaScope::enter(&cx.element_arena); + self.draw_generation += 1; if self.platform_window.prepare_frame() { self.refresh(); @@ -4226,6 +4318,27 @@ impl Window { ) } + /// Nocterm: changes an element state that this frame has not visited, in place, without + /// recording an access: its owner was copied as part of an enclosing reused range, + /// which already carries the access forward. + pub(crate) fn update_unvisited_element_state( + &mut self, + global_id: &GlobalElementId, + update: impl FnOnce(&mut S), + ) { + let key = (global_id.clone(), TypeId::of::()); + let state = match self.next_frame.element_states.get_mut(&key) { + Some(state) => Some(state), + None => self.rendered_frame.element_states.get_mut(&key), + }; + if let Some(state) = state + .and_then(|state| state.inner.downcast_mut::>()) + .and_then(Option::as_mut) + { + update(state); + } + } + /// Updates or initializes state for an element with the given id that lives across multiple /// frames. If an element with this ID existed in the rendered frame, its state will be passed /// to the given closure. The state returned by the closure will be stored so it can be referenced @@ -9329,4 +9442,218 @@ mod inspector_tests { }) .expect("closed inspector has no bookkeeping and no style overrides"); } + + /// Nocterm: a cached view drawn again reuses the cached views nested in it, even after + /// a frame that copied its whole output, and the reused output still takes input. + #[gpui::test] + fn nested_cached_views_survive_their_ancestors_reuse(cx: &mut TestAppContext) { + use crate::{AnyView, App, Entity, InputEvent as _, StyleRefinement, hsla}; + + struct Probe { + renders: Rc>, + clicks: Rc>, + // Each draw adds a child, so the probe's output moves what follows it. + grows: bool, + } + impl Render for Probe { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + self.renders.set(self.renders.get() + 1); + let clicks = self.clicks.clone(); + let children = if self.grows { self.renders.get() } else { 0 }; + div() + .id("probe") + .w(px(40.)) + .h(px(20.)) + .on_mouse_down(MouseButton::Left, move |_, _, _| { + clicks.set(clicks.get() + 1) + }) + .children((0..children).map(|_| div().size(px(1.)).bg(hsla(0., 0., 0., 1.)))) + } + } + struct Middle { + nested: Entity, + sibling: Entity, + } + impl Render for Middle { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + let fixed = StyleRefinement::default().w(px(40.)).h(px(20.)); + div() + .flex() + .flex_col() + .child(self.sibling.clone()) + .child(AnyView::from(self.nested.clone()).cached(fixed)) + } + } + struct Top { + leading: Entity, + middle: Entity, + } + impl Render for Top { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + let fixed = StyleRefinement::default().w(px(40.)).h(px(40.)); + div() + .flex() + .flex_col() + .child(self.leading.clone()) + .child(AnyView::from(self.middle.clone()).cached(fixed)) + } + } + + let nested_renders = Rc::new(Cell::new(0)); + let clicks = Rc::new(Cell::new(0)); + let probe = |renders: &Rc>, grows: bool, cx: &mut App| { + let renders = renders.clone(); + let clicks = clicks.clone(); + cx.new(move |_| Probe { + renders, + clicks, + grows, + }) + }; + let (leading, sibling, nested) = cx.update(|cx| { + ( + probe(&Rc::new(Cell::new(0)), true, cx), + probe(&Rc::new(Cell::new(0)), false, cx), + probe(&nested_renders, false, cx), + ) + }); + let window = cx.add_window({ + let (leading, sibling, nested) = (leading.clone(), sibling.clone(), nested.clone()); + move |_, cx| Top { + leading, + middle: cx.new(|_| Middle { nested, sibling }), + } + }); + let draw = |cx: &mut TestAppContext| { + cx.update_window(window.into(), |_, window, cx| window.draw(cx).clear(cx)) + .unwrap(); + }; + let notify = |probe: &Entity, cx: &mut TestAppContext| { + cx.update(|cx| probe.update(cx, |_, cx| cx.notify())); + }; + draw(cx); + let first = nested_renders.get(); + assert_eq!(first, 1); + + // The sibling's change draws the middle view again; its nested view is reused. + notify(&sibling, cx); + draw(cx); + assert_eq!(nested_renders.get(), first); + // The leading view grows and moves the middle view's output, which is copied whole. + notify(&leading, cx); + draw(cx); + notify(&leading, cx); + draw(cx); + // Drawing the middle view again still reuses the nested view at its new place. + notify(&sibling, cx); + draw(cx); + assert_eq!(nested_renders.get(), first); + + // The reused nested view still receives input where it is drawn. + cx.update_window(window.into(), |_, window, cx| { + let bounds = window + .rendered_frame + .hitboxes + .last() + .map(|hitbox| hitbox.bounds) + .expect("the nested probe registers a hitbox last"); + window.dispatch_event( + MouseDownEvent { + position: bounds.center(), + button: MouseButton::Left, + modifiers: Default::default(), + click_count: 1, + first_mouse: false, + } + .to_platform_input(), + cx, + ); + }) + .unwrap(); + assert_eq!(clicks.get(), 1); + } + + /// Nocterm: a view cached to its content takes its content's size, keeps it while + /// reused, and takes its new size once notified. + #[gpui::test] + fn views_cached_to_content_keep_their_contents_size(cx: &mut TestAppContext) { + use crate::{AnyView, Entity, StyleRefinement}; + + struct Lines { + lines: usize, + renders: Rc>, + } + impl Render for Lines { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + self.renders.set(self.renders.get() + 1); + div() + .flex() + .flex_col() + .w_full() + .children((0..self.lines).map(|_| div().h(px(10.)))) + } + } + struct Column { + lines: Entity, + } + impl Render for Column { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + div() + .flex() + .flex_col() + .w(px(100.)) + .h(px(200.)) + .child(div().h(px(20.))) + .child( + AnyView::from(self.lines.clone()) + .cached_to_content(StyleRefinement::default().w_full()), + ) + .child( + div() + .id("after") + .h(px(5.)) + .on_mouse_down(MouseButton::Left, |_, _, _| {}), + ) + } + } + + let renders = Rc::new(Cell::new(0)); + let lines = cx.update(|cx| { + let renders = renders.clone(); + cx.new(move |_| Lines { lines: 2, renders }) + }); + let window = cx.add_window({ + let lines = lines.clone(); + move |_, _| Column { lines } + }); + let column = window.root(cx).unwrap(); + let after = |cx: &mut TestAppContext| { + cx.update_window(window.into(), |_, window, cx| { + window.draw(cx).clear(cx); + let hitbox = window.rendered_frame.hitboxes.last().unwrap(); + hitbox.bounds.origin.y + }) + .unwrap() + }; + assert_eq!(after(cx), px(40.)); + assert_eq!(renders.get(), 1); + + // The column's own change reuses the view at the size its content took. + cx.update(|cx| column.update(cx, |_, cx| cx.notify())); + assert_eq!(after(cx), px(40.)); + assert_eq!(renders.get(), 1); + + // The notified view lays out its new content. + cx.update(|cx| { + lines.update(cx, |lines, cx| { + lines.lines = 4; + cx.notify(); + }) + }); + assert_eq!(after(cx), px(60.)); + assert_eq!(renders.get(), 2); + cx.update(|cx| column.update(cx, |_, cx| cx.notify())); + assert_eq!(after(cx), px(60.)); + assert_eq!(renders.get(), 2); + } } diff --git a/vendor/gpui-pre/tests/nocterm_list_estimates.rs b/vendor/gpui-pre/tests/nocterm_list_estimates.rs new file mode 100644 index 00000000..e068252f --- /dev/null +++ b/vendor/gpui-pre/tests/nocterm_list_estimates.rs @@ -0,0 +1,82 @@ +// SPDX-License-Identifier: Apache-2.0 +// Nocterm regression: native wheel extent retains estimates through layout invalidation. +use gpui::{ + AppContext, Context, Entity, IntoElement, ListAlignment, ListState, MouseMoveEvent, Render, + ScrollDelta, ScrollWheelEvent, Styled, TestAppContext, VisualTestContext, Window, div, list, + point, px, size, +}; +use std::{cell::RefCell, rc::Rc}; + +struct Rows { + state: ListState, + rendered: Rc>>, +} +impl Render for Rows { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + let rendered = self.rendered.clone(); + list(self.state.clone(), move |index, _, _| { + rendered.borrow_mut().push(index); + div().h(px(20.)).w_full().into_any_element() + }) + .size_full() + } +} +fn paint( + cx: &mut VisualTestContext, + view: &Entity, + rendered: &Rc>>, + width: f32, +) { + rendered.borrow_mut().clear(); + cx.draw(point(px(0.), px(0.)), size(px(width), px(100.)), |_, _| { + view.clone().into_any_element() + }); +} +fn wheel(cx: &mut VisualTestContext, vertical: f32) { + let position = point(px(5.), px(5.)); + cx.simulate_event(MouseMoveEvent { + position, + pressed_button: None, + modifiers: Default::default(), + }); + cx.simulate_event(ScrollWheelEvent { + position, + delta: ScrollDelta::Pixels(point(px(0.), px(vertical))), + ..Default::default() + }); +} + +#[gpui::test] +fn native_wheel_reaches_unseen_rows_after_first_paint_and_width_change(cx: &mut TestAppContext) { + let cx = cx.add_empty_window(); + let rendered = Rc::new(RefCell::new(Vec::new())); + let state = ListState::new(256, ListAlignment::Top, px(0.)).with_uniform_item_height(px(20.)); + let observed = rendered.clone(); + let view = cx.update(|_, cx| { + cx.new(|_| Rows { + state, + rendered: observed, + }) + }); + for width in [120., 240.] { + paint(cx, &view, &rendered, width); + assert!(rendered.borrow().contains(&0)); + assert!(!rendered.borrow().contains(&255)); + assert!( + rendered.borrow().len() < 16, + "only viewport rows are rendered" + ); + wheel(cx, -100_000.); + paint(cx, &view, &rendered, width); + assert!( + rendered.borrow().contains(&255), + "native wheel reaches the final row at width {width}: {:?}", + rendered.borrow() + ); + assert!( + rendered.borrow().len() < 16, + "the end is reached without eager measurement" + ); + wheel(cx, 100_000.); + } +} diff --git a/vendor/patches/gpui-base/0002-input-document-revision.patch b/vendor/patches/gpui-base/0002-input-document-revision.patch new file mode 100644 index 00000000..d0b53cba --- /dev/null +++ b/vendor/patches/gpui-base/0002-input-document-revision.patch @@ -0,0 +1,30 @@ +diff --git a/NOCTERM.md b/NOCTERM.md +index f61dab6786ae15b61da67754fedf46bf0e20df61..b642cc79be30baceee8a973bc2cfd73cab6d7909 100644 +GIT binary patch +delta 399 +zcmX|-p-u!r5JU~?625?hlt98Mf&fL(5Cj6-%oICAZhJP}JrsgKlvajssQB8s&>6v)ab +z`J(A&hk+5aH6E=ECgUhk6K2_T!Z>Xt@z5c8KZ~Zr9yn9+UZ}V$q61e;5DqcI`!pHd +znb&wK3+69cYF&edcazB>fqr#)eSLrXe1p`~`$%&{kyt62;UODdw23CMu)pr9X;B=z +z2VSaXMESdebH(G24oQ=q*#ZDwBm>l=rKtTtWaq5Zz;A;I&5QXuNMOf&;?4LC>9 +PsL7Fv9O=OIH|FCXxKWek + +delta 11 +ScmbO&H%)qi%I1}fMZ5qR-2?jo + +diff --git a/src/input/base/state.rs b/src/input/base/state.rs +index b7085414869596bb0cee74c7c22e839ef02c5916..4b8651eafa77b8aa22d73c111768dca226ce362d 100644 +GIT binary patch +delta 310 +zcmX}ny-EW?7=>ZtPm?PM*c=;C(4+||RuDuiEUZjn=T2t6-5uQhW|^H?f0`f`djS?U +z-hiD}!rVsi2JG#pltl=f`gu9;tI^N*=;JpmuO3(y*0Avnl##w7b|9r4a$>T=X~T!m +zEK9F6=cuj8t*9#Dv_vaPPeTBhOeU~YmqJo6j4>~CgMO^2CRRObGAPQ(T2e?axc2UR +z*%G@XIV%L`l#bWdydcoMZd~7##5Dd=?<5`h{ +zERDSV_iDdD#_M-CmRj9t!~1&;4a@8RMFeX$VE5%|weuW97sH^7VWW#-vx{Nt +JD~9b){sSg^bYK7g + +delta 42 +zcmV+_0M-BE)flPM7_jZSlm4UzlhD)!gE72^F}wkXF}wnYF}wqZF}wt~F}ww_ZsIi* +AL;wH) + +# End of generated Nocterm binary patch. diff --git a/vendor/patches/gpui-base/0003-cached-root-content.patch b/vendor/patches/gpui-base/0003-cached-root-content.patch new file mode 100644 index 00000000..3c33247e --- /dev/null +++ b/vendor/patches/gpui-base/0003-cached-root-content.patch @@ -0,0 +1,26 @@ +diff --git a/NOCTERM.md b/NOCTERM.md +index b642cc79be30baceee8a973bc2cfd73cab6d7909..e41d8ebcb0046b89995873cf6975198bb643641d 100644 +GIT binary patch +delta 256 +zcmXw!I}XA?3`F}h8~_PTM-$~V93WQKWGzLuv^H5%aR*v1!4XkDlztj}o5Ex(#e1Ye)e)IdMU&O5V=xWYz${!r4#Ge~=CpwpZXhI~$;K9*nFCfDwUE0E6k-a| +z?V`shU64@THLD6xGAH5xJng2tx`>k7wnpq)h9cZ@`bxFK3aqJ45 +R+BIMbI$lEIdtuLA^8+=STg(6e + +delta 7 +OcmbQFFk5beG(P|enF2-t + +diff --git a/src/root.rs b/src/root.rs +index 16ea0565a9db319f3176f829b4d4e9f232e48b93..933e8d7430d101cf33c93c222ef695465d4d8661 100644 +GIT binary patch +delta 182 +zcmYk#F$%&k6adgo(M24r>mL+b2c@+1x}z6-qP +z<>OwRXzLnU(u_mjfL7X}5s(OsSAi}GR5ZY(5+@kY3@StR>}X_Qhmd_Vln)6sMaIZ- +z3Sz%V7_7W`%+tEq%%lz!aatAIqx=PB$M=9Iv_4{wk(b>!3`KWOMNja74(Z^!d$wMnQ;{{MW!4hviUuj!$do(uB;9(wLW;8p!UxI! +z{{O+>)9>TMwdqH3U$5mklQMN`Jw~O?z~np<4VYB&0x}RBInN{3z*`HGqIilEBSc8# +z+i2TZcD^ixWFVKq%2+7!d@x8(OHS?Xag7|3XGQW1slV!Gh*HI%6=5K?uKDnc0$I*% +z@os&irFpJ)`#Mb&VuxG0M8mo2HJuE`y0R6|Yg3{a8B-!mqod6F|ea-T**q61L?ipmGy$>do3xXnOlVD +Q<~)WtA;i5niELf-52PHMDF6Tf + +delta 7 +Ocmdn4JV{}LkN^M+cmiJl + +diff --git a/src/dock/dock_area.rs b/src/dock/dock_area.rs +index f1c17c7e5d4cce5a39a31ffbbbd1fed2faeb93a7..7ff4c7839c01916899f0101559bfee02b74db57b 100644 +GIT binary patch +delta 559 +zcmYk3ze^lJ6vr76S&k5tL&%RJuhERKS +zfi$VY6kF+O{sM_bs?;_q@+W9(X^}eYk6iM~d2hb&`}w|$pWg3Z-o=}_{tm`M;SiPe +zVb>NCBVYzXA9k|?!&5P`LMy)m0I-Mba8p@f3m^7`igfN@=5zg}2GcPvga&0y%P*8Xd=l%l*$YC|(m~@Q7&DonDakvs>`o6CRwf)w{6A?!;|-w +z1=xhy#@+@nRm@;o&X!-tvtZiKO#o81`0na_(d|#)x}B5d^2aOkz@2T*m)>i#RK5?% +zy=Ot=iPi`Gci~d4;aRol(f8gOuChTTb*`Y=j?uYy+!8Ti|5|QyXp74 +IDd~Uu1JD4xga7~l + +delta 98 +zcmV-o0G12(U^5Eo2~Jb0A+VAX9Z>b7OL8aCB*JZY&^IWnpVYZggpMc`P7T +zX>Me1cPt7ZARx1d0zMJ5@)Vzolc1y!lTgbDlP{r4lSp?DlRcs#laOr$gD|4EFrooQ +Eh9P_on9|_5GlrBP+O`qe4tK669ngFA&UI7}+Vlc(mTh`~ +z5Mv&pO^X6wpjIwg^a?>ZEh=hx&iT&w^IyNkpWoxtX)IOmyFOQ5S9?;>BxWR50U&&g +z0r`q`&u(s5-c +zT*;;5yNl?5c-s~@C=_Y7mJ9R7ZFc=PLpOU>`sk&oez!@lg#(&iEz!hX-YQAgM-^a! +z4^8OE0AWn`jzQI8Htm?ehhyNwl91ZF7*UU9NW5%NZI-0+JVA%^Z89p!@Tq+I@Cji! +BiG~0G + +delta 41 +zcmV+^0M`G_>;sYL1F*0GlfD5Mv)TbB6|=(^D+7~h9u%`G8<`f94?8G}1;juJgi-B;`{|+~b0zXT-z-swjy;-i +zJza5%p$5VDVi0ZG0lx_s)L3nWbPNMA{i%@)Nyd>qG>nOo)K?rq>S5Prm>lYqJC2C_ +V`!2S_|JxR=`oIZd&-wg5`2gLXQa=Cy + +delta 197 +zcmY+8F$%&!5Jj=IF`ezlw86?VSXz03Av?(qgc;Z!v!wD69>P|z@C@Ec60xxSkN5fi +zUBBwbadSIW+iMUStZ$Gbv%}I_&83r;TudPfj|>|*4K!h(i4#Jil0iL*qKHSk_`*c{ +z#coZV)%Q3@{)P2lDY8Q5H5>?|aDix+z%!JL?*$IcY?y*jM1vJ1?h@zt+*SA02j2ci +ANdN!< + +diff --git a/src/root.rs b/src/root.rs +index 933e8d7430d101cf33c93c222ef695465d4d8661..16ea0565a9db319f3176f829b4d4e9f232e48b93 100644 +GIT binary patch +delta 47 +zcmaEq(VxB{SZDG&9d@DO)SNWEvdq+Sz2uzyyi^TM&B+UOR43mx;F~NZ#mL+b2c@+1x}z6-qP +z<>OwRXzLnU(u_mjfL7X}5s(OsSAi}GR5ZY(5+@kY3@StR>}X_Qhmd_Vln)6sMaIZ- +z3Sz%V7_7W`%+tEq%%lz!aatAIqx=PB$M=9Iv_4{wk(Z2@*kjpeg0pNVdPC3rTFD +z2`MsSG8yEx>dZAWP1BKAcu~wTa0XL9A`KAJ#v9}Siao2PtlfrBocYkLHh8*NCGo_3 +zQ`pF;jlWZ59y03)H8O6C3m)HdSLwekiGeaB^Q;^&$BDJ%TU*fHzeJV5E^DFBs;+LV)|@Njr^?!?aDUP{-{sCH8(H${ +n*t1h1?2JC$Iq$rI2ICJ7^bT5?QRAISbOc>!}L2Zdq|VZLuN-#0UPd7gWJn|q9%yAvAVYq(tk0BE&Z&}P2G +zyl;byhyY@;eRhQd=#D@}7y=?BqCSR@vp#tG_CPZ5B=edOG7bnhqCE^g8xS1i%1FQf +zaS1F0L;+JjW`Z2k9&kJqI7o^t^L#Mu)_jg6u0u#cL?o0rs8t1y!=~}Ip?YS3WNE0r +zoXvCd5)NT(`nF*x$PLaaQlKkLI8LYAOD5R`$qtOWK3YB9ih6an<|VUK*=_6_hnjB0 +ziV4M{W1JF;zl>zRvGUwXDJjl9vZK}1eS1w+r^^eP)H}_Hf+i%(pn7mCjem4f&+e!b +zArW;{dAhohNzTZvC+agY%5%5X$5BzS)54CO{K4$jE%oh|bnlC~QN|Qg`3$eroB8w; +L6HT5joX^W2VeHPj + +delta 14 +WcmX?eh-uF*rVTlcn`>MzL<0ae#Rmld + +diff --git a/src/view.rs b/src/view.rs +index f24a4da9b7906e1c1f8dfd110f75fd6ef9c5aad8..139da1049f459cb0dba7d11a7117373efa0e0348 100644 +GIT binary patch +delta 2696 +zcma)8&u<$=6qeg0O-*SPQb>QK;XxGFZq071R*`Vyrb&R>94gUBy&z-_yU*DX_RMN# +zHf}_v6p1S+cn(|ui6a+Oumlnn68`~z16L#toH#{7GqY=Zt+t9KA3QVfz3=_{K7aRh +zVeoU|$&V}hzVE|59w-#u8mI^%6r=|#A__W0Du@YX3PVs4m9R^(=Rs3R5QOcZ1j<5u +zXwBM!bg>8kU_2Tp%sJq!vj?>8Tw3RuOR8?cJ^_J=#1IGzP%17U;4wuB1Q<$%;n1fE +zepv!fRGcX2Q5Eq-!7i0lQMLnARiPKrAc78(U4v}|Q!3Iy&w5IOwTCEt~|;Y +zuEKSw*P%yQ$a|h?MQaBc3Zf`ygJ0_Hf_{8=>U=D4m-0kH9lF@9)da>J5@5NgKigWE +zhEg|C`r`VfV^w>gzuheAUpFg-ivFwd+H#wV6dX_nC3L7-QW1roL#?54Q9r&i^M;qi +zAyL>$m~A44t=w}LuKE6nkm*+a}B-8{lAT_L7U5 +zb+2Ws33W3K3LSXsZ{|$?$@tZbR79xplM6DUZG~aEloiUeZ>mtzi|dQC?pI~pWZ9f4 +zoIv6s3%$(Q@VSBX3QYRWT_4Lu8r}8%Y1akgLmzJ0a@msw{dDzZedx_@35>Z=E!T;% +z8-*(D*b!>c&`VfPkhol_KD&37Tg9UO*E_4(`o{h^bCB_kN!Oq35X#^a#8_I2aM3PH +z5ZD$dBi9Hs?siqiKfZ4*a$D#yo3Ww_h`{}fITF@EX+!J*7tk}@r6Sw|fyB|r_{aAl +zhzxs$9=yjz;MNtpptz%_auQ1=kaR21B_WzM2gVg`Qw$Bav_YU*p60Vq<1UK5aomuw +z5CtF!BH(RHN@-RZ;Ym+_wXvlCu3tA>vFy3)mSK&S40w#vixU}@A7D_cG3=Eq{k4U` +z?K|5AeSGcgjkGMoy?&NCkIR*`wJk1My12S{?dXHxBIqwS?&yE+&gnCo;h7Pu)Zm(% +zz!}$|Y)$n_X>RcO=1+yZ!Uznw2o1YuJBMLb5X+N<8Li`7iVw88Hq|gE#gh$Jw*sbRl=R2t)p# +zBz!38tIZ21tg?)>!~}U0yi86;aGtLveSdmx(s>SqQH`-+`O9NqW!=Oz-%L^6e?m6| +mqu!dvn^&ecsrvN&DK|Uj?6=j^MuYS>>+^koYH-;6zVJUXo}uXg + +delta 332 +zcmdnIiE-X^#trLKH#e(GN^E{*AIdn{HJE>Lha)eOg6-r5Ub0NqT$34H#V6;x+D~#| +zF)d0>D@rZS$jnPuuvN%S&9$;BN-fAqOitBM%PlQYD9_AG$uHMKsM1j=DJq>TU?DM? +z%iU^ogL^n5BiG~)_Og?C9QEZeR4dpjAhb?i;aR>p&Fc{3nt61?+`25-kz;e%TZd6-hm`Ux3e?v-cC9< +zv&_7EduQ*mQZTK4z}5*qhZ1Q&8NOf%AL^fI@PnA3C}>24h@iwM1}R1jRzuADzB7BX +z+p|%FPIB$O@4WN8zvuVHeISluyT0Yjfk&!7F~QTFIl`YeGpUjTe%UI~0ym)Ye{)dv +ztxpjRN}fC47|GwUSC%^A#J{5h$bcPL5 +zK;4}99o#py1K-{+^>j3R#=V-<7kv7AN_*L&MR$?-2ugCei|v?cn(Cx((?maZezlU*)+v$#wX%7-CL!h7M%F>eaW^iQX-y5%!29ZHDS4rqctzR +z(L>#JrsKQ3npRQ+9w9;ixtJO_Gi=9heB+=rjMI-LWtnY+FCUb)^PxmtF<4{b)T{(8 +zs2RJ#LthyR0NZd>N%krnZlJGT#fM4iHU`}>Qy-s_hVaE#q~0Fea!A3}m)vS7RDAsx +za^Hv8A+j^)<}O*I!6vi)QykRS}^wLoxPVi-8S6QCMm(!&TPPKN^+nm$YssV$Soqq#$+iyL@+onMC(QNpUFO}YPYTU`9WO|w#JJ)Oa7e_UhHkZUX)#5MNl;pE2P;A+Quzys&8uHevWlV_N{3) +z9K`LtkMe(WR_a^F!w#w!3uWCgZM<4Zj&yS-o98yNtP)=}@PCRbe5)dFcrr*reW>Uj1-I1F0>oOCwZp2FEu##&?GfEv0z>yai}?KH#4b>B +zy_)XD&!N#ER4b-VNula62RbP+3!%UG^l>SR^PiKh!~9+Hh%}SNPyQe|h%ZgaTQjx3j(xTC^!KQS{osiyJlx01u%VkVc-ICI#aJZPrC$GX&y3~H+ +zk(1JiM@}ZsKXOuj<-L02fE(&*4r8V_2lP4Jazw8w&Za4<8tp1rR0Fa=9MDUYcn#Zn +zs5h>^um@yqY_0qO*CyarX{Aav&>fQ>d?*w#4L-GO8>oJiupYC*eEYh5#kknC5Q`hX +z;8(o`Yk?GKnOI(db;~|=J-@&W;`jmO+J}!NJL|{xI!kw3q^7~%N=1Wx4z=i#22;%V +zz6Ih?CgFkxGjz!&he^qDcsOfN_HBJ-cZ&PPE}wc;gTl1u7NfQ~x;)~T#Pf4-*qHbz +zvF!=oeosznDX2$l0Kl*<J$+c+qHNw6cLnzrl&$!kP0Fposb@Rz +z!Xc@x*U$Kbik2nx5bx|)uEJAFLhcjJKwe}jKEl&KlZWusi9|a-{}Wl+?GXp^V3ouY +zjscE|L1-^CN2r>H6(F`x;24a-%5F|LjvrUHnY*C5oN>%8uaNA7!j>mz6=`pKv5Z +zvJ=LG`~NbCXRc2S<2N2qx+XdI9&yr{dA*`87nZ@a7Swcxy0wvz?OL+@lx=Y*)<1m+F{f<~Fe9joQ>ku`a&WQL{InoyD +zW(0M!Zlxtj+eBxTs+E)`;^sNwjU|Gn$p&$~?UB*chE!=$G-zSuq=Sp^0-iXY=-t5GQAe@oEqS0r1L{?Y1_o~Xv((>o +zFQq|AxBbS=Yr+ScX4JufAK~6N*o(j +zHn#KHl80s7^Pn<>w>&Oy!~8Sy6*%#%yc4HW2^sg4m3~h8HH>t}rB-Q)J;52u!Kf&s +zVV1(l2bB?KW<*n$8E(ERBN{f_SwZYELomU +zAs;#s37F+*{B9r*)%kr5#?!I-oXx^)nMAVK;fI|uFQlT65p;<+7T)CS=8cJ9Urbwm +z*pP@f0d|FeN5j0vPNp{omvN~Yi};SE4Az^KBWj8!yoo&$A`O)+HWt;9HASMHvqB%k +z+bH*1*h5=>)LYfcu1y47-fTp88-dcqZU{Gq=09DZ&B867USn?uEg+#NT1U~u@jgrt +zA7@ynA?Y>o_HkLFV=&`9moLwABlm|yhb?9tMFi0x7W)dzTTS-z>akC*j9j*zlflF% +z+P{D`#Z|&)qI5-QxS> +zGKl}Nb5Blv+bmC9p!_&HBmrbG^ij4M-V|!Li9wSDE};V2 +zMe8WaMRJB-rb$-K>cb4)5jvUAq8wo(8HSi7tG6-hjIUOLphmXnLrW2>cLUq|qN5PR +z&~ALVs_?Ye+%u<~$=JPAPVjm|mh`lO0+z7fDRM9;tR>cw17A488(gGSK?C{l5rjU{ +z%)8&n+oGI8-3w$cWVuv^Qsgu?GBSqO7A8`IiJUUHA&cnBx~ntMiRr=$T1-;F$cwVu +zfo5Q~jr@`^2qp;wNA%vbM9Up5nyfNfS%On^BZYxox!X+#2cRPLGe^w3fv>5(G;{Sb +zA&-yJD#r^q=xCZ0Ep?|2a@-K}z3<4p1cmg+2oxhvE_XB)Na^i%<`TWcgkIKg@BL2s +zOwrP9pQmg7cj(fA7P5v4Uq?>-hsdfvnP4?EsfAkqH~004`CQw`KHk5A)~O*4I=OJv +dBC(j6bcyV8tIF&)6VD%outz+O50r1J;xFODN&El+ + +delta 7 +OcmcZ^c+YD?oGJhiO9O@g + +diff --git a/src/view.rs b/src/view.rs +index 139da1049f459cb0dba7d11a7117373efa0e0348..f453c3c0b2f95a7194c272862602505872323b59 100644 +GIT binary patch +delta 5046 +zcmcgwT}&L;6&5zecv*stjg4)>9&8Ld_IMWLKfJIQY_R!p!5I3ZMC~%{-epJ3&TMDy +zvfw~Ad9JEP94W0TMU_>lYSc6va~0>fAdsJJ{Ii +zOFKeh=gysT&;8E%zH{zWAJl#J&$^$jG*o8n3};Ru39l~BGc%V=X7o&+Wf#8=Iokmp +zRt;7L*SN>Bu(S>acOhe2oLL-n)3%m@=Nag0%!NglubXT!0sxRmB*0|)Dsw?Eaa)5? +zuxxJR43?D!O?}-J#wxxwyTk`Uhn%Aq81TH#!C@tr(JWYDtmp=YX+?5qhcl+`axirx +zONKz_nwVZJEkVu#xurC>)81-vmzg;gT)vJ!>Tbs;ol)#==&S6QLAz0emCZ=7c7Rq! +zH%%87Z7VG{w!R4EJhRv;bA*3{TCWSt;s#&$_wK4t&Ko8<4yLinTs5#Ou&!8Omn~sI +z=a6;uvJ0+VU_5VF%V6gM%TP9Wp7u%M;Oj-Ef?LYuL3cq9Omc&pOog0d7eMzQkl@=n +z@uFR}cA#XpExF8DWpQDGt{#F3M=o^C(p{m%A=qycj6%_bo7R^~Jl;c(MvFxS&Oxus +zb#7$j))Y8D3>in~ESa1yEx8%TD00KL@OVo%{;i=YHW;jxS<@_5PTSH4M>>J211Is} +zf%*<<&cCkPYssX=%JG7|$^d_L>)6Xi&I4O36_*`7%i@Y=xM_kfuHgN?W6{9%cxDa% +zJbG*&cSQs?}nkE^I3(g7N%nGUu%^UaF5Meev46g-r +zF&UTmLQ1%(c^W4%*WI+ka&bk=875~=d?URfXVo^UTnqMCfq9|{EfiXTuCkHk`J@*} +z(p*i95VI`XVsT|BFZ0Y;&hs7f<~Z2IE5sYD`FFc$KR>H_H+X$Ye<(&5#`uNbU6 +zZn6TiIDQ;E)plFXkytSKAc^+}k5|Y_Kv$P>p~)|fjVKwRsEG2x^9IFBFR+?IljMqc +z3r#mfVRFG?R+c&7Fg>e9dI_?e!`yt*58lnS3Ej=lGd?sfO2K4ycv#&!^I_$M2~*Zx +zA^#TW9Q0?4UDjqU6_tP^IfJX<+QL7Jt#Pl6?xrE2FYAVNSY}_raC3%F$?CgjZtRT^ +z+7jXF=kd4e@H^To)qk8#*5RWIJvgYIh!P?EJi^(&c|`T+F-pg{3|`vEs(x4$NC_Wt +z^!zf8^w;ADeeds81|zsV*?FcmJ;-YRsyRW@jnjR{>Fs6@+Zo$pBp}l<{G-~0e`t*2 +zqW11SVGmmpul15Qo*W2L1sn+zO&P>D6TiJ8iGCZ$3JI!KL_7!v(cST#le +z#BF;;!(aCuZx(_;lN3k|ix`abH&T(6`m&o8h*@u4kK&UnO)u9b5cVPAu(%zk>Pz;v +z#*fm!&YfnTR*bOSm@p-0_=f%Y&swgR3)BxL5-{fIL?_C935s7Xe2y?PN8DFM6JL9u)rVQ>cXvLYXH$mJ^p=QMBrK&Z^Snjx*C1nU}fQSD2-&4ZX9e3B=O;7H;&vm +zc*vuTh^budjfGSAc>1s~yFGStYy9Zecq{#h<)V0TU>JWf*%tAa%BuL@`L?GQJ2=s_ +zKPlNrFh#@kr1|)G`V_u3@Pq%(R9#X!++Y7ZLkBQ@HHz`u&6VH?@Io0C#|AK{0)&ng +z8Qqdc3ov+66dtlB@}F96DC&mA83p(`>YFp>xk~aY+VbFBV{mW{E5j%|0<)5tlp~f+ +zCmI2fVJ&Nsob)1~HrCC?gkm(#w4HvY{@*dynCS)`fRO`CUhBO88ynsdTFrK-hC7Ux +z97}{qR{PBe{&c>-jRcaCP$Y25dya{PvrU1jTvPG>!r6-8rp}q>!ICyCI0yZZO2IO- +zn4?oW4?R85C2F=ND@w=(y1Zqis0eFEC?1I#=+-lKb0<|*$jnsE~0r4Jd9?hw1 +zv~31Jg?hXVMQ-7Lv2|QB>zezz_Nk9K#=ItOkc-B38U7!*V{|75iydm3sF&>P2k#K2R6>Ac%=DIH)&X)R*jUBkkarksUH0N<}i-kx)>GxEA@DV631`f8pra)L84P5<=ZA%`o`4p +z7LBcOW@Tj&9gN_kiMx1pG8R>#$6_vLSsMIwWN1%3g4g=GaJ{Pu|2^5>;^hSrZvB2p82(nwM`p)Ha5?wF=9VhlGSEBnKpYav>bMGk&ApB +zRtkTBB;NxmwcNyE9CuR6iGxT2WK}3eaxN^yGFt7wO?}6H`LX5k`kgposnO5xZ +zjo5d2@y;vp;Ao=F?wV3e4;wKm>Tu}K(4LVF0(Q0(Kckhn_PG_Qg$;3iExNK~WT(Wm +zV34CV(}cqKE=~d(v}fvMSvkwHY}SBBkB$cY(h_LJNgVpU_;5BN6`Cx0q`0f+>M761SM + +diff --git a/src/window.rs b/src/window.rs +index 13eb5b74aa7e59f82ba92ec5dd8925c9c7b9d70c..cb40bd272a448cf74ffd9bddc5b65fd6e18786f1 100644 +GIT binary patch +delta 1394 +zcma)6-)kII6lOAWCfOgYX^fjqOv|Zh%#1Fxo6rg-2D*q(6-*UWC}Eh)+|3Tzo!Rc( +z?d~R<+QLE~N+r0`3&ud?(%>dsrmdHi1Ua8VmVVlVcceN^ +zpN2I3@PY(X=>pJ$4`E<3V5Nc~z;vT*RkW7bLdU(L=|)kjG_>iQq3b&BT$0mte@|9B +zgYN>}{|r>^Rlig#d;hO0YVDOsi?jukYLp%63f08w5qi#`ooOi@6J#^J-%y`Q3#UO< +zqXYW+7L1YKg?{=oDUYV}Uh{32)n{i5RlltL(*s@X!YTUYj5P4H@z^>vy0r=8U>0hn +z5-qf)QOe$sj??*H73FX$s=yB=jtLmb-T_do0%vsnCX9=`exd1{P(Bi6ZQOz9k}WfX +z;oHKsb$WC`9trWEqKB8{*zJy-h?f}0D)SsxrE9+_DcbMC2@s>FH&hAf{3BVRt9N0F +z9^8e&(Sxn`Ha(L$C=TfIyp*QO=b#YYlvFY&U?M4Sa9(6n_YY;_SZ_g5D5t}C+KzTM +zk9F5`{JK%~eXiLJofdbMkyzMe73Z-<`EAJ2#yvRKSvaZC{>8q2`sRH&LmMY0MJ>8~ +z$!~P#u8zl+{00k*k`<8kfjrj9eXFFRQ)9`mc}`&XRd?C-j3(uGl}Rd}l4G=*P!g25 +z2Z>*#hGR|GT4gto&Tb5^mypt$a*-BXUV@IKSSRyD*d#Rm!7AG1EAji +jT7mzJ>80M7hW<6C49@6tPuk=m#F7M3lnNo$yDxd3%?2%7)^ + +# End of generated Nocterm binary patch. diff --git a/vendor/patches/gpui-pre/0004-list-width-estimates.patch b/vendor/patches/gpui-pre/0004-list-width-estimates.patch new file mode 100644 index 00000000..ae154d71 --- /dev/null +++ b/vendor/patches/gpui-pre/0004-list-width-estimates.patch @@ -0,0 +1,73 @@ +diff --git a/Cargo.toml b/Cargo.toml +index 8e64b0eb8891d776f3572a07aab5396b82c839a3..a0bceb780611729a7e996ae3e7e9450809212820 100644 +GIT binary patch +delta 110 +zcmbQ2GA(t3oYCYhLPC?@8gj(tWEPjirxurF<|dY;7AtWTB$i|-*eWQMq!yPH>*wVs +zm!uZu#-pmzD=JpvDoQOZ%`8ey(M?NDEGaEYEmp8qhz4uZEiNr6$S*2Uisb?TEqp0H + +delta 7 +OcmbQ1Ixl5|oDl#GqXPZ_ + +diff --git a/NOCTERM_PATCH.md b/NOCTERM_PATCH.md +index 7aff71ad44614b08dd18bcc4787b87cdcaeda2a6..9516bdf4f6700bd97398b098d865c5b9b0eccd20 100644 +GIT binary patch +delta 622 +zcmX|;3=r{^Q@v-}n2=aCe6nR&OCAvpEv+oE&7%B{ +z%%u4Zv5L92=$*3L-%`!AH+zk9X{XE)EKaiqKXdJ}vyvHC8AiF5ee5M1KR>~USXWZ} +cfW494%8e<=Ogs3gHkq!~LP;i4miaLJ2ZSo$bxy`wUyZo7aEGM7S)$4UEjFUe_C#BUWY6^2!B))GYF%o+wZ8sI{2zRZa +zNfHxOo=41+1hmRk>!#Qy&%tWq$ykqZkz&(Gn8RxJAg2+WNLj&;jdxOrp_X8Z7&Z(l +zNor_Ff`LN$Ut2ixjUtfA14D}xog3BhZ^<1}>=fh@Q~8AL`4Td!hnp^&EpAr_S$}!< +aHe9`AyTf6X?e*^`JAHmWTkQXSf2{!kOlCX) + +delta 97 +zcmV-n0G|J$=LYBH2Cxe0lV79f&c5WV|XOuIlJ0gB>yH$5;Pi=yqJKwA_^(JoLF1S^ef0u(8bBU@?W|GpSX +zvXwaL0y`I*A~}5T&G1PlCvfxm?LWsKbb&VD@%s`j=}My)@VZgO3Xd20e0FjIpIkxc +zYw)=A7=m_IfK{Z|2%8n60qh7Zfk#r>2EahNR)hg))LJ4>2wGk9;wy)#&&r3^mkK(CEn+Wqi`6k!G*T^sPD +zTjRTRTzQSQM4!d{H-&e`yhTGQ8wTGP{;>3hZVi?he8E6dC|`AGmEos5P~WsI-6n%l +zuQM<@P=7$AEokYp9%`X07ayA~~vCf`F#V +zKnjSXk4e-DM|CD4XY&XnjFGW>UEOltEB0Y#XL{b +zd?TyYm?X`$B`1}=mDsmblJ3xL_kVNNb=VT;x3lf6vd}1PDTz;tZqVh-?VQ} +zej2m)6Q}cj>i)6tOku3ibHZ)Y^In?h2gz?#fi-&4g)))T$cP)LaY3^&z-51t7~k1| +z`Yca}+7?|Nbln<7D3R}Y^6<(}I%Z7R3(nE7ePJnownS`VpPa@0Z0*Wue?h=otW;}A +zh_e5ar2R7H$%zh8^yaenli%Vf*@MK?kc9ZGJ2c_2V544XI$+?)^FxFYSYL?QAK!ap +zB@Fa~Tei1r8OtL*vbzV_+JI=KcbgzpMd)RveIOb8TPj=1*dMZ3DZ9jdRUbO!w1!0o +zmn@DdONn(uw{jD$a%{^-MsZyTVR4fTX4VhB;_x&bYtFl(Rlw!m6Z^%BvsaH^2SR*t +z&92q+ji!}st*)G}r6#Q93iWaoyDOSPUr;{6{S4^W(ZO~%?16dlBEAhh$F?aIVZ!&N +z=%!9pbUBl{m-kcm9U~IiHyy`#n%F_u&kuGqHXd7gEDC4~-%l6wJcGqw^ZduO-=aMg +zT?R*tHiM(_fUMs;u}KvIdU}zJD+Xsx(!6jszRi=P`B(>zal?z>?O2?hJ+&ux=op9l +z4ca7y3pjnXr_b@yS#ukOZ=5IKb_L~em*M3u?}XTBL>U~P&gXK@?zVpg{U3>Tr*Isi +z-|NNfA2E3FwKQBmpcO);tujmr6b*&Fi}l?KxD$VGUp`E9aO@7$gxpx7-Q3fFpOu?& +ip)Em&0cr_+i}(W$)e=3_s6y-Gx7dGZ|44d%fBp**ku2>1 + +literal 0 +HcmV?d00001 + +# End of generated Nocterm binary patch. diff --git a/vendor/patches/sources.json b/vendor/patches/sources.json index 5261a38a..0175e65f 100644 --- a/vendor/patches/sources.json +++ b/vendor/patches/sources.json @@ -216,7 +216,11 @@ }, "license": "Apache-2.0", "patches": [ - "vendor/patches/gpui-base/0001-nocterm.patch" + "vendor/patches/gpui-base/0001-nocterm.patch", + "vendor/patches/gpui-base/0002-input-document-revision.patch", + "vendor/patches/gpui-base/0003-cached-root-content.patch", + "vendor/patches/gpui-base/0004-cached-tab-groups.patch", + "vendor/patches/gpui-base/0005-uncached-root-content.patch" ], "omitted": [] }, @@ -250,7 +254,10 @@ }, "license": "Apache-2.0", "patches": [ - "vendor/patches/gpui-pre/0001-nocterm.patch" + "vendor/patches/gpui-pre/0001-nocterm.patch", + "vendor/patches/gpui-pre/0002-nested-view-cache.patch", + "vendor/patches/gpui-pre/0003-content-sized-cache.patch", + "vendor/patches/gpui-pre/0004-list-width-estimates.patch" ], "omitted": [] }, From c5bdb92695fde563781b48c7685d405bd7e649be Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 10 Nov 2026 13:27:23 +0000 Subject: [PATCH 2/5] feat!: hub chats, managed agents and user plugin toolbars MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Integrates 707 commits as two: vendor/ changes land in the commit before this one, as the vendor rule requires. The entries below are the changes, one per original commit. BREAKING CHANGE: hub chats run on the hub and use the agent chat UI; the device-side Cloud code, the node session transport, the session lists and the sessions panel are removed, as is the account server choice. The chat journal format is version 8. workspace::SwapSides is replaced by the Window → Layouts → Swapped sides layout, and Ctrl+E / Cmd+E reach the terminal instead of swapping the side panels. feat(projects): add folder workspaces with scoped chat history feat(projects): support container-backed workspaces fix(projects): remove flex_1 from server label docs: record project terminal contract ownership docs: describe container filesystem ownership docs(project-providers): clarify adapter contract invariants docs: record project adapter ownership feat(app): remove the swap-sides shortcut docs: record panel groups as the owner of side panels docs: record built-in panels and widgets as system plugins fix(workspace): keep focus when hiding a group that does not hold it docs: record the zoned status bar feat(window-layout): read layout presets from settings feat(workspace): switch layout presets from the window menu feat(workspace): replace the swap-sides action with a layout feat(workspace): list the placed panels for the window menu feat(app): add Layouts and Panels submenus and the plugins reference docs: record layout presets as app-wide settings feat(workspace): centre the status bar and order shrinking feat(plugin-model): declare plugin settings schemas feat(ui): render plugin settings forms from schemas feat(monitor-ui): declare the status bar metrics as plugin settings feat(app): list plugin settings in the plugins reference docs: record one plugin model with separate trust tiers feat(plugin-enablement): add the plugins settings section feat(window-layout): keep disabled plugins out of resolved layouts feat(workspace): apply plugin enablement live in every window feat(ui): let settings pages reveal an anchor feat(settings-ui): open a settings page at an anchor feat(plugins-ui): add the Plugins settings page feat(app): compose the Plugins page and plugin settings docs: record plugin enablement and the Plugins page feat(window-layout): edit layout presets on the current settings feat(ui): compose settings pages from owner sections feat(settings-ui): scroll to a section of a settings page feat(workspace): add the status bar context menu feat(layout-ui): edit the status bar and panels in Appearance feat(plugins-ui): link a plugin to the status bar editor feat(app): compose the layout editor into Appearance docs: describe the status bar and panels editor feat(core): define the wall-clock contract feat(local): read the local wall clock feat(workspace): add the clock plugin feat(app): inject the system clock docs: record the wall-clock contract feat(workspace): collapse overflowing status bar items into a menu feat(ui): anchor status bar popovers to a zone feat(workspace): anchor widget popovers to their zone feat(monitor-ui): anchor the host monitor popover to its zone docs: describe the status bar overflow menu and zone anchors feat(xtask): check plugin crate isolation feat(core): read bounded regular files without following links fix(themes-registry): open discovered files without following links feat(plugin-model): add user plugin tiers, ids and the clamp feat(plugin-enablement): keep user plugin ids out of the plugins section feat(plugin-api): add the user plugin contract and declarative runtime feat(plugin-packages): read, hash, store and remove user plugin packages feat(xtask): generate the plugin API surface docs: record the user plugin contract feat(plugin-ports): add observation and effect ports for user plugins feat(plugin-host): add the plugin host, broker and per-plugin data feat(workspace): offer the workspace kind to plugins feat(monitor-ui): offer the monitor reading to plugins feat(files): offer transfer counts to plugins feat(ui): let plugins post and read their own notices feat(app): store plugin records and data docs: record the plugin host feat(plugin-model): add the plugin set of both tiers feat(workspace): read plugins through a source of both tiers feat(plugin-host): expose installed plugins to the shell feat(xtask): let the plugin host notify its observers feat(plugin-view): draw validated plugin views in a host frame feat(ui): find the settings of user plugins through the directory feat(plugin-host): remove a plugin's data on its own feat(plugin-ports): say which capabilities a provider record provides feat(plugins-ui): add the User section behind the user feature docs(plugins): add example declarative plugins feat(app): compose user plugins into windows, menus and check command feat(layout-ui): show the host's facts for user plugin items feat(plugin-packages): read a package directory for the check command feat(xtask): generate the plugin API reference for authors docs(plugins): add the preview plugin guide and feature DoD lines feat(plugin-api): declare the wasm runtime and the code package format feat(plugin-abi): add the guest call ABI and its wire types feat(xtask): check the plugin ABI golden file and document the ABI feat(plugin-wasm): run code plugins on wasmi within resource limits feat(plugin-packages): read, store and check wasm packages feat(plugin-host): count code runtime faults toward the disable limit feat(app): compose the wasm runtime for user plugins and plugin check docs(plugins): record the code runtime decision (ADR 0091) feat(plugin-sdk): add the guest SDK, view builder and export macro feat(plugin-sdk): add testing::Host over the real ABI feat(xtask): keep guest-only crates out of the app closure docs: record the plugin SDK and developer sessions feat(plugin-host): add session-only records and data feat(plugin-packages): add the session package store feat(plugin-view): mark developer plugin frames feat(plugins-ui): offer the development plugin with a DEV consent feat(app): add nocterm plugin dev feat(plugin-sdk): add the example code plugin, its guest test and guide feat(plugin-enablement): add the user plugins switch to [plugins] feat(plugins-ui): show the allow switch and why user plugins are held feat(plugins-ui): confirm before removing a plugin or its data feat(app): hold user plugins in safe mode and until the user allows them fix(app): stop polling user widgets the bar does not draw feat(plugin-api): type the manifest keys of the surface feat(xtask): generate the manifest schema and check the plugin guide docs(plugins): record the start gate and the compatibility promise fix(plugin-enablement): keep a switched-off plugin when the list is full feat(plugin-model): mark a plugin set as held or as a developer's fix(window-layout): keep the user entries a held run cannot show fix(app): publish held and developer sets and test a safe-mode edit feat(workspace): name a developer session's panels with DEV feat(xtask): ban the wasmi auto-dispatch feature in every member feat(app): add Noct brand mark and default themes feat(window-layout): let panels stand alone and users make groups feat(workspace): lay out panel columns in the order of the bar feat(layout-ui): edit the bar in its preview by drag and menus feat(design): let the sidebar widen as far as the agent panel docs: record standalone panels and the preview editor in ADR 0094 feat(session): add Event::Restore for the screen of an attached session feat(vt): reset an emulator in place and keep its generation counting feat(terminal): restore the screen of an attached session feat(terminal): open tabs on an attached session feat(workspace): add a title-bar slot for the environment menu docs: record attached sessions in ADR 0095 and the layer notes feat(xtask): limit the hub contract and protocol SDK to hub-io feat(hubs-model): add hub addresses, trust rules and access ports feat(hub-io): reach hubs through the contract with device credentials docs: record the hub contract boundary in ADR 0096 feat(hub-io): attach terminal tabs to hub sessions feat(hubs): add the hub registry, connection status and sign-in service feat(hubs-ui): add environment menu, sign-in dialog and sessions panel feat(app): open windows in a local or hub environment docs: regenerate the action and dependency references for hubs docs: record window environments in ADR 0097 docs: fix ADR 0097 references docs: describe hub environments in the layer notes and the overview docs: add the hubs user guide and link it from the README docs: record the gated hub test in AGENTS.md and the README fix(hubs-ui): read the workspace after it created the sessions panel docs: regenerate the plugins reference for the sessions panel fix(hubs): refuse a sign-in to a server that is listed already fix(hub-io): keep one client per hub and lock off the worker fix(hubs): keep the organization chosen while a read runs fix(hubs-ui): report a server that cannot be removed feat(hubs-ui): open the selected session with Enter docs: match the hub docs to the labels of the interface fix(app): show the sessions panel first in a hub window feat(window-set): remember the environments of open windows feat(app): restore window environments after restart docs: record window restore in ADR 0098 fix(hubs-model): read window records one by one and keep unopened ones fix(window-set): keep unopened windows and a newer document fix(app): keep remembered servers that are unavailable for one run docs: correct ADR 0098 on skipped entries and the Booted field count feat(hubs-model): add an optional colour to a known server feat(hubs): rename, recolour and safely remove known servers feat(hubs-ui): manage known servers in settings feat(app): add the Servers page and Hub commands docs: describe managing servers fix(hubs-ui): make the Servers page findable by the tests of the app feat(hubs-model): remember the organization and sessions of a hub window feat(window-set): save what a hub window keeps of its server feat(hubs-model): report the id of a session that a terminal starts feat(hubs): prefer a remembered organization in a hub window feat(hubs-ui): remember and reattach the sessions of a hub window feat(app): reattach hub sessions of restored windows docs(hubs): describe remembered hub windows and their reattach fix(hubs-ui): reattach remembered sessions in their own organization fix(hubs-ui): save a server name through the shared settings field docs(hubs): describe sessions remembered with their organization fix(hubs-ui): keep the window ports private behind a constructor feat(session): route the first hop through a hub relay feat(ssh-keys): parse, normalize and fingerprint SSH keys feat(connections-model): reconcile profiles with a replica feat(session): sign in with a key kept in the vault feat(vault): bind a credential to an SSH public key feat(ssh): dial the first hop through a relay stream feat(ui): offer a Nocterm hub as a proxy feat(vault-ui): answer vault key requests and list keys by fingerprint feat(ssh): authenticate with a key kept in the vault feat(terminal): answer vault key requests from the vault only feat(connections-store): replicate profiles docs(adr): record profile replication docs(adr): supersede window environments with ADR 0099 fix(hub-io): sign in through the derived login value feat(hub-io): open relay streams feat(app): dial hub routes through the hub links docs(adr): record the hub as a route step feat(connections): choose, create and import vault keys in the editor docs(workspace): drop the environment menu slot from the UI note docs(adr): keep SSH keys in the vault and record the boundary feat(vault): report offline and unsupported operations feat(hubs-model): add the account server and the account key states feat(hub-io): keep the account keyset and an encrypted cache feat(hub-io): open the account keys at sign-in and recovery feat(hubs): choose the server that keeps the account feat(hubs-ui): sign-in steps for account keys and the account server feat(connections-io): keep the replica state in a private file feat(app): follow the hub account for the vault and profiles docs(hubs): describe account keys, recovery and the account server fix(hub-io): erase the account cache and keys of a revoked device feat(connections-model): place profiles in a space and move them feat(connections-store): place profiles in a space, roll back a refusal feat(vault): mark group credentials and refuse writes to read-only ones feat(hub-io): offer a new recovery key while one is unconfirmed fix(connections-model): read a move between scopes as a move feat(hub-io): serve group profiles and credentials of organizations fix(connections): keep vault key state in its owner and Import in view feat(connections): show organization sections and choose a space feat(hubs-ui): keep the recovery key step open until it is saved feat(app): show the account's spaces and announce a refused change docs(hubs): describe group connections and passwords feat(hubs): redraw the Servers page when account keys open or close fix(connections-store): keep the cursor behind a refused remote change fix(connections): act on a folder only within the clicked scope fix(connections): apply the chosen space as part of the save feat(connections-store): move a profile's secrets with it between spaces feat(hub-io): move credentials between the personal vault and a group feat(hub-io): confirm administrator keys before opening groups fix(connections-store): copy a secret other profiles still use fix(app): satisfy clippy after merging the hub branches fix(hubs-model): forget the account server when the server is removed fix(connections): move a saved connection only after its save fix(app): report the current revision when the vault is switched fix(connections-store): take an account's profiles off on detach docs(reference): regenerate the plugins reference fix(hub-io): refuse a too short password as wrong credentials fix(connections-store): keep a requested move through interruptions fix(connections-store): keep every profile when an account is stopped docs(hubs): say where unsent connections go when switching accounts fix(app): word a personal refusal as the event docs describe it feat(hub-io): confirm administrator keys through a narrow port feat(hubs): list and confirm the pending administrator keys of a hub fix(connections): say a saved secret is shared with an organization fix(connections): list a lost organization's connections read-only fix(hub-io): skip a deleted record when reading a group's secret feat(hubs-ui): confirm administrator keys on the Servers page docs(hubs): describe confirming administrator keys fix(connections-store): share secrets with a group only on request fix(hub-io): copy a shared secret under a new id fix(connections): share a secret when saved with the note shown fix(connections): warn beside the save before sharing a secret fix(keymap): offer the hub commands in the palette fix(hubs-ui): ask to update an outdated server, not Nocterm feat(connections-model): report a locked replica feat(connections-store): show a locked account feat(hub-io): report closed account keys as locked feat(terminal): derive an installation with other credentials feat(vault-ui): install a vault for the windows that use it feat(workspace): give the title bar an environment slot feat(hubs-model): name the environment a window shows feat(connections): install connections kept only in memory feat(connections): show the account state above the list fix(connections): keep the debt ratchets and the actions reference green feat(app): give a hub window the account's connections and vault docs(adr): show one environment in a window feat(hubs-ui): switch a window between this computer and a hub feat(app): open and switch hub windows docs: describe hub windows feat(hubs-ui): sign in again to a known server from its window fix(vault-ui): sign in again when a hub's keys are not on this device fix(agent-runtime): read provider usage only through ACP docs: centralize integrated verification policy feat: integrate cloud chat execution and desktop foundations feat(hub-io): admit persistent runner inbox commands feat(hub-io): answer fresh runner head probes feat(local): contain cloud tool child processes on Linux feat(agent-engine): install runner-bound provider credentials feat(cloud-runner): supply native timer and captured identity IDs feat(runner): supervise persistent Cloud control-plane sessions feat(agent-tools): publish admitted initial sessions feat(cloud): persist captured account conversations feat(local): integrate contained interactive shell sessions fix(hub-io): serialize source clock observations feat(hub-io): retain runner authority across token rotation feat(cloud-chat): handle captured tool approval answers feat(hub-io): restore account Cloud trust and runner catalogs feat(hub-io): import bounded public runner setup documents feat(agent): present captured conversations in the shared panel fix(agent-chat): count snapshot capacity like the retained snapshot feat(agent-tools): reuse connected terminals for allowlisted servers feat(cloud-runner): broker original-run sessions on the native runner feat(runner): compose the execution runtime for the daemon fix: resolve clippy findings in cloud-chat crates fix: satisfy test-support policy and view-field ratchets fix(agent-runtime): gate the test-only work bytes export feat(hubs-model): add the account devices port and its fake feat(hub-io): list and revoke account devices feat(hubs): offer account devices through the registry feat(hubs-ui): split the Servers page into a list and tabs feat(ssh-keys): issue agent certificates from a personal user CA feat(ssh): edit authorized_keys through the user's session feat(hubs-model): add the agent host trust and chat grant ports feat(hub-io): store the agent SSH CA and trust registry as E2E data feat(connections): announce connected sessions to an observer feat(hubs): reach the agent SSH CA through the hub service feat(app): wire the agent hosts observer and agent access ports feat(hubs-ui): add the agent access tab to the Servers page feat(ssh): sign in with a vault key and its user certificate feat(agent-engine): renew provider leases and add the broker endpoint feat(cloud-agents): add the Cloud agents model and catalog service feat(hub-io): adapt the Cloud agents ports over the hub contract feat(agent): add the Cloud tab to the new-agent menu feat(hubs-ui): add the Cloud agents tab to the Servers page feat(runner): add the managed mode with a bounded stdin boot document feat(hub-io): send publication retry horizons and retire deleted chats docs: describe Cloud agents and managed pools (ADR 0120, 0121) feat(cloud-runner): announce deleted chats to the hub for retirement fix(hub-io): keep one retry horizon per publication feat(hubs-ui): list and forget pinned pool keys feat(cloud-runner): persist the outbox retry horizon (journal v6) feat(hubs-ui): lock and unlock the account keys in Agent access feat(cloud-agents): route offline-off personal agents to the own runner feat(hub-io): show the organization name the hub sends in Cloud feat(hubs-ui): show per-model usage in the Cloud agents tab feat(cloud-runner): wake runs parked for provider access feat(runner): read chat access frames from the host feat(runner): run managed tools through the host broker feat(cloud-model): open a chat when its key is admitted feat(cloud-runner): claim a chat's writer on key admission feat(cloud-agents-model): add the Cloud chat launcher port feat(hub-io): persist and republish chat key grants feat(hub-io): create and list Cloud chats feat(agent): open Cloud chats from the Cloud menu feat(app): compose the Cloud chat launcher docs(adr): record Cloud chat creation protocol feat(hub-io): keep own-runner agent keys and carry provider grants feat(hub-io): seal a provider lease to a verified runner boot feat(hub-io): send a provider lease to a chat without a window feat(hub-io): lease a personal agent's key to the own runner feat(agent): open the chat of an online-only agent on the own runner docs(adr): lease a personal agent's key to the own runner feat(runner): read the agent policy from boot v2 feat(agent-engine): offer the model only an allowed set of tools feat(runner): narrow tools, limits and spend by the boot policy feat(cloud-runner): let the runner relax approval for contained targets feat(runner): approval never trusts the runner's contained targets docs(adr): record the managed runner agent policy fix(runner): read the control channel without the stdin buffer docs(architecture): regenerate the dependency reference docs(deps): regenerate third-party notices feat(ai): add file transfer tools to the terminal catalog feat(cloud-model): grant transfers through the execution bit feat(agent): describe file transfer approvals and tool inputs feat(agent-chat): refuse file transfer calls until execution is wired feat(workspace-ports): add the terminal files port feat(terminal): expose the connection filesystem to consumers feat(transfers): add relay requests, endpoints and owned transfer books feat(session): add pipelined remote read batches feat(files): show relayed copies in transfer activity perf(ssh): pipeline SFTP download reads feat(hub-io): journal file transfer tool calls feat(transfer-runtime): relay between remote hosts and run remote only docs(adr): record agent file transfer through the shared runtime docs: describe agent file transfers feat(agent-tools): run file transfers for headless runs feat(runner): relay agent transfers through a remote-only runtime feat(agent-runtime): supply transfer requests to chats feat(agent-chat): run agent file transfers with paired approvals feat(app): share the transfer runtime with agents fix(agent-chat): box the rejected transfer call fix(hub-io): decode transfer arguments in their own function fix(transfers): refuse empty remote path components fix(transfer-runtime): finish a pending write before cancelling fix(ai): refuse control characters in transfer paths fix(transfers): refuse relative local transfer paths fix(runner): drain transfers and sessions under one deadline fix(transfer-runtime): name the failing side of a relay fix(ai): grant transfers per terminal pair fix(agent-chat): approve a transfer card for its terminal pair feat(runner): send the agent's instructions as the managed system prompt fix(cloud-agents): one start per agent, retry a failed start fix(agent): show a locked hub account as an actionable notice feat(hub-io): read failure codes and provider kinds from the contract feat(cloud-agents): stop and remove seats, state the web panel url feat(agent): stop and remove a seat from its agent row feat(hubs-ui): ask for the instructions of a personal agent again fix(runner): report the cause chain of a failed start fix(runner): create the tool workspace on first start fix(hub-io): retry a start while the seat is still being deleted fix(runner): create the inbox cursor when a runner is initialized fix(runner): size the ingress pool for one inbox read feat(cloud-chat): allow a queued run to use the model fix(hub-io): fit the inference grant in the prompt's window fix(hub-io): never provision a seat when opening a chat fix(agent): make the Cloud tab a plain list of agents feat(core): add a one-line error cause chain for log records fix(cloud-model): name the kind of record a projection refusal rejected perf(store): notify once for an optimistic write perf(workspace): cache the tab surface so sidebar redraws reuse it perf(agent): skip redundant notifies and resolve agents once per render perf(projects): redraw panels on search text changes only perf(agent): draw the transcript and history as cached regions perf(agent): keep each message's parsed markdown per row perf(agent): cache the chat footer to its content perf(workspace): cache status bar widgets and keep tab groups current fix(agent): offer Cloud agents only in the window of their hub fix(runner): renew expired access before the boot announcement perf(containers): share host watches and filter listing refreshes fix: make agent workflows portable and proportional fix(cloud-chat): keep a chat live when its runner does not answer fix(hub-io): wake a managed chat's seat before it delivers a prompt feat(agent): list a hub's Cloud chats in the history column feat(cloud-chat): show sent prompts as pending until the runner accepts them feat(agent): draw one composer and footer for Local and Cloud chats feat(cloud-model): derive runner chat summaries from journaled facts feat(cloud-runner): journal v7 summary rows and coordinator summary publication feat(hub-io): sign, seal and publish runner chat summaries feat(runner): publish chat summaries from the delivery sweep docs: record runner chat summaries and journal v7 in ADR 0135 feat(cloud-conversation-model): add the neutral Cloud conversation model and port feat(hub-io): run Cloud chats on the contract device core docs: record the device side on the contract core in ADR 0136 fix(hub-io): coalesce lossy agent catalog hints docs: record shared bounded contract ownership feat(ssh-keys): derive the raw Ed25519 public key of a seed feat(cloud-model): admit chat SSH bindings and certificates feat(hub-io): map chat SSH grants, events and certificates for the runner feat(cloud-runner): keep chat SSH keys in journal v8 and run their transitions feat(ssh): sign a runner in with a chat's in-memory key and certificate feat(runner): bind chats to reachable servers and sign in with their certificates docs: record per-server CA principals and runner certificate sessions in ADR 0137 fix(hub-io): preserve bounded monotonic vault snapshots fix(core): reject stale settings and profile publications fix(connections-io): isolate DNS work and reuse HTTP connections fix(core): reject serialization before publication side effects perf(connections-model): index profiles during reconciliation fix(connections-store): reject unbounded replica feeds fix(connections-store): retry transient flag acquisition failures perf(agent-storage): read catalog metadata from transcripts fix(agent): enforce bounded conversation work fix(hub-io): bound account publication and retire removed sources fix(ssh): serialize cooperating authorized keys edits fix(ssh): preserve terminal admission and retry SFTP setup fix(transfer-runtime): share capacity across independent batches perf(files): publish transfer projections on native changes fix(local): prepare workspace shells on bounded native workers perf(project-container-fs): batch acknowledged upload writes fix(containers-service): subscribe before acquiring snapshots perf(monitor-service): share collection by captured executor perf(project-model): validate decoded catalogs once perf(connections): prepare visible rows on semantic changes perf(terminal): reuse bounded search navigation work perf(terminal): pause inactive cursor timers fix(keymap): publish bindings through retained persistence perf(layout-ui): bound retained layout edit history fix(themes-registry): bound scans and recover interrupted installs fix(ui): coalesce theme browser background requests fix(app): fence background SSH host preparation by source perf(xtask): reuse parsed architecture inventories fix(plugin-packages): retain immutable consented versions fix(plugin-host): retain prepared instances through publication fix(plugins-ui): await admitted management operations fix(ui): publish plugin availability transitions fix(app): retain asynchronous plugin publication workflows docs: describe bounded publication and work ownership feat(hub-io): resume a Cloud command kept across a restart feat(hub-io): wake a chat's seat with the core's WakeSeat feat(cloud-conversation-model): say a chat is Opening before its first round feat(cloud-chat): show a Cloud tool row with the core's title and typed outcome docs(adr): record the hub client's device transport and resumable commands as ADR 0138 feat(cloud-runner): seal the chat SSH row under its own journal class (journal v9) feat(runner): run prompts on bound servers and read boot v3 with the contract feat(cloud-runner): list a bound chat's servers in its summary feat(hub-io): give each trusted server its own CA principal with the contract CA feat(cloud-conversation-model): bind a Cloud chat to servers and run prompts on one feat(agent): a Servers control for Cloud chats and bound servers in history rows feat(cloud-agents): answer bound Cloud chats' certificate requests while unlocked docs(adr): record desktop binding, answering and bound-server prompts as ADR 0139 fix(runner): an unknown session target stays unsupported with or without consent feat: hub chats run on the hub and use the agent chat UI feat: agent access connects with saved credentials; profiles have one space perf(agent): show queued messages on the first frame fix(local): finish PTY sessions when the leader exits feat(hub-agents): follow agent and server changes of a hub fix(hub-io): hub chats no longer freeze the app and start on their first message feat(connections): offer the agent servers this window has not saved feat(agent): separate local and server chats in a hub window's history feat(agent): attach files of any kind with the paperclip feat(agent): attach a hub chat's servers from the attach menu fix(hub-agents): list a hub's agents and servers again after a failure fix(connections): offer unsaved agent servers once the account's list arrived feat(connections): keep a typed password in the window's vault and drop the unsaved agent servers feat(vault): saved passwords sign in to the servers they list and are managed by name feat(agent): every window lists every chat; a resting chat moves to the window that opens it feat(connections): a group's button opens every connection of the group fix(layout-ui): drops land where the pointer aims and never freeze the editor feat(vault-ui, connections): search saved passwords and add one in a dialog feat: toolbars for plugins, and requests, timers and terminals for user plugins feat(plugins-ui): install plugins from a button or the plugins folder fix(hubs-ui): build the test workspace with the hub's change feed fix: tell user plugin panels and toolbars when they are shown fix: keep the plugins folder apart from the plugin host's files feat(plugin-sdk): re-export what guests need for requests and views docs: toolbars, and requests, timers and terminals for user plugins fix: let a window's plugin terminals go with the window fix: apply plugin effects after the host's update and refresh after the workspace's feat(plugins): build plugin views from the application's own components feat(plugins-ui): list external plugins with the built-in ones, with one switch feat(plugins): arrow-left and chevron-left glyphs feat(plugins): a plugin's terminal tab can serve files to the explorer and AI chats feat(plugin-sdk): re-export Effect, so a plugin's tests can read its effects fix(plugins): a plugin terminal reports a clean prompt to AI chats feat(agent): file tools for attached terminals that run no programs feat(plugins): a plugin's terminal tab carries the plugin's bar along one edge feat(plugins-ui): a compact plugin detail with folded lists and removal in a dialog feat(plugins): a plugin's toolbar keeps to its own tabs and the user places it fix(plugins-ui): keep the detail's fold state private and its tests under the line limit feat(plugins): charts in plugin views, toolbars that know their axis, and toolbar moves that apply fix(snippets-ui): every snippet row reaches the panel's right edge fix(deps): take the security fixes of rustls, tokio and webpki-roots docs: fix the links that failed rustdoc in CI Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01WAgRyZqRCp4XuUHFDB1w3k --- .claude/agents/architect.md | 62 +- .claude/agents/implementer.md | 48 +- .claude/agents/reviewer.md | 55 +- .claude/agents/tester.md | 44 +- .claude/hooks/cargo-serial.sh | 42 +- .claude/hooks/guard_bash.py | 31 - .claude/hooks/guard_files.py | 51 +- .claude/hooks/hooklib.py | 32 +- .claude/hooks/post_edit.py | 90 +- .claude/skills/behavior-tests/SKILL.md | 15 +- .claude/skills/crate-extend/SKILL.md | 63 +- .claude/skills/crate-new/SKILL.md | 86 +- .claude/skills/crate-split/SKILL.md | 94 +- .claude/skills/debt-paydown/SKILL.md | 8 +- .claude/skills/design-review/SKILL.md | 51 +- .claude/skills/docs-adr/SKILL.md | 10 +- .claude/skills/feature/SKILL.md | 76 +- .claude/skills/feature/plan-bus.md | 81 +- .claude/skills/gate/SKILL.md | 170 +- .claude/skills/owner-extract/SKILL.md | 22 +- .claude/skills/performance/SKILL.md | 77 + .claude/skills/placement/SKILL.md | 42 +- .claude/skills/plugin-api/SKILL.md | 142 + .claude/skills/port-design/SKILL.md | 26 +- .claude/skills/refactor/SKILL.md | 65 +- .claude/skills/responsibility-map/SKILL.md | 41 +- .claude/skills/typed-errors/SKILL.md | 2 +- .claude/skills/wiring/SKILL.md | 13 +- .dockerignore | 8 + .github/workflows/ci.yml | 270 +- .github/workflows/package.yml | 20 +- .github/workflows/release.yml | 4 +- .gitignore | 23 + AGENTS.md | 273 +- CONTRIBUTING.md | 56 +- Cargo.lock | 1123 +++++++- Cargo.toml | 126 +- README.md | 212 +- THIRD_PARTY_NOTICES.txt | 2505 ++++++++++++++--- assets/icons/nocterm-256.png | Bin 9931 -> 10951 bytes assets/icons/nocterm-512.png | Bin 24072 -> 26315 bytes assets/icons/nocterm.ico | Bin 372526 -> 372526 bytes assets/icons/nocterm.svg | 4 +- assets/keymap.toml | 2 - crates/acp/src/bridge.rs | 95 +- crates/acp/src/bridge/tests.rs | 60 +- crates/acp/src/client/lifecycle_tests.rs | 1 + .../acp/src/client/restore_classification.rs | 1 + crates/acp/src/client/startup_failures.rs | 1 + crates/acp/src/client/tests.rs | 4 + crates/acp/src/lib.rs | 2 + crates/acp/src/mapping/content.rs | 4 +- crates/acp/src/mapping/info.rs | 2 + crates/acp/src/mapping/requests.rs | 1 + crates/acp/src/process.rs | 66 +- crates/agent-chat/Cargo.toml | 5 +- crates/agent-chat/src/chat_deps.rs | 238 +- crates/agent-chat/src/lib.rs | 32 +- crates/agent-chat/src/roster.rs | 360 ++- crates/agent-chat/src/roster/chat_list.rs | 3 + crates/agent-chat/src/roster/contexts.rs | 108 + crates/agent-chat/src/seat.rs | 6 + crates/agent-chat/src/thread.rs | 90 +- .../agent-chat/src/thread/approval_inbox.rs | 46 +- crates/agent-chat/src/thread/archive.rs | 48 +- .../src/thread/archive/load_operation.rs | 51 +- .../agent-chat/src/thread/archive/summary.rs | 27 + crates/agent-chat/src/thread/archive/tests.rs | 20 +- crates/agent-chat/src/thread/bound_call.rs | 15 +- crates/agent-chat/src/thread/client.rs | 167 +- crates/agent-chat/src/thread/client_port.rs | 88 +- crates/agent-chat/src/thread/composer.rs | 76 +- crates/agent-chat/src/thread/config.rs | 9 +- crates/agent-chat/src/thread/document.rs | 171 +- .../src/thread/document/metadata.rs | 23 +- .../agent-chat/src/thread/document/tests.rs | 141 +- .../src/thread/document/transcript.rs | 76 +- crates/agent-chat/src/thread/execution.rs | 58 +- .../src/thread/execution/jobs/tests.rs | 217 -- crates/agent-chat/src/thread/fork.rs | 102 + crates/agent-chat/src/thread/live.rs | 11 +- .../agent-chat/src/thread/local_payloads.rs | 84 + crates/agent-chat/src/thread/queue.rs | 188 +- crates/agent-chat/src/thread/restart.rs | 6 + crates/agent-chat/src/thread/saved.rs | 372 +-- .../agent-chat/src/thread/session_driver.rs | 6 + .../agent-chat/src/thread/session_grants.rs | 24 +- crates/agent-chat/src/thread/title.rs | 33 + .../src/thread/tool_display/tests.rs | 6 +- crates/agent-chat/src/thread/tool_runtime.rs | 178 +- .../src/thread/tool_runtime/command_jobs.rs | 51 +- .../src/thread/tool_runtime/dispatch.rs | 29 + .../src/thread/tool_runtime/file_dispatch.rs | 93 + .../src/thread/tool_runtime/presentation.rs | 42 +- .../thread/tool_runtime/source_execution.rs | 68 + .../thread/tool_runtime/transfer_dispatch.rs | 173 ++ .../src/thread/tool_runtime/transfers.rs | 174 ++ .../src/thread/tool_runtime/turn_context.rs | 48 +- crates/agent-directory/Cargo.toml | 1 + crates/agent-directory/src/lib.rs | 42 +- crates/agent-directory/src/tests.rs | 13 + crates/agent-providers/src/claude.rs | 15 +- crates/agent-providers/src/codex.rs | 7 +- crates/agent-providers/src/lib.rs | 2 +- crates/agent-runtime/Cargo.toml | 6 + crates/agent-runtime/src/bridge.rs | 8 +- crates/agent-runtime/src/catalog.rs | 436 ++- .../agent-runtime/src/catalog/allocation.rs | 66 + crates/agent-runtime/src/catalog/prepared.rs | 132 + .../agent-runtime/src/catalog/reclamation.rs | 35 + crates/agent-runtime/src/client.rs | 119 +- crates/agent-runtime/src/connections.rs | 25 +- crates/agent-runtime/src/documents.rs | 477 +++- .../src/documents/history_tests.rs | 236 +- .../src/documents/history_tests/closing.rs | 50 +- .../src/documents/history_tests/contexts.rs | 580 ++++ .../contexts/catalog_capacity.rs | 291 ++ .../documents/history_tests/environments.rs | 221 ++ .../documents/history_tests/local_launch.rs | 236 ++ .../src/documents/history_tests/ownership.rs | 225 ++ .../documents/history_tests/preferences.rs | 8 +- .../src/documents/history_tests/residency.rs | 504 ++++ .../src/documents/history_tests/usage.rs | 195 +- .../src/documents/history_tests/workspace.rs | 21 +- .../agent-runtime/src/documents/registry.rs | 373 +++ crates/agent-runtime/src/error.rs | 53 + crates/agent-runtime/src/execution.rs | 56 + crates/agent-runtime/src/handle.rs | 354 ++- crates/agent-runtime/src/history_admission.rs | 608 ++++ .../src/history_admission/eviction.rs | 65 + .../src/history_admission/local.rs | 32 + .../history_admission/local_allocations.rs | 170 ++ .../src/history_admission/testing.rs | 120 + crates/agent-runtime/src/holders.rs | 20 + crates/agent-runtime/src/lib.rs | 145 +- crates/agent-runtime/src/lifecycle.rs | 91 +- crates/agent-runtime/src/storage.rs | 118 +- crates/agent-runtime/src/storage/admitted.rs | 124 + crates/agent-runtime/src/usage.rs | 55 +- crates/agent-settings-ui/src/controls.rs | 18 +- crates/agent-settings-ui/src/lib.rs | 11 +- crates/agent-settings-ui/src/render.rs | 9 +- crates/agent-storage/Cargo.toml | 4 +- crates/agent-storage/src/catalog.rs | 140 +- crates/agent-storage/src/catalog_tests.rs | 121 - crates/agent-storage/src/chats.rs | 245 +- crates/agent-storage/src/history_tests.rs | 253 -- crates/agent-storage/src/input.rs | 87 + crates/agent-storage/src/lib.rs | 17 +- crates/agent-storage/src/loading.rs | 49 - crates/agent-storage/src/namespace.rs | 205 ++ crates/agent-storage/src/publication.rs | 31 + .../agent-storage/tests/prepared_history.rs | 136 + crates/agent-tools/Cargo.toml | 21 + .../execution => agent-tools/src}/jobs.rs | 184 +- crates/agent-tools/src/lib.rs | 20 + crates/agent-tools/tests/command_lifecycle.rs | 331 +++ crates/agent-usage/Cargo.toml | 22 - crates/agent-usage/src/claude.rs | 136 - crates/agent-usage/src/lib.rs | 51 - crates/agent-usage/src/parser.rs | 112 - crates/agent-usage/src/session_log.rs | 91 - crates/agent-usage/src/session_log/parser.rs | 28 - crates/agent-usage/src/session_log/tests.rs | 155 - crates/agent/Cargo.toml | 12 +- crates/agent/src/chat_host.rs | 71 +- crates/agent/src/hub_access.rs | 42 + crates/agent/src/image_deps.rs | 63 + crates/agent/src/installation.rs | 64 +- crates/agent/src/installation/source.rs | 41 + crates/agent/src/lib.rs | 59 +- crates/agent/src/panel.rs | 288 +- crates/agent/src/panel/account_notice.rs | 99 + crates/agent/src/panel/approvals.rs | 260 +- crates/agent/src/panel/attach.rs | 95 +- crates/agent/src/panel/attach_files.rs | 37 + crates/agent/src/panel/attachments.rs | 21 +- crates/agent/src/panel/chat.rs | 311 +- crates/agent/src/panel/chat_servers.rs | 173 ++ crates/agent/src/panel/composer.rs | 189 +- crates/agent/src/panel/composer_frame.rs | 66 + crates/agent/src/panel/composer_renderer.rs | 171 +- crates/agent/src/panel/composer_view.rs | 101 +- crates/agent/src/panel/entries.rs | 78 +- crates/agent/src/panel/entries/tool_input.rs | 80 +- .../entries/tool_output/decode/payload.rs | 49 +- .../entries/tool_output/decode/unwrap.rs | 2 +- crates/agent/src/panel/history.rs | 212 +- crates/agent/src/panel/history_column.rs | 79 +- crates/agent/src/panel/image_picker.rs | 14 - crates/agent/src/panel/image_pipeline.rs | 146 +- .../agent/src/panel/image_pipeline/tests.rs | 77 +- crates/agent/src/panel/image_preparation.rs | 9 +- crates/agent/src/panel/images.rs | 42 +- crates/agent/src/panel/layout.rs | 12 +- crates/agent/src/panel/lifecycle.rs | 214 +- crates/agent/src/panel/menu.rs | 8 +- crates/agent/src/panel/message_actions.rs | 26 +- crates/agent/src/panel/message_list.rs | 79 +- crates/agent/src/panel/message_markdown.rs | 97 + crates/agent/src/panel/notices.rs | 14 + crates/agent/src/panel/other_windows.rs | 62 + crates/agent/src/panel/parts.rs | 139 + crates/agent/src/panel/preview_operation.rs | 66 +- crates/agent/src/panel/queue.rs | 122 +- .../src/panel/queue_image_preparation.rs | 24 +- crates/agent/src/panel/shell.rs | 6 +- crates/agent/src/panel/stream.rs | 7 +- crates/agent/src/panel/tests/approvals.rs | 8 +- crates/agent/src/panel/tests/archive.rs | 230 +- .../agent/src/panel/tests/authentication.rs | 238 ++ .../agent/src/panel/tests/bridge_outcomes.rs | 7 +- crates/agent/src/panel/tests/chat_servers.rs | 63 + crates/agent/src/panel/tests/commands.rs | 2 +- crates/agent/src/panel/tests/composer.rs | 149 +- .../src/panel/tests/composer/context_usage.rs | 175 ++ .../agent/src/panel/tests/context_rosters.rs | 449 +++ crates/agent/src/panel/tests/drafts.rs | 85 +- crates/agent/src/panel/tests/drafts/saving.rs | 6 +- crates/agent/src/panel/tests/execution.rs | 74 +- .../src/panel/tests/execution/file_tools.rs | 130 + .../panel/tests/execution/file_transfers.rs | 333 +++ .../tests/execution/local_environments.rs | 243 ++ .../tests/execution/screenshot_history.rs | 20 +- crates/agent/src/panel/tests/flow.rs | 36 +- crates/agent/src/panel/tests/hermes.rs | 15 +- crates/agent/src/panel/tests/history.rs | 64 +- .../agent/src/panel/tests/history/search.rs | 72 + crates/agent/src/panel/tests/invalidation.rs | 164 +- crates/agent/src/panel/tests/labels.rs | 8 +- crates/agent/src/panel/tests/lifecycle.rs | 266 +- crates/agent/src/panel/tests/mod.rs | 443 +-- crates/agent/src/panel/tests/navigation.rs | 55 +- crates/agent/src/panel/tests/other_windows.rs | 137 + crates/agent/src/panel/tests/panel_actions.rs | 14 +- crates/agent/src/panel/tests/persistence.rs | 50 +- .../src/panel/tests/provider_fixtures.rs | 17 +- .../src/panel/tests/provider_permissions.rs | 2 +- crates/agent/src/panel/tests/queue_edit.rs | 3 +- crates/agent/src/panel/tests/queue_layout.rs | 111 +- crates/agent/src/panel/tests/queue_remove.rs | 2 +- crates/agent/src/panel/tests/queue_timing.rs | 154 + crates/agent/src/panel/tests/reconnect.rs | 47 +- crates/agent/src/panel/tests/registration.rs | 40 + crates/agent/src/panel/tests/resources.rs | 71 +- .../src/panel/tests/resources/durability.rs | 7 +- .../src/panel/tests/resources/windows.rs | 64 + crates/agent/src/panel/tests/restoration.rs | 255 +- crates/agent/src/panel/tests/routing.rs | 11 + .../agent/src/panel/tests/scripted_agent.rs | 243 ++ crates/agent/src/panel/tests/seat_binding.rs | 233 ++ crates/agent/src/panel/tests/servers.rs | 81 +- .../panel/tests/servers/background_opening.rs | 4 +- .../src/panel/tests/servers/workspaces.rs | 229 ++ .../agent/src/panel/tests/terminal_fixture.rs | 63 +- crates/agent/src/panel/tests/tool_corpus.rs | 2 +- crates/agent/src/panel/tests/tool_input.rs | 24 +- crates/agent/src/panel/tests/tool_output.rs | 2 +- .../agent/src/panel/tests/usage_dismissal.rs | 12 +- crates/agent/src/panel/widgets.rs | 12 +- crates/agent/src/plugin.rs | 39 + crates/agent/src/workspace_seat.rs | 194 +- crates/ai/Cargo.toml | 7 + crates/ai/src/agents.rs | 4 +- crates/ai/src/agents/available.rs | 32 + crates/ai/src/agents/launch.rs | 59 +- crates/ai/src/agents/profile.rs | 22 +- crates/ai/src/agents/tests.rs | 42 +- crates/ai/src/approval.rs | 53 +- crates/ai/src/connection.rs | 21 + crates/ai/src/context.rs | 24 +- crates/ai/src/execution_origin.rs | 99 + crates/ai/src/execution_origin/error.rs | 10 + crates/ai/src/history.rs | 193 +- crates/ai/src/history/budget.rs | 118 +- crates/ai/src/history/catalog.rs | 164 +- crates/ai/src/history/catalog/tests.rs | 22 +- crates/ai/src/history/catalog_header.rs | 262 ++ crates/ai/src/history/codec.rs | 59 +- crates/ai/src/history/draft_tests.rs | 2 +- crates/ai/src/history/error.rs | 22 +- crates/ai/src/history/header.rs | 124 + crates/ai/src/history/incoming.rs | 163 ++ crates/ai/src/history/prefix.rs | 46 + crates/ai/src/history/scope_tests.rs | 361 +++ .../src/history/scope_tests/environments.rs | 195 ++ crates/ai/src/history/version.rs | 43 + crates/ai/src/history/version_one_tests.rs | 28 +- crates/ai/src/history/wire.rs | 182 +- crates/ai/src/images.rs | 36 + crates/ai/src/lib.rs | 23 +- crates/ai/src/local_environment.rs | 119 + crates/ai/src/mcp.rs | 26 +- crates/ai/src/model/content.rs | 7 +- crates/ai/src/model/controls.rs | 3 + crates/ai/src/model/image_data.rs | 80 + crates/ai/src/model/session.rs | 27 + crates/ai/src/prompt_files.rs | 161 ++ crates/ai/src/session.rs | 26 + crates/ai/src/session/tests.rs | 24 + crates/ai/src/session_config.rs | 2 + crates/ai/src/session_config/tests.rs | 5 + crates/ai/src/storage.rs | 140 +- crates/ai/src/storage/prepared.rs | 88 + crates/ai/src/testing.rs | 3 - crates/ai/src/testing/storage.rs | 225 +- crates/ai/src/testing/usage.rs | 147 - crates/ai/src/tests.rs | 86 +- crates/ai/src/thread.rs | 1 + crates/ai/src/thread/live.rs | 236 ++ crates/ai/src/tool_display.rs | 14 +- crates/ai/src/tool_display/identity.rs | 34 +- crates/ai/src/tools.rs | 119 +- crates/ai/src/tools/catalog.rs | 81 +- crates/ai/src/tools/catalog/tests.rs | 131 +- crates/ai/src/tools/failure.rs | 35 + crates/ai/src/tools/files.rs | 205 ++ crates/ai/src/tools/files/tests.rs | 254 ++ crates/ai/src/tools/files/work.rs | 243 ++ crates/ai/src/tools/fixtures/calls.json | 141 + crates/ai/src/tools/fixtures/tools-list.json | 99 +- crates/ai/src/tools/output.rs | 97 + crates/ai/src/tools/output/tests.rs | 65 + crates/ai/src/tools/transfer.rs | 160 ++ crates/ai/src/tools/transfer/tests.rs | 239 ++ crates/ai/src/usage.rs | 59 +- crates/ai/src/usage/source.rs | 121 - crates/ai/tests/execution_origin.rs | 57 + crates/ai/tests/history_residency.rs | 31 + crates/connections-io/Cargo.toml | 1 + crates/connections-io/src/flags.rs | 13 +- crates/connections-io/src/http.rs | 84 +- crates/connections-io/src/http/tests.rs | 4 +- crates/connections-io/src/lib.rs | 14 +- crates/connections-io/src/os_probe.rs | 70 +- crates/connections-io/src/repository.rs | 8 +- crates/connections-io/src/repository/tests.rs | 26 + crates/connections-io/src/resolver.rs | 143 +- crates/connections-io/src/resolver/tests.rs | 82 + crates/connections-model/Cargo.toml | 1 + crates/connections-model/src/draft.rs | 40 +- crates/connections-model/src/draft/tests.rs | 32 +- crates/connections-model/src/lib.rs | 5 +- crates/connections-model/src/profiles.rs | 29 +- .../src/profiles/mutation.rs | 40 +- .../connections-model/src/profiles/tests.rs | 58 + crates/connections-model/src/replica.rs | 128 + crates/connections-model/src/replica/rules.rs | 277 ++ crates/connections-model/src/replica/state.rs | 68 + crates/connections-model/src/replica/tests.rs | 370 +++ crates/connections-model/src/test_support.rs | 2 + .../src/test_support/replica.rs | 415 +++ crates/connections-store/Cargo.toml | 6 + crates/connections-store/src/connections.rs | 38 + .../src/connections/tests.rs | 1 + .../src/connections/tests/folders.rs | 83 + crates/connections-store/src/facts.rs | 31 +- crates/connections-store/src/facts/flags.rs | 42 +- .../connections-store/src/facts/prepared.rs | 1 + crates/connections-store/src/facts/source.rs | 4 + .../src/facts/tests/flags.rs | 118 + crates/connections-store/src/lib.rs | 20 +- crates/connections-store/src/replication.rs | 240 ++ .../connections-store/src/replication/feed.rs | 145 + .../src/replication/round.rs | 425 +++ .../src/replication/status.rs | 71 + .../src/replication/tests.rs | 359 +++ .../src/replication/tests/conflicts.rs | 258 ++ .../src/replication/tests/feed.rs | 165 ++ .../src/replication/tests/lifecycle.rs | 155 + crates/connections-store/src/test_support.rs | 31 + crates/connections/Cargo.toml | 8 +- crates/connections/src/agent_access.rs | 102 + crates/connections/src/directory.rs | 18 +- crates/connections/src/directory/tests.rs | 4 +- crates/connections/src/editor.rs | 185 +- crates/connections/src/editor/agent_access.rs | 216 ++ .../src/editor/agent_access/session.rs | 112 + crates/connections/src/editor/appearance.rs | 45 +- .../connections/src/editor/authentication.rs | 426 ++- crates/connections/src/editor/password.rs | 133 + crates/connections/src/editor/render.rs | 24 +- crates/connections/src/editor/tests.rs | 30 +- .../src/editor/tests/agent_access.rs | 164 ++ .../connections/src/editor/tests/passwords.rs | 240 ++ .../src/editor/tests/vault_keys.rs | 286 ++ crates/connections/src/editor/vault_key.rs | 261 ++ crates/connections/src/facts_presentation.rs | 86 +- .../src/facts_presentation/tests.rs | 67 + crates/connections/src/installed.rs | 40 +- crates/connections/src/lib.rs | 50 +- crates/connections/src/panel.rs | 351 +-- crates/connections/src/panel/account_state.rs | 128 + crates/connections/src/panel/folder.rs | 132 + crates/connections/src/panel/group_actions.rs | 4 +- crates/connections/src/panel/row.rs | 173 +- crates/connections/src/panel/sections.rs | 228 ++ crates/connections/src/panel/tests.rs | 79 +- .../src/panel/tests/account_state.rs | 170 ++ crates/connections/src/panel/tests/agents.rs | 72 + crates/connections/src/panel/tests/list.rs | 107 + crates/connections/src/panel/tests/reorder.rs | 4 +- crates/connections/src/plugin.rs | 36 + crates/connections/src/plugin_tests.rs | 55 + crates/connections/src/prepared.rs | 137 +- crates/connections/src/probe_sources.rs | 45 +- crates/connections/src/probe_sources/tests.rs | 6 +- .../probe_sources/tests/window_capacity.rs | 2 +- crates/connections/src/saved_password.rs | 96 + crates/connections/src/session_intents.rs | 54 +- .../connections/src/session_intents/tests.rs | 114 + crates/connections/src/test_support.rs | 232 +- crates/connections/src/volatile_tests.rs | 107 + crates/containers-service/Cargo.toml | 26 + crates/containers-service/src/host.rs | 175 ++ crates/containers-service/src/lib.rs | 76 + crates/containers-service/src/test_support.rs | 145 + crates/containers-service/src/tests.rs | 232 ++ crates/containers-service/src/watch.rs | 175 ++ crates/containers-ui/Cargo.toml | 4 + crates/containers-ui/src/lib.rs | 59 +- crates/containers-ui/src/model.rs | 246 +- crates/containers-ui/src/panel.rs | 28 +- crates/containers-ui/src/panel/rows.rs | 2 +- crates/containers-ui/src/plugin.rs | 42 + crates/containers-ui/src/plugin_tests.rs | 52 + crates/containers-ui/src/tests.rs | 193 +- crates/containers/Cargo.toml | 1 + crates/containers/src/engine.rs | 74 +- crates/containers/src/engine/tests.rs | 22 +- crates/containers/src/error.rs | 4 +- crates/containers/src/events.rs | 265 ++ crates/containers/src/events/tests.rs | 170 ++ crates/containers/src/lib.rs | 20 +- crates/core/Cargo.toml | 6 +- crates/core/src/allocation.rs | 121 + crates/core/src/bounded_io.rs | 103 + crates/core/src/clock.rs | 119 + crates/core/src/clock/tests.rs | 83 + crates/core/src/error.rs | 15 + crates/core/src/error/tests.rs | 11 + crates/core/src/fs.rs | 64 +- crates/core/src/fs/bounded.rs | 144 + crates/core/src/fs/private.rs | 72 + crates/core/src/fs/tests.rs | 257 ++ crates/core/src/lib.rs | 18 +- crates/core/src/paths.rs | 5 + crates/core/src/persist.rs | 67 +- crates/core/src/persist/queue.rs | 18 +- crates/core/src/persist/revisioned.rs | 201 ++ crates/core/src/persist/revisioned/tests.rs | 110 + crates/core/src/persist/shutdown.rs | 39 +- crates/design/src/tokens.rs | 80 +- crates/design/src/tokens/format_tests.rs | 17 +- crates/design/src/tokens/layout.rs | 2 + crates/design/src/tokens/layout/codec.rs | 17 +- crates/design/src/tokens/layout/schema.rs | 3 +- crates/design/tokens/default.toml | 220 +- crates/directory-io/Cargo.toml | 17 + crates/directory-io/src/lib.rs | 94 + crates/directory-io/src/tests.rs | 95 + crates/explorer/Cargo.toml | 7 + crates/explorer/src/directory.rs | 100 + crates/explorer/src/directory/error.rs | 30 + .../src/directory/path.rs} | 36 +- crates/explorer/src/lib.rs | 10 +- crates/explorer/src/testing.rs | 89 + crates/files/Cargo.toml | 10 + crates/files/src/activity.rs | 19 +- crates/files/src/drag_ui_tests.rs | 12 +- crates/files/src/host_capabilities.rs | 61 + crates/files/src/host_capabilities/tests.rs | 126 + crates/files/src/lib.rs | 105 +- crates/files/src/listing_ui_tests.rs | 136 +- crates/files/src/local_pane.rs | 140 +- crates/files/src/open_ui_tests.rs | 13 +- crates/files/src/open_ui_tests/local.rs | 6 +- crates/files/src/operations_ui_tests.rs | 8 +- crates/files/src/path_ui_tests.rs | 6 +- crates/files/src/plugin.rs | 50 + crates/files/src/plugin_tests.rs | 85 + crates/files/src/projection.rs | 62 + crates/files/src/registration.rs | 60 +- crates/files/src/remote_pane.rs | 225 +- crates/files/src/rename_ui_tests.rs | 4 +- crates/files/src/selection.rs | 56 + crates/files/src/selection_ui_tests.rs | 93 + crates/files/src/tests.rs | 44 +- crates/files/src/transfer_ui_tests.rs | 29 +- crates/files/src/transfers.rs | 154 +- crates/hub-agents-model/Cargo.toml | 23 + crates/hub-agents-model/src/agent.rs | 110 + crates/hub-agents-model/src/error.rs | 39 + crates/hub-agents-model/src/ids.rs | 56 + crates/hub-agents-model/src/lib.rs | 41 + crates/hub-agents-model/src/ports.rs | 82 + crates/hub-agents-model/src/provider.rs | 126 + crates/hub-agents-model/src/server.rs | 107 + crates/hub-agents-model/src/testing.rs | 365 +++ crates/hub-agents-model/src/tests.rs | 104 + crates/hub-agents/Cargo.toml | 29 + crates/hub-agents/src/access.rs | 101 + crates/hub-agents/src/installed.rs | 38 + crates/hub-agents/src/lib.rs | 32 + crates/hub-agents/src/service.rs | 301 ++ crates/hub-agents/src/tests.rs | 222 ++ crates/hub-io/Cargo.toml | 49 + crates/hub-io/src/account.rs | 258 ++ .../hub-io/src/account/cache_publication.rs | 50 + crates/hub-io/src/account/flow.rs | 594 ++++ crates/hub-io/src/account/flow/tests.rs | 562 ++++ .../src/account/flow/tests/source_binding.rs | 78 + crates/hub-io/src/account/key_admission.rs | 270 ++ crates/hub-io/src/account/keyset_hub.rs | 119 + crates/hub-io/src/account/lifecycle.rs | 76 + crates/hub-io/src/account/operations.rs | 228 ++ crates/hub-io/src/account/recovery.rs | 54 + crates/hub-io/src/account/refresh.rs | 93 + crates/hub-io/src/account/slot.rs | 541 ++++ crates/hub-io/src/account/source.rs | 160 ++ .../hub-io/src/account/source/membership.rs | 110 + crates/hub-io/src/account/source/tests.rs | 269 ++ crates/hub-io/src/account/testing.rs | 155 + crates/hub-io/src/account/testing/accounts.rs | 152 + crates/hub-io/src/account/testing/hub.rs | 158 ++ .../src/account/testing/native_operations.rs | 44 + crates/hub-io/src/account/testing/subjects.rs | 74 + crates/hub-io/src/account/tests.rs | 88 + crates/hub-io/src/account_keys.rs | 105 + crates/hub-io/src/agents.rs | 220 ++ crates/hub-io/src/agents/convert.rs | 129 + crates/hub-io/src/agents/servers.rs | 137 + crates/hub-io/src/agents/tests.rs | 61 + crates/hub-io/src/cache.rs | 347 +++ crates/hub-io/src/cache/tests.rs | 106 + crates/hub-io/src/chats.rs | 28 + crates/hub-io/src/chats/connector.rs | 573 ++++ crates/hub-io/src/chats/hints.rs | 22 + crates/hub-io/src/chats/items.rs | 105 + crates/hub-io/src/chats/options.rs | 289 ++ crates/hub-io/src/chats/repository.rs | 289 ++ crates/hub-io/src/chats/repository/list.rs | 144 + crates/hub-io/src/chats/repository/tests.rs | 85 + crates/hub-io/src/chats/state.rs | 276 ++ crates/hub-io/src/chats/state/tests.rs | 159 ++ crates/hub-io/src/chats/upload.rs | 129 + crates/hub-io/src/chats/watch.rs | 247 ++ crates/hub-io/src/checked.rs | 66 + crates/hub-io/src/classify.rs | 170 ++ crates/hub-io/src/classify/tests.rs | 171 ++ crates/hub-io/src/connect.rs | 182 ++ crates/hub-io/src/connect/login.rs | 31 + crates/hub-io/src/connect/request.rs | 163 ++ crates/hub-io/src/connect/sign_out.rs | 114 + crates/hub-io/src/connect/start.rs | 110 + crates/hub-io/src/connect/tests.rs | 102 + crates/hub-io/src/convert.rs | 33 + crates/hub-io/src/convert/tests.rs | 34 + crates/hub-io/src/credentials.rs | 485 ++++ crates/hub-io/src/credentials/bound.rs | 88 + crates/hub-io/src/credentials/bound/tests.rs | 201 ++ crates/hub-io/src/credentials/publication.rs | 66 + crates/hub-io/src/credentials/seed.rs | 46 + crates/hub-io/src/credentials/tests.rs | 186 ++ crates/hub-io/src/devices.rs | 100 + crates/hub-io/src/directory.rs | 24 + crates/hub-io/src/endpoint.rs | 48 + crates/hub-io/src/hub_account.rs | 89 + crates/hub-io/src/lib.rs | 80 + crates/hub-io/src/links.rs | 276 ++ crates/hub-io/src/pool.rs | 442 +++ crates/hub-io/src/pool/captured.rs | 156 + crates/hub-io/src/pool/observation.rs | 155 + crates/hub-io/src/pool/tests.rs | 174 ++ crates/hub-io/src/relay.rs | 224 ++ crates/hub-io/src/relay/failure.rs | 74 + crates/hub-io/src/relay/failure/tests.rs | 118 + crates/hub-io/src/relay/pump.rs | 132 + crates/hub-io/src/relay/pump/tests.rs | 238 ++ crates/hub-io/src/relay/tests.rs | 51 + crates/hub-io/src/replica.rs | 219 ++ crates/hub-io/src/replica/tests.rs | 152 + .../src/replica/tests/source_binding.rs | 118 + crates/hub-io/src/replica/watch.rs | 55 + crates/hub-io/src/replica/wire.rs | 112 + crates/hub-io/src/replica/wire/tests.rs | 196 ++ crates/hub-io/src/store.rs | 210 ++ crates/hub-io/src/store/client.rs | 48 + crates/hub-io/src/store/grpc.rs | 157 ++ crates/hub-io/src/store/memory.rs | 379 +++ crates/hub-io/src/store/memory/accounts.rs | 39 + crates/hub-io/src/store/tests.rs | 125 + crates/hub-io/src/task.rs | 97 + crates/hub-io/src/vault.rs | 262 ++ crates/hub-io/src/vault/codec.rs | 98 + crates/hub-io/src/vault/codec/tests.rs | 104 + crates/hub-io/src/vault/credentials.rs | 175 ++ crates/hub-io/src/vault/credentials/tests.rs | 348 +++ crates/hub-io/src/vault/idle.rs | 82 + crates/hub-io/src/vault/ledger.rs | 368 +++ crates/hub-io/src/vault/ledger/access.rs | 113 + crates/hub-io/src/vault/ledger/tests.rs | 317 +++ crates/hub-io/src/vault/snapshot_budget.rs | 126 + crates/hub-io/src/vault/tests.rs | 135 + crates/hub-io/src/vault/tests/refresh.rs | 212 ++ .../hub-io/src/vault/tests/source_binding.rs | 230 ++ crates/hub-io/src/vault/unlock.rs | 112 + crates/hub-io/src/vault/unlock/tests.rs | 88 + crates/hub-io/tests/hub.rs | 251 ++ crates/hub-io/tests/hub/account.rs | 321 +++ crates/hub-io/tests/hub/chats.rs | 170 ++ crates/hub-io/tests/hub/revocation.rs | 129 + crates/hubs-model/Cargo.toml | 29 + crates/hubs-model/src/account_keys.rs | 47 + crates/hubs-model/src/address.rs | 182 ++ crates/hubs-model/src/address/tests.rs | 109 + crates/hubs-model/src/color.rs | 54 + crates/hubs-model/src/devices.rs | 36 + crates/hubs-model/src/environment.rs | 43 + crates/hubs-model/src/error.rs | 121 + crates/hubs-model/src/lib.rs | 66 + crates/hubs-model/src/login.rs | 41 + crates/hubs-model/src/ports.rs | 94 + crates/hubs-model/src/records.rs | 104 + crates/hubs-model/src/recovery.rs | 85 + crates/hubs-model/src/settings.rs | 267 ++ crates/hubs-model/src/settings/tests.rs | 247 ++ crates/hubs-model/src/source_identity.rs | 121 + .../hubs-model/src/source_identity/local.rs | 63 + crates/hubs-model/src/testing.rs | 674 +++++ crates/hubs-model/src/testing/account_keys.rs | 68 + crates/hubs-model/src/testing/devices.rs | 104 + crates/hubs-model/src/testing/local_source.rs | 72 + crates/hubs-model/src/testing/login.rs | 41 + crates/hubs-model/src/testing/recovery.rs | 157 ++ crates/hubs-model/src/testing/server.rs | 155 + crates/hubs-model/src/testing/tests.rs | 367 +++ crates/hubs-model/src/trust.rs | 170 ++ crates/hubs-model/src/trust/tests.rs | 111 + crates/hubs-ui/Cargo.toml | 36 + crates/hubs-ui/src/color.rs | 49 + crates/hubs-ui/src/commands.rs | 128 + crates/hubs-ui/src/commands_tests.rs | 108 + crates/hubs-ui/src/installation.rs | 45 + crates/hubs-ui/src/layout.rs | 61 + crates/hubs-ui/src/lib.rs | 86 + crates/hubs-ui/src/manage.rs | 100 + crates/hubs-ui/src/menu.rs | 400 +++ crates/hubs-ui/src/menu_tests.rs | 238 ++ crates/hubs-ui/src/messages.rs | 140 + crates/hubs-ui/src/ports.rs | 39 + crates/hubs-ui/src/problems.rs | 97 + crates/hubs-ui/src/servers.rs | 242 ++ crates/hubs-ui/src/servers/detail.rs | 198 ++ crates/hubs-ui/src/servers/list.rs | 95 + crates/hubs-ui/src/servers/tabs.rs | 9 + .../hubs-ui/src/servers/tabs/account_keys.rs | 165 ++ .../hubs-ui/src/servers/tabs/agent_access.rs | 287 ++ crates/hubs-ui/src/servers/tabs/agents.rs | 258 ++ .../hubs-ui/src/servers/tabs/agents/form.rs | 237 ++ .../src/servers/tabs/agents/providers.rs | 319 +++ crates/hubs-ui/src/servers/tabs/devices.rs | 343 +++ crates/hubs-ui/src/servers/tabs/names.rs | 104 + crates/hubs-ui/src/servers/tabs/overview.rs | 200 ++ crates/hubs-ui/src/servers/tabs/trust.rs | 70 + crates/hubs-ui/src/servers_agents_tests.rs | 123 + crates/hubs-ui/src/servers_tests.rs | 595 ++++ crates/hubs-ui/src/sign_in.rs | 786 ++++++ crates/hubs-ui/src/sign_in/form.rs | 111 + crates/hubs-ui/src/sign_in_tests.rs | 437 +++ crates/hubs-ui/src/status.rs | 59 + crates/hubs-ui/src/status_tests.rs | 31 + crates/hubs-ui/src/switch.rs | 166 ++ crates/hubs-ui/src/switch_tests.rs | 109 + crates/hubs-ui/src/test_support.rs | 261 ++ crates/hubs/Cargo.toml | 30 + crates/hubs/src/error.rs | 37 + crates/hubs/src/fixture.rs | 120 + crates/hubs/src/installed.rs | 37 + crates/hubs/src/lib.rs | 39 + crates/hubs/src/registry.rs | 454 +++ crates/hubs/src/registry/tests.rs | 351 +++ crates/hubs/src/sign_in.rs | 748 +++++ crates/hubs/src/sign_in/tests.rs | 780 +++++ .../hubs/src/sign_in/tests/source_binding.rs | 78 + crates/hubs/src/status.rs | 190 ++ crates/keymap-ui/src/render.rs | 25 +- crates/keymap-ui/src/rows.rs | 80 +- crates/keymap-ui/src/tests.rs | 38 +- crates/keymap-ui/src/view.rs | 6 + crates/keymap/Cargo.toml | 1 + crates/keymap/src/error.rs | 2 + crates/keymap/src/file.rs | 2 +- crates/keymap/src/lib.rs | 297 +- crates/keymap/src/names.rs | 2 + crates/keymap/src/tests.rs | 20 +- crates/layout-ui/Cargo.toml | 28 + crates/layout-ui/src/about.rs | 56 + crates/layout-ui/src/commands.rs | 27 + crates/layout-ui/src/dnd.rs | 293 ++ crates/layout-ui/src/drops.rs | 353 +++ crates/layout-ui/src/face.rs | 194 ++ crates/layout-ui/src/fit.rs | 213 ++ crates/layout-ui/src/history.rs | 190 ++ crates/layout-ui/src/keys.rs | 75 + crates/layout-ui/src/lib.rs | 85 + crates/layout-ui/src/menu.rs | 480 ++++ crates/layout-ui/src/parts.rs | 21 + crates/layout-ui/src/presets.rs | 359 +++ crates/layout-ui/src/preview.rs | 573 ++++ crates/layout-ui/src/rename.rs | 98 + crates/layout-ui/src/section.rs | 602 ++++ crates/layout-ui/src/selection.rs | 69 + crates/layout-ui/src/snapshot.rs | 371 +++ crates/layout-ui/src/tests.rs | 491 ++++ crates/layout-ui/src/tests/aim.rs | 154 + crates/layout-ui/src/tests/dnd.rs | 312 ++ crates/layout-ui/src/tests/editing.rs | 189 ++ crates/layout-ui/src/tests/geometry.rs | 166 ++ crates/layout-ui/src/tests/grouping.rs | 144 + crates/layout-ui/src/tests/history.rs | 126 + crates/layout-ui/src/tests/menu.rs | 167 ++ crates/layout-ui/src/tests/presets.rs | 195 ++ crates/layout-ui/src/tray.rs | 186 ++ crates/layout-ui/src/user_facts.rs | 26 + crates/local/Cargo.toml | 10 +- crates/local/src/clock.rs | 22 + crates/local/src/clock/tests.rs | 35 + crates/local/src/exec.rs | 181 +- crates/local/src/exec/admission.rs | 32 + crates/local/src/exec/process.rs | 6 +- crates/local/src/exec/tests.rs | 61 +- crates/local/src/integration.rs | 22 + crates/local/src/lib.rs | 206 +- crates/local/src/readiness.rs | 86 + crates/local/src/tests.rs | 59 +- crates/local/src/tests/leader_exit.rs | 44 + crates/localfs/src/directory.rs | 11 + crates/localfs/src/directory/portable.rs | 10 + crates/localfs/src/directory/unix.rs | 10 + crates/localfs/tests/capabilities.rs | 36 + crates/monitor-service/Cargo.toml | 23 + crates/monitor-service/src/lib.rs | 195 ++ crates/monitor-service/src/tests.rs | 136 + crates/monitor-service/src/watch.rs | 110 + crates/monitor-ui/Cargo.toml | 15 + crates/monitor-ui/src/details.rs | 90 +- crates/monitor-ui/src/host_capabilities.rs | 79 + crates/monitor-ui/src/installed.rs | 5 + crates/monitor-ui/src/lib.rs | 65 +- crates/monitor-ui/src/model.rs | 350 ++- crates/monitor-ui/src/page.rs | 7 - crates/monitor-ui/src/plugin.rs | 88 + crates/monitor-ui/src/source.rs | 118 + crates/monitor-ui/src/status.rs | 54 +- crates/monitor-ui/src/status_bar_settings.rs | 80 + crates/monitor-ui/src/tests.rs | 63 +- crates/monitor-ui/src/tests/dependencies.rs | 48 +- .../monitor-ui/src/tests/host_capabilities.rs | 183 ++ crates/monitor-ui/src/tests/sources.rs | 452 +++ .../src/tests/status_bar_settings.rs | 209 ++ crates/monitor-ui/src/tests/status_popover.rs | 56 + crates/monitor/src/container.rs | 239 ++ crates/monitor/src/container/membership.rs | 133 + crates/monitor/src/container/sampler.rs | 50 + crates/monitor/src/container/script.rs | 99 + crates/monitor/src/lib.rs | 9 +- crates/monitor/src/platform.rs | 4 + crates/monitor/src/reading.rs | 1 + crates/monitor/src/sampler.rs | 8 + crates/plugin-abi/Cargo.toml | 23 + crates/plugin-abi/abi-golden.json | 48 + crates/plugin-abi/src/codec.rs | 61 + crates/plugin-abi/src/lib.rs | 66 + crates/plugin-abi/src/names.rs | 20 + crates/plugin-abi/src/samples.rs | 598 ++++ crates/plugin-abi/src/samples/bars.rs | 62 + crates/plugin-abi/src/samples/charts.rs | 42 + crates/plugin-abi/src/samples/files.rs | 126 + crates/plugin-abi/src/tests.rs | 332 +++ crates/plugin-abi/src/wire.rs | 258 ++ crates/plugin-api/Cargo.toml | 25 + crates/plugin-api/api-surface.json | 940 +++++++ crates/plugin-api/src/base64.rs | 77 + crates/plugin-api/src/capability.rs | 48 + crates/plugin-api/src/capability/clock.rs | 38 + crates/plugin-api/src/capability/fields.rs | 57 + crates/plugin-api/src/capability/files.rs | 227 ++ crates/plugin-api/src/capability/monitor.rs | 54 + crates/plugin-api/src/capability/net.rs | 265 ++ .../src/capability/notifications.rs | 121 + crates/plugin-api/src/capability/panels.rs | 42 + crates/plugin-api/src/capability/storage.rs | 68 + crates/plugin-api/src/capability/table.rs | 365 +++ crates/plugin-api/src/capability/terminals.rs | 205 ++ crates/plugin-api/src/capability/timers.rs | 45 + crates/plugin-api/src/capability/transfers.rs | 36 + crates/plugin-api/src/capability/workspace.rs | 61 + crates/plugin-api/src/contribution.rs | 148 + crates/plugin-api/src/declarative.rs | 56 + crates/plugin-api/src/effects.rs | 213 ++ crates/plugin-api/src/error.rs | 59 + crates/plugin-api/src/grant.rs | 146 + crates/plugin-api/src/icon.rs | 288 ++ crates/plugin-api/src/identity.rs | 133 + crates/plugin-api/src/lib.rs | 96 + crates/plugin-api/src/manifest.rs | 321 +++ .../plugin-api/src/manifest/contribution.rs | 159 ++ crates/plugin-api/src/manifest/declare.rs | 161 ++ crates/plugin-api/src/manifest/network.rs | 229 ++ crates/plugin-api/src/manifest/settings.rs | 301 ++ crates/plugin-api/src/package_format.rs | 120 + crates/plugin-api/src/ports.rs | 236 ++ crates/plugin-api/src/runtimes.rs | 50 + crates/plugin-api/src/sanitize.rs | 27 + crates/plugin-api/src/snapshot.rs | 97 + crates/plugin-api/src/surface.rs | 386 +++ crates/plugin-api/src/template.rs | 243 ++ crates/plugin-api/src/template/check.rs | 171 ++ crates/plugin-api/src/template/eval.rs | 158 ++ crates/plugin-api/src/template/view.rs | 642 +++++ crates/plugin-api/src/tests.rs | 17 + crates/plugin-api/src/tests/effects.rs | 148 + crates/plugin-api/src/tests/files.rs | 104 + crates/plugin-api/src/tests/grant.rs | 109 + crates/plugin-api/src/tests/identity.rs | 35 + crates/plugin-api/src/tests/manifest.rs | 632 +++++ crates/plugin-api/src/tests/network.rs | 240 ++ crates/plugin-api/src/tests/package_format.rs | 139 + crates/plugin-api/src/tests/runtime.rs | 115 + crates/plugin-api/src/tests/sanitize.rs | 44 + crates/plugin-api/src/tests/support.rs | 106 + crates/plugin-api/src/tests/surface.rs | 579 ++++ crates/plugin-api/src/tests/table.rs | 179 ++ crates/plugin-api/src/tests/template.rs | 590 ++++ crates/plugin-api/src/tests/terminals.rs | 186 ++ crates/plugin-api/src/tests/view.rs | 363 +++ crates/plugin-api/src/version.rs | 63 + crates/plugin-api/src/view.rs | 453 +++ crates/plugin-enablement/Cargo.toml | 22 + crates/plugin-enablement/src/lib.rs | 147 + crates/plugin-enablement/src/tests.rs | 231 ++ crates/plugin-host/Cargo.toml | 31 + crates/plugin-host/src/broker.rs | 297 ++ crates/plugin-host/src/cadence.rs | 211 ++ crates/plugin-host/src/caps.rs | 19 + crates/plugin-host/src/data.rs | 179 ++ crates/plugin-host/src/deferred.rs | 282 ++ crates/plugin-host/src/error.rs | 136 + crates/plugin-host/src/faults.rs | 43 + crates/plugin-host/src/gesture.rs | 227 ++ crates/plugin-host/src/host.rs | 780 +++++ crates/plugin-host/src/inputs.rs | 52 + crates/plugin-host/src/lib.rs | 80 + crates/plugin-host/src/net.rs | 75 + crates/plugin-host/src/rate.rs | 102 + crates/plugin-host/src/records.rs | 126 + crates/plugin-host/src/registry.rs | 425 +++ crates/plugin-host/src/session.rs | 95 + crates/plugin-host/src/snapshot.rs | 96 + crates/plugin-host/src/test_support.rs | 481 ++++ crates/plugin-host/src/tests.rs | 8 + crates/plugin-host/src/tests/calls.rs | 440 +++ crates/plugin-host/src/tests/data.rs | 356 +++ crates/plugin-host/src/tests/deferred.rs | 174 ++ crates/plugin-host/src/tests/effects.rs | 459 +++ crates/plugin-host/src/tests/lifecycle.rs | 524 ++++ crates/plugin-host/src/tests/readers.rs | 109 + crates/plugin-host/src/tests/rig.rs | 213 ++ crates/plugin-host/src/tests/session_ports.rs | 84 + crates/plugin-model/Cargo.toml | 21 + crates/plugin-model/src/catalog.rs | 264 ++ crates/plugin-model/src/contribution.rs | 238 ++ crates/plugin-model/src/error.rs | 83 + crates/plugin-model/src/id.rs | 186 ++ crates/plugin-model/src/lib.rs | 63 + crates/plugin-model/src/plugin.rs | 27 + crates/plugin-model/src/schema.rs | 204 ++ crates/plugin-model/src/schema/edit.rs | 194 ++ crates/plugin-model/src/schema/edit_tests.rs | 410 +++ crates/plugin-model/src/schema/fixtures.rs | 102 + crates/plugin-model/src/schema/marks.rs | 9 + crates/plugin-model/src/schema/resolve.rs | 74 + .../plugin-model/src/schema/resolve_tests.rs | 212 ++ crates/plugin-model/src/schema/tests.rs | 357 +++ crates/plugin-model/src/schema/text.rs | 62 + crates/plugin-model/src/schema/validate.rs | 275 ++ crates/plugin-model/src/schema/values.rs | 80 + crates/plugin-model/src/set.rs | 256 ++ crates/plugin-model/src/set/tests.rs | 190 ++ crates/plugin-model/src/tests.rs | 564 ++++ crates/plugin-model/src/user.rs | 304 ++ crates/plugin-model/src/user/tests.rs | 194 ++ crates/plugin-model/src/view.rs | 196 ++ crates/plugin-net/Cargo.toml | 17 + crates/plugin-net/src/lib.rs | 156 + crates/plugin-net/src/tests.rs | 165 ++ crates/plugin-packages/Cargo.toml | 25 + crates/plugin-packages/src/directory.rs | 71 + crates/plugin-packages/src/lib.rs | 37 + crates/plugin-packages/src/parse.rs | 60 + crates/plugin-packages/src/session.rs | 73 + crates/plugin-packages/src/store.rs | 208 ++ crates/plugin-packages/src/tests.rs | 510 ++++ .../src/tests/code_packages.rs | 299 ++ .../src/tests/session_store.rs | 82 + crates/plugin-ports/Cargo.toml | 25 + crates/plugin-ports/src/lib.rs | 67 + crates/plugin-ports/src/providers.rs | 216 ++ crates/plugin-ports/src/test_support.rs | 118 + crates/plugin-ports/src/tests.rs | 231 ++ crates/plugin-sdk/Cargo.toml | 29 + crates/plugin-sdk/src/cx.rs | 89 + crates/plugin-sdk/src/dispatch.rs | 32 + crates/plugin-sdk/src/effect.rs | 177 ++ crates/plugin-sdk/src/export.rs | 130 + crates/plugin-sdk/src/lib.rs | 98 + crates/plugin-sdk/src/observe.rs | 92 + crates/plugin-sdk/src/testing.rs | 390 +++ crates/plugin-sdk/src/tests.rs | 291 ++ crates/plugin-sdk/src/tests/host.rs | 570 ++++ crates/plugin-sdk/src/view.rs | 401 +++ crates/plugin-view/Cargo.toml | 20 + crates/plugin-view/src/chart.rs | 101 + crates/plugin-view/src/components.rs | 182 ++ crates/plugin-view/src/frame.rs | 188 ++ crates/plugin-view/src/lib.rs | 40 + crates/plugin-view/src/paint.rs | 494 ++++ crates/plugin-view/src/style.rs | 39 + crates/plugin-view/src/tests.rs | 533 ++++ crates/plugin-view/src/tree.rs | 46 + crates/plugin-view/src/view.rs | 172 ++ crates/plugin-wasm/Cargo.toml | 25 + crates/plugin-wasm/src/budget.rs | 34 + crates/plugin-wasm/src/cache.rs | 60 + crates/plugin-wasm/src/engine.rs | 36 + crates/plugin-wasm/src/guest.rs | 124 + crates/plugin-wasm/src/instance.rs | 111 + crates/plugin-wasm/src/lib.rs | 34 + crates/plugin-wasm/src/log.rs | 52 + crates/plugin-wasm/src/runtime.rs | 48 + crates/plugin-wasm/src/tests.rs | 363 +++ crates/plugin-wasm/src/tests/fixtures.rs | 217 ++ crates/plugin-wasm/src/trap.rs | 43 + crates/plugins-ui/Cargo.toml | 32 + crates/plugins-ui/src/lib.rs | 100 + crates/plugins-ui/src/page.rs | 281 ++ crates/plugins-ui/src/tests.rs | 400 +++ crates/plugins-ui/src/toggles.rs | 47 + crates/plugins-ui/src/user.rs | 30 + crates/plugins-ui/src/user/consent.rs | 120 + crates/plugins-ui/src/user/consent_view.rs | 279 ++ crates/plugins-ui/src/user/folder.rs | 78 + crates/plugins-ui/src/user/part_view.rs | 41 + crates/plugins-ui/src/user/port.rs | 207 ++ crates/plugins-ui/src/user/removal.rs | 168 ++ crates/plugins-ui/src/user/section.rs | 542 ++++ crates/plugins-ui/src/user/section_view.rs | 553 ++++ crates/plugins-ui/src/user/session.rs | 63 + crates/plugins-ui/src/user/switch_view.rs | 83 + crates/plugins-ui/src/user/tests.rs | 770 +++++ crates/plugins-ui/src/user/tests/lists.rs | 48 + crates/plugins-ui/src/user/tests/page.rs | 93 + crates/plugins-ui/src/user/tests/removal.rs | 112 + crates/plugins-ui/src/user/tests/switch.rs | 214 ++ .../plugins-ui/src/user/tests/switch_one.rs | 46 + crates/plugins-ui/src/view.rs | 348 +++ crates/project-container-fs/Cargo.toml | 27 + crates/project-container-fs/src/filesystem.rs | 154 + .../src/filesystem/actor.rs | 251 ++ .../src/filesystem/codec.rs | 77 + .../src/filesystem/commands.rs | 105 + .../src/filesystem/download.rs | 125 + .../src/filesystem/handles.rs | 137 + .../src/filesystem/scripts.rs | 160 ++ .../src/filesystem/upload.rs | 176 ++ crates/project-container-fs/src/lib.rs | 17 + crates/project-container-fs/src/session.rs | 126 + crates/project-container-fs/src/tests.rs | 17 + .../src/tests/filesystem.rs | 340 +++ crates/project-environment/Cargo.toml | 23 + crates/project-environment/src/activation.rs | 119 + .../src/activation/tests.rs | 141 + crates/project-environment/src/environment.rs | 158 ++ .../src/environment/paths.rs | 44 + .../src/environment/platform.rs | 197 ++ .../src/environment/probe.rs | 205 ++ .../src/environment/probe/tests.rs | 39 + crates/project-environment/src/languages.rs | 41 + .../project-environment/src/languages/java.rs | 27 + .../src/languages/layout.rs | 61 + .../project-environment/src/languages/node.rs | 26 + .../src/languages/python.rs | 25 + .../src/languages/rust_bun.rs | 25 + crates/project-environment/src/lib.rs | 19 + crates/project-environment/src/managed.rs | 65 + crates/project-model/Cargo.toml | 25 + crates/project-model/src/catalog.rs | 191 ++ crates/project-model/src/catalog_state.rs | 104 + crates/project-model/src/definition.rs | 291 ++ crates/project-model/src/error.rs | 98 + crates/project-model/src/identity.rs | 85 + crates/project-model/src/lib.rs | 38 + crates/project-model/src/metadata.rs | 15 + crates/project-model/src/repository.rs | 19 + crates/project-model/src/root.rs | 46 + crates/project-model/src/target.rs | 112 + crates/project-model/src/testing.rs | 6 + crates/project-model/src/testing/metadata.rs | 70 + .../project-model/src/testing/repository.rs | 149 + crates/project-model/tests/behavior.rs | 13 + .../project-model/tests/behavior/catalog.rs | 215 ++ .../tests/behavior/catalog_state.rs | 96 + .../project-model/tests/behavior/identity.rs | 250 ++ .../tests/behavior/repository.rs | 122 + .../project-model/tests/behavior/targets.rs | 109 + crates/project-model/tests/support/mod.rs | 20 + crates/project-providers/Cargo.toml | 32 + crates/project-providers/src/activation.rs | 18 + .../src/created_environment.rs | 12 + crates/project-providers/src/environment.rs | 207 ++ .../src/environment_commands.rs | 20 + crates/project-providers/src/error.rs | 165 ++ crates/project-providers/src/image_search.rs | 76 + crates/project-providers/src/language.rs | 126 + crates/project-providers/src/lib.rs | 47 + crates/project-providers/src/location.rs | 139 + crates/project-providers/src/progress.rs | 45 + crates/project-providers/src/removal.rs | 75 + crates/project-providers/src/settings.rs | 64 + crates/project-providers/src/setup.rs | 238 ++ crates/project-providers/src/testing.rs | 172 ++ .../src/testing/activation.rs | 27 + .../src/testing/environment.rs | 239 ++ .../src/testing/image_search.rs | 96 + .../project-providers/src/testing/removal.rs | 84 + crates/project-providers/src/tests.rs | 351 +++ crates/project-runtime/Cargo.toml | 28 + crates/project-runtime/src/command.rs | 194 ++ crates/project-runtime/src/image_search.rs | 183 ++ .../project-runtime/src/image_search/tests.rs | 204 ++ crates/project-runtime/src/inspect.rs | 69 + crates/project-runtime/src/lib.rs | 28 + crates/project-runtime/src/ownership.rs | 118 + crates/project-runtime/src/prepare.rs | 320 +++ crates/project-runtime/src/pull_progress.rs | 121 + crates/project-runtime/src/removal.rs | 84 + crates/project-runtime/src/tests.rs | 294 ++ crates/project-runtime/src/tests/commands.rs | 100 + crates/project-runtime/src/tests/creation.rs | 335 +++ crates/project-runtime/src/tests/removal.rs | 151 + crates/project-service/Cargo.toml | 32 + crates/project-service/src/catalog.rs | 233 ++ crates/project-service/src/claims.rs | 143 + crates/project-service/src/environment.rs | 167 ++ .../src/environment/admission.rs | 62 + crates/project-service/src/error.rs | 63 + crates/project-service/src/installation.rs | 307 ++ crates/project-service/src/lib.rs | 37 + crates/project-service/src/prepared.rs | 28 + crates/project-service/src/removal.rs | 133 + .../project-service/src/removal/admission.rs | 94 + crates/project-service/src/setup.rs | 175 ++ crates/project-service/src/setup/admission.rs | 57 + crates/project-service/tests/behavior.rs | 14 + .../project-service/tests/behavior/claims.rs | 221 ++ .../tests/behavior/environment.rs | 284 ++ .../tests/behavior/lifecycle.rs | 184 ++ .../tests/behavior/publication.rs | 312 ++ .../project-service/tests/behavior/removal.rs | 341 +++ .../project-service/tests/behavior/setup.rs | 141 + crates/project-service/tests/support/mod.rs | 54 + crates/project-storage/Cargo.toml | 26 + crates/project-storage/src/lib.rs | 16 + crates/project-storage/src/metadata.rs | 63 + crates/project-storage/src/repository.rs | 66 + crates/project-storage/tests/catalog.rs | 336 +++ crates/projects/Cargo.toml | 40 + crates/projects/assets/languages/bun.svg | 1 + crates/projects/assets/languages/java.svg | 1 + crates/projects/assets/languages/nodejs.svg | 1 + crates/projects/assets/languages/python.svg | 1 + crates/projects/assets/languages/rust.svg | 1 + crates/projects/assets/languages/sources.json | 32 + crates/projects/src/context.rs | 218 ++ crates/projects/src/editor.rs | 539 ++++ crates/projects/src/editor/controls.rs | 296 ++ crates/projects/src/editor/navigation.rs | 88 + crates/projects/src/editor/options.rs | 545 ++++ .../src/editor/options/container_picker.rs | 302 ++ .../src/editor/options/container_row.rs | 137 + .../src/editor/options/image_picker.rs | 448 +++ .../src/editor/options/registry_search.rs | 154 + crates/projects/src/editor/publication.rs | 118 + crates/projects/src/environment.rs | 473 ++++ crates/projects/src/environment/source.rs | 107 + crates/projects/src/error.rs | 19 + crates/projects/src/installation.rs | 171 ++ crates/projects/src/languages.rs | 319 +++ crates/projects/src/lib.rs | 133 + crates/projects/src/location.rs | 27 + crates/projects/src/opening.rs | 193 ++ crates/projects/src/panel.rs | 218 ++ crates/projects/src/panel/library.rs | 98 + crates/projects/src/panel/library/row.rs | 246 ++ crates/projects/src/plugin.rs | 38 + crates/projects/src/progress.rs | 62 + crates/projects/src/removal.rs | 322 +++ crates/projects/src/removal/source.rs | 73 + crates/projects/src/settings_page.rs | 74 + crates/projects/src/tests.rs | 115 + .../projects/src/tests/container_selection.rs | 332 +++ crates/projects/src/tests/creation.rs | 678 +++++ crates/projects/src/tests/creation_layout.rs | 254 ++ crates/projects/src/tests/environment.rs | 453 +++ .../projects/src/tests/environment/native.rs | 175 ++ crates/projects/src/tests/image_selection.rs | 603 ++++ crates/projects/src/tests/library.rs | 270 ++ crates/projects/src/tests/lifecycle.rs | 362 +++ crates/projects/src/tests/plugin.rs | 39 + crates/projects/src/tests/removal.rs | 314 +++ crates/projects/src/tests/settings.rs | 190 ++ crates/projects/src/tests/support.rs | 379 +++ crates/projects/src/tests/terminals.rs | 577 ++++ crates/projects/src/tests/wizard.rs | 419 +++ crates/projects/src/view.rs | 537 ++++ crates/projects/src/view/lifetime.rs | 70 + crates/session/src/command_jobs.rs | 114 + crates/session/src/command_jobs/error.rs | 8 + crates/session/src/connection_failure.rs | 36 + crates/session/src/connection_source.rs | 108 + crates/session/src/connection_source/tests.rs | 63 + crates/session/src/credentials.rs | 51 +- crates/session/src/exec.rs | 101 +- crates/session/src/fs.rs | 25 + crates/session/src/fs/collect.rs | 96 + crates/session/src/launch.rs | 7 + crates/session/src/lib.rs | 39 +- crates/session/src/local_fs.rs | 3 + crates/session/src/local_fs/policy.rs | 5 + crates/session/src/options.rs | 54 + crates/session/src/program.rs | 95 +- crates/session/src/relay.rs | 162 ++ crates/session/src/relay/tests.rs | 122 + crates/session/src/session.rs | 131 +- crates/session/src/target.rs | 30 + crates/session/src/testing/command_jobs.rs | 183 ++ .../session/src/testing/connection_source.rs | 27 + crates/session/src/testing/credentials.rs | 46 + crates/session/src/testing/exec.rs | 31 + crates/session/src/testing/fs.rs | 5 + crates/session/src/testing/fs/native.rs | 19 +- crates/session/src/testing/fs/streams.rs | 80 +- crates/session/src/testing/launch.rs | 32 + crates/session/src/testing/mod.rs | 8 + crates/session/src/testing/relay.rs | 178 ++ crates/session/src/testing/tests.rs | 1 + .../session/src/testing/tests/credentials.rs | 37 +- .../src/testing/tests/native_filesystem.rs | 23 + crates/session/src/testing/tests/relay.rs | 65 + crates/session/src/testing/transport.rs | 40 +- crates/settings-ui/src/catalog.rs | 64 +- crates/settings-ui/src/installation.rs | 13 - crates/settings-ui/src/lib.rs | 110 +- crates/settings-ui/src/nav.rs | 112 + crates/settings-ui/src/tests.rs | 252 +- crates/settings/src/store.rs | 18 +- crates/settings/src/store/tests.rs | 16 + crates/snippets-ui/Cargo.toml | 2 + crates/snippets-ui/src/lib.rs | 30 +- crates/snippets-ui/src/panel.rs | 84 +- crates/snippets-ui/src/panel/listing.rs | 102 + crates/snippets-ui/src/panel/run_tests.rs | 6 +- crates/snippets-ui/src/panel/tests.rs | 211 +- crates/snippets-ui/src/plugin.rs | 42 + crates/snippets-ui/src/plugin_tests.rs | 70 + crates/snippets/src/lib.rs | 28 +- crates/snippets/src/tests.rs | 4 + crates/ssh-keys/Cargo.toml | 24 + crates/ssh-keys/src/authorized_keys.rs | 129 + crates/ssh-keys/src/error.rs | 36 + crates/ssh-keys/src/lib.rs | 32 + crates/ssh-keys/src/material.rs | 106 + crates/ssh-keys/src/port.rs | 59 + crates/ssh-keys/src/public.rs | 68 + crates/ssh-keys/src/testing.rs | 256 ++ .../ssh-keys/src/testing/connected_hosts.rs | 48 + crates/ssh-keys/src/tests.rs | 265 ++ crates/ssh-keys/src/tests/fixtures/ecdsa-p256 | 8 + .../src/tests/fixtures/ecdsa-p256.pub | 1 + crates/ssh-keys/src/tests/fixtures/ecdsa-p521 | 12 + .../src/tests/fixtures/ecdsa-p521.pub | 1 + crates/ssh-keys/src/tests/fixtures/ed25519 | 7 + .../src/tests/fixtures/ed25519-encrypted | 8 + .../src/tests/fixtures/ed25519-encrypted.pub | 1 + .../ssh-keys/src/tests/fixtures/ed25519.pub | 1 + crates/ssh-keys/src/tests/fixtures/rsa-1024 | 16 + .../ssh-keys/src/tests/fixtures/rsa-1024.pub | 1 + crates/ssh-keys/src/tests/fixtures/rsa-2048 | 27 + .../ssh-keys/src/tests/fixtures/rsa-2048.pub | 1 + crates/ssh/Cargo.toml | 5 + crates/ssh/src/auth.rs | 151 +- crates/ssh/src/authorized_keys.rs | 480 ++++ crates/ssh/src/authorized_keys/edit_lock.rs | 75 + crates/ssh/src/connection.rs | 31 +- crates/ssh/src/connection_relay.rs | 45 + crates/ssh/src/exec.rs | 92 +- crates/ssh/src/host_keys.rs | 7 + crates/ssh/src/keys.rs | 78 + crates/ssh/src/lib.rs | 75 +- crates/ssh/src/login.rs | 9 +- crates/ssh/src/proxy.rs | 261 +- crates/ssh/src/proxy/hub_tests.rs | 100 + crates/ssh/src/proxy/relay.rs | 149 + crates/ssh/src/proxy/relay/tests.rs | 157 ++ crates/ssh/src/route.rs | 43 +- crates/ssh/src/sftp.rs | 20 +- crates/ssh/src/sftp/actor.rs | 94 +- crates/ssh/src/sftp/download.rs | 71 + crates/ssh/src/sftp/io.rs | 11 + crates/ssh/src/sftp/listing.rs | 91 +- crates/ssh/src/sftp/tests.rs | 268 +- crates/ssh/src/source_stream.rs | 119 + crates/ssh/tests/cases/exec.rs | 32 + crates/ssh/tests/cases/host_keys.rs | 14 +- crates/ssh/tests/cases/hub_relay.rs | 289 ++ crates/ssh/tests/cases/jump.rs | 177 +- crates/ssh/tests/cases/proxy.rs | 2 +- crates/ssh/tests/cases/source_change.rs | 85 + crates/ssh/tests/cases/vault_key.rs | 78 + crates/ssh/tests/sshd.rs | 236 +- crates/ssh/tests/support/hub_relay.rs | 116 + crates/ssh/tests/support/keys.rs | 24 + crates/ssh/tests/support/relay.rs | 39 + crates/ssh/tests/support/sshd.rs | 239 ++ crates/store/src/error.rs | 2 + crates/store/src/keyed.rs | 22 + crates/store/src/keyed/tests.rs | 43 + crates/store/src/lib.rs | 2 + crates/store/src/persisted.rs | 55 +- crates/store/src/persisted/tests.rs | 73 + crates/terminal/Cargo.toml | 8 +- crates/terminal/src/access.rs | 78 +- crates/terminal/src/access/project_parent.rs | 62 + crates/terminal/src/bar.rs | 104 + crates/terminal/src/credential_capture.rs | 134 + crates/terminal/src/credentials.rs | 24 + crates/terminal/src/credentials/fake.rs | 16 + crates/terminal/src/credentials/tests.rs | 21 + crates/terminal/src/element.rs | 1 + crates/terminal/src/highlighting/tests.rs | 10 +- crates/terminal/src/installed.rs | 252 +- crates/terminal/src/installed/tests.rs | 46 + crates/terminal/src/lib.rs | 129 +- crates/terminal/src/opening_tests.rs | 713 +++++ .../src/opening_tests/credential_owners.rs | 246 ++ .../credential_owners/programs.rs | 225 ++ crates/terminal/src/project.rs | 149 + crates/terminal/src/settings_page/controls.rs | 62 +- crates/terminal/src/settings_page/mod.rs | 11 +- crates/terminal/src/terminal.rs | 80 +- crates/terminal/src/terminal/input.rs | 62 +- crates/terminal/src/terminal/input/tests.rs | 2 +- crates/terminal/src/terminal/lifecycle.rs | 9 + crates/terminal/src/terminal/local_tests.rs | 8 +- crates/terminal/src/terminal/output.rs | 24 + crates/terminal/src/terminal/presentation.rs | 50 +- crates/terminal/src/terminal/program.rs | 21 +- crates/terminal/src/terminal/pump.rs | 2 +- .../terminal/src/terminal/recording_tests.rs | 59 +- crates/terminal/src/terminal/restore_tests.rs | 170 ++ crates/terminal/src/view.rs | 74 +- crates/terminal/src/view/commands.rs | 15 +- crates/terminal/src/view/cursor.rs | 21 +- crates/terminal/src/view/lease_input_tests.rs | 45 + crates/terminal/src/view/paste_tests.rs | 29 +- crates/terminal/src/view/render.rs | 40 +- crates/terminal/src/view_input_tests.rs | 29 + crates/terminal/src/view_prompt_tests.rs | 96 + crates/terminal/src/view_tests.rs | 26 +- crates/themes-registry/src/discovery.rs | 66 +- crates/themes-registry/src/discovery/tests.rs | 90 + crates/themes-registry/src/lib.rs | 13 +- crates/themes-registry/src/publication.rs | 85 +- crates/themes/src/catalog.rs | 71 + crates/themes/src/catalog/tests.rs | 23 + crates/themes/src/error.rs | 2 + crates/themes/src/test_support.rs | 8 + crates/transfer-runtime/src/batch.rs | 12 +- crates/transfer-runtime/src/changes.rs | 32 + crates/transfer-runtime/src/chunks.rs | 17 + crates/transfer-runtime/src/discovery.rs | 210 +- crates/transfer-runtime/src/download.rs | 49 +- crates/transfer-runtime/src/lib.rs | 12 +- crates/transfer-runtime/src/pipeline.rs | 89 + crates/transfer-runtime/src/relay.rs | 88 + .../src/remote_destination.rs | 100 + .../transfer-runtime/src/remote_discovery.rs | 215 ++ crates/transfer-runtime/src/reservations.rs | 2 +- crates/transfer-runtime/src/runtime.rs | 53 +- crates/transfer-runtime/src/scheduler.rs | 89 +- crates/transfer-runtime/src/upload.rs | 110 +- .../transfer-runtime/tests/cases/downloads.rs | 44 +- crates/transfer-runtime/tests/cases/relays.rs | 273 ++ .../transfer-runtime/tests/cases/uploads.rs | 30 +- crates/transfer-runtime/tests/support/mod.rs | 48 +- crates/transfer-runtime/tests/transfers.rs | 64 + crates/transfers/src/endpoint.rs | 210 ++ crates/transfers/src/lib.rs | 15 +- crates/transfers/src/owned.rs | 358 +++ crates/transfers/src/progress.rs | 5 + crates/transfers/src/request.rs | 102 +- crates/transfers/src/service.rs | 95 +- crates/transfers/src/testing.rs | 63 +- crates/transfers/tests/admission.rs | 20 +- crates/transfers/tests/progress.rs | 4 +- crates/ui/Cargo.toml | 11 + crates/ui/assets/icons/nocterm.svg | 3 + crates/ui/src/appearance_page/browser.rs | 70 +- crates/ui/src/appearance_page/commands.rs | 72 +- crates/ui/src/appearance_page/controls.rs | 49 +- crates/ui/src/appearance_page/mod.rs | 11 +- crates/ui/src/appearance_page/tests.rs | 46 + crates/ui/src/choice_picker.rs | 168 ++ crates/ui/src/contribution.rs | 122 + crates/ui/src/directory.rs | 8 + crates/ui/src/directory/navigation.rs | 75 + .../src => ui/src/directory}/path_input.rs | 151 +- .../directory}/path_input/navigation_tests.rs | 9 +- .../src/directory}/path_input/popup.rs | 10 +- .../src/directory}/path_input/state.rs | 40 +- .../src/directory}/path_input/tests.rs | 21 +- crates/ui/src/directory/picker.rs | 206 ++ crates/ui/src/directory/picker/state.rs | 75 + crates/ui/src/directory/picker/tests.rs | 164 ++ crates/ui/src/embedded_pages.rs | 240 ++ crates/ui/src/embedded_pages/tests.rs | 247 ++ crates/ui/src/form.rs | 6 + crates/ui/src/form/controls.rs | 32 + crates/ui/src/form/fields.rs | 50 + crates/ui/src/form/fold.rs | 88 + crates/ui/src/icons.rs | 49 +- crates/ui/src/lib.rs | 34 +- crates/ui/src/notice.rs | 105 +- crates/ui/src/notice/bar.rs | 35 +- crates/ui/src/notice/plugin.rs | 82 + crates/ui/src/notice/plugin/tests.rs | 181 ++ crates/ui/src/notice/tests.rs | 63 +- crates/ui/src/plugin_settings.rs | 32 + crates/ui/src/plugin_settings/binding.rs | 78 + .../ui/src/plugin_settings/binding_tests.rs | 233 ++ crates/ui/src/plugin_settings/controls.rs | 223 ++ crates/ui/src/plugin_settings/directory.rs | 77 + crates/ui/src/plugin_settings/fake.rs | 96 + crates/ui/src/plugin_settings/form.rs | 385 +++ crates/ui/src/plugin_settings/source.rs | 36 + crates/ui/src/plugin_settings/tests.rs | 342 +++ crates/ui/src/plugin_settings/text_tests.rs | 156 + crates/ui/src/plugin_source.rs | 86 + crates/ui/src/plugin_source/tests.rs | 82 + crates/ui/src/session_options.rs | 70 +- crates/ui/src/settings_page.rs | 90 +- crates/ui/src/settings_page_context.rs | 18 +- crates/ui/src/settings_text_field.rs | 27 + crates/ui/src/settings_text_field/tests.rs | 42 +- crates/ui/src/theme.rs | 3 +- crates/vault-store/src/access.rs | 8 + crates/vault-store/src/backend.rs | 11 + crates/vault-store/src/storage.rs | 27 +- .../src/tests/device_registration.rs | 15 +- .../vault-store/src/tests/envelope_files.rs | 14 +- crates/vault-store/src/worker.rs | 8 + crates/vault-ui/Cargo.toml | 2 + crates/vault-ui/src/add_password.rs | 177 ++ crates/vault-ui/src/credential_row.rs | 26 + crates/vault-ui/src/credential_row/tests.rs | 34 + crates/vault-ui/src/credentials.rs | 37 +- crates/vault-ui/src/credentials/tests.rs | 34 + crates/vault-ui/src/credentials_tab.rs | 377 +++ crates/vault-ui/src/lib.rs | 79 +- crates/vault-ui/src/render.rs | 105 +- crates/vault-ui/src/tests.rs | 19 +- crates/vault-ui/src/tests/credentials_tab.rs | 120 + crates/vault-ui/src/tests/keys_missing.rs | 45 + crates/vault-ui/src/tests/scoped_unlock.rs | 169 ++ .../vault-ui/src/tests/settings_authority.rs | 12 +- crates/vault-ui/src/unlock.rs | 45 +- crates/vault-ui/src/view.rs | 49 +- crates/vault/src/access.rs | 8 + crates/vault/src/error.rs | 17 + crates/vault/src/lib.rs | 3 +- crates/vault/src/model.rs | 248 +- crates/vault/src/state.rs | 27 +- crates/vault/src/state/tests.rs | 82 +- crates/vault/src/testing.rs | 25 +- crates/vt/Cargo.toml | 2 + crates/vt/src/emulator.rs | 21 + crates/vt/src/emulator/tests.rs | 18 + crates/vt/src/lib.rs | 17 +- crates/vt/src/search.rs | 37 +- crates/vt/src/search/tests.rs | 16 + .../src/shell_integration.rs} | 106 +- .../src/codec.rs => vt/src/text_codec.rs} | 51 +- crates/window-layout/Cargo.toml | 24 + crates/window-layout/src/builtin.rs | 63 + crates/window-layout/src/disabled_tests.rs | 323 +++ crates/window-layout/src/edit.rs | 362 +++ crates/window-layout/src/edit/kept.rs | 96 + crates/window-layout/src/edit/settle.rs | 146 + crates/window-layout/src/edit/structure.rs | 719 +++++ crates/window-layout/src/edit/toolbars.rs | 40 + crates/window-layout/src/edit_entry_tests.rs | 626 ++++ .../window-layout/src/edit_grouping_tests.rs | 194 ++ crates/window-layout/src/edit_tests.rs | 404 +++ crates/window-layout/src/lib.rs | 77 + crates/window-layout/src/name.rs | 67 + crates/window-layout/src/preset.rs | 193 ++ crates/window-layout/src/resolve.rs | 415 +++ crates/window-layout/src/resolved.rs | 221 ++ crates/window-layout/src/settings.rs | 260 ++ crates/window-layout/src/settings_tests.rs | 585 ++++ crates/window-layout/src/tests.rs | 682 +++++ crates/window-layout/src/toolbar_tests.rs | 269 ++ crates/window-layout/src/transition.rs | 53 + crates/window-layout/src/user_plugin_tests.rs | 336 +++ crates/window-layout/src/wire.rs | 161 ++ crates/workspace-ports/Cargo.toml | 6 + crates/workspace-ports/src/errors.rs | 2 + crates/workspace-ports/src/explorer.rs | 84 + crates/workspace-ports/src/lib.rs | 48 +- crates/workspace-ports/src/monitor.rs | 72 + crates/workspace-ports/src/project_parent.rs | 115 + .../workspace-ports/src/project_terminal.rs | 73 + .../src/project_terminal/error.rs | 51 + crates/workspace-ports/src/session.rs | 11 + crates/workspace-ports/src/session_source.rs | 32 + crates/workspace-ports/src/terminal.rs | 27 +- .../src/terminal/capabilities.rs | 58 + crates/workspace-ports/src/testing.rs | 38 + crates/workspace/Cargo.toml | 17 +- crates/workspace/src/actions.rs | 58 +- crates/workspace/src/clock.rs | 50 + crates/workspace/src/clock/binding.rs | 50 + crates/workspace/src/clock/plugin.rs | 97 + crates/workspace/src/clock/settings.rs | 112 + crates/workspace/src/clock/settings/tests.rs | 125 + crates/workspace/src/clock/tests.rs | 336 +++ crates/workspace/src/clock/view.rs | 99 + crates/workspace/src/context_ports.rs | 28 + crates/workspace/src/dock_item.rs | 183 +- crates/workspace/src/host.rs | 10 +- crates/workspace/src/host_capabilities.rs | 165 ++ .../workspace/src/host_capabilities/tests.rs | 240 ++ crates/workspace/src/lib.rs | 101 +- crates/workspace/src/menu_tests.rs | 77 +- crates/workspace/src/open_on_host.rs | 2 + crates/workspace/src/panel.rs | 29 + crates/workspace/src/plugin.rs | 100 + crates/workspace/src/provider_registry.rs | 44 + crates/workspace/src/right_panel.rs | 34 - crates/workspace/src/right_panel_tests.rs | 88 - crates/workspace/src/test_support.rs | 137 +- crates/workspace/src/test_support/panel.rs | 168 ++ crates/workspace/src/test_support/plugin.rs | 226 ++ .../src/test_support/project_surface.rs | 55 + .../src/test_support/project_terminal.rs | 139 + crates/workspace/src/test_support/shell.rs | 58 + crates/workspace/src/tests.rs | 134 +- crates/workspace/src/tests/close_scopes.rs | 59 + crates/workspace/src/tests/local_directory.rs | 103 + .../workspace/src/tests/project_fixtures.rs | 21 + crates/workspace/src/tests/project_tabs.rs | 140 + .../src/tests/project_tabs/actions.rs | 197 ++ .../src/tests/project_tabs/admission.rs | 474 ++++ .../src/tests/project_tabs/filesystem.rs | 24 + .../src/tests/project_tabs/gestures.rs | 192 ++ .../src/tests/project_tabs/lifecycle.rs | 263 ++ .../src/tests/provider_resolution.rs | 57 + crates/workspace/src/tests/test_plugin.rs | 37 + crates/workspace/src/workspace.rs | 194 +- crates/workspace/src/workspace/actions.rs | 42 +- crates/workspace/src/workspace/background.rs | 97 +- crates/workspace/src/workspace/bindings.rs | 137 +- crates/workspace/src/workspace/chrome.rs | 445 ++- .../src/workspace/chrome/empty_state.rs | 28 +- .../workspace/src/workspace/chrome/menus.rs | 15 +- .../src/workspace/chrome/status_bar.rs | 199 ++ .../src/workspace/chrome/status_bar/inputs.rs | 379 +++ .../src/workspace/chrome/status_bar/menu.rs | 240 ++ .../workspace/chrome/status_bar/overflow.rs | 243 ++ .../chrome/status_bar/overflow/menu.rs | 186 ++ .../chrome/status_bar/overflow/plan.rs | 100 + .../chrome/status_bar/overflow/plan/tests.rs | 137 + .../src/workspace/chrome/status_bar/row.rs | 361 +++ .../src/workspace/chrome/toggle_look.rs | 57 + .../src/workspace/chrome/toolbars.rs | 354 +++ crates/workspace/src/workspace/clock_tests.rs | 278 ++ crates/workspace/src/workspace/composition.rs | 666 +++-- crates/workspace/src/workspace/contexts.rs | 93 + .../src/workspace/enablement_tests.rs | 425 +++ .../src/workspace/focus_transition_tests.rs | 602 ++++ .../workspace/src/workspace/general_target.rs | 30 + .../workspace/src/workspace/groups_tests.rs | 6 +- crates/workspace/src/workspace/interaction.rs | 626 ++-- crates/workspace/src/workspace/layout.rs | 191 +- crates/workspace/src/workspace/layout_edit.rs | 121 + .../workspace/src/workspace/layout_switch.rs | 142 + .../src/workspace/layout_switch_tests.rs | 587 ++++ .../workspace/src/workspace/layout_tests.rs | 428 ++- .../workspace/local_dock/tests/lifecycle.rs | 2 +- .../src/workspace/local_dock/tests/targets.rs | 2 +- .../workspace/native_terminal_tab_tests.rs | 12 +- .../deferred_open.rs | 2 +- .../src/workspace/panel_availability_tests.rs | 139 + .../src/workspace/panel_chrome_tests.rs | 61 + .../workspace/src/workspace/panel_groups.rs | 739 +++++ .../workspace/panel_groups/expanded_tests.rs | 68 + .../src/workspace/panel_groups/group.rs | 109 + .../src/workspace/panel_groups/hosted.rs | 149 + .../src/workspace/panel_groups/key.rs | 87 + .../src/workspace/panel_groups/tests.rs | 699 +++++ .../src/workspace/panel_groups/transition.rs | 37 + crates/workspace/src/workspace/panel_menu.rs | 88 + .../src/workspace/panel_menu_tests.rs | 84 + crates/workspace/src/workspace/panels.rs | 116 - .../workspace/src/workspace/panels/tests.rs | 213 -- .../workspace/src/workspace/plugin_panels.rs | 97 + crates/workspace/src/workspace/ports.rs | 8 + .../src/workspace/project_surface.rs | 294 ++ .../src/workspace/project_terminal.rs | 112 + crates/workspace/src/workspace/queries.rs | 346 ++- .../workspace/src/workspace/registration.rs | 529 ++++ .../src/workspace/registration_tests.rs | 591 ++++ .../workspace/registration_tests/toolbars.rs | 309 ++ .../workspace/registration_tests/widgets.rs | 341 +++ crates/workspace/src/workspace/right_panel.rs | 182 -- .../workspace/src/workspace/sessions/tests.rs | 8 +- .../workspace/src/workspace/shell_layout.rs | 272 ++ .../src/workspace/status_bar_centre_tests.rs | 336 +++ .../src/workspace/status_bar_menu_tests.rs | 137 + .../workspace/status_bar_overflow_tests.rs | 497 ++++ .../src/workspace/status_bar_zone_tests.rs | 157 ++ crates/workspace/src/workspace/surface.rs | 553 ++++ .../src/workspace/surface/actions.rs | 148 + .../src/workspace/surface/admission.rs | 129 + .../src/workspace/surface/commands.rs | 85 + .../src/workspace/surface/membership.rs | 329 +++ .../src/workspace/surface/selection.rs | 186 ++ .../workspace/src/workspace/surface/window.rs | 162 ++ crates/workspace/src/workspace/surfaces.rs | 60 + crates/workspace/src/workspace/tabs.rs | 87 +- .../workspace/src/workspace/tabs/lifecycle.rs | 22 +- .../src/workspace/tabs/project_skin.rs | 131 + .../workspace/src/workspace/tabs/selection.rs | 21 + .../src/workspace/tabs/shell_skin.rs | 154 + .../workspace/src/workspace/tabs/topology.rs | 22 +- .../src/workspace/user_plugin_tests.rs | 350 +++ .../workspace/user_plugin_tests/provider.rs | 227 ++ deny.toml | 17 + deploy/runner/Dockerfile | 32 + docs/AI_AGENTS.md | 152 +- docs/ARCHITECTURE.md | 39 + docs/ARCHITECTURE_SECURITY_AUDIT.md | 30 + docs/HUBS.md | 245 ++ docs/PACKAGING.md | 6 + docs/VAULT.md | 40 +- docs/adr/0065-retained-context-surfaces.md | 17 + docs/adr/0066-scoped-chat-storage.md | 72 + .../0067-project-tabs-with-inner-surfaces.md | 42 + .../0069-workspace-chat-launch-boundary.md | 17 + ...-original-workspace-connection-recovery.md | 38 + docs/adr/0071-provider-workspace-locations.md | 66 + docs/adr/0072-shared-directory-selection.md | 49 + ...-terminal-free-local-workspace-creation.md | 40 + ...host-image-search-and-language-versions.md | 51 + ...served-workspace-environment-management.md | 68 + .../0076-container-filesystem-capability.md | 80 + .../adr/0077-selected-workspace-monitoring.md | 61 + ...78-explicit-workspace-container-removal.md | 59 + ...-project-terminal-contracts-below-shell.md | 60 + docs/adr/0080-project-adapter-ownership.md | 89 + docs/adr/0081-panel-groups-own-side-panels.md | 59 + ...2-system-plugins-and-core-contributions.md | 63 + docs/adr/0083-zoned-status-bar.md | 62 + ...84-layout-presets-are-app-wide-settings.md | 72 + ...-plugin-model-with-separate-trust-tiers.md | 83 + ...-plugin-enablement-and-the-plugins-page.md | 79 + docs/adr/0087-status-bar-and-panels-editor.md | 66 + ...all-clock-contract-and-the-clock-plugin.md | 68 + docs/adr/0089-user-plugin-contract.md | 88 + ...-user-plugin-host-and-passive-providers.md | 66 + ...er-plugin-code-runs-in-process-on-wasmi.md | 96 + .../0092-plugin-sdk-and-developer-sessions.md | 92 + ...-start-off-and-the-api-stays-compatible.md | 108 + ...alone-and-the-bar-preview-is-the-editor.md | 61 + ...95-attached-sessions-restore-and-detach.md | 57 + ...0096-hub-contract-outside-the-workspace.md | 66 + ...097-a-window-belongs-to-one-environment.md | 115 + ...vironments-are-restored-after-a-restart.md | 103 + docs/adr/0099-windows-have-no-environments.md | 65 + docs/adr/0100-the-hub-is-a-route-step.md | 87 + ...ugh-a-replica-beside-the-local-document.md | 75 + .../0102-ssh-keys-are-kept-in-the-vault.md | 48 + ...03-account-data-is-sealed-on-the-device.md | 73 + .../0104-a-window-shows-one-environment.md | 56 + .../0105-provider-usage-through-acp-only.md | 49 + .../0106-shared-terminal-tool-ownership.md | 55 + ...run-authority-and-host-controlled-ports.md | 100 + docs/adr/0108-explicit-cloud-api-engine.md | 85 + ...ative-cloud-journal-and-effect-recovery.md | 95 + ...10-local-history-environment-partitions.md | 97 + ...se-bound-ssh-login-and-captured-sources.md | 76 + .../adr/0113-account-bound-journal-catalog.md | 76 + ...0114-native-captured-account-operations.md | 68 + docs/adr/0115-retained-physical-terminals.md | 73 + .../0116-signed-cloud-baseline-publication.md | 63 + .../0117-contained-local-tool-processes.md | 52 + ...8-account-encrypted-cloud-hub-bootstrap.md | 55 + docs/adr/0119-agent-ssh-ca.md | 65 + ...20-cloud-agent-catalog-in-the-chat-menu.md | 46 + ...0121-managed-pools-as-a-trusted-gateway.md | 53 + docs/adr/0122-cloud-chat-creation-protocol.md | 74 + docs/adr/0123-runner-control-channel.md | 103 + .../adr/0124-managed-execution-containment.md | 50 + ...account-keys-journal-horizon-own-runner.md | 48 + docs/adr/0126-own-runner-provider-lease.md | 57 + docs/adr/0127-managed-runner-agent-policy.md | 69 + ...les-through-the-shared-transfer-runtime.md | 155 + ...ce-allows-a-queued-run-to-use-the-model.md | 54 + .../0130-cloud-agents-only-in-hub-windows.md | 33 + ...one-container-watch-per-host-connection.md | 55 + ...nal-agent-workflows-and-local-resources.md | 76 + ...tory-column-and-seats-woken-for-prompts.md | 51 + ...-every-conversation-and-pending-prompts.md | 52 + ...sponsibility-and-cost-guided-agent-work.md | 70 + ...bounded-streams-and-tool-result-formats.md | 49 + ...0137-observed-configuration-publication.md | 52 + ...0138-shared-captured-monitor-collection.md | 45 + .../0139-cooperative-authorized-keys-edits.md | 43 + .../0140-offer-confirmation-publication.md | 41 + docs/adr/0141-versioned-plugin-publication.md | 43 + ...142-live-transcript-and-image-admission.md | 45 + ...ner-publishes-chat-summaries-journal-v7.md | 74 + ...e-side-runs-on-the-contract-device-core.md | 84 + ...ncipals-and-runner-certificate-sessions.md | 78 + ...device-transport-and-resumable-commands.md | 50 + ...chats-and-runs-prompts-on-bound-servers.md | 93 + ...un-on-the-hub-and-use-the-agent-chat-ui.md | 109 + ...credentials-and-profiles-have-one-space.md | 58 + ...servers-keep-their-secrets-in-its-vault.md | 71 + ...ssword-signs-in-to-the-servers-it-lists.md | 52 + ...-chat-moves-to-the-window-that-opens-it.md | 51 + ...er-plugin-requests-timers-and-terminals.md | 86 + ...external-plugins-share-the-plugins-page.md | 75 + ...-files-to-the-file-browser-and-ai-chats.md | 49 + ...files-of-terminals-that-run-no-programs.md | 64 + ...-carries-the-plugins-bar-along-one-edge.md | 59 + ...-to-its-own-tabs-and-the-user-places-it.md | 55 + ...n-charts-toolbar-axis-and-toolbar-moves.md | 49 + ...he-plugin-feature-where-it-changes-code.md | 54 + docs/adr/README.md | 102 +- docs/architecture/adapter.md | 309 +- docs/architecture/app.md | 140 +- docs/architecture/dependencies.md | 296 +- docs/architecture/domain.md | 341 ++- docs/architecture/feature.md | 312 +- docs/architecture/foundation.md | 25 + docs/architecture/guardrails.md | 89 +- docs/architecture/service.md | 264 +- docs/architecture/ui.md | 191 +- docs/plugins/README.md | 41 + docs/plugins/guide.md | 361 +++ docs/plugins/manifest.schema.json | 439 +++ docs/plugins/reference.md | 235 ++ docs/reference/actions.md | 20 +- docs/reference/design-tokens.md | 89 +- docs/reference/plugins.md | 104 + docs/reference/settings.md | 10 + examples/plugins/clock.toml | 39 + examples/plugins/focus-timer/Cargo.lock | 250 ++ examples/plugins/focus-timer/Cargo.toml | 24 + examples/plugins/focus-timer/README.md | 29 + examples/plugins/focus-timer/plugin.toml | 46 + examples/plugins/focus-timer/src/lib.rs | 150 + examples/plugins/focus-timer/tests/plugin.rs | 157 ++ examples/plugins/notes.toml | 47 + licensing/sources.json | 16 + licensing/supplemental/devicon/LICENSE | 20 + licensing/supplemental/devicon/README.md | 13 + .../grpc-grpc-rust-license-5c686e0f7978.txt | 19 + scripts/check-claude-config.py | 86 +- scripts/check-plugin-api-compat.py | 310 ++ scripts/generate-license-notices.py | 281 +- scripts/test-claude-config.py | 60 +- scripts/test-claude-hooks.py | 345 ++- scripts/test-plugin-api-compat.py | 371 +++ scripts/test-release-pipeline.cjs | 26 + src/agent_auth.rs | 27 +- src/agent_connector.rs | 37 + src/agent_history_tests.rs | 62 + src/agent_history_tests/catalog.rs | 198 ++ src/agent_history_tests/environments.rs | 215 ++ src/agent_history_tests/namespaces.rs | 500 ++++ src/agent_history_tests/transcripts.rs | 424 +++ src/agent_hosts.rs | 180 ++ src/agent_hosts/tests.rs | 131 + src/app_menus.rs | 302 +- src/application.rs | 2 +- src/bootstrap.rs | 825 +++--- src/bootstrap/credential_tests.rs | 163 ++ src/bootstrap/error.rs | 11 + src/bootstrap/observation_tests.rs | 67 + src/bootstrap/vault.rs | 104 + src/bootstrap/workspace.rs | 106 + src/connection_setup.rs | 22 +- src/gui_tests.rs | 183 +- src/gui_tests/agent_installation.rs | 8 +- src/gui_tests/chat_history.rs | 4 +- src/gui_tests/default_shell_geometry.rs | 352 +++ src/gui_tests/hub_account.rs | 560 ++++ src/gui_tests/hub_live.rs | 334 +++ src/gui_tests/hub_live/account_key.rs | 129 + src/gui_tests/hub_live/agent_chat.rs | 159 ++ src/gui_tests/hub_ports.rs | 71 + src/gui_tests/hub_windows.rs | 118 + src/gui_tests/menu_layouts.rs | 333 +++ src/gui_tests/menu_tabs.rs | 57 +- src/gui_tests/plugin_enablement.rs | 107 + src/gui_tests/plugin_registration.rs | 32 + src/gui_tests/program_opening.rs | 2 + src/gui_tests/project_workspaces.rs | 537 ++++ .../project_workspaces/reconnection.rs | 397 +++ src/gui_tests/secret_prompts.rs | 2 +- src/gui_tests/settings_pages.rs | 30 +- src/gui_tests/settings_pages/dependencies.rs | 65 +- src/gui_tests/settings_pages/layout_editor.rs | 303 ++ src/gui_tests/settings_pages/plugins.rs | 372 +++ src/gui_tests/settings_pages/text.rs | 4 +- src/gui_tests/shortcuts.rs | 8 +- src/gui_tests/snippets.rs | 2 +- src/gui_tests/snippets/native.rs | 7 +- src/gui_tests/themes.rs | 4 +- src/gui_tests/titlebar.rs | 96 +- src/gui_tests/user_plugins.rs | 669 +++++ src/gui_tests/vault.rs | 10 +- src/gui_tests/workspace_preferences.rs | 47 + src/hub_agents_wiring.rs | 38 + src/hub_server_access.rs | 119 + src/hub_sources.rs | 363 +++ src/hub_sources/agent.rs | 194 ++ src/hub_sources/follow.rs | 174 ++ src/hub_sources/notices.rs | 37 + src/main.rs | 74 +- src/plugin_command.rs | 238 ++ src/project_setup.rs | 93 + src/project_setup/connect.rs | 117 + src/project_setup/native_tests.rs | 330 +++ src/project_setup/native_tests/activation.rs | 80 + src/project_setup/native_tests/environment.rs | 205 ++ src/project_setup/native_tests/filesystem.rs | 283 ++ src/project_setup/native_tests/monitor.rs | 87 + src/project_setup/tests.rs | 79 + src/project_setup/tests/environment.rs | 240 ++ src/project_setup/tests/preparation.rs | 256 ++ src/project_setup/tests/session.rs | 147 + src/settings_pages.rs | 280 +- src/settings_pages/tests.rs | 143 +- src/shell_setup.rs | 148 + src/shell_setup/layout_editor.rs | 22 + src/shell_setup/no_user_plugins.rs | 84 + src/shell_setup/plugins.rs | 244 ++ src/shell_setup/reference.rs | 252 ++ src/shell_setup/settings.rs | 158 ++ src/shell_setup/user_plugin_contract.rs | 44 + src/shell_setup/user_plugin_panel.rs | 69 + src/shell_setup/user_plugin_panel/bars.rs | 93 + src/shell_setup/user_plugin_panel/content.rs | 310 ++ src/shell_setup/user_plugin_panel/controls.rs | 47 + src/shell_setup/user_plugin_panel/driver.rs | 96 + src/shell_setup/user_plugin_panel/glyphs.rs | 76 + src/shell_setup/user_plugin_panel/sync.rs | 327 +++ src/shell_setup/user_plugin_panel/tests.rs | 290 ++ src/shell_setup/user_plugin_panel/views.rs | 130 + src/shell_setup/user_plugins.rs | 452 +++ src/shell_setup/user_plugins/check.rs | 199 ++ src/shell_setup/user_plugins/check/tests.rs | 243 ++ src/shell_setup/user_plugins/data.rs | 237 ++ src/shell_setup/user_plugins/dev_session.rs | 276 ++ src/shell_setup/user_plugins/facts.rs | 39 + src/shell_setup/user_plugins/folder.rs | 94 + src/shell_setup/user_plugins/gate.rs | 378 +++ src/shell_setup/user_plugins/manager.rs | 531 ++++ src/shell_setup/user_plugins/manager/tests.rs | 530 ++++ src/shell_setup/user_plugins/ports.rs | 57 + src/shell_setup/user_plugins/providers.rs | 72 + src/shell_setup/user_plugins/records.rs | 109 + src/shell_setup/user_plugins/restore.rs | 105 + src/shell_setup/user_plugins/runtime.rs | 47 + src/shell_setup/user_plugins/rust_guest.rs | 89 + src/shell_setup/user_plugins/settings.rs | 89 + src/shell_setup/user_plugins/terminals.rs | 660 +++++ src/shell_setup/user_plugins/terminals/fs.rs | 335 +++ .../user_plugins/terminals/fs/tests.rs | 226 ++ .../user_plugins/terminals/line.rs | 271 ++ .../user_plugins/terminals/tests.rs | 109 + src/shell_setup/user_plugins/tests.rs | 504 ++++ src/startup.rs | 250 +- src/startup/tests.rs | 109 + src/vault_keys.rs | 152 + src/vault_keys/tests.rs | 151 + src/vault_passwords.rs | 145 + src/vault_passwords/tests.rs | 140 + src/window_sources.rs | 93 + src/windows.rs | 180 ++ tests/agent_process.rs | 1 + xtask/Cargo.toml | 7 + xtask/architecture-policy.toml | 260 ++ xtask/baseline.toml | 10 +- xtask/src/actions_reference.rs | 217 ++ xtask/src/external_contracts.rs | 287 ++ xtask/src/isolation.rs | 72 + xtask/src/isolation/gpui.rs | 104 + xtask/src/isolation/graph.rs | 299 ++ xtask/src/isolation/guest_only.rs | 127 + xtask/src/isolation/normal.rs | 76 + xtask/src/isolation/policy.rs | 142 + xtask/src/isolation/scan.rs | 280 ++ xtask/src/isolation/source.rs | 139 + xtask/src/isolation/tests.rs | 776 +++++ xtask/src/lint_policy.rs | 8 +- xtask/src/main.rs | 241 +- xtask/src/plugin_abi.rs | 169 ++ xtask/src/plugin_docs.rs | 219 ++ xtask/src/plugin_guide.rs | 201 ++ xtask/src/plugin_reference.rs | 185 ++ xtask/src/plugin_schema.rs | 234 ++ xtask/src/ratchet/bindings.rs | 24 +- xtask/src/ratchet/includes.rs | 24 +- xtask/src/ratchet/metrics.rs | 137 +- xtask/src/ratchet/mod.rs | 35 +- xtask/src/ratchet/source.rs | 54 +- xtask/src/ratchet/symbols.rs | 23 +- xtask/src/ratchet/tests.rs | 1 + xtask/src/settings_reference.rs | 10 + xtask/src/survey.rs | 7 +- xtask/src/survey/facts.rs | 9 +- 1846 files changed, 226065 insertions(+), 13998 deletions(-) create mode 100644 .claude/skills/performance/SKILL.md create mode 100644 .claude/skills/plugin-api/SKILL.md create mode 100644 .dockerignore create mode 100644 crates/agent-chat/src/roster/contexts.rs create mode 100644 crates/agent-chat/src/thread/archive/summary.rs delete mode 100644 crates/agent-chat/src/thread/execution/jobs/tests.rs create mode 100644 crates/agent-chat/src/thread/fork.rs create mode 100644 crates/agent-chat/src/thread/local_payloads.rs create mode 100644 crates/agent-chat/src/thread/title.rs create mode 100644 crates/agent-chat/src/thread/tool_runtime/file_dispatch.rs create mode 100644 crates/agent-chat/src/thread/tool_runtime/source_execution.rs create mode 100644 crates/agent-chat/src/thread/tool_runtime/transfer_dispatch.rs create mode 100644 crates/agent-chat/src/thread/tool_runtime/transfers.rs create mode 100644 crates/agent-runtime/src/catalog/allocation.rs create mode 100644 crates/agent-runtime/src/catalog/prepared.rs create mode 100644 crates/agent-runtime/src/catalog/reclamation.rs create mode 100644 crates/agent-runtime/src/documents/history_tests/contexts.rs create mode 100644 crates/agent-runtime/src/documents/history_tests/contexts/catalog_capacity.rs create mode 100644 crates/agent-runtime/src/documents/history_tests/environments.rs create mode 100644 crates/agent-runtime/src/documents/history_tests/local_launch.rs create mode 100644 crates/agent-runtime/src/documents/history_tests/ownership.rs create mode 100644 crates/agent-runtime/src/documents/history_tests/residency.rs create mode 100644 crates/agent-runtime/src/documents/registry.rs create mode 100644 crates/agent-runtime/src/error.rs create mode 100644 crates/agent-runtime/src/execution.rs create mode 100644 crates/agent-runtime/src/history_admission.rs create mode 100644 crates/agent-runtime/src/history_admission/eviction.rs create mode 100644 crates/agent-runtime/src/history_admission/local.rs create mode 100644 crates/agent-runtime/src/history_admission/local_allocations.rs create mode 100644 crates/agent-runtime/src/history_admission/testing.rs create mode 100644 crates/agent-runtime/src/holders.rs create mode 100644 crates/agent-runtime/src/storage/admitted.rs delete mode 100644 crates/agent-storage/src/catalog_tests.rs delete mode 100644 crates/agent-storage/src/history_tests.rs create mode 100644 crates/agent-storage/src/input.rs delete mode 100644 crates/agent-storage/src/loading.rs create mode 100644 crates/agent-storage/src/namespace.rs create mode 100644 crates/agent-storage/src/publication.rs create mode 100644 crates/agent-storage/tests/prepared_history.rs create mode 100644 crates/agent-tools/Cargo.toml rename crates/{agent-chat/src/thread/execution => agent-tools/src}/jobs.rs (59%) create mode 100644 crates/agent-tools/src/lib.rs create mode 100644 crates/agent-tools/tests/command_lifecycle.rs delete mode 100644 crates/agent-usage/Cargo.toml delete mode 100644 crates/agent-usage/src/claude.rs delete mode 100644 crates/agent-usage/src/lib.rs delete mode 100644 crates/agent-usage/src/parser.rs delete mode 100644 crates/agent-usage/src/session_log.rs delete mode 100644 crates/agent-usage/src/session_log/parser.rs delete mode 100644 crates/agent-usage/src/session_log/tests.rs create mode 100644 crates/agent/src/hub_access.rs create mode 100644 crates/agent/src/installation/source.rs create mode 100644 crates/agent/src/panel/account_notice.rs create mode 100644 crates/agent/src/panel/attach_files.rs create mode 100644 crates/agent/src/panel/chat_servers.rs create mode 100644 crates/agent/src/panel/composer_frame.rs delete mode 100644 crates/agent/src/panel/image_picker.rs create mode 100644 crates/agent/src/panel/message_markdown.rs create mode 100644 crates/agent/src/panel/other_windows.rs create mode 100644 crates/agent/src/panel/parts.rs create mode 100644 crates/agent/src/panel/tests/authentication.rs create mode 100644 crates/agent/src/panel/tests/chat_servers.rs create mode 100644 crates/agent/src/panel/tests/composer/context_usage.rs create mode 100644 crates/agent/src/panel/tests/context_rosters.rs create mode 100644 crates/agent/src/panel/tests/execution/file_tools.rs create mode 100644 crates/agent/src/panel/tests/execution/file_transfers.rs create mode 100644 crates/agent/src/panel/tests/execution/local_environments.rs create mode 100644 crates/agent/src/panel/tests/history/search.rs create mode 100644 crates/agent/src/panel/tests/other_windows.rs create mode 100644 crates/agent/src/panel/tests/queue_timing.rs create mode 100644 crates/agent/src/panel/tests/registration.rs create mode 100644 crates/agent/src/panel/tests/resources/windows.rs create mode 100644 crates/agent/src/panel/tests/scripted_agent.rs create mode 100644 crates/agent/src/panel/tests/seat_binding.rs create mode 100644 crates/agent/src/panel/tests/servers/workspaces.rs create mode 100644 crates/agent/src/plugin.rs create mode 100644 crates/ai/src/execution_origin.rs create mode 100644 crates/ai/src/execution_origin/error.rs create mode 100644 crates/ai/src/history/catalog_header.rs create mode 100644 crates/ai/src/history/header.rs create mode 100644 crates/ai/src/history/incoming.rs create mode 100644 crates/ai/src/history/prefix.rs create mode 100644 crates/ai/src/history/scope_tests.rs create mode 100644 crates/ai/src/history/scope_tests/environments.rs create mode 100644 crates/ai/src/history/version.rs create mode 100644 crates/ai/src/local_environment.rs create mode 100644 crates/ai/src/model/image_data.rs create mode 100644 crates/ai/src/prompt_files.rs create mode 100644 crates/ai/src/storage/prepared.rs delete mode 100644 crates/ai/src/testing/usage.rs create mode 100644 crates/ai/src/thread/live.rs create mode 100644 crates/ai/src/tools/files.rs create mode 100644 crates/ai/src/tools/files/tests.rs create mode 100644 crates/ai/src/tools/files/work.rs create mode 100644 crates/ai/src/tools/output.rs create mode 100644 crates/ai/src/tools/output/tests.rs create mode 100644 crates/ai/src/tools/transfer.rs create mode 100644 crates/ai/src/tools/transfer/tests.rs delete mode 100644 crates/ai/src/usage/source.rs create mode 100644 crates/ai/tests/execution_origin.rs create mode 100644 crates/ai/tests/history_residency.rs create mode 100644 crates/connections-io/src/resolver/tests.rs create mode 100644 crates/connections-model/src/replica.rs create mode 100644 crates/connections-model/src/replica/rules.rs create mode 100644 crates/connections-model/src/replica/state.rs create mode 100644 crates/connections-model/src/replica/tests.rs create mode 100644 crates/connections-model/src/test_support/replica.rs create mode 100644 crates/connections-store/src/connections/tests/folders.rs create mode 100644 crates/connections-store/src/replication.rs create mode 100644 crates/connections-store/src/replication/feed.rs create mode 100644 crates/connections-store/src/replication/round.rs create mode 100644 crates/connections-store/src/replication/status.rs create mode 100644 crates/connections-store/src/replication/tests.rs create mode 100644 crates/connections-store/src/replication/tests/conflicts.rs create mode 100644 crates/connections-store/src/replication/tests/feed.rs create mode 100644 crates/connections-store/src/replication/tests/lifecycle.rs create mode 100644 crates/connections-store/src/test_support.rs create mode 100644 crates/connections/src/agent_access.rs create mode 100644 crates/connections/src/editor/agent_access.rs create mode 100644 crates/connections/src/editor/agent_access/session.rs create mode 100644 crates/connections/src/editor/password.rs create mode 100644 crates/connections/src/editor/tests/agent_access.rs create mode 100644 crates/connections/src/editor/tests/passwords.rs create mode 100644 crates/connections/src/editor/tests/vault_keys.rs create mode 100644 crates/connections/src/editor/vault_key.rs create mode 100644 crates/connections/src/panel/account_state.rs create mode 100644 crates/connections/src/panel/folder.rs create mode 100644 crates/connections/src/panel/sections.rs create mode 100644 crates/connections/src/panel/tests/account_state.rs create mode 100644 crates/connections/src/panel/tests/agents.rs create mode 100644 crates/connections/src/panel/tests/list.rs create mode 100644 crates/connections/src/plugin.rs create mode 100644 crates/connections/src/plugin_tests.rs create mode 100644 crates/connections/src/saved_password.rs create mode 100644 crates/connections/src/volatile_tests.rs create mode 100644 crates/containers-service/Cargo.toml create mode 100644 crates/containers-service/src/host.rs create mode 100644 crates/containers-service/src/lib.rs create mode 100644 crates/containers-service/src/test_support.rs create mode 100644 crates/containers-service/src/tests.rs create mode 100644 crates/containers-service/src/watch.rs create mode 100644 crates/containers-ui/src/plugin.rs create mode 100644 crates/containers-ui/src/plugin_tests.rs create mode 100644 crates/containers/src/events.rs create mode 100644 crates/containers/src/events/tests.rs create mode 100644 crates/core/src/allocation.rs create mode 100644 crates/core/src/bounded_io.rs create mode 100644 crates/core/src/clock.rs create mode 100644 crates/core/src/clock/tests.rs create mode 100644 crates/core/src/fs/bounded.rs create mode 100644 crates/core/src/fs/private.rs create mode 100644 crates/core/src/persist/revisioned.rs create mode 100644 crates/core/src/persist/revisioned/tests.rs create mode 100644 crates/directory-io/Cargo.toml create mode 100644 crates/directory-io/src/lib.rs create mode 100644 crates/directory-io/src/tests.rs create mode 100644 crates/explorer/src/directory.rs create mode 100644 crates/explorer/src/directory/error.rs rename crates/{files/src/path_input/completion.rs => explorer/src/directory/path.rs} (89%) create mode 100644 crates/explorer/src/testing.rs create mode 100644 crates/files/src/host_capabilities.rs create mode 100644 crates/files/src/host_capabilities/tests.rs create mode 100644 crates/files/src/plugin.rs create mode 100644 crates/files/src/plugin_tests.rs create mode 100644 crates/files/src/projection.rs create mode 100644 crates/files/src/selection.rs create mode 100644 crates/files/src/selection_ui_tests.rs create mode 100644 crates/hub-agents-model/Cargo.toml create mode 100644 crates/hub-agents-model/src/agent.rs create mode 100644 crates/hub-agents-model/src/error.rs create mode 100644 crates/hub-agents-model/src/ids.rs create mode 100644 crates/hub-agents-model/src/lib.rs create mode 100644 crates/hub-agents-model/src/ports.rs create mode 100644 crates/hub-agents-model/src/provider.rs create mode 100644 crates/hub-agents-model/src/server.rs create mode 100644 crates/hub-agents-model/src/testing.rs create mode 100644 crates/hub-agents-model/src/tests.rs create mode 100644 crates/hub-agents/Cargo.toml create mode 100644 crates/hub-agents/src/access.rs create mode 100644 crates/hub-agents/src/installed.rs create mode 100644 crates/hub-agents/src/lib.rs create mode 100644 crates/hub-agents/src/service.rs create mode 100644 crates/hub-agents/src/tests.rs create mode 100644 crates/hub-io/Cargo.toml create mode 100644 crates/hub-io/src/account.rs create mode 100644 crates/hub-io/src/account/cache_publication.rs create mode 100644 crates/hub-io/src/account/flow.rs create mode 100644 crates/hub-io/src/account/flow/tests.rs create mode 100644 crates/hub-io/src/account/flow/tests/source_binding.rs create mode 100644 crates/hub-io/src/account/key_admission.rs create mode 100644 crates/hub-io/src/account/keyset_hub.rs create mode 100644 crates/hub-io/src/account/lifecycle.rs create mode 100644 crates/hub-io/src/account/operations.rs create mode 100644 crates/hub-io/src/account/recovery.rs create mode 100644 crates/hub-io/src/account/refresh.rs create mode 100644 crates/hub-io/src/account/slot.rs create mode 100644 crates/hub-io/src/account/source.rs create mode 100644 crates/hub-io/src/account/source/membership.rs create mode 100644 crates/hub-io/src/account/source/tests.rs create mode 100644 crates/hub-io/src/account/testing.rs create mode 100644 crates/hub-io/src/account/testing/accounts.rs create mode 100644 crates/hub-io/src/account/testing/hub.rs create mode 100644 crates/hub-io/src/account/testing/native_operations.rs create mode 100644 crates/hub-io/src/account/testing/subjects.rs create mode 100644 crates/hub-io/src/account/tests.rs create mode 100644 crates/hub-io/src/account_keys.rs create mode 100644 crates/hub-io/src/agents.rs create mode 100644 crates/hub-io/src/agents/convert.rs create mode 100644 crates/hub-io/src/agents/servers.rs create mode 100644 crates/hub-io/src/agents/tests.rs create mode 100644 crates/hub-io/src/cache.rs create mode 100644 crates/hub-io/src/cache/tests.rs create mode 100644 crates/hub-io/src/chats.rs create mode 100644 crates/hub-io/src/chats/connector.rs create mode 100644 crates/hub-io/src/chats/hints.rs create mode 100644 crates/hub-io/src/chats/items.rs create mode 100644 crates/hub-io/src/chats/options.rs create mode 100644 crates/hub-io/src/chats/repository.rs create mode 100644 crates/hub-io/src/chats/repository/list.rs create mode 100644 crates/hub-io/src/chats/repository/tests.rs create mode 100644 crates/hub-io/src/chats/state.rs create mode 100644 crates/hub-io/src/chats/state/tests.rs create mode 100644 crates/hub-io/src/chats/upload.rs create mode 100644 crates/hub-io/src/chats/watch.rs create mode 100644 crates/hub-io/src/checked.rs create mode 100644 crates/hub-io/src/classify.rs create mode 100644 crates/hub-io/src/classify/tests.rs create mode 100644 crates/hub-io/src/connect.rs create mode 100644 crates/hub-io/src/connect/login.rs create mode 100644 crates/hub-io/src/connect/request.rs create mode 100644 crates/hub-io/src/connect/sign_out.rs create mode 100644 crates/hub-io/src/connect/start.rs create mode 100644 crates/hub-io/src/connect/tests.rs create mode 100644 crates/hub-io/src/convert.rs create mode 100644 crates/hub-io/src/convert/tests.rs create mode 100644 crates/hub-io/src/credentials.rs create mode 100644 crates/hub-io/src/credentials/bound.rs create mode 100644 crates/hub-io/src/credentials/bound/tests.rs create mode 100644 crates/hub-io/src/credentials/publication.rs create mode 100644 crates/hub-io/src/credentials/seed.rs create mode 100644 crates/hub-io/src/credentials/tests.rs create mode 100644 crates/hub-io/src/devices.rs create mode 100644 crates/hub-io/src/directory.rs create mode 100644 crates/hub-io/src/endpoint.rs create mode 100644 crates/hub-io/src/hub_account.rs create mode 100644 crates/hub-io/src/lib.rs create mode 100644 crates/hub-io/src/links.rs create mode 100644 crates/hub-io/src/pool.rs create mode 100644 crates/hub-io/src/pool/captured.rs create mode 100644 crates/hub-io/src/pool/observation.rs create mode 100644 crates/hub-io/src/pool/tests.rs create mode 100644 crates/hub-io/src/relay.rs create mode 100644 crates/hub-io/src/relay/failure.rs create mode 100644 crates/hub-io/src/relay/failure/tests.rs create mode 100644 crates/hub-io/src/relay/pump.rs create mode 100644 crates/hub-io/src/relay/pump/tests.rs create mode 100644 crates/hub-io/src/relay/tests.rs create mode 100644 crates/hub-io/src/replica.rs create mode 100644 crates/hub-io/src/replica/tests.rs create mode 100644 crates/hub-io/src/replica/tests/source_binding.rs create mode 100644 crates/hub-io/src/replica/watch.rs create mode 100644 crates/hub-io/src/replica/wire.rs create mode 100644 crates/hub-io/src/replica/wire/tests.rs create mode 100644 crates/hub-io/src/store.rs create mode 100644 crates/hub-io/src/store/client.rs create mode 100644 crates/hub-io/src/store/grpc.rs create mode 100644 crates/hub-io/src/store/memory.rs create mode 100644 crates/hub-io/src/store/memory/accounts.rs create mode 100644 crates/hub-io/src/store/tests.rs create mode 100644 crates/hub-io/src/task.rs create mode 100644 crates/hub-io/src/vault.rs create mode 100644 crates/hub-io/src/vault/codec.rs create mode 100644 crates/hub-io/src/vault/codec/tests.rs create mode 100644 crates/hub-io/src/vault/credentials.rs create mode 100644 crates/hub-io/src/vault/credentials/tests.rs create mode 100644 crates/hub-io/src/vault/idle.rs create mode 100644 crates/hub-io/src/vault/ledger.rs create mode 100644 crates/hub-io/src/vault/ledger/access.rs create mode 100644 crates/hub-io/src/vault/ledger/tests.rs create mode 100644 crates/hub-io/src/vault/snapshot_budget.rs create mode 100644 crates/hub-io/src/vault/tests.rs create mode 100644 crates/hub-io/src/vault/tests/refresh.rs create mode 100644 crates/hub-io/src/vault/tests/source_binding.rs create mode 100644 crates/hub-io/src/vault/unlock.rs create mode 100644 crates/hub-io/src/vault/unlock/tests.rs create mode 100644 crates/hub-io/tests/hub.rs create mode 100644 crates/hub-io/tests/hub/account.rs create mode 100644 crates/hub-io/tests/hub/chats.rs create mode 100644 crates/hub-io/tests/hub/revocation.rs create mode 100644 crates/hubs-model/Cargo.toml create mode 100644 crates/hubs-model/src/account_keys.rs create mode 100644 crates/hubs-model/src/address.rs create mode 100644 crates/hubs-model/src/address/tests.rs create mode 100644 crates/hubs-model/src/color.rs create mode 100644 crates/hubs-model/src/devices.rs create mode 100644 crates/hubs-model/src/environment.rs create mode 100644 crates/hubs-model/src/error.rs create mode 100644 crates/hubs-model/src/lib.rs create mode 100644 crates/hubs-model/src/login.rs create mode 100644 crates/hubs-model/src/ports.rs create mode 100644 crates/hubs-model/src/records.rs create mode 100644 crates/hubs-model/src/recovery.rs create mode 100644 crates/hubs-model/src/settings.rs create mode 100644 crates/hubs-model/src/settings/tests.rs create mode 100644 crates/hubs-model/src/source_identity.rs create mode 100644 crates/hubs-model/src/source_identity/local.rs create mode 100644 crates/hubs-model/src/testing.rs create mode 100644 crates/hubs-model/src/testing/account_keys.rs create mode 100644 crates/hubs-model/src/testing/devices.rs create mode 100644 crates/hubs-model/src/testing/local_source.rs create mode 100644 crates/hubs-model/src/testing/login.rs create mode 100644 crates/hubs-model/src/testing/recovery.rs create mode 100644 crates/hubs-model/src/testing/server.rs create mode 100644 crates/hubs-model/src/testing/tests.rs create mode 100644 crates/hubs-model/src/trust.rs create mode 100644 crates/hubs-model/src/trust/tests.rs create mode 100644 crates/hubs-ui/Cargo.toml create mode 100644 crates/hubs-ui/src/color.rs create mode 100644 crates/hubs-ui/src/commands.rs create mode 100644 crates/hubs-ui/src/commands_tests.rs create mode 100644 crates/hubs-ui/src/installation.rs create mode 100644 crates/hubs-ui/src/layout.rs create mode 100644 crates/hubs-ui/src/lib.rs create mode 100644 crates/hubs-ui/src/manage.rs create mode 100644 crates/hubs-ui/src/menu.rs create mode 100644 crates/hubs-ui/src/menu_tests.rs create mode 100644 crates/hubs-ui/src/messages.rs create mode 100644 crates/hubs-ui/src/ports.rs create mode 100644 crates/hubs-ui/src/problems.rs create mode 100644 crates/hubs-ui/src/servers.rs create mode 100644 crates/hubs-ui/src/servers/detail.rs create mode 100644 crates/hubs-ui/src/servers/list.rs create mode 100644 crates/hubs-ui/src/servers/tabs.rs create mode 100644 crates/hubs-ui/src/servers/tabs/account_keys.rs create mode 100644 crates/hubs-ui/src/servers/tabs/agent_access.rs create mode 100644 crates/hubs-ui/src/servers/tabs/agents.rs create mode 100644 crates/hubs-ui/src/servers/tabs/agents/form.rs create mode 100644 crates/hubs-ui/src/servers/tabs/agents/providers.rs create mode 100644 crates/hubs-ui/src/servers/tabs/devices.rs create mode 100644 crates/hubs-ui/src/servers/tabs/names.rs create mode 100644 crates/hubs-ui/src/servers/tabs/overview.rs create mode 100644 crates/hubs-ui/src/servers/tabs/trust.rs create mode 100644 crates/hubs-ui/src/servers_agents_tests.rs create mode 100644 crates/hubs-ui/src/servers_tests.rs create mode 100644 crates/hubs-ui/src/sign_in.rs create mode 100644 crates/hubs-ui/src/sign_in/form.rs create mode 100644 crates/hubs-ui/src/sign_in_tests.rs create mode 100644 crates/hubs-ui/src/status.rs create mode 100644 crates/hubs-ui/src/status_tests.rs create mode 100644 crates/hubs-ui/src/switch.rs create mode 100644 crates/hubs-ui/src/switch_tests.rs create mode 100644 crates/hubs-ui/src/test_support.rs create mode 100644 crates/hubs/Cargo.toml create mode 100644 crates/hubs/src/error.rs create mode 100644 crates/hubs/src/fixture.rs create mode 100644 crates/hubs/src/installed.rs create mode 100644 crates/hubs/src/lib.rs create mode 100644 crates/hubs/src/registry.rs create mode 100644 crates/hubs/src/registry/tests.rs create mode 100644 crates/hubs/src/sign_in.rs create mode 100644 crates/hubs/src/sign_in/tests.rs create mode 100644 crates/hubs/src/sign_in/tests/source_binding.rs create mode 100644 crates/hubs/src/status.rs create mode 100644 crates/layout-ui/Cargo.toml create mode 100644 crates/layout-ui/src/about.rs create mode 100644 crates/layout-ui/src/commands.rs create mode 100644 crates/layout-ui/src/dnd.rs create mode 100644 crates/layout-ui/src/drops.rs create mode 100644 crates/layout-ui/src/face.rs create mode 100644 crates/layout-ui/src/fit.rs create mode 100644 crates/layout-ui/src/history.rs create mode 100644 crates/layout-ui/src/keys.rs create mode 100644 crates/layout-ui/src/lib.rs create mode 100644 crates/layout-ui/src/menu.rs create mode 100644 crates/layout-ui/src/parts.rs create mode 100644 crates/layout-ui/src/presets.rs create mode 100644 crates/layout-ui/src/preview.rs create mode 100644 crates/layout-ui/src/rename.rs create mode 100644 crates/layout-ui/src/section.rs create mode 100644 crates/layout-ui/src/selection.rs create mode 100644 crates/layout-ui/src/snapshot.rs create mode 100644 crates/layout-ui/src/tests.rs create mode 100644 crates/layout-ui/src/tests/aim.rs create mode 100644 crates/layout-ui/src/tests/dnd.rs create mode 100644 crates/layout-ui/src/tests/editing.rs create mode 100644 crates/layout-ui/src/tests/geometry.rs create mode 100644 crates/layout-ui/src/tests/grouping.rs create mode 100644 crates/layout-ui/src/tests/history.rs create mode 100644 crates/layout-ui/src/tests/menu.rs create mode 100644 crates/layout-ui/src/tests/presets.rs create mode 100644 crates/layout-ui/src/tray.rs create mode 100644 crates/layout-ui/src/user_facts.rs create mode 100644 crates/local/src/clock.rs create mode 100644 crates/local/src/clock/tests.rs create mode 100644 crates/local/src/exec/admission.rs create mode 100644 crates/local/src/integration.rs create mode 100644 crates/local/src/readiness.rs create mode 100644 crates/local/src/tests/leader_exit.rs create mode 100644 crates/monitor-service/Cargo.toml create mode 100644 crates/monitor-service/src/lib.rs create mode 100644 crates/monitor-service/src/tests.rs create mode 100644 crates/monitor-service/src/watch.rs create mode 100644 crates/monitor-ui/src/host_capabilities.rs create mode 100644 crates/monitor-ui/src/plugin.rs create mode 100644 crates/monitor-ui/src/source.rs create mode 100644 crates/monitor-ui/src/status_bar_settings.rs create mode 100644 crates/monitor-ui/src/tests/host_capabilities.rs create mode 100644 crates/monitor-ui/src/tests/sources.rs create mode 100644 crates/monitor-ui/src/tests/status_bar_settings.rs create mode 100644 crates/monitor-ui/src/tests/status_popover.rs create mode 100644 crates/monitor/src/container.rs create mode 100644 crates/monitor/src/container/membership.rs create mode 100644 crates/monitor/src/container/sampler.rs create mode 100644 crates/monitor/src/container/script.rs create mode 100644 crates/plugin-abi/Cargo.toml create mode 100644 crates/plugin-abi/abi-golden.json create mode 100644 crates/plugin-abi/src/codec.rs create mode 100644 crates/plugin-abi/src/lib.rs create mode 100644 crates/plugin-abi/src/names.rs create mode 100644 crates/plugin-abi/src/samples.rs create mode 100644 crates/plugin-abi/src/samples/bars.rs create mode 100644 crates/plugin-abi/src/samples/charts.rs create mode 100644 crates/plugin-abi/src/samples/files.rs create mode 100644 crates/plugin-abi/src/tests.rs create mode 100644 crates/plugin-abi/src/wire.rs create mode 100644 crates/plugin-api/Cargo.toml create mode 100644 crates/plugin-api/api-surface.json create mode 100644 crates/plugin-api/src/base64.rs create mode 100644 crates/plugin-api/src/capability.rs create mode 100644 crates/plugin-api/src/capability/clock.rs create mode 100644 crates/plugin-api/src/capability/fields.rs create mode 100644 crates/plugin-api/src/capability/files.rs create mode 100644 crates/plugin-api/src/capability/monitor.rs create mode 100644 crates/plugin-api/src/capability/net.rs create mode 100644 crates/plugin-api/src/capability/notifications.rs create mode 100644 crates/plugin-api/src/capability/panels.rs create mode 100644 crates/plugin-api/src/capability/storage.rs create mode 100644 crates/plugin-api/src/capability/table.rs create mode 100644 crates/plugin-api/src/capability/terminals.rs create mode 100644 crates/plugin-api/src/capability/timers.rs create mode 100644 crates/plugin-api/src/capability/transfers.rs create mode 100644 crates/plugin-api/src/capability/workspace.rs create mode 100644 crates/plugin-api/src/contribution.rs create mode 100644 crates/plugin-api/src/declarative.rs create mode 100644 crates/plugin-api/src/effects.rs create mode 100644 crates/plugin-api/src/error.rs create mode 100644 crates/plugin-api/src/grant.rs create mode 100644 crates/plugin-api/src/icon.rs create mode 100644 crates/plugin-api/src/identity.rs create mode 100644 crates/plugin-api/src/lib.rs create mode 100644 crates/plugin-api/src/manifest.rs create mode 100644 crates/plugin-api/src/manifest/contribution.rs create mode 100644 crates/plugin-api/src/manifest/declare.rs create mode 100644 crates/plugin-api/src/manifest/network.rs create mode 100644 crates/plugin-api/src/manifest/settings.rs create mode 100644 crates/plugin-api/src/package_format.rs create mode 100644 crates/plugin-api/src/ports.rs create mode 100644 crates/plugin-api/src/runtimes.rs create mode 100644 crates/plugin-api/src/sanitize.rs create mode 100644 crates/plugin-api/src/snapshot.rs create mode 100644 crates/plugin-api/src/surface.rs create mode 100644 crates/plugin-api/src/template.rs create mode 100644 crates/plugin-api/src/template/check.rs create mode 100644 crates/plugin-api/src/template/eval.rs create mode 100644 crates/plugin-api/src/template/view.rs create mode 100644 crates/plugin-api/src/tests.rs create mode 100644 crates/plugin-api/src/tests/effects.rs create mode 100644 crates/plugin-api/src/tests/files.rs create mode 100644 crates/plugin-api/src/tests/grant.rs create mode 100644 crates/plugin-api/src/tests/identity.rs create mode 100644 crates/plugin-api/src/tests/manifest.rs create mode 100644 crates/plugin-api/src/tests/network.rs create mode 100644 crates/plugin-api/src/tests/package_format.rs create mode 100644 crates/plugin-api/src/tests/runtime.rs create mode 100644 crates/plugin-api/src/tests/sanitize.rs create mode 100644 crates/plugin-api/src/tests/support.rs create mode 100644 crates/plugin-api/src/tests/surface.rs create mode 100644 crates/plugin-api/src/tests/table.rs create mode 100644 crates/plugin-api/src/tests/template.rs create mode 100644 crates/plugin-api/src/tests/terminals.rs create mode 100644 crates/plugin-api/src/tests/view.rs create mode 100644 crates/plugin-api/src/version.rs create mode 100644 crates/plugin-api/src/view.rs create mode 100644 crates/plugin-enablement/Cargo.toml create mode 100644 crates/plugin-enablement/src/lib.rs create mode 100644 crates/plugin-enablement/src/tests.rs create mode 100644 crates/plugin-host/Cargo.toml create mode 100644 crates/plugin-host/src/broker.rs create mode 100644 crates/plugin-host/src/cadence.rs create mode 100644 crates/plugin-host/src/caps.rs create mode 100644 crates/plugin-host/src/data.rs create mode 100644 crates/plugin-host/src/deferred.rs create mode 100644 crates/plugin-host/src/error.rs create mode 100644 crates/plugin-host/src/faults.rs create mode 100644 crates/plugin-host/src/gesture.rs create mode 100644 crates/plugin-host/src/host.rs create mode 100644 crates/plugin-host/src/inputs.rs create mode 100644 crates/plugin-host/src/lib.rs create mode 100644 crates/plugin-host/src/net.rs create mode 100644 crates/plugin-host/src/rate.rs create mode 100644 crates/plugin-host/src/records.rs create mode 100644 crates/plugin-host/src/registry.rs create mode 100644 crates/plugin-host/src/session.rs create mode 100644 crates/plugin-host/src/snapshot.rs create mode 100644 crates/plugin-host/src/test_support.rs create mode 100644 crates/plugin-host/src/tests.rs create mode 100644 crates/plugin-host/src/tests/calls.rs create mode 100644 crates/plugin-host/src/tests/data.rs create mode 100644 crates/plugin-host/src/tests/deferred.rs create mode 100644 crates/plugin-host/src/tests/effects.rs create mode 100644 crates/plugin-host/src/tests/lifecycle.rs create mode 100644 crates/plugin-host/src/tests/readers.rs create mode 100644 crates/plugin-host/src/tests/rig.rs create mode 100644 crates/plugin-host/src/tests/session_ports.rs create mode 100644 crates/plugin-model/Cargo.toml create mode 100644 crates/plugin-model/src/catalog.rs create mode 100644 crates/plugin-model/src/contribution.rs create mode 100644 crates/plugin-model/src/error.rs create mode 100644 crates/plugin-model/src/id.rs create mode 100644 crates/plugin-model/src/lib.rs create mode 100644 crates/plugin-model/src/plugin.rs create mode 100644 crates/plugin-model/src/schema.rs create mode 100644 crates/plugin-model/src/schema/edit.rs create mode 100644 crates/plugin-model/src/schema/edit_tests.rs create mode 100644 crates/plugin-model/src/schema/fixtures.rs create mode 100644 crates/plugin-model/src/schema/marks.rs create mode 100644 crates/plugin-model/src/schema/resolve.rs create mode 100644 crates/plugin-model/src/schema/resolve_tests.rs create mode 100644 crates/plugin-model/src/schema/tests.rs create mode 100644 crates/plugin-model/src/schema/text.rs create mode 100644 crates/plugin-model/src/schema/validate.rs create mode 100644 crates/plugin-model/src/schema/values.rs create mode 100644 crates/plugin-model/src/set.rs create mode 100644 crates/plugin-model/src/set/tests.rs create mode 100644 crates/plugin-model/src/tests.rs create mode 100644 crates/plugin-model/src/user.rs create mode 100644 crates/plugin-model/src/user/tests.rs create mode 100644 crates/plugin-model/src/view.rs create mode 100644 crates/plugin-net/Cargo.toml create mode 100644 crates/plugin-net/src/lib.rs create mode 100644 crates/plugin-net/src/tests.rs create mode 100644 crates/plugin-packages/Cargo.toml create mode 100644 crates/plugin-packages/src/directory.rs create mode 100644 crates/plugin-packages/src/lib.rs create mode 100644 crates/plugin-packages/src/parse.rs create mode 100644 crates/plugin-packages/src/session.rs create mode 100644 crates/plugin-packages/src/store.rs create mode 100644 crates/plugin-packages/src/tests.rs create mode 100644 crates/plugin-packages/src/tests/code_packages.rs create mode 100644 crates/plugin-packages/src/tests/session_store.rs create mode 100644 crates/plugin-ports/Cargo.toml create mode 100644 crates/plugin-ports/src/lib.rs create mode 100644 crates/plugin-ports/src/providers.rs create mode 100644 crates/plugin-ports/src/test_support.rs create mode 100644 crates/plugin-ports/src/tests.rs create mode 100644 crates/plugin-sdk/Cargo.toml create mode 100644 crates/plugin-sdk/src/cx.rs create mode 100644 crates/plugin-sdk/src/dispatch.rs create mode 100644 crates/plugin-sdk/src/effect.rs create mode 100644 crates/plugin-sdk/src/export.rs create mode 100644 crates/plugin-sdk/src/lib.rs create mode 100644 crates/plugin-sdk/src/observe.rs create mode 100644 crates/plugin-sdk/src/testing.rs create mode 100644 crates/plugin-sdk/src/tests.rs create mode 100644 crates/plugin-sdk/src/tests/host.rs create mode 100644 crates/plugin-sdk/src/view.rs create mode 100644 crates/plugin-view/Cargo.toml create mode 100644 crates/plugin-view/src/chart.rs create mode 100644 crates/plugin-view/src/components.rs create mode 100644 crates/plugin-view/src/frame.rs create mode 100644 crates/plugin-view/src/lib.rs create mode 100644 crates/plugin-view/src/paint.rs create mode 100644 crates/plugin-view/src/style.rs create mode 100644 crates/plugin-view/src/tests.rs create mode 100644 crates/plugin-view/src/tree.rs create mode 100644 crates/plugin-view/src/view.rs create mode 100644 crates/plugin-wasm/Cargo.toml create mode 100644 crates/plugin-wasm/src/budget.rs create mode 100644 crates/plugin-wasm/src/cache.rs create mode 100644 crates/plugin-wasm/src/engine.rs create mode 100644 crates/plugin-wasm/src/guest.rs create mode 100644 crates/plugin-wasm/src/instance.rs create mode 100644 crates/plugin-wasm/src/lib.rs create mode 100644 crates/plugin-wasm/src/log.rs create mode 100644 crates/plugin-wasm/src/runtime.rs create mode 100644 crates/plugin-wasm/src/tests.rs create mode 100644 crates/plugin-wasm/src/tests/fixtures.rs create mode 100644 crates/plugin-wasm/src/trap.rs create mode 100644 crates/plugins-ui/Cargo.toml create mode 100644 crates/plugins-ui/src/lib.rs create mode 100644 crates/plugins-ui/src/page.rs create mode 100644 crates/plugins-ui/src/tests.rs create mode 100644 crates/plugins-ui/src/toggles.rs create mode 100644 crates/plugins-ui/src/user.rs create mode 100644 crates/plugins-ui/src/user/consent.rs create mode 100644 crates/plugins-ui/src/user/consent_view.rs create mode 100644 crates/plugins-ui/src/user/folder.rs create mode 100644 crates/plugins-ui/src/user/part_view.rs create mode 100644 crates/plugins-ui/src/user/port.rs create mode 100644 crates/plugins-ui/src/user/removal.rs create mode 100644 crates/plugins-ui/src/user/section.rs create mode 100644 crates/plugins-ui/src/user/section_view.rs create mode 100644 crates/plugins-ui/src/user/session.rs create mode 100644 crates/plugins-ui/src/user/switch_view.rs create mode 100644 crates/plugins-ui/src/user/tests.rs create mode 100644 crates/plugins-ui/src/user/tests/lists.rs create mode 100644 crates/plugins-ui/src/user/tests/page.rs create mode 100644 crates/plugins-ui/src/user/tests/removal.rs create mode 100644 crates/plugins-ui/src/user/tests/switch.rs create mode 100644 crates/plugins-ui/src/user/tests/switch_one.rs create mode 100644 crates/plugins-ui/src/view.rs create mode 100644 crates/project-container-fs/Cargo.toml create mode 100644 crates/project-container-fs/src/filesystem.rs create mode 100644 crates/project-container-fs/src/filesystem/actor.rs create mode 100644 crates/project-container-fs/src/filesystem/codec.rs create mode 100644 crates/project-container-fs/src/filesystem/commands.rs create mode 100644 crates/project-container-fs/src/filesystem/download.rs create mode 100644 crates/project-container-fs/src/filesystem/handles.rs create mode 100644 crates/project-container-fs/src/filesystem/scripts.rs create mode 100644 crates/project-container-fs/src/filesystem/upload.rs create mode 100644 crates/project-container-fs/src/lib.rs create mode 100644 crates/project-container-fs/src/session.rs create mode 100644 crates/project-container-fs/src/tests.rs create mode 100644 crates/project-container-fs/src/tests/filesystem.rs create mode 100644 crates/project-environment/Cargo.toml create mode 100644 crates/project-environment/src/activation.rs create mode 100644 crates/project-environment/src/activation/tests.rs create mode 100644 crates/project-environment/src/environment.rs create mode 100644 crates/project-environment/src/environment/paths.rs create mode 100644 crates/project-environment/src/environment/platform.rs create mode 100644 crates/project-environment/src/environment/probe.rs create mode 100644 crates/project-environment/src/environment/probe/tests.rs create mode 100644 crates/project-environment/src/languages.rs create mode 100644 crates/project-environment/src/languages/java.rs create mode 100644 crates/project-environment/src/languages/layout.rs create mode 100644 crates/project-environment/src/languages/node.rs create mode 100644 crates/project-environment/src/languages/python.rs create mode 100644 crates/project-environment/src/languages/rust_bun.rs create mode 100644 crates/project-environment/src/lib.rs create mode 100644 crates/project-environment/src/managed.rs create mode 100644 crates/project-model/Cargo.toml create mode 100644 crates/project-model/src/catalog.rs create mode 100644 crates/project-model/src/catalog_state.rs create mode 100644 crates/project-model/src/definition.rs create mode 100644 crates/project-model/src/error.rs create mode 100644 crates/project-model/src/identity.rs create mode 100644 crates/project-model/src/lib.rs create mode 100644 crates/project-model/src/metadata.rs create mode 100644 crates/project-model/src/repository.rs create mode 100644 crates/project-model/src/root.rs create mode 100644 crates/project-model/src/target.rs create mode 100644 crates/project-model/src/testing.rs create mode 100644 crates/project-model/src/testing/metadata.rs create mode 100644 crates/project-model/src/testing/repository.rs create mode 100644 crates/project-model/tests/behavior.rs create mode 100644 crates/project-model/tests/behavior/catalog.rs create mode 100644 crates/project-model/tests/behavior/catalog_state.rs create mode 100644 crates/project-model/tests/behavior/identity.rs create mode 100644 crates/project-model/tests/behavior/repository.rs create mode 100644 crates/project-model/tests/behavior/targets.rs create mode 100644 crates/project-model/tests/support/mod.rs create mode 100644 crates/project-providers/Cargo.toml create mode 100644 crates/project-providers/src/activation.rs create mode 100644 crates/project-providers/src/created_environment.rs create mode 100644 crates/project-providers/src/environment.rs create mode 100644 crates/project-providers/src/environment_commands.rs create mode 100644 crates/project-providers/src/error.rs create mode 100644 crates/project-providers/src/image_search.rs create mode 100644 crates/project-providers/src/language.rs create mode 100644 crates/project-providers/src/lib.rs create mode 100644 crates/project-providers/src/location.rs create mode 100644 crates/project-providers/src/progress.rs create mode 100644 crates/project-providers/src/removal.rs create mode 100644 crates/project-providers/src/settings.rs create mode 100644 crates/project-providers/src/setup.rs create mode 100644 crates/project-providers/src/testing.rs create mode 100644 crates/project-providers/src/testing/activation.rs create mode 100644 crates/project-providers/src/testing/environment.rs create mode 100644 crates/project-providers/src/testing/image_search.rs create mode 100644 crates/project-providers/src/testing/removal.rs create mode 100644 crates/project-providers/src/tests.rs create mode 100644 crates/project-runtime/Cargo.toml create mode 100644 crates/project-runtime/src/command.rs create mode 100644 crates/project-runtime/src/image_search.rs create mode 100644 crates/project-runtime/src/image_search/tests.rs create mode 100644 crates/project-runtime/src/inspect.rs create mode 100644 crates/project-runtime/src/lib.rs create mode 100644 crates/project-runtime/src/ownership.rs create mode 100644 crates/project-runtime/src/prepare.rs create mode 100644 crates/project-runtime/src/pull_progress.rs create mode 100644 crates/project-runtime/src/removal.rs create mode 100644 crates/project-runtime/src/tests.rs create mode 100644 crates/project-runtime/src/tests/commands.rs create mode 100644 crates/project-runtime/src/tests/creation.rs create mode 100644 crates/project-runtime/src/tests/removal.rs create mode 100644 crates/project-service/Cargo.toml create mode 100644 crates/project-service/src/catalog.rs create mode 100644 crates/project-service/src/claims.rs create mode 100644 crates/project-service/src/environment.rs create mode 100644 crates/project-service/src/environment/admission.rs create mode 100644 crates/project-service/src/error.rs create mode 100644 crates/project-service/src/installation.rs create mode 100644 crates/project-service/src/lib.rs create mode 100644 crates/project-service/src/prepared.rs create mode 100644 crates/project-service/src/removal.rs create mode 100644 crates/project-service/src/removal/admission.rs create mode 100644 crates/project-service/src/setup.rs create mode 100644 crates/project-service/src/setup/admission.rs create mode 100644 crates/project-service/tests/behavior.rs create mode 100644 crates/project-service/tests/behavior/claims.rs create mode 100644 crates/project-service/tests/behavior/environment.rs create mode 100644 crates/project-service/tests/behavior/lifecycle.rs create mode 100644 crates/project-service/tests/behavior/publication.rs create mode 100644 crates/project-service/tests/behavior/removal.rs create mode 100644 crates/project-service/tests/behavior/setup.rs create mode 100644 crates/project-service/tests/support/mod.rs create mode 100644 crates/project-storage/Cargo.toml create mode 100644 crates/project-storage/src/lib.rs create mode 100644 crates/project-storage/src/metadata.rs create mode 100644 crates/project-storage/src/repository.rs create mode 100644 crates/project-storage/tests/catalog.rs create mode 100644 crates/projects/Cargo.toml create mode 100644 crates/projects/assets/languages/bun.svg create mode 100644 crates/projects/assets/languages/java.svg create mode 100644 crates/projects/assets/languages/nodejs.svg create mode 100644 crates/projects/assets/languages/python.svg create mode 100644 crates/projects/assets/languages/rust.svg create mode 100644 crates/projects/assets/languages/sources.json create mode 100644 crates/projects/src/context.rs create mode 100644 crates/projects/src/editor.rs create mode 100644 crates/projects/src/editor/controls.rs create mode 100644 crates/projects/src/editor/navigation.rs create mode 100644 crates/projects/src/editor/options.rs create mode 100644 crates/projects/src/editor/options/container_picker.rs create mode 100644 crates/projects/src/editor/options/container_row.rs create mode 100644 crates/projects/src/editor/options/image_picker.rs create mode 100644 crates/projects/src/editor/options/registry_search.rs create mode 100644 crates/projects/src/editor/publication.rs create mode 100644 crates/projects/src/environment.rs create mode 100644 crates/projects/src/environment/source.rs create mode 100644 crates/projects/src/error.rs create mode 100644 crates/projects/src/installation.rs create mode 100644 crates/projects/src/languages.rs create mode 100644 crates/projects/src/lib.rs create mode 100644 crates/projects/src/location.rs create mode 100644 crates/projects/src/opening.rs create mode 100644 crates/projects/src/panel.rs create mode 100644 crates/projects/src/panel/library.rs create mode 100644 crates/projects/src/panel/library/row.rs create mode 100644 crates/projects/src/plugin.rs create mode 100644 crates/projects/src/progress.rs create mode 100644 crates/projects/src/removal.rs create mode 100644 crates/projects/src/removal/source.rs create mode 100644 crates/projects/src/settings_page.rs create mode 100644 crates/projects/src/tests.rs create mode 100644 crates/projects/src/tests/container_selection.rs create mode 100644 crates/projects/src/tests/creation.rs create mode 100644 crates/projects/src/tests/creation_layout.rs create mode 100644 crates/projects/src/tests/environment.rs create mode 100644 crates/projects/src/tests/environment/native.rs create mode 100644 crates/projects/src/tests/image_selection.rs create mode 100644 crates/projects/src/tests/library.rs create mode 100644 crates/projects/src/tests/lifecycle.rs create mode 100644 crates/projects/src/tests/plugin.rs create mode 100644 crates/projects/src/tests/removal.rs create mode 100644 crates/projects/src/tests/settings.rs create mode 100644 crates/projects/src/tests/support.rs create mode 100644 crates/projects/src/tests/terminals.rs create mode 100644 crates/projects/src/tests/wizard.rs create mode 100644 crates/projects/src/view.rs create mode 100644 crates/projects/src/view/lifetime.rs create mode 100644 crates/session/src/command_jobs.rs create mode 100644 crates/session/src/command_jobs/error.rs create mode 100644 crates/session/src/connection_failure.rs create mode 100644 crates/session/src/connection_source.rs create mode 100644 crates/session/src/connection_source/tests.rs create mode 100644 crates/session/src/fs/collect.rs create mode 100644 crates/session/src/relay.rs create mode 100644 crates/session/src/relay/tests.rs create mode 100644 crates/session/src/testing/command_jobs.rs create mode 100644 crates/session/src/testing/connection_source.rs create mode 100644 crates/session/src/testing/launch.rs create mode 100644 crates/session/src/testing/relay.rs create mode 100644 crates/session/src/testing/tests/relay.rs delete mode 100644 crates/settings-ui/src/installation.rs create mode 100644 crates/settings-ui/src/nav.rs create mode 100644 crates/snippets-ui/src/panel/listing.rs create mode 100644 crates/snippets-ui/src/plugin.rs create mode 100644 crates/snippets-ui/src/plugin_tests.rs create mode 100644 crates/ssh-keys/Cargo.toml create mode 100644 crates/ssh-keys/src/authorized_keys.rs create mode 100644 crates/ssh-keys/src/error.rs create mode 100644 crates/ssh-keys/src/lib.rs create mode 100644 crates/ssh-keys/src/material.rs create mode 100644 crates/ssh-keys/src/port.rs create mode 100644 crates/ssh-keys/src/public.rs create mode 100644 crates/ssh-keys/src/testing.rs create mode 100644 crates/ssh-keys/src/testing/connected_hosts.rs create mode 100644 crates/ssh-keys/src/tests.rs create mode 100644 crates/ssh-keys/src/tests/fixtures/ecdsa-p256 create mode 100644 crates/ssh-keys/src/tests/fixtures/ecdsa-p256.pub create mode 100644 crates/ssh-keys/src/tests/fixtures/ecdsa-p521 create mode 100644 crates/ssh-keys/src/tests/fixtures/ecdsa-p521.pub create mode 100644 crates/ssh-keys/src/tests/fixtures/ed25519 create mode 100644 crates/ssh-keys/src/tests/fixtures/ed25519-encrypted create mode 100644 crates/ssh-keys/src/tests/fixtures/ed25519-encrypted.pub create mode 100644 crates/ssh-keys/src/tests/fixtures/ed25519.pub create mode 100644 crates/ssh-keys/src/tests/fixtures/rsa-1024 create mode 100644 crates/ssh-keys/src/tests/fixtures/rsa-1024.pub create mode 100644 crates/ssh-keys/src/tests/fixtures/rsa-2048 create mode 100644 crates/ssh-keys/src/tests/fixtures/rsa-2048.pub create mode 100644 crates/ssh/src/authorized_keys.rs create mode 100644 crates/ssh/src/authorized_keys/edit_lock.rs create mode 100644 crates/ssh/src/connection_relay.rs create mode 100644 crates/ssh/src/keys.rs create mode 100644 crates/ssh/src/proxy/hub_tests.rs create mode 100644 crates/ssh/src/proxy/relay.rs create mode 100644 crates/ssh/src/proxy/relay/tests.rs create mode 100644 crates/ssh/src/source_stream.rs create mode 100644 crates/ssh/tests/cases/hub_relay.rs create mode 100644 crates/ssh/tests/cases/source_change.rs create mode 100644 crates/ssh/tests/cases/vault_key.rs create mode 100644 crates/ssh/tests/support/hub_relay.rs create mode 100644 crates/ssh/tests/support/keys.rs create mode 100644 crates/ssh/tests/support/relay.rs create mode 100644 crates/ssh/tests/support/sshd.rs create mode 100644 crates/terminal/src/access/project_parent.rs create mode 100644 crates/terminal/src/bar.rs create mode 100644 crates/terminal/src/credential_capture.rs create mode 100644 crates/terminal/src/opening_tests.rs create mode 100644 crates/terminal/src/opening_tests/credential_owners.rs create mode 100644 crates/terminal/src/opening_tests/credential_owners/programs.rs create mode 100644 crates/terminal/src/project.rs create mode 100644 crates/terminal/src/terminal/restore_tests.rs create mode 100644 crates/transfer-runtime/src/changes.rs create mode 100644 crates/transfer-runtime/src/chunks.rs create mode 100644 crates/transfer-runtime/src/pipeline.rs create mode 100644 crates/transfer-runtime/src/relay.rs create mode 100644 crates/transfer-runtime/src/remote_destination.rs create mode 100644 crates/transfer-runtime/src/remote_discovery.rs create mode 100644 crates/transfer-runtime/tests/cases/relays.rs create mode 100644 crates/transfers/src/endpoint.rs create mode 100644 crates/transfers/src/owned.rs create mode 100644 crates/ui/assets/icons/nocterm.svg create mode 100644 crates/ui/src/choice_picker.rs create mode 100644 crates/ui/src/contribution.rs create mode 100644 crates/ui/src/directory.rs create mode 100644 crates/ui/src/directory/navigation.rs rename crates/{files/src => ui/src/directory}/path_input.rs (82%) rename crates/{files/src => ui/src/directory}/path_input/navigation_tests.rs (97%) rename crates/{files/src => ui/src/directory}/path_input/popup.rs (94%) rename crates/{files/src => ui/src/directory}/path_input/state.rs (88%) rename crates/{files/src => ui/src/directory}/path_input/tests.rs (95%) create mode 100644 crates/ui/src/directory/picker.rs create mode 100644 crates/ui/src/directory/picker/state.rs create mode 100644 crates/ui/src/directory/picker/tests.rs create mode 100644 crates/ui/src/embedded_pages.rs create mode 100644 crates/ui/src/embedded_pages/tests.rs create mode 100644 crates/ui/src/form/controls.rs create mode 100644 crates/ui/src/form/fields.rs create mode 100644 crates/ui/src/form/fold.rs create mode 100644 crates/ui/src/notice/plugin.rs create mode 100644 crates/ui/src/notice/plugin/tests.rs create mode 100644 crates/ui/src/plugin_settings.rs create mode 100644 crates/ui/src/plugin_settings/binding.rs create mode 100644 crates/ui/src/plugin_settings/binding_tests.rs create mode 100644 crates/ui/src/plugin_settings/controls.rs create mode 100644 crates/ui/src/plugin_settings/directory.rs create mode 100644 crates/ui/src/plugin_settings/fake.rs create mode 100644 crates/ui/src/plugin_settings/form.rs create mode 100644 crates/ui/src/plugin_settings/source.rs create mode 100644 crates/ui/src/plugin_settings/tests.rs create mode 100644 crates/ui/src/plugin_settings/text_tests.rs create mode 100644 crates/ui/src/plugin_source.rs create mode 100644 crates/ui/src/plugin_source/tests.rs create mode 100644 crates/vault-ui/src/add_password.rs create mode 100644 crates/vault-ui/src/credential_row.rs create mode 100644 crates/vault-ui/src/credential_row/tests.rs create mode 100644 crates/vault-ui/src/credentials_tab.rs create mode 100644 crates/vault-ui/src/tests/credentials_tab.rs create mode 100644 crates/vault-ui/src/tests/keys_missing.rs create mode 100644 crates/vault-ui/src/tests/scoped_unlock.rs rename crates/{terminal/src/integration.rs => vt/src/shell_integration.rs} (51%) rename crates/{terminal/src/codec.rs => vt/src/text_codec.rs} (54%) create mode 100644 crates/window-layout/Cargo.toml create mode 100644 crates/window-layout/src/builtin.rs create mode 100644 crates/window-layout/src/disabled_tests.rs create mode 100644 crates/window-layout/src/edit.rs create mode 100644 crates/window-layout/src/edit/kept.rs create mode 100644 crates/window-layout/src/edit/settle.rs create mode 100644 crates/window-layout/src/edit/structure.rs create mode 100644 crates/window-layout/src/edit/toolbars.rs create mode 100644 crates/window-layout/src/edit_entry_tests.rs create mode 100644 crates/window-layout/src/edit_grouping_tests.rs create mode 100644 crates/window-layout/src/edit_tests.rs create mode 100644 crates/window-layout/src/lib.rs create mode 100644 crates/window-layout/src/name.rs create mode 100644 crates/window-layout/src/preset.rs create mode 100644 crates/window-layout/src/resolve.rs create mode 100644 crates/window-layout/src/resolved.rs create mode 100644 crates/window-layout/src/settings.rs create mode 100644 crates/window-layout/src/settings_tests.rs create mode 100644 crates/window-layout/src/tests.rs create mode 100644 crates/window-layout/src/toolbar_tests.rs create mode 100644 crates/window-layout/src/transition.rs create mode 100644 crates/window-layout/src/user_plugin_tests.rs create mode 100644 crates/window-layout/src/wire.rs create mode 100644 crates/workspace-ports/src/explorer.rs create mode 100644 crates/workspace-ports/src/monitor.rs create mode 100644 crates/workspace-ports/src/project_parent.rs create mode 100644 crates/workspace-ports/src/project_terminal.rs create mode 100644 crates/workspace-ports/src/project_terminal/error.rs create mode 100644 crates/workspace-ports/src/session_source.rs create mode 100644 crates/workspace-ports/src/testing.rs create mode 100644 crates/workspace/src/clock.rs create mode 100644 crates/workspace/src/clock/binding.rs create mode 100644 crates/workspace/src/clock/plugin.rs create mode 100644 crates/workspace/src/clock/settings.rs create mode 100644 crates/workspace/src/clock/settings/tests.rs create mode 100644 crates/workspace/src/clock/tests.rs create mode 100644 crates/workspace/src/clock/view.rs create mode 100644 crates/workspace/src/context_ports.rs create mode 100644 crates/workspace/src/host_capabilities.rs create mode 100644 crates/workspace/src/host_capabilities/tests.rs create mode 100644 crates/workspace/src/plugin.rs create mode 100644 crates/workspace/src/provider_registry.rs delete mode 100644 crates/workspace/src/right_panel.rs delete mode 100644 crates/workspace/src/right_panel_tests.rs create mode 100644 crates/workspace/src/test_support/panel.rs create mode 100644 crates/workspace/src/test_support/plugin.rs create mode 100644 crates/workspace/src/test_support/project_surface.rs create mode 100644 crates/workspace/src/test_support/project_terminal.rs create mode 100644 crates/workspace/src/test_support/shell.rs create mode 100644 crates/workspace/src/tests/close_scopes.rs create mode 100644 crates/workspace/src/tests/local_directory.rs create mode 100644 crates/workspace/src/tests/project_fixtures.rs create mode 100644 crates/workspace/src/tests/project_tabs.rs create mode 100644 crates/workspace/src/tests/project_tabs/actions.rs create mode 100644 crates/workspace/src/tests/project_tabs/admission.rs create mode 100644 crates/workspace/src/tests/project_tabs/filesystem.rs create mode 100644 crates/workspace/src/tests/project_tabs/gestures.rs create mode 100644 crates/workspace/src/tests/project_tabs/lifecycle.rs create mode 100644 crates/workspace/src/tests/provider_resolution.rs create mode 100644 crates/workspace/src/tests/test_plugin.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar/inputs.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar/menu.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar/overflow.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar/overflow/menu.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar/overflow/plan.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar/overflow/plan/tests.rs create mode 100644 crates/workspace/src/workspace/chrome/status_bar/row.rs create mode 100644 crates/workspace/src/workspace/chrome/toggle_look.rs create mode 100644 crates/workspace/src/workspace/chrome/toolbars.rs create mode 100644 crates/workspace/src/workspace/clock_tests.rs create mode 100644 crates/workspace/src/workspace/contexts.rs create mode 100644 crates/workspace/src/workspace/enablement_tests.rs create mode 100644 crates/workspace/src/workspace/focus_transition_tests.rs create mode 100644 crates/workspace/src/workspace/general_target.rs create mode 100644 crates/workspace/src/workspace/layout_edit.rs create mode 100644 crates/workspace/src/workspace/layout_switch.rs create mode 100644 crates/workspace/src/workspace/layout_switch_tests.rs create mode 100644 crates/workspace/src/workspace/panel_availability_tests.rs create mode 100644 crates/workspace/src/workspace/panel_chrome_tests.rs create mode 100644 crates/workspace/src/workspace/panel_groups.rs create mode 100644 crates/workspace/src/workspace/panel_groups/expanded_tests.rs create mode 100644 crates/workspace/src/workspace/panel_groups/group.rs create mode 100644 crates/workspace/src/workspace/panel_groups/hosted.rs create mode 100644 crates/workspace/src/workspace/panel_groups/key.rs create mode 100644 crates/workspace/src/workspace/panel_groups/tests.rs create mode 100644 crates/workspace/src/workspace/panel_groups/transition.rs create mode 100644 crates/workspace/src/workspace/panel_menu.rs create mode 100644 crates/workspace/src/workspace/panel_menu_tests.rs delete mode 100644 crates/workspace/src/workspace/panels.rs delete mode 100644 crates/workspace/src/workspace/panels/tests.rs create mode 100644 crates/workspace/src/workspace/plugin_panels.rs create mode 100644 crates/workspace/src/workspace/project_surface.rs create mode 100644 crates/workspace/src/workspace/project_terminal.rs create mode 100644 crates/workspace/src/workspace/registration.rs create mode 100644 crates/workspace/src/workspace/registration_tests.rs create mode 100644 crates/workspace/src/workspace/registration_tests/toolbars.rs create mode 100644 crates/workspace/src/workspace/registration_tests/widgets.rs delete mode 100644 crates/workspace/src/workspace/right_panel.rs create mode 100644 crates/workspace/src/workspace/shell_layout.rs create mode 100644 crates/workspace/src/workspace/status_bar_centre_tests.rs create mode 100644 crates/workspace/src/workspace/status_bar_menu_tests.rs create mode 100644 crates/workspace/src/workspace/status_bar_overflow_tests.rs create mode 100644 crates/workspace/src/workspace/status_bar_zone_tests.rs create mode 100644 crates/workspace/src/workspace/surface.rs create mode 100644 crates/workspace/src/workspace/surface/actions.rs create mode 100644 crates/workspace/src/workspace/surface/admission.rs create mode 100644 crates/workspace/src/workspace/surface/commands.rs create mode 100644 crates/workspace/src/workspace/surface/membership.rs create mode 100644 crates/workspace/src/workspace/surface/selection.rs create mode 100644 crates/workspace/src/workspace/surface/window.rs create mode 100644 crates/workspace/src/workspace/surfaces.rs create mode 100644 crates/workspace/src/workspace/tabs/project_skin.rs create mode 100644 crates/workspace/src/workspace/tabs/shell_skin.rs create mode 100644 crates/workspace/src/workspace/user_plugin_tests.rs create mode 100644 crates/workspace/src/workspace/user_plugin_tests/provider.rs create mode 100644 deny.toml create mode 100644 deploy/runner/Dockerfile create mode 100644 docs/HUBS.md create mode 100644 docs/adr/0065-retained-context-surfaces.md create mode 100644 docs/adr/0066-scoped-chat-storage.md create mode 100644 docs/adr/0067-project-tabs-with-inner-surfaces.md create mode 100644 docs/adr/0069-workspace-chat-launch-boundary.md create mode 100644 docs/adr/0070-original-workspace-connection-recovery.md create mode 100644 docs/adr/0071-provider-workspace-locations.md create mode 100644 docs/adr/0072-shared-directory-selection.md create mode 100644 docs/adr/0073-terminal-free-local-workspace-creation.md create mode 100644 docs/adr/0074-captured-host-image-search-and-language-versions.md create mode 100644 docs/adr/0075-observed-workspace-environment-management.md create mode 100644 docs/adr/0076-container-filesystem-capability.md create mode 100644 docs/adr/0077-selected-workspace-monitoring.md create mode 100644 docs/adr/0078-explicit-workspace-container-removal.md create mode 100644 docs/adr/0079-project-terminal-contracts-below-shell.md create mode 100644 docs/adr/0080-project-adapter-ownership.md create mode 100644 docs/adr/0081-panel-groups-own-side-panels.md create mode 100644 docs/adr/0082-system-plugins-and-core-contributions.md create mode 100644 docs/adr/0083-zoned-status-bar.md create mode 100644 docs/adr/0084-layout-presets-are-app-wide-settings.md create mode 100644 docs/adr/0085-one-plugin-model-with-separate-trust-tiers.md create mode 100644 docs/adr/0086-plugin-enablement-and-the-plugins-page.md create mode 100644 docs/adr/0087-status-bar-and-panels-editor.md create mode 100644 docs/adr/0088-wall-clock-contract-and-the-clock-plugin.md create mode 100644 docs/adr/0089-user-plugin-contract.md create mode 100644 docs/adr/0090-user-plugin-host-and-passive-providers.md create mode 100644 docs/adr/0091-user-plugin-code-runs-in-process-on-wasmi.md create mode 100644 docs/adr/0092-plugin-sdk-and-developer-sessions.md create mode 100644 docs/adr/0093-user-plugins-start-off-and-the-api-stays-compatible.md create mode 100644 docs/adr/0094-panels-stand-alone-and-the-bar-preview-is-the-editor.md create mode 100644 docs/adr/0095-attached-sessions-restore-and-detach.md create mode 100644 docs/adr/0096-hub-contract-outside-the-workspace.md create mode 100644 docs/adr/0097-a-window-belongs-to-one-environment.md create mode 100644 docs/adr/0098-windows-and-their-environments-are-restored-after-a-restart.md create mode 100644 docs/adr/0099-windows-have-no-environments.md create mode 100644 docs/adr/0100-the-hub-is-a-route-step.md create mode 100644 docs/adr/0101-profiles-synchronize-through-a-replica-beside-the-local-document.md create mode 100644 docs/adr/0102-ssh-keys-are-kept-in-the-vault.md create mode 100644 docs/adr/0103-account-data-is-sealed-on-the-device.md create mode 100644 docs/adr/0104-a-window-shows-one-environment.md create mode 100644 docs/adr/0105-provider-usage-through-acp-only.md create mode 100644 docs/adr/0106-shared-terminal-tool-ownership.md create mode 100644 docs/adr/0107-cloud-run-authority-and-host-controlled-ports.md create mode 100644 docs/adr/0108-explicit-cloud-api-engine.md create mode 100644 docs/adr/0109-native-cloud-journal-and-effect-recovery.md create mode 100644 docs/adr/0110-local-history-environment-partitions.md create mode 100644 docs/adr/0112-purpose-bound-ssh-login-and-captured-sources.md create mode 100644 docs/adr/0113-account-bound-journal-catalog.md create mode 100644 docs/adr/0114-native-captured-account-operations.md create mode 100644 docs/adr/0115-retained-physical-terminals.md create mode 100644 docs/adr/0116-signed-cloud-baseline-publication.md create mode 100644 docs/adr/0117-contained-local-tool-processes.md create mode 100644 docs/adr/0118-account-encrypted-cloud-hub-bootstrap.md create mode 100644 docs/adr/0119-agent-ssh-ca.md create mode 100644 docs/adr/0120-cloud-agent-catalog-in-the-chat-menu.md create mode 100644 docs/adr/0121-managed-pools-as-a-trusted-gateway.md create mode 100644 docs/adr/0122-cloud-chat-creation-protocol.md create mode 100644 docs/adr/0123-runner-control-channel.md create mode 100644 docs/adr/0124-managed-execution-containment.md create mode 100644 docs/adr/0125-cloud-agents-tail-account-keys-journal-horizon-own-runner.md create mode 100644 docs/adr/0126-own-runner-provider-lease.md create mode 100644 docs/adr/0127-managed-runner-agent-policy.md create mode 100644 docs/adr/0128-agents-transfer-files-through-the-shared-transfer-runtime.md create mode 100644 docs/adr/0129-the-device-allows-a-queued-run-to-use-the-model.md create mode 100644 docs/adr/0130-cloud-agents-only-in-hub-windows.md create mode 100644 docs/adr/0131-one-container-watch-per-host-connection.md create mode 100644 docs/adr/0132-proportional-agent-workflows-and-local-resources.md create mode 100644 docs/adr/0133-cloud-chats-in-the-history-column-and-seats-woken-for-prompts.md create mode 100644 docs/adr/0134-one-composer-for-every-conversation-and-pending-prompts.md create mode 100644 docs/adr/0135-responsibility-and-cost-guided-agent-work.md create mode 100644 docs/adr/0136-shared-bounded-streams-and-tool-result-formats.md create mode 100644 docs/adr/0137-observed-configuration-publication.md create mode 100644 docs/adr/0138-shared-captured-monitor-collection.md create mode 100644 docs/adr/0139-cooperative-authorized-keys-edits.md create mode 100644 docs/adr/0140-offer-confirmation-publication.md create mode 100644 docs/adr/0141-versioned-plugin-publication.md create mode 100644 docs/adr/0142-live-transcript-and-image-admission.md create mode 100644 docs/adr/0143-the-runner-publishes-chat-summaries-journal-v7.md create mode 100644 docs/adr/0144-the-device-side-runs-on-the-contract-device-core.md create mode 100644 docs/adr/0145-per-server-ca-principals-and-runner-certificate-sessions.md create mode 100644 docs/adr/0146-device-transport-and-resumable-commands.md create mode 100644 docs/adr/0147-desktop-answers-bound-chats-and-runs-prompts-on-bound-servers.md create mode 100644 docs/adr/0148-hub-chats-run-on-the-hub-and-use-the-agent-chat-ui.md create mode 100644 docs/adr/0149-agent-access-connects-with-saved-credentials-and-profiles-have-one-space.md create mode 100644 docs/adr/0150-an-accounts-servers-keep-their-secrets-in-its-vault.md create mode 100644 docs/adr/0151-a-saved-password-signs-in-to-the-servers-it-lists.md create mode 100644 docs/adr/0152-every-window-lists-every-chat-and-a-chat-moves-to-the-window-that-opens-it.md create mode 100644 docs/adr/0153-toolbars-and-user-plugin-requests-timers-and-terminals.md create mode 100644 docs/adr/0154-plugin-views-use-the-applications-components-and-external-plugins-share-the-plugins-page.md create mode 100644 docs/adr/0155-a-plugin-terminal-tab-can-serve-files-to-the-file-browser-and-ai-chats.md create mode 100644 docs/adr/0156-agents-work-with-the-files-of-terminals-that-run-no-programs.md create mode 100644 docs/adr/0157-a-plugin-terminal-tab-carries-the-plugins-bar-along-one-edge.md create mode 100644 docs/adr/0158-a-plugin-toolbar-keeps-to-its-own-tabs-and-the-user-places-it.md create mode 100644 docs/adr/0159-plugin-charts-toolbar-axis-and-toolbar-moves.md create mode 100644 docs/adr/0160-ci-compiles-each-kind-once-and-tests-the-plugin-feature-where-it-changes-code.md create mode 100644 docs/plugins/README.md create mode 100644 docs/plugins/guide.md create mode 100644 docs/plugins/manifest.schema.json create mode 100644 docs/plugins/reference.md create mode 100644 docs/reference/plugins.md create mode 100644 examples/plugins/clock.toml create mode 100644 examples/plugins/focus-timer/Cargo.lock create mode 100644 examples/plugins/focus-timer/Cargo.toml create mode 100644 examples/plugins/focus-timer/README.md create mode 100644 examples/plugins/focus-timer/plugin.toml create mode 100644 examples/plugins/focus-timer/src/lib.rs create mode 100644 examples/plugins/focus-timer/tests/plugin.rs create mode 100644 examples/plugins/notes.toml create mode 100644 licensing/supplemental/devicon/LICENSE create mode 100644 licensing/supplemental/devicon/README.md create mode 100644 licensing/upstream/grpc-grpc-rust-license-5c686e0f7978.txt create mode 100644 scripts/check-plugin-api-compat.py create mode 100644 scripts/test-plugin-api-compat.py create mode 100644 src/agent_connector.rs create mode 100644 src/agent_history_tests.rs create mode 100644 src/agent_history_tests/catalog.rs create mode 100644 src/agent_history_tests/environments.rs create mode 100644 src/agent_history_tests/namespaces.rs create mode 100644 src/agent_history_tests/transcripts.rs create mode 100644 src/agent_hosts.rs create mode 100644 src/agent_hosts/tests.rs create mode 100644 src/bootstrap/credential_tests.rs create mode 100644 src/bootstrap/error.rs create mode 100644 src/bootstrap/observation_tests.rs create mode 100644 src/bootstrap/vault.rs create mode 100644 src/bootstrap/workspace.rs create mode 100644 src/gui_tests/default_shell_geometry.rs create mode 100644 src/gui_tests/hub_account.rs create mode 100644 src/gui_tests/hub_live.rs create mode 100644 src/gui_tests/hub_live/account_key.rs create mode 100644 src/gui_tests/hub_live/agent_chat.rs create mode 100644 src/gui_tests/hub_ports.rs create mode 100644 src/gui_tests/hub_windows.rs create mode 100644 src/gui_tests/menu_layouts.rs create mode 100644 src/gui_tests/plugin_enablement.rs create mode 100644 src/gui_tests/plugin_registration.rs create mode 100644 src/gui_tests/project_workspaces.rs create mode 100644 src/gui_tests/project_workspaces/reconnection.rs create mode 100644 src/gui_tests/settings_pages/layout_editor.rs create mode 100644 src/gui_tests/settings_pages/plugins.rs create mode 100644 src/gui_tests/user_plugins.rs create mode 100644 src/gui_tests/workspace_preferences.rs create mode 100644 src/hub_agents_wiring.rs create mode 100644 src/hub_server_access.rs create mode 100644 src/hub_sources.rs create mode 100644 src/hub_sources/agent.rs create mode 100644 src/hub_sources/follow.rs create mode 100644 src/hub_sources/notices.rs create mode 100644 src/plugin_command.rs create mode 100644 src/project_setup.rs create mode 100644 src/project_setup/connect.rs create mode 100644 src/project_setup/native_tests.rs create mode 100644 src/project_setup/native_tests/activation.rs create mode 100644 src/project_setup/native_tests/environment.rs create mode 100644 src/project_setup/native_tests/filesystem.rs create mode 100644 src/project_setup/native_tests/monitor.rs create mode 100644 src/project_setup/tests.rs create mode 100644 src/project_setup/tests/environment.rs create mode 100644 src/project_setup/tests/preparation.rs create mode 100644 src/project_setup/tests/session.rs create mode 100644 src/shell_setup.rs create mode 100644 src/shell_setup/layout_editor.rs create mode 100644 src/shell_setup/no_user_plugins.rs create mode 100644 src/shell_setup/plugins.rs create mode 100644 src/shell_setup/reference.rs create mode 100644 src/shell_setup/settings.rs create mode 100644 src/shell_setup/user_plugin_contract.rs create mode 100644 src/shell_setup/user_plugin_panel.rs create mode 100644 src/shell_setup/user_plugin_panel/bars.rs create mode 100644 src/shell_setup/user_plugin_panel/content.rs create mode 100644 src/shell_setup/user_plugin_panel/controls.rs create mode 100644 src/shell_setup/user_plugin_panel/driver.rs create mode 100644 src/shell_setup/user_plugin_panel/glyphs.rs create mode 100644 src/shell_setup/user_plugin_panel/sync.rs create mode 100644 src/shell_setup/user_plugin_panel/tests.rs create mode 100644 src/shell_setup/user_plugin_panel/views.rs create mode 100644 src/shell_setup/user_plugins.rs create mode 100644 src/shell_setup/user_plugins/check.rs create mode 100644 src/shell_setup/user_plugins/check/tests.rs create mode 100644 src/shell_setup/user_plugins/data.rs create mode 100644 src/shell_setup/user_plugins/dev_session.rs create mode 100644 src/shell_setup/user_plugins/facts.rs create mode 100644 src/shell_setup/user_plugins/folder.rs create mode 100644 src/shell_setup/user_plugins/gate.rs create mode 100644 src/shell_setup/user_plugins/manager.rs create mode 100644 src/shell_setup/user_plugins/manager/tests.rs create mode 100644 src/shell_setup/user_plugins/ports.rs create mode 100644 src/shell_setup/user_plugins/providers.rs create mode 100644 src/shell_setup/user_plugins/records.rs create mode 100644 src/shell_setup/user_plugins/restore.rs create mode 100644 src/shell_setup/user_plugins/runtime.rs create mode 100644 src/shell_setup/user_plugins/rust_guest.rs create mode 100644 src/shell_setup/user_plugins/settings.rs create mode 100644 src/shell_setup/user_plugins/terminals.rs create mode 100644 src/shell_setup/user_plugins/terminals/fs.rs create mode 100644 src/shell_setup/user_plugins/terminals/fs/tests.rs create mode 100644 src/shell_setup/user_plugins/terminals/line.rs create mode 100644 src/shell_setup/user_plugins/terminals/tests.rs create mode 100644 src/shell_setup/user_plugins/tests.rs create mode 100644 src/startup/tests.rs create mode 100644 src/vault_keys.rs create mode 100644 src/vault_keys/tests.rs create mode 100644 src/vault_passwords.rs create mode 100644 src/vault_passwords/tests.rs create mode 100644 src/window_sources.rs create mode 100644 src/windows.rs create mode 100644 xtask/src/actions_reference.rs create mode 100644 xtask/src/external_contracts.rs create mode 100644 xtask/src/isolation.rs create mode 100644 xtask/src/isolation/gpui.rs create mode 100644 xtask/src/isolation/graph.rs create mode 100644 xtask/src/isolation/guest_only.rs create mode 100644 xtask/src/isolation/normal.rs create mode 100644 xtask/src/isolation/policy.rs create mode 100644 xtask/src/isolation/scan.rs create mode 100644 xtask/src/isolation/source.rs create mode 100644 xtask/src/isolation/tests.rs create mode 100644 xtask/src/plugin_abi.rs create mode 100644 xtask/src/plugin_docs.rs create mode 100644 xtask/src/plugin_guide.rs create mode 100644 xtask/src/plugin_reference.rs create mode 100644 xtask/src/plugin_schema.rs diff --git a/.claude/agents/architect.md b/.claude/agents/architect.md index 78a085d0..014c0a98 100644 --- a/.claude/agents/architect.md +++ b/.claude/agents/architect.md @@ -1,55 +1,25 @@ --- name: architect -description: Pipeline stage 1. Designs an implementation plan for a feature or non-trivial change before any code is written. Writes only plan files. +description: Optional design role. Plans a change before implementation; writes only ignored plan files. tools: Read, Grep, Glob, Bash, Write, Edit -model: opus skills: - placement - - responsibility-map - - port-design - - typed-errors - - wiring - - docs-adr --- -You design changes for this repository. You do not write or edit code; you -write only the plan directory `.claude/plans//` named in your prompt -(contract: `.claude/skills/feature/plan-bus.md`); a hook enforces that for the -edit tools, and you never write files from Bash. -Apply the preloaded procedure skills: `placement` → `10-placement.md`, -`responsibility-map` → `20-map.md` when a type, module or crate is restructured -or near an early warning, `port-design`/`typed-errors`/`wiring` for the design, -`docs-adr` for the documents decision, and write the plan as `30-design.md`. -Run Cargo only through `.claude/hooks/cargo-serial.sh` (for example -`cargo xtask survey `). +Design the task using AGENTS.md's workflow, architecture and verification policy. +Read the layer table, affected crate cards, relevant owner sections and current +ADRs. Load `responsibility-map`, `port-design`, `typed-errors`, `wiring`, `docs-adr`, +`plugin-api` or `performance` only when the task needs that procedure. -Read CONTRIBUTING.md, docs/ARCHITECTURE.md, the relevant layer notes, -the ADR index and every accepted ADR relevant to the touched crates, and their -`//!` documentation. Then read the relevant code and produce a plan: +Write only the ignored plan location supplied in the task; do not edit code or +write files from Bash. The plan may be one compact document. Cover goal, approach, +affected crates/layers and files, reuse, public surface/errors, risks, tests and +branch/base. For structural changes include invariants, transitions, lifetimes, +late-result handling and resulting counts. Use responsibility-map where the +split is unclear. Early warnings are advisory. +Record any required docs/ADR changes and verify APIs before citing them. -1. **Goal** — one or two sentences, including what is explicitly out of scope. -2. **Approach** — the chosen design and why; name the alternative you rejected and the reason. -3. **Changes** — files to create or modify, in the order they should be done, each with a one-line description. -4. **Tests** — what must be covered and at which level. -5. **Risks** — breaking changes, migrations, performance, anything the reviewer should look at hardest. -6. **Commits** — proposed Conventional Commit headers, the feature branch name - and its target/base (`master` for release-ready work, `dev` for integration). -7. **Placement** — for each new type or function, its crate, layer and reason, - following "Where code goes" in AGENTS.md. -8. **Shape** — types gaining fields and their resulting field counts; extract - responsibilities before adding to a type over 15 fields or crossing the limit. -9. **Public surface and errors** — new public items and ports, explicit dependency - inputs, and the typed error of each new fallible API. Identify any global or - quit-handler changes; new ones violate the design rules. -10. **Reuse** — existing persistence, shutdown, filesystem and fake mechanisms - used or extracted. Verify that an API exists before naming it as available. -11. **Docs/ADR** — documents to update and whether a new ADR is needed. For new - crates, include the short ownership card described in AGENTS.md. - -Prefer the smallest design that solves the stated problem. Use the ownership -examples in AGENTS.md; existing mixed responsibilities are not precedents. -Reject plans that add I/O or protocol types to domain code, extend a broad -workspace trait, grow a type over the field limit, or bypass a check. -If ambiguity changes the design, report the open questions. -Save plans in ignored `.claude/plans/`, never in `docs/`. End with the Result -block from the plan contract. +Prefer the smallest design that solves the problem while keeping architecture +invariants. Identify material unresolved choices; equivalent implementation of +an already authorized plan does not need another approval. Return the plan and +any concrete blocker. Optional handoff format: `.claude/skills/feature/plan-bus.md`. diff --git a/.claude/agents/implementer.md b/.claude/agents/implementer.md index 37328efc..942ca40b 100644 --- a/.claude/agents/implementer.md +++ b/.claude/agents/implementer.md @@ -1,37 +1,27 @@ --- name: implementer -description: Pipeline stage 2. Implements an approved plan (or fixes review findings) on a feature branch, with tests. -model: sonnet +description: Optional build role. Implements an authorized change or review fixes with behavior tests. skills: - - owner-extract - - typed-errors - - wiring - behavior-tests - - gate --- -You implement an approved plan in this repository. Read the plan directory named -in your prompt (`30-design.md` and the files before it) and keep `40-steps.md` -up to date, one transformation per step. Apply the preloaded procedure skills; -run `gate` in quick mode after each step. Run every Cargo command through -`.claude/hooks/cargo-serial.sh`. +Implement the authorized task or plan on its named feature branch. Read the +supplied plan and relevant architecture/ADRs first; follow AGENTS.md's workflow, +reuse, design and verification rules. Keep the diff limited to this task and +preserve unrelated work. +Load `owner-extract`, `typed-errors`, `wiring`, `plugin-api` and `performance` +when relevant; consult `gate` for the applicable checks and completion scope. -- Work on the feature branch named in the plan, based on its recorded target; - never directly on `master` or `dev`. -- Follow the plan. If it turns out to be wrong or incomplete, stop and report what you found instead of improvising a different design. -- Keep naming and formatting consistent while following the ownership rules in - AGENTS.md; do not copy existing architectural debt. -- If placement requires breaking a design rule, stop and report the conflict; - do not work around a check or silently change the approved design. -- Follow the AGENTS.md design rules; pass dependencies through constructors. -- Near 800 lines, extract a responsibility into a type with private state; - moving methods of the same type to another file does not solve the problem. -- Reuse the mechanisms listed in AGENTS.md. Check that shared APIs exist; - report a missing mechanism for planned extraction instead of duplicating it. -- Write behavior tests alongside the code. Before reporting, run the applicable - "Definition of done" commands in AGENTS.md and record each result. -- Keep the diff limited to the task — no drive-by refactors or reformatting. -- Commit only when the user has explicitly requested it and the orchestrator - relays that request; use the Conventional Commit headers from the plan. +Resolve routine implementation details within the authorized design and report +material deviations. Ask only when scope/authority changes or an unresolved +choice requires it. Do not bypass architecture limits or guards. Early warnings +invite inspection rather than automatic extraction. -Report back: what changed (files), what you ran and the results, deviations from the plan, and anything left undone. +Write behavior tests with the code and use the smallest useful check after a +coherent edit. Keep one compact progress/evidence record when needed; detailed +step files are optional. Reuse valid checks and report pending completion checks +under the input-based Definition of done. Do not launch an independent full +pipeline at every handoff. + +Report changed files, commands/results, meaningful deviations and anything left +undone. Commit, push or open a PR only when the user explicitly requests it. diff --git a/.claude/agents/reviewer.md b/.claude/agents/reviewer.md index df04dd68..13bf5f4c 100644 --- a/.claude/agents/reviewer.md +++ b/.claude/agents/reviewer.md @@ -1,41 +1,28 @@ --- name: reviewer -description: Pipeline stage 4. Reviews changes against the recorded PR target for correctness, design and convention compliance. Writes only its review file. +description: Optional review role. Reviews changes against the recorded target; writes only ignored review notes. tools: Read, Grep, Glob, Bash, Write -model: opus skills: - design-review - - gate --- -You review changes against the target recorded in the plan (`master` or `dev`). -You do not edit project files; you write only `60-review.md` in the plan -directory named in your prompt (never from Bash), using the preloaded -`design-review` table. -Read the plan files and the touched layer notes and relevant ADRs. - -Use `git diff ...HEAD` for committed changes; Git compares HEAD to its -merge base with the target. Also inspect `git diff HEAD` and relevant untracked -files, because commit authorization may follow review. Never substitute `master` -for an integration target: that includes unrelated work. If no target is supplied, -resolve it from the PR or branch plan before reviewing. - -Read enough surrounding code to judge each change in context. Check, in this order: - -1. **Correctness** — logic errors, unhandled cases, races, resource leaks, broken error handling. -2. **Plan conformance** — does it do what the plan said, and only that? -3. **Tests** — do they actually assert the behaviour, or just execute it? -4. **Design** — unnecessary complexity, duplication of existing code, leaky abstractions. -5. **Conventions** — commit headers and branch name per CONTRIBUTING.md; breaking changes marked with `!` and a `BREAKING CHANGE:` footer; no manual edits to release files. - -Every rule in the AGENTS.md sections "Design rules", "Reuse before writing", "Tests" -and "Documents" is blocking. Existing debt does not justify growth. Also block: -methods moved to another file without extracting an owner; a broad workspace trait -grown instead of a narrow consumer port; a future API described as available; -vendor and application changes mixed in one change. - -Run `.claude/hooks/cargo-serial.sh cargo xtask architecture` and report the `xtask/baseline.toml` delta; reject increases. - -Report only findings you have verified in the code. For each: `file:line`, severity (`blocking` / `should-fix` / `nit`), a concrete failure scenario, and a suggested fix. Rank most severe first. - -End with a verdict: **approve** (no blocking findings) or **changes requested**. +Review against the task's recorded branch/base and AGENTS.md's rules. Read the +plan, relevant layer notes/ADRs and enough surrounding code to assess behavior. +Use `git diff ...HEAD` for committed changes, `git diff HEAD` for staged +and unstaged changes, and relevant untracked files. Resolve an absent target +from the task/PR before reviewing; do not silently substitute master. +Load `plugin-api` for contribution/guest contracts and `performance` for hot +paths; consult `gate` to assess missing or stale verification evidence. + +Write only the supplied review location under `.claude/plans/`; never edit +project files or write files from Bash. A section in a compact plan is enough. +Apply design-review to correctness, tests, responsibility, public surface and +conventions. Design/release/debt invariants remain blocking. Vendor and app +changes may share a PR when separate commits and the prescribed merge method +preserve their separation (CONTRIBUTING.md). + +Read existing verification evidence; request only missing, stale or suspect +checks. A review pass does not turn pending checks into a pass. Report verified +findings with file:line, severity, concrete consequence and smallest fix. Return +approve or changes requested; actionable findings continue to implementation +under AGENTS.md, without an arbitrary round-count stop. diff --git a/.claude/agents/tester.md b/.claude/agents/tester.md index a6035515..c8d82292 100644 --- a/.claude/agents/tester.md +++ b/.claude/agents/tester.md @@ -1,34 +1,26 @@ --- name: tester -description: Pipeline stage 3. Verifies an implementation - runs build, lint and tests, exercises the change, and adds missing test coverage. -model: sonnet +description: Optional verification role. Runs checks selected by changed inputs and adds missing behavior coverage. skills: - behavior-tests - - gate --- -You verify a change in this repository. You may add or edit tests; you do not change production code. -Run `gate` in full mode (through `.claude/hooks/cargo-serial.sh`) and write -`50-gate.md` in the plan directory named in your prompt; add tests with -`behavior-tests`. +Verify the supplied change under AGENTS.md's input-based completion policy. +Read the task, existing evidence and affected source/configuration first. You +may add or edit tests; do not change production code. Report a defect with a +failing behavior test where practical and send production fixes back to the +implementer. +The file guard permits dedicated test files; send inline-test edits inside +production files to the implementer too. This is a role handoff, not a request +for new user approval. Load `plugin-api`/`performance` when applicable and use +`gate` for completion checks. -1. Run the applicable "Definition of done" commands from AGENTS.md, including - architecture and generated-doc checks. Record exact commands and results; - for documentation-only work, use its documented verification scope. -2. Compare the tests against the plan's test strategy. Add tests for uncovered behaviour, edge cases and error paths. -3. Exercise the change the way a user would, not just through unit tests, when that is possible. -4. Try to break it: empty input, boundaries, invalid state, concurrency where relevant. +Use the smallest affected behavior target and consumers; run broad verification +only when its impact or the assigned completion scope requires it. Reuse valid +PASS results. Add coverage for relevant boundaries, refusals, errors and races; +exercise user actions when possible. Follow behavior-tests and shared port-fake +rules rather than inspecting private model/view state. -- Name tests and files after behavior. Extend the existing file for that - behavior; create one only when the behavior has no test file. -- Drive actions, events, rendering and public methods. Do not inspect or mutate - private view/model fields to prove the fix. -- Reuse session `test-support` through dev-dependencies and extend its existing - port fakes. If a missing capability has no shared home, propose its extraction - rather than inventing a duplicate fake. -- A regression test reproduces the observable failure, not the internal state - that happened to cause it. - -If you find a defect, write a failing test that reproduces it and report it — do not fix production code yourself. - -Report back: commands run with pass/fail, tests added, defects found (with reproduction), and what you could not verify. +Record commands, scope/source/configuration, results and pending checks once in +the supplied compact plan or evidence location. Report added tests, reproduced +defects and checks that could not run. A stage handoff alone requires no full gate. diff --git a/.claude/hooks/cargo-serial.sh b/.claude/hooks/cargo-serial.sh index 586a9878..d8d59dab 100755 --- a/.claude/hooks/cargo-serial.sh +++ b/.claude/hooks/cargo-serial.sh @@ -1,17 +1,31 @@ #!/usr/bin/env bash -# Runs one Cargo command with CARGO_BUILD_JOBS=2 under one lock shared by every -# agent of this user on the machine (AGENTS.md, Hard rules). The lock is held -# by this script only: the command and anything it leaves running in the -# background do not inherit it. +# Optional command coordination; see CONTRIBUTING.md, Cargo resources. +# Caller CARGO_BUILD_JOBS and Cargo configuration are preserved. +# Default: linked worktrees share a lock in their Git common directory. +# NOCTERM_CARGO_LOCK selects another path, or "off" disables the lock. # Usage: .claude/hooks/cargo-serial.sh cargo test -p nocterm-ssh -# NOCTERM_CARGO_LOCK overrides the lock path (tests only). +# Only the wrapper holds the lock; children/background jobs do not inherit it. set -uo pipefail if [ "$#" -eq 0 ]; then echo "usage: $0 cargo " >&2 exit 64 fi -export CARGO_BUILD_JOBS=2 -lock="${NOCTERM_CARGO_LOCK:-/tmp/nocterm-cargo-$(id -u).lock}" +lock="${NOCTERM_CARGO_LOCK:-}" +if [ "$lock" = off ]; then + exec "$@" +fi +if ! command -v flock >/dev/null 2>&1; then + echo "cargo-serial: locking requires flock; install it, run Cargo directly, or set NOCTERM_CARGO_LOCK=off" >&2 + exit 69 +fi +if [ -z "$lock" ]; then + common=$(git rev-parse --git-common-dir) || { + echo "cargo-serial: outside a Git repository; set NOCTERM_CARGO_LOCK to a path or off" >&2 + exit 64 + } + common=$(CDPATH= cd -- "$common" && pwd -P) || exit 75 + lock="$common/nocterm-cargo.lock" +fi waiting() { echo "cargo-serial: waiting for another Cargo command to finish" >&2; } # Runs the command as a child that a signal to this script reaches too, and # returns its status. A background job of a non-interactive shell starts with @@ -28,16 +42,6 @@ run() { done return "$status" } -if command -v flock >/dev/null 2>&1; then - exec 9>>"$lock" || exit 75 - flock -n 9 || { waiting; flock 9 || exit 75; } - run "$@" - exit $? -fi -# Portable fallback without flock: an atomic directory, removed on exit. -until mkdir "$lock.d" 2>/dev/null; do - [ -n "${announced:-}" ] || { waiting; announced=1; } - sleep 2 -done -trap 'rmdir "$lock.d"' EXIT +exec 9>>"$lock" || exit 75 +flock -n 9 || { waiting; flock 9 || exit 75; } run "$@" diff --git a/.claude/hooks/guard_bash.py b/.claude/hooks/guard_bash.py index 1af80242..632b3ff0 100755 --- a/.claude/hooks/guard_bash.py +++ b/.claude/hooks/guard_bash.py @@ -6,8 +6,6 @@ - `master` and `dev` change only through pull requests: no commits, merges, cherry-picks, reverts or patches on them, and no pushes to them. - Tags are created by release automation only. -- Compiling Cargo commands run through `cargo-serial.sh`, which applies - `CARGO_BUILD_JOBS=2` and one lock shared by every agent of the user. - Release files and debt inventories are changed with Edit/Write, where `guard_files.py` can check them, not with shell redirection. @@ -25,12 +23,6 @@ import hooklib EVENT = "PreToolUse" -WRAPPER = "cargo-serial.sh" -COMPILING = { - "build", "b", "check", "c", "clippy", "test", "t", "run", "r", "xtask", "doc", "d", - "bench", "nextest", "llvm-cov", "install", "rustc", "fix", -} -CARGO_OPTIONS_WITH_VALUE = {"--config", "-Z", "-C", "--color"} PROTECTED = { "CHANGELOG.md", "version.txt", ".release-please-manifest.json", "xtask/baseline.toml", "xtask/oversized-files.toml", @@ -448,27 +440,6 @@ def check_git(state, assignments, words): hooklib.deny(EVENT, f"{', '.join(sorted(reached))} change only through pull requests; push the feature branch instead (CONTRIBUTING.md, Branching).") -def check_cargo(state, assignments, words): - if cargo_subcommand(words[1:]) in COMPILING: - wrapper = hooklib.project_dir(state.payload) / ".claude" / "hooks" / WRAPPER - suggestion = " ".join([*map(shlex.quote, assignments), shlex.quote(str(wrapper)), shlex.join(words)]) - hooklib.deny( - EVENT, - "Compiling Cargo commands run through the shared lock with CARGO_BUILD_JOBS=2 " - f"(AGENTS.md, Hard rules). Run it as (keeping any pipes and redirections):\n{suggestion}", - ) - - -def cargo_subcommand(arguments): - while arguments: - argument, arguments = arguments[0], arguments[1:] - if argument in CARGO_OPTIONS_WITH_VALUE: - arguments = arguments[1:] - elif not argument.startswith(("-", "+")): - return argument - return None - - def writes_protected(state, words, targets): """Whether a command rewrites a protected file in place.""" if any(state.protected(target) for target in targets): @@ -509,8 +480,6 @@ def check(state, command, depth=0): check_git(state, assignments, words) elif name == "eval": check(state, " ".join(words[1:]), depth + 1) - elif name == "cargo": - check_cargo(state, assignments, words) elif name in SHELLS and shell_script(words): check(state, shell_script(words), depth + 1) diff --git a/.claude/hooks/guard_files.py b/.claude/hooks/guard_files.py index ac44b0e8..a2ce563d 100755 --- a/.claude/hooks/guard_files.py +++ b/.claude/hooks/guard_files.py @@ -4,11 +4,11 @@ - Release files are owned by release automation and are never edited. - Debt inventories (`xtask/baseline.toml`, `xtask/oversized-files.toml`) may only shrink: new entries and raised counts are denied. -- DTO exemptions in `xtask/architecture-policy.toml` need design review, so - adding one asks the user. -- Pipeline stages keep to their role (AGENTS.md, Pipeline): `architect` and +- DTO additions and other architecture policy changes need their own design + review; isolation changes include arrays and removed restrictions. +- Delegated agents keep to their explicit role (AGENTS.md, Workflow): `architect` and `reviewer` write only their plan files; `tester` edits tests, and editing - anything else asks the user. + anything else is handed back to the implementer. """ import sys import tomllib @@ -38,9 +38,14 @@ def is_test_file(path): def check_role(agent, path): """Stage-specific write limits; `path` is None outside any checkout.""" if agent in PLAN_ONLY and not (path or "").startswith(PLANS): - hooklib.deny(EVENT, f"The {agent} stage does not write code; write its output under {PLANS}/ (AGENTS.md, Pipeline).") + hooklib.deny(EVENT, f"The {agent} stage does not write code; write its output under {PLANS}/ (AGENTS.md, Workflow; .claude/agents/).") if agent == "tester" and path and not path.startswith(PLANS) and not is_test_file(path): - hooklib.ask(EVENT, f"The tester stage changes tests only; {path} looks like production code (AGENTS.md, Pipeline).") + hooklib.deny( + EVENT, + f"The tester stage does not edit production files: {path}. Send the proposed change " + "to the implementer, including changes confined to an inline test module. " + "This is a role handoff, not a request for user approval (.claude/agents/tester.md).", + ) def flatten(document, prefix=""): @@ -67,6 +72,33 @@ def growth(before, after): return grown +def policy_review(before, after): + """DTO removals shrink exemptions; all other policy changes need review. + + Isolation lists may be allowlists or denylists, so removals cannot safely + be treated as reductions of authority. Compare parsed values, not TOML text. + """ + reasons = [] + old_dto, new_dto = before.get("dto", {}), after.get("dto", {}) + if old_dto != new_dto: + if not isinstance(old_dto, dict) or not isinstance(new_dto, dict): + reasons.append("Changing the plain-data exemption structure needs design review") + else: + changed = [key for key, value in new_dto.items() if old_dto.get(key) != value] + if changed: + reasons.append("Adding or changing plain-data exemptions needs design review: " + ", ".join(sorted(changed))) + sections = sorted( + key for key in before.keys() | after.keys() + if key != "dto" and before.get(key) != after.get(key) + ) + if sections: + reasons.append( + "Changing architecture policy sections needs design review, including isolation " + "permissions and restrictions: " + ", ".join(sections) + ) + return reasons + + def parse(text): try: return tomllib.loads(text) @@ -97,11 +129,14 @@ def main(): hooklib.deny(EVENT, f"{path} would no longer be valid TOML.") if before is None: return + if path == POLICY: + reasons = policy_review(before, after) + if reasons: + hooklib.ask(EVENT, "; ".join(reasons) + " (docs/architecture/guardrails.md).") + return grown = growth(before, after) if not grown: return - if path == POLICY: - hooklib.ask(EVENT, "Adding a plain-data exemption needs design review (docs/architecture/guardrails.md): " + "; ".join(grown)) hooklib.deny( EVENT, f"{path} is an inventory that may only shrink (ADR 0012, AGENTS.md design rule 6). " diff --git a/.claude/hooks/hooklib.py b/.claude/hooks/hooklib.py index 49f05178..74ab0dd1 100755 --- a/.claude/hooks/hooklib.py +++ b/.claude/hooks/hooklib.py @@ -4,7 +4,6 @@ deterministic guards for rules in AGENTS.md; CI remains the authoritative gate. """ import json -import os import sys from pathlib import Path @@ -13,10 +12,6 @@ def event(): return json.load(sys.stdin) -def project_dir(payload): - return Path(os.environ.get("CLAUDE_PROJECT_DIR") or payload.get("cwd") or ".").resolve() - - def absolute(payload, path): path = Path(path) if not path.is_absolute(): @@ -63,6 +58,33 @@ def resulting_text(payload): return text +def applied_edits(payload): + """Exact fragments of a successful Edit/MultiEdit; Write has no old text.""" + data = payload.get("tool_input") or {} + if payload.get("tool_name") == "Edit": + return [data] + if payload.get("tool_name") == "MultiEdit": + return data.get("edits") + return None + + +def previous_text(payload, text): + """Undo only unambiguous replacements to recover a post-edit predecessor. + + Repeated replacement text, deletions and replace_all are ambiguous without + a pre-edit snapshot. Leave them unknown rather than guessing the edit site. + """ + edits = applied_edits(payload) + if not edits: + return None + for edit in reversed(edits): + old, new = edit.get("old_string"), edit.get("new_string") + if old is None or not new or edit.get("replace_all") or text.count(new) != 1: + return None + text = text.replace(new, old, 1) + return text + + def deny(event_name, reason): print(json.dumps({ "hookSpecificOutput": { diff --git a/.claude/hooks/post_edit.py b/.claude/hooks/post_edit.py index 3adda9c0..01a84a28 100755 --- a/.claude/hooks/post_edit.py +++ b/.claude/hooks/post_edit.py @@ -3,11 +3,13 @@ Adds context for the agent; it never blocks. The signals mirror the early warnings of `cargo xtask architecture` and the design-review skill: -- a Rust file at or above 640 lines needs a split designed by responsibility; +- crossing 640 lines or another 40-line band invites responsibility inspection; - a new Rust file holding only `impl` blocks moves methods without an owner; - a catch-all module name hides a responsibility; -- a new crate manifest starts the crate-new workflow. +- a new crate manifest starts the crate-new workflow; +- a plugin declaration, the plugin model or the plugin catalog follows the plugin-api skill. """ +import difflib import re import sys from pathlib import Path @@ -17,21 +19,84 @@ EVENT = "PostToolUse" WARN_LINES = 640 LIMIT = 800 +WARN_STEP = 40 ROOTS = ("src/", "crates/", "xtask/") GENERIC = {"utils", "util", "helpers", "helper", "misc", "common", "stuff", "extra", "ext"} DEFINITION = re.compile(r"^\s*(?:pub(?:\([^)]*\))?\s+)?(?:struct|enum|union|trait|type)\s+\w", re.MULTILINE) +PLUGIN_CONTRACT = re.compile(r"(?:^|/)plugin\.rs$|^crates/plugin-(?:model|api|abi|ports)/") +PLUGIN_REGISTRATION = re.compile( + r"\b(?:PLUGIN|PLUGINS|CORE|PluginCatalog|SystemPlugin|SystemContribution|" + r"register_panel|register_widget|nocterm_plugin_api)\b|\bplugin::catalog\b" +) IMPL = re.compile(r"^\s*(?:unsafe\s+)?impl\b", re.MULTILINE) -def signals(path, text, created): +def line_count(text): + return text.count("\n") + (0 if text.endswith("\n") or not text else 1) + + +def size_warning(text, payload): + lines = line_count(text) + if lines > LIMIT: + return True + if lines < WARN_LINES: + return False + before = hooklib.previous_text(payload, text) + if before is not None: + return lines // WARN_STEP > line_count(before) // WARN_STEP + edits = hooklib.applied_edits(payload) + if edits and all(not edit.get("replace_all") for edit in edits): + # A deletion or ambiguous replacement can still prove there was no + # line growth. At most one trailing unterminated line can be added. + delta = sum( + edit.get("new_string", "").count("\n") - edit.get("old_string", "").count("\n") + for edit in edits + ) + same_endings = all( + edit.get("old_string") and edit.get("new_string") + and edit["old_string"].endswith("\n") == edit["new_string"].endswith("\n") + for edit in edits + ) + if delta < 0 or (delta == 0 and same_endings): + return False + # Write supplies no predecessor. Unknown edits remain conservative; no + # persistent session cache is needed to suppress known unchanged/shrinking edits. + return True + + +def changed_code(payload, text): + edits = hooklib.applied_edits(payload) + lines = [] + if edits is None: + lines = text.splitlines() + else: + for edit in edits: + before = edit.get("old_string", "").splitlines() + after = edit.get("new_string", "").splitlines() + for kind, start, end, new_start, new_end in difflib.SequenceMatcher(None, before, after).get_opcodes(): + if kind != "equal": + lines.extend(before[start:end]) + lines.extend(after[new_start:new_end]) + # This is a routing hint, not a Rust parser or a replacement for review. + return "\n".join(line for line in lines if line.strip() and not line.lstrip().startswith("//")) + + +def signals(path, text, created, payload): found = [] if path.endswith(".rs") and path.startswith(ROOTS): - lines = text.count("\n") + (0 if text.endswith("\n") or not text else 1) - if lines >= WARN_LINES: + lines = line_count(text) + if size_warning(text, payload): + status = "Hard limit exceeded" if lines > LIMIT else "Advisory size warning" + instruction = ( + f"Reduce the file to at most {LIMIT} lines; split production code by responsibility. " + if lines > LIMIT else + "Inspect its responsibility and remaining room; use responsibility-map if separation is unclear. " + "A warning alone does not require extraction. " + ) found.append( - f"{path} has {lines} lines (warn at {WARN_LINES}, limit {LIMIT}). Before it grows, run the " - "responsibility-map skill on its main type and extract an owner with private state; " - "do not cut the file at the limit or move methods of the same type to another file." + f"{status}: {path} has {lines} lines (advisory at {WARN_LINES}, hard limit {LIMIT}). " + f"{instruction}If a split is needed, extract an owner, " + "rather than moving methods of the same type to another file." ) stem = Path(path).stem if created and stem in GENERIC: @@ -44,6 +109,13 @@ def signals(path, text, created): f"{path} only implements types declared elsewhere. Moving methods of a parent type to a new " "file is file slicing, not an owner extraction: use the owner-extract skill (AGENTS.md design rules 1 and 7)." ) + changed = changed_code(payload, text) + if changed and (PLUGIN_CONTRACT.search(path) or PLUGIN_REGISTRATION.search(changed)): + found.append( + f"{path} declares, models or composes plugins. Follow the plugin-api skill: system plugins are " + "`pub static PLUGIN`, core owners declare `CORE`, registration goes through the workspace " + "contract, and nothing is exposed to user plugins by a getter or a `pub` item." + ) if created and re.fullmatch(r"crates/[^/]+/Cargo\.toml", path): found.append( f"{path} starts a new crate. Follow the crate-new skill: placement, ownership card, layer metadata, " @@ -64,7 +136,7 @@ def main(): text = hooklib.absolute(payload, data.get("file_path")).read_text(encoding="utf-8") except (FileNotFoundError, UnicodeDecodeError): return - found = signals(path, text, created) + found = signals(path, text, created, payload) if found: hooklib.context(EVENT, "\n".join(found)) diff --git a/.claude/skills/behavior-tests/SKILL.md b/.claude/skills/behavior-tests/SKILL.md index fd367807..da9dc38f 100644 --- a/.claude/skills/behavior-tests/SKILL.md +++ b/.claude/skills/behavior-tests/SKILL.md @@ -13,8 +13,9 @@ and so do files named after review rounds. - **characterize** (before a refactor): cover every behaviour that passes through the code about to move, run the tests on the **unchanged** code and - see them pass. Record the test names in `40-steps.md`; they must stay - unedited through the refactor. + see them pass. Record relevant tests in the compact plan. Preserve scenarios + and assertions through the refactor; mechanical import, location and fixture + adaptations are allowed. Explain any change to expected behavior separately. - **cover** (new or changed behaviour): one test per rule or outcome, including refusals and errors (`typed-errors` variants), written red-green where practical. @@ -41,14 +42,16 @@ and so do files named after review rounds. `gpui_kit` instead of adding another copy. - Assert outcomes the user or caller sees, not call sequences: the rendered state, the event, the persisted document, the returned error variant. + When reducing external work is itself the requirement, counters on shared + ports may assert requests/bytes avoided; use `performance` for cost evidence. ## Run -Through the shared lock, narrowest first: +Choose the narrowest useful check; Cargo resources follow CONTRIBUTING.md: ```sh -.claude/hooks/cargo-serial.sh cargo test -p +cargo test -p ``` -End with the Result block listing tests added and their pass state on the -unchanged code (characterize) or after the change (cover). +Report tests added and their pass state on unchanged code (characterize) +or after the change (cover), in the shared plan or handoff. diff --git a/.claude/skills/crate-extend/SKILL.md b/.claude/skills/crate-extend/SKILL.md index b8c1a67d..5c9e57cf 100644 --- a/.claude/skills/crate-extend/SKILL.md +++ b/.claude/skills/crate-extend/SKILL.md @@ -1,51 +1,28 @@ --- name: crate-extend -description: Workflow for adding behaviour to an existing crate — confirm it fits the crate's card, check the touched files and types against early warnings, make room with a refactor first if needed, then build, verify and review. Use for most features and non-trivial changes inside one crate. +description: Add behavior to an existing crate, checking ownership, affected APIs and tests; refactor only where the design or hard limits require it. argument-hint: " [crate] [target: master|dev]" --- -# Workflow: extend a crate +# Extend a crate Task: $ARGUMENTS -You orchestrate; pass the plan directory, target branch and verification scope -to every subagent (`.claude/skills/feature/plan-bus.md`). - -## 0. Task - -Write `00-task.md`: change, target, branch `/`, verification scope, -success metric (the behaviour, plus "no new early warnings for touched files" -or the specific counts if a split is needed). - -## 1. Design (architect) - -1. `placement` → `10-placement.md`. If the change belongs to another crate or a - new crate, switch to that workflow; if it spans layers, split it per layer. -2. Survey the target: `.claude/hooks/cargo-serial.sh cargo xtask survey `. - For each file or type the change will grow, check its early warning: - - a type that would exceed 12 fields, a file that would exceed 640 lines or a - type in 4 impl files → run `responsibility-map` and plan the extraction - **before** the feature (it becomes the first steps, or a separate - `refactor` run on its own branch if large); - - under the thresholds → continue. -3. `port-design` for any new collaborator; `typed-errors` for new fallible APIs; - `wiring` for new settings, persistence or installation changes. -4. `docs-adr` decision (usually: card update only). -5. `30-design.md` including expected field and line counts after the change. - -Show `30-design.md` to the user and wait for approval. - -## 2. Build (implementer) - -Make room first (`behavior-tests` characterize → `owner-extract`), then the -change with `behavior-tests` cover. `gate` quick after each step; `40-steps.md`. - -## 3. Verify (tester) — `gate` full; add missing tests. - -## 4. Review (reviewer) — `design-review` → `60-review.md`; two-round limit. - -## 5. Wrap up - -Report gate results, verdict, baseline delta, early warnings before/after, and -proposed commits (refactor commits separate from the feature commit). Commit -only if the user asked. +Follow AGENTS.md's Workflow and Definition of done. One agent and one compact +plan are sufficient for scoped work; use [the handoff contract](../feature/plan-bus.md) +when delegating. Record behavior, target/base, branch, affected crate/layer and checks. + +1. Use `placement` to confirm the behavior fits the crate's ownership card and + allowed dependencies. Separate responsibilities across layers where needed. +2. Inspect the files/types that will grow. Use `cargo xtask survey ` + and `responsibility-map` when ownership is unclear. Early warnings are + advisory: explain consequential growth, but extract only where responsibility + or hard limits require it. Do not require a no-new-warnings metric. +3. Design new collaborators with `port-design`, fallible APIs with `typed-errors`, + and installation/persistence changes with `wiring`; apply `docs-adr` as needed. +4. Implement with `behavior-tests`. If extraction is needed, characterize behavior + first and use `owner-extract`; keep a cohesive fix on one branch where useful. +5. Use `gate` for affected behavior and consumers. Review with `design-review`, + fix findings, then finish the applicable input-based completion checks. +6. Report behavior, checks/results, relevant boundary/count changes and remaining + limitations. Commit/push/PR only when requested. diff --git a/.claude/skills/crate-new/SKILL.md b/.claude/skills/crate-new/SKILL.md index f7f720aa..10f23824 100644 --- a/.claude/skills/crate-new/SKILL.md +++ b/.claude/skills/crate-new/SKILL.md @@ -1,72 +1,30 @@ --- name: crate-new -description: Workflow for adding a new workspace crate — placement proves a new crate is needed, the design fixes its card, ports, errors and wiring, then build, verify and review. Use when a responsibility has no fitting crate or the user asks for a new crate. +description: Add a workspace crate when placement establishes a distinct responsibility, with ownership card, allowed dependencies, typed ports and behavior coverage. argument-hint: " [target: master|dev]" --- -# Workflow: new crate +# New crate Task: $ARGUMENTS -You orchestrate; procedure skills do the work and write the plan directory -(`.claude/skills/feature/plan-bus.md`). Pass the plan directory path, the -target branch and the verification scope to every subagent. - -## 0. Task - -Create `.claude/plans//00-task.md`: responsibility, target (`master` -unless the user says `dev`), branch `feat/` or `refactor/`, -verification scope (full gate), success metric (crate exists with a card, -layer and tests; the code it replaces, if any, is gone from the old crate). - -## 1. Design (architect) - -Spawn `architect` to run, in order: - -1. `placement` → `10-placement.md`. It must conclude **new crate** with at least - two criteria met. If it concludes "module of an existing crate", stop and - switch to `crate-extend`. -2. `port-design` for every capability the crate uses or provides. -3. `docs-adr` decides the documents: an ADR when the crate takes a - responsibility from another crate or adds a port between layers; otherwise - its card and layer-note entry. -4. `30-design.md` with: crate name `nocterm-` in `crates/`, layer, - draft card, public surface, error type, dependencies (checked against the - layer table), wiring step, tests per layer, ADR, commit plan. - -Show `30-design.md` to the user and wait for approval (AGENTS.md gate). - -## 2. Build (implementer) - -Skeleton, in one step: - -- `crates//Cargo.toml` like `crates/containers/Cargo.toml`: `edition`, - `license-file` and `lints` from the workspace, `publish = false`, a - `description`, and `[package.metadata.nocterm] layer = ""`. -- An entry `nocterm- = { path = "crates/" }` under - `[workspace.dependencies]` in the root `Cargo.toml` (`crates/*` are members). -- `crates//src/lib.rs` starting with the card (`Owns:`, `Does not own:`, `Ports`, - `Settings`, `Globals`, `Invariants`), modules named for what they own, - `error.rs` (`typed-errors`). -- `test-support` fakes behind a feature, used only from dev-dependencies. - -Then each behaviour with `behavior-tests` (cover), `wiring` if the crate is -installed by the app, and `gate` quick after each step. Run `cargo xtask docs` -(dependency graph changes) and record steps in `40-steps.md`. - -## 3. Verify (tester) - -`gate` full, `behavior-tests` cover for gaps, plus `cargo machete ---skip-target-dir` and the license-notice check (new manifest). If notices are -stale, regenerate them (`python3 scripts/generate-license-notices.py`). - -## 4. Review (reviewer) - -`design-review` → `60-review.md`. Blocking findings go back to the -implementer; after two failed rounds escalate. - -## 5. Wrap up - -Report each gate command with its result, the review verdict, the baseline -delta, and proposed commits (`feat(): …` / `refactor(): …`, ADR as -`docs: …`). Commit only if the user asked. +Follow AGENTS.md's Workflow and Definition of done. Record responsibility, +branch/base, layer, public surface, dependencies, risks and check scope in a +compact plan; [structured handoffs](../feature/plan-bus.md) are optional. + +1. `placement` must justify a new crate with its criteria; otherwise use a module + and `crate-extend`. Use `port-design`/`typed-errors` for collaborators and + fallible APIs, `wiring` for installation, and `docs-adr` for the boundary decision. +2. Add `crates//Cargo.toml` like `crates/containers/Cargo.toml`: inherited + edition/license/lints, `publish = false`, description and + `[package.metadata.nocterm] layer = ""`. Add its path dependency under + root `[workspace.dependencies]`; `crates/*` supplies workspace membership. +3. Start `lib.rs` with the ownership card (Owns, Does not own, Ports, Settings, + Globals, Invariants). Name modules after responsibilities. Add typed errors + for fallible APIs and feature-gated test-support fakes where needed. +4. Implement behavior with `behavior-tests` and explicit dependencies. Apply + scoped `gate` checks after coherent edits; regenerate docs once dependency + changes settle. Include dependency/notice checks for the new manifest. +5. Review with `design-review`, fix findings and finish the applicable input-based + completion checks. Report responsibility, boundary changes, evidence and + limitations. Commit/push/PR only when requested. diff --git a/.claude/skills/crate-split/SKILL.md b/.claude/skills/crate-split/SKILL.md index e65d784c..8d5ba09c 100644 --- a/.claude/skills/crate-split/SKILL.md +++ b/.claude/skills/crate-split/SKILL.md @@ -1,78 +1,32 @@ --- name: crate-split -description: Workflow for moving a module or responsibility out of a crate into a new or existing crate — survey module references, decide the target layer per piece, escalate layer conflicts, move behind characterization tests, update cards and record an ADR. Use when a crate owns several subjects or a module's dependencies belong to another layer. +description: Move a responsibility between crates, checking layer dependencies, narrow contracts, characterization coverage and an ADR for the new boundary. argument-hint: "[::] [target: master|dev]" --- -# Workflow: split a crate +# Split a crate Subject: $ARGUMENTS -Crate boundaries change, so this workflow always ends with an ADR. Pass the -plan directory, target branch and verification scope to every subagent -(`.claude/skills/feature/plan-bus.md`). - -## 0. Task - -`00-task.md`: subject, target, branch `refactor/`, metric (for example -"`nocterm-agent` lines −N; new crate `nocterm-` owns ``; no -`crate::` references left in the old crate"). - -## 1. Survey and placement (architect) - -1. `responsibility-map` on the crate. Use the `modules` section: a candidate - module has most references to one or two workspace crates and few to its - siblings (`crate::…`). -2. For each candidate run `placement`, using the references as the dependency - list. Every dependency must be allowed by the candidate's layer. - -### Layer conflicts: stop and escalate - -When a candidate's dependencies span layers, it is not one responsibility yet. -Example from `nocterm-agent` (feature): its `thread` module references -`nocterm_ai` (domain) 162 times and `nocterm_workspace` (ui) 62 times, and -holds GPUI state with background work. As a service crate it could not depend -on `nocterm-workspace`; as a feature crate it would be a second agent feature -that the first must not depend on. Escalate with the options instead of -choosing: - -- split the module first: the domain part (transcript rules, `nocterm_ai` - types) moves down; the workspace-facing part stays in the feature; -- move the narrow contract it needs from `nocterm-workspace` into a lower - crate (needs its own ADR); -- keep the module and run `refactor` inside the crate instead. - -Write the conflict and options to `10-placement.md` and return `escalate`. - -3. `port-design` for each reference that crosses the new boundary in both - directions; reject a cycle (the old crate and the new one must not depend on - each other through the same API). -4. `docs-adr`: draft the ADR (context, decision, alternatives) in - `30-design.md`; it is written to `docs/adr/` in the build stage. -5. `30-design.md`: target crate (new via the `crate-new` skeleton, or existing), - files moved, public surface, ports, wiring change, step order, commits. - -Show the design to the user and wait for approval. - -## 2. Move (implementer) - -1. `behavior-tests` characterize through the old crate's public API. -2. Create the target crate (`crate-new` skeleton) or a module in the existing - one; move code in compiling steps; re-export nothing from the old crate as - a compatibility shim unless the design says so, with a removal step. -3. Move tests with the code they cover; the characterization tests stay green. -4. Update both cards, the layer note, `cargo xtask docs`, and write the ADR. -5. `gate` quick after each step; `40-steps.md`. - -## 3. Verify (tester) - -`gate` full plus `cargo machete --skip-target-dir` and the license-notice -check. Rerun the survey on both crates against the metric. - -## 4. Review (reviewer) — `design-review`; two-round limit. - -## 5. Wrap up - -Report metric before/after, gate results, ADR number, baseline delta, verdict -and proposed commits (crate creation, each move, ADR). Commit only if the user -asked. +Follow AGENTS.md's Workflow and Definition of done. Record branch/base, ownership +move, metric, risks and checks in a compact plan; use [structured handoffs](../feature/plan-bus.md) +when helpful. A crate-boundary change requires an ADR. + +1. Survey ownership and references with `responsibility-map`; apply `placement` + to each piece and check its dependency layer. A module spanning incompatible + layers needs to be separated, not placed in the nearest crate. +2. Resolve boundary problems within the authorized design: split domain and + presentation responsibilities, move a narrow contract down, or keep the + responsibility in its existing crate. Present options to the user only when + a material choice remains or the solution changes authorized scope/authority. +3. Use `port-design` for cross-boundary collaborators and reject dependency cycles. + Design target crate/module, public surface, typed errors, wiring, move order, + tests and the `docs-adr` decision. Use the `crate-new` skeleton if needed. +4. Characterize through the old API, move in coherent compiling steps and keep + tests with the code they cover. Avoid compatibility shims unless the design + requires one and gives its removal condition. +5. Update ownership cards, layer notes and ADR; regenerate references when the + combined dependency changes settle. Use `gate` for both sides and consumers. +6. Review with `design-review`, fix findings and finish the applicable completion + checks. Report ownership/metric changes, checks/results and limitations. + Vendor and app changes require separate commits; commit/push/PR only on request. diff --git a/.claude/skills/debt-paydown/SKILL.md b/.claude/skills/debt-paydown/SKILL.md index 7d93c35d..8236972a 100644 --- a/.claude/skills/debt-paydown/SKILL.md +++ b/.claude/skills/debt-paydown/SKILL.md @@ -17,7 +17,7 @@ branch and verification scope to every subagent If no entry is given, list candidates from `xtask/baseline.toml` and `xtask/oversized-files.toml` with `cargo xtask survey ` facts and propose the cheapest one with the most value (small counts first). Record the -entry, its current value and the target value in `00-task.md`; branch +entry, its current value and the target value in the compact plan (or `00-task.md`); branch `refactor/`. ## 1. Route by kind @@ -31,8 +31,10 @@ entry, its current value and the target value in `00-task.md`; branch | `domain_io`, `domain_dependencies` | `port-design` (contract in the domain, implementation in an adapter) | | `complexity_expects` | split the function by the decisions it makes, or extract an owner; never move the `#[expect]` | -Follow the chosen workflow's design approval, build, verify and review -stages. +Follow the relevant procedures under AGENTS.md's Workflow and input-based +Definition of done. Use one compact plan or structured handoffs as useful; +record the entry and goal without requiring every stage or output file. +Changing a baseline entry does not by itself require another full pipeline. ## 2. Lower the record diff --git a/.claude/skills/design-review/SKILL.md b/.claude/skills/design-review/SKILL.md index 37e228d1..f6e62309 100644 --- a/.claude/skills/design-review/SKILL.md +++ b/.claude/skills/design-review/SKILL.md @@ -6,13 +6,36 @@ user-invocable: false # Design review -Input: the diff against the recorded target, including uncommitted changes -(`git diff ...` plus `git diff` and untracked files), and the plan -directory. Output: `60-review.md`. +Input: the task, recorded target and existing evidence. Inspect committed changes +with `git diff ...HEAD`, staged and unstaged changes with `git diff HEAD`, +and relevant untracked files from `git ls-files --others --exclude-standard`. +Read surrounding owners/consumers as needed. Record findings in the compact plan. -Automated checks already cover limits and debt counts; this review covers -intent. Read the plan's `10-placement.md`, `20-map.md` and `30-design.md` -first and check the code against them. +Automated checks cover limits and debt counts; this review covers intent. +Inspect existing scoped/final evidence and pending checks. Do not rerun full +architecture or tests just because review started; the `gate` skill governs +reuse and any missing/suspect check. Finish the applicable input-based Definition +of done after review fixes. Read relevant plan/placement/map notes when present +and check the code against the authorized task. + +## Ownership and cost + +For changed state or boundaries, trace one actual use case through its owners: + +- Who owns the authoritative state, its invariant and all transitions? Does the + proposed split reduce coupling, or just rearrange fields to satisfy a count? +- Who creates/releases subscriptions, tasks and resources? What happens on + close, cancellation, account/session changes and delivery of a stale result? +- Is each module cohesive and understandable? Independent responsibilities need + separation even below limits; small pure modules need no artificial owner. +- For affected hot paths, is work bounded and proportional to changed/visible + data? Inspect redraw/notification fan-out and cache invalidation. Use + `performance` for relevant runtime/GUI evidence; do not demand unrelated benchmarks. + +Structural PASS is evidence about measured rules, not semantic approval. The +current public-field metric covers feature/service; impl spread is counted above +four files; ownership cards are checked for labels. Review the design rules even +where those checks do not report a violation. ## Anti-patterns @@ -33,12 +56,16 @@ first and check the code against them. | 13 | A duplicated non-fake test helper (for example a `click` wrapper) | advisory | | 14 | Crate card, layer note or ADR not updated for a boundary change | blocking for boundary changes | | 15 | `#[allow]`/`#[expect]` for `too_many_lines`/`cognitive_complexity`; raised baseline; new oversized entry | always | -| 16 | A file or type that crossed an early warning in this change with no map or split plan | advisory | +| 16 | A touched responsibility near a hard limit whose growth has not been considered | advisory | +| 17 | A contribution bypassing its `PLUGIN`/`CORE` catalog or registration, a panel entity held outside the workspace, an eagerly created widget, or a native getter misrepresented as guest capability access | blocking; honor the explicit core/required-panel exceptions in `plugin-api` | +| 18 | Mirrored authoritative state, stale async delivery, missing cancellation/teardown or cache invalidation | blocking when a concrete incorrect transition or unbounded resource lifetime is possible | +| 19 | Blocking/expensive work added to render/input paths, redundant full refreshes, unbounded background work or caches | blocking for a demonstrated regression or an identifiable unbounded path; give the workload and consequence | -For each finding give: file:line, the rule (AGENTS.md section or ADR), the -concrete consequence, and the smallest fix. +For each finding give file:line, the rule (AGENTS.md section or ADR), a triggering +scenario/consequence and the smallest fix. Distinguish evidence from hypotheses; +an unmeasured speedup is not a verified result. Style preferences alone are advisory. -## Output: `60-review.md` +## Review to record ``` # Review against @@ -49,5 +76,5 @@ concrete consequence, and the smallest fix. Verdict: approve | changes requested ``` -Changes requested go back to the implementer; after two failed rounds escalate -to the user (AGENTS.md "Pipeline"). +Fix actionable findings under AGENTS.md's Workflow. Escalate material unresolved +choices or concrete blockers, without a fixed round count. diff --git a/.claude/skills/docs-adr/SKILL.md b/.claude/skills/docs-adr/SKILL.md index a1155ccb..6fcbedc9 100644 --- a/.claude/skills/docs-adr/SKILL.md +++ b/.claude/skills/docs-adr/SKILL.md @@ -47,6 +47,8 @@ Status: accepted Add a row to `docs/adr/README.md` (`| [NNNN](NNNN-slug.md) | |`). Accepted records are never edited; a later record supersedes them and says so. +For supersession, link both directions in the mutable index and name which +parts changed, so readers can find the current rule without reading all history. No session status, dates of work, or "implemented by" notes. ## 3. Cards and notes @@ -58,16 +60,18 @@ No session status, dates of work, or "implemented by" notes. or leaves the layer or a port between layers changes. - `cargo xtask docs` regenerates `docs/reference/*` and `docs/architecture/dependencies.md`; run it after changing settings, tokens, - actions, keymap or internal Cargo dependencies. + actions, keymap or internal Cargo dependencies, once the combined inputs + settle. Prose-only changes do not require Cargo generators or architecture + scans; use the documentation scope in `gate`. ## 4. Stale references For each removed or renamed symbol, module or crate: ```sh -grep -rn "" docs/ AGENTS.md CONTRIBUTING.md .claude/skills .claude/agents +rg -n "" docs/ AGENTS.md CONTRIBUTING.md .claude/skills .claude/agents ``` Update current documents; leave accepted ADRs as historical text. -End with the Result block listing documents written or changed. +Report documents written or changed in the shared plan or handoff. diff --git a/.claude/skills/feature/SKILL.md b/.claude/skills/feature/SKILL.md index 0ada63ed..844ab5e2 100644 --- a/.claude/skills/feature/SKILL.md +++ b/.claude/skills/feature/SKILL.md @@ -1,52 +1,42 @@ --- name: feature -description: Run the full agent pipeline (architect → implementer → tester → reviewer) for a feature or non-trivial change. Use when the user asks to build a feature end to end or invokes /feature. +description: Carry a feature or non-trivial change through design, implementation, verification and review, scaling delegation and planning to the task. Use for end-to-end work or /feature. --- -Drive the pipeline described in AGENTS.md for the task: $ARGUMENTS +Task: $ARGUMENTS -You are the orchestrator. Subagents start with no context, so every prompt must -carry the task, plan directory, branch name, target/base branch, verification scope -and prior findings, plus paths to docs/ARCHITECTURE.md, relevant layer notes and -docs/adr/README.md (AGENTS.md is loaded automatically). Decisions live in -`.claude/plans//` as described in [plan-bus.md](plan-bus.md); every stage -reads the files earlier stages wrote and ends with its Result block. +Follow AGENTS.md's Workflow and Definition of done. Use one agent for scoped work; +delegate distinct responsibilities when complexity, risk or independent work +benefits from it. The role files in `.claude/agents/` are optional and inherit +caller model settings. Never assume a delegate has unseen context: supply task, +branch/base, plan/evidence location, scope and relevant prior findings. -0. **Route** — classify the task and follow the matching workflow skill instead - of the generic stages below when one fits: +1. Record a compact goal, approach, affected crates/layers, risks and check scope. + Use [plan-bus.md](plan-bus.md) for optional structured handoffs. Honor an + existing authorized plan; resolve only material unanswered choices. + For changed ownership, name the invariant, transitions and lifetime; for + consequential alternatives, compare correctness, reuse, cost and maintenance. +2. Select relevant procedures rather than launching every workflow: - | Task | Workflow | + | Need | Procedure/workflow | | --- | --- | - | responsibility with no fitting crate, or "new crate" | `crate-new` | - | behaviour added inside one crate | `crate-extend` | - | restructure inside a crate, early warning or limit hit | `refactor` | - | move a module or subject to another crate | `crate-split` | - | shrink an `xtask/baseline.toml` or oversized-file entry | `debt-paydown` | + | Responsibility with no fitting crate | `crate-new` | + | Behavior inside an existing crate | `crate-extend` | + | Restructure within a crate | `refactor` | + | Move ownership across crates | `crate-split` | + | Shrink a debt inventory entry | `debt-paydown` | + | Rendering, caches or other hot paths | `performance` | - Spanning several rows: run them as separate branches in the order - refactor → split → extend. None fits (cross-cutting, app-only): continue - with the stages below, using the procedure skills each agent preloads. - -1. **Design** — spawn `architect` with the task and reading paths above. Require - placement, shape, public surface, reuse, typed errors, docs/ADR and the target - branch in the plan. Show it to the user and wait for approval; an explicit - instruction to implement an already supplied plan supplies that approval. - Relay unresolved design questions. -2. **Branch** — create `/` from the recorded target: `master` for - release-ready changes or `dev` for integration, per CONTRIBUTING.md. Verify - the local base against its remote; do not reset or overwrite unrelated work. -3. **Build** — spawn `implementer` with the full approved plan. -4. **Verify** — spawn `tester` with the plan and the implementer's report. If it reports defects or red checks, send them to `implementer` and verify again. -5. **Review** — spawn `reviewer` with the plan and target. Include committed, - uncommitted and untracked task changes; review must finish before requesting - commit authorization. Require the blocking design checklist. Send `blocking` - and `should-fix` findings to `implementer`, then re-run Verify and Review. -6. **Stop conditions** — after two failed Verify or Review rounds, stop and escalate to the user with the outstanding findings. -7. **Wrap up** — summarise what was built, each applicable verification command - and result, review verdict and remaining nits. Propose Conventional Commit - headers and a PR title. Include the target branch and the `xtask/baseline.toml` - delta in the proposed PR description. Keep vendor changes separate from - application changes. Commit, push and open the PR only if the user asks. - -Documentation-only changes skip subagents under AGENTS.md and use its documented -verification scope. + A cohesive change may combine them on one branch; an early warning alone + does not require a refactor workflow. Split independently reviewable work + when useful, respecting the user's requested base/branch and existing work. +3. Implement the authorized design with behavior tests. Use `gate` scoped mode + after coherent edits where a check answers a concrete uncertainty. +4. Review committed, uncommitted and relevant untracked task changes against the + recorded target. Use `design-review`; fix actionable findings and verify + affected inputs. Escalation follows AGENTS.md, without a fixed round count. +5. Assign one completion executor when working in parallel. Finish the + input-based Definition of done after review fixes; preserve valid evidence. +6. Report what changed, commands/results, scope and remaining limitations. + Propose commit/PR text only when useful. Vendor and app changes belong in + separate commits; commit, push and PR actions require the user's request. diff --git a/.claude/skills/feature/plan-bus.md b/.claude/skills/feature/plan-bus.md index bb24499b..d9c68642 100644 --- a/.claude/skills/feature/plan-bus.md +++ b/.claude/skills/feature/plan-bus.md @@ -1,39 +1,46 @@ -# Plan directory contract - -Subagents start without context, so every workflow keeps its decisions in one -ignored directory, `.claude/plans//`, and each step reads what earlier -steps wrote. Chat summaries are not a substitute: if a decision is not in the -directory, the next step does not know it. - -| File | Written by | Contents | -| --- | --- | --- | -| `00-task.md` | workflow | task, workflow name, target branch (`master`/`dev`), feature branch, verification scope, **success metric** (from `cargo xtask survey`, e.g. "`Thread` fields 15 → ≤ 9; files ≥ 640 lines in `thread/`: 0") | -| `10-placement.md` | `placement` | crate, layer, reason, card check, new-crate decision | -| `20-map.md` | `responsibility-map` | survey facts, matrix interpretation, candidate owners with draft cards | -| `30-design.md` | architect / workflow | owners, ports, errors, wiring, tests, docs/ADR, commit plan; approved by the user | -| `40-steps.md` | implementer | ordered steps with `- [ ]` / `- [x]`, one transformation each, gate result per step | -| `50-gate.md` | `gate` | each command, its result, baseline delta, early warnings before/after | -| `60-review.md` | `design-review` / reviewer | findings (`file:line`, severity, scenario, fix) and verdict | - -Rules: - -- Create the directory in the first step; never write plans into `docs/`. -- Append, do not rewrite, earlier decisions; a changed decision records why. -- A step is complete only when its file exists with the sections above filled. - -## Result block - -Every procedure skill ends its turn with this block, and the workflow checks it -before the next step: - -``` -## Result -status: done | blocked | escalate -wrote: -next: -escalation: +# Plans and handoffs + +Use one compact document under ignored `.claude/plans//` by default. +Record the goal, branch/base, scope, approach, material decisions and risks, +checks/results and remaining work. Scale detail to the task. No plan files are +required for trivial edits; AGENTS.md governs authorization and completion. +For ownership changes include state, invariant, transitions and lifetime. For +hot-path changes include the representative workload and cost evidence when +applicable; these are short sections, not additional stages or mandatory files. + +Delegates receive the task and relevant context or a plan location explicitly. +Do not assume either that every tool shares history or that every delegate +starts without it. Record durable decisions that the next owner needs; avoid +copying the same evidence into several reports. + +For larger work, the following are optional sections or separate files: + +| Section / suggested file | Useful content | +| --- | --- | +| Task / `00-task.md` | goal, branch/base, scope, behavior or migration metric | +| Placement / `10-placement.md` | crate/layer choice and boundary rationale | +| Map / `20-map.md` | survey facts and candidate owners for a structural change | +| Design / `30-design.md` | approach, public surface, errors, tests, docs/ADR | +| Progress / `40-steps.md` | meaningful steps and deviations | +| Evidence / `50-gate.md` | command, source/configuration, scope, result/log, pending checks | +| Review / `60-review.md` | ranked findings and verdict | + +Use only sections relevant to the task. A stage is complete when its work and +needed evidence are available, not when a file list has been filled. Revise a +changed decision with a short reason; an append-only transcript is unnecessary. +Store plans/progress here rather than in current-system `docs/`. + +A handoff may use this optional compact block (a normal concise report is fine): + +```text +status: done | blocked | needs-decision +changed: +evidence: +remaining: ``` -`blocked` means a check or precondition failed and the step can be retried after -a fix; `escalate` means the decision belongs to the user (a layer conflict, a -new ADR, a rule that cannot be met). Never continue past `escalate`. +All procedure-skill output filenames and Result blocks are suggestions under +this contract; their content may live in the shared compact plan. A new ADR or +routine implementation detail does not itself require another user approval. +Never proceed with a genuinely required unresolved authorization; follow +AGENTS.md for material decisions and concrete blockers. diff --git a/.claude/skills/gate/SKILL.md b/.claude/skills/gate/SKILL.md index 4ae8ba77..d14cb08a 100644 --- a/.claude/skills/gate/SKILL.md +++ b/.claude/skills/gate/SKILL.md @@ -1,71 +1,139 @@ --- name: gate -description: Run the definition-of-done checks through the shared Cargo lock and record exact results, the debt baseline delta and early warnings in 50-gate.md. Quick mode after each step, full mode before review or commit. +description: Select local checks by changed inputs, reuse valid evidence, and finish the applicable Definition of done without per-edit or per-stage command bundles. user-invocable: false --- -# Gate +# Verification -Rules: AGENTS.md "Definition of done" and hard rules. Every Cargo command runs -through `.claude/hooks/cargo-serial.sh`, which sets `CARGO_BUILD_JOBS=2` and -holds one lock shared by every agent on the machine; the project hook rejects -compiling Cargo commands that bypass it. +AGENTS.md sets the workflow and requires this procedure's completion checks. +This file owns the detailed check matrix and commands; roles refer here rather +than copying them. Required CI is independent. Cargo resource settings follow +CONTRIBUTING.md; the coordination helper is optional. -## Quick mode (after each step) +## Select by the uncertainty -```sh -W=.claude/hooks/cargo-serial.sh -$W cargo fmt --all --check -$W cargo clippy -p --all-targets -- -D warnings -$W cargo test -p -$W cargo xtask architecture -``` +| Question/change | Useful scoped check | +| --- | --- | +| Reproduced regression | One `cargo test -p --lib ` or existing integration target/filter | +| Behavior spans an owner/package | Affected test targets and consumers | +| API/compilation uncertainty | `cargo check -p --lib` plus affected consumers, unless tests already compile them | +| Lint failure | Clippy for the affected package/targets/features | +| Rust formatting | Format changed sources; use `cargo fmt --all --check` for completion | +| Rendering, caching or other hot paths | Use `performance` for representative cost and correctness evidence; choose relevant GUI/behavior tests | +| Ownership/guarded structure | Inspect changed counts; survey when useful; run architecture for affected guardrails | +| Skills/agent instructions | Configuration validator, links and stale references | +| Hooks/scripts | Their behavior tests and language syntax/configuration checks | +| Prose only | Links/references; Cargo/generated checks only for affected generator inputs | -## Full mode (before review, commit or "done") +Finish a coherent edit before checking. Do not edit checked inputs while their +check runs. A test already compiles its target; do not routinely precede it with +both check and build. In parallel work, coordinate one executor and avoid +queuing the same pending check twice. -```sh -W=.claude/hooks/cargo-serial.sh -$W cargo fmt --all --check -$W cargo build --workspace --all-targets -$W cargo clippy --workspace --all-targets -- -D warnings -$W cargo test --workspace -$W cargo xtask architecture -$W cargo xtask docs --check +## Completion and failures + +At integrated completion, choose the applicable AGENTS.md scope after review +fixes. Broad shared changes, uncertain workspace impact or an explicit +full-verification request need the full workspace suite below. A small change +does not inherit that suite merely because a stage or worktree ended. Apply +required plugin-feature, +SSH/hub, guest, dependency/notice and vendor checks when their inputs change; +confirm fixtures actually ran, rather than counting skipped/zero tests. + +Rerun a failing target first. Preserve successful evidence when relevant +source/configuration is unchanged; identify which results a fix invalidates. +A new stage/report does not invalidate evidence. Reviewers inspect it and rerun +only missing, stale or specifically suspect checks. Keep stable targets and +investigate concrete stale artifacts before cleaning or touching all sources. +Do not change checks, architecture limits or baselines to obtain a pass. + +## Evidence + +One compact evidence section in the plan is enough: + +```text +Command | Source/configuration and targets/features | Result | Log if needed +Reused: +Pending/unavailable: ``` -Add when relevant: +Record the worktree/source snapshot, including relevant uncommitted/untracked +inputs. Separate queue wait and execution time when available. Report scoped +coverage as scoped; a completion pass does not assert a full-workspace pass. + +## Definition of done -| Changed | Also run | +Run the checks applicable to the integrated change, and report each actual +command and result plus omitted/unavailable checks and why: + +| Changed inputs / impact | Local completion checks | | --- | --- | -| SSH/SFTP code | `NOCTERM_REQUIRE_SSHD=1 $W cargo test -p nocterm-ssh --test sshd` | -| dependencies, vendored manifests, licenses, bundled assets | `$W cargo machete --skip-target-dir`; `python3 scripts/generate-license-notices.py --check-inputs` | -| public docs comments | `$W cargo doc --workspace --no-deps -j1` | -| `.claude/hooks/` | `python3 scripts/test-claude-hooks.py` | -| `.claude/skills/`, `.claude/agents/` | `python3 scripts/check-claude-config.py` | +| Documentation or agent instructions | Links and stale references; `python3 scripts/check-claude-config.py` for skills/agent configuration | +| Hooks or repository scripts only | Relevant script tests and syntax/configuration validators; no Cargo unless Rust or generator inputs are affected | +| Scoped Rust behavior | Formatting, lint and behavior tests for affected targets and consumers; architecture checks when guarded structure/ownership changes; generated references when their inputs change | +| Broad shared APIs, dependency/feature graph changes affecting many consumers, cross-cutting behavior, or uncertain workspace impact | Full workspace suite below, plus affected feature/platform fixtures | +| Explicit request for full verification | Full workspace suite and relevant additional checks | -## Interpreting +The full workspace suite is `cargo fmt --all --check`, +`cargo build --workspace --all-targets`, +`cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`, +`cargo xtask architecture` and `cargo xtask docs --check`. A scoped result must +not be presented as a full-workspace pass. Prose/tooling-only edits do not +require Cargo, generated-doc checks or product runtime tests when their inputs +are unchanged. -- A failure stops the gate; fix it, never weaken a check, never `--no-verify`. -- **Baseline**: `git diff xtask/baseline.toml` must show only decreases. If the - architecture check reports debt that shrank, lower the entry in the same - change. -- **Early warnings** from `cargo xtask architecture` are not failures. A - warning for a file or type this change grew means: record it in - `50-gate.md`, and if it is within one step of the limit, run - `responsibility-map` before adding more. +Additional checks follow actual changed inputs: -## Output: `50-gate.md` +- SSH/SFTP changes run the required SSH fixture; hub adapter changes run the + hub end-to-end test (see Commands). +- User-plugin code (`nocterm-plugin-*`, `plugins-ui`, + `src/shell_setup/user_plugin*`) also runs workspace clippy and tests with + `--features nocterm/user-plugins`; the default build compiles it out. + Plugin SDK/example changes also build/test the example and run the gated + WASM guest test. Preserve API/ABI compatibility checks for contract changes. +- Dependencies/manifests require applicable unused-dependency and notice checks; + vendored changes follow CONTRIBUTING.md's replay/history checks. +- Hook changes run `python3 scripts/test-claude-hooks.py`; configuration-validator + changes run `python3 scripts/test-claude-config.py` as well. -``` -# Gate () -| Command | Result | -| --- | --- | -| cargo fmt --all --check | pass | -… -Baseline delta: -Early warnings touched by this change: -Not run: +Commit, push and PR creation still require the user's request. + +## Commands + +```sh +# Choose resource settings locally; cargo-serial.sh is optional. +cargo run +cargo fmt --all --check +cargo build --workspace --all-targets +cargo clippy --workspace --all-targets -- -D warnings +cargo test --workspace +cargo clippy --workspace --all-targets --features nocterm/user-plugins -- -D warnings +cargo test --workspace --features nocterm/user-plugins +cargo test --locked --manifest-path examples/plugins/focus-timer/Cargo.toml +cargo build --locked --release --target wasm32-unknown-unknown --manifest-path examples/plugins/focus-timer/Cargo.toml +NOCTERM_GUEST_WASM=$PWD/examples/plugins/focus-timer/target/wasm32-unknown-unknown/release/focus_timer.wasm NOCTERM_REQUIRE_WASM_GUEST=1 cargo test -p nocterm --features nocterm/user-plugins user_plugin +NOCTERM_REQUIRE_SSHD=1 cargo test -p nocterm-ssh --test sshd +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm-hub-io --test hub +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm hub_live +cargo xtask docs +cargo xtask docs --check +cargo xtask architecture +cargo xtask survey [--json] [--type ] +.claude/hooks/cargo-serial.sh cargo +python3 scripts/test-claude-hooks.py +python3 scripts/check-claude-config.py +python3 scripts/test-claude-config.py +python3 scripts/check-plugin-api-compat.py +cargo machete --skip-target-dir +cargo doc --workspace --no-deps -j1 +python3 scripts/generate-license-notices.py --check-inputs ``` -Report exactly what ran and its result; mark anything skipped and why. -End with the Result block (`blocked` if any command failed). +Linux system dependencies and setup are in [README.md](../../../README.md). GUI checks +require an X11 or Wayland session and Vulkan driver. Regenerate references after +changing schemas, tokens, actions, keymap or internal Cargo dependencies. +Regenerate `THIRD_PARTY_NOTICES.txt` (`python3 scripts/generate-license-notices.py`, +needs `cargo-about` 0.9.2) after changing dependencies, vendored manifests or license +files, or bundled assets; CI and the pre-push hook reject stale notices +([docs/PACKAGING.md](../../../docs/PACKAGING.md)). diff --git a/.claude/skills/owner-extract/SKILL.md b/.claude/skills/owner-extract/SKILL.md index 7e18c164..90ff6c5d 100644 --- a/.claude/skills/owner-extract/SKILL.md +++ b/.claude/skills/owner-extract/SKILL.md @@ -1,20 +1,21 @@ --- name: owner-extract -description: Extract one responsibility of a type into an owner type with private state and transition methods, behind characterization tests. Use for a candidate from responsibility-map, when a type nears 15 fields, or when a file nears 800 lines — never cut a file or move methods of the same type instead. +description: Extract a confirmed state responsibility into a private owner with transition methods and behavior coverage. Use when ownership requires a split; approaching a size limit prompts inspection, not mechanical extraction. user-invocable: false --- # Owner extraction -Input: one candidate from `20-map.md`. Output: source changes and an updated -`40-steps.md` (`.claude/skills/feature/plan-bus.md`). +Input: a confirmed candidate in the compact plan. Record the change and evidence +there (`.claude/skills/feature/plan-bus.md`); separate stage files are optional. This is the transformation AGENTS.md design rules 1, 2 and 7 ask for. Moving methods of the parent into another file is not an extraction and fails review. ## Preconditions -- `20-map.md` names the owner, its fields, its methods and what it returns. +- The plan names the owner, state, invariant, transitions, lifetime and results. + Joint field access or a lower field count alone does not justify the owner. - Characterization tests exist for the behaviour that goes through those methods (`behavior-tests` in characterize mode). If they do not, write them first and see them pass on the unchanged code. @@ -35,9 +36,11 @@ methods of the parent into another file is not an extraction and fails review. short coordinator call that applies the returned effect. 4. **Delete the moved fields** from the parent. Do not leave mirrors or `pub(crate)` getters that expose the owner's internals. -5. **Test the owner directly** with unit tests for its invariants; keep the - characterization tests unchanged and green. -6. Run `gate` in quick mode. +5. **Test the owner directly** through its methods for invariants; preserve the + characterization scenarios and assertions. Mechanical imports/fixtures may move. +6. Use `gate` scoped mode for the owner/characterization behavior after the + coherent extraction when needed. Reuse unchanged results; no command bundle + after each moved method and no full gate for each extracted owner. ## Done when @@ -45,7 +48,8 @@ methods of the parent into another file is not an extraction and fails review. - The parent's impl-file count did not grow (`cargo xtask survey --type `). - The owner has no `pub`/`pub(crate)` fields and does not take the parent. - The owner's module name is a noun for what it owns. -- Characterization tests pass without edits; the owner has its own tests. +- Characterization behavior/expectations remain intact and pass; owner invariants + have coverage. Explain any fixture/API adaptation without weakening assertions. - `xtask/baseline.toml` entries went down or stayed equal; lower any that went down (the check fails until you do). @@ -59,4 +63,4 @@ methods of the parent into another file is not an extraction and fails review. | Fields moved but the parent keeps setters for each | the parent still owns the transitions | | Splitting a function into `step1`/`step2` helpers | complexity moved, not reduced | -End with the Result block and the before/after field counts. +Report behavior evidence and before/after field counts in the shared plan or handoff. diff --git a/.claude/skills/performance/SKILL.md b/.claude/skills/performance/SKILL.md new file mode 100644 index 00000000..87b3281f --- /dev/null +++ b/.claude/skills/performance/SKILL.md @@ -0,0 +1,77 @@ +--- +name: performance +description: Evaluate changed hot paths, rendering, repeated I/O or caches using representative costs and behavior. Use for optimization work or changes that affect input latency, redraws, large collections, background jobs or retained memory; not as a benchmark gate for every edit. +user-invocable: false +--- + +# Runtime cost and rendering + +AGENTS.md's Performance rule applies. Keep notes and results in the existing +compact plan; this procedure creates no extra approval or completion stage. + +## Find the cost + +1. Name the user action and the owner doing the work: typing, scrolling, + streaming output, opening a panel, refresh or background synchronization. +2. Inspect the whole affected path, including consumers. Estimate frequency × + work per event, collection sizes and retained memory. Look for repeated + scans/sorts/parsing, large clones, allocations, task spawning and fan-out. +3. For an optimization, capture a representative baseline using existing + profiling, tracing, counters or a focused benchmark. Choose the metric that + answers the issue: input/frame latency, calls/bytes, CPU or peak/retained memory. + Record workload, build mode and environment; compare the same conditions. + Use distributions or repeated samples for noisy timing, not one fast run. + +Compare plausible solutions when the tradeoff matters. First remove avoidable +work or improve the algorithm/data access; then consider batching, incremental +updates or caching. Account for invalidation, memory and maintenance cost. +Do not introduce a cache/framework just because it might help later. + +## Keep interaction responsive + +- Render and input callbacks consume prepared state; perform I/O and expensive + parsing/decoding in the appropriate adapter/service with bounded background + work. Moving work to a task does not make it free: bound concurrency/queues, + deduplicate equivalent requests and cancel obsolete work. +- Notify the affected owner only when its observable state changes. Coalesce + bursts without losing the final update or delaying required feedback. +- Reuse stable view identities and unaffected render regions. For large lists, + prefer existing virtualization and prepare only visible/needed data. Avoid + rebuilding the entire collection for one row or reparsing text every frame. +- Check theme/font/scale changes, resizing, scrolling, focus, selection and + content updates where affected. A reused frame must still respond correctly. +- Background watchers/pollers have a defined lifetime and cadence; share a + source when existing ownership permits it. Stop unused work on close/disable, + or explain why the underlying service must continue independently. + +## Cache contract + +Before adding or changing a cache, state: + +| Concern | Required answer | +| --- | --- | +| Value | Which repeated expensive work is avoided; why reuse is likely | +| Owner | Who owns entries and releases them; reuse an existing cache if it fits | +| Key | All semantic inputs, revisions and relevant account/session/source identity | +| Freshness | Events/revisions that invalidate entries; TTL only where staleness is acceptable | +| Bounds | Entry/byte budget, eviction and in-flight request limit appropriate to the data | +| Delivery | Old results cannot overwrite newer state or cross an account/session boundary | +| Failure | Miss/error/cancellation behavior; retries and negative caching are bounded | + +Keep authoritative state and access checks outside the cache. Release entries +when their scope ends; do not retain secrets or stale privileges for reuse. + +## Verify the affected behavior and cost + +Exercise cold/miss and warm/hit paths, changed inputs/invalidation, eviction and +late completion where applicable. Reuse public behavior tests and shared fakes. +Cost assertions may count externally requested work (for example network calls) +when reducing it is the requirement; avoid private-field and timing-flaky tests. + +For render changes, use relevant GUI/render tests and inspect the actual affected +interaction when a GUI session is available. Check visual correctness as well as +work avoided, including idle and repeated updates. A compilation pass or a static +screenshot alone does not establish responsive interaction or correct invalidation. +Report measured before/after results and tradeoffs; operation counts establish +less work, not a measured latency improvement. If profiling or GUI execution is +unavailable, state exactly what remains unverified. Completion checks use `gate`. diff --git a/.claude/skills/placement/SKILL.md b/.claude/skills/placement/SKILL.md index be815aeb..382c812b 100644 --- a/.claude/skills/placement/SKILL.md +++ b/.claude/skills/placement/SKILL.md @@ -7,7 +7,7 @@ user-invocable: false # Placement Input: one responsibility in a sentence ("parse container listings", "retain -chat drafts across restarts"). Output: `10-placement.md` in the plan directory +chat drafts across restarts"). Record the decision in the compact plan (contract: `.claude/skills/feature/plan-bus.md`). The rules are AGENTS.md "Where code goes" and docs/ARCHITECTURE.md layers; this @@ -15,10 +15,11 @@ is the procedure for applying them. ## 1. Classify the responsibility -Answer in order; the first "yes" decides the layer. +Classify each responsibility using the layer table; separate mixed responsibilities. | Question | Layer | | --- | --- | +| Is it a reusable low-level primitive with no product policy? | foundation, within its allowed dependencies | | Is it a rule, format or contract with no I/O and no GPUI? | domain | | Does it perform real filesystem, network, process, D-Bus or codec I/O behind a contract? | adapter | | Is it a GPUI entity with background work and no view? | service | @@ -30,39 +31,42 @@ file and validate it") into one piece per layer before going on. ## 2. Find the candidate crate -1. Read `docs/architecture/dependencies.md` and the layer note for the layer. +1. Find candidate owners in `docs/architecture/dependencies.md`; read their + relevant layer-note sections, not every subsystem in the layer. 2. For each candidate crate, read the `//!` card in its `lib.rs` (or `main.rs`). The responsibility must fit `Owns:` and must not appear in `Does not own:`. 3. Check the dependencies the code needs against the layer table in docs/ARCHITECTURE.md. A feature never depends on another feature: features meet through a narrow workspace port (`port-design` skill). -4. Search the ADR index (`docs/adr/README.md`) for the subject and read every - accepted record that names the candidate crate. +4. Search the ADR index (`docs/adr/README.md`) for decisions governing the + changed contracts. Follow supersession links; read older context when needed + to resolve a constraint. An incidental crate mention is not a reading requirement. ## 3. Module or new crate -Propose a new crate when **two or more** hold: +First exclude candidates whose layer/dependencies or ownership card forbid the +responsibility. One such conflict is enough; never put it there as a module. +Reuse a fitting existing crate. Propose a new crate when no existing owner fits; +justify its cohesive responsibility, dependency boundary and consumers. -- it needs a dependency set the candidate crate must not or does not have; -- it belongs to a different layer than the candidate crate; -- it changes for different reasons and has independent tests; -- adding it would put it in the candidate's `Does not own:` or grow the card's - `Owns:` with an unrelated subject. - -Otherwise it is a module of the existing crate, named after the responsibility -its type owns (never `utils`, `helpers`, `common`, `misc`). +Within a fitting crate, use a responsibility-named module (never `utils`, +`helpers`, `common`, `misc`). Independent invariants, lifetimes and reasons to +change guide separation; line counts or independent tests alone do not decide it. +Compare meaningful alternatives by correctness, reuse, coupling and runtime cost. Existing mixed responsibilities are migration debt, not precedent: "agent already does X" is not a reason to put more X there. -## 4. Stop conditions +## 4. Boundary decisions -- The code needs a dependency its layer forbids → `escalate` with the options - (move a narrow contract down a layer, split the responsibility, new crate). +- If the code needs a dependency its layer forbids, resolve the boundary within + the authorized design (move a narrow contract down, split the responsibility, + or choose a fitting crate). Ask only when a material choice or scope change + remains; never introduce a forbidden dependency. - The decision changes a crate boundary or a port between layers → mark "ADR needed" for the `docs-adr` skill. -## Output: `10-placement.md` +## Decision to record ``` # Placement: @@ -74,4 +78,4 @@ already does X" is not a reason to put more X there. - Module: — owns ``` -End with the Result block from the plan contract. +Report placement and any material unresolved decision in the shared plan. diff --git a/.claude/skills/plugin-api/SKILL.md b/.claude/skills/plugin-api/SKILL.md new file mode 100644 index 00000000..590ff7eb --- /dev/null +++ b/.claude/skills/plugin-api/SKILL.md @@ -0,0 +1,142 @@ +--- +name: plugin-api +description: Work on built-in plugins, shell panels/widgets or feature-gated user plugins. Route built-in contributions through the catalog and workspace registration; route guest capabilities through the existing plugin API/ports/host/runtime with grants, compatibility and safe-mode boundaries. Not for ordinary ports between built-in crates or AI MCP tools. +user-invocable: false +--- + +# Plugin APIs and contributions + +Rules: AGENTS.md "Where code goes" and design rules 2 and 3; crate card +`crates/plugin-model/src/lib.rs`; ADR `docs/adr/0082-system-plugins-and-core-contributions.md` +and `docs/adr/0083-zoned-status-bar.md` (placement, zones, pinned and attention rules) and +`docs/adr/0085-one-plugin-model-with-separate-trust-tiers.md` (settings schema, trust tiers) and +`docs/adr/0086-plugin-enablement-and-the-plugins-page.md` (enablement, the Plugins page, nested pages); +registration types in `crates/workspace/src/workspace/registration.rs`; catalog in +`src/shell_setup.rs`. + +## 0. Route +- Consumer is the shell, the layout editor or the Plugins page → this skill. +- Consumer is another built-in feature → `port-design`. +- Consumer is a user plugin → follow the user-plugin route below. It already + exists behind `user-plugins`; a native getter or `pub` field is not a guest API. +- Consumer is an arbitrary user script → identify the supported entrypoint and + trust boundary. Do not invent native script execution or access to app internals. + Resolve a genuinely new authority decision under AGENTS.md's workflow policy. + +## User-plugin route + +Read `docs/plugins/README.md`, the relevant crate cards and ADRs +`docs/adr/0089-user-plugin-contract.md`, +`docs/adr/0090-user-plugin-host-and-passive-providers.md`, +`docs/adr/0091-user-plugin-code-runs-in-process-on-wasmi.md`, +`docs/adr/0092-plugin-sdk-and-developer-sessions.md` and +`docs/adr/0093-user-plugins-start-off-and-the-api-stays-compatible.md`. + +- Contract/capabilities belong to `nocterm-plugin-api`; native providers use + `nocterm-plugin-ports`. The host owns grants/effects, package and WASM adapters + own loading/execution, the view crate draws admitted views, and SDK/ABI define + the guest interface. Inspect existing types before proposing a new surface. +- Compose through `src/shell_setup/user_plugins.rs`. Preserve capability grants, + consent, gesture/rate/resource bounds, safe mode and the default-off release + policy. Do not expose GPUI entities or arbitrary native access to guests. +- Contract changes preserve API/ABI compatibility and generated references. + Run changed-input checks from AGENTS.md, including the user-plugin feature + matrix and SDK/example/gated guest tests when applicable. + +The following sections describe built-in contributions, not guest registration. + +## 1. Declare +A core function (connections, files, the workspace shell) declares +`pub const CORE: &[SystemContribution]` instead; it is never a plugin and never disabled. Do not +turn a core function into a plugin, or a plugin into core, without the owner's decision. +The owner crate's `plugin` module holds `pub static PLUGIN: SystemPlugin` with its id, name, +description, `required` and `contributions`. It is a `static` because an id holds a `Cow`, and a +reference to a `const` of such a value is not promoted to `'static`. The plugin id equals the +contribution namespace. Each contribution is a `SystemContribution` struct literal with id +`.`. Panels set `panel: Some(PanelTraits { … })`; widgets set `panel: None`. A new +entry uses `auto: AutoPlace::Tray` unless the task changes Default on purpose. Set +`required: true` only for a plugin that carries attention, permission or notification UI, and +say why. + +## 2. Compose +Add a new plugin's `PLUGIN` once to the list in `src/shell_setup.rs`; a core owner's `CORE` goes +to the `core` argument of `plugin::catalog` there. + +## 3. Register +In the owner's `register()`, which returns `Result<(), RegistrationError>`: +- panels: `workspace.register_panel(PanelRegistration::new(&DESCRIPTOR, factory))`, adding + `.expandable()` only for an `ExpandablePanel`. The factory is + `Fn(&Workspace, &mut Window, &mut Context) -> Entity`: read what the panel needs + from the `&Workspace` argument (sources, active session) instead of capturing it at + registration. Use an existing workspace source; a missing one is a narrow port designed with + `port-design`, never a new `Workspace` getter. It may run again after the plugin is + re-enabled, so keep nothing that must outlive the panel inside it, and never hold the panel entity elsewhere; use the id queries + (`open_panel`, `toggle_panel`, `panel_is_open`, `panel_is_available`). + `PanelRegistration::with_entity` is for core contributions and required plugins only; the + workspace refuses it for a plugin that can be disabled (`RegistrationError::EagerNotRequired`); +- widgets: `workspace.register_widget(WidgetRegistration::new(&DESCRIPTOR, factory))`, where + `factory` has the same signature and returns `Entity`. The status bar decides when to call + it; never create the view, or a sampler it needs, eagerly elsewhere. A widget whose view opens a + popover implements `ZoneAware` and registers with `WidgetRegistration::zone_aware`, so the popover opens + towards the inside of the window from the zone that places it (`nocterm_ui::zone_anchor`). +Actions, services and observers stay in `register()`; they keep running while the plugin is +disabled. Never add `impl Workspace` in a new file. + +## 4. Test +Build workspace dependencies with `test_support::deps_for(&[&PLUGIN], cx)`, or `deps_for_core` +when the owner declares `CORE`. Keep the owner's descriptor/panel parity test, the "every catalog +entry is registered" bootstrap test, and the default-geometry oracle unchanged unless the change +intends otherwise. + +A widget or panel that shows something native (the local time, a host reading) takes it as a +narrow port through its owner's constructor or `register`, supplied in `src/bootstrap.rs`; do +not add a field to the workspace's `Deps` for a plugin (the workspace's own Clock uses +`Deps::with_clock` because the workspace owns that widget, ADR 0088). A window composed without +the port simply omits the widget. Never reach a native source from a view. + +## Settings +1. **Schema.** Add a `FieldGroup` to the owner's `PLUGIN.settings`, with + `contribution: Some()` when the field belongs to one widget or panel. Kinds are toggle, + choice, multi-choice, bounded number and single-line text (`max_len` at most 256, + sanitized, never masked). There is no colour, secret, password or multi-line field; stop and + `escalate` (ADR 0085 amendment) instead of adding one or a custom view. Never store a + credential in a text field. +2. **Storage.** The field key equals the serde key in the owner's own `SettingsSection`, named + by `settings_section`. Never add a generic table or a second section per plugin. +3. **Binding.** One `SectionBinding::` per plugin, with `read` and `write` functions; writes + run inside `update_checked`. The plugin's own code keeps its typed `setting::` read. The + binding sanitizes and bounds text on every read; a text field's owner `sanitize` should call + `sanitize_text` too, so the owner's own reads are clean. +4. **Tests.** Parity of options and defaults with the owner type; a write through the binding + changes the expected key; `PLUGIN.settings_section == Some(S::KEY)`. + +## Enablement and the plugin's own page (ADR 0086) +1. **Switch.** Never read `[plugins]` or test a plugin's state yourself. The shell creates the + panels and widgets of a disabled plugin nowhere; ask `Workspace::panel_is_enabled` or the + resolved layout. A required plugin (`required: true`) cannot be disabled; make a plugin + required only on the owner's decision. +2. **Own page.** If the plugin keeps a full settings page, set `settings_page: Some()` + in `PLUGIN` and have the application register that page, nested with + `SettingsPageRegistration::lazy(..).under("plugins", )` or, for a page that is a + destination of its own (AI), at the top level; a test must keep the two consistent. A plugin without a page sets `None`. The page stays reachable when the plugin + is disabled. +3. **Anything that runs on its own** (a sampler, a watcher) must stop with the widget or panel + that drives it; test it with a fake executor while the plugin is disabled. +4. **Plugins page code** (`nocterm-plugins-ui`) names no plugin and takes no settings handle; + it receives `PluginToggles` and a `PluginSettingsDirectory`. + +## 5. Regenerate +`docs/reference/plugins.md` is generated from the catalog. After a plugin, descriptor or +settings change run `cargo test -p nocterm plugins_reference` +with `NOCTERM_BLESS=1` set, and include the generated result in the change. +Commit only when the user requests it. + +## Suggested design notes (compact plan or `30-design.md`) +### Plugin +- Owner crate, name, required (and why), contributions +### Contribution +- Kind, zone/order/group/auto, pinned, panel traits, preview +- Default changes: none | +- Tests: + +Report the relevant design, tests and remaining decisions concisely. diff --git a/.claude/skills/port-design/SKILL.md b/.claude/skills/port-design/SKILL.md index 88d614d3..7c9edf34 100644 --- a/.claude/skills/port-design/SKILL.md +++ b/.claude/skills/port-design/SKILL.md @@ -1,14 +1,14 @@ --- name: port-design -description: Design a narrow port (trait or handle) between a consumer and a capability — named for the consumer's need, with a typed error, a shared fake and injection in bootstrap. Use when code needs I/O, another feature, or a service it must not depend on directly. +description: Design a narrow port (trait or handle) between a consumer and a capability — named for the consumer's need, with a typed error, a shared fake and injection in bootstrap. Use when code needs I/O, another feature, or a service it must not depend on directly. Not for contributions shown by the window shell, plugin settings or capabilities exposed to user plugins (plugin-api). user-invocable: false --- # Port design Input: a consumer and what it needs ("the snippets panel needs to paste into the -user's terminal"). Output: a "Ports" section in `30-design.md` -(`.claude/skills/feature/plan-bus.md`), then code. +user's terminal"). Record the contract in the compact plan +(`.claude/skills/feature/plan-bus.md`) before implementing it. Rules: AGENTS.md "Where code goes" item 5, design rules 3 and 4; ADR 0006 (workspace ports), 0017 (local filesystem capabilities), 0032 (narrow terminal @@ -20,20 +20,22 @@ Search for an existing contract before writing one: - `nocterm-session`: `Transport`, `RemoteFs`, `LocalFs`/`LocalMutations`, `CredentialStore`, `HostExec`, `ProgramOpener`. -- `nocterm-workspace`: the narrow terminal and connection seams - (`LocalTerminal`, `UserTerminalPaste`, `TerminalCapabilities`, - `ConnectionDirectory`, …). +- `nocterm-workspace-ports`: terminal contracts (`UserTerminalPaste`, + `TerminalCapabilities`, …), available below the shell (ADR 0062). +- `nocterm-workspace`: shell seams (`LocalTerminal`, `ConnectionDirectory`, …). - `nocterm-store`: `Persisted`, `SnapshotWriter`, `AppendPersisted`, `ShutdownCoordinator`. -Reuse a contract only if the consumer needs **all** of it. Never add a method -to an existing broad trait for a new feature pair: write a separate port. +Reuse a cohesive contract when its dependencies, lifetime and authority fit. +Calling only some methods is not by itself a reason to wrap or duplicate it. +Use a narrower port when extra capabilities grant unwanted authority or couple +independent responsibilities. Never grow a broad trait for a new feature pair. ## 2. Shape - **Name for the consumer's need**: `UserTerminalPaste`, not `TerminalApi`; `ConnectionDirectory`, not `ConnectionsService`. -- **Minimal surface**: the methods this consumer calls today. More than about - six methods, or methods only another consumer uses, means two ports. +- **Minimal surface**: expose the cohesive capability needed today. Split by + independent responsibility or authority, not an arbitrary method count. - **Owner of the contract**: the lowest layer both sides may depend on — a domain crate (pure contract, e.g. session), `workspace` for feature-to-feature seams, a service crate for GPUI handles. Use `placement` if unsure. @@ -57,7 +59,7 @@ to an existing broad trait for a new feature pair: write a separate port. - The real implementation lives in the adapter (I/O) or the providing feature. - Composition injects it in `src/bootstrap.rs` (`wiring`). -## Output (in `30-design.md`) +## Contract to record ``` ### Port @@ -70,4 +72,4 @@ to an existing broad trait for a new feature pair: write a separate port. - Rejected alternative: because … ``` -End with the Result block. +Report the relevant changes, evidence and remaining work concisely. diff --git a/.claude/skills/refactor/SKILL.md b/.claude/skills/refactor/SKILL.md index f25e9974..f7e1a321 100644 --- a/.claude/skills/refactor/SKILL.md +++ b/.claude/skills/refactor/SKILL.md @@ -1,53 +1,28 @@ --- name: refactor -description: Workflow for a behaviour-preserving restructuring inside a crate — map responsibilities from survey facts, pin behaviour with characterization tests, extract owners one at a time, lower the debt baseline. Use when a file or type hits an early warning or limit, or the user asks to split, clean up or restructure code within a crate. +description: Restructure within a crate without changing behavior, using responsibility facts and characterization tests; use for a needed extraction or requested cleanup. argument-hint: " [package] [target: master|dev]" --- -# Workflow: refactor within a crate +# Refactor within a crate Subject: $ARGUMENTS -Behaviour does not change. Pass the plan directory, target branch and -verification scope to every subagent (`.claude/skills/feature/plan-bus.md`). - -## 0. Task - -`00-task.md` with a measurable metric taken from the survey, for example -"`` fields 14 → ≤ 9; files ≥ 640 lines in `/`: 2 → 0; -`impl_spread` entry for `` 6 → ≤ 4". Branch `refactor/`. - -## 1. Map (architect) - -1. `responsibility-map` → `20-map.md`. Its matrix pattern decides the route: - - hidden owners → step 2 per candidate; - - fields are already owners, coordination is the problem → design named - effects or per-workflow coordinators (ADR 0044) in `30-design.md`; - - a module that would rather be a crate → switch to `crate-split`. -2. `30-design.md`: candidates in order (smallest safe extraction first), each - with its expected counts; characterization tests to write; ADR decision via - `docs-adr` (none if crate boundaries hold). - -Show the map and design to the user and wait for approval. - -## 2. Extract (implementer), per candidate - -1. `behavior-tests` characterize — pass on unchanged code; names recorded. -2. `owner-extract` — one owner per step. -3. `gate` quick; tick the step in `40-steps.md` with before/after counts. - -Never mix behaviour changes into these steps. If a step needs one, stop and -report. - -## 3. Verify (tester) - -`gate` full. Rerun the survey and compare with the metric in `00-task.md`; -characterization tests unchanged and green. - -## 4. Review (reviewer) — `design-review`; two-round limit. - -## 5. Wrap up - -Report the metric before/after, gate results, baseline entries lowered and the -verdict. One `refactor(): …` commit per extracted owner is preferred. -Commit only if the user asked. +Follow AGENTS.md's Workflow and Definition of done, using a compact plan or +[structured handoff](../feature/plan-bus.md) as useful. Record branch/base, +behavior to preserve and a relevant structural metric. An early warning alone +does not require this workflow. + +1. Use `responsibility-map` to identify real owners. If fields already represent + owners, consider coordination by use case or named effects (ADR 0044). + Ownership moving to another crate uses `crate-split` instead. +2. Design extractions in a safe order, with expected counts, characterization + coverage and a `docs-adr` decision. Apply AGENTS.md's authorization policy; + routine details within an authorized design do not require another approval. +3. Characterize behavior on unchanged code, then `owner-extract` one responsibility + at a time. Preserve behavior; report any behavior change needed outside scope. +4. Use `gate` for affected behavior after coherent edits, reusing valid evidence. + Lower/remove debt inventory entries when the measured debt shrinks. +5. Review with `design-review`, fix findings and finish the applicable input-based + completion checks. Report before/after metric, behavior evidence and limitations. + Commit/push/PR only when requested. diff --git a/.claude/skills/responsibility-map/SKILL.md b/.claude/skills/responsibility-map/SKILL.md index f5e0a850..66d24c27 100644 --- a/.claude/skills/responsibility-map/SKILL.md +++ b/.claude/skills/responsibility-map/SKILL.md @@ -1,30 +1,29 @@ --- name: responsibility-map -description: Read-only analysis of a type, module or crate that finds its responsibilities and candidate state owners from `cargo xtask survey` facts. Run before any split, owner extraction or crate split, and when a file or type reaches an early-warning threshold. +description: Read-only analysis of a type, module or crate that finds its responsibilities and candidate state owners from `cargo xtask survey` facts. Use for a split or unclear ownership; early warnings invite inspection rather than mandatory extraction. user-invocable: false --- # Responsibility map -Input: a type (`AgentThread`), a module (`agent/thread`) or a crate. Output: -`20-map.md` in the plan directory (`.claude/skills/feature/plan-bus.md`). This -step writes no source code. +Input: a type (`AgentThread`), a module (`agent/thread`) or a crate. Record facts +and the proposed owners in the compact plan (`.claude/skills/feature/plan-bus.md`). +This step writes no source code. The goal is to find where responsibilities separate **by state**, because an owner is a set of fields with the transitions that keep them consistent -(AGENTS.md design rules 1, 2 and 7). Line counts only say that a split is due; -they never say where. +(AGENTS.md design rules 1, 2 and 7). Line counts signal remaining room; they do not decide whether or where to split. ## 1. Collect facts -Run through the shared lock (AGENTS.md hard rules): +Use a survey when it helps resolve ownership (Cargo resources follow CONTRIBUTING.md): ```sh -.claude/hooks/cargo-serial.sh cargo xtask survey --json > .claude/plans//survey.json -.claude/hooks/cargo-serial.sh cargo xtask survey --type +cargo xtask survey --json > .claude/plans//survey.json +cargo xtask survey --type ``` -Record in `20-map.md`: the warnings, the largest files, the type's fields, +Record relevant facts in the plan: the warnings, the largest files, the type's fields, methods and impl files, and the module reference counts. The survey is syntactic: it keys types by name and does not expand macros, so confirm any surprising fact in the source. @@ -37,11 +36,15 @@ coordinators use are listed separately: assign them by hand. | Pattern | Meaning | Next step | | --- | --- | --- | -| A cluster of 2+ fields with its own methods | a hidden owner | `owner-extract` | -| Every field is its own cluster and most methods are cross-cluster or coordinators | the fields are already owners; the type is a coordinator with too large a surface | split coordination by use case: named effects returned by owners (ADR 0044) or a separate coordinator per workflow | +| A cluster of 2+ fields with its own methods | possible owner | confirm one invariant and lifetime before `owner-extract` | +| Every field is its own cluster and most methods are cross-cluster or coordinators | possible coordination among existing owners | inspect actual types and transitions; split by use case only if responsibilities differ (ADR 0044) | | Many methods with no direct field access | delegating wrappers or free functions | move each next to the owner it calls, or into a pure function of the domain | | A field no method touches | state used only through destructuring or by another type | confirm; it may belong to that other type | -| Impl blocks of one type in 4+ files | file slicing already happened | treat each file's methods as a candidate owner, check its fields | +| Impl blocks of one type in 4+ files | possible file slicing | inspect responsibilities; do not treat each existing file as a future owner | + +Clusters reflect syntactic co-access, not semantic ownership. Confirm which +fields must change together, who is authoritative, and when work ends. A cohesive +type may need no split; pure logic may need functions rather than a state owner. For a module or crate, use the `modules` section: a top-level module whose references go mostly to one workspace crate, and which has few references to @@ -57,6 +60,7 @@ For every candidate owner write: - a draft card: `Owns:`, `Does not own:`, `Invariants:`; - its fields, its transition methods, and what it returns to the parent (a value, a typed error, or a named effect for the coordinator to apply); +- creation/teardown, cancellation and the destination of late asynchronous results; - expected field counts after extraction for both the parent and the owner. Reject a candidate that would need the parent passed back into it (`&mut Parent`, @@ -64,10 +68,11 @@ the parent's `Context`): it is not a separate responsibility yet. ## 4. Card drift -Compare what the code does with the crate card. List every responsibility that -is under `Does not own:` or missing from `Owns:`; these go to `docs-adr`. +Compare code with the crate card. A conflicting responsibility needs a placement +fix; do not legitimize it by expanding the card. Use `docs-adr` for actual boundary +decisions and stale descriptions after ownership is resolved. -## Output: `20-map.md` +## Decision to record ``` # Responsibility map: @@ -82,5 +87,5 @@ is under `Does not own:` or missing from `Owns:`; these go to `docs-adr`. ## Recommended order (smallest safe extraction first) ``` -End with the Result block; `next` is usually `behavior-tests` (characterize) -and then `owner-extract`. +Report the map and next useful step; `behavior-tests` and `owner-extract` +apply when an extraction is needed. diff --git a/.claude/skills/typed-errors/SKILL.md b/.claude/skills/typed-errors/SKILL.md index d7421005..616a2cea 100644 --- a/.claude/skills/typed-errors/SKILL.md +++ b/.claude/skills/typed-errors/SKILL.md @@ -45,4 +45,4 @@ count may only shrink. - [ ] causes retained with `#[source]`/`#[from]`/`SharedError` - [ ] variants carry data, not formatted text -End with the Result block. +Report the relevant changes, evidence and remaining work concisely. diff --git a/.claude/skills/wiring/SKILL.md b/.claude/skills/wiring/SKILL.md index f50b8423..8296d9d7 100644 --- a/.claude/skills/wiring/SKILL.md +++ b/.claude/skills/wiring/SKILL.md @@ -33,11 +33,10 @@ installations), 0001/0056/0057 (settings sections and pages). ## 2. Order in `bootstrap` -Insert the new step after everything it depends on and before everything that -uses it. The existing order is: settings → UI and shutdown coordinator → themes -→ layout → terminal dependencies → connections → terminal and vault → snippets -→ agent → files → keymap → monitor → settings pages → `Booted`. GUI tests use -the same `bootstrap` with fakes in `Services`; add the fake there too. +Read the current `bootstrap` in `src/bootstrap.rs`. Insert a step after its +dependencies and before its consumers; do not maintain a second ordered list +here. GUI tests use the same bootstrap with fakes in `Services`; update the +affected supplied capability there too. ## 3. Persistence: pick, do not write @@ -68,6 +67,6 @@ Writing `std::fs::write` in a feature or service is a review blocker. - [ ] the installation is retained, not recreated per window - [ ] persistence uses a row of the table above and is registered for shutdown - [ ] bootstrap order respects dependencies; GUI test services updated -- [ ] references regenerated (`cargo xtask docs`) +- [ ] references regenerated when generator inputs changed (`gate`) -End with the Result block. +Report the relevant changes, evidence and remaining work concisely. diff --git a/.dockerignore b/.dockerignore new file mode 100644 index 00000000..e4e9345a --- /dev/null +++ b/.dockerignore @@ -0,0 +1,8 @@ +# Keeps the build context of deploy/runner/Dockerfile small. Nothing else in +# the repository builds with docker. +.git +.claude +.playwright-mcp +**/target +node_modules +/target*/ diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 8d1b2015..5e1cc416 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,65 +28,73 @@ permissions: contents: read pull-requests: read -jobs: - vendor-patches: - name: Vendored source replay - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v5 - with: - ref: ${{ inputs.ref || github.ref }} - - uses: actions/setup-python@v5 - with: - python-version: "3.12" - - name: Test archive authentication and replay failures - run: python3 scripts/test-vendor-patches.py - - name: Replay published archives and patches - run: python3 scripts/check-vendor-patches.py +env: + CARGO_TERM_COLOR: always + # No debugger symbols in CI: links are faster and the caches several times smaller. + # A failing test still names itself and its assertion. + CARGO_PROFILE_DEV_DEBUG: "0" + CARGO_PROFILE_TEST_DEBUG: "0" - agent-guardrails: - name: Agent hooks and skills +# How the jobs share work: `lint` checks (clippy, rustdoc) and `test` builds, so neither +# repeats the other's compilation; `user-plugins` tests only the packages the feature +# changes. Caches are saved by pushes to `dev` and `master`, also when a step fails, and +# pull requests restore them. +jobs: + policy: + name: Repository policy runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 15 steps: - uses: actions/checkout@v5 with: ref: ${{ inputs.ref || github.ref }} + # Plugin API compatibility compares with the latest release tag. + fetch-depth: 0 - uses: actions/setup-python@v5 with: python-version: "3.12" - - name: Check Claude Code hooks - run: python3 -B scripts/test-claude-hooks.py - - name: Check Claude Code configuration - run: | - python3 -B scripts/test-claude-config.py - python3 -B scripts/check-claude-config.py - - release-pipeline: - name: Release pipeline regressions - runs-on: ubuntu-latest - timeout-minutes: 15 - steps: - - uses: actions/checkout@v5 + - uses: actions/setup-node@v4 with: - ref: ${{ inputs.ref || github.ref }} + node-version: "22" - uses: dtolnay/rust-toolchain@stable with: toolchain: "1.98.0" - - uses: actions/setup-node@v4 + - uses: taiki-e/install-action@v2 with: - node-version: "22" - - name: Install isolated updater test dependencies - run: npm install --prefix "$RUNNER_TEMP/release-pipeline-tests" --ignore-scripts --no-audit --no-fund release-please@17.3.0 yaml@2.9.1 - - name: Check release updates and validation wiring + tool: cargo-machete@0.9.2,cargo-deny + # Every step runs even when an earlier one fails, so one run reports every problem. + - name: Agent hooks and configuration + run: | + python3 -B scripts/test-claude-hooks.py + python3 -B scripts/test-claude-config.py + python3 -B scripts/check-claude-config.py + - name: Plugin API compatibility + if: ${{ !cancelled() }} + run: | + python3 -B scripts/test-plugin-api-compat.py + python3 -B scripts/check-plugin-api-compat.py + - name: Release pipeline + if: ${{ !cancelled() }} env: NODE_PATH: ${{ runner.temp }}/release-pipeline-tests/node_modules - run: node scripts/test-release-pipeline.cjs - - name: Check release notice preparation - run: python3 scripts/test-prepare-release-pr.py - - name: Check architectural history policy fixtures - run: python3 scripts/test-architecture-history.py + run: | + npm install --prefix "$RUNNER_TEMP/release-pipeline-tests" --ignore-scripts --no-audit --no-fund release-please@17.3.0 yaml@2.9.1 + node scripts/test-release-pipeline.cjs + python3 scripts/test-prepare-release-pr.py + python3 scripts/test-architecture-history.py + # Packaging rejects stale notices only after merge; catch the drift while the change + # is open. Release pull requests get their notices regenerated by release.yml, which + # then validates the prepared commit through this workflow. + - name: Third-party notice inputs + if: ${{ !cancelled() && !startsWith(github.head_ref, 'release-please--') }} + run: python3 scripts/generate-license-notices.py --check-inputs + # Manifest-only. --with-metadata is not used: it rewrites Cargo.lock. + - name: Unused dependencies + if: ${{ !cancelled() }} + run: cargo machete --skip-target-dir + - name: Dependency advisories + if: ${{ !cancelled() }} + run: cargo deny check advisories conventions: name: Conventional Commits @@ -122,6 +130,7 @@ jobs: scripts/lint-commit-msg.sh "$RUNNER_TEMP/title" - name: Lint commits + if: ${{ !cancelled() }} env: BASE: ${{ steps.pr.outputs.base || github.event.pull_request.base.sha }} HEAD: ${{ inputs.ref || github.event.pull_request.head.sha }} @@ -134,6 +143,7 @@ jobs: exit $status - name: Check architectural baseline and vendor commit separation + if: ${{ !cancelled() }} env: BASE: ${{ steps.pr.outputs.base || github.event.pull_request.base.sha }} HEAD: ${{ inputs.ref || github.event.pull_request.head.sha }} @@ -161,43 +171,47 @@ jobs: git cat-file -e "$BASE^{commit}" || git fetch --no-tags origin "$BASE" python3 scripts/check-architecture-history.py --base "$BASE" --head "$HEAD" - # Packaging rejects stale notices only after merge; catch the drift here, - # while the change that caused it is still open. - notices: - name: Third-party notices - # Release pull requests get their notices regenerated by release.yml, - # which then validates the prepared commit through this workflow. - if: ${{ !startsWith(github.head_ref, 'release-please--') }} + lint: + name: Lint and docs runs-on: ubuntu-latest - timeout-minutes: 10 + timeout-minutes: 45 + env: + RUSTDOCFLAGS: "-D warnings" steps: - uses: actions/checkout@v5 with: ref: ${{ inputs.ref || github.ref }} + - name: Install native dependencies + run: | + sudo apt-get update + sudo apt-get install -y build-essential pkg-config clang cmake libssl-dev \ + libfontconfig1-dev libfreetype6-dev libxkbcommon-dev libxkbcommon-x11-dev \ + libwayland-dev libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev \ + libvulkan-dev - uses: dtolnay/rust-toolchain@stable with: toolchain: "1.98.0" - - name: Check notice inputs - run: python3 scripts/generate-license-notices.py --check-inputs - - # Manifest-only and fast. --with-metadata is not used: it rewrites Cargo.lock. - unused-dependencies: - name: Unused dependencies - runs-on: ubuntu-latest - timeout-minutes: 10 - steps: - - uses: actions/checkout@v5 - with: - ref: ${{ inputs.ref || github.ref }} - - uses: dtolnay/rust-toolchain@stable + components: rustfmt, clippy + - uses: Swatinem/rust-cache@v2 with: - toolchain: "1.98.0" - - name: Install cargo-machete - run: cargo install --locked --version 0.9.2 cargo-machete - - run: cargo machete --skip-target-dir + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} + - run: cargo fmt --all --check + - name: Clippy + if: ${{ !cancelled() }} + run: cargo clippy --locked --workspace --all-targets -- -D warnings + # The user-plugin track is compiled out of default builds until it ships + # (docs/architecture/guardrails.md); every commit must also build with it. + # Only the crates the feature changes are checked again. + - name: Clippy with user plugins + if: ${{ !cancelled() }} + run: cargo clippy --locked --workspace --all-targets --features nocterm/user-plugins -- -D warnings + - name: Rustdoc + if: ${{ !cancelled() }} + run: cargo doc --locked --workspace --no-deps - rust: - name: Build, lint, tests and documentation + test: + name: Build and test runs-on: ubuntu-latest timeout-minutes: 60 env: @@ -218,12 +232,23 @@ jobs: - uses: dtolnay/rust-toolchain@stable with: toolchain: "1.98.0" - components: rustfmt, clippy - uses: Swatinem/rust-cache@v2 - - run: cargo fmt --all --check - - run: cargo build --locked --workspace --all-targets - - run: cargo clippy --locked --workspace --all-targets -- -D warnings + with: + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} + # One compilation for everything below: the tests reuse it, and the xtask binary is + # run directly, because `cargo xtask` resolves features for xtask alone and rebuilds. + - name: Build + id: build + run: cargo build --locked --workspace --all-targets + - name: Generated documents are current + if: ${{ !cancelled() && steps.build.outcome == 'success' }} + run: target/debug/xtask docs --check + - name: Architecture rules + if: ${{ !cancelled() && steps.build.outcome == 'success' }} + run: target/debug/xtask architecture - name: Run workspace tests + if: ${{ !cancelled() && steps.build.outcome == 'success' }} run: | set -o pipefail cargo test --locked --workspace 2>&1 | tee "$RUNNER_TEMP/nocterm-workspace-tests.log" @@ -241,12 +266,96 @@ jobs: escaped = chunk.replace("%", "%25").replace("\r", "%0D").replace("\n", "%0A") print("::error title=Workspace test failure::" + escaped) PYTHON - - run: cargo test --manifest-path vendor/alacritty_terminal/Cargo.toml --lib + + # The workspace tests above already run every plugin crate. The feature changes only the + # packages that declare it, so only they are tested again with it, against a real guest. + user-plugins: + name: User plugins + runs-on: ubuntu-latest + timeout-minutes: 60 + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ inputs.ref || github.ref }} + - name: Install native dependencies + run: | + sudo apt-get update + sudo apt-get install -y build-essential pkg-config clang cmake libssl-dev \ + libfontconfig1-dev libfreetype6-dev libxkbcommon-dev libxkbcommon-x11-dev \ + libwayland-dev libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev \ + libvulkan-dev mesa-vulkan-drivers dbus + - uses: dtolnay/rust-toolchain@stable + with: + toolchain: "1.98.0" + components: clippy + targets: wasm32-unknown-unknown + - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} + workspaces: | + . -> target + examples/plugins/focus-timer -> target + # The example code plugin is a workspace of its own. The tests below load the module + # it builds, so a Rust guest runs through the real check and runtime. + - name: Build and test the example code plugin + run: | + cargo clippy --locked --all-targets --manifest-path examples/plugins/focus-timer/Cargo.toml -- -D warnings + cargo test --locked --manifest-path examples/plugins/focus-timer/Cargo.toml + cargo build --locked --release --target wasm32-unknown-unknown \ + --manifest-path examples/plugins/focus-timer/Cargo.toml + - name: Test the packages the feature changes + env: + NOCTERM_GUEST_WASM: ${{ github.workspace }}/examples/plugins/focus-timer/target/wasm32-unknown-unknown/release/focus_timer.wasm + NOCTERM_REQUIRE_WASM_GUEST: "1" + run: | + packages=$(cargo metadata --locked --no-deps --format-version 1 | jq -r \ + '.packages[] | select(.features | has("user-plugins") or has("user")) | "--package=\(.name)"') + echo "Testing with user plugins:" $packages + cargo test --locked --features nocterm/user-plugins $packages + + vendored: + name: Vendored crates + runs-on: ubuntu-latest + timeout-minutes: 30 + steps: + - uses: actions/checkout@v5 + with: + ref: ${{ inputs.ref || github.ref }} + - uses: actions/setup-python@v5 + with: + python-version: "3.12" + - name: Test archive authentication and replay failures + run: python3 scripts/test-vendor-patches.py + - name: Replay published archives and patches + if: ${{ !cancelled() }} + run: python3 scripts/check-vendor-patches.py + - name: Install native dependencies + if: ${{ !cancelled() }} + run: | + sudo apt-get update + sudo apt-get install -y build-essential pkg-config clang cmake libssl-dev \ + libfontconfig1-dev libfreetype6-dev libxkbcommon-dev libxkbcommon-x11-dev \ + libwayland-dev libxcb1-dev libxcb-render0-dev libxcb-shape0-dev libxcb-xfixes0-dev \ + libvulkan-dev mesa-vulkan-drivers dbus + - uses: dtolnay/rust-toolchain@stable + if: ${{ !cancelled() }} + with: + toolchain: "1.98.0" + - uses: Swatinem/rust-cache@v2 + if: ${{ !cancelled() }} + with: + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} + workspaces: | + vendor/alacritty_terminal -> target + vendor/gpui-component -> target + - name: Test the vendored terminal emulator + if: ${{ !cancelled() }} + run: cargo test --manifest-path vendor/alacritty_terminal/Cargo.toml --lib - name: Verify native menu patch + if: ${{ !cancelled() }} run: cargo test --manifest-path vendor/gpui-component/Cargo.toml --locked --features test-support --lib menu::app_menu_bar - - run: cargo xtask docs --check - - run: cargo xtask architecture - - run: cargo doc --locked --workspace --no-deps device-unlock-native: name: Device unlock on ${{ matrix.os }} @@ -265,6 +374,9 @@ jobs: toolchain: "1.98.0" components: clippy - uses: Swatinem/rust-cache@v2 + with: + cache-on-failure: true + save-if: ${{ github.event_name == 'push' }} - name: Compile and link native adapters run: cargo build --locked -p nocterm-vault-store -p nocterm-device-unlock --all-targets - name: Lint native adapters @@ -278,7 +390,7 @@ jobs: ci-passed: name: CI passed if: always() - needs: [agent-guardrails, release-pipeline, conventions, integration-history, notices, unused-dependencies, rust, device-unlock-native, vendor-patches] + needs: [policy, conventions, integration-history, lint, test, user-plugins, vendored, device-unlock-native] runs-on: ubuntu-latest timeout-minutes: 5 steps: diff --git a/.github/workflows/package.yml b/.github/workflows/package.yml index bec49539..68fbb182 100644 --- a/.github/workflows/package.yml +++ b/.github/workflows/package.yml @@ -1,10 +1,10 @@ name: Package # Builds installers: Windows (Inno Setup), Linux .deb, .rpm and AppImage. -# Runs on master pushes, manually, or from release.yml for release attachments. +# Runs manually, or from release.yml: once to validate a prepared release commit and +# once to attach the installers to the published release. A push to master builds no +# installers of its own; release.yml already builds them for that commit. on: - push: - branches: [master] workflow_dispatch: workflow_call: inputs: @@ -36,7 +36,9 @@ jobs: toolchain: "1.98.0" - uses: Swatinem/rust-cache@v2 - name: Install notice generator - run: cargo install --locked --version 0.9.2 --features cli cargo-about + uses: taiki-e/install-action@v2 + with: + tool: cargo-about@0.9.2 - name: Verify notices run: python3 scripts/generate-license-notices.py --check - name: Reject tracked release artifacts @@ -49,9 +51,9 @@ jobs: raise SystemExit("Release artifacts must not be tracked: " + ", ".join(artifacts)) PYTHON + # Installers build while the notices are checked; only the upload waits for both. linux: name: Linux packages - needs: licenses # The oldest supported runner keeps the glibc requirement low. runs-on: ubuntu-22.04 timeout-minutes: 60 @@ -71,8 +73,11 @@ jobs: toolchain: "1.98.0" - uses: Swatinem/rust-cache@v2 - name: Install packaging tools + uses: taiki-e/install-action@v2 + with: + tool: cargo-deb,cargo-generate-rpm + - name: Install AppImage tool run: | - cargo install --locked cargo-deb cargo-generate-rpm mkdir -p "$HOME/.local/bin" curl -fsSL -o "$HOME/.local/bin/appimagetool" \ https://github.com/AppImage/appimagetool/releases/download/continuous/appimagetool-x86_64.AppImage @@ -90,7 +95,6 @@ jobs: windows: name: Windows installer - needs: licenses runs-on: windows-latest timeout-minutes: 60 steps: @@ -122,7 +126,7 @@ jobs: upload: name: Attach to release if: inputs.tag != '' - needs: [linux, windows] + needs: [licenses, linux, windows] runs-on: ubuntu-latest timeout-minutes: 10 steps: diff --git a/.github/workflows/release.yml b/.github/workflows/release.yml index 139e3b6a..4b596db0 100644 --- a/.github/workflows/release.yml +++ b/.github/workflows/release.yml @@ -42,7 +42,9 @@ jobs: if: steps.release.outputs.pr != '' - name: Install notice generator if: steps.release.outputs.pr != '' - run: cargo install --locked --version 0.9.2 --features cli cargo-about + uses: taiki-e/install-action@v2 + with: + tool: cargo-about@0.9.2 - name: Prepare release notices if: steps.release.outputs.pr != '' id: prepare diff --git a/.gitignore b/.gitignore index 04aad6fc..1f70ac52 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,8 @@ # Rust / Cargo target/ +/target-contract-msrv/ +# Per-worktree Cargo target directories (target-) +/target-*/ **/*.rs.bk *.pdb @@ -66,3 +69,23 @@ ehthumbs.db .claude/worktrees/ .claude/plans/ mcp.db + +# Playwright MCP session logs and snapshots +.playwright-mcp/ + +# Local data, dumps and transcripts that must never be published +*.sqlite +*.sqlite3 +*.db +*.dump +*.p12 +*.pfx +id_rsa* +id_ecdsa* +id_ed25519* +!crates/ssh-keys/src/tests/fixtures/** +.claude/*.local.* +.claude/reviews/ +CLAUDE.local.md +.aider* +.cursor/ diff --git a/AGENTS.md b/AGENTS.md index f5ce1a3f..e1f4b657 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -1,190 +1,137 @@ # Agent guide -Instructions for AI agents working in this repository. Humans: see [CONTRIBUTING.md](CONTRIBUTING.md). +Follow [CONTRIBUTING.md](CONTRIBUTING.md) for Git, PR and vendor rules. ## Hard rules -- Follow [CONTRIBUTING.md](CONTRIBUTING.md): Conventional Commits, `/` branches, never commit to `master` or `dev` directly. Record the target (`master` release / `dev` integration) and branch from it. -- Never edit `CHANGELOG.md`, `version.txt`, `.release-please-manifest.json` or create tags — releases are automated ([docs/RELEASING.md](docs/RELEASING.md)). -- Never bypass hooks (`--no-verify`) or weaken a check to make it pass. -- Run every Cargo command with `CARGO_BUILD_JOBS=2` and serialize top-level Cargo - commands, including those from other agents, to stay within available memory: - prefix them with `.claude/hooks/cargo-serial.sh`, which does both. -- Commit, push and open PRs only when the user asks. -- Report verification honestly: what was run, what passed, what wasn't checked. -- Keep Rust files at or under 800 lines ([CONTRIBUTING.md](CONTRIBUTING.md#code-size)): split a - growing file into child modules by responsibility instead of adding to it. Never add an - entry to `xtask/oversized-files.toml`. +- Record the target (`master` release / `dev` integration); branch from it using + `/`. Never commit directly to `master` or `dev`. +- Commit, push and open PRs only when the user asks; use Conventional Commits. +- Never edit `CHANGELOG.md`, `version.txt`, `.release-please-manifest.json` or + create tags: releases are automated. Never bypass hooks or weaken checks. +- Rust files stay at or under 800 lines, tests included. This is a ceiling, not + a target; never add an entry to `xtask/oversized-files.toml`. +- Report actual verification and its limits. A structural check does not prove + correct ownership, rendering or performance. ## Read first -Before designing or changing code, read [docs/ARCHITECTURE.md](docs/ARCHITECTURE.md), -the layer note of each touched crate, the [ADR index](docs/adr/README.md) and relevant -accepted decisions, and the crate's `//!` documentation in `lib.rs`. -Accepted ADRs are immutable; supersede a decision with a new ADR. +Read [the layer table](docs/ARCHITECTURE.md#layers), the affected crate's `//!` +card and relevant sections of its layer note. Use the [ADR index](docs/adr/README.md) +to find current decisions for the changed contracts, including superseding records. +Expand reading when dependencies or ownership remain unclear; an incidental crate +mention does not require reading every historical ADR. Accepted ADRs are immutable. ## Where code goes -Name the crate and layer in the plan, in this order: +Name the crate/layer and responsibility before non-trivial changes: -1. Rules and data formats without I/O belong in domain crates. -2. Real filesystem, network and process I/O behind contracts belongs in adapters. -3. GPUI entities with background work and no views belong in service crates. -4. Views and presentation state belong in feature crates. -5. Features communicate through narrow workspace ports named for the consumer's - need; do not expand a broad trait for another feature pair. +1. Pure rules and formats: domain; reusable low-level primitives: foundation. +2. Real filesystem, network and process I/O behind contracts: adapters. +3. GPUI entities with background work and no views: services. +4. Views and presentation: features; shared components: ui. Composition: app. +5. Features meet through narrow consumer-named ports, never feature dependencies. -If the right crate does not exist, propose it instead of using the nearest crate. -Existing mixed responsibilities are migration debt, not a pattern for new code. -Use `Composer` and `SessionState` ([ADR 0009](docs/adr/0009-thread-composer.md), -[ADR 0010](docs/adr/0010-session-lifecycle.md)), `containers` / `containers-ui`, and -`src/bootstrap.rs` as examples of ownership and composition. +Reject a candidate that violates its layer or ownership card. Reuse a fitting +crate; propose a new one if none fits. Existing mixed responsibilities are debt. +For unclear placement or extraction use [placement](.claude/skills/placement/SKILL.md) +and [responsibility-map](.claude/skills/responsibility-map/SKILL.md). + +## Design rules + +1. Keep state within one responsibility. Before adding an owner, name its state, + invariant, transitions and lifetime, including cancellation/late results. + Do not add fields above 15 or spread a parent's impl into more files. +2. Stateful types gain no `pub`/`pub(crate)` fields. DTOs/settings may expose + plain data; transitions preserve invariants and one authoritative state. +3. No new `impl Global`, reads of another crate's global, or `on_app_quit`. + Inject collaborators explicitly; compose their lifecycle in bootstrap. +4. Fallible cross-crate APIs use typed errors (`thiserror`), preserving causes; + convert to text only for display/logging. +5. No I/O in domain code or protocol-specific types (including ACP) in its APIs; + no domain rules in views. Existing violations do not authorize growth. +6. No new `#[expect]`/`#[allow]` for `clippy::too_many_lines` or + `clippy::cognitive_complexity`. `xtask/baseline.toml` may only shrink; + lower entries when debt shrinks. See [guardrails](docs/architecture/guardrails.md). +7. Keep files focused and easy to read. Separate independent responsibilities + when they emerge, even below warning thresholds. Extract state with its + transitions, or cohesive pure functions; never split just by line count, + invent empty owners, or fill a file to its limit. Shared types remain with + their owner. Keep vendor and application changes in separate commits. + +Warnings (640 lines, 12 fields, four impl files) invite inspection, not automatic +extraction. Compare plausible alternatives for consequential decisions: correctness, +reuse, runtime/memory cost and maintenance. Choose the simplest adequate solution; +avoid speculative frameworks and an exhaustive search for a theoretical optimum. ## Reuse before writing -- Ordered persistence: `nocterm_store::{Persisted, ShutdownCoordinator}` - ([ADR 0013](docs/adr/0013-persistence-shutdown-service.md)); the common service - uses `nocterm_core::persist::{WriteQueue, WriteGate, ShutdownDeadline}` - ([ADR 0004](docs/adr/0004-write-queue.md)). -- Atomic files: `nocterm_core::fs::{write_private_atomic, create_private, -AtomicPublication}` for private data; `nocterm_core::persist::{save, save_text, -save_preserving}` for configuration. Check their symlink and format semantics - before reuse ([ADR 0015](docs/adr/0015-private-file-publication.md)). -- Shutdown, link-safe filesystem traversal and test fakes: inspect existing - implementations before adding one; propose shared extraction for missing reuse. - -Layout, settings, snippets, connection state, Runtime chat/favorites and output -recording use the shared persistence service; keyed documents and append streams -share its driver. Native filesystem capabilities are -`nocterm-session::local_fs::{LocalFs, LocalMutations}`, implemented by the injected -`nocterm-localfs` adapter. Session `test-support` supplies shared in-memory port -fakes for dev-dependencies. `ProgramOpener` requests external local programs; the -injected `nocterm-local::LocalProgramOpener` owns their bounded native launch/wait -lifecycle. `nocterm-local::fs` is not an API. +Search for an existing capability and check its semantics before adding one: -## Design rules +- Persistence: `nocterm_store::{Persisted, ShutdownCoordinator}` and its keyed/append + owners; lower-level ordering: `nocterm_core::persist::{WriteQueue, WriteGate, ShutdownDeadline}`. +- Private files: `nocterm_core::fs::{write_private_atomic, create_private, AtomicPublication}`; + configuration: `nocterm_core::persist::{save, save_text, save_preserving}`. + Check [symlink/publication semantics](docs/adr/0015-private-file-publication.md). +- Native files: `nocterm-session::local_fs::{LocalFs, LocalMutations}` via + `nocterm-localfs`; programs: `ProgramOpener` via `nocterm-local::LocalProgramOpener`. + `nocterm-local::fs` is not an API. Reuse shutdown, traversal and shared port fakes. + +Use [port-design](.claude/skills/port-design/SKILL.md) for contracts and +[wiring](.claude/skills/wiring/SKILL.md) for retained installation/persistence. + +## Performance -These rules block review. `cargo xtask architecture` checks layers, file size, -complexity attributes and the exact debt inventory: field counts, public state, -impl spread, globals and panicking reads, domain I/O, quit handlers and string -errors. See [architecture guardrails](docs/architecture/guardrails.md) and -[ADR 0012](docs/adr/0012-architecture-debt-ratchets.md). Entries in -`xtask/baseline.toml` may only decrease; update them when debt shrinks. - -1. Add state only within a type's single responsibility. Do not add a field when - the resulting count exceeds 15 or create a type over that limit; extract an owner with - private fields and transition methods. Do not spread its impl across more files. -2. Stateful types gain no `pub` or `pub(crate)` fields. DTOs and settings may expose - plain data; methods preserve stateful invariants. -3. No new `impl Global`, reads of another crate's global, or `on_app_quit` handlers. - Pass collaborators through explicit dependencies and constructors; compose - them in bootstrap. Existing globals do not authorize new service lookup. -4. Fallible APIs crossing crate boundaries use typed errors (`thiserror`); - convert them to `String` at display only. -5. Add no I/O to domain code or protocol-specific types (including ACP) to its APIs, and no - domain rules to views. Existing violations do not authorize growth. -6. Never add `#[expect]` or `#[allow]` for `clippy::too_many_lines` or - `clippy::cognitive_complexity`, and never raise a baseline to pass a check. -7. Extract responsibilities into modules that own types, not extra methods of a - parent type. Keep vendor changes separate from application code changes. +For changed hot paths, assess frequency × work per event and retained memory. +Keep blocking I/O and expensive computation off render/input callbacks. Remove +redundant work, repeated parsing/scans and unchanged-state notifications; use bounded, +coalesced background work with cancellation. Check the affected visible behavior. +Cache worthwhile repeated work with an explicit owner, complete key, invalidation, +memory bound and teardown; never use a cache as authorization or a second source +of truth. Follow [performance](.claude/skills/performance/SKILL.md) for rendering, +cache and cost evidence. Measure representative before/after cases for optimization +claims; describe unmeasured expectations honestly. Routine edits need no benchmark suite. ## Tests -- Test behavior through actions, events, rendering and public methods; do not - read or write private view/model fields to reproduce a regression. -- Name tests and files after behavior, and extend its existing test file. Do not - create files named after review rounds (`audit`, `verification`, `round2`, etc.). -- Reuse existing port fakes and extend their capabilities. If they cannot be - shared yet, propose a shared home instead of duplicating another fake. +Test through public behavior, actions, events and rendering, not private view/model +fields. Extend behavior-named tests; reuse/extend shared port fakes. Refactors keep +assertions and observable behavior; mechanical import/fixture moves are allowed. +Use [behavior-tests](.claude/skills/behavior-tests/SKILL.md) for coverage and examples. ## Documents -- `docs/` describes the current system. Plans, progress, session logs and audit - trails belong in ignored `.claude/plans/` or the PR description. -- Never record session authorization or implementation status in `docs/`. -- When removing or renaming a symbol, search `docs/` and update current references; - accepted ADRs retain historical context. -- Cross-cutting decisions get an ADR. Architecture notes describe layers, ports - and invariants; product instructions belong in user documentation. -- For new crates, start `lib.rs` with a short `//!` card: purpose, `Owns:`, - `Does not own:`, ports used/provided, settings section, globals and invariants. - Describe actual boundaries; existing crates gain cards when their boundaries change. - -## Pipeline - -Non-trivial work goes through four stages. Each stage is a subagent in `.claude/agents/`; the main session orchestrates (run it with `/feature `). - -| # | Stage | Agent | Writes code | Output | -| --- | ------ | ------------- | ----------- | ------------------------------------------------- | -| 1 | Design | `architect` | plan only | plan: files, steps, risks, test strategy | -| 2 | Build | `implementer` | yes | code + tests on a feature branch | -| 3 | Verify | `tester` | tests only | test run results, added coverage, reproduced bugs | -| 4 | Review | `reviewer` | plan only | findings ranked by severity, verdict | - -Gates: - -- **Design → Build**: the user approves the plan. -- **Verify → Review**: build, lint and tests are green. -- **Review → Done**: no blocking findings. Blocking findings go back to `implementer`; after two failed rounds, stop and escalate to the user. - -Trivial changes (typos, one-line fixes, docs) skip the pipeline. - -`/feature` routes a task to a workflow skill (`crate-new`, `crate-extend`, -`refactor`, `crate-split`, `debt-paydown`) built from procedure skills that each -agent preloads. Stages share decisions through `.claude/plans//` -([plan contract](.claude/skills/feature/plan-bus.md)). Committed hooks in -`.claude/settings.json` enforce the hard rules and stage roles -([ADR 0059](docs/adr/0059-architecture-procedure-skills.md)); CI stays authoritative. -Pass the target branch and verification scope to every stage. Review includes -uncommitted changes as well as commits so it can finish before commit authorization. +Keep `docs/` about the current system; plans, progress and audit trails go in ignored +`.claude/plans/`. Do not record session authorization/status in `docs/`. +Update current references when symbols move; accepted ADRs keep historical context. +Cross-cutting decisions get an ADR. New crates start with a short `//!` ownership +card; update existing cards when boundaries change. Details: [docs-adr](.claude/skills/docs-adr/SKILL.md). + +## Workflow + +Use one agent and one compact ignored plan for ordinary non-trivial work: goal, +branch/base, affected owners/layers, approach, risks and checks. Trivial edits need +no plan. Load only procedures relevant to the task; output filenames are optional. +Delegate independent work when complexity/risk warrants it; supply scope and evidence. +A request to implement a plan authorizes routine decisions within it. Ask only for +material unresolved choices or changes of scope/authority. Review committed, +staged, unstaged and relevant untracked task changes using [design-review](.claude/skills/design-review/SKILL.md). +Fix actionable findings; escalate concrete blockers or repeated lack of progress. +No blocking findings may remain at completion. Preserve unrelated work. + +## Verification policy + +Use [gate](.claude/skills/gate/SKILL.md) to select checks by changed inputs and +consumers. Check coherent edits, not each moved method; a test already compiles +its target. Reuse results while relevant source/configuration stays unchanged. +For parallel work, use one verification executor and do not edit checked inputs +while checks run. Keep stable Cargo targets; investigate before clearing caches. +Cargo uses local settings; the coordination helper is optional (CONTRIBUTING.md). ## Definition of done -Code changes require `cargo fmt --all --check`, -`cargo build --workspace --all-targets`, -`cargo clippy --workspace --all-targets -- -D warnings`, `cargo test --workspace`, -`cargo xtask architecture` and `cargo xtask docs --check` to pass. Run additional -checks below when relevant (for example mandatory SSH/SFTP tests for SSH changes). -For documentation-only changes, check links, stale references and generated-doc -freshness; architecture checks still apply. Build, lint and runtime tests may be -omitted because executable inputs did not change. - -Report each command and its result. Report omitted or unavailable checks with the -reason. No blocking review findings may remain; commit, push and PR creation -still require the user's request. - -## Scopes - -A commit scope is the crate directory name under `crates/` (`agent-runtime`, -`connections-model`, …). Special scopes: `app` (root `src/`), `xtask`, `deps`. - -## Commands - -```sh -# Agents prefix each cargo command with .claude/hooks/cargo-serial.sh. -export CARGO_BUILD_JOBS=2 -cargo run -cargo fmt --all --check -cargo build --workspace --all-targets -cargo clippy --workspace --all-targets -- -D warnings -cargo test --workspace -NOCTERM_REQUIRE_SSHD=1 cargo test -p nocterm-ssh --test sshd -cargo xtask docs -cargo xtask docs --check -cargo xtask architecture -cargo xtask survey [--json] [--type ] -.claude/hooks/cargo-serial.sh cargo -python3 scripts/test-claude-hooks.py -python3 scripts/check-claude-config.py -cargo machete --skip-target-dir -cargo doc --workspace --no-deps -j1 -python3 scripts/generate-license-notices.py --check-inputs -``` - -Linux system dependencies and setup are in [README.md](README.md). GUI checks -require an X11 or Wayland session and Vulkan driver. Regenerate references after -changing schemas, tokens, actions, keymap or internal Cargo dependencies. -Regenerate `THIRD_PARTY_NOTICES.txt` (`python3 scripts/generate-license-notices.py`, -needs `cargo-about` 0.9.2) after changing dependencies, vendored manifests or license -files, or bundled assets; CI and the pre-push hook reject stale notices -([docs/PACKAGING.md](docs/PACKAGING.md)). +After review fixes, finish the [completion matrix and applicable fixtures](.claude/skills/gate/SKILL.md#definition-of-done). +Broad shared changes, uncertain workspace impact or an explicit full-verification +request require the full suite there. Required CI and limits remain unchanged. +Report actual commands/results, scope and omitted checks with reasons; a scoped +pass is not a workspace pass. Commit, push and PR still require the user's request. diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 83a0f1f1..0f590776 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -87,10 +87,13 @@ Pick the type by what the *user of the software* sees, not by which files change ## Code size A Rust source file may have at most 800 lines, tests included. `cargo xtask architecture` -(run in CI) enforces it. When a file approaches the limit, split it by responsibility into -child modules (`foo.rs` + `foo/part.rs`); move a large `mod tests` into `foo/tests.rs`. -For production code, an extracted module owns a responsibility and its own type; -moving more methods of the same growing type into another file is not an extraction. +(run in CI) enforces this ceiling; it is not a target size. Keep files focused on +one coherent responsibility. Separate independent responsibilities as they emerge, +even below warning thresholds, into named child modules; move a large `mod tests` +into `foo/tests.rs`. Stateful extractions own their state and transitions; pure +logic can be a cohesive module of functions without an artificial owner type. +Moving more methods of the same growing type into another file is not an extraction. +Do not fragment a coherent module just to meet an arbitrary preferred line count. The legacy exception list `xtask/oversized-files.toml` is empty; never add an entry to make a check pass. @@ -111,6 +114,51 @@ constrains large structs, implementation spread, public state, globals, shutdown hooks, string errors and domain I/O. Lower or remove entries when code improves; never regenerate or raise the inventory. See [architecture guardrails](docs/architecture/guardrails.md). +## Verification + +During development, choose the smallest check that answers the current question: +one regression test, one test target, a library or an affected package. Batch a +coherent edit before checking it. Do not run a fixed build/lint/test/architecture +bundle after every edit or extraction. A test compiles its target; running both +`cargo check` and `cargo build` first is usually redundant. + +At completion, follow the input-based [completion matrix](.claude/skills/gate/SKILL.md#definition-of-done). +Scoped changes use affected targets and consumers; broad shared changes or +uncertain workspace impact need full workspace verification. Required CI is +independent. Reuse valid results and rerun a failing check first; keep evidence +tied to its source/configuration. The [gate procedure](.claude/skills/gate/SKILL.md) +provides examples. Documentation and script-only work use relevant links, +script tests and configuration validators unless they affect Rust/generator inputs. + +For changed hot paths, account for work per event, frequency and retained memory. +Rendering and input callbacks consume prepared state; expensive work has bounded +background execution. Cache repeated work only with clear keys, invalidation, +bounds and lifecycle. Verify affected interaction and measure optimization claims +with representative before/after workloads; see the [performance procedure](.claude/skills/performance/SKILL.md). + +### Cargo resources + +Cargo keeps the caller's `CARGO_BUILD_JOBS`, Cargo configuration and normal +defaults. Choose a local budget for the machine; no fixed job count is required. +When parallel builds need coordination, the optional helper serializes commands: + +```sh +.claude/hooks/cargo-serial.sh cargo test -p nocterm-ssh +``` + +Its default lock lives in the Git common directory, so linked worktrees share it +and independent clones do not. `NOCTERM_CARGO_LOCK=` selects another lock +(use an absolute path to coordinate different working directories or repositories). +`NOCTERM_CARGO_LOCK=off` runs without the helper lock. The helper needs `flock` +when locking; if unavailable it fails promptly with instructions. Direct Cargo +is supported on every platform and keeps Cargo's own target-directory locking. + +For shared verification, coordinate one executor and avoid duplicate checks on +the same inputs. Keep a stable target directory per worktree; do not touch all +sources or clean caches to start another check. Investigate a concrete stale +artifact first. Local resource settings may be supplied in the shell or personal +configuration and do not belong in committed product rules. + ## Pull requests Vendored forks have reproducible binary patches against checksum-pinned published diff --git a/Cargo.lock b/Cargo.lock index 985f7214..cce13a4c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -236,7 +236,7 @@ dependencies = [ "rustix-openpty", "serde", "signal-hook", - "unicode-width", + "unicode-width 0.2.2", "vte", "windows-sys 0.59.0", ] @@ -282,7 +282,7 @@ checksum = "f211a51805bc641f3ad5b7664c77d2547af685cc33b4cd8d31964027a46f13f1" dependencies = [ "anstyle", "memchr", - "unicode-width", + "unicode-width 0.2.2", ] [[package]] @@ -434,9 +434,9 @@ dependencies = [ [[package]] name = "async-executor" -version = "1.14.0" +version = "1.14.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c96bf972d85afc50bf5ab8fe2d54d1586b4e0b46c97c50a0c9e71e2f7bcd812a" +checksum = "f426a31126c735126297bc5ea7a521263c9faff4f63ca415267ee625651c4f37" dependencies = [ "async-task", "concurrent-queue", @@ -1175,6 +1175,15 @@ version = "0.5.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "0c9ea0ac24bc397ab3c98583a3c9ba74fa56b09a4449bbe172b9b1ddb016027a" +[[package]] +name = "cobs" +version = "0.3.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "0fa961b519f0b462e3a3b4a34b64d119eeaca1d59af726fe450bbba07a9fc0a1" +dependencies = [ + "thiserror 2.0.21", +] + [[package]] name = "cocoa" version = "0.25.0" @@ -1242,7 +1251,7 @@ checksum = "af491d569909a7e4dee0ad7db7f5341fef5c614d5b8ec8cf765732aba3cff681" dependencies = [ "serde", "termcolor", - "unicode-width", + "unicode-width 0.2.2", ] [[package]] @@ -1572,9 +1581,9 @@ checksum = "460fbee9c2c2f33933d720630a6a0bac33ba7053db5344fac858d4b8952d77d5" [[package]] name = "crypto-bigint" -version = "0.7.5" +version = "0.7.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1a52aa3fcda4e6302a9f48734f234d35d4721b96f8fe07d073f07ce9df4f0271" +checksum = "914c195c4d7fbc5ed7f88d14cbf85914d5c664967658d43404b263913a477e82" dependencies = [ "cpubits", "ctutils", @@ -1639,9 +1648,9 @@ dependencies = [ [[package]] name = "ctutils" -version = "0.4.2" +version = "0.4.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7d5515a3834141de9eafb9717ad39eea8247b5674e6066c404e8c4b365d2a29e" +checksum = "261af4dc7a06ea8a527a8717d8cbba3417530d6ea14e1edb3de6b67806be474f" dependencies = [ "cmov", "subtle", @@ -2069,6 +2078,18 @@ dependencies = [ "winreg 0.55.0", ] +[[package]] +name = "embedded-io" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ef1a6892d9eef45c8fa6b9e0086428a2cca8491aca8f787c534a3d6d0bcb3ced" + +[[package]] +name = "embedded-io" +version = "0.6.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d" + [[package]] name = "encoding_rs" version = "0.8.42" @@ -3753,6 +3774,12 @@ version = "1.10.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "6dbf3de79e51f3d586ab4cb9d5c3e2c14aa28ed23d180cf89b4df0454a69cc87" +[[package]] +name = "httpdate" +version = "1.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "df3b46402a9d5adb4c86a0cf463f42e19994e3ee891101b1841f30a545cb49a9" + [[package]] name = "hybrid-array" version = "0.4.15" @@ -3779,6 +3806,7 @@ dependencies = [ "http", "http-body", "httparse", + "httpdate", "itoa", "pin-project-lite", "smallvec", @@ -3800,7 +3828,20 @@ dependencies = [ "tokio", "tokio-rustls", "tower-service", - "webpki-roots 1.0.9", + "webpki-roots 1.0.11", +] + +[[package]] +name = "hyper-timeout" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b90d566bffbce6a75bd8b09a05aa8c2cb1fabb6cb348f8840c9e4c90a0d83b0" +dependencies = [ + "hyper", + "hyper-util", + "pin-project-lite", + "tokio", + "tower-service", ] [[package]] @@ -4394,6 +4435,12 @@ dependencies = [ "leak", ] +[[package]] +name = "leb128fmt" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "09edd9e8b54e49e587e4f6295a7d29c3ea94d469cb40ab8ca70b288248a81db2" + [[package]] name = "lebe" version = "0.5.3" @@ -4508,6 +4555,38 @@ dependencies = [ "value-bag", ] +[[package]] +name = "logos" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "eb2c55a318a87600ea870ff8c2012148b44bf18b74fad48d0f835c38c7d07c5f" +dependencies = [ + "logos-derive", +] + +[[package]] +name = "logos-codegen" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "58b3ffaa284e1350d017a57d04ada118c4583cf260c8fb01e0fe28a2e9cf8970" +dependencies = [ + "fnv", + "proc-macro2", + "quote", + "regex-automata", + "regex-syntax", + "syn 2.0.119", +] + +[[package]] +name = "logos-derive" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "52d3a9855747c17eaf4383823f135220716ab49bea5fbea7dd42cc9a92f8aa31" +dependencies = [ + "logos-codegen", +] + [[package]] name = "loop9" version = "0.1.5" @@ -4736,6 +4815,28 @@ dependencies = [ "paste", ] +[[package]] +name = "miette" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5f98efec8807c63c752b5bd61f862c165c115b0a35685bdcfd9238c7aeb592b7" +dependencies = [ + "cfg-if", + "miette-derive", + "unicode-width 0.1.14", +] + +[[package]] +name = "miette-derive" +version = "7.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db5b29714e950dbb20d5e6f74f9dcec4edbcc1067bb7f8ed198c097b8c1a818b" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.119", +] + [[package]] name = "mime" version = "0.3.17" @@ -4834,6 +4935,12 @@ dependencies = [ "pxfm", ] +[[package]] +name = "multimap" +version = "0.10.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084" + [[package]] name = "multiversion_no_op" version = "1.0.0" @@ -4919,6 +5026,7 @@ name = "nocterm" version = "3.3.0" dependencies = [ "anyhow", + "async-io", "futures", "gpui-kit", "nocterm-acp", @@ -4927,22 +5035,52 @@ dependencies = [ "nocterm-agent-runtime", "nocterm-agent-settings-ui", "nocterm-agent-storage", - "nocterm-agent-usage", + "nocterm-agent-tools", "nocterm-ai", "nocterm-connections", "nocterm-connections-io", "nocterm-connections-model", + "nocterm-connections-store", + "nocterm-containers", "nocterm-containers-ui", "nocterm-core", "nocterm-design", "nocterm-device-unlock", + "nocterm-directory-io", + "nocterm-explorer", "nocterm-files", + "nocterm-hub-agents", + "nocterm-hub-agents-model", + "nocterm-hub-io", + "nocterm-hubs", + "nocterm-hubs-model", + "nocterm-hubs-ui", "nocterm-image-codec", "nocterm-keymap", "nocterm-keymap-ui", + "nocterm-layout-ui", "nocterm-local", "nocterm-localfs", + "nocterm-monitor", "nocterm-monitor-ui", + "nocterm-plugin-api", + "nocterm-plugin-enablement", + "nocterm-plugin-host", + "nocterm-plugin-model", + "nocterm-plugin-net", + "nocterm-plugin-packages", + "nocterm-plugin-ports", + "nocterm-plugin-view", + "nocterm-plugin-wasm", + "nocterm-plugins-ui", + "nocterm-project-container-fs", + "nocterm-project-environment", + "nocterm-project-model", + "nocterm-project-providers", + "nocterm-project-runtime", + "nocterm-project-service", + "nocterm-project-storage", + "nocterm-projects", "nocterm-session", "nocterm-settings", "nocterm-settings-ui", @@ -4951,6 +5089,7 @@ dependencies = [ "nocterm-snippets-store", "nocterm-snippets-ui", "nocterm-ssh", + "nocterm-ssh-keys", "nocterm-store", "nocterm-terminal", "nocterm-theme-service", @@ -4962,13 +5101,18 @@ dependencies = [ "nocterm-vault", "nocterm-vault-store", "nocterm-vault-ui", + "nocterm-window-layout", "nocterm-workspace", + "nocterm-workspace-ports", "serde_json", "tempfile", + "thiserror 2.0.21", + "tokio", "toml 1.1.6+spec-1.1.0", "tracing", "tracing-subscriber", "uuid", + "wat", "winresource", ] @@ -5009,28 +5153,36 @@ dependencies = [ "nocterm-agent-providers", "nocterm-agent-runtime", "nocterm-ai", + "nocterm-hub-agents", + "nocterm-hubs-model", + "nocterm-plugin-model", + "nocterm-project-model", "nocterm-session", "nocterm-store", + "nocterm-transfers", "nocterm-ui", "nocterm-workspace", - "serde", + "nocterm-workspace-ports", "serde_json", "tempfile", "thiserror 2.0.21", + "tracing", ] [[package]] name = "nocterm-agent-chat" version = "0.0.0" dependencies = [ - "async-channel", "futures", "gpui-pre", "nocterm-agent-directory", "nocterm-agent-runtime", "nocterm-ai", + "nocterm-core", + "nocterm-hubs-model", "nocterm-session", "nocterm-store", + "nocterm-transfers", "nocterm-workspace-ports", "serde", "serde_json", @@ -5045,6 +5197,7 @@ version = "0.0.0" dependencies = [ "gpui-pre", "nocterm-ai", + "nocterm-hubs-model", "nocterm-store", ] @@ -5066,7 +5219,10 @@ dependencies = [ "nocterm-agent-providers", "nocterm-ai", "nocterm-core", + "nocterm-hubs-model", + "nocterm-session", "nocterm-store", + "nocterm-transfers", "serde_json", "tempfile", "thiserror 2.0.21", @@ -5095,21 +5251,19 @@ dependencies = [ "futures", "nocterm-ai", "nocterm-core", - "serde_json", + "nocterm-session", "tempfile", "toml 1.1.6+spec-1.1.0", "uuid", ] [[package]] -name = "nocterm-agent-usage" +name = "nocterm-agent-tools" version = "0.0.0" dependencies = [ + "async-channel", "futures", - "gpui-pre-reqwest", - "nocterm-ai", - "serde_json", - "tempfile", + "nocterm-session", ] [[package]] @@ -5120,12 +5274,17 @@ dependencies = [ "base64 0.22.1", "futures", "nocterm-core", + "nocterm-hubs-model", + "nocterm-project-model", + "nocterm-session", "nocterm-settings", + "nocterm-transfers", "regex", "schemars 1.2.2", "serde", "serde_json", "serde_with", + "sha2 0.10.9", "tempfile", "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", @@ -5140,12 +5299,17 @@ dependencies = [ "gpui-kit", "nocterm-connections-model", "nocterm-connections-store", + "nocterm-plugin-model", + "nocterm-project-model", "nocterm-session", + "nocterm-ssh-keys", "nocterm-store", "nocterm-ui", "nocterm-workspace", + "nocterm-workspace-ports", "serde_json", "tempfile", + "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", ] @@ -5161,6 +5325,7 @@ dependencies = [ "serde", "serde_json", "tempfile", + "thiserror 2.0.21", "tracing", "uuid", ] @@ -5169,6 +5334,7 @@ dependencies = [ name = "nocterm-connections-model" version = "0.0.0" dependencies = [ + "futures", "nocterm-core", "nocterm-session", "serde", @@ -5191,6 +5357,7 @@ dependencies = [ "nocterm-store", "schemars 1.2.2", "serde", + "serde_json", "tempfile", "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", @@ -5203,10 +5370,21 @@ version = "0.0.0" dependencies = [ "futures", "nocterm-session", + "serde", "serde_json", "thiserror 2.0.21", ] +[[package]] +name = "nocterm-containers-service" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-containers", + "nocterm-session", +] + [[package]] name = "nocterm-containers-ui" version = "0.0.0" @@ -5214,6 +5392,8 @@ dependencies = [ "futures", "gpui-kit", "nocterm-containers", + "nocterm-containers-service", + "nocterm-plugin-model", "nocterm-session", "nocterm-settings", "nocterm-store", @@ -5229,6 +5409,7 @@ dependencies = [ "futures", "nix 0.28.0", "serde", + "sha2 0.10.9", "tempfile", "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", @@ -5266,13 +5447,25 @@ dependencies = [ "zeroize", ] +[[package]] +name = "nocterm-directory-io" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-explorer", + "nocterm-session", +] + [[package]] name = "nocterm-explorer" version = "0.0.0" dependencies = [ + "futures", + "nocterm-session", "nocterm-settings", "schemars 1.2.2", "serde", + "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", ] @@ -5284,6 +5477,10 @@ dependencies = [ "futures", "gpui-kit", "nocterm-explorer", + "nocterm-plugin-api", + "nocterm-plugin-model", + "nocterm-plugin-ports", + "nocterm-project-model", "nocterm-session", "nocterm-settings", "nocterm-store", @@ -5295,6 +5492,174 @@ dependencies = [ "thiserror 2.0.21", ] +[[package]] +name = "nocterm-hub-agents" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-ai", + "nocterm-hub-agents-model", + "nocterm-hubs-model", + "nocterm-ssh-keys", + "thiserror 2.0.21", + "tracing", +] + +[[package]] +name = "nocterm-hub-agents-model" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-hubs-model", + "nocterm-session", + "thiserror 2.0.21", +] + +[[package]] +name = "nocterm-hub-client" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "base64 0.22.1", + "hex", + "hyper-util", + "nocterm-hub-grpc", + "nocterm-hub-model", + "nocterm-hub-proto", + "nocterm-keys", + "rustls", + "serde", + "sha2 0.11.0", + "thiserror 2.0.21", + "tokio", + "tokio-rustls", + "tokio-stream", + "tonic", + "tower", + "tracing", + "webpki-roots 1.0.11", + "zeroize", +] + +[[package]] +name = "nocterm-hub-grpc" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "nocterm-hub-proto", + "prost", + "prost-types", + "tonic", + "tonic-prost", + "tonic-prost-build", +] + +[[package]] +name = "nocterm-hub-io" +version = "0.0.0" +dependencies = [ + "async-channel", + "base64 0.22.1", + "futures", + "hex", + "nocterm-ai", + "nocterm-connections-model", + "nocterm-core", + "nocterm-hub-agents-model", + "nocterm-hub-client", + "nocterm-hub-proto", + "nocterm-hubs-model", + "nocterm-keys", + "nocterm-session", + "nocterm-vault", + "prost", + "serde", + "serde_json", + "sha2 0.10.9", + "tempfile", + "thiserror 2.0.21", + "tokio", + "toml 1.1.6+spec-1.1.0", + "tonic", + "tracing", + "uuid", + "zeroize", +] + +[[package]] +name = "nocterm-hub-model" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "ed25519-dalek", + "hex", + "serde", + "sha2 0.11.0", + "thiserror 2.0.21", + "uuid", +] + +[[package]] +name = "nocterm-hub-proto" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "nocterm-hub-model", + "prost", + "prost-build", + "protox", +] + +[[package]] +name = "nocterm-hubs" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-core", + "nocterm-hubs-model", + "nocterm-session", + "nocterm-settings", + "nocterm-store", + "thiserror 2.0.21", + "tracing", +] + +[[package]] +name = "nocterm-hubs-model" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-core", + "nocterm-session", + "nocterm-settings", + "schemars 1.2.2", + "serde", + "thiserror 2.0.21", + "toml 1.1.6+spec-1.1.0", +] + +[[package]] +name = "nocterm-hubs-ui" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-kit", + "nocterm-core", + "nocterm-hub-agents", + "nocterm-hub-agents-model", + "nocterm-hubs", + "nocterm-hubs-model", + "nocterm-session", + "nocterm-settings", + "nocterm-ssh-keys", + "nocterm-store", + "nocterm-ui", + "nocterm-workspace", + "thiserror 2.0.21", +] + [[package]] name = "nocterm-image-codec" version = "0.0.0" @@ -5309,6 +5674,7 @@ version = "0.0.0" dependencies = [ "gpui-kit", "nocterm-core", + "nocterm-store", "serde", "tempfile", "thiserror 2.0.21", @@ -5327,17 +5693,52 @@ dependencies = [ "nocterm-ui", ] +[[package]] +name = "nocterm-keys" +version = "0.1.0" +source = "git+https://github.com/cu-e/nocterm-contract?rev=58af32eb20106eaac3c34104b227aae04672ad2f#58af32eb20106eaac3c34104b227aae04672ad2f" +dependencies = [ + "argon2", + "chacha20poly1305", + "ed25519-dalek", + "getrandom 0.4.3", + "hex", + "hkdf 0.13.0", + "nocterm-hub-model", + "sha2 0.11.0", + "thiserror 2.0.21", + "zeroize", +] + +[[package]] +name = "nocterm-layout-ui" +version = "0.0.0" +dependencies = [ + "gpui-kit", + "nocterm-plugin-enablement", + "nocterm-plugin-model", + "nocterm-settings", + "nocterm-store", + "nocterm-ui", + "nocterm-window-layout", + "nocterm-workspace", + "tempfile", +] + [[package]] name = "nocterm-local" version = "0.0.0" dependencies = [ "async-channel", + "chrono", + "filedescriptor", "futures", "nix 0.28.0", "nocterm-core", "nocterm-session", "portable-pty", "process-wrap", + "rustix 1.1.5", "tempfile", "thiserror 2.0.21", "tracing", @@ -5371,22 +5772,291 @@ dependencies = [ "toml 1.1.6+spec-1.1.0", ] +[[package]] +name = "nocterm-monitor-service" +version = "0.0.0" +dependencies = [ + "futures", + "gpui-pre", + "nocterm-monitor", + "nocterm-session", +] + [[package]] name = "nocterm-monitor-ui" version = "0.0.0" dependencies = [ "futures", "gpui-kit", + "nocterm-containers", "nocterm-monitor", + "nocterm-monitor-service", + "nocterm-plugin-api", + "nocterm-plugin-enablement", + "nocterm-plugin-model", + "nocterm-plugin-ports", + "nocterm-project-model", + "nocterm-project-providers", "nocterm-session", "nocterm-settings", "nocterm-store", "nocterm-ui", "nocterm-workspace", + "serde_json", + "tempfile", + "toml 1.1.6+spec-1.1.0", +] + +[[package]] +name = "nocterm-plugin-abi" +version = "0.0.0" +dependencies = [ + "nocterm-plugin-api", + "nocterm-plugin-model", + "postcard", + "serde", + "thiserror 2.0.21", +] + +[[package]] +name = "nocterm-plugin-api" +version = "0.0.0" +dependencies = [ + "nocterm-plugin-model", + "proptest", + "serde", + "serde_json", + "thiserror 2.0.21", + "toml 1.1.6+spec-1.1.0", +] + +[[package]] +name = "nocterm-plugin-enablement" +version = "0.0.0" +dependencies = [ + "nocterm-plugin-model", + "nocterm-settings", + "schemars 1.2.2", + "serde", + "toml 1.1.6+spec-1.1.0", +] + +[[package]] +name = "nocterm-plugin-host" +version = "0.0.0" +dependencies = [ + "gpui-pre", + "nocterm-plugin-api", + "nocterm-plugin-model", + "nocterm-plugin-ports", + "serde", + "serde_json", + "thiserror 2.0.21", + "tracing", +] + +[[package]] +name = "nocterm-plugin-model" +version = "0.0.0" +dependencies = [ + "serde", + "thiserror 2.0.21", + "unicode-general-category", +] + +[[package]] +name = "nocterm-plugin-net" +version = "0.0.0" +dependencies = [ + "gpui-pre-reqwest", + "nocterm-plugin-api", +] + +[[package]] +name = "nocterm-plugin-packages" +version = "0.0.0" +dependencies = [ + "nocterm-core", + "nocterm-plugin-api", + "nocterm-plugin-model", + "proptest", + "sha2 0.10.9", "tempfile", + "thiserror 2.0.21", + "toml 1.1.6+spec-1.1.0", +] + +[[package]] +name = "nocterm-plugin-ports" +version = "0.0.0" +dependencies = [ + "gpui-pre", + "nocterm-plugin-api", + "nocterm-plugin-model", +] + +[[package]] +name = "nocterm-plugin-sdk" +version = "0.0.0" +dependencies = [ + "nocterm-plugin-abi", + "nocterm-plugin-api", + "nocterm-plugin-model", + "thiserror 2.0.21", "toml 1.1.6+spec-1.1.0", ] +[[package]] +name = "nocterm-plugin-view" +version = "0.0.0" +dependencies = [ + "gpui-pre", + "nocterm-plugin-api", +] + +[[package]] +name = "nocterm-plugin-wasm" +version = "0.0.0" +dependencies = [ + "nocterm-plugin-abi", + "nocterm-plugin-api", + "nocterm-plugin-model", + "serde_json", + "wasmi", + "wat", +] + +[[package]] +name = "nocterm-plugins-ui" +version = "0.0.0" +dependencies = [ + "gpui-kit", + "nocterm-core", + "nocterm-plugin-model", + "nocterm-settings", + "nocterm-store", + "nocterm-ui", + "nocterm-workspace", + "thiserror 2.0.21", +] + +[[package]] +name = "nocterm-project-container-fs" +version = "0.0.0" +dependencies = [ + "async-channel", + "async-io", + "futures", + "getrandom 0.4.3", + "nocterm-containers", + "nocterm-project-model", + "nocterm-project-providers", + "nocterm-session", +] + +[[package]] +name = "nocterm-project-environment" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-connections-model", + "nocterm-project-providers", + "nocterm-session", + "serde_json", +] + +[[package]] +name = "nocterm-project-model" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-core", + "nocterm-session", + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "nocterm-project-providers" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-containers", + "nocterm-core", + "nocterm-project-model", + "nocterm-session", + "nocterm-settings", + "schemars 1.2.2", + "serde", + "serde_json", + "thiserror 2.0.21", +] + +[[package]] +name = "nocterm-project-runtime" +version = "0.0.0" +dependencies = [ + "async-io", + "futures", + "getrandom 0.4.3", + "nocterm-containers", + "nocterm-project-model", + "nocterm-project-providers", + "nocterm-session", + "serde", + "serde_json", +] + +[[package]] +name = "nocterm-project-service" +version = "0.0.0" +dependencies = [ + "event-listener", + "futures", + "gpui-pre", + "nocterm-containers", + "nocterm-core", + "nocterm-project-model", + "nocterm-project-providers", + "nocterm-session", + "nocterm-store", + "thiserror 2.0.21", +] + +[[package]] +name = "nocterm-project-storage" +version = "0.0.0" +dependencies = [ + "getrandom 0.4.3", + "nocterm-core", + "nocterm-project-model", + "rustix 1.1.5", + "serde_json", + "tempfile", +] + +[[package]] +name = "nocterm-projects" +version = "0.0.0" +dependencies = [ + "async-channel", + "gpui-kit", + "nocterm-containers", + "nocterm-explorer", + "nocterm-plugin-model", + "nocterm-project-model", + "nocterm-project-providers", + "nocterm-project-service", + "nocterm-session", + "nocterm-store", + "nocterm-ui", + "nocterm-workspace", + "nocterm-workspace-ports", + "thiserror 2.0.21", + "tracing", +] + [[package]] name = "nocterm-session" version = "0.0.0" @@ -5461,6 +6131,7 @@ name = "nocterm-snippets-ui" version = "0.0.0" dependencies = [ "gpui-kit", + "nocterm-plugin-model", "nocterm-session", "nocterm-snippets", "nocterm-snippets-store", @@ -5476,9 +6147,11 @@ name = "nocterm-ssh" version = "0.0.0" dependencies = [ "futures", + "getrandom 0.4.3", "hmac 0.13.0", "nocterm-core", "nocterm-session", + "nocterm-ssh-keys", "russh", "russh-sftp", "rustix 1.1.5", @@ -5486,11 +6159,23 @@ dependencies = [ "tempfile", "thiserror 2.0.21", "tokio", + "tokio-socks", "tracing", "uuid", "zeroize", ] +[[package]] +name = "nocterm-ssh-keys" +version = "0.0.0" +dependencies = [ + "futures", + "nocterm-core", + "nocterm-session", + "ssh-key", + "thiserror 2.0.21", +] + [[package]] name = "nocterm-store" version = "0.0.0" @@ -5510,11 +6195,14 @@ dependencies = [ name = "nocterm-terminal" version = "0.0.0" dependencies = [ - "encoding_rs", "futures", "gpui-kit", + "nocterm-hubs-model", + "nocterm-project-model", + "nocterm-project-providers", "nocterm-session", "nocterm-settings", + "nocterm-ssh-keys", "nocterm-store", "nocterm-terminal-model", "nocterm-theme-service", @@ -5522,6 +6210,7 @@ dependencies = [ "nocterm-ui", "nocterm-vt", "nocterm-workspace", + "nocterm-workspace-ports", "regex", "tempfile", "thiserror 2.0.21", @@ -5609,6 +6298,10 @@ dependencies = [ "image", "nocterm-core", "nocterm-design", + "nocterm-explorer", + "nocterm-plugin-api", + "nocterm-plugin-model", + "nocterm-plugin-ports", "nocterm-session", "nocterm-settings", "nocterm-store", @@ -5676,6 +6369,7 @@ dependencies = [ "gpui-kit", "nocterm-session", "nocterm-settings", + "nocterm-ssh-keys", "nocterm-store", "nocterm-ui", "nocterm-vault", @@ -5688,23 +6382,49 @@ name = "nocterm-vt" version = "0.0.0" dependencies = [ "alacritty_terminal", + "encoding_rs", + "nocterm-session", "regex", "regex-syntax", "thiserror 2.0.21", ] +[[package]] +name = "nocterm-window-layout" +version = "0.0.0" +dependencies = [ + "nocterm-plugin-model", + "nocterm-settings", + "schemars 1.2.2", + "serde", + "serde_json", + "thiserror 2.0.21", + "toml 1.1.6+spec-1.1.0", +] + [[package]] name = "nocterm-workspace" version = "0.0.0" dependencies = [ "gpui-kit", + "nocterm-core", "nocterm-keymap", + "nocterm-plugin-api", + "nocterm-plugin-enablement", + "nocterm-plugin-model", + "nocterm-plugin-ports", + "nocterm-project-model", "nocterm-session", "nocterm-settings", "nocterm-store", "nocterm-ui", + "nocterm-window-layout", "nocterm-workspace-ports", + "schemars 1.2.2", + "serde", + "serde_json", "thiserror 2.0.21", + "toml 1.1.6+spec-1.1.0", "tracing", ] @@ -5713,6 +6433,9 @@ name = "nocterm-workspace-ports" version = "0.0.0" dependencies = [ "gpui-pre", + "nocterm-core", + "nocterm-hubs-model", + "nocterm-project-model", "nocterm-session", "thiserror 2.0.21", ] @@ -6587,6 +7310,17 @@ version = "2.3.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9b4f627cb1b25917193a259e49bdad08f671f8d9708acfd5fe0a8c1455d87220" +[[package]] +name = "petgraph" +version = "0.8.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8701b58ea97060d5e5b155d383a69952a60943f0e6dfe30b04c287beb0b27455" +dependencies = [ + "fixedbitset", + "hashbrown 0.15.5", + "indexmap 2.14.2", +] + [[package]] name = "phc" version = "0.6.1" @@ -6904,6 +7638,18 @@ dependencies = [ "thiserror 1.0.69", ] +[[package]] +name = "postcard" +version = "1.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6764c3b5dd454e283a30e6dfe78e9b31096d9e32036b5d1eaac7a6119ccb9a24" +dependencies = [ + "cobs", + "embedded-io 0.4.0", + "embedded-io 0.6.1", + "serde", +] + [[package]] name = "potential_utf" version = "0.1.6" @@ -7057,6 +7803,118 @@ dependencies = [ "syn 2.0.119", ] +[[package]] +name = "prost" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "528ac67416ff8646872a3c02cad9cc4ee5dc9f9540c9b10771855c95cb2e5ae1" +dependencies = [ + "bytes", + "prost-derive", +] + +[[package]] +name = "prost-build" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "03da047801ff44bb6a4d407d4860c05fd70bb81714e6b2f3812603d5b145b042" +dependencies = [ + "heck 0.5.0", + "itertools 0.14.0", + "log", + "multimap", + "petgraph", + "prettyplease", + "prost", + "prost-types", + "pulldown-cmark", + "pulldown-cmark-to-cmark", + "regex", + "syn 2.0.119", + "tempfile", +] + +[[package]] +name = "prost-derive" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b570b25f7617e43d59005d0990ccb79e950a423952cea19671b7a876da390adf" +dependencies = [ + "anyhow", + "itertools 0.14.0", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "prost-reflect" +version = "0.16.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01b80ea363c31af2de2b92e3c07ed1156628f7838c4afb4df75ee78a37fedbd1" +dependencies = [ + "logos", + "miette", + "prost", + "prost-types", +] + +[[package]] +name = "prost-types" +version = "0.14.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f94967dc7688f3054c7fac87473ffae4cc4c3904800e2d9f5b857246d8963b0a" +dependencies = [ + "prost", +] + +[[package]] +name = "protox" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f43240d7f22cf77d1625885e547acf874dd63cd2790f29f4a38e86256f41996f" +dependencies = [ + "bytes", + "miette", + "prost", + "prost-reflect", + "prost-types", + "protox-parse", + "thiserror 2.0.21", +] + +[[package]] +name = "protox-parse" +version = "0.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "34efee6fad0cf6d71642d20a66bfe0c281919c9577e7c66d20360dfcb7549272" +dependencies = [ + "logos", + "miette", + "prost-types", + "thiserror 2.0.21", +] + +[[package]] +name = "pulldown-cmark" +version = "0.13.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3db2be46835f013dd3f4fa7f5f7402c537787b54ebb6fe2b94bbdd7e2669bb7b" +dependencies = [ + "bitflags 2.13.2", + "memchr", + "unicase", +] + +[[package]] +name = "pulldown-cmark-to-cmark" +version = "22.0.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "84bbb29c624230c4bd1047bbdb2aa47e41c860e9665ce62ba9504eebe91bf867" +dependencies = [ + "pulldown-cmark", +] + [[package]] name = "pulp" version = "0.22.3" @@ -7885,10 +8743,11 @@ dependencies = [ [[package]] name = "rustls" -version = "0.23.45" +version = "0.23.46" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0d41d731c7d2f962d1ccc364cec258de3c0e93b38c2fb3ba97ac74513048d634" +checksum = "48e13bd8c0e9365c43cfa5c9e8f9ad49d3c8444926c9aac819e0e4dc503c8fdf" dependencies = [ + "log", "once_cell", "ring", "rustls-pki-types", @@ -7930,9 +8789,9 @@ dependencies = [ [[package]] name = "rustls-webpki" -version = "0.103.15" +version = "0.103.16" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f3c3cf1d8b1e7d4927e2d154c3fcb02979afb9939629c62cd9048d4f07b60ac2" +checksum = "4443b1b6d39d7c663139942cc4558262562d2c84123bb6282ef177009ca8b281" dependencies = [ "ring", "rustls-pki-types", @@ -8745,6 +9604,16 @@ dependencies = [ "float-cmp", ] +[[package]] +name = "string-interner" +version = "0.19.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "23de088478b31c349c9ba67816fa55d9355232d63c3afea8bf513e31f0f1d2c0" +dependencies = [ + "hashbrown 0.15.5", + "serde", +] + [[package]] name = "string_cache" version = "0.8.9" @@ -9254,9 +10123,9 @@ checksum = "fd3ca314f692efd6c868f8408f53fe444634a845f96c028b97d35f6a1f79f0ee" [[package]] name = "tokio" -version = "1.53.1" +version = "1.54.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "202caea871b69668250d242070849eb495be178ed697a3e98aebce5bc81a0bed" +checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3" dependencies = [ "bytes", "libc", @@ -9302,6 +10171,18 @@ dependencies = [ "tokio", ] +[[package]] +name = "tokio-stream" +version = "0.1.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3d06f0b082ba57c26b79407372e57cf2a1e28124f78e9479fe80322cf53420b" +dependencies = [ + "futures-core", + "pin-project-lite", + "tokio", + "tokio-util", +] + [[package]] name = "tokio-util" version = "0.7.19" @@ -9410,6 +10291,74 @@ version = "1.1.2+spec-1.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7d56353a2a665ad0f41a421187180aab746c8c325620617ad883a99a1cbe66d2" +[[package]] +name = "tonic" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "16ea7e687c08008d22518056608ad0c1cc721204d30ed48c8439a88ae5b9fc73" +dependencies = [ + "async-trait", + "base64 0.22.1", + "bytes", + "h2", + "http", + "http-body", + "http-body-util", + "hyper", + "hyper-timeout", + "hyper-util", + "percent-encoding", + "pin-project", + "socket2", + "sync_wrapper", + "tokio", + "tokio-stream", + "tokio-util", + "tower", + "tower-layer", + "tower-service", + "tracing", +] + +[[package]] +name = "tonic-build" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "040c96f2ebcbe22118dfa258310dad07002ccf92139370cf69532e4047c7d400" +dependencies = [ + "prettyplease", + "proc-macro2", + "quote", + "syn 2.0.119", +] + +[[package]] +name = "tonic-prost" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8fe9efdab8bb668f5ef526e21c247c8cb658853a754983cb70f1dac3fc4f462a" +dependencies = [ + "bytes", + "prost", + "tonic", +] + +[[package]] +name = "tonic-prost-build" +version = "0.14.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8b0e0e10c4980146d75d5acfe4f7a9822278130ca2025c0dc25c82f7307d63ac" +dependencies = [ + "prettyplease", + "proc-macro2", + "prost-build", + "prost-types", + "quote", + "syn 2.0.119", + "tempfile", + "tonic-build", +] + [[package]] name = "tower" version = "0.5.3" @@ -9418,11 +10367,15 @@ checksum = "ebe5ef63511595f1344e2d5cfa636d973292adc0eec1f0ad45fae9f0851ab1d4" dependencies = [ "futures-core", "futures-util", + "indexmap 2.14.2", "pin-project-lite", + "slab", "sync_wrapper", "tokio", + "tokio-util", "tower-layer", "tower-service", + "tracing", ] [[package]] @@ -9748,6 +10701,12 @@ version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ce61d488bcdc9bc8b5d1772c404828b17fc481c0a582b5581e95fb233aef503e" +[[package]] +name = "unicode-general-category" +version = "1.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24adfe8311434967077a6adff125729161e6e4934d76f6b7c55318ac5c9246d3" + [[package]] name = "unicode-id" version = "0.3.7" @@ -9790,6 +10749,12 @@ version = "0.1.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "b1d386ff53b415b7fe27b50bb44679e2cc4660272694b7b6f3326d8480823a94" +[[package]] +name = "unicode-width" +version = "0.1.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" + [[package]] name = "unicode-width" version = "0.2.2" @@ -10107,6 +11072,16 @@ dependencies = [ "unicode-ident", ] +[[package]] +name = "wasm-encoder" +version = "0.262.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4495a6f85dd94c411c749303efcbc5defade0b0cbdbc0541495241ab917a9b5a" +dependencies = [ + "leb128fmt", + "wasmparser 0.262.0", +] + [[package]] name = "wasm-streams" version = "0.4.2" @@ -10132,6 +11107,89 @@ dependencies = [ "web-sys", ] +[[package]] +name = "wasmi" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "78693fcdd618e0fc34af59c6b8efa9ac5d58c68df940beff4bedddb6acfe7c27" +dependencies = [ + "spin 0.9.9", + "wasmi_collections", + "wasmi_core", + "wasmi_ir", + "wasmparser 0.228.0", +] + +[[package]] +name = "wasmi_collections" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8a8be2aa467cf2d29e96ff759472c36eeb44a3c81c67fc9cb76c9a24c519c557" +dependencies = [ + "string-interner", +] + +[[package]] +name = "wasmi_core" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "69372d5fda3ea3d1e0aa6603c7888110e0187e88ea17cd8fc2e2df0a0e1f37fa" +dependencies = [ + "libm", +] + +[[package]] +name = "wasmi_ir" +version = "2.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f17b774caa13c618c7244f1ee51fe23c5e7b8538a471fa46d9949779758aed6" +dependencies = [ + "wasmi_core", +] + +[[package]] +name = "wasmparser" +version = "0.228.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4abf1132c1fdf747d56bbc1bb52152400c70f336870f968b85e89ea422198ae3" +dependencies = [ + "bitflags 2.13.2", + "indexmap 2.14.2", +] + +[[package]] +name = "wasmparser" +version = "0.262.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "974d643b125b272607e2fff8012ea14a8328acd7621e4bafa7afb32500052b1b" +dependencies = [ + "bitflags 2.13.2", + "indexmap 2.14.2", + "semver", +] + +[[package]] +name = "wast" +version = "262.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "30612a0c29249b5f185888a107e8a519d920226b7d07f9d351fd094e9f75eb23" +dependencies = [ + "bumpalo", + "leb128fmt", + "memchr", + "unicode-width 0.2.2", + "wasm-encoder", +] + +[[package]] +name = "wat" +version = "1.262.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e228027f2b07bbaf946980ebbe2437fca73be6f54c40c2a8d4f42f277075f8d1" +dependencies = [ + "wast", +] + [[package]] name = "wayland-backend" version = "0.3.17" @@ -10257,14 +11315,14 @@ version = "0.26.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "521bc38abb08001b01866da9f51eb7c5d647a19260e00054a8c7fd5f9e57f7a9" dependencies = [ - "webpki-roots 1.0.9", + "webpki-roots 1.0.11", ] [[package]] name = "webpki-roots" -version = "1.0.9" +version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7dcd9d09a39985f5344844e66b0c530a33843579125f23e21e9f0f220850f22a" +checksum = "f5f280bbb2b287bc8e7de7e3852945a0ff50d1bbcb5ed42b6c7a568c546df171" dependencies = [ "rustls-pki-types", ] @@ -11299,12 +12357,19 @@ dependencies = [ "nocterm-ai", "nocterm-design", "nocterm-explorer", + "nocterm-hubs-model", "nocterm-monitor", + "nocterm-plugin-abi", + "nocterm-plugin-api", + "nocterm-plugin-enablement", + "nocterm-project-providers", "nocterm-session", "nocterm-settings", "nocterm-terminal-model", "nocterm-ui", "nocterm-vault", + "nocterm-window-layout", + "nocterm-workspace", "proc-macro2", "schemars 1.2.2", "serde", diff --git a/Cargo.toml b/Cargo.toml index ffbb7a30..fa2355f8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -14,8 +14,8 @@ nocterm-ai.workspace = true nocterm-agent-providers.workspace = true nocterm-image-codec.workspace = true nocterm-agent-storage.workspace = true -nocterm-agent-usage.workspace = true nocterm-agent-runtime.workspace = true +nocterm-agent-tools.workspace = true nocterm-acp.workspace = true nocterm-agent.workspace = true nocterm-agent-settings-ui.workspace = true @@ -23,7 +23,9 @@ nocterm-device-unlock.workspace = true nocterm-connections.workspace = true nocterm-connections-model.workspace = true nocterm-connections-io.workspace = true +nocterm-connections-store.workspace = true nocterm-containers-ui.workspace = true +nocterm-containers.workspace = true nocterm-snippets-ui.workspace = true nocterm-snippets.workspace = true nocterm-snippets-store.workspace = true @@ -33,14 +35,32 @@ nocterm-design.workspace = true nocterm-files.workspace = true nocterm-keymap.workspace = true nocterm-keymap-ui.workspace = true +nocterm-layout-ui.workspace = true nocterm-local.workspace = true nocterm-localfs.workspace = true nocterm-monitor-ui.workspace = true +nocterm-project-model.workspace = true +nocterm-project-providers.workspace = true +nocterm-project-runtime.workspace = true +nocterm-project-environment.workspace = true +nocterm-project-container-fs.workspace = true +nocterm-directory-io.workspace = true +nocterm-explorer.workspace = true +nocterm-hub-io.workspace = true +nocterm-hub-agents.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-hubs-model.workspace = true +nocterm-hubs.workspace = true +nocterm-hubs-ui.workspace = true +nocterm-project-service.workspace = true +nocterm-project-storage.workspace = true +nocterm-projects.workspace = true nocterm-session.workspace = true nocterm-settings.workspace = true nocterm-store.workspace = true nocterm-settings-ui.workspace = true nocterm-ssh.workspace = true +nocterm-ssh-keys.workspace = true nocterm-terminal.workspace = true nocterm-transfers.workspace = true nocterm-transfer-runtime.workspace = true @@ -50,16 +70,46 @@ nocterm-theme-service.workspace = true nocterm-vault-ui.workspace = true nocterm-vault.workspace = true nocterm-vault-store.workspace = true +nocterm-plugin-enablement.workspace = true +nocterm-plugin-model.workspace = true +nocterm-plugin-api = { workspace = true, optional = true } +nocterm-plugin-host = { workspace = true, optional = true } +nocterm-plugin-packages = { workspace = true, optional = true } +nocterm-plugin-net = { workspace = true, optional = true } +nocterm-plugin-ports = { workspace = true, optional = true } +nocterm-plugin-view = { workspace = true, optional = true } +nocterm-plugin-wasm = { workspace = true, optional = true } +nocterm-plugins-ui.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true +nocterm-workspace-ports.workspace = true anyhow.workspace = true futures.workspace = true gpui-kit.workspace = true +thiserror.workspace = true toml.workspace = true tracing.workspace = true tracing-subscriber.workspace = true +[features] +# The user-plugin track: compiled out of default and release builds until it ships +# (docs/architecture/guardrails.md, "Isolation of plugin crates"). +user-plugins = [ + "dep:nocterm-plugin-api", + "dep:nocterm-plugin-host", + "dep:nocterm-plugin-packages", + "dep:nocterm-plugin-net", + "dep:nocterm-plugin-ports", + "dep:nocterm-plugin-view", + "dep:nocterm-plugin-wasm", + "nocterm-plugins-ui/user", + "nocterm-workspace/user-plugins", + "nocterm-monitor-ui/user-plugins", + "nocterm-files/user-plugins", + "nocterm-ui/user-plugins", +] + [lints] workspace = true @@ -124,22 +174,36 @@ openssh-clients = "*" winresource = "0.1" [dev-dependencies] +nocterm-plugin-ports = { workspace = true, features = ["test-support"] } +nocterm-connections-store = { workspace = true, features = ["test-support"] } +async-io.workspace = true +nocterm-monitor.workspace = true +nocterm-window-layout.workspace = true +nocterm-project-providers = { workspace = true, features = ["test-support"] } uuid.workspace = true nocterm-store = { workspace = true, features = ["test-support"] } nocterm-snippets = { workspace = true, features = ["test-support"] } nocterm-connections-model = { workspace = true, features = ["test-support"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } nocterm-ai = { workspace = true, features = ["test-support"] } nocterm-device-unlock = { workspace = true, features = ["test-support"] } nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } +nocterm-vault = { workspace = true, features = ["test-support"] } +nocterm-workspace = { workspace = true, features = ["test-support"] } +nocterm-project-model = { workspace = true, features = ["test-support"] } nocterm-transfers = { workspace = true, features = ["test-support"] } nocterm-themes = { workspace = true, features = ["test-support"] } serde_json.workspace = true tempfile.workspace = true +tokio.workspace = true +# Writes the guest modules that the plugin check tests run (user-plugins builds only). +wat.workspace = true gpui-kit = { workspace = true, features = ["test-support"] } [workspace] members = ["crates/*", "xtask"] -exclude = ["vendor/alacritty_terminal", "vendor/gpui-component", "vendor/gpui-base", "vendor/gpui-pre", "vendor/gpui-pre-windows", "vendor/gpui-pre-wgpu", "vendor/tid-rs"] +exclude = ["vendor/alacritty_terminal", "vendor/gpui-component", "vendor/gpui-base", "vendor/gpui-pre", "vendor/gpui-pre-windows", "vendor/gpui-pre-wgpu", "vendor/tid-rs", "examples/plugins/focus-timer"] resolver = "3" [patch.crates-io] @@ -159,14 +223,23 @@ license-file = "LICENSE" nocterm-terminal-model = { path = "crates/terminal-model" } nocterm-explorer = { path = "crates/explorer" } nocterm-ai = { path = "crates/ai" } +nocterm-project-model = { path = "crates/project-model" } +nocterm-project-providers = { path = "crates/project-providers" } +nocterm-project-runtime = { path = "crates/project-runtime" } +nocterm-project-environment = { path = "crates/project-environment" } +nocterm-project-container-fs = { path = "crates/project-container-fs" } +nocterm-directory-io = { path = "crates/directory-io" } +nocterm-project-storage = { path = "crates/project-storage" } +nocterm-project-service = { path = "crates/project-service" } +nocterm-projects = { path = "crates/projects" } nocterm-agent-providers = { path = "crates/agent-providers" } nocterm-agent-directory = { path = "crates/agent-directory" } nocterm-agent-settings-ui = { path = "crates/agent-settings-ui" } nocterm-workspace-ports = { path = "crates/workspace-ports" } nocterm-agent-chat = { path = "crates/agent-chat" } +nocterm-agent-tools = { path = "crates/agent-tools" } nocterm-image-codec = { path = "crates/image-codec" } nocterm-agent-storage = { path = "crates/agent-storage" } -nocterm-agent-usage = { path = "crates/agent-usage" } nocterm-acp = { path = "crates/acp" } nocterm-agent = { path = "crates/agent" } nocterm-agent-runtime = { path = "crates/agent-runtime" } @@ -178,6 +251,7 @@ nocterm-connections-store = { path = "crates/connections-store" } nocterm-connections-model = { path = "crates/connections-model" } nocterm-connections-io = { path = "crates/connections-io" } nocterm-containers = { path = "crates/containers" } +nocterm-containers-service = { path = "crates/containers-service" } nocterm-containers-ui = { path = "crates/containers-ui" } nocterm-snippets = { path = "crates/snippets" } nocterm-snippets-ui = { path = "crates/snippets-ui" } @@ -186,17 +260,37 @@ nocterm-snippets-io = { path = "crates/snippets-io" } nocterm-core = { path = "crates/core" } nocterm-design = { path = "crates/design" } nocterm-files = { path = "crates/files" } +nocterm-hubs-model = { path = "crates/hubs-model" } +nocterm-hub-agents-model = { path = "crates/hub-agents-model" } +nocterm-hub-agents = { path = "crates/hub-agents" } +nocterm-hub-io = { path = "crates/hub-io" } +nocterm-hubs = { path = "crates/hubs" } +nocterm-hubs-ui = { path = "crates/hubs-ui" } nocterm-keymap = { path = "crates/keymap" } nocterm-keymap-ui = { path = "crates/keymap-ui" } +nocterm-layout-ui = { path = "crates/layout-ui" } nocterm-local = { path = "crates/local" } nocterm-localfs = { path = "crates/localfs" } nocterm-monitor = { path = "crates/monitor" } +nocterm-monitor-service = { path = "crates/monitor-service" } nocterm-monitor-ui = { path = "crates/monitor-ui" } +nocterm-plugin-abi = { path = "crates/plugin-abi" } +nocterm-plugin-wasm = { path = "crates/plugin-wasm" } +nocterm-plugin-api = { path = "crates/plugin-api" } +nocterm-plugin-enablement = { path = "crates/plugin-enablement" } +nocterm-plugin-host = { path = "crates/plugin-host" } +nocterm-plugin-packages = { path = "crates/plugin-packages" } +nocterm-plugin-net = { path = "crates/plugin-net" } +nocterm-plugin-ports = { path = "crates/plugin-ports" } +nocterm-plugin-view = { path = "crates/plugin-view" } +nocterm-plugin-model = { path = "crates/plugin-model" } +nocterm-plugins-ui = { path = "crates/plugins-ui" } nocterm-session = { path = "crates/session" } nocterm-settings = { path = "crates/settings" } nocterm-store = { path = "crates/store" } nocterm-settings-ui = { path = "crates/settings-ui" } nocterm-ssh = { path = "crates/ssh" } +nocterm-ssh-keys = { path = "crates/ssh-keys" } nocterm-terminal = { path = "crates/terminal" } nocterm-transfers = { path = "crates/transfers" } nocterm-transfer-runtime = { path = "crates/transfer-runtime" } @@ -205,14 +299,21 @@ nocterm-themes-registry = { path = "crates/themes-registry" } nocterm-theme-service = { path = "crates/theme-service" } nocterm-ui = { path = "crates/ui" } nocterm-vt = { path = "crates/vt" } +nocterm-window-layout = { path = "crates/window-layout" } nocterm-workspace = { path = "crates/workspace" } nocterm-vault = { path = "crates/vault" } nocterm-vault-store = { path = "crates/vault-store" } nocterm-vault-ui = { path = "crates/vault-ui" } +# The hub contract lives outside this workspace (ADR 0096); only nocterm-hub-io may use it. +nocterm-hub-client = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } +nocterm-hub-model = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } +nocterm-hub-proto = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } +nocterm-keys = { git = "https://github.com/cu-e/nocterm-contract", rev = "58af32eb20106eaac3c34104b227aae04672ad2f" } agent-client-protocol = "=2.2.0" agent-client-protocol-schema = { version = "=1.9.1", features = ["unstable_session_fork", "unstable_end_turn_token_usage"] } base64 = "0.22" +hex = "0.4.3" async-io = "2" async-process = "2" @@ -231,17 +332,21 @@ anyhow = "1" async-channel = "2" directories = "6" futures = "0.3" +sha2 = "0.10.9" gpui-kit = "0.7" # GPUI alone, for crates that must not reach the component library. gpui = { package = "gpui-pre", version = "0.3.7", default-features = false } parking_lot = "0.12" portable-pty = "0.9" +prost = "0.14.4" regex = "1" # Zed's reqwest fork, already in the tree through GPUI. reqwest = { package = "gpui-pre-reqwest", version = "0.12", default-features = false, features = ["blocking", "rustls-tls"] } regex-syntax = "0.8" russh = { version = "0.63", default-features = false, features = ["ring", "flate2", "rsa"] } russh-sftp = "3" +# Pinned to the version `russh` resolves, so key text round-trips between the two (ADR 0102). +ssh-key = { version = "=0.7.0-rc.11", default-features = false, features = ["ed25519", "p256", "p384", "p521", "rsa", "encryption"] } schemars = "1" serde = { version = "1", features = ["derive"] } serde_json = "1" @@ -255,6 +360,9 @@ tokio = { version = "1", features = ["rt-multi-thread", "net", "time", "sync", " toml = "1" toml_edit = "0.25" tracing = "0.1" +tonic = { version = "0.14", default-features = false } +# Writes the guest modules the wasm tests run. Dev only: the host reads binary modules. +wat = "1.262" tracing-subscriber = { version = "0.3", features = ["env-filter"] } uuid = { version = "1", features = ["v4", "serde"] } @@ -294,6 +402,18 @@ rustybuzz = { opt-level = 3 } taffy = { opt-level = 3 } ttf-parser = { opt-level = 3 } vte = { opt-level = 3 } +# The code runtime of user plugins interprets guest code, which is unusably slow unoptimised. +wasmi = { opt-level = 3 } +wasmi_collections = { opt-level = 3 } +wasmi_core = { opt-level = 3 } +wasmi_ir = { opt-level = 3 } +wasmparser = { opt-level = 3 } +postcard = { opt-level = 3 } +# `cargo xtask architecture` parses every source file with syn several times; unoptimised, +# that made it the slowest check. Optimised syn also expands derive macros faster. +proc-macro2 = { opt-level = 3 } +syn = { opt-level = 3 } +xtask = { opt-level = 1 } [profile.release] codegen-units = 1 diff --git a/README.md b/README.md index 76b0bc0d..3156012d 100644 --- a/README.md +++ b/README.md @@ -194,7 +194,7 @@ and layout overrides always apply. `known_hosts` stores accepted keys. In the state directory, `servers.toml` keeps detected systems and countries, `flags/` the downloaded flags, `agents.toml` model favorites and the last agent used, `agent-chats/` saved AI chats and -`layout.json` the widths of resizable columns and which side the sidebar is on. +`layout.json` the widths of resizable columns. `keymap.toml` in the configuration directory holds your key binding changes. `vault.bin` stores encrypted credentials and uses a sibling `.lock` file for concurrent-writer protection; profiles contain only opaque credential IDs. Existing OpenSSH known hosts are also read. @@ -207,10 +207,114 @@ The window has the sidebar (Servers, Explorer) on one side of the tabs and the AI panel on the other; both resize by dragging their edge and keep their width. `Ctrl+Shift+C` shows Servers and `Ctrl+Shift+E` the Explorer; pressing it again, or clicking the shown panel's button in the footer, hides the sidebar. -`Ctrl+Alt+B` shows or hides the AI panel and `Ctrl+E` swaps the two sides (`Cmd` -instead of `Ctrl` on macOS). In a focused terminal `Ctrl+Shift+C` copies, and the -terminal no longer receives `Ctrl+E` and `Ctrl+J`; rebind them in Settings → -Keymap if the shell needs them. +`Ctrl+Alt+B` shows or hides the AI panel (`Cmd` instead of `Ctrl` on macOS). +**Window → Layouts** chooses the arrangement of the footer and the panels, for every +open window: Default, Swapped sides (the sidebar and the AI panel on each other's +side) or a layout saved in the `[layouts]` section of `settings.toml`. **Window → +Panels** shows or hides a panel. The command palette also has Next Layout, Previous +Layout and Pick Layout. In a +focused terminal `Ctrl+Shift+C` copies, and the terminal no longer receives +`Ctrl+J`; rebind it in Settings → Keymap if the shell needs it. + +A saved layout is an entry of `layouts.presets` in `settings.toml`; `layouts.active` +holds the id of the selected one (`default`, `swapped` or a preset id): + +```toml +[layouts] +active = "p1" + +[[layouts.presets]] +id = "p1" +name = "Reading" +open = ["files.explorer"] # panels shown when the layout is selected +tray = ["snippets.library"] # registered, but kept off the footer and the menu +[layouts.presets.bar] +left = [ + { group = "navigation", members = ["connections.servers", { separator = true }, "files.explorer"] }, + { item = "monitor.summary" }, +] +center = [{ item = "workspace.notifications" }] +right = [{ group = "assistant", side = "right", members = ["agent.chats"] }] +``` + +The footer has a `left`, a `center` and a `right` zone, each a list of entries drawn in +order. `center` is optional: its entries stay at the middle of the footer and give way first +when the window is narrow (the notification line is cut before any toggle), while with an +empty `center` the footer is one row from the left zone to the right one. +When even then the entries do not fit, the least important move into a **»** menu at the end of +the footer: widgets first, then the terminal toggle, then panel toggles. The notification line +and a toggle that asks for you stay in place; the menu lists what is hidden and does what the +entries would. Popups of widgets open toward the inside of the window. An entry is a +table with exactly one of these keys: + +- `item = ""` places a status bar widget, such as `monitor.summary`; +- `separator = true` draws a divider; dividers never lead, trail or double up; +- `panel = ""` places a panel on its own: it opens in a column of its own and closes + no other panel. Its optional `side` works as a group's; +- `group = ""` places a panel group (`navigation`, `assistant` or one you made, such + as `group-1`) whose `members` are panel ids, or `{ separator = true }` dividers between their + toggles. A group shows one of its panels at a time. Its optional `name` titles a group you + made, and its optional `side` (`"left"` or `"right"`) chooses the side of the content the + group's panels open on; without it the group follows its zone. + +The `navigation` group is always the sidebar; a `primary` key on a group is ignored (and +logged). A contribution the layout does not mention is placed by its own hint when the hint +says to show it, and waits in the tray otherwise; the notifications are pinned and always +shown. + +Panel and widget ids are listed in [Plugins and their contributions](docs/reference/plugins.md). +A panel belongs to the group the layout places it in, wherever its own hint says, or stands on +its own: the sidebar toggle and `Ctrl+Alt+B` act on the groups as the footer draws them. The +open panels and groups are laid out in the order the footer shows them, so the first one of the +footer is the leftmost column on its side. Editing the +selected layout in the file rearranges every open window at once and keeps the panels +that are still placed; switching layouts shows exactly the panels the new layout opens. +If the `[layouts]` table does not read (for example a wrong key type), the window shows +the Default layout and refuses to switch layouts, so that your table is never +overwritten; fix the file and switching works again. + +You need not write the table by hand. **Settings → Appearance → Status bar and panels** +shows the footer on one line, as it will look, and you edit it there. Drag an item to reorder +it, to another zone, onto a gap to make a panel stand on its own, onto a panel to put the two in +one group, or onto **Not in the bar** to take it off the footer. Right-click an item, a group or +a separator for its menu: move it, take a panel out of its group or start a new group with it, +rename or ungroup a group, add separators, choose the side a centre group opens on or whether a +panel opens with the layout, open the plugin's settings, or hide it. Click an item to select it +and move it with `Alt+←`/`→`, `Alt+↑`/`↓` (to another zone), `G` (to the next group), `|` (add a +separator) and `Delete` (take it off the footer into the tray); `Ctrl+Z` and `Ctrl+Shift+Z` +(`Cmd` on macOS) undo and redo. Editing the Default layout in use saves a copy named Custom and +selects it, so the built-in layouts never change; a built-in layout that is not in use offers +**Duplicate to edit**. Saved layouts can be renamed, duplicated, deleted and reset there. +Right-click the footer for **Hide**, **Customize Status Bar…** and **Layouts**. +Settings → Plugins has a "Customize in status bar…" button for each plugin. A layout that +places an item of a plugin that is not installed keeps it there until the first edit, which +moves it to the tray as "Not installed". The editor refuses to save while the `[layouts]` table does not read, and says so; fix the file first. + +Settings → Plugins lists the built-in plugins. A switch turns a plugin off for every window: +its panels and widgets disappear from the window and the layout keeps their place for when it +returns. The AI Agents plugin is required and stays on. Select a plugin to change its +settings, or open its own page with the "settings…" button (AI Agents, Monitor and +Workspaces have one). The status bar metrics of Monitor are under Plugins → Monitor; the +Monitor page keeps the refresh intervals and the details. The switches are the `disabled` +key of the `[plugins]` section of `settings.toml` (see the +[settings reference](docs/reference/settings.md)), and **Window → Panels** omits the panels +of a disabled plugin. If that table does not read, every plugin stays on and the page +refuses to switch one, so that your table is never overwritten; fix the file and the +switches work again. + +The Clock plugin shows the local time as `HH:MM` in the status bar. Default does not place +it; add it from Settings → Appearance → Status bar and panels (it waits in the tray) or put +`{ item = "clock.time" }` in a zone of a saved layout. Switching the plugin off removes it +from every window. Its settings, under Settings → Plugins → Clock, choose a 24-hour or 12-hour clock (`1:05 PM`), seconds (the clock then +updates every second) and a short date before the time (`Oct 24`); they are the `[clock]` +section of `settings.toml` and apply at once in every window. + +User plugins (preview) are installed from a file and run inside the window under the capabilities +you allow. They are in builds made with `cargo build --features user-plugins`; release builds do not +have them yet. Such a build loads none until you turn on Settings → Plugins → User plugins → +Allow user plugins (the `user_plugins` key of the `[plugins]` section) and restart, and +`nocterm --safe-mode` starts without any of them, whatever the setting says, to recover from a +plugin that misbehaves. The [guide](docs/plugins/README.md) is for authors. Settings → Keymap lists every command with its shortcuts and description. Search matches words in any of them and shortcuts in any modifier order; Find by keys @@ -226,6 +330,8 @@ keymap and Cargo manifests: - [Settings](docs/reference/settings.md) - [Design tokens](docs/reference/design-tokens.md) - [Actions and shortcuts](docs/reference/actions.md) +- [Plugins and their contributions](docs/reference/plugins.md) +- [User plugins (preview)](docs/plugins/README.md): the [guide](docs/plugins/guide.md), the [reference](docs/plugins/reference.md) and the [manifest schema](docs/plugins/manifest.schema.json) - [Architecture](docs/ARCHITECTURE.md) ## Development @@ -236,12 +342,18 @@ cargo build --workspace --all-targets cargo clippy --workspace --all-targets -- -D warnings cargo test --workspace NOCTERM_REQUIRE_SSHD=1 cargo test -p nocterm-ssh --test sshd +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm-hub-io --test hub +NOCTERM_REQUIRE_HUB=1 cargo test -p nocterm hub_live cargo xtask docs cargo xtask docs --check cargo xtask architecture cargo doc --workspace --no-deps ``` +The hub tests (the adapter and `hub_live`, which drives the windows) need a running hub (`docker compose up` in the server's repository) and +`NOCTERM_HUB_ADDRESS`, `NOCTERM_HUB_FINGERPRINT`, `NOCTERM_HUB_USERNAME` and `NOCTERM_HUB_PASSWORD`. +Without `NOCTERM_REQUIRE_HUB=1` it passes when they are missing; with it, missing variables or a +missing hub fail the run. The SSH tests start private local OpenSSH servers with temporary keys. With `NOCTERM_REQUIRE_SSHD=1`, missing `sshd` fails instead of skipping the tests. Some OpenSSH installations need `/run/sshd` created by the system administrator. @@ -267,6 +379,14 @@ for tests; the native shutdown diagnostic still needs an upstream reproduction. The terminal emulator is a maintained local patch of alacritty_terminal 0.26.0. See [the patch notes](vendor/alacritty_terminal/NOCTERM.md) before updating it. +## Hubs + +Sign in to a self-hosted Nocterm server with **Hubs: Sign in to server** in the command palette +(or from the Servers settings page), verify the certificate fingerprint and sign in. The hub's +terminal sessions are listed in the Sessions panel of any window, and keep running when you +close their tabs. +See [hubs](docs/HUBS.md). + ## AI agents The right AI panel supports ACP agents Claude, Codex, Hermes and custom @@ -299,3 +419,85 @@ their folders. Local agents are not sandboxed and may read your user files. Nocterm excludes SSH/vault credential fields from context; terminal-output filtering cannot recognize every secret. See [AI setup, tools and privacy](docs/AI_AGENTS.md). + +The Workspaces button beside Containers opens saved folders for the current +connection and other servers. Each workspace has its own terminal tabs and chat +history, using ordinary local or SSH sessions and the server’s normal permissions. + +With no server selected, the sidebar shows **This computer**. You can create a +workspace there without opening a terminal first. A selected disconnected server +must be reconnected before creating a workspace on it. + +Workspace creation offers three modes on this computer or the selected server: + +- **Direct connection** selects a required project directory on the connected + computer or server. Available file access provides folder suggestions and + Home, Up and Refresh controls. +- **Existing container** lists Docker and Podman containers available to the + current user on that host, including stopped containers. Select the + container and optionally choose a project directory. +- **New container** selects Docker or Podman and an image. Compatible Linux + images can install Python, Rust, Bun, Node.js and Java together; Python + is selected initially. Each language offers its actual default or a custom numeric + version: Python minor/exact, Rust exact, Bun exact, Node.js major/exact, Java major. + Python, Node.js and Java default to distribution packages; Rust uses Stable and + Bun uses Latest stable. Requested versions install inside the new container, + according to its detected distribution, architecture and libc. Unsupported + combinations fail explicitly; for example, Alpine uses distribution Node.js + rather than the glibc archives used for pinned Node.js versions. + +The creation window follows **Connection → Project → Tools**. Connection chooses +the mode and resource; Project contains the name, directory and optional Git +repository. Tools appears only for a new container. Back keeps your entries; +preparation starts only after **Create workspace** on the final step. +The server stays in the shared header while you move between steps. Existing +containers can be searched by name, ID, image or engine. Custom image results use +separate local and registry sources with a full-width list. + +**Settings → Workspaces** controls the default container project directory, +initially `/home`. Leaving the directory empty uses the default captured when +that creation window opened. An explicit directory takes precedence. Changing +this setting affects future creation windows and preserves saved workspaces. + +Workspace settings show the actual environment and installed versions when its +connection is available. **Install / update** changes only selected languages; +unchecked languages remain installed. Saving the workspace name or directory +does not install tools. Managed versions use the current user's workspace tool +directory and become available in new workspace terminals. Existing terminals +keep their current environment. Native package installation needs existing root +permissions; Nocterm does not invoke sudo or edit your shell startup files. +Cancellation can leave completed package or language changes in place. +An existing Arch environment needs an initialized, current package database; +Nocterm asks an administrator to complete `pacman -Syu` when it is missing. +Initial package synchronization is performed inside a newly created Arch container. + +Creation shows its current phase beside the action buttons. Download byte counts +are shown when the engine reports them; progress remains indeterminate when a +reliable total is unavailable. + +Custom image entry immediately filters local repositories and tags. After a brief +pause it also searches Docker Hub with Docker, or configured registries with Podman, +using that host's existing engine configuration. Search lists repositories, not tags; +enter a full reference such as `debian:13` for a particular tag. Search never pulls +an image. Local images and manual references remain available when registry search +fails or is rate limited. + +A Git repository is optional. Cloning uses an empty destination and does not +overwrite existing project files. An empty container directory uses the configured +default described above. +Closing a workspace or removing its saved entry leaves containers and project +files intact. Container workspace paths are entered manually. Once connected, +Explorer browses and transfers files inside the selected container, using its +configured user and normal permissions. This needs Linux `/proc` and compatible +standard shell/file utilities, available in ordinary Ubuntu, Debian and Alpine +images; minimal images without them report unavailable file access. No SSH server +or helper is installed in the container. Folder rename and cross-filesystem rename +are unsupported by this adapter; ordinary file rename never overwrites an existing +entry. Server files remain available through the server's own connection. + +The footer monitor follows the selected workspace's active environment. Folder +workspaces use their own server connection; container workspaces use their own +Linux cgroup CPU and memory accounting. Container CPU is shown relative to one +CPU, so it can exceed 100%. Memory without an observable limit shows bytes in use +without a percentage. Host-only metrics stay unavailable in containers. An +unavailable workspace never redirects monitoring to this computer. diff --git a/THIRD_PARTY_NOTICES.txt b/THIRD_PARTY_NOTICES.txt index 89d1d679..a54219e1 100644 --- a/THIRD_PARTY_NOTICES.txt +++ b/THIRD_PARTY_NOTICES.txt @@ -2,9 +2,9 @@ Nocterm third-party licenses and notices ======================================== Generated by scripts/generate-license-notices.py using cargo-about 0.9.2. -Content SHA-256: fdca99e4df7d5a5b1ac09e5b90ae3c1e8d04cc9634142d17ac4e6935a42929fc -Input fingerprint: 27d13e32e4946b845b77cfa218c039d3af551e8414a7606a8eb0aa105569d6f0 -Cargo.lock SHA-256 (release version masked): 7d24ea7b4261c0f6efc538340c4c7710111b6955271d60cca7d49ca6c4611dc1 +Content SHA-256: 45e68425cf4ca9b8d41551f8e7d162988a1a253f55e87b2516fcccce6a82261b +Input fingerprint: fb3a2d3589123c4c9993827d42e570e4c5958135008a83d3a54e8fde12c751f8 +Cargo.lock SHA-256 (release version masked): 5e00a50eee53527cf1c92b0837449a62276ec529ef868a24c90bf9658b9f1d15 Scope: runtime and build dependencies of Nocterm and its optional vault broker for Linux x86_64, Windows x86_64, macOS x86_64 and macOS arm64. @@ -190,9 +190,9 @@ Authors: Wim Looman ; Allen Bui Source: https://crates.io/api/v1/crates/async-compression/0.4.50/download Repository: https://github.com/Nullus157/async-compression -async-executor 1.14.0: Apache-2.0 OR MIT +async-executor 1.14.1: Apache-2.0 OR MIT Authors: Stjepan Glavina ; John Nunley -Source: https://crates.io/api/v1/crates/async-executor/1.14.0/download +Source: https://crates.io/api/v1/crates/async-executor/1.14.1/download Repository: https://github.com/smol-rs/async-executor async-fs 2.2.0: Apache-2.0 OR MIT @@ -649,9 +649,9 @@ Authors: Eira Fransham Source: https://crates.io/api/v1/crates/crunchy/0.2.4/download Repository: https://github.com/eira-fransham/crunchy -crypto-bigint 0.7.5: Apache-2.0 OR MIT +crypto-bigint 0.7.11: Apache-2.0 OR MIT Authors: RustCrypto Developers -Source: https://crates.io/api/v1/crates/crypto-bigint/0.7.5/download +Source: https://crates.io/api/v1/crates/crypto-bigint/0.7.11/download Repository: https://github.com/RustCrypto/crypto-bigint crypto-common 0.1.7: MIT OR Apache-2.0 @@ -678,9 +678,9 @@ Authors: RustCrypto Developers Source: https://crates.io/api/v1/crates/ctr/0.10.1/download Repository: https://github.com/RustCrypto/block-modes -ctutils 0.4.2: Apache-2.0 OR MIT +ctutils 0.4.8: Apache-2.0 OR MIT Authors: RustCrypto Developers -Source: https://crates.io/api/v1/crates/ctutils/0.4.2/download +Source: https://crates.io/api/v1/crates/ctutils/0.4.8/download Repository: https://github.com/RustCrypto/utils cursor-icon 1.2.0: MIT OR Apache-2.0 OR Zlib @@ -1008,6 +1008,11 @@ Authors: Joshua Barretto Source: https://crates.io/api/v1/crates/flume/0.12.0/download Repository: https://github.com/zesterer/flume +fnv 1.0.7: Apache-2.0 OR MIT +Authors: Alex Crichton +Source: https://crates.io/api/v1/crates/fnv/1.0.7/download +Repository: https://github.com/servo/rust-fnv + foldhash 0.1.5: Zlib Authors: Orson Peters Source: https://crates.io/api/v1/crates/foldhash/0.1.5/download @@ -1334,6 +1339,11 @@ Authors: Sean Bowe ; Jack Grigg Source: https://crates.io/api/v1/crates/group/0.14.0/download Repository: https://github.com/zkcrypto/group +h2 0.4.19: MIT +Authors: Carl Lerche ; Sean McArthur +Source: https://crates.io/api/v1/crates/h2/0.4.19/download +Repository: https://github.com/hyperium/h2 + half 2.7.1: MIT OR Apache-2.0 Authors: Kathryn Long Source: https://crates.io/api/v1/crates/half/2.7.1/download @@ -1446,6 +1456,11 @@ Authors: Sean McArthur Source: https://crates.io/api/v1/crates/httparse/1.10.1/download Repository: https://github.com/seanmonstar/httparse +httpdate 1.0.3: MIT OR Apache-2.0 +Authors: Pyfisch +Source: https://crates.io/api/v1/crates/httpdate/1.0.3/download +Repository: https://github.com/pyfisch/httpdate + hybrid-array 0.4.15: MIT OR Apache-2.0 Authors: RustCrypto Developers Source: https://crates.io/api/v1/crates/hybrid-array/0.4.15/download @@ -1460,6 +1475,11 @@ hyper-rustls 0.27.10: Apache-2.0 OR ISC OR MIT Source: https://crates.io/api/v1/crates/hyper-rustls/0.27.10/download Repository: https://github.com/rustls/hyper-rustls +hyper-timeout 0.5.2: MIT OR Apache-2.0 +Authors: Herman J. Radtke III +Source: https://crates.io/api/v1/crates/hyper-timeout/0.5.2/download +Repository: https://github.com/hjr3/hyper-timeout + hyper-util 0.1.21: MIT Authors: Sean McArthur Source: https://crates.io/api/v1/crates/hyper-util/0.1.21/download @@ -1729,6 +1749,21 @@ Authors: The Rust Project Developers Source: https://crates.io/api/v1/crates/log/0.4.34/download Repository: https://github.com/rust-lang/log +logos 0.16.1: MIT OR Apache-2.0 +Authors: Maciej Hirsz ; Jérome Eertmans (maintainer) +Source: https://crates.io/api/v1/crates/logos/0.16.1/download +Repository: https://github.com/maciejhirsz/logos + +logos-codegen 0.16.1: MIT OR Apache-2.0 +Authors: Maciej Hirsz ; Jérome Eertmans (maintainer) +Source: https://crates.io/api/v1/crates/logos-codegen/0.16.1/download +Repository: https://github.com/maciejhirsz/logos + +logos-derive 0.16.1: MIT OR Apache-2.0 +Authors: Maciej Hirsz ; Jérome Eertmans (maintainer) +Source: https://crates.io/api/v1/crates/logos-derive/0.16.1/download +Repository: https://github.com/maciejhirsz/logos + lsp-types 0.97.0: MIT Authors: Markus Westerlind ; Bruno Medeiros Source: https://crates.io/api/v1/crates/lsp-types/0.97.0/download @@ -1828,6 +1863,16 @@ Authors: gfx-rs developers Source: https://crates.io/api/v1/crates/metal/0.33.0/download Repository: https://github.com/gfx-rs/metal-rs +miette 7.6.0: Apache-2.0 +Authors: Kat Marchán +Source: https://crates.io/api/v1/crates/miette/7.6.0/download +Repository: https://github.com/zkat/miette + +miette-derive 7.6.0: Apache-2.0 +Authors: Kat Marchán +Source: https://crates.io/api/v1/crates/miette-derive/7.6.0/download +Repository: https://github.com/zkat/miette + mime 0.3.17: MIT OR Apache-2.0 Authors: Sean McArthur Source: https://crates.io/api/v1/crates/mime/0.3.17/download @@ -1876,6 +1921,11 @@ Authors: Radzivon Bartoshyk Source: https://crates.io/api/v1/crates/moxcms/0.8.1/download Repository: https://github.com/awxkee/moxcms.git +multimap 0.10.1: MIT OR Apache-2.0 +Authors: Håvar Nøvik +Source: https://crates.io/api/v1/crates/multimap/0.10.1/download +Repository: https://github.com/havarnov/multimap + multiversion_no_op 1.0.0: Apache-2.0 OR MIT Authors: Henri Sivonen Source: https://crates.io/api/v1/crates/multiversion_no_op/1.0.0/download @@ -2196,6 +2246,11 @@ Authors: The rust-url developers Source: https://crates.io/api/v1/crates/percent-encoding/2.3.2/download Repository: https://github.com/servo/rust-url/ +petgraph 0.8.3: MIT OR Apache-2.0 +Authors: bluss; mitchmindtree +Source: https://crates.io/api/v1/crates/petgraph/0.8.3/download +Repository: https://github.com/petgraph/petgraph + phc 0.6.1: Apache-2.0 OR MIT Authors: RustCrypto Developers Source: https://crates.io/api/v1/crates/phc/0.6.1/download @@ -2397,6 +2452,51 @@ Authors: Philip Degarmo Source: https://crates.io/api/v1/crates/profiling-procmacros/1.0.18/download Repository: https://github.com/aclysma/profiling +prost 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +prost-build 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost-build/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +prost-derive 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost-derive/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +prost-reflect 0.16.5: MIT OR Apache-2.0 +Authors: Andrew Hickman +Source: https://crates.io/api/v1/crates/prost-reflect/0.16.5/download +Repository: https://github.com/andrewhickman/prost-reflect + +prost-types 0.14.4: Apache-2.0 +Authors: Dan Burkert ; Lucio Franco ; Casper Meijn ; Tokio Contributors +Source: https://crates.io/api/v1/crates/prost-types/0.14.4/download +Repository: https://github.com/tokio-rs/prost + +protox 0.10.0: MIT OR Apache-2.0 +Authors: Andrew Hickman +Source: https://crates.io/api/v1/crates/protox/0.10.0/download +Repository: https://github.com/andrewhickman/protox + +protox-parse 0.10.0: MIT OR Apache-2.0 +Authors: Andrew Hickman +Source: https://crates.io/api/v1/crates/protox-parse/0.10.0/download +Repository: https://github.com/andrewhickman/protox + +pulldown-cmark 0.13.6: MIT +Authors: Raph Levien ; Marcus Klaas de Vries +Source: https://crates.io/api/v1/crates/pulldown-cmark/0.13.6/download +Repository: https://github.com/raphlinus/pulldown-cmark + +pulldown-cmark-to-cmark 22.0.3: Apache-2.0 +Authors: Sebastian Thiel ; Dylan Owen ; Alessandro Ogier ; Zixian Cai <2891235+caizixian@users.noreply.github.com>; Andrew Lyjak +Source: https://crates.io/api/v1/crates/pulldown-cmark-to-cmark/22.0.3/download +Repository: https://github.com/Byron/pulldown-cmark-to-cmark + pulp 0.22.3: MIT Authors: sarah quiñones Source: https://crates.io/api/v1/crates/pulp/0.22.3/download @@ -2663,8 +2763,8 @@ Authors: Dan Gohman Source: https://crates.io/api/v1/crates/rustix-openpty/0.2.0/download Repository: https://github.com/sunfishcode/rustix-openpty -rustls 0.23.45: Apache-2.0 OR ISC OR MIT -Source: https://crates.io/api/v1/crates/rustls/0.23.45/download +rustls 0.23.46: Apache-2.0 OR ISC OR MIT +Source: https://crates.io/api/v1/crates/rustls/0.23.46/download Repository: https://github.com/rustls/rustls rustls-pemfile 2.2.0: Apache-2.0 OR ISC OR MIT @@ -2675,8 +2775,8 @@ rustls-pki-types 1.15.1: MIT OR Apache-2.0 Source: https://crates.io/api/v1/crates/rustls-pki-types/1.15.1/download Repository: https://github.com/rustls/pki-types -rustls-webpki 0.103.15: ISC -Source: https://crates.io/api/v1/crates/rustls-webpki/0.103.15/download +rustls-webpki 0.103.16: ISC +Source: https://crates.io/api/v1/crates/rustls-webpki/0.103.16/download Repository: https://github.com/rustls/webpki rustversion 1.0.23: MIT OR Apache-2.0 @@ -3209,9 +3309,9 @@ Authors: Lokathor Source: https://crates.io/api/v1/crates/tinyvec/1.13.3/download Repository: https://github.com/Lokathor/tinyvec -tokio 1.53.1: MIT +tokio 1.54.1: MIT Authors: Tokio Contributors -Source: https://crates.io/api/v1/crates/tokio/1.53.1/download +Source: https://crates.io/api/v1/crates/tokio/1.54.1/download Repository: https://github.com/tokio-rs/tokio tokio-macros 2.7.2: MIT @@ -3223,6 +3323,16 @@ tokio-rustls 0.26.6: MIT OR Apache-2.0 Source: https://crates.io/api/v1/crates/tokio-rustls/0.26.6/download Repository: https://github.com/rustls/tokio-rustls +tokio-socks 0.5.3: MIT +Authors: Yilin Chen +Source: https://crates.io/api/v1/crates/tokio-socks/0.5.3/download +Repository: https://github.com/sticnarf/tokio-socks + +tokio-stream 0.1.19: MIT +Authors: Tokio Contributors +Source: https://crates.io/api/v1/crates/tokio-stream/0.1.19/download +Repository: https://github.com/tokio-rs/tokio + tokio-util 0.7.19: MIT Authors: Tokio Contributors Source: https://crates.io/api/v1/crates/tokio-util/0.7.19/download @@ -3264,6 +3374,26 @@ toml_writer 1.1.2+spec-1.1.0: MIT OR Apache-2.0 Source: https://crates.io/api/v1/crates/toml_writer/1.1.2+spec-1.1.0/download Repository: https://github.com/toml-rs/toml +tonic 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + +tonic-build 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic-build/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + +tonic-prost 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic-prost/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + +tonic-prost-build 0.14.7: MIT +Authors: gRPC authors +Source: https://crates.io/api/v1/crates/tonic-prost-build/0.14.7/download +Repository: https://github.com/grpc/grpc-rust + tower 0.5.3: MIT Authors: Tower Maintainers Source: https://crates.io/api/v1/crates/tower/0.5.3/download @@ -3438,6 +3568,11 @@ Authors: Yevhenii Reizner Source: https://crates.io/api/v1/crates/unicode-ccc/0.4.0/download Repository: https://github.com/RazrFalcon/unicode-ccc +unicode-general-category 1.0.0: Apache-2.0 +Authors: YesLogic Pty. Ltd. +Source: https://crates.io/api/v1/crates/unicode-general-category/1.0.0/download +Repository: https://github.com/yeslogic/unicode-general-category + unicode-id 0.3.7: MIT OR Apache-2.0 Authors: Boshen ; erick.tryzelaar ; kwantam ; Manish Goregaokar Source: https://crates.io/api/v1/crates/unicode-id/0.3.7/download @@ -3473,6 +3608,11 @@ Authors: Evgeniy Reizner Source: https://crates.io/api/v1/crates/unicode-vo/0.1.0/download Repository: https://github.com/RazrFalcon/unicode-vo +unicode-width 0.1.14: MIT OR Apache-2.0 +Authors: kwantam ; Manish Goregaokar +Source: https://crates.io/api/v1/crates/unicode-width/0.1.14/download +Repository: https://github.com/unicode-rs/unicode-width + unicode-width 0.2.2: MIT OR Apache-2.0 Authors: kwantam ; Manish Goregaokar Source: https://crates.io/api/v1/crates/unicode-width/0.2.2/download @@ -3613,8 +3753,8 @@ webpki-roots 0.26.11: CDLA-Permissive-2.0 Source: https://crates.io/api/v1/crates/webpki-roots/0.26.11/download Repository: https://github.com/rustls/webpki-roots -webpki-roots 1.0.9: CDLA-Permissive-2.0 -Source: https://crates.io/api/v1/crates/webpki-roots/1.0.9/download +webpki-roots 1.0.11: CDLA-Permissive-2.0 +Source: https://crates.io/api/v1/crates/webpki-roots/1.0.11/download Repository: https://github.com/rustls/webpki-roots weezl 0.1.12: MIT OR Apache-2.0 @@ -4197,9 +4337,9 @@ The `cfg_aliases!` macro uses a lot of the code from [`tectonic_cfg_support::tar ======================================================================== License: CDLA-Permissive-2.0 -Used by: webpki-roots 0.26.11, webpki-roots 1.0.9 +Used by: webpki-roots 0.26.11, webpki-roots 1.0.11 Original notice: webpki-roots 0.26.11 / LICENSE -Original notice: webpki-roots 1.0.9 / LICENSE +Original notice: webpki-roots 1.0.11 / LICENSE # Community Data License Agreement - Permissive - Version 2.0 @@ -4660,6 +4800,521 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) { return 0; } +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Codegen exports used by `tonic-build`. + +pub use async_trait::async_trait; +pub use tokio_stream; + +pub use std::future::Future; +pub use std::pin::Pin; +pub use std::sync::Arc; +pub use std::task::{Context, Poll}; +pub use tower_service::Service; +pub type StdError = Box; +pub use crate::codec::{CompressionEncoding, EnabledCompressionEncodings}; +pub use crate::extensions::GrpcMethod; +pub use crate::service::interceptor::InterceptedService; +pub use bytes::Bytes; +pub use http; +pub use http_body::Body; + +pub type BoxFuture = self::Pin> + Send + 'static>>; +pub type BoxStream = + self::Pin> + Send + 'static>>; + +======================================================================== +License: MIT +Used by: tonic-prost 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Prost codec implementation for tonic. +//! +//! This crate provides the [`ProstCodec`] for encoding and decoding protobuf +//! messages using the [`prost`] library. +//! +//! # Example +//! +//! ```rust,ignore +//! use tonic_prost::ProstCodec; +//! +//! let codec = ProstCodec::::default(); +//! ``` + +#![warn( + missing_docs, + missing_debug_implementations, + rust_2018_idioms, + unreachable_pub +)] +#![doc( + html_logo_url = "https://raw.githubusercontent.com/tokio-rs/website/master/public/img/icons/tonic.svg" +)] +#![doc(html_root_url = "https://docs.rs/tonic-prost/0.13.1")] +#![doc(issue_tracker_base_url = "https://github.com/grpc/grpc-rust/issues/")] + +mod codec; + +pub use codec::{ProstCodec, ProstDecoder, ProstEncoder}; + +// Re-export prost types that users might need +pub use prost; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Utilities for using Tower services with Tonic. + +pub mod interceptor; +pub(crate) mod layered; +#[cfg(feature = "router")] +pub(crate) mod router; + +#[doc(inline)] +pub use self::interceptor::{Interceptor, InterceptorLayer}; +pub use self::layered::{LayerExt, Layered}; +#[doc(inline)] +#[cfg(feature = "router")] +pub use self::router::{Routes, RoutesBuilder}; +#[cfg(feature = "router")] +pub use axum::{Router as AxumRouter, body::Body as AxumBody}; + +pub mod recover_error; +pub use self::recover_error::{RecoverError, RecoverErrorLayer}; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +//! Various utilities used throughout tonic. + +// some combinations of features might cause things here not to be used +#![allow(dead_code)] + +pub(crate) mod base64 { + use base64::{ + alphabet, + engine::{ + DecodePaddingMode, + general_purpose::{GeneralPurpose, GeneralPurposeConfig}, + }, + }; + + pub(crate) const STANDARD: GeneralPurpose = GeneralPurpose::new( + &alphabet::STANDARD, + GeneralPurposeConfig::new() + .with_encode_padding(true) + .with_decode_padding_mode(DecodePaddingMode::Indifferent), + ); + + pub(crate) const STANDARD_NO_PAD: GeneralPurpose = GeneralPurpose::new( + &alphabet::STANDARD, + GeneralPurposeConfig::new() + .with_encode_padding(false) + .with_decode_padding_mode(DecodePaddingMode::Indifferent), + ); +} + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +/// A gRPC Method info extension. +#[derive(Debug, Clone)] +pub struct GrpcMethod<'a> { + service: &'a str, + method: &'a str, +} + +impl<'a> GrpcMethod<'a> { + /// Create a new `GrpcMethod` extension. + #[doc(hidden)] + pub fn new(service: &'a str, method: &'a str) -> Self { + Self { service, method } + } + + /// gRPC service name + pub fn service(&self) -> &str { + self.service + } + /// gRPC method name + pub fn method(&self) -> &str { + self.method + } +} + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +mod add_origin; +use self::add_origin::AddOrigin; + +mod user_agent; +use self::user_agent::UserAgent; + +mod reconnect; +use self::reconnect::Reconnect; + +mod connection; +pub(super) use self::connection::Connection; + +mod discover; +pub use self::discover::Change; +pub(super) use self::discover::DynamicServiceStream; + +mod io; +use self::io::BoxedIo; + +mod connector; +pub(crate) use self::connector::Connector; + +mod executor; +pub(super) use self::executor::{Executor, SharedExec}; + +#[cfg(feature = "_tls-any")] +mod tls; +#[cfg(feature = "_tls-any")] +pub(super) use self::tls::TlsConnector; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +mod io; +pub(crate) use self::io::{ConnectInfoLayer, ServerIo}; + +#[cfg(feature = "_tls-any")] +mod tls; +#[cfg(feature = "_tls-any")] +pub(crate) use self::tls::TlsAcceptor; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +pub(crate) mod grpc_timeout; +#[cfg(feature = "_tls-any")] +pub(crate) mod tls; + +pub(crate) use self::grpc_timeout::GrpcTimeout; + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +use crate::transport::channel::BoxFuture; +use hyper_util::rt::TokioExecutor; +use std::{future::Future, sync::Arc}; + +pub(crate) use hyper::rt::Executor; + +#[derive(Clone)] +pub(crate) struct SharedExec { + inner: Arc> + Send + Sync + 'static>, +} + +impl SharedExec { + pub(crate) fn new(exec: E) -> Self + where + E: Executor> + Send + Sync + 'static, + { + Self { + inner: Arc::new(exec), + } + } + + pub(crate) fn tokio() -> Self { + Self::new(TokioExecutor::new()) + } +} + +impl Executor for SharedExec +where + F: Future + Send + 'static, +{ + fn execute(&self, fut: F) { + self.inner.execute(Box::pin(fut)) + } +} + +======================================================================== +License: MIT +Used by: tonic 0.14.7 + +/* + * + * Copyright 2025 gRPC authors. + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + * IN THE SOFTWARE. + * + */ + +use super::Connected; +use std::sync::Arc; + +/// Connection info for Unix domain socket streams. +/// +/// This type will be accessible through [request extensions][ext] if you're using +/// a unix stream. +/// +/// See [Connected] for more details. +/// +/// [ext]: crate::Request::extensions +#[derive(Clone, Debug)] +pub struct UdsConnectInfo { + /// Peer address. This will be "unnamed" for client unix sockets. + pub peer_addr: Option>, + /// Process credentials for the unix socket. + pub peer_cred: Option, +} + +impl Connected for tokio::net::UnixStream { + type ConnectInfo = UdsConnectInfo; + + fn connect_info(&self) -> Self::ConnectInfo { + UdsConnectInfo { + peer_addr: self.peer_addr().ok().map(Arc::new), + peer_cred: self.peer_cred().ok(), + } + } +} + ======================================================================== License: MIT Used by: freetype-sys 0.20.1 @@ -6504,6 +7159,241 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +======================================================================== +License: Apache-2.0 +Used by: miette-derive 7.6.0 +Original notice: miette-derive 7.6.0 / LICENSE + +Apache License + +Version 2.0, January 2004 + +http://www.apache.org/licenses/ TERMS +AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + + + + "License" shall mean the terms and conditions for use, reproduction, and +distribution as defined by Sections 1 through 9 of this document. + + + + +"Licensor" shall mean the copyright owner or entity authorized by the copyright +owner that is granting the License. + + + + "Legal Entity" shall mean the +union of the acting entity and all other entities that control, are controlled +by, or are under common control with that entity. For the purposes of this +definition, "control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or otherwise, or (ii) +ownership of fifty percent (50%) or more of the outstanding shares, or (iii) +beneficial ownership of such entity. + + + + "You" (or "Your") shall mean +an individual or Legal Entity exercising permissions granted by this License. + + + + + "Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation source, and +configuration files. + + + + "Object" form shall mean any form resulting +from mechanical transformation or translation of a Source form, including but not +limited to compiled object code, generated documentation, and conversions to +other media types. + + + + "Work" shall mean the work of authorship, +whether in Source or Object form, made available under the License, as indicated +by a copyright notice that is included in or attached to the work (an example is +provided in the Appendix below). + + + + "Derivative Works" shall mean any +work, whether in Source or Object form, that is based on (or derived from) the +Work and for which the editorial revisions, annotations, elaborations, or other +modifications represent, as a whole, an original work of authorship. For the +purposes of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, the Work +and Derivative Works thereof. + + + + "Contribution" shall mean any work +of authorship, including the original version of the Work and any modifications +or additions to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner or by an +individual or Legal Entity authorized to submit on behalf of the copyright owner. +For the purposes of this definition, "submitted" means any form of electronic, +verbal, or written communication sent to the Licensor or its representatives, +including but not limited to communication on electronic mailing lists, source +code control systems, and issue tracking systems that are managed by, or on +behalf of, the Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise designated in +writing by the copyright owner as "Not a Contribution." + + + + +"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of +whom a Contribution has been received by Licensor and subsequently incorporated +within the Work. + + 2. Grant of Copyright License. Subject to the terms and +conditions of this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license +to reproduce, prepare Derivative Works of, publicly display, publicly perform, +sublicense, and distribute the Work and such Derivative Works in Source or Object +form. + + 3. Grant of Patent License. Subject to the terms and conditions of this +License, each Contributor hereby grants to You a perpetual, worldwide, +non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this +section) patent license to make, have made, use, offer to sell, sell, import, and +otherwise transfer the Work, where such license applies only to those patent +claims licensable by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) with the Work to +which such Contribution(s) was submitted. If You institute patent litigation +against any entity (including a cross-claim or counterclaim in a lawsuit) +alleging that the Work or a Contribution incorporated within the Work constitutes +direct or contributory patent infringement, then any patent licenses granted to +You under this License for that Work shall terminate as of the date such +litigation is filed. + + 4. Redistribution. You may reproduce and distribute +copies of the Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You meet the following +conditions: + + (a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + + (b) You must cause any +modified files to carry prominent notices stating that You changed the files; +and + + (c) You must retain, in the Source form of any Derivative Works that +You distribute, all copyright, patent, trademark, and attribution notices from +the Source form of the Work, excluding those notices that do not pertain to any +part of the Derivative Works; and + + (d) If the Work includes a "NOTICE" text +file as part of its distribution, then any Derivative Works that You distribute +must include a readable copy of the attribution notices contained within such +NOTICE file, excluding those notices that do not pertain to any part of the +Derivative Works, in at least one of the following places: within a NOTICE text +file distributed as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, within a display +generated by the Derivative Works, if and wherever such third-party notices +normally appear. The contents of the NOTICE file are for informational purposes +only and do not modify the License. You may add Your own attribution notices +within Derivative Works that You distribute, alongside or as an addendum to the +NOTICE text from the Work, provided that such additional attribution notices +cannot be construed as modifying the License. + + You may add Your own +copyright statement to Your modifications and may provide additional or different +license terms and conditions for use, reproduction, or distribution of Your +modifications, or for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with the conditions +stated in this License. + + 5. Submission of Contributions. Unless You explicitly +state otherwise, any Contribution intentionally submitted for inclusion in the +Work by You to the Licensor shall be under the terms and conditions of this +License, without any additional terms or conditions. Notwithstanding the above, +nothing herein shall supersede or modify the terms of any separate license +agreement you may have executed with Licensor regarding such Contributions. + + +6. Trademarks. This License does not grant permission to use the trade names, +trademarks, service marks, or product names of the Licensor, except as required +for reasonable and customary use in describing the origin of the Work and +reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless +required by applicable law or agreed to in writing, Licensor provides the Work +(and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT +WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, +without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, +MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible +for determining the appropriateness of using or redistributing the Work and +assume any risks associated with Your exercise of permissions under this +License. + + 8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, unless required +by applicable law (such as deliberate and grossly negligent acts) or agreed to in +writing, shall any Contributor be liable to You for damages, including any +direct, indirect, special, incidental, or consequential damages of any character +arising as a result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, work stoppage, +computer failure or malfunction, or any and all other commercial damages or +losses), even if such Contributor has been advised of the possibility of such +damages. + + 9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, and charge a fee +for, acceptance of support, warranty, indemnity, or other liability obligations +and/or rights consistent with this License. However, in accepting such +obligations, You may act only on Your own behalf and on Your sole responsibility, +not on behalf of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability incurred by, or +claims asserted against, such Contributor by reason of your accepting any such +warranty or additional liability. END OF TERMS AND CONDITIONS + +APPENDIX: How to +apply the Apache License to your work. + +To apply the Apache License to your work, +attach the following boilerplate notice, with the fields enclosed by brackets +"[]" replaced with your own identifying information. (Don't include the +brackets!) The text should be enclosed in the appropriate comment syntax for the +file format. We also recommend that a file or class name and description of +purpose be included on the same "printed page" as the copyright notice for easier +identification within third-party archives. + +Copyright [yyyy] Kat +Marchán + +Licensed under the Apache License, Version 2.0 (the "License"); + +you may +not use this file except in compliance with the License. + +You may obtain a copy +of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by +applicable law or agreed to in writing, software + +distributed under the License +is distributed on an "AS IS" BASIS, + +WITHOUT WARRANTIES OR CONDITIONS OF ANY +KIND, either express or implied. + +See the License for the specific language +governing permissions and + +limitations under the License. + ======================================================================== Original notice: fdeflate 0.3.7 / LICENSE-APACHE Original notice: half 2.7.1 / LICENSE-APACHE @@ -6715,42 +7605,249 @@ Apache License END OF TERMS AND CONDITIONS ======================================================================== -Original notice: windows 0.58.0 / license-apache-2.0 -Original notice: windows 0.62.2 / license-apache-2.0 -Original notice: windows-collections 0.3.2 / license-apache-2.0 -Original notice: windows-core 0.58.0 / license-apache-2.0 -Original notice: windows-core 0.62.2 / license-apache-2.0 -Original notice: windows-future 0.3.2 / license-apache-2.0 -Original notice: windows-implement 0.58.0 / license-apache-2.0 -Original notice: windows-implement 0.60.2 / license-apache-2.0 -Original notice: windows-interface 0.58.0 / license-apache-2.0 -Original notice: windows-interface 0.59.3 / license-apache-2.0 -Original notice: windows-link 0.1.3 / license-apache-2.0 -Original notice: windows-link 0.2.1 / license-apache-2.0 -Original notice: windows-numerics 0.3.1 / license-apache-2.0 -Original notice: windows-registry 0.4.0 / license-apache-2.0 -Original notice: windows-registry 0.6.1 / license-apache-2.0 -Original notice: windows-result 0.2.0 / license-apache-2.0 -Original notice: windows-result 0.3.4 / license-apache-2.0 -Original notice: windows-result 0.4.1 / license-apache-2.0 -Original notice: windows-strings 0.1.0 / license-apache-2.0 -Original notice: windows-strings 0.3.1 / license-apache-2.0 -Original notice: windows-strings 0.5.1 / license-apache-2.0 -Original notice: windows-sys 0.48.0 / license-apache-2.0 -Original notice: windows-sys 0.52.0 / license-apache-2.0 -Original notice: windows-sys 0.59.0 / license-apache-2.0 -Original notice: windows-sys 0.61.2 / license-apache-2.0 -Original notice: windows-targets 0.48.5 / license-apache-2.0 -Original notice: windows-targets 0.52.6 / license-apache-2.0 -Original notice: windows-targets 0.53.5 / license-apache-2.0 -Original notice: windows-threading 0.2.1 / license-apache-2.0 -Original notice: windows-version 0.1.7 / license-apache-2.0 -Original notice: windows_x86_64_gnu 0.48.5 / license-apache-2.0 -Original notice: windows_x86_64_gnu 0.52.6 / license-apache-2.0 -Original notice: windows_x86_64_gnu 0.53.1 / license-apache-2.0 -Original notice: windows_x86_64_msvc 0.48.5 / license-apache-2.0 -Original notice: windows_x86_64_msvc 0.52.6 / license-apache-2.0 -Original notice: windows_x86_64_msvc 0.53.1 / license-apache-2.0 +Original notice: windows 0.58.0 / license-apache-2.0 +Original notice: windows 0.62.2 / license-apache-2.0 +Original notice: windows-collections 0.3.2 / license-apache-2.0 +Original notice: windows-core 0.58.0 / license-apache-2.0 +Original notice: windows-core 0.62.2 / license-apache-2.0 +Original notice: windows-future 0.3.2 / license-apache-2.0 +Original notice: windows-implement 0.58.0 / license-apache-2.0 +Original notice: windows-implement 0.60.2 / license-apache-2.0 +Original notice: windows-interface 0.58.0 / license-apache-2.0 +Original notice: windows-interface 0.59.3 / license-apache-2.0 +Original notice: windows-link 0.1.3 / license-apache-2.0 +Original notice: windows-link 0.2.1 / license-apache-2.0 +Original notice: windows-numerics 0.3.1 / license-apache-2.0 +Original notice: windows-registry 0.4.0 / license-apache-2.0 +Original notice: windows-registry 0.6.1 / license-apache-2.0 +Original notice: windows-result 0.2.0 / license-apache-2.0 +Original notice: windows-result 0.3.4 / license-apache-2.0 +Original notice: windows-result 0.4.1 / license-apache-2.0 +Original notice: windows-strings 0.1.0 / license-apache-2.0 +Original notice: windows-strings 0.3.1 / license-apache-2.0 +Original notice: windows-strings 0.5.1 / license-apache-2.0 +Original notice: windows-sys 0.48.0 / license-apache-2.0 +Original notice: windows-sys 0.52.0 / license-apache-2.0 +Original notice: windows-sys 0.59.0 / license-apache-2.0 +Original notice: windows-sys 0.61.2 / license-apache-2.0 +Original notice: windows-targets 0.48.5 / license-apache-2.0 +Original notice: windows-targets 0.52.6 / license-apache-2.0 +Original notice: windows-targets 0.53.5 / license-apache-2.0 +Original notice: windows-threading 0.2.1 / license-apache-2.0 +Original notice: windows-version 0.1.7 / license-apache-2.0 +Original notice: windows_x86_64_gnu 0.48.5 / license-apache-2.0 +Original notice: windows_x86_64_gnu 0.52.6 / license-apache-2.0 +Original notice: windows_x86_64_gnu 0.53.1 / license-apache-2.0 +Original notice: windows_x86_64_msvc 0.48.5 / license-apache-2.0 +Original notice: windows_x86_64_msvc 0.52.6 / license-apache-2.0 +Original notice: windows_x86_64_msvc 0.53.1 / license-apache-2.0 + +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + + TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + + 1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + + 2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + + 3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + + 4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + + 5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + + 6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + + 7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + + 8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + + 9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + + END OF TERMS AND CONDITIONS + + APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + + Copyright (c) Microsoft Corporation. + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + +======================================================================== +License: Apache-2.0 +Used by: pulldown-cmark-to-cmark 22.0.3 +Original notice: pulldown-cmark-to-cmark 22.0.3 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -6940,7 +8037,7 @@ Apache License same "printed page" as the copyright notice for easier identification within third-party archives. - Copyright (c) Microsoft Corporation. + Copyright 2018 "Sebastian Thiel ", "Dylan Owen ", "Alessandro Ogier ", "Zixian Cai <2891235+caizixian@users.noreply.github.com>" Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -8395,7 +9492,7 @@ Apache License ======================================================================== License: Apache-2.0 -Used by: clang-sys 1.9.1, codespan-reporting 0.13.1, self_cell 1.3.0, unicode-linebreak 0.1.5 +Used by: clang-sys 1.9.1, codespan-reporting 0.13.1, miette 7.6.0, self_cell 1.3.0, unicode-linebreak 0.1.5 License: Apache-2.0 Used by: russh-sftp 3.0.1 Original notice: as-raw-xcb-connection 1.0.1 / LICENSE-APACHE @@ -8406,7 +9503,7 @@ Original notice: clang-sys 1.9.1 / LICENSE.txt Original notice: cmov 0.5.4 / LICENSE-APACHE Original notice: codespan-reporting 0.13.1 / LICENSE Original notice: crypto-primes 0.7.2 / LICENSE-APACHE -Original notice: ctutils 0.4.2 / LICENSE-APACHE +Original notice: ctutils 0.4.8 / LICENSE-APACHE Original notice: encoding_rs 0.8.42 / LICENSE-APACHE Original notice: flume 0.12.0 / LICENSE-APACHE Original notice: gl_generator@0.14.0 / upstream/brendanzab-gl-rs-license-cfc7749b96f6.txt @@ -8415,6 +9512,7 @@ Original notice: khronos_api@3.1.0 / upstream/brendanzab-gl-rs-license-cfc7749b9 Source: https://raw.githubusercontent.com/brendanzab/gl-rs/f150967b1c44ae888e6676f93f639ebc82771bdc/LICENSE Original notice: kurbo 0.11.3 / LICENSE-APACHE Original notice: kurbo 0.13.1 / LICENSE-APACHE +Original notice: miette 7.6.0 / LICENSE Original notice: multiversion_no_op 1.0.0 / LICENSE-APACHE Original notice: nohash-hasher 0.2.0 / LICENSE-APACHE Original notice: pageant@0.2.3 / upstream/brendanzab-gl-rs-license-cfc7749b96f6.txt @@ -9768,6 +10866,8 @@ Apache License limitations under the License. ======================================================================== +License: Apache-2.0 +Used by: unicode-general-category 1.0.0 Original notice: annotate-snippets 0.12.16 / LICENSE-APACHE Original notice: anstyle 1.0.14 / LICENSE-APACHE Original notice: bit_field 0.10.3 / LICENSE-APACHE @@ -9796,6 +10896,7 @@ Original notice: toml_edit 0.25.15+spec-1.1.0 / LICENSE-APACHE Original notice: toml_parser 1.1.3+spec-1.1.0 / LICENSE-APACHE Original notice: toml_write 0.1.2 / LICENSE-APACHE Original notice: toml_writer 1.1.2+spec-1.1.0 / LICENSE-APACHE +Original notice: unicode-general-category 1.0.0 / LICENSE Original notice: winapi 0.3.9 / LICENSE-APACHE Original notice: wio 0.2.2 / LICENSE-APACHE @@ -10975,52 +12076,240 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION of your accepting any such warranty or additional liability. ======================================================================== -Original notice: accesskit@0.24.1 / upstream/AccessKit-accesskit-license-apache-62c7a1e35f56.txt -Source: https://raw.githubusercontent.com/AccessKit/accesskit/a55d3e1a18bb9ef0e4bccc9083fb13c3e0ad8969/LICENSE-APACHE -Original notice: accesskit_atspi_common@0.19.1, accesskit_consumer@0.38.0, accesskit_macos@0.26.3, accesskit_unix@0.22.1, accesskit_windows@0.34.0 / upstream/AccessKit-accesskit-license-apache-62c7a1e35f56.txt -Source: https://raw.githubusercontent.com/AccessKit/accesskit/c88605b96d04431f9c3c792464a0f2f253480e94/LICENSE-APACHE -Original notice: allocator-api2 0.2.21 / LICENSE-APACHE -Original notice: anyhow 1.0.104 / LICENSE-APACHE -Original notice: async-trait 0.1.92 / LICENSE-APACHE -Original notice: dyn-clone 1.0.20 / LICENSE-APACHE -Original notice: enumn 0.1.14 / LICENSE-APACHE -Original notice: erased-serde 0.4.10 / LICENSE-APACHE -Original notice: inventory 0.3.24 / LICENSE-APACHE -Original notice: itoa 1.0.18 / LICENSE-APACHE -Original notice: libc 0.2.190 / LICENSE-APACHE -Original notice: litrs 1.0.0 / LICENSE-APACHE -Original notice: mach2 0.5.0 / LICENSE-APACHE -Original notice: paste 1.0.15 / LICENSE-APACHE -Original notice: prettyplease 0.2.37 / LICENSE-APACHE -Original notice: proc-macro2 1.0.107 / LICENSE-APACHE -Original notice: process-wrap 9.1.1 / LICENSE-APACHE -Original notice: quote 1.0.47 / LICENSE-APACHE -Original notice: ref-cast 1.0.27 / LICENSE-APACHE -Original notice: ref-cast-impl 1.0.27 / LICENSE-APACHE -Original notice: rustc-hash 2.1.3 / LICENSE-APACHE -Original notice: rustversion 1.0.23 / LICENSE-APACHE -Original notice: ryu 1.0.23 / LICENSE-APACHE -Original notice: semver 1.0.28 / LICENSE-APACHE -Original notice: serde 1.0.229 / LICENSE-APACHE -Original notice: serde-saphyr 1.3.0 / LICENSE-APACHE -Original notice: serde_bytes 0.11.19 / LICENSE-APACHE -Original notice: serde_core 1.0.229 / LICENSE-APACHE -Original notice: serde_derive 1.0.229 / LICENSE-APACHE -Original notice: serde_derive_internals 0.30.0 / LICENSE-APACHE -Original notice: serde_json 1.0.151 / LICENSE-APACHE -Original notice: serde_json_lenient 0.2.4 / LICENSE-APACHE -Original notice: serde_repr 0.1.21 / LICENSE-APACHE -Original notice: serde_urlencoded 0.7.1 / LICENSE-APACHE -Original notice: syn 2.0.119 / LICENSE-APACHE -Original notice: syn 3.0.6 / LICENSE-APACHE -Original notice: thiserror 1.0.69 / LICENSE-APACHE -Original notice: thiserror 2.0.21 / LICENSE-APACHE -Original notice: thiserror-impl 1.0.69 / LICENSE-APACHE -Original notice: thiserror-impl 2.0.21 / LICENSE-APACHE -Original notice: typeid 1.0.3 / LICENSE-APACHE -Original notice: unicode-ident 1.0.26 / LICENSE-APACHE -Original notice: utf-8 0.7.6 / LICENSE-APACHE -Original notice: vte 0.15.0 / LICENSE-APACHE +Original notice: accesskit@0.24.1 / upstream/AccessKit-accesskit-license-apache-62c7a1e35f56.txt +Source: https://raw.githubusercontent.com/AccessKit/accesskit/a55d3e1a18bb9ef0e4bccc9083fb13c3e0ad8969/LICENSE-APACHE +Original notice: accesskit_atspi_common@0.19.1, accesskit_consumer@0.38.0, accesskit_macos@0.26.3, accesskit_unix@0.22.1, accesskit_windows@0.34.0 / upstream/AccessKit-accesskit-license-apache-62c7a1e35f56.txt +Source: https://raw.githubusercontent.com/AccessKit/accesskit/c88605b96d04431f9c3c792464a0f2f253480e94/LICENSE-APACHE +Original notice: allocator-api2 0.2.21 / LICENSE-APACHE +Original notice: anyhow 1.0.104 / LICENSE-APACHE +Original notice: async-trait 0.1.92 / LICENSE-APACHE +Original notice: dyn-clone 1.0.20 / LICENSE-APACHE +Original notice: enumn 0.1.14 / LICENSE-APACHE +Original notice: erased-serde 0.4.10 / LICENSE-APACHE +Original notice: inventory 0.3.24 / LICENSE-APACHE +Original notice: itoa 1.0.18 / LICENSE-APACHE +Original notice: libc 0.2.190 / LICENSE-APACHE +Original notice: litrs 1.0.0 / LICENSE-APACHE +Original notice: mach2 0.5.0 / LICENSE-APACHE +Original notice: paste 1.0.15 / LICENSE-APACHE +Original notice: prettyplease 0.2.37 / LICENSE-APACHE +Original notice: proc-macro2 1.0.107 / LICENSE-APACHE +Original notice: process-wrap 9.1.1 / LICENSE-APACHE +Original notice: quote 1.0.47 / LICENSE-APACHE +Original notice: ref-cast 1.0.27 / LICENSE-APACHE +Original notice: ref-cast-impl 1.0.27 / LICENSE-APACHE +Original notice: rustc-hash 2.1.3 / LICENSE-APACHE +Original notice: rustversion 1.0.23 / LICENSE-APACHE +Original notice: ryu 1.0.23 / LICENSE-APACHE +Original notice: semver 1.0.28 / LICENSE-APACHE +Original notice: serde 1.0.229 / LICENSE-APACHE +Original notice: serde-saphyr 1.3.0 / LICENSE-APACHE +Original notice: serde_bytes 0.11.19 / LICENSE-APACHE +Original notice: serde_core 1.0.229 / LICENSE-APACHE +Original notice: serde_derive 1.0.229 / LICENSE-APACHE +Original notice: serde_derive_internals 0.30.0 / LICENSE-APACHE +Original notice: serde_json 1.0.151 / LICENSE-APACHE +Original notice: serde_json_lenient 0.2.4 / LICENSE-APACHE +Original notice: serde_repr 0.1.21 / LICENSE-APACHE +Original notice: serde_urlencoded 0.7.1 / LICENSE-APACHE +Original notice: syn 2.0.119 / LICENSE-APACHE +Original notice: syn 3.0.6 / LICENSE-APACHE +Original notice: thiserror 1.0.69 / LICENSE-APACHE +Original notice: thiserror 2.0.21 / LICENSE-APACHE +Original notice: thiserror-impl 1.0.69 / LICENSE-APACHE +Original notice: thiserror-impl 2.0.21 / LICENSE-APACHE +Original notice: typeid 1.0.3 / LICENSE-APACHE +Original notice: unicode-ident 1.0.26 / LICENSE-APACHE +Original notice: utf-8 0.7.6 / LICENSE-APACHE +Original notice: vte 0.15.0 / LICENSE-APACHE + +Apache License + Version 2.0, January 2004 + http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + + "License" shall mean the terms and conditions for use, reproduction, + and distribution as defined by Sections 1 through 9 of this document. + + "Licensor" shall mean the copyright owner or entity authorized by + the copyright owner that is granting the License. + + "Legal Entity" shall mean the union of the acting entity and all + other entities that control, are controlled by, or are under common + control with that entity. For the purposes of this definition, + "control" means (i) the power, direct or indirect, to cause the + direction or management of such entity, whether by contract or + otherwise, or (ii) ownership of fifty percent (50%) or more of the + outstanding shares, or (iii) beneficial ownership of such entity. + + "You" (or "Your") shall mean an individual or Legal Entity + exercising permissions granted by this License. + + "Source" form shall mean the preferred form for making modifications, + including but not limited to software source code, documentation + source, and configuration files. + + "Object" form shall mean any form resulting from mechanical + transformation or translation of a Source form, including but + not limited to compiled object code, generated documentation, + and conversions to other media types. + + "Work" shall mean the work of authorship, whether in Source or + Object form, made available under the License, as indicated by a + copyright notice that is included in or attached to the work + (an example is provided in the Appendix below). + + "Derivative Works" shall mean any work, whether in Source or Object + form, that is based on (or derived from) the Work and for which the + editorial revisions, annotations, elaborations, or other modifications + represent, as a whole, an original work of authorship. For the purposes + of this License, Derivative Works shall not include works that remain + separable from, or merely link (or bind by name) to the interfaces of, + the Work and Derivative Works thereof. + + "Contribution" shall mean any work of authorship, including + the original version of the Work and any modifications or additions + to that Work or Derivative Works thereof, that is intentionally + submitted to Licensor for inclusion in the Work by the copyright owner + or by an individual or Legal Entity authorized to submit on behalf of + the copyright owner. For the purposes of this definition, "submitted" + means any form of electronic, verbal, or written communication sent + to the Licensor or its representatives, including but not limited to + communication on electronic mailing lists, source code control systems, + and issue tracking systems that are managed by, or on behalf of, the + Licensor for the purpose of discussing and improving the Work, but + excluding communication that is conspicuously marked or otherwise + designated in writing by the copyright owner as "Not a Contribution." + + "Contributor" shall mean Licensor and any individual or Legal Entity + on behalf of whom a Contribution has been received by Licensor and + subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + copyright license to reproduce, prepare Derivative Works of, + publicly display, publicly perform, sublicense, and distribute the + Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of + this License, each Contributor hereby grants to You a perpetual, + worldwide, non-exclusive, no-charge, royalty-free, irrevocable + (except as stated in this section) patent license to make, have made, + use, offer to sell, sell, import, and otherwise transfer the Work, + where such license applies only to those patent claims licensable + by such Contributor that are necessarily infringed by their + Contribution(s) alone or by combination of their Contribution(s) + with the Work to which such Contribution(s) was submitted. If You + institute patent litigation against any entity (including a + cross-claim or counterclaim in a lawsuit) alleging that the Work + or a Contribution incorporated within the Work constitutes direct + or contributory patent infringement, then any patent licenses + granted to You under this License for that Work shall terminate + as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the + Work or Derivative Works thereof in any medium, with or without + modifications, and in Source or Object form, provided that You + meet the following conditions: + + (a) You must give any other recipients of the Work or + Derivative Works a copy of this License; and + + (b) You must cause any modified files to carry prominent notices + stating that You changed the files; and + + (c) You must retain, in the Source form of any Derivative Works + that You distribute, all copyright, patent, trademark, and + attribution notices from the Source form of the Work, + excluding those notices that do not pertain to any part of + the Derivative Works; and + + (d) If the Work includes a "NOTICE" text file as part of its + distribution, then any Derivative Works that You distribute must + include a readable copy of the attribution notices contained + within such NOTICE file, excluding those notices that do not + pertain to any part of the Derivative Works, in at least one + of the following places: within a NOTICE text file distributed + as part of the Derivative Works; within the Source form or + documentation, if provided along with the Derivative Works; or, + within a display generated by the Derivative Works, if and + wherever such third-party notices normally appear. The contents + of the NOTICE file are for informational purposes only and + do not modify the License. You may add Your own attribution + notices within Derivative Works that You distribute, alongside + or as an addendum to the NOTICE text from the Work, provided + that such additional attribution notices cannot be construed + as modifying the License. + + You may add Your own copyright statement to Your modifications and + may provide additional or different license terms and conditions + for use, reproduction, or distribution of Your modifications, or + for any such Derivative Works as a whole, provided Your use, + reproduction, and distribution of the Work otherwise complies with + the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, + any Contribution intentionally submitted for inclusion in the Work + by You to the Licensor shall be under the terms and conditions of + this License, without any additional terms or conditions. + Notwithstanding the above, nothing herein shall supersede or modify + the terms of any separate license agreement you may have executed + with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade + names, trademarks, service marks, or product names of the Licensor, + except as required for reasonable and customary use in describing the + origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or + agreed to in writing, Licensor provides the Work (and each + Contributor provides its Contributions) on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or + implied, including, without limitation, any warranties or conditions + of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A + PARTICULAR PURPOSE. You are solely responsible for determining the + appropriateness of using or redistributing the Work and assume any + risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, + whether in tort (including negligence), contract, or otherwise, + unless required by applicable law (such as deliberate and grossly + negligent acts) or agreed to in writing, shall any Contributor be + liable to You for damages, including any direct, indirect, special, + incidental, or consequential damages of any character arising as a + result of this License or out of the use or inability to use the + Work (including but not limited to damages for loss of goodwill, + work stoppage, computer failure or malfunction, or any and all + other commercial damages or losses), even if such Contributor + has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing + the Work or Derivative Works thereof, You may choose to offer, + and charge a fee for, acceptance of support, warranty, indemnity, + or other liability obligations and/or rights consistent with this + License. However, in accepting such obligations, You may act only + on Your own behalf and on Your sole responsibility, not on behalf + of any other Contributor, and only if You agree to indemnify, + defend, and hold each Contributor harmless for any liability + incurred by, or claims asserted against, such Contributor by reason + of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +======================================================================== +Original notice: futures 0.3.34 / LICENSE-APACHE +Original notice: futures-channel 0.3.34 / LICENSE-APACHE +Original notice: futures-core 0.3.34 / LICENSE-APACHE +Original notice: futures-executor 0.3.34 / LICENSE-APACHE +Original notice: futures-io 0.3.34 / LICENSE-APACHE +Original notice: futures-macro 0.3.34 / LICENSE-APACHE +Original notice: futures-sink 0.3.34 / LICENSE-APACHE +Original notice: futures-task 0.3.34 / LICENSE-APACHE +Original notice: futures-util 0.3.34 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -11199,16 +12488,34 @@ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION END OF TERMS AND CONDITIONS +APPENDIX: How to apply the Apache License to your work. + + To apply the Apache License to your work, attach the following + boilerplate notice, with the fields enclosed by brackets "[]" + replaced with your own identifying information. (Don't include + the brackets!) The text should be enclosed in the appropriate + comment syntax for the file format. We also recommend that a + file or class name and description of purpose be included on the + same "printed page" as the copyright notice for easier + identification within third-party archives. + +Copyright (c) 2016 Alex Crichton +Copyright (c) 2017 The Tokio Authors + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + ======================================================================== -Original notice: futures 0.3.34 / LICENSE-APACHE -Original notice: futures-channel 0.3.34 / LICENSE-APACHE -Original notice: futures-core 0.3.34 / LICENSE-APACHE -Original notice: futures-executor 0.3.34 / LICENSE-APACHE -Original notice: futures-io 0.3.34 / LICENSE-APACHE -Original notice: futures-macro 0.3.34 / LICENSE-APACHE -Original notice: futures-sink 0.3.34 / LICENSE-APACHE -Original notice: futures-task 0.3.34 / LICENSE-APACHE -Original notice: futures-util 0.3.34 / LICENSE-APACHE +Original notice: typenum 1.20.1 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -11398,8 +12705,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright (c) 2016 Alex Crichton -Copyright (c) 2017 The Tokio Authors +Copyright 2014 Paho Lurie-Gregg Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11414,7 +12720,7 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: typenum 1.20.1 / LICENSE-APACHE +Original notice: gpui-pre-reqwest 0.12.15 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -11604,7 +12910,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2014 Paho Lurie-Gregg +Copyright 2016 Sean McArthur Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11619,7 +12925,7 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: gpui-pre-reqwest 0.12.15 / LICENSE-APACHE +Original notice: http 1.5.0 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -11809,7 +13115,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2016 Sean McArthur +Copyright 2017 http-rs authors Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -11824,7 +13130,7 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: http 1.5.0 / LICENSE-APACHE +Original notice: tokio-rustls 0.26.6 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -12014,7 +13320,7 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2017 http-rs authors +Copyright 2017 quininer kel Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. @@ -12029,7 +13335,9 @@ See the License for the specific language governing permissions and limitations under the License. ======================================================================== -Original notice: tokio-rustls 0.26.6 / LICENSE-APACHE +Original notice: logos 0.16.1 / LICENSE-APACHE +Original notice: logos-codegen 0.16.1 / LICENSE-APACHE +Original notice: logos-derive 0.16.1 / LICENSE-APACHE Apache License Version 2.0, January 2004 @@ -12219,13 +13527,13 @@ APPENDIX: How to apply the Apache License to your work. same "printed page" as the copyright notice for easier identification within third-party archives. -Copyright 2017 quininer kel +Copyright 2018 Maciej Hirsz Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -14084,14 +15392,14 @@ limitations under the License. ======================================================================== License: Apache-2.0 -Used by: gethostname 1.1.0, ring 0.17.14 +Used by: gethostname 1.1.0, prost 0.14.4, prost-build 0.14.4, prost-derive 0.14.4, prost-types 0.14.4, ring 0.17.14 Original notice: addr2line 0.25.1 / LICENSE-APACHE Original notice: ahash 0.8.12 / LICENSE-APACHE Original notice: arc-swap 1.9.2 / LICENSE-APACHE Original notice: arrayvec 0.7.8 / LICENSE-APACHE Original notice: async-channel 2.5.0 / LICENSE-APACHE Original notice: async-compression 0.4.50 / LICENSE-APACHE -Original notice: async-executor 1.14.0 / LICENSE-APACHE +Original notice: async-executor 1.14.1 / LICENSE-APACHE Original notice: async-fs 2.2.0 / LICENSE-APACHE Original notice: async-io 2.6.0 / LICENSE-APACHE Original notice: async-lock 3.4.2 / LICENSE-APACHE @@ -14151,6 +15459,7 @@ Original notice: filetime 0.2.29 / LICENSE-APACHE Original notice: find-msvc-tools 0.1.14 / LICENSE-APACHE Original notice: fixedbitset 0.5.7 / LICENSE-APACHE Original notice: flate2 1.1.10 / LICENSE-APACHE +Original notice: fnv 1.0.7 / LICENSE-APACHE Original notice: form_urlencoded 1.2.2 / LICENSE-APACHE Original notice: fs2 0.4.3 / LICENSE-APACHE Original notice: futf 0.1.5 / LICENSE-APACHE @@ -14173,6 +15482,7 @@ Original notice: heck 0.5.0 / LICENSE-APACHE Original notice: html5ever 0.27.0 / LICENSE-APACHE Original notice: httparse 1.10.1 / LICENSE-APACHE Original notice: hyper-rustls 0.27.10 / LICENSE-APACHE +Original notice: hyper-timeout 0.5.2 / LICENSE-APACHE Original notice: idna 1.1.0 / LICENSE-APACHE Original notice: idna_adapter 1.2.2 / LICENSE-APACHE Original notice: indexmap 2.14.2 / LICENSE-APACHE @@ -14191,6 +15501,7 @@ Original notice: markup5ever_rcdom 0.3.0 / LICENSE-APACHE Original notice: metal 0.33.0 / LICENSE-APACHE Original notice: mime 0.3.17 / LICENSE-APACHE Original notice: miow 0.6.1 / LICENSE-APACHE +Original notice: multimap 0.10.1 / LICENSE-APACHE Original notice: num 0.4.3 / LICENSE-APACHE Original notice: num-bigint 0.4.8 / LICENSE-APACHE Original notice: num-bigint 0.5.1 / LICENSE-APACHE @@ -14211,6 +15522,7 @@ Original notice: parking_lot_core 0.9.12 / LICENSE-APACHE Original notice: pathfinder_simd@0.5.6 / upstream/servo-pathfinder-license-apache-a60eea817514.txt Source: https://raw.githubusercontent.com/servo/pathfinder/6c3c0466f451c5bd2007087728cd168798cd64e8/LICENSE-APACHE Original notice: percent-encoding 2.3.2 / LICENSE-APACHE +Original notice: petgraph 0.8.3 / LICENSE-APACHE Original notice: piper 0.2.5 / LICENSE-APACHE Original notice: pkg-config 0.3.34 / LICENSE-APACHE Original notice: png 0.17.16 / LICENSE-APACHE @@ -14220,6 +15532,13 @@ Original notice: pollster 0.2.5 / LICENSE-APACHE Original notice: pollster 0.4.0 / LICENSE-APACHE Original notice: profiling-procmacros@1.0.18, profiling@1.0.18 / upstream/aclysma-profiling-license-apache-10d30a673cd5.txt Source: https://raw.githubusercontent.com/aclysma/profiling/8271551172eb6fa4cba47369aedd93790c623df9/LICENSE-APACHE +Original notice: prost 0.14.4 / LICENSE +Original notice: prost-build 0.14.4 / LICENSE +Original notice: prost-derive 0.14.4 / LICENSE +Original notice: prost-reflect 0.16.5 / LICENSE-APACHE +Original notice: prost-types 0.14.4 / LICENSE +Original notice: protox 0.10.0 / LICENSE-APACHE +Original notice: protox-parse 0.10.0 / LICENSE-APACHE Original notice: rayon 1.12.0 / LICENSE-APACHE Original notice: rayon-core 1.13.0 / LICENSE-APACHE Original notice: regex 1.13.1 / LICENSE-APACHE @@ -14236,7 +15555,7 @@ Original notice: rustc_version 0.4.1 / LICENSE-APACHE Original notice: rustix 0.38.44 / LICENSE-APACHE Original notice: rustix 1.1.5 / LICENSE-APACHE Original notice: rustix-openpty 0.2.0 / LICENSE-APACHE -Original notice: rustls 0.23.45 / LICENSE-APACHE +Original notice: rustls 0.23.46 / LICENSE-APACHE Original notice: rustls-pemfile 2.2.0 / LICENSE-APACHE Original notice: scoped-tls 1.0.1 / LICENSE-APACHE Original notice: scopeguard 1.2.0 / LICENSE-APACHE @@ -14273,6 +15592,7 @@ Original notice: unicode-id 0.3.7 / LICENSE-APACHE Original notice: unicode-properties 0.1.4 / LICENSE-APACHE Original notice: unicode-segmentation 1.13.3 / LICENSE-APACHE Original notice: unicode-vo 0.1.0 / LICENSE-APACHE +Original notice: unicode-width 0.1.14 / LICENSE-APACHE Original notice: unicode-width 0.2.2 / LICENSE-APACHE Original notice: unicode-xid 0.2.6 / LICENSE-APACHE Original notice: url 2.5.8 / LICENSE-APACHE @@ -14725,7 +16045,7 @@ Original notice: const-oid 0.10.2 / LICENSE-APACHE Original notice: cpubits 0.1.1 / LICENSE-APACHE Original notice: cpufeatures 0.2.17 / LICENSE-APACHE Original notice: cpufeatures 0.3.1 / LICENSE-APACHE -Original notice: crypto-bigint 0.7.5 / LICENSE-APACHE +Original notice: crypto-bigint 0.7.11 / LICENSE-APACHE Original notice: crypto-common 0.1.7 / LICENSE-APACHE Original notice: crypto-common 0.2.2 / LICENSE-APACHE Original notice: ctr 0.10.1 / LICENSE-APACHE @@ -16247,6 +17567,211 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. +======================================================================== +Original notice: httpdate 1.0.3 / LICENSE-APACHE + +Apache License +Version 2.0, January 2004 +http://www.apache.org/licenses/ + +TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION + +1. Definitions. + +"License" shall mean the terms and conditions for use, reproduction, +and distribution as defined by Sections 1 through 9 of this document. + +"Licensor" shall mean the copyright owner or entity authorized by +the copyright owner that is granting the License. + +"Legal Entity" shall mean the union of the acting entity and all +other entities that control, are controlled by, or are under common +control with that entity. For the purposes of this definition, +"control" means (i) the power, direct or indirect, to cause the +direction or management of such entity, whether by contract or +otherwise, or (ii) ownership of fifty percent (50%) or more of the +outstanding shares, or (iii) beneficial ownership of such entity. + +"You" (or "Your") shall mean an individual or Legal Entity +exercising permissions granted by this License. + +"Source" form shall mean the preferred form for making modifications, +including but not limited to software source code, documentation +source, and configuration files. + +"Object" form shall mean any form resulting from mechanical +transformation or translation of a Source form, including but +not limited to compiled object code, generated documentation, +and conversions to other media types. + +"Work" shall mean the work of authorship, whether in Source or +Object form, made available under the License, as indicated by a +copyright notice that is included in or attached to the work +(an example is provided in the Appendix below). + +"Derivative Works" shall mean any work, whether in Source or Object +form, that is based on (or derived from) the Work and for which the +editorial revisions, annotations, elaborations, or other modifications +represent, as a whole, an original work of authorship. For the purposes +of this License, Derivative Works shall not include works that remain +separable from, or merely link (or bind by name) to the interfaces of, +the Work and Derivative Works thereof. + +"Contribution" shall mean any work of authorship, including +the original version of the Work and any modifications or additions +to that Work or Derivative Works thereof, that is intentionally +submitted to Licensor for inclusion in the Work by the copyright owner +or by an individual or Legal Entity authorized to submit on behalf of +the copyright owner. For the purposes of this definition, "submitted" +means any form of electronic, verbal, or written communication sent +to the Licensor or its representatives, including but not limited to +communication on electronic mailing lists, source code control systems, +and issue tracking systems that are managed by, or on behalf of, the +Licensor for the purpose of discussing and improving the Work, but +excluding communication that is conspicuously marked or otherwise +designated in writing by the copyright owner as "Not a Contribution." + +"Contributor" shall mean Licensor and any individual or Legal Entity +on behalf of whom a Contribution has been received by Licensor and +subsequently incorporated within the Work. + +2. Grant of Copyright License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +copyright license to reproduce, prepare Derivative Works of, +publicly display, publicly perform, sublicense, and distribute the +Work and such Derivative Works in Source or Object form. + +3. Grant of Patent License. Subject to the terms and conditions of +this License, each Contributor hereby grants to You a perpetual, +worldwide, non-exclusive, no-charge, royalty-free, irrevocable +(except as stated in this section) patent license to make, have made, +use, offer to sell, sell, import, and otherwise transfer the Work, +where such license applies only to those patent claims licensable +by such Contributor that are necessarily infringed by their +Contribution(s) alone or by combination of their Contribution(s) +with the Work to which such Contribution(s) was submitted. If You +institute patent litigation against any entity (including a +cross-claim or counterclaim in a lawsuit) alleging that the Work +or a Contribution incorporated within the Work constitutes direct +or contributory patent infringement, then any patent licenses +granted to You under this License for that Work shall terminate +as of the date such litigation is filed. + +4. Redistribution. You may reproduce and distribute copies of the +Work or Derivative Works thereof in any medium, with or without +modifications, and in Source or Object form, provided that You +meet the following conditions: + +(a) You must give any other recipients of the Work or +Derivative Works a copy of this License; and + +(b) You must cause any modified files to carry prominent notices +stating that You changed the files; and + +(c) You must retain, in the Source form of any Derivative Works +that You distribute, all copyright, patent, trademark, and +attribution notices from the Source form of the Work, +excluding those notices that do not pertain to any part of +the Derivative Works; and + +(d) If the Work includes a "NOTICE" text file as part of its +distribution, then any Derivative Works that You distribute must +include a readable copy of the attribution notices contained +within such NOTICE file, excluding those notices that do not +pertain to any part of the Derivative Works, in at least one +of the following places: within a NOTICE text file distributed +as part of the Derivative Works; within the Source form or +documentation, if provided along with the Derivative Works; or, +within a display generated by the Derivative Works, if and +wherever such third-party notices normally appear. The contents +of the NOTICE file are for informational purposes only and +do not modify the License. You may add Your own attribution +notices within Derivative Works that You distribute, alongside +or as an addendum to the NOTICE text from the Work, provided +that such additional attribution notices cannot be construed +as modifying the License. + +You may add Your own copyright statement to Your modifications and +may provide additional or different license terms and conditions +for use, reproduction, or distribution of Your modifications, or +for any such Derivative Works as a whole, provided Your use, +reproduction, and distribution of the Work otherwise complies with +the conditions stated in this License. + +5. Submission of Contributions. Unless You explicitly state otherwise, +any Contribution intentionally submitted for inclusion in the Work +by You to the Licensor shall be under the terms and conditions of +this License, without any additional terms or conditions. +Notwithstanding the above, nothing herein shall supersede or modify +the terms of any separate license agreement you may have executed +with Licensor regarding such Contributions. + +6. Trademarks. This License does not grant permission to use the trade +names, trademarks, service marks, or product names of the Licensor, +except as required for reasonable and customary use in describing the +origin of the Work and reproducing the content of the NOTICE file. + +7. Disclaimer of Warranty. Unless required by applicable law or +agreed to in writing, Licensor provides the Work (and each +Contributor provides its Contributions) on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +implied, including, without limitation, any warranties or conditions +of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A +PARTICULAR PURPOSE. You are solely responsible for determining the +appropriateness of using or redistributing the Work and assume any +risks associated with Your exercise of permissions under this License. + +8. Limitation of Liability. In no event and under no legal theory, +whether in tort (including negligence), contract, or otherwise, +unless required by applicable law (such as deliberate and grossly +negligent acts) or agreed to in writing, shall any Contributor be +liable to You for damages, including any direct, indirect, special, +incidental, or consequential damages of any character arising as a +result of this License or out of the use or inability to use the +Work (including but not limited to damages for loss of goodwill, +work stoppage, computer failure or malfunction, or any and all +other commercial damages or losses), even if such Contributor +has been advised of the possibility of such damages. + +9. Accepting Warranty or Additional Liability. While redistributing +the Work or Derivative Works thereof, You may choose to offer, +and charge a fee for, acceptance of support, warranty, indemnity, +or other liability obligations and/or rights consistent with this +License. However, in accepting such obligations, You may act only +on Your own behalf and on Your sole responsibility, not on behalf +of any other Contributor, and only if You agree to indemnify, +defend, and hold each Contributor harmless for any liability +incurred by, or claims asserted against, such Contributor by reason +of your accepting any such warranty or additional liability. + +END OF TERMS AND CONDITIONS + +APPENDIX: How to apply the Apache License to your work. + +To apply the Apache License to your work, attach the following +boilerplate notice, with the fields enclosed by brackets "[]" +replaced with your own identifying information. (Don't include +the brackets!) The text should be enclosed in the appropriate +comment syntax for the file format. We also recommend that a +file or class name and description of purpose be included on the +same "printed page" as the copyright notice for easier +identification within third-party archives. + +Copyright [yyyy] [name of copyright owner] + +Licensed under the Apache License, Version 2.0 (the "License"); +you may not use this file except in compliance with the License. +You may obtain a copy of the License at + +http://www.apache.org/licenses/LICENSE-2.0 + +Unless required by applicable law or agreed to in writing, software +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. + ======================================================================== Original notice: document-features 0.2.12 / LICENSE-APACHE SPDX reference license terms: Apache-2.0 @@ -17705,11 +19230,12 @@ THE SOFTWARE. ======================================================================== License: MIT -Used by: either 1.18.0, itertools 0.11.0, itertools 0.13.0, itertools 0.14.0, serde_with 3.24.0, serde_with_macros 3.24.0 +Used by: either 1.18.0, itertools 0.11.0, itertools 0.13.0, itertools 0.14.0, petgraph 0.8.3, serde_with 3.24.0, serde_with_macros 3.24.0 Original notice: either 1.18.0 / LICENSE-MIT Original notice: itertools 0.11.0 / LICENSE-MIT Original notice: itertools 0.13.0 / LICENSE-MIT Original notice: itertools 0.14.0 / LICENSE-MIT +Original notice: petgraph 0.8.3 / LICENSE-MIT Original notice: serde_with 3.24.0 / LICENSE-MIT Original notice: serde_with_macros 3.24.0 / LICENSE-MIT @@ -18203,7 +19729,7 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: fs2 0.4.3, gimli 0.32.3, heck 0.4.1, heck 0.5.0, unicode-bidi 0.3.18, unicode-id 0.3.7, unicode-properties 0.1.4, unicode-segmentation 1.13.3, unicode-width 0.2.2, unicode-xid 0.2.6 +Used by: fs2 0.4.3, gimli 0.32.3, heck 0.4.1, heck 0.5.0, unicode-bidi 0.3.18, unicode-id 0.3.7, unicode-properties 0.1.4, unicode-segmentation 1.13.3, unicode-width 0.1.14, unicode-width 0.2.2, unicode-xid 0.2.6 Original notice: fs2 0.4.3 / LICENSE-MIT Original notice: gimli 0.32.3 / LICENSE-MIT Original notice: heck 0.4.1 / LICENSE-MIT @@ -18212,6 +19738,7 @@ Original notice: unicode-bidi 0.3.18 / LICENSE-MIT Original notice: unicode-id 0.3.7 / LICENSE-MIT Original notice: unicode-properties 0.1.4 / LICENSE-MIT Original notice: unicode-segmentation 1.13.3 / LICENSE-MIT +Original notice: unicode-width 0.1.14 / LICENSE-MIT Original notice: unicode-width 0.2.2 / LICENSE-MIT Original notice: unicode-xid 0.2.6 / LICENSE-MIT @@ -18715,9 +20242,9 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: hyper-rustls 0.27.10, rustls 0.23.45, rustls-pemfile 2.2.0 +Used by: hyper-rustls 0.27.10, rustls 0.23.46, rustls-pemfile 2.2.0 Original notice: hyper-rustls 0.27.10 / LICENSE-MIT -Original notice: rustls 0.23.45 / LICENSE-MIT +Original notice: rustls 0.23.46 / LICENSE-MIT Original notice: rustls-pemfile 2.2.0 / LICENSE-MIT Copyright (c) 2016 Joseph Birr-Pixton @@ -18746,6 +20273,31 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: httpdate 1.0.3 +Original notice: httpdate 1.0.3 / LICENSE-MIT + +Copyright (c) 2016 Pyfisch + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ======================================================================== License: MIT Used by: xkbcommon 0.8.0 @@ -18869,6 +20421,37 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: multimap 0.10.1 +Original notice: multimap 0.10.1 / LICENSE-MIT + +Copyright (c) 2016 multimap developers + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + ======================================================================== License: MIT Used by: indexmap 2.14.2 @@ -19282,6 +20865,37 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: fnv 1.0.7 +Original notice: fnv 1.0.7 / LICENSE-MIT + +Copyright (c) 2017 Contributors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + ======================================================================== License: MIT Used by: globwalk 0.8.1, memoffset 0.9.1 @@ -19490,6 +21104,37 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: h2 0.4.19 +Original notice: h2 0.4.19 / LICENSE + +Copyright (c) 2017 h2 authors + +Permission is hereby granted, free of charge, to any +person obtaining a copy of this software and associated +documentation files (the "Software"), to deal in the +Software without restriction, including without +limitation the rights to use, copy, modify, merge, +publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software +is furnished to do so, subject to the following +conditions: + +The above copyright notice and this permission notice +shall be included in all copies or substantial portions +of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF +ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED +TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A +PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT +SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY +CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR +IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +DEALINGS IN THE SOFTWARE. + ======================================================================== License: MIT Used by: http 1.5.0 @@ -19869,6 +21514,35 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: logos 0.16.1, logos-codegen 0.16.1, logos-derive 0.16.1 +Original notice: logos 0.16.1 / LICENSE-MIT +Original notice: logos-codegen 0.16.1 / LICENSE-MIT +Original notice: logos-derive 0.16.1 / LICENSE-MIT + +Copyright (c) 2018 Maciej Hirsz + +The MIT License (MIT) + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ======================================================================== License: MIT Used by: kurbo 0.11.3, kurbo 0.13.1, polycool 0.4.0 @@ -21709,9 +23383,9 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: argon2 0.6.0, crypto-bigint 0.7.5, phc 0.6.1, pkcs5 0.8.1, scrypt 0.12.0, sec1 0.8.1, spki 0.8.0, ssh-encoding 0.3.0, ssh-key 0.7.0-rc.11 +Used by: argon2 0.6.0, crypto-bigint 0.7.11, phc 0.6.1, pkcs5 0.8.1, scrypt 0.12.0, sec1 0.8.1, spki 0.8.0, ssh-encoding 0.3.0, ssh-key 0.7.0-rc.11 Original notice: argon2 0.6.0 / LICENSE-MIT -Original notice: crypto-bigint 0.7.5 / LICENSE-MIT +Original notice: crypto-bigint 0.7.11 / LICENSE-MIT Original notice: phc 0.6.1 / LICENSE-MIT Original notice: pkcs5 0.8.1 / LICENSE-MIT Original notice: scrypt 0.12.0 / LICENSE-MIT @@ -22667,8 +24341,8 @@ SOFTWARE. ======================================================================== License: MIT -Used by: ctutils 0.4.2 -Original notice: ctutils 0.4.2 / LICENSE-MIT +Used by: ctutils 0.4.8 +Original notice: ctutils 0.4.8 / LICENSE-MIT Copyright (c) 2025-2026 The RustCrypto Project Developers @@ -23558,6 +25232,34 @@ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: tonic 0.14.7, tonic-build 0.14.7 +Original notice: tonic 0.14.7 / LICENSE +Original notice: tonic-build 0.14.7 / LICENSE +Original notice: tonic-prost-build@0.14.7 / upstream/grpc-grpc-rust-license-5c686e0f7978.txt +Source: https://raw.githubusercontent.com/grpc/grpc-rust/818532da3abfa6068b0855f483e3c627d9e14640/LICENSE + +Copyright 2019 gRPC authors. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ======================================================================== License: MIT Used by: x11rb 0.13.2, x11rb-protocol 0.13.2 @@ -24712,8 +26414,8 @@ express Statement of Purpose. ======================================================================== License: ISC -Used by: rustls-webpki 0.103.15 -Original notice: rustls-webpki 0.103.15 / LICENSE +Used by: rustls-webpki 0.103.16 +Original notice: rustls-webpki 0.103.16 / LICENSE Except as otherwise noted, this project is licensed under the following (ISC-style) terms: @@ -26640,6 +28342,17 @@ necessary. Here is a sample; alter the names: That's all there is to it! +======================================================================== +Original notice: petgraph 0.8.3 / assets/images/LICENSE.md + +Graphosaurus (c) by the petgraph project. + +Graphosaurus is licensed under a +Creative Commons Attribution-ShareAlike 4.0 International License. + +You should have received a copy of the license along with this +work. If not, see . + ======================================================================== Original notice: gpui-kit-assets 0.7.0 / LICENSE-LUCIDE @@ -26689,7 +28402,7 @@ SOFTWARE. ======================================================================== Original notice: hyper-rustls 0.27.10 / LICENSE-ISC -Original notice: rustls 0.23.45 / LICENSE-ISC +Original notice: rustls 0.23.46 / LICENSE-ISC Original notice: rustls-pemfile 2.2.0 / LICENSE-ISC ISC License (ISC) @@ -26731,6 +28444,7 @@ Redistributions of this Work must include all inherited and new license notices, Original notice: unicode-id 0.3.7 / COPYRIGHT Original notice: unicode-properties 0.1.4 / COPYRIGHT Original notice: unicode-segmentation 1.13.3 / COPYRIGHT +Original notice: unicode-width 0.1.14 / COPYRIGHT Original notice: unicode-width 0.2.2 / COPYRIGHT Original notice: unicode-xid 0.2.6 / COPYRIGHT @@ -26994,205 +28708,232 @@ SOFTWARE. ======================================================================== License: MIT -Used by: notify-rust 4.18.1 -Original notice: notify-rust 4.18.1 / LICENSE-MIT - -MIT License - -Copyright (c) 2017 Hendrik Sollich - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -======================================================================== -License: MIT -Used by: imagesize 0.13.0, imagesize 0.14.0 -Original notice: imagesize 0.13.0 / LICENSE -Original notice: imagesize 0.14.0 / LICENSE +Used by: notify-rust 4.18.1 +Original notice: notify-rust 4.18.1 / LICENSE-MIT + +MIT License + +Copyright (c) 2017 Hendrik Sollich + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +License: MIT +Used by: imagesize 0.13.0, imagesize 0.14.0 +Original notice: imagesize 0.13.0 / LICENSE +Original notice: imagesize 0.14.0 / LICENSE + +MIT License + +Copyright (c) 2017 Maiddog + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +License: MIT +Used by: static_assertions 1.1.0 +Original notice: static_assertions 1.1.0 / LICENSE-MIT + +MIT License + +Copyright (c) 2017 Nikolai Vazquez + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +License: MIT +Used by: darling 0.24.1, darling_core 0.24.1, darling_macro 0.24.1 +Original notice: darling 0.24.1 / LICENSE +Original notice: darling_core 0.24.1 / LICENSE +Original notice: darling_macro 0.24.1 / LICENSE + +MIT License + +Copyright (c) 2017 Ted Driggs + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +License: MIT +Used by: tiff 0.11.3 +Original notice: tiff 0.11.3 / LICENSE + +MIT License + +Copyright (c) 2018 PistonDevelopers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +License: MIT +Used by: crc32fast 1.5.2 +Original notice: crc32fast 1.5.2 / LICENSE-MIT + +MIT License + +Copyright (c) 2018 Sam Rijs, Alex Crichton and contributors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +Original notice: taffy@0.13.0 / upstream/DioxusLabs-taffy-license-f97daf1a0124.txt +Source: https://raw.githubusercontent.com/DioxusLabs/taffy/45a56299d366ddb383e593a1f0372158d00e8530/LICENSE + +MIT License + +Copyright (c) 2018 Visly Inc. +Copyright (c) 2026 Taffy Authors + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +License: MIT +Used by: filedescriptor 0.8.3, portable-pty 0.9.0 +Original notice: filedescriptor 0.8.3 / LICENSE.md +Original notice: portable-pty 0.9.0 / LICENSE.md + +MIT License + +Copyright (c) 2018 Wez Furlong + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + +======================================================================== +License: MIT +Used by: tokio-socks 0.5.3 +Original notice: tokio-socks 0.5.3 / LICENSE MIT License -Copyright (c) 2017 Maiddog - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -======================================================================== -License: MIT -Used by: static_assertions 1.1.0 -Original notice: static_assertions 1.1.0 / LICENSE-MIT - -MIT License - -Copyright (c) 2017 Nikolai Vazquez - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -======================================================================== -License: MIT -Used by: darling 0.24.1, darling_core 0.24.1, darling_macro 0.24.1 -Original notice: darling 0.24.1 / LICENSE -Original notice: darling_core 0.24.1 / LICENSE -Original notice: darling_macro 0.24.1 / LICENSE - -MIT License - -Copyright (c) 2017 Ted Driggs - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -======================================================================== -License: MIT -Used by: tiff 0.11.3 -Original notice: tiff 0.11.3 / LICENSE - -MIT License - -Copyright (c) 2018 PistonDevelopers - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -======================================================================== -License: MIT -Used by: crc32fast 1.5.2 -Original notice: crc32fast 1.5.2 / LICENSE-MIT - -MIT License - -Copyright (c) 2018 Sam Rijs, Alex Crichton and contributors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -======================================================================== -Original notice: taffy@0.13.0 / upstream/DioxusLabs-taffy-license-f97daf1a0124.txt -Source: https://raw.githubusercontent.com/DioxusLabs/taffy/45a56299d366ddb383e593a1f0372158d00e8530/LICENSE - -MIT License - -Copyright (c) 2018 Visly Inc. -Copyright (c) 2026 Taffy Authors - -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: - -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. - -======================================================================== -License: MIT -Used by: filedescriptor 0.8.3, portable-pty 0.9.0 -Original notice: filedescriptor 0.8.3 / LICENSE.md -Original notice: portable-pty 0.9.0 / LICENSE.md - -MIT License - -Copyright (c) 2018 Wez Furlong +Copyright (c) 2018 Yilin Chen Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal @@ -28244,7 +29985,7 @@ SOFTWARE. ======================================================================== SPDX reference license terms: MIT -Used by: accesskit 0.24.1, accesskit_atspi_common 0.19.1, accesskit_consumer 0.38.0, accesskit_macos 0.26.3, accesskit_unix 0.22.1, accesskit_windows 0.34.0, block 0.1.6, block2 0.6.2, chrono 0.4.45, delegate 0.13.5, dispatch2 0.3.1, gpu-descriptor 0.3.2, gpu-descriptor-types 0.2.0, granit-parser 1.3.0, harfrust 0.5.2, leak 0.1.2, leaky-cow 0.1.1, libm 0.2.16, lyon 1.0.19, lyon_algorithms 1.0.21, lyon_geom 1.0.19, lyon_path 1.0.19, lyon_tessellation 1.0.22, mac 0.1.1, mac-notification-sys 0.6.15, malloc_buf 0.0.6, md5 0.8.1, objc-sys 0.3.5, objc2 0.5.2, objc2 0.6.4, objc2-app-kit 0.2.2, objc2-app-kit 0.3.2, objc2-cloud-kit 0.3.2, objc2-core-data 0.3.2, objc2-core-foundation 0.3.2, objc2-core-graphics 0.3.2, objc2-core-image 0.3.2, objc2-core-location 0.3.2, objc2-core-media 0.3.2, objc2-core-text 0.3.2, objc2-core-video 0.3.2, objc2-encode 4.1.0, objc2-foundation 0.2.2, objc2-foundation 0.3.2, objc2-metal 0.3.2, objc2-quartz-core 0.3.2, objc2-screen-capture-kit 0.3.2, objc2-user-notifications 0.3.2, pathfinder_geometry 0.5.1, pathfinder_simd 0.5.6, profiling 1.0.18, profiling-procmacros 1.0.18, pulp-wasm-simd-flag 0.1.1, rust-i18n-macro 4.2.4, rust-i18n-support 4.2.4, seahash 4.1.0, svg_fmt 0.4.5, taffy 0.13.0, tauri-winrt-notification 0.8.1, tree-sitter-bash 0.23.3, tree-sitter-cpp 0.23.4, tree-sitter-css 0.23.2, tree-sitter-diff 0.1.0, tree-sitter-go 0.23.4, tree-sitter-html 0.23.2, tree-sitter-java 0.23.5, tree-sitter-javascript 0.23.1, tree-sitter-json 0.24.8, tree-sitter-python 0.23.6, tree-sitter-toml-ng 0.7.0, tree-sitter-typescript 0.23.2, windows 0.58.0, windows 0.62.2, windows-collections 0.3.2, windows-core 0.58.0, windows-core 0.62.2, windows-future 0.3.2, windows-implement 0.58.0, windows-implement 0.60.2, windows-interface 0.58.0, windows-interface 0.59.3, windows-link 0.1.3, windows-link 0.2.1, windows-numerics 0.3.1, windows-registry 0.4.0, windows-registry 0.6.1, windows-result 0.2.0, windows-result 0.3.4, windows-result 0.4.1, windows-strings 0.1.0, windows-strings 0.3.1, windows-strings 0.5.1, windows-sys 0.48.0, windows-sys 0.52.0, windows-sys 0.59.0, windows-sys 0.61.2, windows-targets 0.48.5, windows-targets 0.52.6, windows-targets 0.53.5, windows-threading 0.2.1, windows-version 0.1.7, windows_x86_64_gnu 0.48.5, windows_x86_64_gnu 0.52.6, windows_x86_64_gnu 0.53.1, windows_x86_64_msvc 0.48.5, windows_x86_64_msvc 0.52.6, windows_x86_64_msvc 0.53.1, xim-ctext 0.3.0, xim-parser 0.2.2, zune-core 0.4.12, zune-inflate 0.2.54, zune-jpeg 0.4.21 +Used by: accesskit 0.24.1, accesskit_atspi_common 0.19.1, accesskit_consumer 0.38.0, accesskit_macos 0.26.3, accesskit_unix 0.22.1, accesskit_windows 0.34.0, block 0.1.6, block2 0.6.2, chrono 0.4.45, delegate 0.13.5, dispatch2 0.3.1, gpu-descriptor 0.3.2, gpu-descriptor-types 0.2.0, granit-parser 1.3.0, harfrust 0.5.2, leak 0.1.2, leaky-cow 0.1.1, libm 0.2.16, lyon 1.0.19, lyon_algorithms 1.0.21, lyon_geom 1.0.19, lyon_path 1.0.19, lyon_tessellation 1.0.22, mac 0.1.1, mac-notification-sys 0.6.15, malloc_buf 0.0.6, md5 0.8.1, objc-sys 0.3.5, objc2 0.5.2, objc2 0.6.4, objc2-app-kit 0.2.2, objc2-app-kit 0.3.2, objc2-cloud-kit 0.3.2, objc2-core-data 0.3.2, objc2-core-foundation 0.3.2, objc2-core-graphics 0.3.2, objc2-core-image 0.3.2, objc2-core-location 0.3.2, objc2-core-media 0.3.2, objc2-core-text 0.3.2, objc2-core-video 0.3.2, objc2-encode 4.1.0, objc2-foundation 0.2.2, objc2-foundation 0.3.2, objc2-metal 0.3.2, objc2-quartz-core 0.3.2, objc2-screen-capture-kit 0.3.2, objc2-user-notifications 0.3.2, pathfinder_geometry 0.5.1, pathfinder_simd 0.5.6, profiling 1.0.18, profiling-procmacros 1.0.18, pulp-wasm-simd-flag 0.1.1, rust-i18n-macro 4.2.4, rust-i18n-support 4.2.4, seahash 4.1.0, svg_fmt 0.4.5, taffy 0.13.0, tauri-winrt-notification 0.8.1, tonic-prost-build 0.14.7, tree-sitter-bash 0.23.3, tree-sitter-cpp 0.23.4, tree-sitter-css 0.23.2, tree-sitter-diff 0.1.0, tree-sitter-go 0.23.4, tree-sitter-html 0.23.2, tree-sitter-java 0.23.5, tree-sitter-javascript 0.23.1, tree-sitter-json 0.24.8, tree-sitter-python 0.23.6, tree-sitter-toml-ng 0.7.0, tree-sitter-typescript 0.23.2, windows 0.58.0, windows 0.62.2, windows-collections 0.3.2, windows-core 0.58.0, windows-core 0.62.2, windows-future 0.3.2, windows-implement 0.58.0, windows-implement 0.60.2, windows-interface 0.58.0, windows-interface 0.59.3, windows-link 0.1.3, windows-link 0.2.1, windows-numerics 0.3.1, windows-registry 0.4.0, windows-registry 0.6.1, windows-result 0.2.0, windows-result 0.3.4, windows-result 0.4.1, windows-strings 0.1.0, windows-strings 0.3.1, windows-strings 0.5.1, windows-sys 0.48.0, windows-sys 0.52.0, windows-sys 0.59.0, windows-sys 0.61.2, windows-targets 0.48.5, windows-targets 0.52.6, windows-targets 0.53.5, windows-threading 0.2.1, windows-version 0.1.7, windows_x86_64_gnu 0.48.5, windows_x86_64_gnu 0.52.6, windows_x86_64_gnu 0.53.1, windows_x86_64_msvc 0.48.5, windows_x86_64_msvc 0.52.6, windows_x86_64_msvc 0.53.1, xim-ctext 0.3.0, xim-parser 0.2.2, zune-core 0.4.12, zune-inflate 0.2.54, zune-jpeg 0.4.21 MIT License @@ -28298,8 +30039,9 @@ SOFTWARE. ======================================================================== License: MIT -Used by: tokio 1.53.1, tokio-util 0.7.19 -Original notice: tokio 1.53.1 / LICENSE +Used by: tokio 1.54.1, tokio-stream 0.1.19, tokio-util 0.7.19 +Original notice: tokio 1.54.1 / LICENSE +Original notice: tokio-stream 0.1.19 / LICENSE Original notice: tokio-util 0.7.19 / LICENSE MIT License @@ -29822,7 +31564,7 @@ DEALINGS IN THE SOFTWARE. ======================================================================== License: MIT -Used by: adler2 2.0.1, anyhow 1.0.104, async-channel 2.5.0, async-executor 1.14.0, async-fs 2.2.0, async-io 2.6.0, async-lock 3.4.2, async-net 2.0.0, async-process 2.5.0, async-recursion 1.1.1, async-signal 0.2.14, async-task 4.7.1, async-trait 0.1.92, atomic-waker 1.1.2, blocking 1.7.0, concurrent-queue 2.5.0, curve25519-dalek-derive 0.1.1, displaydoc 0.2.7, dyn-clone 1.0.20, endi 1.1.1, enumn 0.1.14, erased-serde 0.4.10, event-listener 5.4.2, event-listener-strategy 0.5.4, fastrand 2.5.0, flume 0.12.0, futures-lite 2.6.1, group 0.14.0, home 0.5.12, inventory 0.3.24, itoa 1.0.18, khronos-egl 6.0.0, lazy_static 1.5.1, linux-raw-sys 0.12.1, linux-raw-sys 0.4.15, minimal-lexical 0.2.1, once_cell 1.21.4, ordered-stream 0.2.0, parking 2.2.1, paste 1.0.15, pin-project 1.1.13, pin-project-internal 1.1.13, pin-project-lite 0.2.17, piper 0.2.5, polling 3.11.0, prettyplease 0.2.37, proc-macro-crate 3.5.0, proc-macro2 1.0.107, process-wrap 9.1.1, quote 1.0.47, ref-cast 1.0.27, ref-cast-impl 1.0.27, rsa 0.10.0-rc.18, rustc-hash 1.1.0, rustc-hash 2.1.3, rustix 0.38.44, rustix 1.1.5, rustix-openpty 0.2.0, rustversion 1.0.23, semver 1.0.28, serde 1.0.229, serde-saphyr 1.3.0, serde_bytes 0.11.19, serde_core 1.0.229, serde_derive 1.0.229, serde_derive_internals 0.30.0, serde_json 1.0.151, serde_json_lenient 0.2.4, serde_repr 0.1.21, smol 2.0.2, smol_str 0.3.6, syn 1.0.109, syn 2.0.119, syn 3.0.6, thiserror 1.0.69, thiserror 2.0.21, thiserror-impl 1.0.69, thiserror-impl 2.0.21, typeid 1.0.3, unicode-ident 1.0.26, utf-8 0.7.6, waker-fn 1.2.0, zbus-lockstep 0.5.2, zbus-lockstep-macros 0.5.2, zed-font-kit 0.14.1-zed, zmij 1.0.23, zvariant_utils 4.2.0 +Used by: adler2 2.0.1, anyhow 1.0.104, async-channel 2.5.0, async-executor 1.14.1, async-fs 2.2.0, async-io 2.6.0, async-lock 3.4.2, async-net 2.0.0, async-process 2.5.0, async-recursion 1.1.1, async-signal 0.2.14, async-task 4.7.1, async-trait 0.1.92, atomic-waker 1.1.2, blocking 1.7.0, concurrent-queue 2.5.0, curve25519-dalek-derive 0.1.1, displaydoc 0.2.7, dyn-clone 1.0.20, endi 1.1.1, enumn 0.1.14, erased-serde 0.4.10, event-listener 5.4.2, event-listener-strategy 0.5.4, fastrand 2.5.0, flume 0.12.0, futures-lite 2.6.1, group 0.14.0, home 0.5.12, inventory 0.3.24, itoa 1.0.18, khronos-egl 6.0.0, lazy_static 1.5.1, linux-raw-sys 0.12.1, linux-raw-sys 0.4.15, minimal-lexical 0.2.1, once_cell 1.21.4, ordered-stream 0.2.0, parking 2.2.1, paste 1.0.15, pin-project 1.1.13, pin-project-internal 1.1.13, pin-project-lite 0.2.17, piper 0.2.5, polling 3.11.0, prettyplease 0.2.37, proc-macro-crate 3.5.0, proc-macro2 1.0.107, process-wrap 9.1.1, prost-reflect 0.16.5, protox 0.10.0, protox-parse 0.10.0, quote 1.0.47, ref-cast 1.0.27, ref-cast-impl 1.0.27, rsa 0.10.0-rc.18, rustc-hash 1.1.0, rustc-hash 2.1.3, rustix 0.38.44, rustix 1.1.5, rustix-openpty 0.2.0, rustversion 1.0.23, semver 1.0.28, serde 1.0.229, serde-saphyr 1.3.0, serde_bytes 0.11.19, serde_core 1.0.229, serde_derive 1.0.229, serde_derive_internals 0.30.0, serde_json 1.0.151, serde_json_lenient 0.2.4, serde_repr 0.1.21, smol 2.0.2, smol_str 0.3.6, syn 1.0.109, syn 2.0.119, syn 3.0.6, thiserror 1.0.69, thiserror 2.0.21, thiserror-impl 1.0.69, thiserror-impl 2.0.21, typeid 1.0.3, unicode-ident 1.0.26, utf-8 0.7.6, waker-fn 1.2.0, zbus-lockstep 0.5.2, zbus-lockstep-macros 0.5.2, zed-font-kit 0.14.1-zed, zmij 1.0.23, zvariant_utils 4.2.0 License: MIT Used by: allocator-api2 0.2.21, glow 0.17.0 Original notice: accesskit@0.24.1 / upstream/AccessKit-accesskit-license-mit-23f18e03dc49.txt @@ -29833,7 +31575,7 @@ Original notice: adler2 2.0.1 / LICENSE-MIT Original notice: allocator-api2 0.2.21 / LICENSE-MIT Original notice: anyhow 1.0.104 / LICENSE-MIT Original notice: async-channel 2.5.0 / LICENSE-MIT -Original notice: async-executor 1.14.0 / LICENSE-MIT +Original notice: async-executor 1.14.1 / LICENSE-MIT Original notice: async-fs 2.2.0 / LICENSE-MIT Original notice: async-io 2.6.0 / LICENSE-MIT Original notice: async-lock 3.4.2 / LICENSE-MIT @@ -29880,6 +31622,9 @@ Original notice: prettyplease 0.2.37 / LICENSE-MIT Original notice: proc-macro-crate 3.5.0 / LICENSE-MIT Original notice: proc-macro2 1.0.107 / LICENSE-MIT Original notice: process-wrap 9.1.1 / LICENSE-MIT +Original notice: prost-reflect 0.16.5 / LICENSE-MIT +Original notice: protox 0.10.0 / LICENSE-MIT +Original notice: protox-parse 0.10.0 / LICENSE-MIT Original notice: quote 1.0.47 / LICENSE-MIT Original notice: ref-cast 1.0.27 / LICENSE-MIT Original notice: ref-cast-impl 1.0.27 / LICENSE-MIT @@ -58881,6 +60626,33 @@ Legal Terms --- end of FTL.TXT --- +======================================================================== +License: MIT +Used by: pulldown-cmark 0.13.6 +Original notice: pulldown-cmark 0.13.6 / LICENSE + +The MIT License + +Copyright 2015 Google Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + ======================================================================== Original notice: lyon@1.0.19, lyon_geom@1.0.19, lyon_path@1.0.19 / upstream/nical-lyon-license-mit-80f275e90d79.txt Source: https://raw.githubusercontent.com/nical/lyon/56219c95c658d4f39de7b190d98e57d5773ba241/LICENSE-MIT @@ -59425,6 +61197,32 @@ COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +Bundled component: devicon/LICENSE +Original notice: / supplemental/devicon/LICENSE +Source: https://raw.githubusercontent.com/devicons/devicon/54cfe13ac10eaa1ef817a343ab0a9437eb3c2e08/LICENSE + +The MIT License (MIT) + +Copyright (c) 2015 konpa + +Permission is hereby granted, free of charge, to any person obtaining a copy of +this software and associated documentation files (the "Software"), to deal in +the Software without restriction, including without limitation the rights to +use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of +the Software, and to permit persons to whom the Software is furnished to do so, +subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS +FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR +COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER +IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN +CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. + ======================================================================== License: MIT Used by: gif 0.13.3, gif 0.14.2 @@ -59645,6 +61443,33 @@ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +======================================================================== +License: MIT +Used by: hyper-timeout 0.5.2 +Original notice: hyper-timeout 0.5.2 / LICENSE-MIT + +The MIT License (MIT) + +Copyright (c) 2016 The weldr Project Developers + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. + ======================================================================== License: MIT Used by: shellexpand 3.1.2 diff --git a/assets/icons/nocterm-256.png b/assets/icons/nocterm-256.png index d4fe86ea0cd1bcc848adba16aabe88d63f74cc82..c9513f6d2630b020d8071b9ee40b1720f8deb16f 100644 GIT binary patch literal 10951 zcmb7KcQ{;I*FSnEg6JVkkVNk-Mu`?Zh~A<{w5U-=ixMFi2_i~FLUclOMu{?N5G7GY z8AL=Ibr^idy}9rE-ao&`cxKLX_S$=`y;s}6wVvtgs#B4(kOKffrKxe}9suBhKk)!k zV(`oI@kb}{i}-<-`W@i>;_q!sVJZM{0-AS}i~_SZvIDZ{jSuP&b;YtefRicG;5s$4 z(v2>0R8rj~SDd6g-q4ne@pzl2e(IAtl0M z>?NBgBx7P@vtzSRV(KBjEMxZZBP{cD=Eb$ij^kw@)T`D-;116M z-W-ZeI+6p;X+AjeZF92H7%|*C*AOxLQ72*d)X1;L#wGVRM9b`1Q%i`#saV8638i8+ zZ9fuDdFhPbHPVSLe)#p*dC9oM>Q`|p`4^NT4jQnr{b3?(beJPsf`|s3@c!YNvvwqZ zf3hPPoc^rB7hua3>rO9L^3yaP@B2JqG(P|rBYOMk)4igD;zFN9y~Vbj%z{%vCdzB{ zvN#0s_czM(hj%eOT6|gjbeMHjID{`AKlYuX0bBBQS15_X1Nh!drtxN7_=jt=$Vb`} zjNy4I>IW3wFtTt8m3IAGVVrTc!Wnmtx|4C}dz`lhxVJ z?D$T+q#goQ14*y)S(|%m9u$QtE4`(wNxMZYC~I@Jm!Fo`s#LeaU*gOd1)`(_guwoc2Bd$5KAme6YT;NTA74D*!~1(D-|O_YqsdsZ|WG?qG3La+)=FaH z6_7$-NsvNJS-yoD{$^-Thw1gZI3|N|)@B0nm5z)^mC5{e4AF>BfQro`W#qKc~49ng6XvOzRO

u`3>);j)H=MD|xotcRYdWMCvY5*CskIudZIPc5JE2 z_;?lL)&DA+m_S&DtuSeNzwt<;&WYd2!EbX10zXYT6iLWq*Y7?qh;4gTETdw{)hp(=*{vex@SEs(Igp~<+&cFlkv zIxqk^XdC zIrx-JUfPV`uT{te{?v~s9_4Q7{-X{x)wqRo4fxZQ!8gIL=|aoo87lWk6}@Gy?i`0e z5DR}ysagpJfX-1NcQ}eOSTTTw!1O+F^QKyYv9VtQ!edl79L>1?w^U*lXqu({puFN= z^{t_?36t5nm!6Zi$$TQ0H#Ibz-gXiCkJ3AXE(qLTVm2JCr1ewXdw{Zh%Eyk6UN)X* zTYU6yTXe4NOKS8aip={u5_+6|ZXNItuapmT&fS$;qOEq|m zqAfX(d-(pizGr5gP0b%`u}tRh3m`4N_PJCqSIPNT(rD}&QMg;i%KotiEbot$KB{NJ z))kD&@85KB)7d}7`}mPb;m*>px3=@!A+Zod&OgI0j)E|XwCGpz*=a1Lcv_yTwg7NV zBmJv+DA%9kT6u?CsO+Se$7m%uy0VOsD>^!U=x7_JBE*Lv%Kw?i4EBuy81l+_Z9|rM zA1H45wU%f5kwIXjtneSE(>VYm2|{D4&uenFy*Ii=yC0b2&9mg9Y2wQL6fr#;=3&YU z4v;Y4x2MZAo;xR}U&R%WrW2+EH-Q9#SM6^#@Hnwc{$`CBO**# z)^A)+Eyt@|KvDomjyktfvgp(l+(^WC0qD^J$x|Rfe8K{9gE9Q9Gv@Q8Q5j?h3;owH zA?UAi0;!DI&xlaY(9aH%9(IxG5*&e=Jke% zmlS|~b0N#&3dg^OXu$w|2?lL7J^L1^6@L+h z=Y6!RfrQPJ@AJ~@s$tYgR_by4xehs)$BRX|_<(9G`@atBa9Mp;J<0pN!=pP?8Q$Fb zw5#|@Kq_wM^50_as=$ExtSExlLO|B8@>TViscteGo|)n>#VS?}n7xi7<~cEBt%nV3 zU;;eyCI{MzN*ItFLVq*?fhGZlB!mmbS_n;BWrY8*b~^OxTE*R%G(zvxkq2L4S?KJah)n*;x*zkBcB^bfULh(RXf zAXuUkPAnCr2Gi~YG5yp-`5Q>j8Zr&oNI2xw0f7@P0g`cKT$-?U(8LG`M74h<5l}pm z$a#sD`auXmxG2J5kJVwOKD?aB3f>D1*1)hb>Gx$=0vfQ}e~XH03)fZ>5n=!bm^Cgt zQ@V^7Gmuk|>zf)8db$fdbZeQa4iGYu7>d`0AkK2CO<@@(e-V#f3wwTL(7;TFyz>Qf zd<4O9zT4!GSA(Ugu#}u?OBh?uHz^5XYUZKy zL&Q0=o+9QA*LfUm&m#Oe@4^?ln|XYTepX}R{1yi?IWVVdb7AKyUxs!yFzd6g_(>Rg_i#_7&zpS`u5)7DZ$U?`LECzX=D2-K zC@;HuEl@rOEu($Mz+pzphgcBt`*M{c95sRAF8h1BJci}Ky%Z8Es|6{7Zl(g1KgAFm z^u7&%^bE$P6h$j(z^ZsQrayNs`J9iV53y@cMPj~JZ>l0-p>$FZY_&4X;ep*HxJ@aU zJ!SRgn+Q1k3ex92UV49Yn1VQ~M_tnzF(m8Z*y6XKq{bm1H9VU*3R@R{izz*}LBQ-}l-pW=98}?kwq#a)r1@qNRj6^VIHJ zlGpfxE1&{X4Y<|c`mNtbvN~M?8Lb|cO+`ydUJU7_xy49)4Vx&17cMr;R#;&d!z|^W zuvrU1RH@xZ(v)2J`z7`8qR3;kS9YRUe_O=qNb{53_Zy&}NGyHHai1TQlGXFF$3%k< zZwZvAB)an+;!`-6NWM>Y>iT5WoS-W+01> zZ2%8O>IdW#vwG~o#y-*ar-6Dra9lUb2Q65J3}|hhrDX5WB;k_o=IazSWFHcBP1vYO zj`!9QH)osj4ST5d*XX&)(Ueg?UtM#%{4RU=?5y`gWK@UIE-lX0T9FpkBc%4 z;N!pfjqwp({PX7`$6x4#y%!0yyh5&%lV66GhkR^g(Mk&mH?a<-$`w4Jv=lpd17?`K z)js%=syY2MKs^7tmMz^2Rtfx`M3%g!P;Ud;p-kbc&N@s<`|DEkttT8V;^%Kc*D;&T z1MvZ=h>*Dfdz-Tqdc$FCdIP~mhwj4=#HMcQgIv%ERwW9vgGA@dal}CfKXWSCkrhpb zR#Qv#Z7Uc#HgaO>pGluS&HWtxK$j`yNu56SEGm}T4%Heu)#xYJio@LcJ=dac3&w77 zb~)d+l8WZ=U7>nXW8-!EgH&9j+_=sPMR?*ehs0y)q1V!Qyom}TYtaJ0)F;PVbGYFL z&3l-VSd;J1rfHBINud4)o;46jFAY39+HA_+pVkBgAW!*j2)yZo*1Ti$JUx_3YQpl>Y=BIs=;?beSkMqdQH;Azi+IqW9rAWz5p$wbh52rH%ZGhC5#!kbH^S@L#I6@a%X`F{I_%8VagozfcpQy&jZGO1p)bBNSKE zibuP@w>HK)X>-UpP`}#x@wN^kHbI1vRhGUmOO&ODvP?1Q>g+lNDezSJrvK*5vt^sm z#5xp=Q44xj+UDFX&KIKE43VM5{N$E}duY zW^87di8(Xn#zJFBW0aX>9*Vb~FT^$qR3VJInT8bBwQ<8wfyAyT7wUJ z?7R72CFLK38BVlrV|vR2mrz1W3nb1INE(1$wx$QQmHFau<k}v=2@)r46gr*zMpv zNJZ4wGg0IOv9Zhf3(J$=#y#mXHX@|-Ixt%@nE8^O@~S7X!{Ygz;uGxrOU&WVIriss zxPaL&Jl?$q*p#_wrQf_zU?yP8>gRZM?1Dku+Y-{Rj*+C~SPwSnjiGEg8W1kG)yS$RkM&jSc2IxEs8&`~R8=Yjo@Iw7BB@#`039m< zZj=QqJReh^5~FTUWLN+$%v$bVv%Ib6)srpuk@Q=htN9nVSR^A}zKjp9o@bMIPylOp z2A*y@NBDIq-21+lZsn!7@Kd1e?7k2HING1I;kK@GNeQ`$G5Yzfh`UkT`b3yyIktf! z${`j9czrWaXSw?7l{kGwrx%drGvhru8~FGvGU0_l>hJGOjN{`evAB(CvoO3mPn3e+ zd7lo}Zmxs#J2w<~nKtFquJcrga?Z>BOPtJV25dEWWj%D?`$;!C8n73wj?TadHvtY&IAHtq z5DJ;`Zhqm?(WG^eMYxs|tG9`k5c zc)+^KR;PC^m(#bvRq3)bee@V>LOVTCW|5?%@}lciYDN35Q1dsmjjI^w97xF=fNB?Gfe`jOmv0H=bCq8W<=L3Z_%^E1clAV;VX9)_QW^zE`E7 zWSfHKFNwXefysnJaSM7Mi^>wen^=XCzLE57Z_=9US=wE(@9a)=nXVQ1@%m_d-UReq zg^cnPQ>L(_o~YB4gBiPz`Lg#E@OujoxCI`I+DBbr>Xlko$82Kb*R{YMam?{5%IC#& z0V=vM2PRn^B}Qv(EQ0+7wclzD)%m@gcy_)`GM6uMJoA_L?f$fcyr-&nE1@2@& z4z>vYaFMlz5PmIi?^&~6;0_A3_I+gG`_}@dtdRQV#svKGZnDGzFRROh1rpIoiXn`a~1gb z`M=)2+qB!8)#EkXa;W~uK4D=<&ZF}~SUsQp(kHkr-_vZ=#+Tx8)Z*;nT#vdIRLz~P zkQDHm>7~s$nL>PRZa5O?l+tNp(1Y$;E~-f$_1l=6$0x2MbdU?;jKKPGI)rSg`$aoL zSfe(|A<1kYOMP{5GuTP>Vx@99&$_Oqq5D4DU$6079&xSQmf?V&H#ga-&-T1?9^MSJ zTfnbz9a9S!+Gmwqd~c{*@kUghS~8h~hap@%2j5gE%xud#L+X&$(toYXwZT|?>}5Jl z#Pmi-VZTDs)Y-^r(nxEl+}@4#?5e5{1YpR+T#NGFQBA#cPsOz5mxas2PQ$2^!}G}N zS_*CTRfYML`Y8CiiM0fg9O7I7M)0PWrpc1<4w)qVVqjgHaNyr zC^;tGLnSpAc?dlcwqLr-do_uqZZ3q*`s7f{h28k5ZtI9T-3k_6-UMY7fKlXzo~h!9 zCE+OkQ7=N$7i?a zV!T-qAGDzDLI~VF#OZ2oKz?H*OAkkwWycYCJXM9cF+Myw(S1v5d8MU{nNn^sR!uN9 z;jcz`$Wok{7wy+$1WwBGkWstA>G9(GCmjjFo1Ize97$7>57DH}mBNS>x~Z06sqVzw zIF7JK8B)n2^IeQNiLdQUX}{eD%C${p@C=KoM=C@~pDiJtolI*8jn~h773n|N@2_gf{tAgP9%RV?6Fgw=> zF_t4)8?PoQK*6-WI6L>Jl?7s7g%!RG_|{8nikY*JUP`D^_V$0tF+m@`7cvYzJ+NRk z4&>6_T^Y0|Upm;3+a7A#a(txydU5eNPnRWq&hF2d3@nlAjhi>W*!!G4(-LYiH3@F& zquHfU3_^4GYZJX-eU; zS?PtReQ|ZW>lG#rk2RQ=UK;Ag9~o_cfonP-#M%7Jx+Q2a>zVU7LN^`f)rTg_o?*6K+S6PLO z`rKDn7ABf$Xw@y(l2K0u`9s;J9)V*@Y1O|J7ImI>;D-)(t+MQkk&K5H>!Z}`> zu?*DXfJ*-jSpx1pz1l~KS=f5FNx>N2z5)Mt&J+q@I$CgOU=`Z?E#!WG(w-1iS|&I= z1UnhY-DlH!#D&Q@w9Mt=zuM$~%2v%{v@{(x*fEel=G&XJCrFjni2G=9OOU>=Lfsw}X78T5_paC11mNt-#DuE9y0vi~K%cX5>GWhHkks z+96Lk7Qx)TBy5nWf0~!iCirwvtwLH?K5rCmn5PG|D#$l}FnR+DVv-kt{qHp*&6P*I z+h6GPRGSr6GRSLQG_p%_9R02qstnq%^Q)i^i{#N}@(hB4C+OL7s(V*vQ~+9P!!i(p z#e0zi#eRF~-01U)M&XFGAMvpMeg~eV?*b7syCKhf))IJwBSqlV>qXmr zC~Vp=%QmVZ9_7u8dIiH**+~h7PaRe8oSFWXExZVF^ zh!7L|WE0D{Gqa188aI|d+iC0EubzF1sbiy+}RF3g+K?oX;2Aai+3=G)7$l4en8zpyK`rj^+#e>^3RTZ)K{P}?#E zZ@On9!A{n*xZT}h=u$Jv=IK^`+Trmjc6IT6LC%C$L?Wxy!Y8P;t4p!BTrW-kDxvsS zLVU0&Q{bB6-|t?3GH3SWNmw{)tEc9TXD7;e24wA2uA8Ydie^ALeJER0zGJVd_9442 zN5B=yM^l%oT0C<_XKpX)8lIRpJZ9j@3cY&9TO)d4_nQmKpH25wk&d*_1_k%IU{YIt z6~7c!X%JvpTkXHGK!Xz-79@N(S2cYmNd;c9A9r`B=|;9+aynnSmLL-<@2@(WI0M?^ZhaBG0FjFk_Y==fB zczylH=XK%39p3Gguq)%k-l!v?bAhpf#dxSy&WK1slp<$%x;2b}=G=!GysGqM4C{bs z&UT;Ha6agy3-%7dRKWXiR*Xuo%t;Ds*Zm{>l!RKRQqmT5wIJ+wVn5(oXgejs@N*wWS$@oKjq@KM5ymYA%U>PHl;JKEI^LM8CP9T%U*2jtRG52mc$-b0 z2?p?DRD^w9h*k!(qwbd_40AG25?2N7vxJ;s83Xq}PugGQyWCFx7xd@a`Qs&9Z7txS ze^%gM>oN9Y>g5CZ6^tw?bCh4QxHLehml$!3&Ws5?Ij}z$I-#YEx0!bf z@^w02UgZ-I`0R~W`5G64+M)!}om|3al=~xSb*V0eFUxu}ZBuU;rEcEJS%qe`CE(i* zCMtqpX&MC4>IFxY64Jg8wF;kdf*|-Vz?6OURPv^L=M={3)&qYtN>tcFzY!8gX zYv_UhJYTBrddks>0l^v&Q|Sb+v1Fym9BUTLasN!g2ZzRpmr~yOXAycN3Ua05f1f!R zDNKmDu&URvegVbMc(h)e>Mep4+`HrpkcLAQ!`l`4D&jwkb7B4LFnyr3e?&{cHHvsU za>8xs;Whz<$N3ttYXj2wq{+C&zv*A8W5l|FA%db8-B0n`+@7vx^J4c8wK40@`?#^R zf6!h{+hOpnW?Flj6f2AQl6(=5t@a$G-+MF}cO1-v-B(S)0f1Q=-fQ>ESIy}`7Txn9 z65fQ0nDwpK=t)LZ%p`aJ<&2n@CLKBrKrg9#oDBXyd~#vA^&N=fBdNSue?pe=D<6&* zTfrgh$?qGDE|?H0JcX6YiOx{x)|`5c>H?&VqA4Pda~)3 z*(IcBmSqYDz(!4EIX=gTWTP=8SG?%ufI~J)5Y+lK|D6YdfM_N=xDHR0txrC4NP07z z0P)MW`Q;`3PO>ZbdWM-W0jU49NTD=|g)0u8BYW_haqSBa)4}Ba>m}^#6GJ0^4(;@F%#l@60nM% zC0ZH?R`ZO$*Ad$eHh55ZNviNgP!l@ueoL>`JI55O#8U55M=Kw5X~5u6qQ_RS!m)^s zcZxd&>Qo8X?*FQ|a_dub?}mTR=HG0SU`M=uo_81MxSeso)CT&&Mk&J6cDET16ZbFg z^B_=UE7uO-6YLHVpM{IT?gRRZ?CpzmaNmBD*dQmga4{ z;u+eLq2eP;J8b$d3D-&PD1qoX=w}>t>aP=EZpBAN;tyBA2!(+Agwb%8HVl6Fc2W3y zD{CX8FZ-q37f}0u28(s;8yWSsZ})6TdCm#G{Kk2F4k_%Nn}vEl8gD$MrB*E`C0rD8 z)BRr>0!6SSL9Yr6>@V=6A-%2>QNxD!)LRZ~;~2XX)zJ~jbC?Rk^C`FoW^^9NWE{B7 zUVJaETS%5QDe<|+zyA!BnpSb~aSP8a`wAwt8fT!TCP1gzpXsF)gBIfB3!mOFzg9K> z9pHy+WQaz>iyXgd-wVtR}RlX|Z`wh3Yz~3kYYWi2eZEGX8hYeh} z)QO@JDpZN(M!+1US^1d&mpvoi(nw)g7va!2 zZ>WOJv$~0?;5&bXI(lRU!p|rJ+yteFlegX98KxyGf(`hJx~9qjK(WG_K<<|A+J|tc zbj`$ibn&lTV$fxS@sJO06Sl5q_lZ9rw2e=TrfVjlt7nyd>ymlzUE8h{0wbV|Cv`8_ ze+bNLW11{C?V0SUZ>xs?m$orXxO;vl5Y#qP?tTorg=B-ET%heN45aT!QwI{)CM#@6 zgeOaF`FvqII#i2;B48yF49$P>I}6RyB>}-{pnHx?WWQJ&)>TL{%@{6xdQ2swvJ~Pk zOfUOmbvJLPj_B{GpMpEKnADlw@NdeKX0re+n;h-Ox$+#Cx!lMV+3)2lM0JZjI~8EM zM{*pZaAcpu55ew(RVoil)Q>f7Gh94hB`u(??5@fGG=@A_#usS23Um9XDTm^PAXhS^L0;yDNS+&-a|9 zKD7ek%9~=g+eMb0MDx-Hi)NfU33%Eo6diJ{kzfycI_Nm!`%nli(-puq5uH zq)jElA9j0(M|ehjkyKiG!$w_OUR?Z8uQ;EFG?MfG^AYYiCxPsFc90&eZi1bufTpVM KopNQ{$o~T`3`%YQ literal 9931 zcmb7qc{r5c`~QO?GEGUgXk^b4B1>c}3E4^3LS#v@RrVP}$dWxOvKArxnr%pyu}gLt z%gB;-WO*3l_vpQR-=9B!mvN2DIp@A#=RWs-PMDsK8jOyO4gdg{hWZTy0HA>WqySD( zLthr|AMKzo>ie2%H-N*VpY-~icmUu8G;S!}_DWfK=bgfC^mzxJljoDB(XPDxlJ}R^ zkZD{D5Oj>^MS3PB#a8(FhN;ViMGbpC^&NZ-fkhvae!U8CZ#iI~R2Jj3gJW#Bg=0>) zD_=3STdY``K-&3iFDc~IU>wsn$6Cl84$>M;{WF}K`r!~lGrlS zBE+X;v&qu-=#)aTxYSuI8yTUwSWS#CpHO>7y6!3-CQ%yvcDE;%`(Mzg+4=(I9`YrZx=mCiwqK{R@HCxA#X`! zD;gPn%%fD*KpTxUmV}8n=<6=^4h>`L+TNV9FbU75_&zx3$f195cVbG~W9mw?SSw%r zrS}Fu4u8e_%kYqE<4AV2AL7p`qx2b7G1-pmj%k{zP+!damU9u*){Nj!(D`{4 zq{+&oBcYJc=eE&k?s*aotk#p%AK`MQZU{)=tq>}rT1V60wPcBCiG9HjXrjIUVSXULO)TeRgDh@%r~;c-y7sk9y0M@)nRd# zS7d%!9j5syAOf2>CDSU?l)!JP#+78%1~0OkKb6U6_rY>6L6ouJ2mHwB=%9##B2jaY z6XXplJM~t3P!blaek?CjomQV!2E;U$b$~TOyMmv{y{w0 z8-FvaT#gh!7Nm462y>a5tbz?etWJEExP%p$cY8qfv7nwH|8Ao#KFrqH*m$^+!#~b4 zlNu+=n(|bSuQo65M5yvQ=vNz=KyC^NN!r1Q_LzQG7PS)7e0ZlWTH;g0%`E0;PgQAB zQ&fnW-~U3{VK<+`9jhMTq4gUiAvlnZ9Zj=p_z7;T`n+qmP(XM%9Tq9S)Hc=X%)#+a zYj_KDRvi__#)GwW9a8E<*QP3J8=ekJaBEFCMEu?qt)3IX2(c-u-o0xokau_~R=sd( zd70hk)@7Wieh?vvzUSKOeA1!|i;Y0UjdaHb=KK0vWB&VF4Ii{Kfez}(Q+3n zoQWQO^7JX}9VE=ZVUF>l(Oj`&l$gd&3*(}~BD_!6Ls~fj2P%`|^lf=dvGpo1;b+#y zlGmP4{>y6NImr<^HRX&xCqctPpRGaXf;*aXx0=`#wYxhuVaE>_mLhe++h$exV@M2F z)ls;qPeD2zX?!g7T7JgmUcRSoYf36^Im|9O?e|m?emMjNt{zg_w#{bE=$*(hwkL@KFQ?zJ5REGKTVSc#Q2$q`OlD+BoGk-JG5s0oIS%H6@XhaEDz$Cz%mE^pM3OW1Dw29roe`?` zyK@GAo!$ZyINjOq251&L9nqq!Ub-JVVA5DZt!G|KWV<_D6ty!^ewGuQ`*<$89=DGaeu{$_gy*woTX)Aa{}q^zHd*rByLVGlJJ+^i>$zd+dD|l*xwn@n zYcM&l{x*uDj_6d~<8is)gI}F=dd#3Bf;kHdpdbm==j_G(t7i8Sk=nvcRP(LFDI?`o z^%S>7>vw(b>UL-CZ+8y;WtdDsqyA7~uU=_Ra$n#LS%C$d-}RAX;|4$cjXXk? zEZOhCsF&cPez5*!=|vt_t+x9DDVu}?__eY1@dmAVpm z;(ESr_FwL!m}O2k^eGZZLVq<&oGvN&6BX!5L18xn3Cwv)L``aY4q-d6e*lnahCM^^ zIG9r{yjWVh#cMdbaOMZs-`3M|83`=-(AA{A6+`Q9$rU_c4|j$@lYV+BZ=2HaU>zMd zo`1uslbbNBNl1U53(?JDyWK;jr&GWFn$&pYkHWY@RO_lXAZC%Umb_`5JM-&3*L$(CLg$^#^*u*WuxB zX@~N*V?C(!43;c@^htDa{vFJ265*$WPk{q0HK!V1I~BMiWTk=DUG^Wp3I7I6ND7TJ zaR}gv3XP$7o7t9v`(t22&+dG41LglvQ2;3vhheTky_J=L3d>%{U8nnu{|9x;CFo>o zPW6mvIpr@jhv-s_yvCdl`0u`2gwQ)OG>vNJ<{F64(m+UQ!(iNhWXJTGRDT%f-4lJb z_do>D8(IPWa{^Wy&~DK_k!4q$gqE74&SE-blB$orCP-@vyo)3t*iG1oRKPqtyMQ0n zREk=47`+O=JxP34f8#jpx+2j$nKI5&ic^!rm!PIgRXi&akmy7SpfXH(u%=MbaHI)Q zFPrrqvonIND<&c z6-ir@L?Oqf_)HyD_DAh7n6s@~ZG+4|!XKQ=pWX=$d1rQkHJI2gkRqMSkaXD_$$%4( z8ZK(9(T2~Oj4N#tF26!@*tTI^=FdaBP1L)_io-K63vPt?kiV6 z+1M|3pRVw->og;61k=Q7eLz@RUL!VdP%9FXc@uPQ5_gVAA+B?z9W)-imcLxJX?0t* zd63zku`I`E`tbUOY|5fc>cC7LAClc)Uglp`GX7uKNo#1+Qz8)*By zyhwH@=y?A~2W>gE+dr@|cNMGXZJRJCoE>>V{DNiOhP@2`m7$Jrr5Dbyb}-rQV_nqP z{*sErJ?Vs^#xfvOm8?=Sd<*qT%BL$p49nkK~|$^=?>wgsqiVl(%|o~}N4eQlnD<+}G}E^dAb;9758 zaJ-AuwCm=k%=cY+`s-~pX^{WFTGP93ey3#YAz$aG*Ju&c&O~%binxguv3Ye9s1rCW z_fE=M$`j?5zfX!OA?h3ID?AjmyL~ULr>w2m|9|_B|X|D?ZMJO4w7@n4?s;tT)c3^~&mkA9GC(aB5-?wmwcC0W+tad2#E_cZW`0}e8@d17&6N%}LyZgob=*kcdBhA?q7c|Lc?9 zzp&gai-#=Iah7#giXRU&_q|LQJg1)ZOG6&)MfcVTT$~U=5+?yVVI;u*(C-3A&SZJ* z!<#+tDo^AiH`IHN_}%-QY~xI`(Un+n$M|CRo(OgoJjU6aSvrFPV~44yC_#$GzIZRt zWoP`Ztk8q39ILIi!?W>rh3)dO1VkdTVV!duG3osD>X&6X5*@jEL#agd%?u2Tt)iY{ z00-C~P6YfE!It8@7G>r1NO(>XD&5Gf4*f({@qo^!C&Io zYw6aMaGYh_V?Ntr)N}XTM8)wjhk!BxOu^|yBdz= zF%>&Ia^1M8)A-vv-<8mK=c}CVgA`5Tj=u(v%}Cw+C4hZi`%^&np6#8nh(i97#{=l8 zBs9oZGqX|}!B*Pf*8xvv13GzG04C$2sT>{HUfZuT;J(EDuG`-WK-!y;?_vi^KgDQ0 znfbuqfNAbNAIiZGc2o6)MYcfht~DGS=9+?^d=SZBQ|#CqZ&I~r-Mg~AgJ8YxBl&j8 zbB<7a0BZ~)=5SMGoT=Uo@fDx0GwXaKT-4ZC;?zv#;I(1g{E40i%@jx$W9q3p=3yt? z5=rYJww-e6#j#ohe3I3vZ9E4)I~99Vl4Byoc&A|j7w<Zm^UEX$`fDZsAuJ@72b`lU{f>)i4# z$*I!&acP8JnE5KL$ue=rek}?)1yVYnib_7RNYDX)okjz-(ZO?A%($Hz&^&z~9LQ4J zcKNcGhL4m8o?uUT=jahbVCLvxB0L5?)=sQG1TMrNyGP?g*SkcKx2wCGuG%F-h5dqF zW%~UjL?JW`6=yp?r<@%o_skau=l(z?Zkmu&$^XnVh(Q!O0v8gHFNLINklSL)vHDa| z@uVT4gm99pD%E;1=^$tM2iaF1TN#iXl(yu{NsDu-SH6Uc4wMeyibZzE9wZl&GbkUQ zn%ri|ZcmXNLBwGR1T|{}y@3TWN7k2kIu%r%mZA|g-?S-_pxIX;CcP&KOav)~81d(K`9D)L7qEU1bu)VX&^s#8zzKuL*q$w3cfR(sbp`mG)-sEo9Bz0lh&2O6zZs4SLP1*IFuxco~M5= zO>-y#0T~daZh&!u1=_?sQt?g`C)6@oZ*0CvXHF*GeS}^rtb;V|jlmmd(`Km45nOA6 z8riWr4fUHwo)r6ftVh*7Iu+8{YHK+J9Fb4zg!^8gSN>Cm5kjWF7TgKV6*LTn5cP(V z_*QZ0b1Bv=>ZmttR5bGzPfk88+`IBRvtu}xXRM!3CLWr$^Md4>NfAT3a&?dTP3BQV z$CzG4#1vV_#)tkoZWl{3n7$B)cm|2oGW#apic^v@Y2r7xmOMJw?NY~?yCXs5_Djp$Uw&fotTTv3_E`utXF=wG>6Da0nl)5Uo?BG{t&JhMKX~{Xrt`Lf zAqlnqcsFp4c-7suZO_+gfi_-0&Qfq9i`X0mopk5-h4U%C{URTG2JG-wY=@tjiiVTx zN&Mi4FV}^UDSM2Zavy}y7p3<12C6~*N9O1%oWKziH&FjIwO$R8`C%CxsLq(3vquMB z7gcy9Wz)#KNTO7`5mI_Qmb4u=|L>%@4V}8(+M=14KVR}mKyqE@iba6Vzz3njuO~&n ze2|``Lc2cqBeOfY$VI}OT-p|A_qFX}Alaz$Ac@1kEn_kyG{u#x_~)5F(PeA98FzR0 zv(i+`2bLp|(1z;T32G=dMaPb!kAiu|=;I!V1+l%(R0ZRQ0V0P7MPk3-^G{0lxq5~A{I09PV*zpH`9qTvt36avj7M?h5=z4ON3li9YcC~HM5 z_3YRmj`xI#<9c8EBQSNsSZS$!NC86PNmQ<%D7=iw4V_*talQ6bzEy`5$-(~&st7b@ zHBAVi@ek@Mh@kt8$7S&2v1oGUoJqrsVAmV-;C`Fdi>K+$g;vEjOqg|2WkRxBIYvmm zIVJOh-8@)Y;o)NuOro>SIXYY9Wh3xp{pe0K-v3$ZzjX7zI=5|oQcp!jSNCX4bp}#v3@_<LErMNlQDuIG>>90O8xv#W{pF4YUtJ7dNAD#TnK&1u)0i|EEPDz)gA!C< zO0(CLG>}pv@(TCjpe2h7o~%XVQ0XG;kz^vYMDf$;yX`weIwH5r&arZ_- z(|BQm2N_Tp6;A|@%lcT!rIJQr^~3AFMkwGT`svA~okwMroXi8av9PV$s%`SJGw5NN zRR(K(041;d@Zrwk)9Fw5&+d*FDPX*Y-z;~sl?r;|(tai(52VI=Q!9%FJ-_w4QNyqA zu1uzB-bKlb0TvJt_J?Dfmi&h2RN8v%SKEFf(ad%8T@CkR5I_{dmkL?)t88IGh8wYb z<=VBLsnj(*ChNNKu{hEXnE}8;5L*7o>(ollWI~ZN{bF~+9%^`yMHDHsR%I(|7y6{UMCpIDnZc{Bj<90A>!E#0cYdza0JsWdflje4Xj1mvlyT5w`#}_@lN~JgQ;m05$XgRwWn>^)(^krK5J3PG0Te ze*i-~3#>ri6UVt|cK9hUTP>7-X-~7;H}s?mcyiWZl4$us2$}Wy{e;VqV zQOX&wEI2pq&+>u$9>vqLb^RpD>|XQW+|5Z$NxaZ$R#{r0&-*zhZ5lfMKzR=-V_WQepb}xRGU_6Z`$0=1Gaq8Kn)yIi5gjVl0osM zkI${0Ot@p_$A=ST(jg}ExhkV%en2TfoTW?(L0Wq%f#g~Wg>@dRE7Uf6+o%Cf--hC@ z25Q@`CK#?ST@rbk5NaNs-vbeXY_fn zr=^grF9=D&1m??v7gUKSZD#*xnDJR1rFsmB7(@D^+LCok9o5Rx=*=}eIO1m5r_;(K zs=)`g2*s1`>+|v;zrg}Fwk?cpRj8~u432WEA}wNs>Z{izpfAfpEh?h)0bH>TLJp*4~nN>o)3l1Ep0k`eY-k&Xq)7=pNPFvuO*1QAXMpyLvB$a4Nl*2^UWu_k#E57_vBC6pcd3K z>W4a0JlLn!rNdXoS}7T@x0kdd5Keryrl2VLr{3U%6}LK3&BQsbQDcb<}x9;OR!GqnRJ3^(8QZ>qO zTq;0XywTXon0{EeW&}W4{mGlLB;~RL6tWuAMzLVf#9F*lITcR)^#sz_ z0%Pdc@DtRw5<3kTSE#)YEVFW=`iQS^KCdRH20adVa`YA;G*3|OYTBq;pedx`WOe~N z@IV2{n7uqQyt{Nc_n2w%0Zy@|3C%(mOrX+0#UW|JO;N6#jC7PK{+5CmAkK4>vD5T( zadty}z5hMIR}2wbjslP<=M{6?i}D1OGxQ{JzeR8H#((Xn?)zT;i<1f~ zlrnjm=Z|#GLBWiHHb|WGt0^&Y_&vj|83!r=fbNrbNevuLihBi6H`rRqAYL8y`4ek2 zqaxW#?1c7UWu+9&YV0isinjBOJ@4e=bG?=hGW0%b4_uK)TAeB9oY-u~ z6QL?Ov+9Ku&-gyXnKf=u2mCgtXxTL}HDzV?j^cz0CkC8o($(2m%JyCs6;spaLDRFL zX6A5p-ijByhrILc8(C^^+g_&m73vx~ou%Dudka;%e6b{VQ;7?g0!V<~y?dv$4bi1l zAEr^c?`30>i~+|$6#4kz{P^jx=SR(z0&OmEuGCK6fD`17J>mCuZM0ql=6Q>^JGM?L z;GLW&pAk>t$=w5UnlwSJf0g~cA~Cet8oFP9N2hrY-ng-IaDa270s?;2dV03nRP!;? zS7z%naQ&A!6$S`wnnI;@6gfIN;y^cismxOm#XEMQCa$SzpykqXxK4P>eabUXiS)+} zix6polx}ckRL#g&d|T|@?{4203XnXyO-_Vvm8bQ*Tx3#KI+UPV;Exk$0#!0jf0Mh$ zRG^le{Tg<*-_6DzSqT1S(;mF6&bPmYIjUTaR!JYp-`iW(@2v}W*szwrC+SAC?Uu^)R5$HFl{0%B{=qA#pTA|VWq_$PIr$7B9-Y9aXQ@YdIO&Du=;g_>RGJ}6rKi9Wl_DC@jDE(ThaK|j&9tq~ghNRGU*gwPWZ0>p z>;0DBheqwuyEk0@snpA?l(s6U=XZDwb5Giou{1`kh?)U z&zJ3|v^v2!Vfhx-g!-wD;it_XquOSncQ1WUW6fha6^Zk=#E;aBgx+&S zokaq4lG;JN^Fh5iO8T#CQp)C^s2p}~s@e{1_6gxY63RQ@8fQJroJYamzK2k}ag~i5--pWev_BjZe(YcS|blJsc zsoRjOBY2o9z8IUEt1ygje97oc#iFIE+I{Tfo_s(zrpex|)sE6nGT@3`Iz8Qa+VvBS z2iqNKDu2wal%yCaKI!O+3JH1&ELgES)BA}s4p9bG?fQMnXlNjt6z22&f4%BD5m@}? a5U|{oqv_WTKMB1~12j~1ZWJn81^z#@wv#jf diff --git a/assets/icons/nocterm-512.png b/assets/icons/nocterm-512.png index ac07ea3866c769d35e73be6dff2bca0d2db4f622..14649749258800d7d6634740c7d30ece9cbe4768 100644 GIT binary patch literal 26315 zcmd2@c{r5q_kRXiB1E?AQMQCAB8DhSSt67*l?r9ww^2yQ7D@IsktIrYBTGVvgluEq z8OAb(G4s2pxA*<_eZTep@2V?Zm*+Y6Ip;q2Sw837PpF=b`hoq2_d^hLKvP3aAA-oh zf5{+fO7LUDvv&vlpuBNWT@50V{-oFD#zGJuq^YKI#WQ&+#UsJMaHj<+WW8USPO@Gv zCu`uYs`{bx1r^lC2gF^|e|+1dI$BG2$Yh?H<{7r4MJ>7&J4Zl@J_rlb z&Lbv$9_n*bC~bo-8}8jH(bf{12pl-F@3iWT@S}wtWX=LXXphJBT}N*0Rd}uqf4e|v zd~75KWwW&GQqh^0hv~t|zo2BA2 zlZiRUG3@R^>h1CM^F-9A>wIg0`+O&!V@4e1(0kVHN+&Lh>X-$R2aMAlmT``H)8v_^ z%eDXUIOM+E_MB;4gMHi){G}Lo9GJ;!AAQ&clQdM-M&HGcy}Lx9y@6vY{It-Q0a& zAVS`Nf^j+owK$s+YndJ z<_*ReOX%VjA7G4ityA%f0u+pNlrbqO5!WT(EYe&&Q9{8JH;$Q&7dJJ^Dbr{&hHBA5q%yK1#mKE=jRj8!d;0QTJXQ+m-1{E&B zX|2pp@bA}qn}p2FWXj0b3*3xDhUgJaoWs4&)$+sjTO6dOh)#+Pyly19b1Cnr6!$o0 z=()J*`(_`Rf@GxbetE^;BShH$$?DaakCsM8k&bdT zb_b^&d~yVS3erYLTAw=o+S}U-T?eDz%v=l&j~7oQzDQZoFqeabf9w)rco%Xy4e^W9 zPP4GQDkvzZj>eSN_8b&Ouw~%C8GsG9iv@cUrVqeCg#aj)_V)V$80Z{)=4{mTF6c*G%o3TGFW=vafj;9UJ1wTylHF0yu}q>nbbRCic+FCA}a^!FBuw!yDC zJ3D_lE?Ztz6pW@Qh>bt~0@>8es?UTVvpbonqPnF(e$ei80DU}*il!#bxr|31YeO`@ zZe%8YnUOyDvLVYkKE?nP+K%>tbl9NB&i%%nQ{m?JM{~w_0* zd-hCNLLzGKTTL8t`B|hgu9|wfI5iAYe!lSGIcgxr5iv1Zh%xl4?#%7*y!F|xsI@$C zmvQI|b#rr;yhfVud1|S1TA@JckNP+JLw+@g%Y^rL_1^3}SX#O_HGS%}mzw{Hx4tdn zNwN+pL(4DdsRFzYu`!^@kwkIjjkx$=?xRPm?I(v0{hJSFOcWFKc1#+IODQcN4%1KF zR52ZnytZ&V{AD97oi#8dN?!Rw(`%+uH$kQgnwmCW@8Hxk{%co};uD|XVO_JT{{H?Q zisVhFugdwiKK0_Q>b@ab9}C&BpLOlEF`{_O_vP%N{TbQWGFxl@dIV81?gto;gCMxD zOgn^AOfx|Gz>gSyKx4t*S>>^8_hp5`(Kr4b=W>TG?UREbcDcLT=NaqN`59joyi&Hg zMuw8t#0^=@Q^z60Ne)aHVJgT^9K2!}3;~Y~g9pIN>`)mu8h6Ul6aXhNYvjsnV|&HS zN~jS??b5Qz9NuC z6#xrVALS3KfI)3-Z4+tp3~iRy{~Yp6{QV4JqA&<_F^_`_3XX64-tes6;x@f^6?r={ zlGPyQ2{%MZKZ>?WTQw(xa&mHB<>!a-s{KoZyH62u8iJ)R)R1J&{UQ4EoU#Re>LNB_ zPh$Z4U-nj57}>N$E}NJf-tF|iEQ}cXRg1J8U@t?}80pocpRok3hJLsoHdy|>!fLtN zvTau_Gv^$7b7MG)e81o;LL;y`oRzMLd{7KeEIIYA~BcXw7k z$*Nc8A^8Uu`~6?mCS=_D3`yRd^S?trfrcoea6^S!IJ*a!{)0NWmJ>zd!sgSw&85e$ z#;X|_F)~i1zdyf>7^$$je_>dNPk=Juf$A2Vx}Fvyv$nR5Ll)-8Az6hH+~aYGrHR3Z zJ?4bVM`APzFH$(&0!p5>zlz*aUv54iuX6BFrMkLdXCQw6}a8WDkv0y)-e;ePiFbMR%#xJBw*{7trg*0^~L zRX$SrRdzN-{81SHqh}$G#al1w_{DX}xMXf2rPVs7{f{0!`dIBq3HiUxf_xe&t)msx z$bak39aUZ!5kWL_5(WaDFt{%eCDV;mB}K}l@?Xl(&u zrh%Zh6ZJV&Ob$R9kBKmvrTfx%dCQS)Qs^e_#} zZ7bz>2SQZi;L~WO{~Vh*WW*DuSFc_v3m>AS2;*yDu#uRDiNGPc!}1GvMVdWDte2AZ zehtJVFJlAFT29U>Y}`RP|1^K{7CVXMlOlf9=@k_f`^YL$Dq)dQ61c2?IoX~|gv-v( zw)Fdy3-X26(Y4Og!^?w1j-kDy#{|PdQZM?6P4grS%ed_#v z#?ZsYq4B)FJ41Z{zrds2G2$hy`Js}VKAYBuH~;O@d#(~LlfmHo_wR@5pB_ZLv2LH~ z_&WNOHhX~w8dn|jhw9qe_P1H8AIbfh^qt2I9U-4$fW{pKy&EbS2%l8*cC6%&QUyE+ zRM&>4ByFxmq;2Z{%iG<0jL(XD{=BhNkfP~}AC}uQY^%?VZ~A5Y#6(tNX#IP1j@oQb zPfy~IOy9$QS;;3!oO<4-IqbC`^>T^l+JwIONTtfcGHmsJwU(A^BuN*x|HYi@bW|dG z|0^Ya#+2h?jxQoRhXr%CprvZGe_^Rhvo6!!%9}0pL5<_)y88bL3LM9Y!Vi?8efv6K zkKwnnYg$UJ+uy&kc8!V~B#%xJD*|ppPgo6S2dd5g#3DTc6yPXl<`<&;iBWh+ujh>A>2^8gSsG7p8vn)O2;q0v6FzbtoC`oY{ODyf945ji$))Ya2a}! zG5v4;$p=B?O2>0D(uW+(-w#txJv1)1aoZy@Zh-s^cC?Y!C2^mBpz>&^F7aQ7WZyJO zu*{zR%8^@!&yQY=zvuSysgnK;24x-pF<BRKQ0{l%%CvTjpSmd2X^Z&XL@AHj~j2cP>!Din< zJ!l+W<~#F*2l7=qvyUkb+3{;u1}gX~iVMg2n-6(ZYtY}M3LsU8oks~@pvY8il~9}(;dQuYT$8*pEsZjccDSp$Id03@E^#^?a~g3)K^a6Z(3RU4w@bm{z$G37Dc>Ds)C9A6$L zkC~`y-k5?U9k=H!m`bl|g_11Jhvw(wfpqDNAd~oXci<&M@VRLHbCBxRK$e5Y^Fgis z0#wj77A2Yh>fm#4?$tO6U2cti7l&;BPYAzEIDPVP#eB{?#{Gt{0P3l2UDvwTQ&P?l zqY@Vt^vqCaR4w$E!ZRIqkK)HGmGiHS4Gj@0j6sK5&J4zBKjDNP(S3LDfOYF?g*sLM z7-2&h_xgD*eiQu2J=p_kf-nr0s+{*I%vYj=G}0?{k^I#P1Bv3`)%+uvt2F8PGuxs5 zpxF4k0KagUZ=SCWb>V`5tY58hZd`XGv}!tjlgs83Y?2a6>+)c?JthSF>px}e6B*?m z6_QiFV88Rx_&fu-#+Y8AwoUyj*mZPw);j;Rq})m956{0e{w5!%Sn7bP^YX6h3A{|M zY@|7*M;*gLzc{+5`+HGt7MC8U1<1xZK81$`Qi&#CA6_O8V8A%`rXEQ#sGXaiy9b)KC`~_C{Wk0-kG3tfPu;BVnQ_N=q`N`|o{Q1AIY573E-|yN) z{N|z=`p5xSpTs_4cRGt+#c4RGT_VdHzq>A^a+q zDnO&D^q}_d^|bj3u9ZC_=yCBep;f3a>L7W*y8^$sY{`yJ;Gj2M zV|xm!e5NKwWX6A7Wtw5Bibtj6oH2=o&7%Yl$8n+vF5kk)3fDqdzH}h_5FXK zfnSL$;IjI7Abx!CPU&FdIt*GZyHLoxf4Vs9Z!q-N4JgFFUN;mv^w0-_t`_DKB|vfX zH=_2d>4VIuKCF&-E%O;+snx0;3NVwAqews4hC2SAnnoDG$q_NUar+Z*nYoZ!ku(ca zOAw4NJPq#__ZuJD5rY^xe6l@QRBFu^Vths5zl#F)_XC4Y#zdK$nf|S-v$+02@_Jah zH$%W}VZ>V!cx*r@3AdyDdE%3Vh~BrVO$raz@Ipb<=Vp1J-*Gk5X1P+czkXL0f)#T{ zGx__q#-+Y5`zIUWunEWx{3*ap8`52+aOG_%)(3J``3*}-r=}eC&r1ROysh$2fitzY z3!hHA<(6ZZ&u{zb0%3|f5B}dz{PZ}v_~qz9a%kFt^}n<53-;b^s1tb#e|X7IqXQOz zk);JWtK2kib;bLiVMTGeSZm<`Ftls`iinl-A_(s6KInH!1&;9i0^J{8O3D8Cr>eZ$x7XX((xS+ z|Eb-7`X60fbp-ZLBGCeax&aU`I?xvRFOy8gVE5l7ac=!T#?NsaL7shm3JCn$*A9cA z|GU@XkTmS4fWRkI|FNfktVR1n0ub1P<9|2afeHBT|4oHR8~=6H7YJLu~;@M9Qn;klMn;9d=R@^@C?lapL>?L)Qnsd|*H6$>_Z`7im9Il?h6z z)!jXs*})oXTK}E3JI5_b4~4Tze8@Z>zP2ubJa>9?Cj-Cy{7Xs25H82 zHhkd^+}z0e#2|)f%KFONVpm-IKu`G4om10jjSFq4W4GIQxghuPR+2GOPp465F|Y?; zl&ECf+gW~{%hpj~TFxWQlNNd>%?CC;JsnYyj^}RGzzsE-;TqFn^>5lCb6~ay+u%Il zh4AmiH34^0{fDpO$dz}umL2!pjK{MWP#*?LZZ0TZ#3?)liVXdZk)5A<@cHmxtlo@C zN(z5nTtipf-68SWGXk4$_@8(5O0oX;FT-|8+#)p-(Y zWhL(qW@n#3Nrwiua6LvKR_S#VHU+7>H!LoZ@^K(v9HR6V$0S@UwYw7e;bJ* zt8!n?%%x(m={wHmG3q3CTM5T*Qf}{=Lk42Ez_bH%8;X9O-sNA~<{Bk{G~&n=G2 zr;GvQx-kEN@gtYaVF$vZ;l|2{-R#Z3Ee=tgjs1Hm^J@1Gc5f{k9%Nzr4N%JNLxs{7 zX?sjtoM%8S9s9$eiqGxP1A_v%uy#q8lFww>cZp1OtiAUF=Oq;tSgOA*CB2Q~vw%Q7 zLjJdBMtP53fzs@0DmXWw*Ml(FSnB5PI=jg}_|CpC*MEQXvuD8^UwJ>SwM2N-u}>G9 znA<$FeSA0$N#El@auPuphp-~^>aiTQwDU=)?{D<#@`FCfO7*NxvS&j{Crbw2@U>W> z{(;MZAZmD%LJf(p?hI9*n$@mcy(_&&x_gJ3X4l786dYP$R8M-+k}<}GMenpusU2vu z6q=X*TUquC^{Nm5o&~T!!Q-=mJ|)}QdJo_?B;g*E2Is4i7hWy}y+#%3qGcq>GIh;v3ji&e!Z^5p#+-RG6>$LiT zYCbRR!ct!)Ght_}Rtn9|&h`uKT#aRtR|V$DTI;>pbd-&FO?OY`7V)xx(@fCU<$VVK z(X4{z!q3epp{Brlt`d-Z3JRvW3^r|L#+D>+ag0svZ$2(~x1%Uzb7ubHn>WhFKq$)r z<+rgB{c&n)YV3WIgT~o_|7ZtwK>z0!M5*USayt>c48WtXCp@+A$g>k~wrNE`jjr|j zifV5h+8Mg-_bgyg&U% zjtAO;U3x}YvGx{UN0MyId~Qn3BJ3B<7EAj4WmUS5fb{tz1COpZ%;z?sv7oB@+gN=< zD}Ng6Ng0PxuF1%=X6mvW+AWdXlD5d0O`aTlY{5?hLfL)h`fKUfXh(Bxz;#nzL_ z2D(ZmeJEj~;NPYx3tRt7JqU#-yoh&ow>Ub1Z~5~6zG)jA_qPtUKai3KuZx4}k6gfIwe5&MvFd|m+&R+tQCoMc~WSDM6BzEr|2#Z7ZH66~tYd1dm zi-hITZ~g)yix;&564PvqjN;?X{(hoq_wOeR`55b?KLl9@ z=rB^{_RTn`H^Z*~gr=pr3eHZu0BGYo#g3#S5c&twk-u9W$t^!8rnV3b95ApNn&Mo1 z*|9!=?c5`)Wj_t<#XB8zbWdFx2H>k-vj%yUoAx~h+MnvX!#pQ6=0;xqlbUUU99*J! z9NW)a`0nmO;;gApI|$Pm*X*S=L@u-J|IvzK%*VNalmt*s(H^6FM2e+9#up-lPTVtp zRm^7-SA)l2;03@2cNi7DGSc6#J4@YTW9d8jNR_*aB>TNLMp~f}oU%7I7s{@dd7p|G z-=zo9x;eA?;%W;oqSumpy%@mCLdkt!)#gPJ2~rR{{%hztkZb8)!)48|pK^-F*VLpS zM*Xm(a-N@6*cG<;=yvABeUyX~0LsL_?7R4?d&e2Zllbdgvbn1I?=Q_G})6%)p2W?R? zH36S|8cuv7flLg~P4%Zocf1I66wa(QIxb8M`s$r1JTlEd4>^%?wA>mzjF0_pMfHoh zY4FO@Ae;CB-qVK~Lk=_-BljYad&^cem<2oPkjBXV96YnmqRkPHDfC{@^#MbOl2o7Q z(&CssRwqnZtKGjJz>fH^DeXwHuQl0vfs5t=|Be=j$kJS?EoEWFm98GkYy~K$P4GX5>5|;137*O3I z6;ng>Se&oi(w9Ifn~CqjJOh@64=57g2$+Sh4eM>>)`fGH8xu`J2zrPDy8YRcNp{59 zC#&l+fhKoTCZ-<~qC&2GJCy?DP5Vh?Rt9daqOqukEaG;O7=|ifVggn%E8#H~OZ@zS zriC|LxgIrTva%(lbJA(zn%qWoAm>?~zmC$ePbk5Y4h*1_74t1t+#QP?a`;#e+w0wL zHwI2!!?*t7$Hg=L=Ce*h(_M;?U>nG@CAYpfU4467eCz?<#l@vN&p=(Kt4rN~9iM`7 zzG`9d032egq4)P&`}2*#^hgq4iRjBE*r$S6NQ9W(82Ewj1aj%frL()A6Vhr%vhQ^9 zc+NN9S;f;zj7KVSC&1@d z?LmSx9>P;!x4Tkk)T?4tY!Rqw(An}dr`?vv>Go?U1}Lw~+Sn{`*3wg?Gk?l53Wnqj zd7{YP!nH2+?CqZEq?nR`3)HF!dvThBRcL9xYor*+EZH2$T#99@9H^n zQy$XXNqz+6zdK)|G~}^VUvRB>qJ%?t0*f-fOfX>*v}}%p@$Ko7fgUG4e0qv`^;09m z%OL#aw@Dj>-Ia#v=N40LVuFFf$L|G83nh;>y$vOP+!v^FDd{AQr5{16sBXcz1vJO@ zbtFwDzwK(8CGIh-;M%#t^UrRT(E;zdT+j@$bR=9Ec54w|-R+PYO`}89jhUG_ z;f`9AtDJmnV{nXLqWD^^Ix{hXQ~s8uQo=fG5pu=$-QDhUJ_v0voaeM4A~gP17v3O} zo1Be)oghMtJK_i88`yxi$CBeQX_*po4x%B@#NpuJLdOQDmOgL65>&-?zJMWzTAH_( zUI(kOeJ=a*%9LQ-?qBxGK)zBfISelgz30CabAPVX`A2nyg|Tv zSIWc1qRNxF`VD-F5dD`zJ2uzlo})8~4s6F=3waHJ=Ur=hGCwy`Z9GXUOnk?;Ny0&l zJd;irI<;%}zh=?qe|brX%GmrxQ)(W_sj%mJ+3d60NlynDqeqeJP5e99#i5N@rhzw30;lf}F5SM^wp6ma zQZf17)Ljv52Fnkq?0sW+*x3)k!k*pnkkgZeDR$ z%EHqre$p?dH|KmftJ~4bt&Tj?{dcB4T1uIA44FBCuZbL3OUfvyz}dzF*cPw7yn>Z?jwLX)mto;gm|^R=ANiWvusQxXdCs z)34fnzz_Sf^XLx_xMoQ8aO5f_#}AlCDmFph`iQ;8Sd;QKX$mv<=5L-`m27775ecEz z*S7bthEn~mE4NmRVM2}dF!IA*aU}g?5=hXWd;@1kJ&e!mn6^A;|Hp0C4})-Pu!XgR zV=FYDuJ;w0hhQ)RA&~y~VgNI!0sJ?4GJpzhGr0UP0MA$s?~S>l@9G6Z4w|&=3h&0QW<+YC9Wt|ecRy#>NvFv(qYo_H*X6I3+r{J z=3RP0GUl?B%n))pid#IiY~qnPUrBbg{vva;#THF@_kzkv{%N$+sZ#(TC1rX}(%FsQ zTP#jmW7LKn_fFB=7iuqcoS!xxuSC?%ox9U?NNE=zp3sFZyTc1ck^6U?o*Idp>Jd_n z4O(_#e44@t9}Z6m9qVCP zKw6vQ?dQP6agS3RLgteXPQZ|)ZNYiwe6)!dE%4ui>Zc`j6GbW1=Wu97QCMfLU+p0`qE%N_m@PP3EqK1O4VH zUk2wwAH22PeLm@Ta2AJoU3vALr{?EVd%K&>qdw?q6J7f^w%-x-)N{!G;m6JNpiWc0yRIc+2c^lFw`UNH=lvLhd zdc-1D7qzQxtrn3T5^%AoY3Vx`nf^1`QBT_SSp=uY$bFCDhzEfe#*#z7uFh?=l-z3V z$<|Gmj@U=b*6f=~5<(0CF@N3Q4~M9jSjp_@UQ!Jj2CTzYKL5=dM(E8%_4!o!fdoZxxTQI4b8$P14jWSfQlUOA z^(IMW;8?MEMud>-hb7t9jl9QSmR7Dgx$BnfyFdX(6d#z{SCb?0JFmBtQpU2PlwRT| zDPQch!ov>!rIZ~TCBn9W;u%e|L@u(!_l!a{1RsW<7-x(Yt=S#rsyW-<*{{@ofk07! zdIO6(D^I?=45wrkN(;xKKGx;D>95(`QixE}0S2Y7N{}QOR7&fYtBL6N;(A>;F;hr3 z^kN`?`azs}C_StgNqA(p7c+$$4|!Ixn%RC&?eq!apxTjxHG7@a?AY8jIlGBNpb*Jg zR!Ygt&OYQaN__KS64<)18_4!jc|C1WR^>@hdQE0SuA2yJUuy^|d~G5mjB$eJp-EOXDU{f>Q2F!XH9~(jwg!pVnrduEQ_Bb)Jp_ z4iRx<0Y?&ZdvIEx}5CY2bwcGoQ~w!oHMR(@ss#e!GTg zWDxth@=@@m^jfks43b4T`wU~lz=2UhGPH2{#VH(knozT~!zp}sluJEAA+z-qIQFDr zN}FpUeFfLTwUgXa1Yco>YHhyWPWIXoOm<&rt=OF5yoaDo6D_XUIqZ*L%S}P#;_UA#|IA&irP5+5vR0bfk zBsdeCqqzG+@5d2D(@^5xDtSC8EO~vm9SSUJFNn-q`0b39%rj&Gii10sg7k!gE>k`E z0E~fBk5Kk-Y4dKIb%2c$lwFL+4!&iGl$+4w^4hj4LUJ&kO7+Bu47;u`(=fmgyNiA{ z9ul}q)~!!-JUu#hJ<+w;65nQQ>%RUPGtBz>YSZM`65^|v$c+JZqUSAn``YEs!TSS3 zjU?=%+R+};fa7LoW+p0bMtu+wN~t&B7STDWs?0amV#zzj7;XPKMSDirm9;SrLCVYb(C^;UAMG+xI?~QsMpQRNoM3a}i$4XEZQHH)H z5>BaaatoGf+mQ9z>~3V%f%?d^d|xt;0*v^d}BJR zQ?#KzoMrer9u)LMRe+?fZq_*HT}~bFnGj0ze>CF0Sg`jdJ&q~%m~LbKDO;fv`h){g z){sWlE|eXG4-cmrODVT(91r2nG>b{-`hG{dzC+gBcu@&XqudmaF9o9E72^(6eFck+^8P3{(%uaDiBB=19BRx_ z*`PSNr$_&|>3gn8B6=>thhAf00=w({Dy7oC<$T7;1y zzSt3Giiaw694Fbycd(5XKFgv0e%SM93&qP{YR=^6=j$06^|_L@!H>PShHT_bPaWeK5Ykff$KE{@A9G!5Fd|LB^5-${Y-OL=vbJir- zC>(LtY~xSEC0uKA3r)*!;KI;rxLM1I5$w!Z^|V+%XaG(&v_p1pkW@fI2B>_*SwQCu zj$+zYP?23S9eyM}7@#TK9?Sifh(G`nP~0~uch8ZXIN-DLor(klAPDQ4$j;!L zKXN^GUmiR&Xkc%Dg4AHT5Xl}%f(Ry~IHMw$hbGHWMkJg?%Ey)Y>l8!a7L+|lGC1UR zCat73<_yS-yrphz>lK*PQnuHb^0=4_GJODOsuL!(WnE7>Q5u{RFbBvUXtn7R2{K#n zQCYd=-q>+$HI@|xTqzyTS?W7!Zj_W8=#T@PvVFDLB}P&9R+;z@urJ@;(5o<2t*g0VI>3smjbe;=9!v@~M5YBeN%kTS3e5Q^w?Kdoi-_Y|pljj+GUE z9e#rOkc`8MEn<@DcrV8X zh4W%j9>&8XyPT)*)iX<{xfe=IaCaolPIjyFJ!q>%B!{4M`G`*vr_tQN$Ka6HhYm)9#O#5O?7u(x{ z#)2}i=yCN*>kHki{QS0OsZ9LfvQU&ZkxEByT<-oE5rhvI9mr0@t4DH0lB+9}?}lf~ zws0xZk0hOSy|QP5pMGw+_I=i}VvubuQk8+56PpCd-Wzz#ptN0Xr+D-sIDvs!R3OMc zi;}}JTN8$b~=xKl^h=wi>yD~y6piXC8bm?RgQDNq;>+)bupTMR?I8(wp zA>=HFWFV=pzcp&tm0+iYpSyYQduel7D$`M50=n=`Ig_D^SkOy~v?5pbZUg+Bos=2P zix6f8@D@h1d+35J$Z5dP_+;%FboD6bTI6ITCu+)t2~6NOBsm}#ZFNI?>dY0+dJ+{i zYvK9?Z^^rmhUJf_myp#WVk|@fedJvilJP7q!1mxbzIF}Y3-$b5GD&23pq&0;vdoe- z&3^|)*_G4)`d^N|PxxP+jzcQZ0Y_|1fE>r>_PU2BtDRytG7NGf-E{G`Q#rV4V;A$~ zP|u6NBfKFJcE5QQ5ZWi!CF->1hA3($W|E53L2SHHl!4y_Esp3>u=U`?W)H*o%g*56 zsOAG~;-bsu!l|oeqFc*>n%+*^86it}mzxsdAVq23a~6@X`IgTDD(v2=^*E$I9Xm7; zJpqWs2s{#*<;y-z#q|a;{CuV%IDFVPguOnRKk$JZ`YJ7J!d_y6%-X$oHeXkR>RrK% z%*YExDEk3k0mSjn6a|r4{_N9!TLatx3%pTu?VV?+-5w))ex#ahzbV^Cv$uEGZuIYz z0JzK|aL6LJ7+;I-9)|H90IpS)lEto7Rm0DFbBSXehpsanH<4)a31VbF?W*zl)G=PN z$IcTpE$EsfpjXH$O%)&$0~dVL!s9d7dR{|uVuXq?Yw7P)$aaI0oN6Nqhwp+X5fkV7 z3dJ5B;MBrZ2Y8Vy9|nf#j^^GqILz-p|LwK<=UL-oiwH-#B@=j)kG%U5`#KIz*d_-G zcPZqpPIhc4X$8Z@nm7BP=-WUuMoy7mnQlDy41pTPsZTqM{&;P$+>Xto19(UC`SZb2 zR6Gg85nlm*mmm#gXW(H?wMEYutD6+YX-TEyxRyi{UDzN?v-w-U5L|I6=;F(aNSW{c{l}yF% zO#!rFp0mT#+a1mxd%+lwpg0al|1tsEepb*V<-fPtpU~y8G8`(Dahn}Z*jO(@%3v%E zuU<_!=@YDV(T+Y%pAZ?tM6R3<90ThKncbms(DZ4SD4_~iDe25SX44u4CtTSJIX3#b z%0oA$zbW!5cO0^j9S&uef>^NVg__IL)er{>Yq?Y1P^|}gv}jZH^Ysr8gQZa5jm4SU z3R?GuY_~jEv#Us`LG%LHyfVA~anJ{tpAU>a94}#c5PQ)a#Ekwj|Hs&Sqc!{Bdxv(1 z2w?aE0Hs%?aDLL~>)Mx!i1p=lQBnye~n9EQo-%R~w0Qnra#cc=5YgKoxUect)yoJ69|+JCUQlVl(TN zm1pMq%Zu&yUSal#{;O5Nl-aR(s|hID9&n0<_mA5xwyVZRR zYpJC3Lfk{oN*QT9VV62yg5EC~R9u(1=tk?NA&YB#2jk;iA^bM35(u z$7l%7B$Cs!Pm|8wsQ+v&dpUQpzpBD^dZ5{|W^mbJ-+1oea~bbT@@>4w4nXr@yc`s1 ziFHkaSzZL(^P5V%)D&+(i38MJGYhgc+ldzo9{l_iA0xRZZFNQQ3Z48XWHpU(ji5RK zEn5xW>g)XRCw6r#VNYXxcTiocjcw1gl_X$KI9&Kl-rb#2S`ZWmlV54$NjTJL%;zGH z3~60#D)Y30I7!P(@#qD6lo=I^4^W2OrJB)k1&@=n0T2cv4w%?3mlRiS-Xu#E<2u z6L&W%=fnzCL$>5r>${7#&eu$?cOmah1%0VPE09p(%af6gWfCkFp5r+c&!y$HkA33J z13ot%pEc|>Yk3yo6x$18`Tla$+Vcx(ulH=_?HcB(DLje9OMFbBWA7DK^09XtvGJr- zb9i`|GAij)U!m#RhIE(Lpz2FNO99G$iHBVul|@bDJh$vNPPj#Z9o|*WY9ZJmd%)#x zQUj=x6z|n}{OD*GhUu%ucXhy3{7xmihp+weR?9;O50-~KJdNP7Q&GL(I}pZ%%l6fi z68L!Kr;;fDYUnGkPv(coO-q~EWJMT|$d#0Op%6qkema%PR74yI`HBoMvB?`{ryv3;zte;#d1s(aRQj89jrAPHiH06>x>4vQ3R zU8n(HZ{<%*}i0`qiK?_sD%m2jE$gvk?9 z31cZOxD49BZ064UjEe9jos$u@Jtyi`eGg@S!qzaKDyo~IJZ{E5vWeQ*?VQq_O*gMT z(!Zl`wYAj01`sGf(g2VIk^CazGAKCcEK)b~Uq)(FIkn&SnCOs26|G;rS^N%sf`Rr< ziq|7Mzk_Jcu@51uODSX?}#98|s212~v}qpA*(@rL-LO$-((54g1X&o?VBNdxC6X?YYlNG0t=en^L_} zBYw0Kk1ENc$kmnOMBGDx8;2kafN`N7Q^G4;YQWph7Z0Nd>!Hpkp7PXe_Z%N~9!hcJ zv0HfN4u+ki9F-BqJ4Z&>W|ABx0o-0t86>y0+X+(CMimi9iN^pL6EWy&1c|*_u{O^5 z<|-6Tz(na^zWg9GwyRkI-Ps)S!IQ#**@(h3hL3d$sW!G^9$7a<+kG6lL zGCYknd@V_+U|!p1*8S#HlV@xq$=);>wMU*J@8+5VpHKpq;y;{W&YkF(kEwZCP=r0&@RY!NRHr)jC55R8Ry%Yr^5XKtGNev|SO zuH|f*&p=H*o`Hlve4(pou&85SeRtv6v7q|Re)C|^I$rUNYBC2na1hC_=qm1UfqT8S zip)g6*-ToAdNBR>y_;~wb2r7qX=GE@=Lopp?{v^Y_KQQk-VfZ_@M7F;jKdSqq2*E_ z+`W|S$qG`(@!Q5~F)Sz|gY+Gx2;)0MTGAA3QE1r^F>=WMBS&u9#9go`X;NT5N-+|Q z$!5g#tKqRJi$%5jXVYXmN(f6guCapgT_wU!Kl0{yW^?MMRVkj9_BH&IAJVrZmtH3d z#z#J7$;nRv>i0(iQD?bAxVrzC7*c@6WS> z#M(bmz&NUV3$B#f?|Ij3_9=KyMwG90;2jptRw}~>94Nkj|GwRBUOw3b0!o3d0?8YI zA)+!3-O`N{wiGv%Y$x3Em#2*>5Ylu9p`3OVFtWBe z>?M`W3%c$f?5o{A#zyJ3fYFMq+WuSD%Iy{FQn3f>Qe_z|yHY}$FUB8sqIo-yY3YP1 zPwY-#l!NM5py+WhwW`b)ppzn>X;)r!H#IHI@go=rzul4e-R5Ozd<{bSanv5qMJ=ro zr?1CIc@CHZKc}gg_kID)k|N|6G(aEeyItoduOHrrEn9)~p$jpw7BGoj;acB_z)ulK0{JI?=G_7$V>b9AqOfb@C5lK zGSFf%VIkef{5kFUjTy}LW4wk$&;xg8FKUsx*}q|X zG}ieK%W!1({(cz;dJ)nD@Nbq$I*s%+w%OAgc<;Kzy`~JXm zS{3*$HUE)E|GBrTIE{8YLh8f5zb}knQB%8fS^+$BwP$~ZH=|=N82&^Ctvn&!Dnb0T zB}Mltoi-%27K zeLaG$nz>LV6%@eONn{j8>>hQ!la0R*2HyX&ayNed;o;p8id6938Ix&8XVMhjzgOXL z&(}#Gez89kFv7?8K68uVywLxyQx(06%eTIxdCxsxD-%eje4QDh&ZuN*L;2H;aEKt1 z{yC85SAT6&uy;N{KV2w3$h zNp7J^xntBpf^-G&&1u+xmzf&PZ`PH$#5FR+9rG{*;`2q42k35cfw`go;Z7!1P9CD> z^>@j4&SaN_DEz?%Ym@_=>VFk;B2i}vtB!@n)N}KN!e+?>=z-~B>-qN0UkZ0kYZlB4fRePx|c9H|;5QVLtdzvSi^2o5Cili374eKDVY{S`9vtpd+V)wNi)ne5#gkeBmV#T_WS;lK`r z8*W~5-@3Jza@@Tz0K(@mXL!iWqXj7QX&zpLq-X{2{zLdFCtK{jf&RQMfvy0V>x!>D zE|yVs2lATaBAjlVYCCexXnkm3j_y}T&9`D(%887P-0ycA2Ls$PIY138|{dfXJkM>&r^obx}7(HI{o40QO(_+7 z;4qobuh1!2>0gs{+I((heT)QRg%v9+sN(0$iy)6!(VG;9d{7stVcBGIZ0pu-_>bdC zW*G>_hM-IN?>@$Xr5LO{K|DZ3`X>6qRPE==^sFaKTd{ERzGl|FNBVVA;1#I1q&fVF{9}8wD zV3hhdYT*Ek7p4CnUh8{sv7rpBt}&v|OnXjo479aFT-TRbu+V$^j2j9e;6duIX4EDz zCGW3iMDgCj4ZGMCk@4m39B>^Vdn54kIgpL!wcquG46gmBZY^6>{D)WiR{xdnr*2;V z@2%~$pI75oy$0kJup4kGm`5}NRQgQ<7kFP2Bs~B@YyZX2^Wb#~EsVL)pW(Qta8G7n zQMOboHvk@5taF>&xoH0V;54ZoR8aIc1;Mk5!OOm84GjSk9rKBTwk`b-0C0`{lou+^ z(m`EOI&o#DdIcAU)EAzud2W3;dXr&QoOY zA(C0B)TxfD*v4@IHw`{vZ#Wx7LRYSBWRs!hw~_61El8)(p788wJKIjhqHRPr^c*_G z&Zy`MCG^t1ZN02NfzSSn?u8D7BK!tnitUCIEx$GhUimk6qMN3F0K@Af{k>J*U z9g8jL&o0{2Z&iuy-UYp5tMd*mixCHaYz=g@Oqo6$Cc%3)$Shm)Pf{*)@Eys`A?rVe?A;pA%xCx|Jr{8oZ()WZ$I^5$>*vdES}1eHtM8 zQ)cW;2QuBGmukdu6dbJw|?4tEd8SA%MeLWOjAV^otAcYRHIdo1(>LKQ3znL z^M#ILp2#?{RzeuKM*LFX?kqAfSvG(CrMmZt%x)jaM6NE7+}8*}gQ9DOvRE!&6htr8 zgkCLtpUZ>l z>oPJUKXh_Vh5m&Jj&j6c71#0Ncb^`y71s^jAUmch$ucojK>)ON?>?xu7;{*+9Ltv$ z@=(*WKC9|~`rWAWY%26>ILdxG0kyM@oeH4o-LJ)@QJDkoYRMl0XDUNOFS_!FCqB!%xsZ2&PYbN7}7iUL5&JTE!7|KB8kktU2!oE>9 z^*>7S0Db4D4o#;U=&*GbLCOz!((Z-9I&T}jI$d@njf~^1DB>1x zjJLQLtEgE=%k2`Fe3{nzkv~nzFP;l8h%Py;JBww)rz|4#?8FRI%OaNo5A;iZPmWye zCDW-3lTA}hw>~}49bID+QBo3SLdxMT3}6{!VL{9KK;OiKU{PtQZdxZ^6<41KDkE!2sw8=VMPyv>6Mg1PZKNU z9n{zs=IQ4*W6avAn36-~9h-Rl-~plv zd8PXT?>ZQMc(_UW6Z@TQzp2|mZo4!bn&FVMe4(;35|zaW^NHsI1_?0jXP~H*vB7o| z$+x`?9{jj1f~HnCt{1=4aW>FZS6@$=trG039ssf%w~P_GD>97z^vg|`ys ze|CygE5f)DWYU87Au=DNQ5#n9g8kKl;D5!ssP!q+z{UL7_C_lKDYKyrAP|ny`pKKE zSdh%XI6GZWM@wDbre=MmmIUd#uKXeeKF+A|UEQ0cnUj_3 z{~lTjA|wpn5e1%>GKY9fEr?34A~RF8Nv%zbA; zi~6&LAg_>)`ycxJK?P=F+;iUd3~1B(=I;$eU>`m8C`)R9$OCK?7NMoSrb8+e-8&S< z1OS}gE{F2>+_KBHaz1ZCBXL&%>dgn?H)s3UuG|XgBxc z9F!35@bP0ISO}5uPmi!(Db1l>J8_>*#BXYw=WahdBRx;%YyuNTg^1ei45`&b`7mHm!BcQZdqvi0cjxNVBLPGQZ9*c+KY21bQoCu z(AL&Q3T9_GGkXAK6%BD!RaHvJ&{phXc^$VqwO($*`Bf&^gX8!a0d>Q)IC0p;o%!q9 z`_^5=5llVNpJHQQp4Od-?j19?ymvGgbG?z9&`ozt!Jr5pm&IDhffgN&>H6TfARtX| zTJ$*cIBbcMT$iN=el);o^BV!Q!&yzi-LH{w2K_S!jx`}cjQm&7sJ-#(NI_g*UsYEL zs|r0d>D5(UB0*n)bRHZJHXBddCCvHUw_*2qF20T=%_g+AAiSz;C0n>~vUA|u!kVMY z!csv1kSv4m=m?-xLK3&Ek&FVHiDA{LgJW!llw`*HJ8!Eju&P>t%9VI71wUXfCV@a( zfzr$k{UPY8s;fmMB~2?C&pTCPcvFPY)HZn_W@#}q1-Tyy4z>e>-^#1ByqH>NDuau8!(v8;sY%2JId zO`oVy`ToOzK+9-jwCVWK)0)TXd;bRkSR~YM686{)p+GSe;Q=y;u>c@amFAH#J zUtmRMMoX~snpjboLWLida_kI`_HKN-)_3?(`ShI0xqoXR^4aC(k&Ard*rJC#hWq?j zd+M4k3;T@Tp3g~@!C;9$WlS7xOa;cvEcsCW2BFTaJL*#$Yj$_8(#l+6>51-BShxciEs_flWvL>Ax4Vf$j{H;`B!oJ z@Hne0dL)c8uXV&B6q`3H7eN8)n0h+;689f=!_-~WGV&PAfO^2PIsRR5E-et&a!kC=2SCDKWO~4((1ejE)HtSGKt8hb9Q!4>QV&jOc-EYilBx&F@c3CE7JD8al^^{ zCvu?{PplvD8$@`3b#gEIc!+6QT#P8%cf7iO;Thj?+8dymVw0S~GMWd~mF$;%sD*3i zmWKve`y-@{4jC4XtcWmPaew{Gca7QMW;V;&f`WKnGn=&r`7qb)snGUd!N1UBsFROM r4`&8ny?*`r==`3T|KHzPOB#Y{Vr}VzI7mia*g;u%k4{8 literal 24072 zcmd2@c|6qH`~M(>P|A{RN-Z-TS)T*Q+__JkN8U_5D2O6QHG`Oi#;B3qcV5MHNMD2qFjn zOAhU(0)H$!cdUays7zIr6(J(&pQO@^2nad^T~w6Qb$&S;@AC4-l~_Wej++xd*Y4i^ z&!NL6^1B_bJ~P>6zVEgJn^py-V?QFKFDP}gC@7?_D0Gx|ly_u^@BiEX z+`6UK!Eq?rWL)TmgxHL^#&Pm?dfOC>(!~Hp%r48)cV6en^2~zeXuE>QgiNe!+1p=# zraSK1(_Jt7`n5;3+uL0Ro7`1IgDWkUi;m$?UrQ&}d$?SYm*;=jTQ}fyM)p{`K~k0`U98!L(IU6A)p-;2v1rE8X4 zr>|NMs}Adk#SiFILIQ~32c@}BM_8Q%a2)o_eeOKr5*UkGHeRNHG9o*#8hOC2_tTSB%svR^A|wsewX_>I+$XP04)a!N2=_ScTohh+K8aO zP~K)jhWz1B`DCwz1gWP=!Oh4IIG;rtCx!7KW~_delEr0x%)mP) z;m0lyHy{qxvb}1*d#mm4tJ7122}U8WD3(D+$q!!>tz6cI6^8FW-$P05^(0o@rkA|A z$BE6-h|-5|`65zFAgMTmADW5=+sqw9Z~+o^moYGvsj){^eC^_UmZPrs(Z}>j1)}qF zT$V%1v6uXODP(swz7RX!-r?Rq6G*uv!*;rOIrNRzm(=27y>+8QZNhT6hCUs_c`X7) z0Q&>J*USs>XmvKw;k@HAS#!?Hp0e9?@#6Uf1*@;;BZ78Qd+oh@_wGR-#rbTN7;MFh zH;_pnx2vmb%RqauX)6UeaYAj!eq-=Ffmk)3xNnCdzH++gdFS_7W|t@eos3s_jEPpR(IilOy>;r# z_yc7|#_N4lLw>p`!}gj|i_xWJs}}`m_pDCYFUNV-G8 zl0t0{_IY(;^F(WJZ|_D|>0yGQ7Ga+}&VM)1-OLj>EPvFcl*8REHhatEW_okY+gIUN zb8hqwTkzjjQt#;zHV;bd z);HmQyV>oS1>2BYMxD7w%>Z2j`flZ|9+OM)aNoMoD0*^IF!+`K&0 zV=Ukw%4aDKUL@GfWkw+nT!9R> zGJo{DsGd~iV?q^{^ID$x{P}!qXQ%YCX*~*CFPpTj7MBTQUv7W6V`b$fO4VhWC>l`s z)^KG3O@2==0yx*Onm|_ZAJ)^SyQ?7 zL-M=iIr^2ht3zywMaYBV>EH>7&&_C`wYBw^32IagdUqre zy<>+>tuL%C?Geg?QSzr^*%*Dq)nd*#_9YB7XVbVGfA>fa^1F${jJ>Ji@)d^0DWH~t zf#9a5CJgMGDsIE*;OH|SXq93qnoFZ zd8S*c;AG>8&kWRFo{h#Ovo!^GJgskP3b4@Kv9yE{A`vuyZz4xH$`r5*Y|`n}3@wpM z-6(xHmx|b97HH??4{gLCZF^@*csyx*T5;~&*4A@l#%Ss>;cGkP!CVi6-U8*Fk~*XC*0%Yo|iHjCEK%2*+lF?2jBFrUz|H0B=sU*6s-?abr4B1dWPF2P-2qytKI5-5k zx4|Njw|@Hw0fh1HG2;~A`Sq8>zTOH`iKziR9Q*)yaJpwD8qzTb*$(2q;K0)_x7tK#P9ypsUmRVX?{z7@J;{bqHf|SgErCST9l5kVKKGTsQ3O z+rW)o9W_8wkLXVBS~v~82?+s8tEX3QOj&Q7`Vu3Ix{>|53&>etXw@z7(8)xJyY*FW z57F+IN3{KeI(9~ocLudVy<4_E>S4fD)4>t@{G8(Sdf8K4i zGGLm+?#T--=((_Qf_x=`iO)_Oghpx`^i1kM7w2MxTe}8B`2EfWSdvIkSKCHU`5I^FElqN5=k40yl`&Jxa`yQ%cTvl$xSG^px~@?0%g#(j@Y{B z4)L+wVlKOZoYHJWJZ!L*mX-?d^o5LN|MD~p^+c8Ue1QWP;Cw}Dg;kX0|; zmFK)TLS-N@cq{ns?EHK>K5PCTw|kjT)jlvVfGx1=i+r@Hq^hc#s{L_)M+X!p%!j<^ zeJw#xaYBWk@RtGeTmbSJ91H>J3DbCE6ZZz?*qX|l*wfGny61)rF~SCx>S!_so3)z1 zFH`{+%@kN8k9L7#aQpey4D&6T?KwIPlw{RklEuIgINs(1UC?cJ3k&McpX+a|XZ+J} z0@)gniYMa~%*-fE-cg?wvl#g7@03-zC-#LiO*tPeYFFc${9#R;4q;f~xB6Zt$S|E~ z>Fo4%FN4_BypFjml$!6V_Jm>zZF`Tu71O(J*?^k$|3?Hx3g1&C9R1$6F@Ma=c4gMS zsaU(z@k{mkQLpFEpNp!9NzquQM|w2C{Wutrzm$>iZ-O~QR-UqlUdRHODP?Bh)nVE z@e5LP6dx95KQtF#^{_m=xA;w3+L#Ty`m}Fwm+5uj<51S@8*+ay9^S%p;Mx#qH2u9ckf}W_C%s2z^5` ziH+H`Itz}*b{2{Gh2bjT@|9t){!)tb&k>;B@f{y;g&O4yW~K7;^H*H1nL1Zb)5;c9 z+_3Q;#o(mIazFmbPLRR^2uL&{C4zPs=U!VPl7O zpCc@K(Cu&6rOtN8e5dkBKLX#>`Daw49D@sO-e>5zZPk3yvO7n5uNuF!sU77Zalw`Q zY-hM|Qy?7tNmnBQ8^reqXlu$oKy2pf>xLDMZB@AA(1{Z#?!IDA@D|fyfXM{OmxC zM5LGReJ*`Q7UCrgDoOERgd7|h%EpBM+P>3a#p6`A==kRF!yN7mYOhaA&rf)Zez0!k zaOj5D!{vXb+a&rC#skI0#ltQvP?L;hlS^RbfSjX*G{noaI+q8cX3oy2S#R7?`a{U< zTVK_HlB0wqM5>HGE;gdbWSyE@8;FsJ$A1h%FX4hwGz zyA)(l6cAsOYT5ZU!z-Z?6kZpH1fj`!8Vuj1KUMmJ7D#4`G7XuP(kxe3_@UjkFS5u7 zA~j!fKrBM~u72L<>aGXM-&}|%I{c}3BO|C@lM12m$salhhb!m?$cyrNlaxipL9+Yb zSexh}X{8M;i>xMUhJPKT#jBA>22knLU!>4A+@cTj2DX6Iuv8VMI5fpyq9_>0Ia7J393e zVvxSi*d+EvfP9L!hU2?P#1}0B`&c9bH-2yHI{6dV>&353azM}c7MW0Sw2%cAh|zEa zxrv5vKFIE91OA(ptUlc`Ew2@b&aF1qBEjWRbM~9jMyRDX;s#aufv4=z#AClflCf80 znI}{59=zvq9UDE7lut{At>YA3JKClx9#zDWl|!n@n05G=u#F~eMivymFVDz!{-n(n z0kR+YsrrHy320C4Nh&XSxd*t!|75!qflEcn$(rT8T2C^uy#*DWzq@B#)`9ZBg;c6W zkf^mD=hW+;spe~nCHBX%q*p9Gl|d0T|MRAeP9l=91?34AWIeU#?8m>omeGarQ+e?| zvnK=Tx4W*+e=1aWI3mClb=avn=2Iv2%xE=aB8|3M-Wbune6Ypu<7aJx;NO?wY6!Ss z{8SJIFJ8Cxc*zDaU2GSJbnX8a8)f)bBBv}*1zFPL{D*v*pUHs$e_=K|6jg=|mx{ew z4nY*BKdkeu7l}Y@`lsn-QDgz(|7}39m-tq+V4;QG1N59t%25oN$ts>Mz2cCibnYW3 z=)cW}@hYLb)rp+V%-$lt^VFMlzt4&?(3$cCS}1{QY6VemJeYv#2Juq)++DBCK$7bL z242yCd_1wjwM&g9V?(v&2!q=-fBNQ-*LxsB1}Mb^~pD zocx~97W(?L_TPEHa6PQ`Al{5)v>TPHl$Fv`SeNX_)6l=_7q6tX<6 z8%k_=2I#*DZ~X!*53ia}0Gg?9S(k!hO+#x^+~fOSWVC*TZRS915u91k)1hCep+k1$ z>mHW@vicu>DD*0!)TPwBb^##y=(R!O^?Kv9pTOvY9o~d13=ZWkZ#JK&_Yc?giEl_m+VkgB3|tKPh@Y0mJD*?V0~4$ zcA?>29C@)5DayVbD&LUKFxuX2|@%j+_6AfO@rx>E)Iw zyhQKbF(OOC4?Ox^`~7qU#m5nDKMbK(}S`%(QysZW$T{Eg+W z_NSY*(PShY>kdlqzjNz!Fyu9#!0^$D>w;VN;o0wn%#v7}nvhb1ys6ydc6p;X6Y;qp zI_MnyhPbX{q9zXv>i7p84=xhgGa74CdZW!&mz82p5ggcb2xg-k_TTO_ikV5Jk3?Ry z;8Op{BDs3M;&TS9T4CmXO;>#O=eUQMH=+J`!{oD6|Bz0~B$8-w=GKLqE)7@o&tfmB z9dydP#9_84T|LJ8?*7|DZAv!fAs(8;!dad^ZZP7}^TmDUTYE+04cBChJU{O7UK$t9 zhir$+$q;0bNP|Dl;c^4=P4YojVP`I*f}wmsp8tE_kQ{}gnc+Cwi6i34sWV!X%pjmC zb8y28u-vkmIUYRP)OBvqKNx5!)z%4xx0*~X6x z)2=Uv%d(r*B$#m>ghn=)K;Fpmr!0)bB#Nx^maH%*Rs`L(uNtfoJtG!WVJVE;%Th9F z>W+4TRU}X}Gk`s2hPup&1V!RmS-2MAzTEFBM(G%Fsi|alBHlky=_HZRD6x3MeL5Ri znQ|?9WkUyG>P};!`hTg*)9DSQD9w$f_lzSWzPWU6)Xc46)|HaEc%F34a~yEuC~Ih` zaA1Tcun@VD9N?RFp0~0Ik5wC;%f**`z@VGc0tdYck(ebFzID&D4wyF?`1~i$knDCR zWgB5QkcLOOt#s$zSv5a$MWSk?H+r-*m!Qe3Uc~z37;zz>?ysK@m?M0|jTbLA#@fmC zNba?7ni8(GCs9x zj?4?=ME4ta5V(YEhR=-XmyE-iqEC44XZhyoVuFiAd@Pdui_K$1n!3a%;>2(KRfBRS zC5xC)v&zNx4*O%oNjv+$C?F|8$x2kBlEOoWp5om>ezSK<#+Vbi78L?Ay)jM{*uS(| zV2lxJ7$?N*h~rvK$1k3^H*rt^;bd$1*S6OQ>h#}=)+@38rGYKZE2_kU!RLCT=z;s_ zKL|QVJA6{gbGV7!Vr93P!>HX&H1C-gakV4nba%y@4QZBt4 zf3p48%I?&|eY%cZV`#osMv!Igu#uBrV9@iz8@{zS?0br7;?w;^ofkjpUAdxwD-LXj zQ7Pj5)e`Z+7foh6SZ}s1yRb=`pM9Yzu`l>rM0ouV2=Yxh>A?9tKw%@XhA`1}6Wdjn zhp3A%m(jW57$K-iF0_qXJy0S*c@9m-D+kqY-LM@*jfVJp>b735M_GfsT!0w%j)nf) zsHlB~rO4*XhSnY_yPmXIzcZ)kQ|>zMYs}cr>J~_~wFtw{z6IC53?#rb z;OHn7-qU5LUcBHpU41V;sx*OQh=UPNboFnlJ2-J`_a6lf3E*!pUjSs^SslAMUrWQ% zT$VBmv{;Xm;~2<_BN0U}rDn`y5IE&9YtGbX(JHq^>0-dCbJW3PS4D1tE7Rp z0;qE?fQZv5<7lT}Z0C2a-Jd9_Ltw7Eps@^4S2>)&GEaE*qlVnNwk%{4AHZ=q>P)6O zjms;MI02@w9}fk9$PeJp*Rq6f!Dgc7{daH9eL1M{Qo{aAp_De*=e+l63TO<*{l8-})v>t9u zV&uHi4-x?tQGt*}&3<>?+qKrv_TyM`6py=X(lV{#?B z&Vjfh=!5AV`f*K|ZtOkL_SJ{fJp7B?k+)06#coWl2MBs4Is^0PppaIOw_2$~9h{Ub0Z_ZR}nKpdG3AKpJr5a_%nxEOA9?{5 zmZev5{l|rPul6QK^wqR0Tnw-9%1?lL91d%{z|Qq+761mlKj@>=RCZ>1 zAo6HDmv8iq4adf7gpdkGZNd>npaELX5PF*awf9XgT41$E2WUu~-8x`H1+_oDNxt1q zhi5=TiOuJnunRQ{5)+|}=2XFj?gfau{31WGDdFMNeN$CE%|0)9GJe!g0bE+{Bk70! z`!!=>y;t$J-#_J6Hw-!)c@S)-bS@HkDv}uX(Xr%EX6R}EP+YMN!LB-dOB4))!!$Y; zO7EFzMm43Vqs!*_CGJ;FF~8*oh3g&wVHB^zk2F0wUc72Ox4}FH)8k|VevH`<*g%lX z@T-=>l#17*86(dMH`(|TaL+QQ1rX){$e494^Q88Q-zd~3h}LTT5oQ#i3#w*|2NceW|nw{Ck;E7I9C~90rQpj=vU)E-h%9@cW zbz;Et3zbgWo1^MgaK{yJde!O3oX1?lD3cSW+5YN%+*$7hUJnHIhhbnZ);XbRMM8e{ zG5eEMs;D*TA1|n2;Eus5$Hz_TmU@OK_=jUYSa;Ww`9sPe)I3|~fTq<5xWlL{4$a!c zGi$boNUR$>26kmLzOg87`kc*pwJZk1D5#?5K@U0dA)rZ#68@I#$Lho?Z<1({xh#x1 zo29O0Gg3u4mB`a$Fx@cPmD5m1GM;Q`fne=zeN^z0KZUXG@NjuDXim-n#H#1>hx{A{ zius;G%D#uzY%YgMi`Z90p^i7eN{As{vIR_HQQQoi>>%?F0bzU7(lcYQ;mW)lzWa!9 z3f_lSJ8hNP>-=*hW!^@tGDUC5akWJIgGR1~#875#nyp2!FQuVGCN4w&4!6%2+$QAu zH>z{%x7TJAfVI(ZXtuBQ=_+f24(gezngmX&1aAnhT8wDsya{}!jWUi=$-+sJu<+&E zw1?qnlfP$B)BLGKge;Dl`M~acQv6k(F3c%auvgY->yV;}F2P+;i$B+2M++*!hmnYL z^UiDh68)aCrO4x)R;bWo?oW5uC?gSCa^9EYfWjj7aB#05d+K{X>cl6t^OCfzM!-m1 zzLtkFr;~(444<4l|Jb;H>B@?9QDrl6LCL`Z#=S0A4}YB``=%7AFHBLd&(q2!MT6nk zK_B}Noc_%%zH=jyZwd5g;TwbRdG`bmoBA{%TYa&$LA1=HreRO*ki$TNwpD1E^UEhfj}em|$tOPq z*#S~$Al`qWCp&Q|KdtB#;g!8@Z{K~)=DSMZJyqWB@*gucZT7ZUL=mxeX#M4JU23g!p-K8hqcvY#- z1jeG#WZ3aErMm3n1dh;b;^+{y0EvpD2Un>0G|x>%KmZXR6ZXR7KAk9dTfVs8!lZF+ zN+xsk;;E>Ly6=8<%510r#Q|~u!-NM)p&8Htxsp*M}=eIh;pHa z>w=yihLu|E%ozv*4XhQL>%|KSCAr{|JG)(!q!wmEP3qE-Yidu9DKk(L3{b=6r?d#` z@0d&l5apHE1JOjne*F(PnU&=v7t{9AqT#~ss_W>UEn0aFm+1kwfpRt?>${GYH(S~j z#I-nnF2APdtA!~@LwepV${fHb;B7)M-Jwx?b#O3T)XIX&43|Wi3y(&E!*kb+L;CN= zi3{-tGEW}qNvx998=rWcoJ_`2UiKsxswQQ3%-0R9P-_;;4uv-{Z|H8Sl?YQ^QE0b| zu&aNm`p$C3Z1P^8(=u2GWY9Qfyq=xUl67_<&?=qmw1$*kX(~(C_5Rf%?BZx2oIVte zqh^nsaiam%T|UXN)|bs$z9W9RswQa`8_L)jARId@pa!3Kz}dfahGEVPpz=Id<%7Q< z;WG(F-WE>Q<-HlgO5fihEaRoEfga`5#WNCbE%-zt?}g1dQGs2>NHKP|eG&%V{3vcXFqi zQ}B3pcr2H_1NnkN7RF3Ul>`nf4LIMPn{#xLSXc`8p6PeziumIVc^=KT`wOSLi?EBv$ypg@YrdpIv^ve|RtHnuH$U_9IOg?{ zgwNw6sS*2bS3}OaRNJT4WHjF;%mA13kRGh!0|(*E2tXUW&qotwH4@zUK(0vsLDMkk z?*68QO$IJYX{lnj{arGpfoTNYy)%Tq17M)&u zlRPi*;BCz`+=JDUusk;$9f>m>(jpKH?(kEsDRB%qbKXvnV$`XP_d7PKS@mkahMdm* zJue=lhBurchd#TWZy9uCn=_<-XOFr%+mAg{fF5`R0$$bdJsBtnqem~O_l+zm^f1Ue z;EEUKW31VH=!`ba)n3X7M{<3t?h-2;u0Xjn)yQK-5gNGTvN+Y^z^fNkZ*}D2hg+bb z!2x4{;J-;qrTi?(FftEA?$?wi^og7uJJ3E+!YS<5PG9H#R%9#}AnbD|uOB@Tei?;v zqA3La53VQ}fuPBasq;Z@@^Tk%2Bs9&*Mu+f$NNMhXGn==Y$Ou2u0IyviTbEWHHK!I zCwaUly8J|fmGi=F{M0!15Xw>CL+@leDRkhg7;Q&lg{L<5@4#XbFrnwaxQNqxb8Lx~ zFlIdR=^Nuv69?*X0fl78LuTr>-U}r^zUPj-j4nCSUmJ-evU~?vt)>8iHNX=%1qH>E zL?+?c-bZ~%w-XO-IzRc|w-TY6xUr@oRJ^`4?Ea8^42|@C$TNoCUyr(y0)s9*#&pYR z5{^1cEKr?UmgIcRu;T~joG{?UxR!z8I7lqCo|od37=HE18CUGd-Dn+g3_(2casM$y zq86co>WiH-INou$24S;7T*~?M{F}ZiZ+LZph0$x)OSlc213ky}@|Z zNCxWrAlIl*#%~t3Xn-D@JdTyNAX7@^2jQN%b}qKy^LLB!h#dS4h=(&~rS%mciV~g-^MUKf^iK#=V_@^e8TFNw$HaKLRcREt3GAK&EMe?NGQR zkd8O+mTbnz?$^Uex0m)HKeyhGS$s4h&RN1%f`t5TDXNQCF-CcSRK{QUG^m#*oL&aF zrw1yUa~K9%=D&AofH_KoSj;{)jS*g~oIg=CC%NJ-u|QWxy2#%@n+HE5;#N2_cHej{Dw}mKZUSIL#VBGjT6Ix-@XJO;a<*PVUA*3JBEKj?qZ~7@Q}~~ z!z14fKVG`dHjsrPcw?L>a;pjBZcBr2Y6+lPD$)F41@Q19~&z^q)Fg)2} zBz)Qw*p66zd>A6Y70&5whZ>e;7C_u}Y8AX2um?;j?ls1xj+3k=wf`7t`esT0avdP7 zTLLJ`PxrP(QUgqETVxvbNJPgvv-t3RZictq|Dej4@Va-4DkmdC8cMnl=h@#>4 z4`$6~nZ54uQ$>LZ%G9|LSL{0?Fh#7)FU6)Re39rYL&DBrL0#+Pk}mJ zcJ3=o?DUpg-$f%3!03T6{&8G7{__5<%$-AKS6K9`=spw1P1&B7yUGXj5yA z`o!Xv;Bw=po@ff%b_l2ENy`^Ly`fN#tlvQvKp?kN(nTFiY3S9F^{-SFc8v{}A3TBZ z4>ZC$zG^$SVpVS}rcat0)!M))TW#G($w`o2CNNTK#Ji9gxqpieQ{n48bqvvI0swgz zS&uKUcO+8?#(aK;Ky>96(ID2R+``fK>3C6B(!aTjvfNnQ7bWz2TR28}7DO6g>i0o5 zy-~YiyFuUGIWSxvOJ@{3WnnB>p9BIgGpMM@hGp)a3}Rm-fox#+XrMTy_Cu9=?t4C^ zkGgki6EZ8Fl2l?n0&co3%}65jxsP!q6#h0>lKM7B?a<4>K|=tk<+8s8^Q(-nHE~B+ zw#2G|V{Gp~KK^dxx#m!7-}Hp-(7~k~MTMocr`COSBJt3!|kg2VW_1ZRxO zZQHJt?yDc4gD`y+OzBd06yk@Mfs@`I$AVR@24ja;(Vca}GxaR2vDKcQ15&JGwyI5AjRB{=-(26&nwA524lH>3d?0g9i5>SRUkKzqr`}nlBhzCs- z#wG@kG5u!sic+2WOYi88o}0+zJMbd9jlx1&CXf{X%K{w=R{p;zxqDi;yiL!843sL! z;-~bQ+3V78SKO~|H@tvRJt8@&KhnO^dJVv)OD8ucUP#S~@6GM;Sz#QQ{qSO|SxK!$ z(0&Qr6w!p~j2RWFb@?=q>FXJm(h)O>Qa{EG`djU0g-J^-MzE+~qUkP=1#Xp*Pww<> zTT&~(NT5%GO1Nx1E9Q(x*)6?yDaA{Nt`2M(%Lr}{>VpvDOY&oXD6(s{^Ke?_xH9U$ zNrh=a%LM1FvSz+b`6)~!ALxAUkWP(K`)wmH^W1gvHb>i@TsQ{|2eUGCQ;xdwn<738 z*`XUiT3h72PGHzrvDm1Ej}D^{1@(g4PZ>b@B*z76ob8baUENUs%}cXA&anB-UU=2< zt4kOCe(lEi^gPMgY;E>hpxZ&}x$%;i+UrHh4f>k#s6vw(8#v_*bLQ(`^diDKNJ!^a zV_!*W#kGgA%d}G(Rh#KncE$Q;FN%tu1Z}^Pi$uH;D8nevk-YELT!!wMv7j_(J9^I` zw91cS$%SZRPqw};l#cn)tydz9N%*QHKazcM19cS?%#<>^+CU72{_mGOLEV($BAzb^Wwwhy=l;ZR^IM_4*X<2Nr!m9AfvvTdll zEaaef?c&yW0ZSey{RO!grD)cqHOLN6SxM%bzGo&FanF1RXF? z%VL1B2;RE|3~UUTi%B&P_iEN^9!vlswxz_dqhcb!KrkfhFL>g=iRWCU7|W#s?l=jb z$_HkiWImx%hq95S%_&MyXf z00T>IJ&ch^_AO866ct1AhopPkV^Ce?>B+d^jnN}zKLD}b@AFGuH%SbjcQD|%RalU8 ziEWE-?mv8wdR6_RM1e%fj~(J-fECV?BbU& zqfIw9i2`;`|MTxhp5li|J)F?JfR&KL;cDRMwLeD-*Br%FeFZdcoz1CG@GjPWv-*#W z5XW-9!brUzfZuDd53z;BIiF|TnA-Jx(;7^5f|d@uA};!AKhuot4G@e;-fv&Z%QNMo zYUj*o7&qR4QVWgpd#gQaz#w{N|D6 z!CthnOOGbVjY)5(Kn`tZNU$}9_R8Xp>zEf0N^W@g`fuuj+HuR2kdYE!V0PT#n$0t< zy4@_0#2+`|@h$I>^z8M>kscKv0it#|7*o?HFyO^ZNhy83Zf_2@$ML<%D5vMKml{rnCGT(d8n?= z!V%BT9o7(8R1dQwc|!BdJ9Sl_FZ?&PoG^(JINnBSCz_@hmy_vgaUpj5(+_pGJk~wM zf^vPCtR<9`l}hi1ov*g;E|%aVoZYb5hSIRr;t76qEs2-c9JoaLdF_(L>~6ER z9}!v2JY~I~Mi1hL{1A21{8x4JUSOhmHoqnU{Hux-8%{A7WMQj3v4hLp4zIaa6%4UZG(A4Y}ISy2WJ$lpdAoSAxa0p3hmiV?ke zQwGarfvg3socgN|7UARJxp168;1wqX9d>g>0gNUNiJH}p>`%mrj-mHvB*<_iuJ?bt z!)@=5go8agUf_qn1B*GXbF=uEg;0&T&F0O4_~m9Nry)&oBx@ELcNJ<@n`v*|h#Zv)G#QBIAA_noMID$}|cr5^h zSh^>Lw5J$DpLTBQUve1|rYf0j(=)r@@LtRHGGP(SMUiy$(Ze?m6wi$}9A6Gf!q_uya*e~$)V1OP^5u6r;9Jf0bJmu4bk1j7PS0q#(k98RyViT9cQ!<7k9 z>v77PvtPK@88)(u#zq&Oy3`x*EpeVQcTYqF#hkJ}&vN3^nLPJ;cv&?G$JzX7U|X{y zv8W_W<0`Jv+CJb{jKJJ~J71hFJSI$6wl>zSo2bMy*4B_q&l`;i6&EG>W%!)Nxecbq z8w;69PNN6>8E4DqP#)3+Hu%%BxIiC%4H=r#O%fUFQcTAzUb>v*Q+t{I;v%jtkqsn$ z$i#=eez%2+Qp$`92R>Lc-zTn$Ip4(I2JF)$xXnMCUQ+qNW@Z~QVy}$rrV`}t&9&UZ+w z9;DU|FF!y3sStsxTg#cpik3wpjTSogYi7$tOH_Qk+g@`p5@EeBg5>)@N?I;jhO3!_ z2JUHqvkwasmy(a1 znhXkU=1!^}AlaPcH|y>ryMlvL-Mh63`2nM(AZIOp|GUr`F7cyJ1|+KHEv}9XCYCW8 z&MCt;NkAu`BWTJMOStb21EY^0KpN>yFyb_wnKoGThm{0l7%vhG21l<@jInN(-T}`_HbM z63gPRfCrJ5Fltig1M^Th5X?thv=`S1L(i9&!ad+)#x;U>F9yu@C9WE)@tALXh=Mys zB0Ppj;9El;djHmQgiPP^W>C0)cuO8g7)P<8 zB~8XfiJ2`)c>lWa<3LqP46TW|PCyKH-3TJZ7Ij~>W(QY<3aqYY@4p}qPOE$8)Ex5O zQh>N#h->g(RVL1E96Z0FlX|LOu~< zU~_UqV$3*ELIe^3oio+CZeE zJp+h9B++9n1Z&SnM=Xj+K_lNM3p{vUs|Ze{;d}Xd$p`Jho}kBx5v_+`;z7waf(V7E zX5M)Cc4xX`x>@K71IKicjUm&_nM)a3nsx z3ecD*iB#dD3uf*c3ZNkph-(c_tZUcI_iGQ*{q{UKYjKoFs0<6eOt{R1qYByE3LonQaVqG~8y0ZA2XCO=orRul7P}4zvn;JFj>i1`i>Wk~1jiQ|rw7&lJkA-NBwdZr6<^lKCg1WPXfp zmF^E)a-Y_ab0cP(dy0nbUV5%`2HlsDnN{x;>r3>YH72%E+fHP|xiHmmz_^ciOG^s` z^&oAUkxF^VM-)Bb&Gt=(cSr%;M_7-`8^c<6o8!e|J$B&fjR=m!9|evp+0H}HLmA!7 zACX{+TX~?Hqr&^n`DVbn z!S>9P@)XZmh!&@TpUOa2)G9wh^mU~xBfN{th%65IjEFB0I}Q;b4%yBe1?eIDAzO** zKE_RS#yhu}RQ)w^;!~?pwi<_|;?YCg1eB)P%y-H9oYdbS74kqmTz_k{ZL^2A=kqFq zRG~Q~2t+Wu+LGhpDz_C~jNVblaTil&5f)fyF2gfMMS0q7j*y)4X!4%wLb7QR zx8y7$eBJ;bt9&)JBzk|^>P)C4jk+1%k*KLsw=2mR`$17W(ozO4){N_+wZ|@io>YEM zwoyE#NVlm@-Iw&@H~0x0r1lKPUxC6)QkV?9uV= zZ&`kUzu+KwoU+1OQeF-k(|@4yEzwLqnIol2J3)5vJ8-!twlGx6oq#y_5ty9he@IHy ze|PG(3PHrNNF+z~J&?mN1Cjm4g~5+}F;4zHq-vFXC*BW!1ZGF~1p?Qj2k83)%SVE% zMoDj>oc}(0pnB@R7Z`4VnE$b$mQC z5NpTp#X&>YghL1A4!s08(eG9yfaid;v32{pul1!yOeTV{a%k9N-;U-Tz(v5#3fJ+r zWG7zdBAv9#0C^jOiSgeZx~)nx5*hStMkp78MEKT(ceF0I>d0h#exArpqfKr5-bBruGgJ`A>0<`p3tMT`b9Bl7GA~aTXnf7TQb$L}lF3S_UoCuya{%XGd zBG8XfgHKftTF})RI1V0pl#kPCDqu)c-+$950DGynOvsB<{hs0TT`mpi%Uc8Nnq zC!X-vghPk^YlT(NROwW3zF~HDcCpWQCDNj0kD7wi?}g+$I;a3N1NLPc8156{A?(Nz zv6e+=e3_LBVvAXs#MT_#_iy$tUt4H(f(~?3=SSQF@PWYfVumPE{QFDmx>GPix`LFD z3SZ;rq=!26z$j?Ux>BBzQj5P-EjR!YctERoq>N11hC5UE0hoS^Y?Gn9wMuvl-^Zp+E{~@;5GCgb*5h~>n7suJRgG^rvGOGi~ zlCTN8ABwG)UU;zU-^>jQOAJh$rIR8n*BOiyzHtzOEV--8|BL1|Mvb)gQkt$Dt^Tmx z;MLaSd~7Ny?flmz->OJEeV>LGt@k~Q&4LQDY{ydMz0rLJikhDd@}CCA^4`R3kM`FC zP|w-+C9kfWR}!b8x7R+HhUPohmq#MR%fqz(w05_ILFZyC7_}?NTl98S2t5Z6k!^?x zz(JFnr%-gzltcPw!9`m0fBCcg78;E-S7Rbp3T+f6dlbM+&%hJ)f+>VUKa&J37m&t1&WVRk>Yqf5ON1oB(xG};UC0Aq^0DK*z8(PRRD8t)``mh0=8Cz`%o^)~-vf6Acj z6MKsC%qxsGel_>Z8ojE#{@$1;X2{_@{W9TD@q>oXj+&Hhie8{Y0hK!=cX?S&wUIhQ zKil!!jP9PNi=4NQ5gU*87TY)U0}4Z*9CTkc_X-DXIhkJr09$k@c^M~pMiZCL5(9_W zadltGOg7i~3A83@qDM+OfE`5r94*)u#-k1L-m(T_Et~$(u*F#`XTH4K*pWEP%@~x*FXutj#YNi5D zV1G69w~c6MEK!Xwe7Z$Rb1=aJ;tcp;6 zHU%t#AQk?=MCt{3^fkyg3%K<^6B%0>+I2;KVk&%d)WpeRq`}*o7cv!t-d@k2rUV~a zqL4K+OBD+GJ9#q>oCN2beBb5i(pQjAP2Q&lfxHm%vieS-_OgDX`+k<6^11K7SKS#) z{B}G1QaUK^LOA;HY2|14?KzCRXxdSqJ0%+_2mO3}$F$|is3=)zBhy^iBB z6LDP@U^5%4DD(K1$$yo)AlXagvQ4@0hlwV)^75A`@Ii-=kf!>l|Mrqkt`a7Tmr-F2 z6_o_@rP?k!@;)uE>iMHyOkE?JP|1yx{B{yQ32rV5qYkY?Vj^nSyopfF*x($JTgTR}% zr#m8PtjrGY+aQA=tBR7o>mT^zHCD?0F`l!PU*`HsVr=f*5g0%tqQCJGMDv1uRn+%D zGhNap#gi^J{RZzM8=HGe~;R1`&wIN!W-R8hba_$oco53W!Ef?mHlC+Vh%F; zx-#izYi{^`%&rKLC7m~Cz^Bdxh|}96hQG>D(z)WbNuONKF!7Pmp*iBt=4`l{#L>mR zC?eM~#TT^_cLjFcI^~_tSEIRS)@Ns3ia6094qqRBFD~+MW#Ajv)Qd5K}G2g0X@gEzQ0VEt$2b_C28Nsr?3dvDKXk`OUAi zF9xwsEs;}!M&CbK1*41LQ&9W}tCja(v+i9Xyq+TiS=n-{Vb_72ye>`^i?%yfZ)f8{ zp80DmgOo`RC*V0aIKnSx++>n430!@8-33Xs?N|mHsF`Cla4#5(?fRZ@ zirjhRJW0AM9@V*Xi?q%w&rf7)Qsqf!y#A`-MbTUAHgOt!5oVCV%qUzVBY5H(__~tZ z%%3?9P!4rhO5-d(^tLrrvvifXERC*}kb}T< zwkL?TT?%RCse?h>VwpCjxG(AIGi7O|;(Bl5$e>b%avy-^Xt0?Xz5IGhOW?q;3-?$q z$JRv8w%;rgAfCAp%1#f;kj?6G^%9H-AX_ zcLR+?usSaE9y_Jkp0_95%^K2I#2n*Lm%taEz*6wJEC0n&H@JjA+_ych=gI+WN&2k9 zcC|8`fOcQ_OQgSoG_d)z?FWhh zI=%m^<;wq|(AxM&SK98BgwM6+63SMD7?cS~mYSI6m8D{kG}p}~#3-*y64}R+Qj%rt zYlD%>J~7;kb%vN2JHrfSyGNhp=bYy`=Q-y*&-Z)2bQ(L2Sn4`ZQMy_` z7!ti3F#8~l)RC?c9lxFlMK{e(_ns07RdMbx6KF~S65Zbk*Kh(>GsWn4L`}A*X}Jf& zXT+JcVCM{lOog5tZP8+4_K}=;?H4}Ci(E4H%rX*e^YPnVX#CP%H5;TvokoD={x?Rs>0*s_6|tR9OBNnHT|K5CABejTtAI=-@`YwgHK$J~%k|z(DdJ3>G|gK)7exUj6g!g|n&K z)n|38yo&qF=xu%BRZ`YB_~np%4^mo-UX!(EX$f0JtX_UMj)dmm|I7O7iL4HKH|$+4 zk}tvz8kjI^$7htGv71h-rSb_%@oBZ7$E`LTmCSqe_(~3S0 zal?BO%Y$yu*EhDG`}vPAvN8}0x4w)ZEcAKO}s| zwliYdUgg>tTHVz-JK-U>y_*7ipVM?a1GX`;q$5y}ZZb8hsHmuWAbT4BMFEaEv)~-U%%lFSth|kdS=$zCxy^x#E)W^;w`b zXlO*(M)>9X?@8~?BG=>LG!IaI0QcFzO$nQrn0ond!m~CCOUW_P6fVXsz2LL~WX+8= z8+ib)#!Wd^vkL-Lk_-(8L%Kv%0V*dWy|tG|F5t4+1&-yj%7{}7$^zGlg9K{5Pe*e7 zJ>MdWF`&KJm$8n$?k2#71nyIuM78*F*;pz&GZ5Wdm?{GOQt`;=m-A)5331`4_rXM; zU%IsKam&}ditcDhyZd+IzP_wDX#V>Y{*D;KYI}h~_DA2Q`BdGrzI!ZsY+d=nVGb`NITJ<7$ocIY#C2wu18=;}R2%31mP&;<06c3!Z zn%(DgTd|#}HkVAq&3@t2DV|lOJ7iL}J#QikR=bEKdDt>jH(EZ$ z|A-BugL!4%h*+*bC(iXd0z5zX!VLc8emX5NpaT@qWnpV^hN#1BM{0>Og&WgJ{N+zL z-<$HIqNm{C+h~Y|BmWFKi}KC;US-HT!vFTcYB%k;NFw>l&IhiiM@3FLI#M^7oqCnA z+7R8@k5LHM+XK-OE2&Bc&GS}QS7T+is|tKus=&6QME?szic%H4sX*{i$@!ESHS6*i z*VH0Q3cHx4#k;0ecLTc1R!DdD8%g?TtX$AUt^*@d9fnhSnLb(DqvY@;=^A&DruoAt zGBr$)!%Z*a&|O_SJ&W{nm6W9Q7ySfdV{j(T<=t9}Dd{GhBW?*ndtDtHzDt!h_DQZ& zSZ~oaQPh^&qleKyzm74g`T~lsE`J95*q&L}_XzATwMP6ws9!o6X7$FQ%~iHAQofG) z*=eEcrVd9&xKYy}cx7cwox<*1dGh+9QeJ*`-wxru4?o0Swpp(RrMn*{&f2c0;|w#O z29L})(?gWo{D($dQ3M2UYGXCYNoPn@NhExvvgEg04Ms?lUl}J;Ow_FAi^yKD$9}t$ zG9zlw_ua8&`OCEgN>+)Jwad1a@6CAqHsx#Ol}4=bKw3SQ+NpOB>IU)pz~~*AwBT3Z zjI|i+M6$o)lT%p18vB%p3oR0)|?3t z2$aQs?L@^c5zs8s#!5J$Kz-L<%U4UyA%3w&94)-dQ zejscbN#-^U@j5WN{Ez*5BH_l@HeGIlG?`moNcFD@^BvB~mh#p;aw$3IT3yF8p>`s9 zi{Eob7T;#v6s3d?GF~Oa(F{A#%cjigxsKpVceBh$x}|Z_`1yj~hJwz7A8mTllYSi8z4O|@6k`+9?nZ4%qbk!tf9gnoD z@xS~Aj^>wS%f=sdD2b3}KUzG$EW8@rj?EQH6jT78r&b(6915b(S9>_V<7nKjm!wyV y4QS{S)Ot7Nw3$i^zR zZXLB~!TfTDWB>d{9s2T%^83#}JE%VSxJ1oAo)P)NdRKe6KvhQtfaqOS} zlqY9Ki1lk;#&1>Ux-~EBfn!nR4!mFY@=JVP|11`OU*^WY-+%vIW1jl)M>Y15QL^go z?o_O@dh&_93Z!1ISFKp~g8J*PzbZWc@{2Fj;%67COP9*j`n79h?(MNHI^(Ma=*TyJ z-rVy0VCDu$NFa$7kgtX|K!n0jhJzdma4 z{r%P8>_EFu4a&Yxb!gLCr8G-a1N-;WZ2*1;+!ttlRqxF1`Y2@g?W3}Rk#zB_)L_hBFI+oxVyy-H8$Qdya5o0cjk-5o1t&nU2}%rV^N zrAwETwM2rrR_NSWGs|Z_clIoK6@-)W9`h0Y@Wb~+(uA{S6lg+zo<6;L$VpXWosFaG zs_m~AbFYJ@s`Izsep6E>O;lxNWxNmLJwFK#=|=}XR9W4-$tzWpBOiKjNO`A$3(`)V zJW2id!w;JG96o=)-o4b3ufNhHoNaEk6j#oG>;Q}YCEw&H9#Sp5BuepOR8hrbbX%Ruy?9X?b^!R@Ql!ehV@l!lv~9_ z)>5NJ4C9!byK%I(Y}%-LcI_fhhG)w38kJq#ZanM3f4^S2qMF1uknSq6az^zAGTl`@ z46CpH#5Uv~$BZ*C-FL-FM??epj;p3Kt$8!OLs}#@kvT3mc28wFBcd_hb&&B@DoaO= zA@TxtReY67+h)`l7Aglfm@Y>&Oq1zp4dmG9fik91vuW9VdaDoj@6(eEmJ2st^Gs8K zQA+Z|-1z=x_dKt;V@9jov19!7KdXO!^9|1X<62TfTFax2TARJPbx~KZUgZvobDlhL zLfzZ5J6E0$V#6mqI!^6=^UdJZQe#FvESEOU5Lj9gC7Tm+$ML)SIKDl03?J*^3LJ00 z^``6_mmxlQ?>!#}zdH84kK-5YZyu=NKWtN3H9kYgMGJi#1Mcgq&Yko6IQG8#u9{ge zy`13>$hLcX_K=6;Gt9(BvFgwlUyv})d}ZA_?lvD6w`h;9op}(|>C>mQ=Qlk65f{oKGAx@Uo zi;yD{(!`R&XSqq8+`Pjf-~8(u?SjBmQ=Xipg-55Lx_C4S6~-HL62;s`}Tz{`EJSO_&B%*RQ|+s#nDI)#1Zx z+~`pi2){afNJU51k_M)+u4&syo;RFpO~btpk*(@O2S!KMzf5n&!h5`xpiyF5lJ0fRGMv4TBx}C_4J|S!Sn-gjY9D;bq74)bDo+Z%aSI^+D)sA zbLYgOYqxxTTH9pYRNnMY?3AYd&xWdZIvb2ou^SU zQE73-g4^@BzuuPM+TWHgQ55Oz)6-y0)PO{NQdxNR_a^ZSZHccvhu`2fe%u(s2e3 z%i@9A;>*%fA7|<3pJAJ{nbKoj-qG=4M97l0j_+Yy-`ywHGZgNNL@# zix$z?Q6q0;<|&lLgR{l?^FR4Gx%IHM0#Yu;#ZNQo@d`;QhR5?Yg&TrrNq?a|JG(Gs+lQJS5v}mE4raNE17Gh@mS2QZL1|_NA9po77f% zfAS;zbncvb`>nTB*G?T|R#$wfoDR;^=1KZ&UAcUjD2Yy<$~LW1xIg$H*>&Q?cWU~S zeANu5Z$S0_@yG9~U3#j_%8ZsJ5B3tvo_mhOu}<0tZNa^CaiNBbdTZA%tu&i7Y@jCQ zJ*Li_IbC5kn>TKh8C`)gw3m42?cHRG_MxN_Wf32wm@pG)-Qm<$6~~W%D>J%*V^}Zo z#?BoiaNo$$+N7+QH*2Qm_@cB_W^@O~@Lpod#3y+6HA4ovfMHLB472dr1r+$2VHSQf zIJFVI%tkQuK0o*=pNHz%rMA>U2G+l3jLd;3qqqyG^1$@y(`bv5vwNsd{bq3u6-7R! z1^@U(-iF>wZ<(wCDY>>4YtYuNgP(k&?SJ|S?c1brD%v!Lj*^L)akAv$fswRFI^bQi z_(tX8F9sk%Kq)5lA-|lg(!HZB897j1d!VL(#OBarLn%jK$*9N2KdLf2cal|lX3COL z{pGcsIs$8;i3Cl%+($NBW5bQxrOI)ct&$QI>>0jwn@5ivQPH()N~2e{EE(NPUK`$6 zKpk?8O=^qYJ-SJw_aIp^2Jc2BMAEK?F&J2+@?X!%lj^?SS<>i}BTL5ilIathg(o#` zsE1}PS*+xuXCY;B=&(tn-*8znu9u9@TP|yNy<3(%+Q)xgS(fDPhREz7OUCcPZ`*Ap zkL}LcHnnu1Aw6HM-ZpsO5L_HtzAb&%FkF1|y!Jqo@MUh=(0|i^+hVouX^TA+Xl-!K zqr;X8%6y4rc81LtcII16>~zGa{dqh$KGEgkRDq2T^gYYm!;tvT+Ss}zWbhS7E9 z$rV+jQd=aeGc~kxt+Bo^~19(WbLK{J|XBw%864p#=E)tFP1?>}87gj4c_`b9}Bj zeahs(e##Al98wb}JXXG5(4p);H=LGw(aMz^r$!(8z~I2x+~%oMKPuW;8CzOQ&xx4= z-ldiJ>@5YMoCTLY8fFB}6ru7$A#?GdwuX^~TEs+~G_*3i+yjUd>G4e0E)O8)h0?+$ zMEA}e1L8a{bmN8%nw>DGPMsodw8=fpWz5);EImKuo6VXu=3eJW=7rKxYyy=6bgu^cseJ97fvV66|6H>@K0epS_U5iPR3zA1CMEjVVBzEA zf$dDb_~^s^q$+gM$6{|8X{|I{D6|v;616oe%y2zuaq;5M(!B<(-hAO7Y+@}3u9D8NE{L3%C0aK4f)(BUAc^xgFz-1NA z^FqG3`18*`R{9`ht5>@w%~&66r*>^L>wA0N_4Nu2X>hI=^TmPv`!s9$T-&uy)oh`Z zJ^bkG71p?q6?;Y+%fWgnU(A{@-N(x9R~Lp+`Rcy0b0^rUt5(gEG@D8H84skvxn9P1 zg~0U>dS2-HpMHW-8m(=seILB<8+6~So*DxFGa=mOw=e^fAco6c!0!5Wx_II-{VC^# zj(cRJW~GnhcZu>s=?HKHu(zl%wJ@dPPHfyzmaV`R|81sNDht_HK`7574Z9dv!=E2K z$o}%0U?i}oBUK%s(DVrYyNn}7FO0FRqE#yyQIIEJ>?l3I&o${cU%vc{1_^}{2eK~+ zwWeq_ALcfq+FH4M8MI6-jZUG=vl;y7Tay@B_~*BwDG07`i1YxsNdPju0`fA;FF#J6 zJfX+aF(VzuWOd!>=3(1n1*+muFO#J!vZUwQ^Ug*Ptu(O_vLa&k%@A|y(+#71O@Q%1 z&mAUeIdWp-SS_tEc1kCcY8mGVJ!CHR7|TV+Ht7N6MOqct!%D#5=EIRLDtE;1bmNig zi-Wv+<#Gi_lLXP&UInsrRi@=}0PC?w$MR`kjM9%)+ohIS8Y|by%|a}!5~&i!&h6Xf z-WR|d0$#s?l65oQJm=<(uWDXsZyzru!QL0adn)(2#pqfd4k|4;hfnr(OK~HTB8#^A|X2-*Vjk;}s_mNR@jz=R1t$W|P>M zfK<=W{IC*eDd;ss|FfJr4hnumsn|=b$!!GpMiz7Km5kM5P0=d8X|2ULbq)DBDDsC= zV)Ni~3$zfCl8pCfQjU4u%4~O9q+F1#W8U!>- zv`N+1q+xxlu(EN*9EivODfK%`BKEb52(keyD;LKYahawifCFDgc=b-;1ZxubB0j<1)OrXAcay z;h?J_i`Qpcp1X}r+TeU1UL1oGH~ujmwRQ7mh489O{4yfY>lBl5(f;@Lmbow2G8-T` z4wWSfUKnY4su|nE8O-h0sl6IGe5jsX-(EdbV>r->N8JI0NhTG*$l=3e&ZoL5#@o|?vXnO@dX^z7PMbBU6V4#=$HU6!Y|u~nvDNi*XS74Um< zS=dRdWyVIltz&GG=~p|5eedfyQId&*!Y03PcgCm>9)z-(!G|aAxWIZ5R)c3bQn8HG`}b z15d%8i5Y;r^f_&9GK>b41v$wQ+33gD>#F#)A)V{gtDoVQ(4g zIDqSK9L)^-;4_X-aDAND3w!Eok0ZgDMsJsz_P=gj!Gne{?|;!cgSJ9TyYYd(XTVUm zF8IBn;i#xq$w@RPYrdm5zy<4`bF|DReX9e-pD@+R%!>km7F;6JqPc>nKPM{QEm ztPXB;j-VN=(j!LfJ@>S4r=vuS6#ciYOUDi>8GeepmdnaIYpk%nryO;R&^|Clk<3x0 z$l&pSL6z(Z<>{fz&EOY4fy^n2FnIzHdm+KMp$uD4=Z{CIyK((!cvKN49Al>3c>Y?4 z(tP-&qy%o$B1A^gti;1c8*9-?qqpt#I_@@{l%;uybRRzSB@vHCA%LtN9nCCXO>Ol0 zY~~1DM`&}-uV#OA(rTw$1&>l@b+2mDfrF=^#gDlMsV7X7-KDa-9=7*_qq1T4>(vv! z)?s#a)?`;*JGNhHII4=}%bwQ{qZrU;<}(WY(*+4`^cDSHO&2E(KA2u4WH&AB)t4N- z4JV|UdV0<*9UDV0Ws#KoO!Wkee@-MYnO*iy_If^&xm8$M6?$XO{8;_~a;DZ1h!^SJ zIH^vy_g6=VVKP9xVeJ|lNQU8p-Kee8;Dw7vYW3ua?{r@-%%D8!8$7!SxsP)3vdkl2 zf6XXjc|YxJ@9*eGtY7;w`?%e3(Qv(SKK<{_(!!hbaWN2?hSdtj=QR==ouK4=#cSsYyO447a>^iIQvb6e4)3x-E_&o^@JyOzzGYIuHy=9c$X@uxWO;eNtL z7~${i-W>!@k1s+8u=L1j%h5athG&Sv?*e3Dk=GJhACjh)_YU!!KvICvZX*nlpbF54 z)N{mF!s)sbG8h(Tz&B!^;@Ie@a@cswdnaJu+5MIeHXN>F1z0*aXJCX8I;;mIYA`gi zT7>N@c!80&xA)v7Y8z$>60L~!`5}4knGd?Y#x)ec*h5r`N`&j~}JLS?C z%Uexc7Kn=xG1ds9%M*;2hwZA-m{t!)<2AOhZzCLnT*>*1Xf=R_?2sRA%}YfU(7JVk zuz2>Ct6|(2!Dtk9JQV^J0anIIBGm>GgoqC*{J{SG6_5hqZH_IBLC|2dXXiiTLxY&% z;sLEL(8?}e^rLYy!L#HwW6&CH^28_L|N6c%QDJ2H&|vFYiARYR12pUFS`;59TDrKbOBk-7Eb0^HcLLoHaJW(cSWPg>Zd;?;fH;xKdOF5CgwEcWftWnTx`;2TaibB7ZYbb>NTH zW|$1M=Ht+m9W&}%b&eYSEN>6-r^yH&Y#w$|#c*+(Gl3L<=WmBLX@P)WS@_FEt1EQD zZV;N{;4@4fxls=dw~Vt!!Fq3RVIX!nfynM*y?X0(V zzomPP{PdHLb?m)-V*Xy4AM@J;Zm|QaTztsbYK&vX`S_Oj05~s2C$-!~&TRE9;06VGitnUm#$VETC(wy=U0fSdn0awoFV=UD{`$ zFsZBZk9U{r`Tx7CeJ&;@f;}=8aRuDAj6=vzG8FwB(pdfznqo5eR=;i?c3ot9$BQ&W zAgIHE@6g>b+Q@d3nSEr^BxfejeW9wHI^oyrD9b1Shr07>LO7mxc~dM~Xp^3`K{CID?3v9sHCGuly&(A7S6VrDKc1sQwzH z;Ua(1M637=QD+m?FBjJSC)Bgy{C2F>3r2-#<5-6aR5w~h$B6ngQF+)f)}nulx_ZTO z5|o9_K#C|B@&0~&eTa(?R4%?q#Dzp;+6N3PTr9%7*?RgBuY@ykAB@Oot$+9Ch?&!; zS%rE{7ZcSlUS9B{`r^a^bbcj2>ecW-%sCj@fAVw0BM*(R3XgEC&k>cyUl{9Bm!-!V zK=u6+b=C40Z4ekn7+mrSgFX1EPW9yoq++c?y`ako8A$pFKw1Su;Sc*UZZWh$96YRd zeboqqbXpHDmKHlOvXzAC+`cVGwm>|$c(Jv2<8_$ZjWFhFgJE{rHowFmu@of{xkrqu z_TZPq0Rx8A5n{~J7zN{ifmA1_^A97e#SQzzU(}Y3=@1pQ-GiqJO3e!moIEw*1)ZhAW z7<|6JUmrW;b#dMhDEQ#xF@!*9+rua z!pb;i?6tl7MSsH_h_d9vFa|J@6m4$d_^W1djjY^_j^sdphGBw+R-jdO&PWNP7GhZe z^L6}NzXh~Z?>ASz@6X{Bf<(;$oHKci47Ywf))q&2a*f&bEK40&(6 z>=(mrAbKnwHtI8=$6)x(=~Gd|yb|TEn{~-JO0U#XS*RYvQntq7D)`#bC4h9*WZoVz zOeQRLUPht-k!LXzp=crc1cFU#UG9#3`=oxn?vCxsNq8% z&;iLnAx{N#iU~-sB1TGnm@RgZ%R&B2_{i4Ed&ESuKFlR1?m)3t z9|e@**u_mW)vsb8dTAo22aJQY0-;+w_p12RVNFdHa|PB$b5c#&#i@=-z`llqU`=f= z=IitgGjG6Np$|3k$aYZ^P2~vb4ytZqi9q?VrVtxnS#KA$a7+Qlts$Karhuwg)7pzU zCd-phT1v8|+CxNvqg|A$N$&{hU@#pNp-EX0rO6=57TJYKjsl?F8G?+)!pWj0JDiUx z*H$`&0KH0M}i^n+@fU35p znkM#`qlOO)glNZ@lmL_RCfJ4dISN5lS5rMDK0u{jS7w@c_J`}UgUFCw(9^LLR56-r zjyT{4N1B1Me(_w#-|_q%ubz@#b9k{?jlgdYrJR0 z5hrrOtg8;pB3aiOy}x&_;?z(UY(q(}1xHiOA;pO>{v!_${HyENsiTMkBig_s12_$T z=R(owy%v z!pPT6ZEsioI>NZ^Cf&x{-X7eaH{rJT^!`7(je#p3a>ikANpe)jg50mKTj=Y-AwS`H z3#2;WcIxX?eLW)NBA%xrCjnnB(zv=7Sa6LZcPkl|X`FKc(uz>R?T#JxO TxJ+_fp5vVw0)OC^4hjDY_eAju literal 372526 zcmeHw3A`Ltm3K#QoK@!=g>3J=?$>*F^}fBAjf5h9{UTm8=a{qDO} zUENi6&prSDJ@?#m@Adhn_}=0B$A9!e9`K#Bug~{>pU*dKTJ`*+f8+B#1D^!~)$`B6 z_s)8!&zH;9&LckGJN-W2f(6y{_(GrWp~XJm#*MY}znV9nI)c3F%`@aXz z&#V7FF#NOd$V}GGJ>WflD*XPf$jA`Gdt}3-w%!BE@)rMnY;=T0{6lPMQDMK|ei^&y z$ochqeCX^|?8*D@QPVyD@?nrgwJazP{`>f-pG5=1>ODSq>>~Cm+~cM*k6>YtNl}Tj z;}2QFj$5{b%`fLvS>ioVru_Gbpr6G8BlUZnd|-t=f6K+}%a@&0e-FJ(v0fU#53o3t zf#2igg|**bev+w7v2OYAlc4~^cffmm=)FtS_rLk9BU#KptjZMY1t*5fvi_)(*q^q2m0fn^iK>2JdHC;B;ow{R9mZQ zE>rzI5HI}qkWb0RLu=E~;Lo5edr=-;3KP4C(iH?q(74px#IuaQ9STzlOEHqM0LCDoKG~;8S z3m*$W@=u?7^~lilGlMf`jI?d|-jLev}p??2x@X9D~7JtUL5@w&f;Jxgq+1aN02m_~_a`-ZK z_KKq*9m&puR06I>URT??TAc@mrd`SJui*WG=lL^ESy*O&x^q2y<&I0)`G?QbJmZ{W zk7loZcS4gMy6HMi`++(IdaJpA4DT;IcHR`1{7cJUc_|0jj}+ry_ZYQqj4*g$CeKzaTn5h_QrliGE6`)j z{dpUR&p37QcwIj)1e-?GEAbiQi6nbz%a_&m`Q}yctJ-fhz7p@BjYqG<`}1ea%f{I~ z7aqq}%qyt2hgcsRgV^RS$(7jaw>+ZW{|oD`ZrF(M4DtS%cz6SEgZVQ=SsCdrFUJLk zu3+0vznG0DlN#NH``7uQ5RaUVZF8s3Fybq0qc}h0;_*N_68wKoJp~YG>IY=56uRg@5QoLQ`O#e`W;XXqr=nZ>0clb3VaUR z7T%}p`an2h_=?=Lh|7ifwE!-vN9TZ|a|)2nMQlcCCNT>*C;& z0`^e=biz4(Kx{TWAM6lrhjTfcyKJ^<@BDJh1?bYJH%jS-2`UGMh;l3L_ zp>hQ02L2Al&mV2F<53P`f8q5{uV-7%I0F19Ro@)P<^?Am$F5m(HoIC#SFJvi%`0RK zISP5Qsr+Dn7}{g-4q|@-WpV$dC$dyjZiS4Fd{`mZ=Z9Y=TdZRto(!u74T8 zE9aWns-=x*u1)~A^)s{b3I2Hy~F(%UaR)a&_Da73qD-;&!UeP`05HMQp>{~j|pZ;DO*JG`{zf%@}z-*A7E4TJU7{QR)m4@Djp-{J7_EGs8M zhB~QlFStHDaN$Sm&%f`2Yc>5x#3tI4`sZh2;Wx#8r1%b^KD6Q}E(hr6uuG0OK}`v0 zpHSaKo#1KRfb=a*9P1<*%j{dfoL zU+ABY=lNu8D?Y!M?|}Uaqi5r13l=Qc7vG^5azI-G`xgsZ^gsA}mDBP0xkU6H)Jenz z_|aGz_(@H`lh{`9r0OilEQyJa*#L&oo{WP{3wHId(Y8-SMi1ckr@SEn@D-?kfA;zO zyL`TPV447*O)nDO8xuaO_E?5~3LGms6;j<#5rz}`BMJ0T0_k-6|0E;9bD}~2gW$*9 z74{GR8OCIpnqX`P#tCZ)>`je7sOA#_n+okpo&pfJ!2Lp)`_)wU5B(YXV9)wz&iHaD zFmiS-5a`A;HwC!x{D^;ecMxMGc!KBE`vEn(i~IO-tY1xn52+eM(1=CgeMLT2S@kCg zW0v5{L4QtdT=7Ma&swOiDeY8QCX@L);N~xIpIX&Uai)vXHqE(UgV+0yGS)+Abf`WKrLT)*DELsduOV+4kX!Zd&ClbZz%G1$nacp}!!!JS z^*Ox%pSh1x)-u5RgPCXi2fV5A^~zWVc>kt8Ixh{rzrOFUFT?UVId=NuG1Xt9`p5dr zOGZ9c{rj7h;qv7N*8M);hS-X7JY4nhS(KrYPO=-cld<9}E&`-zH)0@|^W#yD*86MfT+0>UV`qi-vR`!yS zkCm4L`>CoQ&7=(LAx4M4JI{CDdI>vy$#}~$eDl-WY`y;{FJ4#k%VXTAaXqwt|DfuF zY*hwv+<=z>{gtlwXV zTQaE!{kQ`!#}_U-jw!+6`dDx;>{jeQaDCC|Nhp7HoWi;c`f&?hhR3ftlU2a4*(=-I z$j8ph0da4_Sc`QT^y41947+Z)fE_lL?nxP}-d{UTVqFIO{*Kiv3~_z(fhEVw(E9zW z{_J+kfNdO)N9T2bIo5;IZJf~W2ujDuOU)9N^kg-^g}jE2h}-@iI0f$wi!hGIMfBB~=&$7R*5^2mqKIXl2I8pE(dwk-dW5yEMIBrZ*BelVTc*7 z#yy+W!+Q@Xg1*dFzt?UXr!T{yrE&J^)~{&Ffaf^($;?-Yes8_r@N@8AsWH!1Wzb`~ zF?MZjFX_vG^SxfV=^?cYm|uR>iS==9qcT{$|DX9-cR$OJ3x(O?l{qX6xs|k#umI24(jNV!I*_1*m z0WmLfl>vMUw#JXJEC<2(9hBi-S<0Y9f?vcjMV*|jPN$$QxrqlH%fj(Ec+s}TPsM^E zrpp=SynHhHP%a+b1#R@7v7OF9>Yg3aa#BR{gDJn0wjSBB~VPK;|k2# zg;?FMMo0ZmM*Jgx48!~d=>Jv|%wGsY3e^*j!y95S7habj77t?cM!}b?O)yUreB!Wn zfhPIk+z)-$YHXgG|1mIg#!un5kAwpwm!+doq`jW}6;iQGA`*NMW0>?YdsTlP#t&B4 z#lg6}M(kd74W;JzJw6|_c|5c_{|n}Y3UfnwTp!Mdtgl0Y^F)QXz9Cq#<<4 z-&B0QztWe_>KxXDy16k%R~_T> zstjsauqtx>dNGqIK&;4HTHm2}WdMAzHyR9<>jktpfWGxkJ~wPoWPt60MX}Y(*uH}= zxH`rQZC!KBt~d1n+s0HRlCY9NCLUd{AIlgN8Gwy+sa5%t(a_hh{DUY1v=gn$p9lq? zGL6+mJZRz`yoCj@r=PSg|L7mo^22!Ipy>g`=>6WR{5ZZ_FaLnapjC`6mx~(fiscs5 zfbfC+^5YtR_GEDU{35&d#HH*w$Yr;cvG2_5*(}F-xfttGO|4~c)$s>u;!d`&KBgW& z->$7ZE0GML|wZL*U@gI)_P#S{MC3TyE4FaU~#=}UT%GU*V=S8hrTyaGY+9`iGGQfV5U0&>` z9<*EjZe?&X@*=c@dL7{B8~=GpU-iIV`5SX>u>9T1U~xgIuS?C#&2zM$tlCC#JT8wUwJ*7B(%LD1r!v6b;X3ZTtoj`1m8@Da*6^?Q(Vy6E z`P-F25$2~F%^Tw~K>G*x*wM_J>7!58UirhCH5Kj3z--tzF^VWFTKeO&wmKd$=%i*F=)mHf-2DiF~p*rr%|cTDYLQmNDMY)ubQ z1{kBr>)9;N#0AqD|InDBXBJ%Tk`6bnZPGtb` z%0DwJH~$>wx_(_V*L8qofHB*=oMw3?le(g5j^=>K0OQup%FREYUCN|z{@tL-0Q(nt zMM?5pGBvZ_?#?*>Qy~}`3n;`hohZ(*MArF0I}dD zK7jb0y*PgX-(6pxPS0nRvK83Ne5cH9BgU;m?Ebei$*AFe9i5h4{N22gz;wzN`)2iT+3eBmt5DNq{6k5+DhX1V{oT0g?bofFwW?APHDX0N0=?!rHjm zWc(HwyW6QmLQjJ)d=IR1`$io6->{x-b5i$@#yzB~3HGkW{i}uKhhrhZ9t|-_5EIfO zjli`=iop1Sb%OQpVBWBr_?mboc?dplq{cdV7^lk<#4NoL@{jBZkBUw^-reE{Aez>+DPkirQb$vTn z%LwN(hXej!CSsxMb4n!Vp{mxrYAKUBB9n+d4zWq&=2e=MY^LMimSa%fk;PF%} z=Ggk5POH$YS+o8orzEz(dh~BL>K)fX;AuJ>H})mb&uh?g0r5a%Uw7P3TwV8bXvUlI zXk_z(1q=S#>H6#DerVU_lCd9#VGdZMzInZiIq5(<_JE!TPzTVy06Y1=;_-O2n>usq zx8+=JHrVpJ&F8y|^^eD-1KsKX>H+jGUQR?t4{)j;JGoCKn>`HTnqK2|&-3Pa8>9o> z^b3r50CfQN)_g6Mh#cBU-F5q$aw;`jiH2Vl*Erzyk2&c8tn&_BZIr6`Z?A9ILI++? z$HVixtd+jx9zfeOmrVS(w(dy>8a|_1 z{rAX6WX1!r;OG+&7yh^H;JB0T!aj31;u@mH_1{?UhIpXAJb?UyeZx-b)b4LeS)~H@ z-kZkjrgIJ;9q5Oz*oX&4!9UPLAJLSY68m0#Jrl$AeMkoy?EsD+^uH}YKcQVbxAVP9 zE`1dG@-_8OIskQSG)^Rb)?Xb!UvV$S3v$W$<4tvs^-nq=oetobQ9IbS`tDdEUBta; zP3xa@Kq?);al>pfoVTi1yVr}E8{Fs=_#{;-89>x>e!Fa3h6jREr_4N#E`$drsh;`48<t9&omvn&FyO_(R0~l9qr;b~F2V(i2)zm-ffUqx-Soi!`9vy&~_Genv zt(|KS!@H-c{z(Tsp#!ik>~1@C-0C}-c;pSk`X?QbUI(E5Ys-tYs^QkxaQ!RO`X?Qb zS_kZ`3*EZ*eLnF0H1C;;^)4pr7qrj=_v``Z>fd1BT(SO%2U^rW+J)}ufV1^aI^c(~ z)oSv_d_(8!pLD=WI?$W?cUA{Zhdt9Cg}r57g8f4=;qmE<$AtL8>ORX9PuPAuA}ga1d^Hy-PrC!G!0fjzK!)!~|bcEr7=Zil^Qaldk!U%+FD+8;X(!yH_z`n9^Q z*1u-IbYB16@c`mptZ$>^of}SLg(&VV-M^Sp+~epI?_0xudf(^RA0GckNBg%M?RAg$ z`U%|gV{mW8P(N%!N9*5V9eDK0RhIRSI`QPE)-c?Iu^&2+fHgzDb=R$q)wz)#;Ah{w z^OnBofaCS=pbp@EsYaML`mDOQIJXIUXAk_#MQeLr_gp8C2dXgyz3Us|nl@JaZgsuR z|5*1t)op;z+XA%pjOyC#Gj(s_6PNas2cEqDp1#%pkMH}e;4|or4mef+n*Hq@(gC~t zuh)m|tC!d8Lg#tlm2ZEoZ}tE3x4tISe{Xz5PS(HEI&j94@wV_U>OsRUbXEuYiGQ>O z8!=?P89R2m{+-r=8_zhRV;yMNh0f`~Cof*tH~fG6!qo=#?<@~EQ~xn_uV1Wtp6Ygh z18u?O%NMdg!hYH!zU>}2>_TUB0QU#~_Gfx;|9|VQZ8iI!<6fUTV-Gl2|FG|`Q2$Qz zz$viD@bkA^+>s76>_X>s0C@o00;l``KZSdK;-WPy2KIA5)+cnf{(Gwfi}DHflTWSg zNC%KF?p(V9el1&~`U^=19IJn*6K(zXmIpAlynDN_ zGU<5$*SNOAx7Br6*Adp!hWd6v2TtyRT{xNA0$c;#s(!7mWBns1yQKpQyS59LH`#@g zMF*U$e>ZiY&;z@0GU$NQ_3x$*ApbaR7Y@1(^rHUV)PZmh?7~6TfnL?W+dAO1T{x&Z z(98OFTL%^w6g4);u3td^y-%(@MAJVUL>+LZ{^P3skM(Vo-0Bx#TTqCFy7CvEzF6xc z7&IMluKxXobN>3Z2-wHE=gGJoaN8dE(0doNSGQl*77t+jKtJM&ac?6x_A_##{_~2y z{^jBU)Vm$4SF}~{yltrRKVPFzJ6<#hJm6IQCxAy9_~*y&^FT2kViR4A7i;I4HOCHk zT?d@3f7x_kNzsY^AIH9!4-OCDKDk!yyVZ5%f2@0+WZ;2Q7IhgfcG6N7o&;;v_M-mX z=7GysbP+EKbB0t*+MpOWDp{jsjilGFWy^=WO${|EN_WLW>b)&Y#?=+4eNmL*4rjq84r z>VRYQ4|>!~9astLy>(^hoqVvP>yl~u30?LJ4$4>9EvqhO|8vcD_R^LI)P%=dSG|ud z$(7hB?A3+igJ!9laYIMzzehT7#j%Up9=GTHKU_oT46ySWYtr%cXu4mUu3H_zIli}@ zc`18!>sQ#Hwtdww;k9jNTmrV~uxb5wGj{BF{X47!H=lWA$Nl=BUAKlER7zFXBywOq zA}9R==x^A$ev@IH8|lI~F1x-bIuH+yI@A7Ry^F~Kd*H04vpTMOv=Px()T~1xkq+E( zCd8qbVBYL=GKf}YjC13d5_SDxNc{eODR3a~}|>UtEK`p30MB+!9_i>@WYYn}u~{8keU3Jv$^j19ggvkSE_C3f zcGiCcT{#oli^d%E-mO2-t)K8m*Y4=J{;^+BuYU)0AmKp!U&Q{u*!CM8cVG|L<$tvE zmX=dO-Sf2w7eip=f7gHB~L#OIrF{*R@XKm`hSxaYI@;}CBR+16d z=AL&lW`uowE1oyIj=F>O@01QWS^vGzfh}j4_y2I6Pkg2fb4=Rl8+Ob8!6BG`;E)bD zQ~yb|9I`ITebzr`RFUg@q%8=IqgaPlMdL{0gNd{djM_1CqBG`?b-5WHQ_P( z3DH+9&Odir2NL06*W*86|GlALo$C|XgtqO0>KtQnju}7h?Od~DIN0_6&*ikTr>X9_ zE|3mLtpmwOsO$aTD;Z^1ir2SMPC6j14x}RCXRPLTT3sK@r60xmCmk?|DUv*WF*uFuZrZsqk)I^ZoG$R^`kt?Jk6`s_-6Z6+F0@z3>u>;at~NXP?da~9IdYODIS zx<03zRSK{MG_QM}lMcwO0~q^R%4A|z^=s#PDWg2c>s`!A2jtZOB^vsbow~L2ojJw) z=5!?3!~v)aqyzHl0Q!`2O5%Dub!+E4b1Icm0oH*Nu`eEz4#=egafpvCWwUuZb!+!K zs>r19gs%{GKuKpcI(yd_vVxf$K(=GsDILdMx42>&iOn;p1aU5h{8Ty z<&1K;-Fj`~dsD{pnIEK7JAiaRRvm!-+P?#JI;D+zwf)t(i zJsyC4+MXyDi+|fzWp?=Gd1LuxDVutQbU@Dmc;0^iG8n_$&ATT&lc}7 zWp1T#G>#eJxFL-lb6v2n0}2=GcTaq}7WLTm<+)>pBE}D33<<^& zQ7oCc4&eM|1?oH-kN&!tNp2c1W(r+bfJ8UISTc+!#F*0YTxz0{Nj(F4@iNRY`3v|G zSW`;DXQ^a872()UD!}(}-BPQRhqaBhDGukx32ZL3yAY4M6od0d%EGZGCE&a%MOh{u z`Aaqtc{!Vm{sQhhQA{VcRSM}fV|gVhQI*lFkOW8qBmt5DNq{6k5+DhX1V{oT0g?bo zfFv*g5@7EC?c!p5NA+F!sn4gLzu7pSj~~3!IDZ$OPc+W=!Sjuc^C@`VI8|@BS3KXq zm~eZaZ$4w16*K&r$N7BlK;iM~`7ZH(P=bk8=kS7>>u|nN{64&})w%d}ED)WG5+Heq z(jfgHedxD7Y!n}8gU=?!?;G%={`xNQ`@pw)KGvV>`CQNU`u0FWsL*_fgsiqF!V9VG zjqpIVJrW)W%?JE7URXV4^%B(DyXvPd{a1rVab)prIKUXFz3aZUoHCLCNq{6k5+DhX z1V{oT0g?bofFwW?APJBJNCG4Qk^o77BtQ}%36KOx0we*F07-x(KoTGckOW8qBmt5D zNq{6k5+DhX1V{oT0g?bofFwW?APJBJNCG4Qk^o77BtQ}%36KOx0we*F07-x(KoTGc zkOW8q{g*&lQKl6#%EDqsIj@jXu7|a$A1tJkk3o8(peWB4)5@MgT6rT6>s9B|NqY&_ zqt17gV4Z4M&)PIKtz(_Qy#PB*wRNufde^l)3+rAxn_w+?Tn}DMuugWSi9JHS`k)evU7t$C&V_toDwUere+{K4Iw^rnCXp&A zi3#}L@4E?ZqmJaB7uF1 zxzwT1zTXXP`R{N~2dF<5gE|cj&s4FE__mXX1DxOk9sZFIy1+mFwi*BUnSg&hwt;{C zof+_5vor*p7=ShbIUxaUhLVW>4s3KpWc5K5WM5yf=s=eXx0_UX}I*GR+xo%sXN zk4Sca9e;o^FH|{T#(q^Eh=E@jd<0KHI;;l}n3T87WHLkLZ057*zek^a1M?#O@t8P( zIDq>Id*c(d;sDMAiAV_g2BC*zG5_eK1g!&wE*6V#FJw|zfM4%51>}i7d~@t`E+7sd z4sf3X(4Gjvc%l-Eyqbzf*B5=hw>todNpp*QBH=G)(~skLeFVnmt#QxsPj&#t3J?dl z&jDx)pf6SJ6U4$l2#1G5lLo+D6tt2>|DLi(iH6$5J#l~-Cqx_|2L~Vz1i^Mq#v;E5 zoj$G$XbkjEReL|3yaC4Z_C`mcpFa$E#-#D{wR28=0pb98H~?b}VZ3lJ@W3VjGW7=p zHd~-5e_zU`9zoxJcw{E@^=GJwI6xma+#*gGV~D}_*C*ltc{l+50x=k~RwCh@;c)n! z0t6=QVLTpxH`x1+XJ9S|$Gw;n2M`Bz5;xq>SYbQk0@x4WDFWkI>3HM^lYO31CKDSi zXHri=+=LI6a8DdywSEX* zH(x&luhF=G9ZrzlCzy&x9s&Fh?ATx_t!&9A;wISVJV17U86OY_cxw%jcE<(OvB6m6 z`a!e-in;Vr5X-+8*V-~}`#JtGCk`ME;A;^K-ueMJZ&*#jm>`Y~VtfG19onnJBg+Sg z12CQ+#`#|2YjIiQpE$q_|I`<#@qvfd5^Ki}z<4c~H}rBeIuaZN4!}NtIhX!k3g+;I zHU2)ay*E21JHU(ws4p zFt{90T}vdo-pe)s+WipM|5^;@^QibI4ydg!P8`trI)jtLAAs_KxPaHa92->0WgbXF zY;PU=h=1Y$;()IA8<-p%fHA@_XYgJx+5nYY?oilcM_re<3ID_a!~w(s273=^#|3KR z0W1@X9pNz!*r%LJJsl7Fo7;YF0}uxg2M`BXasaL`jBAPl{@>;m4ya_5Q}VF?j$ykm z;-5HxIDk07k^|5e2=j$c@Cg2=l(VTHTekfo574*(@BsM)$tNi8N7m>Is5U?>`~=`% z`aMP%Mvs!+1}Nt;-%sEkd?L2(9+MqFc0gBr zFu3CaxF03%H6}CuEBRCjV*CO7L!IFtd4f2AIDj}n=M$**1#rK?Y%-jel>=ZspBvJ# zNDcqQ0pfnbc8_V_VQ$ZNlMkjHfP0R@+<@z3#Qy@HZ{M+O`gxq=Q*Hkd|HJ`yaW8(4 zIG`Ks^WP302jITLxc4~VU%EX;$8zZ-3$RCDM8!PEKF@1$e|G%<{Cm_FpuPb01vGtu zIINcmdkZd;%?7As)1SvVeu#hKfa$DhA9C7zT(>V7?K_O|eVB~r2OEDr3T+~+4{*0+ z_@4s!e<23znsfXU2M`BX?>#~s&@KlIfjtC$fZxf4|3WI2fwjFf*ylW;^#zbWKG)EA(>K+X2&?LW`$^aUc| zV~4rH=gWeBoac-Cc<6C2o)ZTU2ej@FP+!0<2LxcxfMiUbdBK%@2G;h2zCVmlikR0Q z69*6n5C>F!ejMv&c`G|W^#R1AFUrvV=kxg)u*Odn`-p$y0NAsf_8xDI^Qk)fUpyx} zfOA4C4p3rICd2-r<$Pul&i7Gq&+$(jKpX(;*b)cyZtw9J%m+%v;|tuj0U+*Ybv6mU zKN0)>Ri854QhiT;tCW|6~V{9YA&fYwSA) zegCOKG8qHlxEFJMUmyr=s1l30J{JIde-9_aqvrT04j>L74se_U{4;Ui;OK*HxBrz~ z_PcSI?{C`vQ(pk^KZ7`cIKWX37^$uc_?X-HAIqnIf@^(Q;-BmQ;sD|R$2edF_J@Ia z06%gc|CwLa$NqU+-zq1cpoRyU<_W>rVf{0T6V#0%kWY~N1crf2m3VZQ+xUn6|DI@I z*k1e3d4V{9IG|%&fH(j-0M|+d{O@)f|6}>g8@6nJj(wgJ2M`Ak2h{ol!k7S#57-zN z7=rze!Tu{)q#E!~x6?<5JZ$74jAd?0&y45eKx#0rKIWIDj~S z;~(=D_{VEH{Np)sfPD^-6aT~k!~w(s=$~&VHRcF#{A1pFTtHs@69*6n5C^n_fBbH{ z93VITi35lOhy!}b0rKOYIDj~SIG|S?;0^o}2M`Ak2lRpiyn}zaIUq4Q%+?+{m)(2G z@$9MV*Rof)U#2EJ#?RKkwK&Xi2*KVf+B5^st&@s>8XKgsL5F>U>4Yrte~528X-K+Q z;d7SFX3ySmKKs+1>su!L4t`gQf8+ye{Ns0s14J7Dk7;bsj!n?YCnyd6p>4E>e|hWx zj90$x+@o7!-iQ5f7xtKXHH^{Ns1bI6x}=dyE4*!9M4M z|Jb;S6=Ff+fSJaa*Yf~zKzp$OM!aCe0n*{$BOHKsJjZgUIr;{c6qHHD0mm&{%ywM! zLH5KK?`AJP`tR(u?@maWUVwMQcievU2iUPo7wKXO2WQSu8ur|iV*VfEUQD)p0q*n# z(AW3$rnOz;A9-Wl4 z_OQ8*V4vF!ukN^Qn z*m=jzf(?N44ZYhJ@C5!P_@Dmt0Yom)`UU0ZfI+tZfAqzFwZOkOI6zkX%gzDkE}PRA zAApE|Jbq!@C2ZxQvM20-y9U_*hjGEJ+kbD^0W#xXb`I!f{*NBl?VM}iuxJafnD6|UF;$QKR2pu8Ng^Nt$_ zjsqsP-)yt(7r8)A4v+``Bc}M5-43{X`9gKApLW`Cr@q_p3(Cm>2bK%$w>uvk7!JTW z0NVIp5#v_J@^OG%`1dXctXx=T&u_cf6qS55P74oa*DZd!Ns&Jr>p)#`OiQd4Sdw)vqZyxz-ew8~@(rfXK)YbKfr*A`YN6 zMcMGQ{UO2nf%*jNg36D7Z*zeAe!(Ph0C9jR2S|Z`@Twk_T{-Mv@8vow51JExx;Q_xO`2!}GKR_z{4>$*)U+{KU{emlO`2~ps zCJzTlhyMZR0I&E3i327F2S|zkNx%W8xEe3G7S+yotenH}Z!RCN$GVc2RKZt+q8*8x7&j*|X3Lfwa5(f+}2gro~$;tu9 zd$Rili30|c17yShWaWUc2jT^Z0|t)+WW@hu<^b9Kg2VxX#R0P7e=>7`?0!MwfWhGa znejiFIY4&5AaTH8aDeRipUfN}yI+tv!22BF1^iED4v^h1NF3m84)6;8Cp!no>=z^s z@Gb{<3ICIw17!9K5(jvb1H6X+$<6^X`vr*uyvG4v#Q$XHfH>@Tf_s0oyDp$p-@o=CSMfjDIRO2F8;@JUaLr$*aNW**ww-;H>6${s0p8&NIq;uX=l>w4 z+f9>k&Jbdsr&AYK)ICAl!LC!^J!jc$;s9Y?F+UueCpp&@mIwd-wz1E-VzP69TYkY` zY+lC_E!G()J3!9|@^OG%_$LmC_+iaZF+p3;*Adm{+y=nhEx+LEL*|&;0J#4Eae&+$ zARqpT12p(YF0jb~ZukYibjb-V@lPC}$3LFSV+Y8If8u~nIRI^ejmItSn_uwhO=~Uj zPaGgG2gr+m;(%^ApkMKVdv1TfE&LM)$jJdR;D2@z{r`w}qtyPG0F4a}v3`vUynOqm zfd84q0m3*yKzD3DCg6TwAg8#${vYB$5j4U+|BN_5V+&aL1Ns>kP__RN|HJ`$+>7V3 za)35FR{eh*|HJ_z=Jm%ia)503Cl2V018{$_M_t%o zZ1o{?Yi&PqfFAEg=dy8tjQA%GXpaNj+F!I9|1)fWf3gFNu+Kk}i34QCKXHIP4mi#A z_+PQz*B=|t5yG(mF;R?=er%wB@c}a9pE#g32V8l)+w*_)csDxNVqL_&cuXAN4hQ6v zgzNkNaQ{Ez-6$E43*y+oJ)dhGLaH#xv7_$Lk+Vs82Q5c|7tyM&!_ z(74*}W7|(0Fr5WvPP3PE@jx8+V(u0PcnSZLnFCh3x?lJ@wzQ-)u#fmB4glsKsfuWrB0m34l;ea#uHk_b0rpE#g~e{XYu7x6z?H~`Ce$9c!Nf_t?2KeO&w zhQ3C%t><_*%E=#KuOEQ@18<9|>& zU~xfVKmF8dSFqpY=QG2*(dWbgp5p+o2<7+o3 z4)7EQ32RE%RN*n-f{*aK`v;mL@ zyuty4hJP<{z~Z92e!epgn$?7TzSb~tfPjDGf&t}#LBzioIN;Pp?)v!-E-Tf21`O5_ zA`Z}DpW6Zh$N_l?=Ksx6*Z&I`&iz9ycba6KH|VyXZ!8(;pA8Zat0+(%f+s_vSKc9Xr!8Z07Ar2UgBH3`#5hGG1Dy4k$yL`{lV6%4ZTmLAPMefH(- zI~#6c&t3Ng_J^DQT}^oWoef*r_OmW$=Po@8_;s||H{d~^pp^LcC9g0f1JYS74|t3Nq{Y9-IN-F!W9$VN z=j|>lr&?g2IKXIsfw+H!ed?q&ZQ)+e4`?53K4l$?fvr^^EAS8pNR5AwaKIJEf}d}P zJL~hQem({Ld>rFeIoSbd2NYv5_MacvVyms!W8Uceu@BwM%CUs!I6!**OUwZn&-dv* z@3)KoJ)Z8udAktL7lnO))kGYi*;h>5Ux**09k8r4#&-S7J&s|Y^TD&9xQ8t*R6NB2 zp1{AP9B{i!YjfiIe7N^l9rqmnm=g!IrSka^ip{4!G&m3ww@z&H*>9JilQBU>vce zK0(jnUjhy||A+(nj(zm=%>qAP3^0$0IKXP(LG6Bn3$uCl$F2X~H~izcfEUIFJ&Avp zIpDtcpV(LY<31b;`1v^Yc}^T)p9Ahb_XB;yKDPnxJm(6n4IrT%;92~;$N~Mb{n5{N z)$s>4;hy84>;NnL?U(kv{oj4t``KAb zX9M=J-EVIDi399lzxtgu2gBORHsku7{@ytU9ocLHNMr|i9{(%UBHd;Hjs2zn^1XpOYP+ za)FJ$0FK3Rd%WNI?{9v%zK)2Dc7Sa7_W}oGBBPGQ|Dd1m?1RD2Ct{x;69?Gahtz@t zWWm3Tc7Tld_W}p3Ic#3nV*pQWS_|=fX%+{(V(9NEm=;s@Qcs`DOo)ZT+&jIfG|F+iF9&6xVCObeL{Es%_Uv@hH->(x6 z*s*2>OM#z{V_u&V2RO?CpE>t~uJ!-#gf)U1>ki4s0dnErdmMm!lVM+A*Om(``vbeS zoX>FHpdRl==fnZdalqPTuokOJ@jtkpU<3cS2B};eARqp{$pPpS#Bl)}8^nG+t_Oq( zkMDc`3Fk7BVKDPzjwF9KXKXCwYfUxIC zb-!`MK2J04#RQG$z6UWv%4mT7_tkgwoCAJv<*h6k2-w8GyLNz-_$Lk^4(JgFEXo$x zZ?5azd;nZm*xK&rZNHeiV+Tl!f8qe*fF5zcVddF9;Q+)wt}SlQ?iaDokNeL7QsbXE zfH9DsJf|6H@fvAzJVD?FY^Fr$5j+T(-%a)9*sCk`ME=n)5`Mn@U?1iKj*z%@m2 zU13~XT-;+s<$;d&97bE9e;nWm{1XQd2lR*o!r&Lgu|bRvXeUMpzl&>%;<~~d_hRl9 zJHRveCk`ME=phG$fCn&65MzaLz5vb{LO&t4@0joyKf|@ea6Q3HIAn->uW>*=t+>Ab z55)hyp&-8PBu$JU#tBi35XA^}KQ@SV0CIr+q}^XY#JzYdvmJnb35frH!{_qfyh?FT zDiXAZf3gF}4(PERFj+VN%UMVzce@OKAK?F2nP|v9{)q#K1Be66*A=z7t}w<_6;jGB z_wir&NhTI%DLeMR_#NsC81ewc3U?nXY~KdJK7%*=0%6!A68wKZavlHW!gsSZ{1XS@ zx*%ezt_@~3N7(Fh;sD|Rt35{}{t;G4Cm(Yg|KR_7IG>2B?LXq5I6%a|`8q*n*yo=U z2M`Ce;(#cuFI!9}A9Nf4^Gf*}im8MK|HJ{r0TKTYGfhJOKcYKs%oU<>0d=iW6MsMg z`p?k+zus;9sFR5D5v{)q#K15ELc+@Qm~cupKJUF#n--~c5WX2neET(|K*x13)* zp3fNKpE!UxfH;894>HO-j}^|uW2~qs2fB^_S+lY;=M?j%_$Lk^4j>LN!ao13GY-fr z3078=X>Q{mpN}y>7z2PEzVK0uCro)ZTU2WW6FjuQ|E z*x~>M;%#8e?R>X!&!4}bT+Yod<}BNP;s6o*{Fv+j4IdB(SaLv4iL-1nsc`&BkWYa) zpckxs0383AlO4eEk2!GwaRB!RVs7pe48hu{#kBG}2{5mD1?U5Oz6iV|V%+MOIDj}n zgL~==0QNZ#7;``z=FAr}%H109NpLc6taRjfKHC=l$qo?lPj)~P7f@e7V+){9FbjV6 zQZ9Xn1d!Lf!h!`0_JwhP=QFVgaRBTqiu;MRO4@lsA`ehsK*IyX0d)>Qxj@YSZvlAw zXuv1Q$-GM8h7znvlnMul11#`Q`wh03A0YMv@R&G2=MQT zBL0a3TDAZD8rcChd4M>8<-xyJ%4YLs_>-7xN^JzV}Xc&;sD|RSgS5- zn18STuiR1U+jfHckvCOe=l{8L|`4Vz%V?EswTUrMKsG{c$9pMx*(L5Npq z;GgV(ws?U20XBJHKsi8(gwW@IzswkK^={+2TwqSQ_*y0oF@f9$APyi7puRxUz61Kb z#&F#enCJ6)v6u?A!kFyW=2i+D)b#;H{1XQd2M`Ce0= z?>F@Why#cNhyx5cAPLyOSf66C_;xFNdEq+t17JL0PgdU#APyi7AP&%QKor)3fU*9U zy>9pGc?oQQqvn(fdo|+$h=1Y$;sD|Rl>@@CwnrhA*jvgdEA;pogmc&{;3h*~0C50u z0C51rI3MUIZyp4!8n$Gu#Z)=LgN6 zy+8N^o-!O0puRv`wg7Q}w>cmQYvF>g{};t#V)`Iq+^Q^d%jMwQvEp;;-of1d7js%e z#5NBQ2Y8bM)cC$^`ngOdHfn{JLA+kcWJbXb_<1=){s7Ug@8p;`z*`(pi|u`;lFdd3 z5!-glRH?+@js5`g3EE#P#Ec(^1H8cj>3CG_?~kQYQ|;npkiWNJf%5k-SLmxaHc0US z-SGf%fV>=lG5y&Dtl^hVO^k)Z?;K=Iw_7%KY!KoDV9xMfiW4LbFkUajXdMx6tRE1q z?%BB)*2ms5=wp2CVqg6JoO0n9j1@K<8z7$``2-E)gyiOc6wKo-r&E8Z2)0I~xlwgYgVo;0*urEKc8VmAFjviFS;Ztt_jbLWoC8!vwr*Ak=k1j!Dt z_6xY%7l=blC$?A6=099cM}zjXoW5lx0k8#*#x+H8UEw+pkR3pFfFyQ60`}#_{dvl{ z)YD@*Z?4y8g+U3fzpcsxg&*P`BL#K8alX&UXj^*@(%$3Zegm}UXs3IQBJN?%E(71* zPr%+^wZP}wR|1?2`YTMqeTHzaG3*!Mo};wyu)V$k`2@T73F3YnY1ro)*5`xvKJ$H; z%eEW<=ZU>J28|fzjg4iXU+_st&yVM`tOzzi2G<;ic@=gN*#Tq+Xxe^ppAli+U@R~K z<4_^6Iiao3ruRS$&+Ykidj6o{$*bjAuwcQyU=tiUw_N=E+;ZW?+0Z|zz#QWO>_uwD z2gCuy0eTL=u{+#v8`tDO`?8YFya>4eJd8;nv4CuR*=b_Ryh0%ZTyPQC1rNX+qdjwq zd4}->$O$;-Fc13^W?_s56OS>l%>$uPrf5@AUJ@+82lh zh8gL&HoER_d|{W-^>;L`PZ(VnUcl(G@B#*e3(ms@Im-6*A8kw2p@zG+3~T_`~~~s|7r1jNBmzC=H*lGzsvl(E&pA& z{zI$ZTlqJwu3Pz6t^BiA*IPKCkp5Z`K=t}YvkIvBrzgy=*ZkYN%&yz?pPN=d%|CA{ zV1M=fO$9u^dfilj8>-h$1z5YxHX0YOCLV*qr|S1>;$h6Bfc5belLGqr^GymkpIc#_=or3W{q-2zRefRY|61+gT?}8)-?+cw3ka8uUO;%f;RD3m8;Dr*bMaAhJgFq` F{{d;tUS - - - + diff --git a/assets/keymap.toml b/assets/keymap.toml index 9ae250ab..49bd55a3 100644 --- a/assets/keymap.toml +++ b/assets/keymap.toml @@ -24,7 +24,6 @@ os = ["linux", "windows"] "ctrl-shift-b" = "workspace::ToggleSidebar" "ctrl-shift-e" = "files::ToggleExplorer" "ctrl-shift-c" = "connections::ToggleServers" -"ctrl-e" = "workspace::SwapSides" "ctrl-," = "workspace::OpenSettings" "ctrl-shift-q" = "workspace::Quit" "ctrl-shift-n" = "connections::NewConnection" @@ -62,7 +61,6 @@ os = ["macos"] "cmd-b" = "workspace::ToggleSidebar" "cmd-shift-e" = "files::ToggleExplorer" "cmd-shift-c" = "connections::ToggleServers" -"cmd-e" = "workspace::SwapSides" "cmd-," = "workspace::OpenSettings" "cmd-q" = "workspace::Quit" "cmd-n" = "connections::NewConnection" diff --git a/crates/acp/src/bridge.rs b/crates/acp/src/bridge.rs index 9a55a93a..12647bd9 100644 --- a/crates/acp/src/bridge.rs +++ b/crates/acp/src/bridge.rs @@ -14,7 +14,7 @@ use futures::{ future::{Either, select}, }; use nocterm_ai::{ - BridgeCall, BridgeLaunch, BridgeRegistration, BridgeRejection, ToolBridge, + BridgeCall, BridgeLaunch, BridgeRegistration, BridgeRejection, ToolBridge, ToolOffer, mcp::{McpSession, McpStep}, }; use nocterm_core::Paths; @@ -30,9 +30,16 @@ type Listener = std::os::unix::net::UnixListener; #[cfg(not(unix))] type Listener = std::net::TcpListener; +/// One connection's way to write lines, shared by its replies and the notifications sent to it, +/// so a line is never written into the middle of another. +type Writer = Arc>; + struct Registration { token: String, sockets: BTreeMap, + writers: BTreeMap, + /// What the chat offers its agent now: the tools listed and admitted. + offer: ToolOffer, cancel: async_channel::Sender<()>, cancelled: async_channel::Receiver<()>, } @@ -122,11 +129,14 @@ impl ToolBridge for BridgeServer { Registration { token: token.clone(), sockets: BTreeMap::new(), + writers: BTreeMap::new(), + offer: ToolOffer::default(), cancel, cancelled, }, ); let inner = Arc::downgrade(&self.inner); + let offering = inner.clone(); let launch = BridgeLaunch { program: relay.program.clone(), args: relay.args.clone(), @@ -143,7 +153,12 @@ impl ToolBridge for BridgeServer { revoke(&inner, id); } }), - )) + ) + .with_offer(Arc::new(move |id, offer| { + if let Some(inner) = offering.upgrade() { + set_offer(&inner, id, offer); + } + }))) } fn calls(&self) -> async_channel::Receiver { self.inner.calls_rx.clone() @@ -311,6 +326,44 @@ fn close_registration(registration: Registration) { } registration.cancel.close(); } +/// Records what a chat offers and, when it changed, tells its connected agents to list the +/// tools again. The notice is written on a thread of its own: a busy agent never blocks the chat. +fn set_offer(inner: &Inner, id: u64, offer: ToolOffer) { + let writers: Vec = { + let mut state = inner.state.lock().unwrap_or_else(|e| e.into_inner()); + let Some(registration) = state.registrations.get_mut(&id) else { + return; + }; + if registration.offer == offer { + return; + } + registration.offer = offer; + registration.writers.values().cloned().collect() + }; + if writers.is_empty() { + return; + } + let _ = std::thread::Builder::new() + .name("nocterm-bridge-notice".into()) + .spawn(move || { + for writer in writers { + let mut socket = writer.lock().unwrap_or_else(|e| e.into_inner()); + let _ = write_line(&mut socket, &nocterm_ai::mcp::list_changed()); + } + }); +} +fn write_line(socket: &mut Socket, value: &Value) -> io::Result<()> { + let bytes = serde_json::to_vec(value)?; + if bytes.len() > nocterm_ai::MAX_MCP_LINE { + return Err(io::Error::new( + io::ErrorKind::InvalidData, + "Bridge response too large", + )); + } + socket.write_all(&bytes)?; + socket.write_all(b"\n")?; + socket.flush() +} fn revoke(inner: &Inner, id: u64) { let registration = inner .state @@ -385,7 +438,7 @@ fn serve(socket: Socket, inner: Arc, epoch: u64, connection_id: u64) -> i .get("token") .and_then(Value::as_str) .unwrap_or_default(); - let (id, cancel) = { + let (id, cancel, writer) = { let mut state = inner.state.lock().unwrap_or_else(|e| e.into_inner()); if inner.generation.load(Ordering::Acquire) != epoch { return Err(io::Error::new( @@ -413,32 +466,44 @@ fn serve(socket: Socket, inner: Arc, epoch: u64, connection_id: u64) -> i "Thread connection limit", )); } + let writer: Writer = Arc::new(Mutex::new(socket.try_clone()?)); registration .sockets .insert(connection_id, socket.try_clone()?); - (*id, registration.cancelled.clone()) + registration.writers.insert(connection_id, writer.clone()); + (*id, registration.cancelled.clone(), writer) }; - let result = serve_authenticated(socket, &mut reader, &inner, id, cancel); + let result = socket + .set_read_timeout(None) + .and_then(|()| serve_authenticated(&writer, &mut reader, &inner, id, cancel)); let mut state = inner.state.lock().unwrap_or_else(|e| e.into_inner()); if let Some(registration) = state.registrations.get_mut(&id) { registration.sockets.remove(&connection_id); + registration.writers.remove(&connection_id); } result } fn serve_authenticated( - mut socket: Socket, + writer: &Writer, reader: &mut BufReader, inner: &Inner, id: u64, cancel: async_channel::Receiver<()>, ) -> io::Result<()> { - socket.set_read_timeout(None)?; let mut session = McpSession::default(); while let Some(line) = bounded_line(reader, nocterm_ai::MAX_MCP_LINE)? { if cancel.is_closed() { break; } - let response = match session.handle(&line) { + let offer = inner + .state + .lock() + .unwrap_or_else(|e| e.into_inner()) + .registrations + .get(&id) + .map(|registration| registration.offer) + .unwrap_or_default(); + let response = match session.handle(&line, offer) { McpStep::Reply(reply) => reply, McpStep::Ignore => continue, McpStep::Rejected { @@ -489,16 +554,10 @@ fn serve_authenticated( nocterm_ai::mcp::tool_result(request_id, result) } }; - let bytes = serde_json::to_vec(&response)?; - if bytes.len() > nocterm_ai::MAX_MCP_LINE { - return Err(io::Error::new( - io::ErrorKind::InvalidData, - "Bridge response too large", - )); - } - socket.write_all(&bytes)?; - socket.write_all(b"\n")?; - socket.flush()?; + write_line( + &mut writer.lock().unwrap_or_else(|e| e.into_inner()), + &response, + )?; } Ok(()) } diff --git a/crates/acp/src/bridge/tests.rs b/crates/acp/src/bridge/tests.rs index c8997696..ccf65df4 100644 --- a/crates/acp/src/bridge/tests.rs +++ b/crates/acp/src/bridge/tests.rs @@ -138,6 +138,46 @@ fn authenticates_routes_and_revokes_pending_calls() { } assert!(pending.respond.is_canceled()); } +fn listed(socket: &mut Socket, reader: &mut BufReader, id: u64) -> Vec { + writeln!( + socket, + "{}", + serde_json::json!({"jsonrpc":"2.0","id":id,"method":"tools/list"}) + ) + .unwrap(); + let response: Value = + serde_json::from_str(&bounded_line(reader, 1024 * 1024).unwrap().unwrap()).unwrap(); + response["result"]["tools"] + .as_array() + .unwrap() + .iter() + .map(|tool| tool["name"].as_str().unwrap().to_owned()) + .collect() +} +#[test] +fn a_chat_offering_file_tools_tells_its_agent_to_list_them() { + let dir = tempfile::tempdir().unwrap(); + let server = bridge(&dir); + let registration = server.register().unwrap(); + let (mut socket, mut reader) = authenticated(®istration); + initialize(&mut socket, &mut reader); + assert!(!listed(&mut socket, &mut reader, 2).contains(&"read_file".to_owned())); + registration.offer(ToolOffer::new(true)); + let notice: Value = + serde_json::from_str(&bounded_line(&mut reader, 1024 * 1024).unwrap().unwrap()).unwrap(); + assert_eq!(notice["method"], "notifications/tools/list_changed"); + assert!(notice.get("id").is_none()); + assert!(listed(&mut socket, &mut reader, 3).contains(&"read_file".to_owned())); + registration.offer(ToolOffer::new(true)); + registration.offer(ToolOffer::default()); + let notice: Value = + serde_json::from_str(&bounded_line(&mut reader, 1024 * 1024).unwrap().unwrap()).unwrap(); + assert_eq!( + notice["method"], "notifications/tools/list_changed", + "the same offer again sends nothing; a change does" + ); + assert!(!listed(&mut socket, &mut reader, 4).contains(&"read_file".to_owned())); +} #[test] fn rejects_bad_tokens_and_limits_thread_connections() { let dir = tempfile::tempdir().unwrap(); @@ -214,21 +254,15 @@ fn actual_stdio_relay_roundtrip() { .iter() .map(|tool| tool["name"].as_str().unwrap()) .collect(); + let catalog: std::collections::BTreeSet<_> = + ToolOffer::default().tools().map(|spec| spec.name).collect(); assert_eq!( - names, - [ - "list_terminals", - "open_terminal", - "read_terminal", - "send_input", - "run_command", - "exec_command", - "read_command", - "cancel_command", - ] - .into_iter() - .collect(), + names, catalog, + "a chat offers its terminal tools from the start" ); + for transfer in ["transfer_files", "read_transfer", "cancel_transfer"] { + assert!(names.contains(transfer)); + } } #[test] fn bounds_unauthenticated_connections_and_protocol_lines() { diff --git a/crates/acp/src/client/lifecycle_tests.rs b/crates/acp/src/client/lifecycle_tests.rs index 51acc68d..61ba1b45 100644 --- a/crates/acp/src/client/lifecycle_tests.rs +++ b/crates/acp/src/client/lifecycle_tests.rs @@ -32,6 +32,7 @@ for line in sys.stdin: args: vec!["-c".into(), script.into(), mode.into()], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.into(), terminal_auth: false, diff --git a/crates/acp/src/client/restore_classification.rs b/crates/acp/src/client/restore_classification.rs index b8466a3f..7f4513ea 100644 --- a/crates/acp/src/client/restore_classification.rs +++ b/crates/acp/src/client/restore_classification.rs @@ -45,6 +45,7 @@ for line in sys.stdin: ], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: dir.path().to_owned(), terminal_auth: false, diff --git a/crates/acp/src/client/startup_failures.rs b/crates/acp/src/client/startup_failures.rs index 3dc22f6d..0b5e5638 100644 --- a/crates/acp/src/client/startup_failures.rs +++ b/crates/acp/src/client/startup_failures.rs @@ -26,6 +26,7 @@ fn request( args, env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.to_owned(), terminal_auth: false, diff --git a/crates/acp/src/client/tests.rs b/crates/acp/src/client/tests.rs index bc31a139..25fe9227 100644 --- a/crates/acp/src/client/tests.rs +++ b/crates/acp/src/client/tests.rs @@ -51,6 +51,7 @@ for line in sys.stdin: args: vec!["-c".into(), script.into()], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.path().to_owned(), }) @@ -157,6 +158,7 @@ for line in sys.stdin: args: vec!["-c".into(), script], env: Default::default(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.path().to_owned(), }) @@ -306,6 +308,7 @@ fn oversized_subprocess_line_fails_initialization_and_stops_processes() { ], env: Default::default(), inherit_env: Vec::new(), + hub: None, }; let result = AcpConnector::unmanaged().connect(ConnectRequest { terminal_auth: false, @@ -576,6 +579,7 @@ for line in sys.stdin: args: vec!["-u".into(), "-c".into(), script], env: Default::default(), inherit_env: vec!["PATH".into()], + hub: None, }; let connection = AcpConnector::unmanaged() .connect(ConnectRequest { diff --git a/crates/acp/src/lib.rs b/crates/acp/src/lib.rs index 7465087d..e7a87390 100644 --- a/crates/acp/src/lib.rs +++ b/crates/acp/src/lib.rs @@ -8,6 +8,8 @@ //! and, on Linux, the `run_agent_host` entry point with its `AgentHostError`. //! Settings section: none. Globals: none. //! Invariants: SDK types never leave this crate; relay registrations authenticate callers; revocation closes owned sockets. +//! The bridge lists and admits only the tools a registration's chat offers, and tells its +//! connected agents when the offer changes without blocking the chat. //! Native taskkill diagnostics retain the original I/O error through shared typed causes; //! the public process shutdown port retains its existing string diagnostic. mod bridge; diff --git a/crates/acp/src/mapping/content.rs b/crates/acp/src/mapping/content.rs index 5fe28580..fedc62a8 100644 --- a/crates/acp/src/mapping/content.rs +++ b/crates/acp/src/mapping/content.rs @@ -40,7 +40,7 @@ pub(super) fn content(value: wire::ContentBlock) -> Result }), wire::ContentBlock::Image(value) => Content::Image(Image { annotations: annotations(value.annotations)?, - data: value.data, + data: value.data.into(), mime_type: value.mime_type, uri: value.uri, meta: extensions(value.meta), @@ -112,7 +112,7 @@ pub(super) fn wire_content(value: Content) -> wire::ContentBlock { .meta(meta(value.meta)), ), Content::Image(value) => wire::ContentBlock::Image( - wire::ImageContent::new(value.data, value.mime_type) + wire::ImageContent::new(String::from(value.data), value.mime_type) .uri(value.uri) .annotations(wire_annotations(value.annotations)) .meta(meta(value.meta)), diff --git a/crates/acp/src/mapping/info.rs b/crates/acp/src/mapping/info.rs index 014d4bc7..239dbe0b 100644 --- a/crates/acp/src/mapping/info.rs +++ b/crates/acp/src/mapping/info.rs @@ -8,6 +8,8 @@ use nocterm_ai::model::{ pub(crate) fn capabilities(value: &wire::AgentCapabilities) -> AgentCapabilitiesSummary { AgentCapabilitiesSummary { image_prompts: value.prompt_capabilities.image, + keeps_history: false, + own_tools: false, } } /// Authentication methods of an unknown future shape are not offered. diff --git a/crates/acp/src/mapping/requests.rs b/crates/acp/src/mapping/requests.rs index 344b90d7..e0f849d3 100644 --- a/crates/acp/src/mapping/requests.rs +++ b/crates/acp/src/mapping/requests.rs @@ -56,6 +56,7 @@ pub(crate) fn opened(value: wire::NewSessionResponse) -> SessionOpened { session: SessionId::new(value.session_id.to_string()), modes: value.modes.map(modes), configuration: value.config_options.map(options), + history: None, } } pub(crate) fn prompt_outcome(value: wire::PromptResponse) -> Result { diff --git a/crates/acp/src/process.rs b/crates/acp/src/process.rs index 0d11e500..7244332e 100644 --- a/crates/acp/src/process.rs +++ b/crates/acp/src/process.rs @@ -22,7 +22,6 @@ pub(crate) struct Spawned { pub secrets: Arc>, } -#[expect(clippy::too_many_lines, reason = "predates the limit")] pub(crate) fn spawn( request: &ConnectRequest, helper: Option<&Path>, @@ -32,20 +31,7 @@ pub(crate) fn spawn( std::env::vars_os() .filter_map(|(key, value)| Some((key.into_string().ok()?, value.into_string().ok()?))), ); - let mut secret_values: Vec = env - .iter() - .filter(|(name, _)| { - ["KEY", "TOKEN", "PASSWORD", "SECRET", "PROXY"] - .iter() - .any(|term| name.to_uppercase().contains(term)) - }) - .map(|(_, value)| value.clone()) - .chain(request.launch.env.values().cloned()) - .filter(|value| value.len() >= 4) - .collect(); - secret_values.sort_by_key(|value| std::cmp::Reverse(value.len())); - secret_values.dedup(); - let secrets = Arc::new(secret_values); + let secrets = secret_values(request, &env); let executable = resolve(&request.launch.command, &env, &request.working_directory) .ok_or_else(|| { AgentError::Io(nocterm_ai::redact::redact(&format!( @@ -84,6 +70,24 @@ pub(crate) fn spawn( } #[cfg(not(target_os = "linux"))] command.args(&process_args); + let child = spawn_child(command, request, env)?; + let group = Arc::new(ProcessGroup::new( + child.id(), + #[cfg(target_os = "linux")] + managed, + )); + Ok(Spawned { + child, + group, + secrets, + }) +} + +fn spawn_child( + mut command: std::process::Command, + request: &ConnectRequest, + env: BTreeMap, +) -> Result { command .env_clear() .envs(env) @@ -96,7 +100,7 @@ pub(crate) fn spawn( use std::os::unix::process::CommandExt as _; command.process_group(0); } - let child = async_process::Command::from(command) + async_process::Command::from(command) .stdin(Stdio::piped()) .stdout(Stdio::piped()) .stderr(Stdio::piped()) @@ -105,17 +109,24 @@ pub(crate) fn spawn( .map_err(|source| match &request.sandbox { Some(_) => AgentError::Sandbox(nocterm_ai::sandbox::SandboxLaunchError::spawn(source)), None => AgentError::Io(format!("Cannot start agent: {source}")), - })?; - let group = Arc::new(ProcessGroup::new( - child.id(), - #[cfg(target_os = "linux")] - managed, - )); - Ok(Spawned { - child, - group, - secrets, - }) + }) +} + +fn secret_values(request: &ConnectRequest, env: &BTreeMap) -> Arc> { + let mut values: Vec<_> = env + .iter() + .filter(|(name, _)| { + ["KEY", "TOKEN", "PASSWORD", "SECRET", "PROXY"] + .iter() + .any(|term| name.to_uppercase().contains(term)) + }) + .map(|(_, value)| value.clone()) + .chain(request.launch.env.values().cloned()) + .filter(|value| value.len() >= 4) + .collect(); + values.sort_by_key(|value| std::cmp::Reverse(value.len())); + values.dedup(); + Arc::new(values) } fn resolve( @@ -199,6 +210,7 @@ mod tests { args, env: BTreeMap::new(), inherit_env: Vec::new(), + hub: None, }, working_directory: directory.to_owned(), } diff --git a/crates/agent-chat/Cargo.toml b/crates/agent-chat/Cargo.toml index cf0e778c..dbab22f0 100644 --- a/crates/agent-chat/Cargo.toml +++ b/crates/agent-chat/Cargo.toml @@ -14,15 +14,16 @@ layer = "service" test-support = [] [dependencies] +nocterm-core.workspace = true gpui.workspace = true nocterm-ai.workspace = true nocterm-agent-runtime.workspace = true nocterm-agent-directory.workspace = true nocterm-store.workspace = true nocterm-session.workspace = true +nocterm-transfers.workspace = true nocterm-workspace-ports.workspace = true futures.workspace = true -async-channel.workspace = true serde.workspace = true serde_json.workspace = true tracing.workspace = true @@ -33,6 +34,8 @@ gpui = { workspace = true, features = ["test-support"] } nocterm-session = { workspace = true, features = ["test-support"] } nocterm-ai = { workspace = true, features = ["test-support"] } nocterm-agent-runtime = { workspace = true, features = ["test-support"] } +nocterm-hubs-model.workspace = true +nocterm-transfers = { workspace = true, features = ["test-support"] } tempfile.workspace = true [lints] diff --git a/crates/agent-chat/src/chat_deps.rs b/crates/agent-chat/src/chat_deps.rs index 6c0ea831..7b8a0ce8 100644 --- a/crates/agent-chat/src/chat_deps.rs +++ b/crates/agent-chat/src/chat_deps.rs @@ -3,10 +3,17 @@ //! [`ChatDeps`] carries the runtime, agent directory, settings, chat //! metadata, the window seat and the sign-in terminal. Views keep their own //! collaborators (images, layout) in the panel host. +use crate::thread::{AgentThread, Fork, RestartData}; use crate::{runtime::RuntimeHandle, seat::Seat, sign_in::SignInTerminal}; -use gpui::App; +use gpui::{App, AppContext as _, Context, Entity, EntityId}; use nocterm_agent_directory::AgentsHandle; -use nocterm_ai::{history::SavedChat, metadata::ChatMetadataSource}; +use nocterm_agent_runtime::{CatalogRow, ChatOpenError, DocumentClaim, DocumentClaimOutcome}; +use nocterm_ai::{ + history::{ContextId, SavedChat}, + local_environment::ChatScope, + metadata::ChatMetadataSource, + storage::{AgentStorageError, require_local_scope}, +}; use nocterm_store::SettingsHandle; use std::{rc::Rc, sync::Arc}; @@ -55,7 +62,7 @@ impl ChatDeps { self.settings().setting::(cx) } pub fn enabled(&self, cx: &App) -> bool { - self.ai(cx).enabled + self.runtime().check_current().is_ok() && self.ai(cx).enabled } /// What chats may do with the window. pub fn seat(&self) -> &Seat { @@ -68,8 +75,229 @@ impl ChatDeps { pub fn timestamp(&self) -> u64 { self.inner.metadata.timestamp() } - pub fn new_chat(&self, agent: String) -> SavedChat { + pub fn new_chat(&self, agent: String) -> Result { + self.runtime().check_current()?; + let scope = self.runtime().chat_scope()?; + Ok(self.frame_chat(agent, scope)) + } + fn frame_chat(&self, agent: String, scope: ChatScope) -> SavedChat { let identity = self.inner.metadata.new_identity(); - SavedChat::for_identity(agent, identity.id, identity.created) + SavedChat::for_environment(agent, identity.id, identity.created, scope) + } + pub(crate) fn new_chat_for_origin( + &self, + agent: String, + origin: &nocterm_ai::execution_origin::ChatKey, + ) -> SavedChat { + self.frame_chat(agent, origin.chat_scope()) + } + pub fn scope(&self) -> ContextId { + self.runtime().scope() + } + pub fn chat_scope(&self) -> Result { + self.runtime().chat_scope() + } + pub(crate) fn require_local_scope(&self, actual: &ChatScope) -> Result<(), AgentStorageError> { + self.runtime().check_current()?; + require_local_scope(&self.chat_scope()?, actual) + } + fn reserve( + &self, + key: nocterm_ai::execution_origin::ChatKey, + replacing: Option, + cx: &mut App, + ) -> Result<(DocumentClaim, Self), ChatOpenError> { + self.require_local_scope(&key.chat_scope())?; + // Seat.id is the original root window entity, not an inner terminal surface. + let window = self.seat().id(); + let outcome = match replacing { + Some(owner) => self + .runtime() + .replace_document(key.id().into(), owner, window, cx)?, + None => self.runtime().claim_document(key.id().into(), window, cx)?, + }; + let claim = match outcome { + DocumentClaimOutcome::Owned(claim) => claim, + DocumentClaimOutcome::AlreadyOpen { window } => { + return Err(ChatOpenError::AlreadyOpen { key, window }); + } + DocumentClaimOutcome::Deleted => return Err(ChatOpenError::Deleted(key)), + }; + let deps = self.for_chat(claim.key())?; + Ok((claim, deps)) + } + fn for_chat( + &self, + key: &nocterm_ai::execution_origin::ChatKey, + ) -> Result { + self.require_local_scope(&key.chat_scope())?; + Ok(Self { + inner: Rc::new(Deps { + runtime: self.inner.runtime.clone(), + agents: self.inner.agents.clone(), + settings: self.inner.settings.clone(), + metadata: self.inner.metadata.clone(), + seat: self.inner.seat.for_chat(key)?, + sign_in: self.inner.sign_in.clone(), + }), + }) + } + fn build_committed( + &self, + claim: DocumentClaim, + deps: Self, + cx: &mut App, + build: impl FnOnce( + Self, + crate::thread::local_payloads::LocalPayloadAllocation, + &mut Context, + ) -> AgentThread, + ) -> Result, ChatOpenError> { + let allocation = self.reserve_payload(cx)?; + self.build_allocated(claim, deps, allocation, cx, build) + } + fn reserve_payload( + &self, + cx: &mut App, + ) -> Result { + let allocations = self.runtime().local_history_allocations(cx); + let resident = allocations + .reserve_resident(None, cx) + .map_err(|error| ChatOpenError::Storage(error.into_storage()))?; + Ok((allocations, resident)) + } + fn build_allocated( + &self, + claim: DocumentClaim, + deps: Self, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, + cx: &mut App, + build: impl FnOnce( + Self, + crate::thread::local_payloads::LocalPayloadAllocation, + &mut Context, + ) -> AgentThread, + ) -> Result, ChatOpenError> { + let thread = cx.new(|cx| build(deps, allocation, cx)); + self.runtime() + .commit_document(claim, crate::thread::client(&thread), cx)?; + Ok(thread) + } + fn build_claimed( + &self, + key: nocterm_ai::execution_origin::ChatKey, + replacing: Option, + cx: &mut App, + build: impl FnOnce( + Self, + crate::thread::local_payloads::LocalPayloadAllocation, + &mut Context, + ) -> AgentThread, + ) -> Result, ChatOpenError> { + let (claim, deps) = self.reserve(key, replacing, cx)?; + self.build_committed(claim, deps, cx, build) + } + pub(crate) fn create_chat( + &self, + agent: String, + cx: &mut App, + ) -> Result, ChatOpenError> { + let chat = self.new_chat(agent)?; + chat.validate_identity().map_err(AgentStorageError::from)?; + let key = nocterm_ai::execution_origin::ChatKey::from_header(&chat.header); + self.build_claimed(key, None, cx, |deps, allocation, cx| { + let mut thread = AgentThread::new(chat, deps, allocation, cx); + thread.preview_config(cx); + thread + }) + } + pub(crate) fn restart_chat( + &self, + data: RestartData, + expected: EntityId, + cx: &mut App, + ) -> Result, ChatOpenError> { + self.build_claimed( + data.key().clone(), + Some(expected), + cx, + |deps, allocation, cx| AgentThread::restarted(data, deps, allocation, cx), + ) + } + pub(crate) fn fork_chat( + &self, + fork: Fork, + cx: &mut App, + ) -> Result, ChatOpenError> { + let fork = fork.prepared().map_err(AgentStorageError::from)?; + let (claim, deps) = self.reserve(fork.key(), None, cx)?; + self.build_committed(claim, deps, cx, |deps, allocation, cx| { + AgentThread::forked(fork, deps, allocation, cx) + }) + } + /// Reserve exact ownership before constructing an entity or starting work. + pub fn restore_chat( + &self, + chat: SavedChat, + cx: &mut App, + ) -> Result, ChatOpenError> { + self.require_local_scope(&chat.header.chat_scope())?; + chat.validate_identity().map_err(AgentStorageError::from)?; + let key = nocterm_ai::execution_origin::ChatKey::from_header(&chat.header); + let chat = nocterm_ai::history::prepare_owned(chat).map_err(AgentStorageError::from)?; + let (claim, deps) = self.reserve(key, None, cx)?; + self.runtime() + .admit_claimed_history(&claim, chat.resident_bytes(), cx)?; + self.build_committed(claim, deps, cx, |deps, allocation, cx| { + AgentThread::restored(chat, deps, allocation, cx) + }) + } + /// Admit catalog metadata and ownership before exposing a saved chat. + pub fn restore_archive( + &self, + row: CatalogRow, + cx: &mut App, + ) -> Result, ChatOpenError> { + self.restore_archive_as(row, None, cx) + } + /// Restores archived chat `row` in place of `expected`, the entity another window held it + /// in, which keeps nothing unsaved. + pub(crate) fn restore_archive_in_place_of( + &self, + row: CatalogRow, + expected: EntityId, + cx: &mut App, + ) -> Result, ChatOpenError> { + self.restore_archive_as(row, Some(expected), cx) + } + fn restore_archive_as( + &self, + row: CatalogRow, + replacing: Option, + cx: &mut App, + ) -> Result, ChatOpenError> { + let summary = row.summary(); + self.require_local_scope(&summary.chat_scope())?; + summary.validate().map_err(AgentStorageError::from)?; + let key = nocterm_ai::execution_origin::ChatKey::from_summary(summary); + let (claim, deps) = self.reserve(key, replacing, cx)?; + self.runtime().admit_claimed_history(&claim, 0, cx)?; + let allocation = self.reserve_payload(cx)?; + let prepared = self + .runtime() + .take_prepared_chat(&claim, allocation.1.clone(), cx)?; + match prepared { + Some(prepared) => { + let built = prepared.map(|chat| { + self.build_allocated(claim, deps, allocation, cx, |deps, allocation, cx| { + AgentThread::restored(chat, deps, allocation, cx) + }) + }); + built.value().clone() + } + None => self.build_allocated(claim, deps, allocation, cx, |deps, allocation, cx| { + AgentThread::archived(row, deps, allocation, cx) + }), + } } } diff --git a/crates/agent-chat/src/lib.rs b/crates/agent-chat/src/lib.rs index d4ebda36..9b89870b 100644 --- a/crates/agent-chat/src/lib.rs +++ b/crates/agent-chat/src/lib.rs @@ -1,22 +1,35 @@ //! Agent chats without views: one [`thread::AgentThread`] per chat and the -//! [`roster::ChatRoster`] of a window's chats. -//! Command jobs privately assemble their owned runner; snapshots and sign-in waits expose -//! immutable facts. +//! [`roster::ChatRoster`] of a window's selected and retained context chats. +//! Command jobs use a supplied session-domain registry; the chat owns scheduling its runner. +//! File transfers go through a supplied `TransferRequests` port; each chat reads and cancels +//! only the transfers it submitted, bound to both endpoint terminals' sessions. +//! A chat offers the file tools while an attached terminal serves files but runs no programs, +//! and runs them only on such terminals, on the filesystem captured with the call's session. +//! Snapshots and sign-in waits expose immutable facts. //! //! Owns: AgentThread coordination, private Composer/ChatConfig/ChatDocument owners, private //! SessionDriver and ToolRuntime owners, the session-scoped ApprovalInbox for provider //! requests, bridge cards, card tokens and terminal grants, ChatRoster membership and chat -//! lifecycle (new, restart, fork, restore, removal admission, resident history budget). +//! lifecycle (new, restart, fork, restore and removal admission). +//! Runtime owns installation-wide history admission. Catalog archives retain an accounted +//! row reference; quiet hidden archives release their entities and references. +//! ChatDeps binds an immutable Local environment lease, context and exact per-chat original seat; every entity factory +//! acquires a scoped document claim and binds the seat before constructing an entity. ChatDocument identity retains its ChatKey. //! ThreadNotice owns status/error presentation facts; ThreadBindings retains the //! window/subscriptions. Finite operations return named completions and original-authority //! polling projections. Archive owns lazy hydration generations, typed pending intentions //! and delivery residency. BackgroundOpen owns the native-opening handoff and its original //! workspace receipt. +//! A hub agent's chat is an ordinary chat: an agent that keeps its sessions reopens them +//! without a saved transcript and replaces it with the history it returns, and a turn the +//! agent runs for another device shows as a turn of the chat (ADR 0148). +//! The private Transcript admits live byte/row growth before mutation; capacity failure preserves accepted rows and queued prompts. //! Does not own: views, the workspace shell, ACP transport, connection admission, -//! filesystem storage or image decoding. +//! filesystem storage, native command execution or image decoding. //! Ports used: AgentConnector and ToolBridge through the runtime, terminal ports from //! nocterm-workspace-ports, [`seat::ChatWorkspace`], [`sign_in::SignInTerminal`] and -//! ChatMetadataSource, all supplied with [`chat_deps::ChatDeps`]. +//! ChatMetadataSource, all supplied with [`chat_deps::ChatDeps`]. The retained runtime supplies +//! CommandJobsFactory; each chat creates its own bounded registry without depending on an adapter. //! Ports provided: [`seat::ChatWorkspace`] and [`sign_in::SignInTerminal`] for the window //! shell to implement. //! Settings section: reads ai; its schema belongs to nocterm-ai. @@ -33,7 +46,8 @@ //! old publication failures cannot pause a newer desired document. //! Session operations carry their original ticket, connection, session and registration //! authority. Tool work retains fixed prompt scope, original executor identities and -//! workspace receipts; cancellation preserves readable job outcomes. +//! workspace receipts; cancellation preserves readable job outcomes. Actual tool dispatch and +//! every background runner poll recheck their original account lease. //! Joined reconnect calls retain their original busy guards and dispatch in acceptance //! order after validating original session, registration, workspace and window authority. //! Ordering releases after synchronous launch; direct calls after connection retain @@ -48,8 +62,10 @@ use nocterm_agent_runtime as runtime; pub use runtime::TerminalAuthRequest; /// Why a terminal sign-in did not succeed. -#[derive(Debug, Clone, thiserror::Error, PartialEq, Eq)] +#[derive(Debug, Clone, thiserror::Error)] pub enum TerminalAuthError { + #[error("Authentication terminal could not be opened.")] + Opening(#[source] nocterm_core::SharedCause), /// The user closed the sign-in terminal. #[error("Authentication canceled.")] Canceled, diff --git a/crates/agent-chat/src/roster.rs b/crates/agent-chat/src/roster.rs index 9757022e..7517d916 100644 --- a/crates/agent-chat/src/roster.rs +++ b/crates/agent-chat/src/roster.rs @@ -5,18 +5,16 @@ //! it, and keeps resident history within budget. Views decide when these //! transitions run and react to what the roster reports. pub mod chat_list; +mod contexts; use crate::{ chat_deps::ChatDeps, thread::{AgentThread, Attachment, RestartData}, }; use chat_list::{ChatList, ChatSlot}; -use gpui::{AnyWindowHandle, App, AppContext as _, Entity, EntityId, Subscription}; +use gpui::{AnyWindowHandle, App, Entity, EntityId, Subscription}; use nocterm_agent_runtime::AgentPersistenceError; -/// Resident transcripts above this many bytes are evicted, oldest first. -const RESIDENT_HISTORY_BUDGET: usize = 64 * 1024 * 1024; - /// How a chat the user opens begins. pub enum ChatStart { /// A new chat with agent `agent`, attaching terminal `terminal`. @@ -25,7 +23,10 @@ pub enum ChatStart { terminal: Option, }, /// The same chat in a new agent process. - Restart(Box), + Restart { + data: Box, + expected_owner: EntityId, + }, } /// Why a chat could not be removed: its document was not retained. @@ -49,43 +50,139 @@ impl Retained { } pub struct ChatRoster { - host: ChatDeps, - chats: ChatList, + contexts: contexts::ContextChats, } impl ChatRoster { pub fn new(host: ChatDeps) -> Self { Self { - host, - chats: ChatList::default(), + contexts: contexts::ContextChats::new(host), + } + } + pub fn host(&self) -> &ChatDeps { + self.contexts.current().host() + } + pub fn contains_any(&self, id: EntityId) -> bool { + self.contexts.contains_any(id) + } + pub fn release_quiet_hidden(&mut self, cx: &App) { + self.contexts.release_quiet_hidden(cx); + } + /// Switches the visible collection; retained threads keep original hosts. + pub fn switch_context(&mut self, host: ChatDeps, cx: &mut App) -> bool { + if host.chat_scope().ok() == self.host().chat_scope().ok() { + return self.contexts.select(host); + } + if let Some(previous) = self.current() { + previous.update(cx, |thread, cx| { + thread.flush_draft(cx); + thread.set_shown(false, cx); + }); } + let released: Vec<_> = self + .iter() + .filter(|thread| thread.read(cx).can_release_archive()) + .map(|thread| thread.entity_id()) + .collect(); + self.contexts + .current_mut() + .chats_mut() + .remove_admitted(&released); + self.contexts.select(host); + if let Some(current) = self.current() { + current.update(cx, |thread, cx| thread.set_shown(true, cx)); + } + true + } + /// Retain device-local snapshots before removing an unavailable original source. + /// This never grants a new account lease or redirects the old writer. + pub fn retire_stale_sources(&mut self, cx: &mut App) -> Retained { + let stale = self + .contexts + .all() + .flat_map(|scope| { + scope + .chats() + .iter() + .filter(|thread| !thread.read(cx).source_current()) + .map(|thread| (scope.host().runtime().clone(), thread.entity_id())) + }) + .collect::>(); + let mut removed = Vec::new(); + let mut error = None; + for (runtime, id) in stale { + match runtime.capture_and_detach_document(id, cx) { + Ok(true) => removed.push(id), + Ok(false) => {} + Err(cause) => { + error = Some(RetainError(cause)); + break; + } + } + } + self.contexts.remove_all(&removed); + Retained { removed, error } } pub fn current(&self) -> Option> { - self.chats.current() + self.contexts.current().chats().current() + } + pub fn find_any(&self, id: EntityId) -> Option<&Entity> { + self.contexts.origin(id)?.chats().find(id) + } + pub fn has_retained_chats(&self) -> bool { + self.contexts.all().any(|scope| !scope.chats().is_empty()) + } + /// Append to a captured document's original context, without switching views. + pub fn append_origin( + &mut self, + slot: ChatSlot, + cx: &App, + ) -> Result { + let thread = slot.thread(); + let scope = thread.read(cx).document().key().chat_scope(); + let id = thread.entity_id(); + let visible = Some(&scope) == self.host().chat_scope().ok().as_ref(); + let origin = self + .contexts + .scope_mut(&scope) + .ok_or(nocterm_agent_runtime::ChatOpenError::Closed)?; + origin.chats_mut().append(slot); + if !visible { + origin.chats_mut().select(id); + } + Ok(visible) } pub fn find(&self, id: EntityId) -> Option<&Entity> { - self.chats.find(id) + self.contexts.current().chats().find(id) } pub fn contains(&self, id: EntityId) -> bool { - self.chats.contains(id) + self.contexts.current().chats().contains(id) } pub fn iter(&self) -> impl ExactSizeIterator> { - self.chats.iter() + self.contexts.current().chats().iter() } pub fn is_empty(&self) -> bool { - self.chats.is_empty() + self.contexts.current().chats().is_empty() + } + /// Leaves the Local roster with no selected document when another captured + /// conversation is presented. Uses the same Local visibility transition. + pub fn deselect(&mut self, cx: &mut App) { + if let Some(previous) = self.current() { + previous.update(cx, |thread, cx| thread.set_shown(false, cx)); + } + self.contexts.current_mut().chats_mut().deselect(); } /// Opens chat `id`, hiding the chat open before. pub fn select(&mut self, id: EntityId, cx: &mut App) { if let Some(previous) = self.current() { previous.update(cx, |thread, cx| thread.set_shown(false, cx)); } - self.chats.select(id); + self.contexts.current_mut().chats_mut().select(id); if let Some(current) = self.current() { current.update(cx, |thread, cx| thread.set_shown(true, cx)); } } - /// Registers `thread`'s document and binds its background sessions to - /// `window`; `observation` lives as long as the chat stays listed. + /// Binds an already admitted chat's background sessions to `window`; + /// `observation` lives as long as the chat stays listed. pub fn track( &self, thread: &Entity, @@ -93,34 +190,32 @@ impl ChatRoster { observation: Subscription, cx: &mut App, ) -> ChatSlot { - self.host - .runtime() - .register_document(crate::thread::client(thread), cx); thread.update(cx, |thread, _| thread.bind_window(window)); ChatSlot::new(thread.clone(), observation) } pub fn append(&mut self, slot: ChatSlot) { - self.chats.append(slot); + self.contexts.current_mut().chats_mut().append(slot); } pub fn prepend(&mut self, slots: Vec) { - self.chats.prepend(slots); + self.contexts.current_mut().chats_mut().prepend(slots); } /// Creates the chat `start` describes, or none for an unknown agent. - pub fn start(&self, start: ChatStart, cx: &mut App) -> Option> { + pub fn start( + &self, + start: ChatStart, + cx: &mut App, + ) -> Result>, nocterm_agent_runtime::ChatOpenError> { let agent = match &start { ChatStart::New { agent, .. } => agent.as_str(), - ChatStart::Restart(data) => data.agent_id(), + ChatStart::Restart { data, .. } => data.agent_id(), }; - self.host.agents().get(agent, cx)?; - self.host.runtime().set_last_agent(agent, cx); - let host = self.host.clone(); - Some(match start { + if self.host().agents().get(agent, cx).is_none() { + return Ok(None); + } + let agent = agent.to_owned(); + let thread = match start { ChatStart::New { agent, terminal } => { - let thread = cx.new(|cx| { - let mut thread = AgentThread::new(agent, host, cx); - thread.preview_config(cx); - thread - }); + let thread = self.host().create_chat(agent, cx)?; if let Some(id) = terminal { thread.update(cx, |thread, cx| { thread.add_composer_attachment(Attachment::Terminal(id), cx) @@ -128,8 +223,13 @@ impl ChatRoster { } thread } - ChatStart::Restart(data) => cx.new(|cx| AgentThread::restarted(*data, host, cx)), - }) + ChatStart::Restart { + data, + expected_owner, + } => self.host().restart_chat(*data, expected_owner, cx)?, + }; + self.host().runtime().set_last_agent(&agent, cx); + Ok(Some(thread)) } /// Copies loaded chat `source` up to and including entry `last` (all of /// it, with `None`) into a new, saved chat. @@ -138,43 +238,106 @@ impl ChatRoster { source: &Entity, last: Option, cx: &mut App, - ) -> Entity { + ) -> Result, nocterm_agent_runtime::ChatOpenError> { + let host = self + .contexts + .origin(source.entity_id()) + .ok_or(nocterm_agent_runtime::ChatOpenError::Closed)? + .host(); + host.require_local_scope(&source.read(cx).document().key().chat_scope())?; let fork = match last { - Some(last) => source.read(cx).fork_at(last + 1), - None => source.read(cx).fork(), + Some(last) => source.update(cx, |thread, cx| thread.fork_at(last + 1, cx)), + None => source.update(cx, |thread, cx| thread.fork(cx)), }; - let host = self.host.clone(); - let thread = cx.new(|cx| AgentThread::forked(fork, host, cx)); + let thread = host.fork_chat(fork?, cx)?; thread.update(cx, |thread, cx| thread.persist(cx)); - thread + Ok(thread) + } + /// The chats of this collection other windows hold, which this window lists without + /// holding them. + pub fn held_elsewhere(&self, cx: &App) -> Vec> { + let host = self.host(); + host.runtime() + .held_elsewhere(host.seat().id(), cx) + .into_iter() + .filter_map(|chat| chat.upgrade()?.downcast::().ok()) + .collect() + } + /// Opens in this window chat `chat`, which another window holds, in its place: while it + /// neither works nor shows there, with what that window had of it. `None` when it stays, + /// or is an archived chat whose row the catalog is reloading. + pub fn take_over( + &self, + chat: &Entity, + cx: &mut App, + ) -> Result>, nocterm_agent_runtime::ChatOpenError> { + let host = self.host(); + let expected = chat.entity_id(); + if !host.runtime().may_move(expected, host.seat().id(), cx) { + return Ok(None); + } + if chat.read(cx).is_archived() { + if !chat.read(cx).can_release_archive() { + return Ok(None); + } + let id = chat.read(cx).chat_id().to_owned(); + let row = host.runtime().catalog_snapshot(cx).and_then(|catalog| { + (0..catalog.rows().len()) + .filter_map(|index| catalog.row(index)) + .find(|row| row.summary().id == id) + }); + return row + .map(|row| host.restore_archive_in_place_of(row, expected, cx)) + .transpose(); + } + chat.update(cx, |thread, cx| thread.release_resources(cx)); + let data = chat + .update(cx, |thread, cx| thread.restart_data(cx)) + .map_err(nocterm_agent_runtime::ChatOpenError::from)?; + host.restart_chat(data, expected, cx).map(Some) } /// The chats saved in earlier runs and not listed yet, newest first: - /// loaded chats, then archived summaries. + /// loaded chats, then archived summaries. A chat another window holds stays there. pub fn saved(&self, cx: &mut App) -> Vec> { - let (chats, catalog) = self.host.runtime().take_history(cx); + let Some(catalog) = self.host().runtime().catalog_snapshot(cx) else { + return Vec::new(); + }; + let elsewhere: std::collections::HashSet = self + .held_elsewhere(cx) + .iter() + .map(|thread| thread.read(cx).chat_id().to_owned()) + .collect(); let mut restored = Vec::new(); - for chat in chats.into_iter().flatten().rev() { - let host = self.host.clone(); - restored.push(cx.new(|cx| AgentThread::restored(chat, host, cx))); - } - for summary in catalog.into_iter().flatten().rev() { + for index in (0..catalog.rows().len()).rev() { + let Some(row) = catalog.row(index) else { + continue; + }; + if elsewhere.contains(&row.summary().id) { + continue; + } if self - .chats + .contexts + .current() + .chats() .iter() .chain(&restored) - .any(|thread| thread.read(cx).chat_id() == summary.id) + .any(|thread| thread.read(cx).chat_id() == row.summary().id) { continue; } - let host = self.host.clone(); - restored.push(cx.new(|cx| AgentThread::archived(summary, host, cx))); + match self.host().restore_archive(row, cx) { + Ok(thread) => restored.push(thread), + // Another window is opening it now. + Err(nocterm_agent_runtime::ChatOpenError::AlreadyOpen { .. }) => {} + Err(error) => tracing::warn!(%error, "could not admit archived chat"), + } } restored } /// Captures chat `id`'s document so it survives removal. fn admit_before_removal(&self, id: EntityId, cx: &mut App) -> Result<(), RetainError> { - if self.host.runtime().capture_and_detach_document(id, cx)? - && let Some(thread) = self.chats.find(id) + if self.host().runtime().capture_and_detach_document(id, cx)? + && let Some(thread) = self.contexts.current().chats().find(id) { thread.update(cx, |thread, cx| { thread.document_admitted(thread.document().desired_revision(), cx) @@ -191,7 +354,9 @@ impl ChatRoster { cx: &mut App, ) -> Retained { let proposed: Vec<_> = self - .chats + .contexts + .current() + .chats() .iter() .filter(|thread| !keep(thread, cx)) .map(|thread| thread.entity_id()) @@ -209,51 +374,82 @@ impl ChatRoster { .collect::>(); Retained { removed, error } } + /// Global AI disable captures every retained context before releasing slots. + pub fn clear_all(&mut self, cx: &mut App) -> Retained { + let clients: Vec<_> = self + .contexts + .all() + .flat_map(|scope| { + scope + .chats() + .iter() + .map(|thread| (scope.host().clone(), thread.clone())) + }) + .collect(); + let mut removed = Vec::new(); + let mut error = None; + for (host, thread) in clients { + match host + .runtime() + .capture_and_detach_document(thread.entity_id(), cx) + { + Ok(admitted) => { + if admitted { + thread.update(cx, |thread, cx| { + thread.document_admitted(thread.document().desired_revision(), cx) + }); + } + removed.push(thread.entity_id()); + } + Err(failure) => error = Some(RetainError::from(failure)), + } + } + self.contexts.remove_all(&removed); + Retained { removed, error } + } /// Removes admitted chats `ids` and their observations. pub fn remove(&mut self, ids: &[EntityId]) { - self.chats.remove_admitted(ids); + self.contexts.current_mut().chats_mut().remove_admitted(ids); } /// Releases chat `id`'s agent and deletes its saved copy; the chat stays /// listed until it is removed. pub fn delete(&self, id: EntityId, cx: &mut App) -> bool { - let Some(thread) = self.chats.find(id).cloned() else { + let Some(thread) = self.contexts.current().chats().find(id).cloned() else { return false; }; let chat = thread.read(cx).document().chat_id().to_owned(); thread.update(cx, |thread, cx| thread.release_resources(cx)); - self.host.runtime().delete_chat(chat, cx); + self.host().runtime().delete_chat(chat, cx); true } /// Evicts the oldest resident transcripts other than the open chat's /// until history fits the budget. Returns the evicted chats whose /// transcripts mention `query`. pub fn trim_resident_history(&self, query: &str, cx: &mut App) -> Vec { - let active = self.current().map(|thread| thread.entity_id()); - let mut resident: usize = self - .chats + let matches: std::collections::HashSet<_> = self + .contexts + .current() + .chats() .iter() - .map(|thread| thread.read(cx).resident_history_bytes()) - .sum(); - let mut candidates = self.chats.iter().cloned().collect::>(); - candidates.sort_by_key(|thread| thread.read(cx).document().updated()); - let mut matched = Vec::new(); - for thread in candidates { - if resident <= RESIDENT_HISTORY_BUDGET { - break; - } - if Some(thread.entity_id()) == active { - continue; - } - let bytes = thread.read(cx).resident_history_bytes(); - let mentions = !query.is_empty() && thread.read(cx).transcript().mentions(query); - let chat_id = thread.read(cx).document().chat_id().to_owned(); - if thread.update(cx, |thread, cx| thread.evict_history(cx)) { - resident = resident.saturating_sub(bytes); - if mentions { - matched.push(chat_id); - } + .filter(|thread| !query.is_empty() && thread.read(cx).transcript().mentions(query)) + .map(|thread| thread.read(cx).document().chat_id().to_owned()) + .collect(); + match self.host().runtime().trim_history(cx) { + Ok(evicted) => evicted + .into_iter() + .filter(|key| { + self.host() + .chat_scope() + .as_ref() + .is_ok_and(|scope| key.chat_scope() == *scope) + && matches.contains(key.id()) + }) + .map(|key| key.id().to_owned()) + .collect(), + Err(error) => { + tracing::warn!(%error, "resident history cannot be trimmed"); + Vec::new() } } - matched } } diff --git a/crates/agent-chat/src/roster/chat_list.rs b/crates/agent-chat/src/roster/chat_list.rs index 8fec1b1b..53179d12 100644 --- a/crates/agent-chat/src/roster/chat_list.rs +++ b/crates/agent-chat/src/roster/chat_list.rs @@ -23,6 +23,9 @@ pub struct ChatList { active: Option, } impl ChatList { + pub fn deselect(&mut self) { + self.active = None; + } pub fn current(&self) -> Option> { self.active.and_then(|id| self.find(id)).cloned() } diff --git a/crates/agent-chat/src/roster/contexts.rs b/crates/agent-chat/src/roster/contexts.rs new file mode 100644 index 00000000..a7c8587e --- /dev/null +++ b/crates/agent-chat/src/roster/contexts.rs @@ -0,0 +1,108 @@ +//! Selected and retained chat collections keep immutable original dependencies. +use super::{ChatDeps, ChatList}; +use gpui::EntityId; +use nocterm_ai::local_environment::ChatScope; +use std::collections::HashMap; + +pub(super) struct ScopeChats { + host: ChatDeps, + chats: ChatList, +} +impl ScopeChats { + fn new(host: ChatDeps) -> Self { + Self { + host, + chats: ChatList::default(), + } + } + pub(super) fn host(&self) -> &ChatDeps { + &self.host + } + pub(super) fn chats(&self) -> &ChatList { + &self.chats + } + pub(super) fn chats_mut(&mut self) -> &mut ChatList { + &mut self.chats + } +} +pub(super) struct ContextChats { + selected: ScopeChats, + hidden: HashMap, +} +impl ContextChats { + pub(super) fn new(host: ChatDeps) -> Self { + Self { + selected: ScopeChats::new(host), + hidden: HashMap::new(), + } + } + pub(super) fn current(&self) -> &ScopeChats { + &self.selected + } + pub(super) fn current_mut(&mut self) -> &mut ScopeChats { + &mut self.selected + } + pub(super) fn contains_any(&self, id: EntityId) -> bool { + self.selected.chats.contains(id) + || self.hidden.values().any(|scope| scope.chats.contains(id)) + } + pub(super) fn release_quiet_hidden(&mut self, cx: &gpui::App) { + self.hidden.retain(|_, scope| { + let released: Vec<_> = scope + .chats + .iter() + .filter(|thread| thread.read(cx).can_release_archive()) + .map(|thread| thread.entity_id()) + .collect(); + scope.chats.remove_admitted(&released); + !scope.chats.is_empty() + }); + } + pub(super) fn origin(&self, id: EntityId) -> Option<&ScopeChats> { + self.all().find(|scope| scope.chats.contains(id)) + } + pub(super) fn scope_mut(&mut self, scope: &ChatScope) -> Option<&mut ScopeChats> { + if self + .selected + .host + .chat_scope() + .as_ref() + .is_ok_and(|current| current == scope) + { + Some(&mut self.selected) + } else { + self.hidden.get_mut(scope) + } + } + pub(super) fn all(&self) -> impl Iterator { + std::iter::once(&self.selected).chain(self.hidden.values()) + } + pub(super) fn remove_all(&mut self, ids: &[EntityId]) { + self.selected.chats.remove_admitted(ids); + self.hidden.retain(|_, scope| { + scope.chats.remove_admitted(ids); + !scope.chats.is_empty() + }); + } + pub(super) fn select(&mut self, host: ChatDeps) -> bool { + let scope = host.chat_scope().ok(); + if scope == self.selected.host.chat_scope().ok() { + self.selected.host = host; + return false; + } + let mut next = scope + .as_ref() + .and_then(|scope| self.hidden.remove(scope)) + .unwrap_or_else(|| ScopeChats::new(host.clone())); + // Future factories use the fresh original surface. Existing entities keep + // their own previously captured ChatDeps/Seat and are never retargeted. + next.host = host; + let previous = std::mem::replace(&mut self.selected, next); + if !previous.chats.is_empty() + && let Ok(scope) = previous.host.chat_scope() + { + self.hidden.insert(scope, previous); + } + true + } +} diff --git a/crates/agent-chat/src/seat.rs b/crates/agent-chat/src/seat.rs index 464fb42a..05e696e7 100644 --- a/crates/agent-chat/src/seat.rs +++ b/crates/agent-chat/src/seat.rs @@ -8,6 +8,12 @@ use nocterm_workspace_ports::{ConnectionSummary, ExpectedSession, TerminalEntry} use std::rc::Rc; pub trait ChatWorkspace: 'static { + /// Bind once to an exact immutable document. Providers must refuse another + /// scope or rebinding an already bound seat to a different chat. + fn for_chat( + &self, + key: &nocterm_ai::execution_origin::ChatKey, + ) -> Result; /// The identity of the window's workspace, stable while it lives. fn id(&self) -> EntityId; fn is_open(&self) -> bool; diff --git a/crates/agent-chat/src/thread.rs b/crates/agent-chat/src/thread.rs index 22440733..c54e67c6 100644 --- a/crates/agent-chat/src/thread.rs +++ b/crates/agent-chat/src/thread.rs @@ -34,7 +34,7 @@ mod execution; mod permissions; mod reconnects; mod session_grants; -pub use approval_inbox::ToolApprovalCard; +pub use approval_inbox::{ApprovalToken, ToolApprovalCard}; pub use permissions::PermissionCard; mod queue; mod tool_display; @@ -45,15 +45,17 @@ mod session_driver; mod tool_runtime; pub use restart::RestartData; mod document; +pub(crate) mod local_payloads; mod saved; mod sign_in_wait; +mod title; pub use sign_in_wait::SignInWait; mod session; mod tools; /// A chat's title from its name and transcript, as the history shows it. #[cfg(any(test, feature = "test-support"))] pub fn chat_title(name: Option<&str>, state: &ThreadState) -> String { - saved::chat_title(name, state) + title::chat_title(name, state) } #[derive(Clone, Debug, PartialEq, Eq)] @@ -80,12 +82,8 @@ impl Restore { self.fork } } -/// What a forked chat starts from. -pub struct Fork { - chat: nocterm_ai::history::SavedChat, - restore: Option, - attachments: Vec, -} +mod fork; +pub use fork::Fork; pub struct AgentThread { session: session_driver::SessionDriver, @@ -97,6 +95,7 @@ pub struct AgentThread { notice: notice::ThreadNotice, deps: crate::chat_deps::ChatDeps, bindings: bindings::ThreadBindings, + payloads: local_payloads::LocalPayloads, } impl AgentThread { @@ -117,6 +116,11 @@ impl AgentThread { cx.notify(); } + /// The original terminal collection captured when this document was opened. + pub fn workspace_seat(&self) -> &crate::seat::Seat { + self.deps.seat() + } + /// Ends background sessions this chat opened that it can no longer use. pub fn prune_background(&mut self, cx: &mut Context) { let resolved: Vec = self @@ -165,6 +169,12 @@ impl AgentThread { .is_none_or(|info| info.at_prompt != Some(true) || info.dirty_input || info.alt_screen) } + pub fn tool_approval_tokens(&self) -> Vec { + self.tools.approval_tool_tokens() + } + pub fn tool_approval_call(&self, token: ApprovalToken) -> Option<&nocterm_ai::TerminalCall> { + self.tools.approval_tool(token) + } pub fn tool_cards(&self) -> Vec { self.tools.approval_tool_cards() } @@ -188,6 +198,7 @@ impl AgentThread { } pub fn context(&mut self, cx: &App) -> String { + self.offer_tools(cx); let input = self::tool_runtime::resolution::Resolution::observe(self.deps.seat(), cx); self.tools .context(&input, self.composer.default_attachments(), cx) @@ -287,7 +298,9 @@ impl AgentThread { let owner = self::client(&cx.entity()); let runtime = self.deps.runtime().clone(); cx.defer(move |cx| { - runtime.request_activation(owner, cx); + if let Err(error) = runtime.request_activation(owner.clone(), cx) { + owner.emit(crate::runtime::SessionEvent::Failed(error.to_string()), cx); + } }); cx.notify(); } @@ -323,11 +336,17 @@ impl AgentThread { fn on_release(&mut self, id: EntityId, cx: &mut gpui::App) { self.cancel_pending(); self.finalize_tool_displays(); - let snapshot = self - .deps - .enabled(cx) - .then(|| self.capture_pending(cx)) - .flatten(); + let snapshot = if self.deps.enabled(cx) { + match self.capture_pending(cx) { + Ok(snapshot) => snapshot, + Err(error) => { + tracing::warn!(%error, "released chat capture failed"); + None + } + } + } else { + None + }; self.cancel_execution(); // The lease releases its connection when it is dropped. self.session.release_lease(); @@ -338,7 +357,9 @@ impl AgentThread { { tracing::warn!(%error,"released chat admission failed"); } - runtime.unregister_document(id, cx); + if let Err(error) = runtime.unregister_document(id, cx) { + tracing::warn!(%error, "could not retire released chat document"); + } }); if let Some(window) = self.bindings.window() { let seat = self.deps.seat().clone(); @@ -359,6 +380,7 @@ impl AgentThread { fn from_document( state: document::ChatDocument, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { let this = cx.weak_entity(); @@ -372,7 +394,7 @@ impl AgentThread { let release = cx.on_release(move |this, cx| this.on_release(id, cx)); Self { session: Default::default(), - tools: Default::default(), + tools: tool_runtime::ToolRuntime::attached(deps.runtime(), cx), archive: Default::default(), composer: Default::default(), state, @@ -380,31 +402,44 @@ impl AgentThread { notice: Default::default(), deps, bindings: bindings::ThreadBindings::new(release, authority), + payloads: local_payloads::LocalPayloads::new(allocation.0, allocation.1, id), } } - pub fn new(agent_id: String, deps: crate::chat_deps::ChatDeps, cx: &mut Context) -> Self { - let chat = deps.new_chat(agent_id); + pub(crate) fn new( + chat: nocterm_ai::history::SavedChat, + deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, + cx: &mut Context, + ) -> Self { Self::from_document( - document::ChatDocument::new(chat.agent_id, chat.id, chat.updated), + document::ChatDocument::new( + chat.header.agent_id.clone(), + nocterm_ai::execution_origin::ChatKey::from_header(&chat.header), + chat.header.updated, + ), deps, + allocation, cx, ) } - pub fn archived( - summary: nocterm_ai::history::ChatSummary, + pub(crate) fn archived( + row: nocterm_agent_runtime::CatalogRow, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { + let summary = row.summary(); let mut state = document::ChatDocument::new( summary.agent_id.clone(), - summary.id.clone(), + nocterm_ai::execution_origin::ChatKey::from_summary(summary), summary.updated, ); - state.archived_clean(&summary); - let mut thread = Self::from_document(state, deps, cx); + state.archived_clean(summary); + let mut thread = Self::from_document(state, deps, allocation, cx); thread.config.saved_model(summary.model.clone()); thread.notice.present("Saved chat".into()); - thread.archive = archive::Archive::archived(summary); + thread.archive = archive::Archive::archived(row); + thread.payloads.archive(); thread } pub fn is_archived(&self) -> bool { @@ -422,6 +457,10 @@ impl AgentThread { pub fn take_changed_rows(&mut self) -> std::collections::HashSet { self.state.take_changed_rows() } + /// Whether a row changed since the last [`Self::take_changed_rows`], without taking it. + pub fn has_changed_rows(&self) -> bool { + self.state.has_changed_rows() + } pub fn document(&self) -> &document::ChatDocument { &self.state } @@ -499,7 +538,6 @@ impl AgentThread { pub fn session_accepts_updates(&self) -> bool { self.session.accepts_updates() } - #[cfg(any(test, feature = "test-support"))] pub fn session_stopped(&self) -> bool { self.session.stopped() } diff --git a/crates/agent-chat/src/thread/approval_inbox.rs b/crates/agent-chat/src/thread/approval_inbox.rs index d9a20553..a79951ec 100644 --- a/crates/agent-chat/src/thread/approval_inbox.rs +++ b/crates/agent-chat/src/thread/approval_inbox.rs @@ -33,6 +33,7 @@ pub(super) struct ToolGate { attached: bool, unsafe_input: bool, expected: Option, + source_expected: Option, } impl ToolGate { pub(super) fn from_context( @@ -51,14 +52,20 @@ impl ToolGate { cx: &gpui::App, ) -> Self { let attached = match (call.terminal_id(), call.server_id()) { - (Some(id), _) => resolved.iter().any(|(candidate, _, _)| candidate == id), + (Some(_), _) => call + .terminals() + .all(|id| resolved.iter().any(|(candidate, _, _)| candidate == id)), (None, Some(id)) => offline.iter().any(|(server, _)| server.server_id == id), (None, None) => true, }; - let info = resolved - .iter() - .find(|(id, _, _)| Some(id.as_str()) == call.terminal_id()) - .and_then(|(_, entry, _)| entry.capabilities.state().info(cx)); + let expected_of = |wanted: Option<&str>| { + resolved + .iter() + .find(|(id, _, _)| Some(id.as_str()) == wanted) + .and_then(|(_, entry, _)| entry.capabilities.state().info(cx)) + }; + let info = expected_of(call.terminal_id()); + let source_expected = expected_of(call.source_terminal_id()).map(|info| info.expected); let unsafe_input = matches!(call, TerminalCall::SendInput(_)) && info.as_ref().is_none_or(|info| { info.at_prompt != Some(true) || info.dirty_input || info.alt_screen @@ -70,10 +77,18 @@ impl ToolGate { unsafe_input, info.map(|info| info.expected), ) + .with_source(source_expected) } pub(super) fn expected(&self) -> Option { self.expected } + pub(super) fn source_expected(&self) -> Option { + self.source_expected + } + fn with_source(mut self, source: Option) -> Self { + self.source_expected = source; + self + } pub(super) fn new( available: bool, @@ -88,6 +103,7 @@ impl ToolGate { attached, unsafe_input, expected, + source_expected: None, } } fn accepts(&self, call: &BridgeCall) -> bool { @@ -162,6 +178,8 @@ impl ApprovalInbox { return ToolDecision::Respond(call.into_call(), ToolRejection::Unavailable); } self.grants.observe(&call.call().call, gate.expected); + self.grants + .observe_source(&call.call().call, gate.source_expected); if self.grants.requires_approval(&call.call().call, policy) || gate.unsafe_input { self.generation = self.generation.wrapping_add(1); self.tools.push(PendingTool { @@ -187,15 +205,18 @@ impl ApprovalInbox { if !allow { return ToolDecision::Respond(call.into_call(), ToolRejection::Denied); } - if !gate.accepts(call.call()) || !gate.attached || call.expected() != gate.expected { + if !gate.accepts(call.call()) + || !gate.attached + || call.expected() != gate.expected + || call.source() != gate.source_expected + { return ToolDecision::Respond(call.into_call(), ToolRejection::Detached); } self.grants.observe(&call.call().call, gate.expected); - if grant - && !gate.unsafe_input - && let Some(id) = call.call().call.target() - { - self.grants.grant(id, call.call().call.capability()); + self.grants + .observe_source(&call.call().call, gate.source_expected); + if grant && !gate.unsafe_input { + self.grants.grant_call(&call.call().call); } ToolDecision::Execute(call) } @@ -228,6 +249,9 @@ impl ApprovalInbox { pub(super) fn permission_cards(&self) -> Vec { self.providers.cards() } + pub(super) fn tool_tokens(&self) -> Vec { + self.tools.iter().map(|entry| entry.token).collect() + } pub(super) fn tool_cards(&self) -> Vec { self.tools .iter() diff --git a/crates/agent-chat/src/thread/archive.rs b/crates/agent-chat/src/thread/archive.rs index 9036fd54..dbee69c6 100644 --- a/crates/agent-chat/src/thread/archive.rs +++ b/crates/agent-chat/src/thread/archive.rs @@ -1,11 +1,13 @@ //! Owns hydration generations, queued intentions, draft overlays and delivery permits. pub(in crate::thread) mod load_operation; +mod summary; use gpui::App; use nocterm_ai::{ history::{ChatSummary, HistoryPermit, SavedChat}, session::Ticket, storage::AgentStorageError, }; +use summary::ArchivedSummary; /// Runs once the saved chat is hydrated, outside the thread's update. pub type AfterHydration = Box; @@ -25,7 +27,7 @@ pub(super) struct LoadId(u64); enum ArchivePhase { Resident, Archived { - summary: ChatSummary, + summary: ArchivedSummary, loading: Option, }, } @@ -118,10 +120,10 @@ impl Default for Archive { } } impl Archive { - pub(super) fn archived(summary: ChatSummary) -> Self { + pub(super) fn archived(summary: impl Into) -> Self { Self { phase: ArchivePhase::Archived { - summary, + summary: summary.into(), loading: None, }, ..Self::default() @@ -133,6 +135,12 @@ impl Archive { ArchivePhase::Archived { summary, .. } => Some(summary), } } + pub(super) fn can_release(&self) -> bool { + matches!(self.phase, ArchivePhase::Archived { loading: None, .. }) + && self.pending.is_empty() + && self.draft_overlay.is_none() + && self.permit.is_none() + } pub(super) fn is_archived(&self) -> bool { self.summary().is_some() } @@ -164,15 +172,31 @@ impl Archive { self.draft_overlay = Some(DraftOverlay::new(text, updated)); true } - pub(super) fn draft_patch(&self, id: &str, agent: &str) -> Option { - self.is_archived().then_some(())?; + pub(super) fn draft_capacity(&self) -> usize { + self.draft_overlay + .as_ref() + .map_or(0, |overlay| overlay.text.len().saturating_mul(2)) + .saturating_add(self.summary().map_or(0, |summary| { + summary + .id + .len() + .saturating_add(summary.agent_id.len()) + .saturating_mul(2) + })) + .saturating_add(64 * 1024) + } + pub(super) fn draft_patch(&self) -> Option { + let summary = self.summary()?; self.draft_overlay.as_ref().map(|overlay| { - SavedChat::archive_draft( - id.to_owned(), - agent.to_owned(), + let mut chat = SavedChat::archive_draft_in_environment( + summary.id.clone(), + summary.agent_id.clone(), overlay.text.clone(), overlay.updated, - ) + summary.chat_scope(), + ); + chat.version = summary.transcript_version().wire(); + chat }) } pub(super) fn accept( @@ -180,7 +204,7 @@ impl Archive { load_id: LoadId, current: bool, result: Result, - permit: HistoryPermit, + permit: Option, ) -> LoadOutcome { let ArchivePhase::Archived { loading, .. } = &mut self.phase else { return LoadOutcome::Stale; @@ -191,7 +215,7 @@ impl Archive { *loading = None; match result { Ok(chat) => { - self.permit = Some(permit); + self.permit = permit; LoadOutcome::Hydrate { chat: Box::new(chat), overlay: self.draft_overlay.take(), @@ -219,7 +243,7 @@ impl Archive { return false; } self.phase = ArchivePhase::Archived { - summary, + summary: summary.into(), loading: None, }; self.resident_bytes = 0; diff --git a/crates/agent-chat/src/thread/archive/load_operation.rs b/crates/agent-chat/src/thread/archive/load_operation.rs index a7ae89f5..a2231558 100644 --- a/crates/agent-chat/src/thread/archive/load_operation.rs +++ b/crates/agent-chat/src/thread/archive/load_operation.rs @@ -1,10 +1,14 @@ -//! Hydration returns its original local generation and delivery permit together. +//! Original archive intent observes the shared admitted native-load service. use super::LoadId; +use crate::thread::AgentThread; +use gpui::Context; +use nocterm_agent_runtime::{LoadedHistory, history_admission::HistoryPayloadPermit}; use nocterm_ai::{ history::{HistoryPermit, SavedChat}, session::Ticket, storage::AgentStorageError, }; + pub(in crate::thread) struct LoadOperation { id: LoadId, ticket: Ticket, @@ -25,31 +29,42 @@ impl LoadOperation { reader, } } - pub(in crate::thread) async fn run(self) -> LoadCompletion { - let (result, permit) = self.reader.load(self.chat).await; - LoadCompletion { - id: self.id, - ticket: self.ticket, - result, - permit, - } + pub(in crate::thread) fn start( + self, + admission: nocterm_agent_runtime::history_admission::HistoryAdmissionHandle, + cx: &mut Context, + ) { + let loading = self.reader.load_admitted(self.chat, admission, cx); + cx.spawn(async move |thread, cx| { + let loaded = loading.await; + let completion = LoadCompletion { + id: self.id, + ticket: self.ticket, + loaded, + }; + let _ = thread.update(cx, |thread, cx| thread.on_archive_loaded(completion, cx)); + }) + .detach(); } } pub(in crate::thread) struct LoadCompletion { id: LoadId, ticket: Ticket, - result: Result, - permit: HistoryPermit, + loaded: LoadedHistory, } impl LoadCompletion { - pub(in crate::thread) fn into_parts( + pub(in crate::thread) fn publish( self, - ) -> ( - LoadId, - Ticket, - Result, - HistoryPermit, + publish: impl FnOnce( + LoadId, + Ticket, + Result, + Option, + Option, + ), ) { - (self.id, self.ticket, self.result, self.permit) + self.loaded.publish(|result, permit, payload| { + publish(self.id, self.ticket, result, permit, payload) + }); } } diff --git a/crates/agent-chat/src/thread/archive/summary.rs b/crates/agent-chat/src/thread/archive/summary.rs new file mode 100644 index 00000000..7d858a98 --- /dev/null +++ b/crates/agent-chat/src/thread/archive/summary.rs @@ -0,0 +1,27 @@ +//! Owns archived metadata without copying an accounted catalog payload. +use nocterm_agent_runtime::CatalogRow; +use nocterm_ai::history::ChatSummary; + +pub(in crate::thread) enum ArchivedSummary { + Owned(Box), + Catalog(CatalogRow), +} +impl From for ArchivedSummary { + fn from(summary: ChatSummary) -> Self { + Self::Owned(Box::new(summary)) + } +} +impl From for ArchivedSummary { + fn from(row: CatalogRow) -> Self { + Self::Catalog(row) + } +} +impl std::ops::Deref for ArchivedSummary { + type Target = ChatSummary; + fn deref(&self) -> &Self::Target { + match self { + Self::Owned(summary) => summary, + Self::Catalog(row) => row.summary(), + } + } +} diff --git a/crates/agent-chat/src/thread/archive/tests.rs b/crates/agent-chat/src/thread/archive/tests.rs index 777f1dfd..e14bbb21 100644 --- a/crates/agent-chat/src/thread/archive/tests.rs +++ b/crates/agent-chat/src/thread/archive/tests.rs @@ -10,10 +10,12 @@ fn saved() -> SavedChat { 1, ) } -fn permit(gate: &HistoryGate) -> HistoryPermit { - gate.acquire() - .now_or_never() - .expect("available history delivery") +fn permit(gate: &HistoryGate) -> Option { + Some( + gate.acquire() + .now_or_never() + .expect("available history delivery"), + ) } fn begin(archive: &mut Archive, intent: ArchiveIntent) -> LoadId { match archive.request(intent, SessionState::default().ticket()) { @@ -68,8 +70,8 @@ fn stale_delivery_does_not_take_latest_empty_draft_or_its_timestamp() { archive.accept(load, false, Ok(chat.clone()), permit(&gate)), LoadOutcome::Stale )); - let patch = archive.draft_patch(&chat.id, &chat.agent_id).unwrap(); - assert_eq!(patch.updated, 21); + let patch = archive.draft_patch().unwrap(); + assert_eq!(patch.header.updated, 21); assert!( patch.draft.is_none(), "the empty overlay is a real clearing edit" @@ -135,7 +137,7 @@ fn intentions_wait_for_one_generation_and_keep_accepted_order() { fn archived_without_edits_has_no_patch_and_failed_load_retains_literal_overlay() { let chat = saved(); let mut archive = Archive::archived(ChatSummary::from_chat(&chat)); - assert!(archive.draft_patch(&chat.id, &chat.agent_id).is_none()); + assert!(archive.draft_patch().is_none()); let load = begin(&mut archive, ArchiveIntent::Pin { pinned: true }); archive.edit_draft("literal".into(), 30); assert!(matches!( @@ -147,9 +149,9 @@ fn archived_without_edits_has_no_patch_and_failed_load_retains_literal_overlay() ), LoadOutcome::Failed(_) )); - let patch = archive.draft_patch(&chat.id, &chat.agent_id).unwrap(); + let patch = archive.draft_patch().unwrap(); assert_eq!(patch.draft.as_deref(), Some("literal")); - assert_eq!(patch.updated, 30); + assert_eq!(patch.header.updated, 30); } #[test] fn eviction_requires_every_durable_residency_gate() { diff --git a/crates/agent-chat/src/thread/bound_call.rs b/crates/agent-chat/src/thread/bound_call.rs index 619ee283..d4d7a39d 100644 --- a/crates/agent-chat/src/thread/bound_call.rs +++ b/crates/agent-chat/src/thread/bound_call.rs @@ -4,10 +4,23 @@ use nocterm_workspace_ports::ExpectedSession; pub(super) struct BoundToolCall { call: BridgeCall, expected: Option, + /// The session a transfer reads from; other calls have none. + source: Option, } impl BoundToolCall { pub(super) fn new(call: BridgeCall, expected: Option) -> Self { - Self { call, expected } + Self { + call, + expected, + source: None, + } + } + pub(super) fn with_source(mut self, source: Option) -> Self { + self.source = source; + self + } + pub(super) fn source(&self) -> Option { + self.source } pub(super) fn call(&self) -> &BridgeCall { &self.call diff --git a/crates/agent-chat/src/thread/client.rs b/crates/agent-chat/src/thread/client.rs index 6c16cbbf..618388f2 100644 --- a/crates/agent-chat/src/thread/client.rs +++ b/crates/agent-chat/src/thread/client.rs @@ -4,6 +4,8 @@ use super::{AgentThread, approval_inbox::ApprovalToken, permissions::PermissionG use super::{ approval_inbox::{ToolDecision, ToolRejection}, bound_call::BoundToolCall, + execution::WaitSubject, + tool_runtime::TransferAdmission, }; use crate::runtime::SessionEvent; use gpui::{App, Context}; @@ -22,6 +24,9 @@ use std::path::PathBuf; use std::sync::Arc; impl AgentThread { + pub fn source_current(&self) -> bool { + self.deps.runtime().check_current().is_ok() + } pub(super) fn on_tool_completion( &mut self, call: BridgeCall, @@ -32,29 +37,24 @@ impl AgentThread { cx.notify(); } + /// Offers the agent the file tools while an attached terminal takes them. + pub(super) fn offer_tools(&mut self, cx: &gpui::App) { + let input = super::tool_runtime::resolution::Resolution::observe(self.deps.seat(), cx); + let registration = self.session.registration().as_ref(); + self.tools.offer_tools( + &input, + self.composer.default_attachments(), + registration, + cx, + ); + } + pub fn revoke_executions(&mut self, cx: &gpui::App) { - if self.tools.active_commands().next().is_none() && self.tools.live_empty() { + if !self.tools.has_active_executions() && self.tools.live_empty() { return; } let resolved = self.resolved(cx); - let attached = resolved - .iter() - .map(|(id, _, _)| id.clone()) - .collect::>(); - let current = resolved - .into_iter() - .map(|(id, entry, _)| { - let executor = entry.capabilities.state().info(cx).and_then(|info| { - entry - .capabilities - .execution()? - .executor(info.expected, cx) - .ok() - }); - (id, executor) - }) - .collect::>(); - self.tools.revoke_replaced_executors(¤t, &attached); + self.tools.revoke_replaced(resolved, cx); } pub(super) fn on_execution_watch(&mut self, cx: &gpui::App) -> bool { @@ -63,7 +63,7 @@ impl AgentThread { } else { self.revoke_executions(cx); } - if self.tools.active_commands().next().is_none() { + if !self.tools.has_active_executions() { self.tools.finish_watch(); false } else { @@ -75,6 +75,42 @@ impl AgentThread { self.tools.cancel_execution(); } + pub(super) fn start_transfer(&mut self, admission: TransferAdmission, cx: &mut Context) { + let source = self + .resolved(cx) + .into_iter() + .find(|(id, _, _)| admission.reads_from(id)) + .map(|(_, entry, _)| entry); + match self.tools.start_transfer(admission, source, cx) { + Ok(started) => { + let (call, id, yield_ms) = started.into_parts(); + self.start_execution_watch(cx); + self.wait_tool(WaitSubject::transfer(&id.to_string()), yield_ms, call, cx); + } + Err(rejected) => { + let (call, error) = rejected.into_parts(); + self.finish(call, Err(error)); + } + } + } + + pub(super) fn read_transfer(&mut self, call: BridgeCall, cx: &mut Context) { + let TerminalCall::ReadTransfer(request) = &call.call else { + return; + }; + let subject = WaitSubject::transfer(&request.transfer_id); + self.wait_tool(subject, request.yield_ms.unwrap_or(0), call, cx); + } + + pub(super) fn cancel_transfer(&mut self, call: BridgeCall, cx: &mut Context) { + let TerminalCall::CancelTransfer(request) = &call.call else { + return; + }; + let redact = self.deps.ai(cx).approval.redact_secrets; + let result = self.tools.cancel_transfer(&request.transfer_id, redact); + self.finish(call, result); + } + pub(super) fn command_payload( &self, id: &str, @@ -150,34 +186,13 @@ impl AgentThread { let gate = self.tool_gate(&call.call, cx); let expected = gate.expected(); let decision = self.tools.approval_receive_tool( - BoundToolCall::new(call, expected), + BoundToolCall::new(call, expected).with_source(gate.source_expected()), &gate, &self.deps.ai(cx).approval, ); self.apply_tool_decision(decision, cx); } - fn tool_destination( - &mut self, - request: &TerminalCall, - entry: Option<&TerminalEntry>, - cx: &App, - ) -> Option { - if let Some(entry) = entry { - let info = entry.capabilities.state().info(cx)?; - if info.local { - return Some("This computer".into()); - } - let target = info.target?; - return Some(format!("{} · {target}", entry.title)); - } - let server = request.server_id()?; - self.offline_servers(cx) - .into_iter() - .find(|(descriptor, _)| descriptor.server_id == server) - .map(|(descriptor, summary)| format!("{} · {}", descriptor.name, summary.target)) - } - fn normalize_tool_displays(&mut self) { let patches = self.tools.normalize_displays(self.state.entries()); self.state.patch_tool_rows(patches, self.deps.timestamp()); @@ -227,7 +242,14 @@ impl AgentThread { .into_iter() .find(|(id, _, _)| Some(id.as_str()) == call.call.terminal_id()) .map(|(_, entry, _)| entry); - let destination = self.tool_destination(&call.call, provided_entry.or(entry.as_ref()), cx); + let input = super::tool_runtime::resolution::Resolution::observe(self.deps.seat(), cx); + let (entry, defaults) = ( + provided_entry.or(entry.as_ref()), + self.composer.default_attachments(), + ); + let destination = self + .tools + .destination(&call.call, entry, &input, defaults, cx); let mut display = ToolDisplay::requested(tool.into(), arguments.clone()); display.destination = destination.map(|text| nocterm_ai::redact::redact(&text)); if self.deps.ai(cx).approval.redact_secrets { @@ -243,18 +265,27 @@ impl AgentThread { cx.notify(); } - pub fn apply_presented_update(&mut self, mut update: SessionUpdate) -> ThreadChange { + pub fn apply_presented_update( + &mut self, + mut update: SessionUpdate, + ) -> Result { self.tools .prepare_displays(self.session.turn(), self.state.entries().len()); tool_display::strip_provider_meta(&mut update); + let tools_changed = matches!( + &update, + SessionUpdate::ToolCall(_) | SessionUpdate::ToolCallUpdate(_) + ); let change = if self.config.report_control(&update) { ThreadChange::Controls } else { - self.state.apply_transcript(update, self.deps.timestamp()) + self.state.apply_transcript(update, self.deps.timestamp())? }; - let patches = self.tools.normalize_displays(self.state.entries()); - self.state.patch_tool_rows(patches, self.deps.timestamp()); - change + if tools_changed { + let patches = self.tools.normalize_displays(self.state.entries()); + self.state.patch_tool_rows(patches, self.deps.timestamp()); + } + Ok(change) } pub(super) fn on_prompt_completion( @@ -283,11 +314,6 @@ impl AgentThread { if response.usage.is_some() { self.state.tokens(response.usage); } - // Agents that keep limits in their own files have - // just written this turn's. - let agent = self.state.agent_id().to_owned(); - let runtime = self.deps.runtime().clone(); - cx.defer(move |cx| runtime.refresh_limits(&agent, cx)); self.notice.present(if self.session.stopped() { "Stopped".into() } else { @@ -336,6 +362,12 @@ impl AgentThread { self.config .keep_control(self.session.ticket(), notification); } + if let SessionUpdate::Turn(activity) = ¬ification.update { + if self.session().as_ref() == Some(¬ification.session) { + self.external_turn(activity, cx); + } + return; + } if self.session().as_ref() != Some(¬ification.session) || !self.session.accepts_updates() || matches!(notification.update, SessionUpdate::UserMessageChunk(_)) @@ -354,7 +386,14 @@ impl AgentThread { notification.update, SessionUpdate::ConfigOptionUpdate(_) | SessionUpdate::CurrentModeUpdate(_) ); - match self.apply_presented_update(notification.update.clone()) { + let change = match self.apply_presented_update(notification.update.clone()) { + Ok(change) => change, + Err(error) => { + self.fail(&error.to_string(), cx); + return; + } + }; + match change { ThreadChange::Transcript => { if let Some(index) = index { self.mark_dirty(index); @@ -489,7 +528,14 @@ impl AgentThread { self.session.retain_failed_restore(ticket, pending_restore); } match result { - Ok(response) => { + Ok(mut response) => { + let history = response.history.take().filter(|_| restored == Some(true)); + if let Some(history) = history + && let Err(error) = self.state.replace_history(history, self.deps.timestamp()) + { + self.fail(&error.to_string(), cx); + return true; + } let session = response.session; if !self.session.accept_open( ticket, @@ -544,6 +590,8 @@ impl AgentThread { self.fail("Terminal bridge is unavailable.", cx); return; }; + // An agent that keeps its sessions reopens one even when nothing of it was saved here. + let keeps_history = info.capabilities.keeps_history; if !self .session .begin_open(commands.clone(), info, workdir.clone()) @@ -557,7 +605,9 @@ impl AgentThread { .session .restore() .clone() - .filter(|restore| !self.state.entries().is_empty() && restore.workdir.is_absolute()) + .filter(|restore| { + keeps_history || (!self.state.entries().is_empty() && restore.workdir.is_absolute()) + }) .map(|mut restore| { restore.workdir = workdir.clone(); restore @@ -664,7 +714,10 @@ impl AgentThread { } pub(super) fn runtime_event(&mut self, event: SessionEvent, cx: &mut Context) { match event { - SessionEvent::Leased(lease) => self.begin_lease(lease), + SessionEvent::Leased(lease) => { + self.begin_lease(lease); + self.offer_tools(cx); + } SessionEvent::Connected { connection, commands, diff --git a/crates/agent-chat/src/thread/client_port.rs b/crates/agent-chat/src/thread/client_port.rs index 5f2629ca..3560f49f 100644 --- a/crates/agent-chat/src/thread/client_port.rs +++ b/crates/agent-chat/src/thread/client_port.rs @@ -14,6 +14,9 @@ impl SessionClient for ThreadClient { fn id(&self) -> EntityId { self.0.entity_id() } + fn chat(&self) -> Option { + Some(self.0.clone().into()) + } fn alive(&self) -> bool { self.0.upgrade().is_some() } @@ -25,31 +28,66 @@ impl SessionClient for ThreadClient { fn state(&self, cx: &App) -> Option { let thread = self.0.upgrade()?; let thread = thread.read(cx); - Some(ClientState { - chat_id: thread.state.chat_id().to_owned(), - workdir: thread - .session - .restore() - .as_ref() - .filter(|restore| { - !thread.state.entries().is_empty() && restore.workdir.is_absolute() - }) - .map(|restore| restore.workdir.clone()), - agent_id: thread.state.agent_id().to_owned(), - session: thread.session().clone(), - busy: thread.session_busy(), - leased: thread.session.leased(), - activation_pending: thread.session.queued(), - closing: thread.session.closing(), - shown: thread.session.shown(), - }) - } - fn snapshot(&self, cx: &App) -> Option> { - self.0.upgrade()?.read(cx).flush_snapshot(cx) - } - fn capture(&self, cx: &mut App) -> Option<(Arc, u64)> { + let resume = thread + .session + .restore() + .as_ref() + .filter(|restore| !thread.state.entries().is_empty() && restore.workdir.is_absolute()) + .map(|restore| restore.workdir.clone()); + let origin = + nocterm_ai::execution_origin::ExecutionOrigin::new(thread.state.key().clone(), resume); + Some(ClientState::new( + origin, + thread.state.agent_id().to_owned(), + thread.session().clone(), + thread.session_busy(), + thread.session.leased(), + thread.session.queued(), + thread.session.closing(), + thread.session.shown(), + )) + } + fn history_usage(&self, cx: &App) -> Option { + let thread = self.0.upgrade()?; + let thread = thread.read(cx); + Some(nocterm_agent_runtime::HistoryUsage::new( + thread.resident_history_bytes(), + thread.document().updated(), + thread.session.shown(), + )) + } + fn history_payload(&self, cx: &App) -> Option { + let thread = self.0.upgrade()?; + let thread = thread.read(cx); + (!thread.is_archived() && thread.history_eviction_gate().is_permitted()) + .then(|| thread.payloads.order()) + .flatten() + } + fn evict_history(&self, cx: &mut App) -> bool { self.0 - .upgrade()? - .update(cx, |thread, cx| thread.capture_pending(cx)) + .upgrade() + .is_some_and(|thread| thread.update(cx, |thread, cx| thread.evict_history(cx))) + } + fn snapshot( + &self, + cx: &mut App, + ) -> Result>, nocterm_ai::storage::AgentStorageError> + { + let Some(thread) = self.0.upgrade() else { + return Ok(None); + }; + thread.update(cx, |thread, cx| thread.flush_snapshot(cx)) + } + fn capture( + &self, + cx: &mut App, + ) -> Result< + Option<(Arc, u64)>, + nocterm_ai::storage::AgentStorageError, + > { + let Some(thread) = self.0.upgrade() else { + return Ok(None); + }; + thread.update(cx, |thread, cx| thread.capture_pending(cx)) } } diff --git a/crates/agent-chat/src/thread/composer.rs b/crates/agent-chat/src/thread/composer.rs index 8e1e037f..1119876a 100644 --- a/crates/agent-chat/src/thread/composer.rs +++ b/crates/agent-chat/src/thread/composer.rs @@ -1,14 +1,16 @@ //! What the user is composing, apart from the conversation it goes into: the -//! unsent draft, the context and images for the next prompt, and the queue of -//! prompts waiting their turn. +//! unsent draft, the context, images and files for the next prompt, and the queue +//! of prompts waiting their turn. pub(in crate::thread) mod prompt_preparation; use super::{Attachment, queue::QueuedPrompt}; use nocterm_ai::images::PromptImage; +use nocterm_ai::prompt_files::{MAX_PROMPT_FILES, PromptFile, PromptFileError}; pub struct Composer { draft: Option>, attachments: Vec, images: Vec, + files: Vec, queue: Vec, queue_paused: bool, /// Composer edits suspend dispatch independently of stops and errors. @@ -21,6 +23,7 @@ pub struct Composer { struct Defaults { attachments: Vec, images: Vec, + files: Vec, } impl Default for Composer { @@ -29,6 +32,7 @@ impl Default for Composer { draft: None, attachments: Vec::new(), images: Vec::new(), + files: Vec::new(), queue: Vec::new(), queue_paused: false, queue_editing: false, @@ -54,10 +58,11 @@ impl Composer { replace: Option, attachments: Vec, ) -> Result { + // Files are small links: they are measured with the prompt itself. let mut saved = nocterm_ai::history::SavedPrompt { id: replace.unwrap_or(self.next_queue_id()), text, - images: Vec::new(), + images: self.files.iter().map(PromptFile::content).collect(), attachments, }; let encoded_len = nocterm_ai::history::prompt_size(&saved)? @@ -66,8 +71,10 @@ impl Composer { .iter() .map(|image| image.encoded_len()) .sum::() - + self.images().len().saturating_sub(1); + + self.images().len(); + let files = std::mem::take(&mut saved.images); saved.images = self.images().iter().map(|image| image.content()).collect(); + saved.images.extend(files); let prompt = QueuedPrompt::prepared(saved, self.attachments().to_vec(), Some(encoded_len)); let candidate = self.prepare_prompt_change(prompt, replace)?; let attachment_count = candidate @@ -94,6 +101,9 @@ impl Composer { pub fn images(&self) -> &[PromptImage] { &self.images } + pub fn files(&self) -> &[PromptFile] { + &self.files + } pub fn queue(&self) -> &[QueuedPrompt] { &self.queue } @@ -137,6 +147,7 @@ impl Composer { pub(super) fn reset_after_disable(&mut self) { self.clear_context(); self.images.clear(); + self.files.clear(); } pub(super) fn remove_image(&mut self, index: usize) -> bool { if index >= self.images.len() { @@ -145,6 +156,27 @@ impl Composer { self.images.remove(index); true } + /// Adds `files` the prompt does not hold yet. + pub(super) fn add_files(&mut self, files: Vec) -> Result<(), PromptFileError> { + let mut next = self.files.clone(); + for file in files { + if !next.contains(&file) { + next.push(file); + } + } + if next.len() > MAX_PROMPT_FILES { + return Err(PromptFileError::TooMany); + } + self.files = next; + Ok(()) + } + pub(super) fn remove_file(&mut self, index: usize) -> bool { + if index >= self.files.len() { + return false; + } + self.files.remove(index); + true + } pub(super) fn replace_images( &mut self, images: Vec, @@ -206,6 +238,7 @@ impl Composer { draft: self.draft.clone(), attachments: self.default_attachments().to_vec(), images: self.images.clone(), + files: self.files.clone(), queue: self.queue.clone(), queue_paused: true, next_queue_id: self.next_queue_id, @@ -241,6 +274,7 @@ impl Composer { self.next_queue_id += 1; } self.images.clear(); + self.files.clear(); } /// Queued prompts wait to be sent and nothing holds them back. @@ -282,12 +316,18 @@ impl Composer { self.defaults.is_some() } - /// Shows a queued prompt's images and context for editing, keeping the + /// Shows a queued prompt's images, files and context for editing, keeping the /// composer's own to return to. - pub fn begin_edit(&mut self, images: Vec, attachments: Vec) { + pub fn begin_edit( + &mut self, + images: Vec, + files: Vec, + attachments: Vec, + ) { debug_assert!(self.defaults.is_none()); self.defaults = Some(Defaults { images: std::mem::replace(&mut self.images, images), + files: std::mem::replace(&mut self.files, files), attachments: std::mem::replace(&mut self.attachments, attachments), }); self.queue_editing = true; @@ -297,6 +337,7 @@ impl Composer { pub fn end_edit(&mut self) { if let Some(defaults) = self.defaults.take() { self.images = defaults.images; + self.files = defaults.files; self.attachments = defaults.attachments; } self.queue_editing = false; @@ -356,7 +397,11 @@ mod tests { vec![prompt(1), prompt(2)], ); composer.resume(); - composer.begin_edit(Vec::new(), vec![Attachment::Group("queued".into())]); + composer.begin_edit( + Vec::new(), + Vec::new(), + vec![Attachment::Group("queued".into())], + ); assert!(composer.editing()); assert_eq!( composer.default_attachments(), @@ -414,6 +459,7 @@ mod tests { composer.replace_context(vec![Attachment::Group("own".into())]); composer.begin_edit( vec![image.clone()], + Vec::new(), vec![Attachment::Group("queued".into())], ); assert!( @@ -441,4 +487,20 @@ mod tests { .collect::>(); assert_eq!(ids, [1, 3]); } + + #[test] + fn files_go_with_the_prompt_after_its_images_and_leave_the_composer() { + let mut composer = Composer::default(); + let file = PromptFile::link(std::path::Path::new("/srv/report.pdf"), 7).unwrap(); + composer + .add_files(vec![file.clone(), file.clone()]) + .unwrap(); + assert_eq!(composer.files().len(), 1, "a file is attached once"); + let candidate = composer + .prepare_submission("read it".into(), None, Vec::new()) + .unwrap(); + assert_eq!(candidate.queue()[0].saved().images, vec![file.content()]); + composer.commit_prompt_change(candidate); + assert!(composer.files().is_empty()); + } } diff --git a/crates/agent-chat/src/thread/config.rs b/crates/agent-chat/src/thread/config.rs index c954f189..3d9ac85d 100644 --- a/crates/agent-chat/src/thread/config.rs +++ b/crates/agent-chat/src/thread/config.rs @@ -95,10 +95,13 @@ impl ChatConfig { pub(super) fn mode_choice(&self) -> Option<&ModeId> { self.mode_choice.as_ref() } - pub fn model(&self) -> Option { + pub(super) fn model_ref(&self) -> Option<&String> { self.last_prompt_model - .clone() - .or_else(|| self.last_saved_model.clone()) + .as_ref() + .or(self.last_saved_model.as_ref()) + } + pub fn model(&self) -> Option { + self.model_ref().cloned() } pub fn config_overrides_modes(&self) -> bool { self.options diff --git a/crates/agent-chat/src/thread/document.rs b/crates/agent-chat/src/thread/document.rs index dfd8048c..b0e02985 100644 --- a/crates/agent-chat/src/thread/document.rs +++ b/crates/agent-chat/src/thread/document.rs @@ -4,6 +4,7 @@ use nocterm_ai::model::content::Content; use nocterm_ai::model::session::SessionUpdate; use nocterm_ai::model::usage::TokenTotals; use nocterm_ai::{ + execution_origin::ChatKey, history::{HistoryCodecError, SavedChat, SharedChat}, thread::{Entry, ThreadChange, ThreadState}, }; @@ -35,14 +36,21 @@ pub(super) struct DocumentRestart { persistence: PersistenceState, } impl DocumentRestart { + pub(super) fn key(&self) -> &ChatKey { + self.identity.key() + } pub(super) fn agent_id(&self) -> &str { self.identity.agent() } } impl ChatDocument { - pub(super) fn new(agent: String, id: String, updated: u64) -> Self { + pub(super) fn new(agent: String, key: ChatKey, updated: u64) -> Self { Self { - identity: Identity::new(agent, id), + identity: Identity::new( + agent, + key, + nocterm_ai::history::TranscriptVersion::ENVIRONMENT.wire(), + ), metadata: Metadata::new(updated), transcript: Transcript::default(), persistence: PersistenceState::default(), @@ -54,7 +62,7 @@ impl ChatDocument { pub(super) fn restarted(data: DocumentRestart) -> Self { let mut document = Self::new( data.identity.agent().into(), - data.identity.id().into(), + data.identity.key().clone(), data.metadata.updated(), ); document.hydrate_restart(data); @@ -66,6 +74,9 @@ impl ChatDocument { pub fn agent_id(&self) -> &str { self.identity.agent() } + pub fn key(&self) -> &ChatKey { + self.identity.key() + } pub fn chat_id(&self) -> &str { self.identity.id() } @@ -110,13 +121,22 @@ impl ChatDocument { self.metadata.timestamp(now); } pub(super) fn imported_clean(&mut self, chat: &mut SavedChat, pending: bool) { - self.identity = Identity::new(chat.agent_id.clone(), chat.id.clone()); - self.metadata = Metadata::restored(chat.name.take(), chat.pinned, chat.updated, pending); + self.identity = Identity::new( + chat.header.agent_id.clone(), + ChatKey::from_header(&chat.header), + chat.version, + ); + self.metadata = Metadata::restored( + chat.header.name.take(), + chat.header.pinned, + chat.header.updated, + pending, + ); self.budget = BudgetState::restored(chat.prepared_budget()); self.transcript = Transcript::restored( std::mem::take(&mut chat.entries), std::mem::take(&mut chat.times), - chat.title.take(), + chat.header.title.take(), ); self.persistence.imported_clean(); self.persistence.begin_hydration(); @@ -136,7 +156,11 @@ impl ChatDocument { self.metadata.timestamp(updated); } pub(super) fn archived_clean(&mut self, summary: &nocterm_ai::history::ChatSummary) { - self.identity = Identity::new(summary.agent_id.clone(), summary.id.clone()); + self.identity = Identity::new( + summary.agent_id.clone(), + ChatKey::from_summary(summary), + summary.transcript_version().wire(), + ); self.metadata = Metadata::restored(summary.name.clone(), summary.pinned, summary.updated, false); self.persistence.imported_clean(); @@ -186,18 +210,58 @@ impl ChatDocument { pub(super) fn edited_archived_draft(&mut self, now: u64) { self.changed(now, true); } - pub(super) fn apply_transcript(&mut self, update: SessionUpdate, now: u64) -> ThreadChange { - let (change, semantic) = self.transcript.apply(update, now); + pub(super) fn apply_transcript( + &mut self, + update: SessionUpdate, + now: u64, + ) -> Result { + let (change, semantic) = self.transcript.apply(update, now)?; if semantic { self.changed(now, false); } - change + Ok(change) + } + /// Adds a prompt another device sent as user rows, marked for redraw. + pub(super) fn append_prompt( + &mut self, + prompt: &[Content], + now: u64, + ) -> Result<(), HistoryCodecError> { + let first = self.entries().len(); + for part in prompt.iter().cloned() { + self.apply_transcript(SessionUpdate::UserMessageChunk(part), now)?; + } + for row in first..self.entries().len() { + self.mark_row(row); + } + Ok(()) + } + /// Shows the agent's own record of the conversation in place of the saved transcript. + pub(super) fn replace_history( + &mut self, + updates: Vec, + now: u64, + ) -> Result<(), HistoryCodecError> { + let next = self.transcript.replaced(updates, now)?; + let rows = self.entries().len().max(next.state().entries.len()); + self.transcript = next; + for row in 0..rows { + self.mark_row(row); + } + self.changed(now, false); + Ok(()) } - pub(super) fn append_user(&mut self, content: Vec, now: u64, input: &Inputs<'_>) { - self.transcript.append_user(content, now); + pub(super) fn append_user( + &mut self, + content: Vec, + now: u64, + input: &Inputs<'_>, + ) -> Result<(), HistoryCodecError> { + self.transcript.append_user(content, now)?; self.persistence.remember_inputs(input); self.mark_row(self.entries().len() - 1); self.changed(now, false); + Ok(()) } pub(super) fn tokens(&mut self, usage: Option) { self.transcript.tokens(usage); @@ -230,6 +294,9 @@ impl ChatDocument { pub fn take_changed_rows(&mut self) -> HashSet { std::mem::take(&mut self.changed_rows) } + pub fn has_changed_rows(&self) -> bool { + !self.changed_rows.is_empty() + } pub(super) fn mark_row(&mut self, row: usize) { self.changed_rows.insert(row); self.budget.mark(row); @@ -238,7 +305,9 @@ impl ChatDocument { self.budget.refresh(&self.transcript.state().entries) } pub fn resident_bytes(&self) -> usize { - self.budget.resident_bytes() + self.transcript + .resident_bytes() + .max(self.budget.resident_bytes()) } pub(super) fn validate_candidate( &mut self, @@ -250,19 +319,72 @@ impl ChatDocument { self.budget.validate(metadata, encoded, memory) } pub(super) fn metadata(&self, input: &Inputs<'_>) -> SavedChat { - let mut chat = SavedChat::for_identity( + let mut chat = SavedChat::for_environment( self.agent_id().into(), self.chat_id().into(), self.updated(), + self.key().chat_scope(), ); - chat.name = self.name().map(str::to_owned); - chat.pinned = self.pinned(); - chat.title = self.transcript().title.clone(); + chat.version = self.identity.wire_version(); + chat.header.name = self.name().map(str::to_owned); + chat.header.pinned = self.pinned(); + chat.header.title = self.transcript().title.clone(); chat.pending_history = self.pending_history(); chat.times = SavedChat::bounded_times(self.entries().len(), &self.transcript().times); input.metadata(&mut chat); chat } + pub(super) fn fork_capacity( + &self, + input: &Inputs<'_>, + len: usize, + ) -> Result { + let end = len.min(self.entries().len()); + if end > nocterm_ai::history::MAX_SAVED_ENTRIES { + return Err(HistoryCodecError::CollectionLimit); + } + Ok(self.snapshot_capacity(input)?.saturating_add( + nocterm_ai::history::retained_value_bytes(&&self.entries()[..end])?, + )) + } + pub(super) fn restart_capacity(&self, input: &Inputs<'_>) -> Result { + let state = self.transcript(); + let plan = state.plan.as_ref().map_or(0, |plan| { + plan.entries.iter().fold(0usize, |total, entry| { + total + .saturating_add(512) + .saturating_add(entry.content.len().saturating_mul(2)) + }) + }); + let meta = state + .usage + .as_ref() + .and_then(|usage| usage.meta.as_ref()) + .map(nocterm_ai::history::retained_value_bytes) + .transpose()? + .unwrap_or(0); + Ok(self + .snapshot_capacity(input)? + .saturating_add(nocterm_ai::history::retained_value_bytes(&state.entries)?) + .saturating_add(plan) + .saturating_add(meta)) + } + pub(super) fn snapshot_capacity(&self, input: &Inputs<'_>) -> Result { + use nocterm_ai::history::retained_value_bytes; + // The transcript-free snapshot uses the same counter as the retained + // snapshot, so header, scope, environment, inputs, queue and times are + // exact. Rows are counted in place, before any transcript is cloned; the + // fixed 64KiB covers the catalog preview drawn from them. + let start = SavedChat::saved_entry_start(self.entries().len()); + let frame = SharedChat { + metadata: self.metadata(input), + queue: input.queue(), + } + .retained_bytes()?; + Ok((64usize * 1024) + .saturating_add(frame) + .saturating_add(retained_value_bytes(&&self.entries()[start..])?)) + } pub(super) fn snapshot(&self, input: &Inputs<'_>) -> Option> { if !self.pinned() && self.name().is_none() @@ -287,6 +409,7 @@ impl ChatDocument { pub(super) fn existing_offer(&self) -> Option<(Arc, u64)> { self.persistence.capture_pending() } + #[cfg(test)] pub(super) fn propose_candidate( &mut self, input: &Inputs<'_>, @@ -297,14 +420,24 @@ impl ChatDocument { if self.persistence.has_current_candidate() { return self.persistence.capture_pending(); } - let snapshot = self.snapshot(input).or_else(|| { + let snapshot = self.candidate_snapshot(input)?; + self.adopt_candidate(input, snapshot) + } + pub(super) fn candidate_snapshot(&self, input: &Inputs<'_>) -> Option> { + self.snapshot(input).or_else(|| { self.history_present().then(|| { Arc::new(SharedChat { metadata: self.metadata(input), queue: Vec::new(), }) }) - })?; + }) + } + pub(super) fn adopt_candidate( + &mut self, + input: &Inputs<'_>, + snapshot: Arc, + ) -> Option<(Arc, u64)> { self.persistence.remember_inputs(input); self.persistence.prepare(snapshot); self.persistence.capture_pending() diff --git a/crates/agent-chat/src/thread/document/metadata.rs b/crates/agent-chat/src/thread/document/metadata.rs index 56f42a96..13536dde 100644 --- a/crates/agent-chat/src/thread/document/metadata.rs +++ b/crates/agent-chat/src/thread/document/metadata.rs @@ -9,17 +9,32 @@ use std::{path::PathBuf, sync::Arc}; #[derive(Clone)] pub(super) struct Identity { agent: String, - id: String, + key: nocterm_ai::execution_origin::ChatKey, + wire_version: u32, } impl Identity { - pub(super) fn new(agent: String, id: String) -> Self { - Self { agent, id } + pub(super) fn new( + agent: String, + key: nocterm_ai::execution_origin::ChatKey, + wire_version: u32, + ) -> Self { + Self { + agent, + key, + wire_version, + } + } + pub(super) fn wire_version(&self) -> u32 { + self.wire_version } pub(super) fn agent(&self) -> &str { &self.agent } + pub(super) fn key(&self) -> &nocterm_ai::execution_origin::ChatKey { + &self.key + } pub(super) fn id(&self) -> &str { - &self.id + self.key.id() } } #[derive(Clone)] diff --git a/crates/agent-chat/src/thread/document/tests.rs b/crates/agent-chat/src/thread/document/tests.rs index 7a6020e1..11ff966b 100644 --- a/crates/agent-chat/src/thread/document/tests.rs +++ b/crates/agent-chat/src/thread/document/tests.rs @@ -12,8 +12,15 @@ const ID: &str = "00000000-0000-4000-8000-000000000001"; fn imported(entries: Vec) -> ChatDocument { let mut saved = SavedChat::for_identity("agent".into(), ID.into(), 40); saved.entries = entries; - saved.title = Some("Recorded title".into()); - let mut document = ChatDocument::new("agent".into(), ID.into(), 40); + saved.header.title = Some("Recorded title".into()); + let mut document = ChatDocument::new( + "agent".into(), + nocterm_ai::execution_origin::ChatKey::new( + nocterm_ai::history::ContextId::General, + ID.into(), + ), + 40, + ); document.imported_clean(&mut saved, false); document } @@ -84,20 +91,30 @@ fn empty_matching_chunks_identical_tool_patches_and_view_updates_are_clean() { .status(ToolStatus::Pending) .raw_output(json!({"out":"same"})); let mut document = imported(vec![Entry::Tool(call), Entry::Agent("answer".into())]); - document.apply_transcript( - update(json!({"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":""}})), - 80, - ); - document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"call","status":"pending","title":"Tool","rawOutput":{"out":"same"}})),80); - document.apply_transcript(update(json!({"sessionUpdate":"plan","entries":[]})), 80); - document.apply_transcript( - update(json!({"sessionUpdate":"usage_update","used":1,"size":10})), - 80, - ); - document.apply_transcript( - update(json!({"sessionUpdate":"session_info_update","title":"Recorded title"})), - 80, - ); + document + .apply_transcript( + update( + json!({"sessionUpdate":"agent_message_chunk","content":{"type":"text","text":""}}), + ), + 80, + ) + .unwrap(); + document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"call","status":"pending","title":"Tool","rawOutput":{"out":"same"}})),80).unwrap(); + document + .apply_transcript(update(json!({"sessionUpdate":"plan","entries":[]})), 80) + .unwrap(); + document + .apply_transcript( + update(json!({"sessionUpdate":"usage_update","used":1,"size":10})), + 80, + ) + .unwrap(); + document + .apply_transcript( + update(json!({"sessionUpdate":"session_info_update","title":"Recorded title"})), + 80, + ) + .unwrap(); document.tokens(None); assert_eq!(document.desired_revision(), 0); assert_eq!(document.updated(), 40); @@ -108,12 +125,16 @@ fn a_new_empty_row_is_durable_but_a_patch_outside_the_saved_window_is_not() { let mut entries = vec![Entry::Tool(ToolRecord::new("old", "Old"))]; entries.extend((0..2000).map(|_| Entry::Agent("saved".into()))); let mut document = imported(entries); - document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"old","title":"Changed old row"})),80); + document.apply_transcript(update(json!({"sessionUpdate":"tool_call_update","toolCallId":"old","title":"Changed old row"})),80).unwrap(); assert_eq!(document.desired_revision(), 0); - document.apply_transcript( - update(json!({"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":""}})), - 81, - ); + document + .apply_transcript( + update( + json!({"sessionUpdate":"agent_thought_chunk","content":{"type":"text","text":""}}), + ), + 81, + ) + .unwrap(); assert_eq!(document.desired_revision(), 1); assert_eq!(document.updated(), 81); } @@ -122,10 +143,14 @@ fn a_new_empty_row_is_durable_but_a_patch_outside_the_saved_window_is_not() { fn genuine_saved_row_transitions_remain_dirty_even_when_the_text_returns_to_its_old_value() { let mut document = imported(vec![Entry::Tool(ToolRecord::new("call", "A"))]); for (title, time) in [("B", 81), ("A", 82)] { - document.apply_transcript( - update(json!({"sessionUpdate":"tool_call_update","toolCallId":"call","title":title})), - time, - ); + document + .apply_transcript( + update( + json!({"sessionUpdate":"tool_call_update","toolCallId":"call","title":title}), + ), + time, + ) + .unwrap(); } assert_eq!(document.desired_revision(), 2); assert_eq!(document.updated(), 82); @@ -194,7 +219,14 @@ fn clearing_a_saved_draft_yields_an_empty_candidate_and_a_never_saved_empty_chat .unwrap(); assert!(snapshot.draft.is_none()); assert!(snapshot.entries.is_empty()); - let mut empty = ChatDocument::new("agent".into(), ID.into(), 40); + let mut empty = ChatDocument::new( + "agent".into(), + nocterm_ai::execution_origin::ChatKey::new( + nocterm_ai::history::ContextId::General, + ID.into(), + ), + 40, + ); empty.establish_clean_inputs(&input(&composer, &config)); assert!( empty @@ -300,8 +332,8 @@ fn disabled_release_retains_unadmitted_or_failed_input_and_an_old_ack_cannot_rel 1, "the driver retains the full accepted transcript" ); - assert_eq!(first.name.as_deref(), Some("first change")); - assert_eq!(newest.name.as_deref(), Some("newer desire")); + assert_eq!(first.header.name.as_deref(), Some("first change")); + assert_eq!(newest.header.name.as_deref(), Some("newer desire")); assert!( document.existing_offer().is_none(), "the writer already owns this immutable intent" @@ -314,3 +346,56 @@ fn disabled_release_retains_unadmitted_or_failed_input_and_an_old_ack_cannot_rel "failure retains the original input after releasing presentation" ); } + +#[test] +fn snapshot_capacity_covers_a_long_transcript_with_entry_times() { + let composer = Composer::default(); + let config = ChatConfig::default(); + let inputs = input(&composer, &config); + let mut saved = SavedChat::for_identity("agent".into(), ID.into(), 40); + saved.entries = (0..400).map(|_| Entry::Agent("a".into())).collect(); + saved.times = (0..400).collect(); + let mut document = ChatDocument::new( + "agent".into(), + nocterm_ai::execution_origin::ChatKey::new( + nocterm_ai::history::ContextId::General, + ID.into(), + ), + 40, + ); + document.imported_clean(&mut saved, false); + let snapshot = document.snapshot(&inputs).expect("non-empty chat"); + assert!(document.snapshot_capacity(&inputs).unwrap() >= snapshot.retained_bytes().unwrap()); +} + +#[test] +fn snapshot_capacity_covers_a_hub_account_header_and_draft() { + use nocterm_ai::local_environment::{ChatScope, LocalEnvironment}; + use nocterm_hubs_model::source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, NamespaceId, + }; + let fingerprint = Fingerprint::from_bytes([7; 32]); + let partition = LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new(NamespaceId::new("hub").unwrap(), fingerprint), + AccountIdentity::new(NamespaceId::new("account").unwrap(), fingerprint), + )); + let scope = ChatScope::new( + LocalEnvironment::HubAccount(partition), + nocterm_ai::history::ContextId::General, + ); + let mut saved = SavedChat::for_environment("agent".into(), ID.into(), 40, scope.clone()); + saved.entries = vec![Entry::Agent("a".into())]; + saved.times = vec![1]; + let mut document = ChatDocument::new( + "agent".into(), + nocterm_ai::execution_origin::ChatKey::in_environment(scope, ID.into()), + 40, + ); + document.imported_clean(&mut saved, false); + let mut composer = Composer::default(); + composer.set_draft("draft".into()); + let config = ChatConfig::default(); + let inputs = input(&composer, &config); + let snapshot = document.snapshot(&inputs).expect("non-empty chat"); + assert!(document.snapshot_capacity(&inputs).unwrap() >= snapshot.retained_bytes().unwrap()); +} diff --git a/crates/agent-chat/src/thread/document/transcript.rs b/crates/agent-chat/src/thread/document/transcript.rs index 9a84d72d..386367d0 100644 --- a/crates/agent-chat/src/thread/document/transcript.rs +++ b/crates/agent-chat/src/thread/document/transcript.rs @@ -6,9 +6,15 @@ use nocterm_ai::model::tools::{ToolChanges, ToolRecord, ToolStatus}; use nocterm_ai::model::usage::TokenTotals; use nocterm_ai::thread::{Entry, ThreadChange, ThreadState}; -#[derive(Clone, Default)] +#[derive(Clone)] pub(super) struct Transcript { state: ThreadState, + live_bytes: usize, +} +impl Default for Transcript { + fn default() -> Self { + Self::restored(Vec::new(), Vec::new(), None) + } } pub(crate) struct ToolMetadataPatch { row: usize, @@ -24,21 +30,28 @@ impl ToolMetadataPatch { } impl Transcript { pub(super) fn restored(entries: Vec, times: Vec, title: Option) -> Self { - Self { - state: ThreadState { - entries, - times, - title, - ..Default::default() - }, - } + let state = ThreadState { + entries, + times, + title, + ..Default::default() + }; + let live_bytes = nocterm_ai::thread::live::resident_bytes(&state).unwrap_or(usize::MAX); + Self { state, live_bytes } } pub(super) fn state(&self) -> &ThreadState { &self.state } - pub(super) fn apply(&mut self, update: SessionUpdate, now: u64) -> (ThreadChange, bool) { + pub(super) fn apply( + &mut self, + update: SessionUpdate, + now: u64, + ) -> Result<(ThreadChange, bool), nocterm_ai::history::HistoryCodecError> { + let next = nocterm_ai::thread::live::admit_update(&self.state, self.live_bytes, &update)?; let semantic = self.changes_saved(&update); - (self.state.apply(update, now), semantic) + let change = self.state.apply(update, now); + self.live_bytes = next; + Ok((change, semantic)) } fn changes_saved(&self, update: &SessionUpdate) -> bool { match update { @@ -69,8 +82,30 @@ impl Transcript { _ => false, } } - pub(super) fn append_user(&mut self, content: Vec, now: u64) { + /// A transcript of the same chat made of `updates` alone, oldest first. + pub(super) fn replaced( + &self, + updates: Vec, + now: u64, + ) -> Result { + let mut next = Self::restored(Vec::new(), Vec::new(), self.state.title.clone()); + for update in updates { + next.apply(update, now)?; + } + Ok(next) + } + pub(super) fn append_user( + &mut self, + content: Vec, + now: u64, + ) -> Result<(), nocterm_ai::history::HistoryCodecError> { + self.live_bytes = + nocterm_ai::thread::live::admit_user(&self.state, self.live_bytes, &content)?; self.state.push_user(content, now); + Ok(()) + } + pub(super) fn resident_bytes(&self) -> usize { + self.live_bytes } pub(super) fn tokens(&mut self, usage: Option) { self.state.tokens = usage; @@ -83,6 +118,23 @@ impl Transcript { if call.meta.as_ref().and_then(|meta| meta.get(key)) == Some(&patch.value) { return false; } + let new_key = call.meta.as_ref().and_then(|meta| meta.get(key)).is_none(); + let removed = call + .meta + .as_ref() + .and_then(|meta| meta.get(key)) + .map_or(Ok(0), nocterm_ai::history::retained_value_bytes) + .unwrap_or(0); + let added = nocterm_ai::history::retained_value_bytes(&patch.value).unwrap_or(usize::MAX); + let next = self + .live_bytes + .saturating_sub(removed) + .saturating_add(added) + .saturating_add(if new_key { 2048 } else { 0 }); + if next > nocterm_ai::history::MAX_RESIDENT_BYTES { + return false; + } + self.live_bytes = next; call.meta .get_or_insert_with(Default::default) .insert(key.into(), patch.value); diff --git a/crates/agent-chat/src/thread/execution.rs b/crates/agent-chat/src/thread/execution.rs index 9e9b61f1..893b6d0a 100644 --- a/crates/agent-chat/src/thread/execution.rs +++ b/crates/agent-chat/src/thread/execution.rs @@ -2,28 +2,40 @@ use super::AgentThread; use nocterm_ai::{BridgeCall, ToolFailure}; use std::time::{Duration, Instant}; -pub(in crate::thread) mod jobs; +/// What a wait reports on; both name only work this chat started. +pub(super) enum WaitSubject { + Command { terminal: String, id: String }, + Transfer { id: String }, +} +impl WaitSubject { + pub(super) fn command(terminal: &str, id: &str) -> Self { + Self::Command { + terminal: terminal.into(), + id: id.into(), + } + } + pub(super) fn transfer(id: &str) -> Self { + Self::Transfer { id: id.into() } + } +} /// Read waits retain the original ticket and never take a replacement executor. -pub(super) struct CommandWaitOperation { - terminal: String, - id: String, +pub(super) struct ToolWaitOperation { + subject: WaitSubject, ticket: nocterm_ai::session::Ticket, yield_time: Duration, until: Instant, _guard: super::session_driver::OperationGuard, } -impl CommandWaitOperation { +impl ToolWaitOperation { pub(super) fn new( - terminal: String, - id: String, + subject: WaitSubject, yield_ms: u64, ticket: nocterm_ai::session::Ticket, guard: super::session_driver::OperationGuard, ) -> Self { Self { - terminal, - id, + subject, ticket, yield_time: Duration::from_millis(yield_ms), until: Instant::now(), @@ -33,23 +45,25 @@ impl CommandWaitOperation { pub(super) fn ticket(&self) -> nocterm_ai::session::Ticket { self.ticket } - pub(super) fn id(&self) -> &str { - &self.id - } - pub(super) fn terminal(&self) -> &str { - &self.terminal - } + /// The current report, or `None` while the work runs and the wait has time left. pub(super) fn frame( &self, - state: super::tool_runtime::CommandSnapshot, + tools: &mut super::tool_runtime::ToolRuntime, redact: bool, ) -> Result, ToolFailure> { - if state.active() && Instant::now() < self.until { - return Ok(None); + use super::tool_runtime::presentation::{command_payload, transfer_payload}; + let waiting = Instant::now() < self.until; + match &self.subject { + WaitSubject::Command { terminal, id } => { + let state = tools.command_snapshot(id, terminal)?; + Ok((!(state.active() && waiting)).then(|| command_payload(id, state, redact))) + } + WaitSubject::Transfer { id } => { + let progress = tools.transfer_progress(id)?; + let running = !progress.state.finished(); + Ok((!(running && waiting)).then(|| transfer_payload(&progress, redact))) + } } - Ok(Some(super::tool_runtime::presentation::command_payload( - &self.id, state, redact, - ))) } pub(super) async fn run( mut self, @@ -59,7 +73,7 @@ impl CommandWaitOperation { ) { self.until = Instant::now() + self.yield_time; loop { - match thread.update(cx, |thread, cx| thread.poll_command_wait(&self, cx)) { + match thread.update(cx, |thread, cx| thread.poll_tool_wait(&self, cx)) { Ok(Ok(None)) => { cx.background_executor() .timer(Duration::from_millis(25)) diff --git a/crates/agent-chat/src/thread/execution/jobs/tests.rs b/crates/agent-chat/src/thread/execution/jobs/tests.rs deleted file mode 100644 index f29600f5..00000000 --- a/crates/agent-chat/src/thread/execution/jobs/tests.rs +++ /dev/null @@ -1,217 +0,0 @@ -use super::*; -use nocterm_session::{ExecError, ExecFuture, ExecOutput}; -use std::sync::atomic::{AtomicUsize, Ordering}; -struct Executor { - calls: AtomicUsize, - output: Mutex>, -} -impl HostExec for Executor { - fn exec(&self, _: ExecRequest) -> ExecFuture { - self.calls.fetch_add(1, Ordering::SeqCst); - let output = self.output.lock().unwrap().take(); - async move { output.ok_or(ExecError::Unsupported) }.boxed() - } -} -fn executor(output: Option) -> Arc { - Arc::new(Executor { - calls: AtomicUsize::new(0), - output: Mutex::new(output), - }) -} -#[test] -fn rejects_a_fifth_active_command_and_foreign_command_ids() { - let mut registry = Jobs::default(); - let mut first = String::new(); - for index in 0..MAX_ACTIVE { - let (id, _, _) = registry.insert("t1".into(), executor(None)).unwrap(); - if index == 0 { - first = id; - } - } - assert_eq!( - registry.insert("t1".into(), executor(None)).err(), - Some(JobError::Busy) - ); - for (id, terminal) in [("c99", "t1"), (first.as_str(), "t2"), ("x", "t1")] { - assert_eq!(registry.get(id, terminal).err(), Some(JobError::Unknown)); - } - assert_eq!( - nocterm_ai::ToolFailure::from(JobError::Busy).message(), - "This chat already has four active commands. Read or cancel one first." - ); -} -#[test] -fn cancelled_or_expired_before_first_poll_never_starts_a_program() { - futures::executor::block_on(async { - for cancelled in [true, false] { - let executor = executor(None); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - if cancelled { - registry.get(&id, "t1").unwrap().cancel(State::Cancelled); - } - run( - executor.clone(), - ExecRequest::new("do-not-run"), - state, - cancel, - futures::future::ready(()), - ) - .await; - assert_eq!(executor.calls.load(Ordering::SeqCst), 0); - assert_eq!( - registry.get(&id, "t1").unwrap().snapshot().state, - if cancelled { - State::Cancelled - } else { - State::TimedOut - } - ); - } - }); -} -#[test] -fn output_is_drained_past_retention_and_preserves_actual_exit() { - futures::executor::block_on(async { - let (sink, output) = ExecOutput::channel(); - let executor = executor(Some(output)); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - let produce = async move { - for _ in 0..40 { - assert!(sink.send(vec![b'x'; 4096]).await); - } - sink.finish(ExecExit { - status: Some(7), - stderr: "failure".into(), - }); - }; - futures::join!( - run( - executor, - ExecRequest::new("test"), - state, - cancel, - futures::future::pending() - ), - produce - ); - let state = registry.get(&id, "t1").unwrap().snapshot(); - assert_eq!(state.state, State::Exited); - assert_eq!(state.stdout.len(), MAX_STDOUT); - assert_eq!(state.total_bytes, 40 * 4096); - assert_eq!(state.exit.unwrap().status, Some(7)); - }); -} -#[test] -fn registry_limits_active_work_retains_finished_jobs_and_checks_ownership() { - let executor = executor(None); - let mut registry = Jobs::default(); - let first = registry.insert("t1".into(), executor.clone()).unwrap().0; - for _ in 1..MAX_ACTIVE { - registry.insert("t1".into(), executor.clone()).unwrap(); - } - assert!(registry.insert("t1".into(), executor.clone()).is_err()); - assert!(registry.get(&first, "t2").is_err()); - assert!(Jobs::default().get(&first, "t1").is_err()); - registry.cancel_all(); - for _ in 0..MAX_RETAINED { - let (id, _, _) = registry.insert("t1".into(), executor.clone()).unwrap(); - registry.get(&id, "t1").unwrap().cancel(State::Cancelled); - } - assert_eq!(registry.jobs.len(), MAX_RETAINED); - assert!(registry.get(&first, "t1").is_err()); -} -#[test] -fn silence_finishes_and_cancellation_drops_transport_output() { - futures::executor::block_on(async { - let (sink, output) = ExecOutput::channel(); - sink.finish(ExecExit { - status: Some(0), - stderr: String::new(), - }); - drop(sink); - let executor = executor(Some(output)); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - run( - executor, - ExecRequest::new("true"), - state, - cancel, - futures::future::pending(), - ) - .await; - assert_eq!( - registry - .get(&id, "t1") - .unwrap() - .snapshot() - .exit - .unwrap() - .status, - Some(0) - ); - }); -} - -#[test] -fn cancelling_running_program_drops_output_and_keeps_exit_unknown() { - futures::executor::block_on(async { - let (sink, output) = ExecOutput::channel(); - let executor = executor(Some(output)); - let mut registry = Jobs::default(); - let (id, state, cancel) = registry.insert("t1".into(), executor.clone()).unwrap(); - let stop = async { - assert!(sink.send(b"running".to_vec()).await); - registry.get(&id, "t1").unwrap().cancel(State::Cancelled); - sink.closed().await; - }; - futures::join!( - run( - executor.clone(), - ExecRequest::new("test"), - state, - cancel, - futures::future::pending() - ), - stop - ); - assert_eq!(executor.calls.load(Ordering::SeqCst), 1); - let state = registry.get(&id, "t1").unwrap().snapshot(); - assert_eq!(state.state, State::Cancelled); - assert!(state.exit.is_none()); - }); -} - -#[test] -fn cancelling_an_owned_start_before_poll_never_requests_native_execution() { - futures::executor::block_on(async { - let executor = Arc::new(nocterm_session::testing::FakeExec::default()); - let mut registry = Jobs::default(); - let start = registry - .start( - "original terminal".into(), - executor.clone(), - ExecRequest::new("do-not-run"), - futures::future::pending(), - ) - .unwrap(); - let (id, owned_run) = start.into_run(); - assert!(executor.requests().is_empty()); - registry - .get(&id, "original terminal") - .unwrap() - .cancel(State::Cancelled); - owned_run.await; - assert!(executor.requests().is_empty()); - assert_eq!( - registry - .get(&id, "original terminal") - .unwrap() - .snapshot() - .state(), - State::Cancelled - ); - }); -} diff --git a/crates/agent-chat/src/thread/fork.rs b/crates/agent-chat/src/thread/fork.rs new file mode 100644 index 00000000..ffaca115 --- /dev/null +++ b/crates/agent-chat/src/thread/fork.rs @@ -0,0 +1,102 @@ +//! A prepared fork owns its copied durable body until the destination admits it. +use super::{Attachment, Composer, Restore, config::ChatConfig, document::ChatDocument}; +/// What a forked chat starts from. +pub struct Fork { + chat: nocterm_ai::history::SavedChat, + restore: Option, + attachments: Vec, + _allocation: nocterm_core::allocation::AllocationLease, +} + +impl Fork { + pub(crate) fn prepared(mut self) -> Result { + self.chat = nocterm_ai::history::prepare_owned(self.chat)?; + Ok(self) + } + pub(crate) fn key(&self) -> nocterm_ai::execution_origin::ChatKey { + nocterm_ai::execution_origin::ChatKey::from_header(&self.chat.header) + } +} + +impl Fork { + pub(super) fn restoration( + session: &Option, + workdir: &Option, + restore: Option<&Restore>, + ) -> Option { + match (session, restore) { + (Some(session), _) => workdir.clone().map(|workdir| Restore { + session: session.clone(), + workdir, + fork: true, + }), + (None, Some(restore)) => Some(Restore { + fork: true, + ..restore.clone() + }), + (None, None) => None, + } + } + pub(super) fn capture( + mut chat: nocterm_ai::history::SavedChat, + source: ForkSource<'_>, + restore: Option, + len: usize, + allocation: nocterm_core::allocation::AllocationLease, + ) -> Self { + let whole = len >= source.document.entries().len(); + let end = len.min(source.document.entries().len()); + chat.entries = source.document.entries()[..end].to_vec(); + chat.times = source + .document + .transcript() + .times + .iter() + .take(end) + .copied() + .collect(); + chat.header.title = source.document.transcript().title.clone(); + chat.header.name = Some(format!("{} (fork)", source.title)); + chat.model = source.config.model(); + chat.config = source.config.choices().clone(); + chat.mode = source.config.mode_choice().map(ToString::to_string); + chat.pending_history = source.document.pending_history() || !whole; + Self { + chat, + restore: restore.filter(|_| whole), + attachments: source.composer.default_attachments().to_vec(), + _allocation: allocation, + } + } + pub(super) fn into_parts( + self, + ) -> ( + nocterm_ai::history::SavedChat, + Option, + Vec, + ) { + (self.chat, self.restore, self.attachments) + } +} + +pub(super) struct ForkSource<'a> { + document: &'a ChatDocument, + composer: &'a Composer, + config: &'a ChatConfig, + title: String, +} +impl<'a> ForkSource<'a> { + pub(super) fn new( + document: &'a ChatDocument, + composer: &'a Composer, + config: &'a ChatConfig, + title: String, + ) -> Self { + Self { + document, + composer, + config, + title, + } + } +} diff --git a/crates/agent-chat/src/thread/live.rs b/crates/agent-chat/src/thread/live.rs index ec8a916a..47934b12 100644 --- a/crates/agent-chat/src/thread/live.rs +++ b/crates/agent-chat/src/thread/live.rs @@ -158,11 +158,12 @@ impl LiveCommandOperation { }, cx, )?; - let (mut payload, bytes) = super::tool_runtime::presentation::text_payload(tail, redact); - payload["completion"] = serde_json::json!(reason); - payload["exit_status"] = serde_json::Value::Null; - payload["completed"] = serde_json::json!(reason == "prompt_returned"); - Ok(Some((payload, bytes))) + let report = super::tool_runtime::presentation::terminal_report(tail, redact); + let bytes = report.text.len(); + Ok(Some(( + serde_json::json!(report.observed(reason.into())), + bytes, + ))) } pub(super) async fn run( mut self, diff --git a/crates/agent-chat/src/thread/local_payloads.rs b/crates/agent-chat/src/thread/local_payloads.rs new file mode 100644 index 00000000..4648f0b2 --- /dev/null +++ b/crates/agent-chat/src/thread/local_payloads.rs @@ -0,0 +1,84 @@ +//! One Local resident body and separately retained immutable snapshot generations. +use gpui::{App, EntityId}; +use nocterm_agent_runtime::history_admission::{ + HistoryPayloadPermit, LocalHistoryAllocations, LocalSnapshotAllocation, +}; +use nocterm_ai::storage::AgentStorageError; + +pub(crate) type LocalPayloadAllocation = (LocalHistoryAllocations, HistoryPayloadPermit); + +pub(super) struct LocalPayloads { + allocations: LocalHistoryAllocations, + resident: Option, + requester: EntityId, +} +impl LocalPayloads { + pub(super) fn new( + allocations: LocalHistoryAllocations, + resident: HistoryPayloadPermit, + requester: EntityId, + ) -> Self { + Self { + allocations, + resident: Some(resident), + requester, + } + } + pub(super) fn order(&self) -> Option { + self.resident.as_ref().map(HistoryPayloadPermit::order) + } + pub(super) fn archive(&mut self) { + self.resident.take(); + } + pub(super) fn ensure_resident(&mut self, cx: &mut App) -> Result<(), AgentStorageError> { + if self.resident.is_none() { + self.resident = Some( + self.allocations + .reserve_resident(Some(self.requester), cx) + .map_err(|error| error.into_storage())?, + ); + } + Ok(()) + } + pub(super) fn adopt_resident(&mut self, payload: HistoryPayloadPermit) { + self.resident = Some(payload); + } + pub(super) fn history_admission( + &self, + ) -> nocterm_agent_runtime::history_admission::HistoryAdmissionHandle { + self.allocations.history_admission() + } + pub(super) fn restart( + &self, + document: &super::document::ChatDocument, + input: &super::document::Inputs<'_>, + existing: usize, + cx: &mut App, + ) -> Result { + Ok(self + .snapshot(existing, document.restart_capacity(input)?, cx)? + .into_lease()) + } + pub(super) fn fork( + &self, + document: &super::document::ChatDocument, + input: &super::document::Inputs<'_>, + existing: usize, + len: usize, + cx: &mut App, + ) -> Result { + Ok(self + .snapshot(existing, document.fork_capacity(input, len)?, cx)? + .into_lease()) + } + pub(super) fn snapshot( + &self, + existing: usize, + capacity: usize, + cx: &mut App, + ) -> Result { + self.allocations + .reserve_snapshot(self.requester, existing, capacity, cx) + .map_err(|error| error.into_storage()) + } +} diff --git a/crates/agent-chat/src/thread/queue.rs b/crates/agent-chat/src/thread/queue.rs index 62c97df5..6069351a 100644 --- a/crates/agent-chat/src/thread/queue.rs +++ b/crates/agent-chat/src/thread/queue.rs @@ -1,22 +1,19 @@ -//! Prompt snapshots and FIFO dispatch. Cancellation completes before dispatch resumes. +//! Prompt snapshots and FIFO dispatch. Cancellation completes before dispatch resumes, and a +//! turn the agent runs for another device holds dispatch until it ends. use super::tools::unreachable; use super::{ background_open::BackgroundOpen, bound_call::BoundToolCall, connection_wait::{ConnectionWaitError, WaitTarget}, - execution::jobs, + execution::WaitSubject, }; use gpui::{App, Context, EntityId, Task}; use nocterm_ai::history::{SavedAttachment, SavedPrompt}; use nocterm_ai::model::content::Content; use nocterm_ai::{BridgeCall, TerminalCall, ToolFailure}; -use nocterm_session::ExecRequest; use nocterm_workspace_ports::{ExpectedSession, TerminalEntry, TerminalStatus}; -use std::sync::{ - Arc, - atomic::{AtomicUsize, Ordering}, -}; +use std::sync::Arc; use super::{AgentThread, Attachment}; @@ -26,7 +23,6 @@ pub(super) use submit_error::SubmitError; #[derive(Clone)] pub struct QueuedPrompt { saved: Arc, - image_keys: Arc>>, attachments: Vec, encoded_len: usize, memory_len: usize, @@ -42,38 +38,19 @@ impl QueuedPrompt { attachments: Vec, encoded_len: Option, ) -> Self { - static NEXT_IMAGE: AtomicUsize = AtomicUsize::new(1); let encoded_len = encoded_len .unwrap_or_else(|| nocterm_ai::history::prompt_size(&saved).unwrap_or(usize::MAX)); - let image_keys = saved - .images - .iter() - .map(|block| match block { - Content::Image(image) => Some(( - NEXT_IMAGE.fetch_add(1, Ordering::Relaxed), - image.data.len(), - true, - )), - _ => None, - }) - .collect(); let memory_len = nocterm_ai::history::prompt_memory(&saved).unwrap_or(usize::MAX); Self { memory_len, saved: Arc::new(saved), attachments, - image_keys: Arc::new(image_keys), encoded_len, } } pub fn saved(&self) -> &Arc { &self.saved } - /// One key per image block: its preview identity, encoded size and - /// whether it still awaits preparation; `None` for other blocks. - pub fn image_keys(&self) -> &[Option<(usize, usize, bool)>] { - &self.image_keys - } /// What the prompt was written about, captured when it was queued. pub fn attachments(&self) -> &[Attachment] { &self.attachments @@ -87,7 +64,7 @@ impl QueuedPrompt { } impl AgentThread { - fn start_execution_watch(&mut self, cx: &mut Context) { + pub(super) fn start_execution_watch(&mut self, cx: &mut Context) { if self.tools.has_watch() { return; } @@ -98,9 +75,9 @@ impl AgentThread { })); } - pub(super) fn poll_command_wait( + pub(super) fn poll_tool_wait( &mut self, - operation: &super::execution::CommandWaitOperation, + operation: &super::execution::ToolWaitOperation, cx: &mut Context, ) -> Result, ToolFailure> { if !self.session.session_current(operation.ticket()) @@ -110,23 +87,18 @@ impl AgentThread { return Err(ToolFailure::from("Chat is unavailable.")); } self.revoke_executions(cx); - let state = self - .tools - .command_snapshot(operation.id(), operation.terminal())?; - operation.frame(state, self.deps.ai(cx).approval.redact_secrets) + operation.frame(&mut self.tools, self.deps.ai(cx).approval.redact_secrets) } - fn wait_command( + pub(super) fn wait_tool( &mut self, - terminal: String, - id: String, + subject: WaitSubject, yield_ms: u64, call: BridgeCall, cx: &mut Context, ) { - let operation = super::execution::CommandWaitOperation::new( - terminal, - id, + let operation = super::execution::ToolWaitOperation::new( + subject, yield_ms, self.session.ticket(), self.hold_operation(), @@ -150,9 +122,8 @@ impl AgentThread { let TerminalCall::ReadCommand(request) = &call.call else { return; }; - let terminal = request.terminal_id.clone(); - let id = request.command_id.clone(); - self.wait_command(terminal, id, request.yield_ms.unwrap_or(0), call, cx); + let subject = WaitSubject::command(&request.terminal_id, &request.command_id); + self.wait_tool(subject, request.yield_ms.unwrap_or(0), call, cx); } pub(super) fn execute_command( @@ -165,48 +136,19 @@ impl AgentThread { let TerminalCall::ExecCommand(request) = &call.call else { return; }; - let executor = match entry - .capabilities - .execution() - .ok_or(nocterm_workspace_ports::TerminalError::Unsupported( - nocterm_workspace_ports::TerminalCapability::StructuredExecution, - )) - .and_then(|port| port.executor(expected, cx)) + let source = + super::tool_runtime::source_execution::SourceExecution::capture(self.deps.runtime()); + let (id, run) = match source + .and_then(|source| source.start(entry, expected, request, &mut self.tools, cx)) { - Ok(executor) => executor, - Err(error) => { - self.finish(call, Err(error.to_string().into())); - return; - } + Ok(started) => started, + Err(error) => return self.finish(call, Err(error)), }; - let program = ExecRequest { - program: request.program.clone(), - args: request.args.clone(), - stdin: request - .stdin - .as_ref() - .map(|stdin| stdin.as_bytes().to_vec()), - }; - let timer = cx - .background_executor() - .timer(jobs::timeout(request.timeout_ms)); - let started = - match self - .tools - .start_command(request.terminal_id.clone(), executor, program, timer) - { - Ok(started) => started, - Err(error) => { - self.finish(call, Err(error)); - return; - } - }; - let (id, run) = started.into_run(); cx.background_executor().spawn(run).detach(); self.start_execution_watch(cx); - let terminal = request.terminal_id.clone(); + let subject = WaitSubject::command(&request.terminal_id, &id); let yield_ms = request.yield_ms.unwrap_or(1000); - self.wait_command(terminal, id, yield_ms, call, cx); + self.wait_tool(subject, yield_ms, call, cx); } fn reconnect_target( @@ -219,6 +161,9 @@ impl AgentThread { .bindings .window() .ok_or(ConnectionWaitError::WorkspaceClosed)?; + if !self.deps.enabled(cx) { + return Err(ConnectionWaitError::Stopped); + } let receipt = self.tools.prepare_reconnect(entry, expected, cx)?; Ok(WaitTarget::new( self.deps.seat().clone(), @@ -363,8 +308,15 @@ impl AgentThread { DispatchAction::Execute(entry, expected, call) => { self.execute_command(entry, expected, call, cx) } + DispatchAction::Files(call, work) => { + let guard = self.hold_operation(); + super::tool_runtime::file_dispatch::complete(call, work, guard, cx); + } DispatchAction::Read(call) => self.read_command(call, cx), DispatchAction::Cancel(call) => self.cancel_command(call, cx), + DispatchAction::Transfer(admission) => self.start_transfer(*admission, cx), + DispatchAction::ReadTransfer(call) => self.read_transfer(call, cx), + DispatchAction::CancelTransfer(call) => self.cancel_transfer(call, cx), DispatchAction::Ignore => {} } } @@ -377,7 +329,9 @@ impl AgentThread { cx: &mut Context, ) { let redact = self.deps.ai(cx).approval.redact_secrets; - let action = self.tools.dispatch(call, entry, expected, redact, cx); + let action = self + .tools + .dispatch(call, entry, expected, redact, self.deps.enabled(cx), cx); self.apply_dispatch_action(action, cx); } @@ -558,6 +512,9 @@ impl AgentThread { call: BridgeCall, cx: &mut Context, ) { + if !self.deps.enabled(cx) { + return self.finish_unreachable(call, "Chat is unavailable."); + } let mut operation = match super::live::LiveCommandOperation::acquire(entry, expected, request, cx) { Ok(operation) => operation, @@ -590,7 +547,11 @@ impl AgentThread { // ACP adapters inspect the first block for slash commands; local // administrative commands may require a single text block. let is_command = nocterm_ai::commands::invocation(&text, self.config.commands()).is_some(); - let context = (!is_command).then(|| self.context(cx)); + // An agent with tools of its own gets only what the person wrote. + let own_tools = self + .agent_info() + .is_some_and(|info| info.capabilities.own_tools); + let context = (!is_command && !own_tools).then(|| self.context(cx)); let history = (self.state.pending_history() && !is_command) .then(|| super::session::history_context(self.state.entries())); let prepared = super::composer::prompt_preparation::PromptPreparation::new( @@ -605,8 +566,15 @@ impl AgentThread { let resume = super::document::Resume::new(Some(session.to_string()), self.session_workdir().clone()); let input = super::document::Inputs::new(&self.composer, &self.config, attachments, resume); - self.state - .append_user(prepared.visible().to_vec(), self.deps.timestamp(), &input); + if let Err(error) = + self.state + .append_user(prepared.visible().to_vec(), self.deps.timestamp(), &input) + { + self.fail(&error.to_string(), cx); + return; + } + // Consume only after live-transcript admission: a refusal preserves the queued body. + let _ = self.composer.take_next(); self.mark_dirty(self.state.entries().len() - 1); self.persist(cx); self.notice.present("Thinking…".into()); @@ -647,6 +615,7 @@ impl AgentThread { self.prune_background(cx); } } + self.offer_tools(cx); self.accept_document_inputs(cx, false); self.save(cx); cx.notify(); @@ -692,7 +661,10 @@ impl AgentThread { if !self.deps.enabled(cx) || self.session.sign_in_required() { return Ok(false); } - if text.trim().is_empty() && self.composer.images().is_empty() { + if text.trim().is_empty() + && self.composer.images().is_empty() + && self.composer.files().is_empty() + { return Ok(false); } let candidate = self.composer.prepare_submission( @@ -726,6 +698,36 @@ impl AgentThread { Ok(true) } + /// A turn the agent runs for another device: its prompt joins the transcript and the chat + /// shows the agent working until the turn ends; then the chat's own queue goes on. + pub(super) fn external_turn( + &mut self, + activity: &nocterm_ai::model::session::TurnActivity, + cx: &mut Context, + ) { + use nocterm_ai::model::session::TurnActivity; + match activity { + TurnActivity::Started { prompt } => { + if let Err(error) = self.state.append_prompt(prompt, self.deps.timestamp()) { + return self.fail(&error.to_string(), cx); + } + if self.session.begin_external_turn() { + self.notice.present("Working…".into()); + } + } + TurnActivity::Ended => { + if !self.session.finish_external_turn() { + return; + } + self.finalize_tool_displays(); + self.notice.present("Ready".into()); + self.persist(cx); + self.dispatch_next(cx); + } + } + cx.notify(); + } + pub fn dispatch_next(&mut self, cx: &mut Context) { if !self.queue_dispatchable() || self.session.generating() @@ -745,7 +747,7 @@ impl AgentThread { if self.session.phase() != nocterm_ai::session::SessionPhase::Ready { return; } - let Some(prompt) = self.composer.take_next() else { + let Some(prompt) = self.composer.queue().first().cloned() else { return; }; // Context is fixed for this turn, including terminal tool access. @@ -770,7 +772,7 @@ pub(super) fn restore_attachments(values: &[SavedAttachment]) -> Vec mod tests { use super::*; #[test] - fn rendering_snapshot_shares_image_payload_and_precomputed_keys() { + fn rendering_snapshot_shares_immutable_image_payload() { let prompt = QueuedPrompt::new( SavedPrompt { id: 1, @@ -784,8 +786,14 @@ mod tests { Vec::new(), ); let copy = prompt.clone(); - assert!(Arc::ptr_eq(&prompt.saved, ©.saved)); - assert!(Arc::ptr_eq(&prompt.image_keys, ©.image_keys)); - assert_eq!(copy.image_keys[0].unwrap().1, 2 * 1024 * 1024); + assert!(Arc::ptr_eq(prompt.saved(), copy.saved())); + let Content::Image(original) = &prompt.saved().images[0] else { + panic!("image"); + }; + let Content::Image(cloned) = ©.saved().images[0] else { + panic!("image"); + }; + assert_eq!(original.data.as_ptr(), cloned.data.as_ptr()); + assert_eq!(cloned.data.len(), 2 * 1024 * 1024); } } diff --git a/crates/agent-chat/src/thread/restart.rs b/crates/agent-chat/src/thread/restart.rs index f1250630..9679c332 100644 --- a/crates/agent-chat/src/thread/restart.rs +++ b/crates/agent-chat/src/thread/restart.rs @@ -5,6 +5,7 @@ pub struct RestartData { config: super::config::RestartConfig, composer: Composer, restore: Option, + _allocation: nocterm_core::allocation::AllocationLease, } impl RestartData { pub(super) fn new( @@ -12,12 +13,14 @@ impl RestartData { config: super::config::RestartConfig, composer: Composer, restore: Option, + allocation: nocterm_core::allocation::AllocationLease, ) -> Self { Self { document, config, composer, restore, + _allocation: allocation, } } pub(super) fn into_parts( @@ -31,6 +34,9 @@ impl RestartData { (self.document, self.config, self.composer, self.restore) } + pub fn key(&self) -> &nocterm_ai::execution_origin::ChatKey { + self.document.key() + } pub fn agent_id(&self) -> &str { self.document.agent_id() } diff --git a/crates/agent-chat/src/thread/saved.rs b/crates/agent-chat/src/thread/saved.rs index 937689cb..be2c6eba 100644 --- a/crates/agent-chat/src/thread/saved.rs +++ b/crates/agent-chat/src/thread/saved.rs @@ -1,9 +1,8 @@ //! A chat as saved in the history: restoring, forking, naming and saving. use gpui::{App, Context}; -use nocterm_ai::model::content::Content; use nocterm_ai::model::controls::ConfigSetting; use nocterm_ai::model::ids::{ConfigId, ModeId, SessionId}; -use nocterm_ai::thread::ThreadState; +use nocterm_ai::{history::SharedChat, storage::AgentStorageError}; use std::{sync::Arc, time::Duration}; use super::{AgentThread, Attachment, Composer, Fork, RestartData, Restore, archive}; @@ -61,10 +60,11 @@ impl AgentThread { pub fn begin_composer_edit( &mut self, images: Vec, + files: Vec, attachments: Vec, cx: &mut Context, ) { - self.composer.begin_edit(images, attachments); + self.composer.begin_edit(images, files, attachments); cx.notify(); } @@ -84,6 +84,23 @@ impl AgentThread { Ok(()) } + /// Adds files to the next prompt; files it holds already are not added twice. + pub fn add_composer_files( + &mut self, + files: Vec, + cx: &mut Context, + ) -> Result<(), nocterm_ai::prompt_files::PromptFileError> { + self.composer.add_files(files)?; + cx.notify(); + Ok(()) + } + + pub fn remove_composer_file(&mut self, index: usize, cx: &mut Context) { + if self.composer.remove_file(index) { + cx.notify(); + } + } + #[cfg(any(test, feature = "test-support"))] pub fn clear_composer_context(&mut self, cx: &mut Context) { self.replace_composer_context(Vec::new(), cx); @@ -218,81 +235,105 @@ impl AgentThread { self.session.restore_descriptor() } - pub fn restarted( + pub(crate) fn restarted( data: RestartData, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { let (document, config, composer, restore) = data.into_parts(); let state = super::document::ChatDocument::restarted(document); - let mut thread = Self::from_document(state, deps, cx); + let mut thread = Self::from_document(state, deps, allocation, cx); thread.config.hydrate_restart(config); thread.composer = composer; thread.session.import_restore(restore); thread } - pub fn restart_data(&self) -> RestartData { - RestartData::new( + pub fn restart_data(&self, cx: &mut App) -> Result { + let input = super::document::Inputs::new( + &self.composer, + &self.config, + self.stable_attachments(self.composer.default_attachments(), cx), + self.persistent_resume(), + ); + let allocation = + self.payloads + .restart(&self.state, &input, self.resident_history_bytes(), cx)?; + Ok(RestartData::new( self.state.restart_snapshot(), self.config.restart_snapshot(), self.composer.restarted(), self.restore_descriptor(), - ) + allocation, + )) } pub fn capture_pending( &mut self, - cx: &App, - ) -> Option<(Arc, u64)> { + cx: &mut App, + ) -> Result, u64)>, AgentStorageError> { self.state.cancel_debounce(); if !self.state.needs_candidate() { - return self.state.existing_offer(); + return Ok(self.state.existing_offer()); } if self.is_archived() { - let snapshot = self - .archive - .draft_patch(self.state.chat_id(), self.state.agent_id()) - .map(|metadata| { - Arc::new(nocterm_ai::history::SharedChat { - metadata, - queue: Vec::new(), - }) - })?; - return self.state.capture_patch(snapshot); + let allocation = self.payloads.snapshot( + self.resident_history_bytes(), + self.archive.draft_capacity(), + cx, + )?; + let Some(metadata) = self.archive.draft_patch() else { + return Ok(None); + }; + let snapshot = allocation.retain(Arc::new(SharedChat { + metadata, + queue: Vec::new(), + }))?; + return Ok(self.state.capture_patch(snapshot)); } let attachments = self.stable_attachments(self.composer.default_attachments(), cx); let resume = self.persistent_resume(); let input = super::document::Inputs::new(&self.composer, &self.config, attachments, resume); - self.state.propose_candidate(&input) + let capacity = self.state.snapshot_capacity(&input)?; + let allocation = self + .payloads + .snapshot(self.resident_history_bytes(), capacity, cx)?; + let Some(snapshot) = self.state.candidate_snapshot(&input) else { + return Ok(None); + }; + let snapshot = allocation.retain(snapshot)?; + Ok(self.state.adopt_candidate(&input, snapshot)) } - pub fn flush_snapshot(&self, cx: &App) -> Option> { + pub fn flush_snapshot( + &self, + cx: &mut App, + ) -> Result>, AgentStorageError> { if self.state.clean() { - return None; + return Ok(None); } if let Some(snapshot) = self.state.current_snapshot() { - return Some(snapshot); + return Ok(Some(snapshot)); } if self.is_archived() { + let allocation = self.payloads.snapshot( + self.resident_history_bytes(), + self.archive.draft_capacity(), + cx, + )?; return self .archive - .draft_patch(self.state.chat_id(), self.state.agent_id()) + .draft_patch() .map(|metadata| { - Arc::new(nocterm_ai::history::SharedChat { + allocation.retain(Arc::new(SharedChat { metadata, queue: Vec::new(), - }) - }); - } - self.shared_snapshot(cx).or_else(|| { - self.state.history_present().then(|| { - Arc::new(nocterm_ai::history::SharedChat { - metadata: self.history_metadata(cx), - queue: Vec::new(), + })) }) - }) - }) + .transpose(); + } + self.shared_snapshot(cx) } pub fn flush_draft(&mut self, cx: &mut Context) { @@ -326,15 +367,25 @@ impl AgentThread { cx.notify(); } - pub fn evict_history(&mut self, cx: &Context) -> bool { - let gate = archive::EvictionGate::new( + pub fn can_release_archive(&self) -> bool { + self.archive.can_release() + && self.state.clean() + && !self.state.draft_changed() + && !self.session.leased() + && !self.session_busy() + } + pub(super) fn history_eviction_gate(&self) -> archive::EvictionGate { + archive::EvictionGate::new( !self.session.leased(), - !self.session_busy(), + !self.session_busy() && !self.session.shown(), self.state.desired_revision() == self.state.published_revision(), !self.state.draft_changed(), self.state.persistence_error().is_none(), self.state.history_present(), - ); + ) + } + pub fn evict_history(&mut self, cx: &mut Context) -> bool { + let gate = self.history_eviction_gate(); let mut summary = nocterm_ai::history::ChatSummary::from_chat(&self.history_metadata(cx)); summary.title = Some(self.title()); summary.has_content = !self.state.entries().is_empty() || !self.composer.is_empty(); @@ -342,8 +393,10 @@ impl AgentThread { return false; } self.state.evict_clean(); + self.payloads.archive(); self.composer = Default::default(); self.tools.clear_displays(); + cx.notify(); true } @@ -371,43 +424,47 @@ impl AgentThread { } } - fn on_archive_loaded( + pub(super) fn on_archive_loaded( &mut self, completion: archive::load_operation::LoadCompletion, cx: &mut Context, ) { - let (load_id, ticket, result, permit) = completion.into_parts(); - - let current = self.session.session_current(ticket) - && self.deps.enabled(cx) - && self - .deps - .runtime() - .document_is_current(self.state.chat_id(), cx.entity_id(), cx); - match self.archive.accept(load_id, current, result, permit) { - archive::LoadOutcome::Stale => return, - archive::LoadOutcome::Failed(error) => { - self.notice - .present(format!("Could not load saved chat: {error}")); - self.notice.mark_error(); - } - archive::LoadOutcome::Hydrate { - chat, - overlay, - intents, - } => { - self.restore_content_with_overlay(*chat, overlay); - self.preview_config(cx); - if self.state.draft_changed() { - self.save(cx); + completion.publish(|load_id, ticket, result, permit, payload| { + let current = self.session.session_current(ticket) + && self.deps.enabled(cx) + && self.deps.runtime().document_is_current( + self.state.chat_id(), + cx.entity_id(), + cx, + ); + match self.archive.accept(load_id, current, result, permit) { + archive::LoadOutcome::Stale => return, + archive::LoadOutcome::Failed(error) => { + self.notice + .present(format!("Could not load saved chat: {error}")); + self.notice.mark_error(); } - for intent in intents { - self.apply_archive_intent(intent, cx); + archive::LoadOutcome::Hydrate { + chat, + overlay, + intents, + } => { + if let Some(payload) = payload { + self.payloads.adopt_resident(payload); + } + self.restore_content_with_overlay(*chat, overlay); + self.preview_config(cx); + if self.state.draft_changed() { + self.save(cx); + } + for intent in intents { + self.apply_archive_intent(intent, cx); + } + self.warm(cx); } - self.warm(cx); } - } - cx.notify(); + cx.notify(); + }); } pub fn load_archive(&mut self, intent: archive::ArchiveIntent, cx: &mut Context) { @@ -427,12 +484,7 @@ impl AgentThread { let reader = self.deps.runtime().history_reader(cx); let operation = archive::load_operation::LoadOperation::new(load_id, ticket, chat_id, reader); - let loading = cx.background_executor().spawn(operation.run()); - cx.spawn(async move |this, cx| { - let completion = loading.await; - let _ = this.update(cx, |this, cx| this.on_archive_loaded(completion, cx)); - }) - .detach(); + operation.start(self.payloads.history_admission(), cx); cx.notify(); } @@ -441,26 +493,43 @@ impl AgentThread { } /// A restored document connects when shown with warm start enabled. - pub fn restored( + pub(crate) fn restored( chat: nocterm_ai::history::SavedChat, deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, cx: &mut Context, ) -> Self { let mut thread = Self::from_document( super::document::ChatDocument::new( - chat.agent_id.clone(), - chat.id.clone(), - chat.updated, + chat.header.agent_id.clone(), + nocterm_ai::execution_origin::ChatKey::from_header(&chat.header), + chat.header.updated, ), deps, + allocation, cx, ); - thread.restore_content(chat); + thread.restore_content_with_overlay(chat, None); thread.preview_config(cx); thread } - pub fn restore_content(&mut self, chat: nocterm_ai::history::SavedChat) { + pub fn restore_content( + &mut self, + chat: nocterm_ai::history::SavedChat, + cx: &mut Context, + ) -> Result<(), AgentStorageError> { + self.deps.require_local_scope(&chat.header.chat_scope())?; + chat.validate_identity()?; + if chat.header.id != self.state.chat_id() { + return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); + } + let chat = nocterm_ai::history::prepare_owned(chat)?; + self.payloads.ensure_resident(cx)?; + self.deps + .runtime() + .admit_history(cx.entity_id(), chat.resident_bytes(), cx)?; self.restore_content_with_overlay(chat, None); + Ok(()) } pub(super) fn restore_content_with_overlay( &mut self, @@ -513,54 +582,54 @@ impl AgentThread { } /// A copy of this chat, to go on with separately. When opened, the copy /// continues in a copy of this chat's agent session. - pub fn fork(&self) -> Fork { - self.fork_at(self.state.entries().len()) + pub fn fork(&self, cx: &mut App) -> Result { + self.fork_at(self.state.entries().len(), cx) } /// A copy of the chat's first `len` entries. Only a copy of the whole /// chat continues in a copy of the agent's session; an earlier cut /// starts a new session, since the agent cannot forget the rest. - pub fn fork_at(&self, len: usize) -> Fork { - let whole = len >= self.state.entries().len(); - let mut chat = self.deps.new_chat(self.state.agent_id().to_owned()); - let mut state = self.state.transcript().clone(); - state.truncate(len); - chat.entries = state.entries; - chat.times = state.times; - chat.title = self.state.transcript().title.clone(); - chat.name = Some(format!("{} (fork)", self.title())); - chat.model = self.model(); - chat.config = self.config.choices().clone(); - chat.mode = self.config.mode_choice().map(ToString::to_string); - chat.pending_history = self.state.pending_history() || !whole; - let restore = match (self.session(), self.session.restore()) { - (Some(session), _) => self.session_workdir().clone().map(|workdir| Restore { - session: session.clone(), - workdir, - fork: true, - }), - (None, Some(restore)) => Some(Restore { - fork: true, - ..restore.clone() - }), - (None, None) => None, - } - .filter(|_| whole); - Fork { + pub fn fork_at(&self, len: usize, cx: &mut App) -> Result { + let input = super::document::Inputs::new( + &self.composer, + &self.config, + self.stable_attachments(self.composer.default_attachments(), cx), + self.persistent_resume(), + ); + let allocation = + self.payloads + .fork(&self.state, &input, self.resident_history_bytes(), len, cx)?; + let chat = self + .deps + .new_chat_for_origin(self.state.agent_id().to_owned(), self.state.key()); + let restore = Fork::restoration( + self.session(), + self.session_workdir(), + self.session.restore().as_ref(), + ); + Ok(Fork::capture( chat, + super::fork::ForkSource::new(&self.state, &self.composer, &self.config, self.title()), restore, - attachments: self.composer.default_attachments().to_vec(), - } + len, + allocation, + )) } /// The chat `fork` made, connected only when work is submitted. - pub fn forked(fork: Fork, deps: crate::chat_deps::ChatDeps, cx: &mut Context) -> Self { + pub(crate) fn forked( + fork: Fork, + deps: crate::chat_deps::ChatDeps, + allocation: crate::thread::local_payloads::LocalPayloadAllocation, + cx: &mut Context, + ) -> Self { + let (chat, restore, attachments) = fork.into_parts(); let pending_history = - fork.chat.pending_history || (fork.restore.is_none() && !fork.chat.entries.is_empty()); - let mut thread = Self::restored(fork.chat, deps, cx); + chat.pending_history || (restore.is_none() && !chat.entries.is_empty()); + let mut thread = Self::restored(chat, deps, allocation, cx); thread .state .set_pending_history(pending_history, thread.deps.timestamp()); - thread.session.import_restore(fork.restore); - thread.composer.replace_context(fork.attachments); + thread.session.import_restore(restore); + thread.composer.replace_context(attachments); thread.establish_clean_document_inputs(cx); thread.state.fork_created(); thread.notice.present("Forked chat".into()); @@ -570,7 +639,7 @@ impl AgentThread { /// the first message. pub fn title(&self) -> String { self.archive.summary().map_or_else( - || chat_title(self.state.name(), self.state.transcript()), + || super::title::chat_title(self.state.name(), self.state.transcript()), |summary| summary.title().to_owned(), ) } @@ -625,10 +694,10 @@ impl AgentThread { } pub fn shared_snapshot( &self, - cx: &gpui::App, - ) -> Option> { + cx: &mut App, + ) -> Result>, AgentStorageError> { if self.is_archived() { - return None; + return Ok(None); } let input = super::document::Inputs::new( &self.composer, @@ -636,7 +705,14 @@ impl AgentThread { self.stable_attachments(self.composer.default_attachments(), cx), self.persistent_resume(), ); - self.state.snapshot(&input) + let capacity = self.state.snapshot_capacity(&input)?; + let allocation = self + .payloads + .snapshot(self.resident_history_bytes(), capacity, cx)?; + self.state + .candidate_snapshot(&input) + .map(|snapshot| allocation.retain(snapshot)) + .transpose() } pub(super) fn accept_document_inputs(&mut self, cx: &gpui::App, draft: bool) -> bool { let attachments = self.stable_attachments(self.composer.default_attachments(), cx); @@ -653,7 +729,14 @@ impl AgentThread { } #[cfg(any(test, feature = "test-support"))] pub fn snapshot(&self, cx: &gpui::App) -> Option { - let shared = self.shared_snapshot(cx)?; + // Assertion-only export; production persistence uses charged captures. + let input = super::document::Inputs::new( + &self.composer, + &self.config, + self.stable_attachments(self.composer.default_attachments(), cx), + self.persistent_resume(), + ); + let shared = self.state.snapshot(&input)?; let mut chat = shared.metadata.clone(); chat.queue = shared .queue @@ -666,8 +749,13 @@ impl AgentThread { self.save(cx); } pub fn save(&mut self, cx: &mut Context) { - let Some((chat, revision)) = self.capture_pending(cx) else { - return; + let (chat, revision) = match self.capture_pending(cx) { + Ok(Some(value)) => value, + Ok(None) => return, + Err(error) => { + self.document_save_failed(self.state.desired_revision(), error.into(), cx); + return; + } }; if !self.state.mark_normal_offer(revision) { return; @@ -691,33 +779,3 @@ impl AgentThread { }); } } - -/// A chat's title: `name`, else the agent's title, else the start of the -/// first message. A chat with nothing in it is a new one. -pub(super) fn chat_title(name: Option<&str>, state: &ThreadState) -> String { - if let Some(name) = name.filter(|name| !name.trim().is_empty()) { - return name.to_owned(); - } - if state.entries.is_empty() { - return "New Agent".into(); - } - state - .title - .as_ref() - .filter(|title| !title.trim().is_empty()) - .cloned() - .or_else(|| { - state.entries.iter().find_map(|entry| match entry { - nocterm_ai::thread::Entry::User(parts) => { - parts.iter().find_map(|part| match part { - Content::Text(text) if !text.text.trim().is_empty() => { - Some(text.text.trim().chars().take(60).collect()) - } - _ => None, - }) - } - _ => None, - }) - }) - .unwrap_or_else(|| "New Agent".into()) -} diff --git a/crates/agent-chat/src/thread/session_driver.rs b/crates/agent-chat/src/thread/session_driver.rs index c3c8ac05..dc7558b0 100644 --- a/crates/agent-chat/src/thread/session_driver.rs +++ b/crates/agent-chat/src/thread/session_driver.rs @@ -222,6 +222,12 @@ impl SessionDriver { pub(super) fn begin_cancel(&mut self) -> bool { self.phase.stop() } + pub(super) fn begin_external_turn(&mut self) -> bool { + self.phase.start_external_turn() + } + pub(super) fn finish_external_turn(&mut self) -> bool { + self.phase.finish_external_turn() + } pub(super) fn opened(&mut self) { self.phase.opened() } diff --git a/crates/agent-chat/src/thread/session_grants.rs b/crates/agent-chat/src/thread/session_grants.rs index e5bfcda4..a65fb1ca 100644 --- a/crates/agent-chat/src/thread/session_grants.rs +++ b/crates/agent-chat/src/thread/session_grants.rs @@ -1,5 +1,5 @@ //! Reusable target grants expire when the observed session changes. -use nocterm_ai::{ApprovalSettings, TerminalCall, ToolCapability, approval::ApprovalGrants}; +use nocterm_ai::{ApprovalSettings, TerminalCall, approval::ApprovalGrants}; use nocterm_workspace_ports::ExpectedSession; use std::collections::HashMap; #[derive(Default)] @@ -9,9 +9,21 @@ pub(super) struct SessionGrants { } impl SessionGrants { pub(super) fn observe(&mut self, call: &TerminalCall, expected: Option) { - let Some(id) = call.terminal_id() else { - return; - }; + if let Some(id) = call.terminal_id() { + self.observe_session(id, expected); + } + } + /// The source terminal of a transfer expires its grants the same way. + pub(super) fn observe_source( + &mut self, + call: &TerminalCall, + expected: Option, + ) { + if let Some(id) = call.source_terminal_id() { + self.observe_session(id, expected); + } + } + fn observe_session(&mut self, id: &str, expected: Option) { if self.sessions.get(id).copied() != expected { self.grants.revoke(id); self.sessions.remove(id); @@ -23,8 +35,8 @@ impl SessionGrants { pub(super) fn requires_approval(&self, call: &TerminalCall, policy: &ApprovalSettings) -> bool { self.grants.requires_approval(call, policy) } - pub(super) fn grant(&mut self, id: &str, capability: ToolCapability) { - self.grants.grant(id, capability); + pub(super) fn grant_call(&mut self, call: &TerminalCall) { + self.grants.grant_call(call); } pub(super) fn clear(&mut self) { self.grants.clear(); diff --git a/crates/agent-chat/src/thread/title.rs b/crates/agent-chat/src/thread/title.rs new file mode 100644 index 00000000..33230911 --- /dev/null +++ b/crates/agent-chat/src/thread/title.rs @@ -0,0 +1,33 @@ +//! What a chat is called in the history. +use nocterm_ai::model::content::Content; +use nocterm_ai::thread::ThreadState; + +/// A chat's title: `name`, else the agent's title, else the start of the +/// first message. A chat with nothing in it is a new one. +pub(crate) fn chat_title(name: Option<&str>, state: &ThreadState) -> String { + if let Some(name) = name.filter(|name| !name.trim().is_empty()) { + return name.to_owned(); + } + if state.entries.is_empty() { + return "New Agent".into(); + } + state + .title + .as_ref() + .filter(|title| !title.trim().is_empty()) + .cloned() + .or_else(|| { + state.entries.iter().find_map(|entry| match entry { + nocterm_ai::thread::Entry::User(parts) => { + parts.iter().find_map(|part| match part { + Content::Text(text) if !text.text.trim().is_empty() => { + Some(text.text.trim().chars().take(60).collect()) + } + _ => None, + }) + } + _ => None, + }) + }) + .unwrap_or_else(|| "New Agent".into()) +} diff --git a/crates/agent-chat/src/thread/tool_display/tests.rs b/crates/agent-chat/src/thread/tool_display/tests.rs index 27901fae..2930835c 100644 --- a/crates/agent-chat/src/thread/tool_display/tests.rs +++ b/crates/agent-chat/src/thread/tool_display/tests.rs @@ -7,9 +7,9 @@ fn normalize(displays: &mut ToolDisplays, entries: &mut Vec) -> Vec Self { + Self::new(runtime.command_jobs(cx), runtime.transfers(cx)) + } + pub(super) fn new( + jobs: Box, + transfers: Arc, + ) -> Self { + Self { + approvals: Default::default(), + turn: Default::default(), + commands: CommandJobs::new(jobs), + live: Default::default(), + terminals: Default::default(), + displays: Default::default(), + reconnect_dispatch: Default::default(), + transfers: ChatTransfers::new(transfers), + } + } + pub(super) fn prepare_reconnect( &mut self, entry: &nocterm_workspace_ports::TerminalEntry, @@ -78,13 +104,62 @@ impl ToolRuntime { pub(super) fn take_ready_reconnect(&mut self, scope: DispatchScope) -> Option { self.reconnect_dispatch.take_ready(scope) } - pub(super) fn revoke_replaced_executors( + /// Revokes the transfers, live terminals and command executors whose terminals detached or + /// changed session, given the attachments `resolved` now. + pub(super) fn revoke_replaced( &mut self, - current: &[(String, Option>)], - attached: &[String], + resolved: Vec<( + String, + nocterm_workspace_ports::TerminalEntry, + nocterm_ai::context::TerminalDescriptor, + )>, + cx: &gpui::App, ) { - self.live.revoke_detached(attached); - self.commands.revoke_replaced(current); + self.revoke_replaced_transfers(&resolved, cx); + let attached = resolved + .iter() + .map(|(id, _, _)| id.clone()) + .collect::>(); + let current = resolved + .into_iter() + .map(|(id, entry, _)| { + let executor = entry.capabilities.state().info(cx).and_then(|info| { + entry + .capabilities + .execution()? + .executor(info.expected, cx) + .ok() + }); + (id, executor) + }) + .collect::>(); + self.live.revoke_detached(&attached); + self.commands.revoke_replaced(¤t); + } + + /// Where a tool call runs, as its row names it: the attached terminal `entry`, else the + /// offline server the request names. + pub(super) fn destination( + &mut self, + request: &TerminalCall, + entry: Option<&nocterm_workspace_ports::TerminalEntry>, + input: &resolution::Resolution, + defaults: &[Attachment], + cx: &gpui::App, + ) -> Option { + if let Some(entry) = entry { + let info = entry.capabilities.state().info(cx)?; + if info.local { + return Some("This computer".into()); + } + let target = info.target?; + return Some(format!("{} · {target}", entry.title)); + } + let server = request.server_id()?; + self.offline_servers(input, defaults, cx) + .into_iter() + .find(|(descriptor, _)| descriptor.server_id == server) + .map(|(descriptor, summary)| format!("{} · {}", descriptor.name, summary.target)) } pub(super) fn prepare_open( @@ -153,8 +228,12 @@ impl ToolRuntime { entry: nocterm_workspace_ports::TerminalEntry, expected: ExpectedSession, redact: bool, + available: bool, cx: &mut gpui::App, ) -> dispatch::DispatchAction { + if !available { + return dispatch::DispatchAction::Respond(call, Err("Chat is unavailable.".into()), 0); + } let action = dispatch::TerminalDispatch::new(call, entry, expected).prepare(redact, cx); if let dispatch::DispatchAction::Respond(_, _, bytes) = &action { self.turn.account_outcome(*bytes); @@ -195,6 +274,25 @@ impl ToolRuntime { ) -> String { self.turn.context(input, defaults, cx) } + /// Tells the chat's bridge what it offers the agent: the file tools only while an attached + /// terminal takes them. The bridge tells a connected agent when that changes. + pub(super) fn offer_tools( + &mut self, + input: &resolution::Resolution, + defaults: &[Attachment], + registration: Option<&nocterm_ai::BridgeRegistration>, + cx: &gpui::App, + ) { + let Some(registration) = registration else { + return; + }; + let files = self + .turn + .resolved(input, defaults, cx) + .iter() + .any(|(_, _, descriptor)| descriptor.file_tools); + registration.offer(nocterm_ai::ToolOffer::new(files)); + } pub(super) fn describe_target( &mut self, call: &TerminalCall, @@ -206,7 +304,7 @@ impl ToolRuntime { } pub(super) fn busy(&self) -> bool { - self.approvals.pending() || self.commands.active().next().is_some() || self.live.active() + self.approvals.pending() || self.commands.has_active() || self.live.active() } pub(super) fn capture_scope(&mut self, scope: Vec) { self.turn.capture_scope(scope); @@ -295,11 +393,56 @@ impl ToolRuntime { pub(super) fn cancel_execution(&mut self) { self.commands.cancel_all(); self.live.cancel_all(); + self.transfers.cancel_all(); } - pub(super) fn active_commands( - &self, - ) -> impl Iterator)> { - self.commands.active() + /// Commands and transfers still running for this chat. + pub(super) fn has_active_executions(&mut self) -> bool { + self.commands.has_active() || self.transfers.has_active() + } + pub(super) fn start_transfer( + &mut self, + admission: TransferAdmission, + source: Option, + cx: &gpui::App, + ) -> Result> { + transfer_dispatch::start(&mut self.transfers, admission, source, cx) + } + pub(super) fn transfer_progress( + &mut self, + id: &str, + ) -> Result { + self.transfers.progress(id) + } + pub(super) fn cancel_transfer( + &mut self, + id: &str, + redact: bool, + ) -> Result { + let progress = self.transfers.cancel(id)?; + Ok(presentation::transfer_payload(&progress, redact)) + } + /// Cancels transfers whose endpoint terminals detached or changed session. + fn revoke_replaced_transfers( + &mut self, + resolved: &[( + String, + nocterm_workspace_ports::TerminalEntry, + nocterm_ai::context::TerminalDescriptor, + )], + cx: &gpui::App, + ) { + let current = resolved + .iter() + .map(|(id, entry, _)| { + let session = entry + .capabilities + .state() + .info(cx) + .map(|info| info.expected); + (id.clone(), session) + }) + .collect::>(); + self.transfers.revoke_replaced(¤t); } pub(super) fn start_command( &mut self, @@ -431,6 +574,9 @@ impl ToolRuntime { pub(super) fn approval_permission_cards(&self) -> Vec { self.approvals.permission_cards() } + pub(super) fn approval_tool_tokens(&self) -> Vec { + self.approvals.tool_tokens() + } pub(super) fn approval_tool_cards(&self) -> Vec { self.approvals.tool_cards() } @@ -441,7 +587,10 @@ mod tests { use super::*; #[test] fn running_scope_does_not_follow_future_defaults_and_detachment_revokes_only_that_scope() { - let mut tools = ToolRuntime::default(); + let mut tools = ToolRuntime::new( + Box::::default(), + Arc::new(nocterm_transfers::testing::FakeTransfers::default()), + ); let fixed = Attachment::Group("accepted turn".into()); let future = vec![Attachment::Group("next turn".into())]; tools.capture_scope(vec![fixed.clone()]); @@ -457,7 +606,10 @@ mod tests { } #[test] fn clearing_a_turn_preserves_opaque_terminal_identity_and_usage_accounting() { - let mut tools = ToolRuntime::default(); + let mut tools = ToolRuntime::new( + Box::::default(), + Arc::new(nocterm_transfers::testing::FakeTransfers::default()), + ); let id = tools.terminal_id("original item"); let server = tools.server_id("profile"); tools.account_context(40); diff --git a/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs b/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs index de1b928f..6eda6124 100644 --- a/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs +++ b/crates/agent-chat/src/thread/tool_runtime/command_jobs.rs @@ -1,33 +1,21 @@ //! Structured jobs and their watchdog have one bounded owner; consumers receive snapshots only. -use super::super::execution::jobs; use gpui::Task; -pub(in crate::thread) use jobs::Snapshot as CommandSnapshot; +pub(in crate::thread) use jobs::CommandSnapshot; use nocterm_ai::ToolFailure; +use nocterm_session::command_jobs as jobs; use nocterm_session::{ExecRequest, HostExec}; use std::sync::Arc; -#[derive(Default)] pub(super) struct CommandJobs { - jobs: jobs::Jobs, + jobs: Box, watch: Option>, } pub(in crate::thread) use jobs::CommandStart; impl CommandJobs { + pub(super) fn new(jobs: Box) -> Self { + Self { jobs, watch: None } + } pub(super) fn revoke_replaced(&self, current: &[(String, Option>)]) { - let revoked = self - .jobs - .active() - .filter_map(|(id, executor)| { - let replacement = current - .iter() - .find(|(candidate, _)| candidate == id) - .and_then(|(_, executor)| executor.as_ref()); - (!replacement.is_some_and(|replacement| Arc::ptr_eq(replacement, executor))) - .then_some(id.to_owned()) - }) - .collect::>(); - for id in revoked { - self.jobs.cancel_terminal(&id); - } + self.jobs.revoke_replaced(current); } pub(super) fn start( @@ -37,22 +25,26 @@ impl CommandJobs { request: ExecRequest, deadline: impl std::future::Future + Send + 'static, ) -> Result { - Ok(self.jobs.start(terminal, executor, request, deadline)?) + self.jobs + .start(terminal, executor, request, Box::pin(deadline)) + .map_err(|error| error.to_string().into()) } pub(super) fn snapshot( &self, id: &str, terminal: &str, ) -> Result { - Ok(self.jobs.get(id, terminal)?.snapshot()) + self.jobs + .snapshot(id, terminal) + .map_err(|error| error.to_string().into()) } pub(super) fn cancel(&self, id: &str, terminal: &str) -> Result { - let job = self.jobs.get(id, terminal)?; - job.cancel(jobs::State::Cancelled); - Ok(job.snapshot()) + self.jobs + .cancel(id, terminal) + .map_err(|error| error.to_string().into()) } - pub(super) fn active(&self) -> impl Iterator)> { - self.jobs.active() + pub(super) fn has_active(&self) -> bool { + self.jobs.has_active() } pub(super) fn cancel_all(&mut self) { @@ -76,7 +68,8 @@ mod tests { #[test] fn admission_bounds_original_jobs_and_cancellation_retains_readable_snapshots() { let executor = Arc::new(nocterm_session::testing::FakeExec::default()); - let mut jobs = CommandJobs::default(); + let mut jobs = + CommandJobs::new(Box::::default()); let start = |jobs: &mut CommandJobs| { jobs.start( "terminal".into(), @@ -101,9 +94,9 @@ mod tests { jobs.cancel_all(); assert_eq!( jobs.snapshot(&id, "terminal").unwrap().state(), - jobs::State::Cancelled + jobs::CommandState::Cancelled ); - assert!(jobs.active().next().is_none()); + assert!(!jobs.has_active()); assert!(start(&mut jobs).is_ok()); assert!( executor.requests().is_empty(), diff --git a/crates/agent-chat/src/thread/tool_runtime/dispatch.rs b/crates/agent-chat/src/thread/tool_runtime/dispatch.rs index b3f69a6b..970b98b5 100644 --- a/crates/agent-chat/src/thread/tool_runtime/dispatch.rs +++ b/crates/agent-chat/src/thread/tool_runtime/dispatch.rs @@ -18,9 +18,14 @@ pub(in crate::thread) enum DispatchAction { BridgeCall, ), Execute(TerminalEntry, ExpectedSession, BridgeCall), + /// A file call with its work on the terminal's files. + Files(BridgeCall, super::file_dispatch::FileWork), Read(BridgeCall), Cancel(BridgeCall), Ignore, + Transfer(Box), + ReadTransfer(BridgeCall), + CancelTransfer(BridgeCall), ExecuteAdmitted(BridgeCall, TerminalEntry, ExpectedSession), List(BridgeCall), Open(String, BridgeCall), @@ -93,6 +98,23 @@ impl TerminalDispatch { TerminalCall::ReadCommand(_) => DispatchAction::Read(self.call), TerminalCall::CancelCommand(_) => DispatchAction::Cancel(self.call), TerminalCall::ListTerminals | TerminalCall::OpenTerminal(_) => DispatchAction::Ignore, + TerminalCall::ListFiles(_) + | TerminalCall::ReadFile(_) + | TerminalCall::WriteFile(_) + | TerminalCall::EditFile(_) + | TerminalCall::MakeDirectory(_) + | TerminalCall::DeletePath(_) + | TerminalCall::MovePath(_) => { + super::file_dispatch::prepare(self.call, &self.entry, self.expected, redact, cx) + } + // Admission routes transfers before terminal dispatch. + TerminalCall::TransferFiles(_) + | TerminalCall::ReadTransfer(_) + | TerminalCall::CancelTransfer(_) => DispatchAction::Respond( + self.call, + Err("This call does not act on a terminal.".into()), + 0, + ), } } } @@ -125,6 +147,10 @@ impl ExecutionAdmission { TerminalCall::OpenTerminal(request) => { DispatchAction::Open(request.server_id.clone(), bound.into_call()) } + TerminalCall::ReadTransfer(_) => DispatchAction::ReadTransfer(bound.into_call()), + TerminalCall::CancelTransfer(_) => { + DispatchAction::CancelTransfer(bound.into_call()) + } _ => return AdmissionRoute::Resolve(Box::new(bound)), } }; @@ -178,6 +204,9 @@ impl ExecutionAdmission { { return DispatchAction::Reconnect(bound, entry, expected); } + if matches!(bound.call().call, TerminalCall::TransferFiles(_)) { + return super::transfer_dispatch::TransferAdmission::admit(bound, entry, expected); + } // Native read/input execution is a separate admitted step interpreted by the coordinator. DispatchAction::ExecuteAdmitted(bound.into_call(), entry, expected) } diff --git a/crates/agent-chat/src/thread/tool_runtime/file_dispatch.rs b/crates/agent-chat/src/thread/tool_runtime/file_dispatch.rs new file mode 100644 index 00000000..306a5f6a --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/file_dispatch.rs @@ -0,0 +1,93 @@ +//! The file tools of a chat: which attached terminals take them, and one call's work on the +//! filesystem captured under the call's `ExpectedSession`. +//! +//! A terminal takes the file tools when it serves a filesystem of its own but runs no programs: +//! the agent cannot reach its files with `exec_command`, so the chat offers these tools while +//! such a terminal is attached, and they act on nothing else. +use std::sync::Arc; + +use futures::future::BoxFuture; +use gpui::{App, Context}; +use nocterm_ai::{BridgeCall, ToolFailure}; +use nocterm_session::RemoteFs; +use nocterm_workspace_ports::{ + ExpectedSession, TerminalCapability, TerminalEntry, TerminalError, TerminalFilesystem, +}; + +use super::dispatch::DispatchAction; +use crate::thread::{AgentThread, session_driver::OperationGuard}; + +/// One file call's work, answered when it ends. +pub(in crate::thread) type FileWork = BoxFuture<'static, Result>; + +/// Whether the agent reaches this terminal's files only through the file tools. +pub(in crate::thread) fn takes_file_tools( + entry: &TerminalEntry, + expected: ExpectedSession, + cx: &App, +) -> bool { + remote(entry, expected, cx).is_ok() + && entry.capabilities.execution().is_none_or(|execution| { + matches!( + execution.executor(expected, cx), + Err(TerminalError::Unsupported(_)) + ) + }) +} + +fn remote( + entry: &TerminalEntry, + expected: ExpectedSession, + cx: &App, +) -> Result, TerminalError> { + let files = entry + .capabilities + .files() + .ok_or(TerminalError::Unsupported(TerminalCapability::Files))?; + match files.filesystem(expected, cx)? { + TerminalFilesystem::Remote { fs, .. } => Ok(fs), + TerminalFilesystem::Local => Err(TerminalError::Unsupported(TerminalCapability::Files)), + } +} + +/// The work of the file call `call` on its terminal, or why the terminal does not take it. +pub(in crate::thread) fn prepare( + call: BridgeCall, + entry: &TerminalEntry, + expected: ExpectedSession, + redact: bool, + cx: &App, +) -> DispatchAction { + if !takes_file_tools(entry, expected, cx) { + return DispatchAction::Respond( + call, + Err( + "The file tools act only on terminals with file_tools: true. Use exec_command or transfer_files on this terminal." + .into(), + ), + 0, + ); + } + match remote(entry, expected, cx) { + Ok(fs) => { + let work = Box::pin(nocterm_ai::files::run(call.call.clone(), fs, redact)); + DispatchAction::Files(call, work) + } + Err(error) => DispatchAction::Respond(call, Err(error.to_string().into()), 0), + } +} + +/// Answers the file call `call` once its work ends, holding the chat's operation until then. +pub(in crate::thread) fn complete( + call: BridgeCall, + work: FileWork, + guard: OperationGuard, + cx: &mut Context, +) { + cx.spawn(async move |this, cx| { + let _guard = guard; + let result = work.await; + let _ = this.update(cx, |this, cx| this.on_tool_completion(call, result, cx)); + }) + .detach(); +} diff --git a/crates/agent-chat/src/thread/tool_runtime/presentation.rs b/crates/agent-chat/src/thread/tool_runtime/presentation.rs index 110c5baf..c4111f59 100644 --- a/crates/agent-chat/src/thread/tool_runtime/presentation.rs +++ b/crates/agent-chat/src/thread/tool_runtime/presentation.rs @@ -1,18 +1,29 @@ //! Terminal outcome framing receives an explicit redaction policy. use super::CommandSnapshot; +use nocterm_ai::tools::output::{CommandReport, TerminalReport}; pub(in crate::thread) fn text_payload( tail: nocterm_workspace_ports::TerminalText, redact: bool, ) -> (serde_json::Value, usize) { + let report = terminal_report(tail, redact); + let bytes = report.text.len(); + (serde_json::json!(report), bytes) +} +pub(in crate::thread) fn terminal_report( + tail: nocterm_workspace_ports::TerminalText, + redact: bool, +) -> TerminalReport { let text = if redact { nocterm_ai::redact::redact(&tail.text) } else { tail.text }; - let bytes = text.len(); - ( - serde_json::json!({"text":text,"first_line":tail.first_line,"next_line":tail.next_line,"cursor_semantics":"inclusive snapshot; replace overlapping lines","truncated":tail.truncated,"alt_screen":tail.alt_screen}), - bytes, + TerminalReport::new( + text, + tail.first_line, + tail.next_line, + tail.truncated, + tail.alt_screen, ) } pub(in crate::thread) fn command_payload( @@ -20,19 +31,12 @@ pub(in crate::thread) fn command_payload( state: CommandSnapshot, redact: bool, ) -> serde_json::Value { - let text = |text: String| { - if redact { - nocterm_ai::redact::redact(&text) - } else { - text - } - }; - serde_json::json!({ - "command_id": id, "state": state.state().as_str(), - "stdout": text(String::from_utf8_lossy(state.stdout()).into_owned()), - "stderr": text(state.exit().map(|exit| exit.stderr.clone()).unwrap_or_default()), - "exit_status": state.exit().and_then(|exit| exit.status), - "total_bytes": state.total_bytes(), "truncated": state.total_bytes() > state.stdout().len() as u64, - "error": state.error().map(nocterm_ai::redact::redact), - }) + serde_json::json!(CommandReport::new(id, &state, redact)) +} +/// The agent-facing report of one transfer. +pub(in crate::thread) fn transfer_payload( + progress: &nocterm_transfers::Progress, + redact: bool, +) -> serde_json::Value { + nocterm_ai::tools::transfer::report(progress, redact) } diff --git a/crates/agent-chat/src/thread/tool_runtime/source_execution.rs b/crates/agent-chat/src/thread/tool_runtime/source_execution.rs new file mode 100644 index 00000000..74426589 --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/source_execution.rs @@ -0,0 +1,68 @@ +//! A native runner retains its original account lease through every scheduler poll. +use futures::{FutureExt as _, future::BoxFuture}; +use nocterm_ai::{ExecCommand, ToolFailure, local_environment::LocalEnvironmentAccess}; +use nocterm_workspace_ports::{ExpectedSession, TerminalCapability, TerminalEntry, TerminalError}; +use std::{future::poll_fn, task::Poll, time::Duration}; + +pub(in crate::thread) struct SourceExecution { + source: LocalEnvironmentAccess, +} +impl SourceExecution { + pub(in crate::thread) fn capture( + runtime: &nocterm_agent_runtime::RuntimeHandle, + ) -> Result { + Ok(Self { + source: runtime + .captured_source() + .map_err(|error| ToolFailure::from(error.to_string()))?, + }) + } + pub(in crate::thread) fn start( + self, + entry: TerminalEntry, + expected: ExpectedSession, + request: &ExecCommand, + tools: &mut super::ToolRuntime, + cx: &mut gpui::App, + ) -> Result<(String, BoxFuture<'static, ()>), ToolFailure> { + self.source + .check_current() + .map_err(|error| ToolFailure::from(error.to_string()))?; + let executor = entry + .capabilities + .execution() + .ok_or(TerminalError::Unsupported( + TerminalCapability::StructuredExecution, + )) + .and_then(|port| port.executor(expected, cx)) + .map_err(|error| ToolFailure::from(error.to_string()))?; + let program = nocterm_session::ExecRequest { + program: request.program.clone(), + args: request.args.clone(), + stdin: request + .stdin + .as_ref() + .map(|stdin| stdin.as_bytes().to_vec()), + }; + let timer = cx + .background_executor() + .timer(Duration::from_millis(request.timeout_ms.unwrap_or(30_000))); + let (id, run) = tools + .start_command(request.terminal_id.clone(), executor, program, timer)? + .into_run(); + Ok((id, self.guard(run))) + } + /// Each native future poll is admitted against the original owner. Already + /// admitted transport work is cancelled by dropping the runner; no new poll + /// can consume credentials from a replacement account. The chat watchdog + /// records cancellation of the retained command separately. + fn guard(self, mut run: BoxFuture<'static, ()>) -> BoxFuture<'static, ()> { + poll_fn(move |cx| { + if self.source.check_current().is_err() { + return Poll::Ready(()); + } + run.as_mut().poll(cx) + }) + .boxed() + } +} diff --git a/crates/agent-chat/src/thread/tool_runtime/transfer_dispatch.rs b/crates/agent-chat/src/thread/tool_runtime/transfer_dispatch.rs new file mode 100644 index 00000000..cd50d683 --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/transfer_dispatch.rs @@ -0,0 +1,173 @@ +//! A transfer call is admitted against two terminals' exact sessions; their filesystems +//! are captured once, under those sessions, into the request the chat submits. +use super::transfers::{ChatTransfers, TransferBinding}; +use crate::thread::bound_call::BoundToolCall; +use crate::thread::tool_runtime::dispatch::DispatchAction; +use gpui::App; +use nocterm_ai::{BridgeCall, TerminalCall, ToolFailure}; +use nocterm_transfers::{CollisionPolicy, Request, TransferEndpoint, TransferId}; +use nocterm_workspace_ports::{ + ExpectedSession, TerminalCapability, TerminalEntry, TerminalError, TerminalFilesystem, +}; + +const DEFAULT_YIELD_MS: u64 = 1000; + +/// An approved `transfer_files` call whose destination is resolved; the source is not yet. +pub(in crate::thread) struct TransferAdmission { + call: BridgeCall, + destination: TerminalEntry, + expected: ExpectedSession, + source_expected: ExpectedSession, +} +/// The submitted transfer and the call waiting for its report. +pub(in crate::thread) struct StartedTransfer { + call: BridgeCall, + id: TransferId, + yield_ms: u64, +} +impl StartedTransfer { + pub(in crate::thread) fn into_parts(self) -> (BridgeCall, TransferId, u64) { + (self.call, self.id, self.yield_ms) + } +} +/// A transfer call that could not start, with the failure to answer it with. Boxed so the +/// `Err` side of the start path stays small. +pub(in crate::thread) struct RejectedTransfer { + call: BridgeCall, + error: ToolFailure, +} +impl RejectedTransfer { + fn new(call: BridgeCall, error: ToolFailure) -> Box { + Box::new(Self { call, error }) + } + pub(in crate::thread) fn into_parts(self) -> (BridgeCall, ToolFailure) { + (self.call, self.error) + } +} +struct PreparedTransfer { + call: BridgeCall, + request: Request, + binding: TransferBinding, + yield_ms: u64, +} + +impl TransferAdmission { + /// A call without a source session was approved against a terminal that is gone. + pub(in crate::thread) fn admit( + bound: BoundToolCall, + destination: TerminalEntry, + expected: ExpectedSession, + ) -> DispatchAction { + let Some(source_expected) = bound.source() else { + let stale = TerminalError::StaleSession.to_string().into(); + return DispatchAction::Respond(bound.into_call(), Err(stale), 0); + }; + DispatchAction::Transfer(Box::new(Self { + call: bound.into_call(), + destination, + expected, + source_expected, + })) + } + /// Whether the call reads from the terminal with this id. + pub(in crate::thread) fn reads_from(&self, id: &str) -> bool { + self.call.call.source_terminal_id() == Some(id) + } + /// Rechecks both sessions, captures both filesystems and builds the request. + fn prepare( + self, + source: Option, + cx: &App, + ) -> Result> { + match self.endpoints(source, cx) { + Ok((request, binding, yield_ms)) => Ok(PreparedTransfer { + call: self.call, + request, + binding, + yield_ms, + }), + Err(error) => Err(RejectedTransfer::new(self.call, error)), + } + } + fn endpoints( + &self, + source: Option, + cx: &App, + ) -> Result<(Request, TransferBinding, u64), ToolFailure> { + let TerminalCall::TransferFiles(call) = &self.call.call else { + return Err("This is not a file transfer.".into()); + }; + let source = source.ok_or_else(|| { + crate::thread::tools::unreachable( + "Terminal is not attached or was closed. Call list_terminals for the current ones.", + ) + })?; + let from = endpoint(&source, self.source_expected, cx)?; + let to = endpoint(&self.destination, self.expected, cx)?; + let collisions = if call.overwrite { + CollisionPolicy::Replace + } else { + CollisionPolicy::Skip + }; + let request = Request::between( + from, + call.sources.clone(), + to, + call.destination.clone(), + collisions, + ) + .map_err(|error| ToolFailure::from(error.to_string()))?; + let binding = TransferBinding::new( + (call.destination_terminal_id.clone(), self.expected), + (call.source_terminal_id.clone(), self.source_expected), + ); + Ok((request, binding, call.yield_ms.unwrap_or(DEFAULT_YIELD_MS))) + } +} +impl PreparedTransfer { + fn submit(self, book: &mut ChatTransfers) -> Result> { + match book.start(self.request, self.binding) { + Ok(id) => Ok(StartedTransfer { + call: self.call, + id, + yield_ms: self.yield_ms, + }), + Err(error) => Err(RejectedTransfer::new(self.call, error)), + } + } +} +/// Submits an admitted transfer to the chat's book. +pub(super) fn start( + book: &mut ChatTransfers, + admission: TransferAdmission, + source: Option, + cx: &App, +) -> Result> { + admission.prepare(source, cx)?.submit(book) +} + +fn endpoint( + entry: &TerminalEntry, + expected: ExpectedSession, + cx: &App, +) -> Result { + let current = entry + .capabilities + .state() + .info(cx) + .is_some_and(|info| info.expected == expected); + let files = if current { + entry + .capabilities + .files() + .ok_or(TerminalError::Unsupported(TerminalCapability::Files)) + .and_then(|port| port.filesystem(expected, cx)) + } else { + Err(TerminalError::StaleSession) + }; + match files { + Ok(TerminalFilesystem::Local) => Ok(TransferEndpoint::Local), + Ok(TerminalFilesystem::Remote { host, fs }) => Ok(TransferEndpoint::Remote { host, fs }), + Err(error) => Err(error.to_string().into()), + } +} diff --git a/crates/agent-chat/src/thread/tool_runtime/transfers.rs b/crates/agent-chat/src/thread/tool_runtime/transfers.rs new file mode 100644 index 00000000..83f12c8f --- /dev/null +++ b/crates/agent-chat/src/thread/tool_runtime/transfers.rs @@ -0,0 +1,174 @@ +//! The chat's file transfers: it reads and cancels only ids it submitted, and a transfer +//! ends when either endpoint terminal detaches or its session is replaced. +use nocterm_ai::ToolFailure; +use nocterm_transfers::{ + OwnedLimits, OwnedTransferError, OwnedTransfers, Progress, Request, TransferId, + TransferRequests, +}; +use nocterm_workspace_ports::ExpectedSession; +use std::sync::Arc; + +/// The terminals a transfer was admitted for and the sessions it captured from them. +pub(super) struct TransferBinding { + destination: (String, ExpectedSession), + source: (String, ExpectedSession), +} +impl TransferBinding { + pub(super) fn new( + destination: (String, ExpectedSession), + source: (String, ExpectedSession), + ) -> Self { + Self { + destination, + source, + } + } + fn holds(&self, current: &[(String, Option)]) -> bool { + [&self.destination, &self.source] + .into_iter() + .all(|(id, expected)| { + current + .iter() + .any(|(attached, session)| attached == id && *session == Some(*expected)) + }) + } +} + +pub(super) struct ChatTransfers { + book: OwnedTransfers, +} +impl ChatTransfers { + pub(super) fn new(port: Arc) -> Self { + Self { + book: OwnedTransfers::new(port, OwnedLimits::default()), + } + } + pub(super) fn start( + &mut self, + request: Request, + binding: TransferBinding, + ) -> Result { + self.book + .submit(request, binding) + .map_err(|error| error.to_string().into()) + } + pub(super) fn progress(&mut self, id: &str) -> Result { + let id = owned_id(id)?; + self.book + .progress(id) + .map_err(|error| error.to_string().into()) + } + pub(super) fn cancel(&mut self, id: &str) -> Result { + let id = owned_id(id)?; + self.book + .cancel(id) + .map_err(|error| error.to_string().into()) + } + /// `current`: every attached terminal id with its present session. + pub(super) fn revoke_replaced(&mut self, current: &[(String, Option)]) { + self.book.retain_active(|binding| binding.holds(current)); + } + pub(super) fn cancel_all(&mut self) { + self.book.cancel_all(); + } + pub(super) fn has_active(&mut self) -> bool { + self.book.has_active() + } +} +/// A malformed id cannot be one this chat submitted. +fn owned_id(text: &str) -> Result { + text.parse() + .map_err(|_| OwnedTransferError::NotOwned.to_string().into()) +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_session::{HostAddress, Target, testing::InMemoryFs}; + use nocterm_transfers::{ + CollisionPolicy, TransferEndpoint, TransferEvent, TransferState, testing::FakeTransfers, + }; + + fn host(name: &str) -> HostAddress { + HostAddress::direct(Target { + host: name.into(), + port: 22, + user: "user".into(), + }) + } + fn relay() -> Request { + let endpoint = |name: &str| TransferEndpoint::Remote { + host: host(name), + fs: Arc::new(InMemoryFs::default()), + }; + Request::between( + endpoint("source"), + vec!["/srv/a".into()], + endpoint("destination"), + "/srv/in".into(), + CollisionPolicy::Skip, + ) + .unwrap() + } + fn session(item: u64, epoch: u64) -> ExpectedSession { + ExpectedSession::new(gpui::EntityId::from(item), epoch) + } + fn binding(epoch: u64) -> TransferBinding { + TransferBinding::new( + ("destination".into(), session(1, epoch)), + ("source".into(), session(2, 0)), + ) + } + fn attached(destination_epoch: u64) -> Vec<(String, Option)> { + vec![ + ("destination".into(), Some(session(1, destination_epoch))), + ("source".into(), Some(session(2, 0))), + ] + } + + #[test] + fn another_chats_or_malformed_ids_are_not_owned() { + let fake = Arc::new(FakeTransfers::default()); + let mut other = ChatTransfers::new(fake.clone()); + let foreign = other.start(relay(), binding(0)).unwrap(); + let mut mine = ChatTransfers::new(fake); + let refused = mine.progress(&foreign.to_string()).unwrap_err(); + assert_eq!( + refused.to_string(), + OwnedTransferError::NotOwned.to_string() + ); + assert!(mine.cancel(&foreign.to_string()).is_err()); + assert!(mine.progress("not an id").is_err()); + assert!(other.progress(&foreign.to_string()).is_ok()); + } + + #[test] + fn a_replaced_or_detached_endpoint_cancels_only_its_transfer() { + let fake = Arc::new(FakeTransfers::default()); + let mut transfers = ChatTransfers::new(fake.clone()); + let kept = transfers.start(relay(), binding(0)).unwrap(); + let replaced = transfers.start(relay(), binding(1)).unwrap(); + transfers.revoke_replaced(&attached(0)); + let state = |transfers: &mut ChatTransfers, id: TransferId| { + transfers.progress(&id.to_string()).unwrap().state + }; + assert_eq!(state(&mut transfers, kept), TransferState::Queued); + assert_eq!(state(&mut transfers, replaced), TransferState::Cancelled); + transfers.revoke_replaced(&attached(0)[..1]); + assert_eq!(state(&mut transfers, kept), TransferState::Cancelled); + assert!(!transfers.has_active()); + } + + #[test] + fn finished_transfers_keep_their_final_report_after_the_runtime_forgets_them() { + let fake = Arc::new(FakeTransfers::default()); + let mut transfers = ChatTransfers::new(fake.clone()); + let id = transfers.start(relay(), binding(0)).unwrap(); + fake.advance(id, TransferEvent::DiscoveryComplete).unwrap(); + fake.advance(id, TransferEvent::WorkersDrained).unwrap(); + assert!(!transfers.has_active()); + fake.forget(id); + let report = transfers.progress(&id.to_string()).unwrap(); + assert_eq!(report.state, TransferState::Completed); + } +} diff --git a/crates/agent-chat/src/thread/tool_runtime/turn_context.rs b/crates/agent-chat/src/thread/tool_runtime/turn_context.rs index d341f61c..65bf1243 100644 --- a/crates/agent-chat/src/thread/tool_runtime/turn_context.rs +++ b/crates/agent-chat/src/thread/tool_runtime/turn_context.rs @@ -149,6 +149,7 @@ impl TurnContext { connection, cwd: info.cwd.map(|path| path.to_string_lossy().into_owned()), status: format!("{:?}", info.status), + file_tools: super::file_dispatch::takes_file_tools(&entry, info.expected, cx), }; Some((id, entry, descriptor)) }) @@ -231,23 +232,34 @@ impl TurnContext { }) .unwrap_or_else(|| "Unavailable server".into()); } - self.resolved(input, defaults, cx) - .into_iter() - .find(|(id, _, _)| Some(id.as_str()) == call.terminal_id()) - .map(|(_, entry, descriptor)| { - format!( - "{}{}", - entry.title, - descriptor - .connection - .as_ref() - .map(|connection| format!( - " · {}@{}:{}", - connection.user, connection.host, connection.port - )) - .unwrap_or_default() - ) - }) - .unwrap_or_else(|| "Unavailable terminal".into()) + let resolved = self.resolved(input, defaults, cx); + let describe = |wanted: Option<&str>| { + resolved + .iter() + .find(|(id, _, _)| Some(id.as_str()) == wanted) + .map(|(_, entry, descriptor)| { + format!( + "{}{}", + entry.title, + descriptor + .connection + .as_ref() + .map(|connection| format!( + " · {}@{}:{}", + connection.user, connection.host, connection.port + )) + .unwrap_or_default() + ) + }) + .unwrap_or_else(|| "Unavailable terminal".into()) + }; + match call.source_terminal_id() { + Some(source) => format!( + "{} → {}", + describe(Some(source)), + describe(call.terminal_id()) + ), + None => describe(call.terminal_id()), + } } } diff --git a/crates/agent-directory/Cargo.toml b/crates/agent-directory/Cargo.toml index f292161f..bcfdf4d3 100644 --- a/crates/agent-directory/Cargo.toml +++ b/crates/agent-directory/Cargo.toml @@ -15,6 +15,7 @@ nocterm-ai.workspace = true nocterm-store.workspace = true [dev-dependencies] +nocterm-hubs-model.workspace = true gpui = { workspace = true, features = ["test-support"] } [lints] diff --git a/crates/agent-directory/src/lib.rs b/crates/agent-directory/src/lib.rs index ad5eb1b9..72a72a29 100644 --- a/crates/agent-directory/src/lib.rs +++ b/crates/agent-directory/src/lib.rs @@ -1,31 +1,48 @@ //! Which agents the user can start now, and from which source. //! -//! Owns: `AgentsHandle`, the one resolution of the injected `AgentCatalog` -//! and the `ai` settings into `AvailableAgents` that every consumer reads. +//! Owns: `AgentsHandle`, the one resolution of the injected `AgentCatalog`, the `ai` settings +//! and the agents of the signed-in hubs ([`HubAgentLaunches`]) into `AvailableAgents` that +//! every consumer reads. //! Does not own: agent processes (`nocterm-agent-runtime`), profile and //! resolution rules (`nocterm-ai`), the built-in profiles (`nocterm-agent-providers`) or views. -//! Ports used: SettingsHandle. Ports provided: AgentsHandle. +//! Ports used: SettingsHandle, HubAgentLaunches. Ports provided: AgentsHandle. //! Settings section: reads `ai`. Globals: none. //! Invariants: consumers see the agents of the current settings revision; //! nothing outside this crate resolves settings into agents. use gpui::App; use nocterm_ai::{ AgentCatalog, AiSettings, AvailableAgents, - agents::{AgentProfile, AvailableAgent}, + agents::{AgentLaunch, AgentProfile, AvailableAgent}, }; use nocterm_store::SettingsHandle; +use std::rc::Rc; + +/// The agents of the hubs this device is signed in to, as they are now. +pub type HubAgentLaunches = Rc Vec>; /// Explicit access to the available agents, retained by the application. #[derive(Clone)] pub struct AgentsHandle { catalog: AgentCatalog, settings: SettingsHandle, + hubs: Option, } impl AgentsHandle { pub fn new(catalog: AgentCatalog, settings: SettingsHandle, cx: &mut App) -> Self { settings.register::(cx); - Self { catalog, settings } + Self { + catalog, + settings, + hubs: None, + } + } + + /// These agents and the agents of the signed-in hubs. + #[must_use] + pub fn with_hub_agents(mut self, hubs: HubAgentLaunches) -> Self { + self.hubs = Some(hubs); + self } /// The profiles nocterm ships, whether or not settings enable them. @@ -35,7 +52,12 @@ impl AgentsHandle { /// The agents the current settings allow, with skipped configurations. pub fn available(&self, cx: &App) -> AvailableAgents { - AvailableAgents::resolve(&self.catalog, self.settings.setting::(cx)) + let settings = self.settings.setting::(cx); + let agents = AvailableAgents::resolve(&self.catalog, settings); + match &self.hubs { + Some(hubs) if settings.enabled => agents.with_hub_agents(hubs(cx)), + _ => agents, + } } pub fn get(&self, id: &str, cx: &App) -> Option { @@ -44,7 +66,13 @@ impl AgentsHandle { /// The profile of agent `id`: its own when available, else the catalog's. pub fn profile(&self, id: &str, cx: &App) -> Option { - match self.available(cx).get(id) { + self.profile_in(&self.available(cx), id) + } + + /// The profile of agent `id` among agents already resolved by [`Self::available`], + /// for callers that look up many agents at once. + pub fn profile_in(&self, available: &AvailableAgents, id: &str) -> Option { + match available.get(id) { Some(agent) => agent.profile().copied(), None => self.catalog.get(id).copied(), } diff --git a/crates/agent-directory/src/tests.rs b/crates/agent-directory/src/tests.rs index a2130d57..c8eb4e06 100644 --- a/crates/agent-directory/src/tests.rs +++ b/crates/agent-directory/src/tests.rs @@ -58,3 +58,16 @@ async fn available_agents_follow_the_current_settings(cx: &mut TestAppContext) { assert_eq!(agents.catalog().iter().count(), 1); }); } + +#[gpui::test] +async fn the_agents_of_signed_in_hubs_are_available_beside_the_local_ones(cx: &mut TestAppContext) { + use nocterm_hubs_model::{HubAddress, HubId}; + let hub = HubId::from(HubAddress::parse("hub.example:7443").unwrap()); + let launch = nocterm_ai::agents::AgentLaunch::hub_agent(hub, "a1".into(), "Ops".into()); + let id = launch.id.clone(); + let agents = handle(cx).with_hub_agents(std::rc::Rc::new(move |_| vec![launch.clone()])); + cx.update(|cx| { + assert_eq!(agents.get(&id, cx).unwrap().source(), AgentSource::Hub); + assert!(agents.get("alpha", cx).is_some()); + }); +} diff --git a/crates/agent-providers/src/claude.rs b/crates/agent-providers/src/claude.rs index 8a623ff4..6e5589a0 100644 --- a/crates/agent-providers/src/claude.rs +++ b/crates/agent-providers/src/claude.rs @@ -1,7 +1,5 @@ //! Claude Code through Zed's `claude-agent-acp` adapter. -use nocterm_ai::agents::{ - AccountUsage, AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile, -}; +use nocterm_ai::agents::{AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; pub(crate) const PROFILE: AgentProfile = AgentProfile::new( "claude", @@ -19,13 +17,10 @@ pub(crate) const PROFILE: AgentProfile = AgentProfile::new( .recognized_by(&["claude-agent-acp"]) .writes_home(&[".claude", ".claude.json", ".config/claude"]) .with_usage( - UsageProfile::new( - UsageMeta::RateLimitRecord { - key: "_claude/rateLimit", - }, - AccountUsage::OAuthAccount, - ) - .with_empty_hint("Claude plan limits load from your Claude Code sign-in."), + UsageProfile::new(UsageMeta::RateLimitRecord { + key: "_claude/rateLimit", + }) + .with_empty_hint("Claude has not reported plan limits."), ) .with_mark( AgentMark::new( diff --git a/crates/agent-providers/src/codex.rs b/crates/agent-providers/src/codex.rs index d67c2873..abde7d92 100644 --- a/crates/agent-providers/src/codex.rs +++ b/crates/agent-providers/src/codex.rs @@ -1,7 +1,5 @@ //! OpenAI Codex through the `codex-acp` adapter. -use nocterm_ai::agents::{ - AccountUsage, AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile, -}; +use nocterm_ai::agents::{AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; pub(crate) const PROFILE: AgentProfile = AgentProfile::new( "codex", @@ -15,8 +13,7 @@ pub(crate) const PROFILE: AgentProfile = AgentProfile::new( .recognized_by(&["codex"]) .writes_home(&[".codex"]) .with_usage( - UsageProfile::new(UsageMeta::None, AccountUsage::SessionLog) - .with_empty_hint("Codex reports plan limits after a reply."), + UsageProfile::new(UsageMeta::None).with_empty_hint("Codex has not reported plan limits."), ) .with_mark(AgentMark::new( "icons/agents/codex.svg", diff --git a/crates/agent-providers/src/lib.rs b/crates/agent-providers/src/lib.rs index e8edb566..87456c80 100644 --- a/crates/agent-providers/src/lib.rs +++ b/crates/agent-providers/src/lib.rs @@ -3,7 +3,7 @@ //! Owns: one module per built-in agent returning its `AgentProfile` (pinned //! launch, home state, plan-limit reporting, brand mark) and `catalog()`. //! Does not own: dialect and resolution rules (`nocterm-ai`), launching -//! (`nocterm-acp`), native usage reads (`nocterm-agent-usage`) or rendering. +//! (`nocterm-acp`), provider credentials or rendering. //! Ports used: none. Ports provided: none; the application injects `catalog()`. //! Settings section: none. Globals: none. //! Invariants: ids are unique and valid; launches are pinned; every profile diff --git a/crates/agent-runtime/Cargo.toml b/crates/agent-runtime/Cargo.toml index d297a77b..1c89db62 100644 --- a/crates/agent-runtime/Cargo.toml +++ b/crates/agent-runtime/Cargo.toml @@ -13,7 +13,10 @@ layer = "service" test-support = [] [dependencies] +nocterm-hubs-model.workspace = true nocterm-ai.workspace = true +nocterm-session.workspace = true +nocterm-transfers.workspace = true nocterm-agent-directory.workspace = true nocterm-core.workspace = true nocterm-store.workspace = true @@ -24,7 +27,10 @@ tracing.workspace = true thiserror.workspace = true [dev-dependencies] +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } nocterm-agent-providers.workspace = true +nocterm-transfers = { workspace = true, features = ["test-support"] } serde_json.workspace = true gpui = { workspace = true, features = ["test-support"] } tempfile.workspace = true diff --git a/crates/agent-runtime/src/bridge.rs b/crates/agent-runtime/src/bridge.rs index e5ac7fb7..d2b008b7 100644 --- a/crates/agent-runtime/src/bridge.rs +++ b/crates/agent-runtime/src/bridge.rs @@ -61,7 +61,13 @@ impl BridgeRegistry { _ => unreachable!(), }; let client = runtime - .read_with(cx, |runtime, _| runtime.bridge.target(registration)) + .read_with(cx, |runtime, cx| { + runtime.bridge.target(registration).filter(|client| { + client.state(cx).is_some_and(|state| { + runtime.chats.source_current(state.origin().key()) + }) + }) + }) .ok() .flatten(); if let Some(client) = client { diff --git a/crates/agent-runtime/src/catalog.rs b/crates/agent-runtime/src/catalog.rs index 77f7d1a5..db338793 100644 --- a/crates/agent-runtime/src/catalog.rs +++ b/crates/agent-runtime/src/catalog.rs @@ -1,15 +1,48 @@ -//! Take-once history and bounded construction seeds, without filesystem access. +//! Installation-wide catalog payload reservations and immutable scoped snapshots. use super::{AgentStorage, HistoryReader, Runtime}; +use crate::history_admission::{HistoryAdmissionError, HistoryAdmissionHandle, HistoryBytesPermit}; use gpui::{Context, Task}; use nocterm_ai::{ - history::{ChatSummary, HistoryGate, SavedChat}, - storage::{AgentStorageError, ChatRepository}, + execution_origin::ChatKey, + history::{ChatSummary, ContextId, HistoryGate, SavedChat}, + local_environment::ChatScope, + storage::{AgentStorageError, ChatRepository, require_local_scope}, }; use std::sync::Arc; +mod allocation; +pub(crate) use allocation::CatalogAllocation; +#[cfg(test)] +pub(crate) use allocation::WORK_BYTES as CATALOG_WORK_BYTES; +mod prepared; +mod reclamation; +use prepared::PreparedRecords; + +#[derive(Clone, Debug, thiserror::Error)] +pub enum CatalogLoadError { + #[error("The original account source is unavailable: {0}")] + AccountSource(#[from] nocterm_hubs_model::source_identity::LocalAccountSourceError), + #[error("All normal installation history payload slots are retained or loading.")] + Capacity, + #[error( + "Catalog metadata needs {incoming} bytes with {retained} retained; installation limit is {limit}." + )] + ResidentCapacity { + retained: usize, + incoming: usize, + limit: usize, + }, + #[error("Catalog metadata admission failed: {0}")] + Admission(#[source] Arc), + #[error(transparent)] + Storage(#[from] AgentStorageError), + #[error("Agent history is shutting down.")] + Closed, + #[error("Agent history load generation is exhausted.")] + GenerationExhausted, +} /// Optional prepared history for hosts that already have bounded decoded records. /// Production leaves this absent and loads the lightweight catalog lazily. -#[derive(Clone)] pub struct PreparedHistory(Vec); impl PreparedHistory { pub fn new(chats: Vec) -> Result { @@ -20,13 +53,18 @@ impl PreparedHistory { let mut ids = std::collections::HashSet::new(); let mut prepared = Vec::with_capacity(chats.len()); for chat in chats { - if !ids.insert(chat.id.clone()) { + require_local_scope(&ContextId::General.into(), &chat.header.chat_scope())?; + if !ids.insert(chat.header.id.clone()) { return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); } - let chat = nocterm_ai::history::prepare(chat)?; + let chat = nocterm_ai::history::prepare_owned(chat)?; resident = resident.saturating_add(chat.resident_bytes()); - if resident > nocterm_ai::history::MAX_RESIDENT_BYTES { - return Err(nocterm_ai::history::HistoryCodecError::StructuralLimit.into()); + if resident > crate::history_admission::MAX_NORMAL_HISTORY_BYTES { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: resident, + limit: crate::history_admission::MAX_NORMAL_HISTORY_BYTES, + }); } prepared.push(chat); } @@ -36,67 +74,384 @@ impl PreparedHistory { &self.0 } } +/// Immutable light metadata for one captured context and load generation. +#[derive(Clone)] +pub struct CatalogSnapshot { + payload: Arc, +} +struct CatalogPayload { + scope: ChatScope, + generation: u64, + rows: Vec, + error: Option, + allocation: CatalogAllocation, +} +impl CatalogSnapshot { + pub fn scope(&self) -> ContextId { + self.payload.scope.context() + } + pub fn chat_scope(&self) -> &ChatScope { + &self.payload.scope + } + pub fn generation(&self) -> u64 { + self.payload.generation + } + pub fn rows(&self) -> &[ChatSummary] { + &self.payload.rows + } + pub fn error(&self) -> Option<&AgentStorageError> { + self.payload.error.as_ref() + } + pub fn row(&self, index: usize) -> Option { + (index < self.rows().len()).then(|| CatalogRow { + snapshot: self.clone(), + index, + }) + } +} +/// A row retains its original accounted payload; no metadata strings are copied. +#[derive(Clone)] +pub struct CatalogRow { + snapshot: CatalogSnapshot, + index: usize, +} +impl CatalogRow { + pub fn scope(&self) -> ContextId { + self.snapshot.scope() + } + pub fn generation(&self) -> u64 { + self.snapshot.generation() + } + pub fn chat_scope(&self) -> &ChatScope { + self.snapshot.chat_scope() + } + pub fn summary(&self) -> &ChatSummary { + // Only CatalogSnapshot::row constructs this immutable validated index. + &self.snapshot.rows()[self.index] + } +} +#[derive(Default)] +struct ScopeCatalog { + snapshot: Option, + generation: u64, + loading: Option>, +} pub(super) struct Catalog { - saved: Option>, - rows: Option>, + saved: PreparedRecords, + scopes: std::collections::HashMap, gate: HistoryGate, - loading: Option>, repository: Arc, closing: bool, + admission: HistoryAdmissionHandle, + sequence: u64, + _reclamation: crate::history_admission::HistoryRegistration, } impl Catalog { - pub(super) fn new(storage: &mut AgentStorage, cx: &mut Context) -> Self { + pub(super) fn new( + storage: &mut AgentStorage, + admission: HistoryAdmissionHandle, + prepared: Option, + cx: &mut Context, + ) -> Self { + let reclamation = admission + .register(std::rc::Rc::new(reclamation::CatalogReclamation::new( + cx.weak_entity(), + ))) + .expect("a new installation admission is open"); let mut catalog = Self { - saved: storage.take_history().map(|history| history.0), - rows: None, + saved: PreparedRecords::new( + storage.take_history().map(|history| history.0), + &admission, + prepared, + cx.entity_id(), + ), + scopes: Default::default(), gate: HistoryGate::default(), - loading: None, repository: storage.chats(), closing: false, + admission, + sequence: 0, + _reclamation: reclamation, }; - if catalog.saved.is_none() { - catalog.reload(cx) + if catalog.saved.prepared_count().is_some() { + if let Err(error) = catalog.install_prepared(cx) { + tracing::warn!(%error, "could not admit prepared history metadata"); + } + } else { + let _ = catalog.reload(ContextId::General.into(), cx); } catalog } - pub(super) fn reload(&mut self, cx: &mut Context) { + fn reserve( + &mut self, + scope: ChatScope, + bytes: usize, + cx: &mut Context, + ) -> Result<(u64, CatalogAllocation), CatalogLoadError> { if self.closing { - return; + return Err(CatalogLoadError::Closed); } + let generation = self + .sequence + .checked_add(1) + .ok_or(CatalogLoadError::GenerationExhausted)?; + let permit = match self + .admission + .reserve_payload_reclaiming(Some(cx.entity_id()), cx) + { + Ok(permit) => permit, + Err(HistoryAdmissionError::Closed) => return Err(CatalogLoadError::Closed), + Err(_) => { + let victim = self + .reclaimable_scope(Some(&scope)) + .ok_or(CatalogLoadError::Capacity)?; + self.scopes.remove(&victim); + self.admission + .reserve_payload() + .map_err(|error| match error { + HistoryAdmissionError::Closed => CatalogLoadError::Closed, + _ => CatalogLoadError::Capacity, + })? + } + }; + let bytes = self.reserve_metadata(&scope, bytes, cx)?; + self.sequence = generation; + Ok((generation, CatalogAllocation::new(permit, bytes))) + } + fn reserve_metadata( + &mut self, + scope: &ChatScope, + bytes: usize, + cx: &mut Context, + ) -> Result { + loop { + match self + .admission + .reserve_entity_bytes(cx.entity_id(), bytes, cx) + { + Ok(permit) => return Ok(permit), + Err(error @ HistoryAdmissionError::ResidentCapacity { .. }) => { + // The borrowed Runtime was excluded from callbacks. Its + // own unpinned cached scopes can be reclaimed directly. + let Some(victim) = self.reclaimable_scope(Some(scope)) else { + return Err(catalog_admission_error(error)); + }; + self.scopes.remove(&victim); + } + Err(error) => return Err(catalog_admission_error(error)), + } + } + } + fn reclaimable_scope(&self, excluding: Option<&ChatScope>) -> Option { + self.scopes + .iter() + .filter(|(scope, entry)| excluding != Some(*scope) && entry.loading.is_none()) + .filter_map(|(scope, entry)| { + entry + .snapshot + .as_ref() + .filter(|snapshot| Arc::strong_count(&snapshot.payload) == 1) + .map(|snapshot| (scope, snapshot.payload.allocation.order())) + }) + .min_by_key(|(_, order)| *order) + .map(|(scope, _)| scope.clone()) + } + fn install_prepared(&mut self, cx: &mut Context) -> Result<(), CatalogLoadError> { + let count = self.saved.prepared_count().unwrap_or(0); + let bytes = (count + 1) + .saturating_mul(allocation::MAX_ROW_BYTES) + .saturating_add(1024 * 1024); + let scope = ContextId::General.into(); + let (generation, allocation) = self.reserve(scope, bytes, cx)?; + let rows = CatalogAllocation::check(Ok(self.saved.rows().unwrap_or_default()))?; + let allocation = allocation + .finish(&self.admission, &rows, rows.capacity()) + .map_err(catalog_admission_error)?; + let payload = Arc::new(CatalogPayload { + scope: ContextId::General.into(), + generation, + rows, + error: None, + allocation, + }); + self.scopes.insert( + ContextId::General.into(), + ScopeCatalog { + snapshot: Some(CatalogSnapshot { payload }), + generation, + loading: None, + }, + ); + Ok(()) + } + pub(super) fn ensure( + &mut self, + scope: ChatScope, + cx: &mut Context, + ) -> Result<(), CatalogLoadError> { + if self.closing { + return Err(CatalogLoadError::Closed); + } + if self.scopes.contains_key(&scope) { + return Ok(()); + } + if scope == ContextId::General.into() && self.saved.prepared_count().is_some() { + return self.install_prepared(cx); + } + self.reload(scope, cx) + } + pub(super) fn reload( + &mut self, + scope: ChatScope, + cx: &mut Context, + ) -> Result<(), CatalogLoadError> { + let (generation, allocation) = self.reserve(scope.clone(), allocation::WORK_BYTES, cx)?; + let entry = self.scopes.entry(scope.clone()).or_default(); + entry.generation = generation; let repository = self.repository.clone(); - let loading = cx - .background_executor() - .spawn(async move { repository.catalog().await }); - self.loading = Some(cx.spawn(async move |runtime, cx| { - let rows = loading.await; + let captured = scope.clone(); + let (complete, loading) = futures::channel::oneshot::channel(); + cx.background_executor() + .spawn(async move { + let rows = CatalogAllocation::check(repository.catalog_in(captured).await); + // The detached bounded read owns its allocation independently of a + // cancelled viewer. An abandoned result drops bytes and proof together. + let _ = complete.send((rows, allocation)); + }) + .detach(); + entry.loading = Some(cx.spawn(async move |runtime, cx| { + let Ok((rows, allocation)) = loading.await else { + return; + }; let _ = runtime.update(cx, |runtime, cx| { - runtime.chats.loaded(rows); + runtime.chats.loaded(scope, generation, rows, allocation); cx.notify(); }); })); + Ok(()) } - pub(super) fn complete(&mut self, rows: Result, AgentStorageError>) { + pub(super) fn complete( + &mut self, + scope: ChatScope, + generation: u64, + rows: Result, AgentStorageError>, + allocation: CatalogAllocation, + ) { if self.closing { return; } - self.rows = Some(rows.unwrap_or_else(|error| { - tracing::warn!(%error,"could not load agent catalog"); - Vec::new() - })); - self.loading.take(); + let Some(entry) = self.scopes.get_mut(&scope) else { + return; + }; + if entry.generation != generation { + return; + } + let (mut rows, error): (Vec, _) = match rows { + Ok(mut rows) => { + rows.retain(|row| { + require_local_scope(&scope, &row.chat_scope()).is_ok() && row.validate().is_ok() + }); + rows.truncate(nocterm_ai::history::MAX_RESTORED); + (rows, None) + } + Err(error) => { + tracing::warn!(%error, "could not load agent catalog"); + (Vec::new(), Some(error)) + } + }; + if scope == ContextId::General.into() + && let Some(prepared) = self.saved.rows() + { + for row in prepared { + rows.retain(|known| known.id != row.id); + rows.push(row); + } + rows.sort_by_key(|row| std::cmp::Reverse((row.pinned, row.updated))); + rows.truncate(nocterm_ai::history::MAX_RESTORED); + } + let allocation = match allocation.finish(&self.admission, &rows, rows.capacity()) { + Ok(allocation) => allocation, + Err(error) => { + entry.loading.take(); + tracing::warn!(%error, "could not retain catalog metadata"); + return; + } + }; + let payload = Arc::new(CatalogPayload { + scope, + generation, + rows, + error, + allocation, + }); + entry.snapshot = Some(CatalogSnapshot { payload }); + entry.loading.take(); + } + pub(super) fn invalidate(&mut self, scope: ChatScope) { + self.scopes.remove(&scope); } - pub(super) fn take_saved(&mut self) -> Option> { - self.saved.take() + pub(super) fn snapshot(&self, scope: ChatScope) -> Option { + self.scopes.get(&scope)?.snapshot.clone() + } + pub(super) fn oldest_payload(&self) -> Option { + let scope = self.reclaimable_scope(None)?; + Some( + self.scopes + .get(&scope)? + .snapshot + .as_ref()? + .payload + .allocation + .order(), + ) + } + pub(super) fn evict_payload(&mut self) -> bool { + let Some(scope) = self.reclaimable_scope(None) else { + return false; + }; + self.scopes.remove(&scope).is_some() } - pub(super) fn take_rows(&mut self) -> Option> { - self.rows.take() + pub(super) fn take_prepared( + &mut self, + key: &ChatKey, + payload: crate::history_admission::HistoryPayloadPermit, + cx: &mut gpui::App, + ) -> Result>, AgentStorageError> { + self.saved.take(key, payload, cx) } - pub(super) fn reader(&self) -> HistoryReader { - HistoryReader::new(self.repository.clone(), self.gate.clone()) + pub(super) fn discard_prepared(&mut self, key: &ChatKey) { + self.saved.discard(key); + } + + pub(super) fn reader(&self, scope: ChatScope) -> HistoryReader { + HistoryReader::new(self.repository.clone(), self.gate.clone(), scope) + } + pub(super) fn unavailable_reader(&self) -> HistoryReader { + HistoryReader::unavailable(self.repository.clone(), self.gate.clone()) } pub(super) fn stop(&mut self) { self.closing = true; - self.loading.take(); + for entry in self.scopes.values_mut() { + entry.loading.take(); + } + } +} + +fn catalog_admission_error(error: HistoryAdmissionError) -> CatalogLoadError { + match error { + HistoryAdmissionError::Closed => CatalogLoadError::Closed, + HistoryAdmissionError::PayloadCapacity => CatalogLoadError::Capacity, + HistoryAdmissionError::ResidentCapacity { + retained, + incoming, + limit, + } => CatalogLoadError::ResidentCapacity { + retained, + incoming, + limit, + }, + HistoryAdmissionError::Storage(error) => CatalogLoadError::Storage(error), + error => CatalogLoadError::Admission(Arc::new(error)), } } @@ -147,7 +502,10 @@ mod tests { .collect(); assert!(matches!( PreparedHistory::new(chats), - Err(AgentStorageError::Codec(HistoryCodecError::StructuralLimit)) + Err(AgentStorageError::ResidentCapacity { + limit: crate::history_admission::MAX_NORMAL_HISTORY_BYTES, + .. + }) )); } #[test] diff --git a/crates/agent-runtime/src/catalog/allocation.rs b/crates/agent-runtime/src/catalog/allocation.rs new file mode 100644 index 00000000..360b20d9 --- /dev/null +++ b/crates/agent-runtime/src/catalog/allocation.rs @@ -0,0 +1,66 @@ +//! Pending scans and retained metadata share the installation's byte allocation. +use crate::history_admission::{ + HistoryAdmissionError, HistoryAdmissionHandle, HistoryBytesPermit, HistoryPayloadPermit, +}; +use nocterm_ai::{ + history::{ChatSummary, HistoryCodecError, MAX_RESTORED}, + storage::AgentStorageError, +}; + +pub(super) const MAX_ROW_BYTES: usize = 32 * 1024; +// At most200 returned rows,200 prepared metadata rows and one native scan +// candidate coexist. OneMiB also covers vector reallocation and assembly state. +pub(crate) const WORK_BYTES: usize = (2 * MAX_RESTORED + 1) * MAX_ROW_BYTES + 1024 * 1024; + +pub(crate) struct CatalogAllocation { + slot: HistoryPayloadPermit, + bytes: HistoryBytesPermit, +} +impl CatalogAllocation { + pub(super) fn new(slot: HistoryPayloadPermit, bytes: HistoryBytesPermit) -> Self { + Self { slot, bytes } + } + pub(super) fn order(&self) -> u64 { + self.slot.order() + } + pub(super) fn finish( + self, + admission: &HistoryAdmissionHandle, + rows: &[ChatSummary], + capacity: usize, + ) -> Result { + let retained = rows.iter().fold( + 4096usize.saturating_add(capacity.saturating_mul(std::mem::size_of::())), + |total, row| total.saturating_add(row_bytes(row)), + ); + Ok(Self { + slot: self.slot, + bytes: admission.shrink_buffer(self.bytes, retained)?, + }) + } + pub(super) fn check( + rows: Result, AgentStorageError>, + ) -> Result, AgentStorageError> { + let rows = rows?; + if rows.len() > MAX_RESTORED { + return Err(HistoryCodecError::CollectionLimit.into()); + } + if rows.iter().any(|row| row_bytes(row) > MAX_ROW_BYTES) { + return Err(HistoryCodecError::StructuralLimit.into()); + } + Ok(rows) + } +} +fn row_bytes(row: &ChatSummary) -> usize { + // Fixed storage covers inline IDs/epochs/fingerprints and the captured + // environment's bounded namespace strings (2*128B or a host<=253B). + // Actual string capacity counts the owned allocations, including decoder + // spare capacity. Serializing fixed fingerprint bytes as JSON nodes would + // overstate this metadata's memory rather than measure its retained owners. + 4096usize + .saturating_add(row.id.capacity()) + .saturating_add(row.agent_id.capacity()) + .saturating_add(row.title.as_ref().map_or(0, String::capacity)) + .saturating_add(row.name.as_ref().map_or(0, String::capacity)) + .saturating_add(row.model.as_ref().map_or(0, String::capacity)) +} diff --git a/crates/agent-runtime/src/catalog/prepared.rs b/crates/agent-runtime/src/catalog/prepared.rs new file mode 100644 index 00000000..d40851a1 --- /dev/null +++ b/crates/agent-runtime/src/catalog/prepared.rs @@ -0,0 +1,132 @@ +//! Prepared local bodies remain accounted before any chat entity owns them. +use crate::{ + HistoryUsage, + history_admission::{ + HistoryAdmissionHandle, HistoryParticipant, HistoryPayloadPermit, HistoryRegistration, + }, +}; +use gpui::App; +use nocterm_ai::{ + execution_origin::ChatKey, + history::{ChatSummary, SavedChat}, + storage::AgentStorageError, +}; +use std::{ + cell::RefCell, + rc::{Rc, Weak}, +}; + +pub(super) struct PreparedRecords { + records: Rc>, + registration: HistoryRegistration, + allocations: crate::history_admission::LocalHistoryAllocations, +} +struct PreparedBatch { + values: Option>, + allocation: Option, +} +struct PreparedUsage(Weak>); +impl HistoryParticipant for PreparedUsage { + fn usage(&self, _cx: &App) -> Result, AgentStorageError> { + Ok(self.0.upgrade().map(|records| { + HistoryUsage::new( + records.borrow().values.as_ref().map_or(0, |values| { + values.iter().map(SavedChat::resident_bytes).sum() + }), + 0, + true, + ) + })) + } +} +impl PreparedRecords { + pub(super) fn prepared_count(&self) -> Option { + self.records.borrow().values.as_ref().map(Vec::len) + } + pub(super) fn new( + records: Option>, + admission: &HistoryAdmissionHandle, + allocation: Option, + runtime: gpui::EntityId, + ) -> Self { + let records = Rc::new(RefCell::new(PreparedBatch { + values: records, + allocation, + })); + let registration = admission + .register(Rc::new(PreparedUsage(Rc::downgrade(&records)))) + .expect("a new installation admission is open"); + Self { + records, + registration, + allocations: crate::history_admission::LocalHistoryAllocations::new( + admission.clone(), + runtime, + ), + } + } + pub(super) fn rows(&self) -> Option> { + self.records + .borrow() + .values + .as_ref() + .map(|records| records.iter().map(ChatSummary::from_chat).collect()) + } + pub(super) fn take( + &self, + key: &ChatKey, + payload: HistoryPayloadPermit, + cx: &mut App, + ) -> Result>, AgentStorageError> { + let values = self.records.borrow(); + let Some(records) = &values.values else { + return Ok(None); + }; + let Some(index) = records.iter().position(|chat| chat.header.id == key.id()) else { + return Ok(None); + }; + let bytes = records[index].resident_bytes(); + let remaining = records + .iter() + .enumerate() + .filter(|(position, _)| *position != index) + .fold(0usize, |total, (_, chat)| { + total.saturating_add(chat.resident_bytes()) + }); + drop(values); + // Admit before removing the source body. No callbacks/await occur between + // reserving this immutable destination and moving that exact owned body. + let allocation = self.allocations.transfer_prepared( + &self.registration, + remaining, + bytes, + payload, + cx, + )?; + let mut batch = self.records.borrow_mut(); + let Some(records) = batch.values.as_mut() else { + return Err(AgentStorageError::Interrupted); + }; + let Some(index) = records.iter().position(|chat| chat.header.id == key.id()) else { + return Err(AgentStorageError::Interrupted); + }; + let chat = records.remove(index); + if records.is_empty() { + batch.allocation.take(); + } + Ok(Some(nocterm_core::allocation::Allocated::new( + chat, allocation, + ))) + } + pub(super) fn discard(&self, key: &ChatKey) { + let mut batch = self.records.borrow_mut(); + if let Some(records) = &mut batch.values { + if let Some(index) = records.iter().position(|chat| chat.header.id == key.id()) { + drop(records.remove(index)); + } + if records.is_empty() { + batch.allocation.take(); + } + } + } +} diff --git a/crates/agent-runtime/src/catalog/reclamation.rs b/crates/agent-runtime/src/catalog/reclamation.rs new file mode 100644 index 00000000..5921aafb --- /dev/null +++ b/crates/agent-runtime/src/catalog/reclamation.rs @@ -0,0 +1,35 @@ +//! Weak catalog reclamation participates in the installation's shared payload cap. +use crate::{HistoryUsage, Runtime, history_admission::HistoryParticipant}; +use gpui::{App, EntityId, WeakEntity}; +use nocterm_ai::storage::AgentStorageError; + +pub(super) struct CatalogReclamation(WeakEntity); +impl CatalogReclamation { + pub(super) fn new(runtime: WeakEntity) -> Self { + Self(runtime) + } +} +impl HistoryParticipant for CatalogReclamation { + fn entity_id(&self) -> Option { + Some(self.0.entity_id()) + } + fn usage(&self, _cx: &App) -> Result, AgentStorageError> { + Ok(None) + } + fn oldest_payload(&self, cx: &App) -> Option { + self.0 + .upgrade() + .and_then(|runtime| runtime.read(cx).chats.oldest_catalog_payload()) + } + fn evict_payload(&self, cx: &mut App) -> bool { + self.0.upgrade().is_some_and(|runtime| { + runtime.update(cx, |runtime, cx| { + let evicted = runtime.chats.evict_catalog_payload(); + if evicted { + cx.notify(); + } + evicted + }) + }) + } +} diff --git a/crates/agent-runtime/src/client.rs b/crates/agent-runtime/src/client.rs index f3030c06..c2234f36 100644 --- a/crates/agent-runtime/src/client.rs +++ b/crates/agent-runtime/src/client.rs @@ -75,34 +75,119 @@ pub enum SessionEvent { /// What the runtime reads about a chat to schedule its connection. pub struct ClientState { - pub chat_id: String, - /// Restored chat directory, resolved before process and sandbox launch. - pub workdir: Option, - pub agent_id: String, - pub session: Option, - /// Whether the session is doing work that closing it would interrupt. - pub busy: bool, - /// Whether the chat holds a connection. - pub leased: bool, - /// Whether the chat waits for a connection. - pub activation_pending: bool, - /// Whether the chat's previous session is still closing. - pub closing: bool, - /// Whether a panel shows the chat; its idle connection is kept. - pub shown: bool, + origin: nocterm_ai::execution_origin::ExecutionOrigin, + agent_id: String, + session: Option, + busy: bool, + leased: bool, + activation_pending: bool, + closing: bool, + shown: bool, +} +impl ClientState { + pub fn new( + origin: nocterm_ai::execution_origin::ExecutionOrigin, + agent_id: String, + session: Option, + busy: bool, + leased: bool, + activation_pending: bool, + closing: bool, + shown: bool, + ) -> Self { + Self { + origin, + agent_id, + session, + busy, + leased, + activation_pending, + closing, + shown, + } + } + pub fn origin(&self) -> &nocterm_ai::execution_origin::ExecutionOrigin { + &self.origin + } + pub fn agent_id(&self) -> &str { + &self.agent_id + } + pub fn session(&self) -> &Option { + &self.session + } + pub fn busy(&self) -> bool { + self.busy + } + pub fn leased(&self) -> bool { + self.leased + } + pub fn activation_pending(&self) -> bool { + self.activation_pending + } + pub fn closing(&self) -> bool { + self.closing + } + pub fn shown(&self) -> bool { + self.shown + } } +/// Immutable resident payload observation; eviction safety remains with the chat. +#[derive(Clone, Copy)] +pub struct HistoryUsage { + bytes: usize, + updated: u64, + shown: bool, +} +impl HistoryUsage { + pub fn new(bytes: usize, updated: u64, shown: bool) -> Self { + Self { + bytes, + updated, + shown, + } + } + pub(crate) fn bytes(self) -> usize { + self.bytes + } + pub(crate) fn updated(self) -> u64 { + self.updated + } + pub(crate) fn shown(self) -> bool { + self.shown + } +} /// A chat as the runtime sees it. Calls on a closed chat do nothing. pub trait SessionClient { fn id(&self) -> EntityId; + /// The chat itself, so that another window can list a chat this one holds. + fn chat(&self) -> Option { + None + } /// Whether the chat is still open. fn alive(&self) -> bool; fn emit(&self, event: SessionEvent, cx: &mut App); fn state(&self, cx: &App) -> Option; + /// Resident history only; callers must exclude a currently borrowed requester. + fn history_usage(&self, cx: &App) -> Option; + fn history_payload(&self, _cx: &App) -> Option { + None + } + /// Evict only through the chat's durability, lease and dirty-input gates. + fn evict_history(&self, cx: &mut App) -> bool; /// The chat's unsaved changes, without changing it. - fn snapshot(&self, cx: &App) -> Option>; + fn snapshot( + &self, + cx: &mut App, + ) -> Result>, nocterm_ai::storage::AgentStorageError>; /// The chat's unsaved changes and the revision they become. - fn capture(&self, cx: &mut App) -> Option<(Arc, u64)>; + fn capture( + &self, + cx: &mut App, + ) -> Result< + Option<(Arc, u64)>, + nocterm_ai::storage::AgentStorageError, + >; } pub type Client = Rc; diff --git a/crates/agent-runtime/src/connections.rs b/crates/agent-runtime/src/connections.rs index 1b2af04d..0808de8a 100644 --- a/crates/agent-runtime/src/connections.rs +++ b/crates/agent-runtime/src/connections.rs @@ -1,33 +1,26 @@ //! Connection membership and retained process-cleanup ownership. use super::{Client, Connection}; use gpui::{EntityId, Task}; -use nocterm_ai::{AgentCommands, AgentInfo, AgentLaunch}; +use nocterm_ai::{AgentCommands, AgentInfo, execution_origin::ChatKey}; use std::{ collections::HashMap, hash::{Hash, Hasher}, - path::{Path, PathBuf}, + path::PathBuf, sync::Arc, }; #[derive(Default)] pub(super) struct ConnectionManager { active: HashMap, - closing: HashMap, + closing: HashMap, cleanup: HashMap>, serial: u64, startups: HashMap, } impl ConnectionManager { - pub(super) fn reserve_key( - &mut self, - launch: &AgentLaunch, - workdir: &Path, - isolated: bool, - ) -> u64 { + pub(super) fn reserve_key(&mut self, identity: &super::execution::ExecutionIdentity) -> u64 { let mut hasher = std::collections::hash_map::DefaultHasher::new(); - launch.hash(&mut hasher); - workdir.hash(&mut hasher); - isolated.hash(&mut hasher); + identity.hash(&mut hasher); self.serial().hash(&mut hasher); hasher.finish() } @@ -62,7 +55,7 @@ impl ConnectionManager { }; connection.commands = Some(commands.clone()); connection.info = Some(info); - connection.workdir = workdir; + connection.identity.prepared_directory(workdir); connection._events = Some(events); let clients = connection.users.values().cloned().collect(); self.retain_cleanup(key, commands); @@ -74,10 +67,10 @@ impl ConnectionManager { pub(super) fn active(&self) -> bool { !self.active.is_empty() || !self.closing.is_empty() } - pub(super) fn same_chat(&self, id: &str) -> bool { + pub(super) fn same_chat(&self, id: &ChatKey) -> bool { self.active .values() - .any(|connection| connection.chat_id == id) + .any(|connection| connection.identity.key() == id) || self.closing.values().any(|chat| chat == id) } pub(super) fn closing_keys(&self) -> impl Iterator { @@ -95,7 +88,7 @@ impl ConnectionManager { let mut connection = self.active.remove(&key)?; connection.cancellation.cancel(); connection.users.remove(&owner); - self.closing.insert(key, connection.chat_id.clone()); + self.closing.insert(key, connection.identity.key().clone()); let commands = commands.or_else(|| connection.commands.take()); if commands.is_none() && let Some(completion) = connection.startup_completion.as_ref() diff --git a/crates/agent-runtime/src/documents.rs b/crates/agent-runtime/src/documents.rs index 0868e78f..1886fc58 100644 --- a/crates/agent-runtime/src/documents.rs +++ b/crates/agent-runtime/src/documents.rs @@ -3,11 +3,13 @@ use super::*; use futures::future::BoxFuture; use gpui::AppContext as _; use nocterm_ai::{ - history::{ChatSummary, SavedChat, SharedChat}, + execution_origin::ChatKey, + history::{ChatSummary, ContextId, SavedChat, SharedChat}, + local_environment::ChatScope, storage::AgentStorageError, }; -use nocterm_store::{KeyedPersisted, KeyedRetention, KeyedStamp, StoreError}; -use std::{cell::RefCell, collections::HashSet, num::NonZeroUsize, rc::Rc}; +use nocterm_store::{KeyedPersisted, KeyedRetention, StoreError}; +use std::{cell::RefCell, num::NonZeroUsize, rc::Rc}; #[derive(Clone)] enum ChatIntent { @@ -20,107 +22,36 @@ struct CandidateStamp { revision: u64, deleted: bool, } -#[derive(Default)] -struct DocumentRegistry { - clients: HashMap, - owners: HashMap, - deleted: HashSet, - stamps: HashMap, - closing: bool, -} -impl DocumentRegistry { - fn register(&mut self, client: Client, chat_id: String) { - if self.closing || self.deleted.contains(&chat_id) { - return; - } - let owner = client.id(); - self.owners.retain(|_, current| *current != owner); - self.owners.insert(chat_id, owner); - self.clients.insert(owner, client); - } - fn retire(&mut self, id: EntityId) -> Vec { - let ids = self - .owners - .iter() - .filter(|(_, owner)| **owner == id) - .map(|(id, _)| id.clone()) - .collect(); - self.clients.remove(&id); - self.owners.retain(|_, owner| *owner != id); - self.stamps.retain(|_, (owner, _)| *owner != id); - ids - } - fn current(&self, id: &str, owner: EntityId) -> bool { - !self.closing && self.capture_current(id, owner) - } - fn capture_current(&self, id: &str, owner: EntityId) -> bool { - !self.deleted.contains(id) && self.owners.get(id) == Some(&owner) - } - fn accepted(&mut self, id: &str, owner: EntityId, revision: u64) { - self.stamps.insert(id.to_owned(), (owner, revision)); - } - fn accepts_revision(&self, id: &str, owner: EntityId, revision: u64) -> bool { - self.capture_current(id, owner) - && self - .stamps - .get(id) - .is_none_or(|(accepted_owner, accepted_revision)| { - *accepted_owner != owner || revision >= *accepted_revision - }) - } - fn seal(&mut self, id: String) { - self.deleted.insert(id.clone()); - self.stamps.remove(&id); - self.owners.remove(&id); - } - fn target( - &self, - stamp: &KeyedStamp, - succeeded: bool, - ) -> Option { - let owner = stamp.tag().owner?; - if stamp.tag().deleted || !self.current(stamp.key(), owner) { - return None; - } - // A stale failure must not pause a newer accepted document revision. - // Successful old publications still acknowledge their actual own revision. - if !succeeded && self.stamps.get(stamp.key()) != Some(&(owner, stamp.tag().revision)) { - return None; - } - self.clients.get(&owner).cloned() - } - fn client(&self, id: EntityId) -> Option { - self.clients.get(&id).cloned() - } - fn clients(&self) -> Vec { - self.clients.values().cloned().collect() - } - fn contains_deleted(&self, id: &str) -> bool { - self.deleted.contains(id) - } - fn stop(&mut self) { - self.closing = true; - } -} +mod registry; +use registry::DocumentRegistry; +pub use registry::{DocumentClaim, DocumentClaimOutcome}; pub(super) struct ChatStore { registry: Rc>, - writer: Entity>, + writer: Entity>, catalog: crate::catalog::Catalog, + admission: crate::history_admission::LocalHistoryAllocations, } impl ChatStore { pub(super) fn new(storage: &mut AgentStorage, cx: &mut Context) -> Self { - let registry = Rc::new(RefCell::new(DocumentRegistry::default())); + let (admission, prepared) = + crate::history_admission::HistoryAdmissionHandle::with_initial_payload( + storage.has_prepared_history(), + ); + let registry = Rc::new(RefCell::new(DocumentRegistry::new(admission.clone()))); let owner = registry.clone(); + let runtime = cx.weak_entity(); let repository = storage.chats(); let backend = repository.clone(); let writer = cx.new(|_| { - KeyedPersisted::::new( + KeyedPersisted::::new( Arc::new(move |id, intent| { let backend = backend.clone(); Box::pin(async move { match intent { ChatIntent::Save(chat) => backend.save(chat).await, - ChatIntent::Delete => backend.delete(id).await, + ChatIntent::Delete => { + backend.delete_in(id.chat_scope(), id.id().to_owned()).await + } } .map_err(StoreError::backend) }) @@ -132,14 +63,21 @@ impl ChatStore { let retained = owner.borrow().target(stamp, succeeded).is_some(); let registry = owner.clone(); let stamp = stamp.clone(); + let runtime = runtime.clone(); // A client may synchronously reenter Runtime and its writer. Deliver only // after the keyed entity borrow ends, and revalidate identity at delivery. cx.defer(move |cx| { + if succeeded && !retained { + let _ = runtime.update(cx, |runtime, cx| { + let _ = runtime.chats.reload(stamp.key().chat_scope(), cx); + cx.notify(); + }); + } let target = registry.borrow().target(&stamp, succeeded); if let Some(client) = target && client .state(cx) - .is_some_and(|state| state.chat_id == *stamp.key()) + .is_some_and(|state| state.origin().key() == stamp.key()) { let event = match result { Ok(()) => SessionEvent::Saved { @@ -160,19 +98,99 @@ impl ChatStore { } }) }); + let accounting = crate::history_admission::LocalHistoryAllocations::new( + admission.clone(), + cx.entity_id(), + ); Self { registry, writer, - catalog: crate::catalog::Catalog::new(storage, cx), + catalog: crate::catalog::Catalog::new(storage, admission, prepared, cx), + admission: accounting, + } + } + pub(super) fn require_client_scope( + expected: ChatScope, + client: &Client, + cx: &mut App, + ) -> Result<(), AgentStorageError> { + let state = client.state(cx); + if let Some(state) = &state { + nocterm_ai::storage::require_local_scope( + &expected, + &state.origin().key().chat_scope(), + )?; + } + if let Some((chat, _)) = client.capture(cx)? { + nocterm_ai::storage::require_local_scope(&expected, &chat.header.chat_scope())?; + if state.is_some_and(|state| state.origin().key().id() != chat.header.id) { + return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); + } + } + Ok(()) + } + pub(super) fn claim( + &self, + key: ChatKey, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, + window: EntityId, + replacing: Option, + ) -> Result { + DocumentRegistry::claim(&self.registry, key, access, window, replacing) + } + /// Commits `claim` to `client`; the owner it replaced in another window, if any. + pub(super) fn commit( + &mut self, + claim: DocumentClaim, + client: Client, + cx: &mut App, + ) -> Result, crate::ChatOpenError> { + if !claim.belongs_to(&self.registry) { + return Err(crate::ChatOpenError::StaleClaim(claim.key().clone())); } + Self::require_client_scope(claim.key().chat_scope(), &client, cx)?; + let state = client.state(cx).ok_or(crate::ChatOpenError::Closed)?; + if state.origin().key() != claim.key() { + return Err(crate::ChatOpenError::Storage( + nocterm_ai::history::HistoryCodecError::IdentityMismatch.into(), + )); + } + let result = self.registry.borrow_mut().commit(&claim, client); + // Rollback after the registry borrow ends, including failed commit. + drop(claim); + result } - pub(super) fn register(&mut self, client: Client, cx: &App) { - if let Some(state) = client.state(cx) { - self.registry.borrow_mut().register(client, state.chat_id) + pub(super) fn take_prepared( + &mut self, + claim: &DocumentClaim, + payload: crate::history_admission::HistoryPayloadPermit, + cx: &mut App, + ) -> Result>, crate::ChatOpenError> { + if !claim.belongs_to(&self.registry) { + return Err(crate::ChatOpenError::StaleClaim(claim.key().clone())); } + self.registry.borrow().require_reservation(claim)?; + Ok(self.catalog.take_prepared(claim.key(), payload, cx)?) + } + pub(super) fn require_owner_scope( + &self, + scope: ChatScope, + id: EntityId, + cx: &mut App, + ) -> Result<(), AgentStorageError> { + if let Some(actual) = self.registry.borrow().scope_of(id) { + nocterm_ai::storage::require_local_scope(&scope, &actual)?; + } + if let Some(client) = self.client(id) { + Self::require_client_scope(scope, &client, cx)?; + } + Ok(()) } pub(super) fn retire(&mut self, id: EntityId, cx: &mut App) { let ids = self.registry.borrow_mut().retire(id); + for key in &ids { + self.catalog.invalidate(key.chat_scope()); + } self.writer.update(cx, |writer, _| { for id in ids { writer.forget_clean(&id); @@ -182,28 +200,48 @@ impl ChatStore { pub(super) fn client(&self, id: EntityId) -> Option { self.registry.borrow().client(id) } + pub(super) fn held_elsewhere(&self, scope: &ChatScope, window: EntityId) -> Vec { + self.registry.borrow().held_elsewhere(scope, window) + } + pub(super) fn window_of(&self, entity: EntityId) -> Option { + self.registry.borrow().window_of(entity) + } + pub(super) fn revoke_stale_sources(&self) -> Vec { + self.registry.borrow_mut().revoke_stale_sources() + } + pub(super) fn source_current(&self, key: &ChatKey) -> bool { + key.environment().is_this_computer() || self.registry.borrow().source_current(key) + } pub(super) fn clients(&self) -> Vec { self.registry.borrow().clients() } - pub(super) fn current(&self, id: &str, owner: EntityId) -> bool { + pub(super) fn current(&self, id: &ChatKey, owner: EntityId) -> bool { self.registry.borrow().current(id, owner) } pub(super) fn save( &mut self, + scope: ChatScope, chat: Arc, owner: EntityId, revision: u64, cx: &mut App, ) -> Result { - if !self.registry.borrow().current(&chat.id, owner) + nocterm_ai::storage::require_local_scope(&scope, &chat.header.chat_scope())?; + let key = ChatKey::in_environment(scope.clone(), chat.header.id.clone()); + if !self.registry.borrow().capture_current(&key, owner) || !self .registry .borrow() - .accepts_revision(&chat.id, owner, revision) + .accepts_revision(&key, owner, revision) { return Ok(false); } - let id = chat.id.clone(); + let existing = self + .client(owner) + .and_then(|client| client.history_usage(cx)) + .map_or(0, |usage| usage.bytes()); + let chat = self.admission.retain_external(owner, existing, chat, cx)?; + let id = key; self.submit( id.clone(), ChatIntent::Save(chat), @@ -218,7 +256,7 @@ impl ChatStore { self.notify_admission(&id, owner, revision, cx); Ok(true) } - fn notify_admission(&self, id: &str, owner: EntityId, revision: u64, cx: &mut App) { + fn notify_admission(&self, id: &ChatKey, owner: EntityId, revision: u64, cx: &mut App) { let registry = self.registry.clone(); let id = id.to_owned(); cx.defer(move |cx| { @@ -236,7 +274,7 @@ impl ChatStore { } fn notify_failed_admission( &self, - id: String, + id: ChatKey, owner: EntityId, revision: u64, error: AgentPersistenceError, @@ -258,7 +296,7 @@ impl ChatStore { } fn submit( &mut self, - id: String, + id: ChatKey, intent: ChatIntent, stamp: CandidateStamp, cx: &mut App, @@ -267,11 +305,13 @@ impl ChatStore { .update(cx, |writer, cx| writer.update_latest(id, intent, stamp, cx)) .map(|_| ()) } - pub(super) fn delete(&mut self, id: String, cx: &mut App) { - if self.registry.borrow().closing { + pub(super) fn delete(&mut self, id: ChatKey, cx: &mut App) { + if self.registry.borrow().closing() { return; } self.registry.borrow_mut().seal(id.clone()); + self.catalog.discard_prepared(&id); + self.catalog.invalidate(id.chat_scope()); if let Err(error) = self.submit( id, ChatIntent::Delete, @@ -293,9 +333,9 @@ impl ChatStore { let Some(client) = self.client(id) else { return Ok(false); }; - if let Some((chat, revision)) = client.capture(cx) { - let key = chat.id.clone(); - match self.save(chat, id, revision, cx) { + if let Some((chat, revision)) = client.capture(cx)? { + let key = ChatKey::in_environment(chat.header.chat_scope(), chat.header.id.clone()); + match self.save(key.chat_scope(), chat, id, revision, cx) { Ok(true) => { let target = client.clone(); cx.defer(move |cx| target.emit(SessionEvent::SaveAdmitted { revision }, cx)); @@ -326,24 +366,84 @@ impl ChatStore { }) .collect() } - pub(super) fn take_saved(&mut self) -> Option> { - self.catalog.take_saved() + pub(super) fn snapshot(&self, scope: ChatScope) -> Option { + self.catalog.snapshot(scope) } - pub(super) fn take_catalog(&mut self) -> Option> { - self.catalog.take_rows().map(|rows| { - rows.into_iter() - .filter(|row| !self.registry.borrow().contains_deleted(&row.id)) - .collect() - }) + pub(super) fn oldest_catalog_payload(&self) -> Option { + self.catalog.oldest_payload() } - pub(super) fn loaded(&mut self, rows: Result, AgentStorageError>) { - self.catalog.complete(rows) + pub(super) fn evict_catalog_payload(&mut self) -> bool { + self.catalog.evict_payload() } - pub(super) fn reload(&mut self, cx: &mut Context) { - self.catalog.reload(cx) + pub(super) fn loaded( + &mut self, + scope: ChatScope, + generation: u64, + rows: Result, AgentStorageError>, + allocation: crate::catalog::CatalogAllocation, + ) { + let rows = rows.map(|mut rows| { + rows.retain(|row| { + !self + .registry + .borrow() + .contains_deleted(&ChatKey::in_environment(scope.clone(), row.id.clone())) + }); + rows + }); + self.catalog.complete(scope, generation, rows, allocation) } - pub(super) fn reader(&self) -> HistoryReader { - self.catalog.reader() + pub(super) fn ensure( + &mut self, + scope: ChatScope, + cx: &mut Context, + ) -> Result<(), crate::CatalogLoadError> { + self.catalog.ensure(scope, cx) + } + pub(super) fn reload( + &mut self, + scope: ChatScope, + cx: &mut Context, + ) -> Result<(), crate::CatalogLoadError> { + self.catalog.reload(scope, cx) + } + pub(super) fn claim_residency( + &self, + claim: &DocumentClaim, + cx: &App, + ) -> Result { + if !claim.belongs_to(&self.registry) { + return Err(crate::ChatOpenError::StaleClaim(claim.key().clone())); + } + self.registry.borrow().require_reservation(claim)?; + Ok(self.history_residency(claim.key().chat_scope(), None, cx)?) + } + pub(super) fn history_residency( + &self, + scope: ChatScope, + requester: Option, + _cx: &App, + ) -> Result { + let registry = self.registry.borrow(); + if registry.closing() { + return Err(AgentStorageError::Interrupted); + } + if let Some(requester) = requester { + let actual = registry + .scope_of(requester) + .ok_or(AgentStorageError::Interrupted)?; + nocterm_ai::storage::require_local_scope(&scope, &actual)?; + } + Ok(self.admission.handle()) + } + pub(super) fn history_admission(&self) -> crate::history_admission::HistoryAdmissionHandle { + self.admission.handle() + } + pub(super) fn unavailable_reader(&self) -> HistoryReader { + self.catalog.unavailable_reader() + } + pub(super) fn reader(&self, scope: ChatScope) -> HistoryReader { + self.catalog.reader(scope) } pub(super) fn begin_shutdown(&mut self) { self.registry.borrow_mut().stop(); @@ -355,21 +455,35 @@ impl ChatStore { if !self .registry .borrow() - .capture_current(&state.chat_id, client.id()) + .capture_current(state.origin().key(), client.id()) { return Ok(()); } - let Some((chat, revision)) = client.capture(cx) else { + let Some((chat, revision)) = client.capture(cx).map_err(StoreError::backend)? else { return Ok(()); }; + nocterm_ai::storage::require_local_scope( + &state.origin().key().chat_scope(), + &chat.header.chat_scope(), + ) + .map_err(StoreError::backend)?; if !self .registry .borrow() - .accepts_revision(&chat.id, client.id(), revision) + .accepts_revision(state.origin().key(), client.id(), revision) { return Ok(()); } - let id = chat.id.clone(); + let chat = self + .admission + .retain_external( + client.id(), + client.history_usage(cx).map_or(0, |usage| usage.bytes()), + chat, + cx, + ) + .map_err(StoreError::backend)?; + let id = state.origin().key().clone(); self.submit( id.clone(), ChatIntent::Save(chat), @@ -394,6 +508,7 @@ impl ChatStore { .filter_map(|client| self.capture_document(&client, cx).err()) .collect::>(); self.catalog.stop(); + self.admission.handle().close(); self.registry.borrow_mut().stop(); let captured = self.writer.update(cx, |writer, _| writer.capture()); Box::pin(async move { @@ -412,14 +527,76 @@ impl ChatStore { } } impl Runtime { - pub fn take_saved_chats(&mut self) -> Option> { - self.chats.take_saved() + #[cfg(test)] + pub(crate) fn claim_document( + &self, + key: ChatKey, + window: EntityId, + replacing: Option, + ) -> Result { + self.chats.claim( + key, + nocterm_ai::local_environment::LocalEnvironmentAccess::this_computer(), + window, + replacing, + ) + } + pub(crate) fn claim_local_document( + &self, + key: ChatKey, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, + window: EntityId, + replacing: Option, + ) -> Result { + self.chats.claim(key, access, window, replacing) + } + pub(crate) fn commit_document( + &mut self, + claim: DocumentClaim, + client: Client, + cx: &mut Context, + ) -> Result<(), crate::ChatOpenError> { + if let Some(chat) = self.chats.commit(claim, client, cx)? { + cx.emit(crate::HolderEvent::Moved { chat }); + } + cx.emit(crate::HolderEvent::Changed); + Ok(()) } - pub fn take_saved_catalog(&mut self) -> Option> { - self.chats.take_catalog() + /// The chats of `scope` windows other than `window` hold. + pub(crate) fn held_elsewhere(&self, scope: &ChatScope, window: EntityId) -> Vec { + self.chats.held_elsewhere(scope, window) + } + /// The window that keeps chat `owner` from moving to `window`: its own, while its agent + /// works there or it shows there. A chat moves from one window to another only at rest. + pub(crate) fn kept_by(&self, owner: EntityId, window: EntityId, cx: &App) -> Option { + let held = self.chats.window_of(owner)?; + if held == window { + return None; + } + let state = self.chats.client(owner).and_then(|client| client.state(cx)); + state + .is_none_or(|state| state.busy() || state.shown()) + .then_some(held) + } + pub(crate) fn take_prepared_chat( + &mut self, + claim: &DocumentClaim, + payload: crate::history_admission::HistoryPayloadPermit, + cx: &mut App, + ) -> Result>, crate::ChatOpenError> { + self.chats.take_prepared(claim, payload, cx) + } + pub(crate) fn catalog_snapshot(&self, scope: ChatScope) -> Option { + self.chats.snapshot(scope) } pub fn document_is_current(&self, chat_id: &str, owner: EntityId) -> bool { - !self.shutting_down && self.chats.current(chat_id, owner) + self.scoped_document_is_current( + &ChatKey::new(ContextId::General, chat_id.to_owned()), + owner, + ) + } + pub(crate) fn scoped_document_is_current(&self, key: &ChatKey, owner: EntityId) -> bool { + !self.shutting_down && self.chats.current(key, owner) } pub fn capture_and_detach_document( &mut self, @@ -432,6 +609,15 @@ impl Runtime { } Ok(detached) } + pub(crate) fn capture_scoped_document( + &mut self, + scope: ChatScope, + id: EntityId, + cx: &mut Context, + ) -> Result { + self.chats.require_owner_scope(scope, id, cx)?; + self.capture_and_detach_document(id, cx) + } pub fn save_chat( &mut self, chat: Arc, @@ -439,13 +625,40 @@ impl Runtime { revision: u64, cx: &mut Context, ) -> Result { - self.chats.save(chat, owner, revision, cx) + self.scoped_save_chat(ContextId::General.into(), chat, owner, revision, cx) + } + pub(crate) fn scoped_save_chat( + &mut self, + scope: ChatScope, + chat: Arc, + owner: EntityId, + revision: u64, + cx: &mut Context, + ) -> Result { + self.chats.save(scope, chat, owner, revision, cx) } pub fn delete_chat(&mut self, id: String, cx: &mut Context) { - self.chats.delete(id, cx) + self.scoped_delete_chat(ChatKey::new(ContextId::General, id), cx) + } + pub(crate) fn scoped_delete_chat(&mut self, key: ChatKey, cx: &mut Context) { + let scope = key.chat_scope(); + self.chats.delete(key, cx); + self.scoped_reload_history(scope, cx); } pub fn reload_history(&mut self, cx: &mut Context) { - self.chats.reload(cx) + self.scoped_reload_history(ContextId::General.into(), cx) + } + pub(crate) fn scoped_reload_history(&mut self, scope: ChatScope, cx: &mut Context) { + if let Err(error) = self.try_scoped_reload_history(scope, cx) { + tracing::warn!(%error, "could not request agent history"); + } + } + pub(crate) fn try_scoped_reload_history( + &mut self, + scope: ChatScope, + cx: &mut Context, + ) -> Result<(), crate::CatalogLoadError> { + self.chats.reload(scope, cx) } } #[cfg(test)] diff --git a/crates/agent-runtime/src/documents/history_tests.rs b/crates/agent-runtime/src/documents/history_tests.rs index f1fba61e..18871d32 100644 --- a/crates/agent-runtime/src/documents/history_tests.rs +++ b/crates/agent-runtime/src/documents/history_tests.rs @@ -10,7 +10,12 @@ use std::{ rc::Rc, }; mod closing; +mod contexts; +mod environments; +mod local_launch; +mod ownership; mod preferences; +mod residency; mod usage; mod workspace; struct UnusedBridge; @@ -39,11 +44,18 @@ struct ProbeClient { marker: Entity<()>, chat: RefCell>, revision: Cell, + closed: Cell, events: RefCell>, on_saved: Option, on_admitted: RefCell>, + history: Cell>, + history_reads: Cell, + allow_history_eviction: Cell, } impl ProbeClient { + fn close(&self) { + self.closed.set(true); + } fn new(cx: &mut TestAppContext, chat: Arc) -> Rc { Self::create(cx, chat, None) } @@ -56,9 +68,13 @@ impl ProbeClient { marker: cx.new(|_| ()), chat: RefCell::new(chat), revision: Cell::new(1), + closed: Cell::new(false), events: RefCell::new(Vec::new()), on_saved, on_admitted: RefCell::new(None), + history: Cell::new(None), + history_reads: Cell::new(0), + allow_history_eviction: Cell::new(false), }) } } @@ -67,7 +83,7 @@ impl SessionClient for ProbeClient { self.marker.entity_id() } fn alive(&self) -> bool { - true + !self.closed.get() } fn emit(&self, event: SessionEvent, cx: &mut App) { let saved = matches!(&event, SessionEvent::Saved { .. }); @@ -81,23 +97,47 @@ impl SessionClient for ProbeClient { } } fn state(&self, _: &App) -> Option { - Some(ClientState { - chat_id: self.chat.borrow().id.clone(), - workdir: None, - agent_id: "codex".into(), - session: None, - busy: false, - leased: false, - activation_pending: false, - closing: false, - shown: false, - }) + if self.closed.get() { + return None; + } + let chat = self.chat.borrow(); + let origin = nocterm_ai::execution_origin::ExecutionOrigin::new( + nocterm_ai::execution_origin::ChatKey::from_header(&chat.header), + None, + ); + Some(ClientState::new( + origin, + "codex".into(), + None, + false, + false, + false, + false, + false, + )) } - fn snapshot(&self, _: &App) -> Option> { - Some(self.chat.borrow().clone()) + fn history_usage(&self, _: &App) -> Option { + self.history_reads.set(self.history_reads.get() + 1); + self.history.get() } - fn capture(&self, _: &mut App) -> Option<(Arc, u64)> { - Some((self.chat.borrow().clone(), self.revision.get())) + fn evict_history(&self, _: &mut App) -> bool { + if !self.allow_history_eviction.get() { + return false; + } + self.history.set(None); + true + } + fn snapshot( + &self, + _: &mut App, + ) -> Result>, nocterm_ai::storage::AgentStorageError> { + Ok(Some(self.chat.borrow().clone())) + } + fn capture( + &self, + _: &mut App, + ) -> Result, u64)>, nocterm_ai::storage::AgentStorageError> { + Ok(Some((self.chat.borrow().clone(), self.revision.get()))) } } fn fixture( @@ -133,50 +173,54 @@ fn runtime_from_storage( storage: AgentStorage, directory: &std::path::Path, ) -> Entity { - runtime_with_usage_source( + runtime_with_workspace_resolver( cx, connector, storage, directory, - Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()), Arc::new(nocterm_ai::testing::ScriptedWorkdirResolver::accept_requests()), ) } -fn runtime_with_usage_source( +fn runtime_with_workspace_resolver( cx: &mut TestAppContext, connector: Arc, storage: AgentStorage, directory: &std::path::Path, - usage_source: Arc, workdir_resolver: Arc, ) -> Entity { cx.update(|cx| { - let settings = nocterm_store::SettingsConfig::in_memory(Default::default()).install(cx); - let agents = nocterm_agent_directory::AgentsHandle::new( - nocterm_agent_providers::catalog(), - settings.clone(), - cx, - ); - cx.new(|cx| { - Runtime::new( - RuntimeServices { - settings, - agents, - connector, - bridge: Arc::new(UnusedBridge), - storage, - usage_source, - workdir_resolver, - workdir: directory.join("work"), - terminal_auth: false, - private_dirs: Vec::new(), - shared_dirs: Vec::new(), - }, - cx, - ) - }) + let services = runtime_services(connector, storage, directory, workdir_resolver, cx); + cx.new(|cx| Runtime::new(services, cx)) }) } +fn runtime_services( + connector: Arc, + storage: AgentStorage, + directory: &std::path::Path, + workdir_resolver: Arc, + cx: &mut App, +) -> RuntimeServices { + let settings = nocterm_store::SettingsConfig::in_memory(Default::default()).install(cx); + let agents = nocterm_agent_directory::AgentsHandle::new( + nocterm_agent_providers::catalog(), + settings.clone(), + cx, + ); + RuntimeServices { + command_jobs: Arc::new(nocterm_session::testing::ScriptedCommandJobsFactory::default()), + transfers: Arc::new(nocterm_transfers::testing::FakeTransfers::default()), + settings, + agents, + connector, + bridge: Arc::new(UnusedBridge), + storage, + workdir_resolver, + workdir: directory.join("work"), + terminal_auth: false, + private_dirs: Vec::new(), + shared_dirs: Vec::new(), + } +} fn snapshot() -> Arc { let metadata=nocterm_ai::history::decode(br#"{"version":1,"id":"00000000-0000-0000-0000-000000000001","agent_id":"codex","updated":1,"draft":"draft from the released window","entries":[]}"#,"00000000-0000-0000-0000-000000000001").unwrap(); @@ -187,7 +231,7 @@ fn snapshot() -> Arc { } fn admit(runtime: &Entity, client: Rc, cx: &mut TestAppContext) { runtime.update(cx, |runtime, cx| { - runtime.register_document(client.clone(), cx); + ownership::register(runtime, client.clone(), cx).unwrap(); runtime .save_chat( client.chat.borrow().clone(), @@ -203,7 +247,7 @@ async fn shutdown_persists_a_dequeued_snapshot_after_its_thread_has_gone(cx: &mu let (runtime, _directory, repository) = fixture(cx); let chat = snapshot(); let client = ProbeClient::new(cx, chat.clone()); - let release = repository.block_next(StorageOperation::Save(chat.id.clone())); + let release = repository.block_next(StorageOperation::Save(chat.header.id.clone())); admit(&runtime, client.clone(), cx); cx.run_until_parked(); runtime.update(cx, |runtime, cx| { @@ -215,7 +259,7 @@ async fn shutdown_persists_a_dequeued_snapshot_after_its_thread_has_gone(cx: &mu final_work.await; let saved = repository.chats(); assert_eq!(saved.len(), 1); - assert_eq!(saved[0].id, chat.id); + assert_eq!(saved[0].header.id, chat.header.id); assert_eq!(saved[0].draft, chat.draft); } #[gpui::test] @@ -225,7 +269,7 @@ async fn the_latest_queued_snapshot_wins_over_an_unacknowledged_disk_write( let (runtime, _directory, repository) = fixture(cx); let old = snapshot(); let client = ProbeClient::new(cx, old.clone()); - let release = repository.block_next(StorageOperation::Save(old.id.clone())); + let release = repository.block_next(StorageOperation::Save(old.header.id.clone())); admit(&runtime, client.clone(), cx); cx.run_until_parked(); let mut metadata = old.metadata.clone(); @@ -251,10 +295,12 @@ async fn deleting_a_chat_overrides_its_unacknowledged_disk_write_at_shutdown( let (runtime, _directory, repository) = fixture(cx); let chat = snapshot(); let client = ProbeClient::new(cx, chat.clone()); - let release = repository.block_next(StorageOperation::Save(chat.id.clone())); + let release = repository.block_next(StorageOperation::Save(chat.header.id.clone())); admit(&runtime, client, cx); cx.run_until_parked(); - runtime.update(cx, |runtime, cx| runtime.delete_chat(chat.id.clone(), cx)); + runtime.update(cx, |runtime, cx| { + runtime.delete_chat(chat.header.id.clone(), cx) + }); let final_work = runtime.update(cx, |runtime, cx| runtime.shutdown(cx)); release.send(Ok(())).unwrap(); final_work.await; @@ -267,17 +313,19 @@ async fn final_tombstone_waits_for_active_disk_write_and_prevents_resurrection( let (runtime, _directory, repository) = fixture(cx); let chat = snapshot(); let client = ProbeClient::new(cx, chat.clone()); - let release = repository.block_next(StorageOperation::Save(chat.id.clone())); + let release = repository.block_next(StorageOperation::Save(chat.header.id.clone())); admit(&runtime, client, cx); cx.run_until_parked(); - runtime.update(cx, |runtime, cx| runtime.delete_chat(chat.id.clone(), cx)); + runtime.update(cx, |runtime, cx| { + runtime.delete_chat(chat.header.id.clone(), cx) + }); let final_work = runtime.update(cx, |runtime, cx| runtime.shutdown(cx)); cx.run_until_parked(); assert!(repository.chats().is_empty()); assert!( !repository .operations() - .contains(&StorageOperation::Delete(chat.id.clone())) + .contains(&StorageOperation::Delete(chat.header.id.clone())) ); release.send(Ok(())).unwrap(); final_work.await; @@ -314,7 +362,7 @@ fn one_chat_failure_does_not_block_another_owner_and_new_intent_retries(cx: &mut let (runtime, _directory, repository) = fixture(cx); let first = snapshot(); let mut second = first.metadata.clone(); - second.id = "00000000-0000-0000-0000-000000000002".into(); + second.header.id = "00000000-0000-0000-0000-000000000002".into(); second.draft = Some("independent successful document".into()); let second = Arc::new(SharedChat { metadata: second, @@ -323,15 +371,15 @@ fn one_chat_failure_does_not_block_another_owner_and_new_intent_retries(cx: &mut let first_client = ProbeClient::new(cx, first.clone()); let second_client = ProbeClient::new(cx, second.clone()); repository.fail_next( - StorageOperation::Save(first.id.clone()), + StorageOperation::Save(first.header.id.clone()), AgentStorageError::Interrupted, ); admit(&runtime, first_client.clone(), cx); admit(&runtime, second_client.clone(), cx); cx.run_until_parked(); - assert!(repository.transcript(&first.id).is_err()); + assert!(repository.transcript(&first.header.id).is_err()); assert_eq!( - repository.transcript(&second.id).unwrap().draft, + repository.transcript(&second.header.id).unwrap().draft, second.draft ); assert!( @@ -358,7 +406,11 @@ fn one_chat_failure_does_not_block_another_owner_and_new_intent_retries(cx: &mut admit(&runtime, first_client.clone(), cx); cx.run_until_parked(); assert_eq!( - repository.transcript(&first.id).unwrap().draft.as_deref(), + repository + .transcript(&first.header.id) + .unwrap() + .draft + .as_deref(), Some("new accepted intent") ); assert!( @@ -375,7 +427,7 @@ fn an_old_owner_failure_cannot_poison_its_replacement(cx: &mut TestAppContext) { let (runtime, _directory, repository) = fixture(cx); let chat = snapshot(); let old = ProbeClient::new(cx, chat.clone()); - let release = repository.block_next(StorageOperation::Save(chat.id.clone())); + let release = repository.block_next(StorageOperation::Save(chat.header.id.clone())); admit(&runtime, old.clone(), cx); cx.run_until_parked(); let mut latest = chat.metadata.clone(); @@ -386,6 +438,7 @@ fn an_old_owner_failure_cannot_poison_its_replacement(cx: &mut TestAppContext) { }); let replacement = ProbeClient::new(cx, latest); replacement.revision.set(2); + ownership::replace(&runtime, old.id(), replacement.clone(), cx); admit(&runtime, replacement.clone(), cx); release.send(Err(AgentStorageError::Interrupted)).unwrap(); cx.run_until_parked(); @@ -410,7 +463,11 @@ fn an_old_owner_failure_cannot_poison_its_replacement(cx: &mut TestAppContext) { .any(|event| matches!(event, SessionEvent::Saved { revision: 2 })) ); assert_eq!( - repository.transcript(&chat.id).unwrap().draft.as_deref(), + repository + .transcript(&chat.header.id) + .unwrap() + .draft + .as_deref(), Some("replacement text") ); } @@ -437,12 +494,12 @@ fn publication_client_can_synchronously_retire_itself_without_reentering_worker_ .iter() .any(|event| matches!(event, SessionEvent::Saved { revision: 1 })) ); - assert!(repository.transcript(&snapshot().id).is_ok()); + assert!(repository.transcript(&snapshot().header.id).is_ok()); cx.update(|cx| { assert!( !runtime .read(cx) - .document_is_current(&snapshot().id, client.id()) + .document_is_current(&snapshot().header.id, client.id()) ) }); } @@ -452,7 +509,7 @@ fn an_older_failed_revision_cannot_pause_a_newer_accepted_document(cx: &mut Test let (runtime, _directory, repository) = fixture(cx); let chat = snapshot(); let client = ProbeClient::new(cx, chat.clone()); - let release = repository.block_next(StorageOperation::Save(chat.id.clone())); + let release = repository.block_next(StorageOperation::Save(chat.header.id.clone())); admit(&runtime, client.clone(), cx); cx.run_until_parked(); let mut metadata = chat.metadata.clone(); @@ -480,7 +537,11 @@ fn an_older_failed_revision_cannot_pause_a_newer_accepted_document(cx: &mut Test .any(|event| matches!(event, SessionEvent::Saved { revision: 2 })) ); assert_eq!( - repository.transcript(&chat.id).unwrap().draft.as_deref(), + repository + .transcript(&chat.header.id) + .unwrap() + .draft + .as_deref(), Some("newer accepted text") ); } @@ -492,8 +553,8 @@ fn an_active_success_acknowledges_only_its_revision_while_the_newer_write_waits( let (runtime, _directory, repository) = fixture(cx); let chat = snapshot(); let client = ProbeClient::new(cx, chat.clone()); - let first = repository.block_next(StorageOperation::Save(chat.id.clone())); - let second = repository.block_next(StorageOperation::Save(chat.id.clone())); + let first = repository.block_next(StorageOperation::Save(chat.header.id.clone())); + let second = repository.block_next(StorageOperation::Save(chat.header.id.clone())); admit(&runtime, client.clone(), cx); cx.run_until_parked(); let mut metadata = chat.metadata.clone(); @@ -506,7 +567,10 @@ fn an_active_success_acknowledges_only_its_revision_while_the_newer_write_waits( admit(&runtime, client.clone(), cx); first.send(Ok(())).unwrap(); cx.run_until_parked(); - assert_eq!(repository.transcript(&chat.id).unwrap().draft, chat.draft); + assert_eq!( + repository.transcript(&chat.header.id).unwrap().draft, + chat.draft + ); assert_eq!( client .events @@ -523,7 +587,11 @@ fn an_active_success_acknowledges_only_its_revision_while_the_newer_write_waits( second.send(Ok(())).unwrap(); cx.run_until_parked(); assert_eq!( - repository.transcript(&chat.id).unwrap().draft.as_deref(), + repository + .transcript(&chat.header.id) + .unwrap() + .draft + .as_deref(), Some("newer desired document"), ); assert_eq!( @@ -547,8 +615,8 @@ fn an_old_owner_success_cannot_acknowledge_its_replacements_blocked_document( let (runtime, _directory, repository) = fixture(cx); let chat = snapshot(); let old = ProbeClient::new(cx, chat.clone()); - let first = repository.block_next(StorageOperation::Save(chat.id.clone())); - let second = repository.block_next(StorageOperation::Save(chat.id.clone())); + let first = repository.block_next(StorageOperation::Save(chat.header.id.clone())); + let second = repository.block_next(StorageOperation::Save(chat.header.id.clone())); admit(&runtime, old.clone(), cx); cx.run_until_parked(); let mut metadata = chat.metadata.clone(); @@ -561,10 +629,14 @@ fn an_old_owner_success_cannot_acknowledge_its_replacements_blocked_document( }), ); replacement.revision.set(2); + ownership::replace(&runtime, old.id(), replacement.clone(), cx); admit(&runtime, replacement.clone(), cx); first.send(Ok(())).unwrap(); cx.run_until_parked(); - assert_eq!(repository.transcript(&chat.id).unwrap().draft, chat.draft); + assert_eq!( + repository.transcript(&chat.header.id).unwrap().draft, + chat.draft + ); assert!( old.events .borrow() @@ -599,7 +671,11 @@ fn an_old_owner_success_cannot_acknowledge_its_replacements_blocked_document( [2], ); assert_eq!( - repository.transcript(&chat.id).unwrap().draft.as_deref(), + repository + .transcript(&chat.header.id) + .unwrap() + .draft + .as_deref(), Some("replacement desired document"), ); } @@ -626,7 +702,10 @@ fn a_delayed_offer_cannot_replace_an_already_admitted_newer_revision(cx: &mut Te "revision one cannot revoke an already admitted revision two" ); cx.run_until_parked(); - assert_eq!(repository.transcript(&old.id).unwrap().draft, latest.draft); + assert_eq!( + repository.transcript(&old.header.id).unwrap().draft, + latest.draft + ); assert!(!client.events.borrow().iter().any(|event| matches!( event, SessionEvent::SaveAdmitted { revision: 1 } | SessionEvent::Saved { revision: 1 } @@ -645,7 +724,7 @@ async fn stale_closing_capture_preserves_the_newer_writer_owned_candidate(cx: &m queue: Vec::new(), }); runtime.update(cx, |runtime, cx| { - runtime.register_document(client.clone(), cx); + ownership::register(runtime, client.clone(), cx).unwrap(); assert!( runtime .save_chat(latest.clone(), client.id(), 2, cx) @@ -654,7 +733,10 @@ async fn stale_closing_capture_preserves_the_newer_writer_owned_candidate(cx: &m }); let final_work = runtime.update(cx, |runtime, cx| runtime.capture_shutdown(cx)); final_work.await.unwrap(); - assert_eq!(repository.transcript(&old.id).unwrap().draft, latest.draft); + assert_eq!( + repository.transcript(&old.header.id).unwrap().draft, + latest.draft + ); } #[gpui::test] @@ -671,7 +753,7 @@ fn a_detached_capture_acknowledges_its_original_client_outside_the_runtime_borro observed.set(true); })); runtime.update(cx, |runtime, cx| { - runtime.register_document(client.clone(), cx); + ownership::register(runtime, client.clone(), cx).unwrap(); assert!( runtime .capture_and_detach_document(client.id(), cx) @@ -692,7 +774,7 @@ fn a_detached_capture_acknowledges_its_original_client_outside_the_runtime_borro .any(|event| matches!(event, SessionEvent::SaveAdmitted { revision: 1 })) ); assert_eq!( - repository.transcript(&snapshot().id).unwrap().draft, + repository.transcript(&snapshot().header.id).unwrap().draft, snapshot().draft ); } diff --git a/crates/agent-runtime/src/documents/history_tests/closing.rs b/crates/agent-runtime/src/documents/history_tests/closing.rs index 66b672ff..5ff6b289 100644 --- a/crates/agent-runtime/src/documents/history_tests/closing.rs +++ b/crates/agent-runtime/src/documents/history_tests/closing.rs @@ -11,7 +11,7 @@ use std::sync::{ atomic::{AtomicUsize, Ordering}, }; -struct BlockedCommands { +pub(super) struct BlockedCommands { closes: AtomicUsize, shutdowns: AtomicUsize, slow_shutdown: bool, @@ -20,7 +20,7 @@ struct BlockedCommands { shutdown_gate: Mutex>>, } impl BlockedCommands { - fn new(slow_shutdown: bool) -> Arc { + pub(super) fn new(slow_shutdown: bool) -> Arc { Arc::new(Self { closes: AtomicUsize::new(0), shutdowns: AtomicUsize::new(0), @@ -94,7 +94,20 @@ fn connected_client( commands: Arc, cx: &mut TestAppContext, ) -> Rc { - let client = ProbeClient::new(cx, snapshot()); + // Separate documents may share a native command owner; duplicate document + // ownership itself is now refused before constructing a replacement. + let mut chat = snapshot().metadata.clone(); + chat.header.id = format!( + "00000000-0000-0000-0000-{:012x}", + connector.requests().len() + 1 + ); + let client = ProbeClient::new( + cx, + Arc::new(SharedChat { + metadata: chat, + queue: Vec::new(), + }), + ); connector.push(Ok(nocterm_ai::AgentConnection { commands, events: async_channel::unbounded().1, @@ -106,20 +119,23 @@ fn connected_client( }, })); runtime.update(cx, |runtime, cx| { - runtime.register_document(client.clone(), cx); - runtime.connect( - client.clone(), - client.chat.borrow().id.clone(), - AgentLaunch { - id: "codex".into(), - name: "test".into(), - command: "unused".into(), - args: Vec::new(), - env: Default::default(), - inherit_env: Vec::new(), - }, - cx, - ); + ownership::register(runtime, client.clone(), cx).unwrap(); + runtime + .connect( + client.clone(), + client.chat.borrow().header.id.clone(), + AgentLaunch { + id: "codex".into(), + name: "test".into(), + command: "unused".into(), + args: Vec::new(), + env: Default::default(), + inherit_env: Vec::new(), + hub: None, + }, + cx, + ) + .unwrap(); }); cx.run_until_parked(); client diff --git a/crates/agent-runtime/src/documents/history_tests/contexts.rs b/crates/agent-runtime/src/documents/history_tests/contexts.rs new file mode 100644 index 00000000..989df9ca --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/contexts.rs @@ -0,0 +1,580 @@ +//! General consumers refuse scoped documents before ownership or process admission. +use super::*; +use nocterm_ai::{ + history::{ContextId, SavedChat}, + storage::AgentStorageError, +}; + +pub(super) fn decoded_allowance( + admission: &crate::history_admission::HistoryAdmissionHandle, + bytes: usize, + cx: &mut App, +) -> nocterm_core::allocation::AllocationLease { + let slot = admission.reserve_payload_reclaiming(None, cx).unwrap(); + let proof = admission.reserve_unowned_bytes(bytes, cx).unwrap(); + nocterm_core::allocation::AllocationLease::new(bytes, 1, (slot, proof)) +} + +pub(super) fn project_chat() -> SavedChat { + let mut chat = snapshot().metadata.clone(); + chat.version = 2; + chat.header.scope = serde_json::from_value(serde_json::json!({ + "kind": "project", "id": "01010101010101010101010101010101" + })) + .unwrap(); + chat +} + +#[test] +fn prepared_general_history_refuses_a_project_instead_of_reassigning_it() { + assert!(matches!( + crate::PreparedHistory::new(vec![project_chat()]), + Err(AgentStorageError::ScopeMismatch { + expected: ContextId::General, + .. + }) + )); +} + +#[gpui::test] +fn general_registration_and_direct_start_refuse_project_documents_without_native_work( + cx: &mut TestAppContext, +) { + let connector = Arc::new(ScriptedConnector::default()); + let (runtime, _directory, repository) = fixture_with_connector(cx, connector.clone()); + let chat = Arc::new(SharedChat { + metadata: project_chat(), + queue: Vec::new(), + }); + let client = ProbeClient::new(cx, chat.clone()); + runtime.update(cx, |runtime, cx| { + assert!(matches!( + ownership::register(runtime, client.clone(), cx), + Err(crate::ChatOpenError::Storage( + AgentStorageError::ScopeMismatch { .. } + )) + )); + assert!(!runtime.document_is_current(&chat.header.id, client.id())); + let launch = nocterm_ai::AgentLaunch { + id: "codex".into(), + name: "test".into(), + command: "unused".into(), + args: Vec::new(), + env: Default::default(), + inherit_env: Vec::new(), + hub: None, + }; + assert!(matches!( + runtime.connect(client.clone(), chat.header.id.clone(), launch, cx), + Err(crate::AgentActivationError::Storage( + AgentStorageError::ScopeMismatch { .. } + )) + )); + assert!(matches!( + runtime.save_chat(chat.clone(), client.id(), 1, cx), + Err(AgentPersistenceError::Storage( + AgentStorageError::ScopeMismatch { .. } + )) + )); + runtime.request_activation(client.clone(), cx); + }); + cx.run_until_parked(); + assert!(connector.requests().is_empty()); + assert!(repository.encoded(&chat.header.id).is_none()); +} + +#[gpui::test] +fn general_hydration_and_catalog_preserve_other_contexts_in_storage(cx: &mut TestAppContext) { + let (runtime, _directory, repository) = fixture(cx); + let project = project_chat(); + let mut general = snapshot().metadata.clone(); + general.header.id = "00000000-0000-0000-0000-000000000002".into(); + repository.seed_chat(&project).unwrap(); + repository.seed_chat(&general).unwrap(); + runtime.update(cx, |runtime, cx| runtime.reload_history(cx)); + cx.run_until_parked(); + let rows = runtime.update(cx, |runtime, _| { + runtime.catalog_snapshot(ContextId::General.into()).unwrap() + }); + assert_eq!(rows.rows().len(), 1); + assert_eq!(rows.rows()[0].id, general.header.id); + let reader = cx.update(|cx| runtime.read(cx).history_reader()); + let (loaded, permit) = + futures::executor::block_on(reader.prepare_load(project.header.id.clone())); + assert!(matches!(loaded, Err(AgentStorageError::NotFound(_)))); + drop(permit); + assert_eq!( + repository + .transcript_in_scope(project.header.scope, &project.header.id) + .unwrap() + .header + .scope, + project.header.scope + ); +} + +type ScopedFixture = ( + RuntimeHandle, + tempfile::TempDir, + Arc, + Arc, + Arc, +); +pub(super) fn scoped_fixture(cx: &mut TestAppContext) -> ScopedFixture { + scoped_fixture_with_history(cx, None) +} +pub(super) fn scoped_fixture_with_history( + cx: &mut TestAppContext, + history: Option, +) -> ScopedFixture { + let directory = tempfile::tempdir().unwrap(); + let repository = Arc::new(InMemoryAgentRepository::default()); + let connector = Arc::new(ScriptedConnector::default()); + let resolver = Arc::new(nocterm_ai::testing::ScriptedWorkdirResolver::accept_requests()); + let storage = AgentStorage::prepared( + repository.clone(), + repository.clone(), + Ok(Default::default()), + ); + let storage = match history { + Some(history) => storage.with_history(history), + None => storage, + }; + let runtime = cx.update(|cx| { + RuntimeHandle::new( + runtime_services( + connector.clone(), + storage, + directory.path(), + resolver.clone(), + cx, + ), + cx, + ) + }); + (runtime, directory, repository, connector, resolver) +} +#[gpui::test] +fn scoped_catalog_snapshots_are_repeatable_and_cannot_consume_another_context( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let mut first = snapshot().metadata.clone(); + first.header.title = Some("General caption".into()); + let mut second = project_chat(); + second.header.title = Some("Project caption".into()); + repository.seed_chat(&first).unwrap(); + repository.seed_chat(&second).unwrap(); + cx.update(|cx| { + general.reload_history(cx); + project.reload_history(cx); + }); + cx.run_until_parked(); + cx.update(|cx| { + let rows = general.catalog_snapshot(cx).unwrap(); + assert_eq!(rows.scope(), ContextId::General); + assert_eq!(rows.rows().len(), 1); + assert_eq!(rows.rows()[0].title(), "General caption"); + let other = project.catalog_snapshot(cx).unwrap(); + assert_eq!(other.scope(), second.header.scope); + assert_eq!(other.rows()[0].title(), "Project caption"); + assert_eq!( + general.catalog_snapshot(cx).unwrap().generation(), + rows.generation() + ); + assert_eq!( + project.catalog_snapshot(cx).unwrap().generation(), + other.generation() + ); + assert_eq!( + general.catalog_snapshot(cx).unwrap().rows()[0].title(), + "General caption" + ); + }); +} +#[gpui::test] +async fn readers_keep_their_original_context_after_another_scoped_handle_is_created( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let mut first = snapshot().metadata.clone(); + first.header.title = Some("General body".into()); + let mut second = project_chat(); + second.header.title = Some("Project body".into()); + repository.seed_chat(&first).unwrap(); + repository.seed_chat(&second).unwrap(); + let reader = cx.update(|cx| project.history_reader(cx)); + let _selected_elsewhere = project.for_scope(ContextId::General); + let (prepared, permit) = reader.prepare_load(second.header.id.clone()).await; + let prepared = prepared.unwrap(); + let allocation = cx.update(|cx| { + decoded_allowance( + &general.history_admission(cx), + prepared.resident_bytes(), + cx, + ) + }); + let loaded = prepared.decode(allocation).await.unwrap(); + assert_eq!(loaded.value().header.scope, second.header.scope); + assert_eq!(loaded.value().header.title, second.header.title); + drop(permit); + let (loaded, permit) = cx + .update(|cx| general.history_reader(cx)) + .prepare_load(first.header.id) + .await; + let prepared = loaded.unwrap(); + let allocation = cx.update(|cx| { + decoded_allowance( + &general.history_admission(cx), + prepared.resident_bytes(), + cx, + ) + }); + assert_eq!( + prepared + .decode(allocation) + .await + .unwrap() + .value() + .header + .title, + first.header.title + ); + drop(permit); +} +#[gpui::test] +fn scoped_writers_and_deletion_do_not_share_a_key_for_identical_chat_ids(cx: &mut TestAppContext) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let first = snapshot(); + let second = Arc::new(SharedChat { + metadata: project_chat(), + queue: Vec::new(), + }); + assert_eq!(first.header.id, second.header.id); + let general_client = ProbeClient::new(cx, first.clone()); + let project_client = ProbeClient::new(cx, second.clone()); + let release = repository.block_next(StorageOperation::Save(first.header.id.clone())); + cx.update(|cx| { + ownership::register_bound(&general, general_client.clone(), cx).unwrap(); + ownership::register_bound(&project, project_client.clone(), cx).unwrap(); + assert!( + general + .save_chat(first.clone(), general_client.id(), 1, cx) + .unwrap() + ); + }); + cx.run_until_parked(); + cx.update(|cx| { + assert!( + project + .save_chat(second.clone(), project_client.id(), 1, cx) + .unwrap() + ) + }); + cx.run_until_parked(); + assert!(repository.encoded(&first.header.id).is_none()); + // The shared Store intentionally publishes one operation at a time. The + // second context must retain its separate queued key while the first waits. + release.send(Ok(())).unwrap(); + cx.run_until_parked(); + assert!(repository.encoded(&first.header.id).is_some()); + let before = repository + .encoded_in_scope(second.header.scope, &second.header.id) + .unwrap(); + cx.update(|cx| general.delete_chat(first.header.id.clone(), cx)); + cx.run_until_parked(); + assert!(repository.encoded(&first.header.id).is_none()); + assert_eq!( + repository + .encoded_in_scope(second.header.scope, &second.header.id) + .unwrap(), + before + ); +} +#[gpui::test] +fn a_scoped_document_uses_the_ordinary_local_connector_without_changing_its_membership( + cx: &mut TestAppContext, +) { + let (general, _directory, _, connector, resolver) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let chat = Arc::new(SharedChat { + metadata: project_chat(), + queue: Vec::new(), + }); + let client = ProbeClient::new(cx, chat.clone()); + cx.update(|cx| { + ownership::register_bound(&project, client.clone(), cx).unwrap(); + project.request_activation(client.clone(), cx).unwrap(); + assert!(matches!( + general.request_activation(client.clone(), cx), + Err(crate::AgentActivationError::Origin( + nocterm_ai::execution_origin::ExecutionOriginError::ScopeMismatch { .. } + )) + )); + let launch = nocterm_ai::AgentLaunch { + id: "codex".into(), + name: "test".into(), + command: "unused".into(), + args: Vec::new(), + env: Default::default(), + inherit_env: Vec::new(), + hub: None, + }; + let registration = project + .register_bridge(&(client.clone() as Client), cx) + .unwrap(); + assert_eq!( + registration.launch().program, + std::path::Path::new("unused") + ); + project + .connect(client.clone(), chat.header.id.clone(), launch, cx) + .unwrap(); + }); + cx.run_until_parked(); + assert_eq!(resolver.requests().len(), 1); + assert_eq!(connector.requests().len(), 1); + assert_eq!(connector.requests()[0].launch.command, "unused"); + assert_eq!( + connector.requests()[0].working_directory, + _directory.path().join("work") + ); + cx.update(|cx| { + let state = client.state(cx).unwrap(); + assert_eq!(state.origin().scope(), chat.header.scope); + assert_eq!(state.origin().key().id(), chat.header.id); + assert!(project.document_is_current(&chat.header.id, client.id(), cx)); + }); +} + +fn project_scope(index: u8) -> ContextId { + serde_json::from_value( + serde_json::json!({"kind": "project", "id": format!("{index:02x}").repeat(16)}), + ) + .unwrap() +} +mod catalog_capacity; +#[gpui::test] +fn closed_clients_and_mismatched_chat_arguments_cannot_reach_native_ports(cx: &mut TestAppContext) { + let (general, _directory, _, connector, resolver) = scoped_fixture(cx); + let chat = snapshot(); + let client = ProbeClient::new(cx, chat.clone()); + let launch = nocterm_ai::AgentLaunch { + id: "codex".into(), + name: "test".into(), + command: "unused".into(), + args: Vec::new(), + env: Default::default(), + inherit_env: Vec::new(), + hub: None, + }; + cx.update(|cx| { + assert!(matches!( + general.connect(client.clone(), "another-id".into(), launch.clone(), cx), + Err(crate::AgentActivationError::Storage( + AgentStorageError::Codec(nocterm_ai::history::HistoryCodecError::IdentityMismatch) + )) + )); + client.close(); + assert!(matches!( + general.connect(client.clone(), chat.header.id.clone(), launch, cx), + Err(crate::AgentActivationError::ClientClosed) + )); + assert!(matches!( + general.register_bridge(&(client.clone() as Client), cx), + Err(nocterm_ai::connection::BridgeRegistrationError::ClientClosed) + )); + }); + cx.run_until_parked(); + assert!(connector.requests().is_empty()); + assert!(resolver.requests().is_empty()); +} +#[gpui::test] +fn fresh_catalog_snapshots_hide_deleted_membership_only_in_its_original_scope( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let first = snapshot().metadata.clone(); + let second = project_chat(); + repository.seed_chat(&first).unwrap(); + repository.seed_chat(&second).unwrap(); + cx.update(|cx| project.reload_history(cx)); + cx.run_until_parked(); + let before = cx.update(|cx| general.catalog_snapshot(cx).unwrap()); + assert_eq!(before.rows().len(), 1); + cx.update(|cx| general.delete_chat(first.header.id.clone(), cx)); + cx.run_until_parked(); + cx.update(|cx| { + assert!(general.catalog_snapshot(cx).unwrap().rows().is_empty()); + assert_eq!(project.catalog_snapshot(cx).unwrap().rows().len(), 1); + }); + assert_eq!(before.rows().len(), 1); // An already-issued snapshot stays immutable. + assert!( + repository + .encoded_in_scope(second.header.scope, &second.header.id) + .is_some() + ); +} +#[gpui::test] +fn a_bound_handle_cannot_retire_or_capture_another_contexts_live_document(cx: &mut TestAppContext) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let chat = Arc::new(SharedChat { + metadata: project_chat(), + queue: Vec::new(), + }); + let client = ProbeClient::new(cx, chat.clone()); + cx.update(|cx| { + ownership::register_bound(&project, client.clone(), cx).unwrap(); + assert!(matches!( + general.capture_and_detach_document(client.id(), cx), + Err(AgentPersistenceError::Storage( + AgentStorageError::ScopeMismatch { + expected: ContextId::General, + .. + } + )) + )); + assert!(matches!( + general.unregister_document(client.id(), cx), + Err(AgentStorageError::ScopeMismatch { .. }) + )); + assert!(project.document_is_current(&chat.header.id, client.id(), cx)); + }); + cx.run_until_parked(); + assert!( + repository + .operations() + .iter() + .all(|operation| !matches!(operation, StorageOperation::Save(_))) + ); +} +#[gpui::test] +fn retained_catalog_rows_hold_the_original_accounted_payload_without_cloning_summaries( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let mut chat = snapshot().metadata.clone(); + chat.header.title = Some("Original row".into()); + repository.seed_chat(&chat).unwrap(); + cx.run_until_parked(); + let first = cx.update(|cx| general.catalog_snapshot(cx).unwrap()); + assert!(first.row(1).is_none()); + let row = first.row(0).unwrap(); + assert_eq!(row.scope(), ContextId::General); + let generation = row.generation(); + drop(first); + let mut retained = Vec::new(); + for index in 1..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS as u8 { + let context = general.for_scope(project_scope(index)); + cx.update(|cx| context.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + retained.push(cx.update(|cx| context.catalog_snapshot(cx).unwrap())); + } + cx.update(|cx| { + assert!(matches!( + general.for_scope(project_scope(32)).try_reload_history(cx), + Err(crate::CatalogLoadError::Capacity) + )) + }); + assert_eq!(row.generation(), generation); + assert_eq!(row.summary().title(), "Original row"); + drop(row); + cx.update(|cx| { + general + .for_scope(project_scope(32)) + .try_reload_history(cx) + .unwrap() + }); + cx.run_until_parked(); +} + +#[gpui::test] +fn prepared_payload_handoff_requires_its_original_claim_and_leaves_other_records_available( + cx: &mut TestAppContext, +) { + let first = snapshot().metadata.clone(); + let mut second = first.clone(); + second.header.id = "00000000-0000-0000-0000-000000000002".into(); + let history = crate::PreparedHistory::new(vec![first.clone(), second.clone()]).unwrap(); + let (general, _directory, _, _, _) = scoped_fixture_with_history(cx, Some(history)); + let (other_runtime, _other_directory, _, _, _) = scoped_fixture(cx); + let window = cx.new(|_| ()); + cx.update(|cx| { + assert_eq!(general.catalog_snapshot(cx).unwrap().rows().len(), 2); + let payload = general.history_admission(cx).reserve_payload().unwrap(); + let claim = ownership::owned_claim( + general + .claim_document(first.header.id.clone(), window.entity_id(), cx) + .unwrap(), + ); + assert!(matches!( + other_runtime.take_prepared_chat(&claim, payload.clone(), cx), + Err(crate::ChatOpenError::StaleClaim(_)) + )); + assert!(matches!( + general + .for_scope(project_scope(1)) + .take_prepared_chat(&claim, payload.clone(), cx), + Err(crate::ChatOpenError::Storage( + AgentStorageError::ScopeMismatch { .. } + )) + )); + assert_eq!( + general + .take_prepared_chat(&claim, payload.clone(), cx) + .unwrap() + .unwrap() + .value() + .draft, + first.draft + ); + assert!( + general + .take_prepared_chat(&claim, payload.clone(), cx) + .unwrap() + .is_none() + ); + let other = ownership::owned_claim( + general + .claim_document(second.header.id.clone(), window.entity_id(), cx) + .unwrap(), + ); + assert_eq!( + general + .take_prepared_chat( + &other, + general.history_admission(cx).reserve_payload().unwrap(), + cx + ) + .unwrap() + .unwrap() + .value() + .header + .id, + second.header.id + ); + }); +} + +#[gpui::test] +fn catalog_reload_keeps_unclaimed_prepared_records_after_another_record_is_deleted( + cx: &mut TestAppContext, +) { + let first = snapshot().metadata.clone(); + let mut second = first.clone(); + second.header.id = "00000000-0000-0000-0000-000000000002".into(); + let history = crate::PreparedHistory::new(vec![first.clone(), second.clone()]).unwrap(); + let (general, _directory, _, _, _) = scoped_fixture_with_history(cx, Some(history)); + cx.update(|cx| general.delete_chat(first.header.id.clone(), cx)); + cx.run_until_parked(); + cx.update(|cx| { + let snapshot = general.catalog_snapshot(cx).unwrap(); + assert_eq!(snapshot.rows().len(), 1); + assert_eq!(snapshot.rows()[0].id, second.header.id); + }); +} diff --git a/crates/agent-runtime/src/documents/history_tests/contexts/catalog_capacity.rs b/crates/agent-runtime/src/documents/history_tests/contexts/catalog_capacity.rs new file mode 100644 index 00000000..f42ba662 --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/contexts/catalog_capacity.rs @@ -0,0 +1,291 @@ +//! Catalog generations, shared slots and retained metadata allocation. +use super::*; +use crate::history_admission::HistoryAdmissionError; + +#[gpui::test] +fn retained_catalog_generations_consume_the_installation_limit_until_dropped( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let mut retained = Vec::new(); + for _ in 0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + retained.push(cx.update(|cx| general.catalog_snapshot(cx).unwrap())); + if retained.len() < crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + } + } + let generation = retained.last().unwrap().generation(); + let attempts = repository.operations().len(); + cx.update(|cx| { + assert!(matches!( + general.try_reload_history(cx), + Err(crate::CatalogLoadError::Capacity) + )); + assert_eq!( + general.catalog_snapshot(cx).unwrap().generation(), + generation + ); + assert!(matches!( + general.for_scope(project_scope(1)).try_reload_history(cx), + Err(crate::CatalogLoadError::Capacity) + )); + }); + cx.run_until_parked(); + assert_eq!(repository.operations().len(), attempts); + drop(retained.remove(0)); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx).unwrap().generation()) > generation); +} +#[gpui::test] +fn pending_catalog_requests_share_the_same_installation_byte_capacity(cx: &mut TestAppContext) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + // General's initial scan already owns its worst-case metadata allocation, + // including before the background work's first poll. + let mut admitted = 1; + let mut refused = None; + cx.update(|cx| { + for index in 1..32 { + match general + .for_scope(project_scope(index)) + .try_reload_history(cx) + { + Ok(()) => admitted += 1, + Err(crate::CatalogLoadError::ResidentCapacity { .. }) => { + refused = Some(index); + break; + } + Err(error) => panic!("unexpected catalog refusal: {error}"), + } + } + }); + assert!(admitted > 1 && admitted < 32); + let refused = refused.expect("bounded pending metadata exhausts bytes before32slots"); + cx.run_until_parked(); + assert_eq!( + repository + .operations() + .iter() + .filter(|operation| **operation == StorageOperation::Catalog) + .count(), + admitted + ); + // Each completed result transfers to a smaller immutable retained allocation. + cx.update(|cx| { + general + .for_scope(project_scope(refused)) + .try_reload_history(cx) + .unwrap() + }); + cx.run_until_parked(); + assert!( + cx.update(|cx| general + .for_scope(project_scope(refused)) + .catalog_snapshot(cx)) + .is_some() + ); +} +#[gpui::test] +fn an_old_catalog_load_cannot_replace_a_newer_scope_generation(cx: &mut TestAppContext) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let project = general.for_scope(project_chat().header.scope); + let release = repository.block_next(StorageOperation::Catalog); + cx.update(|cx| project.reload_history(cx)); + cx.run_until_parked(); + let mut chat = project_chat(); + chat.header.title = Some("Current generation".into()); + repository.seed_chat(&chat).unwrap(); + cx.update(|cx| project.reload_history(cx)); + cx.run_until_parked(); + let latest = cx.update(|cx| project.catalog_snapshot(cx).unwrap()); + chat.header.title = Some("Late stale generation".into()); + repository.seed_chat(&chat).unwrap(); + let _ = release.send(Ok(())); + cx.run_until_parked(); + cx.update(|cx| { + let actual = project.catalog_snapshot(cx).unwrap(); + assert_eq!(actual.generation(), latest.generation()); + assert_eq!(actual.rows()[0].title(), "Current generation"); + }); +} + +#[gpui::test] +fn a_retained_catalog_row_keeps_metadata_charged_until_its_last_reference_drops( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let mut saved = snapshot().metadata.clone(); + saved.header.title = Some("界".repeat(512)); + saved.header.name = saved.header.title.clone(); + saved.model = saved.header.title.clone(); + repository.seed_chat(&saved).unwrap(); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + let snapshot = cx.update(|cx| general.catalog_snapshot(cx).unwrap()); + let row = snapshot.row(0).unwrap(); + let second = row.clone(); + drop(snapshot); + cx.update(|cx| general.delete_chat(saved.header.id, cx)); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + let incoming = crate::history_admission::MAX_NORMAL_HISTORY_BYTES - 8 * 1024; + cx.update(|cx| { + assert!(matches!( + admission.reserve_unowned_bytes(incoming, cx), + Err(HistoryAdmissionError::ResidentCapacity { .. }) + )) + }); + drop(row); + cx.update(|cx| assert!(admission.reserve_unowned_bytes(incoming, cx).is_err())); + assert_eq!(second.summary().name.as_ref().unwrap(), &"界".repeat(512)); + drop(second); + cx.update(|cx| drop(admission.reserve_unowned_bytes(incoming, cx).unwrap())); +} + +#[gpui::test] +fn replacing_a_loading_generation_keeps_its_native_read_budget_until_actual_completion( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + let release = repository.block_next(StorageOperation::Catalog); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + let latest = cx.update(|cx| general.catalog_snapshot(cx).unwrap()); + let generation = latest.generation(); + cx.update(|cx| { + assert!(matches!( + admission.reserve_unowned_bytes(56 * 1024 * 1024, cx), + Err(HistoryAdmissionError::ResidentCapacity { .. }) + )) + }); + release.send(Ok(())).unwrap(); + cx.run_until_parked(); + assert_eq!( + cx.update(|cx| general.catalog_snapshot(cx).unwrap().generation()), + generation + ); + cx.update(|cx| { + drop( + admission + .reserve_unowned_bytes(56 * 1024 * 1024, cx) + .unwrap(), + ) + }); +} + +#[gpui::test] +fn catalog_work_observes_the_same_retained_remote_bytes_before_native_start( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + let pending = cx.update(|cx| { + admission + .reserve_unowned_bytes(56 * 1024 * 1024, cx) + .unwrap() + }); + let attempts = repository.operations().len(); + cx.update(|cx| { + assert!(matches!( + general.try_reload_history(cx), + Err(crate::CatalogLoadError::ResidentCapacity { .. }) + )) + }); + cx.run_until_parked(); + assert_eq!(repository.operations().len(), attempts); + drop(pending); + cx.update(|cx| general.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert_eq!(repository.operations().len(), attempts + 1); +} + +#[gpui::test] +fn byte_pressure_reclaims_an_unretained_catalog_payload_before_refusing_shared_history( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let admission = cx.update(|cx| general.history_admission(cx)); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_some()); + let incoming = cx.update(|cx| { + admission + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap() + }); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_none()); + drop(incoming); + cx.update(|cx| general.ensure_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_some()); +} + +#[gpui::test] +fn a_catalog_reload_reclaims_its_unpinned_cache_without_reentering_runtime( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let old = general.for_scope(project_scope(1)); + for index in 0..nocterm_ai::history::MAX_RESTORED { + let mut chat = project_chat(); + chat.header.scope = old.scope(); + chat.header.id = format!("00000000-0000-0000-0000-{index:012x}"); + chat.header.title = Some("界".repeat(512)); + chat.header.name = chat.header.title.clone(); + chat.model = chat.header.title.clone(); + repository.seed_chat(&chat).unwrap(); + } + cx.update(|cx| old.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert_eq!( + cx.update(|cx| old.catalog_snapshot(cx).unwrap().rows().len()), + nocterm_ai::history::MAX_RESTORED + ); + let admission = cx.update(|cx| general.history_admission(cx)); + let remote = cx.update(|cx| { + admission + // Leaves room for exactly one worst-case catalog reload, so the + // second scope fits only if the unpinned first scope is reclaimed. + .reserve_unowned_bytes( + crate::history_admission::MAX_NORMAL_HISTORY_BYTES + - crate::catalog::CATALOG_WORK_BYTES, + cx, + ) + .unwrap() + }); + let next = general.for_scope(project_scope(2)); + cx.update(|cx| next.try_reload_history(cx).unwrap()); + assert!(cx.update(|cx| old.catalog_snapshot(cx)).is_none()); + cx.run_until_parked(); + assert!(cx.update(|cx| next.catalog_snapshot(cx)).is_some()); + drop(remote); +} + +#[gpui::test] +fn byte_pressure_reclaims_every_cold_scope_offered_by_one_catalog_owner(cx: &mut TestAppContext) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let project = general.for_scope(project_scope(1)); + cx.update(|cx| project.ensure_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_some()); + assert!(cx.update(|cx| project.catalog_snapshot(cx)).is_some()); + let admission = cx.update(|cx| general.history_admission(cx)); + let incoming = cx.update(|cx| { + admission + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap() + }); + assert!(cx.update(|cx| general.catalog_snapshot(cx)).is_none()); + assert!(cx.update(|cx| project.catalog_snapshot(cx)).is_none()); + drop(incoming); +} diff --git a/crates/agent-runtime/src/documents/history_tests/environments.rs b/crates/agent-runtime/src/documents/history_tests/environments.rs new file mode 100644 index 00000000..9adfe6b9 --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/environments.rs @@ -0,0 +1,221 @@ +//! Retained Local account sources fence documents, readers and shared budgets. +use super::*; +use nocterm_ai::{local_environment::LocalEnvironmentAccess, storage::AgentStorageError}; +use nocterm_hubs_model::{ + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, + LocalAccountSourceFactory, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, +}; + +fn partition(hub: u8, account: &str) -> LocalAccountPartition { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("same-instance-id").unwrap(), + Fingerprint::from_bytes([hub; 32]), + ), + AccountIdentity::new( + NamespaceId::new(account).unwrap(), + Fingerprint::from_bytes([3; 32]), + ), + )) +} +fn bind(base: &RuntimeHandle, factory: &ScriptedLocalAccountFactory) -> RuntimeHandle { + base.for_environment(LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap()) + .unwrap() +} +fn chat(handle: &RuntimeHandle, title: &str) -> Arc { + let mut metadata = snapshot().metadata.clone(); + metadata.version = 3; + metadata.header.environment = handle.environment().unwrap().clone(); + metadata.header.title = Some(title.into()); + Arc::new(SharedChat { + metadata, + queue: Vec::new(), + }) +} +#[gpui::test] +fn identical_ids_in_different_authenticated_hubs_have_independent_writers_and_catalogs( + cx: &mut TestAppContext, +) { + let (base, _directory, repository, _, _) = contexts::scoped_fixture(cx); + let first = ScriptedLocalAccountFactory::default(); + let second = ScriptedLocalAccountFactory::default(); + first.sign_in(partition(1, "same-account-id")); + second.sign_in(partition(2, "same-account-id")); + let a = bind(&base, &first); + let b = bind(&base, &second); + let a_chat = chat(&a, "Hub A"); + let b_chat = chat(&b, "Hub B"); + let a_client = ProbeClient::new(cx, a_chat.clone()); + let b_client = ProbeClient::new(cx, b_chat.clone()); + cx.update(|cx| { + ownership::register_bound(&a, a_client.clone(), cx).unwrap(); + ownership::register_bound(&b, b_client.clone(), cx).unwrap(); + a.save_chat(a_chat.clone(), a_client.id(), 1, cx).unwrap(); + b.save_chat(b_chat.clone(), b_client.id(), 1, cx).unwrap(); + }); + cx.run_until_parked(); + cx.update(|cx| { + a.reload_history(cx); + b.reload_history(cx); + }); + cx.run_until_parked(); + cx.update(|cx| { + assert_eq!(a.catalog_snapshot(cx).unwrap().rows()[0].title(), "Hub A"); + assert_eq!(b.catalog_snapshot(cx).unwrap().rows()[0].title(), "Hub B"); + a.delete_chat(a_chat.header.id.clone(), cx); + }); + cx.run_until_parked(); + assert!( + repository + .encoded_in_environment(&a.chat_scope().unwrap(), &a_chat.header.id) + .is_none() + ); + assert!( + repository + .encoded_in_environment(&b.chat_scope().unwrap(), &b_chat.header.id) + .is_some() + ); + assert!( + repository.encoded(&a_chat.header.id).is_none(), + "hub Local never writes ThisComputer" + ); +} +#[gpui::test] +async fn deferred_reader_refuses_the_old_owner_after_a_b_a_and_does_not_retarget( + cx: &mut TestAppContext, +) { + let (base, _directory, repository, _, _) = contexts::scoped_fixture(cx); + let factory = ScriptedLocalAccountFactory::default(); + factory.sign_in(partition(1, "alice")); + let a = bind(&base, &factory); + let original = chat(&a, "A original"); + repository.seed_chat(&original.metadata).unwrap(); + let reader = cx.update(|cx| a.history_reader(cx)); + let release = repository.block_next(StorageOperation::Load(original.header.id.clone())); + let pending = cx + .background_executor + .spawn(async move { reader.prepare_load(original.header.id.clone()).await }); + cx.run_until_parked(); + factory.sign_in(partition(1, "bob")); + let b = bind(&base, &factory); + factory.sign_in(partition(1, "alice")); + let new_a = bind(&base, &factory); + release.send(Ok(())).unwrap(); + let (loaded, permit) = pending.await; + assert!(matches!(loaded, Err(AgentStorageError::AccountSource(_)))); + drop(permit); + assert!(a.check_current().is_err()); + assert!(b.check_current().is_err()); + assert!(new_a.check_current().is_ok()); + let attempts = repository.operations().len(); + let (loaded, permit) = cx + .update(|cx| a.history_reader(cx)) + .prepare_load(snapshot().header.id.clone()) + .await; + assert!(loaded.is_err()); + drop(permit); + assert_eq!( + repository.operations().len(), + attempts, + "stale reads fail before repository I/O" + ); +} +#[gpui::test] +fn stale_claims_and_native_startup_cannot_acquire_the_replacement_account(cx: &mut TestAppContext) { + let (base, _directory, _, connector, _) = contexts::scoped_fixture(cx); + let factory = ScriptedLocalAccountFactory::default(); + factory.sign_in(partition(1, "alice")); + let old = bind(&base, &factory); + let client = ProbeClient::new(cx, chat(&old, "original")); + let claim = cx.update(|cx| { + ownership::owned_claim( + old.claim_document(snapshot().header.id.clone(), client.id(), cx) + .unwrap(), + ) + }); + factory.sign_in(partition(1, "bob")); + cx.update(|cx| { + assert!(old.commit_document(claim, client.clone(), cx).is_err()); + assert!(old.request_activation(client.clone(), cx).is_err()); + assert!( + old.register_bridge(&(client.clone() as Client), cx) + .is_err() + ); + }); + cx.run_until_parked(); + assert!(connector.requests().is_empty()); + let current = bind(&base, &factory); + cx.update(|cx| { + let claim = ownership::owned_claim( + current + .claim_document(snapshot().header.id.clone(), client.id(), cx) + .unwrap(), + ); + assert_eq!(claim.key().environment(), current.environment().unwrap()); + }); +} +#[gpui::test] +fn all_account_context_views_consume_one_installation_catalog_budget(cx: &mut TestAppContext) { + let (base, _directory, repository, _, _) = contexts::scoped_fixture(cx); + cx.run_until_parked(); + let mut retained = Vec::new(); + let mut factories = Vec::new(); + for index in 0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS as u8 { + let factory = ScriptedLocalAccountFactory::default(); + factory.sign_in(partition(index, "alice")); + let handle = bind(&base, &factory); + cx.update(|cx| handle.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + retained.push(cx.update(|cx| handle.catalog_snapshot(cx).unwrap())); + factories.push(factory); + } + let attempts = repository.operations().len(); + cx.update(|cx| { + assert!(matches!( + base.try_reload_history(cx), + Err(crate::CatalogLoadError::Capacity) + )) + }); + assert_eq!(repository.operations().len(), attempts); + drop(retained.remove(0)); + cx.update(|cx| base.try_reload_history(cx).unwrap()); + cx.run_until_parked(); + assert!(cx.update(|cx| base.catalog_snapshot(cx)).is_some()); +} + +#[gpui::test] +fn resident_history_budget_is_shared_across_accounts_and_windows(cx: &mut TestAppContext) { + const MIB: usize = 1024 * 1024; + let (base, _directory, _, _, _) = contexts::scoped_fixture(cx); + let a_factory = ScriptedLocalAccountFactory::default(); + let b_factory = ScriptedLocalAccountFactory::default(); + a_factory.sign_in(partition(1, "alice")); + b_factory.sign_in(partition(2, "alice")); + let a = bind(&base, &a_factory); + let b = bind(&base, &b_factory); + let first = ProbeClient::new(cx, chat(&a, "A")); + let requester = ProbeClient::new(cx, chat(&b, "B")); + first + .history + .set(Some(crate::HistoryUsage::new(48 * MIB, 1, true))); + first.allow_history_eviction.set(true); + requester + .history + .set(Some(crate::HistoryUsage::new(0, 2, false))); + cx.update(|cx| { + ownership::register_bound(&a, first.clone(), cx).unwrap(); + ownership::register_bound(&b, requester.clone(), cx).unwrap(); + assert!(matches!( + b.admit_history(requester.id(), 24 * MIB, cx), + Err(AgentStorageError::ResidentCapacity { .. }) + )); + first + .history + .set(Some(crate::HistoryUsage::new(48 * MIB, 1, false))); + b.admit_history(requester.id(), 24 * MIB, cx).unwrap(); + assert!(first.history.get().is_none()); + }); +} diff --git a/crates/agent-runtime/src/documents/history_tests/local_launch.rs b/crates/agent-runtime/src/documents/history_tests/local_launch.rs new file mode 100644 index 00000000..a9b9070e --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/local_launch.rs @@ -0,0 +1,236 @@ +//! Scoped documents share the ordinary local agent policy and sign-in lifecycle. +use super::*; +use nocterm_ai::{ + AgentConnection, AgentInfo, AgentResourceSettings, AiSettings, SandboxMode, + history::ContextId, + model::auth::{AuthMethod, TerminalSetup}, + testing::ScriptedWorkdirResolver, +}; + +struct LocalLaunch { + runtime: RuntimeHandle, + settings: nocterm_store::SettingsHandle, + directory: tempfile::TempDir, + connector: Arc, + resolver: Arc, +} +struct RunningAgent { + client: Rc, + _events: async_channel::Sender, +} +impl RunningAgent { + fn connection(&self) -> u64 { + self.client + .events + .borrow() + .iter() + .find_map(|event| match event { + SessionEvent::Connected { connection, .. } => Some(*connection), + _ => None, + }) + .expect("ordinary connection must be delivered") + } +} +impl LocalLaunch { + fn new(mode: SandboxMode, cx: &mut TestAppContext) -> Self { + let directory = tempfile::tempdir().unwrap(); + let connector = Arc::new(ScriptedConnector::default()); + let resolver = Arc::new(ScriptedWorkdirResolver::accept_requests()); + let repository = Arc::new(InMemoryAgentRepository::default()); + let storage = + AgentStorage::prepared(repository.clone(), repository, Ok(Default::default())); + let (runtime, settings) = cx.update(|cx| { + let mut services = runtime_services( + connector.clone(), + storage, + directory.path(), + resolver.clone(), + cx, + ); + services.terminal_auth = true; + let settings = services.settings.clone(); + let runtime = RuntimeHandle::new(services, cx); + settings + .update::(cx, move |settings| { + settings.sandbox = mode; + settings.resources = resources(); + }) + .detach(); + (runtime, settings) + }); + Self { + runtime, + settings, + directory, + connector, + resolver, + } + } + fn start(&self, scope: ContextId, cx: &mut TestAppContext) -> RunningAgent { + let handle = self.runtime.for_scope(scope); + let mut metadata = snapshot().metadata.clone(); + metadata.version = 2; + metadata.header.scope = scope; + let client = ProbeClient::new( + cx, + Arc::new(SharedChat { + metadata, + queue: vec![], + }), + ); + let (events, receiver) = async_channel::unbounded(); + self.connector.push(Ok(AgentConnection { + commands: closing::BlockedCommands::new(false), + events: receiver, + info: AgentInfo { + name: "ordinary local agent".into(), + version: "1".into(), + capabilities: Default::default(), + auth_methods: vec![auth_method()], + }, + })); + cx.update(|cx| { + ownership::register_bound(&handle, client.clone(), cx).unwrap(); + handle + .connect( + client.clone(), + client.chat.borrow().header.id.clone(), + launch(), + cx, + ) + .unwrap(); + }); + cx.run_until_parked(); + let running = RunningAgent { + client, + _events: events, + }; + running.connection(); + running + } +} +fn resources() -> AgentResourceSettings { + AgentResourceSettings { + memory_high_mb: 128, + memory_max_mb: 256, + memory_swap_max_mb: 0, + tasks_max: 32, + } +} +fn launch() -> AgentLaunch { + AgentLaunch { + id: "codex".into(), + name: "ordinary local agent".into(), + command: "unused".into(), + args: vec!["--base".into()], + env: Default::default(), + inherit_env: vec![], + hub: None, + } +} +fn auth_method() -> AuthMethod { + AuthMethod::terminal( + "login", + "Test login", + TerminalSetup { + args: vec!["login".into()], + env: [("TEST_AUTH_METHOD".into(), "1".into())].into(), + }, + ) +} +fn scopes() -> [ContextId; 2] { + [ContextId::General, contexts::project_chat().header.scope] +} + +#[gpui::test] +fn workspace_and_general_chats_offer_the_same_ordinary_terminal_sign_in(cx: &mut TestAppContext) { + let fixture = LocalLaunch::new(SandboxMode::Off, cx); + for scope in scopes() { + let running = fixture.start(scope, cx); + cx.update(|cx| { + let request = fixture + .runtime + .for_scope(scope) + .terminal_auth_request(running.connection(), &auth_method(), cx) + .expect("workspace scope must not suppress the ordinary sign-in command"); + assert_eq!(request.program, "unused"); + assert_eq!(request.args, ["--base", "login"]); + assert_eq!( + request.env.get("TEST_AUTH_METHOD").map(String::as_str), + Some("1") + ); + assert_eq!(request.cwd, fixture.directory.path().join("work")); + assert_eq!(running.client.state(cx).unwrap().origin().scope(), scope); + }); + } + assert!( + fixture + .connector + .requests() + .iter() + .all(|request| request.terminal_auth) + ); +} + +#[gpui::test] +fn workspace_and_general_chats_apply_the_same_configured_sandbox_and_resource_limits( + cx: &mut TestAppContext, +) { + for mode in [SandboxMode::Off, SandboxMode::Workspace] { + let fixture = LocalLaunch::new(mode, cx); + let _running = scopes().map(|scope| fixture.start(scope, cx)); + let preparation = fixture.resolver.requests(); + let requests = fixture.connector.requests(); + assert_eq!(preparation.len(), 2); + assert_eq!(requests.len(), 2); + for (prepared, connected) in preparation.iter().zip(&requests) { + assert_eq!(prepared.isolation.is_some(), mode == SandboxMode::Workspace); + assert_eq!(connected.sandbox.is_some(), mode == SandboxMode::Workspace); + assert_eq!(connected.resources, resources()); + assert_eq!( + connected.working_directory, + fixture.directory.path().join("work") + ); + } + assert_eq!(requests[0].sandbox, requests[1].sandbox); + } +} + +#[gpui::test] +fn changing_the_existing_isolation_setting_retires_workspace_and_general_connections( + cx: &mut TestAppContext, +) { + let fixture = LocalLaunch::new(SandboxMode::Off, cx); + let running = scopes().map(|scope| fixture.start(scope, cx)); + cx.update(|cx| { + fixture + .settings + .update::(cx, |settings| { + settings.sandbox = SandboxMode::Workspace; + }) + .detach(); + fixture.runtime.settings_changed(cx); + }); + let requests = fixture.connector.requests(); + assert_eq!(requests.len(), 2); + assert!( + requests + .iter() + .all(|request| request.cancellation.is_cancelled()) + ); + cx.update(|cx| { + for agent in &running { + let key = agent.connection(); + assert!(agent.client.events.borrow().iter().any(|event| { + matches!(event, SessionEvent::Stopped { connection, .. } if *connection == key) + })); + let state = agent.client.state(cx).unwrap(); + assert!( + fixture + .runtime + .for_scope(state.origin().scope()) + .document_is_current(state.origin().key().id(), agent.client.id(), cx,) + ); + } + }); +} diff --git a/crates/agent-runtime/src/documents/history_tests/ownership.rs b/crates/agent-runtime/src/documents/history_tests/ownership.rs new file mode 100644 index 00000000..641a5ca6 --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/ownership.rs @@ -0,0 +1,225 @@ +//! Reservations, duplicate windows and explicit replacement use immutable scoped claims. +use super::contexts::{project_chat, scoped_fixture}; +use super::*; +use nocterm_ai::{history::ContextId, storage::AgentStorageError}; + +pub(super) fn register( + runtime: &mut Runtime, + client: Client, + cx: &mut Context, +) -> Result<(), crate::ChatOpenError> { + ChatStore::require_client_scope(ContextId::General.into(), &client, cx)?; + let key = client + .state(cx) + .ok_or(crate::ChatOpenError::Closed)? + .origin() + .key() + .clone(); + if runtime.scoped_document_is_current(&key, client.id()) { + return Ok(()); + } + let window = client.id(); // The public writer fixture's marker stands in for its root window. + let claim = owned_claim(runtime.claim_document(key, window, None)?); + runtime.commit_document(claim, client, cx) +} +pub(super) fn register_bound( + handle: &RuntimeHandle, + client: Client, + cx: &mut App, +) -> Result<(), crate::ChatOpenError> { + let state = client.state(cx).ok_or(crate::ChatOpenError::Closed)?; + let claim = + owned_claim(handle.claim_document(state.origin().key().id().into(), client.id(), cx)?); + handle.commit_document(claim, client, cx) +} +pub(super) fn replace( + runtime: &Entity, + old: EntityId, + replacement: Client, + cx: &mut TestAppContext, +) { + runtime.update(cx, |runtime, cx| { + let key = replacement.state(cx).unwrap().origin().key().clone(); + let claim = owned_claim(runtime.claim_document(key, old, Some(old)).unwrap()); + runtime.commit_document(claim, replacement, cx).unwrap(); + }); +} + +pub(super) fn owned_claim(outcome: crate::DocumentClaimOutcome) -> crate::DocumentClaim { + match outcome { + crate::DocumentClaimOutcome::Owned(claim) => claim, + _ => panic!("expected a new document claim"), + } +} +#[gpui::test] +fn document_reservations_prevent_duplicate_opening_and_drop_rolls_back_only_the_original_key( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let first_window = cx.new(|_| ()); + let second_window = cx.new(|_| ()); + let id = snapshot().header.id.clone(); + cx.update(|cx| { + let original = owned_claim(general.claim_document(id.clone(), first_window.entity_id(), cx).unwrap()); + assert!(matches!(general.claim_document(id.clone(), second_window.entity_id(), cx).unwrap(), crate::DocumentClaimOutcome::AlreadyOpen { window } if window == first_window.entity_id())); + let other = owned_claim(project.claim_document(id.clone(), second_window.entity_id(), cx).unwrap()); + assert_eq!(other.key().scope(), project.scope()); + drop(original); + let replacement = owned_claim(general.claim_document(id.clone(), second_window.entity_id(), cx).unwrap()); + assert!(matches!(project.claim_document(id, first_window.entity_id(), cx).unwrap(), crate::DocumentClaimOutcome::AlreadyOpen { window } if window == second_window.entity_id())); + drop(replacement); + drop(other); + }); +} +#[gpui::test] +fn committed_claims_keep_the_original_window_and_refuse_wrong_registry_or_scope( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let (another_runtime, _other_directory, _, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let window = cx.new(|_| ()); + let client = ProbeClient::new(cx, snapshot()); + let id = snapshot().header.id.clone(); + cx.update(|cx| { + let claim = owned_claim(general.claim_document(id.clone(), window.entity_id(), cx).unwrap()); + assert!(matches!(another_runtime.commit_document(claim, client.clone(), cx), Err(crate::ChatOpenError::StaleClaim(_)))); + let claim = owned_claim(general.claim_document(id.clone(), window.entity_id(), cx).unwrap()); + assert!(matches!(project.commit_document(claim, client.clone(), cx), Err(crate::ChatOpenError::Storage(AgentStorageError::ScopeMismatch { .. })))); + let claim = owned_claim(general.claim_document(id.clone(), window.entity_id(), cx).unwrap()); + general.commit_document(claim, client.clone(), cx).unwrap(); + assert!(general.document_is_current(&id, client.id(), cx)); + assert!(matches!(general.claim_document(id, client.id(), cx).unwrap(), crate::DocumentClaimOutcome::AlreadyOpen { window: original } if original == window.entity_id())); + }); +} +#[gpui::test] +fn restart_requires_the_exact_live_owner_and_cancelled_replacement_restores_it( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let window = cx.new(|_| ()); + let old = ProbeClient::new(cx, snapshot()); + let replacement = ProbeClient::new(cx, snapshot()); + let id = snapshot().header.id.clone(); + cx.update(|cx| { + let claim = owned_claim( + general + .claim_document(id.clone(), window.entity_id(), cx) + .unwrap(), + ); + general.commit_document(claim, old.clone(), cx).unwrap(); + assert!(matches!( + general.replace_document(id.clone(), replacement.id(), window.entity_id(), cx), + Err(crate::ChatOpenError::StaleClaim(_)) + )); + let aborted = owned_claim( + general + .replace_document(id.clone(), old.id(), window.entity_id(), cx) + .unwrap(), + ); + assert!(!general.document_is_current(&id, old.id(), cx)); + assert!(matches!( + general.replace_document(id.clone(), old.id(), window.entity_id(), cx), + Err(crate::ChatOpenError::StaleClaim(_)) + )); + drop(aborted); + assert!(general.document_is_current(&id, old.id(), cx)); + let admitted = owned_claim( + general + .replace_document(id.clone(), old.id(), window.entity_id(), cx) + .unwrap(), + ); + general + .commit_document(admitted, replacement.clone(), cx) + .unwrap(); + general.unregister_document(old.id(), cx).unwrap(); + assert!(general.document_is_current(&id, replacement.id(), cx)); + assert!(matches!( + general.replace_document(id, old.id(), window.entity_id(), cx), + Err(crate::ChatOpenError::StaleClaim(_)) + )); + }); +} +#[gpui::test] +fn deletion_seals_a_pending_claim_without_sealing_a_same_uuid_in_another_scope( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let project = general.for_scope(project_chat().header.scope); + let window = cx.new(|_| ()); + let client = ProbeClient::new(cx, snapshot()); + let id = snapshot().header.id.clone(); + cx.update(|cx| { + let pending = owned_claim( + general + .claim_document(id.clone(), window.entity_id(), cx) + .unwrap(), + ); + general.delete_chat(id.clone(), cx); + assert!(matches!( + general.commit_document(pending, client.clone(), cx), + Err(crate::ChatOpenError::Deleted(_)) + )); + assert!(matches!( + general + .claim_document(id.clone(), window.entity_id(), cx) + .unwrap(), + crate::DocumentClaimOutcome::Deleted + )); + drop(owned_claim( + project.claim_document(id, window.entity_id(), cx).unwrap(), + )); + }); +} +#[gpui::test] +fn a_closed_owner_can_be_reopened_before_its_late_retirement_arrives(cx: &mut TestAppContext) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let window = cx.new(|_| ()); + let old = ProbeClient::new(cx, snapshot()); + let next = ProbeClient::new(cx, snapshot()); + let id = snapshot().header.id.clone(); + cx.update(|cx| { + let claim = owned_claim( + general + .claim_document(id.clone(), window.entity_id(), cx) + .unwrap(), + ); + general.commit_document(claim, old.clone(), cx).unwrap(); + old.close(); + let claim = owned_claim( + general + .claim_document(id.clone(), window.entity_id(), cx) + .unwrap(), + ); + general.commit_document(claim, next.clone(), cx).unwrap(); + general.unregister_document(old.id(), cx).unwrap(); + assert!(general.document_is_current(&id, next.id(), cx)); + }); +} +#[gpui::test] +async fn shutdown_captures_the_original_document_while_a_replacement_is_reserved( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let window = cx.new(|_| ()); + let old = ProbeClient::new(cx, snapshot()); + let id = snapshot().header.id.clone(); + let (replacement, final_work) = cx.update(|cx| { + let claim = owned_claim( + general + .claim_document(id.clone(), window.entity_id(), cx) + .unwrap(), + ); + general.commit_document(claim, old.clone(), cx).unwrap(); + let replacement = owned_claim( + general + .replace_document(id.clone(), old.id(), window.entity_id(), cx) + .unwrap(), + ); + (replacement, general.capture_shutdown(cx)) + }); + final_work.await.unwrap(); + assert_eq!(repository.transcript(&id).unwrap().draft, snapshot().draft); + drop(replacement); +} diff --git a/crates/agent-runtime/src/documents/history_tests/preferences.rs b/crates/agent-runtime/src/documents/history_tests/preferences.rs index 5cca340d..f52ec9a9 100644 --- a/crates/agent-runtime/src/documents/history_tests/preferences.rs +++ b/crates/agent-runtime/src/documents/history_tests/preferences.rs @@ -100,14 +100,14 @@ async fn captured_chat_failures_preserve_original_primary_and_independent_diagno let (runtime, _directory, repository) = fixture(cx); let first = snapshot(); let mut metadata = first.metadata.clone(); - metadata.id = "00000000-0000-0000-0000-000000000002".into(); + metadata.header.id = "00000000-0000-0000-0000-000000000002".into(); let second = Arc::new(SharedChat { metadata, queue: Vec::new(), }); for chat in [&first, &second] { repository.reject( - StorageOperation::Save(chat.id.clone()), + StorageOperation::Save(chat.header.id.clone()), AgentStorageError::Interrupted, ); let client = ProbeClient::new(cx, chat.clone()); @@ -157,6 +157,6 @@ async fn captured_chat_failures_preserve_original_primary_and_independent_diagno panic!("expected aggregate clone") }; assert!(Arc::ptr_eq(diagnostic, cloned_diagnostic)); - assert!(repository.encoded(&first.id).is_none()); - assert!(repository.encoded(&second.id).is_none()); + assert!(repository.encoded(&first.header.id).is_none()); + assert!(repository.encoded(&second.header.id).is_none()); } diff --git a/crates/agent-runtime/src/documents/history_tests/residency.rs b/crates/agent-runtime/src/documents/history_tests/residency.rs new file mode 100644 index 00000000..97bee38c --- /dev/null +++ b/crates/agent-runtime/src/documents/history_tests/residency.rs @@ -0,0 +1,504 @@ +//! One installation admits hydration across original contexts and windows. +use super::{contexts::scoped_fixture, *}; +use nocterm_ai::{ + history::{ContextId, MAX_RESIDENT_BYTES, SavedChat}, + storage::AgentStorageError, +}; +const MIB: usize = 1024 * 1024; +use crate::{ + HistoryUsage, + history_admission::{HistoryAdmissionError, testing::HistoryProbe}, +}; + +fn client_in(scope: ContextId, cx: &mut TestAppContext) -> Rc { + let mut metadata = snapshot().metadata.clone(); + metadata.version = 2; + metadata.header.scope = scope; + ProbeClient::new( + cx, + Arc::new(SharedChat { + metadata, + queue: Vec::new(), + }), + ) +} + +#[gpui::test] +fn transferring_a_unique_buffer_releases_only_its_unused_reservation(cx: &mut TestAppContext) { + let admission = HistoryProbe::installation(); + let reserved = cx.update(|cx| admission.reserve_unowned_bytes(32 * MIB, cx).unwrap()); + let retained = HistoryProbe::transfer_buffer(&admission, reserved, MIB).unwrap(); + assert_eq!(retained.bytes(), MIB); + cx.update(|cx| drop(admission.reserve_unowned_bytes(60 * MIB, cx).unwrap())); +} + +#[gpui::test] +fn a_shared_buffer_keeps_its_original_charge_until_shared_owners_release_it( + cx: &mut TestAppContext, +) { + let admission = HistoryProbe::installation(); + let reserved = cx.update(|cx| admission.reserve_unowned_bytes(32 * MIB, cx).unwrap()); + let original = reserved.clone(); + let retained = HistoryProbe::transfer_buffer(&admission, reserved, MIB).unwrap(); + assert_eq!(retained.bytes(), 32 * MIB); + cx.update(|cx| { + assert!(matches!( + admission.reserve_unowned_bytes(40 * MIB, cx), + Err(HistoryAdmissionError::ResidentCapacity { .. }) + )) + }); + drop(original); + let retained = HistoryProbe::transfer_buffer(&admission, retained, MIB).unwrap(); + assert_eq!(retained.bytes(), MIB); + cx.update(|cx| drop(admission.reserve_unowned_bytes(40 * MIB, cx).unwrap())); +} + +#[gpui::test] +fn buffer_transfer_refuses_foreign_allocations_and_growth_without_releasing_other_owners( + cx: &mut TestAppContext, +) { + let admission = HistoryProbe::installation(); + let foreign_owner = HistoryProbe::installation(); + let foreign = cx.update(|cx| foreign_owner.reserve_unowned_bytes(MIB, cx).unwrap()); + let retained = foreign.clone(); + assert!(matches!( + HistoryProbe::transfer_buffer(&admission, foreign, MIB / 2), + Err(HistoryAdmissionError::ForeignAllocation) + )); + assert!( + matches!(HistoryProbe::transfer_buffer(&foreign_owner, retained.clone(), MIB * 2), Err(HistoryAdmissionError::AllocationGrowth { reserved, requested }) if reserved==MIB && requested==MIB*2) + ); + cx.update(|cx| { + assert!( + foreign_owner + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .is_err() + ) + }); + drop(retained); + cx.update(|cx| { + drop( + foreign_owner + .reserve_unowned_bytes(crate::history_admission::MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap(), + ) + }); +} +fn scope(index: u8) -> ContextId { + serde_json::from_value(serde_json::json!({"kind":"project", "id": format!("{index:032x}")})) + .unwrap() +} +#[gpui::test] +fn hydration_capacity_is_shared_by_contexts_and_shown_chats_in_other_windows_are_protected( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let a = general.for_scope(scope(1)); + let b = general.for_scope(scope(2)); + let c = general.for_scope(scope(3)); + let first = client_in(a.scope(), cx); + let second = client_in(b.scope(), cx); + let requester = client_in(c.scope(), cx); + first + .history + .set(Some(crate::HistoryUsage::new(24 * MIB, 1, true))); + first.allow_history_eviction.set(true); + second + .history + .set(Some(crate::HistoryUsage::new(24 * MIB, 2, false))); + requester + .history + .set(Some(crate::HistoryUsage::new(0, 3, false))); + cx.update(|cx| { + ownership::register_bound(&a, first.clone(), cx).unwrap(); + ownership::register_bound(&b, second.clone(), cx).unwrap(); + ownership::register_bound(&c, requester.clone(), cx).unwrap(); + assert!(matches!( + c.admit_history(requester.id(), 24 * MIB, cx), + Err(AgentStorageError::ResidentCapacity { .. }) + )); + assert_eq!( + requester.history_reads.get(), + 0, + "requester projection would reenter its borrowed entity" + ); + assert!(first.history.get().is_some()); + assert!(second.history.get().is_some()); + second.allow_history_eviction.set(true); + c.admit_history(requester.id(), 24 * MIB, cx).unwrap(); + assert!(first.history.get().is_some()); + assert!(second.history.get().is_none()); + assert!(matches!( + a.admit_history(requester.id(), 1, cx), + Err(AgentStorageError::ScopeMismatch { .. }) + )); + }); +} +#[gpui::test] +fn remaining_prepared_payloads_count_until_their_claimed_handoff(cx: &mut TestAppContext) { + let mut prepared = snapshot().metadata.clone(); + prepared.draft = Some("p".repeat(MIB)); + let history = crate::PreparedHistory::new(vec![prepared.clone()]).unwrap(); + let retained = history.records()[0].resident_bytes(); + let (general, _directory, _, _, _) = contexts::scoped_fixture_with_history(cx, Some(history)); + let mut other = SavedChat::for_identity( + "codex".into(), + "00000000-0000-0000-0000-000000000002".into(), + 1, + ); + other.header.scope = ContextId::General; + let client = ProbeClient::new( + cx, + Arc::new(SharedChat { + metadata: other, + queue: Vec::new(), + }), + ); + cx.update(|cx| { + ownership::register_bound(&general, client.clone(), cx).unwrap(); + let incoming = MAX_RESIDENT_BYTES - retained + 64 * 1024; + assert!(matches!( + general.admit_history(client.id(), incoming, cx), + Err(AgentStorageError::ResidentCapacity { .. }) + )); + let claim = ownership::owned_claim( + general + .claim_document(prepared.header.id, client.id(), cx) + .unwrap(), + ); + let slot = general.history_admission(cx).reserve_payload().unwrap(); + drop(general.take_prepared_chat(&claim, slot, cx).unwrap()); + general.admit_history(client.id(), incoming, cx).unwrap(); + }); +} + +#[gpui::test] +fn detached_active_and_latest_writer_payloads_remain_in_hydration_admission( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + let mut first = snapshot().metadata.clone(); + first + .entries + .push(nocterm_ai::thread::Entry::Agent("a".repeat(MIB))); + let first = Arc::new(SharedChat { + metadata: first, + queue: Vec::new(), + }); + let mut second = first.metadata.clone(); + second + .entries + .push(nocterm_ai::thread::Entry::Agent("b".repeat(MIB))); + let second = Arc::new(SharedChat { + metadata: second, + queue: Vec::new(), + }); + let retained = first.retained_bytes().unwrap() + second.retained_bytes().unwrap(); + let writer_client = ProbeClient::new(cx, first.clone()); + let project = general.for_scope(scope(1)); + let requester = client_in(project.scope(), cx); + let release = repository.block_next(StorageOperation::Save(first.header.id.clone())); + cx.update(|cx| { + ownership::register_bound(&general, writer_client.clone(), cx).unwrap(); + ownership::register_bound(&project, requester.clone(), cx).unwrap(); + general + .save_chat(first.clone(), writer_client.id(), 1, cx) + .unwrap(); + }); + cx.run_until_parked(); + cx.update(|cx| { + *writer_client.chat.borrow_mut() = second.clone(); + writer_client.revision.set(2); + general + .save_chat(second, writer_client.id(), 2, cx) + .unwrap(); + general.unregister_document(writer_client.id(), cx).unwrap(); + assert!(matches!( + project.admit_history( + requester.id(), + MAX_RESIDENT_BYTES - retained + 64 * 1024, + cx + ), + Err(AgentStorageError::ResidentCapacity { .. }) + )); + }); + drop(first); + drop(writer_client); + release.send(Ok(())).unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + project + .admit_history( + requester.id(), + MAX_RESIDENT_BYTES - retained + 64 * 1024, + cx, + ) + .unwrap() + }); +} + +#[gpui::test] +fn remote_admission_evicts_hidden_local_history_and_local_admission_evicts_remote_history( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let protected = client_in(general.scope(), cx); + let hidden_scope = general.for_scope(scope(1)); + let hidden = client_in(hidden_scope.scope(), cx); + protected + .history + .set(Some(HistoryUsage::new(24 * MIB, 1, true))); + hidden + .history + .set(Some(HistoryUsage::new(24 * MIB, 2, false))); + hidden.allow_history_eviction.set(true); + let remote = HistoryProbe::default(); + cx.update(|cx| { + ownership::register_bound(&general, protected.clone(), cx).unwrap(); + ownership::register_bound(&hidden_scope, hidden.clone(), cx).unwrap(); + let admission = general.history_admission(cx); + let registration = admission.register(remote.participant()).unwrap(); + let buffer = admission + .reserve_bytes(®istration, 24 * MIB, cx) + .unwrap(); + assert!(hidden.history.get().is_none()); + assert!(protected.history.get().is_some()); + remote.set_history(HistoryUsage::new(24 * MIB, 3, false), true); + drop(buffer); + hidden_scope + .admit_history(hidden.id(), 24 * MIB, cx) + .unwrap(); + assert!(remote.history().is_none()); + assert_eq!(remote.evictions(), 1); + assert!(protected.history.get().is_some()); + }); +} + +#[gpui::test] +fn pending_remote_buffer_clones_stay_charged_until_the_last_owner_drops(cx: &mut TestAppContext) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let client = client_in(general.scope(), cx); + cx.update(|cx| { + ownership::register_bound(&general, client.clone(), cx).unwrap(); + let admission = general.history_admission(cx); + let buffer = admission.reserve_unowned_bytes(40 * MIB, cx).unwrap(); + let pending = buffer.clone(); + drop(buffer); + assert!(matches!( + general.admit_history(client.id(), 32 * MIB, cx), + Err(AgentStorageError::ResidentCapacity { .. }) + )); + drop(pending); + general.admit_history(client.id(), 32 * MIB, cx).unwrap(); + }); +} + +#[gpui::test] +fn local_and_remote_payloads_reclaim_each_other_through_one_installation_cap( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let remote = HistoryProbe::default(); + remote.set_history(HistoryUsage::new(0, 1, false), true); + cx.update(|cx| { + let admission = general.history_admission(cx); + let _registration = admission.register(remote.participant()).unwrap(); + for _ in 1..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + remote.retain_payload(admission.reserve_payload().unwrap()); + } + let payload = admission.reserve_payload_reclaiming(None, cx).unwrap(); + assert!( + general.catalog_snapshot(cx).is_none(), + "oldest unpinned Local catalog was reclaimed" + ); + remote.retain_payload(payload); + assert_eq!( + remote.payload_count(), + crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS + ); + general.try_reload_history(cx).unwrap(); + assert_eq!( + remote.payload_count(), + crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS - 1, + "Local load reclaimed a remote payload" + ); + }); + cx.run_until_parked(); + assert!(cx.update(|cx| general.catalog_snapshot(cx).is_some())); +} + +#[gpui::test] +fn history_eviction_callbacks_run_without_a_budget_borrow_and_nested_admission_refuses( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + let remote = HistoryProbe::default(); + remote.set_history(HistoryUsage::new(40 * MIB, 1, false), true); + cx.update(|cx| { + let admission = general.history_admission(cx); + let _registration = admission.register(remote.participant()).unwrap(); + let nested = admission.clone(); + let called = Rc::new(Cell::new(false)); + let observed = called.clone(); + remote.on_evict(Rc::new(move |cx| { + // Registration may mutate budget state, while recursive eviction + // decisions must refuse instead of admitting uncounted work. + let probe = HistoryProbe::default(); + let _registration = nested.register(probe.participant()).unwrap(); + assert!(matches!( + nested.reserve_unowned_bytes(1, cx), + Err(HistoryAdmissionError::Reentrant) + )); + observed.set(true); + })); + let _buffer = admission.reserve_unowned_bytes(40 * MIB, cx).unwrap(); + assert!(called.get()); + }); +} + +#[gpui::test] +fn unchanged_payload_offers_stop_reclamation_even_when_the_callback_reports_success( + cx: &mut TestAppContext, +) { + use crate::history_admission::{HistoryAdmissionError, testing::HistoryProbe}; + let admission = HistoryProbe::installation(); + let neighbor = HistoryProbe::default(); + neighbor.set_history(crate::HistoryUsage::new(0, 0, false), true); + neighbor.report_payload_success_without_release(); + let _registration = admission.register(neighbor.participant()).unwrap(); + for _ in 0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS { + neighbor.retain_payload(admission.reserve_payload().unwrap()); + } + cx.update(|cx| { + assert!(matches!( + admission.reserve_payload_reclaiming(None, cx), + Err(HistoryAdmissionError::PayloadCapacity) + )) + }); + assert_eq!(neighbor.evictions(), 1); + assert_eq!( + neighbor.payload_count(), + crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS + ); +} + +#[gpui::test] +fn normal_allocations_preserve_urgent_control_capacity_inside_the_hard_installation_bounds( + cx: &mut TestAppContext, +) { + use crate::history_admission::{ + MAX_NORMAL_HISTORY_BYTES, MAX_NORMAL_HISTORY_PAYLOADS, URGENT_HISTORY_BYTES, + testing::HistoryProbe, + }; + let admission = HistoryProbe::installation(); + let neighbor = HistoryProbe::default(); + let registration = admission.register(neighbor.participant()).unwrap(); + let normal = (0..MAX_NORMAL_HISTORY_PAYLOADS) + .map(|_| admission.reserve_payload().unwrap()) + .collect::>(); + let bytes = cx.update(|cx| { + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES, cx) + .unwrap() + }); + assert!(admission.reserve_payload().is_err()); + let urgent = cx.update(|cx| { + admission + .reserve_urgent_control(®istration, URGENT_HISTORY_BYTES, 2, cx) + .unwrap() + }); + cx.update(|cx| { + assert!( + admission + .reserve_urgent_control(®istration, 1, 1, cx) + .is_err() + ) + }); + let clone = urgent.clone(); + drop(urgent); + cx.update(|cx| { + assert!( + admission + .reserve_urgent_control(®istration, 1, 1, cx) + .is_err() + ) + }); + drop(clone); + cx.update(|cx| { + drop( + admission + .reserve_urgent_control(®istration, URGENT_HISTORY_BYTES, 2, cx) + .unwrap(), + ) + }); + drop((normal, bytes)); +} + +#[gpui::test] +fn snapshot_arc_clones_share_one_generation_and_release_only_after_the_actual_last_drop( + cx: &mut TestAppContext, +) { + let (general, _directory, _, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let requester = client_in(general.scope(), cx); + let value = snapshot(); + let source = cx.update(|cx| general.local_history_allocations(cx)); + let allocation = cx.update(|cx| { + source + .reserve_snapshot(requester.id(), 0, value.retained_bytes().unwrap(), cx) + .unwrap() + }); + let value = allocation.retain(value).unwrap(); + let clone = value.clone(); + let admission = cx.update(|cx| general.history_admission(cx)); + let others = (0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS - 2) + .map(|_| admission.reserve_payload().unwrap()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); // catalog + snapshot +28normal + drop(value); + assert!(admission.reserve_payload().is_err()); + drop(clone); + let freed = admission.reserve_payload().unwrap(); + drop((freed, others)); +} + +#[gpui::test] +fn active_and_latest_writer_generations_use_distinct_shared_slots_until_native_completion( + cx: &mut TestAppContext, +) { + let (general, _directory, repository, _, _) = scoped_fixture(cx); + cx.run_until_parked(); + let first = snapshot(); + let client = ProbeClient::new(cx, first.clone()); + let release = repository.block_next(StorageOperation::Save(first.header.id.clone())); + cx.update(|cx| { + ownership::register_bound(&general, client.clone(), cx).unwrap(); + general + .save_chat(first.clone(), client.id(), 1, cx) + .unwrap(); + }); + cx.run_until_parked(); + let second = snapshot(); + cx.update(|cx| { + *client.chat.borrow_mut() = second.clone(); + client.revision.set(2); + general + .save_chat(second.clone(), client.id(), 2, cx) + .unwrap(); + general.unregister_document(client.id(), cx).unwrap(); + }); + drop((first, second, client)); + let admission = cx.update(|cx| general.history_admission(cx)); + // The catalog is cold and invalidated by retirement; both actual writer + // generations still exist in the common store/native worker. + let retained = (0..crate::history_admission::MAX_NORMAL_HISTORY_PAYLOADS - 2) + .map(|_| admission.reserve_payload().unwrap()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); + release.send(Ok(())).unwrap(); + cx.run_until_parked(); + let freed = admission.reserve_payload().unwrap(); + drop((freed, retained)); +} diff --git a/crates/agent-runtime/src/documents/history_tests/usage.rs b/crates/agent-runtime/src/documents/history_tests/usage.rs index a028c5c8..b3c59cf3 100644 --- a/crates/agent-runtime/src/documents/history_tests/usage.rs +++ b/crates/agent-runtime/src/documents/history_tests/usage.rs @@ -1,104 +1,129 @@ -//! Account refresh effects through public Runtime methods and the shared source fake. +//! Plan limits arrive through the connected agent's event stream. use super::*; use nocterm_ai::{ - testing::ScriptedUsageSource, - usage::{RateLimit, UsageReadError, Window}, + AgentConnection, + model::{session::SessionNotice, usage::ContextUsage}, + usage::Window, }; -fn with_source(cx: &mut TestAppContext, source: Arc) -> Entity { - let repository = Arc::new(InMemoryAgentRepository::default()); - let storage = AgentStorage::prepared(repository.clone(), repository, Ok(Default::default())); - runtime_with_usage_source( - cx, - Arc::new(ScriptedConnector::default()), - storage, - std::path::Path::new("/unused"), - source, - Arc::new(nocterm_ai::testing::ScriptedWorkdirResolver::accept_requests()), - ) +type UsageFixture = ( + Entity, + tempfile::TempDir, + Rc, + async_channel::Sender, +); +fn connected(cx: &mut TestAppContext, agent: &str) -> UsageFixture { + let connector = Arc::new(ScriptedConnector::default()); + let (events, receiver) = async_channel::unbounded(); + connector.push(Ok(AgentConnection { + commands: closing::BlockedCommands::new(false), + events: receiver, + info: AgentInfo { + name: "Supplied agent".into(), + version: "1".into(), + capabilities: Default::default(), + auth_methods: vec![], + }, + })); + let (runtime, directory, _) = fixture_with_connector(cx, connector); + let client = ProbeClient::new(cx, snapshot()); + let launch = nocterm_agent_providers::catalog() + .get(agent) + .map(|profile| profile.default_launch()) + .unwrap_or_else(|| AgentLaunch { + id: agent.into(), + name: agent.into(), + command: "not-executed".into(), + args: vec![], + env: Default::default(), + inherit_env: vec![], + hub: None, + }); + runtime.update(cx, |runtime, cx| { + ownership::register(runtime, client.clone(), cx).unwrap(); + runtime + .connect( + client.clone(), + client.chat.borrow().header.id.clone(), + launch, + cx, + ) + .unwrap(); + }); + cx.run_until_parked(); + assert!( + client + .events + .borrow() + .iter() + .any(|event| matches!(event, SessionEvent::Connected { .. })) + ); + (runtime, directory, client, events) } -fn limit(used: f64) -> RateLimit { - RateLimit { - window: Window::Weekly, - used: Some(used), - resets_at: None, - reached: false, - } +fn usage(meta: serde_json::Value) -> AgentEvent { + AgentEvent::Session(SessionNotice::new( + "usage-session", + SessionUpdate::UsageUpdate( + ContextUsage::new(50, 100).meta( + meta.as_object() + .cloned() + .map(nocterm_ai::model::extensions::Extensions::from), + ), + ), + )) +} +fn report(kind: &str, used: f64) -> AgentEvent { + usage(serde_json::json!({"_claude/rateLimit": { + "rateLimitType": kind, "utilization": used, "status": "allowed" + }})) } #[gpui::test] -fn account_refresh_coalesces_and_retains_last_limits_on_missing_report_or_failure( +fn received_limit_windows_merge_and_missing_or_malformed_reports_retain_them( cx: &mut TestAppContext, ) { - let source = Arc::new(ScriptedUsageSource::default()); - let answer = source.pending(); - let runtime = with_source(cx, source.clone()); - runtime.update(cx, |runtime, cx| { - runtime.refresh_limits("codex", cx); - runtime.refresh_limits("codex", cx); - }); - cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); - assert_eq!(source.requests()[0].id, "codex"); - answer.send(Ok(Some(vec![limit(0.38)]))).unwrap(); - cx.run_until_parked(); - runtime.read_with(cx, |runtime, _| { - assert_eq!( - runtime - .limits("codex") - .unwrap() - .iter() - .cloned() - .collect::>(), - vec![limit(0.38)] - ) - }); - source.push(Ok(None)); - runtime.update(cx, |runtime, cx| runtime.refresh_limits("codex", cx)); - cx.run_until_parked(); - source.push(Err(UsageReadError::read( - "/unused/report", - std::io::Error::from(std::io::ErrorKind::PermissionDenied), - ))); - runtime.update(cx, |runtime, cx| runtime.refresh_limits("codex", cx)); + let (runtime, _directory, _client, events) = connected(cx, "claude"); + runtime.read_with(cx, |runtime, _| assert!(runtime.limits("claude").is_none())); + for event in [ + report("seven_day", 0.38), + report("five_hour", 0.12), + report("seven_day", 0.42), + usage(serde_json::json!({})), + usage(serde_json::json!({"_claude/rateLimit": "invalid"})), + report("unknown_window", 0.99), + ] { + events.try_send(event).unwrap(); + } cx.run_until_parked(); runtime.read_with(cx, |runtime, _| { - assert_eq!( - runtime - .limits("codex") - .unwrap() - .iter() - .cloned() - .collect::>(), - vec![limit(0.38)] - ) + let limits: Vec<_> = runtime.limits("claude").unwrap().iter().collect(); + assert_eq!(limits.len(), 2); + assert_eq!(limits[0].window, Window::FiveHour); + assert_eq!(limits[0].used, Some(0.12)); + assert_eq!(limits[1].window, Window::Weekly); + assert_eq!(limits[1].used, Some(0.42)); + assert!(runtime.limits("codex").is_none()); }); - assert_eq!(source.requests().len(), 3); } #[gpui::test] -fn stopped_runtime_rejects_late_usage_delivery_and_unknown_agent_has_no_request( - cx: &mut TestAppContext, -) { - let source = Arc::new(ScriptedUsageSource::default()); - let answer = source.pending(); - let runtime = with_source(cx, source.clone()); - runtime.update(cx, |runtime, cx| { - runtime.refresh_limits("unknown-agent", cx) - }); - cx.run_until_parked(); - assert!(source.requests().is_empty()); - runtime.update(cx, |runtime, cx| runtime.refresh_limits("codex", cx)); - cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); +fn agents_without_a_limit_metadata_profile_do_not_fabricate_plan_limits(cx: &mut TestAppContext) { + for agent in ["codex", "unknown-agent"] { + let (runtime, _directory, client, events) = connected(cx, agent); + events.try_send(report("seven_day", 0.99)).unwrap(); + cx.run_until_parked(); + runtime.read_with(cx, |runtime, _| assert!(runtime.limits(agent).is_none())); + assert!(client.events.borrow().iter().any(|event| matches!(event, + SessionEvent::Update(notice) if matches!(notice.update, SessionUpdate::UsageUpdate(_)) + ))); + } +} + +#[gpui::test] +fn a_stopped_connection_does_not_accept_late_plan_limit_reports(cx: &mut TestAppContext) { + let (runtime, _directory, _client, events) = connected(cx, "claude"); let _drain = runtime.update(cx, |runtime, cx| runtime.shutdown(cx)); + let _ = events.try_send(report("seven_day", 0.99)); cx.run_until_parked(); - let _ = answer.send(Ok(Some(vec![limit(0.99)]))); - cx.run_until_parked(); - runtime.update(cx, |runtime, cx| { - assert!(runtime.limits("codex").is_none()); - runtime.refresh_limits("codex", cx); - }); - cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); + runtime.read_with(cx, |runtime, _| assert!(runtime.limits("claude").is_none())); } diff --git a/crates/agent-runtime/src/documents/history_tests/workspace.rs b/crates/agent-runtime/src/documents/history_tests/workspace.rs index 56c93d93..ec00a059 100644 --- a/crates/agent-runtime/src/documents/history_tests/workspace.rs +++ b/crates/agent-runtime/src/documents/history_tests/workspace.rs @@ -2,7 +2,7 @@ use super::*; use nocterm_ai::{ AgentError, - testing::{ScriptedUsageSource, ScriptedWorkdirResolver}, + testing::ScriptedWorkdirResolver, workspace::{PreparedWorkspace, WorkdirError}, }; fn candidate() -> std::path::PathBuf { @@ -26,14 +26,7 @@ fn runtime_with_workspace( ) -> Entity { let repository = Arc::new(InMemoryAgentRepository::default()); let storage = AgentStorage::prepared(repository.clone(), repository, Ok(Default::default())); - runtime_with_usage_source( - cx, - connector, - storage, - &candidate(), - Arc::new(ScriptedUsageSource::default()), - resolver, - ) + runtime_with_workspace_resolver(cx, connector, storage, &candidate(), resolver) } fn connect(runtime: &Entity, cx: &mut TestAppContext) { let client = ProbeClient::new(cx, snapshot()); @@ -44,9 +37,17 @@ fn connect(runtime: &Entity, cx: &mut TestAppContext) { args: Vec::new(), env: Default::default(), inherit_env: Vec::new(), + hub: None, }; runtime.update(cx, |runtime, cx| { - runtime.connect(client.clone(), "test-chat".into(), launch, cx) + runtime + .connect( + client.clone(), + client.chat.borrow().header.id.clone(), + launch, + cx, + ) + .unwrap() }); } #[gpui::test] diff --git a/crates/agent-runtime/src/documents/registry.rs b/crates/agent-runtime/src/documents/registry.rs new file mode 100644 index 00000000..4e0256e0 --- /dev/null +++ b/crates/agent-runtime/src/documents/registry.rs @@ -0,0 +1,373 @@ +//! Installation-wide document ownership and revision delivery, keyed by context. +use super::{CandidateStamp, Client}; +use crate::ChatOpenError; +use gpui::EntityId; +use nocterm_ai::{execution_origin::ChatKey, local_environment::LocalEnvironmentAccess}; +use nocterm_store::KeyedStamp; +use std::{ + cell::RefCell, + collections::{HashMap, HashSet}, + rc::{Rc, Weak}, +}; + +#[derive(Clone, Copy)] +struct DocumentOwner { + entity: Option, + window: EntityId, + incarnation: u64, + reserved: bool, +} + +/// Noncloneable ownership reservation. Drop releases only its original incarnation. +pub struct DocumentClaim { + registry: Weak>, + key: ChatKey, + owner: DocumentOwner, + previous: Option, + access: LocalEnvironmentAccess, +} +impl DocumentClaim { + pub fn key(&self) -> &ChatKey { + &self.key + } + pub(super) fn belongs_to(&self, registry: &Rc>) -> bool { + self.registry + .upgrade() + .is_some_and(|original| Rc::ptr_eq(&original, registry)) + } +} +impl Drop for DocumentClaim { + fn drop(&mut self) { + if let Some(registry) = self.registry.upgrade() { + registry.borrow_mut().rollback(self); + } + } +} + +pub enum DocumentClaimOutcome { + Owned(DocumentClaim), + AlreadyOpen { window: EntityId }, + Deleted, +} + +struct RegisteredClient { + client: Client, + _history: crate::history_admission::HistoryRegistration, +} +pub(super) struct DocumentRegistry { + clients: HashMap, + admission: crate::history_admission::HistoryAdmissionHandle, + owners: HashMap, + deleted: HashSet, + stamps: HashMap, + closing: bool, + sequence: u64, + access: HashMap, +} +impl DocumentRegistry { + pub(super) fn new(admission: crate::history_admission::HistoryAdmissionHandle) -> Self { + Self { + clients: Default::default(), + admission, + owners: Default::default(), + deleted: Default::default(), + stamps: Default::default(), + closing: false, + sequence: 0, + access: Default::default(), + } + } + pub(super) fn claim( + registry: &Rc>, + key: ChatKey, + access: LocalEnvironmentAccess, + window: EntityId, + replacing: Option, + ) -> Result { + access + .check_current() + .map_err(nocterm_ai::storage::AgentStorageError::from)?; + nocterm_ai::storage::require_local_environment(key.environment(), access.environment())?; + let mut this = registry.borrow_mut(); + if this.closing { + return Err(ChatOpenError::Closed); + } + if this.deleted.contains(&key) { + return Ok(DocumentClaimOutcome::Deleted); + } + if let Some(entity) = this + .owners + .get(&key) + .filter(|owner| !owner.reserved) + .and_then(|owner| owner.entity) + && this + .clients + .get(&entity) + .is_none_or(|client| !client.client.alive()) + { + this.retire(entity); + } + if this + .access + .get(&key) + .is_none_or(|access| access.check_current().is_err()) + && let Some(entity) = this.owners.get(&key).and_then(|owner| owner.entity) + { + this.retire(entity); + } + let previous = this.owners.get(&key).copied(); + if let Some(expected) = replacing { + if previous.is_none_or(|owner| owner.reserved || owner.entity != Some(expected)) { + return Err(ChatOpenError::StaleClaim(key)); + } + } else if let Some(owner) = previous { + return Ok(DocumentClaimOutcome::AlreadyOpen { + window: owner.window, + }); + } + let incarnation = this + .sequence + .checked_add(1) + .ok_or(ChatOpenError::IncarnationExhausted)?; + this.sequence = incarnation; + let owner = DocumentOwner { + entity: previous.and_then(|owner| owner.entity), + window, + incarnation, + reserved: true, + }; + this.owners.insert(key.clone(), owner); + Ok(DocumentClaimOutcome::Owned(DocumentClaim { + registry: Rc::downgrade(registry), + key, + owner, + previous, + access, + })) + } + fn reservation_current(&self, claim: &DocumentClaim) -> bool { + self.owners + .get(&claim.key) + .is_some_and(|owner| owner.incarnation == claim.owner.incarnation && owner.reserved) + } + fn rollback(&mut self, claim: &DocumentClaim) { + if !self.reservation_current(claim) { + return; + } + self.owners.remove(&claim.key); + if let Some(previous) = claim.previous + && previous.entity.is_some_and(|id| { + self.clients + .get(&id) + .is_some_and(|client| client.client.alive()) + }) + { + self.owners.insert(claim.key.clone(), previous); + } + } + pub(super) fn require_reservation(&self, claim: &DocumentClaim) -> Result<(), ChatOpenError> { + if self.closing { + return Err(ChatOpenError::Closed); + } + claim + .access + .check_current() + .map_err(nocterm_ai::storage::AgentStorageError::from)?; + if self.deleted.contains(&claim.key) { + return Err(ChatOpenError::Deleted(claim.key.clone())); + } + if !self.reservation_current(claim) { + return Err(ChatOpenError::StaleClaim(claim.key.clone())); + } + Ok(()) + } + /// Makes `client` the owner `claim` reserved; the owner it replaced when that one was in + /// another window, which the chat moved from. + pub(super) fn commit( + &mut self, + claim: &DocumentClaim, + client: Client, + ) -> Result, ChatOpenError> { + self.require_reservation(claim)?; + claim + .access + .check_current() + .map_err(nocterm_ai::storage::AgentStorageError::from)?; + if self + .owners + .iter() + .any(|(key, owner)| key != claim.key() && owner.entity == Some(client.id())) + { + return Err(ChatOpenError::StaleClaim(claim.key.clone())); + } + let history = self + .admission + .register(std::rc::Rc::new( + crate::history_admission::local::SessionHistory::new(client.clone()), + )) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage)?; + if let Some(previous) = claim.previous.and_then(|owner| owner.entity) { + self.clients.remove(&previous); + self.stamps.remove(&claim.key); + } + let moved = claim + .previous + .filter(|previous| previous.window != claim.owner.window) + .and_then(|previous| previous.entity); + let owner = client.id(); + self.owners.insert( + claim.key.clone(), + DocumentOwner { + entity: Some(owner), + reserved: false, + ..claim.owner + }, + ); + self.access.insert(claim.key.clone(), claim.access.clone()); + self.clients.insert( + owner, + RegisteredClient { + client, + _history: history, + }, + ); + Ok(moved) + } + /// The window that holds chat `entity`. + pub(super) fn window_of(&self, entity: EntityId) -> Option { + self.owners + .values() + .find(|owner| owner.entity == Some(entity)) + .map(|owner| owner.window) + } + /// The chats of `scope` windows other than `window` hold, once built. + pub(super) fn held_elsewhere( + &self, + scope: &nocterm_ai::local_environment::ChatScope, + window: EntityId, + ) -> Vec { + self.owners + .iter() + .filter(|(key, owner)| { + !owner.reserved && owner.window != window && key.chat_scope() == *scope + }) + .filter_map(|(_, owner)| Some(self.clients.get(&owner.entity?)?.client.clone())) + .collect() + } + pub(super) fn retire(&mut self, id: EntityId) -> Vec { + let ids = self + .owners + .iter() + .filter(|(_, owner)| owner.entity == Some(id)) + .map(|(id, _)| id.clone()) + .collect(); + self.clients.remove(&id); + self.owners.retain(|_, owner| owner.entity != Some(id)); + self.access.retain(|key, _| self.owners.contains_key(key)); + self.stamps.retain(|_, (owner, _)| *owner != id); + ids + } + pub(super) fn current(&self, id: &ChatKey, owner: EntityId) -> bool { + !self.closing + && self.source_current(id) + && self.owners.get(id).is_some_and(|current| !current.reserved) + && self.capture_current(id, owner) + } + pub(super) fn revoke_stale_sources(&mut self) -> Vec { + let keys = self + .access + .iter() + .filter(|(_, access)| access.check_current().is_err()) + .map(|(key, _)| key.clone()) + .collect::>(); + let mut clients = Vec::new(); + for key in keys { + self.access.remove(&key); + if let Some(client) = self + .owners + .get(&key) + .and_then(|owner| owner.entity) + .and_then(|id| self.clients.get(&id)) + { + clients.push(client.client.clone()); + } + } + clients + } + pub(super) fn source_current(&self, key: &ChatKey) -> bool { + self.access + .get(key) + .is_some_and(|access| access.check_current().is_ok()) + } + pub(super) fn capture_current(&self, id: &ChatKey, owner: EntityId) -> bool { + !self.deleted.contains(id) + && self + .owners + .get(id) + .is_some_and(|current| current.entity == Some(owner)) + } + pub(super) fn accepted(&mut self, id: &ChatKey, owner: EntityId, revision: u64) { + self.stamps.insert(id.to_owned(), (owner, revision)); + } + pub(super) fn accepts_revision(&self, id: &ChatKey, owner: EntityId, revision: u64) -> bool { + self.capture_current(id, owner) + && self + .stamps + .get(id) + .is_none_or(|(accepted_owner, accepted_revision)| { + *accepted_owner != owner || revision >= *accepted_revision + }) + } + pub(super) fn seal(&mut self, id: ChatKey) { + self.deleted.insert(id.clone()); + self.stamps.remove(&id); + self.owners.remove(&id); + self.access.remove(&id); + } + pub(super) fn target( + &self, + stamp: &KeyedStamp, + succeeded: bool, + ) -> Option { + let owner = stamp.tag().owner?; + if stamp.tag().deleted || !self.current(stamp.key(), owner) { + return None; + } + // A stale failure must not pause a newer accepted document revision. + // Successful old publications still acknowledge their actual own revision. + if !succeeded && self.stamps.get(stamp.key()) != Some(&(owner, stamp.tag().revision)) { + return None; + } + self.clients + .get(&owner) + .map(|registered| registered.client.clone()) + } + pub(super) fn scope_of( + &self, + owner: EntityId, + ) -> Option { + self.owners + .iter() + .find_map(|(key, current)| (current.entity == Some(owner)).then_some(key.chat_scope())) + } + pub(super) fn client(&self, id: EntityId) -> Option { + self.clients + .get(&id) + .map(|registered| registered.client.clone()) + } + pub(super) fn clients(&self) -> Vec { + self.clients + .values() + .map(|registered| registered.client.clone()) + .collect() + } + pub(super) fn contains_deleted(&self, id: &ChatKey) -> bool { + self.deleted.contains(id) + } + pub(super) fn closing(&self) -> bool { + self.closing + } + pub(super) fn stop(&mut self) { + self.closing = true; + } +} diff --git a/crates/agent-runtime/src/error.rs b/crates/agent-runtime/src/error.rs new file mode 100644 index 00000000..2d4547c1 --- /dev/null +++ b/crates/agent-runtime/src/error.rs @@ -0,0 +1,53 @@ +//! Typed refusals before allocating any native agent or bridge work. +#[derive(Clone, Debug, thiserror::Error)] +pub enum AgentActivationError { + #[error("{0}")] + Storage(#[from] nocterm_ai::storage::AgentStorageError), + #[error("{0}")] + Origin(#[from] nocterm_ai::execution_origin::ExecutionOriginError), + #[error("The chat is no longer open.")] + ClientClosed, +} + +/// Admission failures retain concrete storage causes until the presentation boundary. +#[derive(Clone, Debug, thiserror::Error)] +pub enum ChatOpenError { + #[error("{0}")] + Storage(#[from] nocterm_ai::storage::AgentStorageError), + #[error("The chat is already open in another window: {key:?}")] + AlreadyOpen { + key: nocterm_ai::execution_origin::ChatKey, + window: gpui::EntityId, + }, + #[error("The chat was deleted: {0:?}")] + Deleted(nocterm_ai::execution_origin::ChatKey), + #[error("The chat ownership reservation is stale: {0:?}")] + StaleClaim(nocterm_ai::execution_origin::ChatKey), + #[error("Agent history is shutting down or the client is closed.")] + Closed, + #[error("Document ownership incarnation is exhausted.")] + IncarnationExhausted, +} +#[cfg(test)] +mod tests { + use super::*; + use std::{error::Error as _, sync::Arc}; + #[test] + fn chat_open_errors_retain_the_original_storage_cause_after_clone() { + let cause = Arc::new(std::io::Error::from(std::io::ErrorKind::PermissionDenied)); + let error = ChatOpenError::from(nocterm_ai::storage::AgentStorageError::Read { + path: "history".into(), + source: cause.clone(), + }); + let cloned = error.clone(); + let stored = cloned + .source() + .unwrap() + .downcast_ref::() + .unwrap(); + let nocterm_ai::storage::AgentStorageError::Read { source, .. } = stored else { + panic!("original storage classification"); + }; + assert!(Arc::ptr_eq(source, &cause)); + } +} diff --git a/crates/agent-runtime/src/execution.rs b/crates/agent-runtime/src/execution.rs new file mode 100644 index 00000000..4fbbf7fd --- /dev/null +++ b/crates/agent-runtime/src/execution.rs @@ -0,0 +1,56 @@ +//! One local agent connection retains its original conversation and prepared directory. +use nocterm_ai::{ + AgentLaunch, + execution_origin::{ChatKey, ExecutionOrigin}, +}; +use std::path::{Path, PathBuf}; + +pub(super) fn admitted_origin( + origin: &ExecutionOrigin, + requested_id: &str, +) -> Result { + if origin.key().id() != requested_id { + return Err(crate::AgentActivationError::Storage( + nocterm_ai::history::HistoryCodecError::IdentityMismatch.into(), + )); + } + Ok(origin.clone()) +} + +#[derive(Hash)] +pub(super) struct ExecutionIdentity { + origin: ExecutionOrigin, + launch: AgentLaunch, + workdir: PathBuf, + isolated: bool, +} +impl ExecutionIdentity { + pub(super) fn new( + origin: ExecutionOrigin, + launch: AgentLaunch, + workdir: PathBuf, + isolated: bool, + ) -> Self { + Self { + origin, + launch, + workdir, + isolated, + } + } + pub(super) fn key(&self) -> &ChatKey { + self.origin.key() + } + pub(super) fn launch(&self) -> &AgentLaunch { + &self.launch + } + pub(super) fn workdir(&self) -> &Path { + &self.workdir + } + pub(super) fn prepared_directory(&mut self, workdir: PathBuf) { + self.workdir = workdir; + } + pub(super) fn isolation_changed(&self, isolated: bool) -> bool { + self.isolated != isolated + } +} diff --git a/crates/agent-runtime/src/handle.rs b/crates/agent-runtime/src/handle.rs index 9d8da09b..358329db 100644 --- a/crates/agent-runtime/src/handle.rs +++ b/crates/agent-runtime/src/handle.rs @@ -8,20 +8,107 @@ use nocterm_ai::model::auth::AuthMethod; use nocterm_ai::model::controls::ConfigOption; use nocterm_ai::{ AgentLaunch, BridgeRegistration, - history::{ChatSummary, SavedChat, SharedChat}, + execution_origin::ChatKey, + history::{ContextId, SavedChat, SharedChat}, + local_environment::{ChatScope, LocalEnvironment, LocalEnvironmentAccess}, }; use std::sync::Arc; #[derive(Clone)] pub struct RuntimeHandle { runtime: Entity, + scope: ContextId, + access: Option, } impl RuntimeHandle { pub fn new(services: RuntimeServices, cx: &mut App) -> Self { Self { runtime: cx.new(|cx| Runtime::new(services, cx)), + scope: ContextId::General, + access: Some(LocalEnvironmentAccess::this_computer()), } } + /// An immutable view of the same installation service, captured before work starts. + pub fn for_scope(&self, scope: ContextId) -> Self { + Self { + runtime: self.runtime.clone(), + scope, + access: self.access.clone(), + } + } + pub fn for_environment( + &self, + access: LocalEnvironmentAccess, + ) -> Result { + access.check_current()?; + Ok(Self { + runtime: self.runtime.clone(), + scope: self.scope, + access: Some(access), + }) + } + pub fn unavailable_account(&self) -> Self { + Self { + runtime: self.runtime.clone(), + scope: self.scope, + access: None, + } + } + pub fn environment( + &self, + ) -> Result<&LocalEnvironment, nocterm_hubs_model::source_identity::LocalAccountSourceError> + { + self.access + .as_ref() + .map(LocalEnvironmentAccess::environment) + .ok_or(nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable) + } + pub fn chat_scope(&self) -> Result { + Ok(ChatScope::new(self.environment()?.clone(), self.scope)) + } + fn current_source( + &self, + ) -> Result<(), nocterm_hubs_model::source_identity::LocalAccountSourceError> { + self.access + .as_ref() + .ok_or(nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable)? + .check_current() + } + pub fn check_current(&self) -> Result<(), nocterm_ai::storage::AgentStorageError> { + self.current_source().map_err(Into::into) + } + /// Retain the original source for native workers that outlive an App callback. + pub fn captured_source( + &self, + ) -> Result { + self.check_current()?; + self.access.clone().ok_or_else(|| { + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable.into() + }) + } + /// Native source observations trigger revalidation of retained original leases. + /// Notifications are hints; each source checks its own actual retained owner. + pub fn refresh_environment_sources(&self, cx: &mut App) { + self.runtime + .update(cx, |runtime, cx| runtime.refresh_environment_sources(cx)); + } + /// Create a chat-owned registry using this installation’s supplied native adapter. + pub fn command_jobs(&self, cx: &App) -> Box { + self.runtime.read(cx).services.command_jobs.create() + } + /// The transfer port this installation supplies; every chat keeps its own book over it. + pub fn transfers(&self, cx: &App) -> Arc { + self.runtime.read(cx).services.transfers.clone() + } + pub fn scope(&self) -> ContextId { + self.scope + } + pub fn catalog_snapshot(&self, cx: &App) -> Option { + self.check_current().ok()?; + self.runtime + .read(cx) + .catalog_snapshot(self.chat_scope().ok()?) + } pub fn observe( &self, cx: &mut App, @@ -47,13 +134,31 @@ impl RuntimeHandle { self.runtime.read(cx).favorites_error(cx) } pub fn history_reader(&self, cx: &App) -> HistoryReader { - self.runtime.read(cx).history_reader() + let runtime = self.runtime.read(cx); + match self.chat_scope() { + Ok(scope) => runtime + .scoped_history_reader(scope) + .with_access(self.access.clone()), + Err(_) => runtime.chats.unavailable_reader(), + } } pub fn limits<'a>(&self, agent: &str, cx: &'a App) -> Option<&'a nocterm_ai::usage::Limits> { self.runtime.read(cx).limits(agent) } pub fn document_is_current(&self, chat_id: &str, owner: EntityId, cx: &App) -> bool { - self.runtime.read(cx).document_is_current(chat_id, owner) + if self.check_current().is_err() { + return false; + } + self.runtime.read(cx).scoped_document_is_current( + &ChatKey::in_environment( + match self.chat_scope() { + Ok(scope) => scope, + Err(_) => return false, + }, + chat_id.to_owned(), + ), + owner, + ) } pub fn terminal_auth_request( &self, @@ -61,11 +166,81 @@ impl RuntimeHandle { method: &AuthMethod, cx: &App, ) -> Option { + self.check_current().ok()?; self.runtime.read(cx).terminal_auth_request(key, method) } - pub fn take_history(&self, cx: &mut App) -> (Option>, Option>) { - self.runtime.update(cx, |runtime, _| { - (runtime.take_saved_chats(), runtime.take_saved_catalog()) + /// The same retained installation budget for Local and remote conversation sources. + pub fn history_admission(&self, cx: &App) -> crate::history_admission::HistoryAdmissionHandle { + self.runtime.read(cx).chats.history_admission() + } + /// Capture the installation and Runtime borrower identity before Local work. + pub fn local_history_allocations( + &self, + cx: &App, + ) -> crate::history_admission::LocalHistoryAllocations { + crate::history_admission::LocalHistoryAllocations::new( + self.history_admission(cx), + self.runtime.entity_id(), + ) + } + /// Admit before a hydrated body mutates its original registered owner. + /// All other contexts/windows and immutable writer generations are included. + pub fn admit_history( + &self, + requester: EntityId, + bytes: usize, + cx: &mut App, + ) -> Result<(), nocterm_ai::storage::AgentStorageError> { + self.check_current()?; + let residency = self.runtime.read(cx).chats.history_residency( + self.chat_scope()?, + Some(requester), + cx, + )?; + residency + .admit(Some(requester), bytes, cx) + .map(|_| ()) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage) + } + pub fn trim_history( + &self, + cx: &mut App, + ) -> Result, nocterm_ai::storage::AgentStorageError> { + self.check_current()?; + let residency = + self.runtime + .read(cx) + .chats + .history_residency(self.chat_scope()?, None, cx)?; + residency + .admit(None, 0, cx) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage) + } + /// Admission for a new decoded document is bound to its exact reservation. + pub fn admit_claimed_history( + &self, + claim: &crate::DocumentClaim, + bytes: usize, + cx: &mut App, + ) -> Result<(), crate::ChatOpenError> { + self.check_current()?; + nocterm_ai::storage::require_local_scope(&self.chat_scope()?, &claim.key().chat_scope())?; + let residency = self.runtime.read(cx).chats.claim_residency(claim, cx)?; + residency + .admit(None, bytes, cx) + .map_err(crate::history_admission::HistoryAdmissionError::into_storage)?; + Ok(()) + } + pub fn take_prepared_chat( + &self, + claim: &crate::DocumentClaim, + payload: crate::history_admission::HistoryPayloadPermit, + cx: &mut App, + ) -> Result>, crate::ChatOpenError> { + self.check_current()?; + nocterm_ai::storage::require_local_scope(&self.chat_scope()?, &claim.key().chat_scope())?; + self.runtime.update(cx, |runtime, cx| { + runtime.take_prepared_chat(claim, payload, cx) }) } pub fn toggle_favorite(&self, agent: &str, option: &str, value: &str, cx: &mut App) { @@ -93,25 +268,84 @@ impl RuntimeHandle { self.runtime .update(cx, |runtime, cx| runtime.set_last_agent(agent, cx)) } - pub fn refresh_limits(&self, agent: &str, cx: &mut App) { - self.runtime - .update(cx, |runtime, cx| runtime.refresh_limits(agent, cx)) + pub fn claim_document( + &self, + id: String, + window: EntityId, + cx: &mut App, + ) -> Result { + self.check_current()?; + self.runtime.read(cx).claim_local_document( + ChatKey::in_environment(self.chat_scope()?, id), + self.access + .clone() + .ok_or(nocterm_ai::storage::AgentStorageError::Interrupted)?, + window, + None, + ) } - pub fn register_document(&self, client: Client, cx: &mut App) { - self.runtime - .update(cx, |runtime, cx| runtime.register_document(client, cx)) + /// Claims chat `id` in `window` in place of `expected_owner`: a restart in the same window, + /// or a move from another, where the chat must be at rest. + pub fn replace_document( + &self, + id: String, + expected_owner: EntityId, + window: EntityId, + cx: &mut App, + ) -> Result { + self.check_current()?; + if let Some(held) = self.runtime.read(cx).kept_by(expected_owner, window, cx) { + return Ok(crate::DocumentClaimOutcome::AlreadyOpen { window: held }); + } + self.runtime.read(cx).claim_local_document( + ChatKey::in_environment(self.chat_scope()?, id), + self.access + .clone() + .ok_or(nocterm_ai::storage::AgentStorageError::Interrupted)?, + window, + Some(expected_owner), + ) } - pub fn unregister_document(&self, id: EntityId, cx: &mut App) { + pub fn commit_document( + &self, + claim: crate::DocumentClaim, + client: Client, + cx: &mut App, + ) -> Result<(), crate::ChatOpenError> { + self.check_current()?; + nocterm_ai::storage::require_local_scope(&self.chat_scope()?, &claim.key().chat_scope())?; self.runtime - .update(cx, |runtime, cx| runtime.unregister_document(id, cx)) + .update(cx, |runtime, cx| runtime.commit_document(claim, client, cx)) } - pub fn request_activation(&self, client: Client, cx: &mut App) { - self.runtime - .update(cx, |runtime, cx| runtime.request_activation(client, cx)) + pub fn unregister_document( + &self, + id: EntityId, + cx: &mut App, + ) -> Result<(), nocterm_ai::storage::AgentStorageError> { + self.runtime.update(cx, |runtime, cx| { + runtime.unregister_scoped_document(self.chat_scope()?, id, cx) + }) } - pub fn connect(&self, client: Client, chat_id: String, launch: AgentLaunch, cx: &mut App) { + pub fn request_activation( + &self, + client: Client, + cx: &mut App, + ) -> Result<(), crate::AgentActivationError> { + self.check_current()?; self.runtime.update(cx, |runtime, cx| { - runtime.connect(client, chat_id, launch, cx) + runtime.request_scoped_activation(self.chat_scope()?, client, cx) + }) + } + pub fn connect( + &self, + client: Client, + chat_id: String, + launch: AgentLaunch, + cx: &mut App, + ) -> Result<(), crate::AgentActivationError> { + self.check_current()?; + self.runtime.update(cx, |runtime, cx| { + runtime.connect_scoped(self.chat_scope()?, client, chat_id, launch, cx) }) } pub fn register_bridge( @@ -119,8 +353,18 @@ impl RuntimeHandle { client: &Client, cx: &mut App, ) -> Result { - self.runtime - .update(cx, |runtime, cx| runtime.register_bridge(client, cx)) + self.current_source()?; + self.runtime.update(cx, |runtime, cx| { + runtime.register_scoped_bridge( + self.chat_scope().map_err(|_| { + nocterm_ai::connection::BridgeRegistrationError::AccountSource( + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable, + ) + })?, + client, + cx, + ) + }) } pub fn save_chat( &self, @@ -130,7 +374,7 @@ impl RuntimeHandle { cx: &mut App, ) -> Result { self.runtime.update(cx, |runtime, cx| { - runtime.save_chat(chat, owner, revision, cx) + runtime.scoped_save_chat(self.chat_scope()?, chat, owner, revision, cx) }) } pub fn capture_and_detach_document( @@ -139,16 +383,74 @@ impl RuntimeHandle { cx: &mut App, ) -> Result { self.runtime.update(cx, |runtime, cx| { - runtime.capture_and_detach_document(id, cx) + runtime.capture_scoped_document(self.chat_scope()?, id, cx) }) } - pub fn delete_chat(&self, id: String, cx: &mut App) { + /// The chats of this collection windows other than `window` hold, which `window` lists + /// without holding them. + pub fn held_elsewhere(&self, window: EntityId, cx: &App) -> Vec { + let (Ok(()), Ok(scope)) = (self.check_current(), self.chat_scope()) else { + return Vec::new(); + }; self.runtime - .update(cx, |runtime, cx| runtime.delete_chat(id, cx)) + .read(cx) + .held_elsewhere(&scope, window) + .iter() + .filter_map(|client| client.chat()) + .collect() + } + /// Asks the window that holds chat `chat` to show it and come forward. + pub fn reveal_chat(&self, chat: EntityId, cx: &mut App) { + self.runtime + .update(cx, |_, cx| cx.emit(crate::HolderEvent::Reveal { chat })); + } + /// Whether chat `chat`, which another window holds, may move to `window`: it neither works + /// nor shows in its own. + pub fn may_move(&self, chat: EntityId, window: EntityId, cx: &App) -> bool { + let runtime = self.runtime.read(cx); + runtime.chats.window_of(chat).is_some() && runtime.kept_by(chat, window, cx).is_none() + } + /// Calls `event` for what the runtime tells every window about the chats windows hold. + pub fn observe_holders( + &self, + cx: &mut App, + mut event: impl FnMut(&crate::HolderEvent, &mut App) + 'static, + ) -> Subscription { + cx.subscribe(&self.runtime, move |_, notice, cx| event(notice, cx)) + } + pub fn delete_chat(&self, id: String, cx: &mut App) { + if self.check_current().is_err() { + return; + } + let Ok(scope) = self.chat_scope() else { + return; + }; + self.runtime.update(cx, |runtime, cx| { + runtime.scoped_delete_chat(ChatKey::in_environment(scope, id), cx) + }) + } + /// Request the captured context once; an existing load or snapshot is retained. + pub fn ensure_history(&self, cx: &mut App) -> Result<(), crate::CatalogLoadError> { + self.current_source()?; + self.runtime.update(cx, |runtime, cx| { + runtime.chats.ensure(self.chat_scope()?, cx) + }) + } + pub fn try_reload_history(&self, cx: &mut App) -> Result<(), crate::CatalogLoadError> { + self.current_source()?; + self.runtime.update(cx, |runtime, cx| { + runtime.try_scoped_reload_history(self.chat_scope()?, cx) + }) } pub fn reload_history(&self, cx: &mut App) { + if self.check_current().is_err() { + return; + } + let Ok(scope) = self.chat_scope() else { + return; + }; self.runtime - .update(cx, |runtime, cx| runtime.reload_history(cx)) + .update(cx, |runtime, cx| runtime.scoped_reload_history(scope, cx)) } pub fn settings_changed(&self, cx: &mut App) { self.runtime diff --git a/crates/agent-runtime/src/history_admission.rs b/crates/agent-runtime/src/history_admission.rs new file mode 100644 index 00000000..75e44515 --- /dev/null +++ b/crates/agent-runtime/src/history_admission.rs @@ -0,0 +1,608 @@ +//! One installation owns Local/remote history bytes and retained payload slots. +//! +//! Participants expose only history accounting and optional hidden-history eviction. +//! Registration and allocation tokens never keep an entity alive. No budget state +//! borrow is held while a participant is observed or asked to evict. +use crate::HistoryUsage; +use gpui::{App, EntityId}; +use nocterm_ai::{ + execution_origin::ChatKey, history::MAX_RESIDENT_BYTES, storage::AgentStorageError, +}; +use std::{ + cell::{Cell, RefCell}, + collections::HashSet, + rc::{Rc, Weak}, + sync::{Arc, Weak as SyncWeak}, +}; + +mod eviction; +mod local_allocations; +pub use local_allocations::{LocalHistoryAllocations, LocalSnapshotAllocation}; +pub(crate) mod local; +use eviction::EvictionCandidate; +#[cfg(any(test, feature = "test-support"))] +pub mod testing; + +pub const MAX_HISTORY_PAYLOADS: usize = 32; +/// The final two payloads and 1MiB remain available to original urgent controls. +pub const MAX_NORMAL_HISTORY_PAYLOADS: usize = MAX_HISTORY_PAYLOADS - 2; +// Guarded fresh-head receipts and upload buffers share this reserved lane. +// This repartitions the same installation total; normal capacity decreases. +pub const URGENT_HISTORY_BYTES: usize = 1024 * 1024; +pub const MAX_NORMAL_HISTORY_BYTES: usize = MAX_RESIDENT_BYTES - URGENT_HISTORY_BYTES; + +#[derive(Debug, thiserror::Error)] +pub enum HistoryAdmissionError { + #[error("History admission is closed.")] + Closed, + #[error("History admission is already deciding an eviction.")] + Reentrant, + #[error("The history registration belongs to another installation.")] + ForeignRegistration, + #[error("The history allocation belongs to another installation.")] + ForeignAllocation, + #[error("A retained history allocation cannot grow from {reserved} to {requested} bytes.")] + AllocationGrowth { reserved: usize, requested: usize }, + #[error("All normal installation history payload slots are retained or loading.")] + PayloadCapacity, + #[error( + "History needs {incoming} bytes with {retained} retained; the installation limit is {limit}." + )] + ResidentCapacity { + retained: usize, + incoming: usize, + limit: usize, + }, + #[error(transparent)] + Storage(#[from] AgentStorageError), +} +impl HistoryAdmissionError { + pub fn into_storage(self) -> AgentStorageError { + match self { + Self::Storage(error) => error, + Self::PayloadCapacity => AgentStorageError::PayloadCapacity, + Self::ResidentCapacity { + retained, + incoming, + limit, + } => AgentStorageError::ResidentCapacity { + retained, + incoming, + limit, + }, + _ => AgentStorageError::Interrupted, + } + } +} + +/// The implementation retains a weak entity/data-owner reference, never an App. +pub trait HistoryParticipant { + fn entity_id(&self) -> Option { + None + } + fn usage(&self, cx: &App) -> Result, AgentStorageError>; + fn evict(&self, _cx: &mut App) -> bool { + false + } + /// Oldest reclaimable payload's installation allocation order. Retained + /// caller snapshots, shown documents and pending work are never candidates. + fn oldest_payload(&self, _cx: &App) -> Option { + None + } + fn evict_payload(&self, _cx: &mut App) -> bool { + false + } + fn local_key(&self, _cx: &App) -> Option { + None + } +} +struct RegisteredHistory { + owner: Weak, + participant: Rc, +} +/// Retains the observation adapter, whose entity reference remains weak. +#[derive(Clone)] +pub struct HistoryRegistration(Rc); + +/// Cloneable allocation proof; cloned retained rows consume one slot together. +#[derive(Clone)] +pub struct HistoryPayloadPermit(Arc); +struct PayloadAllocation { + order: u64, +} +impl HistoryPayloadPermit { + pub fn same_allocation(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } + pub fn order(&self) -> u64 { + self.0.order + } +} +/// An admitted pending buffer. Its immutable bytes stay charged until final drop. +#[derive(Clone)] +pub struct HistoryBytesPermit(Arc); +impl HistoryBytesPermit { + pub fn bytes(&self) -> usize { + *self.0 + } +} +#[derive(Default)] +struct AdmissionState { + participants: Vec>, + payloads: Vec>, + buffers: Vec>, + snapshots: Vec, + closed: bool, + payload_order: u64, +} +struct InstallationHistoryAdmission { + state: RefCell, + admitting: Cell, +} +/// An explicit handle to the existing retained installation, shared by every partition. +#[derive(Clone)] +pub struct HistoryAdmissionHandle(Rc); + +enum Requester { + None, + Entity(EntityId), + Entities(Vec), + Observed { + registration: *const RegisteredHistory, + entities: Vec, + }, + Registration(*const RegisteredHistory), +} + +struct AdmissionTurn<'a>(&'a Cell); +impl Drop for AdmissionTurn<'_> { + fn drop(&mut self) { + self.0.set(false); + } +} +impl HistoryAdmissionHandle { + #[cfg(any(test, feature = "test-support"))] + pub(crate) fn new() -> Self { + Self::with_initial_payload(false).0 + } + /// Installation construction can move one already prepared body batch into + /// its very first generation. No existing owner/quota can race this step. + pub(crate) fn with_initial_payload(prepared: bool) -> (Self, Option) { + let allocation = prepared.then(|| Arc::new(PayloadAllocation { order: 1 })); + let state = AdmissionState { + payloads: allocation.iter().map(Arc::downgrade).collect(), + payload_order: u64::from(prepared), + ..AdmissionState::default() + }; + let handle = Self(Rc::new(InstallationHistoryAdmission { + state: RefCell::new(state), + admitting: Cell::new(false), + })); + (handle, allocation.map(HistoryPayloadPermit)) + } + pub fn register( + &self, + participant: Rc, + ) -> Result { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + state.participants.retain(|entry| entry.strong_count() > 0); + let registration = Rc::new(RegisteredHistory { + owner: Rc::downgrade(&self.0), + participant, + }); + state.participants.push(Rc::downgrade(®istration)); + Ok(HistoryRegistration(registration)) + } + pub fn reserve_payload(&self) -> Result { + self.reserve_payload_to(MAX_NORMAL_HISTORY_PAYLOADS) + } + fn reserve_payload_to( + &self, + limit: usize, + ) -> Result { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + state + .snapshots + .retain(local_allocations::TrackedSnapshot::alive); + state.payloads.retain(|entry| entry.strong_count() > 0); + if state.payloads.len() >= limit { + return Err(HistoryAdmissionError::PayloadCapacity); + } + let order = state + .payload_order + .checked_add(1) + .ok_or(HistoryAdmissionError::PayloadCapacity)?; + state.payload_order = order; + let allocation = Arc::new(PayloadAllocation { order }); + state.payloads.push(Arc::downgrade(&allocation)); + Ok(HistoryPayloadPermit(allocation)) + } + /// Reclaim across Local and Cloud using one installation allocation order. + /// Skip the entity currently borrowed by the caller. All callbacks run + /// outside the budget's RefCell borrow, and nested decisions fail closed. + pub fn reserve_payload_reclaiming( + &self, + requester: Option, + cx: &mut App, + ) -> Result { + self.reserve_payload_excluding(&requester.into_iter().collect::>(), cx) + } + fn reserve_payload_excluding( + &self, + excluded: &[EntityId], + cx: &mut App, + ) -> Result { + if self.0.admitting.replace(true) { + return Err(HistoryAdmissionError::Reentrant); + } + let _turn = AdmissionTurn(&self.0.admitting); + let mut attempted = HashSet::new(); + let maximum_attempts = self + .observations()? + .0 + .len() + .saturating_add(MAX_HISTORY_PAYLOADS); + loop { + match self.reserve_payload() { + Ok(permit) => return Ok(permit), + Err(HistoryAdmissionError::PayloadCapacity) => {} + Err(error) => return Err(error), + } + let (participants, _) = self.observations()?; + let victim = participants + .into_iter() + .filter(|entry| { + entry + .participant + .entity_id() + .is_none_or(|id| !excluded.contains(&id)) + }) + .filter_map(|entry| { + entry + .participant + .oldest_payload(cx) + .filter(|order| !attempted.contains(&(Rc::as_ptr(&entry), *order))) + .map(|order| (entry, order)) + }) + .min_by_key(|(_, order)| *order) + .ok_or(HistoryAdmissionError::PayloadCapacity)?; + if attempted.len() >= maximum_attempts { + return Err(HistoryAdmissionError::PayloadCapacity); + } + attempted.insert((Rc::as_ptr(&victim.0), victim.1)); + victim.0.participant.evict_payload(cx); + // Re-observe live allocations, including caller-retained rows. A + // allocation ordinal is tried once, even if a callback reports success + // without releasing it. At most the32live payloads can be reclaimed. + } + } + pub fn reserve_bytes( + &self, + requester: &HistoryRegistration, + bytes: usize, + cx: &mut App, + ) -> Result { + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + self.reserve(Requester::Registration(Rc::as_ptr(&requester.0)), bytes, cx) + } + /// Move a previously observed Local prepared body into a proof-backed + /// buffer. The remaining source plus destination are admitted together. + fn reserve_observed_buffer( + &self, + requester: &HistoryRegistration, + entities: Vec, + remaining: usize, + bytes: usize, + cx: &mut App, + ) -> Result { + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + self.decide( + Requester::Observed { + registration: Rc::as_ptr(&requester.0), + entities, + }, + remaining.saturating_add(bytes), + cx, + )?; + self.charge(bytes) + } + fn require_payload(&self, payload: &HistoryPayloadPermit) -> Result<(), HistoryAdmissionError> { + let identity = Arc::downgrade(&payload.0); + if self + .0 + .state + .borrow() + .payloads + .iter() + .any(|entry| entry.ptr_eq(&identity)) + { + Ok(()) + } else { + Err(HistoryAdmissionError::ForeignAllocation) + } + } + /// Additional temporary work while the requester is mutably borrowed. Its + /// supplied existing bytes stay counted, but only the new buffer is charged + /// by the returned token. No requester callback is invoked. + pub fn reserve_buffer( + &self, + requester: &HistoryRegistration, + existing: usize, + bytes: usize, + cx: &mut App, + ) -> Result { + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + self.decide( + Requester::Registration(Rc::as_ptr(&requester.0)), + existing.saturating_add(bytes), + cx, + )?; + self.charge(bytes) + } + /// Admission before a new entity or retained native result is materialized. + pub fn reserve_unowned_bytes( + &self, + bytes: usize, + cx: &mut App, + ) -> Result { + self.reserve(Requester::None, bytes, cx) + } + /// Native metadata work is reserved while its owning Runtime is borrowed. + /// Other installations/participants remain visible to this same decision. + pub(crate) fn reserve_entity_bytes( + &self, + requester: EntityId, + bytes: usize, + cx: &mut App, + ) -> Result { + self.reserve(Requester::Entity(requester), bytes, cx) + } + /// Transfer a unique reservation to a smaller immutable allocation. Shared + /// reservations retain their conservative original charge until last drop. + pub(crate) fn shrink_buffer( + &self, + allocation: HistoryBytesPermit, + bytes: usize, + ) -> Result { + if bytes > allocation.bytes() { + return Err(HistoryAdmissionError::AllocationGrowth { + reserved: allocation.bytes(), + requested: bytes, + }); + } + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + let original = Arc::downgrade(&allocation.0); + if !state.buffers.iter().any(|entry| entry.ptr_eq(&original)) { + return Err(HistoryAdmissionError::ForeignAllocation); + } + if Arc::strong_count(&allocation.0) > 1 { + return Ok(allocation); + } + let smaller = Arc::new(bytes); + state.buffers.push(Arc::downgrade(&smaller)); + // No callback, await or externally visible decision occurs between the + // replacement and releasing its old proof. Both byte counts are immutable. + drop(state); + drop(allocation); + Ok(HistoryBytesPermit(smaller)) + } + fn reserve( + &self, + requester: Requester, + bytes: usize, + cx: &mut App, + ) -> Result { + self.decide(requester, bytes, cx)?; + self.charge(bytes) + } + fn charge(&self, bytes: usize) -> Result { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + let allocation = Arc::new(bytes); + state.buffers.push(Arc::downgrade(&allocation)); + Ok(HistoryBytesPermit(allocation)) + } + fn observations(&self) -> Result<(Vec>, usize), HistoryAdmissionError> { + let mut state = self.0.state.borrow_mut(); + if state.closed { + return Err(HistoryAdmissionError::Closed); + } + state.participants.retain(|entry| entry.strong_count() > 0); + state + .snapshots + .retain(local_allocations::TrackedSnapshot::alive); + state.buffers.retain(|entry| entry.strong_count() > 0); + let participants = state + .participants + .iter() + .filter_map(Weak::upgrade) + .collect(); + let buffers = state + .buffers + .iter() + .filter_map(SyncWeak::upgrade) + .fold(0usize, |total, bytes| total.saturating_add(*bytes)); + Ok((participants, buffers)) + } + /// Local admissions and Cloud reservations use exactly this same decision. + pub(crate) fn admit( + &self, + requester: Option, + incoming: usize, + cx: &mut App, + ) -> Result, HistoryAdmissionError> { + self.decide( + requester.map_or(Requester::None, Requester::Entity), + incoming, + cx, + ) + } + fn decide( + &self, + requester: Requester, + incoming: usize, + cx: &mut App, + ) -> Result, HistoryAdmissionError> { + self.decide_to(requester, incoming, MAX_NORMAL_HISTORY_BYTES, cx) + } + fn decide_to( + &self, + requester: Requester, + incoming: usize, + limit: usize, + cx: &mut App, + ) -> Result, HistoryAdmissionError> { + if self.0.admitting.replace(true) { + return Err(HistoryAdmissionError::Reentrant); + } + let _turn = AdmissionTurn(&self.0.admitting); + let mut attempted = HashSet::new(); + let mut evicted = Vec::new(); + let maximum_attempts = self + .observations()? + .0 + .len() + .saturating_add(MAX_HISTORY_PAYLOADS); + loop { + let (participants, mut retained) = self.observations()?; + let mut candidates = Vec::new(); + for participant in participants { + let is_requester = match &requester { + Requester::None => false, + Requester::Entity(id) => participant.participant.entity_id() == Some(*id), + Requester::Entities(ids) => participant + .participant + .entity_id() + .is_some_and(|id| ids.contains(&id)), + Requester::Registration(ptr) => Rc::as_ptr(&participant) == *ptr, + Requester::Observed { + registration, + entities, + } => { + Rc::as_ptr(&participant) == *registration + || participant + .participant + .entity_id() + .is_some_and(|id| entities.contains(&id)) + } + }; + if is_requester { + continue; + } + let usage = participant.participant.usage(cx)?; + if let Some(usage) = usage { + retained = retained.saturating_add(usage.bytes()); + } + if let Some(candidate) = EvictionCandidate::new(participant, usage, cx) + && !attempted.contains(&candidate.identity()) + { + candidates.push(candidate); + } + } + if self.0.state.borrow().closed { + return Err(HistoryAdmissionError::Closed); + } + if retained.saturating_add(incoming) <= limit { + return Ok(evicted); + } + candidates.sort_by_key(EvictionCandidate::priority); + let Some(victim) = candidates + .into_iter() + .next() + .filter(|_| attempted.len() < maximum_attempts) + else { + return Err(HistoryAdmissionError::ResidentCapacity { + retained, + incoming, + limit, + }); + }; + attempted.insert(victim.identity()); + if let Some(key) = victim.evict(cx) { + evicted.push(key); + } + // Re-observe actual data and retained buffers after the callback. A + // callback may release registrations or immutable writer generations. + } + } + /// A bounded allowance for an already captured urgent control. The service + /// selects this only for original Stop, never normal prompt/data allocations. + /// It consumes the same hard32slots/64MiB; it cannot extend either bound. + pub fn reserve_urgent_control( + &self, + requester: &HistoryRegistration, + bytes: usize, + payloads: usize, + cx: &mut App, + ) -> Result { + if payloads > 2 { + return Err(HistoryAdmissionError::PayloadCapacity); + } + if bytes > URGENT_HISTORY_BYTES { + return Err(HistoryAdmissionError::ResidentCapacity { + retained: 0, + incoming: bytes, + limit: URGENT_HISTORY_BYTES, + }); + } + let owner = requester + .0 + .owner + .upgrade() + .ok_or(HistoryAdmissionError::Closed)?; + if !Rc::ptr_eq(&owner, &self.0) { + return Err(HistoryAdmissionError::ForeignRegistration); + } + let mut slots = Vec::with_capacity(payloads); + for _ in 0..payloads { + slots.push(self.reserve_payload_to(MAX_HISTORY_PAYLOADS)?); + } + self.decide_to( + Requester::Registration(Rc::as_ptr(&requester.0)), + bytes, + MAX_RESIDENT_BYTES, + cx, + )?; + let proof = self.charge(bytes)?; + Ok(nocterm_core::allocation::AllocationLease::new( + bytes, + payloads, + (proof, slots), + )) + } + pub(crate) fn close(&self) { + self.0.state.borrow_mut().closed = true; + } +} diff --git a/crates/agent-runtime/src/history_admission/eviction.rs b/crates/agent-runtime/src/history_admission/eviction.rs new file mode 100644 index 00000000..db061c54 --- /dev/null +++ b/crates/agent-runtime/src/history_admission/eviction.rs @@ -0,0 +1,65 @@ +//! One weak participant's offered history or immutable-payload reclamation. +use super::{HistoryUsage, RegisteredHistory}; +use gpui::App; +use nocterm_ai::execution_origin::ChatKey; +use std::rc::Rc; + +enum EvictionKind { + History, + Payload, +} +pub(super) struct EvictionCandidate { + participant: Rc, + age: u64, + kind: EvictionKind, +} +impl EvictionCandidate { + pub(super) fn new( + participant: Rc, + usage: Option, + cx: &App, + ) -> Option { + if let Some(usage) = usage { + if usage.shown() { + return None; + } + if usage.bytes() > 0 { + return Some(Self { + participant, + age: usage.updated(), + kind: EvictionKind::History, + }); + } + } + let age = participant.participant.oldest_payload(cx)?; + Some(Self { + participant, + age, + kind: EvictionKind::Payload, + }) + } + // Reclaim cold metadata before hidden transcript history. Payload ordinals + // and history timestamps are compared only within their own class. + pub(super) fn priority(&self) -> (bool, u64) { + (matches!(self.kind, EvictionKind::History), self.age) + } + pub(super) fn identity(&self) -> (*const RegisteredHistory, (bool, u64)) { + (Rc::as_ptr(&self.participant), self.priority()) + } + pub(super) fn evict(&self, cx: &mut App) -> Option { + match self.kind { + EvictionKind::History => { + let key = self.participant.participant.local_key(cx); + self.participant + .participant + .evict(cx) + .then_some(key) + .flatten() + } + EvictionKind::Payload => { + self.participant.participant.evict_payload(cx); + None + } + } + } +} diff --git a/crates/agent-runtime/src/history_admission/local.rs b/crates/agent-runtime/src/history_admission/local.rs new file mode 100644 index 00000000..b4a0bd45 --- /dev/null +++ b/crates/agent-runtime/src/history_admission/local.rs @@ -0,0 +1,32 @@ +//! Existing local session clients participate through their unchanged history gates. +use super::HistoryParticipant; +use crate::{Client, HistoryUsage}; +use gpui::{App, EntityId}; +use nocterm_ai::{execution_origin::ChatKey, storage::AgentStorageError}; + +pub(crate) struct SessionHistory(Client); +impl SessionHistory { + pub(crate) fn new(client: Client) -> Self { + Self(client) + } +} +impl HistoryParticipant for SessionHistory { + fn entity_id(&self) -> Option { + Some(self.0.id()) + } + fn usage(&self, cx: &App) -> Result, AgentStorageError> { + Ok(self.0.alive().then(|| self.0.history_usage(cx)).flatten()) + } + fn evict(&self, cx: &mut App) -> bool { + self.0.evict_history(cx) + } + fn oldest_payload(&self, cx: &App) -> Option { + self.0.history_payload(cx) + } + fn evict_payload(&self, cx: &mut App) -> bool { + self.0.evict_history(cx) + } + fn local_key(&self, cx: &App) -> Option { + self.0.state(cx).map(|state| state.origin().key().clone()) + } +} diff --git a/crates/agent-runtime/src/history_admission/local_allocations.rs b/crates/agent-runtime/src/history_admission/local_allocations.rs new file mode 100644 index 00000000..de29104a --- /dev/null +++ b/crates/agent-runtime/src/history_admission/local_allocations.rs @@ -0,0 +1,170 @@ +//! Local immutable generations follow the actual Arc through the existing writer. +use super::{ + HistoryAdmissionError, HistoryAdmissionHandle, HistoryBytesPermit, HistoryPayloadPermit, + HistoryRegistration, Requester, +}; +use gpui::{App, EntityId}; +use nocterm_ai::{history::SharedChat, storage::AgentStorageError}; +use std::sync::{Arc, Weak}; + +pub(super) struct TrackedSnapshot { + value: Weak, + _bytes: HistoryBytesPermit, + _payload: HistoryPayloadPermit, +} +impl TrackedSnapshot { + pub(super) fn alive(&self) -> bool { + self.value.strong_count() > 0 + } +} +/// Captured before any Runtime/Thread mutable borrow. It performs no service lookup. +#[derive(Clone)] +pub struct LocalHistoryAllocations { + admission: HistoryAdmissionHandle, + runtime: EntityId, +} +pub struct LocalSnapshotAllocation { + admission: HistoryAdmissionHandle, + bytes: HistoryBytesPermit, + payload: HistoryPayloadPermit, +} +impl LocalHistoryAllocations { + pub(crate) fn new(admission: HistoryAdmissionHandle, runtime: EntityId) -> Self { + Self { admission, runtime } + } + pub(crate) fn handle(&self) -> HistoryAdmissionHandle { + self.admission.clone() + } + pub fn history_admission(&self) -> HistoryAdmissionHandle { + self.admission.clone() + } + pub(crate) fn retain_external( + &self, + requester: EntityId, + existing: usize, + value: Arc, + cx: &mut App, + ) -> Result, AgentStorageError> { + let tracked = self + .admission + .0 + .state + .borrow() + .snapshots + .iter() + .any(|entry| entry.value.ptr_eq(&Arc::downgrade(&value))); + if tracked { + return Ok(value); + } + let capacity = value.retained_bytes()?; + self.reserve_snapshot(requester, existing, capacity, cx) + .map_err(HistoryAdmissionError::into_storage)? + .retain(value) + } + pub(crate) fn transfer_prepared( + &self, + source: &HistoryRegistration, + remaining: usize, + bytes: usize, + payload: HistoryPayloadPermit, + cx: &mut App, + ) -> Result { + self.admission + .require_payload(&payload) + .map_err(HistoryAdmissionError::into_storage)?; + let proof = self + .admission + .reserve_observed_buffer(source, vec![self.runtime], remaining, bytes, cx) + .map_err(HistoryAdmissionError::into_storage)?; + Ok(nocterm_core::allocation::AllocationLease::new( + bytes, + 1, + (proof, payload), + )) + } + pub fn reserve_resident( + &self, + requester: Option, + cx: &mut App, + ) -> Result { + let mut excluded = vec![self.runtime]; + excluded.extend(requester); + self.admission.reserve_payload_excluding(&excluded, cx) + } + /// Snapshot metadata/rows are measured while borrowed, before cloning them. + pub fn reserve_snapshot( + &self, + requester: EntityId, + existing: usize, + capacity: usize, + cx: &mut App, + ) -> Result { + let excluded = vec![self.runtime, requester]; + let payload = self.admission.reserve_payload_excluding(&excluded, cx)?; + self.admission.decide( + Requester::Entities(excluded), + existing.saturating_add(capacity), + cx, + )?; + let bytes = self.admission.charge(capacity)?; + Ok(LocalSnapshotAllocation { + admission: self.admission.clone(), + bytes, + payload, + }) + } +} +impl LocalSnapshotAllocation { + /// The actual decoded body generation moves to its resident owner with this + /// same slot. Bytes remain with the worker/result until atomic publication. + pub fn decode_allocation( + self, + ) -> ( + nocterm_core::allocation::AllocationLease, + HistoryPayloadPermit, + ) { + let slot = self.payload.clone(); + (self.into_lease(), slot) + } + + /// Transfer an already reserved standalone preparation into its actual owner. + pub fn into_lease(self) -> nocterm_core::allocation::AllocationLease { + nocterm_core::allocation::AllocationLease::new( + self.bytes.bytes(), + 1, + (self.bytes, self.payload), + ) + } + + /// Track the actual SharedChat allocation, including active/latest generations, + /// shutdown capture, and a detached native worker after its GUI owner drops. + /// Arc clones/retries keep this one proof, not one proof per observer. + pub fn retain(self, value: Arc) -> Result, AgentStorageError> { + let actual = value.retained_bytes()?; + if actual > self.bytes.bytes() { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: actual, + limit: self.bytes.bytes(), + }); + } + let bytes = self + .admission + .shrink_buffer(self.bytes, actual) + .map_err(HistoryAdmissionError::into_storage)?; + let mut state = self.admission.0.state.borrow_mut(); + state.snapshots.retain(TrackedSnapshot::alive); + if !state + .snapshots + .iter() + .any(|entry| entry.value.ptr_eq(&Arc::downgrade(&value))) + { + state.snapshots.push(TrackedSnapshot { + value: Arc::downgrade(&value), + _bytes: bytes, + _payload: self.payload, + }); + } + Ok(value) + } +} diff --git a/crates/agent-runtime/src/history_admission/testing.rs b/crates/agent-runtime/src/history_admission/testing.rs new file mode 100644 index 00000000..7475a24a --- /dev/null +++ b/crates/agent-runtime/src/history_admission/testing.rs @@ -0,0 +1,120 @@ +//! Shared external history participant for Local/remote admission behavior tests. +use super::{HistoryParticipant, HistoryPayloadPermit}; +use crate::HistoryUsage; +use gpui::App; +use nocterm_ai::storage::AgentStorageError; +use std::{ + cell::RefCell, + rc::{Rc, Weak}, +}; + +#[derive(Default)] +struct ProbeState { + usage: Option, + evictable: bool, + payloads: Vec, + evictions: usize, + on_evict: Option>, + false_payload_success: bool, +} +/// The participant adapter keeps only a weak reference to this external owner. +#[derive(Clone, Default)] +pub struct HistoryProbe(Rc>); +struct ProbeParticipant(Weak>); +impl HistoryProbe { + /// Uses the real installation admission owner without a connector fixture. + pub fn installation() -> super::HistoryAdmissionHandle { + super::HistoryAdmissionHandle::new() + } + /// Drives the real allocation transfer through the shared test collaborator. + pub fn transfer_buffer( + admission: &super::HistoryAdmissionHandle, + allocation: super::HistoryBytesPermit, + bytes: usize, + ) -> Result { + admission.shrink_buffer(allocation, bytes) + } + /// Exercise a dishonest/no-progress collaborator without a second fake. + pub fn report_payload_success_without_release(&self) { + self.0.borrow_mut().false_payload_success = true; + } + pub fn participant(&self) -> Rc { + Rc::new(ProbeParticipant(Rc::downgrade(&self.0))) + } + pub fn set_history(&self, usage: HistoryUsage, evictable: bool) { + let mut state = self.0.borrow_mut(); + state.usage = Some(usage); + state.evictable = evictable; + } + pub fn retain_payload(&self, permit: HistoryPayloadPermit) { + self.0.borrow_mut().payloads.push(permit); + } + pub fn payload_count(&self) -> usize { + self.0.borrow().payloads.len() + } + pub fn history(&self) -> Option { + self.0.borrow().usage + } + pub fn evictions(&self) -> usize { + self.0.borrow().evictions + } + pub fn on_evict(&self, callback: Rc) { + self.0.borrow_mut().on_evict = Some(callback); + } +} +impl HistoryParticipant for ProbeParticipant { + fn usage(&self, _cx: &App) -> Result, AgentStorageError> { + Ok(self.0.upgrade().and_then(|state| state.borrow().usage)) + } + fn evict(&self, cx: &mut App) -> bool { + let Some(owner) = self.0.upgrade() else { + return false; + }; + let callback = { + let mut state = owner.borrow_mut(); + if !state.evictable || state.usage.is_some_and(|usage| usage.shown()) { + return false; + } + state.usage = None; + state.evictions += 1; + state.on_evict.clone() + }; + if let Some(callback) = callback { + callback(cx); + } + true + } + fn oldest_payload(&self, _cx: &App) -> Option { + self.0.upgrade().and_then(|owner| { + let state = owner.borrow(); + if !state.evictable || state.usage.is_some_and(|usage| usage.shown()) { + return None; + } + state.payloads.iter().map(HistoryPayloadPermit::order).min() + }) + } + fn evict_payload(&self, _cx: &mut App) -> bool { + let Some(owner) = self.0.upgrade() else { + return false; + }; + let mut state = owner.borrow_mut(); + if !state.evictable || state.usage.is_some_and(|usage| usage.shown()) { + return false; + } + state.evictions += 1; + if state.false_payload_success { + return true; + } + let Some(index) = state + .payloads + .iter() + .enumerate() + .min_by_key(|(_, permit)| permit.order()) + .map(|(index, _)| index) + else { + return false; + }; + state.payloads.remove(index); + true + } +} diff --git a/crates/agent-runtime/src/holders.rs b/crates/agent-runtime/src/holders.rs new file mode 100644 index 00000000..49798d11 --- /dev/null +++ b/crates/agent-runtime/src/holders.rs @@ -0,0 +1,20 @@ +//! What the windows of one installation tell each other about the chats they hold. +//! +//! A chat's document belongs to the one window that holds it; the other windows list it in +//! their history without holding it. Opening it there asks its window to show it and come +//! forward, or, while it neither works nor shows, moves it: the asking window claims it in +//! place of that window's chat, and that window is told to stop listing it. +use gpui::{EntityId, EventEmitter}; + +/// What the runtime tells every window about the chats windows hold. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HolderEvent { + /// A window began to hold a chat. + Changed, + /// The window holding chat `chat` shows it and comes forward. + Reveal { chat: EntityId }, + /// Chat `chat` moved to another window: its window stops listing it. + Moved { chat: EntityId }, +} + +impl EventEmitter for crate::Runtime {} diff --git a/crates/agent-runtime/src/lib.rs b/crates/agent-runtime/src/lib.rs index d861055e..f441cc31 100644 --- a/crates/agent-runtime/src/lib.rs +++ b/crates/agent-runtime/src/lib.rs @@ -4,11 +4,22 @@ //! keyed write admission, lazy history hydration and usage projections. //! Does not own: settings storage, history/favorites files, ACP subprocesses or views. //! Ports used: AgentConnector, ToolBridge, AgentStateRepository, ChatRepository, -//! AccountUsageSource, WorkdirResolver, explicit SettingsHandle and host-supplied ShutdownCoordinator +//! WorkdirResolver, CommandJobsFactory, explicit SettingsHandle and host-supplied ShutdownCoordinator //! at installation. -//! Ports provided: SessionClient/SessionEvent, HistoryReader and synchronous quit capture. +//! Ports provided: SessionClient/SessionEvent, HistoryReader, HolderEvent (what windows hold, +//! reveal and move requests) and synchronous quit capture. //! Settings section: ai consumer. Globals: none. -//! Invariants: one current document owner per ID; delete seals an ID; persistence +//! Invariants: one current document owner per scoped ChatKey; noncloneable reservations +//! precede entity construction, and explicit restart replaces only its expected owner. A +//! chat moves to another window only while it neither works nor shows in its own. +//! Scope-bound handles/readers and callbacks retain original ownership. Catalog payloads +//! share one installation 32-slot limit, including retained snapshots, archived row references +//! and pending work across every Local environment. Handles retain authenticated account +//! leases independently of General/Project context; unavailable accounts have no fallback. +//! Hydration admission includes all contexts/windows/accounts, remaining prepared +//! records and immutable active/latest writer values; shown chats cannot be evicted. +//! All contexts use the same local agent launcher. Delete seals +//! only its scoped key; persistence //! admission and physical publication are separate revision-bearing events. Registry //! stamps change only after writer acceptance; final work reads no App and joins //! captured admission failures with publication failures. Shared cleanup diagnostics @@ -18,17 +29,24 @@ use nocterm_ai::model::auth::{AuthFlow, AuthMethod}; use nocterm_ai::model::controls::ConfigOption; use nocterm_ai::model::session::SessionUpdate; +mod error; +pub use error::{AgentActivationError, ChatOpenError}; +mod execution; mod handle; pub use handle::RuntimeHandle; +mod holders; +pub use holders::HolderEvent; mod admission; mod bridge; mod catalog; mod client; +pub mod history_admission; mod releases; mod usage; -pub use catalog::PreparedHistory; +pub use catalog::{CatalogLoadError, CatalogRow, CatalogSnapshot, PreparedHistory}; mod connections; mod documents; +pub use documents::{DocumentClaim, DocumentClaimOutcome}; mod lease; mod lifecycle; mod process_cleanup; @@ -36,7 +54,7 @@ mod startup; mod state; use futures::future::BoxFuture; mod storage; -pub use client::{Client, ClientState, SessionClient, SessionEvent}; +pub use client::{Client, ClientState, HistoryUsage, SessionClient, SessionEvent}; use gpui::{App, Context, Entity, EntityId, Task}; pub use lease::SessionLease; use nocterm_ai::{ @@ -44,7 +62,9 @@ use nocterm_ai::{ ToolBridge, }; use std::{collections::HashMap, path::PathBuf, sync::Arc}; -pub use storage::{AgentPersistenceError, AgentStorage, HistoryReader}; +pub use storage::{ + AgentPersistenceError, AgentStorage, HistoryReader, LoadedHistory, PreparedHistoryRead, +}; pub struct TerminalAuthRequest { pub program: String, @@ -60,9 +80,10 @@ pub struct RuntimeServices { pub agents: nocterm_agent_directory::AgentsHandle, pub connector: Arc, pub bridge: Arc, + pub command_jobs: Arc, + /// Starts and follows the file transfers chats run for their agents. + pub transfers: Arc, pub storage: AgentStorage, - /// Account usage is read through a host-injected native capability. - pub usage_source: Arc, pub workdir_resolver: Arc, pub workdir: PathBuf, /// Whether the host can run an agent's sign-in command in a terminal. @@ -75,11 +96,7 @@ pub struct RuntimeServices { pub shared_dirs: Vec, } struct Connection { - chat_id: String, - launch: AgentLaunch, - workdir: PathBuf, - /// Whether the process runs isolated; a changed setting restarts it. - isolated: bool, + identity: execution::ExecutionIdentity, commands: Option>, info: Option, users: HashMap, @@ -119,11 +136,10 @@ impl Runtime { let state = state::AgentStateStore::new(&services.storage, cx); let chats = documents::ChatStore::new(&mut services.storage, cx); let releases = releases::LeaseInbox::new(cx); - let usage = usage::UsageLimits::new(services.usage_source.clone()); Self { state, chats, - usage, + usage: Default::default(), services, connections: Default::default(), admission: Default::default(), @@ -142,7 +158,7 @@ impl Runtime { let keys: Vec<_> = self .connections .iter() - .filter(|(_, connection)| agents.get(&connection.launch.id).is_none()) + .filter(|(_, connection)| agents.get(&connection.identity.launch().id).is_none()) .map(|(key, _)| *key) .collect(); for key in keys { @@ -175,7 +191,7 @@ impl Runtime { let keys: Vec<_> = self .connections .iter() - .filter(|(_, connection)| connection.isolated != isolated) + .filter(|(_, connection)| connection.identity.isolation_changed(isolated)) .map(|(key, _)| *key) .collect(); for key in keys { @@ -258,24 +274,19 @@ impl Runtime { self.state.error(cx) } pub fn history_reader(&self) -> HistoryReader { - self.chats.reader() + self.chats + .reader(nocterm_ai::history::ContextId::General.into()) + } + pub(crate) fn scoped_history_reader( + &self, + scope: nocterm_ai::local_environment::ChatScope, + ) -> HistoryReader { + self.chats.reader(scope) } /// The plan limits last reported for `agent`. pub fn limits(&self, agent: &str) -> Option<&nocterm_ai::usage::Limits> { self.usage.get(agent) } - /// Reads `agent`'s plan limits again, from agents that keep them in their - /// own files. Others report them along with usage. - pub fn refresh_limits(&mut self, agent: &str, cx: &mut Context) { - if self.shutting_down { - return; - } - let Some(agent) = self.services.agents.get(agent, cx) else { - return; - }; - self.usage - .refresh(nocterm_ai::usage::UsageAgent::new(&agent), cx); - } pub fn start_bridge(&mut self, cx: &mut Context) { if self.shutting_down { return; @@ -288,6 +299,18 @@ impl Runtime { client: &Client, cx: &mut Context, ) -> Result { + self.register_scoped_bridge(nocterm_ai::history::ContextId::General.into(), client, cx) + } + pub(crate) fn register_scoped_bridge( + &mut self, + scope: nocterm_ai::local_environment::ChatScope, + client: &Client, + cx: &mut Context, + ) -> Result { + let state = client + .state(cx) + .ok_or(nocterm_ai::connection::BridgeRegistrationError::ClientClosed)?; + state.origin().require_chat_scope(&scope)?; if !self.ai_enabled(cx) { return Err(nocterm_ai::connection::BridgeRegistrationError::AiDisabled); } @@ -305,7 +328,7 @@ impl Runtime { return None; }; let connection = self.connections.get(key)?; - let launch = &connection.launch; + let launch = connection.identity.launch(); let mut args = launch.args.clone(); args.extend(setup.args.iter().cloned()); let mut env = nocterm_ai::env::sanitized_environment(launch, std::env::vars()); @@ -324,7 +347,7 @@ impl Runtime { program: launch.command.clone(), args, env, - cwd: connection.workdir.clone(), + cwd: connection.identity.workdir().to_owned(), title: format!("Sign in: {}", method.label), }) } @@ -334,13 +357,32 @@ impl Runtime { chat_id: String, launch: AgentLaunch, cx: &mut Context, - ) { + ) -> Result<(), AgentActivationError> { + self.connect_scoped( + nocterm_ai::history::ContextId::General.into(), + client, + chat_id, + launch, + cx, + ) + } + pub(crate) fn connect_scoped( + &mut self, + scope: nocterm_ai::local_environment::ChatScope, + client: Client, + chat_id: String, + launch: AgentLaunch, + cx: &mut Context, + ) -> Result<(), AgentActivationError> { + let state = client.state(cx).ok_or(AgentActivationError::ClientClosed)?; + documents::ChatStore::require_client_scope(scope.clone(), &client, cx)?; + let origin = execution::admitted_origin(state.origin(), &chat_id)?; if self.shutting_down || !self.ai_enabled(cx) { - return; + return Ok(()); } - let workdir = client - .state(cx) - .and_then(|state| state.workdir) + let workdir = origin + .provider_resume() + .map(std::path::Path::to_owned) .unwrap_or_else(|| { self.ai(cx) .working_directory @@ -355,15 +397,17 @@ impl Runtime { ), cx, ); - return; + return Ok(()); } let isolated = self.ai(cx).sandbox == nocterm_ai::SandboxMode::Workspace; - let key = self.connections.reserve_key(&launch, &workdir, isolated); - let registration = match self.register_bridge(&client, cx) { + let identity = + execution::ExecutionIdentity::new(origin, launch.clone(), workdir.clone(), isolated); + let key = self.connections.reserve_key(&identity); + let registration = match self.register_scoped_bridge(scope, &client, cx) { Ok(registration) => registration, Err(error) => { client.emit(SessionEvent::Failed(error.to_string()), cx); - return; + return Ok(()); } }; let lease = SessionLease::new(client.id(), key, registration, self.releases.sender()); @@ -385,10 +429,7 @@ impl Runtime { self.connections.insert( key, Connection { - chat_id, - launch: launch.clone(), - workdir: workdir.clone(), - isolated, + identity, commands: None, info: None, users: HashMap::from([(client.id(), client.clone())]), @@ -411,6 +452,7 @@ impl Runtime { } }); self.connections.connecting(key, connecting); + Ok(()) } fn accept_startup( &mut self, @@ -420,6 +462,13 @@ impl Runtime { effective: PathBuf, cx: &mut Context, ) -> bool { + if !self + .connections + .get(key) + .is_some_and(|connection| self.chats.source_current(connection.identity.key())) + { + return false; + } if !self .connections .get(key) @@ -498,9 +547,9 @@ impl Runtime { AgentEvent::Barrier(_) => unreachable!("handled before connection routing"), AgentEvent::Session(notification) => { if let SessionUpdate::UsageUpdate(update) = ¬ification.update - && let Some(limit) = self.limit_in(&connection.launch, update) + && let Some(limit) = self.limit_in(connection.identity.launch(), update) { - let agent = connection.launch.id.clone(); + let agent = connection.identity.launch().id.clone(); self.usage.merge(agent, limit); cx.notify(); } @@ -520,7 +569,7 @@ impl Runtime { .find(|client| { client .state(cx) - .is_some_and(|state| state.session.as_ref() == Some(&request.session)) + .is_some_and(|state| state.session().as_ref() == Some(&request.session)) }) .cloned(); // A request no thread takes is answered `Cancelled` on drop. diff --git a/crates/agent-runtime/src/lifecycle.rs b/crates/agent-runtime/src/lifecycle.rs index dfa2b440..441551c7 100644 --- a/crates/agent-runtime/src/lifecycle.rs +++ b/crates/agent-runtime/src/lifecycle.rs @@ -4,9 +4,6 @@ use super::lease::ReleasedLease; use super::*; use std::time::Duration; impl Runtime { - pub fn register_document(&mut self, client: Client, cx: &mut Context) { - self.chats.register(client, cx); - } fn start_session_maintenance(&mut self, cx: &mut Context) { if !self.admission.running() { self.admission.install(cx.spawn(async move |this, cx| { @@ -33,16 +30,64 @@ impl Runtime { pub fn unregister_document(&mut self, id: EntityId, cx: &mut Context) { self.chats.retire(id, cx); self.admission.withdraw(id); + cx.notify(); + } + pub(crate) fn unregister_scoped_document( + &mut self, + scope: nocterm_ai::local_environment::ChatScope, + id: EntityId, + cx: &mut Context, + ) -> Result<(), nocterm_ai::storage::AgentStorageError> { + self.chats.require_owner_scope(scope, id, cx)?; + self.unregister_document(id, cx); + Ok(()) + } + pub(crate) fn request_scoped_activation( + &mut self, + scope: nocterm_ai::local_environment::ChatScope, + client: Client, + cx: &mut Context, + ) -> Result<(), crate::AgentActivationError> { + let state = client + .state(cx) + .ok_or(crate::AgentActivationError::ClientClosed)?; + state.origin().require_chat_scope(&scope)?; + documents::ChatStore::require_client_scope(scope.clone(), &client, cx)?; + self.request_activation(client, cx); + Ok(()) } pub fn request_activation(&mut self, client: Client, cx: &mut Context) { if self.shutting_down || !self.ai_enabled(cx) { return; } + if !client + .state(cx) + .is_some_and(|state| self.chats.current(state.origin().key(), client.id())) + { + return; + } self.admission.request(client); self.start_session_maintenance(cx); self.maintain_sessions(cx); } + pub(crate) fn refresh_environment_sources(&mut self, cx: &mut Context) { + self.revalidate_sources(cx); + // An explicit native notification can make fresh source captures available. + cx.notify(); + } + fn revalidate_sources(&mut self, cx: &mut Context) -> bool { + let revoked = self.chats.revoke_stale_sources(); + let changed = !revoked.is_empty(); + for client in revoked { + self.admission.withdraw(client.id()); + client.emit(SessionEvent::Shutdown, cx); + } + changed + } fn maintain_sessions(&mut self, cx: &mut Context) { + if self.revalidate_sources(cx) { + cx.notify(); + } if self.shutting_down || !self.ai_enabled(cx) { self.admission.clear(); return; @@ -66,8 +111,8 @@ impl Runtime { .collect::>(); ConnectionObservation::new( *key, - states.iter().any(|state| state.busy), - states.iter().any(|state| state.shown), + states.iter().any(|state| state.busy()), + states.iter().any(|state| state.shown()), ) }) .collect::>(); @@ -78,7 +123,7 @@ impl Runtime { .filter(|client| { client .state(cx) - .is_some_and(|state| state.activation_pending && !state.leased) + .is_some_and(|state| state.activation_pending() && !state.leased()) }) .count(); for key in self @@ -107,24 +152,37 @@ impl Runtime { let Some(state) = client.state(cx) else { continue; }; - if !self.chats.current(&state.chat_id, client.id()) - || state.leased - || !state.activation_pending + if !self.chats.current(state.origin().key(), client.id()) + || state.leased() + || !state.activation_pending() { continue; } - if state.closing || self.connections.same_chat(&state.chat_id) { + if state.closing() || self.connections.same_chat(state.origin().key()) { self.admission.defer(client); continue; } - if let Some(agent) = self.services.agents.get(&state.agent_id, cx) { - self.connect(client, state.chat_id, agent.launch().clone(), cx); + if let Some(agent) = self.services.agents.get(state.agent_id(), cx) { + if let Err(error) = self.connect_scoped( + state.origin().key().chat_scope(), + client.clone(), + state.origin().key().id().to_owned(), + agent.launch().clone(), + cx, + ) { + client.emit(SessionEvent::Failed(error.to_string()), cx); + } } else { client.emit(SessionEvent::AgentRemoved, cx); } } } - fn cleanup_failed(&mut self, chat_id: &str, error: &str, cx: &mut Context) { + fn cleanup_failed( + &mut self, + chat_id: &nocterm_ai::execution_origin::ChatKey, + error: &str, + cx: &mut Context, + ) { tracing::warn!(%error, "agent process cleanup could not be confirmed"); let clients: Vec<_> = self .chats @@ -133,7 +191,7 @@ impl Runtime { .filter(|client| { client .state(cx) - .is_some_and(|state| state.chat_id == chat_id) + .is_some_and(|state| state.origin().key() == chat_id) }) .collect(); for client in clients { @@ -153,7 +211,7 @@ impl Runtime { else { return; }; - let chat_id = connection.chat_id.clone(); + let chat_id = connection.identity.key().clone(); let connecting = connection._connecting.take(); let startup_completion = connection.startup_completion.take(); cx.spawn(async move |this, cx| { @@ -231,7 +289,6 @@ impl Runtime { let cleanup = self.connections.take_cleanup(); self.bridge.stop(); self.services.bridge.stop(); - self.usage.stop(); let executor = cx.background_executor().clone(); Box::pin(async move { // Independent process cleanup cannot be serialized behind synchronous file I/O. @@ -287,7 +344,7 @@ impl Runtime { session.is_none_or(|session| { connection.users.values().any(|client| { client.state(cx).is_some_and(|state| { - state.session.as_ref() == Some(session) + state.session().as_ref() == Some(session) }) }) }) diff --git a/crates/agent-runtime/src/storage.rs b/crates/agent-runtime/src/storage.rs index ecb76cbc..36e6478a 100644 --- a/crates/agent-runtime/src/storage.rs +++ b/crates/agent-runtime/src/storage.rs @@ -4,8 +4,9 @@ use nocterm_ai::{ storage::{AgentStateRepository, AgentStorageError, ChatRepository}, }; use std::sync::Arc; +mod admitted; +pub use admitted::LoadedHistory; -#[derive(Clone)] pub struct AgentStorage { state: Arc, chats: Arc, @@ -35,6 +36,11 @@ impl AgentStorage { self.history = Some(history); self } + pub(super) fn has_prepared_history(&self) -> bool { + self.history + .as_ref() + .is_some_and(|history| !history.records().is_empty()) + } pub(super) fn take_history(&mut self) -> Option { self.history.take() } @@ -64,24 +70,124 @@ impl AgentStorage { pub struct HistoryReader { repository: Arc, gate: nocterm_ai::history::HistoryGate, + scope: Option, + access: Option, } impl HistoryReader { pub(super) fn new( repository: Arc, gate: nocterm_ai::history::HistoryGate, + scope: nocterm_ai::local_environment::ChatScope, + ) -> Self { + Self { + repository, + gate, + scope: Some(scope), + access: Some(nocterm_ai::local_environment::LocalEnvironmentAccess::this_computer()), + } + } + pub(super) fn with_access( + mut self, + access: Option, + ) -> Self { + self.access = access; + self + } + pub(super) fn unavailable( + repository: Arc, + gate: nocterm_ai::history::HistoryGate, ) -> Self { - Self { repository, gate } + Self { + repository, + gate, + scope: None, + access: None, + } + } + /// The captured service performs native work and admits its actual decoded + /// generation before allocation. Cancelling this observer never frees work + /// still owned by an independent native worker/result. + pub fn load_admitted( + &self, + id: String, + admission: crate::history_admission::HistoryAdmissionHandle, + cx: &mut gpui::App, + ) -> gpui::Task { + admitted::AdmittedHistoryLoad::new(self.clone(), id, admission).start(cx) } - pub async fn load( + /// Preflight the original held source before any decoded body is allocated. + /// The existing gate bounds raw native scratch only; it grants no decoded quota. + pub async fn prepare_load( &self, id: String, ) -> ( - Result, + Result, nocterm_ai::history::HistoryPermit, ) { let permit = self.gate.acquire().await; - let loaded = self.repository.load(id).await; - (loaded, permit) + let prepared = + async { + let access = self.access.as_ref().ok_or( + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable, + )?; + let scope = self.scope.as_ref().ok_or( + nocterm_hubs_model::source_identity::LocalAccountSourceError::Unavailable, + )?; + access.check_current()?; + nocterm_ai::storage::require_local_environment( + access.environment(), + scope.environment(), + )?; + let load = self + .repository + .prepare_load_in(scope.clone(), id.clone()) + .await?; + access.check_current()?; + Ok(PreparedHistoryRead { + load, + access: access.clone(), + scope: scope.clone(), + id, + }) + } + .await; + (prepared, permit) + } +} +/// Retains the exact preflight source/bytes; decode cannot reopen a pathname. +pub struct PreparedHistoryRead { + load: Box, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, + scope: nocterm_ai::local_environment::ChatScope, + id: String, +} +impl PreparedHistoryRead { + pub fn resident_bytes(&self) -> usize { + self.load.resident_bytes() + } + pub async fn decode( + self, + allocation: nocterm_core::allocation::AllocationLease, + ) -> Result< + nocterm_core::allocation::Allocated, + AgentStorageError, + > { + self.access.check_current()?; + if allocation.payloads() == 0 || allocation.bytes() < self.load.resident_bytes() { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: self.load.resident_bytes(), + limit: allocation.bytes(), + }); + } + let chat = self.load.decode(allocation).await?; + self.access.check_current()?; + chat.value().validate_identity()?; + if chat.value().header.id != self.id { + return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); + } + nocterm_ai::storage::require_local_scope(&self.scope, &chat.value().header.chat_scope())?; + Ok(chat) } } diff --git a/crates/agent-runtime/src/storage/admitted.rs b/crates/agent-runtime/src/storage/admitted.rs new file mode 100644 index 00000000..68574ea8 --- /dev/null +++ b/crates/agent-runtime/src/storage/admitted.rs @@ -0,0 +1,124 @@ +//! One service pipeline owns native preflight, shared admission and actual workers. +use super::HistoryReader; +use crate::history_admission::{HistoryAdmissionHandle, HistoryPayloadPermit}; +use futures::channel::oneshot; +use gpui::{App, BackgroundExecutor, Task}; +use nocterm_ai::{ + history::{HistoryPermit, SavedChat}, + storage::AgentStorageError, +}; +use nocterm_core::allocation::{Allocated, AllocationLease}; + +pub(super) struct AdmittedHistoryLoad { + reader: HistoryReader, + id: String, + admission: HistoryAdmissionHandle, +} +/// Decoded data, its actual allocation, and raw-history delivery all retire together. +/// Consumers may inspect the borrowed body or atomically move it to its new owner. +pub struct LoadedHistory { + result: Result, AgentStorageError>, + payload: Option, + permit: Option, +} +impl LoadedHistory { + pub fn value(&self) -> Result<&SavedChat, &AgentStorageError> { + self.result.as_ref().map(Allocated::value) + } + /// The callback inspects/drops the body or adopts its same payload proof in + /// a registered resident owner before returning. No naked body is returned. + pub fn publish( + self, + publish: impl FnOnce( + Result, + Option, + Option, + ), + ) { + match self.result { + Ok(charged) => { + charged.map(|chat| publish(Ok(chat), self.permit, self.payload)); + } + Err(error) => publish(Err(error), self.permit, self.payload), + } + } + fn failed(error: AgentStorageError, permit: Option) -> Self { + Self { + result: Err(error), + payload: None, + permit, + } + } +} +impl AdmittedHistoryLoad { + pub(super) fn new( + reader: HistoryReader, + id: String, + admission: HistoryAdmissionHandle, + ) -> Self { + Self { + reader, + id, + admission, + } + } + pub(super) fn start(self, cx: &mut App) -> Task { + let prepared = detached(cx.background_executor(), async move { + self.reader.prepare_load(self.id).await + }); + cx.spawn(async move |cx| { + let Ok((prepared, permit)) = prepared.await else { + return LoadedHistory::failed(AgentStorageError::Interrupted, None); + }; + let prepared = match prepared { + Ok(value) => value, + Err(error) => return LoadedHistory::failed(error, Some(permit)), + }; + let allocation = + cx.update(|cx| allocate(&self.admission, prepared.resident_bytes(), cx)); + let (allocation, payload) = match allocation { + Ok(value) => value, + Err(error) => return LoadedHistory::failed(error, Some(permit)), + }; + let decoded = detached(cx.background_executor(), async move { + (prepared.decode(allocation).await, payload, permit) + }); + match decoded.await { + Ok((result, payload, permit)) => LoadedHistory { + result, + payload: Some(payload), + permit: Some(permit), + }, + Err(_) => LoadedHistory::failed(AgentStorageError::Interrupted, None), + } + }) + } +} +fn allocate( + admission: &HistoryAdmissionHandle, + bytes: usize, + cx: &mut App, +) -> Result<(AllocationLease, HistoryPayloadPermit), AgentStorageError> { + let payload = admission + .reserve_payload_reclaiming(None, cx) + .map_err(|error| error.into_storage())?; + let proof = admission + .reserve_unowned_bytes(bytes, cx) + .map_err(|error| error.into_storage())?; + Ok(( + AllocationLease::new(bytes, 1, (proof, payload.clone())), + payload, + )) +} +fn detached( + executor: &BackgroundExecutor, + work: impl Future + Send + 'static, +) -> oneshot::Receiver { + let (send, receive) = oneshot::channel(); + executor + .spawn(async move { + let _ = send.send(work.await); + }) + .detach(); + receive +} diff --git a/crates/agent-runtime/src/usage.rs b/crates/agent-runtime/src/usage.rs index 7cda0375..97aec2f0 100644 --- a/crates/agent-runtime/src/usage.rs +++ b/crates/agent-runtime/src/usage.rs @@ -1,60 +1,17 @@ -//! Retained usage projections and cancellable refresh ownership. -use super::Runtime; -use gpui::{Context, Task}; -use nocterm_ai::usage::{AccountUsageSource, Limits, RateLimit, UsageAgent, UsageReadError}; -use std::{ - collections::{HashMap, HashSet}, - sync::Arc, -}; +//! Plan limits retained from accepted ACP usage events. +use nocterm_ai::usage::{Limits, RateLimit}; +use std::collections::HashMap; + +#[derive(Default)] pub(super) struct UsageLimits { limits: HashMap, - reading: HashSet, - tasks: HashMap>, - source: Arc, } + impl UsageLimits { - pub(super) fn new(source: Arc) -> Self { - Self { - source, - limits: HashMap::new(), - reading: HashSet::new(), - tasks: HashMap::new(), - } - } pub(super) fn get(&self, id: &str) -> Option<&Limits> { self.limits.get(id).filter(|limits| !limits.is_empty()) } pub(super) fn merge(&mut self, id: String, limit: RateLimit) { self.limits.entry(id).or_default().merge(limit) } - fn complete(&mut self, id: String, limits: Result>, UsageReadError>) { - self.reading.remove(&id); - self.tasks.remove(&id); - if let Ok(Some(limits)) = limits { - self.limits.entry(id).or_default().replace(limits) - } - } - pub(super) fn refresh(&mut self, agent: UsageAgent, cx: &mut Context) { - let id = agent.id.clone(); - if !self.reading.insert(id.clone()) { - return; - } - let source = self.source.clone(); - let reading = cx - .background_executor() - .spawn(async move { source.read(agent).await }); - let key = id.clone(); - let task = cx.spawn(async move |runtime, cx| { - let limits = reading.await; - let _ = runtime.update(cx, |runtime, cx| { - runtime.usage.complete(id, limits); - cx.notify(); - }); - }); - self.tasks.insert(key, task); - } - pub(super) fn stop(&mut self) { - self.tasks.clear(); - self.reading.clear(); - } } diff --git a/crates/agent-settings-ui/src/controls.rs b/crates/agent-settings-ui/src/controls.rs index 410b9793..4a54a884 100644 --- a/crates/agent-settings-ui/src/controls.rs +++ b/crates/agent-settings-ui/src/controls.rs @@ -1,8 +1,7 @@ use super::AgentSettingsPage; use gpui_kit::{ - AnyElement, App, Context, SharedString, TestSupportExt as _, + AnyElement, App, Context, SharedString, component::{Disableable as _, switch::Switch}, - prelude::*, }; use nocterm_settings::SettingsSection; use nocterm_ui::form; @@ -16,7 +15,7 @@ pub(crate) fn toggle( Switch::new(id) .checked(checked) .disabled(disabled) - .on_click(cx.listener(move |this, checked: &bool, _, cx| { + .on_change(cx.listener(move |this, checked: &bool, _, cx| { let checked = *checked; this.save(move |settings| set(settings, checked), cx); })) @@ -29,16 +28,5 @@ pub(crate) fn text_row( description: impl Into, cx: &App, ) -> AnyElement { - let field = view.field(key); - gpui_kit::div() - .id(SharedString::from(format!("settings-field-{key}"))) - .test_support() - .child(form::stacked_row( - label, - description, - field.input(), - field.error(cx), - cx, - )) - .into_any_element() + form::text_field(view.field(key), key, label, description, cx) } diff --git a/crates/agent-settings-ui/src/lib.rs b/crates/agent-settings-ui/src/lib.rs index 04ea9f0d..b89afc4d 100644 --- a/crates/agent-settings-ui/src/lib.rs +++ b/crates/agent-settings-ui/src/lib.rs @@ -117,16 +117,11 @@ impl AgentSettingsPage { cx: &mut Context, ) { let key = key.into(); - let field = SettingsTextField::new( + let field = SettingsTextField::section( self.settings().clone(), key.clone(), - move |document| read(document.get::()), - move |document, text| { - let mut section = document.get::().clone(); - write(&mut section, text).map_err(nocterm_store::StoreError::edit)?; - document.set(section); - Ok(()) - }, + read, + write, window, cx, ); diff --git a/crates/agent-settings-ui/src/render.rs b/crates/agent-settings-ui/src/render.rs index ab66daef..88f197b8 100644 --- a/crates/agent-settings-ui/src/render.rs +++ b/crates/agent-settings-ui/src/render.rs @@ -29,6 +29,7 @@ pub(super) fn render( ) -> AnyElement { let ai = view.settings().setting::(cx).clone(); let off = !ai.enabled; + let available = view.agents().available(cx); let mut page = v_flex() .w_full() .child(form::page_header( @@ -43,7 +44,7 @@ pub(super) fn render( .when_some(view.ai.error.clone(), |page, error| page.child(form::error_text(error, cx))); let mut agents: Vec = Vec::new(); for id in view.ai.agents.clone() { - agents.push(agent(view, &id, &ai, off, cx)); + agents.push(agent(view, &id, &ai, &available, off, cx)); } agents.push(add_agent(view, off, cx)); page = page.child(form::section("Agents", agents, cx)); @@ -252,12 +253,13 @@ fn agent( view: &AgentSettingsPage, id: &str, ai: &AiSettings, + available: &nocterm_ai::AvailableAgents, off: bool, cx: &mut Context, ) -> AnyElement { let mut block = v_flex() .w_full() - .child(agent_heading(view, id, ai, off, cx)); + .child(agent_heading(view, id, ai, available, off, cx)); if view.ai.expanded.contains(id) { block = block.child(agent_details(view, id, cx)); } @@ -281,6 +283,7 @@ fn agent_heading( view: &AgentSettingsPage, id: &str, ai: &AiSettings, + available: &nocterm_ai::AvailableAgents, off: bool, cx: &mut Context, ) -> AnyElement { @@ -309,7 +312,7 @@ fn agent_heading( .small() .text_color(cx.theme().muted_foreground), ) - .child(agent_icon(view.agents().profile(id, cx)).small()) + .child(agent_icon(view.agents().profile_in(available, id)).small()) .child(div().text_sm().font_medium().child(name)) .child( div() diff --git a/crates/agent-storage/Cargo.toml b/crates/agent-storage/Cargo.toml index c8b59d56..e03a32d0 100644 --- a/crates/agent-storage/Cargo.toml +++ b/crates/agent-storage/Cargo.toml @@ -12,11 +12,13 @@ layer = "adapter" [dependencies] nocterm-ai.workspace = true nocterm-core.workspace = true -serde_json.workspace = true +nocterm-session.workspace = true toml.workspace = true uuid.workspace = true [dev-dependencies] +nocterm-ai = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } futures.workspace = true tempfile.workspace = true diff --git a/crates/agent-storage/src/catalog.rs b/crates/agent-storage/src/catalog.rs index 94cf2df4..4778973a 100644 --- a/crates/agent-storage/src/catalog.rs +++ b/crates/agent-storage/src/catalog.rs @@ -1,90 +1,78 @@ -//! Rebuildable bounded sidecars. The transcript remains the source of truth. -use crate::loading; -use nocterm_ai::history::{ChatSummary, FileStamp, MAX_FILE_BYTES, MAX_RESTORED, SavedChat}; -use std::{ - fs, - io::{BufReader, Read as _}, - path::{Path, PathBuf}, - time::UNIX_EPOCH, +//! Per-scope discovery reads authoritative transcript headers directly. +use crate::{ + input::Input, + namespace::{Partition, Root}, }; -const MAX_METADATA_BYTES: u64 = 16 * 1024; -fn cache_path(dir: &Path, id: &str) -> PathBuf { - dir.join(format!("{id}.meta")) -} -fn stamp(metadata: &fs::Metadata) -> FileStamp { - FileStamp { - bytes: metadata.len(), - modified_nanos: metadata - .modified() - .ok() - .and_then(|time| time.duration_since(UNIX_EPOCH).ok()) - .map_or(0, |time| time.as_nanos()), - } -} -pub(super) fn update_cache(dir: &Path, chat: &SavedChat, queued: bool) { - let Ok(path) = loading::file(dir, &chat.id) else { - return; - }; - let Ok(metadata) = fs::metadata(path) else { - return; +use nocterm_ai::{ + history::{self, ChatSummary, MAX_RESTORED}, + local_environment::ChatScope, + storage::{AgentStorageError, require_local_scope}, +}; +fn summary(partition: &Partition, id: &str) -> Result, AgentStorageError> { + let input = Input::open(partition, id, "json")?; + let stamp = input.stamp(); + let summary = if partition.is_legacy() { + // Arbitrary valid v1 field order and fallback titles cannot be proved + // from a bounded prefix. Keep this compatibility scan; do not rewrite it. + let bytes = input.all()?; + if bytes.starts_with(b"{\"version\":2,") { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + let summary = history::decode_summary(&bytes, id, stamp)?; + if summary + .as_ref() + .is_some_and(|row| row.transcript_version().wire() != 1) + { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + summary + } else { + let header = history::decode_catalog_header(&input.prefix()?, id)?; + require_local_scope(partition.chat_scope(), &header.chat_scope())?; + if header.transcript_version().wire() != partition.version() { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + header.summary(stamp) }; - write_cache( - dir, - &ChatSummary::from_chat(chat).with_stamp(stamp(&metadata), queued), - ); -} -fn write_cache(dir: &Path, summary: &ChatSummary) { - if let Ok(bytes) = serde_json::to_vec(summary) { - let _ = nocterm_core::fs::write_private_atomic(&cache_path(dir, &summary.id), &bytes); + if let Some(summary) = &summary { + require_local_scope(partition.chat_scope(), &summary.chat_scope())?; } + Ok(summary) } -pub(super) fn delete_cache(dir: &Path, id: &str) { - let _ = fs::remove_file(cache_path(dir, id)); -} -pub(super) fn load_catalog(dir: &Path) -> Vec { - let Ok(entries) = fs::read_dir(dir) else { - return Vec::new(); +pub(super) fn load(root: &Root, scope: &ChatScope) -> Result, AgentStorageError> { + let Some(directory) = root.open(false)? else { + return Ok(Vec::new()); }; + let partitions = crate::namespace::readable(&directory, scope)?; let mut rows = Vec::new(); - for entry in entries.filter_map(Result::ok) { - let path = entry.path(); - let Some(id) = path.file_stem().and_then(|stem| stem.to_str()) else { + for (index, partition) in partitions.iter().enumerate() { + scan(partition, &partitions[..index], &mut rows)?; + } + Ok(rows) +} +fn scan( + partition: &Partition, + preferred: &[Partition], + rows: &mut Vec, +) -> Result<(), AgentStorageError> { + let mut entries = partition.directory().entries()?; + while let Some(name) = entries.next()? { + let Some(id) = Partition::id(&name) else { continue; }; - if path.extension().is_none_or(|ext| ext != "json") || loading::file(dir, id).is_err() { + let mut shadowed = false; + for preferred in preferred { + shadowed |= preferred.exists(id)?; + } + if shadowed { continue; } - let Ok(metadata) = entry.metadata() else { + let Ok(Some(row)) = summary(partition, id) else { continue; }; - if !metadata.is_file() || metadata.len() > MAX_FILE_BYTES { - continue; - } - let file_stamp = stamp(&metadata); - let cached = fs::File::open(cache_path(dir, id)) - .ok() - .and_then(|file| { - serde_json::from_reader::<_, ChatSummary>(BufReader::new( - file.take(MAX_METADATA_BYTES), - )) - .ok() - }) - .filter(|row| row.matches_stamp(id, file_stamp)); - let summary = cached.or_else(|| { - let bytes = loading::read(&path, id).ok()?; - nocterm_ai::history::decode_summary(&bytes, id, file_stamp) - .ok()? - .inspect(|row| write_cache(dir, row)) - }); - if let Some(row) = summary.filter(|row| row.has_content || row.name.is_some() || row.pinned) - { - rows.push(row); - rows.sort_by_key(|row| std::cmp::Reverse((row.pinned, row.updated))); - rows.truncate(MAX_RESTORED); - } + rows.push(row); + rows.sort_by_key(|row| std::cmp::Reverse((row.pinned, row.updated))); + rows.truncate(MAX_RESTORED); } - rows + Ok(()) } -#[cfg(test)] -#[path = "catalog_tests.rs"] -mod tests; diff --git a/crates/agent-storage/src/catalog_tests.rs b/crates/agent-storage/src/catalog_tests.rs deleted file mode 100644 index bce60363..00000000 --- a/crates/agent-storage/src/catalog_tests.rs +++ /dev/null @@ -1,121 +0,0 @@ -use super::*; -use crate::chats::load; -use nocterm_ai::model::content::Content; -use nocterm_ai::model::tools::ToolRecord; -fn file(dir: &Path, id: &str) -> Option { - crate::loading::file(dir, id).ok() -} -use nocterm_ai::{ - history::{SavedAttachment, SavedPrompt}, - thread::Entry, -}; -fn chat() -> SavedChat { - let mut chat = SavedChat::for_identity("codex".into(), uuid::Uuid::new_v4().to_string(), 1); - chat.entries.push(Entry::User(vec![Content::Text( - nocterm_ai::model::content::Text::new("first message"), - )])); - chat -} -#[test] -fn catalog_is_small_and_payloads_load_only_when_requested() { - let dir = tempfile::tempdir().unwrap(); - let mut chat = chat(); - chat.entries.push(Entry::Agent("x".repeat(1024 * 1024))); - crate::chats::save(dir.path(), &chat).unwrap(); - let rows = load_catalog(dir.path()); - assert_eq!(rows.len(), 1); - assert!(serde_json::to_string(&rows[0]).unwrap().len() < 1024); - assert_eq!(rows[0].title(), "first message"); - assert_eq!(load(dir.path(), &chat.id).unwrap().entries.len(), 2); -} -#[test] -fn stale_cache_rebuilds_and_corrupt_files_survive() { - let dir = tempfile::tempdir().unwrap(); - let mut chat = chat(); - crate::chats::save(dir.path(), &chat).unwrap(); - chat.name = Some("Changed outside nocterm".into()); - fs::write( - file(dir.path(), &chat.id).unwrap(), - serde_json::to_string(&chat).unwrap(), - ) - .unwrap(); - let broken = dir.path().join(format!("{}.json", uuid::Uuid::new_v4())); - fs::write(&broken, "{").unwrap(); - assert_eq!(load_catalog(dir.path())[0].name, chat.name); - assert_eq!(fs::read_to_string(broken).unwrap(), "{"); -} -#[test] -fn legacy_scan_discards_payloads_and_keeps_pinned_newest() { - let dir = tempfile::tempdir().unwrap(); - let mut oldest = String::new(); - for index in 0..MAX_RESTORED + 5 { - let mut chat = chat(); - chat.updated = index as u64; - chat.pinned = index == 0; - if chat.pinned { - oldest = chat.id.clone(); - } - fs::write( - file(dir.path(), &chat.id).unwrap(), - serde_json::to_string(&chat).unwrap(), - ) - .unwrap(); - } - let rows = load_catalog(dir.path()); - assert_eq!(rows.len(), MAX_RESTORED); - assert_eq!(rows[0].id, oldest); - assert!(rows[1].updated > rows[2].updated); -} -#[test] -fn queued_images_drafts_and_metadata_survive_lazy_loading() { - let dir = tempfile::tempdir().unwrap(); - let mut chat = chat(); - chat.entries.clear(); - chat.draft = Some("literal draft".into()); - chat.name = Some("Named".into()); - chat.pinned = true; - let prompt = SavedPrompt { - id: 7, - text: "pending".into(), - images: vec![Content::Image(nocterm_ai::model::content::Image::new( - "A".repeat(600_000), - "image/png", - ))], - attachments: vec![SavedAttachment::Connection("host".into())], - }; - let id = chat.id.clone(); - crate::chats::save_shared( - dir.path(), - &nocterm_ai::history::SharedChat { - metadata: chat, - queue: vec![std::sync::Arc::new(prompt)], - }, - ) - .unwrap(); - assert!(load_catalog(dir.path())[0].has_content); - let loaded = load(dir.path(), &id).unwrap(); - assert_eq!(loaded.draft.as_deref(), Some("literal draft")); - assert_eq!(loaded.queue[0].id, 7); - assert_eq!(loaded.queue[0].attachments.len(), 1); - assert!( - matches!(&loaded.queue[0].images[0], Content::Image(image) if image.data.len() == 600_000) - ); -} -#[test] -fn malformed_collection_and_nested_amplification_are_rejected_without_erasing_files() { - let dir = tempfile::tempdir().unwrap(); - let mut chat = chat(); - chat.entries = (0..2001).map(|_| Entry::Agent(String::new())).collect(); - let path = file(dir.path(), &chat.id).unwrap(); - fs::write(&path, serde_json::to_string(&chat).unwrap()).unwrap(); - assert!(load(dir.path(), &chat.id).is_err()); - chat.entries = vec![Entry::Tool( - ToolRecord::new("tool", "amplification") - .raw_input(serde_json::json!({"tiny":vec![0; 140_000]})), - )]; - fs::write(&path, serde_json::to_string(&chat).unwrap()).unwrap(); - assert!(load(dir.path(), &chat.id).is_err()); - assert!(crate::chats::save(dir.path(), &chat).is_err()); - assert!(chat.validate_size().is_err()); - assert!(path.exists()); -} diff --git a/crates/agent-storage/src/chats.rs b/crates/agent-storage/src/chats.rs index 457095b7..e18ebed1 100644 --- a/crates/agent-storage/src/chats.rs +++ b/crates/agent-storage/src/chats.rs @@ -1,117 +1,178 @@ -//! Immutable chat publication; the service supplies serialization order. -use crate::{catalog, loading}; -use nocterm_ai::{ - history::{self, ChatSummary, SavedChat, SharedChat}, - storage::{AgentStorageError, ChatRepository, StorageFuture}, +//! Immutable scoped documents; the service supplies serialization and write ordering. +use crate::{ + catalog, + input::Input, + namespace::{Partition, Root}, + publication, }; -use std::{ - path::{Path, PathBuf}, - sync::Arc, +use nocterm_ai::{ + history::{self, CatalogHeader, ChatSummary, ContextId, SavedChat, SharedChat}, + local_environment::ChatScope, + storage::{ + AgentStorageError, ChatRepository, PreparedChatBytes, PreparedChatLoad, StorageFuture, + require_local_scope, + }, }; +use nocterm_session::local_fs::{LocalDirectory, LocalFs}; +use std::{path::PathBuf, sync::Arc}; + #[derive(Clone)] pub struct FileChatRepository { - dir: PathBuf, + root: Root, } impl FileChatRepository { - pub fn new(dir: PathBuf) -> Self { - Self { dir } + /// The configured root has trusted application-owned ancestry. Every generated + /// descendant and operation uses the supplied strict held-directory capability. + pub fn new(dir: PathBuf, filesystem: Arc) -> Self { + Self { + root: Root::new(dir, filesystem), + } } } impl ChatRepository for FileChatRepository { - fn catalog(&self) -> StorageFuture> { - let dir = self.dir.clone(); - Box::pin(async move { Ok(catalog::load_catalog(&dir)) }) + fn prepare_load_in( + &self, + scope: ChatScope, + id: String, + ) -> StorageFuture> { + let root = self.root.clone(); + Box::pin(async move { + let (bytes, version) = read(&root, &scope, &id)?; + Ok( + Box::new(PreparedChatBytes::new(bytes, scope, id, Some(version))?) + as Box, + ) + }) + } + fn catalog(&self, scope: ContextId) -> StorageFuture> { + self.catalog_in(scope.into()) + } + fn load(&self, scope: ContextId, id: String) -> StorageFuture { + self.load_in(scope.into(), id) + } + fn delete(&self, scope: ContextId, id: String) -> StorageFuture<()> { + self.delete_in(scope.into(), id) } - fn load(&self, id: String) -> StorageFuture { - let dir = self.dir.clone(); - Box::pin(async move { load(&dir, &id) }) + fn catalog_in(&self, scope: ChatScope) -> StorageFuture> { + let root = self.root.clone(); + Box::pin(async move { catalog::load(&root, &scope) }) + } + fn load_in(&self, scope: ChatScope, id: String) -> StorageFuture { + let root = self.root.clone(); + Box::pin(async move { load(&root, &scope, &id) }) } fn save(&self, chat: Arc) -> StorageFuture<()> { - let dir = self.dir.clone(); - Box::pin(async move { save_shared(&dir, &chat) }) + let root = self.root.clone(); + Box::pin(async move { save(&root, &chat) }) } - fn delete(&self, id: String) -> StorageFuture<()> { - let dir = self.dir.clone(); - Box::pin(async move { delete(&dir, &id) }) + fn delete_in(&self, scope: ChatScope, id: String) -> StorageFuture<()> { + let root = self.root.clone(); + Box::pin(async move { delete(&root, &scope, &id) }) } } -pub(super) fn load(dir: &Path, id: &str) -> Result { - let path = loading::file(dir, id)?; - Ok(history::decode(&loading::read(&path, id)?, id)?) + +fn requested( + root: &Arc, + scope: &ChatScope, + id: &str, +) -> Result, AgentStorageError> { + for partition in crate::namespace::readable(root, scope)? { + if partition.exists(id)? { + return Ok(Some(partition)); + } + } + Ok(None) } -pub(super) fn save(dir: &Path, chat: &SavedChat) -> Result<(), AgentStorageError> { - publish(dir, chat, history::encode(chat)?, !chat.queue.is_empty()) +fn load(root: &Root, scope: &ChatScope, id: &str) -> Result { + let (bytes, version) = read(root, scope, id)?; + let chat = history::decode(&bytes, id)?; + require_local_scope(scope, &chat.header.chat_scope())?; + if version != chat.version { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + Ok(chat) } -pub(super) fn save_shared(dir: &Path, chat: &SharedChat) -> Result<(), AgentStorageError> { +fn read(root: &Root, scope: &ChatScope, id: &str) -> Result<(Vec, u32), AgentStorageError> { + crate::namespace::validate_id(id)?; + let directory = root + .open(false)? + .ok_or_else(|| AgentStorageError::NotFound(id.into()))?; + let partition = + requested(&directory, scope, id)?.ok_or_else(|| AgentStorageError::NotFound(id.into()))?; + let input = Input::open(&partition, id, "json")?; + let bytes = if partition.is_legacy() { + input.all()? + } else { + input.scoped_record(id, scope)? + }; + Ok((bytes, partition.version())) +} +fn check_version(partition: &Partition, version: u32) -> Result<(), AgentStorageError> { + if partition.version() != version { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + Ok(()) +} +pub(super) fn header(partition: &Partition, id: &str) -> Result { + let input = Input::open(partition, id, "json")?; + let header = history::decode_catalog_header(&input.prefix()?, id)?; + require_local_scope(partition.chat_scope(), &header.chat_scope())?; + check_version(partition, header.transcript_version().wire())?; + Ok(header) +} +fn existing(partition: &Partition, id: &str) -> Result<(), AgentStorageError> { + if !partition.exists(id)? { + return Ok(()); + } + if partition.is_legacy() { + let chat = history::decode(&Input::open(partition, id, "json")?.all()?, id)?; + require_local_scope(partition.chat_scope(), &chat.header.chat_scope())?; + check_version(partition, chat.version) + } else { + header(partition, id).map(|_| ()) + } +} +fn save(root: &Root, chat: &SharedChat) -> Result<(), AgentStorageError> { if let Some((draft, updated)) = chat.metadata.draft_patch() { - let mut original = load(dir, &chat.id)?; + let mut original = load(root, &chat.header.chat_scope(), &chat.header.id)?; original.draft = draft; - original.updated = updated; - return save(dir, &original); - } - publish( - dir, - &chat.metadata, - history::encode_shared(chat)?, - !chat.queue.is_empty(), - ) + original.header.updated = updated; + return save_record(root, &original, history::encode(&original)?); + } + save_record(root, &chat.metadata, history::encode_shared(chat)?) } -fn publish( - dir: &Path, - chat: &SavedChat, - bytes: Vec, - queued: bool, -) -> Result<(), AgentStorageError> { - let path = loading::file(dir, &chat.id)?; - nocterm_core::paths::ensure_private_dir(dir).map_err(|source| { - AgentStorageError::Directory { - path: dir.to_owned(), - source: Arc::new(source), - } - })?; - nocterm_core::fs::write_private_atomic(&path, &bytes).map_err(|source| { - AgentStorageError::Publish { - path, - source: Arc::new(source), +fn save_record(root: &Root, chat: &SavedChat, bytes: Vec) -> Result<(), AgentStorageError> { + let directory = root + .open(true)? + .ok_or_else(|| AgentStorageError::NotFound(chat.header.id.clone()))?; + let scope = chat.header.chat_scope(); + let partition = match chat.version { + 1 => { + require_local_scope(&ContextId::General.into(), &scope)?; + Some(Partition::legacy(directory.clone())) } - })?; - catalog::update_cache(dir, chat, queued); + 2 => Partition::scoped(&directory, scope.context(), true)?, + 3 => Partition::environment(&directory, &scope, true)?, + other => return Err(history::HistoryCodecError::UnsupportedVersion(other).into()), + }; + let partition = partition.ok_or_else(|| AgentStorageError::NotFound(chat.header.id.clone()))?; + existing(&partition, &chat.header.id)?; + publication::publish(&partition, &chat.header.id, "json", &bytes)?; Ok(()) } -pub(super) fn delete(dir: &Path, id: &str) -> Result<(), AgentStorageError> { - let path = loading::file(dir, id)?; - match std::fs::remove_file(&path) { - Err(source) if source.kind() != std::io::ErrorKind::NotFound => { - Err(AgentStorageError::Delete { - path, - source: Arc::new(source), - }) - } - _ => { - catalog::delete_cache(dir, id); - Ok(()) - } +fn delete(root: &Root, scope: &ChatScope, id: &str) -> Result<(), AgentStorageError> { + crate::namespace::validate_id(id)?; + let Some(directory) = root.open(false)? else { + return Ok(()); + }; + let partitions = crate::namespace::readable(&directory, scope)?; + for partition in &partitions { + existing(partition, id)?; } + // Oldest first: interruption cannot expose a previously shadowed fallback. + for partition in partitions.iter().rev() { + partition.remove(id, "json")?; + let _ = partition.remove(id, "meta"); + } + Ok(()) } -#[cfg(test)] -fn load_all(dir: &Path) -> Vec { - let mut bytes = 0; - catalog::load_catalog(dir) - .into_iter() - .filter_map(|row| { - if bytes + row.bytes > 64 * 1024 * 1024 { - return None; - } - let chat = load(dir, &row.id).ok()?; - let resident = chat.resident_bytes() as u64; - if bytes + resident > 64 * 1024 * 1024 { - return None; - } - bytes += resident; - Some(chat) - }) - .collect() -} -#[cfg(test)] -#[path = "history_tests.rs"] -mod tests; diff --git a/crates/agent-storage/src/history_tests.rs b/crates/agent-storage/src/history_tests.rs deleted file mode 100644 index 343c0b58..00000000 --- a/crates/agent-storage/src/history_tests.rs +++ /dev/null @@ -1,253 +0,0 @@ -use super::*; -use nocterm_ai::model::content::Content; -use nocterm_ai::{ - history::{MAX_FILE_BYTES, SavedAttachment, SavedPrompt}, - thread::Entry, -}; -const MAX_SAVED_IMAGE_BYTES: usize = 512 * 1024; -const MAX_SAVED_ENTRIES: usize = 2000; - -fn chat() -> SavedChat { - let mut chat = SavedChat::for_identity("codex".into(), uuid::Uuid::new_v4().to_string(), 1); - chat.title = Some("Disk usage".into()); - chat.session_id = Some("session-1".into()); - chat.entries = vec![ - Entry::User(vec![Content::text("df -h?")]), - Entry::Thought("checking".into()), - Entry::Agent("Use `df -h`.".into()), - ]; - chat -} - -#[test] -fn a_chat_keeps_its_configuration_on_disk() { - let dir = tempfile::tempdir().unwrap(); - let mut saved = chat(); - saved.config.insert( - "model".into(), - nocterm_ai::session_config::ConfigValue::Value("opus".into()), - ); - saved.mode = Some("plan".into()); - save(dir.path(), &saved).unwrap(); - let loaded = load(dir.path(), &saved.id).unwrap(); - assert_eq!(loaded.config, saved.config); - assert_eq!(loaded.mode.as_deref(), Some("plan")); -} - -#[test] -fn chats_round_trip_newest_first() { - let dir = tempfile::tempdir().unwrap(); - let mut older = chat(); - older.updated = 10; - let mut newer = chat(); - newer.updated = 20; - save(dir.path(), &older).unwrap(); - save(dir.path(), &newer).unwrap(); - - let loaded = load_all(dir.path()); - - assert_eq!( - loaded - .iter() - .map(|chat| chat.id.as_str()) - .collect::>(), - [newer.id.as_str(), older.id.as_str()] - ); - assert_eq!(loaded[0].entries.len(), 3); - assert!(matches!(&loaded[0].entries[2], Entry::Agent(text) if text == "Use `df -h`.")); - assert_eq!(loaded[0].session_id.as_deref(), Some("session-1")); -} - -#[test] -fn pinned_chats_come_first_with_their_names() { - let dir = tempfile::tempdir().unwrap(); - let mut pinned = chat(); - pinned.updated = 1; - pinned.pinned = true; - pinned.name = Some("Disks".into()); - let mut newer = chat(); - newer.updated = 2; - save(dir.path(), &pinned).unwrap(); - save(dir.path(), &newer).unwrap(); - - let loaded = load_all(dir.path()); - - assert_eq!(loaded[0].id, pinned.id); - assert!(loaded[0].pinned); - assert_eq!(loaded[0].name.as_deref(), Some("Disks")); - assert!(!loaded[1].pinned); -} - -#[cfg(unix)] -#[test] -fn chats_are_private_to_the_user() { - use std::os::unix::fs::PermissionsExt as _; - let root = tempfile::tempdir().unwrap(); - let dir = root.path().join("chats"); - let chat = chat(); - save(&dir, &chat).unwrap(); - let mode = |path: &Path| std::fs::metadata(path).unwrap().permissions().mode() & 0o777; - assert_eq!(mode(&dir), 0o700); - assert_eq!(mode(&dir.join(format!("{}.json", chat.id))), 0o600); -} - -#[test] -fn deleting_removes_the_file_and_ids_cannot_walk_paths() { - let dir = tempfile::tempdir().unwrap(); - let chat = chat(); - save(dir.path(), &chat).unwrap(); - delete(dir.path(), &chat.id).unwrap(); - delete(dir.path(), &chat.id).unwrap(); - assert!(load_all(dir.path()).is_empty()); - - let mut evil = chat.clone(); - evil.id = "../escape".into(); - assert!(save(dir.path(), &evil).is_err()); - assert!(delete(dir.path(), "../escape").is_err()); -} - -#[test] -fn large_images_and_old_entries_are_left_out() { - let dir = tempfile::tempdir().unwrap(); - let mut chat = chat(); - let image = Content::Image(nocterm_ai::model::content::Image::new( - "A".repeat(MAX_SAVED_IMAGE_BYTES + 1), - "image/png", - )); - chat.entries = (0..MAX_SAVED_ENTRIES + 5) - .map(|ix| Entry::Agent(ix.to_string())) - .collect(); - chat.entries.push(Entry::User(vec![image])); - save(dir.path(), &chat).unwrap(); - - let loaded = &load_all(dir.path())[0]; - - assert_eq!(loaded.entries.len(), MAX_SAVED_ENTRIES); - assert!(matches!( - loaded.entries.last(), - Some(Entry::User(blocks)) if matches!(&blocks[0], Content::Text(text) if text.text.contains("not kept")) - )); -} - -#[test] -fn foreign_and_broken_files_are_skipped() { - let dir = tempfile::tempdir().unwrap(); - std::fs::write(dir.path().join("broken.json"), "{").unwrap(); - let chat = chat(); - let text = serde_json::to_string(&chat).unwrap(); - // A valid chat under someone else's name. - std::fs::write( - dir.path().join(format!("{}.json", uuid::Uuid::new_v4())), - text, - ) - .unwrap(); - assert!(load_all(dir.path()).is_empty()); -} -#[test] -fn pending_images_and_attachment_snapshots_round_trip_without_truncation() { - let dir = tempfile::tempdir().unwrap(); - let mut chat = chat(); - let data = "A".repeat(MAX_SAVED_IMAGE_BYTES + 1); - chat.attachments = vec![ - SavedAttachment::Connection("server".into()), - SavedAttachment::Group("production".into()), - ]; - chat.queue = vec![SavedPrompt { - id: 42, - text: "queued image".into(), - images: vec![Content::Image(nocterm_ai::model::content::Image::new( - data.clone(), - "image/png", - ))], - attachments: chat.attachments.clone(), - }]; - save(dir.path(), &chat).unwrap(); - let saved = load_all(dir.path()).remove(0); - assert_eq!(saved.queue[0].text, "queued image"); - assert_eq!(saved.attachments, chat.attachments); - assert_eq!(saved.queue[0].attachments, chat.attachments); - assert!(matches!(&saved.queue[0].images[0], Content::Image(image) if image.data == data)); -} - -#[test] -fn oversized_pending_messages_are_rejected_and_old_chats_still_load() { - let mut chat = chat(); - let mut old = serde_json::to_value(&chat).unwrap(); - old.as_object_mut().unwrap().remove("queue"); - old.as_object_mut().unwrap().remove("attachments"); - let old: SavedChat = serde_json::from_value(old).unwrap(); - assert!(old.queue.is_empty()); - assert!(old.attachments.is_empty()); - chat.queue = vec![SavedPrompt { - id: 1, - text: "x".repeat(MAX_FILE_BYTES as usize), - images: Vec::new(), - attachments: Vec::new(), - }]; - assert!(chat.validate_size().is_err()); -} - -#[test] -fn archived_draft_publication_merges_only_draft_and_updated_without_hydration() { - let directory = tempfile::tempdir().unwrap(); - let mut original = chat(); - original.name = Some("keep the name".into()); - original.queue.push(SavedPrompt { - id: 9, - text: "keep queued text".into(), - images: Vec::new(), - attachments: Vec::new(), - }); - save(directory.path(), &original).unwrap(); - let draft = "literal quote\"\n界"; - let patch = history::SharedChat { - metadata: SavedChat::archive_draft( - original.id.clone(), - "other-agent".into(), - draft.into(), - original.updated + 1, - ), - queue: Vec::new(), - }; - save_shared(directory.path(), &patch).unwrap(); - let loaded = load(directory.path(), &original.id).unwrap(); - assert_eq!( - serde_json::to_value(&loaded.entries).unwrap(), - serde_json::to_value(&original.entries).unwrap() - ); - assert_eq!( - serde_json::to_value(&loaded.queue).unwrap(), - serde_json::to_value(&original.queue).unwrap() - ); - assert_eq!(loaded.name, original.name); - assert_eq!(loaded.agent_id, original.agent_id); - assert_eq!(loaded.draft.as_deref(), Some(draft)); - assert_eq!(loaded.updated, original.updated + 1); -} -#[test] -fn best_effort_cache_failure_does_not_revoke_a_published_transcript() { - let directory = tempfile::tempdir().unwrap(); - let chat = chat(); - std::fs::create_dir(directory.path().join(format!("{}.meta", chat.id))).unwrap(); - save(directory.path(), &chat).unwrap(); - assert_eq!( - serde_json::to_value(load(directory.path(), &chat.id).unwrap().entries).unwrap(), - serde_json::to_value(chat.entries).unwrap() - ); -} - -#[test] -fn a_version_one_file_loads_and_saves_back_byte_for_byte() { - const ID: &str = "00000000-0000-4000-8000-0000000000a1"; - const CANONICAL: &[u8] = include_bytes!("../../ai/src/history/fixtures/v1/canonical.json"); - let directory = tempfile::tempdir().unwrap(); - let path = directory.path().join(format!("{ID}.json")); - std::fs::write(&path, CANONICAL).unwrap(); - - let loaded = load(directory.path(), ID).unwrap(); - assert_eq!(loaded.entries.len(), 6); - assert_eq!(loaded.queue.len(), 1); - save(directory.path(), &loaded).unwrap(); - - assert_eq!(std::fs::read(&path).unwrap(), CANONICAL); -} diff --git a/crates/agent-storage/src/input.rs b/crates/agent-storage/src/input.rs new file mode 100644 index 00000000..d16a2acb --- /dev/null +++ b/crates/agent-storage/src/input.rs @@ -0,0 +1,87 @@ +//! Bounded reads retain the original opened source and its supplied file facts. +use crate::namespace::Partition; +use nocterm_ai::{ + history::{self, FileStamp, HistoryCodecError, MAX_FILE_BYTES}, + local_environment::ChatScope, + storage::{AgentStorageError, require_local_scope}, +}; +use nocterm_session::local_fs::{LocalFsError, LocalSource}; + +pub(super) struct Input { + source: Box, + stamp: FileStamp, +} +impl Input { + pub(super) fn open( + partition: &Partition, + id: &str, + extension: &str, + ) -> Result { + let source = partition + .directory() + .open_source(&partition.name(id, extension)?) + .map_err(|error| match error { + LocalFsError::NotFound(_) => AgentStorageError::NotFound(id.into()), + error => error.into(), + })?; + let metadata = source.metadata()?; + let bytes = metadata.size.ok_or(HistoryCodecError::EncodedLimit)?; + if bytes > MAX_FILE_BYTES { + return Err(HistoryCodecError::EncodedLimit.into()); + } + Ok(Self { + source, + stamp: FileStamp { + bytes, + modified_nanos: u128::from(metadata.modified.unwrap_or(0)) * 1_000_000_000, + }, + }) + } + pub(super) fn stamp(&self) -> FileStamp { + self.stamp + } + fn read(&mut self, limit: usize) -> Result, AgentStorageError> { + let mut bytes = Vec::new(); + self.fill(&mut bytes, limit)?; + Ok(bytes) + } + fn fill(&mut self, bytes: &mut Vec, limit: usize) -> Result<(), AgentStorageError> { + while bytes.len() < limit { + let chunk = self.source.read((limit - bytes.len()).min(32 * 1024))?; + if chunk.is_empty() { + break; + } + bytes.extend(chunk); + } + Ok(()) + } + /// Admit a scoped header before continuing this same held source. A refused + /// prefix never causes a full foreign payload read or a second pathname open. + pub(super) fn scoped_record( + mut self, + id: &str, + scope: &ChatScope, + ) -> Result, AgentStorageError> { + let mut bytes = self.read(history::MAX_HEADER_BYTES)?; + let header = history::decode_catalog_header(&bytes, id)?; + require_local_scope(scope, &header.chat_scope())?; + self.fill(&mut bytes, MAX_FILE_BYTES as usize + 1)?; + self.finish(bytes) + } + pub(super) fn prefix(mut self) -> Result, AgentStorageError> { + let bytes = self.read(nocterm_ai::history::MAX_HEADER_BYTES)?; + self.source.close()?; + Ok(bytes) + } + pub(super) fn all(mut self) -> Result, AgentStorageError> { + let bytes = self.read(MAX_FILE_BYTES as usize + 1)?; + self.finish(bytes) + } + fn finish(self, bytes: Vec) -> Result, AgentStorageError> { + self.source.close()?; + if bytes.len() as u64 > MAX_FILE_BYTES { + return Err(HistoryCodecError::EncodedLimit.into()); + } + Ok(bytes) + } +} diff --git a/crates/agent-storage/src/lib.rs b/crates/agent-storage/src/lib.rs index 42a4dce2..ab6628d5 100644 --- a/crates/agent-storage/src/lib.rs +++ b/crates/agent-storage/src/lib.rs @@ -1,15 +1,22 @@ //! Bounded filesystem repositories for agent history and preferences. -//! Owns: private files, transcript/catalog discovery, sidecar stamps, draft merges +//! Owns: private files, transcript/catalog discovery, draft merges //! and native chat identity/time facts. //! Does not own: conversation rules, views, scheduling or shutdown deadlines. -//! Ports used/provided: AI AgentStateRepository, ChatRepository and -//! ChatMetadataSource. +//! Ports used/provided: injected session LocalFs/LocalDirectory; AI +//! AgentStateRepository, ChatRepository and +//! ChatMetadataSource and bounded PreparedChatLoad using core allocation leases. //! Settings section: none. Globals: none. -//! Invariants: IDs are checked before paths; transcripts are authoritative; cache errors never revoke publication. +//! Invariants: (environment, context, ID) selects one held v3 namespace. Bounded +//! authoritative headers precede full reads; obsolete sidecars are ignored. Old v1/v2 history is +//! ThisComputer only and retains its bytes and paths. The configured private root +//! has trusted ancestry; generated descendants use held operations. Portable +//! identities support only old General history. mod catalog; mod chats; -mod loading; +mod input; mod metadata; +mod namespace; +mod publication; pub use metadata::NativeChatMetadata; mod state; pub use chats::FileChatRepository; diff --git a/crates/agent-storage/src/loading.rs b/crates/agent-storage/src/loading.rs deleted file mode 100644 index 1382ad62..00000000 --- a/crates/agent-storage/src/loading.rs +++ /dev/null @@ -1,49 +0,0 @@ -//! Bounded reads and checked chat paths. -use nocterm_ai::{ - history::{HistoryCodecError, MAX_FILE_BYTES}, - storage::AgentStorageError, -}; -use std::{ - fs::File, - io::Read as _, - path::{Path, PathBuf}, - sync::Arc, -}; -pub(super) fn file(dir: &Path, id: &str) -> Result { - if uuid::Uuid::parse_str(id).is_err() { - return Err(AgentStorageError::InvalidId(id.into())); - } - Ok(dir.join(format!("{id}.json"))) -} -pub(super) fn read(path: &Path, id: &str) -> Result, AgentStorageError> { - let opened = File::open(path).map_err(|error| { - if error.kind() == std::io::ErrorKind::NotFound { - AgentStorageError::NotFound(id.into()) - } else { - read_error(path, error) - } - })?; - if opened - .metadata() - .map_err(|error| read_error(path, error))? - .len() - > MAX_FILE_BYTES - { - return Err(HistoryCodecError::EncodedLimit.into()); - } - let mut bytes = Vec::new(); - opened - .take(MAX_FILE_BYTES + 1) - .read_to_end(&mut bytes) - .map_err(|error| read_error(path, error))?; - if bytes.len() as u64 > MAX_FILE_BYTES { - return Err(HistoryCodecError::EncodedLimit.into()); - } - Ok(bytes) -} -fn read_error(path: &Path, source: std::io::Error) -> AgentStorageError { - AgentStorageError::Read { - path: path.to_owned(), - source: Arc::new(source), - } -} diff --git a/crates/agent-storage/src/namespace.rs b/crates/agent-storage/src/namespace.rs new file mode 100644 index 00000000..33977dd2 --- /dev/null +++ b/crates/agent-storage/src/namespace.rs @@ -0,0 +1,205 @@ +//! Context paths are selected once, then generated descendants use held operations. +use nocterm_ai::{history::ContextId, local_environment::ChatScope, storage::AgentStorageError}; +use nocterm_session::local_fs::{LocalDirectory, LocalFs, LocalFsError}; +use std::{ + ffi::{OsStr, OsString}, + path::PathBuf, + sync::Arc, +}; +pub(super) fn validate_id(id: &str) -> Result<(), AgentStorageError> { + if uuid::Uuid::parse_str(id).is_err() { + return Err(AgentStorageError::InvalidId(id.into())); + } + Ok(()) +} + +#[derive(Clone)] +pub(super) struct Root { + path: PathBuf, + filesystem: Arc, +} +impl Root { + pub(super) fn new(path: PathBuf, filesystem: Arc) -> Self { + Self { path, filesystem } + } + pub(super) fn open( + &self, + create: bool, + ) -> Result>, AgentStorageError> { + if !create { + match std::fs::symlink_metadata(&self.path) { + Ok(_) => {} + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(source) => { + return Err(AgentStorageError::Read { + path: self.path.clone(), + source: Arc::new(source), + }); + } + } + } + nocterm_core::paths::ensure_private_dir(&self.path).map_err(|source| { + AgentStorageError::Directory { + path: self.path.clone(), + source: Arc::new(source), + } + })?; + // Only the configured private root's ancestry is trusted. Resolving it + // preserves supported aliases there; no generated descendant uses a path. + let root = match std::fs::canonicalize(&self.path) { + Ok(root) => root, + Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), + Err(source) => { + return Err(AgentStorageError::Read { + path: self.path.clone(), + source: Arc::new(source), + }); + } + }; + Ok(Some(self.filesystem.open_directory(&root)?)) + } +} + +#[derive(Clone)] +pub(super) struct Partition { + directory: Arc, + scope: ChatScope, + version: u32, +} +impl Partition { + pub(super) fn legacy(directory: Arc) -> Self { + Self { + directory, + scope: ContextId::General.into(), + version: 1, + } + } + pub(super) fn scoped( + root: &Arc, + scope: ContextId, + create: bool, + ) -> Result, AgentStorageError> { + if scope != ContextId::General && !root.identity().is_held() { + return Err(AgentStorageError::ScopeUnsupported(scope)); + } + let names = context_names(scope); + let names = std::iter::once(OsString::from("scoped-v2")) + .chain(names) + .collect(); + open_partition( + root, + scope.into(), + create, + names, + 2, + scope != ContextId::General, + ) + } + pub(super) fn environment( + root: &Arc, + scope: &ChatScope, + create: bool, + ) -> Result, AgentStorageError> { + if !root.identity().is_held() && !create && scope.environment().is_this_computer() { + return Ok(None); + } + let mut names = vec![ + OsString::from("scoped-v3"), + scope.environment().storage_key().into(), + ]; + names.extend(context_names(scope.context())); + open_partition(root, scope.clone(), create, names, 3, true) + } + pub(super) fn chat_scope(&self) -> &ChatScope { + &self.scope + } + pub(super) fn version(&self) -> u32 { + self.version + } + pub(super) fn is_legacy(&self) -> bool { + self.version == 1 + } + pub(super) fn directory(&self) -> &Arc { + &self.directory + } + pub(super) fn name(&self, id: &str, extension: &str) -> Result { + validate_id(id)?; + Ok(format!("{id}.{extension}").into()) + } + pub(super) fn exists(&self, id: &str) -> Result { + Ok(self.directory.metadata(&self.name(id, "json")?)?.is_some()) + } + pub(super) fn remove(&self, id: &str, extension: &str) -> Result<(), AgentStorageError> { + match self.directory.remove_file(&self.name(id, extension)?) { + Ok(()) | Err(LocalFsError::NotFound(_)) => Ok(()), + Err(error) => Err(error.into()), + } + } + pub(super) fn id(name: &OsStr) -> Option<&str> { + let name = name.to_str()?.strip_suffix(".json")?; + uuid::Uuid::parse_str(name).ok()?; + Some(name) + } +} + +fn context_names(scope: ContextId) -> Vec { + match scope { + ContextId::General => vec!["general".into()], + ContextId::Project(id) => vec!["projects".into(), id.to_string().into()], + } +} +fn open_partition( + root: &Arc, + scope: ChatScope, + create: bool, + names: Vec, + version: u32, + held: bool, +) -> Result, AgentStorageError> { + let mut directory = root.clone(); + if held && !directory.identity().is_held() { + return Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))); + } + for name in names { + directory = match if create { + directory.create_child(&name) + } else { + directory.child(&name) + } { + Ok(directory) => directory, + Err(LocalFsError::NotFound(_)) => return Ok(None), + Err(error) => return Err(error.into()), + }; + if held && !directory.identity().is_held() { + return Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))); + } + } + Ok(Some(Partition { + directory, + scope, + version, + })) +} + +pub(super) fn readable( + root: &Arc, + scope: &ChatScope, +) -> Result, AgentStorageError> { + let mut partitions = Vec::new(); + if let Some(current) = Partition::environment(root, scope, false)? { + partitions.push(current); + } + if scope.environment().is_this_computer() { + if let Some(previous) = Partition::scoped(root, scope.context(), false)? { + partitions.push(previous); + } + if scope.context() == ContextId::General { + partitions.push(Partition::legacy(root.clone())); + } + } + Ok(partitions) +} diff --git a/crates/agent-storage/src/publication.rs b/crates/agent-storage/src/publication.rs new file mode 100644 index 00000000..c801138f --- /dev/null +++ b/crates/agent-storage/src/publication.rs @@ -0,0 +1,31 @@ +//! Scoped private staging uses the supplied held-directory publication lifecycle. +use crate::namespace::Partition; +use nocterm_ai::storage::AgentStorageError; +use nocterm_session::fs::UploadMode; + +pub(super) fn publish( + partition: &Partition, + id: &str, + extension: &str, + bytes: &[u8], +) -> Result<(), AgentStorageError> { + let name = partition.name(id, extension)?; + let mut stage = partition.directory().stage()?; + let result = (|| { + for chunk in bytes.chunks(32 * 1024) { + stage.write(chunk)?; + } + stage.sync()?; + stage.publish(&name, UploadMode::Replace) + })(); + match result { + Ok(_) => Ok(()), // Committed durability warnings cannot become failed writes. + Err(operation) => match stage.cancel() { + Ok(()) => Err(operation.into()), + Err(cleanup) => Err(AgentStorageError::StageCleanup { + operation: nocterm_core::SharedError::from_error(operation), + cleanup: nocterm_core::SharedError::from_error(cleanup), + }), + }, + } +} diff --git a/crates/agent-storage/tests/prepared_history.rs b/crates/agent-storage/tests/prepared_history.rs new file mode 100644 index 00000000..b3127850 --- /dev/null +++ b/crates/agent-storage/tests/prepared_history.rs @@ -0,0 +1,136 @@ +//! Prepared native history keeps its original bytes and decoded allocation together. +use futures::executor::block_on; +use nocterm_agent_storage::FileChatRepository; +use nocterm_ai::{ + history::{self, ContextId, SavedChat, SharedChat}, + storage::{AgentStorageError, ChatRepository, PreparedChatBytes, PreparedChatLoad}, + testing::{InMemoryAgentRepository, StorageOperation}, +}; +use nocterm_core::allocation::AllocationLease; +use nocterm_session::testing::InMemoryFs; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; + +const ID: &str = "00000000-0000-4000-8000-0000000000a1"; + +fn chat(draft: &str) -> SavedChat { + let mut chat = SavedChat::for_identity("agent".into(), ID.into(), 1); + chat.draft = Some(draft.into()); + chat +} + +fn prepared() -> Box { + Box::new( + PreparedChatBytes::new( + history::encode(&chat("original")).unwrap(), + ContextId::General.into(), + ID.into(), + Some(2), + ) + .unwrap(), + ) +} + +#[test] +fn prepared_decode_refuses_insufficient_bytes_and_missing_payload_slot() { + let source = prepared(); + let bytes = source.resident_bytes(); + assert!(bytes > 0); + let result = block_on(source.decode(AllocationLease::new(bytes - 1, 1, ()))); + assert!(matches!( + result, + Err(AgentStorageError::ResidentCapacity { .. }) + )); + let source = prepared(); + let bytes = source.resident_bytes(); + let result = block_on(source.decode(AllocationLease::new(bytes, 0, ()))); + assert!(matches!(result, Err(AgentStorageError::PayloadCapacity))); +} + +struct Release(Arc); +impl Drop for Release { + fn drop(&mut self) { + self.0.fetch_add(1, Ordering::SeqCst); + } +} + +#[test] +fn detached_decode_and_returned_generation_retain_the_same_allocation() { + let source = prepared(); + let released = Arc::new(AtomicUsize::new(0)); + let bytes = source.resident_bytes(); + let decode = source.decode(AllocationLease::new(bytes, 1, Release(released.clone()))); + assert_eq!(released.load(Ordering::SeqCst), 0); + let generation = std::thread::spawn(move || Arc::new(block_on(decode).unwrap())) + .join() + .unwrap(); + assert_eq!(generation.value().draft.as_deref(), Some("original")); + assert_eq!(generation.allocation().bytes(), bytes); + assert_eq!(released.load(Ordering::SeqCst), 0); + drop(generation); + assert_eq!(released.load(Ordering::SeqCst), 1); +} + +#[test] +fn native_prepared_history_survives_delete_and_replacement_without_reopening() { + let directory = tempfile::tempdir().unwrap(); + let filesystem = Arc::new(InMemoryFs::default()); + for ancestor in directory + .path() + .ancestors() + .collect::>() + .into_iter() + .rev() + { + filesystem.seed_native_directory(ancestor).unwrap(); + } + let repository = FileChatRepository::new(directory.path().into(), filesystem); + let original = SharedChat { + metadata: chat("original"), + queue: Vec::new(), + }; + block_on(repository.save(Arc::new(original))).unwrap(); + let source = + block_on(repository.prepare_load_in(ContextId::General.into(), ID.into())).unwrap(); + block_on(repository.delete(ContextId::General, ID.into())).unwrap(); + let replacement = SharedChat { + metadata: chat("replacement"), + queue: Vec::new(), + }; + block_on(repository.save(Arc::new(replacement))).unwrap(); + let bytes = source.resident_bytes(); + let loaded = block_on(source.decode(AllocationLease::new(bytes, 1, ()))).unwrap(); + assert_eq!(loaded.value().draft.as_deref(), Some("original")); + let current = block_on(repository.load(ContextId::General, ID.into())).unwrap(); + assert_eq!(current.draft.as_deref(), Some("replacement")); +} + +#[test] +fn shared_repository_preparation_uses_existing_load_failures_and_frozen_bytes() { + let repository = InMemoryAgentRepository::default(); + repository.seed_chat(&chat("original")).unwrap(); + repository.fail_next( + StorageOperation::Load(ID.into()), + AgentStorageError::Interrupted, + ); + assert!(matches!( + block_on(repository.prepare_load_in(ContextId::General.into(), ID.into())), + Err(AgentStorageError::Interrupted) + )); + let source = + block_on(repository.prepare_load_in(ContextId::General.into(), ID.into())).unwrap(); + repository.seed_chat(&chat("replacement")).unwrap(); + let bytes = source.resident_bytes(); + let loaded = block_on(source.decode(AllocationLease::new(bytes, 1, ()))).unwrap(); + assert_eq!(loaded.value().draft.as_deref(), Some("original")); + assert_eq!( + repository.operations(), + vec![ + StorageOperation::Load(ID.into()), + StorageOperation::Load(ID.into()), + StorageOperation::Decoded(ID.into()), + ] + ); +} diff --git a/crates/agent-tools/Cargo.toml b/crates/agent-tools/Cargo.toml new file mode 100644 index 00000000..16786662 --- /dev/null +++ b/crates/agent-tools/Cargo.toml @@ -0,0 +1,21 @@ +[package] +name = "nocterm-agent-tools" +description = "Bounded agent command execution behind session contracts." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "adapter" + +[dependencies] +nocterm-session.workspace = true +futures.workspace = true +async-channel.workspace = true + +[dev-dependencies] +nocterm-session = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/agent-chat/src/thread/execution/jobs.rs b/crates/agent-tools/src/jobs.rs similarity index 59% rename from crates/agent-chat/src/thread/execution/jobs.rs rename to crates/agent-tools/src/jobs.rs index 2edb2901..f36d627e 100644 --- a/crates/agent-chat/src/thread/execution/jobs.rs +++ b/crates/agent-tools/src/jobs.rs @@ -1,116 +1,40 @@ //! Bounded command ownership; cancellation lives independently of read waiters. use futures::FutureExt as _; -use nocterm_session::{ExecExit, ExecRequest, HostExec}; +use nocterm_session::command_jobs::{ + CommandJobs, CommandSnapshot as Snapshot, CommandStart, CommandState as State, JobError, +}; +use nocterm_session::{ExecRequest, HostExec}; use std::{ collections::BTreeMap, sync::{Arc, Mutex}, - time::Duration, }; -pub(in crate::thread) const MAX_ACTIVE: usize = 4; +const MAX_ACTIVE: usize = 4; const MAX_RETAINED: usize = 16; const MAX_STDOUT: usize = 64 * 1024; -/// Why a command was not started or found; the agent sees the text. -#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] -pub(in crate::thread) enum JobError { - #[error("This chat already has four active commands. Read or cancel one first.")] - Busy, - #[error("Unknown command for this chat and terminal.")] - Unknown, -} -impl From for nocterm_ai::ToolFailure { - fn from(error: JobError) -> Self { - error.to_string().into() - } -} - -#[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub(in crate::thread) enum State { - Starting, - Running, - Exited, - TimedOut, - Cancelled, - Failed, -} -impl State { - pub(in crate::thread) fn as_str(self) -> &'static str { - match self { - Self::Starting => "starting", - Self::Running => "running", - Self::Exited => "exited", - Self::TimedOut => "timed_out", - Self::Cancelled => "cancelled", - Self::Failed => "failed", - } - } - fn active(self) -> bool { - matches!(self, Self::Starting | Self::Running) - } -} - -#[derive(Clone, Debug)] -pub(in crate::thread) struct Snapshot { - state: State, - stdout: Vec, - total_bytes: u64, - exit: Option, - error: Option, +fn append_output(snapshot: &mut Snapshot, bytes: &[u8]) { + snapshot.total_bytes = snapshot.total_bytes.saturating_add(bytes.len() as u64); + let keep = bytes + .len() + .min(MAX_STDOUT.saturating_sub(snapshot.stdout.len())); + snapshot.stdout.extend_from_slice(&bytes[..keep]); } -impl Default for Snapshot { - fn default() -> Self { - Self { - state: State::Starting, - stdout: Vec::new(), - total_bytes: 0, - exit: None, - error: None, - } - } -} -impl Snapshot { - pub(in crate::thread) fn state(&self) -> State { - self.state - } - pub(in crate::thread) fn stdout(&self) -> &[u8] { - &self.stdout - } - pub(in crate::thread) fn total_bytes(&self) -> u64 { - self.total_bytes - } - pub(in crate::thread) fn exit(&self) -> Option<&ExecExit> { - self.exit.as_ref() - } - pub(in crate::thread) fn error(&self) -> Option<&str> { - self.error.as_deref() - } - pub(in crate::thread) fn active(&self) -> bool { - self.state.active() - } - fn append(&mut self, bytes: &[u8]) { - self.total_bytes = self.total_bytes.saturating_add(bytes.len() as u64); - let keep = bytes - .len() - .min(MAX_STDOUT.saturating_sub(self.stdout.len())); - self.stdout.extend_from_slice(&bytes[..keep]); - } -} -pub(in crate::thread) struct Job { +struct Job { terminal_id: String, executor: Arc, snapshot: Arc>, cancel: async_channel::Sender<()>, } impl Job { - pub(in crate::thread) fn snapshot(&self) -> Snapshot { + fn snapshot(&self) -> Snapshot { self.snapshot .lock() .unwrap_or_else(std::sync::PoisonError::into_inner) .clone() } - pub(in crate::thread) fn cancel(&self, state: State) { + fn cancel(&self, state: State) { let mut snapshot = self .snapshot .lock() @@ -127,21 +51,12 @@ impl Drop for Job { } } #[derive(Default)] -pub(in crate::thread) struct Jobs { +pub(crate) struct Jobs { jobs: BTreeMap, next: u64, } -pub(in crate::thread) struct CommandStart { - id: String, - run: futures::future::BoxFuture<'static, ()>, -} -impl CommandStart { - pub(in crate::thread) fn into_run(self) -> (String, futures::future::BoxFuture<'static, ()>) { - (self.id, self.run) - } -} impl Jobs { - pub(in crate::thread) fn start( + fn start_owned( &mut self, terminal: String, executor: Arc, @@ -149,10 +64,10 @@ impl Jobs { deadline: impl std::future::Future + Send + 'static, ) -> Result { let (id, state, cancel) = self.insert(terminal, executor.clone())?; - Ok(CommandStart { + Ok(CommandStart::new( id, - run: Box::pin(run(executor, request, state, cancel, deadline)), - }) + Box::pin(run(executor, request, state, cancel, deadline)), + )) } fn insert( @@ -195,24 +110,24 @@ impl Jobs { ); Ok((format!("c{}", self.next), snapshot, receiver)) } - pub(in crate::thread) fn get(&self, id: &str, terminal: &str) -> Result<&Job, JobError> { + fn get(&self, id: &str, terminal: &str) -> Result<&Job, JobError> { id.strip_prefix('c') .and_then(|id| id.parse::().ok()) .and_then(|id| self.jobs.get(&id)) .filter(|job| job.terminal_id == terminal) .ok_or(JobError::Unknown) } - pub(in crate::thread) fn cancel_terminal(&self, terminal: &str) { + fn cancel_terminal(&self, terminal: &str) { for job in self.jobs.values().filter(|job| job.terminal_id == terminal) { job.cancel(State::Cancelled); } } - pub(in crate::thread) fn cancel_all(&self) { + fn cancel_all(&self) { for job in self.jobs.values() { job.cancel(State::Cancelled); } } - pub(in crate::thread) fn active(&self) -> impl Iterator)> { + fn active(&self) -> impl Iterator)> { self.jobs .values() .filter(|job| job.snapshot().active()) @@ -244,7 +159,7 @@ async fn run( .unwrap_or_else(std::sync::PoisonError::into_inner); if state.active() { state.state = State::Failed; - state.error = Some(error.to_string()); + state.error = Some(error); } return; } @@ -259,10 +174,12 @@ async fn run( state.state = State::Running; } while let Some(chunk) = output.next().await { - snapshot - .lock() - .unwrap_or_else(std::sync::PoisonError::into_inner) - .append(&chunk); + append_output( + &mut snapshot + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner), + &chunk, + ); } let exit = output.exit().await; let mut state = snapshot @@ -299,9 +216,40 @@ async fn run( } } } -pub(in crate::thread) fn timeout(value: Option) -> Duration { - Duration::from_millis(value.unwrap_or(30_000)) -} -#[cfg(test)] -mod tests; +impl CommandJobs for Jobs { + fn start( + &mut self, + terminal: String, + executor: Arc, + request: ExecRequest, + deadline: futures::future::BoxFuture<'static, ()>, + ) -> Result { + self.start_owned(terminal, executor, request, deadline) + } + fn snapshot(&self, id: &str, terminal: &str) -> Result { + Ok(self.get(id, terminal)?.snapshot()) + } + fn cancel(&self, id: &str, terminal: &str) -> Result { + let job = self.get(id, terminal)?; + job.cancel(State::Cancelled); + Ok(job.snapshot()) + } + fn cancel_all(&self) { + Jobs::cancel_all(self); + } + fn has_active(&self) -> bool { + Jobs::active(self).next().is_some() + } + fn revoke_replaced(&self, current: &[(String, Option>)]) { + for (id, executor) in Jobs::active(self) { + let replacement = current + .iter() + .find(|(candidate, _)| candidate == id) + .and_then(|(_, executor)| executor.as_ref()); + if !replacement.is_some_and(|replacement| Arc::ptr_eq(replacement, executor)) { + self.cancel_terminal(id); + } + } + } +} diff --git a/crates/agent-tools/src/lib.rs b/crates/agent-tools/src/lib.rs new file mode 100644 index 00000000..3efe7e22 --- /dev/null +++ b/crates/agent-tools/src/lib.rs @@ -0,0 +1,20 @@ +//! Bounded agent commands without GUI state. +//! +//! Owns: per-chat command admission, continuous stdout draining, retained outcomes, +//! cancellation and absolute deadlines, retaining the original executor for every job. +//! Does not own: approvals, SSH authentication, model execution or views. +//! Ports used: session::HostExec. Ports provided: +//! session::command_jobs::{CommandJobs, CommandJobsFactory}. +//! Settings sections: none. Globals: none. +//! Invariants: at most four active and sixteen retained commands per registry; +//! stdout retains a 64KiB prefix while all bytes are drained and counted; +//! cancelling never invents a successful exit or replaces an accepted executor. +mod jobs; + +#[derive(Default)] +pub struct JobsFactory; +impl nocterm_session::command_jobs::CommandJobsFactory for JobsFactory { + fn create(&self) -> Box { + Box::::default() + } +} diff --git a/crates/agent-tools/tests/command_lifecycle.rs b/crates/agent-tools/tests/command_lifecycle.rs new file mode 100644 index 00000000..ec989532 --- /dev/null +++ b/crates/agent-tools/tests/command_lifecycle.rs @@ -0,0 +1,331 @@ +use futures::{ + FutureExt as _, + future::{BoxFuture, pending, ready}, +}; +use nocterm_agent_tools::JobsFactory; +use nocterm_session::{ + ErrorTail, ExecError, ExecRequest, HostExec, + command_jobs::{CommandJobs, CommandJobsFactory, CommandState, JobError}, + testing::FakeExec, +}; +use std::sync::Arc; + +fn start( + jobs: &mut dyn CommandJobs, + executor: Arc, + terminal: &str, +) -> (String, BoxFuture<'static, ()>) { + jobs.start( + terminal.into(), + executor, + ExecRequest::new("program"), + pending().boxed(), + ) + .unwrap() + .into_run() +} + +#[test] +fn four_active_commands_refuse_fifth_and_terminal_or_chat_foreign_ids() { + let mut jobs = JobsFactory.create(); + assert!(!jobs.has_active()); + let executor = Arc::new(FakeExec::default()); + let mut runs = Vec::new(); + for _ in 0..4 { + runs.push(start(&mut *jobs, executor.clone(), "original")); + } + assert!( + jobs.has_active(), + "unpolled starts must keep their owner active" + ); + assert!(matches!( + jobs.start( + "original".into(), + executor.clone(), + ExecRequest::new("no"), + pending().boxed() + ), + Err(JobError::Busy) + )); + for (id, terminal) in [ + (runs[0].0.as_str(), "foreign"), + ("c99", "original"), + ("bad", "original"), + ] { + assert!(matches!( + jobs.snapshot(id, terminal), + Err(JobError::Unknown) + )); + } + assert!(matches!( + JobsFactory.create().snapshot(&runs[0].0, "original"), + Err(JobError::Unknown) + )); + assert!(executor.requests().is_empty()); +} + +#[test] +fn cancellation_or_elapsed_deadline_before_poll_never_executes() { + futures::executor::block_on(async { + for cancelled in [true, false] { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let (id, run) = jobs + .start( + "t".into(), + executor.clone(), + ExecRequest::new("no"), + ready(()).boxed(), + ) + .unwrap() + .into_run(); + if cancelled { + jobs.cancel(&id, "t").unwrap(); + } + run.await; + assert!(executor.requests().is_empty()); + assert_eq!( + jobs.snapshot(&id, "t").unwrap().state(), + if cancelled { + CommandState::Cancelled + } else { + CommandState::TimedOut + } + ); + } + }); +} + +#[test] +fn output_keeps_prefix_while_draining_every_chunk_and_preserving_stderr_exit_and_arguments() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let request = ExecRequest::new("program") + .args(["a b", "$(literal)", "", "'quote'"]) + .stdin(b"first\nsecond\n".to_vec()); + let (id, run) = jobs + .start( + "t".into(), + executor.clone(), + request.clone(), + pending().boxed(), + ) + .unwrap() + .into_run(); + let produce = async move { + for _ in 0..40 { + assert!(sink.send(vec![b'x'; 4096]).await); + } + let mut errors = ErrorTail::default(); + errors.push(&vec![b'e'; 8192]); + errors.push(b"\nlast error"); + sink.finish(errors.exit(Some(7))); + }; + futures::join!(run, produce); + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Exited); + assert_eq!(snapshot.stdout(), vec![b'x'; 65536]); + assert_eq!(snapshot.total_bytes(), 40 * 4096); + assert_eq!(snapshot.exit().unwrap().status, Some(7)); + assert_eq!(snapshot.exit().unwrap().stderr.len(), 4096); + assert!(snapshot.exit().unwrap().stderr.ends_with("last error")); + assert_eq!(executor.requests(), vec![request]); + }); +} + +#[test] +fn silent_exit_finishes_without_output() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::new(vec![("program", Ok(("", 0, "")))])); + let (id, run) = start(&mut *jobs, executor, "t"); + run.await; + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Exited); + assert_eq!(snapshot.exit().unwrap().status, Some(0)); + assert!(snapshot.stdout().is_empty()); + }); +} + +#[test] +fn cancelling_running_command_drops_output_and_retains_unknown_exit() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let (id, mut run) = start(&mut *jobs, executor.clone(), "t"); + assert!(run.as_mut().now_or_never().is_none()); + assert_eq!(executor.requests().len(), 1); + assert!(sink.send(b"running".to_vec()).await); + assert!(run.as_mut().now_or_never().is_none()); + jobs.cancel(&id, "t").unwrap(); + run.await; + sink.closed().await; + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Cancelled); + assert!(snapshot.exit().is_none()); + assert!(sink.is_closed()); + }); +} + +#[test] +fn retained_commands_evict_oldest_completed_result_and_allow_new_admission() { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let mut ids = Vec::new(); + for _ in 0..17 { + let (id, _run) = start(&mut *jobs, executor.clone(), "t"); + jobs.cancel(&id, "t").unwrap(); + ids.push(id); + } + assert!(matches!( + jobs.snapshot(&ids[0], "t"), + Err(JobError::Unknown) + )); + for id in &ids[1..] { + assert_eq!( + jobs.snapshot(id, "t").unwrap().state(), + CommandState::Cancelled + ); + } + assert!(!jobs.has_active()); +} + +#[test] +fn completed_result_churn_never_evicts_an_older_running_command() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let (running, mut run) = start(&mut *jobs, executor.clone(), "live"); + assert!(run.as_mut().now_or_never().is_none()); + let mut completed = Vec::new(); + for _ in 0..20 { + let (id, _run) = start(&mut *jobs, executor.clone(), "short"); + jobs.cancel(&id, "short").unwrap(); + completed.push(id); + } + assert!(jobs.has_active()); + assert_eq!( + jobs.snapshot(&running, "live").unwrap().state(), + CommandState::Running + ); + assert!(matches!( + jobs.snapshot(&completed[0], "short"), + Err(JobError::Unknown) + )); + assert!(!sink.is_closed()); + assert!(matches!( + jobs.cancel(&running, "short"), + Err(JobError::Unknown) + )); + assert!(jobs.snapshot(&running, "live").unwrap().active()); + jobs.cancel(&running, "live").unwrap(); + assert!(!jobs.has_active()); + run.await; + sink.closed().await; + }); +} + +#[test] +fn replacement_executor_revokes_only_its_original_terminal() { + let mut jobs = JobsFactory.create(); + let original: Arc = Arc::new(FakeExec::default()); + let replacement: Arc = Arc::new(FakeExec::default()); + let (first, _run) = start(&mut *jobs, original.clone(), "first"); + let (other, _run) = start(&mut *jobs, original.clone(), "other"); + jobs.revoke_replaced(&[ + ("first".into(), Some(replacement)), + ("other".into(), Some(original)), + ]); + assert_eq!( + jobs.snapshot(&first, "first").unwrap().state(), + CommandState::Cancelled + ); + assert!(jobs.snapshot(&other, "other").unwrap().active()); + jobs.cancel_all(); + assert!(!jobs.snapshot(&other, "other").unwrap().active()); +} + +#[test] +fn startup_failure_preserves_the_typed_transport_error() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::new(vec![( + "program", + Err(ExecError::Disconnected), + )])); + let (id, run) = start(&mut *jobs, executor, "t"); + run.await; + let snapshot = jobs.snapshot(&id, "t").unwrap(); + assert_eq!(snapshot.state(), CommandState::Failed); + assert!(matches!(snapshot.error(), Some(ExecError::Disconnected))); + assert!(snapshot.exit().is_none()); + }); +} + +#[test] +fn absolute_deadline_covers_suspended_startup_and_silent_running_output() { + futures::executor::block_on(async { + for suspended in [true, false] { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + if suspended { + executor.suspend(nocterm_session::testing::Gate::pending()); + } + let (expire, deadline) = async_channel::bounded::<()>(1); + let deadline = async move { + let _ = deadline.recv().await; + } + .boxed(); + let (id, mut run) = jobs + .start( + "t".into(), + executor.clone(), + ExecRequest::new("program"), + deadline, + ) + .unwrap() + .into_run(); + assert!(run.as_mut().now_or_never().is_none()); + assert_eq!(executor.requests().len(), 1); + assert_eq!( + jobs.snapshot(&id, "t").unwrap().state(), + if suspended { + CommandState::Starting + } else { + CommandState::Running + } + ); + expire.close(); + run.await; + assert_eq!( + jobs.snapshot(&id, "t").unwrap().state(), + CommandState::TimedOut + ); + assert!(jobs.snapshot(&id, "t").unwrap().exit().is_none()); + sink.closed().await; + } + }); +} + +#[test] +fn dropping_the_registry_cancels_a_runner_even_when_its_read_waiter_is_gone() { + futures::executor::block_on(async { + let mut jobs = JobsFactory.create(); + let executor = Arc::new(FakeExec::default()); + let sink = executor.queue_stream("program"); + let (id, mut run) = start(&mut *jobs, executor, "t"); + assert!(run.as_mut().now_or_never().is_none()); + let observation = jobs.snapshot(&id, "t").unwrap(); + drop(observation); + assert!(!sink.is_closed()); + drop(jobs); + run.await; + sink.closed().await; + assert!(sink.is_closed()); + }); +} diff --git a/crates/agent-usage/Cargo.toml b/crates/agent-usage/Cargo.toml deleted file mode 100644 index 40041774..00000000 --- a/crates/agent-usage/Cargo.toml +++ /dev/null @@ -1,22 +0,0 @@ -[package] -name = "nocterm-agent-usage" -description = "Bounded native reads of provider account plan limits." -version = "0.0.0" -edition.workspace = true -license-file.workspace = true -publish = false - -[package.metadata.nocterm] -layer = "adapter" - -[dependencies] -nocterm-ai.workspace = true -futures.workspace = true -reqwest.workspace = true -serde_json.workspace = true - -[dev-dependencies] -tempfile.workspace = true - -[lints] -workspace = true diff --git a/crates/agent-usage/src/claude.rs b/crates/agent-usage/src/claude.rs deleted file mode 100644 index b8bf7a81..00000000 --- a/crates/agent-usage/src/claude.rs +++ /dev/null @@ -1,136 +0,0 @@ -//! Claude's account usage, read with Claude Code's own OAuth token. -use futures::future::BoxFuture; -use nocterm_ai::usage::{AccountUsageSource, RateLimit, UsageAgent, UsageReadError}; -use std::{ - io::Read as _, - path::{Path, PathBuf}, - time::{Duration, SystemTime, UNIX_EPOCH}, -}; - -const USAGE_URL: &str = "https://api.anthropic.com/api/oauth/usage"; -const TIMEOUT: Duration = Duration::from_secs(10); -const MAX_BODY_BYTES: u64 = 1024 * 1024; - -/// Reads Claude's plan limits under a configuration directory supplied by -/// composition, for whichever agent the runtime asks about. -pub struct ClaudeUsageReader { - config: Option, -} - -impl ClaudeUsageReader { - pub fn new(config: Option) -> Self { - Self { config } - } -} - -impl AccountUsageSource for ClaudeUsageReader { - fn read( - &self, - _: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - let config = self.config.clone(); - Box::pin(async move { - match config { - Some(config) => match token(&config.join(".credentials.json"))? { - Some(token) => fetch(&token), - None => Ok(None), - }, - None => Ok(None), - } - }) - } -} - -/// The unexpired access token in Claude Code's credentials, if it keeps one -/// there. -fn token(path: &Path) -> Result, UsageReadError> { - let text = match std::fs::read_to_string(path) { - Ok(text) => text, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), - Err(error) => return Err(UsageReadError::read(path.to_owned(), error)), - }; - let Ok(value) = serde_json::from_str::(&text) else { - return Ok(None); - }; - let oauth = &value["claudeAiOauth"]; - let now = SystemTime::now() - .duration_since(UNIX_EPOCH) - .map_or(0, |now| now.as_millis()); - let expired = oauth["expiresAt"] - .as_u64() - .is_some_and(|expires| u128::from(expires) <= now); - Ok(oauth["accessToken"] - .as_str() - .filter(|token| !token.is_empty() && !expired) - .map(str::to_owned)) -} - -fn fetch(token: &str) -> Result>, UsageReadError> { - let client = reqwest::blocking::Client::builder() - .timeout(TIMEOUT) - .user_agent(concat!("nocterm/", env!("CARGO_PKG_VERSION"))) - .build() - .map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - let response = client - .get(USAGE_URL) - .bearer_auth(token) - .header("anthropic-beta", "oauth-2025-04-20") - .header("Content-Type", "application/json") - .send() - .map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - // A rejected or unsupported account reports nothing rather than failing. - if !response.status().is_success() { - return Ok(None); - } - let mut bytes = Vec::new(); - response - .take(MAX_BODY_BYTES) - .read_to_end(&mut bytes) - .map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - let body: serde_json::Value = - serde_json::from_slice(&bytes).map_err(|error| UsageReadError::fetch(USAGE_URL, error))?; - Ok(crate::parser::claude_limits(&body)) -} - -#[cfg(test)] -mod tests { - use super::{ClaudeUsageReader, token}; - use nocterm_ai::usage::{AccountUsageSource as _, UsageAgent}; - - fn agent() -> UsageAgent { - UsageAgent { - id: "claude".into(), - account: nocterm_ai::agents::AccountUsage::OAuthAccount, - } - } - - #[test] - fn missing_credentials_report_nothing() { - let dir = tempfile::tempdir().unwrap(); - let reader = ClaudeUsageReader::new(Some(dir.path().to_owned())); - assert_eq!( - futures::executor::block_on(reader.read(agent())).unwrap(), - None - ); - let unconfigured = ClaudeUsageReader::new(None); - assert_eq!( - futures::executor::block_on(unconfigured.read(agent())).unwrap(), - None - ); - } - - #[test] - fn only_an_unexpired_token_is_used() { - let dir = tempfile::tempdir().unwrap(); - let path = dir.path().join(".credentials.json"); - let write = |json: serde_json::Value| std::fs::write(&path, json.to_string()).unwrap(); - write( - serde_json::json!({"claudeAiOauth": {"accessToken": "fresh", "expiresAt": u64::MAX}}), - ); - assert_eq!(token(&path).unwrap().as_deref(), Some("fresh")); - write(serde_json::json!({"claudeAiOauth": {"accessToken": "stale", "expiresAt": 1}})); - assert_eq!(token(&path).unwrap(), None); - std::fs::write(&path, "not json").unwrap(); - assert_eq!(token(&path).unwrap(), None); - } -} diff --git a/crates/agent-usage/src/lib.rs b/crates/agent-usage/src/lib.rs deleted file mode 100644 index 3dd50f80..00000000 --- a/crates/agent-usage/src/lib.rs +++ /dev/null @@ -1,51 +0,0 @@ -//! Bounded native reads of provider account plan limits. -//! -//! Owns: Claude's account usage request and its response grammar, the read of -//! the OAuth token Claude Code keeps in its configuration directory, Codex's -//! session-log tail reader and grammar, and where both live on this machine -//! (`native_sources`). -//! Does not own: which agent reports limits which way (the agent's -//! `UsageProfile`), scheduling, merging or presenting limits (agent runtime -//! and feature), or token refresh: an expired token yields no report until -//! Claude Code renews it. -//! Ports provided: AI `AccountUsageSource` per `AccountUsage` kind. Ports used: -//! none. -//! Settings section: none. Globals: none. -//! Invariants: readers serve only the agents `UsageSources` routes to them by -//! profile; the token is sent only to Anthropic's API over TLS and never -//! logged; requests are bounded by a timeout, log reads by a tail size, and -//! both run when the runtime polls on its background executor. -mod claude; -mod parser; -mod session_log; - -pub use claude::ClaudeUsageReader; -pub use session_log::CodexUsageReader; - -use nocterm_ai::{agents::AccountUsage, usage::UsageSources}; -use std::{path::PathBuf, sync::Arc}; - -/// The account sources for this machine, with their locations resolved once -/// from `CLAUDE_CONFIG_DIR`, `CODEX_HOME` and `HOME`. -pub fn native_sources() -> UsageSources { - UsageSources::default() - .with( - AccountUsage::OAuthAccount, - Arc::new(ClaudeUsageReader::new(home_dir( - "CLAUDE_CONFIG_DIR", - ".claude", - ))), - ) - .with( - AccountUsage::SessionLog, - Arc::new(CodexUsageReader::new(home_dir("CODEX_HOME", ".codex"))), - ) -} - -/// `variable` when set, otherwise `default` under the home directory. -fn home_dir(variable: &str, default: &str) -> Option { - std::env::var_os(variable) - .filter(|dir| !dir.is_empty()) - .map(PathBuf::from) - .or_else(|| std::env::var_os("HOME").map(|home| PathBuf::from(home).join(default))) -} diff --git a/crates/agent-usage/src/parser.rs b/crates/agent-usage/src/parser.rs deleted file mode 100644 index 4ac39031..00000000 --- a/crates/agent-usage/src/parser.rs +++ /dev/null @@ -1,112 +0,0 @@ -//! Claude's account usage grammar, separate from its native request. -use nocterm_ai::usage::{RateLimit, Window}; -use serde_json::Value; - -/// The plan limits in Claude's account usage response, if it reports any. -pub(crate) fn claude_limits(body: &Value) -> Option> { - let windows = [ - ("five_hour", Window::FiveHour), - ("seven_day", Window::Weekly), - ("seven_day_opus", Window::WeeklyModel("Opus".into())), - ("seven_day_sonnet", Window::WeeklyModel("Sonnet".into())), - ]; - let mut limits: Vec<_> = windows - .into_iter() - .filter_map(|(key, window)| limit(&body[key], window)) - .collect(); - let extra = &body["extra_usage"]; - if extra["is_enabled"].as_bool() == Some(true) { - limits.extend(limit(extra, Window::Extra)); - } - (!limits.is_empty()).then_some(limits) -} - -/// One window, whose utilization is a percentage. -fn limit(window: &Value, kind: Window) -> Option { - let used = (window["utilization"].as_f64()? / 100.).clamp(0., 1.); - Some(RateLimit { - window: kind, - used: Some(used), - resets_at: window["resets_at"].as_str().and_then(unix_seconds), - reached: used >= 1., - }) -} - -/// An RFC 3339 timestamp (`2026-10-20T21:49:59.785225+00:00`) in seconds -/// since the Unix epoch. -fn unix_seconds(text: &str) -> Option { - let (date, rest) = text.split_once('T')?; - let mut date = date.splitn(3, '-').map(str::parse::); - let (year, month, day) = (date.next()?.ok()?, date.next()?.ok()?, date.next()?.ok()?); - let zone = rest.find(['Z', 'z', '+', '-'])?; - let (time, offset) = rest.split_at(zone); - let mut time = time.split(':'); - let hour: i64 = time.next()?.parse().ok()?; - let minute: i64 = time.next()?.parse().ok()?; - let second: i64 = time.next()?.split('.').next()?.parse().ok()?; - let offset = match offset.as_bytes().first()? { - b'Z' | b'z' => 0, - sign => { - let (hours, minutes) = offset[1..].split_once(':')?; - let minutes = hours.parse::().ok()? * 60 + minutes.parse::().ok()?; - if *sign == b'-' { -minutes } else { minutes } - } - }; - let seconds = days_from_civil(year, month, day) * 86_400 + hour * 3_600 + minute * 60 + second - - offset * 60; - u64::try_from(seconds).ok() -} - -/// Days from 1970-01-01 to a proleptic Gregorian date. -fn days_from_civil(year: i64, month: i64, day: i64) -> i64 { - let year = if month <= 2 { year - 1 } else { year }; - let era = year.div_euclid(400); - let year_of_era = year - era * 400; - let day_of_year = (153 * (month + if month > 2 { -3 } else { 9 }) + 2) / 5 + day - 1; - let day_of_era = year_of_era * 365 + year_of_era / 4 - year_of_era / 100 + day_of_year; - era * 146_097 + day_of_era - 719_468 -} - -#[cfg(test)] -mod tests { - use super::{claude_limits, unix_seconds}; - use nocterm_ai::usage::Window; - - #[test] - fn account_usage_reports_each_window_as_a_share() { - let body = serde_json::json!({ - "five_hour": {"utilization": 10.0, "resets_at": "2026-10-20T21:49:59.785225+00:00"}, - "seven_day": {"utilization": 100.0, "resets_at": "2026-10-23T15:59:59.785245+00:00"}, - "seven_day_opus": null, - "seven_day_sonnet": {"utilization": 3.0, "resets_at": null}, - "extra_usage": {"is_enabled": false, "utilization": null}, - }); - let limits = claude_limits(&body).unwrap(); - let windows: Vec<_> = limits.iter().map(|limit| limit.window.clone()).collect(); - assert_eq!( - windows, - [ - Window::FiveHour, - Window::Weekly, - Window::WeeklyModel("Sonnet".into()) - ] - ); - assert_eq!(limits[0].used, Some(0.1)); - assert_eq!(limits[0].resets_at, Some(1_792_532_999)); - assert!(!limits[0].reached); - assert!(limits[1].reached); - assert_eq!(limits[2].resets_at, None); - assert!(claude_limits(&serde_json::json!({"five_hour": null})).is_none()); - } - - #[test] - fn timestamps_honour_their_offset() { - assert_eq!(unix_seconds("1970-01-01T00:00:00Z"), Some(0)); - assert_eq!(unix_seconds("1970-01-01T01:00:00+01:00"), Some(0)); - assert_eq!( - unix_seconds("2000-03-01T00:00:00.5-00:30"), - Some(951_870_600) - ); - assert_eq!(unix_seconds("not a time"), None); - } -} diff --git a/crates/agent-usage/src/session_log.rs b/crates/agent-usage/src/session_log.rs deleted file mode 100644 index b0f79690..00000000 --- a/crates/agent-usage/src/session_log.rs +++ /dev/null @@ -1,91 +0,0 @@ -//! Codex's plan limits, read from the tail of its newest session log. -mod parser; -#[cfg(test)] -mod tests; - -use futures::future::BoxFuture; -use nocterm_ai::usage::{AccountUsageSource, RateLimit, UsageAgent, UsageReadError}; -use parser::codex_limits; -use std::{ - io::{Read as _, Seek as _, SeekFrom}, - path::{Path, PathBuf}, -}; - -const CODEX_TAIL_BYTES: u64 = 256 * 1024; - -/// Reads Codex's latest account report under a home supplied by composition, -/// for whichever agent the runtime asks about. -pub struct CodexUsageReader { - home: Option, -} -impl CodexUsageReader { - pub fn new(home: Option) -> Self { - Self { home } - } -} -impl AccountUsageSource for CodexUsageReader { - fn read( - &self, - _: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - let home = self.home.clone(); - Box::pin(async move { - match home { - Some(home) => read_codex_limits(&home), - None => Ok(None), - } - }) - } -} -fn read_codex_limits(home: &Path) -> Result>, UsageReadError> { - let mut dir = home.join("sessions"); - for _ in 0..3 { - let Some(next) = newest(&dir, |path| path.is_dir())? else { - return Ok(None); - }; - dir = next; - } - let Some(log) = newest(&dir, |path| { - path.extension() - .is_some_and(|extension| extension == "jsonl") - })? - else { - return Ok(None); - }; - let mut file = match std::fs::File::open(&log) { - Ok(file) => file, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), - Err(error) => return Err(read_error(&log, error)), - }; - let length = file - .metadata() - .map_err(|error| read_error(&log, error))? - .len(); - file.seek(SeekFrom::Start(length.saturating_sub(CODEX_TAIL_BYTES))) - .map_err(|source| UsageReadError::seek(log.clone(), source))?; - let mut tail = Vec::new(); - file.take(CODEX_TAIL_BYTES) - .read_to_end(&mut tail) - .map_err(|error| read_error(&log, error))?; - Ok(String::from_utf8_lossy(&tail) - .lines() - .rev() - .find_map(codex_limits)) -} -fn newest(dir: &Path, keep: impl Fn(&Path) -> bool) -> Result, UsageReadError> { - let entries = match std::fs::read_dir(dir) { - Ok(entries) => entries, - Err(error) if error.kind() == std::io::ErrorKind::NotFound => return Ok(None), - Err(source) => { - return Err(UsageReadError::enumerate(dir.to_owned(), source)); - } - }; - Ok(entries - .filter_map(Result::ok) - .map(|entry| entry.path()) - .filter(|path| keep(path)) - .max_by(|a, b| a.file_name().cmp(&b.file_name()))) -} -fn read_error(path: &Path, source: std::io::Error) -> UsageReadError { - UsageReadError::read(path.to_owned(), source) -} diff --git a/crates/agent-usage/src/session_log/parser.rs b/crates/agent-usage/src/session_log/parser.rs deleted file mode 100644 index 51048158..00000000 --- a/crates/agent-usage/src/session_log/parser.rs +++ /dev/null @@ -1,28 +0,0 @@ -//! Codex account-log grammar, separate from its native reader. -use nocterm_ai::usage::{RateLimit, Window}; - -/// The plan limits in one line of a Codex session log, if it reports them. -pub(super) fn codex_limits(line: &str) -> Option> { - if !line.contains("\"rate_limits\"") { - return None; - } - let value: serde_json::Value = serde_json::from_str(line).ok()?; - let limits = value.get("payload")?.get("rate_limits")?; - let reached = limits - .get("rate_limit_reached_type") - .is_some_and(|reached| !reached.is_null()); - let parsed: Vec<_> = ["primary", "secondary"] - .into_iter() - .filter_map(|key| { - let window = limits.get(key)?; - let used = window.get("used_percent")?.as_f64()? / 100.; - Some(RateLimit { - window: Window::from_minutes(window.get("window_minutes")?.as_u64()?), - used: Some(used.clamp(0., 1.)), - resets_at: window.get("resets_at").and_then(serde_json::Value::as_u64), - reached: reached && used >= 1., - }) - }) - .collect(); - (!parsed.is_empty()).then_some(parsed) -} diff --git a/crates/agent-usage/src/session_log/tests.rs b/crates/agent-usage/src/session_log/tests.rs deleted file mode 100644 index bbdb8c7a..00000000 --- a/crates/agent-usage/src/session_log/tests.rs +++ /dev/null @@ -1,155 +0,0 @@ -use super::*; -use futures::executor::block_on; -use nocterm_ai::usage::Window; - -const CODEX_LINE: &str = r#"{"timestamp":"2026-10-03T17:42:09Z","type":"event_msg","payload":{"type":"token_count","info":null,"rate_limits":{"limit_id":"codex","primary":{"used_percent":38.0,"window_minutes":10080,"resets_at":1791588105},"secondary":{"used_percent":7.5,"window_minutes":300,"resets_at":1791500000},"rate_limit_reached_type":null}}}"#; - -#[test] -fn codex_limits_come_from_its_session_log() { - let limits = codex_limits(CODEX_LINE).unwrap(); - assert_eq!(limits.len(), 2); - assert_eq!(limits[0].window, Window::Weekly); - assert_eq!(limits[0].used, Some(0.38)); - assert_eq!(limits[0].resets_at, Some(1_791_588_105)); - assert_eq!(limits[1].window, Window::FiveHour); - assert!(codex_limits(r#"{"payload":{"type":"agent_message"}}"#).is_none()); - assert!(codex_limits(r#"{"payload":{"rate_limits":{"primary":null}}}"#).is_none()); -} - -#[test] -fn the_newest_codex_log_is_read() { - let home = tempfile::tempdir().unwrap(); - let day = |path: &str| { - let dir = home.path().join("sessions").join(path); - std::fs::create_dir_all(&dir).unwrap(); - dir - }; - std::fs::write( - day("2026/09/30").join("rollout-a.jsonl"), - CODEX_LINE.replace("38.0", "99.0"), - ) - .unwrap(); - let newest = day("2026/10/03"); - std::fs::write(newest.join("rollout-a.jsonl"), "{}\n").unwrap(); - std::fs::write( - newest.join("rollout-b.jsonl"), - format!("{CODEX_LINE}\n{{\"payload\":{{\"type\":\"agent_message\"}}}}\n"), - ) - .unwrap(); - - let limits = block_on(CodexUsageReader::new(Some(home.path().to_owned())).read(codex())) - .unwrap() - .unwrap(); - - assert_eq!(limits[0].used, Some(0.38)); - assert!( - block_on(CodexUsageReader::new(Some(home.path().join("missing"))).read(codex())) - .unwrap() - .is_none() - ); -} - -fn codex() -> UsageAgent { - UsageAgent { - id: "custom".into(), - account: nocterm_ai::agents::AccountUsage::SessionLog, - } -} - -#[test] -fn an_unset_home_reports_nothing_and_an_unreadable_one_fails() { - let file = tempfile::NamedTempFile::new().unwrap(); - let reader = CodexUsageReader::new(Some(file.path().to_owned())); - assert!( - block_on(CodexUsageReader::new(None).read(codex())) - .unwrap() - .is_none() - ); - let error = block_on(reader.read(codex())).unwrap_err(); - assert!(matches!(error, UsageReadError::Enumerate { .. })); - use std::error::Error as _; - let native = error - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert_eq!(native.kind(), std::io::ErrorKind::NotADirectory); - let clone = error.clone(); - let shared = clone - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - assert_eq!(error.to_string(), clone.to_string()); -} - -#[test] -fn malformed_and_partial_utf8_tail_lines_preserve_the_last_usable_report() { - let home = tempfile::tempdir().unwrap(); - let day = home.path().join("sessions/2026/10/03"); - std::fs::create_dir_all(&day).unwrap(); - let mut bytes = vec![b'x'; CODEX_TAIL_BYTES as usize + 32]; - bytes.extend_from_slice(b"\xff\xfe\n"); - bytes.extend_from_slice(CODEX_LINE.as_bytes()); - bytes.extend_from_slice(b"\n{\"rate_limits\": broken}\n{}\n"); - std::fs::write(day.join("rollout-a.jsonl"), bytes).unwrap(); - let limits = block_on(CodexUsageReader::new(Some(home.path().to_owned())).read(codex())) - .unwrap() - .unwrap(); - assert_eq!(limits[0].used, Some(0.38)); -} - -#[test] -fn records_outside_the_tail_are_not_read_and_corrupt_reports_return_no_limits() { - let home = tempfile::tempdir().unwrap(); - let day = home.path().join("sessions/2026/10/03"); - std::fs::create_dir_all(&day).unwrap(); - let log = day.join("rollout-a.jsonl"); - let bytes = format!("{CODEX_LINE}\n{}", "x".repeat(CODEX_TAIL_BYTES as usize)); - std::fs::write(&log, bytes).unwrap(); - let reader = CodexUsageReader::new(Some(home.path().to_owned())); - assert!(block_on(reader.read(codex())).unwrap().is_none()); - std::fs::write(&log, "{\"rate_limits\": invalid}\n{}\n").unwrap(); - assert!(block_on(reader.read(codex())).unwrap().is_none()); -} - -#[test] -fn native_read_and_seek_refusals_preserve_the_original_error_mapping() { - use std::{ - error::Error, - io::{Seek, SeekFrom}, - }; - let directory = tempfile::tempdir().unwrap(); - let source = std::fs::read(directory.path()).unwrap_err(); - let read_kind = source.kind(); - let read = read_error(directory.path(), source); - let file = tempfile::NamedTempFile::new().unwrap(); - let mut opened = std::fs::File::open(file.path()).unwrap(); - let source = opened.seek(SeekFrom::Current(i64::MIN)).unwrap_err(); - let seek_kind = source.kind(); - let seek = UsageReadError::seek(file.path(), source); - for (error, kind) in [(read, read_kind), (seek, seek_kind)] { - let clone = error.clone(); - let native = error - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert_eq!(native.kind(), kind); - let shared = clone - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - } -} diff --git a/crates/agent/Cargo.toml b/crates/agent/Cargo.toml index c4897882..16629604 100644 --- a/crates/agent/Cargo.toml +++ b/crates/agent/Cargo.toml @@ -10,12 +10,17 @@ publish = false layer = "feature" [dependencies] +nocterm-hubs-model.workspace = true +nocterm-hub-agents.workspace = true nocterm-ai.workspace = true +nocterm-plugin-model.workspace = true +nocterm-project-model.workspace = true nocterm-agent-chat.workspace = true nocterm-agent-runtime.workspace = true nocterm-agent-directory.workspace = true nocterm-store.workspace = true nocterm-session.workspace = true +nocterm-transfers.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true # Fenced code in chat replies: the languages agents write most. @@ -38,19 +43,24 @@ gpui-kit = { workspace = true, features = [ ] } futures.workspace = true async-channel.workspace = true -serde.workspace = true serde_json.workspace = true base64.workspace = true chrono = { version = "0.4", default-features = false, features = ["clock"] } thiserror.workspace = true +tracing.workspace = true [dev-dependencies] +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-workspace-ports.workspace = true +nocterm-workspace = { workspace = true, features = ["test-support"] } +nocterm-project-model = { workspace = true, features = ["test-support"] } nocterm-agent-chat = { workspace = true, features = ["test-support"] } nocterm-agent-providers.workspace = true nocterm-session = { workspace = true, features = ["test-support"] } gpui-kit = { workspace = true, features = ["test-support"] } nocterm-ai = { workspace = true, features = ["test-support"] } nocterm-agent-runtime = { workspace = true, features = ["test-support"] } +nocterm-transfers = { workspace = true, features = ["test-support"] } tempfile.workspace = true [lints] diff --git a/crates/agent/src/chat_host.rs b/crates/agent/src/chat_host.rs index 29c1d7a9..24312aba 100644 --- a/crates/agent/src/chat_host.rs +++ b/crates/agent/src/chat_host.rs @@ -1,26 +1,48 @@ //! Explicit immutable host collaborators retained by each panel. use crate::chat_deps::ChatDeps; use gpui_kit::WeakEntity; -use nocterm_workspace::Workspace; +use nocterm_workspace::{ProjectSurfaceReceipt, Workspace}; use std::rc::Rc; #[derive(Clone)] pub(crate) struct ChatHost { workspace: WeakEntity, deps: ChatDeps, installed: crate::Installed, + source: ProjectSurfaceReceipt, +} +#[derive(Debug, thiserror::Error)] +pub(crate) enum ChatHostError { + #[error("The account source is not available for a new chat action")] + Account(#[from] nocterm_ai::storage::AgentStorageError), + #[error("The originating root window no longer matches this chat host.")] + WindowMismatch, } impl ChatHost { - pub(crate) fn new(workspace: WeakEntity, installed: crate::Installed) -> Self { - let seat = Rc::new(crate::workspace_seat::WorkspaceSeat::new(workspace.clone())); + pub(crate) fn new( + workspace: WeakEntity, + installed: crate::Installed, + source: ProjectSurfaceReceipt, + ) -> Result { + if source.window().workspace_id() != workspace.entity_id() { + return Err(ChatHostError::WindowMismatch); + } + let runtime = installed + .capture_runtime() + .unwrap_or_else(|_| installed.runtime().unavailable_account()) + .for_scope(source.context()); + let seat = Rc::new( + crate::workspace_seat::WorkspaceSeat::new(workspace.clone(), source) + .with_runtime(runtime.clone()), + ); let sign_in = installed.auth().map(|opener| { Rc::new(crate::workspace_sign_in::WorkspaceSignIn::new( opener, workspace.clone(), )) as Rc }); - Self { + Ok(Self { deps: ChatDeps::new( - installed.runtime().clone(), + runtime, installed.agents().clone(), installed.settings().clone(), installed.metadata(), @@ -29,7 +51,46 @@ impl ChatHost { ), workspace, installed, + source, + }) + } + pub(crate) fn source(&self) -> ProjectSurfaceReceipt { + self.source + } + /// The hub agents this window offers, if the application has hubs. + pub(crate) fn hub_agents(&self) -> Option<&crate::HubAgentsAccess> { + self.installed.hub_agents() + } + + /// The agents the window's new-chat menu offers: the local ones, and in a hub window also + /// that hub's agents. + pub(crate) fn window_agents(&self, cx: &gpui_kit::App) -> nocterm_ai::AvailableAgents { + let hub = self.hub_agents().and_then(|access| access.hub()); + self.agents().available(cx).offered_in(hub) + } + + pub(crate) fn deps_for_source( + &self, + source: ProjectSurfaceReceipt, + ) -> Result { + if source.window().workspace_id() != self.workspace.entity_id() { + return Err(ChatHostError::WindowMismatch); } + let runtime = self + .installed + .capture_runtime()? + .for_scope(source.context()); + Ok(ChatDeps::new( + runtime.clone(), + self.installed.agents().clone(), + self.installed.settings().clone(), + self.installed.metadata(), + Rc::new( + crate::workspace_seat::WorkspaceSeat::new(self.workspace.clone(), source) + .with_runtime(runtime), + ), + self.deps.sign_in().cloned(), + )) } /// The panel's workspace; chats use the seat in [`Self::deps`]. pub(crate) fn workspace(&self) -> &WeakEntity { diff --git a/crates/agent/src/hub_access.rs b/crates/agent/src/hub_access.rs new file mode 100644 index 00000000..31d20be4 --- /dev/null +++ b/crates/agent/src/hub_access.rs @@ -0,0 +1,42 @@ +//! The hub agents a window offers: the shared service and the hub of the window. Only a hub +//! window offers hub agents; the window of this computer shows none of a hub's data. +use gpui_kit::Entity; +use nocterm_hub_agents::HubAgents; +use nocterm_hubs_model::HubId; + +/// What the panel of a window needs of the hubs: the agents service, to follow the hub's chats +/// and agents, and the window's hub. Cheap to clone; the entity is the application's own. +#[derive(Clone)] +pub struct HubAgentsAccess { + agents: Entity, + hub: Option, +} + +impl HubAgentsAccess { + /// The installation-wide access; it offers nothing until bound to the window of a hub. + pub fn new(agents: Entity) -> Self { + Self { agents, hub: None } + } + + /// The access of a window of `hub`: it offers that hub's agents. + pub fn for_hub_window(&self, hub: &HubId) -> Self { + Self { + agents: self.agents.clone(), + hub: Some(hub.clone()), + } + } + + /// The shared service, for the other surfaces that manage the same agents. + pub fn service(&self) -> &Entity { + &self.agents + } + + pub(crate) fn agents(&self) -> &Entity { + &self.agents + } + + /// The hub of the window, if it is a hub window. + pub(crate) fn hub(&self) -> Option<&HubId> { + self.hub.as_ref() + } +} diff --git a/crates/agent/src/image_deps.rs b/crates/agent/src/image_deps.rs index 6a886299..ddb44c3f 100644 --- a/crates/agent/src/image_deps.rs +++ b/crates/agent/src/image_deps.rs @@ -5,12 +5,27 @@ use std::sync::Arc; pub struct ImageDeps { inspector: Arc, thumbnailer: Arc, + available: async_channel::Receiver<()>, + returned: async_channel::Sender<()>, +} +/// Retained by the actual background work, including after a panel disappears. +pub(crate) struct ImageWorkPermit(async_channel::Sender<()>); +impl Drop for ImageWorkPermit { + fn drop(&mut self) { + let _ = self.0.try_send(()); + } } impl ImageDeps { pub fn new(inspector: Arc, thumbnailer: Arc) -> Self { + let (returned, available) = async_channel::bounded(4); + for _ in 0..4 { + returned.try_send(()).expect("new image work slots"); + } Self { inspector, thumbnailer, + available, + returned, } } pub(crate) fn inspector(&self) -> Arc { @@ -19,4 +34,52 @@ impl ImageDeps { pub(crate) fn thumbnailer(&self) -> Arc { self.thumbnailer.clone() } + pub(crate) fn admit_work(&self) -> Option { + self.available.try_recv().ok()?; + Some(ImageWorkPermit(self.returned.clone())) + } + pub(crate) fn work(&self) -> futures::future::BoxFuture<'static, ImageWorkPermit> { + let (available, returned) = (self.available.clone(), self.returned.clone()); + Box::pin(async move { + available.recv().await.expect("retained image work slots"); + ImageWorkPermit(returned) + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn installations_share_a_finite_physical_image_work_budget() { + let codec = Arc::new(nocterm_ai::testing::ScriptedImageCodec::default()); + let deps = ImageDeps::new(codec.clone(), codec); + let other = deps.clone(); + let mut permits = (0..4) + .map(|_| deps.admit_work().unwrap()) + .collect::>(); + assert!(other.admit_work().is_none()); + permits.pop(); + assert!(other.admit_work().is_some()); + } + #[test] + fn queued_decoding_wakes_when_physical_work_finishes_and_cancelled_waiters_release() { + let codec = Arc::new(nocterm_ai::testing::ScriptedImageCodec::default()); + let deps = ImageDeps::new(codec.clone(), codec); + let mut permits = (0..4) + .map(|_| deps.admit_work().unwrap()) + .collect::>(); + futures::executor::block_on(async { + let mut cancelled = deps.work(); + assert!(futures::poll!(&mut cancelled).is_pending()); + drop(cancelled); + let mut waiting = deps.work(); + assert!(futures::poll!(&mut waiting).is_pending()); + permits.pop(); + let released = waiting.await; + assert!(deps.admit_work().is_none()); + drop(released); + assert!(deps.admit_work().is_some()); + }); + } } diff --git a/crates/agent/src/installation.rs b/crates/agent/src/installation.rs index 18672bf5..e44d45d6 100644 --- a/crates/agent/src/installation.rs +++ b/crates/agent/src/installation.rs @@ -5,12 +5,16 @@ use nocterm_agent_directory::AgentsHandle; use nocterm_store::{SettingsHandle, ShutdownCoordinator, StoreError}; use nocterm_ui::{LayoutHandle, UiReady}; use std::{rc::Rc, sync::Arc}; +mod source; +use crate::hub_access::HubAgentsAccess; +use source::SourceBinding; #[derive(Clone)] pub struct Installed { inner: Rc, + source: SourceBinding, + hubs: Option, } struct Installation { - runtime: runtime::RuntimeHandle, agents: AgentsHandle, settings: SettingsHandle, metadata: Arc, @@ -21,7 +25,50 @@ struct Installation { } impl Installed { pub fn runtime(&self) -> &runtime::RuntimeHandle { - &self.inner.runtime + self.source.runtime() + } + pub fn for_environment( + &self, + access: nocterm_ai::local_environment::LocalEnvironmentAccess, + ) -> Result { + Ok(Self { + inner: self.inner.clone(), + hubs: self.hubs.clone(), + source: SourceBinding::fixed(self.runtime().for_environment(access)?), + }) + } + /// A hub window without an authenticated capture provider has no Local environment. + pub fn unavailable_account(&self) -> Self { + Self { + inner: self.inner.clone(), + hubs: self.hubs.clone(), + source: SourceBinding::fixed(self.runtime().unavailable_account()), + } + } + pub fn with_account_source( + &self, + factory: Arc, + ) -> Self { + Self { + inner: self.inner.clone(), + hubs: self.hubs.clone(), + source: SourceBinding::account(self.runtime(), factory), + } + } + /// A window of one hub offers that hub's agents beside the local ones, and lists its chats. + pub fn for_hub_window(&self, hub: &nocterm_hubs_model::HubId) -> Self { + Self { + hubs: self.hubs.as_ref().map(|access| access.for_hub_window(hub)), + ..self.clone() + } + } + pub(crate) fn hub_agents(&self) -> Option<&HubAgentsAccess> { + self.hubs.as_ref() + } + pub(crate) fn capture_runtime( + &self, + ) -> Result { + self.source.capture() } /// The agents available to start, for other surfaces such as settings. pub fn agents(&self) -> &AgentsHandle { @@ -49,18 +96,24 @@ pub(crate) fn install( layout: LayoutHandle, ready: &UiReady, shutdown: &ShutdownCoordinator, + hubs: Option, cx: &mut App, ) -> (Installed, Result<(), StoreError>) { let settings = ready.settings().clone(); - let agents = AgentsHandle::new(services.catalog, settings.clone(), cx); + let mut agents = AgentsHandle::new(services.catalog, settings.clone(), cx); + if let Some(hubs) = &hubs { + let service = hubs.agents().clone(); + agents = agents.with_hub_agents(Rc::new(move |cx| service.read(cx).launches())); + } let runtime = runtime::RuntimeHandle::new( runtime::RuntimeServices { settings: settings.clone(), agents: agents.clone(), connector: services.connector, bridge: services.bridge, + command_jobs: services.command_jobs, + transfers: services.transfers, storage: services.storage, - usage_source: services.usage_source, workdir_resolver: services.workdir_resolver, workdir: services.workdir, terminal_auth: services.terminal_auth.is_some(), @@ -74,8 +127,9 @@ pub(crate) fn install( let registered = shutdown.register_capture(move |cx| captured.capture_shutdown(cx)); ( Installed { + hubs, + source: SourceBinding::fixed(runtime.clone()), inner: Rc::new(Installation { - runtime, agents, settings, metadata: services.metadata, diff --git a/crates/agent/src/installation/source.rs b/crates/agent/src/installation/source.rs new file mode 100644 index 00000000..ac9ced8d --- /dev/null +++ b/crates/agent/src/installation/source.rs @@ -0,0 +1,41 @@ +//! New UI actions capture an account source; existing chats retain their old handle. +use crate::runtime::RuntimeHandle; +use nocterm_ai::{local_environment::LocalEnvironmentAccess, storage::AgentStorageError}; +use nocterm_hubs_model::source_identity::LocalAccountSourceFactory; +use std::sync::Arc; + +#[derive(Clone)] +pub(super) struct SourceBinding { + runtime: RuntimeHandle, + capture: Option>, +} +impl SourceBinding { + pub(super) fn fixed(runtime: RuntimeHandle) -> Self { + Self { + runtime, + capture: None, + } + } + pub(super) fn account( + runtime: &RuntimeHandle, + capture: Arc, + ) -> Self { + // Explicitly unavailable, never a ThisComputer substitute before unlock. + Self { + runtime: runtime.unavailable_account(), + capture: Some(capture), + } + } + pub(super) fn runtime(&self) -> &RuntimeHandle { + &self.runtime + } + pub(super) fn capture(&self) -> Result { + if let Some(capture) = &self.capture { + let access = LocalEnvironmentAccess::capture(capture.capture()?)?; + self.runtime.for_environment(access) + } else { + self.runtime.check_current()?; + Ok(self.runtime.clone()) + } + } +} diff --git a/crates/agent/src/lib.rs b/crates/agent/src/lib.rs index b7840f7d..c20afd05 100644 --- a/crates/agent/src/lib.rs +++ b/crates/agent/src/lib.rs @@ -1,32 +1,47 @@ //! The agent chat panel: views over the chats of `nocterm-agent-chat`. //! Installed retains the actual runtime, available agents, image, layout, settings, metadata -//! and auth collaborators. +//! and auth collaborators. New hub-window actions capture fresh account sources; +//! existing chats keep their original source and are withdrawn when it becomes stale. //! MessageList, ComposerView, HistoryColumn, PanelLayout and MenuView own controls, -//! disposable presentation transitions and their subscription/task lifetimes. +//! disposable presentation transitions and their subscription/task lifetimes. Transcript rows +//! borrow the open AgentThread's transcript into the EntryRenderer. A hub window's chats are +//! ordinary chats of its history (ADR 0148): its new-chat menu offers the hub's agents beside +//! the local ones, and a change of the hub's chats reads the history again. The history also +//! lists, through weak handles, the chats other windows hold (ADR 0152); opening one moves it +//! here while it rests, otherwise its window shows it. //! SignInCards owns secret controls and prompt tokens. PanelNotices owns errors, approval //! identity and cancellable redraw work; PanelBindings retains installation subscriptions. //! Stateless renderers take immutable projections; finite presentation operations return //! typed results to named completions bound to their original view/session authority. //! //! Owns: panel views and presentation state, the panel's ChatRoster, WorkspaceSeat (the -//! ChatWorkspace over the panel's workspace) and WorkspaceSignIn (the SignInTerminal over the +//! ChatWorkspace over an original surface and exact ChatKey) and WorkspaceSignIn (the SignInTerminal over the //! host's TerminalAuthOpener). +//! Image previews retain immutable source identity with a 64 MiB source/PNG/RGBA budget and at most 32 cached entries. +//! Preparation and preview decoding share four physical work permits across windows, retained through actual background work. //! Does not own: chats, sessions and tools (nocterm-agent-chat), ACP transport, connection //! admission, filesystem storage or image decoding. -//! Ports used: AgentConnector, ToolBridge, WorkdirResolver, ConnectionDirectory, +//! Ports used: AgentConnector, ToolBridge, WorkdirResolver, ConnectionDirectory, ChatConnectionOpen, //! ImageInspector and ImageThumbnailer through ImageDeps, and ChatMetadataSource. -//! Ports provided: workspace right panel and TerminalAuthOpener composition callback. +//! Ports provided: the required `agent` system plugin (`plugin::PLUGIN`) with its side panel +//! and the TerminalAuthOpener composition callback. //! Settings section: reads ai; its schema belongs to nocterm-ai. //! Globals: none. //! Invariants: preview cache generations reject stale same-key delivery after clear or chat -//! changes; archive delivery reaches the panel only while the chat it opened is current. +//! changes. Committed outer-surface changes select one scoped roster; async delivery keeps +//! its original owner. Accepted hidden forks finish in that original collection without +//! changing the visible chat. Ordinary terminal operations retain the original surface; +//! explicitly attached saved servers open into that same terminal collection. mod agent_icon; mod chat_host; +mod hub_access; +pub use hub_access::HubAgentsAccess; mod image_deps; mod installation; pub use installation::Installed; mod panel; +pub mod plugin; pub use image_deps::ImageDeps; use nocterm_agent_runtime as runtime; mod workspace_seat; @@ -59,9 +74,9 @@ pub struct AgentServices { pub metadata: Arc, pub connector: Arc, pub bridge: Arc, + pub command_jobs: Arc, + pub transfers: Arc, pub storage: runtime::AgentStorage, - /// The host's account usage capability, without native paths in the view. - pub usage_source: Arc, pub workdir_resolver: Arc, pub workdir: PathBuf, pub terminal_auth: Option, @@ -81,10 +96,11 @@ pub fn init( layout: nocterm_ui::LayoutHandle, ready: &nocterm_ui::UiReady, shutdown: &nocterm_store::ShutdownCoordinator, + hubs: Option, cx: &mut App, ) -> (Installed, Result<(), nocterm_store::StoreError>) { panel::commands::init(cx); - installation::install(services, images, layout, ready, shutdown, cx) + installation::install(services, images, layout, ready, shutdown, hubs, cx) } gpui_kit::actions!( @@ -96,24 +112,39 @@ gpui_kit::actions!( ShowHistory, StopGeneration, ToggleModelPicker, - AttachImage + /// Attach files to the next message; images among them are sent as images. + AttachFiles ] ); /// Installs a window-local panel with an initially empty chat history. +/// +/// A window whose chat context cannot be bound gets no panel; that is logged, not an error. pub fn register( workspace: &mut nocterm_workspace::Workspace, installed: Installed, window: &mut gpui_kit::Window, cx: &mut gpui_kit::Context, -) { +) -> Result<(), nocterm_workspace::RegistrationError> { let owner = cx.entity().downgrade(); - let host = chat_host::ChatHost::new(owner, installed); + let source = workspace.context_source(cx); + let host = match chat_host::ChatHost::new(owner, installed, source) { + Ok(host) => host, + Err(error) => { + tracing::warn!(%error, "could not bind agent panel context"); + return Ok(()); + } + }; let enabled = host .settings() .setting::(cx) .enabled; let panel = cx.new(|panel_cx| panel::AgentPanel::new(host, window, panel_cx)); - workspace.set_right_panel(panel, window, cx); - workspace.set_right_panel_available(enabled, window, cx); + workspace.register_panel( + nocterm_workspace::PanelRegistration::with_entity(&plugin::CHATS, panel).expandable(), + window, + cx, + )?; + workspace.set_panel_available(&plugin::CHATS.id, enabled, window, cx); + Ok(()) } diff --git a/crates/agent/src/panel.rs b/crates/agent/src/panel.rs index df719aa1..5a5811b4 100644 --- a/crates/agent/src/panel.rs +++ b/crates/agent/src/panel.rs @@ -1,28 +1,31 @@ +use crate::chat_host::ChatHostError; use crate::thread::AgentThread; +use account_notice::AccountNotice; use gpui_kit::{ AnyElement, App, Context, Entity, EntityId, EventEmitter, FocusHandle, Focusable, SharedString, - Subscription, TestSupportExt as _, Window, - component::{ActiveTheme as _, v_flex}, - div, - prelude::*, + Subscription, TestSupportExt as _, Window, component::v_flex, div, prelude::*, }; use nocterm_ai::model::controls::ConfigCategory; +use nocterm_plugin_model::Side; use nocterm_ui::IconName; -use nocterm_workspace::{Panel, RightPanel, RightPanelEvent, WorkspaceEvent}; +use nocterm_workspace::{ExpandablePanel, Panel, PanelRequest, WorkspaceEvent}; +mod account_notice; mod approvals; mod attach; +mod attach_files; mod attachments; mod bindings; mod chat; +mod chat_servers; pub(crate) mod commands; mod composer; +mod composer_frame; mod composer_renderer; mod composer_view; mod entries; mod history; mod history_column; -mod image_picker; mod image_pipeline; mod image_preparation; mod images; @@ -32,8 +35,11 @@ mod menu; mod menu_view; mod message_actions; mod message_list; +mod message_markdown; mod navigation; mod notices; +mod other_windows; +mod parts; mod preview_operation; mod queue; mod queue_image_preparation; @@ -68,8 +74,9 @@ pub(crate) struct AgentPanel { layout: layout::PanelLayout, menus: menu_view::MenuView, settings: nocterm_store::SettingsHandle, + source: nocterm_workspace::ProjectSurfaceReceipt, } -impl EventEmitter for AgentPanel {} +impl EventEmitter for AgentPanel {} #[cfg(test)] mod tests; @@ -89,6 +96,10 @@ impl AgentPanel { let layout = layout::PanelLayout::new(layout, cx); let messages = message_list::MessageList::new(images, cx); let subscriptions = Self::bind_panel(&host, &settings, window, cx); + // Whatever the panel redraws for, its cached regions redraw too; thread changes + // and typing redraw only their regions instead of notifying the panel. + cx.observe_self(|panel, cx| panel.layout.parts().invalidate(cx)) + .detach(); let panel = cx.weak_entity(); window.defer(cx, move |window, cx| { let _ = panel.update(cx, |panel, cx| panel.on_runtime_changed(window, cx)); @@ -96,6 +107,7 @@ impl AgentPanel { Self { focus: cx.focus_handle(), chats: crate::roster::ChatRoster::new(host.deps().clone()), + source: host.source(), host, notices: notices::PanelNotices::default(), _bindings: bindings::PanelBindings::new(subscriptions), @@ -132,13 +144,121 @@ impl AgentPanel { let _ = panel.update(cx, |panel, cx| panel.on_runtime_changed(window, cx)); }); }); - [Some(settings), workspace, Some(runtime)] - .into_iter() - .flatten() - .collect() + let panel = cx.weak_entity(); + let holders = host.runtime().observe_holders(cx, move |event, cx| { + let event = *event; + let _ = cx.update_window(originating_window, |_, window, cx| { + let _ = panel.update(cx, |panel, cx| panel.on_holder_event(event, window, cx)); + }); + }); + let hubs = host.hub_agents().map(|access| { + let hub = access.hub().cloned(); + cx.subscribe(access.agents(), move |panel, _, event, cx| { + panel.on_hub_agents_event(hub.as_ref(), event, cx); + }) + }); + [ + Some(settings), + workspace, + Some(runtime), + Some(holders), + hubs, + ] + .into_iter() + .flatten() + .collect() } fn on_runtime_changed(&mut self, window: &mut Window, cx: &mut Context) { + let retired = self.chats.retire_stale_sources(cx); + let removed = !retired.removed().is_empty(); + if let Some(error) = retired.into_error() { + self.notices.set_error(Some(error.to_string())); + } + let deps = self.host.deps_for_source(self.source); + let account_notice = match &deps { + Ok(_) => None, + Err(ChatHostError::Account(error)) => AccountNotice::from_capture(error), + Err(ChatHostError::WindowMismatch) => None, + }; + self.notices.set_account(account_notice); + let switched = deps + .ok() + .is_some_and(|deps| self.chats.switch_context(deps, cx)); + if switched || removed { + self.composer.clear(); + self.history.switch_context(window, cx); + self.sign_ins.clear(); + self.reset_chat_view(cx); + } + self.chats.release_quiet_hidden(cx); + if !self.settings.setting::(cx).enabled { + return; + } + if account_notice.is_some() { + cx.notify(); + return; + } + self.load_history(cx); self.adopt_saved_chats(window, cx); + self.refresh_other_windows(cx); + cx.notify(); + } + /// Asks for the history of the captured context; a failure of the account becomes the + /// account notice, any other the error line. + fn load_history(&mut self, cx: &mut Context) { + if let Err(error) = self.chats.host().runtime().ensure_history(cx) { + match AccountNotice::from_catalog(&error) { + Some(notice) => self.notices.set_account(Some(notice)), + None => self.notices.set_error(Some(error.to_string())), + } + } + } + fn select_context( + &mut self, + source: nocterm_workspace::ProjectSurfaceReceipt, + window: &mut Window, + cx: &mut Context, + ) { + if source == self.source { + return; + } + let deps = match self.host.deps_for_source(source) { + Ok(deps) => deps, + Err(error) => { + match &error { + ChatHostError::Account(cause) => match AccountNotice::from_capture(cause) { + Some(notice) => self.notices.set_account(Some(notice)), + None => self.notices.set_error(Some(error.to_string())), + }, + ChatHostError::WindowMismatch => { + self.notices.set_error(Some(error.to_string())); + } + } + cx.notify(); + return; + } + }; + self.notices.set_account(None); + self.leave_composer(cx); + self.chats.switch_context(deps, cx); + self.source = source; + self.composer.clear(); + self.history.switch_context(window, cx); + self.sign_ins.clear(); + self.notices.set_error(None); + self.reset_chat_view(cx); + if let Some(thread) = self.current() { + let binding = self.composer.bind_loading(thread.entity_id(), cx); + let archived = thread.read(cx).is_archived(); + let draft = thread.read(cx).draft_text().unwrap_or_default().to_owned(); + self.composer + .finish_loading(&binding, draft, archived, window, cx); + } else { + self.composer.set_text("", window, cx); + } + self.load_history(cx); + self.adopt_saved_chats(window, cx); + self.sync_approval_attention(window, cx); cx.notify(); } fn on_workspace_event( @@ -147,14 +267,22 @@ impl AgentPanel { window: &mut Window, cx: &mut Context, ) { - if event == WorkspaceEvent::RightPanelVisibilityChanged { + if event == WorkspaceEvent::ContextChanged { + // This synchronous committed event captures the original source once. + // Async history/runtime callbacks never consult current UI selection. + if let Some(workspace) = self.host.workspace().upgrade() { + let source = workspace.read(cx).context_source(cx); + self.select_context(source, window, cx); + } + } + if event == WorkspaceEvent::PanelVisibilityChanged { let visible = self.history_is_visible(cx); self.history.set_visible(visible, cx); if !self .host .workspace() .upgrade() - .is_some_and(|workspace| workspace.read(cx).right_panel_is_open()) + .is_some_and(|workspace| workspace.read(cx).panel_is_open(&crate::plugin::CHATS.id)) { self.sync_composer_draft(cx); if let Some(thread) = self.current() { @@ -173,7 +301,7 @@ impl AgentPanel { let workspace = self.host.workspace().clone(); window.defer(cx, move |window, cx| { let _ = workspace.update(cx, |workspace, cx| { - workspace.set_right_panel_available(enabled, window, cx) + workspace.set_panel_available(&crate::plugin::CHATS.id, enabled, window, cx) }); }); cx.notify(); @@ -195,12 +323,12 @@ impl AgentPanel { } fn history_is_visible(&self, cx: &App) -> bool { self.layout.history() - && !self.chats.is_empty() + && (!self.chats.is_empty() || !self.history.others().listed(cx).is_empty()) && self .host .workspace() .upgrade() - .is_some_and(|workspace| workspace.read(cx).right_panel_is_open()) + .is_some_and(|workspace| workspace.read(cx).panel_is_open(&crate::plugin::CHATS.id)) } fn on_history_split_settling( &mut self, @@ -235,6 +363,23 @@ impl AgentPanel { pub(crate) fn current(&self) -> Option> { self.chats.current() } + /// The hub's agents or chats changed: a hub window lists the hub's chats again and offers + /// the agents it lists now. + fn on_hub_agents_event( + &mut self, + window_hub: Option<&nocterm_hubs_model::HubId>, + event: &nocterm_hub_agents::HubAgentsEvent, + cx: &mut Context, + ) { + use nocterm_hub_agents::HubAgentsEvent; + match event { + HubAgentsEvent::ChatsChanged(hub) if Some(hub) == window_hub => { + self.host.runtime().reload_history(cx); + } + HubAgentsEvent::Changed(hub) if Some(hub) == window_hub => cx.notify(), + _ => {} + } + } #[cfg(test)] pub(crate) fn chat_documents(&self) -> impl ExactSizeIterator> { self.chats.iter() @@ -251,7 +396,7 @@ impl AgentPanel { /// The agent `NewThreadWithLastAgent` starts: the last one used, else /// the default, else the first enabled one. fn last_agent(&self, cx: &App) -> Option { - let agents = self.host.agents().available(cx); + let agents = self.host.window_agents(cx); let known = |id: &&String| agents.get(id).is_some(); self.host .runtime() @@ -269,8 +414,9 @@ impl AgentPanel { .or_else(|| agents.iter().next().map(|agent| agent.id().to_owned())) } pub(super) fn set_history(&mut self, open: bool, cx: &mut Context) { - if let Some(width) = self.layout.set_history(open, self.current().is_some(), cx) { - cx.emit(RightPanelEvent::Widen(width)); + let chat = self.current().is_some(); + if let Some(width) = self.layout.set_history(open, chat, cx) { + cx.emit(PanelRequest::Widen(width)); } cx.notify(); } @@ -280,18 +426,52 @@ impl AgentPanel { window: &mut Window, cx: &mut Context, ) -> AnyElement { - let history = history::HistoryProjection::render( - &self.chats, - &self.history, - self.host.agents(), - self.host.timestamp(), - cx, - ); + let history = self.layout.parts().history().into_any_element(); self.layout.render_split(chat, history, window, cx) } + /// Draws one region of the panel for its view in [`parts`]. + fn render_part( + &mut self, + kind: parts::PartKind, + window: &mut Window, + cx: &mut Context, + ) -> AnyElement { + match kind { + parts::PartKind::Transcript => self.render_transcript_part(cx), + parts::PartKind::History => { + let stamp = history::HistoryProjection::stamp(&self.chats, &self.history, cx); + self.layout.parts_mut().drew_history(stamp); + let hub_window = self + .host + .hub_agents() + .is_some_and(|access| access.hub().is_some()); + let history = history::HistoryProjection::render( + &self.chats, + &self.history, + (self.host.agents(), hub_window), + self.host.timestamp(), + cx, + ); + div() + .size_full() + .flex() + .flex_col() + .child(history) + .into_any_element() + } + parts::PartKind::Footer => self.render_footer(window, cx), + } + } fn toggle_history(&mut self, cx: &mut Context) { self.set_history(!self.layout.history(), cx); } + /// Lists the chats of `side` in the history of a hub window. + fn show_history_side(&mut self, side: history_column::HistorySide, cx: &mut Context) { + if self.history.show_side(side) { + self.layout.parts().invalidate(cx); + cx.notify(); + } + } fn new_thread_with_last(&mut self, window: &mut Window, cx: &mut Context) { if let Some(id) = self.last_agent(cx) { self.new_thread(id, window, cx); @@ -318,19 +498,22 @@ impl AgentPanel { impl AgentPanel { pub(super) fn render_menu(&mut self, window: &Window, cx: &mut Context) -> AnyElement { let kind = self.menus.current().cloned(); - let attachment = matches!(kind, Some(MenuKind::Context)).then(|| { - attach::AttachmentMenuRenderer::render_attach_menu( - self.current(), + let attachment = matches!(kind, Some(MenuKind::Context)).then(|| match self.current() { + Some(thread) if chat_servers::takes_servers(thread.read(cx)) => { + chat_servers::ChatServersRenderer::render_menu(&thread, cx) + } + thread => attach::AttachmentMenuRenderer::render_attach_menu( + thread, self.host.workspace().clone(), cx, - ) + ), }); let frame = menu::MenuFrame::new( kind, self.current(), self.menus.query_input(), self.menus.query(cx).to_lowercase(), - self.host.agents().available(cx), + self.host.window_agents(cx), self.last_agent(cx), self.focus.clone(), self.host.runtime().favorites(cx).clone(), @@ -362,15 +545,21 @@ impl Panel for AgentPanel { fn icon(&self, _: &App) -> IconName { IconName::Bot } + fn attention(&self, cx: &App) -> Option { + self.chats + .iter() + .any(|thread| thread.read(cx).has_pending_approvals()) + .then(|| "An agent needs permission to continue.".into()) + } } -impl RightPanel for AgentPanel { - fn set_maximized(&mut self, maximized: bool, cx: &mut Context) { - self.layout.set_maximized(maximized); +impl ExpandablePanel for AgentPanel { + fn set_expanded(&mut self, expanded: bool, cx: &mut Context) { + self.layout.set_maximized(expanded); cx.notify(); } - fn set_docked_left(&mut self, left: bool, cx: &mut Context) { - self.layout.set_docked_left(left); + fn set_side(&mut self, side: Side, cx: &mut Context) { + self.layout.set_docked_left(side == Side::Left); cx.notify(); } } @@ -392,20 +581,25 @@ impl Render for AgentPanel { cx, ); let mut body = shell::ShellRenderer::body(&self.focus, cx).child(header); - body = match current { - None => body.child(history::HistoryProjection::render( - &self.chats, - &self.history, - self.host.agents(), - self.host.timestamp(), - cx, - )), - Some(thread) if self.layout.history() => { - let chat = self.render_chat(thread, window, cx); + body = if current.is_none() { + body.child(self.layout.parts().history()) + } else { + let chat = v_flex() + .size_full() + .min_w_0() + .min_h_0() + .child(self.layout.parts().transcript()) + .child(self.layout.parts().footer()) + .into_any_element(); + if self.layout.history() { body.child(self.render_split(chat, window, cx)) + } else { + body.child(chat) } - Some(thread) => body.child(self.render_chat(thread, window, cx)), }; + if let Some(notice) = self.notices.account() { + body = body.child(notice.strip(cx)); + } if let Some(error) = self.notices.error() { body = body.child( shell::ShellRenderer::error(error.to_owned(), cx) diff --git a/crates/agent/src/panel/account_notice.rs b/crates/agent/src/panel/account_notice.rs new file mode 100644 index 00000000..1c22fbaf --- /dev/null +++ b/crates/agent/src/panel/account_notice.rs @@ -0,0 +1,99 @@ +//! What the state of the hub account behind this window's chats lets the person do, and the +//! strip that says why the chats are unavailable. +use gpui_kit::{ + App, ClickEvent, IntoElement, TestSupportExt as _, Window, + component::{ActiveTheme as _, Sizable as _, button::Button, h_flex}, + div, + prelude::*, +}; +use nocterm_ai::storage::AgentStorageError; +use nocterm_hubs_model::HubError; +use nocterm_workspace::{UnlockVault, request_sign_in}; + +use nocterm_agent_runtime::CatalogLoadError; + +/// Why the account of this window cannot serve chats right now. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum AccountNotice { + /// The keys of the account are closed. + Locked, + /// The account is signed out on this device. + SignedOut, + /// The account the window captured is gone or cannot be identified. + Unavailable, +} + +impl AccountNotice { + /// The notice for a failure to capture the account, if the failure is about the account. + pub(super) fn from_capture(error: &AgentStorageError) -> Option { + match error { + AgentStorageError::AccountCapture(HubError::KeysLocked) => Some(Self::Locked), + AgentStorageError::AccountCapture(HubError::NotSignedIn | HubError::Revoked) => { + Some(Self::SignedOut) + } + AgentStorageError::AccountCapture(_) | AgentStorageError::AccountSource(_) => { + Some(Self::Unavailable) + } + _ => None, + } + } + + /// The notice for a failed history load, if the failure is about the account. + pub(super) fn from_catalog(error: &CatalogLoadError) -> Option { + match error { + CatalogLoadError::AccountSource(_) => Some(Self::Unavailable), + CatalogLoadError::Storage(error) => Self::from_capture(error), + _ => None, + } + } + + fn message(self) -> &'static str { + match self { + Self::Locked => "Hub account locked — unlock it to see its chats", + Self::SignedOut => "Signed out of the hub — sign in to see its chats", + Self::Unavailable => "Hub account unavailable — unlock it or sign in again", + } + } + + fn remedy(self) -> &'static str { + match self { + Self::SignedOut => "Sign in", + Self::Locked | Self::Unavailable => "Unlock", + } + } + + fn resolve(self, window: &mut Window, cx: &mut App) { + match self { + Self::Locked | Self::Unavailable => { + window.dispatch_action(Box::new(UnlockVault), cx); + } + Self::SignedOut => { + request_sign_in(window, cx); + } + } + } + + /// The strip shown above the chats. + pub(super) fn strip(self, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + h_flex() + .id("agent-account-notice") + .test_support() + .gap_2() + .mx_2() + .my_1() + .px_2() + .py_1() + .rounded(theme.radius) + .bg(theme.muted) + .text_xs() + .text_color(theme.muted_foreground) + .child(div().flex_1().min_w_0().child(self.message())) + .child( + Button::new("agent-account-action") + .xsmall() + .label(self.remedy()) + .on_click(move |_: &ClickEvent, window, cx| self.resolve(window, cx)), + ) + } +} diff --git a/crates/agent/src/panel/approvals.rs b/crates/agent/src/panel/approvals.rs index bda0992d..eaab029d 100644 --- a/crates/agent/src/panel/approvals.rs +++ b/crates/agent/src/panel/approvals.rs @@ -13,12 +13,20 @@ use gpui_kit::{ prelude::*, px, }; -use nocterm_ai::TerminalCall; +use nocterm_ai::{TerminalCall, TransferFiles}; use nocterm_ui::IconName; use nocterm_workspace::UnlockVault; use super::{AgentPanel, widgets::menu_variant}; -use crate::thread::{AgentThread, PermissionCard, ToolApprovalCard}; +use crate::thread::{AgentThread, PermissionCard}; + +/// What a person answers a terminal tool call that waits for approval. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum ToolApprovalChoice { + Deny, + AllowOnce, + AllowForSession, +} fn sign_in_text(wait: &crate::thread::SignInWait) -> (String, String) { let heading = if wait.vault() { @@ -54,6 +62,45 @@ fn card(cx: &Context) -> gpui_kit::Div { pub(super) struct ApprovalCardsRenderer; impl ApprovalCardsRenderer { + pub(super) fn list( + cards: Vec, + window: &gpui_kit::Window, + cx: &Context, + ) -> Option { + let pending = cards.len(); + if pending == 0 { + return None; + } + Some( + v_flex() + .id("agent-approvals") + .test_support() + .w_full() + .min_w_0() + .flex_shrink_0() + .px_3() + .gap_1() + .child( + div() + .text_xs() + .text_color(cx.theme().warning) + .child(format!("Waiting for you · {pending}")), + ) + .child( + v_flex() + .id("agent-approval-list") + .test_support() + .w_full() + .min_w_0() + .min_h_0() + .max_h(window.viewport_size().height * 0.35) + .overflow_y_scroll() + .gap_2() + .children(cards), + ) + .into_any_element(), + ) + } /// A background session asks for a secret. The user types it here, or /// unlocks the vault when a saved secret answers it; the session never /// becomes a tab. @@ -192,60 +239,72 @@ impl ApprovalCardsRenderer { .child(actions) .into_any_element() } + /// The card of tool call `token` of `thread`, while it waits for approval. pub(super) fn tool_card( thread: &Entity, - approval: ToolApprovalCard, - target: String, - reusable: bool, + token: crate::thread::ApprovalToken, index: usize, cx: &mut Context, - ) -> AnyElement { - let token = approval.token(); - let call = approval.call().clone(); - let owner = thread.downgrade(); + ) -> Option { + let call = thread.read(cx).tool_approval_call(token)?.clone(); + let (target, reusable) = thread.update(cx, |thread, cx| { + ( + thread.describe_target(&call, cx), + !thread.unsafe_live_input(&call, cx), + ) + }); let (heading, exact, grant) = Self::tool_text(&call, &target); let exact = nocterm_ai::redact::redact(&exact); - card(cx) - .child(div().min_w_0().truncate().text_sm().child(heading)) - .child( - h_flex().w_full().min_w_0().flex_wrap().gap_1().children( - [ - (false, false, "Deny"), - (true, false, "Allow once"), - (true, true, grant), - ] - .into_iter() - .filter(|(_, grant, _)| !grant || reusable) - .map(|(allow, grant, label)| { - let owner = owner.clone(); - Button::new((SharedString::from(format!("tool-{label}")), index)) - .custom(menu_variant(cx)) - .small() - .max_w_full() - .label(label) - .on_click(cx.listener(move |_, _, _, cx| { - if let Some(thread) = owner.upgrade() { - thread.update(cx, |thread, cx| { - thread.approve_tool(token, allow, grant, cx) + let weak = thread.downgrade(); + Some( + card(cx) + .child(div().min_w_0().truncate().text_sm().child(heading)) + .child( + h_flex().w_full().min_w_0().flex_wrap().gap_1().children( + [ + (ToolApprovalChoice::Deny, false, "Deny"), + (ToolApprovalChoice::AllowOnce, false, "Allow once"), + (ToolApprovalChoice::AllowForSession, true, grant), + ] + .into_iter() + .filter(|(_, grant, _)| !grant || reusable) + .map(|(choice, _, label)| { + let thread = weak.clone(); + Button::new((SharedString::from(format!("tool-{label}")), index)) + .custom(menu_variant(cx)) + .small() + .max_w_full() + .label(label) + .on_click(cx.listener(move |_, _, _, cx| { + let _ = thread.update(cx, |thread, cx| { + if thread.tool_approval_call(token).is_some() { + thread.approve_tool( + token, + choice != ToolApprovalChoice::Deny, + choice == ToolApprovalChoice::AllowForSession + && reusable, + cx, + ); + } }); - } - })) - }), - ), - ) - .child( - div() - .id(("approval-command", index)) - .test_support() - .w_full() - .min_w_0() - .max_h(px(64.)) - .overflow_x_scroll() - .overflow_y_scroll() - .text_xs() - .child(exact), - ) - .into_any_element() + })) + }), + ), + ) + .child( + div() + .id(("approval-command", index)) + .test_support() + .w_full() + .min_w_0() + .max_h(px(64.)) + .overflow_x_scroll() + .overflow_y_scroll() + .text_xs() + .child(exact), + ) + .into_any_element(), + ) } fn tool_text(call: &TerminalCall, target: &str) -> (&'static str, String, &'static str) { match call { @@ -300,6 +359,109 @@ impl ApprovalCardsRenderer { "Allow for this server", ), TerminalCall::ListTerminals => ("List terminals", String::new(), "Allow"), + TerminalCall::TransferFiles(request) => transfer_text(request, target), + TerminalCall::ReadTransfer(request) => ( + "Read transfer", + request.transfer_id.clone(), + "Allow reading", + ), + TerminalCall::CancelTransfer(request) => ( + "Cancel transfer", + request.transfer_id.clone(), + "Allow cancelling", + ), + TerminalCall::ListFiles(_) + | TerminalCall::ReadFile(_) + | TerminalCall::WriteFile(_) + | TerminalCall::EditFile(_) + | TerminalCall::MakeDirectory(_) + | TerminalCall::DeletePath(_) + | TerminalCall::MovePath(_) => file_text(call, target), + } + } +} + +/// The most of a file's new content or an edit's text a card shows. +const SHOWN: usize = 4000; + +fn shown(text: &str) -> String { + match text.char_indices().nth(SHOWN) { + Some((end, _)) => format!("{}\n…", &text[..end]), + None => text.to_owned(), + } +} + +/// A file call on the terminal `target`: the path, and for a change what it writes. +fn file_text(call: &TerminalCall, target: &str) -> (&'static str, String, &'static str) { + const READ: &str = "Allow reading on this terminal"; + const CHANGE: &str = "Allow file changes on this terminal"; + match call { + TerminalCall::ListFiles(request) => { + ("List files", format!("{target}: {}", request.path), READ) + } + TerminalCall::ReadFile(request) => { + ("Read file", format!("{target}: {}", request.path), READ) } + TerminalCall::WriteFile(request) => ( + if request.overwrite { + "Replace file" + } else { + "Create file" + }, + format!( + "{target}: {}\n{} bytes\n\n{}", + request.path, + request.content.len(), + shown(&request.content) + ), + CHANGE, + ), + TerminalCall::EditFile(request) => ( + "Edit file", + format!( + "{target}: {}{}\n\nReplace:\n{}\n\nWith:\n{}", + request.path, + if request.replace_all { + " (every occurrence)" + } else { + "" + }, + shown(&request.old_text), + shown(&request.new_text) + ), + CHANGE, + ), + TerminalCall::MakeDirectory(request) => ( + "Create folder", + format!("{target}: {}", request.path), + CHANGE, + ), + TerminalCall::DeletePath(request) => { + ("Delete", format!("{target}: {}", request.path), CHANGE) + } + TerminalCall::MovePath(request) => ( + "Move", + format!("{target}: {} → {}", request.from, request.to), + CHANGE, + ), + _ => ("File", target.to_owned(), CHANGE), } } + +/// `target` names the source and destination terminals of the transfer. +fn transfer_text(request: &TransferFiles, target: &str) -> (&'static str, String, &'static str) { + let sources: String = request + .sources + .iter() + .map(|source| format!("\n {source}")) + .collect(); + let existing = if request.overwrite { "replace" } else { "skip" }; + ( + "Transfer files", + format!( + "{target}\nSources:{sources}\nInto: {}\nExisting files: {existing}", + request.destination + ), + "Allow transfers between these terminals", + ) +} diff --git a/crates/agent/src/panel/attach.rs b/crates/agent/src/panel/attach.rs index c3c81f8d..cf393e26 100644 --- a/crates/agent/src/panel/attach.rs +++ b/crates/agent/src/panel/attach.rs @@ -10,7 +10,7 @@ use std::rc::Rc; use gpui_kit::{ AnyElement, Context, Entity, SharedString, TestSupportExt as _, component::{ - ActiveTheme as _, Selectable as _, Sizable as _, StyledExt as _, + ActiveTheme as _, Disableable as _, Selectable as _, Sizable as _, StyledExt as _, button::{Button, ButtonVariants as _}, h_flex, v_flex, }, @@ -18,6 +18,7 @@ use gpui_kit::{ prelude::*, px, }; +use nocterm_agent_chat::seat::Seat; use nocterm_ui::IconName; use nocterm_workspace::{ConnectionDirectory, ConnectionSummary, TerminalEntry, TerminalStatus}; @@ -79,12 +80,23 @@ impl AttachmentMenuRenderer { let (Some(thread), Some(workspace)) = (thread, workspace.upgrade()) else { return div().into_any_element(); }; - let directory = workspace.read(cx).connection_directory(); - let summaries = directory - .as_ref() - .map(|directory| directory.connections(cx)) - .unwrap_or_default(); - let terminals = workspace.read(cx).terminals(cx); + let seat = thread.read(cx).workspace_seat().clone(); + let summaries = seat.connections(cx); + let terminals = seat.terminals(cx).unwrap_or_default(); + let opening = if thread.read(cx).document().key().scope() + == nocterm_ai::history::ContextId::General + { + workspace + .read(cx) + .connection_directory() + .map(|directory| SavedServerOpen::General { + directory, + workspace: workspace.downgrade(), + }) + } else { + seat.opens_connections(cx) + .then(|| SavedServerOpen::Workspace(seat.clone())) + }; let mut menu = v_flex() .w_full() .gap(px(2.)) @@ -97,13 +109,11 @@ impl AttachmentMenuRenderer { entry, &summaries, &thread, - workspace.downgrade(), + seat.clone(), cx, )); } - if let Some(directory) = directory - && !summaries.is_empty() - { + if !summaries.is_empty() { menu = menu.child(heading("Saved servers", cx)); for row in servers::server_rows(&summaries) { menu = menu.child(match row { @@ -115,15 +125,7 @@ impl AttachmentMenuRenderer { && info.status != TerminalStatus::Closed }) }); - Self::server_row( - summary, - nested, - online, - &thread, - &directory, - workspace.downgrade(), - cx, - ) + Self::server_row(summary, nested, online, &thread, opening.clone(), cx) } }); } @@ -138,7 +140,7 @@ impl AttachmentMenuRenderer { entry: &TerminalEntry, summaries: &[ConnectionSummary], thread: &Entity, - workspace: gpui_kit::WeakEntity, + seat: Seat, cx: &mut Context, ) -> AnyElement { let origin = thread.entity_id(); @@ -182,7 +184,7 @@ impl AttachmentMenuRenderer { }); let mut line = h_flex().w_full().gap_1().child(row); if entry.background { - let workspace = workspace.clone(); + let seat = seat.clone(); line = line.child( Button::new(SharedString::from(format!("show-background-{item:?}"))) .custom(menu_variant(cx)) @@ -191,12 +193,8 @@ impl AttachmentMenuRenderer { .tooltip("Opened by an agent in the background. Show it in a tab.") .on_click(cx.listener(move |this, _, window, cx| { this.close_menus(cx); - let workspace = workspace.clone(); - window.defer(cx, move |window, cx| { - let _ = workspace.update(cx, |workspace, cx| { - workspace.show_background_session(item, window, cx) - }); - }); + let seat = seat.clone(); + window.defer(cx, move |window, cx| seat.show_background(item, window, cx)); cx.notify(); })), ); @@ -242,8 +240,7 @@ impl AttachmentMenuRenderer { nested: bool, online: bool, thread: &Entity, - directory: &Rc, - workspace: gpui_kit::WeakEntity, + opening: Option, cx: &mut Context, ) -> AnyElement { let origin = thread.entity_id(); @@ -256,8 +253,7 @@ impl AttachmentMenuRenderer { let covering = covering(thread.read(cx), summary, &attachment); let selected = explicit || covering.is_some(); let id = summary.id.to_string(); - let directory = directory.clone(); - let workspace = workspace.clone(); + // Indented with padding, not margin: a margin beside a full width // pushed the open button past the menu's edge. let line = h_flex() @@ -301,18 +297,20 @@ impl AttachmentMenuRenderer { .custom(menu_variant(cx)) .small() .flex_shrink_0() + .disabled(opening.is_none()) .icon(IconName::ArrowUpRight) .accessibility_label(format!("Open {} in a new tab", summary.name)) .tooltip("Open in a new tab") .on_click(cx.listener(move |this, _, window, cx| { this.close_menus(cx); - let directory = directory.clone(); - let workspace = workspace.clone(); + let opening = opening.clone(); let id = id.clone(); // Opening updates the workspace, which may be // rendering this panel: do it once that is over. window.defer(cx, move |window, cx| { - directory.open(&id, &workspace, window, cx); + if let Some(opening) = opening { + opening.open(&id, window, cx); + } }); cx.notify(); })), @@ -326,3 +324,30 @@ impl AttachmentMenuRenderer { } pub(super) struct AttachmentMenuRenderer; + +/// The arrow retains the original destination; General keeps its recent-use path. +#[derive(Clone)] +enum SavedServerOpen { + General { + directory: Rc, + workspace: gpui_kit::WeakEntity, + }, + Workspace(Seat), +} +impl SavedServerOpen { + fn open(&self, profile: &str, window: &mut gpui_kit::Window, cx: &mut gpui_kit::App) { + match self { + Self::General { + directory, + workspace, + } => { + directory.open(profile, workspace, window, cx); + } + Self::Workspace(seat) => { + if let Some(item) = seat.open_background(profile, window, cx) { + seat.show_background(item, window, cx); + } + } + } + } +} diff --git a/crates/agent/src/panel/attach_files.rs b/crates/agent/src/panel/attach_files.rs new file mode 100644 index 00000000..b5aee4f0 --- /dev/null +++ b/crates/agent/src/panel/attach_files.rs @@ -0,0 +1,37 @@ +//! The paperclip: files of any kind for the next message. Images among them are attached as +//! images the agent sees; every other file as a link to where it is on this computer. +use nocterm_ai::prompt_files::{PromptFile, PromptFileError, is_image}; +use std::path::PathBuf; + +/// A pick delivered only to the chat and composer revision it was started for. +pub(super) struct AttachPick { + chat: Option, + revision: u64, +} + +impl AttachPick { + pub(super) fn new(chat: Option, revision: u64) -> Self { + Self { chat, revision } + } + pub(super) fn current(&self, chat: Option, revision: u64) -> bool { + self.chat == chat && self.revision == revision + } +} + +/// `paths` split into the images (when the agent takes images) and the other files. +pub(super) fn split(paths: Vec, images_taken: bool) -> (Vec, Vec) { + paths + .into_iter() + .partition(|path| images_taken && is_image(path)) +} + +/// Links to `files` with their sizes. Reads file metadata: run it off the UI thread. +pub(super) fn link(files: &[PathBuf]) -> Result, PromptFileError> { + files + .iter() + .map(|path| { + let size = std::fs::metadata(path).map_or(0, |meta| meta.len()); + PromptFile::link(path, size) + }) + .collect() +} diff --git a/crates/agent/src/panel/attachments.rs b/crates/agent/src/panel/attachments.rs index b8ee4290..62da8e07 100644 --- a/crates/agent/src/panel/attachments.rs +++ b/crates/agent/src/panel/attachments.rs @@ -1,7 +1,8 @@ -//! One presentation index per render, independent of terminal grants. +//! One presentation index per render, projected from the chat's original seat. use crate::thread::Attachment; -use gpui_kit::{App, EntityId, WeakEntity}; -use nocterm_workspace::{TerminalStatus, Workspace}; +use gpui_kit::{App, EntityId}; +use nocterm_agent_chat::seat::Seat; +use nocterm_workspace::TerminalStatus; use std::collections::{HashMap, HashSet}; pub(super) struct AttachmentLabels { terminals: HashMap, @@ -10,11 +11,9 @@ pub(super) struct AttachmentLabels { groups: HashSet, } impl AttachmentLabels { - pub(super) fn new(workspace: &WeakEntity, cx: &App) -> Self { - let workspace = workspace.upgrade(); - let terminals = workspace - .as_ref() - .map(|workspace| workspace.read(cx).terminals(cx)) + pub(super) fn new(seat: &Seat, cx: &App) -> Self { + let terminals = seat + .terminals(cx) .unwrap_or_default() .into_iter() .map(|entry| { @@ -26,11 +25,7 @@ impl AttachmentLabels { (entry.item, (entry.title.to_string(), connected)) }) .collect(); - let summaries = workspace - .as_ref() - .and_then(|workspace| workspace.read(cx).connection_directory()) - .map(|directory| directory.connections(cx)) - .unwrap_or_default(); + let summaries = seat.connections(cx); let groups = summaries .iter() .filter_map(|summary| summary.group.as_ref().map(ToString::to_string)) diff --git a/crates/agent/src/panel/chat.rs b/crates/agent/src/panel/chat.rs index 8bf04e0f..d0863e5f 100644 --- a/crates/agent/src/panel/chat.rs +++ b/crates/agent/src/panel/chat.rs @@ -9,6 +9,32 @@ use gpui_kit::AnyElement; use nocterm_ai::usage::{Breakdown, RateLimit}; impl AgentPanel { + /// The open chat's footer: approvals, status, queue, commands and the composer. + pub(super) fn render_footer( + &mut self, + window: &mut Window, + cx: &mut Context, + ) -> AnyElement { + let Some(thread) = self.current() else { + return div().into_any_element(); + }; + let frame = super::composer_frame::ComposerFrame::read(&thread, cx); + let approvals = self.render_approvals(&thread, window, cx); + let status = self.chat_status(&thread, cx); + let labels = attachments::AttachmentLabels::new(thread.read(cx).workspace_seat(), cx); + let queue = self.render_queue(&thread, &labels, cx); + let commands = self.render_commands(window, cx); + let composer = self.render_composer(&frame, &thread, &labels, cx); + v_flex() + .w_full() + .min_w_0() + .children(approvals) + .children(status) + .children(queue) + .children(commands) + .child(composer) + .into_any_element() + } pub(super) fn sync_approval_attention(&mut self, window: &mut Window, cx: &mut Context) { let attention = self .chats @@ -18,14 +44,15 @@ impl AgentPanel { && (self .current() .is_none_or(|thread| !thread.read(cx).has_pending_approvals()) - || self - .host - .workspace() - .upgrade() - .is_none_or(|workspace| !workspace.read(cx).right_panel_is_open())); - let _ = self.host.workspace().update(cx, |workspace, cx| { - workspace.set_right_panel_attention(attention, cx) - }); + || self.host.workspace().upgrade().is_none_or(|workspace| { + !workspace.read(cx).panel_is_open(&crate::plugin::CHATS.id) + })); + // The workspace pulls the attention from `Panel::attention` when the panel notifies, + // so notify only when it changed: this runs on every chunk a chat streams. + let asks = Panel::attention(self, cx).is_some(); + if self.notices.attention_changed(asks) { + cx.notify(); + } let requests = needs_notice.then(|| { self.chats .iter() @@ -57,8 +84,8 @@ impl AgentPanel { move |window, cx| { let _ = panel.update(cx, |panel, cx| panel.review_pending(cx)); let _ = workspace.update(cx, |workspace, cx| { - if !workspace.right_panel_is_open() { - workspace.toggle_right_panel(window, cx); + if !workspace.panel_is_open(&crate::plugin::CHATS.id) { + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx); } }); }, @@ -80,78 +107,50 @@ impl AgentPanel { cx.notify(); } } - /// The cards of every pending request of `thread`; none when nothing waits. + /// The cards of every pending request of the chat: its sign-in, permission and tool + /// approval requests; none when nothing waits. pub(super) fn render_approvals( &mut self, thread: &Entity, window: &mut Window, cx: &mut Context, ) -> Option { - let permissions = thread.read(cx).permission_cards(); - let tools = thread.read(cx).tool_cards(); - let pending = permissions.len() + tools.len() + thread.read(cx).sign_in_waits().len(); - let asking: Vec = self - .chats - .iter() - .flat_map(|thread| { - thread - .read(cx) - .sign_in_waits() - .iter() - .map(|wait| wait.item()) - }) - .collect(); - self.sign_ins.reconcile(&asking); - if pending == 0 { - return None; - } - let mut list = v_flex() - .id("agent-approval-list") - .test_support() - .w_full() - .min_w_0() - .min_h_0() - .max_h(window.viewport_size().height * 0.35) - .overflow_y_scroll() - .gap_2(); - for index in 0..thread.read(cx).sign_in_waits().len() { - let wait = thread.read(cx).sign_in_waits()[index].clone(); - let (input, submission) = self.sign_ins.input_for(thread, &wait, window, cx); - list = list.child(approvals::ApprovalCardsRenderer::sign_in_card( - wait, input, submission, index, cx, - )); - } - for (index, permission) in permissions.into_iter().enumerate() { - list = list.child(approvals::ApprovalCardsRenderer::permission_card( - thread, permission, index, cx, - )); + let mut list = Vec::new(); + { + let asking: Vec = self + .chats + .iter() + .flat_map(|thread| { + thread + .read(cx) + .sign_in_waits() + .iter() + .map(|wait| wait.item()) + }) + .collect(); + self.sign_ins.reconcile(&asking); + for index in 0..thread.read(cx).sign_in_waits().len() { + let wait = thread.read(cx).sign_in_waits()[index].clone(); + let (input, submission) = self.sign_ins.input_for(thread, &wait, window, cx); + list.push(approvals::ApprovalCardsRenderer::sign_in_card( + wait, input, submission, index, cx, + )); + } + for (index, permission) in thread.read(cx).permission_cards().into_iter().enumerate() { + list.push(approvals::ApprovalCardsRenderer::permission_card( + thread, permission, index, cx, + )); + } } - for (index, tool) in tools.into_iter().enumerate() { - let call = tool.call(); - let target = thread.update(cx, |thread, cx| thread.describe_target(call, cx)); - let reusable = thread.update(cx, |thread, cx| !thread.unsafe_live_input(call, cx)); - list = list.child(approvals::ApprovalCardsRenderer::tool_card( - thread, tool, target, reusable, index, cx, - )); + let tokens = thread.read(cx).tool_approval_tokens(); + for (index, token) in tokens.into_iter().enumerate() { + if let Some(card) = + approvals::ApprovalCardsRenderer::tool_card(thread, token, index, cx) + { + list.push(card); + } } - Some( - v_flex() - .id("agent-approvals") - .test_support() - .w_full() - .min_w_0() - .flex_shrink_0() - .px_3() - .gap_1() - .child( - div() - .text_xs() - .text_color(cx.theme().warning) - .child(format!("Waiting for you · {pending}")), - ) - .child(list) - .into_any_element(), - ) + approvals::ApprovalCardsRenderer::list(list, window, cx) } pub(super) fn submit_sign_in( &mut self, @@ -172,16 +171,17 @@ impl AgentPanel { binding: message_list::ActiveListBinding, cx: &mut Context, ) { - let Some(thread) = self.current() else { + let Some(thread) = self.messages.thread() else { return; }; let reduced = cx.reduce_motion(); let changed = self.messages - .advance_reveal(&binding, thread.read(cx).history_entries(), reduced); + .advance_reveal(&binding, &thread.read(cx).transcript().entries, reduced); if let Some(changed) = changed { self.messages.apply_reveal(changed, cx); - cx.notify(); + // The reveal animates the transcript's rows only. + self.layout.parts().notify(parts::PartKind::Transcript, cx); } } pub(super) fn on_preview_completed( @@ -189,7 +189,7 @@ impl AgentPanel { binding: message_list::ActiveListBinding, ticket: image_pipeline::PreviewTicket, row: Option, - image: Option>, + image: Option, cx: &mut Context, ) { if self @@ -232,93 +232,114 @@ impl AgentPanel { ) }) } - pub(super) fn render_chat( - &mut self, - thread: Entity, - window: &mut Window, - cx: &mut Context, - ) -> AnyElement { - let mut body = v_flex().size_full().min_w_0().min_h_0(); - let plan = thread.read(cx).transcript().plan.clone(); - let usage = thread.read(cx).transcript().usage.clone(); - let dirty = thread + /// The transcript of the open chat, for its cached view; the rows changed since its last + /// drawing redraw. + pub(super) fn render_transcript_part(&mut self, cx: &mut Context) -> AnyElement { + let Some(thread) = self.current() else { + return div().into_any_element(); + }; + let dirty: Vec = thread .update(cx, |thread, _| thread.take_changed_rows()) .into_iter() - .collect::>(); - self.messages.bind_chat(thread.entity_id(), cx); - let rows = self.messages.observe_rows( - thread.entity_id(), - thread.read(cx).history_entries(), - &dirty, - thread.read(cx).session_generating(), - cx.reduce_motion(), - ); - self.messages.apply_rows(rows, cx); + .collect(); + let stamp = parts::transcript_stamp(thread.read(cx)); + self.layout.parts_mut().drew_transcript(Some(stamp)); + self.observe_rows(&thread, &dirty, cx); let scroller = self.messages.scroller(cx); let navigation = navigation::NavigationRenderer::render(&self.messages, cx); let overlay = self .menus .is_open(&MenuKind::Usage) .then(|| self.render_usage(cx)); - body = body.child(TranscriptView::area(scroller, navigation, overlay, cx)); - if let Some(plan) = &plan { - body = body.child(TranscriptView::plan(plan, cx)); - } - if let Some(approvals) = self.render_approvals(&thread, window, cx) { - body = body.child(approvals); - } - let labels = attachments::AttachmentLabels::new(self.host.workspace(), cx); - let composer = self.render_composer(&thread, usage.as_ref(), &labels, cx); - let status = self.chat_status(&thread, cx); - body = body - .children(status) - .children(self.render_queue(&thread, &labels, window, cx)) - .children(self.render_commands(window, cx)) - .child(composer); - body.into_any_element() + let plan = thread + .read(cx) + .transcript() + .plan + .as_ref() + .map(|plan| TranscriptView::plan(plan, cx)); + v_flex() + .size_full() + .min_h_0() + .min_w_0() + .child(TranscriptView::area(scroller, navigation, overlay, cx)) + .children(plan) + .into_any_element() } pub(super) fn render_entry( &mut self, index: usize, cx: &mut Context, ) -> gpui_kit::AnyElement { - let Some(thread) = self.current() else { + let Some(thread) = self.messages.thread() else { return div().into_any_element(); }; + let identity = thread.entity_id(); + let markdown = thread + .read(cx) + .transcript() + .entries + .get(index) + .and_then(|entry| { + super::message_markdown::MessageMarkdown::text(entry, index, &self.messages) + }); + if let Some((text, source_len)) = markdown { + self.messages.prepare_markdown(index, text, source_len, cx); + } let state = thread.read(cx); - let Some(entry) = state.history_entries().get(index) else { + let transcript = state.transcript(); + let Some(entry) = transcript.entries.get(index) else { return div().into_any_element(); }; + let generating = state.session_generating(); let frame = RowPresentation::new( - thread.entity_id(), + identity, self.messages.binding(), index, self.messages.expanded(index), entry_is_live( - state.history_entries(), + &transcript.entries, index, - state.session_generating() && state.session_accepts_updates(), + generating && state.session_accepts_updates(), ), - state.session_generating(), - state.transcript().time(index), + generating, + transcript.time(index), self.settings .setting::(cx) .approval .redact_secrets, - ); + ) + .with_fork_available(true); let (row, pending) = EntryRenderer::render(frame, entry, &self.messages, cx); - for (key, data) in pending { + for source in pending { preview_operation::PreviewOperation::cached( &mut self.messages, - key, - || data, - true, + source, Some(index), cx, ); } row } + /// The visible list follows the open chat. Each observer supplies its own dirty rows, so + /// one window never consumes another's change tracking. + pub(super) fn observe_rows( + &mut self, + thread: &Entity, + dirty: &[usize], + cx: &mut Context, + ) { + self.messages.bind_thread(thread, cx); + let reduced = cx.reduce_motion(); + let state = thread.read(cx); + let rows = self.messages.observe_rows( + thread.entity_id(), + &state.transcript().entries, + dirty, + state.session_generating(), + reduced, + ); + self.messages.apply_rows(rows, cx); + } pub(super) fn jump_to_message(&mut self, first: bool, cx: &mut Context) { self.messages.navigate(first, cx); cx.notify(); @@ -344,7 +365,6 @@ impl AgentPanel { if open { self.close_usage(window, cx); } else { - self.refresh_usage(cx); self.menus.open(MenuKind::Usage); // Read-only information must not interrupt composing. if !self.composer.focus(cx).is_focused(window) { @@ -353,12 +373,6 @@ impl AgentPanel { cx.notify(); } } - pub(super) fn refresh_usage(&mut self, cx: &mut Context) { - if let Some(thread) = self.current() { - let agent = thread.read(cx).document().agent_id().to_owned(); - self.host.runtime().refresh_limits(&agent, cx); - } - } pub(super) fn usage_report(&self, cx: &App) -> Option { let thread = self.current()?; let thread = thread.read(cx); @@ -409,34 +423,3 @@ impl AgentPanel { } } } - -impl AgentPanel { - pub(super) fn render_queue( - &mut self, - thread: &Entity, - labels: &attachments::AttachmentLabels, - window: &mut Window, - cx: &mut Context, - ) -> Option { - if thread.read(cx).composer().queue().is_empty() { - return None; - } - let height = queue::QueueRenderer::visible_height(&self.composer, thread, window, cx); - let prompts = if self.composer.queue_open() || height > gpui_kit::px(0.) { - thread.read(cx).composer().queue().to_vec() - } else { - Vec::new() - }; - let previews = - preview_operation::PreviewOperation::queued_previews(&mut self.messages, &prompts, cx); - queue::QueueRenderer::render_queue( - &self.composer, - prompts, - previews, - thread, - labels, - height, - cx, - ) - } -} diff --git a/crates/agent/src/panel/chat_servers.rs b/crates/agent/src/panel/chat_servers.rs new file mode 100644 index 00000000..b7cbcebf --- /dev/null +++ b/crates/agent/src/panel/chat_servers.rs @@ -0,0 +1,173 @@ +//! The servers an agent that runs elsewhere may sign in to, as its chat offers them. +//! +//! Such an agent gets none of this device's terminals: the attach menu lists the servers the +//! chat offers, each checked while the chat has it attached, and the composer shows the +//! attached ones as removable chips. Every server is a flag of the chat's configuration +//! (`ConfigCategory::Server`), so a choice is stored where the agent runs and every device +//! shows it. + +use gpui_kit::{ + AnyElement, Context, Entity, SharedString, + component::{ + ActiveTheme as _, Disableable as _, Selectable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_ai::model::controls::{ConfigCategory, ConfigKind, ConfigOption, ConfigSetting}; +use nocterm_ui::IconName; + +use super::{ + AgentPanel, + widgets::{menu_row, menu_variant}, +}; +use crate::thread::AgentThread; + +/// A server the chat offers, and whether the chat has it attached. +struct Offered { + option: ConfigOption, + attached: bool, +} + +fn offered(thread: &AgentThread) -> Vec { + thread + .config_options() + .into_iter() + .filter(is_server) + .map(|option| Offered { + attached: matches!(option.kind, ConfigKind::Boolean(true)), + option, + }) + .collect() +} + +/// Whether `option` attaches a server rather than configuring the agent. +pub(super) fn is_server(option: &ConfigOption) -> bool { + option.category == Some(ConfigCategory::Server) +} + +/// Whether the chat's agent signs in to the chat's servers instead of using this device's +/// terminals. +pub(super) fn takes_servers(thread: &AgentThread) -> bool { + thread + .agent_info() + .is_some_and(|info| info.capabilities.own_tools) + || thread.config_options().iter().any(is_server) +} + +/// Attaches or detaches the server of `option` through the chat's configuration. +fn choose( + thread: &Entity, + option: &ConfigOption, + attach: bool, + cx: &mut Context, +) { + let id = option.id.clone(); + thread.update(cx, |thread, cx| { + thread.set_config(id, ConfigSetting::Boolean(attach), cx); + }); +} + +pub(super) struct ChatServersRenderer; + +impl ChatServersRenderer { + /// The attach menu of a chat whose agent takes servers. + pub(super) fn render_menu( + thread: &Entity, + cx: &mut Context, + ) -> AnyElement { + let servers = offered(thread.read(cx)); + let busy = thread.read(cx).session_generating(); + let mut menu = v_flex().w_full().gap(px(2.)); + if servers.is_empty() { + menu = menu.child( + div() + .px_2() + .py_1() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child( + "No servers yet. Give agents access to a saved server, or connect one in \ + the hub's web panel.", + ), + ); + } + for (index, server) in servers.into_iter().enumerate() { + menu = menu.child(Self::row(thread, index, server, busy, cx)); + } + menu.into_any_element() + } + + fn row( + thread: &Entity, + index: usize, + server: Offered, + busy: bool, + cx: &mut Context, + ) -> AnyElement { + let Offered { option, attached } = server; + let address = option.description.clone().unwrap_or_default(); + let theme = cx.theme().clone(); + let thread = thread.clone(); + let chosen = option.clone(); + menu_row(("attach-server", index), option.name.clone(), cx) + .icon(if attached { + IconName::Check + } else { + IconName::Server + }) + .child( + div() + .flex_shrink_0() + .max_w(px(180.)) + .truncate() + .text_xs() + .text_color(theme.muted_foreground) + .child(address.clone()), + ) + .tooltip(format!("{} · {address}", option.name)) + .selected(attached) + .disabled(busy) + .on_click(cx.listener(move |_, _, _, cx| { + choose(&thread, &chosen, !attached, cx); + })) + .into_any_element() + } + + /// Removable chips for the servers the chat has attached. + pub(super) fn chips( + thread: &Entity, + cx: &mut Context, + ) -> Vec { + let busy = thread.read(cx).session_generating(); + offered(thread.read(cx)) + .into_iter() + .filter(|server| server.attached) + .enumerate() + .map(|(index, server)| { + let thread = thread.clone(); + let option = server.option; + let name = option.name.clone(); + Button::new(SharedString::from(format!("server-chip-{index}"))) + .custom(menu_variant(cx)) + .small() + .icon(IconName::Server) + .max_w_full() + .label(name.clone()) + .tooltip(format!( + "Detach {name} · {}", + option.description.clone().unwrap_or_default() + )) + .disabled(busy) + .child(gpui_kit::component::Icon::new(IconName::X).xsmall()) + .on_click(cx.listener(move |_, _, _, cx| { + choose(&thread, &option, false, cx); + })) + .into_any_element() + }) + .collect() + } +} diff --git a/crates/agent/src/panel/composer.rs b/crates/agent/src/panel/composer.rs index 2e31f01e..3aee82bb 100644 --- a/crates/agent/src/panel/composer.rs +++ b/crates/agent/src/panel/composer.rs @@ -5,10 +5,9 @@ use super::{ menu::{ConfigurationSelection, FavoriteSelection, ModeSelection}, }; use crate::thread::Attachment; -use gpui_kit::{AnyElement, PathPromptOptions}; +use gpui_kit::AnyElement; use nocterm_ai::images::PromptImage; use nocterm_ai::model::ids::AuthMethodId; -use nocterm_ai::model::usage::ContextUsage; use std::path::PathBuf; impl AgentPanel { @@ -25,7 +24,8 @@ impl AgentPanel { } self.sync_composer_draft(cx); self.sync_command_token(window, cx); - cx.notify(); + // Typing redraws the footer with the composer, not the transcript or history. + self.layout.parts().notify(parts::PartKind::Footer, cx); } pub(super) fn submit_composer(&mut self, window: &mut Window, cx: &mut Context) { self.send(window, cx); @@ -167,10 +167,11 @@ impl AgentPanel { ) { self.composer.select_command_token(range, window, cx); } + /// The composer of the open chat: its field, attachments, controls and notices. pub(super) fn render_composer( &mut self, + frame: &composer_frame::ComposerFrame, thread: &Entity, - usage: Option<&ContextUsage>, labels: &attachments::AttachmentLabels, cx: &mut Context, ) -> AnyElement { @@ -184,16 +185,19 @@ impl AgentPanel { cx, ); let input = composer_renderer::ComposerRenderer::render_input(&self.composer, cx); + let stop = composer_frame::RenderedStop::capture(thread, cx); let footer = composer_renderer::ComposerRenderer::render_footer( + frame, &self.composer, &self.menus, thread, - usage, + stop, cx, ); + let terminal = self.host.sign_in().is_some(); let notices = composer_renderer::ComposerRenderer::render_notices( &self.composer, - self.host.sign_in().is_some(), + terminal, thread, cx, ); @@ -207,6 +211,25 @@ impl AgentPanel { cx, ) } + /// The Local chat's queued prompts above the composer; none while the queue is empty. + pub(super) fn render_queue( + &mut self, + thread: &Entity, + labels: &attachments::AttachmentLabels, + cx: &mut Context, + ) -> Option { + if thread.read(cx).composer().queue().is_empty() { + return None; + } + let prompts = if self.composer.queue_open() { + thread.read(cx).composer().queue().to_vec() + } else { + Vec::new() + }; + let previews = + preview_operation::PreviewOperation::queued_previews(&mut self.messages, &prompts, cx); + queue::QueueRenderer::render_queue(&self.composer, prompts, previews, thread, labels, cx) + } pub(super) fn prepare_images( &mut self, work: impl std::future::Future, ImageAttachError>> @@ -217,7 +240,7 @@ impl AgentPanel { let Some(thread) = self.current() else { return; }; - if self.preparing_queue_edit() + if self.preparing_images() || !thread .read(cx) .agent_info() @@ -225,11 +248,18 @@ impl AgentPanel { { return; } + let Some(permit) = self.messages.admit_image_work() else { + self.notices + .set_error(Some(ImageAttachError::Busy.to_string())); + cx.notify(); + return; + }; let binding = self.composer.prepare_images(thread.entity_id()); cx.notify(); let ticket = thread.read(cx).session_ticket(); let owner = thread.downgrade(); - image_preparation::ImagePreparation::new(binding, owner, ticket, Box::pin(work)).launch(cx); + image_preparation::ImagePreparation::new(binding, owner, ticket, Box::pin(work), permit) + .launch(cx); } pub(super) fn on_composer_images_prepared( &mut self, @@ -269,38 +299,47 @@ impl AgentPanel { cx.notify(); } pub(super) fn add_image_bytes(&mut self, bytes: Vec, cx: &mut Context) { + let Some(thread) = self.current() else { + return; + }; + let budget = nocterm_ai::images::ImageCollectionBudget::remaining( + thread.read(cx).composer().images(), + ); let inspector = self.messages.inspector(); self.prepare_images( - async move { Ok(vec![PromptImage::validate(bytes, inspector.as_ref())?]) }, + async move { + let mut budget = budget?; + budget.admit(bytes.len())?; + Ok(vec![PromptImage::validate(bytes, inspector.as_ref())?]) + }, cx, ); } pub(super) fn add_images(&mut self, paths: Vec, cx: &mut Context) { + let Some(thread) = self.current() else { + return; + }; + let budget = nocterm_ai::images::ImageCollectionBudget::remaining( + thread.read(cx).composer().images(), + ); let inspector = self.messages.inspector(); self.prepare_images( - async move { images::read_images(paths, inspector.as_ref()) }, + async move { images::read_images(paths, inspector.as_ref(), budget?) }, cx, ); } - pub(super) fn on_image_paths_picked( - &mut self, - operation: image_picker::ImagePickerOperation, - paths: Vec, - cx: &mut Context, - ) { - if operation.current( - self.current().map(|thread| thread.entity_id()), - self.composer.revision(), - ) { - self.add_images(paths, cx); - } + /// Whether the composer takes images and files: a chat is open. + pub(super) fn composer_attaches(&self, _cx: &App) -> bool { + self.current().is_some() } - pub(super) fn pick_images(&mut self, cx: &mut Context) { - let operation = image_picker::ImagePickerOperation::new( - self.current().map(|thread| thread.entity_id()), - self.composer.revision(), - ); - let picked = cx.prompt_for_paths(PathPromptOptions { + /// The paperclip: files of any kind, picked for the open chat. + pub(super) fn pick_files(&mut self, cx: &mut Context) { + if !self.composer_attaches(cx) { + return; + } + let chat = self.current().map(|thread| thread.entity_id()); + let pick = attach_files::AttachPick::new(chat, self.composer.revision()); + let picked = cx.prompt_for_paths(gpui_kit::PathPromptOptions { files: true, directories: false, multiple: true, @@ -309,12 +348,52 @@ impl AgentPanel { cx.spawn(async move |this, cx| { if let Ok(Ok(Some(paths))) = picked.await { let _ = this.update(cx, |this, cx| { - this.on_image_paths_picked(operation, paths, cx) + let chat = this.current().map(|thread| thread.entity_id()); + if pick.current(chat, this.composer.revision()) { + this.attach_paths(paths, cx); + } }); } }) .detach(); } + /// Attaches `paths`: images the agent takes as images, every other file as a link. + pub(super) fn attach_paths(&mut self, paths: Vec, cx: &mut Context) { + let Some(thread) = self.current() else { + return; + }; + let images_taken = thread + .read(cx) + .agent_info() + .is_none_or(|info| info.capabilities.image_prompts); + let (images, files) = attach_files::split(paths, images_taken); + if !images.is_empty() { + self.add_images(images, cx); + } + if files.is_empty() { + return; + } + let chat = thread.entity_id(); + let read = cx + .background_executor() + .spawn(async move { attach_files::link(&files) }); + cx.spawn(async move |this, cx| { + let linked = read.await; + let _ = this.update(cx, |this, cx| { + let Some(thread) = this.chats.find(chat).cloned() else { + return; + }; + let added = linked.and_then(|files| { + thread.update(cx, |thread, cx| thread.add_composer_files(files, cx)) + }); + if let Err(error) = added { + this.notices.set_error(Some(error.to_string())); + } + cx.notify(); + }); + }) + .detach(); + } pub(super) fn on_menu_open_changed( &mut self, kind: MenuKind, @@ -362,6 +441,7 @@ impl AgentPanel { pub(super) fn preparing_images(&self) -> bool { self.composer.preparing() } + #[cfg(test)] pub(super) fn preparing_queue_edit(&self) -> bool { self.composer.preparing_edit() } @@ -480,13 +560,23 @@ impl AgentPanel { return; }; let draft = self.original_composer_text(cx); - let ready = prompt.saved().images.is_empty(); + let ready = !prompt + .saved() + .images + .iter() + .any(|content| matches!(content, nocterm_ai::model::content::Content::Image(_))); + let files = prompt + .saved() + .images + .iter() + .filter_map(nocterm_ai::prompt_files::PromptFile::of) + .collect(); let binding = self .composer .begin_edit(thread.entity_id(), id, draft.clone(), ready); thread.update(cx, |thread, cx| { thread.set_draft(draft, cx); - thread.begin_composer_edit(Vec::new(), prompt.attachments().to_vec(), cx); + thread.begin_composer_edit(Vec::new(), files, prompt.attachments().to_vec(), cx); cx.notify(); }); if !ready { @@ -513,7 +603,7 @@ impl AgentPanel { thread.read(cx).session_ticket(), id, saved, - self.messages.inspector(), + self.messages.image_deps(), ) .launch(cx); self.composer.install_edit_preparation(&binding, id, task); @@ -561,16 +651,6 @@ impl AgentPanel { self.composer.set_text(draft, window, cx); } - pub(super) fn on_queue_height_observed( - &mut self, - chat: EntityId, - height: gpui_kit::Pixels, - cx: &mut Context, - ) { - if self.composer.observe_queue_height(chat, height) { - cx.notify(); - } - } pub(super) fn sync_composer_draft(&mut self, cx: &mut Context) { let Some(thread) = self.current() else { return; @@ -580,7 +660,6 @@ impl AgentPanel { thread.update(cx, |thread, cx| thread.set_draft(text, cx)); } } - /// Reads the plan limits again when the card opens. pub(super) fn close_menus(&mut self, cx: &mut Context) { self.menus.reset(); cx.notify(); @@ -593,7 +672,7 @@ impl AgentPanel { return; } self.composer.toggle_queue(); - cx.notify(); + self.layout.parts().notify(parts::PartKind::Footer, cx); } pub(super) fn on_attachment_selected( &mut self, @@ -618,18 +697,28 @@ impl AgentPanel { }); } } + pub(super) fn on_file_removed( + &mut self, + chat: gpui_kit::EntityId, + index: usize, + cx: &mut Context, + ) { + if let Some(thread) = self.chats.find(chat).cloned() { + thread.update(cx, |thread, cx| thread.remove_composer_file(index, cx)); + } + } pub(super) fn on_send_clicked( &mut self, chat: gpui_kit::EntityId, window: &mut Window, cx: &mut Context, ) { - if let Some(thread) = self.current().filter(|thread| thread.entity_id() == chat) { - if composer_renderer::ComposerRenderer::stops(&self.composer, &thread, cx) { - thread.update(cx, |thread, cx| thread.stop(cx)); - } else { - self.send(window, cx); - } + // The send button stops a run through its rendered stop; this sends. + if self + .current() + .is_some_and(|thread| thread.entity_id() == chat) + { + self.send(window, cx); } } pub(super) fn on_restart_clicked( diff --git a/crates/agent/src/panel/composer_frame.rs b/crates/agent/src/panel/composer_frame.rs new file mode 100644 index 00000000..4959e2a9 --- /dev/null +++ b/crates/agent/src/panel/composer_frame.rs @@ -0,0 +1,66 @@ +//! What the composer of the open chat offers, read once per drawing from its thread. +use super::composer_view::ComposerView; +use crate::thread::AgentThread; +use gpui_kit::{App, Entity, EntityId, WeakEntity}; +use nocterm_ai::model::usage::ContextUsage; +use nocterm_ai::session::Ticket; + +pub(super) struct ComposerFrame { + origin: EntityId, + can_prompt: bool, + can_stop: bool, + usage: Option, +} +impl ComposerFrame { + pub(super) fn read(thread: &Entity, cx: &App) -> Self { + let state = thread.read(cx); + Self { + origin: thread.entity_id(), + can_prompt: !state.session_sign_in_required(), + can_stop: state.session_generating() || state.queued_prompt_ids().len() != 0, + usage: state.transcript().usage.clone(), + } + } + /// The chat the composer sends to and its controls act on. + pub(super) fn origin(&self) -> EntityId { + self.origin + } + /// The context usage the usage ring shows. + pub(super) fn usage(&self) -> Option<&ContextUsage> { + self.usage.as_ref() + } + /// Whether the send button stops the run instead: nothing is typed, attached or edited + /// and there is a run to stop. + pub(super) fn stops(&self, composer: &ComposerView, images: bool, cx: &App) -> bool { + self.can_stop && composer.text(cx).trim().is_empty() && !images && !composer.editing() + } + /// Whether sending is unavailable: the chat takes no prompts now or images are being + /// prepared. + pub(super) fn send_disabled(&self, stops: bool, composer: &ComposerView) -> bool { + !stops && (!self.can_prompt || composer.preparing()) + } +} + +/// The stop the send button shows: the chat and the session it was drawn for. A stop drawn +/// for an earlier session of the chat stops nothing. +#[derive(Clone)] +pub(super) struct RenderedStop { + thread: WeakEntity, + ticket: Ticket, +} +impl RenderedStop { + pub(super) fn capture(thread: &Entity, cx: &App) -> Self { + Self { + thread: thread.downgrade(), + ticket: thread.read(cx).session_ticket(), + } + } + pub(super) fn activate(&self, cx: &mut App) { + let Some(thread) = self.thread.upgrade() else { + return; + }; + if thread.read(cx).session_ticket() == self.ticket { + thread.update(cx, |thread, cx| thread.stop(cx)); + } + } +} diff --git a/crates/agent/src/panel/composer_renderer.rs b/crates/agent/src/panel/composer_renderer.rs index c73c1006..f1c1b22f 100644 --- a/crates/agent/src/panel/composer_renderer.rs +++ b/crates/agent/src/panel/composer_renderer.rs @@ -13,7 +13,6 @@ use gpui_kit::{ px, rems, }; use nocterm_ai::model::auth::AuthFlow; -use nocterm_ai::model::usage::ContextUsage; use nocterm_ui::IconName; use super::{ @@ -33,12 +32,19 @@ fn remove_image(origin: gpui_kit::EntityId, index: usize, cx: &mut Context, clipboard: &ClipboardItem, cx: &mut App, ) -> bool { + if !panel + .upgrade() + .is_some_and(|panel| panel.read(cx).composer_attaches(cx)) + { + return false; + } let paths = clipboard .entries() .iter() @@ -49,7 +55,7 @@ pub(super) fn paste( .flatten() .collect::>(); if !paths.is_empty() { - let _ = panel.update(cx, |panel, cx| panel.add_images(paths, cx)); + let _ = panel.update(cx, |panel, cx| panel.attach_paths(paths, cx)); return true; } let image = clipboard.entries().iter().find_map(|entry| match entry { @@ -65,13 +71,15 @@ pub(super) fn paste( pub(super) struct ComposerRenderer; impl ComposerRenderer { - /// Removable chips for the attached terminals and servers. + /// Removable chips for the attached terminals, servers and files. pub(super) fn render_attachment_chips( thread: &Entity, labels: &super::attachments::AttachmentLabels, cx: &mut Context, ) -> Option { - if thread.read(cx).composer().attachments().is_empty() { + let servers = super::chat_servers::ChatServersRenderer::chips(thread, cx); + let composer = thread.read(cx).composer(); + if composer.attachments().is_empty() && composer.files().is_empty() && servers.is_empty() { return None; } let origin = thread.entity_id(); @@ -81,7 +89,8 @@ impl ComposerRenderer { .min_w_0() .gap_1() .flex_wrap() - .items_start(); + .items_start() + .children(servers); for attachment in thread.read(cx).composer().attachments() { let value = attachment.clone(); let label = labels.label(attachment); @@ -104,6 +113,22 @@ impl ComposerRenderer { })), ); } + for (index, file) in thread.read(cx).composer().files().iter().enumerate() { + let name = file.name().to_owned(); + chips = chips.child( + Button::new(("file-chip", index)) + .custom(menu_variant(cx)) + .small() + .icon(IconName::Paperclip) + .max_w_full() + .label(name.clone()) + .tooltip(format!("Remove {name}")) + .child(gpui_kit::component::Icon::new(IconName::X).xsmall()) + .on_click(cx.listener(move |this, _, _, cx| { + this.on_file_removed(origin, index, cx); + })), + ); + } Some(chips.into_any_element()) } /// Thumbnails of the images the next message will carry. @@ -182,7 +207,12 @@ impl ComposerRenderer { ) -> AnyElement { let generating = thread.read(cx).session_generating(); let mut controls = h_flex().w_full().min_w_0().gap_1().flex_wrap(); - for option in &thread.read(cx).config_options() { + let options = thread.read(cx).config_options(); + // Servers are attached from the attach menu, not configured here. + for option in options + .iter() + .filter(|option| !super::chat_servers::is_server(option)) + { let id = option.id.to_string(); controls = controls.child(super::menu::ConfigMenuRenderer::popover( menus, @@ -220,28 +250,60 @@ impl ComposerRenderer { } controls.into_any_element() } - /// Attach buttons, session controls, usage and send. + /// The attach controls, the session's options, usage and send. pub(super) fn render_footer( + frame: &super::composer_frame::ComposerFrame, composer: &super::composer_view::ComposerView, menus: &super::menu_view::MenuView, thread: &Entity, - usage: Option<&ContextUsage>, + stop: super::composer_frame::RenderedStop, cx: &mut Context, ) -> AnyElement { - let context_tooltip = format!( - "{} attached terminals · sent metadata {} B · terminal output {} B", - thread.read(cx).composer().attachments().len(), - thread.read(cx).context_bytes(), - thread.read(cx).tool_bytes() + let images = !thread.read(cx).composer().images().is_empty(); + let stops = frame.stops(composer, images, cx); + let attach = Self::render_attach(composer, menus, thread, cx); + let options = Self::render_config_controls(composer, menus, thread, cx); + let usage = super::usage::UsageCard::usage_button( + menus.is_open(&MenuKind::Usage), + frame.usage(), + cx, + ); + let send = Self::send_button( + frame.origin(), + stops, + frame.send_disabled(stops, composer), + stop, + composer.input_ticket(cx).ok(), + cx, ); - let images_supported = thread - .read(cx) - .agent_info() - .is_some_and(|info| info.capabilities.image_prompts); - let controls = Self::render_config_controls(composer, menus, thread, cx); h_flex() .gap_1() - .child(super::menu::ConfigMenuRenderer::popover( + .children(attach) + .child(div().flex_1().min_w_0().child(options)) + .child(usage) + .child(send) + .into_any_element() + } + /// Attach a terminal or server, and an image. + fn render_attach( + composer: &super::composer_view::ComposerView, + menus: &super::menu_view::MenuView, + thread: &Entity, + cx: &mut Context, + ) -> [AnyElement; 2] { + let context_tooltip = if super::chat_servers::takes_servers(thread.read(cx)) { + "Attach servers the agent may sign in to".to_owned() + } else { + format!( + "Attach terminal or server\n{} attached terminals · sent metadata {} B · terminal \ + output {} B", + thread.read(cx).composer().attachments().len(), + thread.read(cx).context_bytes(), + thread.read(cx).tool_bytes() + ) + }; + [ + super::menu::ConfigMenuRenderer::popover( menus, "context-popover", MenuKind::Context, @@ -249,46 +311,29 @@ impl ComposerRenderer { .ghost() .small() .icon(IconName::Plus) - .tooltip(format!("Attach terminal or server\n{context_tooltip}")), + .tooltip(context_tooltip), Anchor::BottomLeft, cx, - )) - .child( - Button::new("agent-attach-image") - .ghost() - .small() - .icon(IconName::ImagePlus) - .tooltip("Attach image") - .disabled(composer.preparing() || !images_supported) - .on_click(cx.listener(|this, _, _, cx| this.pick_images(cx))), ) - .child(div().flex_1().min_w_0().child(controls)) - .child(super::usage::UsageCard::usage_button( - menus.is_open(&MenuKind::Usage), - usage, - cx, - )) - .child(Self::render_send(composer, thread, cx)) - .into_any_element() - } - /// Whether the send button stops the running turn instead. - pub(super) fn stops( - composer: &super::composer_view::ComposerView, - thread: &Entity, - cx: &App, - ) -> bool { - thread.read(cx).session_generating() - && composer.text(cx).trim().is_empty() - && thread.read(cx).composer().images().is_empty() - && !composer.editing() + .into_any_element(), + Button::new("agent-attach-files") + .ghost() + .small() + .icon(IconName::Paperclip) + .tooltip("Attach files") + .disabled(composer.preparing()) + .on_click(cx.listener(|this, _, _, cx| this.pick_files(cx))) + .into_any_element(), + ] } - pub(super) fn render_send( - composer: &super::composer_view::ComposerView, - thread: &Entity, + fn send_button( + origin: gpui_kit::EntityId, + stop: bool, + disabled: bool, + captured: super::composer_frame::RenderedStop, + input: Option, cx: &mut Context, ) -> AnyElement { - let origin = thread.entity_id(); - let stop = Self::stops(composer, thread, cx); Button::new("agent-send") .primary() .small() @@ -305,10 +350,20 @@ impl ComposerRenderer { }) .flex_shrink_0() .tooltip(if stop { "Stop" } else { "Send" }) - .disabled(thread.read(cx).session_sign_in_required() || composer.preparing()) - .on_click( - cx.listener(move |panel, _, window, cx| panel.on_send_clicked(origin, window, cx)), - ) + .disabled(disabled || input.is_none()) + .on_click(cx.listener(move |panel, _, window, cx| { + if input + .as_ref() + .is_none_or(|input| !panel.composer.input_current(input, cx)) + { + return; + } + if stop { + captured.activate(cx); + } else { + panel.on_send_clicked(origin, window, cx); + } + })) .into_any_element() } /// Image preparation, restart and sign-in rows under the controls. diff --git a/crates/agent/src/panel/composer_view.rs b/crates/agent/src/panel/composer_view.rs index 13eb6957..9caf03ec 100644 --- a/crates/agent/src/panel/composer_view.rs +++ b/crates/agent/src/panel/composer_view.rs @@ -2,12 +2,57 @@ mod commands; use super::{AgentPanel, commands::CommandState}; use gpui_kit::{ - App, AppContext as _, Context, Entity, EntityId, FocusHandle, Focusable as _, Pixels, - SharedString, Subscription, Task, Window, + App, AppContext as _, Context, Entity, EntityId, FocusHandle, Focusable as _, SharedString, + Subscription, Task, Window, component::input::{InputEvent, Textarea, TextareaState}, - px, }; -use std::{collections::HashMap, sync::Arc}; +use std::{cell::RefCell, rc::Rc, sync::Arc}; + +#[derive(Clone)] +pub(super) struct InputTicket { + revision: u64, + reset: Rc<()>, +} +impl PartialEq for InputTicket { + fn eq(&self, other: &Self) -> bool { + self.revision == other.revision && Rc::ptr_eq(&self.reset, &other.reset) + } +} +impl Eq for InputTicket {} +struct InputGeneration { + observed: u64, + reset: Rc<()>, + fenced: bool, +} +impl Default for InputGeneration { + fn default() -> Self { + Self { + observed: 0, + reset: Rc::new(()), + fenced: false, + } + } +} +/// The composer's input was replaced since its text was read. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +#[error("The composer changed meanwhile.")] +pub(super) struct StaleInput; + +impl InputGeneration { + fn ticket(&mut self, revision: u64) -> Result { + // The native counter wraps. Once an observed generation regresses, + // only a new input instance can restore this fence. + self.fenced |= revision < self.observed; + self.observed = self.observed.max(revision); + if self.fenced { + return Err(StaleInput); + } + Ok(InputTicket { + revision, + reset: self.reset.clone(), + }) + } +} #[derive(Clone)] pub(super) struct ComposerBinding { @@ -33,11 +78,6 @@ struct QueueEdit { ready: bool, preparation: Option>, } -#[derive(Default)] -struct QueueView { - open: bool, - heights: HashMap, -} pub(super) struct EditRestoration { chat: EntityId, draft: String, @@ -49,11 +89,12 @@ impl EditRestoration { } pub(super) struct ComposerView { input: Entity, - queue: QueueView, + queue_open: bool, state: ComposerState, commands: CommandState, _bindings: Vec, binding: Option, + input_generation: RefCell, } impl ComposerView { pub(super) fn new(window: &mut Window, cx: &mut Context) -> Self { @@ -73,11 +114,12 @@ impl ComposerView { }); Self { input, - queue: QueueView::default(), + queue_open: false, state: ComposerState::default(), commands: CommandState::default(), _bindings: vec![submit, observe], binding: None, + input_generation: RefCell::new(InputGeneration::default()), } } pub(super) fn text(&self, cx: &App) -> SharedString { @@ -101,6 +143,14 @@ impl ComposerView { pub(super) fn revision(&self) -> u64 { self.state.revision } + pub(super) fn input_ticket(&self, cx: &App) -> Result { + let revision = self.input.read(cx).document_revision(); + self.input_generation.borrow_mut().ticket(revision) + } + pub(super) fn input_current(&self, ticket: &InputTicket, cx: &App) -> bool { + self.input_ticket(cx) + .is_ok_and(|current| ¤t == ticket) + } pub(super) fn editing(&self) -> bool { self.state.edit.is_some() } @@ -140,15 +190,16 @@ impl ComposerView { } } pub(super) fn clear(&mut self) { + self.input_generation.borrow_mut().reset = Rc::new(()); self.commands.reset(); self.state = ComposerState::default(); - self.queue = QueueView::default(); + self.queue_open = false; self.binding = None; } pub(super) fn leave(&mut self) { self.commands.reset(); self.invalidate(); - self.queue.open = false; + self.queue_open = false; } pub(super) fn take_edit(&mut self) -> Option { let edit = self.state.edit.take()?; @@ -273,31 +324,13 @@ impl ComposerView { true } pub(super) fn queue_open(&self) -> bool { - self.queue.open + self.queue_open } pub(super) fn open_queue(&mut self) { - self.queue.open = true; + self.queue_open = true; } pub(super) fn toggle_queue(&mut self) { - self.queue.open = !self.queue.open; - } - pub(super) fn queue_height(&self, chat: EntityId) -> Pixels { - self.queue.heights.get(&chat).copied().unwrap_or(px(0.)) - } - pub(super) fn observe_queue_height(&mut self, chat: EntityId, height: Pixels) -> bool { - if self - .queue - .heights - .get(&chat) - .is_some_and(|old| (*old - height).abs() <= px(0.5)) - { - return false; - } - self.queue.heights.insert(chat, height); - true - } - pub(super) fn removed_chats(&mut self, chats: &[EntityId]) { - self.queue.heights.retain(|id, _| !chats.contains(id)); + self.queue_open = !self.queue_open; } } diff --git a/crates/agent/src/panel/entries.rs b/crates/agent/src/panel/entries.rs index 81cc964f..5dc6bfc7 100644 --- a/crates/agent/src/panel/entries.rs +++ b/crates/agent/src/panel/entries.rs @@ -16,7 +16,7 @@ use super::{ AgentPanel, image_pipeline::PreviewState, message_actions::{MESSAGE_GROUP, escape_markdown, message_text}, - widgets::{chat_markdown, disclosure_header, safe_markdown}, + widgets::{chat_markdown, chat_markdown_in, disclosure_header, safe_markdown}, }; #[derive(Debug, thiserror::Error)] @@ -38,6 +38,7 @@ pub(super) struct RowPresentation { generating: bool, time: Option, redact: bool, + can_fork: bool, } pub(super) struct EntryRenderer; impl EntryRenderer { @@ -60,7 +61,7 @@ impl EntryRenderer { cx: &Context, ) -> ( gpui_kit::AnyElement, - Vec<(super::image_pipeline::ImageKey, Vec)>, + Vec, ) { let row = v_flex() .id(("agent-entry", frame.index)) @@ -82,7 +83,7 @@ impl EntryRenderer { cx, ), Entry::Agent(text) => Self::agent(row, &frame, text, messages, cx), - Entry::Thought(text) => Self::thought(row, &frame, text, cx), + Entry::Thought(text) => Self::thought(row, &frame, text, messages, cx), Entry::Tool(call) => Self::tool(row, &frame, call, cx), Entry::Content(Content::Image(image)) => { let key = Self::pending_image(image, messages, &mut pending); @@ -95,11 +96,12 @@ impl EntryRenderer { fn pending_image( image: &nocterm_ai::model::content::Image, messages: &super::message_list::MessageList, - pending: &mut Vec<(super::image_pipeline::ImageKey, Vec)>, + pending: &mut Vec, ) -> super::image_pipeline::ImageKey { - let key = (image.data.as_ptr() as usize, image.data.len(), true); + let source = super::image_pipeline::PreviewSource::Encoded(image.data.clone()); + let key = source.key(); if !messages.has_preview(&key) { - pending.push((key, image.data.as_bytes().to_vec())); + pending.push(source); } key } @@ -108,7 +110,7 @@ impl EntryRenderer { frame: &RowPresentation, parts: &[Content], messages: &super::message_list::MessageList, - pending: &mut Vec<(super::image_pipeline::ImageKey, Vec)>, + pending: &mut Vec, text: String, cx: &Context, ) -> gpui_kit::Stateful { @@ -125,6 +127,8 @@ impl EntryRenderer { .bg(cx.theme().muted); let mut images = h_flex().flex_wrap().gap_2(); let mut has_images = false; + let mut files = h_flex().flex_wrap().gap_1(); + let mut has_files = false; for (part_index, part) in parts.iter().enumerate() { match part { Content::Text(content) => { @@ -141,15 +145,22 @@ impl EntryRenderer { has_images = true; images = images.child(Self::image(messages.preview_state(&key))); } + Content::ResourceReference(link) => { + has_files = true; + files = files.child(Self::file(&link.name, cx)); + } _ => {} } } if has_images { bubble = bubble.child(images); } + if has_files { + bubble = bubble.child(files); + } row.child(bubble).child( super::message_actions::MessageRowActions::new( - frame.chat, + frame.can_fork.then_some(frame.chat), frame.index, text, frame.time, @@ -158,6 +169,23 @@ impl EntryRenderer { .render(cx), ) } + /// A file the message carried: its name beside a paperclip. + fn file(name: &str, cx: &Context) -> gpui_kit::Div { + h_flex() + .gap_1() + .px_1p5() + .py_0p5() + .rounded(px(6.)) + .border_1() + .border_color(cx.theme().border) + .text_xs() + .child( + gpui_kit::component::Icon::new(nocterm_ui::IconName::Paperclip) + .size_3() + .text_color(cx.theme().muted_foreground), + ) + .child(name.to_owned()) + } fn agent( mut row: gpui_kit::Stateful, frame: &RowPresentation, @@ -165,15 +193,20 @@ impl EntryRenderer { messages: &super::message_list::MessageList, cx: &Context, ) -> gpui_kit::Stateful { - let revealed = messages.revealed_text(frame.index, text); - row = row.group(MESSAGE_GROUP).child( - chat_markdown(("agent-message", frame.index), safe_markdown(&revealed)) - .stream_fade(frame.generating || messages.revealing()), - ); + let markdown = match messages.markdown(frame.index) { + Some(state) => chat_markdown_in(state), + None => chat_markdown( + ("agent-message", frame.index), + safe_markdown(&messages.revealed_text(frame.index, text)), + ), + }; + row = row + .group(MESSAGE_GROUP) + .child(markdown.stream_fade(frame.generating || messages.revealing())); if !frame.live { row = row.child( super::message_actions::MessageRowActions::new( - frame.chat, + frame.can_fork.then_some(frame.chat), frame.index, text.to_owned(), frame.time, @@ -188,6 +221,7 @@ impl EntryRenderer { mut row: gpui_kit::Stateful, frame: &RowPresentation, text: &str, + messages: &super::message_list::MessageList, cx: &Context, ) -> gpui_kit::Stateful { let index = frame.index; @@ -229,10 +263,13 @@ impl EntryRenderer { .max_w_full() .overflow_x_scroll() .child( - chat_markdown(("thought-text", index), safe_markdown(text)) - .text_color(cx.theme().muted_foreground) - .w_full() - .max_w_full(), + match messages.markdown(index) { + Some(state) => chat_markdown_in(state), + None => chat_markdown(("thought-text", index), safe_markdown(text)), + } + .text_color(cx.theme().muted_foreground) + .w_full() + .max_w_full(), ), ); } @@ -302,6 +339,11 @@ impl RowPresentation { generating, time, redact, + can_fork: false, } } + pub(super) fn with_fork_available(mut self, available: bool) -> Self { + self.can_fork = available; + self + } } diff --git a/crates/agent/src/panel/entries/tool_input.rs b/crates/agent/src/panel/entries/tool_input.rs index 66ea2d81..11de88ee 100644 --- a/crates/agent/src/panel/entries/tool_input.rs +++ b/crates/agent/src/panel/entries/tool_input.rs @@ -14,7 +14,7 @@ use gpui_kit::{ }; use nocterm_ai::model::tools::ToolRecord; use nocterm_ai::{ - TerminalCall, + TerminalCall, TransferFiles, tool_display::{self, ToolDisplay}, }; use nocterm_ui::IconName; @@ -217,9 +217,87 @@ fn request_source(call: TerminalCall) -> Source { TerminalCall::ListTerminals => { Source::plain("Scope", "Terminals attached to this chat".into(), None) } + TerminalCall::TransferFiles(v) => transfer_source(v), + TerminalCall::ReadTransfer(v) => transfer_handle(v.transfer_id, v.yield_ms), + TerminalCall::CancelTransfer(v) => transfer_handle(v.transfer_id, None), + call @ (TerminalCall::ListFiles(_) + | TerminalCall::ReadFile(_) + | TerminalCall::WriteFile(_) + | TerminalCall::EditFile(_) + | TerminalCall::MakeDirectory(_) + | TerminalCall::DeletePath(_) + | TerminalCall::MovePath(_)) => file_source(call), } } +/// A file call: its path, with what a change writes. +fn file_source(call: TerminalCall) -> Source { + let terminal = call.terminal_id().unwrap_or_default().to_owned(); + let mut source = match call { + TerminalCall::ListFiles(v) => Source::plain("Folder", v.path, None), + TerminalCall::ReadFile(v) => { + let mut source = Source::plain("File", v.path, None); + source.parameters.push(format!( + "From line {}, {} lines", + v.start_line.unwrap_or(1), + v.max_lines.unwrap_or(2000) + )); + source + } + TerminalCall::WriteFile(v) => { + let mut source = Source::plain("Content", v.content, None); + source.parameters.push(format!("File {}", v.path)); + if v.overwrite { + source.parameters.push("Replaces an existing file".into()); + } + source + } + TerminalCall::EditFile(v) => { + let mut source = Source::plain("Replace", v.old_text, None); + source.extra.push(Section { + label: "With", + text: v.new_text, + language: None, + }); + source.parameters.push(format!("File {}", v.path)); + if v.replace_all { + source.parameters.push("Every occurrence".into()); + } + source + } + TerminalCall::MakeDirectory(v) => Source::plain("New folder", v.path, None), + TerminalCall::DeletePath(v) => Source::plain("Delete", v.path, None), + TerminalCall::MovePath(v) => Source::plain("Move", format!("{}\n→ {}", v.from, v.to), None), + _ => Source::plain("Tool input", String::new(), None), + }; + source.parameters.push(format!("Terminal {terminal}")); + source +} + +fn transfer_source(v: TransferFiles) -> Source { + let mut source = Source::plain("Sources", v.sources.join("\n"), None); + source.parameters = vec![ + format!("From terminal {}", v.source_terminal_id), + format!( + "Into {} on terminal {}", + v.destination, v.destination_terminal_id + ), + format!( + "Existing files: {}", + if v.overwrite { "replace" } else { "skip" } + ), + ]; + source +} + +fn transfer_handle(transfer_id: String, wait_ms: Option) -> Source { + let mut source = Source::plain("Transfer handle", transfer_id, None); + if let Some(wait) = wait_ms { + source.parameters.push(format!("Wait {wait} ms")); + } + source +} + fn shell_script<'a>(program: &str, args: &'a [String]) -> Option<&'a str> { let program = program.rsplit('/').next()?; if !matches!(program, "bash" | "sh") || !matches!(args.first()?.as_str(), "-c" | "-lc") { diff --git a/crates/agent/src/panel/entries/tool_output/decode/payload.rs b/crates/agent/src/panel/entries/tool_output/decode/payload.rs index d6034064..896060d4 100644 --- a/crates/agent/src/panel/entries/tool_output/decode/payload.rs +++ b/crates/agent/src/panel/entries/tool_output/decode/payload.rs @@ -1,7 +1,9 @@ //! Decode the known bridge payload independently of provider envelopes. use super::super::Output; use crate::panel::entries::tool_input::Section; -use serde::Deserialize; +use nocterm_ai::tools::output::{ + CommandReport as Command, ObservationReport as Observation, TerminalReport as Terminal, +}; use serde_json::Value; /// Shape recognition protects payload fields from being mistaken for wrappers. @@ -87,18 +89,6 @@ pub(super) fn known(tool: &str, value: &Value) -> Option { Some(output) } -#[derive(Deserialize)] -struct Command { - command_id: String, - state: String, - stdout: String, - stderr: String, - exit_status: Option, - total_bytes: u64, - truncated: bool, - error: Option, -} - pub(super) fn command(value: &Value) -> Option { let value: Command = serde_json::from_value(value.clone()).ok()?; let state = match value.state.as_str() { @@ -141,18 +131,8 @@ pub(super) fn command(value: &Value) -> Option { Some(output) } -#[derive(Deserialize)] -struct Terminal { - text: String, - first_line: u64, - next_line: u64, - cursor_semantics: String, - truncated: bool, - alt_screen: bool, -} - fn terminal_output(value: Terminal) -> Option { - if value.cursor_semantics != "inclusive snapshot; replace overlapping lines" { + if value.cursor_semantics != Terminal::CURSOR_SEMANTICS { return None; } let mut output = Output::plain("Terminal output", value.text); @@ -172,18 +152,6 @@ pub(super) fn terminal(value: &Value) -> Option { terminal_output(serde_json::from_value(value.clone()).ok()?) } -#[derive(Deserialize)] -struct Observation { - text: String, - first_line: u64, - next_line: u64, - cursor_semantics: String, - truncated: bool, - alt_screen: bool, - completion: String, - completed: bool, - exit_status: Option, -} pub(super) fn observation(value: &Value) -> Option { let value: Observation = serde_json::from_value(value.clone()).ok()?; let status = match (value.completion.as_str(), value.completed) { @@ -195,14 +163,7 @@ pub(super) fn observation(value: &Value) -> Option { if value.exit_status.is_some() { return None; } - let mut output = terminal_output(Terminal { - text: value.text, - first_line: value.first_line, - next_line: value.next_line, - cursor_semantics: value.cursor_semantics, - truncated: value.truncated, - alt_screen: value.alt_screen, - })?; + let mut output = terminal_output(value.terminal)?; output.parameters.push(status.into()); output.parameters.push("Exit status: unknown".into()); Some(output) diff --git a/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs b/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs index 9a6c9576..a77b4f58 100644 --- a/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs +++ b/crates/agent/src/panel/entries/tool_output/decode/unwrap.rs @@ -11,7 +11,7 @@ pub(super) enum Part { } pub(super) fn normalized(tool: Option<&str>, value: &Value, depth: usize) -> Option> { - if serde_json::to_vec(value).ok()?.len() > MAX_DECODE_BYTES { + if nocterm_ai::history::json_size(value).ok()? > MAX_DECODE_BYTES { return None; } let mut parts = Vec::new(); diff --git a/crates/agent/src/panel/history.rs b/crates/agent/src/panel/history.rs index 63f1c53c..94bf78d3 100644 --- a/crates/agent/src/panel/history.rs +++ b/crates/agent/src/panel/history.rs @@ -1,10 +1,13 @@ //! The chat history: search, pinned chats first, and each chat's actions — -//! rename, fork, pin and delete. +//! rename, fork, pin and delete. A hub window lists its hub's chats here too, on their own +//! side: a switch shows either the chats of agents on this computer or those of the hub's +//! agents on the server. The chats other windows hold are listed among them, marked, with +//! no actions: opening one moves it here or brings its window forward. use gpui_kit::{ Anchor, App, Context, Entity, TestSupportExt as _, component::{ - ActiveTheme as _, Sizable as _, + ActiveTheme as _, Selectable as _, Sizable as _, button::{Button, ButtonVariants as _}, h_flex, input::Input, @@ -15,12 +18,13 @@ use gpui_kit::{ px, }; use nocterm_agent_directory::AgentsHandle; -use nocterm_ai::agents::AgentProfile; +use nocterm_ai::agents::{AgentProfile, AvailableAgents}; use nocterm_ui::IconName; use std::time::Duration; use super::{ AgentPanel, + history_column::HistorySide, widgets::{relative_prompt_time, single_line_label}, }; use crate::thread::AgentThread; @@ -40,6 +44,7 @@ pub(super) fn order(chats: &[(bool, u64)]) -> Vec { order } +/// What a history row shows. struct HistoryRow { id: EntityId, title: String, @@ -50,6 +55,8 @@ struct HistoryRow { generating: bool, agent: Option, rename: Option, + /// Another window holds it. + elsewhere: bool, } struct HistoryRowRenderer; impl HistoryRowRenderer { @@ -138,7 +145,12 @@ impl HistoryRowRenderer { let hover = cx.theme().muted.blend(cx.theme().foreground.opacity(0.04)); let name = Self::name(&row.title, row.rename.take(), cx); let details = Self::details(&row, name, cx); - let actions = Self::actions(row.id, row.pinned, cx); + let elsewhere = row.elsewhere; + let actions = if elsewhere { + Self::elsewhere_mark(id, row.generating, cx) + } else { + Self::actions(id, row.pinned, cx) + }; h_flex() .id(("history-thread", id.as_u64())) .test_support() @@ -159,11 +171,41 @@ impl HistoryRowRenderer { cx.new(|_| gpui_kit::component::tooltip::Tooltip::new(title.clone())) .into() }) - .on_click(cx.listener(move |panel, _, _, cx| panel.on_history_selected(id, cx))) + .on_click(cx.listener(move |panel, _, window, cx| { + if elsewhere { + panel.open_from_other_window(id, window, cx); + } else { + panel.on_history_selected(id, cx); + } + })) .child(details) .child(actions) .into_any_element() } + /// Where the actions of a chat another window holds would be: that window has them. + fn elsewhere_mark(id: EntityId, working: bool, cx: &App) -> gpui_kit::AnyElement { + let tip = if working { + "Working in another window: opens there" + } else { + "Open in another window: opens here" + }; + div() + .id(("history-elsewhere", id.as_u64())) + .test_support() + .aria_label(tip) + .flex_shrink_0() + .px_1() + .child( + gpui_kit::component::Icon::new(IconName::ExternalLink) + .xsmall() + .text_color(cx.theme().muted_foreground), + ) + .tooltip(move |_, cx| { + cx.new(|_| gpui_kit::component::tooltip::Tooltip::new(tip)) + .into() + }) + .into_any_element() + } fn actions(id: EntityId, pinned: bool, cx: &mut Context) -> gpui_kit::AnyElement { let panel = cx.weak_entity(); h_flex() @@ -238,41 +280,114 @@ impl HistoryRowRenderer { pub(super) struct HistoryProjection; impl HistoryProjection { - /// The chats to list, in order, matching the search. + /// The chats to list, in order, matching the search, each with whether another window + /// holds it; in a hub window only those of the side shown. fn listed_threads( chats: &crate::roster::ChatRoster, history: &super::history_column::HistoryColumn, + hub_window: bool, cx: &App, - ) -> Vec> { + ) -> Vec<(Entity, bool)> { + let side = history.side(); let query = history.query(cx); - let chats_owner = chats; - let chats: Vec<(bool, u64)> = chats + let threads: Vec<(Entity, bool)> = chats .iter() - .map(|thread| { - ( - thread.read(cx).document().pinned(), - thread.read(cx).document().updated(), - ) + .map(|thread| (thread.clone(), false)) + .chain( + history + .others() + .listed(cx) + .into_iter() + .map(|thread| (thread, true)), + ) + .collect(); + let keys: Vec<(bool, u64)> = threads + .iter() + .map(|(thread, _)| { + let document = thread.read(cx).document(); + (document.pinned(), document.updated()) }) .collect(); - let threads = chats_owner.iter().collect::>(); - order(&chats) + order(&keys) .into_iter() - .map(|index| threads[index]) - .filter(|thread| { + .map(|index| threads[index].clone()) + .filter(|(thread, _)| { + let thread = thread.read(cx); + !hub_window || HistorySide::of(thread.document().agent_id()) == side + }) + .filter(|(thread, _)| { let thread = thread.read(cx); query.is_empty() || thread.title().to_lowercase().contains(&query) || thread.transcript().mentions(&query) || history.matches(thread.document().chat_id()) }) - .cloned() .collect() } + /// What the listed rows show, so a chat's change that none of them shows (a draft + /// keystroke, a streamed chunk) leaves the history as it is. A search matches + /// transcripts, so while one is typed there is no stamp. + pub(super) fn stamp( + chats: &crate::roster::ChatRoster, + history: &super::history_column::HistoryColumn, + cx: &App, + ) -> Option { + use std::hash::{Hash as _, Hasher as _}; + if !history.query(cx).is_empty() { + return None; + } + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + history.side().hash(&mut hasher); + chats + .current() + .map(|current| current.entity_id()) + .hash(&mut hasher); + let others = history.others().listed(cx); + for thread in chats.iter().chain(&others) { + let chat = thread.read(cx); + ( + thread.entity_id(), + chat.title(), + chat.document().pinned(), + chat.document().updated(), + chat.model(), + chat.session_generating(), + chat.document().agent_id(), + chat.draft_text().is_some(), + chat.history_entries().is_empty(), + chat.archive_summary().map(|row| row.has_content), + ) + .hash(&mut hasher); + } + Some(hasher.finish()) + } + /// The switch between the chats of this computer and those of the hub's agents. + fn sides(side: HistorySide, cx: &mut Context) -> gpui_kit::AnyElement { + let button = |id: &'static str, label: &'static str, shown: HistorySide| { + Button::new(id) + .small() + .ghost() + .label(label) + .selected(side == shown) + .on_click(cx.listener(move |panel, _, _, cx| panel.show_history_side(shown, cx))) + }; + h_flex() + .id("agent-history-sides") + .test_support() + .gap_1() + .pb_1() + .child(button("agent-history-local", "Local", HistorySide::Local)) + .child(button( + "agent-history-server", + "Server", + HistorySide::Server, + )) + .into_any_element() + } pub(super) fn render( chats: &crate::roster::ChatRoster, - history_owner: &super::history_column::HistoryColumn, - agents: &AgentsHandle, + column: &super::history_column::HistoryColumn, + (agents, hub_window): (&AgentsHandle, bool), now: u64, cx: &mut Context, ) -> gpui_kit::AnyElement { @@ -284,7 +399,10 @@ impl HistoryProjection { .overflow_y_scroll() .p_2() .gap_1(); - if chats.is_empty() { + if hub_window { + history = history.child(Self::sides(column.side(), cx)); + } + if chats.is_empty() && column.others().listed(cx).is_empty() { return history .child( div() @@ -299,14 +417,19 @@ impl HistoryProjection { } history = history.child( div().pb_1().child( - history_owner + column .query_input() .prefix(gpui_kit::component::Icon::new(IconName::Search).small()) .cleanable(true), ), ); - let listed = Self::listed_threads(chats, history_owner, cx); + let listed = Self::listed_threads(chats, column, hub_window, cx); if listed.is_empty() { + let empty = match (column.query(cx).is_empty(), hub_window, column.side()) { + (false, ..) | (true, false, _) => "No chats match", + (true, true, HistorySide::Local) => "No chats with agents on this computer", + (true, true, HistorySide::Server) => "No chats with the server's agents", + }; history = history.child( div() .id("agent-history-no-match") @@ -314,29 +437,34 @@ impl HistoryProjection { .px_1() .text_xs() .text_color(cx.theme().muted_foreground) - .child("No chats match"), + .child(empty), ); } - for thread in listed { - history = history.child(Self::render_history_row( - &thread, - chats, - history_owner, - agents, - now, - cx, - )); + // Resolved once: resolving builds the whole agent list from the catalog and settings. + let available = agents.available(cx); + // Pinned first, then the most recently changed. + let mut rows: Vec<((bool, u64), HistoryRow)> = Vec::with_capacity(listed.len()); + for (thread, elsewhere) in listed { + let document = thread.read(cx).document(); + let key = (document.pinned(), document.updated()); + let mut row = Self::local_row(&thread, chats, column, (agents, &available), now, cx); + row.elsewhere = elsewhere; + rows.push((key, row)); + } + rows.sort_by_key(|(key, _)| std::cmp::Reverse(*key)); + for (_, row) in rows { + history = history.child(HistoryRowRenderer::render(row, cx)); } history.into_any_element() } - fn render_history_row( + fn local_row( thread: &Entity, chats: &crate::roster::ChatRoster, history: &super::history_column::HistoryColumn, - agents: &AgentsHandle, + (agents, available): (&AgentsHandle, &AvailableAgents), now: u64, - cx: &mut Context, - ) -> gpui_kit::AnyElement { + cx: &App, + ) -> HistoryRow { let id = thread.entity_id(); let chat = thread.read(cx); let when = if chat.archive_summary().is_none_or(|row| !row.has_content) @@ -349,7 +477,7 @@ impl HistoryProjection { now.saturating_sub(chat.updated_at()), ))) }; - let row = HistoryRow { + HistoryRow { id, title: chat.title().to_string(), pinned: chat.document().pinned(), @@ -359,10 +487,10 @@ impl HistoryProjection { when, model: chat.model().unwrap_or_default(), generating: chat.session_generating(), - agent: agents.profile(chat.document().agent_id(), cx), + agent: agents.profile_in(available, chat.document().agent_id()), rename: history.rename_input(id), - }; - HistoryRowRenderer::render(row, cx) + elsewhere: false, + } } } diff --git a/crates/agent/src/panel/history_column.rs b/crates/agent/src/panel/history_column.rs index d360312b..ea9a2aa3 100644 --- a/crates/agent/src/panel/history_column.rs +++ b/crates/agent/src/panel/history_column.rs @@ -1,4 +1,4 @@ -//! Search, rename and display-clock lifetimes of the history column. +//! Search, rename, the listed side and display-clock lifetimes of the history column. use super::AgentPanel; use crate::runtime::HistoryReader; use gpui_kit::{ @@ -17,6 +17,26 @@ use std::{ time::Duration, }; +/// Which chats a hub window's history lists: those of agents on this computer, or those of +/// the hub's agents, which run on the server. Other windows have no hub chats and list all. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub(super) enum HistorySide { + #[default] + Local, + Server, +} + +impl HistorySide { + /// The side of a chat of `agent`. + pub(super) fn of(agent: &str) -> Self { + if nocterm_ai::agents::is_hub_agent(agent) { + Self::Server + } else { + Self::Local + } + } +} + #[derive(Clone)] pub(super) struct SearchTicket { query: String, @@ -111,9 +131,11 @@ struct HistoryClock { } pub(super) struct HistoryColumn { input: Entity, + side: HistorySide, search: SearchState, rename: Option, tick: Option, + others: super::other_windows::OtherWindowChats, _bindings: Vec, } impl HistoryColumn { @@ -126,12 +148,34 @@ impl HistoryColumn { }); Self { input, + side: HistorySide::default(), search: SearchState::default(), rename: None, tick: None, + others: Default::default(), _bindings: vec![subscription], } } + pub(super) fn switch_context(&mut self, window: &mut Window, cx: &mut Context) { + self.rename = None; + self.search = SearchState::default(); + self.input + .update(cx, |input, cx| input.set_value("", window, cx)); + } + pub(super) fn side(&self) -> HistorySide { + self.side + } + /// The chats other windows hold, listed here too. + pub(super) fn others(&self) -> &super::other_windows::OtherWindowChats { + &self.others + } + pub(super) fn others_mut(&mut self) -> &mut super::other_windows::OtherWindowChats { + &mut self.others + } + /// Lists `side`; returns whether that changed what is listed. + pub(super) fn show_side(&mut self, side: HistorySide) -> bool { + std::mem::replace(&mut self.side, side) != side + } pub(super) fn search_requested(&self, cx: &App) -> bool { !self.query(cx).is_empty() || !self.search.query.is_empty() } @@ -151,6 +195,7 @@ impl HistoryColumn { &mut self, ids: Vec, reader: HistoryReader, + admission: nocterm_agent_runtime::history_admission::HistoryAdmissionHandle, catalog: Vec<(EntityId, bool)>, cx: &mut Context, ) { @@ -160,29 +205,31 @@ impl HistoryColumn { let cancellation = Arc::new(AtomicBool::new(false)); let flag = cancellation.clone(); let query = ticket.query.clone(); - let work = cx.background_executor().spawn(async move { + let cancellation = SearchCancellation(cancellation); + self.search.task = Some(cx.spawn(async move |panel, cx| { + let _cancel = cancellation; let mut matches = HashSet::new(); for id in ids { if flag.load(Ordering::Acquire) { break; } - let (loaded, _permit) = reader.load(id.clone()).await; - if let Ok(chat) = loaded { - let transcript = nocterm_ai::thread::ThreadState { - entries: chat.entries, - ..Default::default() - }; - if transcript.mentions(&query) { - matches.insert(id); + let loading = + cx.update(|cx| reader.load_admitted(id.clone(), admission.clone(), cx)); + let loaded = loading.await; + let mut found = false; + loaded.publish(|loaded, _permit, _payload| { + if let Ok(chat) = loaded { + let transcript = nocterm_ai::thread::ThreadState { + entries: chat.entries, + ..Default::default() + }; + found = transcript.mentions(&query); } + }); + if found { + matches.insert(id); } } - matches - }); - let cancellation = SearchCancellation(cancellation); - self.search.task = Some(cx.spawn(async move |panel, cx| { - let _cancel = cancellation; - let matches = work.await; let _ = panel.update(cx, |panel, cx| { panel.on_archive_search_completed(ticket, matches, cx) }); diff --git a/crates/agent/src/panel/image_picker.rs b/crates/agent/src/panel/image_picker.rs deleted file mode 100644 index 76696f7b..00000000 --- a/crates/agent/src/panel/image_picker.rs +++ /dev/null @@ -1,14 +0,0 @@ -//! Picker delivery is bound to the original chat and composer revision. -use gpui_kit::EntityId; -pub(super) struct ImagePickerOperation { - chat: Option, - revision: u64, -} -impl ImagePickerOperation { - pub(super) fn new(chat: Option, revision: u64) -> Self { - Self { chat, revision } - } - pub(super) fn current(&self, chat: Option, revision: u64) -> bool { - self.chat == chat && self.revision == revision - } -} diff --git a/crates/agent/src/panel/image_pipeline.rs b/crates/agent/src/panel/image_pipeline.rs index 19914043..fc910f3e 100644 --- a/crates/agent/src/panel/image_pipeline.rs +++ b/crates/agent/src/panel/image_pipeline.rs @@ -1,15 +1,80 @@ -//! Preview requests retain cache generation and identity across background delivery. +//! Bounded previews retain immutable sources, so pointer identities cannot be reused. use crate::ImageDeps; use gpui_kit::{Image, Task}; -use nocterm_ai::images::{ImageInspector, ImageThumbnailer}; -use std::{collections::HashMap, sync::Arc}; +use nocterm_ai::{ + images::{ImageInspector, MAX_PREVIEW_BYTES}, + model::content::ImageData, +}; +use std::{cell::Cell, collections::HashMap, sync::Arc}; pub(super) type ImageKey = (usize, usize, bool); +const MAX_ENTRIES: usize = 32; +const MAX_BYTES: usize = 64 * 1024 * 1024; +const MAX_PREVIEW_RESIDENT: usize = MAX_PREVIEW_BYTES + 640 * 640 * 8; +const MAX_LOADING: usize = 4; + +#[derive(Clone)] +pub(super) enum PreviewSource { + Encoded(ImageData), + Original(Arc<[u8]>), +} +impl PreviewSource { + pub(super) fn retained_bytes(&self) -> usize { + match self { + Self::Encoded(data) => data.retained_bytes(), + Self::Original(data) => data.len().saturating_add( + 2 * std::mem::size_of::() + std::mem::align_of::() - 1, + ), + } + } + pub(super) fn key(&self) -> ImageKey { + match self { + Self::Encoded(data) => (data.as_ptr() as usize, data.len(), true), + Self::Original(data) => (data.as_ptr() as usize, data.len(), false), + } + } + pub(super) fn decode(&self) -> Result, super::entries::PreviewError> { + match self { + Self::Original(data) => Ok(data.clone()), + Self::Encoded(data) => { + use base64::Engine as _; + if data.len() > nocterm_ai::images::MAX_IMAGE_BYTES.div_ceil(3) * 4 { + return Err(nocterm_ai::images::ImageCodecError::SourceSize.into()); + } + Ok(base64::engine::general_purpose::STANDARD + .decode(data)? + .into()) + } + } + } +} #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(super) struct PreviewTicket { generation: u64, request: u64, key: ImageKey, } +#[derive(Clone)] +pub(super) struct CompletedPreview { + image: Arc, + resident_bytes: usize, +} +impl CompletedPreview { + pub(super) fn from_encoded(preview: nocterm_ai::images::EncodedPreview) -> Self { + let (width, height) = preview.dimensions(); + let resident_bytes = preview.bytes().len() + width as usize * height as usize * 8; + Self { + image: Arc::new(Image::from_bytes( + gpui_kit::ImageFormat::Png, + preview.bytes().to_vec(), + )), + resident_bytes, + } + } + + fn into_ready(self) -> (PreviewState, usize) { + (PreviewState::Ready(self.image), self.resident_bytes) + } +} pub(super) enum PreviewState { Loading, Ready(Arc), @@ -18,17 +83,20 @@ pub(super) enum PreviewState { struct PreviewEntry { ticket: PreviewTicket, state: PreviewState, + source: PreviewSource, + last_used: Cell, task: Option>, + charge: usize, } struct PreviewCache { entries: HashMap, generation: u64, next_request: u64, + clock: Cell, } pub(super) struct ImagePipeline { cache: PreviewCache, - inspector: Arc, - thumbnailer: Arc, + deps: ImageDeps, } impl ImagePipeline { pub(super) fn new(deps: ImageDeps) -> Self { @@ -37,27 +105,60 @@ impl ImagePipeline { entries: HashMap::new(), generation: 0, next_request: 0, + clock: Cell::new(0), }, - inspector: deps.inspector(), - thumbnailer: deps.thumbnailer(), + deps, } } pub(super) fn inspector(&self) -> Arc { - self.inspector.clone() + self.deps.inspector() + } + pub(super) fn image_deps(&self) -> ImageDeps { + self.deps.clone() } - pub(super) fn thumbnailer(&self) -> Arc { - self.thumbnailer.clone() + pub(super) fn admit_work(&self) -> Option { + self.deps.admit_work() } pub(super) fn contains_key(&self, key: &ImageKey) -> bool { - self.cache.entries.contains_key(key) + self.state(key).is_some() } pub(super) fn state(&self, key: &ImageKey) -> Option<&PreviewState> { - self.cache.entries.get(key).map(|entry| &entry.state) + let entry = self.cache.entries.get(key)?; + let next = self.cache.clock.get().saturating_add(1); + self.cache.clock.set(next); + entry.last_used.set(next); + Some(&entry.state) } - pub(super) fn request(&mut self, key: ImageKey) -> Option { + pub(super) fn request(&mut self, source: PreviewSource) -> Option { + let key = source.key(); if self.contains_key(&key) { return None; } + let loading = self + .cache + .entries + .values() + .filter(|entry| matches!(entry.state, PreviewState::Loading)) + .count(); + if loading >= MAX_LOADING { + return None; + } + let charge = source.retained_bytes().saturating_add(MAX_PREVIEW_RESIDENT); + if charge > MAX_BYTES { + return None; + } + while self.cache.entries.len() >= MAX_ENTRIES + || self.charged_bytes().saturating_add(charge) > MAX_BYTES + { + let oldest = self + .cache + .entries + .iter() + .filter(|(_, entry)| !matches!(entry.state, PreviewState::Loading)) + .min_by_key(|(_, entry)| entry.last_used.get()) + .map(|(key, _)| *key)?; + self.cache.entries.remove(&oldest); + } self.cache.next_request = self .cache .next_request @@ -68,16 +169,24 @@ impl ImagePipeline { request: self.cache.next_request, key, }; + let last_used = self.cache.clock.get().saturating_add(1); + self.cache.clock.set(last_used); self.cache.entries.insert( key, PreviewEntry { ticket, state: PreviewState::Loading, + source, + last_used: Cell::new(last_used), task: None, + charge, }, ); Some(ticket) } + fn charged_bytes(&self) -> usize { + self.cache.entries.values().map(|entry| entry.charge).sum() + } pub(super) fn install_task(&mut self, ticket: PreviewTicket, task: Task<()>) { if let Some(entry) = self .cache @@ -88,7 +197,11 @@ impl ImagePipeline { entry.task = Some(task); } } - pub(super) fn complete(&mut self, ticket: PreviewTicket, image: Option>) -> bool { + pub(super) fn complete( + &mut self, + ticket: PreviewTicket, + image: Option, + ) -> bool { let Some(entry) = self .cache .entries @@ -100,7 +213,10 @@ impl ImagePipeline { if !matches!(entry.state, PreviewState::Loading) { return false; } - entry.state = image.map_or(PreviewState::Failed, PreviewState::Ready); + let (state, bytes) = image.map_or((PreviewState::Failed, 0), CompletedPreview::into_ready); + entry.charge = entry.source.retained_bytes().saturating_add(bytes); + entry.state = state; + entry.task = None; true } pub(super) fn clear(&mut self) { diff --git a/crates/agent/src/panel/image_pipeline/tests.rs b/crates/agent/src/panel/image_pipeline/tests.rs index 3db5be13..a6d60a41 100644 --- a/crates/agent/src/panel/image_pipeline/tests.rs +++ b/crates/agent/src/panel/image_pipeline/tests.rs @@ -5,19 +5,86 @@ fn pipeline() -> ImagePipeline { let codec = Arc::new(ScriptedImageCodec::default()); ImagePipeline::new(ImageDeps::new(codec.clone(), codec)) } +fn completed_preview() -> CompletedPreview { + let bytes = include_bytes!("../tests/fixtures/prompt.png").to_vec(); + let bounds = nocterm_ai::images::PreviewBounds::new(640, 640).unwrap(); + let preview = nocterm_ai::images::EncodedPreview::new(bytes.into(), bounds).unwrap(); + CompletedPreview::from_encoded(preview) +} #[test] fn cleared_generation_cannot_install_old_success_or_failure_over_a_reused_key() { let mut pipeline = pipeline(); - let key = (0, 0, false); - let old = pipeline.request(key).unwrap(); + let source = PreviewSource::Original(vec![7].into()); + let key = source.key(); + let old = pipeline.request(source.clone()).unwrap(); pipeline.clear(); - let current = pipeline.request(key).unwrap(); - let image = Arc::new(Image::from_bytes(gpui_kit::ImageFormat::Png, vec![1])); + let current = pipeline.request(source.clone()).unwrap(); + let image = completed_preview(); assert!(!pipeline.complete(old, Some(image.clone()))); assert!(!pipeline.complete(old, None)); assert!(matches!(pipeline.state(&key), Some(PreviewState::Loading))); assert!(pipeline.complete(current, Some(image))); assert!(matches!(pipeline.state(&key), Some(PreviewState::Ready(_)))); - assert!(pipeline.request(key).is_none()); + assert!(pipeline.request(source.clone()).is_none()); assert!(!pipeline.complete(current, None)); } + +#[test] +fn retained_source_prevents_aliasing_and_cloned_image_reuses_its_preview() { + let mut pipeline = pipeline(); + let source = PreviewSource::Encoded("old image".to_owned().into()); + let key = source.key(); + let ticket = pipeline.request(source.clone()).unwrap(); + assert!(pipeline.complete(ticket, None)); + assert!(pipeline.request(source).is_none()); + let other = PreviewSource::Encoded("new image".to_owned().into()); + assert_ne!(key, other.key()); + assert!(pipeline.request(other).is_some()); +} +#[test] +fn preview_admission_bounds_concurrent_decodes_and_reclaims_finished_sources() { + let mut pipeline = pipeline(); + let first = PreviewSource::Original(vec![0; 5 * 1024 * 1024].into()); + let key = first.key(); + let ticket = pipeline.request(first.clone()).unwrap(); + assert!(pipeline.complete(ticket, None)); + for value in 1..=16 { + let source = PreviewSource::Original(vec![value as u8; 5 * 1024 * 1024].into()); + if let Some(ticket) = pipeline.request(source) { + assert!(pipeline.complete(ticket, None)); + } + } + assert!(pipeline.state(&key).is_none()); + pipeline.clear(); + for value in 0..MAX_LOADING { + assert!( + pipeline + .request(PreviewSource::Original(vec![value as u8].into())) + .is_some() + ); + } + assert!( + pipeline + .request(PreviewSource::Original(vec![99].into())) + .is_none() + ); +} + +#[test] +fn small_completed_previews_release_their_decode_reservation() { + let mut pipeline = pipeline(); + let mut sources = Vec::new(); + for value in 0..16u8 { + let source = PreviewSource::Original(vec![value; 1024].into()); + let ticket = pipeline.request(source.clone()).unwrap(); + let image = completed_preview(); + assert!(pipeline.complete(ticket, Some(image))); + sources.push(source); + } + for source in sources { + assert!(matches!( + pipeline.state(&source.key()), + Some(PreviewState::Ready(_)) + )); + } +} diff --git a/crates/agent/src/panel/image_preparation.rs b/crates/agent/src/panel/image_preparation.rs index f42bf872..0cb78ae1 100644 --- a/crates/agent/src/panel/image_preparation.rs +++ b/crates/agent/src/panel/image_preparation.rs @@ -10,6 +10,7 @@ pub(super) struct ImagePreparation { thread: WeakEntity, ticket: Ticket, work: BoxFuture<'static, Result, ImageAttachError>>, + permit: crate::image_deps::ImageWorkPermit, } impl ImagePreparation { pub(super) fn new( @@ -17,12 +18,14 @@ impl ImagePreparation { thread: WeakEntity, ticket: Ticket, work: BoxFuture<'static, Result, ImageAttachError>>, + permit: crate::image_deps::ImageWorkPermit, ) -> Self { Self { binding, thread, ticket, work, + permit, } } pub(super) fn launch(self, cx: &mut Context) { @@ -31,8 +34,12 @@ impl ImagePreparation { thread, ticket, work, + permit, } = self; - let future = cx.background_executor().spawn(work); + let future = cx.background_executor().spawn(async move { + let _permit = permit; + work.await + }); cx.spawn(async move |panel, cx| { let result = future.await; let _ = panel.update(cx, |panel, cx| { diff --git a/crates/agent/src/panel/images.rs b/crates/agent/src/panel/images.rs index fc92eb49..a9cda4ce 100644 --- a/crates/agent/src/panel/images.rs +++ b/crates/agent/src/panel/images.rs @@ -1,5 +1,7 @@ //! Image preparation runs outside the UI and belongs to one composer revision. -use nocterm_ai::images::{ImageCollectionError, MAX_IMAGE_BYTES, PromptImage, PromptImageError}; +use nocterm_ai::images::{ + ImageCollectionBudget, ImageCollectionError, MAX_IMAGE_BYTES, PromptImage, PromptImageError, +}; use std::{io::Read as _, path::PathBuf}; /// Why attaching images to the composer failed. @@ -13,19 +15,57 @@ pub(super) enum ImageAttachError { Invalid(#[from] PromptImageError), #[error(transparent)] Collection(#[from] ImageCollectionError), + #[error("Image processing is busy. Try again when the current images are ready.")] + Busy, } pub(super) fn read_images( paths: Vec, inspector: &dyn nocterm_ai::images::ImageInspector, + mut budget: ImageCollectionBudget, ) -> Result, ImageAttachError> { + budget.check_count(paths.len())?; paths .into_iter() .map(|path| { let mut file = std::fs::File::open(path)?; let mut bytes = Vec::new(); std::io::Read::take(&mut file, (MAX_IMAGE_BYTES + 1) as u64).read_to_end(&mut bytes)?; + budget.admit(bytes.len())?; Ok(PromptImage::validate(bytes, inspector)?) }) .collect() } + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_ai::testing::ScriptedImageCodec; + #[test] + fn excessive_selection_is_refused_before_opening_any_path() { + let codec = ScriptedImageCodec::default(); + let budget = ImageCollectionBudget::remaining(&[]).unwrap(); + let paths = vec![PathBuf::from("missing-image.png"); nocterm_ai::images::MAX_IMAGES + 1]; + assert!(matches!( + read_images(paths, &codec, budget), + Err(ImageAttachError::Collection(_)) + )); + assert!(codec.requests().is_empty()); + } + #[test] + fn aggregate_bytes_are_refused_before_the_next_decode() { + let root = tempfile::tempdir().unwrap(); + let path = root.path().join("image.png"); + std::fs::write(&path, b"uninspected bytes").unwrap(); + let codec = ScriptedImageCodec::default(); + let mut budget = ImageCollectionBudget::remaining(&[]).unwrap(); + budget + .admit(nocterm_ai::images::MAX_TOTAL_IMAGE_BYTES) + .unwrap(); + assert!(matches!( + read_images(vec![path], &codec, budget), + Err(ImageAttachError::Collection(_)) + )); + assert!(codec.requests().is_empty()); + } +} diff --git a/crates/agent/src/panel/layout.rs b/crates/agent/src/panel/layout.rs index 50f4cac9..cb33a11f 100644 --- a/crates/agent/src/panel/layout.rs +++ b/crates/agent/src/panel/layout.rs @@ -1,4 +1,5 @@ -//! The history split retains its shared layout and reversible widening amount. +//! The history split retains its shared layout and reversible widening amount; the +//! panel's regions are views it keeps. use super::AgentPanel; use gpui_kit::{ AnyElement, App, AppContext as _, Context, Entity, Pixels, Subscription, TestSupportExt as _, @@ -87,6 +88,7 @@ pub(super) struct PanelLayout { layout: LayoutHandle, _bindings: Vec, refresh: [SplitRefresh; 2], + parts: super::parts::PanelParts, } impl PanelLayout { pub(super) fn new(layout: LayoutHandle, cx: &mut Context) -> Self { @@ -119,11 +121,19 @@ impl PanelLayout { layout, _bindings: bindings, refresh: std::array::from_fn(|_| SplitRefresh::Idle), + parts: super::parts::PanelParts::new(cx), } } pub(super) fn history(&self) -> bool { self.history } + /// The regions the panel draws as views of their own. + pub(super) fn parts(&self) -> &super::parts::PanelParts { + &self.parts + } + pub(super) fn parts_mut(&mut self) -> &mut super::parts::PanelParts { + &mut self.parts + } pub(super) fn maximized(&self) -> bool { self.maximized } diff --git a/crates/agent/src/panel/lifecycle.rs b/crates/agent/src/panel/lifecycle.rs index 3e8efc14..88fa18f4 100644 --- a/crates/agent/src/panel/lifecycle.rs +++ b/crates/agent/src/panel/lifecycle.rs @@ -23,13 +23,15 @@ impl AgentPanel { .filter(|thread| thread.read(cx).is_archived()) .map(|thread| thread.read(cx).document().chat_id().to_owned()) .collect(); - let reader = self.host.runtime().history_reader(cx); + let reader = self.chats.host().runtime().history_reader(cx); let catalog = self .chats .iter() .map(|thread| (thread.entity_id(), thread.read(cx).is_archived())) .collect(); - self.history.start_search(ids, reader, catalog, cx); + let admission = self.chats.host().runtime().history_admission(cx); + self.history + .start_search(ids, reader, admission, catalog, cx); cx.notify(); } pub(super) fn on_archive_search_completed( @@ -61,7 +63,6 @@ impl AgentPanel { { self.leave_composer_with(true, cx); } - self.composer.removed_chats(&removed); self.chats.remove(&removed); if active.is_some_and(|id| removed.contains(&id)) { self.reset_chat_view(cx); @@ -138,12 +139,27 @@ impl AgentPanel { }); return; } - let thread = self.chats.fork(&source, last, cx); + let thread = match self.chats.fork(&source, last, cx) { + Ok(thread) => thread, + Err(error) => { + self.notices.set_error(Some(error.to_string())); + cx.notify(); + return; + } + }; let slot = self.track(&thread, window, cx); let id = thread.entity_id(); - self.chats.append(slot); - self.on_history_catalog_changed(cx); - self.open_thread(id, cx); + match self.chats.append_origin(slot, cx) { + Ok(true) => { + self.on_history_catalog_changed(cx); + self.open_thread(id, cx); + } + Ok(false) => {} + Err(error) => { + self.notices.set_error(Some(error.to_string())); + cx.notify(); + } + } } pub(crate) fn fork_loaded_archive( &mut self, @@ -152,8 +168,30 @@ impl AgentPanel { window: &mut Window, cx: &mut Context, ) { - if self.chats.contains(source) { - self.fork_thread_at(source, last, window, cx); + let Some(original) = self + .chats + .find_any(source) + .cloned() + .filter(|thread| !thread.read(cx).is_archived()) + else { + return; + }; + let thread = match self.chats.fork(&original, last, cx) { + Ok(thread) => thread, + Err(error) => { + tracing::warn!(%error,"could not finish original archived fork"); + return; + } + }; + let id = thread.entity_id(); + let slot = self.track(&thread, window, cx); + match self.chats.append_origin(slot, cx) { + Ok(true) => { + self.on_history_catalog_changed(cx); + self.open_thread(id, cx); + } + Ok(false) => {} + Err(error) => tracing::warn!(%error,"original fork destination is closed"), } } pub(super) fn set_pinned(&mut self, id: EntityId, pinned: bool, cx: &mut Context) { @@ -280,8 +318,14 @@ impl AgentPanel { if let Some(thread) = self.current() { thread.update(cx, |thread, cx| thread.set_draft(draft, cx)); } - self.retain_threads(|_, _| false, cx); - if !self.chats.is_empty() { + if self.composer.editing_chat().is_some() { + self.leave_composer_with(true, cx); + } + let retained = self.chats.clear_all(cx); + if let Some(error) = retained.into_error() { + self.notices.set_error(Some(error.to_string())); + } + if self.chats.has_retained_chats() { return; } self.composer.clear(); @@ -293,11 +337,15 @@ impl AgentPanel { self.sync_approval_attention(window, cx); } pub(super) fn new_thread(&mut self, id: String, window: &mut Window, cx: &mut Context) { - let terminal = self - .host - .workspace() - .upgrade() - .and_then(|workspace| workspace.read(cx).active_terminal(cx)); + // The new chat is listed on its own side of a hub window's history. + self.history + .show_side(super::history_column::HistorySide::of(&id)); + let terminal = self.chats.host().seat().terminals(cx).and_then(|entries| { + entries + .into_iter() + .find(|entry| entry.active) + .map(|entry| entry.item) + }); self.open_started( ChatStart::New { agent: id, @@ -311,17 +359,23 @@ impl AgentPanel { // An unknown agent leaves the composer and drafts alone. let agent = match &start { ChatStart::New { agent, .. } => agent.as_str(), - ChatStart::Restart(data) => data.agent_id(), + ChatStart::Restart { data, .. } => data.agent_id(), }; if self.host.agents().get(agent, cx).is_none() { return; } + let thread = match self.chats.start(start, cx) { + Ok(Some(thread)) => thread, + Ok(None) => return, + Err(error) => { + self.notices.set_error(Some(error.to_string())); + cx.notify(); + return; + } + }; self.leave_composer(cx); // An empty chat left behind is not history. self.discard_drafts(None, cx); - let Some(thread) = self.chats.start(start, cx) else { - return; - }; let id = thread.entity_id(); let slot = self.track(&thread, window, cx); self.chats.append(slot); @@ -342,24 +396,34 @@ impl AgentPanel { // Release ACP even if another observer still holds the old document entity. old.update(cx, |thread, cx| thread.release_resources(cx)); // Edits and provider tool statuses are final before taking the restart snapshot. - let data = old.read(cx).restart_data(); - self.open_started(ChatStart::Restart(Box::new(data)), window, cx); + let data = match old.update(cx, |thread, cx| thread.restart_data(cx)) { + Ok(data) => data, + Err(error) => { + self.notices.set_error(Some(error.to_string())); + cx.notify(); + return; + } + }; + self.open_started( + ChatStart::Restart { + data: Box::new(data), + expected_owner: old.entity_id(), + }, + window, + cx, + ); let Some(thread) = self .current() .filter(|thread| thread.entity_id() != old.entity_id()) else { return; }; - self.host - .runtime() - .register_document(crate::thread::client(&thread), cx); thread.update(cx, |thread, cx| { thread.save(cx); cx.notify(); }); self.load_composer(cx); let old = old.entity_id(); - self.host.runtime().unregister_document(old, cx); self.retain_threads(|thread, _| thread.entity_id() != old, cx); } /// Shows the chats saved in earlier runs, oldest first, before the @@ -413,6 +477,7 @@ impl AgentPanel { ) { if !self.chats.contains(loaded.entity_id()) { loaded.update(cx, |thread, _| thread.release_history_delivery()); + self.chats.release_quiet_hidden(cx); return; } self.trim_resident_history(cx); @@ -425,6 +490,24 @@ impl AgentPanel { window.defer(cx, move |window, cx| { let _ = panel.update(cx, |panel, cx| panel.sync_approval_attention(window, cx)); }); + // The chat on screen draws its change at once: the transcript only when what it + // shows changed, so typing a draft leaves it. The history waits for the coalesced + // redraw. + if self + .current() + .is_some_and(|current| current.entity_id() == loaded.entity_id()) + { + // The usage card over the transcript shows the chat's usage. + let rows = loaded.read(cx).has_changed_rows() || self.menus.is_open(&MenuKind::Usage); + let stamp = super::parts::transcript_stamp(loaded.read(cx)); + let parts = self.layout.parts(); + parts.transcript_changed(rows, stamp, cx); + parts.notify(super::parts::PartKind::Footer, cx); + } + self.schedule_history_redraw(cx); + } + /// Redraws the history once the changes of the next frames have arrived. + fn schedule_history_redraw(&mut self, cx: &mut Context) { let Some(ticket) = self.notices.request_redraw() else { return; }; @@ -437,13 +520,90 @@ impl AgentPanel { }); self.notices.install_redraw(&ticket, task); } + /// What the runtime tells every window about the chats windows hold. + pub(super) fn on_holder_event( + &mut self, + event: nocterm_agent_runtime::HolderEvent, + window: &mut Window, + cx: &mut Context, + ) { + use nocterm_agent_runtime::HolderEvent; + match event { + HolderEvent::Changed => { + if self.history.others_mut().mark_stale() { + cx.defer_in(window, |panel, _, cx| panel.refresh_other_windows(cx)); + } + } + HolderEvent::Reveal { chat } if self.chats.contains(chat) => { + self.reveal_chat(chat, window, cx); + } + HolderEvent::Moved { chat } if self.chats.contains(chat) => { + self.retain_threads(|thread, _| thread.entity_id() != chat, cx); + self.refresh_other_windows(cx); + } + HolderEvent::Reveal { .. } | HolderEvent::Moved { .. } => {} + } + } + /// Lists the chats other windows hold anew. + pub(super) fn refresh_other_windows(&mut self, cx: &mut Context) { + let chats = self.chats.held_elsewhere(cx); + if self.history.others_mut().set(chats, cx) { + self.schedule_history_redraw(cx); + } + } + /// A chat another window holds changed: its row follows on the coalesced redraw. + pub(super) fn on_other_window_chat_changed(&mut self, cx: &mut Context) { + self.schedule_history_redraw(cx); + } + /// Opens chat `id`, which another window holds: here when it neither works nor shows + /// there, otherwise in that window, which comes forward. + pub(super) fn open_from_other_window( + &mut self, + id: EntityId, + window: &mut Window, + cx: &mut Context, + ) { + let Some(chat) = self.history.others().find(id) else { + return; + }; + match self.chats.take_over(&chat, cx) { + Ok(Some(thread)) => { + let opened = thread.entity_id(); + let slot = self.track(&thread, window, cx); + self.chats.append(slot); + self.refresh_other_windows(cx); + self.on_history_catalog_changed(cx); + self.open_thread(opened, cx); + } + Ok(None) => self.host.runtime().reveal_chat(id, cx), + Err(error) => self.notices.set_error(Some(error.to_string())), + } + cx.notify(); + } + /// Shows chat `chat`, which another window asked for, and brings this window forward. + fn reveal_chat(&mut self, chat: EntityId, window: &mut Window, cx: &mut Context) { + if self + .current() + .is_none_or(|current| current.entity_id() != chat) + { + self.open_thread(chat, cx); + } + if let Some(workspace) = self.host.workspace().upgrade() { + workspace.update(cx, |workspace, cx| { + workspace.open_panel(&crate::plugin::CHATS.id, window, cx) + }); + } + window.activate_window(); + cx.notify(); + } pub(super) fn on_coalesced_redraw( &mut self, ticket: &std::sync::Arc<()>, cx: &mut Context, ) { if self.notices.finish_redraw(ticket) { - cx.notify(); + let stamp = super::history::HistoryProjection::stamp(&self.chats, &self.history, cx); + self.layout.parts().history_changed(stamp, cx); } } } diff --git a/crates/agent/src/panel/menu.rs b/crates/agent/src/panel/menu.rs index 72bde70e..3753c33b 100644 --- a/crates/agent/src/panel/menu.rs +++ b/crates/agent/src/panel/menu.rs @@ -192,7 +192,7 @@ impl ConfigMenuRenderer { &frame.focus, window, cx, - )) + )); } Some(MenuKind::Context) => { menu = menu.child( @@ -237,6 +237,12 @@ impl ConfigMenuRenderer { ) -> String { match kind { Some(MenuKind::Agents) => "New agent".into(), + Some(MenuKind::Context) + if thread + .is_some_and(|thread| super::chat_servers::takes_servers(thread.read(cx))) => + { + "Servers the agent may sign in to".into() + } Some(MenuKind::Context) => "Servers and terminals".into(), Some(MenuKind::Modes) => "Agent mode".into(), Some(MenuKind::Usage) | None => String::new(), diff --git a/crates/agent/src/panel/message_actions.rs b/crates/agent/src/panel/message_actions.rs index d3c24d2a..1a21317b 100644 --- a/crates/agent/src/panel/message_actions.rs +++ b/crates/agent/src/panel/message_actions.rs @@ -65,7 +65,7 @@ pub(super) fn escape_markdown(text: &str) -> String { } pub(super) struct MessageRowActions { - thread: EntityId, + thread: Option, index: usize, text: String, time: Option, @@ -73,7 +73,7 @@ pub(super) struct MessageRowActions { } impl MessageRowActions { pub(super) fn new( - thread: EntityId, + thread: Option, index: usize, text: String, time: Option, @@ -113,16 +113,18 @@ impl MessageRowActions { cx.write_to_clipboard(ClipboardItem::new_string(text.clone())) }), ) - .child( - Button::new(("fork-message", index)) - .ghost() - .xsmall() - .icon(IconName::GitFork) - .tooltip("Fork chat from here") - .on_click(cx.listener(move |this, _, window, cx| { - this.fork_thread_at(thread, Some(index), window, cx) - })), - ) + .when_some(thread, |row, thread| { + row.child( + Button::new(("fork-message", index)) + .ghost() + .xsmall() + .icon(IconName::GitFork) + .tooltip("Fork chat from here") + .on_click(cx.listener(move |this, _, window, cx| { + this.fork_thread_at(thread, Some(index), window, cx) + })), + ) + }) .when_some(time, |row, time| { row.child( div() diff --git a/crates/agent/src/panel/message_list.rs b/crates/agent/src/panel/message_list.rs index 1c48c3c3..79f5815a 100644 --- a/crates/agent/src/panel/message_list.rs +++ b/crates/agent/src/panel/message_list.rs @@ -1,18 +1,21 @@ //! Transcript presentation binds reveal, navigation and previews to one visible chat. use super::{ AgentPanel, - image_pipeline::{ImageKey, ImagePipeline, PreviewState, PreviewTicket}, + image_pipeline::{ + CompletedPreview, ImageKey, ImagePipeline, PreviewSource, PreviewState, PreviewTicket, + }, + message_markdown::MessageMarkdown, navigation::MessageNavigation, stream::StreamReveal, }; use gpui_kit::{ - App, AppContext as _, Context, Entity, EntityId, Image, Task, - component::message_scroller::{MessageScroller, MessageScrollerState}, -}; -use nocterm_ai::{ - images::{ImageInspector, ImageThumbnailer}, - thread::Entry, + App, AppContext as _, Context, Entity, EntityId, Task, WeakEntity, + component::{ + message_scroller::{MessageScroller, MessageScrollerState}, + text::TextViewState, + }, }; +use nocterm_ai::{images::ImageInspector, thread::Entry}; use std::{collections::HashSet, sync::Arc, time::Duration}; #[derive(Clone)] @@ -39,6 +42,8 @@ pub(super) struct MessageList { navigation: MessageNavigation, expanded: HashSet, binding: Option, + thread: Option>, + markdown: MessageMarkdown, } impl MessageList { pub(super) fn new(images: crate::ImageDeps, cx: &mut Context) -> Self { @@ -51,6 +56,8 @@ impl MessageList { navigation: MessageNavigation::default(), expanded: HashSet::new(), binding: None, + thread: None, + markdown: MessageMarkdown::default(), } } pub(super) fn bind_chat(&mut self, chat: EntityId, cx: &mut Context) { @@ -70,6 +77,27 @@ impl MessageList { pub(super) fn binding(&self) -> Option { self.binding.clone() } + pub(super) fn bind_thread( + &mut self, + thread: &Entity, + cx: &mut Context, + ) { + let chat = thread.entity_id(); + if self.bound_to(chat) && self.thread.is_some() { + return; + } + self.bind_chat(chat, cx); + self.thread = Some(thread.downgrade()); + } + pub(super) fn bound_to(&self, chat: EntityId) -> bool { + self.binding + .as_ref() + .is_some_and(|binding| binding.chat == chat) + } + /// The chat the list shows, while it is open. + pub(super) fn thread(&self) -> Option> { + self.thread.as_ref()?.upgrade() + } fn current(&self, binding: &ActiveListBinding) -> bool { self.binding .as_ref() @@ -84,6 +112,8 @@ impl MessageList { self.expanded.clear(); self.pipeline.clear(); self.binding = None; + self.thread = None; + self.markdown.clear(); } pub(super) fn observe_rows( &mut self, @@ -93,10 +123,12 @@ impl MessageList { live: bool, reduced: bool, ) -> MessageRows { + self.markdown.invalidate(dirty); let previous = self.row_count; self.row_count = entries.len(); self.navigation.observe(chat, entries, dirty); let mut changed = dirty.to_vec(); + changed.extend(entries.len()..self.row_count.min(previous)); changed.extend(self.reveal.observe(entries, dirty, live, reduced)); MessageRows { previous, @@ -112,6 +144,7 @@ impl MessageList { } else { self.scroller .update(cx, |list, cx| list.reset(rows.count, cx)); + self.markdown.retain(rows.count); } } self.remeasure_rows(rows.changed, cx); @@ -189,17 +222,36 @@ impl MessageList { pub(super) fn revealed_text(&self, row: usize, text: &str) -> String { self.reveal.text(row, text) } + pub(super) fn revealed_slice<'a>(&self, row: usize, text: &'a str) -> &'a str { + self.reveal.slice(row, text) + } + pub(super) fn markdown_current(&self, row: usize, source_len: usize) -> bool { + self.markdown.current(row, source_len) + } pub(super) fn revealing(&self) -> bool { self.reveal.pending() } + /// Keeps the row's parsed Markdown current with what its entry draws now. + pub(super) fn prepare_markdown( + &mut self, + row: usize, + text: String, + source_len: usize, + cx: &mut Context, + ) { + self.markdown.prepare(row, text, source_len, cx); + } + pub(super) fn markdown(&self, row: usize) -> Option<&Entity> { + self.markdown.state(row) + } pub(super) fn preview_state(&self, key: &ImageKey) -> Option<&PreviewState> { self.pipeline.state(key) } pub(super) fn has_preview(&self, key: &ImageKey) -> bool { self.pipeline.contains_key(key) } - pub(super) fn request_preview(&mut self, key: ImageKey) -> Option { - self.pipeline.request(key) + pub(super) fn request_preview(&mut self, source: PreviewSource) -> Option { + self.pipeline.request(source) } pub(super) fn install_preview_task(&mut self, ticket: PreviewTicket, task: Task<()>) { self.pipeline.install_task(ticket, task); @@ -209,7 +261,7 @@ impl MessageList { binding: &ActiveListBinding, ticket: PreviewTicket, row: Option, - image: Option>, + image: Option, cx: &mut Context, ) -> bool { if !self.current(binding) || !self.pipeline.complete(ticket, image) { @@ -223,8 +275,11 @@ impl MessageList { pub(super) fn inspector(&self) -> Arc { self.pipeline.inspector() } - pub(super) fn thumbnailer(&self) -> Arc { - self.pipeline.thumbnailer() + pub(super) fn image_deps(&self) -> crate::ImageDeps { + self.pipeline.image_deps() + } + pub(super) fn admit_image_work(&self) -> Option { + self.pipeline.admit_work() } pub(super) fn scroller(&self, cx: &mut Context) -> MessageScroller { MessageScroller::new( diff --git a/crates/agent/src/panel/message_markdown.rs b/crates/agent/src/panel/message_markdown.rs new file mode 100644 index 00000000..06ba0e8b --- /dev/null +++ b/crates/agent/src/panel/message_markdown.rs @@ -0,0 +1,97 @@ +//! Parsed Markdown of the visible chat's agent messages and reasoning, kept per row: a +//! row scrolled back into view keeps its parse, and text streamed into a row parses +//! only what was appended instead of the whole message again. +use super::{AgentPanel, message_list::MessageList, parts::PartKind, widgets::safe_markdown}; +use gpui_kit::{AppContext as _, Context, Entity, Subscription, component::text::TextViewState}; +use nocterm_ai::thread::Entry; +use std::collections::HashMap; + +struct RowMarkdown { + state: Entity, + text: String, + source_len: Option, + _parsed: Subscription, +} + +#[derive(Default)] +pub(super) struct MessageMarkdown { + rows: HashMap, +} +impl MessageMarkdown { + /// The Markdown a row draws now: an agent message as revealed so far, or expanded + /// reasoning. Other rows draw no kept Markdown. + pub(super) fn text( + entry: &Entry, + row: usize, + messages: &MessageList, + ) -> Option<(String, usize)> { + let source = match entry { + Entry::Agent(text) => messages.revealed_slice(row, text), + Entry::Thought(text) if messages.expanded(row) => text, + _ => return None, + }; + if messages.markdown_current(row, source.len()) { + return None; + } + Some((safe_markdown(source), source.len())) + } + pub(super) fn current(&self, row: usize, source_len: usize) -> bool { + self.rows + .get(&row) + .is_some_and(|kept| kept.source_len == Some(source_len)) + } + pub(super) fn invalidate(&mut self, dirty: &[usize]) { + for row in dirty { + if let Some(kept) = self.rows.get_mut(row) { + kept.source_len = None; + } + } + } + /// Shows `text` in the row's state, appending when it extends what the row showed. + pub(super) fn prepare( + &mut self, + row: usize, + text: String, + source_len: usize, + cx: &mut Context, + ) { + if let Some(kept) = self.rows.get_mut(&row) { + kept.source_len = Some(source_len); + if kept.text == text { + return; + } + kept.state.update(cx, |state, cx| { + match text.strip_prefix(kept.text.as_str()) { + Some(appended) => state.push_str(appended, cx), + None => state.set_text(&text, cx), + } + }); + kept.text = text; + return; + } + let state = cx.new(|cx| TextViewState::markdown(&text, cx)); + // As an element's keyed state would: a finished parse redraws the transcript. + let parsed = cx.observe(&state, |panel, _, cx| { + panel.layout.parts().notify(PartKind::Transcript, cx); + }); + self.rows.insert( + row, + RowMarkdown { + state, + text, + source_len: Some(source_len), + _parsed: parsed, + }, + ); + } + pub(super) fn state(&self, row: usize) -> Option<&Entity> { + self.rows.get(&row).map(|kept| &kept.state) + } + /// Forgets the rows a shortened transcript no longer has. + pub(super) fn retain(&mut self, count: usize) { + self.rows.retain(|row, _| *row < count); + } + pub(super) fn clear(&mut self) { + self.rows.clear(); + } +} diff --git a/crates/agent/src/panel/notices.rs b/crates/agent/src/panel/notices.rs index 64d63b5a..4ca52e02 100644 --- a/crates/agent/src/panel/notices.rs +++ b/crates/agent/src/panel/notices.rs @@ -2,6 +2,8 @@ use gpui_kit::{EntityId, Task}; use std::sync::Arc; +use super::account_notice::AccountNotice; + struct Redraw { ticket: Arc<()>, task: Option>, @@ -14,16 +16,28 @@ pub(super) enum ApprovalEffect { #[derive(Default)] pub(super) struct PanelNotices { error: Option, + account: Option, redraw: Option, approval: Option>, + attention: bool, } impl PanelNotices { + /// Records whether the panel asks for attention; true when that changed. + pub(super) fn attention_changed(&mut self, attention: bool) -> bool { + std::mem::replace(&mut self.attention, attention) != attention + } pub(super) fn error(&self) -> Option<&str> { self.error.as_deref() } pub(super) fn set_error(&mut self, error: Option) { self.error = error; } + pub(super) fn account(&self) -> Option { + self.account + } + pub(super) fn set_account(&mut self, account: Option) { + self.account = account; + } pub(super) fn approval_changed( &mut self, fingerprint: Option>, diff --git a/crates/agent/src/panel/other_windows.rs b/crates/agent/src/panel/other_windows.rs new file mode 100644 index 00000000..e298abbb --- /dev/null +++ b/crates/agent/src/panel/other_windows.rs @@ -0,0 +1,62 @@ +//! The chats other windows hold, which this window's history lists without holding them. +//! +//! The runtime names them; each is observed so that its row follows it — its title, and the +//! spinner while its agent works. Only weak handles are kept: a chat lives as long as its own +//! window keeps it. A burst of changes (a window adopting its saved chats) refreshes the list +//! once. +use super::AgentPanel; +use crate::thread::AgentThread; +use gpui_kit::{App, Context, Entity, EntityId, Subscription, WeakEntity}; + +#[derive(Default)] +pub(super) struct OtherWindowChats { + chats: Vec>, + stale: bool, + _observations: Vec, +} +impl OtherWindowChats { + /// Lists `chats`, observing each so that its row follows it; whether that changed the list. + pub(super) fn set( + &mut self, + chats: Vec>, + cx: &mut Context, + ) -> bool { + self.stale = false; + let same = chats.len() == self.chats.len() + && chats + .iter() + .zip(&self.chats) + .all(|(chat, known)| chat.entity_id() == known.entity_id()); + if same { + return false; + } + self._observations = chats + .iter() + .map(|chat| cx.observe(chat, |panel, _, cx| panel.on_other_window_chat_changed(cx))) + .collect(); + self.chats = chats.iter().map(Entity::downgrade).collect(); + true + } + /// Marks the list for one refresh; `true` the first time since the last. + pub(super) fn mark_stale(&mut self) -> bool { + !std::mem::replace(&mut self.stale, true) + } + /// The chats with something to list: a chat no one wrote to yet stays in its window. + pub(super) fn listed(&self, cx: &App) -> Vec> { + self.chats + .iter() + .filter_map(WeakEntity::upgrade) + .filter(|chat| { + let chat = chat.read(cx); + !chat.history_entries().is_empty() + || chat.archive_summary().is_some_and(|row| row.has_content) + }) + .collect() + } + pub(super) fn find(&self, id: EntityId) -> Option> { + self.chats + .iter() + .find(|chat| chat.entity_id() == id) + .and_then(WeakEntity::upgrade) + } +} diff --git a/crates/agent/src/panel/parts.rs b/crates/agent/src/panel/parts.rs new file mode 100644 index 00000000..1f26ceca --- /dev/null +++ b/crates/agent/src/panel/parts.rs @@ -0,0 +1,139 @@ +//! The panel's regions as views, so a change redraws only the regions it touches. +//! +//! Every region is cached: the panel's own redraws reuse them unless they are +//! notified. Any notify of the panel invalidates all of them, so state the panel +//! changes stays correct by default; thread changes, typing and queue disclosure can +//! target a single region. +//! The footer (approvals, status, queue, commands and composer) is cached to its +//! content: reused, it keeps the height its content last took. +use super::AgentPanel; +use gpui_kit::{ + App, AppContext as _, Context, Entity, IntoElement, Render, StyleRefinement, Styled, + WeakEntity, Window, div, +}; +use std::hash::{Hash, Hasher}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum PartKind { + Transcript, + History, + Footer, +} + +pub(super) struct PanelPart { + panel: WeakEntity, + kind: PartKind, +} +impl Render for PanelPart { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + let kind = self.kind; + self.panel + .update(cx, |panel, cx| panel.render_part(kind, window, cx)) + .unwrap_or_else(|_| div().into_any_element()) + } +} + +pub(super) struct PanelParts { + transcript: Entity, + history: Entity, + footer: Entity, + transcript_seen: Option, + history_seen: Option, +} +impl PanelParts { + pub(super) fn new(cx: &mut Context) -> Self { + let panel = cx.weak_entity(); + let mut part = |kind| { + let panel = panel.clone(); + cx.new(|_| PanelPart { panel, kind }) + }; + Self { + transcript: part(PartKind::Transcript), + history: part(PartKind::History), + footer: part(PartKind::Footer), + transcript_seen: None, + history_seen: None, + } + } + /// The transcript fills the chat column above the footer. + pub(super) fn transcript(&self) -> impl IntoElement + use<> { + self.transcript.clone().cached( + StyleRefinement::default() + .w_full() + .flex_1() + .min_h_0() + .min_w_0(), + ) + } + /// The history fills its column, or the panel body when no chat is open. + pub(super) fn history(&self) -> impl IntoElement + use<> { + self.history.clone().cached( + StyleRefinement::default() + .w_full() + .flex_1() + .min_h_0() + .min_w_0(), + ) + } + /// The footer spans the chat column below the transcript, as tall as its content. + pub(super) fn footer(&self) -> impl IntoElement + use<> { + self.footer + .clone() + .cached_to_content(StyleRefinement::default().w_full()) + } + /// Redraws the cached regions: the panel's state changed in a way only it knows. + pub(super) fn invalidate(&self, cx: &mut App) { + self.transcript.update(cx, |_, cx| cx.notify()); + self.history.update(cx, |_, cx| cx.notify()); + self.footer.update(cx, |_, cx| cx.notify()); + } + pub(super) fn notify(&self, kind: PartKind, cx: &mut App) { + let part = match kind { + PartKind::Transcript => &self.transcript, + PartKind::History => &self.history, + PartKind::Footer => &self.footer, + }; + part.update(cx, |_, cx| cx.notify()); + } + /// Redraws the transcript when its rows or what they show differ from its last + /// drawing: a draft or a configuration change of the chat leaves it as it is. + pub(super) fn transcript_changed(&self, rows: bool, stamp: u64, cx: &mut App) { + if rows || self.transcript_seen != Some(stamp) { + self.notify(PartKind::Transcript, cx); + } + } + pub(super) fn drew_transcript(&mut self, stamp: Option) { + self.transcript_seen = stamp; + } + /// Redraws the history when the rows it lists differ from its last drawing; without + /// a stamp (a search matches transcripts) it always redraws. + pub(super) fn history_changed(&self, stamp: Option, cx: &mut App) { + if stamp.is_none() || self.history_seen != stamp { + self.notify(PartKind::History, cx); + } + } + pub(super) fn drew_history(&mut self, stamp: Option) { + self.history_seen = stamp; + } +} + +/// What the transcript draws besides its changed rows: the rows' count and liveness, and the +/// plan. +pub(super) fn transcript_stamp(thread: &crate::thread::AgentThread) -> u64 { + let mut hasher = std::collections::hash_map::DefaultHasher::new(); + let transcript = thread.transcript(); + ( + transcript.entries.len(), + thread.session_generating(), + thread.session_accepts_updates(), + ) + .hash(&mut hasher); + if let Some(plan) = &transcript.plan { + for entry in &plan.entries { + entry.content.hash(&mut hasher); + std::mem::discriminant(&entry.priority).hash(&mut hasher); + std::mem::discriminant(&entry.status).hash(&mut hasher); + } + } + hasher.finish() +} diff --git a/crates/agent/src/panel/preview_operation.rs b/crates/agent/src/panel/preview_operation.rs index f07035c8..7f49f972 100644 --- a/crates/agent/src/panel/preview_operation.rs +++ b/crates/agent/src/panel/preview_operation.rs @@ -2,41 +2,35 @@ use super::entries::PreviewError; use super::{ AgentPanel, - image_pipeline::{ImageKey, PreviewTicket}, + image_pipeline::{CompletedPreview, PreviewSource, PreviewTicket}, message_list::{ActiveListBinding, MessageList}, }; -use base64::Engine as _; use gpui_kit::{Context, Task}; -use nocterm_ai::images::ImageThumbnailer; use nocterm_ai::model::content::Content; -use std::sync::Arc; pub(super) struct PreviewOperation { binding: ActiveListBinding, ticket: PreviewTicket, row: Option, - bytes: Vec, - encoded: bool, - thumbnailer: Arc, + source: PreviewSource, + images: crate::ImageDeps, } impl PreviewOperation { pub(super) fn cached( messages: &mut MessageList, - key: ImageKey, - data: impl FnOnce() -> Vec, - encoded: bool, + source: PreviewSource, row: Option, cx: &mut Context, ) -> gpui_kit::AnyElement { + let key = source.key(); if let Some(binding) = messages.binding() - && let Some(ticket) = messages.request_preview(key) + && let Some(ticket) = messages.request_preview(source.clone()) { let task = Self { binding, ticket, row, - bytes: data(), - encoded, - thumbnailer: messages.thumbnailer(), + source, + images: messages.image_deps(), } .launch(cx); messages.install_preview_task(ticket, task); @@ -48,29 +42,17 @@ impl PreviewOperation { binding, ticket, row, - bytes, - encoded, - thumbnailer, + source, + images, } = self; let future = cx.background_executor().spawn(async move { - let bytes = if encoded { - if bytes.len() > nocterm_ai::images::MAX_IMAGE_BYTES.div_ceil(3) * 4 { - return Err(PreviewError::Codec( - nocterm_ai::images::ImageCodecError::SourceSize, - )); - } - base64::engine::general_purpose::STANDARD.decode(bytes)? - } else { - bytes - }; + let _permit = images.work().await; + let bytes = source.decode()?; let bounds = nocterm_ai::images::PreviewBounds::new(640, 640)?; - let preview = thumbnailer.thumbnail(bytes.into(), bounds)?; + let preview = images.thumbnailer().thumbnail(bytes, bounds)?; // Supplied previews retain the same pure size/format contract. let preview = nocterm_ai::images::EncodedPreview::new(preview.bytes().clone(), bounds)?; - Ok::<_, PreviewError>(Arc::new(gpui_kit::Image::from_bytes( - gpui_kit::ImageFormat::Png, - preview.bytes().to_vec(), - ))) + Ok::<_, PreviewError>(CompletedPreview::from_encoded(preview)) }); cx.spawn(async move |panel, cx| { let result = future.await; @@ -92,22 +74,14 @@ impl PreviewOperation { .composer() .images() .iter() - .map(|image| { - let key = (image.bytes().as_ptr() as usize, image.bytes().len(), false); - ( - key, - (!messages.has_preview(&key)).then(|| image.bytes().to_vec()), - ) - }) + .map(|image| PreviewSource::Original(image.shared_bytes())) .collect::>(); if previews.is_empty() { return Vec::new(); } previews .into_iter() - .map(|(key, data)| { - Self::cached(messages, key, || data.unwrap_or_default(), false, None, cx) - }) + .map(|source| Self::cached(messages, source, None, cx)) .collect() } } @@ -125,17 +99,13 @@ impl PreviewOperation { .saved() .images .iter() - .zip(prompt.image_keys().iter()) - .filter_map(|(block, key)| { + .filter_map(|block| { let Content::Image(image) = block else { return None; }; - let key = (*key)?; Some(Self::cached( messages, - key, - || image.data.as_bytes().to_vec(), - true, + PreviewSource::Encoded(image.data.clone()), None, cx, )) diff --git a/crates/agent/src/panel/queue.rs b/crates/agent/src/panel/queue.rs index 0e5117a6..7ca8f2ea 100644 --- a/crates/agent/src/panel/queue.rs +++ b/crates/agent/src/panel/queue.rs @@ -1,11 +1,9 @@ -//! The queue peeks from behind the composer and unfolds above it. +//! The queue peeks from behind the composer and opens at its intrinsic height. use super::{AgentPanel, images::ImageAttachError}; use crate::thread::AgentThread; use base64::Engine as _; use gpui_kit::{ - AnyElement, Context, Entity, EntityId, SharedString, TestSupportExt as _, Window, - base::motion::{Transition, transition}, - canvas, + AnyElement, Context, Entity, EntityId, SharedString, TestSupportExt as _, component::{ ActiveTheme as _, Disableable as _, Sizable as _, button::{Button, ButtonVariants as _}, @@ -16,7 +14,6 @@ use gpui_kit::{ px, rems, }; use nocterm_ui::IconName; -use std::time::Duration; pub(super) fn decode_saved_image(data: &str) -> Result, ImageAttachError> { if data.len() > nocterm_ai::images::MAX_IMAGE_BYTES.div_ceil(3) * 4 { @@ -25,41 +22,8 @@ pub(super) fn decode_saved_image(data: &str) -> Result, ImageAttachError Ok(base64::engine::general_purpose::STANDARD.decode(data)?) } -/// One immutable frame of the queue, without authoritative queued messages. -pub(super) struct QueueFrame { - chat: EntityId, - open: bool, - measured: gpui_kit::Pixels, - editing: bool, -} pub(super) struct QueueRenderer; impl QueueRenderer { - pub(super) fn height( - frame: &QueueFrame, - window: &mut Window, - cx: &mut Context, - ) -> gpui_kit::Pixels { - transition( - ( - SharedString::from(format!("agent-queue-{}", frame.chat)), - "height", - ), - if frame.open { - frame.measured.min(px(240.)) - } else { - px(0.) - }, - Transition::new(Duration::from_millis(220)).ease(|t| { - if t >= 1. { - 1. - } else { - 1. - 2_f32.powf(-10. * t) - } - }), - window, - cx, - ) - } pub(super) fn hood( header: AnyElement, cx: &gpui_kit::App, @@ -76,10 +40,15 @@ impl QueueRenderer { .min_w_0() .child(header) } - fn rows(frame: &QueueFrame, rows: Vec, cx: &mut Context) -> AnyElement { - let owner = cx.weak_entity(); - let chat = frame.chat; - v_flex() + pub(super) fn render( + editing: bool, + header: AnyElement, + rows: Vec, + cx: &mut Context, + ) -> AnyElement { + // Normal layout resolves the current content, width and font in this frame. + // A previous-frame height followed by a transition delays even a warm open. + let rows = v_flex() .id("agent-queue-rows") .test_support() .relative() @@ -88,44 +57,12 @@ impl QueueRenderer { .min_w_0() .gap_2() .p_2() - .children(rows) - .child( - canvas( - move |bounds, _, cx| { - let owner = owner.clone(); - cx.defer(move |cx| { - let _ = owner.update(cx, |panel, cx| { - panel.on_queue_height_observed(chat, bounds.size.height, cx) - }); - }); - }, - |_, _, _, _| {}, - ) - .absolute() - .inset_0(), - ) - .into_any_element() - } - pub(super) fn render( - frame: &QueueFrame, - header: AnyElement, - rows: Vec, - height: gpui_kit::Pixels, - cx: &mut Context, - ) -> AnyElement { - let target = frame.measured.min(px(240.)); - let rows = Self::rows(frame, rows, cx); + .children(rows); let hood = Self::hood(header, cx).child( div() .id("agent-queue-reveal") .test_support() - .h(height) .overflow_hidden() - .opacity(if target > px(0.) { - (height / target).clamp(0., 1.) - } else { - 0. - }) .child( div() .id("agent-queue-scroll") @@ -135,7 +72,7 @@ impl QueueRenderer { .child(rows), ), ); - hood.when(frame.editing, |hood| { + hood.when(editing, |hood| { hood.child( h_flex() .px_2() @@ -314,7 +251,6 @@ impl QueueRenderer { previews: Vec>, thread: &Entity, labels: &super::attachments::AttachmentLabels, - height: gpui_kit::Pixels, cx: &mut Context, ) -> Option { let origin = thread.entity_id(); @@ -323,14 +259,8 @@ impl QueueRenderer { } let count = thread.read(cx).queued_prompt_ids().count(); let first = thread.read(cx).composer().queue()[0].saved().id; - let frame = QueueFrame { - chat: thread.entity_id(), - open: composer.queue_open(), - measured: composer.queue_height(thread.entity_id()), - editing: composer.editing_chat() == Some(thread.entity_id()), - }; let header = Self::queue_header(composer, origin, count, first, cx); - if !frame.open && height <= px(0.) { + if !composer.queue_open() { return Some( QueueRenderer::hood(header, cx) .test_support() @@ -342,23 +272,11 @@ impl QueueRenderer { .zip(previews) .map(|(prompt, images)| Self::queue_row(composer, origin, prompt, images, labels, cx)) .collect(); - Some(Self::render(&frame, header, rows, height, cx)) - } -} - -impl QueueRenderer { - pub(super) fn visible_height( - composer: &super::composer_view::ComposerView, - thread: &Entity, - window: &mut Window, - cx: &mut Context, - ) -> gpui_kit::Pixels { - let frame = QueueFrame { - chat: thread.entity_id(), - open: composer.queue_open(), - measured: composer.queue_height(thread.entity_id()), - editing: composer.editing_chat() == Some(thread.entity_id()), - }; - Self::height(&frame, window, cx) + Some(Self::render( + composer.editing_chat() == Some(origin), + header, + rows, + cx, + )) } } diff --git a/crates/agent/src/panel/queue_image_preparation.rs b/crates/agent/src/panel/queue_image_preparation.rs index ff260ef2..162c4f94 100644 --- a/crates/agent/src/panel/queue_image_preparation.rs +++ b/crates/agent/src/panel/queue_image_preparation.rs @@ -3,10 +3,7 @@ use super::{AgentPanel, composer_view::ComposerBinding, images::ImageAttachError use crate::thread::AgentThread; use gpui_kit::{Context, Task, WeakEntity}; use nocterm_ai::model::content::Content; -use nocterm_ai::{ - images::{ImageInspector, PromptImage}, - session::Ticket, -}; +use nocterm_ai::{images::PromptImage, session::Ticket}; use std::sync::Arc; pub(super) struct QueueImagePreparation { binding: ComposerBinding, @@ -14,7 +11,7 @@ pub(super) struct QueueImagePreparation { ticket: Ticket, prompt_id: u64, saved: Arc, - inspector: Arc, + images: crate::ImageDeps, } impl QueueImagePreparation { pub(super) fn new( @@ -23,7 +20,7 @@ impl QueueImagePreparation { ticket: Ticket, prompt_id: u64, saved: Arc, - inspector: Arc, + images: crate::ImageDeps, ) -> Self { Self { binding, @@ -31,7 +28,7 @@ impl QueueImagePreparation { ticket, prompt_id, saved, - inspector, + images, } } pub(super) fn launch(self, cx: &mut Context) -> Task<()> { @@ -41,15 +38,26 @@ impl QueueImagePreparation { ticket, prompt_id, saved, - inspector, + images, } = self; let future = cx.background_executor().spawn(async move { + let _permit = images.work().await; + let inspector = images.inspector(); + let mut budget = nocterm_ai::images::ImageCollectionBudget::remaining(&[])?; + budget.check_count( + saved + .images + .iter() + .filter(|content| matches!(content, Content::Image(_))) + .count(), + )?; saved .images .iter() .filter_map(|block| match block { Content::Image(image) => Some( super::queue::decode_saved_image(&image.data).and_then(|bytes| { + budget.admit(bytes.len())?; PromptImage::validate(bytes, inspector.as_ref()) .map_err(ImageAttachError::from) }), diff --git a/crates/agent/src/panel/shell.rs b/crates/agent/src/panel/shell.rs index 561e11cd..e4ff8f18 100644 --- a/crates/agent/src/panel/shell.rs +++ b/crates/agent/src/panel/shell.rs @@ -14,7 +14,7 @@ use gpui_kit::{ px, }; use nocterm_ui::IconName; -use nocterm_workspace::RightPanelEvent; +use nocterm_workspace::PanelRequest; pub(super) struct ShellRenderer; impl ShellRenderer { @@ -90,7 +90,7 @@ impl ShellRenderer { .on_action(cx.listener(|this, _: &crate::ToggleModelPicker, _, cx| { this.toggle_model_picker(cx); })) - .on_action(cx.listener(|this, _: &crate::AttachImage, _, cx| this.pick_images(cx))) + .on_action(cx.listener(|this, _: &crate::AttachFiles, _, cx| this.pick_files(cx))) } pub(super) fn header( @@ -157,7 +157,7 @@ impl ShellRenderer { } else { "Expand chat" }) - .on_click(cx.listener(|_, _, _, cx| cx.emit(RightPanelEvent::ToggleMaximized))), + .on_click(cx.listener(|_, _, _, cx| cx.emit(PanelRequest::ToggleExpanded))), ) .into_any_element() } diff --git a/crates/agent/src/panel/stream.rs b/crates/agent/src/panel/stream.rs index c0f3d8ca..b132e65a 100644 --- a/crates/agent/src/panel/stream.rs +++ b/crates/agent/src/panel/stream.rs @@ -79,7 +79,7 @@ impl StreamReveal { }); changed } - pub(super) fn text(&self, index: usize, text: &str) -> String { + pub(super) fn slice<'a>(&self, index: usize, text: &'a str) -> &'a str { let mut end = self .bytes .get(&index) @@ -89,7 +89,10 @@ impl StreamReveal { while !text.is_char_boundary(end) { end -= 1; } - text[..end].to_owned() + &text[..end] + } + pub(super) fn text(&self, index: usize, text: &str) -> String { + self.slice(index, text).to_owned() } pub(super) fn pending(&self) -> bool { !self.backlog.is_empty() diff --git a/crates/agent/src/panel/tests/approvals.rs b/crates/agent/src/panel/tests/approvals.rs index 1b2bfff0..bb28b0d1 100644 --- a/crates/agent/src/panel/tests/approvals.rs +++ b/crates/agent/src/panel/tests/approvals.rs @@ -151,7 +151,7 @@ fn live_group_membership_and_revoked_registration_reject_stale_terminal_ids( fn queued_long_terminal_approvals_fit_short_narrow_panel_and_actions_respond( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let responses = cx.update(|cx| { let thread = f.panel.read(cx).current().unwrap(); @@ -257,7 +257,7 @@ fn queued_long_terminal_approvals_fit_short_narrow_panel_and_actions_respond( fn permission_approval_choices_are_clickable_and_cancel_removes_last_request( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let responses = cx.update(|cx| { let thread = f.panel.read(cx).current().unwrap(); @@ -675,10 +675,6 @@ fn rendered_chat_dispatches_joined_reconnect_inputs_in_acceptance_order(cx: &mut window.click("new-agent-codex", cx); window.render_frame(cx); let thread = f.panel.read(cx).current().unwrap(); - { - let runtime = f.installation.installed.runtime(); - runtime.register_document(crate::thread::client(&thread), cx); - }; thread.update(cx, |thread, cx| thread.request_activation(cx)); }) .unwrap(); diff --git a/crates/agent/src/panel/tests/archive.rs b/crates/agent/src/panel/tests/archive.rs index 04bcd665..2b95e478 100644 --- a/crates/agent/src/panel/tests/archive.rs +++ b/crates/agent/src/panel/tests/archive.rs @@ -23,7 +23,7 @@ pub(super) fn archived( f.panel .read(cx) .chat_documents() - .find(|thread| thread.read(cx).document().chat_id() == chat.id) + .find(|thread| thread.read(cx).document().chat_id() == chat.header.id) .unwrap() .clone() }) @@ -70,7 +70,11 @@ fn catalog_rows_are_idle_and_opening_hydrates_and_connects_with_the_queue_paused cx.update(|cx| { assert!(thread.read(cx).is_archived()); assert!(thread.read(cx).history_entries().is_empty()); - assert!(thread.read(cx).flush_snapshot(cx).is_none()); + assert!( + thread + .update(cx, |thread, cx| thread.flush_snapshot(cx).unwrap()) + .is_none() + ); }); f.panel .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); @@ -104,9 +108,9 @@ fn rename_and_pin_hydrate_before_writing_the_archive(cx: &mut TestAppContext) { }); cx.run_until_parked(); cx.update(|_| { - let loaded = f.repository.transcript(&chat.id).unwrap(); - assert_eq!(loaded.name.as_deref(), Some("New name")); - assert!(loaded.pinned); + let loaded = f.repository.transcript(&chat.header.id).unwrap(); + assert_eq!(loaded.header.name.as_deref(), Some("New name")); + assert!(loaded.header.pinned); assert_eq!(loaded.entries.len(), 1); }); } @@ -125,19 +129,19 @@ fn delete_while_loading_does_not_resurrect_the_document(cx: &mut TestAppContext) .unwrap(); cx.run_until_parked(); cx.update(|cx| { - assert!(f.repository.transcript(&chat.id).is_err()); + assert!(f.repository.transcript(&chat.header.id).is_err()); assert!( f.panel .read(cx) .chat_documents() - .all(|thread| thread.read(cx).document().chat_id() != chat.id) + .all(|thread| thread.read(cx).document().chat_id() != chat.header.id) ); }); } #[gpui_kit::test] fn restored_root_controls_process_sandbox_restore_and_fresh_fallback(cx: &mut TestAppContext) { let resolver = workspace_resolver(); - let f = fixture_with_width(cx, 26., usage_source(), resolver.clone(), image_deps()); + let f = fixture_with_width(cx, 26., resolver.clone(), image_deps()); cx.run_until_parked(); let a = f._directory.path().join("A"); let b = f._directory.path().join("B"); @@ -202,7 +206,7 @@ fn hydration_waits_for_shared_permit_and_clean_archives_are_evicted(cx: &mut Tes let mut chats = Vec::new(); for index in 0..15 { let mut chat = saved(&f); - chat.updated = index; + chat.header.updated = index; chat.entries = (0..2000).map(|_| Entry::Agent("row".into())).collect(); chats.push(chat); } @@ -212,7 +216,7 @@ fn hydration_waits_for_shared_permit_and_clean_archives_are_evicted(cx: &mut Tes let blocked = f .repository .block_next(nocterm_ai::testing::StorageOperation::Load( - chats.first().unwrap().id.clone(), + chats.first().unwrap().header.id.clone(), )); cx.update(|cx| f.installation.installed.runtime().reload_history(cx)); cx.run_until_parked(); @@ -272,12 +276,14 @@ fn typing_while_archive_loading_preserves_latest_literal_draft(cx: &mut TestAppC lazy_start(&f, cx); cx.run_until_parked(); let mut chat = saved(&f); - chat.updated = 1; + chat.header.updated = 1; chat.draft = Some("old saved draft".into()); let thread = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); f.panel .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); cx.run_until_parked(); @@ -299,9 +305,9 @@ fn typing_while_archive_loading_preserves_latest_literal_draft(cx: &mut TestAppC ); assert_eq!(thread.read(cx).draft_text(), Some("new literal\ndraft")); assert_eq!(thread.read(cx).updated_at(), edited_at); - let loaded = f.repository.transcript(&chat.id).unwrap(); + let loaded = f.repository.transcript(&chat.header.id).unwrap(); assert_eq!(loaded.draft.as_deref(), Some("new literal\ndraft")); - assert_eq!(loaded.updated, edited_at); + assert_eq!(loaded.header.updated, edited_at); }); } #[gpui_kit::test] @@ -318,7 +324,9 @@ fn disable_while_loading_saves_draft_patch_without_truncating_archive(cx: &mut T let thread = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); f.panel .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); cx.run_until_parked(); @@ -333,7 +341,7 @@ fn disable_while_loading_saves_draft_patch_without_truncating_archive(cx: &mut T }); cx.run_until_parked(); cx.update(|_| { - let saved = f.repository.transcript(&chat.id).unwrap(); + let saved = f.repository.transcript(&chat.header.id).unwrap(); assert_eq!(saved.draft.as_deref(), Some("draft before disable")); assert_eq!(saved.entries.len(), 1); assert_eq!(saved.queue[0].id, 3); @@ -346,7 +354,7 @@ fn native_quit_flushes_pending_archived_draft_with_history_gate_held(cx: &mut Te let f = fixture(cx); cx.run_until_parked(); let mut chat = saved(&f); - chat.name = Some("original name".into()); + chat.header.name = Some("original name".into()); chat.queue.push(SavedPrompt { id: 4, text: "pending queue".into(), @@ -356,24 +364,26 @@ fn native_quit_flushes_pending_archived_draft_with_history_gate_held(cx: &mut Te let thread = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); f.panel .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); cx.run_until_parked(); enter_literal(&f, "draft at quit", cx); cx.run_until_parked(); cx.update(App::shutdown); - let saved = f.repository.transcript(&chat.id).unwrap(); + let saved = f.repository.transcript(&chat.header.id).unwrap(); assert_eq!(saved.draft.as_deref(), Some("draft at quit")); assert_eq!(saved.entries.len(), 1); assert_eq!(saved.queue[0].id, 4); - assert_eq!(saved.name.as_deref(), Some("original name")); + assert_eq!(saved.header.name.as_deref(), Some("original name")); let _ = blocked.send(Ok(())); } #[gpui_kit::test] fn invalid_saved_root_fails_safely_and_legacy_missing_root_starts_fresh(cx: &mut TestAppContext) { let resolver = workspace_resolver(); - let f = fixture_with_width(cx, 26., usage_source(), resolver.clone(), image_deps()); + let f = fixture_with_width(cx, 26., resolver.clone(), image_deps()); cx.run_until_parked(); let mut invalid = saved(&f); invalid.session_id = Some("old-session".into()); @@ -418,7 +428,7 @@ fn failed_hydration_retries_with_only_new_metadata_intentions(cx: &mut TestAppCo let chat = saved(&f); let thread = archived(&f, &chat, cx); f.repository.fail_next( - nocterm_ai::testing::StorageOperation::Load(chat.id.clone()), + nocterm_ai::testing::StorageOperation::Load(chat.header.id.clone()), nocterm_ai::storage::AgentStorageError::Interrupted, ); thread.update(cx, |thread, cx| { @@ -428,9 +438,9 @@ fn failed_hydration_retries_with_only_new_metadata_intentions(cx: &mut TestAppCo cx.update(|cx| assert!(thread.read(cx).is_archived())); thread.update(cx, |thread, cx| thread.set_pinned(true, cx)); cx.run_until_parked(); - let durable = f.repository.transcript(&chat.id).unwrap(); - assert_eq!(durable.name, chat.name); - assert!(durable.pinned); + let durable = f.repository.transcript(&chat.header.id).unwrap(); + assert_eq!(durable.header.name, chat.header.name); + assert!(durable.header.pinned); assert_eq!(durable.entries.len(), 1); assert_eq!(f.connector.connects.load(Ordering::SeqCst), 0); } @@ -440,12 +450,14 @@ fn deferred_fork_uses_original_chat_and_row_after_active_chat_changes(cx: &mut T let f = fixture(cx); cx.run_until_parked(); let mut chat = saved(&f); - chat.name = Some("original source".into()); + chat.header.name = Some("original source".into()); chat.entries.push(Entry::Agent("later answer".into())); let source = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); cx.update_window(f.handle, |_, window, cx| { f.panel.update(cx, |panel, cx| { panel.fork_thread_at(source.entity_id(), Some(0), window, cx); @@ -460,12 +472,19 @@ fn deferred_fork_uses_original_chat_and_row_after_active_chat_changes(cx: &mut T .repository .chats() .into_iter() - .find(|saved| saved.name.as_deref() == Some("original source (fork)")) + .find(|saved| saved.header.name.as_deref() == Some("original source (fork)")) .expect("original source was forked"); - assert_ne!(fork.id, chat.id); + assert_ne!(fork.header.id, chat.header.id); assert_eq!(fork.entries.len(), 1); assert!(matches!(&fork.entries[0], Entry::Agent(text) if text == "historic answer")); - assert_eq!(f.repository.transcript(&chat.id).unwrap().entries.len(), 2); + assert_eq!( + f.repository + .transcript(&chat.header.id) + .unwrap() + .entries + .len(), + 2 + ); } #[gpui_kit::test] @@ -476,7 +495,9 @@ fn deleted_source_does_not_fork_after_deferred_hydration(cx: &mut TestAppContext let source = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); cx.update_window(f.handle, |_, window, cx| { f.panel.update(cx, |panel, cx| { panel.fork_thread(source.entity_id(), window, cx); @@ -501,7 +522,9 @@ fn clearing_native_input_while_hydration_waits_wins_over_saved_draft(cx: &mut Te let source = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); f.panel .update(cx, |panel, cx| panel.open_thread(source.entity_id(), cx)); cx.run_until_parked(); @@ -510,7 +533,7 @@ fn clearing_native_input_while_hydration_waits_wins_over_saved_draft(cx: &mut Te cx.run_until_parked(); let _ = blocked.send(Ok(())); cx.run_until_parked(); - let loaded = f.repository.transcript(&chat.id).unwrap(); + let loaded = f.repository.transcript(&chat.header.id).unwrap(); assert!(loaded.draft.is_none()); assert_eq!(loaded.entries.len(), 1); cx.update(|cx| { @@ -549,7 +572,7 @@ fn hydration_observer_can_rename_then_delete_without_reentering_owner_borrow( .update(cx, |panel, cx| panel.open_thread(source.entity_id(), cx)); cx.run_until_parked(); assert!(once.get()); - assert!(f.repository.transcript(&chat.id).is_err()); + assert!(f.repository.transcript(&chat.header.id).is_err()); cx.update(|cx| assert_eq!(f.panel.read(cx).chat_documents().len(), 0)); } @@ -561,7 +584,9 @@ fn closed_original_window_does_not_fork_after_hydration(cx: &mut TestAppContext) let source = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); cx.update_window(f.handle, |_, window, cx| { f.panel.update(cx, |panel, cx| { panel.fork_thread(source.entity_id(), window, cx) @@ -576,7 +601,7 @@ fn closed_original_window_does_not_fork_after_hydration(cx: &mut TestAppContext) cx.run_until_parked(); let saved = f.repository.chats(); assert_eq!(saved.len(), 1); - assert_eq!(saved[0].id, chat.id); + assert_eq!(saved[0].header.id, chat.header.id); assert_eq!(f.connector.connects.load(Ordering::SeqCst), 0); } @@ -594,7 +619,9 @@ fn blocked_open_applies_metadata_intentions_once_before_warm_connection(cx: &mut let source = archived(&f, &chat, cx); let blocked = f .repository - .block_next(nocterm_ai::testing::StorageOperation::Load(chat.id.clone())); + .block_next(nocterm_ai::testing::StorageOperation::Load( + chat.header.id.clone(), + )); f.panel .update(cx, |panel, cx| panel.open_thread(source.entity_id(), cx)); cx.run_until_parked(); @@ -607,14 +634,135 @@ fn blocked_open_applies_metadata_intentions_once_before_warm_connection(cx: &mut assert_eq!(f.connector.connects.load(Ordering::SeqCst), 0); let _ = blocked.send(Ok(())); cx.run_until_parked(); - let loaded = f.repository.transcript(&chat.id).unwrap(); - assert_eq!(loaded.name.as_deref(), Some("latest accepted name")); - assert!(loaded.pinned); + let loaded = f.repository.transcript(&chat.header.id).unwrap(); + assert_eq!(loaded.header.name.as_deref(), Some("latest accepted name")); + assert!(loaded.header.pinned); assert_eq!(loaded.entries.len(), 1); assert_eq!(loaded.queue[0].id, 9); assert_eq!(f.connector.connects.load(Ordering::SeqCst), 1); assert!(f.commands.prompts.lock().unwrap().is_empty()); let loads = f.repository.operations().iter().filter(|operation| - matches!(operation, nocterm_ai::testing::StorageOperation::Load(id) if id == &chat.id)).count(); + matches!(operation, nocterm_ai::testing::StorageOperation::Load(id) if id == &chat.header.id)).count(); assert_eq!(loads, 1); } + +#[gpui_kit::test] +fn installation_capacity_refuses_restore_before_mutation_and_keeps_shown_project_chats( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + // Capacity refusal is independent of warm-start connections. + lazy_start(&f, cx); + let mut retained = Vec::new(); + let mut threads = Vec::new(); + cx.update_window(f.handle, |_, window, cx| { + let general = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + for index in 1..=3 { + let project = nocterm_project_model::ProjectId::from_bytes([index; 16]); + let (seat, surface, outer) = super::seat_binding::project_seat(&f, project, window, cx); + let scope = nocterm_ai::history::ContextId::Project(project); + let deps = crate::chat_deps::ChatDeps::new( + general.runtime().for_scope(scope), + general.agents().clone(), + general.settings().clone(), + f._directory.metadata.clone(), + seat, + None, + ); + let mut chat = saved(&f); + chat.header.scope = scope; + chat.draft = Some("original literal draft".into()); + if index < 3 { + chat.entries = vec![Entry::Agent("a".repeat(12 * 1024 * 1024))]; + } + let thread = deps.restore_chat(chat, cx).unwrap(); + thread.update(cx, |thread, cx| thread.set_shown(true, cx)); + threads.push(thread); + retained.push((surface, outer)); + } + let requester = threads.last().unwrap(); + let before = requester.read(cx).snapshot(cx).unwrap(); + let mut incoming = before.clone(); + incoming.entries = vec![Entry::Agent("foreign replacement".repeat(1024 * 1024))]; + assert!(matches!( + requester.update(cx, |thread, cx| thread.restore_content(incoming, cx)), + Err(nocterm_ai::storage::AgentStorageError::ResidentCapacity { .. }) + )); + let after = requester.read(cx).snapshot(cx).unwrap(); + assert_eq!( + serde_json::to_value(before).unwrap(), + serde_json::to_value(after).unwrap() + ); + for thread in &threads[..2] { + assert!(thread.read(cx).resident_history_bytes() > 24 * 1024 * 1024); + } + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(f.connector.connects.load(Ordering::SeqCst), 0); +} + +#[gpui_kit::test] +fn deleting_a_loading_chat_keeps_its_actual_decoded_native_result_charged_until_drop( + cx: &mut TestAppContext, +) { + use nocterm_agent_runtime::history_admission::{ + MAX_NORMAL_HISTORY_BYTES, MAX_NORMAL_HISTORY_PAYLOADS, + }; + let f = fixture(cx); + cx.run_until_parked(); + let mut chat = saved(&f); + chat.entries.push(Entry::Agent("x".repeat(2 * 1024 * 1024))); + let thread = archived(&f, &chat, cx); + let decoded = f + .repository + .block_next(nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone(), + )); + f.panel + .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); + cx.run_until_parked(); + assert!( + f.repository + .operations() + .contains(&nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone() + )) + ); + let admission = cx.update(|cx| f.installation.installed.runtime().history_admission(cx)); + cx.update_window(f.handle, |_, window, cx| { + f.panel.update(cx, |panel, cx| { + panel.delete_thread(thread.entity_id(), window, cx) + }) + }) + .unwrap(); + drop(thread); + cx.run_until_parked(); + let slots = (0..MAX_NORMAL_HISTORY_PAYLOADS) + .filter_map(|_| admission.reserve_payload().ok()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); + cx.update(|cx| { + assert!( + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES - 2 * 1024 * 1024, cx) + .is_err() + ) + }); + decoded.send(Ok(())).unwrap(); + cx.run_until_parked(); + let released = admission.reserve_payload().unwrap(); + cx.update(|cx| { + drop( + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES - 2 * 1024 * 1024, cx) + .unwrap(), + ) + }); + assert!(f.repository.transcript(&chat.header.id).is_err()); + drop((released, slots)); +} diff --git a/crates/agent/src/panel/tests/authentication.rs b/crates/agent/src/panel/tests/authentication.rs new file mode 100644 index 00000000..20a92291 --- /dev/null +++ b/crates/agent/src/panel/tests/authentication.rs @@ -0,0 +1,238 @@ +//! Sign-in controls, prompt authentication and terminal sign-in retries. +use super::*; +use nocterm_ai::model::tools::ToolStatus; + +#[gpui_kit::test] +fn authentication_controls_follow_required_state_and_create_session_after_click( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + f.commands.auth_required.store(true, Ordering::SeqCst); + new_chat(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let thread = f.panel.read(cx).current().unwrap(); + assert!(thread.read(cx).session_sign_in_required()); + assert!( + thread.read(cx).registration().is_some(), + "auth-required must preserve bridge for retry" + ); + assert!(thread.read(cx).session().is_none()); + window.click("authenticate-sign-in", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let thread = f.panel.read(cx).current().unwrap(); + assert!(!thread.read(cx).session_sign_in_required()); + assert!(thread.read(cx).session().is_some()); + assert_eq!(thread.read(cx).status(), "Ready"); + assert!(window.try_find("authenticate-sign-in").is_none()); + assert!(window.try_find("agent-status").is_none()); + }) + .unwrap(); + assert_eq!(f.commands.authentications.load(Ordering::SeqCst), 1); + assert_eq!(f.commands.sessions.load(Ordering::SeqCst), 2); +} + +#[gpui_kit::test] +fn ready_hides_auth_and_status_while_working_status_precedes_composer(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("authenticate-sign-in").is_none()); + assert!(window.try_find("agent-status").is_none()); + }) + .unwrap(); + cx.update(|cx| { + f.panel + .read(cx) + .current() + .unwrap() + .update(cx, |thread, cx| { + thread.send("Question".into(), cx); + }); + }); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let status = window.find("agent-status").bounds(); + let composer = window.find("agent-composer").bounds(); + assert!(status.bottom() <= composer.origin.y); + let thinking = window.find("agent-thinking").bounds(); + assert!(status.contains(&thinking.origin)); + assert_eq!(thinking.origin.x, status.origin.x + gpui_kit::px(12.)); + let thread = f.panel.read(cx).current().unwrap(); + assert_eq!(thread.read(cx).status(), "Thinking…"); + assert!(window.try_find("authenticate-sign-in").is_none()); + }) + .unwrap(); +} + +#[gpui_kit::test] +fn prompt_authentication_failure_keeps_retry_and_existing_session(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + f.commands.auth_required.store(true, Ordering::SeqCst); + f.commands + .authentication_failure + .store(true, Ordering::SeqCst); + cx.update(|cx| { + f.panel + .read(cx) + .current() + .unwrap() + .update(cx, |thread, cx| { let entries = ["pending", "completed"].into_iter().map(|status| { + nocterm_ai::thread::Entry::Tool(serde_json::from_value(serde_json::json!({"toolCallId":status,"title":"Before auth required","status":status})).unwrap()) + }).collect(); + restore_entries(&f, thread, entries, cx); + thread.send("Question".into(), cx); }); + }); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let thread = f.panel.read(cx).current().unwrap(); + assert!(thread.read(cx).session_sign_in_required()); + assert!(!thread.read(cx).session_generating()); + let statuses = thread + .read(cx) + .transcript() + .entries + .iter() + .filter_map(|entry| { + if let nocterm_ai::thread::Entry::Tool(call) = entry { + Some(call.status) + } else { + None + } + }) + .collect::>(); + assert_eq!(statuses, vec![ToolStatus::Failed, ToolStatus::Succeeded]); + thread.update(cx, |thread, cx| { + thread.send("Do not submit while auth-required".into(), cx) + }); + assert_eq!(f.commands.prompts.lock().unwrap().len(), 1); + window.click("authenticate-sign-in", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let thread = f.panel.read(cx).current().unwrap(); + assert!(thread.read(cx).session_sign_in_required()); + assert!(!thread.read(cx).session_signing_in()); + assert_eq!(thread.read(cx).status(), "Sign-in failed"); + assert!(thread.read(cx).session().is_some()); + f.commands + .authentication_failure + .store(false, Ordering::SeqCst); + window.click("authenticate-sign-in", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let thread = f.panel.read(cx).current().unwrap(); + assert!(!thread.read(cx).session_sign_in_required()); + assert!(thread.read(cx).session().is_some()); + assert!(window.try_find("authenticate-sign-in").is_none()); + }) + .unwrap(); + assert_eq!(f.commands.authentications.load(Ordering::SeqCst), 2); + assert_eq!(f.commands.sessions.load(Ordering::SeqCst), 1); +} + +/// Asserts that terminal sign-in failed with `status` and remains retryable, then retries it. +fn retry_failed_terminal_sign_in(f: &Fixture, status: &str, cx: &mut TestAppContext) { + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let thread = f.panel.read(cx).current().unwrap(); + assert!(thread.read(cx).session_sign_in_required()); + assert!(!thread.read(cx).session_signing_in()); + assert_eq!(thread.read(cx).status(), status); + window.click("authenticate-setup", cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn terminal_authentication_uses_host_callback_and_retries_after_cancel_or_failure( + cx: &mut TestAppContext, +) { + let mut f = fixture(cx); + f.commands.auth_required.store(true, Ordering::SeqCst); + let requests = Arc::new(Mutex::new(Vec::new())); + let captured = requests.clone(); + let commands = f.commands.clone(); + let opener: crate::TerminalAuthOpener = Arc::new(move |_, request, _, _| { + let mut requests = captured.lock().unwrap(); + requests.push(request); + let (send, receive) = oneshot::channel(); + match requests.len() { + 1 => {} + 2 => send.send(Err(crate::TerminalAuthError::Failed)).unwrap(), + _ => { + commands.auth_required.store(false, Ordering::SeqCst); + send.send(Ok(())).unwrap(); + } + } + receive + }); + install_fixture( + &mut f, + |f| { + let mut services = fixture_services(f); + services.terminal_auth = Some(opener); + services + }, + cx, + ); + let unrelated = fixture(cx); + f.connector.report_auth_methods(vec![AuthMethod::terminal( + "setup", + "Terminal setup", + nocterm_ai::model::auth::TerminalSetup { + args: vec!["--setup".into(), "$(literal_argument)".into()], + env: Default::default(), + }, + )]); + new_chat(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + window.click("authenticate-setup", cx); + }) + .unwrap(); + cx.run_until_parked(); + retry_failed_terminal_sign_in(&f, "Sign-in cancelled", cx); + retry_failed_terminal_sign_in( + &f, + "Authentication command did not complete successfully.", + cx, + ); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + let thread = f.panel.read(cx).current().unwrap(); + assert!(!thread.read(cx).session_sign_in_required()); + assert!(thread.read(cx).session().is_some()); + assert!(window.try_find("authenticate-setup").is_none()); + }) + .unwrap(); + assert_eq!( + f.commands.authentications.load(Ordering::SeqCst), + 0, + "terminal methods must not use the agent authenticate RPC" + ); + assert_eq!(unrelated.connector.connects.load(Ordering::SeqCst), 0); + assert_eq!(unrelated.commands.authentications.load(Ordering::SeqCst), 0); + let requests = requests.lock().unwrap(); + assert_eq!(requests.len(), 3); + assert_eq!(requests[0].program, "npx"); + assert!( + requests[0] + .args + .ends_with(&["--setup".into(), "$(literal_argument)".into()]) + ); +} diff --git a/crates/agent/src/panel/tests/bridge_outcomes.rs b/crates/agent/src/panel/tests/bridge_outcomes.rs index 472ac994..27a58403 100644 --- a/crates/agent/src/panel/tests/bridge_outcomes.rs +++ b/crates/agent/src/panel/tests/bridge_outcomes.rs @@ -85,9 +85,12 @@ fn check_provider(provider: &str, cx: &mut TestAppContext) { update(&f, &session, SessionUpdate::ToolCall(Box::new(row)), cx); if id == "mcp-rejection" { let mut mcp = nocterm_ai::mcp::McpSession::default(); - mcp.handle(r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#); + mcp.handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + Default::default(), + ); let nocterm_ai::mcp::McpStep::Rejected { tool, arguments, error, .. } = - mcp.handle(&json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":tool,"arguments":args}}).to_string()) + mcp.handle(&json!({"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":tool,"arguments":args}}).to_string(), Default::default()) else { panic!("rejected"); }; f.rejections .try_send(nocterm_ai::BridgeRejection { diff --git a/crates/agent/src/panel/tests/chat_servers.rs b/crates/agent/src/panel/tests/chat_servers.rs new file mode 100644 index 00000000..101a9515 --- /dev/null +++ b/crates/agent/src/panel/tests/chat_servers.rs @@ -0,0 +1,63 @@ +//! A chat whose agent runs elsewhere attaches the servers its agent may sign in to, from the +//! attach menu, instead of this device's terminals; the attached ones show as chips. +use super::*; +use nocterm_ai::model::controls::{ConfigCategory, ConfigChoice, ConfigOption, ConfigSetting}; + +fn server(id: &str, name: &str, attached: bool) -> ConfigOption { + ConfigOption::boolean(format!("server:{id}"), name, attached) + .description(format!("root@{id}.example")) + .category(ConfigCategory::Server) +} + +fn hub_chat(f: &Fixture, cx: &mut TestAppContext) { + f.connector.run_elsewhere(); + f.commands.config.set_options(vec![ + ConfigOption::select("model", "Model", "m1", vec![ConfigChoice::new("m1", "m1")]) + .category(ConfigCategory::Model), + server("web", "Web", false), + server("build", "Build", true), + ]); + new_chat(f, cx); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn the_attach_menu_of_an_agent_that_runs_elsewhere_lists_the_chats_servers( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + hub_chat(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + // Servers are not configured beside the model; one is attached already. + assert!(window.try_find("config-picker-model").is_some()); + assert!(window.try_find("config-picker-server:web").is_none()); + assert!(window.try_find("server-chip-0").is_some()); + assert!(window.try_find("server-chip-1").is_none()); + window.click("agent-attach-context", cx); + window.render_frame(cx); + assert!(window.try_find(("attach-server", 0usize)).is_some()); + assert!(window.try_find(("attach-server", 1usize)).is_some()); + // None of this device's terminals or saved servers are offered. + assert!(window.try_find("attach-group-homelab").is_none()); + window.click(("attach-server", 0usize), cx); + }) + .unwrap(); + cx.run_until_parked(); + let request = f.commands.config.requests().pop().expect("a choice"); + assert_eq!(request.config.as_str(), "server:web"); + assert_eq!(request.setting, ConfigSetting::Boolean(true)); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("server-chip-1").is_some()); + // The menu stays open for more choices; once closed, a chip's cross detaches its server. + assert!(window.try_find(("attach-server", 0usize)).is_some()); + window.click("agent-attach-context", cx); + window.render_frame(cx); + window.click("server-chip-0", cx); + }) + .unwrap(); + cx.run_until_parked(); + let request = f.commands.config.requests().pop().expect("a choice"); + assert_eq!(request.setting, ConfigSetting::Boolean(false)); +} diff --git a/crates/agent/src/panel/tests/commands.rs b/crates/agent/src/panel/tests/commands.rs index 1c1e3437..1404ce53 100644 --- a/crates/agent/src/panel/tests/commands.rs +++ b/crates/agent/src/panel/tests/commands.rs @@ -49,7 +49,7 @@ fn clicked_unicode_command_keeps_focus_and_appends_at_end(cx: &mut TestAppContex #[gpui_kit::test] fn long_description_has_same_row_height_and_bounded_full_details(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let long = "First paragraph\n\n## Instructions\n\n".to_string() + &"Extensive description with examples.\n".repeat(150); diff --git a/crates/agent/src/panel/tests/composer.rs b/crates/agent/src/panel/tests/composer.rs index 7ce01a72..72ccb795 100644 --- a/crates/agent/src/panel/tests/composer.rs +++ b/crates/agent/src/panel/tests/composer.rs @@ -4,79 +4,7 @@ use nocterm_ai::model::controls::{ConfigChoice, ConfigOption}; use nocterm_ai::model::session::{PromptOutcome, SessionNotice, SessionUpdate, StopReason}; use nocterm_ai::model::usage::{ContextUsage, TokenTotals}; -#[gpui_kit::test] -fn usage_card_shows_context_tokens_and_plan_limits(cx: &mut TestAppContext) { - let source = Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()); - source.push(Ok(Some(vec![ - nocterm_ai::usage::RateLimit { - window: nocterm_ai::usage::Window::Weekly, - used: Some(0.38), - resets_at: Some(1_791_588_105), - reached: false, - }, - nocterm_ai::usage::RateLimit { - window: nocterm_ai::usage::Window::FiveHour, - used: Some(0.12), - resets_at: Some(1_791_500_000), - reached: false, - }, - ]))); - let f = fixture_with_width(cx, 26., source, workspace_resolver(), image_deps()); - new_chat(&f, cx); - let session = cx.update(|cx| { - let thread = f.panel.read(cx).current().unwrap(); - thread.update(cx, |thread, cx| thread.send("hello".into(), cx)); - thread.read(cx).session().clone().unwrap() - }); - cx.run_until_parked(); - f.events - .try_send(AgentEvent::Session(SessionNotice::new( - session, - SessionUpdate::UsageUpdate(ContextUsage::new(25_900, 256_000)), - ))) - .unwrap(); - cx.run_until_parked(); - let finish = f.commands.pending.lock().unwrap().take().unwrap(); - finish - .send(PromptOutcome { - usage: Some(TokenTotals { - cache_read: Some(20_000), - ..TokenTotals::new(30_000, 25_000, 5_000) - }), - ..PromptOutcome::new(StopReason::EndTurn) - }) - .unwrap(); - cx.run_until_parked(); - cx.update(|cx| { - let runtime = f.installation.installed.runtime(); - let limits: Vec<_> = runtime - .limits("codex", cx) - .unwrap() - .iter() - .cloned() - .collect(); - assert_eq!(limits.len(), 2); - assert_eq!(limits[0].window, nocterm_ai::usage::Window::FiveHour); - let thread = f.panel.read(cx).current().unwrap(); - assert_eq!( - thread.read(cx).transcript().tokens.as_ref().unwrap().total, - 30_000 - ); - }); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - window.click("agent-context-usage", cx); - window.render_frame(cx); - let card = window.find("agent-usage").bounds(); - let composer = window.find("agent-composer").bounds(); - // Over the chat, above the composer. - assert!(card.bottom() <= composer.origin.y + gpui_kit::px(1.)); - window.click("agent-usage-close", cx); - window.render_frame(cx); - assert!(window.try_find("agent-usage").is_none()); - }) - .unwrap(); -} +mod context_usage; #[gpui_kit::test] fn attach_menu_lists_servers_under_their_folders(cx: &mut TestAppContext) { @@ -152,7 +80,6 @@ fn image_preparation_runs_once_and_renders_a_cached_small_static_preview(cx: &mu let f = fixture_with_width( cx, 26., - usage_source(), workspace_resolver(), crate::ImageDeps::new(codec.clone(), codec.clone()), ); @@ -227,7 +154,7 @@ fn agent_popup_is_anchored_to_header_and_dismisses(cx: &mut TestAppContext) { #[gpui_kit::test] fn composer_popups_select_config_modes_and_context(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); f.commands.config.set_options(vec![ConfigOption::select( "model", "Model", @@ -367,7 +294,7 @@ fn transcript_activity_tracks_only_live_reasoning_and_unfinished_tools() { fn compact_chips_and_live_reasoning_respect_narrow_layout_and_reduced_motion( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); thread.update(cx, |thread, cx| thread.send("Question".into(), cx)); @@ -445,7 +372,7 @@ fn compact_chips_and_live_reasoning_respect_narrow_layout_and_reduced_motion( #[gpui_kit::test] fn expanded_long_mcp_command_json_and_reasoning_stay_within_narrow_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let long = "very_long_unbroken_argument_".repeat(300); cx.update(|cx| { @@ -514,52 +441,30 @@ fn expanded_long_mcp_command_json_and_reasoning_stay_within_narrow_panel(cx: &mu } #[gpui_kit::test] -fn prompt_completion_refreshes_its_original_usage_source_after_another_installation( - cx: &mut TestAppContext, -) { - let source = usage_source(); - source.push(Ok(Some(vec![nocterm_ai::usage::RateLimit { - window: nocterm_ai::usage::Window::Weekly, - used: Some(0.38), - resets_at: None, - reached: false, - }]))); - let first = fixture_with_width(cx, 26., source.clone(), workspace_resolver(), image_deps()); - new_chat(&first, cx); - let thread = cx.update(|cx| first.panel.read(cx).current().unwrap()); - thread.update(cx, |thread, cx| thread.send("original prompt".into(), cx)); - cx.run_until_parked(); - let finish = first.commands.pending.lock().unwrap().take().unwrap(); - let other_source = usage_source(); - let second = fixture_with_width( - cx, - 26., - other_source.clone(), - workspace_resolver(), - image_deps(), - ); - finish - .send(PromptOutcome::new(StopReason::EndTurn)) - .unwrap(); +fn a_file_of_any_kind_goes_with_the_message_as_a_link_and_shows_as_a_chip(cx: &mut TestAppContext) { + let f = fixture(cx); + lazy_start(&f, cx); + new_chat(&f, cx); + let file = f._directory.path().join("report.pdf"); + std::fs::write(&file, b"%PDF").unwrap(); + cx.update(|cx| { + f.panel + .update(cx, |panel, cx| panel.attach_paths(vec![file.clone()], cx)) + }); cx.run_until_parked(); - assert_eq!(source.requests().len(), 1); - assert!(other_source.requests().is_empty()); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find(("file-chip", 0usize)).is_some()); + }) + .unwrap(); + exchange(&f, "read it", "ok", cx); + let prompt = f.commands.prompts.lock().unwrap()[0].clone(); + assert!(prompt.content.iter().any(|part| matches!( + part, + Content::ResourceReference(link) if link.name == "report.pdf" && link.uri.starts_with("file://") + ))); cx.update(|cx| { - assert!( - first - .installation - .installed - .runtime() - .limits("codex", cx) - .is_some() - ); - assert!( - second - .installation - .installed - .runtime() - .limits("codex", cx) - .is_none() - ); + let thread = f.panel.read(cx).current().unwrap(); + assert!(thread.read(cx).composer().files().is_empty()); }); } diff --git a/crates/agent/src/panel/tests/composer/context_usage.rs b/crates/agent/src/panel/tests/composer/context_usage.rs new file mode 100644 index 00000000..81f3b502 --- /dev/null +++ b/crates/agent/src/panel/tests/composer/context_usage.rs @@ -0,0 +1,175 @@ +//! Existing context and plan usage presentation. +use super::*; + +#[gpui_kit::test] +fn usage_card_shows_context_tokens_and_plan_limits(cx: &mut TestAppContext) { + let f = fixture(cx); + panel_actions::new_agent_chat(&f, "claude", cx); + let session = cx.update(|cx| { + let thread = f.panel.read(cx).current().unwrap(); + thread.update(cx, |thread, cx| thread.send("hello".into(), cx)); + thread.read(cx).session().clone().unwrap() + }); + cx.run_until_parked(); + for (kind, used) in [("seven_day", 0.38), ("five_hour", 0.12)] { + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session.clone(), + SessionUpdate::UsageUpdate(plan_usage(kind, used)), + ))) + .unwrap(); + } + cx.run_until_parked(); + let finish = f.commands.pending.lock().unwrap().take().unwrap(); + finish + .send(PromptOutcome { + usage: Some(TokenTotals { + cache_read: Some(20_000), + ..TokenTotals::new(30_000, 25_000, 5_000) + }), + ..PromptOutcome::new(StopReason::EndTurn) + }) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + let runtime = f.installation.installed.runtime(); + let limits: Vec<_> = runtime + .limits("claude", cx) + .unwrap() + .iter() + .cloned() + .collect(); + assert_eq!(limits.len(), 2); + assert_eq!(limits[0].window, nocterm_ai::usage::Window::FiveHour); + let thread = f.panel.read(cx).current().unwrap(); + assert_eq!( + thread.read(cx).transcript().tokens.as_ref().unwrap().total, + 30_000 + ); + }); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + window.click("agent-context-usage", cx); + window.render_frame(cx); + let card = window.find("agent-usage").bounds(); + let composer = window.find("agent-composer").bounds(); + // Over the chat, above the composer. + assert!(card.bottom() <= composer.origin.y + gpui_kit::px(1.)); + window.click("agent-usage-close", cx); + window.render_frame(cx); + assert!(window.try_find("agent-usage").is_none()); + }) + .unwrap(); +} + +#[gpui_kit::test] +fn context_tokens_and_cost_remain_available_without_plan_limits(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("hello".into(), cx)); + cx.run_until_parked(); + let session = thread.read_with(cx, |thread, _| thread.session().clone().unwrap()); + let mut usage = ContextUsage::new(50, 100); + usage.cost = Some(nocterm_ai::model::usage::Cost { + amount: 1.25, + currency: "USD".into(), + }); + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session, + SessionUpdate::UsageUpdate(usage.clone()), + ))) + .unwrap(); + cx.run_until_parked(); + f.commands + .pending + .lock() + .unwrap() + .take() + .unwrap() + .send(PromptOutcome { + usage: Some(TokenTotals::new(30, 20, 10)), + ..PromptOutcome::new(StopReason::EndTurn) + }) + .unwrap(); + cx.run_until_parked(); + thread.read_with(cx, |thread, _| { + assert_eq!(thread.transcript().usage, Some(usage)); + assert_eq!( + thread.transcript().tokens, + Some(TokenTotals::new(30, 20, 10)) + ); + }); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + window.click("agent-context-usage", cx); + window.render_frame(cx); + assert!(window.try_find("agent-usage").is_some()); + assert!( + f.installation + .installed + .runtime() + .limits("codex", cx) + .is_none() + ); + }) + .unwrap(); +} + +fn plan_usage(kind: &str, used: f64) -> ContextUsage { + let meta = serde_json::json!({"_claude/rateLimit": { + "rateLimitType": kind, + "utilization": used, + "resetsAt": 1_791_588_105u64, + "status": "allowed" + }}); + ContextUsage::new(25_900, 256_000).meta( + meta.as_object() + .cloned() + .map(nocterm_ai::model::extensions::Extensions::from), + ) +} + +#[gpui_kit::test] +fn received_plan_limits_stay_with_their_original_installation_after_prompt_completion( + cx: &mut TestAppContext, +) { + let first = fixture(cx); + panel_actions::new_agent_chat(&first, "claude", cx); + let thread = cx.update(|cx| first.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("original prompt".into(), cx)); + cx.run_until_parked(); + let finish = first.commands.pending.lock().unwrap().take().unwrap(); + let second = fixture(cx); + panel_actions::new_agent_chat(&second, "claude", cx); + let session = thread.read_with(cx, |thread, _| thread.session().clone().unwrap()); + first + .events + .try_send(AgentEvent::Session(SessionNotice::new( + session, + SessionUpdate::UsageUpdate(plan_usage("seven_day", 0.38)), + ))) + .unwrap(); + finish + .send(PromptOutcome::new(StopReason::EndTurn)) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + let limits = first + .installation + .installed + .runtime() + .limits("claude", cx) + .unwrap(); + assert_eq!(limits.iter().next().unwrap().used, Some(0.38)); + assert!( + second + .installation + .installed + .runtime() + .limits("claude", cx) + .is_none() + ); + }); +} diff --git a/crates/agent/src/panel/tests/context_rosters.rs b/crates/agent/src/panel/tests/context_rosters.rs new file mode 100644 index 00000000..5de7c87e --- /dev/null +++ b/crates/agent/src/panel/tests/context_rosters.rs @@ -0,0 +1,449 @@ +//! Selected history membership changes while hidden documents keep their owners. +use super::*; +use nocterm_ai::history::{ContextId, SavedChat}; + +fn append( + roster: &mut crate::roster::ChatRoster, + thread: &Entity, + window: &Window, + cx: &mut App, +) { + let observation = cx.observe(thread, |_, _| {}); + let slot = roster.track(thread, window.window_handle(), observation, cx); + roster.append(slot); + roster.select(thread.entity_id(), cx); +} + +#[gpui_kit::test] +fn a_project_chat_starts_the_ordinary_local_agent_after_focus_moves_to_another_project( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + lazy_start(&f, cx); + let original = nocterm_project_model::ProjectId::from_bytes([1; 16]); + cx.update_window(f.handle, |_, window, cx| { + super::seat_binding::project_seat(&f, original, window, cx); + }) + .unwrap(); + cx.run_until_parked(); + new_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + let id = cx.update(|cx| thread.read(cx).chat_id().to_owned()); + cx.update_window(f.handle, |_, window, cx| { + thread.update(cx, |thread, cx| thread.send("Original A prompt".into(), cx)); + super::seat_binding::project_seat( + &f, + nocterm_project_model::ProjectId::from_bytes([2; 16]), + window, + cx, + ); + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(f.connector.connects.load(Ordering::SeqCst), 1); + assert_eq!(f.commands.prompts.lock().unwrap().len(), 1); + cx.update(|cx| { + let original_scope = ContextId::Project(original); + let chat = thread.read(cx); + assert_eq!(chat.document().key().scope(), original_scope); + assert!(chat.session().is_some()); + assert_eq!( + f.commands.prompts.lock().unwrap()[0].session, + *chat.session().as_ref().unwrap() + ); + assert!(f.panel.read(cx).current().is_none()); + assert_eq!(f.panel.read(cx).chat_documents().count(), 0); + assert_eq!( + f.repository + .transcript_in_scope(original_scope, &id) + .unwrap() + .header + .scope, + original_scope + ); + assert!(matches!( + f.repository.transcript(&id), + Err(nocterm_ai::storage::AgentStorageError::NotFound(_)) + )); + }); +} +#[gpui_kit::test] +fn switching_contexts_filters_chat_membership_and_retains_original_document_and_draft( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + cx.update_window(f.handle, |_, window, cx| { + let general = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + let mut roster = crate::roster::ChatRoster::new(general.clone()); + let chat = SavedChat::for_identity( + "codex".into(), + "00000000-0000-0000-0000-000000000001".into(), + 1, + ); + let first = general.restore_chat(chat.clone(), cx).unwrap(); + first.update(cx, |thread, cx| { + thread.set_draft("General literal draft".into(), cx) + }); + append(&mut roster, &first, window, cx); + let project = nocterm_project_model::ProjectId::from_bytes([1; 16]); + let (seat, _surface, _outer) = super::seat_binding::project_seat(&f, project, window, cx); + let scoped = crate::chat_deps::ChatDeps::new( + general.runtime().for_scope(ContextId::Project(project)), + general.agents().clone(), + general.settings().clone(), + f._directory.metadata.clone(), + seat, + None, + ); + roster.switch_context(scoped.clone(), cx); + assert!(roster.is_empty()); + assert!(roster.current().is_none()); + assert!(roster.contains_any(first.entity_id())); + let mut chat = chat; + chat.header.scope = scoped.scope(); + let second = scoped.restore_chat(chat, cx).unwrap(); + append(&mut roster, &second, window, cx); + assert_eq!( + roster + .iter() + .map(|thread| thread.entity_id()) + .collect::>(), + [second.entity_id()] + ); + assert_eq!(roster.host().scope(), scoped.scope()); + roster.switch_context(general, cx); + assert_eq!(roster.current().unwrap().entity_id(), first.entity_id()); + assert_eq!( + roster + .iter() + .map(|thread| thread.entity_id()) + .collect::>(), + [first.entity_id()] + ); + assert_eq!(first.read(cx).draft_text(), Some("General literal draft")); + assert!(roster.contains_any(second.entity_id())); + roster.switch_context(scoped, cx); + assert_eq!(roster.current().unwrap().entity_id(), second.entity_id()); + }) + .unwrap(); +} + +#[gpui_kit::test] +fn leaving_a_quiet_archived_context_releases_its_entity_and_original_catalog_reference( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + let project = nocterm_project_model::ProjectId::from_bytes([1; 16]); + let mut chat = SavedChat::for_identity( + "codex".into(), + "00000000-0000-0000-0000-000000000001".into(), + 1, + ); + chat.header.scope = ContextId::Project(project); + chat.entries.push(nocterm_ai::thread::Entry::Agent( + "archived literal answer".into(), + )); + f.repository.seed_chat(&chat).unwrap(); + let runtime = f + .installation + .installed + .runtime() + .for_scope(chat.header.scope); + cx.update(|cx| runtime.reload_history(cx)); + cx.run_until_parked(); + let (weak, _roster) = cx + .update_window(f.handle, |_, window, cx| { + let general = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + let (seat, _surface, _outer) = + super::seat_binding::pending_project_seat(&f, project, window, cx); + let scoped = crate::chat_deps::ChatDeps::new( + runtime.clone(), + general.agents().clone(), + general.settings().clone(), + f._directory.metadata.clone(), + seat, + None, + ); + let mut roster = crate::roster::ChatRoster::new(scoped); + let archived = roster.saved(cx).pop().unwrap(); + let weak = archived.downgrade(); + let observation = cx.observe(&archived, |_, _| {}); + let slot = roster.track(&archived, window.window_handle(), observation, cx); + roster.append(slot); + drop(archived); + roster.switch_context(general, cx); + assert!(!roster.contains_any(weak.entity_id())); + (weak, roster) + }) + .unwrap(); + cx.run_until_parked(); + assert!(weak.upgrade().is_none()); +} + +#[gpui_kit::test] +fn outer_workspace_selection_filters_panel_history_and_restores_general_literal_input( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + lazy_start(&f, cx); + new_chat(&f, cx); + set_input(&f, "General literal input", cx); + let original = cx.update(|cx| f.panel.read(cx).current().unwrap()); + let (_seat, _surface, outer) = cx + .update_window(f.handle, |_, window, cx| { + super::seat_binding::project_seat( + &f, + nocterm_project_model::ProjectId::from_bytes([1; 16]), + window, + cx, + ) + }) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + assert!(f.panel.read(cx).current().is_none()); + assert_eq!(f.panel.read(cx).chat_documents().count(), 0); + assert_eq!(f.panel.read(cx).composer_text(cx).as_ref(), ""); + }); + new_chat(&f, cx); + let project = cx.update(|cx| f.panel.read(cx).current().unwrap()); + cx.update(|cx| { + assert_eq!( + project.read(cx).document().key().scope(), + ContextId::Project(nocterm_project_model::ProjectId::from_bytes([1; 16])) + ); + assert_eq!(f.panel.read(cx).chat_documents().count(), 1); + }); + cx.update_window(f.handle, |_, window, cx| { + f.workspace.update(cx, |workspace, cx| { + workspace.activate_item_by_id(f.terminal, window, cx); + }) + }) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + assert_eq!( + f.panel.read(cx).current().unwrap().entity_id(), + original.entity_id() + ); + assert_eq!( + f.panel.read(cx).composer_text(cx).as_ref(), + "General literal input" + ); + assert_eq!(f.panel.read(cx).chat_documents().count(), 1); + }); + cx.update_window(f.handle, |_, window, cx| { + f.workspace.update(cx, |workspace, cx| { + workspace.activate_item_by_id(outer.entity_id(), window, cx); + }) + }) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + assert_eq!( + f.panel.read(cx).current().unwrap().entity_id(), + project.entity_id() + ) + }); +} + +#[gpui_kit::test] +fn accepted_archive_fork_finishes_in_its_hidden_original_context_without_changing_the_visible_chat( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + lazy_start(&f, cx); + let project = nocterm_project_model::ProjectId::from_bytes([1; 16]); + let mut saved = SavedChat::for_scope( + "codex".into(), + "00000000-0000-0000-0000-000000000001".into(), + 1, + ContextId::Project(project), + ); + saved + .entries + .push(nocterm_ai::thread::Entry::Agent("original A answer".into())); + f.repository.seed_chat(&saved).unwrap(); + let (_seat, _surface, outer) = cx + .update_window(f.handle, |_, window, cx| { + super::seat_binding::project_seat(&f, project, window, cx) + }) + .unwrap(); + cx.run_until_parked(); + let source = cx.update(|cx| { + f.panel + .read(cx) + .chat_documents() + .next() + .unwrap() + .entity_id() + }); + let release = f + .repository + .block_next(nocterm_ai::testing::StorageOperation::Load(saved.header.id)); + cx.update_window(f.handle, |_, window, cx| { + f.panel + .update(cx, |panel, cx| panel.fork_thread(source, window, cx)) + }) + .unwrap(); + cx.run_until_parked(); + let (_seat_b, _surface_b, _outer_b) = cx + .update_window(f.handle, |_, window, cx| { + super::seat_binding::project_seat( + &f, + nocterm_project_model::ProjectId::from_bytes([2; 16]), + window, + cx, + ) + }) + .unwrap(); + cx.run_until_parked(); + new_chat(&f, cx); + set_input(&f, "B literal draft", cx); + let visible = cx.update(|cx| f.panel.read(cx).current().unwrap().entity_id()); + release.send(Ok(())).unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + assert_eq!(f.panel.read(cx).current().unwrap().entity_id(), visible); + assert_eq!( + f.panel.read(cx).composer_text(cx).as_ref(), + "B literal draft" + ); + assert_eq!(f.panel.read(cx).chat_documents().count(), 1); + }); + cx.update_window(f.handle, |_, window, cx| { + f.workspace.update(cx, |workspace, cx| { + workspace.activate_item_by_id(outer.entity_id(), window, cx); + }) + }) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + assert_eq!(f.panel.read(cx).chat_documents().count(), 2); + let fork = f.panel.read(cx).current().unwrap(); + assert_ne!(fork.entity_id(), source); + assert_eq!( + fork.read(cx).document().key().scope(), + ContextId::Project(project) + ); + assert_eq!( + serde_json::to_value(fork.read(cx).history_entries()).unwrap(), + serde_json::to_value(&saved.entries).unwrap() + ); + }); +} + +#[gpui_kit::test] +async fn disabling_ai_releases_hidden_context_membership_and_preserves_every_scopes_draft( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + lazy_start(&f, cx); + new_chat(&f, cx); + set_input(&f, "General retained draft", cx); + let general = cx.update(|cx| f.panel.read(cx).current().unwrap()); + let general_id = cx.update(|cx| general.read(cx).chat_id().to_owned()); + let project = nocterm_project_model::ProjectId::from_bytes([1; 16]); + let (_seat, _surface, _outer) = cx + .update_window(f.handle, |_, window, cx| { + super::seat_binding::project_seat(&f, project, window, cx) + }) + .unwrap(); + cx.run_until_parked(); + new_chat(&f, cx); + set_input(&f, "Project retained draft", cx); + let scoped = cx.update(|cx| f.panel.read(cx).current().unwrap()); + let project_id = cx.update(|cx| scoped.read(cx).chat_id().to_owned()); + cx.update(|cx| { + f.installation + .ready + .settings() + .update::(cx, |settings| settings.enabled = false) + }) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!( + f.repository + .transcript(&general_id) + .unwrap() + .draft + .as_deref(), + Some("General retained draft") + ); + assert_eq!( + f.repository + .transcript_in_scope(ContextId::Project(project), &project_id) + .unwrap() + .draft + .as_deref(), + Some("Project retained draft") + ); + // Reenable and return to General: a fresh discovery must claim a fresh entity, + // rather than resurrecting the hidden retained roster after global disable. + cx.update(|cx| { + f.installation + .ready + .settings() + .update::(cx, |settings| settings.enabled = true) + }) + .await + .unwrap(); + cx.update_window(f.handle, |_, window, cx| { + f.workspace.update(cx, |workspace, cx| { + workspace.activate_item_by_id(f.terminal, window, cx); + }) + }) + .unwrap(); + cx.run_until_parked(); + cx.update(|cx| { + assert!( + f.panel + .read(cx) + .chat_documents() + .all(|thread| thread.entity_id() != general.entity_id()) + ); + assert!( + f.panel + .read(cx) + .chat_documents() + .any(|thread| thread.read(cx).chat_id() == general_id) + ); + }); +} + +#[gpui_kit::test] +fn forked_transcripts_contribute_their_payload_to_installation_residency(cx: &mut TestAppContext) { + let f = fixture(cx); + lazy_start(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + let general = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + let mut roster = crate::roster::ChatRoster::new(general.clone()); + let mut saved = SavedChat::for_identity( + "codex".into(), + "00000000-0000-0000-0000-000000000001".into(), + 1, + ); + saved + .entries + .push(nocterm_ai::thread::Entry::Agent("a".repeat(1024 * 1024))); + let source = general.restore_chat(saved, cx).unwrap(); + append(&mut roster, &source, window, cx); + let fork = roster.fork(&source, None, cx).unwrap(); + assert!(fork.read(cx).resident_history_bytes() >= 2 * 1024 * 1024); + assert_eq!(fork.read(cx).history_entries().len(), 1); + }) + .unwrap(); +} diff --git a/crates/agent/src/panel/tests/drafts.rs b/crates/agent/src/panel/tests/drafts.rs index d91ade66..470bbcfd 100644 --- a/crates/agent/src/panel/tests/drafts.rs +++ b/crates/agent/src/panel/tests/drafts.rs @@ -12,8 +12,7 @@ fn set_input(f: &Fixture, text: &str, cx: &mut TestAppContext) { } fn saved_value(f: &Fixture, id: &str) -> serde_json::Value { - let bytes = f.repository.encoded(id).unwrap(); - serde_json::from_slice(&bytes).unwrap() + disk_value(&f.repository, id) } pub(super) fn type_draft(f: &Fixture, text: &str, cx: &mut TestAppContext) { @@ -28,8 +27,15 @@ pub(super) fn disk_value( repository: &nocterm_ai::testing::InMemoryAgentRepository, id: &str, ) -> serde_json::Value { - let bytes = repository.encoded(id).unwrap(); - serde_json::from_slice(&bytes).unwrap() + // Assert public decoded document facts across both wire formats. The codec + // tests separately own byte framing and authoritative-envelope validation. + let chat = repository.transcript(id).unwrap(); + let mut value = serde_json::to_value(chat).unwrap(); + if matches!(value["version"].as_u64(), Some(2 | 3)) { + value["chat"].take() + } else { + value + } } mod saving; @@ -97,14 +103,12 @@ fn shutdown_flushes_the_latest_draft_before_a_debounce_can_run(cx: &mut TestAppC #[gpui_kit::test] fn restored_history_reopens_the_unsent_text_without_inserting_a_message(cx: &mut TestAppContext) { let mut f = fixture(cx); - let chat = nocterm_ai::history::SavedChat::for_identity( + let mut chat = nocterm_ai::history::SavedChat::for_identity( "codex".into(), "00000000-0000-4000-8000-00000000006a".into(), 1, ); - let mut value = serde_json::to_value(chat).unwrap(); - value["draft"] = serde_json::json!("restored\ntext"); - let chat = serde_json::from_value(value).unwrap(); + chat.draft = Some("restored\ntext".into()); reinstall_history(&mut f, vec![chat], cx); let panel = f.panel.clone(); cx.run_until_parked(); @@ -163,7 +167,7 @@ fn clearing_a_draft_is_reversible_for_the_same_chat_id(cx: &mut TestAppContext) assert!(chats.is_empty()); } else { assert_eq!(chats.len(), 1); - assert_eq!(chats[0].id, id); + assert_eq!(chats[0].header.id, id); assert_eq!(saved_value(&f, &id)["draft"], text); } } @@ -329,7 +333,12 @@ fn sending_a_large_draft_counts_its_text_once_and_rejection_keeps_it(cx: &mut Te // A second payload must fail without removing its composer draft. let next = "y".repeat(17 * 1024 * 1024); thread.set_draft(next.clone(), cx); - thread.begin_composer_edit(Vec::new(), vec![Attachment::Group("editing".into())], cx); + thread.begin_composer_edit( + Vec::new(), + Vec::new(), + vec![Attachment::Group("editing".into())], + cx, + ); let next_id = thread.composer().next_queue_id(); let queued = thread.composer().queue()[0].saved().clone(); let entries = thread.history_entries().len(); @@ -364,14 +373,66 @@ fn hiding_the_panel_flushes_its_latest_text_without_waiting_for_the_timer(cx: &m }); set_input(&f, "before hide", cx); cx.update_window(f.handle, |_, window, cx| { - f.workspace - .update(cx, |workspace, cx| workspace.toggle_right_panel(window, cx)); + f.workspace.update(cx, |workspace, cx| { + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx) + }); }) .unwrap(); cx.run_until_parked(); assert_eq!(saved_value(&f, &id)["draft"], "before hide"); } +#[gpui_kit::test] +fn each_close_flushes_the_draft_once_and_nothing_else_writes_it(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + let id = cx.update(|cx| { + f.panel + .read(cx) + .current() + .unwrap() + .read(cx) + .document() + .chat_id() + .to_owned() + }); + let toggle = |cx: &mut TestAppContext| { + cx.update_window(f.handle, |_, window, cx| { + f.workspace.update(cx, |workspace, cx| { + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx) + }); + }) + .unwrap(); + cx.run_until_parked(); + }; + set_input(&f, "before hide", cx); + let before = persistence::save_count(&f, &id); + + toggle(cx); + let hidden = persistence::save_count(&f, &id); + for _ in 0..2 { + cx.update_window(f.handle, |_, window, cx| { + f.workspace + .update(cx, |workspace, cx| workspace.toggle_sidebar(window, cx)); + }) + .unwrap(); + cx.run_until_parked(); + } + assert_eq!( + persistence::save_count(&f, &id), + hidden, + "toggling the sidebar while the panel is closed writes nothing" + ); + toggle(cx); + + assert_eq!(hidden, before + 1, "closing flushes once"); + assert_eq!( + persistence::save_count(&f, &id), + hidden, + "showing writes nothing" + ); +} + #[gpui_kit::test] fn a_retained_restarted_model_cannot_overwrite_the_replacement_draft(cx: &mut TestAppContext) { let f = fixture(cx); diff --git a/crates/agent/src/panel/tests/drafts/saving.rs b/crates/agent/src/panel/tests/drafts/saving.rs index e600ef7e..48c52224 100644 --- a/crates/agent/src/panel/tests/drafts/saving.rs +++ b/crates/agent/src/panel/tests/drafts/saving.rs @@ -87,7 +87,7 @@ fn shutdown_saves_a_dormant_chat_without_an_agent_connection(cx: &mut TestAppCon "00000000-0000-4000-8000-00000000006b".into(), 1, ); - let id = saved.id.clone(); + let id = saved.header.id.clone(); saved.draft = Some("saved text".into()); adopt(&mut f, saved, cx); let shutdown = cx.update(|cx| { @@ -115,7 +115,7 @@ fn shutdown_saves_edits_after_the_saved_provider_is_no_longer_configured(cx: &mu "00000000-0000-4000-8000-00000000006c".into(), 1, ); - let id = saved.id.clone(); + let id = saved.header.id.clone(); saved.draft = Some("previous text".into()); adopt(&mut f, saved, cx); let thread = cx.update(|cx| f.panel.read(cx).chat_documents().next().unwrap().clone()); @@ -147,7 +147,7 @@ fn unsent_history_survives_reopen_and_slash_only_then_clears_on_disk(cx: &mut Te let thread = f.panel.read(cx).current().unwrap(); thread.read(cx).snapshot(cx).unwrap() }); - let id = saved.id.clone(); + let id = saved.header.id.clone(); f.commands .restore_errors .lock() diff --git a/crates/agent/src/panel/tests/execution.rs b/crates/agent/src/panel/tests/execution.rs index ead80a30..36d27708 100644 --- a/crates/agent/src/panel/tests/execution.rs +++ b/crates/agent/src/panel/tests/execution.rs @@ -3,40 +3,33 @@ use super::*; use crate::thread::AgentThread; use nocterm_ai::model::auth::PermissionOutcome; use nocterm_ai::{CancelCommand, ExecCommand, ReadCommand, TerminalCall}; -use nocterm_session::{ExecExit, ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec}; +use nocterm_session::{ExecFuture, ExecOutput, ExecRequest, HostExec}; use serde_json::Value; use std::time::Duration; -#[derive(Default)] struct Programs { - requests: Mutex>, - sinks: Mutex>>>, + jobs: Arc, + offset: usize, } - impl HostExec for Programs { - fn exec(&self, request: ExecRequest) -> ExecFuture { - self.requests.lock().unwrap().push(request); - let (sink, output) = ExecOutput::channel(); - self.sinks.lock().unwrap().push(Some(Arc::new(sink))); - async move { Ok(output) }.boxed() + fn exec(&self, _: ExecRequest) -> ExecFuture { + panic!("the consumer's scripted command port must not perform transport I/O") } } - impl Programs { + fn new(jobs: Arc) -> Self { + let offset = jobs.requests().len(); + Self { jobs, offset } + } + fn requests(&self) -> Vec { + self.jobs.requests()[self.offset..].to_vec() + } fn closed(&self, index: usize) -> bool { - self.sinks.lock().unwrap()[index] - .as_ref() - .unwrap() - .is_closed() + !self.jobs.snapshot(self.offset + index).active() } - fn finish(&self, index: usize, stdout: &[u8], status: u32, stderr: &str) { - let sink = self.sinks.lock().unwrap()[index].take().unwrap(); - assert!(futures::executor::block_on(sink.send(stdout.to_vec()))); - sink.finish(ExecExit { - status: Some(status), - stderr: stderr.into(), - }); + self.jobs + .finish(self.offset + index, stdout, status, stderr); } } @@ -108,7 +101,7 @@ fn request(terminal_id: &str) -> ExecCommand { fn setup(cx: &mut TestAppContext) -> (Fixture, Entity, Arc, String) { let f = fixture(cx); - let programs = Arc::new(Programs::default()); + let programs = Arc::new(Programs::new(f._directory.jobs.clone())); *f.access.executor().borrow_mut() = Some(programs.clone()); new_chat(&f, cx); let thread = current(&f, cx); @@ -134,6 +127,12 @@ fn start( matches!(payload["state"].as_str(), Some("starting" | "running")), "{payload}" ); + let jobs = &f._directory.jobs; + let expected = f.access.executor().borrow().as_ref().unwrap().clone(); + assert!(Arc::ptr_eq( + &jobs.executor(jobs.requests().len() - 1), + &expected + )); payload["command_id"].as_str().unwrap().into() } @@ -158,17 +157,16 @@ fn read( } #[gpui_kit::test] -fn structured_requests_preserve_arguments_and_report_real_exit_with_redaction( +fn structured_requests_preserve_arguments_and_present_port_outcomes_with_redaction( cx: &mut TestAppContext, ) { let (f, thread, programs, terminal) = setup(cx); let id = start(&f, &thread, &terminal, cx); - let requests = programs.requests.lock().unwrap(); + let requests = programs.requests(); assert_eq!(requests.len(), 1); assert_eq!(requests[0].program, request(&terminal).program); assert_eq!(requests[0].args, request(&terminal).args); assert_eq!(requests[0].stdin.as_deref(), Some(&b"first\nsecond\n"[..])); - drop(requests); programs.finish( 0, b"output\nDB_PASSWORD=hunter2\n", @@ -223,7 +221,10 @@ fn a_silent_program_and_an_abandoned_read_remain_running_until_cancelled(cx: &mu .unwrap(); assert_eq!(cancelled["state"], "cancelled"); cx.run_until_parked(); - assert!(programs.closed(0), "cancelling drops the transport output"); + assert!( + programs.closed(0), + "cancelling reaches the original command port" + ); assert_eq!( read(&f, &thread, &terminal, &id, cx).unwrap()["state"], "cancelled" @@ -256,7 +257,7 @@ fn approval_rechecks_authentication_and_unsupported_execution_never_falls_back( .message() .contains("authentication") ); - assert!(programs.requests.lock().unwrap().is_empty()); + assert!(programs.requests().is_empty()); f.access.sign_in().asks().set(false); f.access.executor().borrow_mut().take(); let unsupported = result(submit( @@ -270,7 +271,7 @@ fn approval_rechecks_authentication_and_unsupported_execution_never_falls_back( unsupported.unwrap_err().message(), "This terminal does not support structured execution." ); - assert!(programs.requests.lock().unwrap().is_empty()); + assert!(programs.requests().is_empty()); assert!(f.access.sent().borrow().is_empty()); } @@ -313,7 +314,7 @@ fn active_command_limit_and_cancelled_slot_apply_through_bridge_calls(cx: &mut T fifth.unwrap_err().message(), "This chat already has four active commands. Read or cancel one first." ); - assert_eq!(programs.requests.lock().unwrap().len(), 4); + assert_eq!(programs.requests().len(), 4); result(submit( &f, &thread, @@ -327,7 +328,7 @@ fn active_command_limit_and_cancelled_slot_apply_through_bridge_calls(cx: &mut T .unwrap(); let next = start(&f, &thread, &terminal, cx); assert!(!ids.contains(&next)); - assert_eq!(programs.requests.lock().unwrap().len(), 5); + assert_eq!(programs.requests().len(), 5); assert!(programs.closed(0)); } @@ -428,7 +429,7 @@ fn removing_the_active_attachment_cancels_owned_programs(cx: &mut TestAppContext fn replacing_a_session_executor_revokes_old_commands_and_allows_new_ones(cx: &mut TestAppContext) { let (f, thread, programs, terminal) = setup(cx); let old = start(&f, &thread, &terminal, cx); - let replacement = Arc::new(Programs::default()); + let replacement = Arc::new(Programs::new(f._directory.jobs.clone())); *f.access.executor().borrow_mut() = Some(replacement.clone()); cx.executor().advance_clock(Duration::from_millis(150)); cx.run_until_parked(); @@ -439,7 +440,7 @@ fn replacing_a_session_executor_revokes_old_commands_and_allows_new_ones(cx: &mu ); let new = start(&f, &thread, &terminal, cx); assert_ne!(new, old); - assert_eq!(replacement.requests.lock().unwrap().len(), 1); + assert_eq!(replacement.requests().len(), 1); assert!(!replacement.closed(0)); } @@ -460,7 +461,7 @@ fn disabling_ai_cancels_execution_and_revokes_chat_access(cx: &mut TestAppContex } #[gpui_kit::test] -fn deleting_and_releasing_a_chat_drops_its_program_output(cx: &mut TestAppContext) { +fn deleting_and_releasing_a_chat_cancels_its_command_port(cx: &mut TestAppContext) { let (f, thread, programs, terminal) = setup(cx); start(&f, &thread, &terminal, cx); let id = thread.entity_id(); @@ -529,10 +530,13 @@ fn a_joined_reconnect_command_does_not_hold_input_until_its_process_exits(cx: &m )); assert!(result(input).is_ok()); assert_eq!(*f.access.sent().borrow(), ["following input\r"]); - assert_eq!(programs.requests.lock().unwrap().len(), 1); + assert_eq!(programs.requests().len(), 1); assert!(!programs.closed(0), "the first command is still running"); programs.finish(0, b"finished", 0, ""); cx.run_until_parked(); } +mod file_tools; +mod file_transfers; +mod local_environments; mod screenshot_history; diff --git a/crates/agent/src/panel/tests/execution/file_tools.rs b/crates/agent/src/panel/tests/execution/file_tools.rs new file mode 100644 index 00000000..71d34d85 --- /dev/null +++ b/crates/agent/src/panel/tests/execution/file_tools.rs @@ -0,0 +1,130 @@ +//! File tools: offered while an attached terminal serves files but runs no programs, reading +//! freely and changing files only with approval. +use super::*; +use nocterm_ai::{EditFile, ReadFile, ToolOffer}; +use nocterm_session::{HostAddress, Target, testing::InMemoryFs}; + +fn panel_host() -> HostAddress { + HostAddress::direct(Target { + host: "Game panel".into(), + port: 22, + user: "lobby".into(), + }) +} + +/// A chat whose attached terminal serves `fs` and runs no programs, as a plugin's console tab. +fn console(cx: &mut TestAppContext) -> (Fixture, Entity, Arc, String) { + let f = fixture(cx); + let fs = f.access.serve_remote_files(panel_host()); + fs.seed_file("/server.properties", "motd=Hi\nmax-players=20\n") + .unwrap(); + new_chat(&f, cx); + let thread = current(&f, cx); + let id = terminal_id(&thread, cx); + (f, thread, fs, id) +} + +fn last_offer(f: &Fixture, thread: &Entity, cx: &mut TestAppContext) -> ToolOffer { + let id = cx.update(|cx| thread.read(cx).registration().as_ref().unwrap().id); + f.bridge + .offers + .lock() + .unwrap() + .iter() + .rev() + .find(|(registration, _)| *registration == id) + .map(|(_, offer)| *offer) + .expect("the chat offered its tools") +} + +fn read(id: &str) -> TerminalCall { + TerminalCall::ReadFile(ReadFile { + terminal_id: id.into(), + path: "/server.properties".into(), + start_line: None, + max_lines: None, + }) +} + +fn edit(id: &str, old: &str, new: &str) -> TerminalCall { + TerminalCall::EditFile(EditFile { + terminal_id: id.into(), + path: "/server.properties".into(), + old_text: old.into(), + new_text: new.into(), + replace_all: false, + }) +} + +fn text(fs: &InMemoryFs) -> String { + String::from_utf8(fs.bytes("/server.properties").unwrap()).unwrap() +} + +#[gpui_kit::test] +fn file_tools_are_offered_only_while_such_a_terminal_is_attached(cx: &mut TestAppContext) { + let (f, thread, _, _) = console(cx); + assert_eq!(last_offer(&f, &thread, cx), ToolOffer::new(true)); + let context = cx.update(|cx| thread.update(cx, |thread, cx| thread.context(cx))); + assert!(context.contains(r#""file_tools":true"#), "{context}"); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread.attach(Attachment::Terminal(f.terminal), cx); + }) + }); + assert_eq!( + last_offer(&f, &thread, cx), + ToolOffer::new(false), + "detaching the terminal withdraws the file tools" + ); +} + +#[gpui_kit::test] +fn a_terminal_that_runs_programs_gets_no_file_tools(cx: &mut TestAppContext) { + let f = fixture(cx); + let fs = f.access.serve_remote_files(panel_host()); + fs.seed_file("/server.properties", "motd=Hi\n").unwrap(); + *f.access.executor().borrow_mut() = Some(Arc::new(Programs::new(f._directory.jobs.clone()))); + new_chat(&f, cx); + let thread = current(&f, cx); + assert_eq!(last_offer(&f, &thread, cx), ToolOffer::new(false)); + let id = terminal_id(&thread, cx); + let refused = result(submit(&f, &thread, read(&id), true, cx)).unwrap_err(); + assert!(refused.message().contains("file_tools: true"), "{refused}"); +} + +#[gpui_kit::test] +fn an_agent_reads_freely_and_changes_a_file_after_approval(cx: &mut TestAppContext) { + let (f, thread, fs, id) = console(cx); + let content = result(submit(&f, &thread, read(&id), false, cx)).unwrap(); + assert_eq!(content["content"], "motd=Hi\nmax-players=20\n"); + + let pending = submit( + &f, + &thread, + edit(&id, "max-players=20", "max-players=40"), + false, + cx, + ); + assert_eq!( + text(&fs), + "motd=Hi\nmax-players=20\n", + "nothing changes before the card is answered" + ); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, true, true, cx); + }) + }); + cx.run_until_parked(); + assert_eq!(result(pending).unwrap()["replacements"], 1); + assert_eq!(text(&fs), "motd=Hi\nmax-players=40\n"); + + let granted = submit(&f, &thread, edit(&id, "motd=Hi", "motd=Welcome"), false, cx); + assert!( + cx.update(|cx| thread.read(cx).tool_cards().is_empty()), + "the grant covers later changes on this terminal" + ); + result(granted).unwrap(); + assert_eq!(text(&fs), "motd=Welcome\nmax-players=40\n"); +} diff --git a/crates/agent/src/panel/tests/execution/file_transfers.rs b/crates/agent/src/panel/tests/execution/file_transfers.rs new file mode 100644 index 00000000..3c3f70bd --- /dev/null +++ b/crates/agent/src/panel/tests/execution/file_transfers.rs @@ -0,0 +1,333 @@ +//! Agent file transfers reach the transfer port only through approved, session-bound endpoints. +use super::*; +use gpui_kit::EntityId; +use nocterm_ai::TransferFiles; +use nocterm_session::{HostAddress, Target, testing::InMemoryFs}; +use nocterm_transfers::{ + CollisionPolicy, Request, TransferEndpoint, TransferId, TransferService, TransferState, +}; + +fn host(name: &str) -> HostAddress { + HostAddress::direct(Target { + host: name.into(), + port: 22, + user: "user".into(), + }) +} + +/// A background terminal served by its own remote host, for the chat to attach. +fn add_terminal(f: &Fixture, host_name: &str, cx: &mut TestAppContext) -> (Rc, EntityId) { + let access = Access::new(None, Rc::default()); + access.serve_remote_files(host(host_name)); + let item = cx + .update_window(f.handle, |_, window, cx| { + let terminal = cx.new(|cx| FakeTerminal::new(access.clone(), cx)); + f.workspace.update(cx, |workspace, cx| { + workspace.add_background_item(terminal, window, cx) + }) + }) + .unwrap(); + (access, item) +} +fn id_of(thread: &Entity, item: EntityId, cx: &mut TestAppContext) -> String { + cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread + .resolved(cx) + .into_iter() + .find(|(_, entry, _)| entry.item == item) + .map(|(id, _, _)| id) + .expect("the terminal is attached") + }) + }) +} + +struct Pair { + f: Fixture, + thread: Entity, + source: (Rc, String), + destination: (Rc, String), + third: String, +} +fn pair(cx: &mut TestAppContext) -> Pair { + let f = fixture(cx); + f.access.serve_remote_files(host("source")); + let (destination, destination_item) = add_terminal(&f, "destination", cx); + let (_, third_item) = add_terminal(&f, "third", cx); + new_chat(&f, cx); + let thread = current(&f, cx); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread.attach(Attachment::Terminal(destination_item), cx); + thread.attach(Attachment::Terminal(third_item), cx); + }) + }); + let source = terminal_id(&thread, cx); + Pair { + source: (f.access.clone(), source), + destination: (destination, id_of(&thread, destination_item, cx)), + third: id_of(&thread, third_item, cx), + f, + thread, + } +} + +fn transfer(pair: &Pair, destination: &str) -> TerminalCall { + transfer_between(&pair.source.1, destination) +} +fn transfer_between(source: &str, destination: &str) -> TerminalCall { + TerminalCall::TransferFiles(TransferFiles { + source_terminal_id: source.into(), + sources: vec!["/srv/a".into(), "/srv/dir".into()], + destination_terminal_id: destination.into(), + destination: "/srv/in".into(), + overwrite: true, + yield_ms: Some(0), + }) +} +fn read_transfer( + pair: &Pair, + id: &str, + cx: &mut TestAppContext, +) -> Result { + let call = TerminalCall::ReadTransfer(nocterm_ai::ReadTransfer { + transfer_id: id.into(), + yield_ms: Some(0), + }); + result(submit(&pair.f, &pair.thread, call, true, cx)) +} +fn submitted(pair: &Pair, payload: &Value) -> Request { + let id: TransferId = payload["transfer_id"].as_str().unwrap().parse().unwrap(); + pair.f._directory.transfers.request(id).unwrap() +} +fn states(pair: &Pair) -> Vec { + pair.f + ._directory + .transfers + .snapshot() + .into_iter() + .map(|progress| progress.state) + .collect() +} +fn decide(pair: &Pair, allow: bool, grant: bool, cx: &mut TestAppContext) { + cx.update(|cx| { + pair.thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, allow, grant, cx); + }) + }); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn an_approved_transfer_submits_a_relay_between_the_captured_endpoints(cx: &mut TestAppContext) { + let p = pair(cx); + let payload = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + assert_eq!(payload["state"], "queued"); + assert_eq!(payload["direction"], "relay"); + let Request::Relay(relay) = submitted(&p, &payload) else { + panic!("a remote to remote transfer is a relay"); + }; + assert_eq!(relay.sources, ["/srv/a", "/srv/dir"]); + assert_eq!(relay.destination, "/srv/in"); + assert_eq!(relay.source_host, host("source")); + assert_eq!(relay.host, host("destination")); + assert_eq!(relay.collisions, CollisionPolicy::Replace); +} + +#[gpui_kit::test] +fn a_local_source_uploads_and_a_denied_card_submits_nothing(cx: &mut TestAppContext) { + let p = pair(cx); + p.source.0.serve_local_files(); + let denied = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + assert!( + states(&p).is_empty(), + "nothing runs before the card is answered" + ); + decide(&p, false, false, cx); + assert!(result(denied).unwrap_err().to_string().contains("denied")); + assert!(states(&p).is_empty()); + let payload = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + assert!(matches!(submitted(&p, &payload), Request::Upload(_))); +} + +#[gpui_kit::test] +fn one_grant_covers_both_terminals_but_not_a_new_endpoint(cx: &mut TestAppContext) { + let p = pair(cx); + let third = p.third.clone(); + let first = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + decide(&p, true, true, cx); + assert!(result(first).is_ok()); + let repeated = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + assert!( + cx.update(|cx| p.thread.read(cx).tool_cards().is_empty()), + "the granted pair needs no card" + ); + assert!(result(repeated).is_ok()); + let _pending = submit(&p.f, &p.thread, transfer(&p, &third), false, cx); + assert_eq!(cx.update(|cx| p.thread.read(cx).tool_cards().len()), 1); +} + +#[gpui_kit::test] +fn grants_for_two_pairs_do_not_approve_a_transfer_across_them(cx: &mut TestAppContext) { + let p = pair(cx); + let (source, destination, third) = + (p.source.1.clone(), p.destination.1.clone(), p.third.clone()); + let first = submit( + &p.f, + &p.thread, + transfer_between(&source, &destination), + false, + cx, + ); + decide(&p, true, true, cx); + assert!(result(first).is_ok()); + let second = submit( + &p.f, + &p.thread, + transfer_between(&destination, &third), + false, + cx, + ); + assert_eq!( + cx.update(|cx| p.thread.read(cx).tool_cards().len()), + 1, + "the first pair's grant does not cover the destination's new partner" + ); + decide(&p, true, true, cx); + assert!(result(second).is_ok()); + let crossing = submit( + &p.f, + &p.thread, + transfer_between(&source, &third), + false, + cx, + ); + assert_eq!( + cx.update(|cx| p.thread.read(cx).tool_cards().len()), + 1, + "each terminal holds a grant, but never together" + ); + decide(&p, false, false, cx); + assert!(result(crossing).is_err()); + let reverse = submit( + &p.f, + &p.thread, + transfer_between(&third, &destination), + false, + cx, + ); + assert!(cx.update(|cx| p.thread.read(cx).tool_cards().is_empty())); + assert!(result(reverse).is_ok()); +} + +#[gpui_kit::test] +fn a_source_reconnecting_between_card_and_approval_detaches_the_call(cx: &mut TestAppContext) { + let p = pair(cx); + let reply = submit(&p.f, &p.thread, transfer(&p, &p.destination.1), false, cx); + p.source.0.replace_connection(); + decide(&p, true, false, cx); + assert!(result(reply).unwrap_err().to_string().contains("detached")); + assert!(states(&p).is_empty()); +} + +#[gpui_kit::test] +fn a_chat_reads_only_its_own_transfers(cx: &mut TestAppContext) { + let p = pair(cx); + let endpoint = |name: &str| TransferEndpoint::Remote { + host: host(name), + fs: Arc::new(InMemoryFs::default()), + }; + let foreign = nocterm_transfers::TransferRequests::submit( + &*p.f._directory.transfers, + Request::between( + endpoint("a"), + vec!["/x".into()], + endpoint("b"), + "/y".into(), + CollisionPolicy::Skip, + ) + .unwrap(), + ) + .unwrap(); + let refused = read_transfer(&p, &foreign.to_string(), cx).unwrap_err(); + assert!(refused.to_string().contains("not started by this chat")); + let own = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + let id = own["transfer_id"].as_str().unwrap(); + assert_eq!(read_transfer(&p, id, cx).unwrap()["state"], "queued"); +} + +#[gpui_kit::test] +fn stopping_the_chat_cancels_its_active_transfers(cx: &mut TestAppContext) { + let p = pair(cx); + let _ = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + assert_eq!(states(&p), [TransferState::Queued]); + cx.update(|cx| p.thread.update(cx, |thread, cx| thread.stop(cx))); + cx.run_until_parked(); + assert_eq!(states(&p), [TransferState::Cancelled]); +} + +#[gpui_kit::test] +fn replacing_an_endpoint_session_cancels_the_transfer(cx: &mut TestAppContext) { + let p = pair(cx); + let _ = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + p.source.0.replace_connection(); + cx.update(|cx| { + p.thread + .update(cx, |thread, cx| thread.revoke_executions(cx)) + }); + assert_eq!(states(&p), [TransferState::Cancelled]); +} + +#[gpui_kit::test] +fn cancel_transfer_stops_an_own_transfer_and_reports_it(cx: &mut TestAppContext) { + let p = pair(cx); + let own = result(submit( + &p.f, + &p.thread, + transfer(&p, &p.destination.1), + true, + cx, + )) + .unwrap(); + let call = TerminalCall::CancelTransfer(nocterm_ai::CancelTransfer { + transfer_id: own["transfer_id"].as_str().unwrap().into(), + }); + let report = result(submit(&p.f, &p.thread, call, true, cx)).unwrap(); + assert_eq!(report["state"], "cancelled"); +} diff --git a/crates/agent/src/panel/tests/execution/local_environments.rs b/crates/agent/src/panel/tests/execution/local_environments.rs new file mode 100644 index 00000000..a198b7fb --- /dev/null +++ b/crates/agent/src/panel/tests/execution/local_environments.rs @@ -0,0 +1,243 @@ +//! Original ACP callbacks cannot consume credentials belonging to a later account. +use super::*; +use nocterm_hubs_model::{ + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, +}; +fn account(name: &str) -> LocalAccountPartition { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("hub").unwrap(), + Fingerprint::from_bytes([1; 32]), + ), + AccountIdentity::new( + NamespaceId::new(name).unwrap(), + Fingerprint::from_bytes([2; 32]), + ), + )) +} +fn account_fixture(factory: Arc, cx: &mut TestAppContext) -> Fixture { + let mut f = fixture(cx); + let installed = f + .installation + .installed + .with_account_source(factory.clone()); + if let Ok(runtime) = installed.capture_runtime() { + f.access.bind_source(runtime.captured_source().unwrap()); + } + let (handle, workspace, panel, terminal) = cx.update(|cx| { + open_host( + &f._directory, + &installed, + &f.installation.layout, + &f.access, + cx, + ) + }); + f.installation.installed = installed; + f.handle = handle; + f.workspace = workspace; + f.panel = panel; + f.terminal = terminal; + f +} +#[gpui_kit::test] +fn locked_account_host_can_capture_after_unlock_without_rebuilding_its_window( + cx: &mut TestAppContext, +) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + let f = account_fixture(factory.clone(), cx); + cx.update(|cx| assert!(f.panel.read(cx).current().is_none())); + assert!(f.installation.installed.capture_runtime().is_err()); + factory.sign_in(account("alice")); + cx.update(|cx| { + f.installation + .installed + .runtime() + .refresh_environment_sources(cx) + }); + cx.run_until_parked(); + new_chat(&f, cx); + let original = current(&f, cx); + let original_key = cx.update(|cx| original.read(cx).document().key().clone()); + assert!(!original_key.environment().is_this_computer()); + factory.sign_in(account("bob")); + cx.update(|cx| { + f.installation + .installed + .runtime() + .refresh_environment_sources(cx) + }); + cx.run_until_parked(); + new_chat(&f, cx); + let replacement = current(&f, cx); + let replacement_key = cx.update(|cx| replacement.read(cx).document().key().clone()); + assert_ne!(replacement_key.environment(), original_key.environment()); + cx.update(|cx| { + assert!(!original.read(cx).source_current()); + assert!(replacement.read(cx).source_current()); + }); +} +fn refresh_sources(f: &Fixture, cx: &mut TestAppContext) { + cx.update(|cx| { + f.installation + .installed + .runtime() + .refresh_environment_sources(cx) + }); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| window.render_frame(cx)) + .unwrap(); +} +fn shown(f: &Fixture, id: &'static str, cx: &mut TestAppContext) -> bool { + cx.update_window(f.handle, |_, window, _| window.try_find(id).is_some()) + .unwrap() +} +#[gpui_kit::test] +fn a_locked_hub_account_shows_an_unlock_notice_and_clears_it_after_unlock(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + let f = account_fixture(factory.clone(), cx); + refresh_sources(&f, cx); + assert!(shown(&f, "agent-account-notice", cx)); + assert!( + !shown(&f, "agent-error", cx), + "locking is not an error line" + ); + + factory.sign_in(account("alice")); + refresh_sources(&f, cx); + assert!(!shown(&f, "agent-account-notice", cx)); + assert!(!shown(&f, "agent-error", cx)); +} +#[gpui_kit::test] +fn locking_after_capture_shows_the_notice_again(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + refresh_sources(&f, cx); + assert!(!shown(&f, "agent-account-notice", cx)); + + factory.close(); + refresh_sources(&f, cx); + assert!(shown(&f, "agent-account-notice", cx)); + assert!(!shown(&f, "agent-error", cx)); +} +#[gpui_kit::test] +fn the_unlock_button_asks_to_open_the_unlock_dialog(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + let f = account_fixture(factory, cx); + refresh_sources(&f, cx); + let unlocks = std::rc::Rc::new(std::cell::Cell::new(0)); + let counter = unlocks.clone(); + cx.update_window(f.handle, |_, window, cx| { + cx.on_action(move |_: &nocterm_workspace::UnlockVault, _| counter.set(counter.get() + 1)); + window.click("agent-account-action", cx); + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(unlocks.get(), 1); +} +#[gpui_kit::test] +fn a_signed_out_hub_account_shows_a_notice_with_a_sign_in_button(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + factory.sign_out(); + refresh_sources(&f, cx); + assert!(shown(&f, "agent-account-notice", cx)); + assert!(shown(&f, "agent-account-action", cx)); + assert!(!shown(&f, "agent-error", cx)); +} +#[gpui_kit::test] +fn old_approval_cannot_send_input_after_synchronous_account_replacement(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + new_chat(&f, cx); + let thread = current(&f, cx); + let terminal = terminal_id(&thread, cx); + let reply = submit( + &f, + &thread, + TerminalCall::SendInput(nocterm_ai::SendInput { + terminal_id: terminal, + text: "must not reach B".into(), + press_enter: true, + }), + false, + cx, + ); + factory.sign_in(account("bob")); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, true, false, cx); + }) + }); + cx.run_until_parked(); + assert!(result(reply).is_err()); + assert!(f.access.sent().borrow().is_empty()); + assert_eq!(f.access.link().reconnects().get(), 0); +} +#[gpui_kit::test] +fn exec_accepted_before_replacement_is_denied_before_its_first_native_poll( + cx: &mut TestAppContext, +) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + let programs = Arc::new(Programs::new(f._directory.jobs.clone())); + *f.access.executor().borrow_mut() = Some(programs.clone()); + new_chat(&f, cx); + let thread = current(&f, cx); + let terminal = terminal_id(&thread, cx); + let _reply = submit( + &f, + &thread, + TerminalCall::ExecCommand(request(&terminal)), + false, + cx, + ); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + let token = thread.tool_cards()[0].token(); + thread.approve_tool(token, true, false, cx); + // No background executor turn occurs between admission and revocation. + factory.sign_in(account("bob")); + }) + }); + cx.run_until_parked(); + assert_eq!( + programs.requests().len(), + 1, + "the job was admitted before replacement" + ); + assert_eq!( + f._directory.jobs.snapshot(0).state(), + nocterm_session::command_jobs::CommandState::Starting, + "the owned runner never reached its first transport poll" + ); + cx.background_executor + .advance_clock(Duration::from_millis(100)); + cx.run_until_parked(); + assert_eq!( + f._directory.jobs.snapshot(0).state(), + nocterm_session::command_jobs::CommandState::Cancelled + ); + assert!(f.access.sent().borrow().is_empty()); +} +#[gpui_kit::test] +fn a_message_in_a_hub_account_chat_reaches_the_agent(cx: &mut TestAppContext) { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + factory.sign_in(account("alice")); + let f = account_fixture(factory.clone(), cx); + refresh_sources(&f, cx); + new_chat(&f, cx); + let thread = current(&f, cx); + cx.update(|cx| thread.update(cx, |thread, cx| thread.send("hello".into(), cx))); + cx.run_until_parked(); + assert!(f.commands.pending.lock().unwrap().is_some()); + cx.update(|cx| assert!(!thread.read(cx).queue_dispatchable())); +} diff --git a/crates/agent/src/panel/tests/execution/screenshot_history.rs b/crates/agent/src/panel/tests/execution/screenshot_history.rs index 6c7849cd..79b6a106 100644 --- a/crates/agent/src/panel/tests/execution/screenshot_history.rs +++ b/crates/agent/src/panel/tests/execution/screenshot_history.rs @@ -46,10 +46,10 @@ fn screenshot_identity_and_real_mcp_result_survive_late_input_and_history(cx: &m yield_ms: Some(0), }; let payload = bridge(&f, TerminalCall::ExecCommand(request.clone()), cx); - let actual = program.request.lock().unwrap(); - let actual = actual - .as_ref() - .expect("exact attached executor received request"); + let requests = f._directory.jobs.requests(); + let actual = requests.first().expect("command port received request"); + let executor: Arc = program.clone(); + assert!(Arc::ptr_eq(&f._directory.jobs.executor(0), &executor)); assert_eq!(actual.program, "bash"); assert_eq!(actual.args, args); assert_eq!(actual.stdin.as_deref(), Some(&b"input\r\n"[..])); @@ -72,15 +72,9 @@ fn screenshot_identity_and_real_mcp_result_survive_late_input_and_history(cx: &m ); }); let stdout = "line\r\n\t```literal```\n界\n"; - let sink = program.sink.lock().unwrap().take().unwrap(); - assert!(futures::executor::block_on( - sink.send(stdout.as_bytes().to_vec()) - )); - sink.finish(ExecExit { - status: Some(7), - stderr: "warning\n".into(), - }); - drop(sink); + f._directory + .jobs + .finish(0, stdout.as_bytes(), 7, "warning\n"); cx.run_until_parked(); let result = bridge( &f, diff --git a/crates/agent/src/panel/tests/flow.rs b/crates/agent/src/panel/tests/flow.rs index c160c706..75af14af 100644 --- a/crates/agent/src/panel/tests/flow.rs +++ b/crates/agent/src/panel/tests/flow.rs @@ -234,27 +234,13 @@ fn restoration_keeps_descriptor_through_auth_and_falls_back_only_if_unavailable( .snapshot(cx) .unwrap() }); - f.panel.update(cx, |panel, cx| { - panel - .current() - .unwrap() - .update(cx, |thread, cx| thread.release_resources(cx)); - }); - cx.run_until_parked(); - let thread = cx.new(|cx| { - crate::thread::AgentThread::restored( - saved.clone(), - f._directory - .deps(f.workspace.downgrade(), f.installation.installed.clone()), - cx, - ) - }); + let thread = restoration::restore_released(&f, saved.clone(), cx); f.commands .restore_errors .lock() .unwrap() .push_back(AgentError::AuthRequired("Sign in".into())); - cx.update(|cx| activate_thread(&thread, &f.installation.installed, cx)); + cx.update(|cx| activate_thread(&thread, cx)); cx.run_until_parked(); cx.update(|cx| { assert!(thread.read(cx).session_sign_in_required()); @@ -364,28 +350,14 @@ fn transient_restore_retries_without_starting_an_empty_session(cx: &mut TestAppC .snapshot(cx) .unwrap() }); - f.panel.update(cx, |panel, cx| { - panel - .current() - .unwrap() - .update(cx, |thread, cx| thread.release_resources(cx)); - }); - cx.run_until_parked(); - let thread = cx.new(|cx| { - crate::thread::AgentThread::restored( - saved.clone(), - f._directory - .deps(f.workspace.downgrade(), f.installation.installed.clone()), - cx, - ) - }); + let thread = restoration::restore_released(&f, saved.clone(), cx); f.commands .restore_errors .lock() .unwrap() .push_back(AgentError::Io("temporary".into())); let count = f.commands.sessions.load(Ordering::SeqCst); - cx.update(|cx| activate_thread(&thread, &f.installation.installed, cx)); + cx.update(|cx| activate_thread(&thread, cx)); cx.run_until_parked(); cx.update(|cx| { assert_eq!( diff --git a/crates/agent/src/panel/tests/hermes.rs b/crates/agent/src/panel/tests/hermes.rs index a8bda0cf..08a4fc73 100644 --- a/crates/agent/src/panel/tests/hermes.rs +++ b/crates/agent/src/panel/tests/hermes.rs @@ -9,7 +9,6 @@ use nocterm_ai::model::tools::{ToolChanges, ToolContent, ToolPatch, ToolRecord, use nocterm_ai::{ ApprovalPolicy, ExecCommand, ReadCommand, TerminalCall, thread::Entry, tool_display, }; -use nocterm_session::ExecExit; use serde_json::{Value, json}; /// The text Hermes reports for an MCP result, as `tool_dispatch_helpers` builds it. @@ -114,7 +113,7 @@ fn assert_completed_command( thread: &Entity, session: &SessionId, terminal_id: String, - program: &Program, + program: &Arc, cx: &mut TestAppContext, ) { let script = "qm config 9000; echo '--- PVE STORAGE ---'; pvesm status"; @@ -136,15 +135,9 @@ fn assert_completed_command( cx, ); let started = bridge(f, TerminalCall::ExecCommand(request), cx); - let sink = program.sink.lock().unwrap().take().unwrap(); - assert!(futures::executor::block_on( - sink.send(b"boot: order=scsi0\n".to_vec()) - )); - sink.finish(ExecExit { - status: Some(0), - stderr: String::new(), - }); - drop(sink); + let executor: Arc = program.clone(); + assert!(Arc::ptr_eq(&f._directory.jobs.executor(0), &executor)); + f._directory.jobs.finish(0, b"boot: order=scsi0\n", 0, ""); cx.run_until_parked(); let finished = bridge( f, diff --git a/crates/agent/src/panel/tests/history.rs b/crates/agent/src/panel/tests/history.rs index d00a4720..055ec91a 100644 --- a/crates/agent/src/panel/tests/history.rs +++ b/crates/agent/src/panel/tests/history.rs @@ -1,9 +1,11 @@ +mod search; use super::*; use crate::thread::AgentThread; use nocterm_ai::model::content::Content; use nocterm_ai::model::controls::{ConfigCategory, ConfigChoice, ConfigOption}; use nocterm_ai::model::ids::SessionId; use nocterm_ai::model::session::{PromptOutcome, SessionNotice, SessionUpdate, StopReason}; +use nocterm_workspace::test_support::{Toggle, toggle_id}; #[gpui_kit::test] fn chats_are_saved_restored_into_a_new_panel_and_resume_their_session(cx: &mut TestAppContext) { @@ -38,7 +40,7 @@ fn chats_are_saved_restored_into_a_new_panel_and_resume_their_session(cx: &mut T [nocterm_ai::thread::Entry::User(_), nocterm_ai::thread::Entry::Agent(answer)] if answer == "answer" )); // The terminal context sent with the prompt is not part of the chat. - let text = String::from_utf8(f.repository.encoded(&saved[0].id).unwrap()).unwrap(); + let text = String::from_utf8(f.repository.encoded(&saved[0].header.id).unwrap()).unwrap(); assert!( !text.contains(nocterm_ai::context::TERMINAL_RULES), "{text}" @@ -71,7 +73,7 @@ fn chats_are_saved_restored_into_a_new_panel_and_resume_their_session(cx: &mut T // Opening it reopens the agent's session instead of starting another. let sessions = f.commands.sessions.load(Ordering::SeqCst); - cx.update(|cx| activate_thread(&thread, &f.installation.installed, cx)); + cx.update(|cx| activate_thread(&thread, cx)); cx.run_until_parked(); assert_eq!( *f.commands.restores.lock().unwrap(), @@ -190,15 +192,15 @@ fn history_searches_pins_renames_and_forks_chats(cx: &mut TestAppContext) { }) .unwrap(); let saved = f.repository.chats(); - assert_eq!(saved[0].name.as_deref(), Some("Disks")); - assert!(saved[0].pinned); + assert_eq!(saved[0].header.name.as_deref(), Some("Disks")); + assert!(saved[0].header.pinned); // A fork starts with the chat's messages, in a copy of its session. cx.update_window(f.handle, |_, window, cx| { f.panel.update(cx, |panel, cx| { panel.fork_thread(nginx.entity_id(), window, cx); let fork = panel.current().unwrap(); - activate_thread(&fork, &f.installation.installed, cx) + activate_thread(&fork, cx) }) }) .unwrap(); @@ -258,7 +260,12 @@ fn assert_unaccepted_prompts_leave_no_history( assert!(thread.history_entries().is_empty()); thread.send(" ".into(), cx); assert!(thread.history_entries().is_empty()); - thread.begin_composer_edit(Vec::new(), thread.composer().attachments().to_vec(), cx); + thread.begin_composer_edit( + Vec::new(), + Vec::new(), + thread.composer().attachments().to_vec(), + cx, + ); thread.send("Queued before the session is ready".into(), cx); assert_eq!(thread.composer().queue().len(), 1); let ids = thread.queued_prompt_ids().collect::>(); @@ -271,7 +278,7 @@ fn assert_unaccepted_prompts_leave_no_history( #[gpui_kit::test] fn history_records_only_accepted_prompts_and_keeps_model_snapshot(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| { let thread = f.panel.read(cx).current().unwrap(); @@ -407,33 +414,7 @@ fn second_history_window( f: &Fixture, cx: &mut TestAppContext, ) -> (AnyWindowHandle, Entity) { - let mut panel = None; - let (handle, _) = cx.update(|cx| { - gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { - cx.new(|cx| { - let mut workspace = Workspace::new( - nocterm_workspace::Deps::new(f.installation.layout.clone()), - window, - cx, - ); - let owner = cx.entity().downgrade(); - let view = cx.new(|cx| { - AgentPanel::new( - f._directory.host(owner, f.installation.installed.clone()), - window, - cx, - ) - }); - workspace.set_right_panel(view.clone(), window, cx); - workspace.set_right_panel_available(true, window, cx); - workspace.toggle_right_panel(window, cx); - panel = Some(view); - workspace - }) - }) - .unwrap() - }); - let panel = panel.unwrap(); + let (handle, panel) = open_window_panel(f, cx); cx.update_window(handle, |_, window, cx| { window.resize(gpui_kit::size(gpui_kit::px(1800.), gpui_kit::px(900.))); let focus = panel.read(cx).composer_focus(cx); @@ -490,7 +471,7 @@ fn settle_scheduled_history( #[gpui_kit::test] fn shared_history_width_refreshes_painted_windows_without_scalar_echo(cx: &mut TestAppContext) { use gpui_kit::{px, size}; - let f = fixture_with_width(cx, 60., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 60., workspace_resolver(), image_deps()); let layout = &f.installation.layout; cx.update(|cx| cx.set_reduce_motion(true)); new_chat(&f, cx); @@ -525,7 +506,12 @@ fn shared_history_width_refreshes_painted_windows_without_scalar_echo(cx: &mut T "rendered width adoption must not echo a scalar change" ); cx.update_window(other, |_, window, cx| { - window.dispatch_action(Box::new(nocterm_workspace::SwapSides), cx); + window.dispatch_action( + Box::new(nocterm_workspace::SwitchLayout { + id: "swapped".into(), + }), + cx, + ); window.render_frame(cx); window.click("agent-history", cx); window.render_frame(cx); @@ -565,7 +551,7 @@ fn shared_history_width_refreshes_painted_windows_without_scalar_echo(cx: &mut T #[gpui_kit::test] fn closing_one_window_keeps_the_surviving_history_layout_subscription(cx: &mut TestAppContext) { use gpui_kit::px; - let f = fixture_with_width(cx, 60., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 60., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| { cx.set_reduce_motion(true); @@ -616,7 +602,7 @@ fn shared_history_refresh_during_native_drag_keeps_later_pointer_events_safe( cx: &mut TestAppContext, ) { use gpui_kit::{InputEvent as _, MouseButton, MouseDownEvent, MouseUpEvent, point, px, size}; - let f = fixture_with_width(cx, 60., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 60., workspace_resolver(), image_deps()); let layout = &f.installation.layout; cx.update(|cx| cx.set_reduce_motion(true)); new_chat(&f, cx); @@ -698,7 +684,7 @@ fn shared_history_refresh_during_native_drag_keeps_later_pointer_events_safe( fn toggle_workspace_panel(f: &Fixture, cx: &mut TestAppContext) { cx.update_window(f.handle, |_, window, cx| { window.render_frame(cx); - window.click("toggle-right-panel", cx); + window.click(toggle_id(Toggle::SidePanel), cx); window.render_frame(cx); }) .unwrap(); diff --git a/crates/agent/src/panel/tests/history/search.rs b/crates/agent/src/panel/tests/history/search.rs new file mode 100644 index 00000000..cff89daf --- /dev/null +++ b/crates/agent/src/panel/tests/history/search.rs @@ -0,0 +1,72 @@ +//! Archived full-text search uses the same admitted native-body service. +use super::*; +use nocterm_agent_runtime::history_admission::{ + MAX_NORMAL_HISTORY_BYTES, MAX_NORMAL_HISTORY_PAYLOADS, +}; +use nocterm_ai::metadata::ChatMetadataSource as _; + +#[gpui_kit::test] +fn changing_an_archive_search_keeps_the_old_decoded_worker_charged_until_actual_drop( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + cx.run_until_parked(); + let identity = f._directory.metadata.new_identity(); + let mut chat = + nocterm_ai::history::SavedChat::for_identity("codex".into(), identity.id, identity.created); + chat.header.name = Some("Neutral saved title".into()); + chat.entries.push(nocterm_ai::thread::Entry::Agent(format!( + "oldnativeone {}", + "x".repeat(2 * 1024 * 1024) + ))); + let thread = super::super::archive::archived(&f, &chat, cx); + let decoded = f + .repository + .block_next(nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone(), + )); + super::super::panel_actions::show_history(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + replace_textbox("agent-history-search", "oldnativeone", window, cx); + }) + .unwrap(); + cx.run_until_parked(); + assert!( + f.repository + .operations() + .contains(&nocterm_ai::testing::StorageOperation::Decoded( + chat.header.id.clone() + )) + ); + let admission = cx.update(|cx| f.installation.installed.runtime().history_admission(cx)); + cx.update_window(f.handle, |_, window, cx| { + replace_textbox("agent-history-search", "replacement_no_match", window, cx); + }) + .unwrap(); + cx.run_until_parked(); + let slots = (0..MAX_NORMAL_HISTORY_PAYLOADS) + .filter_map(|_| admission.reserve_payload().ok()) + .collect::>(); + assert!(admission.reserve_payload().is_err()); + cx.update(|cx| { + assert!( + admission + .reserve_unowned_bytes(MAX_NORMAL_HISTORY_BYTES - 2 * 1024 * 1024, cx) + .is_err() + ) + }); + decoded.send(Ok(())).unwrap(); + cx.run_until_parked(); + let released = admission.reserve_payload().unwrap(); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("agent-history-no-match").is_some()); + assert!( + window + .try_find(("history-thread", thread.entity_id().as_u64())) + .is_none() + ); + }) + .unwrap(); + drop((released, slots)); +} diff --git a/crates/agent/src/panel/tests/invalidation.rs b/crates/agent/src/panel/tests/invalidation.rs index caa8a06e..ffe06393 100644 --- a/crates/agent/src/panel/tests/invalidation.rs +++ b/crates/agent/src/panel/tests/invalidation.rs @@ -60,8 +60,9 @@ fn closing_and_reopening_pending_chat_updates_attention_without_stream_updates( 0, "visible current approval needs no notice" ); - f.workspace - .update(cx, |w, cx| w.toggle_right_panel(window, cx)); + f.workspace.update(cx, |w, cx| { + w.toggle_panel(&crate::plugin::CHATS.id, window, cx) + }); }) .unwrap(); cx.run_until_parked(); @@ -79,15 +80,17 @@ fn closing_and_reopening_pending_chat_updates_attention_without_stream_updates( 0, "streaming must not repost a dismissed notice" ); - f.workspace - .update(cx, |w, cx| w.toggle_right_panel(window, cx)); + f.workspace.update(cx, |w, cx| { + w.toggle_panel(&crate::plugin::CHATS.id, window, cx) + }); }) .unwrap(); cx.run_until_parked(); cx.update_window(f.handle, |_, window, cx| { assert_eq!(nocterm_ui::notice::count(window, cx), 0); - f.workspace - .update(cx, |w, cx| w.toggle_right_panel(window, cx)); + f.workspace.update(cx, |w, cx| { + w.toggle_panel(&crate::plugin::CHATS.id, window, cx) + }); }) .unwrap(); cx.run_until_parked(); @@ -105,8 +108,9 @@ fn new_hidden_approval_reposts_after_dismissal_but_unchanged_stream_does_not( new_chat(&f, cx); let owner = cx.update(|cx| f.panel.read(cx).current().unwrap()); cx.update_window(f.handle, |_, window, cx| { - f.workspace - .update(cx, |w, cx| w.toggle_right_panel(window, cx)) + f.workspace.update(cx, |w, cx| { + w.toggle_panel(&crate::plugin::CHATS.id, window, cx) + }) }) .unwrap(); for id in ["first", "second"] { @@ -177,8 +181,18 @@ fn separate_installations_keep_original_runtime_and_policy_after_later_compositi }); cx.run_until_parked(); cx.update(|cx| { - assert!(first.workspace.read(cx).right_panel_is_available()); - assert!(!second.workspace.read(cx).right_panel_is_available()); + assert!( + first + .workspace + .read(cx) + .panel_is_available(&crate::plugin::CHATS.id) + ); + assert!( + !second + .workspace + .read(cx) + .panel_is_available(&crate::plugin::CHATS.id) + ); assert_eq!( first .installation @@ -216,6 +230,7 @@ fn refused_shutdown_registration_returns_a_usable_original_installation(cx: &mut f.installation.layout.clone(), &f.installation.ready, &refused, + None, cx, ); assert!(matches!( @@ -379,3 +394,132 @@ async fn ai_off_clears_edit_drafts_stream_and_queue_measurements(cx: &mut TestAp }) .unwrap(); } + +fn count_panel_notifications( + f: &Fixture, + cx: &mut TestAppContext, +) -> (Rc>, gpui_kit::Subscription) { + let notifications = Rc::new(Cell::new(0)); + let seen = notifications.clone(); + let observer = cx.update(|cx| cx.observe(&f.panel, move |_, _| seen.set(seen.get() + 1))); + (notifications, observer) +} + +#[gpui_kit::test] +fn a_streamed_chunk_draws_in_the_open_chat_without_redrawing_the_whole_panel( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "question", "answer", cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("next question".into(), cx)); + cx.run_until_parked(); + let streamed = cx.update(|cx| thread.read(cx).history_entries().len()); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find(("agent-entry", streamed)).is_none()); + }) + .unwrap(); + let (notifications, _observer) = count_panel_notifications(&f, cx); + let session = cx.update(|cx| thread.read(cx).session().clone().unwrap()); + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session, + SessionUpdate::AgentMessageChunk(Content::text("streamed answer")), + ))) + .unwrap(); + cx.run_until_parked(); + cx.executor() + .advance_clock(std::time::Duration::from_millis(100)); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + // A plain draw, which reuses cached views a refresh would redraw. + window.draw(cx).clear(cx); + assert!( + window.try_find(("agent-entry", streamed)).is_some(), + "the streamed answer draws in the open chat" + ); + }) + .unwrap(); + assert_eq!( + notifications.get(), + 0, + "a chunk redraws the chat's regions, not the whole panel" + ); +} + +#[gpui_kit::test] +fn typing_a_draft_saves_it_without_redrawing_the_whole_panel(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "question", "answer", cx); + cx.update_window(f.handle, |_, window, cx| window.render_frame(cx)) + .unwrap(); + let (notifications, _observer) = count_panel_notifications(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + replace_composer_value(f.panel.read(cx).composer_focus(cx), "a draft", window, cx); + window.render_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.executor() + .advance_clock(std::time::Duration::from_millis(100)); + cx.run_until_parked(); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + cx.update(|cx| { + assert_eq!(f.panel.read(cx).composer_text(cx).as_ref(), "a draft"); + assert_eq!(thread.read(cx).draft_text(), Some("a draft")); + }); + assert_eq!( + notifications.get(), + 0, + "typing redraws the composer, not the transcript and history" + ); +} + +#[gpui_kit::test] +fn a_growing_draft_grows_the_cached_composer_below_the_transcript(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "question", "answer", cx); + let draw = |cx: &mut TestAppContext| { + // Plain draws, which reuse cached regions a refresh would redraw. + for _ in 0..2 { + cx.update_window(f.handle, |_, window, cx| window.draw(cx).clear(cx)) + .unwrap(); + cx.run_until_parked(); + } + cx.update_window(f.handle, |_, window, _| { + ( + window.find("agent-composer").bounds(), + window.find("agent-transcript-area").bounds(), + ) + }) + .unwrap() + }; + let (before, _) = draw(cx); + cx.update_window(f.handle, |_, window, cx| { + replace_composer_value( + f.panel.read(cx).composer_focus(cx), + "one\ntwo\nthree\nfour\nfive", + window, + cx, + ); + }) + .unwrap(); + let (after, transcript) = draw(cx); + assert!( + after.size.height > before.size.height, + "the composer grows with its draft: {before:?} -> {after:?}" + ); + assert_eq!( + after.bottom(), + before.bottom(), + "the composer stays at the bottom" + ); + assert!( + transcript.bottom() <= after.top(), + "the transcript gives way: {transcript:?} above {after:?}" + ); +} diff --git a/crates/agent/src/panel/tests/labels.rs b/crates/agent/src/panel/tests/labels.rs index 13d8b337..5bd968a8 100644 --- a/crates/agent/src/panel/tests/labels.rs +++ b/crates/agent/src/panel/tests/labels.rs @@ -50,7 +50,7 @@ fn chat_label_heights(f: &Fixture, raw: &str, cx: &mut TestAppContext) -> [gpui_ #[gpui_kit::test] fn chat_header_and_history_keep_multiline_titles_on_one_line(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let prompt = "смотри в чем соль\nтут"; @@ -98,8 +98,8 @@ fn chat_header_and_history_keep_multiline_titles_on_one_line(cx: &mut TestAppCon }); cx.run_until_parked(); let saved = f.repository.chats(); - assert_eq!(saved[0].name.as_deref(), Some(long.as_str())); - assert_eq!(saved[0].title.as_deref(), Some(agent_title)); + assert_eq!(saved[0].header.name.as_deref(), Some(long.as_str())); + assert_eq!(saved[0].header.title.as_deref(), Some(agent_title)); assert!(matches!(&saved[0].entries[0], Entry::User(parts) if matches!(&parts[0], Content::Text(text) if text.text == prompt))); @@ -132,7 +132,7 @@ fn chat_header_and_history_keep_multiline_titles_on_one_line(cx: &mut TestAppCon #[gpui_kit::test] fn completed_and_live_heredoc_tool_headers_keep_one_line_and_raw_command(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); let script = "python3 - <<'PY'\nfrom pathlib import Path\nPath('/tmp/probe.txt').write_text('nocterm-ui-test-artifacts')\nPY"; diff --git a/crates/agent/src/panel/tests/lifecycle.rs b/crates/agent/src/panel/tests/lifecycle.rs index d059ebff..43e52933 100644 --- a/crates/agent/src/panel/tests/lifecycle.rs +++ b/crates/agent/src/panel/tests/lifecycle.rs @@ -1,8 +1,9 @@ use super::*; -use nocterm_ai::model::auth::{AuthMethod, PermissionOutcome}; +use nocterm_ai::model::auth::PermissionOutcome; use nocterm_ai::model::content::Content; use nocterm_ai::model::session::{PromptOutcome, SessionNotice, SessionUpdate, StopReason}; use nocterm_ai::model::tools::ToolStatus; +use nocterm_workspace::test_support::{Toggle, toggle_id}; #[gpui_kit::test] async fn empty_history_new_chat_streaming_context_and_master_off(cx: &mut TestAppContext) { @@ -53,8 +54,8 @@ async fn empty_history_new_chat_streaming_context_and_master_off(cx: &mut TestAp cx.run_until_parked(); cx.update_window(f.handle, |_, window, cx| { window.render_frame(cx); - assert!(!f.workspace.read(cx).right_panel_is_open()); - assert!(window.try_find("toggle-right-panel").is_none()); + assert!(!f.workspace.read(cx).panel_is_open(&crate::plugin::CHATS.id)); + assert!(window.try_find(toggle_id(Toggle::SidePanel)).is_none()); assert!(f.panel.read(cx).chat_documents().len() == 0); }) .unwrap(); @@ -100,7 +101,6 @@ async fn disabling_ai_cancels_pending_initialization_immediately(cx: &mut TestAp started: started.clone(), cancelled: cancelled.clone(), }); - services.usage_source = Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()); services.workdir = f._directory.path().join("pending"); services }, @@ -180,21 +180,23 @@ async fn master_switch_clears_all_windows_and_reenable_is_lazy(cx: &mut TestAppC gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { let mut workspace = Workspace::new( - nocterm_workspace::Deps::new(f.installation.layout.clone()), + crate::panel::tests::deps(f.installation.layout.clone()), window, cx, ); let owner = cx.entity().downgrade(); + let source = workspace.general_surface_source(cx); let view = cx.new(|cx| { AgentPanel::new( - f._directory.host(owner, f.installation.installed.clone()), + f._directory + .host(owner, f.installation.installed.clone(), source), window, cx, ) }); - workspace.set_right_panel(view.clone(), window, cx); - workspace.set_right_panel_available(true, window, cx); - workspace.toggle_right_panel(window, cx); + crate::panel::tests::host_panel(&mut workspace, &view, window, cx); + workspace.set_panel_available(&crate::plugin::CHATS.id, true, window, cx); + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx); panel = Some(view); workspace }) @@ -220,8 +222,8 @@ async fn master_switch_clears_all_windows_and_reenable_is_lazy(cx: &mut TestAppC cx.update(|cx| { assert!(f.panel.read(cx).chat_documents().len() == 0); assert!(panel.read(cx).chat_documents().len() == 0); - assert!(!f.workspace.read(cx).right_panel_is_open()); - assert!(!workspace.read(cx).right_panel_is_open()); + assert!(!f.workspace.read(cx).panel_is_open(&crate::plugin::CHATS.id)); + assert!(!workspace.read(cx).panel_is_open(&crate::plugin::CHATS.id)); }); assert_eq!(f.commands.shutdowns.load(Ordering::SeqCst), 1); assert_eq!(f.bridge.revoked.lock().unwrap().len(), 1); @@ -236,10 +238,12 @@ async fn master_switch_clears_all_windows_and_reenable_is_lazy(cx: &mut TestAppC cx.run_until_parked(); assert_eq!(f.connector.connects.load(Ordering::SeqCst), 1); cx.update_window(handle, |_, window, cx| { - workspace.update(cx, |workspace, cx| workspace.toggle_right_panel(window, cx)); + workspace.update(cx, |workspace, cx| { + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx) + }); window.render_frame(cx); assert!(window.try_find("agent-history-empty").is_some()); - assert!(window.try_find("toggle-right-panel").is_some()); + assert!(window.try_find(toggle_id(Toggle::SidePanel)).is_some()); }) .unwrap(); } @@ -328,242 +332,6 @@ fn stop_cancels_permissions_drops_late_updates_and_keeps_the_session(cx: &mut Te }); } -#[gpui_kit::test] -fn authentication_controls_follow_required_state_and_create_session_after_click( - cx: &mut TestAppContext, -) { - let f = fixture(cx); - f.commands.auth_required.store(true, Ordering::SeqCst); - new_chat(&f, cx); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let thread = f.panel.read(cx).current().unwrap(); - assert!(thread.read(cx).session_sign_in_required()); - assert!( - thread.read(cx).registration().is_some(), - "auth-required must preserve bridge for retry" - ); - assert!(thread.read(cx).session().is_none()); - window.click("authenticate-sign-in", cx); - }) - .unwrap(); - cx.run_until_parked(); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let thread = f.panel.read(cx).current().unwrap(); - assert!(!thread.read(cx).session_sign_in_required()); - assert!(thread.read(cx).session().is_some()); - assert_eq!(thread.read(cx).status(), "Ready"); - assert!(window.try_find("authenticate-sign-in").is_none()); - assert!(window.try_find("agent-status").is_none()); - }) - .unwrap(); - assert_eq!(f.commands.authentications.load(Ordering::SeqCst), 1); - assert_eq!(f.commands.sessions.load(Ordering::SeqCst), 2); -} - -#[gpui_kit::test] -fn ready_hides_auth_and_status_while_working_status_precedes_composer(cx: &mut TestAppContext) { - let f = fixture(cx); - new_chat(&f, cx); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - assert!(window.try_find("authenticate-sign-in").is_none()); - assert!(window.try_find("agent-status").is_none()); - }) - .unwrap(); - cx.update(|cx| { - f.panel - .read(cx) - .current() - .unwrap() - .update(cx, |thread, cx| { - thread.send("Question".into(), cx); - }); - }); - cx.run_until_parked(); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let status = window.find("agent-status").bounds(); - let composer = window.find("agent-composer").bounds(); - assert!(status.bottom() <= composer.origin.y); - let thinking = window.find("agent-thinking").bounds(); - assert!(status.contains(&thinking.origin)); - assert_eq!(thinking.origin.x, status.origin.x + gpui_kit::px(12.)); - let thread = f.panel.read(cx).current().unwrap(); - assert_eq!(thread.read(cx).status(), "Thinking…"); - assert!(window.try_find("authenticate-sign-in").is_none()); - }) - .unwrap(); -} - -#[gpui_kit::test] -fn prompt_authentication_failure_keeps_retry_and_existing_session(cx: &mut TestAppContext) { - let f = fixture(cx); - new_chat(&f, cx); - f.commands.auth_required.store(true, Ordering::SeqCst); - f.commands - .authentication_failure - .store(true, Ordering::SeqCst); - cx.update(|cx| { - f.panel - .read(cx) - .current() - .unwrap() - .update(cx, |thread, cx| { let entries = ["pending", "completed"].into_iter().map(|status| { - nocterm_ai::thread::Entry::Tool(serde_json::from_value(serde_json::json!({"toolCallId":status,"title":"Before auth required","status":status})).unwrap()) - }).collect(); - restore_entries(&f, thread, entries, cx); - thread.send("Question".into(), cx); }); - }); - cx.run_until_parked(); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let thread = f.panel.read(cx).current().unwrap(); - assert!(thread.read(cx).session_sign_in_required()); - assert!(!thread.read(cx).session_generating()); - let statuses = thread - .read(cx) - .transcript() - .entries - .iter() - .filter_map(|entry| { - if let nocterm_ai::thread::Entry::Tool(call) = entry { - Some(call.status) - } else { - None - } - }) - .collect::>(); - assert_eq!(statuses, vec![ToolStatus::Failed, ToolStatus::Succeeded]); - thread.update(cx, |thread, cx| { - thread.send("Do not submit while auth-required".into(), cx) - }); - assert_eq!(f.commands.prompts.lock().unwrap().len(), 1); - window.click("authenticate-sign-in", cx); - }) - .unwrap(); - cx.run_until_parked(); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let thread = f.panel.read(cx).current().unwrap(); - assert!(thread.read(cx).session_sign_in_required()); - assert!(!thread.read(cx).session_signing_in()); - assert_eq!(thread.read(cx).status(), "Sign-in failed"); - assert!(thread.read(cx).session().is_some()); - f.commands - .authentication_failure - .store(false, Ordering::SeqCst); - window.click("authenticate-sign-in", cx); - }) - .unwrap(); - cx.run_until_parked(); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let thread = f.panel.read(cx).current().unwrap(); - assert!(!thread.read(cx).session_sign_in_required()); - assert!(thread.read(cx).session().is_some()); - assert!(window.try_find("authenticate-sign-in").is_none()); - }) - .unwrap(); - assert_eq!(f.commands.authentications.load(Ordering::SeqCst), 2); - assert_eq!(f.commands.sessions.load(Ordering::SeqCst), 1); -} - -/// Asserts that terminal sign-in failed with `status` and remains retryable, then retries it. -fn retry_failed_terminal_sign_in(f: &Fixture, status: &str, cx: &mut TestAppContext) { - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let thread = f.panel.read(cx).current().unwrap(); - assert!(thread.read(cx).session_sign_in_required()); - assert!(!thread.read(cx).session_signing_in()); - assert_eq!(thread.read(cx).status(), status); - window.click("authenticate-setup", cx); - }) - .unwrap(); - cx.run_until_parked(); -} - -#[gpui_kit::test] -fn terminal_authentication_uses_host_callback_and_retries_after_cancel_or_failure( - cx: &mut TestAppContext, -) { - let mut f = fixture(cx); - f.commands.auth_required.store(true, Ordering::SeqCst); - let requests = Arc::new(Mutex::new(Vec::new())); - let captured = requests.clone(); - let commands = f.commands.clone(); - let opener: crate::TerminalAuthOpener = Arc::new(move |_, request, _, _| { - let mut requests = captured.lock().unwrap(); - requests.push(request); - let (send, receive) = oneshot::channel(); - match requests.len() { - 1 => {} - 2 => send.send(Err(crate::TerminalAuthError::Failed)).unwrap(), - _ => { - commands.auth_required.store(false, Ordering::SeqCst); - send.send(Ok(())).unwrap(); - } - } - receive - }); - install_fixture( - &mut f, - |f| { - let mut services = fixture_services(f); - services.terminal_auth = Some(opener); - services.usage_source = Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()); - services - }, - cx, - ); - let unrelated = fixture(cx); - f.connector.report_auth_methods(vec![AuthMethod::terminal( - "setup", - "Terminal setup", - nocterm_ai::model::auth::TerminalSetup { - args: vec!["--setup".into(), "$(literal_argument)".into()], - env: Default::default(), - }, - )]); - new_chat(&f, cx); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - window.click("authenticate-setup", cx); - }) - .unwrap(); - cx.run_until_parked(); - retry_failed_terminal_sign_in(&f, "Sign-in cancelled", cx); - retry_failed_terminal_sign_in( - &f, - "Authentication command did not complete successfully.", - cx, - ); - cx.update_window(f.handle, |_, window, cx| { - window.render_frame(cx); - let thread = f.panel.read(cx).current().unwrap(); - assert!(!thread.read(cx).session_sign_in_required()); - assert!(thread.read(cx).session().is_some()); - assert!(window.try_find("authenticate-setup").is_none()); - }) - .unwrap(); - assert_eq!( - f.commands.authentications.load(Ordering::SeqCst), - 0, - "terminal methods must not use the agent authenticate RPC" - ); - assert_eq!(unrelated.connector.connects.load(Ordering::SeqCst), 0); - assert_eq!(unrelated.commands.authentications.load(Ordering::SeqCst), 0); - let requests = requests.lock().unwrap(); - assert_eq!(requests.len(), 3); - assert_eq!(requests[0].program, "npx"); - assert!( - requests[0] - .args - .ends_with(&["--setup".into(), "$(literal_argument)".into()]) - ); -} - #[gpui_kit::test] fn stopping_and_failure_finalize_only_unfinished_tool_calls(cx: &mut TestAppContext) { let f = fixture(cx); diff --git a/crates/agent/src/panel/tests/mod.rs b/crates/agent/src/panel/tests/mod.rs index 493c3951..24d20dc2 100644 --- a/crates/agent/src/panel/tests/mod.rs +++ b/crates/agent/src/panel/tests/mod.rs @@ -34,14 +34,7 @@ use std::{ }, }; -fn activate_thread( - thread: &Entity, - installed: &crate::Installed, - cx: &mut App, -) { - installed - .runtime() - .register_document(crate::thread::client(thread), cx); +fn activate_thread(thread: &Entity, cx: &mut App) { thread.update(cx, |thread, cx| thread.request_activation(cx)); } @@ -60,242 +53,15 @@ fn shutdown_runtime(installed: &crate::Installed, cx: &mut TestAppContext) { mod builders; mod config_responses; mod process_commands; +mod scripted_agent; +use scripted_agent::{Bridge, Commands, Connector}; -#[derive(Default)] -struct Commands { - sessions: AtomicU64, - new_dirs: Mutex>, - restore_dirs: Mutex>, - session_gate: Mutex>>, - close_gate: Mutex>>, - closed: Mutex>, - shutdown_failure: AtomicBool, - auth_required: AtomicBool, - authentications: AtomicUsize, - authentication_failure: AtomicBool, - shutdowns: AtomicUsize, - cancels: AtomicUsize, - prompts: Mutex>, - pending: Mutex>>, - /// Each reopened session, and whether it was forked. - restores: Mutex>, - restore_errors: Mutex>, - /// The MCP servers each new session was given. - servers: Mutex>>, - /// The configuration options every session reports. - config: config_responses::ConfigResponses, -} -impl AgentCommands for Commands { - fn new_session( - &self, - request: SessionOpen, - ) -> BoxFuture<'static, Result> { - self.new_dirs - .lock() - .unwrap() - .push(request.working_directory.clone()); - self.servers.lock().unwrap().push(request.endpoints); - let gate = self.session_gate.lock().unwrap().take(); - let id = self.sessions.fetch_add(1, Ordering::SeqCst); - let auth_required = self.auth_required.load(Ordering::SeqCst); - let config = self.config.options(); - let modes = self.config.modes(); - async move { - if let Some(gate) = gate { - let _ = gate.await; - } - if auth_required { - Err(AgentError::AuthRequired("Sign in to continue".into())) - } else { - Ok(opened(format!("s{id}"), config, modes)) - } - } - .boxed() - } - fn restore_session( - &self, - request: nocterm_ai::model::session::RestoreSession, - ) -> BoxFuture<'static, Result> { - self.restore_dirs - .lock() - .unwrap() - .push(request.working_directory.clone()); - self.restores - .lock() - .unwrap() - .push((request.session.clone(), request.fork)); - if let Some(error) = self.restore_errors.lock().unwrap().pop_front() { - return async move { Err(error) }.boxed(); - } - let session = if request.fork { - SessionId::new(format!("fork-of-{}", request.session)) - } else { - request.session - }; - let config = self.config.options(); - let modes = self.config.modes(); - async move { Ok(opened(session, config, modes)) }.boxed() - } - fn prompt(&self, r: PromptRequest) -> BoxFuture<'static, Result> { - self.prompts.lock().unwrap().push(r); - if self.auth_required.load(Ordering::SeqCst) { - return async { Err(AgentError::AuthRequired("Sign in to continue".into())) }.boxed(); - } - let (tx, rx) = oneshot::channel(); - *self.pending.lock().unwrap() = Some(tx); - async move { rx.await.map_err(|_| AgentError::Io("closed".into())) }.boxed() - } - fn cancel(&self, _: SessionId) { - self.cancels.fetch_add(1, Ordering::SeqCst); - } - fn set_mode(&self, _: SetMode) -> BoxFuture<'static, Result<(), AgentError>> { - async { Ok(()) }.boxed() - } - fn set_config_option( - &self, - request: SetConfigOption, - ) -> BoxFuture<'static, Result, AgentError>> { - let options = self.config.choose(request); - async move { Ok(options) }.boxed() - } - fn authenticate(&self, _: AuthMethodId) -> BoxFuture<'static, Result<(), AgentError>> { - self.authentications.fetch_add(1, Ordering::SeqCst); - let failed = self.authentication_failure.load(Ordering::SeqCst); - if !failed { - self.auth_required.store(false, Ordering::SeqCst); - } - async move { - if failed { - Err(AgentError::Io("Sign-in failed".into())) - } else { - Ok(()) - } - } - .boxed() - } - fn close_session( - &self, - session: SessionId, - ) -> BoxFuture<'static, Result> { - self.closed.lock().unwrap().push(session); - let gate = self.close_gate.lock().unwrap().take(); - async move { - if let Some(gate) = gate { - let _ = gate.await; - } - Ok(nocterm_ai::CloseSessionOutcome::Closed) - } - .boxed() - } - fn shutdown_gracefully(&self) -> BoxFuture<'static, Result<(), AgentError>> { - self.shutdown(); - let failed = self.shutdown_failure.load(Ordering::SeqCst); - async move { - if failed { - Err(AgentError::Io("Process tree still alive".into())) - } else { - Ok(()) - } - } - .boxed() - } - fn shutdown(&self) { - self.shutdowns.fetch_add(1, Ordering::SeqCst); - } -} -struct Connector { - distinct_process_handles: AtomicBool, - commands: Arc, - events: async_channel::Receiver, - connects: AtomicUsize, - roots: Mutex< - Vec<( - std::path::PathBuf, - Option, - )>, - >, - event_senders: Mutex>>, - info: Mutex, -} -impl Connector { - fn use_distinct_process_handles(&self) { - self.distinct_process_handles.store(true, Ordering::SeqCst); - } - fn report_auth_methods(&self, methods: Vec) { - self.info.lock().unwrap().auth_methods = methods; - } - fn support_images(&self) { - self.info.lock().unwrap().capabilities.image_prompts = true; - } -} -impl AgentConnector for Connector { - fn connect( - &self, - request: ConnectRequest, - ) -> BoxFuture<'static, Result> { - self.roots - .lock() - .unwrap() - .push((request.working_directory, request.sandbox)); - self.connects.fetch_add(1, Ordering::SeqCst); - let commands: Arc = - if self.distinct_process_handles.load(Ordering::SeqCst) { - Arc::new(process_commands::ProcessCommands::new( - self.commands.clone(), - )) - } else { - self.commands.clone() - }; - let info = self.info.lock().unwrap().clone(); - let (sender, events) = async_channel::bounded(256); - self.event_senders.lock().unwrap().push(sender); - async move { - Ok(AgentConnection { - commands, - events, - info, - }) - } - .boxed() - } -} -struct Bridge { - next: AtomicU64, - revoked: Arc>>, - stops: AtomicUsize, - calls: async_channel::Receiver, - rejections: async_channel::Receiver, -} -impl ToolBridge for Bridge { - fn register( - &self, - ) -> Result { - let id = self.next.fetch_add(1, Ordering::SeqCst); - let revoked = self.revoked.clone(); - Ok(BridgeRegistration::new( - id, - nocterm_ai::BridgeLaunch { - program: "/fake/nocterm".into(), - args: vec!["agent-bridge".into()], - env: vec![("NOCTERM_BRIDGE_TOKEN".into(), "test-token".into())], - }, - Arc::new(move |id| revoked.lock().unwrap().push(id)), - )) - } - fn calls(&self) -> async_channel::Receiver { - self.calls.clone() - } - fn rejections(&self) -> async_channel::Receiver { - self.rejections.clone() - } - fn stop(&self) { - self.stops.fetch_add(1, Ordering::SeqCst); - } -} mod terminal_fixture; use terminal_fixture::{Access, FakeTerminal, SignIn}; struct HostEnvironment { + jobs: Arc, + transfers: Arc, directory: tempfile::TempDir, metadata: Arc, } @@ -307,16 +73,23 @@ impl HostEnvironment { &self, workspace: gpui_kit::WeakEntity, installed: crate::Installed, + source: nocterm_workspace::ProjectSurfaceReceipt, ) -> crate::chat_host::ChatHost { - crate::chat_host::ChatHost::new(workspace, installed) + crate::chat_host::ChatHost::new(workspace, installed, source).unwrap() } /// What chats hosted by `workspace` receive. fn deps( &self, workspace: gpui_kit::WeakEntity, installed: crate::Installed, + cx: &App, ) -> crate::chat_deps::ChatDeps { - self.host(workspace, installed).deps().clone() + let source = workspace + .upgrade() + .unwrap() + .read(cx) + .general_surface_source(cx); + self.host(workspace, installed, source).deps().clone() } } @@ -344,23 +117,21 @@ struct Fixture { repository: Arc, } fn fixture(cx: &mut TestAppContext) -> Fixture { - fixture_with_width(cx, 26., usage_source(), workspace_resolver(), image_deps()) + fixture_with_width(cx, 26., workspace_resolver(), image_deps()) } fn workspace_resolver() -> Arc { Arc::new(nocterm_ai::testing::ScriptedWorkdirResolver::accept_requests()) } -fn usage_source() -> Arc { - Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()) -} #[expect(clippy::too_many_lines, reason = "predates the limit")] fn fixture_with_width( cx: &mut TestAppContext, width: f32, - usage_source: Arc, workdir_resolver: Arc, images: crate::ImageDeps, ) -> Fixture { let directory = HostEnvironment { + jobs: Arc::new(nocterm_session::testing::ScriptedCommandJobsFactory::default()), + transfers: Arc::default(), directory: tempfile::tempdir().unwrap(), metadata: Arc::new(nocterm_ai::testing::ScriptedChatMetadata::default()), }; @@ -373,6 +144,7 @@ fn fixture_with_width( let bridge = Arc::new(Bridge { next: AtomicU64::new(1), revoked: Default::default(), + offers: Default::default(), stops: AtomicUsize::new(0), calls, rejections, @@ -415,6 +187,8 @@ fn fixture_with_width( let layout = nocterm_ui::LayoutMemory::init(None, &shutdown, cx); let (installed, registration) = crate::init( AgentServices { + command_jobs: directory.jobs.clone(), + transfers: directory.transfers.clone(), catalog: nocterm_agent_providers::catalog(), metadata: directory.metadata.clone(), terminal_auth: None, @@ -427,7 +201,6 @@ fn fixture_with_width( repository.clone(), Ok(Default::default()), ), - usage_source, workdir_resolver, workdir: directory.path().join("work"), }, @@ -435,46 +208,17 @@ fn fixture_with_width( layout.clone(), &ui, &shutdown, + None, cx, ); registration.unwrap(); - let mut panel = None; installed .runtime() .inject_events(connector.events.clone(), cx); - let mut terminal = None; - let (handle, workspace) = - gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { - cx.new(|cx| { - let mut workspace = Workspace::new( - nocterm_workspace::Deps::new(layout.clone()), - window, - cx, - ); - let item = cx.new(|cx| FakeTerminal::new(access.clone(), cx)); - terminal = Some(item.entity_id()); - workspace.add_item(item, window, cx); - let owner = cx.entity().downgrade(); - let view = cx.new(|cx| { - AgentPanel::new(directory.host(owner, installed.clone()), window, cx) - }); - workspace.set_right_panel(view.clone(), window, cx); - workspace.set_right_panel_available(true, window, cx); - workspace.toggle_right_panel(window, cx); - panel = Some(view); - workspace - }) - }) - .unwrap(); + let (handle, workspace, panel, terminal) = + open_host(&directory, &installed, &layout, &access, cx); ( - handle, - workspace, - panel.unwrap(), - terminal.unwrap(), - ui, - shutdown, - layout, - installed, + handle, workspace, panel, terminal, ui, shutdown, layout, installed, ) }); Fixture { @@ -501,6 +245,8 @@ fn fixture_with_width( } fn fixture_services(f: &Fixture) -> AgentServices { AgentServices { + command_jobs: f._directory.jobs.clone(), + transfers: f._directory.transfers.clone(), catalog: nocterm_agent_providers::catalog(), metadata: f._directory.metadata.clone(), connector: f.connector.clone(), @@ -510,7 +256,6 @@ fn fixture_services(f: &Fixture) -> AgentServices { f.repository.clone(), Ok(f.repository.state()), ), - usage_source: Arc::new(nocterm_ai::testing::ScriptedUsageSource::default()), workdir_resolver: workspace_resolver(), workdir: f._directory.path().join("work"), terminal_auth: None, @@ -518,6 +263,94 @@ fn fixture_services(f: &Fixture) -> AgentServices { shared_dirs: Vec::new(), } } +/// Opens a window whose workspace holds a terminal and the registered agent panel, shown. +fn open_host( + directory: &HostEnvironment, + installed: &crate::Installed, + layout: &nocterm_ui::LayoutHandle, + access: &Rc, + cx: &mut App, +) -> ( + AnyWindowHandle, + Entity, + Entity, + gpui_kit::EntityId, +) { + let mut panel = None; + let mut terminal = None; + let (handle, workspace) = gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let mut workspace = Workspace::new(deps(layout.clone()), window, cx); + let item = cx.new(|cx| FakeTerminal::new(access.clone(), cx)); + terminal = Some(item.entity_id()); + workspace.add_item(item, window, cx); + let owner = cx.entity().downgrade(); + let source = workspace.general_surface_source(cx); + let view = cx.new(|cx| { + AgentPanel::new(directory.host(owner, installed.clone(), source), window, cx) + }); + host_panel(&mut workspace, &view, window, cx); + workspace.set_panel_available(&crate::plugin::CHATS.id, true, window, cx); + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx); + panel = Some(view); + workspace + }) + }) + .unwrap(); + (handle, workspace, panel.unwrap(), terminal.unwrap()) +} +/// Dependencies whose plugin catalog lists the agent, over `layout`. +/// Another window of the fixture's installation, its chats panel open. +fn open_window_panel( + f: &Fixture, + cx: &mut TestAppContext, +) -> (AnyWindowHandle, Entity) { + let mut panel = None; + let (handle, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let mut workspace = Workspace::new(deps(f.installation.layout.clone()), window, cx); + let owner = cx.entity().downgrade(); + let source = workspace.general_surface_source(cx); + let view = cx.new(|cx| { + AgentPanel::new( + f._directory + .host(owner, f.installation.installed.clone(), source), + window, + cx, + ) + }); + host_panel(&mut workspace, &view, window, cx); + workspace.set_panel_available(&crate::plugin::CHATS.id, true, window, cx); + workspace.toggle_panel(&crate::plugin::CHATS.id, window, cx); + panel = Some(view); + workspace + }) + }) + .unwrap() + }); + (handle, panel.unwrap()) +} +fn deps(layout: nocterm_ui::LayoutHandle) -> nocterm_workspace::Deps { + nocterm_workspace::test_support::deps_over(layout, &[], &[&crate::plugin::PLUGIN]) +} +/// Registers `view` as the agent panel the way installation does, before the host offers it. +fn host_panel( + workspace: &mut Workspace, + view: &Entity, + window: &mut Window, + cx: &mut Context, +) { + workspace + .register_panel( + nocterm_workspace::PanelRegistration::with_entity(&crate::plugin::CHATS, view.clone()) + .expandable(), + window, + cx, + ) + .unwrap(); + workspace.set_panel_available(&crate::plugin::CHATS.id, false, window, cx); +} /// Simulates a fresh host through public shutdown, installation and view creation. fn install_fixture( f: &mut Fixture, @@ -533,6 +366,7 @@ fn install_fixture( f.installation.layout.clone(), &f.installation.ready, &f.installation.shutdown, + None, cx, ); registration.unwrap(); @@ -542,23 +376,23 @@ fn install_fixture( runtime.inject_events(f.connector.events.clone(), cx) }; }); - let panel = cx - .update_window(f.handle, |_, window, cx| { - let panel = cx.new(|cx| { - AgentPanel::new( - f._directory - .host(f.workspace.downgrade(), f.installation.installed.clone()), - window, - cx, - ) - }); - f.workspace.update(cx, |workspace, cx| { - workspace.set_right_panel(panel.clone(), window, cx) - }); - panel - }) - .unwrap(); + let (handle, workspace, panel, terminal) = cx.update(|cx| { + let host = open_host( + &f._directory, + &f.installation.installed, + &f.installation.layout, + &f.access, + cx, + ); + let old = f.handle; + cx.update_window(old, |_, window, _| window.remove_window()) + .unwrap(); + host + }); + f.handle = handle; + f.workspace = workspace; f.panel = panel; + f.terminal = terminal; } /// Supplies bounded prepared records to the real take-once catalog owner. fn reinstall_history( @@ -590,7 +424,9 @@ fn lazy_start(f: &Fixture, cx: &mut TestAppContext) { cx.run_until_parked(); } mod panel_actions; -use panel_actions::{new_chat, open_queue, replace_composer_value, replace_textbox, show_history}; +use panel_actions::{ + new_chat, open_queue, replace_composer_value, replace_textbox, show_history, show_history_in, +}; fn complete_active(f: &Fixture, cx: &mut TestAppContext) { f.commands .pending @@ -653,8 +489,11 @@ impl nocterm_workspace::ConnectionDirectory for Directory { } mod approvals; +mod authentication; mod bridge_outcomes; +mod chat_servers; mod composer; +mod context_rosters; mod durable_config; mod hermes; mod history; @@ -662,14 +501,17 @@ mod invalidation; mod labels; mod lifecycle; mod navigation; +mod other_windows; mod persistence; mod provider_fixtures; mod provider_permissions; mod queue_remove; mod reconnect; +mod registration; mod resources; mod restoration; mod routing; +mod seat_binding; mod servers; mod tool_corpus; mod tool_input; @@ -684,6 +526,7 @@ mod flow; mod queue_edit; mod queue_layout; +mod queue_timing; mod commands; @@ -735,7 +578,7 @@ fn restore_entries( chat.entries = entries; chat.times.clear(); f.repository.seed_chat(&chat).unwrap(); - thread.restore_content(chat); + thread.restore_content(chat, cx).unwrap(); thread.preview_config(cx); for row in 0..thread.history_entries().len() { thread.mark_dirty(row); @@ -750,7 +593,7 @@ fn restore_pending_history( let mut chat = restored_projection(thread, cx); chat.pending_history = true; f.repository.seed_chat(&chat).unwrap(); - thread.restore_content(chat); + thread.restore_content(chat, cx).unwrap(); thread.preview_config(cx); cx.notify(); } diff --git a/crates/agent/src/panel/tests/navigation.rs b/crates/agent/src/panel/tests/navigation.rs index 70adbf8d..d7dd12f6 100644 --- a/crates/agent/src/panel/tests/navigation.rs +++ b/crates/agent/src/panel/tests/navigation.rs @@ -5,6 +5,24 @@ use nocterm_ai::model::session::{SessionNotice, SessionUpdate}; use nocterm_ai::model::tools::ToolRecord; use nocterm_ai::thread::Entry; +#[gpui_kit::test] +fn borrowed_local_rows_keep_their_copy_and_fork_actions(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "Local question", "Local answer", cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find(("fork-message", 0usize)).is_some()); + window.hover(("agent-entry", 0usize), cx); + window.click(("copy-message", 0usize), cx); + assert_eq!( + cx.read_from_clipboard().unwrap().text().as_deref(), + Some("Local question") + ); + }) + .unwrap(); +} + fn transcript(f: &Fixture, cx: &mut TestAppContext) -> Entity { new_chat(f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -317,7 +335,7 @@ fn streaming_and_new_messages_preserve_reader_anchor_and_new_chat_clears_navigat #[gpui_kit::test] fn navigation_pill_fits_narrow_chat_in_both_themes_and_keeps_rows_virtual(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 14., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 14., workspace_resolver(), image_deps()); let thread = transcript(&f, cx); thread.update(cx, |thread, cx| { let mut entries = thread.history_entries().to_vec(); @@ -524,6 +542,41 @@ fn streaming_keeps_reader_scroll_until_jump_to_latest(cx: &mut TestAppContext) { complete_active(&f, cx); } +#[gpui_kit::test] +fn a_code_block_streamed_in_chunks_copies_its_whole_code(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + cx.update(|cx| cx.set_reduce_motion(true)); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| thread.send("Show debounce".into(), cx)); + cx.run_until_parked(); + let session = cx.update(|cx| thread.read(cx).session().clone().unwrap()); + let code = "export function debounce() {\n return null;\n}"; + for chunk in [ + "Here it is:\n\n```typescript\nexport function debounce() {\n", + " return null;\n}\n```\n", + ] { + f.events + .try_send(AgentEvent::Session(SessionNotice::new( + session.clone(), + SessionUpdate::AgentMessageChunk(Content::text(chunk)), + ))) + .unwrap(); + cx.run_until_parked(); + for _ in 0..3 { + // Plain draws, which reuse the cached transcript unless it was told to redraw. + cx.update_window(f.handle, |_, window, cx| window.draw(cx).clear(cx)) + .unwrap(); + cx.run_until_parked(); + } + } + cx.update_window(f.handle, |_, window, cx| { + window.click("copy", cx); + assert_eq!(cx.read_from_clipboard().unwrap().text().unwrap(), code); + }) + .unwrap(); +} + #[gpui_kit::test] fn code_blocks_in_replies_copy_their_code(cx: &mut TestAppContext) { let f = fixture(cx); diff --git a/crates/agent/src/panel/tests/other_windows.rs b/crates/agent/src/panel/tests/other_windows.rs new file mode 100644 index 00000000..b4d1e24f --- /dev/null +++ b/crates/agent/src/panel/tests/other_windows.rs @@ -0,0 +1,137 @@ +//! Two windows of one installation: each lists every chat, a chat belongs to one of them. +use super::*; + +/// Renders `handle` and runs `check` on it. +fn in_window( + handle: AnyWindowHandle, + cx: &mut TestAppContext, + check: impl FnOnce(&mut Window, &mut App) -> R, +) -> R { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + check(window, cx) + }) + .unwrap() +} + +#[gpui_kit::test] +fn a_second_window_lists_the_first_windows_chats_and_opens_an_idle_one_here( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + new_chat(&f, cx); + exchange(&f, "Why is the disk full?", "The logs grew.", cx); + let first = cx.update(|cx| f.panel.read(cx).current().unwrap()); + let chat = cx.update(|cx| first.read(cx).chat_id().to_owned()); + // The first window moves on, so the chat neither works nor shows there. + new_chat(&f, cx); + + let (second, panel) = open_window_panel(&f, cx); + show_history_in(second, cx); + let row = first.entity_id().as_u64(); + in_window(second, cx, |window, cx| { + assert_eq!( + window.find(("history-elsewhere", row)).label(), + Some("Open in another window: opens here") + ); + assert!(window.try_find(("delete-thread", row)).is_none()); + assert_eq!(panel.read(cx).chat_documents().len(), 0, "listed, not held"); + window.click(("history-thread", row), cx); + }); + cx.run_until_parked(); + + let moved = cx.update(|cx| panel.read(cx).current().unwrap()); + cx.update(|cx| { + let thread = moved.read(cx); + assert_eq!(thread.chat_id(), chat); + assert_eq!(thread.transcript().entries.len(), 2, "its whole transcript"); + assert!( + f.panel + .read(cx) + .chat_documents() + .all(|thread| thread.read(cx).chat_id() != chat), + "the first window no longer holds it" + ); + }); + show_history(&f, cx); + in_window(f.handle, cx, |window, _| { + assert!( + window + .try_find(("history-elsewhere", moved.entity_id().as_u64())) + .is_some(), + "the first window lists it where it went" + ); + }); +} + +#[gpui_kit::test] +fn a_chat_working_in_another_window_opens_in_that_window(cx: &mut TestAppContext) { + let f = fixture(cx); + new_chat(&f, cx); + let working = cx.update(|cx| f.panel.read(cx).current().unwrap()); + cx.update(|cx| working.update(cx, |thread, cx| thread.send("Rotate the logs".into(), cx))); + cx.run_until_parked(); + new_chat(&f, cx); + + let (second, panel) = open_window_panel(&f, cx); + show_history_in(second, cx); + let row = working.entity_id().as_u64(); + in_window(second, cx, |window, cx| { + assert_eq!( + window.find(("history-elsewhere", row)).label(), + Some("Working in another window: opens there") + ); + window.click(("history-thread", row), cx); + }); + cx.run_until_parked(); + + cx.update(|cx| { + assert_eq!( + panel.read(cx).chat_documents().len(), + 0, + "it stays where it works" + ); + assert_eq!( + f.panel.read(cx).current().map(|thread| thread.entity_id()), + Some(working.entity_id()), + "its window shows it" + ); + }); + complete_active(&f, cx); +} + +#[gpui_kit::test] +fn a_saved_chat_the_first_window_has_not_opened_moves_with_its_transcript(cx: &mut TestAppContext) { + let f = fixture(cx); + cx.run_until_parked(); + let identity = nocterm_ai::metadata::ChatMetadataSource::new_identity(&*f._directory.metadata); + let mut chat = + nocterm_ai::history::SavedChat::for_identity("codex".into(), identity.id, identity.created); + chat.entries + .push(nocterm_ai::thread::Entry::Agent("Nginx is up.".into())); + let archived = super::archive::archived(&f, &chat, cx); + assert!(cx.update(|cx| archived.read(cx).is_archived())); + + let (second, panel) = open_window_panel(&f, cx); + show_history_in(second, cx); + in_window(second, cx, |window, cx| { + window.click(("history-thread", archived.entity_id().as_u64()), cx); + }); + cx.run_until_parked(); + + let moved = cx.update(|cx| panel.read(cx).current().unwrap()); + cx.update(|cx| { + assert_eq!(moved.read(cx).chat_id(), chat.header.id); + assert_eq!( + moved.read(cx).transcript().entries.len(), + 1, + "loaded where it went" + ); + assert!( + f.panel + .read(cx) + .chat_documents() + .all(|thread| thread.entity_id() != archived.entity_id()) + ); + }); +} diff --git a/crates/agent/src/panel/tests/panel_actions.rs b/crates/agent/src/panel/tests/panel_actions.rs index 4b0fd83a..7d332afe 100644 --- a/crates/agent/src/panel/tests/panel_actions.rs +++ b/crates/agent/src/panel/tests/panel_actions.rs @@ -16,26 +16,28 @@ pub(super) fn replace_composer_value( } } pub(super) fn new_chat(fixture: &Fixture, cx: &mut TestAppContext) { + new_agent_chat(fixture, "codex", cx); +} +pub(super) fn new_agent_chat(fixture: &Fixture, agent: &str, cx: &mut TestAppContext) { cx.update_window(fixture.handle, |_, window, cx| { let focus = fixture.panel.read(cx).composer_focus(cx); window.focus(&focus, cx); window.render_frame(cx); window.click("agent-new-thread", cx); window.render_frame(cx); - window.click("new-agent-codex", cx); + window.click(format!("new-agent-{agent}"), cx); window.render_frame(cx); let thread = fixture.panel.read(cx).current().unwrap(); - { - let runtime = fixture.installation.installed.runtime(); - runtime.register_document(crate::thread::client(&thread), cx) - }; thread.update(cx, |thread, cx| thread.request_activation(cx)); }) .unwrap(); cx.run_until_parked(); } pub(super) fn show_history(f: &Fixture, cx: &mut TestAppContext) { - cx.update_window(f.handle, |_, window, cx| { + show_history_in(f.handle, cx); +} +pub(super) fn show_history_in(handle: AnyWindowHandle, cx: &mut TestAppContext) { + cx.update_window(handle, |_, window, cx| { window.render_frame(cx); if window.try_find("agent-history-column").is_none() { window.click("agent-history", cx); diff --git a/crates/agent/src/panel/tests/persistence.rs b/crates/agent/src/panel/tests/persistence.rs index 9bd085ae..545859b3 100644 --- a/crates/agent/src/panel/tests/persistence.rs +++ b/crates/agent/src/panel/tests/persistence.rs @@ -56,10 +56,13 @@ fn shutdown_preserves_a_snapshot_extracted_by_the_writer_after_document_release( 1, "quit lost the writer-owned snapshot after its document was released" ); - assert_eq!(chats[0].name.as_deref(), Some("pending extracted snapshot")); + assert_eq!( + chats[0].header.name.as_deref(), + Some("pending extracted snapshot") + ); cx.run_until_parked(); assert_eq!( - f.repository.chats()[0].name.as_deref(), + f.repository.chats()[0].header.name.as_deref(), Some("pending extracted snapshot") ); } @@ -121,7 +124,7 @@ fn shutdown_saves_the_replacement_when_the_old_document_is_retained_elsewhere( let chats = f.repository.chats(); assert_eq!(chats.len(), 1); assert_eq!( - chats[0].name.as_deref(), + chats[0].header.name.as_deref(), Some("replacement wins"), "an externally retained old document overwrote its replacement" ); @@ -156,7 +159,7 @@ fn a_late_save_from_a_retained_old_document_cannot_overwrite_the_replacement( }); cx.run_until_parked(); assert_eq!( - f.repository.chats()[0].name.as_deref(), + f.repository.chats()[0].header.name.as_deref(), Some("replacement owner") ); // An outstanding callback can retain the old Entity and eventually attempt to save it. @@ -166,13 +169,13 @@ fn a_late_save_from_a_retained_old_document_cannot_overwrite_the_replacement( }); cx.run_until_parked(); assert_eq!( - f.repository.chats()[0].name.as_deref(), + f.repository.chats()[0].header.name.as_deref(), Some("replacement owner"), "a stale owner saved over the replacement outside shutdown" ); shutdown_runtime(&f.installation.installed, cx); assert_eq!( - f.repository.chats()[0].name.as_deref(), + f.repository.chats()[0].header.name.as_deref(), Some("replacement owner") ); } @@ -198,15 +201,18 @@ fn shutdown_prefers_the_latest_queued_snapshot_over_the_extracted_older_snapshot shutdown_runtime(&f.installation.installed, cx); let chats = f.repository.chats(); assert_eq!(chats.len(), 1); - assert_eq!(chats[0].name.as_deref(), Some("latest queued snapshot")); + assert_eq!( + chats[0].header.name.as_deref(), + Some("latest queued snapshot") + ); cx.run_until_parked(); assert_eq!( - f.repository.chats()[0].name.as_deref(), + f.repository.chats()[0].header.name.as_deref(), Some("latest queued snapshot") ); } -fn save_count(f: &Fixture, id: &str) -> usize { +pub(super) fn save_count(f: &Fixture, id: &str) -> usize { f.repository.operations().iter().filter(|operation|matches!(operation,nocterm_ai::testing::StorageOperation::Save(saved) if saved==id)).count() } #[gpui_kit::test] @@ -224,7 +230,7 @@ fn loaded_browse_noop_actions_detach_and_quit_preserve_the_original_v1_bytes( saved.times = vec![39]; saved.pending_history = true; let thread = super::archive::archived(&f, &saved, cx); - let bytes = f.repository.encoded(&saved.id).unwrap(); + let bytes = f.repository.encoded(&saved.header.id).unwrap(); f.panel .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); cx.run_until_parked(); @@ -232,14 +238,14 @@ fn loaded_browse_noop_actions_detach_and_quit_preserve_the_original_v1_bytes( thread.rename(None, cx); thread.set_pinned(false, cx); thread.set_draft(String::new(), cx); - assert!(thread.shared_snapshot(cx).is_some()); - assert!(thread.flush_snapshot(cx).is_none()); + assert!(thread.shared_snapshot(cx).unwrap().is_some()); + assert!(thread.flush_snapshot(cx).unwrap().is_none()); thread.release_resources(cx); }); cx.run_until_parked(); shutdown_runtime(&f.installation.installed, cx); - assert_eq!(save_count(&f, &saved.id), 0); - assert_eq!(f.repository.encoded(&saved.id).unwrap(), bytes); + assert_eq!(save_count(&f, &saved.header.id), 0); + assert_eq!(f.repository.encoded(&saved.header.id).unwrap(), bytes); cx.update(|cx| assert_eq!(thread.read(cx).updated_at(), 40)); } #[gpui_kit::test] @@ -254,7 +260,7 @@ fn equal_warm_resume_does_not_rewrite_imported_session_metadata(cx: &mut TestApp saved.session_id = Some("resumed".into()); saved.workdir = Some("/tmp".into()); let thread = super::archive::archived(&f, &saved, cx); - let bytes = f.repository.encoded(&saved.id).unwrap(); + let bytes = f.repository.encoded(&saved.header.id).unwrap(); f.panel .update(cx, |panel, cx| panel.open_thread(thread.entity_id(), cx)); cx.run_until_parked(); @@ -267,8 +273,8 @@ fn equal_warm_resume_does_not_rewrite_imported_session_metadata(cx: &mut TestApp thread.update(cx, |thread, cx| thread.release_resources(cx)); cx.run_until_parked(); shutdown_runtime(&f.installation.installed, cx); - assert_eq!(save_count(&f, &saved.id), 0); - assert_eq!(f.repository.encoded(&saved.id).unwrap(), bytes); + assert_eq!(save_count(&f, &saved.header.id), 0); + assert_eq!(f.repository.encoded(&saved.header.id).unwrap(), bytes); } #[gpui_kit::test] fn an_old_disk_failure_before_new_draft_admission_does_not_pause_the_new_desire( @@ -314,14 +320,14 @@ fn captures_before_the_draft_timer_keep_one_revision_and_one_immutable_intent( thread.set_draft("before 300 ms".into(), cx) }); let client = crate::thread::client(&thread); - let first = cx.update(|cx| client.capture(cx).unwrap()); - let second = cx.update(|cx| client.capture(cx).unwrap()); + let first = cx.update(|cx| client.capture(cx).unwrap().unwrap()); + let second = cx.update(|cx| client.capture(cx).unwrap().unwrap()); assert_eq!(first.1, second.1); assert!(Arc::ptr_eq(&first.0, &second.0)); shutdown_runtime(&f.installation.installed, cx); assert_eq!( f.repository - .transcript(&first.0.id) + .transcript(&first.0.header.id) .unwrap() .draft .as_deref(), @@ -341,8 +347,8 @@ fn shared_snapshots_reuse_queued_payload_and_cache_exact_size(cx: &mut TestAppCo .accept_composer_images(vec![prompt_image()], cx) .unwrap(); thread.send("queued\n\"\\界".into(), cx); - let first = thread.shared_snapshot(cx).unwrap(); - let second = thread.shared_snapshot(cx).unwrap(); + let first = thread.shared_snapshot(cx).unwrap().unwrap(); + let second = thread.shared_snapshot(cx).unwrap().unwrap(); assert!(Arc::ptr_eq(&first.queue[0], &second.queue[0])); assert!(Arc::ptr_eq( &first.queue[0], diff --git a/crates/agent/src/panel/tests/provider_fixtures.rs b/crates/agent/src/panel/tests/provider_fixtures.rs index 98eba400..54e22546 100644 --- a/crates/agent/src/panel/tests/provider_fixtures.rs +++ b/crates/agent/src/panel/tests/provider_fixtures.rs @@ -3,23 +3,8 @@ use super::*; use nocterm_ai::TerminalCall; use nocterm_ai::model::ids::SessionId; use nocterm_ai::model::session::{SessionNotice, SessionUpdate}; -use nocterm_session::{ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec}; use serde_json::Value; - -#[derive(Default)] -pub(super) struct Program { - pub(super) request: Mutex>, - pub(super) sink: Mutex>, -} - -impl HostExec for Program { - fn exec(&self, request: ExecRequest) -> ExecFuture { - *self.request.lock().unwrap() = Some(request); - let (sink, output) = ExecOutput::channel(); - *self.sink.lock().unwrap() = Some(sink); - async move { Ok(output) }.boxed() - } -} +pub(super) type Program = nocterm_session::testing::FakeExec; pub(super) fn bridge(f: &Fixture, call: TerminalCall, cx: &mut TestAppContext) -> Value { let registration_id = cx.update(|cx| { diff --git a/crates/agent/src/panel/tests/provider_permissions.rs b/crates/agent/src/panel/tests/provider_permissions.rs index 0a5eca6f..74f08f18 100644 --- a/crates/agent/src/panel/tests/provider_permissions.rs +++ b/crates/agent/src/panel/tests/provider_permissions.rs @@ -138,7 +138,7 @@ fn changes_handle_pending_requests_in_every_chat_and_ask_again_for_future_reques fn no_one_time_choice_stays_visible_with_an_explanation_and_can_be_cancelled( cx: &mut TestAppContext, ) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); set_policy(&f, cx, ApprovalPolicy::Allow); new_chat(&f, cx); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); diff --git a/crates/agent/src/panel/tests/queue_edit.rs b/crates/agent/src/panel/tests/queue_edit.rs index 23244490..cf3651ef 100644 --- a/crates/agent/src/panel/tests/queue_edit.rs +++ b/crates/agent/src/panel/tests/queue_edit.rs @@ -190,7 +190,8 @@ fn mixed_unavailable_local_and_remote_queue_survives_save_and_edit(cx: &mut Test }); cx.update_window(f.handle, |_, window, cx| { window.render_frame(cx); - let labels = super::super::attachments::AttachmentLabels::new(&f.workspace.downgrade(), cx); + let labels = + super::super::attachments::AttachmentLabels::new(thread.read(cx).workspace_seat(), cx); assert!( labels .label(&thread.read(cx).composer().queue()[0].attachments()[0]) diff --git a/crates/agent/src/panel/tests/queue_layout.rs b/crates/agent/src/panel/tests/queue_layout.rs index 65e072bc..90404ae4 100644 --- a/crates/agent/src/panel/tests/queue_layout.rs +++ b/crates/agent/src/panel/tests/queue_layout.rs @@ -1,7 +1,7 @@ use super::*; use gpui_kit::px; -fn settle(f: &Fixture, cx: &mut TestAppContext) { +pub(super) fn settle(f: &Fixture, cx: &mut TestAppContext) { for _ in 0..3 { cx.update_window(f.handle, |_, window, cx| window.render_frame(cx)) .unwrap(); @@ -9,9 +9,60 @@ fn settle(f: &Fixture, cx: &mut TestAppContext) { } } +fn assert_first_open_frame(reduced: bool, cx: &mut TestAppContext) { + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); + new_chat(&f, cx); + cx.update(|cx| cx.set_reduce_motion(reduced)); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| { + thread.clear_composer_context(cx); + thread.send("active".into(), cx); + thread.send("short queued message".into(), cx); + }); + settle(&f, cx); + let viewport = cx + .update_window(f.handle, |_, window, cx| { + let height = window.find("agent-transcript-area").bounds().size.height; + window.click("queue-toggle", cx); + height + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.simulate_next_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + // Preserve normal cached-view invalidation: render_frame() forces a refresh. + window.draw(cx).clear(cx); + let reveal = window.find("agent-queue-reveal"); + let rows = window.find("agent-queue-rows"); + assert!( + reveal.visible(), + "the first post-click frame must show the list" + ); + assert!(rows.bounds().size.height > px(20.)); + assert!((reveal.bounds().size.height - rows.bounds().size.height).abs() <= px(1.)); + let remaining = window.find("agent-transcript-area").bounds().size.height; + assert!((viewport - remaining - reveal.bounds().size.height).abs() <= px(1.)); + }) + .unwrap(); +} + +#[gpui_kit::test] +fn the_queue_opens_at_full_height_in_its_first_frame(cx: &mut TestAppContext) { + assert_first_open_frame(false, cx); +} + +#[gpui_kit::test] +fn reduced_motion_opens_the_queue_without_a_measurement_frame(cx: &mut TestAppContext) { + assert_first_open_frame(true, cx); +} + #[gpui_kit::test] fn expanded_queue_fits_one_and_two_short_messages_without_blank_height(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -68,7 +119,7 @@ fn expanded_queue_fits_one_and_two_short_messages_without_blank_height(cx: &mut #[gpui_kit::test] fn wrapped_text_images_and_attachments_are_measured_then_capped(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -106,7 +157,6 @@ fn collapsed_queue_skips_hidden_row_and_image_rendering(cx: &mut TestAppContext) let f = fixture_with_width( cx, 32., - usage_source(), workspace_resolver(), crate::ImageDeps::new(codec.clone(), codec.clone()), ); @@ -145,3 +195,56 @@ fn collapsed_queue_skips_hidden_row_and_image_rendering(cx: &mut TestAppContext) "opening queue must prepare its images" ); } + +#[gpui_kit::test] +fn reopening_the_queue_keeps_its_thumbnail_and_needs_no_animation_ticks(cx: &mut TestAppContext) { + let codec = image_codec(); + let f = fixture_with_width( + cx, + 18., + workspace_resolver(), + crate::ImageDeps::new(codec.clone(), codec.clone()), + ); + new_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| { + thread.send("active".into(), cx); + thread + .accept_composer_images(vec![prompt_image()], cx) + .unwrap(); + thread.send("image in the queue".into(), cx); + }); + settle(&f, cx); + for open in [true, false, true] { + cx.update_window(f.handle, |_, window, cx| window.click("queue-toggle", cx)) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.simulate_next_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(f.handle, |_, window, cx| { + window.draw(cx).clear(cx); + if open { + let reveal = window.find("agent-queue-reveal"); + let height = window.find("agent-queue-rows").bounds().size.height; + assert!(reveal.visible()); + assert!((reveal.bounds().size.height - height.min(px(240.))).abs() <= px(1.)); + } else { + assert!(window.try_find("agent-queue-rows").is_none()); + } + }) + .unwrap(); + cx.run_until_parked(); + } + let prepared = codec + .requests() + .iter() + .filter(|request| matches!(request, nocterm_ai::testing::ImageRequest::Thumbnail(..))) + .count(); + assert_eq!( + prepared, 1, + "reopening must reuse the existing image preview" + ); +} diff --git a/crates/agent/src/panel/tests/queue_remove.rs b/crates/agent/src/panel/tests/queue_remove.rs index 0dfa9e2e..771b027e 100644 --- a/crates/agent/src/panel/tests/queue_remove.rs +++ b/crates/agent/src/panel/tests/queue_remove.rs @@ -114,7 +114,7 @@ fn removing_the_only_content_of_a_saved_chat_clears_it_on_disk(cx: &mut TestAppC images: Vec::new(), attachments: Vec::new(), }); - let id = saved.id.clone(); + let id = saved.header.id.clone(); f.repository.seed_chat(&saved).unwrap(); reinstall_history(&mut f, vec![saved], cx); cx.update_window(f.handle, |_, window, cx| { diff --git a/crates/agent/src/panel/tests/queue_timing.rs b/crates/agent/src/panel/tests/queue_timing.rs new file mode 100644 index 00000000..194035cd --- /dev/null +++ b/crates/agent/src/panel/tests/queue_timing.rs @@ -0,0 +1,154 @@ +//! Manual CPU/layout timing through the real queue button and normal cached draws. +//! Run with `cargo test -p nocterm-agent --lib queue_opening_frame_trace -- --ignored --nocapture`. +//! Optional NOCTERM_QUEUE_PROBE_{BYTES,COUNT,IMAGE,WARM,REDUCE_MOTION} select the workload. +//! Set NOCTERM_QUEUE_PROBE_NATIVE_TEXT=1 for real system font shaping. +//! Logical 16 ms frame steps are not GPU presentation or wall-clock input latency. +use super::*; +use gpui_kit::px; +use std::time::{Duration, Instant}; + +fn option(name: &str, default: usize) -> usize { + std::env::var(format!("NOCTERM_QUEUE_PROBE_{name}")) + .ok() + .map(|value| { + value + .parse() + .expect("queue probe option must be an integer") + }) + .unwrap_or(default) +} + +fn advance_frame(f: &Fixture, cx: &mut TestAppContext, advance: bool) { + if advance { + cx.executor().advance_clock(Duration::from_millis(16)); + } + cx.update_window(f.handle, |_, window, cx| { + window.simulate_next_frame(cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +fn settle_motion(f: &Fixture, cx: &mut TestAppContext) { + for _ in 0..20 { + advance_frame(f, cx, true); + cx.update_window(f.handle, |_, window, cx| window.draw(cx).clear(cx)) + .unwrap(); + cx.run_until_parked(); + } +} + +#[gpui_kit::test] +#[ignore = "manual queue opening timing probe"] +fn queue_opening_frame_trace(cx: &mut TestAppContext) { + if option("NATIVE_TEXT", 0) == 0 { + measure_queue(cx); + return; + } + let platform = gpui_kit::platform::current_platform(true); + let mut native = TestAppContext::build_with_text_system( + gpui_kit::TestDispatcher::new(0), + Some("queue_opening_native_text"), + platform.text_system(), + ); + measure_queue(&mut native); +} + +#[expect( + clippy::print_stderr, + reason = "explicitly requested manual queue timing diagnostics" +)] +fn measure_queue(cx: &mut TestAppContext) { + let (bytes, count) = (option("BYTES", 32), option("COUNT", 1)); + let (image, warm, reduced) = ( + option("IMAGE", 0) != 0, + option("WARM", 0) != 0, + option("REDUCE_MOTION", 0) != 0, + ); + let codec = image_codec(); + let f = fixture_with_width( + cx, + 18., + workspace_resolver(), + crate::ImageDeps::new(codec.clone(), codec.clone()), + ); + new_chat(&f, cx); + cx.update(|cx| cx.set_reduce_motion(reduced)); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + thread.update(cx, |thread, cx| { + thread.clear_composer_context(cx); + thread.send("active".into(), cx); + if image { + thread + .accept_composer_images(vec![prompt_image()], cx) + .unwrap(); + } + let mut text = "queued message ".repeat(bytes.div_ceil(15)); + text.truncate(bytes); + for _ in 0..count { + thread.send(text.clone(), cx); + } + }); + super::queue_layout::settle(&f, cx); + if warm { + open_queue(&f, cx); + settle_motion(&f, cx); + cx.update_window(f.handle, |_, window, cx| window.click("queue-toggle", cx)) + .unwrap(); + settle_motion(&f, cx); + } + eprintln!( + "QUEUE_WORKLOAD {}", + serde_json::json!({ + "bytes": bytes, "count": count, "image": image, "warm": warm, "reduced": reduced, + }) + ); + trace_open(&f, codec.as_ref(), cx); +} + +#[expect( + clippy::print_stderr, + reason = "explicitly requested manual queue timing diagnostics" +)] +fn trace_open( + f: &Fixture, + codec: &nocterm_ai::testing::ScriptedImageCodec, + cx: &mut TestAppContext, +) { + cx.update_window(f.handle, |_, window, cx| window.click("queue-toggle", cx)) + .unwrap(); + cx.run_until_parked(); + for frame in 0..20 { + advance_frame(f, cx, frame != 0); + let started = Instant::now(); + let (height, visible, rows) = cx + .update_window(f.handle, |_, window, cx| { + window.draw(cx).clear(cx); + let reveal = window.try_find("agent-queue-reveal"); + ( + reveal + .as_ref() + .map(|node| node.bounds().size.height / px(1.)), + reveal.as_ref().is_some_and(|node| node.visible()), + window + .try_find("agent-queue-rows") + .map(|node| node.bounds().size.height / px(1.)), + ) + }) + .unwrap(); + cx.run_until_parked(); + eprintln!( + "QUEUE_FRAME {}", + serde_json::json!({ + "ms": frame * 16, "frame_us": started.elapsed().as_micros(), + "height": height, "visible": visible, "rows": rows, + }) + ); + } + let thumbnails = codec + .requests() + .iter() + .filter(|request| matches!(request, nocterm_ai::testing::ImageRequest::Thumbnail(..))) + .count(); + eprintln!("QUEUE_THUMBNAILS {thumbnails}"); +} diff --git a/crates/agent/src/panel/tests/reconnect.rs b/crates/agent/src/panel/tests/reconnect.rs index e3e09cb9..3d4da12a 100644 --- a/crates/agent/src/panel/tests/reconnect.rs +++ b/crates/agent/src/panel/tests/reconnect.rs @@ -75,7 +75,7 @@ fn an_unreachable_server_is_reported_and_its_tab_stays(cx: &mut TestAppContext) assert!( f.workspace .read(cx) - .items() + .items(cx) .any(|item| item.item_id() == f.terminal) ); }); @@ -112,3 +112,48 @@ fn identical_secret_prompt_tokens_are_specific_to_the_original_request(cx: &mut }); assert_eq!(*f.access.sign_in().answers().borrow(), ["current"]); } + +#[gpui_kit::test] +fn terminal_files_follow_the_configured_filesystem_and_the_exact_connection( + cx: &mut TestAppContext, +) { + use nocterm_workspace::{TerminalError, TerminalFiles as _, TerminalFilesystem}; + let f = fixture(cx); + new_chat(&f, cx); + let expected = cx.update(|cx| f.access.info(cx).unwrap().expected); + cx.update(|cx| { + assert!(matches!( + f.access.filesystem(expected, cx), + Err(TerminalError::Unsupported( + nocterm_workspace::TerminalCapability::Files + )) + )); + }); + f.access.serve_local_files(); + cx.update(|cx| { + assert!(matches!( + f.access.filesystem(expected, cx), + Ok(TerminalFilesystem::Local) + )); + }); + f.access.link().remote().set(true); + let host = + nocterm_session::HostAddress::direct(nocterm_session::Target::new("agent", "server", 22)); + let remote = f.access.serve_remote_files(host.clone()); + cx.update(|cx| { + let Ok(TerminalFilesystem::Remote { host: served, fs }) = f.access.filesystem(expected, cx) + else { + panic!("a remote terminal serves its configured filesystem"); + }; + assert_eq!(served, host); + let remote: Arc = remote; + assert!(Arc::ptr_eq(&fs, &remote)); + }); + f.access.replace_connection(); + cx.update(|cx| { + assert!(matches!( + f.access.filesystem(expected, cx), + Err(TerminalError::StaleSession) + )); + }); +} diff --git a/crates/agent/src/panel/tests/registration.rs b/crates/agent/src/panel/tests/registration.rs new file mode 100644 index 00000000..4eb222fd --- /dev/null +++ b/crates/agent/src/panel/tests/registration.rs @@ -0,0 +1,40 @@ +//! The agent registers as the required plugin its descriptor declares. +use super::*; +use crate::plugin::{CHATS, PLUGIN}; +use nocterm_ui::contribution_icon; +use nocterm_workspace::test_support::{Toggle, toggle_presentation}; + +#[gpui_kit::test] +fn the_side_panel_toggle_shows_the_descriptors_title_and_icon(cx: &mut TestAppContext) { + let f = fixture(cx); + + cx.update(|cx| { + let workspace = f.workspace.read(cx); + assert!(workspace.is_registered(&CHATS.id)); + assert_eq!( + toggle_presentation(workspace, Toggle::SidePanel, cx), + Some((CHATS.title.into(), contribution_icon(CHATS.icon))) + ); + }); +} + +#[gpui_kit::test] +fn the_agent_is_a_required_plugin_and_the_master_switch_withdraws_its_panel( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + assert!(PLUGIN.required); + assert!(cx.update(|cx| f.workspace.read(cx).panel_is_available(&CHATS.id))); + + let disabled = cx.update(|cx| { + f.installation + .ready + .settings() + .update::(cx, |settings| settings.enabled = false) + }); + cx.foreground_executor().block_test(disabled).unwrap(); + cx.run_until_parked(); + + assert!(cx.update(|cx| f.workspace.read(cx).is_registered(&CHATS.id))); + assert!(!cx.update(|cx| f.workspace.read(cx).panel_is_available(&CHATS.id))); +} diff --git a/crates/agent/src/panel/tests/resources.rs b/crates/agent/src/panel/tests/resources.rs index 74b866e3..c53c1057 100644 --- a/crates/agent/src/panel/tests/resources.rs +++ b/crates/agent/src/panel/tests/resources.rs @@ -109,7 +109,12 @@ async fn idle_guards_and_paused_queue_release_independently_of_terminal_ownershi cx.update(|cx| assert!(thread.read(cx).is_leased())); drop(guard); thread.update(cx, |thread, cx| { - thread.begin_composer_edit(Vec::new(), thread.composer().attachments().to_vec(), cx); + thread.begin_composer_edit( + Vec::new(), + Vec::new(), + thread.composer().attachments().to_vec(), + cx, + ); thread.submit("paused".into(), None, cx).unwrap(); thread.pause_queue(cx); thread.end_composer_edit(false, cx); @@ -260,66 +265,6 @@ fn quit_flushes_documents_that_have_no_live_session(cx: &mut TestAppContext) { assert_eq!(chats[0].draft.as_deref(), Some("unsaved dormant edit")); assert_eq!(f.connector.connects.load(Ordering::SeqCst), 0); } -#[gpui_kit::test] -async fn admission_is_shared_between_separate_workspace_windows(cx: &mut TestAppContext) { - let f = fixture(cx); - cx.update(|cx| { - f.installation - .ready - .settings() - .update::(cx, |settings| settings.sessions.max_live = 1) - }) - .await - .unwrap(); - let first = draft(&f, cx); - first.update(cx, |thread, cx| thread.send("window one".into(), cx)); - cx.run_until_parked(); - let (handle, _workspace, panel) = cx.update(|cx| { - let mut panel = None; - let (handle, workspace) = - gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { - cx.new(|cx| { - let mut workspace = Workspace::new( - nocterm_workspace::Deps::new(f.installation.layout.clone()), - window, - cx, - ); - let owner = cx.entity().downgrade(); - let view = cx.new(|cx| { - AgentPanel::new( - f._directory.host(owner, f.installation.installed.clone()), - window, - cx, - ) - }); - workspace.set_right_panel(view.clone(), window, cx); - panel = Some(view); - workspace - }) - }) - .unwrap(); - (handle, workspace, panel.unwrap()) - }); - let second = cx - .update_window(handle, |_, window, cx| { - panel.update(cx, |panel, cx| panel.new_thread("codex".into(), window, cx)); - panel.read(cx).current().unwrap() - }) - .unwrap(); - second.update(cx, |thread, cx| thread.send("window two".into(), cx)); - cx.run_until_parked(); - assert_eq!(f.connector.connects.load(Ordering::SeqCst), 1); - cx.update(|cx| assert_eq!(second.read(cx).composer().queue().len(), 1)); - complete_active(&f, cx); - tick(cx, 1); - assert_eq!(f.connector.connects.load(Ordering::SeqCst), 2); - cx.update(|cx| { - assert!(!first.read(cx).is_leased()); - assert!(second.read(cx).session_generating()); - }); - complete_active(&f, cx); -} - #[gpui_kit::test] async fn warm_idle_limit_evicts_the_oldest_idle_session_and_keeps_documents( cx: &mut TestAppContext, @@ -471,7 +416,7 @@ fn quit_orders_queued_atomic_writes_before_the_latest_document_snapshot(cx: &mut cx.run_until_parked(); let chats = f.repository.chats(); assert_eq!(chats.len(), 1); - assert_eq!(chats[0].name.as_deref(), Some("latest document")); + assert_eq!(chats[0].header.name.as_deref(), Some("latest document")); assert_eq!(f.connector.connects.load(Ordering::SeqCst), 0); } @@ -797,3 +742,5 @@ async fn saved_drafts_do_not_launch_or_keep_an_idle_agent_alive(cx: &mut TestApp mod durability; mod cleanup; + +mod windows; diff --git a/crates/agent/src/panel/tests/resources/durability.rs b/crates/agent/src/panel/tests/resources/durability.rs index 72a0d5e7..96d32b7f 100644 --- a/crates/agent/src/panel/tests/resources/durability.rs +++ b/crates/agent/src/panel/tests/resources/durability.rs @@ -48,10 +48,13 @@ fn shutdown_is_durable_even_if_its_task_is_dropped_and_old_writes_are_pending( shutdown_runtime(&f.installation.installed, cx); let read = || f.repository.chats(); assert_eq!(read().len(), 1); - assert_eq!(read()[0].name.as_deref(), Some("latest unsaved document")); + assert_eq!( + read()[0].header.name.as_deref(), + Some("latest unsaved document") + ); cx.run_until_parked(); assert_eq!( - read()[0].name.as_deref(), + read()[0].header.name.as_deref(), Some("latest unsaved document"), "a stale background snapshot overwrote synchronous quit persistence" ); diff --git a/crates/agent/src/panel/tests/resources/windows.rs b/crates/agent/src/panel/tests/resources/windows.rs new file mode 100644 index 00000000..439d40df --- /dev/null +++ b/crates/agent/src/panel/tests/resources/windows.rs @@ -0,0 +1,64 @@ +//! Admission belongs to the installation across original root windows. +use super::*; + +#[gpui_kit::test] +async fn admission_is_shared_between_separate_workspace_windows(cx: &mut TestAppContext) { + let f = fixture(cx); + cx.update(|cx| { + f.installation + .ready + .settings() + .update::(cx, |settings| settings.sessions.max_live = 1) + }) + .await + .unwrap(); + let first = draft(&f, cx); + first.update(cx, |thread, cx| thread.send("window one".into(), cx)); + cx.run_until_parked(); + let (handle, _workspace, panel) = cx.update(|cx| { + let mut panel = None; + let (handle, workspace) = + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let mut workspace = Workspace::new( + crate::panel::tests::deps(f.installation.layout.clone()), + window, + cx, + ); + let owner = cx.entity().downgrade(); + let source = workspace.general_surface_source(cx); + let view = cx.new(|cx| { + AgentPanel::new( + f._directory + .host(owner, f.installation.installed.clone(), source), + window, + cx, + ) + }); + crate::panel::tests::host_panel(&mut workspace, &view, window, cx); + panel = Some(view); + workspace + }) + }) + .unwrap(); + (handle, workspace, panel.unwrap()) + }); + let second = cx + .update_window(handle, |_, window, cx| { + panel.update(cx, |panel, cx| panel.new_thread("codex".into(), window, cx)); + panel.read(cx).current().unwrap() + }) + .unwrap(); + second.update(cx, |thread, cx| thread.send("window two".into(), cx)); + cx.run_until_parked(); + assert_eq!(f.connector.connects.load(Ordering::SeqCst), 1); + cx.update(|cx| assert_eq!(second.read(cx).composer().queue().len(), 1)); + complete_active(&f, cx); + tick(cx, 1); + assert_eq!(f.connector.connects.load(Ordering::SeqCst), 2); + cx.update(|cx| { + assert!(!first.read(cx).is_leased()); + assert!(second.read(cx).session_generating()); + }); + complete_active(&f, cx); +} diff --git a/crates/agent/src/panel/tests/restoration.rs b/crates/agent/src/panel/tests/restoration.rs index 6b80d824..cba867f5 100644 --- a/crates/agent/src/panel/tests/restoration.rs +++ b/crates/agent/src/panel/tests/restoration.rs @@ -152,32 +152,57 @@ pub(super) fn disk_chat(f: &Fixture, id: &str) -> nocterm_ai::history::SavedChat f.repository .chats() .into_iter() - .find(|chat| chat.id == id) + .find(|chat| chat.header.id == id) .unwrap() } -pub(super) fn reopen( +pub(super) fn restore_released( f: &Fixture, saved: nocterm_ai::history::SavedChat, cx: &mut TestAppContext, ) -> Entity { - f.panel.update(cx, |panel, cx| { - for thread in panel.chat_documents() { - if thread.read(cx).document().chat_id() == saved.id { + let retired = f.panel.update(cx, |panel, cx| { + let threads = panel + .chat_documents() + .filter(|thread| thread.read(cx).document().chat_id() == saved.header.id) + .cloned() + .collect::>(); + threads + .into_iter() + .map(|thread| { thread.update(cx, |thread, cx| thread.release_resources(cx)); - } + thread.entity_id() + }) + .collect::>() + }); + cx.update(|cx| { + for id in retired { + f.installation + .installed + .runtime() + .capture_and_detach_document(id, cx) + .unwrap(); } }); cx.run_until_parked(); - let thread = cx.new(|cx| { - crate::thread::AgentThread::restored( - saved, - f._directory - .deps(f.workspace.downgrade(), f.installation.installed.clone()), - cx, - ) - }); - cx.update(|cx| activate_thread(&thread, &f.installation.installed, cx)); + cx.update(|cx| { + f._directory + .deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ) + .restore_chat(saved, cx) + .unwrap() + }) +} +pub(super) fn reopen( + f: &Fixture, + saved: nocterm_ai::history::SavedChat, + cx: &mut TestAppContext, +) -> Entity { + let thread = restore_released(f, saved, cx); + cx.update(|cx| activate_thread(&thread, cx)); cx.run_until_parked(); thread } @@ -416,3 +441,203 @@ fn a_nonmissing_rpc_failure_does_not_replace_real_history_or_dispatch_its_queue( assert_eq!(thread.read(cx).transcript().entries.len(), 1); }); } + +fn catalog_row( + f: &Fixture, + chat: &SavedChat, + cx: &mut TestAppContext, +) -> nocterm_agent_runtime::CatalogRow { + f.repository.seed_chat(chat).unwrap(); + let runtime = f + .installation + .installed + .runtime() + .for_scope(chat.header.scope); + cx.update(|cx| runtime.reload_history(cx)); + cx.run_until_parked(); + cx.update(|cx| { + let snapshot = runtime.catalog_snapshot(cx).unwrap(); + let index = snapshot + .rows() + .iter() + .position(|row| row.id == chat.header.id) + .unwrap(); + snapshot.row(index).unwrap() + }) +} + +#[gpui_kit::test] +fn project_restore_refuses_before_entity_creation_and_preserves_the_general_chat( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + lazy_start(&f, cx); + new_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap().clone()); + let before = cx.update(|cx| restored_projection(thread.read(cx), cx)); + let count = cx.update(|cx| f.panel.read(cx).chat_documents().count()); + let connects = f.connector.connects.load(Ordering::SeqCst); + let mut project = before.clone(); + project.header.scope = serde_json::from_value(serde_json::json!({ + "kind": "project", "id": "01010101010101010101010101010101" + })) + .unwrap(); + project.draft = Some("foreign project draft".into()); + project.entries = vec![Entry::Agent("foreign project transcript".into())]; + let project_row = catalog_row(&f, &project, cx); + cx.update(|cx| { + let deps = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + assert!(matches!( + deps.restore_chat(project.clone(), cx), + Err(nocterm_agent_runtime::ChatOpenError::Storage( + nocterm_ai::storage::AgentStorageError::ScopeMismatch { .. } + )) + )); + assert!(matches!( + deps.restore_archive(project_row, cx), + Err(nocterm_agent_runtime::ChatOpenError::Storage( + nocterm_ai::storage::AgentStorageError::ScopeMismatch { .. } + )) + )); + let error = thread + .update(cx, |thread, cx| thread.restore_content(project, cx)) + .unwrap_err(); + assert!(matches!( + error, + nocterm_ai::storage::AgentStorageError::ScopeMismatch { .. } + )); + let after = restored_projection(thread.read(cx), cx); + assert_eq!( + serde_json::to_value(after).unwrap(), + serde_json::to_value(before).unwrap() + ); + assert_eq!(f.panel.read(cx).chat_documents().count(), count); + }); + cx.run_until_parked(); + assert_eq!(f.connector.connects.load(Ordering::SeqCst), connects); +} + +#[gpui_kit::test] +fn duplicate_restoration_keeps_the_original_document_and_reports_its_original_window( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + let chat = empty_chat(&f); + let (thread, deps) = cx.update(|cx| { + let deps = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + let thread = deps.restore_chat(chat.clone(), cx).unwrap(); + (thread, deps) + }); + let row = catalog_row(&f, &chat, cx); + thread.update(cx, |thread, cx| { + thread.set_draft("original document input".into(), cx) + }); + cx.update(|cx| { + assert!(matches!(deps.restore_chat(chat.clone(), cx), Err(nocterm_agent_runtime::ChatOpenError::AlreadyOpen { window, .. }) if window == f.workspace.entity_id())); + assert!(matches!(deps.restore_archive(row, cx), Err(nocterm_agent_runtime::ChatOpenError::AlreadyOpen { window, .. }) if window == f.workspace.entity_id())); + assert_eq!(thread.read(cx).snapshot(cx).unwrap().draft.as_deref(), Some("original document input")); + assert!(deps.runtime().document_is_current(&chat.header.id, thread.entity_id(), cx)); + }); + cx.run_until_parked(); + assert_eq!(f.commands.sessions.load(Ordering::SeqCst), 0); +} +#[gpui_kit::test] +fn restored_project_data_keeps_its_scope_after_edits_and_refuses_a_general_hydration( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + let mut chat = empty_chat(&f); + let scope = serde_json::from_value( + serde_json::json!({"kind":"project", "id":"01010101010101010101010101010101"}), + ) + .unwrap(); + chat.header.scope = scope; + let original = chat.clone(); + let (thread, _surface, _outer) = cx + .update_window(f.handle, |_, window, cx| { + let general = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + let nocterm_ai::history::ContextId::Project(project) = scope else { + panic!("project fixture"); + }; + let (seat, surface, outer) = super::seat_binding::project_seat(&f, project, window, cx); + // This fixture exercises saved data without starting a local agent. + let deps = crate::chat_deps::ChatDeps::new( + general.runtime().for_scope(scope), + general.agents().clone(), + general.settings().clone(), + f._directory.metadata.clone(), + seat, + None, + ); + (deps.restore_chat(chat, cx).unwrap(), surface, outer) + }) + .unwrap(); + thread.update(cx, |thread, cx| { + thread.rename(Some("Scoped project title".into()), cx); + thread.set_draft("project literal draft".into(), cx); + }); + cx.update(|cx| { + let before = thread.read(cx).snapshot(cx).unwrap(); + assert_eq!(before.header.scope, scope); + assert_eq!(thread.read(cx).document().key().scope(), scope); + let mut wrong = original; + wrong.header.scope = nocterm_ai::history::ContextId::General; + assert!(matches!( + thread.update(cx, |thread, cx| thread.restore_content(wrong, cx)), + Err(nocterm_ai::storage::AgentStorageError::ScopeMismatch { .. }) + )); + let after = thread.read(cx).snapshot(cx).unwrap(); + assert_eq!( + serde_json::to_value(after).unwrap(), + serde_json::to_value(before).unwrap() + ); + }); + cx.run_until_parked(); + let id = cx.update(|cx| thread.read(cx).chat_id().to_owned()); + assert!(f.repository.encoded(&id).is_none()); + assert_eq!(f.commands.sessions.load(Ordering::SeqCst), 0); +} +#[gpui_kit::test] +fn legacy_general_restore_edits_and_restart_keep_wire_version_while_forks_use_v3( + cx: &mut TestAppContext, +) { + let mut f = fixture(cx); + let mut saved = empty_chat(&f); + saved.version = 1; + let original = adopt_saved(&mut f, saved, cx); + original.update(cx, |thread, cx| { + thread.rename(Some("Legacy title".into()), cx) + }); + cx.update(|cx| assert_eq!(original.read(cx).snapshot(cx).unwrap().version, 1)); + let restarted = cx + .update_window(f.handle, |_, window, cx| { + f.panel.update(cx, |panel, cx| panel.restart(window, cx)); + f.panel.read(cx).current().unwrap() + }) + .unwrap(); + cx.update(|cx| { + assert_ne!(original.entity_id(), restarted.entity_id()); + assert_eq!(restarted.read(cx).snapshot(cx).unwrap().version, 1); + }); + let fork = cx + .update_window(f.handle, |_, window, cx| { + f.panel.update(cx, |panel, cx| { + panel.fork_thread(restarted.entity_id(), window, cx) + }); + f.panel.read(cx).current().unwrap() + }) + .unwrap(); + cx.update(|cx| assert_eq!(fork.read(cx).snapshot(cx).unwrap().version, 3)); +} diff --git a/crates/agent/src/panel/tests/routing.rs b/crates/agent/src/panel/tests/routing.rs index d09f9949..c2573bfe 100644 --- a/crates/agent/src/panel/tests/routing.rs +++ b/crates/agent/src/panel/tests/routing.rs @@ -170,6 +170,17 @@ fn prompts_tell_the_agent_to_report_unreachable_servers(cx: &mut TestAppContext) assert!(context.text.contains("")); } +#[gpui_kit::test] +fn an_agent_with_tools_of_its_own_gets_only_what_the_person_wrote(cx: &mut TestAppContext) { + let f = fixture(cx); + f.connector.info.lock().unwrap().capabilities.own_tools = true; + new_chat(&f, cx); + exchange(&f, "check the server", "ok", cx); + let prompt = f.commands.prompts.lock().unwrap()[0].clone(); + assert_eq!(prompt.content.len(), 1, "{:?}", prompt.content); + assert!(matches!(&prompt.content[0], Content::Text(text) if text.text == "check the server")); +} + #[gpui_kit::test] fn an_unreachable_terminal_asks_the_agent_to_tell_the_user(cx: &mut TestAppContext) { let f = fixture(cx); diff --git a/crates/agent/src/panel/tests/scripted_agent.rs b/crates/agent/src/panel/tests/scripted_agent.rs new file mode 100644 index 00000000..76dd8054 --- /dev/null +++ b/crates/agent/src/panel/tests/scripted_agent.rs @@ -0,0 +1,243 @@ +//! Scripted agent commands, connector and tool bridge behind the panel fixture. +use super::*; + +#[derive(Default)] +pub(super) struct Commands { + pub(super) sessions: AtomicU64, + pub(super) new_dirs: Mutex>, + pub(super) restore_dirs: Mutex>, + pub(super) session_gate: Mutex>>, + pub(super) close_gate: Mutex>>, + pub(super) closed: Mutex>, + pub(super) shutdown_failure: AtomicBool, + pub(super) auth_required: AtomicBool, + pub(super) authentications: AtomicUsize, + pub(super) authentication_failure: AtomicBool, + pub(super) shutdowns: AtomicUsize, + pub(super) cancels: AtomicUsize, + pub(super) prompts: Mutex>, + pub(super) pending: Mutex>>, + /// Each reopened session, and whether it was forked. + pub(super) restores: Mutex>, + pub(super) restore_errors: Mutex>, + /// The MCP servers each new session was given. + pub(super) servers: Mutex>>, + /// The configuration options every session reports. + pub(super) config: super::config_responses::ConfigResponses, +} +impl AgentCommands for Commands { + fn new_session( + &self, + request: SessionOpen, + ) -> BoxFuture<'static, Result> { + self.new_dirs + .lock() + .unwrap() + .push(request.working_directory.clone()); + self.servers.lock().unwrap().push(request.endpoints); + let gate = self.session_gate.lock().unwrap().take(); + let id = self.sessions.fetch_add(1, Ordering::SeqCst); + let auth_required = self.auth_required.load(Ordering::SeqCst); + let config = self.config.options(); + let modes = self.config.modes(); + async move { + if let Some(gate) = gate { + let _ = gate.await; + } + if auth_required { + Err(AgentError::AuthRequired("Sign in to continue".into())) + } else { + Ok(opened(format!("s{id}"), config, modes)) + } + } + .boxed() + } + fn restore_session( + &self, + request: nocterm_ai::model::session::RestoreSession, + ) -> BoxFuture<'static, Result> { + self.restore_dirs + .lock() + .unwrap() + .push(request.working_directory.clone()); + self.restores + .lock() + .unwrap() + .push((request.session.clone(), request.fork)); + if let Some(error) = self.restore_errors.lock().unwrap().pop_front() { + return async move { Err(error) }.boxed(); + } + let session = if request.fork { + SessionId::new(format!("fork-of-{}", request.session)) + } else { + request.session + }; + let config = self.config.options(); + let modes = self.config.modes(); + async move { Ok(opened(session, config, modes)) }.boxed() + } + fn prompt(&self, r: PromptRequest) -> BoxFuture<'static, Result> { + self.prompts.lock().unwrap().push(r); + if self.auth_required.load(Ordering::SeqCst) { + return async { Err(AgentError::AuthRequired("Sign in to continue".into())) }.boxed(); + } + let (tx, rx) = oneshot::channel(); + *self.pending.lock().unwrap() = Some(tx); + async move { rx.await.map_err(|_| AgentError::Io("closed".into())) }.boxed() + } + fn cancel(&self, _: SessionId) { + self.cancels.fetch_add(1, Ordering::SeqCst); + } + fn set_mode(&self, _: SetMode) -> BoxFuture<'static, Result<(), AgentError>> { + async { Ok(()) }.boxed() + } + fn set_config_option( + &self, + request: SetConfigOption, + ) -> BoxFuture<'static, Result, AgentError>> { + let options = self.config.choose(request); + async move { Ok(options) }.boxed() + } + fn authenticate(&self, _: AuthMethodId) -> BoxFuture<'static, Result<(), AgentError>> { + self.authentications.fetch_add(1, Ordering::SeqCst); + let failed = self.authentication_failure.load(Ordering::SeqCst); + if !failed { + self.auth_required.store(false, Ordering::SeqCst); + } + async move { + if failed { + Err(AgentError::Io("Sign-in failed".into())) + } else { + Ok(()) + } + } + .boxed() + } + fn close_session( + &self, + session: SessionId, + ) -> BoxFuture<'static, Result> { + self.closed.lock().unwrap().push(session); + let gate = self.close_gate.lock().unwrap().take(); + async move { + if let Some(gate) = gate { + let _ = gate.await; + } + Ok(nocterm_ai::CloseSessionOutcome::Closed) + } + .boxed() + } + fn shutdown_gracefully(&self) -> BoxFuture<'static, Result<(), AgentError>> { + self.shutdown(); + let failed = self.shutdown_failure.load(Ordering::SeqCst); + async move { + if failed { + Err(AgentError::Io("Process tree still alive".into())) + } else { + Ok(()) + } + } + .boxed() + } + fn shutdown(&self) { + self.shutdowns.fetch_add(1, Ordering::SeqCst); + } +} +pub(super) struct Connector { + pub(super) distinct_process_handles: AtomicBool, + pub(super) commands: Arc, + pub(super) events: async_channel::Receiver, + pub(super) connects: AtomicUsize, + pub(super) roots: Mutex< + Vec<( + std::path::PathBuf, + Option, + )>, + >, + pub(super) event_senders: Mutex>>, + pub(super) info: Mutex, +} +impl Connector { + pub(super) fn use_distinct_process_handles(&self) { + self.distinct_process_handles.store(true, Ordering::SeqCst); + } + pub(super) fn report_auth_methods(&self, methods: Vec) { + self.info.lock().unwrap().auth_methods = methods; + } + pub(super) fn support_images(&self) { + self.info.lock().unwrap().capabilities.image_prompts = true; + } + /// The agent runs elsewhere with tools of its own, as a hub agent does. + pub(super) fn run_elsewhere(&self) { + self.info.lock().unwrap().capabilities.own_tools = true; + } +} +impl AgentConnector for Connector { + fn connect( + &self, + request: ConnectRequest, + ) -> BoxFuture<'static, Result> { + self.roots + .lock() + .unwrap() + .push((request.working_directory, request.sandbox)); + self.connects.fetch_add(1, Ordering::SeqCst); + let commands: Arc = + if self.distinct_process_handles.load(Ordering::SeqCst) { + Arc::new(super::process_commands::ProcessCommands::new( + self.commands.clone(), + )) + } else { + self.commands.clone() + }; + let info = self.info.lock().unwrap().clone(); + let (sender, events) = async_channel::bounded(256); + self.event_senders.lock().unwrap().push(sender); + async move { + Ok(AgentConnection { + commands, + events, + info, + }) + } + .boxed() + } +} +pub(super) struct Bridge { + pub(super) next: AtomicU64, + pub(super) revoked: Arc>>, + /// Every tool offer a chat made, with its registration. + pub(super) offers: Arc>>, + pub(super) stops: AtomicUsize, + pub(super) calls: async_channel::Receiver, + pub(super) rejections: async_channel::Receiver, +} +impl ToolBridge for Bridge { + fn register( + &self, + ) -> Result { + let id = self.next.fetch_add(1, Ordering::SeqCst); + let (revoked, offers) = (self.revoked.clone(), self.offers.clone()); + Ok(BridgeRegistration::new( + id, + nocterm_ai::BridgeLaunch { + program: "/fake/nocterm".into(), + args: vec!["agent-bridge".into()], + env: vec![("NOCTERM_BRIDGE_TOKEN".into(), "test-token".into())], + }, + Arc::new(move |id| revoked.lock().unwrap().push(id)), + ) + .with_offer(Arc::new(move |id, offer| { + offers.lock().unwrap().push((id, offer)); + }))) + } + fn calls(&self) -> async_channel::Receiver { + self.calls.clone() + } + fn rejections(&self) -> async_channel::Receiver { + self.rejections.clone() + } + fn stop(&self) { + self.stops.fetch_add(1, Ordering::SeqCst); + } +} diff --git a/crates/agent/src/panel/tests/seat_binding.rs b/crates/agent/src/panel/tests/seat_binding.rs new file mode 100644 index 00000000..b423ed96 --- /dev/null +++ b/crates/agent/src/panel/tests/seat_binding.rs @@ -0,0 +1,233 @@ +//! A captured terminal seat belongs to one document and never follows selection. +use super::*; +use nocterm_ai::{execution_origin::ChatKey, history::ContextId, storage::AgentStorageError}; +use nocterm_workspace::test_support::ProjectSurfaceProbe as ProjectTab; + +#[gpui_kit::test] +fn a_document_seat_rejects_scope_changes_and_rebinding_to_another_chat(cx: &mut TestAppContext) { + let f = fixture(cx); + cx.update(|cx| { + let deps = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + let key = ChatKey::new( + ContextId::General, + "00000000-0000-0000-0000-000000000001".into(), + ); + let bound = deps.seat().for_chat(&key).unwrap(); + assert_eq!(bound.id(), f.workspace.entity_id()); + assert!(bound.for_chat(&key).is_ok()); + let foreign = ChatKey::new( + ContextId::General, + "00000000-0000-0000-0000-000000000002".into(), + ); + assert!(matches!( + bound.for_chat(&foreign), + Err(AgentStorageError::Codec( + nocterm_ai::history::HistoryCodecError::IdentityMismatch + )) + )); + let project = serde_json::from_value( + serde_json::json!({"kind":"project", "id":"01010101010101010101010101010101"}), + ) + .unwrap(); + assert!(matches!( + deps.seat() + .for_chat(&ChatKey::new(project, key.id().into())), + Err(AgentStorageError::ScopeMismatch { .. }) + )); + assert_eq!(bound.terminals(cx).unwrap().len(), 1); + }); +} + +/// Real outer workspace Item and its ordinary inner terminal surface. +pub(super) fn project_seat( + f: &Fixture, + project: nocterm_project_model::ProjectId, + window: &mut Window, + cx: &mut App, +) -> ( + nocterm_agent_chat::seat::Seat, + nocterm_workspace::ProjectSurface, + Entity, +) { + let (seat, surface, outer) = pending_project_seat(f, project, window, cx); + f.workspace.update(cx, |workspace, cx| { + workspace + .add_project_workspace(outer.clone(), surface.clone(), window, cx) + .unwrap() + }); + (seat, surface, outer) +} +pub(super) fn pending_project_seat( + f: &Fixture, + project: nocterm_project_model::ProjectId, + window: &mut Window, + cx: &mut App, +) -> ( + nocterm_agent_chat::seat::Seat, + nocterm_workspace::ProjectSurface, + Entity, +) { + let factory = f.workspace.read(cx).project_surface_factory(); + let outer = cx.new(|cx| ProjectTab::new(factory, project, window, cx).unwrap()); + let surface = outer.read(cx).surface(); + let seat = Rc::new(crate::workspace_seat::WorkspaceSeat::new( + f.workspace.downgrade(), + surface.source(cx), + )); + (seat, surface, outer) +} + +fn terminal_ids(seat: &nocterm_agent_chat::seat::Seat, cx: &App) -> Vec { + seat.terminals(cx) + .unwrap() + .into_iter() + .map(|entry| entry.item) + .collect() +} +#[gpui_kit::test] +fn chats_keep_their_workspace_terminals_while_another_workspace_is_active(cx: &mut TestAppContext) { + let f = fixture(cx); + cx.update_window(f.handle, |_, window, cx| { + let general = f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.clone(), + cx, + ); + let original_general = terminal_ids(general.seat(), cx); + let project = nocterm_project_model::ProjectId::from_bytes([1; 16]); + let (base, surface, _outer) = project_seat(&f, project, window, cx); + let a = base + .for_chat(&ChatKey::new( + ContextId::Project(project), + "00000000-0000-0000-0000-000000000001".into(), + )) + .unwrap(); + let b = base + .for_chat(&ChatKey::new( + ContextId::Project(project), + "00000000-0000-0000-0000-000000000002".into(), + )) + .unwrap(); + let mut terminals = Vec::new(); + for _ in 0..2 { + let terminal = cx.new(|cx| FakeTerminal::new(Access::new(None, Rc::default()), cx)); + surface + .add_background_item(terminal.clone(), window, cx) + .unwrap(); + terminals.push(terminal); + } + let other_id = nocterm_project_model::ProjectId::from_bytes([2; 16]); + let (other, other_surface, _other_outer) = project_seat(&f, other_id, window, cx); + let other = other + .for_chat(&ChatKey::new( + ContextId::Project(other_id), + "00000000-0000-0000-0000-000000000003".into(), + )) + .unwrap(); + let foreign = cx.new(|cx| FakeTerminal::new(Access::new(None, Rc::default()), cx)); + other_surface + .add_background_item(foreign.clone(), window, cx) + .unwrap(); + let expected: Vec<_> = terminals.iter().map(Entity::entity_id).collect(); + assert_eq!(terminal_ids(&a, cx), expected); + assert_eq!(terminal_ids(&b, cx), expected); + assert_eq!(terminal_ids(general.seat(), cx), original_general); + a.close_background(foreign.entity_id(), window, cx); + assert_eq!(terminal_ids(&other, cx), [foreign.entity_id()]); + let own = terminals[1].entity_id(); + let session = nocterm_agent_chat::seat::current_session(&a, own, cx).unwrap(); + assert!(a.close_if_original(own, Some(session), window, cx)); + assert_eq!(terminal_ids(&b, cx), [terminals[0].entity_id()]); + assert_eq!( + f.workspace.read(cx).selected_context(cx), + ContextId::Project(other_id) + ); + }) + .unwrap(); +} + +#[gpui_kit::test] +fn simultaneous_account_sources_filter_foreign_terminals_and_relogin_cannot_reclaim_old_authority( + cx: &mut TestAppContext, +) { + use nocterm_ai::local_environment::LocalEnvironmentAccess; + use nocterm_hubs_model::{ + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LocalAccountPartition, + LocalAccountSourceFactory, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, + }; + let f = fixture(cx); + let partition = |hub| { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("same-instance-id").unwrap(), + Fingerprint::from_bytes([hub; 32]), + ), + AccountIdentity::new( + NamespaceId::new("same-account-id").unwrap(), + Fingerprint::from_bytes([3; 32]), + ), + )) + }; + let a = Arc::new(ScriptedLocalAccountFactory::default()); + let b = Arc::new(ScriptedLocalAccountFactory::default()); + a.sign_in(partition(1)); + b.sign_in(partition(2)); + let access_a = Access::new(None, Rc::default()); + let access_b = Access::new(None, Rc::default()); + access_a.bind_source(LocalEnvironmentAccess::capture(a.capture().unwrap()).unwrap()); + access_b.bind_source(LocalEnvironmentAccess::capture(b.capture().unwrap()).unwrap()); + let (id_a, id_b) = cx + .update_window(f.handle, |_, window, cx| { + let terminal_a = cx.new(|cx| FakeTerminal::new(access_a, cx)); + let terminal_b = cx.new(|cx| FakeTerminal::new(access_b, cx)); + f.workspace.update(cx, |workspace, cx| { + workspace.add_item(terminal_a.clone(), window, cx); + workspace.add_item(terminal_b.clone(), window, cx); + }); + (terminal_a.entity_id(), terminal_b.entity_id()) + }) + .unwrap(); + let deps = |factory: Arc, cx: &App| { + f._directory.deps( + f.workspace.downgrade(), + f.installation.installed.with_account_source(factory), + cx, + ) + }; + let (a_seat, b_seat) = cx.update(|cx| { + ( + deps(a.clone(), cx).seat().clone(), + deps(b.clone(), cx).seat().clone(), + ) + }); + cx.update(|cx| { + assert_eq!(terminal_ids(&a_seat, cx), [id_a]); + assert_eq!(terminal_ids(&b_seat, cx), [id_b]); + assert!(nocterm_agent_chat::seat::current_session(&b_seat, id_a, cx).is_none()); + }); + a.close(); + cx.update(|cx| { + assert!(a_seat.terminals(cx).is_none()); + assert_eq!(terminal_ids(&b_seat, cx), [id_b]); + }); + a.sign_in(partition(1)); + cx.update(|cx| { + let fresh = deps(a, cx); + assert!( + fresh.seat().terminals(cx).unwrap().is_empty(), + "the old terminal keeps its original login lease" + ); + assert!( + a_seat.terminals(cx).is_none(), + "a new login does not revive an old seat" + ); + assert_eq!(terminal_ids(&b_seat, cx), [id_b]); + }); +} diff --git a/crates/agent/src/panel/tests/servers.rs b/crates/agent/src/panel/tests/servers.rs index 22f2cb67..fdcfb70a 100644 --- a/crates/agent/src/panel/tests/servers.rs +++ b/crates/agent/src/panel/tests/servers.rs @@ -83,6 +83,31 @@ impl nocterm_workspace::ConnectionDirectory for OpeningDirectory { } } +impl nocterm_workspace::ChatConnectionOpen for OpeningDirectory { + fn open( + &self, + profile: &str, + source: nocterm_workspace::ProjectSurfaceReceipt, + workspace: &gpui_kit::WeakEntity, + window: &mut Window, + cx: &mut App, + ) -> Result { + let item = workspace + .update(cx, |workspace, cx| { + workspace.validate_context_source(source, window, cx)?; + let item = self.terminal(profile, cx); + workspace.add_context_background_from(source, item, window, cx) + }) + .map_err(|_| nocterm_workspace_ports::ContextSelectionError::StaleWindow)??; + self.background.borrow_mut().push(item); + let after_open = self.after_background_open.borrow_mut().take(); + if let Some(after_open) = after_open { + after_open(window, cx); + } + Ok(item) + } +} + fn install(f: &Fixture, cx: &mut TestAppContext) -> Rc { let directory = Rc::new(OpeningDirectory { after_background_open: RefCell::default(), @@ -94,7 +119,8 @@ fn install(f: &Fixture, cx: &mut TestAppContext) -> Rc { }); cx.update(|cx| { f.workspace.update(cx, |workspace, _| { - workspace.set_connection_directory(directory.clone()) + workspace.set_connection_directory(directory.clone()); + workspace.set_chat_connection_opener(directory.clone()); }) }); directory @@ -121,7 +147,7 @@ fn open_in_the_attach_menu_opens_a_tab(cx: &mut TestAppContext) { cx.run_until_parked(); cx.update(|cx| { assert_eq!(directory.opened.get(), 1); - assert_eq!(f.workspace.read(cx).items().count(), 2); + assert_eq!(f.workspace.read(cx).items(cx).count(), 2); }); cx.update_window(f.handle, |_, window, cx| { window.render_frame(cx); @@ -185,8 +211,8 @@ fn agents_open_attached_offline_servers_in_the_background(cx: &mut TestAppContex let background = directory.background.borrow()[0]; cx.update(|cx| { let workspace = f.workspace.read(cx); - assert!(workspace.is_background(background)); - assert_eq!(workspace.items().count(), 1, "no tab was opened"); + assert!(workspace.is_background(background, cx)); + assert_eq!(workspace.items(cx).count(), 1, "no tab was opened"); thread.update(cx, |thread, cx| { assert_eq!(thread.background_items(), vec![background]); assert!(thread.resolved(cx).iter().any(|(id, _, _)| *id == terminal)); @@ -196,7 +222,7 @@ fn agents_open_attached_offline_servers_in_the_background(cx: &mut TestAppContex }); }); cx.run_until_parked(); - cx.update(|cx| assert!(!f.workspace.read(cx).is_background(background))); + cx.update(|cx| assert!(!f.workspace.read(cx).is_background(background, cx))); } #[gpui_kit::test] @@ -221,8 +247,8 @@ fn a_background_session_can_be_shown_in_a_tab(cx: &mut TestAppContext) { cx.run_until_parked(); cx.update(|cx| { let workspace = f.workspace.read(cx); - assert!(!workspace.is_background(item)); - assert!(workspace.items().any(|open| open.item_id() == item)); + assert!(!workspace.is_background(item, cx)); + assert!(workspace.items(cx).any(|open| open.item_id() == item)); }); } @@ -272,7 +298,11 @@ fn a_locked_vault_is_unlocked_from_the_chat_without_a_tab(cx: &mut TestAppContex cx.update_window(f.handle, |_, window, cx| { assert_eq!(thread.read(cx).sign_in_waits().len(), 1); assert!(thread.read(cx).sign_in_waits()[0].vault()); - assert_eq!(f.workspace.read(cx).items().count(), 1, "no tab was opened"); + assert_eq!( + f.workspace.read(cx).items(cx).count(), + 1, + "no tab was opened" + ); window.render_frame(cx); assert!(window.try_find("agent-approvals").is_some()); @@ -302,7 +332,7 @@ fn a_locked_vault_is_unlocked_from_the_chat_without_a_tab(cx: &mut TestAppContex assert!(answer["terminal"]["id"].is_string()); cx.update(|cx| { assert!(thread.read(cx).sign_in_waits().is_empty()); - assert_eq!(f.workspace.read(cx).items().count(), 1, "still no tab"); + assert_eq!(f.workspace.read(cx).items(cx).count(), 1, "still no tab"); }); } @@ -349,7 +379,11 @@ fn a_password_is_typed_in_the_chat_without_a_tab(cx: &mut TestAppContext) { assert!(response.try_recv().unwrap().is_none(), "still signing in"); cx.update_window(f.handle, |_, window, cx| { assert!(!thread.read(cx).sign_in_waits()[0].vault()); - assert_eq!(f.workspace.read(cx).items().count(), 1, "no tab was opened"); + assert_eq!( + f.workspace.read(cx).items(cx).count(), + 1, + "no tab was opened" + ); window.render_frame(cx); assert!(window.try_find(("vault-unlock", 0usize)).is_none()); window.click(("sign-in-input", 0usize), cx); @@ -372,7 +406,7 @@ fn a_password_is_typed_in_the_chat_without_a_tab(cx: &mut TestAppContext) { assert!(answer["terminal"]["id"].is_string()); cx.update(|cx| { assert!(thread.read(cx).sign_in_waits().is_empty()); - assert_eq!(f.workspace.read(cx).items().count(), 1, "still no tab"); + assert_eq!(f.workspace.read(cx).items(cx).count(), 1, "still no tab"); }); } @@ -429,8 +463,11 @@ fn rows_attached_through_a_folder_do_not_toggle_on_their_own(cx: &mut TestAppCon fn a_folder_without_servers_says_so(cx: &mut TestAppContext) { let f = fixture(cx); install(&f, cx); + new_chat(&f, cx); cx.update(|cx| { - let labels = super::super::attachments::AttachmentLabels::new(&f.workspace.downgrade(), cx); + let thread = f.panel.read(cx).current().unwrap(); + let labels = + super::super::attachments::AttachmentLabels::new(thread.read(cx).workspace_seat(), cx); assert_eq!( labels.label(&Attachment::Group("prod".into())), "Group prod" @@ -447,13 +484,13 @@ fn a_folder_without_servers_says_so(cx: &mut TestAppContext) { }); } -fn open_server( +fn queue_server_open( f: &Fixture, thread: &Entity, server: &str, cx: &mut TestAppContext, -) -> serde_json::Value { - let (respond, mut response) = oneshot::channel(); +) -> oneshot::Receiver> { + let (respond, response) = oneshot::channel(); cx.update(|cx| { thread.update(cx, |thread, _| { let registration = thread.registration().as_ref().unwrap().id; @@ -471,6 +508,16 @@ fn open_server( }) }); cx.run_until_parked(); + response +} + +fn open_server( + f: &Fixture, + thread: &Entity, + server: &str, + cx: &mut TestAppContext, +) -> serde_json::Value { + let mut response = queue_server_open(f, thread, server, cx); cx.update_window(f.handle, |_, window, cx| { window.render_frame(cx); if thread.read(cx).has_pending_approvals() { @@ -478,6 +525,7 @@ fn open_server( } }) .unwrap(); + cx.run_until_parked(); cx.executor().advance_clock(Duration::from_secs(1)); cx.run_until_parked(); response.try_recv().unwrap().expect("answered").unwrap() @@ -627,7 +675,7 @@ fn stale_background_opening_cannot_close_a_replacement_session(cx: &mut TestAppC cx.run_until_parked(); cx.update(|cx| { let workspace = f.workspace.read(cx); - assert!(workspace.is_background(item)); + assert!(workspace.is_background(item, cx)); let current = workspace .terminals(cx) .into_iter() @@ -644,3 +692,4 @@ fn stale_background_opening_cannot_close_a_replacement_session(cx: &mut TestAppC } mod background_opening; +mod workspaces; diff --git a/crates/agent/src/panel/tests/servers/background_opening.rs b/crates/agent/src/panel/tests/servers/background_opening.rs index 0f50eb77..d6c4f049 100644 --- a/crates/agent/src/panel/tests/servers/background_opening.rs +++ b/crates/agent/src/panel/tests/servers/background_opening.rs @@ -109,7 +109,7 @@ fn deleting_chat_during_background_creation_closes_its_original_session(cx: &mut ); cx.update(|cx| { let workspace = f.workspace.read(cx); - assert!(!workspace.is_background(opened[0])); + assert!(!workspace.is_background(opened[0], cx)); assert!( workspace .terminals(cx) @@ -153,7 +153,7 @@ fn failed_opening_never_closes_a_session_the_user_promoted_to_a_tab(cx: &mut Tes let item = directory.background.borrow()[0]; cx.update(|cx| { let workspace = f.workspace.read(cx); - assert!(!workspace.is_background(item)); + assert!(!workspace.is_background(item, cx)); assert!( workspace .terminals(cx) diff --git a/crates/agent/src/panel/tests/servers/workspaces.rs b/crates/agent/src/panel/tests/servers/workspaces.rs new file mode 100644 index 00000000..1e1e4b0e --- /dev/null +++ b/crates/agent/src/panel/tests/servers/workspaces.rs @@ -0,0 +1,229 @@ +//! Explicit saved-server attachments stay in the chat's original folder surface. +use super::*; +use nocterm_workspace::ProjectSurfaceReceipt; + +fn project_chat(f: &Fixture, cx: &mut TestAppContext) -> ProjectSurfaceReceipt { + let project = nocterm_project_model::ProjectId::from_bytes([1; 16]); + let source = cx + .update_window(f.handle, |_, window, cx| { + let (_, surface, _) = super::super::seat_binding::project_seat(f, project, window, cx); + surface.source(cx) + }) + .unwrap(); + cx.run_until_parked(); + new_chat(f, cx); + source +} +fn attach(f: &Fixture, row: &str, cx: &mut TestAppContext) { + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + window.click("agent-attach-context", cx); + window.render_frame(cx); + window.click(row.to_owned(), cx); + if row.starts_with("attach-") { + window.press("escape", cx); + window.render_frame(cx); + assert!(window.try_find("agent-picker").is_none()); + } + }) + .unwrap(); +} +fn replace_project(f: &Fixture, source: ProjectSurfaceReceipt, window: &mut Window, cx: &mut App) { + f.workspace.update(cx, |workspace, cx| { + let index = workspace + .items(cx) + .position(|item| Some(item.item_id()) == source.outer_item()) + .unwrap(); + workspace.close_item(index, window, cx); + }); + let nocterm_ai::history::ContextId::Project(project) = source.context() else { + panic!("project fixture") + }; + super::super::seat_binding::project_seat(f, project, window, cx); +} + +#[gpui_kit::test] +fn a_workspace_chat_can_attach_and_open_an_additional_saved_server(cx: &mut TestAppContext) { + let f = fixture(cx); + let directory = install(&f, cx); + let source = project_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + attach(&f, "attach-connection-web", cx); + let server = cx.update(|cx| { + thread.update(cx, |thread, cx| { + let servers = thread.offline_servers(cx); + assert_eq!(servers.len(), 1); + assert_eq!(servers[0].1.id.as_ref(), "web"); + servers[0].0.server_id.clone() + }) + }); + let response = open_server(&f, &thread, &server, cx); + let item = directory.background.borrow()[0]; + cx.update(|cx| { + let workspace = f.workspace.read(cx); + let entries = workspace.context_terminals(source, cx).unwrap(); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].item, item); + assert!(entries[0].background); + assert_eq!( + workspace + .context_terminals(workspace.general_surface_source(cx), cx) + .unwrap() + .len(), + 1 + ); + thread.update(cx, |thread, cx| { + assert!( + thread + .resolved(cx) + .iter() + .any(|(id, _, _)| id == response["terminal"]["id"].as_str().unwrap()) + ); + assert_eq!(thread.background_items(), vec![item]); + }); + }); + assert_eq!(directory.opened.get(), 0); +} + +#[gpui_kit::test] +fn a_workspace_group_attachment_exposes_its_saved_servers(cx: &mut TestAppContext) { + let f = fixture(cx); + install(&f, cx); + project_chat(&f, cx); + attach(&f, "attach-group-prod", cx); + cx.update(|cx| { + let thread = f.panel.read(cx).current().unwrap(); + let ids = thread.update(cx, |thread, cx| { + thread + .offline_servers(cx) + .into_iter() + .map(|(_, summary)| summary.id.to_string()) + .collect::>() + }); + assert_eq!(ids, ["web", "db"]); + }); +} + +#[gpui_kit::test] +fn the_saved_server_arrow_opens_a_tab_inside_its_original_workspace(cx: &mut TestAppContext) { + let f = fixture(cx); + let directory = install(&f, cx); + let source = project_chat(&f, cx); + attach(&f, "open-connection-web", cx); + cx.run_until_parked(); + let item = directory.background.borrow()[0]; + cx.update(|cx| { + let workspace = f.workspace.read(cx); + let entries = workspace.context_terminals(source, cx).unwrap(); + assert_eq!(entries.len(), 1); + assert_eq!(entries[0].item, item); + assert!(!entries[0].background); + assert_eq!(workspace.items(cx).count(), 2); + assert_eq!(workspace.context_source(cx), source); + }); + assert_eq!(directory.opened.get(), 0); +} + +#[gpui_kit::test] +fn an_approved_saved_server_open_cannot_target_a_reopened_workspace(cx: &mut TestAppContext) { + let f = fixture(cx); + let directory = install(&f, cx); + let source = project_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + attach(&f, "attach-connection-web", cx); + let server = cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread.offline_servers(cx)[0].0.server_id.clone() + }) + }); + let mut response = queue_server_open(&f, &thread, &server, cx); + let card = cx.update(|cx| thread.read(cx).tool_cards().remove(0)); + cx.update_window(f.handle, |_, window, cx| { + thread.update(cx, |thread, cx| { + thread.approve_tool(card.token(), true, false, cx) + }); + replace_project(&f, source, window, cx); + }) + .unwrap(); + cx.run_until_parked(); + assert!(response.try_recv().unwrap().expect("answered").is_err()); + assert!(directory.background.borrow().is_empty()); + assert!(directory.accesses.borrow().is_empty()); + cx.update(|cx| { + let workspace = f.workspace.read(cx); + assert_ne!(workspace.context_source(cx), source); + assert_eq!(workspace.selected_context(cx), source.context()); + assert!(workspace.terminals(cx).is_empty()); + assert_eq!( + workspace + .context_terminals(workspace.general_surface_source(cx), cx) + .unwrap() + .len(), + 1 + ); + }); +} + +#[gpui_kit::test] +fn a_saved_server_arrow_cannot_target_a_reopened_workspace(cx: &mut TestAppContext) { + let f = fixture(cx); + let directory = install(&f, cx); + let source = project_chat(&f, cx); + cx.update_window(f.handle, |_, window, cx| { + window.render_frame(cx); + window.click("agent-attach-context", cx); + window.render_frame(cx); + window.click("open-connection-web", cx); + replace_project(&f, source, window, cx); + }) + .unwrap(); + cx.run_until_parked(); + assert!(directory.background.borrow().is_empty()); + assert!(directory.accesses.borrow().is_empty()); + cx.update(|cx| { + let workspace = f.workspace.read(cx); + assert_ne!(workspace.context_source(cx), source); + assert!(workspace.terminals(cx).is_empty()); + }); +} + +#[gpui_kit::test] +fn failed_chat_handoff_keeps_a_workspace_session_the_user_already_promoted( + cx: &mut TestAppContext, +) { + let f = fixture(cx); + let directory = install(&f, cx); + let source = project_chat(&f, cx); + let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); + attach(&f, "attach-connection-web", cx); + let server = cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread.offline_servers(cx)[0].0.server_id.clone() + }) + }); + let weak = Rc::downgrade(&directory); + let owner = thread.clone(); + let seat = cx.update(|cx| thread.read(cx).workspace_seat().clone()); + *directory.after_background_open.borrow_mut() = Some(Box::new(move |window, cx| { + let item = weak.upgrade().unwrap().background.borrow()[0]; + seat.show_background(item, window, cx); + owner.update(cx, |thread, cx| { + thread.attach(Attachment::Connection("web".into()), cx) + }); + })); + let mut response = queue_server_open(&f, &thread, &server, cx); + let card = cx.update(|cx| thread.read(cx).tool_cards().remove(0)); + cx.update(|cx| { + thread.update(cx, |thread, cx| { + thread.approve_tool(card.token(), true, false, cx) + }) + }); + cx.run_until_parked(); + assert!(response.try_recv().unwrap().expect("answered").is_err()); + cx.update(|cx| { + let entries = f.workspace.read(cx).context_terminals(source, cx).unwrap(); + assert_eq!(entries.len(), 1); + assert!(!entries[0].background); + assert_eq!(entries[0].item, directory.background.borrow()[0]); + }); +} diff --git a/crates/agent/src/panel/tests/terminal_fixture.rs b/crates/agent/src/panel/tests/terminal_fixture.rs index a5e422c7..60ddf134 100644 --- a/crates/agent/src/panel/tests/terminal_fixture.rs +++ b/crates/agent/src/panel/tests/terminal_fixture.rs @@ -3,10 +3,13 @@ use super::*; pub(super) struct Access { item: std::cell::Cell>, executor: std::cell::RefCell>>, + source: std::cell::RefCell>, lease: std::cell::RefCell>>, at_prompt: std::cell::Cell, sent: std::cell::RefCell>, profile: std::cell::RefCell>, + /// What `TerminalFiles` reports; unset means the terminal has no file access. + files: std::cell::RefCell>, /// Shared by the sessions of a test. sign_in: Rc, link: Link, @@ -36,15 +39,23 @@ impl Access { Rc::new(Self { item: Default::default(), executor: Default::default(), + source: Default::default(), lease: Default::default(), at_prompt: std::cell::Cell::new(true), sent: Default::default(), profile: std::cell::RefCell::new(profile), + files: Default::default(), sign_in, link: Default::default(), }) } + pub(super) fn bind_source( + &self, + source: nocterm_ai::local_environment::LocalEnvironmentAccess, + ) { + *self.source.borrow_mut() = Some(source); + } pub(super) fn replace_connection(&self) { self.link.epoch.set(self.link.epoch.get() + 1); self.link.closed.set(false); @@ -66,6 +77,21 @@ impl Access { impl TerminalState for Access { fn info(&self, _: &App) -> Option { Some(TerminalInfo { + source: self + .source + .borrow() + .as_ref() + .and_then(|access| match access.environment() { + nocterm_ai::local_environment::LocalEnvironment::HubAccount(partition) => { + Some(partition.clone()) + } + _ => None, + }), + source_current: self + .source + .borrow() + .as_ref() + .is_none_or(|access| access.check_current().is_ok()), expected: ExpectedSession::new(self.item.get()?, self.link.epoch.get()), title: "Terminal".into(), local: !self.link.remote.get(), @@ -146,6 +172,24 @@ impl TerminalExecution for Access { )) } } +impl nocterm_workspace::TerminalFiles for Access { + fn filesystem( + &self, + expected: ExpectedSession, + _: &App, + ) -> Result { + self.check(expected)?; + if self.link.closed.get() || self.sign_in.asks.get() || self.sign_in.vault_locked.get() { + return Err(nocterm_workspace::TerminalError::Unavailable); + } + self.files + .borrow() + .clone() + .ok_or(nocterm_workspace::TerminalError::Unsupported( + nocterm_workspace::TerminalCapability::Files, + )) + } +} impl LiveCommands for Access { fn begin( &self, @@ -245,7 +289,8 @@ impl Item for FakeTerminal { .with_live(self.access.clone()) .with_execution(self.access.clone()) .with_prompts(self.access.clone()) - .with_reconnect(self.access.clone()), + .with_reconnect(self.access.clone()) + .with_files(self.access.clone()), ) } } @@ -268,6 +313,22 @@ impl Access { pub(super) fn profile(&self) -> &std::cell::RefCell> { &self.profile } + /// This computer is the terminal's filesystem. + pub(super) fn serve_local_files(&self) { + *self.files.borrow_mut() = Some(nocterm_workspace::TerminalFilesystem::Local); + } + /// A remote host with its own in-memory filesystem, returned for seeding and assertions. + pub(super) fn serve_remote_files( + &self, + host: nocterm_session::HostAddress, + ) -> Arc { + let fs = Arc::new(nocterm_session::testing::InMemoryFs::default()); + *self.files.borrow_mut() = Some(nocterm_workspace::TerminalFilesystem::Remote { + host, + fs: fs.clone(), + }); + fs + } pub(super) fn sign_in(&self) -> &Rc { &self.sign_in } diff --git a/crates/agent/src/panel/tests/tool_corpus.rs b/crates/agent/src/panel/tests/tool_corpus.rs index ba641ebf..d5cd94ec 100644 --- a/crates/agent/src/panel/tests/tool_corpus.rs +++ b/crates/agent/src/panel/tests/tool_corpus.rs @@ -151,7 +151,7 @@ fn measure(directory: &Path) -> Result, String> { std::fs::read(path).map_err(|_| format!("Cannot read corpus chat at index {index}"))?; let chat: SavedChat = serde_json::from_slice(&bytes) .map_err(|_| format!("Invalid JSON/history schema at corpus index {index}"))?; - let counts = counts.entry(provider(&chat.agent_id)).or_default(); + let counts = counts.entry(provider(&chat.header.agent_id)).or_default(); counts.chats += 1; for entry in &chat.entries { let Entry::Tool(call) = entry else { diff --git a/crates/agent/src/panel/tests/tool_input.rs b/crates/agent/src/panel/tests/tool_input.rs index 9e9b2882..1d7a38d1 100644 --- a/crates/agent/src/panel/tests/tool_input.rs +++ b/crates/agent/src/panel/tests/tool_input.rs @@ -222,7 +222,7 @@ fn settle(f: &Fixture, cx: &mut TestAppContext) { } fn tool_fixture(cx: &mut TestAppContext, title: &str, input: Option) -> Fixture { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); let thread = cx.update(|cx| f.panel.read(cx).current().unwrap()); @@ -608,7 +608,9 @@ fn verified_destination_uses_the_connected_host_and_survives_rename_detach_and_r ); cx.update(|cx| { thread.update(cx, |thread, cx| { - thread.apply_presented_update(SessionUpdate::ToolCall(Box::new(call))); + thread + .apply_presented_update(SessionUpdate::ToolCall(Box::new(call))) + .unwrap(); let (respond, _) = oneshot::channel(); let mut request = BridgeCall { arguments: Some(json!({"terminal_id":"t1","command":"echo snapshot"})), @@ -625,12 +627,14 @@ fn verified_destination_uses_the_connected_host_and_survives_rename_detach_and_r *access.target.borrow_mut() = nocterm_session::Target::new("reconnected", "new.example", 22); cx.update(|cx| { thread.update(cx, |thread, _| { - thread.apply_presented_update(SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( - "snapshot", - ToolChanges::default() - .title("Renamed") - .status(ToolStatus::Succeeded), - )))); + thread + .apply_presented_update(SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + "snapshot", + ToolChanges::default() + .title("Renamed") + .status(ToolStatus::Succeeded), + )))) + .unwrap(); let Entry::Tool(call) = &thread.transcript().entries[0] else { unreachable!() }; @@ -654,7 +658,9 @@ fn verified_destination_uses_the_connected_host_and_survives_rename_detach_and_r ); cx.update(|cx| { thread.update(cx, |thread, cx| { - thread.apply_presented_update(SessionUpdate::ToolCall(Box::new(call))); + thread + .apply_presented_update(SessionUpdate::ToolCall(Box::new(call))) + .unwrap(); let (respond, _) = oneshot::channel(); let mut request = BridgeCall { arguments: Some(json!({"terminal_id":"t1","command":"echo ipv6"})), diff --git a/crates/agent/src/panel/tests/tool_output.rs b/crates/agent/src/panel/tests/tool_output.rs index 89f16419..8013e992 100644 --- a/crates/agent/src/panel/tests/tool_output.rs +++ b/crates/agent/src/panel/tests/tool_output.rs @@ -49,7 +49,7 @@ pub(super) fn set_call(f: &Fixture, call: ToolRecord, cx: &mut TestAppContext) { } pub(super) fn expanded_fixture(call: ToolRecord, cx: &mut TestAppContext) -> Fixture { - let f = fixture_with_width(cx, 18., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 18., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update(|cx| cx.set_reduce_motion(true)); set_call(&f, call, cx); diff --git a/crates/agent/src/panel/tests/usage_dismissal.rs b/crates/agent/src/panel/tests/usage_dismissal.rs index 92af50f1..aaf569da 100644 --- a/crates/agent/src/panel/tests/usage_dismissal.rs +++ b/crates/agent/src/panel/tests/usage_dismissal.rs @@ -115,14 +115,14 @@ fn usage_focus_leaving_the_panel_dismisses_without_stealing_focus(cx: &mut TestA new_chat(&f, cx); open_usage(&f, cx); cx.update_window(f.handle, |_, window, cx| { - let terminal = f.workspace.read(cx).find_item::().unwrap(); + let terminal = f.workspace.read(cx).find_item::(cx).unwrap(); let focus = terminal.read(cx).focus_handle(cx); window.focus(&focus, cx); }) .unwrap(); settle(&f, cx); cx.update_window(f.handle, |_, window, cx| { - let terminal = f.workspace.read(cx).find_item::().unwrap(); + let terminal = f.workspace.read(cx).find_item::(cx).unwrap(); let focus = terminal.read(cx).focus_handle(cx); window.render_frame(cx); assert!(window.try_find("agent-usage").is_none()); @@ -182,7 +182,7 @@ fn usage_trigger_toggle_and_outside_menu_action_do_not_reopen_card(cx: &mut Test #[gpui_kit::test] fn usage_stays_above_a_multiline_composer_in_a_narrow_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 14., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 14., workspace_resolver(), image_deps()); new_chat(&f, cx); cx.update_window(f.handle, |_, window, cx| { replace_composer_value( @@ -266,7 +266,7 @@ fn assert_original_usage_bounds(window: &Window) { #[gpui_kit::test] fn usage_retains_original_full_chat_width_in_a_wide_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 40., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 40., workspace_resolver(), image_deps()); new_chat(&f, cx); open_usage(&f, cx); cx.update_window(f.handle, |_, window, _| { @@ -277,7 +277,7 @@ fn usage_retains_original_full_chat_width_in_a_wide_panel(cx: &mut TestAppContex #[gpui_kit::test] fn usage_retains_original_full_chat_width_in_a_narrow_panel(cx: &mut TestAppContext) { - let f = fixture_with_width(cx, 14., usage_source(), workspace_resolver(), image_deps()); + let f = fixture_with_width(cx, 14., workspace_resolver(), image_deps()); new_chat(&f, cx); open_usage(&f, cx); cx.update_window(f.handle, |_, window, _| { @@ -292,7 +292,7 @@ fn usage_original_bounds_follow_window_resize(cx: &mut TestAppContext) { new_chat(&f, cx); cx.update(|cx| { f.workspace.update(cx, |workspace, cx| { - workspace.set_right_panel_maximized(true, cx) + workspace.set_panel_expanded(&crate::plugin::CHATS.id, true, cx) }); }); settle(&f, cx); diff --git a/crates/agent/src/panel/widgets.rs b/crates/agent/src/panel/widgets.rs index 12c8d2d4..5045e9e7 100644 --- a/crates/agent/src/panel/widgets.rs +++ b/crates/agent/src/panel/widgets.rs @@ -174,10 +174,18 @@ pub(super) fn activity_text( } } pub(super) fn chat_markdown(id: impl Into, text: String) -> TextView { + chat_text(TextView::markdown(id, text)) +} +/// [`chat_markdown`] drawing a row's kept, already parsed state. +pub(super) fn chat_markdown_in( + state: &gpui_kit::Entity, +) -> TextView { + chat_text(TextView::new(state)) +} +fn chat_text(view: TextView) -> TextView { let mut code_block = gpui_kit::StyleRefinement::default().text_sm(); code_block.overflow.x = Some(gpui_kit::Overflow::Scroll); - TextView::markdown(id, text) - .selectable(true) + view.selectable(true) .text_sm() .min_w_0() .style(TextViewStyle { diff --git a/crates/agent/src/plugin.rs b/crates/agent/src/plugin.rs new file mode 100644 index 00000000..f877fd61 --- /dev/null +++ b/crates/agent/src/plugin.rs @@ -0,0 +1,39 @@ +//! The agent chats as a required system plugin: one expandable side panel. +use nocterm_plugin_model::{ + AutoPlace, ContributionId, ContributionKind, Icon, PanelChrome, PanelTraits, Placement, + PluginId, SettingsSchema, SystemContribution, SystemPlugin, WidthClass, Zone, +}; + +/// The agent chats in the side panel, which draws its own header. +pub const CHATS: SystemContribution = SystemContribution { + id: ContributionId::new("agent.chats"), + kind: ContributionKind::Panel, + title: "AI Agents", + icon: Icon::PanelRight, + placement: Placement { + zone: Zone::Right, + order: 40, + group: Some("assistant"), + auto: AutoPlace::Show, + }, + panel: Some(PanelTraits { + width: WidthClass::Assistant, + chrome: PanelChrome::OwnHeader, + }), + preview: None, + edge: None, + pinned: false, +}; + +/// AI Agents: chats with coding agents. It cannot be disabled; the master switch in the AI +/// settings withdraws its panel instead. Its settings page is the top-level AI page. +pub static PLUGIN: SystemPlugin = SystemPlugin { + id: PluginId::new("agent"), + name: "AI Agents", + description: "Chats with coding agents in the side panel", + required: true, + contributions: &[CHATS], + settings_section: None, + settings: SettingsSchema::NONE, + settings_page: Some("ai"), +}; diff --git a/crates/agent/src/workspace_seat.rs b/crates/agent/src/workspace_seat.rs index 63d4cdbe..2f7d9c95 100644 --- a/crates/agent/src/workspace_seat.rs +++ b/crates/agent/src/workspace_seat.rs @@ -1,51 +1,133 @@ -//! The [`ChatWorkspace`] seat over the panel's workspace. +//! An immutable original surface and exact document, independent of visible tabs. use gpui_kit::{App, EntityId, Subscription, WeakEntity, Window}; -use nocterm_agent_chat::seat::ChatWorkspace; -use nocterm_workspace::{ConnectionSummary, ExpectedSession, TerminalEntry, Workspace}; - -/// The workspace a panel is attached to, held weakly. -pub(crate) struct WorkspaceSeat(WeakEntity); +use nocterm_agent_chat::seat::{ChatWorkspace, Seat}; +use nocterm_ai::{ + execution_origin::ChatKey, + history::ContextId, + storage::{AgentStorageError, require_chat_scope}, +}; +use nocterm_workspace::{ + ConnectionSummary, ExpectedSession, ProjectSurfaceReceipt, TerminalEntry, Workspace, +}; +use std::rc::Rc; +pub(crate) struct WorkspaceSeat { + workspace: WeakEntity, + source: ProjectSurfaceReceipt, + key: Option, + runtime: Option, +} impl WorkspaceSeat { - pub(crate) fn new(workspace: WeakEntity) -> Self { - Self(workspace) + pub(crate) fn new(workspace: WeakEntity, source: ProjectSurfaceReceipt) -> Self { + Self { + workspace, + source, + key: None, + runtime: None, + } + } + pub(crate) fn with_runtime(mut self, runtime: crate::runtime::RuntimeHandle) -> Self { + self.runtime = Some(runtime); + self + } + fn current(&self) -> bool { + self.runtime + .as_ref() + .is_none_or(|runtime| runtime.check_current().is_ok()) + } + fn permits(&self, workspace: &Workspace, item: EntityId, cx: &App) -> bool { + workspace + .context_terminals(self.source, cx) + .is_ok_and(|entries| entries.iter().any(|entry| entry.item == item)) } } - -/// The connection `item` currently runs, as the workspace places it. -fn current(workspace: &Workspace, item: EntityId, cx: &App) -> Option { - workspace - .terminals(cx) - .into_iter() - .find(|entry| entry.item == item) - .and_then(|entry| entry.capabilities.state().info(cx)) - .map(|info| info.expected) -} - impl ChatWorkspace for WorkspaceSeat { + fn for_chat(&self, key: &ChatKey) -> Result { + require_chat_scope(self.source.context(), key.scope())?; + let environment = match &self.runtime { + Some(runtime) => runtime.environment()?.clone(), + None => Default::default(), + }; + nocterm_ai::storage::require_local_environment(&environment, key.environment())?; + if let Some(runtime) = &self.runtime { + runtime.check_current()?; + } + if self.key.as_ref().is_some_and(|original| original != key) { + return Err(nocterm_ai::history::HistoryCodecError::IdentityMismatch.into()); + } + Ok(Rc::new(Self { + workspace: self.workspace.clone(), + source: self.source, + key: Some(key.clone()), + runtime: self.runtime.clone(), + })) + } fn id(&self) -> EntityId { - self.0.entity_id() + self.workspace.entity_id() } fn is_open(&self) -> bool { - self.0.upgrade().is_some() + self.current() && self.workspace.upgrade().is_some() } fn terminals(&self, cx: &App) -> Option> { - Some(self.0.upgrade()?.read(cx).terminals(cx)) + if !self.current() { + return None; + } + let workspace = self.workspace.upgrade()?; + let workspace = workspace.read(cx); + let expected = self + .runtime + .as_ref() + .map(|runtime| runtime.environment().ok().cloned()) + .unwrap_or(Some(Default::default()))?; + Some( + workspace + .context_terminals(self.source, cx) + .ok()? + .into_iter() + .filter(|entry| { + entry.capabilities.state().info(cx).is_some_and(|info| { + info.source_current + && match (&expected, &info.source) { + ( + nocterm_ai::local_environment::LocalEnvironment::ThisComputer, + None, + ) => true, + ( + nocterm_ai::local_environment::LocalEnvironment::HubAccount( + expected, + ), + Some(actual), + ) => expected == actual, + _ => false, + } + }) + }) + .collect(), + ) } fn connections(&self, cx: &App) -> Vec { - self.0 + if self.terminals(cx).is_none() { + return Vec::new(); + } + self.workspace .upgrade() .and_then(|workspace| workspace.read(cx).connection_directory()) .map(|directory| directory.connections(cx)) .unwrap_or_default() } fn opens_connections(&self, cx: &App) -> bool { - self.0 - .upgrade() - .is_some_and(|workspace| workspace.read(cx).connection_directory().is_some()) + self.terminals(cx).is_some() + && self.workspace.upgrade().is_some_and(|workspace| { + let workspace = workspace.read(cx); + if self.source.context() == ContextId::General { + workspace.connection_directory().is_some() + } else { + workspace.chat_connection_opener().is_some() + } + }) } fn observe(&self, changed: Box, cx: &mut App) -> Option { - let workspace = self.0.upgrade()?; + let workspace = self.workspace.upgrade()?; Some(cx.subscribe(&workspace, move |_, _, cx| changed(cx))) } fn open_background( @@ -54,17 +136,35 @@ impl ChatWorkspace for WorkspaceSeat { window: &mut Window, cx: &mut App, ) -> Option { - let directory = self.0.upgrade()?.read(cx).connection_directory()?; - directory.open_background(profile, &self.0, window, cx) + if !self.current() { + return None; + } + let owner = self.workspace.upgrade()?; + if self.source.context() == ContextId::General { + let directory = owner.read(cx).connection_directory()?; + return directory.open_background(profile, &self.workspace, window, cx); + } + let opener = owner.read(cx).chat_connection_opener()?; + match opener.open(profile, self.source, &self.workspace, window, cx) { + Ok(item) => Some(item), + Err(error) => { + tracing::warn!(%error, "could not open chat's original workspace server"); + None + } + } } fn show_background(&self, item: EntityId, window: &mut Window, cx: &mut App) { - let _ = self.0.update(cx, |workspace, cx| { - workspace.show_background_session(item, window, cx) + let _ = self.workspace.update(cx, |workspace, cx| { + if self.permits(workspace, item, cx) { + let _ = workspace.show_context_background(self.source, item, window, cx); + } }); } fn close_background(&self, item: EntityId, window: &mut Window, cx: &mut App) { - let _ = self.0.update(cx, |workspace, cx| { - workspace.close_background_session(item, window, cx) + let _ = self.workspace.update(cx, |workspace, cx| { + if self.permits(workspace, item, cx) { + let _ = workspace.close_context_background(self.source, item, window, cx); + } }); } fn close_if_original( @@ -74,13 +174,33 @@ impl ChatWorkspace for WorkspaceSeat { window: &mut Window, cx: &mut App, ) -> bool { - self.0 + self.workspace .update(cx, |workspace, cx| { - if !workspace.is_background(item) || current(workspace, item, cx) != expected { + if !self.permits(workspace, item, cx) { + return false; + } + let entry = workspace + .context_terminals(self.source, cx) + .ok() + .and_then(|entries| { + entries + .into_iter() + .find(|entry| entry.item == item && entry.background) + }); + let Some(entry) = entry else { + return false; + }; + let current = entry + .capabilities + .state() + .info(cx) + .map(|info| info.expected); + if current != expected { return false; } - workspace.close_background_session(item, window, cx); - true + workspace + .close_context_background(self.source, item, window, cx) + .is_ok() }) .unwrap_or(false) } diff --git a/crates/ai/Cargo.toml b/crates/ai/Cargo.toml index 1b7855cc..34db92af 100644 --- a/crates/ai/Cargo.toml +++ b/crates/ai/Cargo.toml @@ -9,8 +9,13 @@ layer = "domain" [features] test-support = [] [dependencies] +sha2.workspace = true +nocterm-hubs-model.workspace = true nocterm-settings.workspace = true nocterm-core.workspace = true +nocterm-transfers.workspace = true +nocterm-project-model.workspace = true +nocterm-session.workspace = true serde.workspace = true serde_json = { workspace = true, features = ["preserve_order"] } serde_with.workspace = true @@ -23,6 +28,8 @@ toml.workspace = true thiserror.workspace = true uuid = { version = "1", default-features = false } [dev-dependencies] +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } tempfile.workspace = true [lints] workspace = true diff --git a/crates/ai/src/agents.rs b/crates/ai/src/agents.rs index a743bee4..7451288a 100644 --- a/crates/ai/src/agents.rs +++ b/crates/ai/src/agents.rs @@ -16,5 +16,5 @@ mod tests; pub use available::{AgentConfigWarning, AgentSource, AvailableAgent, AvailableAgents}; pub use catalog::{AgentCatalog, CatalogError}; -pub use launch::{AgentLaunch, valid_id}; -pub use profile::{AccountUsage, AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; +pub use launch::{AgentLaunch, HUB_AGENT_PREFIX, HubAgentRef, is_hub_agent, valid_id}; +pub use profile::{AgentMark, AgentProfile, LaunchDefaults, UsageMeta, UsageProfile}; diff --git a/crates/ai/src/agents/available.rs b/crates/ai/src/agents/available.rs index ccff1ea2..0e33196e 100644 --- a/crates/ai/src/agents/available.rs +++ b/crates/ai/src/agents/available.rs @@ -9,6 +9,8 @@ pub enum AgentSource { Builtin, /// A command the user added in settings. Custom, + /// An agent of a hub the user is signed in to; it runs there. + Hub, } /// An agent configuration that was skipped; the agent is not offered. @@ -45,6 +47,10 @@ impl AvailableAgent { pub fn profile(&self) -> Option<&AgentProfile> { self.profile.as_ref() } + /// The hub whose agent this is; `None` for an agent of this computer. + pub fn hub(&self) -> Option<&nocterm_hubs_model::HubId> { + self.launch.hub.as_ref().map(|agent| &agent.hub) + } } /// The agents that can be started under the current settings, by id. @@ -88,10 +94,35 @@ impl AvailableAgents { Self { agents, warnings } } + /// These agents and the hub agents of `launches` (launches without a hub are ignored). + /// A hub agent never replaces an agent of this computer. + #[must_use] + pub fn with_hub_agents(mut self, launches: impl IntoIterator) -> Self { + for launch in launches.into_iter().filter(|launch| launch.hub.is_some()) { + self.agents + .entry(launch.id.clone()) + .or_insert(AvailableAgent { + launch, + source: AgentSource::Hub, + profile: None, + }); + } + self + } + pub fn get(&self, id: &str) -> Option<&AvailableAgent> { self.agents.get(id) } + /// The agents a window offers in its new-chat menu: every agent of this computer, and + /// the agents of the window's `hub` only. A window of this computer offers no hub agent. + #[must_use] + pub fn offered_in(mut self, hub: Option<&nocterm_hubs_model::HubId>) -> Self { + self.agents + .retain(|_, agent| agent.hub().is_none() || agent.hub() == hub); + self + } + /// Agents in id order. pub fn iter(&self) -> impl Iterator { self.agents.values() @@ -139,6 +170,7 @@ fn apply( args: Vec::new(), env: BTreeMap::new(), inherit_env: Vec::new(), + hub: None, }; (launch, AgentSource::Custom) }); diff --git a/crates/ai/src/agents/launch.rs b/crates/ai/src/agents/launch.rs index 7c565daa..2e268b05 100644 --- a/crates/ai/src/agents/launch.rs +++ b/crates/ai/src/agents/launch.rs @@ -1,6 +1,7 @@ +use nocterm_hubs_model::HubId; use std::collections::BTreeMap; -/// How to start one agent process. +/// How to start one agent: a local process, or an agent of a hub. #[derive(Clone, Debug, PartialEq, Eq, Hash)] pub struct AgentLaunch { pub id: String, @@ -9,9 +10,34 @@ pub struct AgentLaunch { pub args: Vec, pub env: BTreeMap, pub inherit_env: Vec, + /// The hub agent this launch reaches instead of a process; the process fields are empty. + pub hub: Option, +} + +/// An agent that runs on a hub: its chats live there and keep running while no client is +/// open. The hub connector reaches it; no process starts on this computer. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct HubAgentRef { + pub hub: HubId, + /// The hub's identifier of the agent. + pub agent: String, } impl AgentLaunch { + /// The launch of agent `agent` of `hub`, named `name`. Its id is stable for the pair, so a + /// saved chat finds its agent again. + pub fn hub_agent(hub: HubId, agent: String, name: String) -> Self { + Self { + id: hub_agent_id(&hub, &agent), + name, + command: String::new(), + args: Vec::new(), + env: BTreeMap::new(), + inherit_env: Vec::new(), + hub: Some(HubAgentRef { hub, agent }), + } + } + /// Whether the command or an argument contains `marker`, such as the /// package name of an ACP adapter. pub fn mentions(&self, marker: &str) -> bool { @@ -21,6 +47,37 @@ impl AgentLaunch { } } +/// How the ids of hub agents begin. +pub const HUB_AGENT_PREFIX: &str = "hub-"; + +/// Whether `id` names a hub agent ([`AgentLaunch::hub_agent`]): its chats run on the hub. +pub fn is_hub_agent(id: &str) -> bool { + id.strip_prefix(HUB_AGENT_PREFIX).is_some_and(|hash| { + hash.len() == 24 + && hash + .bytes() + .all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f')) + }) +} + +/// [`HUB_AGENT_PREFIX`] and 24 hex digits of a hash of the pair: a valid agent id of at most +/// 64 characters. +fn hub_agent_id(hub: &HubId, agent: &str) -> String { + use sha2::{Digest as _, Sha256}; + let mut hash = Sha256::new(); + for part in [hub.to_string().as_bytes(), agent.as_bytes()] { + hash.update((part.len() as u64).to_be_bytes()); + hash.update(part); + } + let digest = hash.finalize(); + let mut id = String::from(HUB_AGENT_PREFIX); + for byte in &digest[..12] { + use std::fmt::Write as _; + let _ = write!(id, "{byte:02x}"); + } + id +} + /// Agent ids use letters, numbers, underscore or dash, up to 64 characters. pub fn valid_id(id: &str) -> bool { !id.is_empty() diff --git a/crates/ai/src/agents/profile.rs b/crates/ai/src/agents/profile.rs index ffeae8f7..f67d6248 100644 --- a/crates/ai/src/agents/profile.rs +++ b/crates/ai/src/agents/profile.rs @@ -44,31 +44,17 @@ pub enum UsageMeta { RateLimitRecord { key: &'static str }, } -/// How nocterm reads an agent's plan limits outside the protocol. -#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] -pub enum AccountUsage { - /// Limits arrive with usage updates only. - #[default] - None, - /// The agent keeps an OAuth sign-in whose account reports limits. - OAuthAccount, - /// The agent logs limits to session files in its home. - SessionLog, -} - /// How an agent reports plan limits, and what to say while none are known. #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub struct UsageProfile { meta: UsageMeta, - account: AccountUsage, empty_hint: Option<&'static str>, } impl UsageProfile { - pub const fn new(meta: UsageMeta, account: AccountUsage) -> Self { + pub const fn new(meta: UsageMeta) -> Self { Self { meta, - account, empty_hint: None, } } @@ -80,9 +66,6 @@ impl UsageProfile { pub fn meta(&self) -> UsageMeta { self.meta } - pub fn account(&self) -> AccountUsage { - self.account - } pub fn empty_hint(&self) -> Option<&'static str> { self.empty_hint } @@ -141,7 +124,7 @@ impl AgentProfile { launch, markers: &[], state: &[], - usage: UsageProfile::new(UsageMeta::None, AccountUsage::None), + usage: UsageProfile::new(UsageMeta::None), mark: None, } } @@ -207,6 +190,7 @@ impl AgentProfile { .iter() .map(|&name| name.into()) .collect(), + hub: None, } } } diff --git a/crates/ai/src/agents/tests.rs b/crates/ai/src/agents/tests.rs index 8c3f8534..30a66cf3 100644 --- a/crates/ai/src/agents/tests.rs +++ b/crates/ai/src/agents/tests.rs @@ -13,13 +13,10 @@ const ALPHA: AgentProfile = AgentProfile::new( .recognized_by(&["alpha-acp"]) .writes_home(&[".alpha", ".config/alpha"]) .with_usage( - UsageProfile::new( - UsageMeta::RateLimitRecord { - key: "_alpha/rateLimit", - }, - AccountUsage::OAuthAccount, - ) - .with_empty_hint("Alpha limits load from your sign-in."), + UsageProfile::new(UsageMeta::RateLimitRecord { + key: "_alpha/rateLimit", + }) + .with_empty_hint("Alpha has not reported plan limits through ACP."), ); const BETA: AgentProfile = AgentProfile::new("beta", "Beta", LaunchDefaults::new("beta", &["acp"], &[])); @@ -195,3 +192,34 @@ fn catalog_finds_profiles_by_id_then_by_marker() { [".alpha", ".config/alpha"] ); } + +#[test] +fn a_hub_window_offers_its_hub_agents_and_a_local_window_none() { + use nocterm_hubs_model::{HubAddress, HubId}; + let hub = |text: &str| HubId::from(HubAddress::parse(text).unwrap()); + let (work, home) = (hub("work.example:7443"), hub("home.example:7443")); + let agents = AvailableAgents::resolve(&catalog(), &AiSettings::default()).with_hub_agents([ + AgentLaunch::hub_agent(work.clone(), "a1".into(), "Ops".into()), + AgentLaunch::hub_agent(home.clone(), "a1".into(), "Ops".into()), + ]); + let names = |hub: Option<&HubId>| { + agents + .clone() + .offered_in(hub) + .iter() + .map(|agent| (agent.name().to_owned(), agent.hub().cloned())) + .collect::>() + }; + let local = names(None).len(); + let offered = names(Some(&work)); + assert_eq!(offered.len(), local + 1); + assert!(offered.contains(&("Ops".into(), Some(work.clone())))); + let offered = agents.clone().offered_in(Some(&work)); + let ops = offered.iter().find(|agent| agent.hub().is_some()).unwrap(); + assert_eq!(ops.source(), AgentSource::Hub); + assert!(super::valid_id(ops.id())); + assert_ne!( + AgentLaunch::hub_agent(work, "a1".into(), "x".into()).id, + AgentLaunch::hub_agent(home, "a1".into(), "x".into()).id + ); +} diff --git a/crates/ai/src/approval.rs b/crates/ai/src/approval.rs index c8d71380..b396fda5 100644 --- a/crates/ai/src/approval.rs +++ b/crates/ai/src/approval.rs @@ -4,27 +4,64 @@ use std::collections::BTreeSet; #[derive(Default)] pub struct ApprovalGrants { grants: BTreeSet<(String, ToolCapability)>, + /// Transfer grants belong to an unordered pair of terminals, so approving + /// `a`/`b` and `c`/`d` does not approve `a`/`d`. A same-terminal transfer is `(a, a)`. + transfer_pairs: BTreeSet<(String, String)>, +} +/// The unordered terminal pair a transfer moves files between. +fn transfer_pair(call: &TerminalCall) -> Option<(String, String)> { + let (source, destination) = (call.source_terminal_id()?, call.terminal_id()?); + let (first, second) = if source <= destination { + (source, destination) + } else { + (destination, source) + }; + Some((first.into(), second.into())) } impl ApprovalGrants { pub fn grant(&mut self, id: &str, capability: ToolCapability) { self.grants.insert((id.into(), capability)); } + /// Records what a card approval of `call` covers: a transfer grants its terminal + /// pair, every other call grants its capability on each terminal it touches. + pub fn grant_call(&mut self, call: &TerminalCall) { + if let Some(pair) = transfer_pair(call) { + self.transfer_pairs.insert(pair); + return; + } + for id in call.terminals().chain(call.server_id()) { + self.grant(id, call.capability()); + } + } pub fn revoke(&mut self, id: &str) { self.grants.retain(|(target, _)| target != id); + self.transfer_pairs + .retain(|(first, second)| first != id && second != id); } pub fn clear(&mut self) { self.grants.clear(); + self.transfer_pairs.clear(); } + /// A call needs a card unless the policy allows it or every terminal it + /// touches already holds the capability it needs. pub fn requires_approval(&self, call: &TerminalCall, settings: &ApprovalSettings) -> bool { - let Some(id) = call.target() else { + let mut targets = call.terminals().peekable(); + let server = call.server_id(); + if targets.peek().is_none() && server.is_none() { return false; - }; - if call.writes() { - settings.terminal_write == ApprovalPolicy::Ask - && !self.grants.contains(&(id.into(), call.capability())) - } else { - settings.terminal_read == ApprovalPolicy::Ask - && !self.grants.contains(&(id.into(), call.capability())) } + let policy = if call.writes() { + settings.terminal_write + } else { + settings.terminal_read + }; + let capability = call.capability(); + let covered = match transfer_pair(call) { + Some(pair) => self.transfer_pairs.contains(&pair), + None => targets + .chain(server) + .all(|id| self.grants.contains(&(id.into(), capability))), + }; + policy == ApprovalPolicy::Ask && !covered } } diff --git a/crates/ai/src/connection.rs b/crates/ai/src/connection.rs index 2398acb8..6bfc707f 100644 --- a/crates/ai/src/connection.rs +++ b/crates/ai/src/connection.rs @@ -195,8 +195,15 @@ pub enum AgentError { Io(String), } pub type ConnectError = AgentError; + #[derive(Debug, thiserror::Error)] pub enum BridgeRegistrationError { + #[error("The original account source is unavailable: {0}")] + AccountSource(#[from] nocterm_hubs_model::source_identity::LocalAccountSourceError), + #[error("{0}")] + Origin(#[from] crate::execution_origin::ExecutionOriginError), + #[error("The chat is no longer open.")] + ClientClosed, #[error("AI is disabled.")] AiDisabled, #[error("Bridge error: {0}")] @@ -225,6 +232,7 @@ pub struct BridgeRegistration { pub id: u64, launch: BridgeLaunch, revoke: Option>, + offer: Option>, } impl BridgeRegistration { pub fn new(id: u64, launch: BridgeLaunch, revoke: Arc) -> Self { @@ -232,6 +240,19 @@ impl BridgeRegistration { id, launch, revoke: Some(revoke), + offer: None, + } + } + /// A registration whose bridge lists the tools a chat offers, and tells its agent when they + /// change. + pub fn with_offer(mut self, offer: Arc) -> Self { + self.offer = Some(offer); + self + } + /// What the chat offers its agent now. Offering the same again changes nothing. + pub fn offer(&self, offer: crate::ToolOffer) { + if let Some(set) = &self.offer { + set(self.id, offer); } } pub fn launch(&self) -> &BridgeLaunch { diff --git a/crates/ai/src/context.rs b/crates/ai/src/context.rs index 7f2ce641..1c505700 100644 --- a/crates/ai/src/context.rs +++ b/crates/ai/src/context.rs @@ -21,6 +21,9 @@ pub struct TerminalDescriptor { pub connection: Option, pub cwd: Option, pub status: String, + /// The terminal serves files but runs no programs: the file tools reach its files. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub file_tools: bool, } /// A saved server attached to a chat that has no open session. An agent /// opens one in the background with the `open_terminal` tool. @@ -112,14 +115,23 @@ pub fn context_block(terminals: &[TerminalDescriptor], servers: &[ServerDescript server }) .collect(); - let json = if servers.is_empty() { + let files = safe.iter().any(|terminal| terminal.file_tools); + let json = if servers.is_empty() && !files { serde_json::to_string(&safe) } else { - serde_json::to_string(&serde_json::json!({ - "terminals": safe, - "offline_servers": servers, - "note": "Offline servers have no session yet. Call open_terminal with a server_id to connect in the background, then use the returned terminal_id.", - })) + let mut context = serde_json::json!({ "terminals": safe }); + if !servers.is_empty() { + context["offline_servers"] = serde_json::json!(servers); + context["note"] = serde_json::json!( + "Offline servers have no session yet. Call open_terminal with a server_id to connect in the background, then use the returned terminal_id." + ); + } + if files { + context["file_tools_note"] = serde_json::json!( + "Terminals with file_tools: true serve files but run no programs. Read and change their files with list_files, read_file, edit_file, write_file, make_directory, move_path and delete_path; those tools reach no other terminal." + ); + } + serde_json::to_string(&context) } .expect("descriptors serialize"); // Preserve JSON while escaping delimiters embedded in user-editable metadata. diff --git a/crates/ai/src/execution_origin.rs b/crates/ai/src/execution_origin.rs new file mode 100644 index 00000000..947574af --- /dev/null +++ b/crates/ai/src/execution_origin.rs @@ -0,0 +1,99 @@ +//! Immutable document and provider-resume facts captured before asynchronous work. +//! Owns: scoped chat keys and descriptive execution origins. No native I/O, +//! execution capability, provider preparation, views or current-selection lookup. +//! Ports provided: typed immutable scope validation. Settings/globals: none. +//! Invariant: conversation membership never grants workspace tool authority. +mod error; +use crate::{ + history::ContextId, + local_environment::{ChatScope, LocalEnvironment}, +}; +pub use error::ExecutionOriginError; +use std::{ + path::{Path, PathBuf}, + sync::Arc, +}; + +/// Descriptive document identity. Storage validates the ID at its boundary; +/// possessing a key never grants access to a native resource or process. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct ChatKey { + scope: ContextId, + environment: Arc, + id: String, +} +impl ChatKey { + pub fn new(scope: ContextId, id: String) -> Self { + Self { + scope, + id, + environment: Arc::new(LocalEnvironment::ThisComputer), + } + } + pub fn from_header(header: &crate::history::SavedHeader) -> Self { + Self::in_environment(header.chat_scope(), header.id.clone()) + } + pub fn from_summary(summary: &crate::history::ChatSummary) -> Self { + Self::in_environment(summary.chat_scope(), summary.id.clone()) + } + pub fn in_environment(scope: ChatScope, id: String) -> Self { + Self { + scope: scope.context(), + environment: Arc::new(scope.environment().clone()), + id, + } + } + pub fn environment(&self) -> &LocalEnvironment { + &self.environment + } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment().clone(), self.scope) + } + pub fn scope(&self) -> ContextId { + self.scope + } + pub fn id(&self) -> &str { + &self.id + } +} + +/// Saved local provider cwd is a candidate for the injected WorkdirResolver. +/// It describes resume behavior and does not grant workspace tool access. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct ExecutionOrigin { + key: ChatKey, + provider_resume: Option, +} +impl ExecutionOrigin { + pub fn new(key: ChatKey, provider_resume: Option) -> Self { + Self { + key, + provider_resume, + } + } + pub fn key(&self) -> &ChatKey { + &self.key + } + pub fn scope(&self) -> ContextId { + self.key.scope() + } + pub fn provider_resume(&self) -> Option<&Path> { + self.provider_resume.as_deref() + } + pub fn require_chat_scope(&self, expected: &ChatScope) -> Result<(), ExecutionOriginError> { + if self.key.environment() != expected.environment() { + return Err(ExecutionOriginError::EnvironmentMismatch); + } + self.require_scope(expected.context()) + } + pub fn require_scope(&self, expected: ContextId) -> Result<(), ExecutionOriginError> { + if self.scope() == expected { + Ok(()) + } else { + Err(ExecutionOriginError::ScopeMismatch { + expected, + actual: self.scope(), + }) + } + } +} diff --git a/crates/ai/src/execution_origin/error.rs b/crates/ai/src/execution_origin/error.rs new file mode 100644 index 00000000..2fff6bc8 --- /dev/null +++ b/crates/ai/src/execution_origin/error.rs @@ -0,0 +1,10 @@ +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum ExecutionOriginError { + #[error("Execution origin belongs to another original environment")] + EnvironmentMismatch, + #[error("Execution origin belongs to {actual:?}, but the consumer expects {expected:?}")] + ScopeMismatch { + expected: crate::history::ContextId, + actual: crate::history::ContextId, + }, +} diff --git a/crates/ai/src/history.rs b/crates/ai/src/history.rs index 8bfa27ea..339152b1 100644 --- a/crates/ai/src/history.rs +++ b/crates/ai/src/history.rs @@ -1,4 +1,6 @@ -//! Saved chats: one JSON file per chat in a private directory. +//! Pure saved-chat formats: byte-stable v1 and explicitly scoped v2 envelopes. +//! V2 starts with a canonical catalog header bounded to 16 KiB; full hydration +//! checks its projection against the payload. Storage supplies scope/path authority. //! //! A chat keeps what the panel showed — the user's messages, the agent's //! replies and tool calls — and the agent session it belonged to, so it can be @@ -16,21 +18,35 @@ mod budget; mod catalog; mod codec; mod error; -pub use codec::{decode, encode, encode_shared, prepare, structural_bytes}; +pub use codec::{decode, encode, encode_shared, prepare, prepare_owned, structural_bytes}; pub use error::HistoryCodecError; mod decode; mod loading; mod preflight; pub use catalog::{ChatSummary, FileStamp, decode_summary}; pub use loading::{HistoryGate, HistoryPermit}; +mod catalog_header; +mod header; +mod incoming; +mod prefix; mod size; +mod version; mod wire; pub use budget::{HistoryBudget, PreparedBudget}; -const VERSION: u32 = 1; +pub use catalog_header::CatalogHeader; +pub use header::SavedHeader; +pub use nocterm_project_model::ContextId; +pub use prefix::{MAX_HEADER_BYTES, decode_catalog_header}; +pub use version::TranscriptVersion; +const VERSION: u32 = 2; +const ENVIRONMENT_VERSION: u32 = 3; +const LEGACY_VERSION: u32 = 1; /// Images larger than this are left out of saved chats. const MAX_SAVED_IMAGE_BYTES: usize = 512 * 1024; +const OMITTED_IMAGE_TEXT: &str = "[Image not kept in history]"; /// Only the most recent entries of a very long chat are kept. -const MAX_SAVED_ENTRIES: usize = 2_000; +/// Maximum persisted transcript rows, shared by pre-copy admission. +pub const MAX_SAVED_ENTRIES: usize = 2_000; /// Larger files are not read back. pub const MAX_FILE_BYTES: u64 = 32 * 1024 * 1024; /// At most this many chats are restored, newest first. @@ -59,85 +75,60 @@ pub struct SavedPrompt { pub attachments: Vec, } -/// One saved chat. -#[derive(Clone, Debug, Serialize, Deserialize)] +/// One saved chat. Its identity and catalog metadata share one header. +#[derive(Clone, Debug)] pub struct SavedChat { pub version: u32, - /// Names the file; a UUID. - pub id: String, - /// The registry id of the agent the chat was held with. - pub agent_id: String, - /// The title the agent gave the chat. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub title: Option, - /// The name the user gave the chat, over the agent's title. - #[serde(default, skip_serializing_if = "Option::is_none")] - pub name: Option, - /// Pinned chats are listed first and always restored. - #[serde(default, skip_serializing_if = "std::ops::Not::not")] - pub pinned: bool, + pub header: SavedHeader, /// The agent's session, for resuming it. - #[serde(default, skip_serializing_if = "Option::is_none")] pub session_id: Option, - /// The directory the session was opened in; resuming needs the same one. - #[serde(default, skip_serializing_if = "Option::is_none")] + /// The provider's local resume directory, not project root authority. pub workdir: Option, - /// The model of the last prompt. - #[serde(default, skip_serializing_if = "Option::is_none")] pub model: Option, - /// The configuration the chat last had (model, effort, mode, …) by - /// option id; given to every session the chat opens. - #[serde(default, skip_serializing_if = "std::collections::BTreeMap::is_empty")] pub config: crate::session_config::Choices, - /// The mode the chat last had, for agents that report modes apart from - /// their configuration options. - #[serde(default, skip_serializing_if = "Option::is_none")] pub mode: Option, - /// Saved transcript still needs delivery to the current agent session. - #[serde(default, skip_serializing_if = "std::ops::Not::not")] pub pending_history: bool, - /// Unsent composer text, preserved literally and kept out of the transcript. - #[serde(default, skip_serializing_if = "Option::is_none")] + /// Unsent composer text, preserved literally outside the transcript. pub draft: Option, - /// Seconds since the Unix epoch. - pub updated: u64, - #[serde(deserialize_with = "decode::entries")] pub entries: Vec, - #[serde( - default, - skip_serializing_if = "Vec::is_empty", - deserialize_with = "decode::prompts" - )] pub queue: Vec, - #[serde( - default, - skip_serializing_if = "Vec::is_empty", - deserialize_with = "decode::attachments" - )] pub attachments: Vec, /// When each entry began, aligned with `entries`. - #[serde( - default, - skip_serializing_if = "Vec::is_empty", - deserialize_with = "decode::times" - )] pub times: Vec, - #[serde(skip)] prepared: Option, - #[serde(skip)] archived_draft: bool, } +impl Serialize for SavedChat { + fn serialize(&self, serializer: S) -> Result { + wire::Wire::complete(self).serialize(serializer) + } +} +impl<'de> Deserialize<'de> for SavedChat { + fn deserialize>(deserializer: D) -> Result { + incoming::deserialize(deserializer) + } +} impl SavedChat { /// An empty framing value for an already allocated identity and supplied timestamp. pub fn for_identity(agent_id: String, id: String, updated: u64) -> Self { + Self::for_scope( + agent_id, + id, + updated, + nocterm_project_model::ContextId::General, + ) + } + /// Frame a newly allocated chat with an explicit durable owner. + pub fn for_scope( + agent_id: String, + id: String, + updated: u64, + scope: nocterm_project_model::ContextId, + ) -> Self { Self { version: VERSION, - id, - agent_id, - title: None, - name: None, - pinned: false, + header: SavedHeader::new(agent_id, id, updated, scope), session_id: None, workdir: None, model: None, @@ -145,7 +136,6 @@ impl SavedChat { mode: None, pending_history: false, draft: None, - updated, entries: Vec::new(), queue: Vec::new(), attachments: Vec::new(), @@ -155,18 +145,65 @@ impl SavedChat { } } + /// Explicit environment-scoped local documents use version three; no data is uploaded. + pub fn for_environment( + agent_id: String, + id: String, + updated: u64, + scope: crate::local_environment::ChatScope, + ) -> Self { + let mut chat = Self::for_scope(agent_id, id, updated, scope.context()); + chat.version = ENVIRONMENT_VERSION; + chat.header.environment = scope.environment().clone(); + chat + } + pub fn archive_draft_in_environment( + id: String, + agent_id: String, + text: String, + updated: u64, + scope: crate::local_environment::ChatScope, + ) -> Self { + let mut chat = Self::for_environment(agent_id, id, updated, scope); + chat.draft = (!text.is_empty()).then_some(text); + chat.archived_draft = true; + chat + } /// A draft edit made before the archived transcript has been hydrated. pub fn archive_draft(id: String, agent_id: String, text: String, updated: u64) -> Self { - let mut chat = Self::for_identity(agent_id, id, updated); + Self::archive_draft_in_scope( + id, + agent_id, + text, + updated, + nocterm_project_model::ContextId::General, + ) + } + /// A literal draft patch carries the same durable owner as its original document. + pub fn archive_draft_in_scope( + id: String, + agent_id: String, + text: String, + updated: u64, + scope: nocterm_project_model::ContextId, + ) -> Self { + let mut chat = Self::for_scope(agent_id, id, updated, scope); chat.draft = (!text.is_empty()).then_some(text); - chat.updated = updated; + chat.header.updated = updated; chat.archived_draft = true; chat } /// The pending archived draft fields; no transcript is fabricated from this patch. pub fn draft_patch(&self) -> Option<(Option, u64)> { self.archived_draft - .then(|| (self.draft.clone(), self.updated)) + .then(|| (self.draft.clone(), self.header.updated)) + } + /// Admit identity and format ownership before exposing an in-memory record. + pub fn validate_identity(&self) -> Result<(), HistoryCodecError> { + codec::validate_id(&self.header.id)?; + header::resolve_scope(self.version, Some(self.header.scope))?; + header::validate_environment(self.version, &self.header.environment)?; + Ok(()) } /// Reject a prompt before accepting it if it could not be read back. pub fn validate_size(&self) -> Result<(), HistoryCodecError> { @@ -238,12 +275,40 @@ pub struct SharedChat { pub metadata: SavedChat, pub queue: Vec>, } +impl SharedChat { + /// Conservative structural ownership cost, including full retained media and + /// shared queue records. No encoded transcript buffer is allocated. + pub fn retained_bytes(&self) -> Result { + let metadata = size::structural_size(&wire::Wire::complete(&self.metadata))?; + self.queue.iter().try_fold(metadata, |bytes, prompt| { + Ok(bytes.saturating_add(prompt_memory(prompt)?)) + }) + } +} impl std::ops::Deref for SharedChat { type Target = SavedChat; fn deref(&self) -> &Self::Target { &self.metadata } } + +/// Exact compact JSON byte size, without allocating the encoded payload. +/// This counts an already decoded value; it does not parse or authenticate input. +pub fn json_size(value: &impl serde::Serialize) -> Result { + size::encoded_len(value).map_err(HistoryCodecError::from) +} + +/// Conservative retained ownership cost, without allocating an encoded buffer. +/// Consumers must still bound native input before decoding or materializing it. +pub fn retained_value_bytes(value: &impl serde::Serialize) -> Result { + size::structural_size(value).map_err(HistoryCodecError::from) +} + +/// Exact JSON string size, using the same allocation-free codec counter. +pub fn text_size(text: &str) -> Result { + size::encoded_len(&text).map_err(HistoryCodecError::from) +} + pub fn content_size(content: &Content) -> Result { size::encoded_len(content).map_err(HistoryCodecError::from) } @@ -254,4 +319,6 @@ pub fn prompt_size(prompt: &SavedPrompt) -> Result { #[cfg(test)] mod draft_tests; #[cfg(test)] +mod scope_tests; +#[cfg(test)] mod version_one_tests; diff --git a/crates/ai/src/history/budget.rs b/crates/ai/src/history/budget.rs index 6b1d731b..1727c976 100644 --- a/crates/ai/src/history/budget.rs +++ b/crates/ai/src/history/budget.rs @@ -8,11 +8,15 @@ pub struct PreparedBudget { entries: Arc>, memory: Arc>, prompts: Arc>, + preview: super::catalog_header::Preview, } impl PreparedBudget { pub(super) fn new(chat: &SavedChat) -> Result { Ok(Self { resident_bytes: 0, + preview: super::catalog_header::Preview::persisted( + &chat.entries[SavedChat::saved_entry_start(chat.entries.len())..], + ), memory: Arc::new( chat.entries .iter() @@ -43,6 +47,7 @@ impl PreparedBudget { pub struct HistoryBudget { memory: Vec, entries: Vec, + preview: super::catalog_header::Preview, } impl HistoryBudget { pub fn resident_bytes(&self) -> usize { @@ -50,11 +55,15 @@ impl HistoryBudget { } pub fn restored(prepared: Option<&PreparedBudget>) -> Self { Self { + preview: prepared.map_or_else(Default::default, |prepared| prepared.preview.clone()), memory: prepared.map_or_else(Vec::new, |prepared| (*prepared.memory).clone()), entries: prepared.map_or_else(Vec::new, |prepared| (*prepared.entries).clone()), } } pub fn refresh(&mut self, entries: &[Entry], dirty: &[usize]) -> Result<(), HistoryCodecError> { + self.preview = super::catalog_header::Preview::persisted( + &entries[SavedChat::saved_entry_start(entries.len())..], + ); let old = self.entries.len(); self.entries.resize(entries.len(), 0); self.memory.resize(entries.len(), 0); @@ -74,11 +83,15 @@ impl HistoryBudget { metadata: &SavedChat, prompts: impl Iterator, ) -> Result<(), HistoryCodecError> { - let base = size::structural_size(&wire::Wire::new(metadata, None::<&[super::SavedPrompt]>)) - .map_err(HistoryCodecError::from)?; let rows = &self.memory[self.memory.len().saturating_sub(MAX_SAVED_ENTRIES)..]; let (count, prompts) = prompts.fold((0, 0), |(count, total), bytes| (count + 1, total + bytes)); + let base = size::structural_size(&wire::Wire::budget( + metadata, + None::<&[super::SavedPrompt]>, + self.preview.clone().with_prompts(count != 0), + )) + .map_err(HistoryCodecError::from)?; super::check_memory( base + rows.iter().sum::() + prompts + if count == 0 { 0 } else { 2048 }, ) @@ -89,32 +102,30 @@ impl HistoryBudget { metadata: &SavedChat, prompts: impl Iterator, ) -> Result<(), HistoryCodecError> { - let mut metadata_size = - size::encoded_len(&wire::Wire::new(metadata, None::<&[super::SavedPrompt]>)) - .map_err(HistoryCodecError::from)?; + let prompts = prompts.collect::>(); + let preview = self.preview.clone().with_prompts(!prompts.is_empty()); + let mut metadata_size = size::encoded_len(&wire::Wire::budget( + metadata, + None::<&[super::SavedPrompt]>, + preview.clone(), + )) + .map_err(HistoryCodecError::from)?; let entries = &self.entries[self.entries.len().saturating_sub(MAX_SAVED_ENTRIES)..]; metadata_size += entries.iter().sum::() + entries.len().saturating_sub(1); - let prompts = prompts.collect::>(); if !prompts.is_empty() { - // The same serializer provides the field/empty-array framing; no mirrored field formula. - let mut with_queue = SavedChat::for_identity( - metadata.agent_id.clone(), - metadata.id.clone(), - metadata.updated, - ); - with_queue.queue.push(super::SavedPrompt { - id: 0, - text: String::new(), - images: Vec::new(), - attachments: Vec::new(), - }); - let framing = size::encoded_len(&wire::Wire::new( - &with_queue, + // Keep the authoritative projection fixed while measuring queue framing. + let framing = size::encoded_len(&wire::Wire::budget( + metadata, Some(&Vec::::new()), + preview.clone(), )) .map_err(HistoryCodecError::from)? - - size::encoded_len(&wire::Wire::new(&with_queue, None::<&[super::SavedPrompt]>)) - .map_err(HistoryCodecError::from)?; + - size::encoded_len(&wire::Wire::budget( + metadata, + None::<&[super::SavedPrompt]>, + preview, + )) + .map_err(HistoryCodecError::from)?; metadata_size += framing + prompts.iter().sum::() + prompts.len() - 1; } check_size(metadata_size) @@ -134,14 +145,14 @@ mod tests { "00000000-0000-4000-8000-000000000064".into(), 1, ); - chat.title = Some("title\u{0}\n".into()); - chat.name = Some("name".into()); + chat.header.title = Some("title\u{0}\n".into()); + chat.header.name = Some("name".into()); chat.session_id = Some("session".into()); chat.workdir = Some("/tmp/界".into()); chat.model = Some("model".into()); - chat.pinned = true; + chat.header.pinned = true; chat.pending_history = true; - chat.updated = u64::MAX; + chat.header.updated = u64::MAX; chat.draft = Some("literal draft\n\0界\"\\".into()); chat.attachments = vec![ SavedAttachment::LocalTerminal("local".into()), @@ -232,4 +243,59 @@ mod tests { .is_err() ); } + #[test] + fn fallback_title_budget_tracks_image_replacement_dirty_rows_and_an_empty_window() { + fn exact_boundary(budget: &HistoryBudget, chat: &SavedChat) { + let mut with_queue = chat.clone(); + with_queue.queue = vec![SavedPrompt { + id: 1, + text: String::new(), + images: Vec::new(), + attachments: Vec::new(), + }]; + let framing = super::super::encode(&with_queue).unwrap().len(); + let mut prompt = with_queue.queue.remove(0); + prompt.text = "a".repeat(MAX_FILE_BYTES as usize - framing); + let mut metadata = chat.clone(); + metadata.entries.clear(); + metadata.queue.clear(); + assert!( + budget + .validate( + &metadata, + std::iter::once(super::super::prompt_size(&prompt).unwrap()) + ) + .is_ok() + ); + prompt.text.push('a'); + assert!(matches!( + budget.validate( + &metadata, + std::iter::once(super::super::prompt_size(&prompt).unwrap()) + ), + Err(HistoryCodecError::EncodedLimit) + )); + } + let mut chat = SavedChat::for_identity( + "agent".into(), + "00000000-0000-4000-8000-000000000001".into(), + 1, + ); + chat.entries.push(Entry::User(vec![ + Content::Image(crate::model::content::Image::new( + "A".repeat(super::super::MAX_SAVED_IMAGE_BYTES + 1), + "image/png", + )), + Content::text("question after image"), + ])); + let mut budget = HistoryBudget::default(); + budget.refresh(&chat.entries, &[]).unwrap(); + exact_boundary(&budget, &chat); + chat.entries = vec![Entry::User(vec![Content::text("changed first question")])]; + budget.refresh(&chat.entries, &[0]).unwrap(); + exact_boundary(&budget, &chat); + chat.entries.clear(); + budget.refresh(&chat.entries, &[]).unwrap(); + exact_boundary(&budget, &chat); + } } diff --git a/crates/ai/src/history/catalog.rs b/crates/ai/src/history/catalog.rs index d37c844e..007df674 100644 --- a/crates/ai/src/history/catalog.rs +++ b/crates/ai/src/history/catalog.rs @@ -1,7 +1,7 @@ //! Rebuildable metadata cache. Transcript files remain authoritative. -use super::{HistoryCodecError, SavedChat, VERSION}; -use crate::model::content::Content; -use serde::{Deserialize, Serialize}; +use super::HistoryCodecError; +use nocterm_project_model::ContextId; +use serde::Deserialize; /// Primitive file stamp supplied by the storage adapter, never filesystem metadata. #[derive(Clone, Copy, Debug, PartialEq, Eq)] @@ -10,73 +10,14 @@ pub struct FileStamp { pub modified_nanos: u128, } -#[derive(Clone, Debug, Deserialize, Serialize)] -pub struct ChatSummary { - #[serde(default)] - cache_version: u32, - pub id: String, - pub agent_id: String, - pub title: Option, - pub name: Option, - pub pinned: bool, - pub updated: u64, - pub model: Option, - pub has_content: bool, - pub bytes: u64, - modified_nanos: u128, -} -impl ChatSummary { - pub fn from_chat(chat: &SavedChat) -> Self { - let fallback = chat.entries.iter().find_map(|entry| match entry { - crate::thread::Entry::User(blocks) => blocks.iter().find_map(|block| match block { - Content::Text(text) if !text.text.trim().is_empty() => { - Some(text.text.trim().chars().take(60).collect()) - } - _ => None, - }), - _ => None, - }); - Self { - cache_version: 1, - id: chat.id.clone(), - agent_id: bounded(&chat.agent_id), - title: chat.title.as_deref().map(bounded).or(fallback), - name: chat.name.as_deref().map(bounded), - pinned: chat.pinned, - updated: chat.updated, - model: chat.model.as_deref().map(bounded), - has_content: !chat.entries.is_empty() - || !chat.queue.is_empty() - || chat.draft.as_ref().is_some_and(|text| !text.is_empty()), - bytes: 0, - modified_nanos: 0, - } - } - pub fn with_stamp(mut self, stamp: FileStamp, queued: bool) -> Self { - self.bytes = stamp.bytes; - self.modified_nanos = stamp.modified_nanos; - self.has_content |= queued; - self - } - pub fn matches_stamp(&self, id: &str, stamp: FileStamp) -> bool { - self.cache_version == 1 - && self.id == id - && self.bytes == stamp.bytes - && self.modified_nanos == stamp.modified_nanos - } - pub fn title(&self) -> &str { - self.name - .as_deref() - .or(self.title.as_deref()) - .unwrap_or("Saved chat") - } -} -fn bounded(text: &str) -> String { - text.chars().take(512).collect() -} +pub use super::catalog_header::ChatSummary; #[derive(Deserialize)] struct Legacy { version: u32, + #[serde(default)] + scope: Option, + #[serde(default)] + environment: Option, id: String, agent_id: String, #[serde(default)] @@ -208,31 +149,76 @@ pub fn decode_summary( ) -> Result, HistoryCodecError> { super::check_size(bytes.len())?; super::preflight::check(bytes)?; - let old: Legacy = serde_json::from_slice(bytes)?; - if old.version != VERSION { - return Err(HistoryCodecError::UnsupportedVersion(old.version)); + if super::prefix::is_scoped(bytes) { + let header = super::decode_catalog_header(bytes, id)?; + let envelope: ScopedSummary = serde_json::from_slice(bytes)?; + if !matches!( + envelope.version, + super::VERSION | super::ENVIRONMENT_VERSION + ) || envelope.chat.version != envelope.version + || envelope.catalog != header + || envelope.chat.catalog(id)? != header + { + return Err(HistoryCodecError::HeaderMismatch); + } + return Ok(header.summary(stamp)); } - if old.id != id { - return Err(HistoryCodecError::IdentityMismatch); + let old: Legacy = serde_json::from_slice(bytes)?; + if old.version != super::LEGACY_VERSION { + super::header::resolve_scope(old.version, old.scope)?; + return Err(HistoryCodecError::MissingCatalog); } - let content = - old.entries.len != 0 || old.queue != 0 || old.draft.is_some_and(|draft| !draft.is_empty()); - if !content && old.name.is_none() && !old.pinned { - return Ok(None); + Ok(old + .catalog(id)? + .summary(stamp) + .map(|row| row.with_version(super::TranscriptVersion::LEGACY))) +} +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct ScopedSummary { + version: u32, + catalog: super::CatalogHeader, + chat: Legacy, +} +impl Legacy { + fn catalog(self, id: &str) -> Result { + let scope = super::header::resolve_scope(self.version, self.scope)?; + let environment = self + .environment + .ok_or(HistoryCodecError::MissingEnvironment) + .or_else(|error| { + if self.version <= 2 { + Ok(crate::local_environment::LocalEnvironment::ThisComputer) + } else { + Err(error) + } + })?; + super::header::validate_environment(self.version, &environment)?; + if self.id != id { + return Err(HistoryCodecError::IdentityMismatch); + } + let content = self.entries.len != 0 + || self.queue != 0 + || self.draft.is_some_and(|draft| !draft.is_empty()); + let saved = super::SavedHeader { + id: self.id, + agent_id: self.agent_id, + scope, + environment, + title: self.title.or(self.entries.title), + name: self.name, + pinned: self.pinned, + updated: self.updated, + }; + Ok(super::CatalogHeader::from_parts( + &saved, + &self.model, + &super::catalog_header::Preview::for_title(None, content), + None, + false, + self.version, + )) } - Ok(Some(ChatSummary { - cache_version: 1, - id: old.id, - agent_id: bounded(&old.agent_id), - title: old.title.as_deref().map(bounded).or(old.entries.title), - name: old.name.as_deref().map(bounded), - pinned: old.pinned, - updated: old.updated, - model: old.model.as_deref().map(bounded), - has_content: content, - bytes: stamp.bytes, - modified_nanos: stamp.modified_nanos, - })) } #[cfg(test)] diff --git a/crates/ai/src/history/catalog/tests.rs b/crates/ai/src/history/catalog/tests.rs index 11f1d26b..3df6c79e 100644 --- a/crates/ai/src/history/catalog/tests.rs +++ b/crates/ai/src/history/catalog/tests.rs @@ -1,4 +1,4 @@ -use super::*; +use crate::history::SavedChat; use crate::model::content::Content; use crate::{history::SavedPrompt, thread::Entry}; fn chat() -> SavedChat { @@ -27,3 +27,23 @@ fn cached_structural_estimates_cover_preflight_and_pending_only_rows() { assert!(estimated >= preflight, "{estimated} < {preflight}"); assert!(estimated >= bytes.len()); } +#[test] +fn catalog_metadata_admission_rejects_unbounded_display_fields_and_invalid_identity() { + let row = crate::history::ChatSummary::from_chat(&chat()); + row.validate().unwrap(); + for field in ["agent_id", "title", "name", "model"] { + let mut changed = serde_json::to_value(&row).unwrap(); + changed[field] = serde_json::json!("界".repeat(513)); + let changed: crate::history::ChatSummary = serde_json::from_value(changed).unwrap(); + assert!(matches!( + changed.validate(), + Err(crate::history::HistoryCodecError::HeaderLimit) + )); + } + let mut changed = row; + changed.id = "invalid".into(); + assert!(matches!( + changed.validate(), + Err(crate::history::HistoryCodecError::InvalidId(_)) + )); +} diff --git a/crates/ai/src/history/catalog_header.rs b/crates/ai/src/history/catalog_header.rs new file mode 100644 index 00000000..19c65f02 --- /dev/null +++ b/crates/ai/src/history/catalog_header.rs @@ -0,0 +1,262 @@ +//! Bounded authoritative discovery facts live before the transcript payload. +use super::{ContextId, FileStamp, SavedChat, SavedHeader, TranscriptVersion}; +use crate::local_environment::{ChatScope, LocalEnvironment}; +use crate::{model::content::Content, thread::Entry}; +use serde::{Deserialize, Serialize}; + +/// The catalog part of a v2 transcript, not an independently trusted sidecar. +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +#[serde(deny_unknown_fields)] +pub struct CatalogHeader { + id: String, + agent_id: String, + scope: ContextId, + #[serde(default, skip_serializing_if = "Option::is_none")] + environment: Option, + title: Option, + name: Option, + pinned: bool, + updated: u64, + model: Option, + has_content: bool, +} +impl CatalogHeader { + pub fn id(&self) -> &str { + &self.id + } + pub fn scope(&self) -> ContextId { + self.scope + } + pub fn environment(&self) -> LocalEnvironment { + self.environment.clone().unwrap_or_default() + } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment(), self.scope) + } + pub(super) fn require_version(&self, version: u32) -> Result<(), super::HistoryCodecError> { + if (version == super::ENVIRONMENT_VERSION) != self.environment.is_some() { + return Err(super::HistoryCodecError::InvalidHeader); + } + super::header::validate_environment(version, &self.environment()) + } + pub fn transcript_version(&self) -> TranscriptVersion { + if self.environment.is_some() { + TranscriptVersion::ENVIRONMENT + } else { + TranscriptVersion::SCOPED + } + } + pub fn summary(&self, stamp: FileStamp) -> Option { + (self.has_content || self.name.is_some() || self.pinned).then(|| self.to_summary(stamp)) + } + pub(super) fn to_summary(&self, stamp: FileStamp) -> ChatSummary { + ChatSummary { + cache_version: 3, + transcript_version: if self.environment.is_some() { + TranscriptVersion::ENVIRONMENT + } else { + TranscriptVersion::SCOPED + }, + environment: self.environment(), + id: self.id.clone(), + agent_id: self.agent_id.clone(), + scope: self.scope, + title: self.title.clone(), + name: self.name.clone(), + pinned: self.pinned, + updated: self.updated, + model: self.model.clone(), + has_content: self.has_content, + bytes: stamp.bytes, + modified_nanos: stamp.modified_nanos, + } + } + pub(super) fn from_chat(chat: &SavedChat, queued: bool) -> Self { + let start = SavedChat::saved_entry_start(chat.entries.len()); + Self::from_parts( + &chat.header, + &chat.model, + &Preview::new(&chat.entries[start..]), + chat.draft.as_deref(), + queued || !chat.queue.is_empty(), + chat.version, + ) + } + pub(super) fn for_persistence(chat: &SavedChat, queued: bool) -> Self { + let start = SavedChat::saved_entry_start(chat.entries.len()); + Self::from_parts( + &chat.header, + &chat.model, + &Preview::persisted(&chat.entries[start..]), + chat.draft.as_deref(), + queued || !chat.queue.is_empty(), + chat.version, + ) + } + pub(super) fn from_parts( + saved: &SavedHeader, + model: &Option, + preview: &Preview, + draft: Option<&str>, + queued: bool, + version: u32, + ) -> Self { + Self { + id: saved.id.clone(), + agent_id: bounded(&saved.agent_id), + scope: saved.scope, + environment: (version == super::ENVIRONMENT_VERSION).then(|| saved.environment.clone()), + title: saved + .title + .as_deref() + .map(bounded) + .or_else(|| preview.title.clone()), + name: saved.name.as_deref().map(bounded), + pinned: saved.pinned, + updated: saved.updated, + model: model.as_deref().map(bounded), + has_content: preview.has_content + || queued + || draft.is_some_and(|text| !text.is_empty()), + } + } + pub(super) fn validate(&self, id: &str) -> Result<(), super::HistoryCodecError> { + if self.id != id { + return Err(super::HistoryCodecError::IdentityMismatch); + } + validate_metadata(id, &self.agent_id, [&self.title, &self.name, &self.model]) + } +} +fn validate_metadata( + id: &str, + agent: &str, + display: [&Option; 3], +) -> Result<(), super::HistoryCodecError> { + if uuid::Uuid::parse_str(id).is_err() { + return Err(super::HistoryCodecError::InvalidId(id.into())); + } + if std::iter::once(agent) + .chain(display.into_iter().filter_map(|text| text.as_deref())) + .any(|text| text.chars().take(513).count() > 512) + { + return Err(super::HistoryCodecError::HeaderLimit); + } + Ok(()) +} +fn bounded(text: &str) -> String { + text.chars().take(512).collect() +} + +/// Small content facts cached with per-row budgets, including after dirty-row edits. +#[derive(Clone, Debug, Default)] +pub(super) struct Preview { + title: Option, + has_content: bool, +} +impl Preview { + pub(super) fn for_title(title: Option, has_content: bool) -> Self { + Self { title, has_content } + } + pub(super) fn new(entries: &[Entry]) -> Self { + Self::projected(entries, false) + } + pub(super) fn persisted(entries: &[Entry]) -> Self { + Self::projected(entries, true) + } + fn projected(entries: &[Entry], persisted: bool) -> Self { + let title = entries.iter().find_map(|entry| match entry { + Entry::User(blocks) => blocks.iter().find_map(|block| { + let text = match block { + Content::Text(text) => text.text.as_str(), + Content::Image(image) + if persisted && image.data.len() > super::MAX_SAVED_IMAGE_BYTES => + { + super::OMITTED_IMAGE_TEXT + } + _ => return None, + }; + (!text.trim().is_empty()).then(|| text.trim().chars().take(60).collect()) + }), + _ => None, + }); + Self { + title, + has_content: !entries.is_empty(), + } + } + pub(super) fn with_prompts(mut self, present: bool) -> Self { + self.has_content |= present; + self + } +} + +/// Rebuildable sidecar data. Only the authoritative transcript header supplies it. +#[derive(Clone, Debug, PartialEq, Eq, Deserialize, Serialize)] +pub struct ChatSummary { + #[serde(default)] + cache_version: u32, + transcript_version: TranscriptVersion, + pub id: String, + pub agent_id: String, + pub scope: ContextId, + #[serde(default, skip_serializing_if = "LocalEnvironment::is_this_computer")] + pub environment: LocalEnvironment, + pub title: Option, + pub name: Option, + pub pinned: bool, + pub updated: u64, + pub model: Option, + pub has_content: bool, + pub bytes: u64, + modified_nanos: u128, +} +impl ChatSummary { + /// Check the bounded metadata contract when accepting a catalog port's DTO. + /// This does not validate an unread transcript payload or issue authority. + pub fn validate(&self) -> Result<(), super::HistoryCodecError> { + self.transcript_version.require_scope(self.scope)?; + self.transcript_version + .require_environment(&self.environment)?; + validate_metadata( + &self.id, + &self.agent_id, + [&self.title, &self.name, &self.model], + ) + } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment.clone(), self.scope) + } + pub fn transcript_version(&self) -> TranscriptVersion { + self.transcript_version + } + pub(super) fn with_version(mut self, version: TranscriptVersion) -> Self { + self.transcript_version = version; + self + } + pub fn from_chat(chat: &SavedChat) -> Self { + CatalogHeader::for_persistence(chat, false) + .to_summary(FileStamp { + bytes: 0, + modified_nanos: 0, + }) + .with_version(TranscriptVersion::observed(chat.version)) + } + pub fn with_stamp(mut self, stamp: FileStamp, queued: bool) -> Self { + self.bytes = stamp.bytes; + self.modified_nanos = stamp.modified_nanos; + self.has_content |= queued; + self + } + pub fn matches_stamp(&self, id: &str, stamp: FileStamp) -> bool { + self.cache_version == 3 + && self.id == id + && self.bytes == stamp.bytes + && self.modified_nanos == stamp.modified_nanos + } + pub fn title(&self) -> &str { + self.name + .as_deref() + .or(self.title.as_deref()) + .unwrap_or("Saved chat") + } +} diff --git a/crates/ai/src/history/codec.rs b/crates/ai/src/history/codec.rs index 857e9b30..426efc97 100644 --- a/crates/ai/src/history/codec.rs +++ b/crates/ai/src/history/codec.rs @@ -1,6 +1,6 @@ -//! CPU-only version-one transcript encoding and bounded loading. +//! CPU-only versioned transcript encoding and bounded loading. use super::{ - HistoryCodecError, PreparedBudget, SavedChat, SharedChat, VERSION, check_size, preflight, wire, + HistoryCodecError, PreparedBudget, SavedChat, SharedChat, check_size, preflight, wire, }; pub fn structural_bytes(bytes: &[u8]) -> Result { @@ -10,11 +10,8 @@ pub fn decode(bytes: &[u8], id: &str) -> Result { validate_id(id)?; check_size(bytes.len())?; let resident = preflight::check(bytes)?; - let mut chat: SavedChat = serde_json::from_slice(bytes)?; - if chat.version != VERSION { - return Err(HistoryCodecError::UnsupportedVersion(chat.version)); - } - if chat.id != id { + let mut chat = super::incoming::decode_bytes(bytes, id)?; + if chat.header.id != id { return Err(HistoryCodecError::IdentityMismatch); } let mut prepared = PreparedBudget::new(&chat)?; @@ -25,10 +22,9 @@ pub fn decode(bytes: &[u8], id: &str) -> Result { /// Validate an already decoded construction input without trimming its contents. /// Unlike the persistence view, this retains every entry and full image payload. pub fn prepare(chat: SavedChat) -> Result { - validate_id(&chat.id)?; - if chat.version != VERSION { - return Err(HistoryCodecError::UnsupportedVersion(chat.version)); - } + validate_id(&chat.header.id)?; + super::header::resolve_scope(chat.version, Some(chat.header.scope))?; + super::header::validate_environment(chat.version, &chat.header.environment)?; if chat.archived_draft { return Err(HistoryCodecError::ArchivedPatch); } @@ -38,8 +34,38 @@ pub fn prepare(chat: SavedChat) -> Result { return Err(HistoryCodecError::CollectionLimit); } let bytes = serde_json::to_vec(&chat)?; - decode(&bytes, &chat.id) + decode(&bytes, &chat.header.id) } +/// Validate and prepare the same owned decoded body. This avoids serializing +/// and decoding a second full transcript merely to create its cached budgets. +pub fn prepare_owned(mut chat: SavedChat) -> Result { + validate_id(&chat.header.id)?; + super::header::resolve_scope(chat.version, Some(chat.header.scope))?; + super::header::validate_environment(chat.version, &chat.header.environment)?; + if chat.archived_draft { + return Err(HistoryCodecError::ArchivedPatch); + } + chat.validate_collections()?; + if chat.entries.len() > super::MAX_SAVED_ENTRIES || chat.times.len() > super::MAX_SAVED_ENTRIES + { + return Err(HistoryCodecError::CollectionLimit); + } + let resident = { + let value = wire::Wire::complete(&chat); + // Preserve decode's public refusal ordering: encoded limit before the + // structural limit, using the existing allocation-free counters. + check_size(super::size::encoded_len(&value).map_err(HistoryCodecError::from)?)?; + let resident = super::size::structural_size(&value).map_err(HistoryCodecError::from)?; + super::check_memory(resident)?; + resident + }; + super::CatalogHeader::from_chat(&chat, false).validate(&chat.header.id)?; + let mut prepared = PreparedBudget::new(&chat)?; + prepared.resident_bytes = resident; + chat.prepared = Some(prepared); + Ok(chat) +} + pub fn encode(chat: &SavedChat) -> Result, HistoryCodecError> { if chat.archived_draft { return Err(HistoryCodecError::ArchivedPatch); @@ -73,16 +99,15 @@ fn encode_value( chat: &SavedChat, value: &impl serde::Serialize, ) -> Result, HistoryCodecError> { - validate_id(&chat.id)?; - if chat.version != VERSION { - return Err(HistoryCodecError::UnsupportedVersion(chat.version)); - } + validate_id(&chat.header.id)?; + super::header::resolve_scope(chat.version, Some(chat.header.scope))?; + super::header::validate_environment(chat.version, &chat.header.environment)?; let bytes = serde_json::to_vec(value)?; check_size(bytes.len())?; preflight::check(&bytes)?; Ok(bytes) } -fn validate_id(id: &str) -> Result<(), HistoryCodecError> { +pub(super) fn validate_id(id: &str) -> Result<(), HistoryCodecError> { if uuid::Uuid::parse_str(id).is_err() { return Err(HistoryCodecError::InvalidId(id.into())); } diff --git a/crates/ai/src/history/draft_tests.rs b/crates/ai/src/history/draft_tests.rs index eb4e9bee..e120a6c8 100644 --- a/crates/ai/src/history/draft_tests.rs +++ b/crates/ai/src/history/draft_tests.rs @@ -16,7 +16,7 @@ fn legacy_chats_have_no_draft_and_literal_unsent_text_round_trips() { let draft = " \n\r\t\"\\\0界 "; chat.draft = Some(draft.into()); let encoded = encode(&chat).unwrap(); - let restored = decode(&encoded, &chat.id).unwrap(); + let restored = decode(&encoded, &chat.header.id).unwrap(); assert_eq!(restored.draft.as_deref(), Some(draft)); assert_eq!(restored.entries.len(), 1); } diff --git a/crates/ai/src/history/error.rs b/crates/ai/src/history/error.rs index 31ed0088..a21809da 100644 --- a/crates/ai/src/history/error.rs +++ b/crates/ai/src/history/error.rs @@ -1,5 +1,5 @@ //! Semantic failures of the bounded saved-chat format. -use std::sync::Arc; +use nocterm_core::SharedError; #[derive(Clone, Debug, thiserror::Error)] pub enum HistoryCodecError { @@ -9,6 +9,22 @@ pub enum HistoryCodecError { IdentityMismatch, #[error("Unsupported saved chat version: {0}")] UnsupportedVersion(u32), + #[error("A version-two chat requires explicit ownership")] + MissingScope, + #[error("Version-one chats cannot claim project ownership")] + LegacyScope, + #[error("A version-three local chat requires an explicit environment")] + MissingEnvironment, + #[error("Legacy chats belong only to this computer")] + LegacyEnvironment, + #[error("The saved chat requires its authoritative catalog prefix")] + MissingCatalog, + #[error("The saved catalog prefix is malformed or noncanonical")] + InvalidHeader, + #[error("The saved catalog prefix exceeds its bounded format")] + HeaderLimit, + #[error("The saved catalog prefix does not match its transcript")] + HeaderMismatch, #[error("This chat exceeds the saved collection limit.")] CollectionLimit, #[error("This chat exceeds its storage limit. Remove images or shorten the message.")] @@ -20,10 +36,10 @@ pub enum HistoryCodecError { #[error("An archived draft patch requires the shared document writer.")] ArchivedPatch, #[error("Could not encode or decode the saved chat: {0}")] - Json(#[source] Arc), + Json(#[source] SharedError), } impl From for HistoryCodecError { fn from(error: serde_json::Error) -> Self { - Self::Json(Arc::new(error)) + Self::Json(SharedError::from_error(error)) } } diff --git a/crates/ai/src/history/header.rs b/crates/ai/src/history/header.rs new file mode 100644 index 00000000..8728c991 --- /dev/null +++ b/crates/ai/src/history/header.rs @@ -0,0 +1,124 @@ +//! Durable identity and catalog metadata, independent of transcript payloads. +use crate::local_environment::{ChatScope, LocalEnvironment}; +use nocterm_project_model::ContextId; +use serde::{Deserialize, ser::SerializeMap}; + +/// Plain saved metadata. Membership does not select or launch an agent process. +#[derive(Clone, Debug)] +pub struct SavedHeader { + /// Names the saved document; a supplied UUID. + pub id: String, + /// The registry identity of the agent the chat was held with. + pub agent_id: String, + /// The title supplied by the agent. + pub title: Option, + /// The user's name, overriding the agent title. + pub name: Option, + /// Pinned chats sort before unpinned history. + pub pinned: bool, + /// Seconds since the Unix epoch, supplied by the host. + pub updated: u64, + /// Durable chat membership, independent of names, paths and current selection. + pub scope: ContextId, + pub environment: LocalEnvironment, +} +impl SavedHeader { + pub fn new(agent_id: String, id: String, updated: u64, scope: ContextId) -> Self { + Self { + id, + agent_id, + title: None, + name: None, + pinned: false, + updated, + scope, + environment: LocalEnvironment::ThisComputer, + } + } + pub fn chat_scope(&self) -> ChatScope { + ChatScope::new(self.environment.clone(), self.scope) + } + pub(super) fn serialize_prefix(&self, map: &mut M) -> Result<(), M::Error> { + map.serialize_entry("id", &self.id)?; + map.serialize_entry("agent_id", &self.agent_id)?; + if let Some(title) = &self.title { + map.serialize_entry("title", title)?; + } + if let Some(name) = &self.name { + map.serialize_entry("name", name)?; + } + if self.pinned { + map.serialize_entry("pinned", &self.pinned)?; + } + Ok(()) + } +} + +#[derive(Deserialize)] +pub(super) struct IncomingHeader { + id: String, + agent_id: String, + #[serde(default)] + title: Option, + #[serde(default)] + name: Option, + #[serde(default)] + pinned: bool, + updated: u64, + #[serde(default)] + scope: Option, + #[serde(default)] + environment: Option, +} +impl IncomingHeader { + pub(super) fn finish(self, version: u32) -> Result { + let scope = resolve_scope(version, self.scope)?; + let environment = self + .environment + .ok_or(super::HistoryCodecError::MissingEnvironment) + .or_else(|error| { + if version <= 2 { + Ok(LocalEnvironment::ThisComputer) + } else { + Err(error) + } + })?; + validate_environment(version, &environment)?; + Ok(SavedHeader { + id: self.id, + agent_id: self.agent_id, + title: self.title, + name: self.name, + pinned: self.pinned, + updated: self.updated, + scope, + environment, + }) + } +} +pub(super) fn resolve_scope( + version: u32, + scope: Option, +) -> Result { + use super::{HistoryCodecError, LEGACY_VERSION, VERSION}; + match (version, scope) { + (LEGACY_VERSION, None | Some(ContextId::General)) => Ok(ContextId::General), + (LEGACY_VERSION, Some(ContextId::Project(_))) => Err(HistoryCodecError::LegacyScope), + (VERSION | super::ENVIRONMENT_VERSION, Some(scope)) => Ok(scope), + (VERSION | super::ENVIRONMENT_VERSION, None) => Err(HistoryCodecError::MissingScope), + (version, _) => Err(HistoryCodecError::UnsupportedVersion(version)), + } +} + +pub(super) fn validate_environment( + version: u32, + environment: &LocalEnvironment, +) -> Result<(), super::HistoryCodecError> { + match version { + 1 | 2 if !environment.is_this_computer() => { + Err(super::HistoryCodecError::LegacyEnvironment) + } + 1..=3 => Ok(()), + other => Err(super::HistoryCodecError::UnsupportedVersion(other)), + } +} diff --git a/crates/ai/src/history/incoming.rs b/crates/ai/src/history/incoming.rs new file mode 100644 index 00000000..440595a6 --- /dev/null +++ b/crates/ai/src/history/incoming.rs @@ -0,0 +1,163 @@ +//! Version-aware intake preserves legacy leniency without inventing scoped authority. +use super::{ + HistoryCodecError, SavedAttachment, SavedChat, SavedPrompt, decode, header::IncomingHeader, +}; +use crate::thread::Entry; +use serde::Deserialize; +use std::path::PathBuf; + +#[derive(Deserialize)] +pub(super) struct IncomingChat { + version: u32, + #[serde(flatten)] + header: IncomingHeader, + #[serde(default)] + session_id: Option, + #[serde(default)] + workdir: Option, + #[serde(default)] + model: Option, + #[serde(default)] + config: crate::session_config::Choices, + #[serde(default)] + mode: Option, + #[serde(default)] + pending_history: bool, + #[serde(default)] + draft: Option, + #[serde(deserialize_with = "decode::entries")] + entries: Vec, + #[serde(default, deserialize_with = "decode::prompts")] + queue: Vec, + #[serde(default, deserialize_with = "decode::attachments")] + attachments: Vec, + #[serde(default, deserialize_with = "decode::times")] + times: Vec, +} +impl IncomingChat { + pub(super) fn finish(self) -> Result { + Ok(SavedChat { + version: self.version, + header: self.header.finish(self.version)?, + session_id: self.session_id, + workdir: self.workdir, + model: self.model, + config: self.config, + mode: self.mode, + pending_history: self.pending_history, + draft: self.draft, + entries: self.entries, + queue: self.queue, + attachments: self.attachments, + times: self.times, + prepared: None, + archived_draft: false, + }) + } +} + +#[derive(Deserialize)] +#[serde(deny_unknown_fields)] +struct IncomingEnvelope { + version: u32, + catalog: super::CatalogHeader, + chat: IncomingChat, +} +impl IncomingEnvelope { + fn finish(self) -> Result { + if !matches!(self.version, super::VERSION | super::ENVIRONMENT_VERSION) { + return Err(HistoryCodecError::UnsupportedVersion(self.version)); + } + let chat = self.chat.finish()?; + if chat.version != self.version + || self.catalog != super::CatalogHeader::from_chat(&chat, false) + { + return Err(HistoryCodecError::HeaderMismatch); + } + self.catalog.validate(&chat.header.id)?; + Ok(chat) + } +} +fn legacy(chat: IncomingChat) -> Result { + let chat = chat.finish()?; + if chat.version != super::LEGACY_VERSION { + return Err(HistoryCodecError::MissingCatalog); + } + Ok(chat) +} +pub(super) fn decode_bytes(bytes: &[u8], id: &str) -> Result { + if super::prefix::is_scoped(bytes) { + super::decode_catalog_header(bytes, id)?; + serde_json::from_slice::(bytes)?.finish() + } else { + legacy(serde_json::from_slice(bytes)?) + } +} + +/// Replay only the first key/value; this avoids buffering whole payloads to +/// choose between legacy flat records and the scoped envelope. +struct ReplayMap { + key: Option, + value: Option, + rest: A, +} +impl<'de, A: serde::de::MapAccess<'de>> serde::de::MapAccess<'de> for ReplayMap { + type Error = A::Error; + fn next_key_seed>( + &mut self, + seed: K, + ) -> Result, A::Error> { + if let Some(key) = self.key.take() { + seed.deserialize(serde::de::value::StringDeserializer::new(key)) + .map(Some) + } else { + self.rest.next_key_seed(seed) + } + } + fn next_value_seed>( + &mut self, + seed: V, + ) -> Result { + if let Some(value) = self.value.take() { + seed.deserialize(serde::de::value::U32Deserializer::new(value)) + } else { + self.rest.next_value_seed(seed) + } + } +} +pub(super) fn deserialize<'de, D: serde::Deserializer<'de>>( + deserializer: D, +) -> Result { + struct Record; + impl<'de> serde::de::Visitor<'de> for Record { + type Value = SavedChat; + fn expecting(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + formatter.write_str("a legacy chat or scoped chat envelope") + } + fn visit_map>( + self, + mut map: A, + ) -> Result { + use serde::de::{Error as _, value::MapAccessDeserializer}; + let key = map + .next_key::()? + .ok_or_else(|| A::Error::custom("missing chat fields"))?; + let version = (key == "version") + .then(|| map.next_value::()) + .transpose()?; + let scoped = matches!(version, Some(super::VERSION | super::ENVIRONMENT_VERSION)); + let replay = MapAccessDeserializer::new(ReplayMap { + key: Some(key), + value: version, + rest: map, + }); + if scoped { + IncomingEnvelope::deserialize(replay)?.finish() + } else { + legacy(IncomingChat::deserialize(replay)?) + } + .map_err(A::Error::custom) + } + } + deserializer.deserialize_map(Record) +} diff --git a/crates/ai/src/history/prefix.rs b/crates/ai/src/history/prefix.rs new file mode 100644 index 00000000..86f5889c --- /dev/null +++ b/crates/ai/src/history/prefix.rs @@ -0,0 +1,46 @@ +//! A canonical bounded prefix can be acquired without reading transcript payloads. +use super::{CatalogHeader, HistoryCodecError}; +pub const MAX_HEADER_BYTES: usize = 16 * 1024; +pub(super) const START: &[u8] = b"{\"version\":2,\"catalog\":"; +const ENVIRONMENT_START: &[u8] = b"{\"version\":3,\"catalog\":"; +pub(super) fn is_scoped(bytes: &[u8]) -> bool { + bytes.starts_with(START) || bytes.starts_with(ENVIRONMENT_START) +} +pub(super) const PAYLOAD: &[u8] = b",\"chat\":"; + +/// Validate only the authoritative v2 prefix. Full hydration additionally checks +/// that the catalog projection agrees with the decoded body. +pub fn decode_catalog_header(bytes: &[u8], id: &str) -> Result { + let prefix = bytes + .get(..bytes.len().min(MAX_HEADER_BYTES)) + .unwrap_or_default(); + let (input, version) = if let Some(input) = prefix.strip_prefix(START) { + (input, 2) + } else if let Some(input) = prefix.strip_prefix(ENVIRONMENT_START) { + (input, 3) + } else { + return Err(HistoryCodecError::InvalidHeader); + }; + let mut stream = serde_json::Deserializer::from_slice(input).into_iter::(); + let header = stream + .next() + .ok_or(HistoryCodecError::InvalidHeader)? + .map_err(|error| { + if error.is_eof() { + HistoryCodecError::HeaderLimit + } else { + error.into() + } + })?; + let end = stream.byte_offset(); + if !input + .get(end..) + .is_some_and(|tail| tail.starts_with(PAYLOAD)) + || serde_json::to_vec(&header)? != input[..end] + { + return Err(HistoryCodecError::InvalidHeader); + } + header.validate(id)?; + header.require_version(version)?; + Ok(header) +} diff --git a/crates/ai/src/history/scope_tests.rs b/crates/ai/src/history/scope_tests.rs new file mode 100644 index 00000000..d075ea9e --- /dev/null +++ b/crates/ai/src/history/scope_tests.rs @@ -0,0 +1,361 @@ +//! Transcript ownership is explicit in scoped formats and absent in legacy bytes. +use super::*; +use serde_json::{Value, json}; + +const ID: &str = "00000000-0000-4000-8000-0000000000a1"; +const PROJECT: &str = "01010101010101010101010101010101"; +const LEGACY: &[u8] = include_bytes!("fixtures/v1/canonical.json"); + +fn scoped(scope: Value) -> Value { + let mut chat = decode(LEGACY, ID).unwrap(); + chat.version = 2; + let mut value = serde_json::to_value(chat).unwrap(); + value["catalog"]["scope"] = scope.clone(); + value["chat"]["scope"] = scope; + value +} + +fn project() -> Value { + json!({"kind": "project", "id": PROJECT}) +} + +#[test] +fn explicit_general_and_project_ownership_survive_every_transcript_projection() { + for scope in [json!({"kind": "general"}), project()] { + let bytes = serde_json::to_vec(&scoped(scope.clone())).unwrap(); + let chat = decode(&bytes, ID).unwrap(); + let encoded: Value = serde_json::from_slice(&encode(&chat).unwrap()).unwrap(); + assert_eq!(encoded["version"], 2); + assert_eq!(encoded["catalog"]["scope"], scope); + let mut metadata = chat; + let queue = std::mem::take(&mut metadata.queue) + .into_iter() + .map(std::sync::Arc::new) + .collect(); + let shared = SharedChat { metadata, queue }; + let encoded: Value = serde_json::from_slice(&encode_shared(&shared).unwrap()).unwrap(); + assert_eq!(encoded["catalog"]["scope"], scope); + let restored = + prepare(decode(&serde_json::to_vec(&encoded).unwrap(), ID).unwrap()).unwrap(); + let restored: Value = serde_json::from_slice(&encode(&restored).unwrap()).unwrap(); + assert_eq!(restored["catalog"]["scope"], scope); + } +} + +#[test] +fn new_chats_explicitly_belong_to_general_until_a_project_is_supplied() { + let chat = SavedChat::for_identity("agent".into(), ID.into(), 1); + let value: Value = serde_json::from_slice(&encode(&chat).unwrap()).unwrap(); + assert_eq!(value["version"], 2); + assert_eq!(value["catalog"]["scope"], json!({"kind": "general"})); +} + +#[test] +fn scoped_transcripts_refuse_missing_unknown_and_malformed_ownership() { + let mut missing = scoped(project()); + missing["chat"].as_object_mut().unwrap().remove("scope"); + for value in [ + missing, + scoped(Value::Null), + scoped(json!({"kind": "other"})), + scoped(json!({"kind": "project"})), + scoped(json!({"kind": "project", "id": "not-a-project-id"})), + scoped(json!({"kind": "general", "id": PROJECT})), + ] { + let bytes = serde_json::to_vec(&value).unwrap(); + assert!(decode(&bytes, ID).is_err(), "accepted {value}"); + let stamp = FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 1, + }; + assert!( + decode_summary(&bytes, ID, stamp).is_err(), + "catalog accepted {value}" + ); + } +} + +#[test] +fn legacy_version_cannot_carry_a_project_ownership_claim() { + let mut value: Value = serde_json::from_slice(LEGACY).unwrap(); + value["scope"] = project(); + let bytes = serde_json::to_vec(&value).unwrap(); + assert!(decode(&bytes, ID).is_err()); + let stamp = FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 1, + }; + assert!(decode_summary(&bytes, ID, stamp).is_err()); +} + +#[test] +fn scoped_metadata_cache_carries_ownership_and_rejects_legacy_cache_versions() { + let bytes = serde_json::to_vec(&scoped(project())).unwrap(); + let stamp = FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 19, + }; + let summary = decode_summary(&bytes, ID, stamp).unwrap().unwrap(); + assert!(summary.matches_stamp(ID, stamp)); + let mut value = serde_json::to_value(&summary).unwrap(); + assert_eq!(value["scope"], project()); + assert_eq!(value["cache_version"], 3); + value["cache_version"] = json!(1); + let old: ChatSummary = serde_json::from_value(value).unwrap(); + assert!(!old.matches_stamp(ID, stamp)); +} + +#[test] +fn general_legacy_catalog_ownership_does_not_rewrite_authoritative_bytes() { + let stamp = FileStamp { + bytes: LEGACY.len() as u64, + modified_nanos: 5, + }; + let summary = decode_summary(LEGACY, ID, stamp).unwrap().unwrap(); + assert_eq!( + serde_json::to_value(summary).unwrap()["scope"], + json!({"kind": "general"}) + ); + assert_eq!(encode(&decode(LEGACY, ID).unwrap()).unwrap(), LEGACY); +} + +#[test] +fn ownership_refusals_are_typed_and_invalid_scope_retains_its_json_cause() { + let mut missing = scoped(project()); + missing["chat"].as_object_mut().unwrap().remove("scope"); + assert!(matches!( + decode(&serde_json::to_vec(&missing).unwrap(), ID), + Err(HistoryCodecError::MissingScope) + )); + let invalid = scoped(json!({"kind": "project", "id": "invalid"})); + let error = decode(&serde_json::to_vec(&invalid).unwrap(), ID).unwrap_err(); + assert!(matches!(error, HistoryCodecError::Json(_))); + let cloned = error.clone(); + fn original(error: &HistoryCodecError) -> &serde_json::Error { + let mut cause = std::error::Error::source(error); + while let Some(current) = cause { + if let Some(json) = current.downcast_ref::() { + return json; + } + cause = current.source(); + } + panic!("the original JSON error must remain in the source chain") + } + assert!(std::ptr::eq(original(&error), original(&cloned))); +} + +#[test] +fn direct_serde_cannot_invent_missing_ownership_or_drop_a_legacy_project_claim() { + let mut missing = scoped(project()); + missing["chat"].as_object_mut().unwrap().remove("scope"); + assert!(serde_json::from_value::(missing).is_err()); + let mut chat = decode(&serde_json::to_vec(&scoped(project())).unwrap(), ID).unwrap(); + chat.version = 1; + assert!(matches!(encode(&chat), Err(HistoryCodecError::LegacyScope))); + assert!(serde_json::to_vec(&chat).is_err()); +} + +#[test] +fn scoped_documents_start_with_an_authoritative_catalog_before_the_payload() { + let chat = decode(&serde_json::to_vec(&scoped(project())).unwrap(), ID).unwrap(); + let value: Value = serde_json::from_slice(&encode(&chat).unwrap()).unwrap(); + assert_eq!(value["catalog"]["id"], ID); + assert_eq!(value["catalog"]["scope"], project()); + assert_eq!(value["catalog"]["title"], "Disk usage"); + assert_eq!(value["catalog"]["has_content"], true); + assert!(value["chat"]["entries"].is_array()); +} + +#[test] +fn catalog_and_payload_ownership_must_agree_before_hydration() { + let chat = decode(&serde_json::to_vec(&scoped(project())).unwrap(), ID).unwrap(); + let mut value: Value = serde_json::from_slice(&encode(&chat).unwrap()).unwrap(); + value["catalog"]["scope"] = json!({"kind": "general"}); + assert!(decode(&serde_json::to_vec(&value).unwrap(), ID).is_err()); +} + +#[test] +fn a_prefix_is_sufficient_without_a_payload_and_rejects_noncanonical_or_oversized_headers() { + let chat = decode(LEGACY, ID).unwrap(); + let mut chat = chat; + chat.version = 2; + let bytes = encode(&chat).unwrap(); + let marker = b",\"chat\":"; + let end = bytes + .windows(marker.len()) + .position(|part| part == marker) + .unwrap() + + marker.len(); + let header = decode_catalog_header(&bytes[..end], ID).unwrap(); + assert_eq!(header.scope(), ContextId::General); + assert!(decode(&bytes[..end], ID).is_err()); + assert!(decode_catalog_header(&bytes[..end - 1], ID).is_err()); + let mut spaced = bytes.clone(); + spaced.insert(1, b' '); + assert!(decode_catalog_header(&spaced, ID).is_err()); + let mut oversized = b"{\"version\":2,\"catalog\":{\"id\":\"".to_vec(); + oversized.extend(vec![b'x'; MAX_HEADER_BYTES]); + assert!(matches!( + decode_catalog_header(&oversized, ID), + Err(HistoryCodecError::HeaderLimit) + )); + assert!(decode_catalog_header(&bytes, "00000000-0000-4000-8000-000000000002").is_err()); +} +#[test] +fn escaped_multibyte_metadata_fits_the_header_bound_and_round_trips() { + let mut chat = SavedChat::for_identity("\u{0}".repeat(512), ID.into(), 1); + chat.header.title = Some("\u{1}".repeat(512)); + chat.header.name = Some("\u{2}".repeat(512)); + chat.model = Some("😀".repeat(512)); + let bytes = encode(&chat).unwrap(); + assert!(decode_catalog_header(&bytes[..bytes.len().min(MAX_HEADER_BYTES)], ID).is_ok()); + let restored = decode(&bytes, ID).unwrap(); + assert_eq!(restored.header.title, chat.header.title); + assert_eq!(restored.header.name, chat.header.name); + assert_eq!(restored.model, chat.model); +} + +#[test] +fn oversized_user_image_replacements_and_raw_serde_have_matching_catalog_titles() { + use crate::{ + model::content::{Content, Image}, + thread::Entry, + }; + let mut chat = SavedChat::for_identity("agent".into(), ID.into(), 1); + chat.entries.push(Entry::User(vec![ + Content::Image(Image::new( + "A".repeat(MAX_SAVED_IMAGE_BYTES + 1), + "image/png", + )), + Content::text("literal question after the image"), + ])); + let raw = serde_json::to_vec(&chat).unwrap(); + let decoded = decode(&raw, ID).unwrap(); + let raw_summary = decode_catalog_header(&raw, ID) + .unwrap() + .summary(FileStamp { + bytes: raw.len() as u64, + modified_nanos: 0, + }) + .unwrap(); + assert_eq!(raw_summary.title(), "literal question after the image"); + assert!( + matches!(&decoded.entries[0], Entry::User(blocks) if matches!(blocks[0], Content::Image(_))) + ); + let bytes = encode(&chat).unwrap(); + let decoded = decode(&bytes, ID).unwrap(); + let summary = decode_catalog_header(&bytes, ID) + .unwrap() + .summary(FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 0, + }) + .unwrap(); + assert_eq!(summary.title(), "[Image not kept in history]"); + assert_eq!(ChatSummary::from_chat(&chat).title(), summary.title()); + assert!( + matches!(&decoded.entries[0], Entry::User(blocks) if matches!(&blocks[0], Content::Text(text) if text.text == "[Image not kept in history]")) + ); + assert_eq!( + encode_shared(&SharedChat { + metadata: chat, + queue: Vec::new() + }) + .unwrap(), + bytes + ); +} + +#[test] +fn an_image_only_chat_can_reopen_after_its_large_image_is_omitted() { + use crate::{ + model::content::{Content, Image}, + thread::Entry, + }; + let mut chat = SavedChat::for_identity("agent".into(), ID.into(), 1); + chat.entries + .push(Entry::User(vec![Content::Image(Image::new( + "A".repeat(MAX_SAVED_IMAGE_BYTES + 1), + "image/png", + ))])); + assert!(decode(&serde_json::to_vec(&chat).unwrap(), ID).is_ok()); + let bytes = encode(&chat).unwrap(); + let restored = decode(&bytes, ID).unwrap(); + assert_eq!( + ChatSummary::from_chat(&restored).title(), + "[Image not kept in history]" + ); + let stamp = FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 0, + }; + assert_eq!( + decode_summary(&bytes, ID, stamp).unwrap().unwrap().title(), + "[Image not kept in history]" + ); +} +#[test] +fn catalog_rows_preserve_the_transcript_version_independently_of_cache_schema() { + let id = "00000000-0000-0000-0000-000000000001"; + let bytes = br#"{"version":1,"id":"00000000-0000-0000-0000-000000000001","agent_id":"codex","updated":1,"draft":"legacy","entries":[]}"#; + let legacy = decode_summary( + bytes, + id, + FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 0, + }, + ) + .unwrap() + .unwrap(); + assert_eq!(legacy.transcript_version().wire(), 1); + let current = ChatSummary::from_chat(&SavedChat::for_identity("codex".into(), id.into(), 1)); + assert_eq!(current.transcript_version().wire(), 2); + let mut changed = serde_json::to_value(current).unwrap(); + assert_eq!(changed["cache_version"], 3); + changed["transcript_version"] = serde_json::json!(99); + assert!(serde_json::from_value::(changed).is_err()); +} + +mod environments; + +#[test] +fn owned_preparation_preserves_the_body_and_matches_existing_format_validation() { + let legacy = decode(LEGACY, ID).unwrap(); + let scoped = SavedChat::for_identity("agent".into(), ID.into(), 10); + let environment = SavedChat::for_environment( + "agent".into(), + ID.into(), + 10, + crate::local_environment::ChatScope::new( + crate::local_environment::LocalEnvironment::ThisComputer, + ContextId::General, + ), + ); + for mut chat in [legacy, scoped, environment] { + chat.entries + .push(crate::thread::Entry::Agent("kept owned body".into())); + let expected = prepare(chat.clone()).unwrap(); + let rows = chat.entries.as_ptr(); + let actual = prepare_owned(chat).unwrap(); + assert_eq!(actual.entries.as_ptr(), rows); + assert_eq!(encode(&actual).unwrap(), encode(&expected).unwrap()); + assert_eq!(actual.resident_bytes(), expected.resident_bytes()); + } + let invalid = SavedChat::for_identity("agent".into(), "invalid".into(), 1); + assert!(matches!( + prepare_owned(invalid), + Err(HistoryCodecError::InvalidId(_)) + )); + let archived = SavedChat::archive_draft(ID.into(), "agent".into(), "draft".into(), 1); + assert!(matches!( + prepare_owned(archived), + Err(HistoryCodecError::ArchivedPatch) + )); + let mut oversized = SavedChat::for_identity("agent".into(), ID.into(), 1); + oversized.entries = vec![crate::thread::Entry::Agent(String::new()); MAX_SAVED_ENTRIES + 1]; + assert!(matches!( + prepare_owned(oversized), + Err(HistoryCodecError::CollectionLimit) + )); +} diff --git a/crates/ai/src/history/scope_tests/environments.rs b/crates/ai/src/history/scope_tests/environments.rs new file mode 100644 index 00000000..4994f66b --- /dev/null +++ b/crates/ai/src/history/scope_tests/environments.rs @@ -0,0 +1,195 @@ +use super::*; +use crate::{ + execution_origin::ChatKey, + local_environment::{ChatScope, LocalEnvironment, LocalEnvironmentAccess}, +}; +use nocterm_hubs_model::{ + HubAddress, HubId, + source_identity::{ + AccountIdentity, AccountScope, Fingerprint, HubIdentity, LegacyConfiguredAccount, + LocalAccountPartition, LocalAccountSourceFactory, NamespaceId, + }, + testing::ScriptedLocalAccountFactory, +}; +use std::sync::Arc; + +fn partition(authority: u8, account_key: u8) -> LocalAccountPartition { + LocalAccountPartition::Stable(AccountScope::new( + HubIdentity::new( + NamespaceId::new("same-instance-uuid").unwrap(), + Fingerprint::from_bytes([authority; 32]), + ), + AccountIdentity::new( + NamespaceId::new("same-account-uuid").unwrap(), + Fingerprint::from_bytes([account_key; 32]), + ), + )) +} +fn scope(authority: u8, key: u8) -> ChatScope { + ChatScope::new( + LocalEnvironment::HubAccount(partition(authority, key)), + ContextId::General, + ) +} +fn record(scope: ChatScope) -> SavedChat { + let mut chat = SavedChat::for_environment("agent".into(), ID.into(), 1, scope); + chat.entries + .push(crate::thread::Entry::Agent("saved reply".into())); + chat +} + +#[test] +fn environment_and_project_are_independent_coordinates_of_chat_identity() { + let a = ChatKey::in_environment(scope(1, 2), ID.into()); + let b = ChatKey::in_environment(scope(9, 2), ID.into()); + let c = ChatKey::in_environment(scope(1, 9), ID.into()); + let project: ContextId = serde_json::from_value(project()).unwrap(); + let project = + ChatKey::in_environment(ChatScope::new(a.environment().clone(), project), ID.into()); + assert_ne!(a, b); + assert_ne!(a, c); + assert_ne!(a, project); + assert_eq!(a.scope(), ContextId::General); + assert_ne!(a.environment().storage_key(), b.environment().storage_key()); + assert_ne!(a.environment().storage_key(), c.environment().storage_key()); + assert_eq!(a.environment().storage_key().len(), 64); +} + +#[test] +fn version_three_has_matching_bounded_environment_header_and_full_body() { + for scope in [scope(1, 2), ChatScope::this_computer(ContextId::General)] { + let chat = record(scope.clone()); + let bytes = encode(&chat).unwrap(); + assert!(bytes.starts_with(b"{\"version\":3,\"catalog\":")); + let header = decode_catalog_header(&bytes, ID).unwrap(); + assert_eq!(header.chat_scope(), scope); + assert_eq!(header.transcript_version(), TranscriptVersion::ENVIRONMENT); + let decoded = decode(&bytes, ID).unwrap(); + assert_eq!(decoded.header.chat_scope(), scope); + let summary = decode_summary( + &bytes, + ID, + FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 0, + }, + ) + .unwrap() + .unwrap(); + assert_eq!(summary.chat_scope(), scope); + assert_eq!(summary.transcript_version(), TranscriptVersion::ENVIRONMENT); + let value: Value = serde_json::from_slice(&bytes).unwrap(); + assert_eq!( + value["catalog"]["environment"], + value["chat"]["environment"] + ); + assert_eq!(encode(&prepare(decoded).unwrap()).unwrap(), bytes); + } +} + +#[test] +fn version_three_refuses_missing_unknown_moved_or_mismatched_environment() { + let valid = serde_json::to_value(record(scope(1, 2))).unwrap(); + let mut missing = valid.clone(); + missing["catalog"] + .as_object_mut() + .unwrap() + .remove("environment"); + let mut missing_body = valid.clone(); + missing_body["chat"] + .as_object_mut() + .unwrap() + .remove("environment"); + let mut changed = valid.clone(); + changed["chat"]["environment"] = serde_json::to_value(scope(9, 2).environment()).unwrap(); + let mut unknown = valid.clone(); + unknown["catalog"]["environment"] = json!({"kind":"future"}); + let mut malformed = valid.clone(); + malformed["chat"]["environment"] = + json!({"kind":"hub_account","account":{"kind":"stable","identity":{}}}); + for value in [missing, missing_body, changed, unknown, malformed] { + let bytes = serde_json::to_vec(&value).unwrap(); + assert!(decode(&bytes, ID).is_err()); + assert!( + decode_summary( + &bytes, + ID, + FileStamp { + bytes: bytes.len() as u64, + modified_nanos: 0 + } + ) + .is_err() + ); + } +} + +#[test] +fn legacy_versions_never_acquire_hub_account_membership() { + let original = decode(LEGACY, ID).unwrap(); + assert!(original.header.environment.is_this_computer()); + assert_eq!(encode(&original).unwrap(), LEGACY); + for version in [1, 2] { + let mut moved = record(scope(1, 2)); + moved.version = version; + assert!(matches!( + encode(&moved), + Err(HistoryCodecError::LegacyEnvironment) + )); + } + let mut version_two = SavedChat::for_identity("agent".into(), ID.into(), 1); + version_two + .entries + .push(crate::thread::Entry::Agent("local".into())); + let value = serde_json::to_value(&version_two).unwrap(); + assert!(value["catalog"].get("environment").is_none()); + assert_eq!( + decode(&encode(&version_two).unwrap(), ID) + .unwrap() + .header + .environment, + LocalEnvironment::ThisComputer + ); +} + +#[test] +fn new_capture_after_unlock_never_revives_an_old_a_to_b_to_a_lease() { + let factory = Arc::new(ScriptedLocalAccountFactory::default()); + assert!(factory.capture().is_err()); + factory.sign_in(partition(1, 2)); + let original = LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap(); + let refreshed = LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap(); + assert_eq!(original.environment(), refreshed.environment()); + assert!(original.check_current().is_ok()); + factory.sign_in(partition(1, 9)); + assert!(original.check_current().is_err()); + factory.sign_in(partition(1, 2)); + let reopened = LocalEnvironmentAccess::capture(factory.capture().unwrap()).unwrap(); + assert_eq!(original.environment(), reopened.environment()); + assert!(original.check_current().is_err()); + assert!(reopened.check_current().is_ok()); + factory.close(); + assert!(reopened.check_current().is_err()); +} + +#[test] +fn legacy_configured_partition_is_explicit_and_does_not_claim_stable_alias_identity() { + let legacy = |host: &str| { + LocalEnvironment::HubAccount(LocalAccountPartition::LegacyConfigured( + LegacyConfiguredAccount::new( + HubId::from(HubAddress::parse(host).unwrap()), + NamespaceId::new("account").unwrap(), + Fingerprint::from_bytes([8; 32]), + ), + )) + }; + assert_eq!(legacy("https://HUB.home/"), legacy("hub.home:7443")); + assert_ne!(legacy("hub.home"), legacy("alias.home")); + assert_ne!( + legacy("hub.home").storage_key(), + scope(1, 2).environment().storage_key() + ); + let mut value = serde_json::to_value(scope(1, 2).environment()).unwrap(); + value["account"]["identity"]["hub"]["instance"] = json!("x".repeat(129)); + assert!(serde_json::from_value::(value).is_err()); +} diff --git a/crates/ai/src/history/version.rs b/crates/ai/src/history/version.rs new file mode 100644 index 00000000..8bc86abe --- /dev/null +++ b/crates/ai/src/history/version.rs @@ -0,0 +1,43 @@ +//! Transcript format facts are independent of the rebuildable cache schema. +use super::{ContextId, HistoryCodecError}; +use serde::{Deserialize, Serialize}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(try_from = "u32", into = "u32")] +pub struct TranscriptVersion(u32); +impl TranscriptVersion { + pub const LEGACY: Self = Self(super::LEGACY_VERSION); + pub const ENVIRONMENT: Self = Self(super::ENVIRONMENT_VERSION); + pub const SCOPED: Self = Self(super::VERSION); + pub const fn wire(self) -> u32 { + self.0 + } + pub(super) fn observed(version: u32) -> Self { + Self(version) + } + pub(super) fn require_environment( + self, + environment: &crate::local_environment::LocalEnvironment, + ) -> Result<(), HistoryCodecError> { + super::header::validate_environment(self.0, environment) + } + pub(super) fn require_scope(self, scope: ContextId) -> Result<(), HistoryCodecError> { + super::header::resolve_scope(self.0, Some(scope)).map(|_| ()) + } +} +impl TryFrom for TranscriptVersion { + type Error = HistoryCodecError; + fn try_from(version: u32) -> Result { + match version { + super::LEGACY_VERSION | super::VERSION | super::ENVIRONMENT_VERSION => { + Ok(Self(version)) + } + other => Err(HistoryCodecError::UnsupportedVersion(other)), + } + } +} +impl From for u32 { + fn from(version: TranscriptVersion) -> Self { + version.wire() + } +} diff --git a/crates/ai/src/history/version_one_tests.rs b/crates/ai/src/history/version_one_tests.rs index 5b6098b7..0522e6ae 100644 --- a/crates/ai/src/history/version_one_tests.rs +++ b/crates/ai/src/history/version_one_tests.rs @@ -17,19 +17,33 @@ fn a_canonical_saved_chat_reencodes_byte_for_byte() { assert_eq!(encode(&chat).unwrap(), CANONICAL); } +#[test] +fn a_shared_snapshot_keeps_canonical_bytes_with_its_separately_held_queue() { + let mut chat = decode(CANONICAL, ID).unwrap(); + let queue = std::mem::take(&mut chat.queue) + .into_iter() + .map(std::sync::Arc::new) + .collect(); + let shared = SharedChat { + metadata: chat, + queue, + }; + assert_eq!(encode_shared(&shared).unwrap(), CANONICAL); +} + #[test] fn a_canonical_saved_chat_keeps_every_field() { let chat = decode(CANONICAL, ID).unwrap(); - assert_eq!(chat.agent_id, "codex"); - assert_eq!(chat.title.as_deref(), Some("Disk usage")); - assert_eq!(chat.name.as_deref(), Some("Disks")); - assert!(chat.pinned); + assert_eq!(chat.header.agent_id, "codex"); + assert_eq!(chat.header.title.as_deref(), Some("Disk usage")); + assert_eq!(chat.header.name.as_deref(), Some("Disks")); + assert!(chat.header.pinned); assert_eq!(chat.session_id.as_deref(), Some("session-1")); assert_eq!(chat.model.as_deref(), Some("opus")); assert_eq!(chat.mode.as_deref(), Some("plan")); assert!(chat.pending_history); assert_eq!(chat.draft.as_deref(), Some("unsent \n text")); - assert_eq!(chat.updated, 1_700_000_000); + assert_eq!(chat.header.updated, 1_700_000_000); assert_eq!(chat.entries.len(), 6); assert_eq!(chat.times, [10, 11, 12, 13, 14, 15]); assert_eq!(chat.queue.len(), 1); @@ -144,11 +158,11 @@ fn summaries_agree_with_the_fully_decoded_chat() { #[test] fn other_versions_and_identities_are_rejected() { let mut other: Value = serde_json::from_slice(CANONICAL).unwrap(); - other["version"] = json!(2); + other["version"] = json!(99); let bytes = serde_json::to_vec(&other).unwrap(); assert!(matches!( decode(&bytes, ID), - Err(HistoryCodecError::UnsupportedVersion(2)) + Err(HistoryCodecError::UnsupportedVersion(99)) )); assert!(matches!( decode(CANONICAL, "00000000-0000-4000-8000-0000000000a2"), diff --git a/crates/ai/src/history/wire.rs b/crates/ai/src/history/wire.rs index 68bd884a..cf3bc0e6 100644 --- a/crates/ai/src/history/wire.rs +++ b/crates/ai/src/history/wire.rs @@ -1,55 +1,65 @@ //! Borrowed, bounded persistence view. JSON and byte budgeting use the same serializer. +use super::catalog_header::{CatalogHeader, Preview}; use super::{MAX_SAVED_ENTRIES, MAX_SAVED_IMAGE_BYTES, SavedChat, SavedPrompt}; use crate::model::content::Content; use crate::model::tools::ToolRecord; use crate::thread::Entry; -use serde::{Serialize, Serializer, ser::SerializeSeq}; +use serde::{ + Serialize, Serializer, + ser::{SerializeMap, SerializeSeq}, +}; use std::sync::Arc; -#[derive(Serialize)] pub(super) struct Wire<'a, Q: Serialize + ?Sized> { version: u32, - id: &'a str, - agent_id: &'a str, - #[serde(skip_serializing_if = "Option::is_none")] - title: &'a Option, - #[serde(skip_serializing_if = "Option::is_none")] - name: &'a Option, - #[serde(skip_serializing_if = "std::ops::Not::not")] - pinned: bool, - #[serde(skip_serializing_if = "Option::is_none")] + header: &'a super::SavedHeader, session_id: &'a Option, - #[serde(skip_serializing_if = "Option::is_none")] workdir: &'a Option, - #[serde(skip_serializing_if = "Option::is_none")] model: &'a Option, - #[serde(skip_serializing_if = "std::collections::BTreeMap::is_empty")] config: &'a crate::session_config::Choices, - #[serde(skip_serializing_if = "Option::is_none")] mode: &'a Option, - #[serde(skip_serializing_if = "std::ops::Not::not")] pending_history: bool, - #[serde(skip_serializing_if = "Option::is_none")] draft: &'a Option, - updated: u64, entries: Entries<'a>, - #[serde(skip_serializing_if = "Option::is_none")] queue: Option<&'a Q>, - #[serde(skip_serializing_if = "<[super::SavedAttachment]>::is_empty")] attachments: &'a [super::SavedAttachment], - #[serde(skip_serializing_if = "<[u64]>::is_empty")] times: &'a [u64], } impl<'a, Q: Serialize + ?Sized> Wire<'a, Q> { pub(super) fn new(chat: &'a SavedChat, queue: Option<&'a Q>) -> Self { let skip = chat.entries.len().saturating_sub(MAX_SAVED_ENTRIES); + Self::from_entries( + chat, + queue, + Entries { + rows: &chat.entries[skip..], + bounded: true, + preview: Preview::persisted(&chat.entries[skip..]), + }, + chat.times.get(skip..).unwrap_or_default(), + ) + } + pub(super) fn budget(chat: &'a SavedChat, queue: Option<&'a Q>, preview: Preview) -> Self { + Self::from_entries( + chat, + queue, + Entries { + rows: &chat.entries, + bounded: true, + preview, + }, + &chat.times, + ) + } + fn from_entries( + chat: &'a SavedChat, + queue: Option<&'a Q>, + entries: Entries<'a>, + times: &'a [u64], + ) -> Self { Self { version: chat.version, - id: &chat.id, - agent_id: &chat.agent_id, - title: &chat.title, - name: &chat.name, - pinned: chat.pinned, + header: &chat.header, session_id: &chat.session_id, workdir: &chat.workdir, model: &chat.model, @@ -57,20 +67,122 @@ impl<'a, Q: Serialize + ?Sized> Wire<'a, Q> { mode: &chat.mode, pending_history: chat.pending_history, draft: &chat.draft, - updated: chat.updated, - entries: Entries(&chat.entries[skip..]), + entries, queue, attachments: &chat.attachments, - times: chat.times.get(skip..).unwrap_or_default(), + times, + } + } +} +impl<'a> Wire<'a, Vec> { + pub(super) fn complete(chat: &'a SavedChat) -> Self { + Self::from_entries( + chat, + (!chat.queue.is_empty()).then_some(&chat.queue), + Entries { + rows: &chat.entries, + bounded: false, + preview: Preview::new(&chat.entries), + }, + &chat.times, + ) + } +} +impl Serialize for Wire<'_, Q> { + fn serialize(&self, serializer: S) -> Result { + super::header::resolve_scope(self.version, Some(self.header.scope)) + .map_err(serde::ser::Error::custom)?; + super::header::validate_environment(self.version, &self.header.environment) + .map_err(serde::ser::Error::custom)?; + if self.version == super::LEGACY_VERSION { + return Flat(self).serialize(serializer); } + let catalog = CatalogHeader::from_parts( + self.header, + self.model, + &self.entries.preview, + self.draft.as_deref(), + self.queue.is_some(), + self.version, + ); + if super::size::encoded_len(&catalog).map_err(serde::ser::Error::custom)? + + super::prefix::START.len() + + super::prefix::PAYLOAD.len() + > super::MAX_HEADER_BYTES + { + return Err(serde::ser::Error::custom( + super::HistoryCodecError::HeaderLimit, + )); + } + let mut map = serializer.serialize_map(Some(3))?; + map.serialize_entry("version", &self.version)?; + map.serialize_entry("catalog", &catalog)?; + map.serialize_entry("chat", &Flat(self))?; + map.end() } } -pub(super) struct Entries<'a>(pub &'a [Entry]); +struct Flat<'a, 'b, Q: Serialize + ?Sized>(&'b Wire<'a, Q>); +impl Serialize for Flat<'_, '_, Q> { + fn serialize(&self, serializer: S) -> Result { + let wire = self.0; + let mut map = serializer.serialize_map(None)?; + map.serialize_entry("version", &wire.version)?; + wire.header.serialize_prefix(&mut map)?; + if wire.version == super::ENVIRONMENT_VERSION { + map.serialize_entry("environment", &wire.header.environment)?; + } + if wire.version != super::LEGACY_VERSION { + map.serialize_entry("scope", &wire.header.scope)?; + } + optional(&mut map, "session_id", wire.session_id)?; + optional(&mut map, "workdir", wire.workdir)?; + optional(&mut map, "model", wire.model)?; + if !wire.config.is_empty() { + map.serialize_entry("config", wire.config)?; + } + optional(&mut map, "mode", wire.mode)?; + if wire.pending_history { + map.serialize_entry("pending_history", &wire.pending_history)?; + } + optional(&mut map, "draft", wire.draft)?; + map.serialize_entry("updated", &wire.header.updated)?; + map.serialize_entry("entries", &wire.entries)?; + if let Some(queue) = wire.queue { + map.serialize_entry("queue", queue)?; + } + if !wire.attachments.is_empty() { + map.serialize_entry("attachments", wire.attachments)?; + } + if !wire.times.is_empty() { + map.serialize_entry("times", wire.times)?; + } + map.end() + } +} +fn optional( + map: &mut M, + key: &'static str, + value: &Option, +) -> Result<(), M::Error> { + if let Some(value) = value { + map.serialize_entry(key, value)?; + } + Ok(()) +} +pub(super) struct Entries<'a> { + rows: &'a [Entry], + bounded: bool, + preview: Preview, +} impl Serialize for Entries<'_> { fn serialize(&self, serializer: S) -> Result { - let mut seq = serializer.serialize_seq(Some(self.0.len()))?; - for entry in self.0 { - seq.serialize_element(&bounded(entry))?; + let mut seq = serializer.serialize_seq(Some(self.rows.len()))?; + for entry in self.rows { + if self.bounded { + seq.serialize_element(&bounded(entry))?; + } else { + seq.serialize_element(entry)?; + } } seq.end() } @@ -108,7 +220,7 @@ impl Serialize for Block<'_> { fn serialize(&self, serializer: S) -> Result { match self.0 { Content::Image(image) if image.data.len() > MAX_SAVED_IMAGE_BYTES => { - Content::text("[Image not kept in history]").serialize(serializer) + Content::text(super::OMITTED_IMAGE_TEXT).serialize(serializer) } block => block.serialize(serializer), } @@ -143,7 +255,7 @@ pub(super) fn clone_entries(entries: &[Entry]) -> Vec { fn clone_block(block: &Content) -> Content { match block { Content::Image(image) if image.data.len() > MAX_SAVED_IMAGE_BYTES => { - Content::text("[Image not kept in history]") + Content::text(super::OMITTED_IMAGE_TEXT) } block => block.clone(), } diff --git a/crates/ai/src/images.rs b/crates/ai/src/images.rs index c057a100..6867f3c5 100644 --- a/crates/ai/src/images.rs +++ b/crates/ai/src/images.rs @@ -28,6 +28,38 @@ pub enum PromptImageError { #[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)] #[error("At most 8 images and 20 MiB total")] pub struct ImageCollectionError; +/// Remaining admission for one image operation, captured before native reads/decodes. +#[derive(Clone, Copy, Debug)] +pub struct ImageCollectionBudget { + count: usize, + bytes: usize, +} +impl ImageCollectionBudget { + pub fn remaining(images: &[PromptImage]) -> Result { + validate_collection(images)?; + Ok(Self { + count: images.len(), + bytes: images.iter().map(|image| image.bytes().len()).sum(), + }) + } + pub fn check_count(self, incoming: usize) -> Result<(), ImageCollectionError> { + if incoming > MAX_IMAGES.saturating_sub(self.count) { + Err(ImageCollectionError) + } else { + Ok(()) + } + } + /// Refuses before decoding or encoding the incoming image. + pub fn admit(&mut self, bytes: usize) -> Result<(), ImageCollectionError> { + self.check_count(1)?; + if bytes > MAX_TOTAL_IMAGE_BYTES.saturating_sub(self.bytes) { + return Err(ImageCollectionError); + } + self.count += 1; + self.bytes += bytes; + Ok(()) + } +} #[derive(Clone, Debug)] pub struct PromptImage { mime_type: String, @@ -68,6 +100,10 @@ impl PromptImage { pub fn bytes(&self) -> &[u8] { &self.data } + /// Shared immutable source for retained background decoding and preview identity. + pub fn shared_bytes(&self) -> std::sync::Arc<[u8]> { + self.data.clone() + } pub fn mime_type(&self) -> &str { &self.mime_type } diff --git a/crates/ai/src/lib.rs b/crates/ai/src/lib.rs index d668cfdb..47022ae3 100644 --- a/crates/ai/src/lib.rs +++ b/crates/ai/src/lib.rs @@ -1,27 +1,33 @@ -//! Agent contracts, neutral protocol records, bounded context rules and version-one history formats. +//! Agent contracts, neutral protocol records, bounded context rules and scoped history formats. //! //! Owns: neutral `model` records (the only agent vocabulary other crates see), agent //! profiles, catalog and settings resolution (`agents`), configuration rules, transcript reduction, session/tool rules, pure sandbox policy, -//! bounded history codecs and capability/storage ports. +//! bounded history codecs, immutable scoped execution origins and capability/storage ports. +//! The terminal tool catalog includes file transfers and shared agent-facing terminal, +//! command, observation and transfer reports; display data never grants execution authority. +//! File tools for terminals that run no programs are pure rules over a captured `RemoteFs`; +//! catalog groups and the chat's `ToolOffer` decide which tools the bridge lists and admits. +//! Image data is immutable and shared across projections; live transcript admission uses incremental pre-mutation costs. //! Does not own: per-chat configuration/composer state, native workspace/sandbox lookup, //! history/favorites files or persistence scheduling; native capabilities and repositories are adapters. //! Ports used: none for history codecs. Ports provided: metadata::ChatMetadataSource, //! AgentConnector, ToolBridge, -//! AgentStateRepository, ChatRepository, usage::AccountUsageSource, +//! AgentStateRepository, ChatRepository and bounded PreparedChatLoad, //! workspace::WorkdirResolver, sandbox::SandboxProbe and //! images::{ImageInspector, ImageThumbnailer}. //! Image preflight and encoded admission are pure; native decoding is injected. //! ImageCodecCause reuses core shared causes without exposing a native codec type. -//! Account usage diagnostics retain typed native I/O causes through foundation SharedError; -//! narrow domain constructors preserve operation category and path. //! Workspace preparation and sandbox spawn diagnostics retain typed shared native I/O; //! adapter constructors preserve classification without changing policy or mount authority. //! Settings section: ai. Globals: none. //! Invariants: saved records validate identity/version and encoded/structural limits. //! Snapshot framing and transcript timestamps accept supplied identity/time. //! Native chat allocation and wall-clock facts are provided by ChatMetadataSource. -//! No agent protocol SDK type appears in this crate; persisted records serialize as the -//! version-one transcript JSON and `nocterm-acp` maps the SDK to them (ADR 0058). +//! No agent protocol SDK type appears in this crate; `nocterm-acp` maps the SDK to neutral records. +//! Legacy transcripts preserve their version-one bytes. Newly framed documents use version +//! three with explicit device/account environment and independent context ownership. +//! LocalAccountSource leases are captured from an authenticated native owner; persistence identity +//! never grants execution access. Version-two records remain ThisComputer only. //! Agent launch settings validation is pure; native text parsing and page controls belong to Agent. //! No concrete agent is named here: built-in profiles are injected as an `AgentCatalog`. pub mod agents; @@ -30,11 +36,14 @@ pub mod commands; pub mod connection; pub mod context; pub mod env; +pub mod execution_origin; pub mod favorites; pub mod history; pub mod images; +pub mod local_environment; pub mod mcp; pub mod metadata; +pub mod prompt_files; pub mod redact; pub mod sandbox; pub mod session; diff --git a/crates/ai/src/local_environment.rs b/crates/ai/src/local_environment.rs new file mode 100644 index 00000000..46806c36 --- /dev/null +++ b/crates/ai/src/local_environment.rs @@ -0,0 +1,119 @@ +//! Device-local chat partitions, separate from General/Project membership. +use crate::history::ContextId; +use nocterm_hubs_model::source_identity::{ + LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, +}; +use serde::{Deserialize, Serialize}; +use std::sync::Arc; + +/// Descriptive persistence owner. This does not grant native or Cloud authority. +#[derive(Clone, Debug, Default, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde( + tag = "kind", + content = "account", + rename_all = "snake_case", + deny_unknown_fields +)] +pub enum LocalEnvironment { + #[default] + ThisComputer, + HubAccount(LocalAccountPartition), +} +impl LocalEnvironment { + /// Versioned local filename key only; the authoritative header is checked too. + /// Display addresses are used solely by the explicit legacy configured variant. + pub fn storage_key(&self) -> String { + use sha2::{Digest as _, Sha256}; + let mut hash = Sha256::new(); + let mut part = |value: &[u8]| { + hash.update((value.len() as u64).to_be_bytes()); + hash.update(value); + }; + part(b"nocterm/local-history-partition/v3"); + match self { + Self::ThisComputer => part(b"this-computer"), + Self::HubAccount(LocalAccountPartition::Stable(scope)) => { + part(b"stable"); + part(scope.hub().instance().as_str().as_bytes()); + part(scope.hub().authority().as_bytes()); + part(scope.account().id().as_str().as_bytes()); + part(scope.account().key().as_bytes()); + } + Self::HubAccount(LocalAccountPartition::LegacyConfigured(account)) => { + part(b"legacy-configured"); + part(account.hub().to_string().as_bytes()); + part(account.account().as_str().as_bytes()); + part(account.key().as_bytes()); + } + } + let mut output = String::with_capacity(64); + for byte in hash.finalize() { + use std::fmt::Write as _; + let _ = write!(output, "{byte:02x}"); + } + output + } + pub fn is_this_computer(&self) -> bool { + matches!(self, Self::ThisComputer) + } +} + +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub struct ChatScope { + environment: LocalEnvironment, + context: ContextId, +} +impl ChatScope { + pub fn new(environment: LocalEnvironment, context: ContextId) -> Self { + Self { + environment, + context, + } + } + pub fn this_computer(context: ContextId) -> Self { + Self::new(LocalEnvironment::ThisComputer, context) + } + pub fn environment(&self) -> &LocalEnvironment { + &self.environment + } + pub fn context(&self) -> ContextId { + self.context + } +} +impl From for ChatScope { + fn from(context: ContextId) -> Self { + Self::this_computer(context) + } +} + +/// A retained source capture. The original lease never becomes valid again +/// merely because a later sign-in selects the same persistent partition. +#[derive(Clone, Default)] +pub struct LocalEnvironmentAccess { + source: Option>, + environment: Arc, +} +impl LocalEnvironmentAccess { + pub fn this_computer() -> Self { + Self::default() + } + pub fn capture(source: Arc) -> Result { + source.check_current()?; + Ok(Self { + environment: Arc::new(LocalEnvironment::HubAccount(source.partition())), + source: Some(source), + }) + } + pub fn environment(&self) -> &LocalEnvironment { + &self.environment + } + pub fn check_current(&self) -> Result<(), LocalAccountSourceError> { + if let Some(source) = &self.source { + source.check_current()?; + if *self.environment != LocalEnvironment::HubAccount(source.partition()) { + return Err(LocalAccountSourceError::Stale); + } + } + Ok(()) + } +} diff --git a/crates/ai/src/mcp.rs b/crates/ai/src/mcp.rs index 664adedd..91b440ed 100644 --- a/crates/ai/src/mcp.rs +++ b/crates/ai/src/mcp.rs @@ -32,8 +32,13 @@ pub fn tool_result(id: Value, result: Result) -> Value { } } } +/// The notification that tells a connected agent to list the tools again. +pub fn list_changed() -> Value { + json!({"jsonrpc":"2.0","method":"notifications/tools/list_changed"}) +} impl McpSession { - pub fn handle(&mut self, line: &str) -> McpStep { + /// Answers one line under `offer`: only the offered tools are listed or admitted. + pub fn handle(&mut self, line: &str, offer: ToolOffer) -> McpStep { if line.len() > MAX_MCP_LINE { return error(Value::Null, -32600, "Request exceeds limit"); } @@ -57,27 +62,36 @@ impl McpSession { "initialize" => { self.initialized = true; McpStep::Reply( - json!({"jsonrpc":"2.0","id":id,"result":{"protocolVersion":"2024-11-05","serverInfo":{"name":"nocterm","version":"1"},"capabilities":{"tools":{}}}}), + json!({"jsonrpc":"2.0","id":id,"result":{"protocolVersion":"2024-11-05","serverInfo":{"name":"nocterm","version":"1"},"capabilities":{"tools":{"listChanged":true}}}}), ) } "ping" => McpStep::Reply(json!({"jsonrpc":"2.0","id":id,"result":{}})), _ if !self.initialized => error(id, -32002, "Initialize first"), "tools/list" => { - let tools: Vec<_> = catalog::TOOLS.iter().map(|spec| { + let tools: Vec<_> = offer.tools().map(|spec| { json!({"name":spec.name,"description":spec.description,"inputSchema":(spec.schema)()}) }).collect(); McpStep::Reply(json!({"jsonrpc":"2.0","id":id,"result":{"tools":tools}})) } - "tools/call" => call(id, &value["params"]), + "tools/call" => call(id, &value["params"], offer), _ => error(id, -32601, "Unknown method"), } } } -fn call(id: Value, params: &Value) -> McpStep { +fn call(id: Value, params: &Value, offer: ToolOffer) -> McpStep { let args = params.get("arguments").cloned().unwrap_or(json!({})); let tool = params["name"].as_str().unwrap_or_default().to_owned(); - let call = catalog::get(&tool).and_then(|spec| (spec.parse)(args.clone())); + let spec = catalog::get(&tool); + if spec.is_some_and(|spec| !offer.offers(spec.group)) { + return McpStep::Rejected { + id, + tool, + arguments: args, + error: "File tools are offered only while a terminal with file_tools: true is attached; call list_terminals".into(), + }; + } + let call = spec.and_then(|spec| (spec.parse)(args.clone())); match call { Some(call) => match call.validate() { Ok(()) => McpStep::Call { diff --git a/crates/ai/src/model/content.rs b/crates/ai/src/model/content.rs index 2113b62f..1a171931 100644 --- a/crates/ai/src/model/content.rs +++ b/crates/ai/src/model/content.rs @@ -3,6 +3,9 @@ use super::extensions::Extensions; use serde::{Deserialize, Serialize}; use serde_with::{DefaultOnError, VecSkipError, serde_as, skip_serializing_none}; +#[path = "image_data.rs"] +mod image_data; +pub use image_data::ImageData; #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] @@ -61,7 +64,7 @@ pub struct Image { #[serde_as(deserialize_as = "DefaultOnError")] #[serde(default)] pub annotations: Option, - pub data: String, + pub data: ImageData, pub mime_type: String, #[serde_as(deserialize_as = "DefaultOnError")] #[serde(default)] @@ -74,7 +77,7 @@ impl Image { pub fn new(data: impl Into, mime_type: impl Into) -> Self { Self { annotations: None, - data: data.into(), + data: data.into().into(), mime_type: mime_type.into(), uri: None, meta: None, diff --git a/crates/ai/src/model/controls.rs b/crates/ai/src/model/controls.rs index d3deab87..bae8d3ae 100644 --- a/crates/ai/src/model/controls.rs +++ b/crates/ai/src/model/controls.rs @@ -120,6 +120,9 @@ pub enum ConfigCategory { Model, ModelConfig, ThoughtLevel, + /// A server an agent that runs elsewhere may sign in to during the chat: a flag, on while + /// the server is attached to the chat. The option's description is the server's address. + Server, Other(String), } diff --git a/crates/ai/src/model/image_data.rs b/crates/ai/src/model/image_data.rs new file mode 100644 index 00000000..11db7418 --- /dev/null +++ b/crates/ai/src/model/image_data.rs @@ -0,0 +1,80 @@ +//! Immutable encoded image data: clones share their allocation and wire bytes stay a string. +use serde::{Deserialize, Deserializer, Serialize, Serializer}; +use std::{ops::Deref, sync::Arc}; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ImageData(Arc); +impl ImageData { + /// The normalized encoded allocation and its owned String/Arc framing. + pub fn retained_bytes(&self) -> usize { + self.0 + .capacity() + .saturating_add(std::mem::size_of::()) + .saturating_add(2 * std::mem::size_of::()) + } +} +impl Deref for ImageData { + type Target = str; + fn deref(&self) -> &str { + &self.0 + } +} +impl AsRef<[u8]> for ImageData { + fn as_ref(&self) -> &[u8] { + self.as_bytes() + } +} +impl From for ImageData { + fn from(value: String) -> Self { + Self(Arc::new(value.into_boxed_str().into_string())) + } +} +impl From for String { + fn from(value: ImageData) -> Self { + Arc::try_unwrap(value.0).unwrap_or_else(|value| (*value).clone()) + } +} +impl PartialEq for ImageData { + fn eq(&self, other: &String) -> bool { + self.deref() == other + } +} +impl Serialize for ImageData { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(self) + } +} +impl<'de> Deserialize<'de> for ImageData { + fn deserialize>(decoder: D) -> Result { + String::deserialize(decoder).map(Self::from) + } +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn excess_owned_capacity_is_released_before_the_image_can_be_retained() { + let mut input = String::with_capacity(4 * 1024 * 1024); + input.push_str("eA=="); + let data = ImageData::from(input); + assert!(data.retained_bytes() < 1024); + assert!(data.retained_bytes() > data.len()); + assert_eq!(serde_json::to_string(&data).unwrap(), "\"eA==\""); + let owned = String::from(data); + assert_eq!(owned.capacity(), owned.len()); + assert_eq!(owned, "eA=="); + } + #[test] + fn cloning_shares_immutable_bytes_and_preserves_the_string_wire_format() { + let original = ImageData::from("encoded image".to_owned()); + let copy = original.clone(); + assert_eq!(original.as_ptr(), copy.as_ptr()); + assert_eq!(serde_json::to_string(©).unwrap(), "\"encoded image\""); + assert_eq!( + serde_json::from_str::("\"encoded image\"").unwrap(), + original + ); + assert_eq!(String::from(copy), "encoded image"); + } +} diff --git a/crates/ai/src/model/session.rs b/crates/ai/src/model/session.rs index 8ac2a6ee..735c7a21 100644 --- a/crates/ai/src/model/session.rs +++ b/crates/ai/src/model/session.rs @@ -32,6 +32,10 @@ pub struct SessionOpened { pub session: SessionId, pub modes: Option, pub configuration: Option>, + /// The agent's own record of a reopened session, oldest first, from an agent that keeps + /// its sessions ([`AgentCapabilitiesSummary::keeps_history`]). The chat shows it in place + /// of its saved transcript; `None` keeps the saved transcript. + pub history: Option>, } impl SessionOpened { pub fn new(session: impl Into) -> Self { @@ -39,6 +43,7 @@ impl SessionOpened { session: session.into(), modes: None, configuration: None, + history: None, } } } @@ -52,6 +57,10 @@ impl SessionOpened { self.configuration = Some(options); self } + pub fn history(mut self, updates: Vec) -> Self { + self.history = Some(updates); + self + } } /// How the agent starts a local tool server for the session. @@ -133,6 +142,17 @@ pub enum SessionUpdate { ConfigOptionUpdate(Vec), SessionInfoUpdate(SessionInfo), UsageUpdate(ContextUsage), + /// A turn no prompt of this client started, such as one sent from another device. + Turn(TurnActivity), +} + +/// What an agent that runs elsewhere does on its own: it began a turn for a prompt sent from +/// another device, or ended it. A chat shows the prompt and that the agent works, and offers +/// Stop, until the turn ends. +#[derive(Clone, Debug, PartialEq)] +pub enum TurnActivity { + Started { prompt: Vec }, + Ended, } #[derive(Clone, Debug, Default, PartialEq)] @@ -165,4 +185,11 @@ impl std::fmt::Display for RemoteError { #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct AgentCapabilitiesSummary { pub image_prompts: bool, + /// The agent keeps its sessions itself (a hub agent): a chat reopens its session even + /// when it saved nothing of it, and shows the history the agent returns. + pub keeps_history: bool, + /// The agent runs elsewhere with tools of its own (a hub agent on its servers): a prompt + /// carries only what the person wrote, not the context and rules of this device's + /// terminals. + pub own_tools: bool, } diff --git a/crates/ai/src/prompt_files.rs b/crates/ai/src/prompt_files.rs new file mode 100644 index 00000000..62485f4a --- /dev/null +++ b/crates/ai/src/prompt_files.rs @@ -0,0 +1,161 @@ +//! Files a person attaches to a prompt that are not images. A prompt carries a link to the +//! file where it is (`file://`): a local agent opens it there, and a hub chat uploads it. + +use std::path::Path; +use std::sync::Arc; + +use crate::model::content::{Content, ResourceReference}; + +/// The most files one prompt carries besides its images. +pub const MAX_PROMPT_FILES: usize = 16; + +/// The extensions of files attached as images, which the agent sees inline instead. +pub const IMAGE_EXTENSIONS: &[&str] = &["png", "jpg", "jpeg", "gif", "webp"]; + +/// Whether `path` is attached as an image rather than as a file. +pub fn is_image(path: &Path) -> bool { + path.extension() + .and_then(|extension| extension.to_str()) + .is_some_and(|extension| { + IMAGE_EXTENSIONS + .iter() + .any(|known| known.eq_ignore_ascii_case(extension)) + }) +} + +/// Why a file cannot be attached. +#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)] +pub enum PromptFileError { + #[error("Only files given by their full path can be attached")] + Relative, + #[error("At most {MAX_PROMPT_FILES} files can be attached to a message")] + TooMany, +} + +/// A file of a prompt: its name and size for the chip, and the link the agent receives. +#[derive(Clone, Debug, PartialEq)] +pub struct PromptFile { + name: String, + size: u64, + content: Arc, +} + +impl PromptFile { + /// The file at the absolute `path`, of `size` bytes. + pub fn link(path: &Path, size: u64) -> Result { + if !path.is_absolute() { + return Err(PromptFileError::Relative); + } + let name = path.file_name().map_or_else( + || path.display().to_string(), + |name| name.to_string_lossy().into(), + ); + let content = Content::ResourceReference(ResourceReference { + annotations: None, + description: None, + mime_type: None, + name: name.clone(), + size: i64::try_from(size).ok(), + title: None, + uri: file_uri(path), + meta: None, + }); + Ok(Self { + name, + size, + content: Arc::new(content), + }) + } + + /// The file a prompt's `content` links to, when it is one. + pub fn of(content: &Content) -> Option { + let Content::ResourceReference(link) = content else { + return None; + }; + link.uri.starts_with("file://").then(|| Self { + name: link.name.clone(), + size: link + .size + .and_then(|size| u64::try_from(size).ok()) + .unwrap_or(0), + content: Arc::new(content.clone()), + }) + } + + pub fn name(&self) -> &str { + &self.name + } + + pub fn size(&self) -> u64 { + self.size + } + + /// The link the prompt carries. + pub fn content(&self) -> Content { + (*self.content).clone() + } +} + +/// The local path a `file://` link names, if it names one. +pub fn path_of(uri: &str) -> Option { + let rest = uri.strip_prefix("file://")?; + let mut bytes = Vec::with_capacity(rest.len()); + let mut chars = rest.bytes(); + while let Some(byte) = chars.next() { + if byte == b'%' { + let high = chars.next()?; + let low = chars.next()?; + let hex = [high, low]; + bytes.push(u8::from_str_radix(std::str::from_utf8(&hex).ok()?, 16).ok()?); + } else { + bytes.push(byte); + } + } + Some(std::path::PathBuf::from(String::from_utf8(bytes).ok()?)) +} + +/// `file://` and the path with every byte outside the unreserved set and `/` escaped. +fn file_uri(path: &Path) -> String { + use std::fmt::Write as _; + let text = path.to_string_lossy().replace('\\', "/"); + let mut uri = String::from("file://"); + if !text.starts_with('/') { + uri.push('/'); + } + for byte in text.bytes() { + if byte.is_ascii_alphanumeric() || b"-._~/:".contains(&byte) { + uri.push(char::from(byte)); + } else { + let _ = write!(uri, "%{byte:02X}"); + } + } + uri +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_file_is_linked_by_its_escaped_path_and_read_back_from_the_link() { + let path = Path::new("/home/ops/notes 2026/план.txt"); + let file = PromptFile::link(path, 42).unwrap(); + assert_eq!(file.name(), "план.txt"); + let Content::ResourceReference(link) = file.content() else { + panic!("a resource link"); + }; + assert!(link.uri.starts_with("file:///home/ops/notes%202026/")); + assert_eq!(path_of(&link.uri).as_deref(), Some(path)); + assert_eq!(PromptFile::of(&file.content()), Some(file)); + } + + #[test] + fn images_are_told_apart_by_extension_and_relative_paths_are_refused() { + assert!(is_image(Path::new("/tmp/Shot.PNG"))); + assert!(!is_image(Path::new("/tmp/report.pdf"))); + assert_eq!( + PromptFile::link(Path::new("notes.txt"), 1), + Err(PromptFileError::Relative) + ); + } +} diff --git a/crates/ai/src/session.rs b/crates/ai/src/session.rs index 9e65da74..0bee37ab 100644 --- a/crates/ai/src/session.rs +++ b/crates/ai/src/session.rs @@ -67,6 +67,8 @@ pub struct SessionState { closing: bool, /// Failures since the last completed turn. failures: u32, + /// The running turn was started by the agent for another device. + external: bool, } impl SessionState { @@ -196,6 +198,28 @@ impl SessionState { self.turn += 1; Some(self.ticket()) } + /// The agent began a turn no prompt of this chat started (one sent from another device): + /// the chat shows it as running. `false` unless the session is ready. + pub fn start_external_turn(&mut self) -> bool { + if self.start_turn().is_none() { + return false; + } + self.external = true; + true + } + /// The agent's own turn ended. Returns whether one was running. + pub fn finish_external_turn(&mut self) -> bool { + if !self.external || !self.generating() { + return false; + } + self.external = false; + self.phase = SessionPhase::Ready; + true + } + /// The running turn is the agent's own, not one of this chat's prompts. + pub fn external_turn(&self) -> bool { + self.external && self.generating() + } /// The user stops the chat. Returns whether a running turn must be /// cancelled. pub fn stop(&mut self) -> bool { @@ -213,6 +237,7 @@ impl SessionState { if !self.turn_current(ticket) || !self.generating() { return false; } + self.external = false; self.phase = SessionPhase::Ready; if completed { self.failures = 0; @@ -245,6 +270,7 @@ impl SessionState { } /// The session ended with an error. Results of its requests are dropped. pub fn fail(&mut self) { + self.external = false; self.phase = SessionPhase::Failed; self.epoch += 1; self.stopped = false; diff --git a/crates/ai/src/session/tests.rs b/crates/ai/src/session/tests.rs index 238a1185..386e1f7c 100644 --- a/crates/ai/src/session/tests.rs +++ b/crates/ai/src/session/tests.rs @@ -149,3 +149,27 @@ fn a_missing_session_or_a_failed_fork_starts_over() { "a transient error keeps the chat's own session for the next try" ); } + +#[test] +fn a_turn_the_agent_runs_for_another_device_shows_and_ends_on_its_own() { + let mut state = SessionState::default(); + assert!(!state.start_external_turn(), "a detached chat runs nothing"); + state.queue(); + state.leased(); + state.opened(); + assert!(state.start_external_turn()); + assert!(state.generating() && state.external_turn()); + assert!( + state.start_turn().is_none(), + "a prompt waits for the agent's own turn" + ); + assert!(state.finish_external_turn()); + assert!(!state.generating()); + assert!(!state.finish_external_turn()); + let ticket = state.start_turn().unwrap(); + assert!( + !state.finish_external_turn(), + "a prompt's turn is not the agent's own" + ); + assert!(state.finish_turn(ticket, true)); +} diff --git a/crates/ai/src/session_config.rs b/crates/ai/src/session_config.rs index b32c7154..aea80085 100644 --- a/crates/ai/src/session_config.rs +++ b/crates/ai/src/session_config.rs @@ -80,6 +80,7 @@ fn category_name(category: &ConfigCategory) -> String { ConfigCategory::Model => "model".into(), ConfigCategory::ModelConfig => "model_config".into(), ConfigCategory::ThoughtLevel => "thought_level".into(), + ConfigCategory::Server => "server".into(), ConfigCategory::Other(name) => name.clone(), } } @@ -89,6 +90,7 @@ fn category(name: &str) -> ConfigCategory { "model" => ConfigCategory::Model, "model_config" => ConfigCategory::ModelConfig, "thought_level" => ConfigCategory::ThoughtLevel, + "server" => ConfigCategory::Server, other => ConfigCategory::Other(other.into()), } } diff --git a/crates/ai/src/session_config/tests.rs b/crates/ai/src/session_config/tests.rs index f545e8c1..46d36171 100644 --- a/crates/ai/src/session_config/tests.rs +++ b/crates/ai/src/session_config/tests.rs @@ -40,6 +40,9 @@ fn options() -> Vec { .category(ConfigCategory::Mode), ConfigOption::boolean("web", "Web search", false) .category(ConfigCategory::Other("_tools".into())), + ConfigOption::boolean("server:1", "Build box", true) + .description("root@build.example") + .category(ConfigCategory::Server), ] } @@ -73,6 +76,8 @@ fn a_choice_shows_at_once_only_when_the_option_offers_it() { value("opus"), value("low"), value("ask"), + ConfigValue::Flag(true), + // The attached server keeps the value the agent reported. ConfigValue::Flag(true) ] ); diff --git a/crates/ai/src/storage.rs b/crates/ai/src/storage.rs index 85476409..d36a70c3 100644 --- a/crates/ai/src/storage.rs +++ b/crates/ai/src/storage.rs @@ -5,15 +5,57 @@ use crate::{ }; use futures::future::BoxFuture; use std::{path::PathBuf, sync::Arc}; +mod prepared; +pub use prepared::{PreparedChatBytes, PreparedChatLoad}; #[derive(Clone, Debug, thiserror::Error)] pub enum AgentStorageError { + #[error("All normal installation history payload slots are retained or loading")] + PayloadCapacity, + #[error("The history repository cannot prepare a charged decoded load")] + AllocationUnsupported, + #[error( + "Saved history needs {incoming} bytes with {retained} retained; installation limit is {limit} bytes" + )] + ResidentCapacity { + retained: usize, + incoming: usize, + limit: usize, + }, #[error("Storage operation was interrupted before completion")] Interrupted, #[error("Invalid chat identity: {0}")] InvalidId(String), #[error("Saved chat was not found: {0}")] NotFound(String), + #[error( + "Saved chat ownership differs from the requested context: expected {expected:?}, found {actual:?}" + )] + ScopeMismatch { + expected: nocterm_project_model::ContextId, + actual: nocterm_project_model::ContextId, + }, + #[error("Saved chat belongs to another original environment")] + EnvironmentMismatch { + expected: Arc, + actual: Arc, + }, + #[error("This environment requires held native storage directory capabilities")] + EnvironmentUnsupported(Arc), + #[error("The hub account could not be captured")] + AccountCapture(#[from] nocterm_hubs_model::HubError), + #[error("The captured account source is no longer available")] + AccountSource(#[from] nocterm_hubs_model::source_identity::LocalAccountSourceError), + #[error("This context requires held native storage directory capabilities: {0:?}")] + ScopeUnsupported(nocterm_project_model::ContextId), + #[error("The local history filesystem operation failed: {0}")] + Filesystem(#[source] nocterm_core::SharedError), + #[error("History publication failed: {operation}; staging cleanup also failed: {cleanup}")] + StageCleanup { + #[source] + operation: nocterm_core::SharedError, + cleanup: nocterm_core::SharedError, + }, #[error(transparent)] Codec(#[from] HistoryCodecError), #[error("Could not decode agent state: {0}")] @@ -49,6 +91,41 @@ pub enum AgentStorageError { source: Arc, }, } +impl From for AgentStorageError { + fn from(error: nocterm_session::local_fs::LocalFsError) -> Self { + Self::Filesystem(nocterm_core::SharedError::from_error(error)) + } +} +/// Check descriptive document ownership before admitting it to a bound consumer. +/// Matching identities do not issue execution capabilities. +pub fn require_chat_scope( + expected: nocterm_project_model::ContextId, + actual: nocterm_project_model::ContextId, +) -> Result<(), AgentStorageError> { + if expected != actual { + return Err(AgentStorageError::ScopeMismatch { expected, actual }); + } + Ok(()) +} +pub fn require_local_environment( + expected: &crate::local_environment::LocalEnvironment, + actual: &crate::local_environment::LocalEnvironment, +) -> Result<(), AgentStorageError> { + if expected != actual { + return Err(AgentStorageError::EnvironmentMismatch { + expected: Arc::new(expected.clone()), + actual: Arc::new(actual.clone()), + }); + } + Ok(()) +} +pub fn require_local_scope( + expected: &crate::local_environment::ChatScope, + actual: &crate::local_environment::ChatScope, +) -> Result<(), AgentStorageError> { + require_local_environment(expected.environment(), actual.environment())?; + require_chat_scope(expected.context(), actual.context()) +} pub type StorageFuture = BoxFuture<'static, Result>; /// Futures perform I/O only when polled by the background storage executor. pub trait AgentStateRepository: Send + Sync { @@ -56,8 +133,65 @@ pub trait AgentStateRepository: Send + Sync { fn save(&self, state: AgentStateFile) -> StorageFuture<()>; } pub trait ChatRepository: Send + Sync { - fn catalog(&self) -> StorageFuture>; - fn load(&self, id: String) -> StorageFuture; + /// Read bounded original bytes and preflight their decoded ownership cost. + /// Callers must obtain the actual shared allocation before `decode`; an + /// implementation without this capability must refuse rather than hydrate + /// an uncharged value through the older load API. + fn prepare_load_in( + &self, + _scope: crate::local_environment::ChatScope, + _id: String, + ) -> StorageFuture> { + Box::pin(async { Err(AgentStorageError::AllocationUnsupported) }) + } + fn catalog_in( + &self, + scope: crate::local_environment::ChatScope, + ) -> StorageFuture> { + if scope.environment().is_this_computer() { + self.catalog(scope.context()) + } else { + Box::pin(async move { + Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))) + }) + } + } + fn load_in( + &self, + scope: crate::local_environment::ChatScope, + id: String, + ) -> StorageFuture { + if scope.environment().is_this_computer() { + self.load(scope.context(), id) + } else { + Box::pin(async move { + Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))) + }) + } + } + fn delete_in( + &self, + scope: crate::local_environment::ChatScope, + id: String, + ) -> StorageFuture<()> { + if scope.environment().is_this_computer() { + self.delete(scope.context(), id) + } else { + Box::pin(async move { + Err(AgentStorageError::EnvironmentUnsupported(Arc::new( + scope.environment().clone(), + ))) + }) + } + } + /// Filter authoritative ownership before applying per-context discovery limits. + fn catalog(&self, scope: nocterm_project_model::ContextId) -> StorageFuture>; + fn load(&self, scope: nocterm_project_model::ContextId, id: String) + -> StorageFuture; fn save(&self, chat: Arc) -> StorageFuture<()>; - fn delete(&self, id: String) -> StorageFuture<()>; + fn delete(&self, scope: nocterm_project_model::ContextId, id: String) -> StorageFuture<()>; } diff --git a/crates/ai/src/storage/prepared.rs b/crates/ai/src/storage/prepared.rs new file mode 100644 index 00000000..69b99e5f --- /dev/null +++ b/crates/ai/src/storage/prepared.rs @@ -0,0 +1,88 @@ +//! Owned bounded source bytes are decoded only after receiving the caller's lease. +use super::{AgentStorageError, StorageFuture, require_local_scope}; +use crate::{ + history::{self, SavedChat}, + local_environment::ChatScope, +}; +use nocterm_core::allocation::{Allocated, AllocationLease}; +use std::fmt; + +/// An original source prepared by a repository, not a fresh pathname lookup. +/// The estimate covers decoded retained objects; separately bounded native input +/// scratch remains owned by this handle until decoding or cancellation finishes. +pub trait PreparedChatLoad: Send { + fn resident_bytes(&self) -> usize; + fn decode(self: Box, allocation: AllocationLease) -> StorageFuture>; +} + +/// Pure shared implementation for native repositories and their canonical fake. +/// Creation preflights bytes without constructing a decoded SavedChat. Matching +/// descriptive coordinates confer no filesystem or account authority. +pub struct PreparedChatBytes { + bytes: Vec, + scope: ChatScope, + id: String, + expected_version: Option, + resident: usize, +} +impl PreparedChatBytes { + pub fn new( + bytes: Vec, + scope: ChatScope, + id: String, + expected_version: Option, + ) -> Result { + if bytes.len() as u64 > history::MAX_FILE_BYTES { + return Err(history::HistoryCodecError::EncodedLimit.into()); + } + if let Some(version) = expected_version + && !(1..=3).contains(&version) + { + return Err(history::HistoryCodecError::UnsupportedVersion(version).into()); + } + let resident = history::structural_bytes(&bytes)?; + Ok(Self { + bytes, + scope, + id, + expected_version, + resident, + }) + } +} +impl fmt::Debug for PreparedChatBytes { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("PreparedChatBytes") + .field("resident_bytes", &self.resident) + .field("expected_version", &self.expected_version) + .finish_non_exhaustive() + } +} +impl PreparedChatLoad for PreparedChatBytes { + fn resident_bytes(&self) -> usize { + self.resident + } + fn decode(self: Box, allocation: AllocationLease) -> StorageFuture> { + Box::pin(async move { + if allocation.payloads() == 0 { + return Err(AgentStorageError::PayloadCapacity); + } + if allocation.bytes() < self.resident { + return Err(AgentStorageError::ResidentCapacity { + retained: 0, + incoming: self.resident, + limit: allocation.bytes(), + }); + } + let chat = history::decode(&self.bytes, &self.id)?; + require_local_scope(&self.scope, &chat.header.chat_scope())?; + if self + .expected_version + .is_some_and(|version| version != chat.version) + { + return Err(history::HistoryCodecError::InvalidHeader.into()); + } + Ok(Allocated::new(chat, allocation)) + }) + } +} diff --git a/crates/ai/src/testing.rs b/crates/ai/src/testing.rs index 7fe7a056..53b4d6e9 100644 --- a/crates/ai/src/testing.rs +++ b/crates/ai/src/testing.rs @@ -40,9 +40,6 @@ impl AgentConnector for ScriptedConnector { mod storage; pub use storage::{InMemoryAgentRepository, StorageOperation}; -mod usage; -pub use usage::ScriptedUsageSource; - mod sandbox; mod workspace; pub use sandbox::ScriptedSandboxProbe; diff --git a/crates/ai/src/testing/storage.rs b/crates/ai/src/testing/storage.rs index b5b4c48a..abdc5a70 100644 --- a/crates/ai/src/testing/storage.rs +++ b/crates/ai/src/testing/storage.rs @@ -1,8 +1,12 @@ //! One deterministic repository shared by runtime, feature and composition tests. +use crate::local_environment::ChatScope; use crate::{ favorites::AgentStateFile, history::{self, ChatSummary, FileStamp, SavedChat, SharedChat}, - storage::{AgentStateRepository, AgentStorageError, ChatRepository, StorageFuture}, + storage::{ + AgentStateRepository, AgentStorageError, ChatRepository, PreparedChatBytes, + PreparedChatLoad, StorageFuture, + }, }; use futures::channel::oneshot; use std::{ @@ -16,6 +20,8 @@ pub enum StorageOperation { SaveState, Catalog, Load(String), + /// Pause after materialization while the actual decoded body retains its lease. + Decoded(String), Save(String), Delete(String), } @@ -23,7 +29,7 @@ type Release = oneshot::Receiver>; #[derive(Default)] struct State { preferences: AgentStateFile, - chats: BTreeMap>, + chats: HashMap>>, barriers: HashMap>, operations: Vec, rejected: HashMap, @@ -45,7 +51,9 @@ impl InMemoryAgentRepository { .lock() .unwrap() .chats - .insert(chat.id.clone(), bytes); + .entry(chat.header.chat_scope()) + .or_default() + .insert(chat.header.id.clone(), bytes); Ok(()) } /// Restorable catalog documents; encoded() still exposes empty physical records. @@ -53,22 +61,42 @@ impl InMemoryAgentRepository { let state = self.state.lock().unwrap(); let mut chats = state .chats - .iter() + .values() + .flat_map(BTreeMap::iter) .filter_map(|(id, bytes)| history::decode(bytes, id).ok()) .filter(|chat| { let row = ChatSummary::from_chat(chat); row.has_content || row.name.is_some() || row.pinned }) .collect::>(); - chats.sort_by_key(|chat| std::cmp::Reverse((chat.pinned, chat.updated))); + chats.sort_by_key(|chat| std::cmp::Reverse((chat.header.pinned, chat.header.updated))); chats } pub fn encoded(&self, id: &str) -> Option> { - self.state.lock().unwrap().chats.get(id).cloned() + self.encoded_in_scope(history::ContextId::General, id) + } + pub fn encoded_in_scope(&self, scope: history::ContextId, id: &str) -> Option> { + self.encoded_in_environment(&scope.into(), id) + } + pub fn encoded_in_environment(&self, scope: &ChatScope, id: &str) -> Option> { + self.state + .lock() + .unwrap() + .chats + .get(scope)? + .get(id) + .cloned() } pub fn transcript(&self, id: &str) -> Result { + self.transcript_in_scope(history::ContextId::General, id) + } + pub fn transcript_in_scope( + &self, + scope: history::ContextId, + id: &str, + ) -> Result { let bytes = self - .encoded(id) + .encoded_in_scope(scope, id) .ok_or_else(|| AgentStorageError::NotFound(id.to_owned()))?; Ok(history::decode(&bytes, id)?) } @@ -137,14 +165,53 @@ impl AgentStateRepository for InMemoryAgentRepository { } } impl ChatRepository for InMemoryAgentRepository { - fn catalog(&self) -> StorageFuture> { + fn prepare_load_in( + &self, + scope: ChatScope, + id: String, + ) -> StorageFuture> { + let this = self.clone(); + Box::pin(async move { + this.begin(StorageOperation::Load(id.clone())).await?; + let bytes = { + let state = this + .state + .lock() + .map_err(|_| AgentStorageError::Interrupted)?; + state + .chats + .get(&scope) + .and_then(|chats| chats.get(&id)) + .ok_or_else(|| AgentStorageError::NotFound(id.clone()))? + .clone() + }; + let source = PreparedChatBytes::new(bytes, scope, id.clone(), None)?; + Ok(Box::new(PreparedRepositoryLoad { + source: Box::new(source), + repository: this, + id, + }) as Box) + }) + } + fn catalog(&self, scope: history::ContextId) -> StorageFuture> { + self.catalog_in(scope.into()) + } + fn load(&self, scope: history::ContextId, id: String) -> StorageFuture { + self.load_in(scope.into(), id) + } + fn delete(&self, scope: history::ContextId, id: String) -> StorageFuture<()> { + self.delete_in(scope.into(), id) + } + fn catalog_in(&self, scope: ChatScope) -> StorageFuture> { let this = self.clone(); Box::pin(async move { this.begin(StorageOperation::Catalog).await?; let state = this.state.lock().unwrap(); let mut rows = state .chats - .iter() + .get(&scope) + .into_iter() + .flat_map(BTreeMap::iter) .filter_map(|(id, bytes)| { history::decode_summary( bytes, @@ -157,57 +224,167 @@ impl ChatRepository for InMemoryAgentRepository { .ok() .flatten() }) + .filter(|row| row.chat_scope() == scope) .collect::>(); rows.sort_by_key(|row| std::cmp::Reverse((row.pinned, row.updated))); rows.truncate(history::MAX_RESTORED); Ok(rows) }) } - fn load(&self, id: String) -> StorageFuture { + fn load_in(&self, scope: ChatScope, id: String) -> StorageFuture { let this = self.clone(); Box::pin(async move { this.begin(StorageOperation::Load(id.clone())).await?; let state = this.state.lock().unwrap(); let bytes = state .chats - .get(&id) + .get(&scope) + .and_then(|chats| chats.get(&id)) .ok_or_else(|| AgentStorageError::NotFound(id.clone()))?; - Ok(history::decode(bytes, &id)?) + let chat = history::decode(bytes, &id)?; + crate::storage::require_local_scope(&scope, &chat.header.chat_scope())?; + Ok(chat) }) } fn save(&self, chat: Arc) -> StorageFuture<()> { let this = self.clone(); Box::pin(async move { - this.begin(StorageOperation::Save(chat.id.clone())).await?; + this.begin(StorageOperation::Save(chat.header.id.clone())) + .await?; let bytes = if let Some((draft, updated)) = chat.metadata.draft_patch() { let mut original = { let state = this.state.lock().unwrap(); let bytes = state .chats - .get(&chat.id) - .ok_or_else(|| AgentStorageError::NotFound(chat.id.clone()))?; - history::decode(bytes, &chat.id)? + .get(&chat.header.chat_scope()) + .and_then(|chats| chats.get(&chat.header.id)) + .ok_or_else(|| AgentStorageError::NotFound(chat.header.id.clone()))?; + history::decode(bytes, &chat.header.id)? }; + crate::storage::require_local_scope( + &chat.header.chat_scope(), + &original.header.chat_scope(), + )?; original.draft = draft; - original.updated = updated; + original.header.updated = updated; history::encode(&original)? } else { history::encode_shared(&chat)? }; - this.state - .lock() - .unwrap() - .chats - .insert(chat.id.clone(), bytes); + let mut state = this.state.lock().unwrap(); + let chats = state.chats.entry(chat.header.chat_scope()).or_default(); + if let Some(original) = chats.get(&chat.header.id) { + let original = history::decode(original, &chat.header.id)?; + crate::storage::require_local_scope( + &chat.header.chat_scope(), + &original.header.chat_scope(), + )?; + } + chats.insert(chat.header.id.clone(), bytes); Ok(()) }) } - fn delete(&self, id: String) -> StorageFuture<()> { + fn delete_in(&self, scope: ChatScope, id: String) -> StorageFuture<()> { let this = self.clone(); Box::pin(async move { this.begin(StorageOperation::Delete(id.clone())).await?; - this.state.lock().unwrap().chats.remove(&id); + let mut state = this.state.lock().unwrap(); + let Some(chats) = state.chats.get_mut(&scope) else { + return Ok(()); + }; + if let Some(bytes) = chats.get(&id) { + let chat = history::decode(bytes, &id)?; + crate::storage::require_local_scope(&scope, &chat.header.chat_scope())?; + } + chats.remove(&id); Ok(()) }) } } + +struct PreparedRepositoryLoad { + source: Box, + repository: InMemoryAgentRepository, + id: String, +} +impl PreparedChatLoad for PreparedRepositoryLoad { + fn resident_bytes(&self) -> usize { + self.source.resident_bytes() + } + fn decode( + self: Box, + allocation: nocterm_core::allocation::AllocationLease, + ) -> StorageFuture> { + Box::pin(async move { + let decoded = self.source.decode(allocation).await?; + self.repository + .begin(StorageOperation::Decoded(self.id)) + .await?; + Ok(decoded) + }) + } +} + +#[cfg(test)] +mod tests { + use super::*; + use futures::executor::block_on; + #[test] + fn the_same_identity_in_two_scopes_has_independent_documents_and_deletion() { + let repository = InMemoryAgentRepository::default(); + let general = SavedChat::for_identity( + "agent".into(), + "00000000-0000-4000-8000-000000000001".into(), + 1, + ); + let mut project = general.clone(); + project.header.scope = serde_json::from_value( + serde_json::json!({"kind":"project","id":"01010101010101010101010101010101"}), + ) + .unwrap(); + project.header.title = Some("project caption".into()); + repository.seed_chat(&general).unwrap(); + repository.seed_chat(&project).unwrap(); + let before = repository + .encoded_in_scope(project.header.scope, &project.header.id) + .unwrap(); + assert_eq!( + block_on(ChatRepository::load( + &repository, + general.header.scope, + general.header.id.clone() + )) + .unwrap() + .header + .title, + None + ); + assert_eq!( + block_on(ChatRepository::load( + &repository, + project.header.scope, + project.header.id.clone() + )) + .unwrap() + .header + .title, + project.header.title + ); + block_on(repository.delete(general.header.scope, general.header.id.clone())).unwrap(); + assert!(repository.encoded(&general.header.id).is_none()); + assert_eq!( + repository + .encoded_in_scope(project.header.scope, &project.header.id) + .unwrap(), + before + ); + assert!(matches!( + block_on(ChatRepository::load( + &repository, + general.header.scope, + general.header.id + )), + Err(AgentStorageError::NotFound(_)) + )); + } +} diff --git a/crates/ai/src/testing/usage.rs b/crates/ai/src/testing/usage.rs deleted file mode 100644 index efb5db64..00000000 --- a/crates/ai/src/testing/usage.rs +++ /dev/null @@ -1,147 +0,0 @@ -//! Shared scripted account usage source with explicitly completed reads. -use crate::usage::{AccountUsageSource, RateLimit, UsageAgent, UsageReadError}; -use futures::{channel::oneshot, future::BoxFuture}; -use std::{ - collections::VecDeque, - sync::{Arc, Mutex}, -}; - -type Answer = Result>, UsageReadError>; -#[derive(Default)] -struct Script { - requests: Vec, - answers: VecDeque>, -} -#[derive(Clone, Default)] -pub struct ScriptedUsageSource(Arc>); -impl ScriptedUsageSource { - pub fn push(&self, result: Answer) { - self.0 - .lock() - .expect("usage script") - .answers - .push_back(Box::pin(async move { result })); - } - pub fn pending(&self) -> oneshot::Sender { - let (sender, receiver) = oneshot::channel(); - self.0 - .lock() - .expect("usage script") - .answers - .push_back(Box::pin(async move { receiver.await.unwrap_or(Ok(None)) })); - sender - } - pub fn requests(&self) -> Vec { - self.0.lock().expect("usage script").requests.clone() - } -} -impl AccountUsageSource for ScriptedUsageSource { - fn read(&self, agent: UsageAgent) -> BoxFuture<'static, Answer> { - let script = self.0.clone(); - Box::pin(async move { - let answer = { - let mut script = script.lock().expect("usage script"); - script.requests.push(agent); - script.answers.pop_front() - }; - match answer { - Some(answer) => answer.await, - None => Ok(None), - } - }) - } -} - -#[cfg(test)] -mod tests { - use super::*; - use futures::{FutureExt, executor::block_on}; - use std::{error::Error, path::PathBuf}; - fn agent() -> UsageAgent { - UsageAgent { - id: "account".into(), - account: crate::agents::AccountUsage::SessionLog, - } - } - #[test] - fn held_usage_failures_preserve_read_seek_category_native_kind_and_allocation() { - for error in [ - UsageReadError::read( - "/report", - std::io::Error::from(std::io::ErrorKind::PermissionDenied), - ), - UsageReadError::seek( - "/report", - std::io::Error::from(std::io::ErrorKind::InvalidInput), - ), - ] { - let expected = error.clone(); - let source = ScriptedUsageSource::default(); - let answer = source.pending(); - let mut future = source.read(agent()); - assert!(future.as_mut().now_or_never().is_none()); - assert_eq!(source.requests(), vec![agent()]); - answer.send(Err(error)).unwrap(); - let observed = block_on(future).unwrap_err(); - match (&expected, &observed) { - (UsageReadError::Read { path: a, .. }, UsageReadError::Read { path: b, .. }) - | (UsageReadError::Seek { path: a, .. }, UsageReadError::Seek { path: b, .. }) => { - assert_eq!(a, b); - assert_eq!(a, &PathBuf::from("/report")); - } - _ => panic!("usage operation category must be preserved"), - } - let native = expected - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - let shared = observed - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - assert_eq!(native.kind(), shared.kind()); - assert_eq!(expected.to_string(), observed.to_string()); - } - } - #[test] - fn usage_source_preserves_immediate_native_error_before_its_deeper_cause() { - #[derive(Debug)] - struct NativeContext; - impl std::fmt::Display for NativeContext { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - f.write_str("usage context") - } - } - impl Error for NativeContext { - fn source(&self) -> Option<&(dyn Error + 'static)> { - Some(&std::fmt::Error) - } - } - let error = UsageReadError::read("/report", std::io::Error::other(NativeContext)); - let clone = error.clone(); - let native = error - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - let shared = clone - .source() - .unwrap() - .source() - .unwrap() - .downcast_ref::() - .unwrap(); - assert!(std::ptr::eq(native, shared)); - assert_eq!(native.kind(), std::io::ErrorKind::Other); - assert!(native.source().unwrap().is::()); - } -} diff --git a/crates/ai/src/tests.rs b/crates/ai/src/tests.rs index 34a2a3e5..f91707f4 100644 --- a/crates/ai/src/tests.rs +++ b/crates/ai/src/tests.rs @@ -19,6 +19,7 @@ fn environment_blocks_sensitive_and_injection_vars_even_if_requested() { args: Vec::new(), env: Default::default(), inherit_env: vec!["ANTHROPIC_API_KEY".into()], + hub: None, }; for name in [ "SSH_AUTH_SOCK", @@ -136,18 +137,22 @@ fn reducer_handles_transcript_metadata_usage_and_tool_replacement() { fn mcp_requires_initialization_validates_input_and_never_echoes_bad_payload() { let mut m = McpSession::default(); assert!( - matches!(m.handle("marker-secret malformed"),McpStep::Reply(v) if !v.to_string().contains("marker-secret")) + matches!(m.handle("marker-secret malformed", Default::default()),McpStep::Reply(v) if !v.to_string().contains("marker-secret")) ); assert!( - matches!(m.handle(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#),McpStep::Reply(v) if v["error"]["code"]==-32002) + matches!(m.handle(r#"{"jsonrpc":"2.0","id":1,"method":"tools/list"}"#, Default::default()),McpStep::Reply(v) if v["error"]["code"]==-32002) + ); + m.handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + Default::default(), ); - m.handle(r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#); assert!( - matches!(m.handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_terminal","arguments":{"terminal_id":"t1","lines":2001}}}"#),McpStep::Rejected { error, .. } if !error.is_empty()) + matches!(m.handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"read_terminal","arguments":{"terminal_id":"t1","lines":2001}}}"#, Default::default()),McpStep::Rejected { error, .. } if !error.is_empty()) ); assert!(matches!( m.handle( - r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"list_terminals"}}"# + r#"{"jsonrpc":"2.0","id":2,"method":"tools/call","params":{"name":"list_terminals"}}"#, + Default::default() ), McpStep::Call { call: TerminalCall::ListTerminals, @@ -155,11 +160,14 @@ fn mcp_requires_initialization_validates_input_and_never_echoes_bad_payload() { } )); assert!(matches!( - m.handle(r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#), + m.handle( + r#"{"jsonrpc":"2.0","method":"notifications/initialized"}"#, + Default::default() + ), McpStep::Ignore )); assert!(matches!( - m.handle(&"x".repeat(MAX_MCP_LINE + 1)), + m.handle(&"x".repeat(MAX_MCP_LINE + 1), Default::default()), McpStep::Reply(_) )); } @@ -188,6 +196,7 @@ fn explicit_descriptor_payload_filters_user_metadata_and_has_no_credentials() { local: false, cwd: None, status: "connected".into(), + file_tools: false, connection: Some(ConnectionDescriptor { id: "c1".into(), name: "host".into(), @@ -195,7 +204,7 @@ fn explicit_descriptor_payload_filters_user_metadata_and_has_no_credentials() { description: "password=marker-description sk-ant-secretmarker123".into(), host: "example.org".into(), port: 22, - user: "egor".into(), + user: "alice".into(), }), }; let payload = context_block(&[terminal], &[]); @@ -264,6 +273,7 @@ fn descriptor_redaction_preserves_json_escaping_and_blocks_context_delimiter_inj connection: None, cwd: Some(" injected".into()), status: "Connected".into(), + file_tools: false, }; let block = context_block(&[terminal], &[]); let json = block @@ -323,6 +333,66 @@ fn capabilities_do_not_share_execution_input_or_connection_grants() { grants.revoke("t1"); assert!(grants.requires_approval(&execution, &settings)); } +fn transfer_between(source: &str, destination: &str) -> TerminalCall { + TerminalCall::TransferFiles(TransferFiles { + source_terminal_id: source.into(), + sources: vec!["/a".into()], + destination_terminal_id: destination.into(), + destination: "/in".into(), + overwrite: false, + yield_ms: None, + }) +} +#[test] +fn a_transfer_grant_covers_one_unordered_terminal_pair_only() { + let settings = ApprovalSettings::default(); + let mut grants = ApprovalGrants::default(); + let call = transfer_between("t1", "t2"); + assert!(grants.requires_approval(&call, &settings)); + grants.grant("t1", ToolCapability::Transfer); + grants.grant("t2", ToolCapability::Transfer); + assert!( + grants.requires_approval(&call, &settings), + "per-terminal capability grants do not approve a transfer" + ); + grants.grant_call(&call); + assert!(!grants.requires_approval(&call, &settings)); + assert!(!grants.requires_approval(&transfer_between("t2", "t1"), &settings)); + grants.grant_call(&transfer_between("t3", "t4")); + for crossing in [("t1", "t4"), ("t3", "t2"), ("t1", "t3"), ("t1", "t1")] { + assert!( + grants.requires_approval(&transfer_between(crossing.0, crossing.1), &settings), + "{crossing:?}" + ); + } + grants.revoke("t1"); + assert!(grants.requires_approval(&call, &settings)); + assert!(!grants.requires_approval(&transfer_between("t4", "t3"), &settings)); +} +#[test] +fn a_same_terminal_transfer_grant_is_its_own_pair() { + let settings = ApprovalSettings::default(); + let mut grants = ApprovalGrants::default(); + grants.grant_call(&transfer_between("t1", "t1")); + assert!(!grants.requires_approval(&transfer_between("t1", "t1"), &settings)); + assert!(grants.requires_approval(&transfer_between("t1", "t2"), &settings)); +} +#[test] +fn reading_and_cancelling_a_transfer_need_no_terminal_grant() { + let settings = ApprovalSettings::default(); + let grants = ApprovalGrants::default(); + for call in [ + TerminalCall::ReadTransfer(ReadTransfer { + transfer_id: "x".into(), + yield_ms: None, + }), + TerminalCall::CancelTransfer(CancelTransfer { + transfer_id: "x".into(), + }), + ] { + assert!(!grants.requires_approval(&call, &settings)); + } +} #[test] fn structured_execution_bounds_program_arguments_stdin_and_lifetime() { let valid = ExecCommand { diff --git a/crates/ai/src/thread.rs b/crates/ai/src/thread.rs index cc724ae9..c552d4fc 100644 --- a/crates/ai/src/thread.rs +++ b/crates/ai/src/thread.rs @@ -7,6 +7,7 @@ use crate::model::{ usage::{ContextUsage, TokenTotals}, }; use serde::{Deserialize, Serialize}; +pub mod live; #[derive(Clone, Debug, Serialize, Deserialize)] #[serde(tag = "kind", content = "value", rename_all = "snake_case")] pub enum Entry { diff --git a/crates/ai/src/thread/live.rs b/crates/ai/src/thread/live.rs new file mode 100644 index 00000000..4fe60f3c --- /dev/null +++ b/crates/ai/src/thread/live.rs @@ -0,0 +1,236 @@ +//! Pure live-transcript admission. A stream is bounded before it mutates accepted rows. +use super::{Entry, ThreadState}; +use crate::{ + history::{HistoryCodecError, MAX_RESIDENT_BYTES, retained_value_bytes}, + model::{content::Content, controls::Change, session::SessionUpdate}, +}; + +const MAX_LIVE_ENTRIES: usize = 20_000; +pub fn resident_bytes(state: &ThreadState) -> Result { + Ok(retained_value_bytes(&state.entries)? + .saturating_add(plan_bytes(state.plan.as_ref())) + .saturating_add(retained_value_bytes(&state.title)?) + .saturating_add(usage_bytes(state.usage.as_ref())?) + .saturating_add(state.times.len().saturating_mul(8))) +} +fn plan_bytes(value: Option<&crate::model::plan::Plan>) -> usize { + value.map_or(0, |plan| { + plan.entries.iter().fold(1024usize, |bytes, entry| { + bytes + .saturating_add(entry.content.len().saturating_mul(2)) + .saturating_add(1024) + }) + }) +} +fn usage_bytes( + value: Option<&crate::model::usage::ContextUsage>, +) -> Result { + value.map_or(Ok(0), |usage| { + Ok(retained_value_bytes(&usage.meta)? + .saturating_add( + usage + .cost + .as_ref() + .map_or(0, |cost| cost.currency.len().saturating_mul(2)), + ) + .saturating_add(1024)) + }) +} +pub fn admit_user( + state: &ThreadState, + current: usize, + content: &[Content], +) -> Result { + if state.entries.len() >= MAX_LIVE_ENTRIES { + return Err(HistoryCodecError::CollectionLimit); + } + let next = current + .saturating_add(retained_value_bytes(&content)?) + .saturating_add(4096); + if next > MAX_RESIDENT_BYTES { + return Err(HistoryCodecError::StructuralLimit); + } + Ok(next) +} +/// Computes only the changed content; a text append never rescans the existing message. +pub fn admit_update( + state: &ThreadState, + current: usize, + update: &SessionUpdate, +) -> Result { + let Some((removed, added, row)) = update_change(state, update)? else { + return Ok(current); + }; + if state.entries.len().saturating_add(usize::from(row)) > MAX_LIVE_ENTRIES { + return Err(HistoryCodecError::CollectionLimit); + } + let next = current + .saturating_sub(removed) + .saturating_add(added) + .saturating_add(usize::from(row) * 8); + if next > MAX_RESIDENT_BYTES { + return Err(HistoryCodecError::StructuralLimit); + } + Ok(next) +} + +fn update_change( + state: &ThreadState, + update: &SessionUpdate, +) -> Result, HistoryCodecError> { + let change = match update { + SessionUpdate::AgentMessageChunk(content) | SessionUpdate::AgentThoughtChunk(content) => { + let thought = matches!(update, SessionUpdate::AgentThoughtChunk(_)); + match (content, state.entries.last(), thought) { + (Content::Text(text), Some(Entry::Agent(_)), false) + | (Content::Text(text), Some(Entry::Thought(_)), true) => { + (0, text.text.len().saturating_mul(2), false) + } + _ => (0, retained_value_bytes(content)?.saturating_add(4096), true), + } + } + SessionUpdate::UserMessageChunk(content) => ( + 0, + retained_value_bytes(content)?.saturating_add(4096), + !matches!(state.entries.last(), Some(Entry::User(_))), + ), + SessionUpdate::ToolCall(call) => { + (0, retained_value_bytes(call)?.saturating_add(4096), true) + } + SessionUpdate::ToolCallUpdate(patch) => return tool_change(state, patch), + _ => return control_change(state, update), + }; + Ok(Some(change)) +} +fn tool_change( + state: &ThreadState, + patch: &crate::model::tools::ToolPatch, +) -> Result, HistoryCodecError> { + let Some(original) = state.entries.iter().find_map(|entry| match entry { + Entry::Tool(call) if call.id == patch.id => Some(call), + _ => None, + }) else { + return Ok(None); + }; + let mut candidate = original.clone(); + candidate.apply(patch.changes.clone()); + Ok(Some(( + retained_value_bytes(original)?, + retained_value_bytes(&candidate)?, + false, + ))) +} +fn control_change( + state: &ThreadState, + update: &SessionUpdate, +) -> Result, HistoryCodecError> { + let change = match update { + SessionUpdate::Plan(plan) => ( + plan_bytes(state.plan.as_ref()), + plan_bytes(Some(plan)), + false, + ), + SessionUpdate::SessionInfoUpdate(info) => match &info.title { + Change::Set(title) => ( + retained_value_bytes(&state.title)?, + retained_value_bytes(&Some(title))?, + false, + ), + Change::Clear | Change::Keep => return Ok(None), + }, + SessionUpdate::UsageUpdate(usage) => ( + usage_bytes(state.usage.as_ref())?, + usage_bytes(Some(usage))?, + false, + ), + _ => return Ok(None), + }; + Ok(Some(change)) +} + +#[cfg(test)] +mod tests { + use super::*; + #[test] + fn repeated_small_chunks_refuse_before_changing_the_accepted_transcript() { + let mut state = ThreadState::default(); + let mut bytes = resident_bytes(&state).unwrap(); + let update = SessionUpdate::AgentMessageChunk(Content::text("x".repeat(1024 * 1024))); + let mut accepted = 0; + while let Ok(next) = admit_update(&state, bytes, &update) { + state.apply(update.clone(), 1); + bytes = next; + assert!(resident_bytes(&state).unwrap() <= bytes); + accepted += 1; + } + assert!(accepted > 1 && accepted < 64); + assert!(matches!( + admit_update(&state, bytes, &update), + Err(HistoryCodecError::StructuralLimit) + )); + assert!( + matches!(&state.entries[0], Entry::Agent(text) if text.len() == accepted * 1024 * 1024) + ); + } + #[test] + fn replacing_a_tool_payload_releases_its_previous_charge() { + use crate::model::tools::{ToolChanges, ToolPatch, ToolRecord}; + let mut state = ThreadState::default(); + state.apply( + SessionUpdate::ToolCall(Box::new(ToolRecord::new("tool", "x".repeat(4096)))), + 1, + ); + let bytes = resident_bytes(&state).unwrap(); + let update = SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + "tool", + ToolChanges::default().title("done"), + ))); + assert!(admit_update(&state, bytes, &update).unwrap() < bytes); + } + #[test] + fn mixed_updates_keep_the_complete_live_state_inside_the_admitted_budget() { + use crate::model::{ + plan::{Plan, PlanEntry, PlanPriority, PlanStatus}, + session::SessionInfo, + tools::{ToolChanges, ToolPatch, ToolRecord}, + usage::ContextUsage, + }; + let mut state = ThreadState::default(); + let mut bytes = resident_bytes(&state).unwrap(); + let updates = vec![ + SessionUpdate::AgentMessageChunk(Content::text("answer")), + SessionUpdate::AgentThoughtChunk(Content::text("thinking")), + SessionUpdate::ToolCall(Box::new(ToolRecord::new("tool", "command"))), + SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + "tool", + ToolChanges::default().raw_output(serde_json::json!({"output": "x".repeat(4096)})), + ))), + SessionUpdate::Plan(Plan { + entries: vec![PlanEntry { + content: "plan".repeat(1024), + priority: PlanPriority::High, + status: PlanStatus::Pending, + }], + }), + SessionUpdate::UsageUpdate(ContextUsage::new(10, 100).meta( + crate::model::extensions::Extensions::from(serde_json::Map::from_iter([( + "detail".into(), + serde_json::json!("x".repeat(2048)), + )])), + )), + SessionUpdate::SessionInfoUpdate(SessionInfo { + title: Change::Set("title".repeat(1024)), + ..Default::default() + }), + ]; + for update in updates { + bytes = admit_update(&state, bytes, &update).unwrap(); + state.apply(update, 1); + assert!(resident_bytes(&state).unwrap() <= bytes); + } + let content = vec![Content::text("user prompt")]; + bytes = admit_user(&state, bytes, &content).unwrap(); + state.push_user(content, 1); + assert!(resident_bytes(&state).unwrap() <= bytes); + } +} diff --git a/crates/ai/src/tool_display.rs b/crates/ai/src/tool_display.rs index ec05e405..37719b02 100644 --- a/crates/ai/src/tool_display.rs +++ b/crates/ai/src/tool_display.rs @@ -63,9 +63,9 @@ impl ToolDisplay { Ok(value) => ToolOutcome::Ok(value), Err(error) => ToolOutcome::Err(crate::redact::redact(error.message())), }; - self.outcome = serde_json::to_vec(&outcome) + self.outcome = crate::history::json_size(&outcome) .ok() - .filter(|bytes| bytes.len() <= 256 * 1024) + .filter(|bytes| *bytes <= 256 * 1024) .map(|_| outcome); } @@ -148,6 +148,16 @@ pub fn request(call: &TerminalCall) -> (&'static str, Value) { TerminalCall::ExecCommand(v) => json!(v), TerminalCall::ReadCommand(v) => json!(v), TerminalCall::CancelCommand(v) => json!(v), + TerminalCall::TransferFiles(v) => json!(v), + TerminalCall::ReadTransfer(v) => json!(v), + TerminalCall::CancelTransfer(v) => json!(v), + TerminalCall::ListFiles(v) => json!(v), + TerminalCall::ReadFile(v) => json!(v), + TerminalCall::WriteFile(v) => json!(v), + TerminalCall::EditFile(v) => json!(v), + TerminalCall::MakeDirectory(v) => json!(v), + TerminalCall::DeletePath(v) => json!(v), + TerminalCall::MovePath(v) => json!(v), }; (call.name(), arguments) } diff --git a/crates/ai/src/tool_display/identity.rs b/crates/ai/src/tool_display/identity.rs index 79fb355c..6b93df69 100644 --- a/crates/ai/src/tool_display/identity.rs +++ b/crates/ai/src/tool_display/identity.rs @@ -73,7 +73,8 @@ fn explicit_identity(call: &ToolRecord) -> Option>> { /// Only exact names or a consistent explicit MCP envelope establish the request /// format. Neither establishes an execution destination. pub fn envelope(call: &ToolRecord, server: &str) -> Option { - envelope_of(call, registration(server)?, parse) + let (tool, arguments) = raw_envelope(call, server)?; + parse(&tool, arguments) } /// Exact provider identity and original arguments, including rejected requests. @@ -102,37 +103,6 @@ pub fn raw_envelope(call: &ToolRecord, server: &str) -> Option<(String, serde_js Some((named?.tool.into(), input.clone())) } -/// `read` turns the tool's arguments into a request: strictly when matching -/// requests the bridge executed, by shape alone when only showing one. -fn envelope_of( - call: &ToolRecord, - registration: &str, - read: fn(&str, serde_json::Value) -> Option, -) -> Option { - let named = explicit_identity(call)?; - if named.is_some_and(|named| named.registration != registration) { - return None; - } - let Some(input) = call.raw_input.as_ref().filter(|input| !input.is_null()) else { - // Hermes omits the input of a call without arguments. - return read(named?.tool, json!({})); - }; - if ["server", "tool", "arguments"] - .iter() - .any(|key| input.get(key).is_some()) - { - let envelope_server = input.get("server")?.as_str()?; - let tool = input.get("tool")?.as_str()?; - if self::registration(envelope_server) != Some(registration) - || named.is_some_and(|named| named.tool != tool) - { - return None; - } - return read(tool, input.get("arguments")?.clone()); - } - read(named?.tool, input.clone()) -} - /// Presentation identity remains useful even when its arguments are malformed. pub fn fallback_tool(call: &ToolRecord) -> Option<&str> { requested_arguments(call).map(|(tool, _)| tool) diff --git a/crates/ai/src/tools.rs b/crates/ai/src/tools.rs index dff6047b..dc8a9ead 100644 --- a/crates/ai/src/tools.rs +++ b/crates/ai/src/tools.rs @@ -1,7 +1,16 @@ pub mod catalog; mod failure; +pub mod files; +pub mod output; +pub mod transfer; -pub use failure::{TerminalCallError, ToolFailure}; +pub use catalog::{ToolGroup, ToolOffer}; +pub use failure::{TerminalCallError, ToolArgumentsError, ToolFailure}; +pub use files::{DeletePath, EditFile, ListFiles, MakeDirectory, MovePath, ReadFile, WriteFile}; +pub use transfer::{ + CancelTransfer, MAX_TRANSFER_FAILURES, MAX_TRANSFER_PATH_BYTES, MAX_TRANSFER_SOURCE_BYTES, + MAX_TRANSFER_SOURCES, ReadTransfer, TransferFiles, +}; use schemars::JsonSchema; use serde::{Deserialize, Serialize}; @@ -63,12 +72,15 @@ pub enum ToolCapability { LiveInput, Execution, Connection, + Transfer, + /// Changing the files of a terminal that runs no programs. + Files, } -/// Connects to an attached offline server without opening a tab. +/// Opens an allowlisted server or reuses its already connected terminal. #[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] #[serde(deny_unknown_fields)] pub struct OpenTerminal { - /// An `offline_servers` entry of `list_terminals`. + /// A `server_id` from the terminals or offline servers in `list_terminals`. pub server_id: String, } #[derive(Clone, Debug)] @@ -81,19 +93,80 @@ pub enum TerminalCall { ExecCommand(ExecCommand), ReadCommand(ReadCommand), CancelCommand(CancelCommand), + TransferFiles(TransferFiles), + ReadTransfer(ReadTransfer), + CancelTransfer(CancelTransfer), + ListFiles(ListFiles), + ReadFile(ReadFile), + WriteFile(WriteFile), + EditFile(EditFile), + MakeDirectory(MakeDirectory), + DeletePath(DeletePath), + MovePath(MovePath), } impl TerminalCall { + /// Canonical semantic arguments, using the same DTOs as catalog parsing. + /// No execution or provider metadata is included. + pub fn arguments(&self) -> Result { + match self { + Self::ListTerminals => Ok(serde_json::json!({})), + Self::ReadTerminal(v) => serde_json::to_value(v), + Self::SendInput(v) => serde_json::to_value(v), + Self::RunCommand(v) => serde_json::to_value(v), + Self::OpenTerminal(v) => serde_json::to_value(v), + Self::ExecCommand(v) => serde_json::to_value(v), + Self::ReadCommand(v) => serde_json::to_value(v), + Self::CancelCommand(v) => serde_json::to_value(v), + Self::TransferFiles(v) => serde_json::to_value(v), + Self::ReadTransfer(v) => serde_json::to_value(v), + Self::CancelTransfer(v) => serde_json::to_value(v), + Self::ListFiles(v) => serde_json::to_value(v), + Self::ReadFile(v) => serde_json::to_value(v), + Self::WriteFile(v) => serde_json::to_value(v), + Self::EditFile(v) => serde_json::to_value(v), + Self::MakeDirectory(v) => serde_json::to_value(v), + Self::DeletePath(v) => serde_json::to_value(v), + Self::MovePath(v) => serde_json::to_value(v), + } + .map_err(ToolArgumentsError::from) + } pub fn terminal_id(&self) -> Option<&str> { match self { - Self::ListTerminals | Self::OpenTerminal(_) => None, + Self::ListTerminals + | Self::OpenTerminal(_) + | Self::ReadTransfer(_) + | Self::CancelTransfer(_) => None, + Self::TransferFiles(v) => Some(&v.destination_terminal_id), Self::ReadTerminal(v) => Some(&v.terminal_id), Self::SendInput(v) => Some(&v.terminal_id), Self::RunCommand(v) => Some(&v.terminal_id), Self::ExecCommand(v) => Some(&v.terminal_id), Self::ReadCommand(v) => Some(&v.terminal_id), Self::CancelCommand(v) => Some(&v.terminal_id), + Self::ListFiles(v) => Some(&v.terminal_id), + Self::ReadFile(v) => Some(&v.terminal_id), + Self::WriteFile(v) => Some(&v.terminal_id), + Self::EditFile(v) => Some(&v.terminal_id), + Self::MakeDirectory(v) => Some(&v.terminal_id), + Self::DeletePath(v) => Some(&v.terminal_id), + Self::MovePath(v) => Some(&v.terminal_id), + } + } + /// The terminal whose host holds the sources of a transfer. + pub fn source_terminal_id(&self) -> Option<&str> { + match self { + Self::TransferFiles(v) => Some(&v.source_terminal_id), + _ => None, } } + /// Every terminal the call touches: `terminal_id`, then a distinct `source_terminal_id`. + pub fn terminals(&self) -> impl Iterator { + let destination = self.terminal_id(); + let source = self + .source_terminal_id() + .filter(|source| Some(*source) != destination); + destination.into_iter().chain(source) + } pub fn server_id(&self) -> Option<&str> { match self { Self::OpenTerminal(v) => Some(&v.server_id), @@ -104,7 +177,7 @@ impl TerminalCall { pub fn target(&self) -> Option<&str> { self.terminal_id().or_else(|| self.server_id()) } - /// Typing, running and connecting change something; reading does not. + /// Typing, running, connecting and changing files change something; reading does not. pub fn writes(&self) -> bool { matches!( self, @@ -113,16 +186,36 @@ impl TerminalCall { | Self::OpenTerminal(_) | Self::ExecCommand(_) | Self::CancelCommand(_) + | Self::TransferFiles(_) + | Self::CancelTransfer(_) + | Self::WriteFile(_) + | Self::EditFile(_) + | Self::MakeDirectory(_) + | Self::DeletePath(_) + | Self::MovePath(_) ) } + /// A file tool: the call acts on the files of a terminal that runs no programs. + pub fn is_file_tool(&self) -> bool { + self.group() == ToolGroup::Files + } pub fn capability(&self) -> ToolCapability { match self { Self::SendInput(_) | Self::RunCommand(_) => ToolCapability::LiveInput, Self::ExecCommand(_) | Self::CancelCommand(_) => ToolCapability::Execution, Self::OpenTerminal(_) => ToolCapability::Connection, - Self::ListTerminals | Self::ReadTerminal(_) | Self::ReadCommand(_) => { - ToolCapability::Read - } + Self::TransferFiles(_) | Self::CancelTransfer(_) => ToolCapability::Transfer, + Self::WriteFile(_) + | Self::EditFile(_) + | Self::MakeDirectory(_) + | Self::DeletePath(_) + | Self::MovePath(_) => ToolCapability::Files, + Self::ListTerminals + | Self::ReadTerminal(_) + | Self::ReadCommand(_) + | Self::ReadTransfer(_) + | Self::ListFiles(_) + | Self::ReadFile(_) => ToolCapability::Read, } } pub fn validate(&self) -> Result<(), TerminalCallError> { @@ -182,6 +275,16 @@ impl TerminalCall { Self::CancelCommand(v) if v.command_id.is_empty() || v.command_id.len() > 128 => { Err(TerminalCallError::InvalidCommandId) } + Self::TransferFiles(v) => v.validate(), + Self::ReadTransfer(v) => v.validate(), + Self::CancelTransfer(v) => v.validate(), + Self::ListFiles(v) => v.validate(), + Self::ReadFile(v) => v.validate(), + Self::WriteFile(v) => v.validate(), + Self::EditFile(v) => v.validate(), + Self::MakeDirectory(v) => v.validate(), + Self::DeletePath(v) => v.validate(), + Self::MovePath(v) => v.validate(), _ => Ok(()), } } diff --git a/crates/ai/src/tools/catalog.rs b/crates/ai/src/tools/catalog.rs index d592a103..9c1c1db8 100644 --- a/crates/ai/src/tools/catalog.rs +++ b/crates/ai/src/tools/catalog.rs @@ -5,20 +5,58 @@ use serde_json::{Value, json}; pub struct ToolSpec { pub name: &'static str, pub label: &'static str, + /// Terminal tools are always listed; file tools only while a chat offers them. + pub group: ToolGroup, pub description: &'static str, pub schema: fn() -> Value, /// Shape only: execution limits are checked separately by `validate`. pub parse: fn(Value) -> Option, } +/// The tools an agent is given together. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ToolGroup { + /// The terminals, commands and transfers of a chat: always offered. + Terminals, + /// The files of attached terminals that serve files but run no programs. + Files, +} + +/// What a chat offers its agent now beyond the terminal tools. The bridge lists and admits only +/// these, and tells a connected agent when the offer changes. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct ToolOffer { + files: bool, +} + +impl ToolOffer { + /// The terminal tools and, when `files` is true, the file tools. + pub fn new(files: bool) -> Self { + Self { files } + } + + pub fn offers(self, group: ToolGroup) -> bool { + match group { + ToolGroup::Terminals => true, + ToolGroup::Files => self.files, + } + } + + /// The tools listed under this offer, in catalog order. + pub fn tools(self) -> impl Iterator { + TOOLS.iter().filter(move |spec| self.offers(spec.group)) + } +} + // The exhaustive name match makes each TerminalCall variant require a row here. macro_rules! catalog { - ($($variant:ident $(($arguments:ty))? => $name:literal, $label:literal, $description:literal;)*) => { + ($($variant:ident $(($arguments:ty))? => $name:literal, $label:literal, $group:ident, $description:literal;)*) => { enum Index { $($variant,)* } pub static TOOLS: &[ToolSpec] = &[$(ToolSpec { name: $name, label: $label, + group: ToolGroup::$group, description: $description, schema: catalog!(@schema $($arguments)?), parse: catalog!(@parse $variant $($arguments)?), @@ -30,6 +68,11 @@ macro_rules! catalog { $(catalog!(@pattern $variant $($arguments)?) => TOOLS[Index::$variant as usize].name,)* } } + pub fn group(&self) -> ToolGroup { + match self { + $(catalog!(@pattern $variant $($arguments)?) => TOOLS[Index::$variant as usize].group,)* + } + } } }; (@pattern $variant:ident) => { Self::$variant }; @@ -43,22 +86,42 @@ macro_rules! catalog { } catalog! { - ListTerminals => "list_terminals", "List terminals", + ListTerminals => "list_terminals", "List terminals", Terminals, "List terminals attached to this chat, and attached servers that have no session yet (offline_servers). These are the only servers you can reach: nocterm holds their credentials, so never connect on your own with ssh. When the list is empty, tell the user to attach a terminal or server to the chat"; - OpenTerminal(OpenTerminal) => "open_terminal", "Connect", + OpenTerminal(OpenTerminal) => "open_terminal", "Connect", Terminals, "Connect to an attached offline server in the background, without disturbing the user; returns its terminal_id once connected. If it fails, tell the user in one short sentence why and stop"; - ReadTerminal(ReadTerminal) => "read_terminal", "Read terminal", + ReadTerminal(ReadTerminal) => "read_terminal", "Read terminal", Terminals, "Read bounded output of an attached terminal"; - SendInput(SendInput) => "send_input", "Send input", + SendInput(SendInput) => "send_input", "Send input", Terminals, "Send text to an attached terminal with user approval"; - RunCommand(RunCommand) => "run_command", "Run command", + RunCommand(RunCommand) => "run_command", "Run command", Terminals, "Type one command into a known empty live shell prompt. Requires shell integration; an output pause never proves completion. Prefer exec_command for a reliable exit status"; - ExecCommand(ExecCommand) => "exec_command", "Execute program", + ExecCommand(ExecCommand) => "exec_command", "Execute program", Terminals, "Run program with separate args and optional stdin on the attached terminal host over its current connection. Local execution passes args directly; SSH renders a POSIX command line, so arbitrary args require a POSIX-compatible remote command shell and are not guaranteed on Windows SSH. Fresh process: no inherited terminal cwd, environment, aliases or functions. Use an explicit shell for shell syntax. Returns actual exit status or an owned command_id for read_command/cancel_command. Output is bounded and continuously drained"; - ReadCommand(ReadCommand) => "read_command", "Read command", + ReadCommand(ReadCommand) => "read_command", "Read command", Terminals, "Read a bounded full snapshot of a command owned by this chat. Does not start or cancel a command; optional yield_ms waits briefly"; - CancelCommand(CancelCommand) => "cancel_command", "Cancel command", + CancelCommand(CancelCommand) => "cancel_command", "Cancel command", Terminals, "Cancel a command owned by this chat. SSH sends TERM and closes its channel; remote descendant termination is best effort"; + TransferFiles(TransferFiles) => "transfer_files", "Transfer files", Terminals, + "Copy files and folders between the hosts of two attached terminals: this computer (a local terminal) to a server, a server to this computer, or a server to another server. nocterm relays the data over the connections it already holds, so servers need no credentials for each other and you must never use scp, rsync or ssh between them. Every path is absolute (no ~, no .. components). sources are files or folders on the source host; folders copy recursively, and each source is copied into destination under its own name. destination must be an existing folder; it is never created. Existing files are skipped and counted as skipped unless overwrite is true. Symbolic links and special files are skipped. At most 64 sources per call. Two local terminals are refused: use exec_command. Some environments only allow copies within one server. The copy runs in the background: after optional yield_ms (default 1000) it returns the current state and a transfer_id for read_transfer/cancel_transfer. Poll read_transfer until the state is completed, failed or cancelled. Whole files are published atomically; a cancelled or failed file leaves no partial file"; + ReadTransfer(ReadTransfer) => "read_transfer", "Read transfer", Terminals, + "Read a snapshot of a transfer started by this chat: state, file and byte counts and up to 16 failures. Does not start or cancel a transfer; optional yield_ms waits briefly while it is active"; + CancelTransfer(CancelTransfer) => "cancel_transfer", "Cancel transfer", Terminals, + "Cancel a transfer started by this chat. Files already published stay in place; files being copied are discarded"; + ListFiles(ListFiles) => "list_files", "List files", Files, + "List a folder of an attached terminal whose list_terminals entry has file_tools: true. Such a terminal serves files but runs no programs (for example a console tab a plugin opened for a game server panel): exec_command and run_command cannot reach its files, so use the file tools for them. Paths are absolute from / with no ~, . or .. parts. Folders come first; at most 1000 entries. The file tools act only on terminals with file_tools: true, never on other terminals or this computer"; + ReadFile(ReadFile) => "read_file", "Read file", Files, + "Read a UTF-8 text file of an attached terminal with file_tools: true, up to 1 MiB, a range of lines at a time (start_line from 1, max_lines up to 2000, at most 64 KiB per answer; next_start_line says where to continue). The content is exactly the file's text, except where it is shown as [REDACTED]: such text is hidden from you, so change other parts with edit_file and never write the marker back"; + WriteFile(WriteFile) => "write_file", "Write file", Files, + "Write a whole UTF-8 text file (at most 512 KiB) on an attached terminal with file_tools: true, with user approval. An existing file is refused unless overwrite is true; prefer edit_file to change part of a file. The parent folder must exist"; + EditFile(EditFile) => "edit_file", "Edit file", Files, + "Replace exact text in a file of an attached terminal with file_tools: true, with user approval. old_text must occur exactly once, copied exactly from read_file with its whitespace, unless replace_all is true. Send only the part that changes; the file is changed on its host"; + MakeDirectory(MakeDirectory) => "make_directory", "Create folder", Files, + "Create a folder on an attached terminal with file_tools: true, with user approval. Its parent must exist; an existing folder is accepted"; + DeletePath(DeletePath) => "delete_path", "Delete", Files, + "Delete a file or folder on an attached terminal with file_tools: true, with user approval. A folder is removed as the terminal's host removes folders, which may include its contents; it cannot be undone"; + MovePath(MovePath) => "move_path", "Move", Files, + "Move or rename a file or folder within one attached terminal with file_tools: true, with user approval. Both paths are absolute"; } pub fn get(name: &str) -> Option<&'static ToolSpec> { diff --git a/crates/ai/src/tools/catalog/tests.rs b/crates/ai/src/tools/catalog/tests.rs index df4d3a4b..18057d9e 100644 --- a/crates/ai/src/tools/catalog/tests.rs +++ b/crates/ai/src/tools/catalog/tests.rs @@ -10,18 +10,35 @@ fn fixtures() -> Vec { serde_json::from_str(include_str!("../fixtures/calls.json")).unwrap() } +/// Every tool offered, as while a terminal with file tools is attached. +const ALL: ToolOffer = ToolOffer { files: true }; + fn session() -> McpSession { let mut session = McpSession::default(); - session.handle(r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#); + session.handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + ToolOffer::default(), + ); session } fn invoke(session: &mut McpSession, name: &str, arguments: &Value, id: Value) -> McpStep { + invoke_under(session, name, arguments, id, ALL) +} + +fn invoke_under( + session: &mut McpSession, + name: &str, + arguments: &Value, + id: Value, + offer: ToolOffer, +) -> McpStep { session.handle( &json!({"jsonrpc":"2.0","id":id,"method":"tools/call", "params":{ "name":name,"arguments":arguments }}) .to_string(), + offer, ) } @@ -36,6 +53,16 @@ fn correct_variant(name: &str, call: &TerminalCall) -> bool { | ("exec_command", TerminalCall::ExecCommand(_)) | ("read_command", TerminalCall::ReadCommand(_)) | ("cancel_command", TerminalCall::CancelCommand(_)) + | ("transfer_files", TerminalCall::TransferFiles(_)) + | ("read_transfer", TerminalCall::ReadTransfer(_)) + | ("cancel_transfer", TerminalCall::CancelTransfer(_)) + | ("list_files", TerminalCall::ListFiles(_)) + | ("read_file", TerminalCall::ReadFile(_)) + | ("write_file", TerminalCall::WriteFile(_)) + | ("edit_file", TerminalCall::EditFile(_)) + | ("make_directory", TerminalCall::MakeDirectory(_)) + | ("delete_path", TerminalCall::DeletePath(_)) + | ("move_path", TerminalCall::MovePath(_)) ) } @@ -43,19 +70,85 @@ fn correct_variant(name: &str, call: &TerminalCall) -> bool { fn tools_list_preserves_original_order_descriptions_and_schemas() { let expected: Value = serde_json::from_str(include_str!("../fixtures/tools-list.json")).unwrap(); - let McpStep::Reply(actual) = - session().handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/list"}"#) - else { + let McpStep::Reply(actual) = session().handle( + r#"{"jsonrpc":"2.0","id":2,"method":"tools/list"}"#, + ToolOffer::default(), + ) else { panic!("expected tools/list reply"); }; - assert_eq!(actual, expected); + assert_eq!( + actual, expected, + "without file terminals the list is as before" + ); +} + +#[test] +fn file_tools_are_listed_and_admitted_only_while_offered() { + let listed = |offer| { + let McpStep::Reply(reply) = + session().handle(r#"{"jsonrpc":"2.0","id":2,"method":"tools/list"}"#, offer) + else { + panic!("expected tools/list reply"); + }; + reply["result"]["tools"] + .as_array() + .unwrap() + .iter() + .map(|tool| tool["name"].as_str().unwrap().to_owned()) + .collect::>() + }; + let without = listed(ToolOffer::default()); + let with = listed(ToolOffer::new(true)); + assert!(!without.iter().any(|name| name == "read_file")); + assert_eq!(&with[..without.len()], without.as_slice()); + assert_eq!( + &with[without.len()..], + [ + "list_files", + "read_file", + "write_file", + "edit_file", + "make_directory", + "delete_path", + "move_path" + ] + ); + let arguments = json!({"terminal_id":"t3","path":"/server.properties"}); + let McpStep::Rejected { error, .. } = invoke_under( + &mut session(), + "read_file", + &arguments, + json!(5), + ToolOffer::default(), + ) else { + panic!("a tool that is not offered is refused"); + }; + assert!(error.contains("file_tools"), "{error}"); + assert!(matches!( + invoke_under(&mut session(), "read_file", &arguments, json!(6), ALL), + McpStep::Call { + call: TerminalCall::ReadFile(_), + .. + } + )); + let McpStep::Reply(initialized) = McpSession::default().handle( + r#"{"jsonrpc":"2.0","id":1,"method":"initialize"}"#, + ToolOffer::default(), + ) else { + panic!("expected initialize reply"); + }; + assert_eq!( + initialized["result"]["capabilities"]["tools"]["listChanged"], + json!(true), + "the agent is told the list may change" + ); } #[test] fn every_variant_round_trips_through_a_unique_catalogue_entry() { let fixtures = fixtures(); let names: BTreeSet<_> = TOOLS.iter().map(|spec| spec.name).collect(); - assert_eq!(TOOLS.len(), 8); + assert_eq!(TOOLS.len(), 18); assert_eq!(TOOLS.len(), names.len()); assert_eq!(fixtures.len(), names.len()); assert_eq!( @@ -112,7 +205,7 @@ fn mcp_preserves_original_arguments_ids_and_optional_defaults_for_all_tools() { } } assert!( - matches!(session.handle(r#"{"jsonrpc":"2.0","id":42,"method":"tools/call","params":{"name":"list_terminals"}}"#), + matches!(session.handle(r#"{"jsonrpc":"2.0","id":42,"method":"tools/call","params":{"name":"list_terminals"}}"#, ToolOffer::default()), McpStep::Call { call: TerminalCall::ListTerminals, arguments, .. } if arguments == json!({})) ); } @@ -137,6 +230,16 @@ fn catalogue_and_mcp_reject_unknown_tools_bad_shapes_and_extra_fields() { ), ("open_terminal", json!({"server_id":null})), ("read_command", json!({"terminal_id":"t1"})), + ( + "transfer_files", + json!({"source_terminal_id":"t1","sources":"/a","destination_terminal_id":"t2","destination":"/b"}), + ), + ( + "transfer_files", + json!({"source_terminal_id":"t1","sources":["/a"],"destination_terminal_id":"t2","destination":"/b","extra":true}), + ), + ("read_transfer", json!({"transfer_id":7})), + ("cancel_transfer", json!({})), ] { assert!( get(name) @@ -209,3 +312,17 @@ fn both_metadata_versions_use_the_same_shape_parser_without_losing_outcomes() { } } } + +#[test] +fn canonical_tool_arguments_round_trip_through_every_existing_catalog_schema() { + let fixtures: Value = serde_json::from_str(include_str!("../fixtures/calls.json")).unwrap(); + for fixture in fixtures.as_array().unwrap() { + let name = fixture["name"].as_str().unwrap(); + let spec = get(name).unwrap(); + let call = (spec.parse)(fixture["arguments"].clone()).unwrap(); + let arguments = call.arguments().unwrap(); + let restored = (spec.parse)(arguments.clone()).unwrap(); + assert_eq!(restored.arguments().unwrap(), arguments); + assert_eq!(restored.name(), name); + } +} diff --git a/crates/ai/src/tools/failure.rs b/crates/ai/src/tools/failure.rs index 2e408bbb..42658f11 100644 --- a/crates/ai/src/tools/failure.rs +++ b/crates/ai/src/tools/failure.rs @@ -21,6 +21,26 @@ pub enum TerminalCallError { ReadCommandRejected, #[error("Invalid command id")] InvalidCommandId, + #[error( + "Transfer needs 1–64 absolute source paths and one destination folder (each at most 4096 bytes without control characters, sources at most 64 KiB in total) and a yield of at most 10000 ms" + )] + TransferRejected, + #[error("Invalid transfer id or yield interval")] + ReadTransferRejected, + #[error("Invalid transfer id")] + InvalidTransferId, + #[error( + "File paths are absolute, at most 4096 bytes, without control characters and without empty, `.` or `..` parts; a change or a read names something below `/`, and a move does not go into itself" + )] + FilePathRejected, + #[error("start_line is at least 1 and max_lines 1–2000")] + ReadRangeRejected, + #[error("Content is text of at most 512 KiB without NUL")] + FileContentRejected, + #[error( + "old_text is nonempty, old_text and new_text are at most 64 KiB each without NUL, and they differ" + )] + EditRejected, } /// Agent-facing text of a failed tool call. @@ -61,3 +81,18 @@ impl From for ToolFailure { error.to_string().into() } } + +/// Serialization failed without discarding its original concrete cause. +#[derive(Clone, thiserror::Error)] +#[error("Terminal tool arguments could not be encoded")] +pub struct ToolArgumentsError(#[source] nocterm_core::SharedError); +impl From for ToolArgumentsError { + fn from(error: serde_json::Error) -> Self { + Self(nocterm_core::SharedError::from_error(error)) + } +} +impl std::fmt::Debug for ToolArgumentsError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.write_str("ToolArgumentsError") + } +} diff --git a/crates/ai/src/tools/files.rs b/crates/ai/src/tools/files.rs new file mode 100644 index 00000000..4d5783dc --- /dev/null +++ b/crates/ai/src/tools/files.rs @@ -0,0 +1,205 @@ +//! File tools of attached terminals that serve files but run no programs: arguments, bounds, +//! and the work over the terminal's filesystem. +//! +//! An agent reaches such a terminal's files only through these tools: it cannot run a program +//! there to read or change them. Paths are absolute without `.` or `..` components; files are +//! read and written whole as UTF-8 text within the bounds below, and an edit replaces exact +//! text, so an agent sends only what changes. +use super::TerminalCallError; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +mod work; + +pub use work::run; + +pub const MAX_FILE_PATH_BYTES: usize = 4096; +/// The most text one `read_file` answer carries. +pub const MAX_FILE_READ_BYTES: usize = 64 * 1024; +/// The largest file read or edited whole. +pub const MAX_FILE_LOAD_BYTES: usize = 1024 * 1024; +/// The largest content `write_file` takes. +pub const MAX_FILE_WRITE_BYTES: usize = 512 * 1024; +/// The largest `old_text` or `new_text` of an edit. +pub const MAX_EDIT_BYTES: usize = 64 * 1024; +pub const MAX_LISTED_ENTRIES: usize = 1000; +pub const MAX_READ_FILE_LINES: usize = 2000; + +/// List a folder of an attached terminal that has `file_tools`. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ListFiles { + pub terminal_id: String, + /// Absolute path of the folder; `/` is the top. + pub path: String, +} + +/// Read a text file, a range of its lines at a time. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ReadFile { + pub terminal_id: String, + pub path: String, + /// First line to read, from 1 (default 1). + pub start_line: Option, + /// Lines to read, 1–2000 (default 2000); an answer also stops at 64 KiB. + pub max_lines: Option, +} + +/// Write a whole text file. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct WriteFile { + pub terminal_id: String, + pub path: String, + /// The whole new content, UTF-8 text of at most 512 KiB. + pub content: String, + /// Replace a file that exists; by default an existing file is refused. + #[serde(default)] + pub overwrite: bool, +} + +/// Replace exact text in a file. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct EditFile { + pub terminal_id: String, + pub path: String, + /// Text that occurs in the file exactly once, unless `replace_all` is true. + pub old_text: String, + /// The text it becomes. + pub new_text: String, + /// Replace every occurrence. + #[serde(default)] + pub replace_all: bool, +} + +/// Create a folder; its parent must exist. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct MakeDirectory { + pub terminal_id: String, + pub path: String, +} + +/// Delete a file, or a folder as the terminal's host removes one. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct DeletePath { + pub terminal_id: String, + pub path: String, +} + +/// Move or rename a file or folder. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct MovePath { + pub terminal_id: String, + pub from: String, + pub to: String, +} + +/// An absolute path with no empty, `.` or `..` component and no control character, so a path +/// names one place and reads plainly on an approval card. +pub fn path_ok(path: &str) -> bool { + path.len() <= MAX_FILE_PATH_BYTES + && !path.chars().any(char::is_control) + && (path == "/" + || path.strip_prefix('/').is_some_and(|rest| { + rest.split('/') + .all(|part| !part.is_empty() && part != "." && part != "..") + })) +} + +fn paths(paths: &[&str]) -> Result<(), TerminalCallError> { + if paths.iter().all(|path| path_ok(path)) { + Ok(()) + } else { + Err(TerminalCallError::FilePathRejected) + } +} + +/// A path below the top, which a change may name. +fn below_top(path: &str) -> Result<(), TerminalCallError> { + paths(&[path])?; + if path == "/" { + Err(TerminalCallError::FilePathRejected) + } else { + Ok(()) + } +} + +impl ListFiles { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + paths(&[&self.path]) + } +} + +impl ReadFile { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path)?; + if self.start_line == Some(0) + || self + .max_lines + .is_some_and(|n| n == 0 || n > MAX_READ_FILE_LINES) + { + return Err(TerminalCallError::ReadRangeRejected); + } + Ok(()) + } +} + +impl WriteFile { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path)?; + if self.content.len() > MAX_FILE_WRITE_BYTES || self.content.contains('\0') { + return Err(TerminalCallError::FileContentRejected); + } + Ok(()) + } +} + +impl EditFile { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path)?; + if self.old_text.is_empty() + || self.old_text.len() > MAX_EDIT_BYTES + || self.new_text.len() > MAX_EDIT_BYTES + || self.old_text == self.new_text + || self.new_text.contains('\0') + { + return Err(TerminalCallError::EditRejected); + } + Ok(()) + } +} + +impl MakeDirectory { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path) + } +} + +impl DeletePath { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.path) + } +} + +impl MovePath { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + below_top(&self.from)?; + below_top(&self.to)?; + let inside = self + .to + .strip_prefix(&self.from) + .is_some_and(|rest| rest.is_empty() || rest.starts_with('/')); + if inside { + return Err(TerminalCallError::FilePathRejected); + } + Ok(()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ai/src/tools/files/tests.rs b/crates/ai/src/tools/files/tests.rs new file mode 100644 index 00000000..a994052e --- /dev/null +++ b/crates/ai/src/tools/files/tests.rs @@ -0,0 +1,254 @@ +//! File tools: their bounds, and their work on a terminal's filesystem. +use std::sync::Arc; + +use futures::executor::block_on; +use nocterm_session::{RemoteFs, testing::InMemoryFs}; +use serde_json::{Value, json}; + +use super::*; +use crate::{TerminalCall, TerminalCallError, ToolCapability, ToolFailure, ToolGroup}; + +fn files() -> InMemoryFs { + let fs = InMemoryFs::default(); + fs.seed_file("/server.properties", "motd=Hi\nmax-players=20\npvp=true\n") + .unwrap(); + fs.seed_file("/plugins/a.yml", "a: 1\n").unwrap(); + fs +} + +fn run_on(fs: &InMemoryFs, call: TerminalCall, redact: bool) -> Result { + let shared: Arc = Arc::new(fs.clone()); + block_on(run(call, shared, redact)) +} + +fn text(fs: &InMemoryFs, path: &str) -> String { + String::from_utf8(fs.bytes(path).unwrap()).unwrap() +} + +fn edit(old: &str, new: &str, replace_all: bool) -> TerminalCall { + TerminalCall::EditFile(EditFile { + terminal_id: "t1".into(), + path: "/server.properties".into(), + old_text: old.into(), + new_text: new.into(), + replace_all, + }) +} + +#[test] +fn paths_name_one_place_below_the_top() { + for path in ["/", "/a", "/a/b.txt", "/with space/ü"] { + assert!(path_ok(path), "{path}"); + } + for path in ["", "a", "/a/", "//a", "/a/../b", "/./a", "/a\nb", "~/a"] { + assert!(!path_ok(path), "{path:?}"); + } + let delete = |path: &str| { + TerminalCall::DeletePath(DeletePath { + terminal_id: "t1".into(), + path: path.into(), + }) + .validate() + }; + assert_eq!(delete("/"), Err(TerminalCallError::FilePathRejected)); + assert_eq!(delete("/logs"), Ok(())); + let moved = TerminalCall::MovePath(MovePath { + terminal_id: "t1".into(), + from: "/world".into(), + to: "/world/inside".into(), + }); + assert_eq!(moved.validate(), Err(TerminalCallError::FilePathRejected)); + assert_eq!( + edit("same", "same", false).validate(), + Err(TerminalCallError::EditRejected) + ); +} + +#[test] +fn reading_is_a_read_and_every_change_needs_the_files_capability() { + let read = TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/a".into(), + start_line: None, + max_lines: None, + }); + assert!(!read.writes()); + assert_eq!(read.capability(), ToolCapability::Read); + assert_eq!(read.group(), ToolGroup::Files); + let change = edit("a", "b", false); + assert!(change.writes() && change.is_file_tool()); + assert_eq!(change.capability(), ToolCapability::Files); + assert_eq!(change.terminal_id(), Some("t1")); +} + +#[test] +fn a_listing_shows_folders_first_with_the_sizes_of_files() { + let listed = run_on( + &files(), + TerminalCall::ListFiles(ListFiles { + terminal_id: "t1".into(), + path: "/".into(), + }), + false, + ) + .unwrap(); + assert_eq!( + listed["entries"], + json!([ + {"name": "plugins", "type": "directory"}, + {"name": "server.properties", "type": "file", "size": 32} + ]) + ); + assert_eq!(listed["truncated"], json!(false)); +} + +#[test] +fn a_file_is_read_a_range_of_lines_at_a_time() { + let fs = files(); + let read = |start, max| { + run_on( + &fs, + TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/server.properties".into(), + start_line: start, + max_lines: max, + }), + false, + ) + .unwrap() + }; + let first = read(None, Some(2)); + assert_eq!(first["content"], json!("motd=Hi\nmax-players=20\n")); + assert_eq!(first["total_lines"], json!(3)); + assert_eq!(first["next_start_line"], json!(3)); + let rest = read(Some(3), None); + assert_eq!(rest["content"], json!("pvp=true\n")); + assert!(rest.get("next_start_line").is_none()); +} + +#[test] +fn an_edit_replaces_exact_text_once_or_says_why_not() { + let fs = files(); + let answer = run_on(&fs, edit("max-players=20", "max-players=40", false), false).unwrap(); + assert_eq!(answer["replacements"], json!(1)); + assert_eq!( + text(&fs, "/server.properties"), + "motd=Hi\nmax-players=40\npvp=true\n" + ); + let missing = run_on(&fs, edit("max-players=20", "x", false), false).unwrap_err(); + assert!(missing.message().contains("not found"), "{missing}"); + let twice = run_on(&fs, edit("=", ":", false), false).unwrap_err(); + assert!(twice.message().contains("occurs 3 times"), "{twice}"); + let all = run_on(&fs, edit("=", ":", true), false).unwrap(); + assert_eq!(all["replacements"], json!(3)); + assert_eq!( + text(&fs, "/server.properties"), + "motd:Hi\nmax-players:40\npvp:true\n" + ); +} + +#[test] +fn a_write_creates_a_file_and_replaces_one_only_when_asked() { + let fs = files(); + let write = |path: &str, overwrite| { + TerminalCall::WriteFile(WriteFile { + terminal_id: "t1".into(), + path: path.into(), + content: "[]\n".into(), + overwrite, + }) + }; + run_on(&fs, write("/ops.json", false), false).unwrap(); + assert_eq!(text(&fs, "/ops.json"), "[]\n"); + let refused = run_on(&fs, write("/server.properties", false), false).unwrap_err(); + assert!(refused.message().contains("set overwrite"), "{refused}"); + run_on(&fs, write("/server.properties", true), false).unwrap(); + assert_eq!(text(&fs, "/server.properties"), "[]\n"); +} + +#[test] +fn hidden_secrets_are_never_written_back_but_the_rest_of_the_file_can_change() { + let fs = InMemoryFs::default(); + fs.seed_file("/config.yml", "name: lobby\npassword: hunter2\n") + .unwrap(); + let read = run_on( + &fs, + TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/config.yml".into(), + start_line: None, + max_lines: None, + }), + true, + ) + .unwrap(); + assert_eq!(read["redacted"], json!(true)); + let shown = read["content"].as_str().unwrap(); + assert!(!shown.contains("hunter2"), "{shown}"); + let rewrite = TerminalCall::WriteFile(WriteFile { + terminal_id: "t1".into(), + path: "/config.yml".into(), + content: shown.replace("lobby", "hub"), + overwrite: true, + }); + assert!(run_on(&fs, rewrite, true).is_err()); + let rename = TerminalCall::EditFile(EditFile { + terminal_id: "t1".into(), + path: "/config.yml".into(), + old_text: "name: lobby".into(), + new_text: "name: hub".into(), + replace_all: false, + }); + run_on(&fs, rename, true).unwrap(); + assert_eq!(text(&fs, "/config.yml"), "name: hub\npassword: hunter2\n"); +} + +#[test] +fn folders_are_made_moved_and_deleted() { + let fs = files(); + let call = |call| run_on(&fs, call, false); + call(TerminalCall::MakeDirectory(MakeDirectory { + terminal_id: "t1".into(), + path: "/backups".into(), + })) + .unwrap(); + call(TerminalCall::MovePath(MovePath { + terminal_id: "t1".into(), + from: "/plugins/a.yml".into(), + to: "/backups/a.yml".into(), + })) + .unwrap(); + assert_eq!(text(&fs, "/backups/a.yml"), "a: 1\n"); + let deleted = call(TerminalCall::DeletePath(DeletePath { + terminal_id: "t1".into(), + path: "/plugins".into(), + })) + .unwrap(); + assert_eq!(deleted["deleted"], json!("directory")); + let gone = call(TerminalCall::DeletePath(DeletePath { + terminal_id: "t1".into(), + path: "/plugins".into(), + })) + .unwrap_err(); + assert!(gone.message().contains("no such file"), "{gone}"); +} + +#[test] +fn a_binary_file_is_refused_with_the_way_to_copy_it() { + let fs = InMemoryFs::default(); + fs.seed_file("/world/level.dat", vec![0x1f, 0x8b, 0xff, 0x00]) + .unwrap(); + let refused = run_on( + &fs, + TerminalCall::ReadFile(ReadFile { + terminal_id: "t1".into(), + path: "/world/level.dat".into(), + start_line: None, + max_lines: None, + }), + false, + ) + .unwrap_err(); + assert!(refused.message().contains("transfer_files"), "{refused}"); +} diff --git a/crates/ai/src/tools/files/work.rs b/crates/ai/src/tools/files/work.rs new file mode 100644 index 00000000..61064367 --- /dev/null +++ b/crates/ai/src/tools/files/work.rs @@ -0,0 +1,243 @@ +//! The work of one file tool call over a terminal's filesystem, and the answer the agent reads. +use std::sync::Arc; + +use nocterm_session::{ + EntryKind, FsError, RemoteFs, + fs::{DownloadReadError, UploadMode, read_bounded}, +}; +use serde_json::{Value, json}; + +use super::{ + DeletePath, EditFile, MAX_FILE_LOAD_BYTES, MAX_FILE_READ_BYTES, MAX_LISTED_ENTRIES, + MAX_READ_FILE_LINES, ReadFile, WriteFile, +}; +use crate::{TerminalCall, ToolFailure}; + +/// What redaction leaves where it hid text. +const REDACTED: &str = "[REDACTED"; +/// The chunk a file is read or written in. +const CHUNK: usize = 32 * 1024; + +/// Runs the file tool call `call` on `fs`. Text read is redacted when `redact` is set, and a +/// write that would put a redaction marker into a file is then refused: hidden text must never +/// be replaced by its marker. +pub async fn run( + call: TerminalCall, + fs: Arc, + redact: bool, +) -> Result { + let fs = fs.as_ref(); + match call { + TerminalCall::ListFiles(request) => list(fs, &request.path).await, + TerminalCall::ReadFile(request) => read(fs, &request, redact).await, + TerminalCall::WriteFile(request) => write(fs, &request, redact).await, + TerminalCall::EditFile(request) => edit(fs, &request).await, + TerminalCall::MakeDirectory(request) => { + fs.create_dir(&request.path).await.map_err(failed)?; + Ok(json!({"path": request.path, "created": true})) + } + TerminalCall::DeletePath(request) => delete(fs, &request).await, + TerminalCall::MovePath(request) => { + fs.rename(&request.from, &request.to) + .await + .map_err(failed)?; + Ok(json!({"from": request.from, "to": request.to})) + } + _ => Err("This call is not a file tool.".into()), + } +} + +fn failed(error: FsError) -> ToolFailure { + error.to_string().into() +} + +fn kind(kind: EntryKind) -> &'static str { + match kind { + EntryKind::Directory => "directory", + EntryKind::File => "file", + EntryKind::Other => "other", + } +} + +/// Folders first, then files, each by name; at most [`MAX_LISTED_ENTRIES`]. +async fn list(fs: &dyn RemoteFs, path: &str) -> Result { + let mut entries = fs.read_dir(path).await.map_err(failed)?; + entries.sort_by(|a, b| { + (a.kind != EntryKind::Directory, &a.name).cmp(&(b.kind != EntryKind::Directory, &b.name)) + }); + let truncated = entries.len() > MAX_LISTED_ENTRIES; + let listed: Vec = entries + .iter() + .take(MAX_LISTED_ENTRIES) + .map( + |entry| match entry.size.filter(|_| entry.kind == EntryKind::File) { + Some(size) => json!({"name": entry.name, "type": kind(entry.kind), "size": size}), + None => json!({"name": entry.name, "type": kind(entry.kind)}), + }, + ) + .collect(); + Ok(json!({"path": path, "entries": listed, "truncated": truncated})) +} + +/// The whole file as text, at most [`MAX_FILE_LOAD_BYTES`]. +async fn load(fs: &dyn RemoteFs, path: &str) -> Result { + let mut download = fs.download(path).await.map_err(failed)?; + let bytes = read_bounded(download.as_mut(), MAX_FILE_LOAD_BYTES, CHUNK).await; + let _ = download.close().await; + let bytes = bytes.map_err(|error| -> ToolFailure { + match error { + DownloadReadError::TooLarge { .. } => format!( + "{path}: larger than {} KiB; copy it with transfer_files instead", + MAX_FILE_LOAD_BYTES / 1024 + ) + .into(), + DownloadReadError::Read(error) => failed(error), + other => other.to_string().into(), + } + })?; + String::from_utf8(bytes) + .map_err(|_| format!("{path}: not UTF-8 text; copy it with transfer_files instead").into()) +} + +/// Lines `start_line..` of the file, as many as fit [`MAX_FILE_READ_BYTES`]; a longer single +/// line is cut and said to be. +async fn read(fs: &dyn RemoteFs, request: &ReadFile, redact: bool) -> Result { + let text = load(fs, &request.path).await?; + let start = request.start_line.unwrap_or(1); + let max = request.max_lines.unwrap_or(MAX_READ_FILE_LINES); + let total = text.split_inclusive('\n').count(); + let (mut content, mut shown, mut cut) = (String::new(), 0, false); + for line in text.split_inclusive('\n').skip(start - 1).take(max) { + if content.len() + line.len() > MAX_FILE_READ_BYTES { + if shown == 0 { + let mut end = MAX_FILE_READ_BYTES; + while !line.is_char_boundary(end) { + end -= 1; + } + content.push_str(&line[..end]); + (shown, cut) = (1, true); + } + break; + } + content.push_str(line); + shown += 1; + } + let next = start + shown; + let mut answer = json!({ + "path": request.path, + "start_line": start, + "lines": shown, + "total_lines": total, + "content": content, + }); + if next <= total { + answer["next_start_line"] = json!(next); + } + if cut { + answer["line_cut"] = json!(true); + } + if redact { + let hidden = crate::redact::redact(&content); + if hidden != content { + answer["content"] = json!(hidden); + answer["redacted"] = json!(true); + answer["note"] = json!( + "Text shown as [REDACTED] is hidden, not the file's text: change other parts with edit_file." + ); + } + } + Ok(answer) +} + +/// Writes `bytes` as the whole file at `path`. +async fn store( + fs: &dyn RemoteFs, + path: &str, + bytes: &[u8], + mode: UploadMode, +) -> Result<(), ToolFailure> { + let refused = |error: FsError| -> ToolFailure { + match error { + FsError::AlreadyExists { .. } if mode == UploadMode::Create => format!( + "{path}: already exists; set overwrite to replace it, or change it with edit_file" + ) + .into(), + error => failed(error), + } + }; + let mut upload = fs.upload(path, mode).await.map_err(refused)?; + for chunk in bytes.chunks(CHUNK) { + if let Err(error) = upload.write(chunk.to_vec()).await { + let _ = upload.cancel().await; + return Err(refused(error)); + } + } + upload.finish().await.map_err(refused)?; + Ok(()) +} + +async fn write(fs: &dyn RemoteFs, request: &WriteFile, redact: bool) -> Result { + if redact && request.content.contains(REDACTED) { + return Err( + "The content holds a [REDACTED] marker, which would replace hidden text: change the file with edit_file instead." + .into(), + ); + } + let mode = if request.overwrite { + UploadMode::Replace + } else { + UploadMode::Create + }; + store(fs, &request.path, request.content.as_bytes(), mode).await?; + Ok(json!({"path": request.path, "bytes": request.content.len()})) +} + +/// Replaces `old_text` in the file's real text, which redaction never touched. +async fn edit(fs: &dyn RemoteFs, request: &EditFile) -> Result { + let path = &request.path; + let text = load(fs, path).await?; + let found = text.matches(request.old_text.as_str()).count(); + if found == 0 { + return Err(format!( + "{path}: old_text was not found. Read the file again and copy the text exactly; text shown as [REDACTED] cannot be matched." + ) + .into()); + } + if found > 1 && !request.replace_all { + return Err(format!( + "{path}: old_text occurs {found} times; include more surrounding text, or set replace_all." + ) + .into()); + } + let (changed, replaced) = if request.replace_all { + (text.replace(&request.old_text, &request.new_text), found) + } else { + (text.replacen(&request.old_text, &request.new_text, 1), 1) + }; + if changed.len() > MAX_FILE_LOAD_BYTES { + return Err(format!( + "{path}: the edited file would be larger than {} KiB", + MAX_FILE_LOAD_BYTES / 1024 + ) + .into()); + } + store(fs, path, changed.as_bytes(), UploadMode::Replace).await?; + Ok(json!({"path": path, "replacements": replaced, "bytes": changed.len()})) +} + +/// Removes a folder as a folder and anything else as a file. +async fn delete(fs: &dyn RemoteFs, request: &DeletePath) -> Result { + let path = &request.path; + let directory = match fs.stat(path).await { + Ok(Some(entry)) => entry.kind == EntryKind::Directory && !entry.is_symlink, + Ok(None) => return Err(failed(FsError::NotFound { path: path.clone() })), + Err(FsError::Unsupported(_)) => false, + Err(error) => return Err(failed(error)), + }; + if directory { + fs.remove_dir(path).await.map_err(failed)?; + } else { + fs.remove_file(path).await.map_err(failed)?; + } + Ok(json!({"path": path, "deleted": if directory { "directory" } else { "file" }})) +} diff --git a/crates/ai/src/tools/fixtures/calls.json b/crates/ai/src/tools/fixtures/calls.json index de660c11..662d2d3e 100644 --- a/crates/ai/src/tools/fixtures/calls.json +++ b/crates/ai/src/tools/fixtures/calls.json @@ -94,5 +94,146 @@ "terminal_id": "t1", "command_id": "cmd1" } + }, + { + "name": "transfer_files", + "label": "Transfer files", + "arguments": { + "source_terminal_id": "t1", + "sources": [ + "/srv/a", + "/srv/dir b" + ], + "destination_terminal_id": "t2", + "destination": "/home/alice/in" + }, + "normalized": { + "source_terminal_id": "t1", + "sources": [ + "/srv/a", + "/srv/dir b" + ], + "destination_terminal_id": "t2", + "destination": "/home/alice/in", + "overwrite": false, + "yield_ms": null + } + }, + { + "name": "read_transfer", + "label": "Read transfer", + "arguments": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819" + }, + "normalized": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819", + "yield_ms": null + } + }, + { + "name": "cancel_transfer", + "label": "Cancel transfer", + "arguments": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819" + }, + "normalized": { + "transfer_id": "0a0b0c0d-0e0f-1011-1213-141516171819" + } + }, + { + "name": "list_files", + "label": "List files", + "arguments": { + "terminal_id": "t3", + "path": "/plugins" + }, + "normalized": { + "terminal_id": "t3", + "path": "/plugins" + } + }, + { + "name": "read_file", + "label": "Read file", + "arguments": { + "terminal_id": "t3", + "path": "/server.properties" + }, + "normalized": { + "terminal_id": "t3", + "path": "/server.properties", + "start_line": null, + "max_lines": null + } + }, + { + "name": "write_file", + "label": "Write file", + "arguments": { + "terminal_id": "t3", + "path": "/ops.json", + "content": "[]\n" + }, + "normalized": { + "terminal_id": "t3", + "path": "/ops.json", + "content": "[]\n", + "overwrite": false + } + }, + { + "name": "edit_file", + "label": "Edit file", + "arguments": { + "terminal_id": "t3", + "path": "/server.properties", + "old_text": "max-players=20", + "new_text": "max-players=40" + }, + "normalized": { + "terminal_id": "t3", + "path": "/server.properties", + "old_text": "max-players=20", + "new_text": "max-players=40", + "replace_all": false + } + }, + { + "name": "make_directory", + "label": "Create folder", + "arguments": { + "terminal_id": "t3", + "path": "/plugins/Essentials" + }, + "normalized": { + "terminal_id": "t3", + "path": "/plugins/Essentials" + } + }, + { + "name": "delete_path", + "label": "Delete", + "arguments": { + "terminal_id": "t3", + "path": "/logs/old.log" + }, + "normalized": { + "terminal_id": "t3", + "path": "/logs/old.log" + } + }, + { + "name": "move_path", + "label": "Move", + "arguments": { + "terminal_id": "t3", + "from": "/world_old", + "to": "/backups/world_old" + }, + "normalized": { + "terminal_id": "t3", + "from": "/world_old", + "to": "/backups/world_old" + } } ] diff --git a/crates/ai/src/tools/fixtures/tools-list.json b/crates/ai/src/tools/fixtures/tools-list.json index d180aa55..bb5550c2 100644 --- a/crates/ai/src/tools/fixtures/tools-list.json +++ b/crates/ai/src/tools/fixtures/tools-list.json @@ -18,11 +18,11 @@ "inputSchema": { "$schema": "https://json-schema.org/draft/2020-12/schema", "title": "OpenTerminal", - "description": "Connects to an attached offline server without opening a tab.", + "description": "Opens an allowlisted server or reuses its already connected terminal.", "type": "object", "properties": { "server_id": { - "description": "An `offline_servers` entry of `list_terminals`.", + "description": "A `server_id` from the terminals or offline servers in `list_terminals`.", "type": "string" } }, @@ -233,6 +233,101 @@ "command_id" ] } + }, + { + "name": "transfer_files", + "description": "Copy files and folders between the hosts of two attached terminals: this computer (a local terminal) to a server, a server to this computer, or a server to another server. nocterm relays the data over the connections it already holds, so servers need no credentials for each other and you must never use scp, rsync or ssh between them. Every path is absolute (no ~, no .. components). sources are files or folders on the source host; folders copy recursively, and each source is copied into destination under its own name. destination must be an existing folder; it is never created. Existing files are skipped and counted as skipped unless overwrite is true. Symbolic links and special files are skipped. At most 64 sources per call. Two local terminals are refused: use exec_command. Some environments only allow copies within one server. The copy runs in the background: after optional yield_ms (default 1000) it returns the current state and a transfer_id for read_transfer/cancel_transfer. Poll read_transfer until the state is completed, failed or cancelled. Whole files are published atomically; a cancelled or failed file leaves no partial file", + "inputSchema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "TransferFiles", + "description": "Copy files and folders between the hosts of two attached terminals.", + "type": "object", + "properties": { + "source_terminal_id": { + "description": "terminal_id whose host holds the sources; a local terminal means this computer.", + "type": "string" + }, + "sources": { + "description": "Absolute paths of files or folders on the source host; folders copy recursively.", + "type": "array", + "items": { + "type": "string" + } + }, + "destination_terminal_id": { + "description": "terminal_id whose host receives the copies.", + "type": "string" + }, + "destination": { + "description": "Absolute path of an existing folder on the destination host.", + "type": "string" + }, + "overwrite": { + "description": "Replace files that already exist at the destination; by default they are skipped.", + "type": "boolean", + "default": false + }, + "yield_ms": { + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 0 + } + }, + "additionalProperties": false, + "required": [ + "source_terminal_id", + "sources", + "destination_terminal_id", + "destination" + ] + } + }, + { + "name": "read_transfer", + "description": "Read a snapshot of a transfer started by this chat: state, file and byte counts and up to 16 failures. Does not start or cancel a transfer; optional yield_ms waits briefly while it is active", + "inputSchema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "ReadTransfer", + "type": "object", + "properties": { + "transfer_id": { + "type": "string" + }, + "yield_ms": { + "type": [ + "integer", + "null" + ], + "format": "uint64", + "minimum": 0 + } + }, + "additionalProperties": false, + "required": [ + "transfer_id" + ] + } + }, + { + "name": "cancel_transfer", + "description": "Cancel a transfer started by this chat. Files already published stay in place; files being copied are discarded", + "inputSchema": { + "$schema": "https://json-schema.org/draft/2020-12/schema", + "title": "CancelTransfer", + "type": "object", + "properties": { + "transfer_id": { + "type": "string" + } + }, + "additionalProperties": false, + "required": [ + "transfer_id" + ] + } } ] } diff --git a/crates/ai/src/tools/output.rs b/crates/ai/src/tools/output.rs new file mode 100644 index 00000000..c89a3d3b --- /dev/null +++ b/crates/ai/src/tools/output.rs @@ -0,0 +1,97 @@ +//! Agent-facing observations are display data, never execution capabilities. +use nocterm_session::command_jobs::CommandSnapshot; +use serde::{Deserialize, Serialize}; + +/// A bounded terminal snapshot; overlapping line numbers replace earlier text. +#[derive(Serialize, Deserialize)] +pub struct TerminalReport { + pub text: String, + pub first_line: u64, + pub next_line: u64, + pub cursor_semantics: String, + pub truncated: bool, + pub alt_screen: bool, +} +impl TerminalReport { + pub const CURSOR_SEMANTICS: &str = "inclusive snapshot; replace overlapping lines"; + + pub fn new( + text: String, + first_line: u64, + next_line: u64, + truncated: bool, + alt_screen: bool, + ) -> Self { + Self { + text, + first_line, + next_line, + cursor_semantics: Self::CURSOR_SEMANTICS.into(), + truncated, + alt_screen, + } + } + + /// An observation timeout or idle output does not establish process completion. + pub fn observed(self, completion: String) -> ObservationReport { + ObservationReport { + completed: completion == "prompt_returned", + terminal: self, + completion, + exit_status: None, + } + } +} + +#[derive(Serialize, Deserialize)] +pub struct ObservationReport { + #[serde(flatten)] + pub terminal: TerminalReport, + pub completion: String, + pub completed: bool, + pub exit_status: Option, +} + +/// A snapshot of an admitted command. Its identifier carries no control authority. +#[derive(Serialize, Deserialize)] +pub struct CommandReport { + pub command_id: String, + pub state: String, + pub stdout: String, + pub stderr: String, + pub exit_status: Option, + pub total_bytes: u64, + pub truncated: bool, + pub error: Option, +} +impl CommandReport { + pub fn new(id: &str, snapshot: &CommandSnapshot, redact: bool) -> Self { + let text = |value: String| { + if redact { + crate::redact::redact(&value) + } else { + value + } + }; + Self { + command_id: id.into(), + state: snapshot.state().as_str().into(), + stdout: text(String::from_utf8_lossy(snapshot.stdout()).into_owned()), + stderr: text( + snapshot + .exit() + .map(|exit| exit.stderr.clone()) + .unwrap_or_default(), + ), + exit_status: snapshot.exit().and_then(|exit| exit.status), + total_bytes: snapshot.total_bytes(), + truncated: snapshot.total_bytes() > snapshot.stdout().len() as u64, + error: snapshot + .error() + .map(|error| crate::redact::redact(&error.to_string())), + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/ai/src/tools/output/tests.rs b/crates/ai/src/tools/output/tests.rs new file mode 100644 index 00000000..d3e22509 --- /dev/null +++ b/crates/ai/src/tools/output/tests.rs @@ -0,0 +1,65 @@ +use super::*; +use nocterm_session::{ExecError, ExecExit, command_jobs::CommandState}; +use serde_json::json; + +#[test] +fn command_wire_preserves_lossy_output_exit_counts_and_explicit_redaction_policy() { + let snapshot = CommandSnapshot { + state: CommandState::Exited, + stdout: b"password=secret\xff".to_vec(), + total_bytes: 100, + exit: Some(ExecExit { + status: Some(7), + stderr: "token=hidden".into(), + }), + error: Some(ExecError::Failed("password=private".into())), + }; + let literal = json!(CommandReport::new("c1", &snapshot, false)); + assert_eq!( + literal, + json!({ + "command_id": "c1", "state": "exited", "stdout": "password=secret�", + "stderr": "token=hidden", "exit_status": 7, "total_bytes": 100, + "truncated": true, "error": "could not start the program: password=[REDACTED]", + }) + ); + let redacted = json!(CommandReport::new("c1", &snapshot, true)); + assert_eq!(redacted["stdout"], "password=[REDACTED]"); + assert_eq!(redacted["stderr"], "token=[REDACTED]"); + assert_eq!(redacted["total_bytes"], 100); + assert_eq!(redacted["truncated"], true); +} + +#[test] +fn terminal_wire_keeps_cursor_rules_and_observation_never_invents_an_exit() { + let snapshot = || TerminalReport::new("界".into(), 5, 7, true, false); + let expected = json!({ + "text": "界", "first_line": 5, "next_line": 7, + "cursor_semantics": "inclusive snapshot; replace overlapping lines", + "truncated": true, "alt_screen": false, + }); + assert_eq!(json!(snapshot()), expected); + for reason in ["prompt_returned", "output_idle", "timeout"] { + let mut observation = expected.clone(); + observation["completion"] = json!(reason); + observation["completed"] = json!(reason == "prompt_returned"); + observation["exit_status"] = json!(null); + assert_eq!(json!(snapshot().observed(reason.into())), observation); + observation["future_field"] = json!("kept by display fallback"); + let decoded: ObservationReport = serde_json::from_value(observation).unwrap(); + assert_eq!(decoded.terminal.next_line, 7); + assert_eq!(decoded.completed, reason == "prompt_returned"); + assert!(decoded.exit_status.is_none()); + } +} + +#[test] +fn unfinished_commands_report_unknown_exit_and_do_not_mark_exact_output_truncated() { + assert_eq!( + json!(CommandReport::new("c2", &CommandSnapshot::default(), false)), + json!({ + "command_id": "c2", "state": "starting", "stdout": "", "stderr": "", + "exit_status": null, "total_bytes": 0, "truncated": false, "error": null, + }) + ); +} diff --git a/crates/ai/src/tools/transfer.rs b/crates/ai/src/tools/transfer.rs new file mode 100644 index 00000000..f5605431 --- /dev/null +++ b/crates/ai/src/tools/transfer.rs @@ -0,0 +1,160 @@ +//! File transfer tools: arguments, bounds and the agent-facing transfer report. +//! +//! Absolute-path rules are endpoint specific and belong to the transfer request +//! (`nocterm_transfers::Request::between`); this module only bounds the shape. +use super::TerminalCallError; +use nocterm_transfers::{Progress, TransferDirection, TransferState}; +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; +use serde_json::{Value, json}; + +pub const MAX_TRANSFER_SOURCES: usize = 64; +pub const MAX_TRANSFER_PATH_BYTES: usize = 4096; +pub const MAX_TRANSFER_SOURCE_BYTES: usize = 64 * 1024; +pub const MAX_TRANSFER_FAILURES: usize = 16; +const MAX_ID_BYTES: usize = 128; +const MAX_YIELD_MS: u64 = 10_000; + +/// Copy files and folders between the hosts of two attached terminals. +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct TransferFiles { + /// terminal_id whose host holds the sources; a local terminal means this computer. + pub source_terminal_id: String, + /// Absolute paths of files or folders on the source host; folders copy recursively. + pub sources: Vec, + /// terminal_id whose host receives the copies. + pub destination_terminal_id: String, + /// Absolute path of an existing folder on the destination host. + pub destination: String, + /// Replace files that already exist at the destination; by default they are skipped. + #[serde(default)] + pub overwrite: bool, + pub yield_ms: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct ReadTransfer { + pub transfer_id: String, + pub yield_ms: Option, +} + +#[derive(Clone, Debug, Deserialize, Serialize, JsonSchema)] +#[serde(deny_unknown_fields)] +pub struct CancelTransfer { + pub transfer_id: String, +} + +fn id_len_ok(id: &str) -> bool { + !id.is_empty() && id.len() <= MAX_ID_BYTES +} + +/// Control characters (NUL, newlines, escapes) could reshape the approval card text. +fn path_ok(path: &str) -> bool { + !path.is_empty() && path.len() <= MAX_TRANSFER_PATH_BYTES && !path.chars().any(char::is_control) +} + +impl TransferFiles { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + if !id_len_ok(&self.source_terminal_id) || !id_len_ok(&self.destination_terminal_id) { + return Err(TerminalCallError::InvalidTarget); + } + let total: usize = self.sources.iter().map(String::len).sum(); + if self.sources.is_empty() + || self.sources.len() > MAX_TRANSFER_SOURCES + || !self.sources.iter().all(|path| path_ok(path)) + || !path_ok(&self.destination) + || total > MAX_TRANSFER_SOURCE_BYTES + || self.yield_ms.is_some_and(|n| n > MAX_YIELD_MS) + { + return Err(TerminalCallError::TransferRejected); + } + Ok(()) + } +} + +impl ReadTransfer { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + if id_len_ok(&self.transfer_id) && self.yield_ms.is_none_or(|n| n <= MAX_YIELD_MS) { + Ok(()) + } else { + Err(TerminalCallError::ReadTransferRejected) + } + } +} + +impl CancelTransfer { + pub(super) fn validate(&self) -> Result<(), TerminalCallError> { + if id_len_ok(&self.transfer_id) { + Ok(()) + } else { + Err(TerminalCallError::InvalidTransferId) + } + } +} + +fn state_name(state: TransferState) -> &'static str { + match state { + TransferState::Queued => "queued", + TransferState::Discovering => "discovering", + TransferState::Running => "running", + TransferState::Completed => "completed", + TransferState::Failed => "failed", + TransferState::Cancelled => "cancelled", + } +} + +// Seam: `TransferDirection::Relay` arrives with the transfers domain contract; the +// fallthrough already names it, so this function needs no change when it lands. +fn direction_name(direction: TransferDirection) -> &'static str { + if direction == TransferDirection::Upload { + "upload" + } else if direction == TransferDirection::Download { + "download" + } else { + "relay" + } +} + +/// Agent-facing JSON for one transfer; paths and error text are redacted on request. +pub fn report(progress: &Progress, redact: bool) -> Value { + let text = |text: &str| { + if redact { + crate::redact::redact(text) + } else { + text.to_owned() + } + }; + let failures: Vec = progress + .errors + .iter() + .take(MAX_TRANSFER_FAILURES) + .map(|issue| { + json!({ + "path": issue.path().map(|path| text(&path.to_string())), + "operation": issue.operation().to_string(), + "error": text(&issue.cause().to_string()), + }) + }) + .collect(); + json!({ + "transfer_id": progress.id.to_string(), + "state": state_name(progress.state), + "direction": direction_name(progress.direction), + "destination": text(&progress.destination), + "files": { + "discovered": progress.discovered_files, + "completed": progress.completed_files, + "skipped": progress.skipped_files, + "failed": progress.failed_files, + }, + "bytes": {"total": progress.total_bytes, "transferred": progress.sent_bytes}, + "discovery_complete": progress.discovery_complete, + "failures": failures, + "failures_truncated": progress.errors.len() > MAX_TRANSFER_FAILURES, + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/ai/src/tools/transfer/tests.rs b/crates/ai/src/tools/transfer/tests.rs new file mode 100644 index 00000000..16303fd5 --- /dev/null +++ b/crates/ai/src/tools/transfer/tests.rs @@ -0,0 +1,239 @@ +use super::*; +use crate::TerminalCall; +use nocterm_session::{HostAddress, Target}; +use nocterm_transfers::{ + TransferCause, TransferId, TransferIssue, TransferOperation, TransferPath, +}; + +fn transfer() -> TransferFiles { + TransferFiles { + source_terminal_id: "t1".into(), + sources: vec!["/srv/a".into(), "/srv/dir b".into()], + destination_terminal_id: "t2".into(), + destination: "/home/alice/in".into(), + overwrite: false, + yield_ms: Some(1000), + } +} + +fn rejection(change: impl FnOnce(&mut TransferFiles)) -> TerminalCallError { + let mut call = transfer(); + change(&mut call); + TerminalCall::TransferFiles(call).validate().unwrap_err() +} + +fn progress(errors: Vec) -> Progress { + Progress { + id: TransferId::from_bytes([0xab; 16]).unwrap(), + host: HostAddress::direct(Target { + host: "example.test".into(), + port: 22, + user: "alice".into(), + }), + destination: "/srv/in".into(), + direction: TransferDirection::Download, + state: TransferState::Running, + discovered_files: 12, + completed_files: 10, + prepared_directories: 2, + skipped_files: 1, + failed_files: 1, + total_bytes: 1_048_576, + sent_bytes: 524_288, + discovery_complete: true, + errors, + } +} + +fn issue(path: &str, cause: TransferCause) -> TransferIssue { + TransferIssue::new( + Some(TransferPath::Remote(path.into())), + TransferOperation::Write, + cause, + ) +} + +#[test] +fn a_well_formed_transfer_validates_and_writes_on_both_terminals() { + let call = TerminalCall::TransferFiles(transfer()); + assert!(call.validate().is_ok()); + assert!(call.writes()); + assert_eq!(call.capability(), crate::ToolCapability::Transfer); + assert_eq!(call.terminal_id(), Some("t2")); + assert_eq!(call.source_terminal_id(), Some("t1")); + assert_eq!(call.terminals().collect::>(), ["t2", "t1"]); +} + +#[test] +fn terminals_lists_a_shared_source_and_destination_once() { + let mut same = transfer(); + same.source_terminal_id = "t2".into(); + let call = TerminalCall::TransferFiles(same); + assert_eq!(call.terminals().collect::>(), ["t2"]); +} + +#[test] +fn read_and_cancel_have_no_terminal_and_differ_in_writing() { + let read = TerminalCall::ReadTransfer(ReadTransfer { + transfer_id: "x".into(), + yield_ms: None, + }); + let cancel = TerminalCall::CancelTransfer(CancelTransfer { + transfer_id: "x".into(), + }); + for call in [&read, &cancel] { + assert_eq!(call.terminals().count(), 0); + assert!(call.target().is_none()); + } + assert!(!read.writes()); + assert_eq!(read.capability(), crate::ToolCapability::Read); + assert!(cancel.writes()); + assert_eq!(cancel.capability(), crate::ToolCapability::Transfer); +} + +#[test] +fn transfer_terminal_ids_must_be_one_to_128_bytes() { + for id in ["", &"t".repeat(129)] { + assert_eq!( + rejection(|call| call.source_terminal_id = id.into()), + TerminalCallError::InvalidTarget + ); + assert_eq!( + rejection(|call| call.destination_terminal_id = id.into()), + TerminalCallError::InvalidTarget + ); + } +} + +#[test] +fn transfer_sources_and_destination_are_bounded() { + let rejected = TerminalCallError::TransferRejected; + assert_eq!(rejection(|call| call.sources.clear()), rejected); + assert_eq!( + rejection(|call| call.sources = vec!["/a".into(); MAX_TRANSFER_SOURCES + 1]), + rejected + ); + assert_eq!( + rejection(|call| call.sources = vec![String::new()]), + rejected + ); + assert_eq!( + rejection(|call| call.sources = vec!["/a\0".into()]), + rejected + ); + assert_eq!( + rejection(|call| call.sources = vec!["/".repeat(MAX_TRANSFER_PATH_BYTES + 1)]), + rejected + ); + let long = "/".repeat(MAX_TRANSFER_PATH_BYTES); + assert_eq!( + rejection(|call| call.sources = vec![long.clone(); 17]), + rejected + ); + assert_eq!(rejection(|call| call.destination.clear()), rejected); + assert_eq!(rejection(|call| call.destination = "/\0".into()), rejected); + assert_eq!(rejection(|call| call.yield_ms = Some(10_001)), rejected); + let mut edge = transfer(); + edge.sources = vec!["/a".into(); MAX_TRANSFER_SOURCES]; + edge.yield_ms = Some(10_000); + assert!(TerminalCall::TransferFiles(edge).validate().is_ok()); +} + +#[test] +fn control_characters_cannot_reshape_a_transfer_path() { + let rejected = TerminalCallError::TransferRejected; + for text in [ + "/a\nInto: /tmp", + "/a\rb", + "/a\u{1b}[2J", + "/a\tb", + "/a\u{7f}", + ] { + assert_eq!( + rejection(|call| call.sources = vec![text.into()]), + rejected, + "{text:?}" + ); + assert_eq!( + rejection(|call| call.destination = text.into()), + rejected, + "{text:?}" + ); + } +} + +#[test] +fn read_and_cancel_validation_name_their_own_rejection() { + let read = |transfer_id: &str, yield_ms| { + TerminalCall::ReadTransfer(ReadTransfer { + transfer_id: transfer_id.into(), + yield_ms, + }) + .validate() + }; + assert!(read("abc", Some(10_000)).is_ok()); + for result in [ + read("", None), + read("a", Some(10_001)), + read(&"a".repeat(129), None), + ] { + assert_eq!(result, Err(TerminalCallError::ReadTransferRejected)); + } + let cancel = |transfer_id: &str| { + TerminalCall::CancelTransfer(CancelTransfer { + transfer_id: transfer_id.into(), + }) + .validate() + }; + assert!(cancel("abc").is_ok()); + assert_eq!(cancel(""), Err(TerminalCallError::InvalidTransferId)); + assert_eq!( + cancel(&"a".repeat(129)), + Err(TerminalCallError::InvalidTransferId) + ); +} + +#[test] +fn report_describes_progress_for_the_agent() { + let value = report( + &progress(vec![issue("/srv/a/x", TransferCause::Cancelled)]), + false, + ); + assert_eq!(value["transfer_id"], "abababab-abab-abab-abab-abababababab"); + assert_eq!(value["state"], "running"); + assert_eq!(value["direction"], "download"); + assert_eq!(value["destination"], "/srv/in"); + assert_eq!(value["files"]["completed"], 10); + assert_eq!(value["files"]["skipped"], 1); + assert_eq!(value["bytes"]["transferred"], 524_288); + assert_eq!(value["discovery_complete"], true); + assert_eq!(value["failures"][0]["path"], "/srv/a/x"); + assert_eq!(value["failures"][0]["operation"], "write"); + assert_eq!(value["failures"][0]["error"], "Transfer cancelled."); + assert_eq!(value["failures_truncated"], false); +} + +#[test] +fn report_keeps_the_first_sixteen_failures_and_says_so() { + let issues = (0..MAX_TRANSFER_FAILURES + 4) + .map(|n| issue(&format!("/srv/{n}"), TransferCause::Cancelled)) + .collect(); + let value = report(&progress(issues), false); + let failures = value["failures"].as_array().unwrap(); + assert_eq!(failures.len(), MAX_TRANSFER_FAILURES); + assert_eq!(failures[0]["path"], "/srv/0"); + assert_eq!(value["failures_truncated"], true); +} + +#[test] +fn report_redacts_paths_and_error_text_only_on_request() { + let secret = "/srv/ghp_abcdefghijklmnopqrst/key"; + let mut input = progress(vec![issue(secret, TransferCause::Cancelled)]); + input.destination = secret.into(); + let plain = report(&input, false); + assert_eq!(plain["destination"], secret); + assert_eq!(plain["failures"][0]["path"], secret); + let redacted = report(&input, true).to_string(); + assert!(!redacted.contains("ghp_abcdefghijklmnopqrst")); + assert!(redacted.contains("[REDACTED TOKEN]")); +} diff --git a/crates/ai/src/usage.rs b/crates/ai/src/usage.rs index d2bf6b25..20615191 100644 --- a/crates/ai/src/usage.rs +++ b/crates/ai/src/usage.rs @@ -3,13 +3,10 @@ //! //! ACP reports the context window (`usage_update`) and, at the end of a turn, //! the session's token totals. Plan limits are not part of ACP: an agent's -//! `UsageProfile` says whether it attaches them to `usage_update` metadata and -//! which injected account source reads the full report. +//! `UsageProfile` says whether it attaches them to `usage_update` metadata. +//! Nocterm uses only the reports received from the agent. use crate::model::content::Content; -mod source; -pub use source::{AccountUsageSource, UsageAgent, UsageReadError, UsageSources}; - use crate::{model::usage::ContextUsage, thread::Entry}; /// The span a plan limit counts usage over. @@ -86,14 +83,6 @@ impl Limits { } } } - - /// Replaces every limit: the source reports all of them at once. - pub fn replace(&mut self, limits: Vec) { - self.0.clear(); - for limit in limits { - self.merge(limit); - } - } } /// The plan limit an agent attached to `update` as a rate-limit record under @@ -217,50 +206,6 @@ mod tests { assert!(record(&update(serde_json::json!({"other": 1}))).is_none()); } - #[test] - fn usage_sources_read_each_agent_from_the_source_for_its_account() { - use crate::agents::AccountUsage; - use futures::future::BoxFuture; - use std::sync::Arc; - struct Fixed(Result>, UsageReadError>); - impl AccountUsageSource for Fixed { - fn read( - &self, - _: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - let result = self.0.clone(); - Box::pin(async move { result }) - } - } - let report = vec![RateLimit { - window: Window::FiveHour, - used: Some(0.1), - resets_at: None, - reached: false, - }]; - let failure = - || UsageReadError::fetch("https://example.test", std::io::Error::other("down")); - let sources = UsageSources::default() - .with(AccountUsage::OAuthAccount, Arc::new(Fixed(Err(failure())))) - .with( - AccountUsage::OAuthAccount, - Arc::new(Fixed(Ok(Some(report.clone())))), - ) - .with(AccountUsage::SessionLog, Arc::new(Fixed(Err(failure())))); - let read = |account| { - futures::executor::block_on(sources.read(UsageAgent { - id: "agent".into(), - account, - })) - }; - assert_eq!(read(AccountUsage::OAuthAccount).unwrap(), Some(report)); - assert!(matches!( - read(AccountUsage::SessionLog), - Err(UsageReadError::Fetch { .. }) - )); - assert_eq!(read(AccountUsage::None).unwrap(), None); - } - #[test] fn limits_merge_by_window_in_order() { let limit = |window, used| RateLimit { diff --git a/crates/ai/src/usage/source.rs b/crates/ai/src/usage/source.rs deleted file mode 100644 index 0e57032a..00000000 --- a/crates/ai/src/usage/source.rs +++ /dev/null @@ -1,121 +0,0 @@ -//! Account usage capabilities, without native paths or provider log formats. -use super::RateLimit; -use crate::agents::AccountUsage; -use futures::future::BoxFuture; -use nocterm_core::{SharedCause, SharedError}; -use std::path::PathBuf; -use std::sync::Arc; - -/// The agent whose limits to read and how its account reports them. -#[derive(Clone, Debug, PartialEq, Eq)] -pub struct UsageAgent { - pub id: String, - pub account: AccountUsage, -} - -impl UsageAgent { - /// An agent without a profile reports limits with usage updates only. - pub fn new(agent: &crate::agents::AvailableAgent) -> Self { - Self { - id: agent.id().to_owned(), - account: agent - .profile() - .map_or(AccountUsage::None, |profile| profile.usage().account()), - } - } -} - -/// Physical account-source failures retain their native cause. -#[derive(Clone, Debug, thiserror::Error)] -pub enum UsageReadError { - #[error("Could not enumerate account usage at {path}: {source}")] - Enumerate { - path: PathBuf, - #[source] - source: SharedError, - }, - #[error("Could not read account usage at {path}: {source}")] - Read { - path: PathBuf, - #[source] - source: SharedError, - }, - #[error("Could not seek account usage at {path}: {source}")] - Seek { - path: PathBuf, - #[source] - source: SharedError, - }, - #[error("Could not fetch account usage from {url}: {source}")] - Fetch { - url: String, - #[source] - source: SharedCause, - }, -} - -impl UsageReadError { - pub fn enumerate(path: impl Into, source: std::io::Error) -> Self { - Self::Enumerate { - path: path.into(), - source: SharedError::from_error(source), - } - } - pub fn read(path: impl Into, source: std::io::Error) -> Self { - Self::Read { - path: path.into(), - source: SharedError::from_error(source), - } - } - pub fn seek(path: impl Into, source: std::io::Error) -> Self { - Self::Seek { - path: path.into(), - source: SharedError::from_error(source), - } - } - pub fn fetch( - url: impl Into, - source: impl std::error::Error + Send + Sync + 'static, - ) -> Self { - Self::Fetch { - url: url.into(), - source: SharedCause::new(source), - } - } -} - -/// Implementations read only when polled on the host's background executor. -/// Missing or unsupported sources and logs without a usable report return None. -pub trait AccountUsageSource: Send + Sync { - fn read( - &self, - agent: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>>; -} - -/// One account source per way of reporting limits; agents whose way has no -/// source report nothing. -#[derive(Default)] -pub struct UsageSources(Vec<(AccountUsage, Arc)>); - -impl UsageSources { - /// Reads `account` agents' limits from `source`, replacing an earlier one. - #[must_use] - pub fn with(mut self, account: AccountUsage, source: Arc) -> Self { - self.0.retain(|(kind, _)| *kind != account); - self.0.push((account, source)); - self - } -} - -impl AccountUsageSource for UsageSources { - fn read( - &self, - agent: UsageAgent, - ) -> BoxFuture<'static, Result>, UsageReadError>> { - match self.0.iter().find(|(kind, _)| *kind == agent.account) { - Some((_, source)) => source.read(agent), - None => Box::pin(async { Ok(None) }), - } - } -} diff --git a/crates/ai/tests/execution_origin.rs b/crates/ai/tests/execution_origin.rs new file mode 100644 index 00000000..b540796e --- /dev/null +++ b/crates/ai/tests/execution_origin.rs @@ -0,0 +1,57 @@ +//! Captured conversation membership never issues native execution authority. +use nocterm_ai::{ + execution_origin::{ChatKey, ExecutionOrigin, ExecutionOriginError}, + history::ContextId, +}; +use nocterm_project_model::ProjectId; +use std::{ + collections::HashMap, + path::{Path, PathBuf}, +}; +const ID: &str = "00000000-0000-4000-8000-000000000001"; +fn project() -> ContextId { + ContextId::Project(ProjectId::from_bytes([1; 16])) +} +#[test] +fn equal_chat_ids_in_different_contexts_are_different_document_keys() { + let general = ChatKey::new(ContextId::General, ID.into()); + let project = ChatKey::new(project(), ID.into()); + assert_ne!(general, project); + let documents = HashMap::from([(general.clone(), "general"), (project.clone(), "project")]); + assert_eq!(documents.len(), 2); + assert_eq!(documents[&general], "general"); + assert_eq!(documents[&project], "project"); + assert_eq!(project.id(), ID); + assert_eq!(project.scope(), self::project()); +} +#[test] +fn an_origin_retains_its_original_context_and_local_provider_resume_candidate() { + let key = ChatKey::new(project(), ID.into()); + let mut supplied = PathBuf::from("/private/provider/session"); + let origin = ExecutionOrigin::new(key.clone(), Some(supplied.clone())); + supplied.push("other"); + assert_eq!(origin.key(), &key); + assert_eq!(origin.scope(), project()); + assert_eq!( + origin.provider_resume(), + Some(Path::new("/private/provider/session")) + ); + let neighbor = ExecutionOrigin::new( + ChatKey::new(project(), "00000000-0000-4000-8000-000000000002".into()), + origin.provider_resume().map(Path::to_owned), + ); + assert_ne!(neighbor.key(), origin.key()); +} +#[test] +fn a_consumer_cannot_reassign_an_existing_origin_to_its_own_context() { + let origin = ExecutionOrigin::new(ChatKey::new(project(), ID.into()), None); + assert_eq!( + origin.require_scope(ContextId::General), + Err(ExecutionOriginError::ScopeMismatch { + expected: ContextId::General, + actual: project() + }) + ); + assert!(origin.require_scope(project()).is_ok()); + assert_eq!(origin.scope(), project()); +} diff --git a/crates/ai/tests/history_residency.rs b/crates/ai/tests/history_residency.rs new file mode 100644 index 00000000..efc9c4d0 --- /dev/null +++ b/crates/ai/tests/history_residency.rs @@ -0,0 +1,31 @@ +//! Retained immutable writer snapshots include their separate shared queue payloads. +use nocterm_ai::history::{SavedChat, SavedPrompt, SharedChat}; +use std::sync::Arc; + +#[test] +fn retained_snapshot_memory_counts_transcript_and_shared_queue_without_encoding() { + let mut metadata = SavedChat::for_identity( + "codex".into(), + "00000000-0000-0000-0000-000000000001".into(), + 1, + ); + metadata + .entries + .push(nocterm_ai::thread::Entry::Agent("a".repeat(1024 * 1024))); + let body = SharedChat { + metadata, + queue: Vec::new(), + }; + let body_bytes = body.retained_bytes().unwrap(); + assert!(body_bytes >= 1024 * 1024); + let shared = SharedChat { + metadata: body.metadata, + queue: vec![Arc::new(SavedPrompt { + id: 1, + text: "q".repeat(1024 * 1024), + images: Vec::new(), + attachments: Vec::new(), + })], + }; + assert!(shared.retained_bytes().unwrap() >= body_bytes + 1024 * 1024); +} diff --git a/crates/connections-io/Cargo.toml b/crates/connections-io/Cargo.toml index 9cca3850..82d1f991 100644 --- a/crates/connections-io/Cargo.toml +++ b/crates/connections-io/Cargo.toml @@ -17,6 +17,7 @@ uuid.workspace = true reqwest.workspace = true serde.workspace = true serde_json.workspace = true +thiserror.workspace = true tracing.workspace = true [dev-dependencies] diff --git a/crates/connections-io/src/flags.rs b/crates/connections-io/src/flags.rs index 1bae66ac..62b8a3ec 100644 --- a/crates/connections-io/src/flags.rs +++ b/crates/connections-io/src/flags.rs @@ -1,17 +1,19 @@ //! A downloaded flag is usable even when atomic cache publication fails. -use crate::http::{self, MAX_FLAG_BYTES}; +use crate::http::{HttpClient, MAX_FLAG_BYTES}; use nocterm_connections_model::observation::{CountryCode, FlagRepository, ObservationError}; -use std::{io::Read as _, path::PathBuf}; +use std::{io::Read as _, path::PathBuf, sync::Arc}; pub(crate) struct CachedFlags { directory: PathBuf, endpoint: Option, + client: Arc, } impl CachedFlags { - pub(crate) fn new(directory: PathBuf) -> Self { + pub(crate) fn new(directory: PathBuf, client: Arc) -> Self { Self { directory, endpoint: Some("https://flagcdn.com/".into()), + client, } } #[cfg(test)] @@ -19,6 +21,7 @@ impl CachedFlags { Self { directory, endpoint, + client: Arc::default(), } } } @@ -40,7 +43,9 @@ impl FlagRepository for CachedFlags { let Some(endpoint) = &self.endpoint else { return Ok(None); }; - let bytes = http::get(&format!("{endpoint}{}.svg", code.as_str()), MAX_FLAG_BYTES)?; + let bytes = self + .client + .get(&format!("{endpoint}{}.svg", code.as_str()), MAX_FLAG_BYTES)?; if !is_svg(&bytes) { return Err(ObservationError::Decode(Box::new(std::io::Error::new( std::io::ErrorKind::InvalidData, diff --git a/crates/connections-io/src/http.rs b/crates/connections-io/src/http.rs index 7870bc5f..95014647 100644 --- a/crates/connections-io/src/http.rs +++ b/crates/connections-io/src/http.rs @@ -2,7 +2,12 @@ use nocterm_connections_model::observation::{ CountryCode, CountryLocator, ObservationError, PublicAddress, }; -use std::{io::Read as _, time::Duration}; +use nocterm_core::SharedError; +use std::{ + io::Read as _, + sync::{Arc, OnceLock}, + time::Duration, +}; pub(crate) const MAX_GEOIP_BYTES: u64 = 4 * 1024; pub(crate) use nocterm_connections_model::observation::MAX_FLAG_BYTES; @@ -10,18 +15,29 @@ const TIMEOUT: Duration = Duration::from_secs(8); pub(crate) struct HttpCountry { endpoint: String, + client: Arc, } impl Default for HttpCountry { fn default() -> Self { Self { endpoint: "https://api.country.is/".into(), + client: Arc::default(), } } } impl HttpCountry { + pub(crate) fn with_client(client: Arc) -> Self { + Self { + client, + ..Self::default() + } + } #[cfg(test)] pub(crate) fn at(endpoint: String) -> Self { - Self { endpoint } + Self { + endpoint, + client: Arc::default(), + } } } impl CountryLocator for HttpCountry { @@ -30,7 +46,7 @@ impl CountryLocator for HttpCountry { struct Answer { country: Option, } - let body = get( + let body = self.client.get( &format!("{}{ip}", self.endpoint, ip = ip.as_ip()), MAX_GEOIP_BYTES, )?; @@ -46,31 +62,45 @@ impl CountryLocator for HttpCountry { } } -pub(crate) fn get(url: &str, limit: u64) -> Result, ObservationError> { - let client = reqwest::blocking::Client::builder() - .timeout(TIMEOUT) - .user_agent(concat!("nocterm/", env!("CARGO_PKG_VERSION"))) - .build() - .map_err(|source| ObservationError::Network(Box::new(source.without_url())))?; - let response = client - .get(url) - .send() - .map_err(|source| ObservationError::Network(Box::new(source.without_url())))?; - let response = response - .error_for_status() - .map_err(|source| ObservationError::Status { - status: source.status().map_or(0, |status| status.as_u16()), - source: Box::new(source.without_url()), - })?; - let mut body = Vec::new(); - response - .take(limit + 1) - .read_to_end(&mut body) - .map_err(|source| ObservationError::Body(Box::new(source)))?; - if body.len() as u64 > limit { - return Err(ObservationError::BodyLimit { limit }); +/// One installation shares connection pooling; construction occurs on first background use. +#[derive(Default)] +pub(crate) struct HttpClient { + client: OnceLock>>, +} + +impl HttpClient { + pub(crate) fn get(&self, url: &str, limit: u64) -> Result, ObservationError> { + let client = self + .client + .get_or_init(|| { + reqwest::blocking::Client::builder() + .timeout(TIMEOUT) + .user_agent(concat!("nocterm/", env!("CARGO_PKG_VERSION"))) + .build() + .map_err(|source| SharedError::from_error(source.without_url())) + }) + .as_ref() + .map_err(|source| ObservationError::Network(Box::new(source.clone())))?; + let response = client + .get(url) + .send() + .map_err(|source| ObservationError::Network(Box::new(source.without_url())))?; + let response = response + .error_for_status() + .map_err(|source| ObservationError::Status { + status: source.status().map_or(0, |status| status.as_u16()), + source: Box::new(source.without_url()), + })?; + let mut body = Vec::new(); + response + .take(limit + 1) + .read_to_end(&mut body) + .map_err(|source| ObservationError::Body(Box::new(source)))?; + if body.len() as u64 > limit { + return Err(ObservationError::BodyLimit { limit }); + } + Ok(body) } - Ok(body) } #[cfg(test)] diff --git a/crates/connections-io/src/http/tests.rs b/crates/connections-io/src/http/tests.rs index 022744b6..0d0905a6 100644 --- a/crates/connections-io/src/http/tests.rs +++ b/crates/connections-io/src/http/tests.rs @@ -27,7 +27,9 @@ fn country_requests_use_only_ip_and_decode_checked_optional_codes() { fn status_error_retains_native_cause_without_endpoint() { use std::error::Error as _; let (endpoint, worker) = serve(503, vec![]); - let result = get(&endpoint, MAX_GEOIP_BYTES).unwrap_err(); + let result = HttpClient::default() + .get(&endpoint, MAX_GEOIP_BYTES) + .unwrap_err(); worker.join().unwrap(); assert!(matches!( result, diff --git a/crates/connections-io/src/lib.rs b/crates/connections-io/src/lib.rs index df5b96ce..575db8ab 100644 --- a/crates/connections-io/src/lib.rs +++ b/crates/connections-io/src/lib.rs @@ -1,12 +1,15 @@ //! Native connection files, environment/entropy/clock and network observations. //! -//! Owns: three-document repositories, native defaults/UUID/clock, DNS, bounded remote OS reads and HTTP/flag cache. +//! Owns: three-document repositories, native defaults/UUID/clock, a finite native DNS worker budget, bounded remote OS reads and HTTP/flag cache. //! Does not own: profile rules, GPUI images, observation scheduling or persistence workers. //! Ports used: foundation persistence. Ports provided: ConnectionRepository, ProfileIds, //! ConnectionClock, PublicAddressResolver, CountryLocator, //! FlagRepository, RemoteOsDetector. Settings section: none. Globals: none. //! Invariants: country services receive public IPs only; bodies and cached flags -//! are bounded; failure to cache never discards an acquired flag. Blocking document +//! are bounded; failure to cache never discards an acquired flag. DNS admits four workers +//! and four pending queries with an eight-second caller deadline; uncancellable OS work +//! retains its physical worker until it ends. One lazy HTTP client is shared per installation. +//! Blocking document //! acquisition and writes run off foreground; profiles retain configuration publication semantics. mod os_probe; @@ -35,10 +38,11 @@ pub struct NativeObservations { impl NativeObservations { pub fn new(flags_dir: PathBuf) -> Self { + let client = Arc::new(http::HttpClient::default()); Self { - resolver: Arc::new(NativeResolver), - locator: Arc::new(http::HttpCountry::default()), - flags: Arc::new(flags::CachedFlags::new(flags_dir)), + resolver: Arc::new(NativeResolver::default()), + locator: Arc::new(http::HttpCountry::with_client(client.clone())), + flags: Arc::new(flags::CachedFlags::new(flags_dir, client)), } } diff --git a/crates/connections-io/src/os_probe.rs b/crates/connections-io/src/os_probe.rs index 86af6b6c..cacd6799 100644 --- a/crates/connections-io/src/os_probe.rs +++ b/crates/connections-io/src/os_probe.rs @@ -3,7 +3,10 @@ use nocterm_connections_model::{ os::{self, OsId}, os_observation::{OsFuture, OsObservationError, RemoteOsDetector}, }; -use nocterm_session::{EntryKind, FsError, RemoteFs, fs::RemoteDownload}; +use nocterm_session::{ + EntryKind, FsError, RemoteFs, + fs::{DownloadReadError, read_bounded}, +}; use std::sync::Arc; const MAX_OS_RELEASE_BYTES: usize = 16 * 1024; pub struct NativeOsDetector; @@ -93,14 +96,34 @@ async fn read_small(fs: &dyn RemoteFs, path: &str) -> Result, OsO }); } }; - let result = read_bytes(file.as_mut(), path).await.and_then(|bytes| { - String::from_utf8(bytes) - .map(Some) - .map_err(|source| OsObservationError::InvalidUtf8 { + let result = read_bounded(file.as_mut(), MAX_OS_RELEASE_BYTES, 4096) + .await + .map_err(|error| match error { + DownloadReadError::Read(source) => OsObservationError::Read { path: path.into(), source, - }) - }); + }, + DownloadReadError::TooLarge { limit } => OsObservationError::Oversize { + path: path.into(), + limit, + }, + DownloadReadError::InvalidChunk { + requested, + returned, + } => OsObservationError::InvalidChunk { + path: path.into(), + requested, + returned, + }, + }) + .and_then(|bytes| { + String::from_utf8(bytes) + .map(Some) + .map_err(|source| OsObservationError::InvalidUtf8 { + path: path.into(), + source, + }) + }); if let Err(source) = file.close().await { let error = OsObservationError::Close { path: path.into(), @@ -110,38 +133,5 @@ async fn read_small(fs: &dyn RemoteFs, path: &str) -> Result, OsO } result } -async fn read_bytes( - file: &mut dyn RemoteDownload, - path: &str, -) -> Result, OsObservationError> { - let mut bytes = Vec::new(); - loop { - let requested = (MAX_OS_RELEASE_BYTES + 1 - bytes.len()).min(4096); - let chunk = file - .read(requested) - .await - .map_err(|source| OsObservationError::Read { - path: path.into(), - source, - })?; - if chunk.len() > requested { - return Err(OsObservationError::InvalidChunk { - path: path.into(), - requested, - returned: chunk.len(), - }); - } - if chunk.is_empty() { - return Ok(bytes); - } - if bytes.len() + chunk.len() > MAX_OS_RELEASE_BYTES { - return Err(OsObservationError::Oversize { - path: path.into(), - limit: MAX_OS_RELEASE_BYTES, - }); - } - bytes.extend(chunk); - } -} #[cfg(test)] mod tests; diff --git a/crates/connections-io/src/repository.rs b/crates/connections-io/src/repository.rs index 3997a368..95103f89 100644 --- a/crates/connections-io/src/repository.rs +++ b/crates/connections-io/src/repository.rs @@ -11,14 +11,14 @@ use nocterm_connections_model::{ use nocterm_core::persist; use std::{path::PathBuf, sync::Arc}; pub struct FileConnectionRepository { - profiles: PathBuf, + profiles: persist::RevisionedFile, recents: PathBuf, facts: PathBuf, } impl FileConnectionRepository { pub fn new(profiles: PathBuf, recents: PathBuf, facts: PathBuf) -> Self { Self { - profiles, + profiles: persist::RevisionedFile::new(profiles), recents, facts, } @@ -26,7 +26,7 @@ impl FileConnectionRepository { } impl ConnectionRepository for FileConnectionRepository { fn load(&self) -> LoadedConnections { - let (profiles, read_only) = match persist::load::(&self.profiles) { + let (profiles, read_only) = match self.profiles.load::() { Ok(value) => (value.unwrap_or_default(), None), Err(error) => { tracing::error!(%error, "could not read saved connections"); @@ -60,7 +60,7 @@ impl ConnectionRepository for FileConnectionRepository { } } fn save_profiles(&self, profiles: &Profiles) -> Result<(), ConnectionStorageError> { - persist::save_preserving(&self.profiles, profiles).map_err(|error| { + self.profiles.save_preserving(profiles).map_err(|error| { ConnectionStorageError::new( ConnectionDocument::Profiles, StorageOperation::Write, diff --git a/crates/connections-io/src/repository/tests.rs b/crates/connections-io/src/repository/tests.rs index 06af1eb3..40e0088d 100644 --- a/crates/connections-io/src/repository/tests.rs +++ b/crates/connections-io/src/repository/tests.rs @@ -202,3 +202,29 @@ fn a_document_without_jump_hosts_keeps_its_bytes_after_a_save() { first ); } + +#[test] +fn a_parallel_profile_edit_is_preserved_and_the_stale_writer_reports_its_cause() { + let directory = tempfile::tempdir().unwrap(); + let first = repository(directory.path()); + let second = repository(directory.path()); + let mut a = first.load().profiles; + let mut b = second.load().profiles; + let mut changed = profile(); + changed.name = "Saved by first".into(); + a.upsert(changed.clone()); + first.save_profiles(&a).unwrap(); + b.upsert(profile()); + let error = second.save_profiles(&b).unwrap_err(); + assert!(matches!( + error + .source() + .unwrap() + .downcast_ref::(), + Some(persist::PersistError::Conflict { .. }) + )); + assert_eq!( + first.load().profiles.get(changed.id).unwrap().name, + changed.name + ); +} diff --git a/crates/connections-io/src/resolver.rs b/crates/connections-io/src/resolver.rs index 1acb13cd..77dd1047 100644 --- a/crates/connections-io/src/resolver.rs +++ b/crates/connections-io/src/resolver.rs @@ -2,17 +2,109 @@ use nocterm_connections_model::observation::{ ObservationError, PublicAddress, PublicAddressResolver, }; -use std::net::{IpAddr, ToSocketAddrs as _}; +use nocterm_core::SharedError; +use std::{ + io, + net::{IpAddr, ToSocketAddrs as _}, + sync::{ + Arc, Mutex, OnceLock, + mpsc::{self, SyncSender}, + }, + thread, + time::{Duration, Instant}, +}; + +const DNS_WORKERS: usize = 4; +const DNS_PENDING: usize = 4; +const DNS_DEADLINE: Duration = Duration::from_secs(8); + +struct Request { + host: String, + deadline: Instant, + answer: SyncSender>>, +} + +/// Optional country resolution has its own finite physical budget. An OS lookup +/// that ignores cancellation retains one of these workers, not a shared fact slot. +/// Dropping the last resolver closes idle workers; queued expired work is skipped. +pub struct NativeResolver { + requests: OnceLock, SharedError>>, + deadline: Duration, +} -pub struct NativeResolver; +impl Default for NativeResolver { + fn default() -> Self { + Self { + requests: OnceLock::new(), + deadline: DNS_DEADLINE, + } + } +} + +type Lookup = Arc io::Result> + Send + Sync>; +fn workers(lookup: Lookup) -> io::Result> { + let (sender, receiver) = mpsc::sync_channel::(DNS_PENDING); + let receiver = Arc::new(Mutex::new(receiver)); + for index in 0..DNS_WORKERS { + let receiver = receiver.clone(); + let lookup = lookup.clone(); + thread::Builder::new() + .name(format!("nocterm-dns-{index}")) + .spawn(move || { + loop { + let next = receiver + .lock() + .unwrap_or_else(|error| error.into_inner()) + .recv(); + let Ok(request) = next else { break }; + if Instant::now() >= request.deadline { + continue; + } + let resolved = lookup(&request.host); + let _ = request.answer.send(resolved); + } + })?; + } + Ok(sender) +} + +impl NativeResolver { + fn lookup(&self, host: &str) -> io::Result> { + let sender = self + .requests + .get_or_init(|| { + workers(Arc::new(|host| { + (host, 0) + .to_socket_addrs() + .map(|addresses| addresses.map(|address| address.ip()).collect()) + })) + .map_err(SharedError::from_error) + }) + .as_ref() + .map_err(|error| io::Error::other(error.clone()))?; + let (answer, result) = mpsc::sync_channel(1); + sender + .try_send(Request { + host: host.to_owned(), + deadline: Instant::now() + self.deadline, + answer, + }) + .map_err(|error| match error { + mpsc::TrySendError::Full(_) => io::ErrorKind::WouldBlock, + mpsc::TrySendError::Disconnected(_) => io::ErrorKind::BrokenPipe, + })?; + result + .recv_timeout(self.deadline) + .map_err(|error| match error { + mpsc::RecvTimeoutError::Timeout => io::ErrorKind::TimedOut, + mpsc::RecvTimeoutError::Disconnected => io::ErrorKind::BrokenPipe, + })? + } +} impl PublicAddressResolver for NativeResolver { fn resolve(&self, host: &str) -> Result, ObservationError> { - resolve_with(host, |host| { - (host, 0) - .to_socket_addrs() - .map(|addresses| addresses.map(|address| address.ip())) - }) + resolve_with(host, |host| self.lookup(host)) } } @@ -33,39 +125,4 @@ fn resolve_with>( } #[cfg(test)] -mod tests { - use super::*; - #[test] - fn literals_and_local_names_never_use_dns() { - for host in [ - "192.168.1.10", - "[::1]", - "pi.local", - "localhost", - "", - "8.8.8.8", - ] { - let result = - resolve_with::>(host, |_| panic!("unexpected DNS for {host}")).unwrap(); - assert_eq!(result.is_some(), host == "8.8.8.8"); - } - } - #[test] - fn resolution_selects_first_public_address_only() { - let found = resolve_with("example.com", |_| { - Ok(vec![ - "10.0.0.1".parse().unwrap(), - "8.8.8.8".parse().unwrap(), - ]) - }) - .unwrap() - .unwrap(); - assert_eq!(found.as_ip(), "8.8.8.8".parse::().unwrap()); - assert!(matches!( - resolve_with::>("example.com", |_| Err(std::io::Error::other( - "DNS failed" - ))), - Err(ObservationError::Resolution(_)) - )); - } -} +mod tests; diff --git a/crates/connections-io/src/resolver/tests.rs b/crates/connections-io/src/resolver/tests.rs new file mode 100644 index 00000000..dc6de31b --- /dev/null +++ b/crates/connections-io/src/resolver/tests.rs @@ -0,0 +1,82 @@ +use super::*; +#[test] +fn literals_and_local_names_never_use_dns() { + for host in [ + "192.168.1.10", + "[::1]", + "pi.local", + "localhost", + "", + "8.8.8.8", + ] { + let result = + resolve_with::>(host, |_| panic!("unexpected DNS for {host}")).unwrap(); + assert_eq!(result.is_some(), host == "8.8.8.8"); + } +} +#[test] +fn resolution_selects_first_public_address_only() { + let found = resolve_with("example.com", |_| { + Ok(vec![ + "10.0.0.1".parse().unwrap(), + "8.8.8.8".parse().unwrap(), + ]) + }) + .unwrap() + .unwrap(); + assert_eq!(found.as_ip(), "8.8.8.8".parse::().unwrap()); + assert!(matches!( + resolve_with::>("example.com", |_| Err(std::io::Error::other("DNS failed"))), + Err(ObservationError::Resolution(_)) + )); +} + +#[test] +fn stalled_dns_keeps_only_four_workers_and_expired_queued_queries_are_skipped() { + use std::sync::{ + Condvar, + atomic::{AtomicUsize, Ordering}, + }; + let gate = Arc::new((Mutex::new(false), Condvar::new())); + let started = Arc::new(AtomicUsize::new(0)); + let (began, began_rx) = mpsc::channel(); + let lookup: Lookup = { + let gate = gate.clone(); + let started = started.clone(); + Arc::new(move |_| { + started.fetch_add(1, Ordering::SeqCst); + began.send(()).unwrap(); + let (lock, ready) = &*gate; + let _ = ready + .wait_timeout_while(lock.lock().unwrap(), Duration::from_secs(5), |open| !*open) + .unwrap(); + Ok(vec!["8.8.8.8".parse().unwrap()]) + }) + }; + let resolver = Arc::new(NativeResolver { + requests: OnceLock::from(Ok(workers(lookup).unwrap())), + deadline: Duration::from_millis(100), + }); + let calls: Vec<_> = (0..DNS_WORKERS) + .map(|_| { + let resolver = resolver.clone(); + thread::spawn(move || resolver.resolve("stalled.example")) + }) + .collect(); + for _ in 0..DNS_WORKERS { + began_rx.recv_timeout(Duration::from_secs(2)).unwrap(); + } + for call in calls { + assert!(call.join().unwrap().is_err()); + } + assert!(resolver.resolve("expires.example").is_err()); + assert_eq!(started.load(Ordering::SeqCst), DNS_WORKERS); + { + let (lock, ready) = &*gate; + *lock.lock().unwrap() = true; + ready.notify_all(); + } + let resolved = resolver.resolve("ready.example").unwrap().unwrap(); + assert_eq!(resolved.as_ip(), "8.8.8.8".parse::().unwrap()); + assert_eq!(started.load(Ordering::SeqCst), DNS_WORKERS + 1); +} diff --git a/crates/connections-model/Cargo.toml b/crates/connections-model/Cargo.toml index df024bd9..7df6e6a2 100644 --- a/crates/connections-model/Cargo.toml +++ b/crates/connections-model/Cargo.toml @@ -17,6 +17,7 @@ serde.workspace = true toml.workspace = true uuid = { version = "1", default-features = false, features = ["std", "serde"] } thiserror.workspace = true +futures.workspace = true [features] test-support = [] diff --git a/crates/connections-model/src/draft.rs b/crates/connections-model/src/draft.rs index 038e6ead..7b98c48e 100644 --- a/crates/connections-model/src/draft.rs +++ b/crates/connections-model/src/draft.rs @@ -1,8 +1,8 @@ //! Text drafts and checked values; defaults and identifiers are supplied by callers. use crate::jump_route::JumpHostEntry; use nocterm_session::{ - Auth, CredentialId, CredentialIdParseError, DEFAULT_PORT, LaunchValidationError, - SessionOptions, SessionOptionsError, ShellLaunch, Target, + Auth, CredentialId, DEFAULT_PORT, LaunchValidationError, SessionOptions, SessionOptionsError, + ShellLaunch, Target, }; use std::path::{Path, PathBuf}; @@ -12,6 +12,8 @@ pub enum AuthKind { Auto, Password, Key, + /// A key kept in the vault, chosen in the editor. + VaultKey, } #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ValidationField { @@ -19,7 +21,6 @@ pub enum ValidationField { Port, User, Key, - Credential, Arguments, Environment, Launch, @@ -42,8 +43,8 @@ pub enum ProfileValidationError { MissingUser, #[error("Choose the private key file to sign in with.")] MissingKey, - #[error(transparent)] - Credential(#[from] CredentialIdParseError), + #[error("Choose a key from the vault, or create or import one.")] + MissingVaultKey, #[error("Arguments must be a JSON array of strings.")] Arguments(#[source] serde_json::Error), #[error("Environment must be a JSON object of string values.")] @@ -65,8 +66,7 @@ impl ProfileValidationError { Self::MissingHost | Self::InvalidHost => ValidationField::Host, Self::InvalidPort(_) => ValidationField::Port, Self::MissingUser => ValidationField::User, - Self::MissingKey => ValidationField::Key, - Self::Credential(_) => ValidationField::Credential, + Self::MissingKey | Self::MissingVaultKey => ValidationField::Key, Self::Arguments(_) => ValidationField::Arguments, Self::Environment(_) => ValidationField::Environment, Self::Launch(_) => ValidationField::Launch, @@ -93,7 +93,16 @@ pub struct EndpointDraft { pub struct AuthenticationDraft { pub kind: AuthKind, pub key_path: String, - pub credential: String, + /// The vault record and canonical public half of the chosen vault key. + pub vault_key: Option, + /// The vault record of the password the connection signs in with, as the editor kept it. + pub credential: Option, +} +/// A key the vault holds, as the profile will name it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct VaultKeyChoice { + pub credential: CredentialId, + pub public_key: String, } #[derive(Clone, Debug, Default, PartialEq, Eq)] pub struct LaunchDraft { @@ -168,9 +177,6 @@ impl ProfileDraft { ) -> Result { let target = self.endpoint.validate(defaults.user.as_deref())?; let auth = self.authentication.validate(defaults.home.as_deref())?; - let credential = optional(&self.authentication.credential) - .map(|id| id.parse()) - .transpose()?; let launch = self.launch.validate()?; self.options.validate()?; let appearance = self.appearance.validate()?; @@ -183,7 +189,7 @@ impl ProfileDraft { group: optional(&self.metadata.group), target, auth, - credential, + credential: self.authentication.credential, options: self.options.clone(), launch, icon: appearance.icon, @@ -227,6 +233,16 @@ impl AuthenticationDraft { path: expand_home(&path, home), } } + AuthKind::VaultKey => { + let key = self + .vault_key + .clone() + .ok_or(ProfileValidationError::MissingVaultKey)?; + Auth::VaultKey { + credential: key.credential, + public_key: key.public_key, + } + } }) } } diff --git a/crates/connections-model/src/draft/tests.rs b/crates/connections-model/src/draft/tests.rs index 6019b1fe..53a3c996 100644 --- a/crates/connections-model/src/draft/tests.rs +++ b/crates/connections-model/src/draft/tests.rs @@ -46,7 +46,6 @@ fn endpoint_authentication_and_launch_validation_keep_their_order() { draft.endpoint.host.clear(); draft.endpoint.port = "invalid".into(); draft.authentication.kind = AuthKind::Key; - draft.authentication.credential = "invalid".into(); draft.launch.override_launch = true; draft.launch.args = "invalid".into(); assert!(matches!( @@ -64,13 +63,6 @@ fn endpoint_authentication_and_launch_validation_keep_their_order() { ValidationField::Key ); draft.authentication.key_path = "/supplied/key".into(); - assert!(matches!( - draft.validate(&defaults()), - Err(ProfileValidationError::Credential( - CredentialIdParseError::InvalidShape - )) - )); - draft.authentication.credential.clear(); assert_eq!( draft.validate(&defaults()).unwrap_err().field(), ValidationField::Arguments @@ -177,3 +169,27 @@ fn jump_hosts_flow_from_the_draft_into_the_saved_profile() { let profile = validated.into_profile(crate::test_support::profile_id_for("db")); assert_eq!(profile.jump_hosts, draft.jump_hosts); } +#[test] +fn a_vault_key_draft_becomes_an_auth_that_names_the_record_and_its_public_half() { + let mut draft = draft(); + draft.authentication.kind = AuthKind::VaultKey; + let error = draft.validate(&defaults()).unwrap_err(); + assert!(matches!(error, ProfileValidationError::MissingVaultKey)); + assert_eq!(error.field(), ValidationField::Key); + let credential = CredentialId::from_bytes([4; 16]); + draft.authentication.vault_key = Some(VaultKeyChoice { + credential, + public_key: "ssh-ed25519 AAAA".into(), + }); + // The typed file path and password reference are not part of this method. + draft.authentication.key_path = "~/.ssh/ignored".into(); + let profile = draft.validate(&defaults()).unwrap(); + assert_eq!( + profile.auth, + Auth::VaultKey { + credential, + public_key: "ssh-ed25519 AAAA".into() + } + ); + assert_eq!(profile.credential, None); +} diff --git a/crates/connections-model/src/lib.rs b/crates/connections-model/src/lib.rs index cb6890b2..afbca6ec 100644 --- a/crates/connections-model/src/lib.rs +++ b/crates/connections-model/src/lib.rs @@ -1,8 +1,8 @@ //! Pure saved-connection formats, drafts, transitions and native capability ports. //! -//! Owns: profile/recent/facts formats, jump-host references and route resolution, checked mutations, live-profile authority stamps, recent-use intents, drafts, checked OS identities/release policy, observation values and bounded immutable flag bytes. +//! Owns: profile/recent/facts formats, jump-host references and route resolution, checked mutations, live-profile authority stamps, recent-use intents, drafts, checked OS identities/release policy, observation values, bounded immutable flag bytes, and the replica reconciliation rules with the agreed-state document. //! Does not own: DNS, HTTP, files, clocks, GPUI or observation lifecycle. -//! Ports provided: ConnectionRepository, ProfileIds, ConnectionClock and country/flag/RemoteOsDetector observation ports. +//! Ports provided: ConnectionRepository, ProfileIds, ConnectionClock, ProfileReplica and country/flag/RemoteOsDetector observation ports. //! Ports used: session value types. Settings section: none. Globals: none. //! Invariants: only checked public addresses reach country lookup; country keys //! are two lower-case ASCII letters; supplied raw flag bytes are bounded at 256 KiB. IDs/timestamps/defaults are supplied values; @@ -19,6 +19,7 @@ pub mod os_observation; pub mod profile_authority; pub mod profiles; pub mod recents; +pub mod replica; pub mod repository; pub mod value_ports; diff --git a/crates/connections-model/src/profiles.rs b/crates/connections-model/src/profiles.rs index 381f0f33..9a6e1b85 100644 --- a/crates/connections-model/src/profiles.rs +++ b/crates/connections-model/src/profiles.rs @@ -10,7 +10,7 @@ use serde::{Deserialize, Serialize}; use uuid::Uuid; /// Identifies a saved connection across renames and edits. -#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] #[serde(transparent)] pub struct ProfileId(Uuid); @@ -195,6 +195,33 @@ impl Profiles { self.forget_group(group); } + /// Moves the `members` that are in folder `from` to folder `to` (`None`: the top + /// level); connections of the folder that are not listed keep it. + pub fn regroup(&mut self, members: &[ProfileId], from: &str, to: Option<&str>) { + if to == Some(from) { + return; + } + for profile in &mut self.list { + if profile.group.as_deref() == Some(from) && members.contains(&profile.id) { + profile.group = to.map(str::to_owned); + } + } + self.forget_group(from); + self.remember_group(to); + } + + /// Deletes the `members` that are in `group`, returning the removed ones. + pub fn remove_members(&mut self, group: &str, members: &[ProfileId]) -> Vec { + let (removed, kept) = std::mem::take(&mut self.list) + .into_iter() + .partition(|profile| { + profile.group.as_deref() == Some(group) && members.contains(&profile.id) + }); + self.list = kept; + self.forget_group(group); + removed + } + /// Deletes the folder and its connections, returning the removed ones. pub fn remove_group(&mut self, group: &str) -> Vec { let (removed, kept) = std::mem::take(&mut self.list) diff --git a/crates/connections-model/src/profiles/mutation.rs b/crates/connections-model/src/profiles/mutation.rs index cd23e31b..c78af154 100644 --- a/crates/connections-model/src/profiles/mutation.rs +++ b/crates/connections-model/src/profiles/mutation.rs @@ -64,6 +64,19 @@ pub enum ProfileMutation { group: String, expected: Vec, }, + /// Moves the listed `members` of folder `from` to folder `to` (`None`: the top level). + /// Connections of the folder that are not listed, such as one that joined it after it + /// was shown, stay where they are. + Regroup { + members: Vec, + from: String, + to: Option, + }, + /// Deletes the listed `members` of `group`, provided each is still in it. + DeleteMembers { + group: String, + members: Vec, + }, Credential { scope: CredentialScope, id: CredentialId, @@ -75,7 +88,12 @@ impl ProfileMutation { let metadata_bytes = match self { Self::Move { group, .. } => group.as_ref().map_or(0, String::len), Self::RenameGroup { from, to } => from.len().saturating_add(to.len()), - Self::Ungroup(group) | Self::DeleteGroup { group, .. } => group.len(), + Self::Regroup { from, to, .. } => from + .len() + .saturating_add(to.as_ref().map_or(0, String::len)), + Self::Ungroup(group) + | Self::DeleteGroup { group, .. } + | Self::DeleteMembers { group, .. } => group.len(), Self::Credential { scope, .. } => scope .target() .host @@ -146,6 +164,26 @@ impl ProfileMutation { } profiles.remove_group(group); } + Self::Regroup { members, from, to } => { + require_group(profiles, from)?; + let to = match to.as_deref().map(str::trim) { + Some("") => return Err(ProfileError::EmptyGroup), + other => other, + }; + profiles.regroup(members, from, to); + } + Self::DeleteMembers { group, members } => { + require_group(profiles, group)?; + let all_in = members.iter().all(|id| { + profiles + .get(*id) + .is_some_and(|profile| profile.group.as_deref() == Some(group)) + }); + if !all_in { + return Err(ProfileError::GroupChanged(group.clone())); + } + profiles.remove_members(group, members); + } Self::Credential { scope, id } => { profiles.associate_credential(scope, *id); } diff --git a/crates/connections-model/src/profiles/tests.rs b/crates/connections-model/src/profiles/tests.rs index 05e4023f..b7742f36 100644 --- a/crates/connections-model/src/profiles/tests.rs +++ b/crates/connections-model/src/profiles/tests.rs @@ -669,3 +669,61 @@ fn a_routed_session_without_a_saved_connection_has_nowhere_to_remember_a_credent Some(CredentialScope::direct(target, Auth::Auto)) ); } + +#[test] +fn regrouping_listed_members_leaves_the_rest_of_the_folder_in_place() { + let (a, b) = ( + profile("a", "a.example", Some("Work")), + profile("b", "b.example", Some("Work")), + ); + let mut profiles = Profiles::default(); + profiles.upsert(a.clone()); + profiles.upsert(b.clone()); + ProfileMutation::Regroup { + members: vec![a.id], + from: "Work".into(), + to: Some(" Team ".into()), + } + .apply(&mut profiles) + .unwrap(); + assert_eq!(profiles.get(a.id).unwrap().group.as_deref(), Some("Team")); + assert_eq!(profiles.get(b.id).unwrap().group.as_deref(), Some("Work")); + assert!(matches!( + ProfileMutation::Regroup { + members: vec![a.id], + from: "Team".into(), + to: Some(" ".into()), + } + .apply(&mut profiles), + Err(ProfileError::EmptyGroup) + )); +} + +#[test] +fn deleting_listed_members_keeps_the_rest_and_refuses_one_that_left() { + let (a, b) = ( + profile("a", "a.example", Some("Work")), + profile("b", "b.example", Some("Work")), + ); + let mut profiles = Profiles::default(); + profiles.upsert(a.clone()); + profiles.upsert(b.clone()); + let delete = |members: Vec| ProfileMutation::DeleteMembers { + group: "Work".into(), + members, + }; + ProfileMutation::Move { + id: b.id, + group: None, + before: None, + } + .apply(&mut profiles) + .unwrap(); + assert!(matches!( + delete(vec![a.id, b.id]).apply(&mut profiles), + Err(ProfileError::GroupChanged(_)) + )); + assert!(profiles.get(a.id).is_some()); + delete(vec![a.id]).apply(&mut profiles).unwrap(); + assert!(profiles.get(a.id).is_none() && profiles.get(b.id).is_some()); +} diff --git a/crates/connections-model/src/replica.rs b/crates/connections-model/src/replica.rs new file mode 100644 index 00000000..a33ff421 --- /dev/null +++ b/crates/connections-model/src/replica.rs @@ -0,0 +1,128 @@ +//! The port a remote copy of the saved profiles implements, and the reconciliation rules. +//! +//! The local document stays the source the application reads. A replica is a second +//! copy, kept by another party (a hub), that changes independently. The rules in +//! [`rules`] decide, per profile, what to do when the local copy, the replica and the +//! last agreed state differ; they never perform I/O. +mod state; + +pub mod rules; + +pub use state::{ReplicaState, SyncedEntry}; + +use crate::profiles::{Profile, ProfileId}; +use futures::{future::BoxFuture, stream::BoxStream}; +use nocterm_core::SharedCause; +use serde::{Deserialize, Serialize}; + +/// An opaque position in the replica's change feed. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct ReplicaCursor(String); + +impl ReplicaCursor { + pub fn new(token: impl Into) -> Self { + Self(token.into()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +/// The replica's revision of one profile, used as the expectation of the next write. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(transparent)] +pub struct ReplicaVersion(u64); + +impl ReplicaVersion { + pub fn new(version: u64) -> Self { + Self(version) + } + + pub fn get(self) -> u64 { + self.0 + } +} + +/// What the replica holds for a profile. +#[derive(Clone, Debug, PartialEq)] +pub enum RemoteBody { + Present(Box), + /// A tombstone: the profile was deleted and the deletion is itself a version. + Deleted, + /// An object this client cannot decode, usually written by a newer client. + Unrecognized, +} + +/// One entry of the change feed. +#[derive(Clone, Debug, PartialEq)] +pub struct RemoteProfile { + pub id: ProfileId, + pub version: ReplicaVersion, + pub body: RemoteBody, +} + +/// Changes after a cursor, in the order the replica made them. A profile's versions +/// grow along the feed, but the rules do not rely on it: a change older than the +/// version already agreed is ignored. +#[derive(Clone, Debug, PartialEq)] +pub struct ReplicaPage { + pub changes: Vec, + /// Where to continue reading after these changes. + pub cursor: ReplicaCursor, + /// More changes are available immediately after `cursor`. + pub more: bool, +} + +#[derive(Clone, Debug, thiserror::Error)] +pub enum ReplicaError { + /// The original account capture no longer belongs to the current login. + #[error("The original profile replica is no longer current.")] + Stale, + /// The replica cannot be reached now; local work continues. + #[error("The profile replica is not reachable.")] + Unavailable, + /// The keys that open the replica are closed, so nothing can be read or written until + /// they are opened again. The server may well be reachable. + #[error("The profile replica is locked.")] + Locked, + /// The expected version is no longer the replica's current one. + #[error("Profile {id} changed on the replica.")] + Conflict { id: ProfileId }, + /// The account may not change this profile. + #[error("Profile {id} cannot be changed on the replica.")] + Forbidden { id: ProfileId }, + #[error("The profile replica failed: {0}")] + Other(#[source] SharedCause), +} + +/// A remote copy of the saved profiles, one sealed object per profile. +pub trait ProfileReplica: Send + Sync { + /// Changes after `after`; `None` reads from the beginning. + fn changes( + &self, + after: Option, + ) -> BoxFuture<'static, Result>; + + /// Writes `profile`. `expected` is the version this client last saw, or `None` + /// to create; a replica holding another version answers `Conflict`. + fn put( + &self, + profile: Profile, + expected: Option, + ) -> BoxFuture<'static, Result>; + + /// Replaces the profile with a tombstone under the same expectation rules. + fn delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + ) -> BoxFuture<'static, Result>; + + /// Yields once whenever the replica may have new changes. + fn watch(&self) -> BoxStream<'static, ()>; +} + +#[cfg(test)] +mod tests; diff --git a/crates/connections-model/src/replica/rules.rs b/crates/connections-model/src/replica/rules.rs new file mode 100644 index 00000000..75f03de1 --- /dev/null +++ b/crates/connections-model/src/replica/rules.rs @@ -0,0 +1,277 @@ +//! Three-way reconciliation of one profile: local `L`, remote `R` and the last agreed `S`. +//! +//! The rules are pure. They name what must happen and leave doing it, and the order +//! of side effects, to the caller. A remote change always wins a conflict; the local +//! edit it replaces is reported so the user can be told. A change older than the +//! agreed version is a late entry of the feed, never a newer state, and is ignored. +use super::{RemoteBody, RemoteProfile, ReplicaState, ReplicaVersion, SyncedEntry}; +use crate::profiles::{Profile, ProfileId, Profiles}; +use std::collections::{HashMap, HashSet}; + +/// One effect the reconciliation asks for. +#[derive(Clone, Debug, PartialEq)] +pub enum ReplicaAction { + /// Write `profile` locally if the local copy still is `expected` (`None`: absent), + /// then record agreement at `version`. + SaveLocal { + version: ReplicaVersion, + profile: Box, + expected: Option>, + }, + /// Delete the local profile if it still is `expected`, then forget it. + DeleteLocal { + id: ProfileId, + expected: Box, + }, + /// Send `profile`; `expected` is the replica's version to replace (`None`: create). + Push { + profile: Box, + expected: Option, + }, + /// Send a deletion replacing the replica's `expected` version. + PushDelete { + id: ProfileId, + expected: ReplicaVersion, + }, + /// Both sides already hold `profile`; record agreement at `version`. + MarkSynced { + version: ReplicaVersion, + profile: Box, + }, + /// Neither side holds the profile any more; drop the agreement. + Forget { id: ProfileId }, + /// A local edit was replaced by the remote version. + Overwritten { + id: ProfileId, + discarded: Box, + }, + /// The remote version cannot be read; leave the local profile alone. + NeedsNewerClient { id: ProfileId }, +} + +impl ReplicaAction { + pub fn id(&self) -> ProfileId { + match self { + Self::SaveLocal { profile, .. } + | Self::Push { profile, .. } + | Self::MarkSynced { profile, .. } => profile.id, + Self::DeleteLocal { id, .. } + | Self::PushDelete { id, .. } + | Self::Forget { id } + | Self::Overwritten { id, .. } + | Self::NeedsNewerClient { id } => *id, + } + } +} + +/// Reconciles every profile known to any of the three sides. +/// +/// `changes` are the remote changes read since the state's cursor; a profile without +/// one, or with only changes older than its agreed version, is unchanged remotely. A +/// profile the replica holds unreadably stays untouched until a readable version of it +/// arrives. +pub fn plan( + local: &Profiles, + changes: &[RemoteProfile], + state: &ReplicaState, +) -> Vec { + let latest = latest_changes(changes, state); + let local_by_id: HashMap<_, _> = local.iter().map(|profile| (profile.id, profile)).collect(); + let mut remembered: Vec = state.entries().map(|(id, _)| id).collect(); + remembered.sort_unstable(); + let mut seen = HashSet::new(); + local + .iter() + .map(|profile| profile.id) + .chain(changes.iter().map(|change| change.id)) + .chain(remembered) + .filter(|id| seen.insert(*id)) + .filter(|id| !state.is_unrecognized(*id) || latest.contains_key(id)) + .flat_map(|id| { + reconcile( + local_by_id.get(&id).copied(), + latest.get(&id).copied(), + state.entry(id), + ) + }) + .collect() +} + +/// The change that stands for each profile: the later one of the feed. Late entries older +/// than the agreed version never stand. +fn latest_changes<'a>( + changes: &'a [RemoteProfile], + state: &ReplicaState, +) -> HashMap { + changes + .iter() + .filter(|change| !is_stale(change, state.entry(change.id))) + .map(|change| (change.id, change)) + .collect() +} + +/// The feed delivered a version older than the one already agreed for the profile. +fn is_stale(change: &RemoteProfile, synced: Option<&SyncedEntry>) -> bool { + synced.is_some_and(|synced| change.version < synced.version) +} + +/// What to do about one profile. At least one of the three must be present. +pub fn reconcile( + local: Option<&Profile>, + remote: Option<&RemoteProfile>, + synced: Option<&SyncedEntry>, +) -> Vec { + let remote = remote.filter(|change| !is_stale(change, synced)); + let Some(id) = local + .map(|profile| profile.id) + .or_else(|| remote.map(|change| change.id)) + .or_else(|| synced.map(|entry| entry.profile.id)) + else { + return Vec::new(); + }; + if remote.is_some_and(|change| change.body == RemoteBody::Unrecognized) { + return vec![ReplicaAction::NeedsNewerClient { id }]; + } + match synced { + Some(synced) => with_history(local, remote, synced), + None => without_history(local, remote), + } +} + +fn boxed(profile: &Profile) -> Box { + Box::new(profile.clone()) +} + +fn save_remote( + change: &RemoteProfile, + profile: &Profile, + expected: Option<&Profile>, +) -> ReplicaAction { + ReplicaAction::SaveLocal { + version: change.version, + profile: boxed(profile), + expected: expected.map(boxed), + } +} + +fn mark_synced(change: &RemoteProfile, profile: &Profile) -> ReplicaAction { + ReplicaAction::MarkSynced { + version: change.version, + profile: boxed(profile), + } +} + +/// Both disagree with a remote profile that has no agreed state: the remote wins. +fn take_remote( + change: &RemoteProfile, + remote: &Profile, + local: Option<&Profile>, +) -> Vec { + match local { + Some(local) if local == remote => vec![mark_synced(change, remote)], + Some(local) => vec![ + save_remote(change, remote, Some(local)), + ReplicaAction::Overwritten { + id: local.id, + discarded: boxed(local), + }, + ], + None => vec![save_remote(change, remote, None)], + } +} + +fn without_history(local: Option<&Profile>, remote: Option<&RemoteProfile>) -> Vec { + match (local, remote) { + (None, None) => Vec::new(), + (Some(local), None) => vec![ReplicaAction::Push { + profile: boxed(local), + expected: None, + }], + (_, Some(change)) => match &change.body { + RemoteBody::Present(remote) => take_remote(change, remote, local), + RemoteBody::Deleted => local + .map(|local| ReplicaAction::Push { + profile: boxed(local), + expected: Some(change.version), + }) + .into_iter() + .collect(), + RemoteBody::Unrecognized => Vec::new(), + }, + } +} + +#[derive(Clone, Copy)] +enum Side<'a> { + Equal, + Changed(&'a Profile), + Deleted, +} + +/// The remote side relative to `S`. A change carrying `S`'s version, or `S`'s content +/// at a newer version, is not a change of content. +fn remote_side<'a>(remote: Option<&'a RemoteProfile>, synced: &SyncedEntry) -> Side<'a> { + let Some(change) = remote else { + return Side::Equal; + }; + match &change.body { + _ if change.version == synced.version => Side::Equal, + RemoteBody::Present(profile) if **profile == synced.profile => Side::Equal, + RemoteBody::Present(profile) => Side::Changed(profile), + RemoteBody::Deleted => Side::Deleted, + RemoteBody::Unrecognized => Side::Equal, + } +} + +fn local_side<'a>(local: Option<&'a Profile>, synced: &SyncedEntry) -> Side<'a> { + match local { + None => Side::Deleted, + Some(local) if *local == synced.profile => Side::Equal, + Some(local) => Side::Changed(local), + } +} + +fn with_history( + local: Option<&Profile>, + remote: Option<&RemoteProfile>, + synced: &SyncedEntry, +) -> Vec { + let version = remote.map_or(synced.version, |change| change.version); + match ( + local_side(local, synced), + remote_side(remote, synced), + local, + remote, + ) { + (Side::Equal, Side::Equal, ..) if version == synced.version => Vec::new(), + (Side::Equal, Side::Equal, ..) => vec![ReplicaAction::MarkSynced { + version, + profile: boxed(&synced.profile), + }], + (Side::Equal, Side::Changed(profile), _, Some(change)) => { + vec![save_remote(change, profile, Some(&synced.profile))] + } + (Side::Equal, Side::Deleted, ..) => vec![ReplicaAction::DeleteLocal { + id: synced.profile.id, + expected: boxed(&synced.profile), + }], + (Side::Changed(local), Side::Equal | Side::Deleted, ..) => vec![ReplicaAction::Push { + profile: boxed(local), + expected: Some(version), + }], + (Side::Changed(local), Side::Changed(profile), _, Some(change)) => { + take_remote(change, profile, Some(local)) + } + (Side::Deleted, Side::Equal, ..) => vec![ReplicaAction::PushDelete { + id: synced.profile.id, + expected: version, + }], + (Side::Deleted, Side::Changed(profile), _, Some(change)) => { + vec![save_remote(change, profile, None)] + } + (Side::Deleted, Side::Deleted, ..) => vec![ReplicaAction::Forget { + id: synced.profile.id, + }], + _ => Vec::new(), + } +} diff --git a/crates/connections-model/src/replica/state.rs b/crates/connections-model/src/replica/state.rs new file mode 100644 index 00000000..5caa9bd8 --- /dev/null +++ b/crates/connections-model/src/replica/state.rs @@ -0,0 +1,68 @@ +//! The last state both sides agreed on, which tells a local change from a remote one. +use super::{ReplicaCursor, ReplicaVersion}; +use crate::profiles::{Profile, ProfileId}; +use serde::{Deserialize, Serialize}; +use std::collections::{HashMap, HashSet}; + +/// A profile as it was when the local copy and the replica last agreed. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct SyncedEntry { + pub version: ReplicaVersion, + pub profile: Profile, +} + +/// What is persisted between runs: the feed position, the agreed profiles and the +/// profiles the replica holds in a form this client cannot read. +#[derive(Clone, Debug, Default, PartialEq, Serialize, Deserialize)] +pub struct ReplicaState { + #[serde(default, skip_serializing_if = "Option::is_none")] + cursor: Option, + #[serde(default)] + entries: HashMap, + #[serde(default, skip_serializing_if = "HashSet::is_empty")] + unrecognized: HashSet, +} + +impl ReplicaState { + pub fn cursor(&self) -> Option<&ReplicaCursor> { + self.cursor.as_ref() + } + + pub fn entry(&self, id: ProfileId) -> Option<&SyncedEntry> { + self.entries.get(&id) + } + + pub fn entries(&self) -> impl Iterator { + self.entries.iter().map(|(id, entry)| (*id, entry)) + } + + /// The replica holds a version of this profile that this client cannot read. + pub fn is_unrecognized(&self, id: ProfileId) -> bool { + self.unrecognized.contains(&id) + } + + pub fn has_unrecognized(&self) -> bool { + !self.unrecognized.is_empty() + } + + pub fn advance(&mut self, cursor: ReplicaCursor) { + self.cursor = Some(cursor); + } + + /// Records agreement on `entry`, whether this client sent it or took the replica's + /// version; the replica's version is readable again. + pub fn agree(&mut self, id: ProfileId, entry: SyncedEntry) { + self.unrecognized.remove(&id); + self.entries.insert(id, entry); + } + + /// Neither side holds the profile any more: the agreement goes. + pub fn forget(&mut self, id: ProfileId) { + self.unrecognized.remove(&id); + self.entries.remove(&id); + } + + pub fn mark_unrecognized(&mut self, id: ProfileId) { + self.unrecognized.insert(id); + } +} diff --git a/crates/connections-model/src/replica/tests.rs b/crates/connections-model/src/replica/tests.rs new file mode 100644 index 00000000..efe909a3 --- /dev/null +++ b/crates/connections-model/src/replica/tests.rs @@ -0,0 +1,370 @@ +use super::rules::{ReplicaAction, plan, reconcile}; +use super::*; +use crate::{profiles::Profiles, test_support::profile_id_for}; +use nocterm_session::{Auth, Target}; + +fn profile(name: &str) -> Profile { + Profile { + id: profile_id_for("shared"), + name: name.into(), + description: String::new(), + options: Default::default(), + target: Target::new("ci", "host", 22), + auth: Auth::Password, + credential: None, + launch: None, + icon: None, + icon_color: None, + country: None, + group: None, + jump_hosts: Vec::new(), + } +} + +fn id() -> ProfileId { + profile_id_for("shared") +} + +fn version(version: u64) -> ReplicaVersion { + ReplicaVersion::new(version) +} + +fn synced(name: &str, at: u64) -> SyncedEntry { + SyncedEntry { + version: version(at), + profile: profile(name), + } +} + +fn remote(body: RemoteBody, at: u64) -> RemoteProfile { + RemoteProfile { + id: id(), + version: version(at), + body, + } +} + +fn present(name: &str, at: u64) -> RemoteProfile { + remote(RemoteBody::Present(Box::new(profile(name))), at) +} + +fn save(name: &str, at: u64, expected: Option<&str>) -> ReplicaAction { + ReplicaAction::SaveLocal { + version: version(at), + profile: Box::new(profile(name)), + expected: expected.map(|name| Box::new(profile(name))), + } +} + +fn push(name: &str, expected: Option) -> ReplicaAction { + ReplicaAction::Push { + profile: Box::new(profile(name)), + expected: expected.map(version), + } +} + +fn marked(name: &str, at: u64) -> ReplicaAction { + ReplicaAction::MarkSynced { + version: version(at), + profile: Box::new(profile(name)), + } +} + +#[test] +fn nothing_happens_when_local_and_remote_equal_the_synced_profile() { + let local = profile("a"); + let actions = reconcile(Some(&local), Some(&present("a", 3)), Some(&synced("a", 3))); + assert_eq!(actions, []); + assert_eq!(reconcile(Some(&local), None, Some(&synced("a", 3))), []); +} + +#[test] +fn a_newer_version_with_the_synced_content_only_updates_the_agreed_version() { + let local = profile("a"); + let actions = reconcile(Some(&local), Some(&present("a", 4)), Some(&synced("a", 3))); + assert_eq!(actions, [marked("a", 4)]); +} + +#[test] +fn a_remote_change_is_applied_over_an_unchanged_local_profile() { + let local = profile("a"); + let actions = reconcile(Some(&local), Some(&present("b", 4)), Some(&synced("a", 3))); + assert_eq!(actions, [save("b", 4, Some("a"))]); +} + +#[test] +fn a_local_change_is_sent_against_the_synced_version() { + let local = profile("b"); + assert_eq!( + reconcile(Some(&local), None, Some(&synced("a", 3))), + [push("b", Some(3))] + ); + let echo = present("a", 3); + assert_eq!( + reconcile(Some(&local), Some(&echo), Some(&synced("a", 3))), + [push("b", Some(3))] + ); +} + +#[test] +fn equal_edits_on_both_sides_are_marked_synced_without_a_conflict() { + let local = profile("c"); + let actions = reconcile(Some(&local), Some(&present("c", 5)), Some(&synced("a", 3))); + assert_eq!(actions, [marked("c", 5)]); +} + +#[test] +fn different_edits_on_both_sides_apply_the_remote_and_report_the_overwrite() { + let local = profile("b"); + let actions = reconcile(Some(&local), Some(&present("c", 5)), Some(&synced("a", 3))); + assert_eq!( + actions, + [ + save("c", 5, Some("b")), + ReplicaAction::Overwritten { + id: id(), + discarded: Box::new(profile("b")), + }, + ] + ); +} + +#[test] +fn a_new_local_profile_without_history_is_sent_as_new() { + let local = profile("a"); + assert_eq!(reconcile(Some(&local), None, None), [push("a", None)]); +} + +#[test] +fn a_new_remote_profile_without_history_is_applied_as_new() { + let actions = reconcile(None, Some(&present("a", 2)), None); + assert_eq!(actions, [save("a", 2, None)]); +} + +#[test] +fn a_local_and_a_remote_profile_without_history_are_reconciled_like_two_edits() { + let local = profile("a"); + assert_eq!( + reconcile(Some(&local), Some(&present("a", 2)), None), + [marked("a", 2)] + ); + assert_eq!( + reconcile(Some(&profile("b")), Some(&present("a", 2)), None), + [ + save("a", 2, Some("b")), + ReplicaAction::Overwritten { + id: id(), + discarded: Box::new(profile("b")), + }, + ] + ); +} + +#[test] +fn a_local_deletion_is_sent_when_the_remote_is_unchanged() { + let actions = reconcile(None, None, Some(&synced("a", 3))); + assert_eq!( + actions, + [ReplicaAction::PushDelete { + id: id(), + expected: version(3), + }] + ); +} + +#[test] +fn a_remote_edit_restores_a_locally_deleted_profile() { + let actions = reconcile(None, Some(&present("b", 4)), Some(&synced("a", 3))); + assert_eq!(actions, [save("b", 4, None)]); +} + +#[test] +fn a_remote_tombstone_deletes_an_unchanged_local_profile() { + let local = profile("a"); + let tombstone = remote(RemoteBody::Deleted, 4); + let actions = reconcile(Some(&local), Some(&tombstone), Some(&synced("a", 3))); + assert_eq!( + actions, + [ReplicaAction::DeleteLocal { + id: id(), + expected: Box::new(profile("a")), + }] + ); +} + +#[test] +fn a_local_edit_is_sent_over_a_remote_tombstone() { + let local = profile("b"); + let tombstone = remote(RemoteBody::Deleted, 4); + let actions = reconcile(Some(&local), Some(&tombstone), Some(&synced("a", 3))); + assert_eq!(actions, [push("b", Some(4))]); +} + +#[test] +fn deleting_on_both_sides_forgets_the_agreement() { + let tombstone = remote(RemoteBody::Deleted, 4); + let actions = reconcile(None, Some(&tombstone), Some(&synced("a", 3))); + assert_eq!(actions, [ReplicaAction::Forget { id: id() }]); +} + +#[test] +fn an_unrecognized_remote_object_never_touches_the_local_profile() { + let unreadable = remote(RemoteBody::Unrecognized, 4); + let expected = [ReplicaAction::NeedsNewerClient { id: id() }]; + let local = profile("b"); + let history = synced("a", 3); + assert_eq!( + reconcile(Some(&local), Some(&unreadable), Some(&history)), + expected + ); + assert_eq!(reconcile(Some(&local), Some(&unreadable), None), expected); + assert_eq!(reconcile(None, Some(&unreadable), Some(&history)), expected); + assert_eq!(reconcile(None, Some(&unreadable), None), expected); +} + +#[test] +fn a_remote_tombstone_without_history_is_ignored_unless_a_local_profile_exists() { + let tombstone = remote(RemoteBody::Deleted, 4); + assert_eq!(reconcile(None, Some(&tombstone), None), []); + let local = profile("a"); + assert_eq!( + reconcile(Some(&local), Some(&tombstone), None), + [push("a", Some(4))] + ); +} + +fn profiles_of(items: &[Profile]) -> Profiles { + let mut profiles = Profiles::default(); + for item in items { + profiles.upsert(item.clone()); + } + profiles +} + +#[test] +fn a_plan_covers_every_profile_known_to_any_side_once() { + let other = Profile { + id: profile_id_for("other"), + ..profile("other") + }; + let third = Profile { + id: profile_id_for("third"), + ..profile("third") + }; + let local = profiles_of(&[profile("a"), other.clone()]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 3)); + state.agree( + third.id, + SyncedEntry { + version: version(2), + profile: third.clone(), + }, + ); + let change = RemoteProfile { + id: id(), + version: version(4), + body: RemoteBody::Present(Box::new(profile("b"))), + }; + let actions = plan(&local, &[change], &state); + let ids: Vec<_> = actions.iter().map(ReplicaAction::id).collect(); + assert_eq!(ids, [id(), other.id, third.id]); + assert!(matches!(actions[0], ReplicaAction::SaveLocal { .. })); + assert!(matches!( + actions[1], + ReplicaAction::Push { expected: None, .. } + )); + assert!(matches!(actions[2], ReplicaAction::PushDelete { .. })); +} + +#[test] +fn a_plan_leaves_an_unrecognized_profile_alone_until_a_readable_version_arrives() { + let local = profiles_of(&[profile("edited")]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 3)); + state.mark_unrecognized(id()); + assert_eq!(plan(&local, &[], &state), []); + let readable = present("b", 6); + let actions = plan(&local, &[readable], &state); + assert_eq!(actions.len(), 2); + assert!(matches!(actions[1], ReplicaAction::Overwritten { .. })); +} + +#[test] +fn replica_state_survives_serialization_with_its_unrecognized_ids() { + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 3)); + state.advance(ReplicaCursor::new("7")); + state.mark_unrecognized(profile_id_for("newer")); + let text = serde_json::to_string(&state).unwrap(); + let restored: ReplicaState = serde_json::from_str(&text).unwrap(); + assert_eq!(restored, state); + assert!(restored.is_unrecognized(profile_id_for("newer"))); + assert_eq!(restored.cursor().map(ReplicaCursor::as_str), Some("7")); +} + +#[test] +fn agreeing_or_forgetting_a_profile_makes_it_readable_again() { + let mut state = ReplicaState::default(); + state.mark_unrecognized(id()); + assert!(state.has_unrecognized()); + state.agree(id(), synced("a", 3)); + assert!(!state.is_unrecognized(id())); + state.mark_unrecognized(id()); + state.forget(id()); + assert!(!state.has_unrecognized() && state.entry(id()).is_none()); +} + +#[test] +fn a_feed_entry_older_than_the_agreed_version_never_reverts_the_local_profile() { + let synced = synced("current", 5); + let stale = [ + present("old", 3), + remote(RemoteBody::Deleted, 4), + remote(RemoteBody::Unrecognized, 2), + ]; + for change in &stale { + assert_eq!( + reconcile(Some(&profile("current")), Some(change), Some(&synced)), + [] + ); + } +} + +#[test] +fn a_local_edit_is_still_sent_against_the_agreed_version_beside_a_stale_feed_entry() { + assert_eq!( + reconcile( + Some(&profile("edited")), + Some(&present("old", 3)), + Some(&synced("current", 5)), + ), + [push("edited", Some(5))] + ); +} + +#[test] +fn a_plan_ignores_stale_entries_and_keeps_an_unreadable_profile_unreadable() { + let local = profiles_of(&[profile("current")]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("current", 5)); + assert_eq!(plan(&local, &[present("old", 3)], &state), []); + state.mark_unrecognized(id()); + assert_eq!(plan(&local, &[present("old", 3)], &state), []); + assert!(state.is_unrecognized(id())); +} + +#[test] +fn of_two_changes_to_one_profile_the_later_one_stands() { + let local = profiles_of(&[profile("a")]); + let mut state = ReplicaState::default(); + state.agree(id(), synced("a", 2)); + let changes = [present("b", 3), remote(RemoteBody::Deleted, 5)]; + assert_eq!( + plan(&local, &changes, &state), + [ReplicaAction::DeleteLocal { + id: id(), + expected: Box::new(profile("a")), + }] + ); +} diff --git a/crates/connections-model/src/test_support.rs b/crates/connections-model/src/test_support.rs index a15defa7..a22eabd1 100644 --- a/crates/connections-model/src/test_support.rs +++ b/crates/connections-model/src/test_support.rs @@ -11,6 +11,8 @@ mod gates; mod repository; pub use gates::{BlockingGate, BlockingRelease}; pub use repository::{InMemoryConnectionRepository, RepositoryCall, RepositoryGate}; +mod replica; +pub use replica::{MemoryReplica, ReplicaCall}; #[derive(Clone, Debug, PartialEq, Eq)] pub enum ObservationCall { Resolve(String), diff --git a/crates/connections-model/src/test_support/replica.rs b/crates/connections-model/src/test_support/replica.rs new file mode 100644 index 00000000..140b605d --- /dev/null +++ b/crates/connections-model/src/test_support/replica.rs @@ -0,0 +1,415 @@ +//! An in-memory profile replica with scripted remote writers, refusals and outages. +use crate::{ + profiles::{Profile, ProfileId}, + replica::{ + ProfileReplica, RemoteBody, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaPage, + ReplicaVersion, + }, +}; +use futures::{ + FutureExt as _, StreamExt as _, + channel::{mpsc, oneshot}, + future::BoxFuture, + stream::BoxStream, +}; +use std::{ + collections::{HashMap, HashSet, VecDeque}, + sync::Mutex, +}; + +/// A call the replica received, in order. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum ReplicaCall { + Changes(Option), + Put { + id: ProfileId, + expected: Option, + }, + Delete { + id: ProfileId, + expected: ReplicaVersion, + }, +} + +struct Stored { + body: RemoteBody, + version: u64, +} + +/// What writes are refused: single connections, or all of them. +#[derive(Default)] +struct Forbidden { + profiles: HashSet, + all: bool, +} + +#[derive(Default)] +struct Inner { + sequence: u64, + objects: HashMap, + feed: Vec<(u64, ProfileId)>, + offline: bool, + /// The account keys are closed: the server is reachable but every call is refused. + locked: bool, + /// Successful writes left before the replica goes offline. + offline_after: Option, + /// The answer of the next write waits for this signal. + held: Option>, + forbidden: Forbidden, + forced_conflicts: usize, + concurrent: Option, + page_size: Option, + calls: Vec, +} + +/// Versions and cursors come from one counter, so a cursor is "after write N". +#[derive(Default)] +pub struct MemoryReplica { + inner: Mutex, + watchers: Mutex>>, + scripted_pages: Mutex>, + held_read: Mutex>>, +} + +impl Inner { + /// Why a call cannot be served now: closed keys are noticed before the network. + fn unserved(&self) -> Option { + if self.locked { + Some(ReplicaError::Locked) + } else if self.offline { + Some(ReplicaError::Unavailable) + } else { + None + } + } + + fn store(&mut self, id: ProfileId, body: RemoteBody) -> ReplicaVersion { + self.sequence += 1; + self.feed.push((self.sequence, id)); + self.objects.insert( + id, + Stored { + body, + version: self.sequence, + }, + ); + ReplicaVersion::new(self.sequence) + } + + fn check_write( + &mut self, + id: ProfileId, + expected: Option, + ) -> Result<(), ReplicaError> { + if let Some(error) = self.unserved() { + return Err(error); + } + if let Some(profile) = self.concurrent.take() { + let body = RemoteBody::Present(Box::new(profile.clone())); + self.store(profile.id, body); + } + if self.forbidden.all || self.forbidden.profiles.contains(&id) { + return Err(ReplicaError::Forbidden { id }); + } + if self.forced_conflicts > 0 { + self.forced_conflicts -= 1; + return Err(ReplicaError::Conflict { id }); + } + let current = self + .objects + .get(&id) + .map(|stored| ReplicaVersion::new(stored.version)); + if current == expected { + Ok(()) + } else { + Err(ReplicaError::Conflict { id }) + } + } + + fn page(&self, after: Option<&ReplicaCursor>) -> ReplicaPage { + let from = after + .and_then(|cursor| cursor.as_str().parse::().ok()) + .unwrap_or(0); + let mut ids = Vec::new(); + let mut last = from; + let mut more = false; + for (sequence, id) in self.feed.iter().filter(|(sequence, _)| *sequence > from) { + if self.page_size.is_some_and(|size| ids.len() >= size) { + more = true; + break; + } + last = *sequence; + ids.retain(|seen| seen != id); + ids.push(*id); + } + let changes = ids + .into_iter() + .filter_map(|id| { + let stored = self.objects.get(&id)?; + Some(RemoteProfile { + id, + version: ReplicaVersion::new(stored.version), + body: stored.body.clone(), + }) + }) + .collect(); + ReplicaPage { + changes, + cursor: ReplicaCursor::new(last.to_string()), + more, + } + } +} + +impl MemoryReplica { + pub fn new() -> Self { + Self::default() + } + + fn with(&self, work: impl FnOnce(&mut Inner) -> R) -> R { + work(&mut self.inner.lock().unwrap_or_else(|e| e.into_inner())) + } + + fn notify(&self) { + self.watchers + .lock() + .unwrap_or_else(|e| e.into_inner()) + .retain(|watcher| watcher.unbounded_send(()).is_ok()); + } + + /// Another device writes `profile`; watchers are told. + pub fn write_remote(&self, profile: Profile) -> ReplicaVersion { + let id = profile.id; + let version = self.with(|inner| inner.store(id, RemoteBody::Present(Box::new(profile)))); + self.notify(); + version + } + + /// Another device deletes the profile, leaving a tombstone. + pub fn delete_remote(&self, id: ProfileId) -> ReplicaVersion { + let version = self.with(|inner| inner.store(id, RemoteBody::Deleted)); + self.notify(); + version + } + + /// A newer client wrote something this client cannot read. + pub fn write_unrecognized(&self, id: ProfileId) -> ReplicaVersion { + let version = self.with(|inner| inner.store(id, RemoteBody::Unrecognized)); + self.notify(); + version + } + + /// Reachability: while offline every call answers `Unavailable`. + pub fn set_offline(&self, offline: bool) { + self.with(|inner| inner.offline = offline); + if !offline { + self.notify(); + } + } + + /// The account keys: while locked every call answers `Locked`, even when reachable. + /// Unlocking tells watchers, as the real replica does when its keys open. + pub fn set_locked(&self, locked: bool) { + self.with(|inner| inner.locked = locked); + if !locked { + self.notify(); + } + } + + /// The link drops once `writes` more writes succeed, as when it breaks between two + /// requests of one operation. + pub fn go_offline_after_writes(&self, writes: usize) { + self.with(|inner| inner.offline_after = Some(writes)); + } + + /// The next write is carried out, but its answer reaches the caller only after the + /// returned sender is used: whatever happens meanwhile happens during the round. + pub fn hold_next_write(&self) -> oneshot::Sender<()> { + let (release, held) = oneshot::channel(); + self.with(|inner| inner.held = Some(held)); + release + } + + /// Writes of this profile are refused as `Forbidden`. + pub fn refuse_writes_of(&self, id: ProfileId) { + self.with(|inner| inner.forbidden.profiles.insert(id)); + } + + /// Every write is refused as `Forbidden`. + pub fn refuse_writes(&self) { + self.with(|inner| inner.forbidden.all = true); + } + + /// The next `count` writes lose a race regardless of their expectation. + pub fn conflict_next_writes(&self, count: usize) { + self.with(|inner| inner.forced_conflicts = count); + } + + /// Another device writes `profile` just before the next write is checked. + pub fn write_concurrently_before_next_write(&self, profile: Profile) { + self.with(|inner| inner.concurrent = Some(profile)); + } + + /// Changes come in pages of at most `size` writes. + pub fn page_size(&self, size: usize) { + self.with(|inner| inner.page_size = Some(size)); + } + + /// Supplies protocol pages without changing the coherent remote object store. + /// Malformed cursors are allowed for consumer validation tests; response counts stay bounded. + pub fn script_pages(&self, pages: Vec) { + assert!(pages.len() <= 512 && pages.iter().all(|page| page.changes.len() <= 20_000)); + *self + .scripted_pages + .lock() + .unwrap_or_else(|error| error.into_inner()) = pages.into(); + } + /// Suspends the next changes response; dropping its consumer closes the receipt. + pub fn hold_next_read(&self) -> oneshot::Sender<()> { + let (send, receive) = oneshot::channel(); + *self + .held_read + .lock() + .unwrap_or_else(|error| error.into_inner()) = Some(receive); + send + } + + pub fn calls(&self) -> Vec { + self.with(|inner| inner.calls.clone()) + } + + pub fn writes(&self) -> Vec { + self.calls() + .into_iter() + .filter(|call| !matches!(call, ReplicaCall::Changes(_))) + .collect() + } + + /// The version and body of the latest write of a profile. + pub fn stored(&self, id: ProfileId) -> Option<(ReplicaVersion, RemoteBody)> { + self.with(|inner| { + let stored = inner.objects.get(&id)?; + Some((ReplicaVersion::new(stored.version), stored.body.clone())) + }) + } + + /// The live profile stored under `id`, if any. + pub fn profile(&self, id: ProfileId) -> Option { + match self.stored(id)?.1 { + RemoteBody::Present(profile) => Some(*profile), + RemoteBody::Deleted | RemoteBody::Unrecognized => None, + } + } + + fn write( + &self, + id: ProfileId, + call: ReplicaCall, + expected: Option, + body: RemoteBody, + ) -> Result { + let result = self.with(|inner| { + inner.calls.push(call); + inner.check_write(id, expected)?; + let version = inner.store(id, body); + if let Some(left) = inner.offline_after.as_mut() { + *left = left.saturating_sub(1); + if *left == 0 { + inner.offline = true; + inner.offline_after = None; + } + } + Ok(version) + }); + if result.is_ok() { + self.notify(); + } + result + } + + /// The answer of a write, after the hold on it is released. + fn answered( + &self, + result: Result, + ) -> BoxFuture<'static, Result> { + match self.with(|inner| inner.held.take()) { + Some(held) => async move { + held.await.ok(); + result + } + .boxed(), + None => futures::future::ready(result).boxed(), + } + } +} + +impl ProfileReplica for MemoryReplica { + fn changes( + &self, + after: Option, + ) -> BoxFuture<'static, Result> { + let result = self.with(|inner| { + inner.calls.push(ReplicaCall::Changes( + after.as_ref().map(|c| c.as_str().to_owned()), + )); + match inner.unserved() { + Some(error) => Err(error), + None => Ok(self + .scripted_pages + .lock() + .unwrap_or_else(|error| error.into_inner()) + .pop_front() + .unwrap_or_else(|| inner.page(after.as_ref()))), + } + }); + let held = self + .held_read + .lock() + .unwrap_or_else(|error| error.into_inner()) + .take(); + async move { + if let Some(held) = held { + let _ = held.await; + } + result + } + .boxed() + } + + fn put( + &self, + profile: Profile, + expected: Option, + ) -> BoxFuture<'static, Result> { + let call = ReplicaCall::Put { + id: profile.id, + expected, + }; + let result = self.write( + profile.id, + call, + expected, + RemoteBody::Present(Box::new(profile)), + ); + self.answered(result) + } + + fn delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + ) -> BoxFuture<'static, Result> { + let call = ReplicaCall::Delete { id, expected }; + let result = self.write(id, call, Some(expected), RemoteBody::Deleted); + self.answered(result) + } + + fn watch(&self) -> BoxStream<'static, ()> { + let (sender, receiver) = mpsc::unbounded(); + self.watchers + .lock() + .unwrap_or_else(|e| e.into_inner()) + .push(sender); + receiver.boxed() + } +} diff --git a/crates/connections-store/Cargo.toml b/crates/connections-store/Cargo.toml index 0609fb47..93e0813f 100644 --- a/crates/connections-store/Cargo.toml +++ b/crates/connections-store/Cargo.toml @@ -7,6 +7,10 @@ license-file.workspace = true publish = false [package.metadata.nocterm] layer = "service" +[features] +default = [] +test-support = [] + [dependencies] nocterm-core.workspace = true gpui.workspace = true @@ -16,8 +20,10 @@ nocterm-store.workspace = true futures.workspace = true tracing.workspace = true thiserror.workspace = true +serde_json.workspace = true [dev-dependencies] nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-store = { workspace = true, features = ["test-support"] } gpui = { workspace = true, features = ["test-support"] } nocterm-connections-model = { workspace = true, features = ["test-support"] } toml.workspace = true diff --git a/crates/connections-store/src/connections.rs b/crates/connections-store/src/connections.rs index 63947a20..b8e55e63 100644 --- a/crates/connections-store/src/connections.rs +++ b/crates/connections-store/src/connections.rs @@ -227,6 +227,44 @@ impl ConnectionStore { ) -> Task> { self.enqueue(Mutation::DeleteGroup { group, expected }, cx) } + /// Renames the folder `from` for the listed `members` only (`to` empty: error); a + /// connection that joined the folder after it was listed keeps the old name. + pub fn rename_group_of( + &mut self, + members: Vec, + from: String, + to: String, + cx: &mut Context, + ) -> Task> { + let to = Some(to); + self.enqueue(Mutation::Regroup { members, from, to }, cx) + } + /// Moves the listed `members` of a folder to the top level; the folder name is forgotten. + pub fn ungroup_of( + &mut self, + members: Vec, + group: String, + cx: &mut Context, + ) -> Task> { + let to = None; + self.enqueue( + Mutation::Regroup { + members, + from: group, + to, + }, + cx, + ) + } + /// Deletes the listed `members` of a folder, provided each is still in it. + pub fn delete_group_of( + &mut self, + group: String, + members: Vec, + cx: &mut Context, + ) -> Task> { + self.enqueue(Mutation::DeleteMembers { group, members }, cx) + } pub fn associate_credential( &mut self, scope: CredentialScope, diff --git a/crates/connections-store/src/connections/tests.rs b/crates/connections-store/src/connections/tests.rs index 59565e89..7d29947d 100644 --- a/crates/connections-store/src/connections/tests.rs +++ b/crates/connections-store/src/connections/tests.rs @@ -7,6 +7,7 @@ use nocterm_session::{Auth, Target}; use nocterm_store::{ShutdownCoordinator, SnapshotWriter, StoreError}; use std::sync::{Arc, Mutex}; +mod folders; mod publication; mod shutdown; diff --git a/crates/connections-store/src/connections/tests/folders.rs b/crates/connections-store/src/connections/tests/folders.rs new file mode 100644 index 00000000..03c206a8 --- /dev/null +++ b/crates/connections-store/src/connections/tests/folders.rs @@ -0,0 +1,83 @@ +use super::*; + +#[gpui::test] +async fn folder_actions_for_listed_members_leave_the_other_members_alone( + cx: &mut gpui::TestAppContext, +) { + let grouped = |name: &str| { + let mut item = profile(name); + item.group = Some("Work".into()); + item + }; + let (mine, theirs) = (grouped("mine"), grouped("theirs")); + let entity = cx.new(|_| Connections::in_memory()); + for item in [&mine, &theirs] { + entity + .update(cx, |e, cx| e.save_profile(item.clone(), cx)) + .await + .unwrap(); + } + let group_of = |id, cx: &mut gpui::TestAppContext| { + entity.read_with(cx, |e, cx| e.profiles(cx).get(id).unwrap().group.clone()) + }; + entity + .update(cx, |e, cx| { + e.rename_group_of(vec![mine.id], "Work".into(), " Team ".into(), cx) + }) + .await + .unwrap(); + assert_eq!(group_of(mine.id, cx), Some("Team".into())); + assert_eq!(group_of(theirs.id, cx), Some("Work".into())); + + entity + .update(cx, |e, cx| e.ungroup_of(vec![mine.id], "Team".into(), cx)) + .await + .unwrap(); + assert_eq!(group_of(mine.id, cx), None); + assert_eq!(group_of(theirs.id, cx), Some("Work".into())); + + entity + .update(cx, |e, cx| { + e.save_profile( + Profile { + group: Some("Work".into()), + ..mine.clone() + }, + cx, + ) + }) + .await + .unwrap(); + entity + .update(cx, |e, cx| { + e.delete_group_of("Work".into(), vec![mine.id], cx) + }) + .await + .unwrap(); + entity.read_with(cx, |e, cx| { + assert!(e.profiles(cx).get(mine.id).is_none()); + assert!(e.profiles(cx).get(theirs.id).is_some()); + }); +} + +#[gpui::test] +async fn deleting_listed_members_is_refused_when_one_left_the_folder( + cx: &mut gpui::TestAppContext, +) { + let mut a = profile("a"); + a.group = Some("Work".into()); + let entity = cx.new(|_| Connections::in_memory()); + entity + .update(cx, |e, cx| e.save_profile(a.clone(), cx)) + .await + .unwrap(); + entity + .update(cx, |e, cx| e.move_profile(a.id, None, cx)) + .await + .unwrap(); + entity + .update(cx, |e, cx| e.delete_group_of("Work".into(), vec![a.id], cx)) + .await + .unwrap_err(); + assert!(entity.read_with(cx, |e, cx| e.profiles(cx).get(a.id).is_some())); +} diff --git a/crates/connections-store/src/facts.rs b/crates/connections-store/src/facts.rs index 169d9aeb..b423ff2a 100644 --- a/crates/connections-store/src/facts.rs +++ b/crates/connections-store/src/facts.rs @@ -91,6 +91,7 @@ pub struct FactsService { probes: admission::ProbeLedger, locations: location::LocationAdmission, flags: flags::RawFlags, + flag_retry: Option<(std::time::Instant, gpui::Task<()>)>, jobs: jobs::ObservationJobs, policy: policy::DetectionPolicy, } @@ -347,6 +348,7 @@ impl FactsService { if self.closing(cx) { return; } + self.flags.retry_due(cx.background_executor().now()); while let Some(code) = self.flags.pending() { let Some(job) = self.jobs.reserve_flag() else { break; @@ -363,7 +365,33 @@ impl FactsService { }) .detach(); } + self.schedule_flag_retry(cx); } + + fn schedule_flag_retry(&mut self, cx: &mut Context) { + let deadline = self.flags.retry_deadline(); + if self.flag_retry.as_ref().map(|(deadline, _)| *deadline) == deadline { + return; + } + self.flag_retry = deadline.map(|deadline| { + let timer = cx + .background_executor() + .timer(deadline.saturating_duration_since(cx.background_executor().now())); + ( + deadline, + cx.spawn(async move |this, cx| { + timer.await; + this.update(cx, |this, cx| { + this.flag_retry = None; + this.drain_flags(cx); + cx.notify(); + }) + .ok(); + }), + ) + }); + } + fn finish_flag( &mut self, code: CountryCode, @@ -376,7 +404,8 @@ impl FactsService { if !released || self.closing(cx) { return; } - self.flags.finish(code, &job, result); + self.flags + .finish(code, &job, result, cx.background_executor().now()); self.progress(cx); } fn progress(&mut self, cx: &mut Context) { diff --git a/crates/connections-store/src/facts/flags.rs b/crates/connections-store/src/facts/flags.rs index 3c9b419f..3a44e875 100644 --- a/crates/connections-store/src/facts/flags.rs +++ b/crates/connections-store/src/facts/flags.rs @@ -1,6 +1,10 @@ use super::tickets::JobKey; use nocterm_connections_model::observation::{CountryCode, FlagBytes, ObservationError}; -use std::{collections::HashMap, sync::Arc}; +use std::{ + collections::HashMap, + sync::Arc, + time::{Duration, Instant}, +}; #[derive(Clone, Debug)] pub enum FlagState { Requested, @@ -11,6 +15,8 @@ pub enum FlagState { struct FlagEntry { state: FlagState, job: Option, + attempts: u8, + retry_at: Option, } pub(super) struct RawFlags { entries: HashMap, @@ -29,6 +35,8 @@ impl RawFlags { FlagState::Unavailable }, job: None, + attempts: 0, + retry_at: None, }); } pub(super) fn pending(&self) -> Option { @@ -38,6 +46,8 @@ impl RawFlags { } pub(super) fn loading(&mut self, code: CountryCode, job: JobKey) { if let Some(entry) = self.entries.get_mut(&code) { + entry.attempts += 1; + entry.retry_at = None; entry.state = FlagState::Loading; entry.job = Some(job); } @@ -47,6 +57,7 @@ impl RawFlags { code: CountryCode, job: &JobKey, result: Result>, ObservationError>, + now: Instant, ) -> bool { let Some(entry) = self.entries.get_mut(&code) else { return false; @@ -70,12 +81,41 @@ impl RawFlags { Ok(None) => FlagState::Unavailable, Err(error) => { tracing::debug!(%error,"flag acquisition failed"); + if entry.attempts < 3 && retryable(&error) { + entry.retry_at = + Some(now + Duration::from_secs(if entry.attempts == 1 { 5 } else { 30 })); + } FlagState::Unavailable } }; true } + pub(super) fn retry_due(&mut self, now: Instant) { + for entry in self.entries.values_mut() { + if entry.retry_at.is_some_and(|deadline| deadline <= now) { + entry.retry_at = None; + entry.state = FlagState::Requested; + } + } + } + pub(super) fn retry_deadline(&self) -> Option { + self.entries + .values() + .filter_map(|entry| entry.retry_at) + .min() + } pub(super) fn state(&self, code: CountryCode) -> Option<&FlagState> { self.entries.get(&code).map(|entry| &entry.state) } } + +fn retryable(error: &ObservationError) -> bool { + match error { + ObservationError::Network(_) + | ObservationError::Resolution(_) + | ObservationError::Body(_) + | ObservationError::Cache(_) => true, + ObservationError::Status { status, .. } => *status >= 500 || matches!(*status, 408 | 429), + ObservationError::BodyLimit { .. } | ObservationError::Decode(_) => false, + } +} diff --git a/crates/connections-store/src/facts/prepared.rs b/crates/connections-store/src/facts/prepared.rs index 33c1cfe7..71639d10 100644 --- a/crates/connections-store/src/facts/prepared.rs +++ b/crates/connections-store/src/facts/prepared.rs @@ -55,6 +55,7 @@ impl FactsPrepared { probes: admission::ProbeLedger::new(), locations, flags: flags::RawFlags::new(), + flag_retry: None, jobs: jobs::ObservationJobs::new(executor), policy, }); diff --git a/crates/connections-store/src/facts/source.rs b/crates/connections-store/src/facts/source.rs index f60d116b..d151cd53 100644 --- a/crates/connections-store/src/facts/source.rs +++ b/crates/connections-store/src/facts/source.rs @@ -37,6 +37,10 @@ impl ProbeSourceLease { pub(super) fn identity(&self) -> ProbeSourceIdentity { self.identity } + /// Equality of the captured lease lineage, independent of descriptive host/profile ids. + pub fn same_origin(&self, other: &Self) -> bool { + Rc::ptr_eq(&self.revoked, &other.revoked) + } pub fn revoke(&self) { self.revoked.set(true); } diff --git a/crates/connections-store/src/facts/tests/flags.rs b/crates/connections-store/src/facts/tests/flags.rs index 5e09076b..2aba3b16 100644 --- a/crates/connections-store/src/facts/tests/flags.rs +++ b/crates/connections-store/src/facts/tests/flags.rs @@ -96,3 +96,121 @@ fn manual_saved_countries_start_without_a_known_row_or_live_session(cx: &mut Tes ); assert_eq!(fixture.os.calls(), 0); } + +fn transient_flag_failure() -> nocterm_connections_model::observation::ObservationError { + nocterm_connections_model::observation::ObservationError::Network(Box::new( + std::io::Error::from(std::io::ErrorKind::NotConnected), + )) +} + +#[gpui::test] +fn transient_flag_failure_retries_after_cooldown_and_publishes_the_new_source( + cx: &mut TestAppContext, +) { + let fixture = Fixture::new( + profile(), + Default::default(), + true, + threaded(cx), + ready_writer(), + cx, + ); + let code = CountryCode::new("de").unwrap(); + fixture.script.flag_next(Err(transient_flag_failure())); + fixture.script.flag_next(Ok(Some(b"".to_vec()))); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Unavailable)) + }) + }); + for _ in 0..20 { + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + } + assert_eq!(fixture.script.calls().len(), 1); + cx.executor().advance_clock(Duration::from_secs(5)); + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Ready(_))) + }) + }); + assert_eq!(fixture.script.calls().len(), 2); +} + +#[gpui::test] +fn an_offline_flag_has_at_most_three_automatic_attempts(cx: &mut TestAppContext) { + let fixture = Fixture::new( + profile(), + Default::default(), + true, + threaded(cx), + ready_writer(), + cx, + ); + let code = CountryCode::new("de").unwrap(); + for _ in 0..3 { + fixture.script.flag_next(Err(transient_flag_failure())); + } + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + for attempt in 1..=3 { + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Unavailable)) + }) && fixture.script.calls().len() == attempt + }); + cx.executor().advance_clock(Duration::from_secs(31)); + cx.run_until_parked(); + } + assert_eq!(fixture.script.calls().len(), 3); + cx.executor().advance_clock(Duration::from_secs(300)); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + cx.run_until_parked(); + assert_eq!(fixture.script.calls().len(), 3); +} + +#[gpui::test] +fn a_missing_flag_is_permanently_negative_without_network_retries(cx: &mut TestAppContext) { + let fixture = Fixture::new( + profile(), + Default::default(), + true, + threaded(cx), + ready_writer(), + cx, + ); + let code = CountryCode::new("xx").unwrap(); + fixture.script.flag_next(Err( + nocterm_connections_model::observation::ObservationError::Status { + status: 404, + source: Box::new(std::io::Error::from(std::io::ErrorKind::NotFound)), + }, + )); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + until(cx, |cx| { + fixture.facts.read_with(cx, |facts, _| { + matches!(facts.flag(code), Some(FlagState::Unavailable)) + }) + }); + cx.executor().advance_clock(Duration::from_secs(300)); + fixture + .facts + .update(cx, |facts, cx| facts.request_flag(code, cx)) + .unwrap(); + cx.run_until_parked(); + assert_eq!(fixture.script.calls().len(), 1); +} diff --git a/crates/connections-store/src/lib.rs b/crates/connections-store/src/lib.rs index 38ce58f8..2178f52e 100644 --- a/crates/connections-store/src/lib.rs +++ b/crates/connections-store/src/lib.rs @@ -1,27 +1,38 @@ //! Ordered connection publication and bounded raw server-fact acquisition. //! Owns: checked profiles/recents, committed publication carrier, raw Known/flag -//! state, original-source tickets, policy history, physical reservations and capture. +//! state, original-source tickets, policy history, physical reservations and capture, +//! and the synchronization of profiles with a replica (ProfileReplication), including the rollback of a refused change. //! Does not own: native I/O, views/images, session providers, settings storage, //! credentials, terminal implementation or observation composition. //! Ports used: supplied writers/clock, DNS/country/flag/OS observation contracts, -//! CountryPolicySource, ProbeSourceAuthority, BackgroundExecutor, ShutdownCoordinator. -//! Ports provided: ConnectionStore, ProfilePublications, FactsService. +//! CountryPolicySource, ProbeSourceAuthority, BackgroundExecutor, ShutdownCoordinator, +//! ProfileReplica (supplied by an adapter) and a supplied Persisted replica state. +//! Ports provided: ConnectionStore, ProfilePublications, FactsService, ProfileReplication. //! Settings: supplied enabled/revision policy projection. Globals: none. //! Invariants: committed carrier and current original source/policy are read at //! admission/completion; stale jobs keep their physical slot until actual finish; //! 32 slots are shared across windows and paired reservations are atomic. Facts //! use common optimistic persistence/capture and its existing 180-ms deadline. +//! Replication writes local profiles only through ConnectionStore, never echoes an +//! applied remote change, leaves a profile the replica holds unreadably untouched, +//! reconciles nothing while the local document failed to load and repeats a pass +//! that met a concurrent change at most three times. //! Read-only diagnostics preserve their original allocation and concrete sources through core SharedError. mod connections; mod error; mod prepared; mod publication; +mod replication; pub use connections::ConnectionStore; pub use error::ConnectionsError; pub use nocterm_connections_model::profile_authority::{ ProfileAuthorityRow, ProfilePublication, ProfileStamp, }; pub use publication::projection::ProfilePublications; +pub use replication::{ + ProfileReplication, ReplicationEvent, ReplicationFailure, ReplicationFeedError, + ReplicationStatus, +}; mod facts; mod observation; @@ -31,3 +42,6 @@ pub use facts::{ ProbeSourceAuthority, ProbeSourceIdentity, ProbeSourceLease, }; pub use observation::ObservationDeps; + +#[cfg(feature = "test-support")] +pub mod test_support; diff --git a/crates/connections-store/src/replication.rs b/crates/connections-store/src/replication.rs new file mode 100644 index 00000000..0137dce6 --- /dev/null +++ b/crates/connections-store/src/replication.rs @@ -0,0 +1,240 @@ +//! Keeps the saved profiles and a remote replica of them in agreement. +//! +//! The store stays the one the application reads and writes through +//! [`ConnectionStore`]; for a hub account it is an in-memory mirror that [`ProfileReplication::detach`] +//! empties. This entity watches its committed publications, reads the +//! replica's changes, reconciles the three sides with the pure rules of +//! `connections-model` and applies the result: remote changes through the store's +//! checked writes, local ones through the replica port. What the two sides last +//! agreed on is a [`ReplicaState`] document persisted through `nocterm_store`, so a +//! restart continues where it stopped. +mod feed; +mod round; +pub use feed::ReplicationFeedError; +mod status; + +use crate::ConnectionStore; +use futures::StreamExt as _; +use gpui::{Context, Entity, EventEmitter, Subscription, Task}; +use nocterm_connections_model::{ + profiles::ProfileId, + replica::{ProfileReplica, ReplicaState}, +}; +use nocterm_store::{Persisted, Publication, SnapshotWriter}; +use round::{Outcome, Round}; +use std::{collections::HashSet, sync::Arc}; + +pub use status::{ReplicationEvent, ReplicationFailure, ReplicationStatus}; + +/// Pending writes of the agreed-state document; its edits are coalesced snapshots. +const STATE_WRITES: usize = 8; + +/// A standing refusal that has been reported. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +enum Announced { + /// The local document refused a remote change. + Skipped(ProfileId), + /// The replica refused a local profile that has no agreed version to go back to. + Refused(ProfileId), +} + +pub struct ProfileReplication { + store: Entity, + state: Entity>, + replica: Option>, + status: ReplicationStatus, + run: Option>, + rerun: bool, + watcher: Option>, + /// Refusals already reported and still standing: a remote change the local document + /// refused (the cursor stays behind it) and a local profile the replica refused and + /// never agreed to. Each pass meets them again but says so once. + refused: HashSet, + _publications: Subscription, +} + +impl EventEmitter for ProfileReplication {} + +impl ProfileReplication { + /// `state` is the document built by [`Self::state_document`] and registered + /// with the shutdown coordinator by the application. + pub fn new( + store: Entity, + state: Entity>, + cx: &mut Context, + ) -> Self { + let publications = store.update(cx, |store, cx| store.publications(cx)); + let subscription = cx.observe(&publications, |this, _, cx| this.request_sync(cx)); + Self { + store, + state, + replica: None, + status: ReplicationStatus::Detached, + run: None, + rerun: false, + watcher: None, + refused: HashSet::new(), + _publications: subscription, + } + } + + /// The agreed-state document. It publishes optimistically: the replication reads + /// its own latest edits back while the disk catches up. + pub fn state_document( + initial: ReplicaState, + writer: SnapshotWriter, + ) -> Persisted { + Persisted::new(initial, writer, Publication::Optimistic, STATE_WRITES) + } + + /// Starts synchronizing with `replica`, replacing a previously attached one. + /// The agreed state is kept: a restart continues with the same replica. + pub fn attach(&mut self, replica: Arc, cx: &mut Context) { + self.stop(cx); + let mut changes = replica.watch(); + self.watcher = Some(cx.spawn(async move |this, cx| { + while changes.next().await.is_some() { + if this.update(cx, |this, cx| this.request_sync(cx)).is_err() { + break; + } + } + })); + self.replica = Some(replica); + self.start(cx); + } + + /// Stops synchronizing, forgets what was agreed with that replica and empties the store: + /// the store is the mirror of one account, so nothing in it may be offered to another + /// account's replica or shown after the account left. The agreement and the feed position + /// mean nothing to another replica either. + pub fn detach(&mut self, cx: &mut Context) { + self.stop(cx); + self.clear_mirror(cx); + self.state.update(cx, |state, cx| { + if let Err(error) = state.update_latest(|state| *state = ReplicaState::default(), cx) { + tracing::error!(%error, "could not reset the profile replica state"); + } + }); + self.set_status(ReplicationStatus::Detached, cx); + } + + /// Removes every profile from the store. With no replica attached nothing is sent. + fn clear_mirror(&mut self, cx: &mut Context) { + let removals: Vec<_> = self.store.update(cx, |store, cx| { + let ids: Vec = store + .profiles(cx) + .iter() + .map(|profile| profile.id) + .collect(); + ids.into_iter() + .map(|id| store.delete_profile(id, cx)) + .collect() + }); + cx.spawn(async move |_, _| { + for removal in removals { + if let Err(error) = removal.await { + tracing::error!(%error, "could not clear a profile of the mirror"); + } + } + }) + .detach(); + } + + pub fn status(&self) -> &ReplicationStatus { + &self.status + } + + fn stop(&mut self, cx: &mut Context) { + self.replica = None; + self.watcher = None; + self.run = None; + self.rerun = false; + cx.notify(); + } + + fn request_sync(&mut self, cx: &mut Context) { + if self.replica.is_none() { + return; + } + if self.run.is_some() { + self.rerun = true; + } else { + self.start(cx); + } + } + + fn start(&mut self, cx: &mut Context) { + let Some(replica) = self.replica.clone() else { + return; + }; + let round = Round::new( + replica, + self.store.clone(), + self.state.clone(), + cx.weak_entity(), + ); + self.set_status(ReplicationStatus::Syncing, cx); + self.run = Some(cx.spawn(async move |this, cx| { + let outcome = round.run(cx).await; + if let Some(outcome) = outcome { + this.update(cx, |this, cx| this.finish(outcome, cx)).ok(); + } + })); + } + + /// Reports a profile the pass skipped. A refused remote change is read again by + /// every pass until it can be applied, but is announced only the first time. + fn announce_skip( + &mut self, + id: ProfileId, + failure: ReplicationFailure, + cx: &mut Context, + ) { + let first = !matches!(failure, ReplicationFailure::Rejected { .. }) + || self.refused.insert(Announced::Skipped(id)); + if first { + cx.emit(ReplicationEvent::Failed(failure)); + } + } + + /// Reports a refusal of the replica. One that restored the agreed version is news + /// once by itself; one that left a never-agreed profile local would repeat with + /// every pass, so it is announced only the first time. + fn announce_refusal(&mut self, id: ProfileId, event: ReplicationEvent, cx: &mut Context) { + let restored = matches!(event, ReplicationEvent::Refused { restored: true, .. }); + if restored || self.refused.insert(Announced::Refused(id)) { + cx.emit(event); + } + } + + /// A pass left nothing refused behind it: later refusals are news again. + fn forget_refusals(&mut self) { + self.refused.clear(); + } + + fn finish(&mut self, outcome: Outcome, cx: &mut Context) { + self.run = None; + let status = match outcome { + Outcome::Synced => ReplicationStatus::Synced, + Outcome::NeedsNewerClient => ReplicationStatus::NeedsNewerClient, + Outcome::Offline => ReplicationStatus::Offline, + Outcome::Locked => ReplicationStatus::Locked, + Outcome::Failed(failure) => { + cx.emit(ReplicationEvent::Failed(failure.clone())); + ReplicationStatus::Failed(failure) + } + }; + self.set_status(status, cx); + if std::mem::take(&mut self.rerun) { + self.start(cx); + } + } + + fn set_status(&mut self, status: ReplicationStatus, cx: &mut Context) { + self.status = status; + cx.notify(); + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/connections-store/src/replication/feed.rs b/crates/connections-store/src/replication/feed.rs new file mode 100644 index 00000000..77b27650 --- /dev/null +++ b/crates/connections-store/src/replication/feed.rs @@ -0,0 +1,145 @@ +//! Bounded acquisition of one complete feed, before any reconciliation effects. +use nocterm_connections_model::{ + profiles::Profile, + replica::{ + ProfileReplica, RemoteBody, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaPage, + }, +}; +use nocterm_core::{SharedError, bounded_io::CountingWriter}; +use std::{collections::HashSet, io, sync::Arc}; + +const MAX_PAGES: usize = 256; +const MAX_CHANGES: usize = 16_384; +const MAX_BYTES: usize = 16 * 1024 * 1024; +const MAX_CURSOR_BYTES: usize = 4096; + +#[derive(Clone, Debug, thiserror::Error)] +pub enum ReplicationFeedError { + #[error("The profile feed exceeds {MAX_PAGES} pages; nothing was synchronized.")] + Pages, + #[error("The profile feed exceeds {MAX_CHANGES} changes; nothing was synchronized.")] + Changes, + #[error("The profile feed exceeds its 16 MiB payload budget; nothing was synchronized.")] + Bytes, + #[error("The profile feed cursor exceeds {MAX_CURSOR_BYTES} bytes.")] + CursorSize, + #[error("The profile feed cursor did not advance.")] + NonProgress, + #[error("The profile feed repeated an earlier cursor.")] + Cycle, + #[error("The profile feed did not finish within two minutes.")] + TimedOut, + #[error("The profile feed could not be encoded for size validation: {0}")] + Encoding(#[source] SharedError), +} +pub(super) enum FetchError { + Remote(ReplicaError), + Invalid(ReplicationFeedError), +} +impl From for FetchError { + fn from(error: ReplicationFeedError) -> Self { + Self::Invalid(error) + } +} + +struct Feed { + changes: Vec, + cursor: Option, + seen: HashSet, + pages: usize, + bytes: usize, +} +impl Feed { + fn new(cursor: Option) -> Result { + let mut seen = HashSet::new(); + if let Some(cursor) = &cursor { + check_cursor(cursor)?; + seen.insert(cursor.as_str().to_owned()); + } + Ok(Self { + changes: Vec::new(), + cursor, + seen, + pages: 0, + bytes: 0, + }) + } + fn accept(&mut self, page: ReplicaPage) -> Result { + check_cursor(&page.cursor)?; + if page.changes.len() > MAX_CHANGES.saturating_sub(self.changes.len()) { + return Err(ReplicationFeedError::Changes); + } + let unchanged = self.cursor.as_ref() == Some(&page.cursor); + if unchanged && (page.more || !page.changes.is_empty()) { + return Err(ReplicationFeedError::NonProgress); + } + if !unchanged && !self.seen.insert(page.cursor.as_str().to_owned()) { + return Err(ReplicationFeedError::Cycle); + } + for change in &page.changes { + self.charge(std::mem::size_of::())?; + if let RemoteBody::Present(profile) = &change.body { + self.charge(std::mem::size_of::())?; + let bytes = profile_bytes(profile, MAX_BYTES - self.bytes)?; + self.charge(bytes)?; + } + } + self.pages += 1; + self.changes.extend(page.changes); + self.cursor = Some(page.cursor); + Ok(page.more) + } + fn charge(&mut self, bytes: usize) -> Result<(), ReplicationFeedError> { + self.bytes = self + .bytes + .checked_add(bytes) + .filter(|bytes| *bytes <= MAX_BYTES) + .ok_or(ReplicationFeedError::Bytes)?; + Ok(()) + } +} +fn check_cursor(cursor: &ReplicaCursor) -> Result<(), ReplicationFeedError> { + if cursor.as_str().len() > MAX_CURSOR_BYTES { + Err(ReplicationFeedError::CursorSize) + } else { + Ok(()) + } +} + +/// Runs on the supplied background executor. A failing/partial feed is discarded; +/// it never becomes a partial plan or an advanced durable cursor. +pub(super) async fn fetch( + replica: Arc, + cursor: Option, +) -> Result<(Vec, ReplicaCursor), FetchError> { + let mut feed = Feed::new(cursor)?; + loop { + if feed.pages == MAX_PAGES { + return Err(ReplicationFeedError::Pages.into()); + } + let page = replica + .changes(feed.cursor.clone()) + .await + .map_err(FetchError::Remote)?; + if !feed.accept(page)? { + break; + } + } + Ok(( + feed.changes, + feed.cursor.unwrap_or_else(|| ReplicaCursor::new("")), + )) +} + +fn profile_bytes(profile: &Profile, limit: usize) -> Result { + let mut writer = CountingWriter::new(limit); + match serde_json::to_writer(&mut writer, profile) { + Ok(()) => Ok(writer.count()), + Err(error) if error.io_error_kind() == Some(io::ErrorKind::OutOfMemory) => { + Err(ReplicationFeedError::Bytes) + } + Err(error) => Err(ReplicationFeedError::Encoding(SharedError::from_error( + error, + ))), + } +} diff --git a/crates/connections-store/src/replication/round.rs b/crates/connections-store/src/replication/round.rs new file mode 100644 index 00000000..7ebc1fdc --- /dev/null +++ b/crates/connections-store/src/replication/round.rs @@ -0,0 +1,425 @@ +//! One synchronization pass: read remote changes, reconcile, apply, advance the cursor. +//! +//! Every action is persisted in the agreed state as soon as it succeeds, and the +//! cursor moves only after a pass in which nothing was left undone: not after a +//! conflict, and not after a remote change the local document refused. A pass that +//! is interrupted, retried or refused re-reads changes it already handled; those +//! reconcile to nothing because the agreed state already holds them, while a refused +//! change is offered again instead of being lost. +use super::{ProfileReplication, ReplicationEvent, ReplicationFailure}; +use crate::{ConnectionStore, ConnectionsError}; +use gpui::{AsyncApp, Entity, WeakEntity}; +use nocterm_connections_model::{ + profiles::{Profile, ProfileError, ProfileId, Profiles}, + replica::{ + ProfileReplica, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaState, ReplicaVersion, + SyncedEntry, + rules::{self, ReplicaAction}, + }, +}; +use nocterm_store::Persisted; +use std::{collections::HashSet, sync::Arc}; + +/// Passes tried before a profile that keeps changing under us is reported. +pub(super) const MAX_ATTEMPTS: usize = 3; + +pub(super) enum Outcome { + Synced, + NeedsNewerClient, + Offline, + Locked, + Failed(ReplicationFailure), +} + +/// What applying the planned actions left behind. +enum Applied { + Done, + /// Nothing is left to retry, but the replica refused a profile that stays local. + Refused, + /// A conflict left something undone: plan again from the same cursor. + Retry, + /// A remote change could not be written locally: it stays ahead of the cursor. + Deferred, +} + +/// The pass cannot continue. +enum Stop { + Halt(Outcome), + /// The application or the owning entity is gone. + Gone, +} + +/// Why one action did not finish. +enum Flow { + /// One side changed under the action; read and reconcile again. + Conflict, + /// This profile cannot be synchronized; the others still can. + Skip(ReplicationFailure), + /// The replica refused the profile and the refusal was already dealt with and + /// reported; leave the rest of its actions undone. + Refused, + Stop(Stop), +} + +impl From for Flow { + fn from(stop: Stop) -> Self { + Self::Stop(stop) + } +} + +fn gone(_: E) -> Stop { + Stop::Gone +} + +fn halt(failure: ReplicationFailure) -> Stop { + Stop::Halt(Outcome::Failed(failure)) +} + +fn remote_stop(error: ReplicaError) -> Stop { + match error { + ReplicaError::Unavailable => Stop::Halt(Outcome::Offline), + ReplicaError::Locked | ReplicaError::Stale => Stop::Halt(Outcome::Locked), + error => halt(error.into()), + } +} + +fn remote_flow(error: ReplicaError) -> Flow { + match error { + ReplicaError::Conflict { .. } => Flow::Conflict, + error => remote_stop(error).into(), + } +} + +fn local_flow(id: ProfileId, error: ConnectionsError) -> Flow { + match &error { + ConnectionsError::Profile(ProfileError::Conflict(_) | ProfileError::MissingProfile(_)) => { + Flow::Conflict + } + ConnectionsError::Profile(_) => { + Flow::Skip(ReplicationFailure::Rejected { id, source: error }) + } + _ => halt(ReplicationFailure::Local(error)).into(), + } +} + +/// The action brings a remote change into the local document. +fn writes_remote_change(action: &ReplicaAction) -> bool { + matches!( + action, + ReplicaAction::SaveLocal { .. } | ReplicaAction::DeleteLocal { .. } + ) +} + +pub(super) struct Round { + replica: Arc, + store: Entity, + state: Entity>, + events: WeakEntity, +} + +impl Round { + pub(super) fn new( + replica: Arc, + store: Entity, + state: Entity>, + events: WeakEntity, + ) -> Self { + Self { + replica, + store, + state, + events, + } + } + + /// `None` when the application went away before the pass finished. + pub(super) async fn run(self, cx: &mut AsyncApp) -> Option { + for _ in 0..MAX_ATTEMPTS { + match self.attempt(cx).await { + Ok(true) => {} + Ok(false) => return Some(self.finished(cx)), + Err(Stop::Halt(outcome)) => return Some(outcome), + Err(Stop::Gone) => return None, + } + } + Some(Outcome::Failed(ReplicationFailure::Conflicts)) + } + + fn finished(&self, cx: &mut AsyncApp) -> Outcome { + let unreadable = self + .state + .read_with(cx, |state, _| state.visible().has_unrecognized()); + if unreadable { + Outcome::NeedsNewerClient + } else { + Outcome::Synced + } + } + + /// Whether the pass must be repeated. + async fn attempt(&self, cx: &mut AsyncApp) -> Result { + self.require_readable_local(cx)?; + let (changes, cursor) = self.fetch(cx).await?; + let actions = self.plan(&changes, cx)?; + match self.apply(actions, cx).await? { + Applied::Retry => return Ok(true), + Applied::Refused => self.record(cx, move |state| state.advance(cursor))?, + Applied::Done => { + self.record(cx, move |state| state.advance(cursor))?; + self.events + .update(cx, |this, _| this.forget_refusals()) + .map_err(gone)?; + } + Applied::Deferred => {} + } + Ok(false) + } + + fn require_readable_local(&self, cx: &mut AsyncApp) -> Result<(), Stop> { + let unreadable = self + .store + .read_with(cx, |store, _| store.load_error().is_some()); + if unreadable { + return Err(halt(ReplicationFailure::LocalUnavailable)); + } + Ok(()) + } + + async fn fetch(&self, cx: &mut AsyncApp) -> Result<(Vec, ReplicaCursor), Stop> { + let after = self + .state + .read_with(cx, |state, _| state.visible().cursor().cloned()); + let replica = self.replica.clone(); + let executor = cx.background_executor().clone(); + let acquisition = executor.spawn(async move { super::feed::fetch(replica, after).await }); + let deadline = executor.timer(std::time::Duration::from_secs(120)); + futures::pin_mut!(acquisition, deadline); + match futures::future::select(acquisition, deadline).await { + futures::future::Either::Left((result, _)) => result.map_err(|error| match error { + super::feed::FetchError::Remote(error) => remote_stop(error), + super::feed::FetchError::Invalid(error) => halt(error.into()), + }), + futures::future::Either::Right(_) => { + Err(halt(super::ReplicationFeedError::TimedOut.into())) + } + } + } + + fn plan( + &self, + changes: &[RemoteProfile], + cx: &mut AsyncApp, + ) -> Result, Stop> { + let local: Profiles = self + .store + .read_with(cx, |store, cx| store.profiles(cx).clone()); + let state = self.state.read_with(cx, |state, _| state.visible().clone()); + Ok(rules::plan(&local, changes, &state)) + } + + async fn apply(&self, actions: Vec, cx: &mut AsyncApp) -> Result { + let mut skipped = HashSet::new(); + let mut applied = Applied::Done; + let mut refused = false; + for action in actions { + let id = action.id(); + if skipped.contains(&id) { + continue; + } + let from_remote = writes_remote_change(&action); + match self.execute(action, cx).await { + Ok(()) => {} + Err(Flow::Conflict) => { + applied = Applied::Retry; + skipped.insert(id); + } + Err(Flow::Refused) => { + refused = true; + skipped.insert(id); + } + Err(Flow::Skip(failure)) => { + skipped.insert(id); + if from_remote && matches!(applied, Applied::Done) { + applied = Applied::Deferred; + } + self.skipped(id, failure, cx)?; + } + Err(Flow::Stop(stop)) => return Err(stop), + } + } + Ok(match applied { + Applied::Done if refused => Applied::Refused, + other => other, + }) + } + + async fn execute(&self, action: ReplicaAction, cx: &mut AsyncApp) -> Result<(), Flow> { + match action { + ReplicaAction::SaveLocal { + version, + profile, + expected, + } => { + self.save_local(version, *profile, expected.map(|p| *p), cx) + .await + } + ReplicaAction::DeleteLocal { id, expected } => { + self.delete_local(id, *expected, cx).await + } + ReplicaAction::Push { profile, expected } => self.push(*profile, expected, cx).await, + ReplicaAction::PushDelete { id, expected } => self.push_delete(id, expected, cx).await, + ReplicaAction::MarkSynced { version, profile } => { + Ok(self.agree(version, *profile, cx)?) + } + ReplicaAction::Forget { id } => Ok(self.record(cx, move |state| state.forget(id))?), + ReplicaAction::Overwritten { id, discarded } => { + Ok(self.emit(ReplicationEvent::Overwritten { id, discarded }, cx)?) + } + ReplicaAction::NeedsNewerClient { id } => { + Ok(self.record(cx, move |state| state.mark_unrecognized(id))?) + } + } + } + + async fn save_local( + &self, + version: ReplicaVersion, + profile: Profile, + expected: Option, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let id = profile.id; + let agreed = profile.clone(); + let saving = self.store.update(cx, |store, cx| { + store.save_profile_checked(profile, expected, cx) + }); + saving.await.map_err(|error| local_flow(id, error))?; + Ok(self.agree(version, agreed, cx)?) + } + + /// The comparison and the deletion happen in one synchronous step, so an edit + /// made after planning is not deleted unseen. + async fn delete_local( + &self, + id: ProfileId, + expected: Profile, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let deleting = self.store.update(cx, |store, cx| { + (store.profiles(cx).get(id) == Some(&expected)).then(|| store.delete_profile(id, cx)) + }); + let Some(deleting) = deleting else { + return Err(Flow::Conflict); + }; + deleting.await.map_err(|error| local_flow(id, error))?; + Ok(self.record(cx, move |state| state.forget(id))?) + } + + async fn push( + &self, + profile: Profile, + expected: Option, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let id = profile.id; + match self.replica.put(profile.clone(), expected).await { + Ok(version) => Ok(self.agree(version, profile, cx)?), + Err(ReplicaError::Forbidden { .. }) => self.refused(id, profile, cx).await, + Err(error) => Err(remote_flow(error)), + } + } + + async fn push_delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + match self.replica.delete(id, expected).await { + Ok(_) => Ok(self.record(cx, move |state| state.forget(id))?), + Err(ReplicaError::Forbidden { .. }) => { + let agreed = self + .state + .read_with(cx, |state, _| state.visible().entry(id).cloned()); + match agreed { + Some(entry) => self.refused(id, entry.profile, cx).await, + None => Err(Flow::Refused), + } + } + Err(error) => Err(remote_flow(error)), + } + } + + /// The account may not write `profile`. The user is told once, and the local profile + /// goes back to what was last agreed, so nothing is sent again. A profile never agreed + /// stays local. + async fn refused( + &self, + id: ProfileId, + profile: Profile, + cx: &mut AsyncApp, + ) -> Result<(), Flow> { + let agreed = self + .state + .read_with(cx, |state, _| state.visible().entry(id).cloned()); + let restored = agreed.is_some(); + if let Some(entry) = agreed { + let current = self + .store + .read_with(cx, |store, cx| store.profiles(cx).get(id).cloned()); + if current.as_ref() != Some(&entry.profile) { + let restoring = self.store.update(cx, |store, cx| { + store.save_profile_checked(entry.profile, current, cx) + }); + restoring.await.map_err(|error| local_flow(id, error))?; + } + } + let event = ReplicationEvent::Refused { + profile: Box::new(profile), + restored, + }; + self.events + .update(cx, |this, cx| this.announce_refusal(id, event, cx)) + .map_err(gone)?; + Err(Flow::Refused) + } + + /// The replica's version stands, whether this client sent it, wrote it here or both + /// already held it. + fn agree( + &self, + version: ReplicaVersion, + profile: Profile, + cx: &mut AsyncApp, + ) -> Result<(), Stop> { + let id = profile.id; + let entry = SyncedEntry { version, profile }; + self.record(cx, move |state| state.agree(id, entry)) + } + + fn record( + &self, + cx: &mut AsyncApp, + edit: impl FnOnce(&mut ReplicaState) + 'static, + ) -> Result<(), Stop> { + self.state + .update(cx, |state, cx| state.update_latest(edit, cx)) + .map(|_| ()) + .map_err(|error| halt(ReplicationFailure::State(error))) + } + + fn skipped( + &self, + id: ProfileId, + failure: ReplicationFailure, + cx: &mut AsyncApp, + ) -> Result<(), Stop> { + self.events + .update(cx, |this, cx| this.announce_skip(id, failure, cx)) + .map_err(gone) + } + + fn emit(&self, event: ReplicationEvent, cx: &mut AsyncApp) -> Result<(), Stop> { + self.events.update(cx, |_, cx| cx.emit(event)).map_err(gone) + } +} diff --git a/crates/connections-store/src/replication/status.rs b/crates/connections-store/src/replication/status.rs new file mode 100644 index 00000000..be50dfda --- /dev/null +++ b/crates/connections-store/src/replication/status.rs @@ -0,0 +1,71 @@ +//! What the replication reports to its observers. +use crate::ConnectionsError; +use nocterm_connections_model::{ + profiles::{Profile, ProfileId}, + replica::ReplicaError, +}; +use nocterm_store::StoreError; + +/// Where synchronization stands. +#[derive(Clone, Debug)] +pub enum ReplicationStatus { + /// No replica is attached; profiles stay local. + Detached, + Syncing, + /// Local and remote profiles agree. + Synced, + /// The replica could not be reached; local work continues and sync retries on the + /// next local change or remote notification. + Offline, + /// The keys that open the replica are closed; the server may be reachable. Local work + /// continues and sync resumes when the keys open and the replica notifies, or on the + /// next attach. + Locked, + /// The replica holds a profile this client cannot read; the local copy is untouched. + NeedsNewerClient, + Failed(ReplicationFailure), +} + +/// Why a synchronization could not finish, or why one profile was skipped. +#[derive(Clone, Debug, thiserror::Error)] +pub enum ReplicationFailure { + /// Saved connections failed to load, so an empty local copy would look like a + /// deletion of everything. + #[error("Saved connections could not be read, so nothing was synchronized.")] + LocalUnavailable, + /// A profile kept changing under the synchronization. + #[error("Profiles kept changing during synchronization.")] + Conflicts, + #[error(transparent)] + Feed(#[from] super::ReplicationFeedError), + #[error(transparent)] + Replica(#[from] ReplicaError), + /// The local document refused one profile; the others were synchronized. + #[error("Connection {id} could not be saved locally: {source}")] + Rejected { + id: ProfileId, + #[source] + source: ConnectionsError, + }, + #[error("Connections could not be updated: {0}")] + Local(#[source] ConnectionsError), + #[error("Synchronization state could not be kept: {0}")] + State(#[source] StoreError), +} + +#[derive(Clone, Debug)] +pub enum ReplicationEvent { + /// A remote version replaced this unsynchronized local edit. + Overwritten { + id: ProfileId, + discarded: Box, + }, + Failed(ReplicationFailure), + /// The replica refused to change `profile`, because the account may not. When + /// `restored`, the local profile is back to the last agreed version; otherwise it stays + /// local only and is not sent again until it changes. + Refused { + profile: Box, + restored: bool, + }, +} diff --git a/crates/connections-store/src/replication/tests.rs b/crates/connections-store/src/replication/tests.rs new file mode 100644 index 00000000..4afa2c01 --- /dev/null +++ b/crates/connections-store/src/replication/tests.rs @@ -0,0 +1,359 @@ +use super::*; +use gpui::AppContext as _; +use nocterm_connections_model::{ + profiles::{Profile, Profiles, Recents}, + replica::{RemoteBody, ReplicaCursor, ReplicaVersion, SyncedEntry}, + test_support::{MemoryReplica, ReplicaCall, profile_id_for}, +}; +use nocterm_session::{Auth, Target}; +use nocterm_store::test_support::{WriteLog, gated_writer}; +use std::sync::Mutex; + +mod conflicts; +mod feed; +mod lifecycle; + +fn profile(name: &str) -> Profile { + Profile { + id: profile_id_for(name), + name: name.into(), + description: String::new(), + options: Default::default(), + target: Target::new("ci", format!("{name}.example"), 22), + auth: Auth::Password, + credential: None, + launch: None, + icon: None, + icon_color: None, + country: None, + group: None, + jump_hosts: Vec::new(), + } +} + +fn at_host(profile: &Profile, host: &str) -> Profile { + let mut edited = profile.clone(); + edited.target.host = host.into(); + edited +} + +fn profiles_of(items: &[Profile]) -> Profiles { + let mut profiles = Profiles::default(); + for item in items { + profiles.upsert(item.clone()); + } + profiles +} + +/// A store and a replication over it, wired to one in-memory replica. +struct Fixture { + store: Entity, + replication: Entity, + replica: Arc, + events: Arc>>, + writes: WriteLog, +} + +impl Fixture { + fn new(cx: &mut gpui::TestAppContext, local: &[Profile]) -> Self { + Self::resumed( + cx, + local, + ReplicaState::default(), + Arc::new(MemoryReplica::new()), + ) + } + + /// A start with `state` already agreed, as after a restart. + fn resumed( + cx: &mut gpui::TestAppContext, + local: &[Profile], + state: ReplicaState, + replica: Arc, + ) -> Self { + let local = profiles_of(local); + let store = ConnectionStore::with_documents(local, Recents::default()); + Self::over(cx, store, state, replica) + } + + fn over( + cx: &mut gpui::TestAppContext, + store: ConnectionStore, + state: ReplicaState, + replica: Arc, + ) -> Self { + let store = cx.new(|_| store); + let (writer, release, writes) = gated_writer().into_parts(); + release.send(Ok(())).ok(); + let document = cx.new(|_| ProfileReplication::state_document(state, writer)); + let replication = { + let store = store.clone(); + cx.new(|cx| ProfileReplication::new(store, document, cx)) + }; + let events = Arc::new(Mutex::new(Vec::new())); + let seen = events.clone(); + cx.update(|cx| { + cx.subscribe(&replication, move |_, event: &ReplicationEvent, _| { + seen.lock().unwrap().push(event.clone()); + }) + .detach(); + }); + Self { + store, + replication, + replica, + events, + writes, + } + } + + fn attach(&self, cx: &mut gpui::TestAppContext) { + let replica = self.replica.clone(); + self.replication + .update(cx, |replication, cx| replication.attach(replica, cx)); + cx.run_until_parked(); + } + + async fn save(&self, cx: &mut gpui::TestAppContext, profile: Profile) { + self.store + .update(cx, |store, cx| store.save_profile(profile, cx)) + .await + .unwrap(); + cx.run_until_parked(); + } + + async fn delete(&self, cx: &mut gpui::TestAppContext, profile: &Profile) { + let id = profile.id; + self.store + .update(cx, |store, cx| store.delete_profile(id, cx)) + .await + .unwrap(); + cx.run_until_parked(); + } + + fn local(&self, cx: &gpui::TestAppContext, profile: &Profile) -> Option { + let id = profile.id; + self.store + .read_with(cx, |store, cx| store.profiles(cx).get(id).cloned()) + } + + fn status(&self, cx: &gpui::TestAppContext) -> ReplicationStatus { + self.replication + .read_with(cx, |replication, _| replication.status().clone()) + } + + fn events(&self) -> Vec { + self.events.lock().unwrap().clone() + } + + /// The last agreed state handed to the writer, as a restart would load it. + fn persisted_state(&self) -> ReplicaState { + self.writes + .lock() + .unwrap() + .last() + .cloned() + .unwrap_or_default() + } + + fn put_count(&self) -> usize { + self.replica + .writes() + .iter() + .filter(|call| matches!(call, ReplicaCall::Put { .. })) + .count() + } +} + +#[gpui::test] +async fn a_new_remote_profile_is_applied_locally_without_echoing_it_back( + cx: &mut gpui::TestAppContext, +) { + let fixture = Fixture::new(cx, &[]); + let remote = profile("remote"); + fixture.replica.write_remote(remote.clone()); + fixture.attach(cx); + assert_eq!(fixture.local(cx, &remote), Some(remote.clone())); + assert_eq!(fixture.replica.writes(), []); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); +} + +#[gpui::test] +async fn a_new_local_profile_is_sent_once_and_its_echo_is_ignored(cx: &mut gpui::TestAppContext) { + let local = profile("local"); + let fixture = Fixture::new(cx, std::slice::from_ref(&local)); + fixture.attach(cx); + assert_eq!( + fixture.replica.writes(), + [ReplicaCall::Put { + id: local.id, + expected: None + }] + ); + assert_eq!(fixture.replica.profile(local.id), Some(local.clone())); + let created = fixture.replica.stored(local.id).unwrap().0; + fixture.replica.write_remote(profile("other")); + cx.run_until_parked(); + assert_eq!(fixture.put_count(), 1); + assert_eq!( + fixture.persisted_state().entry(local.id).map(|e| e.version), + Some(created) + ); +} + +#[gpui::test] +async fn a_remote_edit_replaces_an_unchanged_local_profile(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let edited = at_host(&original, "moved.example"); + fixture.replica.write_remote(edited.clone()); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(edited)); + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.events().len(), 0); +} + +#[gpui::test] +async fn a_local_edit_is_sent_against_the_agreed_version(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let agreed = fixture.replica.stored(original.id).unwrap().0; + let edited = at_host(&original, "moved.example"); + fixture.save(cx, edited.clone()).await; + assert_eq!( + fixture.replica.writes().last(), + Some(&ReplicaCall::Put { + id: original.id, + expected: Some(agreed) + }) + ); + assert_eq!(fixture.replica.profile(original.id), Some(edited)); +} + +#[gpui::test] +async fn the_remote_wins_when_both_sides_changed_and_the_loss_is_reported( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + cx.run_until_parked(); + let local_edit = at_host(&original, "local.example"); + fixture.save(cx, local_edit.clone()).await; + let remote_edit = at_host(&original, "remote.example"); + fixture.replica.write_remote(remote_edit.clone()); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(remote_edit)); + let events = fixture.events(); + assert!(matches!( + events.as_slice(), + [ReplicationEvent::Overwritten { id, discarded }] + if *id == original.id && **discarded == local_edit + )); +} + +#[gpui::test] +async fn equal_edits_on_both_sides_are_agreed_without_a_write_or_an_event( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + let edited = at_host(&original, "same.example"); + fixture.save(cx, edited.clone()).await; + let version = fixture.replica.write_remote(edited); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.events().len(), 0); + assert_eq!( + fixture + .persisted_state() + .entry(original.id) + .map(|e| e.version), + Some(version) + ); +} + +#[gpui::test] +async fn a_local_deletion_leaves_a_tombstone_on_the_replica(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let agreed = fixture.replica.stored(original.id).unwrap().0; + fixture.delete(cx, &original).await; + assert_eq!( + fixture.replica.writes().last(), + Some(&ReplicaCall::Delete { + id: original.id, + expected: agreed + }) + ); + assert!(matches!( + fixture.replica.stored(original.id).map(|s| s.1), + Some(RemoteBody::Deleted) + )); + assert!(fixture.persisted_state().entry(original.id).is_none()); +} + +#[gpui::test] +async fn a_remote_tombstone_deletes_the_unchanged_local_profile(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.delete_remote(original.id); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), None); + assert_eq!(fixture.put_count(), 1); + assert!(fixture.persisted_state().entry(original.id).is_none()); +} + +#[gpui::test] +async fn a_remote_edit_restores_a_profile_deleted_locally_while_offline( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + fixture.delete(cx, &original).await; + let edited = at_host(&original, "remote.example"); + fixture.replica.write_remote(edited.clone()); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(edited)); + assert!( + fixture + .replica + .writes() + .iter() + .all(|call| !matches!(call, ReplicaCall::Delete { .. })) + ); +} + +#[gpui::test] +async fn a_local_edit_is_sent_over_a_remote_tombstone(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.set_offline(true); + let edited = at_host(&original, "local.example"); + fixture.save(cx, edited.clone()).await; + let tombstone = fixture.replica.delete_remote(original.id); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert_eq!( + fixture.replica.writes().last(), + Some(&ReplicaCall::Put { + id: original.id, + expected: Some(tombstone) + }) + ); + assert_eq!(fixture.replica.profile(original.id), Some(edited.clone())); + assert_eq!(fixture.local(cx, &original), Some(edited)); +} diff --git a/crates/connections-store/src/replication/tests/conflicts.rs b/crates/connections-store/src/replication/tests/conflicts.rs new file mode 100644 index 00000000..4e1f5855 --- /dev/null +++ b/crates/connections-store/src/replication/tests/conflicts.rs @@ -0,0 +1,258 @@ +use super::*; + +fn failed_with(status: ReplicationStatus) -> Option { + match status { + ReplicationStatus::Failed(failure) => Some(failure), + _ => None, + } +} + +#[gpui::test] +async fn a_write_that_lost_a_race_is_reconciled_against_the_change_it_missed( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let remote_edit = at_host(&original, "remote.example"); + fixture + .replica + .write_concurrently_before_next_write(remote_edit.clone()); + let local_edit = at_host(&original, "local.example"); + fixture.save(cx, local_edit.clone()).await; + assert_eq!(fixture.local(cx, &original), Some(remote_edit)); + assert_eq!(fixture.put_count(), 2); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Overwritten { discarded, .. }] if **discarded == local_edit + )); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); +} + +#[gpui::test] +async fn a_write_that_keeps_conflicting_is_reported_after_three_attempts( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.conflict_next_writes(10); + fixture.save(cx, at_host(&original, "local.example")).await; + assert_eq!(fixture.put_count(), 1 + 3); + let failure = failed_with(fixture.status(cx)); + assert!(matches!(failure, Some(ReplicationFailure::Conflicts))); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Failed(ReplicationFailure::Conflicts)] + )); +} + +#[gpui::test] +async fn an_unreadable_remote_profile_never_changes_the_local_one(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.write_unrecognized(original.id); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &original), Some(original.clone())); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::NeedsNewerClient + )); + let edited = at_host(&original, "local.example"); + fixture.save(cx, edited.clone()).await; + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.local(cx, &original), Some(edited)); +} + +#[gpui::test] +async fn an_unreadable_profile_stays_unreadable_across_a_restart_and_clears_when_readable( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.write_unrecognized(original.id); + cx.run_until_parked(); + let restarted = Fixture::resumed( + cx, + std::slice::from_ref(&original), + fixture.persisted_state(), + fixture.replica.clone(), + ); + restarted.attach(cx); + assert!(matches!( + restarted.status(cx), + ReplicationStatus::NeedsNewerClient + )); + let readable = at_host(&original, "newer.example"); + restarted.replica.write_remote(readable.clone()); + cx.run_until_parked(); + assert!(matches!(restarted.status(cx), ReplicationStatus::Synced)); + assert_eq!(restarted.local(cx, &original), Some(readable)); +} + +#[gpui::test] +async fn an_unreachable_replica_leaves_local_work_alone_and_catches_up_when_back( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.replica.set_offline(true); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Offline)); + let added = profile("b"); + fixture.save(cx, added.clone()).await; + assert_eq!(fixture.local(cx, &added), Some(added.clone())); + assert_eq!(fixture.replica.writes(), []); + fixture.replica.set_offline(false); + cx.run_until_parked(); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); + assert_eq!(fixture.replica.profile(original.id), Some(original)); + assert_eq!(fixture.replica.profile(added.id), Some(added)); + assert_eq!(fixture.events().len(), 0); +} + +#[gpui::test] +async fn a_locked_account_is_reported_apart_from_an_unreachable_server_and_resumes_when_opened( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.replica.set_locked(true); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Locked)); + let added = profile("b"); + fixture.save(cx, added.clone()).await; + assert_eq!(fixture.local(cx, &added), Some(added.clone())); + assert_eq!(fixture.replica.writes(), []); + assert!(matches!(fixture.status(cx), ReplicationStatus::Locked)); + fixture.replica.set_locked(false); + cx.run_until_parked(); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); + assert_eq!(fixture.replica.profile(original.id), Some(original)); + assert_eq!(fixture.replica.profile(added.id), Some(added)); + assert_eq!(fixture.events().len(), 0); +} + +#[gpui::test] +async fn attaching_again_after_the_keys_opened_synchronizes_a_locked_account( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.replica.set_locked(true); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Locked)); + fixture.replica.set_locked(false); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); + assert_eq!(fixture.replica.profile(original.id), Some(original)); +} + +#[gpui::test] +async fn a_forbidden_profile_is_reported_without_blocking_the_others( + cx: &mut gpui::TestAppContext, +) { + let (shared, own) = (profile("shared"), profile("own")); + let fixture = Fixture::new(cx, &[shared.clone(), own.clone()]); + fixture.replica.refuse_writes_of(shared.id); + fixture.attach(cx); + assert_eq!(fixture.replica.profile(own.id), Some(own)); + assert_eq!(fixture.replica.profile(shared.id), None); + assert_eq!(fixture.local(cx, &shared), Some(shared.clone())); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Refused { profile, restored: false }] + if profile.id == shared.id + )); +} + +#[gpui::test] +async fn a_remote_profile_the_local_document_refuses_is_reported_and_skipped( + cx: &mut gpui::TestAppContext, +) { + let fixture = Fixture::new(cx, &[]); + let mut oversized = profile("big"); + oversized.description = "x".repeat(70 * 1024); + let fine = profile("fine"); + fixture.replica.write_remote(oversized.clone()); + fixture.replica.write_remote(fine.clone()); + fixture.attach(cx); + assert_eq!(fixture.local(cx, &oversized), None); + assert_eq!(fixture.local(cx, &fine), Some(fine)); + assert!(matches!( + fixture.events().as_slice(), + [ReplicationEvent::Failed(ReplicationFailure::Rejected { id, .. })] if *id == oversized.id + )); +} + +/// A remote edit of `original` that the local document refuses for its size. +fn oversized_edit(original: &Profile) -> Profile { + let mut edited = original.clone(); + edited.description = "x".repeat(70 * 1024); + edited +} + +fn rejected_ids(events: &[ReplicationEvent]) -> Vec { + events + .iter() + .filter_map(|event| match event { + ReplicationEvent::Failed(ReplicationFailure::Rejected { id, .. }) => Some(*id), + _ => None, + }) + .collect() +} + +#[gpui::test] +async fn a_remote_change_the_local_document_refuses_keeps_the_cursor_behind_it_and_is_reported_once( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let before = fixture.persisted_state().cursor().cloned(); + assert!(before.is_some(), "the first pass recorded a position"); + fixture.replica.write_remote(oversized_edit(&original)); + cx.run_until_parked(); + assert_eq!(rejected_ids(&fixture.events()), [original.id]); + assert_eq!(fixture.persisted_state().cursor().cloned(), before); + let other = profile("b"); + fixture.replica.write_remote(other.clone()); + cx.run_until_parked(); + assert_eq!(fixture.local(cx, &other), Some(other)); + assert_eq!(fixture.local(cx, &original), Some(original.clone())); + assert_eq!( + rejected_ids(&fixture.events()), + [original.id], + "the refused change is read again by every pass but reported once" + ); + assert_eq!(fixture.persisted_state().cursor().cloned(), before); +} + +#[gpui::test] +async fn a_local_edit_after_a_refused_remote_change_does_not_end_in_conflicts( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.replica.write_remote(oversized_edit(&original)); + cx.run_until_parked(); + let puts = fixture.put_count(); + let edited = at_host(&original, "local.example"); + fixture.save(cx, edited.clone()).await; + assert_eq!(fixture.local(cx, &original), Some(edited)); + assert_eq!( + fixture.put_count(), + puts, + "nothing is sent over a newer version" + ); + assert!( + !matches!( + failed_with(fixture.status(cx)), + Some(ReplicationFailure::Conflicts) + ), + "the edit is not stuck retrying against the missed change" + ); +} diff --git a/crates/connections-store/src/replication/tests/feed.rs b/crates/connections-store/src/replication/tests/feed.rs new file mode 100644 index 00000000..103c6998 --- /dev/null +++ b/crates/connections-store/src/replication/tests/feed.rs @@ -0,0 +1,165 @@ +//! Malformed or excessive feeds never produce a partial plan or advance the cursor. +use super::*; +use nocterm_connections_model::replica::{RemoteProfile, ReplicaPage}; +use std::time::Duration; + +fn page(cursor: &str, profiles: Vec, more: bool) -> ReplicaPage { + ReplicaPage { + changes: profiles + .into_iter() + .map(|profile| RemoteProfile { + id: profile.id, + version: ReplicaVersion::new(1), + body: RemoteBody::Present(Box::new(profile)), + }) + .collect(), + cursor: ReplicaCursor::new(cursor), + more, + } +} +fn unchanged(fixture: &Fixture, candidate: &Profile, cx: &gpui::TestAppContext) { + assert!(fixture.local(cx, candidate).is_none()); + assert!(fixture.replica.writes().is_empty()); + assert!(fixture.persisted_state().cursor().is_none()); +} + +#[gpui::test] +fn a_feed_that_stalls_with_more_pages_is_rejected_before_any_profile_changes( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture.replica.script_pages(vec![ + page("first", vec![candidate.clone()], true), + page("first", vec![], true), + ]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::NonProgress)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn a_feed_cycle_is_refused_even_when_its_last_page_claims_completion( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture.replica.script_pages(vec![ + page("first", vec![candidate.clone()], true), + page("second", vec![], true), + page("first", vec![], false), + ]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Cycle)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn excessive_pages_stop_requesting_the_feed_without_advancing_the_cursor( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + let mut pages = vec![page("first", vec![candidate.clone()], true)]; + pages.extend((1..=256).map(|index| page(&format!("cursor-{index}"), vec![], true))); + fixture.replica.script_pages(pages); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Pages)) + )); + assert_eq!(fixture.replica.calls().len(), 256); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn excessive_change_counts_are_refused_before_planning(cx: &mut gpui::TestAppContext) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + let change = RemoteProfile { + id: candidate.id, + version: ReplicaVersion::new(1), + body: RemoteBody::Deleted, + }; + fixture.replica.script_pages(vec![ReplicaPage { + changes: vec![change; 16_385], + cursor: ReplicaCursor::new("end"), + more: false, + }]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Changes)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn the_payload_budget_applies_across_pages_before_any_remote_or_local_effect( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + let mut large = profile("large"); + large.description = "x".repeat(9 * 1024 * 1024); + let mut second = profile("second"); + second.description = "y".repeat(9 * 1024 * 1024); + fixture.replica.script_pages(vec![ + page("first", vec![candidate.clone(), large], true), + page("second", vec![second], false), + ]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::Bytes)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn an_oversized_cursor_never_becomes_the_saved_replication_position(cx: &mut gpui::TestAppContext) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture.replica.script_pages(vec![page( + &"x".repeat(4097), + vec![candidate.clone()], + false, + )]); + fixture.attach(cx); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::CursorSize)) + )); + unchanged(&fixture, &candidate, cx); +} + +#[gpui::test] +fn a_stalled_feed_has_a_complete_acquisition_deadline_and_cancels_its_reader( + cx: &mut gpui::TestAppContext, +) { + let candidate = profile("not-applied"); + let fixture = Fixture::new(cx, &[]); + fixture + .replica + .script_pages(vec![page("end", vec![candidate.clone()], false)]); + let release = fixture.replica.hold_next_read(); + fixture.attach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Syncing)); + cx.executor().advance_clock(Duration::from_secs(121)); + cx.run_until_parked(); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::Feed(ReplicationFeedError::TimedOut)) + )); + assert!( + release.send(()).is_err(), + "the original request was dropped" + ); + unchanged(&fixture, &candidate, cx); +} diff --git a/crates/connections-store/src/replication/tests/lifecycle.rs b/crates/connections-store/src/replication/tests/lifecycle.rs new file mode 100644 index 00000000..3f8f646c --- /dev/null +++ b/crates/connections-store/src/replication/tests/lifecycle.rs @@ -0,0 +1,155 @@ +use super::*; +use nocterm_connections_model::repository::{ + ConnectionDocument, ConnectionStorageError, StorageOperation, +}; + +#[gpui::test] +async fn a_restart_continues_from_the_persisted_state_without_resending( + cx: &mut gpui::TestAppContext, +) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + let saved = fixture.persisted_state(); + let cursor = saved.cursor().cloned().expect("a cursor was kept"); + let added = profile("added"); + fixture.replica.write_remote(added.clone()); + let restarted = Fixture::resumed( + cx, + std::slice::from_ref(&original), + saved, + fixture.replica.clone(), + ); + restarted.attach(cx); + assert_eq!(restarted.local(cx, &added), Some(added)); + assert_eq!(restarted.put_count(), 1); + assert!( + restarted + .replica + .calls() + .contains(&ReplicaCall::Changes(Some(cursor.as_str().to_owned()))) + ); +} + +impl Fixture { + fn detach(&self, cx: &mut gpui::TestAppContext) { + self.replication + .update(cx, |replication, cx| replication.detach(cx)); + cx.run_until_parked(); + } +} + +#[gpui::test] +async fn detaching_stops_synchronizing_and_forgets_the_agreement(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.detach(cx); + assert!(matches!(fixture.status(cx), ReplicationStatus::Detached)); + let local = profile("local"); + fixture.save(cx, local.clone()).await; + fixture.replica.write_remote(profile("remote")); + cx.run_until_parked(); + assert_eq!(fixture.put_count(), 1); + assert_eq!(fixture.local(cx, &profile("remote")), None); + assert!(fixture.replica.profile(local.id).is_none()); + assert!(fixture.persisted_state().entry(original.id).is_none()); +} + +#[gpui::test] +async fn detaching_empties_the_mirror_but_not_the_replica(cx: &mut gpui::TestAppContext) { + let (agreed, edited) = (profile("agreed"), profile("edited")); + let fixture = Fixture::new(cx, &[agreed.clone(), edited.clone()]); + fixture.attach(cx); + fixture.replica.set_offline(true); + fixture.save(cx, at_host(&edited, "unsent.example")).await; + fixture.detach(cx); + assert_eq!(fixture.local(cx, &agreed), None); + assert_eq!(fixture.local(cx, &edited), None); + assert_eq!(fixture.replica.profile(agreed.id), Some(agreed)); +} + +#[gpui::test] +async fn attaching_again_after_detaching_shows_the_account_afresh(cx: &mut gpui::TestAppContext) { + let agreed = profile("agreed"); + let fixture = Fixture::new(cx, std::slice::from_ref(&agreed)); + fixture.attach(cx); + fixture.detach(cx); + assert_eq!(fixture.local(cx, &agreed), None); + fixture.attach(cx); + assert_eq!(fixture.local(cx, &agreed), Some(agreed)); +} + +#[gpui::test] +async fn attaching_another_replica_moves_synchronization_to_it(cx: &mut gpui::TestAppContext) { + let original = profile("a"); + let fixture = Fixture::new(cx, std::slice::from_ref(&original)); + fixture.attach(cx); + fixture.detach(cx); + let second = Arc::new(MemoryReplica::new()); + fixture + .replication + .update(cx, |replication, cx| replication.attach(second.clone(), cx)); + cx.run_until_parked(); + // What was agreed with the first replica belongs to it, not to the second. + assert_eq!(second.profile(original.id), None); + assert_eq!(fixture.local(cx, &original), None); + let added = profile("added"); + fixture.save(cx, added.clone()).await; + assert_eq!(second.profile(added.id), Some(added)); + assert_eq!(fixture.put_count(), 1); +} + +#[gpui::test] +async fn changes_are_read_page_by_page_until_none_remain(cx: &mut gpui::TestAppContext) { + let fixture = Fixture::new(cx, &[]); + let remote: Vec = ["a", "b", "c"].iter().map(|name| profile(name)).collect(); + for profile in &remote { + fixture.replica.write_remote(profile.clone()); + } + fixture.replica.page_size(1); + fixture.attach(cx); + for profile in &remote { + assert_eq!(fixture.local(cx, profile), Some(profile.clone())); + } + assert!(matches!(fixture.status(cx), ReplicationStatus::Synced)); +} + +#[gpui::test] +async fn nothing_is_synchronized_while_the_local_document_failed_to_load( + cx: &mut gpui::TestAppContext, +) { + let kept = profile("kept"); + let mut state = ReplicaState::default(); + state.agree( + kept.id, + SyncedEntry { + version: ReplicaVersion::new(1), + profile: kept.clone(), + }, + ); + state.advance(ReplicaCursor::new("1")); + let replica = Arc::new(MemoryReplica::new()); + replica.write_remote(kept.clone()); + let unreadable = ConnectionStore::prepared( + Profiles::default(), + Recents::default(), + Arc::new(|_| Box::pin(async { Ok(()) })), + Arc::new(|_| Box::pin(async { Ok(()) })), + Some(Arc::new(ConnectionStorageError::new( + ConnectionDocument::Profiles, + StorageOperation::Read, + std::io::Error::from(std::io::ErrorKind::PermissionDenied), + ))), + Arc::new(nocterm_connections_model::value_ports::SuppliedValues::new( + 1, 0, + )), + ); + let fixture = Fixture::over(cx, unreadable, state, replica); + fixture.attach(cx); + assert_eq!(fixture.replica.writes(), []); + assert!(matches!( + fixture.status(cx), + ReplicationStatus::Failed(ReplicationFailure::LocalUnavailable) + )); +} diff --git a/crates/connections-store/src/test_support.rs b/crates/connections-store/src/test_support.rs new file mode 100644 index 00000000..feb24dfc --- /dev/null +++ b/crates/connections-store/src/test_support.rs @@ -0,0 +1,31 @@ +//! Controlled foreground source authority for consumers of captured probe leases. +use crate::{ProbeSourceAuthority, ProbeSourceIdentity, ProbeSourceLease}; +use gpui::App; +use std::{cell::Cell, rc::Rc}; +#[derive(Clone)] +pub struct ScriptedProbeSource { + identity: ProbeSourceIdentity, + current: Rc>, +} +impl ScriptedProbeSource { + pub fn new(identity: ProbeSourceIdentity) -> Self { + Self { + identity, + current: Rc::new(Cell::new(true)), + } + } + pub fn lease(&self) -> ProbeSourceLease { + ProbeSourceLease::new(self.identity, Rc::new(self.clone())) + } + pub fn set_current(&self, current: bool) { + self.current.set(current); + } +} +impl ProbeSourceAuthority for ScriptedProbeSource { + fn is_current(&self, _: &App) -> bool { + self.current.get() + } + fn same_session_alive(&self, _: &App) -> bool { + self.current.get() + } +} diff --git a/crates/connections/Cargo.toml b/crates/connections/Cargo.toml index 2cdb75b2..281652a7 100644 --- a/crates/connections/Cargo.toml +++ b/crates/connections/Cargo.toml @@ -12,19 +12,25 @@ layer = "feature" [dependencies] nocterm-connections-model.workspace = true nocterm-connections-store.workspace = true +nocterm-plugin-model.workspace = true +nocterm-project-model.workspace = true nocterm-store.workspace = true nocterm-session.workspace = true +nocterm-ssh-keys.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true +nocterm-workspace-ports.workspace = true +futures.workspace = true gpui-kit.workspace = true serde_json.workspace = true +thiserror.workspace = true [dev-dependencies] nocterm-workspace = { workspace = true, features = ["test-support"] } toml.workspace = true -futures.workspace = true nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } nocterm-connections-model = { workspace = true, features = ["test-support"] } gpui-kit = { workspace = true, features = ["test-support"] } tempfile.workspace = true diff --git a/crates/connections/src/agent_access.rs b/crates/connections/src/agent_access.rs new file mode 100644 index 00000000..0220bc2a --- /dev/null +++ b/crates/connections/src/agent_access.rs @@ -0,0 +1,102 @@ +//! The port through which a hub window shows and changes whether the hub's agents may work on a +//! saved server (ADR 0148): the editor's Agent access switch and the state on the server's row. +//! The composition root implements it over the hub agents service. +use gpui_kit::{App, SharedString, Subscription, Task}; +use nocterm_connections_model::profiles::Profile; + +/// The saved server whose agent access is shown and changed. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AgentServer { + /// The id of the saved connection. + pub connection: String, + pub name: String, + pub host: String, + pub port: u16, + pub user: String, +} + +impl AgentServer { + /// The saved connection `profile`, as the hub's agent servers are matched against it. + pub(crate) fn of(profile: &Profile) -> Self { + Self { + connection: profile.id.to_string(), + name: profile.name.clone(), + host: profile.target.host.clone(), + port: profile.target.port, + user: profile.target.user.clone(), + } + } +} + +/// What the hub's agents last found on a server they may sign in to. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum AgentAccessState { + /// Recorded on the hub; agents have not signed in yet. + Unchecked, + /// Agents sign in. + Ready, + /// The server refuses the agent key. + NoAccess, + /// The server presents another host key than the recorded one. + HostKeyChanged, + /// The server could not be reached. + Unreachable, + /// The agent key was replaced after its line was written: the server needs the new one. + Reconnect, +} + +impl AgentAccessState { + /// The short text of the server's row. + pub fn label(self) -> &'static str { + match self { + Self::Unchecked => "Agents", + Self::Ready => "Agents ✓", + Self::NoAccess => "No access", + Self::HostKeyChanged => "Host key changed", + Self::Unreachable => "Unreachable", + Self::Reconnect => "Reconnect", + } + } + + /// Whether nothing needs the user's attention. + pub fn is_ready(self) -> bool { + matches!(self, Self::Ready | Self::Unchecked) + } +} + +/// Why agent access did not change, in words the editor shows. +#[derive(Clone, Debug, thiserror::Error)] +#[error("{reason}")] +pub struct AgentAccessRefused { + reason: SharedString, +} + +impl AgentAccessRefused { + pub fn new(reason: impl Into) -> Self { + Self { + reason: reason.into(), + } + } + + pub fn reason(&self) -> &SharedString { + &self.reason + } +} + +/// Whether the agents of the window's hub may sign in to a saved server, and the switch for it. +pub trait ServerAgentAccess: Send + Sync { + /// The state of `server` among the hub's agent servers, as last listed; none while the hub + /// does not record it. + fn state(&self, server: &AgentServer, cx: &App) -> Option; + /// Whether a session to `server` is open, so the agent key's line can be written through it. + fn session_open(&self, server: &AgentServer, cx: &App) -> bool; + /// Grants or withdraws access; the task ends once the hub holds the new state. + fn set( + &self, + server: AgentServer, + granted: bool, + cx: &mut App, + ) -> Task>; + /// Calls `changed` whenever the hub's agent servers may have changed. + fn observe(&self, changed: Box, cx: &mut App) -> Subscription; +} diff --git a/crates/connections/src/directory.rs b/crates/connections/src/directory.rs index c67e6c00..bdaa3857 100644 --- a/crates/connections/src/directory.rs +++ b/crates/connections/src/directory.rs @@ -6,8 +6,7 @@ use std::collections::HashSet; use crate::{ Installed, - session_intents::{open_profile, report_route_error}, - spec_for_profile, + session_intents::{open_background, open_profile}, }; pub(crate) struct Directory { @@ -78,20 +77,7 @@ impl ConnectionDirectory for Directory { cx: &mut App, ) -> Option { let profile = self.profile(id, cx)?; - let spec = spec_for_profile(&profile, self.installed.connections().read(cx).profiles(cx)); - let spec = match spec { - Ok(spec) => spec, - Err(error) => { - report_route_error(&error, window, cx); - return None; - } - }; - workspace - .update(cx, |workspace, cx| { - workspace.open_background_session(spec, window, cx) - }) - .ok() - .flatten() + open_background(&self.installed, workspace, &profile, window, cx) } } diff --git a/crates/connections/src/directory/tests.rs b/crates/connections/src/directory/tests.rs index 54ae5abb..7ab7c6f2 100644 --- a/crates/connections/src/directory/tests.rs +++ b/crates/connections/src/directory/tests.rs @@ -81,7 +81,7 @@ async fn empty_center_reconnect_uses_normal_directory_and_current_profile( let (handle, workspace, opened, installed) = crate::test_support::workspace(cx); cx.update_window(handle, |_, window, cx| { workspace.update(cx, |workspace, cx| { - crate::register(&installed, workspace, window, cx) + crate::register(&installed, workspace, window, cx).unwrap(); }); }) .unwrap(); @@ -311,7 +311,7 @@ async fn a_second_workspace_keeps_shared_installation_after_first_window_closes( ] { cx.update_window(handle, |_, window, cx| { workspace.update(cx, |workspace, cx| { - crate::register(&installed, workspace, window, cx) + crate::register(&installed, workspace, window, cx).unwrap(); }) }) .unwrap(); diff --git a/crates/connections/src/editor.rs b/crates/connections/src/editor.rs index e429c771..d9df9ee2 100644 --- a/crates/connections/src/editor.rs +++ b/crates/connections/src/editor.rs @@ -1,7 +1,8 @@ //! The form that creates and edits a saved connection. use gpui_kit::{ - App, Context, Entity, FocusHandle, Focusable, SharedString, Subscription, WeakEntity, Window, + App, Context, Entity, FocusHandle, Focusable, ScrollHandle, SharedString, Subscription, + WeakEntity, Window, component::{ ActiveTheme as _, Disableable as _, Sizable as _, StyledExt as _, WindowExt as _, button::{Button, ButtonVariants as _}, @@ -18,7 +19,9 @@ use nocterm_connections_model::{jump_route::resolve_jump_route, profiles::Profil use nocterm_ui::ActiveDesign as _; use nocterm_workspace::Workspace; -use crate::{Installed, session_intents::open_profile}; +use crate::{Installed, PasswordNotKept, session_intents::open_profile}; +use futures::future::BoxFuture; +use nocterm_session::{Auth, CredentialId}; /// Two rows of icon tiles: 28 px tiles with a 4 px gap. const ICON_ROWS_HEIGHT: f32 = 60.; @@ -50,18 +53,23 @@ impl EditorSection { ]; } +use agent_access::AgentAccessField; use appearance::AppearanceFields; use authentication::AuthenticationFields; use connection::ConnectionFields; use jump_hosts::JumpHostsFields; use launch::LaunchFields; -use nocterm_connections_model::draft::{AuthKind, ProfileDraft, ValidationField}; +use nocterm_connections_model::draft::{AuthKind, ProfileDraft, ValidationField, expand_home}; use state::EditState; +use vault_key::VaultKeyEvent; +mod agent_access; mod authentication; mod connection; mod jump_hosts; mod launch; +mod password; mod state; +mod vault_key; #[derive(Debug)] struct FormError { @@ -86,6 +94,10 @@ pub fn open_editor( window: &mut Window, cx: &mut App, ) { + // A change the account does not receive would be lost. + if !crate::panel::account_state::AccountState::of(&installed, cx).editable() { + return; + } let _ = open_editor_view(installed, profile, workspace, window, cx); } fn open_editor_view( @@ -148,7 +160,10 @@ pub struct ConnectionEditor { appearance: AppearanceFields, jump_hosts: JumpHostsFields, options: Entity, + agent_access: AgentAccessField, section: EditorSection, + /// The page's scroll, to bring a row that just appeared into view. + scroll: ScrollHandle, error: Option, _subscriptions: Vec, } @@ -194,7 +209,13 @@ impl ConnectionEditor { .map_or_else(|| inputs.next_id(), |profile| profile.id); let existing = profile.as_ref(); let connection = ConnectionFields::new(existing, inputs.defaults(), window, cx); - let authentication = AuthenticationFields::new(existing, window, cx); + let authentication = AuthenticationFields::new( + existing, + inputs.keys().clone(), + inputs.passwords().clone(), + window, + cx, + ); let launch = LaunchFields::new(existing, window, cx); let appearance = AppearanceFields::new(existing, &installed, window, cx); let choices = JumpHostsFields::choices(id, &installed, cx); @@ -207,8 +228,10 @@ impl ConnectionEditor { cx, ) }); - let subscription = cx.observe(installed.facts(), |_, _, cx| cx.notify()); - Self { + let agent_access = AgentAccessField::new(inputs.agent_access().cloned()); + let mut subscriptions = vec![cx.observe(installed.facts(), |_, _, cx| cx.notify())]; + subscriptions.extend(agent_access.observe(cx)); + let editor = Self { deps: installed, focus: cx.focus_handle(), edit: EditState::new(id, profile), @@ -219,10 +242,16 @@ impl ConnectionEditor { appearance, jump_hosts, options, + agent_access, section: EditorSection::Connection, + scroll: ScrollHandle::new(), error: None, - _subscriptions: vec![subscription], + _subscriptions: subscriptions, + }; + if editor.authentication.kind() == AuthKind::VaultKey { + editor.refresh_vault_keys(window, cx); } + editor } fn fields(&self, cx: &App) -> ProfileDraft { let (metadata, endpoint) = self.connection.snapshot(cx); @@ -261,12 +290,13 @@ impl ConnectionEditor { } } - /// Saves the connection, and opens it when `and_connect` is set. + /// Saves the connection, and opens it when `and_connect` is set. A typed password is kept + /// in the window's vault first, and the connection names the record it went to. fn save(&mut self, and_connect: bool, window: &mut Window, cx: &mut Context) { if !self.edit.try_begin() { return; } - let profile = match self.validated_profile(cx) { + let mut profile = match self.validated_profile(cx) { Ok(profile) => profile, Err(error) => { if self.edit.finish() { @@ -276,13 +306,32 @@ impl ConnectionEditor { return; } }; - - let saved = self.deps.connections().update(cx, |connections, cx| { - connections.save_profile_checked(profile.clone(), self.edit.original().cloned(), cx) - }); + let original = self.edit.original().cloned(); + let kept = self.kept_password(&profile, original.as_ref(), cx); self.error = None; cx.notify(); cx.spawn_in(window, async move |this, cx| { + if let Some(kept) = kept { + match kept.await { + Ok(credential) => profile.credential = Some(credential), + Err(error) => { + let _ = this.update(cx, |this, cx| { + if this.edit.finish() { + this.error = Some(error.to_string().into()); + cx.notify(); + } + }); + return; + } + } + } + let Ok(saved) = this.update(cx, |this, cx| { + this.deps.connections().update(cx, |connections, cx| { + connections.save_profile_checked(profile.clone(), original, cx) + }) + }) else { + return; + }; let result = saved.await; let _ = this.update_in(cx, |this, window, cx| { if !this.edit.finish() { @@ -307,6 +356,33 @@ impl ConnectionEditor { .detach(); } + /// What the vault does with the connection's password before it is saved: a typed one + /// becomes the new password of the chosen record (every server it signs in to takes it) + /// or of a new one; a chosen saved password gains the address when the connection is new + /// to it. Resolves to the record the connection names; none when nothing changes. + fn kept_password( + &self, + profile: &Profile, + original: Option<&Profile>, + cx: &App, + ) -> Option>> { + let passwords = self.deps.inputs().passwords(); + let target = profile.target.clone(); + if let Some(password) = self.authentication.typed_password(cx) { + let kept = passwords.keep(profile.credential, profile.name.clone(), target, password); + return Some(kept); + } + let chosen = profile.credential?; + let unchanged = original.is_some_and(|original| { + original.credential == Some(chosen) && original.target == profile.target + }); + if unchanged || !matches!(profile.auth, Auth::Auto | Auth::Password) { + return None; + } + let attached = passwords.attach(chosen, target); + Some(Box::pin(async move { attached.await.map(|()| chosen) })) + } + fn validated_profile(&self, cx: &App) -> Result { let mut fields = self.fields(cx); fields.options = self @@ -330,17 +406,84 @@ impl ConnectionEditor { fn set_auth(&mut self, auth: AuthKind, window: &mut Window, cx: &mut Context) { self.authentication.select(auth); self.error = None; - if auth == AuthKind::Key { - self.authentication + match auth { + AuthKind::Key => self + .authentication .key_path() - .update(cx, |input, cx| input.focus(window, cx)); + .update(cx, |input, cx| input.focus(window, cx)), + AuthKind::VaultKey => self.refresh_vault_keys(window, cx), + AuthKind::Auto | AuthKind::Password => {} + } + cx.notify(); + } + + fn refresh_vault_keys(&self, window: &mut Window, cx: &mut Context) { + self.authentication + .vault_key() + .update(cx, |field, cx| field.refresh(window, cx)); + } + + /// What a vault key operation came to: a chosen key signs the connection + /// in, a failure is the error the editor shows. + fn on_vault_key(&mut self, event: &VaultKeyEvent, cx: &mut Context) { + match event { + VaultKeyEvent::Chosen => { + self.authentication.select(AuthKind::VaultKey); + self.error = None; + } + VaultKeyEvent::PassphraseAsked => self.scroll.scroll_to_bottom(), + VaultKeyEvent::Failed(message) => self.error = Some(message.clone().into()), + } + cx.notify(); + } + + /// The name a key created or imported here gets when none is typed. + fn key_label(&self, cx: &App) -> String { + let (metadata, endpoint) = self.connection.snapshot(cx); + [metadata.name, endpoint.host] + .into_iter() + .map(|text| text.trim().to_owned()) + .find(|text| !text.is_empty()) + .unwrap_or_else(|| "SSH key".to_owned()) + } + + fn create_vault_key(&mut self, window: &mut Window, cx: &mut Context) { + let label = self.key_label(cx); + self.error = None; + self.authentication + .vault_key() + .update(cx, |field, cx| field.create(&label, window, cx)); + cx.notify(); + } + + /// Stores the key file named in the editor in the vault. Both "Import" + /// and "Move to vault" end with the connection using the stored copy. + fn import_vault_key(&mut self, window: &mut Window, cx: &mut Context) { + let typed = self.authentication.snapshot(cx).key_path; + let typed = typed.trim(); + if typed.is_empty() { + self.show_validation( + FormError::new( + ValidationField::Key, + "Enter the path of the key file to import.", + ), + window, + cx, + ); + return; } + let path = expand_home(typed, self.deps.inputs().defaults().home.as_deref()); + let label = self.key_label(cx); + self.error = None; + self.authentication + .vault_key() + .update(cx, |field, cx| field.import(&label, path, window, cx)); cx.notify(); } fn show_validation(&mut self, error: FormError, window: &mut Window, cx: &mut Context) { let section = match error.field { - ValidationField::Key | ValidationField::Credential => EditorSection::Authentication, + ValidationField::Key => EditorSection::Authentication, ValidationField::Arguments | ValidationField::Environment | ValidationField::Launch => { EditorSection::Launch } @@ -354,7 +497,6 @@ impl ConnectionEditor { ValidationField::Port => Some(self.connection.port()), ValidationField::User => Some(self.connection.user()), ValidationField::Key => Some(self.authentication.key_path()), - ValidationField::Credential => Some(self.authentication.credential()), ValidationField::Arguments => Some(&self.launch.inputs()[1]), ValidationField::Environment => Some(&self.launch.inputs()[3]), ValidationField::Launch => Some(&self.launch.inputs()[0]), @@ -465,9 +607,10 @@ impl ConnectionEditor { self.section = section; let focus = match section { EditorSection::Connection => self.focus_handle(cx), - EditorSection::Authentication => { - self.authentication.credential().read(cx).focus_handle(cx) - } + EditorSection::Authentication => self.authentication.first_input(cx).map_or_else( + || self.focus.clone(), + |input| input.read(cx).focus_handle(cx), + ), EditorSection::JumpHosts => self.jump_hosts.address().read(cx).focus_handle(cx), EditorSection::Session => self.options.read(cx).focus_handle(cx), EditorSection::Launch if self.launch.overridden() => { diff --git a/crates/connections/src/editor/agent_access.rs b/crates/connections/src/editor/agent_access.rs new file mode 100644 index 00000000..b2de9cc3 --- /dev/null +++ b/crates/connections/src/editor/agent_access.rs @@ -0,0 +1,216 @@ +//! Agent access of the saved connection being edited, offered in hub windows only. +//! +//! Turning it on writes the agent key's line through a session to the server: the one the user +//! has open, or else one opened without a tab with the connection's own saved credentials, the +//! way the sidebar opens it. A session that asks the user to sign in is shown in its tab. +mod session; + +use crate::agent_access::{AgentAccessRefused, AgentServer, ServerAgentAccess}; +use gpui_kit::{ + AnyElement, Context, SharedString, Subscription, Task, Window, + base::TestSupportExt as _, + component::{ActiveTheme as _, Disableable as _, switch::Switch}, + div, + prelude::*, +}; +use nocterm_connections_model::profiles::Profile; +use nocterm_ui::form; +use session::{BackgroundSession, POLL, Step}; +use std::sync::Arc; + +use super::ConnectionEditor; + +const DESCRIPTION: &str = "Lets this hub's agents sign in with the account's agent key. Turning it on adds the key to the server's authorized_keys, connecting with the saved credentials when no session is open."; + +/// What a change of agent access is doing. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +enum Progress { + /// A session to the server is being opened with its saved credentials. + Connecting, + /// The opened session waits for the user to sign in, in its tab. + SigningIn, + /// The server and the hub are being changed. + Changing, +} + +impl Progress { + fn note(self) -> Option<&'static str> { + match self { + Self::Connecting => Some("Connecting with the saved credentials…"), + Self::SigningIn => { + Some("The server asks you to sign in: finish in its tab, then access is added.") + } + Self::Changing => None, + } + } +} + +pub(super) struct AgentAccessField { + port: Option>, + /// What a change is doing; none while nothing changes. A change runs to its end even if the + /// editor closes, so a written key line is always recorded on the hub. + progress: Option, + problem: Option, +} + +impl AgentAccessField { + pub(super) fn new(port: Option>) -> Self { + Self { + port, + progress: None, + problem: None, + } + } + + /// Renders the editor again whenever the hub's agent servers change. + pub(super) fn observe(&self, cx: &mut Context) -> Option { + let editor = cx.weak_entity(); + let changed = move |cx: &mut gpui_kit::App| { + let _ = editor.update(cx, |_, cx| cx.notify()); + }; + Some(self.port.as_ref()?.observe(Box::new(changed), cx)) + } + + /// The row for the saved connection `profile`; none outside hub windows or before the + /// connection is saved. + pub(super) fn row( + &self, + profile: Option<&Profile>, + cx: &mut Context, + ) -> Option { + let (port, profile) = (self.port.as_ref()?, profile?.clone()); + let granted = port.state(&AgentServer::of(&profile), cx).is_some(); + let switch = Switch::new("profile-agent-access") + .checked(granted) + .disabled(self.progress.is_some()) + .on_click(cx.listener(move |editor, checked: &bool, window, cx| { + Self::change(editor, &profile, *checked, window, cx); + })); + let row = form::row("Agent access", DESCRIPTION, switch, cx); + let note = match (&self.problem, self.progress.and_then(Progress::note)) { + (Some(problem), _) => Some(("profile-agent-access-problem", problem.clone(), true)), + (None, Some(note)) => Some(("profile-agent-access-progress", note.into(), false)), + (None, None) => None, + }; + Some(match note { + Some((id, text, problem)) => { + let text = if problem { + form::error_text(text, cx).into_any_element() + } else { + div() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(text) + .into_any_element() + }; + div() + .id(id) + .test_support() + .child(row) + .child(text) + .into_any_element() + } + None => row, + }) + } + + /// Grants or withdraws access for `profile`. Granting without an open session first opens + /// one with the connection's saved credentials. + fn change( + editor: &mut ConnectionEditor, + profile: &Profile, + granted: bool, + window: &mut Window, + cx: &mut Context, + ) { + let field = &mut editor.agent_access; + let Some(port) = field.port.clone().filter(|_| field.progress.is_none()) else { + return; + }; + let server = AgentServer::of(profile); + field.problem = None; + if !granted || port.session_open(&server, cx) { + field.progress = Some(Progress::Changing); + let change = port.set(server, granted, cx); + cx.spawn(async move |editor, cx| { + let result = change.await; + let _ = editor.update(cx, |editor, cx| editor.agent_access.finish(result, cx)); + }) + .detach(); + } else { + match BackgroundSession::open(&editor.deps, &editor.workspace, profile, window, cx) { + Some(session) => { + field.progress = Some(Progress::Connecting); + connect_then_grant(port, server, session, window, cx).detach(); + } + None => field.problem = Some("The server could not be opened.".into()), + } + } + cx.notify(); + } + + fn report(&mut self, progress: Progress, cx: &mut Context) { + if self.progress != Some(progress) { + self.progress = Some(progress); + cx.notify(); + } + } + + fn finish( + &mut self, + result: Result<(), AgentAccessRefused>, + cx: &mut Context, + ) { + self.progress = None; + self.problem = result.err().map(|refused| refused.reason().clone()); + cx.notify(); + } +} + +/// Waits for `session` to come up, then grants access through it and closes it, unless the user +/// was shown it to sign in. The editor hears how it goes while it is open. +fn connect_then_grant( + port: Arc, + server: AgentServer, + mut session: BackgroundSession, + window: &mut Window, + cx: &mut Context, +) -> Task<()> { + cx.spawn_in(window, async move |editor, cx| { + let connected = loop { + cx.background_executor().timer(POLL).await; + let step = cx.update(|window, cx| { + let open = port.session_open(&server, cx); + session.poll(open, window, cx) + }); + match step { + Ok(Step::Waiting { signing_in }) => { + let progress = if signing_in { + Progress::SigningIn + } else { + Progress::Connecting + }; + let _ = + editor.update(cx, |editor, cx| editor.agent_access.report(progress, cx)); + } + Ok(Step::Connected) => break Ok(()), + Ok(Step::Failed(reason)) => break Err(AgentAccessRefused::new(reason)), + Err(_) => return, + } + }; + let result = match connected { + Ok(()) => { + let _ = editor.update(cx, |editor, cx| { + editor.agent_access.report(Progress::Changing, cx); + }); + match cx.update(|_, cx| port.set(server, true, cx)) { + Ok(change) => change.await, + Err(_) => return, + } + } + Err(refused) => Err(refused), + }; + let _ = cx.update(|window, cx| session.close(window, cx)); + let _ = editor.update(cx, |editor, cx| editor.agent_access.finish(result, cx)); + }) +} diff --git a/crates/connections/src/editor/agent_access/session.rs b/crates/connections/src/editor/agent_access/session.rs new file mode 100644 index 00000000..e7f0d586 --- /dev/null +++ b/crates/connections/src/editor/agent_access/session.rs @@ -0,0 +1,112 @@ +//! A session opened without a tab so the agent key's line can be written through it, and the +//! wait until it is up. +use crate::Installed; +use gpui_kit::{App, EntityId, SharedString, WeakEntity, Window}; +use nocterm_connections_model::profiles::Profile; +use nocterm_workspace::{TerminalStatus, Workspace}; +use std::time::Duration; + +/// How often the session is looked at while it comes up. +pub(super) const POLL: Duration = Duration::from_millis(250); +/// Looks before an unattended sign-in gives up: one minute. +const CONNECT_POLLS: u32 = 240; +/// Looks before a sign-in the user was asked for gives up: three minutes. +const SIGN_IN_POLLS: u32 = 720; + +/// Where the wait for the session stands. +#[derive(Debug, PartialEq, Eq)] +pub(super) enum Step { + /// Not up yet; `signing_in` once the user was asked to sign in. + Waiting { + signing_in: bool, + }, + /// The server can be written to. + Connected, + Failed(SharedString), +} + +pub(super) struct BackgroundSession { + workspace: WeakEntity, + item: EntityId, + /// The session asked the user to sign in and was shown in its tab; it stays open then. + shown: bool, + polls: u32, +} + +impl BackgroundSession { + /// Opens `profile` without a tab, signing in with its saved credentials. + pub(super) fn open( + installed: &Installed, + workspace: &WeakEntity, + profile: &Profile, + window: &mut Window, + cx: &mut App, + ) -> Option { + let item = + crate::session_intents::open_background(installed, workspace, profile, window, cx)?; + Some(Self { + workspace: workspace.clone(), + item, + shown: false, + polls: 0, + }) + } + + /// One look at the session; `open` tells whether the server can be written to now. A + /// session that asks the user to sign in is shown in its tab, once. + pub(super) fn poll(&mut self, open: bool, window: &mut Window, cx: &mut App) -> Step { + if open { + return Step::Connected; + } + self.polls += 1; + match self.status(cx) { + None | Some(TerminalStatus::Closed) => { + return Step::Failed( + "Could not connect to the server with its saved credentials.".into(), + ); + } + Some(TerminalStatus::AwaitingUser | TerminalStatus::AwaitingVault) if !self.shown => { + self.shown = true; + let item = self.item; + let _ = self.workspace.update(cx, |workspace, cx| { + workspace.show_background_session(item, window, cx) + }); + } + Some(_) => {} + } + let limit = if self.shown { + SIGN_IN_POLLS + } else { + CONNECT_POLLS + }; + if self.polls >= limit { + Step::Failed("Connecting to the server timed out.".into()) + } else { + Step::Waiting { + signing_in: self.shown, + } + } + } + + fn status(&self, cx: &App) -> Option { + let workspace = self.workspace.upgrade()?; + let workspace = workspace.read(cx); + let terminals = workspace + .context_terminals(workspace.general_surface_source(cx), cx) + .ok()?; + let entry = terminals + .into_iter() + .find(|entry| entry.item == self.item)?; + entry.capabilities.state().info(cx).map(|info| info.status) + } + + /// Closes the session, unless the user was shown it. + pub(super) fn close(self, window: &mut Window, cx: &mut App) { + if self.shown { + return; + } + let _ = self.workspace.update(cx, |workspace, cx| { + workspace.close_background_session(self.item, window, cx) + }); + } +} diff --git a/crates/connections/src/editor/appearance.rs b/crates/connections/src/editor/appearance.rs index 53083669..8e2f2924 100644 --- a/crates/connections/src/editor/appearance.rs +++ b/crates/connections/src/editor/appearance.rs @@ -13,7 +13,7 @@ use gpui_kit::{ prelude::*, px, rems, }; -use nocterm_ui::{IconName, form}; +use nocterm_ui::form; use super::{ConnectionEditor, ICON_ROWS_HEIGHT, field, text}; use nocterm_connections_model::draft::AppearanceDraft; @@ -242,41 +242,16 @@ impl AppearanceFields { }) .child(h_flex().flex_wrap().gap_1().children(tiles)) .when(!expanded, |grid| { - grid.child( - div() - .absolute() - .left_0() - .right_0() - .bottom_0() - .h(px(ICON_ROWS_HEIGHT / 2.)) - .bg(gpui_kit::linear_gradient( - 180., - gpui_kit::linear_color_stop(background.opacity(0.), 0.), - gpui_kit::linear_color_stop(background, 1.), - )), - ) + grid.child(form::fade(background, px(ICON_ROWS_HEIGHT / 2.))) }); - let icon = v_flex().gap_1().child(grid).child( - div().child( - Button::new("editor-icons-expand") - .xsmall() - .ghost() - .icon(if expanded { - IconName::ChevronUp - } else { - IconName::ChevronDown - }) - .label(if expanded { - "Show fewer".to_owned() - } else { - format!("Show all {count}") - }) - .on_click(cx.listener(|this, _, _, cx| { - this.appearance.toggle_expanded(); - cx.notify(); - })), - ), - ); + let icon = v_flex().gap_1().child(grid).child(div().child( + form::fold_button("editor-icons-expand", expanded, count).on_click(cx.listener( + |this, _, _, cx| { + this.appearance.toggle_expanded(); + cx.notify(); + }, + )), + )); icon.into_any_element() } fn color_control(&self, cx: &mut Context) -> AnyElement { diff --git a/crates/connections/src/editor/authentication.rs b/crates/connections/src/editor/authentication.rs index a529bd92..365619fc 100644 --- a/crates/connections/src/editor/authentication.rs +++ b/crates/connections/src/editor/authentication.rs @@ -1,44 +1,69 @@ -//! Authentication selection and its native inputs. -use super::{ConnectionEditor, field, text, watch}; +//! Authentication selection and its native inputs, and the password the connection signs in +//! with: typed here and kept in the window's vault, or one the vault already keeps. +use super::{ + ConnectionEditor, field, password::PasswordField, text, vault_key::VaultKeyField, watch, +}; +use crate::{SavedPasswords, VaultPlace}; use gpui_kit::{App, Context, Entity, Subscription, Window, component::input::InputState}; use nocterm_connections_model::draft::{AuthKind, AuthenticationDraft}; use nocterm_connections_model::profiles::Profile; -use nocterm_session::Auth; +use nocterm_session::{Auth, Secret}; +use nocterm_ssh_keys::VaultSshKeys; +use std::sync::Arc; pub(super) struct AuthenticationFields { kind: AuthKind, key_path: Entity, - credential: Entity, + password: Entity, + place: VaultPlace, + /// The connection signs in with a key file, which a hub window offers only to keep it. + key_file: bool, + vault_key: Entity, _subscriptions: Vec, } impl AuthenticationFields { pub(super) fn new( profile: Option<&Profile>, + keys: Arc, + passwords: Arc, window: &mut Window, cx: &mut Context, ) -> Self { let (kind, path) = match profile.map(|p| &p.auth) { Some(Auth::Password) => (AuthKind::Password, String::new()), Some(Auth::Key { path }) => (AuthKind::Key, path.display().to_string()), + Some(Auth::VaultKey { .. }) => (AuthKind::VaultKey, String::new()), _ => (AuthKind::Auto, String::new()), }; let key_path = field("~/.ssh/id_ed25519", path, window, cx); - let credential = field( - "Credential ID copied from Vault (optional)", - profile - .and_then(|p| p.credential) - .map(|id| id.to_string()) - .unwrap_or_default(), - window, - cx, - ); - let subscriptions = [&key_path, &credential] + let place = passwords.place(); + let chosen = profile.and_then(|p| p.credential); + let password = cx.new(|cx| { + let mut field = PasswordField::new(passwords, chosen, window, cx); + field.refresh(cx); + field + }); + let auth = profile.map(|p| &p.auth); + let vault_key = cx.new(|cx| VaultKeyField::new(keys, auth, window, cx)); + let typed = password.read(cx).input().clone(); + let (label, passphrase) = { + let field = vault_key.read(cx); + (field.label().clone(), field.passphrase().clone()) + }; + let mut subscriptions: Vec = [&key_path, &typed, &label, &passphrase] .into_iter() .map(|input| watch(input, window, cx)) .collect(); + subscriptions.push(cx.observe(&password, |_, _, cx| cx.notify())); + subscriptions.push(cx.subscribe(&vault_key, |this, _, event, cx| { + this.on_vault_key(event, cx) + })); Self { kind, key_path, - credential, + password, + place, + key_file: kind == AuthKind::Key, + vault_key, _subscriptions: subscriptions, } } @@ -51,29 +76,50 @@ impl AuthenticationFields { pub(super) fn key_path(&self) -> &Entity { &self.key_path } - pub(super) fn credential(&self) -> &Entity { - &self.credential + pub(super) fn vault_key(&self) -> &Entity { + &self.vault_key + } + pub(super) fn password(&self) -> &Entity { + &self.password + } + /// The field the page of the method starts with. + pub(super) fn first_input(&self, cx: &App) -> Option> { + match self.kind { + AuthKind::Auto | AuthKind::Password => Some(self.password.read(cx).input().clone()), + AuthKind::Key => Some(self.key_path.clone()), + AuthKind::VaultKey => None, + } + } + /// The password typed to keep, for a method that signs in with one. + pub(super) fn typed_password(&self, cx: &App) -> Option { + matches!(self.kind, AuthKind::Auto | AuthKind::Password) + .then(|| self.password.read(cx).typed(cx)) + .flatten() } pub(super) fn snapshot(&self, cx: &App) -> AuthenticationDraft { AuthenticationDraft { kind: self.kind, key_path: text(&self.key_path, cx), - credential: text(&self.credential, cx), + vault_key: self.vault_key.read(cx).chosen(), + credential: self.password.read(cx).chosen(), } } } use super::render::field_row; +use gpui_kit::component::input::Input; use gpui_kit::{ - AnyElement, + AnyElement, ClipboardItem, SharedString, TestSupportExt as _, component::{ - Selectable as _, Sizable as _, + ActiveTheme as _, Icon, Selectable as _, Sizable as _, button::{Button, ButtonVariants as _}, - h_flex, + h_flex, v_flex, }, + div, prelude::*, + rems, }; -use nocterm_ui::form; +use nocterm_ui::{IconName, form}; impl AuthenticationFields { fn render_auth_choice(&self, cx: &mut Context) -> AnyElement { @@ -88,44 +134,340 @@ impl AuthenticationFields { .selected(self.kind == kind) .on_click(cx.listener(move |this, _, window, cx| this.set_auth(kind, window, cx))) }; + // A key file stays on this computer: a hub window keeps keys in the account's vault. + let key_file = self.place == VaultPlace::ThisComputer || self.key_file; h_flex() .gap_1() .child(choice("auth-auto", "Automatic", AuthKind::Auto, cx)) .child(choice("auth-password", "Password", AuthKind::Password, cx)) - .child(choice("auth-key", "Key file", AuthKind::Key, cx)) + .when(key_file, |row| { + row.child(choice("auth-key", "Key file", AuthKind::Key, cx)) + }) + .child(choice( + "auth-vault-key", + "Vault key", + AuthKind::VaultKey, + cx, + )) .into_any_element() } pub(super) fn page(&self, cx: &mut Context) -> Vec { + let account = self.place == VaultPlace::Account; let hint = match self.kind { - AuthKind::Auto => "Like ssh: the agent, then your default keys, then a password.", - AuthKind::Password => "Always ask for the account's password.", + AuthKind::Auto => { + "Like ssh: this computer's agent and default keys, then the password." + } + AuthKind::Password => "Sign in with the account's password.", + AuthKind::Key if account => { + "A key file on this computer only. Move it to the vault to sign in from your other \ + computers too." + } AuthKind::Key => { "Sign in with one private key; its passphrase is asked for if it has one." } + AuthKind::VaultKey if account => { + "Sign in with a key kept in your account's vault on the hub. Every computer of \ + the account signs in with it." + } + AuthKind::VaultKey => { + "Sign in with a key kept in the vault. It follows the vault to your other devices." + } }; let choice = self.render_auth_choice(cx); let mut sign_in = vec![form::stacked_row("Method", hint, choice, None, cx)]; - if self.kind == AuthKind::Key { - sign_in.push(field_row( - "Private key", - "Path to the key file.", - &self.key_path, + match self.kind { + AuthKind::Key => { + sign_in.push(field_row( + "Private key", + "Path to the key file.", + &self.key_path, + cx, + )); + sign_in.push(form::row( + "Move to the vault", + "Stores this key in the vault and signs in with it from there.", + Button::new("vault-key-move") + .small() + .label("Move to vault") + .on_click( + cx.listener(|this, _, window, cx| this.import_vault_key(window, cx)), + ), + cx, + )); + } + AuthKind::VaultKey => sign_in.extend(self.vault_key_rows(cx)), + AuthKind::Auto | AuthKind::Password => { + sign_in.push(self.password_row(cx)); + sign_in.extend(self.saved_passwords_row(cx)); + } + } + vec![form::section("Sign in", sign_in, cx)] + } + + /// The password to keep, and where it is kept. + fn password_row(&self, cx: &mut Context) -> AnyElement { + let account = self.place == VaultPlace::Account; + let field = self.password.read(cx); + let hint: SharedString = match (field.chosen_listed(), field.chosen().is_some(), account) { + (Some(saved), _, _) if saved.servers > 1 => format!( + "Signs in with \u{201c}{}\u{201d}, shared by {} servers. A new password changes \ + it for all of them.", + saved.label, saved.servers + ) + .into(), + (Some(saved), _, _) => format!( + "Signs in with \u{201c}{}\u{201d} from the vault. Type a new one to replace it.", + saved.label + ) + .into(), + (None, true, true) => { + "Saved in your account's vault on the hub. Type a new one to replace it.".into() + } + (None, true, false) => { + "Saved in this computer's vault. Type a new one to replace it.".into() + } + (None, false, true) => "Kept in your account's vault on the hub, encrypted end to \ + end: every computer of the account signs in with it. Empty: \ + asked when connecting." + .into(), + (None, false, false) => { + "Kept in this computer's vault. Empty: asked when connecting.".into() + } + }; + let forget = field.chosen().is_some().then(|| { + Button::new("editor-forget-password") + .small() + .ghost() + .label("Forget") + .tooltip("Ask for the password when connecting") + .on_click(cx.listener(|this, _, _, cx| { + this.authentication + .password() + .update(cx, |field, cx| field.forget(cx)); + })) + }); + let input = field.input().clone(); + form::row( + "Password", + hint, + h_flex() + .gap_1() + .child( + div().w(rems(13.)).child( + Input::new(&input) + .id("editor-field-Password") + .small() + .mask_toggle(), + ), + ) + .children(forget), + cx, + ) + } + + /// The passwords the vault keeps, to sign in with one of them; none when it keeps none. + fn saved_passwords_row(&self, cx: &mut Context) -> Option { + let field = self.password.read(cx); + if field.listed().is_empty() { + return None; + } + let chosen = field.chosen(); + let query = field.query().clone(); + let matching: Vec<(usize, String, usize, bool)> = field + .matching(cx) + .into_iter() + .map(|(index, saved)| { + let picked = chosen == Some(saved.id); + (index, saved.label.clone(), saved.servers, picked) + }) + .collect(); + let empty = matching.is_empty(); + let rows: Vec = matching + .into_iter() + .map(|(index, label, servers, picked)| { + saved_password_row(index, label, servers, picked, cx) + }) + .collect(); + let list = v_flex() + .w_full() + .gap_0p5() + .child( + div().pb_1().child( + Input::new(&query) + .id("saved-passwords-search") + .small() + .prefix(Icon::new(IconName::Search).small()) + .cleanable(true), + ), + ) + .children(rows) + .when(empty, |list| { + list.child(form::note("No saved password has this name.", cx)) + }); + Some(form::stacked_row( + "Saved passwords", + "Sign in with a password the vault keeps. One password can sign in to several \ + servers; add and rename them on the vault's Credentials tab.", + list.into_any_element(), + None, + cx, + )) + } +} + +/// One saved password to sign in with: its name and how many servers use it, left aligned, +/// the chosen one marked. +fn saved_password_row( + index: usize, + label: String, + servers: usize, + picked: bool, + cx: &mut Context, +) -> AnyElement { + let theme = cx.theme(); + let hover = theme.foreground.opacity(0.06); + let servers = match servers { + 0 => "no server yet".to_owned(), + 1 => "1 server".to_owned(), + n => format!("{n} servers"), + }; + h_flex() + .id(SharedString::from(format!("saved-password-{index}"))) + .test_support() + .role(gpui_kit::Role::Button) + .aria_label(label.clone()) + .w_full() + .min_w_0() + .gap_2() + .px_2() + .py_1() + .rounded(theme.radius) + .cursor_pointer() + .text_sm() + .when(picked, |row| row.bg(theme.secondary_active)) + .when(!picked, |row| row.hover(move |style| style.bg(hover))) + .child(Icon::new(IconName::KeyRound).small().text_color(if picked { + theme.primary + } else { + theme.muted_foreground + })) + .child(div().min_w_0().truncate().child(label)) + .child( + div() + .flex_shrink_0() + .text_xs() + .text_color(theme.muted_foreground) + .child(servers), + ) + .child(div().flex_1()) + .when(picked, |row| { + row.child(Icon::new(IconName::Check).small().text_color(theme.primary)) + }) + .on_click(cx.listener(move |this, _, window, cx| { + this.authentication + .password() + .update(cx, |field, cx| field.choose(index, window, cx)); + })) + .into_any_element() +} + +impl AuthenticationFields { + /// The vault's keys to choose from, and ways to add one. + fn vault_key_rows(&self, cx: &mut Context) -> Vec { + let field = self.vault_key.read(cx); + let keys = field.listed().iter().enumerate().map(|(index, key)| { + Button::new(SharedString::from(format!("vault-key-{index}"))) + .small() + .ghost() + .label(format!("{} \u{b7} {}", key.label, key.fingerprint)) + .selected(field.is_chosen(key)) + .on_click(cx.listener(move |this, _, _, cx| { + this.authentication + .vault_key() + .update(cx, |field, cx| field.choose_listed(index, cx)); + })) + }); + let list = v_flex().gap_1().children(keys).children( + field + .note() + .map(|note| div().text_xs().child(note.to_owned())), + ); + let mut rows = vec![form::stacked_row( + "Key", + "The keys the vault holds.", + list.into_any_element(), + None, + cx, + )]; + if let Some(fingerprint) = field.chosen_fingerprint() { + rows.push(form::row( + "Public key", + fingerprint, + Button::new("vault-key-copy") + .small() + .ghost() + .icon(IconName::Copy) + .label("Copy public key") + .on_click(cx.listener(|this, _, _, cx| { + if let Some(chosen) = this.authentication.vault_key().read(cx).chosen() { + cx.write_to_clipboard(ClipboardItem::new_string(chosen.public_key)); + } + })), cx, )); } - vec![ - form::section("Sign in", sign_in, cx), - form::section( - "Saved password", - [field_row( - "Credential ID", - "Saved from the sign-in prompt. Empty: ask each time.", - &self.credential, - cx, - )], + rows.push(field_row( + "Key name", + "How a new or imported key is named in the vault.", + field.label(), + cx, + )); + rows.push(form::row( + "New key", + "Creates an Ed25519 key in the vault.", + Button::new("vault-key-create") + .small() + .label("Create") + .on_click(cx.listener(|this, _, window, cx| this.create_vault_key(window, cx))), + cx, + )); + rows.push(self.import_row(cx)); + if field.passphrase_needed() { + rows.push(field_row( + "Passphrase", + "The key file is protected; the vault keeps it without the passphrase.", + field.passphrase(), cx, - ), - ] + )); + } + rows + } + + /// The key file to import, with its button beside the path so that the + /// button stays where it was when the passphrase row appears below. + fn import_row(&self, cx: &Context) -> AnyElement { + form::row( + "Private key", + "Path to a key file to import into the vault.", + h_flex() + .gap_1() + .child( + div().w(rems(13.)).child( + Input::new(&self.key_path) + .id("editor-field-Private key") + .small(), + ), + ) + .child( + Button::new("vault-key-import") + .small() + .label("Import") + .on_click( + cx.listener(|this, _, window, cx| this.import_vault_key(window, cx)), + ), + ), + cx, + ) } } diff --git a/crates/connections/src/editor/password.rs b/crates/connections/src/editor/password.rs new file mode 100644 index 00000000..595d67fd --- /dev/null +++ b/crates/connections/src/editor/password.rs @@ -0,0 +1,133 @@ +//! The password a connection signs in with: typed here to keep it in the window's vault, or one +//! the vault already keeps, which may sign in to other servers as well. +//! +//! [`PasswordField`] owns the typed text, the chosen record, the vault's list of passwords and +//! the search that narrows it; the editor reads them when it saves and decides what to keep or +//! attach. +use crate::{SavedPassword, SavedPasswords}; +use gpui_kit::{ + AppContext as _, Context, Entity, Subscription, Task, Window, + component::input::{InputEvent, InputState}, +}; +use nocterm_session::{CredentialId, Secret}; +use std::sync::Arc; + +pub(super) struct PasswordField { + passwords: Arc, + /// A new password to keep; empty keeps the chosen one. + input: Entity, + /// The saved password the connection signs in with, until another is chosen or it is + /// forgotten. + chosen: Option, + listed: Vec, + /// What the list is narrowed to. + query: Entity, + /// Bumped by every listing, so that a superseded answer is dropped. + epoch: u64, + listing: Option>, + _query_changed: Subscription, +} + +impl PasswordField { + pub(super) fn new( + passwords: Arc, + chosen: Option, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let input = cx.new(|cx| { + InputState::new(window, cx) + .placeholder("Password") + .masked(true) + }); + let query = cx.new(|cx| InputState::new(window, cx).placeholder("Search saved passwords")); + let query_changed = cx.subscribe(&query, |_, _, event: &InputEvent, cx| { + if matches!(event, InputEvent::Change) { + cx.notify(); + } + }); + Self { + passwords, + input, + chosen, + listed: Vec::new(), + query, + epoch: 0, + listing: None, + _query_changed: query_changed, + } + } + + pub(super) fn input(&self) -> &Entity { + &self.input + } + + pub(super) fn chosen(&self) -> Option { + self.chosen + } + + pub(super) fn listed(&self) -> &[SavedPassword] { + &self.listed + } + + pub(super) fn query(&self) -> &Entity { + &self.query + } + + /// The listed passwords whose name has the searched text, with their place in the list. + pub(super) fn matching(&self, cx: &gpui_kit::App) -> Vec<(usize, &SavedPassword)> { + let query = self.query.read(cx).value().trim().to_lowercase(); + self.listed + .iter() + .enumerate() + .filter(|(_, password)| { + query.is_empty() || password.label.to_lowercase().contains(&query) + }) + .collect() + } + + /// The saved password the connection signs in with, as listed. + pub(super) fn chosen_listed(&self) -> Option<&SavedPassword> { + let chosen = self.chosen?; + self.listed.iter().find(|password| password.id == chosen) + } + + /// The password typed to keep, if any. + pub(super) fn typed(&self, cx: &gpui_kit::App) -> Option { + let typed = self.input.read(cx).value().to_string(); + (!typed.is_empty()).then(|| Secret::new(typed)) + } + + /// Signs in with the saved password at `index` of the list; a typed one is dropped. + pub(super) fn choose(&mut self, index: usize, window: &mut Window, cx: &mut Context) { + let Some(password) = self.listed.get(index) else { + return; + }; + self.chosen = Some(password.id); + self.input + .update(cx, |input, cx| input.set_value("", window, cx)); + cx.notify(); + } + + /// Unlinks the saved password: the connection asks for it again. + pub(super) fn forget(&mut self, cx: &mut Context) { + self.chosen = None; + cx.notify(); + } + + /// Reads the vault's passwords again; a vault that cannot list them offers none. + pub(super) fn refresh(&mut self, cx: &mut Context) { + self.epoch = self.epoch.wrapping_add(1); + let epoch = self.epoch; + let listed = self.passwords.list(); + self.listing = Some(cx.spawn(async move |this, cx| { + let listed = listed.await.unwrap_or_default(); + let _ = this.update(cx, |this, cx| { + if this.epoch == epoch { + this.listed = listed; + cx.notify(); + } + }); + })); + } +} diff --git a/crates/connections/src/editor/render.rs b/crates/connections/src/editor/render.rs index 3f41fdc1..f15dec4a 100644 --- a/crates/connections/src/editor/render.rs +++ b/crates/connections/src/editor/render.rs @@ -55,14 +55,21 @@ impl Render for ConnectionEditor { (window.viewport_size().height - rems(10.).to_pixels(window.rem_size())).max(px(0.)); let nav_width = rems(layout.connection_nav_width); let page = match self.section { - EditorSection::Connection => self.connection.page( - &self.launch, - &self.appearance, - self.edit.id(), - self.edit.editing(), - &self.deps, - cx, - ), + EditorSection::Connection => { + let mut page = self.connection.page( + &self.launch, + &self.appearance, + self.edit.id(), + self.edit.editing(), + &self.deps, + cx, + ); + let agents = self.agent_access.row(self.edit.original(), cx); + if let Some(row) = agents { + page.insert(1, form::section("Agents", [row], cx)); + } + page + } EditorSection::Authentication => self.authentication.page(cx), EditorSection::JumpHosts => { let labels = self.jump_hosts.labels(&self.deps, cx); @@ -103,6 +110,7 @@ impl Render for ConnectionEditor { .min_h_0() .pr_2() .overflow_y_scroll() + .track_scroll(&self.scroll) .children(page), ) } diff --git a/crates/connections/src/editor/tests.rs b/crates/connections/src/editor/tests.rs index 3d3d991c..8e8c7602 100644 --- a/crates/connections/src/editor/tests.rs +++ b/crates/connections/src/editor/tests.rs @@ -369,7 +369,7 @@ fn hidden_invalid_session_options_open_the_session_section(cx: &mut TestAppConte .unwrap(); } #[gpui_kit::test] -fn hidden_invalid_credential_focuses_credential_and_keeps_draft(cx: &mut TestAppContext) { +fn a_password_with_no_vault_to_keep_it_keeps_the_draft_open(cx: &mut TestAppContext) { let (handle, workspace, _, installed) = crate::test_support::workspace(cx); cx.update_window(handle, |_, window, cx| { open_editor(installed.clone(), None, workspace.downgrade(), window, cx); @@ -381,28 +381,20 @@ fn hidden_invalid_credential_focuses_credential_and_keeps_draft(cx: &mut TestApp cx.run_until_parked(); cx.update_window(handle, |_, window, cx| { window.render_frame(cx); - window.input("not-an-id", cx); - window.click("editor-section-connection", cx); - }) - .unwrap(); - cx.run_until_parked(); - cx.update_window(handle, |_, window, cx| { - window.render_frame(cx); + // The section opens on its password field. + window.input("s3cret", cx); window.click("editor-save", cx); }) .unwrap(); cx.run_until_parked(); cx.update_window(handle, |_, window, cx| { window.render_frame(cx); - assert!(window.try_find("editor-field-Credential ID").is_some()); - assert_eq!( - window.focused_input(cx).unwrap().value(cx).as_ref(), - "not-an-id" - ); + assert!(window.try_find("dialog").is_some()); assert_eq!( installed.connections().read(cx).profiles(cx).iter().count(), 0 ); + window.click("editor-field-Password", cx); window.press("ctrl-a", cx); window.press("backspace", cx); window.click("editor-save", cx); @@ -512,11 +504,11 @@ fn every_field_is_used() { assert_eq!(profile.group.as_deref(), Some("Work")); } #[test] -fn launch_override_and_credential_id_validate_and_preserve_arguments() { +fn launch_override_and_the_saved_password_validate_and_preserve_arguments() { let mut draft = fields("example.com"); assert!(build(&draft, AuthKind::Auto).unwrap().launch.is_none()); let id = nocterm_session::CredentialId::from_bytes([42; 16]); - draft.authentication.credential = id.to_string(); + draft.authentication.credential = Some(id); draft.launch.override_launch = true; draft.launch.program = "/bin/bash".into(); draft.launch.args = r#"["-c","printf '%s' \"a b\""]"#.into(); @@ -530,9 +522,6 @@ fn launch_override_and_credential_id_validate_and_preserve_arguments() { assert!(!launch.integration); draft.launch.env = r#"{"INVALID;KEY":"value"}"#.into(); assert!(build(&draft, AuthKind::Auto).is_err()); - draft.launch.override_launch = false; - draft.authentication.credential = "plain password".into(); - assert!(build(&draft, AuthKind::Auto).is_err()); } #[test] fn starting_directory_sets_launch_cwd_without_override() { @@ -753,7 +742,7 @@ fn unsaved_country_input_loads_flag_when_detection_is_off_and_no_session_exists( installed.connections().read(cx).profiles(cx).iter().count(), 0 ); - assert_eq!(workspace.read(cx).items().count(), 0); + assert_eq!(workspace.read(cx).items(cx).count(), 0); }) .unwrap(); assert_eq!( @@ -762,5 +751,8 @@ fn unsaved_country_input_loads_flag_when_detection_is_off_and_no_session_exists( ); } +mod agent_access; mod appearance; mod jump_hosts; +mod passwords; +mod vault_keys; diff --git a/crates/connections/src/editor/tests/agent_access.rs b/crates/connections/src/editor/tests/agent_access.rs new file mode 100644 index 00000000..e0f1423c --- /dev/null +++ b/crates/connections/src/editor/tests/agent_access.rs @@ -0,0 +1,164 @@ +//! In a hub window the editor of a saved connection offers Agent access through the injected +//! port, connecting with the connection's saved credentials when no session to it is open; +//! elsewhere, and for a connection not saved yet, it offers none. +use super::*; +use crate::test_support::ScriptedAgentAccess; +use gpui_kit::{Context, EntityId}; +use nocterm_workspace::{SessionFactory, SessionSpec, Workspace}; +use std::{cell::RefCell, rc::Rc, sync::Arc, time::Duration}; + +/// Opens sessions without a tab and records them; with `connects`, the session comes up at once. +struct BackgroundSessions { + opened: Rc>>, + access: Arc, + connects: bool, +} + +impl SessionFactory for BackgroundSessions { + fn open_background( + &self, + _: &mut Workspace, + spec: SessionSpec, + _: &mut Window, + cx: &mut Context, + ) -> Option { + self.opened.borrow_mut().push(spec); + if self.connects { + self.access.open_session(); + } + Some(cx.new(|_| ()).entity_id()) + } +} + +fn saved(installed: &Installed, cx: &mut gpui_kit::App) -> Profile { + use futures::FutureExt as _; + let profile = build(&fields("web.example"), AuthKind::Auto).unwrap(); + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + profile +} + +/// The editor of a saved connection over `access`; `None` is the window of this computer. +/// `connects` is how sessions opened without a tab come up. +fn editor_of_saved( + access: Option>, + connects: bool, + cx: &mut TestAppContext, +) -> ( + gpui_kit::AnyWindowHandle, + Profile, + Rc>>, +) { + let opened = Rc::default(); + let (handle, workspace, _, installed) = match &access { + Some(access) => crate::test_support::workspace_with_agent_access(cx, access.clone()), + None => crate::test_support::workspace(cx), + }; + if let Some(access) = access { + let sessions = BackgroundSessions { + opened: Rc::clone(&opened), + access, + connects, + }; + workspace.update(cx, |workspace, _| workspace.set_session_factory(sessions)); + } + cx.simulate_window_resize(handle, gpui_kit::size(px(1400.), px(1800.))); + let profile = cx + .update_window(handle, |_, window, cx| { + let profile = saved(&installed, cx); + open_editor( + installed, + Some(profile.clone()), + workspace.downgrade(), + window, + cx, + ); + profile + }) + .unwrap(); + cx.run_until_parked(); + (handle, profile, opened) +} + +fn toggle(handle: gpui_kit::AnyWindowHandle, cx: &mut TestAppContext) { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + window.click("profile-agent-access", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.executor().advance_clock(Duration::from_secs(1)); + cx.run_until_parked(); +} + +fn shows(handle: gpui_kit::AnyWindowHandle, id: &'static str, cx: &mut TestAppContext) -> bool { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + window.try_find(id).is_some() + }) + .unwrap() +} + +#[gpui_kit::test] +fn the_switch_grants_and_withdraws_access_of_the_saved_server(cx: &mut TestAppContext) { + let access = Arc::new(ScriptedAgentAccess::with_session()); + let (handle, profile, opened) = editor_of_saved(Some(access.clone()), false, cx); + for _ in 0..2 { + toggle(handle, cx); + } + assert_eq!(access.granted(), vec![true, false]); + assert!(opened.borrow().is_empty(), "the open session was used"); + let server = access.changes.lock().unwrap()[0].0.clone(); + assert_eq!(server.connection, profile.id.to_string()); + assert_eq!(server.host, "web.example"); + assert_eq!(server.user, profile.target.user); +} + +#[gpui_kit::test] +fn without_an_open_session_the_switch_connects_with_the_saved_credentials_first( + cx: &mut TestAppContext, +) { + let access = Arc::new(ScriptedAgentAccess::default()); + let (handle, profile, opened) = editor_of_saved(Some(access.clone()), true, cx); + toggle(handle, cx); + let opened = opened.borrow(); + assert_eq!(opened.len(), 1, "one session was opened without a tab"); + assert_eq!(opened[0].target, profile.target); + assert_eq!(opened[0].profile.as_deref(), Some(&*profile.id.to_string())); + assert_eq!(access.granted(), vec![true]); + assert!(!shows(handle, "profile-agent-access-problem", cx)); +} + +#[gpui_kit::test] +fn a_session_that_does_not_come_up_is_said_under_the_switch_and_changes_nothing( + cx: &mut TestAppContext, +) { + let access = Arc::new(ScriptedAgentAccess::default()); + let (handle, _, opened) = editor_of_saved(Some(access.clone()), false, cx); + toggle(handle, cx); + assert_eq!(opened.borrow().len(), 1); + assert!(access.granted().is_empty()); + assert!(shows(handle, "profile-agent-access-problem", cx)); +} + +#[gpui_kit::test] +fn a_refused_change_is_said_under_the_switch(cx: &mut TestAppContext) { + let access = Arc::new(ScriptedAgentAccess { + refusal: Some("The hub did not accept the change."), + ..ScriptedAgentAccess::with_session() + }); + let (handle, _, _) = editor_of_saved(Some(access), false, cx); + toggle(handle, cx); + assert!(shows(handle, "profile-agent-access-problem", cx)); +} + +#[gpui_kit::test] +fn the_window_of_this_computer_offers_no_agent_access(cx: &mut TestAppContext) { + let (handle, _, _) = editor_of_saved(None, false, cx); + assert!(!shows(handle, "profile-agent-access", cx)); +} diff --git a/crates/connections/src/editor/tests/passwords.rs b/crates/connections/src/editor/tests/passwords.rs new file mode 100644 index 00000000..ee892e7c --- /dev/null +++ b/crates/connections/src/editor/tests/passwords.rs @@ -0,0 +1,240 @@ +//! The password a connection signs in with, typed in the "Authentication" section and kept in +//! the window's vault. +use super::*; +use crate::{VaultPlace, test_support::ScriptedPasswords}; +use nocterm_connections_model::profiles::Profile; +use nocterm_session::CredentialId; +use std::sync::Arc; + +fn saved_profiles(cx: &mut TestAppContext, installed: &crate::Installed) -> Vec { + cx.read(|cx| { + installed + .connections() + .read(cx) + .profiles(cx) + .iter() + .cloned() + .collect() + }) +} + +fn act( + cx: &mut TestAppContext, + handle: gpui_kit::AnyWindowHandle, + steps: impl FnOnce(&mut gpui_kit::Window, &mut gpui_kit::App), +) { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + steps(window, cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +/// Opens the editor of `profile` (a new connection to `prod.example` without one) on its +/// "Authentication" section. +fn open( + cx: &mut TestAppContext, + passwords: &Arc, + profile: Option, +) -> (gpui_kit::AnyWindowHandle, crate::Installed) { + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_passwords(cx, passwords.clone()); + if let Some(profile) = &profile { + cx.update(|cx| { + use futures::FutureExt as _; + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + }); + } + cx.simulate_window_resize(handle, gpui_kit::size(px(900.), px(900.))); + let is_new = profile.is_none(); + act(cx, handle, |window, cx| { + open_editor( + installed.clone(), + profile, + workspace.downgrade(), + window, + cx, + ); + window.render_frame(cx); + if is_new { + window.input("prod.example", cx); + } + window.click("editor-section-authentication", cx); + }); + (handle, installed) +} + +fn type_password(cx: &mut TestAppContext, handle: gpui_kit::AnyWindowHandle, password: &str) { + act(cx, handle, |window, cx| { + window.click("editor-field-Password", cx); + window.input(password, cx); + }); +} + +#[gpui_kit::test] +fn a_typed_password_goes_to_the_windows_vault_and_the_connection_names_its_record( + cx: &mut TestAppContext, +) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + let (handle, installed) = open(cx, &passwords, None); + act(cx, handle, |window, cx| window.click("auth-password", cx)); + type_password(cx, handle, "s3cret"); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + + let kept = passwords.kept.lock().unwrap().clone(); + assert_eq!(kept.len(), 1); + let (replacing, label, target, password) = &kept[0]; + assert_eq!(*replacing, None); + assert_eq!(label, "prod.example"); + assert_eq!(*target, Target::new("tester", "prod.example", 22)); + assert_eq!(password, "s3cret"); + let profiles = saved_profiles(cx, &installed); + assert_eq!(profiles[0].auth, Auth::Password); + assert_eq!( + profiles[0].credential, + Some(CredentialId::from_bytes([1; 16])) + ); +} + +#[gpui_kit::test] +fn a_new_password_replaces_the_saved_one_and_forgetting_unlinks_it(cx: &mut TestAppContext) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + let id = CredentialId::from_bytes([9; 16]); + let mut draft = fields("prod.example"); + draft.endpoint.user = "ops".into(); + let mut profile = build(&draft, AuthKind::Password).unwrap(); + profile.credential = Some(id); + let (handle, installed) = open(cx, &passwords, Some(profile.clone())); + type_password(cx, handle, "n3w"); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + let kept = passwords.kept.lock().unwrap().clone(); + assert_eq!( + kept[0].0, + Some(id), + "the saved record takes the new password" + ); + assert_eq!(saved_profiles(cx, &installed)[0].credential, Some(id)); + + profile = saved_profiles(cx, &installed).remove(0); + let (handle, installed) = open(cx, &passwords, Some(profile)); + act(cx, handle, |window, cx| { + window.click("editor-forget-password", cx) + }); + act(cx, handle, |window, cx| { + assert!(window.try_find("editor-forget-password").is_none()); + window.click("editor-save", cx); + }); + assert_eq!(saved_profiles(cx, &installed)[0].credential, None); + assert_eq!(passwords.kept.lock().unwrap().len(), 1, "nothing new kept"); +} + +#[gpui_kit::test] +fn a_locked_vault_keeps_the_editor_open_and_saves_nothing(cx: &mut TestAppContext) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + passwords + .locked + .store(true, std::sync::atomic::Ordering::SeqCst); + let (handle, installed) = open(cx, &passwords, None); + type_password(cx, handle, "s3cret"); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + act(cx, handle, |window, _| { + assert!( + window.try_find("editor-save").is_some(), + "the editor stays open" + ); + }); + assert!(saved_profiles(cx, &installed).is_empty()); +} + +#[gpui_kit::test] +fn a_hub_window_offers_no_key_file_and_this_computer_does(cx: &mut TestAppContext) { + for (place, offered) in [ + (VaultPlace::Account, false), + (VaultPlace::ThisComputer, true), + ] { + let passwords = Arc::new(ScriptedPasswords::new(place)); + let (handle, _) = open(cx, &passwords, None); + act(cx, handle, |window, _| { + assert_eq!(window.try_find("auth-key").is_some(), offered); + assert!(window.try_find("auth-vault-key").is_some()); + }); + } +} + +#[gpui_kit::test] +fn a_saved_password_is_chosen_from_the_vault_and_signs_in_to_the_new_server_too( + cx: &mut TestAppContext, +) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + let office = CredentialId::from_bytes([5; 16]); + passwords.saved.lock().unwrap().push(crate::SavedPassword { + id: office, + label: "Office".into(), + servers: 2, + }); + let (handle, installed) = open(cx, &passwords, None); + type_password(cx, handle, "typed first"); + act(cx, handle, |window, cx| { + window.click("saved-password-0", cx) + }); + act(cx, handle, |window, cx| { + assert!(window.try_find("editor-forget-password").is_some()); + window.click("editor-save", cx); + }); + assert!( + passwords.kept.lock().unwrap().is_empty(), + "choosing drops the typed one" + ); + assert_eq!( + *passwords.attached.lock().unwrap(), + vec![(office, Target::new("tester", "prod.example", 22))] + ); + let profile = saved_profiles(cx, &installed).remove(0); + assert_eq!(profile.credential, Some(office)); + + // Saving it again without changing the password or the address attaches nothing more. + let (handle, _) = open(cx, &passwords, Some(profile)); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + assert_eq!(passwords.attached.lock().unwrap().len(), 1); +} + +#[gpui_kit::test] +fn the_search_narrows_the_saved_passwords_by_name(cx: &mut TestAppContext) { + let passwords = Arc::new(ScriptedPasswords::new(VaultPlace::Account)); + for (byte, label) in [(1u8, "Office"), (2, "homelab"), (3, "Office VPN")] { + passwords.saved.lock().unwrap().push(crate::SavedPassword { + id: CredentialId::from_bytes([byte; 16]), + label: label.into(), + servers: 1, + }); + } + let (handle, _) = open(cx, &passwords, None); + let shown = |window: &mut gpui_kit::Window| { + (0..3) + .filter(|index| window.try_find(format!("saved-password-{index}")).is_some()) + .collect::>() + }; + act(cx, handle, |window, cx| { + assert_eq!(shown(window), [0, 1, 2]); + window.click("saved-passwords-search", cx); + window.input("office", cx); + }); + act(cx, handle, |window, cx| { + assert_eq!( + shown(window), + [0, 2], + "each keeps its place in the vault's list" + ); + window.click("saved-password-2", cx); + }); + act(cx, handle, |window, _| { + assert!(window.try_find("editor-forget-password").is_some()); + }); +} diff --git a/crates/connections/src/editor/tests/vault_keys.rs b/crates/connections/src/editor/tests/vault_keys.rs new file mode 100644 index 00000000..0d569d48 --- /dev/null +++ b/crates/connections/src/editor/tests/vault_keys.rs @@ -0,0 +1,286 @@ +//! Choosing, creating and importing vault keys in the "Authentication" section. +use super::*; +use futures::FutureExt as _; +use nocterm_connections_model::profiles::Profile; +use nocterm_ssh_keys::{VaultSshKeys, testing::MemoryVaultSshKeys}; +use std::sync::Arc; + +const ENCRYPTED: &str = include_str!("../../../../ssh-keys/src/tests/fixtures/ed25519-encrypted"); +const PASSPHRASE: &str = "correct horse"; + +fn keys_of(keys: &MemoryVaultSshKeys) -> Vec { + keys.list().now_or_never().unwrap().unwrap() +} + +fn saved_profiles(cx: &mut TestAppContext, installed: &crate::Installed) -> Vec { + cx.read(|cx| { + installed + .connections() + .read(cx) + .profiles(cx) + .iter() + .cloned() + .collect() + }) +} + +fn open_authentication( + cx: &mut TestAppContext, + handle: gpui_kit::AnyWindowHandle, + workspace: &gpui_kit::Entity, + installed: &crate::Installed, + profile: Option, +) { + cx.simulate_window_resize(handle, gpui_kit::size(px(900.), px(900.))); + let is_new = profile.is_none(); + cx.update_window(handle, |_, window, cx| { + open_editor( + installed.clone(), + profile, + workspace.downgrade(), + window, + cx, + ); + window.render_frame(cx); + if is_new { + window.input("vault.test", cx); + } + window.click("editor-section-authentication", cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +fn act( + cx: &mut TestAppContext, + handle: gpui_kit::AnyWindowHandle, + steps: impl FnOnce(&mut gpui_kit::Window, &mut gpui_kit::App), +) { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + steps(window, cx); + }) + .unwrap(); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn a_created_key_is_chosen_and_the_connection_saves_with_it(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| window.click("auth-vault-key", cx)); + act(cx, handle, |window, cx| { + assert!(window.try_find("vault-key-0").is_none(), "an empty vault"); + window.click("vault-key-create", cx); + }); + act(cx, handle, |window, cx| { + assert!( + window.try_find("vault-key-0").is_some(), + "the new key is listed" + ); + window.click("editor-save", cx); + }); + let stored = keys_of(&keys); + assert_eq!(stored.len(), 1); + assert_eq!(stored[0].label, "vault.test"); + let profiles = saved_profiles(cx, &installed); + assert_eq!( + profiles[0].auth, + Auth::VaultKey { + credential: stored[0].credential, + public_key: stored[0].public_key.as_str().to_owned(), + } + ); + assert_eq!( + profiles[0].credential, None, + "the key needs no password record" + ); +} + +#[gpui_kit::test] +fn a_key_already_in_the_vault_is_chosen_from_the_list(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + keys.generate("first".into()) + .now_or_never() + .unwrap() + .unwrap(); + let second = keys + .generate("second".into()) + .now_or_never() + .unwrap() + .unwrap(); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| window.click("auth-vault-key", cx)); + act(cx, handle, |window, cx| window.click("vault-key-1", cx)); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + let profiles = saved_profiles(cx, &installed); + assert!(matches!( + &profiles[0].auth, + Auth::VaultKey { credential, .. } if *credential == second.credential + )); +} + +#[gpui_kit::test] +fn saving_without_a_chosen_key_keeps_the_editor_open(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + keys.set_locked(true); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| window.click("auth-vault-key", cx)); + act(cx, handle, |window, cx| { + window.click("vault-key-create", cx) + }); + act(cx, handle, |window, cx| window.click("editor-save", cx)); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("dialog").is_some()); + assert!(window.try_find("vault-key-0").is_none()); + }) + .unwrap(); + assert!(saved_profiles(cx, &installed).is_empty()); +} + +#[gpui_kit::test] +fn an_encrypted_key_file_is_imported_after_its_passphrase_is_asked(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default() + .with_file("/keys/id_ed25519", nocterm_session::Secret::new(ENCRYPTED)); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + open_authentication(cx, handle, &workspace, &installed, None); + act(cx, handle, |window, cx| { + window.click("auth-vault-key", cx); + }); + act(cx, handle, |window, cx| { + window.click("editor-field-Private key", cx); + window.input("/keys/id_ed25519", cx); + assert!(window.try_find("editor-field-Passphrase").is_none()); + window.click("vault-key-import", cx); + }); + act(cx, handle, |window, cx| { + assert!( + window.try_find("vault-key-0").is_none(), + "nothing stored yet" + ); + window.click("editor-field-Passphrase", cx); + window.input(PASSPHRASE, cx); + window.click("vault-key-import", cx); + }); + act(cx, handle, |window, cx| { + assert!(window.try_find("vault-key-0").is_some()); + window.click("editor-save", cx); + }); + let stored = keys_of(&keys); + assert_eq!(stored.len(), 1); + // The vault holds the key without its passphrase. + let text = keys.secret_of(stored[0].credential).unwrap(); + assert!(nocterm_ssh_keys::KeyMaterial::import(&text, None).is_ok()); + assert!(matches!( + &saved_profiles(cx, &installed)[0].auth, + Auth::VaultKey { credential, .. } if *credential == stored[0].credential + )); +} + +#[gpui_kit::test] +fn moving_a_key_file_to_the_vault_turns_the_connection_into_a_vault_key_one( + cx: &mut TestAppContext, +) { + let plain = include_str!("../../../../ssh-keys/src/tests/fixtures/ed25519"); + let keys = + MemoryVaultSshKeys::default().with_file("/keys/id", nocterm_session::Secret::new(plain)); + let (handle, workspace, _opened, installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + let mut draft = fields("moved.test"); + draft.authentication.key_path = "/keys/id".into(); + let mut profile = build(&draft, AuthKind::Key).unwrap(); + profile.name = "Moved".into(); + cx.update(|cx| { + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + }); + open_authentication(cx, handle, &workspace, &installed, Some(profile)); + act(cx, handle, |window, cx| window.click("vault-key-move", cx)); + act(cx, handle, |window, cx| { + assert!( + window.try_find("vault-key-0").is_some(), + "the vault key view is shown" + ); + window.click("editor-save", cx); + }); + let stored = keys_of(&keys); + assert_eq!(stored.len(), 1); + assert_eq!(stored[0].label, "Moved"); + assert!(matches!( + &saved_profiles(cx, &installed)[0].auth, + Auth::VaultKey { credential, .. } if *credential == stored[0].credential + )); +} + +fn events_of( + cx: &mut TestAppContext, + keys: &MemoryVaultSshKeys, + run: impl FnOnce( + &mut crate::editor::vault_key::VaultKeyField, + &mut gpui_kit::Window, + &mut gpui_kit::Context, + ), +) -> Vec { + use crate::editor::vault_key::VaultKeyField; + let (handle, _workspace, _opened, _installed) = + crate::test_support::workspace_with_keys(cx, Arc::new(keys.clone())); + let events = std::rc::Rc::new(std::cell::RefCell::new(Vec::new())); + let seen = events.clone(); + let field = cx + .update_window(handle, |_, window, cx| { + let field = cx.new(|cx| VaultKeyField::new(Arc::new(keys.clone()), None, window, cx)); + cx.subscribe(&field, move |_, event, _| { + seen.borrow_mut().push(event.clone()) + }) + .detach(); + field.update(cx, |field, cx| run(field, window, cx)); + field + }) + .unwrap(); + cx.run_until_parked(); + drop(field); + events.borrow().clone() +} + +#[gpui_kit::test] +fn the_key_field_reports_a_created_key_as_chosen(cx: &mut TestAppContext) { + let keys = MemoryVaultSshKeys::default(); + let events = events_of(cx, &keys, |field, window, cx| { + field.create("made", window, cx) + }); + assert_eq!(events, [crate::editor::vault_key::VaultKeyEvent::Chosen]); +} + +#[gpui_kit::test] +fn the_key_field_reports_a_refusal_and_a_protected_file_as_events(cx: &mut TestAppContext) { + use crate::editor::vault_key::VaultKeyEvent; + let locked = MemoryVaultSshKeys::default(); + locked.set_locked(true); + let events = events_of(cx, &locked, |field, window, cx| { + field.create("made", window, cx) + }); + assert!(matches!(events.as_slice(), [VaultKeyEvent::Failed(_)])); + + let protected = MemoryVaultSshKeys::default() + .with_file("/keys/id", nocterm_session::Secret::new(ENCRYPTED)); + let events = events_of(cx, &protected, |field, window, cx| { + field.import("made", "/keys/id".into(), window, cx) + }); + assert!(matches!( + events.as_slice(), + [VaultKeyEvent::PassphraseAsked, VaultKeyEvent::Failed(_)] + )); +} diff --git a/crates/connections/src/editor/vault_key.rs b/crates/connections/src/editor/vault_key.rs new file mode 100644 index 00000000..d1120e63 --- /dev/null +++ b/crates/connections/src/editor/vault_key.rs @@ -0,0 +1,261 @@ +//! The key kept in the vault that a connection signs in with: choosing one, +//! creating an Ed25519 key and importing a key file. +//! +//! [`VaultKeyField`] owns its state and reports outcomes as [`VaultKeyEvent`]s; +//! the editor decides what they mean for the connection and its error line. +use futures::future::BoxFuture; +use gpui_kit::{ + AppContext as _, Context, Entity, EventEmitter, Task, Window, component::input::InputState, +}; +use nocterm_connections_model::draft::VaultKeyChoice; +use nocterm_session::{Auth, Secret}; +use nocterm_ssh_keys::{ + KeyImportError, KeySource, PublicKeyText, StoredKey, VaultSshKeys, VaultSshKeysError, +}; +use std::{path::PathBuf, sync::Arc}; + +/// Where the list of vault keys stands. +enum Listing { + Loading, + Ready, + Unavailable(String), +} + +/// What an operation of the field came to, for the editor to act on. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) enum VaultKeyEvent { + /// A key is now the chosen one. + Chosen, + /// The key file is protected and its passphrase is asked for now. + PassphraseAsked, + /// An operation failed; the text is what the person reads. + Failed(String), +} + +pub(super) struct VaultKeyField { + keys: Arc, + listed: Vec, + listing: Listing, + chosen: Option, + /// The name a new or imported key is stored under. + label: Entity, + /// Asked for only after the key turned out to be encrypted. + passphrase: Entity, + passphrase_needed: bool, + /// Bumped by every operation so that a superseded answer is dropped. + epoch: u64, + task: Option>, +} + +impl EventEmitter for VaultKeyField {} + +impl VaultKeyField { + pub(super) fn new( + keys: Arc, + auth: Option<&Auth>, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let chosen = match auth { + Some(Auth::VaultKey { + credential, + public_key, + }) => Some(VaultKeyChoice { + credential: *credential, + public_key: public_key.clone(), + }), + _ => None, + }; + let label = cx.new(|cx| InputState::new(window, cx).placeholder("Name of the new key")); + let passphrase = cx.new(|cx| { + InputState::new(window, cx) + .placeholder("Passphrase of the key file") + .masked(true) + }); + Self { + keys, + listed: Vec::new(), + listing: Listing::Loading, + chosen, + label, + passphrase, + passphrase_needed: false, + epoch: 0, + task: None, + } + } + pub(super) fn chosen(&self) -> Option { + self.chosen.clone() + } + pub(super) fn label(&self) -> &Entity { + &self.label + } + pub(super) fn passphrase(&self) -> &Entity { + &self.passphrase + } + pub(super) fn passphrase_needed(&self) -> bool { + self.passphrase_needed + } + pub(super) fn listed(&self) -> &[StoredKey] { + &self.listed + } + pub(super) fn is_chosen(&self, key: &StoredKey) -> bool { + self.chosen + .as_ref() + .is_some_and(|chosen| chosen.credential == key.credential) + } + /// What to tell the person about the list, when it is not simply shown. + pub(super) fn note(&self) -> Option<&str> { + match &self.listing { + Listing::Loading => Some("Loading the vault's keys…"), + Listing::Unavailable(message) => Some(message), + Listing::Ready if self.listed.is_empty() => { + Some("The vault holds no keys yet. Create one or import a key file.") + } + Listing::Ready => None, + } + } + /// The fingerprint of the chosen key, from the list or its public half. + pub(super) fn chosen_fingerprint(&self) -> Option { + let chosen = self.chosen.as_ref()?; + match self + .listed + .iter() + .find(|key| key.credential == chosen.credential) + { + Some(key) => Some(key.fingerprint.clone()), + None => PublicKeyText::parse(&chosen.public_key) + .ok() + .map(|public| public.fingerprint().to_owned()), + } + } + pub(super) fn choose_listed(&mut self, index: usize, cx: &mut Context) { + if let Some(key) = self.listed.get(index).cloned() { + self.choose(&key, cx); + } + } + fn choose(&mut self, key: &StoredKey, cx: &mut Context) { + self.chosen = Some(VaultKeyChoice { + credential: key.credential, + public_key: key.public_key.as_str().to_owned(), + }); + cx.emit(VaultKeyEvent::Chosen); + cx.notify(); + } + + /// Reads the vault's keys again. + pub(super) fn refresh(&mut self, window: &mut Window, cx: &mut Context) { + self.listing = Listing::Loading; + let list = self.keys.list(); + let epoch = self.begin(); + self.task = Some(cx.spawn_in(window, async move |this, cx| { + let result = list.await; + let _ = this.update(cx, |field, cx| { + if field.epoch == epoch { + field.listing = match result { + Ok(keys) => { + field.listed = keys; + Listing::Ready + } + Err(error) => Listing::Unavailable(describe(&error)), + }; + cx.notify(); + } + }); + })); + } + + /// Stores a new Ed25519 key and chooses it. + pub(super) fn create( + &mut self, + fallback_label: &str, + window: &mut Window, + cx: &mut Context, + ) { + let label = self.label_or(fallback_label, cx); + let stored = self.keys.generate(label); + self.store(stored, window, cx); + } + + /// Stores the key file at `path`, asking for its passphrase when it has one. + pub(super) fn import( + &mut self, + fallback_label: &str, + path: PathBuf, + window: &mut Window, + cx: &mut Context, + ) { + let label = self.label_or(fallback_label, cx); + let text = self.passphrase.read(cx).value().to_string(); + let passphrase = (!text.is_empty()).then(|| Secret::new(text)); + let stored = self.keys.import(label, KeySource::File(path), passphrase); + self.store(stored, window, cx); + } + + fn label_or(&self, fallback: &str, cx: &Context) -> String { + let typed = self.label.read(cx).value().trim().to_owned(); + if typed.is_empty() { + fallback.to_owned() + } else { + typed + } + } + + fn begin(&mut self) -> u64 { + self.epoch = self.epoch.wrapping_add(1); + self.epoch + } + + fn store( + &mut self, + stored: BoxFuture<'static, Result>, + window: &mut Window, + cx: &mut Context, + ) { + let epoch = self.begin(); + self.task = Some(cx.spawn_in(window, async move |this, cx| { + let result = stored.await; + let _ = this.update_in(cx, |field, window, cx| { + if field.epoch != epoch { + return; + } + match result { + Ok(key) => { + field.listed.push(key.clone()); + field.listing = Listing::Ready; + field.passphrase_needed = false; + field + .passphrase + .update(cx, |input, cx| input.set_value("", window, cx)); + field.choose(&key, cx); + } + Err(error) => { + if matches!( + error, + VaultSshKeysError::Key(KeyImportError::PassphraseRequired) + | VaultSshKeysError::Key(KeyImportError::WrongPassphrase) + ) { + field.passphrase_needed = true; + cx.emit(VaultKeyEvent::PassphraseAsked); + } + cx.emit(VaultKeyEvent::Failed(describe(&error))); + } + } + cx.notify(); + }); + })); + } +} + +/// What the person reads when the vault could not serve a key operation. +fn describe(error: &VaultSshKeysError) -> String { + match error { + VaultSshKeysError::Locked => "Unlock the vault to use its keys.".to_owned(), + VaultSshKeysError::Offline => "The vault is offline.".to_owned(), + VaultSshKeysError::Unavailable => "No vault is available.".to_owned(), + VaultSshKeysError::Key(KeyImportError::PassphraseRequired) => { + "The key file is protected: enter its passphrase and import again.".to_owned() + } + other => other.to_string(), + } +} diff --git a/crates/connections/src/facts_presentation.rs b/crates/connections/src/facts_presentation.rs index aa88636b..d813e91b 100644 --- a/crates/connections/src/facts_presentation.rs +++ b/crates/connections/src/facts_presentation.rs @@ -1,28 +1,57 @@ //! Images and embedded fallbacks are shared presentation, never raw fact acquisition. use gpui_kit::{Image, ImageFormat}; -use nocterm_connections_model::{observation::CountryCode, profiles::Profile}; +use nocterm_connections_model::{ + observation::{CountryCode, FlagBytes}, + profiles::Profile, +}; use nocterm_connections_store::FlagState; use std::{cell::RefCell, collections::HashMap, sync::Arc}; #[derive(Default)] pub(crate) struct FactsPresentation { - images: RefCell>>>, + images: RefCell>, + icons: RefCell>>, } impl FactsPresentation { pub(crate) fn flag(&self, code: CountryCode, state: Option) -> Option> { - let state = state?; - if matches!(state, FlagState::Requested | FlagState::Loading) { - return None; + let mut images = self.images.borrow_mut(); + let source = match state { + Some(FlagState::Ready(bytes)) => Some(bytes), + Some(FlagState::Unavailable) => None, + // A new attempt must not reuse its previous result or fallback. + None | Some(FlagState::Requested | FlagState::Loading) => { + images.remove(&code); + return None; + } + }; + if let Some(cached) = images.get(&code) + && cached.same_source(source.as_ref()) + { + return cached.image.clone(); } - if let Some(image) = self.images.borrow().get(&code) { - return image.clone(); + let bytes = source.as_ref().map_or_else( + || crate::geo::builtin_flag(code.as_str()), + |bytes| Some(bytes.bytes().to_vec()), + ); + let byte_cost = bytes.as_ref().map_or(0, Vec::len) + + source.as_ref().map_or(0, |bytes| bytes.bytes().len()); + // Installation-scoped, at most 64 entries and 4 MiB of retained SVG bytes. + // Clearing the small cache on saturation also evicts obsolete country choices. + images.remove(&code); + if images.len() >= 64 + || images.values().map(|image| image.byte_cost).sum::() + byte_cost + > 4 * 1024 * 1024 + { + images.clear(); } - let bytes = match state { - FlagState::Ready(bytes) => Some(bytes.bytes().to_vec()), - FlagState::Unavailable => crate::geo::builtin_flag(code.as_str()), - FlagState::Requested | FlagState::Loading => None, - }; let image = bytes.map(|bytes| Arc::new(Image::from_bytes(ImageFormat::Svg, bytes))); - self.images.borrow_mut().insert(code, image.clone()); + images.insert( + code, + FlagImage { + source, + image: image.clone(), + byte_cost, + }, + ); image } pub(crate) fn icon( @@ -40,10 +69,37 @@ impl FactsPresentation { .as_deref() .filter(|color| crate::os::is_valid_color(color)) .unwrap_or(os.color); - Some(Arc::new(Image::from_bytes( + let rgb = crate::os::rgb(color)?; + let mut icons = self.icons.borrow_mut(); + let key = (os.id, rgb); + if let Some(image) = icons.get(&key) { + return Some(image.clone()); + } + // OS catalog identity and validated RGB are the complete rendering inputs. + if icons.len() >= 256 { + icons.clear(); + } + let image = Arc::new(Image::from_bytes( ImageFormat::Svg, crate::os::svg(os, color), - ))) + )); + icons.insert(key, image.clone()); + Some(image) + } +} +struct FlagImage { + // Retaining the source makes pointer equality safe even after raw-cache eviction. + source: Option, + image: Option>, + byte_cost: usize, +} +impl FlagImage { + fn same_source(&self, source: Option<&FlagBytes>) -> bool { + match (self.source.as_ref(), source) { + (Some(old), Some(new)) => std::ptr::eq(old.bytes(), new.bytes()), + (None, None) => true, + _ => false, + } } } #[cfg(test)] diff --git a/crates/connections/src/facts_presentation/tests.rs b/crates/connections/src/facts_presentation/tests.rs index 411638d9..58b16e87 100644 --- a/crates/connections/src/facts_presentation/tests.rs +++ b/crates/connections/src/facts_presentation/tests.rs @@ -25,3 +25,70 @@ fn embedded_fallback_waits_until_raw_negative_result() { .is_none() ); } + +#[test] +fn ready_source_changes_and_retries_replace_the_presented_flag() { + let cache = FactsPresentation::default(); + let code = CountryCode::new("de").unwrap(); + let ready = || FlagState::Ready(FlagBytes::new(b"".to_vec()).unwrap()); + let old = cache.flag(code, Some(ready())).unwrap(); + let changed = cache.flag(code, Some(ready())).unwrap(); + assert!( + !Arc::ptr_eq(&old, &changed), + "replacement raw source refreshes presentation" + ); + assert!(cache.flag(code, Some(FlagState::Loading)).is_none()); + let fallback = cache.flag(code, Some(FlagState::Unavailable)).unwrap(); + assert!(!Arc::ptr_eq(&changed, &fallback)); + let current = cache.flag(code, Some(ready())).unwrap(); + assert!(!Arc::ptr_eq(&fallback, ¤t)); +} + +#[test] +fn projected_flags_evict_old_countries_at_the_byte_budget() { + let cache = FactsPresentation::default(); + let first_code = CountryCode::new("aa").unwrap(); + let raw = FlagBytes::new(vec![b' '; 256 * 1024]).unwrap(); + let first = cache + .flag(first_code, Some(FlagState::Ready(raw.clone()))) + .unwrap(); + for second in b'b'..=b'j' { + let code = CountryCode::new(&format!("a{}", char::from(second))).unwrap(); + cache.flag(code, Some(FlagState::Ready(raw.clone()))); + } + let revisited = cache.flag(first_code, Some(FlagState::Ready(raw))).unwrap(); + assert!(!Arc::ptr_eq(&first, &revisited)); +} + +#[test] +fn projected_icons_share_canonical_color_and_evict_old_choices() { + let cache = FactsPresentation::default(); + let mut profile = nocterm_connections_model::profiles::Profile { + id: nocterm_connections_model::test_support::profile_id_for("icons"), + name: "icons".into(), + target: nocterm_session::Target::new("ci", "icons", 22), + description: String::new(), + options: Default::default(), + auth: Default::default(), + group: None, + credential: None, + launch: None, + icon: Some("ubuntu".into()), + icon_color: Some("#abc".into()), + country: None, + jump_hosts: Vec::new(), + }; + let first = cache.icon(&profile, None).unwrap(); + profile.icon_color = Some("#AABBCC".into()); + assert!(Arc::ptr_eq(&first, &cache.icon(&profile, None).unwrap())); + for color in 0..256 { + profile.icon_color = Some(format!("#{color:06x}")); + cache.icon(&profile, None); + } + profile.icon_color = Some("#abc".into()); + assert!(!Arc::ptr_eq(&first, &cache.icon(&profile, None).unwrap())); + profile.icon_color = Some("invalid".into()); + let default = cache.icon(&profile, None).unwrap(); + profile.icon_color = None; + assert!(Arc::ptr_eq(&default, &cache.icon(&profile, None).unwrap())); +} diff --git a/crates/connections/src/installed.rs b/crates/connections/src/installed.rs index e59ceba7..42da6e24 100644 --- a/crates/connections/src/installed.rs +++ b/crates/connections/src/installed.rs @@ -5,10 +5,10 @@ use nocterm_connections_model::{ observation::CountryCode, profiles::{Profile, ProfileId}, }; -use nocterm_connections_store::{FactsEvent, FactsPrepared, FactsService}; +use nocterm_connections_store::{FactsEvent, FactsPrepared, FactsService, ProfileReplication}; use nocterm_store::{SettingsHandle, ShutdownCoordinator, StoreError}; use nocterm_ui::UiReady; -use std::{rc::Rc, sync::Arc}; +use std::{cell::OnceCell, rc::Rc, sync::Arc}; #[derive(Clone)] pub struct Installed(Rc); struct Installation { @@ -18,6 +18,8 @@ struct Installation { settings: SettingsHandle, presentation: Rc, sources: Rc, + /// Created after this installation, once the application knows the account. + replication: OnceCell>, _subscriptions: Vec, } impl Installed { @@ -84,6 +86,7 @@ impl Installed { settings, presentation: Rc::new(Default::default()), sources, + replication: OnceCell::new(), _subscriptions: subscriptions, }))) } @@ -93,6 +96,20 @@ impl Installed { pub fn facts(&self) -> &Entity { &self.0.facts } + /// Tells the views which replication keeps the saved connections in step with an + /// account, so they show whether the list may be changed. Only the first call has an + /// effect. + pub fn bind_replication(&self, replication: Entity) { + let _ = self.0.replication.set(replication); + } + /// Tells `observer` about every session of a saved connection as it comes up. Only the + /// first call has an effect. + pub fn bind_host_observer(&self, observer: Rc) { + self.0.sources.bind_observer(observer); + } + pub(crate) fn replication(&self) -> Option<&Entity> { + self.0.replication.get() + } pub fn settings(&self) -> &SettingsHandle { &self.0.settings } @@ -163,3 +180,22 @@ pub fn install( cx, ) } +/// Installs connections that live only in memory, for a window whose list something else +/// keeps (the mirror of an account). It registers no settings, no key bindings and nothing +/// for shutdown: the owner of the list saves it, and `install` already did the rest for the +/// application. +pub fn install_volatile( + prepared: PreparedConnections, + ready: &UiReady, + cx: &mut App, +) -> Result { + let (connections, inputs, facts) = prepared.into_owners(); + Installed::from_owners( + connections, + facts, + inputs, + ready.settings().clone(), + &ShutdownCoordinator::default(), + cx, + ) +} diff --git a/crates/connections/src/lib.rs b/crates/connections/src/lib.rs index 89d15979..dfe4d766 100644 --- a/crates/connections/src/lib.rs +++ b/crates/connections/src/lib.rs @@ -10,24 +10,37 @@ //! Opening a connection asks the workspace for a session //! ([`Workspace::open_session`]); this crate never names the terminal. //! -//! Owns: views, editor inputs, session-intent conversion and detected-server image presentation and weak source projection. +//! Owns: views (the panel and the strip for the account's state, from the bound replication; the list may be changed only while the copy is in step with the account; in a hub window, the editor's Agent access switch of a saved server, which first opens a session without a tab with the connection's saved credentials when none is open, and the agent access state on each server's row), editor inputs and their shared `ConnectionInputs`, the in-memory installation `install_volatile` (no settings, key bindings or shutdown registration), session-intent conversion and detected-server image presentation and weak source projection. //! Does not own: profile/recent publication, committed authority, profile formats/rules, files, native defaults/IDs/clock, session transport, //! credentials, remote OS acquisition, DNS, HTTP, flag files or //! persistence worker lifecycle. //! Ports used: SettingsHandle, RemoteFs, SnapshotWriter, ShutdownCoordinator, //! ProfileIds, ConnectionClock, FactsService and original terminal capability receipts. -//! Ports provided: ConnectionDirectory. Settings section: none. +//! Ports provided: ConnectionDirectory, checked original-workspace parent +//! SessionSpec resolution for app composition and the core `connections.servers` +//! contribution (`plugin::CORE`); `HostObserver`, told once per freshly connected session (the application supplies it through `Installed::bind_host_observer`); `ServerAgentAccess`, supplied through `EditorInputs::with_agent_access` by a hub window; `SavedPasswords`, the window's vault the editor keeps a typed password in (`EditorInputs::with_passwords`). Settings section: none. //! Globals: none. Installed retains exact owners/settings and observer lifetimes. //! Invariants: profiles publish after physical success; independent recents survive //! profile failure; derived recent effects require a successful profile projection. //! Connections aliases the actual service entity; windows/actions use supplied installation. +//! Sidebar projections retain positions until profile, replication or filter +//! changes; only visible rows own short presentation and drag captures. OS/flag images +//! have installation-scoped bounded caches keyed by rendering inputs and retained raw source. //! Raw facts/jobs belong to the supplied actual service entity. Original Item //! capability receipts retain session authority independently of window projection. +//! Workspace recovery compares original profile, destination and ordered resolved +//! route before opening; it never supplies authentication from project metadata. +mod agent_access; mod installed; mod session_intents; -pub use installed::{Installed, install}; -pub use session_intents::{connect, spec_for_profile}; +pub use agent_access::{AgentAccessRefused, AgentAccessState, AgentServer, ServerAgentAccess}; +mod saved_password; +pub use installed::{Installed, install, install_volatile}; +pub use saved_password::{ + NoSavedPasswords, PasswordNotKept, SavedPassword, SavedPasswords, VaultPlace, +}; +pub use session_intents::{connect, resolve_parent_spec, spec_for_profile}; mod country_policy; mod directory; mod editor; @@ -36,15 +49,19 @@ mod geo; mod menu; pub mod os; mod panel; +pub mod plugin; mod prepared; mod probe_sources; -pub use prepared::{ConnectionWriters, EditorInputs, PreparedConnections}; +pub use prepared::{ConnectionInputs, ConnectionWriters, EditorInputs, PreparedConnections}; +pub use probe_sources::HostObserver; #[cfg(test)] mod test_support; +#[cfg(test)] +mod volatile_tests; use gpui_kit::{Context, Window, prelude::*}; -use nocterm_workspace::Workspace; +use nocterm_workspace::{PanelRegistration, RegistrationError, Workspace}; pub use editor::{ConnectionEditor, open_editor}; pub use menu::NewTabMenu; @@ -65,12 +82,14 @@ gpui_kit::actions!( /// Adds the connections panel, the new-tab menu and the commands to a /// workspace. +/// +/// The menu and the commands are registered even when the workspace refuses the panel. pub fn register( installed: &Installed, workspace: &mut Workspace, window: &mut Window, cx: &mut Context, -) { +) -> Result<(), RegistrationError> { workspace.set_connection_directory(std::rc::Rc::new(directory::Directory::new( installed.clone(), ))); @@ -84,7 +103,7 @@ pub fn register( // Facts feed cached panels and the recent servers on the empty screen. cx.observe(installed.facts(), |workspace, _, cx| { cx.emit(nocterm_workspace::WorkspaceEvent::ConnectionsChanged); - if workspace.items().next().is_none() && !workspace.local_terminal_is_visible(cx) { + if workspace.items(cx).next().is_none() && !workspace.local_terminal_is_visible(cx) { cx.notify(); } }) @@ -112,17 +131,24 @@ pub fn register( .detach(); let handle = cx.entity().downgrade(); - let panel = cx.new(|cx| ConnectionsPanel::new(installed.clone(), handle.clone(), window, cx)); - workspace.add_panel(panel, cx); - let menu = cx.new(|cx| NewTabMenu::new(installed.clone(), handle, window, cx)); workspace.set_new_tab_menu(menu, cx); workspace.register_action(|workspace, _: &ToggleServers, window, cx| { - workspace.toggle_panel_of::(window, cx); + workspace.toggle_panel(&plugin::SERVERS.id, window, cx); }); let editors = installed.clone(); workspace.register_action(move |_, _: &NewConnection, window, cx| { open_editor(editors.clone(), None, cx.entity().downgrade(), window, cx); }); + + let installed = installed.clone(); + workspace.register_panel( + PanelRegistration::new(&plugin::SERVERS, move |_, window, cx| { + let owner = cx.entity().downgrade(); + cx.new(|cx| ConnectionsPanel::new(installed.clone(), owner, window, cx)) + }), + window, + cx, + ) } diff --git a/crates/connections/src/panel.rs b/crates/connections/src/panel.rs index 44e3fc46..d5e52f55 100644 --- a/crates/connections/src/panel.rs +++ b/crates/connections/src/panel.rs @@ -8,7 +8,7 @@ use gpui_kit::{ MouseButton, SharedString, Subscription, WeakEntity, Window, base::TestSupportExt as _, component::{ - ActiveTheme as _, Icon, Sizable as _, StyledExt as _, WindowExt as _, + ActiveTheme as _, Disableable as _, Icon, Sizable as _, StyledExt as _, WindowExt as _, button::{Button, ButtonVariants as _}, dialog::DialogFooter, h_flex, @@ -26,10 +26,15 @@ use crate::{ os::{self, Os}, session_intents::open_profile, }; +use account_state::AccountState; +pub(crate) mod account_state; +mod folder; mod group_actions; mod row; +mod sections; use group_actions::{GroupAction, run_group_action}; +use sections::ConnectionList; /// A server's icon: `os` in `color` (its brand colour when none), or the /// generic server icon in `color` (`fallback` when none). @@ -96,6 +101,7 @@ pub struct ConnectionsPanel { /// Folders the user folded away. collapsed: HashSet, renaming: Option, + listing: ConnectionList, _subscriptions: Vec, } @@ -107,11 +113,12 @@ impl ConnectionsPanel { cx: &mut Context, ) -> Self { let filter = cx.new(|cx| InputState::new(window, cx).placeholder("Filter")); - let subscriptions = vec![ + let mut subscriptions = vec![ cx.observe_in( installed.connections(), window, |this, connections, window, cx| { + this.listing.invalidate(); // The group may have been deleted or ungrouped while being renamed. if let Some(rename) = &this.renaming && !connections.read(cx).profiles(cx).has_group(&rename.group) @@ -128,12 +135,30 @@ impl ConnectionsPanel { // Detected systems and countries, and flags as they load. cx.observe(installed.facts(), |_, _, cx| cx.notify()), cx.subscribe_in(&filter, window, |this, _, event, window, cx| match event { - InputEvent::Change => cx.notify(), + InputEvent::Change => { + this.listing.invalidate(); + cx.notify(); + } // Enter opens the only match. InputEvent::PressEnter { .. } => this.open_single_match(window, cx), _ => {} }), ]; + // Whether the list may be changed follows the account, not only the profiles. + if let Some(replication) = installed.replication() { + subscriptions.push(cx.observe(replication, |this, _, cx| { + this.listing.invalidate(); + cx.notify(); + })); + } + // The rows show what the hub's agents found on each server. + if let Some(access) = installed.inputs().agent_access() { + let panel = cx.weak_entity(); + let changed = move |cx: &mut App| { + let _ = panel.update(cx, |_, cx| cx.notify()); + }; + subscriptions.push(access.observe(Box::new(changed), cx)); + } Self { installed, workspace, @@ -141,10 +166,16 @@ impl ConnectionsPanel { filter, collapsed: HashSet::new(), renaming: None, + listing: ConnectionList::default(), _subscriptions: subscriptions, } } + /// Whether the list may be changed now: see [`account_state`]. + fn editable(&self, cx: &App) -> bool { + AccountState::of(&self.installed, cx).editable() + } + fn filter_text(&self, cx: &App) -> String { self.filter.read(cx).value().to_string() } @@ -163,6 +194,22 @@ impl ConnectionsPanel { open_profile(&self.installed, &self.workspace, &profile, window, cx); } + /// Opens every connection of `group`, each in its tab. + fn open_group(&mut self, group: &str, window: &mut Window, cx: &mut Context) { + let members: Vec<_> = self + .installed + .connections() + .read(cx) + .profiles(cx) + .iter() + .filter(|profile| profile.group.as_deref() == Some(group)) + .cloned() + .collect(); + for profile in &members { + open_profile(&self.installed, &self.workspace, profile, window, cx); + } + } + fn open_single_match(&mut self, window: &mut Window, cx: &mut Context) { let filter = self.filter_text(cx); let matches: Vec = self @@ -180,6 +227,9 @@ impl ConnectionsPanel { } fn edit(&mut self, id: ProfileId, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } let profile = self .installed .connections() @@ -199,6 +249,9 @@ impl ConnectionsPanel { } fn confirm_delete(&mut self, id: ProfileId, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } let Some(name) = self .installed .connections() @@ -240,17 +293,22 @@ impl ConnectionsPanel { }); } - fn confirm_delete_group(&mut self, group: String, window: &mut Window, cx: &mut Context) { - // The delete only applies while the group still holds these connections. - let members: Vec = self - .installed - .connections() - .read(cx) - .profiles(cx) - .in_group(Some(&group), "") + /// The connections in folder `group`. + fn folder_members(&self, group: &str, cx: &App) -> Vec { + let profiles = self.installed.connections().read(cx).profiles(cx); + profiles + .in_group(Some(group), "") .iter() .map(|profile| profile.id) - .collect(); + .collect() + } + + fn confirm_delete_group(&mut self, group: String, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } + // The delete only applies while the group still holds these connections. + let members = self.folder_members(&group, cx); let count = members.len(); let panel = cx.weak_entity(); let connections = self.installed.connections().clone(); @@ -309,6 +367,9 @@ impl ConnectionsPanel { } fn start_rename(&mut self, group: &str, window: &mut Window, cx: &mut Context) { + if !self.editable(cx) { + return; + } if self.renaming.as_ref().is_some_and(|r| r.group == group) { return; } @@ -325,6 +386,7 @@ impl ConnectionsPanel { this.finish_rename(true, false, window, cx) }), ]; + self.listing.invalidate(); self.renaming = Some(GroupRename { group: group.to_owned(), input, @@ -345,6 +407,7 @@ impl ConnectionsPanel { let Some(GroupRename { group, input, .. }) = self.renaming.take() else { return; }; + self.listing.invalidate(); if restore_focus { window.focus(&self.focus_handle, cx); } @@ -364,8 +427,9 @@ impl ConnectionsPanel { } else { self.collapsed.remove(&to); } + let members = self.folder_members(&group, cx); let renamed = self.installed.connections().update(cx, |connections, cx| { - connections.rename_group(group.clone(), to.clone(), cx) + connections.rename_group_of(members, group.clone(), to.clone(), cx) }); cx.spawn_in(window, async move |this, cx| { let result = renamed.await; @@ -401,6 +465,7 @@ impl ConnectionsPanel { if self.renaming.as_ref().is_some_and(|r| r.group == group) { return; } + self.listing.invalidate(); if !self.collapsed.remove(group) { self.collapsed.insert(group.to_owned()); } @@ -416,6 +481,9 @@ impl ConnectionsPanel { cx: &mut Context, ) { cx.stop_propagation(); + if !self.editable(cx) { + return; + } let moved = self.installed.connections().update(cx, |connections, cx| { connections.place_profile(dragged.0, group, before, cx) }); @@ -435,216 +503,43 @@ impl ConnectionsPanel { .detach(); } - #[expect(clippy::too_many_lines, reason = "predates the limit")] - fn render_group_header( - &self, - group: &str, - collapsed: bool, - cx: &mut Context, - ) -> impl IntoElement { - let theme = cx.theme(); - let name = group.to_owned(); - let drop_group = name.clone(); - let renaming = self.renaming.as_ref().filter(|r| r.group == group); - let label = match renaming { - Some(rename) => { - let group = name.clone(); - div() - .id(SharedString::from(format!("group-rename-{group}"))) - .test_support() - .flex_1() - .min_w_0() - .child(Input::new(&rename.input).small()) - .on_click(|_: &ClickEvent, _, cx| cx.stop_propagation()) - .on_key_down( - cx.listener(|this, event: &gpui_kit::KeyDownEvent, window, cx| { - if event.keystroke.key == "escape" { - cx.stop_propagation(); - this.finish_rename(false, true, window, cx); - } - }), - ) - .into_any_element() - } - None => { - let group = name.clone(); - div() - .id(SharedString::from(format!("group-name-{name}"))) - .test_support() - .flex_1() - .min_w_0() - .truncate() - .child(name.clone()) - // Keep the header from also treating the second click as a toggle. - .on_mouse_down(MouseButton::Left, |event, _, cx| { - if event.click_count == 2 { - cx.stop_propagation(); - } - }) - .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { - if event.click_count() == 2 { - // Undo the first click's toggle, then edit. - cx.stop_propagation(); - this.toggle_group(&group, cx); - this.start_rename(&group, window, cx); - } - })) - .into_any_element() - } - }; - let toggle_group = name.clone(); - let delete_group = name.clone(); - h_flex() - .id(SharedString::from(format!("group-{group}"))) - .test_support() - .group(GROUP_HEADER) - .gap_1() - .mx_1() - .px_1() - .py_1() - .rounded(theme.radius) - .cursor_pointer() - .text_xs() - .font_semibold() - .text_color(theme.muted_foreground) - .hover(|row| row.bg(theme.sidebar_accent)) - .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) - .on_drop( - cx.listener(move |this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, Some(drop_group.clone()), None, window, cx) - }), - ) - .child( - Icon::new(if collapsed { - IconName::ChevronRight - } else { - IconName::ChevronDown - }) - .xsmall(), - ) - .child( - Icon::new(if collapsed { - IconName::FolderClosed - } else { - IconName::FolderOpen - }) - .small(), - ) - .child(label) - .when(renaming.is_none(), |header| { - header.child( - h_flex() - .invisible() - .group_hover(GROUP_HEADER, |buttons| buttons.visible()) - .child( - Button::new(SharedString::from(format!("group-toggle-{group}"))) - .ghost() - .xsmall() - .icon(if collapsed { - IconName::ChevronRight - } else { - IconName::ChevronDown - }) - .tooltip(if collapsed { "Expand" } else { "Collapse" }) - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { - cx.stop_propagation(); - this.toggle_group(&toggle_group, cx); - })), - ) - .child( - Button::new(SharedString::from(format!("group-delete-{group}"))) - .ghost() - .xsmall() - .icon(IconName::Trash) - .tooltip("Delete group") - .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { - cx.stop_propagation(); - this.confirm_delete_group(delete_group.clone(), window, cx); - })), - ), - ) - }) - .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.toggle_group(&name, cx))) - } - - fn render_list(&self, cx: &mut Context) -> impl IntoElement { + fn render_list(&mut self, cx: &mut Context) -> impl IntoElement { let filter = self.filter_text(cx); - let filtering = !filter.trim().is_empty(); - let connections = self.installed.connections().read(cx); - let profiles = connections.profiles(cx); - - let (top, groups) = profiles.grouped(&filter); - let top: Vec<_> = top.into_iter().cloned().collect(); - let groups: Vec<_> = groups - .into_iter() - .map(|(name, members)| (name, members.into_iter().cloned().collect::>())) - .collect(); - let empty = profiles.is_empty() && groups.is_empty(); - let nothing_matches = top.is_empty() && groups.is_empty(); - - let mut list = v_flex().gap_px().py_1().child( - h_flex() - .id("connections-ungrouped") - .test_support() - .mx_1() - .px_2() - .py_1() - .rounded(cx.theme().radius) - .gap_1() - .text_xs() - .text_color(cx.theme().muted_foreground) - .child(Icon::new(IconName::Server).small()) - .child("Ungrouped") - .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) - .on_drop(cx.listener(|this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, None, None, window, cx) - })), - ); - for profile in &top { - list = list.child(self.render_row(profile, cx)); - } - list = list.child(self.render_append_zone(None, cx)); - for (group, members) in &groups { - // A filter shows every match, folded or not. - let collapsed = !filtering && self.collapsed.contains(group); - list = list.child(self.render_group_header(group, collapsed, cx)); - if !collapsed { - let drop_group = group.clone(); - let mut folder = v_flex() - .id(SharedString::from(format!("group-members-{group}"))) - .test_support() - .pl_3() - .gap_px() - .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) - .on_drop( - cx.listener(move |this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, Some(drop_group.clone()), None, window, cx) - }), - ); - for profile in members { - folder = folder.child(self.render_row(profile, cx)); - } - list = list.child(folder.child(self.render_append_zone(Some(group), cx))); - } + let editable = self.editable(cx); + let profiles = self.installed.connections().read(cx).profiles(cx); + let rename = self.renaming.as_ref().map(|rename| { + ( + rename.group.as_str(), + rename.input.read(cx).focus_handle(cx), + ) + }); + self.listing + .prepare(profiles, editable, &filter, &self.collapsed, rename); + if self.listing.empty() { + return self.render_empty(cx).into_any_element(); } - - if empty { - self.render_empty(cx).into_any_element() - } else if nothing_matches { - div() + if self.listing.nothing_matches() { + return div() .p_3() .text_sm() .text_color(cx.theme().muted_foreground) .child("No connection matches.") - .into_any_element() - } else { - div() - .id("connections-list") - .size_full() - .overflow_y_scroll() - .child(list) - .into_any_element() + .into_any_element(); } + let rows = self.listing.rows(); + div() + .id("connections-list") + .test_support() + .size_full() + .py_1() + .child( + gpui_kit::list( + self.listing.scroll(), + cx.processor(move |this, index: usize, _, cx| rows[index].render(this, cx)), + ) + .size_full(), + ) + .into_any_element() } fn render_empty(&self, cx: &mut Context) -> impl IntoElement { @@ -658,6 +553,7 @@ impl ConnectionsPanel { .child( Button::new("empty-new-connection") .small() + .disabled(!self.editable(cx)) .icon(IconName::ServerPlus) .label("New Connection") .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { @@ -704,6 +600,9 @@ impl Render for ConnectionsPanel { .persistence_error(cx) .map(|error| error.to_string()) }); + let account = AccountState::of(&self.installed, cx); + let editable = account.editable(); + let notice = account.notice(); let theme = cx.theme(); let danger = theme.danger; @@ -716,17 +615,22 @@ impl Render for ConnectionsPanel { .px_2() .pb_1() .child( - div().flex_1().child( - Input::new(&self.filter) - .small() - .cleanable(true) - .prefix(Icon::new(IconName::Search).small()), - ), + div() + .id("connections-filter") + .test_support() + .flex_1() + .child( + Input::new(&self.filter) + .small() + .cleanable(true) + .prefix(Icon::new(IconName::Search).small()), + ), ) .child( Button::new("new-connection") .ghost() .small() + .disabled(!editable) .icon(IconName::ServerPlus) .tooltip("New Connection") .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { @@ -740,6 +644,9 @@ impl Render for ConnectionsPanel { })), ), ) + .when_some(notice, |panel, notice| { + panel.child(account_state::strip(¬ice, cx)) + }) .when_some(load_error, |panel, error| { panel.child( div() diff --git a/crates/connections/src/panel/account_state.rs b/crates/connections/src/panel/account_state.rs new file mode 100644 index 00000000..b8ad0c4d --- /dev/null +++ b/crates/connections/src/panel/account_state.rs @@ -0,0 +1,128 @@ +//! What the state of the account that keeps the list lets the user do, and the strip above +//! the list that says why something is unavailable. +//! +//! Without an account the list is only this computer's and always editable. With one, the +//! list is a copy: a change that is not delivered to the account is lost, so the list may be +//! changed only while the copy is in step with it. +use super::*; +use nocterm_connections_store::{ReplicationFailure, ReplicationStatus}; +use nocterm_workspace::{UnlockVault, request_sign_in}; + +/// Where the copy of the account's list stands, read from the bound replication; none +/// without an account. +pub(crate) struct AccountState(Option); + +impl AccountState { + pub(crate) fn of(installed: &Installed, cx: &App) -> Self { + Self( + installed + .replication() + .map(|replication| replication.read(cx).status().clone()), + ) + } + + /// Whether the list may be changed: always without an account, and with one only while + /// the copy is in step with it, because a change that stays here is lost. + pub(crate) fn editable(&self) -> bool { + self.0.as_ref().is_none_or(editable) + } + + /// What to tell the user about the account above the list. + pub(crate) fn notice(&self) -> Option { + self.0.as_ref().and_then(notice) + } +} + +/// Whether the list may be changed while the copy is in the state `status`. +pub(crate) fn editable(status: &ReplicationStatus) -> bool { + matches!( + status, + ReplicationStatus::Synced | ReplicationStatus::Syncing + ) +} + +/// What the user needs to hear about the account above the list. +#[derive(Clone, Debug)] +pub(crate) enum AccountNotice { + /// The keys of the account are closed. + Locked, + /// The server cannot be reached; the list shown is the last one received. + Offline, + /// The account is signed out, so the list is empty. + SignedOut, + /// A connection was saved by a newer version of the application. + NeedsNewerClient, + Failed(ReplicationFailure), +} + +/// The notice for a copy in the state `status`; none while it is in step. +pub(crate) fn notice(status: &ReplicationStatus) -> Option { + match status { + ReplicationStatus::Synced | ReplicationStatus::Syncing => None, + ReplicationStatus::Detached => Some(AccountNotice::SignedOut), + ReplicationStatus::Offline => Some(AccountNotice::Offline), + ReplicationStatus::Locked => Some(AccountNotice::Locked), + ReplicationStatus::NeedsNewerClient => Some(AccountNotice::NeedsNewerClient), + ReplicationStatus::Failed(failure) => Some(AccountNotice::Failed(failure.clone())), + } +} + +impl AccountNotice { + fn message(&self) -> String { + match self { + Self::Locked => "Account locked".to_owned(), + Self::Offline => "Server unreachable — showing the last list, read-only".to_owned(), + Self::SignedOut => "Signed out".to_owned(), + Self::NeedsNewerClient => { + "Some connections need a newer Nocterm — the list is read-only".to_owned() + } + Self::Failed(failure) => format!("Could not synchronize: {failure}"), + } + } + + /// The label of the button that resolves the notice, if the user can. + fn remedy(&self) -> Option<&'static str> { + match self { + Self::Locked => Some("Unlock"), + Self::SignedOut => Some("Sign in"), + _ => None, + } + } + + fn resolve(&self, window: &mut Window, cx: &mut App) { + match self { + Self::Locked => window.dispatch_action(Box::new(UnlockVault), cx), + Self::SignedOut => { + request_sign_in(window, cx); + } + _ => {} + } + } +} + +/// The strip above the list for `notice`. +pub(super) fn strip(notice: &AccountNotice, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + let remedy = notice.clone(); + h_flex() + .id("connections-account-notice") + .test_support() + .gap_2() + .mx_2() + .mb_1() + .px_2() + .py_1() + .rounded(theme.radius) + .bg(theme.muted) + .text_xs() + .text_color(theme.muted_foreground) + .child(div().flex_1().min_w_0().child(notice.message())) + .when_some(notice.remedy(), |strip, label| { + strip.child( + Button::new("connections-account-action") + .xsmall() + .label(label) + .on_click(move |_: &ClickEvent, window, cx| remedy.resolve(window, cx)), + ) + }) +} diff --git a/crates/connections/src/panel/folder.rs b/crates/connections/src/panel/folder.rs new file mode 100644 index 00000000..7f89dc4d --- /dev/null +++ b/crates/connections/src/panel/folder.rs @@ -0,0 +1,132 @@ +//! The folder rows of the connections list: group header and its members. +use super::*; + +impl ConnectionsPanel { + #[expect(clippy::too_many_lines, reason = "predates the limit")] + pub(super) fn render_group_header( + &self, + group: &str, + collapsed: bool, + cx: &mut Context, + ) -> impl IntoElement { + let theme = cx.theme(); + let name = group.to_owned(); + let drop_group = name.clone(); + let renaming = self.renaming.as_ref().filter(|r| r.group == group); + let label = match renaming { + Some(rename) => { + let group = name.clone(); + div() + .id(SharedString::from(format!("group-rename-{group}"))) + .test_support() + .flex_1() + .min_w_0() + .child(Input::new(&rename.input).small()) + .on_click(|_: &ClickEvent, _, cx| cx.stop_propagation()) + .on_key_down( + cx.listener(|this, event: &gpui_kit::KeyDownEvent, window, cx| { + if event.keystroke.key == "escape" { + cx.stop_propagation(); + this.finish_rename(false, true, window, cx); + } + }), + ) + .into_any_element() + } + None => { + let group = name.clone(); + div() + .id(SharedString::from(format!("group-name-{name}"))) + .test_support() + .flex_1() + .min_w_0() + .truncate() + .child(name.clone()) + // Keep the header from also treating the second click as a toggle. + .on_mouse_down(MouseButton::Left, |event, _, cx| { + if event.click_count == 2 { + cx.stop_propagation(); + } + }) + .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { + if event.click_count() == 2 { + // Undo the first click's toggle, then edit. + cx.stop_propagation(); + this.toggle_group(&group, cx); + this.start_rename(&group, window, cx); + } + })) + .into_any_element() + } + }; + let open_group = name.clone(); + let delete_group = name.clone(); + h_flex() + .id(SharedString::from(format!("group-{group}"))) + .test_support() + .group(GROUP_HEADER) + .gap_1() + .mx_1() + .px_1() + .py_1() + .rounded(theme.radius) + .cursor_pointer() + .text_xs() + .font_semibold() + .text_color(theme.muted_foreground) + .hover(|row| row.bg(theme.sidebar_accent)) + .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) + .on_drop( + cx.listener(move |this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, Some(drop_group.clone()), None, window, cx) + }), + ) + .child( + Icon::new(if collapsed { + IconName::ChevronRight + } else { + IconName::ChevronDown + }) + .xsmall(), + ) + .child( + Icon::new(if collapsed { + IconName::FolderClosed + } else { + IconName::FolderOpen + }) + .small(), + ) + .child(label) + .when(renaming.is_none(), |header| { + header.child( + h_flex() + .invisible() + .group_hover(GROUP_HEADER, |buttons| buttons.visible()) + .child( + Button::new(SharedString::from(format!("group-open-{group}"))) + .ghost() + .xsmall() + .icon(IconName::ArrowUpRight) + .tooltip("Open every connection of the group") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.open_group(&open_group, window, cx); + })), + ) + .child( + Button::new(SharedString::from(format!("group-delete-{group}"))) + .ghost() + .xsmall() + .icon(IconName::Trash) + .tooltip("Delete group") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.confirm_delete_group(delete_group.clone(), window, cx); + })), + ), + ) + }) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| this.toggle_group(&name, cx))) + } +} diff --git a/crates/connections/src/panel/group_actions.rs b/crates/connections/src/panel/group_actions.rs index dbce155f..a9596fa1 100644 --- a/crates/connections/src/panel/group_actions.rs +++ b/crates/connections/src/panel/group_actions.rs @@ -17,12 +17,12 @@ pub(super) fn run_group_action( ) { let (done, key, title) = connections.update(cx, |connections, cx| match action { GroupAction::Ungroup => ( - connections.ungroup(group.clone(), cx), + connections.ungroup_of(expected, group.clone(), cx), "connections-ungroup", "Could not ungroup connections", ), GroupAction::Delete => ( - connections.delete_group(group.clone(), expected, cx), + connections.delete_group_of(group.clone(), expected, cx), "connections-delete-group", "Could not delete group", ), diff --git a/crates/connections/src/panel/row.rs b/crates/connections/src/panel/row.rs index 5d934c49..d4d07c97 100644 --- a/crates/connections/src/panel/row.rs +++ b/crates/connections/src/panel/row.rs @@ -1,28 +1,70 @@ //! The same passive server row supplies sidebar content and its drag appearance. use super::*; +use crate::agent_access::{AgentAccessState, AgentServer}; use gpui_kit::{Div, ObjectFit, base::ElementExt as _}; use nocterm_ui::{DragSource, IconName}; #[derive(Clone)] -struct RowVisual { - profile: Profile, - os: Option<&'static Os>, +pub(super) struct RowVisual { + id: ProfileId, + name: SharedString, + target: SharedString, + description: Option, + icon: Option>, + color: Option, flag: Option<(String, Arc)>, + group: Option, + tooltip: Option, + /// What the hub's agents found on the server, in a hub window that lets them sign in to it. + agents: Option, } impl RowVisual { - fn new(profile: &Profile, installed: &Installed, cx: &App) -> Self { + pub(super) fn new(profile: &Profile, installed: &Installed, cx: &App) -> Self { Self { - profile: profile.clone(), - os: installed.shown_os(profile, cx), + id: profile.id, + name: profile.name.clone().into(), + target: profile.target.to_string().into(), + description: (!profile.description.is_empty()) + .then(|| description_preview(&profile.description).into()), + icon: installed.icon(profile, cx), + color: profile.icon_color.as_deref().and_then(os::rgb), + group: profile.group.clone(), + tooltip: (!profile.description.is_empty()) + .then(|| description_tooltip(&profile.description)), flag: installed .shown_country(profile, cx) .and_then(|code| Some((code.to_uppercase(), installed.flag(&code, cx)?))), + agents: installed + .inputs() + .agent_access() + .and_then(|access| access.state(&AgentServer::of(profile), cx)), } } + /// The agent access state beside the name: quiet while agents sign in, a warning otherwise. + fn agents_badge(&self, cx: &App) -> Option> { + let state = self.agents?; + let theme = cx.theme(); + let color = if state.is_ready() { + theme.muted_foreground + } else { + theme.warning + }; + Some( + div() + .id(SharedString::from(format!("connection-agents-{}", self.id))) + .test_support() + .aria_label(state.label()) + .flex_shrink_0() + .text_xs() + .text_color(color) + .child(state.label()), + ) + } + fn buttons(&self) -> (Button, Button) { - let id = self.profile.id; + let id = self.id; ( Button::new(SharedString::from(format!("edit-{id}"))) .ghost() @@ -38,7 +80,6 @@ impl RowVisual { #[expect(clippy::too_many_lines, reason = "predates the limit")] fn render(&self, hovered: bool, actions: AnyElement, cx: &App) -> Div { let theme = cx.theme(); - let profile = &self.profile; h_flex() .w_full() .gap_2() @@ -49,20 +90,23 @@ impl RowVisual { .hover(|row| row.bg(theme.sidebar_accent)) .child( div() - .id(SharedString::from(format!( - "connection-icon-{}", - profile.id - ))) + .id(SharedString::from(format!("connection-icon-{}", self.id))) .test_support() .flex_shrink_0() - .child(server_icon( - self.os, - profile - .icon_color - .as_deref() - .filter(|color| os::is_valid_color(color)), - theme.muted_foreground, - )), + .child(match &self.icon { + Some(icon) => img(icon.clone()) + .size_4() + .flex_shrink_0() + .into_any_element(), + None => { + Icon::new(IconName::Server) + .small() + .text_color(self.color.map_or(theme.muted_foreground, |rgb| { + gpui_kit::rgb(rgb).into() + })) + .into_any_element() + } + }), ) .child( v_flex() @@ -74,21 +118,18 @@ impl RowVisual { .min_w_0() .child( div() - .id(SharedString::from(format!( - "connection-name-{}", - profile.id - ))) + .id(SharedString::from(format!("connection-name-{}", self.id))) .test_support() .text_sm() .truncate() - .child(profile.name.clone()), + .child(self.name.clone()), ) .when_some(self.flag.clone(), |line, (country, flag)| { line.child( div() .id(SharedString::from(format!( "connection-flag-{}", - profile.id + self.id ))) .test_support() .flex_shrink_0() @@ -113,32 +154,30 @@ impl RowVisual { }) }), ) - }), + }) + .children(self.agents_badge(cx)), ) .child( div() - .id(SharedString::from(format!( - "connection-target-{}", - profile.id - ))) + .id(SharedString::from(format!("connection-target-{}", self.id))) .test_support() .text_xs() .truncate() .text_color(theme.muted_foreground) - .child(profile.target.to_string()), + .child(self.target.clone()), ) - .when(!profile.description.is_empty(), |column| { + .when_some(self.description.clone(), |column, description| { column.child( div() .id(SharedString::from(format!( "connection-description-{}", - profile.id + self.id ))) .test_support() .text_xs() .truncate() .text_color(theme.muted_foreground) - .child(description_preview(&profile.description)), + .child(description), ) }), ) @@ -199,28 +238,37 @@ impl ConnectionsPanel { ) } - pub(super) fn render_row(&self, profile: &Profile, cx: &mut Context) -> impl IntoElement { - let id = profile.id; - let visual = RowVisual::new(profile, &self.installed, cx); + /// A connection's row. Without `writable` it can only be opened: no buttons, no dragging. + pub(super) fn render_row( + &self, + visual: RowVisual, + writable: bool, + cx: &mut Context, + ) -> impl IntoElement { + let id = visual.id; let source = DragSource::default(); let source_visual = visual.clone(); - let group = profile.group.clone(); + let group = visual.group.clone(); + let tooltip = visual.tooltip.clone(); let (edit, delete) = visual.buttons(); let actions = h_flex() .invisible() .group_hover(ROW_GROUP, |buttons| buttons.visible()) - .child(edit.tooltip("Edit").on_click(cx.listener( - move |this, _: &ClickEvent, window, cx| { - cx.stop_propagation(); - this.edit(id, window, cx); - }, - ))) - .child(delete.tooltip("Delete").on_click(cx.listener( - move |this, _: &ClickEvent, window, cx| { - cx.stop_propagation(); - this.confirm_delete(id, window, cx); - }, - ))) + .when(writable, |actions| { + actions + .child(edit.tooltip("Edit").on_click(cx.listener( + move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.edit(id, window, cx); + }, + ))) + .child(delete.tooltip("Delete").on_click(cx.listener( + move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.confirm_delete(id, window, cx); + }, + ))) + }) .into_any_element(); div() .id(SharedString::from(format!("connection-{id}"))) @@ -233,18 +281,19 @@ impl ConnectionsPanel { let source = source.clone(); move |bounds, window, _| source.capture(bounds, window) }) - .on_drag(DraggedProfile(id), move |_, _, _, cx| { - cx.stop_propagation(); - let visual = visual.clone(); - cx.new(|_| source.preview(move |_, cx| visual.preview(cx))) + .when(writable, |row| { + row.on_drag(DraggedProfile(id), move |_, _, _, cx| { + cx.stop_propagation(); + let visual = visual.clone(); + cx.new(|_| source.preview(move |_, cx| visual.preview(cx))) + }) + .on_drop(cx.listener( + move |this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, group.clone(), Some(id), window, cx) + }, + )) }) - .on_drop( - cx.listener(move |this, dragged: &DraggedProfile, window, cx| { - this.move_profile(dragged, group.clone(), Some(id), window, cx) - }), - ) - .when(!profile.description.is_empty(), |row| { - let description = description_tooltip(&profile.description); + .when_some(tooltip, |row, description| { row.tooltip(move |_, cx| { cx.new(|_| gpui_kit::component::tooltip::Tooltip::new(description.clone())) .into() diff --git a/crates/connections/src/panel/sections.rs b/crates/connections/src/panel/sections.rs new file mode 100644 index 00000000..8ecc7746 --- /dev/null +++ b/crates/connections/src/panel/sections.rs @@ -0,0 +1,228 @@ +//! Prepared sidebar positions. Saved profiles stay in their service. +use super::*; +use gpui_kit::{ListAlignment, ListState, px}; +use nocterm_connections_model::profiles::Profiles; +use std::{collections::HashMap, rc::Rc}; + +pub(super) enum Entry { + Ungrouped, + Profile { + index: usize, + id: ProfileId, + group: Option>, + writable: bool, + }, + Group(Rc, bool), + Append(Option>), +} + +impl Entry { + pub(super) fn render( + &self, + panel: &ConnectionsPanel, + cx: &mut Context, + ) -> AnyElement { + let row = match self { + Self::Ungrouped => ungrouped(cx).into_any_element(), + Self::Profile { + index, + id, + group, + writable, + } => profile_row(*index, *id, group.as_ref(), *writable, panel, cx), + Self::Group(group, collapsed) => panel + .render_group_header(group, *collapsed, cx) + .into_any_element(), + Self::Append(group) => { + let zone = panel + .render_append_zone(group.as_deref(), cx) + .into_any_element(); + match group { + Some(group) => div() + .id(SharedString::from(format!("group-members-{group}"))) + .test_support() + .pl_3() + .child(zone) + .into_any_element(), + None => div().child(zone).into_any_element(), + } + } + }; + div().pb_px().child(row).into_any_element() + } +} +fn ungrouped(cx: &mut Context) -> impl IntoElement { + h_flex() + .id("connections-ungrouped") + .test_support() + .mx_1() + .px_2() + .py_1() + .rounded(cx.theme().radius) + .gap_1() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(Icon::new(IconName::Server).small()) + .child("Ungrouped") + .drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) + .on_drop(cx.listener(|this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, None, None, window, cx) + })) +} +fn profile_row( + index: usize, + id: ProfileId, + group: Option<&Rc>, + writable: bool, + panel: &ConnectionsPanel, + cx: &mut Context, +) -> AnyElement { + let profiles = panel.installed.connections().read(cx).profiles(cx); + // A frame prepared before a deferred publication observation cannot display + // a different record that moved into the same position. + let Some(profile) = profiles + .iter() + .nth(index) + .filter(|profile| profile.id == id) + else { + return div().into_any_element(); + }; + let visual = row::RowVisual::new(profile, &panel.installed, cx); + let row = panel.render_row(visual, writable, cx).into_any_element(); + div() + .when(group.is_some(), |row| row.pl_3()) + .when_some(group.cloned(), |row, group| { + row.drag_over::(|style, _, _, cx| style.bg(cx.theme().accent)) + .on_drop( + cx.listener(move |this, dragged: &DraggedProfile, window, cx| { + this.move_profile(dragged, Some(group.to_string()), None, window, cx) + }), + ) + }) + .child(row) + .into_any_element() +} + +pub(super) struct ConnectionList { + rows: Rc>, + scroll: ListState, + dirty: bool, + empty: bool, + nothing_matches: bool, +} +impl Default for ConnectionList { + fn default() -> Self { + Self { + rows: Rc::default(), + scroll: ListState::new(0, ListAlignment::Top, px(100.)), + dirty: true, + empty: true, + nothing_matches: false, + } + } +} +impl ConnectionList { + pub(super) fn invalidate(&mut self) { + self.dirty = true; + } + pub(super) fn rows(&self) -> Rc> { + self.rows.clone() + } + pub(super) fn scroll(&self) -> ListState { + self.scroll.clone() + } + pub(super) fn empty(&self) -> bool { + self.empty + } + pub(super) fn nothing_matches(&self) -> bool { + self.nothing_matches + } + /// `editable`: whether the list may be changed now, so its rows can be edited and moved. + pub(super) fn prepare( + &mut self, + profiles: &Profiles, + editable: bool, + filter: &str, + collapsed: &HashSet, + rename: Option<(&str, FocusHandle)>, + ) { + if !self.dirty { + return; + } + let listing = Listing::new(profiles, filter); + self.empty = profiles.is_empty(); + self.nothing_matches = listing.is_empty(); + let rows = listing.into_rows(profiles, editable, filter, collapsed); + let offset = self.scroll.logical_scroll_top(); + self.scroll.splice_focusable( + 0..self.rows.len(), + rows.iter().map(|entry| match (entry, &rename) { + (Entry::Group(group, _), Some((name, focus))) if group.as_ref() == *name => { + Some(focus.clone()) + } + _ => None, + }), + ); + // Unmeasured rows otherwise contribute zero scroll extent. A height hint lets + // native wheel/thumb jumps reach unseen rows without constructing the whole list. + self.scroll = self.scroll.clone().with_uniform_item_height(px(64.)); + self.scroll.scroll_to(offset); + self.rows = Rc::new(rows); + self.dirty = false; + } +} + +/// A temporary borrowed partition, used only when semantic list inputs change. +struct Listing<'a> { + top: Vec<&'a Profile>, + groups: Vec<(String, Vec<&'a Profile>)>, +} +impl<'a> Listing<'a> { + fn new(profiles: &'a Profiles, filter: &str) -> Self { + let (top, groups) = profiles.grouped(filter); + Self { top, groups } + } + fn is_empty(&self) -> bool { + self.top.is_empty() && self.groups.is_empty() + } + fn into_rows( + self, + profiles: &Profiles, + editable: bool, + filter: &str, + collapsed: &HashSet, + ) -> Vec { + let indices: HashMap<_, _> = profiles + .iter() + .enumerate() + .map(|(index, profile)| (profile.id, index)) + .collect(); + let profile_row = |profile: &Profile, group| Entry::Profile { + index: indices[&profile.id], + id: profile.id, + group, + writable: editable, + }; + let mut rows = vec![Entry::Ungrouped]; + rows.extend( + self.top + .into_iter() + .map(|profile| profile_row(profile, None)), + ); + rows.push(Entry::Append(None)); + for (name, members) in self.groups { + let collapsed = filter.trim().is_empty() && collapsed.contains(&name); + let group: Rc = name.into(); + rows.push(Entry::Group(group.clone(), collapsed)); + if !collapsed { + rows.extend( + members + .into_iter() + .map(|profile| profile_row(profile, Some(group.clone()))), + ); + rows.push(Entry::Append(Some(group))); + } + } + rows + } +} diff --git a/crates/connections/src/panel/tests.rs b/crates/connections/src/panel/tests.rs index 7c76d185..90d382b4 100644 --- a/crates/connections/src/panel/tests.rs +++ b/crates/connections/src/panel/tests.rs @@ -2,7 +2,26 @@ use super::*; use futures::FutureExt as _; use gpui_kit::{TestAppContext, test::TestWindowExt as _}; +/// Registers the connections panel as the Servers contribution. +fn add_servers( + workspace: &mut Workspace, + panel: Entity, + window: &mut Window, + cx: &mut Context, +) { + workspace + .register_panel( + nocterm_workspace::PanelRegistration::with_entity(&crate::plugin::SERVERS, panel), + window, + cx, + ) + .unwrap(); +} + +mod account_state; +mod agents; mod drag; +mod list; mod rename; mod reorder; @@ -55,7 +74,9 @@ fn native_drag_moves_to_collapsed_folder_root_and_remembered_empty_folder(cx: &m }); let panel = cx .new(|cx| ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx)); - workspace.update(cx, |workspace, cx| workspace.add_panel(panel, cx)); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel, window, cx) + }); window.render_frame(cx); window.click("group-Personal", cx); window.render_frame(cx); @@ -265,7 +286,9 @@ fn setup_with_writer( let panel = cx.new(|cx| { ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx) }); - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel.clone(), window, cx) + }); window.render_frame(cx); panel }) @@ -314,17 +337,21 @@ fn set_rename_value( #[gpui_kit::test] fn header_click_toggles_once_and_hover_buttons_do_not_double_fire(cx: &mut TestAppContext) { - let (handle, panel, _, _installed) = setup(cx, &[gp("a", Some("Work"))]); + let (handle, panel, opened, _installed) = setup(cx, &[gp("a", Some("Work"))]); cx.update_window(handle, |_, window, cx| { window.click("group-Work", cx); assert!(panel.read(cx).collapsed.contains("Work")); window.hover("group-Work", cx); window.render_frame(cx); - window.click("group-toggle-Work", cx); - assert!(!panel.read(cx).collapsed.contains("Work")); + window.click("group-open-Work", cx); + assert!( + panel.read(cx).collapsed.contains("Work"), + "opening the group leaves it collapsed" + ); assert!(!window.has_active_dialog(cx)); }) .unwrap(); + assert_eq!(opened.borrow().len(), 1); } fn open_group_dialog(cx: &mut TestAppContext, handle: gpui_kit::AnyWindowHandle) { @@ -492,19 +519,35 @@ fn collapsed_of(cx: &mut TestAppContext, panel: &Entity) -> Ve } #[gpui_kit::test] -fn header_toggle_button_round_trips(cx: &mut TestAppContext) { - let (handle, panel, opened, _installed) = setup(cx, &[gp("a", Some("Work"))]); - for expected in [true, false, true] { - cx.update_window(handle, |_, window, cx| { - window.hover("group-Work", cx); - window.render_frame(cx); - window.click("group-toggle-Work", cx); - window.render_frame(cx); - assert_eq!(panel.read(cx).collapsed.contains("Work"), expected); - }) - .unwrap(); - } - assert!(opened.borrow().is_empty()); +fn the_group_button_opens_every_connection_of_the_group(cx: &mut TestAppContext) { + let (handle, panel, opened, _installed) = setup( + cx, + &[ + gp("web", Some("Work")), + gp("home", Some("Personal")), + gp("db", Some("Work")), + gp("solo", None), + ], + ); + cx.update_window(handle, |_, window, cx| { + window.hover("group-Work", cx); + window.render_frame(cx); + window.click("group-open-Work", cx); + window.render_frame(cx); + assert!(!panel.read(cx).collapsed.contains("Work"), "it stays open"); + }) + .unwrap(); + let mut hosts: Vec = opened + .borrow() + .iter() + .map(|spec| spec.target.host.clone()) + .collect(); + hosts.sort(); + assert_eq!( + hosts, + ["db", "web"], + "every member of the group and nothing else" + ); } #[gpui_kit::test] diff --git a/crates/connections/src/panel/tests/account_state.rs b/crates/connections/src/panel/tests/account_state.rs new file mode 100644 index 00000000..ce1c6538 --- /dev/null +++ b/crates/connections/src/panel/tests/account_state.rs @@ -0,0 +1,170 @@ +use super::*; +use crate::test_support::{replicate, unattached}; +use nocterm_connections_model::test_support::MemoryReplica; +use nocterm_connections_store::{ReplicationFailure, ReplicationStatus}; +use nocterm_workspace::UnlockVault; +use std::{cell::Cell, rc::Rc}; + +/// Stands in for the action of the hubs crate, which this crate cannot name. It is declared in +/// a module so the actions reference does not list it twice. +mod stand_in { + gpui_kit::actions!(hubs, [SignInToServer]); +} +use stand_in::SignInToServer; + +/// How the account stands in a panel over one personal connection. +enum Account { + /// Attached to a replica that answers. + Synced, + /// Attached to a replica that cannot be reached. + Offline, + /// Attached to a replica whose keys are closed. + Locked, + /// Bound to an account that is signed out. + SignedOut, +} + +fn account( + cx: &mut TestAppContext, + state: Account, +) -> (gpui_kit::AnyWindowHandle, Entity, Profile) { + let (handle, workspace, _opened, installed) = crate::test_support::workspace(cx); + let own = gp("own", None); + cx.update(|cx| { + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(own.clone(), cx) + .now_or_never() + .unwrap() + .unwrap() + }); + }); + match state { + Account::SignedOut => { + unattached(&installed, cx); + } + state => { + let replica = Arc::new(MemoryReplica::new()); + match state { + Account::Offline => replica.set_offline(true), + Account::Locked => replica.set_locked(true), + _ => {} + } + replicate(&installed, replica, cx); + } + } + let panel = cx + .update_window(handle, |_, window, cx| { + let panel = cx.new(|cx| { + ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx) + }); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel.clone(), window, cx) + }); + window.render_frame(cx); + panel + }) + .unwrap(); + (handle, panel, own) +} + +fn has(cx: &mut TestAppContext, handle: gpui_kit::AnyWindowHandle, id: &str) -> bool { + cx.update_window(handle, |_, window, _| { + window.try_find(id.to_owned()).is_some() + }) + .unwrap() +} + +#[gpui_kit::test] +fn a_list_in_step_with_the_account_has_no_strip_and_can_be_changed(cx: &mut TestAppContext) { + let (handle, _panel, own) = account(cx, Account::Synced); + assert!(!has(cx, handle, "connections-account-notice")); + assert!(has(cx, handle, &format!("edit-{}", own.id))); + assert!(has(cx, handle, &format!("delete-{}", own.id))); +} + +#[gpui_kit::test] +fn a_locked_account_shows_an_unlock_strip_that_asks_for_the_vault(cx: &mut TestAppContext) { + let (handle, _panel, _own) = account(cx, Account::Locked); + assert!(has(cx, handle, "connections-account-notice")); + let asked = Rc::new(Cell::new(0)); + let counter = asked.clone(); + cx.update(|cx| { + cx.on_action(move |_: &UnlockVault, _| counter.set(counter.get() + 1)); + }); + cx.update_window(handle, |_, window, cx| { + window.click("connections-account-action", cx) + }) + .unwrap(); + assert_eq!(asked.get(), 1); +} + +#[gpui_kit::test] +fn a_signed_out_account_shows_a_sign_in_strip_that_asks_to_sign_in(cx: &mut TestAppContext) { + let (handle, _panel, _own) = account(cx, Account::SignedOut); + assert!(has(cx, handle, "connections-account-notice")); + let asked = Rc::new(Cell::new(0)); + let counter = asked.clone(); + cx.update(|cx| { + cx.on_action(move |_: &SignInToServer, _| counter.set(counter.get() + 1)); + }); + cx.update_window(handle, |_, window, cx| { + window.click("connections-account-action", cx) + }) + .unwrap(); + assert_eq!(asked.get(), 1); +} + +#[gpui_kit::test] +fn an_unreachable_server_is_told_without_a_remedy(cx: &mut TestAppContext) { + let (handle, _panel, _own) = account(cx, Account::Offline); + assert!(has(cx, handle, "connections-account-notice")); + assert!(!has(cx, handle, "connections-account-action")); +} + +#[gpui_kit::test] +fn outside_sync_nothing_is_added_edited_deleted_or_moved(cx: &mut TestAppContext) { + for state in [Account::Offline, Account::Locked, Account::SignedOut] { + let (handle, panel, own) = account(cx, state); + assert!(!has(cx, handle, &format!("edit-{}", own.id))); + assert!(!has(cx, handle, &format!("delete-{}", own.id))); + cx.update_window(handle, |_, window, cx| { + panel.update(cx, |panel, cx| { + panel.edit(own.id, window, cx); + panel.confirm_delete(own.id, window, cx); + panel.start_rename("Work", window, cx); + panel.confirm_delete_group("Work".into(), window, cx); + }); + crate::open_editor( + panel.read(cx).installed.clone(), + None, + panel.read(cx).workspace.clone(), + window, + cx, + ); + window.render_frame(cx); + assert!(window.try_find("dialog".to_owned()).is_none()); + }) + .unwrap(); + } +} + +#[test] +fn only_a_list_in_step_with_the_account_is_editable() { + use crate::panel::account_state::{editable, notice}; + let failed = ReplicationStatus::Failed(ReplicationFailure::Conflicts); + for status in [ReplicationStatus::Synced, ReplicationStatus::Syncing] { + assert!(editable(&status)); + assert!(notice(&status).is_none()); + } + for status in [ + ReplicationStatus::Detached, + ReplicationStatus::Offline, + ReplicationStatus::Locked, + ReplicationStatus::NeedsNewerClient, + failed, + ] { + assert!(!editable(&status), "{status:?}"); + assert!(notice(&status).is_some(), "{status:?}"); + } +} diff --git a/crates/connections/src/panel/tests/agents.rs b/crates/connections/src/panel/tests/agents.rs new file mode 100644 index 00000000..67d1665b --- /dev/null +++ b/crates/connections/src/panel/tests/agents.rs @@ -0,0 +1,72 @@ +//! In a hub window each saved server's row says what the hub's agents found there; a server +//! the agents may not sign in to, and every row of other windows, says nothing. +use super::*; +use crate::{AgentAccessState, test_support::ScriptedAgentAccess}; + +/// A panel listing one saved server, in a hub window when `access` is given. +fn panel_of( + access: Option>, + cx: &mut TestAppContext, +) -> (gpui_kit::AnyWindowHandle, Profile) { + let (handle, workspace, _, installed) = match access { + Some(access) => crate::test_support::workspace_with_agent_access(cx, access), + None => crate::test_support::workspace(cx), + }; + let profile = gp("web", None); + cx.update_window(handle, |_, window, cx| { + installed.connections().update(cx, |connections, cx| { + connections + .save_profile(profile.clone(), cx) + .now_or_never() + .unwrap() + .unwrap(); + }); + let panel = cx + .new(|cx| ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx)); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel, window, cx) + }); + }) + .unwrap(); + (handle, profile) +} + +fn badge( + handle: gpui_kit::AnyWindowHandle, + profile: &Profile, + cx: &mut TestAppContext, +) -> Option { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let id = SharedString::from(format!("connection-agents-{}", profile.id)); + window + .try_find(id) + .map(|badge| badge.label().unwrap_or_default().to_owned()) + }) + .unwrap() +} + +#[gpui_kit::test] +fn a_row_says_what_the_agents_found_on_its_server(cx: &mut TestAppContext) { + for (state, label) in [ + (AgentAccessState::Ready, "Agents ✓"), + (AgentAccessState::NoAccess, "No access"), + (AgentAccessState::HostKeyChanged, "Host key changed"), + (AgentAccessState::Reconnect, "Reconnect"), + ] { + let access = Arc::new(ScriptedAgentAccess::default()); + *access.state.lock().unwrap() = Some(state); + let (handle, profile) = panel_of(Some(access), cx); + assert_eq!(badge(handle, &profile, cx).as_deref(), Some(label)); + } +} + +#[gpui_kit::test] +fn a_server_without_agent_access_and_the_window_of_this_computer_show_none( + cx: &mut TestAppContext, +) { + let (handle, profile) = panel_of(Some(Arc::new(ScriptedAgentAccess::default())), cx); + assert_eq!(badge(handle, &profile, cx), None); + let (handle, profile) = panel_of(None, cx); + assert_eq!(badge(handle, &profile, cx), None); +} diff --git a/crates/connections/src/panel/tests/list.rs b/crates/connections/src/panel/tests/list.rs new file mode 100644 index 00000000..e233d722 --- /dev/null +++ b/crates/connections/src/panel/tests/list.rs @@ -0,0 +1,107 @@ +//! Rendering and filtering large connection folders through the public sidebar. +use super::*; + +fn profile(index: usize) -> Profile { + let name = format!("Server {index:03}"); + Profile { + id: nocterm_connections_model::test_support::profile_id_for(&name), + name, + group: Some("Work".into()), + target: nocterm_session::Target::new("ci", "host", 22), + description: "A saved host".into(), + options: Default::default(), + auth: Default::default(), + credential: None, + launch: None, + icon: Some("ubuntu".into()), + icon_color: None, + country: None, + jump_hosts: Vec::new(), + } +} + +fn rendered(window: &Window) -> Vec { + (0..256) + .filter(|index| { + window + .try_find(SharedString::from(format!( + "connection-{}", + profile(*index).id + ))) + .is_some() + }) + .collect() +} + +#[gpui_kit::test] +fn expanded_large_folders_build_visible_rows_and_open_the_filtered_last_server( + cx: &mut TestAppContext, +) { + let (handle, workspace, opened, installed) = crate::test_support::workspace(cx); + cx.update_window(handle, |_, window, cx| { + installed.connections().update(cx, |connections, cx| { + for index in 0..256 { + connections + .save_profile(profile(index), cx) + .now_or_never() + .unwrap() + .unwrap(); + } + }); + let panel = cx + .new(|cx| ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx)); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel, window, cx) + }); + window.render_frame(cx); + let materialized = rendered(window).len(); + assert!( + (1..40).contains(&materialized), + "materialized {materialized}/256 rows" + ); + assert!( + window + .try_find(SharedString::from(format!( + "connection-{}", + profile(255).id + ))) + .is_none() + ); + window.scroll( + "connections-list", + gpui_kit::ScrollDelta::Pixels(gpui_kit::point( + gpui_kit::px(0.), + gpui_kit::px(-100_000.), + )), + cx, + ); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let visible = rendered(window); + assert!( + visible.contains(&255), + "native wheel reaches the last row: {visible:?}" + ); + window.click("connections-filter", cx); + window.input("Server 255", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + assert!( + window + .try_find(SharedString::from(format!("connection-{}", profile(0).id))) + .is_none() + ); + window.click( + SharedString::from(format!("connection-{}", profile(255).id)), + cx, + ); + }) + .unwrap(); + assert_eq!(opened.borrow().len(), 1); +} diff --git a/crates/connections/src/panel/tests/reorder.rs b/crates/connections/src/panel/tests/reorder.rs index 26735ecc..f459fdee 100644 --- a/crates/connections/src/panel/tests/reorder.rs +++ b/crates/connections/src/panel/tests/reorder.rs @@ -37,7 +37,9 @@ fn dropping_on_a_row_places_the_connection_above_it(cx: &mut TestAppContext) { }); let panel = cx .new(|cx| ConnectionsPanel::new(installed.clone(), workspace.downgrade(), window, cx)); - workspace.update(cx, |workspace, cx| workspace.add_panel(panel, cx)); + workspace.update(cx, |workspace, cx| { + add_servers(workspace, panel, window, cx) + }); window.render_frame(cx); window.drag_to( SharedString::from(format!("connection-{}", b.id)), diff --git a/crates/connections/src/plugin.rs b/crates/connections/src/plugin.rs new file mode 100644 index 00000000..1b18128f --- /dev/null +++ b/crates/connections/src/plugin.rs @@ -0,0 +1,36 @@ +//! What the connections feature contributes to the window shell. +//! +//! Connections are core: the saved servers are never disabled and belong to no Plugins page. +//! A catalog lists them through [`CORE`]. +use nocterm_plugin_model::{ + AutoPlace, ContributionId, ContributionKind, Icon, PanelChrome, PanelTraits, Placement, + SystemContribution, WidthClass, Zone, +}; + +/// The saved servers in the sidebar. +pub const SERVERS: SystemContribution = SystemContribution { + id: ContributionId::new("connections.servers"), + kind: ContributionKind::Panel, + title: "Connections", + icon: Icon::Server, + placement: Placement { + zone: Zone::Left, + order: 10, + group: Some("navigation"), + auto: AutoPlace::Show, + }, + panel: Some(PanelTraits { + width: WidthClass::Navigation, + chrome: PanelChrome::Titled, + }), + preview: None, + edge: None, + pinned: false, +}; + +/// Every contribution of the feature. +pub const CORE: &[SystemContribution] = &[SERVERS]; + +#[cfg(test)] +#[path = "plugin_tests.rs"] +mod tests; diff --git a/crates/connections/src/plugin_tests.rs b/crates/connections/src/plugin_tests.rs new file mode 100644 index 00000000..aa2ba094 --- /dev/null +++ b/crates/connections/src/plugin_tests.rs @@ -0,0 +1,55 @@ +//! The servers panel registers under its descriptor and the toggle action drives that id. +use super::SERVERS; +use gpui_kit::{AppContext as _, Focusable as _, TestAppContext}; +use nocterm_ui::contribution_icon; +use nocterm_workspace::test_support::{Toggle, toggle_presentation}; + +fn registered( + cx: &mut TestAppContext, +) -> ( + gpui_kit::AnyWindowHandle, + gpui_kit::Entity, +) { + let (window, workspace, _, installed) = crate::test_support::workspace(cx); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + crate::register(&installed, workspace, window, cx).unwrap(); + }); + }) + .unwrap(); + cx.run_until_parked(); + (window, workspace) +} + +#[gpui_kit::test] +fn the_sidebar_toggle_shows_the_descriptors_title_and_icon(cx: &mut TestAppContext) { + let (_, workspace) = registered(cx); + cx.update(|cx| { + let workspace = workspace.read(cx); + assert!(workspace.is_registered(&SERVERS.id)); + assert_eq!( + toggle_presentation(workspace, Toggle::Navigation(0), cx), + Some((SERVERS.title.into(), contribution_icon(SERVERS.icon))) + ); + }); +} + +#[gpui_kit::test] +fn the_toggle_action_hides_the_shown_servers_and_shows_them_again(cx: &mut TestAppContext) { + let (window, workspace) = registered(cx); + let dispatch = |cx: &mut TestAppContext| { + cx.update_window(window, |_, window, cx| { + window.focus(&workspace.read(cx).focus_handle(cx), cx); + window.dispatch_action(Box::new(crate::ToggleServers), cx); + }) + .unwrap(); + cx.run_until_parked(); + }; + let open = + |cx: &mut TestAppContext| cx.update(|cx| workspace.read(cx).panel_is_open(&SERVERS.id)); + assert!(open(cx), "the sidebar opens on its first panel"); + dispatch(cx); + assert!(!open(cx)); + dispatch(cx); + assert!(open(cx)); +} diff --git a/crates/connections/src/prepared.rs b/crates/connections/src/prepared.rs index 76fff3be..d1e3fb63 100644 --- a/crates/connections/src/prepared.rs +++ b/crates/connections/src/prepared.rs @@ -1,5 +1,8 @@ //! Prepared documents and injected collaborators, installed without foreground native acquisition. -use crate::{Connections, FactsPrepared, ObservationDeps}; +use crate::{ + Connections, FactsPrepared, NoSavedPasswords, ObservationDeps, SavedPasswords, + ServerAgentAccess, +}; use nocterm_connections_model::{ draft::EditorDefaults, facts::Known, @@ -8,16 +11,51 @@ use nocterm_connections_model::{ repository::LoadedConnections, value_ports::{ConnectionClock, ProfileIds, SuppliedValues}, }; +use nocterm_ssh_keys::{NoVaultSshKeys, VaultSshKeys}; use nocterm_store::SnapshotWriter; use std::sync::Arc; #[derive(Clone)] pub struct EditorInputs { defaults: EditorDefaults, ids: Arc, + keys: Arc, + passwords: Arc, + agent_access: Option>, } impl EditorInputs { + /// Inputs of a window without a vault: its key and password choices report that. pub fn new(defaults: EditorDefaults, ids: Arc) -> Self { - Self { defaults, ids } + Self { + defaults, + ids, + keys: Arc::new(NoVaultSshKeys), + passwords: Arc::new(NoSavedPasswords), + agent_access: None, + } + } + /// A password typed in the editor is kept in `passwords`. + pub fn with_passwords(mut self, passwords: Arc) -> Self { + self.passwords = passwords; + self + } + pub(crate) fn passwords(&self) -> &Arc { + &self.passwords + } + /// The keys the editor offers are those of `keys`. + pub fn with_keys(mut self, keys: Arc) -> Self { + self.keys = keys; + self + } + pub(crate) fn keys(&self) -> &Arc { + &self.keys + } + /// The editor offers Agent access of a saved server through `access`. + pub fn with_agent_access(mut self, access: Arc) -> Self { + self.agent_access = Some(access); + self + } + pub(crate) fn agent_access(&self) -> Option<&Arc> { + self.agent_access.as_ref() } pub fn defaults(&self) -> &EditorDefaults { &self.defaults @@ -44,42 +82,101 @@ impl ConnectionWriters { } } } +/// What the connections need besides documents and writers: the same for the saved list of +/// this computer and for a list kept only in memory, so a window can be given either. +#[derive(Clone)] +pub struct ConnectionInputs { + editor: EditorInputs, + clock: Arc, + observations: ObservationDeps, +} +impl ConnectionInputs { + pub fn new( + editor: EditorInputs, + clock: Arc, + observations: ObservationDeps, + ) -> Self { + Self { + editor, + clock, + observations, + } + } + /// The editor offers the keys of `keys` instead of reporting no vault. + pub fn with_keys(mut self, keys: Arc) -> Self { + self.editor = self.editor.with_keys(keys); + self + } + /// The editor keeps a typed password in `passwords` instead of reporting no vault. + pub fn with_passwords(mut self, passwords: Arc) -> Self { + self.editor = self.editor.with_passwords(passwords); + self + } + /// The editor offers Agent access of a saved server through `access`. + pub fn with_agent_access(mut self, access: Arc) -> Self { + self.editor = self.editor.with_agent_access(access); + self + } +} pub struct PreparedConnections { documents: LoadedConnections, writers: Option, - inputs: EditorInputs, - clock: Arc, - observations: ObservationDeps, + inputs: ConnectionInputs, } impl PreparedConnections { pub fn new( documents: LoadedConnections, writers: ConnectionWriters, - inputs: EditorInputs, - clock: Arc, - observations: ObservationDeps, + inputs: ConnectionInputs, ) -> Self { Self { documents, writers: Some(writers), inputs, - clock, - observations, } } - pub fn memory(defaults: EditorDefaults) -> Self { - let values = Arc::new(SuppliedValues::new(1, 0)); + /// An empty list kept only in memory, for a list that something else keeps: it writes + /// nothing, and new connections get the identifiers `inputs` supplies. + pub fn volatile(inputs: ConnectionInputs) -> Self { Self { documents: LoadedConnections::default(), writers: None, - inputs: EditorInputs::new(defaults, values.clone()), - clock: values, - observations: ObservationDeps::offline(), + inputs, } } + /// The inputs these connections were prepared with, to prepare another list the same way. + pub fn inputs(&self) -> ConnectionInputs { + self.inputs.clone() + } + /// The editor offers the keys of `keys` instead of reporting no vault. + pub fn with_keys(mut self, keys: Arc) -> Self { + self.inputs = self.inputs.with_keys(keys); + self + } + /// The editor keeps a typed password in `passwords` instead of reporting no vault. + pub fn with_passwords(mut self, passwords: Arc) -> Self { + self.inputs = self.inputs.with_passwords(passwords); + self + } + pub fn memory(defaults: EditorDefaults) -> Self { + Self::memory_with_keys(defaults, Arc::new(NoVaultSshKeys)) + } + pub fn memory_with_keys(defaults: EditorDefaults, keys: Arc) -> Self { + let values = Arc::new(SuppliedValues::new(1, 0)); + Self::volatile(ConnectionInputs::new( + EditorInputs::new(defaults, values.clone()).with_keys(keys), + values, + ObservationDeps::offline(), + )) + } pub(crate) fn into_owners(self) -> (Connections, EditorInputs, FactsPrepared) { let profiles = self.documents.profiles; let recents = self.documents.recents; + let ConnectionInputs { + editor, + clock, + observations, + } = self.inputs; let (connections, facts) = match self.writers { Some(writers) => ( Connections::prepared( @@ -88,15 +185,15 @@ impl PreparedConnections { writers.profiles, writers.recents, self.documents.read_only, - self.clock, + clock, ), - FactsPrepared::new(self.documents.known, writers.facts, self.observations), + FactsPrepared::new(self.documents.known, writers.facts, observations), ), None => ( - Connections::memory_with_clock(profiles, recents, self.clock), - FactsPrepared::in_memory(self.observations), + Connections::memory_with_clock(profiles, recents, clock), + FactsPrepared::in_memory(observations), ), }; - (connections, self.inputs, facts) + (connections, editor, facts) } } diff --git a/crates/connections/src/probe_sources.rs b/crates/connections/src/probe_sources.rs index 96b052ae..77dfe364 100644 --- a/crates/connections/src/probe_sources.rs +++ b/crates/connections/src/probe_sources.rs @@ -7,6 +7,7 @@ use nocterm_connections_store::{ ProbeSourceIdentity, ProbeSourceLease, }; use nocterm_session::{RemoteFs, Target}; +use nocterm_ssh_keys::authorized_keys::{ConnectedHost, HostIdentity}; use nocterm_workspace::{ConnectionFactSession, ExpectedSession, Workspace}; use std::{ cell::{Cell, RefCell}, @@ -14,6 +15,10 @@ use std::{ rc::Rc, sync::{Arc, Weak}, }; +/// Told once for every session of a saved connection that comes up, with its file system. +pub trait HostObserver { + fn connected(&self, host: ConnectedHost, source: ProbeSourceLease, cx: &mut App); +} struct OriginalSource { original: Rc, expected: ExpectedSession, @@ -100,6 +105,18 @@ impl ProjectedSource { fs: snapshot.session.fs?, }) } + fn announcement(&self) -> ConnectedHost { + ConnectedHost { + connection: self.association.to_string(), + identity: HostIdentity { + user: self.target.user.clone(), + host: self.target.host.clone(), + host_key: String::new(), + }, + port: self.target.port, + fs: self.fs.clone(), + } + } fn fresh_slot(&self) -> SourceSlot { let authority = OriginalSource { original: self.original.clone(), @@ -136,14 +153,19 @@ impl SourceSlot { } } impl ProbeSource { - fn reproject(&mut self, connections: &Entity, cx: &App) -> Vec { + fn reproject( + &mut self, + connections: &Entity, + arrived: &mut Vec<(ConnectedHost, ProbeSourceLease)>, + cx: &App, + ) -> Vec { let Some(workspace) = self.workspace.upgrade() else { return Vec::new(); }; let projected: Vec<_> = workspace .read(cx) - .items() - .filter_map(|item| ProjectedSource::read(item, connections, cx)) + .items(cx) + .filter_map(|item| ProjectedSource::read(item.as_ref(), connections, cx)) .collect(); let present: HashSet<_> = projected.iter().map(|row| row.expected.item()).collect(); self.leases.retain(|id, slot| { @@ -167,7 +189,9 @@ impl ProbeSource { { old.lease.revoke(); } - self.leases.insert(id, projection.fresh_slot()); + let slot = projection.fresh_slot(); + arrived.push((projection.announcement(), slot.lease.clone())); + self.leases.insert(id, slot); } self.leases .get(&id) @@ -180,8 +204,12 @@ impl ProbeSource { pub(crate) struct ProbeSources { sources: RefCell>, rotation: Cell, + observer: RefCell>>, } impl ProbeSources { + pub(crate) fn bind_observer(&self, observer: Rc) { + self.observer.borrow_mut().get_or_insert(observer); + } pub(crate) fn register(&self, workspace: WeakEntity) { let mut sources = self.sources.borrow_mut(); if !sources @@ -201,6 +229,7 @@ impl ProbeSources { facts: &Entity, cx: &mut App, ) { + let observer = self.observer.borrow().clone(); let mut sources = self.sources.borrow_mut(); sources.retain(|source| source.workspace.upgrade().is_some()); let count = sources.len(); @@ -208,7 +237,13 @@ impl ProbeSources { for offset in 0..count { let index = (start + offset) % count; let source = &mut sources[index]; - let requests = source.reproject(connections, cx); + let mut arrived = Vec::new(); + let requests = source.reproject(connections, &mut arrived, cx); + if let Some(observer) = observer.as_ref() { + for (host, source) in arrived { + observer.connected(host, source, cx); + } + } let length = requests.len(); if length == 0 { source.cursor = 0; diff --git a/crates/connections/src/probe_sources/tests.rs b/crates/connections/src/probe_sources/tests.rs index e2e098f1..35f86a64 100644 --- a/crates/connections/src/probe_sources/tests.rs +++ b/crates/connections/src/probe_sources/tests.rs @@ -90,7 +90,11 @@ impl Scene { gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { Workspace::new( - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)), + nocterm_workspace::test_support::deps_for_core( + &[crate::plugin::CORE], + &[], + cx, + ), window, cx, ) diff --git a/crates/connections/src/probe_sources/tests/window_capacity.rs b/crates/connections/src/probe_sources/tests/window_capacity.rs index 71f4eea9..4d481a4e 100644 --- a/crates/connections/src/probe_sources/tests/window_capacity.rs +++ b/crates/connections/src/probe_sources/tests/window_capacity.rs @@ -49,7 +49,7 @@ fn fully_registered_items_changes_scan_after_workspace_update_has_returned( let scene = Scene::new(false, cx); cx.update_window(scene.window, |_, window, cx| { scene.workspace.update(cx, |workspace, cx| { - crate::register(&scene.installed, workspace, window, cx) + crate::register(&scene.installed, workspace, window, cx).unwrap(); }) }) .unwrap(); diff --git a/crates/connections/src/saved_password.rs b/crates/connections/src/saved_password.rs new file mode 100644 index 00000000..88f9e614 --- /dev/null +++ b/crates/connections/src/saved_password.rs @@ -0,0 +1,96 @@ +//! The port through which the connection editor keeps the password a saved connection signs in +//! with, in the vault of its window: the account's vault on the hub in a hub window, this +//! computer's vault otherwise. A saved password may sign in to several servers; the editor +//! offers the vault's passwords and attaches the one chosen to the connection's address. The +//! composition root implements it over that vault. +use futures::future::BoxFuture; +use nocterm_session::{CredentialId, Secret, Target}; + +/// Which vault a window keeps its passwords and keys in, as the editor names it. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub enum VaultPlace { + /// The vault of this computer. + #[default] + ThisComputer, + /// The account's vault on the window's hub, sealed end to end: every computer of the + /// account signs in with what it keeps. + Account, +} + +/// Why a password was not kept, in words the editor shows. +#[derive(Debug, thiserror::Error)] +pub enum PasswordNotKept { + #[error("Unlock the vault to keep the password.")] + Locked, + #[error("This window has no vault to keep the password in.")] + Unavailable, + #[error("The saved password is no longer in the vault.")] + Missing, + #[error("The vault could not keep the password.")] + Vault(#[source] Box), +} + +/// A password the vault keeps, as the editor offers it to sign in with. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct SavedPassword { + pub id: CredentialId, + pub label: String, + /// How many servers it signs in to. + pub servers: usize, +} + +/// The vault the window keeps passwords in. +pub trait SavedPasswords: Send + Sync { + /// Which vault it is. + fn place(&self) -> VaultPlace; + /// The passwords the vault keeps, in its order. + fn list(&self) -> BoxFuture<'static, Result, PasswordNotKept>>; + /// Keeps `password` for `target`: as the new password of the record `replacing`, which + /// keeps its name and every server it signs in to and gains `target`, or in a new record + /// named `label` that signs in to `target` only. Resolves to the record. + fn keep( + &self, + replacing: Option, + label: String, + target: Target, + password: Secret, + ) -> BoxFuture<'static, Result>; + /// Lets the saved password `id` sign in to `target` as well. + fn attach( + &self, + id: CredentialId, + target: Target, + ) -> BoxFuture<'static, Result<(), PasswordNotKept>>; +} + +/// The port of a window composed without a vault: nothing is kept. +#[derive(Clone, Copy, Debug, Default)] +pub struct NoSavedPasswords; + +impl SavedPasswords for NoSavedPasswords { + fn place(&self) -> VaultPlace { + VaultPlace::ThisComputer + } + + fn list(&self) -> BoxFuture<'static, Result, PasswordNotKept>> { + Box::pin(std::future::ready(Ok(Vec::new()))) + } + + fn keep( + &self, + _: Option, + _: String, + _: Target, + _: Secret, + ) -> BoxFuture<'static, Result> { + Box::pin(std::future::ready(Err(PasswordNotKept::Unavailable))) + } + + fn attach( + &self, + _: CredentialId, + _: Target, + ) -> BoxFuture<'static, Result<(), PasswordNotKept>> { + Box::pin(std::future::ready(Err(PasswordNotKept::Unavailable))) + } +} diff --git a/crates/connections/src/session_intents.rs b/crates/connections/src/session_intents.rs index 33ebb051..1ce58fd4 100644 --- a/crates/connections/src/session_intents.rs +++ b/crates/connections/src/session_intents.rs @@ -1,12 +1,41 @@ //! Feature-owned conversion and foreground open intent; background routes skip recents. use crate::Installed; -use gpui_kit::{App, SharedString, WeakEntity, Window}; +use gpui_kit::{App, EntityId, SharedString, WeakEntity, Window}; use nocterm_connections_model::{ jump_route::{JumpRouteError, resolve_jump_route}, profiles::{Profile, ProfileId, Profiles, Recent}, }; use nocterm_session::Target; use nocterm_workspace::{SessionSpec, Workspace}; + +/// Resolve the original saved workspace server without opening a connection. +/// Profile grouping is insufficient: its destination and ordered route must +/// still match the saved workspace exactly before any authentication or I/O. +pub fn resolve_parent_spec( + host: &nocterm_project_model::HostLocator, + profiles: &Profiles, +) -> Result { + use nocterm_project_model::HostLocator; + use nocterm_workspace_ports::ProjectConnectionError; + let HostLocator::Ssh { + profile, + target, + via, + } = host + else { + return Err(ProjectConnectionError::UnsupportedHost); + }; + let saved = profiles + .iter() + .find(|saved| saved.id.to_string() == *profile) + .ok_or(ProjectConnectionError::MissingProfile)?; + let spec = spec_for_profile(saved, profiles).map_err(ProjectConnectionError::invalid_route)?; + if spec.target != *target || spec.address().via != *via { + return Err(ProjectConnectionError::ChangedTargetOrRoute); + } + Ok(spec) +} + /// The session to open for `profile`, with its jump hosts resolved against /// `profiles`. Fails when a jump host is gone, loops back, or the route is too long. pub fn spec_for_profile( @@ -90,6 +119,29 @@ pub(crate) fn open_profile( } } } +/// Opens a saved connection without a tab and returns the session's item, or reports why its +/// route cannot be resolved. It signs in as a tab would and is not recorded among recents. +pub(crate) fn open_background( + installed: &Installed, + workspace: &WeakEntity, + profile: &Profile, + window: &mut Window, + cx: &mut App, +) -> Option { + let spec = match spec_for_profile(profile, installed.connections().read(cx).profiles(cx)) { + Ok(spec) => spec, + Err(error) => { + report_route_error(&error, window, cx); + return None; + } + }; + workspace + .update(cx, |workspace, cx| { + workspace.open_background_session(spec, window, cx) + }) + .ok() + .flatten() +} /// Records the foreground intent before asking the existing workspace session factory. pub fn connect( installed: &Installed, diff --git a/crates/connections/src/session_intents/tests.rs b/crates/connections/src/session_intents/tests.rs index 2ec2ec09..54c5abe6 100644 --- a/crates/connections/src/session_intents/tests.rs +++ b/crates/connections/src/session_intents/tests.rs @@ -103,3 +103,117 @@ fn a_typed_address_hop_signs_in_automatically_without_a_credential() { assert_eq!(spec.jumps[0].host.auth, nocterm_session::Auth::Auto); assert_eq!(spec.jumps[0].credential, None); } + +fn workspace_parent( + profile: &Profile, + via: Vec, +) -> nocterm_project_model::HostLocator { + nocterm_project_model::HostLocator::Ssh { + profile: profile.id.to_string(), + target: profile.target.clone(), + via, + } +} + +#[test] +fn workspace_reconnect_keeps_the_saved_authentication_and_exact_ordered_jump_route() { + let first = saved("first-workspace-hop", "first.example"); + let mut second = saved("second-workspace-hop", "second.example"); + second.jump_hosts = vec![JumpHostEntry::Profile(first.id)]; + let mut destination = saved("workspace-destination", "destination.example"); + destination.jump_hosts = vec![JumpHostEntry::Profile(second.id)]; + let mut profiles = Profiles::default(); + for profile in [&first, &second, &destination] { + profiles.upsert(profile.clone()); + } + let expected = workspace_parent( + &destination, + vec![first.target.clone(), second.target.clone()], + ); + let spec = resolve_parent_spec(&expected, &profiles).unwrap(); + assert_eq!(spec.address().via, vec![first.target, second.target]); + assert_eq!(spec.auth, destination.auth); + assert_eq!(spec.credential, destination.credential); + assert_eq!(spec.jumps[0].credential, first.credential); + assert_eq!(spec.jumps[1].credential, second.credential); +} + +#[test] +fn workspace_reconnect_refuses_changed_destinations_and_routes_before_opening() { + use nocterm_workspace_ports::ProjectConnectionError; + let mut destination = saved("checked-workspace-destination", "original.example"); + let hop = nocterm_session::Target::new("ops", "hop.example", 22); + destination.jump_hosts = vec![JumpHostEntry::Address(hop.clone())]; + let expected = workspace_parent(&destination, vec![hop.clone()]); + let mut profiles = Profiles::default(); + let mut changed = destination.clone(); + changed.target.host = "different.example".into(); + profiles.upsert(changed); + assert!(matches!( + resolve_parent_spec(&expected, &profiles), + Err(ProjectConnectionError::ChangedTargetOrRoute) + )); + destination.jump_hosts.clear(); + profiles.upsert(destination.clone()); + assert!(matches!( + resolve_parent_spec(&expected, &profiles), + Err(ProjectConnectionError::ChangedTargetOrRoute) + )); + destination.jump_hosts = vec![ + JumpHostEntry::Address(hop.clone()), + JumpHostEntry::Address(nocterm_session::Target::new("ops", "second.example", 22)), + ]; + profiles.upsert(destination.clone()); + let reversed = workspace_parent( + &destination, + vec![ + nocterm_session::Target::new("ops", "second.example", 22), + hop, + ], + ); + assert!(matches!( + resolve_parent_spec(&reversed, &profiles), + Err(ProjectConnectionError::ChangedTargetOrRoute) + )); +} + +#[test] +fn workspace_reconnect_distinguishes_removed_profiles_from_invalid_routes() { + use nocterm_workspace_ports::ProjectConnectionError; + use std::error::Error as _; + let hop = saved("deleted-workspace-hop", "hop.example"); + let mut destination = saved("deleted-workspace-destination", "destination.example"); + destination.jump_hosts = vec![JumpHostEntry::Profile(hop.id)]; + let expected = workspace_parent(&destination, vec![hop.target]); + let mut profiles = Profiles::default(); + assert!(matches!( + resolve_parent_spec(&expected, &profiles), + Err(ProjectConnectionError::MissingProfile) + )); + profiles.upsert(destination); + let error = resolve_parent_spec(&expected, &profiles).err().unwrap(); + assert!(matches!(error, ProjectConnectionError::InvalidRoute(_))); + assert!( + error + .source() + .unwrap() + .source() + .unwrap() + .downcast_ref::() + .is_some() + ); +} + +#[test] +fn saved_workspace_profile_resolution_requires_a_saved_profile() { + use nocterm_project_model::HostLocator; + use nocterm_workspace_ports::ProjectConnectionError; + let direct = HostLocator::SshDirect { + target: nocterm_session::Target::new("ops", "manual.example", 22), + via: vec![], + }; + assert!(matches!( + resolve_parent_spec(&direct, &Profiles::default()), + Err(ProjectConnectionError::UnsupportedHost) + )); +} diff --git a/crates/connections/src/test_support.rs b/crates/connections/src/test_support.rs index 1a44ee5e..13f78be5 100644 --- a/crates/connections/src/test_support.rs +++ b/crates/connections/src/test_support.rs @@ -15,6 +15,131 @@ pub(crate) type Opened = Rc>>; pub(crate) fn workspace( cx: &mut TestAppContext, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + workspace_with_keys(cx, std::sync::Arc::new(nocterm_ssh_keys::NoVaultSshKeys)) +} + +/// A workspace whose connection editor offers the keys of `keys`. +pub(crate) fn workspace_with_keys( + cx: &mut TestAppContext, + keys: std::sync::Arc, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + workspace_over(cx, memory(keys)) +} + +/// A workspace of a hub window, whose editor offers Agent access through `access`. +pub(crate) fn workspace_with_agent_access( + cx: &mut TestAppContext, + access: std::sync::Arc, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + let prepared = memory(std::sync::Arc::new(nocterm_ssh_keys::NoVaultSshKeys)); + let inputs = prepared.inputs().with_agent_access(access); + workspace_over(cx, crate::PreparedConnections::volatile(inputs)) +} + +/// A workspace whose connection editor keeps a typed password in `passwords`. +pub(crate) fn workspace_with_passwords( + cx: &mut TestAppContext, + passwords: std::sync::Arc, +) -> (AnyWindowHandle, Entity, Opened, Installed) { + let prepared = memory(std::sync::Arc::new(nocterm_ssh_keys::NoVaultSshKeys)); + let inputs = prepared.inputs().with_passwords(passwords); + workspace_over(cx, crate::PreparedConnections::volatile(inputs)) +} + +/// A vault of `place` that records every password kept in it, or refuses with `locked`. +pub(crate) struct ScriptedPasswords { + place: crate::VaultPlace, + pub(crate) locked: std::sync::atomic::AtomicBool, + /// The passwords the vault offers. + pub(crate) saved: std::sync::Mutex>, + /// `(record, address)` of each saved password attached to an address, in order. + pub(crate) attached: + std::sync::Mutex>, + /// `(replacing, label, target, password)` of each password kept, in order. + pub(crate) kept: std::sync::Mutex< + Vec<( + Option, + String, + nocterm_session::Target, + String, + )>, + >, +} + +impl ScriptedPasswords { + pub(crate) fn new(place: crate::VaultPlace) -> Self { + Self { + place, + locked: Default::default(), + saved: Default::default(), + attached: Default::default(), + kept: Default::default(), + } + } +} + +impl crate::SavedPasswords for ScriptedPasswords { + fn place(&self) -> crate::VaultPlace { + self.place + } + + fn list( + &self, + ) -> futures::future::BoxFuture< + 'static, + Result, crate::PasswordNotKept>, + > { + Box::pin(std::future::ready(Ok(self.saved.lock().unwrap().clone()))) + } + + fn attach( + &self, + id: nocterm_session::CredentialId, + target: nocterm_session::Target, + ) -> futures::future::BoxFuture<'static, Result<(), crate::PasswordNotKept>> { + if self.locked.load(std::sync::atomic::Ordering::SeqCst) { + return Box::pin(std::future::ready(Err(crate::PasswordNotKept::Locked))); + } + self.attached.lock().unwrap().push((id, target)); + Box::pin(std::future::ready(Ok(()))) + } + + fn keep( + &self, + replacing: Option, + label: String, + target: nocterm_session::Target, + password: nocterm_session::Secret, + ) -> futures::future::BoxFuture< + 'static, + Result, + > { + if self.locked.load(std::sync::atomic::Ordering::SeqCst) { + return Box::pin(std::future::ready(Err(crate::PasswordNotKept::Locked))); + } + let mut kept = self.kept.lock().unwrap(); + let id = replacing.unwrap_or_else(|| { + nocterm_session::CredentialId::from_bytes([u8::try_from(kept.len() + 1).unwrap(); 16]) + }); + kept.push((replacing, label, target, password.expose().to_owned())); + Box::pin(std::future::ready(Ok(id))) + } +} + +fn memory(keys: std::sync::Arc) -> crate::PreparedConnections { + crate::PreparedConnections::memory_with_keys( + nocterm_connections_model::draft::EditorDefaults { + user: Some("tester".into()), + home: None, + }, + keys, + ) +} + +fn workspace_over( + cx: &mut TestAppContext, + prepared: crate::PreparedConnections, ) -> (AnyWindowHandle, Entity, Opened, Installed) { let opened = Opened::default(); let calls = opened.clone(); @@ -28,10 +153,7 @@ pub(crate) fn workspace( cx, ); let installed = crate::install( - crate::PreparedConnections::memory(nocterm_connections_model::draft::EditorDefaults { - user: Some("tester".into()), - home: None, - }), + prepared, &ready, &nocterm_store::ShutdownCoordinator::default(), cx, @@ -40,8 +162,11 @@ pub(crate) fn workspace( let (window, workspace) = gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { - let deps = - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)); + let deps = nocterm_workspace::test_support::deps_for_core( + &[crate::plugin::CORE], + &[], + cx, + ); let mut workspace = Workspace::new(deps, window, cx); workspace .set_session_opener(move |_, spec, _, _| calls.borrow_mut().push(spec)); @@ -118,3 +243,98 @@ fn model_inputs(connections: &Connections, cx: &gpui_kit::App) -> crate::EditorI values, ) } + +/// Binds a replication to `installed` that no replica is attached to: the state of an +/// account that is signed out. +pub(crate) fn unattached( + installed: &Installed, + cx: &mut TestAppContext, +) -> Entity { + use nocterm_connections_model::replica::ReplicaState; + use nocterm_connections_store::ProfileReplication; + let replication = cx.update(|cx| { + let store = installed.connections().clone(); + let writer: nocterm_store::SnapshotWriter = + std::sync::Arc::new(|_| Box::pin(async { Ok(()) })); + let document = + cx.new(|_| ProfileReplication::state_document(ReplicaState::default(), writer)); + cx.new(|cx| ProfileReplication::new(store, document, cx)) + }); + installed.bind_replication(replication.clone()); + replication +} + +/// Keeps `installed` in step with `replica`, as the application does for the chosen +/// account, and runs the first synchronization. +pub(crate) fn replicate( + installed: &Installed, + replica: std::sync::Arc, + cx: &mut TestAppContext, +) -> Entity { + let replication = unattached(installed, cx); + replication.update(cx, |replication, cx| replication.attach(replica, cx)); + cx.run_until_parked(); + replication +} + +/// An Agent access port that records each change and grants it, or refuses it with `refusal`. +/// `session` tells whether a session to the server is open; `state` is what the hub records. +#[derive(Default)] +pub(crate) struct ScriptedAgentAccess { + pub(crate) state: std::sync::Mutex>, + pub(crate) session: std::sync::atomic::AtomicBool, + pub(crate) changes: std::sync::Mutex>, + pub(crate) refusal: Option<&'static str>, +} + +impl ScriptedAgentAccess { + pub(crate) fn with_session() -> Self { + let access = Self::default(); + access.open_session(); + access + } + + pub(crate) fn open_session(&self) { + self.session + .store(true, std::sync::atomic::Ordering::SeqCst); + } + + pub(crate) fn granted(&self) -> Vec { + let changes = self.changes.lock().unwrap(); + changes.iter().map(|(_, granted)| *granted).collect() + } +} + +impl crate::ServerAgentAccess for ScriptedAgentAccess { + fn state(&self, _: &crate::AgentServer, _: &gpui_kit::App) -> Option { + *self.state.lock().unwrap() + } + + fn session_open(&self, _: &crate::AgentServer, _: &gpui_kit::App) -> bool { + self.session.load(std::sync::atomic::Ordering::SeqCst) + } + + fn set( + &self, + server: crate::AgentServer, + granted: bool, + _: &mut gpui_kit::App, + ) -> gpui_kit::Task> { + self.changes.lock().unwrap().push((server, granted)); + match self.refusal { + Some(reason) => gpui_kit::Task::ready(Err(crate::AgentAccessRefused::new(reason))), + None => { + *self.state.lock().unwrap() = granted.then_some(crate::AgentAccessState::Unchecked); + gpui_kit::Task::ready(Ok(())) + } + } + } + + fn observe( + &self, + _: Box, + _: &mut gpui_kit::App, + ) -> gpui_kit::Subscription { + gpui_kit::Subscription::new(|| {}) + } +} diff --git a/crates/connections/src/volatile_tests.rs b/crates/connections/src/volatile_tests.rs new file mode 100644 index 00000000..9c374c3e --- /dev/null +++ b/crates/connections/src/volatile_tests.rs @@ -0,0 +1,107 @@ +//! Connections kept only in memory, for a window whose list something else keeps. +use crate::{ConnectionInputs, EditorInputs, Installed, PreparedConnections, open_editor}; +use gpui_kit::{AppContext as _, TestAppContext, WindowOptions, test::TestWindowExt as _}; +use nocterm_connections_model::{ + draft::EditorDefaults, + profiles::ProfileId, + test_support::profile_id_for, + value_ports::{ProfileIds, SuppliedValues}, +}; +use nocterm_store::{SettingsConfig, ShutdownCoordinator}; +use nocterm_ui::DesignTokens; +use nocterm_workspace::Workspace; +use std::sync::Arc; + +/// Hands out one identifier, standing in for the UUIDs of the application. +struct FixedId(ProfileId); +impl ProfileIds for FixedId { + fn next(&self) -> ProfileId { + self.0 + } +} + +fn inputs(id: ProfileId) -> ConnectionInputs { + let values = Arc::new(SuppliedValues::new(1, 0)); + ConnectionInputs::new( + EditorInputs::new( + EditorDefaults { + user: Some("tester".into()), + home: None, + }, + Arc::new(FixedId(id)), + ), + values, + crate::ObservationDeps::offline(), + ) +} + +fn ready(cx: &mut gpui_kit::App) -> nocterm_ui::UiReady { + gpui_kit::init(cx); + nocterm_ui::init( + DesignTokens::builtin(), + SettingsConfig::in_memory(Default::default()).install(cx), + cx, + ) +} + +fn bindings(cx: &gpui_kit::App) -> usize { + cx.key_bindings().borrow().bindings().count() +} + +#[gpui_kit::test] +fn installing_volatile_connections_twice_adds_no_key_bindings(cx: &mut TestAppContext) { + let (before, after) = cx.update(|cx| { + let ready = ready(cx); + crate::install( + PreparedConnections::volatile(inputs(profile_id_for("a"))), + &ready, + &ShutdownCoordinator::default(), + cx, + ) + .unwrap(); + let before = bindings(cx); + for _ in 0..2 { + crate::install_volatile( + PreparedConnections::volatile(inputs(profile_id_for("a"))), + &ready, + cx, + ) + .unwrap(); + } + (before, bindings(cx)) + }); + assert_eq!(before, after); +} + +#[gpui_kit::test] +fn a_new_connection_in_volatile_connections_takes_the_supplied_identifier(cx: &mut TestAppContext) { + let id = profile_id_for("fresh"); + let (handle, workspace, installed): (_, _, Installed) = cx.update(|cx| { + cx.set_reduce_motion(true); + let ready = ready(cx); + let installed = + crate::install_volatile(PreparedConnections::volatile(inputs(id)), &ready, cx).unwrap(); + let (window, workspace) = + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let deps = nocterm_workspace::test_support::deps_for_core(&[], &[], cx); + Workspace::new(deps, window, cx) + }) + }) + .unwrap(); + (window, workspace, installed) + }); + cx.update_window(handle, |_, window, cx| { + open_editor(installed.clone(), None, workspace.downgrade(), window, cx); + window.render_frame(cx); + window.input("host.test", cx); + window.press("enter", cx); + }) + .unwrap(); + cx.run_until_parked(); + cx.read(|cx| { + let connections = installed.connections().read(cx); + assert!(connections.profiles(cx).get(id).is_some()); + assert_eq!(connections.profiles(cx).iter().count(), 1); + }); +} diff --git a/crates/containers-service/Cargo.toml b/crates/containers-service/Cargo.toml new file mode 100644 index 00000000..f6cfa752 --- /dev/null +++ b/crates/containers-service/Cargo.toml @@ -0,0 +1,26 @@ +[package] +name = "nocterm-containers-service" +description = "One shared watch of each host's containers and images, relisted only for events that change them." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +gpui.workspace = true +nocterm-containers.workspace = true +nocterm-session.workspace = true +futures.workspace = true + +[features] +test-support = [] + +[lints] +workspace = true + +[dev-dependencies] +nocterm-session = { workspace = true, features = ["test-support"] } +gpui = { workspace = true, features = ["test-support"] } diff --git a/crates/containers-service/src/host.rs b/crates/containers-service/src/host.rs new file mode 100644 index 00000000..d330ad7b --- /dev/null +++ b/crates/containers-service/src/host.rs @@ -0,0 +1,175 @@ +//! What one host connection's engine last reported, and the watch that keeps it current. + +use std::sync::Arc; + +use futures::channel::mpsc; +use gpui::{Context, Task}; +use nocterm_containers::{Changes, Container, ContainersError, Engine, Image, Snapshot}; +use nocterm_session::HostExec; + +use crate::watch; + +/// Where the listing of a host stands. +#[derive(Debug)] +pub enum ListingStatus { + /// The first listing, or one after a failure, is under way. + Loading, + Ready, + /// The last attempt failed; unless the host has no engine, another follows shortly. + Failed(ContainersError), +} + +impl ListingStatus { + /// Whether no watch follows: the host has no engine, or cannot run programs. + fn is_final(&self) -> bool { + matches!( + self, + Self::Failed(ContainersError::NotInstalled | ContainersError::Unsupported) + ) + } +} + +/// A listing that replaces one or both lists of the snapshot. +pub(crate) enum Listed { + Both(Snapshot), + Containers(Vec), + Images(Vec), +} + +/// The containers and images of one host connection, shared by everyone watching it. +pub struct HostContainers { + exec: Arc, + engine: Option, + snapshot: Snapshot, + status: ListingStatus, + /// What the running watch is asked to list again; closed once it has ended. + requests: mpsc::UnboundedSender, + _watch: Task<()>, +} + +impl HostContainers { + pub(crate) fn new(exec: Arc, cx: &mut Context) -> Self { + let (requests, watch) = Self::spawn(exec.clone(), None, cx); + Self { + exec, + engine: None, + snapshot: Snapshot::default(), + status: ListingStatus::Loading, + requests, + _watch: watch, + } + } + + fn spawn( + exec: Arc, + engine: Option, + cx: &mut Context, + ) -> (mpsc::UnboundedSender, Task<()>) { + let (requests, asked) = mpsc::unbounded(); + let task = + cx.spawn(async move |this, cx| watch::watch(this, exec, engine, asked, cx).await); + (requests, task) + } + + /// How programs run on this host. + pub fn exec(&self) -> &Arc { + &self.exec + } + + /// The engine the host was found to have. + pub fn engine(&self) -> Option { + self.engine + } + + pub fn snapshot(&self) -> &Snapshot { + &self.snapshot + } + + pub fn status(&self) -> &ListingStatus { + &self.status + } + + /// Lists `changes` again soon, as after an action on the host or when the person asks; + /// a watch that has stopped starts again, looking for the engine anew. + pub fn relist(&mut self, changes: Changes, cx: &mut Context) { + if changes.is_empty() { + return; + } + if self.requests.unbounded_send(changes).is_ok() { + return; + } + self.restart(cx); + } + + /// Starts again at once a watch that is waiting out a failure, or has stopped. + pub(crate) fn revive(&mut self, cx: &mut Context) { + if matches!(self.status, ListingStatus::Failed(_)) { + self.relist(Changes::ALL, cx); + } + } + + fn restart(&mut self, cx: &mut Context) { + let engine = if self.status.is_final() { + None + } else { + self.engine + }; + let (requests, watch) = Self::spawn(self.exec.clone(), engine, cx); + self.requests = requests; + self._watch = watch; + self.set_status(ListingStatus::Loading, cx); + } + + pub(crate) fn listed(&mut self, engine: Engine, listed: Listed, cx: &mut Context) { + let changed = self.engine != Some(engine); + self.engine = Some(engine); + let changed = changed + | match listed { + Listed::Both(snapshot) => replace(&mut self.snapshot, snapshot), + Listed::Containers(containers) => { + replace(&mut self.snapshot.containers, containers) + } + Listed::Images(images) => replace(&mut self.snapshot.images, images), + }; + if changed { + cx.notify(); + } + self.set_status(ListingStatus::Ready, cx); + } + + /// The watch's attempt ended with `outcome`; whether to try again. + pub(crate) fn ended( + &mut self, + outcome: Result<(), ContainersError>, + cx: &mut Context, + ) -> bool { + // An ended event stream leaves the listing standing. + let Err(error) = outcome else { return true }; + self.set_status(ListingStatus::Failed(error), cx); + !self.status.is_final() + } + + fn set_status(&mut self, status: ListingStatus, cx: &mut Context) { + // Native error sources have no equality contract. Deduplicate only the + // visible status category and text; recovery always matches typed variants. + let unchanged = match (&self.status, &status) { + (ListingStatus::Failed(previous), ListingStatus::Failed(next)) => { + std::mem::discriminant(previous) == std::mem::discriminant(next) + && previous.to_string() == next.to_string() + } + (previous, next) => std::mem::discriminant(previous) == std::mem::discriminant(next), + }; + if !unchanged { + self.status = status; + cx.notify(); + } + } +} + +fn replace(slot: &mut T, value: T) -> bool { + if *slot == value { + return false; + } + *slot = value; + true +} diff --git a/crates/containers-service/src/lib.rs b/crates/containers-service/src/lib.rs new file mode 100644 index 00000000..eba2a53f --- /dev/null +++ b/crates/containers-service/src/lib.rs @@ -0,0 +1,76 @@ +//! One shared watch of each host's containers and images, shared by every window. +//! +//! Owns: [`ContainerHosts`], the registry that hands every caller asking about the same host +//! connection the same [`HostContainers`]; the engine found there, its last listings and +//! where listing stands; the one event stream followed per connection; and the rules for +//! when to list again: only for events that change a listing, only the listing they change, +//! once per burst, and every [`RETRY`] where there is no event stream. +//! Does not own: command lines, listing and event formats (`nocterm-containers`), host +//! execution or connections (`HostExec`), which host a window looks at, actions and views +//! (`nocterm-containers-ui`). +//! Ports used: `nocterm_session::HostExec`. Ports provided: [`ContainerHosts`] and +//! [`HostContainers`]. Settings section: none. Globals: none. +//! Invariants: a host connection, identified by its executor, has at most one watch, and so +//! one `events` process, however many windows show it; the watch lives while someone holds +//! its entity and stops its programs when the last one lets go; an event that changes no +//! listing runs no program. + +mod host; +#[cfg(any(test, feature = "test-support"))] +pub mod test_support; +mod watch; + +use std::{ + cell::RefCell, + rc::Rc, + sync::{Arc, Weak}, +}; + +use gpui::{App, AppContext as _, Entity, WeakEntity}; +use nocterm_session::HostExec; + +pub use host::{HostContainers, ListingStatus}; +pub use watch::{RETRY, SETTLE}; + +/// The watched hosts, by connection. Cloning shares the registry. +#[derive(Clone, Default)] +pub struct ContainerHosts { + watched: Rc>>, +} + +struct Watched { + exec: Weak, + host: WeakEntity, +} + +impl ContainerHosts { + pub fn new() -> Self { + Self::default() + } + + /// The containers of the host reached through `exec`, watched once for everyone who asks + /// about the same executor. A watch that stopped after a failure starts again. + pub fn watch(&self, exec: &Arc, cx: &mut App) -> Entity { + let found = { + let mut watched = self.watched.borrow_mut(); + watched.retain(|entry| entry.host.upgrade().is_some()); + watched + .iter() + .filter(|entry| std::ptr::addr_eq(entry.exec.as_ptr(), Arc::as_ptr(exec))) + .find_map(|entry| entry.host.upgrade()) + }; + if let Some(host) = found { + host.update(cx, |host, cx| host.revive(cx)); + return host; + } + let host = cx.new(|cx| HostContainers::new(exec.clone(), cx)); + self.watched.borrow_mut().push(Watched { + exec: Arc::downgrade(exec), + host: host.downgrade(), + }); + host + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/containers-service/src/test_support.rs b/crates/containers-service/src/test_support.rs new file mode 100644 index 00000000..32f5b6e8 --- /dev/null +++ b/crates/containers-service/src/test_support.rs @@ -0,0 +1,145 @@ +//! A host with a scripted Docker, for tests of everything that watches containers. + +use futures::future::BoxFuture; +use std::{ + collections::VecDeque, + sync::{Arc, Mutex}, +}; + +use futures::FutureExt as _; +use nocterm_session::{ExecExit, ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec}; + +/// One running container named `web`. +pub const RUNNING: &str = + r#"{"ID":"a1","Image":"nginx","Names":"web","State":"running","Status":"Up 2 hours"}"#; +/// The same container, exited. +pub const EXITED: &str = + r#"{"ID":"a1","Image":"nginx","Names":"web","State":"exited","Status":"Exited (0)"}"#; +/// One image. +pub const IMAGE: &str = r#"{"ID":"sha256:1","Repository":"nginx","Tag":"1","Size":"7MB"}"#; + +/// A host whose `docker` lists what it is told to, keeps `docker events` running until the +/// test prints to it or lets it end, and does every other command it is asked. Without +/// Docker, every command is missing. +pub struct FakeDockerHost { + docker: bool, + containers: Mutex, + images: Mutex, + ran: Mutex>, + events: Mutex>, + listings: Mutex>>, +} + +impl FakeDockerHost { + pub fn new(docker: bool) -> Arc { + Arc::new(Self { + docker, + containers: Mutex::new(RUNNING.into()), + images: Mutex::new(String::new()), + ran: Mutex::default(), + events: Mutex::default(), + listings: Mutex::default(), + }) + } + + /// Suspends the next container listing after capturing its reply. + pub fn suspend_listing(&self, gate: BoxFuture<'static, ()>) { + lock(&self.listings).push_back(gate); + } + + /// Every command line run so far. + pub fn ran(&self) -> Vec { + lock(&self.ran).clone() + } + + /// How many command lines run so far start with `prefix`. + pub fn count(&self, prefix: &str) -> usize { + lock(&self.ran) + .iter() + .filter(|line| line.starts_with(prefix)) + .count() + } + + /// What `docker ps` prints from now on. + pub fn set_containers(&self, listing: &str) { + *lock(&self.containers) = listing.into(); + } + + /// What `docker images` prints from now on. + pub fn set_images(&self, listing: &str) { + *lock(&self.images) = listing.into(); + } + + /// Prints `line` and a newline to the latest event stream. + pub fn announce(&self, line: &str) { + let events = lock(&self.events); + let sink = events.last().expect("events are followed"); + let mut bytes = line.as_bytes().to_vec(); + bytes.push(b'\n'); + assert_eq!(sink.send(bytes).now_or_never(), Some(true)); + } + + /// Ends the latest event stream, as an engine whose events are unavailable does. + pub fn end_events(&self) { + let sink = lock(&self.events).pop().expect("events are followed"); + sink.finish(ExecExit::default()); + } + + /// Whether the event stream started `index`th is still being read. + pub fn following(&self, index: usize) -> bool { + !lock(&self.events)[index].is_closed() + } + + /// How many event streams are still being read. + pub fn followers(&self) -> usize { + lock(&self.events) + .iter() + .filter(|sink| !sink.is_closed()) + .count() + } +} + +/// A Docker event line of `kind` (`container` or `image`) for `action` on `id`. +pub fn docker_event(kind: &str, action: &str, id: &str) -> String { + format!(r#"{{"Type":"{kind}","Action":"{action}","Actor":{{"ID":"{id}"}}}}"#) +} + +impl HostExec for FakeDockerHost { + fn exec(&self, request: ExecRequest) -> ExecFuture { + let line = request.command_line(); + lock(&self.ran).push(line.clone()); + let (sink, output) = ExecOutput::channel(); + if self.docker && line.starts_with("docker events") { + lock(&self.events).push(sink); + return Box::pin(async { Ok(output) }); + } + let (stdout, status) = match () { + _ if !self.docker => (String::new(), 127), + _ if line.starts_with("docker ps") => (lock(&self.containers).clone(), 0), + _ if line.starts_with("docker images") => (lock(&self.images).clone(), 0), + _ if line.starts_with("docker") => (String::new(), 0), + _ => (String::new(), 127), + }; + let gate = line + .starts_with("docker ps") + .then(|| lock(&self.listings).pop_front()) + .flatten(); + Box::pin(async move { + if let Some(gate) = gate { + gate.await; + } + assert!(sink.send(stdout.into_bytes()).await); + sink.finish(ExecExit { + status: Some(status), + stderr: String::new(), + }); + Ok(output) + }) + } +} + +fn lock(mutex: &Mutex) -> std::sync::MutexGuard<'_, T> { + mutex + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) +} diff --git a/crates/containers-service/src/tests.rs b/crates/containers-service/src/tests.rs new file mode 100644 index 00000000..13de6b95 --- /dev/null +++ b/crates/containers-service/src/tests.rs @@ -0,0 +1,232 @@ +use futures::FutureExt as _; +use std::sync::Arc; + +use gpui::{Entity, TestAppContext}; +use nocterm_containers::{Changes, ContainersError, Engine, State}; +use nocterm_session::HostExec; + +use crate::{ + ContainerHosts, HostContainers, ListingStatus, RETRY, SETTLE, + test_support::{EXITED, FakeDockerHost, IMAGE, docker_event}, +}; + +fn watch( + cx: &mut TestAppContext, + hosts: &ContainerHosts, + host: &Arc, +) -> Entity { + let exec: Arc = host.clone(); + let watched = cx.update(|cx| hosts.watch(&exec, cx)); + cx.run_until_parked(); + watched +} + +fn settle(cx: &mut TestAppContext) { + cx.executor().advance_clock(SETTLE * 2); + cx.run_until_parked(); +} + +/// Lets GPUI drop entities nobody holds any more. +fn release(cx: &mut TestAppContext) { + cx.update(|_| {}); + cx.run_until_parked(); +} + +fn listings(host: &FakeDockerHost) -> (usize, usize) { + (host.count("docker ps"), host.count("docker images")) +} + +#[gpui::test] +fn every_window_on_a_host_shares_one_watch(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let first = watch(cx, &hosts, &local); + let second = watch(cx, &hosts, &local); + assert_eq!(first.entity_id(), second.entity_id()); + assert_eq!(local.count("docker events"), 1); + assert_eq!(listings(&local), (1, 1)); + first.read_with(cx, |host, _| { + assert_eq!(host.engine(), Some(Engine::Docker)); + assert!(matches!(host.status(), ListingStatus::Ready)); + assert_eq!(host.snapshot().running(), 1); + }); + + let other = FakeDockerHost::new(true); + let elsewhere = watch(cx, &hosts, &other); + assert_ne!(elsewhere.entity_id(), first.entity_id()); + assert_eq!(other.count("docker events"), 1); +} + +#[gpui::test] +fn health_checks_list_nothing_again(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let _watched = watch(cx, &hosts, &local); + for _ in 0..20 { + for action in [ + "exec_create: pg_isready", + "exec_start: pg_isready", + "exec_die", + ] { + local.announce(&docker_event("container", action, "pg")); + } + settle(cx); + } + local.announce(&docker_event("container", "health_status: healthy", "pg")); + settle(cx); + assert_eq!(listings(&local), (2, 1), "a new health status, once"); + local.announce(&docker_event("container", "health_status: healthy", "pg")); + settle(cx); + assert_eq!(listings(&local), (2, 1), "the same status again is no news"); +} + +#[gpui::test] +fn a_change_lists_only_what_it_changed_once_per_burst(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let watched = watch(cx, &hosts, &local); + + local.set_containers(EXITED); + for action in ["kill", "die", "stop"] { + local.announce(&docker_event("container", action, "a1")); + } + settle(cx); + assert_eq!( + listings(&local), + (2, 1), + "containers once, images not at all" + ); + watched.read_with(cx, |host, _| { + assert_eq!(host.snapshot().containers[0].state, State::Exited); + }); + + local.set_images(IMAGE); + local.announce(&docker_event("image", "pull", "nginx:1")); + settle(cx); + assert_eq!(listings(&local), (2, 2), "images alone"); + watched.read_with(cx, |host, _| { + assert_eq!(host.snapshot().images[0].name(), "nginx:1"); + }); + assert!(local.following(0), "the same stream is still followed"); +} + +#[gpui::test] +fn asking_to_relist_reuses_the_followed_stream(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let watched = watch(cx, &hosts, &local); + watched.update(cx, |host, cx| host.relist(Changes::CONTAINERS, cx)); + settle(cx); + assert_eq!(listings(&local), (2, 1)); + watched.update(cx, |host, cx| host.relist(Changes::ALL, cx)); + settle(cx); + assert_eq!(listings(&local), (3, 2)); + assert_eq!(local.count("docker events"), 1); + assert!(local.following(0)); +} + +#[gpui::test] +fn the_watch_stops_when_nobody_holds_it(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let first = watch(cx, &hosts, &local); + let second = watch(cx, &hosts, &local); + drop(first); + release(cx); + assert!(local.following(0), "one window still shows it"); + drop(second); + release(cx); + assert_eq!(local.followers(), 0, "the last one let go"); + + let _again = watch(cx, &hosts, &local); + assert_eq!( + local.count("docker events"), + 2, + "a new watch, from the start" + ); + assert_eq!(local.followers(), 1); +} + +#[gpui::test] +fn without_events_the_host_is_listed_every_retry(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let _watched = watch(cx, &hosts, &local); + local.end_events(); + cx.run_until_parked(); + assert_eq!(listings(&local), (1, 1)); + cx.executor().advance_clock(RETRY); + cx.run_until_parked(); + assert_eq!(listings(&local), (2, 2)); + assert_eq!(local.count("docker events"), 2); +} + +#[gpui::test] +fn a_host_without_an_engine_is_asked_again_only_on_request(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let bare = FakeDockerHost::new(false); + let watched = watch(cx, &hosts, &bare); + watched.read_with(cx, |host, _| { + assert!(matches!( + host.status(), + ListingStatus::Failed(ContainersError::NotInstalled) + )); + }); + let asked = bare.ran().len(); + cx.executor().advance_clock(RETRY * 6); + cx.run_until_parked(); + assert_eq!(bare.ran().len(), asked, "not asked again by itself"); + + let same = watch(cx, &hosts, &bare); + assert_eq!(same.entity_id(), watched.entity_id()); + assert!( + bare.ran().len() > asked, + "showing it again looks for an engine anew" + ); +} + +#[gpui::test] +fn a_refresh_during_initial_listing_is_not_lost(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + let gate = nocterm_session::testing::Gate::pending(); + let waiting = gate.clone(); + local.suspend_listing(async move { waiting.wait().await }.boxed()); + let watched = watch(cx, &hosts, &local); + assert_eq!( + local.count("docker events"), + 1, + "subscribed before snapshot" + ); + local.set_containers(EXITED); + watched.update(cx, |host, cx| host.relist(Changes::CONTAINERS, cx)); + gate.release(); + cx.run_until_parked(); + settle(cx); + watched.read_with(cx, |host, _| { + assert_eq!(host.snapshot().containers[0].state, State::Exited) + }); + assert_eq!(listings(&local), (2, 1)); +} + +#[gpui::test] +fn a_stalled_listing_times_out_and_can_be_retried(cx: &mut TestAppContext) { + let hosts = ContainerHosts::new(); + let local = FakeDockerHost::new(true); + local.suspend_listing(futures::future::pending().boxed()); + let watched = watch(cx, &hosts, &local); + cx.executor() + .advance_clock(std::time::Duration::from_secs(31)); + cx.run_until_parked(); + watched.read_with(cx, |host, _| { + assert!(matches!( + host.status(), + ListingStatus::Failed(ContainersError::TimedOut) + )) + }); + watched.update(cx, |host, cx| host.relist(Changes::ALL, cx)); + cx.run_until_parked(); + watched.read_with(cx, |host, _| { + assert!(matches!(host.status(), ListingStatus::Ready)) + }); +} diff --git a/crates/containers-service/src/watch.rs b/crates/containers-service/src/watch.rs new file mode 100644 index 00000000..5df27623 --- /dev/null +++ b/crates/containers-service/src/watch.rs @@ -0,0 +1,175 @@ +//! Following one host's engine: a listing, then another only of what its events change. + +use std::{sync::Arc, time::Duration}; + +use futures::{ + FutureExt as _, StreamExt as _, + channel::mpsc, + future::{Either, select}, + pin_mut, +}; +use gpui::{AsyncApp, BackgroundExecutor, WeakEntity}; +use nocterm_containers::{Changes, ContainersError, Engine, EventReader}; +use nocterm_session::{ExecOutput, HostExec}; + +use crate::host::{HostContainers, Listed}; + +/// How long a failed listing, or an ended event stream, waits before the host is listed +/// again, unless someone asks sooner. +pub const RETRY: Duration = Duration::from_secs(10); +/// How long a burst of events (a Compose project starting) may take before the host is +/// listed once for all of it. +pub const SETTLE: Duration = Duration::from_millis(300); + +/// Lists the host and follows its changes, again after failures, until the host has no +/// engine or nobody holds the entity any more. +pub(crate) async fn watch( + this: WeakEntity, + exec: Arc, + mut engine: Option, + mut asked: mpsc::UnboundedReceiver, + cx: &mut AsyncApp, +) { + loop { + let outcome = follow(&this, exec.clone(), &mut engine, &mut asked, cx).await; + let retry = this.update(cx, |this, cx| this.ended(outcome, cx)); + if !matches!(retry, Ok(true)) { + return; + } + let timer = cx.background_executor().timer(RETRY); + let request = asked.next(); + pin_mut!(timer, request); + if let Either::Right((None, _)) = select(timer, request).await { + return; + } + } +} + +/// What woke the watch. +enum Heard { + Output(Vec), + Asked(Changes), + Ended, +} + +/// One attempt: a listing, then another of what changed after each burst of events that +/// change something, until the event stream ends. +async fn follow( + this: &WeakEntity, + exec: Arc, + engine: &mut Option, + asked: &mut mpsc::UnboundedReceiver, + cx: &mut AsyncApp, +) -> Result<(), ContainersError> { + // Only requests preceding this acquisition are covered by its snapshot. + while asked.try_recv().is_ok() {} + let executor = cx.background_executor().clone(); + let known = *engine; + let opening_exec = exec.clone(); + let (found, snapshot, mut events) = bounded(&executor, async move { + open(opening_exec.as_ref(), known).await + }) + .await?; + *engine = Some(found); + publish(this, found, Listed::Both(snapshot), cx)?; + let mut reader = EventReader::default(); + loop { + let mut changes = match next(&mut events, asked).await { + Heard::Output(chunk) => reader.feed(&chunk), + Heard::Asked(changes) => changes, + Heard::Ended => return Ok(()), + }; + if changes.is_empty() { + continue; + } + cx.background_executor().timer(SETTLE).await; + while let Some(Some(chunk)) = events.next().now_or_never() { + changes |= reader.feed(&chunk); + } + while let Ok(more) = asked.try_recv() { + changes |= more; + } + let exec = exec.clone(); + let listed = bounded(&executor, async move { + relist(found, exec.as_ref(), changes).await + }) + .await?; + publish(this, found, listed, cx)?; + } +} + +/// Subscribe before acquisition; changes arriving while listing stay in the stream. +async fn open( + exec: &dyn HostExec, + known: Option, +) -> Result<(Engine, nocterm_containers::Snapshot, ExecOutput), ContainersError> { + let mut failure = ContainersError::NotInstalled; + for engine in Engine::ALL + .into_iter() + .filter(|engine| known.is_none_or(|known| known == *engine)) + { + let events = match engine.events(exec).await { + Ok(events) => events, + Err(ContainersError::NotInstalled) => continue, + Err(error) => return Err(error), + }; + match engine.snapshot(exec).await { + Ok(snapshot) => return Ok((engine, snapshot, events)), + Err(error @ (ContainersError::Disconnected | ContainersError::Unsupported)) => { + return Err(error); + } + Err(ContainersError::NotInstalled) => {} + Err(error) => failure = error, + } + } + Err(failure) +} + +/// Decode and collection never occupy the foreground executor; deadline owns cancellation. +async fn bounded( + executor: &BackgroundExecutor, + operation: impl std::future::Future> + Send + 'static, +) -> Result { + let task = executor.spawn(operation); + let timer = executor.timer(Duration::from_secs(30)); + pin_mut!(task, timer); + match select(task, timer).await { + Either::Left((result, _)) => result, + Either::Right(_) => Err(ContainersError::TimedOut), + } +} + +async fn next(events: &mut ExecOutput, asked: &mut mpsc::UnboundedReceiver) -> Heard { + let output = events.next(); + let request = asked.next(); + pin_mut!(output, request); + match select(output, request).await { + Either::Left((Some(chunk), _)) => Heard::Output(chunk), + Either::Right((Some(changes), _)) => Heard::Asked(changes), + // The program ended, or nobody holds the entity any more. + Either::Left((None, _)) | Either::Right((None, _)) => Heard::Ended, + } +} + +/// Only the listings `changes` names. +async fn relist( + engine: Engine, + exec: &dyn HostExec, + changes: Changes, +) -> Result { + Ok(match (changes.containers(), changes.images()) { + (true, false) => Listed::Containers(engine.containers(exec).await?), + (false, true) => Listed::Images(engine.images(exec).await?), + _ => Listed::Both(engine.snapshot(exec).await?), + }) +} + +fn publish( + this: &WeakEntity, + engine: Engine, + listed: Listed, + cx: &mut AsyncApp, +) -> Result<(), ContainersError> { + this.update(cx, |this, cx| this.listed(engine, listed, cx)) + .map_err(|_| ContainersError::Disconnected) +} diff --git a/crates/containers-ui/Cargo.toml b/crates/containers-ui/Cargo.toml index 1542786e..a66b2a76 100644 --- a/crates/containers-ui/Cargo.toml +++ b/crates/containers-ui/Cargo.toml @@ -11,6 +11,8 @@ layer = "feature" [dependencies] nocterm-containers.workspace = true +nocterm-containers-service.workspace = true +nocterm-plugin-model.workspace = true nocterm-session.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true @@ -21,6 +23,8 @@ gpui-kit.workspace = true workspace = true [dev-dependencies] +nocterm-containers-service = { workspace = true, features = ["test-support"] } nocterm-store.workspace = true gpui-kit = { workspace = true, features = ["test-support"] } nocterm-settings.workspace = true +nocterm-workspace = { workspace = true, features = ["test-support"] } diff --git a/crates/containers-ui/src/lib.rs b/crates/containers-ui/src/lib.rs index d01ba30e..74288f7e 100644 --- a/crates/containers-ui/src/lib.rs +++ b/crates/containers-ui/src/lib.rs @@ -9,18 +9,24 @@ //! already has; the footer's switcher shows how many containers run. //! //! Owns: active-host container presentation, captured operation intents and workspace tabs. -//! Local host execution is supplied explicitly at registration and retained by the model. -//! Does not own: container command formats, host execution or connection establishment. -//! Ports used: workspace active-session/program contracts and session `HostExec`. +//! [`install`] retains the supplied local host execution and the application's +//! `ContainerHosts`; every window's model receives them at registration. +//! Does not own: container command formats, watching a host and deciding when to list it +//! again (`nocterm-containers-service`), host execution or connection establishment. +//! Ports used: workspace active-session/program contracts, session `HostExec` and the +//! shared `ContainerHosts`. +//! Ports provided: the `containers` system plugin (`plugin::PLUGIN`) with its sidebar panel. //! Settings section: none. Globals: none. //! Invariants: host changes invalidate confirmations; completion retains its original context. mod model; mod ops; mod panel; +pub mod plugin; use gpui_kit::{AppContext as _, Context, Window}; +use nocterm_containers_service::ContainerHosts; use nocterm_session::HostExec; -use nocterm_workspace::Workspace; +use nocterm_workspace::{PanelRegistration, RegistrationError, Workspace}; use std::sync::Arc; pub use panel::ContainersPanel; @@ -34,21 +40,48 @@ gpui_kit::actions!( ] ); +/// What every window's containers panel shares: this computer's executor, and the watch of +/// each host, so a host shown in several windows is watched once. +#[derive(Clone)] +pub struct Installed { + local: Option>, + hosts: ContainerHosts, +} + +/// Prepares the containers panels of every window, with `local` running this computer's +/// programs. +pub fn install(local: Option>) -> Installed { + Installed { + local, + hosts: ContainerHosts::new(), + } +} + /// Adds the containers panel to the sidebar of `workspace`. +/// +/// The action is registered even when the workspace refuses the panel. pub fn register( - local: Option>, + installed: &Installed, workspace: &mut Workspace, - _window: &mut Window, + window: &mut Window, cx: &mut Context, -) { - let handle = cx.entity(); - let session = workspace.active_session(cx); - let model = cx.new(|cx| model::ContainersModel::new(&handle, session, local, cx)); - let panel = cx.new(|cx| ContainersPanel::new(model, handle.downgrade(), cx)); - workspace.add_panel(panel, cx); +) -> Result<(), RegistrationError> { workspace.register_action(|workspace, _: &ToggleContainers, window, cx| { - workspace.toggle_panel_of::(window, cx); + workspace.toggle_panel(&plugin::LIST.id, window, cx); }); + let installed = installed.clone(); + workspace.register_panel( + PanelRegistration::new(&plugin::LIST, move |workspace, _, cx| { + let session = workspace.active_session(cx); + let handle = cx.entity(); + let Installed { local, hosts } = installed.clone(); + let model = + cx.new(|cx| model::ContainersModel::new(&handle, session, local, hosts, cx)); + cx.new(|cx| ContainersPanel::new(model, handle.downgrade(), cx)) + }), + window, + cx, + ) } #[cfg(test)] diff --git a/crates/containers-ui/src/model.rs b/crates/containers-ui/src/model.rs index 22c88dcf..8c280a2a 100644 --- a/crates/containers-ui/src/model.rs +++ b/crates/containers-ui/src/model.rs @@ -1,31 +1,30 @@ //! The containers of the active host, kept current. //! //! The model follows the workspace's active host ([`nocterm_workspace::host`]), -//! like the resource monitor, over the connection that host already has. It -//! keeps watching while the panel is hidden, so the footer's count stays -//! right. The engine's event stream says when to list again; where there is -//! none, or it ends, the listing is repeated every [`RETRY`]. +//! like the resource monitor, over the connection that host already has. Its +//! containers come from the application's [`ContainerHosts`], which watches each +//! host connection once for every window showing it. The model holds that watch +//! while the panel is hidden, so the footer's count stays right. -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; -use futures::FutureExt as _; -use gpui_kit::{AsyncApp, Context, Entity, Subscription, Task, WeakEntity}; -use nocterm_containers::{Action, ContainersError, Engine, Snapshot, detect}; +use gpui_kit::{App, Context, Entity, Subscription, Task}; +use nocterm_containers::{Action, Changes, ContainersError, Engine, Snapshot}; +use nocterm_containers_service::{ContainerHosts, HostContainers, ListingStatus}; use nocterm_session::{ExecRequest, HostExec}; use nocterm_workspace::{ Host, ProgramSpec, SessionContext, Workspace, host::follow_active_session, }; -/// How long a failed listing, or an ended event stream, waits before the -/// host is listed again. -const RETRY: Duration = Duration::from_secs(10); -/// How long a burst of events (a Compose project starting) may take before -/// the host is listed once for all of it. -const SETTLE: Duration = Duration::from_millis(300); +/// What a host shows before its engine has answered. +static NOTHING: Snapshot = Snapshot { + containers: Vec::new(), + images: Vec::new(), +}; /// Where the listing of the current host stands. #[derive(Debug)] -pub(crate) enum Status { +pub(crate) enum Status<'a> { /// There is no host to list. Off, /// The host's session is not connected. @@ -34,7 +33,7 @@ pub(crate) enum Status { Ready, /// The last attempt failed; unless the host has no engine, another /// follows shortly. - Failed(ContainersError), + Failed(&'a ContainersError), } /// A confirmed operation belongs to one observed host connection. Moving @@ -57,30 +56,33 @@ impl ActionIntent { pub(crate) struct ContainersModel { epoch: Arc<()>, local: Option>, + hosts: ContainerHosts, host: Option, - /// The engine the host was found to have. - engine: Option, - snapshot: Snapshot, - status: Status, - task: Option>, + /// The shared watch of a connected host. + watched: Option, _follow: Subscription, } +/// A host's shared watch, redrawn by this model when it changes. +struct Watched { + containers: Entity, + _observe: Subscription, +} + impl ContainersModel { pub(crate) fn new( workspace: &Entity, session: Option, local: Option>, + hosts: ContainerHosts, cx: &mut Context, ) -> Self { let mut this = Self { epoch: Arc::new(()), local, + hosts, host: None, - engine: None, - snapshot: Snapshot::default(), - status: Status::Off, - task: None, + watched: None, _follow: follow_active_session(workspace, cx, Self::follow), }; this.follow(session, cx); @@ -91,16 +93,33 @@ impl ContainersModel { self.host.as_ref() } - pub(crate) fn engine(&self) -> Option { - self.engine + fn containers<'a>(&self, cx: &'a App) -> Option<&'a HostContainers> { + self.watched + .as_ref() + .map(|watched| watched.containers.read(cx)) + } + + pub(crate) fn engine(&self, cx: &App) -> Option { + self.containers(cx)?.engine() } - pub(crate) fn snapshot(&self) -> &Snapshot { - &self.snapshot + pub(crate) fn snapshot<'a>(&self, cx: &'a App) -> &'a Snapshot { + self.containers(cx) + .map_or(&NOTHING, HostContainers::snapshot) } - pub(crate) fn status(&self) -> &Status { - &self.status + pub(crate) fn status<'a>(&self, cx: &'a App) -> Status<'a> { + let Some(host) = &self.host else { + return Status::Off; + }; + if !host.connected { + return Status::Offline; + } + match self.containers(cx).map(HostContainers::status) { + Some(ListingStatus::Ready) => Status::Ready, + Some(ListingStatus::Failed(error)) => Status::Failed(error), + Some(ListingStatus::Loading) | None => Status::Loading, + } } fn follow(&mut self, session: Option, cx: &mut Context) { @@ -113,49 +132,58 @@ impl ContainersModel { if unchanged { return; } - let key = |host: &Option| host.as_ref().map(|host| host.key.clone()); - if key(&self.host) != key(&host) { - self.engine = None; - self.snapshot = Snapshot::default(); - } self.epoch = Arc::new(()); + self.watched = host + .as_ref() + .filter(|host| host.connected) + .map(|host| self.watch(&host.exec, cx)); self.host = host; - self.refresh(cx); + cx.notify(); } - /// Lists the host again, and follows it from there. + fn watch(&self, exec: &Arc, cx: &mut Context) -> Watched { + let containers = self.hosts.watch(exec, cx); + Watched { + _observe: cx.observe(&containers, |_, _, cx| cx.notify()), + containers, + } + } + + /// Lists the host again, as the person asked. pub(crate) fn refresh(&mut self, cx: &mut Context) { - self.task = None; - let Some(host) = self.host.clone() else { - self.set_status(Status::Off, cx); - return; - }; - if !host.connected { - self.set_status(Status::Offline, cx); - return; + self.relist(Changes::ALL, cx); + } + + fn relist(&self, changes: Changes, cx: &mut Context) { + if let Some(watched) = &self.watched { + watched + .containers + .update(cx, |containers, cx| containers.relist(changes, cx)); } - self.set_status(Status::Loading, cx); - let engine = self.engine; - self.task = Some(cx.spawn(async move |this, cx| watch(this, host, engine, cx).await)); } - pub(crate) fn intent(&self, action: Action, ids: Vec) -> Option { + pub(crate) fn intent( + &self, + action: Action, + ids: Vec, + cx: &App, + ) -> Option { let host = self.host.clone()?; if !host.connected { return None; } Some(ActionIntent { host, - engine: self.engine?, + engine: self.engine(cx)?, epoch: self.epoch.clone(), action, ids, }) } - fn accepts(&self, intent: &ActionIntent) -> bool { + fn accepts(&self, intent: &ActionIntent, cx: &App) -> bool { Arc::ptr_eq(&self.epoch, &intent.epoch) - && self.engine == Some(intent.engine) + && self.engine(cx) == Some(intent.engine) && self .host .as_ref() @@ -169,7 +197,7 @@ impl ContainersModel { ids: Vec, cx: &mut Context, ) -> Task> { - let Some(intent) = self.intent(action, ids) else { + let Some(intent) = self.intent(action, ids, cx) else { return Task::ready(Err(ContainersError::Disconnected)); }; self.perform_intent(intent, cx) @@ -183,18 +211,27 @@ impl ContainersModel { cx: &mut Context, ) -> Task> { cx.spawn(async move |this, cx| { - let accepted = this.update(cx, |this, _| this.accepts(&intent)); + let accepted = this.update(cx, |this, cx| this.accepts(&intent, cx)); if !matches!(accepted, Ok(true)) { return Err(ContainersError::Disconnected); } - let done = intent - .engine - .perform(intent.host.exec.as_ref(), intent.action, &intent.ids) - .await; - // Finishing on A must not interrupt B's watcher. + let deadline = cx + .background_executor() + .timer(std::time::Duration::from_secs(75)); + let action = + intent + .engine + .perform(intent.host.exec.as_ref(), intent.action, &intent.ids); + futures::pin_mut!(deadline, action); + let done = match futures::future::select(action, deadline).await { + futures::future::Either::Left((result, _)) => result, + futures::future::Either::Right(_) => Err(ContainersError::TimedOut), + }; + // Its events say so too, but not every engine reports them. Finishing on A + // must not relist B. let _ = this.update(cx, |this, cx| { - if this.accepts(&intent) { - this.refresh(cx); + if this.accepts(&intent, cx) { + this.relist(intent.action.changes(), cx); } }); done @@ -207,94 +244,13 @@ impl ContainersModel { &self, title: String, program: impl FnOnce(Engine) -> ExecRequest, + cx: &App, ) -> Option { Some(ProgramSpec { title: title.into(), host: self.host.clone()?, - program: program(self.engine?), + program: program(self.engine(cx)?), shell_syntax: None, }) } - - fn apply(&mut self, engine: Engine, snapshot: Snapshot, cx: &mut Context) { - self.engine = Some(engine); - if self.snapshot != snapshot { - self.snapshot = snapshot; - cx.notify(); - } - self.set_status(Status::Ready, cx); - } - - fn set_status(&mut self, status: Status, cx: &mut Context) { - // Native error sources have no equality contract. Deduplicate only the - // visible status category and text; recovery always matches typed variants. - let unchanged = match (&self.status, &status) { - (Status::Failed(previous), Status::Failed(next)) => { - std::mem::discriminant(previous) == std::mem::discriminant(next) - && previous.to_string() == next.to_string() - } - (previous, next) => std::mem::discriminant(previous) == std::mem::discriminant(next), - }; - if !unchanged { - self.status = status; - cx.notify(); - } - } -} - -/// Lists the host and follows its changes, again after failures, until the -/// host has no engine or the model moves on. -async fn watch( - this: WeakEntity, - host: Host, - mut engine: Option, - cx: &mut AsyncApp, -) { - loop { - let outcome = follow_engine(&this, &host, &mut engine, cx).await; - let retry = this.update(cx, |this, cx| match outcome { - // The event stream ended; the listing still stands. - Ok(()) => true, - Err(error) => { - let retry = !matches!( - error, - ContainersError::NotInstalled | ContainersError::Unsupported - ); - this.set_status(Status::Failed(error), cx); - retry - } - }); - if !matches!(retry, Ok(true)) { - return; - } - cx.background_executor().timer(RETRY).await; - } -} - -/// One attempt: a listing, then another after each change the engine -/// reports, until its event stream ends. -async fn follow_engine( - this: &WeakEntity, - host: &Host, - engine: &mut Option, - cx: &mut AsyncApp, -) -> Result<(), ContainersError> { - let exec = host.exec.as_ref(); - let (found, snapshot) = match *engine { - Some(known) => (known, known.snapshot(exec).await?), - None => detect(exec).await?, - }; - *engine = Some(found); - let apply = |snapshot, cx: &mut AsyncApp| { - this.update(cx, |this, cx| this.apply(found, snapshot, cx)) - .map_err(|_| ContainersError::Disconnected) - }; - apply(snapshot, cx)?; - let mut events = found.events(exec).await?; - while events.next().await.is_some() { - cx.background_executor().timer(SETTLE).await; - while let Some(Some(_)) = events.next().now_or_never() {} - apply(found.snapshot(exec).await?, cx)?; - } - Ok(()) } diff --git a/crates/containers-ui/src/panel.rs b/crates/containers-ui/src/panel.rs index f74284c4..f7bbd064 100644 --- a/crates/containers-ui/src/panel.rs +++ b/crates/containers-ui/src/panel.rs @@ -18,7 +18,7 @@ use gpui_kit::{ div, prelude::*, }; -use nocterm_containers::{Action, ContainersError}; +use nocterm_containers::{Action, ContainersError, Engine}; use nocterm_ui::IconName; use nocterm_workspace::{Panel, ShellSyntax, Workspace}; @@ -119,14 +119,12 @@ impl ContainersPanel { Op::Logs => ("Logs", None), Op::Act(_) => return, }; - let Some(mut spec) = - self.model - .read(cx) - .program(format!("{label}: {}", subject.name), |engine| match op { - Op::Shell => engine.shell(id), - _ => engine.logs(id), - }) - else { + let title = format!("{label}: {}", subject.name); + let program = |engine: Engine| match op { + Op::Shell => engine.shell(id), + _ => engine.logs(id), + }; + let Some(mut spec) = self.model.read(cx).program(title, program, cx) else { return; }; spec.shell_syntax = shell_syntax; @@ -143,7 +141,7 @@ impl ContainersPanel { cx: &mut Context, ) { let Subject { kind, name, ids } = subject; - let Some(intent) = self.model.read(cx).intent(action, ids) else { + let Some(intent) = self.model.read(cx).intent(action, ids, cx) else { return; }; let panel = cx.entity().downgrade(); @@ -224,7 +222,7 @@ impl ContainersPanel { let host = model .host() .map_or_else(|| "No host".to_owned(), |host| host.key.label()); - let engine = model.engine().map(|engine| engine.name()); + let engine = model.engine(cx).map(|engine| engine.name()); let theme = cx.theme(); h_flex() .gap_1() @@ -250,8 +248,8 @@ impl ContainersPanel { /// A line on why there is nothing, or nothing current, to show. fn render_status(&self, cx: &mut Context) -> Option { let model = self.model.read(cx); - let found = model.engine().is_some(); - let (text, danger) = match model.status() { + let found = model.engine(cx).is_some(); + let (text, danger) = match model.status(cx) { Status::Ready => return None, Status::Loading if found => return None, Status::Loading => ("Looking for Docker or Podman…".to_owned(), false), @@ -292,7 +290,7 @@ impl Panel for ContainersPanel { /// How many containers run; none shows no count. fn badge(&self, cx: &App) -> Option { - let running = self.model.read(cx).snapshot().running(); + let running = self.model.read(cx).snapshot(cx).running(); (running > 0).then(|| running.to_string().into()) } } @@ -305,7 +303,7 @@ impl Focusable for ContainersPanel { impl Render for ContainersPanel { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { - let shown = self.model.read(cx).engine().is_some(); + let shown = self.model.read(cx).engine(cx).is_some(); v_flex() .track_focus(&self.focus_handle) .size_full() diff --git a/crates/containers-ui/src/panel/rows.rs b/crates/containers-ui/src/panel/rows.rs index 531a72ff..9e65a2cf 100644 --- a/crates/containers-ui/src/panel/rows.rs +++ b/crates/containers-ui/src/panel/rows.rs @@ -27,7 +27,7 @@ const PROJECT_GROUP: &str = "container-project"; impl ContainersPanel { pub(super) fn render_tree(&self, cx: &mut Context) -> impl IntoElement { - let snapshot = self.model.read(cx).snapshot().clone(); + let snapshot = self.model.read(cx).snapshot(cx).clone(); let mut tree = v_flex().gap_px().pb_1(); let containers = !self.folded.contains(&Fold::Containers); diff --git a/crates/containers-ui/src/plugin.rs b/crates/containers-ui/src/plugin.rs new file mode 100644 index 00000000..8aea3a3e --- /dev/null +++ b/crates/containers-ui/src/plugin.rs @@ -0,0 +1,42 @@ +//! The containers list as a system plugin: one sidebar panel. +use nocterm_plugin_model::{ + AutoPlace, ContributionId, ContributionKind, Icon, PanelChrome, PanelTraits, Placement, + PluginId, SettingsSchema, SystemContribution, SystemPlugin, WidthClass, Zone, +}; + +/// The containers of the active host in the sidebar. +pub const LIST: SystemContribution = SystemContribution { + id: ContributionId::new("containers.list"), + kind: ContributionKind::Panel, + title: "Containers", + icon: Icon::Container, + placement: Placement { + zone: Zone::Left, + order: 40, + group: Some("navigation"), + auto: AutoPlace::Show, + }, + panel: Some(PanelTraits { + width: WidthClass::Navigation, + chrome: PanelChrome::Titled, + }), + preview: None, + edge: None, + pinned: false, +}; + +/// Containers: the containers and images of the active host. +pub static PLUGIN: SystemPlugin = SystemPlugin { + id: PluginId::new("containers"), + name: "Containers", + description: "Containers and images of the active host", + required: false, + contributions: &[LIST], + settings_section: None, + settings: SettingsSchema::NONE, + settings_page: None, +}; + +#[cfg(test)] +#[path = "plugin_tests.rs"] +mod tests; diff --git a/crates/containers-ui/src/plugin_tests.rs b/crates/containers-ui/src/plugin_tests.rs new file mode 100644 index 00000000..87a7de12 --- /dev/null +++ b/crates/containers-ui/src/plugin_tests.rs @@ -0,0 +1,52 @@ +//! The containers list registers under its descriptor and the toggle action drives that id. +use super::LIST; +use crate::tests::fixture; +use gpui_kit::{AppContext as _, Focusable as _, TestAppContext}; +use nocterm_ui::contribution_icon; +use nocterm_workspace::test_support::{Toggle, toggle_presentation}; + +fn registered(cx: &mut TestAppContext) -> crate::tests::Fixture { + let fixture = fixture(cx, None); + cx.update_window(fixture.window, |_, window, cx| { + fixture.workspace.update(cx, |workspace, cx| { + crate::register(&crate::install(None), workspace, window, cx).unwrap(); + }); + }) + .unwrap(); + cx.run_until_parked(); + fixture +} + +#[gpui_kit::test] +fn the_sidebar_toggle_shows_the_descriptors_title_and_icon(cx: &mut TestAppContext) { + let fixture = registered(cx); + cx.update(|cx| { + let workspace = fixture.workspace.read(cx); + assert!(workspace.is_registered(&LIST.id)); + assert_eq!( + toggle_presentation(workspace, Toggle::Navigation(0), cx), + Some((LIST.title.into(), contribution_icon(LIST.icon))) + ); + }); +} + +#[gpui_kit::test] +fn the_toggle_action_hides_the_shown_list_and_shows_it_again(cx: &mut TestAppContext) { + let fixture = registered(cx); + let dispatch = |cx: &mut TestAppContext| { + cx.update_window(fixture.window, |_, window, cx| { + window.focus(&fixture.workspace.read(cx).focus_handle(cx), cx); + window.dispatch_action(Box::new(crate::ToggleContainers), cx); + }) + .unwrap(); + cx.run_until_parked(); + }; + let open = |cx: &mut TestAppContext| { + cx.update(|cx| fixture.workspace.read(cx).panel_is_open(&LIST.id)) + }; + assert!(open(cx), "the sidebar opens on its first panel"); + dispatch(cx); + assert!(!open(cx)); + dispatch(cx); + assert!(open(cx)); +} diff --git a/crates/containers-ui/src/tests.rs b/crates/containers-ui/src/tests.rs index f5e28b71..ac17b56c 100644 --- a/crates/containers-ui/src/tests.rs +++ b/crates/containers-ui/src/tests.rs @@ -1,4 +1,4 @@ -use std::sync::{Arc, Mutex}; +use std::sync::Arc; use futures::FutureExt as _; use gpui_kit::{ @@ -6,7 +6,11 @@ use gpui_kit::{ TestAppContext, Window, WindowOptions, div, prelude::*, }; use nocterm_containers::{Action, ContainersError, Engine}; -use nocterm_session::{ExecExit, ExecFuture, ExecOutput, ExecRequest, ExecSink, HostExec, Target}; +use nocterm_containers_service::{ + ContainerHosts, + test_support::{EXITED, FakeDockerHost, RUNNING, docker_event}, +}; +use nocterm_session::{HostExec, Target}; use nocterm_settings::SettingsDocument; use nocterm_store::SettingsConfig; use nocterm_ui::DesignTokens; @@ -17,76 +21,11 @@ use crate::{ model::{ContainersModel, Status}, }; -const RUNNING: &str = - r#"{"ID":"a1","Image":"nginx","Names":"web","State":"running","Status":"Up 2 hours"}"#; -const EXITED: &str = - r#"{"ID":"a1","Image":"nginx","Names":"web","State":"exited","Status":"Exited (0)"}"#; +/// A host with a scripted Docker; see [`FakeDockerHost`]. +pub(crate) type FakeHost = FakeDockerHost; -/// A host whose `docker` lists `containers`, keeps `docker events` running -/// until the test prints to it, and does every other command it is asked. -pub(crate) struct FakeHost { - docker: bool, - containers: Mutex<&'static str>, - ran: Mutex>, - events: Mutex>, -} - -impl FakeHost { - pub(crate) fn new(docker: bool) -> Arc { - Arc::new(Self { - docker, - containers: Mutex::new(RUNNING), - ran: Mutex::default(), - events: Mutex::default(), - }) - } - - pub(crate) fn ran(&self) -> Vec { - self.ran.lock().unwrap().clone() - } - - /// Reports a change from the latest event stream. - fn announce(&self) { - let events = self.events.lock().unwrap(); - let sink = events.last().expect("events are followed"); - assert_eq!( - sink.send(b"container die\n".to_vec()).now_or_never(), - Some(true) - ); - } - - /// Whether the event stream started `index`th is still being read. - fn following(&self, index: usize) -> bool { - !self.events.lock().unwrap()[index].is_closed() - } -} - -impl HostExec for FakeHost { - fn exec(&self, request: ExecRequest) -> ExecFuture { - let line = request.command_line(); - self.ran.lock().unwrap().push(line.clone()); - let (sink, output) = ExecOutput::channel(); - if self.docker && line.starts_with("docker events") { - self.events.lock().unwrap().push(sink); - return Box::pin(async { Ok(output) }); - } - let (stdout, status) = match () { - _ if !self.docker => ("", 127), - _ if line.starts_with("docker ps") => (*self.containers.lock().unwrap(), 0), - _ if line.starts_with("docker images") => ("", 0), - _ if line.starts_with("docker") => ("", 0), - _ => ("", 127), - }; - assert_eq!( - sink.send(stdout.as_bytes().to_vec()).now_or_never(), - Some(true) - ); - sink.finish(ExecExit { - status: Some(status), - stderr: String::new(), - }); - Box::pin(async { Ok(output) }) - } +fn announce_exit(host: &FakeHost) { + host.announce(&docker_event("container", "die", "a1")); } struct SessionItem { @@ -137,12 +76,13 @@ pub(crate) fn fixture(cx: &mut TestAppContext, local: Option>) gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { let deps = - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)); + nocterm_workspace::test_support::deps_for(&[&crate::plugin::PLUGIN], cx); Workspace::new(deps, window, cx) }) }) .unwrap(); - let model = cx.new(|cx| ContainersModel::new(&workspace, None, local, cx)); + let model = + cx.new(|cx| ContainersModel::new(&workspace, None, local, ContainerHosts::new(), cx)); let panel = cx.new(|cx| ContainersPanel::new(model.clone(), workspace.downgrade(), cx)); Fixture { window, @@ -160,11 +100,11 @@ fn assert_status( fixture: &Fixture, expected: impl FnOnce(&Status) -> bool, ) { - fixture.model.read_with(cx, |model, _| { + fixture.model.read_with(cx, |model, cx| { assert!( - expected(model.status()), + expected(&model.status(cx)), "unexpected status: {:?}", - model.status() + model.status(cx) ); }); } @@ -205,8 +145,8 @@ fn containers_are_listed_again_when_the_engine_reports_a_change(cx: &mut TestApp .any(|line| line.starts_with("docker events")) ); - *local.containers.lock().unwrap() = EXITED; - local.announce(); + local.set_containers(EXITED); + announce_exit(&local); cx.executor() .advance_clock(std::time::Duration::from_secs(1)); cx.run_until_parked(); @@ -225,12 +165,22 @@ fn actions_and_programs_run_on_the_followed_host(cx: &mut TestAppContext) { cx.run_until_parked(); assert!(matches!(done.now_or_never(), Some(Ok(())))); assert!(local.ran().contains(&"docker stop a1".to_owned())); - assert!(!local.following(0), "a refresh follows the engine anew"); + // With the events the action caused, once they settle. + cx.executor() + .advance_clock(nocterm_containers_service::SETTLE); + cx.run_until_parked(); + assert_eq!( + local.count("docker ps"), + 2, + "the containers are listed again" + ); + assert_eq!(local.count("docker images"), 1, "the images are not"); + assert!(local.following(0), "over the same event stream"); let spec = fixture .model - .read_with(cx, |model, _| { - model.program("Logs: web".into(), |engine| engine.logs("a1")) + .read_with(cx, |model, cx| { + model.program("Logs: web".into(), |engine| engine.logs("a1"), cx) }) .unwrap(); assert_eq!(spec.host.key, HostKey::Local); @@ -270,8 +220,11 @@ fn the_panel_follows_the_active_session_without_connections_of_its_own(cx: &mut assert!( fixture .model - .read_with(cx, |model, _| model - .program("Logs".into(), |e| e.logs("a1"))) + .read_with(cx, |model, cx| model.program( + "Logs".into(), + |e| e.logs("a1"), + cx + )) .is_none(), "no engine is known for it" ); @@ -294,9 +247,9 @@ fn nothing_is_listed_without_a_host(cx: &mut TestAppContext) { fn destructive_intents_cannot_move_to_another_host_or_reconnect(cx: &mut TestAppContext) { let local = FakeHost::new(true); let fixture = fixture(cx, Some(local.clone())); - let intent = fixture.model.read_with(cx, |model, _| { + let intent = fixture.model.read_with(cx, |model, cx| { model - .intent(Action::RemoveImage, vec!["same-image".into()]) + .intent(Action::RemoveImage, vec!["same-image".into()], cx) .unwrap() }); let another = FakeHost::new(true); @@ -312,9 +265,9 @@ fn destructive_intents_cannot_move_to_another_host_or_reconnect(cx: &mut TestApp assert!(!local.ran().iter().any(|line| line.contains("same-image"))); assert!(!another.ran().iter().any(|line| line.contains("same-image"))); - let intent = fixture.model.read_with(cx, |model, _| { + let intent = fixture.model.read_with(cx, |model, cx| { model - .intent(Action::RemoveImage, vec!["same-image".into()]) + .intent(Action::RemoveImage, vec!["same-image".into()], cx) .unwrap() }); // Same target, a new connection/executor. @@ -341,15 +294,18 @@ fn disconnecting_invalidates_captured_intent_and_rejects_new_actions(cx: &mut Te let host = FakeHost::new(true); let fixture = fixture(cx, None); open_session(cx, &fixture, remote(&host, true)); - let intent = fixture.model.read_with(cx, |model, _| { - model.intent(Action::Remove, vec!["a1".into()]).unwrap() + let intent = fixture.model.read_with(cx, |model, cx| { + model.intent(Action::Remove, vec!["a1".into()], cx).unwrap() }); open_session(cx, &fixture, remote(&host, false)); assert!( fixture .model - .read_with(cx, |model, _| model - .intent(Action::Remove, vec!["a1".into()])) + .read_with(cx, |model, cx| model.intent( + Action::Remove, + vec!["a1".into()], + cx + )) .is_none() ); let done = fixture @@ -367,7 +323,7 @@ fn disconnecting_invalidates_captured_intent_and_rejects_new_actions(cx: &mut Te #[gpui_kit::test] fn malformed_successful_listing_remains_a_retryable_failure(cx: &mut TestAppContext) { let local = FakeHost::new(true); - *local.containers.lock().unwrap() = "not JSON"; + local.set_containers("not JSON"); let fixture = fixture(cx, Some(local.clone())); assert_status(cx, &fixture, |status| { let Status::Failed(ContainersError::Parse(error)) = status else { @@ -379,7 +335,7 @@ fn malformed_successful_listing_remains_a_retryable_failure(cx: &mut TestAppCont ) }); let attempts = local.ran().len(); - *local.containers.lock().unwrap() = RUNNING; + local.set_containers(RUNNING); cx.executor() .advance_clock(std::time::Duration::from_secs(10)); cx.run_until_parked(); @@ -389,38 +345,45 @@ fn malformed_successful_listing_remains_a_retryable_failure(cx: &mut TestAppCont } #[gpui_kit::test] -fn registered_container_panels_keep_their_supplied_host_across_other_windows( - cx: &mut TestAppContext, -) { +fn windows_on_the_same_host_share_one_watch(cx: &mut TestAppContext) { let first = FakeHost::new(true); let second = FakeHost::new(true); - for host in [first.clone(), second.clone(), first.clone()] { + let shared = crate::install(Some(first.clone())); + let other = crate::install(Some(second.clone())); + for installed in [&shared, &other, &shared] { let fixture = fixture(cx, None); cx.update_window(fixture.window, |_, window, cx| { fixture.workspace.update(cx, |workspace, cx| { - crate::register(Some(host.clone()), workspace, window, cx); + crate::register(installed, workspace, window, cx).unwrap(); }); }) .unwrap(); cx.run_until_parked(); } - assert_eq!( - first - .ran() - .iter() - .filter(|command| command.starts_with("docker events")) - .count(), - 2 - ); - assert_eq!( - second - .ran() - .iter() - .filter(|command| command.starts_with("docker events")) - .count(), - 1 - ); + assert_eq!(first.count("docker events"), 1, "two windows, one watch"); + assert_eq!(first.count("docker ps"), 1); + assert_eq!(second.count("docker events"), 1); assert!(first.following(0)); - assert!(first.following(1)); assert!(second.following(0)); } + +#[gpui_kit::test] +fn health_checks_do_not_list_the_host_again(cx: &mut TestAppContext) { + let local = FakeHost::new(true); + let fixture = fixture(cx, Some(local.clone())); + for _ in 0..10 { + for action in [ + "exec_create: pg_isready", + "exec_start: pg_isready", + "exec_die", + ] { + local.announce(&docker_event("container", action, "pg")); + } + cx.executor() + .advance_clock(std::time::Duration::from_secs(3)); + cx.run_until_parked(); + } + assert_eq!(local.count("docker ps"), 1); + assert_eq!(local.count("docker images"), 1); + assert_eq!(badge(cx, &fixture).as_deref(), Some("1")); +} diff --git a/crates/containers/Cargo.toml b/crates/containers/Cargo.toml index f3d76337..32ea0f7a 100644 --- a/crates/containers/Cargo.toml +++ b/crates/containers/Cargo.toml @@ -13,6 +13,7 @@ layer = "domain" nocterm-session.workspace = true futures.workspace = true serde_json.workspace = true +serde.workspace = true thiserror.workspace = true [lints] diff --git a/crates/containers/src/engine.rs b/crates/containers/src/engine.rs index 7515a160..e43efd3d 100644 --- a/crates/containers/src/engine.rs +++ b/crates/containers/src/engine.rs @@ -3,7 +3,11 @@ use futures::future::try_join; use nocterm_session::{ExecOutput, ExecRequest, HostExec}; -use crate::{ContainersError, model::Snapshot, parse}; +use crate::{ + Changes, ContainersError, + model::{Container, Image, Snapshot}, + parse, +}; /// The most a listing may print; hosts with more containers than this /// describes are not served. @@ -16,7 +20,8 @@ const SHELL: &str = "command -v bash >/dev/null 2>&1 && exec bash || exec sh"; const NOT_FOUND: u32 = 127; /// A container engine's command-line client. -#[derive(Clone, Copy, Debug, PartialEq, Eq)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)] +#[serde(rename_all = "snake_case")] pub enum Engine { Docker, Podman, @@ -47,6 +52,14 @@ impl Action { } } + /// The listings doing it changes. + pub fn changes(self) -> Changes { + match self { + Self::RemoveImage => Changes::ALL, + _ => Changes::CONTAINERS, + } + } + fn args(self) -> &'static [&'static str] { match self { Self::Start => &["start"], @@ -89,38 +102,59 @@ impl Engine { } } - fn command(self) -> ExecRequest { + pub fn command(self) -> ExecRequest { ExecRequest::new(match self { Self::Docker => "docker", Self::Podman => "podman", }) } + /// Read-only listing requests; execution belongs to the caller's adapter. + pub fn container_listing(self) -> ExecRequest { + self.command() + .args(["ps", "--all", "--no-trunc", "--format", "json"]) + } + + pub fn image_listing(self) -> ExecRequest { + self.command() + .args(["images", "--all", "--no-trunc", "--format", "json"]) + } + /// Every container, running or not, and every image. pub async fn snapshot(self, exec: &dyn HostExec) -> Result { - let list = |what: &str, all: &str| { - let request = self - .command() - .arg(what) - .arg(all) - .arg("--format") - .arg("json"); - run(exec, request) - }; - let (containers, images) = try_join(list("ps", "--all"), list("images", "--all")).await?; - Ok(Snapshot { - containers: parse::containers(&containers).map_err(ContainersError::Parse)?, - images: parse::images(&images).map_err(ContainersError::Parse)?, - }) + let (containers, images) = try_join(self.containers(exec), self.images(exec)).await?; + Ok(Snapshot { containers, images }) + } + + /// Every container, running or not. + pub async fn containers(self, exec: &dyn HostExec) -> Result, ContainersError> { + let output = run(exec, self.listing("ps")).await?; + parse::containers(&output).map_err(ContainersError::Parse) + } + + /// Every image, intermediate ones included. + pub async fn images(self, exec: &dyn HostExec) -> Result, ContainersError> { + let output = run(exec, self.listing("images")).await?; + parse::images(&output).map_err(ContainersError::Parse) + } + + fn listing(self, what: &str) -> ExecRequest { + self.command().args([what, "--all", "--format", "json"]) } - /// A stream that prints whenever a container or image changes, until it - /// is dropped. + /// A stream of what happens to containers and images, one JSON object + /// per line, until it is dropped; [`EventReader`](crate::EventReader) + /// reads it. pub async fn events(self, exec: &dyn HostExec) -> Result { let request = self .command() .args(["events", "--filter", "type=container"]) - .args(["--filter", "type=image"]); + .args(["--filter", "type=image", "--format"]) + .arg(match self { + // A template, which every Docker version reads. + Self::Docker => "{{json .}}", + Self::Podman => "json", + }); Ok(exec.exec(request).await?) } diff --git a/crates/containers/src/engine/tests.rs b/crates/containers/src/engine/tests.rs index 18d01169..98187825 100644 --- a/crates/containers/src/engine/tests.rs +++ b/crates/containers/src/engine/tests.rs @@ -111,7 +111,7 @@ fn logs_and_shells_are_programs_for_a_terminal() { block_on(Engine::Docker.events(&host)).unwrap(); assert_eq!( host.ran(), - ["docker events --filter type=container --filter type=image"] + ["docker events --filter type=container --filter type=image --format '{{json .}}'"] ); } @@ -230,3 +230,23 @@ fn malformed_podman_array_keeps_the_native_location_and_eof_cause() { assert!(source.column() > 0); assert!(source.is_eof()); } + +#[test] +fn containers_and_images_are_listed_on_their_own() { + let host = FakeExec::new(vec![ + ("docker ps --all", Ok((CONTAINER, 0, ""))), + ("docker images --all", Ok((IMAGE, 0, ""))), + ("podman events", Ok(("", 0, ""))), + ]); + let containers = block_on(Engine::Docker.containers(&host)).unwrap(); + assert_eq!(containers[0].name, "box"); + assert_eq!(host.ran(), ["docker ps --all --format json"]); + let images = block_on(Engine::Docker.images(&host)).unwrap(); + assert_eq!(images[0].name(), "alpine:3"); + assert_eq!(host.ran().len(), 2); + block_on(Engine::Podman.events(&host)).unwrap(); + assert_eq!( + host.ran()[2], + "podman events --filter type=container --filter type=image --format json" + ); +} diff --git a/crates/containers/src/error.rs b/crates/containers/src/error.rs index ed07140d..b844237d 100644 --- a/crates/containers/src/error.rs +++ b/crates/containers/src/error.rs @@ -46,6 +46,8 @@ pub enum ContainersError { Disconnected, #[error("This host cannot run programs.")] Unsupported, + #[error("The container engine did not finish the operation in time.")] + TimedOut, #[error("{0}")] Parse(#[from] ListingError), #[error("{0}")] @@ -64,7 +66,7 @@ impl From for ContainersError { ExecError::NotFound(_) => Self::NotInstalled, ExecError::Disconnected => Self::Disconnected, ExecError::Unsupported => Self::Unsupported, - error @ ExecError::Failed(_) => Self::Execution(error), + error @ (ExecError::Failed(_) | ExecError::Source(_)) => Self::Execution(error), } } } diff --git a/crates/containers/src/events.rs b/crates/containers/src/events.rs new file mode 100644 index 00000000..585968d8 --- /dev/null +++ b/crates/containers/src/events.rs @@ -0,0 +1,265 @@ +//! What an engine's event stream means for its listings. +//! +//! An engine reports far more than what changes a listing: a health check +//! inside a container is an `exec_create`, `exec_start` and `exec_die` every +//! few seconds, and Podman repeats an unchanged health status after every +//! check. Relisting for each of them makes the daemon walk every container +//! and image again and again, so [`EventReader`] keeps only the events that +//! change what a listing shows, and says which of the two listings they +//! change. A line it cannot read changes both: being unsure costs a listing, +//! never a stale one. + +use std::collections::HashMap; + +use serde::Deserialize; + +/// The longest event line kept; a longer one is dropped, as unreadable. +const MAX_LINE: usize = 64 * 1024; +/// The most containers whose health status is remembered at once. +const MAX_HEALTH: usize = 4096; + +/// Container events that never change a listing: commands run inside a +/// container (health checks are these), terminals attached to it and files +/// copied from it. +const QUIET_CONTAINER: [&str; 16] = [ + "exec_create", + "exec_start", + "exec_die", + "exec_detach", + "exec", + "exec_died", + "attach", + "detach", + "resize", + "top", + "copy", + "export", + "archive-path", + "extract-to-dir", + "mount", + "unmount", +]; +/// Image events that never change a listing. +const QUIET_IMAGE: [&str; 4] = ["push", "save", "mount", "unmount"]; +/// Image events that rename or remove an image a container may be shown by. +const RENAMING_IMAGE: [&str; 5] = ["tag", "untag", "delete", "remove", "prune"]; +/// The container event that makes a new image of a container. +const COMMIT: &str = "commit"; +const HEALTH: &str = "health_status"; + +/// Which listings an engine's events made stale. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct Changes { + containers: bool, + images: bool, +} + +impl Changes { + /// Nothing to list again. + pub const NONE: Self = Self { + containers: false, + images: false, + }; + /// Both listings. + pub const ALL: Self = Self { + containers: true, + images: true, + }; + pub const CONTAINERS: Self = Self { + containers: true, + images: false, + }; + pub const IMAGES: Self = Self { + containers: false, + images: true, + }; + + pub fn containers(self) -> bool { + self.containers + } + + pub fn images(self) -> bool { + self.images + } + + pub fn is_empty(self) -> bool { + self == Self::NONE + } +} + +impl std::ops::BitOr for Changes { + type Output = Self; + + fn bitor(self, other: Self) -> Self { + Self { + containers: self.containers || other.containers, + images: self.images || other.images, + } + } +} + +impl std::ops::BitOrAssign for Changes { + fn bitor_assign(&mut self, other: Self) { + *self = *self | other; + } +} + +/// Reads the output of [`Engine::events`](crate::Engine::events), Docker's +/// or Podman's, in chunks as they arrive. +#[derive(Debug, Default)] +pub struct EventReader { + /// The end of the last chunk, up to its first newline. + partial: Vec, + /// The rest of a line too long to keep is skipped. + skipping: bool, + /// The last health status heard of each container. + health: HashMap, +} + +impl EventReader { + /// The listings the complete lines of `chunk`, with what came before it, + /// make stale. + pub fn feed(&mut self, chunk: &[u8]) -> Changes { + let mut changes = Changes::NONE; + let mut rest = chunk; + while let Some(end) = rest.iter().position(|&byte| byte == b'\n') { + let (line, after) = rest.split_at(end); + rest = &after[1..]; + if std::mem::take(&mut self.skipping) { + self.partial.clear(); + continue; + } + if self.partial.is_empty() { + changes |= self.line(line); + } else { + let mut whole = std::mem::take(&mut self.partial); + whole.extend_from_slice(line); + changes |= self.line(&whole); + } + } + if !self.skipping { + self.partial.extend_from_slice(rest); + if self.partial.len() > MAX_LINE { + self.partial = Vec::new(); + self.skipping = true; + changes = Changes::ALL; + } + } + changes + } + + fn line(&mut self, line: &[u8]) -> Changes { + if line.iter().all(u8::is_ascii_whitespace) { + return Changes::NONE; + } + let Ok(event) = serde_json::from_slice::(line) else { + return Changes::ALL; + }; + let Some(action) = event.action() else { + return Changes::ALL; + }; + let (name, detail) = match action.split_once(':') { + Some((name, detail)) => (name.trim(), Some(detail.trim())), + None => (action.trim(), None), + }; + match event.kind.as_str() { + "container" => self.container(&event, name, detail), + "image" => image(name), + // Only containers and images are asked for. + _ => Changes::NONE, + } + } + + fn container(&mut self, event: &Event, name: &str, detail: Option<&str>) -> Changes { + let id = event.id(); + if name == HEALTH { + let status = event.health.as_deref().or(detail); + return match (id, status) { + (Some(id), Some(status)) => self.health_changed(id, status), + _ => Changes::CONTAINERS, + }; + } + if QUIET_CONTAINER.contains(&name) { + return Changes::NONE; + } + // Whatever else happened to it, its next health status is news. + if let Some(id) = id { + self.health.remove(id); + } + if name == COMMIT { + Changes::ALL + } else { + Changes::CONTAINERS + } + } + + fn health_changed(&mut self, id: &str, status: &str) -> Changes { + if self.health.get(id).is_some_and(|known| known == status) { + return Changes::NONE; + } + if self.health.len() >= MAX_HEALTH && !self.health.contains_key(id) { + self.health.clear(); + } + self.health.insert(id.to_owned(), status.to_owned()); + Changes::CONTAINERS + } +} + +fn image(name: &str) -> Changes { + if QUIET_IMAGE.contains(&name) { + Changes::NONE + } else if RENAMING_IMAGE.contains(&name) { + Changes::ALL + } else { + Changes::IMAGES + } +} + +/// One event, as either engine prints it: Docker names what happened +/// `Action` (with a deprecated `status`) and the object in `Actor`, Podman +/// names it `Status` with the object's `ID` and a separate `HealthStatus`. +#[derive(Deserialize)] +struct Event { + #[serde(rename = "Type", default)] + kind: String, + #[serde(rename = "Action")] + action: Option, + #[serde(rename = "Status")] + podman_status: Option, + #[serde(rename = "status")] + docker_status: Option, + #[serde(rename = "Actor")] + actor: Option, + #[serde(rename = "ID")] + podman_id: Option, + #[serde(rename = "id")] + docker_id: Option, + #[serde(rename = "HealthStatus")] + health: Option, +} + +#[derive(Deserialize)] +struct Actor { + #[serde(rename = "ID")] + id: Option, +} + +impl Event { + fn action(&self) -> Option<&str> { + [&self.action, &self.podman_status, &self.docker_status] + .into_iter() + .find_map(|value| value.as_deref().filter(|value| !value.trim().is_empty())) + } + + fn id(&self) -> Option<&str> { + self.actor + .as_ref() + .and_then(|actor| actor.id.as_deref()) + .or(self.podman_id.as_deref()) + .or(self.docker_id.as_deref()) + .filter(|id| !id.is_empty()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/containers/src/events/tests.rs b/crates/containers/src/events/tests.rs new file mode 100644 index 00000000..9d32b51d --- /dev/null +++ b/crates/containers/src/events/tests.rs @@ -0,0 +1,170 @@ +use super::*; + +/// A Docker event line, as `docker events --format '{{json .}}'` prints it. +fn docker(kind: &str, action: &str, id: &str) -> String { + format!( + r#"{{"status":"{action}","id":"{id}","Type":"{kind}","Action":"{action}","Actor":{{"ID":"{id}","Attributes":{{"name":"pg"}}}},"scope":"local","time":1}}"# + ) + "\n" +} + +/// A Podman event line, as `podman events --format json` prints it. +fn podman(action: &str, id: &str, health: Option<&str>) -> String { + let health = health.map_or(String::new(), |health| { + format!(r#","HealthStatus":"{health}""#) + }); + format!(r#"{{"ID":"{id}","Name":"pg","Status":"{action}","Type":"container"{health}}}"#) + "\n" +} + +fn feed(reader: &mut EventReader, line: &str) -> Changes { + reader.feed(line.as_bytes()) +} + +#[test] +fn health_check_commands_change_no_listing() { + let mut reader = EventReader::default(); + for action in [ + "exec_create: pg_isready -U nocterm", + "exec_start: pg_isready -U nocterm", + "exec_die", + ] { + assert_eq!( + feed(&mut reader, &docker("container", action, "c1")), + Changes::NONE + ); + } + for action in ["exec", "exec_died"] { + assert_eq!( + feed(&mut reader, &podman(action, "c1", None)), + Changes::NONE + ); + } +} + +#[test] +fn container_life_changes_only_the_container_listing() { + let mut reader = EventReader::default(); + for action in [ + "create", "start", "die", "stop", "pause", "destroy", "rename", + ] { + assert_eq!( + feed(&mut reader, &docker("container", action, "c1")), + Changes::CONTAINERS, + "{action}" + ); + } + assert_eq!( + feed(&mut reader, &podman("died", "c1", None)), + Changes::CONTAINERS + ); + assert_eq!( + feed(&mut reader, &docker("container", "commit", "c1")), + Changes::ALL, + "a commit makes an image" + ); +} + +#[test] +fn image_events_change_the_image_listing_and_renames_both() { + let mut reader = EventReader::default(); + assert_eq!( + feed(&mut reader, &docker("image", "pull", "alpine:3")), + Changes::IMAGES + ); + assert_eq!( + feed(&mut reader, &docker("image", "push", "alpine:3")), + Changes::NONE + ); + for action in ["tag", "untag", "delete"] { + assert_eq!( + feed(&mut reader, &docker("image", action, "sha256:1")), + Changes::ALL, + "containers may be shown by the image's name: {action}" + ); + } + assert_eq!( + feed(&mut reader, &docker("network", "connect", "n1")), + Changes::NONE + ); +} + +#[test] +fn only_a_new_health_status_changes_the_listing() { + let mut reader = EventReader::default(); + let healthy = docker("container", "health_status: healthy", "c1"); + assert_eq!(feed(&mut reader, &healthy), Changes::CONTAINERS); + assert_eq!(feed(&mut reader, &healthy), Changes::NONE); + assert_eq!( + feed( + &mut reader, + &docker("container", "health_status: healthy", "c2") + ), + Changes::CONTAINERS, + "another container's status is its own" + ); + assert_eq!( + feed( + &mut reader, + &docker("container", "health_status: unhealthy", "c1") + ), + Changes::CONTAINERS + ); + + // Podman repeats the status after every check. + let repeated = podman("health_status", "p1", Some("healthy")); + assert_eq!(feed(&mut reader, &repeated), Changes::CONTAINERS); + assert_eq!(feed(&mut reader, &repeated), Changes::NONE); + // A restart resets the status the listing shows, so the same one is news again. + assert_eq!( + feed(&mut reader, &podman("restart", "p1", None)), + Changes::CONTAINERS + ); + assert_eq!(feed(&mut reader, &repeated), Changes::CONTAINERS); + // Without a status to compare, the listing is the only way to know. + let unknown = podman("health_status", "p1", None); + assert_eq!(feed(&mut reader, &unknown), Changes::CONTAINERS); +} + +#[test] +fn lines_split_across_chunks_are_read_whole() { + let mut reader = EventReader::default(); + let line = docker("container", "start", "c1"); + let (head, tail) = line.split_at(line.len() / 2); + assert_eq!(feed(&mut reader, head), Changes::NONE); + assert_eq!(feed(&mut reader, tail), Changes::CONTAINERS); + + let quiet = docker("container", "exec_die", "c1"); + let both = format!("{quiet}{}", docker("image", "pull", "alpine")); + assert_eq!(feed(&mut reader, &both), Changes::IMAGES); +} + +#[test] +fn unreadable_lines_relist_everything() { + let mut reader = EventReader::default(); + assert_eq!(feed(&mut reader, "\n \n"), Changes::NONE); + assert_eq!(feed(&mut reader, "container die\n"), Changes::ALL); + assert_eq!( + feed(&mut reader, "{\"Type\":\"container\"}\n"), + Changes::ALL + ); + + let long = "x".repeat(MAX_LINE + 1); + assert_eq!(feed(&mut reader, &long), Changes::ALL); + assert_eq!( + feed(&mut reader, "rest of it\n"), + Changes::NONE, + "the rest of a dropped line is skipped" + ); + assert_eq!( + feed(&mut reader, &docker("container", "start", "c1")), + Changes::CONTAINERS + ); +} + +#[test] +fn changes_combine() { + assert_eq!(Changes::CONTAINERS | Changes::IMAGES, Changes::ALL); + let mut changes = Changes::NONE; + assert!(changes.is_empty()); + changes |= Changes::IMAGES; + assert!(changes.images() && !changes.containers()); +} diff --git a/crates/containers/src/lib.rs b/crates/containers/src/lib.rs index af15c35c..ee99e9ab 100644 --- a/crates/containers/src/lib.rs +++ b/crates/containers/src/lib.rs @@ -1,11 +1,12 @@ //! Containers on a host, independent of any UI. //! -//! Owns: engine detection, CLI requests, listing parsing and container actions. +//! Owns: engine detection, CLI requests, listing parsing, which events change a listing +//! and container actions. //! Does not own: session creation, daemon sockets, credential storage or views. //! Ports used: HostExec. Ports provided: none. Settings section: none. Globals: none. //! Invariants: operations use the supplied host executor and its authority; //! unavailable engines remain distinct from typed execution or decoding failures; -//! listing output is bounded. +//! listing output and event lines are bounded; an unreadable event changes every listing. //! //! A host is asked about its containers through a [`HostExec`], with the //! container engine's own command line: Docker's, or Podman's where Docker @@ -14,7 +15,8 @@ //! with the rights of the user signed in. //! //! [`Engine`] builds the commands — listing, the event stream that says -//! when to list again, actions, and the programs a tab runs (a container's +//! when to list again (read by [`EventReader`], which keeps only the events +//! that change a listing and says which), actions, and the programs a tab runs (a container's //! log, a shell inside it) — and [`Snapshot`] is what a listing found, //! grouped by Compose project for display. //! @@ -22,9 +24,21 @@ mod engine; mod error; +mod events; mod model; mod parse; pub use engine::{Action, Engine, detect}; pub use error::{ContainersError, ListingEntity, ListingError}; +pub use events::{Changes, EventReader}; pub use model::{Container, Group, Image, Snapshot, State}; + +/// Decode a bounded container listing acquired by a host adapter. +pub fn decode_containers(output: &str) -> Result, ListingError> { + parse::containers(output) +} + +/// Decode a bounded image listing acquired by a host adapter. +pub fn decode_images(output: &str) -> Result, ListingError> { + parse::images(output) +} diff --git a/crates/core/Cargo.toml b/crates/core/Cargo.toml index e38f8e66..abb93517 100644 --- a/crates/core/Cargo.toml +++ b/crates/core/Cargo.toml @@ -13,13 +13,17 @@ layer = "foundation" directories.workspace = true serde.workspace = true thiserror.workspace = true +sha2.workspace = true toml.workspace = true toml_edit.workspace = true tempfile.workspace = true futures.workspace = true [target.'cfg(unix)'.dependencies] -nix = { version = "0.28", features = ["user"] } +nix = { version = "0.28", features = ["fs", "user"] } + +[features] +test-support = [] [dev-dependencies] tempfile.workspace = true diff --git a/crates/core/src/allocation.rs b/crates/core/src/allocation.rs new file mode 100644 index 00000000..60982fd1 --- /dev/null +++ b/crates/core/src/allocation.rs @@ -0,0 +1,121 @@ +//! Opaque allocation lifetime carriers shared by actual native quota owners. +//! +//! Counts describe the owner's real reservation. This module creates no quota, +//! authenticates no source, and grants no execution or publication authority. +use std::{fmt, sync::Arc}; + +struct RetainedAllocation { + bytes: usize, + payloads: usize, + _keepalive: Box, +} +/// Clones retain one actual reservation until the final lease is dropped. +#[derive(Clone)] +pub struct AllocationLease(Arc); +impl AllocationLease { + /// The supplied owner must retain the actual permits matching these immutable + /// counts. Descriptive counts or an arbitrary keepalive do not prove admission. + pub fn new(bytes: usize, payloads: usize, keepalive: impl Send + Sync + 'static) -> Self { + Self(Arc::new(RetainedAllocation { + bytes, + payloads, + _keepalive: Box::new(keepalive), + })) + } + pub fn bytes(&self) -> usize { + self.0.bytes + } + pub fn payloads(&self) -> usize { + self.0.payloads + } +} +impl fmt::Debug for AllocationLease { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("AllocationLease") + .field("bytes", &self.bytes()) + .field("payloads", &self.payloads()) + .finish_non_exhaustive() + } +} +/// Owns the actual value and its reservation together. Share this generation with +/// `Arc>`; a deep clone of T requires a separate real reservation. +/// There is deliberately no Clone or uncharged consuming extraction API. +pub struct Allocated { + // Field order keeps the reservation alive through destruction of the value. + value: T, + allocation: AllocationLease, +} +impl Allocated { + pub fn new(value: T, allocation: AllocationLease) -> Self { + Self { value, allocation } + } + pub fn value(&self) -> &T { + &self.value + } + pub fn allocation(&self) -> &AllocationLease { + &self.allocation + } + /// Retains the same reservation throughout a consuming transformation. + /// The owner must first reserve separately if the transformation can exceed + /// this reservation or retain another independently allocated generation. + pub fn map(self, transform: impl FnOnce(T) -> U) -> Allocated { + let Self { value, allocation } = self; + Allocated { + value: transform(value), + allocation, + } + } +} +impl fmt::Debug for Allocated { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("Allocated") + .field("allocation", &self.allocation) + .finish_non_exhaustive() + } +} + +#[cfg(test)] +mod tests { + use super::*; + use std::sync::atomic::{AtomicUsize, Ordering}; + struct Guard(Arc); + impl Drop for Guard { + fn drop(&mut self) { + self.0.fetch_add(1, Ordering::SeqCst); + } + } + #[test] + fn shared_generations_keep_one_actual_reservation_until_the_last_reference_drops() { + let released = Arc::new(AtomicUsize::new(0)); + let allocation = AllocationLease::new(8, 1, Guard(released.clone())); + let observer = allocation.clone(); + let value = Arc::new(Allocated::new(7u32, allocation)); + let native_worker = value.clone(); + drop(value); + drop(observer); + assert_eq!(released.load(Ordering::SeqCst), 0); + assert_eq!( + ( + *native_worker.value(), + native_worker.allocation().bytes(), + native_worker.allocation().payloads() + ), + (7, 8, 1) + ); + drop(native_worker); + assert_eq!(released.load(Ordering::SeqCst), 1); + } + #[test] + fn consuming_a_value_transfers_its_allocation_without_an_uncharged_gap() { + let released = Arc::new(AtomicUsize::new(0)); + let value = Allocated::new(3u32, AllocationLease::new(8, 0, Guard(released.clone()))); + let changed = value.map(|value| { + assert_eq!(released.load(Ordering::SeqCst), 0); + u64::from(value) + 1 + }); + assert_eq!(*changed.value(), 4); + assert_eq!(released.load(Ordering::SeqCst), 0); + drop(changed); + assert_eq!(released.load(Ordering::SeqCst), 1); + } +} diff --git a/crates/core/src/bounded_io.rs b/crates/core/src/bounded_io.rs new file mode 100644 index 00000000..ac19722b --- /dev/null +++ b/crates/core/src/bounded_io.rs @@ -0,0 +1,103 @@ +//! In-memory byte sinks with caller-supplied bounds; no filesystem or codec policy. +use std::io::{self, Write}; + +/// Counts bytes without retaining them and refuses writes beyond its limit. +pub struct CountingWriter { + count: usize, + limit: usize, +} +impl CountingWriter { + pub fn new(limit: usize) -> Self { + Self { count: 0, limit } + } + pub fn count(&self) -> usize { + self.count + } +} +impl Write for CountingWriter { + fn write(&mut self, bytes: &[u8]) -> io::Result { + self.count = self + .count + .checked_add(bytes.len()) + .filter(|count| *count <= self.limit) + .ok_or_else(capacity)?; + Ok(bytes.len()) + } + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} + +/// Retains at most `limit` bytes; a rejected write leaves the buffer untouched. +pub struct BoundedBuffer { + bytes: Vec, + limit: usize, + exceeded: bool, +} +impl BoundedBuffer { + pub fn new(limit: usize) -> Self { + Self::with_capacity(limit, 0) + } + pub fn with_capacity(limit: usize, capacity: usize) -> Self { + Self { + bytes: Vec::with_capacity(capacity.min(limit)), + limit, + exceeded: false, + } + } + pub fn exceeded(&self) -> bool { + self.exceeded + } + pub fn into_inner(self) -> Vec { + self.bytes + } +} +impl Write for BoundedBuffer { + fn write(&mut self, bytes: &[u8]) -> io::Result { + if bytes.len() > self.limit.saturating_sub(self.bytes.len()) { + self.exceeded = true; + return Err(capacity()); + } + self.bytes.extend_from_slice(bytes); + Ok(bytes.len()) + } + fn flush(&mut self) -> io::Result<()> { + Ok(()) + } +} +fn capacity() -> io::Error { + io::ErrorKind::OutOfMemory.into() +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn exact_limit_is_accepted_and_failed_write_retains_prefix() { + let mut output = BoundedBuffer::with_capacity(3, 100); + output.write_all(b"abc").unwrap(); + assert!(!output.exceeded()); + assert_eq!( + output.write_all(b"d").unwrap_err().kind(), + io::ErrorKind::OutOfMemory + ); + assert!(output.exceeded()); + assert_eq!(output.into_inner(), b"abc"); + } + + #[test] + fn counting_refuses_overflow_without_changing_accepted_count() { + let mut output = CountingWriter::new(3); + output.write_all(b"abc").unwrap(); + assert_eq!( + output.write_all(b"d").unwrap_err().kind(), + io::ErrorKind::OutOfMemory + ); + assert_eq!(output.count(), 3); + output.write_all(b"").unwrap(); + let mut empty = CountingWriter::new(0); + assert!(empty.write_all(b"x").is_err()); + assert_eq!(empty.count(), 0); + } +} diff --git a/crates/core/src/clock.rs b/crates/core/src/clock.rs new file mode 100644 index 00000000..23dc9e33 --- /dev/null +++ b/crates/core/src/clock.rs @@ -0,0 +1,119 @@ +//! The wall clock as a contract: the local date and time are read through [`WallClock`], never +//! from the system by a consumer. +//! +//! [`WallTime`] holds only integers, so no calendar or time-zone library is needed here. The +//! native reading is an adapter's (`nocterm-local`); `FixedClock` serves tests. + +#[cfg(any(test, feature = "test-support"))] +use std::sync::{Mutex, PoisonError}; + +/// An instant on the wall clock: UTC seconds and the offset of the local time zone then. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] +pub struct WallTime { + /// Seconds since 1970-01-01 00:00:00 UTC. + pub unix_seconds: i64, + /// Seconds the local time is ahead of UTC; negative west of Greenwich. + pub utc_offset_seconds: i32, +} + +const SECONDS_PER_DAY: i64 = 86_400; + +impl WallTime { + /// Seconds since local midnight, in `0..86_400`. + pub fn local_second_of_day(self) -> u32 { + let local = self + .unix_seconds + .saturating_add(i64::from(self.utc_offset_seconds)); + local.rem_euclid(SECONDS_PER_DAY) as u32 + } + + /// The local hour (`0..24`) and minute (`0..60`). + pub fn local_hour_minute(self) -> (u8, u8) { + let (hour, minute, _) = self.local_hour_minute_second(); + (hour, minute) + } + + /// The local hour (`0..24`), minute (`0..60`) and second (`0..60`). + pub fn local_hour_minute_second(self) -> (u8, u8, u8) { + let second = self.local_second_of_day(); + ( + (second / 3600) as u8, + (second % 3600 / 60) as u8, + (second % 60) as u8, + ) + } + + /// The local calendar date as year, month (`1..=12`) and day (`1..=31`), in the proleptic + /// Gregorian calendar. + pub fn local_date(self) -> (i64, u8, u8) { + let local = self + .unix_seconds + .saturating_add(i64::from(self.utc_offset_seconds)); + // Days since 1970-01-01 to a civil date (Howard Hinnant's `civil_from_days`). + let z = local.div_euclid(SECONDS_PER_DAY) + 719_468; + let era = z.div_euclid(146_097); + let day_of_era = z.rem_euclid(146_097); + let year_of_era = + (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365; + let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); + let shifted_month = (5 * day_of_year + 2) / 153; + let day = (day_of_year - (153 * shifted_month + 2) / 5 + 1) as u8; + let month = if shifted_month < 10 { + shifted_month + 3 + } else { + shifted_month - 9 + } as u8; + let year = year_of_era + era * 400 + i64::from(month <= 2); + (year, month, day) + } + + /// Seconds until the local minute changes, in `1..=60`. + pub fn seconds_to_next_minute(self) -> u8 { + 60 - (self.local_second_of_day() % 60) as u8 + } +} + +/// Reads the wall clock. +pub trait WallClock: Send + Sync + 'static { + /// The current instant. Infallible: an adapter that cannot read the time zone reports UTC. + fn now(&self) -> WallTime; +} + +/// A clock a test sets and moves. +#[cfg(any(test, feature = "test-support"))] +#[derive(Debug, Default)] +pub struct FixedClock { + time: Mutex, + reads: std::sync::atomic::AtomicUsize, +} + +#[cfg(any(test, feature = "test-support"))] +impl FixedClock { + pub fn new(time: WallTime) -> Self { + Self { + time: Mutex::new(time), + reads: Default::default(), + } + } + + /// Moves the clock to `time`. + pub fn set(&self, time: WallTime) { + *self.time.lock().unwrap_or_else(PoisonError::into_inner) = time; + } + + /// How many times the clock was read. + pub fn reads(&self) -> usize { + self.reads.load(std::sync::atomic::Ordering::SeqCst) + } +} + +#[cfg(any(test, feature = "test-support"))] +impl WallClock for FixedClock { + fn now(&self) -> WallTime { + self.reads.fetch_add(1, std::sync::atomic::Ordering::SeqCst); + *self.time.lock().unwrap_or_else(PoisonError::into_inner) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/core/src/clock/tests.rs b/crates/core/src/clock/tests.rs new file mode 100644 index 00000000..59748d69 --- /dev/null +++ b/crates/core/src/clock/tests.rs @@ -0,0 +1,83 @@ +use super::*; + +fn time(unix_seconds: i64, utc_offset_seconds: i32) -> WallTime { + WallTime { + unix_seconds, + utc_offset_seconds, + } +} + +#[test] +fn utc_midnight_is_zero_zero() { + assert_eq!(time(0, 0).local_hour_minute(), (0, 0)); + assert_eq!(time(86_400 * 3, 0).local_hour_minute(), (0, 0)); +} + +#[test] +fn the_offset_moves_the_local_time_across_midnight() { + // 23:30 UTC is 05:00 the next day at +05:30 and 18:30 at -05:00. + let utc = 23 * 3600 + 30 * 60; + assert_eq!(time(utc, 5 * 3600 + 1800).local_hour_minute(), (5, 0)); + assert_eq!(time(utc, -5 * 3600).local_hour_minute(), (18, 30)); +} + +#[test] +fn a_time_before_the_epoch_still_lands_in_the_day() { + // 1969-12-31 23:59:59 UTC. + assert_eq!(time(-1, 0).local_hour_minute(), (23, 59)); + assert_eq!(time(-1, 3600).local_hour_minute(), (0, 59)); +} + +#[test] +fn the_wait_for_the_next_minute_is_between_one_and_sixty_seconds() { + assert_eq!(time(0, 0).seconds_to_next_minute(), 60); + assert_eq!(time(59, 0).seconds_to_next_minute(), 1); + assert_eq!(time(61, 0).seconds_to_next_minute(), 59); + assert_eq!(time(10, 45).seconds_to_next_minute(), 5); +} + +#[test] +fn the_local_second_is_part_of_the_time_of_day() { + assert_eq!(time(0, 0).local_hour_minute_second(), (0, 0, 0)); + assert_eq!( + time(9 * 3600 + 5 * 60 + 7, 0).local_hour_minute_second(), + (9, 5, 7) + ); + assert_eq!(time(-1, 0).local_hour_minute_second(), (23, 59, 59)); +} + +#[test] +fn the_local_date_is_the_calendar_day_of_the_local_time() { + assert_eq!(time(0, 0).local_date(), (1970, 1, 1)); + assert_eq!(time(-1, 0).local_date(), (1969, 12, 31)); + // 2024-02-29 12:00:00 UTC, a leap day. + assert_eq!(time(1_709_208_000, 0).local_date(), (2024, 2, 29)); + // 2026-10-24 23:30:00 UTC is already the 25th at +05:00 and still the 24th at -05:00. + let utc = 1_792_884_600; + assert_eq!(time(utc, 0).local_date(), (2026, 10, 24)); + assert_eq!(time(utc, 5 * 3600).local_date(), (2026, 10, 25)); + assert_eq!(time(utc, -5 * 3600).local_date(), (2026, 10, 24)); + // 2000-03-01 00:00:00 UTC follows the leap day of a century year. + assert_eq!(time(951_868_800, 0).local_date(), (2000, 3, 1)); + assert_eq!(time(951_868_799, 0).local_date(), (2000, 2, 29)); +} + +#[test] +fn extreme_values_do_not_overflow() { + for unix_seconds in [i64::MIN, i64::MAX] { + let time = time(unix_seconds, i32::MAX); + let (hour, minute) = time.local_hour_minute(); + assert!(hour < 24 && minute < 60); + let (_, month, day) = time.local_date(); + assert!((1..=12).contains(&month) && (1..=31).contains(&day)); + } +} + +#[test] +fn a_fixed_clock_reports_what_was_set_and_counts_reads() { + let clock = FixedClock::new(time(60, 0)); + assert_eq!(clock.now(), time(60, 0)); + clock.set(time(120, 3600)); + assert_eq!(clock.now(), time(120, 3600)); + assert_eq!(clock.reads(), 2); +} diff --git a/crates/core/src/error.rs b/crates/core/src/error.rs index d0053c15..14f9f43b 100644 --- a/crates/core/src/error.rs +++ b/crates/core/src/error.rs @@ -1,6 +1,21 @@ //! Immutable shared causes preserve the original concrete error in source chains. use std::{error::Error, fmt, sync::Arc}; +/// The error and every cause beneath it as one line, outermost first, for a log record. +/// +/// It prints only what each frame's `Display` shows; callers log chains of typed errors +/// whose messages carry no secrets. +pub fn chain(error: &(dyn Error + 'static)) -> String { + let mut line = error.to_string(); + let mut cause = error.source(); + while let Some(next) = cause { + line.push_str(": "); + line.push_str(&next.to_string()); + cause = next.source(); + } + line +} + /// Cheaply clone an error without requiring the original error to implement Clone. /// /// The holder is one source frame; the next frame is the original error, including diff --git a/crates/core/src/error/tests.rs b/crates/core/src/error/tests.rs index 8e1a1d86..069ef7e0 100644 --- a/crates/core/src/error/tests.rs +++ b/crates/core/src/error/tests.rs @@ -74,3 +74,14 @@ fn construction_from_arc_reuses_the_existing_allocation() { erased.downcast_ref().unwrap() )); } + +#[test] +fn a_chain_lists_the_error_and_every_cause_outermost_first() { + let error = Operation(SharedCause::new(io::Error::new( + io::ErrorKind::PermissionDenied, + "denied", + ))); + assert_eq!(chain(&error), "operation failed: denied: denied: denied"); + let plain = io::Error::new(io::ErrorKind::NotFound, "missing"); + assert_eq!(chain(&plain), "missing"); +} diff --git a/crates/core/src/fs.rs b/crates/core/src/fs.rs index 429a30aa..0289aeb4 100644 --- a/crates/core/src/fs.rs +++ b/crates/core/src/fs.rs @@ -1,8 +1,14 @@ -//! Exclusive private-file creation and staged atomic publication. +//! Exclusive private-file creation, staged atomic publication and bounded reads. //! -//! These helpers replace the final path without following its symlink. Parent +//! These helpers replace or open the final path without following its symlink. Parent //! directories must be trusted: this is not a pinned-directory traversal API. +mod bounded; +mod private; + +pub use bounded::{BoundedReadError, read_regular_bounded}; +pub use private::{PrivateFileError, open_private_regular, require_private_regular}; + use std::{ fs::{self, File, OpenOptions, Permissions}, io::{self, Write as _}, @@ -75,6 +81,19 @@ pub struct AtomicPublication { destination: PathBuf, } +/// A completed namespace replacement whose parent directory still needs best-effort sync. +/// Dropping this receipt cannot remove or roll back the published file. +#[must_use = "finish parent sync outside the owner's publication lock"] +pub struct CommittedPublication { + destination: PathBuf, +} + +impl CommittedPublication { + pub fn sync_parent(self) { + sync_parent_best_effort(&self.destination); + } +} + impl AtomicPublication { /// Stages a mode-0600 snapshot in an existing parent directory. pub fn stage_private(path: &Path, bytes: &[u8]) -> Result { @@ -135,15 +154,46 @@ impl AtomicPublication { /// in-memory state. Best-effort directory sync cannot turn a commit into an /// apparent failed write. Ancestors are not pinned against concurrent changes. pub fn publish(self) -> Result<(), FileError> { + self.commit()?.sync_parent(); + Ok(()) + } + + /// Commits only the namespace replacement. The caller can publish matching + /// memory under its authority lock, then sync the parent after releasing it. + pub fn commit(self) -> Result { self.temporary .persist(&self.destination) .map_err(|error| FileError::at(&self.destination, error.error))?; - #[cfg(unix)] - if let Ok(directory) = File::open(parent_directory(&self.destination)) { - let _ = directory.sync_all(); - } - Ok(()) + Ok(CommittedPublication { + destination: self.destination, + }) + } + + /// Publishes only if the final path does not exist, including a dangling link. + /// Returns the actual published descriptor; no destination is overwritten. + /// + /// Success commits the namespace operation. The tempfile fallback can leave + /// a second staging hard link, and parent sync is best effort. A private-key + /// owner must validate this descriptor and confirm held-parent durability + /// before announcing readiness. A postcommit refusal must retain the identity, + /// rather than deleting or silently regenerating the destination. + pub fn publish_new(self) -> Result { + let file = self + .temporary + .persist_noclobber(&self.destination) + .map_err(|error| FileError::at(&self.destination, error.error))?; + sync_parent_best_effort(&self.destination); + Ok(file) + } +} + +fn sync_parent_best_effort(path: &Path) { + #[cfg(unix)] + if let Ok(directory) = File::open(parent_directory(path)) { + let _ = directory.sync_all(); } + #[cfg(not(unix))] + let _ = path; } fn parent_directory(path: &Path) -> &Path { diff --git a/crates/core/src/fs/bounded.rs b/crates/core/src/fs/bounded.rs new file mode 100644 index 00000000..eb950bd5 --- /dev/null +++ b/crates/core/src/fs/bounded.rs @@ -0,0 +1,144 @@ +//! Reads one regular file without following a final link, within a size bound. + +use std::{ + fs::{File, Metadata, OpenOptions}, + io::{self, Read as _}, + path::{Path, PathBuf}, +}; + +/// A bounded read refused the path or failed. +#[derive(Debug, thiserror::Error)] +pub enum BoundedReadError { + /// The path is not a regular file, or its final component is a link. + #[error("{}: not a regular file", path.display())] + NotRegular { path: PathBuf }, + /// The file holds more than the allowed bytes. + #[error("{}: larger than {cap} bytes", path.display())] + TooLarge { path: PathBuf, cap: usize }, + #[error("{}: {source}", path.display())] + Io { path: PathBuf, source: io::Error }, +} + +impl BoundedReadError { + fn io(path: &Path, source: io::Error) -> Self { + Self::Io { + path: path.to_owned(), + source, + } + } + + /// The operating-system error kind, when the failure came from the system. + pub fn io_kind(&self) -> Option { + match self { + Self::Io { source, .. } => Some(source.kind()), + Self::NotRegular { .. } | Self::TooLarge { .. } => None, + } + } +} + +/// Reads a regular file of at most `cap` bytes, never following a link at the final component. +/// +/// The file is opened first (`O_NOFOLLOW | O_NONBLOCK | O_CLOEXEC` on Unix, with +/// `FILE_FLAG_OPEN_REPARSE_POINT` and a reparse-point check on Windows) and its kind and size +/// are read from the open handle, so a path swapped after the check cannot change what is +/// read. A named pipe or device is refused without blocking. The read itself stops one byte +/// past `cap`, so a file that grows while it is read cannot exceed the bound. +/// +/// Parent directories must be trusted: this is not a pinned-directory traversal API. Callers +/// that hash the content hash the returned buffer, which is the exact content they parse. +pub fn read_regular_bounded(path: &Path, cap: usize) -> Result, BoundedReadError> { + let (file, metadata) = open_regular(path)?; + let limit = u64::try_from(cap).unwrap_or(u64::MAX); + if metadata.len() > limit { + return Err(BoundedReadError::TooLarge { + path: path.to_owned(), + cap, + }); + } + let mut bytes = Vec::with_capacity(usize::try_from(metadata.len()).unwrap_or(0)); + file.take(limit.saturating_add(1)) + .read_to_end(&mut bytes) + .map_err(|source| BoundedReadError::io(path, source))?; + if bytes.len() > cap { + return Err(BoundedReadError::TooLarge { + path: path.to_owned(), + cap, + }); + } + Ok(bytes) +} + +pub(super) fn open_regular(path: &Path) -> Result<(File, Metadata), BoundedReadError> { + let file = open_without_following(path).map_err(|source| { + if is_link_refusal(&source) { + BoundedReadError::NotRegular { + path: path.to_owned(), + } + } else { + BoundedReadError::io(path, source) + } + })?; + let metadata = file + .metadata() + .map_err(|source| BoundedReadError::io(path, source))?; + if !metadata.is_file() || is_reparse_point(&metadata) { + return Err(BoundedReadError::NotRegular { + path: path.to_owned(), + }); + } + Ok((file, metadata)) +} + +#[cfg(unix)] +fn open_without_following(path: &Path) -> io::Result { + use nix::fcntl::OFlag; + use std::os::unix::fs::OpenOptionsExt as _; + OpenOptions::new() + .read(true) + .custom_flags( + (OFlag::O_NOFOLLOW | OFlag::O_NONBLOCK | OFlag::O_NOCTTY | OFlag::O_CLOEXEC).bits(), + ) + .open(path) +} + +#[cfg(windows)] +fn open_without_following(path: &Path) -> io::Result { + use std::os::windows::fs::OpenOptionsExt as _; + /// `FILE_FLAG_OPEN_REPARSE_POINT`: open the link itself, not its target. + const OPEN_REPARSE_POINT: u32 = 0x0020_0000; + OpenOptions::new() + .read(true) + .custom_flags(OPEN_REPARSE_POINT) + .open(path) +} + +#[cfg(not(any(unix, windows)))] +fn open_without_following(path: &Path) -> io::Result { + OpenOptions::new().read(true).open(path) +} + +/// `O_NOFOLLOW` reports a final link as ELOOP (Linux) or EMLINK (BSD, macOS). +#[cfg(unix)] +fn is_link_refusal(error: &io::Error) -> bool { + use nix::errno::Errno; + error + .raw_os_error() + .is_some_and(|code| matches!(Errno::from_raw(code), Errno::ELOOP | Errno::EMLINK)) +} + +#[cfg(not(unix))] +fn is_link_refusal(_: &io::Error) -> bool { + false +} + +#[cfg(windows)] +fn is_reparse_point(metadata: &Metadata) -> bool { + use std::os::windows::fs::MetadataExt as _; + const FILE_ATTRIBUTE_REPARSE_POINT: u32 = 0x400; + metadata.file_attributes() & FILE_ATTRIBUTE_REPARSE_POINT != 0 +} + +#[cfg(not(windows))] +fn is_reparse_point(_: &Metadata) -> bool { + false +} diff --git a/crates/core/src/fs/private.rs b/crates/core/src/fs/private.rs new file mode 100644 index 00000000..709fc948 --- /dev/null +++ b/crates/core/src/fs/private.rs @@ -0,0 +1,72 @@ +//! Validate the actual opened descriptor before a private owner reads secrets. +use super::bounded::{BoundedReadError, open_regular}; +use std::{ + fs::{File, Metadata}, + io, + path::Path, +}; + +#[derive(Debug, thiserror::Error)] +pub enum PrivateFileError { + #[error("the private file could not be opened")] + Open(#[source] BoundedReadError), + #[error("the opened private file could not be inspected")] + Inspect(#[source] io::Error), + #[error("the opened file is not an owner-only single-link regular file")] + Policy, + #[error("private file ownership checks are unavailable on this platform")] + Unsupported, +} +impl PrivateFileError { + pub fn io_kind(&self) -> Option { + match self { + Self::Open(error) => error.io_kind(), + Self::Inspect(error) => Some(error.kind()), + Self::Policy | Self::Unsupported => None, + } + } +} + +/// Opens without following the final link and checks the opened inode's policy. +/// On Unix only current effective-UID ownership, one link and exact0400/0600 +/// permissions are accepted. Other platforms refuse before opening the path. +/// +/// No secret bytes are read here. The owner supplies its fixed zeroizing buffer +/// and bounds reads, including one-byte growth/overflow detection. Parents must +/// be trusted; this does not pin directory ancestry or create process isolation. +pub fn open_private_regular(path: &Path) -> Result { + if !cfg!(unix) { + return Err(PrivateFileError::Unsupported); + } + let (file, metadata) = open_regular(path).map_err(PrivateFileError::Open)?; + check(&metadata)?; + Ok(file) +} + +/// Applies the same policy to a held descriptor, including after publication. +/// A refusal after publication does not remove or roll back that published file. +pub fn require_private_regular(file: &File) -> Result<(), PrivateFileError> { + if !cfg!(unix) { + return Err(PrivateFileError::Unsupported); + } + let metadata = file.metadata().map_err(PrivateFileError::Inspect)?; + check(&metadata) +} + +#[cfg(unix)] +fn check(metadata: &Metadata) -> Result<(), PrivateFileError> { + use std::os::unix::fs::MetadataExt as _; + if metadata.is_file() + && metadata.uid() == nix::unistd::Uid::effective().as_raw() + && metadata.nlink() == 1 + && matches!(metadata.mode() & 0o7777, 0o400 | 0o600) + { + Ok(()) + } else { + Err(PrivateFileError::Policy) + } +} +#[cfg(not(unix))] +fn check(_: &Metadata) -> Result<(), PrivateFileError> { + Err(PrivateFileError::Unsupported) +} diff --git a/crates/core/src/fs/tests.rs b/crates/core/src/fs/tests.rs index 75b8ab0c..2f02818d 100644 --- a/crates/core/src/fs/tests.rs +++ b/crates/core/src/fs/tests.rs @@ -1,5 +1,26 @@ use super::*; +#[test] +fn namespace_commit_is_visible_before_parent_sync_and_receipt_drop_never_rolls_it_back() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("snapshot"); + fs::write(&path, b"old").unwrap(); + let committed = AtomicPublication::stage_private(&path, b"new") + .unwrap() + .commit() + .unwrap(); + assert_eq!(fs::read(&path).unwrap(), b"new"); + drop(committed); + assert_eq!(fs::read(&path).unwrap(), b"new"); + assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1); + AtomicPublication::stage_private(&path, b"synced") + .unwrap() + .commit() + .unwrap() + .sync_parent(); + assert_eq!(fs::read(path).unwrap(), b"synced"); +} + #[test] fn binary_snapshot_round_trips_and_creates_parents() { let directory = tempfile::tempdir().unwrap(); @@ -34,6 +55,155 @@ fn failed_publication_preserves_destination_and_cleans_staging() { assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1); } +#[test] +fn new_publication_has_one_winner_and_never_overwrites_an_existing_identity() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("identity"); + let first = AtomicPublication::stage_private(&path, b"first identity").unwrap(); + let second = AtomicPublication::stage_private(&path, b"second identity").unwrap(); + let barrier = std::sync::Arc::new(std::sync::Barrier::new(2)); + let ready = barrier.clone(); + let one = std::thread::spawn(move || { + ready.wait(); + first.publish_new() + }); + let two = std::thread::spawn(move || { + barrier.wait(); + second.publish_new() + }); + let outcomes = [one.join().unwrap(), two.join().unwrap()]; + assert_eq!(outcomes.iter().filter(|result| result.is_ok()).count(), 1); + assert_eq!( + outcomes + .iter() + .filter_map(|result| result.as_ref().err()) + .filter(|error| error.kind() == io::ErrorKind::AlreadyExists) + .count(), + 1 + ); + let bytes = fs::read(&path).unwrap(); + assert!(bytes == b"first identity" || bytes == b"second identity"); + assert!( + AtomicPublication::stage_private(&path, b"replacement") + .unwrap() + .publish_new() + .is_err() + ); + assert_eq!(fs::read(&path).unwrap(), bytes); + assert_eq!(fs::read_dir(directory.path()).unwrap().count(), 1); +} + +#[cfg(unix)] +#[test] +fn new_publication_preserves_live_and_dangling_links_without_reading_their_target() { + let directory = tempfile::tempdir().unwrap(); + let target = directory.path().join("target"); + let path = directory.path().join("identity"); + fs::write(&target, b"preserve").unwrap(); + std::os::unix::fs::symlink(&target, &path).unwrap(); + for exists in [true, false] { + if !exists { + fs::remove_file(&target).unwrap(); + } + let result = AtomicPublication::stage_private(&path, b"new identity") + .unwrap() + .publish_new(); + assert_eq!(result.unwrap_err().kind(), io::ErrorKind::AlreadyExists); + assert_eq!(fs::read_link(&path).unwrap(), target); + if exists { + assert_eq!(fs::read(&target).unwrap(), b"preserve"); + } else { + assert!(!target.exists()); + } + } +} + +#[cfg(unix)] +#[test] +fn private_open_checks_actual_permissions_and_links_and_retains_original_file() { + use std::{io::Read as _, os::unix::fs::PermissionsExt as _}; + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("identity"); + let published = AtomicPublication::stage_private(&path, b"original") + .unwrap() + .publish_new() + .unwrap(); + require_private_regular(&published).unwrap(); + for mode in [0o600, 0o400] { + fs::set_permissions(&path, Permissions::from_mode(mode)).unwrap(); + let mut original = open_private_regular(&path).unwrap(); + let mut bytes = Vec::new(); + original.read_to_end(&mut bytes).unwrap(); + assert_eq!(bytes, b"original"); + } + for mode in [0o640, 0o604, 0o700, 0o4600] { + fs::set_permissions(&path, Permissions::from_mode(mode)).unwrap(); + assert!(matches!( + open_private_regular(&path), + Err(PrivateFileError::Policy) + )); + } + fs::set_permissions(&path, Permissions::from_mode(0o600)).unwrap(); + let link = directory.path().join("backup-link"); + fs::hard_link(&path, &link).unwrap(); + assert!(matches!( + require_private_regular(&published), + Err(PrivateFileError::Policy) + )); + assert!(matches!( + open_private_regular(&path), + Err(PrivateFileError::Policy) + )); + assert_eq!(fs::read(&path).unwrap(), b"original"); + fs::remove_file(&link).unwrap(); + let mut original = open_private_regular(&path).unwrap(); + let replacement = directory.path().join("replacement"); + fs::write(&replacement, b"replacement").unwrap(); + fs::rename(&replacement, &path).unwrap(); + let mut bytes = Vec::new(); + original.read_to_end(&mut bytes).unwrap(); + assert_eq!(bytes, b"original"); + assert_eq!(fs::read(path).unwrap(), b"replacement"); +} + +#[cfg(unix)] +#[test] +fn private_open_refuses_final_links_directories_and_fifo_without_reading() { + let directory = tempfile::tempdir().unwrap(); + let target = directory.path().join("identity"); + drop( + AtomicPublication::stage_private(&target, b"identity") + .unwrap() + .publish_new() + .unwrap(), + ); + let link = directory.path().join("link"); + std::os::unix::fs::symlink(&target, &link).unwrap(); + assert!(matches!( + open_private_regular(&link), + Err(PrivateFileError::Open(BoundedReadError::NotRegular { .. })) + )); + assert!(matches!( + open_private_regular(directory.path()), + Err(PrivateFileError::Open(BoundedReadError::NotRegular { .. })) + )); + let fifo = directory.path().join("pipe"); + nix::unistd::mkfifo(&fifo, nix::sys::stat::Mode::from_bits_truncate(0o600)).unwrap(); + assert!(matches!( + open_private_regular(&fifo), + Err(PrivateFileError::Open(BoundedReadError::NotRegular { .. })) + )); +} + +#[cfg(not(unix))] +#[test] +fn private_open_refuses_unsupported_platform_before_opening() { + assert!(matches!( + open_private_regular(Path::new("not-opened")), + Err(PrivateFileError::Unsupported) + )); +} + #[test] fn parent_creation_failure_leaves_existing_file_intact() { let directory = tempfile::tempdir().unwrap(); @@ -124,3 +294,90 @@ fn default_snapshots_and_streams_are_private_but_explicit_modes_are_retained() { 0o644 ); } + +#[test] +fn bounded_read_returns_a_regular_file_up_to_its_cap() { + let directory = tempfile::tempdir().unwrap(); + let path = directory.path().join("package.toml"); + fs::write(&path, b"exactly 7").unwrap(); + assert_eq!(read_regular_bounded(&path, 9).unwrap(), b"exactly 7"); + assert_eq!(read_regular_bounded(&path, 4096).unwrap(), b"exactly 7"); + assert!(matches!( + read_regular_bounded(&path, 8).unwrap_err(), + BoundedReadError::TooLarge { cap: 8, .. } + )); + let empty = directory.path().join("empty"); + fs::write(&empty, b"").unwrap(); + assert!(read_regular_bounded(&empty, 0).unwrap().is_empty()); +} + +#[test] +fn bounded_read_reports_a_missing_file_with_its_kind() { + let directory = tempfile::tempdir().unwrap(); + let error = read_regular_bounded(&directory.path().join("absent"), 16).unwrap_err(); + assert_eq!(error.io_kind(), Some(io::ErrorKind::NotFound)); +} + +#[test] +fn bounded_read_refuses_directories() { + let directory = tempfile::tempdir().unwrap(); + assert!(matches!( + read_regular_bounded(directory.path(), 16).unwrap_err(), + BoundedReadError::NotRegular { .. } | BoundedReadError::Io { .. } + )); +} + +#[cfg(unix)] +#[test] +fn bounded_read_refuses_a_final_symlink_to_a_file_or_a_dangling_one() { + let directory = tempfile::tempdir().unwrap(); + let target = directory.path().join("target"); + fs::write(&target, b"secret").unwrap(); + let link = directory.path().join("link"); + std::os::unix::fs::symlink(&target, &link).unwrap(); + assert!(matches!( + read_regular_bounded(&link, 64).unwrap_err(), + BoundedReadError::NotRegular { .. } + )); + fs::remove_file(&target).unwrap(); + assert!(matches!( + read_regular_bounded(&link, 64).unwrap_err(), + BoundedReadError::NotRegular { .. } + )); +} + +#[cfg(unix)] +#[test] +fn bounded_read_follows_links_in_parent_directories_only() { + let directory = tempfile::tempdir().unwrap(); + let real = directory.path().join("real"); + fs::create_dir(&real).unwrap(); + fs::write(real.join("file"), b"content").unwrap(); + let alias = directory.path().join("alias"); + std::os::unix::fs::symlink(&real, &alias).unwrap(); + assert_eq!( + read_regular_bounded(&alias.join("file"), 64).unwrap(), + b"content" + ); +} + +#[cfg(unix)] +#[test] +fn bounded_read_refuses_a_fifo_without_blocking() { + let directory = tempfile::tempdir().unwrap(); + let fifo = directory.path().join("pipe"); + nix::unistd::mkfifo(&fifo, nix::sys::stat::Mode::from_bits_truncate(0o600)).unwrap(); + assert!(matches!( + read_regular_bounded(&fifo, 64).unwrap_err(), + BoundedReadError::NotRegular { .. } + )); +} + +#[cfg(unix)] +#[test] +fn bounded_read_refuses_devices_whose_size_is_unbounded() { + assert!(matches!( + read_regular_bounded(Path::new("/dev/zero"), 64).unwrap_err(), + BoundedReadError::NotRegular { .. } + )); +} diff --git a/crates/core/src/lib.rs b/crates/core/src/lib.rs index 9f8dcac5..611931c6 100644 --- a/crates/core/src/lib.rs +++ b/crates/core/src/lib.rs @@ -1,13 +1,23 @@ //! Shared error ownership, filesystem primitives and standard application locations. //! -//! Owns: paths, exclusive private creation, atomic publication, TOML persistence, -//! ordered write queues, shutdown deadlines and immutable concrete error causes. +//! Owns: paths, exclusive private creation, replacing or no-clobber atomic publication, +//! strict opened private-file policy, bounded no-follow reads of +//! regular files, bounded in-memory byte sinks, TOML persistence with cooperative +//! document locks and observed-revision conflict detection, +//! ordered write queues, shutdown deadlines, opaque allocation lifetime carriers, +//! immutable concrete error causes and the wall-clock +//! contract (`clock::{WallClock, WallTime}`; no I/O, no time-zone data). //! Does not own: transport protocols, encryption, storage schemas, views or tasks. -//! Ports used/provided: none. Settings section: none. Globals: none. +//! Ports used: none. Ports provided: `clock::WallClock`, implemented by `nocterm-local`; its +//! test fake is behind `test-support`. Settings section: none. Globals: none. //! Invariants: staging is exclusive; failed precommit writes retain old bytes; -//! private publication replaces final symlinks, while TOML saves follow them. +//! private publication replaces final symlinks, while TOML saves follow them; a bounded read +//! opens before it checks, refuses links, pipes and devices, and never exceeds its cap. //! Parent directories must be trusted; traversal capabilities belong to adapters. +pub mod allocation; +pub mod bounded_io; +pub mod clock; pub mod error; pub mod fs; pub mod paths; diff --git a/crates/core/src/paths.rs b/crates/core/src/paths.rs index da1edb90..80f82066 100644 --- a/crates/core/src/paths.rs +++ b/crates/core/src/paths.rs @@ -131,6 +131,11 @@ impl Paths { self.config_dir.join("snippets.toml") } + /// Saved workspace folder definitions. + pub fn projects_file(&self) -> PathBuf { + self.state_dir.join("workspaces.json") + } + /// Host keys the user accepted from inside nocterm. pub fn known_hosts_file(&self) -> PathBuf { self.config_dir.join("known_hosts") diff --git a/crates/core/src/persist.rs b/crates/core/src/persist.rs index 70e05f56..8f2485f3 100644 --- a/crates/core/src/persist.rs +++ b/crates/core/src/persist.rs @@ -13,7 +13,9 @@ use serde::{Serialize, de::DeserializeOwned}; use toml_edit::{DocumentMut, Item, Table}; pub mod queue; +mod revisioned; mod shutdown; +pub use revisioned::RevisionedFile; pub use shutdown::{ShutdownDeadline, WriteGate}; @@ -30,6 +32,10 @@ pub enum PersistError { path: PathBuf, source: Box, }, + /// An observed document changed before this owner published its next edit. + Conflict { + path: PathBuf, + }, Serialize { path: PathBuf, source: Box, @@ -39,6 +45,11 @@ pub enum PersistError { impl fmt::Display for PersistError { fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result { match self { + Self::Conflict { path } => write!( + formatter, + "{} changed outside this owner; reload before saving", + path.display() + ), Self::Io { path, source } => write!(formatter, "{}: {source}", path.display()), Self::Parse { path, source } => write!(formatter, "{}: {source}", path.display()), Self::Serialize { path, source } => write!(formatter, "{}: {source}", path.display()), @@ -48,6 +59,7 @@ impl fmt::Display for PersistError { impl std::error::Error for PersistError { fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { match self { + Self::Conflict { .. } => None, Self::Io { source, .. } => Some(source), Self::Parse { source, .. } => Some(source.as_ref()), Self::Serialize { source, .. } => Some(source.as_ref()), @@ -101,25 +113,58 @@ pub fn save_text(path: &Path, contents: &str) -> Result<(), PersistError> { /// is first copied to `.bak`, so a hand-edited file is never lost to a /// syntax error. pub fn save_preserving(path: &Path, value: &T) -> Result<(), PersistError> { - let fresh = serialize(path, value)? - .parse::() - .expect("serialised TOML is valid TOML"); - - let mut document = match read(path)? { - None => DocumentMut::new(), - Some(text) => match text.parse::() { - Ok(document) => document, + save_preserving_owned(path, value, |_| true) +} + +/// Replaces owned top-level tables while preserving the current unowned tables. +/// The formatting document is acquired and parsed once for the whole merge. +pub fn save_preserving_owned( + path: &Path, + value: &T, + owns: impl Fn(&str) -> bool, +) -> Result<(), PersistError> { + let fresh = fresh_document(path, value)?; + let original = read(path)?; + let existing = original.is_some(); + let mut document = formatting_document(path, original.as_deref())?; + merge_document(fresh, owns, existing, &mut document); + write_atomic(path, &document.to_string()).map_err(|source| PersistError::io(path, source)) +} + +fn formatting_document(path: &Path, original: Option<&str>) -> Result { + match original { + None => Ok(DocumentMut::new()), + Some(text) => match text.parse() { + Ok(document) => Ok(document), Err(_) => { let backup = backup_path(path); crate::fs::write_private_atomic(&backup, text.as_bytes()) .map_err(|error| PersistError::io(&backup, error.into_io_error()))?; - DocumentMut::new() + Ok(DocumentMut::new()) } }, - }; + } +} + +fn fresh_document(path: &Path, value: &T) -> Result { + Ok(serialize(path, value)? + .parse() + .expect("serialised TOML is valid TOML")) +} +fn merge_document( + mut fresh: DocumentMut, + owns: impl Fn(&str) -> bool, + existing: bool, + document: &mut DocumentMut, +) { + if existing { + fresh.as_table_mut().retain(|key, _| owns(key)); + for (key, item) in document.iter().filter(|(key, _)| !owns(key)) { + fresh.insert(key, item.clone()); + } + } sync_table(document.as_table_mut(), fresh.as_table()); - write_atomic(path, &document.to_string()).map_err(|source| PersistError::io(path, source)) } fn read(path: &Path) -> Result, PersistError> { diff --git a/crates/core/src/persist/queue.rs b/crates/core/src/persist/queue.rs index 8aaa9b83..a89af75f 100644 --- a/crates/core/src/persist/queue.rs +++ b/crates/core/src/persist/queue.rs @@ -28,7 +28,7 @@ pub struct WriteQueue { limit: usize, draining: bool, closing: bool, - writes: Arc, + writes: InFlight, } /// Marks one disk write as in flight until dropped. @@ -36,7 +36,7 @@ pub struct WriteQueue { pub struct Writing(Arc); /// Counts the writes in flight, independently of the queue. -#[derive(Clone)] +#[derive(Clone, Default)] pub struct InFlight(Arc); impl WriteQueue { @@ -47,7 +47,7 @@ impl WriteQueue { limit, draining: false, closing: false, - writes: Arc::default(), + writes: InFlight::default(), } } @@ -99,12 +99,11 @@ impl WriteQueue { /// Marks a disk write as in flight until the guard drops. Writes made /// outside the queue, such as of related files, may use it too. pub fn begin_write(&self) -> Writing { - self.writes.fetch_add(1, Ordering::AcqRel); - Writing(self.writes.clone()) + self.writes.begin_write() } pub fn in_flight(&self) -> InFlight { - InFlight(self.writes.clone()) + self.writes.clone() } } @@ -115,6 +114,13 @@ impl Drop for Writing { } impl InFlight { + /// Tracks native work independently from a pending queue or its caller. + /// The returned guard belongs to the actual task through physical completion. + pub fn begin_write(&self) -> Writing { + self.0.fetch_add(1, Ordering::AcqRel); + Writing(self.0.clone()) + } + pub fn count(&self) -> usize { self.0.load(Ordering::Acquire) } diff --git a/crates/core/src/persist/revisioned.rs b/crates/core/src/persist/revisioned.rs new file mode 100644 index 00000000..5b446938 --- /dev/null +++ b/crates/core/src/persist/revisioned.rs @@ -0,0 +1,201 @@ +//! Cooperative document publication and optimistic conflict detection. +//! Lock files are stable siblings of the resolved configuration target and are never unlinked. +use super::{PersistError, read}; +use serde::{Serialize, de::DeserializeOwned}; +use sha2::{Digest as _, Sha256}; +use std::{ + collections::BTreeMap, + fs::{self, File}, + io, + path::{Path, PathBuf}, + sync::{Arc, Mutex, PoisonError}, +}; +use toml_edit::DocumentMut; + +/// One document owner and the revision it last read or successfully published. +/// Clones share that revision; independently constructed owners detect stale edits. +/// +/// Native callers dispatch load/save on their disk worker. A non-cooperating editor can +/// still race the final rename; the lock coordinates Nocterm writers, not arbitrary programs. +#[derive(Clone, Debug)] +pub struct RevisionedFile { + path: PathBuf, + observed: Arc>>, +} + +#[derive(Debug, PartialEq, Eq)] +struct Revision { + target: PathBuf, + content: Content, +} +#[derive(Debug, PartialEq, Eq)] +enum Content { + Missing, + Tables(BTreeMap), + Invalid([u8; 32]), +} + +impl RevisionedFile { + pub fn new(path: impl Into) -> Self { + Self { + path: path.into(), + observed: Arc::default(), + } + } + pub fn path(&self) -> &Path { + &self.path + } + + /// Reading observes the same bytes used to decode the returned document. + pub fn load(&self) -> Result, PersistError> { + let mut observed = self.observed.lock().unwrap_or_else(PoisonError::into_inner); + let target = resolve(&self.path).map_err(|error| PersistError::io(&self.path, error))?; + let text = read(&target)?; + let parsed = text.as_deref().map(str::parse::); + *observed = Some(revision(target, text.as_deref(), parsed.as_ref())); + text.map(|text| { + toml::from_str(&text).map_err(|source| PersistError::Parse { + path: self.path.clone(), + source: Box::new(source), + }) + }) + .transpose() + } + + pub fn save_preserving(&self, value: &T) -> Result<(), PersistError> { + self.save_preserving_owned(value, |_| true) + } + + /// Unowned top-level tables may change independently and keep their current bytes. + /// Conflicts in owned tables leave the document and observed revision untouched. + pub fn save_preserving_owned( + &self, + value: &T, + owns: impl Fn(&str) -> bool, + ) -> Result<(), PersistError> { + let fresh = super::fresh_document(&self.path, value)?; + let mut observed = self.observed.lock().unwrap_or_else(PoisonError::into_inner); + let target = resolve(&self.path).map_err(|error| PersistError::io(&self.path, error))?; + let parent = target.parent().unwrap_or(Path::new(".")); + fs::create_dir_all(parent).map_err(|error| PersistError::io(parent, error))?; + let _lock = publication_lock(&target).map_err(|error| PersistError::io(&target, error))?; + let text = read(&target)?; + let parsed = text.as_deref().map(str::parse::); + let current = revision(target.clone(), text.as_deref(), parsed.as_ref()); + if observed + .as_ref() + .is_some_and(|prior| conflicts(prior, ¤t, &owns)) + { + return Err(PersistError::Conflict { + path: self.path.clone(), + }); + } + let mut document = match parsed { + Some(Ok(document)) => document, + // Preserve the existing malformed-file backup contract after conflict admission. + _ => super::formatting_document(&target, text.as_deref())?, + }; + super::merge_document(fresh, &owns, text.is_some(), &mut document); + let published = document.to_string(); + super::write_atomic(&target, &published) + .map_err(|error| PersistError::io(&target, error))?; + *observed = Some(Revision { + target, + content: tables(&document), + }); + Ok(()) + } +} + +fn resolve(path: &Path) -> io::Result { + match fs::canonicalize(path) { + Ok(path) => Ok(path), + Err(error) if error.kind() == io::ErrorKind::NotFound => { + let absolute = std::path::absolute(path)?; + let parent = absolute.parent().unwrap_or(Path::new(".")); + Ok(fs::canonicalize(parent).map_or(absolute.clone(), |parent| { + parent.join(absolute.file_name().unwrap_or_default()) + })) + } + Err(error) => Err(error), + } +} + +fn publication_lock(target: &Path) -> io::Result { + let mut name = target.file_name().unwrap_or_default().to_os_string(); + name.push(".lock"); + let path = target.with_file_name(name); + let file = match crate::fs::create_private(&path) { + Ok(file) => file, + Err(error) if error.kind() == io::ErrorKind::AlreadyExists => open_existing_lock(&path)?, + Err(error) => return Err(error.into_io_error()), + }; + file.try_lock().map_err(io::Error::other)?; + Ok(file) +} + +#[cfg(unix)] +fn open_existing_lock(path: &Path) -> io::Result { + crate::fs::open_private_regular(path).map_err(io::Error::other) +} +#[cfg(not(unix))] +fn open_existing_lock(path: &Path) -> io::Result { + let mut options = fs::OpenOptions::new(); + options.read(true).write(true); + #[cfg(windows)] + { + use std::os::windows::fs::OpenOptionsExt as _; + // Open the reparse point itself; it must not select another file's lock. + options.custom_flags(0x0020_0000); + } + if fs::symlink_metadata(path)?.file_type().is_symlink() { + return Err(io::Error::from(io::ErrorKind::InvalidInput)); + } + let file = options.open(path)?; + if !file.metadata()?.is_file() { + return Err(io::Error::from(io::ErrorKind::InvalidInput)); + } + Ok(file) +} + +fn revision( + target: PathBuf, + text: Option<&str>, + parsed: Option<&Result>, +) -> Revision { + let content = match (text, parsed) { + (_, Some(Ok(document))) => tables(document), + (Some(text), _) => Content::Invalid(digest(text)), + _ => Content::Missing, + }; + Revision { target, content } +} +fn digest(text: &str) -> [u8; 32] { + Sha256::digest(text.as_bytes()).into() +} +fn tables(document: &DocumentMut) -> Content { + Content::Tables( + document + .iter() + .map(|(key, item)| (key.to_owned(), digest(&item.to_string()))) + .collect(), + ) +} +fn conflicts(prior: &Revision, current: &Revision, owns: &impl Fn(&str) -> bool) -> bool { + if prior.target != current.target { + return true; + } + match (&prior.content, ¤t.content) { + (Content::Tables(prior), Content::Tables(current)) => prior + .keys() + .chain(current.keys()) + .any(|key| owns(key) && prior.get(key) != current.get(key)), + // The empty document and a missing file are equivalent for owned keys. + (Content::Missing, Content::Tables(current)) => current.keys().any(|key| owns(key)), + (Content::Tables(prior), Content::Missing) => prior.keys().any(|key| owns(key)), + (prior, current) => prior != current, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/core/src/persist/revisioned/tests.rs b/crates/core/src/persist/revisioned/tests.rs new file mode 100644 index 00000000..6caa64e4 --- /dev/null +++ b/crates/core/src/persist/revisioned/tests.rs @@ -0,0 +1,110 @@ +use super::*; + +fn values(text: &str) -> toml::Table { + toml::from_str(text).unwrap() +} + +#[test] +fn independent_owners_cannot_replace_a_newer_owned_document() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + fs::write(&path, "[terminal]\nsize = 14\n").unwrap(); + let first = RevisionedFile::new(&path); + let second = RevisionedFile::new(&path); + let _: toml::Table = first.load().unwrap().unwrap(); + let _: toml::Table = second.load().unwrap().unwrap(); + first + .save_preserving(&values("[terminal]\nsize = 15\n")) + .unwrap(); + assert!(matches!( + second.save_preserving(&values("[terminal]\nsize = 16\n")), + Err(PersistError::Conflict { .. }) + )); + assert!(fs::read_to_string(&path).unwrap().contains("size = 15")); + let _: toml::Table = second.load().unwrap().unwrap(); + second + .save_preserving(&values("[terminal]\nsize = 16\n")) + .unwrap(); +} + +#[test] +fn unowned_external_tables_survive_while_owned_edits_conflict() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + fs::write(&path, "[future]\nflag = true\n").unwrap(); + let file = RevisionedFile::new(&path); + let _: toml::Table = file.load().unwrap().unwrap(); + fs::write(&path, "[future]\n# retain this comment\nflag = false\n").unwrap(); + file.save_preserving_owned(&values("[terminal]\nsize = 15\n"), |key| key == "terminal") + .unwrap(); + assert!( + fs::read_to_string(&path) + .unwrap() + .contains("# retain this comment\nflag = false") + ); + fs::write(&path, "[terminal]\nsize = 18\n").unwrap(); + assert!(matches!( + file.save_preserving_owned(&values("[terminal]\nsize = 16\n"), |key| key == "terminal"), + Err(PersistError::Conflict { .. }) + )); +} + +#[test] +fn clones_share_a_publication_revision_and_missing_file_creation_is_observed() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("new/settings.toml"); + let file = RevisionedFile::new(&path); + assert!(file.load::().unwrap().is_none()); + let clone = file.clone(); + file.save_preserving(&values("a = 1")).unwrap(); + clone.save_preserving(&values("a = 2")).unwrap(); + let other = RevisionedFile::new(&path); + other.load::().unwrap(); + clone.save_preserving(&values("a = 3")).unwrap(); + assert!(matches!( + other.save_preserving(&values("a = 4")), + Err(PersistError::Conflict { .. }) + )); +} + +#[cfg(unix)] +#[test] +fn symlink_aliases_use_the_same_lock_and_lock_symlinks_are_refused() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + let alias = dir.path().join("alias.toml"); + fs::write(&path, "a = 1").unwrap(); + std::os::unix::fs::symlink(&path, &alias).unwrap(); + let file = RevisionedFile::new(&alias); + file.load::().unwrap(); + let lock = publication_lock(&path).unwrap(); + assert!(file.save_preserving(&values("a = 2")).is_err()); + drop(lock); + file.save_preserving(&values("a = 2")).unwrap(); + assert!(fs::symlink_metadata(&alias).unwrap().is_symlink()); + fs::remove_file(path.with_file_name("settings.toml.lock")).unwrap(); + let victim = dir.path().join("victim"); + fs::write(&victim, "untouched").unwrap(); + std::os::unix::fs::symlink(&victim, path.with_file_name("settings.toml.lock")).unwrap(); + assert!(file.save_preserving(&values("a = 3")).is_err()); + assert_eq!(fs::read_to_string(victim).unwrap(), "untouched"); +} + +#[test] +fn serialization_refusal_does_not_touch_an_unreadable_document_or_create_a_lock() { + let dir = tempfile::tempdir().unwrap(); + let path = dir.path().join("settings.toml"); + fs::write(&path, "bad = =").unwrap(); + let file = RevisionedFile::new(&path); + assert!(matches!( + file.save_preserving(&42u32), + Err(PersistError::Serialize { .. }) + )); + assert_eq!(fs::read_to_string(&path).unwrap(), "bad = ="); + assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1); + assert!(matches!( + super::super::save_preserving(&path, &42u32), + Err(PersistError::Serialize { .. }) + )); + assert_eq!(fs::read_dir(dir.path()).unwrap().count(), 1); +} diff --git a/crates/core/src/persist/shutdown.rs b/crates/core/src/persist/shutdown.rs index 26b7ae2a..dc88b157 100644 --- a/crates/core/src/persist/shutdown.rs +++ b/crates/core/src/persist/shutdown.rs @@ -14,7 +14,7 @@ pub struct WriteGate(Arc); #[derive(Default)] struct Gate { closing: AtomicBool, - disk: futures::lock::Mutex<()>, + disk: Arc>, } impl WriteGate { @@ -33,6 +33,18 @@ impl WriteGate { (!self.is_frozen()).then_some(guard) } + /// An off-thread publication retains this owned guard until its actual + /// native operation ends, even when the original waiter is cancelled. + pub async fn owned_normal(&self) -> Option> { + let guard = self.0.disk.clone().lock_owned().await; + (!self.is_frozen()).then_some(guard) + } + + /// Final publication uses the same owned gate after normal admission closes. + pub async fn owned_final_write(&self) -> futures::lock::OwnedMutexGuard<()> { + self.0.disk.clone().lock_owned().await + } + /// The final writer runs after active disk work and bypasses frozen admission. pub async fn final_write(&self) -> futures::lock::MutexGuard<'_, ()> { self.0.disk.lock().await @@ -59,6 +71,31 @@ mod tests { use super::*; use futures::{FutureExt as _, executor::block_on}; + #[test] + fn an_owned_publication_outlives_its_caller_and_blocks_final_write() { + let gate = WriteGate::default(); + let writer = block_on(gate.owned_normal()).unwrap(); + let observer = gate.clone(); + let tracked = crate::persist::InFlight::default(); + let writing = tracked.begin_write(); + let (release, wait) = std::sync::mpsc::channel(); + let work = std::thread::spawn(move || { + let _writer = writer; + let _writing = writing; + wait.recv().unwrap(); + }); + drop(gate); + observer.freeze(); + assert_eq!(tracked.count(), 1); + assert!(observer.owned_final_write().now_or_never().is_none()); + release.send(()).unwrap(); + work.join().unwrap(); + assert_eq!(tracked.count(), 0); + assert!(block_on(observer.owned_normal()).is_none()); + assert!(observer.owned_final_write().now_or_never().is_some()); + assert!(observer.final_write().now_or_never().is_some()); + } + #[test] fn freeze_is_nonblocking_and_final_waits_for_active_write() { let gate = WriteGate::default(); diff --git a/crates/design/src/tokens.rs b/crates/design/src/tokens.rs index 7e6632fc..f17048ff 100644 --- a/crates/design/src/tokens.rs +++ b/crates/design/src/tokens.rs @@ -254,7 +254,9 @@ impl DesignTokens { /// on top, key by key. pub fn with_overrides(overrides: &str) -> Result { let mut source: toml::Table = toml::from_str(BUILTIN).map_err(Box::new)?; - let overrides: toml::Table = toml::from_str(overrides).map_err(Box::new)?; + let mut overrides: toml::Table = toml::from_str(overrides).map_err(Box::new)?; + flatten_ui(&mut source); + flatten_ui(&mut overrides); merge(&mut source, overrides); let tokens: Self = source.try_into().map_err(Box::new)?; @@ -385,6 +387,12 @@ impl DesignTokens { 5.0, 60.0, )?; + within( + "layout.status_bar_center_max", + layout.status_bar_center_max, + 4.0, + 100.0, + )?; Ok(()) } } @@ -410,6 +418,36 @@ fn within(token: &'static str, value: f32, min: f32, max: f32) -> Result<(), Tok Ok(()) } +/// Spells every interface colour name of both palettes flat (`"primary.background"`), +/// so that a nested override replaces a quoted built-in colour and the other way round. +fn flatten_ui(tokens: &mut toml::Table) { + fn flatten(prefix: &str, table: toml::Table, flat: &mut toml::Table) { + for (name, value) in table { + let path = if prefix.is_empty() { + name + } else { + format!("{prefix}.{name}") + }; + match value { + toml::Value::Table(group) => flatten(&path, group, flat), + value => { + flat.insert(path, value); + } + } + } + } + for appearance in ["light", "dark"] { + let Some(toml::Value::Table(palette)) = tokens.get_mut(appearance) else { + continue; + }; + if let Some(toml::Value::Table(ui)) = palette.get_mut("ui") { + let mut flat = toml::Table::new(); + flatten("", std::mem::take(ui), &mut flat); + *ui = flat; + } + } +} + /// Layers `overlay` over `base`: tables merge key by key, anything else replaces. fn merge(base: &mut toml::Table, overlay: toml::Table) { for (key, value) in overlay { @@ -435,7 +473,20 @@ mod tests { let tokens = DesignTokens::builtin(); assert_eq!(tokens.name, "Nocterm Default"); - assert!(tokens.light.ui.is_empty()); + // The built-in palettes are Noct Day and Noct Night. + let background = |ui: &UiColors| { + ui.iter() + .find(|(name, _)| *name == "background") + .map(|(_, color)| color) + }; + assert_eq!( + background(&tokens.light.ui), + Some(Color::rgb(0xf3, 0xf1, 0xec)) + ); + assert_eq!( + background(&tokens.dark.ui), + Some(Color::rgb(0x1f, 0x1f, 0x21)) + ); assert_eq!(tokens.typography.ui_font, None); } @@ -488,17 +539,24 @@ mod tests { ) .unwrap(); - let names: Vec<&str> = tokens.dark.ui.iter().map(|(name, _)| name).collect(); + let color = |name: &str| { + tokens + .dark + .ui + .iter() + .find(|(key, _)| *key == name) + .map(|(_, color)| color) + }; + assert_eq!(color("background"), Some(Color::rgb(0x10, 0x10, 0x10))); + assert_eq!( + color("primary.background"), + Some(Color::rgb(0x33, 0x66, 0xff)) + ); assert_eq!( - names, - [ - "background", - "link.hover", - "primary.background", - "tab.active.background", - "tab.active.foreground" - ] + color("tab.active.foreground"), + Some(Color::rgb(0xff, 0xff, 0xff)) ); + assert_eq!(color("link.hover"), Some(Color::rgb(0x99, 0xaa, 0xff))); } #[test] diff --git a/crates/design/src/tokens/format_tests.rs b/crates/design/src/tokens/format_tests.rs index 8641aa59..42c16272 100644 --- a/crates/design/src/tokens/format_tests.rs +++ b/crates/design/src/tokens/format_tests.rs @@ -6,7 +6,7 @@ fn grouped_tokens_keep_flat_required_keys_and_optional_omission() { let tokens = DesignTokens::builtin(); let table: toml::Table = toml::to_string(&tokens).unwrap().parse().unwrap(); let layout = table["layout"].as_table().unwrap(); - assert_eq!(layout.len(), 18); + assert_eq!(layout.len(), 19); assert!(layout.contains_key("agent_panel_min_width")); assert!(!layout.contains_key("agent_panel")); let terminal = table["dark"]["terminal"].as_table().unwrap(); @@ -97,12 +97,13 @@ fn each_ansi_override_changes_only_its_existing_index() { #[test] fn layout_generic_sequence_decode_keeps_original_field_order() { use serde::de::value::{Error, SeqDeserializer}; - let values: Vec = (1..=18).map(|value| value as f32).collect(); + let values: Vec = (1..=19).map(|value| value as f32).collect(); let layout = Layout::deserialize(SeqDeserializer::<_, Error>::new(values.into_iter())).unwrap(); assert_eq!(layout.agent_panel.width, 1.0); assert_eq!(layout.sidebar.max_width, 6.0); assert_eq!(layout.tab_max_width, 7.0); assert_eq!(layout.local_terminal_height, 18.0); + assert_eq!(layout.status_bar_center_max, 19.0); let short = SeqDeserializer::<_, Error>::new([1.0_f32].into_iter()); assert!(Layout::deserialize(short).is_err()); } @@ -113,9 +114,9 @@ fn public_token_schema_keeps_flat_properties_and_required_names() { let layout = Layout::json_schema(&mut generator); assert_eq!( layout.as_value()["properties"].as_object().unwrap().len(), - 18 + 19 ); - assert_eq!(layout.as_value()["required"].as_array().unwrap().len(), 18); + assert_eq!(layout.as_value()["required"].as_array().unwrap().len(), 19); assert_eq!(layout.as_value()["additionalProperties"], false); let terminal = TerminalColors::json_schema(&mut generator); assert_eq!( @@ -171,12 +172,12 @@ fn generic_map_duplicate_fields_are_rejected_before_missing_fields() { #[test] fn truncated_generic_sequences_keep_native_struct_length_diagnostics() { use serde::de::value::{Error, SeqDeserializer}; - for length in 0..18 { + for length in 0..19 { let values = (0..length).map(|value| value as f32); let error = Layout::deserialize(SeqDeserializer::<_, Error>::new(values)).unwrap_err(); assert_eq!( error.to_string(), - format!("invalid length {length}, expected struct Layout with 18 elements") + format!("invalid length {length}, expected struct Layout with 19 elements") ); } let empty = SeqDeserializer::<_, Error>::new(std::iter::empty::()); @@ -205,14 +206,14 @@ fn scalar_struct_errors_keep_the_original_generic_expectation() { #[test] fn numeric_identifiers_keep_native_out_of_range_diagnostics() { use serde::de::value::{Error, MapDeserializer}; - for index in [18_u64, 19, u64::MAX] { + for index in [19_u64, 20, u64::MAX] { let error = Layout::deserialize(MapDeserializer::<_, Error>::new( [(index, 0_f32)].into_iter(), )) .unwrap_err(); assert_eq!( error.to_string(), - format!("invalid value: integer `{index}`, expected field index 0 <= i < 18") + format!("invalid value: integer `{index}`, expected field index 0 <= i < 19") ); } for index in [20_u64, 21, u64::MAX] { diff --git a/crates/design/src/tokens/layout.rs b/crates/design/src/tokens/layout.rs index beb46d3c..52911601 100644 --- a/crates/design/src/tokens/layout.rs +++ b/crates/design/src/tokens/layout.rs @@ -39,4 +39,6 @@ pub struct Layout { pub button_padding: f32, /// Initial height of the independent local terminal dock, in rem. pub local_terminal_height: f32, + /// Widest the centre of the status bar grows, in rem. + pub status_bar_center_max: f32, } diff --git a/crates/design/src/tokens/layout/codec.rs b/crates/design/src/tokens/layout/codec.rs index b9c5d699..cb025cb9 100644 --- a/crates/design/src/tokens/layout/codec.rs +++ b/crates/design/src/tokens/layout/codec.rs @@ -36,6 +36,7 @@ struct Fields { button_height: Option, button_padding: Option, local_terminal_height: Option, + status_bar_center_max: Option, } const NAMES: &[&str] = &[ "agent_panel_width", @@ -56,11 +57,12 @@ const NAMES: &[&str] = &[ "button_height", "button_padding", "local_terminal_height", + "status_bar_center_max", ]; impl Serialize for Layout { fn serialize(&self, serializer: S) -> Result { - let mut state = serializer.serialize_struct("Layout", 18)?; + let mut state = serializer.serialize_struct("Layout", 19)?; state.serialize_field("agent_panel_width", &self.agent_panel.width)?; state.serialize_field("agent_panel_min_width", &self.agent_panel.min_width)?; state.serialize_field("agent_panel_max_width", &self.agent_panel.max_width)?; @@ -82,6 +84,7 @@ impl Serialize for Layout { state.serialize_field("button_height", &self.button_height)?; state.serialize_field("button_padding", &self.button_padding)?; state.serialize_field("local_terminal_height", &self.local_terminal_height)?; + state.serialize_field("status_bar_center_max", &self.status_bar_center_max)?; state.end() } } @@ -214,6 +217,12 @@ impl Fields { } self.local_terminal_height = Some(map.next_value()?); } + "status_bar_center_max" => { + if self.status_bar_center_max.is_some() { + return Err(de::Error::duplicate_field("status_bar_center_max")); + } + self.status_bar_center_max = Some(map.next_value()?); + } _ => return Ok(false), } Ok(true) @@ -246,6 +255,8 @@ impl Fields { let button_padding = required::<_, E>(self.button_padding, "button_padding")?; let local_terminal_height = required::<_, E>(self.local_terminal_height, "local_terminal_height")?; + let status_bar_center_max = + required::<_, E>(self.status_bar_center_max, "status_bar_center_max")?; Ok(Layout { agent_panel: WidthBounds { width: agent_panel_width, @@ -269,6 +280,7 @@ impl Fields { button_height, button_padding, local_terminal_height, + status_bar_center_max, }) } } @@ -293,7 +305,7 @@ impl<'de> Visitor<'de> for TokenVisitor { } fn visit_seq>(self, mut sequence: A) -> Result { let mut fields = Fields::default(); - let expected = "struct Layout with 18 elements"; + let expected = "struct Layout with 19 elements"; fields.agent_panel.width = Some(next(&mut sequence, 0, &expected)?); fields.agent_panel.min_width = Some(next(&mut sequence, 1, &expected)?); fields.agent_panel.max_width = Some(next(&mut sequence, 2, &expected)?); @@ -312,6 +324,7 @@ impl<'de> Visitor<'de> for TokenVisitor { fields.button_height = Some(next(&mut sequence, 15, &expected)?); fields.button_padding = Some(next(&mut sequence, 16, &expected)?); fields.local_terminal_height = Some(next(&mut sequence, 17, &expected)?); + fields.status_bar_center_max = Some(next(&mut sequence, 18, &expected)?); fields.finish() } } diff --git a/crates/design/src/tokens/layout/schema.rs b/crates/design/src/tokens/layout/schema.rs index aadc209e..34a81873 100644 --- a/crates/design/src/tokens/layout/schema.rs +++ b/crates/design/src/tokens/layout/schema.rs @@ -32,8 +32,9 @@ impl JsonSchema for Layout { "button_height": described(generator.subschema_for::(), "Height of a default button; other button sizes keep their relative hierarchy."), "button_padding": described(generator.subschema_for::(), "Horizontal padding of a default button."), "local_terminal_height": described(generator.subschema_for::(), "Initial height of the independent local terminal dock, in rem."), + "status_bar_center_max": described(generator.subschema_for::(), "Widest the centre of the status bar grows, in rem."), }, - "required": ["agent_panel_width", "agent_panel_min_width", "agent_panel_max_width", "sidebar_width", "sidebar_min_width", "sidebar_max_width", "tab_max_width", "terminal_padding", "picker_width", "dialog_width", "settings_width", "connection_editor_width", "connection_editor_height", "connection_nav_width", "connection_description_height", "button_height", "button_padding", "local_terminal_height"], + "required": ["agent_panel_width", "agent_panel_min_width", "agent_panel_max_width", "sidebar_width", "sidebar_min_width", "sidebar_max_width", "tab_max_width", "terminal_padding", "picker_width", "dialog_width", "settings_width", "connection_editor_width", "connection_editor_height", "connection_nav_width", "connection_description_height", "button_height", "button_padding", "local_terminal_height", "status_bar_center_max"], "description": "Sizes of the interface's fixed regions, in rem.", }) } diff --git a/crates/design/tokens/default.toml b/crates/design/tokens/default.toml index 30862a32..606c0e02 100644 --- a/crates/design/tokens/default.toml +++ b/crates/design/tokens/default.toml @@ -53,13 +53,14 @@ agent_panel_min_width = 18.0 # minimum AI panel width agent_panel_max_width = 64.0 # maximum AI panel width sidebar_width = 17.0 # initial width of the left sidebar sidebar_min_width = 12.0 # the sidebar cannot be dragged narrower … -sidebar_max_width = 32.0 # … or wider than this +sidebar_max_width = 64.0 # … or wider than this tab_max_width = 14.0 # a session tab ellipsizes past this width terminal_padding = 0.5 # space between the terminal grid and its pane picker_width = 22.0 # the "new tab" picker dialog_width = 30.0 # the connection editor local_terminal_height = 15.0 # initial height of the bottom local shell -settings_width = 45.0 # the settings form and its footer +status_bar_center_max = 40.0 # the centre of the status bar grows no wider than this +settings_width = 52.0 # the settings form and its footer connection_editor_width = 45.0 # connection editor, including section navigation connection_editor_height = 32.0 # maximum connection editor form height connection_nav_width = 9.0 # connection editor section navigation @@ -68,71 +69,180 @@ button_height = 1.75 # default button height button_padding = 0.5 # default horizontal button padding -# ── Light palette ───────────────────────────────────────────────────────────── +# ── Light palette: Noct Day ─────────────────────────────────────────────────── [light] # The backdrop behind floating cards (`appearance.layout = "floating"`). -# canvas = "#ececec" # unset: a shade darker than the background +# canvas = "#ece9e2" # unset: a shade darker than the background # Interface colours, in the component theme's own vocabulary # (`background`, `primary.background`, `sidebar.background`, `title_bar.border`, …). -# Empty: the standard light theme. +# Unset names keep the standard light theme. [light.ui] +"background" = "#f3f1ec" +"foreground" = "#161616" +"border" = "#d5d1c7" +"window.border" = "#d5d1c7" +"sidebar.border" = "#d5d1c7" +"title_bar.border" = "#d5d1c7" +"status_bar.border" = "#d5d1c7" +"input.border" = "#e1ddd4" +"ring" = "#5642e0" +"list.active.border" = "#5642e0" +"muted.background" = "#e9e6df" +"muted.foreground" = "#57544e" +"tab.foreground" = "#57544e" +"secondary.background" = "#e9e6df" +"tab_bar.segmented.background" = "#e9e6df" +"secondary.hover.background" = "#e9e6df" +"secondary.active.background" = "#ddd9d0" +"secondary.foreground" = "#161616" +"popover.foreground" = "#161616" +"sidebar.foreground" = "#161616" +"accent.foreground" = "#161616" +"tab.active.foreground" = "#161616" +"accent.background" = "#e9e6df" +"list.hover.background" = "#e9e6df" +"sidebar.accent.background" = "#e9e6df" +"list.active.background" = "#e9e6df" +"sidebar.primary.background" = "#e9e6df" +"popover.background" = "#e9e6df" +"sidebar.background" = "#f3f1ec" +"list.head.background" = "#f3f1ec" +"title_bar.background" = "#f3f1ec" +"status_bar.background" = "#f3f1ec" +"tab_bar.background" = "#f3f1ec" +"tab.background" = "#f3f1ec" +"tab.active.background" = "#e9e6df" +"scrollbar.background" = "#00000000" +"scrollbar.thumb.background" = "#57544e40" +"scrollbar.thumb.hover.background" = "#57544e66" +"link" = "#5642e0" +"link.hover" = "#5642e0" +"drop_target.background" = "#5642e033" +"primary.background" = "#5642e0" +"primary.foreground" = "#f3f1ec" +"caret" = "#5642e0" +"selection.background" = "#5642e03d" +"danger.background" = "#cf2539" +"success.background" = "#2f7d4f" +"warning.background" = "#8a6a0c" +"info.background" = "#2f5fc4" +"base.red" = "#cf2539" +"base.green" = "#2f7d4f" +"base.yellow" = "#8a6a0c" +"base.blue" = "#2f5fc4" +"base.magenta" = "#5642e0" +"base.cyan" = "#1f6e64" [light.terminal] -# background = "#ffffff" # unset: the interface background -# foreground = "#1f2328" # unset: the interface foreground -# cursor = "#1f2328" # unset: the terminal foreground -# selection = "#0969da40" # unset: the interface selection colour - -black = "#000000" -red = "#cd3131" -green = "#107c10" -yellow = "#949800" -blue = "#0451a5" -magenta = "#bc05bc" -cyan = "#0598bc" -white = "#555555" - -bright_black = "#666666" -bright_red = "#cd3131" -bright_green = "#14ce14" -bright_yellow = "#b5ba00" -bright_blue = "#0451a5" -bright_magenta = "#bc05bc" -bright_cyan = "#0598bc" -bright_white = "#a5a5a5" - - -# ── Dark palette ────────────────────────────────────────────────────────────── +background = "#f3f1ec" +foreground = "#161616" +cursor = "#5642e0" +selection = "#5642e03d" + +black = "#161616" +red = "#cf2539" +green = "#2f7d4f" +yellow = "#8a6a0c" +blue = "#2f5fc4" +magenta = "#5642e0" +cyan = "#1f6e64" +white = "#a9a59c" + +bright_black = "#57544e" +bright_red = "#e04556" +bright_green = "#3f9a64" +bright_yellow = "#a88318" +bright_blue = "#4c78d6" +bright_magenta = "#7464ea" +bright_cyan = "#2c8a7d" +bright_white = "#7c7972" + + +# ── Dark palette: Noct Night ────────────────────────────────────────────────── [dark] -# canvas = "#0e0e0e" # unset: a shade darker than the background +# canvas = "#19191b" # unset: a shade darker than the background -# Standard dark theme with a lighter active tab. +# Unset names keep the standard dark theme. [dark.ui] -"tab.active.background" = "#272727" +"background" = "#1f1f21" +"foreground" = "#efeeea" +"border" = "#39393d" +"window.border" = "#39393d" +"sidebar.border" = "#39393d" +"title_bar.border" = "#39393d" +"status_bar.border" = "#39393d" +"input.border" = "#2f2f32" +"ring" = "#b4a7ff" +"list.active.border" = "#b4a7ff" +"muted.background" = "#2a2a2d" +"muted.foreground" = "#a8a7a2" +"tab.foreground" = "#a8a7a2" +"secondary.background" = "#2a2a2d" +"tab_bar.segmented.background" = "#2a2a2d" +"secondary.hover.background" = "#2a2a2d" +"secondary.active.background" = "#353538" +"secondary.foreground" = "#efeeea" +"popover.foreground" = "#efeeea" +"sidebar.foreground" = "#efeeea" +"accent.foreground" = "#efeeea" +"tab.active.foreground" = "#efeeea" +"accent.background" = "#2a2a2d" +"list.hover.background" = "#2a2a2d" +"sidebar.accent.background" = "#2a2a2d" +"list.active.background" = "#2a2a2d" +"sidebar.primary.background" = "#2a2a2d" +"popover.background" = "#2a2a2d" +"sidebar.background" = "#1f1f21" +"list.head.background" = "#1f1f21" +"title_bar.background" = "#1f1f21" +"status_bar.background" = "#1f1f21" +"tab_bar.background" = "#1f1f21" +"tab.background" = "#1f1f21" +"tab.active.background" = "#2a2a2d" +"scrollbar.background" = "#00000000" +"scrollbar.thumb.background" = "#a8a7a240" +"scrollbar.thumb.hover.background" = "#a8a7a266" +"link" = "#b4a7ff" +"link.hover" = "#b4a7ff" +"drop_target.background" = "#b4a7ff33" +"primary.background" = "#b4a7ff" +"primary.foreground" = "#1f1f21" +"caret" = "#b4a7ff" +"selection.background" = "#b4a7ff52" +"danger.background" = "#ff5a5a" +"success.background" = "#5fc98a" +"warning.background" = "#f2c94c" +"info.background" = "#6fa2ff" +"base.red" = "#ff5a5a" +"base.green" = "#5fc98a" +"base.yellow" = "#f2c94c" +"base.blue" = "#6fa2ff" +"base.magenta" = "#b4a7ff" +"base.cyan" = "#8fd0c4" [dark.terminal] -background = "#181818" -# foreground = "#e5e5e5" -# cursor = "#e5e5e5" -# selection = "#3b8eea40" - -black = "#000000" -red = "#cd3131" -green = "#0dbc79" -yellow = "#e5e510" -blue = "#2472c8" -magenta = "#bc3fbc" -cyan = "#11a8cd" -white = "#e5e5e5" - -bright_black = "#666666" -bright_red = "#f14c4c" -bright_green = "#23d18b" -bright_yellow = "#f5f543" -bright_blue = "#3b8eea" -bright_magenta = "#d670d6" -bright_cyan = "#29b8db" -bright_white = "#ffffff" +background = "#1f1f21" +foreground = "#efeeea" +cursor = "#b4a7ff" +selection = "#b4a7ff52" + +black = "#2a2a2d" +red = "#ff5a5a" +green = "#5fc98a" +yellow = "#f2c94c" +blue = "#6fa2ff" +magenta = "#b4a7ff" +cyan = "#8fd0c4" +white = "#a8a7a2" + +bright_black = "#5f5e5a" +bright_red = "#ff8a85" +bright_green = "#8edcab" +bright_yellow = "#f7dc85" +bright_blue = "#9fc0ff" +bright_magenta = "#d4ccff" +bright_cyan = "#b6e3da" +bright_white = "#efeeea" diff --git a/crates/directory-io/Cargo.toml b/crates/directory-io/Cargo.toml new file mode 100644 index 00000000..a0b2c790 --- /dev/null +++ b/crates/directory-io/Cargo.toml @@ -0,0 +1,17 @@ +[package] +name = "nocterm-directory-io" +version = "0.0.0" +description = "Read-only directory readers over injected local and session filesystems." +edition.workspace = true +license-file.workspace = true +publish = false +[package.metadata.nocterm] +layer = "adapter" +[dependencies] +nocterm-explorer.workspace = true +nocterm-session.workspace = true +[dev-dependencies] +nocterm-session = { workspace = true, features = ["test-support"] } +futures.workspace = true +[lints] +workspace = true diff --git a/crates/directory-io/src/lib.rs b/crates/directory-io/src/lib.rs new file mode 100644 index 00000000..22b31237 --- /dev/null +++ b/crates/directory-io/src/lib.rs @@ -0,0 +1,94 @@ +//! Read-only directory adapters over existing filesystem capabilities. +//! Owns: captured source selection and bounded directory/home reads. +//! Does not own: native traversal, mutations, request scheduling or GUI state. +//! Ports used: LocalFs, RemoteFs. Ports provided: DirectoryRead, DirectoryReaders. +//! Settings: none. Globals: none. +//! Invariants: no remote-to-local fallback; names and linked parent paths stay literal. +use nocterm_explorer::directory::{ + Directory, DirectoryError, DirectoryFuture, DirectoryListing, DirectoryRead, +}; +use nocterm_session::{ + RemoteFs, + local_fs::{CancelFlag, ListingLimits, LocalFs}, +}; +use std::sync::Arc; + +pub struct LocalDirectoryReader { + source: Arc, +} +impl LocalDirectoryReader { + pub fn new(source: Arc) -> Self { + Self { source } + } +} +impl DirectoryRead for LocalDirectoryReader { + fn home(&self) -> DirectoryFuture { + let source = self.source.clone(); + Box::pin(async move { + source + .home()? + .map(Directory::Local) + .ok_or(DirectoryError::HomeUnavailable) + }) + } + fn list(&self, directory: Directory, cancel: CancelFlag) -> DirectoryFuture { + let source = self.source.clone(); + Box::pin(async move { + let Directory::Local(path) = directory else { + return Err(DirectoryError::SourceMismatch); + }; + Ok(DirectoryListing::local(source.browse( + &path, + ListingLimits::default(), + &cancel, + )?)) + }) + } +} +pub struct RemoteDirectoryReader { + source: Arc, +} +impl RemoteDirectoryReader { + pub fn new(source: Arc) -> Self { + Self { source } + } +} +impl DirectoryRead for RemoteDirectoryReader { + fn home(&self) -> DirectoryFuture { + let source = self.source.clone(); + Box::pin(async move { Ok(Directory::Remote(source.home().await?)) }) + } + fn list(&self, directory: Directory, cancel: CancelFlag) -> DirectoryFuture { + let source = self.source.clone(); + Box::pin(async move { + cancel.check()?; + let Directory::Remote(path) = directory else { + return Err(DirectoryError::SourceMismatch); + }; + let entries = source.read_dir(&path).await?; + cancel.check()?; + DirectoryListing::remote(path, entries) + }) + } +} +/// Retained application composition for views that follow changing session sources. +pub struct DirectoryReaders { + local: Arc, +} +impl DirectoryReaders { + pub fn new(local: Arc) -> Self { + Self { + local: Arc::new(LocalDirectoryReader::new(local)), + } + } +} +impl nocterm_explorer::directory::DirectoryReaders for DirectoryReaders { + fn local(&self) -> Arc { + self.local.clone() + } + fn remote(&self, source: Arc) -> Arc { + Arc::new(RemoteDirectoryReader::new(source)) + } +} +#[cfg(test)] +mod tests; diff --git a/crates/directory-io/src/tests.rs b/crates/directory-io/src/tests.rs new file mode 100644 index 00000000..0d245da1 --- /dev/null +++ b/crates/directory-io/src/tests.rs @@ -0,0 +1,95 @@ +use super::*; +use futures::executor::block_on; +use nocterm_session::{ + FsError, RemoteFs, + local_fs::LocalFsError, + testing::{FsOperation, InMemoryFs}, +}; + +#[test] +fn remote_home_and_directory_listing_use_the_captured_source() { + let source = Arc::new(InMemoryFs::default()); + block_on(source.create_dir("/home")).unwrap(); + source.set_home("/home").unwrap(); + block_on(source.create_dir("/home/資料 folder")).unwrap(); + source.seed_file("/home/file", vec![]).unwrap(); + let reader = RemoteDirectoryReader::new(source); + assert_eq!( + block_on(reader.home()).unwrap(), + Directory::Remote("/home".into()) + ); + let listing = + block_on(reader.list(Directory::Remote("/home".into()), CancelFlag::default())).unwrap(); + assert_eq!( + listing + .candidates + .iter() + .map(|candidate| candidate.name.as_str()) + .collect::>(), + ["資料 folder"] + ); +} +#[test] +fn remote_permission_errors_and_wrong_source_never_fall_back_to_local() { + let source = Arc::new(InMemoryFs::default()); + source.fail_next( + FsOperation::List, + "/private", + FsError::PermissionDenied { + path: "/private".into(), + }, + ); + let reader = RemoteDirectoryReader::new(source); + let error = block_on(reader.list(Directory::Remote("/private".into()), CancelFlag::default())) + .unwrap_err(); + assert!(matches!( + error, + DirectoryError::Remote(FsError::PermissionDenied { .. }) + )); + assert!(matches!( + std::error::Error::source(&error).and_then(|cause| cause.downcast_ref::()), + Some(FsError::PermissionDenied { .. }) + )); + assert!(matches!( + block_on(reader.list(Directory::Local("/".into()), CancelFlag::default())), + Err(DirectoryError::SourceMismatch) + )); +} +#[test] +fn local_listing_checks_access_and_preserves_original_native_directory() { + let source = Arc::new(InMemoryFs::default()); + let directory = if cfg!(windows) { + std::path::PathBuf::from("C:\\workspace") + } else { + "/workspace".into() + }; + source.seed_native_directory(&directory).unwrap(); + source + .seed_native_directory(&directory.join("資料 folder")) + .unwrap(); + source + .seed_native_file(&directory.join("file"), vec![]) + .unwrap(); + let reader = LocalDirectoryReader::new(source); + let listing = + block_on(reader.list(Directory::Local(directory.clone()), CancelFlag::default())).unwrap(); + assert_eq!(listing.directory, Directory::Local(directory.clone())); + assert_eq!(listing.candidates.len(), 1); + assert_eq!(listing.candidates[0].name, "資料 folder"); + let cancel = CancelFlag::default(); + cancel.cancel(); + assert!(matches!( + block_on(reader.list(Directory::Local(directory), cancel)), + Err(DirectoryError::Local(LocalFsError::Cancelled)) + )); +} +#[test] +fn missing_remote_home_retains_its_typed_cause() { + let source = Arc::new(InMemoryFs::default()); + source.fail_next(FsOperation::Home, "/", FsError::NotConnected); + let reader = RemoteDirectoryReader::new(source); + assert!(matches!( + block_on(reader.home()), + Err(DirectoryError::Remote(FsError::NotConnected)) + )); +} diff --git a/crates/explorer/Cargo.toml b/crates/explorer/Cargo.toml index 9a8d78f3..e102fc2b 100644 --- a/crates/explorer/Cargo.toml +++ b/crates/explorer/Cargo.toml @@ -7,7 +7,14 @@ license-file.workspace = true publish = false [package.metadata.nocterm] layer = "domain" +[features] +default = [] +test-support = [] + [dependencies] +nocterm-session.workspace = true +futures.workspace = true +thiserror.workspace = true nocterm-settings.workspace = true serde.workspace = true schemars.workspace = true diff --git a/crates/explorer/src/directory.rs b/crates/explorer/src/directory.rs new file mode 100644 index 00000000..183998c2 --- /dev/null +++ b/crates/explorer/src/directory.rs @@ -0,0 +1,100 @@ +//! Literal navigation rules and a read-only directory capability. +mod error; +mod path; +pub use error::DirectoryError; +use futures::future::BoxFuture; +use nocterm_session::{ + DirEntry, EntryKind, RemoteFs, + local_fs::{CancelFlag, LocalListing}, +}; +pub use path::{Candidate, Cycle, Directory, PathError, Query, query, resolve}; +use std::sync::Arc; + +pub type DirectoryFuture = BoxFuture<'static, Result>; + +/// A captured filesystem source. Futures may block: poll on a background executor. +/// `list` must actually read the directory, reporting permission and type errors. +pub trait DirectoryRead: Send + Sync + 'static { + fn home(&self) -> DirectoryFuture; + fn list(&self, directory: Directory, cancel: CancelFlag) -> DirectoryFuture; +} + +/// Composition supplies source adapters; views never select a native implementation. +pub trait DirectoryReaders: Send + Sync + 'static { + fn local(&self) -> Arc; + fn remote(&self, source: Arc) -> Arc; +} + +#[derive(Clone, Debug)] +pub struct DirectoryListing { + pub directory: Directory, + pub candidates: Vec, +} +impl DirectoryListing { + pub fn local(listing: LocalListing) -> Self { + let candidates = listing + .entries + .into_iter() + .filter(|entry| entry.completion_directory) + .filter_map(|entry| entry.name.to_str().map(str::to_owned)) + .filter(|name| valid_name(name, true)) + .map(|name| Candidate { + name, + directory: true, + }) + .collect(); + Self { + directory: Directory::Local(listing.directory), + candidates, + } + } + pub fn remote(directory: String, entries: Vec) -> Result { + if entries.len() > 100_000 + || entries.iter().map(|entry| entry.name.len()).sum::() > 8 * 1024 * 1024 + { + return Err(DirectoryError::ListingLimit); + } + let candidates = entries + .into_iter() + .filter(|entry| entry.kind == EntryKind::Directory && valid_name(&entry.name, false)) + .map(|entry| Candidate { + name: entry.name, + directory: true, + }) + .collect(); + Ok(Self { + directory: Directory::Remote(directory), + candidates: sorted(candidates), + }) + } +} +fn sorted(mut candidates: Vec) -> Vec { + candidates.sort_by_cached_key(|entry| (entry.name.to_lowercase(), entry.name.clone())); + candidates +} +pub fn valid_name(name: &str, local: bool) -> bool { + !name.is_empty() + && name != "." + && name != ".." + && !name.contains('/') + && !(local && cfg!(windows) && name.contains('\\')) + && !name.chars().any(char::is_control) +} +impl Directory { + /// A display value cannot replace a native non-UTF8 path's identity. + pub fn text(&self) -> Option<&str> { + match self { + Self::Local(path) => path.to_str(), + Self::Remote(path) => Some(path), + } + } + pub fn parent(&self) -> Option { + match self { + Self::Local(path) => path.parent().map(|path| Self::Local(path.to_owned())), + Self::Remote(path) if path != "/" => Some(Self::Remote( + nocterm_session::fs::path::parent(path).to_owned(), + )), + Self::Remote(_) => None, + } + } +} diff --git a/crates/explorer/src/directory/error.rs b/crates/explorer/src/directory/error.rs new file mode 100644 index 00000000..7de2c663 --- /dev/null +++ b/crates/explorer/src/directory/error.rs @@ -0,0 +1,30 @@ +//! Failures at the shared read-only directory boundary. +#[derive(Clone, Debug, thiserror::Error)] +pub enum DirectoryError { + #[error("{0}")] + Path( + #[from] + #[source] + super::PathError, + ), + #[error("Reconnect to load directory suggestions.")] + Disconnected, + #[error("The home directory is unavailable.")] + HomeUnavailable, + #[error("This directory belongs to a different filesystem source.")] + SourceMismatch, + #[error("Directory exceeds the listing limit; enter a narrower path.")] + ListingLimit, + #[error("{0}")] + Local( + #[from] + #[source] + nocterm_session::local_fs::LocalFsError, + ), + #[error("{0}")] + Remote( + #[from] + #[source] + nocterm_session::FsError, + ), +} diff --git a/crates/files/src/path_input/completion.rs b/crates/explorer/src/directory/path.rs similarity index 89% rename from crates/files/src/path_input/completion.rs rename to crates/explorer/src/directory/path.rs index db124a02..b56f06bf 100644 --- a/crates/files/src/path_input/completion.rs +++ b/crates/explorer/src/directory/path.rs @@ -2,33 +2,33 @@ use std::path::PathBuf; #[derive(Clone, Debug, PartialEq, Eq)] -pub(crate) enum Directory { +pub enum Directory { Local(PathBuf), Remote(String), } #[derive(Clone, Debug, PartialEq, Eq)] -pub(super) struct Query { +pub struct Query { pub directory: Directory, pub head: String, pub prefix: String, } #[derive(Clone, Debug, PartialEq, Eq)] -pub(super) struct Candidate { +pub struct Candidate { pub name: String, pub directory: bool, } -#[derive(Debug, thiserror::Error)] -pub(super) enum PathError { +#[derive(Clone, Debug, thiserror::Error)] +pub enum PathError { #[error("Enter a directory path.")] EmptyOrInvalid, #[error("The home directory is unavailable.")] HomeUnavailable, } -pub(super) fn resolve( +pub fn resolve( value: &str, base: &Directory, home: Option<&Directory>, @@ -63,11 +63,7 @@ fn join(base: &Directory, value: &str) -> Directory { } } -pub(super) fn query( - value: &str, - base: &Directory, - home: Option<&Directory>, -) -> Result { +pub fn query(value: &str, base: &Directory, home: Option<&Directory>) -> Result { let value = if value == "~" { "~/" } else { value }; let separator = value .rfind(|c| c == '/' || (c == '\\' && cfg!(windows) && matches!(base, Directory::Local(_)))); @@ -87,23 +83,23 @@ pub(super) fn query( } #[derive(Default)] -pub(super) struct Cycle { +pub struct Cycle { candidates: Vec, selected: Option, head: String, } impl Cycle { - pub(super) fn candidate_count(&self) -> usize { + pub fn candidate_count(&self) -> usize { self.candidates.len() } - pub(super) fn candidate(&self, index: usize) -> &Candidate { - &self.candidates[index] + pub fn candidate(&self, index: usize) -> Option<&Candidate> { + self.candidates.get(index) } - pub(super) fn selected(&self) -> Option { + pub fn selected(&self) -> Option { self.selected } - pub(super) fn new(query: Query, entries: &[Candidate]) -> Self { + pub fn new(query: Query, entries: &[Candidate]) -> Self { Self { candidates: entries .iter() @@ -114,7 +110,7 @@ impl Cycle { selected: None, } } - pub(super) fn step(&mut self, reverse: bool) -> Option { + pub fn step(&mut self, reverse: bool) -> Option { let count = self.candidates.len(); if count == 0 { return None; @@ -127,7 +123,7 @@ impl Cycle { }; self.choose(index) } - pub(super) fn choose(&mut self, index: usize) -> Option { + pub fn choose(&mut self, index: usize) -> Option { let entry = self.candidates.get(index)?; self.selected = Some(index); Some(format!( @@ -206,7 +202,7 @@ mod tests { }, ]; let mut cycle = Cycle::new(query("ab", &base(), None).unwrap(), &entries); - assert_eq!(cycle.candidates.len(), 2); + assert_eq!(cycle.candidate_count(), 2); assert_eq!(cycle.step(false).as_deref(), Some("abc/")); assert_eq!(cycle.step(false).as_deref(), Some("abd/")); assert_eq!(cycle.step(false).as_deref(), Some("abc/")); diff --git a/crates/explorer/src/lib.rs b/crates/explorer/src/lib.rs index b8ef4570..34393e1b 100644 --- a/crates/explorer/src/lib.rs +++ b/crates/explorer/src/lib.rs @@ -1,7 +1,13 @@ //! Explorer indexing and file-opening rules. //! Owns: settings schemas, matching, argument substitution and sanitization. -//! Does not own: traversal, transfers, native paths or program execution. -//! Ports: SettingsSection. Settings: explorer. Globals: none. +//! Does not own: traversal, transfers or program execution. +//! Directory paths, completion rules and narrow read-only capabilities are pure values; +//! callers dispatch readers off the UI thread and retain their original source. +//! Ports: SettingsSection, DirectoryRead, DirectoryReaders. Settings: explorer. Globals: none. //! Invariant: ordered first matching opener and literal arguments preserve file policy. mod settings; pub use settings::*; + +pub mod directory; +#[cfg(feature = "test-support")] +pub mod testing; diff --git a/crates/explorer/src/testing.rs b/crates/explorer/src/testing.rs new file mode 100644 index 00000000..5c88026b --- /dev/null +++ b/crates/explorer/src/testing.rs @@ -0,0 +1,89 @@ +//! Test-only directory capabilities backed by session's shared in-memory ports. +//! No native traversal or private filesystem tree is implemented here. +use crate::directory::{ + Directory, DirectoryError, DirectoryFuture, DirectoryListing, DirectoryRead, DirectoryReaders, +}; +use nocterm_session::{ + RemoteFs, + local_fs::{CancelFlag, ListingLimits, LocalFs}, +}; +use std::sync::Arc; + +pub struct MemoryDirectoryReaders { + local: Arc, +} +impl MemoryDirectoryReaders { + pub fn new(local: Arc) -> Self { + Self { local } + } +} +impl DirectoryReaders for MemoryDirectoryReaders { + fn local(&self) -> Arc { + Arc::new(MemoryDirectoryReader::local(self.local.clone())) + } + fn remote(&self, source: Arc) -> Arc { + Arc::new(MemoryDirectoryReader::remote(source)) + } +} +/// Adapts existing session test capabilities; arbitrary native implementations must +/// never be supplied to this test-only owner. +pub struct MemoryDirectoryReader { + source: MemorySource, +} +enum MemorySource { + Local(Arc), + Remote(Arc), +} +impl MemoryDirectoryReader { + pub fn local(source: Arc) -> Self { + Self { + source: MemorySource::Local(source), + } + } + pub fn remote(source: Arc) -> Self { + Self { + source: MemorySource::Remote(source), + } + } +} +impl DirectoryRead for MemoryDirectoryReader { + fn home(&self) -> DirectoryFuture { + match &self.source { + MemorySource::Local(source) => { + let source = source.clone(); + Box::pin(async move { + source + .home()? + .map(Directory::Local) + .ok_or(DirectoryError::HomeUnavailable) + }) + } + MemorySource::Remote(source) => { + let future = source.home(); + Box::pin(async move { Ok(Directory::Remote(future.await?)) }) + } + } + } + fn list(&self, directory: Directory, cancel: CancelFlag) -> DirectoryFuture { + match (&self.source, directory) { + (MemorySource::Local(source), Directory::Local(path)) => { + let source = source.clone(); + Box::pin(async move { + Ok(DirectoryListing::local(source.browse( + &path, + ListingLimits::default(), + &cancel, + )?)) + }) + } + (MemorySource::Remote(source), Directory::Remote(path)) => { + let future = source.read_dir(&path); + Box::pin(async move { + cancel.check()?; + DirectoryListing::remote(path, future.await?) + }) + } + _ => Box::pin(async { Err(DirectoryError::SourceMismatch) }), + } + } +} diff --git a/crates/files/Cargo.toml b/crates/files/Cargo.toml index 2bdff8ec..1440f9e7 100644 --- a/crates/files/Cargo.toml +++ b/crates/files/Cargo.toml @@ -12,18 +12,28 @@ layer = "feature" [dependencies] nocterm-explorer.workspace = true thiserror.workspace = true +nocterm-plugin-model.workspace = true nocterm-session.workspace = true nocterm-store.workspace = true nocterm-transfers.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true +nocterm-plugin-api = { workspace = true, optional = true } +nocterm-plugin-ports = { workspace = true, optional = true } futures.workspace = true gpui-kit.workspace = true parking_lot.workspace = true chrono = { version = "0.4", default-features = false, features = ["std"] } +[features] +# The transfer counts, offered to user plugins that were granted `transfers.counts`. +user-plugins = ["dep:nocterm-plugin-api", "dep:nocterm-plugin-ports"] + [dev-dependencies] +nocterm-explorer = { workspace = true, features = ["test-support"] } +nocterm-project-model.workspace = true nocterm-settings.workspace = true +nocterm-workspace = { workspace = true, features = ["test-support"] } nocterm-transfers = { workspace = true, features = ["test-support"] } nocterm-session = { workspace = true, features = ["test-support"] } tempfile.workspace = true diff --git a/crates/files/src/activity.rs b/crates/files/src/activity.rs index bfb8767d..e568769b 100644 --- a/crates/files/src/activity.rs +++ b/crates/files/src/activity.rs @@ -45,12 +45,14 @@ impl Activity { } } -/// Whether a finished upload changed the remote folder `shown` on `target`: -/// it was copied into that folder or into one of its direct subfolders. +/// Whether a finished upload or relay changed the remote folder `shown` on +/// `target`: it was copied into that folder or into one of its direct subfolders. pub(crate) fn changes_remote(job: &Progress, host: &HostAddress, shown: &str) -> bool { let trim = |value: &str| value.trim_end_matches('/').to_owned(); - job.direction == TransferDirection::Upload - && job.host == *host + matches!( + job.direction, + TransferDirection::Upload | TransferDirection::Relay + ) && job.host == *host && (trim(&job.destination) == trim(shown) || trim(path::parent(&job.destination)) == trim(shown)) } @@ -75,6 +77,7 @@ pub(crate) fn strip<'a>( let verb = match job.direction { TransferDirection::Upload => "Uploading", TransferDirection::Download => "Downloading", + TransferDirection::Relay => "Copying", }; let fraction = if job.total_bytes > 0 { job.sent_bytes as f32 / job.total_bytes as f32 @@ -94,6 +97,7 @@ pub(crate) fn strip<'a>( Icon::new(match job.direction { TransferDirection::Upload => IconName::ArrowUp, TransferDirection::Download => IconName::ArrowDown, + TransferDirection::Relay => IconName::ArrowRight, }) .xsmall(), ) @@ -207,5 +211,12 @@ mod tests { assert!(changes_local(&download, Path::new("/tmp/in"))); assert!(changes_local(&download, Path::new("/tmp"))); assert!(!changes_local(&upload, Path::new("/srv/app"))); + let relay = job( + TransferDirection::Relay, + "/srv/app", + TransferState::Completed, + ); + assert!(changes_remote(&relay, &target, "/srv/app")); + assert!(!changes_local(&relay, Path::new("/srv/app"))); } } diff --git a/crates/files/src/drag_ui_tests.rs b/crates/files/src/drag_ui_tests.rs index 83312a68..84d91207 100644 --- a/crates/files/src/drag_ui_tests.rs +++ b/crates/files/src/drag_ui_tests.rs @@ -96,8 +96,10 @@ fn native_local_and_remote_previews_keep_full_rows_and_symbolic_link_names( let fs = Arc::new(PendingFs::default()); let (deps, local) = fixture_deps_at(directory.path()); let (handle, workspace, _, panel) = panel_fixture_with(cx, fs.clone(), deps); - cx.update_window(handle, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + cx.update_window(handle, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(directory.path().into(), cx) }); @@ -163,8 +165,10 @@ fn native_remote_multiselection_preview_keeps_grabbed_name_and_transfers_both_fi let service = deps.transfers.clone(); let (handle, workspace, _, panel) = panel_fixture_with(cx, fs.clone(), deps); let service = cx - .update_window(handle, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + .update_window(handle, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(directory.path().into(), cx) }); diff --git a/crates/files/src/host_capabilities.rs b/crates/files/src/host_capabilities.rs new file mode 100644 index 00000000..717f574e --- /dev/null +++ b/crates/files/src/host_capabilities.rs @@ -0,0 +1,61 @@ +//! How many transfers run and how many failed, offered to user plugins that were granted +//! `transfers.counts`. +//! +//! Passive: it reads the snapshot of the transfer service the status bar and the queue already +//! poll, and starts, retries or cancels nothing. +use std::sync::Arc; + +use gpui_kit::{App, Entity}; +use nocterm_plugin_api::{ + HostError, + capability::transfers::{Counts, CountsNow}, +}; +use nocterm_plugin_model::PluginId; +use nocterm_plugin_ports::Observe; +use nocterm_transfers::TransferService; + +use crate::Installed; + +/// The counts of the transfer queue shared by every window. +pub struct TransferCounts { + source: CountsSource, +} +enum CountsSource { + Projection(Entity), + Direct(Arc), +} + +impl TransferCounts { + pub fn new(installed: &Installed) -> Self { + Self { + source: CountsSource::Projection(installed.projection().clone()), + } + } + + /// The counts of `service`, for a composition that holds the service without [`Installed`]. + pub fn over(service: Arc) -> Self { + Self { + source: CountsSource::Direct(service), + } + } +} + +impl Observe for TransferCounts { + /// Transfers not yet finished, and transfers in which a file failed. + fn sample(&self, _: &PluginId, cx: &App) -> Result { + let snapshot: Arc<[nocterm_transfers::Progress]> = match &self.source { + CountsSource::Projection(projection) => projection.read(cx).entries().clone(), + CountsSource::Direct(service) => service.snapshot().into(), + }; + let count = |matching: &dyn Fn(&nocterm_transfers::Progress) -> bool| { + u16::try_from(snapshot.iter().filter(|job| matching(job)).count()).unwrap_or(u16::MAX) + }; + Ok(Counts { + active: count(&|job| !job.state.finished()), + failed: count(&|job| job.failed_files > 0), + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/files/src/host_capabilities/tests.rs b/crates/files/src/host_capabilities/tests.rs new file mode 100644 index 00000000..9c030361 --- /dev/null +++ b/crates/files/src/host_capabilities/tests.rs @@ -0,0 +1,126 @@ +//! The counts follow the transfer queue. +use std::sync::Arc; + +use futures::executor::block_on; +use gpui_kit::TestAppContext; +use nocterm_plugin_api::{ + HostError, + capability::transfers::{Counts, CountsNow}, +}; +use nocterm_plugin_model::PluginId; +use nocterm_plugin_ports::Observe; +use nocterm_session::{HostAddress, RemoteFs as _, Target, testing::InMemoryFs}; +use nocterm_transfers::{ + CollisionPolicy, DownloadRequest, TransferCause, TransferEvent, TransferId, TransferIssue, + TransferOperation, TransferService, testing::FakeTransfers, +}; + +use super::TransferCounts; + +struct Queue { + recording: Arc, + counts: TransferCounts, + fs: Arc, +} + +fn queue() -> Queue { + let recording = Arc::new(FakeTransfers::default()); + let fs = Arc::new(InMemoryFs::default()); + block_on(fs.create_dir("/home")).unwrap(); + Queue { + counts: TransferCounts::over(recording.clone()), + recording, + fs, + } +} + +impl Queue { + fn start(&self) -> TransferId { + self.recording + .enqueue_download(DownloadRequest { + sources: vec!["/home".into()], + host: HostAddress::direct(Target::new("test", "host", 22)), + local_destination: "/tmp/received".into(), + fs: self.fs.clone(), + collisions: CollisionPolicy::Skip, + }) + .unwrap() + } + + fn finish(&self, id: TransferId) { + for event in [ + TransferEvent::DiscoveryComplete, + TransferEvent::WorkersDrained, + ] { + self.recording.advance(id, event).unwrap(); + } + } + + fn fail_a_file(&self, id: TransferId) { + let issue = TransferIssue::new( + None, + TransferOperation::Discover, + TransferCause::InvalidName, + ); + self.recording + .advance(id, TransferEvent::FileFailed(issue)) + .unwrap(); + } + + fn read(&self, cx: &mut TestAppContext) -> Result { + let plugin = PluginId::user("acme", "queue").unwrap(); + cx.update(|cx| Observe::::sample(&self.counts, &plugin, cx)) + } +} + +fn counts(active: u16, failed: u16) -> Result { + Ok(Counts { active, failed }) +} + +#[gpui_kit::test] +fn an_empty_queue_has_nothing_running_or_failed(cx: &mut TestAppContext) { + assert_eq!(queue().read(cx), counts(0, 0)); +} + +#[gpui_kit::test] +fn running_transfers_are_counted_until_they_finish(cx: &mut TestAppContext) { + let queue = queue(); + let first = queue.start(); + queue.start(); + assert_eq!(queue.read(cx), counts(2, 0)); + queue.finish(first); + assert_eq!(queue.read(cx), counts(1, 0), "a completed transfer is done"); +} + +#[gpui_kit::test] +fn a_transfer_with_a_failed_file_counts_as_failed_once_finished(cx: &mut TestAppContext) { + let queue = queue(); + let id = queue.start(); + queue.fail_a_file(id); + assert_eq!( + queue.read(cx), + counts(1, 1), + "the failure shows while it runs" + ); + queue.finish(id); + assert_eq!(queue.read(cx), counts(0, 1)); +} + +#[gpui_kit::test] +fn a_cancelled_transfer_is_neither_running_nor_failed(cx: &mut TestAppContext) { + let queue = queue(); + let id = queue.start(); + queue.recording.cancel(id).unwrap(); + assert_eq!(queue.read(cx), counts(0, 0)); +} + +#[gpui_kit::test] +fn reading_the_counts_changes_nothing_in_the_queue(cx: &mut TestAppContext) { + let queue = queue(); + queue.start(); + let before = queue.recording.snapshot(); + for _ in 0..3 { + queue.read(cx).unwrap(); + } + assert_eq!(queue.recording.snapshot(), before); +} diff --git a/crates/files/src/lib.rs b/crates/files/src/lib.rs index 6a6de1f7..de1c010d 100644 --- a/crates/files/src/lib.rs +++ b/crates/files/src/lib.rs @@ -1,29 +1,41 @@ //! Explorer navigation, file dialogs and transfer presentation. -//! Browser generations, scan results and completion cursors remain private transition owners. +//! Browser generations, shared row selections, scan results and completion cursors remain private transition owners. //! -//! Owns: independent pane views, path editors, selection and pinned operation dialogs. +//! Owns: independent pane views, acknowledged browser navigation, selection and pinned dialogs. +//! Shared path editors and navigation controls belong to nocterm-ui. //! Does not own: native filesystem traversal/mutations, transfer execution or process spawning. -//! Ports used: `LocalFs`, `LocalMutations`, `RemoteFs`, `TransferService`, `ProgramOpener`, +//! Ports used: `LocalFs`, `LocalMutations`, `RemoteFs`, `DirectoryReaders`, `TransferService`, `ProgramOpener`, //! workspace `LocalTerminal` and the retained settings service handle. -//! Ports provided: `Panel`, `SettingsPageSpec`. Settings section: explorer. Globals: none. +//! Ports provided: `Panel`, `SettingsPageSpec` and the core `files.explorer` and +//! `files.transfers` contributions (`plugin::CORE`) and, behind `user-plugins`, the +//! `TransferCounts` provider of `transfers.counts` for user plugins. Settings section: explorer. Globals: none. +//! ExplorerSource supplies ordinary session files or a local folder for the +//! injected LocalFs. The folder is an initial location; navigation stays ordinary. //! Invariants: native paths keep their original names; each pane rejects stale navigation; //! accepted operations retain their original capability while later navigation proceeds. mod activity; mod dialogs; +#[cfg(feature = "user-plugins")] +mod host_capabilities; mod local; mod local_pane; mod open; mod operations; -mod path_input; +pub mod plugin; +mod projection; +use nocterm_ui::directory as path_input; mod registration; mod remote; mod remote_pane; mod row; +mod selection; mod settings; mod settings_page; mod statistics; #[cfg(test)] mod tests; +#[cfg(test)] +use nocterm_workspace::SessionContext; mod transfers; use gpui_kit::{ @@ -46,12 +58,15 @@ use nocterm_session::{EntryKind, RemoteFs, fs::path}; use nocterm_transfers::{CollisionPolicy, DownloadRequest, UploadRequest}; use nocterm_ui::{ActiveDesign as _, IconName}; use nocterm_workspace::{ - FileDrag, Panel, RemoteFileDrag as RemotePaths, SessionContext, Workspace, WorkspaceEvent, + ExplorerSource, FileDrag, Panel, RemoteFileDrag as RemotePaths, Workspace, WorkspaceEvent, }; use operations::FileTarget; use remote::{Browser, listing}; -use std::{collections::BTreeSet, path::PathBuf, rc::Rc, sync::Arc, time::Duration}; +use selection::RowSelection; +use std::{path::PathBuf, rc::Rc, sync::Arc, time::Duration}; +#[cfg(feature = "user-plugins")] +pub use host_capabilities::TransferCounts; pub use registration::{ Installed, OpenExplorerSettings, ShowTransfers, ToggleExplorer, install, register, }; @@ -68,19 +83,25 @@ pub struct Deps { mutations: Arc, transfers: Arc, opener: Arc, + directories: Arc, } impl Deps { + pub fn directory_readers(&self) -> Arc { + self.directories.clone() + } pub fn new( local: Arc, mutations: Arc, transfers: Arc, opener: Arc, + directories: Arc, ) -> Self { Self { local, mutations, transfers, opener, + directories, } } } @@ -106,6 +127,7 @@ enum PaneIntent { Upload { sources: Vec, destination: Option, + source: Option, }, Download { files: RemotePaths, @@ -195,7 +217,7 @@ impl FilesPanel { fn new( installed: Installed, workspace: Entity, - session: Option, + source: Option, window: &mut Window, cx: &mut Context, ) -> Self { @@ -205,25 +227,48 @@ impl FilesPanel { window: window.window_handle(), }; let local = cx.new(|cx| LocalPane::new(&installed, host.clone(), window, cx)); - let remote = - cx.new(|cx| RemotePane::new(installed.local().clone(), host, session, window, cx)); + if let Some(directory) = source + .as_ref() + .filter(|source| source.is_local()) + .and_then(ExplorerSource::initial_directory) + { + local.update(cx, |local, cx| local.load_local(directory.into(), cx)); + } + let remote = cx.new(|cx| RemotePane::new(host, source, window, cx)); let local_events = cx.subscribe(&local, |this, _, event, cx| this.intent(event, cx)); let remote_events = cx.subscribe(&remote, |this, _, event, cx| this.intent(event, cx)); let workspace_events = cx.subscribe(&workspace, |this, workspace, event, cx| { - if *event == WorkspaceEvent::ActiveSessionChanged { + if matches!( + event, + WorkspaceEvent::ActiveSessionChanged + | WorkspaceEvent::ContextChanged + | WorkspaceEvent::SurfaceItemsChanged(_) + ) { let workspace = workspace.clone(); let panel = cx.entity().downgrade(); cx.defer(move |cx| { - let session = workspace.read(cx).active_session(cx); + let source = workspace.read(cx).explorer_source(cx); let _ = panel.update(cx, |this, cx| { - this.remote - .update(cx, |remote, cx| remote.follow(session, cx)) + if let Some(directory) = this + .remote + .update(cx, |remote, cx| remote.follow(source, cx)) + { + this.local + .update(cx, |local, cx| local.load_local(directory, cx)); + } }); }); } else if *event == WorkspaceEvent::LocalDirectoryChanged { this.local.update(cx, |_, cx| cx.notify()); } }); + let transfer_events = cx.observe(installed.projection(), |this, _, cx| { + this.transfers_changed(cx) + }); + let weak = cx.entity().downgrade(); + cx.defer(move |cx| { + let _ = weak.update(cx, |this, cx| this.transfers_changed(cx)); + }); let tick = cx.spawn_in(window, async move |this, cx| { loop { cx.background_executor() @@ -246,7 +291,12 @@ impl FilesPanel { collisions: CollisionPolicy::Skip, window: window.window_handle(), _observers: PanelObservers { - _subscriptions: vec![local_events, remote_events, workspace_events], + _subscriptions: vec![ + local_events, + remote_events, + workspace_events, + transfer_events, + ], _tick: tick, }, installed, @@ -262,11 +312,17 @@ impl FilesPanel { PaneIntent::Upload { sources, destination, + source, } => { - let result = self - .remote - .read(cx) - .transfer_target(destination.clone()) + let result = source + .as_ref() + .and_then(|source| { + Some(( + source.filesystem()?, + source.address()?, + destination.clone()?, + )) + }) .map(|(fs, host, destination)| { self.installed.transfers().enqueue(UploadRequest { sources: sources.clone(), @@ -340,7 +396,7 @@ impl FilesPanel { }); } fn synchronize(&mut self, finished: &[nocterm_transfers::Progress], cx: &mut Context) { - let jobs = self.installed.transfers().snapshot(); + let jobs = self.installed.projection().read(cx).entries().to_vec(); self.local.update(cx, |local, cx| { local.transfers( jobs.iter() @@ -359,9 +415,14 @@ impl FilesPanel { remote.transfers(jobs, finished, ready, self.collisions, cx) }); } + fn transfers_changed(&mut self, cx: &mut Context) { + let snapshot = self.installed.projection().read(cx).entries().to_vec(); + let (changed, finished) = self.activity.update(snapshot); + if changed { + self.synchronize(&finished, cx); + } + } fn poll(&mut self, window: &mut Window, cx: &mut Context) { - let (_, finished) = self.activity.update(self.installed.transfers().snapshot()); - self.synchronize(&finished, cx); self.local .update(cx, |local, cx| local.poll_statistics(window, cx)); self.remote diff --git a/crates/files/src/listing_ui_tests.rs b/crates/files/src/listing_ui_tests.rs index 437725f2..638fa02a 100644 --- a/crates/files/src/listing_ui_tests.rs +++ b/crates/files/src/listing_ui_tests.rs @@ -8,6 +8,106 @@ mod drag_ui; #[path = "path_ui_tests.rs"] mod path_ui; +#[gpui_kit::test] +fn a_local_workspace_uses_the_local_filesystem_and_allows_parent_navigation( + cx: &mut TestAppContext, +) { + let directory = tempfile::tempdir().unwrap(); + let folder = directory.path().join("folder's code"); + std::fs::create_dir(&folder).unwrap(); + std::fs::write(folder.join("inside.txt"), b"inside").unwrap(); + std::fs::write(directory.path().join("outside.txt"), b"outside").unwrap(); + let (handle, workspace, _, _general, installed) = panel_fixture_with_installation( + cx, + Arc::new(PendingFs::default()), + fixture_deps_at(directory.path()).0, + ); + cx.run_until_parked(); + let panel = cx + .update_window(handle, |_, window, cx| { + let source = ExplorerSource::local_folder( + nocterm_project_model::ProjectId::from_bytes([1; 16]), + nocterm_project_model::RootPath::parse(folder.to_str().unwrap()).unwrap(), + ); + cx.new(|cx| FilesPanel::new(installed, workspace, Some(source), window, cx)) + }) + .unwrap(); + cx.run_until_parked(); + panel.read_with(cx, |panel, cx| { + assert_eq!(panel.remote_access(cx), RemoteAccess::NoSession); + assert_eq!( + panel.local_listing(cx).path().as_deref(), + Some(folder.as_path()) + ); + assert_eq!(panel.local_listing(cx).names(), ["inside.txt"]); + }); + panel.update(cx, |panel, cx| { + panel.navigate_local(directory.path().into(), cx) + }); + cx.run_until_parked(); + panel.read_with(cx, |panel, cx| { + assert_eq!( + panel.local_listing(cx).path().as_deref(), + Some(directory.path()) + ); + assert!( + panel + .local_listing(cx) + .names() + .iter() + .any(|name| name == "outside.txt") + ); + }); +} + +#[gpui_kit::test] +fn a_workspace_folder_is_the_initial_location_and_does_not_block_absolute_navigation( + cx: &mut TestAppContext, +) { + let fs = Arc::new(nocterm_session::testing::InMemoryFs::default()); + for directory in ["/home", "/home/test", "/home/test/folder's code"] { + block_on(fs.create_dir(directory)).unwrap(); + } + fs.set_home("/home/test").unwrap(); + fs.seed_file("/home/test/folder's code/inside.txt", b"inside".to_vec()) + .unwrap(); + fs.seed_file("/outside.txt", b"outside".to_vec()).unwrap(); + let (handle, workspace, _, _general, installed) = + panel_fixture_with_installation(cx, fs, fixture_deps()); + cx.run_until_parked(); + let panel = cx + .update_window(handle, |_, window, cx| { + let session = workspace.read(cx).active_session(cx).unwrap(); + let source = ExplorerSource::folder( + nocterm_project_model::ProjectId::from_bytes([1; 16]), + nocterm_project_model::RootPath::parse("/home/test/folder's code").unwrap(), + session, + ); + cx.new(|cx| FilesPanel::new(installed, workspace, Some(source), window, cx)) + }) + .unwrap(); + cx.run_until_parked(); + panel.read_with(cx, |panel, cx| { + assert_eq!( + panel.remote_listing(cx).path().as_deref(), + Some("/home/test/folder's code") + ); + assert_eq!(panel.remote_listing(cx).names(), ["inside.txt"]); + }); + panel.update(cx, |panel, cx| panel.navigate_remote(Some("/".into()), cx)); + cx.run_until_parked(); + panel.read_with(cx, |panel, cx| { + assert_eq!(panel.remote_listing(cx).path().as_deref(), Some("/")); + assert!( + panel + .remote_listing(cx) + .names() + .iter() + .any(|name| name == "outside.txt") + ); + }); +} + #[gpui_kit::test] fn hidden_explorer_reports_partial_statistics_once_and_stale_action_cannot_navigate( cx: &mut TestAppContext, @@ -178,6 +278,8 @@ fn remote_folders_are_counted_except_the_home_folder(cx: &mut TestAppContext) { /// Publishes every upload at once and counts listings of each folder. #[gpui_kit::test] fn a_finished_upload_lists_the_shown_remote_folder_again(cx: &mut TestAppContext) { + use super::transfer_ui::{drag_row, show_local}; + use gpui_kit::test::TestWindowExt as _; let directory = tempfile::tempdir().unwrap(); let file = directory.path().join("report.txt"); std::fs::write(&file, b"done").unwrap(); @@ -185,22 +287,16 @@ fn a_finished_upload_lists_the_shown_remote_folder_again(cx: &mut TestAppContext fs.script_listings(); let (deps, _, recording) = transfer_fixture_deps_at(directory.path()); let service = deps.transfers.clone(); - let (handle, _, _, panel) = panel_fixture_with(cx, fs.clone(), deps); + let (handle, workspace, _, panel) = panel_fixture_with(cx, fs.clone(), deps); cx.run_until_parked(); let initial = fs.take_pending_listing().unwrap(); assert_eq!(initial.path(), "/home/test"); initial.send(Ok(Vec::new())).unwrap(); cx.run_until_parked(); - cx.update_window(handle, |_, _, cx| { - panel.update(cx, |panel, cx| { - panel.intent( - &PaneIntent::Upload { - sources: vec![file.clone()], - destination: None, - }, - cx, - ) - }); + show_local(cx, handle, &workspace, &panel, directory.path()); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + drag_row(window, "local-home", "files-home", 36., cx); }) .unwrap(); let id = service.snapshot()[0].id; @@ -247,8 +343,10 @@ fn empty_directory_upload_refreshes_the_shown_remote_destination(cx: &mut TestAp let (deps, _, recording) = transfer_fixture_deps_at(directory.path()); let service = deps.transfers.clone(); let (handle, workspace, _, panel) = panel_fixture_with(cx, fs.clone(), deps); - cx.update_window(handle, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + cx.update_window(handle, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); }) .unwrap(); cx.simulate_window_resize(handle, gpui_kit::size(px(1400.), px(1000.))); @@ -308,8 +406,10 @@ fn empty_directory_download_refreshes_the_shown_local_destination(cx: &mut TestA let (deps, local, recording) = transfer_fixture_deps_at(directory.path()); let service = deps.transfers.clone(); let (handle, workspace, _, panel) = panel_fixture_with(cx, fs, deps); - cx.update_window(handle, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + cx.update_window(handle, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(directory.path().to_owned(), cx) }); @@ -372,8 +472,10 @@ fn partial_directory_download_refreshes(cx: &mut TestAppContext, cancelled: bool let (deps, local, recording) = transfer_fixture_deps_at(directory.path()); let service = deps.transfers.clone(); let (handle, workspace, _, panel) = panel_fixture_with(cx, fs.clone(), deps); - cx.update_window(handle, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + cx.update_window(handle, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(directory.path().to_owned(), cx) }); diff --git a/crates/files/src/local_pane.rs b/crates/files/src/local_pane.rs index d32bb3f5..124b803d 100644 --- a/crates/files/src/local_pane.rs +++ b/crates/files/src/local_pane.rs @@ -20,8 +20,7 @@ struct LocalBrowser { path: Option, requested: Option, entries: Vec, - selected: BTreeSet, - anchor: Option, + selection: RowSelection, loading: bool, error: Option, counter: statistics::Counter, @@ -47,7 +46,8 @@ impl LocalPane { window: &mut Window, cx: &mut Context, ) -> Self { - let path = cx.new(|cx| path_input::PathInput::new(installed.local().clone(), window, cx)); + let path = + cx.new(|cx| path_input::PathInput::new(installed.directories().local(), window, cx)); let subscription = cx.subscribe(&path, |this, _, event: &path_input::Navigate, cx| { if let path_input::Directory::Local(path) = &event.directory { this.load_local_with_navigation(path.clone(), event.mode, cx); @@ -102,7 +102,7 @@ impl LocalPane { .iter() .map(|entry| entry.name.clone()) .collect(), - selected: self.browser.selected.iter().copied().collect(), + selected: self.browser.selection.indices().collect(), loading: self.browser.loading, error: self.browser.error.clone(), statistics: statistics::summary(self.browser.counter.shown()), @@ -304,8 +304,7 @@ impl LocalPane { }); self.browser.path = Some(directory.to_owned()); self.browser.entries = entries; - self.browser.selected.clear(); - self.browser.anchor = None; + self.browser.selection.clear(); } Err(error) => { self.path.update(cx, |input, cx| { @@ -328,21 +327,11 @@ impl LocalPane { event: &gpui_kit::MouseDownEvent, cx: &mut Context, ) { - if event.modifiers.shift { - let anchor = self.browser.anchor.unwrap_or(ix); - self.browser - .selected - .extend(anchor.min(ix)..=anchor.max(ix)); - } else if event.modifiers.control || event.modifiers.platform { - if !self.browser.selected.insert(ix) { - self.browser.selected.remove(&ix); - } - self.browser.anchor = Some(ix); - } else { - self.browser.selected.clear(); - self.browser.selected.insert(ix); - self.browser.anchor = Some(ix); - } + self.browser.selection.click( + ix, + event.modifiers.shift, + event.modifiers.control || event.modifiers.platform, + ); if event.click_count == 2 && let Some(entry) = self .browser @@ -372,27 +361,16 @@ impl LocalPane { } pub(super) fn paths(&self, index: usize) -> Vec { - if self.browser.selected.contains(&index) { - self.browser - .selected - .iter() - .filter_map(|ix| self.browser.entries.get(*ix)) - .map(|e| e.path.clone()) - .collect() - } else { - self.browser - .entries - .get(index) - .map(|e| vec![e.path.clone()]) - .unwrap_or_default() - } + self.browser + .selection + .for_row(index) + .filter_map(|ix| self.browser.entries.get(ix)) + .map(|entry| entry.path.clone()) + .collect() } fn select_for_menu(&mut self, ix: usize, cx: &mut Context) { - if !self.browser.selected.contains(&ix) { - self.browser.selected.clear(); - self.browser.selected.insert(ix); - self.browser.anchor = Some(ix); + if self.browser.selection.context_click(ix) { cx.notify(); } } @@ -400,7 +378,7 @@ impl LocalPane { pub(super) fn render_local_row(&self, ix: usize, cx: &mut Context) -> AnyElement { let entry = &self.browser.entries[ix]; let paths = self.paths(ix); - let visual = ExplorerRow::local(entry, self.browser.selected.contains(&ix), cx); + let visual = ExplorerRow::local(entry, self.browser.selection.contains(ix), cx); let source = nocterm_ui::DragSource::default(); let destination = entry.path.clone(); let directory = entry.kind == EntryKind::Directory && !entry.symlink; @@ -481,57 +459,37 @@ impl LocalPane { h_flex() .gap_1() .child(div().flex_1().text_sm().child("Local")) - .child( - Button::new("local-home") - .small() - .ghost() - .label("Home") - .disabled(self.browser.loading) - .on_click(cx.listener(|this, _, _, cx| { - if let Some(home) = this.browser.home.clone() { - this.load_local(home, cx); - } - })), - ) - .child( - Button::new("local-parent") - .small() - .ghost() - .label("Up") - .disabled( - self.browser.loading - || self - .browser - .path - .as_deref() - .and_then(|p| p.parent()) - .is_none(), - ) - .on_click(cx.listener(|this, _, _, cx| { - if let Some(parent) = this - .browser - .path - .as_deref() - .and_then(|p| p.parent()) - .map(|p| p.to_owned()) - { - this.load_local(parent, cx); - } - })), - ) - .child( - Button::new("local-refresh") - .small() - .ghost() - .icon(IconName::RefreshCw) - .tooltip("Refresh local directory") - .disabled(self.browser.loading) - .on_click(cx.listener(|this, _, _, cx| { - if let Some(path) = this.browser.requested.clone() { - this.load_local(path, cx); - } - })), - ) + .child(path_input::DirectoryNavigation::new( + "local", + !self.browser.loading, + self.browser + .path + .as_deref() + .and_then(|path| path.parent()) + .is_some(), + { + let pane = cx.entity().downgrade(); + move |action, _, cx| { + let _ = pane.update(cx, |pane, cx| { + let directory = match action { + path_input::NavigationAction::Home => pane.browser.home.clone(), + path_input::NavigationAction::Up => pane + .browser + .path + .as_deref() + .and_then(|path| path.parent()) + .map(ToOwned::to_owned), + path_input::NavigationAction::Refresh => { + pane.browser.requested.clone() + } + }; + if let Some(directory) = directory { + pane.load_local(directory, cx); + } + }); + } + }, + )) } fn footer(&self, cwd: Option, cx: &mut Context) -> impl IntoElement { let information = statistics::summary(self.browser.counter.shown()); diff --git a/crates/files/src/open_ui_tests.rs b/crates/files/src/open_ui_tests.rs index b043b4f3..dda7fb35 100644 --- a/crates/files/src/open_ui_tests.rs +++ b/crates/files/src/open_ui_tests.rs @@ -69,8 +69,11 @@ fn remote_fixture( let (window, workspace) = gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { - let deps = - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)); + let deps = nocterm_workspace::test_support::deps_for_core( + &[crate::plugin::CORE], + &[], + cx, + ); let mut workspace = Workspace::new(deps, window, cx); workspace .set_session_opener(move |_, spec, _, _| record.borrow_mut().push(spec)); @@ -86,11 +89,13 @@ fn remote_fixture( focus: cx.focus_handle(), }); workspace.update(cx, |workspace, cx| workspace.add_item(item, window, cx)); - let session = workspace.read(cx).active_session(cx); + let session = workspace.read(cx).explorer_source(cx); let panel = cx.new(|cx| { FilesPanel::new(installed.clone(), workspace.clone(), session, window, cx) }); - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel }) .unwrap(); diff --git a/crates/files/src/open_ui_tests/local.rs b/crates/files/src/open_ui_tests/local.rs index 09dd2dc0..d80850aa 100644 --- a/crates/files/src/open_ui_tests/local.rs +++ b/crates/files/src/open_ui_tests/local.rs @@ -45,8 +45,10 @@ fn fixture_with_store( .with(settings) .with(nocterm_session::LocalShellSettings::default()), ); - cx.update_window(window, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(directory.path().into(), cx) }); diff --git a/crates/files/src/operations_ui_tests.rs b/crates/files/src/operations_ui_tests.rs index 25b841b1..b564eda2 100644 --- a/crates/files/src/operations_ui_tests.rs +++ b/crates/files/src/operations_ui_tests.rs @@ -152,7 +152,9 @@ fn visible_panel_with( cx.update_window(handle, |_, window, cx| { window.activate_window(); cx.set_reduce_motion(true); - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| panel.navigate_local(local.into(), cx)); }) .unwrap(); @@ -736,7 +738,9 @@ fn local_properties_apply_and_right_click_keep_multi_selection(cx: &mut TestAppC cx.update_window(handle, |_, window, cx| { window.activate_window(); cx.set_reduce_motion(true); - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(directory.path().into(), cx) }); diff --git a/crates/files/src/path_ui_tests.rs b/crates/files/src/path_ui_tests.rs index f80592d6..a90e4268 100644 --- a/crates/files/src/path_ui_tests.rs +++ b/crates/files/src/path_ui_tests.rs @@ -15,8 +15,10 @@ fn show_panel( panel: &Entity, cx: &mut TestAppContext, ) { - cx.update_window(handle, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + cx.update_window(handle, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); }) .unwrap(); cx.simulate_window_resize(handle, gpui_kit::size(px(1400.), px(1000.))); diff --git a/crates/files/src/plugin.rs b/crates/files/src/plugin.rs new file mode 100644 index 00000000..09286060 --- /dev/null +++ b/crates/files/src/plugin.rs @@ -0,0 +1,50 @@ +//! What the files feature contributes to the window shell. +//! +//! Files are core: the Explorer and the transfer status are never disabled and belong to no +//! Plugins page. A catalog lists them through [`CORE`]. +use nocterm_plugin_model::{ + AutoPlace, ContributionId, ContributionKind, Icon, PanelChrome, PanelTraits, Placement, + SystemContribution, WidthClass, Zone, +}; + +/// The Explorer in the sidebar. +pub const EXPLORER: SystemContribution = SystemContribution { + id: ContributionId::new("files.explorer"), + kind: ContributionKind::Panel, + title: "Explorer", + icon: Icon::FolderTree, + placement: Placement { + zone: Zone::Left, + order: 20, + group: Some("navigation"), + auto: AutoPlace::Show, + }, + panel: Some(PanelTraits { + width: WidthClass::Navigation, + chrome: PanelChrome::Titled, + }), + preview: None, + edge: None, + pinned: false, +}; + +/// The progress of file transfers in the status bar. +pub const TRANSFERS: SystemContribution = SystemContribution { + id: ContributionId::new("files.transfers"), + kind: ContributionKind::Widget, + title: "Transfers", + icon: Icon::Upload, + placement: Placement { + zone: Zone::Right, + order: 20, + group: None, + auto: AutoPlace::Show, + }, + panel: None, + preview: None, + edge: None, + pinned: false, +}; + +/// Every contribution of the feature. +pub const CORE: &[SystemContribution] = &[EXPLORER, TRANSFERS]; diff --git a/crates/files/src/plugin_tests.rs b/crates/files/src/plugin_tests.rs new file mode 100644 index 00000000..71a0008d --- /dev/null +++ b/crates/files/src/plugin_tests.rs @@ -0,0 +1,85 @@ +//! The Explorer registers under its descriptors and the toggle action drives that id. +use super::*; +use crate::plugin::{EXPLORER, TRANSFERS}; +use gpui_kit::{AppContext as _, Focusable as _}; +use nocterm_ui::contribution_icon; +use nocterm_workspace::test_support::{Toggle, toggle_presentation}; + +fn registered(cx: &mut TestAppContext) -> (AnyWindowHandle, Entity) { + let (window, workspace, _, _, installed) = + panel_fixture_with_installation(cx, Arc::new(FakeFs), fixture_deps()); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + crate::register(&installed, workspace, window, cx).unwrap(); + }); + }) + .unwrap(); + cx.run_until_parked(); + (window, workspace) +} + +#[gpui_kit::test] +fn the_sidebar_toggle_shows_the_descriptors_title_and_icon(cx: &mut TestAppContext) { + let (_, workspace) = registered(cx); + cx.update(|cx| { + let workspace = workspace.read(cx); + assert!(workspace.is_registered(&EXPLORER.id)); + assert!(workspace.is_registered(&TRANSFERS.id)); + assert_eq!( + toggle_presentation(workspace, Toggle::Navigation(0), cx), + Some((EXPLORER.title.into(), contribution_icon(EXPLORER.icon))) + ); + }); +} + +#[gpui_kit::test] +fn the_toggle_action_hides_the_shown_explorer_and_shows_it_again(cx: &mut TestAppContext) { + let (window, workspace) = registered(cx); + let dispatch = |cx: &mut TestAppContext| { + cx.update_window(window, |_, window, cx| { + window.focus(&workspace.read(cx).focus_handle(cx), cx); + window.dispatch_action(Box::new(ToggleExplorer), cx); + }) + .unwrap(); + cx.run_until_parked(); + }; + let open = + |cx: &mut TestAppContext| cx.update(|cx| workspace.read(cx).panel_is_open(&EXPLORER.id)); + assert!(open(cx), "the sidebar opens on its first panel"); + dispatch(cx); + assert!(!open(cx)); + dispatch(cx); + assert!(open(cx)); +} + +#[gpui_kit::test] +fn explorer_status_and_queue_share_one_periodic_transfer_snapshot(cx: &mut TestAppContext) { + let directory = tempfile::tempdir().unwrap(); + let (deps, _, recording) = transfer_fixture_deps_at(directory.path()); + let (window, workspace, _, _panel, installed) = + panel_fixture_with_installation(cx, Arc::new(FakeFs), deps); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + crate::register(&installed, workspace, window, cx).unwrap() + }); + let queue = cx.new(|cx| { + crate::transfers::TransfersView::new( + installed.transfers().clone(), + installed.projection().clone(), + workspace.downgrade(), + cx, + ) + }); + workspace.update(cx, |workspace, cx| workspace.add_item(queue, window, cx)); + }) + .unwrap(); + cx.run_until_parked(); + let before = recording.snapshot_count(); + cx.executor().advance_clock(Duration::from_millis(150)); + cx.run_until_parked(); + assert_eq!( + recording.snapshot_count(), + before + 1, + "legacy polling is shared by all views" + ); +} diff --git a/crates/files/src/projection.rs b/crates/files/src/projection.rs new file mode 100644 index 00000000..ce86a82e --- /dev/null +++ b/crates/files/src/projection.rs @@ -0,0 +1,62 @@ +//! One installation-scoped, coalesced projection of the shared transfer service. +//! Native snapshot locking/copying is background work; views observe immutable entries. +use gpui_kit::{Context, Task}; +use nocterm_transfers::{Progress, TransferService}; +use std::{sync::Arc, time::Duration}; + +pub(crate) struct TransferProjection { + entries: Arc<[Progress]>, + _poll: Task<()>, +} +impl TransferProjection { + pub(crate) fn new(service: Arc, cx: &mut Context) -> Self { + let poll = cx.spawn(async move |this, cx| { + let mut revision = None; + let mut previous: Arc<[Progress]> = Arc::from([]); + loop { + let poll_service = service.clone(); + let before = previous.clone(); + let result = cx + .background_executor() + .spawn(async move { + let next = poll_service.revision(); + if next.is_some() && next != Some(u64::MAX) && next == revision { + return (revision, None); + } + let snapshot: Arc<[Progress]> = poll_service.snapshot().into(); + let changed = (snapshot != before).then_some(snapshot); + (next, changed) + }) + .await; + revision = result.0; + if let Some(snapshot) = result.1 { + previous = snapshot.clone(); + if this + .update(cx, |this, cx| { + this.entries = snapshot; + cx.notify(); + }) + .is_err() + { + return; + } + } else if this.upgrade().is_none() { + return; + } + if let Some(wait) = revision.and_then(|revision| service.changed_since(revision)) { + wait.await; + } + cx.background_executor() + .timer(Duration::from_millis(150)) + .await; + } + }); + Self { + entries: Arc::from([]), + _poll: poll, + } + } + pub(crate) fn entries(&self) -> &Arc<[Progress]> { + &self.entries + } +} diff --git a/crates/files/src/registration.rs b/crates/files/src/registration.rs index 36dee536..68dcbd6a 100644 --- a/crates/files/src/registration.rs +++ b/crates/files/src/registration.rs @@ -1,9 +1,9 @@ //! The Explorer's place in the workspace: its sidebar panel, the transfer //! status and the commands that show them. -use gpui_kit::{App, AppContext as _, Context, Window}; -use nocterm_workspace::Workspace; +use gpui_kit::{App, AppContext as _, Context, Entity, Window}; +use nocterm_workspace::{PanelRegistration, RegistrationError, WidgetRegistration, Workspace}; -use crate::{Deps, FilesPanel, transfers}; +use crate::{Deps, FilesPanel, plugin, transfers}; gpui_kit::actions!( files, @@ -23,8 +23,17 @@ gpui_kit::actions!( pub struct Installed { deps: Deps, settings: nocterm_store::SettingsHandle, + projection: Entity, } impl Installed { + pub(crate) fn projection(&self) -> &Entity { + &self.projection + } + pub(crate) fn directories( + &self, + ) -> &std::sync::Arc { + &self.deps.directories + } pub(crate) fn settings(&self) -> &nocterm_store::SettingsHandle { &self.settings } @@ -56,36 +65,57 @@ pub fn install(deps: Deps, ready: &nocterm_ui::UiReady, cx: &mut App) -> Install ready .settings() .register::(cx); - crate::path_input::install_bindings(cx); + let projection = + cx.new(|cx| crate::projection::TransferProjection::new(deps.transfers.clone(), cx)); Installed { + projection, deps, settings: ready.settings().clone(), } } +/// Adds the Explorer panel, the transfer status and the commands to a workspace. +/// +/// The commands are registered even when the workspace refuses a contribution. pub fn register( installed: &Installed, workspace: &mut Workspace, window: &mut Window, cx: &mut Context, -) { - let active = workspace.active_session(cx); - let handle = cx.entity(); - let panel = cx.new(|cx| FilesPanel::new(installed.clone(), handle, active, window, cx)); - workspace.add_panel(panel, cx); - let status = - cx.new(|cx| transfers::TransferStatus::new(installed.deps.transfers.clone(), window, cx)); - workspace.add_status_view(status, cx); +) -> Result<(), RegistrationError> { workspace.register_action(|workspace, _: &ToggleExplorer, window, cx| { - workspace.toggle_panel_of::(window, cx); + workspace.toggle_panel(&plugin::EXPLORER.id, window, cx); }); let service = installed.deps.transfers.clone(); + let projection = installed.projection.clone(); workspace.register_action(move |workspace, _: &ShowTransfers, window, cx| { - if let Some(view) = workspace.find_item::() { + if let Some(view) = workspace.find_item::(cx) { workspace.activate_item_by_id(view.entity_id(), window, cx); } else { let handle = cx.entity().downgrade(); - let view = cx.new(|cx| transfers::TransfersView::new(service.clone(), handle, cx)); + let view = cx.new(|cx| { + transfers::TransfersView::new(service.clone(), projection.clone(), handle, cx) + }); workspace.add_item(view, window, cx); } }); + let panel_installed = installed.clone(); + let panel = workspace.register_panel( + PanelRegistration::new(&plugin::EXPLORER, move |workspace, window, cx| { + let handle = cx.entity(); + let active = workspace.explorer_source(cx); + cx.new(|cx| FilesPanel::new(panel_installed.clone(), handle, active, window, cx)) + }), + window, + cx, + ); + let transfers = installed.projection.clone(); + let status = workspace.register_widget( + WidgetRegistration::new(&plugin::TRANSFERS, move |_, window, cx| { + let transfers = transfers.clone(); + cx.new(|cx| transfers::TransferStatus::new(transfers, window, cx)) + }), + window, + cx, + ); + panel.and(status) } diff --git a/crates/files/src/remote_pane.rs b/crates/files/src/remote_pane.rs index 4733919b..9ef14904 100644 --- a/crates/files/src/remote_pane.rs +++ b/crates/files/src/remote_pane.rs @@ -10,12 +10,24 @@ struct RemotePresentation { collisions: CollisionPolicy, download_enabled: bool, } +impl RemotePresentation { + fn hint(source: Option<&ExplorerSource>) -> Option<&'static str> { + match source { + None => Some("Open a connection to browse remote files."), + Some(source) if source.is_local() => Some("Open a connection to browse remote files."), + Some(source) if !source.connected() => Some("Disconnected. Reconnect to browse files."), + Some(source) if source.filesystem().is_none() => { + Some("This host does not provide SFTP.") + } + _ => None, + } + } +} pub(super) struct RemotePane { - session: Option, + source: Option, browser: Browser, requested: Option, - selected: BTreeSet, - anchor: Option, + selection: RowSelection, path: Entity, _subscription: Subscription, counter: statistics::Counter, @@ -26,24 +38,23 @@ pub(super) struct RemotePane { impl EventEmitter for RemotePane {} impl RemotePane { pub(super) fn new( - local: Arc, host: PaneHost, - session: Option, + source: Option, window: &mut Window, cx: &mut Context, ) -> Self { - let path = cx.new(|cx| path_input::PathInput::new(local, window, cx)); + let path = cx + .new(|cx| path_input::PathInput::new(host.installed.directories().local(), window, cx)); let subscription = cx.subscribe(&path, |this, _, event: &path_input::Navigate, cx| { if let path_input::Directory::Remote(path) = &event.directory { this.load_with_navigation(Some(path.clone()), event.mode, cx); } }); let mut pane = Self { - session: None, + source: None, browser: Browser::default(), requested: None, - selected: BTreeSet::new(), - anchor: None, + selection: RowSelection::default(), path, _subscription: subscription, counter: statistics::Counter::default(), @@ -56,7 +67,7 @@ impl RemotePane { download_enabled: false, }, }; - pane.follow(session, cx); + let _ = pane.follow(source, cx); pane } pub(super) fn listing(&self) -> ListingState { @@ -69,30 +80,21 @@ impl RemotePane { .iter() .map(|entry| entry.name.clone()) .collect(), - selected: self.selected.iter().copied().collect(), + selected: self.selection.indices().collect(), loading: self.browser.loading(), error: self.browser.error().map(ToString::to_string), statistics: statistics::summary(self.counter.shown()), } } pub(super) fn access(&self) -> RemoteAccess { - match &self.session { + match &self.source { None => RemoteAccess::NoSession, - Some(session) if !session.connected => RemoteAccess::Disconnected, - Some(session) if session.fs.is_none() => RemoteAccess::Unsupported, + Some(session) if session.is_local() => RemoteAccess::NoSession, + Some(session) if !session.connected() => RemoteAccess::Disconnected, + Some(session) if session.filesystem().is_none() => RemoteAccess::Unsupported, Some(_) => RemoteAccess::Available, } } - pub(super) fn transfer_target( - &self, - destination: Option, - ) -> Option<(Arc, nocterm_session::HostAddress, String)> { - Some(( - self.filesystem()?, - self.session.as_ref()?.address(), - destination.or_else(|| self.browser.path().map(str::to_owned))?, - )) - } pub(super) fn transfers( &mut self, jobs: Vec, @@ -101,22 +103,28 @@ impl RemotePane { collisions: CollisionPolicy, cx: &mut Context, ) { - if let Some(session) = &self.session + if let Some(session) = &self.source && let Some(shown) = self.browser.path() - && finished - .iter() - .any(|job| activity::changes_remote(job, &session.address(), shown)) + && finished.iter().any(|job| { + session + .address() + .is_some_and(|address| activity::changes_remote(job, &address, shown)) + }) { self.stale = true; } let activity = self - .session + .source .as_ref() .map(|session| { jobs.into_iter() .filter(|job| { - job.host == session.address() - && job.direction == nocterm_transfers::TransferDirection::Upload + Some(&job.host) == session.address().as_ref() + && matches!( + job.direction, + nocterm_transfers::TransferDirection::Upload + | nocterm_transfers::TransferDirection::Relay + ) && !job.state.finished() }) .collect() @@ -158,31 +166,48 @@ impl RemotePane { }); }) } - pub(super) fn follow(&mut self, session: Option, cx: &mut Context) { - if matches!((&self.session, &session), (Some(before), Some(after)) if before.same_as(after)) - { - return; + pub(super) fn follow( + &mut self, + source: Option, + cx: &mut Context, + ) -> Option { + if matches!((&self.source, &source), (Some(before), Some(after)) if before.same_as(after)) { + return None; } - self.session = session; - let filesystem = self.filesystem(); + let initial_directory = source + .as_ref() + .and_then(ExplorerSource::initial_directory) + .map(str::to_owned); + self.source = source; + let reader = self.filesystem().map(|source| { + self.presentation + .host + .installed + .directories() + .remote(source) + }); self.path - .update(cx, |input, cx| input.reset_remote(filesystem, cx)); + .update(cx, |input, cx| input.replace_reader(reader, cx)); self.requested = None; self.browser.clear(); - self.selected.clear(); - self.anchor = None; + self.selection.clear(); self.task = None; self.counter.set(Default::default()); if self.filesystem().is_some() { - self.load(None, cx); + self.load(initial_directory, cx); } cx.notify(); + self.source + .as_ref() + .filter(|source| source.is_local()) + .and_then(ExplorerSource::initial_directory) + .map(PathBuf::from) } pub(super) fn filesystem(&self) -> Option> { - self.session + self.source .as_ref() - .filter(|s| s.connected) - .and_then(|s| s.fs.clone()) + .filter(|s| s.connected() && !s.is_local()) + .and_then(|s| s.filesystem()) } } impl RemotePane { @@ -202,8 +227,7 @@ impl RemotePane { self.path .update(cx, |input, cx| input.begin_navigation_with_mode(mode, cx)); let generation = self.browser.begin(); - self.selected.clear(); - self.anchor = None; + self.selection.clear(); self.requested = directory.clone(); cx.notify(); let window = self.presentation.host.window; @@ -293,11 +317,11 @@ impl RemotePane { pub(super) fn remote_file_target(&self, index: usize) -> Option { let entry = self.browser.entries().get(index)?; - let session = self.session.as_ref()?; + let session = self.source.as_ref()?; Some(FileTarget::Remote { path: path::join(self.browser.path()?, &entry.name), - host: session.address(), - fs: session.fs.clone()?, + host: session.address()?, + fs: session.filesystem()?, }) } @@ -309,7 +333,7 @@ impl RemotePane { .collect(); Some(RemotePaths { sources, - host: self.session.as_ref()?.address(), + host: self.source.as_ref()?.address()?, fs: self.filesystem()?, }) } @@ -320,19 +344,11 @@ impl RemotePane { event: &gpui_kit::MouseDownEvent, cx: &mut Context, ) { - if event.modifiers.shift { - let anchor = self.anchor.unwrap_or(ix); - self.selected.extend(anchor.min(ix)..=anchor.max(ix)); - } else if event.modifiers.control || event.modifiers.platform { - if !self.selected.insert(ix) { - self.selected.remove(&ix); - } - self.anchor = Some(ix); - } else { - self.selected.clear(); - self.selected.insert(ix); - self.anchor = Some(ix); - } + self.selection.click( + ix, + event.modifiers.shift, + event.modifiers.control || event.modifiers.platform, + ); if event.click_count == 2 && let Some(entry) = self .browser @@ -366,32 +382,21 @@ impl RemotePane { } fn select_for_menu(&mut self, ix: usize, cx: &mut Context) { - if !self.selected.contains(&ix) { - self.selected.clear(); - self.selected.insert(ix); - self.anchor = Some(ix); + if self.selection.context_click(ix) { cx.notify(); } } - fn selection_for_row(&self, ix: usize) -> Vec { - if self.selected.contains(&ix) { - self.selected.iter().copied().collect() - } else { - vec![ix] - } - } - pub(super) fn render_remote_row(&self, ix: usize, cx: &mut Context) -> AnyElement { let entry = &self.browser.entries()[ix]; let directory = entry.kind == EntryKind::Directory; - let selected = self.selection_for_row(ix); + let selected = self.selection.for_row(ix).collect::>(); let download = self.paths(selected.iter().copied()); let visual = ExplorerRow { name: entry.name.clone(), directory: entry.kind == EntryKind::Directory, symlink: entry.is_symlink, - selected: self.selected.contains(&ix), + selected: self.selection.contains(ix), font_size: cx.design().typography.explorer_size.unwrap_or(12.0), }; let source = nocterm_ui::DragSource::default(); @@ -468,6 +473,8 @@ impl RemotePane { stop_propagation: bool, cx: &mut Context, ) -> E { + let upload_source = self.source.clone(); + let drop_path = drop_path.or_else(|| self.browser.path().map(str::to_owned)); row.can_drop(|drag, _, _| { matches!(drag.downcast_ref::(), Some(FileDrag::Local(_))) || drag.is::() @@ -484,12 +491,14 @@ impl RemotePane { }) .on_drop(cx.listener({ let drop_path = drop_path.clone(); + let source = upload_source.clone(); move |_, files: &FileDrag, _, cx| { if stop_propagation { cx.stop_propagation(); } if let FileDrag::Local(paths) = files { cx.emit(PaneIntent::Upload { + source: source.clone(), sources: paths.clone(), destination: drop_path.clone(), }); @@ -501,6 +510,7 @@ impl RemotePane { cx.stop_propagation(); } cx.emit(PaneIntent::Upload { + source: upload_source.clone(), sources: files.paths().to_vec(), destination: drop_path.clone(), }); @@ -517,10 +527,12 @@ impl RemotePane { .icon(IconName::ArrowDown) .tooltip("Download selected items to the local Explorer directory") .disabled( - !enabled || self.selected.is_empty() || !self.presentation.download_enabled, + !enabled + || self.selection.is_empty() + || !self.presentation.download_enabled, ) .on_click(cx.listener(|this, _, _, cx| { - if let Some(files) = this.paths(this.selected.iter().copied()) { + if let Some(files) = this.paths(this.selection.indices()) { cx.emit(PaneIntent::Download { files, destination: None, @@ -528,33 +540,27 @@ impl RemotePane { } })), ) - .child( - Button::new("files-home") - .small() - .ghost() - .label("Home") - .disabled(!enabled) - .on_click(cx.listener(|this, _, _, cx| this.load(None, cx))), - ) - .child( - Button::new("files-parent") - .small() - .ghost() - .label("Up") - .disabled(!enabled || self.browser.path().is_none_or(|p| p == "/")) - .on_click(cx.listener(|this, _, _, cx| { - this.load(this.browser.path().map(|p| path::parent(p).to_owned()), cx) - })), - ) - .child( - Button::new("files-refresh") - .small() - .ghost() - .icon(IconName::RefreshCw) - .tooltip("Refresh remote directory") - .disabled(!enabled) - .on_click(cx.listener(|this, _, _, cx| this.load(this.requested.clone(), cx))), - ) + .child(path_input::DirectoryNavigation::new( + "files", + enabled, + self.browser.path().is_some_and(|path| path != "/"), + { + let pane = cx.entity().downgrade(); + move |action, _, cx| { + let _ = pane.update(cx, |pane, cx| { + let directory = match action { + path_input::NavigationAction::Home => None, + path_input::NavigationAction::Up => pane + .browser + .path() + .map(|directory| path::parent(directory).to_owned()), + path_input::NavigationAction::Refresh => pane.requested.clone(), + }; + pane.load(directory, cx); + }); + } + }, + )) } fn collision_controls(&self, cx: &mut Context) -> impl IntoElement { h_flex() @@ -594,12 +600,7 @@ impl RemotePane { } pub(super) fn render_remote(&self, cx: &mut Context) -> AnyElement { let enabled = self.filesystem().is_some() && !self.browser.loading(); - let hint = match &self.session { - None => Some("Open a connection to browse remote files."), - Some(s) if !s.connected => Some("Disconnected. Reconnect to browse files."), - Some(s) if s.fs.is_none() => Some("This host does not provide SFTP."), - _ => None, - }; + let hint = RemotePresentation::hint(self.source.as_ref()); let browser = v_flex() .id("remote-browser") .overflow_hidden() diff --git a/crates/files/src/rename_ui_tests.rs b/crates/files/src/rename_ui_tests.rs index fd4149e0..7e49b3df 100644 --- a/crates/files/src/rename_ui_tests.rs +++ b/crates/files/src/rename_ui_tests.rs @@ -104,7 +104,9 @@ fn local_navigation_during_rename_keeps_original_path_without_refreshing_new_fol cx.update_window(handle, |_, window, cx| { window.activate_window(); cx.set_reduce_motion(true); - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(source.path().into(), cx) }); diff --git a/crates/files/src/selection.rs b/crates/files/src/selection.rs new file mode 100644 index 00000000..2e4b7d1d --- /dev/null +++ b/crates/files/src/selection.rs @@ -0,0 +1,56 @@ +//! Row membership and range-anchor transitions shared by both Explorer panes. +use std::collections::BTreeSet; + +#[derive(Default)] +pub(super) struct RowSelection { + selected: BTreeSet, + anchor: Option, +} +impl RowSelection { + pub(super) fn clear(&mut self) { + self.selected.clear(); + self.anchor = None; + } + pub(super) fn click(&mut self, index: usize, shift: bool, toggle: bool) { + if shift { + let anchor = self.anchor.unwrap_or(index); + self.selected.extend(anchor.min(index)..=anchor.max(index)); + } else if toggle { + if !self.selected.insert(index) { + self.selected.remove(&index); + } + self.anchor = Some(index); + } else { + self.replace(index); + } + } + /// A context click keeps a multi-selection when its row is already a member. + pub(super) fn context_click(&mut self, index: usize) -> bool { + if self.contains(index) { + false + } else { + self.replace(index); + true + } + } + fn replace(&mut self, index: usize) { + self.selected.clear(); + self.selected.insert(index); + self.anchor = Some(index); + } + pub(super) fn contains(&self, index: usize) -> bool { + self.selected.contains(&index) + } + pub(super) fn is_empty(&self) -> bool { + self.selected.is_empty() + } + pub(super) fn indices(&self) -> impl Iterator + '_ { + self.selected.iter().copied() + } + pub(super) fn for_row(&self, index: usize) -> impl Iterator + '_ { + let selected = self.contains(index); + self.indices() + .take(if selected { self.selected.len() } else { 0 }) + .chain((!selected).then_some(index)) + } +} diff --git a/crates/files/src/selection_ui_tests.rs b/crates/files/src/selection_ui_tests.rs new file mode 100644 index 00000000..4f3ee670 --- /dev/null +++ b/crates/files/src/selection_ui_tests.rs @@ -0,0 +1,93 @@ +//! Both panes expose the same selection gestures while keeping separate paths. +use super::*; +use gpui_kit::{Modifiers, test::TestWindowExt as _}; + +#[gpui_kit::test] +fn both_panes_preserve_context_selection_and_reset_the_range_anchor_on_navigation( + cx: &mut TestAppContext, +) { + let directory = tempfile::tempdir().unwrap(); + for name in ["a.txt", "b.txt", "c.txt"] { + std::fs::write(directory.path().join(name), b"local").unwrap(); + } + let (handle, workspace, _, panel) = + panel_fixture_at(cx, transfer_remote(false), directory.path()); + transfer_ui::show_local(cx, handle, &workspace, &panel, directory.path()); + for local in [true, false] { + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let group = if local { "local-entry" } else { "remote-entry" }; + let first = window.find((group, 0usize)).bounds().center(); + let third = window.find((group, 2usize)).bounds().center(); + pointer_click(window, first, Default::default(), cx); + pointer_click( + window, + third, + Modifiers { + shift: true, + ..Default::default() + }, + cx, + ); + let middle = window.find((group, 1usize)).bounds().center(); + pointer_click( + window, + middle, + Modifiers { + platform: true, + ..Default::default() + }, + cx, + ); + }) + .unwrap(); + assert_selection(&panel, local, &[0, 2], cx); + operations_ui::context_menu(cx, handle, local, 2); + assert_selection(&panel, local, &[0, 2], cx); + cx.update_window(handle, |_, window, cx| window.press("escape", cx)) + .unwrap(); + operations_ui::context_menu(cx, handle, local, 1); + assert_selection(&panel, local, &[1], cx); + cx.update_window(handle, |_, window, cx| window.press("escape", cx)) + .unwrap(); + panel.update(cx, |panel, cx| { + if local { + panel.navigate_local(directory.path().to_owned(), cx); + } else { + panel.navigate_remote(Some("/home/test".into()), cx); + } + }); + cx.run_until_parked(); + assert_selection(&panel, local, &[], cx); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + let group = if local { "local-entry" } else { "remote-entry" }; + let third = window.find((group, 2usize)).bounds().center(); + pointer_click( + window, + third, + Modifiers { + shift: true, + ..Default::default() + }, + cx, + ); + }) + .unwrap(); + assert_selection(&panel, local, &[2], cx); + } +} +fn assert_selection( + panel: &Entity, + local: bool, + expected: &[usize], + cx: &mut TestAppContext, +) { + panel.read_with(cx, |panel, cx| { + if local { + assert_eq!(panel.local_listing(cx).selected(), expected); + } else { + assert_eq!(panel.remote_listing(cx).selected(), expected); + } + }); +} diff --git a/crates/files/src/tests.rs b/crates/files/src/tests.rs index 65ef3a28..dd9a51ef 100644 --- a/crates/files/src/tests.rs +++ b/crates/files/src/tests.rs @@ -65,6 +65,9 @@ fn fixture_deps() -> Deps { let local = Arc::new(nocterm_session::testing::InMemoryFs::default()); Deps { local: local.clone(), + directories: Arc::new(nocterm_explorer::testing::MemoryDirectoryReaders::new( + local.clone(), + )), mutations: local, transfers: Arc::new(nocterm_transfers::testing::FakeTransfers::default()), opener: Arc::new(nocterm_session::testing::FakeExec::default()), @@ -76,6 +79,9 @@ fn fixture_deps_at(root: &std::path::Path) -> (Deps, Arc nocterm_transfers::DownloadRequest { match recording.request(id).unwrap() { nocterm_transfers::Request::Download(request) => request, - nocterm_transfers::Request::Upload(_) => panic!("expected a download request"), + nocterm_transfers::Request::Upload(_) | nocterm_transfers::Request::Relay(_) => { + panic!("expected a download request") + } } } fn recorded_upload( @@ -124,7 +132,9 @@ fn recorded_upload( ) -> nocterm_transfers::UploadRequest { match recording.request(id).unwrap() { nocterm_transfers::Request::Upload(request) => request, - nocterm_transfers::Request::Download(_) => panic!("expected an upload request"), + nocterm_transfers::Request::Download(_) | nocterm_transfers::Request::Relay(_) => { + panic!("expected an upload request") + } } } fn complete_recorded( @@ -188,6 +198,21 @@ fn native_bytes(local: &dyn nocterm_session::local_fs::LocalFs, path: &std::path source.close().unwrap(); result } +/// Registers the files panel as the Explorer contribution. +fn add_explorer( + workspace: &mut Workspace, + panel: Entity, + window: &mut Window, + cx: &mut Context, +) { + workspace + .register_panel( + nocterm_workspace::PanelRegistration::with_entity(&crate::plugin::EXPLORER, panel), + window, + cx, + ) + .unwrap(); +} fn panel_fixture( cx: &mut TestAppContext, fs: Arc, @@ -265,8 +290,11 @@ fn panel_fixture_with_settings( let (window, workspace) = gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { - let deps = - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)); + let deps = nocterm_workspace::test_support::deps_for_core( + &[crate::plugin::CORE], + &[], + cx, + ); Workspace::new(deps, window, cx) }) }) @@ -284,7 +312,7 @@ fn panel_fixture_with_settings( workspace.update(cx, |workspace, cx| { workspace.add_item(item.clone(), window, cx) }); - let session = workspace.read(cx).active_session(cx); + let session = workspace.read(cx).explorer_source(cx); let panel = cx.new(|cx| { FilesPanel::new(installed.clone(), workspace.clone(), session, window, cx) }); @@ -558,7 +586,7 @@ fn local_directory_buttons_sync_only_local_context_and_report_busy_shell(cx: &mu busy: false, }); workspace.update(cx, |workspace, cx| { - workspace.add_panel(panel.clone(), cx); + add_explorer(workspace, panel.clone(), window, cx); workspace.set_local_terminal(shell.clone(), window, cx); }); panel.update(cx, |panel, cx| panel.navigate_local(first.clone(), cx)); @@ -684,6 +712,10 @@ fn pointer_click( mod listing_ui; #[path = "operations_ui_tests.rs"] mod operations_ui; +#[path = "plugin_tests.rs"] +mod plugin; +#[path = "selection_ui_tests.rs"] +mod selection_ui; #[path = "transfer_ui_tests.rs"] mod transfer_ui; diff --git a/crates/files/src/transfer_ui_tests.rs b/crates/files/src/transfer_ui_tests.rs index 2fbb6cb3..e33b65bf 100644 --- a/crates/files/src/transfer_ui_tests.rs +++ b/crates/files/src/transfer_ui_tests.rs @@ -1,15 +1,17 @@ //! Transfer UI emits pinned requests; streaming belongs to adapter integration tests. use super::*; -fn show_local( +pub(super) fn show_local( cx: &mut TestAppContext, handle: AnyWindowHandle, workspace: &Entity, panel: &Entity, directory: &std::path::Path, ) { - cx.update_window(handle, |_, _, cx| { - workspace.update(cx, |workspace, cx| workspace.add_panel(panel.clone(), cx)); + cx.update_window(handle, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + add_explorer(workspace, panel.clone(), window, cx) + }); panel.update(cx, |panel, cx| { panel.navigate_local(directory.to_owned(), cx) }); @@ -18,7 +20,13 @@ fn show_local( cx.simulate_window_resize(handle, gpui_kit::size(px(1400.), px(1000.))); cx.run_until_parked(); } -fn drag_row(window: &mut Window, source: &str, destination: &str, offset: f32, cx: &mut App) { +pub(super) fn drag_row( + window: &mut Window, + source: &str, + destination: &str, + offset: f32, + cx: &mut App, +) { use gpui_kit::test::TestWindowExt as _; let source = window.find(SharedString::from(source.to_owned())).bounds(); let destination = window @@ -86,7 +94,8 @@ fn external_folder_drop_pins_context_internal_drag_queues_and_queue_cancel_works let replacement = transfer_remote(true); let (deps, _, recording) = transfer_fixture_deps_at(directory.path()); let service = deps.transfers.clone(); - let (handle, workspace, item, panel) = panel_fixture_with(cx, original.clone(), deps); + let (handle, workspace, item, panel, installed) = + panel_fixture_with_installation(cx, original.clone(), deps); show_local(cx, handle, &workspace, &panel, directory.path()); cx.update_window(handle, |_, window, cx| { drop_external_on_remote(window, &file, cx); @@ -122,8 +131,14 @@ fn external_folder_drop_pins_context_internal_drag_queues_and_queue_cancel_works "second-host" ); cx.update_window(handle, |_, window, cx| { - let queue = - cx.new(|cx| transfers::TransfersView::new(service.clone(), workspace.downgrade(), cx)); + let queue = cx.new(|cx| { + transfers::TransfersView::new( + service.clone(), + installed.projection().clone(), + workspace.downgrade(), + cx, + ) + }); workspace.update(cx, |workspace, cx| workspace.add_item(queue, window, cx)); window.render_frame(cx); window.click(SharedString::from(format!("cancel-upload-{current}")), cx); diff --git a/crates/files/src/transfers.rs b/crates/files/src/transfers.rs index f0295e31..071162bd 100644 --- a/crates/files/src/transfers.rs +++ b/crates/files/src/transfers.rs @@ -1,7 +1,9 @@ //! Presentation of the retained transfer service. Available outside Explorer. +use crate::projection::TransferProjection; use crate::{ShowTransfers, local::bytes}; use gpui_kit::{ - App, Context, EventEmitter, FocusHandle, Focusable, SharedString, Task, WeakEntity, Window, + App, Context, Entity, EventEmitter, FocusHandle, Focusable, SharedString, Subscription, + WeakEntity, Window, component::{ ActiveTheme as _, Disableable as _, Icon, Sizable as _, button::{Button, ButtonVariants as _}, @@ -13,78 +15,66 @@ use gpui_kit::{ use nocterm_transfers::{Progress, TransferDirection, TransferService}; use nocterm_ui::IconName; use nocterm_workspace::{Item, ItemEvent, Workspace}; -use std::{sync::Arc, time::Duration}; +use std::sync::Arc; pub(super) struct TransferStatus { - service: Arc, - snapshot: Vec, - _poll: Task<()>, + snapshot: Arc<[Progress]>, + _projection: Entity, + _observation: Subscription, } impl TransferStatus { pub(super) fn new( - service: Arc, + projection: Entity, window: &mut Window, cx: &mut Context, ) -> Self { + let snapshot = projection.read(cx).entries().clone(); + let observation = cx.observe_in(&projection, window, |this, projection, window, cx| { + let snapshot = projection.read(cx).entries().clone(); + this.changed(snapshot, window, cx); + }); Self { - snapshot: service.snapshot(), - service, - _poll: cx.spawn_in(window, async move |this, cx| { - loop { - cx.background_executor() - .timer(Duration::from_millis(150)) - .await; - if this - .update_in(cx, |this, window, cx| { - let snapshot = this.service.snapshot(); - if snapshot != this.snapshot { - for job in &snapshot { - if job.state.finished() - && job.failed_files > 0 - && !this - .snapshot - .iter() - .any(|old| old.id == job.id && old.state.finished()) - { - let error = job - .errors - .first() - .map(ToString::to_string) - .unwrap_or_else(|| { - format!( - "{} files failed; review the transfer queue.", - job.failed_files - ) - }); - nocterm_ui::notice::error_action( - window, - cx, - "files-transfer-failed", - "Transfer failed", - error, - "Open Transfers", - |window, cx| { - window.defer(cx, |window, cx| { - window.dispatch_action( - Box::new(ShowTransfers), - cx, - ) - }); - }, - ); - } - } - this.snapshot = snapshot; - cx.notify(); - } + snapshot, + _projection: projection, + _observation: observation, + } + } + fn changed(&mut self, snapshot: Arc<[Progress]>, window: &mut Window, cx: &mut Context) { + for job in snapshot.iter() { + if job.state.finished() + && job.failed_files > 0 + && !self + .snapshot + .iter() + .any(|old| old.id == job.id && old.state.finished()) + { + let error = job + .errors + .first() + .map(ToString::to_string) + .unwrap_or_else(|| { + format!( + "{} files failed; review the transfer queue.", + job.failed_files + ) + }); + nocterm_ui::notice::error_action( + window, + cx, + "files-transfer-failed", + "Transfer failed", + error, + "Open Transfers", + |window, cx| { + window.defer(cx, |window, cx| { + window.dispatch_action(Box::new(ShowTransfers), cx) }) - .is_err() - { - break; - } - } - }), + }, + ); + } } + self.snapshot = snapshot; + cx.notify(); } } impl Render for TransferStatus { @@ -110,40 +100,31 @@ pub(super) struct TransfersView { service: Arc, focus: FocusHandle, workspace: WeakEntity, - snapshot: Vec, - _poll: Task<()>, + snapshot: Arc<[Progress]>, + _projection: Entity, + _observation: Subscription, } impl TransfersView { pub(super) fn new( service: Arc, + projection: Entity, workspace: WeakEntity, cx: &mut Context, ) -> Self { - let snapshot = service.snapshot(); + // One bounded initial read preserves immediate queue actions when opened. + // Periodic reads and all subsequent fan-out use the background projection. + let snapshot = service.snapshot().into(); + let observation = cx.observe(&projection, |this, projection, cx| { + this.snapshot = projection.read(cx).entries().clone(); + cx.notify(); + }); Self { service, focus: cx.focus_handle(), workspace, snapshot, - _poll: cx.spawn(async move |this, cx| { - loop { - cx.background_executor() - .timer(Duration::from_millis(150)) - .await; - if this - .update(cx, |this, cx| { - let snapshot = this.service.snapshot(); - if this.snapshot != snapshot { - this.snapshot = snapshot; - cx.notify(); - } - }) - .is_err() - { - break; - } - } - }), + _projection: projection, + _observation: observation, } } } @@ -182,11 +163,14 @@ impl Render for TransfersView { let id = job.id; let retry_fs = workspace .as_ref() - .and_then(|workspace| workspace.read(cx).connected_session_for_host(&job.host)) + .and_then(|workspace| { + workspace.read(cx).connected_session_for_host(&job.host, cx) + }) .and_then(|session| session.fs); let direction = match job.direction { TransferDirection::Upload => "Upload", TransferDirection::Download => "Download", + TransferDirection::Relay => "Copy", }; let description = format!( "{direction} · {} · {} complete / {} files · {} / {}{} · {:?}", diff --git a/crates/hub-agents-model/Cargo.toml b/crates/hub-agents-model/Cargo.toml new file mode 100644 index 00000000..147a5f2a --- /dev/null +++ b/crates/hub-agents-model/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "nocterm-hub-agents-model" +description = "Agents and agent servers of a hub: records, validation and the ports the hub adapter implements." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "domain" + +[features] +test-support = [] + +[dependencies] +nocterm-hubs-model.workspace = true +nocterm-session.workspace = true + +futures.workspace = true +thiserror.workspace = true + +[lints] +workspace = true diff --git a/crates/hub-agents-model/src/agent.rs b/crates/hub-agents-model/src/agent.rs new file mode 100644 index 00000000..13dfdef2 --- /dev/null +++ b/crates/hub-agents-model/src/agent.rs @@ -0,0 +1,110 @@ +//! An agent: a named configuration that costs nothing while no chat runs it. + +use crate::error::DraftError; +use crate::ids::{AgentId, ProviderId, ServerId}; + +/// The longest agent name, in characters. +pub const MAX_NAME_CHARS: usize = 80; +/// The longest instructions, in bytes. +pub const MAX_INSTRUCTIONS_BYTES: usize = 32 * 1024; +/// The longest model name, in bytes. +const MAX_MODEL_BYTES: usize = 200; + +/// When an agent waits for a person before it uses a tool. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ApprovalMode { + /// Every tool runs at once. + #[default] + Auto, + /// Tools that change something wait for "Allow / Allow in this chat / Deny" from any + /// device; read-only tools run at once. + Ask, +} + +/// One agent of the account, as the hub lists it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubAgent { + pub id: AgentId, + pub name: String, + pub instructions: String, + pub provider: ProviderId, + /// The model new chats start with; always one of `models`. + pub model: String, + /// The models a chat may switch to. + pub models: Vec, + pub approval: ApprovalMode, + /// The servers new chats start with. + pub servers: Vec, +} + +impl HubAgent { + /// The form that edits this agent. + pub fn draft(&self) -> AgentDraft { + AgentDraft { + name: self.name.clone(), + instructions: self.instructions.clone(), + provider: Some(self.provider.clone()), + model: self.model.clone(), + models: self.models.clone(), + approval: self.approval, + servers: self.servers.clone(), + } + } +} + +/// What a person typed to create or change an agent. [`AgentDraft::validate`] checks it +/// before it reaches the hub. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct AgentDraft { + pub name: String, + pub instructions: String, + pub provider: Option, + /// The default model; empty takes the first of `models`. + pub model: String, + pub models: Vec, + pub approval: ApprovalMode, + pub servers: Vec, +} + +impl AgentDraft { + /// The draft with trimmed text, duplicate models dropped and the default model among the + /// models, or the first rule it breaks. + pub fn validate(mut self) -> Result { + self.name = self.name.trim().to_owned(); + if self.name.is_empty() { + return Err(DraftError::NameMissing); + } + if self.name.chars().count() > MAX_NAME_CHARS { + return Err(DraftError::NameTooLong); + } + if self.instructions.len() > MAX_INSTRUCTIONS_BYTES { + return Err(DraftError::InstructionsTooLong); + } + if self.provider.is_none() { + return Err(DraftError::ProviderMissing); + } + let mut models: Vec = Vec::with_capacity(self.models.len()); + for model in self.models.iter().map(|model| model.trim()) { + if model.is_empty() || models.iter().any(|known| known == model) { + continue; + } + if model.len() > MAX_MODEL_BYTES { + return Err(DraftError::ModelTooLong); + } + models.push(model.to_owned()); + } + let model = self.model.trim(); + let model = if model.is_empty() { + models.first().cloned().ok_or(DraftError::ModelMissing)? + } else { + model.to_owned() + }; + if !models.contains(&model) { + models.insert(0, model.clone()); + } + self.model = model; + self.models = models; + self.servers.dedup(); + Ok(self) + } +} diff --git a/crates/hub-agents-model/src/error.rs b/crates/hub-agents-model/src/error.rs new file mode 100644 index 00000000..1bdae637 --- /dev/null +++ b/crates/hub-agents-model/src/error.rs @@ -0,0 +1,39 @@ +//! Typed failures of the agent ports and of the forms people fill in. + +use nocterm_hubs_model::HubError; + +/// Why a form cannot be saved as typed. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum DraftError { + #[error("Enter a name.")] + NameMissing, + #[error("The name is too long.")] + NameTooLong, + #[error("The instructions are too long.")] + InstructionsTooLong, + #[error("Choose a provider.")] + ProviderMissing, + #[error("Add at least one model.")] + ModelMissing, + #[error("A model name is too long.")] + ModelTooLong, + #[error("The address must start with https:// or http://.")] + BaseUrl, +} + +/// Why an agent, provider or server call failed. +#[derive(Clone, Debug, thiserror::Error)] +pub enum HubAgentsError { + /// The hub could not be reached or refused the call; the cause tells why. + #[error(transparent)] + Hub(#[from] HubError), + /// The form breaks a rule; nothing was sent. + #[error(transparent)] + Draft(#[from] DraftError), + /// The hub answered with a record this client cannot read. + #[error("the hub sent a record this client cannot read")] + Malformed, + /// A limit set by the hub's administrator is reached. + #[error("a limit of the hub is reached")] + LimitReached, +} diff --git a/crates/hub-agents-model/src/ids.rs b/crates/hub-agents-model/src/ids.rs new file mode 100644 index 00000000..de065999 --- /dev/null +++ b/crates/hub-agents-model/src/ids.rs @@ -0,0 +1,56 @@ +//! Identifiers the hub gives its agents, providers and servers. + +use std::fmt; + +/// The longest identifier this client accepts from a hub. +pub const MAX_ID_BYTES: usize = 128; + +/// An identifier a hub sent was empty, too long or not plain text. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +#[error("a hub identifier holds 1 to 128 visible ASCII characters")] +pub struct IdError; + +macro_rules! hub_id { + ($(#[$meta:meta])* $name:ident) => { + $(#[$meta])* + #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] + pub struct $name(String); + + impl $name { + /// Checks the shape only; the hub decides whether it names anything. + pub fn new(id: impl Into) -> Result { + let id = id.into(); + if id.is_empty() + || id.len() > MAX_ID_BYTES + || !id.bytes().all(|byte| byte.is_ascii_graphic()) + { + return Err(IdError); + } + Ok(Self(id)) + } + + pub fn as_str(&self) -> &str { + &self.0 + } + } + + impl fmt::Display for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } + } + }; +} + +hub_id!( + /// An agent of the account. + AgentId +); +hub_id!( + /// A model provider: one of the hub's or one of the account's own. + ProviderId +); +hub_id!( + /// A server connected to the account's agents. + ServerId +); diff --git a/crates/hub-agents-model/src/lib.rs b/crates/hub-agents-model/src/lib.rs new file mode 100644 index 00000000..59b4f020 --- /dev/null +++ b/crates/hub-agents-model/src/lib.rs @@ -0,0 +1,41 @@ +//! The agents, model providers and agent servers of a hub account, as the desktop shows and +//! edits them. +//! +//! Owns: the records of a listing ([`HubAgent`], [`Provider`], [`HubServer`], [`AgentKey`]), +//! the validated forms people fill in ([`AgentDraft`], [`ProviderDraft`], [`NewServer`]) with +//! their rules ([`DraftError`]), the server states agents report ([`ServerStatus`]), the ports +//! the hub adapter implements ([`HubAgentDirectory`], [`HubServers`], [`HubChanges`]) and +//! their typed error ([`HubAgentsError`]). +//! Does not own: the hub protocol (`nocterm-hub-io` is the adapter), the per-hub lists and their +//! refresh (`nocterm-hub-agents`), chats and their transcript (an ordinary agent chat over the +//! hub's `AgentConnector`), views. +//! Ports provided: `HubAgentDirectory`, `HubServers`, `HubChanges`. Ports used: +//! `nocterm-hubs-model` (`HubId`, `HubError`) and the `Secret` of `nocterm-session`. +//! Settings: none. Globals: none. +//! Invariants: no I/O and no protocol types; a provider key is write-only and never formatted; +//! an agent's default model is one of its models; a draft that breaks a rule never reaches a +//! port. +//! +//! With the `test-support` feature, `testing::FakeHubAgents` implements every port in memory +//! for consumers' tests. + +mod agent; +mod error; +mod ids; +mod ports; +mod provider; +mod server; +#[cfg(any(test, feature = "test-support"))] +pub mod testing; + +pub use agent::{AgentDraft, ApprovalMode, HubAgent, MAX_INSTRUCTIONS_BYTES, MAX_NAME_CHARS}; +pub use error::{DraftError, HubAgentsError}; +pub use ids::{AgentId, IdError, MAX_ID_BYTES, ProviderId, ServerId}; +pub use ports::{ + AgentsFuture, HubAgentDirectory, HubChange, HubChangeHints, HubChanges, HubServers, +}; +pub use provider::{Provider, ProviderDraft, ProviderKind, ProviderScope, ProviderTest}; +pub use server::{AgentKey, HubServer, NewServer, ServerRemoval, ServerStatus}; + +#[cfg(test)] +mod tests; diff --git a/crates/hub-agents-model/src/ports.rs b/crates/hub-agents-model/src/ports.rs new file mode 100644 index 00000000..73ace9a8 --- /dev/null +++ b/crates/hub-agents-model/src/ports.rs @@ -0,0 +1,82 @@ +//! The ports through which the desktop reaches the agents and agent servers of a hub. +//! +//! Each call names the hub; the adapter uses the account this device is signed in to there. +//! None needs the vault: agents, providers, servers and chats never wait for an unlock. + +use futures::future::BoxFuture; +use nocterm_hubs_model::HubId; + +use crate::agent::{AgentDraft, HubAgent}; +use crate::error::HubAgentsError; +use crate::ids::{AgentId, ProviderId, ServerId}; +use crate::provider::{Provider, ProviderDraft, ProviderTest}; +use crate::server::{AgentKey, HubServer, NewServer, ServerRemoval}; + +/// A call of an agents port. +pub type AgentsFuture = BoxFuture<'static, Result>; + +/// The account's agents and the providers they may use. +pub trait HubAgentDirectory: Send + Sync { + fn agents(&self, hub: &HubId) -> AgentsFuture>; + fn create_agent(&self, hub: &HubId, draft: AgentDraft) -> AgentsFuture; + fn update_agent( + &self, + hub: &HubId, + agent: &AgentId, + draft: AgentDraft, + ) -> AgentsFuture; + /// Deletes the agent and its chats. + fn delete_agent(&self, hub: &HubId, agent: &AgentId) -> AgentsFuture<()>; + + /// The hub's providers and the account's own. + fn providers(&self, hub: &HubId) -> AgentsFuture>; + /// Adds a provider of the account's own. + fn create_provider(&self, hub: &HubId, draft: ProviderDraft) -> AgentsFuture; + fn update_provider( + &self, + hub: &HubId, + provider: &ProviderId, + draft: ProviderDraft, + ) -> AgentsFuture; + fn delete_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture<()>; + /// Calls the provider from an agent worker and lists the models it offers. + fn test_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture; +} + +/// The account's agent key and the servers connected to its agents. +pub trait HubServers: Send + Sync { + /// The account's agent key; the hub creates it on first use. + fn agent_key(&self, hub: &HubId) -> AgentsFuture; + /// Replaces the agent key; every server needs its line written again. + fn rotate_agent_key(&self, hub: &HubId) -> AgentsFuture; + fn servers(&self, hub: &HubId) -> AgentsFuture>; + /// Records a server whose `authorized_keys` already holds the agent key's line. + fn add_server(&self, hub: &HubId, server: NewServer) -> AgentsFuture; + /// Signs in with the agent key and updates the status. + fn check_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture; + /// Agents stop using the server at once. The line stays on the server. + fn remove_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture; +} + +/// What changed on a hub, here or on another device. A hint means "list it again". +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HubChange { + /// Chats were created, renamed, ran or deleted. + Chats, + /// Agents, or the providers and models they use. + Agents, + /// Servers connected to agents, the agent key, or what agents last found on a server. + Servers, +} + +/// Lossy hints of what changed on a hub. +pub trait HubChangeHints: Send { + /// Waits for the next hint; `None` when the hub ended the stream (subscribe again). + fn next(&mut self) -> BoxFuture<'_, Result, HubAgentsError>>; +} + +/// Follows the chats, agents and servers of a hub, so every window shows what other devices +/// changed. +pub trait HubChanges: Send + Sync { + fn watch(&self, hub: &HubId) -> AgentsFuture>; +} diff --git a/crates/hub-agents-model/src/provider.rs b/crates/hub-agents-model/src/provider.rs new file mode 100644 index 00000000..72a67a04 --- /dev/null +++ b/crates/hub-agents-model/src/provider.rs @@ -0,0 +1,126 @@ +//! Model providers: an API endpoint with a key, the hub's or the account's own. + +use std::fmt; + +use nocterm_session::Secret; + +use crate::error::DraftError; +use crate::ids::ProviderId; + +/// The longest provider name, in characters. +const MAX_PROVIDER_NAME_CHARS: usize = 80; +/// The longest base URL, in bytes. +const MAX_BASE_URL_BYTES: usize = 2048; + +/// The API a provider speaks. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ProviderKind { + /// OpenAI-compatible chat completions (OpenAI, Gemini, OpenRouter, vLLM, Ollama, ...). + #[default] + OpenAi, + /// The Anthropic messages API. + Anthropic, +} + +impl ProviderKind { + /// The base URL a new provider of this kind starts with. + pub fn default_base_url(self) -> &'static str { + match self { + Self::OpenAi => "https://api.openai.com/v1", + Self::Anthropic => "https://api.anthropic.com", + } + } +} + +/// Who a provider belongs to. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ProviderScope { + /// The account's own; only its owner sees and uses it. + #[default] + Account, + /// The hub's; administrators manage it and every account may use it. + Hub, +} + +/// A provider the account may use, as the hub lists it. Its key is never returned. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Provider { + pub id: ProviderId, + pub scope: ProviderScope, + pub kind: ProviderKind, + pub name: String, + pub base_url: String, + /// The models agents may use through it. + pub models: Vec, + pub has_key: bool, + /// The caller may change or delete it. + pub editable: bool, +} + +/// What a person typed to add or change a provider of their own. +#[derive(Clone, Default, PartialEq)] +pub struct ProviderDraft { + pub kind: ProviderKind, + pub name: String, + pub base_url: String, + pub models: Vec, + /// Write-only. `None` on a change keeps the stored key. + pub api_key: Option, +} + +impl fmt::Debug for ProviderDraft { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("ProviderDraft") + .field("kind", &self.kind) + .field("name", &self.name) + .field("base_url", &self.base_url) + .field("models", &self.models) + .field("api_key", &self.api_key.as_ref().map(|_| "")) + .finish() + } +} + +impl ProviderDraft { + /// The draft with trimmed text and models, or the first rule it breaks. + pub fn validate(mut self) -> Result { + self.name = self.name.trim().to_owned(); + if self.name.is_empty() { + return Err(DraftError::NameMissing); + } + if self.name.chars().count() > MAX_PROVIDER_NAME_CHARS { + return Err(DraftError::NameTooLong); + } + self.base_url = self.base_url.trim().trim_end_matches('/').to_owned(); + if !self.base_url.starts_with("https://") && !self.base_url.starts_with("http://") { + return Err(DraftError::BaseUrl); + } + if self.base_url.len() > MAX_BASE_URL_BYTES { + return Err(DraftError::BaseUrl); + } + let mut models: Vec = Vec::new(); + for model in self.models.iter().map(|model| model.trim()) { + if !model.is_empty() && !models.iter().any(|known| known == model) { + models.push(model.to_owned()); + } + } + self.models = models; + if self + .api_key + .as_ref() + .is_some_and(|key| key.expose().trim().is_empty()) + { + self.api_key = None; + } + Ok(self) + } +} + +/// What testing a provider showed. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ProviderTest { + pub ok: bool, + /// What went wrong, readable by a person; empty when it worked. + pub message: String, + /// The models the endpoint lists, when it lists any. + pub models: Vec, +} diff --git a/crates/hub-agents-model/src/server.rs b/crates/hub-agents-model/src/server.rs new file mode 100644 index 00000000..157ce657 --- /dev/null +++ b/crates/hub-agents-model/src/server.rs @@ -0,0 +1,107 @@ +//! The account's agent key and the servers it is written to. + +use crate::ids::ServerId; + +/// The account's agent key: one Ed25519 key per account, without expiry. A server is +/// connected to agents when its `authorized_keys` holds [`AgentKey::authorized_line`]. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AgentKey { + /// OpenSSH public key text. + pub public_key: String, + /// The whole `authorized_keys` line with its restrictions. + pub authorized_line: String, + /// `SHA256:…`. + pub fingerprint: String, +} + +impl AgentKey { + /// Whether `line` of an `authorized_keys` file is this key's line, whatever options or + /// comment it carries. + pub fn is_line_of(&self, line: &str) -> bool { + let key = key_blob(&self.public_key); + !key.is_empty() && line.split_whitespace().any(|part| part == key) + } +} + +/// The base64 blob of an OpenSSH public key text (`ssh-ed25519 AAAA… comment`). +fn key_blob(public_key: &str) -> &str { + public_key.split_whitespace().nth(1).unwrap_or_default() +} + +/// What agents last found when they signed in to a server. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)] +pub enum ServerStatus { + /// Not checked yet. + #[default] + Unchecked, + /// The agent key signs in. + Ready, + /// The server refuses the agent key. + NoAccess, + /// The server presents another host key than the recorded one; agents do not connect. + HostKeyChanged, + /// The server could not be reached. + Unreachable, + /// The agent key was replaced after its line was written. + KeyRotated, +} + +impl ServerStatus { + /// Short text for the server's row. + pub fn label(self) -> &'static str { + match self { + Self::Unchecked => "Not checked", + Self::Ready => "Agents ✓", + Self::NoAccess => "No access — reconnect", + Self::HostKeyChanged => "Host key changed", + Self::Unreachable => "Unreachable", + Self::KeyRotated => "Reconnect needed", + } + } + + /// Whether agents can sign in now. + pub fn is_ready(self) -> bool { + self == Self::Ready + } +} + +/// A server connected to the account's agents. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubServer { + pub id: ServerId, + pub name: String, + pub host: String, + pub port: u16, + pub user: String, + pub host_key_fingerprint: String, + pub status: ServerStatus, + /// Why the status is not ready, readable by a person; empty otherwise. + pub status_message: String, +} + +impl HubServer { + /// Whether this record is the server `host:port` with `user`. + pub fn is(&self, host: &str, port: u16, user: &str) -> bool { + self.host.eq_ignore_ascii_case(host) && self.port == port && self.user == user + } +} + +/// A server whose `authorized_keys` already holds the agent key's line. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NewServer { + pub name: String, + pub host: String, + pub port: u16, + pub user: String, + /// The OpenSSH public host key this device recorded for the server. + pub host_key: String, +} + +/// What removing a server came to. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct ServerRemoval { + /// The agent key's line was removed from the server too. + pub line_removed: bool, + /// Why the line stayed, readable by a person. + pub message: String, +} diff --git a/crates/hub-agents-model/src/testing.rs b/crates/hub-agents-model/src/testing.rs new file mode 100644 index 00000000..6dcf484f --- /dev/null +++ b/crates/hub-agents-model/src/testing.rs @@ -0,0 +1,365 @@ +//! An in-memory hub for consumers' tests: agents, providers, the agent key and servers. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::channel::mpsc; +use futures::future::BoxFuture; +use futures::{FutureExt as _, StreamExt as _}; +use nocterm_hubs_model::{HubError, HubId}; + +use crate::{ + AgentDraft, AgentId, AgentKey, AgentsFuture, HubAgent, HubAgentDirectory, HubAgentsError, + HubChange, HubChangeHints, HubChanges, HubServer, HubServers, NewServer, Provider, + ProviderDraft, ProviderId, ProviderScope, ProviderTest, ServerId, ServerRemoval, ServerStatus, +}; + +#[derive(Default)] +struct Account { + agents: Vec, + providers: Vec, + servers: Vec, + key: u32, + next: u32, +} + +#[derive(Default)] +struct State { + accounts: HashMap, + offline: bool, + hints: Vec>, + calls: Vec, +} + +/// Implements every agents port in memory. Clones share one state. +#[derive(Clone, Default)] +pub struct FakeHubAgents(Arc>); + +fn lock(state: &Mutex) -> MutexGuard<'_, State> { + state.lock().unwrap_or_else(PoisonError::into_inner) +} + +fn ready(value: Result) -> AgentsFuture { + Box::pin(async move { value }) +} + +impl FakeHubAgents { + pub fn new() -> Self { + Self::default() + } + + /// Adds a provider of the hub with `models`. + pub fn with_provider(self, hub: &HubId, name: &str, models: &[&str]) -> Self { + { + let mut state = lock(&self.0); + let account = state.accounts.entry(hub.clone()).or_default(); + account.next += 1; + let id = ProviderId::new(format!("provider-{}", account.next)).expect("id"); + account.providers.push(Provider { + id, + scope: ProviderScope::Hub, + kind: crate::ProviderKind::OpenAi, + name: name.to_owned(), + base_url: "https://models.example/v1".into(), + models: models.iter().map(|model| (*model).to_owned()).collect(), + has_key: true, + editable: false, + }); + } + self + } + + /// Adds an agent with `model` through the first provider of `hub`. + pub fn with_agent(self, hub: &HubId, name: &str, model: &str) -> Self { + let provider = lock(&self.0) + .accounts + .get(hub) + .and_then(|account| account.providers.first()) + .map(|provider| provider.id.clone()) + .unwrap_or_else(|| ProviderId::new("provider-0").expect("id")); + let draft = AgentDraft { + name: name.to_owned(), + provider: Some(provider), + models: vec![model.to_owned()], + ..AgentDraft::default() + }; + futures::executor::block_on(self.create_agent(hub, draft)).expect("agent"); + self + } + + /// Makes every call fail as if the hub could not be reached. + pub fn set_offline(&self, offline: bool) { + lock(&self.0).offline = offline; + } + + /// Sets the status agents report for the server `host`. + pub fn set_status(&self, hub: &HubId, host: &str, status: ServerStatus) { + let mut state = lock(&self.0); + if let Some(account) = state.accounts.get_mut(hub) { + for server in account.servers.iter_mut().filter(|s| s.host == host) { + server.status = status; + } + } + } + + /// Tells every watcher that `change` happened, as another device would. + pub fn changed(&self, change: HubChange) { + lock(&self.0) + .hints + .retain(|hint| hint.unbounded_send(change).is_ok()); + } + + /// The names of the calls made so far, oldest first. + pub fn calls(&self) -> Vec { + lock(&self.0).calls.clone() + } + + fn with( + &self, + call: &str, + hub: &HubId, + change: impl FnOnce(&mut Account) -> Result, + ) -> AgentsFuture { + let mut state = lock(&self.0); + state.calls.push(call.to_owned()); + if state.offline { + return ready(Err(HubError::NotSignedIn.into())); + } + let account = state.accounts.entry(hub.clone()).or_default(); + ready(change(account)) + } +} + +fn next_id(account: &mut Account, prefix: &str) -> String { + account.next += 1; + format!("{prefix}-{}", account.next) +} + +fn agent_of(id: AgentId, draft: AgentDraft) -> HubAgent { + HubAgent { + id, + name: draft.name, + instructions: draft.instructions, + provider: draft.provider.expect("validated"), + model: draft.model, + models: draft.models, + approval: draft.approval, + servers: draft.servers, + } +} + +fn provider_of(id: ProviderId, draft: ProviderDraft) -> Provider { + Provider { + id, + scope: ProviderScope::Account, + kind: draft.kind, + name: draft.name, + base_url: draft.base_url, + models: draft.models, + has_key: draft.api_key.is_some(), + editable: true, + } +} + +fn agent_key(generation: u32) -> AgentKey { + let public_key = format!("ssh-ed25519 AAAAkey{generation} nocterm-agents"); + AgentKey { + authorized_line: format!( + "no-port-forwarding,no-agent-forwarding,no-X11-forwarding {public_key}" + ), + public_key, + fingerprint: format!("SHA256:key{generation}"), + } +} + +impl HubAgentDirectory for FakeHubAgents { + fn agents(&self, hub: &HubId) -> AgentsFuture> { + self.with("agents", hub, |account| Ok(account.agents.clone())) + } + + fn create_agent(&self, hub: &HubId, draft: AgentDraft) -> AgentsFuture { + self.with("create_agent", hub, |account| { + let draft = draft.validate()?; + let id = AgentId::new(next_id(account, "agent")).expect("id"); + let agent = agent_of(id, draft); + account.agents.push(agent.clone()); + Ok(agent) + }) + } + + fn update_agent( + &self, + hub: &HubId, + agent: &AgentId, + draft: AgentDraft, + ) -> AgentsFuture { + let agent = agent.clone(); + self.with("update_agent", hub, move |account| { + let draft = draft.validate()?; + let stored = account + .agents + .iter_mut() + .find(|stored| stored.id == agent) + .ok_or(HubAgentsError::Hub(HubError::NotFound))?; + *stored = agent_of(agent, draft); + Ok(stored.clone()) + }) + } + + fn delete_agent(&self, hub: &HubId, agent: &AgentId) -> AgentsFuture<()> { + let agent = agent.clone(); + self.with("delete_agent", hub, move |account| { + account.agents.retain(|stored| stored.id != agent); + Ok(()) + }) + } + + fn providers(&self, hub: &HubId) -> AgentsFuture> { + self.with("providers", hub, |account| Ok(account.providers.clone())) + } + + fn create_provider(&self, hub: &HubId, draft: ProviderDraft) -> AgentsFuture { + self.with("create_provider", hub, |account| { + let draft = draft.validate()?; + let id = ProviderId::new(next_id(account, "provider")).expect("id"); + let provider = provider_of(id, draft); + account.providers.push(provider.clone()); + Ok(provider) + }) + } + + fn update_provider( + &self, + hub: &HubId, + provider: &ProviderId, + draft: ProviderDraft, + ) -> AgentsFuture { + let provider = provider.clone(); + self.with("update_provider", hub, move |account| { + let draft = draft.validate()?; + let stored = account + .providers + .iter_mut() + .find(|stored| stored.id == provider && stored.editable) + .ok_or(HubAgentsError::Hub(HubError::NotFound))?; + let has_key = stored.has_key || draft.api_key.is_some(); + *stored = Provider { + has_key, + ..provider_of(provider, draft) + }; + Ok(stored.clone()) + }) + } + + fn delete_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture<()> { + let provider = provider.clone(); + self.with("delete_provider", hub, move |account| { + account + .providers + .retain(|stored| stored.id != provider || !stored.editable); + Ok(()) + }) + } + + fn test_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture { + let provider = provider.clone(); + self.with("test_provider", hub, move |account| { + let stored = account + .providers + .iter() + .find(|stored| stored.id == provider) + .ok_or(HubAgentsError::Hub(HubError::NotFound))?; + Ok(ProviderTest { + ok: stored.has_key, + message: if stored.has_key { + String::new() + } else { + "The provider has no key.".into() + }, + models: stored.models.clone(), + }) + }) + } +} + +impl HubServers for FakeHubAgents { + fn agent_key(&self, hub: &HubId) -> AgentsFuture { + self.with("agent_key", hub, |account| Ok(agent_key(account.key))) + } + + fn rotate_agent_key(&self, hub: &HubId) -> AgentsFuture { + self.with("rotate_agent_key", hub, |account| { + account.key += 1; + for server in &mut account.servers { + server.status = ServerStatus::KeyRotated; + } + Ok(agent_key(account.key)) + }) + } + + fn servers(&self, hub: &HubId) -> AgentsFuture> { + self.with("servers", hub, |account| Ok(account.servers.clone())) + } + + fn add_server(&self, hub: &HubId, server: NewServer) -> AgentsFuture { + self.with("add_server", hub, move |account| { + let id = ServerId::new(next_id(account, "server")).expect("id"); + let record = HubServer { + id, + name: server.name, + host: server.host, + port: server.port, + user: server.user, + host_key_fingerprint: server.host_key, + status: ServerStatus::Ready, + status_message: String::new(), + }; + account.servers.push(record.clone()); + Ok(record) + }) + } + + fn check_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let server = server.clone(); + self.with("check_server", hub, move |account| { + account + .servers + .iter() + .find(|stored| stored.id == server) + .cloned() + .ok_or(HubAgentsError::Hub(HubError::NotFound)) + }) + } + + fn remove_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let server = server.clone(); + self.with("remove_server", hub, move |account| { + account.servers.retain(|stored| stored.id != server); + Ok(ServerRemoval { + line_removed: false, + message: String::new(), + }) + }) + } +} + +struct Hints(mpsc::UnboundedReceiver); + +impl HubChangeHints for Hints { + fn next(&mut self) -> BoxFuture<'_, Result, HubAgentsError>> { + self.0.next().map(Ok).boxed() + } +} + +impl HubChanges for FakeHubAgents { + fn watch(&self, hub: &HubId) -> AgentsFuture> { + let (sender, receiver) = mpsc::unbounded(); + let mut state = lock(&self.0); + state.calls.push(format!("watch {hub}")); + if state.offline { + return ready(Err(HubError::NotSignedIn.into())); + } + state.hints.push(sender); + ready(Ok(Box::new(Hints(receiver)) as Box)) + } +} diff --git a/crates/hub-agents-model/src/tests.rs b/crates/hub-agents-model/src/tests.rs new file mode 100644 index 00000000..8415227b --- /dev/null +++ b/crates/hub-agents-model/src/tests.rs @@ -0,0 +1,104 @@ +use nocterm_session::Secret; + +use crate::{ + AgentDraft, AgentKey, DraftError, ProviderDraft, ProviderId, ProviderKind, ServerStatus, +}; + +fn provider() -> Option { + Some(ProviderId::new("p1").unwrap()) +} + +#[test] +fn an_agent_needs_a_name_a_provider_and_a_model() { + let draft = |name: &str, provider, models: &[&str]| AgentDraft { + name: name.into(), + provider, + models: models.iter().map(|m| (*m).to_owned()).collect(), + ..AgentDraft::default() + }; + assert_eq!( + draft(" ", provider(), &["m"]).validate(), + Err(DraftError::NameMissing) + ); + assert_eq!( + draft("ops", None, &["m"]).validate(), + Err(DraftError::ProviderMissing) + ); + assert_eq!( + draft("ops", provider(), &[" "]).validate(), + Err(DraftError::ModelMissing) + ); + let valid = draft(" ops ", provider(), &["a", "b", "a"]) + .validate() + .unwrap(); + assert_eq!(valid.name, "ops"); + assert_eq!(valid.model, "a"); + assert_eq!(valid.models, ["a", "b"]); +} + +#[test] +fn the_default_model_is_always_one_of_the_models() { + let valid = AgentDraft { + name: "ops".into(), + provider: provider(), + model: "big".into(), + models: vec!["small".into()], + ..AgentDraft::default() + } + .validate() + .unwrap(); + assert_eq!(valid.model, "big"); + assert_eq!(valid.models, ["big", "small"]); +} + +#[test] +fn a_provider_needs_a_web_address_and_keeps_its_key_out_of_debug() { + let draft = ProviderDraft { + kind: ProviderKind::Anthropic, + name: "Mine".into(), + base_url: "ftp://x".into(), + models: vec![], + api_key: Some(Secret::new("sk-secret")), + }; + assert_eq!(draft.clone().validate(), Err(DraftError::BaseUrl)); + let valid = ProviderDraft { + base_url: "https://api.anthropic.com/".into(), + ..draft + } + .validate() + .unwrap(); + assert_eq!(valid.base_url, "https://api.anthropic.com"); + assert!(!format!("{valid:?}").contains("sk-secret")); +} + +#[test] +fn an_empty_key_on_a_change_keeps_the_stored_one() { + let valid = ProviderDraft { + name: "Mine".into(), + base_url: "https://x".into(), + api_key: Some(Secret::new(" ")), + ..ProviderDraft::default() + } + .validate() + .unwrap(); + assert!(valid.api_key.is_none()); +} + +#[test] +fn the_agent_key_line_is_recognized_with_any_options_or_comment() { + let key = AgentKey { + public_key: "ssh-ed25519 AAAAblob nocterm".into(), + authorized_line: "no-pty ssh-ed25519 AAAAblob nocterm".into(), + fingerprint: "SHA256:x".into(), + }; + assert!(key.is_line_of("restrict ssh-ed25519 AAAAblob other")); + assert!(!key.is_line_of("ssh-ed25519 AAAAother nocterm")); + assert!(!key.is_line_of("")); +} + +#[test] +fn only_a_ready_server_is_usable() { + assert!(ServerStatus::Ready.is_ready()); + assert!(!ServerStatus::KeyRotated.is_ready()); + assert_eq!(ServerStatus::Ready.label(), "Agents ✓"); +} diff --git a/crates/hub-agents/Cargo.toml b/crates/hub-agents/Cargo.toml new file mode 100644 index 00000000..682eaa09 --- /dev/null +++ b/crates/hub-agents/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "nocterm-hub-agents" +description = "The agents, agent servers and chat lists of the signed-in hubs, kept per hub and refreshed on change." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +nocterm-ai.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-hubs-model.workspace = true +nocterm-ssh-keys.workspace = true + +futures.workspace = true +gpui.workspace = true +thiserror.workspace = true +tracing.workspace = true + +[dev-dependencies] +nocterm-hub-agents-model = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } +gpui = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hub-agents/src/access.rs b/crates/hub-agents/src/access.rs new file mode 100644 index 00000000..e76bc3f7 --- /dev/null +++ b/crates/hub-agents/src/access.rs @@ -0,0 +1,101 @@ +//! Agent access of a saved server: the account's agent key line in the server's +//! `authorized_keys`, written through the session the user opened, and the hub's record of the +//! server (ADR 0148). + +use std::sync::Arc; + +use nocterm_hub_agents_model::{ + HubAgentsError, HubServer, HubServers, NewServer, ServerId, ServerRemoval, +}; +use nocterm_hubs_model::HubId; +use nocterm_ssh_keys::authorized_keys::{AgentHosts, HostAccessError, with_line, without_lines}; + +/// The saved connection whose agent access changes. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct AgentTarget { + /// The id of the saved connection, as its session announced it. + pub connection: String, + /// The name the hub shows for the server. + pub name: String, + pub host: String, + pub port: u16, + pub user: String, +} + +/// Why agent access could not be changed. +#[derive(Debug, thiserror::Error)] +pub enum AgentAccessError { + /// No session to the server is open, so its `authorized_keys` cannot be written. + #[error("no session to the server is open")] + NotConnected, + /// This device recorded no host key for the server, so the hub could not check it. + #[error("no host key is recorded for the server")] + UnknownHostKey, + /// The server's `authorized_keys` could not be read or replaced. + #[error("the server's authorized_keys could not be edited")] + Host(#[source] HostAccessError), + /// The hub refused or could not be reached. + #[error(transparent)] + Hub(#[from] HubAgentsError), +} + +impl From for AgentAccessError { + fn from(error: HostAccessError) -> Self { + match error { + HostAccessError::NotConnected => Self::NotConnected, + error => Self::Host(error), + } + } +} + +/// Writes the agent key's line to the server, then records the server on the hub with the host +/// key this device recorded for it. +pub(crate) async fn grant( + servers: Arc, + hosts: Arc, + hub: HubId, + target: AgentTarget, +) -> Result { + let access = hosts.access(&target.connection)?; + if access.identity.host_key.is_empty() { + return Err(AgentAccessError::UnknownHostKey); + } + let key = servers.agent_key(&hub).await?; + let current = access.file.read().await?; + let updated = with_line(¤t, &key.authorized_line); + if updated != current { + access.file.replace(current, updated).await?; + } + let server = NewServer { + name: target.name, + host: target.host, + port: target.port, + user: access.identity.user, + host_key: access.identity.host_key, + }; + Ok(servers.add_server(&hub, server).await?) +} + +/// Removes the agent key's line while a session to the server is open, then the hub's record; +/// without a session the hub is asked to remove the line itself. +pub(crate) async fn withdraw( + servers: Arc, + hosts: Arc, + hub: HubId, + connection: String, + server: ServerId, +) -> Result { + match hosts.access(&connection) { + Ok(access) => { + let key = servers.agent_key(&hub).await?; + let current = access.file.read().await?; + let updated = without_lines(¤t, |line| key.is_line_of(line)); + if updated != current { + access.file.replace(current, updated).await?; + } + } + Err(HostAccessError::NotConnected) => {} + Err(error) => return Err(AgentAccessError::Host(error)), + } + Ok(servers.remove_server(&hub, &server).await?) +} diff --git a/crates/hub-agents/src/installed.rs b/crates/hub-agents/src/installed.rs new file mode 100644 index 00000000..b3fc5d58 --- /dev/null +++ b/crates/hub-agents/src/installed.rs @@ -0,0 +1,38 @@ +//! The ports the service needs and the handle to the installed entity. + +use std::sync::Arc; + +use gpui::{App, AppContext as _, Entity}; +use nocterm_hub_agents_model::{HubAgentDirectory, HubChanges, HubServers}; +use nocterm_ssh_keys::authorized_keys::AgentHosts; + +use crate::service::HubAgents; + +/// The agents ports, supplied by the composition root. +#[derive(Clone)] +pub struct Deps { + pub directory: Arc, + pub servers: Arc, + pub changes: Arc, + /// The sessions of saved connections, through which Agent access edits `authorized_keys`. + pub hosts: Arc, +} + +/// The entity shared by every window, kept alive by whoever composes the application. +#[derive(Clone)] +pub struct Installed { + agents: Entity, +} + +impl Installed { + pub fn agents(&self) -> &Entity { + &self.agents + } +} + +/// Creates the service. It tracks no hub until [`HubAgents::track`] is called for it. +pub fn install(deps: Deps, cx: &mut App) -> Installed { + Installed { + agents: cx.new(|_| HubAgents::new(deps)), + } +} diff --git a/crates/hub-agents/src/lib.rs b/crates/hub-agents/src/lib.rs new file mode 100644 index 00000000..eea2f70c --- /dev/null +++ b/crates/hub-agents/src/lib.rs @@ -0,0 +1,32 @@ +//! The agents, agent servers and chat lists of the signed-in hubs, shared by every window. +//! +//! Owns: one entry per tracked hub with its agents and agent servers (listed when the hub is +//! tracked and again on [`HubAgents::refresh`]), the follow of its chat list (the hub's change +//! hints, re-subscribed with growing delays), and the launches of its agents as ordinary +//! agents of `nocterm-ai`, so a hub window's new-chat menu offers them beside the local ones, +//! and Agent access of a saved server ([`AgentTarget`]): whether a session to it is open, the +//! agent key's line written to or removed from its `authorized_keys` through that session, and +//! the hub's record of it. +//! Does not own: the hub protocol (the injected ports are implemented by `nocterm-hub-io`), +//! the rules of a draft (`nocterm-hub-agents-model`), the chats themselves (an agent chat over +//! the hub connector), views. +//! Ports used: `HubAgentDirectory`, `HubServers` and `HubChanges` of +//! `nocterm-hub-agents-model` and `AgentHosts` of `nocterm-ssh-keys`, passed in [`Deps`]. Provides: [`Installed`] with the +//! [`HubAgents`] entity and its [`HubAgentsEvent`]s. +//! Settings: none. Globals: none. +//! Invariants: a hub that is no longer tracked forgets its lists and stops its follow; a +//! failed listing keeps the last lists shown and is tried again with growing delays, and a +//! re-subscribed follow reads the lists and the chats again (hints sent meanwhile are lost); every change of a hub's lists emits +//! [`HubAgentsEvent::Changed`] once, every hint of its chat list +//! [`HubAgentsEvent::ChatsChanged`], and a hint of its agents or servers lists them again; a server is recorded on the hub only after the agent +//! key's line was written to it, with the host key this device recorded. + +mod access; +mod installed; +mod service; +#[cfg(test)] +mod tests; + +pub use access::{AgentAccessError, AgentTarget}; +pub use installed::{Deps, Installed, install}; +pub use service::{HubAgents, HubAgentsEvent}; diff --git a/crates/hub-agents/src/service.rs b/crates/hub-agents/src/service.rs new file mode 100644 index 00000000..cb2a6a03 --- /dev/null +++ b/crates/hub-agents/src/service.rs @@ -0,0 +1,301 @@ +//! The entity every window reads the agents and agent servers of a hub from. + +use std::collections::HashMap; +use std::sync::Arc; +use std::time::Duration; + +use gpui::{Context, EventEmitter, Task}; +use nocterm_ai::agents::AgentLaunch; +use nocterm_hub_agents_model::{ + HubAgent, HubAgentDirectory, HubAgentsError, HubChange, HubChanges, HubServer, HubServers, + ServerId, ServerRemoval, +}; +use nocterm_hubs_model::HubId; + +use crate::access::{self, AgentAccessError, AgentTarget}; +use crate::installed::Deps; + +const FIRST_RETRY: Duration = Duration::from_secs(2); +const MAX_RETRY: Duration = Duration::from_secs(60); + +/// What changed for the views. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum HubAgentsEvent { + /// The agents or agent servers of the hub changed. + Changed(HubId), + /// The hub's chats changed: a history that lists them reads them again. + ChatsChanged(HubId), +} + +/// The lists of one tracked hub. +#[derive(Default)] +struct Entry { + agents: Vec, + servers: Vec, + /// Why the last listing failed, while it does. + problem: Option, + /// How long the next listing after a failed one waits; none while listings succeed. + retry: Option, + _listing: Option>, + _follow: Option>, +} + +/// The agents and agent servers of every tracked hub. +pub struct HubAgents { + deps: Deps, + hubs: HashMap, +} + +impl EventEmitter for HubAgents {} + +impl HubAgents { + pub(crate) fn new(deps: Deps) -> Self { + Self { + deps, + hubs: HashMap::new(), + } + } + + /// Starts keeping the lists of `hub`. Tracking twice is harmless. + pub fn track(&mut self, hub: &HubId, cx: &mut Context) { + if self.hubs.contains_key(hub) { + return; + } + let follow = follow_changes(self.deps.changes.clone(), hub.clone(), cx); + self.hubs.insert( + hub.clone(), + Entry { + _follow: Some(follow), + ..Entry::default() + }, + ); + self.refresh(hub, cx); + } + + /// Forgets `hub`: its lists are dropped and its chats are no longer followed. + pub fn untrack(&mut self, hub: &HubId, cx: &mut Context) { + if self.hubs.remove(hub).is_some() { + cx.emit(HubAgentsEvent::Changed(hub.clone())); + cx.notify(); + } + } + + /// Lists the agents and agent servers of `hub` again. + pub fn refresh(&mut self, hub: &HubId, cx: &mut Context) { + let Some(entry) = self.hubs.get_mut(hub) else { + return; + }; + let agents = self.deps.directory.agents(hub); + let servers = self.deps.servers.servers(hub); + let hub_id = hub.clone(); + entry._listing = Some(cx.spawn(async move |this, cx| { + let (agents, servers) = futures::join!(agents, servers); + this.update(cx, |this, cx| this.listed(&hub_id, agents, servers, cx)) + .ok(); + })); + } + + fn listed( + &mut self, + hub: &HubId, + agents: Result, HubAgentsError>, + servers: Result, HubAgentsError>, + cx: &mut Context, + ) { + let Some(entry) = self.hubs.get_mut(hub) else { + return; + }; + let mut problem = None; + match agents { + Ok(agents) => entry.agents = agents, + Err(error) => problem = Some(error), + } + match servers { + Ok(servers) => entry.servers = servers, + Err(error) => problem = problem.or(Some(error)), + } + if let Some(error) = &problem { + tracing::debug!(%error, hub = %hub, "the hub's agents could not be listed"); + // A hub that is still connecting answers soon: the lists are read again then. + let delay = entry + .retry + .map_or(FIRST_RETRY, |delay| (delay * 2).min(MAX_RETRY)); + entry.retry = Some(delay); + let hub_id = hub.clone(); + entry._listing = Some(cx.spawn(async move |this, cx| { + cx.background_executor().timer(delay).await; + this.update(cx, |this, cx| this.refresh(&hub_id, cx)).ok(); + })); + } else { + entry.retry = None; + } + entry.problem = problem; + cx.emit(HubAgentsEvent::Changed(hub.clone())); + cx.notify(); + } + + /// The agents of `hub`, as last listed. + pub fn agents(&self, hub: &HubId) -> &[HubAgent] { + self.hubs.get(hub).map_or(&[], |entry| &entry.agents) + } + + /// The servers connected to the agents of `hub`, as last listed. + pub fn servers(&self, hub: &HubId) -> &[HubServer] { + self.hubs.get(hub).map_or(&[], |entry| &entry.servers) + } + + /// Why the last listing of `hub` failed, while it does. + pub fn problem(&self, hub: &HubId) -> Option<&HubAgentsError> { + self.hubs.get(hub)?.problem.as_ref() + } + + /// The record of `target` among the agent servers of `hub`, as last listed. + pub fn server_of(&self, hub: &HubId, target: &AgentTarget) -> Option<&HubServer> { + self.servers(hub) + .iter() + .find(|server| server.is(&target.host, target.port, &target.user)) + } + + /// Whether a session to the saved connection `connection` is open, so the agent key's line + /// can be written through it now. + pub fn session_open(&self, connection: &str) -> bool { + self.deps.hosts.access(connection).is_ok() + } + + /// Lets the agents of `hub` work on `target`: the agent key's line is written through the + /// session the user opened and the server is recorded on the hub, then in the list. + pub fn grant_access( + &mut self, + hub: &HubId, + target: AgentTarget, + cx: &mut Context, + ) -> Task> { + let work = cx.background_executor().spawn(access::grant( + self.deps.servers.clone(), + self.deps.hosts.clone(), + hub.clone(), + target, + )); + let hub = hub.clone(); + cx.spawn(async move |this, cx| { + let server = work.await?; + this.update(cx, |this, cx| this.recorded(&hub, Some(server), None, cx)) + .ok(); + Ok(()) + }) + } + + /// Stops the agents of `hub` working on `target`. `None` when the hub has no record of it. + pub fn withdraw_access( + &mut self, + hub: &HubId, + target: &AgentTarget, + cx: &mut Context, + ) -> Task, AgentAccessError>> { + let Some(server) = self.server_of(hub, target) else { + return Task::ready(Ok(None)); + }; + let work = cx.background_executor().spawn(access::withdraw( + self.deps.servers.clone(), + self.deps.hosts.clone(), + hub.clone(), + target.connection.clone(), + server.id.clone(), + )); + let (hub, id) = (hub.clone(), server.id.clone()); + cx.spawn(async move |this, cx| { + let removal = work.await?; + this.update(cx, |this, cx| this.recorded(&hub, None, Some(&id), cx)) + .ok(); + Ok(Some(removal)) + }) + } + + /// Applies what the hub answered to a change of its servers: `added` replaces a record of + /// the same id, `removed` leaves the list. + fn recorded( + &mut self, + hub: &HubId, + added: Option, + removed: Option<&ServerId>, + cx: &mut Context, + ) { + let Some(entry) = self.hubs.get_mut(hub) else { + return; + }; + let gone = |server: &HubServer| { + added.as_ref().is_some_and(|added| added.id == server.id) || removed == Some(&server.id) + }; + entry.servers.retain(|server| !gone(server)); + entry.servers.extend(added); + cx.emit(HubAgentsEvent::Changed(hub.clone())); + cx.notify(); + } + + /// The agents of every tracked hub as agents a chat can start. + pub fn launches(&self) -> Vec { + self.hubs + .iter() + .flat_map(|(hub, entry)| { + entry.agents.iter().map(|agent| { + AgentLaunch::hub_agent(hub.clone(), agent.id.to_string(), agent.name.clone()) + }) + }) + .collect() + } + + /// The agents port, for the forms that edit agents and providers. + pub fn directory(&self) -> &Arc { + &self.deps.directory + } + + /// The servers port, for the agent key and the server rows. + pub fn server_port(&self) -> &Arc { + &self.deps.servers + } +} + +/// Follows what changes on `hub`, re-subscribing with growing delays, until it is untracked: +/// changed chats are announced, changed agents or servers are listed again. Hints sent while +/// no subscription was open are lost, so after a re-subscription everything is read again. +fn follow_changes( + changes: Arc, + hub: HubId, + cx: &mut Context, +) -> Task<()> { + cx.spawn(async move |this, cx| { + let mut delay = FIRST_RETRY; + let mut subscribed = false; + loop { + if let Ok(mut hints) = changes.watch(&hub).await { + delay = FIRST_RETRY; + if std::mem::replace(&mut subscribed, true) { + let alive = this.update(cx, |this, cx| { + this.refresh(&hub, cx); + cx.emit(HubAgentsEvent::ChatsChanged(hub.clone())); + }); + if alive.is_err() { + return; + } + } + while let Ok(Some(change)) = hints.next().await { + let alive = this.update(cx, |this, cx| match change { + HubChange::Chats => cx.emit(HubAgentsEvent::ChatsChanged(hub.clone())), + HubChange::Agents | HubChange::Servers => this.refresh(&hub, cx), + }); + if alive.is_err() { + return; + } + } + } + cx.background_executor().timer(delay).await; + delay = (delay * 2).min(MAX_RETRY); + if this + .read_with(cx, |this, _| !this.hubs.contains_key(&hub)) + .unwrap_or(true) + { + return; + } + } + }) +} diff --git a/crates/hub-agents/src/tests.rs b/crates/hub-agents/src/tests.rs new file mode 100644 index 00000000..15d06c5b --- /dev/null +++ b/crates/hub-agents/src/tests.rs @@ -0,0 +1,222 @@ +use std::sync::Arc; + +use gpui::TestAppContext; +use nocterm_hub_agents_model::HubChange; +use nocterm_hub_agents_model::testing::FakeHubAgents; +use nocterm_hubs_model::{HubAddress, HubId}; + +use crate::{AgentAccessError, AgentTarget, Deps, HubAgentsEvent, install}; +use nocterm_ssh_keys::testing::{FakeAgentHosts, FakeAuthorizedKeys}; + +fn hub() -> HubId { + HubId::from(HubAddress::parse("hub.example:7443").unwrap()) +} + +fn deps(fake: &FakeHubAgents) -> Deps { + deps_over(fake, &FakeAgentHosts::default()) +} + +fn deps_over(fake: &FakeHubAgents, hosts: &FakeAgentHosts) -> Deps { + Deps { + directory: Arc::new(fake.clone()), + servers: Arc::new(fake.clone()), + changes: Arc::new(fake.clone()), + hosts: Arc::new(hosts.clone()), + } +} + +fn web() -> AgentTarget { + AgentTarget { + connection: "web".into(), + name: "Web".into(), + host: "web.example".into(), + port: 22, + user: "deploy".into(), + } +} + +#[gpui::test] +fn a_tracked_hub_offers_its_agents_as_agents_a_chat_can_start(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new() + .with_provider(&hub(), "Models", &["m1"]) + .with_agent(&hub(), "Ops", "m1"); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert_eq!(agents.agents(&hub()).len(), 1); + let launches = agents.launches(); + assert_eq!(launches.len(), 1); + assert_eq!(launches[0].name, "Ops"); + assert_eq!(launches[0].hub.as_ref().unwrap().hub, hub()); + }); + agents.update(cx, |agents, cx| agents.untrack(&hub(), cx)); + agents.read_with(cx, |agents, _| assert!(agents.launches().is_empty())); +} + +#[gpui::test] +fn a_change_of_the_hubs_chats_is_told_to_the_views(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new(); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + let seen = Arc::new(std::sync::Mutex::new(Vec::new())); + let _subscription = cx.update({ + let seen = seen.clone(); + |cx| { + cx.subscribe(&agents, move |_, event: &HubAgentsEvent, _| { + seen.lock().unwrap().push(event.clone()); + }) + } + }); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + fake.changed(HubChange::Chats); + cx.run_until_parked(); + assert!( + seen.lock() + .unwrap() + .contains(&HubAgentsEvent::ChatsChanged(hub())) + ); +} + +#[gpui::test] +fn a_failed_listing_keeps_the_last_lists_and_says_why(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new() + .with_provider(&hub(), "Models", &["m1"]) + .with_agent(&hub(), "Ops", "m1"); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + fake.set_offline(true); + agents.update(cx, |agents, cx| agents.refresh(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert_eq!(agents.agents(&hub()).len(), 1); + assert!(agents.problem(&hub()).is_some()); + }); +} + +#[gpui::test] +fn a_listing_that_failed_is_read_again_once_the_hub_answers(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new() + .with_provider(&hub(), "Models", &["m1"]) + .with_agent(&hub(), "Ops", "m1"); + fake.set_offline(true); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(agents.problem(&hub()).is_some())); + fake.set_offline(false); + cx.executor() + .advance_clock(std::time::Duration::from_secs(2)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert!(agents.problem(&hub()).is_none()); + assert_eq!(agents.agents(&hub()).len(), 1); + }); +} + +#[gpui::test] +fn granting_access_writes_the_agent_key_line_and_records_the_server(cx: &mut TestAppContext) { + let (fake, hosts) = (FakeHubAgents::new(), FakeAgentHosts::default()); + let file = FakeAuthorizedKeys::with_text("ssh-ed25519 AAAAown me\n"); + hosts.connect("web", "deploy", "web.example", file.clone()); + let agents = cx + .update(|cx| install(deps_over(&fake, &hosts), cx)) + .agents() + .clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(agents.session_open("web"))); + + let granted = agents.update(cx, |agents, cx| agents.grant_access(&hub(), web(), cx)); + cx.run_until_parked(); + + assert!(cx.foreground_executor().block_test(granted).is_ok()); + let text = file.text(); + assert!(text.starts_with("ssh-ed25519 AAAAown me\n"), "{text}"); + assert!(text.contains("AAAAkey0 nocterm-agents"), "{text}"); + agents.read_with(cx, |agents, _| { + let server = agents + .server_of(&hub(), &web()) + .expect("the server is recorded"); + assert_eq!(server.host_key_fingerprint, "ssh-ed25519 AAAAfakehostkey"); + }); +} + +#[gpui::test] +fn access_needs_an_open_session_and_records_nothing_without_one(cx: &mut TestAppContext) { + let fake = FakeHubAgents::new(); + let agents = cx.update(|cx| install(deps(&fake), cx)).agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(!agents.session_open("web"))); + + let granted = agents.update(cx, |agents, cx| agents.grant_access(&hub(), web(), cx)); + cx.run_until_parked(); + + assert!(matches!( + cx.foreground_executor().block_test(granted), + Err(AgentAccessError::NotConnected) + )); + agents.read_with(cx, |agents, _| { + assert!(agents.server_of(&hub(), &web()).is_none()) + }); +} + +#[gpui::test] +fn withdrawing_access_removes_only_the_agent_key_line_and_the_record(cx: &mut TestAppContext) { + let (fake, hosts) = (FakeHubAgents::new(), FakeAgentHosts::default()); + let file = FakeAuthorizedKeys::with_text("ssh-ed25519 AAAAown me\n"); + hosts.connect("web", "deploy", "web.example", file.clone()); + let agents = cx + .update(|cx| install(deps_over(&fake, &hosts), cx)) + .agents() + .clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + let granted = agents.update(cx, |agents, cx| agents.grant_access(&hub(), web(), cx)); + cx.run_until_parked(); + assert!(cx.foreground_executor().block_test(granted).is_ok()); + + let withdrawn = agents.update(cx, |agents, cx| agents.withdraw_access(&hub(), &web(), cx)); + cx.run_until_parked(); + + assert!(matches!( + cx.foreground_executor().block_test(withdrawn), + Ok(Some(_)) + )); + assert_eq!(file.text(), "ssh-ed25519 AAAAown me\n"); + agents.read_with(cx, |agents, _| { + assert!(agents.server_of(&hub(), &web()).is_none()) + }); +} + +#[gpui::test] +fn a_server_connected_on_another_device_is_listed_after_its_hint(cx: &mut TestAppContext) { + use nocterm_hub_agents_model::{HubServers as _, NewServer}; + let fake = FakeHubAgents::new(); + let installed = cx.update(|cx| install(deps(&fake), cx)); + let agents = installed.agents().clone(); + agents.update(cx, |agents, cx| agents.track(&hub(), cx)); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| assert!(agents.servers(&hub()).is_empty())); + let elsewhere = fake.add_server( + &hub(), + NewServer { + name: "prod".into(), + host: "prod.example".into(), + port: 22, + user: "ops".into(), + host_key: String::new(), + }, + ); + futures::executor::block_on(elsewhere).unwrap(); + fake.changed(HubChange::Servers); + cx.run_until_parked(); + agents.read_with(cx, |agents, _| { + assert_eq!(agents.servers(&hub())[0].name, "prod"); + }); +} diff --git a/crates/hub-io/Cargo.toml b/crates/hub-io/Cargo.toml new file mode 100644 index 00000000..fb2d2a3c --- /dev/null +++ b/crates/hub-io/Cargo.toml @@ -0,0 +1,49 @@ +[package] +name = "nocterm-hub-io" +description = "Reaches Nocterm hubs through the hub contract: pinned TLS, device credentials and sign-in." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "adapter" + +[dependencies] +hex.workspace = true +nocterm-core.workspace = true +nocterm-ai.workspace = true +nocterm-hubs-model.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-session.workspace = true +nocterm-hub-client.workspace = true +nocterm-hub-proto.workspace = true +nocterm-keys.workspace = true +nocterm-connections-model.workspace = true +nocterm-vault.workspace = true + +async-channel.workspace = true +base64.workspace = true +futures.workspace = true +prost.workspace = true +serde.workspace = true +serde_json.workspace = true +sha2.workspace = true +thiserror.workspace = true +tokio.workspace = true +tonic.workspace = true +toml.workspace = true +tracing.workspace = true +uuid.workspace = true +zeroize.workspace = true + +[dev-dependencies] +nocterm-core = { workspace = true, features = ["test-support"] } +tokio = { workspace = true, features = ["test-util"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } +tempfile.workspace = true +nocterm-connections-model = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hub-io/src/account.rs b/crates/hub-io/src/account.rs new file mode 100644 index 00000000..8346bd15 --- /dev/null +++ b/crates/hub-io/src/account.rs @@ -0,0 +1,258 @@ +//! The open keys of the account, per hub, kept in memory only while the vault is unlocked. +//! +//! A hub's [`Slot`] holds the keyset opened with the vault password (never the password or +//! the unlock key beyond opening it), the collection keys opened from it, and a copy of the +//! encrypted cache file. Locking drops the keys, which wipes them. One notice is raised for +//! every change so a vault can report its status without polling. +//! Forget releases registry residency but preserves a weak Slot identity while +//! a general factory still uses it. A new login promotes that cleared Slot; +//! original captures remain bound to their retired membership and open keys. + +mod cache_publication; +mod flow; +pub(crate) mod key_admission; +mod keyset_hub; +mod lifecycle; +mod recovery; +mod refresh; +mod slot; +pub(crate) use slot::Slot; +pub(crate) mod operations; +pub(crate) mod source; +#[cfg(test)] +pub(crate) mod testing; + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError, Weak}; + +use nocterm_hub_client::Keyset; +use nocterm_hubs_model::{HubError, HubId}; +use nocterm_keys::{CollectionKey, OpenError, UnlockKdf, UnlockKey}; +use tokio::sync::watch; + +use crate::cache::{CacheFile, CacheWork, UnlockMethod}; +use crate::classify::{credentials, remote}; + +pub(crate) use flow::KeyFlow; +pub(crate) use lifecycle::SlotReceipt; + +/// Raises a counter whenever keys are opened, dropped or replaced. +pub(crate) struct Notice(watch::Sender); + +impl Notice { + fn new() -> Self { + Self(watch::channel(0).0) + } + + pub(crate) fn raise(&self) { + self.0.send_modify(|count| *count += 1); + } + + pub(crate) fn count(&self) -> u64 { + *self.0.borrow() + } + + pub(crate) fn subscribe(&self) -> watch::Receiver { + self.0.subscribe() + } +} + +/// The opened keyset and the collection keys opened with it. +pub(crate) struct OpenKeys { + keyset: Keyset, + collections: Mutex>>, +} + +impl OpenKeys { + pub(crate) fn new(keyset: Keyset) -> Self { + Self { + keyset, + collections: Mutex::default(), + } + } + + fn collections(&self) -> MutexGuard<'_, HashMap>> { + self.collections + .lock() + .unwrap_or_else(PoisonError::into_inner) + } + + /// The opened keyset. + pub(crate) fn keyset(&self) -> &Keyset { + &self.keyset + } + + /// The key of a collection of the account: the envelope the hub holds was sealed by the + /// account key. An envelope that does not open is [`OpenError`]: the hub altered it. + pub(crate) fn own_collection( + &self, + collection_id: &str, + envelope: &[u8], + ) -> Result, OpenError> { + self.remembered(collection_id, || { + self.keyset.open_collection_key(collection_id, envelope) + }) + } + + fn remembered( + &self, + collection_id: &str, + open: impl FnOnce() -> Result, + ) -> Result, OpenError> { + if let Some(key) = self.collections().get(collection_id) { + return Ok(Arc::clone(key)); + } + let key = Arc::new(open()?); + self.collections() + .insert(collection_id.to_owned(), Arc::clone(&key)); + Ok(key) + } + + /// A new collection key of its own, with the envelope the hub stores for this account. + pub(crate) fn new_collection( + &self, + collection_id: &str, + ) -> Result<(Arc, Vec), HubError> { + let (key, envelope) = self.generate_collection(collection_id)?; + self.collections() + .insert(collection_id.to_owned(), Arc::clone(&key)); + Ok((key, envelope)) + } + /// Transient owners retain the key themselves rather than populating a cache. + pub(crate) fn generate_collection( + &self, + collection_id: &str, + ) -> Result<(Arc, Vec), HubError> { + let key = CollectionKey::generate().map_err(remote)?; + let envelope = self + .keyset + .seal_collection_key(&key, collection_id) + .map_err(remote)?; + Ok((Arc::new(key), envelope)) + } +} + +/// A keyset the typed password does not open, kept until a recovery key opens it. +pub(crate) struct Recovery { + account_id: String, + unlock: UnlockKey, + kdf: UnlockKdf, + method: UnlockMethod, +} + +/// The slots of the hubs this device knows, and where their caches are kept. +pub(crate) struct Accounts { + root: PathBuf, + slots: Mutex>, + notice: Arc, + work: Arc, + // Serialize first loads with eviction, outside the short map lookup lock. + loading: Mutex<()>, +} + +enum SlotRetention { + Resident(Arc), + Forgotten(Weak), +} +impl SlotRetention { + fn upgrade(&self) -> Option> { + match self { + Self::Resident(slot) => Some(slot.clone()), + Self::Forgotten(slot) => slot.upgrade(), + } + } + fn retain(&mut self) -> Option> { + if let Self::Resident(slot) = self { + return Some(slot.clone()); + } + let slot = self.upgrade()?; + *self = Self::Resident(slot.clone()); + Some(slot) + } + fn alive(&self) -> bool { + match self { + Self::Resident(_) => true, + Self::Forgotten(slot) => slot.strong_count() != 0, + } + } +} + +impl Accounts { + #[cfg(test)] + pub(crate) fn new(root: PathBuf) -> Self { + Self::with_runtime(root, None) + } + + pub(crate) fn with_runtime(root: PathBuf, runtime: Option) -> Self { + Self { + root, + slots: Mutex::default(), + notice: Arc::new(Notice::new()), + work: CacheWork::new(runtime), + loading: Mutex::new(()), + } + } + + pub(crate) fn notice(&self) -> &Arc { + &self.notice + } + + /// Release residency without severing a live general account factory. A + /// failed settings removal can still sign in through that cleared Slot. + /// Captured authority remains retired by the preceding erase transition. + pub(crate) fn evict_if(&self, hub: &HubId, original: &Arc) { + let _loading = self.loading.lock().unwrap_or_else(PoisonError::into_inner); + let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner); + slots.retain(|_, slot| slot.alive()); + if slots + .get(hub) + .and_then(SlotRetention::upgrade) + .is_some_and(|slot| Arc::ptr_eq(&slot, original)) + { + slots.insert( + hub.clone(), + SlotRetention::Forgotten(Arc::downgrade(original)), + ); + } + } + + pub(crate) fn loaded_slot(&self, hub: &HubId) -> Option> { + let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner); + let slot = slots.get(hub).and_then(SlotRetention::upgrade); + if slot.is_none() { + slots.remove(hub); + } + slot + } + + fn resident_slot(&self, hub: &HubId) -> Option> { + self.slots + .lock() + .unwrap_or_else(PoisonError::into_inner) + .get_mut(hub) + .and_then(SlotRetention::retain) + } + + /// The slot of a hub; its cache is read the first time. + pub(crate) fn slot(&self, hub: &HubId) -> Result, HubError> { + if let Some(slot) = self.resident_slot(hub) { + return Ok(slot); + } + let _loading = self.loading.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(slot) = self.resident_slot(hub) { + return Ok(slot); + } + let file = CacheFile::new(&self.root, hub.address()) + .map_err(credentials)? + .with_work(self.work.clone()); + let slot = Arc::new(Slot::new(hub.clone(), file, Arc::clone(&self.notice))); + let mut slots = self.slots.lock().unwrap_or_else(PoisonError::into_inner); + slots.retain(|_, slot| slot.alive()); + slots.insert(hub.clone(), SlotRetention::Resident(slot.clone())); + Ok(slot) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/account/cache_publication.rs b/crates/hub-io/src/account/cache_publication.rs new file mode 100644 index 00000000..ce1c7568 --- /dev/null +++ b/crates/hub-io/src/account/cache_publication.rs @@ -0,0 +1,50 @@ +//! An unpublished cache candidate retains its exact original content generation. +//! Staging has no authority; only Slot can atomically admit its namespace commit. +use std::sync::Arc; + +use nocterm_core::fs::{AtomicPublication, CommittedPublication}; + +use crate::cache::{AccountCache, CacheError}; + +pub(crate) struct CacheDraft { + original: Option>, + next: Arc, + publication: AtomicPublication, +} + +impl CacheDraft { + pub(super) fn new( + original: Option>, + next: AccountCache, + publication: AtomicPublication, + ) -> Self { + Self { + original, + next: Arc::new(next), + publication, + } + } + + pub(super) fn account(&self) -> &str { + &self.next.account_id + } + + pub(super) fn matches(&self, current: &Option>) -> bool { + same_generation(&self.original, current) + } + + pub(super) fn commit(self) -> Result<(Arc, CommittedPublication), CacheError> { + Ok((self.next, self.publication.commit()?)) + } +} + +pub(super) fn same_generation( + original: &Option>, + current: &Option>, +) -> bool { + match (original, current) { + (Some(original), Some(current)) => Arc::ptr_eq(original, current), + (None, None) => true, + _ => false, + } +} diff --git a/crates/hub-io/src/account/flow.rs b/crates/hub-io/src/account/flow.rs new file mode 100644 index 00000000..5ab8f868 --- /dev/null +++ b/crates/hub-io/src/account/flow.rs @@ -0,0 +1,594 @@ +//! Opening, creating and recovering the account keys around a sign-in. +//! +//! A first sign-in creates the keyset (one conditional write: a second device that races it +//! opens the keyset that won, it never overwrites it). A later sign-in opens it with the key +//! derived from the password. A password that does not open it (it was reset) leaves the keys +//! waiting for the recovery key. A keyset that does not authenticate although the key is right +//! was altered by the hub: that is an error and nothing is written. + +use super::{ + key_admission::{KeyAdmission, KeySnapshot}, + source::{AccountSubject, AccountSubjects, CapturedMembership, LocalSourceProvider}, +}; +use crate::pool::captured::CapturedClient; +use std::sync::{Arc, Weak}; +use tokio::runtime::Handle; + +use futures::future::BoxFuture; +use nocterm_core::SharedCause; +use nocterm_hub_client::{Keyset, KeysetError, UnlockMethod as KeysetUnlock}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, HubId, KeyStatus}; +use nocterm_keys::{LoginError, LoginSecrets, RecoveryKey, UnlockKdf, UnlockKey}; +use nocterm_session::Secret; + +use super::keyset_hub::KeysetHub; +use super::{Accounts, OpenKeys, Recovery}; +use crate::cache::{AccountCache, KeysetCopy, UnlockMethod}; +use crate::classify::remote; +use crate::directory::who_am_i; +use crate::pool::Pool; +use nocterm_hubs_model::{RecoveryKeyOffer, RecoveryKeyReissue}; + +/// The key work of a sign-in, for every hub. +pub(crate) struct KeyFlow { + pool: Arc, + accounts: Arc, + handle: Handle, + weak: Weak, +} + +/// How often the replacement of the recovery envelope is tried against a keyset that other +/// devices keep writing. +const RESEAL_ATTEMPTS: u32 = 4; + +/// What a keyset write decided. +enum Creation { + Created(nocterm_hub_client::NewKeyset), + /// Another device created the keyset first. + Raced, +} + +/// A keyset that someone else already created is opened, not replaced. +fn creation( + result: Result, +) -> Result { + match result { + Ok(created) => Ok(Creation::Created(created)), + Err(KeysetError::AlreadyExists) => Ok(Creation::Raced), + Err(error) => Err(error), + } +} + +impl KeyFlow { + pub(crate) fn new(pool: Arc, accounts: Arc, handle: Handle) -> Arc { + Arc::new_cyclic(|weak| Self { + pool, + accounts, + handle, + weak: weak.clone(), + }) + } + + pub(crate) fn capture(&self, hub: &HubId) -> Result, HubError> { + let slot = self + .accounts + .loaded_slot(hub) + .ok_or(HubError::NotSignedIn)?; + let member = LocalSourceProvider::new(slot, self.pool.clone()).membership()?; + KeyAdmission::native( + member, + Arc::new(CapturedClient::current(self.pool.clone(), hub.clone())?), + ) + } + + pub(crate) fn published( + &self, + hub: &HubId, + subject: &Arc, + ) -> Result, HubError> { + let slot = self + .accounts + .loaded_slot(hub) + .ok_or(HubError::NotSignedIn)?; + let subjects: Arc = self.pool.clone(); + let member = CapturedMembership::published(&slot, &subjects, subject)?; + KeyAdmission::native( + member, + Arc::new(CapturedClient::current(self.pool.clone(), hub.clone())?), + ) + } + + pub(crate) fn bind_snapshot( + &self, + snapshot: KeySnapshot, + ) -> Result, HubError> { + snapshot.check()?; + let client = Arc::new(CapturedClient::current( + self.pool.clone(), + snapshot.member().hub().clone(), + )?); + KeyAdmission::bound(snapshot, client) + } + + pub(crate) fn run( + &self, + captured: Result, HubError>, + work: impl FnOnce(Arc) -> F + Send + 'static, + ) -> nocterm_hubs_model::HubFuture + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let owner = match captured { + Ok(owner) => owner, + Err(error) => return Box::pin(async move { Err(error) }), + }; + let returned = owner.clone(); + let task = crate::task::spawn(&self.handle, async move { + owner.during(work(owner.clone())).await? + }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + + pub(crate) fn recovery_offer(&self, hub: &HubId) -> Result { + let owner = self.capture(hub)?; + owner.keys()?; + Ok(self.issuer(owner.snapshot()?)) + } + + pub(super) fn issuer(&self, snapshot: KeySnapshot) -> RecoveryKeyReissue { + super::recovery::issuer(self.weak.clone(), snapshot) + } + + pub(super) fn offer( + &self, + owner: &KeyAdmission, + secret: Secret, + ) -> Result { + Ok(super::recovery::offer(secret, owner.snapshot()?)) + } + + fn unconfirmed(&self, owner: &KeyAdmission) -> Result { + owner.recovery_unconfirmed() + } + + fn opened(&self, owner: &KeyAdmission) -> Result { + if self.unconfirmed(owner)? { + Ok(KeyStatus::RecoveryKeyUnconfirmed( + self.issuer(owner.snapshot()?), + )) + } else { + Ok(KeyStatus::Open) + } + } + + /// The keys after a password sign-in: the login password also derives the unlock key. + pub(crate) async fn after_password( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + username: &str, + secrets: LoginSecrets, + ) -> Result { + owner.check()?; + if owner.member().subject()?.account() != account_id { + return Err(HubError::Stale); + } + let kdf = secrets.kdf().clone(); + let unlock = secrets.into_unlock(); + let method = UnlockMethod::Login { + username: username.trim().to_owned(), + }; + self.settle(owner, client, account_id, method, unlock, kdf) + .await + } + + /// Whether the keyset `record` is opened by something other than the login password. The + /// record says so itself: the client never guesses it from how the user signed in. + fn needs_vault_passphrase( + &self, + owner: &KeyAdmission, + record: &v2::Keyset, + ) -> Result { + match KeysetUnlock::of(record).map_err(|error| self.keyset_failure(owner, error))? { + KeysetUnlock::LoginPassword => Ok(false), + KeysetUnlock::VaultPassword(_) => Ok(true), + } + } + + /// The keys after a browser sign-in: nothing opens them until a passphrase is typed. + pub(crate) fn after_browser(&self, owner: &KeyAdmission) -> Result { + owner.check()?; + match owner.keys() { + Ok(_) => self.opened(owner), + Err(HubError::KeysLocked) => Ok(KeyStatus::PassphraseNeeded), + Err(error) => Err(error), + } + } + + /// Whether the recovery key made on this device was never confirmed as saved. + pub(crate) fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + let Some(slot) = self.accounts.loaded_slot(hub) else { + return false; + }; + slot.open() + .is_some_and(|keys| slot.recovery_unconfirmed(keys.keyset().account_id())) + } + + /// The count of changes to any hub's account keys (opened, dropped, replaced) so far. + pub(crate) fn keys_revision(&self) -> u64 { + self.accounts.notice().count() + } + + /// Resolves with the count once it differs from `seen`. + pub(crate) fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64> { + let mut changes = self.accounts.notice().subscribe(); + Box::pin(async move { + loop { + let now = *changes.borrow_and_update(); + if now != seen || changes.changed().await.is_err() { + return now; + } + } + }) + } + + pub(super) async fn reissue_recovery_key( + &self, + owner: &KeyAdmission, + ) -> Result { + let keys = owner.keys()?; + let client = owner.fresh().await?; + self.reissue_with(owner, &client, keys.keyset()).await + } + + async fn reissue_with( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + keyset: &Keyset, + ) -> Result { + let hub = owner.member().hub(); + let before = owner.cache()?.recovery_unconfirmed; + owner.keys()?; + owner.begin_recovery_issue().await?; + let recovery = RecoveryKey::generate().map_err(remote)?; + let written = self.reseal(owner, client, keyset, &recovery).await; + match written { + Ok(record) => { + keep_recovery_record(owner, &record).await; + Ok(Secret::new(recovery.display().as_str())) + } + Err(error) => { + if let Err(error) = owner + .update_cache(|cache| cache.recovery_unconfirmed = before) + .await + { + tracing::warn!(%error, hub = %hub, "the account cache cannot be written"); + } + Err(error) + } + } + } + + /// Writes the recovery envelope conditional on the keyset version read just before. A + /// device that wrote the keyset in between (another sign-in, a recovery) makes it conflict: + /// the record is read again and the write repeated, a few times. + async fn reseal( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + keyset: &Keyset, + recovery: &RecoveryKey, + ) -> Result { + let mut attempt = 1; + loop { + match owner + .during(client.reseal_recovery(keyset, recovery)) + .await? + { + Ok(record) => return Ok(record), + Err(KeysetError::Hub(error)) + if error.is_conflict() && attempt < RESEAL_ATTEMPTS => + { + attempt += 1; + } + Err(error) => return Err(self.keyset_failure(owner, error)), + } + } + } + + /// Opens, or creates, the keys with the vault passphrase of an account without a password. + pub(crate) async fn passphrase( + &self, + owner: &KeyAdmission, + passphrase: Secret, + ) -> Result { + let client = owner.fresh().await?; + let account = who_am_i(&client, owner).await?; + self.open_with_passphrase(owner, &client, account.id.as_str(), passphrase) + .await + } + + async fn open_with_passphrase( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + passphrase: Secret, + ) -> Result { + owner.check()?; + if owner.member().subject()?.account() != account_id { + return Err(HubError::Stale); + } + let record = match self.record(owner, client).await? { + Some(record) => record, + None => { + let kdf = UnlockKdf::random().map_err(remote)?; + let unlock = derive(&passphrase, &kdf).await?; + let method = UnlockMethod::Passphrase; + if let Some(created) = self + .try_create(owner, client, account_id, &method, &unlock, &kdf) + .await? + { + return Ok(created); + } + // Another device made the keyset first, derived with a salt of its own: the + // passphrase is tried again against the keyset that exists. + self.record(owner, client) + .await? + .ok_or_else(|| damaged(KeysetError::Incomplete))? + } + }; + // The record says which password opens it; a keyset sealed with the login password + // cannot be opened with a passphrase, whatever is typed. + let kdf = + match KeysetUnlock::of(&record).map_err(|error| self.keyset_failure(owner, error))? { + KeysetUnlock::VaultPassword(kdf) => kdf, + KeysetUnlock::LoginPassword => { + return Err(HubError::UnlockSourceMismatch { + needs_login_password: true, + }); + } + }; + let unlock = derive(&passphrase, &kdf).await?; + match Keyset::open(&record, account_id, &unlock) { + Err(KeysetError::WrongUnlockKey) => { + // Nothing says whether the passphrase was mistyped or is a new one: the + // recovery key decides, and then the typed passphrase is kept. + let fresh = UnlockKdf::random().map_err(remote)?; + let unlock = derive(&passphrase, &fresh).await?; + owner.recovering(Recovery { + account_id: account_id.to_owned(), + unlock, + kdf: fresh, + method: UnlockMethod::Passphrase, + })?; + Ok(KeyStatus::RecoveryNeeded) + } + other => { + let keyset = other.map_err(|error| self.keyset_failure(owner, error))?; + let unconfirmed = self.unconfirmed(owner)?; + self.keep(owner, client, keyset, UnlockMethod::Passphrase, unconfirmed) + .await?; + self.opened(owner) + } + } + } + + /// Opens the keys with the recovery key, and makes the password typed at sign-in (or the + /// passphrase) open them from now on. + pub(crate) async fn recover( + &self, + owner: &KeyAdmission, + recovery_key: Secret, + ) -> Result<(), HubError> { + let client = owner.fresh().await?; + self.recover_with(owner, &client, recovery_key).await + } + + async fn recover_with( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + recovery_key: Secret, + ) -> Result<(), HubError> { + let pending = owner.recovery()?; + let key = + RecoveryKey::parse(recovery_key.expose()).map_err(|_| HubError::RecoveryKeyInvalid)?; + let keyset = owner + .during(client.recover(&pending.account_id, &key, &pending.unlock, &pending.kdf)) + .await? + .map_err(|error| match error { + KeysetError::WrongRecoveryKey => HubError::RecoveryKeyInvalid, + other => self.keyset_failure(owner, other), + })?; + // The person typed the recovery key, so it is the one they hold. + self.keep(owner, client, keyset, pending.method.clone(), false) + .await + } + + async fn settle( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + method: UnlockMethod, + unlock: UnlockKey, + kdf: UnlockKdf, + ) -> Result { + let record = match self.record(owner, client).await? { + Some(record) => record, + None => { + let created = self + .try_create(owner, client, account_id, &method, &unlock, &kdf) + .await?; + if let Some(created) = created { + return Ok(created); + } + // Another device made the keyset first: this one opens it, never replaces it. + self.record(owner, client) + .await? + .ok_or_else(|| damaged(KeysetError::Incomplete))? + } + }; + if self.needs_vault_passphrase(owner, &record)? { + // The login password cannot open it, and is not a wrong password either. + return Ok(KeyStatus::PassphraseNeeded); + } + match Keyset::open(&record, account_id, &unlock) { + Ok(keyset) => { + let unconfirmed = self.unconfirmed(owner)?; + self.keep(owner, client, keyset, method, unconfirmed) + .await?; + self.opened(owner) + } + Err(KeysetError::WrongUnlockKey) => { + owner.recovering(Recovery { + account_id: account_id.to_owned(), + unlock, + kdf, + method, + })?; + Ok(KeyStatus::RecoveryNeeded) + } + Err(error) => Err(self.keyset_failure(owner, error)), + } + } + + /// Creates the keyset and opens the vault with it. `None` when another device created the + /// keyset first. + async fn try_create( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + account_id: &str, + method: &UnlockMethod, + unlock: &UnlockKey, + kdf: &UnlockKdf, + ) -> Result, HubError> { + let made = creation(owner.during(client.create(account_id, unlock, kdf)).await?) + .map_err(|error| self.keyset_failure(owner, error))?; + match made { + Creation::Created(new) => { + // Marked before the key is shown: if it is never confirmed, the next time the + // keys open another is offered. + if let Err(error) = self + .keep(owner, client, new.keyset, method.clone(), true) + .await + { + owner.remember_pending_recovery()?; + return Err(error); + } + Ok(Some(KeyStatus::Created(self.offer( + owner, + Secret::new(new.recovery.display().as_str()), + )?))) + } + Creation::Raced => Ok(None), + } + } + + /// Opens the vault with `keyset` and writes the cache from the record the hub now holds. + async fn keep( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + keyset: Keyset, + method: UnlockMethod, + unconfirmed: bool, + ) -> Result<(), HubError> { + let record = self + .record(owner, client) + .await? + .ok_or_else(|| damaged(KeysetError::Incomplete))?; + let mut cache = AccountCache::new( + client.hub_uid(), + keyset.account_id().to_owned(), + method, + &record, + ); + cache.recovery_unconfirmed = unconfirmed; + let keys = OpenKeys::new(keyset); + owner.install(keys, cache).await + } + + async fn record( + &self, + owner: &KeyAdmission, + client: &dyn KeysetHub, + ) -> Result, HubError> { + owner + .during(client.record()) + .await? + .map_err(|error| owner.failure(error)) + } + + fn keyset_failure(&self, owner: &KeyAdmission, error: KeysetError) -> HubError { + match error { + KeysetError::Hub(error) => owner.failure(error), + other => keyset_error(other), + } + } +} + +/// Keep the hub's accepted recovery envelope in the offline copy. The pending +/// recovery fact was persisted before resealing; a cache failure cannot undo +/// the successful remote replacement or suppress the new recovery key. +async fn keep_recovery_record(owner: &KeyAdmission, record: &v2::Keyset) { + if let Err(error) = owner + .update_cache(|cache| cache.keyset = KeysetCopy::of(record)) + .await + { + tracing::warn!(%error, hub = %owner.member().hub(), "the account cache cannot be written"); + } +} + +/// A keyset failure that is not a hub call failing. +fn keyset_error(error: KeysetError) -> HubError { + match error { + KeysetError::Damaged(_) + | KeysetError::Kdf(_) + | KeysetError::Incomplete + | KeysetError::AlreadyExists + | KeysetError::WrongRecoveryKey + | KeysetError::WrongUnlockKey + | KeysetError::UnsupportedUnlock { .. } => damaged(error), + KeysetError::WrongUnlockSource { stored, .. } => HubError::UnlockSourceMismatch { + needs_login_password: matches!(stored, nocterm_keys::UnlockSource::LoginPassword), + }, + other => remote(other), + } +} + +fn damaged(error: KeysetError) -> HubError { + HubError::KeysetDamaged { + cause: SharedCause::new(error), + } +} + +/// The unlock key of a vault passphrase. Key derivation is slow and memory hard, so it does not +/// run on a thread that serves other hub calls. +async fn derive(passphrase: &Secret, kdf: &UnlockKdf) -> Result { + let (passphrase, kdf) = (passphrase.clone(), kdf.clone()); + let derived = tokio::task::spawn_blocking(move || { + UnlockKey::from_vault_password(passphrase.expose(), &kdf) + }) + .await + .map_err(remote)?; + derived.map_err(|error| match error { + LoginError::PasswordTooShort { .. } | LoginError::EmptyUsername => { + HubError::InvalidVaultPassphrase + } + other => remote(other), + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/account/flow/tests.rs b/crates/hub-io/src/account/flow/tests.rs new file mode 100644 index 00000000..d4792b31 --- /dev/null +++ b/crates/hub-io/src/account/flow/tests.rs @@ -0,0 +1,562 @@ +use nocterm_keys::OpenError; +mod source_binding; + +use super::*; +use crate::account::Slot; + +#[test] +fn a_device_that_loses_the_race_opens_the_keyset_that_exists() { + assert!(matches!( + creation(Err(KeysetError::AlreadyExists)), + Ok(Creation::Raced) + )); +} + +#[test] +fn other_creation_failures_stay_failures() { + assert!(matches!( + creation(Err(KeysetError::Incomplete)), + Err(KeysetError::Incomplete) + )); +} + +#[test] +fn a_keyset_the_hub_altered_is_reported_as_damaged_not_overwritten() { + for error in [ + KeysetError::Damaged(OpenError::Rejected), + KeysetError::Incomplete, + KeysetError::WrongUnlockKey, + ] { + assert!(matches!( + keyset_error(error), + HubError::KeysetDamaged { .. } + )); + } +} + +pub(super) mod race { + use std::path::Path; + + use nocterm_vault::VaultStatus; + use tempfile::TempDir; + + use super::*; + use crate::account::testing::{ACCOUNT_ID, HUB_UID, MemoryKeysets, PASSWORD, hub}; + use crate::cache::KeysetCopy; + + const PASSPHRASE: &str = "a long vault passphrase"; + + /// One device of the account: its own cache directory and its own key flow. + pub(super) struct Device { + pub(super) flow: Arc, + subjects: Arc, + accounts: Arc, + dir: TempDir, + } + + impl Device { + pub(super) fn new() -> Self { + Self::in_dir(tempfile::tempdir().unwrap()) + } + + fn in_dir(dir: TempDir) -> Self { + let accounts = Arc::new(Accounts::new(dir.path().to_path_buf())); + let subjects = Arc::new(crate::account::testing::ScriptedSubjects::for_slot( + &accounts.slot(&hub()).unwrap(), + )); + Self { + flow: KeyFlow::new( + pool(dir.path(), &accounts), + Arc::clone(&accounts), + Handle::current(), + ), + subjects, + accounts, + dir, + } + } + + /// The same device after the client was closed and opened again: what it kept on disk + /// stays, what it held in memory is gone. + pub(super) fn restarted(self) -> Self { + Self::in_dir(self.dir) + } + + pub(super) fn slot(&self) -> Arc { + self.accounts.slot(&hub()).unwrap() + } + + pub(super) fn owner(&self) -> Arc { + if self.subjects.current(&hub()).is_none() { + self.subjects.sign_in(hub(), ACCOUNT_ID, "device"); + } + let provider = LocalSourceProvider::new(self.slot(), self.subjects.clone()); + KeyAdmission::scripted(provider.membership().unwrap()) + } + pub(super) async fn confirm(&self) { + let owner = self.owner(); + self.flow + .offer(&owner, Secret::new("displayed fixture key")) + .unwrap() + .confirm() + .await + .unwrap(); + } + + pub(super) async fn sign_in(&self, hub_keys: &MemoryKeysets, password: &str) -> KeyStatus { + self.subjects.sign_in(hub(), ACCOUNT_ID, "device"); + let owner = self.owner(); + let secrets = LoginSecrets::derive(password, HUB_UID, "ada").unwrap(); + self.flow + .after_password(&owner, hub_keys, ACCOUNT_ID, "ada", secrets) + .await + .unwrap() + } + + async fn with_passphrase(&self, hub_keys: &MemoryKeysets, passphrase: &str) -> KeyStatus { + let passphrase = Secret::new(passphrase); + self.flow + .open_with_passphrase(&self.owner(), hub_keys, ACCOUNT_ID, passphrase) + .await + .unwrap() + } + } + + fn pool(dir: &Path, accounts: &Arc) -> Arc { + Arc::new(Pool::new(dir.to_path_buf(), "test", Arc::clone(accounts))) + } + + #[tokio::test] + async fn remote_first_creation_with_failed_local_installation_requires_recovery_reissue_after_repair() + { + let device = Device::new(); + let hub_keys = MemoryKeysets::default(); + let owner = device.owner(); + let path = device.dir.path().join(format!( + "{}.cache", + crate::credentials::file_stem(hub().address()) + )); + std::fs::create_dir(&path).unwrap(); + let secrets = LoginSecrets::derive(PASSWORD, HUB_UID, "ada").unwrap(); + assert!(matches!( + device + .flow + .after_password(&owner, &hub_keys, ACCOUNT_ID, "ada", secrets) + .await, + Err(HubError::Credentials { .. }) + )); + assert!( + hub_keys.stored().is_some(), + "remote creation already succeeded" + ); + assert!( + device.slot().open().is_none(), + "failed durable installation cannot open keys" + ); + std::fs::remove_dir(path).unwrap(); + let secrets = LoginSecrets::derive(PASSWORD, HUB_UID, "ada").unwrap(); + assert!(matches!( + device + .flow + .after_password(&owner, &hub_keys, ACCOUNT_ID, "ada", secrets) + .await + .unwrap(), + KeyStatus::RecoveryKeyUnconfirmed(_) + )); + let keys = owner.keys().unwrap(); + let shown = device + .flow + .reissue_with(&owner, &hub_keys, keys.keyset()) + .await + .unwrap(); + device + .flow + .offer(&owner, shown) + .unwrap() + .confirm() + .await + .unwrap(); + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } + + /// Both devices read "no keyset" before either creates one. + fn racing_hub() -> MemoryKeysets { + let hub_keys = MemoryKeysets::default(); + hub_keys.meet_before_creating(2); + hub_keys + } + + #[tokio::test] + async fn recovery_acknowledgement_rechecks_its_postcommit_generation_after_parent_sync() { + use crate::account::testing::blocked_cache_job; + use futures::poll; + let device = Device::new(); + let hub_keys = MemoryKeysets::default(); + let KeyStatus::Created(shown) = device.sign_in(&hub_keys, PASSWORD).await else { + panic!("created") + }; + let slot = device.slot(); + let (job, entered, first_release) = blocked_cache_job(&slot); + let first = tokio::spawn(job); + tokio::time::timeout(std::time::Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + let (job, entered, second_release) = blocked_cache_job(&slot); + let second = tokio::spawn(job); + tokio::time::timeout(std::time::Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + let mut confirmation = shown.confirm(); + assert!(poll!(&mut confirmation).is_pending()); + let (mut job, entered, final_release) = blocked_cache_job(&slot); + assert!(poll!(&mut job).is_pending()); + let confirming = tokio::spawn(confirmation); + let final_job = tokio::spawn(job); + first_release.release(); + tokio::time::timeout(std::time::Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + tokio::time::timeout(std::time::Duration::from_secs(5), async { + while slot.recovery_unconfirmed(ACCOUNT_ID) { + tokio::task::yield_now().await; + } + }) + .await + .unwrap(); + slot.lock(); + second_release.release(); + final_release.release(); + first.await.unwrap().unwrap(); + second.await.unwrap().unwrap(); + final_job.await.unwrap().unwrap(); + assert!(matches!(confirming.await.unwrap(), Err(HubError::Stale))); + } + + fn count(statuses: [&KeyStatus; 2], wanted: impl Fn(&KeyStatus) -> bool) -> usize { + statuses.into_iter().filter(|status| wanted(status)).count() + } + + fn is_created(status: &KeyStatus) -> bool { + matches!(status, KeyStatus::Created(_)) + } + + #[tokio::test] + async fn two_devices_creating_the_keyset_at_once_end_with_one_keyset_and_one_recovery_key() { + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + + let (a, b) = tokio::join!( + first.sign_in(&hub_keys, PASSWORD), + second.sign_in(&hub_keys, PASSWORD) + ); + + assert_eq!(hub_keys.creations(), 2, "both devices tried to create it"); + assert_eq!(count([&a, &b], is_created), 1, "{a:?} {b:?}"); + assert_eq!(count([&a, &b], |s| *s == KeyStatus::Open), 1, "{a:?} {b:?}"); + let stored = hub_keys.stored().unwrap(); + assert_eq!(stored.version, 1, "the loser did not write over the keyset"); + for device in [&first, &second] { + let slot = device.slot(); + assert_eq!(slot.status(), VaultStatus::Unlocked); + let cached = slot.read_cache(|cache| cache.map(|cache| cache.keyset.clone())); + assert_eq!( + cached, + Some(KeysetCopy::of(&stored)), + "both keep the winner's" + ); + } + } + + #[tokio::test] + async fn the_device_that_lost_the_race_holds_the_keys_of_the_one_that_won() { + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + tokio::join!( + first.sign_in(&hub_keys, PASSWORD), + second.sign_in(&hub_keys, PASSWORD) + ); + + let (_, envelope) = first + .slot() + .open() + .unwrap() + .new_collection("shared") + .unwrap(); + let opened = second + .slot() + .open() + .unwrap() + .own_collection("shared", &envelope); + assert!( + opened.is_ok(), + "one account key opens what the other sealed" + ); + } + + #[tokio::test] + async fn a_vault_passphrase_that_loses_the_race_opens_the_keyset_that_won() { + // Each device chose a salt of its own before it knew of the other: the loser must + // derive the key again with the salt of the keyset that exists. + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + + let (a, b) = tokio::join!( + first.with_passphrase(&hub_keys, PASSPHRASE), + second.with_passphrase(&hub_keys, PASSPHRASE) + ); + + assert_eq!(count([&a, &b], is_created), 1, "{a:?} {b:?}"); + assert_eq!(count([&a, &b], |s| *s == KeyStatus::Open), 1, "{a:?} {b:?}"); + for device in [&first, &second] { + assert_eq!(device.slot().status(), VaultStatus::Unlocked); + } + } + + #[tokio::test] + async fn a_keyset_sealed_with_the_login_password_is_not_opened_with_a_passphrase() { + let hub_keys = MemoryKeysets::default(); + let (first, second) = (Device::new(), Device::new()); + assert!(is_created(&first.sign_in(&hub_keys, PASSWORD).await)); + + let refused = second + .flow + .open_with_passphrase( + &second.owner(), + &hub_keys, + ACCOUNT_ID, + Secret::new(PASSPHRASE), + ) + .await; + + assert!(matches!( + refused, + Err(HubError::UnlockSourceMismatch { + needs_login_password: true + }) + )); + assert!(second.slot().open().is_none()); + assert!( + second.slot().recovery().is_none(), + "a wrong source is not a wrong password: no recovery key is asked for" + ); + } + + #[tokio::test] + async fn a_keyset_sealed_with_a_vault_password_asks_for_it_instead_of_the_login_password() { + let hub_keys = MemoryKeysets::default(); + let (first, second) = (Device::new(), Device::new()); + first.with_passphrase(&hub_keys, PASSPHRASE).await; + + let status = second.sign_in(&hub_keys, PASSWORD).await; + + assert_eq!(status, KeyStatus::PassphraseNeeded); + assert!(second.slot().open().is_none()); + assert!(second.slot().recovery().is_none()); + assert_eq!( + second.with_passphrase(&hub_keys, PASSPHRASE).await, + KeyStatus::Open + ); + } + + #[tokio::test] + async fn a_device_with_another_password_loses_the_race_and_waits_for_the_recovery_key() { + let hub_keys = racing_hub(); + let (first, second) = (Device::new(), Device::new()); + + let (a, b) = tokio::join!( + first.sign_in(&hub_keys, PASSWORD), + second.sign_in(&hub_keys, "another long password") + ); + + let (winner, loser, created, waiting) = match (&a, &b) { + (KeyStatus::Created(key), KeyStatus::RecoveryNeeded) => (&first, &second, key, &b), + (KeyStatus::RecoveryNeeded, KeyStatus::Created(key)) => (&second, &first, key, &a), + other => panic!("one creates, the other waits for the recovery key: {other:?}"), + }; + assert_eq!(*waiting, KeyStatus::RecoveryNeeded); + assert_eq!(winner.slot().status(), VaultStatus::Unlocked); + assert!( + loser.slot().open().is_none(), + "nothing opens without the key" + ); + + // The recovery key made by the winner opens the loser's keys. + let recovered = loser + .flow + .recover_with(&loser.owner(), &hub_keys, created.key().clone()) + .await; + assert!(recovered.is_ok()); + assert_eq!(loser.slot().status(), VaultStatus::Unlocked); + } +} + +mod reissue { + use nocterm_keys::{RecoveryKey, UnlockKey}; + + use super::race::Device; + use super::*; + use crate::account::testing::{ACCOUNT_ID, HUB_UID, MemoryKeysets, PASSWORD, hub}; + + /// A device that created the account's keys, and the recovery key it was shown. + async fn created() -> (Device, MemoryKeysets, RecoveryKey) { + let (device, hub_keys) = (Device::new(), MemoryKeysets::default()); + let KeyStatus::Created(shown) = device.sign_in(&hub_keys, PASSWORD).await else { + panic!("a first sign-in creates the keys"); + }; + let key = RecoveryKey::parse(shown.key().expose()).unwrap(); + (device, hub_keys, key) + } + + async fn reissue(device: &Device, hub_keys: &MemoryKeysets) -> Result { + let slot = device.slot(); + let keys = slot.open().ok_or(HubError::KeysLocked)?; + let shown = device + .flow + .reissue_with(&device.owner(), hub_keys, keys.keyset()) + .await?; + Ok(RecoveryKey::parse(shown.expose()).unwrap()) + } + + fn opens_with(hub_keys: &MemoryKeysets, key: &RecoveryKey) -> bool { + Keyset::recover(&hub_keys.stored().unwrap(), ACCOUNT_ID, key).is_ok() + } + + fn password_opens(hub_keys: &MemoryKeysets) -> bool { + let secrets = LoginSecrets::derive(PASSWORD, HUB_UID, "ada").unwrap(); + let unlock: UnlockKey = secrets.into_unlock(); + Keyset::open(&hub_keys.stored().unwrap(), ACCOUNT_ID, &unlock).is_ok() + } + + #[tokio::test] + async fn a_recovery_key_is_unconfirmed_from_its_creation_until_the_person_confirms_it() { + let (device, ..) = created().await; + assert!(device.flow.recovery_key_unconfirmed(&hub())); + + device.confirm().await; + + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } + + #[tokio::test] + async fn an_unconfirmed_key_is_offered_again_when_the_keys_open_after_a_restart() { + let (device, hub_keys, _) = created().await; + + let device = device.restarted(); + assert!( + !device.flow.recovery_key_unconfirmed(&hub()), + "nothing is open before the password opens the keys" + ); + let status = device.sign_in(&hub_keys, PASSWORD).await; + assert!(matches!(status, KeyStatus::RecoveryKeyUnconfirmed(_))); + assert!(device.flow.recovery_key_unconfirmed(&hub())); + + device.confirm().await; + let device = device.restarted(); + assert_eq!(device.sign_in(&hub_keys, PASSWORD).await, KeyStatus::Open); + } + + #[tokio::test] + async fn another_device_of_the_account_does_not_inherit_the_marker() { + let (_first, hub_keys, _) = created().await; + + let second = Device::new(); + + assert_eq!(second.sign_in(&hub_keys, PASSWORD).await, KeyStatus::Open); + } + + #[tokio::test] + async fn reissuing_replaces_the_recovery_key_the_hub_can_open_and_keeps_the_password() { + let (device, hub_keys, old) = created().await; + device.confirm().await; + let version = hub_keys.stored().unwrap().version; + + let new = reissue(&device, &hub_keys).await.unwrap(); + + assert!(opens_with(&hub_keys, &new)); + assert!(!opens_with(&hub_keys, &old), "the old key no longer opens"); + assert!(password_opens(&hub_keys), "the password still opens it"); + let stored = hub_keys.stored().unwrap(); + assert_eq!(stored.version, version + 1); + assert_eq!( + device + .slot() + .read_cache(|cache| cache.unwrap().keyset.clone()), + KeysetCopy::of(&stored), + "the cache holds the record the hub now has" + ); + } + + #[tokio::test] + async fn a_reissued_key_is_unconfirmed_until_confirmed_and_survives_a_restart() { + let (device, hub_keys, _) = created().await; + device.confirm().await; + + reissue(&device, &hub_keys).await.unwrap(); + assert!(device.flow.recovery_key_unconfirmed(&hub())); + + let device = device.restarted(); + assert!(matches!( + device.sign_in(&hub_keys, PASSWORD).await, + KeyStatus::RecoveryKeyUnconfirmed(_) + )); + } + + #[tokio::test] + async fn a_write_that_loses_to_another_device_is_repeated_against_the_new_version() { + let (device, hub_keys, _) = created().await; + let version = hub_keys.stored().unwrap().version; + hub_keys.contend_recovery_writes(2); + + let new = reissue(&device, &hub_keys).await.unwrap(); + + assert!(opens_with(&hub_keys, &new)); + assert_eq!( + hub_keys.stored().unwrap().version, + version + 3, + "two writes of the other device and then ours" + ); + } + + #[tokio::test] + async fn a_keyset_that_keeps_changing_leaves_the_old_key_valid_and_the_marker_as_it_was() { + let (device, hub_keys, old) = created().await; + device.confirm().await; + hub_keys.contend_recovery_writes(100); + + let result = reissue(&device, &hub_keys).await; + + assert!(result.is_err()); + assert!(opens_with(&hub_keys, &old), "nothing was replaced"); + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } + + #[tokio::test] + async fn reissuing_with_closed_keys_is_refused() { + let (device, ..) = created().await; + let device = device.restarted(); + + let result = device.flow.reissue_recovery_key(&device.owner()).await; + + assert!(matches!(result, Err(HubError::KeysLocked))); + } + + #[tokio::test] + async fn typing_the_recovery_key_confirms_that_the_person_holds_it() { + let (device, hub_keys, key) = created().await; + let device = device.restarted(); + let other = device.sign_in(&hub_keys, "another long password").await; + assert_eq!(other, KeyStatus::RecoveryNeeded); + + device + .flow + .recover_with( + &device.owner(), + &hub_keys, + Secret::new(key.display().as_str()), + ) + .await + .unwrap(); + + assert!(!device.flow.recovery_key_unconfirmed(&hub())); + } +} diff --git a/crates/hub-io/src/account/flow/tests/source_binding.rs b/crates/hub-io/src/account/flow/tests/source_binding.rs new file mode 100644 index 00000000..62f736fc --- /dev/null +++ b/crates/hub-io/src/account/flow/tests/source_binding.rs @@ -0,0 +1,78 @@ +use super::{race::Device, *}; +use crate::account::{ + key_admission::KeyAdmission, + source::LocalSourceProvider, + testing::{ACCOUNT_ID, MemoryKeysets, NativeAccounts, PASSWORD}, +}; +use nocterm_hubs_model::{Account, AccountId}; + +#[tokio::test] +async fn an_old_displayed_recovery_key_cannot_confirm_a_reissued_key() { + let device = Device::new(); + let records = MemoryKeysets::default(); + let KeyStatus::Created(first) = device.sign_in(&records, PASSWORD).await else { + panic!("created") + }; + let owner = device.owner(); + let keys = owner.keys().unwrap(); + let key = device + .flow + .reissue_with(&owner, &records, keys.keyset()) + .await + .unwrap(); + let second = device.flow.offer(&owner, key).unwrap(); + assert!(matches!(first.confirm().await, Err(HubError::Stale))); + assert!( + device + .flow + .recovery_key_unconfirmed(&crate::account::testing::hub()) + ); + second.confirm().await.unwrap(); + assert!( + !device + .flow + .recovery_key_unconfirmed(&crate::account::testing::hub()) + ); + assert!(matches!(second.confirm().await, Err(HubError::Stale))); +} + +#[tokio::test] +async fn same_account_sign_in_never_revives_an_earlier_recovery_card() { + let device = Device::new(); + let records = MemoryKeysets::default(); + let KeyStatus::Created(first) = device.sign_in(&records, PASSWORD).await else { + panic!("created") + }; + device.sign_in(&records, PASSWORD).await; + assert!(matches!(first.confirm().await, Err(HubError::Stale))); + assert!( + device + .flow + .recovery_key_unconfirmed(&crate::account::testing::hub()) + ); +} + +#[tokio::test] +async fn a_display_account_never_replaces_the_authenticated_native_recipient() { + let fixture = NativeAccounts::new(); + let provider = LocalSourceProvider::new(fixture.slot().clone(), fixture.subjects().clone()); + let owner = KeyAdmission::scripted(provider.membership().unwrap()); + let account = |id: &str| Account { + id: AccountId::new(id), + username: "display".into(), + display_name: "Display".into(), + admin: false, + }; + assert!(crate::directory::checked_account(&owner, account(ACCOUNT_ID)).is_ok()); + assert!(matches!( + crate::directory::checked_account(&owner, account("account-2")), + Err(HubError::Stale) + )); + fixture.login(1); + fixture.login(0); + assert!(matches!( + crate::directory::checked_account(&owner, account(ACCOUNT_ID)), + Err(HubError::Stale) + )); + assert!(fixture.source().check_current().is_ok()); +} diff --git a/crates/hub-io/src/account/key_admission.rs b/crates/hub-io/src/account/key_admission.rs new file mode 100644 index 00000000..974558fd --- /dev/null +++ b/crates/hub-io/src/account/key_admission.rs @@ -0,0 +1,270 @@ +//! One bounded key operation; display capabilities keep immutable snapshots, not this owner. +use super::{OpenKeys, Recovery, SlotReceipt, source::CapturedMembership}; +use crate::{cache::AccountCache, pool::captured::CapturedClient}; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hubs_model::HubError; +use std::sync::{Arc, Mutex, PoisonError}; + +#[derive(Clone)] +enum Requester { + Native(Arc), + #[cfg(test)] + Scripted, +} + +/// Unlike a running operation, an offer's immutable snapshot never follows publication. +#[derive(Clone)] +pub(crate) struct KeySnapshot { + member: Arc, + receipt: SlotReceipt, +} +impl KeySnapshot { + pub(crate) fn check(&self) -> Result<(), HubError> { + self.member.with_current(|| { + self.member + .slot()? + .check_receipt(&self.member.subject()?, &self.receipt) + }) + } + pub(crate) fn member(&self) -> &Arc { + &self.member + } + pub(crate) async fn confirm(&self) -> Result<(), HubError> { + let slot = self.member.slot()?; + let _turn = slot.cache_turn().await; + loop { + self.check()?; + let original = slot.cache_snapshot(); + let mut next = original.as_deref().cloned().ok_or(HubError::NotFound)?; + next.recovery_unconfirmed = false; + let draft = slot.stage_cache(original, next).await?; + let committed = self.member.with_current(|| { + slot.commit_member_cache(&self.member.subject()?, &self.receipt, draft, true) + })?; + if let Some((receipt, committed)) = committed { + slot.finish_cache(committed).await; + self.member + .with_current(|| slot.check_receipt(&self.member.subject()?, &receipt))?; + slot.notice().raise(); + return Ok(()); + } + } + } +} + +pub(crate) struct KeyAdmission { + member: Arc, + receipt: Mutex, + requester: Requester, +} +impl KeyAdmission { + pub(crate) fn native( + member: Arc, + client: Arc, + ) -> Result, HubError> { + let receipt = member.with_current(|| member.slot()?.receipt(&member.subject()?))?; + let admission = Arc::new(Self { + member, + receipt: Mutex::new(receipt), + requester: Requester::Native(client), + }); + admission.check()?; + Ok(admission) + } + pub(crate) fn bound( + snapshot: KeySnapshot, + client: Arc, + ) -> Result, HubError> { + snapshot.check()?; + let admission = Arc::new(Self { + member: snapshot.member, + receipt: Mutex::new(snapshot.receipt), + requester: Requester::Native(client), + }); + admission.check()?; + Ok(admission) + } + #[cfg(test)] + pub(crate) fn scripted(member: Arc) -> Arc { + let receipt = member + .with_current(|| member.slot()?.receipt(&member.subject()?)) + .unwrap(); + Arc::new(Self { + member, + receipt: Mutex::new(receipt), + requester: Requester::Scripted, + }) + } + fn receipt(&self) -> SlotReceipt { + self.receipt + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } + fn replaced(&self, receipt: SlotReceipt) { + *self.receipt.lock().unwrap_or_else(PoisonError::into_inner) = receipt; + } + pub(crate) fn snapshot(&self) -> Result { + let snapshot = KeySnapshot { + member: self.member.clone(), + receipt: self.receipt(), + }; + snapshot.check()?; + Ok(snapshot) + } + pub(crate) fn member(&self) -> &Arc { + &self.member + } + pub(crate) fn check(&self) -> Result<(), HubError> { + match &self.requester { + Requester::Native(client) => client.check()?, + #[cfg(test)] + Requester::Scripted => {} + } + self.snapshot().map(|_| ()) + } + pub(crate) fn cache(&self) -> Result { + let receipt = self.receipt(); + self.member.with_current(|| { + self.member + .slot()? + .cache_for(&self.member.subject()?, &receipt) + }) + } + pub(crate) fn recovery_unconfirmed(&self) -> Result { + let receipt = self.receipt(); + self.member.with_current(|| { + self.member + .slot()? + .pending_recovery_for(&self.member.subject()?, &receipt) + }) + } + + pub(crate) fn remember_pending_recovery(&self) -> Result<(), HubError> { + let receipt = self.receipt(); + let next = self.member.with_current(|| { + self.member + .slot()? + .remember_pending_recovery(&self.member.subject()?, &receipt) + })?; + self.replaced(next); + Ok(()) + } + pub(crate) fn keys(&self) -> Result, HubError> { + self.check()?; + let receipt = self.receipt(); + self.member.with_current(|| { + let slot = self.member.slot()?; + let subject = self.member.subject()?; + slot.check_receipt(&subject, &receipt)?; + slot.open_for(&subject) + }) + } + pub(crate) async fn install( + &self, + keys: OpenKeys, + mut cache: AccountCache, + ) -> Result<(), HubError> { + let slot = self.member.slot()?; + let _turn = slot.cache_turn().await; + let receipt = self.receipt(); + self.check()?; + let original = slot.cache_snapshot(); + if let Some(earlier) = original + .as_deref() + .filter(|old| old.account_id == cache.account_id) + { + cache.vault = earlier.vault.clone(); + } + let keys = Arc::new(keys); + let draft = slot.stage_cache(original, cache).await?; + let committed = self + .member + .with_current(|| slot.commit_install(&self.member.subject()?, &receipt, keys, draft))? + .ok_or(HubError::Stale)?; + self.replaced(committed.0); + slot.finish_cache(committed.1).await; + slot.notice().raise(); + Ok(()) + } + pub(crate) fn recovering(&self, recovery: Recovery) -> Result<(), HubError> { + let receipt = self.receipt(); + let next = self.member.with_current(|| { + self.member + .slot()? + .recovering_if(&self.member.subject()?, &receipt, recovery) + })?; + self.replaced(next); + Ok(()) + } + pub(crate) fn recovery(&self) -> Result, HubError> { + let receipt = self.receipt(); + self.member.with_current(|| { + let slot = self.member.slot()?; + slot.check_receipt(&self.member.subject()?, &receipt)?; + slot.recovery().ok_or(HubError::NoRecoveryPending) + }) + } + pub(crate) async fn update_cache( + &self, + change: impl Fn(&mut AccountCache), + ) -> Result<(), HubError> { + self.change_cache(change, false).await + } + + async fn change_cache( + &self, + change: impl Fn(&mut AccountCache), + rotate: bool, + ) -> Result<(), HubError> { + let slot = self.member.slot()?; + let _turn = slot.cache_turn().await; + let receipt = self.receipt(); + loop { + self.check()?; + let original = slot.cache_snapshot(); + let mut next = original.as_deref().cloned().ok_or(HubError::NotFound)?; + change(&mut next); + let draft = slot.stage_cache(original, next).await?; + let committed = self.member.with_current(|| { + slot.commit_member_cache(&self.member.subject()?, &receipt, draft, rotate) + })?; + if let Some((receipt, committed)) = committed { + self.replaced(receipt); + slot.finish_cache(committed).await; + if rotate { + slot.notice().raise(); + } + return Ok(()); + } + } + } + pub(crate) async fn begin_recovery_issue(&self) -> Result<(), HubError> { + self.change_cache(|cache| cache.recovery_unconfirmed = true, true) + .await + } + pub(crate) async fn fresh(&self) -> Result { + self.check()?; + let client = match &self.requester { + Requester::Native(client) => client.fresh().await?, + #[cfg(test)] + Requester::Scripted => return Err(HubError::NotSignedIn), + }; + self.check()?; + Ok(client) + } + pub(crate) fn failure(&self, error: ClientError) -> HubError { + match &self.requester { + Requester::Native(client) => client.failure(error), + #[cfg(test)] + Requester::Scripted => crate::classify::classify(self.member.hub().address(), error), + } + } + + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} diff --git a/crates/hub-io/src/account/keyset_hub.rs b/crates/hub-io/src/account/keyset_hub.rs new file mode 100644 index 00000000..38f01585 --- /dev/null +++ b/crates/hub-io/src/account/keyset_hub.rs @@ -0,0 +1,119 @@ +//! The keyset calls of a sign-in, as one narrow port to a hub. +//! +//! [`KeyFlow`](super::KeyFlow) decides what to do with the account's keys; this port is only +//! what it asks of the hub. [`HubClient`] implements it over the contract; tests implement it +//! over memory, so a race between two devices needs no network. + +use futures::future::BoxFuture; +use nocterm_hub_client::{ClientError, HubClient, Keyset, KeysetError, NewKeyset}; +use nocterm_hub_proto::v2; +use nocterm_keys::{RecoveryKey, UnlockKdf, UnlockKey}; + +/// What a key flow asks of one hub. +pub(crate) trait KeysetHub: Send + Sync { + /// The hub's own identifier, which the login key is derived with. + fn hub_uid(&self) -> String; + + /// The account's keyset record; `None` before a device created it. + fn record(&self) -> BoxFuture<'_, Result, ClientError>>; + + /// Creates the keyset, conditional on there being none: a device that lost the race gets + /// [`KeysetError::AlreadyExists`]. + fn create<'a>( + &'a self, + account_id: &'a str, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result>; + + /// Opens the keyset with the recovery key and seals the account key anew under `unlock`. + fn recover<'a>( + &'a self, + account_id: &'a str, + recovery: &'a RecoveryKey, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result>; + + /// Seals the account key of `keyset`, which is open, under `recovery` in place of the + /// recovery envelope the hub holds. One write conditional on the version just read: a + /// version that moved in between is a conflict ([`ClientError::is_conflict`]) the caller + /// retries. Returns the record the hub now holds. + fn reseal_recovery<'a>( + &'a self, + keyset: &'a Keyset, + recovery: &'a RecoveryKey, + ) -> BoxFuture<'a, Result>; +} + +/// The write that replaces the recovery envelope of `record` and nothing else: the other +/// values are the hub's own, and the write is conditional on the version just read. +pub(crate) fn resealed( + record: &v2::Keyset, + keyset: &Keyset, + recovery: &RecoveryKey, +) -> Result { + Ok(v2::PutKeysetRequest { + expected_version: record.version, + kdf: record.kdf.clone(), + salt: record.salt.clone(), + wrapped_account_key: record.wrapped_account_key.clone(), + recovery_wrapped_account_key: recovery.wrap(keyset.account_key(), keyset.account_id())?, + unlock_kind: record.unlock_kind, + }) +} + +impl KeysetHub for HubClient { + fn hub_uid(&self) -> String { + self.info().hub_id.clone() + } + + fn record(&self) -> BoxFuture<'_, Result, ClientError>> { + Box::pin(self.keyset_record()) + } + + fn create<'a>( + &'a self, + account_id: &'a str, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(self.create_keyset(account_id, unlock, kdf)) + } + + fn recover<'a>( + &'a self, + account_id: &'a str, + recovery: &'a RecoveryKey, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(self.recover_keyset(account_id, recovery, unlock, kdf)) + } + + fn reseal_recovery<'a>( + &'a self, + keyset: &'a Keyset, + recovery: &'a RecoveryKey, + ) -> BoxFuture<'a, Result> { + Box::pin(reseal(self, keyset, recovery)) + } +} + +async fn reseal( + client: &HubClient, + keyset: &Keyset, + recovery: &RecoveryKey, +) -> Result { + let record = client + .keyset_record() + .await? + .ok_or(KeysetError::Incomplete)?; + let request = resealed(&record, keyset, recovery)?; + let stored = client + .vault() + .put_keyset(request) + .await + .map_err(ClientError::from)?; + Ok(stored.into_inner()) +} diff --git a/crates/hub-io/src/account/lifecycle.rs b/crates/hub-io/src/account/lifecycle.rs new file mode 100644 index 00000000..5e7da847 --- /dev/null +++ b/crates/hub-io/src/account/lifecycle.rs @@ -0,0 +1,76 @@ +//! Nonpersistent native membership and key-lifecycle publication receipts. +use super::source::AccountSubject; +use nocterm_hubs_model::HubError; +use std::sync::{Arc, Weak}; + +#[derive(Clone)] +pub(crate) struct SlotReceipt(Arc<()>); + +impl SlotReceipt { + fn fresh() -> Self { + Self(Arc::new(())) + } + fn matches(&self, expected: &Self) -> bool { + Arc::ptr_eq(&self.0, &expected.0) + } +} + +pub(super) struct SlotAuthority { + owner: Option>, + receipt: SlotReceipt, +} + +impl SlotAuthority { + pub(super) fn new() -> Self { + Self { + owner: None, + receipt: SlotReceipt::fresh(), + } + } + pub(super) fn begin(&mut self, owner: &Arc) { + self.owner = Some(Arc::downgrade(owner)); + self.rotate(); + } + pub(super) fn rotate(&mut self) -> SlotReceipt { + self.receipt = SlotReceipt::fresh(); + self.receipt.clone() + } + pub(super) fn receipt(&self, owner: &Arc) -> Result { + self.check_owner(owner)?; + Ok(self.receipt.clone()) + } + pub(super) fn observed(&self) -> SlotReceipt { + self.receipt.clone() + } + pub(super) fn check_observed(&self, receipt: &SlotReceipt) -> Result<(), HubError> { + if self.receipt.matches(receipt) { + Ok(()) + } else { + Err(HubError::Stale) + } + } + pub(super) fn check_owner(&self, expected: &Arc) -> Result<(), HubError> { + if self + .owner + .as_ref() + .and_then(Weak::upgrade) + .is_some_and(|current| Arc::ptr_eq(¤t, expected)) + { + Ok(()) + } else { + Err(HubError::Stale) + } + } + pub(super) fn check( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result<(), HubError> { + self.check_owner(owner)?; + if self.receipt.matches(receipt) { + Ok(()) + } else { + Err(HubError::Stale) + } + } +} diff --git a/crates/hub-io/src/account/operations.rs b/crates/hub-io/src/account/operations.rs new file mode 100644 index 00000000..dbd02d14 --- /dev/null +++ b/crates/hub-io/src/account/operations.rs @@ -0,0 +1,228 @@ +//! Shared original-account capture for native vault, replica and credential workers. +use super::{ + OpenKeys, Slot, SlotReceipt, + source::{CapturedAccount, CapturedMembership, LocalSourceProvider}, +}; +use crate::{ + pool::Pool, + store::{HubStore, StoreOps}, + vault::Ledger, +}; +use nocterm_hubs_model::{ + HubError, + source_identity::{LocalAccountPartition, LocalAccountSource, LocalAccountSourceError}, +}; +use std::sync::{Arc, Weak}; + +/// The single shared store selector. Production uses the original SDK client; +/// MemoryHub implements this seam in the existing native test-support owner. +pub(crate) trait OperationStores: Send + Sync { + fn opened( + &self, + native: &CapturedAccount, + source: Arc, + ) -> Result, HubError>; + fn membership(&self, native: &CapturedMembership) -> Result, HubError>; +} + +struct PooledStores(Weak); +impl OperationStores for PooledStores { + fn opened( + &self, + native: &CapturedAccount, + source: Arc, + ) -> Result, HubError> { + let pool = self.0.upgrade().ok_or(HubError::Stale)?; + Ok(Arc::new(HubStore::captured(pool, native, source)?)) + } + fn membership(&self, native: &CapturedMembership) -> Result, HubError> { + let pool = self.0.upgrade().ok_or(HubError::Stale)?; + Ok(Arc::new(HubStore::membership(pool, native)?)) + } +} + +/// A caller-provided lease only restricts the independently authenticated capture. +pub(crate) struct OpenedSource { + native: Arc, + restriction: Arc, +} +impl OpenedSource { + pub(crate) fn new( + native: Arc, + restriction: Arc, + ) -> Result, HubError> { + if native.partition() != restriction.partition() { + return Err(HubError::Stale); + } + let source = Arc::new(Self { + native, + restriction, + }); + source.check_current().map_err(|_| HubError::Stale)?; + Ok(source) + } + pub(crate) fn native(&self) -> &Arc { + &self.native + } + pub(crate) fn check(&self) -> Result<(), HubError> { + self.check_current().map_err(|_| HubError::Stale) + } + pub(crate) fn publish(&self, publish: impl FnOnce() -> T) -> Result { + self.restriction + .check_current() + .map_err(|_| HubError::Stale)?; + self.native.publish(publish) + } + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} +impl LocalAccountSource for OpenedSource { + fn partition(&self) -> LocalAccountPartition { + self.native.partition() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + self.native.check_current()?; + self.restriction.check_current() + } +} + +/// A bound facade retains weak key authority; only admitted workers hold keys. +#[derive(Clone)] +pub(crate) struct BoundOperations { + source: Arc, + stores: Arc, +} +impl BoundOperations { + pub(crate) fn source(&self) -> &Arc { + &self.source + } + pub(crate) fn capture(&self) -> Result { + self.source.check()?; + let keys = self.source.native.keys()?; + let ops = self + .stores + .opened(&self.source.native, self.source.clone())?; + self.source.check()?; + Ok(OpenedWork { + source: self.source.clone(), + keys, + ops, + }) + } +} + +pub(crate) struct OpenedWork { + source: Arc, + keys: Arc, + ops: Arc, +} +impl OpenedWork { + pub(crate) fn source(&self) -> &Arc { + &self.source + } + pub(crate) fn store(&self) -> crate::store::Store { + crate::store::Store::new(self.ops.clone(), self.keys.clone()) + } + pub(crate) fn ledger(&self) -> Result { + self.source.check()?; + Ok(Ledger::captured( + self.source.native.slot()?, + self.ops.clone(), + self.source.native.clone(), + )) + } + pub(crate) fn ops(&self) -> &Arc { + &self.ops + } +} + +pub(crate) struct MembershipWork { + membership: Arc, + receipt: SlotReceipt, + ops: Arc, +} +impl MembershipWork { + pub(crate) fn membership(&self) -> &Arc { + &self.membership + } + pub(crate) fn receipt(&self) -> &SlotReceipt { + &self.receipt + } + pub(crate) fn ops(&self) -> &Arc { + &self.ops + } + pub(crate) fn slot(&self) -> Result, HubError> { + self.membership.slot() + } + pub(crate) fn check(&self) -> Result<(), HubError> { + self.membership.with_current(|| { + self.slot()? + .check_receipt(&self.membership.subject()?, &self.receipt) + }) + } +} + +pub(crate) struct AccountOperations { + provider: LocalSourceProvider, + stores: Arc, +} +impl AccountOperations { + pub(crate) fn current_opened_source(&self) -> Result, HubError> { + self.provider.capture_native() + } + pub(crate) fn current_membership(&self) -> Result, HubError> { + self.provider.membership() + } + #[cfg(test)] + pub(crate) fn with_stores( + provider: LocalSourceProvider, + stores: Arc, + ) -> Self { + Self { provider, stores } + } + pub(crate) fn new(provider: LocalSourceProvider, pool: &Arc) -> Self { + Self { + provider, + stores: Arc::new(PooledStores(Arc::downgrade(pool))), + } + } + pub(crate) fn stores(&self) -> &Arc { + &self.stores + } + pub(crate) fn bind( + &self, + restriction: Arc, + ) -> Result { + let source = OpenedSource::new(self.provider.capture_native()?, restriction)?; + Ok(BoundOperations { + source, + stores: self.stores.clone(), + }) + } + pub(crate) fn opened(&self) -> Result { + let native = self.provider.capture_native()?; + let source = OpenedSource::new(native.clone(), native)?; + BoundOperations { + source, + stores: self.stores.clone(), + } + .capture() + } + pub(crate) fn membership(&self) -> Result { + let membership = self.provider.membership()?; + let receipt = + membership.with_current(|| membership.slot()?.receipt(&membership.subject()?))?; + let ops = self.stores.membership(&membership)?; + let work = MembershipWork { + membership, + receipt, + ops, + }; + work.check()?; + Ok(work) + } +} diff --git a/crates/hub-io/src/account/recovery.rs b/crates/hub-io/src/account/recovery.rs new file mode 100644 index 00000000..58741ec7 --- /dev/null +++ b/crates/hub-io/src/account/recovery.rs @@ -0,0 +1,54 @@ +//! Display capabilities retain immutable original recipient/action snapshots. +use super::{KeyFlow, key_admission::KeySnapshot}; +use nocterm_hubs_model::{ + HubError, HubFuture, RecoveryKeyConfirmation, RecoveryKeyIssuer, RecoveryKeyOffer, + RecoveryKeyReissue, +}; +use nocterm_session::Secret; +use std::sync::{Arc, Weak}; + +struct Acknowledgement(KeySnapshot); +impl RecoveryKeyConfirmation for Acknowledgement { + fn check_current(&self) -> Result<(), HubError> { + self.0.check() + } + fn confirm(&self) -> HubFuture<()> { + let snapshot = self.0.clone(); + Box::pin(async move { snapshot.confirm().await }) + } +} +pub(super) fn offer(secret: Secret, snapshot: KeySnapshot) -> RecoveryKeyOffer { + RecoveryKeyOffer::new( + snapshot.member().hub().clone(), + secret, + Arc::new(Acknowledgement(snapshot)), + ) +} + +struct Issuer { + flow: Weak, + snapshot: KeySnapshot, +} +impl RecoveryKeyIssuer for Issuer { + fn check_current(&self) -> Result<(), HubError> { + self.snapshot.check()?; + self.flow.upgrade().map(|_| ()).ok_or(HubError::Stale) + } + fn issue(&self) -> HubFuture { + let Some(flow) = self.flow.upgrade() else { + return Box::pin(async { Err(HubError::Stale) }); + }; + let captured = flow.bind_snapshot(self.snapshot.clone()); + let worker = flow.clone(); + flow.run(captured, move |owner| async move { + let shown = worker.reissue_recovery_key(&owner).await?; + worker.offer(&owner, shown) + }) + } +} +pub(super) fn issuer(flow: Weak, snapshot: KeySnapshot) -> RecoveryKeyReissue { + RecoveryKeyReissue::new( + snapshot.member().hub().clone(), + Arc::new(Issuer { flow, snapshot }), + ) +} diff --git a/crates/hub-io/src/account/refresh.rs b/crates/hub-io/src/account/refresh.rs new file mode 100644 index 00000000..86910bce --- /dev/null +++ b/crates/hub-io/src/account/refresh.rs @@ -0,0 +1,93 @@ +//! Only a successful concurrent personal refresh satisfies waiting readers. +use super::OpenKeys; +use std::sync::atomic::{AtomicU64, Ordering}; +use std::sync::{Arc, Mutex, PoisonError, Weak}; + +#[derive(Default)] +pub(super) struct Refresh { + serial: tokio::sync::Mutex<()>, + completed: AtomicU64, + completed_keys: Mutex>, +} + +pub(crate) struct RefreshTurn<'a> { + owner: &'a Refresh, + _guard: tokio::sync::MutexGuard<'a, ()>, + needed: bool, + keys: Weak, +} + +impl Refresh { + pub(super) async fn enter(&self, keys: &Arc) -> RefreshTurn<'_> { + let before = self.completed.load(Ordering::Acquire); + let guard = self.serial.lock().await; + let same_keys = self + .completed_keys + .lock() + .unwrap_or_else(PoisonError::into_inner) + .upgrade() + .is_some_and(|completed| Arc::ptr_eq(&completed, keys)); + RefreshTurn { + owner: self, + _guard: guard, + needed: !same_keys || before == self.completed.load(Ordering::Acquire), + keys: Arc::downgrade(keys), + } + } +} + +impl RefreshTurn<'_> { + pub(crate) fn needed(&self) -> bool { + self.needed + } + + pub(crate) fn complete(self) { + if self.needed { + *self + .owner + .completed_keys + .lock() + .unwrap_or_else(PoisonError::into_inner) = self.keys; + self.owner.completed.fetch_add(1, Ordering::Release); + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::account::testing::open_keys; + use futures::{FutureExt as _, poll}; + + #[tokio::test] + async fn waiting_readers_reuse_only_a_successful_refresh_of_the_same_open_keys() { + let refresh = Refresh::default(); + let keys = Arc::new(open_keys()); + let leader = refresh.enter(&keys).await; + assert!(leader.needed()); + let mut same = refresh.enter(&keys).boxed(); + let replacement = Arc::new(open_keys()); + let mut next = refresh.enter(&replacement).boxed(); + assert!(poll!(&mut same).is_pending()); + assert!(poll!(&mut next).is_pending()); + leader.complete(); + let follower = same.await; + assert!(!follower.needed()); + follower.complete(); + assert!(next.await.needed()); + } + + #[tokio::test] + async fn cancelled_or_failed_leaders_do_not_satisfy_waiting_readers() { + let refresh = Refresh::default(); + let keys = Arc::new(open_keys()); + let leader = refresh.enter(&keys).await; + let mut waiting = refresh.enter(&keys).boxed(); + assert!(poll!(&mut waiting).is_pending()); + drop(leader); + let retry = waiting.await; + assert!(retry.needed()); + retry.complete(); + assert!(refresh.enter(&keys).await.needed()); + } +} diff --git a/crates/hub-io/src/account/slot.rs b/crates/hub-io/src/account/slot.rs new file mode 100644 index 00000000..37ea02fa --- /dev/null +++ b/crates/hub-io/src/account/slot.rs @@ -0,0 +1,541 @@ +//! One native account Slot owns key lifecycle, original membership and private cache publication. +//! Immutable cache generations separate content contention from lifecycle receipts. +//! A serialized async writer stages on shared bounded workers; admission retains +//! only the final generation check, namespace replacement and memory transition. +use super::cache_publication::CacheDraft; +use super::{ + Notice, OpenKeys, Recovery, SlotReceipt, lifecycle::SlotAuthority, source::AccountSubject, +}; +use crate::{ + cache::{AccountCache, CacheFile}, + classify::credentials, +}; +use nocterm_core::fs::CommittedPublication; +use nocterm_hubs_model::{HubError, HubId}; +use nocterm_vault::VaultStatus; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +pub(crate) struct SlotEnd { + receipt: SlotReceipt, + result: Result<(), HubError>, +} +impl SlotEnd { + pub(crate) fn into_parts(self) -> (SlotReceipt, Result<(), HubError>) { + (self.receipt, self.result) + } +} + +enum KeyState { + Locked, + Open(Arc), + Recovering(Arc), +} + +struct SlotState { + keys: KeyState, + cache: Option>, + authority: SlotAuthority, + pending_recovery: Option, +} + +/// Everything this device knows of one hub's account keys. +pub(crate) struct Slot { + hub: HubId, + file: CacheFile, + notice: Arc, + state: Mutex, + cache_write: tokio::sync::Mutex<()>, + refresh: super::refresh::Refresh, +} + +impl Slot { + pub(crate) fn pending_recovery_for( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result { + let state = self.state(); + state.authority.check(owner, receipt)?; + Ok(state + .cache + .as_ref() + .is_some_and(|cache| cache.account_id == owner.account() && cache.recovery_unconfirmed) + || state + .pending_recovery + .as_ref() + .is_some_and(|pending| state.authority.check_observed(pending).is_ok())) + } + + /// A remote keyset was created, but its first durable local installation + /// failed. Keep only this bounded fact for the original current generation. + pub(crate) fn remember_pending_recovery( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result { + let next = { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + let next = state.authority.rotate(); + state.pending_recovery = Some(next.clone()); + next + }; + self.notice.raise(); + Ok(next) + } + pub(crate) async fn refresh_turn( + &self, + keys: &Arc, + ) -> super::refresh::RefreshTurn<'_> { + self.refresh.enter(keys).await + } + + pub(crate) fn cache_work(&self) -> &Arc { + self.file.work() + } + pub(crate) async fn cache_turn(&self) -> tokio::sync::MutexGuard<'_, ()> { + self.cache_write.lock().await + } + + pub(crate) fn cache_snapshot(&self) -> Option> { + self.state().cache.clone() + } + + pub(crate) async fn stage_cache( + &self, + original: Option>, + next: AccountCache, + ) -> Result { + let file = self.file.clone(); + self.file + .work() + .run(move || { + let publication = file.stage(&next)?; + Ok(CacheDraft::new(original, next, publication)) + }) + .await + .map_err(credentials) + } + + pub(crate) async fn finish_cache(&self, committed: CommittedPublication) { + if let Err(error) = self + .file + .work() + .run(move || { + committed.sync_parent(); + Ok(()) + }) + .await + { + tracing::warn!(%error, "the committed account cache parent cannot be synced"); + } + } + + pub(crate) fn commit_cache_if( + &self, + expected: &Arc, + draft: CacheDraft, + ) -> Result, HubError> { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) + || draft.account() != expected.keyset().account_id() + { + return Err(HubError::Stale); + } + if !draft.matches(&state.cache) { + return Ok(None); + } + let (next, committed) = draft.commit().map_err(credentials)?; + state.cache = Some(next); + Ok(Some(committed)) + } + + /// Ordinary encrypted refresh remains usable when its offline copy cannot + /// be written. Security facts use the durable commit methods exclusively. + pub(crate) fn accept_memory_cache_if( + &self, + expected: &Arc, + original: &Option>, + next: Arc, + ) -> Result { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) + || next.account_id != expected.keyset().account_id() + { + return Err(HubError::Stale); + } + if !super::cache_publication::same_generation(original, &state.cache) { + return Ok(false); + } + state.cache = Some(next); + Ok(true) + } + + pub(crate) fn commit_member_cache( + &self, + owner: &Arc, + receipt: &SlotReceipt, + draft: CacheDraft, + rotate: bool, + ) -> Result, HubError> { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + if draft.account() != owner.account() { + return Err(HubError::Stale); + } + if !draft.matches(&state.cache) { + return Ok(None); + } + let (next, committed) = draft.commit().map_err(credentials)?; + state.cache = Some(next); + state.pending_recovery = None; + let receipt = if rotate { + state.authority.rotate() + } else { + receipt.clone() + }; + Ok(Some((receipt, committed))) + } + + pub(crate) fn commit_install( + &self, + owner: &Arc, + receipt: &SlotReceipt, + keys: Arc, + draft: CacheDraft, + ) -> Result, HubError> { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + if draft.account() != owner.account() || keys.keyset().account_id() != owner.account() { + return Err(HubError::Stale); + } + if !draft.matches(&state.cache) { + return Ok(None); + } + let (next, committed) = draft.commit().map_err(credentials)?; + state.cache = Some(next); + state.keys = KeyState::Open(keys); + state.pending_recovery = None; + Ok(Some((state.authority.rotate(), committed))) + } + + pub(super) fn new(hub: HubId, file: CacheFile, notice: Arc) -> Self { + let cache = match file.load() { + Ok(cache) => cache, + Err(error) => { + tracing::warn!(%error, hub = %hub, "the account cache is not usable; it is read again from the hub"); + None + } + }; + Self { + hub, + file, + notice, + state: Mutex::new(SlotState { + keys: KeyState::Locked, + cache: cache.map(Arc::new), + authority: SlotAuthority::new(), + pending_recovery: None, + }), + cache_write: tokio::sync::Mutex::new(()), + refresh: super::refresh::Refresh::default(), + } + } + + fn state(&self) -> MutexGuard<'_, SlotState> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } + + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + + pub(crate) fn notice(&self) -> &Arc { + &self.notice + } + + /// Called under Pool's clients mutex before a new membership is visible. + pub(crate) fn begin_membership(&self, owner: &Arc) { + { + let mut state = self.state(); + state.keys = KeyState::Locked; + state.authority.begin(owner); + state.pending_recovery = None; + if state + .cache + .as_ref() + .is_some_and(|cache| cache.account_id != owner.account()) + { + state.cache = None; + if let Err(error) = self.file.erase() { + tracing::warn!(%error, "the former account cache cannot be erased"); + } + } + } + self.notice.raise(); + } + + pub(crate) fn receipt(&self, owner: &Arc) -> Result { + self.state().authority.receipt(owner) + } + + pub(crate) fn check_receipt( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result<(), HubError> { + self.state().authority.check(owner, receipt) + } + + pub(crate) fn observed(&self) -> SlotReceipt { + self.state().authority.observed() + } + pub(crate) fn check_observed(&self, receipt: &SlotReceipt) -> Result<(), HubError> { + self.state().authority.check_observed(receipt) + } + pub(crate) fn belongs_to(&self, owner: &Arc) -> Result<(), HubError> { + self.state().authority.check_owner(owner) + } + + pub(crate) fn close_for(&self, owner: &Arc) -> Result { + let next = { + let mut state = self.state(); + state.authority.check_owner(owner)?; + state.keys = KeyState::Locked; + state.authority.rotate() + }; + self.notice.raise(); + Ok(next) + } + + pub(crate) fn end_observed( + &self, + receipt: &SlotReceipt, + erase: bool, + ) -> Result { + let ended = { + let mut state = self.state(); + state.authority.check_observed(receipt)?; + state.keys = KeyState::Locked; + if erase { + state.cache = None; + } + let receipt = state.authority.rotate(); + let result = if erase { + self.file.erase().map_err(credentials) + } else { + Ok(()) + }; + SlotEnd { receipt, result } + }; + self.notice.raise(); + Ok(ended) + } + + pub(crate) fn open_for(&self, owner: &Arc) -> Result, HubError> { + let state = self.state(); + state.authority.check_owner(owner)?; + match &state.keys { + KeyState::Open(keys) if keys.keyset().account_id() == owner.account() => { + Ok(keys.clone()) + } + _ => Err(HubError::KeysLocked), + } + } + + pub(crate) fn cache_for( + &self, + owner: &Arc, + receipt: &SlotReceipt, + ) -> Result { + let state = self.state(); + state.authority.check(owner, receipt)?; + state.cache.as_deref().cloned().ok_or(HubError::NotFound) + } + + pub(super) fn recovering_if( + &self, + owner: &Arc, + receipt: &SlotReceipt, + recovery: Recovery, + ) -> Result { + let next = { + let mut state = self.state(); + state.authority.check(owner, receipt)?; + if recovery.account_id != owner.account() { + return Err(HubError::Stale); + } + state.keys = KeyState::Recovering(Arc::new(recovery)); + state.authority.rotate() + }; + self.notice.raise(); + Ok(next) + } + + pub(crate) fn lock_keys_if(&self, expected: &Arc) -> bool { + { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) { + return false; + } + state.keys = KeyState::Locked; + state.authority.rotate(); + } + self.notice.raise(); + true + } + + pub(crate) fn while_keys_current( + &self, + expected: &Arc, + publish: impl FnOnce() -> T, + ) -> Result { + let state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) { + return Err(HubError::Stale); + } + Ok(publish()) + } + + pub(crate) fn erase_for(&self, owner: &Arc) -> Result<(), HubError> { + let result = { + let mut state = self.state(); + state.authority.check_owner(owner)?; + state.keys = KeyState::Locked; + state.cache = None; + state.authority.rotate(); + self.file.erase().map_err(credentials) + }; + self.notice.raise(); + result + } + + /// The keys, while the vault is unlocked. + pub(crate) fn open(&self) -> Option> { + match &self.state().keys { + KeyState::Open(keys) => Some(Arc::clone(keys)), + _ => None, + } + } + + pub(crate) fn status(&self) -> VaultStatus { + let state = self.state(); + match (&state.keys, &state.cache) { + (KeyState::Open(_), _) => VaultStatus::Unlocked, + (_, Some(_)) => VaultStatus::Locked, + _ => VaultStatus::Missing, + } + } + + /// Reads the cached account, if the hub has one on this device. + pub(crate) fn read_cache(&self, read: impl FnOnce(Option<&AccountCache>) -> R) -> R { + let snapshot = self.state().cache.clone(); + read(snapshot.as_deref()) + } + + /// Uses only the original open account, under the same mutex as cache access. + pub(crate) fn read_cache_if( + &self, + expected: &Arc, + read: impl FnOnce(&AccountCache) -> R, + ) -> Result { + let state = self.state(); + let current = matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)); + let cache = state + .cache + .as_ref() + .filter(|cache| current && cache.account_id == expected.keyset().account_id()) + .ok_or(nocterm_vault::VaultError::Stale)?; + let cache = cache.clone(); + drop(state); + Ok(read(&cache)) + } + + /// Whether the recovery key of `account_id` was shown on this device and never confirmed. + /// A cache of another account says nothing about this one. + pub(crate) fn recovery_unconfirmed(&self, account_id: &str) -> bool { + self.read_cache(|cache| { + cache.is_some_and(|cache| cache.account_id == account_id && cache.recovery_unconfirmed) + }) + } + + /// Opens the vault with `keys`, remembering `cache` on disk. A cache that cannot be + /// written is logged: the account still works, but cannot unlock without the hub. + #[cfg(test)] + pub(crate) fn install(&self, keys: OpenKeys, cache: AccountCache) { + { + let mut state = self.state(); + if let Err(error) = self.file.save(&cache) { + tracing::warn!(%error, hub = %self.hub, "the account cache cannot be written"); + } + state.cache = Some(Arc::new(cache)); + state.keys = KeyState::Open(Arc::new(keys)); + state.authority.rotate(); + } + self.notice.raise(); + } + + /// Opens the vault with `keys` from a cache that is already kept. + #[cfg(test)] + pub(crate) fn unlocked(&self, keys: OpenKeys) { + { + let mut state = self.state(); + state.keys = KeyState::Open(Arc::new(keys)); + state.authority.rotate(); + } + self.notice.raise(); + } + + pub(super) fn recovery(&self) -> Option> { + match &self.state().keys { + KeyState::Recovering(recovery) => Some(Arc::clone(recovery)), + _ => None, + } + } + + /// Drops the keys: the vault is locked, the cache stays. + pub(crate) fn lock(&self) { + let was_unlocked = { + let mut state = self.state(); + let was = !matches!(state.keys, KeyState::Locked); + state.keys = KeyState::Locked; + state.authority.rotate(); + was + }; + if was_unlocked { + self.notice.raise(); + } + } + + /// A captured revoked operation may erase only its original open account. + pub(crate) fn erase_if(&self, expected: &Arc) -> Result<(), HubError> { + let mut state = self.state(); + if !matches!(&state.keys, KeyState::Open(keys) if Arc::ptr_eq(keys, expected)) + || !state + .cache + .as_ref() + .is_some_and(|cache| cache.account_id == expected.keyset().account_id()) + { + return Ok(()); + } + state.keys = KeyState::Locked; + state.cache = None; + state.authority.rotate(); + let result = self.file.erase().map_err(credentials); + drop(state); + self.notice.raise(); + result + } + + /// Drops the keys and erases the cache: the account is signed out of this device. + pub(crate) fn erase(&self) -> Result<(), HubError> { + { + let mut state = self.state(); + state.keys = KeyState::Locked; + state.cache = None; + state.authority.rotate(); + } + self.notice.raise(); + self.file.erase().map_err(credentials) + } +} diff --git a/crates/hub-io/src/account/source.rs b/crates/hub-io/src/account/source.rs new file mode 100644 index 00000000..a0cd2099 --- /dev/null +++ b/crates/hub-io/src/account/source.rs @@ -0,0 +1,160 @@ +//! Captured local-history identity of the signed-in account. +use super::{OpenKeys, Slot}; +use crate::classify::remote; +use nocterm_hubs_model::{ + HubError, HubId, + source_identity::{ + Fingerprint, LegacyConfiguredAccount, LocalAccountPartition, LocalAccountSource, + LocalAccountSourceError, NamespaceId, + }, +}; +use std::sync::{Arc, Weak}; + +mod membership; +pub(crate) use membership::CapturedMembership; + +/// Actual account/device metadata of one installed pool membership. Its Arc +/// identity ends at eviction or a new login, independently from token rotation. +#[derive(PartialEq, Eq)] +pub(crate) struct AccountSubject { + account: String, + device: String, +} +impl AccountSubject { + pub(crate) fn new(account: String, device: String) -> Self { + Self { account, device } + } + pub(crate) fn device(&self) -> &str { + &self.device + } + pub(crate) fn account(&self) -> &str { + &self.account + } +} + +/// Synchronous, non-I/O metadata access to the actual installed client. No device +/// keys, tokens or mutable SDK client cross this consumer-specific boundary. +pub(crate) trait AccountSubjects: Send + Sync { + fn current(&self, hub: &HubId) -> Option>; + fn with_current( + &self, + hub: &HubId, + expected: &Arc, + action: &mut dyn FnMut() -> Result<(), HubError>, + ) -> Result<(), HubError>; +} + +#[derive(Clone)] +pub(crate) struct LocalSourceProvider { + slot: Arc, + subjects: Arc, +} +impl LocalSourceProvider { + pub(crate) fn new(slot: Arc, subjects: Arc) -> Self { + Self { slot, subjects } + } + pub(crate) fn capture(&self) -> Result, HubError> { + self.capture_native() + .map(|source| source as Arc) + } + pub(crate) fn membership(&self) -> Result, HubError> { + CapturedMembership::capture(&self.slot, &self.subjects) + } + pub(crate) fn capture_native(&self) -> Result, HubError> { + self.membership()?.opened() + } +} + +pub(crate) struct CapturedAccount { + slot: Weak, + subjects: Weak, + partition: LocalAccountPartition, + keys: Weak, + subject: Weak, +} +impl CapturedAccount { + /// The callback must not call Slot or Pool; both native ownership locks are held. + pub(crate) fn publish(&self, publish: impl FnOnce() -> T) -> Result { + let membership = self.membership()?; + let expected = self.keys.upgrade().ok_or(HubError::Stale)?; + membership.with_current(|| self.slot()?.while_keys_current(&expected, publish)) + } + + pub(crate) fn membership(&self) -> Result, HubError> { + let slot = self.slot()?; + let subjects = self.subjects.upgrade().ok_or(HubError::Stale)?; + let subject = self.subject()?; + CapturedMembership::published(&slot, &subjects, &subject) + } + + pub(crate) fn with_current( + &self, + action: impl FnOnce(&Arc) -> Result, + ) -> Result { + let membership = self.membership()?; + let expected = self.keys.upgrade().ok_or(HubError::Stale)?; + membership.with_current(|| { + let keys = self.slot()?.open_for(&membership.subject()?)?; + if !Arc::ptr_eq(&keys, &expected) { + return Err(HubError::Stale); + } + action(&keys) + }) + } + pub(crate) fn keys(&self) -> Result, HubError> { + self.check_current().map_err(|_| HubError::NotSignedIn)?; + self.keys.upgrade().ok_or(HubError::KeysLocked) + } + pub(crate) fn slot(&self) -> Result, HubError> { + self.slot.upgrade().ok_or(HubError::NotSignedIn) + } + pub(crate) fn subject(&self) -> Result, HubError> { + self.check_current().map_err(|_| HubError::NotSignedIn)?; + self.subject.upgrade().ok_or(HubError::NotSignedIn) + } +} + +impl LocalAccountSource for CapturedAccount { + fn partition(&self) -> LocalAccountPartition { + self.partition.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + let slot = self.slot.upgrade().ok_or(LocalAccountSourceError::Stale)?; + let subjects = self + .subjects + .upgrade() + .ok_or(LocalAccountSourceError::Stale)?; + let expected_keys = self.keys.upgrade().ok_or(LocalAccountSourceError::Stale)?; + let expected_subject = self + .subject + .upgrade() + .ok_or(LocalAccountSourceError::Stale)?; + let subject = subjects + .current(slot.hub()) + .ok_or(LocalAccountSourceError::Stale)?; + let keys = slot + .open_for(&subject) + .map_err(|_| LocalAccountSourceError::Stale)?; + if !Arc::ptr_eq(&expected_keys, &keys) + || !Arc::ptr_eq(&expected_subject, &subject) + || keys.keyset().account_id() != subject.account() + { + return Err(LocalAccountSourceError::Stale); + } + Ok(()) + } +} + +/// The local history partition of `account` on `hub`. It depends on the account only, never +/// on its keys, so a locked vault keeps the same chats. +pub(crate) fn partition_of(hub: &HubId, account: &str) -> Result { + use sha2::{Digest as _, Sha256}; + let id = NamespaceId::new(account.to_owned()).map_err(remote)?; + let digest: [u8; 32] = Sha256::digest(account.as_bytes()).into(); + Ok(LocalAccountPartition::LegacyConfigured( + LegacyConfiguredAccount::new(hub.clone(), id, Fingerprint::from_bytes(digest)), + )) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/account/source/membership.rs b/crates/hub-io/src/account/source/membership.rs new file mode 100644 index 00000000..d2f4ef72 --- /dev/null +++ b/crates/hub-io/src/account/source/membership.rs @@ -0,0 +1,110 @@ +//! Native login membership remains usable for explicit unlock while keys are locked. +use super::{AccountSubject, AccountSubjects, CapturedAccount, partition_of}; +use crate::account::Slot; +use nocterm_hubs_model::source_identity::{ + LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, +}; +use nocterm_hubs_model::{HubError, HubId}; +use std::sync::{Arc, Weak}; + +pub(crate) struct CapturedMembership { + slot: Weak, + subjects: Weak, + subject: Weak, + hub: HubId, + partition: LocalAccountPartition, +} + +impl CapturedMembership { + pub(super) fn capture( + slot: &Arc, + subjects: &Arc, + ) -> Result, HubError> { + let subject = subjects.current(slot.hub()).ok_or(HubError::NotSignedIn)?; + Self::published(slot, subjects, &subject) + } + + pub(crate) fn published( + slot: &Arc, + subjects: &Arc, + subject: &Arc, + ) -> Result, HubError> { + let captured = Arc::new(Self { + slot: Arc::downgrade(slot), + subjects: Arc::downgrade(subjects), + subject: Arc::downgrade(subject), + hub: slot.hub().clone(), + partition: partition_of(slot.hub(), subject.account())?, + }); + captured.check()?; + Ok(captured) + } + + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + + pub(crate) fn slot(&self) -> Result, HubError> { + self.slot.upgrade().ok_or(HubError::Stale) + } + + pub(crate) fn subject(&self) -> Result, HubError> { + self.subject.upgrade().ok_or(HubError::Stale) + } + + pub(crate) fn check(&self) -> Result<(), HubError> { + self.with_current(|| Ok(())) + } + + pub(crate) fn opened(self: &Arc) -> Result, HubError> { + self.with_current(|| { + let slot = self.slot()?; + let subject = self.subject()?; + let keys = slot.open_for(&subject)?; + let partition = self.partition.clone(); + Ok(Arc::new(CapturedAccount { + slot: Arc::downgrade(&slot), + subjects: self.subjects.clone(), + partition, + keys: Arc::downgrade(&keys), + subject: Arc::downgrade(&subject), + })) + }) + } + + /// The source provider owns admission, including the native Pool mutex. + /// Actions must be synchronous: native ownership cannot change until return. + pub(crate) fn with_current( + &self, + action: impl FnOnce() -> Result, + ) -> Result { + let subjects = self.subjects.upgrade().ok_or(HubError::Stale)?; + let expected = self.subject()?; + let mut action = Some(action); + let mut result = None; + subjects.with_current(&self.hub, &expected, &mut || { + let run = action.take().ok_or(HubError::Stale)?; + result = Some(run()?); + Ok(()) + })?; + result.ok_or(HubError::Stale) + } + + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} + +/// The membership alone is the local history source of a hub window: a locked vault keeps its +/// chats; sign-out or another login makes it stale. +impl LocalAccountSource for CapturedMembership { + fn partition(&self) -> LocalAccountPartition { + self.partition.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + self.check().map_err(|_| LocalAccountSourceError::Stale) + } +} diff --git a/crates/hub-io/src/account/source/tests.rs b/crates/hub-io/src/account/source/tests.rs new file mode 100644 index 00000000..989090f9 --- /dev/null +++ b/crates/hub-io/src/account/source/tests.rs @@ -0,0 +1,269 @@ +use super::*; +use crate::{ + account::{Accounts, testing::*}, + cache::UnlockMethod, +}; +use nocterm_hub_proto::v2; +use nocterm_keys::UnlockKey; + +struct Fixture { + _directory: tempfile::TempDir, + slot: Arc, + subjects: Arc, + provider: LocalSourceProvider, + record: v2::Keyset, + unlock: UnlockKey, +} +impl Fixture { + fn new() -> Self { + let directory = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(directory.path().to_path_buf()); + let slot = accounts.slot(&hub()).unwrap(); + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + let subjects = Arc::new(ScriptedSubjects::for_slot(&slot)); + subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + slot.install( + open(&record, &unlock), + cache(&record, UnlockMethod::Passphrase), + ); + let provider = LocalSourceProvider::new(slot.clone(), subjects.clone()); + Self { + _directory: directory, + slot, + subjects, + provider, + record, + unlock, + } + } + fn capture(&self) -> Arc { + self.provider.capture().unwrap() + } + fn unlock(&self) { + self.slot.unlocked(open(&self.record, &self.unlock)); + } +} + +#[test] +fn local_partition_binds_the_configured_hub_and_the_account_but_not_its_keys() { + let fixture = Fixture::new(); + let source = fixture.capture(); + let LocalAccountPartition::LegacyConfigured(partition) = source.partition() else { + panic!("a hub window's history is partitioned by hub and account") + }; + assert_eq!(partition.hub(), &hub()); + assert_eq!(partition.account().as_str(), ACCOUNT_ID); + assert_eq!(source.check_current(), Ok(())); + let membership = fixture.provider.membership().unwrap(); + fixture.slot.lock(); + assert_eq!( + LocalAccountSource::partition(membership.as_ref()), + source.partition(), + "a locked vault keeps the same chats" + ); + assert_eq!( + LocalAccountSource::check_current(membership.as_ref()), + Ok(()) + ); +} + +#[test] +fn locking_drops_open_keys_despite_retained_history_captures_and_providers() { + let fixture = Fixture::new(); + let keys = Arc::downgrade(&fixture.slot.open().unwrap()); + let source = fixture.capture(); + fixture.slot.lock(); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + assert!( + keys.upgrade().is_none(), + "the source cannot retain the secret owner" + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + let next = fixture.capture(); + assert_eq!(next.partition(), source.partition()); + assert_eq!(next.check_current(), Ok(())); +} + +#[test] +fn a_same_account_relogin_invalidates_capture_even_before_keys_are_reinstalled() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + let next = fixture.capture(); + assert_eq!(next.partition(), original.partition()); + assert_eq!(next.check_current(), Ok(())); +} + +#[test] +fn an_account_round_trip_never_revives_an_old_capture() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture + .subjects + .sign_in(hub(), "another-account", "device-2"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + assert_eq!(fixture.capture().check_current(), Ok(())); +} + +#[test] +fn pool_eviction_invalidates_capture_before_key_erasure_or_remote_work() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture.subjects.evict(&hub()); + assert!( + fixture.slot.open().is_some(), + "sign-out's remote work has not erased keys yet" + ); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::NotSignedIn) + )); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); +} + +#[test] +fn ordinary_refresh_retains_the_capture_but_device_replacement_does_not() { + let fixture = Fixture::new(); + let original = fixture.capture(); + fixture.subjects.renew(hub(), ACCOUNT_ID, "device-1"); + assert_eq!(original.check_current(), Ok(())); + fixture.subjects.renew(hub(), ACCOUNT_ID, "device-2"); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); + fixture.unlock(); + assert_eq!(fixture.capture().partition(), original.partition()); +} + +#[test] +fn replacing_keys_invalidates_capture_but_keeps_its_local_partition() { + let fixture = Fixture::new(); + let original = fixture.capture(); + let (replacement, _) = record(&fixture.unlock); + fixture.slot.install( + open(&replacement, &fixture.unlock), + cache(&replacement, UnlockMethod::Passphrase), + ); + assert_eq!( + original.check_current(), + Err(LocalAccountSourceError::Stale) + ); + assert_eq!(fixture.capture().partition(), original.partition()); +} + +#[test] +fn a_retained_provider_can_wait_for_the_first_unlock_without_capturing_keys() { + let fixture = Fixture::new(); + fixture.slot.lock(); + let provider = LocalSourceProvider::new(fixture.slot.clone(), fixture.subjects.clone()); + assert!(matches!(provider.capture(), Err(HubError::KeysLocked))); + fixture.unlock(); + assert_eq!(provider.capture().unwrap().check_current(), Ok(())); +} + +#[test] +fn a_capture_alone_keeps_neither_the_account_owner_nor_its_open_keys_alive() { + let (source, keys) = { + let fixture = Fixture::new(); + let keys = Arc::downgrade(&fixture.slot.open().unwrap()); + (fixture.capture(), keys) + }; + assert!(keys.upgrade().is_none()); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); +} + +#[tokio::test] +async fn locked_membership_admits_unlock_but_an_explicit_lock_retires_its_receipt() { + let fixture = Fixture::new(); + fixture.slot.lock(); + let membership = fixture.provider.membership().unwrap(); + let subject = membership.subject().unwrap(); + let receipt = membership + .with_current(|| fixture.slot.receipt(&subject)) + .unwrap(); + assert!(membership.check().is_ok()); + fixture.slot.lock(); + let next = cache(&fixture.record, UnlockMethod::Passphrase); + let keys = Arc::new(open(&fixture.record, &fixture.unlock)); + let draft = fixture + .slot + .stage_cache(fixture.slot.cache_snapshot(), next) + .await + .unwrap(); + let result = + membership.with_current(|| fixture.slot.commit_install(&subject, &receipt, keys, draft)); + assert!(matches!(result, Err(HubError::Stale))); + assert!(matches!( + fixture.provider.capture(), + Err(HubError::KeysLocked) + )); +} + +#[tokio::test] +async fn a_late_unlock_never_installs_keys_after_another_login_is_published() { + let fixture = Fixture::new(); + fixture.slot.lock(); + let original = fixture.provider.membership().unwrap(); + let subject = original.subject().unwrap(); + let receipt = fixture.slot.receipt(&subject).unwrap(); + let next = cache(&fixture.record, UnlockMethod::Passphrase); + let keys = Arc::new(open(&fixture.record, &fixture.unlock)); + let draft = fixture + .slot + .stage_cache(fixture.slot.cache_snapshot(), next) + .await + .unwrap(); + fixture.subjects.sign_in(hub(), ACCOUNT_ID, "device-1"); + let result = + original.with_current(|| fixture.slot.commit_install(&subject, &receipt, keys, draft)); + assert!(matches!(result, Err(HubError::Stale))); + assert!(fixture.slot.open().is_none()); + fixture.unlock(); + assert!(fixture.provider.capture().is_ok()); + assert!(matches!(original.check(), Err(HubError::Stale))); +} diff --git a/crates/hub-io/src/account/testing.rs b/crates/hub-io/src/account/testing.rs new file mode 100644 index 00000000..5f7bfe94 --- /dev/null +++ b/crates/hub-io/src/account/testing.rs @@ -0,0 +1,155 @@ +//! Keys for tests, made the way a first sign-in makes them but without a hub. + +use std::sync::Arc; + +use nocterm_hub_client::Keyset; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use nocterm_keys::{AccountKey, KdfParams, RecoveryKey, UnlockKdf, UnlockKey, UnlockSource}; + +use super::{Accounts, OpenKeys, Slot}; +use crate::cache::{AccountCache, UnlockMethod}; + +mod accounts; +mod hub; +mod native_operations; +mod subjects; +pub(crate) use accounts::NativeAccounts; +pub(crate) use native_operations::{NativeVault, native_operations}; + +pub(crate) use hub::MemoryKeysets; +pub(crate) use subjects::ScriptedSubjects; + +pub(crate) const ACCOUNT_ID: &str = "account-1"; +pub(crate) const HUB_UID: &str = "hub-uid"; +pub(crate) const PASSWORD: &str = "correct horse battery"; + +/// Occupies the existing physical cache worker until the shared gate is released. +/// Polling its returned future controls FIFO admission without an I/O injection hook. +pub(crate) struct CacheJobRelease(nocterm_session::testing::Gate); +impl CacheJobRelease { + pub(crate) fn release(&self) { + self.0.release(); + } +} +impl Drop for CacheJobRelease { + fn drop(&mut self) { + self.0.release(); + } +} + +pub(crate) fn blocked_cache_job( + slot: &Slot, +) -> ( + futures::future::BoxFuture<'static, Result<(), crate::cache::CacheError>>, + nocterm_session::testing::Gate, + CacheJobRelease, +) { + let entered = nocterm_session::testing::Gate::pending(); + let release = nocterm_session::testing::Gate::pending(); + let worker_entered = entered.clone(); + let worker_release = release.clone(); + let work = slot.cache_work().clone(); + let job = Box::pin(async move { + work.run(move || { + worker_entered.release(); + futures::executor::block_on(worker_release.wait()); + Ok(()) + }) + .await + }); + (job, entered, CacheJobRelease(release)) +} + +pub(crate) fn kdf() -> UnlockKdf { + UnlockKdf::new(KdfParams::MINIMUM, vec![7; 16]).expect("a valid derivation") +} + +pub(crate) fn unlock(password: &str) -> UnlockKey { + UnlockKey::from_vault_password(password, &kdf()).expect("a usable password") +} + +/// A keyset record, as a first sign-in seals it, at version 0. +pub(crate) fn sealed_record( + account_id: &str, + unlock: &UnlockKey, + kdf: &UnlockKdf, + account_key: &AccountKey, + recovery: &RecoveryKey, +) -> v2::Keyset { + v2::Keyset { + version: 0, + kdf: kdf.description(), + salt: kdf.salt().to_vec(), + wrapped_account_key: account_key.seal(unlock, account_id).unwrap(), + updated_at_ms: 0, + recovery_wrapped_account_key: recovery.wrap(account_key, account_id).unwrap(), + unlock_kind: match unlock.source() { + UnlockSource::LoginPassword => v2::UnlockKind::LoginPassword, + UnlockSource::VaultPassword => v2::UnlockKind::VaultPassword, + } as i32, + } +} + +/// A keyset record sealed under `unlock`, and the recovery key that also opens it. +pub(crate) fn record(unlock: &UnlockKey) -> (v2::Keyset, RecoveryKey) { + let account_key = AccountKey::generate().unwrap(); + let recovery = RecoveryKey::generate().unwrap(); + let mut record = sealed_record(ACCOUNT_ID, unlock, &kdf(), &account_key, &recovery); + record.version = 1; + (record, recovery) +} + +pub(crate) fn open(record: &v2::Keyset, unlock: &UnlockKey) -> OpenKeys { + OpenKeys::new(Keyset::open(record, ACCOUNT_ID, unlock).expect("the record opens")) +} + +pub(crate) fn cache(record: &v2::Keyset, method: UnlockMethod) -> AccountCache { + AccountCache::new(HUB_UID.into(), ACCOUNT_ID.into(), method, record) +} + +/// Open keys on their own, for code that only needs to seal and open collections. +pub(crate) fn open_keys() -> Arc { + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + Arc::new(open(&record, &unlock)) +} + +pub(crate) fn hub() -> HubId { + hub_for("hub.test") +} + +pub(crate) fn hub_for(address: &str) -> HubId { + HubId::from(address.parse::().unwrap()) +} + +/// The slot of the test hub in `accounts`, opened as a sign-in with `PASSWORD` opens it. +pub(crate) fn sign_in(accounts: &Accounts) -> (Arc, v2::Keyset) { + let slot = accounts.slot(&hub()).unwrap(); + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + slot.install( + open(&record, &unlock), + cache(&record, UnlockMethod::Passphrase), + ); + (slot, record) +} + +/// A slot whose vault was opened by a sign-in with `PASSWORD`, kept in `dir`. +pub(crate) fn signed_in(dir: &std::path::Path) -> (Accounts, Arc, v2::Keyset) { + let accounts = Accounts::new(dir.to_path_buf()); + let (slot, record) = sign_in(&accounts); + (accounts, slot, record) +} + +/// Another device of the same account: the same keyset, opened with the same password. +pub(crate) fn second_device(dir: &std::path::Path, record: &v2::Keyset) -> (Accounts, Arc) { + let accounts = Accounts::new(dir.to_path_buf()); + let slot = accounts.slot(&hub()).unwrap(); + let unlock = unlock(PASSWORD); + slot.install( + open(record, &unlock), + cache(record, UnlockMethod::Passphrase), + ); + (accounts, slot) +} diff --git a/crates/hub-io/src/account/testing/accounts.rs b/crates/hub-io/src/account/testing/accounts.rs new file mode 100644 index 00000000..331395b4 --- /dev/null +++ b/crates/hub-io/src/account/testing/accounts.rs @@ -0,0 +1,152 @@ +//! Two actual fake native account owners sharing the production capture/ledger path. +use super::{self as testing, ScriptedSubjects}; +use crate::{ + account::{ + Accounts, OpenKeys, Slot, operations::AccountOperations, source::LocalSourceProvider, + }, + cache::{AccountCache, UnlockMethod}, + store::{ + StoreOps as _, VAULT_KIND, + memory::{MemoryAccountStores, MemoryHub}, + }, + vault::{HubVault, credentials::CredentialCapture}, +}; +use nocterm_hub_client::Keyset; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, HubId, source_identity::LocalAccountSource}; +use nocterm_session::{CredentialId, CredentialStore, Secret, SecretRequest, Target}; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct NativeAccounts { + _dir: tempfile::TempDir, + slot: Arc, + subjects: Arc, + provider: LocalSourceProvider, + operations: Arc, + vault: Arc, + hubs: [Arc; 2], + records: [v2::Keyset; 2], + unlock: nocterm_keys::UnlockKey, +} +impl NativeAccounts { + pub(crate) fn new() -> Self { + Self::at(testing::hub()) + } + pub(crate) fn at(hub: HubId) -> Self { + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + let slot = accounts.slot(&hub).unwrap(); + let unlock = testing::unlock(testing::PASSWORD); + let (a, _) = testing::record(&unlock); + let b = testing::sealed_record( + "account-2", + &unlock, + &testing::kdf(), + &nocterm_keys::AccountKey::generate().unwrap(), + &nocterm_keys::RecoveryKey::generate().unwrap(), + ); + let subjects = Arc::new(ScriptedSubjects::for_slot(&slot)); + let provider = LocalSourceProvider::new(slot.clone(), subjects.clone()); + let hubs = [ + Arc::new(MemoryHub::default()), + Arc::new(MemoryHub::default()), + ]; + let stores = Arc::new(MemoryAccountStores::new([ + (testing::ACCOUNT_ID.into(), hubs[0].clone()), + ("account-2".into(), hubs[1].clone()), + ])); + let operations = Arc::new(AccountOperations::with_stores(provider.clone(), stores)); + let vault = Arc::new(HubVault::new( + Handle::current(), + slot.clone(), + operations.clone(), + )); + let fixture = Self { + _dir: dir, + slot, + subjects, + provider, + operations, + vault, + hubs, + records: [a, b], + unlock, + }; + fixture.login(0); + fixture + } + pub(crate) fn slot(&self) -> &Arc { + &self.slot + } + pub(crate) fn subjects(&self) -> &Arc { + &self.subjects + } + pub(crate) fn hub(&self, index: usize) -> &Arc { + &self.hubs[index] + } + pub(crate) fn operations(&self) -> &Arc { + &self.operations + } + pub(crate) fn vault(&self) -> &Arc { + &self.vault + } + pub(crate) fn source(&self) -> Arc { + self.provider.capture().unwrap() + } + pub(crate) fn capture_credentials( + &self, + source: Arc, + ) -> Result, HubError> { + CredentialCapture::new( + Handle::current(), + self.operations.stores().clone(), + &self.vault.idle(), + ) + .capture(&self.provider, source) + } + pub(crate) fn store(&self) -> Arc { + self.capture_credentials(self.source()).unwrap() + } + pub(crate) fn login(&self, index: usize) { + let account = if index == 0 { + testing::ACCOUNT_ID + } else { + "account-2" + }; + self.subjects + .sign_in(self.slot.hub().clone(), account, "device"); + self.hubs[index].set_keyset(Some(self.records[index].clone())); + self.slot.install( + OpenKeys::new(Keyset::open(&self.records[index], account, &self.unlock).unwrap()), + AccountCache::new( + testing::HUB_UID.into(), + account.into(), + UnlockMethod::Passphrase, + &self.records[index], + ), + ); + } + pub(crate) async fn seed(&self) -> CredentialId { + self.store() + .save( + "A".into(), + &SecretRequest::Password { + target: Target::new("ann", "example.test", 22), + retry: false, + }, + Secret::new("secret A"), + ) + .await + .unwrap() + } + pub(crate) async fn count(&self, index: usize) -> usize { + self.hubs[index] + .collections(VAULT_KIND) + .await + .unwrap() + .iter() + .map(|at| self.hubs[index].objects(&at.collection_id).len()) + .sum() + } +} diff --git a/crates/hub-io/src/account/testing/hub.rs b/crates/hub-io/src/account/testing/hub.rs new file mode 100644 index 00000000..f7da6bd1 --- /dev/null +++ b/crates/hub-io/src/account/testing/hub.rs @@ -0,0 +1,158 @@ +//! The keyset of one account on a pretend hub that several devices share. + +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::future::BoxFuture; +use nocterm_hub_client::{ClientError, Keyset, KeysetError, NewKeyset}; +use nocterm_hub_proto::v2; +use nocterm_keys::{AccountKey, RecoveryKey, UnlockKdf, UnlockKey}; +use tokio::sync::Barrier; + +use super::{HUB_UID, sealed_record}; +use crate::account::keyset_hub::{KeysetHub, resealed}; + +#[derive(Default)] +struct Held { + record: Option, + /// Devices that read "no keyset" wait here for each other before they go on. + meet: Option>, + creations: usize, + /// Writes of the recovery envelope that find the version moved, as if another device wrote + /// the keyset between the read and the write. + contended: usize, +} + +/// What the hub answers a conditional write whose version moved. +fn conflict() -> ClientError { + let mut status = tonic::Status::new(tonic::Code::Aborted, "the keyset changed"); + status.metadata_mut().insert( + "x-nocterm-error", + "ERROR_CODE_CONFLICT".parse().expect("a header value"), + ); + ClientError::from(status) +} + +/// The keyset a hub holds for one account. Clones are devices of the same account: they share +/// what the hub holds. +#[derive(Clone, Default)] +pub(crate) struct MemoryKeysets { + held: Arc>, +} + +impl MemoryKeysets { + fn held(&self) -> MutexGuard<'_, Held> { + self.held.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// The record the hub holds. + pub(crate) fn stored(&self) -> Option { + self.held().record.clone() + } + + /// Makes the first `devices` that find no keyset wait for each other before they create it, + /// so all of them try at once. + pub(crate) fn meet_before_creating(&self, devices: usize) { + self.held().meet = Some(Arc::new(Barrier::new(devices))); + } + + /// Makes the next `writes` replacements of the recovery envelope lose to another device that + /// wrote the keyset first. + pub(crate) fn contend_recovery_writes(&self, writes: usize) { + self.held().contended = writes; + } + + /// How many devices tried to create the keyset. + pub(crate) fn creations(&self) -> usize { + self.held().creations + } +} + +impl KeysetHub for MemoryKeysets { + fn hub_uid(&self) -> String { + HUB_UID.to_owned() + } + + fn record(&self) -> BoxFuture<'_, Result, ClientError>> { + Box::pin(async move { + let (seen, meet) = { + let held = self.held(); + (held.record.clone(), held.meet.clone()) + }; + if let (None, Some(meet)) = (&seen, meet) { + meet.wait().await; + } + Ok(seen) + }) + } + + fn create<'a>( + &'a self, + account_id: &'a str, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(async move { + let mut held = self.held(); + held.creations += 1; + if held.record.is_some() { + return Err(KeysetError::AlreadyExists); + } + let account_key = AccountKey::generate()?; + let recovery = RecoveryKey::generate()?; + let mut record = sealed_record(account_id, unlock, kdf, &account_key, &recovery); + record.version = 1; + let keyset = Keyset::open(&record, account_id, unlock)?; + held.record = Some(record); + Ok(NewKeyset { keyset, recovery }) + }) + } + + fn recover<'a>( + &'a self, + account_id: &'a str, + recovery: &'a RecoveryKey, + unlock: &'a UnlockKey, + kdf: &'a UnlockKdf, + ) -> BoxFuture<'a, Result> { + Box::pin(async move { + let mut held = self.held(); + let record = held.record.clone().ok_or(KeysetError::Incomplete)?; + let keyset = Keyset::recover(&record, account_id, recovery)?; + let mut next = sealed_record(account_id, unlock, kdf, keyset.account_key(), recovery); + next.version = record.version + 1; + let opened = Keyset::open(&next, account_id, unlock)?; + held.record = Some(next); + Ok(opened) + }) + } + + fn reseal_recovery<'a>( + &'a self, + keyset: &'a Keyset, + recovery: &'a RecoveryKey, + ) -> BoxFuture<'a, Result> { + Box::pin(async move { + let mut held = self.held(); + let record = held.record.clone().ok_or(KeysetError::Incomplete)?; + let request = resealed(&record, keyset, recovery)?; + if held.contended > 0 { + held.contended -= 1; + if let Some(stored) = held.record.as_mut() { + stored.version += 1; + } + return Err(KeysetError::Hub(conflict())); + } + let next = v2::Keyset { + version: record.version + 1, + kdf: request.kdf, + salt: request.salt, + wrapped_account_key: request.wrapped_account_key, + updated_at_ms: 0, + recovery_wrapped_account_key: request.recovery_wrapped_account_key, + unlock_kind: request.unlock_kind, + }; + held.record = Some(next.clone()); + Ok(next) + }) + } +} diff --git a/crates/hub-io/src/account/testing/native_operations.rs b/crates/hub-io/src/account/testing/native_operations.rs new file mode 100644 index 00000000..571d1b9a --- /dev/null +++ b/crates/hub-io/src/account/testing/native_operations.rs @@ -0,0 +1,44 @@ +//! Shared real-key/native-account fixtures over MemoryHub and ScriptedSubjects. +use super::{ACCOUNT_ID, PASSWORD, ScriptedSubjects, unlock}; +use crate::{ + account::{OpenKeys, Slot, operations::AccountOperations, source::LocalSourceProvider}, + store::memory::{MemoryAccountStores, MemoryHub}, +}; +use nocterm_hub_client::Keyset; +use std::sync::Arc; + +pub(crate) fn native_operations(slot: &Arc, hub: Arc) -> Arc { + let cached = slot.read_cache(|cache| cache.cloned()); + let was_open = slot.open().is_some(); + let account = cached + .as_ref() + .map_or(ACCOUNT_ID, |cache| cache.account_id.as_str()); + let subjects = Arc::new(ScriptedSubjects::for_slot(slot)); + subjects.sign_in(slot.hub().clone(), account, "device"); + if was_open && let Some(cache) = &cached { + let keyset = Keyset::open(&cache.keyset.record(), account, &unlock(PASSWORD)).unwrap(); + slot.install(OpenKeys::new(keyset), cache.clone()); + } + let stores = Arc::new(MemoryAccountStores::new([(account.to_owned(), hub)])); + let provider = LocalSourceProvider::new(slot.clone(), subjects); + Arc::new(AccountOperations::with_stores(provider, stores)) +} + +/// Drives the real general-vault and bound-replica ports over one MemoryHub. +pub(crate) struct NativeVault { + vault: crate::vault::HubVault, +} +impl NativeVault { + pub(crate) fn new(slot: Arc, hub: Arc) -> Self { + let operations = native_operations(&slot, hub); + Self { + vault: crate::vault::HubVault::new(tokio::runtime::Handle::current(), slot, operations), + } + } +} +impl std::ops::Deref for NativeVault { + type Target = crate::vault::HubVault; + fn deref(&self) -> &Self::Target { + &self.vault + } +} diff --git a/crates/hub-io/src/account/testing/subjects.rs b/crates/hub-io/src/account/testing/subjects.rs new file mode 100644 index 00000000..f86730b6 --- /dev/null +++ b/crates/hub-io/src/account/testing/subjects.rs @@ -0,0 +1,74 @@ +//! One shared fake of the non-I/O account-subject reader, without a hub service. +use crate::account::Slot; +use crate::account::source::{AccountSubject, AccountSubjects}; +use nocterm_hubs_model::{HubError, HubId}; +use std::{ + collections::HashMap, + sync::{Arc, Mutex, Weak}, +}; + +#[derive(Default)] +pub(crate) struct ScriptedSubjects { + subjects: Mutex>>, + slot: Option>, +} +impl ScriptedSubjects { + pub(crate) fn for_slot(slot: &Arc) -> Self { + Self { + subjects: Mutex::default(), + slot: Some(Arc::downgrade(slot)), + } + } + + fn begin(&self, hub: &HubId, subject: &Arc) { + if let Some(slot) = self.slot.as_ref().and_then(Weak::upgrade) + && slot.hub() == hub + { + slot.begin_membership(subject); + } + } + + pub(crate) fn sign_in(&self, hub: HubId, account: &str, device: &str) { + let mut subjects = self.subjects.lock().unwrap(); + let subject = Arc::new(AccountSubject::new(account.into(), device.into())); + self.begin(&hub, &subject); + subjects.insert(hub, subject); + } + pub(crate) fn renew(&self, hub: HubId, account: &str, device: &str) { + let mut subjects = self.subjects.lock().unwrap(); + let next = AccountSubject::new(account.into(), device.into()); + if subjects + .get(&hub) + .is_some_and(|previous| previous.as_ref() == &next) + { + return; + } + let next = Arc::new(next); + self.begin(&hub, &next); + subjects.insert(hub, next); + } + pub(crate) fn evict(&self, hub: &HubId) { + self.subjects.lock().unwrap().remove(hub); + } +} +impl AccountSubjects for ScriptedSubjects { + fn with_current( + &self, + hub: &HubId, + expected: &Arc, + action: &mut dyn FnMut() -> Result<(), HubError>, + ) -> Result<(), HubError> { + let subjects = self.subjects.lock().unwrap(); + if !subjects + .get(hub) + .is_some_and(|current| Arc::ptr_eq(current, expected)) + { + return Err(HubError::Stale); + } + action() + } + + fn current(&self, hub: &HubId) -> Option> { + self.subjects.lock().unwrap().get(hub).cloned() + } +} diff --git a/crates/hub-io/src/account/tests.rs b/crates/hub-io/src/account/tests.rs new file mode 100644 index 00000000..34e16410 --- /dev/null +++ b/crates/hub-io/src/account/tests.rs @@ -0,0 +1,88 @@ +use super::testing::*; +use super::*; +use crate::cache::CacheFile; +use nocterm_vault::VaultStatus; + +#[test] +fn a_signed_in_account_is_unlocked_and_locking_drops_its_keys() { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, _) = signed_in(dir.path()); + assert_eq!(slot.status(), VaultStatus::Unlocked); + let keys = slot.open().unwrap(); + slot.lock(); + assert_eq!(slot.status(), VaultStatus::Locked); + assert!(slot.open().is_none()); + drop(keys); +} + +#[test] +fn locking_and_signing_in_are_announced() { + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + let slot = accounts.slot(&hub()).unwrap(); + let before = accounts.notice().count(); + let unlock = unlock(PASSWORD); + let (record, _) = record(&unlock); + slot.install( + open(&record, &unlock), + cache(&record, UnlockMethod::Passphrase), + ); + assert!(accounts.notice().count() > before); + let opened = accounts.notice().count(); + slot.lock(); + assert!(accounts.notice().count() > opened); + let locked = accounts.notice().count(); + slot.lock(); + assert_eq!( + accounts.notice().count(), + locked, + "locking twice changes nothing" + ); +} + +#[test] +fn a_new_session_finds_the_account_locked_from_its_cache() { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, _slot, _) = signed_in(dir.path()); + let next = Accounts::new(dir.path().to_path_buf()); + let slot = next.slot(&hub()).unwrap(); + assert_eq!(slot.status(), VaultStatus::Locked); + assert!(slot.read_cache(|cache| cache.is_some())); +} + +#[test] +fn an_account_without_a_cache_is_missing() { + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + assert_eq!( + accounts.slot(&hub()).unwrap().status(), + VaultStatus::Missing + ); +} + +#[test] +fn signing_out_erases_the_cache_and_the_keys() { + let dir = tempfile::tempdir().unwrap(); + let (accounts, slot, _) = signed_in(dir.path()); + slot.erase().unwrap(); + assert_eq!(slot.status(), VaultStatus::Missing); + let file = CacheFile::new(dir.path(), hub().address()).unwrap(); + assert!(file.load().unwrap().is_none()); + assert_eq!( + accounts.slot(&hub()).unwrap().status(), + VaultStatus::Missing + ); +} + +#[test] +fn collection_keys_are_separate_and_open_only_their_own_collection() { + let keys = open_keys(); + let (first, first_envelope) = keys.new_collection("c-1").unwrap(); + let (second, second_envelope) = keys.new_collection("c-2").unwrap(); + let sealed = first.seal_object("c-1", "o", "k", b"secret").unwrap(); + assert!(second.open_object("c-1", "o", "k", &sealed).is_err()); + assert_ne!(first_envelope, second_envelope); + // An envelope belongs to its collection: it does not open as another one. + assert!(keys.own_collection("c-3", &first_envelope).is_err()); + assert!(keys.own_collection("c-1", &first_envelope).is_ok()); +} diff --git a/crates/hub-io/src/account_keys.rs b/crates/hub-io/src/account_keys.rs new file mode 100644 index 00000000..fb86c1ca --- /dev/null +++ b/crates/hub-io/src/account_keys.rs @@ -0,0 +1,105 @@ +//! The account key lock over the hub links: whether the keys of an account are open on this +//! device, opening them with the typed password, and closing them. +//! +//! It acts on the same account slot as the vault, so unlocking here opens the vault and +//! locking closes it. It does not own the vault's idle timer: only vault use restarts that. +//! Status/lock/unlock admission use only an already loaded native Slot. An +//! unloaded Slot has no current native membership or open keys; unlock captures +//! that original membership before its existing background worker starts I/O. + +use std::sync::Arc; + +use futures::FutureExt as _; +use nocterm_core::error::SharedCause; +use nocterm_hubs_model::{AccountKeyError, AccountKeyFuture, AccountKeyLock, HubError, HubId}; +use nocterm_session::Secret; +use nocterm_vault::{VaultAccess as _, VaultError}; + +use crate::links::HubLinks; + +/// Implements [`AccountKeyLock`] over the hub links. +pub struct AccountKeys { + links: Arc, +} + +impl AccountKeys { + pub fn new(links: Arc) -> Self { + Self { links } + } +} + +impl AccountKeyLock for AccountKeys { + fn is_open(&self, hub: &HubId) -> bool { + self.links.account_keys_open(hub) + } + + fn unlock(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()> { + match self.links.loaded_account(hub) { + Some(account) => { + let unlocking = account.vault().unlock(password); + async move { unlocking.await.map_err(failure) }.boxed() + } + None => async move { Err(sign_in_failure(HubError::NotSignedIn)) }.boxed(), + } + } + + fn lock(&self, hub: &HubId) { + if let Some(account) = self.links.loaded_account(hub) { + account.vault().lock(); + } + } +} + +fn failure(error: VaultError) -> AccountKeyError { + match error { + VaultError::Authentication => AccountKeyError::WrongPassword, + VaultError::Offline => AccountKeyError::Offline, + VaultError::Stale | VaultError::Cancelled => AccountKeyError::NotSignedIn, + other => AccountKeyError::Failed { + cause: SharedCause::new(other), + }, + } +} + +fn sign_in_failure(error: HubError) -> AccountKeyError { + match error { + HubError::NotSignedIn | HubError::Stale | HubError::NotFound => { + AccountKeyError::NotSignedIn + } + other => AccountKeyError::Failed { + cause: SharedCause::new(other), + }, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_wrong_password_and_an_unreachable_hub_are_told_apart() { + assert!(matches!( + failure(VaultError::Authentication), + AccountKeyError::WrongPassword + )); + assert!(matches!( + failure(VaultError::Offline), + AccountKeyError::Offline + )); + assert!(matches!( + failure(VaultError::Stale), + AccountKeyError::NotSignedIn + )); + let other = failure(VaultError::Format); + assert!(matches!(other, AccountKeyError::Failed { .. })); + assert!(std::error::Error::source(&other).is_some()); + } + + #[test] + fn a_hub_without_a_sign_in_is_not_signed_in() { + assert!(matches!( + sign_in_failure(HubError::NotSignedIn), + AccountKeyError::NotSignedIn + )); + } +} diff --git a/crates/hub-io/src/agents.rs b/crates/hub-io/src/agents.rs new file mode 100644 index 00000000..42dbf64a --- /dev/null +++ b/crates/hub-io/src/agents.rs @@ -0,0 +1,220 @@ +//! The agents, providers, agent key and agent servers of the account, over `AgentService` and +//! `ServerService`, as the ports of `nocterm-hub-agents-model`. +//! +//! Every call captures the account's original membership before it returns its future and +//! checks it again after every await, like the devices of the account: a result for an +//! account that signed out or was replaced meanwhile is `HubError::Stale`. + +mod convert; +mod servers; + +pub(crate) use convert::server as hub_server; + +use std::sync::Arc; + +use nocterm_hub_agents_model::{ + AgentDraft, AgentId, AgentsFuture, HubAgent, HubAgentDirectory, HubAgentsError, Provider, + ProviderDraft, ProviderId, ProviderTest, +}; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use tokio::runtime::Handle; + +use crate::classify::classify; +use crate::devices::{Captured, run}; +use crate::links::HubLinks; +use crate::pool::Pool; + +/// Implements the agents ports over the clients of the hubs this device is signed in to. +pub struct HubAgentsLink { + handle: Handle, + pool: Arc, + hints: Arc, +} + +impl HubAgentsLink { + pub fn new(links: &HubLinks) -> Self { + let (handle, pool) = links.agent_parts(); + Self { + handle, + pool, + hints: links.chat_hints(), + } + } + + /// Runs `call` with a fresh client of the account on `hub`. + fn call(&self, hub: &HubId, call: F) -> AgentsFuture + where + T: Send + 'static, + F: FnOnce(HubClient) -> Fut + Send + 'static, + Fut: std::future::Future> + Send + 'static, + { + let captured = Captured::new(self.pool.clone(), hub); + let outcome = run(captured, &self.handle, move |captured| async move { + let client = captured.client.clone(); + let address = captured.hub.address().clone(); + let fresh = captured.observed.during(client.ensure_fresh()).await?; + if let Err(error) = fresh { + return Ok(Err(HubAgentsError::Hub(classify(&address, error)))); + } + let result = captured.observed.during(call(client)).await?; + Ok(result.map_err(|error| error.into_agents(&address))) + }); + Box::pin(async move { outcome.await.map_err(HubAgentsError::Hub)? }) + } +} + +/// A failed call before it is told in the port's terms. +pub(crate) enum CallError { + Client(ClientError), + Agents(HubAgentsError), +} + +impl From for CallError { + fn from(status: tonic::Status) -> Self { + Self::Client(ClientError::from(status)) + } +} + +impl From for CallError { + fn from(error: HubAgentsError) -> Self { + Self::Agents(error) + } +} + +impl CallError { + fn into_agents(self, address: &nocterm_hubs_model::HubAddress) -> HubAgentsError { + match self { + Self::Agents(error) => error, + Self::Client(ClientError::Status { + hub_code: Some(v2::ErrorCode::LimitReached), + .. + }) => HubAgentsError::LimitReached, + Self::Client(error) => HubAgentsError::Hub(classify(address, error)), + } + } +} + +impl HubAgentDirectory for HubAgentsLink { + fn agents(&self, hub: &HubId) -> AgentsFuture> { + self.call(hub, |client| async move { + let listed = client + .agents() + .list_agents(v2::Empty {}) + .await? + .into_inner(); + listed + .agents + .into_iter() + .map(convert::agent) + .collect::>() + .map_err(CallError::from) + }) + } + + fn create_agent(&self, hub: &HubId, draft: AgentDraft) -> AgentsFuture { + self.call(hub, |client| async move { + let input = convert::agent_input(draft.validate().map_err(HubAgentsError::from)?); + let made = client.agents().create_agent(input).await?.into_inner(); + Ok(convert::agent(made)?) + }) + } + + fn update_agent( + &self, + hub: &HubId, + agent: &AgentId, + draft: AgentDraft, + ) -> AgentsFuture { + let id = agent.to_string(); + self.call(hub, |client| async move { + let input = convert::agent_input(draft.validate().map_err(HubAgentsError::from)?); + let request = v2::UpdateAgentRequest { + id, + agent: Some(input), + }; + let saved = client.agents().update_agent(request).await?.into_inner(); + Ok(convert::agent(saved)?) + }) + } + + fn delete_agent(&self, hub: &HubId, agent: &AgentId) -> AgentsFuture<()> { + let id = agent.to_string(); + self.call(hub, |client| async move { + client.agents().delete_agent(v2::IdRequest { id }).await?; + Ok(()) + }) + } + + fn providers(&self, hub: &HubId) -> AgentsFuture> { + self.call(hub, |client| async move { + let listed = client + .agents() + .list_providers(v2::Empty {}) + .await? + .into_inner(); + listed + .providers + .into_iter() + .map(convert::provider) + .collect::>() + .map_err(CallError::from) + }) + } + + fn create_provider(&self, hub: &HubId, draft: ProviderDraft) -> AgentsFuture { + self.call(hub, |client| async move { + let input = convert::provider_input(draft.validate().map_err(HubAgentsError::from)?); + let made = client.agents().create_provider(input).await?.into_inner(); + Ok(convert::provider(made)?) + }) + } + + fn update_provider( + &self, + hub: &HubId, + provider: &ProviderId, + draft: ProviderDraft, + ) -> AgentsFuture { + let id = provider.to_string(); + self.call(hub, |client| async move { + let input = convert::provider_input(draft.validate().map_err(HubAgentsError::from)?); + let request = v2::UpdateProviderRequest { + id, + provider: Some(input), + }; + let saved = client.agents().update_provider(request).await?.into_inner(); + Ok(convert::provider(saved)?) + }) + } + + fn delete_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture<()> { + let id = provider.to_string(); + self.call(hub, |client| async move { + client + .agents() + .delete_provider(v2::IdRequest { id }) + .await?; + Ok(()) + }) + } + + fn test_provider(&self, hub: &HubId, provider: &ProviderId) -> AgentsFuture { + let id = provider.to_string(); + self.call(hub, |client| async move { + let request = v2::TestProviderRequest { + target: Some(v2::test_provider_request::Target::ProviderId(id)), + }; + let tested = client.agents().test_provider(request).await?.into_inner(); + Ok(ProviderTest { + ok: tested.ok, + message: tested.message, + models: tested.models, + }) + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/agents/convert.rs b/crates/hub-io/src/agents/convert.rs new file mode 100644 index 00000000..67dde92e --- /dev/null +++ b/crates/hub-io/src/agents/convert.rs @@ -0,0 +1,129 @@ +//! Agent, provider and server records of the hub protocol as the records of the model. + +use nocterm_hub_agents_model::{ + AgentDraft, AgentId, AgentKey, ApprovalMode, HubAgent, HubAgentsError, HubServer, Provider, + ProviderDraft, ProviderId, ProviderKind, ProviderScope, ServerId, ServerStatus, +}; +use nocterm_hub_proto::v2; + +fn malformed(_: T) -> HubAgentsError { + HubAgentsError::Malformed +} + +pub(super) fn approval(wire: i32) -> ApprovalMode { + match v2::ApprovalMode::try_from(wire) { + Ok(v2::ApprovalMode::Ask) => ApprovalMode::Ask, + _ => ApprovalMode::Auto, + } +} + +pub(super) fn approval_to_wire(mode: ApprovalMode) -> i32 { + match mode { + ApprovalMode::Auto => v2::ApprovalMode::Auto, + ApprovalMode::Ask => v2::ApprovalMode::Ask, + } + .into() +} + +fn server_ids(ids: Vec) -> Result, HubAgentsError> { + ids.into_iter() + .map(|id| ServerId::new(id).map_err(malformed)) + .collect() +} + +pub(super) fn agent(wire: v2::Agent) -> Result { + Ok(HubAgent { + id: AgentId::new(wire.id).map_err(malformed)?, + name: wire.name, + instructions: wire.instructions, + provider: ProviderId::new(wire.provider_id).map_err(malformed)?, + model: wire.model, + models: wire.models, + approval: approval(wire.approval), + servers: server_ids(wire.server_ids)?, + }) +} + +/// A validated draft as the hub takes it. +pub(super) fn agent_input(draft: AgentDraft) -> v2::AgentInput { + v2::AgentInput { + name: draft.name, + instructions: draft.instructions, + provider_id: draft + .provider + .map(|provider| provider.to_string()) + .unwrap_or_default(), + model: draft.model, + models: draft.models, + approval: approval_to_wire(draft.approval), + server_ids: draft.servers.iter().map(ToString::to_string).collect(), + max_steps: 0, + } +} + +pub(super) fn provider(wire: v2::Provider) -> Result { + Ok(Provider { + id: ProviderId::new(wire.id).map_err(malformed)?, + scope: match v2::Scope::try_from(wire.scope) { + Ok(v2::Scope::Server) => ProviderScope::Hub, + _ => ProviderScope::Account, + }, + kind: match v2::ProviderKind::try_from(wire.kind) { + Ok(v2::ProviderKind::Anthropic) => ProviderKind::Anthropic, + _ => ProviderKind::OpenAi, + }, + name: wire.name, + base_url: wire.base_url, + models: wire.models, + has_key: wire.has_key, + editable: wire.editable, + }) +} + +/// A provider of the account's own, as the hub takes it. The key goes once and is never read +/// back. +pub(super) fn provider_input(draft: ProviderDraft) -> v2::ProviderInput { + v2::ProviderInput { + scope: v2::Scope::Account.into(), + kind: match draft.kind { + ProviderKind::OpenAi => v2::ProviderKind::Openai, + ProviderKind::Anthropic => v2::ProviderKind::Anthropic, + } + .into(), + name: draft.name, + base_url: draft.base_url, + models: draft.models, + api_key: draft + .api_key + .map(|key| key.expose().to_owned()) + .unwrap_or_default(), + } +} + +pub(super) fn agent_key(wire: v2::AgentKey) -> AgentKey { + AgentKey { + public_key: wire.public_key, + authorized_line: wire.authorized_line, + fingerprint: wire.fingerprint, + } +} + +pub(crate) fn server(wire: v2::Server) -> Result { + Ok(HubServer { + id: ServerId::new(wire.id).map_err(malformed)?, + name: wire.name, + host: wire.host, + port: u16::try_from(wire.port).map_err(malformed)?, + user: wire.user, + host_key_fingerprint: wire.host_key_fingerprint, + status: match v2::ServerStatus::try_from(wire.status) { + Ok(v2::ServerStatus::Ready) => ServerStatus::Ready, + Ok(v2::ServerStatus::NoAccess) => ServerStatus::NoAccess, + Ok(v2::ServerStatus::HostKeyChanged) => ServerStatus::HostKeyChanged, + Ok(v2::ServerStatus::Unreachable) => ServerStatus::Unreachable, + Ok(v2::ServerStatus::KeyRotated) => ServerStatus::KeyRotated, + _ => ServerStatus::Unchecked, + }, + status_message: wire.status_message, + }) +} diff --git a/crates/hub-io/src/agents/servers.rs b/crates/hub-io/src/agents/servers.rs new file mode 100644 index 00000000..af84f0a2 --- /dev/null +++ b/crates/hub-io/src/agents/servers.rs @@ -0,0 +1,137 @@ +//! The agent key and agent servers of the account, over `ServerService`. + +use nocterm_hub_agents_model::{ + AgentKey, AgentsFuture, HubAgentsError, HubChange, HubChangeHints, HubChanges, HubServer, + HubServers, NewServer, ServerId, ServerRemoval, +}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; + +use super::{CallError, HubAgentsLink, convert}; + +impl HubServers for HubAgentsLink { + fn agent_key(&self, hub: &HubId) -> AgentsFuture { + self.call(hub, |client| async move { + let key = client.servers().get_agent_key(v2::Empty {}).await?; + Ok(convert::agent_key(key.into_inner())) + }) + } + + fn rotate_agent_key(&self, hub: &HubId) -> AgentsFuture { + self.call(hub, |client| async move { + let key = client.servers().rotate_agent_key(v2::Empty {}).await?; + Ok(convert::agent_key(key.into_inner())) + }) + } + + fn servers(&self, hub: &HubId) -> AgentsFuture> { + self.call(hub, |client| async move { + let listed = client.servers().list_servers(v2::Empty {}).await?; + listed + .into_inner() + .servers + .into_iter() + .map(convert::server) + .collect::>() + .map_err(CallError::from) + }) + } + + fn add_server(&self, hub: &HubId, server: NewServer) -> AgentsFuture { + self.call(hub, move |client| async move { + let request = v2::AddServerRequest { + name: server.name, + host: server.host, + port: server.port.into(), + user: server.user, + host_key: server.host_key, + }; + let added = client.servers().add_server(request).await?; + Ok(convert::server(added.into_inner())?) + }) + } + + fn check_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let id = server.to_string(); + self.call(hub, |client| async move { + let checked = client.servers().check_server(v2::IdRequest { id }).await?; + Ok(convert::server(checked.into_inner())?) + }) + } + + fn remove_server(&self, hub: &HubId, server: &ServerId) -> AgentsFuture { + let id = server.to_string(); + self.call(hub, |client| async move { + let request = v2::RemoveServerRequest { + id, + credential: None, + }; + let removed = client.servers().remove_server(request).await?.into_inner(); + Ok(ServerRemoval { + line_removed: removed.line_removed, + message: removed.message, + }) + }) + } +} + +/// Hints from one `Watch` of the account (no open chats) and from the chat lists this device +/// refreshed. +struct ChangeHints { + hub: tonic::Streaming, + here: tokio::sync::watch::Receiver, +} + +/// The hint of an event of the account's stream, if it is one. +fn hint_of(event: v2::watch_event::Event) -> Option { + use v2::watch_event::Event; + match event { + Event::Chat(_) | Event::ChatDeleted(_) | Event::Resync(_) => Some(HubChange::Chats), + Event::Account(changed) => match changed.list() { + v2::AccountList::Agents | v2::AccountList::Providers => Some(HubChange::Agents), + v2::AccountList::Servers => Some(HubChange::Servers), + v2::AccountList::Unspecified => None, + }, + Event::Item(_) | Event::Draft(_) | Event::Ready(_) => None, + } +} + +impl HubChangeHints for ChangeHints { + fn next( + &mut self, + ) -> futures::future::BoxFuture<'_, Result, HubAgentsError>> { + Box::pin(async move { + loop { + tokio::select! { + changed = self.here.changed() => { + if changed.is_ok() { + return Ok(Some(HubChange::Chats)); + } + } + message = self.hub.message() => match message { + Ok(Some(event)) => { + if let Some(hint) = event.event.and_then(hint_of) { + return Ok(Some(hint)); + } + } + Ok(None) | Err(_) => return Ok(None), + }, + } + } + }) + } +} + +impl HubChanges for HubAgentsLink { + fn watch(&self, hub: &HubId) -> AgentsFuture> { + let here = self.hints.subscribe(); + self.call(hub, |client| async move { + let request = v2::WatchRequest { + chats_after_rev: 0, + open: Vec::new(), + }; + let hub = client.chats().watch(request).await?.into_inner(); + Ok(Box::new(ChangeHints { hub, here }) as Box) + }) + } +} diff --git a/crates/hub-io/src/agents/tests.rs b/crates/hub-io/src/agents/tests.rs new file mode 100644 index 00000000..7186ad42 --- /dev/null +++ b/crates/hub-io/src/agents/tests.rs @@ -0,0 +1,61 @@ +use nocterm_hub_agents_model::{AgentDraft, ApprovalMode, ProviderId, ServerStatus}; +use nocterm_hub_proto::v2; + +use super::convert; + +#[test] +fn an_agent_draft_goes_to_the_hub_with_its_mode_and_servers() { + let draft = AgentDraft { + name: "ops".into(), + provider: Some(ProviderId::new("p1").unwrap()), + models: vec!["m1".into()], + approval: ApprovalMode::Ask, + servers: vec![nocterm_hub_agents_model::ServerId::new("s1").unwrap()], + ..AgentDraft::default() + } + .validate() + .unwrap(); + let input = convert::agent_input(draft); + assert_eq!(input.provider_id, "p1"); + assert_eq!(input.model, "m1"); + assert_eq!(input.approval, i32::from(v2::ApprovalMode::Ask)); + assert_eq!(input.server_ids, ["s1"]); +} + +#[test] +fn an_agent_of_the_hub_reads_back_with_its_mode() { + let agent = convert::agent(v2::Agent { + id: "a1".into(), + name: "ops".into(), + provider_id: "p1".into(), + model: "m1".into(), + models: vec!["m1".into()], + approval: v2::ApprovalMode::Ask.into(), + ..Default::default() + }) + .unwrap(); + assert_eq!(agent.approval, ApprovalMode::Ask); + assert!(convert::agent(v2::Agent::default()).is_err()); +} + +#[test] +fn a_server_reads_back_with_its_status() { + let server = convert::server(v2::Server { + id: "s1".into(), + host: "prod".into(), + port: 22, + user: "root".into(), + status: v2::ServerStatus::KeyRotated.into(), + ..Default::default() + }) + .unwrap(); + assert_eq!(server.status, ServerStatus::KeyRotated); + assert!( + convert::server(v2::Server { + id: "s".into(), + port: 70_000, + ..Default::default() + }) + .is_err() + ); +} diff --git a/crates/hub-io/src/cache.rs b/crates/hub-io/src/cache.rs new file mode 100644 index 00000000..bde922ba --- /dev/null +++ b/crates/hub-io/src/cache.rs @@ -0,0 +1,347 @@ +//! The encrypted copy of an account's keyset and vault kept on this device. +//! +//! One mode-0600 file per hub, `_.cache` beside the credential file. It holds only +//! what the hub holds: the sealed keyset, the sealed key envelope of the vault collection and +//! sealed objects, plus the non-secret facts needed to derive the unlock key again (the user +//! name, the way the key is derived and the hub's own identifier). Nothing secret in it opens +//! without the vault password, so it may sit on disk, and it lets the vault unlock while the hub +//! is unreachable. +//! Read and write share a 32 MiB byte limit. Personal-vault refresh publication +//! keeps the newest collection cursor and skips identical snapshots. Vault data +//! is shared immutably across readers; staging and parent sync use two physical +//! installation-owned workers, independent of the Slot's short authority lock. + +use std::collections::BTreeMap; +use std::io; +use std::path::PathBuf; +use std::sync::Arc; + +use nocterm_core::{ + bounded_io::BoundedBuffer, + fs::{AtomicPublication, BoundedReadError, FileError, read_regular_bounded}, +}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubAddress; +use serde::{Deserialize, Serialize}; + +use crate::credentials::{MAX_NAME_BYTES, file_stem, prepare_private_directory}; + +/// The cache holds sealed credentials of one account; anything larger is not ours. +const MAX_FILE_BYTES: usize = 32 * 1024 * 1024; +/// The format the file is written in. A newer file is left alone and not read. +const FORMAT: u32 = 2; + +/// Why the cache of a hub could not be read or kept. +#[derive(Debug, thiserror::Error)] +pub(crate) enum CacheError { + #[error("the account cache cannot be read")] + Read(#[from] BoundedReadError), + #[error("the account cache cannot be written")] + Write(#[from] FileError), + #[error("the account cache directory cannot be prepared")] + Prepare(#[source] io::Error), + #[error("the account cache is not valid")] + Format(#[source] serde_json::Error), + #[error("the account cache was written by a newer version")] + Newer, + #[error("the account cache exceeds its 32 MiB limit")] + Limit, + #[error("the hub address is too long for a file name")] + NameTooLong, + #[error("the account cache worker is unavailable")] + Worker, + #[error("the account cache worker failed")] + Join(#[from] tokio::task::JoinError), +} + +/// How the unlock key of the account's keyset is derived from what the user types. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(tag = "kind", rename_all = "snake_case")] +pub(crate) enum UnlockMethod { + /// The login password of the account, bound to the hub and the user name. + Login { username: String }, + /// A vault passphrase of its own (accounts that sign in through a provider). + Passphrase, +} + +/// Bytes as base64 text. +mod bytes { + use base64::{Engine as _, engine::general_purpose::STANDARD}; + use serde::{Deserialize, Deserializer, Serializer}; + + pub(super) fn serialize(value: &[u8], out: S) -> Result { + out.serialize_str(&STANDARD.encode(value)) + } + + pub(super) fn deserialize<'de, D: Deserializer<'de>>(input: D) -> Result, D::Error> { + let text = String::deserialize(input)?; + STANDARD.decode(text).map_err(serde::de::Error::custom) + } +} + +/// The sealed keyset as the hub stores it. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct KeysetCopy { + pub version: u64, + pub kdf: String, + #[serde(with = "bytes")] + pub salt: Vec, + #[serde(with = "bytes")] + pub wrapped_account_key: Vec, + #[serde(with = "bytes")] + pub recovery_wrapped_account_key: Vec, + /// Which password the unlock key comes from, as the hub's `UnlockKind` number. + pub unlock_kind: i32, +} + +impl KeysetCopy { + pub(crate) fn of(record: &v2::Keyset) -> Self { + Self { + version: record.version, + kdf: record.kdf.clone(), + salt: record.salt.clone(), + wrapped_account_key: record.wrapped_account_key.clone(), + recovery_wrapped_account_key: record.recovery_wrapped_account_key.clone(), + unlock_kind: record.unlock_kind, + } + } + + pub(crate) fn record(&self) -> v2::Keyset { + v2::Keyset { + version: self.version, + kdf: self.kdf.clone(), + salt: self.salt.clone(), + wrapped_account_key: self.wrapped_account_key.clone(), + updated_at_ms: 0, + recovery_wrapped_account_key: self.recovery_wrapped_account_key.clone(), + unlock_kind: self.unlock_kind, + } + } +} + +/// One sealed object (or tombstone) of the vault collection. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct ObjectCopy { + pub version: u64, + pub kind: String, + #[serde(with = "bytes")] + pub ciphertext: Vec, + pub deleted: bool, +} + +/// The vault collection: its key envelope and every object read so far. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct VaultCopy { + pub collection_id: String, + #[serde(with = "bytes")] + pub key_envelope: Vec, + /// The stream version up to which `objects` is complete. + pub cursor: u64, + pub objects: BTreeMap, +} + +/// What the cache file holds. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct AccountCache { + pub v: u32, + /// The hub's own identifier, which the login key is derived with. + pub hub_uid: String, + pub account_id: String, + pub method: UnlockMethod, + pub keyset: KeysetCopy, + #[serde( + default, + skip_serializing_if = "Option::is_none", + with = "shared_vault" + )] + pub vault: Option>, + /// The keys were created on this device and the person has not yet confirmed saving the + /// recovery key shown then. A fact about the device, not a secret: the next time the keys + /// open, a new recovery key is offered. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub recovery_unconfirmed: bool, +} + +impl AccountCache { + /// A delayed refresh never replaces a more recent personal-vault snapshot. + /// Equal snapshots need neither another memory publication nor a disk write. + pub(crate) fn accept_shared_vault(&mut self, next: Arc) -> bool { + if self.vault.as_ref().is_some_and(|held| { + held == &next || (held.collection_id == next.collection_id && held.cursor > next.cursor) + }) { + return false; + } + self.vault = Some(next); + true + } + + pub(crate) fn new( + hub_uid: String, + account_id: String, + method: UnlockMethod, + record: &v2::Keyset, + ) -> Self { + Self { + v: FORMAT, + hub_uid, + account_id, + method, + keyset: KeysetCopy::of(record), + vault: None, + recovery_unconfirmed: false, + } + } +} + +/// Sharing is an in-memory ownership detail; the existing file representation stays plain. +mod shared_vault { + use super::{Arc, VaultCopy}; + use serde::{Deserialize, Deserializer, Serialize, Serializer}; + + pub(super) fn serialize( + value: &Option>, + out: S, + ) -> Result { + value.as_deref().serialize(out) + } + + pub(super) fn deserialize<'de, D: Deserializer<'de>>( + input: D, + ) -> Result>, D::Error> { + Option::::deserialize(input).map(|value| value.map(Arc::new)) + } +} + +/// The cache file of one hub. It keeps no state: callers decide when to read and write. +#[derive(Clone, Debug)] +pub(crate) struct CacheFile { + path: PathBuf, + work: Arc, +} + +/// One installation shares two physical filesystem jobs. A cancelled waiter +/// releases admission; a running job retains its permit until the I/O finishes. +#[derive(Debug)] +pub(crate) struct CacheWork { + runtime: Option, + slots: Arc, + operations: Arc, +} + +impl CacheWork { + pub(crate) fn new(runtime: Option) -> Arc { + Arc::new(Self { + runtime, + slots: Arc::new(tokio::sync::Semaphore::new(2)), + operations: Arc::new(tokio::sync::Semaphore::new(4)), + }) + } + + pub(crate) async fn operation(&self) -> Result { + self.operations + .clone() + .acquire_owned() + .await + .map_err(|_| CacheError::Worker) + } + + pub(crate) async fn run( + &self, + work: impl FnOnce() -> Result + Send + 'static, + ) -> Result { + let permit = self + .slots + .clone() + .acquire_owned() + .await + .map_err(|_| CacheError::Worker)?; + let runtime = self + .runtime + .clone() + .or_else(|| tokio::runtime::Handle::try_current().ok()) + .ok_or(CacheError::Worker)?; + runtime + .spawn_blocking(move || { + let _permit = permit; + work() + }) + .await? + } +} + +impl CacheFile { + pub(crate) fn new(root: &std::path::Path, address: &HubAddress) -> Result { + let stem = file_stem(address); + if stem.len() > MAX_NAME_BYTES { + return Err(CacheError::NameTooLong); + } + Ok(Self { + path: root.join(format!("{stem}.cache")), + work: CacheWork::new(None), + }) + } + + pub(crate) fn with_work(mut self, work: Arc) -> Self { + self.work = work; + self + } + + pub(crate) fn work(&self) -> &Arc { + &self.work + } + + /// The cached account; `None` when nothing was kept for the hub. + pub(crate) fn load(&self) -> Result, CacheError> { + let bytes = match read_regular_bounded(&self.path, MAX_FILE_BYTES) { + Ok(bytes) => bytes, + Err(error) if error.io_kind() == Some(io::ErrorKind::NotFound) => return Ok(None), + Err(error) => return Err(error.into()), + }; + #[derive(Deserialize)] + struct Header { + v: u32, + } + let header: Header = serde_json::from_slice(&bytes).map_err(CacheError::Format)?; + if header.v > FORMAT { + return Err(CacheError::Newer); + } + let cache: AccountCache = serde_json::from_slice(&bytes).map_err(CacheError::Format)?; + Ok(Some(cache)) + } + + /// Replaces the file atomically; it is created readable by the user only. + #[cfg(test)] + pub(crate) fn save(&self, cache: &AccountCache) -> Result<(), CacheError> { + self.stage(cache)?.publish()?; + Ok(()) + } + + pub(crate) fn stage(&self, cache: &AccountCache) -> Result { + if let Some(parent) = self.path.parent() { + prepare_private_directory(parent).map_err(CacheError::Prepare)?; + } + let mut bytes = BoundedBuffer::new(MAX_FILE_BYTES); + if let Err(error) = serde_json::to_writer(&mut bytes, cache) { + return Err(if bytes.exceeded() { + CacheError::Limit + } else { + CacheError::Format(error) + }); + } + Ok(AtomicPublication::stage_private( + &self.path, + &bytes.into_inner(), + )?) + } + + /// Removes the cache; no file is not an error. + pub(crate) fn erase(&self) -> io::Result<()> { + match std::fs::remove_file(&self.path) { + Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error), + _ => Ok(()), + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/cache/tests.rs b/crates/hub-io/src/cache/tests.rs new file mode 100644 index 00000000..ee7abf8b --- /dev/null +++ b/crates/hub-io/src/cache/tests.rs @@ -0,0 +1,106 @@ +use super::*; + +fn address() -> HubAddress { + "hub.example:7443".parse().unwrap() +} + +fn record() -> v2::Keyset { + v2::Keyset { + version: 3, + kdf: "argon2id;m=65536;t=3;p=4".into(), + salt: vec![1; 16], + wrapped_account_key: vec![2; 60], + updated_at_ms: 9, + recovery_wrapped_account_key: vec![5; 60], + unlock_kind: v2::UnlockKind::LoginPassword as i32, + } +} + +fn account() -> AccountCache { + let mut cache = AccountCache::new( + "hub-uid".into(), + "acc-1".into(), + UnlockMethod::Login { + username: "ann".into(), + }, + &record(), + ); + cache.vault = Some(Arc::new(VaultCopy { + collection_id: "c1".into(), + key_envelope: vec![7; 80], + cursor: 2, + objects: BTreeMap::from([( + "o1".into(), + ObjectCopy { + version: 1, + kind: "credential".into(), + ciphertext: vec![9; 40], + deleted: false, + }, + )]), + })); + cache +} + +#[test] +fn a_saved_account_reads_back_unchanged() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + assert!(file.load().unwrap().is_none()); + file.save(&account()).unwrap(); + assert_eq!(file.load().unwrap(), Some(account())); + assert_eq!(account().keyset.record().wrapped_account_key, vec![2; 60]); +} + +#[cfg(unix)] +#[test] +fn the_cache_is_private_and_holds_no_plain_text_of_the_secrets() { + use std::os::unix::fs::PermissionsExt as _; + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + file.save(&account()).unwrap(); + let path = dir.path().join("hub.example_7443.cache"); + let mode = std::fs::metadata(&path).unwrap().permissions().mode(); + assert_eq!(mode & 0o777, 0o600); + let text = std::fs::read_to_string(path).unwrap(); + assert!(!text.contains("password") && !text.contains("secret")); +} + +#[test] +fn a_damaged_cache_is_reported_and_left_in_place() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + let path = dir.path().join("hub.example_7443.cache"); + std::fs::write(&path, b"{ not json").unwrap(); + assert!(matches!(file.load(), Err(CacheError::Format(_)))); + assert!(path.exists()); +} + +#[test] +fn a_cache_from_a_newer_version_is_not_read() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + std::fs::write(dir.path().join("hub.example_7443.cache"), br#"{"v":99}"#).unwrap(); + assert!(matches!(file.load(), Err(CacheError::Newer))); +} + +#[test] +fn erasing_a_missing_cache_is_not_an_error() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + file.erase().unwrap(); + file.save(&account()).unwrap(); + file.erase().unwrap(); + assert!(file.load().unwrap().is_none()); +} + +#[test] +fn an_oversized_cache_write_preserves_the_last_readable_account() { + let dir = tempfile::tempdir().unwrap(); + let file = CacheFile::new(dir.path(), &address()).unwrap(); + file.save(&account()).unwrap(); + let mut oversized = account(); + oversized.hub_uid = "x".repeat(MAX_FILE_BYTES); + assert!(matches!(file.save(&oversized), Err(CacheError::Limit))); + assert_eq!(file.load().unwrap(), Some(account())); +} diff --git a/crates/hub-io/src/chats.rs b/crates/hub-io/src/chats.rs new file mode 100644 index 00000000..eee0b46c --- /dev/null +++ b/crates/hub-io/src/chats.rs @@ -0,0 +1,28 @@ +//! Hub chats as ordinary agent chats. +//! +//! A hub agent's chat runs on the hub; this module shows it through the neutral agent port of +//! `nocterm-ai`, so the desktop presents it with the same chat views as a local agent. +//! [`HubAgentConnector`] implements `AgentConnector` over `ChatService`: a session is a hub +//! chat (a new one is created by its first message under the session's id, a reopened one +//! reads its `History`), a prompt is `Send` and resolves when the +//! run that answers it ends, cancel is `Stop`, an approval request is a permission request +//! answered with `ResolveApproval`, and the chat's settings are configuration options: the model +//! and approval mode (`SetChatModel`) and one flag per server of the account, on while the chat +//! has it attached (`SetChatServers`; the agent signs in only to attached servers). +//! One `Watch` per open chat follows its items and the text the agent is writing; a turn +//! someone started on another device arrives as `SessionUpdate::Turn`. +//! [`HubChatRepository`] puts the hub's chats beside the local history of a hub window and +//! sends renames, pins and deletions of hub chats to the hub. + +mod connector; +mod hints; +mod items; +mod options; +mod repository; +mod state; +mod upload; +mod watch; + +pub use connector::HubAgentConnector; +pub(crate) use hints::ChatHints; +pub use repository::HubChatRepository; diff --git a/crates/hub-io/src/chats/connector.rs b/crates/hub-io/src/chats/connector.rs new file mode 100644 index 00000000..aa4b0276 --- /dev/null +++ b/crates/hub-io/src/chats/connector.rs @@ -0,0 +1,573 @@ +//! The hub connector: a hub agent's chats as the sessions of an agent connection. + +use std::collections::HashMap; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::future::BoxFuture; +use nocterm_ai::model::{ + controls::ConfigOption, + diagnostic::ModelError, + ids::{AuthMethodId, SessionId}, + session::{ + AgentCapabilitiesSummary, PromptOutcome, PromptRequest, RestoreSession, SessionOpen, + SessionOpened, SetConfigOption, SetMode, + }, +}; +use nocterm_ai::{ + AgentCommands, AgentConnection, AgentConnector, AgentError, AgentEvent, AgentInfo, + CloseSessionOutcome, ConnectError, ConnectRequest, +}; +use nocterm_hub_agents_model::HubServer; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubError; +use tokio::runtime::Handle; + +use super::options::{Changed, ChatOptions}; +use super::state::ChatState; +use super::watch::Follow; +use crate::classify::classify; +use crate::devices::Captured; +use crate::links::HubLinks; +use crate::pool::Pool; + +/// The most history items a reopened chat reads at once. +const HISTORY_PAGE: u32 = 200; +/// The most items a reopened chat shows; older ones stay on the hub. +const MAX_HISTORY: usize = nocterm_ai::history::MAX_SAVED_ENTRIES; + +/// Connects chats of hub agents (launches with [`nocterm_ai::agents::HubAgentRef`]). +pub struct HubAgentConnector { + handle: Handle, + pool: Arc, +} + +impl HubAgentConnector { + pub fn new(links: &HubLinks) -> Self { + let (handle, pool) = links.agent_parts(); + Self { handle, pool } + } +} + +/// A failed hub call before it is told in the agent port's terms. +pub(super) enum CallFailure { + Client(ClientError), + Hub(HubError), + Refused(String), +} + +impl From for CallFailure { + fn from(status: tonic::Status) -> Self { + Self::Client(ClientError::from(status)) + } +} + +impl From for CallFailure { + fn from(error: HubError) -> Self { + Self::Hub(error) + } +} + +impl CallFailure { + fn into_agent(self, captured: &Captured) -> AgentError { + let error = match self { + Self::Refused(message) => return AgentError::Io(message), + Self::Client(ClientError::Status { + hub_code: Some(v2::ErrorCode::LimitReached), + message, + .. + }) => return AgentError::Io(format!("A limit of the hub is reached: {message}")), + Self::Client(error) => classify(captured.hub.address(), error), + Self::Hub(error) => error, + }; + AgentError::Io(error.to_string()) + } +} + +fn hub_failure(error: &HubError) -> AgentError { + AgentError::Io(error.to_string()) +} + +/// A client of the captured account with a fresh access token. +pub(super) async fn fresh(captured: &Captured) -> Result { + let client = captured.client.clone(); + captured + .observed + .during(client.ensure_fresh()) + .await? + .map_err(CallFailure::Client)?; + Ok(client) +} + +/// Runs `work` on the adapter's threads with a fresh client of the captured account. +pub(super) fn call( + handle: &Handle, + captured: &Arc, + work: F, +) -> BoxFuture<'static, Result> +where + T: Send + 'static, + F: FnOnce(HubClient) -> Fut + Send + 'static, + Fut: std::future::Future> + Send + 'static, +{ + let captured = captured.clone(); + let task = crate::task::spawn(handle, async move { + let outcome = async { + let client = fresh(&captured).await?; + captured.observed.during(work(client)).await? + } + .await; + outcome.map_err(|failure| failure.into_agent(&captured)) + }); + Box::pin(async move { + task.await + .unwrap_or_else(|| Err(hub_failure(&HubError::Stale))) + }) +} + +impl AgentConnector for HubAgentConnector { + fn connect( + &self, + request: ConnectRequest, + ) -> BoxFuture<'static, Result> { + let Some(agent) = request.launch.hub.clone() else { + return Box::pin(async { + Err(AgentError::Unsupported(ModelError::Unsupported( + "a launch that is not a hub agent", + ))) + }); + }; + let captured = match Captured::new(self.pool.clone(), &agent.hub) { + Ok(captured) => Arc::new(captured), + Err(error) => return Box::pin(async move { Err(hub_failure(&error)) }), + }; + let handle = self.handle.clone(); + let name = request.launch.name.clone(); + let wanted = agent.agent.clone(); + let listed = call(&handle, &captured, |client| async move { + let agents = client.agents().list_agents(v2::Empty {}).await?; + let agent = agents + .into_inner() + .agents + .into_iter() + .find(|agent| agent.id == wanted) + .ok_or_else(|| { + CallFailure::Refused("The agent no longer exists on the hub.".into()) + })?; + // Without the servers a chat still works; it offers none until they are listed. + let servers = servers_of(&client).await.unwrap_or_default(); + Ok((agent, servers)) + }); + Box::pin(async move { + let (record, servers) = listed.await?; + let (events_sender, events) = async_channel::unbounded(); + let commands = Arc::new(HubChatCommands(Arc::new(Inner { + handle, + captured, + agent: record, + events: events_sender, + chats: Mutex::default(), + servers: Mutex::new(servers), + }))); + Ok(AgentConnection { + info: AgentInfo { + name, + version: String::new(), + capabilities: AgentCapabilitiesSummary { + image_prompts: true, + keeps_history: true, + own_tools: true, + }, + auth_methods: Vec::new(), + }, + commands, + events, + }) + }) + } +} + +pub(super) struct Inner { + pub(super) handle: Handle, + pub(super) captured: Arc, + agent: v2::Agent, + pub(super) events: async_channel::Sender, + chats: Mutex>, + /// The account's servers as last listed: a chat offers to attach each of them. + servers: Mutex>, +} + +/// The account's servers; a record the hub sent malformed is left out. +async fn servers_of(client: &HubClient) -> Result, CallFailure> { + let listed = client.servers().list_servers(v2::Empty {}).await?; + Ok(listed + .into_inner() + .servers + .into_iter() + .filter_map(|server| crate::agents::hub_server(server).ok()) + .collect()) +} + +/// A chat this connection shows. A new chat exists only here until its first message creates +/// it on the hub with the same id; then its `Watch` is followed. +struct OpenChat { + state: Arc>, + options: Arc>, + follow: Option, +} + +impl Drop for OpenChat { + fn drop(&mut self) { + if let Some(follow) = &self.follow { + follow.abort(); + } + } +} + +/// The commands of one hub agent's connection; every session is a chat of the agent. +struct HubChatCommands(Arc); + +fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(PoisonError::into_inner) +} + +impl Inner { + fn chats(&self) -> MutexGuard<'_, HashMap> { + lock(&self.chats) + } + + /// What `options` shows as the chat's configuration, with the account's servers. + pub(super) fn config(&self, options: &ChatOptions) -> Vec { + options.config(&lock(&self.servers)) + } + + /// Lists the account's servers again; `false` when the hub did not answer. + pub(super) async fn list_servers(&self) -> bool { + let listed = async { + let client = fresh(&self.captured).await?; + self.captured.observed.during(servers_of(&client)).await? + }; + match listed.await { + Ok(servers) => { + *lock(&self.servers) = servers; + true + } + Err(_) => false, + } + } + + /// Shows `chat` from `state` on, returning its options; it is followed once + /// [`Self::follow`] is called. + fn track(&self, chat: &v2::Chat, state: ChatState) -> Vec { + let options = ChatOptions::of(chat, &self.agent.models); + let shown = self.config(&options); + self.chats().insert( + chat.id.clone(), + OpenChat { + state: Arc::new(Mutex::new(state)), + options: Arc::new(Mutex::new(options)), + follow: None, + }, + ); + shown + } + + /// Follows the hub's chat `id`, telling the person what its state left open. Following + /// twice is harmless. + fn follow(self: &Arc, id: &str) { + let mut chats = self.chats(); + let Some(open) = chats.get_mut(id).filter(|open| open.follow.is_none()) else { + return; + }; + let (opening, running) = { + let state = lock(&open.state); + (state.open_approvals(), state.running()) + }; + let follow = Follow { + inner: Arc::downgrade(self), + chat: id.to_owned(), + state: open.state.clone(), + options: open.options.clone(), + }; + open.follow = Some( + self.handle + .spawn(follow.run(opening, running)) + .abort_handle(), + ); + } + + /// The chat's state, its options, and whether it exists on the hub yet. + fn open_chat( + &self, + chat: &str, + ) -> Option<(Arc>, Arc>, bool)> { + self.chats().get(chat).map(|open| { + ( + open.state.clone(), + open.options.clone(), + open.follow.is_some(), + ) + }) + } +} + +impl Inner { + /// Follows `id` once its first message created it on the hub. + fn follow_if_created(self: &Arc, id: &str, created: bool) { + if created { + self.follow(id); + } + } +} + +/// Creates the chat `id` of `agent` on the hub with the model, mode and servers it was shown +/// with. Creating it again (a retried first message) returns it. +async fn create( + client: &HubClient, + agent: String, + id: &str, + options: ChatOptions, +) -> Result<(), CallFailure> { + let (model, approval) = options.model(); + let chat = client + .chats() + .create_chat(v2::CreateChatRequest { + agent_id: agent, + chat_id: id.to_owned(), + model: model.clone(), + server_ids: options.servers(), + ..Default::default() + }) + .await? + .into_inner(); + if chat.approval != approval { + let request = v2::SetChatModelRequest { + id: id.to_owned(), + model, + approval, + }; + client.chats().set_chat_model(request).await?; + } + Ok(()) +} + +/// Reads the chat's history, oldest first, up to [`MAX_HISTORY`] items. +async fn history(client: &HubClient, chat: &str) -> Result, CallFailure> { + let mut pages = Vec::new(); + let mut before = 0; + let mut count = 0; + loop { + let page = client + .chats() + .history(v2::HistoryRequest { + chat_id: chat.to_owned(), + before_seq: before, + limit: HISTORY_PAGE, + }) + .await? + .into_inner(); + count += page.items.len(); + let first = page.items.first().map(|item| item.seq); + let more = page.more; + pages.push(page.items); + match first { + Some(first) if more && count < MAX_HISTORY => before = first, + _ => break, + } + } + Ok(pages.into_iter().rev().flatten().collect()) +} + +impl AgentCommands for HubChatCommands { + /// A new chat is shown with the agent's model and mode at once; its first message creates + /// it on the hub under the same id, so opening a chat leaves nothing on the hub. + fn new_session( + &self, + _request: SessionOpen, + ) -> BoxFuture<'static, Result> { + let inner = &self.0; + let chat = v2::Chat { + id: uuid::Uuid::new_v4().to_string(), + agent_id: inner.agent.id.clone(), + model: inner.agent.model.clone(), + approval: inner.agent.approval, + ..Default::default() + }; + let options = inner.track(&chat, ChatState::default()); + Box::pin(async move { Ok(SessionOpened::new(chat.id).configuration(options)) }) + } + + fn restore_session( + &self, + request: RestoreSession, + ) -> BoxFuture<'static, Result> { + if request.fork { + return Box::pin(async { + Err(AgentError::RestoreUnavailable( + "A hub chat cannot be copied.".into(), + )) + }); + } + let inner = self.0.clone(); + let id = request.session.to_string(); + let read = call(&inner.handle, &inner.captured, |client| async move { + let chat = client + .chats() + .get_chat(v2::IdRequest { id: id.clone() }) + .await? + .into_inner(); + let items = history(&client, &id).await?; + Ok((chat, items)) + }); + Box::pin(async move { + let (chat, items) = read.await?; + let mut state = ChatState::default(); + let updates = items.iter().flat_map(|item| state.replay(item)).collect(); + let options = inner.track(&chat, state); + inner.follow(&chat.id); + Ok(SessionOpened::new(chat.id) + .configuration(options) + .history(updates)) + }) + } + + fn prompt( + &self, + request: PromptRequest, + ) -> BoxFuture<'static, Result> { + let inner = self.0.clone(); + let chat = request.session.to_string(); + let Some((state, options, created)) = inner.open_chat(&chat) else { + return Box::pin(async { Err(AgentError::Io("The chat is not open.".into())) }); + }; + let client_message = uuid::Uuid::new_v4().to_string(); + let (done, answer) = futures::channel::oneshot::channel(); + lock(&state).expect(client_message.clone(), done); + let creation = (!created).then(|| (inner.agent.id.clone(), lock(&options).clone())); + let id = chat.clone(); + let sent = call(&inner.handle, &inner.captured, move |client| async move { + if let Some((agent, options)) = creation { + create(&client, agent, &chat, options).await?; + } + let (text, attachment_ids) = + super::upload::prepare(&client, &chat, request.content).await?; + let request = v2::SendRequest { + chat_id: chat, + client_message_id: client_message, + text, + attachment_ids, + }; + client.chats().send(request).await?; + Ok(()) + }); + Box::pin(async move { + let outcome = sent.await; + // Once created, the chat is followed even when the message did not go. + inner.follow_if_created(&id, created || outcome.is_ok()); + if let Err(error) = outcome { + lock(&state).abandon(); + return Err(error); + } + answer.await.unwrap_or_else(|_| { + Err(AgentError::Io( + "The chat closed before the agent answered.".into(), + )) + }) + }) + } + + fn cancel(&self, session: SessionId) { + let id = session.to_string(); + let stopping = call(&self.0.handle, &self.0.captured, |client| async move { + client.chats().stop(v2::IdRequest { id }).await?; + Ok(()) + }); + self.0.handle.spawn(async move { + if let Err(error) = stopping.await { + tracing::warn!(%error, "the hub did not stop the chat's agent"); + } + }); + } + + fn set_mode(&self, _request: SetMode) -> BoxFuture<'static, Result<(), AgentError>> { + Box::pin(async { + Err(AgentError::Unsupported(ModelError::Unsupported( + "modes of a hub agent", + ))) + }) + } + + /// Chooses the model or the approval mode, or attaches or detaches a server. A chat its + /// first message has not created keeps the choice until it does. + fn set_config_option( + &self, + request: SetConfigOption, + ) -> BoxFuture<'static, Result, AgentError>> { + let inner = self.0.clone(); + let chat = request.session.to_string(); + let Some((current, created)) = inner + .chats() + .get(&chat) + .map(|open| (open.options.clone(), open.follow.is_some())) + else { + return Box::pin(async { Err(AgentError::Io("The chat is not open.".into())) }); + }; + let mut chosen = lock(¤t).clone(); + let changed = match chosen.choose(request.config.as_str(), &request.setting) { + Ok(changed) => changed, + Err(refused) => return Box::pin(async move { Err(AgentError::Io(refused.0.into())) }), + }; + if !created { + // Not on the hub yet: its first message creates it with these. + let shown = inner.config(&chosen); + *lock(¤t) = chosen; + return Box::pin(async move { Ok(shown) }); + } + let saved = call(&inner.handle, &inner.captured, move |client| async move { + let mut chats = client.chats(); + let stored = match changed { + Changed::Model => { + let (model, approval) = chosen.model(); + let request = v2::SetChatModelRequest { + id: chat, + model, + approval, + }; + chats.set_chat_model(request).await? + } + Changed::Servers => { + let request = v2::SetChatServersRequest { + id: chat, + server_ids: chosen.servers(), + }; + chats.set_chat_servers(request).await? + } + }; + Ok(stored.into_inner()) + }); + Box::pin(async move { + let chat = saved.await?; + let stored = lock(¤t).stored(&chat); + let shown = inner.config(&stored); + *lock(¤t) = stored; + Ok(shown) + }) + } + + fn authenticate(&self, _method: AuthMethodId) -> BoxFuture<'static, Result<(), AgentError>> { + Box::pin(async { Ok(()) }) + } + + fn close_session( + &self, + session: SessionId, + ) -> BoxFuture<'static, Result> { + self.0.chats().remove(session.as_str()); + Box::pin(async { Ok(CloseSessionOutcome::Closed) }) + } + + fn shutdown(&self) { + self.0.chats().clear(); + self.0.events.close(); + } +} diff --git a/crates/hub-io/src/chats/hints.rs b/crates/hub-io/src/chats/hints.rs new file mode 100644 index 00000000..407e6137 --- /dev/null +++ b/crates/hub-io/src/chats/hints.rs @@ -0,0 +1,22 @@ +//! Hints that a hub's chat list changed on this device: a refreshed list that differs from the +//! kept one. Watchers of the chat list hear them beside the hub's own changes. + +use tokio::sync::watch; + +pub(crate) struct ChatHints(watch::Sender); + +impl Default for ChatHints { + fn default() -> Self { + Self(watch::channel(0).0) + } +} + +impl ChatHints { + pub(crate) fn raise(&self) { + self.0.send_modify(|count| *count = count.wrapping_add(1)); + } + + pub(crate) fn subscribe(&self) -> watch::Receiver { + self.0.subscribe() + } +} diff --git a/crates/hub-io/src/chats/items.rs b/crates/hub-io/src/chats/items.rs new file mode 100644 index 00000000..922cdb39 --- /dev/null +++ b/crates/hub-io/src/chats/items.rs @@ -0,0 +1,105 @@ +//! Chat items of the hub protocol as the neutral session updates a chat shows. + +use nocterm_ai::model::{ + content::Content, + ids::ToolId, + session::SessionUpdate, + tools::{ToolChanges, ToolKind, ToolPatch, ToolRecord, ToolStatus}, + usage::TokenTotals, +}; +use nocterm_hub_proto::v2; + +/// The prompt of a person's message: its text, and the names of its attachments. +pub(super) fn prompt(message: &v2::UserMessage) -> Vec { + let mut content = Vec::with_capacity(1 + message.attachments.len()); + if !message.text.is_empty() { + content.push(Content::text(message.text.clone())); + } + content.extend( + message + .attachments + .iter() + .map(|attachment| Content::text(format!("[{}]", attachment.name))), + ); + content +} + +fn kind_of(call: &v2::ToolCall) -> ToolKind { + match call.tool.as_str() { + "server.run" => ToolKind::Execute, + "server.read_file" => ToolKind::Read, + "server.write_file" => ToolKind::Edit, + "server.send_file" => ToolKind::Fetch, + _ if call.read_only => ToolKind::Read, + _ => ToolKind::Other, + } +} + +pub(super) fn tool_call(call: &v2::ToolCall) -> SessionUpdate { + let arguments = serde_json::from_str(&call.arguments_json).ok(); + let title = if call.title.is_empty() { + call.tool.clone() + } else { + call.title.clone() + }; + SessionUpdate::ToolCall(Box::new( + ToolRecord::new(ToolId::new(call.call_id.clone()), title) + .name(call.tool.clone()) + .kind(kind_of(call)) + .status(ToolStatus::Running) + .raw_input(arguments), + )) +} + +pub(super) fn tool_result(result: &v2::ToolResult) -> SessionUpdate { + let mut text = String::new(); + for part in &result.content { + match &part.part { + Some(v2::content_part::Part::Text(value)) => text.push_str(value), + Some(v2::content_part::Part::File(file)) => { + text.push_str(&format!("\n[{}]", file.name)); + } + None => {} + } + } + if result.truncated { + text.push_str("\n… (output cut to the hub's limit)"); + } + if result.unknown_outcome { + text.push_str( + "\nThe run was interrupted while this ran; whether it took effect is unknown.", + ); + } + let failed = result.is_error || result.unknown_outcome; + let output = serde_json::json!({ + "exit_code": result.exit_code, + "truncated": result.truncated, + }); + SessionUpdate::ToolCallUpdate(Box::new(ToolPatch::new( + ToolId::new(result.call_id.clone()), + ToolChanges::default() + .status(if failed { + ToolStatus::Failed + } else { + ToolStatus::Succeeded + }) + .content(vec![text.trim_start().to_owned().into()]) + .raw_output(output), + ))) +} + +/// What the end of a run adds to the transcript: its error, when it has one. +pub(super) fn run_error(ended: &v2::RunEnded) -> Option { + (!ended.error.is_empty()) + .then(|| SessionUpdate::AgentMessageChunk(Content::text(format!("\n\n⚠ {}", ended.error)))) +} + +pub(super) fn usage(ended: &v2::RunEnded) -> Option { + ended.usage.as_ref().map(|usage| { + TokenTotals::new( + usage.input_tokens + usage.output_tokens, + usage.input_tokens, + usage.output_tokens, + ) + }) +} diff --git a/crates/hub-io/src/chats/options.rs b/crates/hub-io/src/chats/options.rs new file mode 100644 index 00000000..81d2485e --- /dev/null +++ b/crates/hub-io/src/chats/options.rs @@ -0,0 +1,289 @@ +//! A hub chat's settings as the configuration options of its session: its model among the +//! agent's models, whether changing tools wait for an approval, and which of the account's +//! servers the chat has attached (one flag per server; the agent signs in only to those). + +use nocterm_ai::model::controls::{ConfigCategory, ConfigChoice, ConfigOption, ConfigSetting}; +use nocterm_hub_agents_model::HubServer; +use nocterm_hub_proto::v2; + +/// The configuration option that chooses the chat's model. +const MODEL: &str = "model"; +/// The configuration option that chooses whether changing tools wait for an approval. +const APPROVAL: &str = "approval"; +/// The prefix of the options that attach one server each: `server:`. +const SERVER: &str = "server:"; +const AUTO: &str = "auto"; +const ASK: &str = "ask"; + +/// What a choice changed, so the hub is told with the call that stores it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Changed { + /// The model or the approval mode (`SetChatModel`). + Model, + /// The attached servers (`SetChatServers`). + Servers, +} + +/// A choice the chat does not take. +#[derive(Debug, PartialEq, Eq)] +pub(super) struct Refused(pub(super) &'static str); + +/// The chat's settings as last stored on the hub, or as chosen for a chat its first message +/// has not created yet. +#[derive(Clone, Debug)] +pub(super) struct ChatOptions { + model: String, + approval: i32, + models: Vec, + title: String, + servers: Vec, +} + +impl ChatOptions { + pub(super) fn of(chat: &v2::Chat, models: &[String]) -> Self { + Self { + model: chat.model.clone(), + approval: chat.approval, + models: models.to_vec(), + title: chat.title.clone(), + servers: chat.server_ids.clone(), + } + } + + /// The same settings after the hub stored `chat`. + pub(super) fn stored(&self, chat: &v2::Chat) -> Self { + Self::of(chat, &self.models) + } + + pub(super) fn title(&self) -> &str { + &self.title + } + + /// The model and approval mode as `SetChatModel` takes them. + pub(super) fn model(&self) -> (String, i32) { + (self.model.clone(), self.approval) + } + + /// The ids of the attached servers, as `SetChatServers` takes them. + pub(super) fn servers(&self) -> Vec { + self.servers.clone() + } + + /// Whether `chat` shows other options than these. + pub(super) fn differs(&self, chat: &v2::Chat) -> bool { + chat.model != self.model + || chat.approval != self.approval + || chat.server_ids != self.servers + } + + /// The options of the chat, with a flag for each server of `offered`, by name. A server + /// attached but no longer offered stays attached and is not shown. + pub(super) fn config(&self, offered: &[HubServer]) -> Vec { + let mut options = self.model_options(); + let mut servers: Vec<&HubServer> = offered.iter().collect(); + servers.sort_by_cached_key(|server| (server.name.to_lowercase(), server.id.to_string())); + options.extend(servers.into_iter().map(|server| { + let id = server.id.to_string(); + let name = if server.name.is_empty() { + server.host.clone() + } else { + server.name.clone() + }; + let attached = self.servers.contains(&id); + ConfigOption::boolean(format!("{SERVER}{id}"), name, attached) + .description(address(server)) + .category(ConfigCategory::Server) + })); + options + } + + fn model_options(&self) -> Vec { + let mut choices: Vec = self + .models + .iter() + .map(|model| ConfigChoice::new(model.clone(), model.clone())) + .collect(); + if !self.model.is_empty() && !self.models.contains(&self.model) { + choices.insert(0, ConfigChoice::new(self.model.clone(), self.model.clone())); + } + let ask = v2::ApprovalMode::try_from(self.approval) == Ok(v2::ApprovalMode::Ask); + vec![ + ConfigOption::select(MODEL, "Model", self.model.clone(), choices) + .category(ConfigCategory::Model), + ConfigOption::select( + APPROVAL, + "Approval", + if ask { ASK } else { AUTO }, + vec![ + ConfigChoice::new(AUTO, "Auto"), + ConfigChoice::new(ASK, "Ask before changes"), + ], + ) + .category(ConfigCategory::Mode), + ] + } + + /// Applies a choice of the option `config`, telling which call stores it. + pub(super) fn choose( + &mut self, + config: &str, + setting: &ConfigSetting, + ) -> Result { + if let Some(server) = config.strip_prefix(SERVER) { + let ConfigSetting::Boolean(attach) = setting else { + return Err(Refused("A server is attached or not.")); + }; + self.servers.retain(|id| id != server); + if *attach { + self.servers.push(server.to_owned()); + } + return Ok(Changed::Servers); + } + let ConfigSetting::Choice(choice) = setting else { + return Err(Refused("The option takes a choice.")); + }; + match config { + MODEL => self.model = choice.to_string(), + APPROVAL => { + self.approval = match choice.as_str() { + AUTO => v2::ApprovalMode::Auto.into(), + ASK => v2::ApprovalMode::Ask.into(), + _ => return Err(Refused("Unknown approval mode.")), + } + } + _ => return Err(Refused("The chat has no such option.")), + } + Ok(Changed::Model) + } +} + +/// `user@host`, the port when it is not 22, and what keeps agents out when something does. +fn address(server: &HubServer) -> String { + let mut text = format!("{}@{}", server.user, server.host); + if server.port != 22 { + text.push_str(&format!(":{}", server.port)); + } + let status = server.status; + if !status.is_ready() && status != nocterm_hub_agents_model::ServerStatus::Unchecked { + text.push_str(" · "); + text.push_str(status.label()); + } + text +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_hub_agents_model::{ServerId, ServerStatus}; + + fn server(id: &str, name: &str, status: ServerStatus) -> HubServer { + HubServer { + id: ServerId::new(id.to_owned()).unwrap(), + name: name.to_owned(), + host: format!("{id}.example"), + port: 22, + user: "root".to_owned(), + host_key_fingerprint: String::new(), + status, + status_message: String::new(), + } + } + + fn chat(servers: &[&str]) -> ChatOptions { + let chat = v2::Chat { + model: "m1".into(), + server_ids: servers.iter().map(ToString::to_string).collect(), + ..Default::default() + }; + ChatOptions::of(&chat, &["m1".into(), "m2".into()]) + } + + fn flags(options: &[ConfigOption]) -> Vec<(String, String, bool)> { + options + .iter() + .filter(|option| option.category == Some(ConfigCategory::Server)) + .map(|option| { + let on = matches!( + option.kind, + nocterm_ai::model::controls::ConfigKind::Boolean(true) + ); + (option.name.clone(), option.description.clone().unwrap(), on) + }) + .collect() + } + + #[test] + fn every_server_of_the_account_is_a_flag_on_while_the_chat_has_it() { + let offered = [ + server("b", "Web", ServerStatus::Ready), + server("a", "build", ServerStatus::NoAccess), + ]; + let options = chat(&["b"]).config(&offered); + assert_eq!(options[0].category, Some(ConfigCategory::Model)); + assert_eq!( + flags(&options), + vec![ + ( + "build".into(), + "root@a.example · No access — reconnect".into(), + false + ), + ("Web".into(), "root@b.example".into(), true), + ] + ); + } + + #[test] + fn attaching_and_detaching_a_server_is_stored_with_set_chat_servers() { + let mut options = chat(&["b"]); + let attach = options.choose("server:a", &ConfigSetting::Boolean(true)); + assert_eq!(attach, Ok(Changed::Servers)); + assert_eq!(options.servers(), vec!["b".to_owned(), "a".to_owned()]); + options + .choose("server:b", &ConfigSetting::Boolean(false)) + .unwrap(); + assert_eq!(options.servers(), vec!["a".to_owned()]); + assert!( + options + .choose("server:a", &ConfigSetting::Choice("x".into())) + .is_err() + ); + } + + #[test] + fn the_model_and_the_approval_mode_are_stored_with_set_chat_model() { + let mut options = chat(&[]); + let model = options.choose("model", &ConfigSetting::Choice("m2".into())); + assert_eq!(model, Ok(Changed::Model)); + options + .choose("approval", &ConfigSetting::Choice("ask".into())) + .unwrap(); + assert_eq!( + options.model(), + ("m2".to_owned(), v2::ApprovalMode::Ask.into()) + ); + assert!( + options + .choose("approval", &ConfigSetting::Choice("never".into())) + .is_err() + ); + assert!( + options + .choose("other", &ConfigSetting::Boolean(true)) + .is_err() + ); + } + + #[test] + fn a_chat_stored_with_other_servers_differs() { + let options = chat(&["a"]); + let mut stored = v2::Chat { + model: "m1".into(), + server_ids: vec!["a".into()], + ..Default::default() + }; + assert!(!options.differs(&stored)); + stored.server_ids.clear(); + assert!(options.differs(&stored)); + } +} diff --git a/crates/hub-io/src/chats/repository.rs b/crates/hub-io/src/chats/repository.rs new file mode 100644 index 00000000..876946b7 --- /dev/null +++ b/crates/hub-io/src/chats/repository.rs @@ -0,0 +1,289 @@ +//! The history of a hub window: the local chats of its account and the hub's chats. +//! +//! Local chats stay in the local repository. Chats of hub agents live on the hub: the list +//! comes from `ListChats` and is kept in a private file per hub, so it shows at once after a +//! restart and while the hub cannot be reached; a hub chat's transcript comes from the hub +//! when the chat opens. Renaming, pinning and deleting a hub chat goes to the hub. A hub chat +//! started on this device keeps the id the chat had here; the file remembers which hub chat +//! that is. + +mod list; + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use nocterm_ai::agents::AgentLaunch; +use nocterm_ai::history::{ChatSummary, ContextId, SavedChat, SharedChat}; +use nocterm_ai::local_environment::{ChatScope, LocalEnvironment}; +use nocterm_ai::storage::{ + AgentStorageError, ChatRepository, PreparedChatBytes, PreparedChatLoad, StorageFuture, +}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use nocterm_hubs_model::source_identity::LocalAccountPartition; +use tokio::runtime::Handle; + +use self::list::{ChatList, Row}; +use super::ChatHints; +use super::connector::call; +use crate::devices::Captured; +use crate::links::HubLinks; +use crate::pool::Pool; + +/// The most chats of a hub the history lists. +const MAX_CHATS: u32 = 500; + +/// Puts the chats of hub agents beside the local history of hub windows. +pub struct HubChatRepository { + local: Arc, + handle: Handle, + pool: Arc, + root: PathBuf, + hints: Arc, + lists: Mutex>>>, +} + +fn lock(mutex: &Mutex) -> MutexGuard<'_, T> { + mutex.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// The hub whose account `scope` belongs to. +fn hub_of(scope: &ChatScope) -> Option { + match scope.environment() { + LocalEnvironment::HubAccount(LocalAccountPartition::LegacyConfigured(account)) => { + Some(account.hub().clone()) + } + _ => None, + } +} + +impl HubChatRepository { + /// Hub chats beside `local`; the lists are kept in private files under `root`. + pub fn new(links: &HubLinks, local: Arc, root: PathBuf) -> Self { + let (handle, pool) = links.agent_parts(); + Self { + local, + handle, + pool, + root, + hints: links.chat_hints(), + lists: Mutex::default(), + } + } + + fn list(&self, hub: &HubId) -> Arc> { + lock(&self.lists) + .entry(hub.clone()) + .or_insert_with(|| Arc::new(Mutex::new(ChatList::load(&self.root, hub)))) + .clone() + } + + /// Reads the hub's chats in the background; a changed list is kept and hinted, so the + /// history is read again. + fn refresh(&self, hub: &HubId) { + let Ok(captured) = Captured::new(self.pool.clone(), hub) else { + return; + }; + let captured = Arc::new(captured); + let list = self.list(hub); + let hints = self.hints.clone(); + let root = self.root.clone(); + let hub = hub.clone(); + let read = call(&self.handle, &captured, |client| async move { + let request = v2::ListChatsRequest { + cursor: String::new(), + limit: MAX_CHATS, + }; + Ok(client.chats().list_chats(request).await?.into_inner().chats) + }); + self.handle.spawn(async move { + let Ok(chats) = read.await else { + return; + }; + let changed = lock(&list).replace(chats.iter().map(Row::of).collect()); + if changed { + lock(&list).save(&root, &hub); + hints.raise(); + } + }); + } + + /// A hub chat as a saved chat without transcript: the session reopens on the hub. + fn hub_chat(&self, scope: &ChatScope, hub: &HubId, id: &str) -> Option { + let list = self.list(hub); + let list = lock(&list); + saved_chat(&list, scope, hub, id) + } + + /// Sends what changed of a hub chat's name or pin to the hub. + fn publish(&self, hub: &HubId, chat: &SharedChat) { + let header = &chat.metadata.header; + let list = self.list(hub); + let (id, rename, pin) = { + let mut list = lock(&list); + if let Some(session) = &chat.metadata.session_id { + list.remember(&header.id, session); + } + let Some(row) = list.row_for(&header.id).cloned() else { + return; + }; + let rename = header + .name + .clone() + .filter(|name| !name.is_empty() && *name != row.title); + let pin = (header.pinned != row.pinned).then_some(header.pinned); + list.apply(&row.id, rename.as_deref(), pin); + list.save(&self.root, hub); + (row.id, rename, pin) + }; + if rename.is_none() && pin.is_none() { + return; + } + let Ok(captured) = Captured::new(self.pool.clone(), hub) else { + return; + }; + let captured = Arc::new(captured); + let sent = call(&self.handle, &captured, move |client| async move { + if let Some(title) = rename { + let request = v2::RenameChatRequest { + id: id.clone(), + title, + }; + client.chats().rename_chat(request).await?; + } + if let Some(pinned) = pin { + client + .chats() + .pin_chat(v2::PinChatRequest { id, pinned }) + .await?; + } + Ok(()) + }); + self.handle.spawn(async move { + if let Err(error) = sent.await { + tracing::warn!(%error, "the hub did not take the chat's new name or pin"); + } + }); + } +} + +impl ChatRepository for HubChatRepository { + fn prepare_load_in( + &self, + scope: ChatScope, + id: String, + ) -> StorageFuture> { + let Some(hub) = hub_of(&scope) else { + return self.local.prepare_load_in(scope, id); + }; + let Some(chat) = self.hub_chat(&scope, &hub, &id) else { + return self.local.prepare_load_in(scope, id); + }; + Box::pin(async move { + let bytes = nocterm_ai::history::encode(&chat)?; + let prepared = PreparedChatBytes::new(bytes, scope, id, Some(chat.version))?; + Ok(Box::new(prepared) as Box) + }) + } + + fn catalog_in(&self, scope: ChatScope) -> StorageFuture> { + let local = self.local.catalog_in(scope.clone()); + let Some(hub) = hub_of(&scope) else { + return local; + }; + self.refresh(&hub); + let hub_chats: Vec = { + let list = self.list(&hub); + let list = lock(&list); + list.local_ids() + .filter_map(|id| saved_chat(&list, &scope, &hub, &id)) + .map(|chat| ChatSummary::from_chat(&chat)) + .collect() + }; + Box::pin(async move { + let mut rows = local.await?; + rows.retain(|row| !hub_chats.iter().any(|hub_row| hub_row.id == row.id)); + rows.extend(hub_chats); + Ok(rows) + }) + } + + fn load_in(&self, scope: ChatScope, id: String) -> StorageFuture { + match hub_of(&scope).and_then(|hub| self.hub_chat(&scope, &hub, &id)) { + Some(chat) => Box::pin(async move { Ok(chat) }), + None => self.local.load_in(scope, id), + } + } + + fn delete_in(&self, scope: ChatScope, id: String) -> StorageFuture<()> { + let local = self.local.delete_in(scope.clone(), id.clone()); + let Some(hub) = hub_of(&scope) else { + return local; + }; + let list = self.list(&hub); + let Some(chat) = lock(&list).forget(&id) else { + return local; + }; + lock(&list).save(&self.root, &hub); + let deleted = Captured::new(self.pool.clone(), &hub).map(|captured| { + call(&self.handle, &Arc::new(captured), |client| async move { + client + .chats() + .delete_chat(v2::IdRequest { id: chat }) + .await?; + Ok(()) + }) + }); + Box::pin(async move { + if let Ok(deleted) = deleted + && let Err(error) = deleted.await + { + tracing::warn!(%error, "the hub did not delete the chat"); + } + match local.await { + Ok(()) | Err(AgentStorageError::NotFound(_)) => Ok(()), + Err(error) => Err(error), + } + }) + } + + fn catalog(&self, scope: ContextId) -> StorageFuture> { + self.local.catalog(scope) + } + + fn load(&self, scope: ContextId, id: String) -> StorageFuture { + self.local.load(scope, id) + } + + fn save(&self, chat: Arc) -> StorageFuture<()> { + let scope = chat.metadata.header.chat_scope(); + match hub_of(&scope) { + Some(hub) if nocterm_ai::agents::is_hub_agent(&chat.metadata.header.agent_id) => { + self.publish(&hub, &chat); + Box::pin(async { Ok(()) }) + } + _ => self.local.save(chat), + } + } + + fn delete(&self, scope: ContextId, id: String) -> StorageFuture<()> { + self.local.delete(scope, id) + } +} + +/// The row `id` of `list` as a saved chat without transcript: the session reopens on the hub. +/// Takes the locked list, so listing every row locks it once. +fn saved_chat(list: &ChatList, scope: &ChatScope, hub: &HubId, id: &str) -> Option { + let row = list.row_for(id)?; + let agent = AgentLaunch::hub_agent(hub.clone(), row.agent_id.clone(), row.agent_name.clone()); + let mut chat = + SavedChat::for_environment(agent.id, id.to_owned(), row.updated(), scope.clone()); + chat.header.title = Some(row.title.clone()).filter(|title| !title.is_empty()); + chat.header.pinned = row.pinned; + chat.session_id = Some(row.id.clone()); + Some(chat) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/chats/repository/list.rs b/crates/hub-io/src/chats/repository/list.rs new file mode 100644 index 00000000..4ed1a1ef --- /dev/null +++ b/crates/hub-io/src/chats/repository/list.rs @@ -0,0 +1,144 @@ +//! The chats of one hub as this device last read them, kept in a private file. + +use std::collections::BTreeMap; +use std::path::{Path, PathBuf}; + +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubId; +use serde::{Deserialize, Serialize}; + +/// A list larger than this is not ours. +const MAX_FILE_BYTES: usize = 2 * 1024 * 1024; +/// The format the file is written in; another one is read as empty. +const FORMAT: u32 = 1; + +/// One hub chat as the history lists it. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(super) struct Row { + pub(super) id: String, + pub(super) agent_id: String, + pub(super) agent_name: String, + pub(super) title: String, + pub(super) pinned: bool, + updated_at_ms: i64, +} + +impl Row { + pub(super) fn of(chat: &v2::Chat) -> Self { + Self { + id: chat.id.clone(), + agent_id: chat.agent_id.clone(), + agent_name: chat.agent_name.clone(), + title: chat.title.clone(), + pinned: chat.pinned, + updated_at_ms: chat.updated_at_ms, + } + } + + /// Seconds since the Unix epoch, as the history sorts by. + pub(super) fn updated(&self) -> u64 { + u64::try_from(self.updated_at_ms / 1000).unwrap_or(0) + } +} + +/// The hub's chats, and the hub chat behind each chat started here under its own id. +#[derive(Debug, Default, PartialEq, Serialize, Deserialize)] +pub(super) struct ChatList { + v: u32, + rows: Vec, + /// Local id of a chat started on this device → its hub chat. + started_here: BTreeMap, +} + +fn path(root: &Path, hub: &HubId) -> PathBuf { + let stem = crate::credentials::file_stem(hub.address()); + root.join(format!("{stem}.chats.json")) +} + +impl ChatList { + /// The list kept for `hub`, or an empty one when none is kept or it cannot be read. + pub(super) fn load(root: &Path, hub: &HubId) -> Self { + let read = nocterm_core::fs::read_regular_bounded(&path(root, hub), MAX_FILE_BYTES); + let Ok(bytes) = read else { + return Self::default(); + }; + serde_json::from_slice::(&bytes) + .ok() + .filter(|list| list.v == FORMAT) + .unwrap_or_default() + } + + /// Keeps the list; a failure is logged, the list still works in memory. + pub(super) fn save(&self, root: &Path, hub: &HubId) { + let saved = Self { + v: FORMAT, + rows: self.rows.clone(), + started_here: self.started_here.clone(), + }; + let written = serde_json::to_vec(&saved) + .map_err(std::io::Error::other) + .and_then(|bytes| { + nocterm_core::fs::write_private_atomic(&path(root, hub), &bytes) + .map_err(std::io::Error::other) + }); + if let Err(error) = written { + tracing::warn!(%error, "the hub's chat list cannot be kept on this device"); + } + } + + /// Replaces the rows with what the hub listed; `true` when they changed. Chats started + /// here that the hub no longer lists are forgotten. + pub(super) fn replace(&mut self, rows: Vec) -> bool { + if rows == self.rows { + return false; + } + self.started_here + .retain(|_, hub_chat| rows.iter().any(|row| row.id == *hub_chat)); + self.rows = rows; + true + } + + /// Notes that the chat `local` started here is the hub chat `hub_chat`. + pub(super) fn remember(&mut self, local: &str, hub_chat: &str) { + if local != hub_chat && !self.started_here.contains_key(local) { + self.started_here + .insert(local.to_owned(), hub_chat.to_owned()); + } + } + + /// The ids the history lists the hub chats by. + pub(super) fn local_ids(&self) -> impl Iterator + '_ { + self.rows.iter().map(|row| { + self.started_here + .iter() + .find(|(_, hub_chat)| **hub_chat == row.id) + .map_or_else(|| row.id.clone(), |(local, _)| local.clone()) + }) + } + + /// The hub chat a history id stands for. + pub(super) fn row_for(&self, id: &str) -> Option<&Row> { + let hub_chat = self.started_here.get(id).map_or(id, String::as_str); + self.rows.iter().find(|row| row.id == hub_chat) + } + + /// Shows a rename or pin before the hub confirms it. + pub(super) fn apply(&mut self, hub_chat: &str, title: Option<&str>, pinned: Option) { + if let Some(row) = self.rows.iter_mut().find(|row| row.id == hub_chat) { + if let Some(title) = title { + title.clone_into(&mut row.title); + } + if let Some(pinned) = pinned { + row.pinned = pinned; + } + } + } + + /// Forgets the chat a history id stands for and answers its hub chat. + pub(super) fn forget(&mut self, id: &str) -> Option { + let hub_chat = self.row_for(id)?.id.clone(); + self.rows.retain(|row| row.id != hub_chat); + self.started_here.retain(|_, chat| *chat != hub_chat); + Some(hub_chat) + } +} diff --git a/crates/hub-io/src/chats/repository/tests.rs b/crates/hub-io/src/chats/repository/tests.rs new file mode 100644 index 00000000..a33e802a --- /dev/null +++ b/crates/hub-io/src/chats/repository/tests.rs @@ -0,0 +1,85 @@ +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubAddress, HubId}; + +use super::list::{ChatList, Row}; + +fn hub() -> HubId { + HubId::from(HubAddress::parse("hub.example:7443").unwrap()) +} + +fn row(id: &str, title: &str) -> Row { + Row::of(&v2::Chat { + id: id.into(), + agent_id: "a1".into(), + agent_name: "Ops".into(), + title: title.into(), + updated_at_ms: 5_000, + ..Default::default() + }) +} + +#[test] +fn a_chat_started_here_keeps_its_own_id_in_the_history() { + let mut list = ChatList::default(); + assert!(list.replace(vec![row("h1", "one"), row("h2", "two")])); + list.remember("local-1", "h1"); + let ids: Vec<_> = list.local_ids().collect(); + assert_eq!(ids, ["local-1", "h2"]); + assert_eq!(list.row_for("local-1").unwrap().id, "h1"); + assert_eq!(list.row_for("h2").unwrap().updated(), 5); + assert!(!list.replace(vec![row("h1", "one"), row("h2", "two")])); +} + +#[test] +fn a_chat_the_hub_no_longer_lists_is_forgotten() { + let mut list = ChatList::default(); + list.replace(vec![row("h1", "one")]); + list.remember("local-1", "h1"); + list.replace(vec![row("h2", "two")]); + assert!(list.row_for("local-1").is_none()); + assert_eq!(list.forget("h2").as_deref(), Some("h2")); + assert_eq!(list.local_ids().count(), 0); +} + +#[test] +fn a_rename_shows_before_the_hub_confirms_it_and_the_list_survives_a_restart() { + let dir = tempfile::tempdir().unwrap(); + let mut list = ChatList::default(); + list.replace(vec![row("h1", "one")]); + list.apply("h1", Some("renamed"), Some(true)); + list.save(dir.path(), &hub()); + let kept = ChatList::load(dir.path(), &hub()); + let shown = kept.row_for("h1").unwrap(); + assert_eq!(shown.title, "renamed"); + assert!(shown.pinned); + assert_eq!( + ChatList::load( + dir.path(), + &HubId::from(HubAddress::parse("other:1").unwrap()) + ), + ChatList::default() + ); +} + +#[test] +fn every_listed_hub_chat_reads_as_a_saved_chat_of_its_hub_agent() { + use nocterm_ai::local_environment::ChatScope; + let mut list = ChatList::default(); + list.replace(vec![row("h1", "one"), row("h2", "")]); + list.remember("local-1", "h1"); + let scope = ChatScope::this_computer(nocterm_ai::history::ContextId::General); + let chats: Vec<_> = list + .local_ids() + .filter_map(|id| super::saved_chat(&list, &scope, &hub(), &id)) + .collect(); + assert_eq!(chats.len(), 2); + assert_eq!(chats[0].header.title.as_deref(), Some("one")); + assert_eq!(chats[0].session_id.as_deref(), Some("h1")); + assert!(chats[1].header.title.is_none()); + assert!( + chats[0] + .header + .agent_id + .starts_with(nocterm_ai::agents::HUB_AGENT_PREFIX) + ); +} diff --git a/crates/hub-io/src/chats/state.rs b/crates/hub-io/src/chats/state.rs new file mode 100644 index 00000000..3f984e08 --- /dev/null +++ b/crates/hub-io/src/chats/state.rs @@ -0,0 +1,276 @@ +//! What one open hub chat has shown so far, and what each new item or draft adds. +//! +//! The rules are pure: the watch task feeds items and drafts in the order the hub sends them +//! and performs the outputs. A prompt of this client resolves when the first run that started +//! after its message ends; a message from another device starts a turn of its own. + +use std::collections::{HashMap, HashSet}; + +use futures::channel::oneshot; +use nocterm_ai::AgentError; +use nocterm_ai::model::content::Content; +use nocterm_ai::model::session::{PromptOutcome, SessionUpdate, StopReason, TurnActivity}; +use nocterm_hub_proto::v2; + +use super::items; + +/// What the watch task does after an item or draft. +pub(super) enum Output { + Notice(Box), + /// Ask the person whether the tool call `call` may run. + Permission { + approval: String, + call: String, + title: String, + }, + /// The prompt of this client ended. + Resolve( + oneshot::Sender>, + Result, + ), +} + +impl Output { + pub(super) fn notice(update: SessionUpdate) -> Self { + Self::Notice(Box::new(update)) + } +} + +/// A prompt this client sent and waits the end of. +struct Pending { + client_message: String, + /// The seq of its message, once the hub listed it. + after: Option, + /// Runs that were already running when the message arrived: their end is not its end. + earlier: HashSet, + done: oneshot::Sender>, +} + +#[derive(Default)] +pub(super) struct ChatState { + last_seq: u64, + sent: HashSet, + pending: Option, + /// Runs with items and no end yet. + active: HashSet, + /// Text of each run's draft already shown, and how many parts it holds. + drafts: HashMap, + /// Titles of tool calls, for the approval questions about them. + calls: HashMap, + /// Approvals asked for and not resolved yet, with their call. + approvals: HashMap, +} + +impl ChatState { + pub(super) fn last_seq(&self) -> u64 { + self.last_seq + } + + /// Whether a run is under way: the chat shows that the agent works. + pub(super) fn running(&self) -> bool { + !self.active.is_empty() + } + + /// A prompt this client is about to send under `client_message`. + pub(super) fn expect( + &mut self, + client_message: String, + done: oneshot::Sender>, + ) { + self.sent.insert(client_message.clone()); + self.pending = Some(Pending { + client_message, + after: None, + earlier: HashSet::new(), + done, + }); + } + + /// The prompt could not be sent; it waits for nothing. + pub(super) fn abandon(&mut self) -> Option>> { + self.pending.take().map(|pending| pending.done) + } + + /// Questions still open, asked again when a chat reopens. + pub(super) fn open_approvals(&self) -> Vec { + self.approvals + .iter() + .map(|(approval, call)| Output::Permission { + approval: approval.clone(), + call: call.clone(), + title: self.calls.get(call).cloned().unwrap_or_default(), + }) + .collect() + } + + /// Replays a page of history: what the chat shows, without questions or turns. + pub(super) fn replay(&mut self, item: &v2::ChatItem) -> Vec { + self.apply(item, true) + .into_iter() + .filter_map(|output| match output { + Output::Notice(update) => Some(*update), + _ => None, + }) + .collect() + } + + /// One live item of the chat. + pub(super) fn item(&mut self, item: &v2::ChatItem) -> Vec { + if item.seq <= self.last_seq { + return Vec::new(); + } + self.apply(item, false) + } + + fn apply(&mut self, item: &v2::ChatItem, replay: bool) -> Vec { + use v2::chat_item::Body; + self.last_seq = self.last_seq.max(item.seq); + if !item.run_id.is_empty() && !matches!(item.body, Some(Body::RunEnded(_))) { + self.active.insert(item.run_id.clone()); + } + let Some(body) = &item.body else { + return Vec::new(); + }; + match body { + Body::User(message) => self.user(item.seq, message, replay), + Body::Agent(text) => self.agent_text(&item.run_id, &text.text), + Body::ToolCall(call) => { + self.calls.insert(call.call_id.clone(), call.title.clone()); + vec![Output::notice(items::tool_call(call))] + } + Body::ToolResult(result) => vec![Output::notice(items::tool_result(result))], + Body::ApprovalRequest(request) => { + self.approvals + .insert(request.approval_id.clone(), request.call_id.clone()); + if replay { + return Vec::new(); + } + vec![Output::Permission { + approval: request.approval_id.clone(), + call: request.call_id.clone(), + title: self + .calls + .get(&request.call_id) + .cloned() + .unwrap_or_default(), + }] + } + Body::ApprovalResolution(resolution) => { + self.approvals.remove(&resolution.approval_id); + Vec::new() + } + Body::RunEnded(ended) => self.run_ended(item.seq, &item.run_id, ended, replay), + Body::Change(_) => Vec::new(), + } + } + + fn user(&mut self, seq: u64, message: &v2::UserMessage, replay: bool) -> Vec { + let prompt = items::prompt(message); + if replay { + return prompt + .into_iter() + .map(|part| Output::notice(SessionUpdate::UserMessageChunk(part))) + .collect(); + } + if let Some(pending) = self + .pending + .as_mut() + .filter(|pending| pending.client_message == message.client_message_id) + { + pending.after = Some(seq); + pending.earlier = self.active.clone(); + return Vec::new(); + } + if self.sent.contains(&message.client_message_id) { + return Vec::new(); + } + vec![Output::notice(SessionUpdate::Turn(TurnActivity::Started { + prompt, + }))] + } + + fn agent_text(&mut self, run: &str, text: &str) -> Vec { + let shown = self + .drafts + .remove(run) + .map(|(shown, _)| shown) + .unwrap_or_default(); + let rest = text.strip_prefix(shown.as_str()).unwrap_or(text); + if rest.is_empty() { + return Vec::new(); + } + vec![Output::notice(SessionUpdate::AgentMessageChunk( + Content::text(rest.to_owned()), + ))] + } + + /// Text the agent of run `draft.run_id` is writing; only the part not shown yet is told. + pub(super) fn draft(&mut self, draft: &v2::Draft) -> Vec { + let (shown, parts) = self.drafts.entry(draft.run_id.clone()).or_default(); + let whole = if draft.snapshot { + draft.text.clone() + } else if draft.part == *parts + 1 { + format!("{shown}{}", draft.text) + } else { + return Vec::new(); + }; + *parts = draft.part; + let Some(rest) = whole.strip_prefix(shown.as_str()) else { + return Vec::new(); + }; + let rest = rest.to_owned(); + *shown = whole; + if rest.is_empty() { + return Vec::new(); + } + vec![Output::notice(SessionUpdate::AgentMessageChunk( + Content::text(rest), + ))] + } + + fn run_ended( + &mut self, + seq: u64, + run: &str, + ended: &v2::RunEnded, + replay: bool, + ) -> Vec { + let status = v2::RunStatus::try_from(ended.status).unwrap_or(v2::RunStatus::Completed); + self.drafts.remove(run); + if status == v2::RunStatus::Interrupted { + // The run goes on as a new run; nothing ended for the person. + self.active.remove(run); + return Vec::new(); + } + self.active.remove(run); + self.approvals.clear(); + let mut outputs: Vec = items::run_error(ended) + .map(Output::notice) + .into_iter() + .collect(); + if replay { + return outputs; + } + let answers_pending = self.pending.as_ref().is_some_and(|pending| { + pending.after.is_some_and(|after| seq > after) && !pending.earlier.contains(run) + }); + if answers_pending && let Some(pending) = self.pending.take() { + let stop_reason = match status { + v2::RunStatus::Stopped => StopReason::Cancelled, + v2::RunStatus::Limit => StopReason::MaxTurnRequests, + _ => StopReason::EndTurn, + }; + let outcome = PromptOutcome { + stop_reason, + usage: items::usage(ended), + }; + outputs.push(Output::Resolve(pending.done, Ok(outcome))); + } else { + outputs.push(Output::notice(SessionUpdate::Turn(TurnActivity::Ended))); + } + outputs + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/chats/state/tests.rs b/crates/hub-io/src/chats/state/tests.rs new file mode 100644 index 00000000..170cc8a5 --- /dev/null +++ b/crates/hub-io/src/chats/state/tests.rs @@ -0,0 +1,159 @@ +use futures::channel::oneshot; +use nocterm_ai::model::session::{SessionUpdate, StopReason, TurnActivity}; +use nocterm_hub_proto::v2::{self, chat_item::Body}; + +use super::{ChatState, Output}; + +fn item(seq: u64, run: &str, body: Body) -> v2::ChatItem { + v2::ChatItem { + seq, + at_ms: 0, + run_id: run.into(), + body: Some(body), + } +} + +fn user(seq: u64, client_message: &str, text: &str) -> v2::ChatItem { + item( + seq, + "", + Body::User(v2::UserMessage { + text: text.into(), + client_message_id: client_message.into(), + ..Default::default() + }), + ) +} + +fn ended(seq: u64, run: &str, status: v2::RunStatus) -> v2::ChatItem { + item( + seq, + run, + Body::RunEnded(v2::RunEnded { + status: status.into(), + ..Default::default() + }), + ) +} + +fn text(seq: u64, run: &str, text: &str) -> v2::ChatItem { + item(seq, run, Body::Agent(v2::AgentText { text: text.into() })) +} + +fn chunks(outputs: &[Output]) -> Vec { + outputs + .iter() + .filter_map(|output| match output { + Output::Notice(update) => match update.as_ref() { + SessionUpdate::AgentMessageChunk(nocterm_ai::model::content::Content::Text( + text, + )) => Some(text.text.clone()), + _ => None, + }, + _ => None, + }) + .collect() +} + +#[test] +fn a_prompt_ends_with_the_run_that_started_after_it() { + let mut chat = ChatState::default(); + chat.item(&text(1, "r0", "earlier run")); + let (done, mut answer) = oneshot::channel(); + chat.expect("m1".into(), done); + chat.item(&user(2, "m1", "hello")); + let outputs = chat.item(&ended(3, "r0", v2::RunStatus::Completed)); + assert!(matches!( + outputs.last(), + Some(Output::Notice(update)) if matches!(**update, SessionUpdate::Turn(TurnActivity::Ended)) + )); + assert!(answer.try_recv().unwrap().is_none()); + chat.item(&text(4, "r1", "hi")); + let outputs = chat.item(&ended(5, "r1", v2::RunStatus::Stopped)); + let Some(Output::Resolve(sender, result)) = outputs.into_iter().last() else { + panic!("the prompt resolves"); + }; + let _ = sender.send(result); + let outcome = answer.try_recv().unwrap().unwrap().unwrap(); + assert_eq!(outcome.stop_reason, StopReason::Cancelled); +} + +#[test] +fn a_message_from_another_device_starts_and_ends_a_turn() { + let mut chat = ChatState::default(); + let outputs = chat.item(&user(1, "other-device", "deploy")); + assert!(matches!( + outputs.as_slice(), + [Output::Notice(update)] + if matches!(&**update, SessionUpdate::Turn(TurnActivity::Started { prompt }) if prompt.len() == 1) + )); + chat.item(&text(2, "r1", "done")); + assert!(chat.running()); + let outputs = chat.item(&ended(3, "r1", v2::RunStatus::Completed)); + assert!(matches!( + outputs.as_slice(), + [Output::Notice(update)] if matches!(**update, SessionUpdate::Turn(TurnActivity::Ended)) + )); + assert!(!chat.running()); +} + +#[test] +fn a_draft_shows_each_new_part_once_and_the_final_text_adds_only_the_rest() { + let mut chat = ChatState::default(); + let draft = |part, text: &str, snapshot| v2::Draft { + chat_id: "c".into(), + run_id: "r".into(), + part, + text: text.into(), + snapshot, + }; + assert_eq!(chunks(&chat.draft(&draft(1, "Hel", false))), ["Hel"]); + assert!(chat.draft(&draft(3, "skipped", false)).is_empty()); + assert_eq!(chunks(&chat.draft(&draft(2, "lo", false))), ["lo"]); + assert_eq!(chunks(&chat.draft(&draft(2, "Hello wo", true))), [" wo"]); + assert_eq!(chunks(&chat.item(&text(1, "r", "Hello world"))), ["rld"]); +} + +#[test] +fn a_replayed_question_is_not_asked_but_stays_open_until_resolved() { + let mut chat = ChatState::default(); + let call = item( + 1, + "r", + Body::ToolCall(v2::ToolCall { + call_id: "c1".into(), + tool: "server.run".into(), + title: "prod · reboot".into(), + ..Default::default() + }), + ); + let ask = item( + 2, + "r", + Body::ApprovalRequest(v2::ApprovalRequest { + approval_id: "a1".into(), + call_id: "c1".into(), + }), + ); + assert_eq!(chat.replay(&call).len(), 1); + assert!(chat.replay(&ask).is_empty()); + let open = chat.open_approvals(); + assert!(matches!( + open.as_slice(), + [Output::Permission { approval, title, .. }] if approval == "a1" && title == "prod · reboot" + )); + chat.item(&item( + 3, + "r", + Body::ApprovalResolution(v2::ApprovalResolution { + approval_id: "a1".into(), + ..Default::default() + }), + )); + assert!(chat.open_approvals().is_empty()); + assert_eq!(chat.last_seq(), 3); + assert!( + chat.item(&ask).is_empty(), + "an old item is not applied twice" + ); +} diff --git a/crates/hub-io/src/chats/upload.rs b/crates/hub-io/src/chats/upload.rs new file mode 100644 index 00000000..7a2f4358 --- /dev/null +++ b/crates/hub-io/src/chats/upload.rs @@ -0,0 +1,129 @@ +//! Images and files of a prompt, uploaded to the chat in parts before the message is sent. A +//! file is a link to where it is on this device; its bytes are read when the message goes. + +use base64::Engine as _; +use nocterm_ai::model::content::Content; +use nocterm_hub_client::HubClient; +use nocterm_hub_proto::v2; +use sha2::{Digest as _, Sha256}; + +use super::connector::CallFailure; + +/// The largest part sent when the hub names none. +const PART_BYTES: usize = 1024 * 1024; + +/// The text of a prompt and the attachments its other parts became. +pub(super) async fn prepare( + client: &HubClient, + chat: &str, + content: Vec, +) -> Result<(String, Vec), CallFailure> { + let mut text = String::new(); + let mut attachments = Vec::new(); + for (index, part) in content.into_iter().enumerate() { + match part { + Content::Text(value) => { + if !text.is_empty() { + text.push('\n'); + } + text.push_str(&value.text); + } + Content::Image(image) => { + let bytes = base64::engine::general_purpose::STANDARD + .decode(image.data.as_bytes()) + .map_err(|_| CallFailure::Refused("The image could not be read.".into()))?; + let extension = image.mime_type.rsplit('/').next().unwrap_or("png"); + let name = format!("image-{}.{extension}", index + 1); + attachments.push(upload(client, chat, &name, &image.mime_type, &bytes).await?); + } + // A file attached on this device: the hub agent gets its bytes. + Content::ResourceReference(link) => { + let Some(path) = nocterm_ai::prompt_files::path_of(&link.uri) else { + continue; + }; + let bytes = read_file(path).await?; + let mime = link + .mime_type + .clone() + .unwrap_or_else(|| "application/octet-stream".to_owned()); + attachments.push(upload(client, chat, &link.name, &mime, &bytes).await?); + } + // Audio and embedded resources are not something a hub agent reads. + _ => {} + } + } + Ok((text, attachments)) +} + +/// The most bytes of one attached file this device reads; the hub's own limit, usually lower, +/// refuses a larger upload with its reason. +const MAX_FILE_BYTES: u64 = 256 * 1024 * 1024; + +/// The bytes of an attached file, read off the adapter's async threads. +async fn read_file(path: std::path::PathBuf) -> Result, CallFailure> { + let shown = path.display().to_string(); + let read = tokio::task::spawn_blocking(move || { + use std::io::Read as _; + let file = std::fs::File::open(&path)?; + let mut bytes = Vec::new(); + file.take(MAX_FILE_BYTES + 1).read_to_end(&mut bytes)?; + Ok::<_, std::io::Error>(bytes) + }) + .await; + match read { + Ok(Ok(bytes)) if bytes.len() as u64 <= MAX_FILE_BYTES => Ok(bytes), + Ok(Ok(_)) => Err(CallFailure::Refused(format!( + "{shown} is larger than {} MiB.", + MAX_FILE_BYTES / (1024 * 1024) + ))), + Ok(Err(_)) | Err(_) => Err(CallFailure::Refused(format!("{shown} could not be read."))), + } +} + +async fn upload( + client: &HubClient, + chat: &str, + name: &str, + mime: &str, + bytes: &[u8], +) -> Result { + let sha256 = hex::encode(Sha256::digest(bytes)); + let begun = client + .chats() + .begin_upload(v2::BeginUploadRequest { + chat_id: chat.to_owned(), + name: name.to_owned(), + mime: mime.to_owned(), + size: bytes.len() as u64, + sha256, + }) + .await? + .into_inner(); + let part = match usize::try_from(begun.part_size) { + Ok(0) | Err(_) => PART_BYTES, + Ok(part) => part.min(PART_BYTES), + }; + let mut offset = usize::try_from(begun.received) + .unwrap_or(0) + .min(bytes.len()); + while offset < bytes.len() { + let end = (offset + part).min(bytes.len()); + client + .chats() + .upload_part(v2::UploadPartRequest { + upload_id: begun.upload_id.clone(), + offset: offset as u64, + data: bytes[offset..end].to_vec(), + }) + .await?; + offset = end; + } + let finished = client + .chats() + .finish_upload(v2::IdRequest { + id: begun.upload_id, + }) + .await? + .into_inner(); + Ok(finished.id) +} diff --git a/crates/hub-io/src/chats/watch.rs b/crates/hub-io/src/chats/watch.rs new file mode 100644 index 00000000..fdd1629b --- /dev/null +++ b/crates/hub-io/src/chats/watch.rs @@ -0,0 +1,247 @@ +//! One `Watch` per open chat: its new items, the text its agent is writing and its settings, +//! told to the chat as session updates until the chat closes. + +use std::sync::{Arc, Mutex, PoisonError, Weak}; +use std::time::Duration; + +use nocterm_ai::model::{ + auth::{PermissionChoice, PermissionEffect, PermissionOutcome, ProviderPermission}, + controls::Change, + ids::ToolId, + session::{SessionInfo, SessionNotice, SessionUpdate, TurnActivity}, + tools::{ToolChanges, ToolPatch}, +}; +use nocterm_ai::{AgentEvent, PermissionResponder}; +use nocterm_hub_proto::v2; + +use super::connector::{Inner, fresh}; +use super::options::ChatOptions; +use super::state::{ChatState, Output}; + +/// How long a broken stream waits before it is opened again. +const RETRY: Duration = Duration::from_secs(3); + +const ALLOW: &str = "allow"; +const ALLOW_IN_CHAT: &str = "allow_chat"; +const DENY: &str = "deny"; + +/// The task that follows one chat. It holds the connection weakly: closing the chat or the +/// connection ends it. +pub(super) struct Follow { + pub(super) inner: Weak, + pub(super) chat: String, + pub(super) state: Arc>, + pub(super) options: Arc>, +} + +impl Follow { + /// Follows the chat; `opening` are the questions a reopened chat left open, and `running` + /// says a run was under way when it was read. + pub(super) async fn run(self, opening: Vec, running: bool) { + let mut first = Some((opening, running)); + loop { + let Some(inner) = self.inner.upgrade() else { + return; + }; + let after = self + .state + .lock() + .unwrap_or_else(PoisonError::into_inner) + .last_seq(); + let stream = async { + let client = fresh(&inner.captured).await.ok()?; + let request = v2::WatchRequest { + chats_after_rev: 0, + open: vec![v2::OpenChat { + chat_id: self.chat.clone(), + after_seq: after, + }], + }; + client + .chats() + .watch(request) + .await + .ok() + .map(|s| s.into_inner()) + } + .await; + drop(inner); + if let Some(mut stream) = stream { + if let Some((opening, running)) = first.take() { + if running { + self.notice(SessionUpdate::Turn(TurnActivity::Started { + prompt: Vec::new(), + })); + } + self.perform(opening); + } + while let Ok(Some(event)) = stream.message().await { + match self.event(event) { + Next::Continue => {} + Next::Reopen => break, + Next::End => return, + } + } + } + tokio::time::sleep(RETRY).await; + } + } + + fn event(&self, event: v2::WatchEvent) -> Next { + use v2::watch_event::Event; + let outputs = match event.event { + Some(Event::Item(event)) if event.chat_id == self.chat => match &event.item { + Some(item) => self.state().item(item), + None => Vec::new(), + }, + Some(Event::Draft(draft)) if draft.chat_id == self.chat => self.state().draft(&draft), + Some(Event::Chat(chat)) if chat.id == self.chat => self.settings(&chat), + Some(Event::ChatDeleted(id)) if id == self.chat => { + if let Some(done) = self.state().abandon() { + let _ = done.send(Err(nocterm_ai::AgentError::Io( + "The chat was deleted.".into(), + ))); + } + return Next::End; + } + Some(Event::Account(changed)) + if changed.list == i32::from(v2::AccountList::Servers) => + { + self.servers_changed(); + Vec::new() + } + Some(Event::Resync(_)) => return Next::Reopen, + _ => Vec::new(), + }; + self.perform(outputs); + Next::Continue + } + + fn state(&self) -> std::sync::MutexGuard<'_, ChatState> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// A changed title, model or set of servers of the chat, told as metadata and options. + fn settings(&self, chat: &v2::Chat) -> Vec { + let mut options = self.options.lock().unwrap_or_else(PoisonError::into_inner); + let mut outputs = Vec::new(); + if chat.title != options.title() && !chat.title.is_empty() { + outputs.push(Output::notice(SessionUpdate::SessionInfoUpdate( + SessionInfo { + title: Change::Set(chat.title.clone()), + meta: None, + }, + ))); + } + let differs = options.differs(chat); + *options = options.stored(chat); + if differs && let Some(inner) = self.inner.upgrade() { + outputs.push(Output::notice(SessionUpdate::ConfigOptionUpdate( + inner.config(&options), + ))); + } + outputs + } + + /// The account's servers changed: they are listed again and the chat shows them. + fn servers_changed(&self) { + let Some(inner) = self.inner.upgrade() else { + return; + }; + let options = self.options.clone(); + let chat = self.chat.clone(); + let weak = self.inner.clone(); + inner.handle.spawn(async move { + let Some(inner) = weak.upgrade() else { + return; + }; + if !inner.list_servers().await { + return; + } + let shown = inner.config(&options.lock().unwrap_or_else(PoisonError::into_inner)); + let notice = SessionNotice::new(chat, SessionUpdate::ConfigOptionUpdate(shown)); + let _ = inner.events.try_send(AgentEvent::Session(notice)); + }); + } + + fn perform(&self, outputs: Vec) { + for output in outputs { + match output { + Output::Notice(update) => self.notice(*update), + Output::Permission { + approval, + call, + title, + } => self.ask(approval, call, title), + Output::Resolve(done, result) => { + let _ = done.send(result); + } + } + } + } + + fn notice(&self, update: SessionUpdate) { + if let Some(inner) = self.inner.upgrade() { + let notice = SessionNotice::new(self.chat.clone(), update); + let _ = inner.events.try_send(AgentEvent::Session(notice)); + } + } + + /// Asks the person about a tool call and sends the answer to the hub. Any device may answer + /// first; a later answer here changes nothing. + fn ask(&self, approval: String, call: String, title: String) { + let Some(inner) = self.inner.upgrade() else { + return; + }; + let (respond, answer) = PermissionResponder::channel(); + let request = ProviderPermission::new( + self.chat.clone(), + ToolPatch::new(ToolId::new(call), ToolChanges::default().title(title)), + vec![ + PermissionChoice::new(ALLOW, "Allow", PermissionEffect::AllowOnce), + PermissionChoice::new( + ALLOW_IN_CHAT, + "Allow in this chat", + PermissionEffect::AllowRemembered, + ), + PermissionChoice::new(DENY, "Deny", PermissionEffect::DenyOnce), + ], + ); + if inner + .events + .try_send(AgentEvent::Permission { request, respond }) + .is_err() + { + return; + } + let chat = self.chat.clone(); + let captured = inner.captured.clone(); + inner.handle.spawn(async move { + let Ok(PermissionOutcome::Selected(choice)) = answer.await else { + return; + }; + let decision = match choice.as_str() { + ALLOW => v2::ApprovalDecision::AllowOnce, + ALLOW_IN_CHAT => v2::ApprovalDecision::AllowChat, + _ => v2::ApprovalDecision::Deny, + }; + let Ok(client) = fresh(&captured).await else { + return; + }; + let request = v2::ResolveApprovalRequest { + chat_id: chat, + approval_id: approval, + decision: decision.into(), + }; + if let Err(status) = client.chats().resolve_approval(request).await { + tracing::debug!(code = ?status.code(), "the approval was not taken"); + } + }); + } +} + +enum Next { + Continue, + Reopen, + End, +} diff --git a/crates/hub-io/src/checked.rs b/crates/hub-io/src/checked.rs new file mode 100644 index 00000000..cff31b5b --- /dev/null +++ b/crates/hub-io/src/checked.rs @@ -0,0 +1,66 @@ +//! Poll admission under a caller's original authority, including completed work. +use std::future::{Future, poll_fn}; +use std::task::Poll; + +pub(crate) async fn during( + check: impl Fn() -> Result<(), E>, + future: impl Future, +) -> Result { + let mut future = std::pin::pin!(future); + poll_fn(|cx| { + check()?; + match future.as_mut().poll(cx) { + Poll::Pending => Poll::Pending, + Poll::Ready(value) => Poll::Ready(check().map(|()| value)), + } + }) + .await +} + +#[cfg(test)] +mod tests { + use super::*; + use std::cell::Cell; + + #[test] + fn invalid_authority_never_polls_the_operation() { + let polled = Cell::new(false); + let result = + futures::executor::block_on(during(|| Err("stale"), async { polled.set(true) })); + assert_eq!(result, Err("stale")); + assert!(!polled.get()); + } + + #[test] + fn resumed_work_rechecks_authority_before_polling_again() { + let valid = Cell::new(true); + let polls = Cell::new(0); + let work = poll_fn(|cx| { + polls.set(polls.get() + 1); + valid.set(false); + cx.waker().wake_by_ref(); + Poll::<()>::Pending + }); + let result = + futures::executor::block_on(during(|| valid.get().then_some(()).ok_or("stale"), work)); + assert_eq!(result, Err("stale")); + assert_eq!(polls.get(), 1); + } + + #[test] + fn completion_rechecks_authority_before_delivering_the_result() { + let valid = Cell::new(true); + let result = futures::executor::block_on(during( + || valid.get().then_some(()).ok_or("stale"), + async { + valid.set(false); + 7 + }, + )); + assert_eq!(result, Err("stale")); + assert_eq!( + futures::executor::block_on(during(|| Ok::<_, ()>(()), async { 7 })), + Ok(7) + ); + } +} diff --git a/crates/hub-io/src/classify.rs b/crates/hub-io/src/classify.rs new file mode 100644 index 00000000..074d33f1 --- /dev/null +++ b/crates/hub-io/src/classify.rs @@ -0,0 +1,170 @@ +//! What went wrong, in the terms of `HubError`. +//! +//! Every function keeps the original failure as the `source()` of the error it returns. + +use std::error::Error; + +use nocterm_core::SharedCause; +use nocterm_hub_client::{ClientError, HubEndpoint, OidcError, ProbeError, StatusCode}; +use nocterm_hubs_model::{Fingerprint, HubAddress, HubEndpointSpec, HubError, TrustPolicy}; +use nocterm_keys::LoginError; + +use crate::credentials::CredentialError; + +pub(crate) fn remote(error: impl Error + Send + Sync + 'static) -> HubError { + HubError::Remote { + cause: SharedCause::new(error), + } +} + +pub(crate) fn credentials(error: impl Error + Send + Sync + 'static) -> HubError { + HubError::Credentials { + cause: SharedCause::new(error), + } +} + +pub(crate) fn credential_file(error: CredentialError) -> HubError { + match error { + CredentialError::StaleLogin => HubError::Stale, + other => credentials(other), + } +} + +/// Whether the hub cannot be asked now: it does not answer, or this device has no (more) +/// credentials for it. Work that needs the hub waits; work that has a local copy goes on. +pub(crate) fn unreachable(error: &HubError) -> bool { + matches!( + error, + HubError::Unreachable { .. } | HubError::NotSignedIn | HubError::Revoked + ) +} + +/// How a call to a hub that was signed in to, or not, is classified. +enum Kind { + Settled(HubError), + Unreachable, + Credentials, + Remote, +} + +fn kind(error: &ClientError) -> Kind { + match error { + ClientError::UpgradeRequired { hub, client } => Kind::Settled(HubError::UpgradeRequired { + hub: *hub, + client: *client, + }), + ClientError::NotSignedIn => Kind::Settled(HubError::NotSignedIn), + ClientError::Revoked => Kind::Settled(HubError::Revoked), + ClientError::Oidc(OidcError::TimedOut) => Kind::Settled(HubError::SignInTimedOut), + ClientError::Oidc(OidcError::Provider { .. } | OidcError::NoCode) => { + Kind::Settled(HubError::SignInCancelled) + } + ClientError::SessionStore(_) | ClientError::Randomness(_) => Kind::Credentials, + ClientError::Connect(_) => Kind::Unreachable, + ClientError::Status { code, .. } => match code { + StatusCode::Unauthenticated => Kind::Settled(HubError::NotSignedIn), + StatusCode::PermissionDenied => Kind::Settled(HubError::PermissionDenied), + StatusCode::NotFound => Kind::Settled(HubError::NotFound), + StatusCode::Unavailable | StatusCode::DeadlineExceeded | StatusCode::Cancelled => { + Kind::Unreachable + } + _ => Kind::Remote, + }, + _ => Kind::Remote, + } +} + +/// A failure of a call on a hub at `address`. +pub(crate) fn classify(address: &HubAddress, error: ClientError) -> HubError { + match kind(&error) { + Kind::Settled(settled) => settled, + Kind::Unreachable => HubError::Unreachable { + address: address.clone(), + cause: SharedCause::new(error), + }, + Kind::Credentials => credentials(error), + Kind::Remote => remote(error), + } +} + +/// Which sign-in method failed. +#[derive(Clone, Copy)] +pub(crate) enum Method { + Password, + Oidc, +} + +/// A failure of a sign-in: a refusal means something else here than on any other call. +pub(crate) fn classify_sign_in( + address: &HubAddress, + method: Method, + error: ClientError, +) -> HubError { + // A password the hub could not have accepted is refused before it is sent. + if let ( + Method::Password, + ClientError::Login(LoginError::PasswordTooShort { .. } | LoginError::EmptyUsername), + ) = (method, &error) + { + return HubError::InvalidCredentials; + } + if let ClientError::Status { code, .. } = &error { + match (method, code) { + ( + Method::Password, + StatusCode::Unauthenticated + | StatusCode::InvalidArgument + | StatusCode::NotFound + | StatusCode::PermissionDenied, + ) => return HubError::InvalidCredentials, + (_, StatusCode::FailedPrecondition | StatusCode::Unimplemented) + | (Method::Oidc, StatusCode::NotFound | StatusCode::InvalidArgument) => { + return HubError::MethodUnavailable; + } + _ => {} + } + } + classify(address, error) +} + +/// A failure to connect. The client only says the connection failed, so the certificate the +/// hub presents is read and compared with the decided trust to tell a changed certificate +/// from a hub that is simply not there. +pub(crate) async fn explain_connect(spec: &HubEndpointSpec, error: ClientError) -> HubError { + if !matches!(error, ClientError::Connect(_)) { + return classify(&spec.address, error); + } + let Ok(probe) = HubEndpoint::probe(&spec.address.to_string()).await else { + return classify(&spec.address, error); + }; + let presented = Fingerprint::from_bytes(probe.fingerprint); + match spec.trust { + TrustPolicy::Pinned(pinned) if pinned != presented => HubError::CertificateChanged { + address: spec.address.clone(), + pinned, + presented, + }, + TrustPolicy::PublicCa if !probe.publicly_trusted => HubError::CertificateUntrusted { + address: spec.address.clone(), + presented, + }, + _ => classify(&spec.address, error), + } +} + +/// A failure to read the certificate of a hub. +pub(crate) fn probe_failed(address: &HubAddress, error: ProbeError) -> HubError { + match error { + ProbeError::Unreachable(_) + | ProbeError::Handshake(_) + | ProbeError::TimedOut + | ProbeError::NoCertificate => HubError::Unreachable { + address: address.clone(), + cause: SharedCause::new(error), + }, + _ => remote(error), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/classify/tests.rs b/crates/hub-io/src/classify/tests.rs new file mode 100644 index 00000000..e4e4286c --- /dev/null +++ b/crates/hub-io/src/classify/tests.rs @@ -0,0 +1,171 @@ +use nocterm_hub_client::{ConnectError, EndpointError, OidcError}; +use tonic::{Code, Status}; + +use super::*; + +fn address() -> HubAddress { + HubAddress::parse("hub.example").unwrap() +} + +fn status(code: Code) -> ClientError { + ClientError::from(Status::new(code, "details the user never sees")) +} + +fn coded(code: Code, hub_code: &str) -> ClientError { + let mut status = Status::new(code, "revoked"); + status + .metadata_mut() + .insert("x-nocterm-error", hub_code.parse().unwrap()); + ClientError::from(status) +} + +#[test] +fn a_revoked_device_is_revoked_however_the_hub_says_it() { + assert!(matches!( + classify(&address(), ClientError::Revoked), + HubError::Revoked + )); + let wire = coded(Code::Unauthenticated, "ERROR_CODE_DEVICE_REVOKED"); + assert!(matches!(classify(&address(), wire), HubError::Revoked)); +} + +#[test] +fn statuses_become_the_errors_a_caller_acts_on() { + assert!(matches!( + classify(&address(), status(Code::Unauthenticated)), + HubError::NotSignedIn + )); + assert!(matches!( + classify(&address(), status(Code::PermissionDenied)), + HubError::PermissionDenied + )); + assert!(matches!( + classify(&address(), status(Code::NotFound)), + HubError::NotFound + )); + for code in [Code::Unavailable, Code::DeadlineExceeded] { + assert!(matches!( + classify(&address(), status(code)), + HubError::Unreachable { .. } + )); + } + assert!(matches!( + classify(&address(), status(Code::Internal)), + HubError::Remote { .. } + )); +} + +#[test] +fn an_old_client_is_told_both_versions() { + let error = ClientError::UpgradeRequired { hub: 3, client: 1 }; + assert!(matches!( + classify(&address(), error), + HubError::UpgradeRequired { hub: 3, client: 1 } + )); +} + +#[test] +fn a_connection_that_failed_is_unreachable_and_keeps_its_cause() { + let error = ClientError::Connect(ConnectError::Endpoint(EndpointError::Address)); + let classified = classify(&address(), error); + let HubError::Unreachable { address: at, .. } = &classified else { + panic!("{classified:?}"); + }; + assert_eq!(at.to_string(), "hub.example:7443"); + let cause = classified.source().unwrap(); + let inner = cause + .source() + .and_then(|source| source.downcast_ref::()) + .or_else(|| cause.downcast_ref::()); + assert!(matches!(inner, Some(ClientError::Connect(_))), "{cause:?}"); +} + +#[test] +fn a_failure_to_keep_tokens_is_a_credentials_error() { + let error = ClientError::SessionStore(std::io::Error::other("disk")); + assert!(matches!( + classify(&address(), error), + HubError::Credentials { .. } + )); +} + +#[test] +fn browser_sign_in_failures_say_why() { + let timed_out = ClientError::Oidc(OidcError::TimedOut); + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, timed_out), + HubError::SignInTimedOut + )); + let refused = ClientError::Oidc(OidcError::Provider { + error: "access_denied".into(), + description: String::new(), + }); + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, refused), + HubError::SignInCancelled + )); + let loopback = ClientError::Oidc(OidcError::Loopback(std::io::Error::other("bind"))); + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, loopback), + HubError::Remote { .. } + )); +} + +#[test] +fn a_refused_password_is_invalid_credentials_only_when_signing_in() { + for code in [ + Code::Unauthenticated, + Code::InvalidArgument, + Code::PermissionDenied, + ] { + assert!(matches!( + classify_sign_in(&address(), Method::Password, status(code)), + HubError::InvalidCredentials + )); + } + assert!(matches!( + classify(&address(), status(Code::InvalidArgument)), + HubError::Remote { .. } + )); +} + +#[test] +fn a_password_too_short_for_any_account_is_invalid_credentials() { + let short = || ClientError::Login(LoginError::PasswordTooShort { min: 8 }); + assert!(matches!( + classify_sign_in(&address(), Method::Password, short()), + HubError::InvalidCredentials + )); +} + +#[test] +fn a_method_the_hub_does_not_offer_is_unavailable() { + for method in [Method::Password, Method::Oidc] { + assert!(matches!( + classify_sign_in(&address(), method, status(Code::Unimplemented)), + HubError::MethodUnavailable + )); + } + assert!(matches!( + classify_sign_in(&address(), Method::Oidc, status(Code::NotFound)), + HubError::MethodUnavailable + )); +} + +#[test] +fn a_hub_that_is_not_there_is_unreachable_not_a_changed_certificate() { + let spec = HubEndpointSpec { + address: HubAddress::parse("127.0.0.1:1").unwrap(), + trust: TrustPolicy::Pinned(Fingerprint::from_bytes([1; 32])), + }; + let error = ClientError::Connect(ConnectError::Endpoint(EndpointError::Address)); + let classified = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap() + .block_on(explain_connect(&spec, error)); + assert!( + matches!(classified, HubError::Unreachable { .. }), + "{classified:?}" + ); +} diff --git a/crates/hub-io/src/connect.rs b/crates/hub-io/src/connect.rs new file mode 100644 index 00000000..bb8dc00a --- /dev/null +++ b/crates/hub-io/src/connect.rs @@ -0,0 +1,182 @@ +//! Connecting to a hub and signing in or out. + +use std::sync::Arc; + +use futures::channel::oneshot; +use nocterm_hub_client::{HubClient, Session}; +use nocterm_hubs_model::{ + Account, HubEndpointSpec, HubError, HubId, KeyStatus, SignedIn, TrustPolicy, +}; +use nocterm_keys::DeviceKey; +use nocterm_session::Secret; + +use crate::account::{KeyFlow, key_admission::KeyAdmission}; +use crate::classify::{Method, classify_sign_in, explain_connect}; +use crate::credentials::{CredentialFile, CredentialOrigin, LoginPublication}; +use crate::directory::who_am_i; +use crate::endpoint::endpoint; +use crate::pool::Pool; +use crate::pool::observation::LoginObservation; +use zeroize::Zeroize as _; + +mod login; +mod request; +mod sign_out; +mod start; +pub(crate) use request::Reconnect; +pub(crate) use sign_out::SignOut; +pub(crate) use start::LoginStart; + +/// Connects to hubs and keeps the credentials of the ones the device signs in to. +pub(crate) struct Connector { + pool: Arc, + keys: Arc, +} + +/// A sign-in under way: the client and the file its tokens will be kept in. A file created for +/// a sign-in that did not finish is removed, so a hub never signed in to leaves no key behind. +struct Attempt { + hub: HubId, + file: CredentialFile, + client: HubClient, + trust: TrustPolicy, + created: bool, + finished: bool, + origin: CredentialOrigin, + observed: Arc, +} + +impl Drop for Attempt { + fn drop(&mut self) { + if self.created && !self.finished { + let _ = self.file.erase_unpublished(self.origin.public_key()); + } + } +} + +impl Connector { + pub(crate) fn new(pool: Arc, keys: Arc) -> Self { + Self { pool, keys } + } + + pub(crate) fn reconnect(&self, spec: HubEndpointSpec) -> Result { + Reconnect::capture(self.pool.clone(), spec) + } + pub(crate) fn begin(&self, spec: HubEndpointSpec) -> Result { + LoginStart::capture(self.pool.clone(), spec) + } + pub(crate) fn sign_out(&self, hub: &HubId) -> Result { + SignOut::capture(self.pool.clone(), hub.clone()) + } + + /// Keeps the tokens of a finished sign-in and reads who signed in, with the client that + /// signed in. + async fn complete( + &self, + mut attempt: Attempt, + mut session: Session, + ) -> Result<(Account, HubClient, Arc), HubError> { + let client = attempt.client.clone(); + let published = self.pool.publish_login( + attempt.hub.clone(), + attempt.trust, + client.clone(), + LoginPublication::new(&attempt.file, &session, &attempt.origin), + &attempt.observed.snapshot(), + ); + session.access_token.zeroize(); + session.refresh_token.zeroize(); + let published = published?; + // After this point even a stale result never owns an unpublished file. + attempt.finished = true; + let (_, subject) = published.into_parts(); + attempt.observed.became(&subject)?; + let owner = self.keys.published(&attempt.hub, &subject)?; + let account = who_am_i(&client, &owner).await?; + Ok((account, client, owner)) + } + + pub(crate) async fn password( + &self, + start: LoginStart, + username: String, + password: Secret, + ) -> Result { + let attempt = start.open().await?; + let hub = attempt.hub.clone(); + let signed_in = attempt + .observed + .during( + attempt + .client + .password_login(&username, password.expose(), self.pool.device()), + ) + .await? + .map_err(|error| classify_sign_in(hub.address(), Method::Password, error))?; + let secrets = signed_in.secrets; + let (account, client, owner) = self.complete(attempt, signed_in.session).await?; + // The login password also derives the key that opens the account's keyset. + let keys = + self.keys + .after_password(&owner, &client, account.id.as_str(), &username, secrets); + let keys = owner.during(keys).await??; + Ok(SignedIn { + account, + keys, + login: login::receipt(&self.pool, owner.member().clone()), + }) + } + + /// Signs in through the browser. `browser` receives the address to open. + pub(crate) async fn oidc( + &self, + start: LoginStart, + provider: String, + browser: oneshot::Sender, + ) -> Result { + let attempt = start.open().await?; + let hub = attempt.hub.clone(); + let observed = attempt.observed.clone(); + let session = attempt + .observed + .during( + attempt + .client + .oidc_login(&provider, self.pool.device(), |url| { + if observed.check().is_ok() { + let _ = browser.send(url.to_owned()); + } + }), + ) + .await? + .map_err(|error| classify_sign_in(hub.address(), Method::Oidc, error))?; + let (account, _, owner) = self.complete(attempt, session).await?; + let keys: KeyStatus = self.keys.after_browser(&owner)?; + Ok(SignedIn { + account, + keys, + login: login::receipt(&self.pool, owner.member().clone()), + }) + } + + /// Explicit removal of the configured hub is separate from an account worker. + pub(crate) fn forget(&self, hub: &HubId) -> Result<(), HubError> { + self.pool.forget_hub(hub) + } +} + +/// A client for the hub with the decided trust; a failure says why the hub was not reached. +async fn open( + spec: &HubEndpointSpec, + key: DeviceKey, + session: Option, +) -> Result { + let endpoint = endpoint(spec)?; + match HubClient::connect(&endpoint, key, session).await { + Ok(client) => Ok(client), + Err(error) => Err(explain_connect(spec, error).await), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/connect/login.rs b/crates/hub-io/src/connect/login.rs new file mode 100644 index 00000000..22f2f5d9 --- /dev/null +++ b/crates/hub-io/src/connect/login.rs @@ -0,0 +1,31 @@ +//! A successful sign-in hands its original native cleanup authority to the consumer. +use crate::{account::source::CapturedMembership, pool::Pool}; +use nocterm_hubs_model::{HubError, LoginOwner, LoginReceipt}; +use std::sync::{Arc, Weak}; +struct Owner { + pool: Weak, + member: Arc, +} +impl LoginOwner for Owner { + fn check_current(&self) -> Result<(), HubError> { + self.member.check() + } + fn discard(&self) -> Result<(), HubError> { + self.member.check()?; + let pool = self.pool.upgrade().ok_or(HubError::Stale)?; + let subject = self.member.subject()?; + let (_, result) = pool + .end_membership(self.member.hub(), &subject, false)? + .into_parts(); + result + } +} +pub(super) fn receipt(pool: &Arc, member: Arc) -> LoginReceipt { + LoginReceipt::new( + member.hub().clone(), + Arc::new(Owner { + pool: Arc::downgrade(pool), + member, + }), + ) +} diff --git a/crates/hub-io/src/connect/request.rs b/crates/hub-io/src/connect/request.rs new file mode 100644 index 00000000..fe03bdf6 --- /dev/null +++ b/crates/hub-io/src/connect/request.rs @@ -0,0 +1,163 @@ +//! Reconnect retains original device credentials before it is enqueued. +use super::open; +use crate::{ + classify::{credential_file, remote}, + convert, + credentials::{CredentialFile, CredentialLoginId, Device}, + pool::{Pool, observation::LoginObservation}, + task::spawn, +}; +use nocterm_hub_client::{ClientError, HubClient, StatusCode}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubEndpointSpec, HubError, HubFuture, HubId, HubInfo}; +use nocterm_keys::DeviceKey; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct Reconnect { + pool: Arc, + observed: Arc, + spec: HubEndpointSpec, + file: CredentialFile, + device: Option, + known: Option<(HubClient, Option)>, + file_subject: Option<(String, String)>, +} +impl Reconnect { + pub(crate) fn capture(pool: Arc, spec: HubEndpointSpec) -> Result { + let hub = HubId::from(spec.address.clone()); + let observed = LoginObservation::capture(pool.clone(), hub.clone())?; + let known = pool.connected(&hub, spec.trust); + let file = pool.file(&spec.address)?; + let mut device = match file.load().map_err(credential_file)? { + Some(device) => device, + None => Device { + key: DeviceKey::generate().map_err(remote)?, + session: None, + login: None, + }, + }; + if let Some(subject) = observed.original_subject()? { + let matches = device.session.as_ref().is_some_and(|session| { + session.account_id == subject.account() && session.device_id == subject.device() + }); + if !matches + || known + .as_ref() + .is_some_and(|(_, login)| *login != device.login) + { + device.wipe_session(); + return Err(HubError::Stale); + } + } + if let Err(error) = observed.check() { + device.wipe_session(); + return Err(error); + } + let file_subject = device + .session + .as_ref() + .map(|session| (session.account_id.clone(), session.device_id.clone())); + Ok(Self { + pool, + observed, + spec, + file, + device: Some(device), + known, + file_subject, + }) + } + pub(crate) fn start(self, handle: &Handle) -> HubFuture { + let observed = self.observed.clone(); + let returned = observed.clone(); + let task = spawn(handle, async move { observed.during(self.worker()).await? }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + async fn worker(mut self) -> Result { + if let Some((client, login)) = self.known.take() { + let now = self + .observed + .during(client.auth().get_hub_info(v2::Empty {})) + .await?; + self.observed.check()?; + if let Ok(now) = now { + let now = now.into_inner(); + if now.hub_id == client.info().hub_id + && now.protocol_version == client.info().protocol_version + { + return self.renewed(client, login).await; + } + } + } + let mut device = self.device.take().ok_or(HubError::Stale)?; + let login = device.login; + let store = device + .session + .as_ref() + .map(|session| self.file.clone().bound(session, login)); + let client = self + .observed + .during(open(&self.spec, device.key, device.session.take())) + .await??; + self.observed.check()?; + match store { + Some(store) => client.persist_with(Arc::new(store)), + None => return Ok(convert::hub_info(client.info(), false)), + } + self.renewed(client, login).await + } + async fn renewed( + &self, + client: HubClient, + login: Option, + ) -> Result { + match self.observed.during(client.ensure_fresh()).await? { + Ok(_) => { + self.observed.check()?; + let info = convert::hub_info(client.info(), true); + let next = self.pool.renew_observed( + self.observed.hub().clone(), + self.spec.trust, + client, + login, + &self.observed.snapshot(), + )?; + self.observed.published(next); + Ok(info) + } + Err(error) => { + let expired = matches!( + error, + ClientError::Status { + code: StatusCode::Unauthenticated, + .. + } + ); + let revoked = matches!(error, ClientError::Revoked); + if expired || revoked { + let subject = self + .file_subject + .as_ref() + .map(|(account, device)| (account.as_str(), device.as_str())); + self.observed.retire(&self.file, subject, login, revoked)?; + if expired { + return Ok(convert::hub_info(client.info(), false)); + } + } + Err(crate::classify::classify(&self.spec.address, error)) + } + } + } +} +impl Drop for Reconnect { + fn drop(&mut self) { + if let Some(device) = &mut self.device { + device.wipe_session(); + } + } +} diff --git a/crates/hub-io/src/connect/sign_out.rs b/crates/hub-io/src/connect/sign_out.rs new file mode 100644 index 00000000..c9c823a4 --- /dev/null +++ b/crates/hub-io/src/connect/sign_out.rs @@ -0,0 +1,114 @@ +//! Sign-out retains the original native member or original offline private-file origin. +use crate::{ + classify::credential_file, + credentials::{CredentialFile, CredentialOrigin}, + pool::{Pool, observation::LoginObservation}, + task::spawn, +}; +use nocterm_hub_client::HubClient; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, HubFuture, HubId}; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct SignOut { + observed: Arc, + target: Target, +} +enum Target { + Member { + pool: Arc, + client: HubClient, + subject: Arc, + }, + Offline { + file: CredentialFile, + origin: Option, + }, +} +impl SignOut { + pub(crate) fn capture(pool: Arc, hub: HubId) -> Result { + let observed = LoginObservation::capture(pool.clone(), hub.clone())?; + let target = if let Some(subject) = observed.original_subject()? { + let client = pool.captured(&hub, &subject)?; + Target::Member { + pool, + client, + subject, + } + } else { + let file = pool.file(hub.address())?; + let origin = file.load().map_err(credential_file)?.map(|mut device| { + let origin = device.origin(); + device.wipe_session(); + origin + }); + Target::Offline { file, origin } + }; + observed.check()?; + Ok(Self { observed, target }) + } + pub(crate) fn start(self, handle: &Handle) -> HubFuture<()> { + let observed = self.observed.clone(); + let returned = observed.clone(); + let task = spawn(handle, async move { observed.during(self.worker()).await? }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + async fn worker(self) -> Result<(), HubError> { + match self.target { + Target::Member { + pool, + client, + subject, + } => { + let (next, result) = pool + .end_membership(self.observed.hub(), &subject, false)? + .into_parts(); + self.observed.published(next); + result?; + // Signing out revokes this device on the hub. + if let Err(error) = self + .observed + .during(client.account().logout(v2::Empty {})) + .await? + { + tracing::debug!(%error,"the hub did not end the original device session"); + } + Ok(()) + } + Target::Offline { file, origin } => { + let expected = self.observed.snapshot(); + let mut cleanup = None; + self.observed.with_current(|| { + let retire = || { + cleanup = Some( + match self.observed.slot().end_observed(expected.receipt(), true) { + Ok(ended) => { + let (receipt, result) = ended.into_parts(); + self.observed.ended_absence(receipt); + result + } + Err(error) => Err(error), + }, + ); + }; + let retired = match origin { + Some(ref origin) => file.retire_origin(origin, retire), + None => file.while_absent(retire), + } + .map_err(credential_file)?; + if !retired { + return Err(HubError::Stale); + } + cleanup.take().ok_or(HubError::Stale)??; + Ok(()) + })?; + Ok(()) + } + } + } +} diff --git a/crates/hub-io/src/connect/start.rs b/crates/hub-io/src/connect/start.rs new file mode 100644 index 00000000..ea67ae8d --- /dev/null +++ b/crates/hub-io/src/connect/start.rs @@ -0,0 +1,110 @@ +//! An explicit login owns its original native selection and private device file. +use super::{Attempt, open}; +use crate::{ + classify::{credential_file, remote}, + credentials::{CredentialFile, CredentialOrigin, Device}, + pool::{Pool, observation::LoginObservation}, + task::spawn, +}; +use nocterm_hubs_model::{HubEndpointSpec, HubError, HubFuture, HubId}; +use nocterm_keys::DeviceKey; +use std::sync::Arc; +use tokio::runtime::Handle; + +pub(crate) struct LoginStart { + spec: HubEndpointSpec, + observed: Arc, + file: CredentialFile, + device: Option, + origin: CredentialOrigin, + created: bool, + handed_off: bool, +} +impl LoginStart { + pub(crate) fn capture(pool: Arc, spec: HubEndpointSpec) -> Result { + let observed = LoginObservation::capture(pool.clone(), HubId::from(spec.address.clone()))?; + let file = pool.file(&spec.address)?; + let (mut device, created) = match file.load().map_err(credential_file)? { + Some(device) => (device, false), + None => ( + Device { + key: DeviceKey::generate().map_err(remote)?, + session: None, + login: None, + }, + true, + ), + }; + if let Err(error) = observed.check() { + device.wipe_session(); + return Err(error); + } + let origin = device.origin(); + Ok(Self { + spec, + observed, + file, + device: Some(device), + origin, + created, + handed_off: false, + }) + } + pub(crate) fn observation(&self) -> &Arc { + &self.observed + } + pub(crate) fn start( + self, + handle: &Handle, + work: impl FnOnce(Self) -> F + Send + 'static, + ) -> HubFuture + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let observed = self.observed.clone(); + let returned = observed.clone(); + let task = spawn(handle, async move { observed.during(work(self)).await? }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + returned.check()?; + result + }) + } + pub(super) async fn open(mut self) -> Result { + let device = self.device.as_ref().ok_or(HubError::Stale)?; + self.observed.with_current(|| { + self.file + .prepare_login(device, self.created) + .map_err(credential_file) + })?; + let mut device = self.device.take().ok_or(HubError::Stale)?; + let client = self + .observed + .during(open(&self.spec, device.key, device.session.take())) + .await??; + self.observed.check()?; + let attempt = Attempt { + hub: self.observed.hub().clone(), + file: self.file.clone(), + client, + trust: self.spec.trust, + created: self.created, + finished: false, + origin: self.origin.clone(), + observed: self.observed.clone(), + }; + self.handed_off = true; + Ok(attempt) + } +} +impl Drop for LoginStart { + fn drop(&mut self) { + if let Some(device) = &mut self.device { + device.wipe_session(); + } + if self.created && !self.handed_off { + let _ = self.file.erase_unpublished(self.origin.public_key()); + } + } +} diff --git a/crates/hub-io/src/connect/tests.rs b/crates/hub-io/src/connect/tests.rs new file mode 100644 index 00000000..f081837b --- /dev/null +++ b/crates/hub-io/src/connect/tests.rs @@ -0,0 +1,102 @@ +//! Native deferred admission uses the original private-file generation. +use super::*; +use crate::{account::Accounts, credentials::CredentialDir}; +use nocterm_hub_client::Session; +use nocterm_hubs_model::{Fingerprint, HubAddress}; + +struct Device { + _dir: tempfile::TempDir, + file: CredentialFile, + connector: Connector, + spec: HubEndpointSpec, +} +impl Device { + fn new() -> Self { + let dir = tempfile::tempdir().unwrap(); + let address = HubAddress::parse("127.0.0.1:1").unwrap(); + let accounts = Arc::new(Accounts::new(dir.path().to_owned())); + let pool = Arc::new(Pool::new(dir.path().to_owned(), "test", accounts.clone())); + let keys = KeyFlow::new(pool.clone(), accounts, tokio::runtime::Handle::current()); + let file = CredentialDir::new(dir.path().to_owned()) + .file(&address) + .unwrap(); + Self { + _dir: dir, + file, + connector: Connector::new(pool, keys), + spec: HubEndpointSpec { + address, + trust: TrustPolicy::Pinned(Fingerprint::from_bytes([7; 32])), + }, + } + } + fn login(&self, name: &str) { + self.file.load_or_create().unwrap(); + self.file + .save_session(&Session { + account_id: name.into(), + device_id: "device".into(), + access_token: format!("access-{name}"), + refresh_token: format!("refresh-{name}"), + access_expires_at_ms: 10, + refresh_expires_at_ms: 20, + }) + .unwrap(); + } + fn account(&self) -> String { + self.file + .load() + .unwrap() + .unwrap() + .session + .unwrap() + .account_id + } +} +#[tokio::test] +async fn a_login_queued_without_credentials_never_adopts_a_newer_private_file() { + let device = Device::new(); + let start = device.connector.begin(device.spec.clone()).unwrap(); + device.login("B"); + assert!(matches!( + device + .connector + .password(start, "A".into(), Secret::new("password")) + .await, + Err(HubError::Stale) + )); + assert_eq!(device.account(), "B"); +} +#[tokio::test] +async fn a_login_queued_with_a_session_cannot_replace_another_login_of_the_same_account() { + let device = Device::new(); + device.login("A"); + let start = device.connector.begin(device.spec.clone()).unwrap(); + let original = device.file.load().unwrap().unwrap().login; + device.login("A"); + let current = device.file.load().unwrap().unwrap().login; + assert_ne!(original, current); + assert!(matches!( + device + .connector + .password(start, "A".into(), Secret::new("password")) + .await, + Err(HubError::Stale) + )); + assert_eq!(device.file.load().unwrap().unwrap().login, current); +} +#[tokio::test] +async fn an_offline_sign_out_captured_before_a_new_login_preserves_that_login() { + let device = Device::new(); + device.login("A"); + let sign_out = device + .connector + .sign_out(&HubId::from(device.spec.address.clone())) + .unwrap(); + device.login("B"); + assert!(matches!( + sign_out.start(&tokio::runtime::Handle::current()).await, + Err(HubError::Stale) + )); + assert_eq!(device.account(), "B"); +} diff --git a/crates/hub-io/src/convert.rs b/crates/hub-io/src/convert.rs new file mode 100644 index 00000000..50db23a6 --- /dev/null +++ b/crates/hub-io/src/convert.rs @@ -0,0 +1,33 @@ +//! Records of the hub protocol as the records of the model. + +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{Account, AccountId, HubInfo, OidcProvider}; + +pub(crate) fn hub_info(info: &v2::HubInfo, signed_in: bool) -> HubInfo { + HubInfo { + name: info.name.clone(), + password_login: info.password_login, + oidc_providers: info + .oidc_providers + .iter() + .map(|provider| OidcProvider { + id: provider.id.clone(), + display_name: provider.display_name.clone(), + }) + .collect(), + bootstrapped: !info.needs_setup, + signed_in, + } +} + +pub(crate) fn account(me: v2::Me) -> Account { + Account { + id: AccountId::new(me.account_id), + username: me.username, + display_name: me.display_name, + admin: me.admin, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/convert/tests.rs b/crates/hub-io/src/convert/tests.rs new file mode 100644 index 00000000..d4b3510a --- /dev/null +++ b/crates/hub-io/src/convert/tests.rs @@ -0,0 +1,34 @@ +use super::*; + +#[test] +fn hub_info_keeps_what_a_sign_in_screen_needs() { + let wire = v2::HubInfo { + name: "Team hub".into(), + needs_setup: false, + password_login: true, + oidc_providers: vec![v2::OidcProvider { + id: "corp".into(), + display_name: "Corporate".into(), + }], + ..Default::default() + }; + let info = hub_info(&wire, true); + assert_eq!(info.name, "Team hub"); + assert!(info.password_login && info.bootstrapped && info.signed_in); + assert_eq!(info.oidc_providers[0].id, "corp"); + assert!(!hub_info(&wire, false).signed_in); +} + +#[test] +fn an_account_says_whether_it_administers_the_hub() { + let me = v2::Me { + account_id: "a1".into(), + username: "ada".into(), + display_name: "Ada".into(), + admin: true, + ..Default::default() + }; + let account = account(me); + assert_eq!(account.id.as_str(), "a1"); + assert!(account.admin); +} diff --git a/crates/hub-io/src/credentials.rs b/crates/hub-io/src/credentials.rs new file mode 100644 index 00000000..c30f3cf4 --- /dev/null +++ b/crates/hub-io/src/credentials.rs @@ -0,0 +1,485 @@ +//! The private credential file of this device for each hub. +//! +//! One file per hub, `_.toml` in a mode-0700 directory, holds the device key seed +//! and the tokens. A sibling `.lock` file serializes token rotation between processes. + +use std::any::Any; +use std::fs::{self, File, OpenOptions}; +use std::io; +use std::path::{Path, PathBuf}; + +use nocterm_core::fs::{ + BoundedReadError, create_private, read_regular_bounded, write_private_atomic, +}; +use nocterm_hub_client::{Session, SessionStore}; +use nocterm_hubs_model::HubAddress; +use nocterm_keys::DeviceKey; +use serde::{Deserialize, Serialize}; +use tokio::runtime::{Handle, RuntimeFlavor}; + +mod bound; +mod publication; +mod seed; +use bound::BoundSessionStore; +pub(crate) use publication::{CredentialOrigin, LoginPublication}; + +/// A credential file is a few hundred bytes; anything larger is not ours. +const MAX_FILE_BYTES: usize = 16 * 1024; +/// Keeps `.toml.lock` below the usual 255-byte file name limit. +pub(crate) const MAX_NAME_BYTES: usize = 200; + +/// Why the credentials of a hub could not be read or kept. +#[derive(Debug, thiserror::Error)] +pub(crate) enum CredentialError { + #[error("the credential file cannot be read")] + Read(#[from] BoundedReadError), + #[error("the credential file cannot be written")] + Write(#[from] nocterm_core::fs::FileError), + #[error("the credential directory or lock cannot be prepared")] + Prepare(#[source] io::Error), + #[error("the credential file is not valid")] + Format(#[from] toml::de::Error), + #[error("the credential file cannot be encoded")] + Encode(#[from] toml::ser::Error), + #[error("the credential file holds no valid device key")] + Seed(#[from] seed::SeedError), + #[error("the credential file belongs to another hub")] + OtherHub, + #[error("the local login identity cannot be generated")] + LoginIdentity(#[source] nocterm_keys::KeyError), + #[error("the original local login file was replaced")] + StaleLogin, + #[error("the hub address is too long for a file name")] + NameTooLong, +} + +/// Where every hub's credential file lives. +#[derive(Clone, Debug)] +pub(crate) struct CredentialDir { + root: PathBuf, +} + +impl CredentialDir { + pub(crate) fn new(root: PathBuf) -> Self { + Self { root } + } + + pub(crate) fn file(&self, address: &HubAddress) -> Result { + let name = file_stem(address); + if name.len() > MAX_NAME_BYTES { + return Err(CredentialError::NameTooLong); + } + Ok(CredentialFile { + path: self.root.join(format!("{name}.toml")), + lock_path: self.root.join(format!("{name}.toml.lock")), + address: address.clone(), + }) + } +} + +/// `host_port`; `:` becomes `_`, which no host name contains, so names are unique. +pub(crate) fn file_stem(address: &HubAddress) -> String { + // Also names the account cache beside the credential file. + format!("{}_{}", address.host(), address.port()).replace(':', "_") +} + +/// Identifies one explicit native login, independently of account/device reuse. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub(crate) struct CredentialLoginId([u8; 16]); + +/// What the file holds, as stored. +#[derive(Serialize, Deserialize)] +struct Stored { + address: String, + seed: String, + #[serde(default, skip_serializing_if = "Option::is_none")] + login: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + session: Option, +} + +/// The device key and tokens read from a credential file. +pub(crate) struct Device { + pub(crate) key: DeviceKey, + pub(crate) session: Option, + pub(crate) login: Option, +} +impl Device { + pub(crate) fn origin(&self) -> CredentialOrigin { + CredentialOrigin::of(self) + } + pub(crate) fn wipe_session(&mut self) { + use zeroize::Zeroize as _; + if let Some(session) = &mut self.session { + session.access_token.zeroize(); + session.refresh_token.zeroize(); + } + } +} + +/// The credential file of one hub. Also the [`SessionStore`] its client rotates tokens in. +#[derive(Clone, Debug)] +pub(crate) struct CredentialFile { + path: PathBuf, + lock_path: PathBuf, + address: HubAddress, +} + +impl CredentialFile { + /// Pins token adoption and refresh publication to this original login subject. + pub(crate) fn bound( + self, + session: &Session, + login: Option, + ) -> BoundSessionStore { + BoundSessionStore::new(self, session, login) + } + + /// The stored device; `None` when nothing was ever kept for the hub. + pub(crate) fn load(&self) -> Result, CredentialError> { + let bytes = match read_regular_bounded(&self.path, MAX_FILE_BYTES) { + Ok(bytes) => bytes, + Err(error) if error.io_kind() == Some(io::ErrorKind::NotFound) => return Ok(None), + Err(error) => return Err(error.into()), + }; + let stored = self.parse(&bytes)?; + Ok(Some(Device { + key: DeviceKey::from_seed(&seed::decode(&stored.seed)?), + session: stored.session, + login: stored.login, + })) + } + + fn parse(&self, bytes: &[u8]) -> Result { + let text = String::from_utf8_lossy(bytes); + let stored: Stored = toml::from_str(&text)?; + if stored.address != self.address.to_string() { + return Err(CredentialError::OtherHub); + } + Ok(stored) + } + + /// The stored device, or a new key kept without tokens. The flag says it was created. + #[cfg(test)] + pub(crate) fn load_or_create(&self) -> Result<(Device, bool), CredentialError> { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + if let Some(device) = self.load()? { + return Ok((device, false)); + } + let key = DeviceKey::generate() + .map_err(|error| CredentialError::Prepare(io::Error::other(error)))?; + self.write(&key, None, None)?; + Ok(( + Device { + key, + session: None, + login: None, + }, + true, + )) + } + + /// Publishes a new explicit login, even when the subject is unchanged. + #[cfg(test)] + pub(crate) fn save_session( + &self, + session: &Session, + ) -> Result { + self.save_session_inner(session, None) + } + + fn save_session_expected( + &self, + session: &Session, + origin: &CredentialOrigin, + ) -> Result { + self.save_session_inner(session, Some(origin)) + } + + fn save_session_inner( + &self, + session: &Session, + origin: Option<&CredentialOrigin>, + ) -> Result { + let login = CredentialLoginId( + nocterm_keys::random_bytes().map_err(CredentialError::LoginIdentity)?, + ); + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let mut device = self + .load()? + .ok_or_else(|| CredentialError::Prepare(io::Error::from(io::ErrorKind::NotFound)))?; + let current = origin.is_none_or(|origin| origin.matches(&device)); + device.wipe_session(); + if !current { + return Err(CredentialError::StaleLogin); + } + self.write(&device.key, Some(session), Some(login))?; + Ok(login) + } + + /// Establishes only the captured original device file before native login I/O. + pub(crate) fn prepare_login( + &self, + device: &Device, + absent: bool, + ) -> Result<(), CredentialError> { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + match self.load()? { + None if absent => self.write(&device.key, None, None), + Some(mut current) if !absent => { + let matches = device.origin().matches(¤t); + current.wipe_session(); + if matches { + Ok(()) + } else { + Err(CredentialError::StaleLogin) + } + } + Some(mut current) => { + current.wipe_session(); + Err(CredentialError::StaleLogin) + } + None => Err(CredentialError::StaleLogin), + } + } + + /// Forgets the tokens but keeps the key, so the device signs in again as itself. + #[cfg(test)] + pub(crate) fn clear_session(&self) -> Result<(), CredentialError> { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + self.replace_session(None) + } + + /// Replaces the tokens for a caller that holds the lock already: the client of the + /// contract does while it rotates. A second lock would wait for itself. + fn save_locked(&self, session: &Session) -> Result<(), CredentialError> { + self.replace_session(Some(session)) + } + + fn replace_session(&self, session: Option<&Session>) -> Result<(), CredentialError> { + let device = self + .load()? + .ok_or_else(|| CredentialError::Prepare(io::Error::from(io::ErrorKind::NotFound)))?; + self.write(&device.key, session, device.login) + } + + fn write( + &self, + key: &DeviceKey, + session: Option<&Session>, + login: Option, + ) -> Result<(), CredentialError> { + self.prepare_directory().map_err(CredentialError::Prepare)?; + let stored = Stored { + address: self.address.to_string(), + seed: seed::encode(&key.seed()), + session: session.cloned(), + login, + }; + let text = toml::to_string(&stored)?; + write_private_atomic(&self.path, text.as_bytes())?; + Ok(()) + } + + /// Checks a captured login while excluding token/file replacement. + pub(crate) fn matches_login( + &self, + account: &str, + device: &str, + login: Option, + ) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + self.matches_login_locked(account, device, login) + } + + fn matches_login_locked( + &self, + account: &str, + device: &str, + login: Option, + ) -> Result { + use zeroize::Zeroize as _; + Ok(self.load()?.is_some_and(|stored| { + stored.session.is_some_and(|mut session| { + let matches = stored.login == login + && session.account_id == account + && session.device_id == device; + session.access_token.zeroize(); + session.refresh_token.zeroize(); + matches + }) + })) + } + + /// Retires only the original local login under the shared file lock. + #[cfg(test)] + pub(crate) fn erase_if( + &self, + account: &str, + device: &str, + login: Option, + ) -> Result<(), CredentialError> { + self.retire_if(account, device, login, true, || {}) + .map(|_| ()) + } + + /// The existing file-generation check also fences original cache retirement. + /// Callers hold Pool admission; the action must not reacquire this file lock. + pub(crate) fn retire_if( + &self, + account: &str, + device: &str, + login: Option, + erase: bool, + retire: impl FnOnce(), + ) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let matches = self.matches_login_locked(account, device, login)?; + if matches { + retire(); + if erase { + self.remove_locked().map_err(CredentialError::Prepare)?; + } else { + self.replace_session(None)?; + } + } + Ok(matches) + } + + /// A failed first sign-in owns only its still unpublished original device file. + pub(crate) fn erase_unpublished( + &self, + expected_public_key: &[u8; 32], + ) -> Result<(), CredentialError> { + use zeroize::Zeroize as _; + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let Some(mut stored) = self.load()? else { + return Ok(()); + }; + let unpublished = stored.login.is_none() + && stored.session.is_none() + && stored.key.public_key() == *expected_public_key; + if let Some(session) = &mut stored.session { + session.access_token.zeroize(); + session.refresh_token.zeroize(); + } + if unpublished { + self.remove_locked().map_err(CredentialError::Prepare)?; + } + Ok(()) + } + + pub(crate) fn retire_origin( + &self, + origin: &CredentialOrigin, + retire: impl FnOnce(), + ) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + let Some(mut current) = self.load()? else { + return Ok(false); + }; + let matches = origin.matches(¤t); + current.wipe_session(); + if matches { + retire(); + self.remove_locked().map_err(CredentialError::Prepare)?; + } + Ok(matches) + } + + pub(crate) fn while_absent(&self, retire: impl FnOnce()) -> Result { + let _lock = self.lock_exclusive().map_err(CredentialError::Prepare)?; + if let Some(mut current) = self.load()? { + current.wipe_session(); + return Ok(false); + } + retire(); + Ok(true) + } + + /// Removes the credentials of the hub; no file is not an error. + pub(crate) fn erase(&self) -> io::Result<()> { + let _lock = self.lock_exclusive()?; + self.remove_locked() + } + + fn remove_locked(&self) -> io::Result<()> { + match fs::remove_file(&self.path) { + Err(error) if error.kind() != io::ErrorKind::NotFound => Err(error), + _ => Ok(()), + } + } + + fn prepare_directory(&self) -> io::Result<()> { + match self.path.parent() { + Some(parent) => prepare_private_directory(parent), + None => Ok(()), + } + } + + /// Opens the lock file, creating it private, and waits for the exclusive lock. Waiting + /// does not hold up the worker of the runtime it runs on. + fn lock_exclusive(&self) -> io::Result { + self.prepare_directory()?; + let file = match create_private(&self.lock_path) { + Ok(file) => file, + Err(error) if error.kind() == io::ErrorKind::AlreadyExists => { + open_existing(&self.lock_path)? + } + Err(error) => return Err(error.into_io_error()), + }; + off_worker(|| file.lock())?; + Ok(file) + } +} + +/// Creates `directory` and its parents, private to the user where the platform has modes. +pub(crate) fn prepare_private_directory(directory: &Path) -> io::Result<()> { + let mut builder = fs::DirBuilder::new(); + builder.recursive(true); + #[cfg(unix)] + { + use std::os::unix::fs::DirBuilderExt as _; + builder.mode(0o700); + } + builder.create(directory) +} + +/// Runs a call that waits for another holder of the lock. On a multi-thread runtime the +/// worker hands its other tasks over first, so the holder, which may be one of them awaiting +/// the network, keeps being polled and releases the lock. +fn off_worker(wait: impl FnOnce() -> T) -> T { + match Handle::try_current() { + Ok(handle) if handle.runtime_flavor() == RuntimeFlavor::MultiThread => { + tokio::task::block_in_place(wait) + } + _ => wait(), + } +} + +fn open_existing(path: &Path) -> io::Result { + OpenOptions::new().write(true).open(path) +} + +impl SessionStore for CredentialFile { + fn lock(&self) -> io::Result> { + Ok(Box::new(self.lock_exclusive()?)) + } + + fn load(&self) -> io::Result> { + Ok(CredentialFile::load(self) + .map_err(into_io)? + .and_then(|device| device.session)) + } + + fn save(&self, session: &Session) -> io::Result<()> { + self.save_locked(session).map_err(into_io) + } +} + +fn into_io(error: CredentialError) -> io::Error { + io::Error::other(error) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/credentials/bound.rs b/crates/hub-io/src/credentials/bound.rs new file mode 100644 index 00000000..ca7aa95f --- /dev/null +++ b/crates/hub-io/src/credentials/bound.rs @@ -0,0 +1,88 @@ +//! A client's token store can refresh its original account/device only. +use std::{any::Any, io}; + +use nocterm_hub_client::{Session, SessionStore}; +use zeroize::Zeroize as _; + +use super::{CredentialFile, CredentialLoginId}; + +#[derive(Debug)] +pub(crate) struct BoundSessionStore { + file: CredentialFile, + account: String, + device: String, + login: Option, +} + +#[derive(Debug, thiserror::Error)] +#[error("the stored login no longer belongs to this client")] +struct ReplacedLogin; + +impl BoundSessionStore { + pub(super) fn new( + file: CredentialFile, + session: &Session, + login: Option, + ) -> Self { + Self { + file, + account: session.account_id.clone(), + device: session.device_id.clone(), + login, + } + } + + fn matches(&self, session: &Session) -> bool { + session.account_id == self.account && session.device_id == self.device + } + + fn current(&self) -> io::Result { + let current = self.file.load().map_err(super::into_io)?; + let same_login = current + .as_ref() + .is_some_and(|stored| stored.login == self.login); + let Some(mut session) = current.and_then(|stored| stored.session) else { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + ReplacedLogin, + )); + }; + if !same_login || !self.matches(&session) { + session.access_token.zeroize(); + session.refresh_token.zeroize(); + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + ReplacedLogin, + )); + } + Ok(session) + } +} + +impl SessionStore for BoundSessionStore { + fn lock(&self) -> io::Result> { + SessionStore::lock(&self.file) + } + + fn load(&self) -> io::Result> { + self.current().map(Some) + } + + fn save(&self, session: &Session) -> io::Result<()> { + if !self.matches(session) { + return Err(io::Error::new( + io::ErrorKind::PermissionDenied, + ReplacedLogin, + )); + } + // SDK owns the file lock through load, remote refresh and save. A new + // login uses that same lock, so this check cannot race replacement. + let mut current = self.current()?; + current.access_token.zeroize(); + current.refresh_token.zeroize(); + SessionStore::save(&self.file, session) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/credentials/bound/tests.rs b/crates/hub-io/src/credentials/bound/tests.rs new file mode 100644 index 00000000..6a31f4ea --- /dev/null +++ b/crates/hub-io/src/credentials/bound/tests.rs @@ -0,0 +1,201 @@ +use super::*; +use crate::credentials::CredentialDir; + +fn session(account: &str, token: &str) -> Session { + Session { + account_id: account.into(), + device_id: "device".into(), + access_token: token.into(), + refresh_token: format!("refresh-{token}"), + access_expires_at_ms: i64::MAX, + refresh_expires_at_ms: i64::MAX, + } +} +fn file(dir: &tempfile::TempDir) -> CredentialFile { + CredentialDir::new(dir.path().to_path_buf()) + .file(&"hub.test".parse().unwrap()) + .unwrap() +} +fn signed_in(dir: &tempfile::TempDir, session: &Session) -> BoundSessionStore { + let file = file(dir); + file.load_or_create().unwrap(); + let login = file.save_session(session).unwrap(); + file.bound(session, Some(login)) +} + +#[test] +fn a_bound_store_adopts_only_a_refresh_of_the_original_subject() { + let dir = tempfile::tempdir().unwrap(); + let store = signed_in(&dir, &session("A", "initial")); + let refreshed = session("A", "new"); + { + let current = file(&dir); + let _lock = current.lock().unwrap(); + SessionStore::save(¤t, &refreshed).unwrap(); + } + let _lock = store.lock().unwrap(); + assert_eq!(store.load().unwrap(), Some(refreshed.clone())); + store.save(&session("A", "next")).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("A", "next")) + ); +} + +#[test] +fn an_old_a_store_cannot_adopt_b_and_a_new_b_store_can_refresh() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + let current = signed_in(&dir, &session("B", "login")); + { + let _lock = old.lock().unwrap(); + let error = old.load().unwrap_err(); + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied); + assert!(error.get_ref().unwrap().is::()); + } + let _lock = current.lock().unwrap(); + assert_eq!(current.load().unwrap(), Some(session("B", "login"))); + current.save(&session("B", "refreshed")).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("B", "refreshed")) + ); +} + +#[test] +fn a_late_a_refresh_cannot_overwrite_the_file_of_b() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + file(&dir).save_session(&session("B", "login")).unwrap(); + let _lock = old.lock().unwrap(); + assert_eq!( + old.save(&session("A", "late")).unwrap_err().kind(), + io::ErrorKind::PermissionDenied + ); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("B", "login")) + ); +} + +#[test] +fn a_cleared_login_cannot_implicitly_refresh_old_tokens() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + file(&dir).clear_session().unwrap(); + let _lock = old.lock().unwrap(); + assert!(old.load().is_err()); + assert!(old.save(&session("A", "late")).is_err()); + assert!(SessionStore::load(&file(&dir)).unwrap().is_none()); +} + +#[test] +fn a_changed_device_is_a_replacement_even_for_the_same_account() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "initial")); + let mut next = session("A", "login"); + next.device_id = "new-device".into(); + file(&dir).save_session(&next).unwrap(); + let _lock = old.lock().unwrap(); + assert!(old.load().is_err()); + assert!(old.save(&session("A", "late")).is_err()); + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next)); +} + +#[test] +fn captured_revocation_erases_only_the_matching_file_subject() { + let dir = tempfile::tempdir().unwrap(); + let _old = signed_in(&dir, &session("A", "initial")); + file(&dir).save_session(&session("B", "login")).unwrap(); + file(&dir).erase_if("A", "device", _old.login).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("B", "login")) + ); + file(&dir) + .erase_if("B", "device", file(&dir).load().unwrap().unwrap().login) + .unwrap(); + assert!(file(&dir).load().unwrap().is_none()); +} + +#[test] +fn a_same_subject_new_login_rejects_old_adoption_refresh_and_revocation_before_pool_publication() { + let dir = tempfile::tempdir().unwrap(); + let old = signed_in(&dir, &session("A", "old-login")); + // This is the exact formerly vulnerable window: the new file is durable, + // while a retained old membership can still deliver its delayed failure. + let next = session("A", "new-login"); + let new_login = file(&dir).save_session(&next).unwrap(); + assert_ne!(old.login, Some(new_login)); + assert!(!file(&dir).matches_login("A", "device", old.login).unwrap()); + assert!( + file(&dir) + .matches_login("A", "device", Some(new_login)) + .unwrap() + ); + file(&dir).erase_if("A", "device", old.login).unwrap(); + { + let _lock = old.lock().unwrap(); + assert_eq!( + old.load().unwrap_err().kind(), + io::ErrorKind::PermissionDenied + ); + assert_eq!( + old.save(&session("A", "late-refresh")).unwrap_err().kind(), + io::ErrorKind::PermissionDenied + ); + } + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next.clone())); + let current = file(&dir).bound(&next, Some(new_login)); + let _lock = current.lock().unwrap(); + current.save(&session("A", "current-refresh")).unwrap(); + assert_eq!( + SessionStore::load(&file(&dir)).unwrap(), + Some(session("A", "current-refresh")) + ); + assert_eq!(file(&dir).load().unwrap().unwrap().login, Some(new_login)); +} + +#[test] +fn a_legacy_login_cannot_retire_or_adopt_a_new_explicit_login() { + let dir = tempfile::tempdir().unwrap(); + let original = session("A", "legacy"); + file(&dir).load_or_create().unwrap(); + SessionStore::save(&file(&dir), &original).unwrap(); + let old = file(&dir).bound(&original, None); + let next = session("A", "new-login"); + file(&dir).save_session(&next).unwrap(); + file(&dir).erase_if("A", "device", None).unwrap(); + let _lock = old.lock().unwrap(); + assert!(old.load().is_err()); + assert!(old.save(&session("A", "late")).is_err()); + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next)); +} + +#[test] +fn late_original_revocation_preserves_new_same_account_file_and_open_slot() { + use crate::account::{ + Accounts, + testing::{ACCOUNT_ID, hub, sign_in}, + }; + use std::sync::Arc; + let dir = tempfile::tempdir().unwrap(); + let accounts = Accounts::new(dir.path().to_path_buf()); + let (slot, _) = sign_in(&accounts); + let original_keys = slot.open().unwrap(); + let old = signed_in(&dir, &session(ACCOUNT_ID, "original")); + // Publish the replacement credentials before the replacement membership. + let next = session(ACCOUNT_ID, "new-login"); + file(&dir).save_session(&next).unwrap(); + let (new_slot, _) = sign_in(&accounts); + let current_keys = new_slot.open().unwrap(); + file(&dir) + .erase_if(ACCOUNT_ID, "device", old.login) + .unwrap(); + slot.erase_if(&original_keys).unwrap(); + assert_eq!(SessionStore::load(&file(&dir)).unwrap(), Some(next)); + assert!(Arc::ptr_eq( + &accounts.slot(&hub()).unwrap().open().unwrap(), + ¤t_keys + )); +} diff --git a/crates/hub-io/src/credentials/publication.rs b/crates/hub-io/src/credentials/publication.rs new file mode 100644 index 00000000..2a822690 --- /dev/null +++ b/crates/hub-io/src/credentials/publication.rs @@ -0,0 +1,66 @@ +//! A captured private-file origin fences a delayed explicit login publication. +use super::{CredentialError, CredentialFile, CredentialLoginId, Device}; +use nocterm_hub_client::{HubClient, Session}; +use std::sync::Arc; + +#[derive(Clone)] +pub(crate) struct CredentialOrigin { + public_key: [u8; 32], + login: Option, + subject: Option<(String, String)>, +} +impl CredentialOrigin { + pub(super) fn of(device: &Device) -> Self { + Self { + public_key: device.key.public_key(), + login: device.login, + subject: device + .session + .as_ref() + .map(|session| (session.account_id.clone(), session.device_id.clone())), + } + } + pub(super) fn matches(&self, device: &Device) -> bool { + self.public_key == device.key.public_key() + && self.login == device.login + && self + .subject + .as_ref() + .map(|(account, device)| (account.as_str(), device.as_str())) + == device + .session + .as_ref() + .map(|session| (session.account_id.as_str(), session.device_id.as_str())) + } + pub(crate) fn public_key(&self) -> &[u8; 32] { + &self.public_key + } +} + +pub(crate) struct LoginPublication<'a> { + file: &'a CredentialFile, + session: &'a Session, + origin: &'a CredentialOrigin, +} +impl<'a> LoginPublication<'a> { + pub(crate) fn new( + file: &'a CredentialFile, + session: &'a Session, + origin: &'a CredentialOrigin, + ) -> Self { + Self { + file, + session, + origin, + } + } + pub(crate) fn session(&self) -> &Session { + self.session + } + pub(crate) fn save(&self) -> Result { + self.file.save_session_expected(self.session, self.origin) + } + pub(crate) fn bind(&self, client: &HubClient, login: CredentialLoginId) { + client.persist_with(Arc::new(self.file.clone().bound(self.session, Some(login)))); + } +} diff --git a/crates/hub-io/src/credentials/seed.rs b/crates/hub-io/src/credentials/seed.rs new file mode 100644 index 00000000..4af938ff --- /dev/null +++ b/crates/hub-io/src/credentials/seed.rs @@ -0,0 +1,46 @@ +//! The device key seed as hexadecimal text. + +/// The seed is not 64 hexadecimal digits. +#[derive(Debug, thiserror::Error)] +#[error("the key seed is not 64 hexadecimal digits")] +pub(crate) struct SeedError; + +pub(crate) fn encode(seed: &[u8; 32]) -> String { + hex::encode(seed) +} + +pub(crate) fn decode(text: &str) -> Result<[u8; 32], SeedError> { + let mut seed = [0_u8; 32]; + hex::decode_to_slice(text, &mut seed).map_err(|_| SeedError)?; + Ok(seed) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_seed_round_trips_through_hex() { + let mut seed = [0_u8; 32]; + for (index, byte) in seed.iter_mut().enumerate() { + *byte = u8::try_from(index * 8).unwrap(); + } + assert_eq!(decode(&encode(&seed)).unwrap(), seed); + } + + #[test] + fn text_that_is_not_a_seed_is_refused() { + assert!(decode("").is_err()); + assert!(decode(&"a".repeat(63)).is_err()); + assert!(decode(&"g".repeat(64)).is_err()); + assert!(decode(&"é".repeat(32)).is_err()); + assert!(decode(&"+f".repeat(32)).is_err()); + } + + #[test] + fn mixed_case_seeds_keep_the_same_bytes_and_encode_canonically() { + let seed = decode(&"aF".repeat(32)).unwrap(); + assert_eq!(seed, [175; 32]); + assert_eq!(encode(&seed), "af".repeat(32)); + } +} diff --git a/crates/hub-io/src/credentials/tests.rs b/crates/hub-io/src/credentials/tests.rs new file mode 100644 index 00000000..5341189f --- /dev/null +++ b/crates/hub-io/src/credentials/tests.rs @@ -0,0 +1,186 @@ +use std::sync::mpsc; +use std::thread; +use std::time::Duration; + +use super::*; + +fn address(text: &str) -> HubAddress { + HubAddress::parse(text).unwrap() +} + +fn session(token: &str) -> Session { + Session { + account_id: "account".into(), + device_id: "device".into(), + access_token: token.into(), + access_expires_at_ms: 10, + refresh_token: format!("refresh-{token}"), + refresh_expires_at_ms: 20, + } +} + +fn file_in(dir: &tempfile::TempDir, text: &str) -> CredentialFile { + CredentialDir::new(dir.path().join("hubs")) + .file(&address(text)) + .unwrap() +} + +#[test] +fn a_hub_without_a_file_has_no_device() { + let dir = tempfile::tempdir().unwrap(); + assert!(file_in(&dir, "hub.example").load().unwrap().is_none()); +} + +#[test] +fn a_new_device_key_is_kept_and_found_again() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + let (created, was_new) = file.load_or_create().unwrap(); + assert!(was_new); + assert!(created.session.is_none()); + + let (again, was_new) = file.load_or_create().unwrap(); + assert!(!was_new); + assert_eq!(again.key.public_key(), created.key.public_key()); +} + +#[test] +fn tokens_are_saved_beside_the_key_and_cleared_without_losing_it() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + let (device, _) = file.load_or_create().unwrap(); + + SessionStore::save(&file, &session("one")).unwrap(); + let stored = file.load().unwrap().unwrap(); + assert_eq!(stored.session, Some(session("one"))); + assert_eq!(stored.key.public_key(), device.key.public_key()); + assert_eq!(SessionStore::load(&file).unwrap(), Some(session("one"))); + + file.clear_session().unwrap(); + let cleared = file.load().unwrap().unwrap(); + assert!(cleared.session.is_none()); + assert_eq!(cleared.key.public_key(), device.key.public_key()); +} + +#[test] +fn saving_tokens_for_a_hub_without_a_file_fails() { + let dir = tempfile::tempdir().unwrap(); + assert!(SessionStore::save(&file_in(&dir, "hub.example"), &session("one")).is_err()); +} + +#[cfg(unix)] +#[test] +fn the_file_and_its_directory_are_private() { + use std::os::unix::fs::PermissionsExt as _; + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + file.load_or_create().unwrap(); + SessionStore::save(&file, &session("one")).unwrap(); + + let mode = |path: &Path| fs::metadata(path).unwrap().permissions().mode() & 0o777; + assert_eq!(mode(&file.path), 0o600); + assert_eq!(mode(&file.lock_path), 0o600); + assert_eq!(mode(file.path.parent().unwrap()), 0o700); +} + +#[test] +fn erasing_removes_the_credentials_and_tolerates_their_absence() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + file.load_or_create().unwrap(); + file.erase().unwrap(); + assert!(file.load().unwrap().is_none()); + file.erase().unwrap(); +} + +#[test] +fn every_hub_address_has_its_own_file() { + let dir = tempfile::tempdir().unwrap(); + let names: Vec<_> = ["a.example", "a.example:9", "b.example", "[::1]", "[::1]:9"] + .iter() + .map(|text| file_in(&dir, text).path) + .collect(); + for (index, name) in names.iter().enumerate() { + assert!(!names[..index].contains(name), "{name:?}"); + } +} + +#[test] +fn a_file_of_another_hub_or_a_damaged_one_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let file = file_in(&dir, "hub.example"); + file.load_or_create().unwrap(); + let other = file_in(&dir, "other.example"); + fs::copy(&file.path, &other.path).unwrap(); + assert!(matches!(other.load(), Err(CredentialError::OtherHub))); + + fs::write(&file.path, "not toml [").unwrap(); + assert!(matches!(file.load(), Err(CredentialError::Format(_)))); + fs::write( + &file.path, + "address = \"hub.example:7443\"\nseed = \"abcd\"\n", + ) + .unwrap(); + assert!(matches!(file.load(), Err(CredentialError::Seed(_)))); +} + +#[test] +fn an_address_too_long_for_a_file_name_is_refused() { + let dir = tempfile::tempdir().unwrap(); + let label = "a".repeat(60); + let host = format!("{label}.{label}.{label}.{label}.example"); + let result = CredentialDir::new(dir.path().to_owned()).file(&address(&host)); + assert!(matches!(result, Err(CredentialError::NameTooLong))); +} + +#[test] +fn a_second_lock_waits_until_the_first_is_released() { + let dir = tempfile::tempdir().unwrap(); + let first = file_in(&dir, "hub.example"); + let guard = SessionStore::lock(&first).unwrap(); + + let second = file_in(&dir, "hub.example"); + let (acquired, signal) = mpsc::channel(); + let waiter = thread::spawn(move || { + let _guard = SessionStore::lock(&second).unwrap(); + acquired.send(()).unwrap(); + }); + assert!(signal.recv_timeout(Duration::from_millis(200)).is_err()); + drop(guard); + signal.recv_timeout(Duration::from_secs(10)).unwrap(); + waiter.join().unwrap(); +} + +#[test] +fn waiting_for_the_lock_does_not_stall_the_workers_of_the_runtime_that_holds_it() { + let dir = tempfile::tempdir().unwrap(); + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(2) + .enable_all() + .build() + .unwrap(); + let (held, holding) = mpsc::channel(); + let holder = file_in(&dir, "hub.example"); + runtime.spawn(async move { + let guard = SessionStore::lock(&holder).unwrap(); + held.send(()).unwrap(); + // The holder awaits the network, as a token rotation does. + tokio::time::sleep(Duration::from_millis(200)).await; + drop(guard); + }); + holding.recv_timeout(Duration::from_secs(10)).unwrap(); + + let (acquired, signal) = mpsc::channel(); + for _ in 0..2 { + let (waiter, acquired) = (file_in(&dir, "hub.example"), acquired.clone()); + runtime.spawn(async move { + let _guard = SessionStore::lock(&waiter).unwrap(); + acquired.send(()).unwrap(); + }); + } + for _ in 0..2 { + signal + .recv_timeout(Duration::from_secs(10)) + .expect("both waiters get the lock once the holder is done"); + } +} diff --git a/crates/hub-io/src/devices.rs b/crates/hub-io/src/devices.rs new file mode 100644 index 00000000..7fcc1526 --- /dev/null +++ b/crates/hub-io/src/devices.rs @@ -0,0 +1,100 @@ +//! The devices of the account, listed and revoked through the original native membership. +//! +//! The membership is captured before the future is returned and rechecked after every await, +//! as in [`crate::connect`]'s sign-out: a result for an account that was signed out, replaced +//! or revoked meanwhile is [`HubError::Stale`] and is never shown for the new account. + +use std::sync::Arc; + +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{DeviceSummary, HubError, HubFuture, HubId}; +use tokio::runtime::Handle; + +use crate::classify::classify; +use crate::pool::{Pool, observation::LoginObservation}; +use crate::task::spawn; + +/// The captured membership and its client. +pub(crate) struct Captured { + pub(crate) observed: Arc, + pub(crate) client: HubClient, + pub(crate) hub: HubId, +} + +impl Captured { + pub(crate) fn new(pool: Arc, hub: &HubId) -> Result { + let observed = LoginObservation::capture(pool.clone(), hub.clone())?; + let subject = observed.original_subject()?.ok_or(HubError::NotSignedIn)?; + let client = pool.captured(hub, &subject)?; + observed.check()?; + Ok(Self { + observed, + client, + hub: hub.clone(), + }) + } +} + +pub(crate) fn list(pool: Arc, handle: &Handle, hub: &HubId) -> HubFuture> { + run(Captured::new(pool, hub), handle, |captured| async move { + let listed = captured + .observed + .during(captured.client.account().list_devices(v2::Empty {})) + .await? + .map_err(|status| classify(captured.hub.address(), ClientError::from(status)))? + .into_inner(); + Ok(listed + .devices + .into_iter() + .filter(|device| !device.revoked) + .map(|device| DeviceSummary { + id: device.device_id, + name: device.name, + platform: device.platform, + created_at_ms: device.created_at_ms, + last_seen_at_ms: device.last_seen_at_ms, + is_current: device.current, + }) + .collect()) + }) +} + +pub(crate) fn revoke(pool: Arc, handle: &Handle, hub: &HubId, device: &str) -> HubFuture<()> { + let request = v2::IdRequest { + id: device.to_owned(), + }; + run(Captured::new(pool, hub), handle, |captured| async move { + captured + .observed + .during(captured.client.account().revoke_device(request)) + .await? + .map_err(|status| classify(captured.hub.address(), ClientError::from(status)))?; + Ok(()) + }) +} + +/// Runs `work` on the adapter's threads once the membership was captured, and checks it again +/// before the result is returned. +pub(crate) fn run( + captured: Result, + handle: &Handle, + work: F, +) -> HubFuture +where + T: Send + 'static, + F: FnOnce(Arc) -> Fut, + Fut: std::future::Future> + Send + 'static, +{ + let captured = match captured { + Ok(captured) => Arc::new(captured), + Err(error) => return Box::pin(async move { Err(error) }), + }; + let observed = captured.observed.clone(); + let task = spawn(handle, work(captured)); + Box::pin(async move { + let result = task.await.unwrap_or(Err(HubError::Stale)); + observed.check()?; + result + }) +} diff --git a/crates/hub-io/src/directory.rs b/crates/hub-io/src/directory.rs new file mode 100644 index 00000000..a39cb653 --- /dev/null +++ b/crates/hub-io/src/directory.rs @@ -0,0 +1,24 @@ +//! Account display values are checked against the original authenticated native subject. +use crate::{account::key_admission::KeyAdmission, convert}; +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{Account, HubError}; + +pub(crate) async fn who_am_i( + client: &HubClient, + owner: &KeyAdmission, +) -> Result { + let response = owner + .during(client.account().who_am_i(v2::Empty {})) + .await? + .map_err(|status| owner.failure(ClientError::from(status)))? + .into_inner(); + checked_account(owner, convert::account(response)) +} +pub(crate) fn checked_account(owner: &KeyAdmission, account: Account) -> Result { + owner.check()?; + if account.id.as_str() != owner.member().subject()?.account() { + return Err(HubError::Stale); + } + Ok(account) +} diff --git a/crates/hub-io/src/endpoint.rs b/crates/hub-io/src/endpoint.rs new file mode 100644 index 00000000..4157c46f --- /dev/null +++ b/crates/hub-io/src/endpoint.rs @@ -0,0 +1,48 @@ +//! The contract's endpoint for a hub and the trust decided for it. + +use nocterm_hub_client::{HubEndpoint, Trust}; +use nocterm_hubs_model::{HubEndpointSpec, HubError, TrustPolicy}; + +use crate::classify::remote; + +pub(crate) fn endpoint(spec: &HubEndpointSpec) -> Result { + let trust = match spec.trust { + TrustPolicy::Pinned(fingerprint) => Trust::Pinned(*fingerprint.as_bytes()), + TrustPolicy::PublicCa => Trust::PublicCa, + }; + HubEndpoint::with_trust(&spec.address.to_string(), trust).map_err(remote) +} + +#[cfg(test)] +mod tests { + use nocterm_hubs_model::{Fingerprint, HubAddress}; + + use super::*; + + fn spec(address: &str, trust: TrustPolicy) -> HubEndpointSpec { + HubEndpointSpec { + address: HubAddress::parse(address).unwrap(), + trust, + } + } + + #[test] + fn the_decided_trust_becomes_the_contracts_trust() { + let pinned = Fingerprint::from_bytes([9; 32]); + let endpoint = endpoint(&spec("hub.example:9000", TrustPolicy::Pinned(pinned))).unwrap(); + assert_eq!(endpoint.host(), "hub.example"); + assert_eq!(endpoint.port(), 9000); + assert_eq!(endpoint.trust(), &Trust::Pinned([9; 32])); + + let public = super::endpoint(&spec("hub.example", TrustPolicy::PublicCa)).unwrap(); + assert_eq!(public.port(), 7443); + assert_eq!(public.trust(), &Trust::PublicCa); + } + + #[test] + fn an_ipv6_address_keeps_its_brackets_out_of_the_host() { + let endpoint = endpoint(&spec("[::1]:7443", TrustPolicy::PublicCa)).unwrap(); + assert_eq!(endpoint.host(), "::1"); + assert_eq!(endpoint.port(), 7443); + } +} diff --git a/crates/hub-io/src/hub_account.rs b/crates/hub-io/src/hub_account.rs new file mode 100644 index 00000000..042f8691 --- /dev/null +++ b/crates/hub-io/src/hub_account.rs @@ -0,0 +1,89 @@ +//! The encrypted data of one signed-in account, as the application uses it. + +use std::sync::Arc; + +use tokio::runtime::Handle; + +use crate::account::Slot; +use crate::account::operations::AccountOperations; +use crate::account::source::LocalSourceProvider; +use crate::pool::Pool; +use crate::replica::HubProfileReplica; +use crate::vault::{HubVault, credentials::CredentialCapture}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +/// A hub account's profile replica and vault. They share the keys of one account: unlocking +/// the vault opens both, and locking it (or signing out) closes both. +pub struct HubAccount { + handle: Handle, + vault: Arc, + source: LocalSourceProvider, + credentials: CredentialCapture, + relay: crate::relay::RelayCapture, + operations: Arc, +} + +impl HubAccount { + pub(crate) fn new(handle: Handle, pool: Arc, slot: Arc) -> Self { + let relay = crate::relay::RelayCapture::new(handle.clone(), pool.clone()); + let source = LocalSourceProvider::new(Arc::clone(&slot), pool.clone()); + let operations = Arc::new(AccountOperations::new(source.clone(), &pool)); + let vault = Arc::new(HubVault::new( + handle.clone(), + Arc::clone(&slot), + operations.clone(), + )); + let credentials = + CredentialCapture::new(handle.clone(), operations.stores().clone(), &vault.idle()); + Self { + handle, + vault, + source, + credentials, + relay, + operations, + } + } + + /// The saved profiles of the account. + pub fn replica(&self) -> Result, HubError> { + self.local_replica(self.source.capture()?) + } + + /// An immutable replica for this exact authenticated account/key owner. + pub fn local_replica( + &self, + source: Arc, + ) -> Result, HubError> { + Ok(Arc::new(HubProfileReplica::new( + self.handle.clone(), + self.operations.bind(source)?, + ))) + } + + /// The vault of the account. + pub fn vault(&self) -> Arc { + Arc::clone(&self.vault) + } + /// Captures this configured hub's currently signed-in, unlocked account for + /// device-local history. Sign-out or replacement login makes it stale; lock and + /// ordinary token refresh do not. + pub fn local_source(&self) -> Result, HubError> { + self.source.capture() + } + /// Relay requester tied to the original account, membership and key owner. + pub fn local_relay( + &self, + source: Arc, + ) -> Result, HubError> { + self.relay.account(&self.source, source) + } + /// Credentials restricted to the captured native account and the caller's + /// current source. A stale capture never follows a replacement login. + pub fn local_credentials( + &self, + source: Arc, + ) -> Result, HubError> { + self.credentials.capture(&self.source, source) + } +} diff --git a/crates/hub-io/src/lib.rs b/crates/hub-io/src/lib.rs new file mode 100644 index 00000000..d3cd1a87 --- /dev/null +++ b/crates/hub-io/src/lib.rs @@ -0,0 +1,80 @@ +//! Hub adapter: reaches Nocterm hubs through the external hub contract. +//! +//! This is the only crate that knows the hub protocol exists. It speaks it through +//! `nocterm-hub-client` (pinned TLS, signed requests, token refresh) and shows the rest of the +//! application the neutral ports of `nocterm-hubs-model`, `nocterm-hub-agents-model`, +//! `nocterm-ai`, `nocterm-vault`, `nocterm-connections-model` and `nocterm-session`. Like +//! `nocterm-ssh`, it owns a small Tokio runtime, so callers need none: every call returns a +//! runtime-agnostic future. +//! +//! Owns: connections to hubs (one client per hub), the probe of a hub's certificate, password +//! and browser sign-in and sign-out (which revokes the device on the hub), the private +//! credential file of this device for each hub with its inter-process lock, the mapping of hub +//! failures to `HubError`, the mapping of hub records to the model's records, the relayed byte +//! streams of SSH routes that use a hub as their first step, and the encrypted data of one +//! signed-in account: the account key (opened at sign-in, unlock and recovery, zeroed on lock), +//! the vault collections sealed under it, their encrypted local cache, the vault ([`HubVault`]) +//! and the profile replica ([`HubProfileReplica`]) built on them, handed out together as a +//! [`HubAccount`]. +//! Agents, providers and servers (`agents`, `servers`) implement the ports of +//! `nocterm-hub-agents-model` over `AgentService` and `ServerService`; [`HubAgentConnector`] +//! implements `nocterm_ai::AgentConnector` over `ChatService` (a hub agent's chat is an +//! ordinary agent chat whose session is the hub chat), and [`HubChatRepository`] lists, renames, +//! pins and deletes hub chats beside the local history of a hub window. +//! Does not own: which hubs the user knows or trusts (the `[hubs]` settings section and the +//! trust decision belong to `nocterm-hubs-model` and the callers), connection status, retries, +//! windows, terminal emulation, the transcript of a chat, or any view. +//! Ports provided: `HubTrust`, `HubSignIn` and `AccountDevices` from `nocterm-hubs-model` +//! (by [`HubLinks`]) and `AccountKeyLock` (by [`AccountKeys`]); `HubAgentDirectory`, +//! `HubServers` and `HubChanges` (by [`HubAgentsLink`]); `AgentConnector` and +//! `ChatRepository` of `nocterm-ai`; the `LocalAccountSource` captured by [`HubAccount`], its +//! captured `CredentialStore` of `nocterm-session`, the `VaultAccess` of `nocterm-vault` and +//! the `ProfileReplica` of `nocterm-connections-model`, implemented by [`HubVault`] and +//! [`HubProfileReplica`], and the `RelayDialer` of `nocterm-session`, implemented by +//! [`HubRelay`] over the clients of the hubs this device is signed in to. Ports used: none. +//! Settings section: none; the callers pass the directory for credentials. +//! Globals: none. +//! Invariants: the device key and tokens live only in a mode-0600 file of a mode-0700 +//! directory, published atomically and read under an inter-process lock; a pinned hub is +//! trusted only with the exact pinned certificate; a revoked device erases its credentials, its +//! account cache and its keys; the account key lives only in memory, is zeroed on lock and +//! follows its own lock policy, independent of the local vault's; only ciphertext and non-secret +//! facts are written to disk (mode 0600, atomically); the local history partition of a hub +//! window depends on the signed-in account only, so a locked vault never blocks chats; no tonic +//! or prost type crosses the crate boundary, and no text built from a failure does; a relay +//! finds its hub only among the hubs signed in to and never signs in; every buffer between the +//! transport and the hub's stream is bounded, and dropping either end of a relayed stream ends +//! both directions; a record of the hub that does not authenticate is an error, never a silent +//! skip or overwrite; collections and record kinds this version does not know are never +//! listed, changed or deleted. + +mod account; +mod account_keys; +mod agents; +mod cache; +mod chats; +mod checked; +mod classify; +mod connect; +mod convert; +mod credentials; +mod devices; +mod directory; +mod endpoint; +mod hub_account; +mod links; +mod pool; +mod relay; +mod replica; +mod store; +mod task; +mod vault; + +pub use account_keys::AccountKeys; +pub use agents::HubAgentsLink; +pub use chats::{HubAgentConnector, HubChatRepository}; +pub use hub_account::HubAccount; +pub use links::{HubLinks, HubLinksConfig, HubStartError}; +pub use relay::HubRelay; +pub use replica::HubProfileReplica; +pub use vault::HubVault; diff --git a/crates/hub-io/src/links.rs b/crates/hub-io/src/links.rs new file mode 100644 index 00000000..ec70b4f6 --- /dev/null +++ b/crates/hub-io/src/links.rs @@ -0,0 +1,276 @@ +//! The hub adapter's entry point: the runtime and the three ports. + +use std::future::Future; +use std::path::PathBuf; +use std::sync::Arc; + +use futures::FutureExt as _; +use futures::channel::oneshot; +use futures::future::BoxFuture; +use nocterm_hub_client::HubEndpoint; +use nocterm_hubs_model::{ + AccountDevices, DeviceSummary, Fingerprint, HubAddress, HubEndpointSpec, HubError, HubFuture, + HubId, HubInfo, HubSignIn, HubTrust, KeyStatus, OidcSignIn, ProbedHub, SignedIn, +}; +use nocterm_session::Secret; +use tokio::runtime::{Builder, Handle, Runtime}; + +use crate::account::{Accounts, KeyFlow}; +use crate::classify::{probe_failed, remote}; +use crate::connect::Connector; +use crate::hub_account::HubAccount; +use crate::pool::Pool; + +/// Threads serving every hub connection of the application. +const WORKER_THREADS: usize = 2; + +/// Where the adapter keeps what it needs and how this device is named to hubs. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct HubLinksConfig { + /// The private directory with one credential file per hub. Created when first needed. + pub credentials_dir: PathBuf, + /// The machine as the user knows it, listed by every hub this device signs in to. + pub device_name: String, +} + +/// The runtime of the adapter could not start. +#[derive(Debug, thiserror::Error)] +#[error("the hub connection threads cannot start")] +pub struct HubStartError(#[source] std::io::Error); + +/// Reaches hubs. Implements the hub ports of `nocterm-hubs-model`. +/// +/// Calls run on the adapter's threads; dropping the future of a call abandons it. Dropping the +/// links ends every call under way. +pub struct HubLinks { + // `None` only while the links are being dropped. + runtime: Option, + handle: Handle, + pool: Arc, + connector: Arc, + accounts: Arc, + keys: Arc, + chat_hints: Arc, +} + +/// The adapter's threads have stopped. +#[derive(Debug, thiserror::Error)] +#[error("the hub connection threads have stopped")] +struct Stopped; + +impl HubLinks { + pub fn start(config: HubLinksConfig) -> Result, HubStartError> { + let runtime = Builder::new_multi_thread() + .worker_threads(WORKER_THREADS) + .thread_name("nocterm-hub") + .enable_all() + .build() + .map_err(HubStartError)?; + let accounts = Arc::new(Accounts::with_runtime( + config.credentials_dir.clone(), + Some(runtime.handle().clone()), + )); + let pool = Arc::new(Pool::new( + config.credentials_dir, + &config.device_name, + Arc::clone(&accounts), + )); + let keys = KeyFlow::new( + Arc::clone(&pool), + Arc::clone(&accounts), + runtime.handle().clone(), + ); + Ok(Arc::new(Self { + handle: runtime.handle().clone(), + runtime: Some(runtime), + connector: Arc::new(Connector::new(Arc::clone(&pool), Arc::clone(&keys))), + pool, + accounts, + keys, + chat_hints: Arc::default(), + })) + } + + /// The encrypted data of the account this device is signed in to on `hub`: its profile + /// replica and its vault. Both share the account's keys, which open at sign-in and close + /// when the vault locks or the device signs out. + pub fn open_account(&self, hub: &HubId) -> Result { + let slot = self.accounts.slot(hub)?; + Ok(HubAccount::new( + self.handle.clone(), + Arc::clone(&self.pool), + slot, + )) + } + + pub(crate) fn account_keys_open(&self, hub: &HubId) -> bool { + self.accounts + .loaded_slot(hub) + .is_some_and(|slot| slot.open().is_some()) + } + + /// Native membership publication always creates its Slot first. An absent + /// loaded Slot therefore cannot be unlocked for a current native recipient. + pub(crate) fn loaded_account(&self, hub: &HubId) -> Option { + self.accounts + .loaded_slot(hub) + .map(|slot| HubAccount::new(self.handle.clone(), self.pool.clone(), slot)) + } + + /// What a relay needs of the links: their threads and the hubs they are signed in to. + pub(crate) fn relay_parts(&self) -> (Handle, Arc) { + (self.handle.clone(), Arc::clone(&self.pool)) + } + + /// Hints that a chat list kept on this device changed. + pub(crate) fn chat_hints(&self) -> Arc { + Arc::clone(&self.chat_hints) + } + + /// What the agents and chats adapters need of the links. + pub(crate) fn agent_parts(&self) -> (Handle, Arc) { + (self.handle.clone(), Arc::clone(&self.pool)) + } + + fn run_boxed( + &self, + work: impl Future> + Send + 'static, + ) -> HubFuture { + crate::task::spawn(&self.handle, work) + .map(|result| result.unwrap_or_else(|| Err(remote(Stopped)))) + .boxed() + } +} + +impl Drop for HubLinks { + fn drop(&mut self) { + self.pool.stop(); + // A runtime must not block where it is dropped: that may be a thread that is itself + // running async code. + if let Some(runtime) = self.runtime.take() { + runtime.shutdown_background(); + } + } +} + +impl HubTrust for HubLinks { + fn probe(&self, address: &HubAddress) -> HubFuture { + let address = address.clone(); + self.run_boxed(async move { + let probe = HubEndpoint::probe(&address.to_string()) + .await + .map_err(|error| probe_failed(&address, error))?; + Ok(ProbedHub { + fingerprint: Fingerprint::from_bytes(probe.fingerprint), + publicly_trusted: probe.publicly_trusted, + }) + }) + } +} + +impl HubSignIn for HubLinks { + fn connect(&self, endpoint: HubEndpointSpec) -> HubFuture { + match self.connector.reconnect(endpoint) { + Ok(request) => request.start(&self.handle), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + fn password( + &self, + endpoint: HubEndpointSpec, + username: String, + password: Secret, + ) -> HubFuture { + let connector = Arc::clone(&self.connector); + match connector.begin(endpoint) { + Ok(start) => start.start(&self.handle, move |start| async move { + connector.password(start, username, password).await + }), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + fn oidc(&self, endpoint: HubEndpointSpec, provider: String) -> OidcSignIn { + let connector = Arc::clone(&self.connector); + let (browser_sender, browser_receiver) = oneshot::channel(); + match connector.begin(endpoint) { + Ok(start) => { + let observed = start.observation().clone(); + let done = start.start(&self.handle, move |start| async move { + connector.oidc(start, provider, browser_sender).await + }); + let browser = Box::pin(async move { + observed.check()?; + let result = observed + .during(browser_receiver) + .await? + .map_err(|_| HubError::Stale)?; + observed.check()?; + Ok(result) + }); + OidcSignIn { browser, done } + } + Err(error) => OidcSignIn { + browser: Box::pin(async { Err(HubError::Stale) }), + done: Box::pin(async move { Err(error) }), + }, + } + } + + fn set_vault_passphrase(&self, hub: &HubId, passphrase: Secret) -> HubFuture { + let keys = self.keys.clone(); + self.keys + .run(self.keys.capture(hub), move |owner| async move { + keys.passphrase(&owner, passphrase).await + }) + } + + fn recover(&self, hub: &HubId, recovery_key: Secret) -> HubFuture<()> { + let keys = self.keys.clone(); + self.keys + .run(self.keys.capture(hub), move |owner| async move { + keys.recover(&owner, recovery_key).await + }) + } + + fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + self.keys.recovery_key_unconfirmed(hub) + } + + fn keys_revision(&self) -> u64 { + self.keys.keys_revision() + } + + fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64> { + self.keys.keys_changed(seen) + } + + fn recovery_key_offer( + &self, + hub: &HubId, + ) -> Result { + self.keys.recovery_offer(hub) + } + + fn sign_out(&self, hub: &HubId) -> HubFuture<()> { + match self.connector.sign_out(hub) { + Ok(request) => request.start(&self.handle), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + fn forget(&self, hub: &HubId) -> Result<(), HubError> { + self.connector.forget(hub) + } +} + +impl AccountDevices for HubLinks { + fn list(&self, hub: &HubId) -> HubFuture> { + crate::devices::list(Arc::clone(&self.pool), &self.handle, hub) + } + + fn revoke(&self, hub: &HubId, device: &str) -> HubFuture<()> { + crate::devices::revoke(Arc::clone(&self.pool), &self.handle, hub, device) + } +} diff --git a/crates/hub-io/src/pool.rs b/crates/hub-io/src/pool.rs new file mode 100644 index 00000000..d71e6fc2 --- /dev/null +++ b/crates/hub-io/src/pool.rs @@ -0,0 +1,442 @@ +//! The clients of the hubs this device is signed in to, and where it keeps their credentials. + +pub(crate) mod captured; +pub(crate) mod observation; +use observation::{MembershipEnd, ObservedLogin}; + +use std::collections::HashMap; +use std::path::PathBuf; +use std::sync::{ + Arc, Mutex, MutexGuard, PoisonError, + atomic::{AtomicBool, Ordering}, +}; + +use nocterm_hub_client::{DeviceInfo, HubClient}; +use nocterm_hubs_model::{HubAddress, HubError, HubId, TrustPolicy}; + +use crate::account::source::{AccountSubject, AccountSubjects}; +use crate::account::{Accounts, OpenKeys, Slot}; +use crate::classify::credential_file; +use crate::credentials::{CredentialDir, CredentialFile, CredentialLoginId, LoginPublication}; + +/// The application name a hub lists beside the device. +const APP_NAME: &str = "Nocterm"; + +/// The client of a hub and the trust it was connected with. +struct Pooled { + trust: TrustPolicy, + client: HubClient, + subject: Option>, + login: Option, +} + +/// The exact native owner created by one successful explicit publication. +pub(crate) struct PublishedLogin { + login: CredentialLoginId, + subject: Arc, +} +impl PublishedLogin { + pub(crate) fn into_parts(self) -> (CredentialLoginId, Arc) { + (self.login, self.subject) + } +} + +/// One client per hub: clients of a hub share the turn in which its tokens are renewed, and +/// two of them would renew the same refresh token against each other. +/// +/// The pool also knows the account keys of the hubs, only to wipe them when a hub revokes this +/// device: whatever the hub no longer trusts leaves nothing of its account behind. +pub(crate) struct Pool { + credentials: CredentialDir, + device: DeviceInfo, + accounts: Arc, + clients: Mutex>, + stopped: AtomicBool, +} + +impl Pool { + pub(crate) fn new( + credentials_dir: PathBuf, + device_name: &str, + accounts: Arc, + ) -> Self { + Self { + credentials: CredentialDir::new(credentials_dir), + device: DeviceInfo::new(device_name, APP_NAME), + accounts, + clients: Mutex::default(), + stopped: AtomicBool::new(false), + } + } + + pub(crate) fn stop(&self) { + self.stopped.store(true, Ordering::Release); + } + pub(crate) fn is_stopped(&self) -> bool { + self.stopped.load(Ordering::Acquire) + } + pub(crate) fn device(&self) -> &DeviceInfo { + &self.device + } + + pub(crate) fn file(&self, address: &HubAddress) -> Result { + self.credentials.file(address).map_err(credential_file) + } + + fn clients(&self) -> MutexGuard<'_, HashMap> { + self.clients.lock().unwrap_or_else(PoisonError::into_inner) + } + + /// Publishes only this captured file origin and original native selection. + /// All paths acquire clients before the credential file lock. + pub(crate) fn publish_login( + &self, + hub: HubId, + trust: TrustPolicy, + client: HubClient, + publication: LoginPublication<'_>, + expected: &ObservedLogin, + ) -> Result { + let subject = Arc::new(subject_of(&client).ok_or(HubError::NotSignedIn)?); + let session = publication.session(); + if subject.account() != session.account_id || subject.device() != session.device_id { + return Err(HubError::Stale); + } + let slot = self.accounts.slot(&hub)?; + let mut clients = self.clients(); + self.validate_observed(&clients, &hub, &slot, expected)?; + let login = publication.save().map_err(credential_file)?; + publication.bind(&client, login); + slot.begin_membership(&subject); + clients.insert( + hub, + Pooled { + trust, + client, + subject: Some(subject.clone()), + login: Some(login), + }, + ); + Ok(PublishedLogin { login, subject }) + } + + /// A reconnect/refresh retains membership only for the same actual subject + /// and trust. Explicit sign-in publishes a new local login even for the same ids. + pub(crate) fn renew_observed( + &self, + hub: HubId, + trust: TrustPolicy, + client: HubClient, + login: Option, + expected: &ObservedLogin, + ) -> Result { + self.renew_inner(hub, trust, client, login, Some(expected)) + } + + fn renew_inner( + &self, + hub: HubId, + trust: TrustPolicy, + client: HubClient, + login: Option, + expected: Option<&ObservedLogin>, + ) -> Result { + let actual = subject_of(&client).ok_or(HubError::NotSignedIn)?; + let slot = self.accounts.slot(&hub)?; + let mut clients = self.clients(); + if let Some(expected) = expected { + self.validate_observed(&clients, &hub, &slot, expected)?; + } + if !self + .file(hub.address())? + .matches_login(actual.account(), actual.device(), login) + .map_err(credential_file)? + { + return Err(HubError::Stale); + } + let retained = clients.get(&hub).and_then(|pooled| { + let subject = pooled.subject.as_ref()?; + (pooled.trust == trust + && pooled.login == login + && pooled.client.info().hub_id == client.info().hub_id + && &actual == subject.as_ref() + && subject_of(&pooled.client).as_ref() == Some(subject.as_ref())) + .then(|| Arc::clone(subject)) + }); + let subject = match retained { + Some(subject) => subject, + None => { + let subject = Arc::new(actual); + slot.begin_membership(&subject); + subject + } + }; + clients.insert( + hub, + Pooled { + trust, + client, + subject: Some(subject.clone()), + login, + }, + ); + Ok(ObservedLogin::new(Some(&subject), slot.observed())) + } + + /// The client of the hub if it was connected with this trust. + pub(crate) fn connected( + &self, + hub: &HubId, + trust: TrustPolicy, + ) -> Option<(HubClient, Option)> { + let clients = self.clients(); + let pooled = clients.get(hub)?; + (pooled.trust == trust).then(|| (pooled.client.clone(), pooled.login)) + } + + /// A direct configured-hub removal serializes against every pending login. + pub(crate) fn forget_hub(&self, hub: &HubId) -> Result<(), HubError> { + let slot = self.accounts.slot(hub)?; + let mut clients = self.clients(); + clients.remove(hub); + let cache = slot.erase(); + let file = self + .file(hub.address())? + .erase() + .map_err(crate::classify::credentials); + if cache.is_ok() { + self.accounts.evict_if(hub, &slot); + } + cache.and(file) + } + + /// Acquires only this original membership, synchronously and without I/O. + pub(crate) fn captured( + &self, + hub: &HubId, + expected: &Arc, + ) -> Result { + self.with_captured(hub, expected, |client| Ok(client.clone())) + } + + /// One admission policy for original native I/O and synchronous publication. + /// The action may lock Slot/cache, but must never await or call back into Pool. + pub(crate) fn with_captured( + &self, + hub: &HubId, + expected: &Arc, + action: impl FnOnce(&HubClient) -> Result, + ) -> Result { + let clients = self.clients(); + let pooled = self.matching(&clients, hub, expected)?; + action(&pooled.client) + } + + fn matching<'a>( + &self, + clients: &'a HashMap, + hub: &HubId, + expected: &Arc, + ) -> Result<&'a Pooled, HubError> { + let pooled = clients.get(hub).ok_or(HubError::Stale)?; + let subject = pooled.subject.as_ref().ok_or(HubError::Stale)?; + if self.is_stopped() + || !Arc::ptr_eq(subject, expected) + || subject_of(&pooled.client).as_ref() != Some(expected.as_ref()) + { + return Err(HubError::Stale); + } + Ok(pooled) + } + + pub(crate) fn observe(&self, hub: &HubId) -> Result<(Arc, ObservedLogin), HubError> { + // Use the existing subject reader to retire an SDK subject that already ended. + // Otherwise a cleared SDK session would leave reconnect stuck on a stale map row. + let _ = self.current(hub); + let slot = self.accounts.slot(hub)?; + let clients = self.clients(); + if self.is_stopped() { + return Err(HubError::Stale); + } + let subject = clients.get(hub).and_then(|pooled| pooled.subject.as_ref()); + if let Some(subject) = subject { + self.matching(&clients, hub, subject)?; + } + let observed = ObservedLogin::new(subject, slot.observed()); + Ok((slot, observed)) + } + + fn validate_observed( + &self, + clients: &HashMap, + hub: &HubId, + slot: &Slot, + expected: &ObservedLogin, + ) -> Result<(), HubError> { + if self.is_stopped() || slot.hub() != hub { + return Err(HubError::Stale); + } + match expected.subject()? { + Some(subject) => { + self.matching(clients, hub, &subject)?; + return slot.belongs_to(&subject); + } + None if clients.contains_key(hub) => return Err(HubError::Stale), + None => {} + } + slot.check_observed(expected.receipt()) + } + + pub(crate) fn with_observed( + &self, + hub: &HubId, + slot: &Slot, + expected: &ObservedLogin, + action: impl FnOnce() -> Result, + ) -> Result { + let clients = self.clients(); + self.validate_observed(&clients, hub, slot, expected)?; + action() + } + + pub(crate) fn observe_original( + &self, + hub: &HubId, + expected: &Arc, + ) -> Result { + let slot = self.accounts.slot(hub)?; + self.with_captured(hub, expected, |_| { + slot.belongs_to(expected)?; + Ok(ObservedLogin::new(Some(expected), slot.observed())) + }) + } + + pub(crate) fn retire_captured( + &self, + hub: &HubId, + expected: &Arc, + slot: &Slot, + keys: &Arc, + ) { + if self.retire_membership(hub, expected) + && let Err(error) = slot.erase_if(keys) + { + tracing::warn!(%error, "cannot erase captured revoked account cache"); + } + } + + /// Retires the exact membership and its local file login; relay callers + /// without unlocked keys use this same conditional credential boundary. + pub(crate) fn retire_membership(&self, hub: &HubId, expected: &Arc) -> bool { + match self.retire_original(hub, expected, false) { + Ok(Some(ended)) => { + let (_, result) = ended.into_parts(); + if let Err(error) = result { + tracing::warn!(%error,"cannot retire original revoked membership"); + } + true + } + Ok(None) => false, + Err(error) => { + tracing::warn!(%error,"cannot retire original revoked membership"); + false + } + } + } + + pub(crate) fn end_membership( + &self, + hub: &HubId, + expected: &Arc, + expire: bool, + ) -> Result { + self.retire_original(hub, expected, expire)? + .ok_or(HubError::Stale) + } + + fn retire_original( + &self, + hub: &HubId, + expected: &Arc, + expire: bool, + ) -> Result, HubError> { + let mut clients = self.clients(); + if !clients + .get(hub) + .and_then(|pooled| pooled.subject.as_ref()) + .is_some_and(|subject| Arc::ptr_eq(subject, expected)) + { + return Ok(None); + } + let slot = self.accounts.slot(hub)?; + let login = clients.remove(hub).and_then(|pooled| pooled.login); + let mut slot_error = None; + let retired = self + .file(hub.address())? + .retire_if( + expected.account(), + expected.device(), + login, + !expire, + || { + let result = if expire { + slot.close_for(expected).map(|_| ()) + } else { + slot.erase_for(expected) + }; + slot_error = result.err(); + }, + ) + .map_err(credential_file); + if !matches!(&retired, Ok(true)) { + // A newer private-file login belongs to somebody else. Drop only our + // original in-memory keys; never remove that login's disk cache. + let _ = slot.close_for(expected); + } + let result = retired.and_then(|owned| { + if let Some(error) = slot_error { + return Err(error); + } + if owned { Ok(()) } else { Err(HubError::Stale) } + }); + Ok(Some(MembershipEnd::new( + ObservedLogin::new(None, slot.observed()), + result, + ))) + } +} + +impl AccountSubjects for Pool { + fn with_current( + &self, + hub: &HubId, + expected: &Arc, + action: &mut dyn FnMut() -> Result<(), HubError>, + ) -> Result<(), HubError> { + self.with_captured(hub, expected, |_| action()) + } + + fn current(&self, hub: &HubId) -> Option> { + let mut clients = self.clients(); + let pooled = clients.get(hub)?; + let subject = pooled.subject.as_ref()?; + if subject_of(&pooled.client).as_ref() == Some(subject.as_ref()) { + return Some(Arc::clone(subject)); + } + // A changed or cleared SDK subject cannot later revive this membership. + let original = Arc::clone(subject); + clients.remove(hub); + if let Some(slot) = self.accounts.loaded_slot(hub) { + let _ = slot.close_for(&original); + } + None + } +} + +pub(crate) fn subject_of(client: &HubClient) -> Option { + let session = client.session()?; + Some(AccountSubject::new(session.account_id, session.device_id)) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/pool/captured.rs b/crates/hub-io/src/pool/captured.rs new file mode 100644 index 00000000..72c43a59 --- /dev/null +++ b/crates/hub-io/src/pool/captured.rs @@ -0,0 +1,156 @@ +//! One original SDK client and native membership, shared by captured I/O consumers. +use super::{Pool, subject_of}; +use crate::{ + account::{ + OpenKeys, Slot, + source::{AccountSubject, AccountSubjects, CapturedAccount}, + }, + classify::classify, +}; +use nocterm_hub_client::{ClientError, HubClient, StatusCode}; +use nocterm_hubs_model::{HubError, HubId, source_identity::LocalAccountSource}; +use std::sync::{Arc, Weak}; + +struct KeyOwner { + slot: Weak, + keys: Weak, +} +pub(crate) struct CapturedClient { + pool: Arc, + hub: HubId, + client: HubClient, + subject: Weak, + source: Option>, + owner: Option, +} +impl CapturedClient { + /// Capture the actual signed-in requester now, before any worker is spawned. + pub(crate) fn current(pool: Arc, hub: HubId) -> Result { + let subject = pool.current(&hub).ok_or(HubError::NotSignedIn)?; + let client = pool.captured(&hub, &subject)?; + let owner = pool.accounts.loaded_slot(&hub).and_then(|slot| { + let keys = slot.open()?; + (keys.keyset().account_id() == subject.account()).then(|| KeyOwner { + slot: Arc::downgrade(&slot), + keys: Arc::downgrade(&keys), + }) + }); + Ok(Self { + pool, + hub, + client, + subject: Arc::downgrade(&subject), + source: None, + owner, + }) + } + pub(crate) fn account( + pool: Arc, + native: &CapturedAccount, + source: Arc, + ) -> Result { + source.check_current().map_err(|_| HubError::NotSignedIn)?; + native.check_current().map_err(|_| HubError::NotSignedIn)?; + if source.partition() != native.partition() { + return Err(HubError::NotSignedIn); + } + let slot = native.slot()?; + let keys = native.keys()?; + let subject = native.subject()?; + let hub = slot.hub().clone(); + let client = pool.captured(&hub, &subject)?; + Ok(Self { + pool, + hub, + client, + subject: Arc::downgrade(&subject), + source: Some(source), + owner: Some(KeyOwner { + slot: Arc::downgrade(&slot), + keys: Arc::downgrade(&keys), + }), + }) + } + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + pub(crate) fn check(&self) -> Result<(), HubError> { + if self.pool.is_stopped() { + return Err(HubError::NotSignedIn); + } + if let Some(source) = &self.source { + source.check_current().map_err(|_| HubError::NotSignedIn)?; + } + let expected = self.subject.upgrade().ok_or(HubError::NotSignedIn)?; + self.pool.captured(&self.hub, &expected)?; + if subject_of(&self.client).as_ref() != Some(expected.as_ref()) { + return Err(HubError::NotSignedIn); + } + if self.source.is_some() + && let Some(owner) = &self.owner + { + let slot = owner.slot.upgrade().ok_or(HubError::NotSignedIn)?; + let keys = owner.keys.upgrade().ok_or(HubError::KeysLocked)?; + if !slot + .open() + .is_some_and(|current| Arc::ptr_eq(¤t, &keys)) + { + return Err(HubError::NotSignedIn); + } + } + Ok(()) + } + /// Actual worker admission, including every resume after an awaited operation. + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } + pub(crate) async fn fresh(&self) -> Result { + self.check()?; + self.during(self.client.ensure_fresh()) + .await? + .map_err(|error| self.failure(error))?; + self.check()?; + Ok(self.client.clone()) + } + pub(crate) fn failure(&self, error: ClientError) -> HubError { + if matches!(error, ClientError::Revoked) + && let Some(subject) = self.subject.upgrade() + { + let original = self + .owner + .as_ref() + .and_then(|owner| Some((owner.slot.upgrade()?, owner.keys.upgrade()?))); + if let Some((slot, keys)) = original { + self.pool.retire_captured(&self.hub, &subject, &slot, &keys); + } else { + self.pool.retire_membership(&self.hub, &subject); + } + } + if matches!( + &error, + ClientError::NotSignedIn + | ClientError::Status { + code: StatusCode::Unauthenticated, + .. + } + ) { + let Some(subject) = self.subject.upgrade() else { + return HubError::Stale; + }; + // Authentication rejection ends only the original local login. Unlike + // an explicit Revoked code, expiry keeps the device key and ciphertext. + let ended = match self.pool.end_membership(&self.hub, &subject, true) { + Ok(ended) => ended, + Err(error) => return error, + }; + let (_, retired) = ended.into_parts(); + if let Err(error) = retired { + return error; + } + } + classify(self.hub.address(), error) + } +} diff --git a/crates/hub-io/src/pool/observation.rs b/crates/hub-io/src/pool/observation.rs new file mode 100644 index 00000000..bad32f14 --- /dev/null +++ b/crates/hub-io/src/pool/observation.rs @@ -0,0 +1,155 @@ +//! A reconnect observes one original native owner, including an initially absent one. +use super::{AccountSubject, Pool}; +use crate::account::{Slot, SlotReceipt}; +use crate::{ + classify::credential_file, + credentials::{CredentialFile, CredentialLoginId}, +}; +use nocterm_hubs_model::{HubError, HubId}; +use std::sync::{Arc, Mutex, PoisonError, Weak}; + +#[derive(Clone)] +pub(crate) struct ObservedLogin { + subject: Option>, + receipt: SlotReceipt, +} +impl ObservedLogin { + pub(super) fn new(subject: Option<&Arc>, receipt: SlotReceipt) -> Self { + Self { + subject: subject.map(Arc::downgrade), + receipt, + } + } + pub(super) fn subject(&self) -> Result>, HubError> { + self.subject + .as_ref() + .map(|subject| subject.upgrade().ok_or(HubError::Stale)) + .transpose() + } + pub(crate) fn receipt(&self) -> &SlotReceipt { + &self.receipt + } +} + +/// Retirement can close the local owner even when a disk cleanup reports an error. +pub(crate) struct MembershipEnd { + observed: ObservedLogin, + result: Result<(), HubError>, +} +impl MembershipEnd { + pub(super) fn new(observed: ObservedLogin, result: Result<(), HubError>) -> Self { + Self { observed, result } + } + pub(crate) fn into_parts(self) -> (ObservedLogin, Result<(), HubError>) { + (self.observed, self.result) + } +} + +pub(crate) struct LoginObservation { + pool: Arc, + hub: HubId, + slot: Arc, + expected: Mutex, +} +impl LoginObservation { + pub(crate) fn capture(pool: Arc, hub: HubId) -> Result, HubError> { + let (slot, expected) = pool.observe(&hub)?; + Ok(Arc::new(Self { + pool, + hub, + slot, + expected: Mutex::new(expected), + })) + } + pub(crate) fn hub(&self) -> &HubId { + &self.hub + } + pub(crate) fn slot(&self) -> &Arc { + &self.slot + } + pub(crate) fn snapshot(&self) -> ObservedLogin { + self.expected + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } + pub(crate) fn published(&self, expected: ObservedLogin) { + *self.expected.lock().unwrap_or_else(PoisonError::into_inner) = expected; + } + pub(crate) fn ended_absence(&self, receipt: SlotReceipt) { + self.published(ObservedLogin::new(None, receipt)); + } + pub(crate) fn original_subject(&self) -> Result>, HubError> { + self.snapshot().subject() + } + pub(crate) fn became(&self, subject: &Arc) -> Result<(), HubError> { + self.published(self.pool.observe_original(&self.hub, subject)?); + Ok(()) + } + pub(crate) fn retire( + &self, + file: &CredentialFile, + subject: Option<(&str, &str)>, + login: Option, + erase: bool, + ) -> Result<(), HubError> { + if let Some(subject) = self.original_subject()? { + let (next, result) = self + .pool + .end_membership(&self.hub, &subject, !erase)? + .into_parts(); + self.published(next); + return result; + } + let expected = self.snapshot(); + let next = self.with_current(|| { + let mut slot_error = None; + let owned = match subject { + Some((account, device)) => file + .retire_if(account, device, login, erase, || { + match self.slot.end_observed(expected.receipt(), erase) { + Ok(ended) => { + let (receipt, result) = ended.into_parts(); + self.ended_absence(receipt); + slot_error = result.err(); + } + Err(error) => slot_error = Some(error), + } + }) + .map_err(credential_file)?, + None => false, + }; + if !owned { + let (receipt, _) = self + .slot + .end_observed(expected.receipt(), false)? + .into_parts(); + self.ended_absence(receipt); + return Err(HubError::Stale); + } + if let Some(error) = slot_error { + return Err(error); + } + Ok(ObservedLogin::new(None, self.slot.observed())) + })?; + self.published(next); + Ok(()) + } + pub(crate) fn check(&self) -> Result<(), HubError> { + self.pool + .with_observed(&self.hub, &self.slot, &self.snapshot(), || Ok(())) + } + pub(crate) fn with_current( + &self, + action: impl FnOnce() -> Result, + ) -> Result { + self.pool + .with_observed(&self.hub, &self.slot, &self.snapshot(), action) + } + pub(crate) async fn during( + &self, + future: impl std::future::Future, + ) -> Result { + crate::checked::during(|| self.check(), future).await + } +} diff --git a/crates/hub-io/src/pool/tests.rs b/crates/hub-io/src/pool/tests.rs new file mode 100644 index 00000000..c8ceb42f --- /dev/null +++ b/crates/hub-io/src/pool/tests.rs @@ -0,0 +1,174 @@ +//! Offline/reconnect failures retire the captured native file and Slot receipt. +use super::*; +use crate::{ + account::testing::{ACCOUNT_ID, hub, sign_in}, + cache::CacheFile, + pool::observation::LoginObservation, +}; +use nocterm_hub_client::Session; +use nocterm_vault::VaultStatus; + +#[test] +fn forgetting_a_hub_releases_its_original_slot_and_fences_old_absent_observations() { + let login = Login::new(); + let original = Arc::downgrade(&login.slot); + login.pool.forget_hub(&hub()).unwrap(); + assert!(matches!(login.observed.check(), Err(HubError::Stale))); + assert_eq!(login.slot.status(), VaultStatus::Missing); + let accounts = login.accounts.clone(); + drop(login); + assert!(original.upgrade().is_none()); + assert_eq!( + accounts.slot(&hub()).unwrap().status(), + VaultStatus::Missing + ); +} + +#[test] +fn a_retained_account_factory_can_sign_in_after_forget_without_reviving_its_old_capture() { + use crate::account::{source::LocalSourceProvider, testing::ScriptedSubjects}; + let login = Login::new(); + let subjects = Arc::new(ScriptedSubjects::for_slot(&login.slot)); + subjects.sign_in(hub(), ACCOUNT_ID, "device"); + sign_in(&login.accounts); + let factory = LocalSourceProvider::new(login.slot.clone(), subjects.clone()); + let original = factory.capture().unwrap(); + login.pool.forget_hub(&hub()).unwrap(); + subjects.evict(&hub()); + assert!(factory.capture().is_err()); + // Settings publication failed: the same configured factory remains installed. + subjects.sign_in(hub(), ACCOUNT_ID, "device"); + sign_in(&login.accounts); + let replacement = factory.capture().unwrap(); + assert!(replacement.check_current().is_ok()); + assert!(original.check_current().is_err()); +} + +struct Login { + directory: tempfile::TempDir, + accounts: Arc, + pool: Arc, + file: CredentialFile, + slot: Arc, + session: Session, + login: CredentialLoginId, + observed: Arc, +} +impl Login { + fn new() -> Self { + let directory = tempfile::tempdir().unwrap(); + let accounts = Arc::new(Accounts::new(directory.path().to_path_buf())); + let (slot, _) = sign_in(&accounts); + let pool = Arc::new(Pool::new( + directory.path().to_path_buf(), + "test", + accounts.clone(), + )); + let file = pool.file(hub().address()).unwrap(); + file.load_or_create().unwrap(); + let session = Session { + account_id: ACCOUNT_ID.into(), + device_id: "device".into(), + access_token: "access-original".into(), + refresh_token: "refresh-original".into(), + access_expires_at_ms: i64::MAX, + refresh_expires_at_ms: i64::MAX, + }; + let login = file.save_session(&session).unwrap(); + let observed = LoginObservation::capture(pool.clone(), hub()).unwrap(); + Self { + directory, + accounts, + pool, + file, + slot, + session, + login, + observed, + } + } + fn cached(&self) -> bool { + CacheFile::new(self.directory.path(), hub().address()) + .unwrap() + .load() + .unwrap() + .is_some() + } + fn retire(&self, erase: bool) -> Result<(), HubError> { + self.observed.retire( + &self.file, + Some((&self.session.account_id, &self.session.device_id)), + Some(self.login), + erase, + ) + } +} + +#[test] +fn a_captured_revocation_erases_only_its_original_native_login_keys_and_cache() { + let login = Login::new(); + assert!(login.cached()); + login.retire(true).unwrap(); + assert!(login.slot.open().is_none()); + assert_eq!(login.slot.status(), VaultStatus::Missing); + assert!(!login.cached()); + assert!(login.file.load().unwrap().is_none()); +} + +#[test] +fn a_captured_revocation_preserves_the_keys_and_cache_of_another_hub() { + let login = Login::new(); + let other = HubId::from(HubAddress::parse("other.test").unwrap()); + let file = login.pool.file(other.address()).unwrap(); + file.load_or_create().unwrap(); + let mut session = login.session.clone(); + session.account_id = "other-account".into(); + let id = file.save_session(&session).unwrap(); + let observed = LoginObservation::capture(login.pool.clone(), other).unwrap(); + observed + .retire( + &file, + Some((&session.account_id, &session.device_id)), + Some(id), + true, + ) + .unwrap(); + assert_eq!(login.slot.status(), VaultStatus::Unlocked); + assert!(login.cached()); + assert_eq!( + login.file.load().unwrap().unwrap().session, + Some(login.session) + ); +} + +#[test] +fn captured_expiry_closes_keys_but_keeps_the_device_key_and_ciphertext_for_sign_in() { + let login = Login::new(); + let public_key = login.file.load().unwrap().unwrap().key.public_key(); + login.retire(false).unwrap(); + assert!(login.slot.open().is_none()); + assert_eq!(login.slot.status(), VaultStatus::Locked); + assert!(login.cached()); + let device = login.file.load().unwrap().unwrap(); + assert_eq!(device.key.public_key(), public_key); + assert!(device.session.is_none()); +} + +#[test] +fn a_late_captured_revocation_cannot_erase_a_new_login_of_the_same_account() { + let login = Login::new(); + let mut replacement = login.session.clone(); + replacement.access_token = "access-replacement".into(); + replacement.refresh_token = "refresh-replacement".into(); + let current = login.file.save_session(&replacement).unwrap(); + assert_ne!(current, login.login); + sign_in(&login.accounts); + let keys = login.slot.open().unwrap(); + assert!(matches!(login.retire(true), Err(HubError::Stale))); + assert_eq!(login.slot.status(), VaultStatus::Unlocked); + assert!(Arc::ptr_eq(&login.slot.open().unwrap(), &keys)); + assert!(login.cached()); + let device = login.file.load().unwrap().unwrap(); + assert_eq!(device.login, Some(current)); + assert_eq!(device.session, Some(replacement)); +} diff --git a/crates/hub-io/src/relay.rs b/crates/hub-io/src/relay.rs new file mode 100644 index 00000000..252e3ba7 --- /dev/null +++ b/crates/hub-io/src/relay.rs @@ -0,0 +1,224 @@ +//! The hub as the first step of an SSH route: [`RelayDialer`] over the hub's relay stream. +//! +//! A dial finds the hub by its address among the hubs this device is signed in to, asks it to +//! open a connection to the destination and hands the transport the two bounded frame channels +//! of a [`RelayStream`]. A task on the adapter's runtime then carries frames between those +//! channels and the hub's stream ([`pump`]); the two runtimes share nothing else. +//! +//! The hub learns the destination address and what it is told to forward, never what is inside +//! the SSH connection: host keys and sign-in are the transport's, checked against the +//! destination. + +mod failure; +mod pump; + +use std::sync::Arc; + +use futures::FutureExt as _; +use futures::future::BoxFuture; +use nocterm_hub_client::RelayTarget; +use nocterm_hubs_model::{HubAddress, HubId}; +use nocterm_session::{RelayDialer, RelayError, RelayStream, Target}; +use tokio::runtime::Handle; + +use crate::account::source::LocalSourceProvider; +use crate::links::HubLinks; +use crate::pool::{Pool, captured::CapturedClient}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +/// Opens relayed streams through the hubs this device is signed in to. +pub struct HubRelay { + handle: Handle, + pool: Arc, +} + +impl HubRelay { + /// A relay that runs on the threads of `links` and uses the hubs `links` signed in to. + /// + /// Once the links are dropped, dials fail with [`RelayError::Closed`]. + pub fn new(links: &HubLinks) -> Self { + let (handle, pool) = links.relay_parts(); + Self { handle, pool } + } +} + +/// Capture capability for an account-bound terminal, composed by the application. +pub(crate) struct RelayCapture { + handle: Handle, + pool: Arc, +} +impl RelayCapture { + pub(crate) fn new(handle: Handle, pool: Arc) -> Self { + Self { handle, pool } + } + pub(crate) fn account( + &self, + provider: &LocalSourceProvider, + source: Arc, + ) -> Result, HubError> { + let native = provider.capture_native()?; + let client = CapturedClient::account(self.pool.clone(), &native, source)?; + Ok(Arc::new(AccountRelay { + handle: self.handle.clone(), + client: Arc::new(client), + })) + } +} +struct AccountRelay { + handle: Handle, + client: Arc, +} +struct CapturedRelay { + handle: Handle, + client: Arc, + hub: String, + target: RelayTarget, + principal: String, +} +impl HubRelay { + fn captured(&self, name: &str, target: &Target) -> Result, RelayError> { + if self.pool.is_stopped() { + return Err(RelayError::Closed); + } + let missing = || RelayError::NotSignedIn { + hub: name.to_owned(), + }; + let hub = HubId::from(HubAddress::parse(name).map_err(|_| missing())?); + let client = CapturedClient::current(self.pool.clone(), hub) + .map_err(|error| failure::without_client(name, error))?; + Ok(Arc::new(CapturedRelay::new( + self.handle.clone(), + Arc::new(client), + name, + target, + ))) + } +} +impl RelayDialer for HubRelay { + fn capture(&self, hub: &str, target: &Target) -> Result, RelayError> { + self.captured(hub, target) + } + fn dial( + &self, + hub: &str, + target: &Target, + ) -> BoxFuture<'static, Result> { + match self.capture(hub, target) { + Ok(captured) => captured.dial(hub, target), + Err(error) => async move { Err(error) }.boxed(), + } + } +} +impl RelayDialer for AccountRelay { + fn capture(&self, hub: &str, target: &Target) -> Result, RelayError> { + let valid = + HubAddress::parse(hub).ok().map(HubId::from).as_ref() == Some(self.client.hub()); + if !valid { + return Err(RelayError::NotSignedIn { + hub: hub.to_owned(), + }); + } + self.client + .check() + .map_err(|error| failure::without_client(hub, error))?; + Ok(Arc::new(CapturedRelay::new( + self.handle.clone(), + self.client.clone(), + hub, + target, + ))) + } + fn dial( + &self, + hub: &str, + target: &Target, + ) -> BoxFuture<'static, Result> { + match self.capture(hub, target) { + Ok(captured) => captured.dial(hub, target), + Err(error) => async move { Err(error) }.boxed(), + } + } +} +impl CapturedRelay { + fn new(handle: Handle, client: Arc, hub: &str, target: &Target) -> Self { + Self { + handle, + client, + hub: hub.to_owned(), + principal: target.user.clone(), + target: RelayTarget { + host: target.host.clone(), + port: target.port, + }, + } + } + fn matches(&self, hub: &str, target: &Target) -> bool { + self.hub == hub + && self.target.host == target.host + && self.target.port == target.port + && self.principal == target.user + } + async fn open(self) -> Result { + let client = self + .client + .fresh() + .await + .map_err(|error| failure::without_client(&self.hub, error))?; + // The SDK and membership are already captured; this never selects B. + self.client + .check() + .map_err(|error| failure::without_client(&self.hub, error))?; + let conn = self + .client + .during(client.relay(&self.target)) + .await + .map_err(|error| failure::without_client(&self.hub, error))? + .map_err(|error| failure::opening(&self.client, &self.hub, error))?; + self.client + .check() + .map_err(|error| failure::without_client(&self.hub, error))?; + let (stream, peer) = RelayStream::pair(); + let (writer, reader) = conn.split(); + let client = self.client.clone(); + self.handle.spawn(async move { + let _ = client.during(pump::carry(peer, writer, reader)).await; + }); + Ok(stream) + } +} +impl RelayDialer for CapturedRelay { + fn capture(&self, hub: &str, target: &Target) -> Result, RelayError> { + if !self.matches(hub, target) { + return Err(RelayError::CaptureUnsupported); + } + self.client + .check() + .map_err(|error| failure::without_client(hub, error))?; + Ok(Arc::new(Self::new( + self.handle.clone(), + self.client.clone(), + hub, + target, + ))) + } + fn dial( + &self, + hub: &str, + target: &Target, + ) -> BoxFuture<'static, Result> { + let captured = match self.capture(hub, target) { + Ok(value) => value, + Err(error) => return async move { Err(error) }.boxed(), + }; + // Recreate only an immutable exact-route view; no account lookup occurs. + drop(captured); + let opening = Self::new(self.handle.clone(), self.client.clone(), hub, target); + // Dropping the dial aborts only opening; an issued stream owns its pump. + crate::task::spawn(&self.handle, async move { opening.open().await }) + .map(|result| result.unwrap_or(Err(RelayError::Closed))) + .boxed() + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/relay/failure.rs b/crates/hub-io/src/relay/failure.rs new file mode 100644 index 00000000..7ad1eb6b --- /dev/null +++ b/crates/hub-io/src/relay/failure.rs @@ -0,0 +1,74 @@ +//! A failed relay in the terms of `nocterm-session`'s `RelayError`. +//! +//! The contract names a refusal by a code and keeps its text for display, so the code decides +//! and the text is never read. The original failure stays the `source()` of what is returned. + +use nocterm_core::SharedCause; +use nocterm_hub_client::{ClientError, RelayError as HubRelayError}; +use nocterm_hub_proto::v2::ErrorCode; +use nocterm_hubs_model::HubError; +use nocterm_session::{RelayError, RelayRefusal}; + +use crate::pool::captured::CapturedClient; + +/// Why the hub named by `name` has no usable client. +pub(super) fn without_client(name: &str, error: HubError) -> RelayError { + match error { + HubError::NotSignedIn | HubError::Revoked => RelayError::NotSignedIn { + hub: name.to_owned(), + }, + other => cannot_reach(other), + } +} + +/// Why the hub did not open a relay to a destination. +/// +/// A device the hub revoked is forgotten on the spot, as for any other call. +pub(super) fn opening(client: &CapturedClient, name: &str, error: HubRelayError) -> RelayError { + classify_opening(name, error, |error| client.failure(error)) +} +fn classify_opening( + name: &str, + error: HubRelayError, + failed: impl FnOnce(ClientError) -> HubError, +) -> RelayError { + if let Some(code) = code_of(&error) + && let Some(refused) = coded(code) + { + return refused; + } + match error { + HubRelayError::Hub(error) => without_client(name, failed(error)), + other => cannot_reach(other), + } +} + +/// The hub's own reason for a failure, when it gave one. +fn code_of(error: &HubRelayError) -> Option { + match error { + HubRelayError::Closed { code, .. } + | HubRelayError::Hub(ClientError::Status { hub_code: code, .. }) => *code, + _ => None, + } +} + +/// What a hub's code says about a relay, if it says anything. +fn coded(code: ErrorCode) -> Option { + match code { + // The contract does not say which rule refused, only that one did. + ErrorCode::RelayDenied => Some(RelayError::Refused { + reason: RelayRefusal::Policy, + }), + ErrorCode::RelayLimit => Some(RelayError::Refused { + reason: RelayRefusal::Limit, + }), + _ => None, + } +} + +fn cannot_reach(cause: impl std::error::Error + Send + Sync + 'static) -> RelayError { + RelayError::Unreachable(SharedCause::new(cause)) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/relay/failure/tests.rs b/crates/hub-io/src/relay/failure/tests.rs new file mode 100644 index 00000000..1775f4b4 --- /dev/null +++ b/crates/hub-io/src/relay/failure/tests.rs @@ -0,0 +1,118 @@ +use crate::classify::classify; +use nocterm_hub_client::{ClientError, RelayError as HubRelayError}; +use nocterm_hubs_model::{HubAddress, HubError}; +use tonic::{Code, Status}; + +use super::*; + +fn open(error: HubRelayError) -> RelayError { + classify_opening("hub.example", error, |error| { + classify(&HubAddress::parse("hub.example").unwrap(), error) + }) +} + +fn closed(code: ErrorCode) -> HubRelayError { + HubRelayError::Closed { + code: Some(code), + message: "text that is never read".into(), + } +} + +#[test] +fn the_hubs_refusals_are_the_sessions_refusals() { + assert_eq!( + open(closed(ErrorCode::RelayDenied)), + RelayError::Refused { + reason: RelayRefusal::Policy + } + ); + assert_eq!( + open(closed(ErrorCode::RelayLimit)), + RelayError::Refused { + reason: RelayRefusal::Limit + } + ); +} + +#[test] +fn a_refusal_in_a_status_is_a_refusal_too() { + let mut status = Status::new(Code::PermissionDenied, "no"); + status.metadata_mut().insert( + "x-nocterm-error", + "ERROR_CODE_RELAY_DENIED".parse().unwrap(), + ); + assert_eq!( + open(HubRelayError::from(status)), + RelayError::Refused { + reason: RelayRefusal::Policy + } + ); +} + +#[test] +fn a_hub_without_relays_is_unreachable_and_keeps_its_cause() { + let RelayError::Unreachable(cause) = open(HubRelayError::Unsupported) else { + panic!("expected Unreachable"); + }; + assert!(matches!( + cause.downcast_ref::(), + Some(HubRelayError::Unsupported) + )); +} + +#[test] +fn a_broken_stream_is_unreachable() { + for error in [ + HubRelayError::Interrupted, + HubRelayError::UnexpectedFrame, + HubRelayError::BadPeerAddress, + closed(ErrorCode::Unspecified), + ] { + assert!(matches!(open(error), RelayError::Unreachable(_))); + } +} + +#[test] +fn tokens_the_hub_no_longer_accepts_mean_not_signed_in() { + let rejected = HubRelayError::from(Status::new(Code::Unauthenticated, "expired")); + assert_eq!( + open(rejected), + RelayError::NotSignedIn { + hub: "hub.example".into() + } + ); + assert_eq!( + open(HubRelayError::Hub(ClientError::NotSignedIn)), + RelayError::NotSignedIn { + hub: "hub.example".into() + } + ); +} + +#[test] +fn a_hub_that_does_not_answer_is_unreachable_and_keeps_its_cause() { + let down = HubRelayError::from(Status::new(Code::Unavailable, "down")); + let RelayError::Unreachable(cause) = open(down) else { + panic!("expected Unreachable"); + }; + assert!(matches!( + cause.downcast_ref::(), + Some(HubError::Unreachable { .. }) + )); +} + +#[test] +fn a_hub_without_a_client_is_not_signed_in_unless_it_failed_otherwise() { + assert_eq!( + without_client("h", HubError::NotSignedIn), + RelayError::NotSignedIn { hub: "h".into() } + ); + assert_eq!( + without_client("h", HubError::Revoked), + RelayError::NotSignedIn { hub: "h".into() } + ); + assert!(matches!( + without_client("h", HubError::NotFound), + RelayError::Unreachable(_) + )); +} diff --git a/crates/hub-io/src/relay/pump.rs b/crates/hub-io/src/relay/pump.rs new file mode 100644 index 00000000..bc7ca553 --- /dev/null +++ b/crates/hub-io/src/relay/pump.rs @@ -0,0 +1,132 @@ +//! Carries frames between the transport's bounded channels and the hub's relay stream. +//! +//! Every link of the chain is bounded, so a side that stops reading stops the other instead +//! of making this side buffer: +//! +//! - towards the hub, the transport's channel queues [`nocterm_session::RELAY_FRAMES`] frames, +//! the pump holds one, the hub client queues its own bounded number and then the HTTP/2 +//! window stops the rest; +//! - towards the transport, the HTTP/2 window stops the hub while the pump holds one frame +//! and the transport's channel queues [`nocterm_session::RELAY_FRAMES`]. +//! +//! The directions end independently. The transport dropping its sender is an end of file for +//! the target, which may still answer; the target closing is an end of file for the transport, +//! which may still send. A failure of the hub's stream or a dropped channel ends both: the +//! pump returns and releases every end it holds, which aborts the hub's stream and tells the +//! transport through its channels. + +use std::future::Future; +use std::pin::pin; + +use nocterm_hub_client::{RelayError, RelayReader, RelayWriter}; +use nocterm_session::{RELAY_FRAME_BYTES, RelayPeer, RelayReceiver, RelaySender}; + +/// The way towards the target: what the transport sends. +pub(super) trait Uplink: Send { + /// Waits while the hub still holds as many frames as it may, then queues `data`. + fn send(&mut self, data: &[u8]) -> impl Future> + Send; + /// Tells the target that nothing more follows; it may still answer. + fn finish(self) -> impl Future + Send; +} + +/// The way back: what the target sends. +pub(super) trait Downlink: Send { + /// The next bytes of the target; `Ok(None)` when it closed cleanly. + fn next(&mut self) -> impl Future>, RelayError>> + Send; +} + +impl Uplink for RelayWriter { + async fn send(&mut self, data: &[u8]) -> Result<(), RelayError> { + RelayWriter::send(self, data).await + } + + async fn finish(self) { + // The stream is going away either way; there is nobody to tell. + let _ = RelayWriter::finish(self).await; + } +} + +impl Downlink for RelayReader { + async fn next(&mut self) -> Result>, RelayError> { + self.recv().await + } +} + +/// How a direction ended. +#[derive(Clone, Copy, PartialEq, Eq)] +enum Ended { + /// The direction reached its end; the other may go on. + Finished, + /// The stream or the other end of this direction is gone, so the whole relay is. + Broken, +} + +/// Moves frames both ways until both directions end or one breaks. +pub(super) async fn carry(peer: RelayPeer, up: U, down: D) { + let (from_transport, to_transport) = peer.into_parts(); + let mut upload = pin!(upload(from_transport, up)); + let mut download = pin!(download(down, to_transport)); + let (mut uploading, mut downloading) = (true, true); + while uploading || downloading { + let ended = tokio::select! { + ended = &mut upload, if uploading => { + uploading = false; + ended + } + ended = &mut download, if downloading => { + downloading = false; + ended + } + }; + if ended == Ended::Broken { + return; + } + } +} + +/// Moves what the transport sends to the hub, one frame at a time. +async fn upload(from_transport: RelayReceiver, mut up: U) -> Ended { + while let Ok(frame) = from_transport.recv().await { + // An empty frame carries nothing, and an empty data frame is not one the hub accepts. + if frame.is_empty() { + continue; + } + if let Err(error) = up.send(&frame).await { + tracing::debug!(%error, "the relay stream refused data"); + return Ended::Broken; + } + } + // The transport dropped its sender: end of file for the target, after what was queued. + up.finish().await; + Ended::Finished +} + +/// Moves what the target sends to the transport, in frames the transport accepts. +/// +/// Ends as soon as the transport has dropped its receiver, even while the target is silent. +async fn download(mut down: D, to_transport: RelaySender) -> Ended { + loop { + let next = tokio::select! { + next = down.next() => next, + () = to_transport.closed() => return Ended::Broken, + }; + match next { + Ok(Some(data)) => { + for frame in data.chunks(RELAY_FRAME_BYTES) { + if to_transport.send(frame.to_vec()).await.is_err() { + return Ended::Broken; + } + } + } + // Dropping the sender is the end of file for the transport. + Ok(None) => return Ended::Finished, + Err(error) => { + tracing::debug!(%error, "the relay stream ended"); + return Ended::Broken; + } + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/relay/pump/tests.rs b/crates/hub-io/src/relay/pump/tests.rs new file mode 100644 index 00000000..b6779400 --- /dev/null +++ b/crates/hub-io/src/relay/pump/tests.rs @@ -0,0 +1,238 @@ +use std::sync::Arc; +use std::sync::atomic::{AtomicUsize, Ordering}; +use std::time::Duration; + +use nocterm_hub_client::RelayError; +use nocterm_session::{RELAY_FRAME_BYTES, RELAY_FRAMES, RelayReceiver, RelaySender, RelayStream}; +use tokio::sync::{Notify, Semaphore, mpsc, oneshot}; +use tokio::task::JoinHandle; +use tokio::time::timeout; + +use super::{Downlink, Uplink, carry}; + +const PATIENCE: Duration = Duration::from_secs(5); + +/// What the hub's stream was told, in order. +#[derive(Debug, PartialEq, Eq)] +enum Told { + Data(Vec), + Finished, +} + +/// A hub stream going towards the target. `gate` holds sends back like a slow target does; +/// `gone` resolves when the stream is dropped, which aborts a real one. +struct FakeUp { + told: mpsc::UnboundedSender, + gate: Option>, + entered: Arc, + _gone: oneshot::Sender<()>, +} + +impl Uplink for FakeUp { + async fn send(&mut self, data: &[u8]) -> Result<(), RelayError> { + self.entered.notify_one(); + if let Some(gate) = &self.gate { + gate.acquire().await.unwrap().forget(); + } + self.told + .send(Told::Data(data.to_vec())) + .map_err(|_| RelayError::Interrupted) + } + + async fn finish(self) { + let _ = self.told.send(Told::Finished); + } +} + +/// A hub stream coming back from the target, fed by the test. +struct FakeDown { + script: mpsc::UnboundedReceiver>, RelayError>>, + taken: Arc, +} + +impl Downlink for FakeDown { + async fn next(&mut self) -> Result>, RelayError> { + let next = self + .script + .recv() + .await + .unwrap_or(Err(RelayError::Interrupted)); + self.taken.fetch_add(1, Ordering::SeqCst); + next + } +} + +struct Relay { + /// The transport's ends. + to_hub: RelaySender, + from_hub: RelayReceiver, + told: mpsc::UnboundedReceiver, + entered: Arc, + gone: oneshot::Receiver<()>, + answers: mpsc::UnboundedSender>, RelayError>>, + taken: Arc, + pump: JoinHandle<()>, +} + +fn relay(gate: Option>) -> Relay { + let (stream, peer) = RelayStream::pair(); + let (told, told_rx) = mpsc::unbounded_channel(); + let (answers, script) = mpsc::unbounded_channel(); + let (gone_tx, gone) = oneshot::channel(); + let entered = Arc::new(Notify::new()); + let taken = Arc::new(AtomicUsize::new(0)); + let up = FakeUp { + told, + gate, + entered: entered.clone(), + _gone: gone_tx, + }; + let down = FakeDown { + script, + taken: taken.clone(), + }; + let (to_hub, from_hub) = stream.into_parts(); + Relay { + to_hub, + from_hub, + told: told_rx, + entered, + gone, + answers, + taken, + pump: tokio::spawn(carry(peer, up, down)), + } +} + +async fn ended(pump: JoinHandle<()>) { + timeout(PATIENCE, pump) + .await + .expect("the relay did not end") + .unwrap(); +} + +#[tokio::test] +async fn frames_reach_the_hub_in_order_and_an_end_of_file_is_a_half_close() { + let mut relay = relay(None); + relay.to_hub.send(b"one".to_vec()).await.unwrap(); + relay.to_hub.send(b"two".to_vec()).await.unwrap(); + drop(relay.to_hub); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"one".to_vec()))); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"two".to_vec()))); + assert_eq!(relay.told.recv().await, Some(Told::Finished)); + // The target may still answer after the half-close. + relay.answers.send(Ok(Some(b"late".to_vec()))).unwrap(); + assert_eq!(relay.from_hub.recv().await.unwrap(), b"late"); +} + +#[tokio::test] +async fn an_empty_frame_from_the_transport_is_not_sent_to_the_hub() { + let mut relay = relay(None); + relay.to_hub.send(Vec::new()).await.unwrap(); + relay.to_hub.send(b"one".to_vec()).await.unwrap(); + relay.to_hub.send(Vec::new()).await.unwrap(); + drop(relay.to_hub); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"one".to_vec()))); + assert_eq!(relay.told.recv().await, Some(Told::Finished)); +} + +#[tokio::test] +async fn data_from_the_target_comes_in_frames_the_transport_accepts() { + let relay = relay(None); + let data: Vec = (0..(3 * RELAY_FRAME_BYTES + 1)) + .map(|index| (index % 251) as u8) + .collect(); + relay.answers.send(Ok(Some(data.clone()))).unwrap(); + let mut received = Vec::new(); + while received.len() < data.len() { + let frame = timeout(PATIENCE, relay.from_hub.recv()) + .await + .unwrap() + .unwrap(); + assert!(frame.len() <= RELAY_FRAME_BYTES); + received.extend(frame); + } + assert_eq!(received, data); +} + +#[tokio::test] +async fn a_clean_close_by_the_target_is_end_of_file_after_the_queued_data() { + let mut relay = relay(None); + relay.answers.send(Ok(Some(b"bye".to_vec()))).unwrap(); + relay.answers.send(Ok(None)).unwrap(); + assert_eq!(relay.from_hub.recv().await.unwrap(), b"bye"); + assert!(relay.from_hub.recv().await.is_err(), "end of file"); + // The transport may still send: its direction has not ended. + relay.to_hub.send(b"still".to_vec()).await.unwrap(); + assert_eq!(relay.told.recv().await, Some(Told::Data(b"still".to_vec()))); +} + +#[tokio::test] +async fn a_failed_hub_stream_closes_both_directions() { + let relay = relay(None); + relay.answers.send(Err(RelayError::Interrupted)).unwrap(); + ended(relay.pump).await; + assert!(relay.from_hub.recv().await.is_err()); + assert!(relay.to_hub.send(b"x".to_vec()).await.is_err()); +} + +#[tokio::test] +async fn dropping_both_transport_ends_releases_the_hub_stream_while_the_target_is_silent() { + let relay = relay(None); + drop((relay.to_hub, relay.from_hub)); + ended(relay.pump).await; + // The sender of `gone` was dropped with the stream: this resolves with an error. + assert!(timeout(PATIENCE, relay.gone).await.unwrap().is_err()); +} + +#[tokio::test] +async fn a_transport_that_stops_reading_releases_the_hub_stream_while_the_target_is_silent() { + let relay = relay(None); + drop(relay.from_hub); + ended(relay.pump).await; + assert!(relay.to_hub.send(b"x".to_vec()).await.is_err()); +} + +#[tokio::test] +async fn a_target_that_stops_reading_stalls_the_transport_at_the_bounded_queue() { + let gate = Arc::new(Semaphore::new(0)); + let mut relay = relay(Some(gate.clone())); + relay.to_hub.send(vec![0]).await.unwrap(); + timeout(PATIENCE, relay.entered.notified()).await.unwrap(); + // The pump holds that frame; the channel takes `RELAY_FRAMES` more and no further. + let mut queued = 0; + while relay.to_hub.try_send(vec![1]).is_ok() { + queued += 1; + } + assert_eq!(queued, RELAY_FRAMES); + // Letting the target read again moves everything, in order. + gate.add_permits(RELAY_FRAMES + 1); + assert_eq!(relay.told.recv().await, Some(Told::Data(vec![0]))); + for _ in 0..RELAY_FRAMES { + assert_eq!(relay.told.recv().await, Some(Told::Data(vec![1]))); + } +} + +#[tokio::test] +async fn a_transport_that_stops_reading_stalls_the_hub_at_the_bounded_queue() { + let relay = relay(None); + for _ in 0..(10 * RELAY_FRAMES) { + relay.answers.send(Ok(Some(vec![7]))).unwrap(); + } + // Wait until the pump has taken what it can. + let mut last = 0; + loop { + tokio::time::sleep(Duration::from_millis(50)).await; + let now = relay.taken.load(Ordering::SeqCst); + if now == last { + break; + } + last = now; + } + // `RELAY_FRAMES` in the channel, one held by the pump, and the one it asked for next. + assert!( + last <= RELAY_FRAMES + 2, + "the pump took {last} frames from a hub the transport does not read" + ); + assert!(last >= RELAY_FRAMES, "the channel was not filled: {last}"); +} diff --git a/crates/hub-io/src/relay/tests.rs b/crates/hub-io/src/relay/tests.rs new file mode 100644 index 00000000..3f863bcf --- /dev/null +++ b/crates/hub-io/src/relay/tests.rs @@ -0,0 +1,51 @@ +use futures::executor::block_on; +use nocterm_session::{RelayDialer as _, RelayError, Target}; + +use super::HubRelay; +use crate::{HubLinks, HubLinksConfig}; + +fn target() -> Target { + Target { + host: "db.internal".into(), + port: 22, + user: "deploy".into(), + } +} + +fn links(dir: &tempfile::TempDir) -> std::sync::Arc { + HubLinks::start(HubLinksConfig { + credentials_dir: dir.path().join("hubs"), + device_name: "relay test".into(), + }) + .unwrap() +} + +#[test] +fn a_hub_this_device_is_not_signed_in_to_is_not_dialled() { + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let relay = HubRelay::new(&links); + for hub in [ + "hub.example", + "https://Hub.Example:7443/", + "not a hub address", + "", + ] { + let dialled = block_on(relay.dial(hub, &target())); + assert_eq!( + dialled.unwrap_err(), + RelayError::NotSignedIn { hub: hub.into() }, + "{hub:?}" + ); + } +} + +#[test] +fn dialling_after_the_links_stopped_finds_the_relay_closed() { + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let relay = HubRelay::new(&links); + drop(links); + let dialled = block_on(relay.dial("hub.example", &target())); + assert_eq!(dialled.unwrap_err(), RelayError::Closed); +} diff --git a/crates/hub-io/src/replica.rs b/crates/hub-io/src/replica.rs new file mode 100644 index 00000000..1b996681 --- /dev/null +++ b/crates/hub-io/src/replica.rs @@ -0,0 +1,219 @@ +//! The saved profiles on the account hub, one sealed object per profile. +//! +//! [`HubProfileReplica`] implements the replica port of the connection store over the +//! account's `profiles` collection. It works only while the vault is unlocked, because the keys +//! that seal profiles are the vault's; locked or unreachable is "unavailable", never an error. + +mod watch; +mod wire; + +use futures::StreamExt as _; +use futures::future::BoxFuture; +use futures::stream::BoxStream; +use nocterm_connections_model::profiles::{Profile, ProfileId}; +use nocterm_connections_model::replica::{ + ProfileReplica, ReplicaCursor, ReplicaError, ReplicaPage, ReplicaVersion, +}; +use nocterm_core::SharedCause; +use nocterm_hubs_model::HubError; +use tokio::runtime::Handle; +use tokio::sync::mpsc; + +use self::wire::{KIND, Positions}; +use crate::account::operations::{BoundOperations, OpenedWork}; +use crate::classify::unreachable; +use crate::store::{Located, OpError, PROFILES_KIND, Store}; +use crate::task::spawn; + +pub struct HubProfileReplica { + handle: Handle, + bound: BoundOperations, +} + +/// A failure of a store call as the replica reports it. `id` names the profile a write was +/// about. +fn replica_error(error: OpError, id: Option) -> ReplicaError { + match (error, id) { + (OpError::Hub(HubError::NotSignedIn | HubError::Stale), _) => ReplicaError::Stale, + (OpError::Conflict, Some(id)) => ReplicaError::Conflict { id }, + (OpError::Hub(HubError::NotFound), Some(id)) => ReplicaError::Conflict { id }, + (OpError::Hub(HubError::PermissionDenied), Some(id)) => ReplicaError::Forbidden { id }, + (OpError::Hub(error), _) if unreachable(&error) => ReplicaError::Unavailable, + (error, _) => ReplicaError::Other(SharedCause::new(error)), + } +} + +impl HubProfileReplica { + pub(crate) fn new(handle: Handle, bound: BoundOperations) -> Self { + Self { handle, bound } + } + + fn run( + &self, + work: impl FnOnce(OpenedWork) -> F + Send + 'static, + ) -> BoxFuture<'static, Result> + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let admitted = match self.bound.capture() { + Ok(admitted) => admitted, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let source = admitted.source().clone(); + let returned = source.clone(); + let task = spawn(&self.handle, async move { + source + .during(work(admitted)) + .await + .map_err(native_failure)? + }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(ReplicaError::Unavailable)); + returned.check().map_err(native_failure)?; + result + }) + } +} + +fn native_failure(error: HubError) -> ReplicaError { + match error { + HubError::KeysLocked => ReplicaError::Locked, + HubError::NotSignedIn | HubError::Stale => ReplicaError::Stale, + other => ReplicaError::Other(SharedCause::new(other)), + } +} + +async fn changes( + work: OpenedWork, + after: Option, +) -> Result { + let store = work.store(); + let mut positions = Positions::parse(after.as_ref()); + let Some(at) = store + .find(PROFILES_KIND) + .await + .map_err(|error| replica_error(error, None))? + else { + return Ok(ReplicaPage { + changes: Vec::new(), + cursor: positions.cursor(), + more: false, + }); + }; + let from = positions.get(at.id()); + let batch = store + .changes(&at, from) + .await + .map_err(|error| replica_error(error, None))?; + let mut seen = Vec::new(); + let mut position = from; + for object in &batch.changes { + position = position.max(object.stream_version); + if let Some(profile) = wire::read(at.id(), at.key(), object)? { + seen.push(profile); + } + } + if !batch.more { + position = position.max(batch.head); + } + positions.set(at.id(), position); + Ok(ReplicaPage { + changes: seen, + cursor: positions.cursor(), + more: batch.more, + }) +} + +/// The collection a write goes to: the account's own, created by the first write. +async fn target(store: &Store, id: ProfileId, create: bool) -> Result { + let located = if create { + store.ensure(PROFILES_KIND).await.map(Some) + } else { + store.find(PROFILES_KIND).await + } + .map_err(|error| replica_error(error, None))?; + located.ok_or(ReplicaError::Conflict { id }) +} + +async fn put( + work: OpenedWork, + profile: Profile, + expected: Option, +) -> Result { + let id = profile.id; + let store = work.store(); + let plaintext = + wire::encode(&profile).map_err(|error| ReplicaError::Other(SharedCause::new(error)))?; + let at = target(&store, id, true).await?; + let stored = store + .put( + &at, + KIND, + &id.to_string(), + &plaintext, + expected.map_or(0, ReplicaVersion::get), + ) + .await + .map_err(|error| replica_error(error, Some(id)))?; + Ok(ReplicaVersion::new(stored.version)) +} + +async fn delete( + work: OpenedWork, + id: ProfileId, + expected: ReplicaVersion, +) -> Result { + let store = work.store(); + let at = target(&store, id, false).await?; + let stored = store + .remove(&at, &id.to_string(), expected.get()) + .await + .map_err(|error| replica_error(error, Some(id)))?; + Ok(ReplicaVersion::new(stored.version)) +} + +impl ProfileReplica for HubProfileReplica { + fn changes( + &self, + after: Option, + ) -> BoxFuture<'static, Result> { + self.run(move |work| changes(work, after)) + } + + fn put( + &self, + profile: Profile, + expected: Option, + ) -> BoxFuture<'static, Result> { + self.run(move |work| put(work, profile, expected)) + } + + fn delete( + &self, + id: ProfileId, + expected: ReplicaVersion, + ) -> BoxFuture<'static, Result> { + self.run(move |work| delete(work, id, expected)) + } + + fn watch(&self) -> BoxStream<'static, ()> { + let work = match self.bound.capture() { + Ok(work) => work, + Err(_) => return futures::stream::empty().boxed(), + }; + let (ticks, receiver) = mpsc::channel(1); + self.handle.spawn(watch::follow(work, ticks)); + let source = self.bound.source().clone(); + futures::stream::unfold((receiver, source), |(mut receiver, source)| async move { + source.check().ok()?; + receiver.recv().await?; + source.check().ok()?; + Some(((), (receiver, source))) + }) + .boxed() + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/replica/tests.rs b/crates/hub-io/src/replica/tests.rs new file mode 100644 index 00000000..ce79ed15 --- /dev/null +++ b/crates/hub-io/src/replica/tests.rs @@ -0,0 +1,152 @@ +use std::sync::Arc; +use std::time::Duration; + +use nocterm_connections_model::replica::RemoteBody; +use nocterm_connections_model::test_support::profile_id_for; + +use super::*; +use crate::account::{ + Slot, + operations::AccountOperations, + testing::{native_operations, second_device, signed_in}, +}; +use crate::store::StoreOps as _; +use crate::store::memory::MemoryHub; +mod source_binding; + +fn profile(name: &str) -> Profile { + serde_json::from_value(serde_json::json!({ + "id": profile_id_for(name), + "name": name, + "host": "example.org", + "user": "ann", + })) + .unwrap() +} + +struct Device { + replica: HubProfileReplica, + slot: Arc, + _dir: tempfile::TempDir, + _operations: Arc, +} + +/// A signed-in device of the account. +fn device(hub: &Arc) -> (Device, nocterm_hub_proto::v2::Keyset) { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + let operations = native_operations(&slot, hub.clone()); + let source = operations.current_opened_source().unwrap(); + let replica = HubProfileReplica::new(Handle::current(), operations.bind(source).unwrap()); + ( + Device { + replica, + slot, + _dir: dir, + _operations: operations, + }, + record, + ) +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_profile_put_on_one_device_arrives_on_another() { + let hub = Arc::new(MemoryHub::default()); + let (first, record) = device(&hub); + let web = profile("web"); + let version = first.replica.put(web.clone(), None).await.unwrap(); + assert_eq!(version, ReplicaVersion::new(1)); + + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot) = second_device(dir.path(), &record); + let operations = native_operations(&slot, hub.clone()); + let source = operations.current_opened_source().unwrap(); + let second = HubProfileReplica::new(Handle::current(), operations.bind(source).unwrap()); + let page = second.changes(None).await.unwrap(); + assert!(!page.more); + assert_eq!(page.changes.len(), 1); + assert_eq!(page.changes[0].body, RemoteBody::Present(Box::new(web))); + // Reading again from the returned cursor finds nothing new. + let next = second.changes(Some(page.cursor)).await.unwrap(); + assert!(next.changes.is_empty()); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_stale_write_conflicts_and_a_delete_is_a_tombstone() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let web = profile("web"); + let v1 = device.replica.put(web.clone(), None).await.unwrap(); + let again = device.replica.put(web.clone(), None).await; + assert!(matches!(again, Err(ReplicaError::Conflict { id }) if id == web.id)); + let v2 = device.replica.put(web.clone(), Some(v1)).await.unwrap(); + let v3 = device.replica.delete(web.id, v2).await.unwrap(); + assert!(v3 > v2); + let page = device.replica.changes(None).await.unwrap(); + assert_eq!(page.changes.len(), 1); + assert_eq!(page.changes[0].body, RemoteBody::Deleted); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_unreachable_hub_is_unavailable_not_failed() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + hub.set_offline(true); + assert!(matches!( + device.replica.changes(None).await, + Err(ReplicaError::Unavailable) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn closing_account_keys_makes_the_original_replica_stale() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + device.slot.lock(); + assert!(matches!( + device.replica.changes(None).await, + Err(ReplicaError::Stale) + )); + assert!(matches!( + device.replica.put(profile("web"), None).await, + Err(ReplicaError::Stale) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_profile_the_hub_altered_stops_the_feed_with_an_error() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let web = profile("web"); + device.replica.put(web.clone(), None).await.unwrap(); + let collection = hub.collections(PROFILES_KIND).await.unwrap().remove(0); + hub.tamper(&collection.collection_id, &web.id.to_string(), vec![5; 64]); + assert!(matches!( + device.replica.changes(None).await, + Err(ReplicaError::Other(_)) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn an_old_watch_ends_after_lock_and_a_fresh_capture_watches_after_unlock() { + let hub = Arc::new(MemoryHub::default()); + let (device, record) = device(&hub); + device.slot.lock(); + let mut old = device.replica.watch(); + assert_eq!(old.next().await, None); + let keys = crate::account::testing::open( + &record, + &crate::account::testing::unlock(crate::account::testing::PASSWORD), + ); + device.slot.unlocked(keys); + assert_eq!(old.next().await, None); + let source = device._operations.current_opened_source().unwrap(); + let fresh = HubProfileReplica::new(Handle::current(), device._operations.bind(source).unwrap()); + let mut ticks = fresh.watch(); + assert_eq!( + tokio::time::timeout(Duration::from_secs(5), ticks.next()) + .await + .unwrap(), + Some(()) + ); +} diff --git a/crates/hub-io/src/replica/tests/source_binding.rs b/crates/hub-io/src/replica/tests/source_binding.rs new file mode 100644 index 00000000..03850c91 --- /dev/null +++ b/crates/hub-io/src/replica/tests/source_binding.rs @@ -0,0 +1,118 @@ +use super::*; +use crate::account::testing::NativeAccounts; +use crate::store::memory::StoreBoundary; + +fn captured(fixture: &NativeAccounts) -> HubProfileReplica { + HubProfileReplica::new( + Handle::current(), + fixture.operations().bind(fixture.source()).unwrap(), + ) +} + +#[tokio::test] +async fn queued_replica_mutations_never_choose_the_replacement_account() { + let fixture = NativeAccounts::new(); + let replica = captured(&fixture); + let item = profile("A"); + let put = replica.put(item.clone(), None); + let delete = replica.delete(item.id, ReplicaVersion::new(1)); + fixture.login(1); + assert!(matches!(put.await, Err(ReplicaError::Stale))); + assert!(matches!(delete.await, Err(ReplicaError::Stale))); + assert!( + fixture + .hub(0) + .collections(PROFILES_KIND) + .await + .unwrap() + .is_empty() + ); + assert!( + fixture + .hub(1) + .collections(PROFILES_KIND) + .await + .unwrap() + .is_empty() + ); +} + +#[tokio::test] +async fn late_replica_changes_publish_no_page_for_b() { + let fixture = NativeAccounts::new(); + let a = captured(&fixture); + a.put(profile("A"), None).await.unwrap(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let old = a.changes(None); + entered.wait().await; + fixture.login(1); + let b = captured(&fixture); + assert!(b.changes(None).await.unwrap().changes.is_empty()); + release.release(); + assert!(matches!(old.await, Err(ReplicaError::Stale))); +} + +#[tokio::test] +async fn a_finished_replica_page_is_rechecked_when_its_caller_first_polls() { + let fixture = NativeAccounts::new(); + let replica = captured(&fixture); + replica.put(profile("A"), None).await.unwrap(); + let calls = fixture.hub(0).change_calls(); + let old = replica.changes(None); + while fixture.hub(0).change_calls() == calls { + tokio::task::yield_now().await; + } + fixture.login(1); + assert!(matches!(old.await, Err(ReplicaError::Stale))); +} + +#[tokio::test] +async fn an_old_replica_stays_stale_after_same_account_login_and_a_b_a() { + let fixture = NativeAccounts::new(); + let old = captured(&fixture); + fixture.login(0); + assert!(matches!(old.changes(None).await, Err(ReplicaError::Stale))); + let next = captured(&fixture); + fixture.login(1); + fixture.login(0); + assert!(matches!( + next.put(profile("old"), None).await, + Err(ReplicaError::Stale) + )); + assert!( + captured(&fixture) + .changes(None) + .await + .unwrap() + .changes + .is_empty() + ); +} + +#[tokio::test] +async fn a_queued_watch_tick_cannot_outlive_its_original_account_capture() { + let fixture = NativeAccounts::new(); + let replica = captured(&fixture); + replica.put(profile("A"), None).await.unwrap(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::WatchReady); + let mut old = replica.watch(); + entered.wait().await; + release.release(); + tokio::task::yield_now().await; + fixture.login(1); + assert_eq!(old.next().await, None); +} + +#[tokio::test] +async fn a_valid_source_from_another_hub_cannot_select_this_accounts_ports() { + let a = NativeAccounts::new(); + let b = NativeAccounts::at(crate::account::testing::hub_for("other.test")); + assert!(a.source().check_current().is_ok()); + assert!(b.source().check_current().is_ok()); + assert!(matches!( + a.operations().bind(b.source()), + Err(HubError::Stale) + )); + captured(&a).put(profile("A"), None).await.unwrap(); + assert!(captured(&b).changes(None).await.unwrap().changes.is_empty()); +} diff --git a/crates/hub-io/src/replica/watch.rs b/crates/hub-io/src/replica/watch.rs new file mode 100644 index 00000000..57f85c3c --- /dev/null +++ b/crates/hub-io/src/replica/watch.rs @@ -0,0 +1,55 @@ +//! Change hints retain one original native source and never reopen dynamic Slot. +use crate::{account::operations::OpenedWork, store::PROFILES_KIND}; +use futures::StreamExt as _; +use std::time::Duration; +use tokio::sync::mpsc; + +const RETRY: Duration = Duration::from_secs(20); + +pub(super) async fn follow(work: OpenedWork, ticks: mpsc::Sender<()>) { + let source = work.source().clone(); + let Ok(slot) = source.native().slot() else { + return; + }; + let mut notice = slot.notice().subscribe(); + loop { + if source.check().is_err() { + return; + } + notice.borrow_and_update(); + tokio::select! { + () = ticks.closed() => return, + changed = notice.changed() => { + if changed.is_err() || source.check().is_err() { return; } + // The keys changed: restart the original feed with them. + }, + result = source.during(once(&work, &ticks)) => { + if result.is_err() { return; } + } + } + } +} + +async fn once(work: &OpenedWork, ticks: &mpsc::Sender<()>) { + let store = work.store(); + let collections: Vec = match store.find(PROFILES_KIND).await { + Ok(found) => found.iter().map(|at| at.id().to_owned()).collect(), + Err(_) => Vec::new(), + }; + if collections.is_empty() { + let _ = work.source().publish(|| ticks.try_send(())); + tokio::time::sleep(RETRY).await; + return; + } + let Ok(mut hints) = work.ops().watch(collections).await else { + tokio::time::sleep(RETRY).await; + return; + }; + let _ = work.source().publish(|| ticks.try_send(())); + while hints.next().await.is_some() { + if work.source().publish(|| ticks.try_send(())).is_err() { + return; + } + } + tokio::time::sleep(RETRY).await; +} diff --git a/crates/hub-io/src/replica/wire.rs b/crates/hub-io/src/replica/wire.rs new file mode 100644 index 00000000..3daf3622 --- /dev/null +++ b/crates/hub-io/src/replica/wire.rs @@ -0,0 +1,112 @@ +//! What a profile object holds and how an object of the feed is read. +//! +//! The plaintext is `{"v":1,"profile":…}`. An object this client cannot read (a newer format +//! version, another kind, or a profile under another id) is reported as unrecognized so the +//! reconciliation leaves it alone. An object that does not authenticate is the hub's +//! forgery: it is an error and the feed does not move past it. + +use std::str::FromStr as _; + +use nocterm_connections_model::profiles::{Profile, ProfileId}; +use nocterm_connections_model::replica::{ + RemoteBody, RemoteProfile, ReplicaCursor, ReplicaError, ReplicaVersion, +}; +use nocterm_core::SharedCause; +use nocterm_hub_proto::v2; +use nocterm_keys::CollectionKey; +use serde::{Deserialize, Serialize}; + +/// The kind of the objects that hold profiles. +pub(super) const KIND: &str = "profile"; +const FORMAT: u32 = 1; + +#[derive(Serialize)] +struct Write<'a> { + v: u32, + profile: &'a Profile, +} + +#[derive(Deserialize)] +struct Head { + v: u32, +} + +#[derive(Deserialize)] +struct Read { + profile: Profile, +} + +pub(super) fn encode(profile: &Profile) -> Result, serde_json::Error> { + serde_json::to_vec(&Write { v: FORMAT, profile }) +} + +/// One object of the feed as the replica reports it; `None` for an object that is not a +/// profile at all (its id is not a profile id), which is neither shown nor touched. +pub(super) fn read( + collection_id: &str, + key: &CollectionKey, + object: &v2::StoredObject, +) -> Result, ReplicaError> { + let Ok(id) = ProfileId::from_str(&object.object_id) else { + return Ok(None); + }; + let body = if object.deleted { + RemoteBody::Deleted + } else if object.kind != KIND { + RemoteBody::Unrecognized + } else { + let plain = key + .open_object( + collection_id, + &object.object_id, + &object.kind, + &object.ciphertext, + ) + .map_err(|error| ReplicaError::Other(SharedCause::new(error)))?; + body_of(id, &plain) + }; + Ok(Some(RemoteProfile { + id, + version: ReplicaVersion::new(object.version), + body, + })) +} + +fn body_of(id: ProfileId, plaintext: &[u8]) -> RemoteBody { + match serde_json::from_slice::(plaintext) { + Ok(Head { v: FORMAT }) => {} + _ => return RemoteBody::Unrecognized, + } + match serde_json::from_slice::(plaintext) { + Ok(Read { profile }) if profile.id == id => RemoteBody::Present(Box::new(profile)), + _ => RemoteBody::Unrecognized, + } +} + +/// The position in the feed of every collection read: JSON, so it stays opaque to the +/// reconciliation and can grow to several collections. +#[derive(Default, Serialize, Deserialize)] +pub(super) struct Positions(std::collections::BTreeMap); + +impl Positions { + pub(super) fn parse(cursor: Option<&ReplicaCursor>) -> Self { + cursor + .and_then(|cursor| serde_json::from_str(cursor.as_str()).ok()) + .unwrap_or_default() + } + + pub(super) fn get(&self, collection_id: &str) -> u64 { + self.0.get(collection_id).copied().unwrap_or(0) + } + + pub(super) fn set(&mut self, collection_id: &str, position: u64) { + self.0.insert(collection_id.to_owned(), position); + } + + pub(super) fn cursor(&self) -> ReplicaCursor { + ReplicaCursor::new(serde_json::to_string(self).unwrap_or_else(|_| "{}".to_owned())) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/replica/wire/tests.rs b/crates/hub-io/src/replica/wire/tests.rs new file mode 100644 index 00000000..5af66bca --- /dev/null +++ b/crates/hub-io/src/replica/wire/tests.rs @@ -0,0 +1,196 @@ +use nocterm_connections_model::test_support::profile_id_for; +use nocterm_keys::OpenError; + +use super::*; + +fn profile(name: &str) -> Profile { + serde_json::from_value(serde_json::json!({ + "id": profile_id_for(name), + "name": name, + "host": "example.org", + "user": "ann", + })) + .unwrap() +} + +fn key() -> CollectionKey { + CollectionKey::generate().unwrap() +} + +fn object( + key: &CollectionKey, + profile_id: ProfileId, + kind: &str, + plain: &[u8], +) -> v2::StoredObject { + let object_id = profile_id.to_string(); + v2::StoredObject { + collection_id: "c".into(), + object_id: object_id.clone(), + version: 3, + kind: kind.into(), + ciphertext: key.seal_object("c", &object_id, kind, plain).unwrap(), + deleted: false, + updated_at_ms: 0, + updated_by_device: String::new(), + stream_version: 7, + } +} + +#[test] +fn a_profile_written_by_this_client_reads_back_with_its_version() { + let key = key(); + let web = profile("web"); + let stored = object(&key, web.id, KIND, &encode(&web).unwrap()); + let read = read("c", &key, &stored).unwrap().unwrap(); + assert_eq!(read.id, web.id); + assert_eq!(read.version, ReplicaVersion::new(3)); + assert_eq!(read.body, RemoteBody::Present(Box::new(web))); +} + +#[test] +fn a_tombstone_is_a_deletion_without_opening_anything() { + let key = key(); + let id = profile_id_for("gone"); + let mut stored = object(&key, id, KIND, b"{}"); + stored.deleted = true; + stored.ciphertext.clear(); + let read = read("c", &key, &stored).unwrap().unwrap(); + assert_eq!(read.body, RemoteBody::Deleted); +} + +#[test] +fn an_object_this_client_cannot_decode_is_unrecognized() { + let key = key(); + let web = profile("web"); + let newer = object(&key, web.id, KIND, br#"{"v":2,"profile":{}}"#); + assert_eq!( + read("c", &key, &newer).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); + let other_kind = object(&key, web.id, "profile-v2", &encode(&web).unwrap()); + assert_eq!( + read("c", &key, &other_kind).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); + let garbage = object(&key, web.id, KIND, b"not json"); + assert_eq!( + read("c", &key, &garbage).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); +} + +#[test] +fn a_profile_stored_under_another_profiles_id_is_unrecognized() { + let key = key(); + let web = profile("web"); + let other = profile("other"); + let moved = object(&key, other.id, KIND, &encode(&web).unwrap()); + assert_eq!( + read("c", &key, &moved).unwrap().unwrap().body, + RemoteBody::Unrecognized + ); +} + +#[test] +fn an_object_that_does_not_authenticate_is_an_error() { + let key = key(); + let web = profile("web"); + let mut forged = object(&key, web.id, KIND, &encode(&web).unwrap()); + forged.ciphertext[30] ^= 0xff; + let error = read("c", &key, &forged).unwrap_err(); + let ReplicaError::Other(cause) = error else { + panic!("a forgery is a failure of the replica"); + }; + assert!(cause.downcast_ref::().is_some()); + // The same bytes under another key do not open either. + let stranger = self::key(); + let sealed = object(&key, web.id, KIND, &encode(&web).unwrap()); + assert!(read("c", &stranger, &sealed).is_err()); +} + +#[test] +fn an_object_that_is_not_a_profile_id_is_skipped() { + let key = key(); + let stored = v2::StoredObject { + object_id: "not-a-profile".into(), + ..object(&key, profile_id_for("x"), KIND, b"{}") + }; + assert!(read("c", &key, &stored).unwrap().is_none()); +} + +#[test] +fn positions_round_trip_and_an_unreadable_cursor_starts_over() { + let mut positions = Positions::default(); + positions.set("a", 4); + let cursor = positions.cursor(); + assert_eq!(Positions::parse(Some(&cursor)).get("a"), 4); + assert_eq!( + Positions::parse(Some(&ReplicaCursor::new("???"))).get("a"), + 0 + ); + assert_eq!(Positions::parse(None).get("a"), 0); +} + +#[test] +fn a_profile_the_web_panel_keeps_reads_as_a_server_that_signs_in_with_its_password() { + // Exactly what the web panel writes for a server connected with a password (`newProfile`). + let id = profile_id_for("prod"); + let credential = "ab".repeat(16); + let plain = serde_json::json!({ + "v": 1, + "profile": { + "id": id, + "name": "prod", + "host": "prod.example.com", + "port": 2222, + "user": "ops", + "auth": {"method": "password"}, + "credential": credential, + } + }); + let RemoteBody::Present(profile) = body_of(id, plain.to_string().as_bytes()) else { + panic!("the web panel's profile reads here"); + }; + assert_eq!(profile.name, "prod"); + assert_eq!( + profile.target, + nocterm_session::Target::new("ops", "prod.example.com", 2222) + ); + assert_eq!(profile.auth, nocterm_session::Auth::Password); + assert_eq!( + profile.credential.map(|id| id.to_string()), + Some(credential) + ); +} + +#[test] +fn a_profile_the_web_panel_keeps_with_a_key_signs_in_with_that_vault_key() { + let id = profile_id_for("prod"); + let key = "cd".repeat(16); + let public_key = + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID7XjK7UMquFonNQf6mOb5ODK/CIOubLkjrBcI6wLh5+"; + let plain = serde_json::json!({ + "v": 1, + "profile": { + "id": id, + "name": "prod", + "host": "prod.example.com", + "user": "ops", + "auth": {"method": "vault_key", "credential": key, "public_key": public_key}, + } + }); + let RemoteBody::Present(profile) = body_of(id, plain.to_string().as_bytes()) else { + panic!("the web panel's profile reads here"); + }; + let nocterm_session::Auth::VaultKey { + credential, + public_key: named, + } = profile.auth + else { + panic!("signs in with a vault key: {:?}", profile.auth); + }; + assert_eq!(credential.to_string(), key); + assert_eq!(named, public_key); + assert_eq!(profile.credential, None); +} diff --git a/crates/hub-io/src/store.rs b/crates/hub-io/src/store.rs new file mode 100644 index 00000000..0303d002 --- /dev/null +++ b/crates/hub-io/src/store.rs @@ -0,0 +1,210 @@ +//! Encrypted collections of one account on a hub. +//! +//! [`StoreOps`] is the narrow seam to the hub's vault service: plain calls with sealed bytes. +//! [`Store`] adds what needs the account key: finding or creating the account's collection of +//! a kind, and sealing what goes into it. Only the kinds this client knows are asked for, so a +//! collection of another kind is never listed, opened, changed or deleted here. + +mod client; +mod grpc; +#[cfg(test)] +pub(crate) mod memory; + +use std::sync::Arc; + +use futures::future::BoxFuture; +use futures::stream::BoxStream; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubError; +use nocterm_keys::{CollectionKey, OpenError, random_bytes}; +use uuid::Builder; + +use crate::account::OpenKeys; +use crate::classify::remote; + +pub(crate) use grpc::HubStore; + +/// The kind of the collection that holds the vault's credentials. +pub(crate) const VAULT_KIND: &str = "vault"; +/// The kind of the collection that holds the saved profiles. +pub(crate) const PROFILES_KIND: &str = "profiles"; + +/// Why a store call failed. +#[derive(Debug, thiserror::Error)] +pub(crate) enum OpError { + /// The version the write expected is not the stored one. + #[error("the object changed on the hub")] + Conflict, + /// The hub returned a key or an object that does not authenticate: it was altered. + #[error("the hub returned data that does not authenticate")] + Damaged(#[source] OpenError), + #[error(transparent)] + Hub(#[from] HubError), +} + +pub(crate) type Call<'a, T> = BoxFuture<'a, Result>; + +/// The calls of the vault service this client makes, with sealed bytes only. +pub(crate) trait StoreOps: Send + Sync { + /// The account's keyset record as the hub stores it; `None` before one was created. + fn keyset(&self) -> Call<'_, Option>; + fn collections(&self, kind: &str) -> Call<'_, Vec>; + fn create_collection(&self, request: v2::CreateCollectionRequest) -> Call<'_, v2::Collection>; + /// The key envelope of a collection, sealed by the account key. + fn collection_key(&self, collection_id: &str) -> Call<'_, v2::CollectionKey>; + fn put_object(&self, request: v2::PutObjectRequest) -> Call<'_, v2::ObjectVersion>; + fn delete_object(&self, request: v2::DeleteObjectRequest) -> Call<'_, v2::ObjectVersion>; + fn changes(&self, collection_id: &str, after: u64) -> Call<'_, v2::ChangeBatch>; + /// Yields whenever a head of one of `collections` moved; ends when the connection does. + fn watch(&self, collections: Vec) -> Call<'_, BoxStream<'static, ()>>; +} + +/// A collection of the account and the key that opens its objects. +#[derive(Clone)] +pub(crate) struct Located { + id: String, + key: Arc, + envelope: Vec, +} + +impl Located { + pub(crate) fn id(&self) -> &str { + &self.id + } + + pub(crate) fn key(&self) -> &CollectionKey { + &self.key + } + + /// The sealed key as the hub holds it, for the cache. + pub(crate) fn envelope(&self) -> &[u8] { + &self.envelope + } +} + +/// Of the live collections of `kind`, the oldest: when two devices made one at +/// the same moment, it is the collection and the others stay unused. +fn oldest<'a>(listed: &'a [v2::Collection], kind: &str) -> Option<&'a v2::Collection> { + listed + .iter() + .filter(|c| !c.deleted && c.kind == kind) + .min_by(|a, b| { + (a.created_at_ms, &a.collection_id).cmp(&(b.created_at_ms, &b.collection_id)) + }) +} + +/// The account's collections on one hub, with the keys to read them. +pub(crate) struct Store { + ops: Arc, + keys: Arc, +} + +impl Store { + pub(crate) fn new(ops: Arc, keys: Arc) -> Self { + Self { ops, keys } + } + + /// The account's collection of `kind`, if the hub has one. When several exist (two + /// devices made one at once) the oldest is the collection; the others stay unused. + pub(crate) async fn find(&self, kind: &str) -> Result, OpError> { + let listed = self.ops.collections(kind).await?; + match oldest(&listed, kind) { + Some(chosen) => Ok(Some(self.open(chosen).await?)), + None => Ok(None), + } + } + + /// The account's own collection: the envelope was sealed by this account's key. + async fn open(&self, chosen: &v2::Collection) -> Result { + let record = self.ops.collection_key(&chosen.collection_id).await?; + if record.collection_id != chosen.collection_id || record.wrapped_key.len() > 256 { + return Err(OpError::Hub(HubError::PermissionDenied)); + } + let key = self + .keys + .own_collection(&chosen.collection_id, &record.wrapped_key) + .map_err(OpError::Damaged)?; + Ok(Located { + id: chosen.collection_id.clone(), + key, + envelope: record.wrapped_key, + }) + } + + /// The personal collection of `kind`, created first if the account has none. + pub(crate) async fn ensure(&self, kind: &str) -> Result { + if let Some(found) = self.find(kind).await? { + return Ok(found); + } + let id = Builder::from_random_bytes(random_bytes::<16>().map_err(remote)?) + .into_uuid() + .to_string(); + let (key, envelope) = self.keys.new_collection(&id)?; + let sealed_name = key.seal_name(&id, kind).map_err(remote)?; + self.ops + .create_collection(v2::CreateCollectionRequest { + kind: kind.to_owned(), + sealed_name, + wrapped_key: envelope, + collection_id: id, + }) + .await?; + // A device that created one at the same moment may have made an older one. + self.find(kind) + .await? + .ok_or(OpError::Hub(HubError::NotFound)) + } + + /// Seals `plaintext` for the object and stores it. `expected` is the stored version, or 0 + /// to create. + pub(crate) async fn put( + &self, + at: &Located, + kind: &str, + object_id: &str, + plaintext: &[u8], + expected: u64, + ) -> Result { + let ciphertext = at + .key + .seal_object(&at.id, object_id, kind, plaintext) + .map_err(remote)?; + self.ops + .put_object(v2::PutObjectRequest { + collection_id: at.id.clone(), + object_id: object_id.to_owned(), + expected_version: expected, + kind: kind.to_owned(), + ciphertext, + }) + .await + } + + /// Replaces the object with a tombstone. + pub(crate) async fn remove( + &self, + at: &Located, + object_id: &str, + expected: u64, + ) -> Result { + self.ops + .delete_object(v2::DeleteObjectRequest { + collection_id: at.id.clone(), + object_id: object_id.to_owned(), + expected_version: expected, + }) + .await + } + + /// The changes of the collection after stream version `after`. + pub(crate) async fn changes( + &self, + at: &Located, + after: u64, + ) -> Result { + self.ops.changes(&at.id, after).await + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/store/client.rs b/crates/hub-io/src/store/client.rs new file mode 100644 index 00000000..678254a1 --- /dev/null +++ b/crates/hub-io/src/store/client.rs @@ -0,0 +1,48 @@ +//! Client selection is separate from the one shared store RPC implementation. +use std::sync::Arc; + +use nocterm_hub_client::{ClientError, HubClient}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +use super::OpError; +use crate::{ + account::source::{CapturedAccount, CapturedMembership}, + pool::{Pool, captured::CapturedClient}, +}; + +pub(super) struct StoreClient { + fixed: CapturedClient, +} +impl StoreClient { + pub(super) fn captured( + pool: Arc, + native: &CapturedAccount, + source: Arc, + ) -> Result { + let fixed = CapturedClient::account(pool.clone(), native, source)?; + Ok(Self { fixed }) + } + pub(super) async fn fresh(&self) -> Result { + self.fixed.fresh().await.map_err(OpError::from) + } + + pub(super) fn membership( + pool: Arc, + native: &CapturedMembership, + ) -> Result { + native.check()?; + let fixed = CapturedClient::current(pool.clone(), native.hub().clone())?; + if fixed.hub() != native.hub() { + return Err(HubError::Stale); + } + native.check()?; + Ok(Self { fixed }) + } + + pub(super) fn failure(&self, error: ClientError) -> OpError { + if error.is_conflict() { + return OpError::Conflict; + } + OpError::Hub(self.fixed.failure(error)) + } +} diff --git a/crates/hub-io/src/store/grpc.rs b/crates/hub-io/src/store/grpc.rs new file mode 100644 index 00000000..25936f3e --- /dev/null +++ b/crates/hub-io/src/store/grpc.rs @@ -0,0 +1,157 @@ +//! The store service of a signed-in hub. + +use std::sync::Arc; + +use super::client::StoreClient; +use crate::account::source::{CapturedAccount, CapturedMembership}; +use futures::StreamExt as _; +use nocterm_hub_client::ClientError; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; + +use super::{Call, OpError, StoreOps}; +use crate::pool::Pool; + +/// Reaches the vault service of one hub with the pooled client. +pub(crate) struct HubStore { + client: StoreClient, +} + +macro_rules! store_rpc { + ($client:expr, $method:ident, $request:expr) => { + $client.vault().$method($request).await + }; +} + +impl HubStore { + pub(crate) fn captured( + pool: Arc, + native: &CapturedAccount, + source: Arc, + ) -> Result { + Ok(Self { + client: StoreClient::captured(pool, native, source)?, + }) + } + + fn failure(&self, error: ClientError) -> OpError { + self.client.failure(error) + } + + pub(crate) fn membership( + pool: Arc, + native: &CapturedMembership, + ) -> Result { + Ok(Self { + client: StoreClient::membership(pool, native)?, + }) + } +} + +impl StoreOps for HubStore { + fn keyset(&self) -> Call<'_, Option> { + Box::pin(async move { + let client = self.client.fresh().await?; + client + .keyset_record() + .await + .map_err(|error| self.failure(error)) + }) + } + + fn collections(&self, kind: &str) -> Call<'_, Vec> { + let kind = kind.to_owned(); + Box::pin(async move { + let client = self.client.fresh().await?; + let list = store_rpc!( + client, + list_collections, + v2::ListCollectionsRequest { kind } + ) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(list.into_inner().collections) + }) + } + + fn create_collection(&self, request: v2::CreateCollectionRequest) -> Call<'_, v2::Collection> { + Box::pin(async move { + let client = self.client.fresh().await?; + let made = store_rpc!(client, create_collection, request) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(made.into_inner()) + }) + } + + fn collection_key(&self, collection_id: &str) -> Call<'_, v2::CollectionKey> { + let collection_id = collection_id.to_owned(); + Box::pin(async move { + let client = self.client.fresh().await?; + let key = store_rpc!( + client, + get_collection_key, + v2::IdRequest { id: collection_id } + ) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(key.into_inner()) + }) + } + + fn put_object(&self, request: v2::PutObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + let client = self.client.fresh().await?; + let stored = store_rpc!(client, put_object, request) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(stored.into_inner()) + }) + } + + fn delete_object(&self, request: v2::DeleteObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + let client = self.client.fresh().await?; + let stored = store_rpc!(client, delete_object, request) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(stored.into_inner()) + }) + } + + fn changes(&self, collection_id: &str, after: u64) -> Call<'_, v2::ChangeBatch> { + let collection_id = collection_id.to_owned(); + Box::pin(async move { + let client = self.client.fresh().await?; + let batch = store_rpc!( + client, + list_changes, + v2::ListChangesRequest { + collection_id, + after, + limit: 0, + } + ) + .map_err(|status| self.failure(ClientError::from(status)))?; + Ok(batch.into_inner()) + }) + } + + fn watch(&self, collections: Vec) -> Call<'_, futures::stream::BoxStream<'static, ()>> { + Box::pin(async move { + let client = self.client.fresh().await?; + let stream = store_rpc!( + client, + watch_changes, + v2::WatchChangesRequest { + collection_ids: collections, + } + ) + .map_err(|status| self.failure(ClientError::from(status)))? + .into_inner(); + // A hint only says "read again"; the stream ends at the first failure. + let hints = futures::stream::unfold(stream, |mut stream| async move { + match stream.message().await { + Ok(Some(_)) => Some(((), stream)), + _ => None, + } + }); + Ok(hints.boxed()) + }) + } +} diff --git a/crates/hub-io/src/store/memory.rs b/crates/hub-io/src/store/memory.rs new file mode 100644 index 00000000..4b691d49 --- /dev/null +++ b/crates/hub-io/src/store/memory.rs @@ -0,0 +1,379 @@ +//! An in-memory hub store for tests: versions, conflicts, tombstones and change streams as the +//! hub keeps them, and an offline switch. + +use std::collections::{BTreeMap, HashMap, VecDeque}; +use std::sync::atomic::{AtomicBool, Ordering}; +use std::sync::{Mutex, MutexGuard, PoisonError}; + +use futures::StreamExt as _; +use nocterm_core::SharedCause; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubError; +use nocterm_session::testing::Gate; + +use super::{Call, OpError, StoreOps}; +mod accounts; +pub(crate) use accounts::MemoryAccountStores; + +#[derive(Default)] +struct Held { + collections: Vec, + keys: HashMap, + objects: HashMap>, + heads: HashMap, + clock: i64, + keyset: Option, + scripted_changes: VecDeque, + change_calls: usize, +} + +/// One account's collections on a pretend hub. +#[derive(Default)] +pub(crate) struct MemoryHub { + held: Mutex, + offline: AtomicBool, + pauses: Mutex)>>, +} + +/// Deterministic boundaries around actual MemoryHub behavior. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub(crate) enum StoreBoundary { + KeysetReady, + WatchReady, + Collections, + CreateCollection, + CreateCollectionReady, + ChangesReady, + PutReady, + Put, +} + +impl MemoryHub { + pub(crate) fn script_changes(&self, pages: Vec) { + self.held().scripted_changes = pages.into(); + } + + pub(crate) fn change_calls(&self) -> usize { + self.held().change_calls + } + /// Returns (entered, release). The next matching operation pauses once. + pub(crate) fn pause_next(&self, at: StoreBoundary) -> (Gate, Gate) { + self.pause_outcome(at, None) + } + pub(crate) fn pause_next_error(&self, at: StoreBoundary, error: HubError) -> (Gate, Gate) { + self.pause_outcome(at, Some(error)) + } + fn pause_outcome(&self, at: StoreBoundary, error: Option) -> (Gate, Gate) { + let entered = Gate::pending(); + let release = Gate::pending(); + self.pauses + .lock() + .unwrap() + .insert(at, (entered.clone(), release.clone(), error)); + (entered, release) + } + + async fn boundary(&self, at: StoreBoundary) -> Result<(), OpError> { + let pause = self.pauses.lock().unwrap().remove(&at); + if let Some((entered, release, error)) = pause { + entered.release(); + release.wait().await; + if let Some(error) = error { + return Err(OpError::Hub(error)); + } + } + Ok(()) + } + + pub(crate) fn set_offline(&self, offline: bool) { + self.offline.store(offline, Ordering::SeqCst); + } + + fn held(&self) -> MutexGuard<'_, Held> { + self.held.lock().unwrap_or_else(PoisonError::into_inner) + } + + fn reach(&self) -> Result<(), OpError> { + if self.offline.load(Ordering::SeqCst) { + let address = "hub.test".parse().expect("a valid address"); + return Err(OpError::Hub(HubError::Unreachable { + address, + cause: SharedCause::new(std::io::Error::other("offline")), + })); + } + Ok(()) + } + + /// The keyset record the hub holds for the account. + pub(crate) fn set_keyset(&self, record: Option) { + self.held().keyset = record; + } + + /// A collection another client made, of any kind. + pub(crate) fn add_foreign(&self, kind: &str) -> String { + let mut held = self.held(); + held.clock += 1; + let id = format!("foreign-{}", held.collections.len()); + let collection = v2::Collection { + collection_id: id.clone(), + kind: kind.to_owned(), + sealed_name: vec![1], + head: 0, + created_at_ms: held.clock, + deleted: false, + }; + held.collections.push(collection); + id + } + + /// The ids of the collections of `kind`. + pub(crate) fn collections_of_kind(&self, kind: &str) -> Vec { + self.held() + .collections + .iter() + .filter(|c| c.kind == kind) + .map(|c| c.collection_id.clone()) + .collect() + } + + /// Every stored object of a collection, tombstones included, as the hub holds them. + pub(crate) fn objects(&self, collection_id: &str) -> Vec { + let held = self.held(); + held.objects + .get(collection_id) + .map(|objects| objects.values().cloned().collect()) + .unwrap_or_default() + } + + /// Replaces an object's ciphertext behind the client's back, as a hostile hub could. + pub(crate) fn tamper(&self, collection_id: &str, object_id: &str, ciphertext: Vec) { + let mut held = self.held(); + if let Some(object) = held + .objects + .get_mut(collection_id) + .and_then(|objects| objects.get_mut(object_id)) + { + object.ciphertext = ciphertext; + } + } + + /// Replaces the sealed key a collection hands out. + pub(crate) fn tamper_key(&self, collection_id: &str, envelope: Vec) { + if let Some(record) = self.held().keys.get_mut(collection_id) { + record.wrapped_key = envelope; + } + } + + fn write( + &self, + collection_id: &str, + object_id: &str, + expected: u64, + content: Option<(String, Vec)>, + ) -> Result { + let mut held = self.held(); + if !held + .collections + .iter() + .any(|c| c.collection_id == collection_id) + { + return Err(OpError::Hub(HubError::NotFound)); + } + let current = held + .objects + .get(collection_id) + .and_then(|objects| objects.get(object_id)) + .map(|object| object.version) + .unwrap_or(0); + if current != expected { + return Err(OpError::Conflict); + } + let head = held.heads.entry(collection_id.to_owned()).or_default(); + *head += 1; + let stream_version = *head; + let (kind, ciphertext, deleted) = match content { + Some((kind, ciphertext)) => (kind, ciphertext, false), + None => { + let kind = held + .objects + .get(collection_id) + .and_then(|objects| objects.get(object_id)) + .map(|object| object.kind.clone()) + .unwrap_or_default(); + (kind, Vec::new(), true) + } + }; + let version = current + 1; + held.objects + .entry(collection_id.to_owned()) + .or_default() + .insert( + object_id.to_owned(), + v2::StoredObject { + collection_id: collection_id.to_owned(), + object_id: object_id.to_owned(), + version, + kind, + ciphertext, + deleted, + updated_at_ms: 0, + updated_by_device: String::new(), + stream_version, + }, + ); + Ok(v2::ObjectVersion { + collection_id: collection_id.to_owned(), + object_id: object_id.to_owned(), + version, + stream_version, + }) + } +} + +impl StoreOps for MemoryHub { + fn keyset(&self) -> Call<'_, Option> { + Box::pin(async move { + self.reach()?; + let record = self.held().keyset.clone(); + self.boundary(StoreBoundary::KeysetReady).await?; + Ok(record) + }) + } + + fn collections(&self, kind: &str) -> Call<'_, Vec> { + let kind = kind.to_owned(); + Box::pin(async move { + self.boundary(StoreBoundary::Collections).await?; + self.reach()?; + Ok(self + .held() + .collections + .iter() + .filter(|c| c.kind == kind) + .cloned() + .collect()) + }) + } + + fn create_collection(&self, request: v2::CreateCollectionRequest) -> Call<'_, v2::Collection> { + Box::pin(async move { + self.boundary(StoreBoundary::CreateCollection).await?; + self.reach()?; + let collection = { + let mut held = self.held(); + if held + .collections + .iter() + .any(|row| row.collection_id == request.collection_id) + { + return Err(OpError::Conflict); + } + held.clock += 1; + let collection = v2::Collection { + collection_id: request.collection_id.clone(), + kind: request.kind, + sealed_name: request.sealed_name, + head: 0, + created_at_ms: held.clock, + deleted: false, + }; + held.keys.insert( + request.collection_id.clone(), + v2::CollectionKey { + collection_id: request.collection_id, + key_version: 1, + wrapped_key: request.wrapped_key, + }, + ); + held.collections.push(collection.clone()); + collection + }; + self.boundary(StoreBoundary::CreateCollectionReady).await?; + Ok(collection) + }) + } + + fn collection_key(&self, collection_id: &str) -> Call<'_, v2::CollectionKey> { + let id = collection_id.to_owned(); + Box::pin(async move { + self.reach()?; + self.held() + .keys + .get(&id) + .cloned() + .ok_or(OpError::Hub(HubError::NotFound)) + }) + } + + fn put_object(&self, request: v2::PutObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + self.boundary(StoreBoundary::Put).await?; + self.reach()?; + let written = self.write( + &request.collection_id, + &request.object_id, + request.expected_version, + Some((request.kind, request.ciphertext)), + )?; + self.boundary(StoreBoundary::PutReady).await?; + Ok(written) + }) + } + + fn delete_object(&self, request: v2::DeleteObjectRequest) -> Call<'_, v2::ObjectVersion> { + Box::pin(async move { + self.reach()?; + self.write( + &request.collection_id, + &request.object_id, + request.expected_version, + None, + ) + }) + } + + fn changes(&self, collection_id: &str, after: u64) -> Call<'_, v2::ChangeBatch> { + let id = collection_id.to_owned(); + Box::pin(async move { + self.reach()?; + let scripted = { + let mut held = self.held(); + held.change_calls += 1; + held.scripted_changes.pop_front() + }; + let batch = scripted.unwrap_or_else(|| { + let held = self.held(); + let mut changes: Vec<_> = held + .objects + .get(&id) + .map(|objects| { + objects + .values() + .filter(|object| object.stream_version > after) + .cloned() + .collect() + }) + .unwrap_or_default(); + changes.sort_by_key(|object: &v2::StoredObject| object.stream_version); + v2::ChangeBatch { + changes, + head: held.heads.get(&id).copied().unwrap_or(0), + more: false, + } + }); + self.boundary(StoreBoundary::ChangesReady).await?; + Ok(batch) + }) + } + + fn watch( + &self, + _collections: Vec, + ) -> Call<'_, futures::stream::BoxStream<'static, ()>> { + Box::pin(async move { + self.reach()?; + self.boundary(StoreBoundary::WatchReady).await?; + Ok(futures::stream::pending().boxed()) + }) + } +} diff --git a/crates/hub-io/src/store/memory/accounts.rs b/crates/hub-io/src/store/memory/accounts.rs new file mode 100644 index 00000000..8a1bee40 --- /dev/null +++ b/crates/hub-io/src/store/memory/accounts.rs @@ -0,0 +1,39 @@ +//! Native account routing over the shared MemoryHub storage implementation. +use super::MemoryHub; +use crate::{ + account::{ + operations::OperationStores, + source::{CapturedAccount, CapturedMembership}, + }, + store::StoreOps, +}; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; +use std::{collections::HashMap, sync::Arc}; + +pub(crate) struct MemoryAccountStores(HashMap>); +impl MemoryAccountStores { + pub(crate) fn new(accounts: impl IntoIterator)>) -> Self { + Self(accounts.into_iter().collect()) + } + fn account(&self, id: &str) -> Result, HubError> { + self.0 + .get(id) + .cloned() + .map(|hub| hub as Arc) + .ok_or(HubError::Stale) + } +} +impl OperationStores for MemoryAccountStores { + fn opened( + &self, + native: &CapturedAccount, + source: Arc, + ) -> Result, HubError> { + source.check_current().map_err(|_| HubError::Stale)?; + self.account(native.subject()?.account()) + } + fn membership(&self, native: &CapturedMembership) -> Result, HubError> { + native.check()?; + self.account(native.subject()?.account()) + } +} diff --git a/crates/hub-io/src/store/tests.rs b/crates/hub-io/src/store/tests.rs new file mode 100644 index 00000000..37017961 --- /dev/null +++ b/crates/hub-io/src/store/tests.rs @@ -0,0 +1,125 @@ +use std::sync::Arc; + +use super::memory::MemoryHub; +use super::*; +use crate::account::testing::open_keys; + +fn store(hub: &Arc) -> Store { + Store::new(hub.clone(), open_keys()) +} + +#[tokio::test] +async fn the_collection_of_a_kind_is_created_once_and_found_again() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + assert!(store.find(VAULT_KIND).await.unwrap().is_none()); + let first = store.ensure(VAULT_KIND).await.unwrap(); + let again = store.ensure(VAULT_KIND).await.unwrap(); + assert_eq!(first.id(), again.id()); +} + +#[tokio::test] +async fn objects_are_stored_sealed_and_a_stale_write_conflicts() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + let at = store.ensure(VAULT_KIND).await.unwrap(); + let v1 = store + .put(&at, "credential", "o1", b"plain text", 0) + .await + .unwrap(); + assert_eq!(v1.version, 1); + let stored = hub.objects(at.id()); + assert_eq!(stored.len(), 1); + assert!(!String::from_utf8_lossy(&stored[0].ciphertext).contains("plain text")); + assert!(matches!( + store.put(&at, "credential", "o1", b"again", 0).await, + Err(OpError::Conflict) + )); + let v2 = store + .put(&at, "credential", "o1", b"again", 1) + .await + .unwrap(); + assert_eq!(v2.version, 2); + let opened = at + .key() + .open_object( + at.id(), + "o1", + "credential", + &hub.objects(at.id())[0].ciphertext, + ) + .unwrap(); + assert_eq!(opened.as_slice(), b"again"); +} + +#[tokio::test] +async fn removing_leaves_a_tombstone_in_the_change_feed() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + let at = store.ensure(PROFILES_KIND).await.unwrap(); + store.put(&at, "profile", "p", b"{}", 0).await.unwrap(); + store.remove(&at, "p", 1).await.unwrap(); + let batch = store.changes(&at, 1).await.unwrap(); + assert_eq!(batch.changes.len(), 1); + assert!(batch.changes[0].deleted); + assert_eq!(batch.head, 2); +} + +#[tokio::test] +async fn collections_of_other_kinds_are_never_picked() { + let hub = Arc::new(MemoryHub::default()); + hub.add_foreign("future-kind"); + let store = store(&hub); + assert!(store.find(VAULT_KIND).await.unwrap().is_none()); + let mine = store.ensure(VAULT_KIND).await.unwrap(); + assert!(!mine.id().starts_with("foreign")); +} + +#[tokio::test] +async fn when_two_devices_each_made_a_collection_the_oldest_is_used() { + let hub = Arc::new(MemoryHub::default()); + let one = store(&hub); + let first = one.ensure(VAULT_KIND).await.unwrap(); + // A second device that listed nothing before the first one's create made its own. + let keys = open_keys(); + let (key, envelope) = keys.new_collection("later").unwrap(); + hub.create_collection(v2::CreateCollectionRequest { + kind: VAULT_KIND.into(), + sealed_name: key.seal_name("later", VAULT_KIND).unwrap(), + wrapped_key: envelope, + collection_id: "later".into(), + }) + .await + .unwrap(); + assert_eq!( + one.find(VAULT_KIND).await.unwrap().unwrap().id(), + first.id() + ); +} + +#[tokio::test] +async fn a_key_envelope_the_hub_altered_is_damage_not_a_new_collection() { + let hub = Arc::new(MemoryHub::default()); + let store = store(&hub); + let at = store.ensure(VAULT_KIND).await.unwrap(); + // Another client session has no cached key: it must open the envelope the hub returns. + let fresh = Store::new(hub.clone(), open_keys()); + hub.tamper_key(at.id(), vec![0; 64]); + assert!(matches!( + fresh.find(VAULT_KIND).await, + Err(OpError::Damaged(_)) + )); +} + +#[tokio::test] +async fn an_unreachable_hub_is_a_hub_error_and_creates_nothing() { + let hub = Arc::new(MemoryHub::default()); + hub.set_offline(true); + let store = store(&hub); + assert!(matches!( + store.ensure(VAULT_KIND).await, + Err(OpError::Hub(HubError::Unreachable { .. })) + )); + hub.set_offline(false); + assert!(store.find(VAULT_KIND).await.unwrap().is_none()); +} diff --git a/crates/hub-io/src/task.rs b/crates/hub-io/src/task.rs new file mode 100644 index 00000000..06567df7 --- /dev/null +++ b/crates/hub-io/src/task.rs @@ -0,0 +1,97 @@ +//! Work on the adapter's threads whose result any executor can await. + +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +use futures::FutureExt as _; +use futures::channel::oneshot; +use tokio::runtime::Handle; +use tokio::task::AbortHandle; + +/// The result of a task on the adapter's runtime; abandons the task when dropped. +/// +/// Resolves to `None` when the runtime stopped before the task finished. +pub(crate) struct Spawned { + result: oneshot::Receiver, + task: AbortHandle, +} + +/// Runs `work` on the runtime of `handle`. +pub(crate) fn spawn( + handle: &Handle, + work: impl Future + Send + 'static, +) -> Spawned { + let (sender, result) = oneshot::channel(); + let task = handle + .spawn(async move { + let _ = sender.send(work.await); + }) + .abort_handle(); + Spawned { result, task } +} + +impl Future for Spawned { + type Output = Option; + + fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll { + self.result.poll_unpin(cx).map(Result::ok) + } +} + +impl Drop for Spawned { + fn drop(&mut self) { + self.task.abort(); + } +} + +#[cfg(test)] +mod tests { + use super::*; + + struct Release(Option>); + impl Drop for Release { + fn drop(&mut self) { + if let Some(sender) = self.0.take() { + let _ = sender.send(()); + } + } + } + + #[test] + fn a_result_can_be_awaited_by_an_executor_without_a_tokio_context() { + let runtime = tokio::runtime::Builder::new_multi_thread() + .worker_threads(1) + .build() + .unwrap(); + assert_eq!( + futures::executor::block_on(spawn(runtime.handle(), async { 7 })), + Some(7) + ); + } + + #[tokio::test] + async fn dropping_an_unpolled_answer_releases_the_task_resources() { + let (sender, released) = oneshot::channel(); + let resource = Release(Some(sender)); + let answer = spawn(&Handle::current(), async move { + let _resource = resource; + std::future::pending::<()>().await; + }); + drop(answer); + tokio::time::timeout(std::time::Duration::from_secs(1), released) + .await + .unwrap() + .unwrap(); + } + + #[test] + fn runtime_shutdown_ends_an_unanswered_task() { + let runtime = tokio::runtime::Builder::new_current_thread() + .build() + .unwrap(); + let answer = spawn(runtime.handle(), std::future::pending::<()>()); + runtime.shutdown_background(); + assert_eq!(futures::executor::block_on(answer), None); + } +} diff --git a/crates/hub-io/src/vault.rs b/crates/hub-io/src/vault.rs new file mode 100644 index 00000000..d5b221f7 --- /dev/null +++ b/crates/hub-io/src/vault.rs @@ -0,0 +1,262 @@ +//! The vault of the account hub: credentials kept as sealed objects on the hub. +//! +//! [`HubVault`] implements the vault port over one hub's `vault` collection. The vault's +//! password is the account's: the login password, or a passphrase of its own for an account +//! that signs in through a provider. Credentials are readable offline from the encrypted cache; +//! writes need the hub. The vault has no password of its own to create or change here, and no +//! device unlock, so those operations are [`VaultError::Unsupported`]. + +mod codec; +pub(crate) mod credentials; + +mod idle; +mod ledger; +mod snapshot_budget; +mod unlock; +pub(crate) use idle::Idle; + +use std::sync::Arc; +use std::time::Duration; + +use futures::channel::mpsc as progress; +use futures::future::BoxFuture; +use nocterm_session::{CredentialId, Secret}; +use nocterm_vault::{ + CredentialBinding, CredentialInfo, DeviceCapability, VaultAccess, VaultError, VaultFuture, + VaultStatus, +}; +use tokio::runtime::Handle; + +pub(crate) use self::ledger::Ledger; +use self::unlock::Unlocker; +use crate::account::{Slot, operations::AccountOperations}; +use crate::task::spawn; + +pub struct HubVault { + handle: Handle, + slot: Arc, + operations: Arc, + idle: Arc, +} + +impl HubVault { + pub(crate) fn new(handle: Handle, slot: Arc, operations: Arc) -> Self { + Self { + idle: Arc::new(Idle::new(handle.clone())), + handle, + slot, + operations, + } + } + + pub(crate) fn idle(&self) -> Arc { + self.idle.clone() + } + + fn run(&self, work: impl FnOnce(Ledger) -> F + Send + 'static) -> VaultFuture + where + T: Send + 'static, + F: std::future::Future> + Send + 'static, + { + let opened = match self.operations.opened() { + Ok(opened) => opened, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let source = opened.source().clone(); + let returned = source.clone(); + let idle = self.idle.clone(); + let task = spawn(&self.handle, async move { + let result = source + .during(async { + let ledger = opened.ledger().map_err(native_failure)?; + work(ledger).await + }) + .await + .map_err(native_failure) + .and_then(|result| result); + idle.touch(source.native()); + drop(opened); + result + }); + Box::pin(async move { + let result = task.await.unwrap_or(Err(VaultError::Cancelled)); + returned.check().map_err(native_failure)?; + result + }) + } + + fn unsupported() -> VaultFuture { + Box::pin(async { Err(VaultError::Unsupported) }) + } +} + +impl VaultAccess for HubVault { + fn is_unlocked(&self) -> bool { + self.slot.status() == VaultStatus::Unlocked + } + + fn exists(&self) -> bool { + self.slot.status() != VaultStatus::Missing + } + + fn status(&self) -> VaultStatus { + self.slot.status() + } + + fn revision(&self) -> u64 { + self.slot.notice().count() + } + + fn changed(&self, seen: u64) -> BoxFuture<'static, u64> { + let mut changes = self.slot.notice().subscribe(); + Box::pin(async move { + loop { + let now = *changes.borrow_and_update(); + if now != seen || changes.changed().await.is_err() { + return now; + } + } + }) + } + + fn wait_unlocked(&self) -> BoxFuture<'static, ()> { + let member = self.operations.current_membership(); + let slot = Arc::clone(&self.slot); + let mut changes = slot.notice().subscribe(); + Box::pin(async move { + let Ok(member) = member else { + return; + }; + loop { + match member.with_current(|| Ok(slot.status() == VaultStatus::Unlocked)) { + Ok(false) => {} + Ok(true) | Err(_) => return, + } + if changes.changed().await.is_err() { + return; + } + } + }) + } + + fn set_auto_lock(&self, duration: Duration) { + self.idle + .set(duration, self.operations.current_opened_source().ok()); + } + + fn lock(&self) { + if let Ok(member) = self.operations.current_membership() { + let _ = member.with_current(|| { + self.idle.stop(); + self.slot.lock(); + Ok(()) + }); + } + } + + fn create(&self, _password: Secret) -> VaultFuture<()> { + Self::unsupported() + } + + fn unlock(&self, password: Secret) -> VaultFuture<()> { + let work = match self.operations.membership() { + Ok(work) => work, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let member = work.membership().clone(); + let returned = member.clone(); + let slot = match work.slot() { + Ok(slot) => slot, + Err(error) => return Box::pin(async move { Err(native_failure(error)) }), + }; + let final_slot = slot.clone(); + let idle = self.idle.clone(); + let task = spawn(&self.handle, async move { + let unlocker = Unlocker::new(work); + let receipt = member + .during(unlocker.unlock(password)) + .await + .map_err(native_failure)??; + member + .with_current(|| slot.check_receipt(&member.subject()?, &receipt)) + .map_err(native_failure)?; + if let Ok(native) = member.opened() { + idle.touch(&native); + } + Ok(receipt) + }); + Box::pin(async move { + let receipt = task.await.unwrap_or(Err(VaultError::Cancelled))?; + returned + .with_current(|| final_slot.check_receipt(&returned.subject()?, &receipt)) + .map_err(native_failure) + }) + } + + fn probe_device_unlock(&self) -> VaultFuture { + Self::unsupported() + } + + fn enable_device_unlock(&self) -> VaultFuture<()> { + Self::unsupported() + } + + fn unlock_with_device(&self) -> VaultFuture<()> { + Self::unsupported() + } + + fn unlock_with_device_progress(&self) -> (VaultFuture<()>, progress::UnboundedReceiver) { + let (_, steps) = progress::unbounded(); + (Self::unsupported(), steps) + } + + fn disable_device_unlock(&self) -> VaultFuture<()> { + Self::unsupported() + } + + fn change_password(&self, _password: Secret) -> VaultFuture<()> { + Self::unsupported() + } + + fn list(&self) -> VaultFuture> { + self.run(|ledger| async move { ledger.list().await }) + } + + fn get(&self, id: CredentialId, binding: CredentialBinding) -> VaultFuture { + self.run(move |ledger| async move { ledger.get(id, binding).await }) + } + + fn put( + &self, + id: Option, + label: String, + binding: CredentialBinding, + secret: Secret, + ) -> VaultFuture { + self.run(move |ledger| async move { ledger.put(id, label, binding, secret).await }) + } + + fn update( + &self, + id: CredentialId, + label: String, + binding: CredentialBinding, + ) -> VaultFuture<()> { + self.run(move |ledger| async move { ledger.update(id, label, binding).await }) + } + + fn delete(&self, id: CredentialId) -> VaultFuture<()> { + self.run(move |ledger| async move { ledger.delete(id).await }) + } +} + +fn native_failure(error: nocterm_hubs_model::HubError) -> VaultError { + match error { + nocterm_hubs_model::HubError::KeysLocked => VaultError::Locked, + nocterm_hubs_model::HubError::NotFound => VaultError::NotFound, + other => ledger::vault_error(crate::store::OpError::Hub(other)), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/codec.rs b/crates/hub-io/src/vault/codec.rs new file mode 100644 index 00000000..c61b8acc --- /dev/null +++ b/crates/hub-io/src/vault/codec.rs @@ -0,0 +1,98 @@ +//! What a vault record holds, as sealed into one object of the vault collection. +//! +//! The plaintext is `{"v":1,"label":…,"binding":…,"secret":…}`. A record this client cannot +//! read (a newer format version, or a binding of a kind it does not know) is +//! [`Decoded::Unrecognized`]: the caller hides it, never overwrites it and never deletes it. + +use nocterm_keys::CollectionKey; +use nocterm_session::Secret; +use nocterm_vault::{CredentialBinding, VaultError}; +use serde::{Deserialize, Serialize}; +use zeroize::{Zeroize as _, Zeroizing}; + +use crate::cache::ObjectCopy; + +/// The kind of the objects that hold credentials. +pub(super) const KIND: &str = "credential"; +/// The only format this client writes and reads. +const FORMAT: u32 = 1; + +#[derive(Serialize, Deserialize)] +struct Wire { + v: u32, + label: String, + binding: serde_json::Value, + secret: String, +} + +#[derive(Deserialize)] +struct Head { + v: u32, +} + +/// A credential read from a record. +pub(super) struct Record { + pub(super) label: String, + pub(super) binding: CredentialBinding, + pub(super) secret: Secret, +} + +pub(super) enum Decoded { + Known(Record), + Unrecognized, +} + +/// The sealed text of a credential. The buffer is wiped when dropped. +pub(super) fn encode( + label: &str, + binding: &CredentialBinding, + secret: &Secret, +) -> Result>, serde_json::Error> { + let mut wire = Wire { + v: FORMAT, + label: label.to_owned(), + binding: serde_json::to_value(binding)?, + secret: secret.expose().to_owned(), + }; + let bytes = serde_json::to_vec(&wire); + wire.secret.zeroize(); + bytes.map(Zeroizing::new) +} + +/// Opens a sealed credential object: `Ok(None)` for a tombstone or an object of another kind, +/// and [`VaultError::Authentication`] for one that does not authenticate. +pub(super) fn open_sealed( + key: &CollectionKey, + collection_id: &str, + object_id: &str, + object: &ObjectCopy, +) -> Result, VaultError> { + if object.deleted || object.kind != KIND { + return Ok(None); + } + let plain = key + .open_object(collection_id, object_id, KIND, &object.ciphertext) + .map_err(|_| VaultError::Authentication)?; + Ok(Some(decode(&plain))) +} + +pub(super) fn decode(plaintext: &[u8]) -> Decoded { + match serde_json::from_slice::(plaintext) { + Ok(Head { v: FORMAT }) => {} + _ => return Decoded::Unrecognized, + } + let Ok(wire) = serde_json::from_slice::(plaintext) else { + return Decoded::Unrecognized; + }; + match serde_json::from_value::(wire.binding) { + Ok(binding) => Decoded::Known(Record { + label: wire.label, + binding, + secret: Secret::new(wire.secret), + }), + Err(_) => Decoded::Unrecognized, + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/codec/tests.rs b/crates/hub-io/src/vault/codec/tests.rs new file mode 100644 index 00000000..32202401 --- /dev/null +++ b/crates/hub-io/src/vault/codec/tests.rs @@ -0,0 +1,104 @@ +use std::path::PathBuf; + +use nocterm_session::Target; + +use super::*; + +fn round_trip(binding: CredentialBinding) { + let bytes = encode("label", &binding, &Secret::new("s3cret")).unwrap(); + match decode(&bytes) { + Decoded::Known(record) => { + assert_eq!(record.label, "label"); + assert_eq!(record.binding, binding); + assert_eq!(record.secret.expose(), "s3cret"); + } + Decoded::Unrecognized => panic!("a record this client wrote must read back"), + } +} + +#[test] +fn every_binding_kind_round_trips() { + round_trip(CredentialBinding::password([Target::new( + "ann", + "example.org", + 22, + )])); + round_trip(CredentialBinding::KeyPassphrase { + path: PathBuf::from("/home/ann/.ssh/id_ed25519"), + }); + round_trip(CredentialBinding::PrivateKey { + public_key: "ssh-ed25519 AAAAC3Nza".into(), + }); +} + +#[test] +fn a_binding_of_an_unknown_kind_is_unrecognized() { + let text = br#"{"v":1,"label":"x","binding":{"kind":"hardware_token","slot":3},"secret":"s"}"#; + assert!(matches!(decode(text), Decoded::Unrecognized)); +} + +#[test] +fn a_newer_format_version_is_unrecognized() { + let text = br#"{"v":2,"label":"x","binding":{"kind":"private_key","public_key":"k"},"secret":"s","extra":1}"#; + assert!(matches!(decode(text), Decoded::Unrecognized)); +} + +#[test] +fn text_that_is_not_a_record_is_unrecognized() { + assert!(matches!(decode(b"not json"), Decoded::Unrecognized)); + assert!(matches!(decode(br#"{"v":1}"#), Decoded::Unrecognized)); +} + +#[test] +fn a_password_the_web_panel_keeps_reads_here_bound_to_its_address() { + // Exactly what the web panel seals (`sealPassword` of `nocterm-keys-wasm`): the port only + // when it is not 22. + for (target, port) in [ + ( + serde_json::json!({"host": "prod.example.com", "user": "ops"}), + 22, + ), + ( + serde_json::json!({"host": "prod.example.com", "port": 2222, "user": "ops"}), + 2222, + ), + ] { + let plain = serde_json::json!({ + "v": 1, + "label": "ops@prod.example.com", + "binding": {"kind": "password", "target": target}, + "secret": "s3cret", + }); + let Decoded::Known(record) = decode(plain.to_string().as_bytes()) else { + panic!("the web panel's record reads here"); + }; + assert_eq!( + record.binding, + CredentialBinding::password([Target::new("ops", "prod.example.com", port)]) + ); + assert_eq!(record.secret.expose(), "s3cret"); + } +} + +#[test] +fn a_key_the_web_panel_keeps_reads_here_bound_to_its_public_half() { + // What `sealSshKey` of `nocterm-keys-wasm` seals: the key without its passphrase. + let public_key = + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID7XjK7UMquFonNQf6mOb5ODK/CIOubLkjrBcI6wLh5+"; + let plain = serde_json::json!({ + "v": 1, + "label": "prod", + "binding": {"kind": "private_key", "public_key": public_key}, + "secret": "-----BEGIN OPENSSH PRIVATE KEY-----\n…\n-----END OPENSSH PRIVATE KEY-----\n", + }); + let Decoded::Known(record) = decode(plain.to_string().as_bytes()) else { + panic!("the web panel's key reads here"); + }; + assert_eq!( + record.binding, + CredentialBinding::PrivateKey { + public_key: public_key.to_owned(), + } + ); + assert_eq!(record.label, "prod"); +} diff --git a/crates/hub-io/src/vault/credentials.rs b/crates/hub-io/src/vault/credentials.rs new file mode 100644 index 00000000..dfa791ce --- /dev/null +++ b/crates/hub-io/src/vault/credentials.rs @@ -0,0 +1,175 @@ +//! Terminal credentials bound to one native account capture, including worker I/O. +use std::sync::{Arc, Weak}; + +use futures::future::BoxFuture; +use nocterm_hubs_model::{HubError, source_identity::LocalAccountSource}; +use nocterm_session::{ + CredentialFuture, CredentialId, CredentialStore, CredentialStoreError, Secret, SecretRequest, +}; +use nocterm_vault::{CredentialBinding, VaultError}; +use tokio::runtime::Handle; + +use super::ledger::Ledger; +use crate::{ + account::{ + operations::{OpenedSource, OperationStores}, + source::LocalSourceProvider, + }, + task::spawn, +}; + +/// Retained by HubAccount so capture can be retried after an ordinary unlock. +pub(crate) struct CredentialCapture { + handle: Handle, + operations: Arc, + idle: Weak, +} + +impl CredentialCapture { + pub(crate) fn new( + handle: Handle, + operations: Arc, + idle: &Arc, + ) -> Self { + Self { + handle, + operations, + idle: Arc::downgrade(idle), + } + } + + pub(crate) fn capture( + &self, + provider: &LocalSourceProvider, + source: Arc, + ) -> Result, HubError> { + source.check_current().map_err(|_| HubError::NotSignedIn)?; + let context = OpenedSource::new(provider.capture_native()?, source)?; + context.check_current().map_err(|_| HubError::NotSignedIn)?; + Ok(Arc::new(CapturedCredentials { + handle: self.handle.clone(), + operations: Arc::downgrade(&self.operations), + context, + idle: self.idle.clone(), + })) + } +} + +struct CapturedCredentials { + handle: Handle, + operations: Weak, + context: Arc, + idle: Weak, +} +impl CapturedCredentials { + fn run( + &self, + work: impl FnOnce(Ledger) -> BoxFuture<'static, Result> + Send + 'static, + ) -> CredentialFuture { + let context = Arc::clone(&self.context); + let returned = Arc::clone(&context); + let operations = self.operations.clone(); + let idle = self.idle.clone(); + let task = spawn(&self.handle, async move { + context + .check_current() + .map_err(|_| CredentialStoreError::Stale)?; + let operations = operations.upgrade().ok_or(CredentialStoreError::Stale)?; + // Only an admitted worker retains keys and the actual original + // client; the facade itself owns weak references to both. + let keys = context + .native() + .keys() + .map_err(|_| CredentialStoreError::Stale)?; + let slot = context + .native() + .slot() + .map_err(|_| CredentialStoreError::Stale)?; + let ops = operations + .opened(context.native(), context.clone()) + .map_err(hub_failure)?; + let ledger = Ledger::captured(slot, ops, context.native().clone()); + let result = work(ledger).await; + context + .check_current() + .map_err(|_| CredentialStoreError::Stale)?; + if let Some(idle) = idle.upgrade() { + idle.touch(context.native()); + } + drop(keys); + result.map_err(failure) + }); + Box::pin(async move { + let result = task.await.ok_or(CredentialStoreError::Stale)?; + // The worker may have finished before this future was first polled. + returned + .check_current() + .map_err(|_| CredentialStoreError::Stale)?; + result + }) + } +} + +fn hub_failure(error: HubError) -> CredentialStoreError { + match error { + HubError::NotSignedIn | HubError::KeysLocked | HubError::Stale => { + CredentialStoreError::Stale + } + other => CredentialStoreError::backend(other), + } +} + +fn failure(error: VaultError) -> CredentialStoreError { + match error { + VaultError::NotFound => CredentialStoreError::NotFound, + VaultError::Locked => CredentialStoreError::Locked, + VaultError::Stale => CredentialStoreError::Stale, + other => CredentialStoreError::backend(other), + } +} + +fn not_storable() -> CredentialFuture { + Box::pin(async { Err(CredentialStoreError::NotStorable) }) +} + +impl CredentialStore for CapturedCredentials { + fn is_available(&self) -> bool { + self.operations.upgrade().is_some() && self.context.check_current().is_ok() + } + + fn wait_available(&self) -> BoxFuture<'static, ()> { + // A capture never revives. A new attempt obtains a new capture after + // unlock/login, instead of waiting on an unrelated future account. + if self.is_available() { + Box::pin(async {}) + } else { + Box::pin(futures::future::pending()) + } + } + + fn get(&self, id: CredentialId, request: &SecretRequest) -> CredentialFuture { + let Some(binding) = CredentialBinding::for_request(request) else { + return not_storable(); + }; + self.run(move |ledger| Box::pin(async move { ledger.get(id, binding).await })) + } + + fn save( + &self, + label: String, + request: &SecretRequest, + secret: Secret, + ) -> CredentialFuture { + let Some(binding) = + CredentialBinding::for_request(request).filter(|_| request.is_storable()) + else { + return not_storable(); + }; + self.run(move |ledger| { + Box::pin(async move { ledger.put(None, label, binding, secret).await }) + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/credentials/tests.rs b/crates/hub-io/src/vault/credentials/tests.rs new file mode 100644 index 00000000..7d2ea2fc --- /dev/null +++ b/crates/hub-io/src/vault/credentials/tests.rs @@ -0,0 +1,348 @@ +use super::*; +use crate::account::testing::{self, NativeAccounts as Fixture}; +use crate::store::memory::StoreBoundary; +use nocterm_hubs_model::source_identity::{LocalAccountPartition, LocalAccountSourceError}; +use nocterm_session::Target; + +fn request() -> SecretRequest { + SecretRequest::Password { + target: Target::new("ann", "example.test", 22), + retry: false, + } +} + +#[tokio::test] +async fn a_captured_store_reads_and_saves_only_its_account() { + let fixture = Fixture::new(); + let store = fixture.store(); + let id = fixture.seed().await; + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret A" + ); + fixture + .subjects() + .renew(fixture.slot().hub().clone(), testing::ACCOUNT_ID, "device"); + assert!(store.is_available()); + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret A" + ); + assert_eq!(fixture.count(0).await, 1); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn a_worker_queued_before_an_account_change_never_looks_up_b() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let pending = store.get(id, &request()); + fixture.login(1); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!(fixture.count(1).await, 0); + assert!(!store.is_available()); +} + +#[tokio::test] +async fn a_queued_save_does_not_follow_a_new_login() { + let fixture = Fixture::new(); + let store = fixture.store(); + let pending = store.save("late".into(), &request(), Secret::new("secret A")); + fixture.login(1); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!(fixture.count(0).await, 0); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn a_late_read_cannot_publish_a_cache_into_b() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let pending = store.get(id, &request()); + entered.wait().await; + fixture.login(1); + let b_id = fixture + .store() + .save("B".into(), &request(), Secret::new("secret B")) + .await + .unwrap(); + release.release(); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!( + fixture + .store() + .get(b_id, &request()) + .await + .unwrap() + .expose(), + "secret B" + ); + fixture.hub(1).set_offline(true); + assert_eq!( + fixture + .store() + .get(b_id, &request()) + .await + .unwrap() + .expose(), + "secret B" + ); +} + +#[tokio::test] +async fn an_admitted_remote_save_stays_on_a_and_cannot_replace_b_cache() { + let fixture = Fixture::new(); + let store = fixture.store(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::Put); + let pending = store.save("late A".into(), &request(), Secret::new("secret A")); + entered.wait().await; + fixture.login(1); + let b_id = fixture + .store() + .save("B".into(), &request(), Secret::new("secret B")) + .await + .unwrap(); + release.release(); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert_eq!(fixture.count(0).await, 1); + assert_eq!(fixture.count(1).await, 1); + fixture.hub(1).set_offline(true); + assert_eq!( + fixture + .store() + .get(b_id, &request()) + .await + .unwrap() + .expose(), + "secret B" + ); +} + +#[tokio::test] +async fn lock_drops_keys_even_while_a_captured_store_is_retained() { + let fixture = Fixture::new(); + let store = fixture.store(); + let keys = Arc::downgrade(&fixture.slot().open().unwrap()); + fixture.slot().lock(); + assert!(keys.upgrade().is_none()); + assert!(!store.is_available()); + assert!(matches!( + store.save("A".into(), &request(), Secret::new("A")).await, + Err(CredentialStoreError::Stale) + )); + fixture.login(0); + assert!(!store.is_available()); + assert!(fixture.store().is_available()); +} + +#[tokio::test] +async fn a_to_b_to_a_and_eviction_never_revive_an_old_store() { + let fixture = Fixture::new(); + let store = fixture.store(); + fixture.login(1); + fixture.login(0); + assert!(!store.is_available()); + assert!(matches!( + store + .get(CredentialId::from_bytes([1; 16]), &request()) + .await, + Err(CredentialStoreError::Stale) + )); + let current = fixture.store(); + fixture.subjects().evict(fixture.slot().hub()); + assert!(!current.is_available()); + assert!(matches!( + current.save("A".into(), &request(), Secret::new("A")).await, + Err(CredentialStoreError::Stale) + )); +} + +#[tokio::test] +async fn same_account_new_login_invalidates_before_replacing_keys() { + let fixture = Fixture::new(); + let store = fixture.store(); + fixture + .subjects() + .sign_in(fixture.slot().hub().clone(), testing::ACCOUNT_ID, "device"); + assert!(!store.is_available()); + assert!(matches!( + store.save("A".into(), &request(), Secret::new("A")).await, + Err(CredentialStoreError::Stale) + )); + assert_eq!(fixture.count(0).await, 0); +} + +#[tokio::test] +async fn a_completed_worker_result_is_rechecked_when_the_future_is_polled() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let keys = Arc::downgrade(&fixture.slot().open().unwrap()); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::Collections); + let pending = store.get(id, &request()); + entered.wait().await; + assert!( + keys.strong_count() > 1, + "the entered worker holds its original key lease" + ); + release.release(); + // On this single-thread executor the final key lease drops in the same + // poll that sends the worker result. Wait for that observable release, + // rather than assuming a number of scheduler yields completed the worker. + tokio::time::timeout(std::time::Duration::from_secs(10), async { + while keys.strong_count() != 1 { + tokio::task::yield_now().await; + } + }) + .await + .expect("the worker released its key lease before result polling"); + fixture.login(1); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); +} + +struct RestrictedSource { + source: Arc, + allowed: std::sync::atomic::AtomicBool, +} +impl RestrictedSource { + fn deny(&self) { + self.allowed + .store(false, std::sync::atomic::Ordering::SeqCst); + } +} +impl LocalAccountSource for RestrictedSource { + fn partition(&self) -> LocalAccountPartition { + self.source.partition() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + self.source.check_current()?; + if self.allowed.load(std::sync::atomic::Ordering::SeqCst) { + Ok(()) + } else { + Err(LocalAccountSourceError::Stale) + } + } +} + +struct DescribedSource(LocalAccountPartition); +impl LocalAccountSource for DescribedSource { + fn partition(&self) -> LocalAccountPartition { + self.0.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + Ok(()) + } +} + +#[tokio::test] +async fn a_caller_partition_cannot_select_credentials_of_another_native_account() { + let fixture = Fixture::new(); + let source = fixture.source(); + let described = Arc::new(DescribedSource(source.partition())); + fixture.login(1); + assert!(matches!( + fixture.capture_credentials(source), + Err(HubError::NotSignedIn) + )); + assert!(matches!( + fixture.capture_credentials(described), + Err(HubError::Stale) + )); + assert!(fixture.store().is_available()); +} + +#[tokio::test] +async fn an_app_availability_wrapper_can_only_restrict_native_credentials() { + let fixture = Fixture::new(); + let restriction = Arc::new(RestrictedSource { + source: fixture.source(), + allowed: true.into(), + }); + let store = fixture.capture_credentials(restriction.clone()).unwrap(); + let id = fixture.seed().await; + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret A" + ); + restriction.deny(); + assert!(!store.is_available()); + assert!(matches!( + store + .save("denied".into(), &request(), Secret::new("secret")) + .await, + Err(CredentialStoreError::Stale) + )); + assert_eq!(fixture.count(0).await, 1); +} + +#[tokio::test] +async fn in_flight_keys_are_dropped_after_a_locked_operation_finishes() { + let fixture = Fixture::new(); + let id = fixture.seed().await; + let store = fixture.store(); + let keys = Arc::downgrade(&fixture.slot().open().unwrap()); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let pending = store.get(id, &request()); + entered.wait().await; + fixture.slot().lock(); + assert!( + keys.upgrade().is_some(), + "only the in-flight worker holds the original keys" + ); + release.release(); + assert!(matches!(pending.await, Err(CredentialStoreError::Stale))); + assert!(keys.upgrade().is_none()); + assert!(!store.is_available()); +} + +#[tokio::test] +async fn interactive_answers_and_private_keys_are_never_saved_from_a_prompt() { + let fixture = Fixture::new(); + let store = fixture.store(); + let requests = [ + SecretRequest::Interactive { + prompt: "MFA".into(), + echo: false, + }, + SecretRequest::PrivateKey { + credential: CredentialId::from_bytes([1; 16]), + public_key: "ssh-ed25519 exact".into(), + }, + ]; + for request in requests { + assert!(matches!( + store + .save("denied".into(), &request, Secret::new("secret")) + .await, + Err(CredentialStoreError::NotStorable) + )); + } + assert_eq!(fixture.count(0).await, 0); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn a_captured_revocation_cannot_erase_replacement_keys_or_cache() { + let fixture = Fixture::new(); + let original = fixture.slot().open().unwrap(); + fixture.login(1); + let store = fixture.store(); + let id = store + .save("B".into(), &request(), Secret::new("secret B")) + .await + .unwrap(); + fixture.slot().erase_if(&original).unwrap(); + fixture.hub(1).set_offline(true); + assert!(store.is_available()); + assert_eq!( + store.get(id, &request()).await.unwrap().expose(), + "secret B" + ); + let current = fixture.slot().open().unwrap(); + fixture.slot().erase_if(¤t).unwrap(); + assert!(!store.is_available()); + assert_eq!(fixture.slot().status(), nocterm_vault::VaultStatus::Missing); +} diff --git a/crates/hub-io/src/vault/idle.rs b/crates/hub-io/src/vault/idle.rs new file mode 100644 index 00000000..02a327d7 --- /dev/null +++ b/crates/hub-io/src/vault/idle.rs @@ -0,0 +1,82 @@ +//! Inactivity belongs to the exact native account whose work completed. +use crate::account::source::CapturedAccount; +use std::{ + sync::{Arc, Mutex, PoisonError}, + time::Duration, +}; +use tokio::{runtime::Handle, task::AbortHandle}; + +#[derive(Default)] +struct IdleState { + limit: Option, + timer: Option, +} + +pub(crate) struct Idle { + handle: Handle, + state: Mutex, +} + +impl Idle { + pub(crate) fn new(handle: Handle) -> Self { + Self { + handle, + state: Mutex::default(), + } + } + fn state(&self) -> std::sync::MutexGuard<'_, IdleState> { + self.state.lock().unwrap_or_else(PoisonError::into_inner) + } + /// Locks after `duration` without use; zero keeps the keys open. + pub(crate) fn set(&self, duration: Duration, original: Option>) { + self.state().limit = (!duration.is_zero()).then_some(duration); + if let Some(original) = original { + self.touch(&original); + } + } + pub(crate) fn touch(&self, original: &Arc) { + // Pool membership stays admitted while the timer state changes. Late A + // cannot abort or restart B's countdown. + let _ = original.publish(|| { + let mut state = self.state(); + if let Some(timer) = state.timer.take() { + timer.abort(); + } + let Some(limit) = state.limit else { + return; + }; + let owner = original.clone(); + state.timer = Some( + self.handle + .spawn(async move { + tokio::time::sleep(limit).await; + let _ = owner.with_current(|keys| { + owner.slot()?.lock_keys_if(keys); + Ok(()) + }); + }) + .abort_handle(), + ); + }); + } + /// Caller holds original native membership admission for explicit lock. + pub(crate) fn stop(&self) { + if let Some(timer) = self.state().timer.take() { + timer.abort(); + } + } +} + +impl Drop for Idle { + fn drop(&mut self) { + if let Some(timer) = self + .state + .get_mut() + .unwrap_or_else(PoisonError::into_inner) + .timer + .take() + { + timer.abort(); + } + } +} diff --git a/crates/hub-io/src/vault/ledger.rs b/crates/hub-io/src/vault/ledger.rs new file mode 100644 index 00000000..b13d6b39 --- /dev/null +++ b/crates/hub-io/src/vault/ledger.rs @@ -0,0 +1,368 @@ +//! The credentials of the vault collection: read, written and deleted as sealed objects. +//! +//! Reads come from the encrypted cache, which every call first tries to bring up to date from +//! the hub; an unreachable hub leaves the cached copy, so credentials stay readable offline. +//! Writes need the hub. A record that does not decode (unknown format or binding) is not +//! listed, cannot be overwritten or deleted, and stays on the hub untouched. A record that +//! does not authenticate is the hub's forgery and is an error, never a silent skip. + +use std::collections::BTreeMap; +use std::sync::Arc; + +use nocterm_hubs_model::HubError; +use nocterm_keys::random_bytes; +use nocterm_session::{CredentialId, Secret}; +use nocterm_vault::{CredentialBinding, CredentialInfo, VaultError}; + +use super::codec::{self, Decoded, KIND}; +use crate::account::{OpenKeys, Slot, source::CapturedAccount}; +mod access; +use super::snapshot_budget::{SnapshotBudget, merge_vault}; +use crate::cache::{ObjectCopy, VaultCopy}; +use crate::classify::unreachable; +use crate::store::{Located, OpError, Store, StoreOps, VAULT_KIND}; +use access::LedgerAccess; + +/// How often a write retries after another device changed the same record. +const WRITE_ATTEMPTS: usize = 2; + +pub(crate) struct Ledger { + access: LedgerAccess, + ops: Arc, +} + +/// A failure of a store call, as the vault reports it. +pub(super) fn vault_error(error: OpError) -> VaultError { + match error { + OpError::Conflict => VaultError::Conflict, + OpError::Hub(HubError::NotSignedIn | HubError::Stale) => VaultError::Stale, + OpError::Damaged(_) => VaultError::Authentication, + OpError::Hub(error) if unreachable(&error) => VaultError::Offline, + OpError::Hub(error) => VaultError::Io(std::io::Error::other(error)), + } +} + +impl Ledger { + #[cfg(test)] + pub(crate) fn new(slot: Arc, ops: Arc) -> Self { + Self { + access: LedgerAccess::live(slot), + ops, + } + } + + pub(crate) fn captured( + slot: Arc, + ops: Arc, + capture: Arc, + ) -> Self { + Self { + access: LedgerAccess::captured(slot, capture), + ops, + } + } + + fn store(&self, keys: &Arc) -> Store { + Store::new(Arc::clone(&self.ops), Arc::clone(keys)) + } + + fn keys(&self) -> Result, VaultError> { + self.access.keys() + } + + /// Brings the cache up to date when the hub is reachable. Only a reachable hub's failure + /// is an error. + async fn sync(&self, keys: &Arc) -> Result<(), VaultError> { + let turn = self.access.slot().refresh_turn(keys).await; + self.keys()?; + if !turn.needed() { + return Ok(()); + } + let store = Store::new(Arc::clone(&self.ops), Arc::clone(keys)); + let result = match store.find(VAULT_KIND).await { + Ok(Some(at)) => self.pull(&store, &at).await, + Ok(None) => Ok(()), + Err(OpError::Hub(error)) if unreachable(&error) => Ok(()), + Err(error) => Err(vault_error(error)), + }; + if result.is_ok() { + turn.complete(); + } + result + } + + /// Reads the changes after the cached position and writes the cache through. + async fn pull(&self, store: &Store, at: &Located) -> Result<(), VaultError> { + let copy = self.access.read(|cache| { + cache + .and_then(|cache| cache.vault.clone()) + .filter(|vault| vault.collection_id == at.id()) + })?; + let mut vault = copy.unwrap_or_else(|| { + Arc::new(VaultCopy { + collection_id: at.id().to_owned(), + key_envelope: at.envelope().to_vec(), + cursor: 0, + objects: BTreeMap::new(), + }) + }); + let mut changed = false; + let mut budget: Option = None; + loop { + let batch = store.changes(at, vault.cursor).await.map_err(vault_error)?; + if !changed + && batch.changes.is_empty() + && !batch.more + && batch.head <= vault.cursor + && vault.key_envelope == at.envelope() + { + return Ok(()); + } + changed = true; + let envelope = at.envelope().to_vec(); + let merged = self + .access + .slot() + .cache_work() + .run(move || Ok(merge_vault(vault, budget, envelope, batch))) + .await + .map_err(|error| VaultError::Io(std::io::Error::other(error)))? + .map_err(vault_error)?; + vault = merged.0; + budget = Some(merged.1); + if !merged.2 { + break; + } + } + self.access.publish_vault(vault).await?; + Ok(()) + } + + /// Opens one cached object: `Ok(None)` when it is a tombstone or another kind, and an + /// error when it does not authenticate. + fn open( + keys: &OpenKeys, + collection: &str, + envelope: &[u8], + object_id: &str, + object: &ObjectCopy, + ) -> Result, VaultError> { + if object.deleted || object.kind != KIND { + return Ok(None); + } + let key = keys + .own_collection(collection, envelope) + .map_err(|_| VaultError::Authentication)?; + codec::open_sealed(&key, collection, object_id, object) + } + + fn cached(&self) -> Result>, VaultError> { + self.access + .read(|cache| cache.and_then(|cache| cache.vault.clone())) + } + + pub(super) async fn list(&self) -> Result, VaultError> { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + self.sync(&keys).await?; + let Some(vault) = self.cached()? else { + return Ok(Vec::new()); + }; + let mut listed = Vec::new(); + for (object_id, object) in &vault.objects { + if let Some(Decoded::Known(record)) = Self::open( + &keys, + &vault.collection_id, + &vault.key_envelope, + object_id, + object, + )? && let Ok(id) = object_id.parse::() + { + listed.push(CredentialInfo { + id, + label: record.label, + binding: record.binding, + }); + } + } + Ok(listed) + } + + pub(super) async fn get( + &self, + id: CredentialId, + binding: CredentialBinding, + ) -> Result { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + self.sync(&keys).await?; + let record = self.record(&keys, &id.to_string())?; + if !record.binding.answers(&binding) { + return Err(VaultError::Binding); + } + Ok(record.secret) + } + + /// Renames the record `id` and binds it as `binding` while its secret stays. + pub(super) async fn update( + &self, + id: CredentialId, + label: String, + binding: CredentialBinding, + ) -> Result<(), VaultError> { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + self.sync(&keys).await?; + let object_id = id.to_string(); + let record = self.record(&keys, &object_id)?; + if !record.binding.may_become(&binding) { + return Err(VaultError::Binding); + } + let plaintext = + codec::encode(&label, &binding, &record.secret).map_err(|_| VaultError::Format)?; + let store = self.store(&keys); + self.write_personal(&keys, &store, &object_id, &plaintext) + .await + } + + /// The readable record `object_id` of the cached vault. + fn record(&self, keys: &Arc, object_id: &str) -> Result { + let personal = self.access.read(|cache| { + let vault = cache?.vault.as_ref()?; + let object = vault + .objects + .get(object_id) + .filter(|object| !object.deleted)?; + Some(( + vault.collection_id.clone(), + vault.key_envelope.clone(), + object.clone(), + )) + })?; + let Some((collection, envelope, object)) = personal else { + return Err(VaultError::NotFound); + }; + match Self::open(keys, &collection, &envelope, object_id, &object)? { + Some(Decoded::Known(record)) => Ok(record), + Some(Decoded::Unrecognized) | None => Err(VaultError::NotFound), + } + } + + pub(super) async fn put( + &self, + id: Option, + label: String, + binding: CredentialBinding, + secret: Secret, + ) -> Result { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + let store = self.store(&keys); + let id = match id { + Some(id) => id, + None => { + CredentialId::from_bytes(random_bytes::<16>().map_err(|_| VaultError::Randomness)?) + } + }; + let object_id = id.to_string(); + let plaintext = codec::encode(&label, &binding, &secret).map_err(|_| VaultError::Format)?; + self.write_personal(&keys, &store, &object_id, &plaintext) + .await?; + Ok(id) + } + + /// Writes a record of the vault. + async fn write_personal( + &self, + keys: &Arc, + store: &Store, + object_id: &str, + plaintext: &[u8], + ) -> Result<(), VaultError> { + for attempt in 1..=WRITE_ATTEMPTS { + let at = store.ensure(VAULT_KIND).await.map_err(vault_error)?; + self.pull(store, &at).await?; + let expected = self.expected(keys, object_id, true)?; + match store.put(&at, KIND, object_id, plaintext, expected).await { + Ok(_) => return self.pull(store, &at).await, + Err(OpError::Conflict) if attempt < WRITE_ATTEMPTS => {} + Err(error) => return Err(vault_error(error)), + } + } + Err(VaultError::Conflict) + } + + pub(super) async fn delete(&self, id: CredentialId) -> Result<(), VaultError> { + let _operation = self.access.operation().await?; + let keys = self.keys()?; + let store = self.store(&keys); + let object_id = id.to_string(); + self.remove_personal(&keys, &store, &object_id).await + } + + /// Deletes a record of the vault. + async fn remove_personal( + &self, + keys: &Arc, + store: &Store, + object_id: &str, + ) -> Result<(), VaultError> { + for attempt in 1..=WRITE_ATTEMPTS { + let at = store + .find(VAULT_KIND) + .await + .map_err(vault_error)? + .ok_or(VaultError::NotFound)?; + self.pull(store, &at).await?; + let expected = self.expected(keys, object_id, false)?; + match store.remove(&at, object_id, expected).await { + Ok(_) => return self.pull(store, &at).await, + Err(OpError::Conflict) if attempt < WRITE_ATTEMPTS => {} + Err(error) => return Err(vault_error(error)), + } + } + Err(VaultError::Conflict) + } + + /// The version a write must expect for `object_id`. A record that is not ours to change + /// (it does not decode) is [`VaultError::Format`] when writing and not found when deleting. + fn expected( + &self, + keys: &OpenKeys, + object_id: &str, + creating: bool, + ) -> Result { + let missing = if creating { + Ok(0) + } else { + Err(VaultError::NotFound) + }; + let Some(vault) = self.cached()? else { + return missing; + }; + let Some(object) = vault.objects.get(object_id) else { + return missing; + }; + if object.deleted { + return if creating { + Ok(object.version) + } else { + Err(VaultError::NotFound) + }; + } + match Self::open( + keys, + &vault.collection_id, + &vault.key_envelope, + object_id, + object, + )? { + Some(Decoded::Known(_)) => Ok(object.version), + _ if creating => Err(VaultError::Format), + _ => Err(VaultError::NotFound), + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/ledger/access.rs b/crates/hub-io/src/vault/ledger/access.rs new file mode 100644 index 00000000..7086b0f2 --- /dev/null +++ b/crates/hub-io/src/vault/ledger/access.rs @@ -0,0 +1,113 @@ +//! The keys and cache a ledger may use; captured operations never follow a new login. +use std::sync::Arc; + +use nocterm_hubs_model::source_identity::LocalAccountSource; +use nocterm_vault::VaultError; + +use crate::account::{OpenKeys, Slot, source::CapturedAccount}; +use crate::cache::{AccountCache, VaultCopy}; + +pub(super) struct LedgerAccess { + slot: Arc, + capture: Option>, +} + +impl LedgerAccess { + pub(super) async fn operation(&self) -> Result { + self.slot + .cache_work() + .operation() + .await + .map_err(|error| VaultError::Io(std::io::Error::other(error))) + } + + pub(super) fn slot(&self) -> &Arc { + &self.slot + } + #[cfg(test)] + pub(super) fn live(slot: Arc) -> Self { + Self { + slot, + capture: None, + } + } + + pub(super) fn captured(slot: Arc, capture: Arc) -> Self { + Self { + slot, + capture: Some(capture), + } + } + + pub(super) fn keys(&self) -> Result, VaultError> { + match &self.capture { + Some(capture) => capture.keys().map_err(|_| VaultError::Stale), + None => self.slot.open().ok_or(VaultError::Locked), + } + } + + pub(super) fn read( + &self, + read: impl FnOnce(Option<&AccountCache>) -> R, + ) -> Result { + match &self.capture { + Some(capture) => { + let keys = self.keys()?; + let result = self.slot.read_cache_if(&keys, |cache| read(Some(cache)))?; + capture.check_current().map_err(|_| VaultError::Stale)?; + Ok(result) + } + None => Ok(self.slot.read_cache(read)), + } + } + + pub(super) async fn publish_vault(&self, vault: Arc) -> Result<(), VaultError> { + let _turn = self.slot.cache_turn().await; + let keys = self.keys()?; + loop { + let original = self.slot.cache_snapshot(); + let Some(mut next) = original.as_deref().cloned() else { + return Err(VaultError::Stale); + }; + if !next.accept_shared_vault(vault.clone()) { + return Ok(()); + } + let next = Arc::new(next); + let draft = self + .slot + .stage_cache(original.clone(), next.as_ref().clone()) + .await; + let result = draft.and_then(|draft| match &self.capture { + Some(capture) => capture.with_current(|_| self.slot.commit_cache_if(&keys, draft)), + None => self.slot.commit_cache_if(&keys, draft), + }); + match result { + Ok(Some(committed)) => { + self.slot.finish_cache(committed).await; + return Ok(()); + } + Ok(None) => {} + Err(error) if cache_write_failed(&error) => { + let accepted = match &self.capture { + Some(capture) => capture.with_current(|_| { + self.slot.accept_memory_cache_if(&keys, &original, next) + }), + None => self.slot.accept_memory_cache_if(&keys, &original, next), + } + .map_err(super::super::native_failure)?; + if accepted { + tracing::warn!(error = %nocterm_core::error::chain(&error), "the vault is current in memory; its offline cache could not be written"); + return Ok(()); + } + } + Err(error) => return Err(super::super::native_failure(error)), + } + } + } +} + +fn cache_write_failed(error: &nocterm_hubs_model::HubError) -> bool { + matches!(error, nocterm_hubs_model::HubError::Credentials { cause } + if matches!(cause.downcast_ref::(), + Some(crate::cache::CacheError::Write(_) | crate::cache::CacheError::Prepare(_)))) +} diff --git a/crates/hub-io/src/vault/ledger/tests.rs b/crates/hub-io/src/vault/ledger/tests.rs new file mode 100644 index 00000000..a01062a0 --- /dev/null +++ b/crates/hub-io/src/vault/ledger/tests.rs @@ -0,0 +1,317 @@ +use std::path::PathBuf; +use std::sync::Arc; + +use nocterm_session::Target; + +use super::codec::KIND; +use super::*; +use crate::account::testing::{second_device, signed_in}; +use crate::store::VAULT_KIND; +use crate::store::memory::MemoryHub; + +struct Device { + ledger: Ledger, + slot: Arc, + _dir: tempfile::TempDir, +} + +fn device(hub: &Arc) -> (Device, nocterm_hub_proto::v2::Keyset) { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + let ledger = Ledger::new(Arc::clone(&slot), hub.clone()); + ( + Device { + ledger, + slot, + _dir: dir, + }, + record, + ) +} + +fn password_binding() -> CredentialBinding { + CredentialBinding::password([Target::new("ann", "example.org", 22)]) +} + +#[tokio::test] +async fn a_saved_credential_is_listed_and_read_back() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let listed = device.ledger.list().await.unwrap(); + assert_eq!(listed.len(), 1); + assert_eq!((listed[0].id, listed[0].label.as_str()), (id, "Work")); + let secret = device.ledger.get(id, password_binding()).await.unwrap(); + assert_eq!(secret.expose(), "pw"); +} + +#[tokio::test] +async fn a_credential_does_not_answer_another_request() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let other = CredentialBinding::KeyPassphrase { + path: PathBuf::from("/k"), + }; + assert!(matches!( + device.ledger.get(id, other).await, + Err(VaultError::Binding) + )); +} + +#[tokio::test] +async fn a_private_key_record_round_trips() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let binding = CredentialBinding::PrivateKey { + public_key: "ssh-ed25519 AAAA".into(), + }; + let id = device + .ledger + .put( + None, + "key".into(), + binding.clone(), + Secret::new("PRIVATE KEY TEXT"), + ) + .await + .unwrap(); + let secret = device.ledger.get(id, binding).await.unwrap(); + assert_eq!(secret.expose(), "PRIVATE KEY TEXT"); +} + +#[tokio::test] +async fn the_hub_holds_only_ciphertext() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + device + .ledger + .put( + None, + "Work label".into(), + password_binding(), + Secret::new("hunter2hunter2"), + ) + .await + .unwrap(); + let collection = hub + .collections(crate::store::VAULT_KIND) + .await + .unwrap() + .remove(0); + let objects = hub.objects(&collection.collection_id); + assert_eq!(objects.len(), 1); + let text = String::from_utf8_lossy(&objects[0].ciphertext).into_owned(); + assert!(!text.contains("hunter2") && !text.contains("Work label")); +} + +#[tokio::test] +async fn credentials_stay_readable_offline_but_cannot_be_written() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + hub.set_offline(true); + assert_eq!(device.ledger.list().await.unwrap().len(), 1); + assert_eq!( + device + .ledger + .get(id, password_binding()) + .await + .unwrap() + .expose(), + "pw" + ); + assert!(matches!( + device + .ledger + .put(None, "New".into(), password_binding(), Secret::new("x")) + .await, + Err(VaultError::Offline) + )); + assert!(matches!( + device.ledger.delete(id).await, + Err(VaultError::Offline) + )); +} + +#[tokio::test] +async fn a_credential_saved_on_one_device_is_listed_on_another() { + let hub = Arc::new(MemoryHub::default()); + let (first, record) = device(&hub); + let id = first + .ledger + .put(None, "Shared".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot) = second_device(dir.path(), &record); + let second = Ledger::new(slot, hub.clone()); + let listed = second.list().await.unwrap(); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0].id, id); + assert_eq!( + second.get(id, password_binding()).await.unwrap().expose(), + "pw" + ); +} + +#[tokio::test] +async fn deleting_leaves_a_tombstone_and_the_list_without_the_credential() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + device.ledger.delete(id).await.unwrap(); + assert!(device.ledger.list().await.unwrap().is_empty()); + assert!(matches!( + device.ledger.get(id, password_binding()).await, + Err(VaultError::NotFound) + )); + assert!(matches!( + device.ledger.delete(id).await, + Err(VaultError::NotFound) + )); +} + +#[tokio::test] +async fn a_locked_vault_reads_and_writes_nothing() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + device.slot.lock(); + assert!(matches!( + device.ledger.list().await, + Err(VaultError::Locked) + )); + assert!(matches!( + device + .ledger + .put(None, "x".into(), password_binding(), Secret::new("x")) + .await, + Err(VaultError::Locked) + )); +} + +#[tokio::test] +async fn a_record_the_hub_altered_is_an_error_not_a_silent_skip() { + let hub = Arc::new(MemoryHub::default()); + let (device, record) = device(&hub); + let id = device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + let collection = hub + .collections(crate::store::VAULT_KIND) + .await + .unwrap() + .remove(0); + hub.tamper(&collection.collection_id, &id.to_string(), vec![9; 64]); + // A device that already holds the record keeps what it read; another reads the altered one. + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot) = second_device(dir.path(), &record); + let other = Ledger::new(slot, hub.clone()); + assert!(matches!( + other.list().await, + Err(VaultError::Authentication) + )); +} + +/// Writes an object the way a newer client would: sealed with the right key but holding a +/// binding this client does not know. +async fn write_foreign(device: &Device, hub: &Arc, object_id: &str, text: &str) { + let keys = device.slot.open().unwrap(); + let store = Store::new(hub.clone(), keys); + let at = store.ensure(VAULT_KIND).await.unwrap(); + store + .put(&at, KIND, object_id, text.as_bytes(), 0) + .await + .unwrap(); +} + +#[tokio::test] +async fn a_record_of_an_unknown_kind_is_hidden_and_never_changed() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let foreign: CredentialId = "00000000000000000000000000000042".parse().unwrap(); + let text = + r#"{"v":1,"label":"token","binding":{"kind":"hardware_token","slot":1},"secret":"s"}"#; + write_foreign(&device, &hub, &foreign.to_string(), text).await; + let before = { + let collection = hub.collections(VAULT_KIND).await.unwrap().remove(0); + hub.objects(&collection.collection_id) + }; + + assert!(device.ledger.list().await.unwrap().is_empty()); + assert!(matches!( + device.ledger.get(foreign, password_binding()).await, + Err(VaultError::NotFound) + )); + assert!(matches!( + device.ledger.delete(foreign).await, + Err(VaultError::NotFound) + )); + assert!(matches!( + device + .ledger + .put( + Some(foreign), + "mine".into(), + password_binding(), + Secret::new("x") + ) + .await, + Err(VaultError::Format) + )); + // Other credentials still work beside it. + device + .ledger + .put(None, "Work".into(), password_binding(), Secret::new("pw")) + .await + .unwrap(); + assert_eq!(device.ledger.list().await.unwrap().len(), 1); + + let collection = hub.collections(VAULT_KIND).await.unwrap().remove(0); + let after = hub.objects(&collection.collection_id); + let kept = after + .iter() + .find(|object| object.object_id == foreign.to_string()) + .unwrap(); + assert_eq!( + kept, + before + .iter() + .find(|o| o.object_id == foreign.to_string()) + .unwrap() + ); + assert!(!kept.deleted); +} + +#[tokio::test] +async fn a_record_of_a_newer_format_is_hidden_and_never_changed() { + let hub = Arc::new(MemoryHub::default()); + let (device, _) = device(&hub); + let foreign: CredentialId = "00000000000000000000000000000043".parse().unwrap(); + let text = + r#"{"v":2,"label":"x","binding":{"kind":"private_key","public_key":"k"},"secret":"s"}"#; + write_foreign(&device, &hub, &foreign.to_string(), text).await; + assert!(device.ledger.list().await.unwrap().is_empty()); + assert!(matches!( + device.ledger.delete(foreign).await, + Err(VaultError::NotFound) + )); +} diff --git a/crates/hub-io/src/vault/snapshot_budget.rs b/crates/hub-io/src/vault/snapshot_budget.rs new file mode 100644 index 00000000..10261d2c --- /dev/null +++ b/crates/hub-io/src/vault/snapshot_budget.rs @@ -0,0 +1,126 @@ +//! Bounds one operation's sealed snapshots before retaining another object or page. +use std::{collections::BTreeMap, sync::Arc}; + +use nocterm_hub_proto::v2; + +use crate::{ + cache::{ObjectCopy, VaultCopy}, + classify::remote, + store::OpError, +}; + +pub(super) fn merge_vault( + mut vault: Arc, + budget: Option, + envelope: Vec, + batch: v2::ChangeBatch, +) -> Result<(Arc, SnapshotBudget, bool), OpError> { + let mut budget = match budget { + Some(budget) => budget, + None => { + let mut budget = SnapshotBudget::default(); + budget.add(&vault.objects)?; + budget + } + }; + let next = Arc::make_mut(&mut vault); + next.key_envelope = envelope; + let more = budget.apply(&mut next.objects, &mut next.cursor, batch)?; + Ok((vault, budget, more)) +} + +const MAX_BYTES: usize = 24 * 1024 * 1024; +const MAX_OBJECTS: usize = 32_768; + +const MAX_PAGES: usize = 1_024; + +#[derive(Debug, thiserror::Error)] +enum SnapshotError { + #[error("the vault snapshot exceeds its memory or page limit")] + Capacity, + #[error("the vault change page does not advance its cursor")] + Progress, +} + +#[derive(Default)] +pub(super) struct SnapshotBudget { + bytes: usize, + objects: usize, + pages: usize, +} + +impl SnapshotBudget { + pub(super) fn add(&mut self, objects: &BTreeMap) -> Result<(), OpError> { + for (id, object) in objects { + self.admit(0, charge(id, object), true)?; + } + Ok(()) + } + + fn admit(&mut self, old: usize, next: usize, fresh: bool) -> Result<(), OpError> { + let bytes = self.bytes.saturating_sub(old).saturating_add(next); + let objects = self.objects.saturating_add(usize::from(fresh)); + if bytes > MAX_BYTES || objects > MAX_OBJECTS { + return Err(OpError::Hub(remote(SnapshotError::Capacity))); + } + self.bytes = bytes; + self.objects = objects; + Ok(()) + } + + pub(super) fn apply( + &mut self, + objects: &mut BTreeMap, + cursor: &mut u64, + batch: v2::ChangeBatch, + ) -> Result { + self.pages += 1; + if self.pages > MAX_PAGES { + return Err(OpError::Hub(remote(SnapshotError::Capacity))); + } + let before = *cursor; + for object in batch.changes { + if object.stream_version <= before || object.stream_version > batch.head { + return Err(OpError::Hub(remote(SnapshotError::Progress))); + } + *cursor = (*cursor).max(object.stream_version); + if objects + .get(&object.object_id) + .is_some_and(|held| held.version > object.version) + { + continue; + } + let next = ObjectCopy { + version: object.version, + kind: object.kind, + ciphertext: object.ciphertext, + deleted: object.deleted, + }; + let old = objects + .get_key_value(&object.object_id) + .map(|(id, old)| charge(id, old)); + self.admit( + old.unwrap_or(0), + charge(&object.object_id, &next), + old.is_none(), + )?; + objects.insert(object.object_id, next); + } + if batch.more && *cursor == before { + return Err(OpError::Hub(remote(SnapshotError::Progress))); + } + if !batch.more { + *cursor = (*cursor).max(batch.head); + } + Ok(batch.more) + } +} + +fn charge(id: &String, object: &ObjectCopy) -> usize { + // Includes tree/node metadata, JSON escaping and base64 expansion, as well + // as retained capacities. This leaves file framing/keyset headroom below 32 MiB. + 256usize + .saturating_add(id.capacity().saturating_mul(6)) + .saturating_add(object.kind.capacity().saturating_mul(6)) + .saturating_add(object.ciphertext.capacity().saturating_mul(4).div_ceil(3)) +} diff --git a/crates/hub-io/src/vault/tests.rs b/crates/hub-io/src/vault/tests.rs new file mode 100644 index 00000000..7d1a06c4 --- /dev/null +++ b/crates/hub-io/src/vault/tests.rs @@ -0,0 +1,135 @@ +use std::path::PathBuf; +use std::sync::Arc; +use std::time::Duration; + +use nocterm_session::Target; + +use super::*; +use crate::account::testing::NativeVault; +use crate::account::testing::{PASSWORD, signed_in}; +use crate::store::memory::MemoryHub; +mod refresh; +mod source_binding; + +fn vault() -> (NativeVault, Arc, tempfile::TempDir) { + let hub = Arc::new(MemoryHub::default()); + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + hub.set_keyset(Some(record)); + (NativeVault::new(slot, hub.clone()), hub, dir) +} + +fn binding() -> CredentialBinding { + CredentialBinding::password([Target::new("ann", "example.org", 22)]) +} + +#[tokio::test(flavor = "multi_thread")] +async fn the_vault_reports_its_status_as_the_account_keys_change() { + let (vault, _hub, _dir) = vault(); + assert_eq!(vault.status(), VaultStatus::Unlocked); + let seen = vault.revision(); + vault.lock(); + assert_eq!(vault.status(), VaultStatus::Locked); + assert!(vault.revision() > seen); + assert!(vault.exists() && !vault.is_unlocked()); + vault.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert_eq!(vault.status(), VaultStatus::Unlocked); +} + +#[tokio::test(flavor = "multi_thread")] +async fn waiting_for_a_change_ends_when_the_vault_locks() { + let (vault, _hub, _dir) = vault(); + let seen = vault.revision(); + let changed = vault.changed(seen); + vault.lock(); + let now = tokio::time::timeout(Duration::from_secs(5), changed) + .await + .unwrap(); + assert!(now > seen); +} + +#[tokio::test(flavor = "multi_thread")] +async fn waiting_for_unlock_ends_when_the_password_is_accepted() { + let (vault, _hub, _dir) = vault(); + vault.lock(); + let waiting = vault.wait_unlocked(); + vault.unlock(Secret::new(PASSWORD)).await.unwrap(); + tokio::time::timeout(Duration::from_secs(5), waiting) + .await + .unwrap(); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_vault_on_a_hub_has_no_password_of_its_own_to_create_or_change() { + let (vault, _hub, _dir) = vault(); + assert!(matches!( + vault.create(Secret::new("a password")).await, + Err(VaultError::Unsupported) + )); + assert!(matches!( + vault.change_password(Secret::new("a password")).await, + Err(VaultError::Unsupported) + )); + assert!(matches!( + vault.enable_device_unlock().await, + Err(VaultError::Unsupported) + )); + assert!(matches!( + vault.unlock_with_device().await, + Err(VaultError::Unsupported) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn credentials_are_saved_and_read_through_the_port() { + let (vault, _hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + assert_eq!(vault.list().await.unwrap().len(), 1); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); + vault.delete(id).await.unwrap(); + assert!(vault.list().await.unwrap().is_empty()); +} + +#[tokio::test(flavor = "multi_thread")] +async fn offline_a_write_is_refused_as_offline() { + let (vault, hub, _dir) = vault(); + hub.set_offline(true); + assert!(matches!( + vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await, + Err(VaultError::Offline) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_locked_vault_does_not_answer() { + let (vault, _hub, _dir) = vault(); + vault.lock(); + assert!(matches!(vault.list().await, Err(VaultError::Locked))); + let other = CredentialBinding::KeyPassphrase { + path: PathBuf::from("/k"), + }; + assert!(matches!( + vault.get(CredentialId::from_bytes([1; 16]), other).await, + Err(VaultError::Locked) + )); +} + +#[tokio::test(flavor = "multi_thread")] +async fn the_vault_locks_itself_after_a_quiet_period() { + let (vault, _hub, _dir) = vault(); + vault.set_auto_lock(Duration::from_millis(60)); + assert_eq!(vault.status(), VaultStatus::Unlocked); + tokio::time::timeout(Duration::from_secs(5), async { + while vault.status() == VaultStatus::Unlocked { + tokio::time::sleep(Duration::from_millis(20)).await; + } + }) + .await + .unwrap(); + assert_eq!(vault.status(), VaultStatus::Locked); +} diff --git a/crates/hub-io/src/vault/tests/refresh.rs b/crates/hub-io/src/vault/tests/refresh.rs new file mode 100644 index 00000000..71158368 --- /dev/null +++ b/crates/hub-io/src/vault/tests/refresh.rs @@ -0,0 +1,212 @@ +//! Refresh ordering and unchanged-cache behavior through the actual native vault port. +use super::*; +use crate::store::memory::StoreBoundary; +use nocterm_hub_proto::v2; + +#[tokio::test(flavor = "multi_thread")] +async fn an_unordered_change_page_after_the_requested_cursor_is_accepted() { + let (vault, hub, _dir) = vault(); + let first = vault + .put(None, "First".into(), binding(), Secret::new("one")) + .await + .unwrap(); + let second = vault + .put(None, "Second".into(), binding(), Secret::new("two")) + .await + .unwrap(); + let collection = hub + .collections_of_kind(crate::store::VAULT_KIND) + .pop() + .unwrap(); + let mut changes = hub.objects(&collection); + assert_eq!(changes.len(), 2); + changes[0].stream_version = 4; + changes[1].stream_version = 3; + hub.script_changes(vec![v2::ChangeBatch { + changes, + head: 4, + more: false, + }]); + assert_eq!(vault.list().await.unwrap().len(), 2); + hub.set_offline(true); + assert_eq!(vault.get(first, binding()).await.unwrap().expose(), "one"); + assert_eq!(vault.get(second, binding()).await.unwrap().expose(), "two"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn unordered_changes_for_one_object_keep_its_newest_version() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("old")) + .await + .unwrap(); + let collection = hub + .collections_of_kind(crate::store::VAULT_KIND) + .pop() + .unwrap(); + let mut old = hub.objects(&collection).pop().unwrap(); + vault + .put(Some(id), "Changed".into(), binding(), Secret::new("new")) + .await + .unwrap(); + let mut new = hub.objects(&collection).pop().unwrap(); + new.stream_version = 4; + new.version = 4; + old.stream_version = 3; + old.version = 3; + hub.script_changes(vec![v2::ChangeBatch { + changes: vec![new, old], + head: 4, + more: false, + }]); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "new"); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "new"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_remote_write_remains_successful_when_only_its_offline_copy_cannot_be_published() { + let (vault, hub, dir) = vault(); + let first = vault + .put(None, "First".into(), binding(), Secret::new("one")) + .await + .unwrap(); + let path = std::fs::read_dir(dir.path()) + .unwrap() + .map(|entry| entry.unwrap().path()) + .find(|path| { + path.extension() + .is_some_and(|extension| extension == "cache") + }) + .unwrap(); + std::fs::remove_file(&path).unwrap(); + std::fs::create_dir(&path).unwrap(); + let second = vault + .put(None, "Second".into(), binding(), Secret::new("two")) + .await + .unwrap(); + hub.set_offline(true); + assert_eq!(vault.get(first, binding()).await.unwrap().expose(), "one"); + assert_eq!(vault.get(second, binding()).await.unwrap().expose(), "two"); + assert_eq!(vault.list().await.unwrap().len(), 2); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_nonadvancing_change_page_is_refused_once_and_preserves_offline_credentials() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let before = hub.change_calls(); + hub.script_changes(vec![v2::ChangeBatch { + changes: Vec::new(), + head: 100, + more: true, + }]); + assert!(matches!(vault.list().await, Err(VaultError::Io(_)))); + assert_eq!(hub.change_calls(), before + 1); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn oversized_snapshot_content_is_refused_before_cache_publication() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + hub.script_changes(vec![v2::ChangeBatch { + changes: vec![v2::StoredObject { + object_id: "new-format".into(), + kind: "future".into(), + stream_version: 2, + version: 1, + ciphertext: vec![0; 19 * 1024 * 1024], + ..Default::default() + }], + head: 2, + more: false, + }]); + assert!(matches!(vault.list().await, Err(VaultError::Io(_)))); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_progressing_but_unending_change_stream_has_a_finite_page_budget() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let before = hub.change_calls(); + hub.script_changes( + (2..=1_026) + .map(|cursor| v2::ChangeBatch { + changes: vec![v2::StoredObject { + object_id: "future-object".into(), + kind: "future".into(), + stream_version: cursor, + version: cursor, + deleted: true, + ..Default::default() + }], + head: cursor, + more: true, + }) + .collect(), + ); + assert!(matches!(vault.list().await, Err(VaultError::Io(_)))); + assert_eq!(hub.change_calls(), before + 1_025); + hub.set_offline(true); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); +} + +#[tokio::test(flavor = "multi_thread")] +async fn a_refresh_started_before_deletion_cannot_restore_the_deleted_offline_credential() { + let (vault, hub, _dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let (entered, release) = hub.pause_next(StoreBoundary::ChangesReady); + let delayed = vault.get(id, binding()); + tokio::time::timeout(Duration::from_secs(5), entered.wait()) + .await + .unwrap(); + vault.delete(id).await.unwrap(); + release.release(); + let _ = delayed.await; + hub.set_offline(true); + assert!(matches!( + vault.get(id, binding()).await, + Err(VaultError::NotFound) + )); + assert!(vault.list().await.unwrap().is_empty()); +} + +#[cfg(unix)] +#[tokio::test(flavor = "multi_thread")] +async fn reading_an_unchanged_vault_does_not_replace_its_cache_file() { + use std::os::unix::fs::MetadataExt as _; + let (vault, _hub, dir) = vault(); + let id = vault + .put(None, "Work".into(), binding(), Secret::new("pw")) + .await + .unwrap(); + let path = std::fs::read_dir(dir.path()) + .unwrap() + .map(|entry| entry.unwrap().path()) + .find(|path| { + path.extension() + .is_some_and(|extension| extension == "cache") + }) + .unwrap(); + let original = std::fs::File::open(&path).unwrap(); + let inode = original.metadata().unwrap().ino(); + assert_eq!(vault.get(id, binding()).await.unwrap().expose(), "pw"); + assert_eq!(vault.list().await.unwrap().len(), 1); + assert_eq!(std::fs::metadata(path).unwrap().ino(), inode); +} diff --git a/crates/hub-io/src/vault/tests/source_binding.rs b/crates/hub-io/src/vault/tests/source_binding.rs new file mode 100644 index 00000000..3d0237d5 --- /dev/null +++ b/crates/hub-io/src/vault/tests/source_binding.rs @@ -0,0 +1,230 @@ +use super::*; +use crate::account::testing::NativeAccounts; +use crate::store::memory::StoreBoundary; + +#[tokio::test] +async fn a_vault_read_queued_before_login_changes_cannot_read_the_replacement_account() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let pending = fixture.vault().get(id, request_binding()); + fixture.login(1); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn queued_vault_writes_and_deletes_refuse_a_replacement_login() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let put = fixture + .vault() + .put(None, "A".into(), request_binding(), Secret::new("A")); + let delete = fixture.vault().delete(id); + fixture.login(1); + assert!(matches!(put.await, Err(VaultError::Stale))); + assert!(matches!(delete.await, Err(VaultError::Stale))); + assert_eq!(fixture.count(0).await, 1); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn late_vault_cache_publication_does_not_install_a_records_in_b() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let pending = fixture.vault().get(id, request_binding()); + entered.wait().await; + fixture.login(1); + let b = fixture.seed().await; + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + fixture.hub(1).set_offline(true); + assert_eq!( + fixture + .vault() + .get(b, request_binding()) + .await + .unwrap() + .expose(), + "secret A" + ); + assert!(matches!( + fixture.vault().get(id, request_binding()).await, + Err(VaultError::NotFound) + )); +} + +#[tokio::test] +async fn completed_vault_work_is_rechecked_before_its_caller_first_polls() { + let fixture = NativeAccounts::new(); + let pending = fixture + .vault() + .put(None, "A".into(), request_binding(), Secret::new("A")); + while fixture.count(0).await == 0 { + tokio::task::yield_now().await; + } + // The native write/pull has no pending boundary, so it finishes on the same poll. + tokio::task::yield_now().await; + fixture.login(1); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert_eq!(fixture.count(1).await, 0); +} + +#[tokio::test] +async fn an_unlock_response_cannot_install_keys_after_a_replacement_login() { + let fixture = NativeAccounts::new(); + fixture.vault().lock(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::KeysetReady); + let pending = fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)); + entered.wait().await; + fixture.login(1); + let current = fixture.source(); + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert!(current.check_current().is_ok()); + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); +} + +#[tokio::test] +async fn an_explicit_lock_cancels_an_unlock_already_waiting_for_its_record() { + let fixture = NativeAccounts::new(); + fixture.vault().lock(); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::KeysetReady); + let pending = fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)); + entered.wait().await; + fixture.vault().lock(); + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert_eq!(fixture.vault().status(), VaultStatus::Locked); + fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)) + .await + .unwrap(); +} + +#[tokio::test] +async fn an_unlock_completed_before_caller_poll_never_publishes_success_for_b() { + let fixture = NativeAccounts::new(); + fixture.vault().lock(); + let revision = fixture.vault().revision(); + let pending = fixture + .vault() + .unlock(Secret::new(crate::account::testing::PASSWORD)); + fixture.vault().changed(revision).await; + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); + fixture.login(1); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert!(fixture.source().check_current().is_ok()); +} + +#[tokio::test] +async fn same_account_relogin_and_a_b_a_never_revive_a_pending_vault_read() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let old = fixture.vault().get(id, request_binding()); + fixture.login(0); + assert!(matches!(old.await, Err(VaultError::Stale))); + let old = fixture.vault().get(id, request_binding()); + fixture.login(1); + fixture.login(0); + assert!(matches!(old.await, Err(VaultError::Stale))); + assert_eq!( + fixture + .vault() + .get(id, request_binding()) + .await + .unwrap() + .expose(), + "secret A" + ); +} + +#[tokio::test(start_paused = true)] +async fn an_old_account_idle_timer_cannot_lock_a_new_account_without_activity() { + let fixture = NativeAccounts::new(); + fixture.vault().set_auto_lock(Duration::from_secs(100)); + tokio::task::yield_now().await; + fixture.login(1); + tokio::time::advance(Duration::from_secs(101)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); +} + +#[tokio::test(start_paused = true)] +async fn a_late_a_completion_cannot_extend_or_cancel_b_idle_deadline() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + fixture.vault().set_auto_lock(Duration::from_secs(100)); + let (entered, release) = fixture.hub(0).pause_next(StoreBoundary::ChangesReady); + let old = fixture.vault().get(id, request_binding()); + entered.wait().await; + fixture.login(1); + fixture.seed().await; + tokio::task::yield_now().await; + tokio::time::advance(Duration::from_secs(40)).await; + release.release(); + assert!(matches!(old.await, Err(VaultError::Stale))); + tokio::time::advance(Duration::from_secs(60)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Locked); +} + +#[tokio::test(start_paused = true)] +async fn captured_terminal_credential_activity_refreshes_only_its_own_idle_timer() { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + fixture.vault().set_auto_lock(Duration::from_secs(100)); + tokio::task::yield_now().await; + let store = fixture.store(); + tokio::time::advance(Duration::from_secs(40)).await; + store + .get( + id, + &nocterm_session::SecretRequest::Password { + target: Target::new("ann", "example.test", 22), + retry: false, + }, + ) + .await + .unwrap(); + tokio::task::yield_now().await; + tokio::time::advance(Duration::from_secs(60)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); + tokio::time::advance(Duration::from_secs(40)).await; + tokio::task::yield_now().await; + assert_eq!(fixture.vault().status(), VaultStatus::Locked); +} + +fn request_binding() -> CredentialBinding { + CredentialBinding::password([Target::new("ann", "example.test", 22)]) +} + +#[tokio::test] +async fn a_delayed_original_revocation_cannot_discard_a_replacement_or_its_idle_owner() { + for replacements in [vec![0], vec![1], vec![1, 0]] { + let fixture = NativeAccounts::new(); + let id = fixture.seed().await; + let (entered, release) = fixture.hub(0).pause_next_error( + StoreBoundary::ChangesReady, + nocterm_hubs_model::HubError::Revoked, + ); + let pending = fixture.vault().get(id, request_binding()); + entered.wait().await; + for index in replacements { + fixture.login(index); + } + let current = fixture.source(); + release.release(); + assert!(matches!(pending.await, Err(VaultError::Stale))); + assert!(current.check_current().is_ok()); + assert_eq!(fixture.vault().status(), VaultStatus::Unlocked); + fixture.seed().await; + assert!(current.check_current().is_ok()); + } +} diff --git a/crates/hub-io/src/vault/unlock.rs b/crates/hub-io/src/vault/unlock.rs new file mode 100644 index 00000000..fc2ab700 --- /dev/null +++ b/crates/hub-io/src/vault/unlock.rs @@ -0,0 +1,112 @@ +//! Unlocking the vault: the typed password opens the keyset, from the hub's record when the +//! hub answers and from the cached record when it does not. + +use std::sync::Arc; + +use nocterm_hub_client::{Keyset, KeysetError, UnlockMethod as KeysetUnlock}; +use nocterm_hub_proto::v2; +use nocterm_keys::{LoginError, LoginSecrets, UnlockKey}; +use nocterm_session::Secret; +use nocterm_vault::VaultError; + +use super::ledger::vault_error; +use super::native_failure as failure; +use crate::account::{OpenKeys, SlotReceipt, operations::MembershipWork}; +use crate::cache::{AccountCache, KeysetCopy, UnlockMethod}; +use crate::classify::unreachable; +use crate::store::OpError; + +pub(super) struct Unlocker { + work: MembershipWork, +} + +impl Unlocker { + pub(super) fn new(work: MembershipWork) -> Self { + Self { work } + } + + pub(super) async fn unlock(&self, password: Secret) -> Result { + self.work.check().map_err(failure)?; + let slot = self.work.slot().map_err(failure)?; + let member = self.work.membership(); + let subject = member.subject().map_err(failure)?; + if slot.open_for(&subject).is_ok() { + return Ok(self.work.receipt().clone()); + } + let mut cached = member + .with_current(|| slot.cache_for(&subject, self.work.receipt())) + .map_err(failure)?; + let record = self.record(&cached).await?; + let unlock = derive(&cached, &record, password).await?; + self.work.check().map_err(failure)?; + let keyset = + Keyset::open(&record, &cached.account_id, &unlock).map_err(|error| match error { + KeysetError::WrongUnlockKey | KeysetError::Damaged(_) => VaultError::Authentication, + _ => VaultError::Format, + })?; + cached.keyset = KeysetCopy::of(&record); + let _turn = slot.cache_turn().await; + self.work.check().map_err(failure)?; + let original = slot.cache_snapshot(); + if let Some(latest) = original.as_deref() { + cached.vault = latest.vault.clone(); + cached.recovery_unconfirmed = latest.recovery_unconfirmed; + } + let keys = Arc::new(OpenKeys::new(keyset)); + let draft = slot.stage_cache(original, cached).await.map_err(failure)?; + let (receipt, committed) = member + .with_current(|| slot.commit_install(&subject, self.work.receipt(), keys, draft)) + .map_err(failure)? + .ok_or(VaultError::Stale)?; + slot.finish_cache(committed).await; + slot.notice().raise(); + Ok(receipt) + } + + /// The keyset record to open: the hub's when it answers, else the cached copy. + async fn record(&self, cached: &AccountCache) -> Result { + match self.work.ops().keyset().await { + Ok(Some(record)) => Ok(record), + Ok(None) => Ok(cached.keyset.record()), + Err(OpError::Hub(error)) if unreachable(&error) => Ok(cached.keyset.record()), + Err(error) => Err(vault_error(error)), + } + } +} + +/// The unlock key of the typed password, derived the way the keyset records ([`KeysetUnlock`]), +/// not the way the person happened to sign in. Key derivation is slow and memory hard, so it +/// runs on a thread of its own. +async fn derive( + cached: &AccountCache, + record: &v2::Keyset, + password: Secret, +) -> Result { + let method = KeysetUnlock::of(record).map_err(|_| VaultError::Format)?; + let hub_uid = cached.hub_uid.clone(); + // Only a login password needs the user name, which the cache kept from the sign-in. + let username = match &cached.method { + UnlockMethod::Login { username } => Some(username.clone()), + UnlockMethod::Passphrase => None, + }; + let derived = tokio::task::spawn_blocking(move || match (method, username) { + (KeysetUnlock::LoginPassword, Some(username)) => { + LoginSecrets::derive(password.expose(), &hub_uid, &username) + .map(LoginSecrets::into_unlock) + } + (KeysetUnlock::LoginPassword, None) => Err(LoginError::EmptyUsername), + (KeysetUnlock::VaultPassword(kdf), _) => { + UnlockKey::from_vault_password(password.expose(), &kdf) + } + }) + .await + .map_err(|_| VaultError::Cancelled)?; + derived.map_err(|error| match error { + LoginError::PasswordTooShort { .. } => VaultError::Password, + LoginError::EmptyUsername => VaultError::Format, + LoginError::Key(_) => VaultError::Randomness, + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/hub-io/src/vault/unlock/tests.rs b/crates/hub-io/src/vault/unlock/tests.rs new file mode 100644 index 00000000..f423b5b6 --- /dev/null +++ b/crates/hub-io/src/vault/unlock/tests.rs @@ -0,0 +1,88 @@ +use std::sync::Arc; + +use super::*; +use crate::account::{ + Slot, + operations::AccountOperations, + testing::{PASSWORD, native_operations, record, signed_in, unlock}, +}; +use crate::store::memory::MemoryHub; + +fn locked_device( + hub: &Arc, +) -> ( + Unlocker, + Arc, + tempfile::TempDir, + Arc, +) { + let dir = tempfile::tempdir().unwrap(); + let (_accounts, slot, record) = signed_in(dir.path()); + hub.set_keyset(Some(record)); + let operations = native_operations(&slot, hub.clone()); + slot.lock(); + let unlocker = Unlocker::new(operations.membership().unwrap()); + (unlocker, slot, dir, operations) +} + +#[tokio::test] +async fn the_password_unlocks_with_the_hub_reachable() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + unlocker.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert!(slot.open().is_some()); +} + +#[tokio::test] +async fn the_password_unlocks_with_the_hub_unreachable() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + hub.set_offline(true); + unlocker.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert!(slot.open().is_some()); +} + +#[tokio::test] +async fn a_wrong_password_stays_locked_and_says_so() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + let result = unlocker.unlock(Secret::new("another password")).await; + assert!(matches!(result, Err(VaultError::Authentication))); + assert!(slot.open().is_none()); +} + +#[tokio::test] +async fn a_password_that_is_too_short_is_not_tried() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, _slot, _dir, _operations) = locked_device(&hub); + assert!(matches!( + unlocker.unlock(Secret::new("short")).await, + Err(VaultError::Password) + )); +} + +#[tokio::test] +async fn a_keyset_changed_on_another_device_is_taken_from_the_hub() { + let hub = Arc::new(MemoryHub::default()); + let (unlocker, slot, _dir, _operations) = locked_device(&hub); + // Another device recovered the account: the hub now holds a newer record that the same + // password opens. + let (mut newer, _) = record(&unlock(PASSWORD)); + newer.version = 5; + hub.set_keyset(Some(newer.clone())); + unlocker.unlock(Secret::new(PASSWORD)).await.unwrap(); + assert_eq!(slot.read_cache(|c| c.unwrap().keyset.version), 5); +} + +#[tokio::test] +async fn an_account_without_cached_keys_has_nothing_to_unlock() { + let dir = tempfile::tempdir().unwrap(); + let accounts = crate::account::Accounts::new(dir.path().to_path_buf()); + let slot = accounts.slot(&crate::account::testing::hub()).unwrap(); + let operations = native_operations(&slot, Arc::new(MemoryHub::default())); + let unlocker = Unlocker::new(operations.membership().unwrap()); + assert!(matches!( + unlocker.unlock(Secret::new(PASSWORD)).await, + Err(VaultError::NotFound) + )); +} diff --git a/crates/hub-io/tests/hub.rs b/crates/hub-io/tests/hub.rs new file mode 100644 index 00000000..f5363263 --- /dev/null +++ b/crates/hub-io/tests/hub.rs @@ -0,0 +1,251 @@ +//! End-to-end tests against a real hub. +//! +//! They need a running hub (`docker compose up` in the nocterm-server repository) and these +//! variables: `NOCTERM_HUB_ADDRESS`, `NOCTERM_HUB_FINGERPRINT` (SHA-256 of its certificate), +//! `NOCTERM_HUB_USERNAME` and `NOCTERM_HUB_PASSWORD`. Without `NOCTERM_REQUIRE_HUB` the tests +//! pass vacuously when the variables are missing; with it, missing variables fail the run, so +//! CI cannot silently skip them. +//! +//! The relay test that reaches a destination also needs `NOCTERM_HUB_RELAY_TARGET` (`host:port` +//! of an SSH server the hub is allowed to reach, for example a container of the compose file); +//! without it that test passes vacuously. + +use futures::executor::block_on; +use nocterm_hub_io::{HubLinks, HubLinksConfig, HubRelay}; +use nocterm_hubs_model::{ + Fingerprint, HubAddress, HubEndpointSpec, HubError, HubId, HubSignIn as _, HubTrust as _, + SignedIn, TrustPolicy, +}; +use nocterm_session::{RelayDialer as _, RelayError, RelayRefusal, Secret, Target}; + +#[path = "hub/account.rs"] +mod account; +#[path = "hub/chats.rs"] +mod chats; +#[path = "hub/revocation.rs"] +mod revocation; + +struct Hub { + address: HubAddress, + fingerprint: Fingerprint, + username: String, + password: String, +} + +impl Hub { + fn spec(&self) -> HubEndpointSpec { + HubEndpointSpec { + address: self.address.clone(), + trust: TrustPolicy::Pinned(self.fingerprint), + } + } + + fn id(&self) -> HubId { + HubId::from(self.address.clone()) + } +} + +fn hub() -> Option { + let variable = |name: &str| std::env::var(name).ok(); + let settings = ( + variable("NOCTERM_HUB_ADDRESS"), + variable("NOCTERM_HUB_FINGERPRINT"), + variable("NOCTERM_HUB_USERNAME"), + variable("NOCTERM_HUB_PASSWORD"), + ); + let (Some(address), Some(fingerprint), Some(username), Some(password)) = settings else { + assert!( + std::env::var_os("NOCTERM_REQUIRE_HUB").is_none(), + "NOCTERM_REQUIRE_HUB is set but NOCTERM_HUB_ADDRESS, NOCTERM_HUB_FINGERPRINT, \ + NOCTERM_HUB_USERNAME and NOCTERM_HUB_PASSWORD are not all set" + ); + return None; + }; + Some(Hub { + address: HubAddress::parse(&address).expect("NOCTERM_HUB_ADDRESS"), + fingerprint: Fingerprint::parse(&fingerprint).expect("NOCTERM_HUB_FINGERPRINT"), + username, + password, + }) +} + +fn links(dir: &tempfile::TempDir) -> std::sync::Arc { + HubLinks::start(HubLinksConfig { + credentials_dir: dir.path().join("hubs"), + device_name: "hub-io test".into(), + }) + .unwrap() +} + +/// Signs in with the account's password. The hub limits how often an account signs in and the +/// tests sign in at once, so a refusal for that reason is waited out. +fn sign_in(links: &HubLinks, hub: &Hub) -> Result { + for _ in 0..40 { + let signed = block_on(links.password( + hub.spec(), + hub.username.clone(), + Secret::new(hub.password.clone()), + )); + match signed { + Err(error) if format!("{error:?}").contains("ResourceExhausted") => { + std::thread::sleep(std::time::Duration::from_millis(250)); + } + other => return other, + } + } + panic!("the hub kept refusing to sign {} in", hub.username) +} + +#[test] +fn probe_shows_the_certificate_that_was_pinned() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let probed = block_on(links(&dir).probe(&hub.address)).unwrap(); + assert_eq!(probed.fingerprint, hub.fingerprint); +} + +#[test] +fn another_pinned_certificate_is_a_changed_certificate() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let wrong = Fingerprint::from_bytes([0xAB; 32]); + let spec = HubEndpointSpec { + address: hub.address.clone(), + trust: TrustPolicy::Pinned(wrong), + }; + match block_on(links(&dir).connect(spec)) { + Err(HubError::CertificateChanged { + pinned, presented, .. + }) => { + assert_eq!(pinned, wrong); + assert_eq!(presented, hub.fingerprint); + } + other => panic!("{other:?}"), + } +} + +#[test] +fn sign_in_survives_a_restart_and_sign_out_ends_it() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + + let first = links(&dir); + let info = block_on(first.connect(hub.spec())).unwrap(); + assert!(!info.signed_in); + assert!(info.password_login); + + let refused = block_on(first.password(hub.spec(), hub.username.clone(), Secret::new("wrong"))); + assert!( + matches!(refused, Err(HubError::InvalidCredentials)), + "{refused:?}" + ); + + let account = sign_in(&first, &hub).unwrap().account; + assert_eq!(account.username, hub.username); + drop(first); + + // A new process reads the credentials without a new sign-in. + let second = links(&dir); + let info = block_on(second.connect(hub.spec())).unwrap(); + assert!(info.signed_in); + + block_on(second.sign_out(&hub.id())).unwrap(); + let info = block_on(second.connect(hub.spec())).unwrap(); + assert!(!info.signed_in); +} + +/// Waits for `future` for a while: a relay that never answers must fail the test, not hang it. +fn within(future: impl std::future::Future) -> T { + tokio::runtime::Builder::new_current_thread() + .enable_time() + .build() + .unwrap() + .block_on(async { + tokio::time::timeout(std::time::Duration::from_secs(20), future) + .await + .expect("no answer in time") + }) +} + +fn signed_in_relay(hub: &Hub, dir: &tempfile::TempDir) -> (std::sync::Arc, HubRelay) { + let links = links(dir); + sign_in(&links, hub).unwrap(); + let relay = HubRelay::new(&links); + (links, relay) +} + +fn target(host: &str, port: u16) -> Target { + Target { + host: host.into(), + port, + user: "relay".into(), + } +} + +/// The `host:port` a variable names, or `None` when it is not set. +fn destination(variable: &str) -> Option<(String, u16)> { + let destination = std::env::var(variable).ok()?; + let (host, port) = destination.rsplit_once(':').expect(variable); + Some((host.to_owned(), port.parse().expect(variable))) +} + +/// Reads the server's banner from a relay, then half-closes it and waits for the server to end. +fn expect_banner_then_end(stream: nocterm_session::RelayStream) { + let (to_server, from_server) = stream.into_parts(); + let banner = within(from_server.recv()).expect("the server sends its banner first"); + assert!( + banner.starts_with(b"SSH-"), + "{:?}", + String::from_utf8_lossy(&banner) + ); + // Ending our direction is a half-close: the server, which waits for our banner, closes. + drop(to_server); + within(async { while from_server.recv().await.is_ok() {} }); +} + +#[test] +fn a_relay_reaches_a_server_carries_its_banner_and_ends_with_it() { + let Some(hub) = hub() else { return }; + let Some((host, port)) = destination("NOCTERM_HUB_RELAY_TARGET") else { + return; + }; + let dir = tempfile::tempdir().unwrap(); + let (_links, relay) = signed_in_relay(&hub, &dir); + + // The address is found as the user may type it, not only as it was signed in with. + let address = format!("https://{}/", hub.address); + let stream = within(relay.dial(&address, &target(&host, port))).unwrap(); + expect_banner_then_end(stream); +} + +#[test] +fn a_relay_refuses_the_hubs_own_network_and_the_metadata_service() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let (_links, relay) = signed_in_relay(&hub, &dir); + for (host, port) in [("127.0.0.1", 22), ("169.254.169.254", 80)] { + let refused = within(relay.dial(&hub.address.to_string(), &target(host, port))); + assert!( + matches!( + refused, + Err(RelayError::Refused { + reason: RelayRefusal::Policy | RelayRefusal::PrivateNetwork + }) + ), + "{host}: {refused:?}" + ); + } +} + +#[test] +fn a_relay_needs_a_hub_that_was_signed_in_to_on_this_device() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let relay = HubRelay::new(&links); + let refused = within(relay.dial(&hub.address.to_string(), &target("example.com", 22))); + assert!( + matches!(refused, Err(RelayError::NotSignedIn { .. })), + "{refused:?}" + ); +} diff --git a/crates/hub-io/tests/hub/account.rs b/crates/hub-io/tests/hub/account.rs new file mode 100644 index 00000000..50bcea2b --- /dev/null +++ b/crates/hub-io/tests/hub/account.rs @@ -0,0 +1,321 @@ +//! The keys of an account on a real hub: what one device keeps, another device reads. +//! +//! Two devices are two credential directories. They sign in with the same account, so the +//! second one opens the keyset the first one created. The records the tests write are removed +//! again, and the account is left as it was found. + +use super::{Hub, hub, links, sign_in}; +use futures::executor::block_on; +use nocterm_connections_model::{ + profiles::Profile, + replica::{ProfileReplica as _, RemoteBody}, + test_support::profile_id_for, +}; +use nocterm_hub_io::HubLinks; +use nocterm_hubs_model::{HubSignIn as _, KeyStatus, source_identity::LocalAccountSourceError}; +use nocterm_session::{Auth, Secret}; +use nocterm_vault::{CredentialBinding, VaultAccess as _}; +use std::sync::Arc; + +/// One device: its own credential directory, signed in with the account's password. +pub(super) struct Device { + links: Arc, + login: nocterm_hubs_model::LoginReceipt, + _dir: tempfile::TempDir, +} + +impl Device { + pub(super) fn sign_in(hub: &Hub) -> (Self, KeyStatus) { + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + let signed = sign_in(&links, hub).expect("the account signs in"); + ( + Self { + links, + login: signed.login, + _dir: dir, + }, + signed.keys, + ) + } + + pub(super) fn account(&self, hub: &Hub) -> nocterm_hub_io::HubAccount { + self.links.open_account(&hub.id()).expect("an account") + } +} + +impl Drop for Device { + fn drop(&mut self) { + // The device is not left registered with the account. + if let Some(hub) = hub() { + let _ = block_on(self.links.sign_out(&hub.id())); + } + } +} + +pub(super) fn unique(name: &str) -> String { + format!("{name}-{}", uuid::Uuid::new_v4()) +} + +#[test] +fn the_keys_of_an_account_open_on_a_second_device() { + let Some(hub) = hub() else { return }; + let (_first, keys) = Device::sign_in(&hub); + // The first sign-in of an account makes the keys; any later one opens them. + assert!( + matches!(keys, KeyStatus::Created(_) | KeyStatus::Open), + "{keys:?}" + ); + let (second, keys) = Device::sign_in(&hub); + assert!(matches!(keys, KeyStatus::Open), "{keys:?}"); + assert!(second.account(&hub).vault().is_unlocked()); +} + +#[test] +fn a_retained_account_factory_recovers_after_forget_and_preserves_captured_source_fencing() { + use nocterm_hubs_model::AccountDevices as _; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let original = account.local_source().unwrap(); + let replica = account.local_replica(original.clone()).unwrap(); + let old_device = block_on(device.links.list(&hub.id())) + .unwrap() + .into_iter() + .find(|device| device.is_current) + .unwrap() + .id; + device.links.forget(&hub.id()).unwrap(); + assert!(original.check_current().is_err()); + assert!(account.local_source().is_err()); + // A failed settings save leaves this general factory installed, then the + // same configured hub signs in again through a fresh native membership. + sign_in(&device.links, &hub).unwrap(); + block_on(device.links.revoke(&hub.id(), &old_device)).unwrap(); + let replacement = account.local_source().unwrap(); + assert!(replacement.check_current().is_ok()); + assert_eq!(replacement.partition(), original.partition()); + assert!(account.vault().is_unlocked()); + assert!(original.check_current().is_err()); + assert!(matches!( + block_on(replica.changes(None)), + Err(nocterm_connections_model::replica::ReplicaError::Stale) + )); +} + +#[test] +fn local_history_capture_survives_reconnect_but_not_lock_or_a_replacement_login() { + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let source = account.local_source().unwrap(); + let partition = source.partition(); + block_on(device.links.connect(hub.spec())).unwrap(); + assert_eq!(source.check_current(), Ok(())); + + let vault = account.vault(); + vault.lock(); + assert!( + device.login.check_current().is_ok(), + "a key lock is not a new login" + ); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + block_on(vault.unlock(Secret::new(hub.password.clone()))).unwrap(); + let unlocked = account.local_source().unwrap(); + assert_eq!(unlocked.partition(), partition); + assert_eq!(source.check_current(), Err(LocalAccountSourceError::Stale)); + + sign_in(&device.links, &hub).unwrap(); + assert_eq!( + unlocked.check_current(), + Err(LocalAccountSourceError::Stale) + ); + let next = account.local_source().unwrap(); + assert!(matches!( + device.login.discard(), + Err(nocterm_hubs_model::HubError::Stale) + )); + assert!( + next.check_current().is_ok(), + "old same-account cleanup preserves the replacement" + ); + assert_eq!(next.partition(), partition); + block_on(device.links.sign_out(&hub.id())).unwrap(); + assert_eq!(next.check_current(), Err(LocalAccountSourceError::Stale)); +} + +#[test] +fn a_credential_kept_on_one_device_is_read_on_another() { + let Some(hub) = hub() else { return }; + let (first, _) = Device::sign_in(&hub); + let (second, _) = Device::sign_in(&hub); + let label = unique("live-credential"); + let binding = CredentialBinding::KeyPassphrase { + path: "/live/key".into(), + }; + + let id = block_on(first.account(&hub).vault().put( + None, + label.clone(), + binding.clone(), + Secret::new("hunter2"), + )) + .unwrap(); + let vault = second.account(&hub).vault(); + let listed = block_on(vault.list()).unwrap(); + assert!( + listed + .iter() + .any(|info| info.id == id && info.label == label) + ); + let secret = block_on(vault.get(id, binding)).unwrap(); + assert_eq!(secret.expose(), "hunter2"); + + block_on(first.account(&hub).vault().delete(id)).unwrap(); +} + +#[test] +fn captured_terminal_credentials_keep_the_original_account_and_expire_on_lock_and_login() { + use nocterm_session::{CredentialStoreError, SecretRequest}; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let source = account.local_source().unwrap(); + let store = account.local_credentials(source).unwrap(); + let request = SecretRequest::KeyPassphrase { + path: "/live/captured-key".into(), + retry: false, + }; + let id = block_on(store.save( + unique("captured-secret"), + &request, + Secret::new("original account"), + )) + .unwrap(); + block_on(device.links.connect(hub.spec())).unwrap(); + assert_eq!( + block_on(store.get(id, &request)).unwrap().expose(), + "original account" + ); + account.vault().lock(); + assert!(!store.is_available()); + assert!(matches!( + block_on(store.get(id, &request)), + Err(CredentialStoreError::Stale) + )); + block_on(account.vault().unlock(Secret::new(hub.password.clone()))).unwrap(); + let next = account + .local_credentials(account.local_source().unwrap()) + .unwrap(); + assert_eq!( + block_on(next.get(id, &request)).unwrap().expose(), + "original account" + ); + sign_in(&device.links, &hub).unwrap(); + assert!(matches!( + block_on(next.get(id, &request)), + Err(CredentialStoreError::Stale) + )); + let current = account + .local_credentials(account.local_source().unwrap()) + .unwrap(); + assert_eq!( + block_on(current.get(id, &request)).unwrap().expose(), + "original account" + ); + block_on(account.vault().delete(id)).unwrap(); +} + +#[test] +fn a_profile_saved_on_one_device_is_read_on_another() { + let Some(hub) = hub() else { return }; + let (first, _) = Device::sign_in(&hub); + let (second, _) = Device::sign_in(&hub); + let name = unique("live-profile"); + let profile = Profile { + id: profile_id_for(&name), + name: name.clone(), + description: String::new(), + options: Default::default(), + target: nocterm_session::Target::new("ci", "live.example", 22), + auth: Auth::Password, + credential: None, + launch: None, + icon: None, + icon_color: None, + country: None, + group: None, + jump_hosts: Vec::new(), + }; + + let written = block_on( + first + .account(&hub) + .replica() + .unwrap() + .put(profile.clone(), None), + ) + .unwrap(); + let page = block_on(second.account(&hub).replica().unwrap().changes(None)).unwrap(); + let seen = page + .changes + .iter() + .find(|change| change.id == profile.id) + .expect("the other device sees the profile"); + assert_eq!(seen.body, RemoteBody::Present(Box::new(profile.clone()))); + + block_on( + first + .account(&hub) + .replica() + .unwrap() + .delete(profile.id, written), + ) + .unwrap(); +} + +#[test] +fn captured_relay_cannot_follow_relogin_or_change_its_original_route() { + use nocterm_session::{RelayError, Target}; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let account = device.account(&hub); + let source = account.local_source().unwrap(); + let relay = account.local_relay(source).unwrap(); + let name = hub.address.to_string(); + let target = Target::new("relay", "example.com", 22); + let captured = relay.capture(&name, &target).unwrap(); + let other = Target::new("another-principal", "example.com", 22); + assert!(matches!( + captured.capture(&name, &other), + Err(RelayError::CaptureUnsupported) + )); + sign_in(&device.links, &hub).unwrap(); + let stale = super::within(captured.dial(&name, &target)); + assert!( + matches!(stale, Err(RelayError::NotSignedIn { .. })), + "an old native worker selected replacement membership" + ); + let current = account + .local_relay(account.local_source().unwrap()) + .unwrap(); + current.capture(&name, &target).unwrap(); +} +#[test] +fn an_ordinary_relay_captures_before_its_future_is_polled() { + use nocterm_hub_io::HubRelay; + use nocterm_session::{RelayDialer as _, RelayError, Target}; + let Some(hub) = hub() else { return }; + let (device, _) = Device::sign_in(&hub); + let relay = HubRelay::new(&device.links); + let name = hub.address.to_string(); + let target = Target::new("relay", "example.com", 22); + let original = relay.capture(&name, &target).unwrap(); + sign_in(&device.links, &hub).unwrap(); + assert!(matches!( + super::within(original.dial(&name, &target)), + Err(RelayError::NotSignedIn { .. }) + )); + let current = relay.capture(&name, &target).unwrap(); + drop(current); +} diff --git a/crates/hub-io/tests/hub/chats.rs b/crates/hub-io/tests/hub/chats.rs new file mode 100644 index 00000000..128dc11e --- /dev/null +++ b/crates/hub-io/tests/hub/chats.rs @@ -0,0 +1,170 @@ +//! A hub chat's servers on a real hub: a server attached before the first message is attached +//! to the chat the message creates, every device reopens the chat with it, and detaching it +//! is stored at once. The agent, provider and server it records are removed again. + +use super::{Hub, hub, links, sign_in}; +use futures::executor::block_on; +use nocterm_ai::agents::AgentLaunch; +use nocterm_ai::model::content::Content; +use nocterm_ai::model::controls::{ConfigCategory, ConfigKind, ConfigOption, ConfigSetting}; +use nocterm_ai::model::ids::SessionId; +use nocterm_ai::model::session::{PromptRequest, RestoreSession, SessionOpen, SetConfigOption}; +use nocterm_ai::{AgentCommands, AgentConnector as _, ConnectRequest}; +use nocterm_hub_agents_model::{ + AgentDraft, ApprovalMode, HubAgentDirectory as _, HubServers as _, NewServer, ProviderDraft, + ProviderKind, +}; +use nocterm_hub_io::{HubAgentConnector, HubAgentsLink, HubLinks}; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +/// A host key the hub accepts; no server answers with it, and none needs to. +const HOST_KEY: &str = + "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOMqqnkVzrm0SdG6UOoqKLsabgH5C9okWi0dh2l9GKJl"; + +fn connect(links: &HubLinks, launch: &AgentLaunch) -> Arc { + let request = ConnectRequest { + launch: launch.clone(), + working_directory: std::env::temp_dir(), + terminal_auth: false, + sandbox: None, + resources: Default::default(), + cancellation: Default::default(), + }; + block_on(HubAgentConnector::new(links).connect(request)) + .expect("the hub agent connects") + .commands +} + +/// Whether the options show the server flag `id`, and whether it is on. +fn flag(options: &[ConfigOption], id: &str) -> Option { + options + .iter() + .find(|option| option.id.as_str() == format!("server:{id}")) + .map(|option| { + assert_eq!(option.category, Some(ConfigCategory::Server)); + matches!(option.kind, ConfigKind::Boolean(true)) + }) +} + +/// The chat as another device reopens it, once the hub has created it. +fn reopened(commands: &Arc, chat: &SessionId) -> Vec { + let deadline = Instant::now() + Duration::from_secs(20); + loop { + let restore = RestoreSession { + session: chat.clone(), + working_directory: std::env::temp_dir(), + endpoints: Vec::new(), + fork: false, + }; + match block_on(commands.restore_session(restore)) { + Ok(opened) => return opened.configuration.unwrap_or_default(), + Err(error) if Instant::now() > deadline => panic!("the chat never opened: {error}"), + Err(_) => std::thread::sleep(Duration::from_millis(200)), + } + } +} + +#[test] +fn a_server_attached_before_the_first_message_stays_with_the_chat() { + let Some(hub) = hub() else { return }; + let dir = tempfile::tempdir().unwrap(); + let links = links(&dir); + sign_in(&links, &hub).expect("the account signs in"); + let directory = HubAgentsLink::new(&links); + let (agent, provider, server) = records(&directory, &hub); + let launch = AgentLaunch::hub_agent(hub.id(), agent.id.to_string(), agent.name.clone()); + + let here = connect(&links, &launch); + let open = SessionOpen { + working_directory: std::env::temp_dir(), + endpoints: Vec::new(), + }; + let opened = block_on(here.new_session(open)).expect("a new chat"); + let id = server.id.to_string(); + assert_eq!(flag(&opened.configuration.unwrap(), &id), Some(false)); + let attach = SetConfigOption { + session: opened.session.clone(), + config: format!("server:{id}").into(), + setting: ConfigSetting::Boolean(true), + }; + assert_eq!( + flag(&block_on(here.set_config_option(attach)).unwrap(), &id), + Some(true) + ); + // The first message creates the chat; its answer does not matter here. + let prompt = here.prompt(PromptRequest { + session: opened.session.clone(), + content: vec![Content::text("hello")], + }); + std::thread::spawn(move || drop(block_on(prompt))); + + let elsewhere = connect(&links, &launch); + assert_eq!( + flag(&reopened(&elsewhere, &opened.session), &id), + Some(true) + ); + let detach = SetConfigOption { + session: opened.session.clone(), + config: format!("server:{id}").into(), + setting: ConfigSetting::Boolean(false), + }; + assert_eq!( + flag(&block_on(here.set_config_option(detach)).unwrap(), &id), + Some(false) + ); + let again = connect(&links, &launch); + assert_eq!(flag(&reopened(&again, &opened.session), &id), Some(false)); + + block_on(directory.remove_server(&hub.id(), &server.id)).expect("the server is removed"); + block_on(directory.delete_agent(&hub.id(), &agent.id)).expect("the agent is removed"); + block_on(directory.delete_provider(&hub.id(), &provider)).expect("the provider is removed"); + let _ = block_on(nocterm_hubs_model::HubSignIn::sign_out(&*links, &hub.id())); +} + +/// An agent with a provider, and a server of the account, as the web panel makes them. +fn records( + directory: &HubAgentsLink, + hub: &Hub, +) -> ( + nocterm_hub_agents_model::HubAgent, + nocterm_hub_agents_model::ProviderId, + nocterm_hub_agents_model::HubServer, +) { + let provider = block_on(directory.create_provider( + &hub.id(), + ProviderDraft { + kind: ProviderKind::OpenAi, + name: "Stub".into(), + base_url: "https://models.example/v1".into(), + models: vec!["m1".into()], + api_key: None, + }, + )) + .expect("a provider"); + let agent = block_on(directory.create_agent( + &hub.id(), + AgentDraft { + name: "Ops".into(), + instructions: String::new(), + provider: Some(provider.id.clone()), + model: "m1".into(), + models: vec!["m1".into()], + approval: ApprovalMode::Auto, + servers: Vec::new(), + }, + )) + .expect("an agent"); + let server = block_on(directory.add_server( + &hub.id(), + NewServer { + name: "build".into(), + host: "build.example".into(), + port: 22, + user: "ops".into(), + host_key: HOST_KEY.into(), + }, + )) + .expect("a server"); + (agent, provider.id, server) +} diff --git a/crates/hub-io/tests/hub/revocation.rs b/crates/hub-io/tests/hub/revocation.rs new file mode 100644 index 00000000..e6738eaa --- /dev/null +++ b/crates/hub-io/tests/hub/revocation.rs @@ -0,0 +1,129 @@ +//! Actual server revocation reaches the native conditional cleanup boundary. +use super::{Hub, hub, sign_in}; +use futures::executor::block_on; +use nocterm_connections_model::replica::ProfileReplica as _; +use nocterm_hub_client::{DeviceInfo, HubClient, HubEndpoint, Trust}; +use nocterm_hub_io::{HubLinks, HubLinksConfig}; +use nocterm_hub_proto::v2; +use nocterm_hubs_model::HubSignIn as _; +use nocterm_keys::DeviceKey; +use nocterm_vault::{VaultAccess as _, VaultError, VaultStatus}; +use tokio::runtime::Runtime; + +/// A second real device of the same account, removed again even if an assertion fails. +struct Revoker { + runtime: Runtime, + client: HubClient, + device: String, +} +impl Revoker { + fn sign_in(hub: &Hub) -> Self { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_all() + .build() + .unwrap(); + let endpoint = HubEndpoint::with_trust( + &hub.address.to_string(), + Trust::Pinned(*hub.fingerprint.as_bytes()), + ) + .unwrap(); + let client = runtime + .block_on(HubClient::connect( + &endpoint, + DeviceKey::generate().unwrap(), + None, + )) + .unwrap(); + let device = DeviceInfo::new(super::account::unique("revoker"), "Nocterm test"); + let signed = runtime + .block_on(client.password_login(&hub.username, &hub.password, &device)) + .unwrap(); + Self { + runtime, + client, + device: signed.session.device_id, + } + } + fn revoke_named(&self, name: &str) { + self.runtime.block_on(async { + let listed = self + .client + .account() + .list_devices(v2::Empty {}) + .await + .unwrap() + .into_inner(); + let device = listed + .devices + .into_iter() + .find(|device| device.name == name) + .expect("the native device is registered"); + self.client + .account() + .revoke_device(v2::IdRequest { + id: device.device_id.clone(), + }) + .await + .unwrap(); + let listed = self + .client + .account() + .list_devices(v2::Empty {}) + .await + .unwrap() + .into_inner(); + assert!( + listed + .devices + .iter() + .any(|known| known.device_id == device.device_id && known.revoked), + "the server confirms the native device is revoked" + ); + }); + } +} +impl Drop for Revoker { + fn drop(&mut self) { + let _ = self + .runtime + .block_on(self.client.account().revoke_device(v2::IdRequest { + id: self.device.clone(), + })); + } +} + +#[test] +fn current_server_revocation_erases_only_the_original_native_login_and_keys() { + let Some(hub) = hub() else { + return; + }; + let directory = tempfile::tempdir().unwrap(); + let label = super::account::unique("native-revoked"); + let links = HubLinks::start(HubLinksConfig { + credentials_dir: directory.path().join("hubs"), + device_name: label.clone(), + }) + .unwrap(); + let signed = sign_in(&links, &hub).unwrap(); + let account = links.open_account(&hub.id()).unwrap(); + let source = account.local_source().unwrap(); + let vault = account.vault(); + let revoker = Revoker::sign_in(&hub); + revoker.revoke_named(&DeviceInfo::new(label, "Nocterm").display_name()); + let replica = account.replica().unwrap(); + let denied = block_on(vault.list()); + let remote = block_on(replica.changes(None)).map(|page| page.changes.len()); + let source_current = source.check_current().is_ok(); + assert!( + matches!(&denied, Err(VaultError::Stale)), + "native vault outcome: {:?}; remote-only replica outcome: {remote:?}; original source current: {source_current}", + denied.as_ref().map(Vec::len) + ); + assert!( + matches!(vault.status(), VaultStatus::Missing | VaultStatus::Locked), + "confirmed auth rejection closes native keys; untyped expiry may retain ciphertext" + ); + assert!(source.check_current().is_err()); + assert!(signed.login.check_current().is_err()); + assert!(!block_on(links.connect(hub.spec())).unwrap().signed_in); +} diff --git a/crates/hubs-model/Cargo.toml b/crates/hubs-model/Cargo.toml new file mode 100644 index 00000000..ee143e28 --- /dev/null +++ b/crates/hubs-model/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "nocterm-hubs-model" +description = "Hub addresses and trust rules, the hubs settings section and the hub access ports." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "domain" + +[features] +test-support = [] + +[dependencies] +nocterm-core.workspace = true +nocterm-session.workspace = true +nocterm-settings.workspace = true + +futures.workspace = true +schemars.workspace = true +serde.workspace = true +thiserror.workspace = true + +[dev-dependencies] +toml.workspace = true + +[lints] +workspace = true diff --git a/crates/hubs-model/src/account_keys.rs b/crates/hubs-model/src/account_keys.rs new file mode 100644 index 00000000..9884a2f3 --- /dev/null +++ b/crates/hubs-model/src/account_keys.rs @@ -0,0 +1,47 @@ +//! Whether the keys of an account are open on this device, and the ways to open or close them. +//! +//! Everything that reads the account's encrypted records (the agent access records, the +//! vault) needs open keys. A restart leaves them closed until the user types the password, and +//! the user may close them again at any time. + +use futures::future::BoxFuture; +use nocterm_core::error::SharedCause; +use nocterm_session::Secret; + +use crate::address::HubId; + +/// Why the keys of an account could not be opened. +#[derive(Clone, Debug, thiserror::Error)] +pub enum AccountKeyError { + /// The password does not open the keys. + #[error("the password does not open the account keys")] + WrongPassword, + /// The hub cannot be reached and no copy of the keys' record is kept on this device. + #[error("the hub cannot be reached")] + Offline, + /// This device is not signed in to the hub, or the sign-in ended meanwhile. + #[error("this device is not signed in to the hub")] + NotSignedIn, + /// The account keys cannot be opened for another reason. + #[error("the account keys cannot be opened")] + Failed { + #[source] + cause: SharedCause, + }, +} + +/// A result that completes later. +pub type AccountKeyFuture = BoxFuture<'static, Result>; + +/// The keys of the account on a hub: their state, opening with the password, and closing. +pub trait AccountKeyLock: Send + Sync { + /// Whether the keys of `hub`'s account are open now. + fn is_open(&self, hub: &HubId) -> bool; + + /// Opens the keys with `password`: the login password, or the vault passphrase of an + /// account that signs in through a provider. Already open keys stay open. + fn unlock(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()>; + + /// Closes the keys: records that need them are unreadable until they are opened again. + fn lock(&self, hub: &HubId); +} diff --git a/crates/hubs-model/src/address.rs b/crates/hubs-model/src/address.rs new file mode 100644 index 00000000..64a7ad66 --- /dev/null +++ b/crates/hubs-model/src/address.rs @@ -0,0 +1,182 @@ +//! Where a hub listens: the canonical address that also identifies it. + +use std::fmt; +use std::net::Ipv6Addr; +use std::str::FromStr; + +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::error::HubAddressError; + +/// The port a hub listens on unless the address names another. +pub const DEFAULT_PORT: u16 = 7443; + +const MAX_HOST_LEN: usize = 253; +const MAX_LABEL_LEN: usize = 63; + +/// A hub's `host:port`, lower-case, with the default port made explicit. +/// +/// Only TLS addresses exist: `https://` is accepted and dropped, `http://` is refused. +#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct HubAddress { + host: String, + port: u16, +} + +impl HubAddress { + /// Reads what a person types: `hub.example.com`, `hub.example.com:7443`, + /// `[::1]:7443` or `https://hub.example.com/`. + pub fn parse(text: &str) -> Result { + let text = text.trim(); + if text.is_empty() { + return Err(HubAddressError::Empty); + } + let rest = strip_scheme(text)?; + let rest = rest.trim_end_matches('/'); + let (host, port) = split_host_port(rest)?; + let port = match port { + Some(port) => port + .parse::() + .ok() + .filter(|port| *port != 0) + .ok_or(HubAddressError::InvalidPort)?, + None => DEFAULT_PORT, + }; + let host = host.to_ascii_lowercase(); + if !valid_host(&host) { + return Err(HubAddressError::InvalidHost); + } + Ok(Self { host, port }) + } + + /// The host name or IP literal, lower-case and without brackets. + pub fn host(&self) -> &str { + &self.host + } + + pub fn port(&self) -> u16 { + self.port + } +} + +/// Drops `https://`; refuses plaintext and every other scheme. +fn strip_scheme(text: &str) -> Result<&str, HubAddressError> { + let Some((scheme, rest)) = text.split_once("://") else { + return Ok(text); + }; + if scheme.eq_ignore_ascii_case("https") { + Ok(rest) + } else if scheme.eq_ignore_ascii_case("http") { + Err(HubAddressError::PlaintextRefused) + } else { + Err(HubAddressError::UnsupportedScheme) + } +} + +/// `host` and the port text after it, if any; an IPv6 literal is in brackets. +fn split_host_port(text: &str) -> Result<(&str, Option<&str>), HubAddressError> { + if let Some(rest) = text.strip_prefix('[') { + let (host, after) = rest.split_once(']').ok_or(HubAddressError::InvalidHost)?; + if host.parse::().is_err() { + return Err(HubAddressError::InvalidHost); + } + return match after { + "" => Ok((host, None)), + after => after + .strip_prefix(':') + .map(|port| (host, Some(port))) + .ok_or(HubAddressError::InvalidHost), + }; + } + match text.split_once(':') { + None => Ok((text, None)), + Some((host, port)) if !port.contains(':') => Ok((host, Some(port))), + // A second colon: an IPv6 literal without brackets cannot carry a port. + Some(_) => Err(HubAddressError::InvalidHost), + } +} + +fn valid_host(host: &str) -> bool { + if host.contains(':') { + return host.parse::().is_ok(); + } + host.len() <= MAX_HOST_LEN + && host.split('.').all(|label| { + !label.is_empty() + && label.len() <= MAX_LABEL_LEN + && !label.starts_with('-') + && !label.ends_with('-') + && label + .bytes() + .all(|b| b.is_ascii_alphanumeric() || b == b'-') + }) +} + +impl fmt::Display for HubAddress { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + if self.host.contains(':') { + write!(f, "[{}]:{}", self.host, self.port) + } else { + write!(f, "{}:{}", self.host, self.port) + } + } +} + +impl fmt::Debug for HubAddress { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "HubAddress({self})") + } +} + +impl FromStr for HubAddress { + type Err = HubAddressError; + + fn from_str(text: &str) -> Result { + Self::parse(text) + } +} + +impl Serialize for HubAddress { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_str(self) + } +} + +impl<'de> Deserialize<'de> for HubAddress { + fn deserialize>(deserializer: D) -> Result { + let text = String::deserialize(deserializer)?; + Self::parse(&text).map_err(serde::de::Error::custom) + } +} + +/// The identity of a hub on this device: its canonical address. +#[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(transparent)] +pub struct HubId(HubAddress); + +impl HubId { + pub fn address(&self) -> &HubAddress { + &self.0 + } +} + +impl From for HubId { + fn from(address: HubAddress) -> Self { + Self(address) + } +} + +impl fmt::Display for HubId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.fmt(f) + } +} + +impl fmt::Debug for HubId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "HubId({})", self.0) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/address/tests.rs b/crates/hubs-model/src/address/tests.rs new file mode 100644 index 00000000..3f80b5bd --- /dev/null +++ b/crates/hubs-model/src/address/tests.rs @@ -0,0 +1,109 @@ +use super::*; + +fn parsed(text: &str) -> String { + HubAddress::parse(text).unwrap().to_string() +} + +#[test] +fn a_bare_host_gets_the_default_port_and_lower_case() { + assert_eq!(parsed("Hub.Example.COM"), "hub.example.com:7443"); + assert_eq!(parsed(" hub.home "), "hub.home:7443"); +} + +#[test] +fn an_explicit_port_is_kept() { + let address = HubAddress::parse("hub.home:9000").unwrap(); + assert_eq!(address.host(), "hub.home"); + assert_eq!(address.port(), 9000); + assert_eq!(parsed("10.0.0.5:7443"), "10.0.0.5:7443"); +} + +#[test] +fn ipv6_literals_need_brackets_and_keep_them_when_shown() { + assert_eq!(parsed("[::1]:7443"), "[::1]:7443"); + assert_eq!(parsed("[::1]"), "[::1]:7443"); + assert_eq!(parsed("[FE80::1]:9"), "[fe80::1]:9"); + assert_eq!( + HubAddress::parse("::1"), + Err(HubAddressError::InvalidHost), + "a second colon without brackets is ambiguous" + ); + assert_eq!(HubAddress::parse("[::1"), Err(HubAddressError::InvalidHost)); + assert_eq!( + HubAddress::parse("[not-an-address]:1"), + Err(HubAddressError::InvalidHost) + ); +} + +#[test] +fn https_is_dropped_and_http_is_refused() { + assert_eq!(parsed("https://hub.example.com/"), "hub.example.com:7443"); + assert_eq!( + parsed("HTTPS://hub.example.com:8443"), + "hub.example.com:8443" + ); + assert_eq!( + HubAddress::parse("http://hub.example.com"), + Err(HubAddressError::PlaintextRefused) + ); + assert_eq!( + HubAddress::parse("HTTP://127.0.0.1:7443"), + Err(HubAddressError::PlaintextRefused) + ); + assert_eq!( + HubAddress::parse("ssh://hub.example.com"), + Err(HubAddressError::UnsupportedScheme) + ); +} + +#[test] +fn empty_and_garbage_addresses_are_refused() { + assert_eq!(HubAddress::parse(""), Err(HubAddressError::Empty)); + assert_eq!(HubAddress::parse(" "), Err(HubAddressError::Empty)); + for garbage in [ + ":7443", + "hub example", + "hub.example.com/path", + "user@hub.example.com", + "-hub.example.com", + "hub..example.com", + "hub_example.com", + "hub.example.com.", + ] { + assert_eq!( + HubAddress::parse(garbage), + Err(HubAddressError::InvalidHost), + "{garbage}" + ); + } +} + +#[test] +fn ports_outside_the_range_are_refused() { + for port in ["0", "65536", "-1", "abc", ""] { + assert_eq!( + HubAddress::parse(&format!("hub.home:{port}")), + Err(HubAddressError::InvalidPort), + "{port:?}" + ); + } + assert_eq!(parsed("hub.home:65535"), "hub.home:65535"); +} + +#[test] +fn spellings_of_one_hub_are_one_identity() { + let first = HubId::from(HubAddress::parse("HUB.home").unwrap()); + let second = HubId::from(HubAddress::parse("https://hub.home:7443/").unwrap()); + assert_eq!(first, second); + assert_eq!(first.to_string(), "hub.home:7443"); + assert_ne!( + first, + HubId::from(HubAddress::parse("hub.home:7444").unwrap()) + ); +} + +#[test] +fn an_address_round_trips_through_its_text() { + let address = HubAddress::parse("[::1]:9").unwrap(); + assert_eq!(address.to_string().parse::().unwrap(), address); +} diff --git a/crates/hubs-model/src/color.rs b/crates/hubs-model/src/color.rs new file mode 100644 index 00000000..7711c526 --- /dev/null +++ b/crates/hubs-model/src/color.rs @@ -0,0 +1,54 @@ +//! The colour a server is marked with in menus. + +use schemars::JsonSchema; +use serde::{Deserialize, Serialize}; + +/// A named colour. The views map each name to the current theme, so a server keeps its colour +/// across themes; the names, not the shades, are what the settings keep. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "kebab-case")] +pub enum HubColor { + Red, + Yellow, + Green, + Cyan, + Blue, + Magenta, +} + +impl HubColor { + /// Every colour, in the order a chooser lists them. + pub const ALL: [Self; 6] = [ + Self::Red, + Self::Yellow, + Self::Green, + Self::Cyan, + Self::Blue, + Self::Magenta, + ]; + + /// The name the settings and the views use. + pub fn name(self) -> &'static str { + match self { + Self::Red => "red", + Self::Yellow => "yellow", + Self::Green => "green", + Self::Cyan => "cyan", + Self::Blue => "blue", + Self::Magenta => "magenta", + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn every_name_is_the_one_the_settings_use() { + for color in HubColor::ALL { + let value = toml::Value::try_from(color).unwrap(); + assert_eq!(value, toml::Value::String(color.name().to_owned())); + } + } +} diff --git a/crates/hubs-model/src/devices.rs b/crates/hubs-model/src/devices.rs new file mode 100644 index 00000000..311cac83 --- /dev/null +++ b/crates/hubs-model/src/devices.rs @@ -0,0 +1,36 @@ +//! The devices of an account on a hub, as the Servers page lists and revokes them. + +use crate::address::HubId; +use crate::ports::HubFuture; + +/// One device signed in to the account on a hub. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct DeviceSummary { + /// The hub's identifier of the device. + pub id: String, + /// The name the device gave itself when it signed in. + pub name: String, + /// What kind of client it is, as the hub reports it (`desktop`, `browser`, `hubctl`, ...). + pub platform: String, + /// When the device first signed in, in milliseconds since the Unix epoch. + pub created_at_ms: i64, + /// When the hub last heard from the device, in milliseconds since the Unix epoch. + pub last_seen_at_ms: i64, + /// Whether this is the device asking. + pub is_current: bool, +} + +/// Lists and revokes the devices of the account this device is signed in to on a hub. +/// +/// An implementation captures the original native membership before it returns the future +/// and rechecks it after every await: a result for an account that was signed out, replaced +/// or revoked meanwhile is [`crate::HubError::Stale`], never shown for the new account. +pub trait AccountDevices: Send + Sync { + /// The devices of the account that are not revoked, the current one included. + /// [`crate::HubError::NotSignedIn`] when this device holds no credentials for `hub`. + fn list(&self, hub: &HubId) -> HubFuture>; + + /// Revokes `device`: its tokens stop working. Revoking this device ends its own session; + /// the caller signs out instead. + fn revoke(&self, hub: &HubId, device: &str) -> HubFuture<()>; +} diff --git a/crates/hubs-model/src/environment.rs b/crates/hubs-model/src/environment.rs new file mode 100644 index 00000000..37e843ca --- /dev/null +++ b/crates/hubs-model/src/environment.rs @@ -0,0 +1,43 @@ +//! The environment a window belongs to. + +use crate::address::HubId; + +/// What a window shows: this computer, or the account of one hub. +/// +/// It is chosen when the window is composed and never changes inside a window. +#[derive(Clone, Debug, PartialEq, Eq, Hash)] +pub enum Environment { + Local, + Hub(HubId), +} + +impl Environment { + pub const fn is_local(&self) -> bool { + matches!(self, Self::Local) + } + + /// The hub of a hub window. + pub const fn hub(&self) -> Option<&HubId> { + match self { + Self::Local => None, + Self::Hub(hub) => Some(hub), + } + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::address::HubAddress; + + #[test] + fn a_local_environment_has_no_hub_and_a_hub_environment_names_its_hub() { + let hub = HubId::from(HubAddress::parse("hub.home").unwrap()); + assert!(Environment::Local.is_local()); + assert_eq!(Environment::Local.hub(), None); + let environment = Environment::Hub(hub.clone()); + assert!(!environment.is_local()); + assert_eq!(environment.hub(), Some(&hub)); + assert_ne!(environment, Environment::Local); + } +} diff --git a/crates/hubs-model/src/error.rs b/crates/hubs-model/src/error.rs new file mode 100644 index 00000000..72172783 --- /dev/null +++ b/crates/hubs-model/src/error.rs @@ -0,0 +1,121 @@ +//! Typed failures of hub addresses, fingerprints and hub access. + +use nocterm_core::error::SharedCause; + +use crate::address::HubAddress; +use crate::trust::Fingerprint; + +/// Why a typed hub address was refused. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum HubAddressError { + #[error("the hub address is empty")] + Empty, + #[error("the hub address has no valid host name")] + InvalidHost, + #[error("the hub address has no valid port")] + InvalidPort, + /// `http://` has no certificate to check, so a hub is never reached over it. + #[error("plain http:// hubs are refused; hubs use TLS")] + PlaintextRefused, + #[error("the hub address has an unsupported scheme; use https:// or no scheme")] + UnsupportedScheme, +} + +/// Why a typed certificate fingerprint was refused. +#[derive(Clone, Debug, PartialEq, Eq, thiserror::Error)] +pub enum FingerprintError { + /// A SHA-256 fingerprint has 64 hexadecimal digits; `found` digits were typed. + #[error("a fingerprint has 64 hexadecimal digits, found {found}")] + Length { found: usize }, + #[error("a fingerprint holds only hexadecimal digits")] + NotHex, +} + +/// What went wrong reaching or using a hub, in terms the caller can act on. +/// +/// The original failure stays reachable through `source()`; no variant carries text. +#[derive(Clone, Debug, thiserror::Error)] +pub enum HubError { + /// The hub did not answer. + #[error("cannot reach the hub at {address}")] + Unreachable { + address: HubAddress, + #[source] + cause: SharedCause, + }, + /// The hub presents another certificate than the pinned one. Never retried and never + /// accepted without the user removing the hub and signing in again. + #[error("the certificate of {address} changed")] + CertificateChanged { + address: HubAddress, + pinned: Fingerprint, + presented: Fingerprint, + }, + /// The certificate is not vouched for by a public authority and nothing is pinned. + #[error("the certificate of {address} is not trusted")] + CertificateUntrusted { + address: HubAddress, + presented: Fingerprint, + }, + /// The hub and this client speak different protocol versions. + #[error("the hub speaks protocol {hub}, this client speaks {client}")] + UpgradeRequired { hub: u32, client: u32 }, + /// There are no usable credentials for this hub on this device. + #[error("not signed in to the hub")] + NotSignedIn, + /// The original authenticated account owner was replaced or closed. + #[error("the original account is no longer current")] + Stale, + /// The hub revoked this device; its credentials are gone. + #[error("this device was revoked by the hub")] + Revoked, + #[error("the user name or password was refused")] + InvalidCredentials, + /// The hub does not offer the requested sign-in method. + #[error("the hub does not offer this way of signing in")] + MethodUnavailable, + #[error("signing in took too long")] + SignInTimedOut, + #[error("signing in was cancelled")] + SignInCancelled, + #[error("the hub refused the request for lack of permission")] + PermissionDenied, + #[error("the hub has no such object")] + NotFound, + /// The recovery key is not one of the account, or is not a recovery key at all. + #[error("the recovery key does not open the account")] + RecoveryKeyInvalid, + /// The vault passphrase cannot be used to derive a key (too short). + #[error("the vault passphrase is not acceptable")] + InvalidVaultPassphrase, + /// The account's key record on the hub does not open although the key is right: the hub + /// altered it, or it is too weak to trust. Nothing is overwritten. + #[error("the account's key record on the hub is damaged")] + KeysetDamaged { + #[source] + cause: SharedCause, + }, + /// The keys are protected by the other password than the one that was offered: the login + /// password where a vault passphrase is needed, or the reverse. Whatever is typed, the + /// other password cannot open them. + #[error("the account keys are protected by another password than the one offered")] + UnlockSourceMismatch { needs_login_password: bool }, + /// No recovery is under way for this hub: sign in first. + #[error("there is nothing to recover for this hub")] + NoRecoveryPending, + /// The account keys are not open on this device: unlock the vault or sign in first. + #[error("the account keys are not open on this device")] + KeysLocked, + /// The credential file of this device could not be read or written. + #[error("the credentials of this device cannot be kept")] + Credentials { + #[source] + cause: SharedCause, + }, + /// Any other failure of the hub client. + #[error("the hub request failed")] + Remote { + #[source] + cause: SharedCause, + }, +} diff --git a/crates/hubs-model/src/lib.rs b/crates/hubs-model/src/lib.rs new file mode 100644 index 00000000..022a9445 --- /dev/null +++ b/crates/hubs-model/src/lib.rs @@ -0,0 +1,66 @@ +//! The hubs this device knows and the neutral ports through which the client reaches a hub. +//! +//! Owns: `Environment` (what a window shows: this computer or one hub), hub addresses and ids, +//! certificate fingerprints and the trust decision, captured descriptive hub/account source +//! identities (`source_identity`, distinct from address and certificate observations), +//! the colour that marks a server +//! (`HubColor`), the `[hubs]` settings section (`HubsSettings`), the hub access +//! ports `HubTrust`, `HubSignIn` with their records and `HubError`, +//! including the status of the account keys after sign-in (`KeyStatus`), opaque +//! recovery issue/acknowledgement capabilities (`RecoveryKeyReissue`, `RecoveryKeyOffer`), +//! and `LoginReceipt` for original-login cleanup after delayed settings publication. +//! Does not own: the hub wire protocol and its SDK types (the hub contract crates, reached +//! only by `nocterm-hub-io`), device keys and tokens (private adapter files), connection +//! state over time (`nocterm-hubs`), views. +//! Ports provided: HubTrust, HubSignIn, AccountKeyLock (the account keys and their lock policy) +//! and AccountDevices (the devices of an account, listed and revoked). Ports used: +//! nocterm-session `Secret`. +//! Settings section: `hubs`. Globals: none. +//! Invariants: no I/O; no protocol types; a hub is identified by its canonical address; +//! captured source identities include hub authority and account-key fingerprints, never display +//! addresses or local project ids; their constructors do not authenticate directory values; +//! displayed confirmations carry injected original-recipient capabilities, never authority +//! reconstructed from display ids; stale recipient/action errors stay typed; +//! a pinned fingerprint never changes silently: a different certificate is an error, not a +//! prompt; plaintext addresses are refused; secrets never appear in `HubsSettings`. +//! +//! With the `test-support` feature, `testing::FakeHub` implements the two access ports in +//! memory for consumers' tests. + +//! LocalAccountSource/LocalAccountSourceFactory are consumer ports for captured native +//! account owners. Identity constructors validate shape; adapters authenticate values, and +//! lease checks prevent old owners from returning after sign-out or key replacement. + +pub mod account_keys; +pub mod address; +pub mod color; +pub mod devices; +pub mod environment; +pub mod error; +mod login; +pub use login::{LoginOwner, LoginReceipt}; +pub mod ports; +pub mod records; +mod recovery; +pub use recovery::{ + RecoveryKeyConfirmation, RecoveryKeyIssuer, RecoveryKeyOffer, RecoveryKeyReissue, +}; +pub mod settings; +pub mod source_identity; +pub mod trust; + +#[cfg(any(test, feature = "test-support"))] +pub mod testing; + +pub use account_keys::{AccountKeyError, AccountKeyFuture, AccountKeyLock}; +pub use address::{DEFAULT_PORT, HubAddress, HubId}; +pub use color::HubColor; +pub use devices::{AccountDevices, DeviceSummary}; +pub use environment::Environment; +pub use error::{FingerprintError, HubAddressError, HubError}; +pub use ports::{HubFuture, HubSignIn, HubTrust, OidcSignIn}; +pub use records::{ + Account, AccountId, HubEndpointSpec, HubInfo, KeyStatus, OidcProvider, SignedIn, +}; +pub use settings::{HubRecord, HubsSettings}; +pub use trust::{Fingerprint, ProbedHub, TrustDecision, TrustPolicy, decide}; diff --git a/crates/hubs-model/src/login.rs b/crates/hubs-model/src/login.rs new file mode 100644 index 00000000..ca6b17fb --- /dev/null +++ b/crates/hubs-model/src/login.rs @@ -0,0 +1,41 @@ +//! Conditional cleanup of the original login after a consumer's delayed publication. +use crate::{HubError, HubId}; +use std::{fmt, sync::Arc}; + +/// The adapter retains native membership and local file-login ownership. +pub trait LoginOwner: Send + Sync { + fn check_current(&self) -> Result<(), HubError>; + fn discard(&self) -> Result<(), HubError>; +} +/// A successful login's original owner, independent from the vault key lifecycle. +#[derive(Clone)] +pub struct LoginReceipt { + hub: HubId, + owner: Arc, +} +impl LoginReceipt { + pub fn new(hub: HubId, owner: Arc) -> Self { + Self { hub, owner } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn check_current(&self) -> Result<(), HubError> { + self.owner.check_current() + } + pub fn discard(&self) -> Result<(), HubError> { + self.owner.discard() + } +} +impl fmt::Debug for LoginReceipt { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("LoginReceipt") + .field("hub", &self.hub) + .finish_non_exhaustive() + } +} +impl PartialEq for LoginReceipt { + fn eq(&self, other: &Self) -> bool { + self.hub == other.hub && Arc::ptr_eq(&self.owner, &other.owner) + } +} diff --git a/crates/hubs-model/src/ports.rs b/crates/hubs-model/src/ports.rs new file mode 100644 index 00000000..1ec60623 --- /dev/null +++ b/crates/hubs-model/src/ports.rs @@ -0,0 +1,94 @@ +//! The hub access ports. The application's hub adapter implements them; services use them. +//! +//! Every method returns a runtime-agnostic future that can be awaited on any executor. + +use futures::future::BoxFuture; +use nocterm_session::Secret; + +use crate::address::{HubAddress, HubId}; +use crate::error::HubError; +use crate::records::{HubEndpointSpec, HubInfo, KeyStatus, SignedIn}; +use crate::trust::ProbedHub; + +/// The outcome of one hub call. +pub type HubFuture = BoxFuture<'static, Result>; + +/// Reads the certificate of an address nobody has decided to trust yet. +pub trait HubTrust: Send + Sync { + /// Completes a TLS handshake that accepts any certificate and reports its fingerprint and + /// whether a public authority vouches for it. Sends no credentials and makes no hub call. + fn probe(&self, address: &HubAddress) -> HubFuture; +} + +/// A single sign-in through an identity provider, in two parts. +pub struct OidcSignIn { + /// Resolves with the address the user must open in a browser, once the sign-in listens. + pub browser: HubFuture, + /// Resolves when the browser sign-in finishes. Dropping it cancels the sign-in and closes + /// its local listener. + pub done: HubFuture, +} + +/// Connecting to a hub and signing in or out, with the trust the caller decided. +pub trait HubSignIn: Send + Sync { + /// Connects with the decided trust and reads what the hub says of itself. The returned + /// `signed_in` says whether this device already holds working credentials; expired tokens + /// are renewed on the way. A certificate that differs from the pinned one is + /// [`HubError::CertificateChanged`]; a revoked device erases its credentials and is + /// [`HubError::Revoked`]. + fn connect(&self, endpoint: HubEndpointSpec) -> HubFuture; + + /// Signs in with a user name and password and keeps the credentials on this device. The + /// password also opens the account's keys, or creates them on a first sign-in: the answer + /// says which ([`SignedIn::keys`]). + fn password( + &self, + endpoint: HubEndpointSpec, + username: String, + password: Secret, + ) -> HubFuture; + + /// Signs in through the identity provider `provider` and keeps the credentials. The keys + /// stay closed until [`HubSignIn::set_vault_passphrase`] opens them. + fn oidc(&self, endpoint: HubEndpointSpec, provider: String) -> OidcSignIn; + + /// Opens (or, on the first time, creates) the account's keys with a vault passphrase, for an + /// account that signed in without a password. A passphrase that does not open the keys + /// leaves them waiting for the recovery key ([`KeyStatus::RecoveryNeeded`]). + fn set_vault_passphrase(&self, hub: &HubId, passphrase: Secret) -> HubFuture; + + /// Opens the keys that wait for the recovery key, and makes the secret typed at sign-in + /// open them from now on. [`HubError::RecoveryKeyInvalid`] when the key is not the one. + fn recover(&self, hub: &HubId, recovery_key: Secret) -> HubFuture<()>; + + /// Whether the recovery key made when the account's keys were created on this device was + /// never confirmed as saved, and the keys are open: a new one can be issued. + fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool; + + /// How many times the account keys of any hub were opened, dropped or replaced so far. Start + /// from it to follow [`HubSignIn::keys_changed`]. + fn keys_revision(&self) -> u64; + + /// Resolves with the new count once it differs from `seen`: the keys of some hub were + /// opened (a vault unlock, a sign-in), dropped or replaced, so what depends on them, such as + /// [`HubSignIn::recovery_key_unconfirmed`], should be read again. + fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64>; + + /// Captures the current recipient and pending recovery generation for a displayed action. + fn recovery_key_offer(&self, hub: &HubId) -> Result; + + /// Issues through a capability captured before the future is returned. + fn reissue_recovery_key(&self, hub: &HubId) -> HubFuture { + match self.recovery_key_offer(hub) { + Ok(offer) => offer.issue(), + Err(error) => Box::pin(async move { Err(error) }), + } + } + + /// Ends this device's session on the hub and erases its credentials, even when the hub + /// cannot be reached. + fn sign_out(&self, hub: &HubId) -> HubFuture<()>; + + /// Erases this device's credentials for the hub without contacting it. + fn forget(&self, hub: &HubId) -> Result<(), HubError>; +} diff --git a/crates/hubs-model/src/records.rs b/crates/hubs-model/src/records.rs new file mode 100644 index 00000000..7ac95eef --- /dev/null +++ b/crates/hubs-model/src/records.rs @@ -0,0 +1,104 @@ +//! Plain data the hub access ports exchange. None of it carries a secret. + +use std::fmt; + +use crate::address::HubAddress; +use crate::trust::TrustPolicy; + +macro_rules! text_id { + ($(#[$meta:meta])* $name:ident) => { + $(#[$meta])* + #[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] + pub struct $name(String); + + impl $name { + pub fn new(id: impl Into) -> Self { + Self(id.into()) + } + + pub fn as_str(&self) -> &str { + &self.0 + } + } + + impl fmt::Display for $name { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } + } + }; +} + +text_id!( + /// An account on a hub. + AccountId +); + +/// A hub and the trust decided for it; the adapter refuses a certificate that does not match. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubEndpointSpec { + pub address: HubAddress, + pub trust: TrustPolicy, +} + +/// An identity provider the hub offers for single sign-on. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct OidcProvider { + pub id: String, + pub display_name: String, +} + +/// What a hub says of itself, and whether this device is signed in to it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubInfo { + pub name: String, + /// The hub accepts a user name and password. + pub password_login: bool, + pub oidc_providers: Vec, + /// The hub has an owner. A hub without one cannot be signed in to from this client. + pub bootstrapped: bool, + /// This device holds working credentials for the hub. + pub signed_in: bool, +} + +/// Who this device is signed in as. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Account { + pub id: AccountId, + pub username: String, + pub display_name: String, + /// The account administers the hub (its users, server-wide providers and limits). + pub admin: bool, +} + +/// What a sign-in leaves to do about the account's keys. +/// +/// The keys protect what the account keeps in its vault on the hub (saved servers and their +/// credentials); the hub never holds them. Chats and agents never need them. They are opened with the login password, or with a vault passphrase for an +/// account that signs in through a provider. +#[derive(Clone, Debug, PartialEq)] +pub enum KeyStatus { + /// The keys are open: the account's data on the hub can be read. + Open, + /// The keys are open, but the recovery key made with them was never confirmed as saved + /// (the dialog was closed or the client stopped before): a new one can be issued, because + /// the open keys may replace the recovery envelope. + RecoveryKeyUnconfirmed(crate::RecoveryKeyReissue), + /// The account had no keys; they were made now. The recovery key is the only way back + /// after the password is lost, so it must be shown once and kept nowhere by the client. + Created(crate::RecoveryKeyOffer), + /// A browser sign-in cannot open the keys: the user types the vault passphrase. + PassphraseNeeded, + /// The keys exist but the password does not open them (it was reset): the recovery key + /// does. + RecoveryNeeded, +} + +/// A finished sign-in. +#[derive(Clone, Debug, PartialEq)] +pub struct SignedIn { + pub account: Account, + pub keys: KeyStatus, + /// Original login authority for delayed publication or conditional cleanup. + pub login: crate::LoginReceipt, +} diff --git a/crates/hubs-model/src/recovery.rs b/crates/hubs-model/src/recovery.rs new file mode 100644 index 00000000..4195e190 --- /dev/null +++ b/crates/hubs-model/src/recovery.rs @@ -0,0 +1,85 @@ +//! The displayed recovery key retains its original recipient and acknowledgement. +use crate::{HubError, HubFuture, HubId}; +use nocterm_session::Secret; +use std::{fmt, sync::Arc}; + +/// Native authority for acknowledging exactly one displayed recovery key. +pub trait RecoveryKeyConfirmation: Send + Sync { + fn check_current(&self) -> Result<(), HubError>; + fn confirm(&self) -> HubFuture<()>; +} + +#[derive(Clone)] +pub struct RecoveryKeyOffer { + hub: HubId, + key: Secret, + confirmation: Arc, +} +impl RecoveryKeyOffer { + /// The injected capability establishes authority; this wrapper does not authenticate it. + pub fn new(hub: HubId, key: Secret, confirmation: Arc) -> Self { + Self { + hub, + key, + confirmation, + } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn key(&self) -> &Secret { + &self.key + } + pub fn confirm(&self) -> HubFuture<()> { + self.confirmation.confirm() + } + pub fn check_current(&self) -> Result<(), HubError> { + self.confirmation.check_current() + } +} +impl fmt::Debug for RecoveryKeyOffer { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("RecoveryKeyOffer").finish_non_exhaustive() + } +} +impl PartialEq for RecoveryKeyOffer { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.confirmation, &other.confirmation) + } +} + +/// Native authority to replace the recovery envelope of one original open owner. +pub trait RecoveryKeyIssuer: Send + Sync { + fn check_current(&self) -> Result<(), HubError>; + fn issue(&self) -> HubFuture; +} + +#[derive(Clone)] +pub struct RecoveryKeyReissue { + hub: HubId, + issuer: Arc, +} +impl RecoveryKeyReissue { + pub fn new(hub: HubId, issuer: Arc) -> Self { + Self { hub, issuer } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn issue(&self) -> HubFuture { + self.issuer.issue() + } + pub fn check_current(&self) -> Result<(), HubError> { + self.issuer.check_current() + } +} +impl fmt::Debug for RecoveryKeyReissue { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_struct("RecoveryKeyReissue").finish_non_exhaustive() + } +} +impl PartialEq for RecoveryKeyReissue { + fn eq(&self, other: &Self) -> bool { + self.hub == other.hub && Arc::ptr_eq(&self.issuer, &other.issuer) + } +} diff --git a/crates/hubs-model/src/settings.rs b/crates/hubs-model/src/settings.rs new file mode 100644 index 00000000..998bb99f --- /dev/null +++ b/crates/hubs-model/src/settings.rs @@ -0,0 +1,267 @@ +//! The `[hubs]` section: the servers this device knows. It holds no secrets. +//! +//! ```toml +//! [[hubs.servers]] +//! address = "hub.home:7443" +//! fingerprint = "ab:cd:…" # pins a self-signed certificate +//! label = "Home" +//! color = "blue" # optional: red, yellow, green, cyan, blue or magenta +//! +//! [[hubs.servers]] +//! address = "hub.example.com" +//! trust = "public-ca" # any certificate a public authority vouches for +//! +//! [hubs] +//! key_lock_minutes = 0 # the account keys of a hub stay open until locked or signed out +//! ``` + +use std::collections::HashSet; + +use nocterm_settings::SettingsSection; +use schemars::JsonSchema; +use serde::{Deserialize, Deserializer, Serialize}; + +use crate::address::{HubAddress, HubId}; +use crate::color::HubColor; +use crate::trust::{Fingerprint, TrustPolicy}; + +/// Longest label kept, in characters. +const MAX_LABEL_CHARS: usize = 64; + +/// One known server. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[serde(try_from = "RawRecord", into = "RawRecord")] +#[schemars(with = "RawRecord")] +pub struct HubRecord { + pub address: HubAddress, + pub trust: TrustPolicy, + /// How the server is named in menus; empty shows the address. + pub label: String, + /// The colour that marks the server in menus; none leaves it unmarked. + pub color: Option, +} + +impl HubRecord { + pub fn new(address: HubAddress, trust: TrustPolicy, label: impl Into) -> Self { + Self { + address, + trust, + label: clean_label(&label.into()), + color: None, + } + } + + /// The record marked with `color`. + pub fn with_color(mut self, color: Option) -> Self { + self.color = color; + self + } + + pub fn id(&self) -> HubId { + HubId::from(self.address.clone()) + } + + /// The label, or the address when there is none. + pub fn display_label(&self) -> String { + if self.label.is_empty() { + self.address.to_string() + } else { + self.label.clone() + } + } +} + +fn clean_label(label: &str) -> String { + let printable: String = label.chars().filter(|c| !c.is_control()).collect(); + printable.trim().chars().take(MAX_LABEL_CHARS).collect() +} + +/// The shape of one `[[hubs.servers]]` table. +#[derive(Clone, Debug, Serialize, Deserialize, JsonSchema)] +struct RawRecord { + /// The server as `host` or `host:port`; the port defaults to 7443. + address: String, + /// SHA-256 fingerprint of the server's certificate, in hex with or without colons. + #[serde(default, skip_serializing_if = "Option::is_none")] + fingerprint: Option, + /// `public-ca` trusts certificates that public authorities vouch for. + #[serde(default, skip_serializing_if = "Option::is_none")] + trust: Option, + /// How the server is named in menus. + #[serde(default, skip_serializing_if = "String::is_empty")] + label: String, + /// `red`, `yellow`, `green`, `cyan`, `blue` or `magenta`. A name this version does not know + /// leaves the server unmarked. + #[serde( + default, + deserialize_with = "lenient_color", + skip_serializing_if = "Option::is_none" + )] + #[schemars(with = "Option")] + color: Option, +} + +/// Reads a colour this version knows; anything else is no colour, so a newer file does not +/// make the server unusable. +fn lenient_color<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + #[derive(Deserialize)] + #[serde(untagged)] + enum Entry { + Known(HubColor), + Unusable(serde::de::IgnoredAny), + } + Ok(match Entry::deserialize(deserializer)? { + Entry::Known(color) => Some(color), + Entry::Unusable(_) => None, + }) +} + +#[derive(Clone, Copy, Debug, Serialize, Deserialize, JsonSchema)] +#[serde(rename_all = "kebab-case")] +enum RawTrust { + PublicCa, +} + +/// Why a table is not a usable server. +#[derive(Debug, thiserror::Error)] +enum RecordError { + #[error("invalid address")] + Address(#[from] crate::error::HubAddressError), + #[error("invalid fingerprint")] + Fingerprint(#[from] crate::error::FingerprintError), + #[error("neither a fingerprint nor `trust = \"public-ca\"`")] + NoTrust, +} + +impl TryFrom for HubRecord { + type Error = RecordError; + + fn try_from(raw: RawRecord) -> Result { + // A fingerprint is the stricter statement, so it wins over `trust`. + let trust = match (raw.fingerprint, raw.trust) { + (Some(fingerprint), _) => TrustPolicy::Pinned(Fingerprint::parse(&fingerprint)?), + (None, Some(RawTrust::PublicCa)) => TrustPolicy::PublicCa, + (None, None) => return Err(RecordError::NoTrust), + }; + Ok(Self::new(HubAddress::parse(&raw.address)?, trust, raw.label).with_color(raw.color)) + } +} + +impl From for RawRecord { + fn from(record: HubRecord) -> Self { + let (fingerprint, trust) = match record.trust { + TrustPolicy::Pinned(fingerprint) => (Some(fingerprint.to_string()), None), + TrustPolicy::PublicCa => (None, Some(RawTrust::PublicCa)), + }; + Self { + address: record.address.to_string(), + fingerprint, + trust, + label: record.label, + color: record.color, + } + } +} + +/// The servers this device has signed in to. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize, JsonSchema)] +#[serde(default)] +pub struct HubsSettings { + /// Known servers in the order they were added. A table that is not a usable server is + /// ignored without affecting the others. + #[serde(deserialize_with = "lenient_servers")] + pub servers: Vec, + /// Close the account keys of a hub after this many minutes without vault use (at most + /// 1440); 0 keeps them open until the person locks them or signs out. Independent of the + /// local vault's auto-lock: chats and agents never need the keys. + pub key_lock_minutes: u32, +} + +/// The longest wait before the account keys of a hub close. +pub const MAX_KEY_LOCK_MINUTES: u32 = 1440; + +/// Reads every table that is a usable server and skips the rest. +fn lenient_servers<'de, D: Deserializer<'de>>(deserializer: D) -> Result, D::Error> { + #[derive(Deserialize)] + #[serde(untagged)] + enum Entry { + Server(HubRecord), + Unusable(serde::de::IgnoredAny), + } + let entries = Vec::::deserialize(deserializer)?; + Ok(entries + .into_iter() + .filter_map(|entry| match entry { + Entry::Server(record) => Some(record), + Entry::Unusable(_) => None, + }) + .collect()) +} + +impl HubsSettings { + /// How long the account keys stay open without vault use; `None` keeps them open. + pub fn key_lock(&self) -> Option { + (self.key_lock_minutes > 0) + .then(|| std::time::Duration::from_secs(u64::from(self.key_lock_minutes) * 60)) + } + + pub fn find(&self, hub: &HubId) -> Option<&HubRecord> { + self.servers.iter().find(|record| record.id() == *hub) + } + + /// Adds a server, or replaces the one with the same address in place. + pub fn upsert(&mut self, record: HubRecord) { + match self + .servers + .iter_mut() + .find(|existing| existing.address == record.address) + { + Some(existing) => *existing = record, + None => self.servers.push(record), + } + } + + /// Renames a server; empty shows its address. `true` when it was known. + pub fn set_label(&mut self, hub: &HubId, label: &str) -> bool { + self.edit(hub, |record| record.label = clean_label(label)) + } + + /// Marks a server with a colour, or clears the mark. `true` when it was known. + pub fn set_color(&mut self, hub: &HubId, color: Option) -> bool { + self.edit(hub, |record| record.color = color) + } + + fn edit(&mut self, hub: &HubId, change: impl FnOnce(&mut HubRecord)) -> bool { + match self.servers.iter_mut().find(|record| record.id() == *hub) { + Some(record) => { + change(record); + true + } + None => false, + } + } + + /// Forgets a server; `true` when it was known. + pub fn remove(&mut self, hub: &HubId) -> bool { + let before = self.servers.len(); + self.servers.retain(|record| record.id() != *hub); + self.servers.len() != before + } +} + +impl SettingsSection for HubsSettings { + const KEY: &'static str = "hubs"; + + /// Keeps the first server of each address, shortens labels and bounds the key lock. + fn sanitize(&mut self) { + self.key_lock_minutes = self.key_lock_minutes.min(MAX_KEY_LOCK_MINUTES); + let mut seen = HashSet::new(); + self.servers.retain(|record| seen.insert(record.id())); + for record in &mut self.servers { + record.label = clean_label(&record.label); + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/settings/tests.rs b/crates/hubs-model/src/settings/tests.rs new file mode 100644 index 00000000..8e53fa3a --- /dev/null +++ b/crates/hubs-model/src/settings/tests.rs @@ -0,0 +1,247 @@ +use super::*; + +const PRINT: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"; + +fn load(text: &str) -> HubsSettings { + let table: toml::Table = text.parse().unwrap(); + let mut settings = table + .get("hubs") + .map(|hubs| hubs.clone().try_into::().unwrap()) + .unwrap_or_default(); + settings.sanitize(); + settings +} + +fn pinned(address: &str, label: &str) -> HubRecord { + HubRecord::new( + HubAddress::parse(address).unwrap(), + TrustPolicy::Pinned(Fingerprint::parse(PRINT).unwrap()), + label, + ) +} + +#[test] +fn servers_are_read_with_a_pinned_fingerprint_or_public_trust() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "Hub.Home" +fingerprint = "{PRINT}" +label = "Home" + +[[hubs.servers]] +address = "https://hub.example.com:8443/" +trust = "public-ca" +"# + )); + assert_eq!(settings.servers.len(), 2); + assert_eq!(settings.servers[0], pinned("hub.home:7443", "Home")); + assert_eq!(settings.servers[1].trust, TrustPolicy::PublicCa); + assert_eq!( + settings.servers[1].address.to_string(), + "hub.example.com:8443" + ); + assert_eq!(settings.servers[1].display_label(), "hub.example.com:8443"); +} + +#[test] +fn a_broken_server_does_not_drop_the_others() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "http://plain.example.com" +trust = "public-ca" + +[[hubs.servers]] +address = "no trust.example.com" + +[[hubs.servers]] +address = "hub.home" +fingerprint = "not-hex" + +[[hubs.servers]] +address = "hub.home" +trust = "everyone" + +[[hubs.servers]] +address = "kept.home" +fingerprint = "{PRINT}" +"# + )); + assert_eq!(settings.servers, vec![pinned("kept.home", "")]); +} + +#[test] +fn servers_with_the_same_address_collapse_to_the_first() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "hub.home" +fingerprint = "{PRINT}" +label = "First" + +[[hubs.servers]] +address = "HUB.home:7443" +trust = "public-ca" +label = "Second" +"# + )); + assert_eq!(settings.servers, vec![pinned("hub.home", "First")]); +} + +#[test] +fn a_fingerprint_wins_over_public_trust_in_one_table() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "hub.home" +fingerprint = "{PRINT}" +trust = "public-ca" +"# + )); + assert_eq!(settings.servers, vec![pinned("hub.home", "")]); +} + +#[test] +fn saving_writes_only_the_known_fields() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("hub.home", "Home")); + settings.upsert(HubRecord::new( + HubAddress::parse("hub.example.com").unwrap(), + TrustPolicy::PublicCa, + "", + )); + let text = toml::to_string(&toml::Table::from_iter([( + "hubs".to_owned(), + toml::Value::try_from(&settings).unwrap(), + )])) + .unwrap(); + assert!(text.contains("address = \"hub.home:7443\"")); + assert!(text.contains("trust = \"public-ca\"")); + assert!(!text.contains("label = \"\""), "{text}"); + assert!(!text.contains("token") && !text.contains("seed"), "{text}"); + assert_eq!(load(&text), settings); +} + +#[test] +fn upsert_replaces_in_place_and_remove_forgets() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("a.home", "A")); + settings.upsert(pinned("b.home", "B")); + settings.upsert(pinned("a.home", "Renamed")); + assert_eq!( + settings + .servers + .iter() + .map(HubRecord::display_label) + .collect::>(), + ["Renamed", "B"] + ); + let id = HubId::from(HubAddress::parse("a.home").unwrap()); + assert!(settings.find(&id).is_some()); + assert!(settings.remove(&id)); + assert!(!settings.remove(&id)); + assert!(settings.find(&id).is_none()); +} + +#[test] +fn labels_lose_control_characters_and_are_shortened() { + let long = "x".repeat(200); + let record = pinned("hub.home", &format!(" Ho\nme\t{long}")); + assert_eq!(record.label.chars().count(), 64); + assert!(record.label.starts_with("Home")); + assert!(!record.label.contains('\n')); +} + +#[test] +fn a_missing_section_means_no_servers() { + assert_eq!(load(""), HubsSettings::default()); + assert_eq!(HubsSettings::KEY, "hubs"); +} + +#[test] +fn a_colour_is_read_written_and_survives_a_round_trip() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("hub.home", "Home").with_color(Some(HubColor::Blue))); + settings.upsert(pinned("plain.home", "Plain")); + let text = toml::to_string(&toml::Table::from_iter([( + "hubs".to_owned(), + toml::Value::try_from(&settings).unwrap(), + )])) + .unwrap(); + assert!(text.contains("color = \"blue\""), "{text}"); + assert_eq!( + text.matches("color").count(), + 1, + "unmarked servers write none" + ); + assert_eq!(load(&text), settings); +} + +#[test] +fn a_colour_this_version_does_not_know_leaves_the_server_unmarked_but_listed() { + let settings = load(&format!( + r#" +[[hubs.servers]] +address = "future.home" +fingerprint = "{PRINT}" +color = "chartreuse" + +[[hubs.servers]] +address = "odd.home" +fingerprint = "{PRINT}" +color = 7 +label = "Odd" +unheard_of = true + +[[hubs.servers]] +address = "kept.home" +fingerprint = "{PRINT}" +color = "green" +"# + )); + assert_eq!( + settings.servers, + vec![ + pinned("future.home", ""), + pinned("odd.home", "Odd"), + pinned("kept.home", "").with_color(Some(HubColor::Green)), + ] + ); +} + +#[test] +fn editing_a_server_changes_only_that_server() { + let mut settings = HubsSettings::default(); + settings.upsert(pinned("a.home", "A")); + settings.upsert(pinned("b.home", "B")); + let a = HubId::from(HubAddress::parse("a.home").unwrap()); + let unknown = HubId::from(HubAddress::parse("c.home").unwrap()); + assert!(settings.set_label(&a, " \u{7}Renamed ")); + assert!(settings.set_color(&a, Some(HubColor::Red))); + assert!(!settings.set_label(&unknown, "x")); + assert!(!settings.set_color(&unknown, Some(HubColor::Red))); + assert_eq!( + settings.servers, + vec![ + pinned("a.home", "Renamed").with_color(Some(HubColor::Red)), + pinned("b.home", "B") + ] + ); + assert!(settings.set_color(&a, None)); + assert_eq!(settings.find(&a).unwrap().color, None); +} + +#[test] +fn the_hub_keys_stay_open_by_default_and_their_lock_is_bounded() { + let mut settings = HubsSettings::default(); + assert_eq!(settings.key_lock(), None); + settings.key_lock_minutes = 10_000; + settings.sanitize(); + assert_eq!( + settings.key_lock(), + Some(std::time::Duration::from_secs(1440 * 60)) + ); + let read: HubsSettings = toml::from_str("key_lock_minutes = 30").unwrap(); + assert_eq!(read.key_lock(), Some(std::time::Duration::from_secs(1800))); +} diff --git a/crates/hubs-model/src/source_identity.rs b/crates/hubs-model/src/source_identity.rs new file mode 100644 index 00000000..be4210c6 --- /dev/null +++ b/crates/hubs-model/src/source_identity.rs @@ -0,0 +1,121 @@ +//! Captured descriptive source identity shared by chat, storage and cloud owners. +//! Constructors check shape only; trusted adapters authenticate the supplied values. +use serde::{Deserialize, Serialize}; +use std::fmt; +mod local; +pub use local::{ + LegacyConfiguredAccount, LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, + LocalAccountSourceFactory, +}; + +pub const MAX_NAMESPACE_BYTES: usize = 128; + +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum SourceIdentityError { + #[error("an opaque namespace identifier must contain 1 to 128 UTF-8 bytes")] + NamespaceLength, +} + +/// Supplied digest/fingerprint bytes, not a key validator or signature verifier. +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +pub struct Fingerprint([u8; 32]); +impl Fingerprint { + pub const fn from_bytes(bytes: [u8; 32]) -> Self { + Self(bytes) + } + pub const fn as_bytes(&self) -> &[u8; 32] { + &self.0 + } +} + +#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(try_from = "String", into = "String")] +pub struct NamespaceId(String); +impl NamespaceId { + pub fn new(value: impl Into) -> Result { + let value = value.into(); + if value.is_empty() || value.len() > MAX_NAMESPACE_BYTES { + return Err(SourceIdentityError::NamespaceLength); + } + Ok(Self(value)) + } + pub fn as_str(&self) -> &str { + &self.0 + } +} +impl TryFrom for NamespaceId { + type Error = SourceIdentityError; + fn try_from(value: String) -> Result { + Self::new(value) + } +} +impl From for String { + fn from(value: NamespaceId) -> Self { + value.0 + } +} +impl fmt::Debug for NamespaceId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.debug_tuple("NamespaceId").field(&self.0.len()).finish() + } +} + +/// An instance label bound to its originally authenticated authority, not its address. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct HubIdentity { + instance: NamespaceId, + authority: Fingerprint, +} +impl HubIdentity { + pub fn new(instance: NamespaceId, authority: Fingerprint) -> Self { + Self { + instance, + authority, + } + } + pub fn instance(&self) -> &NamespaceId { + &self.instance + } + pub fn authority(&self) -> Fingerprint { + self.authority + } +} + +/// Captured account ID and expected account-key identity from the original installation. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AccountIdentity { + id: NamespaceId, + key: Fingerprint, +} +impl AccountIdentity { + pub fn new(id: NamespaceId, key: Fingerprint) -> Self { + Self { id, key } + } + pub fn id(&self) -> &NamespaceId { + &self.id + } + pub fn key(&self) -> Fingerprint { + self.key + } +} + +/// A descriptive account partition. Construction is not account authentication. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct AccountScope { + hub: HubIdentity, + account: AccountIdentity, +} +impl AccountScope { + pub fn new(hub: HubIdentity, account: AccountIdentity) -> Self { + Self { hub, account } + } + pub fn hub(&self) -> &HubIdentity { + &self.hub + } + pub fn account(&self) -> &AccountIdentity { + &self.account + } +} diff --git a/crates/hubs-model/src/source_identity/local.rs b/crates/hubs-model/src/source_identity/local.rs new file mode 100644 index 00000000..46273736 --- /dev/null +++ b/crates/hubs-model/src/source_identity/local.rs @@ -0,0 +1,63 @@ +//! Captured local-history partition and validity; no Cloud authority is inferred. +use super::{AccountScope, Fingerprint, NamespaceId}; +use crate::HubId; +use serde::{Deserialize, Serialize}; + +/// Old hubs have no authenticated stable instance authority. Their local history +/// is separated by retained configuration, authenticated account and account key. +/// Address aliases may have distinct partitions; this is never a Cloud identity. +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde(deny_unknown_fields)] +pub struct LegacyConfiguredAccount { + hub: HubId, + account: NamespaceId, + key: Fingerprint, +} +impl LegacyConfiguredAccount { + pub fn new(hub: HubId, account: NamespaceId, key: Fingerprint) -> Self { + Self { hub, account, key } + } + pub fn hub(&self) -> &HubId { + &self.hub + } + pub fn account(&self) -> &NamespaceId { + &self.account + } + pub fn key(&self) -> Fingerprint { + self.key + } +} + +#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] +#[serde( + tag = "kind", + content = "identity", + rename_all = "snake_case", + deny_unknown_fields +)] +pub enum LocalAccountPartition { + Stable(AccountScope), + LegacyConfigured(LegacyConfiguredAccount), +} + +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum LocalAccountSourceError { + #[error("the captured hub account is no longer signed in or unlocked")] + Stale, + #[error("the captured hub account identity is unavailable")] + Unavailable, +} + +/// Authenticated adapter-owned capture. A plain partition constructor is not +/// evidence of authentication. Ordinary token refresh retains the source; +/// sign-out, replacement login, key rotation and lock invalidate the old capture. +pub trait LocalAccountSource: Send + Sync { + fn partition(&self) -> LocalAccountPartition; + fn check_current(&self) -> Result<(), LocalAccountSourceError>; +} + +/// Captures only a new attempt; existing conversations retain their original source. +/// Unlock/sign-in can make later captures available without rewriting old handles. +pub trait LocalAccountSourceFactory: Send + Sync { + fn capture(&self) -> Result, crate::HubError>; +} diff --git a/crates/hubs-model/src/testing.rs b/crates/hubs-model/src/testing.rs new file mode 100644 index 00000000..eaaf5e85 --- /dev/null +++ b/crates/hubs-model/src/testing.rs @@ -0,0 +1,674 @@ +//! An in-memory hub for tests: one fake behind the access ports. +//! +//! Enable `test-support` in a consumer's dev-dependencies. The fake does no I/O and keeps its +//! "hubs" in memory. A call completes when it is polled; [`FakeHub::hold`] makes the next call +//! of one kind wait until the test releases it, so a test can observe the state in between. + +use std::collections::{BTreeMap, BTreeSet, VecDeque}; +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::FutureExt as _; +use futures::channel::oneshot; +use futures::future::BoxFuture; +use nocterm_core::error::SharedCause; +use nocterm_session::Secret; + +use crate::address::{HubAddress, HubId}; +use crate::error::HubError; +use crate::ports::{HubFuture, HubSignIn, HubTrust, OidcSignIn}; +use crate::records::{Account, HubEndpointSpec, HubInfo, KeyStatus, SignedIn}; +use crate::trust::{ProbedHub, TrustPolicy}; + +mod account_keys; +mod devices; +mod local_source; +mod login; +mod recovery; +mod server; +pub use local_source::ScriptedLocalAccountFactory; + +pub use account_keys::FakeAccountKeys; +pub use devices::FakeAccountDevices; +pub use server::{FakeKeyset, FakeServer, FakeUser}; + +/// The protocol version the fake client speaks. +pub const CLIENT_PROTOCOL: u32 = 1; + +/// The kinds of call a test can observe, hold or make fail. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub enum FakeOp { + Probe, + Connect, + Password, + Oidc, + SetVaultPassphrase, + Recover, + ReissueRecoveryKey, + ConfirmRecoveryKey, + SignOut, + Forget, +} + +/// One call the fake received. Secrets are not recorded. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum FakeCall { + Probe(HubAddress), + Connect(HubId), + Password { hub: HubId, username: String }, + Oidc { hub: HubId, provider: String }, + SetVaultPassphrase(HubId), + Recover(HubId), + ReissueRecoveryKey(HubId), + ConfirmRecoveryKey(HubId), + SignOut(HubId), + Forget(HubId), +} + +/// Why a fake hub did not answer. +#[derive(Debug, thiserror::Error)] +#[error("the fake hub is offline")] +pub struct FakeOffline; + +/// An in-memory hub implementing [`HubTrust`] and [`HubSignIn`]. +/// +/// Clones share one state, so a test keeps a handle while a service owns another. +#[derive(Clone, Default)] +pub struct FakeHub { + state: Arc>, +} + +#[derive(Default)] +struct State { + servers: BTreeMap, + receipts: recovery::Receipts, + /// Hubs this device holds credentials for, and who it is signed in as. + credentials: BTreeMap, + revoked: BTreeSet, + calls: Vec, + failures: VecDeque<(FakeOp, HubError)>, + holds: Vec<(FakeOp, oneshot::Receiver<()>)>, + oidc: Option>>, + oidc_cancelled: usize, + /// Keys a sign-in could not open, waiting for the recovery key, and what should open them + /// afterwards. + recovering: BTreeMap, + /// Hubs whose recovery key was shown on this device and not confirmed as saved. + unconfirmed: BTreeSet, + /// Hubs whose account keys are closed, as after a restart until the vault is unlocked. + locked: BTreeSet, + /// How many times the keys of any hub were opened or dropped, and who waits for the next. + key_changes: u64, + key_waiters: Vec>, +} + +impl State { + fn discard(&mut self, hub: &HubId) -> Result<(), HubError> { + self.calls.push(FakeCall::Forget(hub.clone())); + let injected = self + .failures + .iter() + .position(|(op, _)| *op == FakeOp::Forget) + .and_then(|index| self.failures.remove(index)); + if let Some((_, error)) = injected { + return Err(error); + } + self.credentials.remove(hub); + self.recovering.remove(hub); + self.unconfirmed.remove(hub); + self.receipts.begin(hub); + self.keys_changed(); + Ok(()) + } + fn keys_changed(&mut self) { + self.key_changes += 1; + for waiter in self.key_waiters.drain(..) { + let _ = waiter.send(()); + } + } +} + +fn lock(state: &Mutex) -> MutexGuard<'_, State> { + state.lock().unwrap_or_else(PoisonError::into_inner) +} + +/// Lets the held call continue when [`Release::release`] is called or the value is dropped. +#[derive(Debug)] +pub struct Release(oneshot::Sender<()>); + +impl Release { + pub fn release(self) { + let _ = self.0.send(()); + } +} + +impl FakeHub { + pub fn new() -> Self { + Self::default() + } + + /// Adds a server reachable at `server.address`. + #[must_use] + pub fn with_server(self, server: FakeServer) -> Self { + lock(&self.state) + .servers + .insert(server.address.clone(), server); + self + } + + /// Changes a server through `change`; panics when the address is not served. + pub fn edit_server(&self, address: &HubAddress, change: impl FnOnce(&mut FakeServer)) { + let mut state = lock(&self.state); + change(state.servers.get_mut(address).expect("a served address")); + } + + /// Pretends this device already signed in to `hub` as `username`. + pub fn sign_in_as(&self, hub: &HubId, username: &str) { + let mut state = lock(&self.state); + state.receipts.begin(hub); + state.credentials.insert(hub.clone(), username.to_owned()); + } + + /// Makes the hub revoke this device: its next call finds the device revoked. + /// Ends the sign-in of this device as an expired login does: the credentials are gone, the + /// account keys it holds are not. + pub fn expire_login(&self, hub: &HubId) { + lock(&self.state).credentials.remove(hub); + } + + /// Marks the recovery key shown on this device for `hub` as never confirmed, as a dialog + /// closed on that step leaves it. + pub fn leave_recovery_key_unconfirmed(&self, hub: &HubId) { + lock(&self.state).unconfirmed.insert(hub.clone()); + } + + /// Closes the account keys of `hub`, as a restart does until the vault is unlocked. + pub fn lock_keys(&self, hub: &HubId) { + let mut state = lock(&self.state); + state.receipts.rotate(hub); + if state.locked.insert(hub.clone()) { + state.keys_changed(); + } + } + + /// Opens the account keys of `hub`, as unlocking the vault does. + pub fn unlock_keys(&self, hub: &HubId) { + let mut state = lock(&self.state); + state.receipts.rotate(hub); + if state.locked.remove(hub) { + state.keys_changed(); + } + } + + pub fn revoke_device(&self, hub: &HubId) { + lock(&self.state).revoked.insert(hub.clone()); + } + + /// Whether the account keys of `hub` are open: signed in and not locked. + pub fn keys_open(&self, hub: &HubId) -> bool { + let state = lock(&self.state); + state.credentials.contains_key(hub) && !state.locked.contains(hub) + } + + pub fn is_signed_in(&self, hub: &HubId) -> bool { + lock(&self.state).credentials.contains_key(hub) + } + + /// Every call received, oldest first. + pub fn calls(&self) -> Vec { + lock(&self.state).calls.clone() + } + + /// How many calls of one kind arrived. + pub fn count(&self, op: FakeOp) -> usize { + lock(&self.state) + .calls + .iter() + .filter(|call| call.op() == op) + .count() + } + + /// The next call of kind `op` fails with `error`. + pub fn fail_next(&self, op: FakeOp, error: HubError) { + lock(&self.state).failures.push_back((op, error)); + } + + /// The next call of kind `op` waits until the returned handle releases it. + #[must_use] + pub fn hold(&self, op: FakeOp) -> Release { + let (release, wait) = oneshot::channel(); + lock(&self.state).holds.push((op, wait)); + Release(release) + } + + /// Finishes the pending browser sign-in as `username`. + pub fn complete_oidc(&self, username: &str) { + if let Some(done) = lock(&self.state).oidc.take() { + let _ = done.send(Ok(username.to_owned())); + } + } + + /// Fails the pending browser sign-in. + pub fn fail_oidc(&self, error: HubError) { + if let Some(done) = lock(&self.state).oidc.take() { + let _ = done.send(Err(error)); + } + } + + /// How many browser sign-ins were dropped before they finished. + pub fn cancelled_oidc(&self) -> usize { + lock(&self.state).oidc_cancelled + } + + /// Records the call, then runs `answer` against the state when the future is polled, + /// after any hold on `op` was released. + fn call( + &self, + op: FakeOp, + call: FakeCall, + answer: impl FnOnce(&mut State) -> Result + Send + 'static, + ) -> HubFuture { + let hold = { + let mut state = lock(&self.state); + state.calls.push(call); + let index = state.holds.iter().position(|(held, _)| *held == op); + index.map(|index| state.holds.remove(index).1) + }; + let shared = Arc::clone(&self.state); + async move { + if let Some(hold) = hold { + let _ = hold.await; + } + let mut state = lock(&shared); + let injected = state + .failures + .iter() + .position(|(failing, _)| *failing == op) + .and_then(|index| state.failures.remove(index)); + match injected { + Some((_, error)) => Err(error), + None => answer(&mut state), + } + } + .boxed() + } +} + +impl FakeCall { + fn op(&self) -> FakeOp { + match self { + Self::Probe(_) => FakeOp::Probe, + Self::Connect(_) => FakeOp::Connect, + Self::Password { .. } => FakeOp::Password, + Self::Oidc { .. } => FakeOp::Oidc, + Self::SetVaultPassphrase(_) => FakeOp::SetVaultPassphrase, + Self::Recover(_) => FakeOp::Recover, + Self::ReissueRecoveryKey(_) => FakeOp::ReissueRecoveryKey, + Self::ConfirmRecoveryKey(_) => FakeOp::ConfirmRecoveryKey, + Self::SignOut(_) => FakeOp::SignOut, + Self::Forget(_) => FakeOp::Forget, + } + } +} + +impl State { + fn unreachable(address: &HubAddress) -> HubError { + HubError::Unreachable { + address: address.clone(), + cause: SharedCause::new(FakeOffline), + } + } + + /// The server a call reaches, or why it cannot be reached. + fn reach(&self, address: &HubAddress) -> Result<&FakeServer, HubError> { + self.servers + .get(address) + .filter(|server| server.online) + .ok_or_else(|| Self::unreachable(address)) + } + + /// Checks the certificate against the decided trust, as the real adapter does. + fn verify(&self, endpoint: &HubEndpointSpec) -> Result<&FakeServer, HubError> { + let server = self.reach(&endpoint.address)?; + match endpoint.trust { + TrustPolicy::Pinned(pinned) if pinned != server.certificate => { + Err(HubError::CertificateChanged { + address: endpoint.address.clone(), + pinned, + presented: server.certificate, + }) + } + TrustPolicy::PublicCa if !server.publicly_trusted => { + Err(HubError::CertificateUntrusted { + address: endpoint.address.clone(), + presented: server.certificate, + }) + } + _ => Ok(server), + } + } + + /// The signed-in user of a hub; a revoked device loses its credentials. + fn authenticate(&mut self, hub: &HubId) -> Result<&FakeServer, HubError> { + if self.revoked.remove(hub) { + self.credentials.remove(hub); + return Err(HubError::Revoked); + } + if !self.credentials.contains_key(hub) { + return Err(HubError::NotSignedIn); + } + self.reach(hub.address()) + } + + /// What a sign-in with `secret` does to the keys of `username`: opens them, makes them (the + /// recovery key is `recovery-`) or waits for the recovery key. + fn open_keys( + &mut self, + hub: &HubId, + username: &str, + secret: &str, + shared: std::sync::Weak>, + ) -> Result { + let server = self + .servers + .get_mut(hub.address()) + .filter(|server| server.online) + .ok_or_else(|| Self::unreachable(hub.address()))?; + if !server.keyed { + return Ok(KeyStatus::Open); + } + match server.keysets.get(username) { + None => { + let recovery = format!("recovery-{username}"); + server.keysets.insert( + username.to_owned(), + FakeKeyset { + secret: secret.to_owned(), + recovery: recovery.clone(), + }, + ); + self.unconfirmed.insert(hub.clone()); + Ok(KeyStatus::Created( + recovery::Recipient::capture(shared, self, hub).offer(Secret::new(recovery)), + )) + } + Some(keyset) if keyset.secret == secret => Ok(if self.unconfirmed.contains(hub) { + KeyStatus::RecoveryKeyUnconfirmed( + recovery::Recipient::capture(shared, self, hub).issuer(), + ) + } else { + KeyStatus::Open + }), + Some(_) => { + self.recovering.insert(hub.clone(), secret.to_owned()); + Ok(KeyStatus::RecoveryNeeded) + } + } + } + + fn account_of(&self, hub: &HubId, username: &str) -> Result { + let server = self.reach(hub.address())?; + server + .users + .iter() + .find(|user| user.username == username) + .map(|user| user.account.clone()) + .ok_or(HubError::InvalidCredentials) + } +} + +impl HubTrust for FakeHub { + fn probe(&self, address: &HubAddress) -> HubFuture { + let address = address.clone(); + self.call( + FakeOp::Probe, + FakeCall::Probe(address.clone()), + move |state| { + let server = state.reach(&address)?; + Ok(ProbedHub { + fingerprint: server.certificate, + publicly_trusted: server.publicly_trusted, + }) + }, + ) + } +} + +impl HubSignIn for FakeHub { + fn connect(&self, endpoint: HubEndpointSpec) -> HubFuture { + let hub = HubId::from(endpoint.address.clone()); + self.call( + FakeOp::Connect, + FakeCall::Connect(hub.clone()), + move |state| { + let server = state.verify(&endpoint)?; + if server.protocol != CLIENT_PROTOCOL { + return Err(HubError::UpgradeRequired { + hub: server.protocol, + client: CLIENT_PROTOCOL, + }); + } + if state.revoked.remove(&hub) { + state.credentials.remove(&hub); + return Err(HubError::Revoked); + } + let signed_in = state.credentials.contains_key(&hub); + let server = state.verify(&endpoint)?; + Ok(server.info(signed_in)) + }, + ) + } + + fn password( + &self, + endpoint: HubEndpointSpec, + username: String, + password: Secret, + ) -> HubFuture { + let hub = HubId::from(endpoint.address.clone()); + let call = FakeCall::Password { + hub: hub.clone(), + username: username.clone(), + }; + let shared = Arc::downgrade(&self.state); + self.call(FakeOp::Password, call, move |state| { + let server = state.verify(&endpoint)?; + if !server.password_login { + return Err(HubError::MethodUnavailable); + } + let known = server.users.iter().any(|user| { + user.username == username && user.password.as_str() == password.expose() + }); + if !known { + return Err(HubError::InvalidCredentials); + } + state.revoked.remove(&hub); + state.receipts.begin(&hub); + state.credentials.insert(hub.clone(), username.clone()); + let account = state.account_of(&hub, &username)?; + let keys = state.open_keys(&hub, &username, password.expose(), shared.clone())?; + let login = login::receipt(shared.clone(), state, &hub); + Ok(SignedIn { + account, + keys, + login, + }) + }) + } + + fn oidc(&self, endpoint: HubEndpointSpec, provider: String) -> OidcSignIn { + let hub = HubId::from(endpoint.address.clone()); + let call = FakeCall::Oidc { + hub: hub.clone(), + provider: provider.clone(), + }; + let (browser_tx, browser) = oneshot::channel(); + let (finish, finished) = oneshot::channel(); + let shared = Arc::clone(&self.state); + let ready = { + let provider = provider.clone(); + self.call(FakeOp::Oidc, call, move |state| { + let server = state.verify(&endpoint)?; + if !server + .oidc_providers + .iter() + .any(|known| known.id == provider) + { + return Err(HubError::MethodUnavailable); + } + state.oidc = Some(finish); + let _ = browser_tx.send(format!("https://idp.fake/authorize?provider={provider}")); + Ok(()) + }) + }; + let done = async move { + let mut guard = CancelGuard { + state: Arc::clone(&shared), + finished: false, + }; + ready.await?; + let username = finished.await.map_err(|_| HubError::SignInCancelled)??; + guard.finished = true; + let mut state = lock(&shared); + state.revoked.remove(&hub); + state.receipts.begin(&hub); + state.credentials.insert(hub.clone(), username.clone()); + state.recovering.remove(&hub); + let account = state.account_of(&hub, &username)?; + let keys = if state.reach(hub.address())?.keyed { + KeyStatus::PassphraseNeeded + } else { + KeyStatus::Open + }; + let login = login::receipt(Arc::downgrade(&shared), &mut state, &hub); + Ok(SignedIn { + account, + keys, + login, + }) + } + .boxed(); + OidcSignIn { + browser: Box::pin(async move { browser.await.map_err(|_| HubError::SignInCancelled) }), + done, + } + } + + fn set_vault_passphrase(&self, hub: &HubId, passphrase: Secret) -> HubFuture { + let hub = hub.clone(); + let shared = Arc::downgrade(&self.state); + self.call( + FakeOp::SetVaultPassphrase, + FakeCall::SetVaultPassphrase(hub.clone()), + move |state| { + state.authenticate(&hub)?; + let username = state.credentials[&hub].clone(); + state.open_keys(&hub, &username, passphrase.expose(), shared) + }, + ) + } + + fn recover(&self, hub: &HubId, recovery_key: Secret) -> HubFuture<()> { + let hub = hub.clone(); + self.call( + FakeOp::Recover, + FakeCall::Recover(hub.clone()), + move |state| { + state.authenticate(&hub)?; + let username = state.credentials[&hub].clone(); + let secret = state + .recovering + .get(&hub) + .cloned() + .ok_or(HubError::NoRecoveryPending)?; + let keyset = state + .servers + .get_mut(hub.address()) + .and_then(|server| server.keysets.get_mut(&username)) + .ok_or(HubError::NoRecoveryPending)?; + if keyset.recovery != recovery_key.expose() { + return Err(HubError::RecoveryKeyInvalid); + } + keyset.secret = secret; + state.recovering.remove(&hub); + Ok(()) + }, + ) + } + + fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + let state = lock(&self.state); + state.unconfirmed.contains(hub) + && !state.recovering.contains_key(hub) + && !state.locked.contains(hub) + } + + fn keys_revision(&self) -> u64 { + lock(&self.state).key_changes + } + + fn keys_changed(&self, seen: u64) -> BoxFuture<'static, u64> { + let state = Arc::clone(&self.state); + Box::pin(async move { + loop { + let waiting = { + let mut state = lock(&state); + if state.key_changes != seen { + return state.key_changes; + } + let (sender, receiver) = oneshot::channel(); + state.key_waiters.push(sender); + receiver + }; + if waiting.await.is_err() { + return lock(&state).key_changes; + } + } + }) + } + + fn recovery_key_offer(&self, hub: &HubId) -> Result { + let mut state = lock(&self.state); + if state.locked.contains(hub) || state.recovering.contains_key(hub) { + return Err(HubError::KeysLocked); + } + Ok(recovery::Recipient::capture(Arc::downgrade(&self.state), &mut state, hub).issuer()) + } + + fn sign_out(&self, hub: &HubId) -> HubFuture<()> { + let hub = hub.clone(); + self.call( + FakeOp::SignOut, + FakeCall::SignOut(hub.clone()), + move |state| { + state.receipts.begin(&hub); + state.credentials.remove(&hub); + state.recovering.remove(&hub); + state.unconfirmed.remove(&hub); + Ok(()) + }, + ) + } + + fn forget(&self, hub: &HubId) -> Result<(), HubError> { + lock(&self.state).discard(hub) + } +} + +/// Counts a browser sign-in that was dropped before it finished. +struct CancelGuard { + state: Arc>, + finished: bool, +} + +impl Drop for CancelGuard { + fn drop(&mut self) { + if !self.finished { + let mut state = lock(&self.state); + state.oidc = None; + state.oidc_cancelled += 1; + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/testing/account_keys.rs b/crates/hubs-model/src/testing/account_keys.rs new file mode 100644 index 00000000..885c9645 --- /dev/null +++ b/crates/hubs-model/src/testing/account_keys.rs @@ -0,0 +1,68 @@ +//! In-memory control of account keys, for tests of what unlocks and locks them. + +use std::sync::{Arc, Mutex, PoisonError}; + +use futures::FutureExt as _; +use nocterm_session::Secret; + +use crate::account_keys::{AccountKeyError, AccountKeyFuture, AccountKeyLock}; +use crate::address::HubId; +use crate::testing::FakeHub; + +/// An in-memory [`AccountKeyLock`] over the key state of a [`FakeHub`], which also closes what +/// the hub's other fakes read ([`FakeHub::lock_keys`]). Opening needs `password`. +#[derive(Clone)] +pub struct FakeAccountKeys { + hub: FakeHub, + password: String, + attempts: Arc>>, +} + +impl FakeAccountKeys { + /// Keys of `hub` that open with `password`. + pub fn alongside(hub: &FakeHub, password: &str) -> Self { + Self { + hub: hub.clone(), + password: password.to_owned(), + attempts: Arc::default(), + } + } + + /// Whether each unlock attempt so far was accepted, in order. + pub fn attempts(&self) -> Vec { + self.attempts + .lock() + .unwrap_or_else(PoisonError::into_inner) + .clone() + } +} + +impl AccountKeyLock for FakeAccountKeys { + fn is_open(&self, hub: &HubId) -> bool { + self.hub.keys_open(hub) + } + + fn unlock(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()> { + let (keys, hub) = (self.clone(), hub.clone()); + async move { + let accepted = password.expose() == keys.password; + keys.attempts + .lock() + .unwrap_or_else(PoisonError::into_inner) + .push(accepted); + if !keys.hub.is_signed_in(&hub) { + return Err(AccountKeyError::NotSignedIn); + } + if !accepted { + return Err(AccountKeyError::WrongPassword); + } + keys.hub.unlock_keys(&hub); + Ok(()) + } + .boxed() + } + + fn lock(&self, hub: &HubId) { + self.hub.lock_keys(hub); + } +} diff --git a/crates/hubs-model/src/testing/devices.rs b/crates/hubs-model/src/testing/devices.rs new file mode 100644 index 00000000..32123153 --- /dev/null +++ b/crates/hubs-model/src/testing/devices.rs @@ -0,0 +1,104 @@ +//! The devices of fake hub accounts, for tests of what lists and revokes them. + +use std::sync::{Arc, Mutex, MutexGuard, PoisonError}; + +use futures::FutureExt as _; +use futures::channel::oneshot; + +use crate::address::HubId; +use crate::devices::{AccountDevices, DeviceSummary}; +use crate::error::HubError; +use crate::ports::HubFuture; + +/// An in-memory [`AccountDevices`]. Clones share one state, so a test keeps a handle while a +/// service owns another. +#[derive(Clone, Default)] +pub struct FakeAccountDevices { + state: Arc>, +} + +#[derive(Default)] +struct State { + devices: Vec<(HubId, DeviceSummary)>, + revoked: Vec<(HubId, String)>, + failure: Option, + held: Option>, +} + +fn lock(state: &Mutex) -> MutexGuard<'_, State> { + state.lock().unwrap_or_else(PoisonError::into_inner) +} + +impl FakeAccountDevices { + /// The account on `hub` has these devices from now on. + pub fn set(&self, hub: &HubId, devices: Vec) { + let mut state = lock(&self.state); + state.devices.retain(|(known, _)| known != hub); + state + .devices + .extend(devices.into_iter().map(|device| (hub.clone(), device))); + } + + /// Every call fails with `error` until [`FakeAccountDevices::recover`]. + pub fn fail_with(&self, error: HubError) { + lock(&self.state).failure = Some(error); + } + + /// Calls work again. + pub fn recover(&self) { + lock(&self.state).failure = None; + } + + /// The next listing waits until the returned sender is used or dropped. + pub fn hold_next_list(&self) -> oneshot::Sender<()> { + let (release, held) = oneshot::channel(); + lock(&self.state).held = Some(held); + release + } + + /// The devices revoked so far, in order. + pub fn revoked(&self) -> Vec<(HubId, String)> { + lock(&self.state).revoked.clone() + } +} + +impl AccountDevices for FakeAccountDevices { + fn list(&self, hub: &HubId) -> HubFuture> { + let hub = hub.clone(); + let state = self.state.clone(); + let held = lock(&state).held.take(); + async move { + if let Some(held) = held { + let _ = held.await; + } + let state = lock(&state); + if let Some(error) = &state.failure { + return Err(error.clone()); + } + Ok(state + .devices + .iter() + .filter(|(known, _)| *known == hub) + .map(|(_, device)| device.clone()) + .collect()) + } + .boxed() + } + + fn revoke(&self, hub: &HubId, device: &str) -> HubFuture<()> { + let (hub, device) = (hub.clone(), device.to_owned()); + let state = self.state.clone(); + async move { + let mut state = lock(&state); + if let Some(error) = &state.failure { + return Err(error.clone()); + } + state + .devices + .retain(|(known, summary)| *known != hub || summary.id != device); + state.revoked.push((hub, device)); + Ok(()) + } + .boxed() + } +} diff --git a/crates/hubs-model/src/testing/local_source.rs b/crates/hubs-model/src/testing/local_source.rs new file mode 100644 index 00000000..b0cf1320 --- /dev/null +++ b/crates/hubs-model/src/testing/local_source.rs @@ -0,0 +1,72 @@ +//! Per-capture source lifetimes for consumer tests; no authentication is performed. +use crate::{ + HubError, + source_identity::{ + LocalAccountPartition, LocalAccountSource, LocalAccountSourceError, + LocalAccountSourceFactory, + }, +}; +use std::sync::{ + Arc, Mutex, PoisonError, + atomic::{AtomicBool, Ordering}, +}; + +struct SourceOwner { + partition: LocalAccountPartition, + current: AtomicBool, +} +impl LocalAccountSource for SourceOwner { + fn partition(&self) -> LocalAccountPartition { + self.partition.clone() + } + fn check_current(&self) -> Result<(), LocalAccountSourceError> { + if self.current.load(Ordering::Acquire) { + Ok(()) + } else { + Err(LocalAccountSourceError::Stale) + } + } +} + +#[derive(Clone, Default)] +pub struct ScriptedLocalAccountFactory(Arc>>>, Arc); +impl ScriptedLocalAccountFactory { + /// A new login or key owner, even for the same persistent partition. + pub fn sign_in(&self, partition: LocalAccountPartition) { + self.1.store(false, Ordering::Release); + let mut slot = self.0.lock().unwrap_or_else(PoisonError::into_inner); + if let Some(previous) = slot.take() { + previous.current.store(false, Ordering::Release); + } + *slot = Some(Arc::new(SourceOwner { + partition, + current: AtomicBool::new(true), + })); + } + /// The account is signed out: later captures fail as not signed in until a sign in. + pub fn sign_out(&self) { + self.close(); + self.1.store(true, Ordering::Release); + } + /// The keys lock: later captures fail as locked until a sign in. + pub fn close(&self) { + if let Some(previous) = self.0.lock().unwrap_or_else(PoisonError::into_inner).take() { + previous.current.store(false, Ordering::Release); + } + } +} +impl LocalAccountSourceFactory for ScriptedLocalAccountFactory { + fn capture(&self) -> Result, HubError> { + let signed_out = self.1.load(Ordering::Acquire); + self.0 + .lock() + .unwrap_or_else(PoisonError::into_inner) + .as_ref() + .map(|source| source.clone() as Arc) + .ok_or(if signed_out { + HubError::NotSignedIn + } else { + HubError::KeysLocked + }) + } +} diff --git a/crates/hubs-model/src/testing/login.rs b/crates/hubs-model/src/testing/login.rs new file mode 100644 index 00000000..733d6e2b --- /dev/null +++ b/crates/hubs-model/src/testing/login.rs @@ -0,0 +1,41 @@ +//! Membership ownership is independent from the fake's displayed key action receipts. +use super::{State, lock}; +use crate::{HubError, HubId, LoginOwner, LoginReceipt}; +use std::sync::{Arc, Mutex, Weak}; +struct Owner { + state: Weak>, + hub: HubId, + member: Arc<()>, +} +impl Owner { + fn with_current( + &self, + action: impl FnOnce(&mut State) -> Result, + ) -> Result { + let shared = self.state.upgrade().ok_or(HubError::Stale)?; + let mut state = lock(&shared); + state.receipts.check_member(&self.hub, &self.member)?; + if !state.credentials.contains_key(&self.hub) { + return Err(HubError::Stale); + } + action(&mut state) + } +} +impl LoginOwner for Owner { + fn check_current(&self) -> Result<(), HubError> { + self.with_current(|_| Ok(())) + } + fn discard(&self) -> Result<(), HubError> { + self.with_current(|state| state.discard(&self.hub)) + } +} +pub(super) fn receipt(shared: Weak>, state: &mut State, hub: &HubId) -> LoginReceipt { + LoginReceipt::new( + hub.clone(), + Arc::new(Owner { + state: shared, + hub: hub.clone(), + member: state.receipts.member(hub), + }), + ) +} diff --git a/crates/hubs-model/src/testing/recovery.rs b/crates/hubs-model/src/testing/recovery.rs new file mode 100644 index 00000000..861f7ba0 --- /dev/null +++ b/crates/hubs-model/src/testing/recovery.rs @@ -0,0 +1,157 @@ +//! Original recipient and action identities for the shared fake hub. +use super::{FakeCall, FakeHub, FakeOp, State, lock}; +use crate::{ + HubError, HubFuture, HubId, RecoveryKeyConfirmation, RecoveryKeyIssuer, RecoveryKeyOffer, + RecoveryKeyReissue, +}; +use nocterm_session::Secret; +use std::{ + collections::BTreeMap, + sync::{Arc, Mutex, Weak}, +}; + +#[derive(Default)] +pub(super) struct Receipts { + actions: BTreeMap>, + members: BTreeMap>, +} +impl Receipts { + pub(super) fn begin(&mut self, hub: &HubId) { + self.members.insert(hub.clone(), Arc::new(())); + self.rotate(hub); + } + pub(super) fn member(&mut self, hub: &HubId) -> Arc<()> { + self.members.entry(hub.clone()).or_default().clone() + } + pub(super) fn check_member(&self, hub: &HubId, expected: &Arc<()>) -> Result<(), HubError> { + if self + .members + .get(hub) + .is_some_and(|now| Arc::ptr_eq(now, expected)) + { + Ok(()) + } else { + Err(HubError::Stale) + } + } + pub(super) fn rotate(&mut self, hub: &HubId) { + self.actions.insert(hub.clone(), Arc::new(())); + } + pub(super) fn current(&mut self, hub: &HubId) -> Arc<()> { + self.actions.entry(hub.clone()).or_default().clone() + } + fn check(&self, hub: &HubId, expected: &Arc<()>) -> Result<(), HubError> { + if self + .actions + .get(hub) + .is_some_and(|now| Arc::ptr_eq(now, expected)) + { + Ok(()) + } else { + Err(HubError::Stale) + } + } +} +#[derive(Clone)] +pub(super) struct Recipient { + shared: Weak>, + hub: HubId, + receipt: Arc<()>, +} +impl Recipient { + pub(super) fn capture(shared: Weak>, state: &mut State, hub: &HubId) -> Self { + Self { + shared, + hub: hub.clone(), + receipt: state.receipts.current(hub), + } + } + pub(super) fn check(&self, state: &State) -> Result<(), HubError> { + state.receipts.check(&self.hub, &self.receipt)?; + if state.locked.contains(&self.hub) { + return Err(HubError::KeysLocked); + } + Ok(()) + } + pub(super) fn with_current( + &self, + action: impl FnOnce(&mut State) -> Result, + ) -> Result { + let shared = self.shared.upgrade().ok_or(HubError::Stale)?; + let mut state = lock(&shared); + self.check(&state)?; + action(&mut state) + } + pub(super) fn issuer(&self) -> RecoveryKeyReissue { + RecoveryKeyReissue::new(self.hub.clone(), Arc::new(self.clone())) + } + pub(super) fn offer(&self, key: Secret) -> RecoveryKeyOffer { + RecoveryKeyOffer::new(self.hub.clone(), key, Arc::new(self.clone())) + } +} +impl RecoveryKeyConfirmation for Recipient { + fn check_current(&self) -> Result<(), HubError> { + self.with_current(|_| Ok(())) + } + fn confirm(&self) -> HubFuture<()> { + let Some(shared) = self.shared.upgrade() else { + return Box::pin(async { Err(HubError::Stale) }); + }; + let original = self.clone(); + FakeHub { state: shared }.call( + FakeOp::ConfirmRecoveryKey, + FakeCall::ConfirmRecoveryKey(self.hub.clone()), + move |state| { + original.check(state)?; + state.unconfirmed.remove(&original.hub); + state.receipts.rotate(&original.hub); + state.keys_changed(); + Ok(()) + }, + ) + } +} +impl RecoveryKeyIssuer for Recipient { + fn check_current(&self) -> Result<(), HubError> { + RecoveryKeyConfirmation::check_current(self) + } + fn issue(&self) -> HubFuture { + let Some(shared) = self.shared.upgrade() else { + return Box::pin(async { Err(HubError::Stale) }); + }; + if let Err(error) = self.with_current(|_| Ok(())) { + return Box::pin(async move { Err(error) }); + } + let original = self.clone(); + let fake = FakeHub { + state: shared.clone(), + }; + let action = fake.call( + FakeOp::ReissueRecoveryKey, + FakeCall::ReissueRecoveryKey(self.hub.clone()), + move |state| { + original.check(state)?; + state.authenticate(&original.hub)?; + let username = state.credentials[&original.hub].clone(); + if state.recovering.contains_key(&original.hub) { + return Err(HubError::KeysLocked); + } + let keyset = state + .servers + .get_mut(original.hub.address()) + .and_then(|server| server.keysets.get_mut(&username)) + .ok_or(HubError::KeysLocked)?; + keyset.recovery.push('+'); + let key = Secret::new(keyset.recovery.clone()); + state.unconfirmed.insert(original.hub.clone()); + state.receipts.rotate(&original.hub); + Ok(Recipient::capture(Arc::downgrade(&shared), state, &original.hub).offer(key)) + }, + ); + Box::pin(async move { + let shown = action.await?; + shown.check_current()?; + Ok(shown) + }) + } +} diff --git a/crates/hubs-model/src/testing/server.rs b/crates/hubs-model/src/testing/server.rs new file mode 100644 index 00000000..43482dbd --- /dev/null +++ b/crates/hubs-model/src/testing/server.rs @@ -0,0 +1,155 @@ +//! The data of one fake hub server. + +use std::collections::BTreeMap; + +use crate::address::HubAddress; +use crate::records::{Account, AccountId, HubInfo, OidcProvider}; +use crate::settings::HubRecord; +use crate::testing::CLIENT_PROTOCOL; +use crate::trust::{Fingerprint, TrustPolicy}; + +/// A user who can sign in to a fake server. +#[derive(Clone, Debug)] +pub struct FakeUser { + pub username: String, + pub password: String, + pub account: Account, +} + +/// The account keys a fake server keeps for one user: what opens them and the recovery key. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct FakeKeyset { + /// The password (or vault passphrase) that opens the keys. + pub secret: String, + pub recovery: String, +} + +/// A server a [`crate::testing::FakeHub`] can reach. The fields are plain data a test edits. +#[derive(Clone, Debug)] +pub struct FakeServer { + pub address: HubAddress, + pub name: String, + pub certificate: Fingerprint, + /// A public authority vouches for the certificate. + pub publicly_trusted: bool, + /// The server answers; `false` makes every call unreachable. + pub online: bool, + /// The protocol version the server speaks. + pub protocol: u32, + pub bootstrapped: bool, + pub password_login: bool, + pub oidc_providers: Vec, + pub users: Vec, + /// The server keeps account keys: a sign-in opens, creates or waits for the recovery of + /// the keys of its user. Off, every sign-in finds the keys open. + pub keyed: bool, + /// The keys of the users that made them, by user name. + pub keysets: BTreeMap, +} + +impl FakeServer { + /// A self-signed server named "Fake hub" whose certificate is `[seed; 32]`. + pub fn new(address: &str, seed: u8) -> Self { + Self { + address: HubAddress::parse(address).expect("a valid fake address"), + name: "Fake hub".into(), + certificate: Fingerprint::from_bytes([seed; 32]), + publicly_trusted: false, + online: true, + protocol: CLIENT_PROTOCOL, + bootstrapped: true, + password_login: true, + oidc_providers: Vec::new(), + users: Vec::new(), + keyed: false, + keysets: BTreeMap::new(), + } + } + + /// Makes the server keep account keys; users without keys get them on their first sign-in. + #[must_use] + pub fn keyed(mut self) -> Self { + self.keyed = true; + self + } + + /// Gives `username` account keys that `secret` opens and `recovery` recovers. + #[must_use] + pub fn with_keyset(mut self, username: &str, secret: &str, recovery: &str) -> Self { + self.keyed = true; + self.keysets.insert( + username.to_owned(), + FakeKeyset { + secret: secret.into(), + recovery: recovery.into(), + }, + ); + self + } + + #[must_use] + pub fn named(mut self, name: &str) -> Self { + self.name = name.into(); + self + } + + /// The settings record of this server as a sign-in leaves it: pinned to its certificate. + pub fn pinned_record(&self, label: &str) -> HubRecord { + HubRecord::new( + self.address.clone(), + TrustPolicy::Pinned(self.certificate), + label, + ) + } + + #[must_use] + pub fn publicly_trusted(mut self) -> Self { + self.publicly_trusted = true; + self + } + + #[must_use] + pub fn with_provider(mut self, id: &str, display_name: &str) -> Self { + self.oidc_providers.push(OidcProvider { + id: id.into(), + display_name: display_name.into(), + }); + self + } + + /// A user of the hub. + #[must_use] + pub fn with_user(mut self, username: &str, password: &str) -> Self { + self.users.push(FakeUser { + username: username.into(), + password: password.into(), + account: Account { + id: AccountId::new(format!("account-{username}")), + username: username.into(), + display_name: username.to_uppercase(), + admin: false, + }, + }); + self + } + + /// Makes the existing user `username` an administrator of the hub; a user that does not + /// exist is left alone. + #[must_use] + pub fn with_admin(mut self, username: &str) -> Self { + if let Some(user) = self.users.iter_mut().find(|user| user.username == username) { + user.account.admin = true; + } + self + } + + pub(super) fn info(&self, signed_in: bool) -> HubInfo { + HubInfo { + name: self.name.clone(), + password_login: self.password_login, + oidc_providers: self.oidc_providers.clone(), + bootstrapped: self.bootstrapped, + signed_in, + } + } +} diff --git a/crates/hubs-model/src/testing/tests.rs b/crates/hubs-model/src/testing/tests.rs new file mode 100644 index 00000000..73fe3716 --- /dev/null +++ b/crates/hubs-model/src/testing/tests.rs @@ -0,0 +1,367 @@ +use futures::executor::block_on; + +use super::*; +use crate::Fingerprint; + +const ADDRESS: &str = "hub.example:7443"; + +fn hub_id() -> HubId { + HubId::from(HubAddress::parse(ADDRESS).unwrap()) +} + +fn server() -> FakeServer { + FakeServer::new(ADDRESS, 7) + .with_user("ada", "secret") + .with_provider("corp", "Corporate SSO") +} + +fn endpoint(trust: TrustPolicy) -> HubEndpointSpec { + HubEndpointSpec { + address: HubAddress::parse(ADDRESS).unwrap(), + trust, + } +} + +fn pinned() -> TrustPolicy { + TrustPolicy::Pinned(Fingerprint::from_bytes([7; 32])) +} + +fn signed_in() -> FakeHub { + let hub = FakeHub::new().with_server(server()); + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))).unwrap(); + hub +} + +#[test] +fn probe_shows_the_certificate_without_trusting_it() { + let hub = FakeHub::new().with_server(server()); + let probed = block_on(hub.probe(&HubAddress::parse(ADDRESS).unwrap())).unwrap(); + assert_eq!(probed.fingerprint, Fingerprint::from_bytes([7; 32])); + assert!(!probed.publicly_trusted); +} + +#[test] +fn an_unknown_or_offline_address_is_unreachable() { + let hub = FakeHub::new().with_server(server()); + let other = HubAddress::parse("nowhere.example").unwrap(); + assert!(matches!( + block_on(hub.probe(&other)), + Err(HubError::Unreachable { .. }) + )); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| s.online = false); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::Unreachable { .. }) + )); +} + +#[test] +fn connect_reports_the_certificate_that_changed_since_it_was_pinned() { + let hub = FakeHub::new().with_server(server()); + let old = TrustPolicy::Pinned(Fingerprint::from_bytes([1; 32])); + match block_on(hub.connect(endpoint(old))) { + Err(HubError::CertificateChanged { + pinned, presented, .. + }) => { + assert_eq!(pinned, Fingerprint::from_bytes([1; 32])); + assert_eq!(presented, Fingerprint::from_bytes([7; 32])); + } + other => panic!("{other:?}"), + } +} + +#[test] +fn public_trust_needs_a_certificate_a_public_authority_vouches_for() { + let hub = FakeHub::new().with_server(server()); + assert!(matches!( + block_on(hub.connect(endpoint(TrustPolicy::PublicCa))), + Err(HubError::CertificateUntrusted { .. }) + )); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| { + s.publicly_trusted = true; + }); + assert!(block_on(hub.connect(endpoint(TrustPolicy::PublicCa))).is_ok()); +} + +#[test] +fn connect_tells_whether_this_device_is_signed_in() { + let hub = FakeHub::new().with_server(server()); + let info = block_on(hub.connect(endpoint(pinned()))).unwrap(); + assert!(!info.signed_in); + assert_eq!(info.oidc_providers.len(), 1); + hub.sign_in_as(&hub_id(), "ada"); + assert!(block_on(hub.connect(endpoint(pinned()))).unwrap().signed_in); +} + +#[test] +fn connect_refuses_a_hub_that_needs_a_newer_client() { + let hub = FakeHub::new().with_server(server()); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| s.protocol = 9); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::UpgradeRequired { hub: 9, .. }) + )); +} + +#[test] +fn password_sign_in_returns_the_account_and_remembers_the_device() { + let hub = FakeHub::new().with_server(server()); + let wrong = block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("nope"))); + assert!(matches!(wrong, Err(HubError::InvalidCredentials))); + assert!(!hub.is_signed_in(&hub_id())); + + let signed_in = + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))).unwrap(); + assert_eq!(signed_in.account.username, "ada"); + assert!(!signed_in.account.admin); + assert_eq!(signed_in.keys, KeyStatus::Open); + assert!(hub.is_signed_in(&hub_id())); +} + +#[test] +fn password_sign_in_is_refused_when_the_hub_offers_only_single_sign_on() { + let hub = FakeHub::new().with_server(server()); + hub.edit_server(&HubAddress::parse(ADDRESS).unwrap(), |s| { + s.password_login = false; + }); + let result = block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))); + assert!(matches!(result, Err(HubError::MethodUnavailable))); +} + +#[test] +fn calls_never_record_the_password() { + let hub = FakeHub::new().with_server(server()); + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new("secret"))).unwrap(); + let text = format!("{:?}", hub.calls()); + assert!(text.contains("ada")); + assert!(!text.contains("secret")); +} + +#[test] +fn browser_sign_in_hands_out_the_address_and_finishes_when_the_browser_does() { + let hub = FakeHub::new().with_server(server()); + let OidcSignIn { browser, done } = hub.oidc(endpoint(pinned()), "corp".into()); + let browser_side = async { + let url = browser.await.unwrap(); + assert!(url.contains("provider=corp")); + hub.complete_oidc("ada"); + }; + let (result, ()) = block_on(futures::future::join(done, browser_side)); + assert_eq!(result.unwrap().account.username, "ada"); + assert!(hub.is_signed_in(&hub_id())); + assert_eq!(hub.cancelled_oidc(), 0); +} + +#[test] +fn dropping_a_browser_sign_in_counts_as_cancelling_it() { + let hub = FakeHub::new().with_server(server()); + let OidcSignIn { browser, mut done } = hub.oidc(endpoint(pinned()), "corp".into()); + block_on(async { + assert!(futures::poll!(&mut done).is_pending()); + browser.await.unwrap(); + }); + drop(done); + assert_eq!(hub.cancelled_oidc(), 1); + assert!(!hub.is_signed_in(&hub_id())); +} + +#[test] +fn a_failed_browser_sign_in_reports_why() { + let hub = FakeHub::new().with_server(server()); + let OidcSignIn { browser, done } = hub.oidc(endpoint(pinned()), "corp".into()); + let browser_side = async { + browser.await.unwrap(); + hub.fail_oidc(HubError::SignInTimedOut); + }; + let (result, ()) = block_on(futures::future::join(done, browser_side)); + assert!(matches!(result, Err(HubError::SignInTimedOut))); + assert!(!hub.is_signed_in(&hub_id())); +} + +#[test] +fn an_unknown_provider_is_unavailable() { + let hub = FakeHub::new().with_server(server()); + let sign_in = hub.oidc(endpoint(pinned()), "other".into()); + assert!(matches!( + block_on(sign_in.done), + Err(HubError::MethodUnavailable) + )); +} + +#[test] +fn a_revoked_device_loses_its_sign_in() { + let hub = signed_in(); + hub.revoke_device(&hub_id()); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::Revoked) + )); + assert!(!hub.is_signed_in(&hub_id())); + assert!(!block_on(hub.connect(endpoint(pinned()))).unwrap().signed_in); +} + +#[test] +fn sign_out_and_forget_remove_the_credentials() { + let hub = signed_in(); + block_on(hub.sign_out(&hub_id())).unwrap(); + assert!(!hub.is_signed_in(&hub_id())); + hub.sign_in_as(&hub_id(), "ada"); + hub.forget(&hub_id()).unwrap(); + assert!(!hub.is_signed_in(&hub_id())); + assert!(hub.calls().contains(&FakeCall::Forget(hub_id()))); +} + +#[test] +fn an_injected_failure_hits_one_call_of_that_kind_only() { + let hub = signed_in(); + hub.fail_next(FakeOp::Connect, HubError::PermissionDenied); + assert!(matches!( + block_on(hub.connect(endpoint(pinned()))), + Err(HubError::PermissionDenied) + )); + assert!(block_on(hub.connect(endpoint(pinned()))).is_ok()); + assert_eq!(hub.count(FakeOp::Connect), 2); +} + +#[test] +fn a_held_call_waits_until_released() { + let hub = signed_in(); + let release = hub.hold(FakeOp::Connect); + let mut pending = hub.connect(endpoint(pinned())); + block_on(async { assert!(futures::poll!(&mut pending).is_pending()) }); + release.release(); + assert!(block_on(pending).is_ok()); +} + +fn keyed_hub() -> FakeHub { + FakeHub::new().with_server(server().keyed()) +} + +fn sign_in(hub: &FakeHub, password: &str) -> SignedIn { + block_on(hub.password(endpoint(pinned()), "ada".into(), Secret::new(password))).unwrap() +} + +#[test] +fn a_first_sign_in_creates_the_keys_and_hands_out_the_recovery_key_once() { + let hub = keyed_hub(); + let KeyStatus::Created(first) = sign_in(&hub, "secret").keys else { + panic!("created"); + }; + assert_eq!(first.key(), &Secret::new("recovery-ada")); + assert!(hub.recovery_key_unconfirmed(&hub_id())); + assert!(matches!( + sign_in(&hub, "secret").keys, + KeyStatus::RecoveryKeyUnconfirmed(_) + )); + assert!(matches!(block_on(first.confirm()), Err(HubError::Stale))); + let current = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + block_on(current.confirm()).unwrap(); + assert!(!hub.recovery_key_unconfirmed(&hub_id())); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::Open); +} + +#[test] +fn reissuing_replaces_the_recovery_key_until_it_is_confirmed() { + let hub = keyed_hub(); + let KeyStatus::Created(first) = sign_in(&hub, "secret").keys else { + panic!("created"); + }; + block_on(first.confirm()).unwrap(); + + let key = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + assert_eq!(key.key(), &Secret::new("recovery-ada+")); + assert!(hub.recovery_key_unconfirmed(&hub_id())); + block_on(key.confirm()).unwrap(); + + let again = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + assert_ne!(again, key); +} + +#[test] +fn reissuing_needs_open_keys() { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "old", "recovery-key")); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::RecoveryNeeded); + let result = block_on(hub.reissue_recovery_key(&hub_id())); + assert!(matches!(result, Err(HubError::KeysLocked))); + assert!(!hub.recovery_key_unconfirmed(&hub_id())); +} + +#[test] +fn a_password_that_does_not_open_the_keys_waits_for_the_recovery_key() { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "old", "recovery-key")); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::RecoveryNeeded); + + let wrong = block_on(hub.recover(&hub_id(), Secret::new("not it"))); + assert!(matches!(wrong, Err(HubError::RecoveryKeyInvalid))); + block_on(hub.recover(&hub_id(), Secret::new("recovery-key"))).unwrap(); + assert_eq!(sign_in(&hub, "secret").keys, KeyStatus::Open); +} + +#[test] +fn recovering_without_a_waiting_sign_in_is_refused() { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "secret", "recovery-key")); + sign_in(&hub, "secret"); + let result = block_on(hub.recover(&hub_id(), Secret::new("recovery-key"))); + assert!(matches!(result, Err(HubError::NoRecoveryPending))); +} + +#[test] +fn a_browser_sign_in_needs_the_vault_passphrase_to_open_or_make_the_keys() { + let hub = keyed_hub(); + let OidcSignIn { browser, done } = hub.oidc(endpoint(pinned()), "corp".into()); + let browser_side = async { + browser.await.unwrap(); + hub.complete_oidc("ada"); + }; + let (result, ()) = block_on(futures::future::join(done, browser_side)); + assert_eq!(result.unwrap().keys, KeyStatus::PassphraseNeeded); + + let made = block_on(hub.set_vault_passphrase(&hub_id(), Secret::new("a long passphrase"))); + let KeyStatus::Created(key) = made.unwrap() else { + panic!("created"); + }; + assert_eq!(key.key(), &Secret::new("recovery-ada")); + block_on(key.confirm()).unwrap(); + let again = block_on(hub.set_vault_passphrase(&hub_id(), Secret::new("a long passphrase"))); + assert_eq!(again.unwrap(), KeyStatus::Open); + let other = block_on(hub.set_vault_passphrase(&hub_id(), Secret::new("another passphrase"))); + assert_eq!(other.unwrap(), KeyStatus::RecoveryNeeded); +} + +#[test] +fn an_old_recovery_acknowledgement_cannot_confirm_a_new_reissue() { + let hub = keyed_hub(); + let KeyStatus::Created(first) = sign_in(&hub, "secret").keys else { + panic!("created"); + }; + let second = block_on(hub.reissue_recovery_key(&hub_id())).unwrap(); + assert!(matches!(block_on(first.confirm()), Err(HubError::Stale))); + assert!(hub.recovery_key_unconfirmed(&hub_id())); + block_on(second.confirm()).unwrap(); + assert!(!hub.recovery_key_unconfirmed(&hub_id())); +} + +#[test] +fn a_held_recovery_issue_does_not_choose_a_replacement_account() { + let hub = keyed_hub(); + sign_in(&hub, "secret"); + let action = hub.recovery_key_offer(&hub_id()).unwrap(); + let held = hub.hold(FakeOp::ReissueRecoveryKey); + let pending = action.issue(); + hub.sign_in_as(&hub_id(), "bob"); + held.release(); + assert!(matches!(block_on(pending), Err(HubError::Stale))); +} + +#[test] +fn a_login_receipt_survives_key_lock_and_ordinary_refresh_but_not_a_new_login() { + let hub = keyed_hub(); + let signed = sign_in(&hub, "secret"); + hub.lock_keys(&hub_id()); + hub.unlock_keys(&hub_id()); + block_on(hub.connect(endpoint(pinned()))).unwrap(); + assert!(signed.login.check_current().is_ok()); + hub.sign_in_as(&hub_id(), "ada"); + assert!(matches!(signed.login.discard(), Err(HubError::Stale))); + assert!(hub.is_signed_in(&hub_id())); +} diff --git a/crates/hubs-model/src/trust.rs b/crates/hubs-model/src/trust.rs new file mode 100644 index 00000000..51e2ffaf --- /dev/null +++ b/crates/hubs-model/src/trust.rs @@ -0,0 +1,170 @@ +//! Certificate fingerprints and the decision whether to trust a hub's certificate. + +use std::fmt; +use std::str::FromStr; + +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::error::FingerprintError; + +/// SHA-256 of a hub's leaf certificate. +#[derive(Clone, Copy, PartialEq, Eq, Hash)] +pub struct Fingerprint([u8; 32]); + +impl Fingerprint { + pub const fn from_bytes(bytes: [u8; 32]) -> Self { + Self(bytes) + } + + pub const fn as_bytes(&self) -> &[u8; 32] { + &self.0 + } + + /// Reads the digits a hub prints or a person pastes: hex, any case, with or without + /// colons and spaces. + pub fn parse(text: &str) -> Result { + let digits: Vec = text + .chars() + .filter(|c| *c != ':' && !c.is_whitespace()) + .collect(); + if digits.iter().any(|c| !c.is_ascii_hexdigit()) { + return Err(FingerprintError::NotHex); + } + if digits.len() != 64 { + return Err(FingerprintError::Length { + found: digits.len(), + }); + } + let mut bytes = [0_u8; 32]; + for (byte, pair) in bytes.iter_mut().zip(digits.chunks(2)) { + let high = pair[0].to_digit(16).unwrap_or(0); + let low = pair[1].to_digit(16).unwrap_or(0); + *byte = u8::try_from(high * 16 + low).unwrap_or(0); + } + Ok(Self(bytes)) + } + + /// Eight groups of four digits for reading aloud and comparing by eye. + pub fn groups(&self) -> [String; 8] { + std::array::from_fn(|group| { + self.0[group * 4..group * 4 + 4] + .iter() + .map(|byte| format!("{byte:02x}")) + .collect() + }) + } +} + +impl fmt::Display for Fingerprint { + /// Lower-case pairs separated by colons, the form stored in settings. + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + for (index, byte) in self.0.iter().enumerate() { + if index > 0 { + f.write_str(":")?; + } + write!(f, "{byte:02x}")?; + } + Ok(()) + } +} + +impl fmt::Debug for Fingerprint { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "Fingerprint({self})") + } +} + +impl FromStr for Fingerprint { + type Err = FingerprintError; + + fn from_str(text: &str) -> Result { + Self::parse(text) + } +} + +impl Serialize for Fingerprint { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_str(self) + } +} + +impl<'de> Deserialize<'de> for Fingerprint { + fn deserialize>(deserializer: D) -> Result { + let text = String::deserialize(deserializer)?; + Self::parse(&text).map_err(serde::de::Error::custom) + } +} + +/// How a hub's certificate is checked on every connection. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)] +pub enum TrustPolicy { + /// Exactly this certificate; a different one is an error, never a prompt. + Pinned(Fingerprint), + /// Any certificate a public authority vouches for under the hub's name. + PublicCa, +} + +/// What a TLS handshake without trust showed of a hub. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct ProbedHub { + pub fingerprint: Fingerprint, + /// A public authority vouches for the certificate under the probed name. + pub publicly_trusted: bool, +} + +/// What to do about the certificate a hub presented. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum TrustDecision { + /// The fingerprint the user typed matches: pin it, no question to ask. + Pinned(Fingerprint), + /// A public authority vouches for the certificate and nothing was typed. + PublicCa, + /// A self-signed certificate and nothing typed: show the fingerprint and let the user + /// confirm it against the server's output before pinning. + ConfirmFirstUse(Fingerprint), + /// The typed fingerprint is not the presented one. There is nothing to accept. + Mismatch { + typed: Fingerprint, + presented: Fingerprint, + }, +} + +impl TrustDecision { + /// The policy that needs no further answer, if there is one. + pub const fn settled(&self) -> Option { + match self { + Self::Pinned(fingerprint) => Some(TrustPolicy::Pinned(*fingerprint)), + Self::PublicCa => Some(TrustPolicy::PublicCa), + Self::ConfirmFirstUse(_) | Self::Mismatch { .. } => None, + } + } + + /// The policy after the user confirmed the shown fingerprint; only a first-use + /// decision can be confirmed. + pub const fn confirmed(&self) -> Option { + match self { + Self::ConfirmFirstUse(fingerprint) => Some(TrustPolicy::Pinned(*fingerprint)), + Self::Pinned(_) | Self::PublicCa | Self::Mismatch { .. } => None, + } + } +} + +/// Decides how to treat a probed certificate given the fingerprint the user typed, if any. +/// +/// A typed fingerprint always wins: it must match, even for a certificate a public authority +/// vouches for. Without one, a publicly trusted chain needs no pinning, so certificate +/// rotation stays silent, and a self-signed certificate must be confirmed once. +pub fn decide(probe: &ProbedHub, typed: Option<&Fingerprint>) -> TrustDecision { + match typed { + Some(typed) if *typed == probe.fingerprint => TrustDecision::Pinned(probe.fingerprint), + Some(typed) => TrustDecision::Mismatch { + typed: *typed, + presented: probe.fingerprint, + }, + None if probe.publicly_trusted => TrustDecision::PublicCa, + None => TrustDecision::ConfirmFirstUse(probe.fingerprint), + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs-model/src/trust/tests.rs b/crates/hubs-model/src/trust/tests.rs new file mode 100644 index 00000000..19723ca8 --- /dev/null +++ b/crates/hubs-model/src/trust/tests.rs @@ -0,0 +1,111 @@ +use super::*; + +const PLAIN: &str = "00112233445566778899aabbccddeeff00112233445566778899aabbccddeeff"; + +fn print() -> Fingerprint { + Fingerprint::parse(PLAIN).unwrap() +} + +#[test] +fn a_fingerprint_reads_hex_with_or_without_colons_in_any_case() { + let colons = PLAIN + .as_bytes() + .chunks(2) + .map(|pair| std::str::from_utf8(pair).unwrap()) + .collect::>() + .join(":"); + assert_eq!(Fingerprint::parse(&colons).unwrap(), print()); + assert_eq!(Fingerprint::parse(&PLAIN.to_uppercase()).unwrap(), print()); + assert_eq!( + Fingerprint::parse(&format!(" {PLAIN}\n")).unwrap(), + print() + ); + assert_eq!(print().as_bytes()[1], 0x11); +} + +#[test] +fn a_fingerprint_of_the_wrong_length_or_with_other_characters_is_refused() { + assert_eq!( + Fingerprint::parse(&PLAIN[..62]), + Err(FingerprintError::Length { found: 62 }) + ); + assert_eq!( + Fingerprint::parse(&format!("{PLAIN}ab")), + Err(FingerprintError::Length { found: 66 }) + ); + assert_eq!( + Fingerprint::parse(&format!("{}zz", &PLAIN[..62])), + Err(FingerprintError::NotHex) + ); + assert_eq!( + Fingerprint::parse(""), + Err(FingerprintError::Length { found: 0 }) + ); +} + +#[test] +fn a_fingerprint_is_shown_with_colons_and_in_groups_of_four_digits() { + let shown = print().to_string(); + assert!(shown.starts_with("00:11:22:33:44")); + assert_eq!(shown.len(), 95); + assert_eq!(Fingerprint::parse(&shown).unwrap(), print()); + let groups = print().groups(); + assert_eq!(groups[0], "00112233"); + assert_eq!(groups[7], "ccddeeff"); +} + +#[test] +fn a_typed_fingerprint_that_matches_pins_the_certificate() { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted: false, + }; + let decision = decide(&probe, Some(&print())); + assert_eq!(decision, TrustDecision::Pinned(print())); + assert_eq!(decision.settled(), Some(TrustPolicy::Pinned(print()))); + assert_eq!(decision.confirmed(), None); +} + +#[test] +fn a_typed_fingerprint_that_differs_is_a_mismatch_even_for_a_public_certificate() { + let other = Fingerprint::from_bytes([9; 32]); + for publicly_trusted in [false, true] { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted, + }; + let decision = decide(&probe, Some(&other)); + assert_eq!( + decision, + TrustDecision::Mismatch { + typed: other, + presented: print() + } + ); + assert_eq!(decision.settled(), None); + assert_eq!(decision.confirmed(), None, "a mismatch cannot be accepted"); + } +} + +#[test] +fn a_public_certificate_without_a_typed_fingerprint_is_not_pinned() { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted: true, + }; + let decision = decide(&probe, None); + assert_eq!(decision, TrustDecision::PublicCa); + assert_eq!(decision.settled(), Some(TrustPolicy::PublicCa)); +} + +#[test] +fn a_self_signed_certificate_needs_one_confirmation_before_it_is_pinned() { + let probe = ProbedHub { + fingerprint: print(), + publicly_trusted: false, + }; + let decision = decide(&probe, None); + assert_eq!(decision, TrustDecision::ConfirmFirstUse(print())); + assert_eq!(decision.settled(), None); + assert_eq!(decision.confirmed(), Some(TrustPolicy::Pinned(print()))); +} diff --git a/crates/hubs-ui/Cargo.toml b/crates/hubs-ui/Cargo.toml new file mode 100644 index 00000000..ce36f280 --- /dev/null +++ b/crates/hubs-ui/Cargo.toml @@ -0,0 +1,36 @@ +[package] +name = "nocterm-hubs-ui" +description = "The environment menu, the sign-in dialog and the Servers page of nocterm hubs." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "feature" + +[dependencies] +nocterm-core.workspace = true +nocterm-hubs.workspace = true +nocterm-hub-agents.workspace = true +nocterm-hub-agents-model.workspace = true +nocterm-hubs-model.workspace = true +nocterm-session.workspace = true +nocterm-ui.workspace = true +nocterm-workspace.workspace = true +gpui-kit.workspace = true +thiserror.workspace = true +futures.workspace = true + +[dev-dependencies] +nocterm-hub-agents-model = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } +nocterm-ssh-keys = { workspace = true, features = ["test-support"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-settings.workspace = true +nocterm-store.workspace = true +gpui-kit = { workspace = true, features = ["test-support"] } +nocterm-workspace = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hubs-ui/src/color.rs b/crates/hubs-ui/src/color.rs new file mode 100644 index 00000000..0e805594 --- /dev/null +++ b/crates/hubs-ui/src/color.rs @@ -0,0 +1,49 @@ +//! The colours that mark a server, drawn in the current theme. + +use gpui_kit::{App, Hsla, component::ActiveTheme as _}; +use nocterm_hubs_model::HubColor; + +/// The shade of `color` in the current theme. +pub(crate) fn tint(color: HubColor, cx: &App) -> Hsla { + let theme = cx.theme(); + match color { + HubColor::Red => theme.red, + HubColor::Yellow => theme.yellow, + HubColor::Green => theme.green, + HubColor::Cyan => theme.cyan, + HubColor::Blue => theme.blue, + HubColor::Magenta => theme.magenta, + } +} + +#[cfg(test)] +mod tests { + use super::tint; + use crate::test_support::{fixture, server}; + use gpui_kit::{Hsla, TestAppContext}; + use nocterm_hubs_model::{Environment, HubColor, testing::FakeHub}; + + #[gpui_kit::test] + fn every_colour_has_its_own_shade_in_the_theme(cx: &mut TestAppContext) { + let _window = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + Vec::new(), + None, + ); + let shades: Vec = cx.update(|cx| { + HubColor::ALL + .into_iter() + .map(|color| tint(color, cx)) + .collect() + }); + for (index, shade) in shades.iter().enumerate() { + assert!( + !shades[..index].contains(shade), + "{} repeats an earlier colour", + HubColor::ALL[index].name() + ); + } + } +} diff --git a/crates/hubs-ui/src/commands.rs b/crates/hubs-ui/src/commands.rs new file mode 100644 index 00000000..43e66ca0 --- /dev/null +++ b/crates/hubs-ui/src/commands.rs @@ -0,0 +1,128 @@ +//! The "Hubs" commands of the command palette: open a server in a new window and sign out of +//! one. Each asks which server in a picker, then does what the menu and the Servers page do. + +use gpui_kit::{App, Entity, Window}; +use nocterm_hubs::{HubStatus, HubSummary, Hubs}; +use nocterm_hubs_model::{Environment, HubId}; +use nocterm_ui::{PickerOption, open_choice_picker}; + +use crate::ports::Placement; +use crate::status::status_chip; +use crate::switch::Switch; +use crate::{manage, problems, sign_in}; + +/// Opens `summary` the way its status allows: as an environment when the server can be +/// reached, in the sign-in dialog when it needs a sign-in, or as an explanation when it is in +/// trouble that the user has to deal with. +pub(crate) fn open_server( + hubs: Entity, + summary: HubSummary, + placement: Placement, + switch: Switch, + window: &mut Window, + cx: &mut App, +) { + match summary.status.clone() { + HubStatus::Online | HubStatus::Connecting | HubStatus::Offline { .. } => { + switch.open(Environment::Hub(summary.id), placement, window, cx); + } + HubStatus::SignedOut | HubStatus::Revoked => { + sign_in::start(&hubs, Some(&summary.id), Some(switch), window, cx); + } + HubStatus::TrustFailed { pinned, presented } => { + problems::open_trust_failed(hubs, summary, pinned, presented, window, cx); + } + HubStatus::UpgradeRequired { hub, client } => { + problems::open_upgrade_required(&summary, hub, client, window, cx); + } + } +} + +/// Whether the server holds credentials of this device that signing out would erase. A server +/// whose certificate changed has none to offer here: removing it is the way out, and signing +/// out would hide the warning until the next connection attempt. +pub(crate) fn can_sign_out(summary: &HubSummary) -> bool { + !matches!( + summary.status, + HubStatus::SignedOut | HubStatus::Revoked | HubStatus::TrustFailed { .. } + ) +} + +fn options(servers: Vec, cx: &App) -> Vec> { + servers + .into_iter() + .map(|summary| { + let (status, _) = status_chip(&summary.status, cx); + let label = format!("{} · {} · {status}", summary.label, summary.address); + PickerOption::new(label, false, summary.id) + }) + .collect() +} + +/// Asks which known server to open in a new window. +pub(crate) fn pick_server_to_open( + hubs: &Entity, + switch: Switch, + window: &mut Window, + cx: &mut App, +) { + let servers = hubs.read(cx).list(); + if servers.is_empty() { + sign_in::start(hubs, None, Some(switch), window, cx); + return; + } + let picks = options(servers, cx); + let hubs = hubs.clone(); + open_choice_picker( + "Open a server in a new window", + picks, + move |id, window, cx| { + let Some(summary) = hubs.read(cx).summary(&id) else { + return; + }; + open_server( + hubs.clone(), + summary, + Placement::NewWindow, + switch.clone(), + window, + cx, + ); + }, + window, + cx, + ); +} + +/// Asks which signed-in server to sign out of. +pub(crate) fn pick_server_to_sign_out(hubs: &Entity, window: &mut Window, cx: &mut App) { + let servers: Vec = hubs + .read(cx) + .list() + .into_iter() + .filter(can_sign_out) + .collect(); + if servers.is_empty() { + nocterm_ui::notice::info( + window, + cx, + "hub-manage", + "No server to sign out of", + "This device is not signed in to any server.".to_owned(), + ); + return; + } + let picks = options(servers, cx); + let hubs = hubs.clone(); + open_choice_picker( + "Sign out of a server", + picks, + move |id, window, cx| manage::sign_out(&hubs, &id, window, cx), + window, + cx, + ); +} + +#[cfg(test)] +#[path = "commands_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/commands_tests.rs b/crates/hubs-ui/src/commands_tests.rs new file mode 100644 index 00000000..646a478e --- /dev/null +++ b/crates/hubs-ui/src/commands_tests.rs @@ -0,0 +1,108 @@ +//! The Hubs commands of the palette pick a known server and act on it. +use crate::test_support::{Fixture, fixture, hub_id, pinned_record, server}; +use crate::{OpenServerInNewWindow, Placement, SignInToServer, SignOutOfServer}; +use gpui_kit::{AppContext as _, TestAppContext}; +use nocterm_hubs::HubStatus; +use nocterm_hubs_model::{Environment, Fingerprint, HubRecord, TrustPolicy, testing::FakeHub}; + +fn window(cx: &mut TestAppContext, known: Vec, signed_in: Option<&str>) -> Fixture { + fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + known, + signed_in, + ) +} + +fn status(window: &Fixture, cx: &mut TestAppContext) -> Option { + cx.update(|cx| window.installed.hubs().read(cx).status(&hub_id())) +} + +fn notices(window: &Fixture, cx: &mut TestAppContext) -> usize { + cx.update_window(window.window, |_, window, cx| { + nocterm_ui::notice::count(window, cx) + }) + .unwrap() +} + +#[gpui_kit::test] +fn the_chosen_server_opens_in_a_new_window(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], Some("ada")); + window.dispatch(OpenServerInNewWindow, cx); + assert!(window.has_dialog(cx), "the servers are offered"); + window.press("enter", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn dismissing_the_picker_opens_nothing(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], Some("ada")); + window.dispatch(OpenServerInNewWindow, cx); + window.press("escape", cx); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn a_server_that_needs_signing_in_opens_the_sign_in_dialog_instead(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], None); + window.dispatch(OpenServerInNewWindow, cx); + window.press("enter", cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn a_changed_certificate_is_explained_and_never_opened(cx: &mut TestAppContext) { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let window = window(cx, vec![changed], Some("ada")); + window.dispatch(OpenServerInNewWindow, cx); + window.press("enter", cx); + window.settle_dialog(cx); + assert!(window.shows("trust-failed-remove", cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn without_known_servers_opening_one_starts_signing_in(cx: &mut TestAppContext) { + let window = window(cx, Vec::new(), None); + window.dispatch(OpenServerInNewWindow, cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn signing_out_of_the_chosen_server_keeps_it_listed(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], Some("ada")); + window.dispatch(SignOutOfServer, cx); + assert!(window.has_dialog(cx), "the signed-in servers are offered"); + window.press("enter", cx); + cx.run_until_parked(); + assert_eq!(status(&window, cx), Some(HubStatus::SignedOut)); + assert!(!window.hub.is_signed_in(&hub_id())); +} + +#[gpui_kit::test] +fn with_no_signed_in_server_signing_out_says_so(cx: &mut TestAppContext) { + let window = window(cx, vec![pinned_record()], None); + window.dispatch(SignOutOfServer, cx); + assert!(!window.has_dialog(cx), "nothing to choose from"); + assert_eq!(notices(&window, cx), 1); +} + +#[gpui_kit::test] +fn the_sign_in_command_still_opens_the_sign_in_dialog(cx: &mut TestAppContext) { + let window = window(cx, Vec::new(), None); + window.dispatch(SignInToServer, cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); +} diff --git a/crates/hubs-ui/src/installation.rs b/crates/hubs-ui/src/installation.rs new file mode 100644 index 00000000..54e15d1f --- /dev/null +++ b/crates/hubs-ui/src/installation.rs @@ -0,0 +1,45 @@ +//! The hubs installation windows are composed from. + +use gpui_kit::{App, Entity}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::HubId; + +/// The registry of known hubs, kept by the application and given to each window. +#[derive(Clone)] +pub struct Installed { + hubs: nocterm_hubs::Installed, + hub_agents: Option>, +} + +impl Installed { + /// The name the user gave the server, or `None` when it is not in the list. + pub fn label(&self, hub: &HubId, cx: &App) -> Option { + self.hubs() + .read(cx) + .summary(hub) + .map(|summary| summary.label) + } + + /// The Servers page also manages the account's agents, providers and agent servers + /// through `agents`. + pub fn with_hub_agents(mut self, agents: Entity) -> Self { + self.hub_agents = Some(agents); + self + } + + pub(crate) fn hub_agents(&self) -> Option<&Entity> { + self.hub_agents.as_ref() + } + + pub(crate) fn hubs(&self) -> &Entity { + self.hubs.hubs() + } +} + +/// Keeps the service installation for the views of every window. +pub fn install(hubs: nocterm_hubs::Installed) -> Installed { + Installed { + hubs, + hub_agents: None, + } +} diff --git a/crates/hubs-ui/src/layout.rs b/crates/hubs-ui/src/layout.rs new file mode 100644 index 00000000..949dbd01 --- /dev/null +++ b/crates/hubs-ui/src/layout.rs @@ -0,0 +1,61 @@ +//! Small pieces the steps of the sign-in dialog are built from. + +use gpui_kit::{ + App, IntoElement, SharedString, + component::{ActiveTheme as _, StyledExt as _, v_flex}, + div, + prelude::*, +}; +use nocterm_hubs_model::Fingerprint; + +/// A labelled field. +pub(crate) fn field(label: &'static str, input: impl IntoElement) -> impl IntoElement { + v_flex() + .gap_1() + .child(div().text_xs().font_semibold().child(label)) + .child(input) +} + +pub(crate) fn title(text: String, _: &App) -> impl IntoElement { + div().font_semibold().child(text) +} + +/// A quiet sentence. +pub(crate) fn note(text: impl Into, cx: &App) -> impl IntoElement { + div() + .text_sm() + .text_color(cx.theme().muted_foreground) + .child(text.into()) +} + +/// A sentence about what went wrong. +pub(crate) fn problem(text: String, cx: &App) -> impl IntoElement { + div().text_sm().text_color(cx.theme().danger).child(text) +} + +/// A fingerprint in groups of four bytes, which are easier to compare by eye. +pub(crate) fn fingerprint_block( + label: &'static str, + fingerprint: &Fingerprint, + cx: &App, +) -> impl IntoElement { + let groups = fingerprint.groups(); + v_flex() + .gap_1() + .child( + div() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(label), + ) + .child( + div() + .font_family(cx.theme().mono_font_family.clone()) + .text_sm() + .child(format!( + "{}\n{}", + groups[..4].join(" "), + groups[4..].join(" ") + )), + ) +} diff --git a/crates/hubs-ui/src/lib.rs b/crates/hubs-ui/src/lib.rs new file mode 100644 index 00000000..008369a3 --- /dev/null +++ b/crates/hubs-ui/src/lib.rs @@ -0,0 +1,86 @@ +//! The environment menu, the sign-in dialog and the Servers page of nocterm hubs. +//! +//! A window shows one environment: this computer, or the account of one hub. This crate gives +//! the window the title-bar menu that switches between them, the dialog that signs in to a +//! server, the commands that sign in to, open and sign out of one, and the Servers page of the +//! settings. +//! +//! Owns: the presentation of the known hubs and their status ([`register`] installs the menu); +//! the Servers page of the settings ([`settings_page`]), a list of servers and, for the chosen +//! one, the tabs Overview, Devices (listed and revoked through the service), Keys (the vault +//! keys of the account, opened and closed on this device, and a new recovery key), Agents (the +//! account's agents and model providers, through `nocterm-hub-agents`) and Agent access (the +//! agent key and the servers connected to agents, checked and disconnected), and the palette +//! commands; the steps of the sign-in dialog as +//! the service orders them; the confirmation before a window with tabs switches. +//! +//! Does not own: the registry of hubs and the order of sign-in (`nocterm-hubs`); hub I/O and +//! trust rules (`nocterm-hubs-model` and the injected adapter); composing windows (the +//! application, through [`EnvironmentOpener`]); terminal tabs. +//! +//! Ports: uses [`EnvironmentOpener`], implemented by the application; it contributes no panel +//! and no plugin. +//! +//! Settings: none of its own; the service owns the `[hubs]` section and the Servers page edits +//! it through the service. +//! +//! Globals: none. +//! +//! Invariants: a certificate that is not the trusted one is never offered for acceptance, on +//! the Servers page as in the menu: removing the server is the way out; the trust step is never +//! confirmed by Enter alone; a password is cleared from its field when it is sent; switching a +//! window that has tabs asks first; closing the sign-in dialog cancels a sign-in in flight. + +mod color; +mod commands; +mod installation; +mod layout; +mod manage; +mod menu; +mod messages; +mod ports; +mod problems; +mod servers; +mod sign_in; +mod status; +mod switch; + +pub use installation::{Installed, install}; +pub use menu::register; +pub use ports::{EnvironmentOpenError, EnvironmentOpener, Placement}; +pub use servers::settings_page; +pub use sign_in::sign_in_again; + +gpui_kit::actions!( + hubs, + [ + /// Open the environment menu in the title bar. + OpenEnvironmentMenu, + /// Sign in to a server that is not in the list yet. + SignInToServer, + /// Open the Servers page of the settings, where known servers are renamed, coloured, signed out of or removed. + ManageServers, + /// Choose a known server and open it in a new window. + OpenServerInNewWindow, + /// Choose a signed-in server and sign this device out of it. + SignOutOfServer, + ] +); + +#[cfg(test)] +mod test_support; + +#[cfg(test)] +mod sign_in_action_tests { + #[gpui_kit::test] + fn the_sign_in_action_other_features_dispatch_by_name_exists( + cx: &mut gpui_kit::TestAppContext, + ) { + cx.update(|cx| { + let action = cx + .build_action(nocterm_workspace::SIGN_IN_TO_SERVER_ACTION, None) + .expect("registered"); + assert!(action.partial_eq(&crate::SignInToServer)); + }); + } +} diff --git a/crates/hubs-ui/src/manage.rs b/crates/hubs-ui/src/manage.rs new file mode 100644 index 00000000..58611c3f --- /dev/null +++ b/crates/hubs-ui/src/manage.rs @@ -0,0 +1,100 @@ +//! What the user does to a server they already know: sign out, remove, rename, colour. Each +//! starts the service call and tells the user when it did not work; the service keeps the +//! rules. + +use gpui_kit::{ + App, ClickEvent, Entity, Task, Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + }, + prelude::*, +}; +use nocterm_hubs::{HubSummary, Hubs, HubsError}; +use nocterm_hubs_model::{HubColor, HubId}; + +use crate::messages::hubs_problem; + +/// Shows a notice titled `title` when `task` fails, once it has finished. +pub(crate) fn report( + task: Task>, + title: &'static str, + window: &mut Window, + cx: &mut App, +) { + window + .spawn(cx, async move |cx| { + if let Err(error) = task.await { + cx.update(|window, cx| { + nocterm_ui::notice::error( + window, + cx, + "hub-manage", + title, + hubs_problem(&error), + ); + }) + .ok(); + } + }) + .detach(); +} + +/// Ends this device's session on the server. The server stays in the list, signed out. +pub(crate) fn sign_out(hubs: &Entity, hub: &HubId, window: &mut Window, cx: &mut App) { + let signing_out = hubs.update(cx, |hubs, cx| hubs.sign_out(hub, cx)); + report(signing_out, "Could not sign out", window, cx); +} + +/// Forgets the server and its credentials. +pub(crate) fn remove(hubs: &Entity, hub: &HubId, window: &mut Window, cx: &mut App) { + let removing = hubs.update(cx, |hubs, cx| hubs.remove(hub, cx)); + report(removing, "Could not remove the server", window, cx); +} + +/// Marks the server with `color`, or clears the mark. +pub(crate) fn recolor( + hubs: &Entity, + hub: &HubId, + color: Option, + window: &mut Window, + cx: &mut App, +) { + let marking = hubs.update(cx, |hubs, cx| hubs.set_color(hub, color, cx)); + report(marking, "Could not change the colour", window, cx); +} + +/// Asks before removing a server: it leaves this device's list and its credentials are +/// erased. Sessions on the server itself are not touched. +pub(crate) fn confirm_remove( + hubs: Entity, + hub: HubSummary, + window: &mut Window, + cx: &mut App, +) { + window.open_alert_dialog(cx, move |alert, _, _| { + let (hubs, id) = (hubs.clone(), hub.id.clone()); + let confirm = Button::new("remove-server-confirm") + .danger() + .label("Remove server") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + remove(&hubs, &id, window, cx); + }); + let keep = Button::new("remove-server-keep") + .primary() + .label("Keep it") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + }); + alert + .title(format!("Remove {}?", hub.label)) + .description(format!( + "{} leaves the list and this device is signed out of it. Its sessions keep running on the server.", + hub.address + )) + .on_ok(|_, _, _| true) + .footer(DialogFooter::new().child(confirm).child(keep)) + }); +} diff --git a/crates/hubs-ui/src/menu.rs b/crates/hubs-ui/src/menu.rs new file mode 100644 index 00000000..d16762c0 --- /dev/null +++ b/crates/hubs-ui/src/menu.rs @@ -0,0 +1,400 @@ +//! The environment menu in the title bar: this computer, every known server with its +//! status, and a way to sign in to another one. + +use std::rc::Rc; +use std::time::Duration; + +use gpui_kit::{ + Anchor, App, AppContext as _, ClickEvent, Context, Entity, EventEmitter, FocusHandle, + Focusable, Hsla, KeyDownEvent, SharedString, Subscription, Task, TestSupportExt as _, Window, + base::actions::Confirm, + component::{ + ActiveTheme as _, Icon, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, + popover::Popover, + v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::{HubSummary, Hubs, HubsEvent}; +use nocterm_hubs_model::Environment; +use nocterm_ui::IconName; +use nocterm_workspace::Workspace; + +use crate::color::tint; +use crate::commands::{open_server, pick_server_to_open, pick_server_to_sign_out}; +use crate::installation::Installed; +use crate::ports::{EnvironmentOpener, Placement}; +use crate::status::{status_chip, status_icon}; +use crate::switch::Switch; +use crate::{OpenEnvironmentMenu, OpenServerInNewWindow, SignInToServer, SignOutOfServer, sign_in}; + +/// How often the countdown of an offline server is redrawn while the menu shows. +const TICK: Duration = Duration::from_secs(1); + +/// Puts the environment menu in the title bar of `workspace` and handles +/// [`OpenEnvironmentMenu`], [`SignInToServer`], [`OpenServerInNewWindow`] and +/// [`SignOutOfServer`] there. `environment` is what the window shows; opening it again does +/// nothing. +pub fn register( + installed: &Installed, + environment: Environment, + opener: Rc, + workspace: &mut Workspace, + window: &mut Window, + cx: &mut gpui_kit::Context, +) { + let switch = Switch::new(opener, cx.weak_entity(), environment); + let hubs = installed.hubs().clone(); + let menu = cx.new(|cx| EnvironmentMenu::new(hubs.clone(), switch.clone(), window, cx)); + workspace.set_environment_menu(menu.clone(), cx); + let opening = menu.downgrade(); + workspace.register_action(move |_, _: &OpenEnvironmentMenu, _, cx| { + let _ = opening.update(cx, |menu, cx| menu.set_open(true, cx)); + }); + let (signing_in, opening, signing_out) = (hubs.clone(), hubs.clone(), hubs); + let (sign_in_switch, open_switch) = (switch.clone(), switch); + workspace.register_action(move |_, _: &SignInToServer, window, cx| { + sign_in::start(&signing_in, None, Some(sign_in_switch.clone()), window, cx); + }); + workspace.register_action(move |_, _: &OpenServerInNewWindow, window, cx| { + let (hubs, switch) = (opening.clone(), open_switch.clone()); + window.defer(cx, move |window, cx| { + pick_server_to_open(&hubs, switch, window, cx); + }); + }); + workspace.register_action(move |_, _: &SignOutOfServer, window, cx| { + let hubs = signing_out.clone(); + window.defer(cx, move |window, cx| { + pick_server_to_sign_out(&hubs, window, cx); + }); + }); +} + +/// The title-bar button and the list it opens. +struct EnvironmentMenu { + hubs: Entity, + environment: Environment, + list: Entity, + open: bool, + _subscriptions: Vec, +} + +impl EnvironmentMenu { + fn new(hubs: Entity, switch: Switch, _: &mut Window, cx: &mut Context) -> Self { + let environment = switch.current().clone(); + let list = cx.new(|cx| EnvironmentList::new(hubs.clone(), switch, cx)); + let subscriptions = vec![ + cx.observe(&hubs, |_, _, cx| cx.notify()), + cx.subscribe(&list, |menu, _, _: &ListDone, cx| menu.set_open(false, cx)), + ]; + Self { + hubs, + environment, + list, + open: false, + _subscriptions: subscriptions, + } + } + + fn set_open(&mut self, open: bool, cx: &mut Context) { + if self.open == open { + return; + } + self.open = open; + self.list.update(cx, |list, cx| list.shown(open, cx)); + cx.notify(); + } + + /// The colour of the server this window shows, when the owner chose one. + fn mark(&self, cx: &App) -> Option { + let Environment::Hub(id) = &self.environment else { + return None; + }; + let color = self.hubs.read(cx).summary(id)?.color?; + Some(tint(color, cx)) + } + + fn label(&self, cx: &App) -> SharedString { + match &self.environment { + Environment::Local => "This computer".into(), + Environment::Hub(id) => self + .hubs + .read(cx) + .summary(id) + .map_or_else(|| id.address().to_string(), |summary| summary.label) + .into(), + } + } +} + +impl Render for EnvironmentMenu { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let icon = match self.environment { + Environment::Local => IconName::Terminal, + Environment::Hub(_) => IconName::Server, + }; + let mark = self.mark(cx); + let trigger = Button::new("environment-menu") + .ghost() + .small() + .icon(match mark { + Some(color) => Icon::new(icon).text_color(color), + None => Icon::new(icon), + }) + .label(self.label(cx)); + let list = self.list.clone(); + let focus = list.read(cx).focus_handle(cx); + let menu = cx.weak_entity(); + div().flex().items_center().h_full().child( + Popover::new("environment-popover") + .anchor(Anchor::BottomLeft) + .trigger(trigger) + .track_focus(&focus) + .open(self.open) + .on_open_change(move |open, _, cx| { + let _ = menu.update(cx, |menu, cx| menu.set_open(*open, cx)); + }) + .content(move |_, _, _| list.clone()), + ) + } +} + +/// The list asks its menu to close. +struct ListDone; + +/// One line of the list. +#[derive(Clone)] +enum Row { + Local, + Hub(HubSummary), + SignIn, +} + +impl Row { + fn environment(&self) -> Option { + match self { + Self::Local => Some(Environment::Local), + Self::Hub(summary) => Some(Environment::Hub(summary.id.clone())), + Self::SignIn => None, + } + } +} + +struct EnvironmentList { + hubs: Entity, + switch: Switch, + selected: usize, + focus: FocusHandle, + /// Redraws the countdown of offline servers while the menu is open. + tick: Option>, + _subscriptions: Vec, +} + +impl EventEmitter for EnvironmentList {} + +impl EnvironmentList { + fn new(hubs: Entity, switch: Switch, cx: &mut Context) -> Self { + let subscriptions = vec![cx.subscribe(&hubs, |_, _, _: &HubsEvent, cx| cx.notify())]; + Self { + hubs, + switch, + selected: 0, + focus: cx.focus_handle(), + tick: None, + _subscriptions: subscriptions, + } + } + + fn rows(&self, cx: &App) -> Vec { + std::iter::once(Row::Local) + .chain(self.hubs.read(cx).list().into_iter().map(Row::Hub)) + .chain(std::iter::once(Row::SignIn)) + .collect() + } + + /// The menu opened or closed. An open menu starts on the row of the environment the + /// window shows and counts down the retries of offline servers. + fn shown(&mut self, open: bool, cx: &mut Context) { + if !open { + self.tick = None; + return; + } + let current = self.switch.current().clone(); + self.selected = self + .rows(cx) + .iter() + .position(|row| row.environment().as_ref() == Some(¤t)) + .unwrap_or(0); + self.tick = Some(cx.spawn(async move |this, cx| { + loop { + cx.background_executor().timer(TICK).await; + if this.update(cx, |_, cx| cx.notify()).is_err() { + break; + } + } + })); + cx.notify(); + } + + fn move_selection(&mut self, down: bool, cx: &mut Context) { + let rows = self.rows(cx).len(); + self.selected = if down { + (self.selected + 1) % rows + } else { + (self.selected + rows - 1) % rows + }; + cx.notify(); + } + + /// Does what a row stands for: opens the environment, or deals with the trouble the + /// server is in. + fn activate( + &mut self, + row: Row, + placement: Placement, + window: &mut Window, + cx: &mut Context, + ) { + cx.emit(ListDone); + let (hubs, switch) = (self.hubs.clone(), self.switch.clone()); + // The dialogs open once the popover has let go of the focus. + window.defer(cx, move |window, cx| match row { + Row::Local => switch.open(Environment::Local, placement, window, cx), + Row::SignIn => sign_in::start(&hubs, None, Some(switch), window, cx), + Row::Hub(summary) => open_server(hubs, summary, placement, switch, window, cx), + }); + } + + fn on_key(&mut self, event: &KeyDownEvent, window: &mut Window, cx: &mut Context) { + let key = event.keystroke.key.as_str(); + match key { + "up" | "down" => { + self.move_selection(key == "down", cx); + cx.stop_propagation(); + } + "enter" if event.keystroke.modifiers.secondary() => { + self.activate_selected(Placement::NewWindow, window, cx); + cx.stop_propagation(); + } + _ => {} + } + } + + fn activate_selected( + &mut self, + placement: Placement, + window: &mut Window, + cx: &mut Context, + ) { + if let Some(row) = self.rows(cx).get(self.selected).cloned() { + self.activate(row, placement, window, cx); + } + } + + fn render_row(&self, index: usize, row: &Row, cx: &mut Context) -> gpui_kit::AnyElement { + let theme = cx.theme(); + let (muted, hover, accent) = (theme.muted_foreground, theme.accent, theme.accent); + let icon_color = match row { + Row::Hub(summary) => summary.color.map_or(muted, |color| tint(color, cx)), + _ => muted, + }; + let (icon, label, chip) = match row { + Row::Local => (IconName::Terminal, "This computer".to_owned(), None), + Row::Hub(summary) => ( + status_icon(&summary.status), + summary.label.clone(), + Some(status_chip(&summary.status, cx)), + ), + Row::SignIn => ( + IconName::ServerPlus, + "Sign in to a server…".to_owned(), + None, + ), + }; + let current = row.environment().as_ref() == Some(self.switch.current()); + let opens_elsewhere = row.environment().is_some(); + let (row_click, row_new) = (row.clone(), row.clone()); + h_flex() + .id(("environment-row", index)) + .test_support() + .gap_2() + .px_2() + .py_1() + .items_center() + .rounded(theme.radius) + .cursor_pointer() + .hover(|style| style.bg(hover)) + .when(index == self.selected, |style| style.bg(accent)) + .child(Icon::new(icon).small().text_color(icon_color)) + .child(div().flex_1().min_w_0().truncate().text_sm().child(label)) + .when(current, |style| { + style.child(Icon::new(IconName::Check).small().text_color(muted)) + }) + .when_some(chip, |style, (text, color)| { + style.child(div().text_xs().text_color(color).child(text)) + }) + .when(opens_elsewhere, |style| { + style.child( + Button::new(("environment-new-window", index)) + .ghost() + .xsmall() + .icon(IconName::ArrowUpRight) + .tooltip("Open in a new window") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + cx.stop_propagation(); + this.activate(row_new.clone(), Placement::NewWindow, window, cx); + })), + ) + }) + .on_click(cx.listener(move |this, event: &ClickEvent, window, cx| { + let placement = if event.modifiers().secondary() { + Placement::NewWindow + } else { + Placement::ThisWindow + }; + this.activate(row_click.clone(), placement, window, cx); + })) + .into_any_element() + } +} + +impl Focusable for EnvironmentList { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +impl Render for EnvironmentList { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let rows = self.rows(cx); + let last = rows.len().saturating_sub(1); + let border = cx.theme().border; + v_flex() + .id("environment-list") + .track_focus(&self.focus) + .key_context("EnvironmentMenu") + .w(px(340.)) + .gap_px() + .on_key_down(cx.listener(|this, event: &KeyDownEvent, window, cx| { + this.on_key(event, window, cx); + })) + .on_action(cx.listener(|this, _: &Confirm, window, cx| { + this.activate_selected(Placement::ThisWindow, window, cx); + })) + .children(rows.iter().enumerate().flat_map(|(index, row)| { + let separator = + (index == last).then(|| div().my_1().h_px().bg(border).into_any_element()); + separator + .into_iter() + .chain(std::iter::once(self.render_row(index, row, cx))) + })) + } +} + +#[cfg(test)] +#[path = "menu_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/menu_tests.rs b/crates/hubs-ui/src/menu_tests.rs new file mode 100644 index 00000000..821118fb --- /dev/null +++ b/crates/hubs-ui/src/menu_tests.rs @@ -0,0 +1,238 @@ +//! The environment menu lists this computer and the known servers, and opens what is chosen. +use crate::status::chip; +use crate::test_support::{Fixture, fixture, fixture_over, hub_id, pinned_record, server}; +use crate::{OpenEnvironmentMenu, Placement}; +use gpui_kit::{AppContext as _, Hsla, TestAppContext, hsla}; +use nocterm_hubs::HubStatus; +use nocterm_hubs_model::{Environment, Fingerprint, HubRecord, TrustPolicy, testing::FakeHub}; +use nocterm_settings::SettingsDocument; +use nocterm_store::{SettingsConfig, SnapshotWriter, StoreError}; +use std::sync::Arc; +use std::time::{Duration, Instant}; + +fn local_window(cx: &mut TestAppContext, known: Vec) -> Fixture { + fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + known, + Some("ada"), + ) +} + +fn open_menu(window: &Fixture, cx: &mut TestAppContext) { + window.dispatch(OpenEnvironmentMenu, cx); +} + +#[gpui_kit::test] +fn without_servers_the_menu_offers_this_computer_and_signing_in(cx: &mut TestAppContext) { + let window = local_window(cx, Vec::new()); + open_menu(&window, cx); + assert!( + window.shows(("environment-row", 0usize), cx), + "this computer" + ); + assert!(window.shows(("environment-row", 1usize), cx), "sign in"); + assert!(!window.shows(("environment-row", 2usize), cx)); +} + +#[gpui_kit::test] +fn each_known_server_gets_a_row_between_this_computer_and_signing_in(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + assert!(window.shows(("environment-row", 2usize), cx)); + assert!(!window.shows(("environment-row", 3usize), cx)); +} + +#[gpui_kit::test] +fn clicking_a_signed_in_server_opens_it_in_this_window(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::ThisWindow)] + ); +} + +#[gpui_kit::test] +fn the_new_window_button_of_a_row_opens_it_beside_this_window(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + window.click(("environment-new-window", 1usize), cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn enter_opens_the_selected_row_and_ctrl_enter_opens_it_in_a_new_window(cx: &mut TestAppContext) { + let window = local_window(cx, vec![pinned_record()]); + open_menu(&window, cx); + window.press("down", cx); + window.press("enter", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::ThisWindow)] + ); + open_menu(&window, cx); + window.press("down", cx); + window.press("ctrl-enter", cx); + assert_eq!( + window.environments.requests().last(), + Some(&(Environment::Hub(hub_id()), Placement::NewWindow)) + ); +} + +#[gpui_kit::test] +fn a_server_that_needs_a_new_certificate_decision_opens_no_window(cx: &mut TestAppContext) { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let window = local_window(cx, vec![changed]); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); + assert!( + window.has_dialog(cx), + "the certificate problem is explained" + ); + assert!(window.shows("trust-failed-close", cx)); + assert!(window.shows("trust-failed-remove", cx)); + assert!( + window.environments.requests().is_empty(), + "a changed certificate is never opened, let alone accepted" + ); +} + +fn changed_certificate_window(cx: &mut TestAppContext, config: SettingsConfig) -> Fixture { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let window = fixture_over( + cx, + config, + Environment::Local, + FakeHub::new().with_server(server()), + vec![changed], + Some("ada"), + ); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); + window +} + +#[gpui_kit::test] +fn removing_a_server_whose_certificate_changed_drops_its_row(cx: &mut TestAppContext) { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let window = changed_certificate_window(cx, config); + window.click("trust-failed-remove", cx); + cx.run_until_parked(); + assert!(!window.has_dialog(cx)); + open_menu(&window, cx); + assert!(!window.shows(("environment-row", 2usize), cx)); +} + +#[gpui_kit::test] +fn a_server_that_cannot_be_removed_says_so_instead_of_staying_listed_silently( + cx: &mut TestAppContext, +) { + let writer: SnapshotWriter = Arc::new(|_| { + Box::pin(async { + Err(StoreError::backend(std::io::Error::from( + std::io::ErrorKind::PermissionDenied, + ))) + }) + }); + let config = SettingsConfig::with_writer(SettingsDocument::default(), writer); + let window = changed_certificate_window(cx, config); + window.click("trust-failed-remove", cx); + cx.run_until_parked(); + window.render(cx); + let notices = cx + .update_window(window.window, |_, window, cx| { + nocterm_ui::notice::count(window, cx) + }) + .unwrap(); + assert_eq!(notices, 1, "the failure is reported"); +} + +#[gpui_kit::test] +fn a_signed_out_server_opens_the_sign_in_dialog(cx: &mut TestAppContext) { + let window = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + vec![pinned_record()], + None, + ); + open_menu(&window, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn this_computer_row_opens_nothing_in_a_local_window(cx: &mut TestAppContext) { + let window = local_window(cx, Vec::new()); + open_menu(&window, cx); + window.click(("environment-row", 0usize), cx); + assert!(window.environments.requests().is_empty()); +} + +fn colors() -> [Hsla; 4] { + [ + hsla(0.3, 1., 0.5, 1.), + hsla(0., 0., 0.5, 1.), + hsla(0.1, 1., 0.5, 1.), + hsla(0., 1., 0.5, 1.), + ] +} + +fn text(status: &HubStatus, now: Instant) -> String { + let [good, quiet, warn, bad] = colors(); + chip(status, now, good, quiet, warn, bad).0.to_string() +} + +#[test] +fn every_status_has_a_chip_and_an_offline_server_counts_down() { + let now = Instant::now(); + let fingerprint = Fingerprint::from_bytes([1; 32]); + assert_eq!(text(&HubStatus::Online, now), "Online"); + assert_eq!(text(&HubStatus::Connecting, now), "Connecting…"); + assert_eq!(text(&HubStatus::SignedOut, now), "Sign-in required"); + assert_eq!(text(&HubStatus::Revoked, now), "Device revoked"); + assert_eq!( + text( + &HubStatus::TrustFailed { + pinned: Some(fingerprint), + presented: fingerprint + }, + now + ), + "Certificate changed" + ); + assert_eq!( + text(&HubStatus::UpgradeRequired { hub: 2, client: 1 }, now), + "Update Nocterm" + ); + let offline = HubStatus::Offline { + retry_at: now + Duration::from_secs(5), + }; + assert_eq!(text(&offline, now), "Offline · retry in 5 s"); + assert_eq!( + text(&offline, now + Duration::from_millis(1500)), + "Offline · retry in 4 s" + ); + assert_eq!( + text(&offline, now + Duration::from_secs(9)), + "Offline · retry in 0 s" + ); +} diff --git a/crates/hubs-ui/src/messages.rs b/crates/hubs-ui/src/messages.rs new file mode 100644 index 00000000..71453656 --- /dev/null +++ b/crates/hubs-ui/src/messages.rs @@ -0,0 +1,140 @@ +//! The words the views use for the failures of the hub service. The service reports typed +//! errors; this is the only place that turns them into text. + +use nocterm_hub_agents_model::HubAgentsError; +use nocterm_hubs::{HubsError, SignInError}; +use nocterm_hubs_model::HubError; + +/// Why a sign-in step did not work, for the dialog. +pub(crate) fn sign_in_problem(error: &SignInError) -> String { + match error { + SignInError::Address(error) => capitalize(&error.to_string()), + SignInError::Fingerprint(error) => capitalize(&error.to_string()), + SignInError::Hub(error) => hub_problem(error), + SignInError::AlreadyKnown(hub) => format!( + "{hub} is in the list already. Use its row to sign in again, or remove it first." + ), + SignInError::NotInitialized => { + "This server has no owner yet. Set it up first, then sign in here.".to_owned() + } + SignInError::Save(_) => { + "The server could not be saved to the settings, so this device was signed out of it again." + .to_owned() + } + } +} + +/// Why forgetting a server or signing out of it did not work. +pub(crate) fn hubs_problem(error: &HubsError) -> String { + match error { + HubsError::Unknown(hub) => format!("{hub} is not in the list."), + HubsError::Hub(error) => hub_problem(error), + HubsError::Save(_) => { + "The settings could not be saved, so the server stays listed.".to_owned() + } + } +} + +/// What a failed call of the agents or servers of a hub means to the person. +pub(crate) fn agents_problem(error: &HubAgentsError) -> String { + match error { + HubAgentsError::Hub(error) => hub_problem(error), + HubAgentsError::Draft(error) => error.to_string(), + HubAgentsError::Malformed => { + "The server answered with something this version cannot read. Update Nocterm." + .to_owned() + } + HubAgentsError::LimitReached => { + "A limit set by the server's administrator is reached.".to_owned() + } + } +} + +/// What a failed call on a hub means to the person using it. +pub(crate) fn hub_problem(error: &HubError) -> String { + match error { + HubError::Unreachable { address, .. } => { + format!("Cannot reach {address}. Check the address and your network.") + } + HubError::CertificateChanged { address, .. } => { + format!("The certificate of {address} is not the one that was pinned.") + } + HubError::CertificateUntrusted { address, .. } => { + format!("The certificate of {address} is not trusted.") + } + HubError::UpgradeRequired { hub, client } => format!( + "This server speaks protocol {hub} and Nocterm speaks {client}. Update the older one." + ), + HubError::InvalidCredentials => "The user name or password was not accepted.".to_owned(), + HubError::Stale => "The account changed. Start this action again for the current account.".to_owned(), + HubError::RecoveryKeyInvalid => { + "That is not the recovery key of this account. Check it and try again.".to_owned() + } + HubError::InvalidVaultPassphrase => { + "The vault passphrase is too short. Use at least eight characters.".to_owned() + } + HubError::KeysLocked => { + "The account keys are closed on this device. Unlock the vault or sign in with your password first." + .to_owned() + } + HubError::UnlockSourceMismatch { + needs_login_password: true, + } => "These account keys are opened with your login password, not with a vault passphrase. Sign in with the password instead." + .to_owned(), + HubError::UnlockSourceMismatch { + needs_login_password: false, + } => "These account keys are opened with a vault passphrase, not with your login password. Type the passphrase." + .to_owned(), + HubError::KeysetDamaged { .. } => { + "The account keys on the server do not verify. The server may have been tampered with; nothing was changed." + .to_owned() + } + other => capitalize(&other.to_string()), + } +} + +fn capitalize(text: &str) -> String { + let mut chars = text.chars(); + match chars.next() { + Some(first) => first.to_uppercase().chain(chars).collect::() + ".", + None => String::new(), + } +} + +/// The sentence for a failed attempt to open the account keys. +pub(crate) fn account_key_problem(error: &nocterm_hubs_model::AccountKeyError) -> String { + use nocterm_hubs_model::AccountKeyError as Error; + match error { + Error::WrongPassword => { + "That password does not open the account keys. Check it and try again.".to_owned() + } + Error::Offline => { + "The server cannot be reached, and this device keeps no copy of the keys to open offline." + .to_owned() + } + Error::NotSignedIn => { + "This device is not signed in to the server. Sign in first.".to_owned() + } + Error::Failed { .. } => "The account keys could not be opened.".to_owned(), + } +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_hubs_model::HubAddressError; + + #[test] + fn a_refused_address_reads_as_a_sentence() { + let error = SignInError::Address(HubAddressError::Empty); + assert_eq!(sign_in_problem(&error), "The hub address is empty."); + } + + #[test] + fn refused_credentials_name_the_user_name_and_password() { + assert_eq!( + hub_problem(&HubError::InvalidCredentials), + "The user name or password was not accepted." + ); + } +} diff --git a/crates/hubs-ui/src/ports.rs b/crates/hubs-ui/src/ports.rs new file mode 100644 index 00000000..52499671 --- /dev/null +++ b/crates/hubs-ui/src/ports.rs @@ -0,0 +1,39 @@ +//! What the views of this crate need from the application, named for the consumer. + +use gpui_kit::{App, Window}; +use nocterm_core::error::SharedCause; +use nocterm_hubs_model::{Environment, HubError, HubId}; + +/// Where an environment is opened. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Placement { + /// Instead of what the window shows now. + ThisWindow, + /// In a window of its own; the window asking is left alone. + NewWindow, +} + +/// Why an environment could not be opened. +#[derive(Debug, thiserror::Error)] +pub enum EnvironmentOpenError { + #[error("no such server: {0}")] + UnknownHub(HubId), + #[error("the account of the server cannot be opened")] + Account(#[source] HubError), + #[error("the window could not be opened")] + Window(#[source] SharedCause), +} + +/// Opens an environment in a window. The application implements it: only it can compose a +/// window. +pub trait EnvironmentOpener { + /// Opens `environment` in `placement`. [`Placement::ThisWindow`] replaces the window `window` + /// shows; the replacement may happen after this call returns. + fn open( + &self, + environment: Environment, + placement: Placement, + window: &mut Window, + cx: &mut App, + ) -> Result<(), EnvironmentOpenError>; +} diff --git a/crates/hubs-ui/src/problems.rs b/crates/hubs-ui/src/problems.rs new file mode 100644 index 00000000..0b735088 --- /dev/null +++ b/crates/hubs-ui/src/problems.rs @@ -0,0 +1,97 @@ +//! The dialogs for hubs that cannot be signed in to: a certificate that is not the trusted +//! one, and a protocol this Nocterm does not speak. Neither offers a way to accept what the +//! hub presents. + +use gpui_kit::{ + App, ClickEvent, Entity, Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + v_flex, + }, + prelude::*, + px, +}; +use nocterm_hubs::{HubSummary, Hubs}; +use nocterm_hubs_model::Fingerprint; + +use crate::layout::{fingerprint_block, note, problem}; + +/// Tells that the certificate of `hub` is not the trusted one, shows both fingerprints, and +/// offers to forget the server. Signing in again to the same server then asks anew. +pub(crate) fn open_trust_failed( + hubs: Entity, + hub: HubSummary, + pinned: Option, + presented: Fingerprint, + window: &mut Window, + cx: &mut App, +) { + window.open_alert_dialog(cx, move |alert, _, cx| { + let (summary, hubs) = (hub.clone(), hubs.clone()); + let remove = Button::new("trust-failed-remove") + .label("Remove this server") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + crate::manage::remove(&hubs, &summary.id, window, cx); + window.close_dialog(cx); + }); + let close = Button::new("trust-failed-close") + .primary() + .label("Close") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + }); + let (heading, reason) = match &pinned { + Some(_) => ( + "The certificate changed", + "The server presents another certificate than the one pinned when it was added. Nocterm will not connect until you have checked why.", + ), + None => ( + "The certificate is not trusted", + "The server's certificate is not issued by a public authority and none is pinned. Nocterm will not connect.", + ), + }; + let mut details = v_flex().gap_3().child(problem(reason.to_owned(), cx)); + if let Some(pinned) = &pinned { + details = details.child(fingerprint_block("Pinned", pinned, cx)); + } + details = details + .child(fingerprint_block("Presented now", &presented, cx)) + .child(note( + "Remove the server and sign in again only if you can confirm the new fingerprint with whoever runs it.", + cx, + )); + alert + .title(format!("{}: {heading}", hub.label)) + .width(px(480.)) + .description(details) + .on_ok(|_, _, _| true) + .footer(DialogFooter::new().child(remove).child(close)) + }); +} + +/// Tells that `hub` and this client speak different protocol versions. +pub(crate) fn open_upgrade_required( + hub: &HubSummary, + hub_version: u32, + client_version: u32, + window: &mut Window, + cx: &mut App, +) { + let label = hub.label.clone(); + window.open_alert_dialog(cx, move |alert, _, _| { + let newer = if hub_version > client_version { + "Update Nocterm to connect." + } else { + "The server needs an update before this Nocterm can connect." + }; + alert + .title(format!("{label}: update needed")) + .description(format!( + "The server speaks protocol {hub_version} and this Nocterm speaks {client_version}. {newer}" + )) + .ok_text("Close") + .show_cancel(false) + }); +} diff --git a/crates/hubs-ui/src/servers.rs b/crates/hubs-ui/src/servers.rs new file mode 100644 index 00000000..90dbb502 --- /dev/null +++ b/crates/hubs-ui/src/servers.rs @@ -0,0 +1,242 @@ +//! The Servers page of the settings: the known servers in a list, and the selected one in +//! tabs: its name, colour, status and certificate with the ways to sign out of it or remove it, +//! the devices of the account, its vault keys, its agents and providers, and the agent key +//! with the servers connected to the agents. +//! +//! The page edits through the service and never decides who may be trusted: a certificate is +//! shown, never accepted, and the only way out of a changed one is to remove the server. The +//! page owns only which server and which tab are selected; each tab owns its own state. + +mod detail; +mod list; +mod tabs; + +use std::time::Duration; + +use gpui_kit::{ + App, Context, ElementId, Entity, FocusHandle, Focusable, Subscription, Task, + TestSupportExt as _, Window, + component::{ActiveTheme as _, StyledExt as _, h_flex, v_flex}, + div, + prelude::*, +}; +use nocterm_hubs::HubSummary; +use nocterm_hubs::{HubStatus, Hubs}; +use nocterm_hubs_model::HubId; +use nocterm_ui::{IconName, SettingsPage, SettingsPageSpec}; + +use crate::installation::Installed; +use crate::layout::note; +use detail::{Access, Tab, TabViews, detail}; +use list::server_list; +use tabs::{ + account_keys::AccountKeys, agent_access::AgentAccessTab, agents::AgentsTab, devices::Devices, + names::NameFields, +}; + +/// The Servers page of the settings, listed with the other pages by the application. +pub fn settings_page(installed: &Installed) -> SettingsPageSpec { + let hubs = installed.hubs().clone(); + let agents = installed.hub_agents().cloned(); + SettingsPageSpec::new("servers", "Servers", move |_, cx| { + let (hubs, agents) = (hubs.clone(), agents.clone()); + cx.new(|cx| ServersPage::new(hubs, agents, cx)) + }) + .with_icon(IconName::Server) +} + +/// How often the countdown of an offline server is redrawn. +const TICK: Duration = Duration::from_secs(1); + +/// The id of the element of kind `kind` of the server at `address`. +pub(crate) fn item(kind: &str, address: &str) -> ElementId { + ElementId::Name(format!("{kind}-{address}").into()) +} + +/// The settings page that lists the known servers and shows one of them. +pub(crate) struct ServersPage { + hubs: Entity, + /// The server the person chose; the first one while it is none or no longer listed. + selected: Option, + tab: Tab, + names: NameFields, + devices: Entity, + keys: Entity, + agents: Option>, + access: Option>, + focus: FocusHandle, + _redraw: Task<()>, + _hubs: Subscription, +} + +impl ServersPage { + pub(crate) fn new( + hubs: Entity, + agents: Option>, + cx: &mut Context, + ) -> Self { + let redraw = cx.spawn(async move |this, cx| { + loop { + cx.background_executor().timer(TICK).await; + let alive = this.update(cx, |page, cx| { + if page.counts_down(cx) { + cx.notify(); + } + }); + if alive.is_err() { + break; + } + } + }); + Self { + selected: None, + tab: Tab::Overview, + names: NameFields::new(hubs.clone()), + devices: cx.new(|_| Devices::new(hubs.clone())), + keys: cx.new(|_| AccountKeys::new(hubs.clone())), + access: agents + .clone() + .map(|service| cx.new(|cx| AgentAccessTab::new(service, cx))), + agents: agents.map(|service| cx.new(|cx| AgentsTab::new(service, cx))), + focus: cx.focus_handle(), + _redraw: redraw, + _hubs: cx.observe(&hubs, |_, _, cx| cx.notify()), + hubs, + } + } + + /// Whether a server shows a countdown that must be redrawn. + fn counts_down(&self, cx: &App) -> bool { + self.hubs + .read(cx) + .list() + .iter() + .any(|summary| matches!(summary.status, HubStatus::Offline { .. })) + } + + /// Shows `hub`, from its first tab. + fn select_hub(&mut self, hub: HubId, cx: &mut Context) { + if self.selected.as_ref() != Some(&hub) { + self.tab = Tab::Overview; + } + self.selected = Some(hub); + cx.notify(); + } + + fn select_tab(&mut self, tab: Tab, cx: &mut Context) { + self.tab = tab; + cx.notify(); + } +} + +impl SettingsPage for ServersPage { + fn flush(&mut self, window: &mut Window, cx: &mut Context) { + self.names.flush(window, cx); + } +} + +impl Focusable for ServersPage { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +/// The server whose `wanted` tab is open, while this device can reach its account. +fn on_show(shown: Option<&HubSummary>, current: Tab, wanted: Tab) -> Option { + shown + .filter(|summary| current == wanted && Access::of(&summary.status) == Access::SignedIn) + .map(|summary| summary.id.clone()) +} + +impl Render for ServersPage { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + self.names.sync(window, cx); + let summaries = self.hubs.read(cx).list(); + let shown = self + .selected + .as_ref() + .and_then(|hub| summaries.iter().find(|summary| &summary.id == hub)) + .or(summaries.first()); + // The devices are read only while their tab is on show, and again each time it is. + let devices_on_show = on_show(shown, self.tab, Tab::Devices); + self.devices + .update(cx, |devices, cx| match &devices_on_show { + Some(hub) => devices.show(hub, cx), + None => devices.hide(), + }); + if let Some(hub) = on_show(shown, self.tab, Tab::Keys) { + self.keys.update(cx, |keys, cx| keys.show(&hub, cx)); + } + // The agents and the agent servers are read again each time their tab is on show. + let agents_on_show = on_show(shown, self.tab, Tab::Agents); + if let Some(tab) = &self.agents { + tab.update(cx, |tab, cx| match &agents_on_show { + Some(hub) => tab.show(hub, cx), + None => tab.hide(), + }); + } + let access_on_show = on_show(shown, self.tab, Tab::AgentAccess); + if let Some(tab) = &self.access { + tab.update(cx, |tab, cx| match &access_on_show { + Some(hub) => tab.show(hub, cx), + None => tab.hide(), + }); + } + let body = shown.and_then(|summary| { + let name = self.names.get(&summary.id)?; + Some(detail( + summary, + &name, + self.tab, + TabViews { + devices: &self.devices, + keys: &self.keys, + agents: self.agents.as_ref(), + access: self.access.as_ref(), + }, + cx.weak_entity(), + &self.hubs, + cx, + )) + }); + let list = server_list(&summaries, shown.map(|summary| &summary.id), cx); + v_flex() + .id("servers-page") + .test_support() + .track_focus(&self.focus) + .size_full() + .p_4() + .gap_4() + .child( + v_flex() + .gap_1() + .child(div().text_lg().font_semibold().child("Servers")) + .child(note( + "Nocterm hubs this device is signed in to. Credentials stay in a private file on this device.", + cx, + )), + ) + .child( + h_flex() + .gap_4() + .items_start() + .child(list) + .children(body) + .when(summaries.is_empty(), |page| { + page.child(div().id("servers-empty").test_support().child(note( + "No servers yet. Sign in to one to use your hub from this computer.", + cx, + ))) + }), + ) + .text_color(cx.theme().foreground) + } +} + +#[cfg(test)] +#[path = "servers_tests.rs"] +mod tests; + +#[cfg(test)] +#[path = "servers_agents_tests.rs"] +mod agent_tests; diff --git a/crates/hubs-ui/src/servers/detail.rs b/crates/hubs-ui/src/servers/detail.rs new file mode 100644 index 00000000..d11818f1 --- /dev/null +++ b/crates/hubs-ui/src/servers/detail.rs @@ -0,0 +1,198 @@ +//! The selected server on the right of the Servers page: its name and the tabs. + +use gpui_kit::{ + App, ClickEvent, Entity, TestSupportExt as _, WeakEntity, Window, + component::{ + ActiveTheme as _, Icon, StyledExt as _, + button::{Button, ButtonVariants as _}, + h_flex, + tab::{Tab as TabButton, TabBar}, + v_flex, + }, + div, + prelude::*, +}; +use nocterm_hubs::{HubStatus, HubSummary, Hubs}; +use nocterm_ui::SettingsTextField; + +use super::tabs::{ + account_keys::AccountKeys, agent_access::AgentAccessTab, agents::AgentsTab, devices::Devices, + overview::overview, trust::trust, +}; +use super::{ServersPage, item}; +use crate::color::tint; +use crate::layout::note; +use crate::status::status_icon; + +/// The tabs of a server. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Tab { + Overview, + Devices, + Keys, + Agents, + AgentAccess, +} + +impl Tab { + const ALL: [Self; 5] = [ + Self::Overview, + Self::Devices, + Self::Keys, + Self::Agents, + Self::AgentAccess, + ]; + + fn title(self) -> &'static str { + match self { + Self::Overview => "Overview", + Self::Devices => "Devices", + Self::Keys => "Keys", + Self::Agents => "Agents", + Self::AgentAccess => "Agent access", + } + } +} + +/// The views of the tabs that keep their own state. +pub(super) struct TabViews<'a> { + pub(super) devices: &'a Entity, + pub(super) keys: &'a Entity, + pub(super) agents: Option<&'a Entity>, + pub(super) access: Option<&'a Entity>, +} + +/// Whether the account of a server can be reached from this device. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(super) enum Access { + /// This device holds credentials. + SignedIn, + /// Signing in comes first. + SignedOut, + /// The certificate or the protocol stands in the way; the Overview says which. + Blocked, +} + +impl Access { + pub(super) fn of(status: &HubStatus) -> Self { + match status { + HubStatus::SignedOut | HubStatus::Revoked => Self::SignedOut, + HubStatus::TrustFailed { .. } | HubStatus::UpgradeRequired { .. } => Self::Blocked, + HubStatus::Online | HubStatus::Connecting | HubStatus::Offline { .. } => Self::SignedIn, + } + } +} + +/// The server's name and the tab `tab` of it. +pub(super) fn detail( + summary: &HubSummary, + name: &SettingsTextField, + tab: Tab, + views: TabViews<'_>, + page: WeakEntity, + hubs: &Entity, + cx: &App, +) -> gpui_kit::AnyElement { + let key = summary.address.to_string(); + let theme = cx.theme(); + let mark = summary + .color + .map_or(theme.muted_foreground, |color| tint(color, cx)); + let selected = Tab::ALL.iter().position(|each| *each == tab).unwrap_or(0); + let tabs = TabBar::new("servers-tabs") + .underline() + .selected_index(selected) + .children(Tab::ALL.map(|tab| TabButton::new().label(tab.title()))) + .on_click(move |index, _, cx| { + let tab = Tab::ALL[*index]; + let _ = page.update(cx, |page, cx| page.select_tab(tab, cx)); + }); + let Some(record) = hubs.read(cx).record(&summary.id).cloned() else { + return div().into_any_element(); + }; + let body = match (tab, Access::of(&summary.status)) { + (Tab::Overview, _) => overview(summary, &record, name, hubs, cx), + (_, Access::SignedOut) => signed_out(summary, hubs, &key, cx), + (_, Access::Blocked) => blocked(&key, cx), + (Tab::Devices, Access::SignedIn) => views.devices.clone().into_any_element(), + (Tab::Keys, Access::SignedIn) => trust(summary, hubs, views.keys, cx), + (Tab::Agents, Access::SignedIn) => match views.agents { + Some(tab) => tab.clone().into_any_element(), + None => without_agents(cx), + }, + (Tab::AgentAccess, Access::SignedIn) => match views.access { + Some(tab) => tab.clone().into_any_element(), + None => without_agents(cx), + }, + }; + v_flex() + .id(item("server", &key)) + .test_support() + .flex_1() + .min_w_0() + .gap_3() + .child( + h_flex() + .gap_2() + .items_center() + .child(Icon::new(status_icon(&summary.status)).text_color(mark)) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .font_semibold() + .child(summary.label.clone()), + ), + ) + .child(tabs) + .child(body) + .into_any_element() +} + +/// The empty state of a tab that needs the account, with the way to sign in. +fn signed_out( + summary: &HubSummary, + hubs: &Entity, + key: &str, + cx: &App, +) -> gpui_kit::AnyElement { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + v_flex() + .id(item("server-tab-signed-out", key)) + .test_support() + .gap_2() + .items_start() + .child(note( + "This device is not signed in to this server. Sign in to see this.", + cx, + )) + .child( + Button::new(item("server-tab-sign-in", key)) + .primary() + .label("Sign in") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + crate::sign_in::start(&hubs, Some(&id), None, window, cx); + }), + ) + .into_any_element() +} + +/// The agent tabs of an application composed without the agents service. +fn without_agents(cx: &App) -> gpui_kit::AnyElement { + div() + .child(note("This build has no hub agents.", cx)) + .into_any_element() +} + +/// The empty state of a tab while the server cannot be trusted or spoken to. +fn blocked(key: &str, cx: &App) -> gpui_kit::AnyElement { + div() + .id(item("server-tab-blocked", key)) + .test_support() + .child(note( + "This is not available while the server cannot be reached safely. The Overview tab says why.", + cx, + )) + .into_any_element() +} diff --git a/crates/hubs-ui/src/servers/list.rs b/crates/hubs-ui/src/servers/list.rs new file mode 100644 index 00000000..0d99c832 --- /dev/null +++ b/crates/hubs-ui/src/servers/list.rs @@ -0,0 +1,95 @@ +//! The compact list of known servers on the left of the Servers page. + +use gpui_kit::{ + App, ClickEvent, Context, TestSupportExt as _, WeakEntity, Window, + component::{ + ActiveTheme as _, Icon, + button::{Button, ButtonVariants as _}, + h_flex, v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::HubSummary; +use nocterm_hubs_model::HubId; + +use super::{ServersPage, item}; +use crate::SignInToServer; +use crate::color::tint; +use crate::status::{status_chip, status_icon}; + +/// The servers, the one in `selected` marked, and the button that signs in to another. +pub(super) fn server_list( + summaries: &[HubSummary], + selected: Option<&HubId>, + cx: &mut Context, +) -> gpui_kit::AnyElement { + let page = cx.weak_entity(); + let rows: Vec<_> = summaries + .iter() + .map(|summary| entry(summary, selected == Some(&summary.id), page.clone(), cx)) + .collect(); + v_flex() + .id("servers-list") + .test_support() + .w(px(240.)) + .flex_none() + .gap_2() + .child(v_flex().gap_1().children(rows)) + .child( + Button::new("servers-add") + .primary() + .label("Sign in to a server…") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.dispatch_action(Box::new(SignInToServer), cx); + }), + ) + .into_any_element() +} + +/// One server: its colour mark, its name and a dot in the colour of its status. +fn entry( + summary: &HubSummary, + selected: bool, + page: WeakEntity, + cx: &App, +) -> impl IntoElement { + let key = summary.address.to_string(); + let theme = cx.theme(); + let (_, status_color) = status_chip(&summary.status, cx); + let mark = summary + .color + .map_or(theme.muted_foreground, |color| tint(color, cx)); + let id = summary.id.clone(); + h_flex() + .id(item("server-select", &key)) + .test_support() + .gap_2() + .px_2() + .py_1p5() + .items_center() + .rounded(theme.radius) + .cursor_pointer() + .when(selected, |entry| entry.bg(theme.accent)) + .child(Icon::new(status_icon(&summary.status)).text_color(mark)) + .child( + div() + .flex_1() + .min_w_0() + .truncate() + .child(summary.label.clone()), + ) + .child( + div() + .id(item("server-dot", &key)) + .test_support() + .size_2() + .flex_none() + .rounded_full() + .bg(status_color), + ) + .on_click(move |_, _, cx| { + let _ = page.update(cx, |page, cx| page.select_hub(id.clone(), cx)); + }) +} diff --git a/crates/hubs-ui/src/servers/tabs.rs b/crates/hubs-ui/src/servers/tabs.rs new file mode 100644 index 00000000..7cdd37d5 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs.rs @@ -0,0 +1,9 @@ +//! The tabs of one server on the Servers page, each an owner of its own state. + +pub(super) mod account_keys; +pub(super) mod agent_access; +pub(super) mod agents; +pub(super) mod devices; +pub(super) mod names; +pub(super) mod overview; +pub(super) mod trust; diff --git a/crates/hubs-ui/src/servers/tabs/account_keys.rs b/crates/hubs-ui/src/servers/tabs/account_keys.rs new file mode 100644 index 00000000..b3d6b2cf --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/account_keys.rs @@ -0,0 +1,165 @@ +//! The state of the account keys on this device, with the password prompt that opens them and +//! the button that closes them. +//! +//! The keys open the account's vault (saved servers and their credentials); chats and agents +//! never need them. They follow their own lock policy (`[hubs] key_lock_minutes`), not the +//! local vault's. This view never decides what the password opens: it hands the typed secret +//! to the service and reports the outcome. + +use gpui_kit::{ + ClickEvent, Context, Entity, Focusable as _, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Disableable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, + input::{Input, InputState}, + v_flex, + }, + div, + prelude::*, +}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::HubId; +use nocterm_session::Secret; + +use crate::layout::{field, note, problem}; +use crate::messages::account_key_problem; + +/// The lock of the account keys of the server that is shown. +pub(in crate::servers) struct AccountKeys { + hubs: Entity, + hub: Option, + /// Created with the first frame, because a field needs a window. + password: Option>, + /// Why the last attempt to unlock failed, as a sentence. + refusal: Option, + unlocking: Option>, +} + +impl AccountKeys { + pub(in crate::servers) fn new(hubs: Entity) -> Self { + Self { + hubs, + hub: None, + password: None, + refusal: None, + unlocking: None, + } + } + + /// Shows the keys of `hub`; another server forgets the last refusal. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() != Some(hub) { + self.hub = Some(hub.clone()); + self.refusal = None; + self.unlocking = None; + cx.notify(); + } + } + + fn lock(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + self.hubs.read(cx).lock_account_keys(&hub); + cx.notify(); + } + + fn unlock(&mut self, window: &mut Window, cx: &mut Context) { + let (Some(hub), Some(input)) = (self.hub.clone(), self.password.clone()) else { + return; + }; + let typed = input.read(cx).value().to_string(); + input.update(cx, |input, cx| input.set_value("", window, cx)); + let unlocking = self + .hubs + .read(cx) + .unlock_account_keys(&hub, Secret::new(typed)); + self.refusal = None; + self.unlocking = Some(cx.spawn(async move |this, cx| { + let result = unlocking.await; + this.update(cx, |keys, cx| { + keys.refusal = result.err().map(|error| account_key_problem(&error)); + cx.notify(); + }) + .ok(); + })); + cx.notify(); + } + + fn prompt(&mut self, window: &mut Window, cx: &mut Context) -> gpui_kit::AnyElement { + if self.password.is_none() { + let input = cx.new(|cx| { + InputState::new(window, cx) + .masked(true) + .placeholder("Password") + }); + // The prompt appears because the user needs it: type at once. + window.focus(&input.focus_handle(cx), cx); + self.password = Some(input); + } + let input = self.password.clone().expect("created above"); + let busy = self.unlocking.is_some(); + v_flex() + .id("account-keys-locked") + .test_support() + .gap_2() + .child(note( + "The account keys are closed on this device. Type your password (or the vault passphrase of an account that signs in through a provider) to open them.", + cx, + )) + .child(field("Password", Input::new(&input))) + .children( + self.refusal + .clone() + .map(|text| div().id("account-keys-refused").test_support().child(problem(text, cx))), + ) + .child( + h_flex().child( + Button::new("account-keys-unlock") + .primary() + .label("Unlock") + .disabled(busy) + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.unlock(window, cx); + })), + ), + ) + .into_any_element() + } + + fn open(&self, cx: &mut Context) -> gpui_kit::AnyElement { + let theme = cx.theme(); + h_flex() + .id("account-keys-open") + .test_support() + .gap_3() + .items_center() + .child( + div() + .flex_1() + .text_xs() + .text_color(theme.muted_foreground) + .child("Account keys are open on this device."), + ) + .child( + Button::new("account-keys-lock") + .small() + .label("Lock account keys") + .on_click(cx.listener(|this, _: &ClickEvent, _: &mut Window, cx| { + this.lock(cx); + })), + ) + .into_any_element() + } +} + +impl Render for AccountKeys { + fn render(&mut self, window: &mut Window, cx: &mut Context) -> impl IntoElement { + match self.hub.clone() { + None => div().into_any_element(), + Some(hub) if self.hubs.read(cx).account_keys_open(&hub) => self.open(cx), + Some(_) => self.prompt(window, cx), + } + } +} diff --git a/crates/hubs-ui/src/servers/tabs/agent_access.rs b/crates/hubs-ui/src/servers/tabs/agent_access.rs new file mode 100644 index 00000000..9ed7d57d --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agent_access.rs @@ -0,0 +1,287 @@ +//! The Agent access tab of a server: the account's agent key, and the servers connected to the +//! agents with what agents last found there. +//! +//! A server is connected from the connection editor of a saved server ("Agent access"), which +//! writes the key's line to its `authorized_keys`. Here the person checks a server again, +//! disconnects it (agents stop using it at once; the line stays until it is removed on the +//! server) and replaces the key (every server then needs its line written again). + +use gpui_kit::{ + ClickEvent, ClipboardItem, Context, Entity, Subscription, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Sizable as _, StyledExt as _, WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + h_flex, v_flex, + }, + div, + prelude::*, +}; +use nocterm_hub_agents::HubAgents; +use nocterm_hub_agents_model::{AgentKey, HubServer, ServerId}; +use nocterm_hubs_model::HubId; + +use crate::layout::{field, note, problem}; +use crate::messages::agents_problem; +use crate::servers::item; + +/// The agent key and servers of the server that is shown. +pub(in crate::servers) struct AgentAccessTab { + service: Entity, + hub: Option, + key: Option, + problem: Option, + working: Option>, + _service: Subscription, +} + +impl AgentAccessTab { + pub(in crate::servers) fn new(service: Entity, cx: &mut Context) -> Self { + Self { + _service: cx.observe(&service, |_, _, cx| cx.notify()), + service, + hub: None, + key: None, + problem: None, + working: None, + } + } + + /// Shows the agent key and servers of `hub`, read again when another server was shown. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() == Some(hub) { + return; + } + self.hub = Some(hub.clone()); + self.key = None; + self.problem = None; + self.service.update(cx, |service, cx| { + service.track(hub, cx); + service.refresh(hub, cx); + }); + let reading = self.service.read(cx).server_port().agent_key(hub); + self.key_from(reading, cx); + } + + pub(in crate::servers) fn hide(&mut self) { + self.hub = None; + self.working = None; + } + + fn key_from( + &mut self, + reading: nocterm_hub_agents_model::AgentsFuture, + cx: &mut Context, + ) { + self.working = Some(cx.spawn(async move |this, cx| { + let result = reading.await; + this.update(cx, |tab, cx| { + match result { + Ok(key) => tab.key = Some(key), + Err(error) => tab.problem = Some(agents_problem(&error)), + } + if let Some(hub) = tab.hub.clone() { + tab.service + .update(cx, |service, cx| service.refresh(&hub, cx)); + } + cx.notify(); + }) + .ok(); + })); + } + + fn rotate(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let rotating = self.service.read(cx).server_port().rotate_agent_key(&hub); + self.key_from(rotating, cx); + } + + /// Runs a call on one server, then lists the servers again. + fn on_server( + &mut self, + call: nocterm_hub_agents_model::AgentsFuture, + cx: &mut Context, + ) { + self.problem = None; + self.working = Some(cx.spawn(async move |this, cx| { + let result = call.await; + this.update(cx, |tab, cx| { + if let Err(error) = result { + tab.problem = Some(agents_problem(&error)); + } + if let Some(hub) = tab.hub.clone() { + tab.service + .update(cx, |service, cx| service.refresh(&hub, cx)); + } + cx.notify(); + }) + .ok(); + })); + cx.notify(); + } + + fn check(&mut self, server: &ServerId, cx: &mut Context) { + if let Some(hub) = self.hub.clone() { + let checking = self + .service + .read(cx) + .server_port() + .check_server(&hub, server); + self.on_server(checking, cx); + } + } + + fn disconnect(&mut self, server: &ServerId, cx: &mut Context) { + if let Some(hub) = self.hub.clone() { + let removing = self + .service + .read(cx) + .server_port() + .remove_server(&hub, server); + self.on_server(removing, cx); + } + } + + /// Asks before replacing the key: every server needs its line written again. + fn confirm_rotate(view: Entity, window: &mut Window, cx: &mut gpui_kit::App) { + window.open_alert_dialog(cx, move |alert, _, _| { + let view = view.clone(); + let confirm = Button::new("agent-key-rotate-confirm") + .danger() + .label("Replace the key") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut gpui_kit::App| { + window.close_dialog(cx); + view.update(cx, |tab, cx| tab.rotate(cx)); + }); + let keep = Button::new("agent-key-rotate-keep") + .primary() + .label("Keep it") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut gpui_kit::App| { + window.close_dialog(cx); + }); + alert + .title("Replace the agent key?") + .description( + "Agents stop signing in to every connected server until its line is written again: turn Agent access off and on in each server's editor.", + ) + .footer(DialogFooter::new().child(keep).child(confirm)) + }); + } + + fn render_key(&self, key: &str, cx: &mut Context) -> gpui_kit::AnyElement { + let Some(agent_key) = &self.key else { + return note("Reading the agent key…", cx).into_any_element(); + }; + let line = agent_key.authorized_line.clone(); + field( + "Agent key", + v_flex() + .id(item("agent-key", key)) + .test_support() + .gap_1() + .child( + div() + .font_family(cx.theme().mono_font_family.clone()) + .text_sm() + .child(agent_key.fingerprint.clone()), + ) + .child(note( + "Agents sign in to your servers with this key. Its line goes into authorized_keys with port, agent and X11 forwarding off.", + cx, + )) + .child( + h_flex() + .gap_2() + .child( + Button::new(item("agent-key-copy", key)) + .small() + .label("Copy line") + .on_click(move |_: &ClickEvent, _, cx: &mut gpui_kit::App| { + cx.write_to_clipboard(ClipboardItem::new_string(line.clone())); + }), + ) + .child( + Button::new(item("agent-key-rotate", key)) + .small() + .ghost() + .label("Replace key") + .on_click(cx.listener(|_, _: &ClickEvent, window, cx| { + Self::confirm_rotate(cx.entity(), window, cx); + })), + ), + ), + ) + .into_any_element() + } + + fn render_server(server: &HubServer, cx: &mut Context) -> gpui_kit::AnyElement { + let (check, remove) = (server.id.clone(), server.id.clone()); + let status = if server.status_message.is_empty() { + server.status.label().to_owned() + } else { + format!("{} — {}", server.status.label(), server.status_message) + }; + h_flex() + .id(item("agent-server", server.id.as_str())) + .test_support() + .gap_2() + .child( + v_flex() + .flex_1() + .min_w_0() + .child(div().text_sm().font_semibold().child(server.name.clone())) + .child(note( + format!("{}@{}:{} · {status}", server.user, server.host, server.port), + cx, + )), + ) + .child( + Button::new(item("agent-server-check", server.id.as_str())) + .small() + .ghost() + .label("Check") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| tab.check(&check, cx))), + ) + .child( + Button::new(item("agent-server-disconnect", server.id.as_str())) + .small() + .ghost() + .label("Disconnect") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + tab.disconnect(&remove, cx); + })), + ) + .into_any_element() + } +} + +impl Render for AgentAccessTab { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + let Some(hub) = self.hub.clone() else { + return div().into_any_element(); + }; + let key = hub.to_string(); + let servers = self.service.read(cx).servers(&hub).to_vec(); + let rows: Vec<_> = servers + .iter() + .map(|server| Self::render_server(server, cx)) + .collect(); + v_flex() + .id(item("agent-access-tab", &key)) + .test_support() + .gap_3() + .children(self.problem.clone().map(|text| problem(text, cx))) + .child(self.render_key(&key, cx)) + .child(div().font_semibold().child("Servers agents may use")) + .when(rows.is_empty(), |tab| { + tab.child(note( + "None yet. Open a saved server in this window's connection editor and turn on Agent access.", + cx, + )) + }) + .children(rows) + .into_any_element() + } +} diff --git a/crates/hubs-ui/src/servers/tabs/agents.rs b/crates/hubs-ui/src/servers/tabs/agents.rs new file mode 100644 index 00000000..43431b42 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agents.rs @@ -0,0 +1,258 @@ +//! The Agents tab of a server: the account's agents, created, edited and deleted in one form +//! (name, instructions, provider and models, default servers, approval mode), and the +//! providers the agents use, with the account's own ones added, changed, tested and removed. +//! +//! Every write goes to the hub through the agents port; the tab then lists the hub again. A +//! provider key is typed once and never shown again: the hub keeps it. + +mod form; +mod providers; + +use gpui_kit::{ + ClickEvent, Context, Entity, Subscription, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Sizable as _, StyledExt as _, + button::{Button, ButtonVariants as _}, + h_flex, v_flex, + }, + div, + prelude::*, +}; +use nocterm_hub_agents::HubAgents; +use nocterm_hub_agents_model::{AgentId, HubAgent, HubAgentsError, Provider}; +use nocterm_hubs_model::HubId; + +use crate::layout::{note, problem}; +use crate::messages::agents_problem; +use crate::servers::item; +use form::AgentForm; +use providers::ProviderForm; + +/// The agents and providers of the server that is shown, and the form open on one of them. +pub(in crate::servers) struct AgentsTab { + service: Entity, + hub: Option, + providers: Vec, + agent_form: Option, + provider_form: Option, + problem: Option, + working: Option>, + _service: Subscription, +} + +impl AgentsTab { + pub(in crate::servers) fn new(service: Entity, cx: &mut Context) -> Self { + Self { + _service: cx.observe(&service, |_, _, cx| cx.notify()), + service, + hub: None, + providers: Vec::new(), + agent_form: None, + provider_form: None, + problem: None, + working: None, + } + } + + /// Shows the agents of `hub`; another server closes the forms. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() == Some(hub) { + return; + } + self.hub = Some(hub.clone()); + self.agent_form = None; + self.provider_form = None; + self.problem = None; + self.service.update(cx, |service, cx| { + service.track(hub, cx); + service.refresh(hub, cx); + }); + self.reload_providers(cx); + } + + pub(in crate::servers) fn hide(&mut self) { + self.hub = None; + self.working = None; + self.agent_form = None; + self.provider_form = None; + } + + fn reload_providers(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let listing = self.service.read(cx).directory().providers(&hub); + self.working = Some(cx.spawn(async move |this, cx| { + let result = listing.await; + this.update(cx, |tab, cx| { + match result { + Ok(providers) => tab.providers = providers, + Err(error) => tab.problem = Some(agents_problem(&error)), + } + cx.notify(); + }) + .ok(); + })); + } + + /// Runs a write against the hub, then lists the agents and providers again. + fn write(&mut self, call: nocterm_hub_agents_model::AgentsFuture<()>, cx: &mut Context) { + self.problem = None; + self.working = Some(cx.spawn(async move |this, cx| { + let result = call.await; + this.update(cx, |tab, cx| tab.written(result, cx)).ok(); + })); + cx.notify(); + } + + fn written(&mut self, result: Result<(), HubAgentsError>, cx: &mut Context) { + match result { + Ok(()) => { + self.agent_form = None; + self.provider_form = None; + } + Err(error) => self.problem = Some(agents_problem(&error)), + } + if let Some(hub) = self.hub.clone() { + self.service + .update(cx, |service, cx| service.refresh(&hub, cx)); + } + self.reload_providers(cx); + cx.notify(); + } + + fn save_agent(&mut self, cx: &mut Context) { + let (Some(hub), Some(form)) = (self.hub.clone(), &self.agent_form) else { + return; + }; + let draft = form.draft(cx); + let directory = self.service.read(cx).directory().clone(); + let call: nocterm_hub_agents_model::AgentsFuture<()> = match form.editing().cloned() { + Some(agent) => { + let saving = directory.update_agent(&hub, &agent, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + None => { + let saving = directory.create_agent(&hub, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + }; + self.write(call, cx); + } + + fn delete_agent(&mut self, agent: &AgentId, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let deleting = self.service.read(cx).directory().delete_agent(&hub, agent); + self.write(deleting, cx); + } + + fn open_agent( + &mut self, + agent: Option<&HubAgent>, + window: &mut Window, + cx: &mut Context, + ) { + self.agent_form = Some(AgentForm::new(agent, window, cx)); + self.provider_form = None; + cx.notify(); + } + + fn render_agents(&self, hub: &HubId, cx: &mut Context) -> gpui_kit::AnyElement { + let key = hub.to_string(); + let agents = self.service.read(cx).agents(hub).to_vec(); + let mut list = v_flex() + .id(item("server-agents", &key)) + .test_support() + .gap_1(); + if agents.is_empty() { + list = list.child(note( + "No agents yet. An agent is a name, instructions and a model; it costs nothing while no chat runs it.", + cx, + )); + } + for agent in agents { + let (edit, remove) = (agent.clone(), agent.id.clone()); + list = list.child( + h_flex() + .id(item("server-agent", agent.id.as_str())) + .test_support() + .gap_2() + .child( + v_flex() + .flex_1() + .min_w_0() + .child(div().text_sm().font_semibold().child(agent.name.clone())) + .child(note(format!("{} · {}", agent.model, approval(&agent)), cx)), + ) + .child( + Button::new(item("server-agent-edit", agent.id.as_str())) + .small() + .ghost() + .label("Edit") + .on_click(cx.listener(move |tab, _: &ClickEvent, window, cx| { + tab.open_agent(Some(&edit), window, cx); + })), + ) + .child( + Button::new(item("server-agent-delete", agent.id.as_str())) + .small() + .ghost() + .label("Delete") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + tab.delete_agent(&remove, cx); + })), + ), + ); + } + list.into_any_element() + } +} + +fn approval(agent: &HubAgent) -> &'static str { + match agent.approval { + nocterm_hub_agents_model::ApprovalMode::Auto => "Auto", + nocterm_hub_agents_model::ApprovalMode::Ask => "Ask before changes", + } +} + +impl Render for AgentsTab { + fn render(&mut self, _window: &mut Window, cx: &mut Context) -> impl IntoElement { + let Some(hub) = self.hub.clone() else { + return div().into_any_element(); + }; + let key = hub.to_string(); + let servers = self.service.read(cx).servers(&hub).to_vec(); + let agents = self.render_agents(&hub, cx); + let agent_form = self + .agent_form + .as_ref() + .map(|form| form.render(&self.providers, &servers, cx)); + let providers = providers::render(&self.providers, self.provider_form.as_ref(), cx); + v_flex() + .id(item("server-agents-tab", &key)) + .test_support() + .gap_3() + .children(self.problem.clone().map(|text| problem(text, cx))) + .child( + h_flex() + .justify_between() + .child(div().font_semibold().child("Agents")) + .child( + Button::new(item("server-agent-new", &key)) + .small() + .primary() + .label("New agent") + .on_click(cx.listener(|tab, _: &ClickEvent, window, cx| { + tab.open_agent(None, window, cx); + })), + ), + ) + .child(agents) + .children(agent_form) + .child(div().h_px().bg(cx.theme().border)) + .child(providers) + .into_any_element() + } +} diff --git a/crates/hubs-ui/src/servers/tabs/agents/form.rs b/crates/hubs-ui/src/servers/tabs/agents/form.rs new file mode 100644 index 00000000..b5f90307 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agents/form.rs @@ -0,0 +1,237 @@ +//! The form of one agent: name, instructions, provider and models, default servers and whether +//! changing tools wait for an approval. + +use std::collections::BTreeSet; + +use gpui_kit::{ + AppContext as _, ClickEvent, Context, Entity, TestSupportExt as _, Window, + component::{ + Selectable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + checkbox::Checkbox, + h_flex, + input::{Input, InputState, Textarea, TextareaState}, + v_flex, + }, + prelude::*, +}; +use nocterm_hub_agents_model::{ + AgentDraft, AgentId, ApprovalMode, HubAgent, HubServer, Provider, ProviderId, ServerId, +}; + +use super::AgentsTab; +use crate::layout::{field, note}; +use crate::servers::item; + +/// What the person has typed and chosen for an agent. +pub(super) struct AgentForm { + editing: Option, + name: Entity, + instructions: Entity, + models: Entity, + model: Entity, + provider: Option, + approval: ApprovalMode, + servers: BTreeSet, +} + +fn input( + placeholder: &'static str, + text: &str, + window: &mut Window, + cx: &mut Context, +) -> Entity { + let text = text.to_owned(); + cx.new(|cx| { + let mut input = InputState::new(window, cx).placeholder(placeholder); + input.set_value(text, window, cx); + input + }) +} + +impl AgentForm { + /// A form for a new agent, or one that edits `agent`. + pub(super) fn new( + agent: Option<&HubAgent>, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let draft = agent.map(HubAgent::draft).unwrap_or_default(); + let instructions = draft.instructions.clone(); + Self { + editing: agent.map(|agent| agent.id.clone()), + name: input("Name", &draft.name, window, cx), + instructions: cx.new(|cx| { + let mut state = + TextareaState::new(window, cx).placeholder("How the agent should behave"); + state.set_value(instructions, window, cx); + state + }), + models: input( + "Models, separated by commas", + &draft.models.join(", "), + window, + cx, + ), + model: input("Default model", &draft.model, window, cx), + provider: draft.provider, + approval: draft.approval, + servers: draft.servers.into_iter().collect(), + } + } + + pub(super) fn editing(&self) -> Option<&AgentId> { + self.editing.as_ref() + } + + /// The form as a draft; its rules are checked where it is saved. + pub(super) fn draft(&self, cx: &gpui_kit::App) -> AgentDraft { + let text = |input: &Entity| input.read(cx).value().to_string(); + AgentDraft { + name: text(&self.name), + instructions: self.instructions.read(cx).value().to_string(), + provider: self.provider.clone(), + model: text(&self.model), + models: text(&self.models) + .split(',') + .map(|model| model.trim().to_owned()) + .collect(), + approval: self.approval, + servers: self.servers.iter().cloned().collect(), + } + } + + fn choose_provider( + &mut self, + provider: &Provider, + window: &mut Window, + cx: &mut gpui_kit::App, + ) { + self.provider = Some(provider.id.clone()); + // The provider's models are what the agent may use unless the person names others. + if self.models.read(cx).value().trim().is_empty() { + let models = provider.models.join(", "); + self.models + .update(cx, |input, cx| input.set_value(models, window, cx)); + } + } + + pub(super) fn render( + &self, + providers: &[Provider], + servers: &[HubServer], + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let defaults = if servers.is_empty() { + note( + "No servers are connected to agents yet: turn on Agent access of a saved server.", + cx, + ) + .into_any_element() + } else { + self.server_choices(servers, cx).into_any_element() + }; + v_flex() + .id("server-agent-form") + .test_support() + .gap_2() + .child(field("Name", Input::new(&self.name))) + .child(field("Instructions", Textarea::new(&self.instructions))) + .child(field("Provider", self.provider_choices(providers, cx))) + .child(field("Models", Input::new(&self.models))) + .child(field("Default model", Input::new(&self.model))) + .child(self.approval_choice(cx)) + .child(field("Default servers", defaults)) + .child(actions(cx)) + .into_any_element() + } + + fn provider_choices( + &self, + providers: &[Provider], + cx: &mut Context, + ) -> impl IntoElement { + h_flex() + .flex_wrap() + .gap_1() + .children(providers.iter().map(|provider| { + let chosen = provider.clone(); + Button::new(item("server-agent-provider", provider.id.as_str())) + .small() + .ghost() + .label(provider.name.clone()) + .selected(self.provider.as_ref() == Some(&provider.id)) + .on_click(cx.listener(move |tab, _: &ClickEvent, window, cx| { + if let Some(form) = tab.agent_form.as_mut() { + form.choose_provider(&chosen, window, cx); + } + cx.notify(); + })) + })) + } + + fn approval_choice(&self, cx: &mut Context) -> impl IntoElement { + let ask = self.approval == ApprovalMode::Ask; + Checkbox::new("server-agent-ask") + .label("Ask before changes: tools that change something wait for Allow") + .checked(ask) + .on_click(cx.listener(move |tab, _: &bool, _, cx| { + if let Some(form) = tab.agent_form.as_mut() { + form.approval = if ask { + ApprovalMode::Auto + } else { + ApprovalMode::Ask + }; + } + cx.notify(); + })) + } + + fn server_choices( + &self, + servers: &[HubServer], + cx: &mut Context, + ) -> impl IntoElement { + v_flex().gap_1().children(servers.iter().map(|server| { + let id = server.id.clone(); + let on = self.servers.contains(&id); + Checkbox::new(item("server-agent-server", server.id.as_str())) + .label(format!("{} ({}@{})", server.name, server.user, server.host)) + .checked(on) + .on_click(cx.listener(move |tab, _: &bool, _, cx| { + if let Some(form) = tab.agent_form.as_mut() { + if on { + form.servers.remove(&id); + } else { + form.servers.insert(id.clone()); + } + } + cx.notify(); + })) + })) + } +} + +/// Cancel and Save under the form. +fn actions(cx: &mut Context) -> impl IntoElement { + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("server-agent-cancel") + .small() + .ghost() + .label("Cancel") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| { + tab.agent_form = None; + cx.notify(); + })), + ) + .child( + Button::new("server-agent-save") + .small() + .primary() + .label("Save") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| tab.save_agent(cx))), + ) +} diff --git a/crates/hubs-ui/src/servers/tabs/agents/providers.rs b/crates/hubs-ui/src/servers/tabs/agents/providers.rs new file mode 100644 index 00000000..8bd2c006 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/agents/providers.rs @@ -0,0 +1,319 @@ +//! The model providers an agent may use: the hub's, listed, and the account's own, added, +//! changed, tested and removed. A key is typed once; the hub keeps it and never shows it. + +use gpui_kit::{ + AppContext as _, ClickEvent, Context, Entity, TestSupportExt as _, Window, + component::{ + Selectable as _, Sizable as _, StyledExt as _, + button::{Button, ButtonVariants as _}, + h_flex, + input::{Input, InputState}, + v_flex, + }, + div, + prelude::*, +}; +use nocterm_hub_agents_model::{Provider, ProviderDraft, ProviderId, ProviderKind, ProviderScope}; +use nocterm_session::Secret; + +use super::AgentsTab; +use crate::layout::{field, note}; +use crate::messages::agents_problem; +use crate::servers::item; + +/// What the person has typed for a provider of their own. +pub(super) struct ProviderForm { + editing: Option, + kind: ProviderKind, + name: Entity, + base_url: Entity, + models: Entity, + key: Entity, +} + +impl ProviderForm { + fn new(provider: Option<&Provider>, window: &mut Window, cx: &mut Context) -> Self { + let kind = provider.map_or(ProviderKind::default(), |provider| provider.kind); + let mut input = |placeholder: &'static str, text: String, masked: bool| { + cx.new(|cx| { + let mut input = InputState::new(window, cx) + .placeholder(placeholder) + .masked(masked); + input.set_value(text, window, cx); + input + }) + }; + Self { + editing: provider.map(|provider| provider.id.clone()), + kind, + name: input( + "Name", + provider.map(|p| p.name.clone()).unwrap_or_default(), + false, + ), + base_url: input( + "Base URL", + provider.map_or_else( + || kind.default_base_url().to_owned(), + |p| p.base_url.clone(), + ), + false, + ), + models: input( + "Models, separated by commas", + provider.map(|p| p.models.join(", ")).unwrap_or_default(), + false, + ), + key: input( + if provider.is_some() { + "API key (empty keeps the stored one)" + } else { + "API key" + }, + String::new(), + true, + ), + } + } + + fn draft(&self, cx: &gpui_kit::App) -> ProviderDraft { + let text = |input: &Entity| input.read(cx).value().to_string(); + let key = text(&self.key); + ProviderDraft { + kind: self.kind, + name: text(&self.name), + base_url: text(&self.base_url), + models: text(&self.models) + .split(',') + .map(|model| model.trim().to_owned()) + .collect(), + api_key: (!key.trim().is_empty()).then(|| Secret::new(key)), + } + } +} + +impl AgentsTab { + fn open_provider( + &mut self, + provider: Option<&Provider>, + window: &mut Window, + cx: &mut Context, + ) { + self.provider_form = Some(ProviderForm::new(provider, window, cx)); + self.agent_form = None; + cx.notify(); + } + + fn save_provider(&mut self, cx: &mut Context) { + let (Some(hub), Some(form)) = (self.hub.clone(), &self.provider_form) else { + return; + }; + let draft = form.draft(cx); + let directory = self.service.read(cx).directory().clone(); + let call: nocterm_hub_agents_model::AgentsFuture<()> = match form.editing.clone() { + Some(provider) => { + let saving = directory.update_provider(&hub, &provider, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + None => { + let saving = directory.create_provider(&hub, draft); + Box::pin(async move { saving.await.map(|_| ()) }) + } + }; + self.write(call, cx); + } + + fn delete_provider(&mut self, provider: &ProviderId, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let deleting = self + .service + .read(cx) + .directory() + .delete_provider(&hub, provider); + self.write(deleting, cx); + } + + /// Calls the provider from an agent worker and says what came of it. + fn test_provider(&mut self, provider: &ProviderId, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + let testing = self + .service + .read(cx) + .directory() + .test_provider(&hub, provider); + self.working = Some(cx.spawn(async move |this, cx| { + let result = testing.await; + this.update(cx, |tab, cx| { + tab.problem = Some(match result { + Ok(test) if test.ok => format!( + "The provider answers. Models: {}", + if test.models.is_empty() { + "none listed".to_owned() + } else { + test.models.join(", ") + } + ), + Ok(test) => format!("The provider did not answer: {}", test.message), + Err(error) => agents_problem(&error), + }); + cx.notify(); + }) + .ok(); + })); + } +} + +fn row(provider: &Provider, cx: &mut Context) -> gpui_kit::AnyElement { + let scope = match provider.scope { + ProviderScope::Hub => "the hub's", + ProviderScope::Account => "your own", + }; + let id = provider.id.clone(); + let mut actions = h_flex().gap_1().child( + Button::new(item("server-provider-test", provider.id.as_str())) + .small() + .ghost() + .label("Test") + .on_click(cx.listener({ + let id = id.clone(); + move |tab, _: &ClickEvent, _, cx| tab.test_provider(&id, cx) + })), + ); + if provider.editable { + let edit = provider.clone(); + actions = actions + .child( + Button::new(item("server-provider-edit", provider.id.as_str())) + .small() + .ghost() + .label("Edit") + .on_click(cx.listener(move |tab, _: &ClickEvent, window, cx| { + tab.open_provider(Some(&edit), window, cx); + })), + ) + .child( + Button::new(item("server-provider-delete", provider.id.as_str())) + .small() + .ghost() + .label("Delete") + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + tab.delete_provider(&id, cx); + })), + ); + } + h_flex() + .id(item("server-provider", provider.id.as_str())) + .test_support() + .gap_2() + .child( + v_flex() + .flex_1() + .min_w_0() + .child(div().text_sm().font_semibold().child(provider.name.clone())) + .child(note( + format!( + "{scope} · {} · {}", + provider.base_url, + provider.models.join(", ") + ), + cx, + )), + ) + .child(actions) + .into_any_element() +} + +fn form(form: &ProviderForm, cx: &mut Context) -> gpui_kit::AnyElement { + let kinds = h_flex().gap_1().children( + [ + (ProviderKind::OpenAi, "OpenAI-compatible"), + (ProviderKind::Anthropic, "Anthropic"), + ] + .map(|(kind, label)| { + Button::new(gpui_kit::ElementId::Name( + format!("server-provider-kind-{label}").into(), + )) + .small() + .ghost() + .label(label) + .selected(form.kind == kind) + .on_click(cx.listener(move |tab, _: &ClickEvent, _, cx| { + if let Some(form) = tab.provider_form.as_mut() { + form.kind = kind; + } + cx.notify(); + })) + }), + ); + v_flex() + .id("server-provider-form") + .test_support() + .gap_2() + .child(field("Kind", kinds)) + .child(field("Name", Input::new(&form.name))) + .child(field("Base URL", Input::new(&form.base_url))) + .child(field("Models", Input::new(&form.models))) + .child(field("API key", Input::new(&form.key))) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("server-provider-cancel") + .small() + .ghost() + .label("Cancel") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| { + tab.provider_form = None; + cx.notify(); + })), + ) + .child( + Button::new("server-provider-save") + .small() + .primary() + .label("Save") + .on_click(cx.listener(|tab, _: &ClickEvent, _, cx| tab.save_provider(cx))), + ), + ) + .into_any_element() +} + +/// The providers section: the providers, the form open on one, and adding one. +pub(super) fn render( + providers: &[Provider], + open: Option<&ProviderForm>, + cx: &mut Context, +) -> gpui_kit::AnyElement { + let rows: Vec<_> = providers.iter().map(|provider| row(provider, cx)).collect(); + v_flex() + .id("server-providers") + .test_support() + .gap_1() + .child( + h_flex() + .justify_between() + .child(div().font_semibold().child("Model providers")) + .child( + Button::new("server-provider-new") + .small() + .label("Add your own") + .on_click(cx.listener(|tab, _: &ClickEvent, window, cx| { + tab.open_provider(None, window, cx); + })), + ), + ) + .when(rows.is_empty(), |list| { + list.child(note( + "No providers yet. Add your own, or ask the hub's administrator for one.", + cx, + )) + }) + .children(rows) + .children(open.map(|open| form(open, cx))) + .into_any_element() +} diff --git a/crates/hubs-ui/src/servers/tabs/devices.rs b/crates/hubs-ui/src/servers/tabs/devices.rs new file mode 100644 index 00000000..6123fa6b --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/devices.rs @@ -0,0 +1,343 @@ +//! The Devices tab of a server: the devices signed in to the account, and revoking them. +//! +//! The list is read from the hub through the service whenever the tab is shown for a server, +//! and again after a device was revoked. Revoking this device is signing out of the server. + +use gpui_kit::{ + App, ClickEvent, Context, Entity, Task, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Sizable as _, StyledExt as _, WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + h_flex, v_flex, + }, + div, + prelude::*, +}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::{DeviceSummary, HubError, HubId}; + +use crate::layout::{note, problem}; +use crate::manage; +use crate::messages::hub_problem; +use crate::servers::item; + +/// What the list of devices shows. +enum Load { + Loading, + Ready(Vec), + Failed(HubError), +} + +/// The devices of the account on the server that is shown. +pub(in crate::servers) struct Devices { + hubs: Entity, + hub: Option, + load: Load, + loading: Option>, +} + +impl Devices { + pub(in crate::servers) fn new(hubs: Entity) -> Self { + Self { + hubs, + hub: None, + load: Load::Loading, + loading: None, + } + } + + /// Shows the devices of `hub`, reading them when another server was shown before. Showing + /// the same server again changes nothing. + pub(in crate::servers) fn show(&mut self, hub: &HubId, cx: &mut Context) { + if self.hub.as_ref() == Some(hub) { + return; + } + self.hub = Some(hub.clone()); + self.reload(cx); + } + + /// Forgets what was read: the next [`Devices::show`] reads again. + pub(in crate::servers) fn hide(&mut self) { + self.hub = None; + self.loading = None; + } + + fn reload(&mut self, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + self.load = Load::Loading; + let listing = self.hubs.read(cx).list_devices(&hub); + self.loading = Some(cx.spawn(async move |this, cx| { + let result = listing.await; + this.update(cx, |devices, cx| { + if devices.hub.as_ref() != Some(&hub) { + return; + } + devices.load = match result { + Ok(listed) => Load::Ready(sorted(listed)), + Err(error) => Load::Failed(error), + }; + cx.notify(); + }) + .ok(); + })); + cx.notify(); + } + + /// Revokes `device`; this device signs out instead. A failure is reported, and the list is + /// read again either way. + fn revoke(&mut self, device: &DeviceSummary, window: &mut Window, cx: &mut Context) { + let Some(hub) = self.hub.clone() else { + return; + }; + if device.is_current { + manage::sign_out(&self.hubs, &hub, window, cx); + return; + } + let revoking = self.hubs.read(cx).revoke_device(&hub, &device.id); + let view = cx.weak_entity(); + window + .spawn(cx, async move |cx| { + let result = revoking.await; + cx.update(|window, cx| { + if let Err(error) = &result { + nocterm_ui::notice::error( + window, + cx, + "hub-devices", + "Could not revoke the device", + hub_problem(error), + ); + } + let _ = view.update(cx, |devices, cx| devices.reload(cx)); + }) + .ok(); + }) + .detach(); + } + + /// Asks before revoking: the device is signed out and has to sign in again. + fn confirm_revoke( + view: Entity, + device: DeviceSummary, + window: &mut Window, + cx: &mut App, + ) { + window.open_alert_dialog(cx, move |alert, _, _| { + let (view, chosen) = (view.clone(), device.clone()); + let confirm = Button::new("revoke-device-confirm") + .danger() + .label("Revoke") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + view.update(cx, |devices, cx| devices.revoke(&chosen, window, cx)); + }); + let keep = Button::new("revoke-device-keep") + .primary() + .label("Keep it") + .on_click(|_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + }); + let consequence = if device.is_current { + "This is this device: revoking it signs you out of the server." + } else { + "It is signed out of the account and has to sign in again." + }; + alert + .title(format!("Revoke {}?", device.name)) + .description(consequence) + .on_ok(|_, _, _| true) + .footer(DialogFooter::new().child(confirm).child(keep)) + }); + } +} + +/// This device first, then the others by when they were last seen, newest first. +fn sorted(mut devices: Vec) -> Vec { + devices.sort_by_key(|device| { + ( + !device.is_current, + std::cmp::Reverse(device.last_seen_at_ms), + ) + }); + devices +} + +impl Render for Devices { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let view = cx.entity(); + let body = match &self.load { + Load::Loading => div() + .id("devices-loading") + .test_support() + .child(note("Reading the devices of your account…", cx)) + .into_any_element(), + Load::Failed(error) => self.failed(error, cx), + Load::Ready(devices) if devices.is_empty() => div() + .id("devices-empty") + .test_support() + .child(note("The server lists no devices.", cx)) + .into_any_element(), + Load::Ready(devices) => v_flex() + .gap_2() + .children(devices.iter().map(|device| row(device, &view, cx))) + .into_any_element(), + }; + v_flex() + .id("devices-tab") + .test_support() + .gap_3() + .child(note( + "The devices signed in to your account on this server. Revoking one signs it out.", + cx, + )) + .child(body) + } +} + +impl Devices { + fn failed(&self, error: &HubError, cx: &mut Context) -> gpui_kit::AnyElement { + let view = cx.entity(); + v_flex() + .id("devices-error") + .test_support() + .gap_2() + .child(problem(hub_problem(error), cx)) + .child( + h_flex().child(Button::new("devices-retry").label("Try again").on_click( + move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + view.update(cx, |devices, cx| devices.reload(cx)); + }, + )), + ) + .into_any_element() + } +} + +/// One device: its name, kind and last sign-in, and the button that revokes it. +fn row(device: &DeviceSummary, view: &Entity, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + let (view, chosen) = (view.clone(), device.clone()); + let seen = if device.last_seen_at_ms > 0 { + format!("Last seen {}", utc(device.last_seen_at_ms)) + } else { + "Never seen".to_owned() + }; + h_flex() + .id(item("device", &device.id)) + .test_support() + .gap_3() + .p_3() + .items_center() + .rounded(theme.radius) + .border_1() + .border_color(theme.border) + .child( + v_flex() + .flex_1() + .min_w_0() + .gap_1() + .child( + h_flex() + .gap_2() + .items_center() + .child(div().font_semibold().truncate().child(device.name.clone())) + .when(device.is_current, |line| { + line.child( + div() + .id(item("device-current", &device.id)) + .test_support() + .text_xs() + .text_color(theme.success) + .child("This device"), + ) + }), + ) + .child( + div() + .id(item("device-seen", &device.id)) + .test_support() + .text_xs() + .text_color(theme.muted_foreground) + .child(format!("{} · {seen}", kind(&device.platform))), + ), + ) + .child( + Button::new(item("device-revoke", &device.id)) + .danger() + .small() + .label("Revoke") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + Devices::confirm_revoke(view.clone(), chosen.clone(), window, cx); + }), + ) +} + +/// The kind of client as a person reads it; an empty report is an unknown kind. +fn kind(platform: &str) -> String { + let mut chars = platform.chars(); + match chars.next() { + Some(first) => first.to_uppercase().chain(chars).collect(), + None => "Unknown kind".to_owned(), + } +} + +/// A moment as `2023-11-14 22:13 UTC`, so a list reads the same wherever it is shown. +pub(in crate::servers) fn utc(ms: i64) -> String { + let seconds = ms.div_euclid(1000); + let (days, rest) = (seconds.div_euclid(86_400), seconds.rem_euclid(86_400)); + let z = days + 719_468; + let era = z.div_euclid(146_097); + let day_of_era = z.rem_euclid(146_097); + let year_of_era = + (day_of_era - day_of_era / 1_460 + day_of_era / 36_524 - day_of_era / 146_096) / 365; + let day_of_year = day_of_era - (365 * year_of_era + year_of_era / 4 - year_of_era / 100); + let shifted_month = (5 * day_of_year + 2) / 153; + let day = day_of_year - (153 * shifted_month + 2) / 5 + 1; + let month = if shifted_month < 10 { + shifted_month + 3 + } else { + shifted_month - 9 + }; + let year = year_of_era + era * 400 + i64::from(month <= 2); + format!( + "{year:04}-{month:02}-{day:02} {:02}:{:02} UTC", + rest / 3_600, + rest % 3_600 / 60 + ) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn a_moment_reads_as_a_utc_date_and_time() { + assert_eq!(utc(0), "1970-01-01 00:00 UTC"); + assert_eq!(utc(1_700_000_000_000), "2023-11-14 22:13 UTC"); + assert_eq!(utc(951_782_400_000), "2000-02-29 00:00 UTC"); + } + + #[test] + fn this_device_is_listed_first_and_the_others_by_recency() { + let device = |id: &str, seen, current| DeviceSummary { + id: id.into(), + name: id.into(), + platform: "desktop".into(), + created_at_ms: 0, + last_seen_at_ms: seen, + is_current: current, + }; + let order: Vec<_> = sorted(vec![ + device("old", 1, false), + device("new", 9, false), + device("here", 0, true), + ]) + .into_iter() + .map(|device| device.id) + .collect(); + assert_eq!(order, ["here", "new", "old"]); + } +} diff --git a/crates/hubs-ui/src/servers/tabs/names.rs b/crates/hubs-ui/src/servers/tabs/names.rs new file mode 100644 index 00000000..5635729f --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/names.rs @@ -0,0 +1,104 @@ +//! The name fields of the known servers. + +use std::collections::HashMap; + +use gpui_kit::{App, Context, Entity, Subscription, Window}; +use nocterm_hubs::Hubs; +use nocterm_hubs_model::{HubId, HubsSettings}; +use nocterm_ui::SettingsTextField; + +/// The name field of one server: the shared text field of the settings, which keeps what the +/// user typed until its save is settled, saves a distinct text once and shows the stored name +/// again when the settings change elsewhere. +struct NameField { + field: SettingsTextField, + _observed: Subscription, +} + +/// The name fields of the known servers, one each. +pub(in crate::servers) struct NameFields { + hubs: Entity, + fields: HashMap, +} + +impl NameFields { + pub(in crate::servers) fn new(hubs: Entity) -> Self { + Self { + hubs, + fields: HashMap::new(), + } + } + + /// The field of `hub`, once [`NameFields::sync`] made it. + pub(in crate::servers) fn get(&self, hub: &HubId) -> Option { + self.fields.get(hub).map(|name| name.field.clone()) + } + + /// Saves what is typed in every field that has unsaved text. + pub(in crate::servers) fn flush(&self, window: &mut Window, cx: &mut App) { + for name in self.fields.values() { + name.field.flush(window, cx); + } + } + + /// Gives each known server a name field and drops the fields of forgotten ones. A field + /// that exists keeps what the user is typing. The context owner is redrawn when a field changes. + pub(in crate::servers) fn sync( + &mut self, + window: &mut Window, + cx: &mut Context, + ) { + let known: Vec = self + .hubs + .read(cx) + .list() + .into_iter() + .map(|s| s.id) + .collect(); + self.fields.retain(|hub, _| known.contains(hub)); + for hub in known { + if self.fields.contains_key(&hub) { + continue; + } + let Some(address) = self + .hubs + .read(cx) + .record(&hub) + .map(|record| record.address.to_string()) + else { + continue; + }; + let (reading, writing) = (hub.clone(), hub.clone()); + let field = SettingsTextField::new( + self.hubs.read(cx).settings(), + format!("server-name-input-{address}"), + move |document| { + document + .get::() + .find(&reading) + .map(|record| record.label.clone()) + .unwrap_or_default() + }, + move |document, text| { + document.update::(|settings| { + settings.set_label(&writing, text); + }); + Ok(()) + }, + window, + cx, + ); + let page = cx.weak_entity(); + let observed = field.observe(cx, move |cx| { + let _ = page.update(cx, |_, cx| cx.notify()); + }); + self.fields.insert( + hub, + NameField { + field, + _observed: observed, + }, + ); + } + } +} diff --git a/crates/hubs-ui/src/servers/tabs/overview.rs b/crates/hubs-ui/src/servers/tabs/overview.rs new file mode 100644 index 00000000..fc62a8f7 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/overview.rs @@ -0,0 +1,200 @@ +//! The Overview tab of a server on the Servers page. + +use gpui_kit::{ + App, ClickEvent, Entity, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Icon, Selectable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::{HubStatus, HubSummary, Hubs}; +use nocterm_hubs_model::{HubColor, HubRecord, TrustPolicy}; +use nocterm_ui::{IconName, SettingsTextField}; + +use crate::color::tint; +use crate::commands::can_sign_out; +use crate::layout::{field, fingerprint_block, note, problem}; +use crate::manage; +use crate::servers::item; +use crate::status::status_chip; + +/// Everything about the server itself: status, address, name, colour, certificate and what can +/// be done to it. +pub(in crate::servers) fn overview( + summary: &HubSummary, + record: &HubRecord, + name: &SettingsTextField, + hubs: &Entity, + cx: &App, +) -> gpui_kit::AnyElement { + let key = summary.address.to_string(); + let (status, status_color) = status_chip(&summary.status, cx); + v_flex() + .id(item("server-overview", &key)) + .test_support() + .gap_3() + .child( + h_flex() + .gap_2() + .items_center() + .child( + div() + .id(item("server-status", &key)) + .test_support() + .text_xs() + .text_color(status_color) + .child(status), + ) + .children(retry(summary, hubs, &key)), + ) + .child(note(summary.address.to_string(), cx)) + .child(field( + "Name", + div() + .id(item("server-name", &key)) + .test_support() + .child(name.input()), + )) + .children(name.error(cx).map(|error| problem(error.to_string(), cx))) + .child(colors(summary, hubs, &key, cx)) + .child(certificate(summary, record, &key, cx)) + .child(actions(summary, hubs, &key)) + .into_any_element() +} + +/// The button that connects at once while the server waits for the next attempt. +fn retry(summary: &HubSummary, hubs: &Entity, key: &str) -> Option { + if !matches!(summary.status, HubStatus::Offline { .. }) { + return None; + } + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Some( + Button::new(item("server-retry", key)) + .xsmall() + .label("Try again") + .on_click(move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + hubs.update(cx, |hubs, cx| hubs.retry(&id, cx)); + }), + ) +} + +/// The chooser of the colour that marks the server. +fn colors(summary: &HubSummary, hubs: &Entity, key: &str, cx: &App) -> impl IntoElement { + let theme = cx.theme(); + let (border, foreground) = (theme.border, theme.foreground); + let none = { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Button::new(item("server-color-none", key)) + .ghost() + .xsmall() + .label("None") + .selected(summary.color.is_none()) + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + manage::recolor(&hubs, &id, None, window, cx); + }) + }; + let swatches = HubColor::ALL.into_iter().map(|color| { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + let chosen = summary.color == Some(color); + div() + .id(item(&format!("server-color-{}", color.name()), key)) + .test_support() + .size_5() + .rounded_full() + .flex() + .items_center() + .justify_center() + .cursor_pointer() + .bg(tint(color, cx)) + .border_2() + .border_color(if chosen { foreground } else { border }) + .when(chosen, |swatch| { + swatch.child(Icon::new(IconName::Check).xsmall().text_color(foreground)) + }) + .on_click(move |_, window, cx| { + manage::recolor(&hubs, &id, Some(color), window, cx); + }) + }); + field( + "Colour", + h_flex() + .gap_2() + .items_center() + .child(none) + .children(swatches), + ) +} + +/// How the server's certificate is trusted. It is only shown: a certificate that changed is +/// never accepted here, and removing the server is the way to sign in to the new one. +fn certificate(summary: &HubSummary, record: &HubRecord, key: &str, cx: &App) -> impl IntoElement { + let mut section = v_flex() + .id(item("server-trust", key)) + .test_support() + .gap_2(); + match record.trust { + TrustPolicy::Pinned(fingerprint) => { + section = section.child(div().id(item("server-pinned", key)).test_support().child( + fingerprint_block("Pinned certificate (SHA-256)", &fingerprint, cx), + )); + } + TrustPolicy::PublicCa => { + section = section.child( + div() + .id(item("server-public-ca", key)) + .test_support() + .child(note( + "Trusted through a public certificate authority. No certificate is pinned.", + cx, + )), + ); + } + } + if let HubStatus::TrustFailed { presented, .. } = &summary.status { + section = section + .child(problem( + "The server presents another certificate than the one trusted. Nocterm will not connect. Remove the server and sign in again only if you can confirm the new fingerprint with whoever runs it." + .to_owned(), + cx, + )) + .child( + div() + .id(item("server-presented", key)) + .test_support() + .child(fingerprint_block("Presented now", presented, cx)), + ); + } + section +} + +/// Sign out and remove. +fn actions(summary: &HubSummary, hubs: &Entity, key: &str) -> impl IntoElement { + let signed_in = can_sign_out(summary); + let sign_out = signed_in.then(|| { + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Button::new(item("server-sign-out", key)) + .label("Sign out") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + manage::sign_out(&hubs, &id, window, cx); + }) + }); + let remove = { + let (hubs, summary) = (hubs.clone(), summary.clone()); + Button::new(item("server-remove", key)) + .danger() + .label("Remove…") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + manage::confirm_remove(hubs.clone(), summary.clone(), window, cx); + }) + }; + h_flex() + .gap_2() + .justify_end() + .mt(px(2.)) + .children(sign_out) + .child(remove) +} diff --git a/crates/hubs-ui/src/servers/tabs/trust.rs b/crates/hubs-ui/src/servers/tabs/trust.rs new file mode 100644 index 00000000..cab5f818 --- /dev/null +++ b/crates/hubs-ui/src/servers/tabs/trust.rs @@ -0,0 +1,70 @@ +//! The Keys tab of a server: the account keys, opened and closed on this device, and a +//! recovery key that was never confirmed as saved. + +use gpui_kit::{ + App, ClickEvent, Entity, TestSupportExt as _, Window, + component::{button::Button, h_flex, v_flex}, + prelude::*, +}; +use nocterm_hubs::{HubSummary, Hubs}; + +use super::account_keys::AccountKeys; +use crate::layout::{field, note}; +use crate::servers::item; + +/// The account keys of the server and, while one was never saved, the offer of a new recovery +/// key. +pub(in crate::servers) fn trust( + summary: &HubSummary, + hubs: &Entity, + keys: &Entity, + cx: &App, +) -> gpui_kit::AnyElement { + let key = summary.address.to_string(); + let recovery = recovery_key(summary, hubs, &key, cx); + v_flex() + .id(item("server-trust-tab", &key)) + .test_support() + .gap_3() + .child(field("Vault keys", keys.clone())) + .children(recovery) + .into_any_element() +} + +/// The offer of a new recovery key, while the one made with the account keys on this device was +/// never confirmed as saved and the keys are open. +fn recovery_key( + summary: &HubSummary, + hubs: &Entity, + key: &str, + cx: &App, +) -> Option { + if !hubs.read(cx).recovery_key_unconfirmed(&summary.id) { + return None; + } + let offer = hubs.read(cx).recovery_key_offer(&summary.id).ok()?; + let (hubs, id) = (hubs.clone(), summary.id.clone()); + Some( + field( + "Recovery key", + v_flex() + .id(item("server-recovery", key)) + .test_support() + .gap_2() + .child(note( + "The recovery key made with your account keys was never confirmed as saved. Issue a new one so you can still open them if you lose your password.", + cx, + )) + .child( + h_flex().justify_end().child( + Button::new(item("server-recovery-issue", key)) + .label("Issue a new recovery key") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + crate::sign_in::start_reissue(&hubs, &id, offer.clone(), window, cx); + }), + ), + ), + ) + .into_any_element(), + ) +} diff --git a/crates/hubs-ui/src/servers_agents_tests.rs b/crates/hubs-ui/src/servers_agents_tests.rs new file mode 100644 index 00000000..933eb2ba --- /dev/null +++ b/crates/hubs-ui/src/servers_agents_tests.rs @@ -0,0 +1,123 @@ +//! The Agents and Agent access tabs of a server read and write the account's agents through the +//! agents service. +use super::{ServersPage, item}; +use crate::test_support::{Fixture, fixture_over, hub_id, pinned_record, server}; +use gpui_kit::{AppContext as _, TestAppContext, WindowOptions, test::TestWindowExt as _}; +use nocterm_hub_agents_model::{HubServers as _, NewServer}; +use nocterm_hubs_model::Environment; +use nocterm_hubs_model::testing::FakeHub; +use nocterm_settings::SettingsDocument; +use nocterm_store::SettingsConfig; + +const AGENTS: usize = 3; +const ACCESS: usize = 4; + +fn page(cx: &mut TestAppContext) -> Fixture { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let setup = fixture_over( + cx, + config, + Environment::Local, + FakeHub::new().with_server(server()), + vec![pinned_record()], + Some("ada"), + ); + let hubs = setup.installed.hubs().clone(); + let agents = setup.installed.hub_agents().cloned(); + let (window, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |_, cx| { + cx.new(|cx| ServersPage::new(hubs, agents, cx)) + }) + .unwrap() + }); + let page = Fixture { window, ..setup }; + page.render(cx); + page +} + +fn open_tab(page: &Fixture, index: usize, cx: &mut TestAppContext) { + page.render(cx); + cx.update_window(page.window, |_, window, cx| { + window.within("servers-tabs").click(index, cx); + }) + .unwrap(); + cx.run_until_parked(); + page.render(cx); + page.render(cx); +} + +#[gpui_kit::test] +fn the_agents_tab_lists_the_accounts_agents_and_providers(cx: &mut TestAppContext) { + let page = page(cx); + let _ = page + .agents + .clone() + .with_provider(&hub_id(), "Models", &["m1"]) + .with_agent(&hub_id(), "Ops", "m1"); + open_tab(&page, AGENTS, cx); + assert!(page.shows(item("server-agents-tab", &hub_id().to_string()), cx)); + assert!(page.shows(item("server-agent", "agent-2"), cx)); + assert!(page.shows(item("server-provider", "provider-1"), cx)); +} + +#[gpui_kit::test] +fn deleting_an_agent_removes_it_on_the_hub_and_from_the_list(cx: &mut TestAppContext) { + let page = page(cx); + let _ = page + .agents + .clone() + .with_provider(&hub_id(), "Models", &["m1"]) + .with_agent(&hub_id(), "Ops", "m1"); + open_tab(&page, AGENTS, cx); + page.click(item("server-agent-delete", "agent-2"), cx); + cx.run_until_parked(); + page.render(cx); + assert!(page.agents.calls().contains(&"delete_agent".to_owned())); + assert!(!page.shows(item("server-agent", "agent-2"), cx)); +} + +#[gpui_kit::test] +fn the_new_agent_form_opens_and_closes(cx: &mut TestAppContext) { + let page = page(cx); + open_tab(&page, AGENTS, cx); + page.click(item("server-agent-new", &hub_id().to_string()), cx); + assert!(page.shows("server-agent-form", cx)); + page.click("server-agent-cancel", cx); + assert!(!page.shows("server-agent-form", cx)); +} + +#[gpui_kit::test] +fn the_agent_access_tab_shows_the_key_and_disconnects_a_server(cx: &mut TestAppContext) { + let page = page(cx); + let added = futures::executor::block_on(page.agents.add_server( + &hub_id(), + NewServer { + name: "prod".into(), + host: "prod.example".into(), + port: 22, + user: "root".into(), + host_key: "ssh-ed25519 AAAAhost".into(), + }, + )) + .unwrap(); + open_tab(&page, ACCESS, cx); + assert!(page.shows(item("agent-key", &hub_id().to_string()), cx)); + assert!(page.shows(item("agent-server", added.id.as_str()), cx)); + page.click(item("agent-server-disconnect", added.id.as_str()), cx); + cx.run_until_parked(); + page.render(cx); + assert!(!page.shows(item("agent-server", added.id.as_str()), cx)); +} + +#[gpui_kit::test] +fn replacing_the_agent_key_asks_first(cx: &mut TestAppContext) { + let page = page(cx); + open_tab(&page, ACCESS, cx); + page.click(item("agent-key-rotate", &hub_id().to_string()), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx)); + assert!(!page.agents.calls().contains(&"rotate_agent_key".to_owned())); + page.click("agent-key-rotate-confirm", cx); + cx.run_until_parked(); + assert!(page.agents.calls().contains(&"rotate_agent_key".to_owned())); +} diff --git a/crates/hubs-ui/src/servers_tests.rs b/crates/hubs-ui/src/servers_tests.rs new file mode 100644 index 00000000..660e09bd --- /dev/null +++ b/crates/hubs-ui/src/servers_tests.rs @@ -0,0 +1,595 @@ +//! The Servers page lists the known servers and manages them through the service. +use std::cell::Cell; +use std::rc::Rc; +use std::sync::Arc; + +use super::{ServersPage, item, settings_page}; +use crate::test_support::{Fixture, fixture_over, hub_id, pinned_record, server}; +use gpui_kit::{AppContext as _, TestAppContext, WindowOptions, test::TestWindowExt as _}; +use nocterm_hubs::{HubStatus, HubSummary}; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp, FakeServer}; +use nocterm_hubs_model::{ + DeviceSummary, Environment, Fingerprint, HubColor, HubError, HubRecord, TrustPolicy, +}; +use nocterm_settings::SettingsDocument; +use nocterm_store::{SettingsConfig, SnapshotWriter, StoreError}; + +const KEY: &str = "hub.home:7443"; +const OTHER: &str = "hub.work:7443"; + +const OVERVIEW: usize = 0; +const DEVICES: usize = 1; +const KEYS: usize = 2; + +/// Opens the tab `index` of the selected server. +fn open_tab(page: &Fixture, index: usize, cx: &mut TestAppContext) { + page.render(cx); + cx.update_window(page.window, |_, window, cx| { + window.within("servers-tabs").click(index, cx); + }) + .unwrap(); + cx.run_until_parked(); + page.render(cx); +} + +/// The page in a window of its own, over a hub that `signed_in` names a user of. +fn page_over( + cx: &mut TestAppContext, + config: SettingsConfig, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + page_on( + cx, + config, + FakeHub::new().with_server(server()), + known, + signed_in, + ) +} + +fn page_on( + cx: &mut TestAppContext, + config: SettingsConfig, + hub: FakeHub, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + let setup = fixture_over(cx, config, Environment::Local, hub, known, signed_in); + let hubs = setup.installed.hubs().clone(); + let (window, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |_, cx| { + cx.new(|cx| ServersPage::new(hubs, None, cx)) + }) + .unwrap() + }); + let page = Fixture { window, ..setup }; + page.render(cx); + page +} + +fn page(cx: &mut TestAppContext, known: Vec) -> Fixture { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + page_over(cx, config, known, Some("ada")) +} + +fn servers(page: &Fixture, cx: &mut TestAppContext) -> Vec { + cx.update(|cx| page.installed.hubs().read(cx).list()) +} + +/// How many times the settings were published. +fn revision(page: &Fixture, cx: &mut TestAppContext) -> u64 { + cx.update(|cx| page.installed.hubs().read(cx).settings().revision(cx)) +} + +fn failing_config() -> SettingsConfig { + let writer: SnapshotWriter = Arc::new(|_| { + Box::pin(async { + Err(StoreError::backend(std::io::Error::from( + std::io::ErrorKind::PermissionDenied, + ))) + }) + }); + SettingsConfig::with_writer(SettingsDocument::default(), writer) +} + +fn notices(page: &Fixture, cx: &mut TestAppContext) -> usize { + cx.update_window(page.window, |_, window, cx| { + nocterm_ui::notice::count(window, cx) + }) + .unwrap() +} + +#[gpui_kit::test] +fn without_servers_the_page_invites_signing_in(cx: &mut TestAppContext) { + let page = page(cx, Vec::new()); + assert!(page.shows("servers-empty", cx)); + assert!(page.shows("servers-add", cx)); +} + +#[gpui_kit::test] +fn the_page_is_the_servers_page_of_the_settings(cx: &mut TestAppContext) { + let page = page(cx, Vec::new()); + let spec = settings_page(&page.installed); + assert_eq!(spec.id, "servers"); + assert_eq!(spec.title.as_ref(), "Servers"); +} + +#[gpui_kit::test] +fn a_pinned_server_shows_its_fingerprint(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + assert!(page.shows(item("server", KEY), cx)); + assert!(page.shows(item("server-pinned", KEY), cx)); + assert!(!page.shows(item("server-public-ca", KEY), cx)); + assert!(!page.shows(item("server-presented", KEY), cx)); +} + +#[gpui_kit::test] +fn a_server_trusted_through_a_public_authority_says_nothing_is_pinned(cx: &mut TestAppContext) { + let record = HubRecord::new(pinned_record().address, TrustPolicy::PublicCa, "Home"); + let page = page(cx, vec![record]); + assert!(page.shows(item("server-public-ca", KEY), cx)); + assert!(!page.shows(item("server-pinned", KEY), cx)); +} + +#[gpui_kit::test] +fn a_changed_certificate_is_shown_and_removing_is_the_only_way_out(cx: &mut TestAppContext) { + let changed = HubRecord::new( + pinned_record().address, + TrustPolicy::Pinned(Fingerprint::from_bytes([9; 32])), + "Home", + ); + let page = page(cx, vec![changed.clone()]); + assert!(matches!( + servers(&page, cx)[0].status, + HubStatus::TrustFailed { .. } + )); + assert!(page.shows(item("server-pinned", KEY), cx)); + assert!(page.shows(item("server-presented", KEY), cx)); + assert!(page.shows(item("server-remove", KEY), cx)); + assert!(!page.shows(item("server-sign-out", KEY), cx)); + assert!(!page.shows("sign-in-trust", cx), "nothing to accept"); + assert_eq!( + cx.update(|cx| page.installed.hubs().read(cx).record(&hub_id()).cloned()), + Some(changed), + "the record is as it was" + ); +} + +#[gpui_kit::test] +fn a_name_typed_in_the_row_is_saved_with_enter(cx: &mut TestAppContext) { + let page = page(cx, vec![server().pinned_record("")]); + page.click(item("server-name", KEY), cx); + page.type_text("Office", cx); + page.press("enter", cx); + cx.run_until_parked(); + let label = cx.update(|cx| page.installed.label(&hub_id(), cx)); + assert_eq!(label.as_deref(), Some("Office")); +} + +#[gpui_kit::test] +fn a_name_that_was_not_edited_never_overwrites_a_newer_one(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.render(cx); + // The name changes elsewhere, for instance in another window. + page.installed + .hubs() + .update(cx, |hubs, cx| hubs.set_label(&hub_id(), "Elsewhere", cx)) + .detach(); + cx.run_until_parked(); + + page.click(item("server-name", KEY), cx); + page.press("tab", cx); + cx.run_until_parked(); + + let label = cx.update(|cx| page.installed.label(&hub_id(), cx)); + assert_eq!(label.as_deref(), Some("Elsewhere")); +} + +#[gpui_kit::test] +fn a_name_is_saved_once_when_enter_is_followed_by_leaving_the_field(cx: &mut TestAppContext) { + let page = page(cx, vec![server().pinned_record("")]); + page.click(item("server-name", KEY), cx); + page.type_text("Office", cx); + let before = revision(&page, cx); + page.press("enter", cx); + page.press("tab", cx); + cx.run_until_parked(); + + assert_eq!(revision(&page, cx), before + 1); +} + +#[gpui_kit::test] +fn a_cleared_name_shows_the_address_again(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-name", KEY), cx); + page.press("ctrl-a", cx); + page.press("backspace", cx); + page.press("enter", cx); + cx.run_until_parked(); + let label = cx.update(|cx| page.installed.label(&hub_id(), cx)); + assert_eq!(label.as_deref(), Some(KEY)); +} + +#[gpui_kit::test] +fn choosing_a_colour_marks_the_server_and_none_clears_the_mark(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-color-blue", KEY), cx); + cx.run_until_parked(); + assert_eq!(servers(&page, cx)[0].color, Some(HubColor::Blue)); + page.click(item("server-color-none", KEY), cx); + cx.run_until_parked(); + assert_eq!(servers(&page, cx)[0].color, None); +} + +#[gpui_kit::test] +fn signing_out_keeps_the_server_listed_and_offers_no_second_sign_out(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + assert!(page.shows(item("server-sign-out", KEY), cx)); + page.click(item("server-sign-out", KEY), cx); + cx.run_until_parked(); + page.render(cx); + let listed = servers(&page, cx); + assert_eq!(listed.len(), 1); + assert_eq!(listed[0].status, HubStatus::SignedOut); + assert!(!page.hub.is_signed_in(&hub_id())); + assert!(!page.shows(item("server-sign-out", KEY), cx)); + assert!(page.shows(item("server-remove", KEY), cx)); +} + +#[gpui_kit::test] +fn a_server_that_is_not_signed_in_has_no_sign_out(cx: &mut TestAppContext) { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_over(cx, config, vec![pinned_record()], None); + assert!(!page.shows(item("server-sign-out", KEY), cx)); +} + +#[gpui_kit::test] +fn keeping_a_server_at_the_confirmation_changes_nothing(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-remove", KEY), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx), "removal asks first"); + assert!(servers(&page, cx).len() == 1); + page.click("remove-server-keep", cx); + cx.run_until_parked(); + assert_eq!(servers(&page, cx).len(), 1); + assert!(page.hub.is_signed_in(&hub_id())); +} + +#[gpui_kit::test] +fn confirming_removes_the_server_and_erases_its_credentials(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + page.click(item("server-remove", KEY), cx); + page.settle_dialog(cx); + page.click("remove-server-confirm", cx); + cx.run_until_parked(); + page.render(cx); + assert!(servers(&page, cx).is_empty()); + assert!(!page.hub.is_signed_in(&hub_id())); + assert!(page.hub.calls().contains(&FakeCall::Forget(hub_id()))); + assert!(!page.shows(item("server", KEY), cx)); + assert!(page.shows("servers-empty", cx)); +} + +#[gpui_kit::test] +fn a_change_that_cannot_be_saved_is_reported(cx: &mut TestAppContext) { + let page = page_over(cx, failing_config(), vec![pinned_record()], Some("ada")); + page.click(item("server-color-red", KEY), cx); + cx.run_until_parked(); + page.render(cx); + assert_eq!(notices(&page, cx), 1); + assert_eq!(servers(&page, cx)[0].color, None, "the colour was not kept"); +} + +#[gpui_kit::test] +fn a_server_whose_recovery_key_was_never_confirmed_offers_a_new_one(cx: &mut TestAppContext) { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "secret", "recovery-ada")); + hub.leave_recovery_key_unconfirmed(&hub_id()); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub, vec![pinned_record()], Some("ada")); + open_tab(&page, KEYS, cx); + assert!(page.shows(item("server-recovery-issue", KEY), cx)); + + page.click(item("server-recovery-issue", KEY), cx); + page.settle_dialog(cx); + cx.run_until_parked(); + page.render(cx); + assert!(page.shows("sign-in-key-saved", cx)); + assert_eq!(page.hub.count(FakeOp::ReissueRecoveryKey), 1); + + page.click("sign-in-key-saved", cx); + cx.run_until_parked(); + page.render(cx); + assert!( + !page.shows(item("server-recovery-issue", KEY), cx), + "a saved key is not offered again" + ); +} + +#[gpui_kit::test] +fn a_server_with_a_confirmed_recovery_key_offers_no_new_one(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + open_tab(&page, KEYS, cx); + assert!(!page.shows(item("server-recovery-issue", KEY), cx)); +} + +/// Counts how often the page asks to be drawn again. +fn count_redraws(page: &Fixture, cx: &mut TestAppContext) -> Rc> { + let redraws = Rc::new(Cell::new(0)); + let counted = Rc::clone(&redraws); + cx.update_window(page.window, |root, _, cx| { + let servers = root + .downcast::() + .unwrap() + .read(cx) + .view() + .clone() + .downcast::() + .unwrap(); + cx.observe(&servers, move |_, _| counted.set(counted.get() + 1)) + .detach(); + }) + .unwrap(); + redraws +} + +#[gpui_kit::test] +fn the_offer_of_a_new_recovery_key_appears_as_soon_as_the_vault_is_unlocked( + cx: &mut TestAppContext, +) { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "secret", "recovery-ada")); + hub.leave_recovery_key_unconfirmed(&hub_id()); + hub.lock_keys(&hub_id()); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub.clone(), vec![pinned_record()], Some("ada")); + open_tab(&page, KEYS, cx); + assert!( + !page.shows(item("server-recovery-issue", KEY), cx), + "a locked vault offers nothing" + ); + let redraws = count_redraws(&page, cx); + + hub.unlock_keys(&hub_id()); + cx.run_until_parked(); + + assert!(redraws.get() > 0, "the page is redrawn by the unlock alone"); + page.render(cx); + assert!(page.shows(item("server-recovery-issue", KEY), cx)); +} + +fn two_servers() -> (FakeHub, Vec) { + let other = FakeServer::new(OTHER, 7) + .named("Work hub") + .with_user("ada", "secret"); + let records = vec![pinned_record(), other.pinned_record("Work")]; + ( + FakeHub::new().with_server(server()).with_server(other), + records, + ) +} + +fn device(id: &str, name: &str, seen: i64, current: bool) -> DeviceSummary { + DeviceSummary { + id: id.into(), + name: name.into(), + platform: "desktop".into(), + created_at_ms: 1_000, + last_seen_at_ms: seen, + is_current: current, + } +} + +/// The page over a signed-in server whose account has a laptop (this device) and a phone. +fn page_with_devices(cx: &mut TestAppContext) -> Fixture { + let page = page(cx, vec![pinned_record()]); + page.devices.set( + &hub_id(), + vec![ + device("phone", "Pixel", 1_700_000_000_000, false), + device("laptop", "Laptop", 1_700_000_500_000, true), + ], + ); + page +} + +#[gpui_kit::test] +fn the_first_server_is_selected_and_choosing_another_shows_its_overview(cx: &mut TestAppContext) { + let (hub, records) = two_servers(); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub, records, Some("ada")); + assert!(page.shows(item("server-select", KEY), cx)); + assert!(page.shows(item("server-select", OTHER), cx)); + assert!(page.shows(item("server-overview", KEY), cx)); + assert!(!page.shows(item("server-overview", OTHER), cx)); + + page.click(item("server-select", OTHER), cx); + + assert!(page.shows(item("server-overview", OTHER), cx)); + assert!(!page.shows(item("server-overview", KEY), cx)); +} + +#[gpui_kit::test] +fn the_tabs_switch_between_the_overview_the_devices_and_the_keys(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + assert!(page.shows(item("server-overview", KEY), cx)); + + open_tab(&page, DEVICES, cx); + assert!(page.shows("devices-tab", cx)); + assert!(!page.shows(item("server-overview", KEY), cx)); + + open_tab(&page, KEYS, cx); + assert!(page.shows(item("server-trust-tab", KEY), cx)); + assert!(!page.shows("devices-tab", cx)); + + open_tab(&page, OVERVIEW, cx); + assert!(page.shows(item("server-overview", KEY), cx)); +} + +#[gpui_kit::test] +fn choosing_another_server_starts_at_its_overview(cx: &mut TestAppContext) { + let (hub, records) = two_servers(); + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_on(cx, config, hub, records, Some("ada")); + open_tab(&page, KEYS, cx); + + page.click(item("server-select", OTHER), cx); + + assert!(page.shows(item("server-overview", OTHER), cx)); +} + +#[gpui_kit::test] +fn the_devices_tab_lists_the_account_devices_and_marks_this_one(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + assert!(page.shows(item("device", "phone"), cx)); + assert!(page.shows(item("device", "laptop"), cx)); + assert!(page.shows(item("device-current", "laptop"), cx)); + assert!(!page.shows(item("device-current", "phone"), cx)); + assert!(page.shows(item("device-seen", "phone"), cx)); +} + +#[gpui_kit::test] +fn the_devices_tab_says_so_while_the_list_is_read_and_when_there_is_none(cx: &mut TestAppContext) { + let page = page(cx, vec![pinned_record()]); + let release = page.devices.hold_next_list(); + open_tab(&page, DEVICES, cx); + assert!(page.shows("devices-loading", cx)); + + drop(release); + cx.run_until_parked(); + page.render(cx); + assert!(!page.shows("devices-loading", cx)); + assert!(page.shows("devices-empty", cx)); +} + +#[gpui_kit::test] +fn a_device_list_that_cannot_be_read_is_reported_and_read_again_on_request( + cx: &mut TestAppContext, +) { + let page = page_with_devices(cx); + page.devices.fail_with(HubError::PermissionDenied); + open_tab(&page, DEVICES, cx); + assert!(page.shows("devices-error", cx)); + assert!(!page.shows(item("device", "phone"), cx)); + + page.devices.recover(); + page.click("devices-retry", cx); + cx.run_until_parked(); + page.render(cx); + + assert!(!page.shows("devices-error", cx)); + assert!(page.shows(item("device", "phone"), cx)); +} + +#[gpui_kit::test] +fn revoking_a_device_asks_first_and_keeping_it_changes_nothing(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + page.click(item("device-revoke", "phone"), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx), "revoking asks first"); + assert!(page.devices.revoked().is_empty()); + page.click("revoke-device-keep", cx); + cx.run_until_parked(); + + assert!(page.devices.revoked().is_empty()); + assert!(page.shows(item("device", "phone"), cx)); +} + +#[gpui_kit::test] +fn confirming_revokes_the_device_and_the_list_is_read_again(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + page.click(item("device-revoke", "phone"), cx); + page.settle_dialog(cx); + page.click("revoke-device-confirm", cx); + cx.run_until_parked(); + page.render(cx); + + assert_eq!(page.devices.revoked(), vec![(hub_id(), "phone".to_owned())]); + assert!(!page.shows(item("device", "phone"), cx)); + assert!(page.shows(item("device", "laptop"), cx)); +} + +#[gpui_kit::test] +fn revoking_this_device_signs_out_of_the_server(cx: &mut TestAppContext) { + let page = page_with_devices(cx); + open_tab(&page, DEVICES, cx); + + page.click(item("device-revoke", "laptop"), cx); + page.settle_dialog(cx); + page.click("revoke-device-confirm", cx); + cx.run_until_parked(); + page.render(cx); + + assert!( + page.devices.revoked().is_empty(), + "the sign-out path ends the session" + ); + assert_eq!(page.hub.count(FakeOp::SignOut), 1); + assert_eq!(servers(&page, cx)[0].status, HubStatus::SignedOut); + assert!(page.shows(item("server-tab-signed-out", KEY), cx)); +} + +#[gpui_kit::test] +fn a_server_that_is_not_signed_in_offers_signing_in_instead_of_devices_and_keys( + cx: &mut TestAppContext, +) { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + let page = page_over(cx, config, vec![pinned_record()], None); + + open_tab(&page, DEVICES, cx); + assert!(page.shows(item("server-tab-signed-out", KEY), cx)); + assert!(page.shows(item("server-tab-sign-in", KEY), cx)); + assert!(!page.shows("devices-tab", cx)); + + open_tab(&page, KEYS, cx); + assert!(page.shows(item("server-tab-signed-out", KEY), cx)); + assert!(!page.shows(item("server-trust-tab", KEY), cx)); + + page.click(item("server-tab-sign-in", KEY), cx); + page.settle_dialog(cx); + assert!(page.has_dialog(cx), "the sign-in dialog opens"); +} + +/// The Keys tab of a server whose account keys are closed. +fn locked_keys(cx: &mut TestAppContext) -> Fixture { + let page = page(cx, vec![pinned_record()]); + page.hub.lock_keys(&hub_id()); + open_tab(&page, KEYS, cx); + page +} + +fn submit_password(page: &Fixture, password: &str, cx: &mut TestAppContext) { + page.type_text(password, cx); + page.click("account-keys-unlock", cx); + cx.run_until_parked(); + page.render(cx); +} + +#[gpui_kit::test] +fn the_right_password_opens_the_account_keys(cx: &mut TestAppContext) { + let page = locked_keys(cx); + assert!(page.shows("account-keys-locked", cx)); + + submit_password(&page, crate::test_support::PASSWORD, cx); + + assert!(page.shows("account-keys-open", cx)); + assert!(!page.shows("account-keys-locked", cx)); + assert_eq!(page.account_keys.attempts(), vec![true]); +} + +#[gpui_kit::test] +fn a_wrong_password_is_refused_and_the_account_keys_stay_closed(cx: &mut TestAppContext) { + let page = locked_keys(cx); + + submit_password(&page, "not it", cx); + + assert!(page.shows("account-keys-refused", cx)); + assert!(page.shows("account-keys-locked", cx)); + assert_eq!(page.account_keys.attempts(), vec![false]); +} diff --git a/crates/hubs-ui/src/sign_in.rs b/crates/hubs-ui/src/sign_in.rs new file mode 100644 index 00000000..4fa92240 --- /dev/null +++ b/crates/hubs-ui/src/sign_in.rs @@ -0,0 +1,786 @@ +//! The dialog that signs in to a server: address and fingerprint, the trust decision, then a +//! password or the browser. The order of the steps belongs to [`SignInSession`]; this view +//! shows the current step and passes the user's choices on. + +mod form; + +use gpui_kit::{ + App, AppContext as _, ClickEvent, Context, Entity, FocusHandle, Focusable, Subscription, + Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + h_flex, + input::Input, + v_flex, + }, + div, + prelude::*, + px, +}; +use nocterm_hubs::{Hubs, SignInError, SignInEvent, SignInSession, SignInStep, TrustNote}; +use nocterm_hubs_model::{HubId, OidcProvider}; +use nocterm_session::Secret; + +use crate::layout::{field, fingerprint_block, note, problem, title}; +use crate::messages::sign_in_problem; +use crate::switch::Switch; +use form::SignInForm; + +/// Starts signing in to a server not known yet, or again to `hub`, and shows the dialog. A hub +/// that is not known any more shows nothing. With a `switch`, finishing the sign-in opens the +/// server's window. +pub(crate) fn start( + hubs: &Entity, + hub: Option<&HubId>, + switch: Option, + window: &mut Window, + cx: &mut App, +) { + let session = hubs.update(cx, |hubs, cx| match hub { + Some(hub) => hubs.start_sign_in_for(hub, cx), + None => Some(hubs.start_sign_in(cx)), + }); + if let Some(session) = session { + open(session, switch, window, cx); + } +} + +/// Signs in again to the known server `hub` from its own window, such as when this device +/// holds no keys for its account. Nothing is switched: the window stays where it is. +pub fn sign_in_again(hubs: &Entity, hub: &HubId, window: &mut Window, cx: &mut App) { + start(hubs, Some(hub), None, window, cx); +} + +/// Shows the dialog that issues a new recovery key for the listed, signed-in server `hub`. +pub(crate) fn start_reissue( + hubs: &Entity, + hub: &HubId, + offer: nocterm_hubs_model::RecoveryKeyReissue, + window: &mut Window, + cx: &mut App, +) { + let session = hubs.update(cx, |hubs, cx| { + hubs.start_recovery_key_reissue(hub, offer, cx) + }); + if let Some(session) = session { + open(session, None, window, cx); + } +} + +/// Opens the dialog over `session`. Closing the dialog ends the sign-in: a browser sign-in +/// in flight is cancelled with the session. While a recovery key is being issued or shown the +/// dialog cannot be closed (button, backdrop or Escape): the key exists on the server and is +/// shown only here. +pub(crate) fn open( + session: Entity, + switch: Option, + window: &mut Window, + cx: &mut App, +) { + let dialog = cx.new(|cx| SignInDialog::new(session, switch, window, cx)); + let first = dialog.clone(); + window.open_dialog(cx, move |shell, _, cx| { + let submit = dialog.clone(); + let guard = dialog.downgrade(); + let (held, title) = { + let dialog = dialog.read(cx); + (dialog.holds_the_key(cx), dialog.title(cx)) + }; + shell + .title(title) + .w(px(480.)) + .close_button(!held) + .overlay_closable(!held) + .keyboard(!held) + .on_cancel(move |_, _, cx| { + guard + .upgrade() + .is_none_or(|dialog| !dialog.read(cx).holds_the_key(cx)) + }) + // Enter reaches the dialog as the confirm action. The step decides what it means + // and the dialog closes itself when it is done. + .on_ok(move |_, window, cx| { + submit.update(cx, |dialog, cx| dialog.primary(window, cx)); + false + }) + .footer(div()) + .child(dialog.clone()) + }); + first.update(cx, |dialog, cx| dialog.follow_step(window, cx)); +} + +struct SignInDialog { + session: Entity, + /// Opens the server's window once the sign-in is finished. + switch: Option, + form: SignInForm, + focus: FocusHandle, + /// The kind of step the focus was last placed for. + placed: Option>, + _subscriptions: Vec, +} + +impl SignInDialog { + fn new( + session: Entity, + switch: Option, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let form = SignInForm::new(window, cx); + let subscriptions = vec![ + cx.observe_in(&session, window, |this, _, window, cx| { + this.follow_step(window, cx); + }), + cx.subscribe(&session, |_, _, event, cx| { + if let SignInEvent::OpenBrowser(url) = event { + cx.open_url(url); + } + }), + ]; + Self { + session, + switch, + form, + focus: cx.focus_handle(), + placed: None, + _subscriptions: subscriptions, + } + } + + /// Puts the cursor where the step asks for input when the step changes. + fn follow_step(&mut self, window: &mut Window, cx: &mut Context) { + let step = self.step(cx); + let kind = std::mem::discriminant(&step); + if self.placed != Some(kind) { + self.placed = Some(kind); + let target = match step { + SignInStep::Address { .. } if self.session.read(cx).address_editable() => { + self.form.address().read(cx).focus_handle(cx) + } + SignInStep::Method { password: true, .. } => { + self.form.username().read(cx).focus_handle(cx) + } + SignInStep::Passphrase { .. } => self.form.passphrase().read(cx).focus_handle(cx), + SignInStep::Recovery { .. } => self.form.recovery_key().read(cx).focus_handle(cx), + _ => self.focus.clone(), + }; + window.focus(&target, cx); + } + cx.notify(); + } + + fn step(&self, cx: &App) -> SignInStep { + self.session.read(cx).step().clone() + } + + /// A recovery key is being issued or shown: leaving now would lose it. + fn holds_the_key(&self, cx: &App) -> bool { + matches!( + self.session.read(cx).step(), + SignInStep::RecoveryKey { .. } | SignInStep::Issuing + ) + } + + fn title(&self, cx: &App) -> &'static str { + if self.session.read(cx).reissue_only() { + "Recovery key" + } else { + "Sign in to a server" + } + } + + /// What Enter does on this step. Never a choice that cannot be taken back: on the trust + /// step it goes back. + fn primary(&mut self, window: &mut Window, cx: &mut Context) { + match self.step(cx) { + SignInStep::Address { .. } if self.session.read(cx).address_editable() => { + self.submit_address(cx); + } + SignInStep::Address { .. } | SignInStep::Failed(_) => self.close(window, cx), + SignInStep::ConfirmFingerprint { .. } | SignInStep::Mismatch { .. } => { + self.session.update(cx, |session, cx| session.back(cx)); + } + SignInStep::Method { password: true, .. } => self.sign_in_with_password(window, cx), + SignInStep::Finished(hub) => self.finish(&hub, window, cx), + SignInStep::Passphrase { .. } => self.set_vault_passphrase(window, cx), + SignInStep::Recovery { .. } => self.recover(window, cx), + // Enter must not skip past the one showing of the recovery key, nor replace a key + // the person may still hold. + SignInStep::RecoveryKey { .. } + | SignInStep::RecoveryOffer { .. } + | SignInStep::Issuing + | SignInStep::Method { .. } + | SignInStep::Checking + | SignInStep::Authenticating + | SignInStep::Browser { .. } + | SignInStep::Saving => {} + } + } + + fn close(&self, window: &mut Window, cx: &mut Context) { + window.close_dialog(cx); + } + + /// Closes the dialog and shows the server that was signed in to, when the dialog was asked to. + fn finish(&self, hub: &HubId, window: &mut Window, cx: &mut Context) { + self.close(window, cx); + if let Some(switch) = &self.switch { + switch.open_signed_in(hub, window, cx); + } + } + + fn submit_address(&mut self, cx: &mut Context) { + let address = self.form.address_text(cx); + let fingerprint = self.form.fingerprint_text(cx); + self.session.update(cx, |session, cx| { + session.submit_address(&address, &fingerprint, cx) + }); + } + + fn sign_in_with_password(&mut self, window: &mut Window, cx: &mut Context) { + let username = self.form.username_text(cx); + let password = self.form.take_password(window, cx); + self.session.update(cx, |session, cx| { + session.sign_in_with_password(&username, password, cx); + }); + } + + fn set_vault_passphrase(&mut self, window: &mut Window, cx: &mut Context) { + let passphrase = self.form.take_passphrase(window, cx); + self.session.update(cx, |session, cx| { + session.set_vault_passphrase(passphrase, cx); + }); + } + + fn recover(&mut self, window: &mut Window, cx: &mut Context) { + let recovery_key = self.form.take_recovery_key(window, cx); + self.session.update(cx, |session, cx| { + session.recover(recovery_key, cx); + }); + } + + fn sign_in_with_provider(&mut self, provider: &str, cx: &mut Context) { + self.session.update(cx, |session, cx| { + session.sign_in_with_provider(provider, cx) + }); + } + + fn render_step(&mut self, step: SignInStep, cx: &mut Context) -> impl IntoElement { + match step { + SignInStep::Address { problem } => self.render_address(problem.as_ref(), cx), + SignInStep::Checking => self.render_busy("Checking the server…", true, cx), + SignInStep::Authenticating => self.render_busy("Signing in…", false, cx), + SignInStep::Saving => self.render_busy("Saving the server…", false, cx), + SignInStep::ConfirmFingerprint { + address, + fingerprint, + } => self.render_confirm(&address.to_string(), &fingerprint, cx), + SignInStep::Mismatch { typed, presented } => { + self.render_mismatch(&typed, &presented, cx) + } + SignInStep::Method { + hub_name, + password, + providers, + note, + problem, + } => self.render_method(&hub_name, password, &providers, note, problem.as_ref(), cx), + SignInStep::Browser { provider, url } => self.render_browser(&provider, url, cx), + SignInStep::RecoveryKey { key } => self.render_recovery_key(key.key(), cx), + SignInStep::RecoveryOffer { problem, .. } => { + self.render_recovery_offer(problem.as_ref(), cx) + } + SignInStep::Issuing => v_flex() + .gap_3() + .child(note("Issuing a new recovery key…", cx)) + .into_any_element(), + SignInStep::Passphrase { problem } => self.render_passphrase(problem.as_ref(), cx), + SignInStep::Recovery { problem } => self.render_recovery(problem.as_ref(), cx), + SignInStep::Finished(hub) => self.render_finished(hub, cx), + SignInStep::Failed(error) => self.render_failed(&error, cx), + } + } + + fn render_address( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let editable = self.session.read(cx).address_editable(); + v_flex() + .gap_3() + .when(editable, |step| { + step.child(field("Server", Input::new(self.form.address()))) + .child(field( + "Certificate fingerprint", + Input::new(self.form.fingerprint()), + )) + .child(note( + "Leave the fingerprint empty to see the certificate first. A server with a certificate from a public authority needs none.", + cx, + )) + }) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-cancel") + .label("Cancel") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.close(window, cx); + })), + ) + .when(editable, |buttons| { + buttons.child( + Button::new("sign-in-continue") + .primary() + .label("Continue") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.submit_address(cx); + })), + ) + }), + ) + .into_any_element() + } + + fn render_busy( + &mut self, + what: &'static str, + cancels_dialog: bool, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(note(what, cx)) + .child( + h_flex() + .justify_end() + .child( + Button::new("sign-in-stop") + .label("Cancel") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + if cancels_dialog { + this.close(window, cx); + } else { + this.session.update(cx, |session, cx| session.back(cx)); + } + })), + ), + ) + .into_any_element() + } + + fn render_confirm( + &mut self, + address: &str, + fingerprint: &nocterm_hubs_model::Fingerprint, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(title(format!("Trust {address}?"), cx)) + .child(note( + "This server's certificate is not from a public authority. Compare its fingerprint with the one shown by the server, then trust and pin it.", + cx, + )) + .child(fingerprint_block("Fingerprint", fingerprint, cx)) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-back") + .primary() + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ) + .child( + Button::new("sign-in-trust") + .label("Trust and pin this certificate") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session + .update(cx, |session, cx| session.confirm_fingerprint(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_mismatch( + &mut self, + typed: &nocterm_hubs_model::Fingerprint, + presented: &nocterm_hubs_model::Fingerprint, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(problem( + "The server's certificate is not the one you typed. Do not continue unless you know why it changed." + .to_owned(), + cx, + )) + .child(fingerprint_block("You typed", typed, cx)) + .child(fingerprint_block("The server presented", presented, cx)) + .child( + h_flex().justify_end().child( + Button::new("sign-in-back") + .primary() + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_method( + &mut self, + hub_name: &str, + password: bool, + providers: &[OidcProvider], + trust: TrustNote, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let editable = self.session.read(cx).address_editable(); + let trust = match trust { + TrustNote::Pinned => "The certificate matches the fingerprint.", + TrustNote::PublicCa => "The certificate is vouched for by a public authority.", + TrustNote::Confirmed => "The certificate was pinned after you confirmed it.", + }; + v_flex() + .gap_3() + .child(title(format!("Sign in to {hub_name}"), cx)) + .child(note(trust, cx)) + .when(password, |step| { + step.child(field("User name", Input::new(self.form.username()))) + .child(field("Password", Input::new(self.form.password()))) + }) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child(v_flex().gap_2().children(providers.iter().map(|provider| { + let id = provider.id.clone(); + Button::new(provider_key(&provider.id)) + .w_full() + .label(format!("Continue with {}", provider.display_name)) + .on_click(cx.listener(move |this, _: &ClickEvent, _, cx| { + this.sign_in_with_provider(&id, cx); + })) + }))) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-cancel") + .label(if editable { "Back" } else { "Cancel" }) + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + if editable { + this.session.update(cx, |session, cx| session.back(cx)); + } else { + this.close(window, cx); + } + })), + ) + .when(password, |buttons| { + buttons.child( + Button::new("sign-in-password") + .primary() + .label("Sign in") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.sign_in_with_password(window, cx); + })), + ) + }), + ) + .into_any_element() + } + + fn render_browser( + &mut self, + provider: &str, + url: Option, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(note( + format!("Finish signing in with {provider} in your browser. This window continues by itself."), + cx, + )) + .when_some(url.clone(), |step, url| step.child(note(url, cx))) + .child( + h_flex() + .gap_2() + .justify_end() + .when_some(url, |buttons, url| { + buttons.child(Button::new("sign-in-copy-link").label("Copy link").on_click( + move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + cx.write_to_clipboard(gpui_kit::ClipboardItem::new_string( + url.clone(), + )); + }, + )) + }) + .child( + Button::new("sign-in-stop") + .label("Cancel") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_recovery_key( + &mut self, + key: &Secret, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let text = key.expose().to_owned(); + v_flex() + .gap_3() + .child(title("Write down your recovery key".to_owned(), cx)) + .child(note( + "Your account keys were created. This key is the only way to open them if you lose your password, and it is not shown again or kept on this device.", + cx, + )) + .child(note(text.clone(), cx)) + .child( + h_flex() + .gap_2() + .justify_end() + .child(Button::new("sign-in-copy-key").label("Copy key").on_click( + move |_: &ClickEvent, _: &mut Window, cx: &mut App| { + cx.write_to_clipboard(gpui_kit::ClipboardItem::new_string( + text.clone(), + )); + }, + )) + .child( + Button::new("sign-in-key-saved") + .primary() + .label("I have saved it") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| { + session.acknowledge_recovery_key(cx); + }); + })), + ), + ) + .into_any_element() + } + + fn render_recovery_offer( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + let standalone = self.session.read(cx).reissue_only(); + v_flex() + .gap_3() + .child(title("Your recovery key was not confirmed".to_owned(), cx)) + .child(note( + "The recovery key made with your account keys was never confirmed as saved, so it may be lost. Issue a new one now: the old key stops working, and the new one is shown once.", + cx, + )) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-offer-later") + .label("Not now") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + if standalone { + this.close(window, cx); + } else { + this.session + .update(cx, |session, cx| session.skip_recovery_offer(cx)); + } + })), + ) + .child( + Button::new("sign-in-offer-issue") + .primary() + .label("Issue a new recovery key") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session + .update(cx, |session, cx| session.issue_recovery_key(cx)); + })), + ), + ) + .into_any_element() + } + + fn render_passphrase( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(title("Vault passphrase".to_owned(), cx)) + .child(note( + "Signing in through a provider does not open your account keys. Type the passphrase that protects them; the first time, this sets it.", + cx, + )) + .child(field("Passphrase", Input::new(self.form.passphrase()))) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-passphrase-back") + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ) + .child( + Button::new("sign-in-passphrase") + .primary() + .label("Continue") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.set_vault_passphrase(window, cx); + })), + ), + ) + .into_any_element() + } + + fn render_recovery( + &mut self, + failure: Option<&SignInError>, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(title("Enter your recovery key".to_owned(), cx)) + .child(note( + "This password does not open your account keys; it was probably reset. The recovery key you saved when the account was created opens them, and this password opens them from now on.", + cx, + )) + .child(field("Recovery key", Input::new(self.form.recovery_key()))) + .when_some(failure, |step, error| { + step.child(problem(sign_in_problem(error), cx)) + }) + .child( + h_flex() + .gap_2() + .justify_end() + .child( + Button::new("sign-in-recovery-back") + .label("Back") + .on_click(cx.listener(|this, _: &ClickEvent, _, cx| { + this.session.update(cx, |session, cx| session.back(cx)); + })), + ) + .child( + Button::new("sign-in-recovery") + .primary() + .label("Recover") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.recover(window, cx); + })), + ), + ) + .into_any_element() + } + + fn render_finished(&mut self, hub: HubId, cx: &mut Context) -> gpui_kit::AnyElement { + let (heading, text) = if self.session.read(cx).reissue_only() { + ( + "Recovery key saved", + "The new recovery key opens your account keys. The old one no longer does.", + ) + } else { + let text = if self.switch.is_some() { + "The server is saved. Its connections and vault open in its own window." + } else { + "The server is saved." + }; + ("Signed in", text) + }; + v_flex() + .gap_3() + .child(title(heading.to_owned(), cx)) + .child(note(text, cx)) + .child( + h_flex().justify_end().child( + Button::new("sign-in-done") + .primary() + .label("Done") + .on_click(cx.listener(move |this, _: &ClickEvent, window, cx| { + this.finish(&hub, window, cx); + })), + ), + ) + .into_any_element() + } + + fn render_failed( + &mut self, + error: &SignInError, + cx: &mut Context, + ) -> gpui_kit::AnyElement { + v_flex() + .gap_3() + .child(problem(sign_in_problem(error), cx)) + .child( + h_flex().justify_end().child( + Button::new("sign-in-close") + .primary() + .label("Close") + .on_click(cx.listener(|this, _: &ClickEvent, window, cx| { + this.close(window, cx); + })), + ), + ) + .into_any_element() + } +} + +/// A stable element id part for a provider. +fn provider_key(id: &str) -> gpui_kit::SharedString { + format!("sign-in-provider-{id}").into() +} + +impl Focusable for SignInDialog { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +impl Render for SignInDialog { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let step = self.step(cx); + v_flex() + .id("sign-in-dialog") + .track_focus(&self.focus) + .w_full() + .child(self.render_step(step, cx)) + } +} + +#[cfg(test)] +#[path = "sign_in_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/sign_in/form.rs b/crates/hubs-ui/src/sign_in/form.rs new file mode 100644 index 00000000..64fb3c49 --- /dev/null +++ b/crates/hubs-ui/src/sign_in/form.rs @@ -0,0 +1,111 @@ +//! The text fields of the sign-in dialog. + +use gpui_kit::{AppContext as _, Context, Entity, Window, component::input::InputState}; +use nocterm_session::Secret; + +/// The address, fingerprint, user name, password, vault passphrase and recovery key fields. A +/// secret is read out of its field once and the field is emptied, so a failed attempt starts +/// from a blank one. +pub(crate) struct SignInForm { + address: Entity, + fingerprint: Entity, + username: Entity, + password: Entity, + passphrase: Entity, + recovery_key: Entity, +} + +impl SignInForm { + pub(crate) fn new(window: &mut Window, cx: &mut Context) -> Self { + Self { + address: cx.new(|cx| InputState::new(window, cx).placeholder("hub.example.com:7443")), + fingerprint: cx + .new(|cx| InputState::new(window, cx).placeholder("SHA-256 fingerprint, optional")), + username: cx.new(|cx| InputState::new(window, cx).placeholder("User name")), + password: cx.new(|cx| { + InputState::new(window, cx) + .masked(true) + .placeholder("Password") + }), + passphrase: cx.new(|cx| { + InputState::new(window, cx) + .masked(true) + .placeholder("Vault passphrase") + }), + recovery_key: cx.new(|cx| InputState::new(window, cx).placeholder("Recovery key")), + } + } + + pub(crate) fn address(&self) -> &Entity { + &self.address + } + + pub(crate) fn fingerprint(&self) -> &Entity { + &self.fingerprint + } + + pub(crate) fn username(&self) -> &Entity { + &self.username + } + + pub(crate) fn password(&self) -> &Entity { + &self.password + } + + pub(crate) fn passphrase(&self) -> &Entity { + &self.passphrase + } + + pub(crate) fn recovery_key(&self) -> &Entity { + &self.recovery_key + } + + pub(crate) fn address_text(&self, cx: &Context) -> String { + self.address.read(cx).value().to_string() + } + + pub(crate) fn fingerprint_text(&self, cx: &Context) -> String { + self.fingerprint.read(cx).value().to_string() + } + + pub(crate) fn username_text(&self, cx: &Context) -> String { + self.username.read(cx).value().to_string() + } + + /// The typed password; the field is emptied. + pub(crate) fn take_password( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Secret { + take(&self.password, window, cx) + } + + /// The typed vault passphrase; the field is emptied. + pub(crate) fn take_passphrase( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Secret { + take(&self.passphrase, window, cx) + } + + /// The typed recovery key; the field is emptied. + pub(crate) fn take_recovery_key( + &self, + window: &mut Window, + cx: &mut Context, + ) -> Secret { + take(&self.recovery_key, window, cx) + } +} + +fn take( + field: &Entity, + window: &mut Window, + cx: &mut Context, +) -> Secret { + let secret = Secret::new(field.read(cx).value().to_string()); + field.update(cx, |input, cx| input.set_value("", window, cx)); + secret +} diff --git a/crates/hubs-ui/src/sign_in_tests.rs b/crates/hubs-ui/src/sign_in_tests.rs new file mode 100644 index 00000000..db3e9938 --- /dev/null +++ b/crates/hubs-ui/src/sign_in_tests.rs @@ -0,0 +1,437 @@ +//! Signing in to a server through the dialog, step by step, over a fake hub. +use crate::Placement; +use crate::SignInToServer; +use crate::test_support::{Fixture, fixture, server}; +use gpui_kit::TestAppContext; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp, FakeServer}; +use nocterm_hubs_model::{Environment, Fingerprint, HubError}; + +fn dialog_over(server: FakeServer, cx: &mut TestAppContext) -> Fixture { + dialog_over_tabs(server, false, cx) +} + +fn dialog_over_tabs(server: FakeServer, tabs: bool, cx: &mut TestAppContext) -> Fixture { + let window = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server), + Vec::new(), + None, + ); + if tabs { + window.add_tab(cx); + } + window.dispatch(SignInToServer, cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + window +} + +/// Types the address (and a fingerprint) and moves on. +fn submit( + window: &Fixture, + address: &str, + fingerprint: Option<&Fingerprint>, + cx: &mut TestAppContext, +) { + window.type_text(address, cx); + if let Some(fingerprint) = fingerprint { + window.press("tab", cx); + window.type_text(&fingerprint.to_string(), cx); + } + window.click("sign-in-continue", cx); + cx.run_until_parked(); + window.render(cx); +} + +fn sign_in_with_password(window: &Fixture, user: &str, password: &str, cx: &mut TestAppContext) { + window.type_text(user, cx); + window.press("tab", cx); + window.type_text(password, cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); +} + +fn saved(window: &Fixture, cx: &mut TestAppContext) -> usize { + cx.update(|cx| window.installed.hubs().read(cx).list().len()) +} + +#[gpui_kit::test] +fn a_matching_fingerprint_goes_straight_to_the_password(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit( + &window, + "hub.home:7443", + Some(&Fingerprint::from_bytes([7; 32])), + cx, + ); + assert!(window.shows("sign-in-password", cx)); + assert!(!window.shows("sign-in-trust", cx)); +} + +#[gpui_kit::test] +fn a_fingerprint_that_does_not_match_offers_only_going_back(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit( + &window, + "hub.home:7443", + Some(&Fingerprint::from_bytes([8; 32])), + cx, + ); + assert!(window.shows("sign-in-back", cx)); + assert!(!window.shows("sign-in-trust", cx)); + assert!(!window.shows("sign-in-password", cx)); + assert!(!window.shows("sign-in-continue", cx)); + assert_eq!( + window.hub.count(FakeOp::Connect), + 0, + "the hub is not contacted again" + ); + assert_eq!(saved(&window, cx), 0); +} + +#[gpui_kit::test] +fn a_self_signed_server_asks_to_trust_and_pin_its_certificate(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-trust", cx)); + assert!(window.shows("sign-in-back", cx)); + window.click("sign-in-trust", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-password", cx)); +} + +#[gpui_kit::test] +fn enter_on_the_trust_step_goes_back_instead_of_trusting(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit(&window, "hub.home:7443", None, cx); + window.press("enter", cx); + assert!(window.shows("sign-in-continue", cx), "back at the address"); + assert!(!window.shows("sign-in-password", cx)); + assert_eq!(window.hub.count(FakeOp::Connect), 0); +} + +#[gpui_kit::test] +fn a_certificate_from_a_public_authority_needs_no_decision(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-password", cx)); + assert!(!window.shows("sign-in-trust", cx)); +} + +#[gpui_kit::test] +fn a_wrong_password_empties_the_field_and_the_right_one_signs_in(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "wrong", cx); + assert!(window.shows("sign-in-password", cx), "back at the methods"); + assert!(!window.shows("sign-in-done", cx)); + // Had the wrong password stayed in its field, this would be "wrongsecret". + window.press("tab", cx); + window.type_text("secret", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1, "the server is saved"); + let attempts = window + .hub + .calls() + .iter() + .filter(|call| matches!(call, FakeCall::Password { .. })) + .count(); + assert_eq!(attempts, 2); +} + +#[gpui_kit::test] +fn done_closes_the_dialog_and_the_server_is_listed(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + window.click("sign-in-done", cx); + assert!(!window.has_dialog(cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn finishing_a_sign_in_from_an_empty_window_opens_the_server_in_it(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + window.click("sign-in-done", cx); + assert_eq!( + window.environments.requests(), + vec![( + Environment::Hub(crate::test_support::hub_id()), + Placement::ThisWindow + )] + ); +} + +#[gpui_kit::test] +fn finishing_a_sign_in_from_a_window_with_tabs_opens_the_server_beside_it(cx: &mut TestAppContext) { + let window = dialog_over_tabs(server().publicly_trusted(), true, cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + window.click("sign-in-done", cx); + assert_eq!( + window.environments.requests(), + vec![( + Environment::Hub(crate::test_support::hub_id()), + Placement::NewWindow + )] + ); +} + +#[gpui_kit::test] +fn browser_sign_in_opens_the_provider_page_and_finishes_when_the_browser_does( + cx: &mut TestAppContext, +) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.render(cx); + assert_eq!( + cx.opened_url().as_deref(), + Some("https://idp.fake/authorize?provider=corp") + ); + assert!(window.shows("sign-in-copy-link", cx)); + window.hub.complete_oidc("ada"); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); +} + +#[gpui_kit::test] +fn cancelling_the_browser_sign_in_stops_waiting_for_it(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.render(cx); + window.click("sign-in-stop", cx); + cx.run_until_parked(); + assert_eq!(window.hub.cancelled_oidc(), 1); + assert!(window.shows("sign-in-password", cx), "back at the methods"); + assert_eq!(saved(&window, cx), 0); +} + +#[gpui_kit::test] +fn closing_the_dialog_during_a_browser_sign_in_cancels_it(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.render(cx); + window.press("escape", cx); + cx.run_until_parked(); + assert!(!window.has_dialog(cx)); + assert_eq!(window.hub.cancelled_oidc(), 1); +} + +#[gpui_kit::test] +fn a_server_that_speaks_another_protocol_is_refused_at_the_address(cx: &mut TestAppContext) { + let mut outdated = server().publicly_trusted(); + outdated.protocol = 9; + let window = dialog_over(outdated, cx); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-continue", cx), "still at the address"); + assert!(!window.shows("sign-in-password", cx)); + assert_eq!(saved(&window, cx), 0); +} + +#[gpui_kit::test] +fn a_server_that_does_not_answer_leaves_the_address_to_correct(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + window.hub.fail_next( + FakeOp::Probe, + HubError::Unreachable { + address: nocterm_hubs_model::HubAddress::parse("hub.home:7443").unwrap(), + cause: nocterm_core::error::SharedCause::new(std::io::Error::from( + std::io::ErrorKind::ConnectionRefused, + )), + }, + ); + submit(&window, "hub.home:7443", None, cx); + assert!(window.shows("sign-in-continue", cx)); +} + +#[gpui_kit::test] +fn the_address_is_validated_before_the_hub_is_asked(cx: &mut TestAppContext) { + let window = dialog_over(server(), cx); + submit(&window, "http://hub.home", None, cx); + assert!(window.shows("sign-in-continue", cx)); + assert_eq!(window.hub.count(FakeOp::Probe), 0); +} + +#[gpui_kit::test] +fn a_new_account_shows_the_recovery_key_and_lists_the_server_once_it_is_saved( + cx: &mut TestAppContext, +) { + let window = dialog_over(server().publicly_trusted().keyed(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert_eq!(saved(&window, cx), 0, "not listed before the key is saved"); + + window.press("enter", cx); + window.render(cx); + assert!( + window.shows("sign-in-key-saved", cx), + "Enter does not skip the one showing of the key" + ); + + window.click("sign-in-key-saved", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn a_reset_password_asks_for_the_recovery_key_and_a_wrong_one_is_reported(cx: &mut TestAppContext) { + let window = dialog_over( + server() + .publicly_trusted() + .with_keyset("ada", "old", "recovery-key"), + cx, + ); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + assert!(window.shows("sign-in-recovery", cx)); + + window.type_text("not it", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-recovery", cx)); + assert_eq!(saved(&window, cx), 0); + + window.type_text("recovery-key", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn a_provider_sign_in_asks_for_the_vault_passphrase(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted().keyed(), cx); + submit(&window, "hub.home:7443", None, cx); + window.click("sign-in-provider-corp", cx); + cx.run_until_parked(); + window.hub.complete_oidc("ada"); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-passphrase", cx)); + + window.type_text("a long passphrase", cx); + window.press("enter", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert_eq!(window.hub.count(FakeOp::SetVaultPassphrase), 1); +} + +#[gpui_kit::test] +fn a_new_recovery_key_cannot_be_left_behind_by_closing_the_dialog(cx: &mut TestAppContext) { + let window = dialog_over(server().publicly_trusted().keyed(), cx); + submit(&window, "hub.home:7443", None, cx); + sign_in_with_password(&window, "ada", "secret", cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert!( + !window.shows("close", cx), + "the close button is not offered" + ); + + window.press("escape", cx); + cx.run_until_parked(); + window.render(cx); + + assert!(window.has_dialog(cx)); + assert!( + window.shows("sign-in-key-saved", cx), + "Escape does not close it" + ); + + window.click("sign-in-key-saved", cx); + cx.run_until_parked(); + window.render(cx); + window.press("escape", cx); + cx.run_until_parked(); + window.render(cx); + assert!( + !window.has_dialog(cx), + "once the key is saved Escape closes it" + ); +} + +/// A dialog over a server this device is signed in to whose recovery key was never confirmed. +fn unconfirmed_key_dialog(cx: &mut TestAppContext) -> Fixture { + let hub = FakeHub::new().with_server(server().publicly_trusted().with_keyset( + "ada", + "secret", + "recovery-ada", + )); + let id = crate::test_support::hub_id(); + hub.leave_recovery_key_unconfirmed(&id); + let window = fixture(cx, Environment::Local, hub, Vec::new(), Some("ada")); + window.dispatch(SignInToServer, cx); + window.settle_dialog(cx); + submit(&window, "hub.home:7443", None, cx); + window +} + +#[gpui_kit::test] +fn an_unconfirmed_recovery_key_is_offered_again_and_the_new_one_must_be_saved( + cx: &mut TestAppContext, +) { + let window = unconfirmed_key_dialog(cx); + assert!(window.shows("sign-in-offer-issue", cx)); + assert_eq!(saved(&window, cx), 0); + + window.click("sign-in-offer-issue", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-key-saved", cx)); + assert_eq!(window.hub.count(FakeOp::ReissueRecoveryKey), 1); + assert!(!window.shows("close", cx), "the new key is shown once"); + + window.click("sign-in-key-saved", cx); + cx.run_until_parked(); + window.render(cx); + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); +} + +#[gpui_kit::test] +fn the_offer_of_a_new_recovery_key_can_wait(cx: &mut TestAppContext) { + let window = unconfirmed_key_dialog(cx); + + window.click("sign-in-offer-later", cx); + cx.run_until_parked(); + window.render(cx); + + assert!(window.shows("sign-in-done", cx)); + assert_eq!(saved(&window, cx), 1); + assert_eq!(window.hub.count(FakeOp::ReissueRecoveryKey), 0); +} + +#[gpui_kit::test] +fn a_failed_reissue_says_why_and_offers_it_again(cx: &mut TestAppContext) { + let window = unconfirmed_key_dialog(cx); + window + .hub + .fail_next(FakeOp::ReissueRecoveryKey, HubError::KeysLocked); + + window.click("sign-in-offer-issue", cx); + cx.run_until_parked(); + window.render(cx); + + assert!(window.shows("sign-in-offer-issue", cx)); + assert!(!window.shows("sign-in-key-saved", cx)); +} diff --git a/crates/hubs-ui/src/status.rs b/crates/hubs-ui/src/status.rs new file mode 100644 index 00000000..47e6c1d4 --- /dev/null +++ b/crates/hubs-ui/src/status.rs @@ -0,0 +1,59 @@ +//! How the status of a server reads, and in what colour, wherever a server is listed. + +use gpui_kit::{App, Hsla, SharedString, component::ActiveTheme as _}; +use nocterm_hubs::HubStatus; +use nocterm_ui::IconName; + +/// The status as a short phrase with the colour it is drawn in. +pub(crate) fn status_chip(status: &HubStatus, cx: &App) -> (SharedString, Hsla) { + let theme = cx.theme(); + let now = cx.background_executor().now(); + chip( + status, + now, + theme.success, + theme.muted_foreground, + theme.warning, + theme.danger, + ) +} + +/// How a server's status reads in the list, and in what colour. +pub(crate) fn chip( + status: &HubStatus, + now: std::time::Instant, + good: Hsla, + quiet: Hsla, + warn: Hsla, + bad: Hsla, +) -> (SharedString, Hsla) { + match status { + HubStatus::Online => ("Online".into(), good), + HubStatus::Connecting => ("Connecting…".into(), quiet), + HubStatus::Offline { retry_at } => { + let seconds = retry_at.saturating_duration_since(now).as_secs_f64().ceil(); + (format!("Offline · retry in {seconds:.0} s").into(), warn) + } + HubStatus::SignedOut => ("Sign-in required".into(), warn), + HubStatus::Revoked => ("Device revoked".into(), bad), + HubStatus::TrustFailed { .. } => ("Certificate changed".into(), bad), + HubStatus::UpgradeRequired { hub, client } if hub < client => { + ("Update the server".into(), warn) + } + HubStatus::UpgradeRequired { .. } => ("Update Nocterm".into(), warn), + } +} + +pub(crate) fn status_icon(status: &HubStatus) -> IconName { + match status { + HubStatus::Online | HubStatus::Connecting | HubStatus::SignedOut => IconName::Server, + HubStatus::Offline { .. } => IconName::ServerOff, + HubStatus::Revoked | HubStatus::TrustFailed { .. } | HubStatus::UpgradeRequired { .. } => { + IconName::TriangleAlert + } + } +} + +#[cfg(test)] +#[path = "status_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/status_tests.rs b/crates/hubs-ui/src/status_tests.rs new file mode 100644 index 00000000..9777acdb --- /dev/null +++ b/crates/hubs-ui/src/status_tests.rs @@ -0,0 +1,31 @@ +//! The chip of a server that speaks another protocol says which side needs the update. + +use super::status_chip; +use crate::test_support::{fixture, server}; +use gpui_kit::TestAppContext; +use nocterm_hubs::HubStatus; +use nocterm_hubs_model::Environment; +use nocterm_hubs_model::testing::FakeHub; + +fn label(status: HubStatus, cx: &mut TestAppContext) -> String { + let _page = fixture( + cx, + Environment::Local, + FakeHub::new().with_server(server()), + Vec::new(), + None, + ); + cx.update(|cx| status_chip(&status, cx).0.to_string()) +} + +#[gpui_kit::test] +fn a_server_behind_this_nocterm_is_the_one_to_update(cx: &mut TestAppContext) { + let status = HubStatus::UpgradeRequired { hub: 1, client: 2 }; + assert_eq!(label(status, cx), "Update the server"); +} + +#[gpui_kit::test] +fn a_server_ahead_of_this_nocterm_asks_to_update_nocterm(cx: &mut TestAppContext) { + let status = HubStatus::UpgradeRequired { hub: 3, client: 2 }; + assert_eq!(label(status, cx), "Update Nocterm"); +} diff --git a/crates/hubs-ui/src/switch.rs b/crates/hubs-ui/src/switch.rs new file mode 100644 index 00000000..89865aba --- /dev/null +++ b/crates/hubs-ui/src/switch.rs @@ -0,0 +1,166 @@ +//! Moving the window from one environment to another, or opening one beside it. + +use std::rc::Rc; + +use gpui_kit::{ + App, ClickEvent, WeakEntity, Window, + component::{ + WindowExt as _, + button::{Button, ButtonVariants as _}, + dialog::DialogFooter, + }, + prelude::*, +}; +use nocterm_hubs_model::{Environment, HubId}; +use nocterm_workspace::Workspace; + +use crate::ports::{EnvironmentOpener, Placement}; + +/// Opens environments for the window it belongs to, asking first when switching would end +/// tabs that cannot be taken back. +#[derive(Clone)] +pub(crate) struct Switch { + opener: Rc, + workspace: WeakEntity, + current: Environment, +} + +impl Switch { + pub(crate) fn new( + opener: Rc, + workspace: WeakEntity, + current: Environment, + ) -> Self { + Self { + opener, + workspace, + current, + } + } + + pub(crate) fn current(&self) -> &Environment { + &self.current + } + + /// Opens `target`. Switching this window while it has tabs asks first: their sessions end + /// with the window's contents. + pub(crate) fn open( + &self, + target: Environment, + placement: Placement, + window: &mut Window, + cx: &mut App, + ) { + if placement == Placement::ThisWindow && target == self.current { + return; + } + let tabs = self.tabs(cx); + if placement == Placement::ThisWindow && tabs > 0 { + self.confirm(target, tabs, window, cx); + } else { + self.perform(&target, placement, window, cx); + } + } + + /// Shows the server just signed in to: in this window when it has no tabs to lose, else in + /// a window of its own. + pub(crate) fn open_signed_in(&self, hub: &HubId, window: &mut Window, cx: &mut App) { + let placement = if self.tabs(cx) == 0 { + Placement::ThisWindow + } else { + Placement::NewWindow + }; + self.open(Environment::Hub(hub.clone()), placement, window, cx); + } + + fn tabs(&self, cx: &App) -> usize { + self.workspace + .read_with(cx, |workspace, cx| workspace.items(cx).count()) + .unwrap_or(0) + } + + fn perform( + &self, + target: &Environment, + placement: Placement, + window: &mut Window, + cx: &mut App, + ) { + if let Err(error) = self.opener.open(target.clone(), placement, window, cx) { + nocterm_ui::notice::error( + window, + cx, + "environment-open", + "Could not open the window", + error.to_string(), + ); + } + } + + /// "N tabs are open in this window. Their sessions will end." + fn confirm(&self, target: Environment, tabs: usize, window: &mut Window, cx: &mut App) { + let switch = self.clone(); + window.open_alert_dialog(cx, move |alert, _, _| { + let button = |id: &'static str, label: &'static str, placement, close: bool| { + let (switch, target) = (switch.clone(), target.clone()); + Button::new(id).label(label).on_click( + move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + if close { + switch.perform(&target, placement, window, cx); + } + }, + ) + }; + let new_window = { + let (switch, target) = (switch.clone(), target.clone()); + Button::new("switch-new-window") + .primary() + .label("Open in a new window") + .on_click(move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + window.close_dialog(cx); + switch.perform(&target, Placement::NewWindow, window, cx); + }) + }; + let ok = { + let (switch, target) = (switch.clone(), target.clone()); + move |_: &ClickEvent, window: &mut Window, cx: &mut App| { + switch.perform(&target, Placement::NewWindow, window, cx); + true + } + }; + alert + .title("Switch this window?") + .description(format!( + "{} open in this window. Their sessions end if the window switches.", + if tabs == 1 { + "1 tab is".to_owned() + } else { + format!("{tabs} tabs are") + } + )) + // Enter takes the safe choice, which keeps the tabs. + .on_ok(ok) + .footer( + DialogFooter::new() + .child(button( + "switch-cancel", + "Cancel", + Placement::ThisWindow, + false, + )) + .child(button( + "switch-here", + "Switch and close tabs", + Placement::ThisWindow, + true, + )) + .child(new_window), + ) + }); + } +} + +#[cfg(test)] +#[path = "switch_tests.rs"] +mod tests; diff --git a/crates/hubs-ui/src/switch_tests.rs b/crates/hubs-ui/src/switch_tests.rs new file mode 100644 index 00000000..a4a36959 --- /dev/null +++ b/crates/hubs-ui/src/switch_tests.rs @@ -0,0 +1,109 @@ +//! Switching a window asks first when it would end its tabs. +use crate::test_support::{Fixture, fixture, hub_id, pinned_record, server}; +use crate::{OpenEnvironmentMenu, Placement}; +use gpui_kit::TestAppContext; +use nocterm_hubs_model::{Environment, testing::FakeHub}; + +fn window_with_a_tab(environment: Environment, cx: &mut TestAppContext) -> Fixture { + let window = fixture( + cx, + environment, + FakeHub::new().with_server(server()), + vec![pinned_record()], + Some("ada"), + ); + window.add_tab(cx); + window +} + +/// Chooses the server in the menu of a local window with a tab. +fn choose_the_server(window: &Fixture, cx: &mut TestAppContext) { + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-row", 1usize), cx); + window.settle_dialog(cx); +} + +#[gpui_kit::test] +fn a_local_window_with_tabs_asks_before_it_switches(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + assert!(window.has_dialog(cx)); + assert!(window.shows("switch-here", cx)); + assert!(window.shows("switch-new-window", cx)); + assert!(window.shows("switch-cancel", cx)); + assert!( + window.environments.requests().is_empty(), + "nothing opens before the answer" + ); +} + +#[gpui_kit::test] +fn cancelling_the_question_changes_nothing(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + window.click("switch-cancel", cx); + assert!(!window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn enter_takes_the_choice_that_keeps_the_tabs(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + window.press("enter", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn switch_and_close_tabs_replaces_this_window(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + choose_the_server(&window, cx); + window.click("switch-here", cx); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::ThisWindow)] + ); +} + +#[gpui_kit::test] +fn opening_in_a_new_window_asks_nothing(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Local, cx); + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-new-window", 1usize), cx); + assert!(!window.has_dialog(cx)); + assert_eq!( + window.environments.requests(), + vec![(Environment::Hub(hub_id()), Placement::NewWindow)] + ); +} + +#[gpui_kit::test] +fn a_hub_window_with_tabs_asks_before_it_switches_back(cx: &mut TestAppContext) { + let window = window_with_a_tab(Environment::Hub(hub_id()), cx); + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-row", 0usize), cx); + window.settle_dialog(cx); + assert!(window.has_dialog(cx)); + assert!(window.environments.requests().is_empty()); +} + +#[gpui_kit::test] +fn a_window_without_tabs_switches_without_asking(cx: &mut TestAppContext) { + let window = fixture( + cx, + Environment::Hub(hub_id()), + FakeHub::new().with_server(server()), + vec![pinned_record()], + Some("ada"), + ); + window.dispatch(OpenEnvironmentMenu, cx); + window.click(("environment-row", 0usize), cx); + assert!(!window.has_dialog(cx)); + assert_eq!( + window.environments.requests(), + vec![(Environment::Local, Placement::ThisWindow)] + ); +} diff --git a/crates/hubs-ui/src/test_support.rs b/crates/hubs-ui/src/test_support.rs new file mode 100644 index 00000000..4256a3fa --- /dev/null +++ b/crates/hubs-ui/src/test_support.rs @@ -0,0 +1,261 @@ +//! Fixtures the tests of this crate share: a window over a [`FakeHub`], and a recording stand-in +//! for the port the application implements. + +use std::cell::RefCell; +use std::rc::Rc; +use std::sync::Arc; + +use gpui_kit::{ + AnyWindowHandle, AppContext as _, ElementId, Entity, Focusable as _, TestAppContext, + WindowOptions, component::WindowExt as _, test::TestWindowExt as _, +}; +use nocterm_hubs_model::testing::{FakeAccountDevices, FakeAccountKeys, FakeHub, FakeServer}; +use nocterm_hubs_model::{Environment, HubAddress, HubId, HubRecord, HubsSettings}; +use nocterm_settings::SettingsDocument; +use nocterm_store::SettingsConfig; +use nocterm_ui::DesignTokens; +use nocterm_workspace::{Workspace, test_support::deps_for}; + +use crate::{EnvironmentOpenError, EnvironmentOpener, Installed, Placement, install, register}; + +pub(crate) const ADDRESS: &str = "hub.home:7443"; +/// The password that opens the account keys in the fixture. +pub(crate) const PASSWORD: &str = "correct horse"; + +pub(crate) fn hub_id() -> HubId { + HubId::from(HubAddress::parse(ADDRESS).unwrap()) +} + +/// The server most tests talk to: certificate `[7; 32]`, user `ada`. +pub(crate) fn server() -> FakeServer { + FakeServer::new(ADDRESS, 7) + .named("Home hub") + .with_user("ada", "secret") + .with_provider("corp", "Corporate SSO") +} + +/// The record of [`server`] as the settings keep it after a sign-in. +pub(crate) fn pinned_record() -> HubRecord { + server().pinned_record("Home") +} + +/// Remembers which environments the views asked to open. +#[derive(Default)] +pub(crate) struct RecordingEnvironments { + requests: RefCell>, +} + +impl RecordingEnvironments { + pub(crate) fn requests(&self) -> Vec<(Environment, Placement)> { + self.requests.borrow().clone() + } +} + +impl EnvironmentOpener for RecordingEnvironments { + fn open( + &self, + environment: Environment, + placement: Placement, + _: &mut gpui_kit::Window, + _: &mut gpui_kit::App, + ) -> Result<(), EnvironmentOpenError> { + self.requests.borrow_mut().push((environment, placement)); + Ok(()) + } +} + +pub(crate) struct Fixture { + pub(crate) window: AnyWindowHandle, + pub(crate) workspace: Entity, + pub(crate) hub: FakeHub, + pub(crate) devices: FakeAccountDevices, + pub(crate) agents: nocterm_hub_agents_model::testing::FakeHubAgents, + pub(crate) account_keys: FakeAccountKeys, + pub(crate) installed: Installed, + pub(crate) environments: Rc, +} + +/// A window over `hub`, with `known` servers in the settings. The hub is signed in to when +/// `signed_in` names a user of the server. +pub(crate) fn fixture( + cx: &mut TestAppContext, + environment: Environment, + hub: FakeHub, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + let config = SettingsConfig::in_memory(SettingsDocument::default()); + fixture_over(cx, config, environment, hub, known, signed_in) +} + +/// As [`fixture`], but the settings are kept by `config`, which may fail to save. +pub(crate) fn fixture_over( + cx: &mut TestAppContext, + config: SettingsConfig, + environment: Environment, + hub: FakeHub, + known: Vec, + signed_in: Option<&str>, +) -> Fixture { + if let Some(user) = signed_in { + hub.sign_in_as(&hub_id(), user); + } + let devices = FakeAccountDevices::default(); + let agents = nocterm_hub_agents_model::testing::FakeHubAgents::new(); + let account_keys = FakeAccountKeys::alongside(&hub, PASSWORD); + let environments = Rc::new(RecordingEnvironments::default()); + let (window, workspace, installed) = cx.update(|cx| { + gpui_kit::init(cx); + let settings = config + .with(HubsSettings { + servers: known, + ..HubsSettings::default() + }) + .install(cx); + nocterm_ui::init(DesignTokens::builtin(), settings.clone(), cx); + let installed = install(nocterm_hubs::install( + nocterm_hubs::Deps { + trust: Arc::new(hub.clone()), + sign_in: Arc::new(hub.clone()), + devices: Arc::new(devices.clone()), + account_keys: Arc::new(account_keys.clone()), + }, + settings, + cx, + )) + .with_hub_agents( + nocterm_hub_agents::install( + nocterm_hub_agents::Deps { + directory: Arc::new(agents.clone()), + servers: Arc::new(agents.clone()), + changes: Arc::new(agents.clone()), + hosts: Arc::new(nocterm_ssh_keys::testing::FakeAgentHosts::default()), + }, + cx, + ) + .agents() + .clone(), + ); + let (window, workspace) = + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| Workspace::new(deps_for(&[], cx), window, cx)) + }) + .unwrap(); + (window, workspace, installed) + }); + cx.run_until_parked(); + let setup = Fixture { + window, + workspace, + hub, + devices, + agents, + account_keys, + installed, + environments, + }; + setup.compose(cx, environment); + cx.run_until_parked(); + setup +} + +impl Fixture { + /// Adds a tab of a remote session, so the window holds something to lose. + pub(crate) fn add_tab(&self, cx: &mut TestAppContext) { + use nocterm_session::{Auth, Target}; + use nocterm_workspace::{SessionSpec, test_support::SessionProbe}; + cx.update_window(self.window, |_, window, cx| { + self.workspace.update(cx, |workspace, cx| { + let spec = SessionSpec { + options: Default::default(), + title: "shell".into(), + profile: None, + target: Target::new("me", "host.example", 22), + auth: Auth::Auto, + launch: None, + credential: None, + jumps: Vec::new(), + }; + let tab = SessionProbe::new(spec, true, cx); + workspace.add_item(tab, window, cx); + }); + }) + .unwrap(); + cx.run_until_parked(); + } + + fn compose(&self, cx: &mut TestAppContext, environment: Environment) { + cx.update_window(self.window, |_, window, cx| { + self.workspace.update(cx, |workspace, cx| { + register( + &self.installed, + environment, + self.environments.clone(), + workspace, + window, + cx, + ); + }); + }) + .unwrap(); + } + + /// Draws the window. + pub(crate) fn render(&self, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.render_frame(cx)) + .unwrap(); + cx.run_until_parked(); + } + + /// Runs `action` as if the focused workspace received it. + pub(crate) fn dispatch(&self, action: impl gpui_kit::Action, cx: &mut TestAppContext) { + // Focus belongs to a drawn tree. + self.render(cx); + cx.update_window(self.window, |_, window, cx| { + window.focus(&self.workspace.read(cx).focus_handle(cx), cx); + window.dispatch_action(Box::new(action), cx); + }) + .unwrap(); + self.render(cx); + self.render(cx); + } + + pub(crate) fn click(&self, id: impl Into, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.click(id, cx)) + .unwrap(); + self.render(cx); + } + + pub(crate) fn press(&self, key: &str, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.press(key, cx)) + .unwrap(); + self.render(cx); + } + + pub(crate) fn type_text(&self, text: &str, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| window.input(text, cx)) + .unwrap(); + self.render(cx); + } + + /// Whether an element with `id` was drawn. + pub(crate) fn shows(&self, id: impl Into, cx: &mut TestAppContext) -> bool { + let id = id.into(); + cx.update_window(self.window, |_, window, _| window.try_find(id).is_some()) + .unwrap() + } + + pub(crate) fn has_dialog(&self, cx: &mut TestAppContext) -> bool { + cx.update_window(self.window, |_, window, cx| window.has_active_dialog(cx)) + .unwrap() + } + + /// Waits for the entrance animation of a dialog, so its buttons can be clicked. + pub(crate) fn settle_dialog(&self, cx: &mut TestAppContext) { + self.render(cx); + std::thread::sleep( + *gpui_kit::component::dialog::ANIMATION_DURATION + std::time::Duration::from_millis(50), + ); + self.render(cx); + } +} diff --git a/crates/hubs/Cargo.toml b/crates/hubs/Cargo.toml new file mode 100644 index 00000000..c9200f41 --- /dev/null +++ b/crates/hubs/Cargo.toml @@ -0,0 +1,30 @@ +[package] +name = "nocterm-hubs" +description = "The hubs this device knows: connection status shared by every window, and sign-in." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +nocterm-hubs-model.workspace = true +nocterm-session.workspace = true +nocterm-store.workspace = true + +gpui.workspace = true +futures.workspace = true +thiserror.workspace = true +tracing.workspace = true + +[dev-dependencies] +nocterm-store = { workspace = true, features = ["test-support"] } +nocterm-hubs-model = { workspace = true, features = ["test-support"] } +nocterm-core.workspace = true +nocterm-settings.workspace = true +gpui = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/hubs/src/error.rs b/crates/hubs/src/error.rs new file mode 100644 index 00000000..9472a62e --- /dev/null +++ b/crates/hubs/src/error.rs @@ -0,0 +1,37 @@ +//! Typed failures of signing in to a hub and of managing known hubs. + +use nocterm_hubs_model::{FingerprintError, HubAddressError, HubError, HubId}; +use nocterm_store::StoreError; + +/// Why signing in to a hub did not complete. +#[derive(Clone, Debug, thiserror::Error)] +pub enum SignInError { + #[error("the server address is not valid")] + Address(#[from] HubAddressError), + #[error("the fingerprint is not valid")] + Fingerprint(#[from] FingerprintError), + #[error("the hub could not be used")] + Hub(#[from] HubError), + /// The address is a server that is listed already. Signing in again to it goes through its + /// row, and a server whose certificate changed has to be removed first. + #[error("the server {0} is listed already")] + AlreadyKnown(HubId), + /// The hub has no owner yet; this client cannot set it up. + #[error("the hub is not set up yet")] + NotInitialized, + /// The sign-in worked but the server could not be added to the settings, so the + /// credentials of this device were erased again. + #[error("the server could not be added to the settings")] + Save(#[source] StoreError), +} + +/// Why forgetting or signing out of a known hub failed. +#[derive(Clone, Debug, thiserror::Error)] +pub enum HubsError { + #[error("no such hub: {0}")] + Unknown(HubId), + #[error("the hub could not be used")] + Hub(#[from] HubError), + #[error("the settings could not be saved")] + Save(#[source] StoreError), +} diff --git a/crates/hubs/src/fixture.rs b/crates/hubs/src/fixture.rs new file mode 100644 index 00000000..5b58340f --- /dev/null +++ b/crates/hubs/src/fixture.rs @@ -0,0 +1,120 @@ +//! A registry over a [`FakeHub`] and in-memory settings, shared by the tests of this crate. + +use std::sync::Arc; + +use gpui::{Entity, TestAppContext}; +use nocterm_hubs_model::testing::{FakeHub, FakeServer}; +use nocterm_hubs_model::{HubAddress, HubId, HubRecord, HubsSettings}; +use nocterm_settings::SettingsDocument; +use nocterm_store::{SettingsConfig, SettingsHandle, StoreError}; + +use crate::{Deps, Hubs, install}; + +pub(crate) const ADDRESS: &str = "hub.home:7443"; + +pub(crate) struct Fixture { + pub hub: FakeHub, + pub hubs: Entity, + pub settings: SettingsHandle, +} + +pub(crate) fn address() -> HubAddress { + HubAddress::parse(ADDRESS).unwrap() +} + +pub(crate) fn id() -> HubId { + HubId::from(address()) +} + +/// The server most tests talk to: certificate `[7; 32]`, user `ada`. +pub(crate) fn server() -> FakeServer { + FakeServer::new(ADDRESS, 7) + .named("Home hub") + .with_user("ada", "secret") + .with_provider("corp", "Corporate SSO") +} + +pub(crate) fn pinned_record() -> HubRecord { + server().pinned_record("Home") +} + +/// Installs the registry over `hub` with `known` servers in the settings. +pub(crate) fn fixture(cx: &mut TestAppContext, hub: FakeHub, known: Vec) -> Fixture { + build(cx, hub, known, None) +} + +/// As [`fixture`], but every write of the settings fails. +pub(crate) fn fixture_with_failing_settings(cx: &mut TestAppContext, hub: FakeHub) -> Fixture { + fixture_with_known_failing_settings(cx, hub, Vec::new()) +} + +/// As [`fixture_with_failing_settings`], with `known` servers already listed. +pub(crate) fn fixture_with_known_failing_settings( + cx: &mut TestAppContext, + hub: FakeHub, + known: Vec, +) -> Fixture { + let writer: nocterm_store::SnapshotWriter = Arc::new(|_| { + Box::pin(async { + Err(StoreError::backend(std::io::Error::from( + std::io::ErrorKind::PermissionDenied, + ))) + }) + }); + build(cx, hub, known, Some(writer)) +} + +/// Installs the ordinary shared snapshot writer supplied by a behavior test. +pub(crate) fn fixture_with_writer( + cx: &mut TestAppContext, + hub: FakeHub, + known: Vec, + writer: nocterm_store::SnapshotWriter, +) -> Fixture { + build(cx, hub, known, Some(writer)) +} + +fn build( + cx: &mut TestAppContext, + hub: FakeHub, + known: Vec, + writer: Option>, +) -> Fixture { + cx.update(|cx| { + let document = SettingsDocument::default(); + let config = match writer { + Some(writer) => SettingsConfig::with_writer(document, writer), + None => SettingsConfig::in_memory(document), + }; + let settings = config + .with(HubsSettings { + servers: known, + ..HubsSettings::default() + }) + .install(cx); + let deps = Deps { + trust: Arc::new(hub.clone()), + sign_in: Arc::new(hub.clone()), + devices: Arc::new(nocterm_hubs_model::testing::FakeAccountDevices::default()), + account_keys: Arc::new(nocterm_hubs_model::testing::FakeAccountKeys::alongside( + &hub, "password", + )), + }; + let installed = install(deps, settings.clone(), cx); + Fixture { + hub, + hubs: installed.hubs().clone(), + settings, + } + }) +} + +impl Fixture { + pub(crate) fn status(&self, cx: &mut TestAppContext) -> Option { + self.hubs.read_with(cx, |hubs, _| hubs.status(&id())) + } + + pub(crate) fn servers(&self, cx: &mut TestAppContext) -> Vec { + cx.update(|cx| self.settings.setting::(cx).servers.clone()) + } +} diff --git a/crates/hubs/src/installed.rs b/crates/hubs/src/installed.rs new file mode 100644 index 00000000..bab8f863 --- /dev/null +++ b/crates/hubs/src/installed.rs @@ -0,0 +1,37 @@ +//! The ports the service needs and the handle to the installed registry. + +use std::sync::Arc; + +use gpui::{App, AppContext as _, Entity}; +use nocterm_hubs_model::{AccountDevices, AccountKeyLock, HubSignIn, HubTrust}; +use nocterm_store::SettingsHandle; + +use crate::registry::Hubs; + +/// The hub access ports, supplied by the composition root. +#[derive(Clone)] +pub struct Deps { + pub trust: Arc, + pub sign_in: Arc, + pub devices: Arc, + pub account_keys: Arc, +} + +/// The registry of known hubs, kept alive by whoever composes the application. +#[derive(Clone)] +pub struct Installed { + hubs: Entity, +} + +impl Installed { + pub fn hubs(&self) -> &Entity { + &self.hubs + } +} + +/// Registers the `[hubs]` settings section and starts connecting to every known hub. +pub fn install(deps: Deps, settings: SettingsHandle, cx: &mut App) -> Installed { + Installed { + hubs: cx.new(|cx| Hubs::new(deps, settings, cx)), + } +} diff --git a/crates/hubs/src/lib.rs b/crates/hubs/src/lib.rs new file mode 100644 index 00000000..3bc88c2e --- /dev/null +++ b/crates/hubs/src/lib.rs @@ -0,0 +1,39 @@ +//! The hubs this device knows: the status of each and sign-in. +//! +//! Owns: the registry of known hubs ([`Hubs`]) and the connection status every window shares +//! ([`HubStatus`]); the order of the sign-in steps ([`SignInSession`]); publishing a signed-in +//! server to `[hubs]`, and renaming, colouring, signing out of and removing a known one. +//! +//! Does not own: any I/O or protocol (the injected hub ports do, see `nocterm-hubs-model`); +//! the commands and dialog that show this state (`nocterm-hubs-ui`). +//! +//! Ports used: [`nocterm_hubs_model::HubTrust`], [`nocterm_hubs_model::HubSignIn`], +//! [`nocterm_hubs_model::AccountDevices`] (the devices of the account, listed and revoked +//! through [`Hubs`]) and [`nocterm_hubs_model::AccountKeyLock`] (the account keys, opened and +//! closed through [`Hubs`]), passed in [`Deps`]; `nocterm_store::SettingsHandle`. +//! Provides: [`Installed`] with the [`Hubs`] entity. +//! +//! Settings: the `[hubs]` section ([`nocterm_hubs_model::HubsSettings`]), registered by +//! [`install`]. +//! +//! Globals: none. +//! +//! Invariants: a certificate that is not the trusted one is never retried and never +//! accepted; an unreachable hub is retried with growing delays; a server enters the settings +//! displayed recovery offers pass unchanged to their original capabilities; +//! only after a sign-in worked, in one edit, and credentials made for a server that could not +//! be saved are erased again; every window shares one status and one connection per hub. + +mod error; +#[cfg(test)] +mod fixture; +mod installed; +mod registry; +mod sign_in; +mod status; + +pub use error::{HubsError, SignInError}; +pub use installed::{Deps, Installed, install}; +pub use registry::{HubSummary, Hubs, HubsEvent}; +pub use sign_in::{SignInEvent, SignInSession, SignInStep, TrustNote}; +pub use status::HubStatus; diff --git a/crates/hubs/src/registry.rs b/crates/hubs/src/registry.rs new file mode 100644 index 00000000..913d0b5c --- /dev/null +++ b/crates/hubs/src/registry.rs @@ -0,0 +1,454 @@ +//! Every hub this device knows, with the connection status every window shares. + +use std::sync::Arc; + +use gpui::{AppContext as _, Context, Entity, EventEmitter, Subscription, Task}; +use nocterm_hubs_model::{ + AccountKeyFuture, DeviceSummary, HubAddress, HubColor, HubEndpointSpec, HubError, HubFuture, + HubId, HubInfo, HubRecord, HubsSettings, +}; +use nocterm_session::Secret; +use nocterm_store::SettingsHandle; + +use crate::error::HubsError; +use crate::installed::Deps; +use crate::sign_in::{SignInEvent, SignInSession}; +use crate::status::{HubStatus, Settled, retry_delay, settle_connect}; + +/// What changes in the registry. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum HubsEvent { + /// A hub was added or removed, or a status changed. + Changed, +} + +/// A known hub as a menu shows it. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct HubSummary { + pub id: HubId, + pub label: String, + pub address: HubAddress, + pub color: Option, + pub status: HubStatus, +} + +struct HubEntry { + record: HubRecord, + status: HubStatus, + /// Unsuccessful attempts to reach the hub in a row. + failures: u32, + /// The connection being made, or the wait before the next attempt. + attempt: Option>, +} + +/// The known hubs, in the order of the settings, and the status of each. +pub struct Hubs { + settings: SettingsHandle, + deps: Deps, + entries: Vec, + _settings: Subscription, + _keys: Task<()>, +} + +impl EventEmitter for Hubs {} + +impl Hubs { + pub(crate) fn new(deps: Deps, settings: SettingsHandle, cx: &mut Context) -> Self { + settings.register::(cx); + let weak = cx.weak_entity(); + let observed = settings.observe(cx, move |cx| { + weak.update(cx, |hubs, cx| hubs.sync(cx)).ok(); + }); + let keys = Self::redraw_on_key_changes(&deps, cx); + let mut hubs = Self { + settings, + deps, + entries: Vec::new(), + _settings: observed, + _keys: keys, + }; + hubs.sync(cx); + hubs + } + + /// Tells the observers of the registry whenever account keys open or close, so a view of + /// what depends on them (the offer of a new recovery key) follows a vault unlock at once. + fn redraw_on_key_changes(deps: &Deps, cx: &mut Context) -> Task<()> { + let sign_in = Arc::clone(&deps.sign_in); + let mut seen = sign_in.keys_revision(); + cx.spawn(async move |this, cx| { + loop { + seen = sign_in.keys_changed(seen).await; + if this.update(cx, |_, cx| cx.notify()).is_err() { + break; + } + } + }) + } + + /// The known hubs in the order of the settings. + pub fn list(&self) -> Vec { + self.entries + .iter() + .map(|entry| HubSummary { + id: entry.record.id(), + label: entry.record.display_label(), + address: entry.record.address.clone(), + color: entry.record.color, + status: entry.status.clone(), + }) + .collect() + } + + pub fn summary(&self, hub: &HubId) -> Option { + self.entry(hub).map(|entry| HubSummary { + id: entry.record.id(), + label: entry.record.display_label(), + address: entry.record.address.clone(), + color: entry.record.color, + status: entry.status.clone(), + }) + } + + pub fn status(&self, hub: &HubId) -> Option { + self.entry(hub).map(|entry| entry.status.clone()) + } + + pub fn record(&self, hub: &HubId) -> Option<&HubRecord> { + self.entry(hub).map(|entry| &entry.record) + } + + fn entry(&self, hub: &HubId) -> Option<&HubEntry> { + self.entries.iter().find(|entry| entry.record.id() == *hub) + } + + fn entry_mut(&mut self, hub: &HubId) -> Option<&mut HubEntry> { + self.entries + .iter_mut() + .find(|entry| entry.record.id() == *hub) + } + + /// Brings the entries in line with the published settings: new servers start + /// connecting, removed ones disappear, a changed trust connects again. + fn sync(&mut self, cx: &mut Context) { + let records = &self.settings.setting::(cx).servers; + if records + .iter() + .eq(self.entries.iter().map(|entry| &entry.record)) + { + return; + } + let records = records.clone(); + let mut previous = std::mem::take(&mut self.entries); + let mut reconnect = Vec::new(); + for record in records { + let id = record.id(); + let kept = previous.iter().position(|entry| entry.record.id() == id); + let entry = match kept { + Some(index) => { + let mut entry = previous.remove(index); + if entry.record.trust != record.trust { + reconnect.push(id); + } + entry.record = record; + entry + } + None => { + reconnect.push(id); + HubEntry { + record, + status: HubStatus::Connecting, + failures: 0, + attempt: None, + } + } + }; + self.entries.push(entry); + } + for hub in reconnect { + self.connect(&hub, cx); + } + cx.emit(HubsEvent::Changed); + cx.notify(); + } + + /// Connects to a hub with the trust the settings state. + fn connect(&mut self, hub: &HubId, cx: &mut Context) { + let Some(index) = self.entries.iter().position(|e| e.record.id() == *hub) else { + return; + }; + let entry = &mut self.entries[index]; + entry.status = HubStatus::Connecting; + let connecting = self.deps.sign_in.connect(HubEndpointSpec { + address: entry.record.address.clone(), + trust: entry.record.trust, + }); + let hub = hub.clone(); + entry.attempt = Some(cx.spawn(async move |this, cx| { + let result = connecting.await; + this.update(cx, |hubs, cx| hubs.settled(&hub, result, cx)) + .ok(); + })); + cx.emit(HubsEvent::Changed); + cx.notify(); + } + + fn settled(&mut self, hub: &HubId, result: Result, cx: &mut Context) { + let settled = settle_connect(&result); + self.rest(hub, settled, cx); + } + + /// Records where a hub ended up and, for a hub that did not answer, when to try again. + fn rest(&mut self, hub: &HubId, settled: Settled, cx: &mut Context) { + let now = cx.background_executor().now(); + let Some(entry) = self.entry_mut(hub) else { + return; + }; + entry.attempt = None; + let mut retry = None; + match settled { + Settled::Status(status) => { + entry.failures = 0; + entry.status = status; + } + Settled::Retry => { + entry.failures += 1; + let delay = retry_delay(entry.failures); + entry.status = HubStatus::Offline { + retry_at: now + delay, + }; + retry = Some(delay); + } + } + if let Some(delay) = retry { + let again = hub.clone(); + let waiting = cx.spawn(async move |this, cx| { + cx.background_executor().timer(delay).await; + this.update(cx, |hubs, cx| hubs.connect(&again, cx)).ok(); + }); + if let Some(entry) = self.entry_mut(hub) { + entry.attempt = Some(waiting); + } + } + cx.emit(HubsEvent::Changed); + cx.notify(); + } + + /// Tries a hub again now, instead of at the scheduled time. + pub fn retry(&mut self, hub: &HubId, cx: &mut Context) { + if self + .status(hub) + .is_some_and(|status| !matches!(status, HubStatus::Connecting | HubStatus::Online)) + { + self.connect(hub, cx); + } + } + + /// Starts a sign-in to a server that is not known yet. + pub fn start_sign_in(&mut self, cx: &mut Context) -> Entity { + let (deps, settings) = (self.deps.clone(), self.settings.clone()); + let session = cx.new(|_| SignInSession::new(&deps, settings)); + self.connect_when_signed_in(&session, cx); + session + } + + /// A finished sign-in connects the hub again, so a hub that was signed out or revoked + /// comes back without a change of the settings. A new server is already connecting. + fn connect_when_signed_in(&self, session: &Entity, cx: &mut Context) { + cx.subscribe(session, |hubs, _, event, cx| { + if let SignInEvent::Finished(hub) = event + && hubs.status(hub).is_some_and(|s| s != HubStatus::Connecting) + { + hubs.connect(hub, cx); + } + }) + .detach(); + } + + /// Starts signing in again to a known hub, with the trust its record states. + pub fn start_sign_in_for( + &mut self, + hub: &HubId, + cx: &mut Context, + ) -> Option> { + let record = self.record(hub)?.clone(); + let (deps, settings) = (self.deps.clone(), self.settings.clone()); + let session = cx.new(|cx| SignInSession::resume(&deps, settings, record, cx)); + self.connect_when_signed_in(&session, cx); + Some(session) + } + + /// Whether the account keys of `hub` are open and the recovery key made with them was never + /// confirmed as saved: a new one can be issued with [`Hubs::start_recovery_key_reissue`]. + pub fn recovery_key_unconfirmed(&self, hub: &HubId) -> bool { + self.entry(hub).is_some() && self.deps.sign_in.recovery_key_unconfirmed(hub) + } + + /// Captures the original recipient of a displayed recovery action. + pub fn recovery_key_offer( + &self, + hub: &HubId, + ) -> Result { + if self.entry(hub).is_none() { + return Err(nocterm_hubs_model::HubError::Stale); + } + self.deps.sign_in.recovery_key_offer(hub) + } + + /// Starts issuing a new recovery key for `hub`, which replaces the unconfirmed one. The + /// dialog shows it once; `None` when `hub` is not listed. + pub fn start_recovery_key_reissue( + &mut self, + hub: &HubId, + offer: nocterm_hubs_model::RecoveryKeyReissue, + cx: &mut Context, + ) -> Option> { + self.entry(hub)?; + if offer.hub() != hub || offer.check_current().is_err() { + return None; + } + let (deps, settings) = (self.deps.clone(), self.settings.clone()); + let session = cx.new(|cx| SignInSession::reissue(&deps, settings, hub, offer, cx)); + // Saving the new key ends the offer: whoever shows it is told to look again. + cx.subscribe(&session, |_, _, event, cx| { + if matches!(event, SignInEvent::Finished(_)) { + cx.notify(); + } + }) + .detach(); + Some(session) + } + + /// The devices of the account on `hub`. The call captures the original membership before it + /// returns, so a result never belongs to an account that replaced it; a hub that is not + /// listed is [`HubError::Stale`]. + pub fn list_devices(&self, hub: &HubId) -> HubFuture> { + if self.entry(hub).is_none() { + return Box::pin(async { Err(HubError::Stale) }); + } + self.deps.devices.list(hub) + } + + /// Revokes `device` of the account on `hub`. Revoking this device is [`Hubs::sign_out`]. + pub fn revoke_device(&self, hub: &HubId, device: &str) -> HubFuture<()> { + if self.entry(hub).is_none() { + return Box::pin(async { Err(HubError::Stale) }); + } + self.deps.devices.revoke(hub, device) + } + + /// Whether the keys of the account on `hub` are open on this device. + pub fn account_keys_open(&self, hub: &HubId) -> bool { + self.deps.account_keys.is_open(hub) + } + + /// Opens the keys of the account on `hub` with `password`. + pub fn unlock_account_keys(&self, hub: &HubId, password: Secret) -> AccountKeyFuture<()> { + self.deps.account_keys.unlock(hub, password) + } + + /// Closes the keys of the account on `hub`. + pub fn lock_account_keys(&self, hub: &HubId) { + self.deps.account_keys.lock(hub); + } + + /// Ends this device's session on the hub and erases its credentials. The server stays + /// in the list, signed out. + pub fn sign_out(&mut self, hub: &HubId, cx: &mut Context) -> Task> { + if self.entry(hub).is_none() { + return Task::ready(Err(HubsError::Unknown(hub.clone()))); + } + let signing_out = self.deps.sign_in.sign_out(hub); + let hub = hub.clone(); + cx.spawn(async move |this, cx| { + let result = signing_out.await; + this.update(cx, |hubs, cx| { + if result.is_ok() { + hubs.rest(&hub, Settled::Status(HubStatus::SignedOut), cx); + } + }) + .ok(); + result.map_err(HubsError::from) + }) + } + + /// The settings the registry edits, for a text field that saves a server's name through + /// them in the same one-edit way as [`Hubs::set_label`]. + pub fn settings(&self) -> SettingsHandle { + self.settings.clone() + } + + /// Renames a server in the settings, in one edit. An empty name shows the address. + pub fn set_label( + &mut self, + hub: &HubId, + label: &str, + cx: &mut Context, + ) -> Task> { + let label = label.to_owned(); + self.edit_record(hub, cx, move |settings, hub| { + settings.set_label(hub, &label); + }) + } + + /// Marks a server with a colour, or clears the mark, in one edit of the settings. + pub fn set_color( + &mut self, + hub: &HubId, + color: Option, + cx: &mut Context, + ) -> Task> { + self.edit_record(hub, cx, move |settings, hub| { + settings.set_color(hub, color); + }) + } + + fn edit_record( + &mut self, + hub: &HubId, + cx: &mut Context, + edit: impl FnOnce(&mut HubsSettings, &HubId) + 'static, + ) -> Task> { + if self.entry(hub).is_none() { + return Task::ready(Err(HubsError::Unknown(hub.clone()))); + } + let id = hub.clone(); + let saving = self + .settings + .update_checked::(cx, move |settings| { + edit(settings, &id); + Ok(()) + }); + cx.spawn(async move |_, _| saving.await.map(|_| ()).map_err(HubsError::Save)) + } + + /// Removes a server from the settings and erases the credentials kept for it. When the + /// settings cannot be saved the server stays listed without credentials, and the hub + /// connects again to show where it stands. + pub fn remove(&mut self, hub: &HubId, cx: &mut Context) -> Task> { + if self.entry(hub).is_none() { + return Task::ready(Err(HubsError::Unknown(hub.clone()))); + } + if let Err(error) = self.deps.sign_in.forget(hub) { + return Task::ready(Err(error.into())); + } + let id = hub.clone(); + let removed = hub.clone(); + let saving = self + .settings + .update_checked::(cx, move |settings| { + settings.remove(&removed); + Ok(()) + }); + cx.spawn(async move |this, cx| { + let saved = saving.await; + if saved.is_err() { + this.update(cx, |hubs, cx| hubs.connect(&id, cx)).ok(); + } + saved.map(|_| ()).map_err(HubsError::Save) + }) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs/src/registry/tests.rs b/crates/hubs/src/registry/tests.rs new file mode 100644 index 00000000..5a26cd91 --- /dev/null +++ b/crates/hubs/src/registry/tests.rs @@ -0,0 +1,351 @@ +use std::time::Duration; + +use gpui::TestAppContext; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp}; +use nocterm_hubs_model::{Fingerprint, HubColor, HubError, HubRecord, TrustPolicy}; + +use crate::fixture::{ + address, fixture, fixture_with_known_failing_settings, id, pinned_record, server, +}; +use crate::{HubStatus, HubsError}; + +fn signed_in_hub() -> FakeHub { + let hub = FakeHub::new().with_server(server()); + hub.sign_in_as(&id(), "ada"); + hub +} + +#[gpui::test] +async fn a_known_hub_connects_and_goes_online(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + assert_eq!(fx.status(cx), Some(HubStatus::Connecting)); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); +} + +#[gpui::test] +async fn a_hub_without_credentials_is_signed_out(cx: &mut TestAppContext) { + let hub = FakeHub::new().with_server(server()); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::SignedOut)); +} + +#[gpui::test] +async fn an_unreachable_hub_is_retried_with_growing_delays_until_it_answers( + cx: &mut TestAppContext, +) { + let hub = signed_in_hub(); + hub.edit_server(&address(), |server| server.online = false); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert!(matches!(fx.status(cx), Some(HubStatus::Offline { .. }))); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); + + cx.executor().advance_clock(Duration::from_secs(2)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 2); + + // The second wait is five seconds, so two more do not reach it. + cx.executor().advance_clock(Duration::from_secs(2)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 2); + + fx.hub + .edit_server(&address(), |server| server.online = true); + cx.executor().advance_clock(Duration::from_secs(3)); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn a_changed_certificate_is_a_trust_failure_that_is_never_retried(cx: &mut TestAppContext) { + let hub = signed_in_hub(); + hub.edit_server(&address(), |server| { + server.certificate = Fingerprint::from_bytes([9; 32]); + }); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!( + fx.status(cx), + Some(HubStatus::TrustFailed { + pinned: Some(Fingerprint::from_bytes([7; 32])), + presented: Fingerprint::from_bytes([9; 32]), + }) + ); + cx.executor().advance_clock(Duration::from_secs(300)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); +} + +#[gpui::test] +async fn a_revoked_device_loses_its_credentials_and_is_not_retried(cx: &mut TestAppContext) { + let hub = signed_in_hub(); + hub.revoke_device(&id()); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Revoked)); + assert!(!fx.hub.is_signed_in(&id())); + cx.executor().advance_clock(Duration::from_secs(300)); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); +} + +#[gpui::test] +async fn a_hub_with_another_protocol_asks_for_an_upgrade(cx: &mut TestAppContext) { + let hub = signed_in_hub(); + hub.edit_server(&address(), |server| server.protocol = 2); + let fx = fixture(cx, hub, vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!( + fx.status(cx), + Some(HubStatus::UpgradeRequired { hub: 2, client: 1 }) + ); +} + +#[gpui::test] +async fn an_unknown_hub_has_no_status_or_removal(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), Vec::new()); + assert_eq!(fx.status(cx), None); + let removed = fx.hubs.update(cx, |hubs, cx| hubs.remove(&id(), cx)); + assert!(removed.await.is_err()); +} + +#[gpui::test] +async fn a_server_added_to_the_settings_starts_connecting(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), Vec::new()); + cx.update(|cx| { + fx.settings + .update::(cx, |settings| { + settings.upsert(pinned_record()); + }) + }) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn removing_a_server_forgets_its_credentials_and_drops_it_from_the_list( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.remove(&id(), cx)) + .await + .unwrap(); + cx.run_until_parked(); + assert!(fx.servers(cx).is_empty()); + assert_eq!(fx.status(cx), None); + assert!(fx.hub.calls().contains(&FakeCall::Forget(id()))); + assert!(!fx.hub.is_signed_in(&id())); +} + +#[gpui::test] +async fn signing_out_keeps_the_server_listed_but_signed_out(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.sign_out(&id(), cx)) + .await + .unwrap(); + assert_eq!(fx.status(cx), Some(HubStatus::SignedOut)); + assert_eq!(fx.servers(cx).len(), 1); + assert!(!fx.hub.is_signed_in(&id())); +} + +#[gpui::test] +async fn a_changed_trust_in_the_settings_connects_again(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let changed = HubRecord::new(address(), TrustPolicy::PublicCa, "Home"); + cx.update(|cx| { + fx.settings + .update::(cx, move |s| s.upsert(changed)) + }) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(fx.hub.count(FakeOp::Connect), 2); + // The fake server is not publicly trusted, so the new trust fails and stays failed. + assert!(matches!( + fx.status(cx), + Some(HubStatus::TrustFailed { pinned: None, .. }) + )); +} + +fn revision(fx: &crate::fixture::Fixture, cx: &mut TestAppContext) -> u64 { + cx.read(|cx| fx.settings.revision(cx)) +} + +#[gpui::test] +async fn renaming_a_server_is_one_settings_edit_and_shows_in_the_list(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let before = revision(&fx, cx); + + fx.hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), " Office\n", cx)) + .await + .unwrap(); + cx.run_until_parked(); + + assert_eq!(revision(&fx, cx), before + 1); + assert_eq!(fx.servers(cx)[0].label, "Office"); + let label = fx + .hubs + .read_with(cx, |hubs, _| hubs.summary(&id()).unwrap().label); + assert_eq!(label, "Office"); + assert_eq!( + fx.hub.count(FakeOp::Connect), + 1, + "no new connection for a name" + ); +} + +#[gpui::test] +async fn an_empty_name_shows_the_address_again(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), " ", cx)) + .await + .unwrap(); + cx.run_until_parked(); + let label = fx + .hubs + .read_with(cx, |hubs, _| hubs.summary(&id()).unwrap().label); + assert_eq!(label, address().to_string()); +} + +#[gpui::test] +async fn colouring_a_server_is_one_settings_edit_and_can_be_undone(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let before = revision(&fx, cx); + + fx.hubs + .update(cx, |hubs, cx| { + hubs.set_color(&id(), Some(HubColor::Green), cx) + }) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(revision(&fx, cx), before + 1); + let color = fx + .hubs + .read_with(cx, |hubs, _| hubs.summary(&id()).unwrap().color); + assert_eq!(color, Some(HubColor::Green)); + assert_eq!(fx.status(cx), Some(HubStatus::Online), "still connected"); + + fx.hubs + .update(cx, |hubs, cx| hubs.set_color(&id(), None, cx)) + .await + .unwrap(); + assert_eq!(fx.servers(cx)[0].color, None); +} + +#[gpui::test] +async fn editing_an_unknown_server_fails_and_changes_nothing(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), Vec::new()); + let before = revision(&fx, cx); + let renamed = fx + .hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), "x", cx)); + let colored = fx.hubs.update(cx, |hubs, cx| { + hubs.set_color(&id(), Some(HubColor::Red), cx) + }); + assert!(matches!(renamed.await, Err(HubsError::Unknown(_)))); + assert!(matches!(colored.await, Err(HubsError::Unknown(_)))); + assert_eq!(revision(&fx, cx), before); +} + +#[gpui::test] +async fn a_failed_save_leaves_the_name_and_colour_as_they_were(cx: &mut TestAppContext) { + let fx = fixture_with_known_failing_settings(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + let renamed = fx + .hubs + .update(cx, |hubs, cx| hubs.set_label(&id(), "Office", cx)); + assert!(matches!(renamed.await, Err(HubsError::Save(_)))); + let colored = fx.hubs.update(cx, |hubs, cx| { + hubs.set_color(&id(), Some(HubColor::Cyan), cx) + }); + assert!(matches!(colored.await, Err(HubsError::Save(_)))); + cx.run_until_parked(); + assert_eq!(fx.servers(cx), vec![pinned_record()]); +} + +#[gpui::test] +async fn signing_out_erases_the_credentials_and_keeps_name_and_colour(cx: &mut TestAppContext) { + let marked = pinned_record().with_color(Some(HubColor::Magenta)); + let fx = fixture(cx, signed_in_hub(), vec![marked.clone()]); + cx.run_until_parked(); + fx.hubs + .update(cx, |hubs, cx| hubs.sign_out(&id(), cx)) + .await + .unwrap(); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::SignedOut)); + assert_eq!(fx.servers(cx), vec![marked]); + assert!(fx.hub.calls().contains(&FakeCall::SignOut(id()))); + assert!(!fx.hub.is_signed_in(&id())); +} + +#[gpui::test] +async fn a_server_that_cannot_be_removed_from_the_settings_shows_as_signed_out( + cx: &mut TestAppContext, +) { + let fx = fixture_with_known_failing_settings(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); + + let removed = fx.hubs.update(cx, |hubs, cx| hubs.remove(&id(), cx)); + assert!(matches!(removed.await, Err(HubsError::Save(_)))); + cx.run_until_parked(); + + assert_eq!(fx.servers(cx), vec![pinned_record()], "still listed"); + assert!(!fx.hub.is_signed_in(&id()), "the credentials are gone"); + assert_eq!( + fx.status(cx), + Some(HubStatus::SignedOut), + "the list does not claim a connection that cannot work" + ); +} + +#[gpui::test] +async fn credentials_that_cannot_be_erased_keep_the_server_listed(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.run_until_parked(); + fx.hub.fail_next(FakeOp::Forget, HubError::NotSignedIn); + let removed = fx.hubs.update(cx, |hubs, cx| hubs.remove(&id(), cx)); + assert!(matches!(removed.await, Err(HubsError::Hub(_)))); + cx.run_until_parked(); + assert_eq!(fx.servers(cx), vec![pinned_record()]); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn an_unrelated_settings_edit_does_not_publish_a_hub_change(cx: &mut TestAppContext) { + let fx = fixture(cx, signed_in_hub(), vec![pinned_record()]); + cx.update(|cx| fx.settings.register::(cx)); + cx.run_until_parked(); + let redraws = std::rc::Rc::new(std::cell::Cell::new(0)); + let changed = redraws.clone(); + let _subscription = + cx.update(|cx| cx.observe(&fx.hubs, move |_, _| changed.set(changed.get() + 1))); + let update = cx.update(|cx| { + fx.settings + .update::(cx, |settings| { + settings.keepalive_interval_secs = 43 + }) + }); + update.await.unwrap(); + cx.run_until_parked(); + assert_eq!(redraws.get(), 0); + assert_eq!(fx.hub.count(FakeOp::Connect), 1); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} diff --git a/crates/hubs/src/sign_in.rs b/crates/hubs/src/sign_in.rs new file mode 100644 index 00000000..1db2f499 --- /dev/null +++ b/crates/hubs/src/sign_in.rs @@ -0,0 +1,748 @@ +//! Signing in to a server: address, certificate trust, then a password or the browser. +//! +//! One [`SignInSession`] is one run of the dialog. It owns the order of the steps and +//! publishes the server to the settings in a single edit once the sign-in worked. + +use std::sync::Arc; + +use futures::FutureExt as _; +use gpui::{Context, EventEmitter, Task}; +use nocterm_hubs_model::{ + Fingerprint, HubAddress, HubEndpointSpec, HubError, HubId, HubInfo, HubRecord, HubSignIn, + HubTrust, HubsSettings, KeyStatus, OidcProvider, OidcSignIn, ProbedHub, RecoveryKeyOffer, + RecoveryKeyReissue, SignedIn, TrustDecision, TrustPolicy, decide, +}; +use nocterm_session::Secret; +use nocterm_store::SettingsHandle; + +use crate::error::SignInError; +use crate::installed::Deps; + +/// How the certificate of the server came to be trusted. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum TrustNote { + /// It matches the fingerprint the user typed (or the record states). + Pinned, + /// A public certificate authority vouches for it. + PublicCa, + /// The user confirmed its fingerprint on first use. + Confirmed, +} + +/// Where the dialog is. +#[derive(Clone, Debug)] +pub enum SignInStep { + /// Waiting for an address and an optional fingerprint. + Address { + problem: Option, + }, + /// Reading the certificate or connecting. + Checking, + /// The certificate is not publicly trusted and no fingerprint was typed: the user + /// decides whether to trust and pin it. + ConfirmFingerprint { + address: HubAddress, + fingerprint: Fingerprint, + }, + /// The certificate is not the one the user typed. + Mismatch { + typed: Fingerprint, + presented: Fingerprint, + }, + /// The certificate is trusted; waiting for a password or a provider. + Method { + hub_name: String, + password: bool, + providers: Vec, + note: TrustNote, + problem: Option, + }, + /// A password was sent. + Authenticating, + /// Waiting for the user to finish in the browser. + Browser { + provider: String, + /// The address to open; absent until the sign-in listens. + url: Option, + }, + /// The account had no keys and they were made. The recovery key is shown here once and kept + /// nowhere: it is the only way back after the password is lost. + RecoveryKey { + key: RecoveryKeyOffer, + }, + /// The keys are open, but the recovery key shown when they were made was never confirmed as + /// saved (the dialog was closed or the client stopped on that step). A new one can be + /// issued; the old one stays valid until then. + RecoveryOffer { + offer: RecoveryKeyReissue, + problem: Option, + }, + /// Replacing the recovery key. The new key exists on the server before it is shown, so this + /// step cannot be left: stopping now would lose it. + Issuing, + /// A browser sign-in cannot open the account's keys: waiting for the vault passphrase. + Passphrase { + problem: Option, + }, + /// The password does not open the account's keys (it was reset): waiting for the + /// recovery key. + Recovery { + problem: Option, + }, + /// Adding the server to the settings. + Saving, + Finished(HubId), + /// The server could not be added. Final. + Failed(SignInError), +} + +/// What the dialog asks its host to do. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum SignInEvent { + /// Open this address in the system browser. + OpenBrowser(String), + /// The server is signed in to and saved. + Finished(HubId), +} + +/// What is known of the server being signed in to. +#[derive(Default)] +struct Attempt { + address: Option, + /// The fingerprint the user typed. + expected: Option, + trust: Option, + note: Option, + info: Option, + /// The label of a server that is already known. + label: Option, + /// Authority supplied with this successful login, independent of recovery key actions. + login: Option, + /// The address cannot be changed: signing in again to a known server. + fixed: bool, + /// The run only issues a new recovery key for a server that is signed in to and listed. + reissue_only: bool, +} + +/// One run of the sign-in dialog. +pub struct SignInSession { + trust: Arc, + sign_in: Arc, + settings: SettingsHandle, + step: SignInStep, + attempt: Attempt, + /// The call in flight; dropping it cancels a browser sign-in. + work: Option>, +} + +impl EventEmitter for SignInSession {} + +impl SignInSession { + pub(crate) fn new(deps: &Deps, settings: SettingsHandle) -> Self { + Self { + trust: Arc::clone(&deps.trust), + sign_in: Arc::clone(&deps.sign_in), + settings, + step: SignInStep::Address { problem: None }, + attempt: Attempt::default(), + work: None, + } + } + + /// Signing in again to a known server: its trust is decided, so the dialog goes straight + /// to connecting. + pub(crate) fn resume( + deps: &Deps, + settings: SettingsHandle, + record: HubRecord, + cx: &mut Context, + ) -> Self { + let mut session = Self::new(deps, settings); + let note = match record.trust { + TrustPolicy::Pinned(_) => TrustNote::Pinned, + TrustPolicy::PublicCa => TrustNote::PublicCa, + }; + session.attempt = Attempt { + address: Some(record.address.clone()), + label: Some(record.label.clone()), + fixed: true, + ..Attempt::default() + }; + session.connect(record.trust, note, cx); + session + } + + /// A run that issues a new recovery key for the signed-in server `hub`, without signing in. + pub(crate) fn reissue( + deps: &Deps, + settings: SettingsHandle, + hub: &HubId, + offer: RecoveryKeyReissue, + cx: &mut Context, + ) -> Self { + let mut session = Self::new(deps, settings); + session.attempt = Attempt { + address: Some(hub.address().clone()), + fixed: true, + reissue_only: true, + ..Attempt::default() + }; + session.issue(offer, cx); + session + } + + /// The run only issues a new recovery key: nothing is signed in to or saved. + pub fn reissue_only(&self) -> bool { + self.attempt.reissue_only + } + + pub fn step(&self) -> &SignInStep { + &self.step + } + + /// The server may be edited: the dialog was not opened for a known one. + pub fn address_editable(&self) -> bool { + !self.attempt.fixed + } + + fn move_to(&mut self, step: SignInStep, cx: &mut Context) { + self.step = step; + cx.notify(); + } + + /// Reads the certificate of the typed server. `fingerprint` may be empty. + pub fn submit_address(&mut self, address: &str, fingerprint: &str, cx: &mut Context) { + if !matches!(self.step, SignInStep::Address { .. }) || self.attempt.fixed { + return; + } + let parsed = parse_inputs(address, fingerprint); + let (address, expected) = match parsed { + Ok(parsed) => parsed, + Err(problem) => { + return self.move_to( + SignInStep::Address { + problem: Some(problem), + }, + cx, + ); + } + }; + // A known server keeps the certificate it was pinned with: a new sign-in must not + // replace it, so a changed certificate is dealt with by removing the server first. + let known = HubId::from(address.clone()); + if self + .settings + .setting::(cx) + .find(&known) + .is_some() + { + return self.back_to_address(SignInError::AlreadyKnown(known), cx); + } + self.attempt.address = Some(address.clone()); + self.attempt.expected = expected; + self.move_to(SignInStep::Checking, cx); + let probing = self.trust.probe(&address); + self.work = Some(cx.spawn(async move |this, cx| { + let probed = probing.await; + this.update(cx, |session, cx| session.probed(probed, cx)) + .ok(); + })); + } + + fn probed(&mut self, probed: Result, cx: &mut Context) { + self.work = None; + let probe = match probed { + Ok(probe) => probe, + Err(error) => return self.back_to_address(error.into(), cx), + }; + let decision = decide(&probe, self.attempt.expected.as_ref()); + match decision { + TrustDecision::Pinned(_) | TrustDecision::PublicCa => { + let note = if matches!(decision, TrustDecision::PublicCa) { + TrustNote::PublicCa + } else { + TrustNote::Pinned + }; + if let Some(trust) = decision.settled() { + self.connect(trust, note, cx); + } + } + TrustDecision::ConfirmFirstUse(fingerprint) => { + if let Some(address) = self.attempt.address.clone() { + self.move_to( + SignInStep::ConfirmFingerprint { + address, + fingerprint, + }, + cx, + ); + } + } + TrustDecision::Mismatch { typed, presented } => { + self.move_to(SignInStep::Mismatch { typed, presented }, cx); + } + } + } + + /// The user trusts the certificate that was shown and wants it pinned. + pub fn confirm_fingerprint(&mut self, cx: &mut Context) { + let SignInStep::ConfirmFingerprint { fingerprint, .. } = self.step else { + return; + }; + self.connect(TrustPolicy::Pinned(fingerprint), TrustNote::Confirmed, cx); + } + + fn back_to_address(&mut self, problem: SignInError, cx: &mut Context) { + self.move_to( + SignInStep::Address { + problem: Some(problem), + }, + cx, + ); + } + + /// Connects with the decided trust and learns which ways of signing in the server offers. + fn connect(&mut self, trust: TrustPolicy, note: TrustNote, cx: &mut Context) { + let Some(address) = self.attempt.address.clone() else { + return; + }; + self.attempt.trust = Some(trust); + self.attempt.note = Some(note); + self.move_to(SignInStep::Checking, cx); + let connecting = self.sign_in.connect(HubEndpointSpec { address, trust }); + self.work = Some(cx.spawn(async move |this, cx| { + let result = connecting.await; + this.update(cx, |session, cx| session.connected(result, cx)) + .ok(); + })); + } + + fn connected(&mut self, result: Result, cx: &mut Context) { + self.work = None; + let info = match result { + Ok(info) => info, + Err(error) => return self.back_to_address(error.into(), cx), + }; + if !info.bootstrapped { + return self.back_to_address(SignInError::NotInitialized, cx); + } + let signed_in = info.signed_in; + self.attempt.info = Some(info); + if signed_in { + // Keys that are open already, from a run of this dialog that was closed on the + // recovery key, still wait for it to be confirmed. + let unconfirmed = self + .hub() + .is_some_and(|hub| self.sign_in.recovery_key_unconfirmed(&hub)); + if unconfirmed { + let offer = self + .hub() + .ok_or(HubError::Stale) + .and_then(|hub| self.sign_in.recovery_key_offer(&hub)); + match offer { + Ok(offer) => self.move_to( + SignInStep::RecoveryOffer { + offer, + problem: None, + }, + cx, + ), + Err(error) => self.move_to(SignInStep::Failed(error.into()), cx), + } + } else { + self.publish(cx); + } + } else { + self.show_methods(None, cx); + } + } + + fn show_methods(&mut self, problem: Option, cx: &mut Context) { + let Some(info) = &self.attempt.info else { + return; + }; + let step = SignInStep::Method { + hub_name: info.name.clone(), + password: info.password_login, + providers: info.oidc_providers.clone(), + note: self.attempt.note.unwrap_or(TrustNote::Pinned), + problem, + }; + self.move_to(step, cx); + } + + fn endpoint(&self) -> Option { + Some(HubEndpointSpec { + address: self.attempt.address.clone()?, + trust: self.attempt.trust?, + }) + } + + /// Signs in with a user name and password. + pub fn sign_in_with_password( + &mut self, + username: &str, + password: Secret, + cx: &mut Context, + ) { + let (SignInStep::Method { .. }, Some(endpoint)) = (&self.step, self.endpoint()) else { + return; + }; + self.move_to(SignInStep::Authenticating, cx); + let signing_in = self + .sign_in + .password(endpoint, username.trim().to_owned(), password); + self.work = Some(cx.spawn(async move |this, cx| { + let result = signing_in.await; + this.update(cx, |session, cx| session.signed(result, cx)) + .ok(); + })); + } + + /// Signs in through the identity provider `provider` in the system browser. + pub fn sign_in_with_provider(&mut self, provider: &str, cx: &mut Context) { + let (SignInStep::Method { .. }, Some(endpoint)) = (&self.step, self.endpoint()) else { + return; + }; + self.move_to( + SignInStep::Browser { + provider: provider.to_owned(), + url: None, + }, + cx, + ); + let OidcSignIn { browser, done } = self.sign_in.oidc(endpoint, provider.to_owned()); + self.work = Some(cx.spawn(async move |this, cx| { + let result = await_browser_sign_in(browser, done, |url| { + this.update(cx, |session, cx| session.browser_ready(url, cx)) + .ok(); + }) + .await; + this.update(cx, |session, cx| session.signed(result, cx)) + .ok(); + })); + } + + fn browser_ready(&mut self, url: String, cx: &mut Context) { + if let SignInStep::Browser { url: shown, .. } = &mut self.step { + *shown = Some(url.clone()); + cx.emit(SignInEvent::OpenBrowser(url)); + cx.notify(); + } + } + + fn signed(&mut self, result: Result, cx: &mut Context) { + self.work = None; + match result { + Ok(signed) => { + self.attempt.login = Some(signed.login); + self.keys_settled(signed.keys, cx); + } + Err(error) => self.show_methods(Some(error.into()), cx), + } + } + + /// Goes on from what a sign-in (or a passphrase) did to the account's keys. + fn keys_settled(&mut self, keys: KeyStatus, cx: &mut Context) { + let displayed = match &keys { + KeyStatus::Created(key) => Some(key.hub()), + KeyStatus::RecoveryKeyUnconfirmed(offer) => Some(offer.hub()), + _ => None, + }; + if displayed.is_some_and(|hub| Some(hub) != self.hub().as_ref()) { + return self.move_to(SignInStep::Failed(HubError::Stale.into()), cx); + } + match keys { + KeyStatus::Open => self.publish(cx), + KeyStatus::RecoveryKeyUnconfirmed(offer) => self.move_to( + SignInStep::RecoveryOffer { + offer, + problem: None, + }, + cx, + ), + KeyStatus::Created(key) => self.move_to(SignInStep::RecoveryKey { key }, cx), + KeyStatus::PassphraseNeeded => { + self.move_to(SignInStep::Passphrase { problem: None }, cx) + } + KeyStatus::RecoveryNeeded => self.move_to(SignInStep::Recovery { problem: None }, cx), + } + } + + fn hub(&self) -> Option { + self.attempt.address.clone().map(HubId::from) + } + + /// The user wrote the recovery key down: it is no longer offered again, and the sign-in + /// goes on. + pub fn acknowledge_recovery_key(&mut self, cx: &mut Context) { + let SignInStep::RecoveryKey { key } = &self.step else { + return; + }; + let confirming = key.confirm(); + self.move_to(SignInStep::Saving, cx); + self.work = Some(cx.spawn(async move |this, cx| { + let result = confirming.await; + this.update(cx, |session, cx| session.recovery_acknowledged(result, cx)) + .ok(); + })); + } + + fn recovery_acknowledged(&mut self, result: Result<(), HubError>, cx: &mut Context) { + self.work = None; + if let Err(error) = result { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + let hub = self.hub(); + match hub { + Some(hub) if self.attempt.reissue_only => { + self.move_to(SignInStep::Finished(hub.clone()), cx); + cx.emit(SignInEvent::Finished(hub)); + } + _ => self.publish(cx), + } + } + + /// Issues a new recovery key, because the one made with the keys was never confirmed. + pub fn issue_recovery_key(&mut self, cx: &mut Context) { + let SignInStep::RecoveryOffer { offer, .. } = &self.step else { + return; + }; + self.issue(offer.clone(), cx); + } + + /// Keeps the current recovery envelope without replacing it. + pub fn skip_recovery_offer(&mut self, cx: &mut Context) { + let SignInStep::RecoveryOffer { offer, .. } = &self.step else { + return; + }; + if let Err(error) = offer.check_current() { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + if !self.attempt.reissue_only { + self.publish(cx); + } + } + + fn issue(&mut self, offer: RecoveryKeyReissue, cx: &mut Context) { + let issuing = offer.issue(); + self.move_to(SignInStep::Issuing, cx); + self.work = Some(cx.spawn(async move |this, cx| { + let result = issuing.await; + this.update(cx, |session, cx| session.issued(offer, result, cx)) + .ok(); + })); + } + + fn issued( + &mut self, + offer: RecoveryKeyReissue, + result: Result, + cx: &mut Context, + ) { + self.work = None; + match result { + Ok(key) => self.move_to(SignInStep::RecoveryKey { key }, cx), + Err(error) => self.move_to( + SignInStep::RecoveryOffer { + offer, + problem: Some(error.into()), + }, + cx, + ), + } + } + + /// Opens (or makes) the account's keys with the vault passphrase of a browser sign-in. + pub fn set_vault_passphrase(&mut self, passphrase: Secret, cx: &mut Context) { + let (SignInStep::Passphrase { .. }, Some(hub)) = (&self.step, self.hub()) else { + return; + }; + self.move_to(SignInStep::Authenticating, cx); + let opening = self.sign_in.set_vault_passphrase(&hub, passphrase); + self.work = Some(cx.spawn(async move |this, cx| { + let result = opening.await; + this.update(cx, |session, cx| session.passphrase_set(result, cx)) + .ok(); + })); + } + + fn passphrase_set(&mut self, result: Result, cx: &mut Context) { + self.work = None; + match result { + Ok(keys) => self.keys_settled(keys, cx), + Err(error) => self.move_to( + SignInStep::Passphrase { + problem: Some(error.into()), + }, + cx, + ), + } + } + + /// Opens the account's keys with the recovery key, after the password did not. + pub fn recover(&mut self, recovery_key: Secret, cx: &mut Context) { + let (SignInStep::Recovery { .. }, Some(hub)) = (&self.step, self.hub()) else { + return; + }; + self.move_to(SignInStep::Authenticating, cx); + let recovering = self.sign_in.recover(&hub, recovery_key); + self.work = Some(cx.spawn(async move |this, cx| { + let result = recovering.await; + this.update(cx, |session, cx| session.recovered(result, cx)) + .ok(); + })); + } + + fn recovered(&mut self, result: Result<(), HubError>, cx: &mut Context) { + self.work = None; + match result { + Ok(()) => self.publish(cx), + Err(error) => self.move_to( + SignInStep::Recovery { + problem: Some(error.into()), + }, + cx, + ), + } + } + + /// Goes one step back; a browser sign-in in flight is cancelled. + pub fn back(&mut self, cx: &mut Context) { + let target = match &self.step { + SignInStep::Browser { .. } + | SignInStep::Authenticating + | SignInStep::Passphrase { .. } + | SignInStep::Recovery { .. } => { + self.work = None; + self.show_methods(None, cx); + return; + } + SignInStep::Method { .. } if self.attempt.fixed => return, + SignInStep::Checking => { + self.work = None; + SignInStep::Address { problem: None } + } + SignInStep::ConfirmFingerprint { .. } + | SignInStep::Mismatch { .. } + | SignInStep::Method { .. } => SignInStep::Address { problem: None }, + SignInStep::Address { .. } + | SignInStep::RecoveryKey { .. } + | SignInStep::RecoveryOffer { .. } + | SignInStep::Issuing + | SignInStep::Saving + | SignInStep::Finished(_) + | SignInStep::Failed(_) => return, + }; + self.move_to(target, cx); + } + + /// Adds the server to the settings in one edit. + fn publish(&mut self, cx: &mut Context) { + if let Some(login) = &self.attempt.login + && let Err(error) = login.check_current() + { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + let (Some(address), Some(trust), Some(info)) = ( + self.attempt.address.clone(), + self.attempt.trust, + self.attempt.info.as_ref(), + ) else { + return; + }; + let label = self + .attempt + .label + .clone() + .unwrap_or_else(|| info.name.clone()); + let record = HubRecord::new(address, trust, label); + let hub = record.id(); + self.move_to(SignInStep::Saving, cx); + let original = self.attempt.login.clone(); + let saving = self + .settings + .update_checked::(cx, move |settings| { + if let Some(original) = &original { + original + .check_current() + .map_err(nocterm_store::StoreError::backend)?; + } + // Signing in again to a known server keeps the colour its owner chose. + let color = settings.find(&record.id()).and_then(|known| known.color); + settings.upsert(record.with_color(color)); + Ok(()) + }); + self.work = Some(cx.spawn(async move |this, cx| { + let saved = saving.await; + this.update(cx, |session, cx| session.saved(&hub, saved.map(|_| ()), cx)) + .ok(); + })); + } + + fn saved( + &mut self, + hub: &HubId, + saved: Result<(), nocterm_store::StoreError>, + cx: &mut Context, + ) { + self.work = None; + if let Some(login) = &self.attempt.login + && let Err(error) = login.check_current() + { + return self.move_to(SignInStep::Failed(error.into()), cx); + } + match saved { + Ok(()) => { + self.move_to(SignInStep::Finished(hub.clone()), cx); + cx.emit(SignInEvent::Finished(hub.clone())); + } + Err(error) => { + // Only the original login may be discarded after the awaited save. + if let Some(login) = &self.attempt.login + && let Err(forget) = login.discard() + { + tracing::warn!(%hub,error=%forget,"credentials of the original unlisted login were kept"); + } + self.move_to(SignInStep::Failed(SignInError::Save(error)), cx); + } + } + } +} + +fn parse_inputs( + address: &str, + fingerprint: &str, +) -> Result<(HubAddress, Option), SignInError> { + let address = HubAddress::parse(address)?; + let fingerprint = fingerprint.trim(); + let expected = if fingerprint.is_empty() { + None + } else { + Some(Fingerprint::parse(fingerprint)?) + }; + Ok((address, expected)) +} + +/// Waits for a browser sign-in, reporting the address to open as soon as it is known. +async fn await_browser_sign_in( + browser: nocterm_hubs_model::HubFuture, + done: nocterm_hubs_model::HubFuture, + mut open: impl FnMut(String), +) -> Result { + let mut browser = browser.fuse(); + let mut done = done.fuse(); + loop { + futures::select! { + url = browser => { + if let Ok(url) = url { + open(url); + } + } + result = done => return result, + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/hubs/src/sign_in/tests.rs b/crates/hubs/src/sign_in/tests.rs new file mode 100644 index 00000000..405a77c4 --- /dev/null +++ b/crates/hubs/src/sign_in/tests.rs @@ -0,0 +1,780 @@ +use std::cell::RefCell; +use std::rc::Rc; + +use gpui::{Entity, TestAppContext}; +use nocterm_hubs_model::testing::{FakeCall, FakeHub, FakeOp}; +use nocterm_hubs_model::{Fingerprint, HubColor, HubError, HubSignIn as _, TrustPolicy}; +use nocterm_session::Secret; + +use crate::fixture::{ADDRESS, Fixture, fixture, fixture_with_failing_settings, id, server}; +use crate::{HubStatus, SignInError, SignInEvent, SignInSession, SignInStep, TrustNote}; + +fn hub() -> FakeHub { + FakeHub::new().with_server(server()) +} + +fn start(cx: &mut TestAppContext, fx: &Fixture) -> Entity { + fx.hubs.update(cx, |hubs, cx| hubs.start_sign_in(cx)) +} + +fn step(cx: &mut TestAppContext, session: &Entity) -> SignInStep { + session.read_with(cx, |session, _| session.step().clone()) +} + +fn submit(cx: &mut TestAppContext, session: &Entity, address: &str, print: &str) { + session.update(cx, |session, cx| session.submit_address(address, print, cx)); + cx.run_until_parked(); +} + +fn events( + cx: &mut TestAppContext, + session: &Entity, +) -> (Rc>>, gpui::Subscription) { + let seen = Rc::new(RefCell::new(Vec::new())); + let sink = Rc::clone(&seen); + let subscription = cx.update(|cx| { + cx.subscribe(session, move |_, event: &SignInEvent, _| { + sink.borrow_mut().push(event.clone()) + }) + }); + (seen, subscription) +} + +fn password(cx: &mut TestAppContext, session: &Entity, user: &str, secret: &str) { + session.update(cx, |session, cx| { + session.sign_in_with_password(user, Secret::new(secret), cx); + }); + cx.run_until_parked(); +} + +#[gpui::test] +async fn an_unknown_certificate_waits_for_the_user_then_pins_and_signs_in(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + let revision = cx.read(|cx| fx.settings.revision(cx)); + let (seen, _keep) = events(cx, &session); + + submit(cx, &session, ADDRESS, ""); + let SignInStep::ConfirmFingerprint { fingerprint, .. } = step(cx, &session) else { + panic!("expected the fingerprint to be confirmed"); + }; + assert_eq!(fingerprint, Fingerprint::from_bytes([7; 32])); + assert_eq!( + fx.hub.count(FakeOp::Connect), + 0, + "nothing is sent before the user trusts it" + ); + + session.update(cx, |session, cx| session.confirm_fingerprint(cx)); + cx.run_until_parked(); + let SignInStep::Method { + note, + password: offered, + providers, + hub_name, + .. + } = step(cx, &session) + else { + panic!("expected the sign-in methods"); + }; + assert_eq!(note, TrustNote::Confirmed); + assert!(offered); + assert_eq!(providers.len(), 1); + assert_eq!(hub_name, "Home hub"); + assert!( + fx.servers(cx).is_empty(), + "the server is not listed before signing in" + ); + + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::Finished(hub) if hub == id())); + assert_eq!(*seen.borrow(), [SignInEvent::Finished(id())]); + + let servers = fx.servers(cx); + assert_eq!(servers.len(), 1); + assert_eq!( + servers[0].trust, + TrustPolicy::Pinned(Fingerprint::from_bytes([7; 32])) + ); + assert_eq!(servers[0].label, "Home hub"); + assert_eq!( + cx.read(|cx| fx.settings.revision(cx)), + revision + 1, + "one edit" + ); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn a_typed_fingerprint_that_matches_skips_the_confirmation(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + &format!("https://{ADDRESS}"), + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + note: TrustNote::Pinned, + .. + } + )); +} + +#[gpui::test] +async fn a_typed_fingerprint_that_differs_is_refused_before_any_credentials_are_sent( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([1; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::Mismatch { typed, presented } + if typed == Fingerprint::from_bytes([1; 32]) && presented == Fingerprint::from_bytes([7; 32]) + )); + assert_eq!(fx.hub.count(FakeOp::Connect), 0); + session.update(cx, |session, cx| session.back(cx)); + assert!(matches!( + step(cx, &session), + SignInStep::Address { problem: None } + )); +} + +#[gpui::test] +async fn a_publicly_trusted_certificate_needs_no_confirmation(cx: &mut TestAppContext) { + let hub = FakeHub::new().with_server(server().publicly_trusted()); + let fx = fixture(cx, hub, Vec::new()); + let session = start(cx, &fx); + submit(cx, &session, ADDRESS, ""); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + note: TrustNote::PublicCa, + .. + } + )); + password(cx, &session, "ada", "secret"); + assert_eq!(fx.servers(cx)[0].trust, TrustPolicy::PublicCa); +} + +#[gpui::test] +async fn a_malformed_address_or_fingerprint_is_reported_on_the_address_step( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit(cx, &session, "http://hub.home", ""); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::Address(_)) + } + )); + submit(cx, &session, ADDRESS, "xyz"); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::Fingerprint(_)) + } + )); + assert_eq!(fx.hub.calls(), []); +} + +#[gpui::test] +async fn an_unreachable_server_returns_to_the_address_step(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit(cx, &session, "nowhere.example", ""); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::Hub(HubError::Unreachable { .. })) + } + )); +} + +#[gpui::test] +async fn a_server_without_an_owner_cannot_be_signed_in_to(cx: &mut TestAppContext) { + let hub = hub(); + hub.edit_server(&crate::fixture::address(), |server| { + server.bootstrapped = false + }); + let fx = fixture(cx, hub, Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::NotInitialized) + } + )); +} + +#[gpui::test] +async fn a_wrong_password_returns_to_the_methods_and_changes_nothing(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + let revision = cx.read(|cx| fx.settings.revision(cx)); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + password(cx, &session, "ada", "wrong"); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + problem: Some(SignInError::Hub(HubError::InvalidCredentials)), + .. + } + )); + assert!(fx.servers(cx).is_empty()); + assert_eq!(cx.read(|cx| fx.settings.revision(cx)), revision); + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); +} + +#[gpui::test] +async fn a_device_that_is_already_signed_in_is_listed_without_asking_for_a_password( + cx: &mut TestAppContext, +) { + let hub = hub(); + hub.sign_in_as(&id(), "ada"); + let fx = fixture(cx, hub, Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.hub.count(FakeOp::Password), 0); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn the_browser_sign_in_opens_the_address_the_hub_gives_and_finishes_in_the_browser( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + let (seen, _keep) = events(cx, &session); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + let opened = seen.borrow().clone(); + assert_eq!(opened.len(), 1); + let SignInEvent::OpenBrowser(url) = &opened[0] else { + panic!("expected the browser to be opened"); + }; + assert!(url.contains("corp")); + assert!(matches!( + step(cx, &session), + SignInStep::Browser { url: Some(shown), .. } if shown == *url + )); + + fx.hub.complete_oidc("ada"); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn going_back_from_the_browser_cancels_the_sign_in(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + session.update(cx, |session, cx| session.back(cx)); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Method { problem: None, .. } + )); + assert_eq!(fx.hub.cancelled_oidc(), 1); + assert!(fx.servers(cx).is_empty()); +} + +#[gpui::test] +async fn a_browser_sign_in_that_fails_returns_to_the_methods(cx: &mut TestAppContext) { + let fx = fixture(cx, hub(), Vec::new()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + fx.hub.fail_oidc(HubError::SignInTimedOut); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + problem: Some(SignInError::Hub(HubError::SignInTimedOut)), + .. + } + )); +} + +#[gpui::test] +async fn credentials_made_for_a_server_that_could_not_be_saved_are_erased(cx: &mut TestAppContext) { + let fx = fixture_with_failing_settings(cx, hub()); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + password(cx, &session, "ada", "secret"); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Save(_)) + )); + assert!(!fx.hub.is_signed_in(&id())); + assert!(fx.hub.calls().contains(&FakeCall::Forget(id()))); + assert!(fx.servers(cx).is_empty()); +} + +#[gpui::test] +async fn signing_in_again_to_a_revoked_server_starts_at_the_methods(cx: &mut TestAppContext) { + let hub = hub(); + hub.sign_in_as(&id(), "ada"); + hub.revoke_device(&id()); + let fx = fixture(cx, hub, vec![crate::fixture::pinned_record()]); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Revoked)); + + let session = fx + .hubs + .update(cx, |hubs, cx| hubs.start_sign_in_for(&id(), cx)) + .expect("a known hub"); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Method { + note: TrustNote::Pinned, + .. + } + )); + assert!(!session.read_with(cx, |session, _| session.address_editable())); + session.update(cx, |session, cx| session.back(cx)); + assert!( + matches!(step(cx, &session), SignInStep::Method { .. }), + "the address stays fixed" + ); + + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx)[0].label, "Home", "a known label is kept"); + cx.run_until_parked(); + assert_eq!(fx.status(cx), Some(HubStatus::Online)); +} + +#[gpui::test] +async fn a_known_address_is_refused_so_a_changed_certificate_is_never_pinned_again( + cx: &mut TestAppContext, +) { + let hub = hub(); + let fx = fixture(cx, hub, vec![crate::fixture::pinned_record()]); + cx.run_until_parked(); + let pinned = fx.servers(cx); + fx.hub.edit_server(&crate::fixture::address(), |server| { + server.certificate = Fingerprint::from_bytes([9; 32]); + }); + let probes = fx.hub.count(FakeOp::Probe); + + let session = start(cx, &fx); + submit(cx, &session, ADDRESS, ""); + + assert!( + matches!( + step(cx, &session), + SignInStep::Address { + problem: Some(SignInError::AlreadyKnown(hub)) + } if hub == id() + ), + "the dialog does not offer to trust the new certificate" + ); + assert_eq!(fx.hub.count(FakeOp::Probe), probes, "nothing was asked"); + assert_eq!(fx.servers(cx), pinned, "the pinned record is unchanged"); +} + +#[gpui::test] +async fn a_resumed_sign_in_cannot_be_pointed_at_another_server(cx: &mut TestAppContext) { + let hub = hub(); + let fx = fixture(cx, hub, vec![crate::fixture::pinned_record()]); + cx.run_until_parked(); + fx.hub + .edit_server(&crate::fixture::address(), |server| server.online = false); + let session = fx + .hubs + .update(cx, |hubs, cx| hubs.start_sign_in_for(&id(), cx)) + .expect("a known hub"); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Address { .. })); + let (probes, connects) = (fx.hub.count(FakeOp::Probe), fx.hub.count(FakeOp::Connect)); + + submit(cx, &session, "other.example:7443", ""); + + assert!(matches!(step(cx, &session), SignInStep::Address { .. })); + assert_eq!(fx.hub.count(FakeOp::Probe), probes); + assert_eq!(fx.hub.count(FakeOp::Connect), connects); +} + +#[gpui::test] +async fn signing_in_again_keeps_the_colour_chosen_for_the_server(cx: &mut TestAppContext) { + let hub = hub(); + hub.sign_in_as(&id(), "ada"); + hub.revoke_device(&id()); + let marked = crate::fixture::pinned_record().with_color(Some(HubColor::Blue)); + let fx = fixture(cx, hub, vec![marked]); + cx.run_until_parked(); + + let session = fx + .hubs + .update(cx, |hubs, cx| hubs.start_sign_in_for(&id(), cx)) + .expect("a known hub"); + cx.run_until_parked(); + password(cx, &session, "ada", "secret"); + + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx)[0].color, Some(HubColor::Blue)); +} + +fn keyed_hub() -> FakeHub { + FakeHub::new().with_server(server().keyed()) +} + +fn at_methods(cx: &mut TestAppContext, fx: &Fixture) -> Entity { + let session = start(cx, fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + session +} + +#[gpui::test] +async fn a_first_sign_in_shows_the_recovery_key_once_and_lists_the_server_when_it_is_acknowledged( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + + let SignInStep::RecoveryKey { key } = step(cx, &session) else { + panic!("expected the recovery key"); + }; + assert_eq!(key.key(), &Secret::new("recovery-ada")); + assert!( + fx.servers(cx).is_empty(), + "the server waits for the key to be written down" + ); + + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn a_reset_password_asks_for_the_recovery_key_and_refuses_a_wrong_one( + cx: &mut TestAppContext, +) { + let hub = FakeHub::new().with_server(server().with_keyset("ada", "old", "recovery-key")); + let fx = fixture(cx, hub, Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + assert!(matches!( + step(cx, &session), + SignInStep::Recovery { problem: None } + )); + + session.update(cx, |session, cx| { + session.recover(Secret::new("not it"), cx); + }); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Recovery { + problem: Some(SignInError::Hub(HubError::RecoveryKeyInvalid)) + } + )); + assert!(fx.servers(cx).is_empty()); + + session.update(cx, |session, cx| { + session.recover(Secret::new("recovery-key"), cx); + }); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); +} + +#[gpui::test] +async fn a_browser_sign_in_asks_for_a_vault_passphrase_before_listing_the_server( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + session.update(cx, |session, cx| session.sign_in_with_provider("corp", cx)); + cx.run_until_parked(); + fx.hub.complete_oidc("ada"); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Passphrase { problem: None } + )); + + session.update(cx, |session, cx| { + session.set_vault_passphrase(Secret::new("a long passphrase"), cx); + }); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); +} + +#[gpui::test] +async fn the_key_steps_do_nothing_out_of_turn(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + session.update(cx, |session, cx| { + session.acknowledge_recovery_key(cx); + session.issue_recovery_key(cx); + session.skip_recovery_offer(cx); + session.recover(Secret::new("recovery-ada"), cx); + session.set_vault_passphrase(Secret::new("a long passphrase"), cx); + }); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Method { .. })); + assert_eq!(fx.hub.count(FakeOp::Recover), 0); + assert_eq!(fx.hub.count(FakeOp::ReissueRecoveryKey), 0); + assert_eq!(fx.hub.count(FakeOp::SetVaultPassphrase), 0); +} + +/// The sign-in of a first account, closed on the recovery-key step: the server is not listed and +/// the key was shown but never confirmed. +fn closed_on_the_recovery_key(cx: &mut TestAppContext, fx: &Fixture) { + let session = at_methods(cx, fx); + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); + drop(session); + cx.run_until_parked(); +} + +fn issue(cx: &mut TestAppContext, session: &Entity) { + session.update(cx, |session, cx| session.issue_recovery_key(cx)); + cx.run_until_parked(); +} + +#[gpui::test] +async fn acknowledging_the_recovery_key_confirms_it_so_it_is_not_offered_again( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + assert!(fx.hub.recovery_key_unconfirmed(&id())); + + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + + assert!(!fx.hub.recovery_key_unconfirmed(&id())); +} + +#[gpui::test] +async fn a_sign_in_closed_on_the_recovery_key_offers_a_new_one_next_time(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + assert!(fx.servers(cx).is_empty()); + + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + assert!(matches!( + step(cx, &session), + SignInStep::RecoveryOffer { problem: None, .. } + )); + assert!( + fx.servers(cx).is_empty(), + "nothing is listed before it is settled" + ); + + issue(cx, &session); + let SignInStep::RecoveryKey { key } = step(cx, &session) else { + panic!("expected the new recovery key"); + }; + assert_eq!( + key.key(), + &Secret::new("recovery-ada+"), + "the old key was replaced" + ); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); + assert!(!fx.hub.recovery_key_unconfirmed(&id())); +} + +#[gpui::test] +async fn a_password_sign_in_that_finds_the_key_unconfirmed_offers_a_new_one( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + fx.hub.expire_login(&id()); + + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + + assert!(matches!( + step(cx, &session), + SignInStep::RecoveryOffer { problem: None, .. } + )); +} + +#[gpui::test] +async fn leaving_the_offer_for_later_lists_the_server_and_keeps_the_key_unconfirmed( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + + session.update(cx, |session, cx| session.skip_recovery_offer(cx)); + cx.run_until_parked(); + + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(fx.servers(cx).len(), 1); + assert!( + fx.hubs + .read_with(cx, |hubs, _| hubs.recovery_key_unconfirmed(&id())) + ); +} + +#[gpui::test] +async fn a_failed_reissue_returns_to_the_offer_and_can_be_tried_again(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + let session = start(cx, &fx); + submit( + cx, + &session, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + fx.hub + .fail_next(FakeOp::ReissueRecoveryKey, HubError::KeysLocked); + + issue(cx, &session); + assert!(matches!( + step(cx, &session), + SignInStep::RecoveryOffer { + problem: Some(SignInError::Hub(HubError::KeysLocked)), + .. + } + )); + + issue(cx, &session); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); +} + +#[gpui::test] +async fn the_servers_page_can_issue_a_new_key_for_a_listed_server(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + closed_on_the_recovery_key(cx, &fx); + let first = start(cx, &fx); + submit( + cx, + &first, + ADDRESS, + &Fingerprint::from_bytes([7; 32]).to_string(), + ); + // Listed, with the key shown but not confirmed. + first.update(cx, |session, cx| session.skip_recovery_offer(cx)); + cx.run_until_parked(); + assert_eq!(fx.servers(cx).len(), 1); + assert!( + fx.hubs + .read_with(cx, |hubs, _| hubs.recovery_key_unconfirmed(&id())) + ); + + let session = fx + .hubs + .update(cx, |hubs, cx| { + hubs.start_recovery_key_reissue(&id(), fx.hub.recovery_key_offer(&id()).unwrap(), cx) + }) + .expect("a listed server"); + cx.run_until_parked(); + assert!(session.read_with(cx, |session, _| session.reissue_only())); + let (seen, _keep) = events(cx, &session); + let SignInStep::RecoveryKey { key } = step(cx, &session) else { + panic!("expected the new recovery key"); + }; + assert_eq!(key.key(), &Secret::new("recovery-ada+")); + + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Finished(_))); + assert_eq!(*seen.borrow(), vec![SignInEvent::Finished(id())]); + assert!( + !fx.hubs + .read_with(cx, |hubs, _| hubs.recovery_key_unconfirmed(&id())) + ); +} + +#[gpui::test] +async fn no_key_is_issued_for_a_server_that_is_not_listed(cx: &mut TestAppContext) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = fx.hubs.update(cx, |hubs, cx| { + hubs.start_recovery_key_reissue(&id(), fx.hub.recovery_key_offer(&id()).unwrap(), cx) + }); + assert!(session.is_none()); + assert_eq!(fx.hub.count(FakeOp::ReissueRecoveryKey), 0); +} + +mod source_binding; diff --git a/crates/hubs/src/sign_in/tests/source_binding.rs b/crates/hubs/src/sign_in/tests/source_binding.rs new file mode 100644 index 00000000..630d607e --- /dev/null +++ b/crates/hubs/src/sign_in/tests/source_binding.rs @@ -0,0 +1,78 @@ +use super::*; + +#[gpui::test] +async fn an_earlier_recovery_card_cannot_acknowledge_the_replacement_accounts_key( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + assert!(matches!(step(cx, &session), SignInStep::RecoveryKey { .. })); + fx.hub.sign_in_as(&id(), "another account"); + fx.hub.leave_recovery_key_unconfirmed(&id()); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Hub(HubError::Stale)) + )); + assert!(fx.servers(cx).is_empty()); + assert!(fx.hub.recovery_key_unconfirmed(&id())); +} + +#[gpui::test] +async fn a_failed_delayed_settings_write_never_discards_a_replacement_login( + cx: &mut TestAppContext, +) { + for replacements in [vec!["ada"], vec!["bob", "ada"]] { + let (writer, release, writes) = nocterm_store::test_support::gated_writer().into_parts(); + let fx = crate::fixture::fixture_with_writer(cx, hub(), Vec::new(), writer); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Saving)); + assert_eq!(writes.lock().unwrap().len(), 1); + for account in replacements { + fx.hub.sign_in_as(&id(), account); + } + release + .send(Err(nocterm_store::StoreError::backend( + std::io::Error::from(std::io::ErrorKind::PermissionDenied), + ))) + .unwrap(); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Hub(HubError::Stale)) + )); + assert!(fx.hub.is_signed_in(&id())); + assert!(!fx.hub.calls().contains(&FakeCall::Forget(id()))); + assert!(fx.servers(cx).is_empty()); + } +} + +#[gpui::test] +async fn a_recovery_acknowledgement_waits_for_durable_completion_and_rechecks_its_recipient( + cx: &mut TestAppContext, +) { + let fx = fixture(cx, keyed_hub(), Vec::new()); + let session = at_methods(cx, &fx); + password(cx, &session, "ada", "secret"); + let release = fx.hub.hold(FakeOp::ConfirmRecoveryKey); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + cx.run_until_parked(); + assert!(matches!(step(cx, &session), SignInStep::Saving)); + assert!(fx.servers(cx).is_empty()); + session.update(cx, |session, cx| session.acknowledge_recovery_key(cx)); + assert_eq!(fx.hub.count(FakeOp::ConfirmRecoveryKey), 1); + fx.hub.sign_in_as(&id(), "replacement"); + fx.hub.leave_recovery_key_unconfirmed(&id()); + release.release(); + cx.run_until_parked(); + assert!(matches!( + step(cx, &session), + SignInStep::Failed(SignInError::Hub(HubError::Stale)) + )); + assert!(fx.servers(cx).is_empty()); + assert!(fx.hub.recovery_key_unconfirmed(&id())); +} diff --git a/crates/hubs/src/status.rs b/crates/hubs/src/status.rs new file mode 100644 index 00000000..ae52815d --- /dev/null +++ b/crates/hubs/src/status.rs @@ -0,0 +1,190 @@ +//! What a known hub is doing, and what to make of the outcome of reaching it. + +use std::time::{Duration, Instant}; + +use nocterm_hubs_model::{Fingerprint, HubError, HubInfo}; + +/// The state of one known hub, shared by every window. +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum HubStatus { + /// Credentials are missing, so the user has to sign in. + SignedOut, + /// A connection is being made. + Connecting, + /// Connected with working credentials. + Online, + /// The hub did not answer; the next attempt starts at `retry_at`. + Offline { retry_at: Instant }, + /// The hub revoked this device; its credentials are gone. + Revoked, + /// The hub's certificate is not the trusted one. Never retried, never accepted. + TrustFailed { + /// The pinned fingerprint; `None` when a public authority was trusted instead. + pinned: Option, + presented: Fingerprint, + }, + /// The hub and this client speak different protocol versions. + UpgradeRequired { hub: u32, client: u32 }, +} + +impl HubStatus { + pub const fn is_online(&self) -> bool { + matches!(self, Self::Online) + } + + /// Signing in again is the way forward. + pub const fn needs_sign_in(&self) -> bool { + matches!(self, Self::SignedOut | Self::Revoked) + } +} + +/// What reaching a hub leads to. +#[derive(Debug, PartialEq, Eq)] +pub(crate) enum Settled { + /// A resting status. + Status(HubStatus), + /// The hub could not be reached for now; try again later. + Retry, +} + +/// Reads the outcome of connecting to a hub. +pub(crate) fn settle_connect(result: &Result) -> Settled { + match result { + Ok(info) if info.signed_in => Settled::Status(HubStatus::Online), + Ok(_) => Settled::Status(HubStatus::SignedOut), + Err(error) => settle_failure(error).unwrap_or(Settled::Retry), + } +} + +/// Reads a failure of a call on a hub that was online; `None` leaves the status alone. +pub(crate) fn settle_failure(error: &HubError) -> Option { + Some(match error { + HubError::Unreachable { .. } => Settled::Retry, + HubError::CertificateChanged { + pinned, presented, .. + } => Settled::Status(HubStatus::TrustFailed { + pinned: Some(*pinned), + presented: *presented, + }), + HubError::CertificateUntrusted { presented, .. } => { + Settled::Status(HubStatus::TrustFailed { + pinned: None, + presented: *presented, + }) + } + HubError::UpgradeRequired { hub, client } => Settled::Status(HubStatus::UpgradeRequired { + hub: *hub, + client: *client, + }), + HubError::NotSignedIn => Settled::Status(HubStatus::SignedOut), + HubError::Revoked => Settled::Status(HubStatus::Revoked), + // Credentials that cannot be kept or any other trouble of the hub client: the hub is + // not usable now, and trying again later is the harmless answer. + HubError::Credentials { .. } | HubError::Remote { .. } => Settled::Retry, + HubError::InvalidCredentials + | HubError::MethodUnavailable + | HubError::SignInTimedOut + | HubError::SignInCancelled + | HubError::PermissionDenied + | HubError::NotFound + | HubError::Stale + | HubError::RecoveryKeyInvalid + | HubError::InvalidVaultPassphrase + | HubError::KeysetDamaged { .. } + | HubError::UnlockSourceMismatch { .. } + | HubError::KeysLocked + | HubError::NoRecoveryPending => return None, + }) +} + +/// Waits between attempts to reach a hub that does not answer; the last one repeats. +const RETRY_DELAYS: [Duration; 5] = [ + Duration::from_secs(2), + Duration::from_secs(5), + Duration::from_secs(10), + Duration::from_secs(30), + Duration::from_secs(60), +]; + +/// How long to wait after `failures` unsuccessful attempts in a row (at least one). +pub(crate) fn retry_delay(failures: u32) -> Duration { + let index = usize::try_from(failures.saturating_sub(1)).unwrap_or(usize::MAX); + RETRY_DELAYS[index.min(RETRY_DELAYS.len() - 1)] +} + +#[cfg(test)] +mod tests { + use nocterm_core::error::SharedCause; + use nocterm_hubs_model::HubAddress; + + use super::*; + + fn info(signed_in: bool) -> HubInfo { + HubInfo { + name: "Home".into(), + password_login: true, + oidc_providers: Vec::new(), + bootstrapped: true, + signed_in, + } + } + + fn unreachable() -> HubError { + #[derive(Debug, thiserror::Error)] + #[error("down")] + struct Down; + HubError::Unreachable { + address: HubAddress::parse("hub.home").unwrap(), + cause: SharedCause::new(Down), + } + } + + #[test] + fn a_signed_in_device_is_online_and_an_unknown_one_is_signed_out() { + assert_eq!( + settle_connect(&Ok(info(true))), + Settled::Status(HubStatus::Online) + ); + assert_eq!( + settle_connect(&Ok(info(false))), + Settled::Status(HubStatus::SignedOut) + ); + } + + #[test] + fn an_unreachable_hub_is_retried_but_a_changed_certificate_never_is() { + assert_eq!(settle_connect(&Err(unreachable())), Settled::Retry); + let changed = HubError::CertificateChanged { + address: HubAddress::parse("hub.home").unwrap(), + pinned: Fingerprint::from_bytes([1; 32]), + presented: Fingerprint::from_bytes([2; 32]), + }; + assert_eq!( + settle_connect(&Err(changed)), + Settled::Status(HubStatus::TrustFailed { + pinned: Some(Fingerprint::from_bytes([1; 32])), + presented: Fingerprint::from_bytes([2; 32]), + }) + ); + } + + #[test] + fn revoked_and_outdated_hubs_rest_without_retries() { + assert_eq!( + settle_connect(&Err(HubError::Revoked)), + Settled::Status(HubStatus::Revoked) + ); + assert_eq!( + settle_connect(&Err(HubError::UpgradeRequired { hub: 2, client: 1 })), + Settled::Status(HubStatus::UpgradeRequired { hub: 2, client: 1 }) + ); + assert!(settle_failure(&HubError::PermissionDenied).is_none()); + } + + #[test] + fn retries_back_off_and_the_last_delay_repeats() { + let delays: Vec = (1..=7).map(|n| retry_delay(n).as_secs()).collect(); + assert_eq!(delays, [2, 5, 10, 30, 60, 60, 60]); + assert_eq!(retry_delay(0).as_secs(), 2); + } +} diff --git a/crates/keymap-ui/src/render.rs b/crates/keymap-ui/src/render.rs index 6387a02e..d9b6ce21 100644 --- a/crates/keymap-ui/src/render.rs +++ b/crates/keymap-ui/src/render.rs @@ -17,7 +17,7 @@ use nocterm_keymap::Source; use nocterm_ui::{IconName, form}; use crate::{ - rows::{Row, matches, rows}, + rows::{Query, Row}, view::{KeymapView, Recording}, }; @@ -230,13 +230,26 @@ impl KeymapView { impl Render for KeymapView { fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { - let query = self.search.read(cx).value().to_string(); - let listed: Vec = rows(&self.keymap, cx) - .into_iter() - .filter(|row| matches(row, &query)) + // Local actions may render before deferred owner observations are delivered. + // Read the semantic key here; unchanged frames still reuse all prepared rows. + let revision = self.keymap.revision(cx); + if revision != self.rows_revision { + self.rows = std::rc::Rc::new(crate::rows::rows(&self.keymap, cx)); + self.rows_revision = revision; + } + let query = Query::new(&self.search.read(cx).value()); + let listed: Vec = self + .rows + .iter() + .filter(|row| query.matches(row)) + .cloned() .collect(); let count = listed.len(); - let file_error = self.keymap.error(cx).map(ToString::to_string); + let file_error = self + .keymap + .error(cx) + .map(ToString::to_string) + .or_else(|| self.keymap.publication_error(cx).map(ToString::to_string)); let mut table = v_flex().id("keymap-table").test_support().w_full(); table = table.child(Self::column_titles(cx)); for (index, row) in listed.into_iter().enumerate() { diff --git a/crates/keymap-ui/src/rows.rs b/crates/keymap-ui/src/rows.rs index 9532121b..313f3204 100644 --- a/crates/keymap-ui/src/rows.rs +++ b/crates/keymap-ui/src/rows.rs @@ -35,6 +35,7 @@ pub(crate) fn default_context(action: &str, defaults: &[Binding]) -> Option Vec { let docs = cx.action_documentation(); + let customized = keymap.customized_actions(cx); let row = |action: &str, binding: Option<&Binding>| Row { action: action.to_owned(), label: humanize(action).into(), @@ -46,9 +47,13 @@ pub(crate) fn rows(keymap: &KeymapHandle, cx: &App) -> Vec { keystrokes: binding.map(|binding| binding.keystrokes.clone()), context: binding.and_then(|binding| binding.context.clone()), source: binding.map(|binding| binding.source), - customized: keymap.is_customized(action, cx), + customized: customized.contains(action), }; let bindings = keymap.bindings(cx); + let bound: std::collections::BTreeSet<_> = bindings + .iter() + .map(|binding| binding.action.as_str()) + .collect(); let mut rows: Vec = bindings .iter() .filter(|binding| offered(&binding.action)) @@ -59,7 +64,10 @@ pub(crate) fn rows(keymap: &KeymapHandle, cx: &App) -> Vec { .iter() .copied() .filter(|name| offered(name)) - .filter(|name| !bindings.iter().any(|binding| binding.action == *name)) + // A command that cannot be built without arguments is sent by a menu; a binding to it + // would be skipped when the keymap loads. + .filter(|name| cx.build_action(name, None).is_ok()) + .filter(|name| !bound.contains(name)) .collect(); unbound.sort_unstable(); unbound.dedup(); @@ -72,38 +80,56 @@ pub(crate) fn rows(keymap: &KeymapHandle, cx: &App) -> Vec { /// name, description, shortcut or context, ignoring case. A query that reads /// as a shortcut (`ctrl-shift-p`, `shift-ctrl-p`) also matches the shortcut /// however its modifiers are ordered. -pub(crate) fn matches(row: &Row, query: &str) -> bool { - let query = query.trim().to_lowercase(); - if query.is_empty() { - return true; +pub(crate) struct Query { + text: String, + keys: Option, +} +impl Query { + pub(crate) fn new(query: &str) -> Self { + let text = query.trim().to_lowercase(); + let keys = text + .contains('-') + .then(|| normalize(&text).ok()) + .flatten() + .map(|keys| keys.to_lowercase()); + Self { text, keys } } - if query.contains('-') - && let (Ok(wanted), Some(keys)) = (normalize(&query), &row.keystrokes) - && normalize(keys).is_ok_and(|keys| keys.to_lowercase() == wanted.to_lowercase()) - { - return true; + pub(crate) fn matches(&self, row: &Row) -> bool { + if self.text.is_empty() { + return true; + } + if let (Some(wanted), Some(keys)) = (&self.keys, &row.keystrokes) + && normalize(keys).is_ok_and(|keys| keys.to_lowercase() == *wanted) + { + return true; + } + let haystack = format!( + "{} {} {} {} {}", + row.label, + row.action, + row.description, + row.keystrokes.as_deref().unwrap_or("unbound"), + row.context.as_deref().unwrap_or_default(), + ) + .to_lowercase(); + self.text + .split_whitespace() + .all(|word| haystack.contains(word)) } - let haystack = format!( - "{} {} {} {} {}", - row.label, - row.action, - row.description, - row.keystrokes.as_deref().unwrap_or("unbound"), - row.context.as_deref().unwrap_or_default(), - ) - .to_lowercase(); - query.split_whitespace().all(|word| haystack.contains(word)) } #[cfg(test)] mod tests { use super::*; + fn matches(row: &Row, query: &str) -> bool { + Query::new(query).matches(row) + } fn row(keys: Option<&str>) -> Row { Row { - action: "workspace::SwapSides".into(), - label: "Workspace: Swap Sides".into(), - description: "Put the sidebar and the AI panel on each other's side".into(), + action: "workspace::ToggleSidebar".into(), + label: "Workspace: Toggle Sidebar".into(), + description: "Show or hide the sidebar".into(), keystrokes: keys.map(str::to_owned), context: Some("Workspace".into()), source: Some(Source::Default), @@ -114,11 +140,11 @@ mod tests { #[test] fn search_matches_words_anywhere_and_shortcuts_in_any_order() { let bound = row(Some("ctrl-shift-e")); - assert!(matches(&bound, "swap")); - assert!(matches(&bound, "SIDEBAR panel")); + assert!(matches(&bound, "toggle")); + assert!(matches(&bound, "SIDEBAR hide")); assert!(matches(&bound, "shift-ctrl-e")); assert!(matches(&bound, "ctrl-shift")); - assert!(!matches(&bound, "swap terminal")); + assert!(!matches(&bound, "toggle terminal")); assert!(matches(&row(None), "unbound")); } diff --git a/crates/keymap-ui/src/tests.rs b/crates/keymap-ui/src/tests.rs index aeaa604d..7847f230 100644 --- a/crates/keymap-ui/src/tests.rs +++ b/crates/keymap-ui/src/tests.rs @@ -5,11 +5,22 @@ use nocterm_store::SettingsConfig; use crate::KeymapView; +/// Commands declared for the tests only; kept in a module so the actions reference, which +/// reads top-level declarations, does not list them. +mod declared { + gpui_kit::actions!(workspace, [KeyBindable]); + + /// A command only a menu sends: it cannot be built from a keymap entry. + #[derive(Clone, Debug, PartialEq, gpui_kit::Action)] + #[action(namespace = workspace, no_json)] + pub(super) struct MenuOnly; +} + const DEFAULTS: &str = r#" [[section]] context = "Workspace" [section.bindings] -"ctrl-e" = "workspace::SwapSides" +"ctrl-e" = "workspace::ToggleSidebar" "#; fn setup(cx: &mut TestAppContext) -> (gpui_kit::AnyWindowHandle, Entity, KeymapHandle) { @@ -46,7 +57,7 @@ fn pressing_keys_on_a_shortcut_rebinds_it_and_reset_restores_it(cx: &mut TestApp cx.update_window(handle, |_, window, cx| { window.render_frame(cx); window.click("keymap-search", cx); - window.input("swap sides", cx); + window.input("toggle sidebar", cx); window.render_frame(cx); assert!( window.try_find(("keymap-row", 1usize)).is_none(), @@ -59,13 +70,13 @@ fn pressing_keys_on_a_shortcut_rebinds_it_and_reset_restores_it(cx: &mut TestApp }) .unwrap(); assert!(bound(&keymap, cx, "ctrl-e").is_empty()); - assert_eq!(bound(&keymap, cx, "alt-s"), ["workspace::SwapSides"]); + assert_eq!(bound(&keymap, cx, "alt-s"), ["workspace::ToggleSidebar"]); cx.update_window(handle, |_, window, cx| { window.click(("keymap-reset", 0usize), cx); window.render_frame(cx); }) .unwrap(); - assert_eq!(bound(&keymap, cx, "ctrl-e"), ["workspace::SwapSides"]); + assert_eq!(bound(&keymap, cx, "ctrl-e"), ["workspace::ToggleSidebar"]); assert!(bound(&keymap, cx, "alt-s").is_empty()); } @@ -84,7 +95,7 @@ fn escape_cancels_recording_and_find_by_keys_fills_the_search(cx: &mut TestAppCo window.press("alt-s", cx); }) .unwrap(); - assert_eq!(bound(&keymap, cx, "ctrl-e"), ["workspace::SwapSides"]); + assert_eq!(bound(&keymap, cx, "ctrl-e"), ["workspace::ToggleSidebar"]); assert!(bound(&keymap, cx, "alt-s").is_empty()); } @@ -103,7 +114,7 @@ fn search_swap(handle: gpui_kit::AnyWindowHandle, cx: &mut TestAppContext) { cx.update_window(handle, |_, window, cx| { window.render_frame(cx); window.click("keymap-search", cx); - window.input("swap sides", cx); + window.input("toggle sidebar", cx); window.render_frame(cx); }) .unwrap(); @@ -168,5 +179,18 @@ fn shared_pages_refresh_survive_window_closure_and_release_recording(cx: &mut Te .unwrap(); close_page(second, second_view, cx); close_page(third, third_view, cx); - assert_eq!(bound(&keymap, cx, "alt-t"), ["workspace::SwapSides"]); + assert_eq!(bound(&keymap, cx, "alt-t"), ["workspace::ToggleSidebar"]); +} + +#[gpui_kit::test] +fn commands_a_keymap_entry_cannot_build_are_not_offered_for_binding(cx: &mut TestAppContext) { + let (_handle, _view, keymap) = setup(cx); + let actions: Vec = cx.update(|cx| { + crate::rows::rows(&keymap, cx) + .into_iter() + .map(|row| row.action) + .collect() + }); + assert!(actions.contains(&"workspace::KeyBindable".to_owned())); + assert!(!actions.contains(&"workspace::MenuOnly".to_owned())); } diff --git a/crates/keymap-ui/src/view.rs b/crates/keymap-ui/src/view.rs index f53fb2ae..08313dae 100644 --- a/crates/keymap-ui/src/view.rs +++ b/crates/keymap-ui/src/view.rs @@ -35,6 +35,8 @@ pub struct KeymapView { _subscription: Subscription, keymap: KeymapHandle, _keymap_subscription: Subscription, + rows: std::rc::Rc>, + rows_revision: u64, } impl KeymapView { @@ -51,6 +53,8 @@ impl KeymapView { let keymap_subscription = keymap.observe(cx, move |cx| { let _ = view.update(cx, |_, cx| cx.notify()); }); + let rows = std::rc::Rc::new(crate::rows::rows(&keymap, cx)); + let rows_revision = keymap.revision(cx); Self { keymap, _keymap_subscription: keymap_subscription, @@ -60,6 +64,8 @@ impl KeymapView { message: None, interceptor: None, _subscription: subscription, + rows, + rows_revision, } } diff --git a/crates/keymap/Cargo.toml b/crates/keymap/Cargo.toml index 4f92ee59..30da18e7 100644 --- a/crates/keymap/Cargo.toml +++ b/crates/keymap/Cargo.toml @@ -11,6 +11,7 @@ layer = "ui" [dependencies] nocterm-core.workspace = true +nocterm-store.workspace = true gpui-kit.workspace = true serde.workspace = true toml.workspace = true diff --git a/crates/keymap/src/error.rs b/crates/keymap/src/error.rs index 5d853469..d092cbc0 100644 --- a/crates/keymap/src/error.rs +++ b/crates/keymap/src/error.rs @@ -14,4 +14,6 @@ pub enum KeymapError { }, #[error(transparent)] Persistence(#[from] nocterm_core::SharedError), + #[error(transparent)] + Store(#[from] nocterm_store::StoreError), } diff --git a/crates/keymap/src/file.rs b/crates/keymap/src/file.rs index 0270534a..1055d14d 100644 --- a/crates/keymap/src/file.rs +++ b/crates/keymap/src/file.rs @@ -80,7 +80,7 @@ fn key(context: &Option, keystrokes: &str) -> (Option, String) { impl KeymapFile { /// The bindings for this platform, in file order. - fn here(&self) -> impl Iterator, &String, &String)> { + pub(crate) fn here(&self) -> impl Iterator, &String, &String)> { self.sections .iter() .filter(|section| section.applies_here()) diff --git a/crates/keymap/src/lib.rs b/crates/keymap/src/lib.rs index eb8559c9..18c2b7a0 100644 --- a/crates/keymap/src/lib.rs +++ b/crates/keymap/src/lib.rs @@ -2,8 +2,9 @@ //! //! Owns: keymap format, default/user merge, override persistence and GPUI installation. //! Does not own: action handlers, component bindings or the Keymap settings view. -//! Ports used/provided: explicit `KeymapHandle`. Settings section: none. Globals: none. -//! Native loading and synchronous desired-file publication remain existing UI-layer debt. +//! Ports used: shared Persisted snapshot execution and ShutdownCoordinator. Provided: explicit +//! KeymapHandle with desired binding admission and publication receipt. Settings: none. Globals: none. +//! Native loading remains startup acquisition; accepted writes use the shared bounded snapshot worker. //! Invariants: installation replaces only this crate's marked bindings; explicit //! unbindings remove defaults without modifying unrelated toolkit bindings; //! desired edits remain installed when typed persistence errors are reported. @@ -21,76 +22,90 @@ mod error; mod file; mod names; -use std::{path::PathBuf, rc::Rc}; - use gpui_kit::{ App, AppContext as _, DummyKeyboardMapper, Entity, KeyBinding, KeyBindingContextPredicate, - KeyBindingMetaIndex, NoAction, Subscription, + KeyBindingMetaIndex, NoAction, Subscription, Task, }; +use nocterm_core::{SharedError, persist::PersistError}; +use nocterm_store::{Persisted, Publication, ShutdownCoordinator, SnapshotWriter, StoreError}; +use std::{path::PathBuf, rc::Rc, sync::Arc}; pub use error::KeymapError; pub use file::{Binding, KeymapFile, Section, Source, UNBOUND, merge, normalize}; pub use names::{OFFERED_NAMESPACES, humanize, offered}; - -/// Marks the bindings this crate installs, so a reload replaces only them. const DEFAULT_META: KeyBindingMetaIndex = KeyBindingMetaIndex(0x6e6f_6301); const USER_META: KeyBindingMetaIndex = KeyBindingMetaIndex(0x6e6f_6302); -/// The defaults, the user's changes and the file they are kept in. +enum Failure { + Load(SharedError), + Write(StoreError), +} struct Keymap { defaults: KeymapFile, - user: KeymapFile, - file: Option, - /// Why the user's file could not be read or written, if it could not. - error: Option>, + user: Entity>, + failure: Option, + revision: u64, + generation: u64, + publication: Option>>, } -/// Explicit shared binding owner retained by the composition root and its pages. +/// Original binding owner; its Persisted document is the only authoritative user keymap. #[derive(Clone)] pub struct KeymapHandle { state: Entity, } impl KeymapHandle { - /// Reads the defaults from `defaults` and the user's changes from - /// `file`, then binds the merge. A malformed user file is reported in - /// [`KeymapHandle::error`] and ignored, rather than costing every binding. pub fn install(defaults: &str, file: Option, cx: &mut App) -> Self { let defaults = toml::from_str(defaults).unwrap_or_else(|error| { tracing::error!(%error, "the default keymap is malformed"); KeymapFile::default() }); - let (user, error) = match file.as_deref().map(nocterm_core::persist::load) { + let (initial, failure) = match file.as_deref().map(nocterm_core::persist::load) { None | Some(Ok(None)) => (KeymapFile::default(), None), Some(Ok(Some(user))) => (user, None), - Some(Err(error)) => { - tracing::error!(%error, "the user keymap is malformed"); - ( - KeymapFile::default(), - Some(nocterm_core::SharedError::from_error(error)), - ) - } + Some(Err(error)) => ( + KeymapFile::default(), + Some(Failure::Load(SharedError::from_error(error))), + ), }; + let user = cx.new(|_| Persisted::new(initial, writer(file), Publication::Optimistic, 8)); let state = cx.new(|_| Keymap { defaults, user, - file, - error, + failure, + revision: 0, + generation: 0, + publication: None, }); let handle = Self { state }; handle.replace_bindings(cx); handle } - - /// Last native load/publication failure, retained without reconstructing its cause. - pub fn error<'a>( + /// Registers the existing snapshot owner, including accepted writes, with application quit. + pub fn register_shutdown( &self, - cx: &'a App, - ) -> Option<&'a nocterm_core::SharedError> { - self.state.read(cx).error.as_ref() + shutdown: &ShutdownCoordinator, + cx: &App, + ) -> Result<(), StoreError> { + shutdown.register(&self.state.read(cx).user) + } + /// Last original native load/write cause, preserving its concrete allocation. + pub fn error<'a>(&self, cx: &'a App) -> Option<&'a SharedError> { + match self.state.read(cx).failure.as_ref()? { + Failure::Load(error) => Some(error), + Failure::Write(error) => native_error(error), + } + } + pub fn publication_error<'a>(&self, cx: &'a App) -> Option<&'a StoreError> { + match self.state.read(cx).failure.as_ref()? { + Failure::Write(error) => Some(error), + _ => None, + } + } + pub fn revision(&self, cx: &App) -> u64 { + self.state.read(cx).revision } - - /// Observe completed installation/error transitions; callers retain the subscription. pub fn observe( &self, cx: &mut App, @@ -98,15 +113,31 @@ impl KeymapHandle { ) -> Subscription { cx.observe(&self.state, move |_, cx| on_change(cx)) } - pub fn bindings(&self, cx: &App) -> Vec { - self.state.read(cx).bindings() + let state = self.state.read(cx); + merge(&state.defaults, state.user.read(cx).visible()) } pub fn defaults(&self, cx: &App) -> Vec { - self.state.read(cx).defaults() + merge(&self.state.read(cx).defaults, &KeymapFile::default()) + } + pub fn customized_actions(&self, cx: &App) -> std::collections::BTreeSet { + let state = self.state.read(cx); + state + .user + .read(cx) + .visible() + .here() + .filter_map(|(context, keys, action)| { + if action == UNBOUND { + state.defaults.action_for(context, keys).map(str::to_owned) + } else { + Some(action.clone()) + } + }) + .collect() } pub fn is_customized(&self, action: &str, cx: &App) -> bool { - self.state.read(cx).is_customized(action) + self.customized_actions(cx).contains(action) } pub fn conflicts( &self, @@ -115,11 +146,20 @@ impl KeymapHandle { keys: &str, cx: &App, ) -> Vec { - self.state.read(cx).conflicts(action, context, keys) + let Ok(keys) = normalize(keys) else { + return Vec::new(); + }; + self.bindings(cx) + .into_iter() + .filter(|binding| { + binding.context == *context + && binding.keystrokes == keys + && binding.action != action + }) + .map(|binding| binding.action) + .collect() } - - /// Binds `keystrokes` to `action` in `context`. With `replacing`, the - /// binding being edited goes away. + /// Installs the desired edit immediately; the returned result reports admission. pub fn bind( &self, action: &str, @@ -128,61 +168,85 @@ impl KeymapHandle { keystrokes: &str, cx: &mut App, ) -> Result<(), KeymapError> { - let keystrokes = normalize(keystrokes)?; - self.change(cx, |keymap| { + let keys = normalize(keystrokes)?; + self.change(cx, |user, defaults| { if let Some(old) = replacing { - keymap.unbind_entry(action, &context, old); + unbind_entry(user, defaults, action, &context, old); } - keymap.user.set(&context, &keystrokes, action); + user.set(&context, &keys, action); }) } - - /// Removes the binding of `keystrokes` to `action` in `context`. pub fn unbind( &self, action: &str, context: Option, - keystrokes: &str, + keys: &str, cx: &mut App, ) -> Result<(), KeymapError> { - self.change(cx, |keymap| { - keymap.unbind_entry(action, &context, keystrokes) + self.change(cx, |user, defaults| { + unbind_entry(user, defaults, action, &context, keys) }) } - - /// Returns `action` to its default bindings. pub fn reset(&self, action: &str, cx: &mut App) -> Result<(), KeymapError> { - self.change(cx, |keymap| { - let defaults = keymap.defaults.clone(); - keymap.user.retain(|context, keys, bound| { + self.change(cx, |user, defaults| { + user.retain(|context, keys, bound| { bound != action && !(bound == UNBOUND && defaults.action_for(context, keys) == Some(action)) - }); + }) }) } - - /// Publish the desired edit even when its synchronous native save fails. - fn change(&self, cx: &mut App, edit: impl FnOnce(&mut Keymap)) -> Result<(), KeymapError> { - let result = self.state.update(cx, |keymap, _| { - edit(keymap); - let result = match &keymap.file { - Some(file) => nocterm_core::persist::save(file, &keymap.user) - .map_err(nocterm_core::SharedError::from_error), - None => Ok(()), - }; - keymap.error = result.as_ref().err().cloned(); - result.map_err(KeymapError::Persistence) + /// The latest accepted publication; dropping this waiter leaves the store's work accepted. + pub fn flush(&self, cx: &mut App) -> Task> { + self.state + .update(cx, |state, _| state.publication.take()) + .unwrap_or_else(|| Task::ready(Ok(()))) + } + fn change( + &self, + cx: &mut App, + edit: impl FnOnce(&mut KeymapFile, &KeymapFile), + ) -> Result<(), KeymapError> { + let state = self.state.read(cx); + let user = state.user.clone(); + let mut next = user.read(cx).visible().clone(); + edit(&mut next, &state.defaults); + let changed = next != *user.read(cx).visible(); + let receipt = user.update(cx, |user, cx| { + user.update(move |current| *current = next, cx) + })?; + self.state.update(cx, |state, cx| { + state.revision = state.revision.wrapping_add(u64::from(changed)); + state.generation = state.generation.wrapping_add(1); + let generation = state.generation; + state.publication = Some(cx.spawn(async move |state, cx| { + let result = receipt.wait().await; + let _ = state.update(cx, |state, cx| { + if state.generation == generation { + let next = result.as_ref().err().cloned().map(Failure::Write); + let changed = state.failure.is_some() || next.is_some(); + state.failure = next; + if changed { + cx.notify(); + } + } + }); + result.map(drop).map_err(publication_error) + })); + cx.notify(); }); - self.replace_bindings(cx); - self.state.update(cx, |_, cx| cx.notify()); - result + if changed { + self.replace_bindings(cx); + } + Ok(()) } - fn replace_bindings(&self, cx: &mut App) { let ours = { - let keymap = self.state.read(cx); + let state = self.state.read(cx); let mut ours = Vec::new(); - for (file, meta) in [(&keymap.defaults, DEFAULT_META), (&keymap.user, USER_META)] { + for (file, meta) in [ + (&state.defaults, DEFAULT_META), + (state.user.read(cx).visible(), USER_META), + ] { ours.extend( bindings_of(file, cx) .into_iter() @@ -191,7 +255,7 @@ impl KeymapHandle { } ours }; - let others: Vec = cx + let others: Vec<_> = cx .key_bindings() .borrow() .bindings() @@ -202,54 +266,45 @@ impl KeymapHandle { cx.bind_keys(others.into_iter().chain(ours)); } } - -impl Keymap { - /// Every binding in effect on this platform. - fn bindings(&self) -> Vec { - merge(&self.defaults, &self.user) - } - - /// The default bindings of this platform. - fn defaults(&self) -> Vec { - merge(&self.defaults, &KeymapFile::default()) - } - - /// Whether the user changed how `action` is bound. - fn is_customized(&self, action: &str) -> bool { - let mut customized = false; - let mut user = self.user.clone(); - user.retain(|context, keys, bound| { - customized |= bound == action - || (bound == UNBOUND && self.defaults.action_for(context, keys) == Some(action)); - true - }); - customized - } - - /// The actions other than `action` that `keystrokes` runs in `context`. - fn conflicts(&self, action: &str, context: &Option, keystrokes: &str) -> Vec { - let Ok(keystrokes) = normalize(keystrokes) else { - return Vec::new(); - }; - self.bindings() - .into_iter() - .filter(|binding| { - binding.context == *context - && binding.keystrokes == keystrokes - && binding.action != action - }) - .map(|binding| binding.action) - .collect() +fn unbind_entry( + user: &mut KeymapFile, + defaults: &KeymapFile, + action: &str, + context: &Option, + keys: &str, +) { + if defaults.action_for(context, keys) == Some(action) { + user.set(context, keys, UNBOUND); + } else { + user.remove(context, keys); } - - fn unbind_entry(&mut self, action: &str, context: &Option, keystrokes: &str) { - if self.defaults.action_for(context, keystrokes) == Some(action) { - self.user.set(context, keystrokes, UNBOUND); - } else { - self.user.remove(context, keystrokes); - } +} +fn writer(file: Option) -> SnapshotWriter { + Arc::new(move |user| { + let file = file.clone(); + Box::pin(async move { + if let Some(file) = file { + nocterm_core::persist::save(&file, &user) + .map_err(SharedError::from_error) + .map_err(StoreError::backend)?; + } + Ok(()) + }) + }) +} +fn native_error(error: &StoreError) -> Option<&SharedError> { + if let StoreError::Backend(cause) = error { + cause.downcast_ref() + } else { + None } } +fn publication_error(error: StoreError) -> KeymapError { + native_error(&error) + .cloned() + .map(KeymapError::Persistence) + .unwrap_or(KeymapError::Store(error)) +} /// `file`'s bindings for this platform as GPUI bindings. Entries that do /// not resolve are logged and skipped: one stale binding must not cost the diff --git a/crates/keymap/src/names.rs b/crates/keymap/src/names.rs index 351b3797..97f3abb9 100644 --- a/crates/keymap/src/names.rs +++ b/crates/keymap/src/names.rs @@ -10,6 +10,7 @@ pub const OFFERED_NAMESPACES: &[&str] = &[ "files", "agent", "vault", + "hubs", ]; /// Whether `name` is a command a person runs: in nocterm's namespaces, and @@ -64,6 +65,7 @@ mod tests { assert!(offered("terminal::Copy")); assert!(offered("snippets::ToggleSnippets")); assert!(offered("snippets::NewSnippet")); + assert!(offered("hubs::SignInToServer")); assert!(!offered("input::Backspace")); assert!(!offered("workspace::ToggleCommandPalette")); } diff --git a/crates/keymap/src/tests.rs b/crates/keymap/src/tests.rs index 0b79b158..381886e2 100644 --- a/crates/keymap/src/tests.rs +++ b/crates/keymap/src/tests.rs @@ -22,7 +22,7 @@ fn bound(keymap: &KeymapHandle, cx: &App, keys: &str) -> Vec { } #[gpui_kit::test] -fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestAppContext) { +async fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestAppContext) { let directory = tempfile::tempdir().unwrap(); let file = directory.path().join("keymap.toml"); let keymap = cx.update(|cx| { @@ -56,6 +56,7 @@ fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestA .any(|binding| binding.action().name() == "test::Library") ); }); + cx.update(|cx| keymap.flush(cx)).await.unwrap(); let saved: KeymapFile = toml::from_str(&std::fs::read_to_string(&file).unwrap()).unwrap(); let workspace = Some("Workspace".to_owned()); assert_eq!(saved.action_for(&workspace, "ctrl-e"), Some(UNBOUND)); @@ -71,6 +72,9 @@ fn rebinding_saves_only_the_change_and_reset_restores_the_default(cx: &mut TestA keymap .unbind("test::Second", Some("Workspace".into()), "ctrl-j", cx) .unwrap(); + }); + cx.update(|cx| keymap.flush(cx)).await.unwrap(); + cx.update(|cx| { let keymap = KeymapHandle::install(DEFAULTS, Some(file.clone()), cx); assert!(bound(&keymap, cx, "ctrl-j").is_empty()); }); @@ -169,7 +173,9 @@ fn assert_native_failure( } #[gpui_kit::test] -fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mut TestAppContext) { +async fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it( + cx: &mut TestAppContext, +) { let directory = tempfile::tempdir().unwrap(); let file = directory.path().join("keymap.toml"); // A directory at the destination deterministically refuses atomic file publication. @@ -179,7 +185,7 @@ fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mu KeymapHandle::install(DEFAULTS, Some(file.clone()), cx) }); cx.update(|cx| { - let error = keymap + keymap .bind( "test::First", Some("Workspace".into()), @@ -187,7 +193,10 @@ fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mu "alt-e", cx, ) - .unwrap_err(); + .unwrap(); + }); + let error = cx.update(|cx| keymap.flush(cx)).await.unwrap_err(); + cx.update(|cx| { assert_native_failure(&keymap, &file, &error, cx); assert!(bound(&keymap, cx, "ctrl-e").is_empty()); assert_eq!(bound(&keymap, cx, "alt-e"), ["test::First"]); @@ -225,9 +234,10 @@ fn failed_save_keeps_the_desired_binding_installed_and_retry_persists_it(cx: &mu cx, ) .unwrap(); - assert!(keymap.error(cx).is_none()); assert_eq!(bound(&keymap, cx, "alt-e"), ["test::First"]); }); + cx.update(|cx| keymap.flush(cx)).await.unwrap(); + cx.update(|cx| assert!(keymap.error(cx).is_none())); let saved = nocterm_core::persist::load::(&file) .unwrap() .unwrap(); diff --git a/crates/layout-ui/Cargo.toml b/crates/layout-ui/Cargo.toml new file mode 100644 index 00000000..7b86cfca --- /dev/null +++ b/crates/layout-ui/Cargo.toml @@ -0,0 +1,28 @@ +[package] +name = "nocterm-layout-ui" +description = "The editor of the status bar and the panel groups, inside Appearance." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "feature" + +[dependencies] +nocterm-plugin-enablement.workspace = true +nocterm-plugin-model.workspace = true +nocterm-store.workspace = true +nocterm-ui.workspace = true +nocterm-window-layout.workspace = true +nocterm-workspace.workspace = true +gpui-kit.workspace = true + +[dev-dependencies] +nocterm-settings.workspace = true +tempfile.workspace = true +nocterm-ui = { workspace = true, features = ["test-support"] } +gpui-kit = { workspace = true, features = ["test-support"] } + +[lints] +workspace = true diff --git a/crates/layout-ui/src/about.rs b/crates/layout-ui/src/about.rs new file mode 100644 index 00000000..985ee62f --- /dev/null +++ b/crates/layout-ui/src/about.rs @@ -0,0 +1,56 @@ +//! What the tooltip of a chip of the bar preview says, made when it shows: the chip's name, +//! whether the panel opens with the layout and, for an item of a user plugin, what the host +//! knows of the plugin, in the host's own words. + +use std::sync::Arc; + +use gpui_kit::{AnyView, App, AppContext as _, SharedString, Window, component::tooltip::Tooltip}; +use nocterm_plugin_model::{ContributionId, PluginSet, PluginTier}; + +use crate::user_facts::SharedFacts; + +/// The tooltip of the chip of `id`, named `name`. +pub(crate) fn tooltip( + catalog: &Arc, + facts: Option<&SharedFacts>, + id: &ContributionId, + name: SharedString, + open: bool, +) -> impl Fn(&mut Window, &mut App) -> AnyView + 'static { + let (catalog, facts, id) = (catalog.clone(), facts.cloned(), id.clone()); + move |_, cx| { + let text = text(&catalog, facts.as_ref(), &id, &name, open, cx); + cx.new(|_| Tooltip::new(text)).into() + } +} + +fn text( + catalog: &PluginSet, + facts: Option<&SharedFacts>, + id: &ContributionId, + name: &str, + open: bool, + cx: &App, +) -> SharedString { + let mut lines = vec![name.to_owned()]; + if open { + lines.push("Opens with the layout".to_owned()); + } + let Some(plugin) = catalog + .plugin_of(id) + .filter(|plugin| plugin.tier() == PluginTier::User) + else { + return lines.join("\n").into(); + }; + lines.push(format!("Plugin \u{b7} {} ({})", plugin.name(), plugin.id())); + if let Some(facts) = facts.and_then(|facts| facts.facts(plugin.id(), cx)) { + lines.push(format!("Publisher: {} (unverified)", facts.publisher)); + lines.push(format!("Fingerprint: {}", facts.fingerprint)); + lines.push(if facts.capabilities.is_empty() { + "Allowed: nothing".to_owned() + } else { + format!("Allowed: {}", facts.capabilities.join("; ")) + }); + } + lines.join("\n").into() +} diff --git a/crates/layout-ui/src/commands.rs b/crates/layout-ui/src/commands.rs new file mode 100644 index 00000000..25e91d44 --- /dev/null +++ b/crates/layout-ui/src/commands.rs @@ -0,0 +1,27 @@ +//! How the parts of the editor reach the section that owns the state. + +use gpui_kit::{App, Context, WeakEntity, Window}; + +use crate::section::LayoutEditorSection; + +/// A handle for the click handlers of the editor's parts. +#[derive(Clone)] +pub(crate) struct Commands(WeakEntity); + +impl Commands { + pub(crate) fn new(section: WeakEntity) -> Self { + Self(section) + } + + /// Runs `command` on the section if it is still there. + pub(crate) fn run( + &self, + window: &mut Window, + cx: &mut App, + command: impl FnOnce(&mut LayoutEditorSection, &mut Window, &mut Context), + ) { + let _ = self + .0 + .update(cx, |section, cx| command(section, window, cx)); + } +} diff --git a/crates/layout-ui/src/dnd.rs b/crates/layout-ui/src/dnd.rs new file mode 100644 index 00000000..228818da --- /dev/null +++ b/crates/layout-ui/src/dnd.rs @@ -0,0 +1,293 @@ +//! Dragging the items of the bar: what a drag carries, what a drop asks for, how an item starts +//! a drag, and the tray, which takes what is dropped on it out of the bar. +//! +//! The rules stay in `nocterm-window-layout`: a landing only names the list and the place, and +//! the section sends the resulting [`EditOp`] like any other edit. Where a drop over the bar +//! preview lands is worked out by `drops`. + +use gpui_kit::{ + App, Bounds, InteractiveElement, ParentElement, Pixels, StatefulInteractiveElement, Styled, + Window, canvas, component::ActiveTheme as _, prelude::*, px, +}; +use nocterm_plugin_model::{ContributionId, ContributionKind, ContributionRef, GroupId}; +use nocterm_ui::DragSource; +use nocterm_window_layout::{EditOp, Slot, Subject}; + +use crate::{ + commands::Commands, + face::{self, Look}, +}; + +/// What kind of thing is dragged, which decides where it may land. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Kind { + /// A status bar widget: lands in a zone. + Widget, + /// A panel: lands in a group, beside a panel on its own, or on its own in a zone. + Panel, + /// A panel group: lands in a zone. + Group, +} + +/// The payload of a drag: the item and where it came from. +#[derive(Clone, Debug)] +pub(crate) struct Dragged { + subject: Subject, + kind: Kind, + pinned: bool, + /// The list and place it leaves, when it is in the bar. + from: Option<(Slot, usize)>, +} + +impl Dragged { + pub(crate) fn new( + subject: Subject, + kind: Kind, + pinned: bool, + from: Option<(Slot, usize)>, + ) -> Self { + Self { + subject, + kind, + pinned, + from, + } + } + + /// A widget or a panel, at `from` in the bar or waiting in the tray. + pub(crate) fn contribution( + contribution: ContributionRef<'_>, + from: Option<(Slot, usize)>, + ) -> Self { + // The editor lists no toolbar; one would move like a widget. + let kind = match contribution.kind() { + ContributionKind::Widget | ContributionKind::Toolbar => Kind::Widget, + ContributionKind::Panel => Kind::Panel, + }; + let subject = Subject::Contribution(contribution.id().clone()); + Self::new(subject, kind, contribution.pinned(), from) + } + + /// The group `id`, at `from` in the bar. + pub(crate) fn group(id: &GroupId, from: (Slot, usize)) -> Self { + Self::new(Subject::Group(id.clone()), Kind::Group, false, Some(from)) + } + + pub(crate) fn subject(&self) -> &Subject { + &self.subject + } + + pub(crate) fn kind(&self) -> Kind { + self.kind + } + + fn in_bar(&self) -> bool { + self.from.is_some() + } + + /// Whether dropping at `index` of `place` leaves it where it is: on its own place, or right + /// after it. + fn stays(&self, place: &Slot, index: usize) -> bool { + matches!(&self.from, Some((held, at)) if held == place && (index == *at || index == at + 1)) + } +} + +/// Where a drop puts the dragged item. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Landing { + /// In front of the entry at `index` of a zone or a group, or last when `index` is its + /// length. + At(Slot, usize), + /// Into a group with the panel that stands on its own here. + With(ContributionId), + /// Out of the bar. + Tray, +} + +impl Landing { + /// The edit a drop of `dragged` here asks for; `None` when it changes nothing, because the + /// item is already there or does not go there. + pub(crate) fn op(&self, dragged: &Dragged) -> Option { + match (self, &dragged.subject) { + (Self::At(Slot::Group(_), _), _) if dragged.kind != Kind::Panel => None, + (Self::At(place, index), subject) => { + (!dragged.stays(place, *index)).then(|| EditOp::MoveTo { + subject: subject.clone(), + place: place.clone(), + index: *index, + }) + } + (Self::With(target), Subject::Contribution(panel)) + if dragged.kind == Kind::Panel && panel != target => + { + Some(EditOp::GroupWith { + panel: panel.clone(), + target: target.clone(), + }) + } + (Self::Tray, Subject::Contribution(id)) if dragged.in_bar() => { + Some(EditOp::RemoveToTray { id: id.clone() }) + } + _ => None, + } + } + + /// Whether the drop goes in front of the entry at `index` of `place`. + pub(crate) fn is_at(&self, place: &Slot, index: usize) -> bool { + matches!(self, Self::At(at, before) if at == place && *before == index) + } +} + +/// Calls `painted` with the box of `element` each time it is painted. `border` is the width +/// of the element's border, which the measuring canvas, filling the padding box, leaves out. +pub(crate) fn measured( + element: E, + border: f32, + painted: impl FnOnce(Bounds, &mut Window, &mut App) + 'static, +) -> E { + element.child( + canvas( + move |bounds, window, cx| painted(bounds.dilate(px(border)), window, cx), + |_, _, _, _| {}, + ) + .absolute() + .top_0() + .left_0() + .size_full(), + ) +} + +/// Makes `element` start a drag of `dragged`. The ghost draws what `look` makes when the drag +/// starts, lifted, in the box `measured` took when `element` was last painted. +pub(crate) fn source( + element: E, + dragged: Dragged, + measured: DragSource, + look: impl Fn() -> Look + 'static, +) -> E +where + E: StatefulInteractiveElement, +{ + element.on_drag(dragged, move |_, _, _, cx| { + cx.stop_propagation(); + let look = look(); + cx.new(|_| measured.preview(move |_, cx| face::ghost(&look, cx))) + }) +} + +/// Makes the tray take what is dropped on it out of the bar. It is tinted while that would +/// happen, in the colour of a refusal for an item that is always shown. A drop that changes +/// nothing, like an item of the tray put back on it, is taken all the same and ends the drag. +pub(crate) fn tray(element: E, commands: Commands) -> E { + element + .on_drop(move |dragged: &Dragged, window, cx| { + let dragged = dragged.clone(); + commands.run(window, cx, move |section, _, cx| { + section.dropped(&dragged, &Landing::Tray, cx); + }); + }) + .drag_over::(|style, dragged, _, cx| { + if Landing::Tray.op(dragged).is_none() { + return style; + } + let theme = cx.theme(); + let color = if dragged.pinned { + theme.danger + } else { + theme.primary + }; + style.bg(color.opacity(0.1)).border_color(color) + }) +} + +#[cfg(test)] +mod tests { + use nocterm_plugin_model::{ContributionId, Zone}; + + use super::*; + + fn dragged(kind: Kind, from: Option<(Slot, usize)>) -> Dragged { + let subject = Subject::Contribution(ContributionId::new("a.b")); + Dragged::new(subject, kind, false, from) + } + + #[test] + fn a_widget_lands_in_a_zone_and_a_panel_in_a_group_or_on_its_own_in_a_zone() { + let widget = dragged(Kind::Widget, Some((Slot::Zone(Zone::Left), 0))); + let panel = dragged(Kind::Panel, Some((Slot::Group(GroupId::new("g")), 0))); + let zone = Landing::At(Slot::Zone(Zone::Right), 2); + let member = Landing::At(Slot::Group(GroupId::new("h")), 1); + + assert!(zone.op(&widget).is_some()); + assert!(matches!( + zone.op(&panel), + Some(EditOp::MoveTo { + place: Slot::Zone(Zone::Right), + index: 2, + .. + }) + )); + assert!(matches!( + member.op(&panel), + Some(EditOp::MoveTo { + place: Slot::Group(_), + index: 1, + .. + }) + )); + assert!(member.op(&widget).is_none(), "a widget never joins a group"); + } + + #[test] + fn a_panel_dropped_on_a_panel_of_its_own_groups_with_it_and_nothing_else_does() { + let panel = dragged(Kind::Panel, Some((Slot::Zone(Zone::Left), 0))); + let widget = dragged(Kind::Widget, Some((Slot::Zone(Zone::Left), 0))); + let alone = Landing::With(ContributionId::new("c.d")); + + assert!(matches!(alone.op(&panel), Some(EditOp::GroupWith { .. }))); + assert!( + alone.op(&widget).is_none(), + "a widget does not make a group" + ); + let itself = Landing::With(ContributionId::new("a.b")); + assert!(itself.op(&panel).is_none(), "not with itself"); + } + + #[test] + fn dropping_on_its_own_place_or_the_next_one_changes_nothing() { + let widget = dragged(Kind::Widget, Some((Slot::Zone(Zone::Left), 2))); + let at = |zone, index| Landing::At(Slot::Zone(zone), index); + + assert!(at(Zone::Left, 2).op(&widget).is_none()); + assert!(at(Zone::Left, 3).op(&widget).is_none()); + assert!(at(Zone::Left, 1).op(&widget).is_some()); + assert!(at(Zone::Left, 4).op(&widget).is_some()); + assert!(at(Zone::Right, 2).op(&widget).is_some()); + } + + #[test] + fn the_tray_takes_what_is_in_the_bar_and_nothing_else() { + let placed = dragged(Kind::Widget, Some((Slot::Zone(Zone::Left), 0))); + let waiting = dragged(Kind::Widget, None); + + assert!(matches!( + Landing::Tray.op(&placed), + Some(EditOp::RemoveToTray { .. }) + )); + assert!(Landing::Tray.op(&waiting).is_none()); + } + + #[test] + fn a_panel_of_the_tray_dropped_on_a_zone_stands_there_on_its_own() { + let waiting = dragged(Kind::Panel, None); + + assert!(matches!( + Landing::At(Slot::Zone(Zone::Center), 0).op(&waiting), + Some(EditOp::MoveTo { + place: Slot::Zone(Zone::Center), + index: 0, + .. + }) + )); + } +} diff --git a/crates/layout-ui/src/drops.rs b/crates/layout-ui/src/drops.rs new file mode 100644 index 00000000..0212896c --- /dev/null +++ b/crates/layout-ui/src/drops.rs @@ -0,0 +1,353 @@ +//! Where a drop over the bar preview lands, worked out from where the pointer is against what +//! the preview painted last. +//! +//! The half of an entry the pointer is over puts the drop in front of the entry or after it. +//! A panel over a group goes among its members, so the outer halves of the group's first and +//! last members are its start and its end; a widget or a group over a group goes beside the +//! group. A panel over the middle of a panel on its own makes a group with it. Away from every +//! entry the nearest zone takes the drop, so a drop over the preview always lands somewhere, +//! and the preview marks that place while the item is held over it. + +use gpui_kit::{Bounds, Context, Entity, InteractiveElement, Pixels, Point}; +use nocterm_plugin_model::{ContributionId, GroupId, Zone}; +use nocterm_window_layout::{Slot, Subject}; + +use crate::{ + commands::Commands, + dnd::{Dragged, Kind, Landing}, +}; + +/// What an entry of the preview holds, as far as a drop over it goes. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Holds { + /// A widget, a separator or a member of a group. + Item, + /// A panel that stands on its own. + Alone(ContributionId), + /// A group, with the members recorded under its own slot. + Group(GroupId), +} + +/// One entry of the preview: the entry at `index` of a zone, or a member of a group. +#[derive(Clone, Debug)] +pub(crate) struct Spot { + place: Slot, + index: usize, + holds: Holds, +} + +impl Spot { + pub(crate) fn new(place: Slot, index: usize, holds: Holds) -> Self { + Self { + place, + index, + holds, + } + } + + /// The list and place a drag of this entry leaves. + pub(crate) fn from(&self) -> (Slot, usize) { + (self.place.clone(), self.index) + } +} + +/// The preview as it was last painted, and the drag held over it. +#[derive(Debug, Default)] +pub(crate) struct Drops { + zones: Vec<(Zone, Bounds)>, + spots: Vec<(Spot, Bounds)>, + /// Where the item held over the preview would land, when that changes something. + aimed: Option, + /// The item being dragged, which its place in the preview shows as moving. + moving: Option, +} + +impl Drops { + /// Forgets what was painted, before the preview is drawn again. + pub(crate) fn repaint(&mut self) { + self.zones.clear(); + self.spots.clear(); + } + + pub(crate) fn zone(&mut self, zone: Zone, bounds: Bounds) { + self.zones.push((zone, bounds)); + } + + pub(crate) fn spot(&mut self, spot: Spot, bounds: Bounds) { + self.spots.push((spot, bounds)); + } + + pub(crate) fn aimed(&self) -> Option<&Landing> { + self.aimed.as_ref() + } + + pub(crate) fn moving(&self) -> Option<&Subject> { + self.moving.as_ref() + } + + /// Where a drop of `dragged` at `at` lands; `None` before the preview was painted. + pub(crate) fn landing(&self, at: Point, dragged: &Dragged) -> Option { + let x = at.x; + let (zone, _) = self + .zones + .iter() + .min_by(|(_, a), (_, b)| distance(a, x).total_cmp(&distance(b, x)))?; + let place = Slot::Zone(*zone); + let under = self + .spots + .iter() + .find(|(spot, bounds)| spot.place == place && bounds.left() <= x && x < bounds.right()); + if let (Some((spot, bounds)), Kind::Panel) = (under, dragged.kind()) { + match &spot.holds { + Holds::Group(group) => { + let members = Slot::Group(group.clone()); + let index = self.before(&members, x); + return Some(Landing::At(members, index)); + } + Holds::Alone(panel) if middle(bounds, x) => { + return Some(Landing::With(panel.clone())); + } + _ => {} + } + } + let index = self.before(&place, x); + Some(Landing::At(place, index)) + } + + /// Follows the pointer of a drag of `dragged`; `at` is `None` while it is away from the + /// preview. Returns whether what the preview shows changed. + pub(crate) fn aim(&mut self, at: Option>, dragged: &Dragged) -> bool { + let aimed = at + .and_then(|at| self.landing(at, dragged)) + .filter(|landing| landing.op(dragged).is_some()); + let moving = Some(dragged.subject().clone()); + let changed = aimed != self.aimed || moving != self.moving; + self.aimed = aimed; + self.moving = moving; + changed + } + + /// Forgets the drag, which ended. Returns whether what the preview shows changed. + pub(crate) fn settle(&mut self) -> bool { + let changed = self.aimed.is_some() || self.moving.is_some(); + self.aimed = None; + self.moving = None; + changed + } + + /// How many entries of `place` are left of `x` by their middle: the index a drop at `x` + /// takes. + fn before(&self, place: &Slot, x: Pixels) -> usize { + self.spots + .iter() + .filter(|(spot, bounds)| spot.place == *place && bounds.center().x < x) + .count() + } +} + +/// How far `x` is from `bounds`, sideways. +fn distance(bounds: &Bounds, x: Pixels) -> f32 { + let (left, right, x) = ( + f32::from(bounds.left()), + f32::from(bounds.right()), + f32::from(x), + ); + (left - x).max(x - right).max(0.) +} + +/// Whether `x` is in the middle half of `bounds`. +fn middle(bounds: &Bounds, x: Pixels) -> bool { + let offset = f32::from(x - bounds.center().x).abs(); + offset <= f32::from(bounds.size.width) / 4. +} + +/// Makes the whole preview take every drop of a dragged item, where `drops` says it lands, and +/// follow the pointer while one is held over it. A drop that changes nothing ends the drag all +/// the same, so no drop over the preview is refused. +pub(crate) fn target( + element: E, + drops: Entity, + commands: Commands, +) -> E { + let held = drops.clone(); + element + .on_drag_move::(move |event, _, cx| { + let at = event.event.position; + let over = event.bounds.contains(&at).then_some(at); + let dragged = event.drag(cx).clone(); + held.update(cx, |drops, cx| notify(drops.aim(over, &dragged), cx)); + }) + .on_drop(move |dragged: &Dragged, window, cx| { + let at = window.mouse_position(); + let landing = drops.update(cx, |drops, cx| { + let landing = drops.landing(at, dragged); + notify(drops.settle(), cx); + landing + }); + let Some(landing) = landing else { + return; + }; + let dragged = dragged.clone(); + commands.run(window, cx, move |section, _, cx| { + section.dropped(&dragged, &landing, cx); + }); + }) +} + +fn notify(changed: bool, cx: &mut Context) { + if changed { + cx.notify(); + } +} + +#[cfg(test)] +mod tests { + use gpui_kit::{point, px, size}; + + use super::*; + + const LEFT: Slot = Slot::Zone(Zone::Left); + + fn dragged(kind: Kind, id: &'static str) -> Dragged { + let subject = Subject::Contribution(ContributionId::new(id)); + Dragged::new(subject, kind, false, None) + } + + fn widget() -> Dragged { + dragged(Kind::Widget, "a.widget") + } + + fn panel() -> Dragged { + dragged(Kind::Panel, "a.panel") + } + + fn span(left: f32, width: f32) -> Bounds { + Bounds::new(point(px(left), px(0.)), size(px(width), px(28.))) + } + + fn at(x: f32) -> Point { + point(px(x), px(14.)) + } + + /// A left zone from 0 to 200 with a widget at 10..40, a group "g" at 50..130 holding two + /// panels at 56..86 and 92..122, and a panel "p.alone" at 140..180; a right zone from 400 + /// to 500 with a widget at 410..440. + fn drops() -> Drops { + let mut drops = Drops::default(); + let group = || Slot::Group(GroupId::new("g")); + drops.zone(Zone::Left, span(0., 200.)); + drops.zone(Zone::Right, span(400., 100.)); + drops.spot(Spot::new(LEFT, 0, Holds::Item), span(10., 30.)); + let pill = Holds::Group(GroupId::new("g")); + drops.spot(Spot::new(LEFT, 1, pill), span(50., 80.)); + drops.spot(Spot::new(group(), 0, Holds::Item), span(56., 30.)); + drops.spot(Spot::new(group(), 1, Holds::Item), span(92., 30.)); + let alone = Holds::Alone(ContributionId::new("p.alone")); + drops.spot(Spot::new(LEFT, 2, alone), span(140., 40.)); + let right = Slot::Zone(Zone::Right); + drops.spot(Spot::new(right, 0, Holds::Item), span(410., 30.)); + drops + } + + #[test] + fn the_half_of_an_entry_under_the_pointer_puts_the_drop_in_front_of_it_or_after_it() { + let drops = drops(); + + assert_eq!( + drops.landing(at(15.), &widget()), + Some(Landing::At(LEFT, 0)) + ); + assert_eq!( + drops.landing(at(35.), &widget()), + Some(Landing::At(LEFT, 1)) + ); + assert_eq!( + drops.landing(at(45.), &widget()), + Some(Landing::At(LEFT, 1)) + ); + } + + #[test] + fn a_panel_over_a_group_lands_among_its_members_from_its_start_to_its_end() { + let drops = drops(); + let group = |index| Some(Landing::At(Slot::Group(GroupId::new("g")), index)); + + assert_eq!(drops.landing(at(52.), &panel()), group(0), "the start edge"); + assert_eq!(drops.landing(at(60.), &panel()), group(0)); + assert_eq!(drops.landing(at(80.), &panel()), group(1)); + assert_eq!(drops.landing(at(118.), &panel()), group(2), "the end"); + assert_eq!(drops.landing(at(128.), &panel()), group(2), "the end edge"); + } + + #[test] + fn a_widget_over_a_group_lands_beside_the_group() { + let drops = drops(); + + assert_eq!( + drops.landing(at(60.), &widget()), + Some(Landing::At(LEFT, 1)) + ); + assert_eq!( + drops.landing(at(118.), &widget()), + Some(Landing::At(LEFT, 2)) + ); + let group = dragged(Kind::Group, "unused"); + assert_eq!(drops.landing(at(118.), &group), Some(Landing::At(LEFT, 2))); + } + + #[test] + fn a_panel_over_the_middle_of_a_panel_on_its_own_groups_with_it_and_its_edges_go_beside() { + let drops = drops(); + let with = Some(Landing::With(ContributionId::new("p.alone"))); + + assert_eq!(drops.landing(at(160.), &panel()), with); + assert_eq!( + drops.landing(at(142.), &panel()), + Some(Landing::At(LEFT, 2)) + ); + assert_eq!( + drops.landing(at(178.), &panel()), + Some(Landing::At(LEFT, 3)) + ); + assert_eq!( + drops.landing(at(160.), &widget()), + Some(Landing::At(LEFT, 2)) + ); + } + + #[test] + fn away_from_every_entry_the_nearest_zone_takes_the_drop() { + let drops = drops(); + let right = |index| Some(Landing::At(Slot::Zone(Zone::Right), index)); + + assert_eq!( + drops.landing(at(195.), &widget()), + Some(Landing::At(LEFT, 3)) + ); + assert_eq!( + drops.landing(at(250.), &widget()), + Some(Landing::At(LEFT, 3)) + ); + assert_eq!(drops.landing(at(380.), &widget()), right(0)); + assert_eq!(drops.landing(at(560.), &widget()), right(1)); + assert_eq!(Drops::default().landing(at(10.), &widget()), None); + } + + #[test] + fn the_aim_shows_only_a_drop_that_changes_something_and_ends_with_the_drag() { + let mut drops = drops(); + let placed = Subject::Contribution(ContributionId::new("a.widget")); + let held = Dragged::new(placed.clone(), Kind::Widget, false, Some((LEFT, 0))); + + assert!(drops.aim(Some(at(15.)), &held), "the drag starts"); + assert_eq!(drops.aimed(), None, "in front of itself changes nothing"); + assert_eq!(drops.moving(), Some(&placed)); + assert!(drops.aim(Some(at(160.)), &held)); + assert_eq!(drops.aimed(), Some(&Landing::At(LEFT, 2))); + assert!(!drops.aim(Some(at(158.)), &held), "nothing new to show"); + assert!(drops.aim(None, &held), "away from the preview"); + assert_eq!(drops.aimed(), None); + assert!(drops.settle()); + assert_eq!(drops.moving(), None); + } +} diff --git a/crates/layout-ui/src/face.rs b/crates/layout-ui/src/face.rs new file mode 100644 index 00000000..bba12745 --- /dev/null +++ b/crates/layout-ui/src/face.rs @@ -0,0 +1,194 @@ +//! How an item of the bar looks: one chip, with its icon and, for a widget while there is +//! room, its text; a group of chips on a background of its own; a separator; and the marks of +//! where a drop would land. The preview, the tray and the ghost that follows a drag draw the +//! same boxes, so that a lifted item looks like itself and fills the box it was measured in. + +use gpui_kit::{ + AnyElement, App, Div, IntoElement, ParentElement, SharedString, Styled, TestSupportExt as _, + component::{ActiveTheme as _, Icon, Sizable as _, Size, h_flex}, + div, + prelude::*, + px, +}; +use nocterm_ui::IconName; + +/// How tall a chip is. +pub(crate) const CHIP_HEIGHT: f32 = 28.; +/// The border of a chip, a group and a zone, transparent unless it shows something. +pub(crate) const BORDER: f32 = 1.; +/// The padding of a group around its panels, border included. +pub(crate) const PILL_PADDING: f32 = 2.; +/// How wide the gap between the entries of a zone, or the members of a group, is. +pub(crate) const GAP_WIDTH: f32 = 6.; +/// How thick the line that marks where a drop lands is. +const MARK: f32 = 2.; +/// The id of the one mark of where a drop would land. +const DROP_MARK: &str = "layout-drop-marker"; + +/// What one chip shows. +#[derive(Clone, Debug)] +pub(crate) struct Face { + icon: IconName, + text: Option, + /// A panel that opens with the layout, which the accent marks. + open: bool, +} + +impl Face { + pub(crate) fn new(icon: IconName, text: Option, open: bool) -> Self { + Self { icon, text, open } + } + + /// `chip` drawn as this face: the box every chip has, and its content. + pub(crate) fn fill(self, chip: E, cx: &App) -> E + where + E: ParentElement + Styled + IntoElement, + { + let theme = cx.theme(); + let icon = Icon::new(self.icon) + .with_size(Size::Medium) + .when(self.open, |icon| icon.text_color(theme.primary)); + chip.relative() + .flex_none() + .h(px(CHIP_HEIGHT)) + .gap_1() + .px_1p5() + .items_center() + .rounded(theme.radius) + .border_1() + .border_color(gpui_kit::transparent_black()) + .when(self.open, |chip| { + chip.bg(theme.primary.opacity(0.14)).child( + div() + .absolute() + .bottom(px(1.)) + .left(px(6.)) + .right(px(6.)) + .h(px(2.)) + .rounded_full() + .bg(theme.primary), + ) + }) + .child(icon) + .children(self.text.map(|text| div().text_xs().child(text))) + } +} + +/// The box of a group around its members. An `empty` group shows its title instead. +pub(crate) fn pill(empty: bool, cx: &App) -> Div { + h_flex() + .relative() + .flex_none() + .h(px(CHIP_HEIGHT + 2. * PILL_PADDING)) + .py(px(PILL_PADDING - BORDER)) + .when(empty, |pill| pill.px(px(PILL_PADDING - BORDER))) + .items_center() + .rounded(cx.theme().radius) + .border_1() + .border_color(gpui_kit::transparent_black()) +} + +/// The title an empty group shows. +pub(crate) fn title(title: SharedString, cx: &App) -> Div { + div() + .px_1p5() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(title) +} + +/// A separator between two entries: a short line with a little room on both sides. +pub(crate) fn separator(cx: &App) -> Div { + div() + .flex_none() + .h(px(CHIP_HEIGHT)) + .px(px(2.)) + .flex() + .items_center() + .child(div().w(px(1.)).h(px(14.)).bg(cx.theme().border)) +} + +/// The room between two entries. +pub(crate) fn gap() -> Div { + div() + .relative() + .flex_none() + .w(px(GAP_WIDTH)) + .h(px(CHIP_HEIGHT)) +} + +/// The accent line, in a gap, where the item held over the bar would land. +pub(crate) fn line(cx: &App) -> AnyElement { + div() + .id(DROP_MARK) + .test_support() + .absolute() + .top(px(2.)) + .bottom(px(2.)) + .left(px((GAP_WIDTH - MARK) / 2.)) + .w(px(MARK)) + .rounded_full() + .bg(cx.theme().primary) + .into_any_element() +} + +/// The tint on what the item held over the bar would go into: a panel on its own that it would +/// make a group with, or a group without members. +pub(crate) fn tint(cx: &App) -> AnyElement { + let theme = cx.theme(); + div() + .id(DROP_MARK) + .test_support() + .absolute() + .top_0() + .bottom_0() + .left_0() + .right_0() + .rounded(theme.radius) + .bg(theme.primary.opacity(0.12)) + .border_1() + .border_color(theme.primary) + .into_any_element() +} + +/// What a dragged item looks like. +#[derive(Clone, Debug)] +pub(crate) enum Look { + Chip(Face), + /// A group: its members, `None` for a separator, or the title of an empty one. + Group { + members: Vec>, + title: SharedString, + }, +} + +/// The ghost that follows the pointer: the item as the bar draws it, lifted, filling the box +/// the item was measured in. +pub(crate) fn ghost(look: &Look, cx: &App) -> AnyElement { + let theme = cx.theme(); + let body = match look { + Look::Chip(face) => face.clone().fill(h_flex(), cx), + Look::Group { members, title } if members.is_empty() => { + pill(true, cx).child(self::title(title.clone(), cx)) + } + Look::Group { members, .. } => { + let mut entries = Vec::with_capacity(2 * members.len() + 1); + for member in members { + entries.push(gap().into_any_element()); + entries.push(match member { + Some(face) => face.clone().fill(h_flex(), cx).into_any_element(), + None => separator(cx).into_any_element(), + }); + } + entries.push(gap().into_any_element()); + pill(false, cx).children(entries) + } + }; + body.id("layout-drag-ghost") + .test_support() + .size_full() + .border_color(theme.primary) + .bg(theme.popover) + .shadow_md() + .into_any_element() +} diff --git a/crates/layout-ui/src/fit.rs b/crates/layout-ui/src/fit.rs new file mode 100644 index 00000000..06af970d --- /dev/null +++ b/crates/layout-ui/src/fit.rs @@ -0,0 +1,213 @@ +//! How much of each chip of the bar preview is drawn, so that the bar stays on one line. +//! +//! The box of the preview takes the width it is given. What the bar needs is estimated from +//! its entries. The bar is drawn the way the real one is, with the centre in the middle, while +//! that fits; then with its zones packed side by side; and compact chips scroll sideways when +//! even those do not fit. + +use std::{ + cell::{Cell, RefCell}, + rc::Rc, + sync::Arc, +}; + +use gpui_kit::{Pixels, px}; +use nocterm_plugin_model::{PluginSet, Zone}; +use nocterm_window_layout::{ResolvedItem, ResolvedMember}; + +use crate::{ + face::{GAP_WIDTH, PILL_PADDING}, + snapshot::Snapshot, +}; + +/// What a chip shows. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Density { + /// The icon and the sample text of a widget. + Full, + /// The icon only; the text is a tooltip. + Compact, +} + +/// How the zones share the line. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Spread { + /// The sides share what the centre leaves, as in the bar, so the centre is in the middle. + Centred, + /// Each zone takes what it needs, side by side. + Packed, +} + +/// How the bar is drawn in its box. +#[derive(Clone, Copy, Debug, PartialEq)] +pub(crate) struct Plan { + density: Density, + spread: Spread, + /// The width the compact bar needs, when that is more than the box has and the bar scrolls. + scroll: Option, +} + +impl Plan { + pub(crate) fn density(&self) -> Density { + self.density + } + + pub(crate) fn spread(&self) -> Spread { + self.spread + } + + pub(crate) fn scroll(&self) -> Option { + self.scroll + } +} + +/// The width the preview's box was drawn with last time, and the plan made for it. +/// +/// The estimate of a chip is generous, so that a bar that is said to fit does. +#[derive(Clone, Default)] +pub(crate) struct Fit { + width: Rc>>, + /// The last plan, which holds while the layout, the plugins and the width are the same. + planned: Rc>>, +} + +struct Planned { + snapshot: Rc, + catalog: Arc, + width: Option, + plan: Plan, +} + +/// An average advance of the small text of a chip. +const CHAR: f32 = 7.; +/// The icon of a chip. +const ICON: f32 = 16.; +/// A chip's border and the padding on both sides. +const CHIP_FRAME: f32 = 14.; +/// The gap between the icon and the text of a chip. +const INNER_GAP: f32 = 4.; +/// A separator and the room on both sides of it. +const SEPARATOR: f32 = 5.; +/// The gap between zones. +const ZONE_GAP: f32 = 12.; +/// The padding of the preview's box. +const BOX_PADDING: f32 = 16.; +/// The border of a zone, which shows while something is dragged over it. +const ZONE_BORDER: f32 = 2.; +/// The narrowest a zone is drawn, so that one with nothing in it still takes a drop. +pub(crate) const EMPTY: f32 = 48.; + +impl Fit { + /// Remembers the width of the box. Returns whether it changed, which asks for another draw. + pub(crate) fn observe(&self, width: Pixels) -> bool { + let changed = self + .width + .get() + .is_none_or(|seen| (f32::from(seen) - f32::from(width)).abs() >= 1.); + self.width.set(Some(width)); + changed + } + + /// How to draw the bar in a box as wide as the last one seen. Before any is seen, in full. + pub(crate) fn plan(&self, snapshot: &Rc, catalog: &Arc) -> Plan { + let width = self.width.get(); + if let Some(planned) = self.planned.borrow().as_ref() + && Rc::ptr_eq(&planned.snapshot, snapshot) + && Arc::ptr_eq(&planned.catalog, catalog) + && planned.width == width + { + return planned.plan; + } + let plan = plan_for(width, snapshot, catalog); + self.planned.replace(Some(Planned { + snapshot: snapshot.clone(), + catalog: catalog.clone(), + width, + plan, + })); + plan + } +} + +fn plan_for(width: Option, snapshot: &Snapshot, catalog: &PluginSet) -> Plan { + let Some(width) = width else { + return Plan { + density: Density::Full, + spread: Spread::Centred, + scroll: None, + }; + }; + let room = f32::from(width); + let fits = |density, spread| needed(snapshot, catalog, density, spread) <= room; + let (density, spread) = [ + (Density::Full, Spread::Centred), + (Density::Compact, Spread::Centred), + (Density::Compact, Spread::Packed), + ] + .into_iter() + .find(|(density, spread)| fits(*density, *spread)) + .unwrap_or((Density::Compact, Spread::Packed)); + let need = needed(snapshot, catalog, density, spread); + Plan { + density, + spread, + scroll: (need > room).then(|| px(need)), + } +} + +/// The width the bar takes at `density`. Centred, the wider of the two sides counts on both, as +/// the real bar centres its middle. +fn needed(snapshot: &Snapshot, catalog: &PluginSet, density: Density, spread: Spread) -> f32 { + let width = |zone| zone_width(snapshot, catalog, zone, density); + let (left, centre, right) = (width(Zone::Left), width(Zone::Center), width(Zone::Right)); + let sides = match spread { + Spread::Centred => 2. * left.max(right), + Spread::Packed => left + right, + }; + BOX_PADDING + sides + centre + 2. * ZONE_GAP +} + +fn zone_width(snapshot: &Snapshot, catalog: &PluginSet, zone: Zone, density: Density) -> f32 { + let items = snapshot.layout().zone(zone); + let entries: f32 = items + .iter() + .map(|item| match item { + ResolvedItem::Widget(id) => { + let text = catalog.contribution(id).map_or(0, |spec| { + spec.preview().unwrap_or(spec.title()).chars().count() + }); + chip(density, Some(text)) + } + ResolvedItem::Separator => SEPARATOR, + ResolvedItem::Group(group) if group.is_solo() => chip(density, None), + ResolvedItem::Group(group) => { + let members: Vec = group + .members() + .iter() + .map(|member| match member { + ResolvedMember::Panel(_) => chip(density, None), + ResolvedMember::Separator => SEPARATOR, + }) + .collect(); + if members.is_empty() { + let title = snapshot.group_title(group).chars().count() as f32 * CHAR; + 2. * PILL_PADDING + title + CHIP_FRAME + } else { + // The border, and a gap around every member. + 2. + members.iter().sum::() + (members.len() + 1) as f32 * GAP_WIDTH + } + } + }) + .sum(); + let gaps = (items.len() + 1) as f32 * GAP_WIDTH; + (ZONE_BORDER + entries + gaps).max(EMPTY) +} + +/// One chip: an icon, and the text of a widget when the density shows it. +fn chip(density: Density, text: Option) -> f32 { + let label = match (density, text) { + (Density::Full, Some(chars)) => INNER_GAP + chars as f32 * CHAR, + _ => 0., + }; + CHIP_FRAME + ICON + label +} diff --git a/crates/layout-ui/src/history.rs b/crates/layout-ui/src/history.rs new file mode 100644 index 00000000..63253b44 --- /dev/null +++ b/crates/layout-ui/src/history.rs @@ -0,0 +1,190 @@ +//! Linear snapshot undo with both step and retained-byte budgets. + +use nocterm_window_layout::{BarItem, EditOutcome, GroupEntry, LayoutPreset}; +use std::collections::VecDeque; + +const DEPTH: usize = 100; +const MAX_BYTES: usize = 8 * 1024 * 1024; +type Snapshot = (Vec, usize); + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum Step { + Edit, + Undo, + Redo, +} + +/// History contains complete snapshots only while their aggregate bytes fit the budget. +/// The current document is compared by reference before another copy is retained. +#[derive(Debug)] +pub(crate) struct History { + undo: VecDeque, + redo: VecDeque, + written: Vec, + bytes: usize, +} + +impl History { + pub(crate) fn new(presets: Vec) -> Self { + Self { + undo: VecDeque::new(), + redo: VecDeque::new(), + written: presets, + bytes: 0, + } + } + pub(crate) fn can_undo(&self) -> bool { + !self.undo.is_empty() + } + pub(crate) fn can_redo(&self) -> bool { + !self.redo.is_empty() + } + pub(crate) fn previous(&self) -> Option<&Vec> { + self.undo.back().map(|(value, _)| value) + } + pub(crate) fn next(&self) -> Option<&Vec> { + self.redo.back().map(|(value, _)| value) + } + + pub(crate) fn record(&mut self, step: Step, outcome: &EditOutcome) { + match step { + Step::Edit => { + if outcome.before != outcome.after { + for (_, bytes) in self.redo.drain(..) { + self.bytes -= bytes; + } + push(&mut self.undo, &mut self.bytes, &outcome.before); + } + } + Step::Undo => { + pop(&mut self.undo, &mut self.bytes); + push(&mut self.redo, &mut self.bytes, &outcome.before); + } + Step::Redo => { + pop(&mut self.redo, &mut self.bytes); + push(&mut self.undo, &mut self.bytes, &outcome.before); + } + } + while self.bytes > MAX_BYTES { + let removed = self.undo.pop_front().or_else(|| self.redo.pop_front()); + if let Some((_, bytes)) = removed { + self.bytes -= bytes; + } else { + break; + } + } + self.written = outcome.after.clone(); + } + pub(crate) fn observe(&mut self, stored: &[LayoutPreset]) -> bool { + if self.written == stored { + return false; + } + self.undo.clear(); + self.redo.clear(); + self.bytes = 0; + self.written = stored.to_vec(); + true + } +} +fn pop(steps: &mut VecDeque, bytes: &mut usize) { + if let Some((_, removed)) = steps.pop_back() { + *bytes -= removed; + } +} +fn push(steps: &mut VecDeque, bytes: &mut usize, presets: &[LayoutPreset]) { + let size = snapshot_bytes(presets); + if size > MAX_BYTES { + for (_, removed) in steps.drain(..) { + *bytes -= removed; + } + return; + } + steps.push_back((presets.to_vec(), size)); + *bytes += size; + if steps.len() > DEPTH + && let Some((_, removed)) = steps.pop_front() + { + *bytes -= removed; + } +} +/// Counts allocations made by cloning this document, including all nested owned strings. +fn snapshot_bytes(presets: &[LayoutPreset]) -> usize { + let mut bytes = std::mem::size_of_val(presets); + for preset in presets { + bytes += preset.id.as_str().len() + preset.name.len(); + for ids in [&preset.tray, &preset.open] { + bytes += std::mem::size_of_val(ids.as_slice()); + bytes += ids.iter().map(|id| id.as_str().len()).sum::(); + } + for zone in [&preset.bar.left, &preset.bar.center, &preset.bar.right] { + bytes += std::mem::size_of_val(zone.as_slice()); + for item in zone { + bytes += match item { + BarItem::Item { id } | BarItem::Panel { id, .. } => id.as_str().len(), + BarItem::Separator => 0, + BarItem::Group { + id, name, members, .. + } => { + id.as_str().len() + + name.as_ref().map_or(0, String::len) + + std::mem::size_of_val(members.as_slice()) + + members + .iter() + .map(|member| match member { + GroupEntry::Panel(id) => id.as_str().len(), + GroupEntry::Separator => 0, + }) + .sum::() + } + }; + } + } + } + bytes +} + +#[cfg(test)] +mod tests { + use super::*; + use nocterm_window_layout::PresetId; + + #[test] + fn a_snapshot_larger_than_the_byte_budget_is_not_offered_for_undo() { + let before = vec![LayoutPreset { + name: "large".repeat(MAX_BYTES / 5 + 1), + ..Default::default() + }]; + let mut history = History::new(before.clone()); + history.record( + Step::Edit, + &EditOutcome { + before, + after: Vec::new(), + selected: PresetId::new("default"), + }, + ); + assert!(!history.can_undo()); + assert!(history.previous().is_none()); + } + + #[test] + fn an_unrelated_publication_keeps_the_same_undo_step() { + let before = vec![LayoutPreset { + name: "Original".into(), + ..Default::default() + }]; + let mut after = before.clone(); + after[0].name = "Changed".into(); + let mut history = History::new(before.clone()); + history.record( + Step::Edit, + &EditOutcome { + before: before.clone(), + after: after.clone(), + selected: PresetId::new("default"), + }, + ); + assert!(!history.observe(&after)); + assert_eq!(history.previous(), Some(&before)); + } +} diff --git a/crates/layout-ui/src/keys.rs b/crates/layout-ui/src/keys.rs new file mode 100644 index 00000000..c3989936 --- /dev/null +++ b/crates/layout-ui/src/keys.rs @@ -0,0 +1,75 @@ +//! The keys of the editor, apart from what they do. + +use gpui_kit::Keystroke; + +/// What a key asks of the selected item or of the history. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub(crate) enum KeyCommand { + Undo, + Redo, + /// One place earlier (-1) or later (+1) in its list. + Move(i32), + /// One zone toward the left (-1) or the right (+1). + Zone(i32), + /// To the next panel group. + NextGroup, + /// A separator after the item. + Separator, + /// Out of the bar, into the tray. + Remove, +} + +pub(crate) fn command(stroke: &Keystroke) -> Option { + let modifiers = &stroke.modifiers; + let key = stroke.key.as_str(); + if (modifiers.control || modifiers.platform) && !modifiers.alt { + return match (key, modifiers.shift) { + ("z", false) => Some(KeyCommand::Undo), + ("z", true) | ("y", false) => Some(KeyCommand::Redo), + _ => None, + }; + } + if modifiers.alt && !modifiers.control && !modifiers.platform && !modifiers.shift { + return match key { + "left" => Some(KeyCommand::Move(-1)), + "right" => Some(KeyCommand::Move(1)), + "up" => Some(KeyCommand::Zone(-1)), + "down" => Some(KeyCommand::Zone(1)), + _ => None, + }; + } + if modifiers.control || modifiers.platform || modifiers.alt { + return None; + } + let typed = stroke.key_char.as_deref().unwrap_or(key); + match typed { + "g" | "G" => Some(KeyCommand::NextGroup), + "|" => Some(KeyCommand::Separator), + "delete" => Some(KeyCommand::Remove), + _ => None, + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn stroke(text: &str) -> KeyCommand { + command(&Keystroke::parse(text).unwrap()).unwrap() + } + + #[test] + fn the_keys_name_their_commands() { + assert_eq!(stroke("alt-left"), KeyCommand::Move(-1)); + assert_eq!(stroke("alt-right"), KeyCommand::Move(1)); + assert_eq!(stroke("alt-up"), KeyCommand::Zone(-1)); + assert_eq!(stroke("alt-down"), KeyCommand::Zone(1)); + assert_eq!(stroke("g"), KeyCommand::NextGroup); + assert_eq!(stroke("delete"), KeyCommand::Remove); + assert_eq!(command(&Keystroke::parse("backspace").unwrap()), None); + assert_eq!(stroke("ctrl-z"), KeyCommand::Undo); + assert_eq!(stroke("cmd-z"), KeyCommand::Undo); + assert_eq!(stroke("ctrl-shift-z"), KeyCommand::Redo); + assert!(command(&Keystroke::parse("ctrl-g").unwrap()).is_none()); + } +} diff --git a/crates/layout-ui/src/lib.rs b/crates/layout-ui/src/lib.rs new file mode 100644 index 00000000..a0bc0d43 --- /dev/null +++ b/crates/layout-ui/src/lib.rs @@ -0,0 +1,85 @@ +//! The editor of the status bar and the panels: which layout is used and where each item of it +//! sits, in one section of Appearance. Everything is moved by dragging it in a preview of the +//! bar, or through the item's menu. +//! +//! Owns: the section's view and its state (the layout shown, the selected item, undo and redo, +//! renaming a layout or a group and confirming a delete), the drags, menus and keys that edit +//! the bar (a drop names a place, found from the pointer against what the preview painted; the +//! rules of the move stay in `nocterm-window-layout`), how the bar preview fits its one line, +//! and what a sent edit led to (the notice, the selection that follows it, the history step). +//! Does not own: the rules of layouts and their edits (`nocterm-window-layout`), the write of +//! an edit (`nocterm_workspace::edit_layouts`, which refuses a `[layouts]` table that does not +//! read), what the plugins are or whether they are on (`nocterm-plugin-model`, +//! `nocterm-plugin-enablement`), the settings of a plugin (the Plugins page, which an item's +//! menu opens), the Settings host, or how the footer and the panels are drawn +//! (`nocterm-workspace`). +//! Ports used: `SettingsHandle` (the layouts and the enablement), `UserPluginFacts` (what the +//! host knows of a user plugin, in the tooltip of its items), `nocterm-workspace` +//! (`edit_layouts` and its `LayoutEditError`, the `OpenPlugins` action). Provided: +//! [`layout_section`], an `EmbeddedPageSpec` for the application to place in Appearance. +//! Settings section: none of its own; it edits `layouts` (window-layout) through the rules. +//! Globals: none. +//! Invariants: every change to the layouts goes through the pure `edit` and shows the result of +//! the write, never a guess; editing the layout in use changes the footer at once; editing a +//! built-in layout in use saves a custom copy that stays in use; history is dropped when +//! anything else changes the layouts. +//! Undo is bounded to 100 steps and 8 MiB of cloned snapshots per editor. The layout shown is +//! resolved once for each settings revision, set of plugins and layout picked, and its bar is +//! fitted once for each width, so a drag, which draws the window on every move, redraws cheaply. + +mod about; +mod commands; +mod dnd; +mod drops; +mod face; +mod fit; +mod history; +mod keys; +mod menu; +mod parts; +mod presets; +mod preview; +mod rename; +mod section; +mod selection; +mod snapshot; +mod tray; +mod user_facts; + +use gpui_kit::AppContext as _; +use nocterm_store::SettingsHandle; +use std::rc::Rc; + +use nocterm_ui::{EmbeddedPageSpec, PluginSource}; + +pub use section::LayoutEditorSection; +pub use user_facts::{UserFacts, UserPluginFacts}; + +/// The section of Appearance that edits the status bar and the panels. +/// +/// `facts` tells what the host knows of an installed user plugin; without it an item of a user +/// plugin shows its name and its plugin alone. +/// +/// `legacy_swapped` is the frozen `workspace.swapped` flag of the earlier swap-sides choice, which +/// decides what the built-in layout in use is while nothing else has been chosen. +pub fn layout_section( + settings: SettingsHandle, + source: Rc, + facts: Option>, + legacy_swapped: bool, +) -> EmbeddedPageSpec { + EmbeddedPageSpec::new("layout", "Status bar and panels", move |_, cx| { + cx.new(|cx| { + LayoutEditorSection::new( + settings.clone(), + source.clone(), + facts.clone(), + legacy_swapped, + cx, + ) + }) + }) +} + +#[cfg(test)] +mod tests; diff --git a/crates/layout-ui/src/menu.rs b/crates/layout-ui/src/menu.rs new file mode 100644 index 00000000..7febb5d2 --- /dev/null +++ b/crates/layout-ui/src/menu.rs @@ -0,0 +1,480 @@ +//! What one item of the bar offers in its menu: move it, group it, separate it, hide it or add +//! it to the bar. The list is plain data, built when a menu opens, so the preview and the tray +//! build the same menu and a test reads it without opening one. + +use std::{cell::RefCell, rc::Rc, sync::Arc}; + +use gpui_kit::{ + App, Context, SharedString, Window, + component::menu::{PopupMenu, PopupMenuItem}, +}; +use nocterm_plugin_model::{ + ContributionId, ContributionKind, GroupId, PluginId, PluginSet, SetOwner, Side, Zone, +}; +use nocterm_window_layout::{EditOp, ResolvedGroup, Slot, Subject}; +use nocterm_workspace::OpenPlugins; + +use crate::{commands::Commands, snapshot::Snapshot}; + +const ZONES: [(Zone, &str); 3] = [ + (Zone::Left, "Left"), + (Zone::Center, "Centre"), + (Zone::Right, "Right"), +]; + +/// One line of a menu. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Entry { + /// Sends `op`; greyed out when `enabled` is false, with a check mark when `checked`. + Do { + label: String, + op: EditOp, + enabled: bool, + checked: bool, + }, + /// Asks something that is not an edit of the layout. + Ask { + label: String, + ask: Ask, + enabled: bool, + }, + Submenu { + label: String, + entries: Vec, + }, + Rule, +} + +/// What a menu entry asks for besides an edit. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Ask { + /// Type a new name for the group. + RenameGroup(GroupId), + /// Open the plugin on the Plugins page, where its settings are. + Plugin(PluginId), +} + +impl Ask { + fn run(&self, commands: &Commands, window: &mut Window, cx: &mut App) { + match self { + Self::RenameGroup(group) => { + let (group, commands) = (group.clone(), commands.clone()); + // After the menu has closed and handed the focus back, so the field keeps it. + window.defer(cx, move |window, cx| { + commands.run(window, cx, move |section, window, cx| { + section.begin_group_rename(group, window, cx); + }); + }); + } + Self::Plugin(plugin) => { + let action = OpenPlugins { + plugin: Some(plugin.as_str().to_owned().into()), + }; + window.dispatch_action(Box::new(action), cx); + } + } + } +} + +/// What a menu is opened on. +#[derive(Clone, Debug)] +pub(crate) enum MenuOf { + Item(Subject), + /// The separator at `index` of a list. + Separator(Slot, usize), +} + +/// The menus of the items of the layout as it was drawn, each built when it opens. +#[derive(Clone)] +pub(crate) struct Menus { + snapshot: Rc, + catalog: Arc, + commands: Commands, +} + +impl Menus { + pub(crate) fn new(snapshot: Rc, catalog: Arc, commands: Commands) -> Self { + Self { + snapshot, + catalog, + commands, + } + } + + /// Fills a popup with the entries of `of`; a click sends the operation to the section. + pub(crate) fn popup( + &self, + of: &MenuOf, + menu: PopupMenu, + window: &mut Window, + cx: &mut Context, + ) -> PopupMenu { + let entries = match of { + MenuOf::Item(subject) => entries(subject, &self.snapshot, &self.catalog), + MenuOf::Separator(place, index) => separator(place.clone(), *index, &self.snapshot), + }; + fill(menu, &entries, &self.commands, window, cx) + } +} + +/// The menu a right click inside a group opens. Menus do not nest, so the group's menu opens on +/// every right click within it; a panel or separator of the group claims that click on the way +/// down, and its own menu opens instead. +#[derive(Clone, Default)] +pub(crate) struct Claim(Rc>>); + +impl Claim { + pub(crate) fn claim(&self, of: MenuOf) { + self.0.replace(Some(of)); + } + + /// What a member claimed for this click, or the group's `own`. + pub(crate) fn take_or(&self, own: &MenuOf) -> MenuOf { + self.0.take().unwrap_or_else(|| own.clone()) + } +} + +fn fill( + mut menu: PopupMenu, + entries: &[Entry], + commands: &Commands, + window: &mut Window, + cx: &mut Context, +) -> PopupMenu { + for entry in entries { + menu = match entry { + Entry::Rule => menu.separator(), + Entry::Do { + label, + op, + enabled, + checked, + } => { + let (commands, op) = (commands.clone(), op.clone()); + menu.item( + PopupMenuItem::new(SharedString::from(label.clone())) + .disabled(!enabled) + .checked(*checked) + .on_click(move |_, window, cx| { + let op = op.clone(); + commands.run(window, cx, move |section, _, cx| section.edit(op, cx)); + }), + ) + } + Entry::Ask { + label, + ask, + enabled, + } => { + let (commands, ask) = (commands.clone(), ask.clone()); + menu.item( + PopupMenuItem::new(SharedString::from(label.clone())) + .disabled(!enabled) + .on_click(move |_, window, cx| ask.run(&commands, window, cx)), + ) + } + Entry::Submenu { label, entries } => { + let (commands, entries) = (commands.clone(), Rc::new(entries.clone())); + menu.submenu(label.clone(), window, cx, move |submenu, window, cx| { + fill(submenu, &entries, &commands, window, cx) + }) + } + }; + } + menu +} + +/// The entries for `subject`: the ones for an item in the bar, or for one waiting in the tray. +pub(crate) fn entries(subject: &Subject, snapshot: &Snapshot, catalog: &PluginSet) -> Vec { + let menu = Menu { + snapshot, + catalog, + editable: snapshot.editable(), + }; + let Some(zone) = snapshot.zone_of(subject) else { + return menu.unplaced(subject); + }; + match subject { + Subject::Group(group) => menu.group(group, zone), + Subject::Contribution(id) if is_panel(id, catalog) => menu.panel(id, zone), + Subject::Contribution(id) => menu.widget(id, zone), + } +} + +/// The entries of the separator at `index` of `place`. +pub(crate) fn separator(place: Slot, index: usize, snapshot: &Snapshot) -> Vec { + vec![Entry::Do { + label: "Remove separator".to_owned(), + op: EditOp::RemoveSeparator { place, index }, + enabled: snapshot.editable(), + checked: false, + }] +} + +struct Menu<'a> { + snapshot: &'a Snapshot, + catalog: &'a PluginSet, + editable: bool, +} + +impl Menu<'_> { + fn run(&self, label: impl Into, op: EditOp, enabled: bool) -> Entry { + Entry::Do { + label: label.into(), + op, + enabled: enabled && self.editable, + checked: false, + } + } + + fn widget(&self, id: &ContributionId, zone: Zone) -> Vec { + let subject = Subject::Contribution(id.clone()); + let mut entries = self.zones(&subject, zone); + entries.push(Entry::Rule); + entries.extend(self.shifts(&subject)); + entries.push(Entry::Rule); + entries.extend(self.separators(&subject)); + entries.extend(self.tail(id)); + entries + } + + /// A panel on its own moves between zones like a widget; a panel of a group can leave it. + fn panel(&self, id: &ContributionId, zone: Zone) -> Vec { + let subject = Subject::Contribution(id.clone()); + let group = self.snapshot.group_of(id); + let mut entries = Vec::new(); + match group.filter(|group| !group.is_solo()) { + Some(group) => { + if let Some((place, index)) = + self.snapshot.after(&Subject::Group(group.id().clone())) + { + let op = EditOp::MoveTo { + subject: subject.clone(), + place, + index, + }; + entries.push(self.run("Take out of group", op, true)); + } + } + None => entries.extend(self.zones(&subject, zone)), + } + entries.extend(self.join_group(id)); + let op = EditOp::NewGroup { panel: id.clone() }; + entries.push(self.run("New group", op, true)); + entries.push(Entry::Rule); + entries.extend(self.shifts(&subject)); + entries.push(Entry::Rule); + let open = self.snapshot.opens_with_layout(id); + entries.push(Entry::Do { + label: "Open with the layout".to_owned(), + op: EditOp::SetOpen { + panel: id.clone(), + open: !open, + }, + enabled: self.editable, + checked: open, + }); + match group { + Some(group) if group.is_solo() => entries.extend(self.sides(group, zone)), + Some(group) => entries.push(Entry::Submenu { + label: format!("Group \u{201c}{}\u{201d}", self.snapshot.group_title(group)), + entries: self.group(group.id(), zone), + }), + None => {} + } + entries.push(Entry::Rule); + entries.extend(self.separators(&subject)); + entries.extend(self.tail(id)); + entries + } + + fn group(&self, id: &GroupId, zone: Zone) -> Vec { + let subject = Subject::Group(id.clone()); + let mut entries = self.zones(&subject, zone); + entries.push(Entry::Rule); + entries.extend(self.shifts(&subject)); + entries.push(Entry::Rule); + entries.push(Entry::Ask { + label: "Rename group\u{2026}".to_owned(), + ask: Ask::RenameGroup(id.clone()), + enabled: self.editable, + }); + let group = self.snapshot.group(id); + if group.is_some_and(|group| !group.is_primary()) { + let op = EditOp::Ungroup { group: id.clone() }; + entries.push(self.run("Ungroup", op, true)); + } + if let Some(group) = group { + entries.extend(self.sides(group, zone)); + } + entries.push(Entry::Rule); + entries.extend(self.separators(&subject)); + entries + } + + /// "Move to" each other zone. + fn zones(&self, subject: &Subject, zone: Zone) -> Vec { + ZONES + .into_iter() + .filter(|(to, _)| *to != zone) + .map(|(to, name)| { + let op = EditOp::ToZone { + subject: subject.clone(), + zone: to, + }; + self.run(format!("Move to {name}"), op, true) + }) + .collect() + } + + fn shifts(&self, subject: &Subject) -> Vec { + [("Move left", -1), ("Move right", 1)] + .into_iter() + .map(|(label, by)| { + let op = EditOp::Move { + subject: subject.clone(), + by, + }; + self.run(label, op, self.snapshot.can_shift(subject, by)) + }) + .collect() + } + + fn separators(&self, subject: &Subject) -> Vec { + [ + ("Add separator before", self.snapshot.before(subject)), + ("Add separator after", self.snapshot.after(subject)), + ] + .into_iter() + .filter_map(|(label, at)| { + let (place, index) = at?; + Some(self.run(label, EditOp::InsertSeparator { place, index }, true)) + }) + .collect() + } + + /// Where a group in the centre opens; a group in a side zone opens on that side. + fn sides(&self, group: &ResolvedGroup, zone: Zone) -> Vec { + if zone != Zone::Center { + return Vec::new(); + } + let entries = [(Side::Left, "Left"), (Side::Right, "Right")] + .into_iter() + .map(|(side, label)| Entry::Do { + label: label.to_owned(), + op: EditOp::SetSide { + group: group.id().clone(), + side: Some(side), + }, + enabled: self.editable, + checked: group.side() == side, + }) + .collect(); + vec![Entry::Submenu { + label: "Opens on".to_owned(), + entries, + }] + } + + /// "Move into group", with the groups other than the panel's own. + fn join_group(&self, panel: &ContributionId) -> Option { + let current = self + .snapshot + .group_of(panel) + .map(|group| group.id().clone()); + let entries = self + .snapshot + .group_choices() + .into_iter() + .filter(|(group, _)| Some(group) != current.as_ref()) + .map(|(group, title)| { + let op = EditOp::ToGroup { + panel: panel.clone(), + group, + }; + self.run(title, op, true) + }) + .collect::>(); + (!entries.is_empty()).then(|| Entry::Submenu { + label: "Move into group".to_owned(), + entries, + }) + } + + /// The plugin's settings and hiding, the end of the menu of a contribution. + fn tail(&self, id: &ContributionId) -> Vec { + let mut entries = self.settings(id); + if self + .catalog + .contribution(id) + .is_some_and(|spec| !spec.pinned()) + { + entries.push(Entry::Rule); + let op = EditOp::RemoveToTray { id: id.clone() }; + entries.push(self.run("Hide from the bar", op, true)); + } + entries + } + + /// The entries for a contribution that is not in the bar. + /// + /// A widget can be added to any zone. A panel is added to the group its hints name; when + /// that group is not in the bar either, the group comes with it into any zone. + fn unplaced(&self, subject: &Subject) -> Vec { + let Subject::Contribution(id) = subject else { + return Vec::new(); + }; + let host = self + .snapshot + .layout() + .host_of(id) + .filter(|_| is_panel(id, self.catalog)) + .filter(|group| self.snapshot.zone_of_group(group).is_some()); + let mut entries = match host.and_then(|group| self.snapshot.group(group)) { + Some(group) => { + let title = self.snapshot.group_title(group); + let op = EditOp::Restore { id: id.clone() }; + vec![self.run(format!("Add to {title}"), op, true)] + } + None => { + let zones = ZONES + .into_iter() + .map(|(zone, name)| { + let op = EditOp::PlaceIn { + id: id.clone(), + zone, + }; + self.run(name, op, true) + }) + .collect(); + vec![Entry::Submenu { + label: "Add to bar".to_owned(), + entries: zones, + }] + } + }; + entries.extend(self.settings(id)); + entries + } + + /// The way to the settings of the plugin `id` belongs to, which the Plugins page shows. + fn settings(&self, id: &ContributionId) -> Vec { + let Some(SetOwner::Plugin(plugin)) = self.catalog.owner(id) else { + return Vec::new(); + }; + vec![ + Entry::Rule, + Entry::Ask { + label: format!("{} settings\u{2026}", plugin.name()), + ask: Ask::Plugin(plugin.id().clone()), + enabled: true, + }, + ] + } +} + +fn is_panel(id: &ContributionId, catalog: &PluginSet) -> bool { + catalog + .contribution(id) + .is_some_and(|spec| spec.kind() == ContributionKind::Panel) +} diff --git a/crates/layout-ui/src/parts.rs b/crates/layout-ui/src/parts.rs new file mode 100644 index 00000000..c388b4d3 --- /dev/null +++ b/crates/layout-ui/src/parts.rs @@ -0,0 +1,21 @@ +//! Small pieces the parts of the editor share. + +use gpui_kit::{ + App, ElementId, SharedString, Window, + component::{Disableable as _, Sizable as _, button::Button}, +}; + +/// A small outlined button. A disabled one ignores clicks. +pub(crate) fn action( + id: impl Into, + label: impl Into, + enabled: bool, + on_click: impl Fn(&mut Window, &mut App) + 'static, +) -> Button { + Button::new(id) + .small() + .outline() + .label(label) + .disabled(!enabled) + .on_click(move |_, window, cx| on_click(window, cx)) +} diff --git a/crates/layout-ui/src/presets.rs b/crates/layout-ui/src/presets.rs new file mode 100644 index 00000000..0047659b --- /dev/null +++ b/crates/layout-ui/src/presets.rs @@ -0,0 +1,359 @@ +//! The layouts to choose from and one line of tools under them: what can be done to the layout +//! shown, what just happened to it, and undo. Renaming and confirming a delete take that line +//! for themselves, so the bar below never moves. + +use std::rc::Rc; + +use gpui_kit::{ + AnyElement, App, Entity, IntoElement, RenderOnce, SharedString, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Disableable as _, Selectable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, + input::{Input, InputState}, + v_flex, + }, + div, + prelude::*, + rems, +}; +use nocterm_ui::IconName; +use nocterm_window_layout::EditOp; + +use crate::{ + commands::Commands, + parts::action, + rename::Renamed, + section::{LayoutEditorSection, Mode}, + snapshot::Snapshot, +}; + +/// The height of the line of tools, whatever it holds. +const TOOLS_HEIGHT_REMS: f32 = 2.; + +/// What the line of tools shows. +enum Line { + Tools, + Renaming { + what: &'static str, + input: Entity, + error: Option, + }, + ConfirmingDelete, +} + +/// A line about what just happened. +struct NoticeLine { + text: SharedString, + failed: bool, + undoable: bool, +} + +#[derive(IntoElement)] +pub(crate) struct Presets { + snapshot: Rc, + line: Line, + notice: Option, + can_undo: bool, + can_redo: bool, + commands: Commands, +} + +impl Presets { + pub(crate) fn new( + section: &LayoutEditorSection, + snapshot: Rc, + commands: Commands, + ) -> Self { + let line = match section.mode() { + Mode::Idle => Line::Tools, + Mode::Renaming(draft) => Line::Renaming { + what: match draft.renamed() { + Renamed::Layout(_) => "Layout name", + Renamed::Group(_) => "Group name", + }, + input: draft.input().clone(), + error: draft.error().cloned(), + }, + Mode::ConfirmingDelete => Line::ConfirmingDelete, + }; + Self { + snapshot, + line, + notice: section.notice().map(|notice| NoticeLine { + text: notice.text().clone(), + failed: notice.is_failure(), + undoable: notice.offers_undo(), + }), + can_undo: section.can_undo(), + can_redo: section.can_redo(), + commands, + } + } + + fn choices(&self) -> AnyElement { + let buttons = self.snapshot.choices().iter().map(|choice| { + let (commands, id) = (self.commands.clone(), choice.id().clone()); + let label = if choice.in_use() { + format!("{} \u{b7} in use", choice.name()) + } else { + choice.name().to_owned() + }; + Button::new(SharedString::from(format!("layout-choice:{}", choice.id()))) + .small() + .ghost() + .label(label) + .selected(choice.id() == self.snapshot.shown()) + .on_click(move |_, window, cx| { + let id = id.clone(); + commands.run(window, cx, move |section, window, cx| { + section.show(id, window, cx); + }); + }) + .into_any_element() + }); + h_flex() + .flex_wrap() + .gap_1() + .children(buttons) + .into_any_element() + } + + /// Duplicate, and for a saved layout Rename, Reset and Delete; then what just happened, and + /// the history. A built-in that is not in use cannot be changed, so its copy is offered. + fn tools(&self, cx: &App) -> AnyElement { + let commands = &self.commands; + let duplicate = { + let commands = commands.clone(); + let run = move |window: &mut Window, cx: &mut App| { + commands.run(window, cx, |section, _, cx| { + section.edit(EditOp::Duplicate, cx) + }); + }; + if self.snapshot.editable() { + action("layout-duplicate", "Duplicate", true, run) + } else { + Button::new("layout-duplicate-to-edit") + .small() + .primary() + .label("Duplicate to edit") + .on_click(move |_, window, cx| run(window, cx)) + } + }; + let saved = !self.snapshot.built_in(); + let rename = { + let commands = commands.clone(); + action("layout-rename", "Rename", true, move |window, cx| { + commands.run(window, cx, |section, window, cx| { + section.begin_rename(window, cx); + }); + }) + }; + let reset = { + let commands = commands.clone(); + action("layout-reset", "Reset", true, move |window, cx| { + commands.run(window, cx, |section, _, cx| section.edit(EditOp::Reset, cx)); + }) + }; + let delete = { + let commands = commands.clone(); + action("layout-delete", "Delete", true, move |window, cx| { + commands.run(window, cx, |section, _, cx| section.ask_delete(cx)); + }) + }; + let undo = { + let commands = commands.clone(); + history("layout-undo", "Undo", self.can_undo, move |window, cx| { + commands.run(window, cx, |section, _, cx| section.undo(cx)); + }) + }; + let redo = { + let commands = commands.clone(); + history("layout-redo", "Redo", self.can_redo, move |window, cx| { + commands.run(window, cx, |section, _, cx| section.redo(cx)); + }) + }; + h_flex() + .gap_2() + .child(duplicate) + .when(saved, |row| row.child(rename).child(reset).child(delete)) + .child( + div() + .flex_1() + .min_w_0() + .children(self.notice.as_ref().map(|notice| self.notice(notice, cx))), + ) + .child(undo) + .child(redo) + .into_any_element() + } + + fn renaming( + &self, + what: &'static str, + input: &Entity, + error: Option<&SharedString>, + cx: &App, + ) -> AnyElement { + let (save, cancel) = (self.commands.clone(), self.commands.clone()); + h_flex() + .id("layout-rename-row") + .test_support() + .gap_2() + .child( + div() + .text_xs() + .text_color(cx.theme().muted_foreground) + .child(what), + ) + .child(div().w(rems(16.)).child(Input::new(input).small())) + .child(action( + "layout-rename-save", + "Save", + true, + move |window, cx| { + save.run(window, cx, |section, window, cx| { + section.commit_rename(window, cx); + }); + }, + )) + .child(action( + "layout-rename-cancel", + "Cancel", + true, + move |window, cx| { + cancel.run(window, cx, |section, window, cx| { + section.cancel_rename(window, cx); + }); + }, + )) + .children(error.map(|error| { + div() + .id("layout-rename-error") + .aria_label(error.clone()) + .test_support() + .min_w_0() + .truncate() + .text_xs() + .text_color(cx.theme().danger) + .child(error.clone()) + })) + .into_any_element() + } + + fn confirming_delete(&self) -> AnyElement { + let (delete, cancel) = (self.commands.clone(), self.commands.clone()); + h_flex() + .id("layout-delete-confirm") + .aria_label("Delete this layout?") + .test_support() + .gap_2() + .child( + div() + .text_sm() + .child(format!("Delete \u{201c}{}\u{201d}?", self.snapshot.name())), + ) + .child( + Button::new("layout-delete-confirmed") + .small() + .danger() + .label("Delete layout") + .on_click(move |_, window, cx| { + delete.run(window, cx, |section, _, cx| section.confirm_delete(cx)); + }), + ) + .child(action( + "layout-delete-cancel", + "Keep it", + true, + move |window, cx| { + cancel.run(window, cx, |section, _, cx| section.cancel_delete(cx)); + }, + )) + .into_any_element() + } + + /// What just happened, on the line of tools: the text, Undo when the change can be undone, + /// and a way to dismiss it. + fn notice(&self, notice: &NoticeLine, cx: &App) -> AnyElement { + let (undo, dismiss) = (self.commands.clone(), self.commands.clone()); + let theme = cx.theme(); + h_flex() + .id("layout-notice") + .aria_label(notice.text.clone()) + .test_support() + .min_w_0() + .gap_1() + .child( + div() + .min_w_0() + .truncate() + .text_xs() + .text_color(if notice.failed { + theme.danger + } else { + theme.muted_foreground + }) + .child(notice.text.clone()), + ) + .when(notice.undoable && self.can_undo, |row| { + row.child( + Button::new("layout-notice-undo") + .xsmall() + .link() + .label("Undo") + .on_click(move |_, window, cx| { + undo.run(window, cx, |section, _, cx| section.undo(cx)); + }), + ) + }) + .child( + Button::new("layout-notice-dismiss") + .xsmall() + .ghost() + .icon(IconName::Close) + .on_click(move |_, window, cx| { + dismiss.run(window, cx, |section, _, cx| section.dismiss_notice(cx)); + }), + ) + .into_any_element() + } +} + +/// A quiet button of the history. +fn history( + id: &'static str, + label: &'static str, + enabled: bool, + on_click: impl Fn(&mut Window, &mut App) + 'static, +) -> Button { + Button::new(id) + .small() + .ghost() + .label(label) + .disabled(!enabled) + .on_click(move |_, window, cx| on_click(window, cx)) +} + +impl RenderOnce for Presets { + fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { + let line = match &self.line { + Line::Tools => self.tools(cx), + Line::Renaming { what, input, error } => self.renaming(what, input, error.as_ref(), cx), + Line::ConfirmingDelete => self.confirming_delete(), + }; + v_flex() + .id("layout-presets") + .test_support() + .gap_2() + .child(self.choices()) + .child( + h_flex() + .id("layout-tools") + .test_support() + .h(rems(TOOLS_HEIGHT_REMS)) + .items_center() + .child(div().flex_1().min_w_0().child(line)), + ) + } +} diff --git a/crates/layout-ui/src/preview.rs b/crates/layout-ui/src/preview.rs new file mode 100644 index 00000000..d009aa61 --- /dev/null +++ b/crates/layout-ui/src/preview.rs @@ -0,0 +1,573 @@ +//! The status bar as it will look, on one line, where it is edited: the left zone flush left, +//! the centre in the middle and the right zone flush right, drawn from the preview text of each +//! widget. A group has a background of its own, and the panel that opens with the layout is +//! marked with the accent. +//! +//! Every chip and group is a drag source and opens its menu, built when it opens, on a right +//! click. The whole preview takes a drop, where `drops` says it lands; while an item is held +//! over it, one accent line in a gap shows where it would go, or a tint the panel it would make +//! a group with. The line never wraps: when it is too wide the chips lose their text and keep +//! their icons, and when even those do not fit it scrolls sideways. + +use std::{rc::Rc, sync::Arc}; + +use gpui_kit::{ + AnyElement, App, Entity, FocusHandle, InteractiveElement, IntoElement, MouseButton, + ParentElement, Pixels, RenderOnce, SharedString, StatefulInteractiveElement as _, Styled, + TestSupportExt as _, Toggled, Window, + base::ElementExt as _, + component::{ActiveTheme as _, h_flex, menu::ContextMenuExt as _, tooltip::Tooltip}, + div, + prelude::*, + px, +}; +use nocterm_plugin_model::{ContributionId, ContributionKind, ContributionRef, PluginSet, Zone}; +use nocterm_ui::{DragSource, contribution_icon}; +use nocterm_window_layout::{ResolvedGroup, ResolvedItem, ResolvedMember, Slot, Subject}; + +use crate::{ + about, + commands::Commands, + dnd::{self, Dragged, Landing}, + drops::{self, Drops, Holds, Spot}, + face::{self, BORDER, CHIP_HEIGHT, Face, Look, PILL_PADDING}, + fit::{Density, EMPTY, Fit, Spread}, + keys, + menu::{Claim, MenuOf, Menus}, + snapshot::Snapshot, + user_facts::SharedFacts, +}; + +/// How visible the item being dragged stays in its place. +const MOVING: f32 = 0.4; +/// How much a chip under the pointer darkens, or lightens in a dark theme. +const HOVER: f32 = 0.07; + +#[derive(IntoElement)] +pub(crate) struct Preview { + snapshot: Rc, + catalog: Arc, + selected: Option, + facts: Option, + fit: Fit, + focus: FocusHandle, + commands: Commands, +} + +/// One drawing of the preview, and the drag held over it. +struct Bar<'a> { + preview: &'a Preview, + density: Density, + menus: Menus, + /// Where the entries painted are recorded; `None` when the layout shown takes no drops. + drops: Option>, + /// Where the item held over the preview would land. + aimed: Option, + /// The item being dragged. + moving: Option, +} + +/// Where a chip is, and the group it is drawn in, which opens the chip's menu for it. +struct Setting { + spot: Spot, + host: Option, +} + +impl Preview { + pub(crate) fn new( + snapshot: Rc, + catalog: Arc, + selected: Option, + facts: Option, + fit: Fit, + focus: FocusHandle, + commands: Commands, + ) -> Self { + Self { + snapshot, + catalog, + selected, + facts, + fit, + focus, + commands, + } + } +} + +impl Bar<'_> { + fn snapshot(&self) -> &Snapshot { + &self.preview.snapshot + } + + fn zone(&self, zone: Zone, spread: Spread, cx: &App) -> AnyElement { + let items = self.snapshot().layout().zone(zone); + let name = name(zone); + let place = Slot::Zone(zone); + let gap_id = |index| move || format!("layout-preview-gap:{name}:{index}"); + let mut entries = Vec::with_capacity(2 * items.len() + 1); + for (index, item) in items.iter().enumerate() { + entries.push(self.gap(gap_id(index), &place, index, cx)); + entries.push(self.item(zone, index, item, cx)); + } + entries.push(self.gap(gap_id(items.len()), &place, items.len(), cx)); + let row = h_flex() + .id(SharedString::from(format!("layout-preview-zone:{name}"))) + .test_support() + .min_w(px(EMPTY)) + .h(px(CHIP_HEIGHT + 2. * PILL_PADDING)) + .items_center() + .rounded(cx.theme().radius) + .border_1() + .border_color(gpui_kit::transparent_black()) + .children(entries); + let row = match &self.drops { + Some(drops) => { + let drops = drops.clone(); + dnd::measured(row, BORDER, move |bounds, _, cx| { + drops.update(cx, |drops, _| drops.zone(zone, bounds)); + }) + } + None => row, + }; + match (zone, spread) { + (Zone::Left, Spread::Centred) => row.flex_1().justify_start(), + (Zone::Right, Spread::Centred) => row.flex_1().justify_end(), + _ => row.flex_none(), + } + .into_any_element() + } + + /// The room in front of the entry at `index` of `place`, or after the last one, which + /// shows the line where a drop would land. + fn gap(&self, id: impl FnOnce() -> String, place: &Slot, index: usize, cx: &App) -> AnyElement { + let marked = self + .aimed + .as_ref() + .is_some_and(|aimed| aimed.is_at(place, index)); + let gap = face::gap().when(marked, |gap| gap.child(face::line(cx))); + if self.drops.is_some() { + gap.id(SharedString::from(id())) + .test_support() + .into_any_element() + } else { + gap.into_any_element() + } + } + + fn item(&self, zone: Zone, index: usize, item: &ResolvedItem, cx: &App) -> AnyElement { + let spot = |holds| Spot::new(Slot::Zone(zone), index, holds); + match item { + ResolvedItem::Widget(id) => { + let setting = Setting { + spot: spot(Holds::Item), + host: None, + }; + self.contribution(id, setting, cx) + } + ResolvedItem::Separator => self.separator( + SharedString::from(format!("layout-preview-separator:{}:{index}", name(zone))), + spot(Holds::Item), + None, + cx, + ), + ResolvedItem::Group(group) if group.is_solo() => { + let Some(ResolvedMember::Panel(panel)) = group.members().first() else { + return div().into_any_element(); + }; + let setting = Setting { + spot: spot(Holds::Alone(panel.clone())), + host: None, + }; + self.contribution(panel, setting, cx) + } + ResolvedItem::Group(group) => { + self.group(group, spot(Holds::Group(group.id().clone())), cx) + } + } + } + + fn contribution(&self, id: &ContributionId, setting: Setting, cx: &App) -> AnyElement { + match self.preview.catalog.contribution(id) { + Some(contribution) => self.chip(contribution, setting, cx), + None => div().into_any_element(), + } + } + + /// A group: its panels on a background of their own, without a title. + fn group(&self, group: &ResolvedGroup, spot: Spot, cx: &App) -> AnyElement { + let subject = Subject::Group(group.id().clone()); + let selected = self.preview.selected.as_ref() == Some(&subject); + let title: SharedString = self.snapshot().group_title(group).into(); + let claim = Claim::default(); + let members = self.members(group, &claim, cx); + let empty = members.is_empty(); + let joined = empty + && self + .aimed + .as_ref() + .is_some_and(|aimed| aimed.is_at(&Slot::Group(group.id().clone()), 0)); + let (commands, picked) = (self.preview.commands.clone(), subject.clone()); + let theme = cx.theme(); + let pill = face::pill(empty, cx) + .id(SharedString::from(format!( + "layout-preview-group:{}", + group.id() + ))) + .aria_label(title.clone()) + .aria_selected(selected) + .test_support() + .bg(theme.secondary_active) + .when(selected, |pill| pill.border_color(theme.primary)) + .when(self.moving.as_ref() == Some(&subject), |pill| { + pill.opacity(MOVING) + }) + .cursor_pointer() + .on_click(move |_, window, cx| { + // A release that ends a drag started on a member is the drop, not a click. + if !cx.has_active_drag() { + pick(&commands, picked.clone(), window, cx); + } + }) + .when(empty, |pill| { + let tooltip = title.clone(); + pill.child(face::title(title, cx)) + .tooltip(move |_, cx| cx.new(|_| Tooltip::new(tooltip.clone())).into()) + }) + .children(members) + .when(joined, |pill| pill.child(face::tint(cx))); + let pill = match &self.drops { + Some(drops) => { + let source = DragSource::default(); + let dragged = Dragged::group(group.id(), spot.from()); + let pill = painted(pill, spot, Some(source.clone()), drops, BORDER); + let (snapshot, catalog) = + (self.preview.snapshot.clone(), self.preview.catalog.clone()); + let id = group.id().clone(); + dnd::source(pill, dragged, source, move || { + group_look(&snapshot, &catalog, &id) + }) + } + None => pill, + }; + let (menus, own) = (self.menus.clone(), MenuOf::Item(subject)); + pill.context_menu(move |popup, window, cx| { + menus.popup(&claim.take_or(&own), popup, window, cx) + }) + .into_any_element() + } + + /// The panels and separators of a group, with the gaps around them. + fn members(&self, group: &ResolvedGroup, claim: &Claim, cx: &App) -> Vec { + let members = group.members(); + if members.is_empty() { + return Vec::new(); + } + let place = Slot::Group(group.id().clone()); + let gap_id = |index| move || format!("layout-preview-member-gap:{}:{index}", group.id()); + let mut entries = Vec::with_capacity(2 * members.len() + 1); + for (member, entry) in members.iter().enumerate() { + entries.push(self.gap(gap_id(member), &place, member, cx)); + let spot = Spot::new(place.clone(), member, Holds::Item); + entries.push(match entry { + ResolvedMember::Panel(id) => { + let host = Some(claim.clone()); + self.contribution(id, Setting { spot, host }, cx) + } + ResolvedMember::Separator => self.separator( + SharedString::from(format!( + "layout-preview-member-separator:{}:{member}", + group.id() + )), + spot, + Some(claim), + cx, + ), + }); + } + entries.push(self.gap(gap_id(members.len()), &place, members.len(), cx)); + entries + } + + fn separator( + &self, + id: SharedString, + spot: Spot, + host: Option<&Claim>, + cx: &App, + ) -> AnyElement { + let (place, index) = spot.from(); + let line = face::separator(cx).id(id).test_support(); + let line = match &self.drops { + Some(drops) => painted(line, spot, None, drops, 0.), + None => line, + }; + with_menu(line, &self.menus, MenuOf::Separator(place, index), host) + } + + /// A widget or a panel toggle. A widget shows its sample text while the density allows; + /// otherwise the chip is its icon and the tooltip names it. + fn chip(&self, contribution: ContributionRef<'_>, setting: Setting, cx: &App) -> AnyElement { + let snapshot = self.snapshot(); + let id = contribution.id().clone(); + let subject = Subject::Contribution(id.clone()); + let selected = self.preview.selected.as_ref() == Some(&subject); + let disabled = snapshot.is_disabled(&id, &self.preview.catalog); + let panel = contribution.kind() == ContributionKind::Panel; + let open = panel && snapshot.opens_with_layout(&id); + let text = (self.density == Density::Full && !panel).then(|| { + SharedString::from( + contribution + .preview() + .unwrap_or(contribution.title()) + .to_owned(), + ) + }); + let face = Face::new(contribution_icon(contribution.icon()), text, open); + let name: SharedString = if disabled { + format!("{} (disabled)", contribution.title()).into() + } else { + contribution.title().to_owned().into() + }; + let grouped = matches!(&self.aimed, Some(Landing::With(target)) if *target == id); + let (commands, picked) = (self.preview.commands.clone(), subject.clone()); + let theme = cx.theme(); + let chip = face + .clone() + .fill(h_flex(), cx) + .id(SharedString::from(format!("layout-preview:{id}"))) + .aria_label(name.clone()) + .aria_selected(selected) + .when(panel, |chip| { + chip.aria_toggled(if open { Toggled::True } else { Toggled::False }) + }) + .test_support() + .when(selected, |chip| chip.border_color(theme.primary)) + .when(!open, |chip| { + chip.hover(|style| style.bg(theme.foreground.opacity(HOVER))) + }) + .when(disabled, |chip| chip.opacity(0.5)) + .when(self.moving.as_ref() == Some(&subject), |chip| { + chip.opacity(MOVING) + }) + .cursor_pointer() + .on_click(move |_, window, cx| { + // A release that ends a drag is the drop; the group around a panel is not + // picked by the same click. + if !cx.has_active_drag() { + cx.stop_propagation(); + pick(&commands, picked.clone(), window, cx); + } + }) + .tooltip(about::tooltip( + &self.preview.catalog, + self.preview.facts.as_ref(), + &id, + name, + open, + )) + .when(grouped, |chip| chip.child(face::tint(cx))); + let chip = match &self.drops { + Some(drops) => { + let source = DragSource::default(); + let dragged = Dragged::contribution(contribution, Some(setting.spot.from())); + let chip = painted(chip, setting.spot, Some(source.clone()), drops, BORDER); + dnd::source(chip, dragged, source, move || Look::Chip(face.clone())) + } + None => chip, + }; + with_menu( + chip, + &self.menus, + MenuOf::Item(subject), + setting.host.as_ref(), + ) + } +} + +/// What the ghost of the group `id` shows: its panels, or its title while it has none. +fn group_look( + snapshot: &Snapshot, + catalog: &PluginSet, + id: &nocterm_plugin_model::GroupId, +) -> Look { + let group = snapshot.group(id); + let members = group.map_or_else(Vec::new, |group| { + group + .members() + .iter() + .map(|member| match member { + ResolvedMember::Panel(panel) => catalog.contribution(panel).map(|contribution| { + let open = snapshot.opens_with_layout(panel); + Face::new(contribution_icon(contribution.icon()), None, open) + }), + ResolvedMember::Separator => None, + }) + .collect() + }); + let title = group + .map(|group| snapshot.group_title(group)) + .unwrap_or_default(); + Look::Group { + members, + title: title.into(), + } +} + +/// `element` recording, each time it is painted, where it is as `spot` in `drops`, and +/// measuring itself for the ghost of `source`. +fn painted( + element: E, + spot: Spot, + source: Option, + drops: &Entity, + border: f32, +) -> E { + let drops = drops.clone(); + dnd::measured(element, border, move |bounds, window, cx| { + if let Some(source) = &source { + source.capture(bounds, window); + } + drops.update(cx, |drops, _| drops.spot(spot, bounds)); + }) +} + +/// Selects `subject` for the keys of the editor. +fn pick(commands: &Commands, subject: Subject, window: &mut Window, cx: &mut App) { + commands.run(window, cx, move |section, window, cx| { + section.pick(Some(subject), window, cx); + }); +} + +/// `element` opening the menu `of` on a right click: by itself, or through the group it is +/// drawn in. +fn with_menu(element: E, menus: &Menus, of: MenuOf, host: Option<&Claim>) -> AnyElement +where + E: InteractiveElement + ParentElement + Styled + IntoElement + 'static, +{ + match host { + Some(host) => { + let host = host.clone(); + element + .on_mouse_down(MouseButton::Right, move |_, _, _| host.claim(of.clone())) + .into_any_element() + } + None => { + let menus = menus.clone(); + element + .context_menu(move |popup, window, cx| menus.popup(&of, popup, window, cx)) + .into_any_element() + } + } +} + +fn name(zone: Zone) -> &'static str { + match zone { + Zone::Left => "left", + Zone::Center => "center", + Zone::Right => "right", + } +} + +impl<'a> Bar<'a> { + /// A drawing of `preview` at `density`, with the drag held over it when the layout takes + /// drops. A drag that ended since the last drawing is forgotten. + fn new(preview: &'a Preview, density: Density, window: &mut Window, cx: &mut App) -> Self { + let drops = preview + .snapshot + .editable() + .then(|| window.use_keyed_state("layout-preview-drops", cx, |_, _| Drops::default())); + let held = cx.has_active_drag(); + let (aimed, moving) = drops.as_ref().map_or((None, None), |drops| { + drops.update(cx, |drops, _| { + if !held { + drops.settle(); + } + drops.repaint(); + (drops.aimed().cloned(), drops.moving().cloned()) + }) + }); + let menus = Menus::new( + preview.snapshot.clone(), + preview.catalog.clone(), + preview.commands.clone(), + ); + Self { + preview, + density, + menus, + drops, + aimed, + moving, + } + } + + /// The three zones on one line, as wide as the box, or as the bar needs when it scrolls. + fn line(&self, spread: Spread, scroll: Option, cx: &App) -> AnyElement { + let line = h_flex() + .id("layout-preview-line") + .test_support() + .items_center() + .gap_x_3() + .when(spread == Spread::Packed, |line| line.justify_between()) + .child(self.zone(Zone::Left, spread, cx)) + .child(self.zone(Zone::Center, spread, cx)) + .child(self.zone(Zone::Right, spread, cx)); + match scroll { + Some(width) => line.w(width).flex_none(), + None => line.w_full(), + } + .into_any_element() + } +} + +impl RenderOnce for Preview { + fn render(self, window: &mut Window, cx: &mut App) -> impl IntoElement { + let plan = self.fit.plan(&self.snapshot, &self.catalog); + let scroll = plan.scroll(); + let bar = Bar::new(&self, plan.density(), window, cx); + let (line, drops) = (bar.line(plan.spread(), scroll, cx), bar.drops.clone()); + let (fit, commands, keys) = ( + self.fit.clone(), + self.commands.clone(), + self.commands.clone(), + ); + let theme = cx.theme(); + let preview = div() + .id("layout-preview") + .test_support() + .track_focus(&self.focus) + .on_key_down(move |event, window, cx| { + if let Some(command) = keys::command(&event.keystroke) { + cx.stop_propagation(); + keys.run(window, cx, move |section, _, cx| section.key(command, cx)); + } + }) + .w_full() + .min_w_0() + .px_2() + .py_1() + .rounded(theme.radius_lg) + .bg(theme.muted) + .child( + div() + .id("layout-preview-scroll") + .test_support() + .w_full() + .min_w_0() + .when(scroll.is_some(), |frame| frame.overflow_x_scroll()) + .child(line), + ) + .on_prepaint(move |bounds, window, cx| { + if fit.observe(bounds.size.width) { + window.defer(cx, move |window, cx| { + commands.run(window, cx, |_, _, cx| cx.notify()); + }); + } + }); + match drops { + Some(drops) => drops::target(preview, drops, self.commands.clone()), + None => preview, + } + } +} diff --git a/crates/layout-ui/src/rename.rs b/crates/layout-ui/src/rename.rs new file mode 100644 index 00000000..b6f36906 --- /dev/null +++ b/crates/layout-ui/src/rename.rs @@ -0,0 +1,98 @@ +//! The name being typed for a layout or a group. + +use gpui_kit::{ + AppContext as _, Context, Entity, Focusable as _, SharedString, Subscription, Window, + component::input::{InputEvent, InputState}, +}; +use nocterm_plugin_model::GroupId; +use nocterm_window_layout::{EditOp, LayoutSettings, PresetId, validate_name}; + +use crate::section::LayoutEditorSection; + +/// What is being renamed. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) enum Renamed { + Layout(PresetId), + Group(GroupId), +} + +/// A single-line input holding the new name of a layout or a group, with the reason the last +/// name was refused. +pub(crate) struct RenameDraft { + renamed: Renamed, + input: Entity, + error: Option, + _subscription: Subscription, +} + +impl RenameDraft { + pub(crate) fn new( + renamed: Renamed, + name: String, + window: &mut Window, + cx: &mut Context, + ) -> Self { + let input = cx.new(|cx| InputState::new(window, cx).default_value(name)); + let subscription = + cx.subscribe_in( + &input, + window, + |section, _, event, window, cx| match event { + InputEvent::PressEnter { .. } => section.commit_rename(window, cx), + InputEvent::Change => section.clear_rename_error(cx), + _ => {} + }, + ); + window.focus(&input.read(cx).focus_handle(cx), cx); + Self { + renamed, + input, + error: None, + _subscription: subscription, + } + } + + pub(crate) fn renamed(&self) -> &Renamed { + &self.renamed + } + + pub(crate) fn input(&self) -> &Entity { + &self.input + } + + pub(crate) fn error(&self) -> Option<&SharedString> { + self.error.as_ref() + } + + pub(crate) fn refuse(&mut self, reason: impl Into) { + self.error = Some(reason.into()); + } + + pub(crate) fn clear_error(&mut self) { + self.error = None; + } + + /// The edit that gives the name typed, if it is accepted; otherwise the reason is kept + /// beside the input. A layout's name is checked against the others here, a group's by the + /// edit. + pub(crate) fn checked( + &mut self, + settings: &LayoutSettings, + cx: &gpui_kit::App, + ) -> Option { + let typed = self.input.read(cx).value().to_string(); + match &self.renamed { + Renamed::Layout(preset) => match validate_name(settings, Some(preset), &typed) { + Ok(name) => Some(EditOp::Rename { name }), + Err(reason) => { + self.refuse(reason.to_string()); + None + } + }, + Renamed::Group(group) => Some(EditOp::RenameGroup { + group: group.clone(), + name: typed, + }), + } + } +} diff --git a/crates/layout-ui/src/section.rs b/crates/layout-ui/src/section.rs new file mode 100644 index 00000000..2488b645 --- /dev/null +++ b/crates/layout-ui/src/section.rs @@ -0,0 +1,602 @@ +//! The editor entity: which layout and item are shown, the writes it sends and what they led to. + +use std::{rc::Rc, sync::Arc}; + +use gpui_kit::{ + App, Context, FocusHandle, Focusable, InteractiveElement as _, IntoElement, ParentElement as _, + Render, SharedString, Styled as _, Subscription, TestSupportExt as _, Window, + component::v_flex, +}; +use nocterm_plugin_enablement::PluginEnablement; +use nocterm_plugin_model::{ContributionKind, GroupId, PluginSet, Zone}; +use nocterm_store::SettingsHandle; +use nocterm_ui::{PluginSource, SettingsPage}; +use nocterm_window_layout::{ + EditError, EditOp, EditOutcome, LayoutSettings, PresetId, Subject, select, +}; +use nocterm_workspace::{LayoutEditError, edit_layouts}; + +use crate::{ + commands::Commands, + dnd::{Dragged, Landing}, + fit::Fit, + history::{History, Step}, + keys::KeyCommand, + presets::Presets, + preview::Preview, + rename::{RenameDraft, Renamed}, + selection::Selection, + snapshot::{Snapshot, Snapshots}, + tray::Tray, + user_facts::SharedFacts, +}; + +/// What the section is asking the user about, apart from the layout itself. +pub(crate) enum Mode { + Idle, + /// A new name is being typed for the layout or one of its groups. + Renaming(RenameDraft), + /// The layout shown is about to be deleted. + ConfirmingDelete, +} + +/// A line under the picker about what just happened. +pub(crate) struct Notice { + text: SharedString, + failed: bool, + /// Whether the line offers to undo the change. + undoable: bool, +} + +/// The "Status bar and panels" section of Appearance: the arrangement of the status bar and the +/// panel groups, edited in place by dragging the items of the bar and through their menus. +pub struct LayoutEditorSection { + settings: SettingsHandle, + /// Where the plugins come from, which changes while the page is open when the user installs + /// or removes a plugin. + source: Rc, + catalog: Arc, + legacy_swapped: bool, + selection: Selection, + history: History, + /// What the host knows of installed user plugins, when the application has any. + facts: Option, + /// Writes sent and not yet settled. + pending: usize, + mode: Mode, + notice: Option, + focus: FocusHandle, + /// The width the bar preview was drawn with, which decides how much of it is shown. + fit: Fit, + /// What is drawn of the layout shown, until the settings, the plugins or the choice change. + snapshots: Snapshots, + _subscriptions: Vec, + observed: (Option, PluginEnablement), +} + +impl LayoutEditorSection { + pub fn new( + settings: SettingsHandle, + source: Rc, + facts: Option, + legacy_swapped: bool, + cx: &mut Context, + ) -> Self { + settings.register::(cx); + settings.register::(cx); + let history = History::new(settings.setting::(cx).presets.clone()); + let weak = cx.weak_entity(); + let changed = settings.observe(cx, move |cx| { + let _ = weak.update(cx, |section, cx| section.stored_changed(cx)); + }); + let catalog = source.current(cx); + let weak = cx.weak_entity(); + let plugins = source.observe( + Box::new(move |cx| { + let _ = weak.update(cx, |section, cx| section.plugins_changed(cx)); + }), + cx, + ); + let observed = ( + settings.setting::(cx).active.clone(), + settings.setting::(cx).clone(), + ); + Self { + settings, + source, + catalog, + legacy_swapped, + selection: Selection::default(), + history, + facts, + pending: 0, + mode: Mode::Idle, + notice: None, + focus: cx.focus_handle(), + fit: Fit::default(), + snapshots: Snapshots::default(), + _subscriptions: vec![changed, plugins], + observed, + } + } + + /// What the editor shows of the layout shown, built once for each state of the settings, + /// the plugins and the layout picked. + pub(crate) fn snapshot(&mut self, cx: &App) -> Rc { + let (settings, catalog, shown) = (&self.settings, &self.catalog, self.selection.shown()); + self.snapshots + .get(settings.revision(cx), catalog, shown, || { + Snapshot::new( + settings.setting::(cx), + settings.setting::(cx), + self.legacy_swapped, + shown, + catalog, + ) + }) + } + + pub(crate) fn mode(&self) -> &Mode { + &self.mode + } + + pub(crate) fn notice(&self) -> Option<&Notice> { + self.notice.as_ref() + } + + /// Whether an undo is possible now. A step restores the whole list the history kept, so it + /// is not offered while a write is on its way: it would restore a list that the write is + /// about to replace. + pub(crate) fn can_undo(&self) -> bool { + self.pending == 0 && self.history.can_undo() + } + + /// Whether a redo is possible now, on the same terms as an undo. + pub(crate) fn can_redo(&self) -> bool { + self.pending == 0 && self.history.can_redo() + } + + fn in_use(&self, cx: &App) -> PresetId { + select( + self.settings.setting::(cx), + self.legacy_swapped, + ) + } + + /// Selects an item of the layout, or nothing, for the keys of the editor. + pub(crate) fn pick( + &mut self, + item: Option, + window: &mut Window, + cx: &mut Context, + ) { + self.selection.pick(item); + window.focus(&self.focus, cx); + cx.notify(); + } + + /// Shows another layout in the editor. It does not change the layout in use. + pub(crate) fn show(&mut self, id: PresetId, window: &mut Window, cx: &mut Context) { + let in_use = self.in_use(cx); + self.selection.show(id, &in_use); + self.mode = Mode::Idle; + self.notice = None; + window.focus(&self.focus, cx); + cx.notify(); + } + + /// Sends one operation on the layout shown. + pub(crate) fn edit(&mut self, op: EditOp, cx: &mut Context) { + self.send(op, Step::Edit, cx); + } + + /// Puts a dragged item where it was dropped, by the same rules as every other edit. + pub(crate) fn dropped(&mut self, dragged: &Dragged, landing: &Landing, cx: &mut Context) { + if !self.snapshot(cx).editable() { + return; + } + if let Some(op) = landing.op(dragged) { + self.edit(op, cx); + } + } + + pub(crate) fn undo(&mut self, cx: &mut Context) { + if !self.can_undo() { + return; + } + if let Some(presets) = self.history.previous().cloned() { + self.send(EditOp::RestorePresets(presets), Step::Undo, cx); + } + } + + pub(crate) fn redo(&mut self, cx: &mut Context) { + if !self.can_redo() { + return; + } + if let Some(presets) = self.history.next().cloned() { + self.send(EditOp::RestorePresets(presets), Step::Redo, cx); + } + } + + fn send(&mut self, op: EditOp, step: Step, cx: &mut Context) { + // The notice names the layout as it is before the write: a deleted one is not found + // after it. + let name = self.snapshot(cx).name().to_owned(); + let target = self.selection.target(&self.in_use(cx)); + let written = edit_layouts( + &self.settings, + self.catalog.clone(), + self.legacy_swapped, + target, + op.clone(), + cx, + ); + self.pending += 1; + cx.spawn(async move |section, cx| { + let result = written.await; + let _ = section.update(cx, |section, cx| { + section.settled(op, step, &name, result, cx) + }); + }) + .detach(); + } + + fn settled( + &mut self, + op: EditOp, + step: Step, + name: &str, + result: Result, LayoutEditError>, + cx: &mut Context, + ) { + self.pending = self.pending.saturating_sub(1); + match result { + Ok(Some(outcome)) => self.stored(&op, step, name, &outcome, cx), + Ok(None) => {} + Err(error) => self.refused(&op, &error), + } + self.stored_changed(cx); + cx.notify(); + } + + fn stored( + &mut self, + op: &EditOp, + step: Step, + name: &str, + outcome: &EditOutcome, + cx: &mut Context, + ) { + self.history.record(step, outcome); + let in_use = self.in_use(cx); + let settings = self.settings.setting::(cx).clone(); + self.selection.followed(op, outcome, &in_use, |id| { + nocterm_window_layout::BuiltIn::of(id).is_some() || settings.exists(id) + }); + self.notice = match op { + EditOp::Delete => Some(Notice::undoable(format!("Deleted \u{201c}{name}\u{201d}."))), + EditOp::Reset => Some(Notice::undoable(format!("Reset \u{201c}{name}\u{201d}."))), + _ => None, + }; + if matches!( + op, + EditOp::Rename { .. } | EditOp::RenameGroup { .. } | EditOp::Delete + ) { + self.mode = Mode::Idle; + } + } + + fn refused(&mut self, op: &EditOp, error: &LayoutEditError) { + let text: SharedString = match error { + LayoutEditError::Refused(EditError::Pinned(id)) => { + let title = self + .catalog + .contribution(id) + .map_or(id.as_str(), |it| it.title()); + format!("\u{201c}{title}\u{201d} is always shown and cannot be hidden.").into() + } + LayoutEditError::Store(other) => format!("Could not save the layout: {other}").into(), + refusal => refusal.to_string().into(), + }; + match (op, &mut self.mode) { + (EditOp::Rename { .. } | EditOp::RenameGroup { .. }, Mode::Renaming(draft)) => { + draft.refuse(text) + } + _ => self.notice = Some(Notice::failed(text)), + } + } + + /// Reads the plugins again after one was installed, replaced or removed. An item that is no + /// longer in the set leaves the selection. + fn plugins_changed(&mut self, cx: &mut Context) { + self.catalog = self.source.current(cx); + if let Some(Subject::Contribution(id)) = self.selection.item() + && self.catalog.contribution(id).is_none() + { + self.selection.clear(); + } + cx.notify(); + } + + /// Forgets the history when the layouts changed by someone else, once no write of ours is + /// on its way. + fn stored_changed(&mut self, cx: &mut Context) { + let active = self.settings.setting::(cx).active.clone(); + let enablement = self.settings.setting::(cx); + let mut changed = self.observed.0 != active || self.observed.1 != *enablement; + if changed { + self.observed = (active, enablement.clone()); + } + if self.pending == 0 { + let presets = &self.settings.setting::(cx).presets; + if self.history.observe(presets) { + self.notice = None; + changed = true; + } + } + if changed { + cx.notify(); + } + } + + pub(crate) fn dismiss_notice(&mut self, cx: &mut Context) { + self.notice = None; + cx.notify(); + } + + pub(crate) fn begin_rename(&mut self, window: &mut Window, cx: &mut Context) { + let snapshot = self.snapshot(cx); + if snapshot.built_in() { + return; + } + self.notice = None; + self.mode = Mode::Renaming(RenameDraft::new( + Renamed::Layout(snapshot.shown().clone()), + snapshot.name().to_owned(), + window, + cx, + )); + cx.notify(); + } + + /// Asks for a new name of the group `id` of the layout shown. + pub(crate) fn begin_group_rename( + &mut self, + id: GroupId, + window: &mut Window, + cx: &mut Context, + ) { + let snapshot = self.snapshot(cx); + let Some(group) = snapshot.group(&id).filter(|_| snapshot.editable()) else { + return; + }; + let name = snapshot.group_title(group); + self.notice = None; + self.mode = Mode::Renaming(RenameDraft::new(Renamed::Group(id), name, window, cx)); + cx.notify(); + } + + pub(crate) fn commit_rename(&mut self, _: &mut Window, cx: &mut Context) { + let settings = self.settings.setting::(cx).clone(); + let Mode::Renaming(draft) = &mut self.mode else { + return; + }; + if let Some(op) = draft.checked(&settings, cx) { + self.edit(op, cx); + } + cx.notify(); + } + + pub(crate) fn clear_rename_error(&mut self, cx: &mut Context) { + if let Mode::Renaming(draft) = &mut self.mode { + draft.clear_error(); + cx.notify(); + } + } + + pub(crate) fn cancel_rename(&mut self, window: &mut Window, cx: &mut Context) { + if matches!(self.mode, Mode::Renaming(_)) { + self.mode = Mode::Idle; + window.focus(&self.focus, cx); + cx.notify(); + } + } + + pub(crate) fn ask_delete(&mut self, cx: &mut Context) { + let snapshot = self.snapshot(cx); + if !snapshot.built_in() { + self.notice = None; + self.mode = Mode::ConfirmingDelete; + cx.notify(); + } + } + + pub(crate) fn cancel_delete(&mut self, cx: &mut Context) { + self.mode = Mode::Idle; + cx.notify(); + } + + pub(crate) fn confirm_delete(&mut self, cx: &mut Context) { + if matches!(self.mode, Mode::ConfirmingDelete) { + self.edit(EditOp::Delete, cx); + } + } + + /// Runs a key of the editor on the selected item. + pub(crate) fn key(&mut self, command: KeyCommand, cx: &mut Context) { + match command { + KeyCommand::Undo => return self.undo(cx), + KeyCommand::Redo => return self.redo(cx), + _ => {} + } + let snapshot = self.snapshot(cx); + let Some(subject) = self + .selection + .item() + .cloned() + .filter(|_| snapshot.editable()) + else { + return; + }; + if let Some(op) = self.op_for(command, &subject, &snapshot) { + self.edit(op, cx); + } + } + + fn op_for( + &self, + command: KeyCommand, + subject: &Subject, + snapshot: &Snapshot, + ) -> Option { + match command { + KeyCommand::Move(by) => Some(EditOp::Move { + subject: subject.clone(), + by, + }), + KeyCommand::Zone(step) => { + let moved = self.movable(subject, snapshot)?; + let zones = [Zone::Left, Zone::Center, Zone::Right]; + let at = zones + .iter() + .position(|zone| Some(*zone) == snapshot.zone_of(&moved))?; + let to = at.checked_add_signed(step as isize)?; + Some(EditOp::ToZone { + subject: moved, + zone: *zones.get(to)?, + }) + } + KeyCommand::NextGroup => { + let Subject::Contribution(panel) = subject else { + return None; + }; + if self.catalog.contribution(panel)?.kind() != ContributionKind::Panel { + return None; + } + let groups = snapshot.group_choices(); + let current = snapshot.group_of(panel).map(|group| group.id().clone()); + let at = groups + .iter() + .position(|(group, _)| Some(group) == current.as_ref()); + let (next, _) = groups.get(at.map_or(0, |at| (at + 1) % groups.len()))?; + Some(EditOp::ToGroup { + panel: panel.clone(), + group: next.clone(), + }) + } + KeyCommand::Separator => { + let (place, index) = snapshot.after(subject)?; + Some(EditOp::InsertSeparator { place, index }) + } + KeyCommand::Remove => match subject { + Subject::Contribution(id) => Some(EditOp::RemoveToTray { id: id.clone() }), + Subject::Group(_) => None, + }, + KeyCommand::Undo | KeyCommand::Redo => None, + } + } + + /// What moves between zones when `subject` is asked to: a widget, a panel on its own, or the + /// group of a panel. + fn movable(&self, subject: &Subject, snapshot: &Snapshot) -> Option { + match subject { + Subject::Group(_) => Some(subject.clone()), + Subject::Contribution(id) => match self.catalog.contribution(id)?.kind() { + ContributionKind::Widget => Some(subject.clone()), + ContributionKind::Toolbar => None, + ContributionKind::Panel => snapshot.group_of(id).map(|group| { + if group.is_solo() { + subject.clone() + } else { + Subject::Group(group.id().clone()) + } + }), + }, + } + } +} + +impl Notice { + pub(crate) fn text(&self) -> &SharedString { + &self.text + } + + pub(crate) fn is_failure(&self) -> bool { + self.failed + } + + pub(crate) fn offers_undo(&self) -> bool { + self.undoable + } + + fn undoable(text: String) -> Self { + Self { + text: text.into(), + failed: false, + undoable: true, + } + } + + fn failed(text: SharedString) -> Self { + Self { + text, + failed: true, + undoable: false, + } + } +} + +impl SettingsPage for LayoutEditorSection { + fn on_deactivate(&mut self, _: &mut Window, _: &mut Context) { + self.mode = Mode::Idle; + } + + /// Selects the contribution or panel group whose id is `anchor`. + fn reveal(&mut self, anchor: &str, window: &mut Window, cx: &mut Context) -> bool { + let subject = self + .catalog + .entries() + .find(|entry| entry.id().as_str() == anchor) + .map(|entry| Subject::Contribution(entry.id().clone())) + .or_else(|| { + self.catalog + .group(anchor) + .map(|group| Subject::Group(group.id.clone())) + }); + let known = subject.is_some(); + if known { + self.pick(subject, window, cx); + } + known + } +} + +impl Focusable for LayoutEditorSection { + fn focus_handle(&self, _: &App) -> FocusHandle { + self.focus.clone() + } +} + +impl Render for LayoutEditorSection { + fn render(&mut self, _: &mut Window, cx: &mut Context) -> impl IntoElement { + let snapshot = self.snapshot(cx); + let commands = Commands::new(cx.weak_entity()); + v_flex() + .id("layout-editor") + .test_support() + .w_full() + .min_w_0() + .gap_3() + .child(Presets::new(self, snapshot.clone(), commands.clone())) + .child(Preview::new( + snapshot.clone(), + self.catalog.clone(), + self.selection.item().cloned(), + self.facts.clone(), + self.fit.clone(), + self.focus.clone(), + commands.clone(), + )) + .child(Tray::new(snapshot, self.catalog.clone(), commands)) + } +} diff --git a/crates/layout-ui/src/selection.rs b/crates/layout-ui/src/selection.rs new file mode 100644 index 00000000..4d23f3de --- /dev/null +++ b/crates/layout-ui/src/selection.rs @@ -0,0 +1,69 @@ +//! Which layout the editor shows and which item of it is selected. + +use nocterm_window_layout::{EditOp, EditOutcome, EditTarget, PresetId, Subject}; + +/// The layout and the item the editor shows. +#[derive(Debug, Default)] +pub(crate) struct Selection { + /// The layout picked in the picker; `None` follows the layout in use. + shown: Option, + item: Option, +} + +impl Selection { + pub(crate) fn shown(&self) -> Option<&PresetId> { + self.shown.as_ref() + } + + pub(crate) fn item(&self) -> Option<&Subject> { + self.item.as_ref() + } + + /// Shows `id`, which follows the layout in use when it is that one. + pub(crate) fn show(&mut self, id: PresetId, in_use: &PresetId) { + self.shown = (id != *in_use).then_some(id); + self.item = None; + } + + pub(crate) fn pick(&mut self, item: Option) { + self.item = item; + } + + /// Selects nothing. + pub(crate) fn clear(&mut self) { + self.item = None; + } + + /// The target of an operation on the layout shown: the layout in use is named by its role, + /// so that an operation queued behind the first edit of a built-in lands on the copy. + pub(crate) fn target(&self, in_use: &PresetId) -> EditTarget { + match &self.shown { + Some(id) if id != in_use => EditTarget::Preset(id.clone()), + _ => EditTarget::Selected, + } + } + + /// Follows a stored edit: the copy a duplicate made is shown, a deleted layout gives way + /// to the one the outcome names, and undo keeps the layout shown while it exists. + pub(crate) fn followed( + &mut self, + op: &EditOp, + outcome: &EditOutcome, + in_use: &PresetId, + exists: impl Fn(&PresetId) -> bool, + ) { + match op { + EditOp::Duplicate | EditOp::Delete => self.show(outcome.selected.clone(), in_use), + EditOp::RestorePresets(_) => { + if self.shown.as_ref().is_some_and(|id| !exists(id)) { + self.shown = None; + } + } + _ => { + if self.shown.is_some() { + self.shown = (outcome.selected != *in_use).then(|| outcome.selected.clone()); + } + } + } + } +} diff --git a/crates/layout-ui/src/snapshot.rs b/crates/layout-ui/src/snapshot.rs new file mode 100644 index 00000000..b78f1287 --- /dev/null +++ b/crates/layout-ui/src/snapshot.rs @@ -0,0 +1,371 @@ +//! What the editor shows of one layout, derived from the settings, and kept until they change. + +use std::{collections::BTreeSet, rc::Rc, sync::Arc}; + +use nocterm_plugin_enablement::PluginEnablement; +use nocterm_plugin_model::{ + ContributionId, ContributionKind, GroupId, GroupSpec, PluginId, PluginSet, Zone, +}; +use nocterm_window_layout::{ + BuiltIn, LayoutSettings, PresetId, ResolvedGroup, ResolvedItem, ResolvedLayout, ResolvedMember, + Slot, Subject, select, +}; + +/// A layout the picker offers. +#[derive(Clone, Debug, PartialEq, Eq)] +pub(crate) struct Choice { + id: PresetId, + name: String, + built_in: bool, + /// The layout the windows show. + in_use: bool, +} + +impl Choice { + pub(crate) fn id(&self) -> &PresetId { + &self.id + } + + pub(crate) fn name(&self) -> &str { + &self.name + } + + pub(crate) fn in_use(&self) -> bool { + self.in_use + } +} + +/// The layout the editor shows, resolved with every plugin enabled so that nothing the user +/// can edit is hidden, and the plugins that are in fact disabled. +#[derive(Clone, Debug)] +pub(crate) struct Snapshot { + shown: PresetId, + name: String, + built_in: bool, + in_use: bool, + choices: Vec, + layout: ResolvedLayout, + /// Contributions the layout leaves out of the bar, in catalog order. + tray: Vec, + /// Ids in the tray that this application does not know. + unknown: Vec, + disabled: BTreeSet, + groups: Vec, +} + +impl Snapshot { + /// `shown` is the layout to show; `None` follows the one in use. + pub(crate) fn new( + settings: &LayoutSettings, + enablement: &PluginEnablement, + legacy_swapped: bool, + shown: Option<&PresetId>, + catalog: &PluginSet, + ) -> Self { + let in_use_id = select(settings, legacy_swapped); + let shown = shown + .filter(|id| BuiltIn::of(id).is_some() || settings.exists(id)) + .cloned() + .unwrap_or_else(|| in_use_id.clone()); + let built_in = BuiltIn::of(&shown); + let name = match built_in { + Some(built_in) => built_in.name().to_owned(), + None => settings + .preset(&shown) + .map(|preset| preset.name.clone()) + .unwrap_or_default(), + }; + let layout = settings.resolve(&shown, catalog, &BTreeSet::new()); + // Toolbars are shown and hidden from the status bar's menu, not arranged here. + let tray = catalog + .entries() + .filter(|contribution| contribution.kind() != ContributionKind::Toolbar) + .filter(|contribution| !layout.is_placed(contribution.id())) + .map(|contribution| contribution.id().clone()) + .collect(); + let unknown = settings + .preset(&shown) + .map(|preset| { + preset + .tray + .iter() + .filter(|id| catalog.contribution(id).is_none()) + .cloned() + .collect() + }) + .unwrap_or_default(); + let choices = BuiltIn::ALL + .into_iter() + .map(|built_in| (built_in.id(), built_in.name().to_owned(), true)) + .chain( + settings + .presets + .iter() + .map(|preset| (preset.id.clone(), preset.name.clone(), false)), + ) + .map(|(id, name, built_in)| Choice { + in_use: id == in_use_id, + id, + name, + built_in, + }) + .collect(); + Self { + in_use: shown == in_use_id, + shown, + name, + built_in: built_in.is_some(), + choices, + layout, + tray, + unknown, + disabled: enablement.effective_disabled(catalog.catalog()), + groups: catalog.groups().to_vec(), + } + } + + pub(crate) fn shown(&self) -> &PresetId { + &self.shown + } + + pub(crate) fn name(&self) -> &str { + &self.name + } + + pub(crate) fn built_in(&self) -> bool { + self.built_in + } + + pub(crate) fn choices(&self) -> &[Choice] { + &self.choices + } + + pub(crate) fn layout(&self) -> &ResolvedLayout { + &self.layout + } + + pub(crate) fn tray(&self) -> &[ContributionId] { + &self.tray + } + + pub(crate) fn unknown(&self) -> &[ContributionId] { + &self.unknown + } + + /// The groups a panel can be put in: the groups of the bar in bar order, then the + /// catalog's groups the bar leaves out. A panel on its own is not a group to join. + pub(crate) fn group_choices(&self) -> Vec<(GroupId, String)> { + let placed = self + .layout + .groups() + .filter(|group| !group.is_solo()) + .map(|group| (group.id().clone(), self.group_title(group))); + let unplaced = self + .groups + .iter() + .filter(|spec| self.zone_of_group(&spec.id).is_none()) + .map(|spec| (spec.id.clone(), spec.title.to_owned())); + placed.chain(unplaced).collect() + } + + /// The name the user sees for a group: the one they gave it, else the catalog's title. + pub(crate) fn group_title(&self, group: &ResolvedGroup) -> String { + group + .name() + .map(str::to_owned) + .or_else(|| { + self.groups + .iter() + .find(|spec| spec.id == *group.id()) + .map(|spec| spec.title.to_owned()) + }) + .unwrap_or_else(|| "Group".to_owned()) + } + + /// Whether the controls act on this layout. A built-in can be edited only while it is the + /// layout in use, because the first edit copies it and the copy takes its place. + pub(crate) fn editable(&self) -> bool { + !self.built_in || self.in_use + } + + pub(crate) fn is_disabled(&self, id: &ContributionId, catalog: &PluginSet) -> bool { + catalog + .plugin_of(id) + .is_some_and(|plugin| self.disabled.contains(plugin.id())) + } + + /// The zone a placed widget, group or panel is in. + pub(crate) fn zone_of(&self, subject: &Subject) -> Option { + self.find(subject).map(|found| found.zone) + } + + /// Where a separator goes to follow `subject`: the zone for a widget or a group, the group + /// for a panel. + pub(crate) fn after(&self, subject: &Subject) -> Option<(Slot, usize)> { + let found = self.find(subject)?; + Some(match found.member { + Some((group, member)) => (Slot::Group(group), member + 1), + None => (Slot::Zone(found.zone), found.index + 1), + }) + } + + /// Where a separator goes to precede `subject`: its own place in the list. + pub(crate) fn before(&self, subject: &Subject) -> Option<(Slot, usize)> { + let found = self.find(subject)?; + Some(match found.member { + Some((group, member)) => (Slot::Group(group), member), + None => (Slot::Zone(found.zone), found.index), + }) + } + + /// Whether `subject` has an entry before it (`by` is -1) or after it (+1) in its list, so + /// that moving it by one changes something. + pub(crate) fn can_shift(&self, subject: &Subject, by: i32) -> bool { + let Some(found) = self.find(subject) else { + return false; + }; + let (index, len) = match &found.member { + Some((_, member)) => (*member, found.members), + None => (found.index, found.len), + }; + if by < 0 { index > 0 } else { index + 1 < len } + } + + fn find(&self, subject: &Subject) -> Option { + for zone in [Zone::Left, Zone::Center, Zone::Right] { + let len = self.layout.zone(zone).len(); + for (index, item) in self.layout.zone(zone).iter().enumerate() { + match (item, subject) { + (ResolvedItem::Widget(id), Subject::Contribution(wanted)) if id == wanted => { + return Some(Found { + zone, + index, + len, + members: 0, + member: None, + }); + } + (ResolvedItem::Group(group), Subject::Group(wanted)) + if group.id() == wanted => + { + return Some(Found { + zone, + index, + len, + members: 0, + member: None, + }); + } + // A panel on its own is an entry of its zone. + (ResolvedItem::Group(group), Subject::Contribution(wanted)) + if group.is_solo() && group.holds(wanted) => + { + return Some(Found { + zone, + index, + len, + members: 0, + member: None, + }); + } + (ResolvedItem::Group(group), Subject::Contribution(wanted)) => { + let member = group.members().iter().position( + |member| matches!(member, ResolvedMember::Panel(id) if id == wanted), + ); + if let Some(member) = member { + return Some(Found { + zone, + index, + len, + members: group.members().len(), + member: Some((group.id().clone(), member)), + }); + } + } + _ => {} + } + } + } + None + } + + pub(crate) fn group_of(&self, panel: &ContributionId) -> Option<&ResolvedGroup> { + self.layout.group_of(panel) + } + + /// The group `id` of the bar. + pub(crate) fn group(&self, id: &GroupId) -> Option<&ResolvedGroup> { + self.layout.groups().find(|group| group.id() == id) + } + + /// Whether the panel opens when the layout is chosen. + pub(crate) fn opens_with_layout(&self, panel: &ContributionId) -> bool { + self.layout.open().contains(panel) + } + + pub(crate) fn zone_of_group(&self, group: &GroupId) -> Option { + [Zone::Left, Zone::Center, Zone::Right] + .into_iter() + .find(|zone| { + self.layout + .zone(*zone) + .iter() + .any(|item| matches!(item, ResolvedItem::Group(placed) if placed.id() == group)) + }) + } +} + +/// The snapshot of the layout shown, built again only when what it is drawn from changed: the +/// settings, by their revision, the set of plugins, or the layout picked. An editor never +/// changes its `legacy_swapped`. It holds the one snapshot drawn last. +#[derive(Default)] +pub(crate) struct Snapshots { + last: Option<(Drawn, Rc)>, +} + +/// What a snapshot was built from. +struct Drawn { + revision: u64, + catalog: Arc, + shown: Option, +} + +impl Snapshots { + /// The snapshot for these inputs: the last one when they are the same, else what `build` + /// makes of them. + pub(crate) fn get( + &mut self, + revision: u64, + catalog: &Arc, + shown: Option<&PresetId>, + build: impl FnOnce() -> Snapshot, + ) -> Rc { + if let Some((drawn, snapshot)) = &self.last + && drawn.revision == revision + && Arc::ptr_eq(&drawn.catalog, catalog) + && drawn.shown.as_ref() == shown + { + return snapshot.clone(); + } + let snapshot = Rc::new(build()); + let drawn = Drawn { + revision, + catalog: catalog.clone(), + shown: shown.cloned(), + }; + self.last = Some((drawn, snapshot.clone())); + snapshot + } +} + +/// Where `find` met a subject. +struct Found { + zone: Zone, + index: usize, + /// How many entries the zone holds. + len: usize, + /// For a panel, how many entries its group holds. + members: usize, + /// For a panel, its group and its place among the members. + member: Option<(GroupId, usize)>, +} diff --git a/crates/layout-ui/src/tests.rs b/crates/layout-ui/src/tests.rs new file mode 100644 index 00000000..99759c3a --- /dev/null +++ b/crates/layout-ui/src/tests.rs @@ -0,0 +1,491 @@ +//! The editor edits the layouts through the store: drags, menus and keys in the bar preview, +//! presets and history. + +use std::{borrow::Cow, rc::Rc, sync::Arc}; + +use gpui_kit::{ + AnyWindowHandle, AppContext as _, Bounds, InputEvent as _, Modifiers, MouseButton, + MouseDownEvent, MouseMoveEvent, MouseUpEvent, Pixels, PlatformInput, Point, TestAppContext, + WindowOptions, test::TestWindowExt as _, +}; +use nocterm_plugin_enablement::PluginEnablement; +use nocterm_plugin_model::{ + AutoPlace, ContributionId, ContributionKind, FieldGroup, FieldKind, FieldSpec, GroupId, + GroupSpec, Icon, PanelChrome, PanelTraits, Placement, PluginCatalog, PluginId, PluginSet, + SettingsSchema, SystemContribution, SystemPlugin, WidthClass, Zone, +}; +use nocterm_settings::SettingsDocument; +use nocterm_store::SettingsHandle; +use nocterm_ui::StaticPluginSource; +use nocterm_window_layout::LayoutSettings; + +use crate::section::LayoutEditorSection; + +mod aim; +mod dnd; +mod editing; +mod geometry; +mod grouping; +mod history; +mod menu; +mod presets; + +const fn widget( + id: &'static str, + title: &'static str, + zone: Zone, + order: u16, +) -> SystemContribution { + SystemContribution { + id: ContributionId::new(id), + kind: ContributionKind::Widget, + title, + icon: Icon::Clock, + placement: Placement { + zone, + order, + group: None, + auto: AutoPlace::Show, + }, + panel: None, + preview: None, + edge: None, + pinned: false, + } +} + +const fn panel( + id: &'static str, + title: &'static str, + group: &'static str, + order: u16, +) -> SystemContribution { + let mut panel = widget(id, title, Zone::Left, order); + panel.kind = ContributionKind::Panel; + panel.placement.group = Some(group); + panel.panel = Some(PanelTraits { + width: WidthClass::Navigation, + chrome: PanelChrome::Titled, + }); + panel +} + +const fn pinned(mut widget: SystemContribution) -> SystemContribution { + widget.pinned = true; + widget +} + +const CORE: &[SystemContribution] = &[ + panel("shell.servers", "Servers", "navigation", 10), + panel("shell.files", "Files", "navigation", 20), + panel("shell.agent", "Agent", "assistant", 40), + pinned(widget("shell.notices", "Notices", Zone::Right, 10)), + widget("shell.transfers", "Transfers", Zone::Right, 20), + widget("shell.terminal", "Terminal", Zone::Right, 30), +]; + +const CLOCK_GROUP: FieldGroup = FieldGroup { + title: Cow::Borrowed("Clock"), + contribution: Some(ContributionId::new("tools.clock")), + fields: Cow::Borrowed(&[FieldSpec { + renamed_from: None, + key: Cow::Borrowed("seconds"), + label: Cow::Borrowed("Show seconds"), + help: None, + kind: FieldKind::Toggle { default: false }, + }]), +}; + +pub(super) static TOOLS: SystemPlugin = SystemPlugin { + id: PluginId::new("tools"), + name: "Tools", + description: "Small helpers", + required: false, + contributions: &[ + widget("tools.clock", "Clock", Zone::Right, 25), + widget("tools.cpu", "Processor", Zone::Right, 26), + ], + settings_section: Some("tools"), + settings: SettingsSchema { + version: 1, + groups: Cow::Borrowed(&[CLOCK_GROUP]), + }, + settings_page: Some("tools"), +}; + +static PLAIN: SystemPlugin = SystemPlugin { + id: PluginId::new("plain"), + name: "Plain", + description: "Nothing to set", + required: false, + contributions: &[widget("plain.note", "Note", Zone::Right, 40)], + settings_section: None, + settings: SettingsSchema::NONE, + settings_page: None, +}; + +static OFF: SystemPlugin = SystemPlugin { + id: PluginId::new("off"), + name: "Switched off", + description: "Turned off by the user", + required: false, + contributions: &[widget("off.item", "Dormant", Zone::Right, 50)], + settings_section: None, + settings: SettingsSchema::NONE, + settings_page: None, +}; + +fn plugin_catalog() -> PluginCatalog { + let groups = [ + GroupSpec { + id: GroupId::new("navigation"), + title: "Sidebar", + primary: true, + }, + GroupSpec { + id: GroupId::new("assistant"), + title: "Assistant", + primary: false, + }, + ]; + PluginCatalog::new(&groups, &[CORE], &[&TOOLS, &PLAIN, &OFF]).unwrap() +} + +fn catalog() -> Arc { + Arc::new(PluginSet::from(plugin_catalog())) +} + +pub(super) struct Fixture { + pub(super) window: AnyWindowHandle, + pub(super) settings: SettingsHandle, +} + +pub(super) fn open(cx: &mut TestAppContext) -> Fixture { + open_with(LayoutSettings::default(), cx) +} + +/// An editor over the stored layouts `stored`, with the plugin `off` disabled. +pub(super) fn open_with(stored: LayoutSettings, cx: &mut TestAppContext) -> Fixture { + let mut document = SettingsDocument::default(); + document.register::(); + document.set(stored); + open_over(document, cx) +} + +/// An editor over the settings `document`, with the plugin `off` disabled. +pub(super) fn open_over(document: SettingsDocument, cx: &mut TestAppContext) -> Fixture { + open_config( + nocterm_store::SettingsConfig::in_memory(prepared(document)), + cx, + ) +} + +/// An editor over the default layouts kept in a settings file in `directory`, which is written +/// and waited for like the application's own. +pub(super) fn open_on_disk(directory: &std::path::Path, cx: &mut TestAppContext) -> Fixture { + let file = nocterm_settings::SettingsFile::new(directory.join("settings.toml")); + let document = prepared(SettingsDocument::default()); + open_config(nocterm_store::SettingsConfig::new(document, file), cx) +} + +/// The `document` with the sections the editor reads and the plugin `off` disabled. +fn prepared(mut document: SettingsDocument) -> SettingsDocument { + document.register::(); + document.update::(|enablement| { + enablement.set_enabled(&plugin_catalog(), &PluginId::new("off"), false); + }); + document +} + +fn open_config(config: nocterm_store::SettingsConfig, cx: &mut TestAppContext) -> Fixture { + let (window, settings) = cx.update(|cx| { + gpui_kit::init(cx); + let settings = config.install(cx); + nocterm_ui::init(nocterm_ui::DesignTokens::builtin(), settings.clone(), cx); + settings.register::(cx); + settings.register::(cx); + let (window, _) = gpui_kit::open_window(WindowOptions::default(), cx, |_, cx| { + cx.new(|cx| { + LayoutEditorSection::new( + settings.clone(), + Rc::new(StaticPluginSource::new(catalog())), + None, + false, + cx, + ) + }) + }) + .unwrap(); + (window, settings) + }); + cx.update_window(window, |_, window, _| window.activate_window()) + .unwrap(); + cx.simulate_window_resize( + window, + gpui_kit::size(gpui_kit::px(1200.), gpui_kit::px(2400.)), + ); + cx.run_until_parked(); + Fixture { window, settings } +} + +impl Fixture { + /// Draws a frame and lets `check` look at the window. + pub(super) fn frame( + &self, + cx: &mut TestAppContext, + check: impl FnOnce(&mut gpui_kit::Window, &mut gpui_kit::App), + ) { + // The first frame records the width of the bar preview, which the second one is drawn for. + cx.update_window(self.window, |_, window, cx| window.render_frame(cx)) + .unwrap(); + cx.run_until_parked(); + cx.update_window(self.window, |_, window, cx| { + window.render_frame(cx); + check(window, cx); + }) + .unwrap(); + } + + /// Drags the element `from` onto the element `to` and lets the writes it caused settle. + pub(super) fn drag(&self, from: &str, to: &str, cx: &mut TestAppContext) { + let (from, to) = (from.to_owned(), to.to_owned()); + self.frame(cx, |window, cx| window.drag_to(from, to, cx)); + cx.run_until_parked(); + } + + /// Drags the element `from` to the point `across` of the way over the element `to`, from + /// its left edge, and lets the writes it caused settle. + pub(super) fn drag_across(&self, from: &str, to: &str, across: f32, cx: &mut TestAppContext) { + let target = self.bounds(to, cx); + let at = gpui_kit::point( + target.left() + target.size.width * across, + target.center().y, + ); + let from = from.to_owned(); + self.frame(cx, |window, cx| { + let start = window.find(from).bounds().center(); + window.drag(start, at, cx); + }); + cx.run_until_parked(); + } + + /// Presses the pointer on the middle of the element `from` and moves it, held, to `to`: a + /// drag in progress, drawn after every move as a real one is. + pub(super) fn hold(&self, from: &str, to: Point, cx: &mut TestAppContext) { + let from = from.to_owned(); + self.frame(cx, |window, cx| { + let start = window.find(from).bounds().center(); + pointer(window, moved(start, None), cx); + pointer(window, pressed(start), cx); + for step in 1..=8 { + let at = start + (to - start) * (step as f32 / 8.); + pointer(window, moved(at, Some(MouseButton::Left)), cx); + } + }); + } + + /// Releases the held pointer at `at` and draws nothing itself: the frame that follows is + /// the one the release asked for, if any. + pub(super) fn release(&self, at: Point, cx: &mut TestAppContext) { + cx.update_window(self.window, |_, window, cx| { + window.dispatch_event(released(at), cx); + }) + .unwrap(); + cx.run_until_parked(); + } + + /// Where the last frame drawn shows the element `id`, without drawing another one. + pub(super) fn shown(&self, id: &str, cx: &mut TestAppContext) -> Option> { + cx.update_window(self.window, |_, window, _| { + window + .try_find(id.to_owned()) + .map(|element| element.bounds()) + }) + .unwrap() + } + + /// The bounds of the element `id` in the frame drawn now. + pub(super) fn bounds(&self, id: &str, cx: &mut TestAppContext) -> Bounds { + let mut found = None; + self.frame(cx, |window, _| { + found = window + .try_find(id.to_owned()) + .map(|element| element.bounds()); + }); + found.unwrap_or_else(|| panic!("{id} is not in the frame")) + } + + /// Clicks the element `id` in the frame drawn now and lets the writes it caused settle. + pub(super) fn click(&self, id: &str, cx: &mut TestAppContext) { + let id = id.to_owned(); + self.frame(cx, |window, cx| window.click(id, cx)); + cx.run_until_parked(); + } + + pub(super) fn stored(&self, cx: &mut TestAppContext) -> LayoutSettings { + cx.update(|cx| self.settings.setting::(cx).clone()) + } + + /// Runs the executors until the stored layouts satisfy `done`, for a store whose writes + /// take real time. Panics when they never do. + pub(super) fn wait_for( + &self, + cx: &mut TestAppContext, + done: impl Fn(&LayoutSettings) -> bool, + ) -> LayoutSettings { + cx.executor().allow_parking(); + for _ in 0..400 { + cx.run_until_parked(); + let stored = self.stored(cx); + if done(&stored) { + return stored; + } + std::thread::sleep(std::time::Duration::from_millis(5)); + } + panic!("the layouts never reached the state waited for"); + } + + /// The accessible label of the element `id` in the frame drawn now. + pub(super) fn label(&self, id: &str, cx: &mut TestAppContext) -> Option { + let mut found = None; + self.frame(cx, |window, _| { + found = window + .try_find(id.to_owned()) + .and_then(|element| element.label().map(str::to_owned)); + }); + found + } + + /// Resizes the window to `width` pixels, as a Settings page of that width would be. + pub(super) fn resize(&self, width: f32, cx: &mut TestAppContext) { + cx.simulate_window_resize( + self.window, + gpui_kit::size(gpui_kit::px(width), gpui_kit::px(2400.)), + ); + cx.run_until_parked(); + } + + pub(super) fn keys(&self, keys: &str, cx: &mut TestAppContext) { + cx.simulate_keystrokes(self.window, keys); + cx.run_until_parked(); + } + + /// Whether an element with `id` is in the frame drawn now. + pub(super) fn has(&self, id: &str, cx: &mut TestAppContext) -> bool { + let mut found = false; + self.frame(cx, |window, _| { + found = window.try_find(id.to_owned()).is_some() + }); + found + } + + /// The items among `ids` that the bar preview shows, from left to right. An id `group:g` + /// names the group `g`. + pub(super) fn left_to_right(&self, ids: &[&str], cx: &mut TestAppContext) -> Vec { + let mut found = Vec::new(); + self.frame(cx, |window, _| { + for id in ids { + if let Some(element) = window.try_find(preview_id(id)) { + found.push((f32::from(element.bounds().origin.x), (*id).to_owned())); + } + } + }); + found.sort_by(|a, b| a.0.total_cmp(&b.0)); + found.into_iter().map(|(_, id)| id).collect() + } + + /// Whether the preview shows the item `id` (or `group:g`) selected. + pub(super) fn is_selected(&self, id: &str, cx: &mut TestAppContext) -> bool { + let mut selected = false; + self.frame(cx, |window, _| { + selected = window + .try_find(preview_id(id)) + .and_then(|element| element.selected()) + .unwrap_or(false); + }); + selected + } + + /// Whether the preview marks the panel `id` as opening with the layout; `None` for a chip + /// that is no panel. + pub(super) fn opens(&self, id: &str, cx: &mut TestAppContext) -> Option { + let mut opens = None; + self.frame(cx, |window, _| { + opens = window + .try_find(preview_id(id)) + .and_then(|element| element.checked()); + }); + opens + } + + /// Opens the menu of the element `id` and chooses the entry `label` on its first level, + /// then lets the writes it caused settle. + pub(super) fn choose(&self, id: &str, label: &str, cx: &mut TestAppContext) { + let id = id.to_owned(); + self.frame(cx, |window, cx| window.right_click(id, cx)); + cx.run_until_parked(); + let label = label.to_owned(); + self.frame(cx, |window, cx| { + let mut menu = window.within("popup-menu"); + let at = (0usize..64) + .find(|at| { + menu.try_find(*at) + .is_some_and(|entry| entry.label() == Some(label.as_str())) + }) + .unwrap_or_else(|| panic!("the menu has no entry \u{201c}{label}\u{201d}")); + menu.click(at, cx); + }); + cx.run_until_parked(); + } +} + +/// Dispatches one pointer event and draws the frame it leads to, as the platform would. +fn pointer(window: &mut gpui_kit::Window, event: PlatformInput, cx: &mut gpui_kit::App) { + window.dispatch_event(event, cx); + window.render_frame(cx); +} + +fn moved(position: Point, pressed_button: Option) -> PlatformInput { + MouseMoveEvent { + position, + pressed_button, + modifiers: Modifiers::default(), + } + .to_platform_input() +} + +fn pressed(position: Point) -> PlatformInput { + MouseDownEvent { + button: MouseButton::Left, + position, + modifiers: Modifiers::default(), + click_count: 1, + first_mouse: false, + } + .to_platform_input() +} + +fn released(position: Point) -> PlatformInput { + MouseUpEvent { + button: MouseButton::Left, + position, + modifiers: Modifiers::default(), + click_count: 1, + } + .to_platform_input() +} + +/// The element of the bar preview that draws `id`, or the group `g` for `group:g`. +pub(super) fn preview_id(id: &str) -> String { + match id.strip_prefix("group:") { + Some(group) => format!("layout-preview-group:{group}"), + None => format!("layout-preview:{id}"), + } +} + +pub(super) const RIGHT: [&str; 4] = [ + "shell.notices", + "shell.transfers", + "tools.clock", + "shell.terminal", +]; diff --git a/crates/layout-ui/src/tests/aim.rs b/crates/layout-ui/src/tests/aim.rs new file mode 100644 index 00000000..a5ab02f0 --- /dev/null +++ b/crates/layout-ui/src/tests/aim.rs @@ -0,0 +1,154 @@ +//! While an item is held over the bar preview, one mark shows where it would land and the ghost +//! looks like the item; a release anywhere ends the drag, and the frame after it shows neither. + +use gpui_kit::{Bounds, Pixels, TestAppContext, point, px}; +use nocterm_window_layout::LayoutSettings; + +use super::{Fixture, open}; + +const MARKER: &str = "layout-drop-marker"; +const GHOST: &str = "layout-drag-ghost"; + +fn inside(inner: Bounds, outer: Bounds) -> bool { + inner.left() >= outer.left() + && inner.right() <= outer.right() + && inner.top() >= outer.top() + && inner.bottom() <= outer.bottom() +} + +/// The point `across` of the way over the element `id`, on its middle line. +fn across( + fixture: &Fixture, + id: &str, + across: f32, + cx: &mut TestAppContext, +) -> gpui_kit::Point { + let bounds = fixture.bounds(id, cx); + point( + bounds.left() + bounds.size.width * across, + bounds.center().y, + ) +} + +/// Whether the drag ended and the frame drawn after it shows no ghost and no mark. +fn ended(fixture: &Fixture, cx: &mut TestAppContext) -> bool { + !cx.update(|cx| cx.has_active_drag()) + && fixture.shown(GHOST, cx).is_none() + && fixture.shown(MARKER, cx).is_none() +} + +#[gpui_kit::test] +fn an_item_held_over_a_chip_shows_one_line_where_it_would_land(cx: &mut TestAppContext) { + let fixture = open(cx); + let at = across(&fixture, "layout-preview:shell.transfers", 0.25, cx); + fixture.hold("layout-preview:shell.terminal", at, cx); + + let marker = fixture.shown(MARKER, cx).expect("a line marks the place"); + let gap = fixture.bounds("layout-preview-gap:right:1", cx); + assert!( + inside(marker, gap), + "{marker:?} in front of transfers, in {gap:?}" + ); + + fixture.release(at, cx); + assert!(ended(&fixture, cx)); +} + +#[gpui_kit::test] +fn a_widget_released_over_a_panel_of_a_group_ends_the_drag_and_lands_beside_the_group( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + let files = fixture.bounds("layout-preview:shell.files", cx); + fixture.hold("layout-preview:tools.clock", files.center(), cx); + assert!( + fixture.shown(GHOST, cx).is_some(), + "the ghost follows the pointer" + ); + + fixture.release(files.center(), cx); + + assert!(ended(&fixture, cx), "the frame after the release is drawn"); + assert_eq!( + fixture.left_to_right(&["group:navigation", "tools.clock", "group:assistant"], cx), + ["group:navigation", "tools.clock", "group:assistant"] + ); +} + +#[gpui_kit::test] +fn a_release_that_changes_nothing_ends_the_drag_and_leaves_no_ghost_or_mark( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + for target in [ + "layout-preview:shell.transfers", + "layout-preview:tools.clock", + ] { + let at = across(&fixture, target, 0.25, cx); + fixture.hold("layout-preview:shell.transfers", at, cx); + assert!( + fixture.shown(MARKER, cx).is_none(), + "{target}: no place to mark" + ); + + fixture.release(at, cx); + assert!(ended(&fixture, cx), "{target}"); + } + assert_eq!(fixture.stored(cx), LayoutSettings::default()); +} + +#[gpui_kit::test] +fn a_release_away_from_every_chip_lands_in_the_nearest_zone(cx: &mut TestAppContext) { + let fixture = open(cx); + let (preview, zone) = ( + fixture.bounds("layout-preview", cx), + fixture.bounds("layout-preview-zone:right", cx), + ); + let below_the_end = point(zone.right() - px(1.), preview.bottom() - px(1.)); + fixture.hold("layout-preview:shell.terminal", below_the_end, cx); + fixture.release(below_the_end, cx); + + assert!(ended(&fixture, cx)); + assert_eq!( + fixture.left_to_right(&["shell.terminal", "off.item"], cx), + ["off.item", "shell.terminal"], + "last in the right zone" + ); +} + +#[gpui_kit::test] +fn a_panel_held_over_the_middle_of_a_panel_on_its_own_tints_that_panel(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.files", "Take out of group", cx); + let files = fixture.bounds("layout-preview:shell.files", cx); + fixture.hold("layout-preview:shell.agent", files.center(), cx); + + let tint = fixture.shown(MARKER, cx).expect("the panel is tinted"); + assert!(inside(tint, files), "{tint:?} on {files:?}"); + + fixture.release(files.center(), cx); + assert!(ended(&fixture, cx)); + assert!( + fixture.has("layout-preview-group:group-1", cx), + "the two make a group" + ); +} + +#[gpui_kit::test] +fn the_ghost_of_a_drag_is_as_large_as_the_chip_or_the_group_it_lifts(cx: &mut TestAppContext) { + let fixture = open(cx); + for id in [ + "layout-preview:shell.transfers", + "layout-preview-group:navigation", + ] { + let item = fixture.bounds(id, cx); + let away = item.center() + point(px(0.), px(80.)); + fixture.hold(id, away, cx); + let ghost = fixture.shown(GHOST, cx).expect("a ghost"); + let root = fixture.shown("drag-preview", cx).expect("the drag root"); + fixture.release(away, cx); + + assert_eq!(root.size, item.size, "{id}: the box it was measured in"); + assert_eq!(ghost.size, item.size, "{id}: the ghost fills it"); + } +} diff --git a/crates/layout-ui/src/tests/dnd.rs b/crates/layout-ui/src/tests/dnd.rs new file mode 100644 index 00000000..adc8fac5 --- /dev/null +++ b/crates/layout-ui/src/tests/dnd.rs @@ -0,0 +1,312 @@ +//! Dragging an item of the bar preview edits the layout in use like the keys and the menus do: +//! reordering, moving between zones and groups, to and from the tray. + +use gpui_kit::{TestAppContext, test::TestWindowExt as _}; +use nocterm_window_layout::LayoutSettings; + +use super::{Fixture, RIGHT, open}; + +fn x_of(fixture: &Fixture, id: &'static str, cx: &mut TestAppContext) -> f32 { + let mut x = None; + fixture.frame(cx, |window, _| { + x = Some(f32::from(window.find(id).bounds().origin.x)); + }); + x.unwrap_or_else(|| panic!("{id} is not in the frame")) +} + +/// How far across a chip its left half and its right half are. +const LEFT_HALF: f32 = 0.25; +const RIGHT_HALF: f32 = 0.75; + +#[gpui_kit::test] +fn a_chip_dropped_on_the_left_half_of_another_takes_the_place_in_front_of_it( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + fixture.drag_across( + "layout-preview:shell.terminal", + "layout-preview:shell.transfers", + LEFT_HALF, + cx, + ); + + assert_eq!( + fixture.left_to_right(&RIGHT, cx), + [ + "shell.notices", + "shell.terminal", + "shell.transfers", + "tools.clock" + ] + ); + let stored = fixture.stored(cx); + assert_eq!( + stored.presets.len(), + 1, + "the first edit saved a custom layout" + ); +} + +#[gpui_kit::test] +fn a_chip_dropped_on_the_right_half_of_another_takes_the_place_after_it(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag_across( + "layout-preview:shell.terminal", + "layout-preview:shell.notices", + RIGHT_HALF, + cx, + ); + + assert_eq!( + fixture.left_to_right(&RIGHT, cx), + [ + "shell.notices", + "shell.terminal", + "shell.transfers", + "tools.clock" + ] + ); +} + +#[gpui_kit::test] +fn a_chip_dropped_on_a_gap_takes_that_place(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag( + "layout-preview:shell.terminal", + "layout-preview-gap:right:0", + cx, + ); + + assert_eq!( + fixture.left_to_right(&RIGHT, cx), + [ + "shell.terminal", + "shell.notices", + "shell.transfers", + "tools.clock" + ] + ); +} + +#[gpui_kit::test] +fn a_widget_dropped_on_another_zone_moves_there(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag( + "layout-preview:shell.transfers", + "layout-preview-zone:center", + cx, + ); + + let moved = x_of(&fixture, "layout-preview:shell.transfers", cx); + assert!( + moved < x_of(&fixture, "layout-preview:shell.notices", cx), + "transfers left the right zone" + ); + assert!( + moved > x_of(&fixture, "layout-preview:shell.files", cx), + "and is not in the left one" + ); +} + +#[gpui_kit::test] +fn a_panel_dropped_on_another_group_joins_it_at_the_end(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag( + "layout-preview:shell.files", + "layout-preview-member-gap:assistant:1", + cx, + ); + + assert_eq!( + fixture.left_to_right(&["shell.files", "shell.agent", "shell.servers"], cx), + ["shell.servers", "shell.agent", "shell.files"] + ); + assert_eq!(fixture.stored(cx).presets.len(), 1); +} + +#[gpui_kit::test] +fn a_panel_dropped_on_the_left_half_of_a_panel_of_another_group_lands_in_front_of_it( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + fixture.drag_across( + "layout-preview:shell.files", + "layout-preview:shell.agent", + LEFT_HALF, + cx, + ); + + assert_eq!( + fixture.left_to_right(&["shell.files", "shell.agent"], cx), + ["shell.files", "shell.agent"] + ); + assert!( + x_of(&fixture, "layout-preview:shell.files", cx) + > x_of(&fixture, "layout-preview-group:assistant", cx), + "it is in the assistant's group" + ); +} + +#[gpui_kit::test] +fn a_panel_dropped_at_the_start_or_the_end_edge_of_a_group_is_its_first_or_last_member( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + let members = ["shell.servers", "shell.files", "shell.agent"]; + let in_sidebar = |fixture: &Fixture, cx: &mut TestAppContext| { + let pill = fixture.bounds("layout-preview-group:navigation", cx); + let chip = fixture.bounds("layout-preview:shell.agent", cx); + chip.left() >= pill.left() && chip.right() <= pill.right() + }; + // The outer half of the first member, then of the last one. + for (edge, across, order) in [ + ( + "shell.servers", + LEFT_HALF, + ["shell.agent", "shell.servers", "shell.files"], + ), + ( + "shell.files", + RIGHT_HALF, + ["shell.servers", "shell.files", "shell.agent"], + ), + ] { + let edge = format!("layout-preview:{edge}"); + fixture.drag_across("layout-preview:shell.agent", &edge, across, cx); + + assert_eq!(fixture.left_to_right(&members, cx), order, "{edge}"); + assert!(in_sidebar(&fixture, cx), "{edge}: inside the group"); + } +} + +#[gpui_kit::test] +fn a_panel_moved_inside_its_group_leaves_the_group_unselected(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag_across( + "layout-preview:shell.servers", + "layout-preview:shell.files", + RIGHT_HALF, + cx, + ); + + assert_eq!( + fixture.left_to_right(&["shell.servers", "shell.files"], cx), + ["shell.files", "shell.servers"] + ); + assert!( + !fixture.is_selected("group:navigation", cx), + "the release over the group was the drop, not a click on it" + ); +} + +#[gpui_kit::test] +fn a_group_dropped_on_a_zone_moves_whole(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag( + "layout-preview-group:navigation", + "layout-preview-gap:right:0", + cx, + ); + + let group = x_of(&fixture, "layout-preview-group:navigation", cx); + assert!(group < x_of(&fixture, "layout-preview:shell.notices", cx)); + assert!(group > x_of(&fixture, "layout-preview-zone:center", cx)); + assert!( + x_of(&fixture, "layout-preview:shell.servers", cx) > group, + "its panels follow it" + ); +} + +#[gpui_kit::test] +fn a_chip_dropped_in_the_tray_leaves_the_bar(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag("layout-preview:shell.terminal", "layout-tray", cx); + + assert!(fixture.has("layout-tray:shell.terminal", cx)); + assert!(!fixture.has("layout-preview:shell.terminal", cx)); +} + +#[gpui_kit::test] +fn an_item_dragged_out_of_the_tray_returns_in_front_of_the_chip_it_is_dropped_on( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + fixture.drag("layout-preview:tools.clock", "layout-tray", cx); + fixture.drag_across( + "layout-tray:tools.clock", + "layout-preview:shell.notices", + LEFT_HALF, + cx, + ); + + assert!(!fixture.has("layout-tray:tools.clock", cx)); + assert_eq!( + fixture.left_to_right(&RIGHT, cx), + [ + "tools.clock", + "shell.notices", + "shell.transfers", + "shell.terminal" + ] + ); +} + +#[gpui_kit::test] +fn a_click_on_an_item_of_the_tray_puts_it_back(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag("layout-preview:tools.clock", "layout-tray", cx); + fixture.click("layout-tray:tools.clock", cx); + + assert!(fixture.has("layout-preview:tools.clock", cx)); + assert!(!fixture.has("layout-tray:tools.clock", cx)); +} + +#[gpui_kit::test] +fn the_pinned_notice_cannot_be_dropped_in_the_tray_and_the_editor_says_why( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + fixture.drag("layout-preview:shell.notices", "layout-tray", cx); + + assert_eq!(fixture.stored(cx), LayoutSettings::default()); + assert!(fixture.has("layout-preview:shell.notices", cx)); + assert!(!fixture.has("layout-tray:shell.notices", cx)); + assert!(fixture.has("layout-notice", cx), "the refusal is shown"); +} + +#[gpui_kit::test] +fn a_drop_is_one_step_of_the_history(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag_across( + "layout-preview:shell.terminal", + "layout-preview:shell.transfers", + LEFT_HALF, + cx, + ); + fixture.click("layout-undo", cx); + + assert_eq!(fixture.left_to_right(&RIGHT, cx), RIGHT); +} + +#[gpui_kit::test] +fn nothing_is_written_when_an_item_is_dropped_where_it_already_is(cx: &mut TestAppContext) { + let fixture = open(cx); + for (to, across) in [ + ("layout-preview:shell.transfers", LEFT_HALF), + ("layout-preview:shell.transfers", RIGHT_HALF), + ("layout-preview:tools.clock", LEFT_HALF), + ] { + fixture.drag_across("layout-preview:shell.transfers", to, across, cx); + } + + assert_eq!(fixture.stored(cx), LayoutSettings::default()); +} + +#[gpui_kit::test] +fn a_built_in_layout_that_is_not_in_use_takes_no_drops(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-choice:swapped", cx); + fixture.drag("layout-preview:shell.terminal", "layout-tray", cx); + + assert_eq!(fixture.stored(cx), LayoutSettings::default()); +} diff --git a/crates/layout-ui/src/tests/editing.rs b/crates/layout-ui/src/tests/editing.rs new file mode 100644 index 00000000..14a3d335 --- /dev/null +++ b/crates/layout-ui/src/tests/editing.rs @@ -0,0 +1,189 @@ +//! Moving, grouping and removing items edits the layout in use. + +use gpui_kit::TestAppContext; +use nocterm_settings::SettingsDocument; +use nocterm_window_layout::{LayoutSettings, PresetId}; + +use super::{RIGHT, open, open_over, open_with}; + +#[gpui_kit::test] +fn the_first_move_in_the_default_layout_saves_an_active_custom_layout(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-left", cx); + + let stored = fixture.stored(cx); + assert_eq!(stored.presets.len(), 1); + assert_eq!(stored.presets[0].name, "Custom"); + assert_eq!(stored.active, Some(PresetId::from_owned("p1".to_owned()))); + assert_eq!( + fixture.left_to_right(&RIGHT, cx), + [ + "shell.transfers", + "shell.notices", + "tools.clock", + "shell.terminal" + ] + ); +} + +#[gpui_kit::test] +fn two_quick_moves_from_the_keyboard_land_in_one_custom_layout(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-right alt-right", cx); + + let stored = fixture.stored(cx); + assert_eq!(stored.presets.len(), 1, "one Custom layout, not two"); + assert_eq!(stored.active, Some(PresetId::from_owned("p1".to_owned()))); + assert_eq!( + fixture.left_to_right(&RIGHT, cx), + [ + "shell.notices", + "tools.clock", + "shell.transfers", + "shell.terminal" + ] + ); +} + +#[gpui_kit::test] +fn an_item_moves_to_another_zone_from_its_menu(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.transfers", "Move to Left", cx); + + assert_eq!( + fixture.left_to_right(&["shell.files", "shell.transfers", "shell.notices"], cx), + ["shell.files", "shell.transfers", "shell.notices"], + ); +} + +#[gpui_kit::test] +fn a_pinned_item_cannot_be_removed_from_the_bar(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.notices", cx); + fixture.keys("delete", cx); + + assert!(fixture.stored(cx).presets.is_empty(), "nothing was written"); + assert!(fixture.has("layout-preview:shell.notices", cx)); +} + +#[gpui_kit::test] +fn a_hidden_item_waits_in_the_tray_until_it_is_added_back(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:tools.clock", "Hide from the bar", cx); + + assert!(!fixture.has("layout-preview:tools.clock", cx)); + assert!(fixture.has("layout-tray:tools.clock", cx)); + + fixture.click("layout-tray:tools.clock", cx); + assert!(fixture.has("layout-preview:tools.clock", cx)); + assert!(!fixture.has("layout-tray:tools.clock", cx)); +} + +#[gpui_kit::test] +fn the_delete_key_removes_the_selected_item(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.terminal", cx); + fixture.keys("delete", cx); + + assert!(fixture.has("layout-tray:shell.terminal", cx)); + assert!(!fixture.has("layout-preview:shell.terminal", cx)); +} + +#[gpui_kit::test] +fn a_click_selects_the_chip_the_keys_act_on(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.terminal", cx); + + assert!(fixture.is_selected("shell.terminal", cx)); + assert!(!fixture.is_selected("shell.transfers", cx)); + + fixture.click("layout-preview-group:navigation", cx); + assert!(fixture.is_selected("group:navigation", cx)); + assert!(!fixture.is_selected("shell.terminal", cx)); +} + +#[gpui_kit::test] +fn a_separator_is_added_after_the_item_and_removed_from_its_own_menu(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.transfers", "Add separator after", cx); + + let separator = "layout-preview-separator:right:2"; + assert!(fixture.has(separator, cx)); + fixture.choose(separator, "Remove separator", cx); + assert!(!fixture.has(separator, cx)); +} + +#[gpui_kit::test] +fn a_panel_opens_with_the_layout_when_its_menu_says_so(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.files", "Open with the layout", cx); + + let stored = fixture.stored(cx); + assert_eq!(stored.presets.len(), 1); + assert_eq!(stored.presets[0].open.len(), 1); + assert_eq!(stored.presets[0].open[0].as_str(), "shell.files"); +} + +#[gpui_kit::test] +fn a_stored_id_the_application_does_not_know_can_be_forgotten(cx: &mut TestAppContext) { + let mut preset = nocterm_window_layout::LayoutPreset { + id: PresetId::from_owned("p1".to_owned()), + name: "Mine".to_owned(), + ..Default::default() + }; + preset + .tray + .push(nocterm_plugin_model::ContributionId::new("ghost.thing")); + let stored = nocterm_window_layout::LayoutSettings { + active: Some(preset.id.clone()), + presets: vec![preset], + }; + let fixture = open_with(stored, cx); + + assert!(fixture.has("layout-unknown:ghost.thing", cx)); + fixture.click("layout-forget:ghost.thing", cx); + assert!(!fixture.has("layout-unknown:ghost.thing", cx)); +} + +#[gpui_kit::test] +fn an_item_of_a_disabled_plugin_is_marked_and_still_shown(cx: &mut TestAppContext) { + let fixture = open(cx); + + assert_eq!( + fixture.label("layout-preview:off.item", cx).as_deref(), + Some("Dormant (disabled)") + ); + assert_eq!( + fixture.label("layout-preview:tools.clock", cx).as_deref(), + Some("Clock") + ); +} + +#[gpui_kit::test] +fn a_layouts_table_that_does_not_read_is_not_overwritten(cx: &mut TestAppContext) { + let table = "[layouts]\nactive = \"p1\"\n[[layouts.presets]]\nid = \"p1\"\n\ + [layouts.presets.bar]\nleft = [{ separator = false }]\n"; + let mut document = SettingsDocument::from_table(table.parse().expect("the text is toml")); + document.register::(); + let written = document.to_table().get("layouts").cloned(); + let fixture = open_over(document, cx); + + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-left", cx); + + cx.update(|cx| { + assert_eq!( + fixture + .settings + .document(cx) + .to_table() + .get("layouts") + .cloned(), + written + ); + assert_eq!(fixture.settings.revision(cx), 0, "nothing was written"); + }); + assert!(fixture.has("layout-notice", cx), "the user is told why"); +} diff --git a/crates/layout-ui/src/tests/geometry.rs b/crates/layout-ui/src/tests/geometry.rs new file mode 100644 index 00000000..a107a61f --- /dev/null +++ b/crates/layout-ui/src/tests/geometry.rs @@ -0,0 +1,166 @@ +//! The editor lays itself out inside the width it is given: the bar stays one line inside its +//! box and the tray stays below it, at a Settings page's width and at a narrow one. + +use gpui_kit::{Bounds, Pixels, TestAppContext}; + +use super::{Fixture, open}; + +/// The widths tried: a wide window, a Settings page beside its navigation, a narrow window. +const WIDTHS: [f32; 3] = [1200., 660., 420.]; + +/// The widths at which the whole bar of the default layout fits, in full, packed or as icons. +const FITTING: [f32; 3] = [1200., 660., 580.]; + +fn overlap(a: Bounds, b: Bounds) -> bool { + let slack = gpui_kit::px(0.5); + a.left() + slack < b.right() + && b.left() + slack < a.right() + && a.top() + slack < b.bottom() + && b.top() + slack < a.bottom() +} + +fn inside(inner: Bounds, outer: Bounds) -> bool { + let slack = gpui_kit::px(0.5); + inner.left() + slack >= outer.left() + && inner.right() <= outer.right() + slack + && inner.top() + slack >= outer.top() + && inner.bottom() <= outer.bottom() + slack +} + +fn bounds(fixture: &Fixture, id: &str, cx: &mut TestAppContext) -> Bounds { + fixture.bounds(id, cx) +} + +/// The chips of the bar preview that the default layout places. +const CHIPS: [&str; 10] = [ + "layout-preview:shell.servers", + "layout-preview:shell.files", + "layout-preview:shell.agent", + "layout-preview:shell.notices", + "layout-preview:shell.transfers", + "layout-preview:tools.clock", + "layout-preview:tools.cpu", + "layout-preview:shell.terminal", + "layout-preview:plain.note", + "layout-preview:off.item", +]; + +fn centre_y(bounds: Bounds) -> f32 { + f32::from(bounds.origin.y + bounds.size.height / 2.) +} + +/// The chips of the preview in the frame, with their bounds. +fn chips(fixture: &Fixture, cx: &mut TestAppContext) -> Vec<(&'static str, Bounds)> { + let mut found = Vec::new(); + for id in CHIPS { + if fixture.has(id, cx) { + found.push((id, bounds(fixture, id, cx))); + } + } + found +} + +#[gpui_kit::test] +fn the_bar_preview_is_one_line_inside_its_box_at_every_width(cx: &mut TestAppContext) { + let fixture = open(cx); + for width in FITTING { + fixture.resize(width, cx); + let editor = bounds(&fixture, "layout-editor", cx); + let preview = bounds(&fixture, "layout-preview", cx); + assert!( + inside(preview, editor), + "{width}: {preview:?} in {editor:?}" + ); + let chips = chips(&fixture, cx); + assert!(chips.len() >= 6, "{width}: the bar was drawn"); + let line = centre_y(chips[0].1); + for (id, chip) in &chips { + assert!( + (centre_y(*chip) - line).abs() < 0.5, + "{width}: {id} {chip:?} is not on the line at {line}" + ); + assert!( + inside(*chip, preview), + "{width}: {id} {chip:?} in {preview:?}" + ); + } + for zone in ["left", "center", "right"] { + let zone = bounds(&fixture, &format!("layout-preview-zone:{zone}"), cx); + assert!(inside(zone, preview), "{width}: {zone:?} in {preview:?}"); + assert!((centre_y(zone) - line).abs() < 0.5, "{width}: {zone:?}"); + } + } +} + +#[gpui_kit::test] +fn the_bar_preview_names_its_chips_when_there_is_room_and_only_shows_icons_when_not( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + let id = "layout-preview:shell.transfers"; + + fixture.resize(2800., cx); + let roomy = bounds(&fixture, id, cx); + fixture.resize(660., cx); + let tight = bounds(&fixture, id, cx); + + assert!( + roomy.size.width > tight.size.width + gpui_kit::px(20.), + "{roomy:?} / {tight:?}" + ); + assert!(fixture.has("layout-preview-line", cx), "one line in both"); +} + +#[gpui_kit::test] +fn the_bar_preview_keeps_the_zones_in_order_from_left_to_right(cx: &mut TestAppContext) { + let fixture = open(cx); + for width in FITTING { + fixture.resize(width, cx); + let left = bounds(&fixture, "layout-preview-zone:left", cx); + let centre = bounds(&fixture, "layout-preview-zone:center", cx); + let right = bounds(&fixture, "layout-preview-zone:right", cx); + assert!(left.right() <= centre.left() + gpui_kit::px(0.5), "{width}"); + assert!( + centre.right() <= right.left() + gpui_kit::px(0.5), + "{width}" + ); + } +} + +#[gpui_kit::test] +fn the_bar_preview_in_a_very_narrow_box_scrolls_instead_of_wrapping(cx: &mut TestAppContext) { + let fixture = open(cx); + for width in [420., 180.] { + fixture.resize(width, cx); + let editor = bounds(&fixture, "layout-editor", cx); + let preview = bounds(&fixture, "layout-preview", cx); + assert!( + inside(preview, editor), + "{width}: {preview:?} in {editor:?}" + ); + let chips = chips(&fixture, cx); + let line = centre_y(chips[0].1); + assert!( + chips + .iter() + .all(|(_, chip)| (centre_y(*chip) - line).abs() < 0.5), + "{width}: one line: {chips:?}" + ); + } +} + +#[gpui_kit::test] +fn the_tray_stays_below_the_bar_and_inside_the_editor(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag("layout-preview:tools.clock", "layout-tray", cx); + for width in WIDTHS { + fixture.resize(width, cx); + let tray = bounds(&fixture, "layout-tray", cx); + let preview = bounds(&fixture, "layout-preview", cx); + let chip = bounds(&fixture, "layout-tray:tools.clock", cx); + assert!(!overlap(tray, preview), "{width}: {tray:?} / {preview:?}"); + assert!(preview.bottom() <= tray.top(), "{width}: below"); + assert!(inside(chip, tray), "{width}: {chip:?} in {tray:?}"); + assert!(inside(tray, bounds(&fixture, "layout-editor", cx))); + } +} diff --git a/crates/layout-ui/src/tests/grouping.rs b/crates/layout-ui/src/tests/grouping.rs new file mode 100644 index 00000000..67b2534d --- /dev/null +++ b/crates/layout-ui/src/tests/grouping.rs @@ -0,0 +1,144 @@ +//! Panels stand alone in the bar or share a group: dragging and the menus take a panel out of +//! its group, put two panels together, start, rename and undo a group. + +use gpui_kit::{Bounds, Pixels, TestAppContext}; + +use super::{Fixture, open, preview_id}; + +fn bounds(fixture: &Fixture, id: &str, cx: &mut TestAppContext) -> Bounds { + fixture.bounds(&preview_id(id), cx) +} + +/// Whether the chip of `panel` is drawn inside the pill of `group`. +fn in_group(fixture: &Fixture, panel: &str, group: &str, cx: &mut TestAppContext) -> bool { + let (chip, pill) = ( + bounds(fixture, panel, cx), + bounds(fixture, &format!("group:{group}"), cx), + ); + chip.left() >= pill.left() && chip.right() <= pill.right() +} + +#[gpui_kit::test] +fn a_panel_dropped_between_groups_stands_alone_there(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.drag( + "layout-preview:shell.files", + "layout-preview-gap:left:1", + cx, + ); + + assert!(!in_group(&fixture, "shell.files", "navigation", cx)); + assert!(!in_group(&fixture, "shell.files", "assistant", cx)); + assert_eq!( + fixture.left_to_right(&["group:navigation", "shell.files", "group:assistant"], cx), + ["group:navigation", "shell.files", "group:assistant"] + ); + assert_eq!(fixture.stored(cx).presets.len(), 1); +} + +#[gpui_kit::test] +fn a_panel_taken_out_of_its_group_from_the_menu_stands_right_after_it(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.files", "Take out of group", cx); + + assert!(!in_group(&fixture, "shell.files", "navigation", cx)); + assert!(in_group(&fixture, "shell.servers", "navigation", cx)); + assert_eq!( + fixture.left_to_right(&["group:navigation", "shell.files", "group:assistant"], cx), + ["group:navigation", "shell.files", "group:assistant"] + ); +} + +#[gpui_kit::test] +fn a_panel_dropped_on_a_panel_that_stands_alone_makes_a_group_of_the_two(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.files", "Take out of group", cx); + fixture.drag( + "layout-preview:shell.agent", + "layout-preview:shell.files", + cx, + ); + + assert!(fixture.has("layout-preview-group:group-1", cx)); + assert!(in_group(&fixture, "shell.files", "group-1", cx)); + assert!(in_group(&fixture, "shell.agent", "group-1", cx)); +} + +#[gpui_kit::test] +fn a_new_group_from_the_menu_holds_the_panel_and_ungrouping_lets_it_stand_alone( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.agent", "New group", cx); + + assert!(in_group(&fixture, "shell.agent", "group-1", cx)); + + fixture.choose("layout-preview-member-gap:group-1:0", "Ungroup", cx); + assert!(!fixture.has("layout-preview-group:group-1", cx)); + assert!(fixture.has("layout-preview:shell.agent", cx)); +} + +#[gpui_kit::test] +fn a_group_is_renamed_from_its_menu(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose( + "layout-preview-member-gap:assistant:0", + "Rename group\u{2026}", + cx, + ); + assert!(fixture.has("layout-rename-row", cx)); + + fixture.keys("ctrl-a", cx); + fixture.keys("H e l p enter", cx); + + assert!(!fixture.has("layout-rename-row", cx)); + assert_eq!( + fixture + .label("layout-preview-group:assistant", cx) + .as_deref(), + Some("Help") + ); +} + +#[gpui_kit::test] +fn a_click_on_a_panel_of_a_group_selects_the_panel_and_one_beside_it_the_group( + cx: &mut TestAppContext, +) { + let fixture = open(cx); + fixture.click("layout-preview:shell.agent", cx); + + assert!(fixture.is_selected("shell.agent", cx)); + assert!(!fixture.is_selected("group:assistant", cx)); + + fixture.click("layout-preview-member-gap:assistant:0", cx); + assert!(fixture.is_selected("group:assistant", cx)); + assert!(!fixture.is_selected("shell.agent", cx)); +} + +#[gpui_kit::test] +fn a_right_click_on_a_panel_of_a_group_opens_the_menu_of_the_panel(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.choose("layout-preview:shell.agent", "New group", cx); + + assert!(in_group(&fixture, "shell.agent", "group-1", cx)); +} + +#[gpui_kit::test] +fn the_panel_that_opens_with_the_layout_is_marked_in_its_group(cx: &mut TestAppContext) { + let fixture = open(cx); + assert_eq!(fixture.opens("shell.files", cx), Some(false)); + + fixture.choose("layout-preview:shell.files", "Open with the layout", cx); + + assert_eq!(fixture.opens("shell.files", cx), Some(true)); + assert_eq!( + fixture.opens("shell.servers", cx), + Some(false), + "one per group" + ); + assert_eq!( + fixture.opens("shell.transfers", cx), + None, + "a widget opens nothing" + ); +} diff --git a/crates/layout-ui/src/tests/history.rs b/crates/layout-ui/src/tests/history.rs new file mode 100644 index 00000000..e60a9cee --- /dev/null +++ b/crates/layout-ui/src/tests/history.rs @@ -0,0 +1,126 @@ +//! Undo and redo restore the layouts the editor changed, and give way to outside changes. + +use gpui_kit::TestAppContext; +use nocterm_window_layout::LayoutSettings; + +use super::{RIGHT, open, open_on_disk}; + +const ORIGINAL: [&str; 4] = [ + "shell.notices", + "shell.transfers", + "tools.clock", + "shell.terminal", +]; + +#[gpui_kit::test] +fn undo_puts_an_item_back_and_redo_moves_it_again(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-left", cx); + let moved = fixture.left_to_right(&RIGHT, cx); + assert_ne!(moved, ORIGINAL); + + fixture.click("layout-undo", cx); + assert_eq!(fixture.left_to_right(&RIGHT, cx), ORIGINAL); + + fixture.click("layout-redo", cx); + assert_eq!(fixture.left_to_right(&RIGHT, cx), moved); +} + +#[gpui_kit::test] +fn the_keyboard_undoes_and_redoes(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-right", cx); + let moved = fixture.left_to_right(&RIGHT, cx); + + fixture.keys("ctrl-z", cx); + assert_eq!(fixture.left_to_right(&RIGHT, cx), ORIGINAL); + + fixture.keys("ctrl-shift-z", cx); + assert_eq!(fixture.left_to_right(&RIGHT, cx), moved); +} + +#[gpui_kit::test] +fn there_is_nothing_to_undo_before_an_edit(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-undo", cx); + fixture.keys("ctrl-z", cx); + + assert_eq!(fixture.stored(cx), LayoutSettings::default()); +} + +#[gpui_kit::test] +fn a_change_from_elsewhere_ends_the_history(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-left", cx); + + // Another window renames the layout the editor just saved. + cx.update(|cx| { + drop(fixture.settings.update::(cx, |layouts| { + layouts.presets[0].name = "Elsewhere".to_owned(); + })); + }); + cx.run_until_parked(); + fixture.click("layout-undo", cx); + + let stored = fixture.stored(cx); + assert_eq!( + stored.presets.len(), + 1, + "undo would have overwritten the other change" + ); + assert_eq!(stored.presets[0].name, "Elsewhere"); +} + +#[gpui_kit::test] +fn two_undos_pressed_before_the_first_is_stored_take_back_one_step_and_lose_none( + cx: &mut TestAppContext, +) { + let directory = tempfile::tempdir().unwrap(); + let fixture = open_on_disk(directory.path(), cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-left", cx); + let first = fixture.wait_for(cx, |stored| stored.presets.len() == 1); + fixture.click("layout-preview:shell.terminal", cx); + fixture.keys("alt-left", cx); + let second = fixture.wait_for(cx, |stored| stored != &first); + + // Both presses arrive while the file is still being written. + fixture.keys("ctrl-z ctrl-z", cx); + let undone = fixture.wait_for(cx, |stored| stored != &second); + assert_eq!(undone, first, "one step back, not two"); + + fixture.keys("ctrl-z", cx); + let start = fixture.wait_for(cx, |stored| stored.presets.is_empty()); + assert!( + start.presets.is_empty(), + "the first edit could still be undone" + ); + let mut on_disk = nocterm_settings::SettingsFile::new(directory.path().join("settings.toml")) + .load() + .unwrap(); + on_disk.register::(); + assert_eq!(on_disk.get::().presets, start.presets); +} + +#[gpui_kit::test] +fn an_undo_pressed_right_after_a_move_leaves_the_move_alone(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-preview:shell.terminal", cx); + fixture.keys("alt-left", cx); + let first = fixture.left_to_right(&RIGHT, cx); + fixture.click("layout-preview:shell.transfers", cx); + + // The undo would restore the layouts from before the first move, and lose the second. + fixture.keys("alt-right ctrl-z", cx); + let second = fixture.left_to_right(&RIGHT, cx); + assert_ne!(second, first); + assert_ne!(second, ORIGINAL, "the second move is still there"); + + fixture.keys("ctrl-z", cx); + assert_eq!(fixture.left_to_right(&RIGHT, cx), first); + fixture.keys("ctrl-z", cx); + assert_eq!(fixture.left_to_right(&RIGHT, cx), ORIGINAL); +} diff --git a/crates/layout-ui/src/tests/menu.rs b/crates/layout-ui/src/tests/menu.rs new file mode 100644 index 00000000..6fb649dc --- /dev/null +++ b/crates/layout-ui/src/tests/menu.rs @@ -0,0 +1,167 @@ +//! The menu of an item offers the moves the layout allows and none it refuses. + +use std::collections::BTreeSet; + +use nocterm_plugin_enablement::PluginEnablement; +use nocterm_plugin_model::{ContributionId, GroupId, Zone}; +use nocterm_window_layout::{EditOp, LayoutSettings, Subject}; + +use super::catalog; +use crate::{ + menu::{Entry, entries}, + snapshot::Snapshot, +}; + +fn menu_of(subject: &Subject) -> Vec { + let catalog = catalog(); + let snapshot = Snapshot::new( + &LayoutSettings::default(), + &PluginEnablement::default(), + false, + None, + &catalog, + ); + entries(subject, &snapshot, &catalog) +} + +fn item(id: &'static str) -> Subject { + Subject::Contribution(ContributionId::new(id)) +} + +fn labels(entries: &[Entry]) -> BTreeSet { + entries + .iter() + .filter_map(|entry| match entry { + Entry::Do { label, .. } | Entry::Ask { label, .. } | Entry::Submenu { label, .. } => { + Some(label.clone()) + } + Entry::Rule => None, + }) + .collect() +} + +fn op(entries: &[Entry], wanted: &str) -> Option<(EditOp, bool)> { + entries.iter().find_map(|entry| match entry { + Entry::Do { + label, op, enabled, .. + } if label == wanted => Some((op.clone(), *enabled)), + _ => None, + }) +} + +fn submenu<'a>(entries: &'a [Entry], wanted: &str) -> Option<&'a [Entry]> { + entries.iter().find_map(|entry| match entry { + Entry::Submenu { label, entries } if label.starts_with(wanted) => Some(entries.as_slice()), + _ => None, + }) +} + +fn group(id: &'static str) -> Subject { + Subject::Group(GroupId::new(id)) +} + +#[test] +fn a_widget_offers_the_other_zones_and_hiding() { + let menu = menu_of(&item("shell.transfers")); + + let labels = labels(&menu); + assert!(labels.contains("Move to Left")); + assert!(labels.contains("Move to Centre")); + assert!(!labels.contains("Move to Right"), "it is there already"); + assert!(labels.contains("Hide from the bar")); +} + +#[test] +fn the_pinned_notice_cannot_be_hidden_from_its_menu() { + let menu = menu_of(&item("shell.notices")); + + assert!(!labels(&menu).contains("Hide from the bar")); +} + +#[test] +fn the_first_widget_cannot_move_left_and_the_last_cannot_move_right() { + let first = menu_of(&item("shell.notices")); + let last = menu_of(&item("off.item")); + + assert_eq!( + op(&first, "Move left").map(|(_, enabled)| enabled), + Some(false) + ); + assert_eq!( + op(&first, "Move right").map(|(_, enabled)| enabled), + Some(true) + ); + assert_eq!( + op(&last, "Move right").map(|(_, enabled)| enabled), + Some(false) + ); +} + +#[test] +fn a_panel_of_a_group_can_leave_it_join_another_or_start_its_own() { + let menu = menu_of(&item("shell.files")); + + let labels = labels(&menu); + assert!(labels.contains("Take out of group")); + assert!(labels.contains("Move into group")); + assert!(labels.contains("New group")); + assert!( + !labels.contains("Move to Centre"), + "its group moves, not the panel" + ); + assert_eq!( + op(&menu, "New group").map(|(op, _)| op), + Some(EditOp::NewGroup { + panel: ContributionId::new("shell.files") + }) + ); +} + +#[test] +fn a_panel_moves_its_group_between_zones_from_the_group_submenu() { + let menu = menu_of(&item("shell.files")); + let of_group = submenu(&menu, "Group").expect("the group's own entries"); + + let (to_center, enabled) = op(of_group, "Move to Centre").expect("a zone entry"); + assert!(enabled); + assert_eq!( + to_center, + EditOp::ToZone { + subject: group("navigation"), + zone: Zone::Center, + } + ); +} + +#[test] +fn every_group_can_be_renamed_and_every_group_but_the_primary_one_ungrouped() { + let navigation = labels(&menu_of(&group("navigation"))); + let assistant = labels(&menu_of(&group("assistant"))); + + assert!(navigation.contains("Rename group\u{2026}")); + assert!(!navigation.contains("Ungroup"), "the primary group stays"); + assert!(assistant.contains("Rename group\u{2026}")); + assert!(assistant.contains("Ungroup")); +} + +#[test] +fn only_a_group_in_the_centre_chooses_the_side_it_opens_on() { + let left = labels(&menu_of(&group("navigation"))); + assert!(!left.contains("Opens on"), "a left group opens on the left"); +} + +#[test] +fn a_panel_can_open_with_the_layout() { + let menu = menu_of(&item("shell.files")); + + assert_eq!( + op(&menu, "Open with the layout"), + Some(( + EditOp::SetOpen { + panel: ContributionId::new("shell.files"), + open: true, + }, + true + )) + ); +} diff --git a/crates/layout-ui/src/tests/presets.rs b/crates/layout-ui/src/tests/presets.rs new file mode 100644 index 00000000..6a2ca22c --- /dev/null +++ b/crates/layout-ui/src/tests/presets.rs @@ -0,0 +1,195 @@ +//! Choosing, duplicating, renaming and deleting layouts. + +use gpui_kit::{TestAppContext, test::TestWindowExt as _}; +use nocterm_window_layout::{LayoutPreset, LayoutSettings, PresetId}; + +use super::{open, open_with}; + +fn pid(id: &str) -> PresetId { + PresetId::from_owned(id.to_owned()) +} + +fn saved(id: &str, name: &str) -> LayoutPreset { + LayoutPreset { + id: pid(id), + name: name.to_owned(), + ..LayoutPreset::default() + } +} + +fn two_saved() -> LayoutSettings { + LayoutSettings { + active: Some(pid("p1")), + presets: vec![saved("p1", "Work"), saved("p2", "Home")], + } +} + +#[gpui_kit::test] +fn showing_another_layout_does_not_switch_the_one_in_use(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + fixture.click("layout-choice:p2", cx); + + assert_eq!(fixture.stored(cx), two_saved()); +} + +#[gpui_kit::test] +fn a_built_in_layout_not_in_use_is_read_only_until_it_is_duplicated(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + fixture.click("layout-choice:swapped", cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-left delete", cx); + + assert_eq!(fixture.stored(cx), two_saved(), "the keys change nothing"); + assert!(!fixture.has("layout-duplicate", cx)); + assert!( + !fixture.has("layout-preview-gap:right:0", cx), + "nothing to drop on" + ); + + fixture.click("layout-duplicate-to-edit", cx); + + let stored = fixture.stored(cx); + assert_eq!(stored.presets.len(), 3); + assert_eq!( + stored.active, + Some(pid("p1")), + "the layout in use is the same" + ); + assert!( + fixture.has("layout-preview-gap:right:0", cx), + "the copy is shown and editable" + ); + assert!(fixture.has("layout-duplicate", cx)); +} + +#[gpui_kit::test] +fn duplicating_the_default_layout_leaves_it_in_use(cx: &mut TestAppContext) { + let fixture = open(cx); + fixture.click("layout-duplicate", cx); + + let stored = fixture.stored(cx); + assert_eq!(stored.presets.len(), 1); + assert_eq!(stored.active, None); +} + +#[gpui_kit::test] +fn a_layout_is_renamed_with_enter(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + fixture.click("layout-rename", cx); + fixture.keys("ctrl-a", cx); + fixture.keys("O f f i c e enter", cx); + + assert_eq!(fixture.stored(cx).presets[0].name, "Office"); + assert!(!fixture.has("layout-rename-row", cx)); +} + +#[gpui_kit::test] +fn a_name_that_is_taken_is_refused_beside_the_field(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + fixture.click("layout-rename", cx); + fixture.keys("ctrl-a", cx); + fixture.keys("H o m e", cx); + fixture.click("layout-rename-save", cx); + + assert!(fixture.has("layout-rename-row", cx), "still asking"); + assert!(fixture.has("layout-rename-error", cx)); + assert_eq!(fixture.stored(cx).presets[0].name, "Work"); + + fixture.click("layout-rename-cancel", cx); + assert!(!fixture.has("layout-rename-row", cx)); +} + +#[gpui_kit::test] +fn a_blank_name_is_refused(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + fixture.click("layout-rename", cx); + fixture.keys("ctrl-a backspace", cx); + fixture.click("layout-rename-save", cx); + + assert!(fixture.has("layout-rename-error", cx)); + assert_eq!(fixture.stored(cx).presets[0].name, "Work"); +} + +#[gpui_kit::test] +fn deleting_a_layout_asks_first_and_can_be_undone(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + fixture.click("layout-delete", cx); + assert!(fixture.has("layout-delete-confirm", cx)); + assert_eq!( + fixture.stored(cx).presets.len(), + 2, + "nothing is deleted yet" + ); + + fixture.click("layout-delete-cancel", cx); + assert!(!fixture.has("layout-delete-confirm", cx)); + + fixture.click("layout-delete", cx); + fixture.click("layout-delete-confirmed", cx); + assert_eq!(fixture.stored(cx).presets.len(), 1); + assert_eq!( + fixture.label("layout-notice", cx).as_deref(), + Some("Deleted \u{201c}Work\u{201d}."), + "the notice names the layout that went" + ); + + fixture.click("layout-notice-undo", cx); + assert_eq!(fixture.stored(cx).presets, two_saved().presets); +} + +#[gpui_kit::test] +fn resetting_a_layout_arranges_it_by_the_hints_again_and_can_be_undone(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + fixture.click("layout-preview:shell.transfers", cx); + fixture.keys("alt-left", cx); + let moved = fixture.stored(cx); + assert_ne!(moved.presets[0].bar, LayoutPreset::default().bar); + + fixture.click("layout-reset", cx); + assert_eq!( + fixture.stored(cx).presets[0].bar, + LayoutPreset::default().bar + ); + assert_eq!( + fixture.stored(cx).presets[0].name, + "Work", + "only the arrangement" + ); + assert_eq!( + fixture.label("layout-notice", cx).as_deref(), + Some("Reset \u{201c}Work\u{201d}.") + ); + + fixture.click("layout-notice-undo", cx); + assert_eq!(fixture.stored(cx), moved); +} + +#[gpui_kit::test] +fn a_built_in_layout_offers_no_rename_reset_or_delete(cx: &mut TestAppContext) { + let fixture = open(cx); + + assert!(fixture.has("layout-duplicate", cx)); + for tool in ["layout-rename", "layout-reset", "layout-delete"] { + assert!(!fixture.has(tool, cx), "{tool}"); + } +} + +#[gpui_kit::test] +fn renaming_takes_the_line_of_tools_so_the_bar_stays_where_it_is(cx: &mut TestAppContext) { + let fixture = open_with(two_saved(), cx); + let at = |cx: &mut TestAppContext| { + let mut y = None; + fixture.frame(cx, |window, _| { + y = window + .try_find("layout-preview") + .map(|preview| f32::from(preview.bounds().origin.y)); + }); + y.expect("the preview is drawn") + }; + let before = at(cx); + + fixture.click("layout-rename", cx); + assert!(fixture.has("layout-rename-row", cx)); + assert!(!fixture.has("layout-duplicate", cx), "the tools give way"); + assert!((at(cx) - before).abs() < 0.5); +} diff --git a/crates/layout-ui/src/tray.rs b/crates/layout-ui/src/tray.rs new file mode 100644 index 00000000..f6348865 --- /dev/null +++ b/crates/layout-ui/src/tray.rs @@ -0,0 +1,186 @@ +//! What the layout leaves out of the bar: contributions that can be put back, and ids this +//! application does not know. The tray takes what is dropped on it out of the bar. + +use std::{rc::Rc, sync::Arc}; + +use gpui_kit::{ + AnyElement, App, IntoElement, RenderOnce, SharedString, TestSupportExt as _, Window, + component::{ + ActiveTheme as _, Disableable as _, Sizable as _, + button::{Button, ButtonVariants as _}, + h_flex, + menu::ContextMenuExt as _, + }, + div, + prelude::*, + px, +}; +use nocterm_plugin_model::{ContributionId, ContributionRef, PluginSet}; +use nocterm_ui::{DragSource, IconName, contribution_icon}; +use nocterm_window_layout::{EditOp, Subject}; + +use crate::{ + commands::Commands, + dnd::{self, Dragged}, + face::{BORDER, CHIP_HEIGHT, Face, Look}, + menu::{MenuOf, Menus}, + snapshot::Snapshot, +}; + +#[derive(IntoElement)] +pub(crate) struct Tray { + snapshot: Rc, + catalog: Arc, + commands: Commands, +} + +impl Tray { + pub(crate) fn new(snapshot: Rc, catalog: Arc, commands: Commands) -> Self { + Self { + snapshot, + catalog, + commands, + } + } + + /// A contribution that waits out of the bar: a click puts it back where its hints say, a + /// drag puts it anywhere. + fn entry(&self, contribution: ContributionRef<'_>, menus: &Menus, cx: &App) -> AnyElement { + let id = contribution.id().clone(); + let subject = Subject::Contribution(id.clone()); + let disabled = self.snapshot.is_disabled(&id, &self.catalog); + let editable = self.snapshot.editable(); + let add = self.commands.clone(); + let theme = cx.theme(); + let name: SharedString = if disabled { + format!("{} (disabled)", contribution.title()).into() + } else { + contribution.title().to_owned().into() + }; + let title = SharedString::from(contribution.title().to_owned()); + let face = Face::new(contribution_icon(contribution.icon()), Some(title), false); + let chip = face + .clone() + .fill(h_flex(), cx) + .id(SharedString::from(format!("layout-tray:{id}"))) + .aria_label(name) + .test_support() + .bg(theme.secondary) + .when(editable, |chip| { + chip.cursor_pointer() + .hover(|style| style.bg(theme.secondary_active)) + .on_click(move |_, window, cx| { + // A release that ends a drag is the drop, not a click. + if cx.has_active_drag() { + return; + } + let id = id.clone(); + add.run(window, cx, move |section, _, cx| { + section.edit(EditOp::Restore { id }, cx); + }); + }) + }) + .when(disabled, |chip| chip.opacity(0.5)); + let chip = if editable { + let source = DragSource::default(); + let measure = source.clone(); + let chip = dnd::measured(chip, BORDER, move |bounds, window, _| { + measure.capture(bounds, window); + }); + let dragged = Dragged::contribution(contribution, None); + dnd::source(chip, dragged, source, move || Look::Chip(face.clone())) + } else { + chip + }; + let (menus, of) = (menus.clone(), MenuOf::Item(subject)); + chip.context_menu(move |popup, window, cx| menus.popup(&of, popup, window, cx)) + .into_any_element() + } + + fn unknown(&self, id: &ContributionId, cx: &App) -> AnyElement { + let (commands, forget) = (self.commands.clone(), id.clone()); + let text = format!("Not installed: {id}"); + let theme = cx.theme(); + h_flex() + .id(SharedString::from(format!("layout-unknown:{id}"))) + .aria_label(text.clone()) + .test_support() + .flex_none() + .h(px(CHIP_HEIGHT)) + .gap_1() + .pl_1p5() + .items_center() + .rounded(theme.radius) + .border_1() + .border_dashed() + .border_color(theme.border) + .text_xs() + .text_color(theme.muted_foreground) + .child(text) + .child( + Button::new(SharedString::from(format!("layout-forget:{id}"))) + .xsmall() + .ghost() + .icon(IconName::Close) + .tooltip("Forget") + .disabled(!self.snapshot.editable()) + .on_click(move |_, window, cx| { + let id = forget.clone(); + commands.run(window, cx, move |section, _, cx| { + section.edit(EditOp::Forget { id }, cx); + }); + }), + ) + .into_any_element() + } +} + +impl RenderOnce for Tray { + fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement { + let menus = Menus::new( + self.snapshot.clone(), + self.catalog.clone(), + self.commands.clone(), + ); + let entries: Vec = self + .snapshot + .tray() + .iter() + .filter_map(|id| self.catalog.contribution(id)) + .map(|contribution| self.entry(contribution, &menus, cx)) + .chain( + self.snapshot + .unknown() + .iter() + .map(|id| self.unknown(id, cx)), + ) + .collect(); + let theme = cx.theme(); + let tray = h_flex() + .id("layout-tray") + .test_support() + .w_full() + .flex_wrap() + .gap_1p5() + .items_center() + .min_h(px(CHIP_HEIGHT + 12.)) + .px_2() + .py_1() + .rounded(theme.radius_lg) + .border_1() + .border_dashed() + .border_color(theme.border) + .child( + div() + .text_xs() + .text_color(theme.muted_foreground) + .child("Not in the bar"), + ) + .children(entries); + if self.snapshot.editable() { + dnd::tray(tray, self.commands).into_any_element() + } else { + tray.into_any_element() + } + } +} diff --git a/crates/layout-ui/src/user_facts.rs b/crates/layout-ui/src/user_facts.rs new file mode 100644 index 00000000..ecbe4f23 --- /dev/null +++ b/crates/layout-ui/src/user_facts.rs @@ -0,0 +1,26 @@ +//! What the host knows about an installed user plugin, which the tooltip of its items shows: +//! the facts are the host's own, never taken from the plugin's text. + +use std::rc::Rc; + +use gpui_kit::App; +use nocterm_plugin_model::PluginId; + +/// The facts of one user plugin, worded by the host. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct UserFacts { + /// Who published it, as the package says. Unverified. + pub publisher: String, + /// The short form of the package hash the user compared at install. + pub fingerprint: String, + /// What the user allowed the plugin to do, in the host's words. + pub capabilities: Vec, +} + +/// Where the editor reads the facts of user plugins from. +pub trait UserPluginFacts: 'static { + /// The facts of `plugin`, if it is installed. + fn facts(&self, plugin: &PluginId, cx: &App) -> Option; +} + +pub(crate) type SharedFacts = Rc; diff --git a/crates/local/Cargo.toml b/crates/local/Cargo.toml index 79f83fba..4a800e52 100644 --- a/crates/local/Cargo.toml +++ b/crates/local/Cargo.toml @@ -19,12 +19,20 @@ futures.workspace = true tracing.workspace = true tempfile.workspace = true thiserror.workspace = true +chrono = { version = "0.4", default-features = false, features = ["clock"] } [target.'cfg(unix)'.dependencies] -nix = { version = "0.28", features = ["signal", "fs", "event"] } +nix = { version = "0.28", features = ["signal", "fs", "event", "poll"] } + +[target.'cfg(target_os = "linux")'.dependencies] +filedescriptor = "0.8.3" +rustix = { version = "1.1.5", features = ["process"] } [target.'cfg(windows)'.dependencies] process-wrap = { version = "9.1.0", default-features = false, features = ["std", "job-object", "creation-flags"] } +[dev-dependencies] +nocterm-session = {workspace=true,features=["test-support"]} + [lints] workspace = true diff --git a/crates/local/src/clock.rs b/crates/local/src/clock.rs new file mode 100644 index 00000000..5757716c --- /dev/null +++ b/crates/local/src/clock.rs @@ -0,0 +1,22 @@ +//! The local wall clock, read from the operating system. + +use chrono::{Local, Offset as _}; +use nocterm_core::clock::{WallClock, WallTime}; + +/// Reads the system time and the local time zone, so the zone is the one in effect at that +/// instant (daylight saving included). A zone the system cannot report is UTC. +#[derive(Clone, Copy, Debug, Default)] +pub struct SystemClock; + +impl WallClock for SystemClock { + fn now(&self) -> WallTime { + let now = Local::now(); + WallTime { + unix_seconds: now.timestamp(), + utc_offset_seconds: now.offset().fix().local_minus_utc(), + } + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/local/src/clock/tests.rs b/crates/local/src/clock/tests.rs new file mode 100644 index 00000000..217b32c3 --- /dev/null +++ b/crates/local/src/clock/tests.rs @@ -0,0 +1,35 @@ +use std::time::{SystemTime, UNIX_EPOCH}; + +use super::*; + +const LARGEST_OFFSET: i32 = 14 * 3600; + +#[test] +fn the_offset_is_a_real_time_zone() { + let time = SystemClock.now(); + assert!( + (-LARGEST_OFFSET..=LARGEST_OFFSET).contains(&time.utc_offset_seconds), + "offset {}", + time.utc_offset_seconds + ); +} + +#[test] +fn the_time_follows_the_system_clock() { + let before = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() as i64; + let time = SystemClock.now(); + let after = SystemTime::now() + .duration_since(UNIX_EPOCH) + .unwrap() + .as_secs() as i64; + assert!((before..=after + 1).contains(&time.unix_seconds)); +} + +#[test] +fn the_local_time_is_of_the_day() { + let (hour, minute) = SystemClock.now().local_hour_minute(); + assert!(hour < 24 && minute < 60); +} diff --git a/crates/local/src/exec.rs b/crates/local/src/exec.rs index 082c1879..55f5e3ae 100644 --- a/crates/local/src/exec.rs +++ b/crates/local/src/exec.rs @@ -17,68 +17,126 @@ use nocterm_session::{ ShellLaunch, TerminalRequest, }; +mod admission; mod process; +use admission::{Admission, Permit}; #[cfg(all(test, unix))] mod tests; const READ_BUFFER: usize = 16 * 1024; -const EXIT_POLL: Duration = Duration::from_millis(10); +#[cfg(not(unix))] +const EXIT_POLL: Duration = Duration::from_millis(100); const ERROR_GRACE: Duration = Duration::from_millis(200); -#[derive(Clone, Copy, Debug, Default)] -pub struct LocalExec; +/// One explicitly shared native-command admission owner. +#[derive(Clone, Debug, Default)] +pub struct LocalExec { + admission: Arc, +} +pub(crate) struct ScopedLocalExec { + source: nocterm_session::ConnectionSource, + executor: LocalExec, +} +impl ScopedLocalExec { + pub(crate) fn new(source: nocterm_session::ConnectionSource) -> Self { + Self { + source, + executor: LocalExec::default(), + } + } +} impl HostExec for LocalExec { fn exec(&self, request: ExecRequest) -> ExecFuture { - Box::pin(async move { - let (ready, started) = oneshot::channel(); - spawn("nocterm-exec-start", move || { - if !ready.is_canceled() { - // If the future was dropped during spawn, sending drops - // the output and the supervisor immediately cancels it. - let _ = ready.send(start(request)); - } - })?; - started - .await - .map_err(|error| ExecError::Failed(error.to_string()))? - }) + execute(request, Default::default(), self.admission.clone()) } - fn terminal(&self, request: TerminalRequest) -> Session { - let launch = ShellLaunch { - program: Some(request.program.program), - args: request.program.args, - integration: false, - ..ShellLaunch::default() - }; - crate::open(launch, false, request.term, request.size) + terminal(request, Default::default()) } } +impl HostExec for ScopedLocalExec { + fn exec(&self, request: ExecRequest) -> ExecFuture { + execute( + request, + self.source.clone(), + self.executor.admission.clone(), + ) + } + fn terminal(&self, request: TerminalRequest) -> Session { + terminal(request, self.source.clone()) + } +} +fn execute( + request: ExecRequest, + source: nocterm_session::ConnectionSource, + admission: Arc, +) -> ExecFuture { + source.clone().guard(async move { + let permit = admission.reserve()?; + let (ready, started) = oneshot::channel(); + spawn("nocterm-exec-start", move || { + if !ready.is_canceled() { + let _ = ready.send(start(request, source, permit)); + } + })?; + started + .await + .map_err(|error| ExecError::Failed(error.to_string()))? + }) +} +fn terminal(request: TerminalRequest, source: nocterm_session::ConnectionSource) -> Session { + let launch = ShellLaunch { + program: Some(request.program.program), + args: request.program.args, + integration: false, + ..ShellLaunch::default() + }; + crate::open_scoped(launch, false, request.term, request.size, source) +} -fn start(request: ExecRequest) -> Result { - let mut process = process::Process::spawn(&request)?; +fn start( + request: ExecRequest, + source: nocterm_session::ConnectionSource, + permit: Arc, +) -> Result { + source.check()?; + let mut process = process::Process::spawn(&request, &source)?; let (Some(stdout), Some(stderr)) = (process.stdout(), process.stderr()) else { return Err(ExecError::Failed("no output pipe".into())); }; + let stopped = Arc::new(AtomicBool::new(false)); if let (Some(input), Some(stdin)) = (request.stdin, process.stdin()) { - write_input(stdin, input)?; + write_input( + stdin, + input, + source.clone(), + stopped.clone(), + permit.clone(), + ) + .inspect_err(|_| stopped.store(true, Ordering::Release))?; } - let (sink, output) = ExecOutput::channel(); + let (sink, output) = ExecOutput::channel_with_source(source.clone()); let sink = Arc::new(sink); - let stopped = Arc::new(AtomicBool::new(false)); - let drained = read_output(stdout, sink.clone(), stopped.clone())?; - let errors = read_errors(stderr, stopped.clone())?; + let drained = read_output(stdout, sink.clone(), stopped.clone(), permit.clone()) + .inspect_err(|_| stopped.store(true, Ordering::Release))?; + let errors = read_errors(stderr, stopped.clone(), permit.clone()) + .inspect_err(|_| stopped.store(true, Ordering::Release))?; + let failed = stopped.clone(); spawn("nocterm-exec-wait", move || { // Observe the leader independently from pipe EOF: descendants may // retain both output pipes after the leader has exited. + let _permit = permit; + let mut pause = Duration::from_millis(1); let natural = loop { - if sink.is_closed() { + if sink.is_closed() || source.check().is_err() { break false; } match process.exited() { Ok(true) => break true, - Ok(false) => thread::sleep(EXIT_POLL), + Ok(false) => { + thread::sleep(pause); + pause = (pause * 2).min(Duration::from_millis(100)); + } Err(_) => break false, } }; @@ -92,13 +150,34 @@ fn start(request: ExecRequest) -> Result { }); let tail = errors.recv_timeout(ERROR_GRACE).unwrap_or_default(); sink.finish(tail.exit(status)); - })?; + }) + .inspect_err(|_| failed.store(true, Ordering::Release))?; Ok(output) } -fn write_input(mut stdin: ChildStdin, input: Vec) -> Result<(), ExecError> { +fn write_input( + mut stdin: ChildStdin, + input: Vec, + source: nocterm_session::ConnectionSource, + stopped: Arc, + permit: Arc, +) -> Result<(), ExecError> { + #[cfg(unix)] + process::nonblocking(&stdin)?; spawn("nocterm-exec-in", move || { - let _ = stdin.write_all(&input); + let _permit = permit; + let mut remaining = input.as_slice(); + while !remaining.is_empty() && source.check().is_ok() && !stopped.load(Ordering::Acquire) { + match stdin.write(&remaining[..remaining.len().min(1024)]) { + Ok(0) => break, + Ok(n) => remaining = &remaining[n..], + Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { + wait_writable(&stdin) + } + Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {} + Err(_) => break, + } + } }) } @@ -106,11 +185,13 @@ fn read_output( mut stdout: ChildStdout, sink: Arc, stopped: Arc, + permit: Arc, ) -> Result, ExecError> { #[cfg(unix)] process::nonblocking(&stdout)?; let (finished, drained) = oneshot::channel(); spawn("nocterm-exec-out", move || { + let _permit = permit; let mut buffer = vec![0; READ_BUFFER]; loop { match stdout.read(&mut buffer) { @@ -125,7 +206,7 @@ fn read_output( if stopped.load(Ordering::Acquire) || sink.is_closed() { break; } - thread::sleep(EXIT_POLL); + wait_readable(&stdout); } Err(_) => break, } @@ -138,11 +219,13 @@ fn read_output( fn read_errors( mut stderr: ChildStderr, stopped: Arc, + permit: Arc, ) -> Result, ExecError> { #[cfg(unix)] process::nonblocking(&stderr)?; let (send, errors) = std::sync::mpsc::channel(); spawn("nocterm-exec-err", move || { + let _permit = permit; let mut tail = ErrorTail::default(); let mut buffer = [0; 1024]; loop { @@ -154,7 +237,7 @@ fn read_errors( if stopped.load(Ordering::Acquire) { break; } - thread::sleep(EXIT_POLL); + wait_readable(&stderr); } Err(_) => break, } @@ -171,3 +254,25 @@ fn spawn(name: &str, body: impl FnOnce() + Send + 'static) -> Result<(), ExecErr .map(drop) .map_err(|error| ExecError::Failed(error.to_string())) } + +#[cfg(unix)] +fn wait_readable(pipe: &impl std::os::fd::AsFd) { + wait_pipe(pipe, nix::poll::PollFlags::POLLIN); +} +#[cfg(unix)] +fn wait_writable(pipe: &impl std::os::fd::AsFd) { + wait_pipe(pipe, nix::poll::PollFlags::POLLOUT); +} +#[cfg(unix)] +fn wait_pipe(pipe: &impl std::os::fd::AsFd, events: nix::poll::PollFlags) { + let mut fds = [nix::poll::PollFd::new(pipe.as_fd(), events)]; + let _ = nix::poll::poll(&mut fds, 100u16); +} +#[cfg(not(unix))] +fn wait_readable(_: &T) { + thread::sleep(EXIT_POLL); +} +#[cfg(not(unix))] +fn wait_writable(_: &T) { + thread::sleep(EXIT_POLL); +} diff --git a/crates/local/src/exec/admission.rs b/crates/local/src/exec/admission.rs new file mode 100644 index 00000000..8b989beb --- /dev/null +++ b/crates/local/src/exec/admission.rs @@ -0,0 +1,32 @@ +//! The original executor owns a finite native command budget through actual cleanup. +use nocterm_session::ExecError; +use std::sync::{ + Arc, + atomic::{AtomicUsize, Ordering}, +}; + +const MAX_COMMANDS: usize = 32; +#[derive(Debug, Default)] +pub(super) struct Admission { + active: AtomicUsize, +} +impl Admission { + pub(super) fn reserve(self: &Arc) -> Result, ExecError> { + self.active + .fetch_update(Ordering::AcqRel, Ordering::Acquire, |active| { + (active < MAX_COMMANDS).then_some(active + 1) + }) + .map_err(|_| { + ExecError::Failed( + "Local command capacity is full (32). Wait for a command to finish.".into(), + ) + })?; + Ok(Arc::new(Permit(self.clone()))) + } +} +pub(super) struct Permit(Arc); +impl Drop for Permit { + fn drop(&mut self) { + self.0.active.fetch_sub(1, Ordering::AcqRel); + } +} diff --git a/crates/local/src/exec/process.rs b/crates/local/src/exec/process.rs index 8f7c76f9..bdadcbfa 100644 --- a/crates/local/src/exec/process.rs +++ b/crates/local/src/exec/process.rs @@ -19,7 +19,10 @@ pub(super) struct Process { } impl Process { - pub(super) fn spawn(request: &ExecRequest) -> Result { + pub(super) fn spawn( + request: &ExecRequest, + source: &nocterm_session::ConnectionSource, + ) -> Result { let mut command = Command::new(&request.program); command .args(&request.args) @@ -30,6 +33,7 @@ impl Process { }) .stdout(Stdio::piped()) .stderr(Stdio::piped()); + source.check()?; #[cfg(unix)] let spawned = { use std::os::unix::process::CommandExt as _; diff --git a/crates/local/src/exec/tests.rs b/crates/local/src/exec/tests.rs index e06c1d52..63ac7a42 100644 --- a/crates/local/src/exec/tests.rs +++ b/crates/local/src/exec/tests.rs @@ -17,7 +17,7 @@ fn runs_a_script_from_its_input() { let request = ExecRequest::new("sh") .arg("-s") .stdin("echo one; echo \"$((40 + 2))\"\n"); - let output = block_on(LocalExec.exec(request)).unwrap(); + let output = block_on(LocalExec::default().exec(request)).unwrap(); assert_eq!(collect(output), "one\n42\n"); } @@ -30,7 +30,7 @@ fn dropping_the_output_stops_the_program_and_its_children() { file.display() ); let request = ExecRequest::new("sh").arg("-c").arg(script); - let mut output = block_on(LocalExec.exec(request)).unwrap(); + let mut output = block_on(LocalExec::default().exec(request)).unwrap(); assert_eq!(block_on(output.next()).as_deref(), Some(&b"started\n"[..])); drop(output); thread::sleep(Duration::from_millis(600)); @@ -42,18 +42,22 @@ fn programs_report_their_exit_and_errors() { let request = ExecRequest::new("sh") .arg("-c") .arg("echo out; echo 'no such container' >&2; exit 3"); - let collected = block_on(block_on(LocalExec.exec(request)).unwrap().collect(1024)); + let collected = block_on( + block_on(LocalExec::default().exec(request)) + .unwrap() + .collect(1024), + ); assert_eq!(collected.text(), "out\n"); assert_eq!(collected.exit.status, Some(3)); assert_eq!(collected.exit.error(), "no such container"); - let fine = block_on(LocalExec.exec(ExecRequest::new("true"))).unwrap(); + let fine = block_on(LocalExec::default().exec(ExecRequest::new("true"))).unwrap(); assert!(block_on(fine.exit()).success()); } #[test] fn programs_run_on_a_terminal_of_their_own() { use nocterm_session::{CloseReason, Event, PtySize}; - let session = LocalExec.terminal(TerminalRequest { + let session = LocalExec::default().terminal(TerminalRequest { program: ExecRequest::new("sh") .arg("-c") .arg("test -t 1 && echo on-a-tty; exit 4"), @@ -78,7 +82,8 @@ fn programs_run_on_a_terminal_of_their_own() { #[test] fn a_missing_program_is_an_error() { let started = Instant::now(); - let error = block_on(LocalExec.exec(ExecRequest::new("/nonexistent/nocterm"))).unwrap_err(); + let error = + block_on(LocalExec::default().exec(ExecRequest::new("/nonexistent/nocterm"))).unwrap_err(); assert_eq!(error, ExecError::NotFound("/nonexistent/nocterm".into())); assert!(started.elapsed() < Duration::from_secs(5)); } @@ -88,7 +93,7 @@ fn constructing_or_dropping_an_unpolled_exec_never_spawns() { let directory = tempfile::tempdir().unwrap(); let marker = directory.path().join("unexpected"); let request = ExecRequest::new("touch").arg(marker.to_string_lossy().to_string()); - let future = LocalExec.exec(request); + let future = LocalExec::default().exec(request); thread::sleep(Duration::from_millis(50)); assert!(!marker.exists()); drop(future); @@ -105,7 +110,11 @@ fn exited_leader_does_not_leave_descendants_or_hold_stdout_open() { marker.display() )); let started = Instant::now(); - let collected = block_on(block_on(LocalExec.exec(request)).unwrap().collect(1024)); + let collected = block_on( + block_on(LocalExec::default().exec(request)) + .unwrap() + .collect(1024), + ); assert_eq!(collected.text(), "started\n"); assert_eq!(collected.exit.status, Some(7)); assert!(started.elapsed() < Duration::from_millis(900)); @@ -121,7 +130,7 @@ fn cancellation_after_parent_exit_kills_a_child_that_closed_its_output() { "echo started; (exec >/dev/null 2>&1; sleep 0.3; touch '{}') & exit", marker.display() )); - let output = block_on(LocalExec.exec(request)).unwrap(); + let output = block_on(LocalExec::default().exec(request)).unwrap(); thread::sleep(Duration::from_millis(100)); drop(output); thread::sleep(Duration::from_millis(500)); @@ -134,7 +143,7 @@ fn exit_observation_does_not_discard_buffered_output() { .arg("-c") .arg("i=0; while [ $i -lt 4096 ]; do printf '1234567890\\n'; i=$((i+1)); done; exit 9"); let collected = block_on( - block_on(LocalExec.exec(request)) + block_on(LocalExec::default().exec(request)) .unwrap() .collect(128 * 1024), ); @@ -149,7 +158,7 @@ fn dropping_the_startup_process_guard_stops_the_whole_group() { let request = ExecRequest::new("sh") .arg("-c") .arg(format!("(sleep 0.3; touch '{}') & wait", marker.display())); - let process = process::Process::spawn(&request).unwrap(); + let process = process::Process::spawn(&request, &Default::default()).unwrap(); thread::sleep(Duration::from_millis(50)); // This is also the cleanup path when pipe/worker setup fails. drop(process); @@ -165,7 +174,7 @@ fn cancellation_interrupts_a_backpressured_output_worker_and_stops_descendants() "(sleep 1; touch '{}') & while :; do printf '%01024d\\n' 0; done", marker.display() )); - let mut output = block_on(LocalExec.exec(request)).unwrap(); + let mut output = block_on(LocalExec::default().exec(request)).unwrap(); assert!(block_on(output.next()).is_some()); // Leave stdout unread long enough to fill the bounded ExecOutput queue. thread::sleep(Duration::from_millis(100)); @@ -176,3 +185,31 @@ fn cancellation_interrupts_a_backpressured_output_worker_and_stops_descendants() "cancellation must bypass output backpressure" ); } + +#[test] +fn cloned_executors_share_the_native_command_capacity_and_release_it_after_cleanup() { + let exec = LocalExec::default(); + let clone = exec.clone(); + let mut outputs = Vec::new(); + for _ in 0..32 { + let mut output = + block_on(exec.exec(ExecRequest::new("sh").args(["-c", "echo ready; sleep 60"]))) + .unwrap(); + assert!(block_on(output.next()).is_some()); + outputs.push(output); + } + assert!(matches!( + block_on(clone.exec(ExecRequest::new("true"))), + Err(ExecError::Failed(_)) + )); + drop(outputs.pop()); + let deadline = Instant::now() + Duration::from_secs(5); + let output = loop { + match block_on(clone.exec(ExecRequest::new("true"))) { + Ok(output) => break output, + Err(_) if Instant::now() < deadline => thread::sleep(Duration::from_millis(10)), + error => panic!("command capacity did not recover: {error:?}"), + } + }; + assert!(block_on(output.exit()).success()); +} diff --git a/crates/local/src/integration.rs b/crates/local/src/integration.rs new file mode 100644 index 00000000..95bcb9bd --- /dev/null +++ b/crates/local/src/integration.rs @@ -0,0 +1,22 @@ +//! The same bundled integration programs are used by ordinary and contained shells. +pub(super) fn bundled(stem: &str) -> Option<(&'static str, &'static str)> { + Some(match stem { + "bash" => ( + "bashrc", + include_str!("../../../assets/shell-integration/bash.bash"), + ), + "zsh" => ( + ".zshrc", + include_str!("../../../assets/shell-integration/zsh.zsh"), + ), + "fish" => ( + "integration.fish", + include_str!("../../../assets/shell-integration/fish.fish"), + ), + "pwsh" | "powershell" => ( + "integration.ps1", + include_str!("../../../assets/shell-integration/powershell.ps1"), + ), + _ => return None, + }) +} diff --git a/crates/local/src/lib.rs b/crates/local/src/lib.rs index 0385356e..cc295fa5 100644 --- a/crates/local/src/lib.rs +++ b/crates/local/src/lib.rs @@ -1,9 +1,13 @@ -//! Native local PTY, structured commands and detached external editors. -//! Owns: native launch, bounded I/O, exclusive pinned output logs and process ownership. +//! Native local PTY, structured commands, detached external editors and the local wall clock. +//! Owns: native launch, bounded I/O, exclusive pinned output logs, process ownership and +//! reading the system time and time zone. //! Does not own: session contracts, filesystem traversal, editor selection or views. //! Ports used: session channels. Ports provided: Transport, HostExec, ProgramOpener, -//! recording::OutputLogFactory. Output-log operations run only on their caller executor. +//! recording::OutputLogFactory, nocterm_core::clock::WallClock (`SystemClock`). Output-log +//! operations run only on their caller executor. //! Settings section: local options are composed by the app. Globals: none. +//! Linux PTY reads wait for descriptor readiness, owner closure or the owned leader's pidfd; +//! kernels denying pidfds and other platforms retain bounded process-status polling. //! Invariants: PTY/exec cancellation owns process cleanup; external editors instead //! retain a bounded independent wait owner and survive receipt/application exit. @@ -21,14 +25,21 @@ use std::{ use futures::{FutureExt, executor::block_on, select}; use nocterm_session::{ CloseReason, Command, ConnectRequest, Event, PtySize, Session, SessionError, ShellLaunch, - Transport, channel, + Transport, }; use portable_pty::{CommandBuilder, PtySize as NativeSize, native_pty_system}; +mod clock; +#[cfg(target_os = "linux")] mod exec; +mod integration; mod program_opener; +#[cfg(target_os = "linux")] +mod readiness; mod recording; +pub use clock::SystemClock; +#[cfg(target_os = "linux")] pub use exec::LocalExec; pub use program_opener::{LocalProgramOpener, MAX_OPEN_PROGRAMS}; pub use recording::LocalOutputLogFactory; @@ -37,10 +48,59 @@ pub use recording::LocalOutputLogFactory; #[derive(Clone, Debug)] pub struct LocalTransport(pub ShellLaunch); +impl LocalTransport { + /// Carries native launch preparation to the existing startup worker. + pub fn prepared( + launch: ShellLaunch, + preparation: Option>, + ) -> Arc { + Arc::new(PreparedLocalTransport { + launch, + preparation, + }) + } +} +struct PreparedLocalTransport { + launch: ShellLaunch, + preparation: Option>, +} +impl Transport for PreparedLocalTransport { + fn open(&self, request: ConnectRequest) -> Session { + self.open_with(request, Default::default()) + } + fn open_with( + &self, + request: ConnectRequest, + options: nocterm_session::ConnectionOpenOptions, + ) -> Session { + open_prepared( + self.launch.clone(), + false, + request.term, + request.size, + options.source().clone(), + self.preparation.clone(), + ) + } +} + impl Transport for LocalTransport { fn open(&self, request: ConnectRequest) -> Session { open(self.0.clone(), false, request.term, request.size) } + fn open_with( + &self, + request: ConnectRequest, + options: nocterm_session::ConnectionOpenOptions, + ) -> Session { + open_scoped( + self.0.clone(), + false, + request.term, + request.size, + options.source().clone(), + ) + } } /// A command PTY with the host environment and platform PTY metadata only. @@ -51,22 +111,83 @@ impl Transport for IsolatedLocalTransport { fn open(&self, request: ConnectRequest) -> Session { open(self.0.clone(), true, request.term, request.size) } + fn open_with( + &self, + request: ConnectRequest, + options: nocterm_session::ConnectionOpenOptions, + ) -> Session { + open_scoped( + self.0.clone(), + true, + request.term, + request.size, + options.source().clone(), + ) + } } /// Starts `launch` on a new PTY, on a thread of its own. fn open(launch: ShellLaunch, isolated: bool, term: String, pty: PtySize) -> Session { + open_scoped(launch, isolated, term, pty, Default::default()) +} +fn open_scoped( + launch: ShellLaunch, + isolated: bool, + term: String, + pty: PtySize, + source: nocterm_session::ConnectionSource, +) -> Session { + open_prepared(launch, isolated, term, pty, source, None) +} + +fn open_prepared( + launch: ShellLaunch, + isolated: bool, + term: String, + pty: PtySize, + source: nocterm_session::ConnectionSource, + preparation: Option>, +) -> Session { + if let Err(error) = source.check() { + return Session::failed(error.into()); + } if let Err(error) = launch.validate() { return Session::failed(SessionError::InvalidLaunch(error)); } - let (session, driver) = channel(None); - let session = session.with_exec(Arc::new(LocalExec)); + let (session, driver) = nocterm_session::channel_with_source(None, source.clone()); + let session = session.with_exec(Arc::new(exec::ScopedLocalExec::new(source))); thread::spawn(move || { + if driver.closed().now_or_never().is_some() { + block_on(driver.emit(Event::Closed(CloseReason::ClosedByUser))); + return; + } + let prepared = driver + .source() + .check() + .map_err(SessionError::from) + .and_then(|()| { + let launch = match preparation { + Some(preparation) => preparation + .prepare(launch) + .map_err(SessionError::InvalidLaunch)?, + None => launch, + }; + launch.validate().map_err(SessionError::InvalidLaunch)?; + Ok(launch) + }); + let launch = match prepared { + Ok(launch) => launch, + Err(error) => { + block_on(driver.emit(Event::Closed(CloseReason::Failed(error)))); + return; + } + }; if let Err(error) = start(launch, isolated, term, pty, driver.clone()) { - block_on( - driver.emit(Event::Closed(CloseReason::Failed(SessionError::Other( - error.to_string(), - )))), - ); + let error = match error { + LaunchError::Source(error) => SessionError::Source(error), + other => SessionError::Other(other.to_string()), + }; + block_on(driver.emit(Event::Closed(CloseReason::Failed(error)))); } }); session @@ -83,6 +204,8 @@ fn size(s: PtySize) -> NativeSize { #[derive(Debug, thiserror::Error)] enum LaunchError { + #[error(transparent)] + Source(#[from] nocterm_session::ConnectionSourceError), #[error("native PTY error: {0}")] Pty(String), #[error("native PTY has no file descriptor")] @@ -91,6 +214,9 @@ enum LaunchError { Fcntl(String), #[error("I/O error: {0}")] Io(#[from] std::io::Error), + #[cfg(target_os = "linux")] + #[error(transparent)] + Descriptor(#[from] filedescriptor::Error), } #[expect(clippy::too_many_lines, reason = "predates the limit")] @@ -101,6 +227,7 @@ fn start( pty: PtySize, driver: nocterm_session::SessionDriver, ) -> Result<(), LaunchError> { + driver.source().check()?; let pair = native_pty_system() .openpty(size(pty)) .map_err(|e| LaunchError::Pty(e.to_string()))?; @@ -134,6 +261,13 @@ fn start( .master .take_writer() .map_err(|e| LaunchError::Pty(e.to_string()))?; + #[cfg(target_os = "linux")] + let (mut readiness, close_wake) = readiness::Readiness::new(pair.master.as_ref())?; + driver.source().check()?; + if driver.closed().now_or_never().is_some() { + block_on(driver.emit(Event::Closed(CloseReason::ClosedByUser))); + return Ok(()); + } let mut child = pair .slave .spawn_command(command) @@ -148,6 +282,13 @@ fn start( let shell_group = child_pid.map(|pid| pid as i32); #[cfg(not(unix))] let shell_group = None; + #[cfg(target_os = "linux")] + if let Err(error) = readiness.observe_exit(child.process_id()) { + let mut failed_killer = child.clone_killer(); + kill_process(failed_killer.as_mut(), &master, shell_group); + let _ = child.wait(); + return Err(error.into()); + } let (done_tx, done_rx) = async_channel::bounded::<()>(1); let command_driver = driver.clone(); let closed_by_user = Arc::new(AtomicBool::new(false)); @@ -158,6 +299,10 @@ fn start( let close_master = master.clone(); let command_master = master.clone(); thread::spawn(move || { + // Dropping this pipe wakes a readiness wait even when a descendant + // still holds the slave and killing the shell produces no PTY EOF. + #[cfg(target_os = "linux")] + let _wake = close_wake; let requested = block_on(async { select! { _ = close_driver.closed().fuse() => true, @@ -190,6 +335,9 @@ fn start( } } Some(Command::Resize(s)) => { + if command_driver.source().check().is_err() { + break; + } let _ = command_master .lock() .unwrap_or_else(|e| e.into_inner()) @@ -208,6 +356,8 @@ fn start( kill_process(disconnected_killer.as_mut(), &master, shell_group); } let mut buffer = [0; 8192]; + #[cfg(target_os = "linux")] + let mut read_failure = None; loop { // Closing is independent of slave EOF: a disowned/HUP-ignoring // background job can legitimately retain the terminal descriptor. @@ -225,6 +375,14 @@ fn start( done_tx.close(); break; } + #[cfg(target_os = "linux")] + if let Err(error) = readiness.wait() { + read_failure = Some(error); + let mut failed_killer = child.clone_killer(); + kill_process(failed_killer.as_mut(), &master, shell_group); + break; + } + #[cfg(not(target_os = "linux"))] thread::sleep(Duration::from_millis(10)); } Err(error) if error.kind() == std::io::ErrorKind::Interrupted => {} @@ -251,6 +409,10 @@ fn start( done_tx.close(); // Keep ephemeral scripts alive until the shell has exited. drop(integration); + #[cfg(target_os = "linux")] + if let Some(error) = read_failure { + return Err(error.into()); + } block_on( driver.emit(Event::Closed(if closed_by_user.load(Ordering::Acquire) { CloseReason::ClosedByUser @@ -288,6 +450,7 @@ fn write_input( "PTY closed", )); } + driver.source().check().map_err(std::io::Error::other)?; match writer.write(&bytes[offset..]) { Ok(0) => return Err(std::io::ErrorKind::WriteZero.into()), Ok(count) => offset += count, @@ -305,6 +468,7 @@ fn write_input( "PTY closed", )); } + driver.source().check().map_err(std::io::Error::other)?; match writer.flush() { Err(error) if error.kind() == std::io::ErrorKind::WouldBlock => { thread::sleep(Duration::from_millis(10)) @@ -365,24 +529,8 @@ fn prepare_integration( .file_stem() .and_then(|s| s.to_str()) .unwrap_or(""); - let (name, script) = match stem { - "bash" => ( - "bashrc", - include_str!("../../../assets/shell-integration/bash.bash"), - ), - "zsh" => ( - ".zshrc", - include_str!("../../../assets/shell-integration/zsh.zsh"), - ), - "fish" => ( - "integration.fish", - include_str!("../../../assets/shell-integration/fish.fish"), - ), - "pwsh" | "powershell" => ( - "integration.ps1", - include_str!("../../../assets/shell-integration/powershell.ps1"), - ), - _ => return Ok(None), + let Some((name, script)) = integration::bundled(stem) else { + return Ok(None); }; let directory = tempfile::tempdir()?; let path = directory.path().join(name); diff --git a/crates/local/src/readiness.rs b/crates/local/src/readiness.rs new file mode 100644 index 00000000..91f82732 --- /dev/null +++ b/crates/local/src/readiness.rs @@ -0,0 +1,86 @@ +//! Native PTY readiness, with separate wakeups for owner close and leader exit. +//! +//! Linux pidfds let an idle terminal sleep without missing a shell that exits +//! while a detached descendant retains its slave. Older kernels and sandboxes +//! denying pidfds retain the bounded process-status fallback. +use crate::LaunchError; +use filedescriptor::FileDescriptor; +use nix::{ + errno::Errno, + fcntl::OFlag, + poll::{PollFd, PollFlags, PollTimeout, poll}, + unistd::pipe2, +}; +use std::{ + io, + os::fd::{AsFd as _, AsRawFd, OwnedFd}, +}; + +pub(super) struct Readiness { + pty: FileDescriptor, + closing: OwnedFd, + leader: Option, +} + +/// The borrow keeps the native master alive through safe descriptor duplication. +struct Master<'a>(&'a dyn portable_pty::MasterPty); +impl AsRawFd for Master<'_> { + fn as_raw_fd(&self) -> std::os::fd::RawFd { + self.0.as_raw_fd().unwrap_or(-1) + } +} + +impl Readiness { + pub(super) fn new( + master: &dyn portable_pty::MasterPty, + ) -> Result<(Self, OwnedFd), LaunchError> { + let pty = FileDescriptor::dup(&Master(master))?; + let (closing, wake) = pipe2(OFlag::O_CLOEXEC).map_err(io::Error::from)?; + Ok(( + Self { + pty, + closing, + leader: None, + }, + wake, + )) + } + + pub(super) fn observe_exit(&mut self, pid: Option) -> io::Result<()> { + use rustix::{ + io::Errno, + process::{Pid, PidfdFlags, pidfd_open}, + }; + let Some(pid) = pid + .and_then(|pid| i32::try_from(pid).ok()) + .and_then(Pid::from_raw) + else { + return Ok(()); + }; + self.leader = match pidfd_open(pid, PidfdFlags::empty()) { + Ok(fd) => Some(fd), + Err(Errno::NOSYS | Errno::PERM | Errno::ACCESS) => None, + Err(error) => return Err(error.into()), + }; + Ok(()) + } + + pub(super) fn wait(&self) -> io::Result<()> { + let mut descriptors = vec![ + PollFd::new(self.pty.as_fd(), PollFlags::POLLIN), + PollFd::new(self.closing.as_fd(), PollFlags::POLLIN), + ]; + if let Some(leader) = &self.leader { + descriptors.push(PollFd::new(leader.as_fd(), PollFlags::POLLIN)); + } + let timeout = if self.leader.is_some() { + PollTimeout::NONE + } else { + PollTimeout::from(10u16) + }; + match poll(&mut descriptors, timeout) { + Ok(_) | Err(Errno::EINTR) => Ok(()), + Err(error) => Err(error.into()), + } + } +} diff --git a/crates/local/src/tests.rs b/crates/local/src/tests.rs index a3d978b9..1c10fe00 100644 --- a/crates/local/src/tests.rs +++ b/crates/local/src/tests.rs @@ -2,7 +2,7 @@ #![cfg(unix)] use super::*; -fn request(launch: ShellLaunch) -> ConnectRequest { +pub(super) fn request(launch: ShellLaunch) -> ConnectRequest { ConnectRequest { launch, target: nocterm_session::Target::new("user", "localhost", 22), @@ -15,7 +15,7 @@ fn request(launch: ShellLaunch) -> ConnectRequest { jumps: Vec::new(), } } -fn events(session: &Session) -> std::sync::mpsc::Receiver { +pub(super) fn events(session: &Session) -> std::sync::mpsc::Receiver { let session = session.clone(); let (tx, rx) = std::sync::mpsc::channel(); thread::spawn(move || { @@ -30,10 +30,14 @@ fn events(session: &Session) -> std::sync::mpsc::Receiver { }); rx } -fn next(rx: &std::sync::mpsc::Receiver) -> Event { +pub(super) fn next(rx: &std::sync::mpsc::Receiver) -> Event { rx.recv_timeout(std::time::Duration::from_secs(10)) .expect("PTY event timed out") } + +#[cfg(target_os = "linux")] +mod leader_exit; + #[test] fn invalid_launch_reports_typed_failure_before_starting_a_pty() { let launch = ShellLaunch { @@ -382,7 +386,7 @@ fn close_interrupts_full_input_and_kills_shell_process_group() { ); // The direct child is reaped by this adapter. A killed grandchild is // adopted/reaped by init; Linux containers can leave an init-owned - // zombie briefly, but no descendant may remain executing. + // zombie or a dead (X) task briefly, but no descendant may remain executing. let descendant_alive = nix::sys::signal::kill(nix::unistd::Pid::from_raw(descendant), None).is_ok(); #[cfg(target_os = "linux")] @@ -390,9 +394,9 @@ fn close_interrupts_full_input_and_kills_shell_process_group() { let stat = std::fs::read_to_string(format!("/proc/{descendant}/stat")).unwrap_or_default(); assert!( stat.is_empty() - || stat - .rsplit_once(") ") - .is_some_and(|(_, fields)| fields.starts_with('Z')), + || stat.rsplit_once(") ").is_some_and(|(_, fields)| { + matches!(fields.split_ascii_whitespace().next(), Some("Z" | "X")) + }), "descendant still running: {stat}" ); } @@ -705,3 +709,44 @@ fn dropping_the_last_session_owner_reaps_shell_without_event_consumer() { thread::sleep(Duration::from_millis(5)); } } + +#[test] +fn a_revoked_source_never_starts_a_new_local_pty_or_exec() { + use nocterm_session::{ + ConnectionOpenOptions, ConnectionSourceError, testing::ScriptedConnectionSource, + }; + let source = ScriptedConnectionSource::default(); + source.revoke(); + let launch = ShellLaunch { + program: Some("/bin/sh".into()), + ..Default::default() + }; + let session = LocalTransport(launch.clone()) + .open_with(request(launch), ConnectionOpenOptions::new(source.source())); + assert!(matches!( + block_on(session.next_event()), + Some(Event::Closed(CloseReason::Failed(SessionError::Source( + ConnectionSourceError::Revoked + )))) + )); +} +#[test] +fn a_retained_local_executor_cannot_start_after_its_account_is_revoked() { + use nocterm_session::{ + ConnectionOpenOptions, ExecError, ExecRequest, testing::ScriptedConnectionSource, + }; + let source = ScriptedConnectionSource::default(); + let launch = ShellLaunch { + program: Some("/bin/sh".into()), + integration: false, + ..Default::default() + }; + let session = LocalTransport(launch.clone()) + .open_with(request(launch), ConnectionOpenOptions::new(source.source())); + let executor = session.exec().unwrap(); + let pending = executor.exec(ExecRequest::new("/bin/echo").arg("never")); + source.revoke(); + assert!(matches!(block_on(pending), Err(ExecError::Source(_)))); + assert!(session.exec().is_none()); + session.close(); +} diff --git a/crates/local/src/tests/leader_exit.rs b/crates/local/src/tests/leader_exit.rs new file mode 100644 index 00000000..fadb8828 --- /dev/null +++ b/crates/local/src/tests/leader_exit.rs @@ -0,0 +1,44 @@ +//! A child-exit wake is independent from slave EOF and preserves the leader's tail. +use super::*; +use std::time::Instant; + +#[test] +fn an_exited_leader_closes_after_its_tail_even_when_a_child_holds_the_slave() { + let directory = tempfile::tempdir().unwrap(); + let child_file = directory.path().join("child"); + let ready_file = directory.path().join("ready"); + let launch = ShellLaunch { + program: Some("/bin/sh".into()), + args: vec![ + "-c".into(), + "(trap '' HUP; printf ready > \"$2\"; exec sleep 30) & printf '%s\\n' \"$!\" > \"$1\"; while [ ! -e \"$2\" ]; do sleep 0.001; done; printf leader-tail; exit 7" + .into(), + "leader-test".into(), + child_file.to_string_lossy().into_owned(), + ready_file.to_string_lossy().into_owned(), + ], + integration: false, + ..Default::default() + }; + let session = LocalTransport(launch.clone()).open(request(launch)); + let events = events(&session); + let deadline = Instant::now() + Duration::from_secs(2); + let mut output = Vec::new(); + let reason = loop { + match events.recv_timeout(deadline.saturating_duration_since(Instant::now())) { + Ok(Event::Output(bytes)) => output.extend(bytes), + Ok(Event::Closed(reason)) => break Some(reason), + Ok(_) => {} + Err(_) => break None, + } + }; + // This intentionally orphaned descendant is owned by the fixture. The + // transport must finish before it is removed, without waiting for slave EOF. + if let Ok(pid) = std::fs::read_to_string(child_file) { + let pid = nix::unistd::Pid::from_raw(pid.trim().parse().unwrap()); + let _ = nix::sys::signal::kill(pid, nix::sys::signal::Signal::SIGKILL); + } + session.close(); + assert_eq!(reason, Some(CloseReason::Exited(Some(7)))); + assert_eq!(output, b"leader-tail"); +} diff --git a/crates/localfs/src/directory.rs b/crates/localfs/src/directory.rs index f63318fd..2418f54c 100644 --- a/crates/localfs/src/directory.rs +++ b/crates/localfs/src/directory.rs @@ -105,6 +105,17 @@ impl LocalDirectory for DirectoryCapability { fn open_source(&self, name: &OsStr) -> LocalResult> { crate::source::open_relative(self, name) } + fn remove_file(&self, name: &OsStr) -> LocalResult<()> { + check_name(name)?; + #[cfg(unix)] + { + unix::remove_file(self, name) + } + #[cfg(not(unix))] + { + portable::remove_file(self, name) + } + } fn stage(&self) -> LocalResult> { crate::stage::create(self.clone()) } diff --git a/crates/localfs/src/directory/portable.rs b/crates/localfs/src/directory/portable.rs index c2ad0b41..c85f1fba 100644 --- a/crates/localfs/src/directory/portable.rs +++ b/crates/localfs/src/directory/portable.rs @@ -72,3 +72,13 @@ pub(super) fn metadata( pub(super) fn target_is_directory(directory: &DirectoryCapability, name: &OsStr) -> bool { std::fs::metadata(directory.path.join(name)).is_ok_and(|metadata| metadata.is_dir()) } +pub(super) fn remove_file(directory: &DirectoryCapability, name: &OsStr) -> LocalResult<()> { + strict(directory.path())?; + let path = directory.path.join(name); + if metadata(directory, name)?.is_some_and(|metadata| { + metadata.kind == nocterm_session::EntryKind::Directory && !metadata.is_symlink + }) { + return Err(LocalFsError::NotRegular(path)); + } + std::fs::remove_file(&path).map_err(|error| LocalFsError::from_io(path, error)) +} diff --git a/crates/localfs/src/directory/unix.rs b/crates/localfs/src/directory/unix.rs index a9e5e68d..4beeae24 100644 --- a/crates/localfs/src/directory/unix.rs +++ b/crates/localfs/src/directory/unix.rs @@ -81,6 +81,16 @@ pub(super) fn target_is_directory(parent: &DirectoryCapability, name: &OsStr) -> fs::statat(parent.file(), name, AtFlags::empty()) .is_ok_and(|stat| FileType::from_raw_mode(stat.st_mode).is_dir()) } +pub(super) fn remove_file(parent: &DirectoryCapability, name: &OsStr) -> LocalResult<()> { + let path = parent.path.join(name); + if metadata(parent, name)?.is_some_and(|metadata| { + metadata.kind == nocterm_session::EntryKind::Directory && !metadata.is_symlink + }) { + return Err(LocalFsError::NotRegular(path)); + } + fs::unlinkat(parent.file(), name, AtFlags::empty()) + .map_err(|error| LocalFsError::from_io(path, error.into())) +} // libc typedef widths vary across supported Unix platforms. #[allow(clippy::unnecessary_cast)] fn from_stat(stat: fs::Stat) -> FileMetadata { diff --git a/crates/localfs/tests/capabilities.rs b/crates/localfs/tests/capabilities.rs index 951b3b95..9d1a31d4 100644 --- a/crates/localfs/tests/capabilities.rs +++ b/crates/localfs/tests/capabilities.rs @@ -4,6 +4,42 @@ use std::{ffi::OsStr, path::Path}; #[cfg(unix)] use std::{io::Write, time::Duration}; +#[cfg(unix)] +#[test] +fn held_file_removal_refuses_directories_and_unlinks_a_link_without_following_it() { + let root = tempfile::tempdir().unwrap(); + let outside = tempfile::tempdir().unwrap(); + let selected = root.path().join("selected"); + std::fs::create_dir(&selected).unwrap(); + std::fs::create_dir(selected.join("directory")).unwrap(); + std::fs::write(selected.join("owned"), b"owned").unwrap(); + std::fs::write(outside.path().join("secret"), b"secret").unwrap(); + std::os::unix::fs::symlink(outside.path().join("secret"), selected.join("link")).unwrap(); + let fs = LocalFilesystem::with_base(root.path().to_owned()).unwrap(); + let directory = fs.open_directory(&selected).unwrap(); + assert!(directory.remove_file(OsStr::new("directory")).is_err()); + for name in ["", ".", "..", "../secret", "a/b"] { + assert!(matches!( + directory.remove_file(OsStr::new(name)), + Err(LocalFsError::InvalidName) + )); + } + directory.remove_file(OsStr::new("link")).unwrap(); + assert_eq!( + std::fs::read(outside.path().join("secret")).unwrap(), + b"secret" + ); + std::fs::rename(&selected, root.path().join("moved")).unwrap(); + std::os::unix::fs::symlink(outside.path(), &selected).unwrap(); + directory.remove_file(OsStr::new("owned")).unwrap(); + assert!(!root.path().join("moved/owned").exists()); + assert!(root.path().join("moved/directory").is_dir()); + assert_eq!( + std::fs::read(outside.path().join("secret")).unwrap(), + b"secret" + ); +} + #[test] fn listing_and_sources_preserve_names_bounds_and_same_opened_metadata() { let root = tempfile::tempdir().unwrap(); diff --git a/crates/monitor-service/Cargo.toml b/crates/monitor-service/Cargo.toml new file mode 100644 index 00000000..ad1166fe --- /dev/null +++ b/crates/monitor-service/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "nocterm-monitor-service" +description = "Shared resource monitoring over captured host execution." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "service" + +[dependencies] +gpui.workspace = true +nocterm-monitor.workspace = true +nocterm-session.workspace = true + +[dev-dependencies] +gpui = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } +futures.workspace = true + +[lints] +workspace = true diff --git a/crates/monitor-service/src/lib.rs b/crates/monitor-service/src/lib.rs new file mode 100644 index 00000000..2e266fa8 --- /dev/null +++ b/crates/monitor-service/src/lib.rs @@ -0,0 +1,195 @@ +//! Raw resource collection shared by consumers of the same captured executor. +//! Owns: one watch per executor/scope, the metric union and fastest requested cadence, +//! typed watch status, one latest reading, and cancellation when the last consumer leaves. +//! Does not own: selected windows/surfaces, presentation histories, views or native processes. +//! Ports used: HostExec. Ports provided: MonitorWatches, MonitorWatch. Settings: none. Globals: none. +//! Invariants: descriptive host names never merge authority; subscriptions retain their original +//! executor; obsolete plans/results cannot update the current watch generation. +mod watch; +use gpui::{App, AppContext as _, Context, Entity, EntityId, Task, WeakEntity}; +use nocterm_monitor::{MetricSet, Platform, Reading}; +use nocterm_session::{ExecError, HostExec}; +use std::{ + cell::RefCell, + rc::Rc, + sync::{Arc, Weak}, + time::Duration, +}; + +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct WatchPlan { + pub metrics: MetricSet, + pub interval: Duration, +} +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum WatchStatus { + Off, + Detecting, + Starting, + Running, + Unsupported(String), + Failed(ExecError), +} +#[derive(Clone, Default)] +pub struct MonitorWatches { + watched: Rc>>, +} +struct Watched { + exec: Weak, + container: bool, + watch: WeakEntity, +} +impl MonitorWatches { + pub fn watch( + &self, + exec: Arc, + container: bool, + platform: Option, + cx: &mut App, + ) -> Entity { + let mut watched = self.watched.borrow_mut(); + watched.retain(|entry| entry.watch.upgrade().is_some()); + if let Some(watch) = watched + .iter() + .filter(|entry| { + entry.container == container + && std::ptr::addr_eq(entry.exec.as_ptr(), Arc::as_ptr(&exec)) + }) + .find_map(|entry| entry.watch.upgrade()) + { + return watch; + } + let watch = cx.new(|_| MonitorWatch { + exec: exec.clone(), + container, + platform: platform.map(|platform| scoped_platform(platform, container)), + plans: Default::default(), + plan: None, + status: WatchStatus::Off, + reading: None, + sequence: 0, + generation: 0, + task: None, + }); + watched.push(Watched { + exec: Arc::downgrade(&exec), + container, + watch: watch.downgrade(), + }); + watch + } +} +pub struct MonitorWatch { + exec: Arc, + container: bool, + platform: Option, + plans: std::collections::BTreeMap, + plan: Option, + status: WatchStatus, + reading: Option, + sequence: u64, + generation: u64, + task: Option>, +} +impl MonitorWatch { + pub fn platform(&self) -> Option<&Platform> { + self.platform.as_ref() + } + pub fn status(&self) -> &WatchStatus { + &self.status + } + pub fn reading(&self) -> Option<(u64, &Reading)> { + self.reading + .as_ref() + .map(|reading| (self.sequence, reading)) + } + pub fn subscribe(&mut self, consumer: EntityId, plan: WatchPlan, cx: &mut Context) { + if plan.metrics.is_empty() { + self.plans.remove(&consumer); + } else { + self.plans.insert( + consumer, + WatchPlan { + interval: plan.interval.max(Duration::from_secs(1)), + ..plan + }, + ); + } + self.refresh(cx); + } + pub fn unsubscribe(&mut self, consumer: EntityId, cx: &mut Context) { + self.plans.remove(&consumer); + self.refresh(cx); + } + fn refresh(&mut self, cx: &mut Context) { + let plan = self + .plans + .values() + .copied() + .reduce(|left, right| WatchPlan { + metrics: left.metrics.union(right.metrics), + interval: left.interval.min(right.interval), + }); + if self.plan == plan { + return; + } + self.task = None; + self.generation = self.generation.saturating_add(1); + self.plan = plan; + let Some(plan) = plan else { + self.set_status(WatchStatus::Off, cx); + return; + }; + if self.generation == u64::MAX { + self.set_status( + WatchStatus::Failed(ExecError::Failed("Monitor generation exhausted".into())), + cx, + ); + return; + } + let generation = self.generation; + let exec = self.exec.clone(); + let container = self.container; + let platform = self.platform.clone(); + self.set_status( + if platform.is_some() { + WatchStatus::Starting + } else { + WatchStatus::Detecting + }, + cx, + ); + self.task = Some(cx.spawn(async move |this, cx| { + watch::watch(this, exec, container, platform, plan, generation, cx).await + })); + } + fn set_status(&mut self, status: WatchStatus, cx: &mut Context) { + if self.status != status { + self.status = status; + cx.notify(); + } + } + fn accept(&mut self, generation: u64, reading: Reading, cx: &mut Context) -> bool { + if generation != self.generation || self.plan.is_none() { + return false; + } + self.sequence = self.sequence.saturating_add(1); + self.reading = Some(reading); + self.status = WatchStatus::Running; + cx.notify(); + true + } +} + +fn scoped_platform(platform: Platform, container: bool) -> Platform { + if !container { + platform + } else if platform == Platform::Linux || platform == Platform::LinuxContainer { + Platform::LinuxContainer + } else { + Platform::Unsupported("container accounting requires Linux".into()) + } +} + +#[cfg(test)] +mod tests; diff --git a/crates/monitor-service/src/tests.rs b/crates/monitor-service/src/tests.rs new file mode 100644 index 00000000..6b79312c --- /dev/null +++ b/crates/monitor-service/src/tests.rs @@ -0,0 +1,136 @@ +//! Public captured-watch lifecycle and shared work, using the session execution fake. +use crate::{MonitorWatches, WatchPlan, WatchStatus}; +use gpui::{AppContext as _, TestAppContext}; +use nocterm_monitor::{MetricSet, MonitorMetric, Platform}; +use nocterm_session::{ExecExit, HostExec, testing::FakeExec}; +use std::{sync::Arc, time::Duration}; + +fn plan(metric: MonitorMetric, seconds: u64) -> WatchPlan { + WatchPlan { + metrics: [metric].into_iter().collect(), + interval: Duration::from_secs(seconds), + } +} + +#[gpui::test] +fn consumers_share_one_captured_watch_and_the_last_one_stops_it(cx: &mut TestAppContext) { + let exec = Arc::new(FakeExec::default()); + let sink = exec.queue_stream("sh"); + let host: Arc = exec.clone(); + let registry = MonitorWatches::default(); + let (watch, again, first, second) = cx.update(|cx| { + let first = cx.new(|_| ()); + let second = cx.new(|_| ()); + let watch = registry.watch(host.clone(), false, Some(Platform::Linux), cx); + let again = registry.watch(host, false, Some(Platform::Linux), cx); + watch.update(cx, |watch, cx| { + watch.subscribe(first.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + again.update(cx, |watch, cx| { + watch.subscribe(second.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + (watch, again, first, second) + }); + cx.run_until_parked(); + assert_eq!(watch.entity_id(), again.entity_id()); + assert_eq!(exec.requests().len(), 1); + assert!(!sink.is_closed()); + watch.update(cx, |watch, cx| watch.unsubscribe(first.entity_id(), cx)); + cx.run_until_parked(); + assert!(!sink.is_closed()); + again.update(cx, |watch, cx| watch.unsubscribe(second.entity_id(), cx)); + cx.run_until_parked(); + assert!(sink.is_closed()); + watch.read_with(cx, |watch, _| assert_eq!(watch.status(), &WatchStatus::Off)); +} + +#[gpui::test] +fn details_expand_one_collection_and_releasing_them_restores_the_remaining_plan( + cx: &mut TestAppContext, +) { + let exec = Arc::new(FakeExec::default()); + let slow = exec.queue_stream("sh"); + let registry = MonitorWatches::default(); + let (watch, summary, details) = cx.update(|cx| { + let summary = cx.new(|_| ()); + let details = cx.new(|_| ()); + let watch = registry.watch(exec.clone(), false, Some(Platform::Linux), cx); + watch.update(cx, |watch, cx| { + watch.subscribe(summary.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + (watch, summary, details) + }); + cx.run_until_parked(); + let fast = exec.queue_stream("sh"); + watch.update(cx, |watch, cx| { + watch.subscribe( + details.entity_id(), + WatchPlan { + metrics: MetricSet::all(), + interval: Duration::from_secs(2), + }, + cx, + ) + }); + cx.run_until_parked(); + assert!(slow.is_closed()); + assert!(!fast.is_closed()); + let request = exec.requests().last().unwrap().stdin.clone().unwrap(); + assert!(String::from_utf8(request).unwrap().contains("@@netdev")); + let restored = exec.queue_stream("sh"); + watch.update(cx, |watch, cx| watch.unsubscribe(details.entity_id(), cx)); + cx.run_until_parked(); + assert!(fast.is_closed()); + assert!(!restored.is_closed()); + let request = exec.requests().last().unwrap().stdin.clone().unwrap(); + assert!(!String::from_utf8(request).unwrap().contains("@@netdev")); + watch.update(cx, |watch, cx| watch.unsubscribe(summary.entity_id(), cx)); +} + +#[gpui::test] +fn different_executor_authorities_never_share_a_collection(cx: &mut TestAppContext) { + let registry = MonitorWatches::default(); + let first = Arc::new(FakeExec::default()); + let second = Arc::new(FakeExec::default()); + let (a, b, c) = cx.update(|cx| { + ( + registry.watch(first.clone(), false, None, cx), + registry.watch(second, false, None, cx), + registry.watch(first, true, Some(Platform::Linux), cx), + ) + }); + assert_ne!(a.entity_id(), b.entity_id()); + assert_ne!(a.entity_id(), c.entity_id()); + c.read_with(cx, |watch, _| { + assert_eq!(watch.platform(), Some(&Platform::LinuxContainer)) + }); +} + +#[gpui::test] +fn one_latest_reading_is_shared_as_data_without_a_second_sampler(cx: &mut TestAppContext) { + let exec = Arc::new(FakeExec::default()); + let sink = exec.queue_stream("sh"); + let registry = MonitorWatches::default(); + let (watch, consumer) = cx.update(|cx| { + let consumer = cx.new(|_| ()); + let watch = registry.watch(exec, false, Some(Platform::Linux), cx); + watch.update(cx, |watch, cx| { + watch.subscribe(consumer.entity_id(), plan(MonitorMetric::Memory, 30), cx) + }); + (watch, consumer) + }); + cx.run_until_parked(); + futures::executor::block_on( + sink.send(b"@@meminfo\nMemTotal: 1000\nMemAvailable: 250\n@@end\n".to_vec()), + ); + cx.run_until_parked(); + watch.read_with(cx, |watch, _| { + assert_eq!(watch.status(), &WatchStatus::Running); + assert_eq!( + watch.reading().unwrap().1.memory, + Some((1000 * 1024, 250 * 1024)) + ); + }); + watch.update(cx, |watch, cx| watch.unsubscribe(consumer.entity_id(), cx)); + sink.finish(ExecExit::default()); +} diff --git a/crates/monitor-service/src/watch.rs b/crates/monitor-service/src/watch.rs new file mode 100644 index 00000000..b11517ef --- /dev/null +++ b/crates/monitor-service/src/watch.rs @@ -0,0 +1,110 @@ +//! One generation-bound collection attempt and bounded retry cadence. +use crate::{MonitorWatch, WatchPlan, WatchStatus}; +use gpui::{AsyncApp, WeakEntity}; +use nocterm_monitor::{Platform, Watch, detect}; +use nocterm_session::{ExecError, HostExec}; +use std::{sync::Arc, time::Duration}; + +pub(super) async fn watch( + this: WeakEntity, + exec: Arc, + container: bool, + mut platform: Option, + plan: WatchPlan, + generation: u64, + cx: &mut AsyncApp, +) { + loop { + let outcome = run( + &this, + exec.as_ref(), + container, + &mut platform, + plan, + generation, + cx, + ) + .await; + let retry = this.update(cx, |this, cx| { + if this.generation != generation || this.plan.is_none() { + return false; + } + let status = match outcome { + Err(ExecError::Unsupported) => { + let name = match &platform { + Some(Platform::Unsupported(name)) => name.clone(), + Some(Platform::LinuxContainer) => "container cgroup accounting".into(), + _ => "unknown".into(), + }; + this.set_status(WatchStatus::Unsupported(name), cx); + return false; + } + Err(error @ ExecError::Source(_)) => { + this.set_status(WatchStatus::Failed(error), cx); + return false; + } + Err(error) => WatchStatus::Failed(error), + Ok(()) => { + WatchStatus::Failed(ExecError::Failed("The monitor script stopped.".into())) + } + }; + this.set_status(status, cx); + true + }); + if !matches!(retry, Ok(true)) { + return; + } + cx.background_executor() + .timer(Duration::from_secs(10)) + .await; + } +} +async fn run( + this: &WeakEntity, + exec: &dyn HostExec, + container: bool, + platform: &mut Option, + plan: WatchPlan, + generation: u64, + cx: &mut AsyncApp, +) -> Result<(), ExecError> { + let detected = match platform.clone() { + Some(platform) => platform, + None => { + let detected = detect(exec).await?; + let detected = crate::scoped_platform(detected, container); + *platform = Some(detected.clone()); + let accepted = this + .update(cx, |this, cx| { + if this.generation != generation { + return false; + } + this.platform = Some(detected.clone()); + this.set_status(WatchStatus::Starting, cx); + true + }) + .map_err(|_| ExecError::Disconnected)?; + if !accepted { + return Err(ExecError::Disconnected); + } + detected + } + }; + let mut watch = Watch::start(exec, detected, plan.metrics, plan.interval).await?; + while let Some(reading) = watch.next().await { + if container + && reading.container.as_ref().is_none_or(|counters| { + counters.cpu_nanoseconds.is_none() && counters.memory_current.is_none() + }) + { + return Err(ExecError::Unsupported); + } + if !matches!( + this.update(cx, |this, cx| this.accept(generation, reading, cx)), + Ok(true) + ) { + return Ok(()); + } + } + Ok(()) +} diff --git a/crates/monitor-ui/Cargo.toml b/crates/monitor-ui/Cargo.toml index 2a1ce5b5..c2f761f0 100644 --- a/crates/monitor-ui/Cargo.toml +++ b/crates/monitor-ui/Cargo.toml @@ -11,17 +11,32 @@ layer = "feature" [dependencies] nocterm-monitor.workspace = true +nocterm-monitor-service.workspace = true +nocterm-plugin-model.workspace = true +nocterm-project-model.workspace = true +nocterm-project-providers.workspace = true nocterm-session.workspace = true nocterm-store.workspace = true nocterm-ui.workspace = true nocterm-workspace.workspace = true +nocterm-plugin-api = { workspace = true, optional = true } +nocterm-plugin-ports = { workspace = true, optional = true } gpui-kit.workspace = true +[features] +# The monitor's reading, offered to user plugins that were granted `monitor.usage`. +user-plugins = ["dep:nocterm-plugin-api", "dep:nocterm-plugin-ports"] + [lints] workspace = true [dev-dependencies] +nocterm-containers.workspace = true +serde_json.workspace = true +nocterm-workspace = { workspace = true, features = ["test-support"] } +nocterm-session = { workspace = true, features = ["test-support"] } nocterm-settings.workspace = true +nocterm-plugin-enablement.workspace = true tempfile.workspace = true toml.workspace = true futures.workspace = true diff --git a/crates/monitor-ui/src/details.rs b/crates/monitor-ui/src/details.rs index 583b0642..d5e33034 100644 --- a/crates/monitor-ui/src/details.rs +++ b/crates/monitor-ui/src/details.rs @@ -1,6 +1,7 @@ //! The popup with everything the details setting asks for. use crate::MonitorSettings; +use gpui_kit::TestSupportExt as _; use gpui_kit::{ AnyElement, App, Context, Entity, Hsla, SharedString, Window, component::{ @@ -43,7 +44,7 @@ impl Render for DetailsView { .and_then(|sampler| sampler.snapshot().host.as_ref()) .map(|host| host.hostname.clone()) .filter(|name| !name.is_empty()) - .or_else(|| monitor.host().map(|host| host.label())) + .or_else(|| monitor.label()) .unwrap_or_default(); body = body.child(header(title, monitor, settings, cx)); match monitor.sampler() { @@ -63,6 +64,7 @@ impl Render for DetailsView { } v_flex() .id("host-monitor-details") + .test_support() .w(rems(26.)) .max_h(rems(36.)) .overflow_y_scroll() @@ -153,8 +155,15 @@ fn header( .into_any_element() } -/// Whether any frame arrived yet: every script reports the host or its uptime. +/// Whether a supported metric is ready; host scripts also report identity or uptime. fn has_readings(snapshot: &Snapshot) -> bool { + if snapshot + .container + .as_ref() + .is_some_and(|container| container.memory_current.is_some() || snapshot.cpu.is_some()) + { + return true; + } snapshot.host.is_some() || snapshot.uptime.is_some() } @@ -217,6 +226,9 @@ fn block( cx: &App, ) -> AnyElement { v_flex() + .id(format!("host-monitor-section-{title}")) + .aria_label(title) + .test_support() .gap_1() .child( h_flex() @@ -239,6 +251,46 @@ fn strong(text: String) -> impl IntoElement { div().text_sm().font_semibold().child(text) } +fn memory_section( + snapshot: &Snapshot, + history: &History, + span: f64, + cx: &App, +) -> Option { + let theme = cx.theme(); + if let Some(container) = snapshot + .container + .as_ref() + .filter(|container| container.memory_limit.is_none()) + { + return Some(block( + "Memory", + strong(format::bytes(container.memory_current?)), + muted_line("Limit is unlimited or not visible", cx), + cx, + )); + } + let memory = snapshot.memory?; + Some(block( + "Memory", + strong(format!( + "{} / {}", + format::bytes(memory.used), + format::bytes(memory.total) + )), + v_flex() + .gap_1() + .child(graph::meter(memory.percent(), theme.chart_2, cx)) + .child(graph::sparkline( + vec![Line::new(&history.memory, theme.chart_2)], + span, + Some(100.0), + cx, + )), + cx, + )) +} + #[expect(clippy::too_many_lines, reason = "predates the limit")] fn section( metric: MonitorMetric, @@ -252,12 +304,20 @@ fn section( MonitorMetric::Cpu => { let cpu = snapshot.cpu?; block( - "CPU", + if snapshot.container.is_some() { + "CPU (one CPU)" + } else { + "CPU" + }, strong(format::percent(cpu)), graph::sparkline( vec![Line::new(&history.cpu, theme.chart_1)], span, - Some(100.0), + if snapshot.container.is_some() { + None + } else { + Some(100.0) + }, cx, ), cx, @@ -272,27 +332,7 @@ fn section( cx, ) } - MonitorMetric::Memory => { - let memory = snapshot.memory?; - block( - "Memory", - strong(format!( - "{} / {}", - format::bytes(memory.used), - format::bytes(memory.total) - )), - v_flex() - .gap_1() - .child(graph::meter(memory.percent(), theme.chart_2, cx)) - .child(graph::sparkline( - vec![Line::new(&history.memory, theme.chart_2)], - span, - Some(100.0), - cx, - )), - cx, - ) - } + MonitorMetric::Memory => memory_section(snapshot, history, span, cx)?, MonitorMetric::Swap => { let swap = snapshot.swap.filter(|swap| swap.total > 0)?; block( diff --git a/crates/monitor-ui/src/host_capabilities.rs b/crates/monitor-ui/src/host_capabilities.rs new file mode 100644 index 00000000..2e14f195 --- /dev/null +++ b/crates/monitor-ui/src/host_capabilities.rs @@ -0,0 +1,79 @@ +//! The monitor's latest reading, offered to user plugins that were granted `monitor.usage`. +//! +//! Passive: the provider only looks at the monitor the status bar widget already created. A +//! window whose layout does not place the widget, a disabled plugin and a monitor that is off +//! have no reading to give, so the answer is `unavailable`; the provider never starts a watch. +use std::{cell::RefCell, rc::Rc}; + +use gpui_kit::{App, Context, Entity, WeakEntity, Window}; +use nocterm_plugin_api::{ + HostError, + capability::monitor::{Usage, UsageNow}, +}; +use nocterm_plugin_model::PluginId; +use nocterm_plugin_ports::Observe; +use nocterm_workspace::{RegistrationError, Workspace}; + +use crate::{ + Installed, + model::{HostMonitor, Status}, +}; + +/// The CPU and memory use of the window's monitored host. +#[derive(Clone, Default)] +pub struct MonitorUsage { + monitor: Rc>>>, +} + +impl MonitorUsage { + pub fn new() -> Self { + Self::default() + } + + fn publish(&self, monitor: &Entity) { + *self.monitor.borrow_mut() = Some(monitor.downgrade()); + } +} + +/// [`register`](crate::register), also letting `usage` read the monitor the widget creates. +pub fn register_observed( + installed: &Installed, + usage: &MonitorUsage, + workspace: &mut Workspace, + window: &mut Window, + cx: &mut Context, +) -> Result<(), RegistrationError> { + let usage = usage.clone(); + crate::register_with( + installed, + move |monitor| usage.publish(monitor), + workspace, + window, + cx, + ) +} + +impl Observe for MonitorUsage { + fn sample(&self, _: &PluginId, cx: &App) -> Result { + let monitor = self + .monitor + .borrow() + .as_ref() + .and_then(WeakEntity::upgrade) + .ok_or(HostError::Unavailable)?; + let monitor = monitor.read(cx); + if *monitor.status() != Status::Running { + return Err(HostError::Unavailable); + } + let snapshot = monitor.sampler().ok_or(HostError::Unavailable)?.snapshot(); + let cpu = snapshot.cpu.ok_or(HostError::Unavailable)?; + let memory = snapshot.memory.ok_or(HostError::Unavailable)?.percent(); + if !cpu.is_finite() || !memory.is_finite() { + return Err(HostError::Unavailable); + } + Ok(Usage { + cpu_percent: cpu.clamp(0.0, 100.0), + memory_percent: memory.clamp(0.0, 100.0), + }) + } +} diff --git a/crates/monitor-ui/src/installed.rs b/crates/monitor-ui/src/installed.rs index ba6c85fc..f6ecc107 100644 --- a/crates/monitor-ui/src/installed.rs +++ b/crates/monitor-ui/src/installed.rs @@ -8,8 +8,12 @@ use std::sync::Arc; pub struct Installed { settings: SettingsHandle, local: Option>, + collectors: nocterm_monitor_service::MonitorWatches, } impl Installed { + pub(crate) fn collectors(&self) -> &nocterm_monitor_service::MonitorWatches { + &self.collectors + } pub(crate) fn settings(&self) -> &SettingsHandle { &self.settings } @@ -26,5 +30,6 @@ pub fn install( Installed { settings: ready.settings().clone(), local, + collectors: Default::default(), } } diff --git a/crates/monitor-ui/src/lib.rs b/crates/monitor-ui/src/lib.rs index a9daca42..616da02f 100644 --- a/crates/monitor-ui/src/lib.rs +++ b/crates/monitor-ui/src/lib.rs @@ -1,34 +1,50 @@ -//! The active host's resources at the start of the status bar. +//! The selected workspace environment's resources in the status bar. //! //! A compact summary — CPU and memory by default — follows the active tab's -//! host; clicking it opens details with every core, memory, temperatures, +//! environment; clicking it opens supported CPU, memory, temperature, //! disks, uptime and graphs of network and disk throughput. Collection runs //! on the host itself through [`nocterm_monitor`]: while the details are //! closed only what the summary shows is read, at the slow interval. //! -//! Owns: active-host sampling lifecycle, status/details views and monitor settings controls. +//! Owns: selected monitor-source lifecycle, generation-bound sampling, resource-specific +//! history, status/details views and monitor settings controls. //! Installation retains actual settings and optional supplied local host execution; models share it. //! Does not own: metric parsing, host execution or session establishment. -//! Ports used: workspace active-session contracts and session `HostExec` and supplied SettingsHandle; provided: `SettingsPageSpec`. +//! Ports used: immutable workspace MonitorSource, scoped session HostExec and supplied +//! SettingsHandle; provided: SettingsPageSpec, the `monitor` system plugin +//! (`plugin::PLUGIN`) with its status bar widget and its "Status bar" settings schema, and +//! `plugin_settings` (a `SectionBinding` over `[monitor]`) and, behind `user-plugins`, the +//! `MonitorUsage` provider of `monitor.usage` for user plugins (it only reads the monitor the +//! widget created, so a disabled plugin or an unplaced widget answers `unavailable`). //! Settings section: `[monitor]`. Globals: none. -//! Invariants: one host script collects the current metric union; retained history is bounded. +//! Invariants: one script collects the current metric union; eight retained records stay +//! distinct by surface and resource; unavailable projects never use a local fallback. +//! Container CPU uses a labeled one-CPU basis; cgroup memory never uses host capacity. mod details; mod installed; pub use installed::{Installed, install}; mod graph; +#[cfg(feature = "user-plugins")] +mod host_capabilities; mod model; mod page; +pub mod plugin; mod settings; +mod source; mod status; +mod status_bar_settings; -use gpui_kit::{AppContext as _, Context, Window}; +use gpui_kit::{AppContext as _, Context, Entity, Window}; use nocterm_ui::SettingsPageSpec; -use nocterm_workspace::Workspace; +use nocterm_workspace::{RegistrationError, WidgetRegistration, Workspace}; +#[cfg(feature = "user-plugins")] +pub use host_capabilities::{MonitorUsage, register_observed}; pub use page::MonitorPage; pub use settings::{ MONITOR_DETAIL_INTERVAL_RANGE, MONITOR_HISTORY_RANGE, MONITOR_INTERVAL_RANGE, MonitorSettings, }; +pub use status_bar_settings::plugin_settings; gpui_kit::actions!( monitor, @@ -39,17 +55,38 @@ gpui_kit::actions!( ); /// Adds the monitor to the footer of `workspace`. +/// +/// The widget creates the monitor, which starts reading the host: a window without the +/// registration runs no program. pub fn register( installed: &Installed, workspace: &mut Workspace, - _window: &mut Window, + window: &mut Window, cx: &mut Context, -) { - let handle = cx.entity(); - let session = workspace.active_session(cx); - let monitor = cx.new(|cx| model::HostMonitor::new(&handle, session, installed, cx)); - let status = cx.new(|cx| status::MonitorStatus::new(monitor, cx)); - workspace.add_leading_status_view(status, cx); +) -> Result<(), RegistrationError> { + register_with(installed, |_| {}, workspace, window, cx) +} + +/// [`register`], handing each monitor the widget creates to `created`. +fn register_with( + installed: &Installed, + created: impl Fn(&Entity) + 'static, + workspace: &mut Workspace, + window: &mut Window, + cx: &mut Context, +) -> Result<(), RegistrationError> { + let installed = installed.clone(); + workspace.register_widget( + WidgetRegistration::zone_aware(&plugin::SUMMARY, move |workspace, _, cx| { + let session = workspace.monitor_source(cx); + let handle = cx.entity(); + let monitor = cx.new(|cx| model::HostMonitor::new(&handle, session, &installed, cx)); + created(&monitor); + cx.new(|cx| status::MonitorStatus::new(monitor, cx)) + }), + window, + cx, + ) } /// Supplies the lazy Monitor page to the application Settings host. diff --git a/crates/monitor-ui/src/model.rs b/crates/monitor-ui/src/model.rs index f089e619..e11392d9 100644 --- a/crates/monitor-ui/src/model.rs +++ b/crates/monitor-ui/src/model.rs @@ -1,39 +1,24 @@ -//! Which host the monitor watches, and the one script that watches it. +//! Selected surface resource identity and one generation-bound monitoring watch. //! -//! The model follows the workspace's active host ([`nocterm_workspace::host`]): -//! a remote session over its own connection, otherwise this computer, when -//! the settings allow. Only one script runs at a time, -//! and it collects only what is on screen: the status bar's metrics while -//! the details are closed, at the slow interval, and everything the details -//! show while they are open, at the fast one. +//! General contexts retain explicitly supplied local policy. Project contexts +//! observe only their original inner session; unavailable resources never fall +//! back to an outer or local executor. History is bounded per surface/resource. +use nocterm_monitor_service::{MonitorWatch, MonitorWatches, WatchPlan, WatchStatus}; use std::{sync::Arc, time::Duration}; use crate::MonitorSettings; -use gpui_kit::{App, AsyncApp, Context, Entity, EventEmitter, Subscription, Task, WeakEntity}; -use nocterm_monitor::{MetricSet, Platform, Sampler, Watch, detect, local_platform}; -use nocterm_session::{ExecError, HostExec}; -use nocterm_workspace::{Host, HostKey, SessionContext, Workspace, host::follow_active_session}; +#[cfg(test)] +pub(crate) use crate::source::watched_host; +use crate::source::{MonitorKey, WatchHost, watched_source}; +use gpui_kit::{App, Context, Entity, EventEmitter, Subscription}; +use nocterm_monitor::{MetricSet, Platform, Sampler, local_platform}; +use nocterm_session::HostExec; +use nocterm_workspace::{HostKey, MonitorSource, Workspace, WorkspaceEvent}; -/// How long a failed or ended watch waits before it is tried again. -const RETRY: Duration = Duration::from_secs(10); /// Hosts whose platform and history are remembered for switching back. const REMEMBERED_HOSTS: usize = 8; -/// The host to watch: the active session's, or this computer when the -/// settings allow; nothing while monitoring is off. -pub(crate) fn watched_host( - session: Option<&SessionContext>, - local: Option<&Arc>, - settings: &MonitorSettings, -) -> Option { - let local = local.filter(|_| settings.local); - settings - .enabled - .then(|| Host::resolve(session, local)) - .flatten() -} - /// What to collect and how often. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub(crate) struct Plan { @@ -78,24 +63,34 @@ pub(crate) enum Status { /// A remembered host: what it is and what it looked like. struct Record { - key: HostKey, - platform: Option, + key: MonitorKey, sampler: Sampler, + platform: Option<(std::sync::Weak, Platform)>, } pub(crate) struct HostMonitor { + consumer: gpui_kit::EntityId, settings: nocterm_store::SettingsHandle, local: Option>, - session: Option, - host: Option, + source: MonitorSource, + host: Option, open: bool, status: Status, /// Most recently used first; the current host, when there is one, leads. records: Vec, /// The host and plan the task was started for. - wanted: Option<(Host, Plan)>, - task: Option>, - _subscriptions: [Subscription; 2], + wanted: Option<(WatchHost, Plan)>, + collectors: MonitorWatches, + watch: Option, + generation: u64, + _subscriptions: Vec, +} + +/// The model's retained subscription and last applied raw reading. +struct CollectedWatch { + collection: Entity, + sequence: u64, + _observation: Subscription, } /// Fired whenever new readings or a new status are available. @@ -106,26 +101,50 @@ impl EventEmitter for HostMonitor {} impl HostMonitor { pub(crate) fn new( workspace: &Entity, - session: Option, + source: MonitorSource, installed: &crate::Installed, cx: &mut Context, ) -> Self { - let follow = follow_active_session(workspace, cx, Self::follow); + let follow = cx.subscribe(workspace, |_, workspace, event, cx| { + if matches!( + event, + WorkspaceEvent::ContextChanged + | WorkspaceEvent::ActiveSessionChanged + | WorkspaceEvent::ActiveItemChanged + | WorkspaceEvent::ItemsChanged + ) { + let workspace = workspace.downgrade(); + let this = cx.entity().downgrade(); + cx.defer(move |cx| { + if let Some(workspace) = workspace.upgrade() { + let source = workspace.read(cx).monitor_source(cx); + let _ = this.update(cx, |this, cx| this.follow(source, cx)); + } + }); + } + }); let weak = cx.weak_entity(); let observation = installed.settings().observe(cx, move |cx| { let _ = weak.update(cx, |this, cx| this.refresh(cx)); }); let mut this = Self { + consumer: cx.entity_id(), local: installed.local(), settings: installed.settings().clone(), - session, + source, host: None, open: false, status: Status::Off, records: Vec::new(), wanted: None, - task: None, - _subscriptions: [follow, observation], + collectors: installed.collectors().clone(), + watch: None, + generation: 0, + _subscriptions: vec![ + follow, + observation, + cx.on_release(|this, cx| this.stop_watch(cx)), + ], }; this.refresh(cx); this @@ -138,13 +157,20 @@ impl HostMonitor { &self.status } + #[cfg(test)] pub(crate) fn host(&self) -> Option<&HostKey> { - self.host.as_ref().map(|host| &host.key) + self.host.as_ref().map(|host| &host.host().key) + } + pub(crate) fn container(&self) -> bool { + self.host.as_ref().is_some_and(|host| host.container()) + } + pub(crate) fn label(&self) -> Option { + self.host.as_ref().map(WatchHost::label) } /// What the current host looked like at the last reading. pub(crate) fn sampler(&self) -> Option<&Sampler> { - let key = &self.host.as_ref()?.key; + let key = self.host.as_ref()?.key(); self.records .first() .filter(|record| record.key == *key) @@ -163,8 +189,8 @@ impl HostMonitor { } } - fn follow(&mut self, session: Option, cx: &mut Context) { - self.session = session; + fn follow(&mut self, source: MonitorSource, cx: &mut Context) { + self.source = source; self.refresh(cx); } @@ -172,7 +198,7 @@ impl HostMonitor { /// settings and the details. fn refresh(&mut self, cx: &mut Context) { let settings = self.settings(cx).clone(); - let host = watched_host(self.session.as_ref(), self.local.as_ref(), &settings); + let host = watched_source(&self.source, self.local.as_ref(), &settings); let plan = Plan::new(&settings, self.open); let history = settings.history_points as usize; for record in &mut self.records { @@ -182,60 +208,161 @@ impl HostMonitor { let wanted = host.clone().zip(plan); let unchanged = match (&self.wanted, &wanted) { (Some((was, before)), Some((host, plan))) => was.same_as(host) && before == plan, - (None, None) => true, + (None, None) => false, _ => false, }; if unchanged { return; } - self.task = None; + self.generation = self.generation.saturating_add(1); self.wanted = wanted; self.host = host; let Some((host, plan)) = self.wanted.clone() else { - self.set_status(Status::Off, cx); + self.stop_watch(cx); + self.set_status( + if settings.enabled + && plan.is_some() + && (!self.source.local() || !self.source.available()) + { + Status::Offline + } else { + Status::Off + }, + cx, + ); return; }; - self.remember(&host.key, history); - if !host.connected { + self.remember(host.key(), history); + if !host.host().connected { + self.stop_watch(cx); self.set_status(Status::Offline, cx); return; } - let platform = match host.key { - HostKey::Local => Some(local_platform()), - HostKey::Remote { .. } => self.records[0].platform.clone(), + if self.generation == u64::MAX { + self.stop_watch(cx); + self.set_status(Status::Failed("Monitor generation exhausted".into()), cx); + return; + } + self.attach(host, plan, cx); + } + + fn attach(&mut self, host: WatchHost, plan: Plan, cx: &mut Context) { + let platform = if matches!(host.host().key, HostKey::Local) { + Some(local_platform()) + } else { + self.records[0] + .platform + .as_ref() + .filter(|(exec, _)| { + std::ptr::addr_eq(exec.as_ptr(), Arc::as_ptr(&host.host().exec)) + }) + .map(|(_, platform)| platform.clone()) }; - self.set_status( - if platform.is_some() { - Status::Starting - } else { - Status::Detecting - }, - cx, - ); - self.task = Some(cx.spawn(async move |this, cx| { - watch(this, host, platform, plan, cx).await; - })); + let collection = + self.collectors + .watch(host.host().exec.clone(), host.container(), platform, cx); + let old = self.watch.take(); + let sequence = old + .as_ref() + .filter(|old| old.collection == collection) + .map_or(0, |old| old.sequence); + if let Some(old) = old + && old.collection != collection + { + old.collection + .update(cx, |watch, cx| watch.unsubscribe(self.consumer, cx)); + } + let generation = self.generation; + let observation = cx.observe(&collection, move |this, collection, cx| { + this.apply_collection(&collection, generation, cx) + }); + let consumer = cx.entity_id(); + collection.update(cx, |watch, cx| { + watch.subscribe( + consumer, + WatchPlan { + metrics: plan.metrics, + interval: plan.interval, + }, + cx, + ) + }); + self.watch = Some(CollectedWatch { + collection: collection.clone(), + sequence, + _observation: observation, + }); + self.apply_collection(&collection, generation, cx); + } + + fn stop_watch(&mut self, cx: &mut App) { + if let Some(watch) = self.watch.take() { + let consumer = self.consumer; + watch + .collection + .update(cx, |watch, cx| watch.unsubscribe(consumer, cx)); + } + } + + fn apply_collection( + &mut self, + collection: &Entity, + generation: u64, + cx: &mut Context, + ) { + if self.generation != generation { + return; + } + let Some(watch) = self + .watch + .as_mut() + .filter(|watch| watch.collection == *collection) + else { + return; + }; + let collected = collection.read(cx); + if let Some(platform) = collected.platform() + && let (Some(host), Some(record)) = (&self.host, self.records.first_mut()) + { + record.platform = Some((Arc::downgrade(&host.host().exec), platform.clone())); + } + let status = match collected.status() { + WatchStatus::Off => Status::Off, + WatchStatus::Detecting => Status::Detecting, + WatchStatus::Starting => Status::Starting, + WatchStatus::Running => Status::Running, + WatchStatus::Unsupported(name) => Status::Unsupported(name.clone()), + WatchStatus::Failed(error) => Status::Failed(error.to_string()), + }; + let reading = collected + .reading() + .filter(|(sequence, _)| *sequence != watch.sequence) + .map(|(sequence, reading)| (sequence, reading.clone())); + if let Some((sequence, reading)) = reading { + watch.sequence = sequence; + if let Some(record) = self.records.first_mut() { + record.sampler.apply(reading); + cx.emit(Changed); + cx.notify(); + } + } + self.set_status(status, cx); } /// Moves the record of `key` to the front, creating it if need be. - fn remember(&mut self, key: &HostKey, history: usize) { + fn remember(&mut self, key: &MonitorKey, history: usize) { let record = match self.records.iter().position(|record| record.key == *key) { Some(index) => self.records.remove(index), None => Record { key: key.clone(), - platform: None, sampler: Sampler::new(history), + platform: None, }, }; self.records.insert(0, record); self.records.truncate(REMEMBERED_HOSTS); } - /// The record of the watched host, if it is still `key`. - fn current(&mut self, key: &HostKey) -> Option<&mut Record> { - self.records.first_mut().filter(|record| record.key == *key) - } - fn set_status(&mut self, status: Status, cx: &mut Context) { if self.status != status { self.status = status; @@ -244,86 +371,3 @@ impl HostMonitor { } } } - -/// Detects the host if need be, then runs its script, again after failures. -async fn watch( - this: WeakEntity, - host: Host, - mut platform: Option, - plan: Plan, - cx: &mut AsyncApp, -) { - let key = host.key.clone(); - loop { - let outcome = run(&this, &host, &mut platform, plan, cx).await; - let retry = this.update(cx, |this, cx| { - let status = match outcome { - Err(ExecError::Unsupported) => { - let name = match &platform { - Some(Platform::Unsupported(name)) => name.clone(), - _ => "unknown".into(), - }; - this.set_status(Status::Unsupported(name), cx); - return false; - } - Err(ExecError::Disconnected) => "The connection closed.".into(), - Err(ExecError::Failed(error)) => error, - Err(error @ ExecError::NotFound(_)) => error.to_string(), - Ok(()) => "The monitor script stopped.".into(), - }; - if this.current(&key).is_some() { - this.set_status(Status::Failed(status), cx); - } - true - }); - if !matches!(retry, Ok(true)) { - return; - } - cx.background_executor().timer(RETRY).await; - } -} - -/// One attempt: detection, then readings until the script ends. -async fn run( - this: &WeakEntity, - host: &Host, - platform: &mut Option, - plan: Plan, - cx: &mut AsyncApp, -) -> Result<(), ExecError> { - let detected = match platform.clone() { - Some(platform) => platform, - None => { - let detected = detect(host.exec.as_ref()).await?; - *platform = Some(detected.clone()); - let key = host.key.clone(); - let remembered = detected.clone(); - this.update(cx, |this, cx| { - if let Some(record) = this.current(&key) { - record.platform = Some(remembered); - this.set_status(Status::Starting, cx); - } - }) - .map_err(|_| ExecError::Disconnected)?; - detected - } - }; - let mut watch = Watch::start(host.exec.as_ref(), detected, plan.metrics, plan.interval).await?; - while let Some(reading) = watch.next().await { - let key = host.key.clone(); - let applied = this.update(cx, |this, cx| { - let Some(record) = this.current(&key) else { - return false; - }; - record.sampler.apply(reading); - this.status = Status::Running; - cx.emit(Changed); - cx.notify(); - true - }); - if !matches!(applied, Ok(true)) { - return Ok(()); - } - } - Ok(()) -} diff --git a/crates/monitor-ui/src/page.rs b/crates/monitor-ui/src/page.rs index 1fc28821..956d10be 100644 --- a/crates/monitor-ui/src/page.rs +++ b/crates/monitor-ui/src/page.rs @@ -341,12 +341,6 @@ impl Render for MonitorPage { }) .collect::>() }; - let status_bar = rows( - "monitor-status", - &monitor.status_bar, - |monitor| &mut monitor.status_bar, - cx, - ); let details = rows( "monitor-details", &monitor.details, @@ -363,7 +357,6 @@ impl Render for MonitorPage { )) .child(form::section("General", general, cx)) .child(form::section("Refresh", refresh, cx)) - .child(form::section("Status bar", status_bar, cx)) .child(form::section("Details", details, cx)) } } diff --git a/crates/monitor-ui/src/plugin.rs b/crates/monitor-ui/src/plugin.rs new file mode 100644 index 00000000..1410bad6 --- /dev/null +++ b/crates/monitor-ui/src/plugin.rs @@ -0,0 +1,88 @@ +//! The host monitor as a system plugin: one status bar widget. +use std::borrow::Cow; + +use nocterm_plugin_model::{ + AutoPlace, ChoiceOption, ContributionId, ContributionKind, FieldGroup, FieldKind, FieldSpec, + Icon, Placement, PluginId, SettingsSchema, SystemContribution, SystemPlugin, Zone, +}; + +/// The resource summary of the active environment in the status bar. +pub const SUMMARY: SystemContribution = SystemContribution { + id: ContributionId::new("monitor.summary"), + kind: ContributionKind::Widget, + title: "Monitor", + icon: Icon::Activity, + placement: Placement { + zone: Zone::Left, + order: 60, + group: None, + auto: AutoPlace::Show, + }, + panel: None, + preview: None, + edge: None, + pinned: false, +}; + +/// Monitor: CPU and memory of the active environment. +pub static PLUGIN: SystemPlugin = SystemPlugin { + id: PluginId::new("monitor"), + name: "Monitor", + description: "CPU and memory of the active environment in the status bar", + required: false, + contributions: &[SUMMARY], + settings_section: Some("monitor"), + settings: SettingsSchema { + version: 1, + groups: Cow::Borrowed(&[STATUS_BAR_GROUP]), + }, + settings_page: Some("monitor"), +}; + +/// The key of the status bar metrics field; it is the serde key in `[monitor]`. +pub(crate) const STATUS_BAR_KEY: &str = "status_bar"; + +const fn metric(key: &'static str, label: &'static str) -> ChoiceOption { + ChoiceOption { + key: Cow::Borrowed(key), + label: Cow::Borrowed(label), + help: None, + } +} + +/// One option per `MonitorMetric`, in the order of `MonitorMetric::ALL`. +static METRICS: [ChoiceOption; 10] = [ + metric("cpu", "CPU"), + metric("cores", "CPU cores"), + metric("memory", "Memory"), + metric("swap", "Swap"), + metric("load", "Load average"), + metric("uptime", "Uptime"), + metric("temperature", "Temperature"), + metric("disks", "Disk space"), + metric("network", "Network"), + metric("disk_io", "Disk I/O"), +]; + +static DEFAULT_METRICS: [Cow<'static, str>; 2] = [Cow::Borrowed("cpu"), Cow::Borrowed("memory")]; + +static STATUS_BAR_FIELDS: [FieldSpec; 1] = [FieldSpec { + renamed_from: None, + key: Cow::Borrowed(STATUS_BAR_KEY), + label: Cow::Borrowed("Metrics in the status bar"), + help: Some(Cow::Borrowed( + "What the summary shows. Details open on click and show everything the host reports.", + )), + kind: FieldKind::MultiChoice { + options: Cow::Borrowed(&METRICS), + default: Cow::Borrowed(&DEFAULT_METRICS), + ordered: true, + max: 10, + }, +}]; + +const STATUS_BAR_GROUP: FieldGroup = FieldGroup { + title: Cow::Borrowed("Status bar"), + contribution: Some(ContributionId::new("monitor.summary")), + fields: Cow::Borrowed(&STATUS_BAR_FIELDS), +}; diff --git a/crates/monitor-ui/src/source.rs b/crates/monitor-ui/src/source.rs new file mode 100644 index 00000000..e5cde089 --- /dev/null +++ b/crates/monitor-ui/src/source.rs @@ -0,0 +1,118 @@ +//! Resource-specific monitor identity without changing shared host resolution. +use crate::MonitorSettings; +use gpui_kit::EntityId; +use nocterm_project_model::ProjectTarget; +use nocterm_project_providers::ContainerLocation; +use nocterm_session::HostExec; +use nocterm_workspace::{Host, HostKey, MonitorSource, SessionContext}; +use std::sync::Arc; + +#[derive(Clone, Debug, PartialEq, Eq)] +pub(super) struct MonitorKey { + surface: EntityId, + target: Option, + host: HostKey, +} +#[derive(Clone)] +pub(super) struct WatchHost { + key: MonitorKey, + host: Host, + container: bool, +} +impl WatchHost { + pub(super) fn key(&self) -> &MonitorKey { + &self.key + } + pub(super) fn host(&self) -> &Host { + &self.host + } + pub(super) fn container(&self) -> bool { + self.container + } + pub(super) fn same_as(&self, other: &Self) -> bool { + self.key == other.key && self.host.same_as(&other.host) + } + pub(super) fn label(&self) -> String { + match self + .key + .target + .as_ref() + .and_then(|target| ContainerLocation::from_target(target).ok()) + { + Some(location) => format!( + "{} {} — {}", + location.engine().name(), + &location.container()[..12], + self.host.key.label() + ), + None => self.host.key.label(), + } + } +} +pub(super) fn watched_source( + source: &MonitorSource, + local: Option<&Arc>, + settings: &MonitorSettings, +) -> Option { + if !source.available() { + return None; + } + let container = match source.target() { + Some(target @ ProjectTarget::Provider { .. }) => { + ContainerLocation::from_target(target).ok()?; + true + } + _ => false, + }; + // A selected project with no inner session is unavailable, even on a local parent. + if source.target().is_some() && source.session().is_none() && !source.local() { + return None; + } + let host = if source.local() { + if container || !settings.local || !settings.enabled { + return None; + } + Host { + key: HostKey::Local, + exec: source + .session() + .filter(|session| session.connected) + .and_then(|session| session.exec.clone()) + .or_else(|| local.cloned())?, + connected: source.session().is_none_or(|session| session.connected), + session: source.session().cloned(), + } + } else { + watched_host(source.session(), None, settings)? + }; + Some(WatchHost { + key: MonitorKey { + surface: source.surface(), + target: source.target().cloned(), + host: host.key.clone(), + }, + host, + container, + }) +} +pub(crate) fn watched_host( + session: Option<&SessionContext>, + local: Option<&Arc>, + settings: &MonitorSettings, +) -> Option { + if !settings.enabled { + return None; + } + if let Some(session) = session { + return Some(Host { + key: HostKey::Remote { + target: session.target.clone(), + via: session.via.clone(), + }, + exec: session.exec.clone()?, + connected: session.connected, + session: Some(session.clone()), + }); + } + settings.local.then(|| Host::resolve(None, local)).flatten() +} diff --git a/crates/monitor-ui/src/status.rs b/crates/monitor-ui/src/status.rs index ad5e70ae..0722647b 100644 --- a/crates/monitor-ui/src/status.rs +++ b/crates/monitor-ui/src/status.rs @@ -1,7 +1,7 @@ //! The compact summary at the start of the footer, which opens the details. use gpui_kit::{ - Anchor, AnyElement, App, AppContext as _, Context, Entity, FocusHandle, Hsla, Window, + AnyElement, App, AppContext as _, Context, Entity, FocusHandle, Hsla, Window, component::{ ActiveTheme as _, Icon, Sizable as _, button::{Button, ButtonVariants as _}, @@ -13,7 +13,9 @@ use gpui_kit::{ px, }; use nocterm_monitor::{MonitorMetric, Snapshot, format}; -use nocterm_ui::IconName; +use nocterm_plugin_model::Zone; +use nocterm_ui::{IconName, zone_anchor}; +use nocterm_workspace::ZoneAware; use crate::{ details::DetailsView, @@ -25,6 +27,7 @@ pub(crate) struct MonitorStatus { monitor: Entity, details: Entity, focus: FocusHandle, + zone: Zone, } impl MonitorStatus { @@ -35,6 +38,16 @@ impl MonitorStatus { monitor, details, focus: cx.focus_handle(), + zone: Zone::Left, + } + } +} + +impl ZoneAware for MonitorStatus { + fn set_zone(&mut self, zone: Zone, cx: &mut Context) { + if self.zone != zone { + self.zone = zone; + cx.notify(); } } } @@ -52,8 +65,13 @@ impl Render for MonitorStatus { Status::Failed(error) => error.clone(), Status::Running | Status::Off => "Host resources".to_owned(), }; - let tooltip = match monitor.host() { - Some(host) => format!("{} — {tooltip}", host.label()), + let tooltip = if monitor.container() { + format!("{tooltip}. CPU: 100% is one CPU; memory uses own cgroup accounting.") + } else { + tooltip + }; + let tooltip = match monitor.label() { + Some(label) => format!("{label} — {tooltip}"), None => tooltip, }; let stale = !matches!(monitor.status(), Status::Running); @@ -70,7 +88,7 @@ impl Render for MonitorStatus { let details = self.details.clone(); let model = self.monitor.downgrade(); Popover::new("host-monitor-details") - .anchor(Anchor::BottomLeft) + .anchor(zone_anchor(self.zone)) .trigger(button) .track_focus(&self.focus) .open(open) @@ -107,7 +125,33 @@ fn summary( .into_any_element() } +fn container_item(metric: MonitorMetric, snapshot: &Snapshot, muted: Hsla) -> Option { + let container = snapshot.container.as_ref()?; + let (icon, label, value) = match metric { + MonitorMetric::Cpu => (IconName::Cpu, "1 CPU", snapshot.cpu.map(format::percent)), + MonitorMetric::Memory if container.memory_limit.is_none() => ( + IconName::MemoryStick, + "", + container.memory_current.map(format::bytes), + ), + _ => return None, + }; + Some( + h_flex() + .gap_1() + .child(Icon::new(icon).xsmall().text_color(muted)) + .when(!label.is_empty(), |row| { + row.child(div().text_color(muted).child(label)) + }) + .child(value.unwrap_or_else(|| "—".into())) + .into_any_element(), + ) +} + fn item(metric: MonitorMetric, snapshot: &Snapshot, muted: Hsla, cx: &App) -> AnyElement { + if let Some(item) = container_item(metric, snapshot, muted) { + return item; + } let theme = cx.theme(); let icon = |name: IconName| Icon::new(name).xsmall().text_color(muted); let text = |label: &'static str| div().text_color(muted).child(label); diff --git a/crates/monitor-ui/src/status_bar_settings.rs b/crates/monitor-ui/src/status_bar_settings.rs new file mode 100644 index 00000000..35b68295 --- /dev/null +++ b/crates/monitor-ui/src/status_bar_settings.rs @@ -0,0 +1,80 @@ +//! The status bar metrics as the schema field `status_bar` of the `monitor` plugin. +//! +//! The field is stored where it always was, in `[monitor].status_bar`; this module only maps +//! between the schema's option keys and [`MonitorMetric`]. + +use std::rc::Rc; + +use nocterm_monitor::MonitorMetric; +use nocterm_plugin_model::{FieldError, SettingValue, SettingValues}; +use nocterm_ui::{PluginSettingsSource, SectionBinding}; + +use crate::{Installed, MonitorSettings, plugin}; + +/// The option key of `metric`, equal to its serde name. +fn key_of(metric: MonitorMetric) -> &'static str { + match metric { + MonitorMetric::Cpu => "cpu", + MonitorMetric::Cores => "cores", + MonitorMetric::Memory => "memory", + MonitorMetric::Swap => "swap", + MonitorMetric::Load => "load", + MonitorMetric::Uptime => "uptime", + MonitorMetric::Temperature => "temperature", + MonitorMetric::Disks => "disks", + MonitorMetric::Network => "network", + MonitorMetric::DiskIo => "disk_io", + } +} + +fn metric_of(key: &str) -> Option { + MonitorMetric::ALL + .into_iter() + .find(|metric| key_of(*metric) == key) +} + +fn read(settings: &MonitorSettings) -> SettingValues { + let mut values = SettingValues::default(); + values.insert( + plugin::STATUS_BAR_KEY, + SettingValue::Choices( + settings + .status_bar + .iter() + .map(|metric| key_of(*metric).to_string()) + .collect(), + ), + ); + values +} + +fn write(settings: &mut MonitorSettings, key: &str, value: SettingValue) -> Result<(), FieldError> { + match (key, value) { + (plugin::STATUS_BAR_KEY, SettingValue::Choices(chosen)) => { + let metrics = chosen + .iter() + .map(|key| { + metric_of(key).ok_or_else(|| FieldError::UnknownOption { + key: plugin::STATUS_BAR_KEY.to_string(), + option: key.clone(), + }) + }) + .collect::, _>>()?; + settings.status_bar = metrics; + Ok(()) + } + (key, _) => Err(FieldError::UnknownKey(key.to_string())), + } +} + +/// Where the Monitor's plugin settings are stored: section `[monitor]` of the installed +/// settings. The binding runs no program; a running monitor follows a change as it does for the +/// Monitor page. +pub fn plugin_settings(installed: &Installed) -> Rc { + Rc::new(SectionBinding::::new( + installed.settings().clone(), + &plugin::PLUGIN.settings, + read, + write, + )) +} diff --git a/crates/monitor-ui/src/tests.rs b/crates/monitor-ui/src/tests.rs index e1cf4d4b..7de1ab05 100644 --- a/crates/monitor-ui/src/tests.rs +++ b/crates/monitor-ui/src/tests.rs @@ -136,8 +136,7 @@ fn sessions_that_run_programs_are_watched_remotely() { )); let without_exec = SessionContext::new(target("user@other"), None, true); - let watched = watched_host(Some(&without_exec), Some(&local), &settings).unwrap(); - assert_eq!(watched.key, HostKey::Local); + assert!(watched_host(Some(&without_exec), Some(&local), &settings).is_none()); assert_eq!( watched_host(None, Some(&local), &settings).unwrap().key, HostKey::Local @@ -231,12 +230,19 @@ fn fixture_with_ready( gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { let deps = - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)); + nocterm_workspace::test_support::deps_for(&[&crate::plugin::PLUGIN], cx); Workspace::new(deps, window, cx) }) }) .unwrap(); - let monitor = cx.new(|cx| HostMonitor::new(&workspace, None, &installed, cx)); + let monitor = cx.new(|cx| { + HostMonitor::new( + &workspace, + workspace.read(cx).monitor_source(cx), + &installed, + cx, + ) + }); (window, workspace, monitor, installed, ready) }) } @@ -355,7 +361,7 @@ fn registered_monitors_keep_their_supplied_host_across_other_windows(cx: &mut Te cx.update_window(window, |_, window, cx| { workspace.update(cx, |workspace, cx| { let installed = crate::install(Some(host.clone()), &ready, cx); - crate::register(&installed, workspace, window, cx); + crate::register(&installed, workspace, window, cx).unwrap(); }); }) .unwrap(); @@ -368,4 +374,51 @@ fn registered_monitors_keep_their_supplied_host_across_other_windows(cx: &mut Te assert!(second.reading(0)); } +#[gpui_kit::test] +fn registering_the_summary_starts_reading_the_host(cx: &mut TestAppContext) { + let host = FakeHost::new("Linux"); + let (window, workspace, installed) = cx.update(|cx| { + gpui_kit::init(cx); + let ready = nocterm_ui::init( + DesignTokens::builtin(), + (SettingsConfig::in_memory(SettingsDocument::default())).install(cx), + cx, + ); + let installed = crate::install(None, &ready, cx); + let (window, workspace) = + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let deps = + nocterm_workspace::test_support::deps_for(&[&crate::plugin::PLUGIN], cx); + Workspace::new(deps, window, cx) + }) + }) + .unwrap(); + (window, workspace, installed) + }); + open_session(cx, window, &workspace, remote(&host, true)); + cx.run_until_parked(); + assert!(host.programs().is_empty()); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + crate::register(&installed, workspace, window, cx).unwrap(); + }); + }) + .unwrap(); + cx.run_until_parked(); + assert!(host.reading(0)); + cx.update(|cx| { + assert!(workspace.read(cx).is_registered(&crate::plugin::SUMMARY.id)); + }); +} + mod dependencies; + +mod sources; + +mod status_bar_settings; + +mod status_popover; + +#[cfg(feature = "user-plugins")] +mod host_capabilities; diff --git a/crates/monitor-ui/src/tests/dependencies.rs b/crates/monitor-ui/src/tests/dependencies.rs index 2f62e7cb..f709f45f 100644 --- a/crates/monitor-ui/src/tests/dependencies.rs +++ b/crates/monitor-ui/src/tests/dependencies.rs @@ -59,28 +59,44 @@ fn independent_policy_cannot_redirect_an_existing_watch_and_closed_model_is_rele let weak = monitor.downgrade(); cx.update_window(window, |_, window, _| window.remove_window()) .unwrap(); - drop(workspace); - drop(monitor); + // Flush entity release before admitting a new consumer. Merely parking + // the executor does not run GPUI's deferred release callbacks. + cx.update(|_| { + drop(workspace); + drop(monitor); + }); cx.run_until_parked(); assert!(weak.upgrade().is_none()); + assert!( + !host.reading(1), + "the last subscriber releases its native watch" + ); let (_, new_workspace, new_monitor) = cx.update(|cx| { let (window, workspace) = gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { cx.new(|cx| { - Workspace::new( - nocterm_workspace::Deps::new(nocterm_ui::LayoutHandle::in_memory(cx)), - window, - cx, - ) + let deps = + nocterm_workspace::test_support::deps_for(&[&crate::plugin::PLUGIN], cx); + Workspace::new(deps, window, cx) }) }) .unwrap(); - let monitor = cx.new(|cx| HostMonitor::new(&workspace, None, &installed, cx)); + let monitor = cx.new(|cx| { + HostMonitor::new( + &workspace, + workspace.read(cx).monitor_source(cx), + &installed, + cx, + ) + }); (window, workspace, monitor) }); cx.run_until_parked(); - assert!(host.reading(2)); - assert_ne!(status(cx, &new_monitor), Status::Off); + assert_eq!(status(cx, &new_monitor), Status::Starting); + assert!( + host.reading(2), + "a new subscriber gets a fresh native watch" + ); drop(new_workspace); } @@ -122,6 +138,18 @@ fn monitor_page_native_switch_keeps_original_policy_after_unrelated_installation assert!(weak.upgrade().is_none()); } +#[gpui_kit::test] +fn the_monitor_page_leaves_the_status_bar_metrics_to_the_plugins_page(cx: &mut TestAppContext) { + let (_, handle, _) = number_page(SettingsConfig::in_memory(Default::default()), cx); + cx.update_window(handle, |_, window, cx| { + window.render_frame(cx); + assert!(window.try_find("monitor-status-Cpu").is_none()); + window.find("monitor-details-Cpu"); + window.find(("monitor-number", 0usize)); + }) + .unwrap(); +} + fn number_page( store: SettingsConfig, cx: &mut TestAppContext, diff --git a/crates/monitor-ui/src/tests/host_capabilities.rs b/crates/monitor-ui/src/tests/host_capabilities.rs new file mode 100644 index 00000000..16393aad --- /dev/null +++ b/crates/monitor-ui/src/tests/host_capabilities.rs @@ -0,0 +1,183 @@ +//! The monitor's reading reaches a plugin only while the status bar widget shows it. +use super::*; +use crate::{MonitorUsage, register_observed}; +use nocterm_plugin_api::{ + HostError, + capability::monitor::{Usage, UsageNow}, +}; +use nocterm_plugin_enablement::PluginEnablement; +use nocterm_plugin_model::PluginId; +use nocterm_plugin_ports::Observe; + +struct Rig { + settings: nocterm_store::SettingsHandle, + window: gpui_kit::AnyWindowHandle, + workspace: Entity, + installed: crate::Installed, + usage: MonitorUsage, +} + +fn rig(cx: &mut TestAppContext) -> Rig { + cx.update(|cx| { + gpui_kit::init(cx); + let mut document = SettingsDocument::default(); + document.register::(); + let settings = SettingsConfig::in_memory(document).install(cx); + let ready = nocterm_ui::init(DesignTokens::builtin(), settings.clone(), cx); + let installed = crate::install(None, &ready, cx); + let (window, workspace) = + gpui_kit::open_window(WindowOptions::default(), cx, |window, cx| { + cx.new(|cx| { + let deps = + nocterm_workspace::test_support::deps_for(&[&crate::plugin::PLUGIN], cx) + .with_settings(settings.clone()); + Workspace::new(deps, window, cx) + }) + }) + .unwrap(); + Rig { + settings, + window, + workspace, + installed, + usage: MonitorUsage::new(), + } + }) +} + +fn register(rig: &Rig, cx: &mut TestAppContext) { + cx.update_window(rig.window, |_, window, cx| { + rig.workspace.update(cx, |workspace, cx| { + register_observed(&rig.installed, &rig.usage, workspace, window, cx).unwrap(); + }); + }) + .unwrap(); + cx.run_until_parked(); +} + +fn sample(rig: &Rig, cx: &mut TestAppContext) -> Result { + let plugin = PluginId::user("acme", "meter").unwrap(); + cx.update(|cx| Observe::::sample(&rig.usage, &plugin, cx)) +} + +fn frame(host: &FakeHost, busy: u64, idle: u64, cx: &mut TestAppContext) { + host.print(&format!( + "@@stat\ncpu {busy} 0 0 {idle} 0 0 0 0 0 0\n@@meminfo\nMemTotal: 1000\nMemAvailable: 250\n@@end\n" + )); + cx.run_until_parked(); +} + +#[gpui_kit::test] +fn nothing_is_reported_before_the_widget_exists(cx: &mut TestAppContext) { + let rig = rig(cx); + assert_eq!(sample(&rig, cx), Err(HostError::Unavailable)); +} + +#[gpui_kit::test] +fn a_running_monitor_reports_processor_and_memory_use(cx: &mut TestAppContext) { + let rig = rig(cx); + register(&rig, cx); + let host = FakeHost::new("Linux"); + open_session(cx, rig.window, &rig.workspace, remote(&host, true)); + assert_eq!( + sample(&rig, cx), + Err(HostError::Unavailable), + "no reading yet" + ); + + frame(&host, 100, 100, cx); + assert_eq!( + sample(&rig, cx), + Err(HostError::Unavailable), + "a load needs a second reading" + ); + frame(&host, 150, 150, cx); + assert_eq!( + sample(&rig, cx), + Ok(Usage { + cpu_percent: 50.0, + memory_percent: 75.0 + }) + ); +} + +#[gpui_kit::test] +fn a_disconnected_host_reports_nothing(cx: &mut TestAppContext) { + let rig = rig(cx); + register(&rig, cx); + let host = FakeHost::new("Linux"); + let item = open_session(cx, rig.window, &rig.workspace, remote(&host, true)); + frame(&host, 100, 100, cx); + frame(&host, 150, 150, cx); + assert!(sample(&rig, cx).is_ok()); + + item.update(cx, |item, cx| { + item.session.connected = false; + cx.emit(ItemEvent::Changed); + }); + cx.run_until_parked(); + assert_eq!(sample(&rig, cx), Err(HostError::Unavailable)); +} + +#[gpui_kit::test] +fn a_monitor_that_is_switched_off_reports_nothing(cx: &mut TestAppContext) { + let rig = rig(cx); + register(&rig, cx); + let host = FakeHost::new("Linux"); + open_session(cx, rig.window, &rig.workspace, remote(&host, true)); + frame(&host, 100, 100, cx); + frame(&host, 150, 150, cx); + assert!(sample(&rig, cx).is_ok()); + + cx.update(|cx| { + rig.installed + .settings() + .update::(cx, |settings| settings.enabled = false) + .detach() + }); + cx.run_until_parked(); + assert_eq!(sample(&rig, cx), Err(HostError::Unavailable)); +} + +#[gpui_kit::test] +fn sampling_never_starts_a_program(cx: &mut TestAppContext) { + let rig = rig(cx); + let host = FakeHost::new("Linux"); + open_session(cx, rig.window, &rig.workspace, remote(&host, true)); + for _ in 0..3 { + assert_eq!(sample(&rig, cx), Err(HostError::Unavailable)); + } + cx.run_until_parked(); + assert!( + host.programs().is_empty(), + "without the widget the provider cannot start a watch" + ); +} + +#[gpui_kit::test] +fn a_disabled_monitor_plugin_reports_nothing(cx: &mut TestAppContext) { + let rig = rig(cx); + register(&rig, cx); + let host = FakeHost::new("Linux"); + open_session(cx, rig.window, &rig.workspace, remote(&host, true)); + frame(&host, 100, 100, cx); + frame(&host, 150, 150, cx); + assert!(sample(&rig, cx).is_ok()); + + let disabled = PluginEnablement { + disabled: vec![PluginId::new("monitor")], + ..PluginEnablement::default() + }; + let task = cx.update(|cx| { + rig.settings + .update::(cx, move |value| *value = disabled) + }); + cx.run_until_parked(); + cx.foreground_executor().block_test(task).unwrap(); + cx.run_until_parked(); + assert_eq!( + sample(&rig, cx), + Err(HostError::Unavailable), + "the widget is gone, and so is the monitor it owned" + ); +} diff --git a/crates/monitor-ui/src/tests/sources.rs b/crates/monitor-ui/src/tests/sources.rs new file mode 100644 index 00000000..af2350d5 --- /dev/null +++ b/crates/monitor-ui/src/tests/sources.rs @@ -0,0 +1,452 @@ +//! Source selection and cancellation through the real shell projection. +use super::*; +use nocterm_project_model::{ + HostLocator, ProjectId, ProjectTarget, ProviderId, ProviderResourceRef, RootPath, +}; +use nocterm_project_providers::{ContainerLocation, ContainerOrigin}; +use nocterm_session::testing::{ExecResponse, FakeExec, Gate}; +use nocterm_workspace::{ + ProjectSurface, SessionSpec, + test_support::{ProjectSurfaceProbe, SessionProbe}, +}; +use std::rc::Rc; + +fn spec() -> SessionSpec { + SessionSpec { + options: Default::default(), + title: "Inner".into(), + profile: None, + target: target("user@samehost"), + auth: Default::default(), + launch: None, + credential: None, + jumps: vec![], + } +} +fn executor() -> FakeExec { + let exec = FakeExec::default(); + exec.respond("uname", Ok(ExecResponse::text("Linux", 0, ""))); + exec +} +fn container(id: u8) -> ProjectTarget { + // Engine identity is part of the checked provider reference, independent of host. + ContainerLocation::new( + HostLocator::Local, + nocterm_containers::Engine::Docker, + format!("{id:x}").repeat(64), + RootPath::parse("/home").unwrap(), + ContainerOrigin::Attached, + ) + .unwrap() + .to_target() + .unwrap() +} +fn project( + workspace: &Entity, + id: u8, + target: Option, + window: &mut Window, + cx: &mut App, +) -> (Entity, ProjectSurface) { + let factory = workspace.read(cx).project_surface_factory(); + let item = cx.new(|cx| { + ProjectSurfaceProbe::new(factory, ProjectId::from_bytes([id; 16]), window, cx).unwrap() + }); + let surface = item.read(cx).surface(); + if let Some(target) = target { + surface.set_monitor_target(target, cx); + } + workspace.update(cx, |workspace, cx| { + workspace + .add_project_workspace(item.clone(), surface.clone(), window, cx) + .unwrap() + }); + (item, surface) +} +fn inner( + surface: &ProjectSurface, + exec: Option<&FakeExec>, + window: &mut Window, + cx: &mut App, +) -> Entity { + let item = SessionProbe::from_app(spec(), true, None, cx); + if let Some(exec) = exec { + item.update(cx, |item, cx| item.set_executor(Arc::new(exec.clone()), cx)); + } + surface + .add_item(surface.selection_receipt(cx), item.clone(), window, cx) + .unwrap(); + item +} +fn frame(cpu: u64, memory: u64) -> String { + format!( + "@@cgroup\n0::/a\n@@mountinfo\n1 2 0:1 / /cg ro - cgroup2 cgroup rw\n@@selected\ncpu_path /cg/a\nmemory_path /cg/a\ncpu_version 2\nmemory_version 2\n@@container\nclock 10\ncpu_us {cpu}\nmemory {memory}\nlimit 100\n@@end\n" + ) +} +fn print(sink: &ExecSink, text: &str) { + assert!(block_on(sink.send(text.as_bytes().to_vec()))); +} +fn memory(monitor: &Entity, cx: &mut TestAppContext) -> u64 { + monitor.read_with(cx, |monitor, _| { + monitor.sampler().unwrap().snapshot().memory.unwrap().used + }) +} +#[gpui_kit::test] +fn monitor_target_changes_once_and_is_shared_by_surface_handle_clones(cx: &mut TestAppContext) { + let (window, workspace, _) = fixture(cx, None); + let surface = cx + .update_window(window, |_, window, cx| { + project(&workspace, 1, None, window, cx).1 + }) + .unwrap(); + cx.run_until_parked(); + let changes = Rc::new(std::cell::RefCell::new(Vec::new())); + let observed = changes.clone(); + let subscription = cx.update(|cx| { + let workspace = workspace.clone(); + surface.observe(cx, move |event, cx| { + if let nocterm_workspace::ProjectSurfaceEvent::Changed(_) = event { + observed + .borrow_mut() + .push(workspace.read(cx).monitor_source(cx).target().cloned()); + } + }) + }); + let clone = surface.clone(); + cx.update(|cx| clone.set_monitor_target(container(1), cx)); + cx.run_until_parked(); + assert_eq!(&*changes.borrow(), &[Some(container(1))]); + cx.update(|cx| surface.set_monitor_target(container(1), cx)); + cx.run_until_parked(); + assert_eq!(changes.borrow().len(), 1); + cx.update(|cx| surface.set_monitor_target(container(2), cx)); + cx.run_until_parked(); + assert_eq!( + &*changes.borrow(), + &[Some(container(1)), Some(container(2))] + ); + cx.update(|cx| clone.set_monitor_target(container(2), cx)); + cx.run_until_parked(); + assert_eq!(changes.borrow().len(), 2); + drop(subscription); +} +#[gpui_kit::test] +fn selected_inner_container_history_never_uses_parent_or_unrelated_local_host( + cx: &mut TestAppContext, +) { + let local = executor(); + let local_sink = local.queue_stream("sh"); + let (window, workspace, monitor) = fixture(cx, Some(Arc::new(local.clone()))); + cx.run_until_parked(); + assert!(!local_sink.is_closed()); + let first = executor(); + let first_sink = first.queue_stream("sh"); + let (first_tab, first_surface) = cx + .update_window(window, |_, window, cx| { + let (tab, surface) = project(&workspace, 1, Some(container(1)), window, cx); + inner(&surface, Some(&first), window, cx); + (tab, surface) + }) + .unwrap(); + cx.run_until_parked(); + assert!(local_sink.is_closed()); + print(&first_sink, &frame(10, 11)); + cx.run_until_parked(); + assert_eq!(memory(&monitor, cx), 11); + let second = executor(); + let second_sink = second.queue_stream("sh"); + cx.update_window(window, |_, window, cx| { + let (_, surface) = project(&workspace, 2, Some(container(2)), window, cx); + inner(&surface, Some(&second), window, cx); + }) + .unwrap(); + cx.run_until_parked(); + assert!(first_sink.is_closed()); + print(&second_sink, &frame(20, 22)); + cx.run_until_parked(); + assert_eq!(memory(&monitor, cx), 22); + let resumed = first.queue_stream("sh"); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + workspace.activate_item_by_id(first_tab.entity_id(), window, cx) + }) + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(memory(&monitor, cx), 11); + assert!(second_sink.is_closed()); + assert!(!resumed.is_closed()); + assert_eq!( + first + .requests() + .iter() + .filter(|request| request.program == "uname") + .count(), + 1 + ); + assert!( + first + .requests() + .iter() + .all(|request| request.program == "sh" || request.program == "uname") + ); + assert_eq!( + cx.update(|cx| workspace.read(cx).monitor_source(cx).target().cloned()), + Some(container(1)) + ); + drop(first_surface); +} +#[gpui_kit::test] +fn empty_unknown_and_remote_without_execution_are_explicitly_unavailable(cx: &mut TestAppContext) { + let local = executor(); + let sink = local.queue_stream("sh"); + let (window, workspace, monitor) = fixture(cx, Some(Arc::new(local.clone()))); + cx.run_until_parked(); + cx.update_window(window, |_, window, cx| { + project(&workspace, 1, Some(container(1)), window, cx); + }) + .unwrap(); + cx.run_until_parked(); + assert!(sink.is_closed()); + assert_eq!(status(cx, &monitor), Status::Offline); + let unknown = ProjectTarget::Provider { + provider: ProviderId::parse("external.unknown").unwrap(), + resource_version: 1, + reference: ProviderResourceRef::new(serde_json::json!({})).unwrap(), + }; + cx.update_window(window, |_, window, cx| { + let (_, surface) = project(&workspace, 2, Some(unknown), window, cx); + inner(&surface, None, window, cx); + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(status(cx, &monitor), Status::Offline); + assert_eq!(local.requests().len(), 1); + open_session( + cx, + window, + &workspace, + SessionContext::new(target("user@without-exec"), None, true), + ); + assert_eq!(status(cx, &monitor), Status::Offline); + assert_eq!(local.requests().len(), 1); +} +#[gpui_kit::test] +fn hidden_completion_and_away_back_cannot_accept_an_old_platform_or_frame(cx: &mut TestAppContext) { + let (window, workspace, monitor) = fixture(cx, None); + let old = executor(); + let gate = Gate::pending(); + old.suspend(gate.clone()); + let old_sink = old.queue_stream("sh"); + let (tab, surface, item) = cx + .update_window(window, |_, window, cx| { + let (tab, surface) = project(&workspace, 1, Some(container(1)), window, cx); + let item = inner(&surface, Some(&old), window, cx); + (tab, surface, item) + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(status(cx, &monitor), Status::Detecting); + let fresh = executor(); + let fresh_sink = fresh.queue_stream("sh"); + cx.update_window(window, |_, window, cx| { + project(&workspace, 2, None, window, cx); + item.update(cx, |item, cx| { + item.set_executor(Arc::new(fresh.clone()), cx) + }); + }) + .unwrap(); + cx.run_until_parked(); + assert_eq!(status(cx, &monitor), Status::Offline); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + workspace.activate_item_by_id(tab.entity_id(), window, cx) + }) + }) + .unwrap(); + cx.run_until_parked(); + gate.release(); + cx.run_until_parked(); + assert!( + old.requests() + .iter() + .all(|request| request.program == "uname") + ); + print(&fresh_sink, &frame(20, 33)); + cx.run_until_parked(); + assert_eq!(status(cx, &monitor), Status::Running); + assert_eq!(memory(&monitor, cx), 33); + item.update(cx, |item, cx| item.set_connected(false, cx).unwrap()); + cx.run_until_parked(); + assert_eq!(status(cx, &monitor), Status::Offline); + assert!(fresh_sink.is_closed()); + drop((old_sink, surface)); +} + +#[gpui_kit::test] +fn local_and_remote_folder_sources_require_their_actual_selected_authority( + cx: &mut TestAppContext, +) { + use crate::source::watched_source; + use nocterm_workspace::MonitorSource; + let (_, workspace, _) = fixture(cx, None); + let surface = cx.update(|cx| workspace.read(cx).monitor_source(cx).surface()); + let settings = MonitorSettings::default(); + let local: Arc = Arc::new(executor()); + let target = ProjectTarget::HostFolder { + host: HostLocator::Local, + root: RootPath::parse("/home").unwrap(), + }; + let cached = SessionContext::new(super::target("user@previous-remote"), None, true) + .with_exec(Some(Arc::new(executor()))); + let source = MonitorSource::project(surface, Some(target.clone()), Some(cached), true); + assert!(source.session().is_none()); + assert!(Arc::ptr_eq( + &watched_source(&source, Some(&local), &settings) + .unwrap() + .host() + .exec, + &local + )); + assert_eq!( + watched_source(&source, Some(&local), &settings) + .unwrap() + .host() + .key, + HostKey::Local + ); + let empty = MonitorSource::project(surface, Some(target.clone()), None, false); + assert!(watched_source(&empty, Some(&local), &settings).is_none()); + let remote = executor(); + let session = SessionContext::new(super::target("user@folder"), None, true) + .with_exec(Some(Arc::new(remote.clone()))); + let source = MonitorSource::project(surface, Some(target), Some(session), false); + let watched = watched_source(&source, Some(&local), &settings).unwrap(); + assert!(!watched.container()); + assert!(matches!(watched.host().key, HostKey::Remote { .. })); + assert!(remote.requests().is_empty()); +} + +#[gpui_kit::test] +fn closing_the_selected_container_restores_general_monitoring_and_cancels_its_watch( + cx: &mut TestAppContext, +) { + let local = executor(); + let initial = local.queue_stream("sh"); + let (window, workspace, monitor) = fixture(cx, Some(Arc::new(local.clone()))); + cx.run_until_parked(); + assert!(!initial.is_closed()); + let resource = executor(); + let resource_output = resource.queue_stream("sh"); + let tab = cx + .update_window(window, |_, window, cx| { + let (tab, surface) = project(&workspace, 1, Some(container(1)), window, cx); + inner(&surface, Some(&resource), window, cx); + tab + }) + .unwrap(); + cx.run_until_parked(); + assert!(initial.is_closed()); + print(&resource_output, &frame(10, 11)); + cx.run_until_parked(); + assert_eq!(memory(&monitor, cx), 11); + + let resumed = local.queue_stream("sh"); + cx.update_window(window, |_, window, cx| { + workspace.update(cx, |workspace, cx| { + workspace.close_tabs( + tab.entity_id(), + nocterm_workspace::TabCloseScope::Current, + window, + cx, + ); + }); + }) + .unwrap(); + cx.run_until_parked(); + assert!(resource_output.is_closed()); + assert!(!resumed.is_closed()); + cx.update(|cx| { + assert_eq!( + workspace.read(cx).selected_context(cx), + nocterm_project_model::ContextId::General + ) + }); + print( + &resumed, + "@@meminfo\nMemTotal: 1000\nMemAvailable: 250\n@@end\n", + ); + cx.run_until_parked(); + monitor.read_with(cx, |monitor, _| { + let snapshot = monitor.sampler().unwrap().snapshot(); + assert_eq!(snapshot.memory.unwrap().percent(), 75.); + assert!(snapshot.container.is_none()); + }); +} + +#[gpui_kit::test] +fn cpu_only_container_details_render_the_available_cpu_without_memory(cx: &mut TestAppContext) { + use gpui_kit::test::TestWindowExt as _; + let (window, workspace, monitor, installed) = fixture_with_installation(cx, None); + cx.update(|cx| { + installed + .settings() + .update::(cx, |settings| { + settings.status_bar = vec![MonitorMetric::Cpu]; + settings.details = vec![MonitorMetric::Cpu]; + }) + .detach() + }); + cx.run_until_parked(); + let resource = executor(); + let status_output = resource.queue_stream("sh"); + cx.update_window(window, |_, window, cx| { + let (_, surface) = project(&workspace, 1, Some(container(1)), window, cx); + inner(&surface, Some(&resource), window, cx); + }) + .unwrap(); + cx.run_until_parked(); + let details_output = resource.queue_stream("sh"); + monitor.update(cx, |monitor, cx| monitor.set_open(true, cx)); + cx.run_until_parked(); + assert!(status_output.is_closed()); + let (details_window, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |_, cx| { + cx.new(|cx| crate::details::DetailsView::new(monitor.clone(), cx)) + }) + .unwrap() + }); + let first = frame(1000, 0) + .lines() + .filter(|line| !line.starts_with("memory") && !line.starts_with("limit ")) + .collect::>() + .join("\n") + + "\n"; + print(&details_output, &first); + cx.run_until_parked(); + cx.update_window(details_window, |_, window, cx| { + window.render_frame(cx); + assert!( + window + .try_find("host-monitor-section-CPU (one CPU)") + .is_none() + ); + }) + .unwrap(); + print( + &details_output, + &first + .replace("clock 10", "clock 11") + .replace("cpu_us 1000", "cpu_us 501000"), + ); + cx.run_until_parked(); + cx.update_window(details_window, |_, window, cx| { + window.render_frame(cx); + assert_eq!( + window.find("host-monitor-section-CPU (one CPU)").label(), + Some("CPU (one CPU)") + ); + assert!(window.try_find("host-monitor-section-Memory").is_none()); + }) + .unwrap(); +} diff --git a/crates/monitor-ui/src/tests/status_bar_settings.rs b/crates/monitor-ui/src/tests/status_bar_settings.rs new file mode 100644 index 00000000..72fbe0ae --- /dev/null +++ b/crates/monitor-ui/src/tests/status_bar_settings.rs @@ -0,0 +1,209 @@ +//! The status bar metrics as a plugin settings field stay in step with the Monitor page. +use super::*; +use nocterm_plugin_model::{ChoiceOption, FieldEdit, FieldKind, SettingValue, toggle_choice}; +use nocterm_settings::SettingsSection as _; + +fn metrics() -> (Vec, Vec, bool, u8) { + let field = crate::plugin::PLUGIN + .settings + .field("status_bar") + .expect("the monitor declares status_bar"); + match &field.kind { + FieldKind::MultiChoice { + options, + default, + ordered, + max, + } => ( + options.to_vec(), + default.iter().map(ToString::to_string).collect(), + *ordered, + *max, + ), + other => panic!("status_bar is {other:?}"), + } +} + +fn serde_name(metric: MonitorMetric) -> String { + serde_json::to_value(metric) + .unwrap() + .as_str() + .unwrap() + .to_string() +} + +#[test] +fn the_plugin_stores_its_settings_in_the_monitor_section() { + assert_eq!( + crate::plugin::PLUGIN.settings_section, + Some(MonitorSettings::KEY) + ); +} + +#[test] +fn options_and_labels_are_the_monitor_metrics_in_order() { + let (options, _, _, max) = metrics(); + assert_eq!(options.len(), MonitorMetric::ALL.len()); + assert_eq!(usize::from(max), MonitorMetric::ALL.len()); + for (option, metric) in options.iter().zip(MonitorMetric::ALL) { + assert_eq!(option.key, serde_name(metric)); + assert_eq!(option.label, metric.label()); + } +} + +#[test] +fn the_default_is_the_default_monitor_settings() { + let (_, default, _, _) = metrics(); + let expected: Vec = MonitorSettings::default() + .status_bar + .into_iter() + .map(serde_name) + .collect(); + assert_eq!(default, expected); +} + +#[test] +fn toggling_a_metric_matches_the_page_for_every_metric_and_starting_list() { + let (options, _, ordered, _) = metrics(); + assert!(ordered); + let starts: [&[MonitorMetric]; 5] = [ + &[], + &[MonitorMetric::Cpu, MonitorMetric::Memory], + &[MonitorMetric::Memory, MonitorMetric::Cpu], + &[ + MonitorMetric::Network, + MonitorMetric::Swap, + MonitorMetric::Load, + ], + &MonitorMetric::ALL, + ]; + for start in starts { + for metric in MonitorMetric::ALL { + for on in [true, false] { + let mut page = start.to_vec(); + crate::page::toggle(&mut page, metric, on); + let current: Vec = start.iter().copied().map(serde_name).collect(); + let schema = toggle_choice(&options, ¤t, &serde_name(metric), on, ordered); + let page: Vec = page.into_iter().map(serde_name).collect(); + assert_eq!(schema, page, "{start:?} {metric:?} {on}"); + } + } + } +} + +#[gpui_kit::test] +fn writing_through_the_binding_changes_the_status_bar_and_runs_no_program(cx: &mut TestAppContext) { + let host = FakeHost::new("Linux"); + let installed = cx.update(|cx| { + gpui_kit::init(cx); + let ready = nocterm_ui::init( + DesignTokens::builtin(), + SettingsConfig::in_memory(SettingsDocument::default()).install(cx), + cx, + ); + crate::install(Some(host.clone()), &ready, cx) + }); + cx.run_until_parked(); + let source = crate::plugin_settings(&installed); + cx.update(|cx| { + let shown = source.values(cx); + assert_eq!( + shown.get("status_bar"), + Some(&SettingValue::Choices(vec!["cpu".into(), "memory".into()])) + ); + source + .write( + "status_bar", + FieldEdit::Toggle { + option: "swap".into(), + on: true, + }, + cx, + ) + .detach(); + }); + cx.run_until_parked(); + cx.update(|cx| { + assert_eq!( + installed + .settings() + .setting::(cx) + .status_bar, + vec![ + MonitorMetric::Cpu, + MonitorMetric::Memory, + MonitorMetric::Swap + ] + ); + }); + assert!(host.programs().is_empty()); +} + +#[gpui_kit::test] +fn a_write_from_the_form_and_one_from_the_page_both_land(cx: &mut TestAppContext) { + let (_window, _workspace, _monitor, installed) = fixture_with_installation(cx, None); + let source = crate::plugin_settings(&installed); + cx.update(|cx| { + source + .write( + "status_bar", + FieldEdit::Toggle { + option: "load".into(), + on: true, + }, + cx, + ) + .detach(); + installed + .settings() + .update::(cx, |settings| settings.interval_secs = 7) + .detach(); + source + .write( + "status_bar", + FieldEdit::Toggle { + option: "cpu".into(), + on: false, + }, + cx, + ) + .detach(); + }); + cx.run_until_parked(); + cx.update(|cx| { + let monitor = installed.settings().setting::(cx); + assert_eq!(monitor.interval_secs, 7); + assert_eq!( + monitor.status_bar, + vec![MonitorMetric::Memory, MonitorMetric::Load] + ); + }); +} + +#[gpui_kit::test] +fn an_unknown_option_is_refused_and_changes_nothing(cx: &mut TestAppContext) { + let (_window, _workspace, _monitor, installed) = fixture_with_installation(cx, None); + let source = crate::plugin_settings(&installed); + let task = cx.update(|cx| { + source.write( + "status_bar", + FieldEdit::Toggle { + option: "gpu".into(), + on: true, + }, + cx, + ) + }); + cx.run_until_parked(); + let refused = block_on(task).unwrap_err(); + assert_eq!(refused.to_string(), "`gpu` is not one of the options."); + cx.update(|cx| { + assert_eq!( + installed + .settings() + .setting::(cx) + .status_bar, + MonitorSettings::default().status_bar + ); + }); +} diff --git a/crates/monitor-ui/src/tests/status_popover.rs b/crates/monitor-ui/src/tests/status_popover.rs new file mode 100644 index 00000000..73881190 --- /dev/null +++ b/crates/monitor-ui/src/tests/status_popover.rs @@ -0,0 +1,56 @@ +//! The details popover opens toward the inside of the window from the zone that places it. +use super::*; +use crate::status::MonitorStatus; +use gpui_kit::{Bounds, Pixels, px, test::TestWindowExt as _}; +use nocterm_plugin_model::Zone; +use nocterm_workspace::ZoneAware as _; + +/// Shows the summary in the middle of the window, where either side has room. +struct Middle(Entity); + +impl Render for Middle { + fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { + div().flex().pl(px(500.)).pt(px(300.)).child(self.0.clone()) + } +} + +fn details_in(zone: Zone, cx: &mut TestAppContext) -> (Bounds, Bounds) { + let (_, _, monitor) = fixture(cx, Some(FakeHost::new("Linux"))); + cx.run_until_parked(); + let (window, _) = cx.update(|cx| { + gpui_kit::open_window(WindowOptions::default(), cx, |_, cx| { + let status = cx.new(|cx| MonitorStatus::new(monitor.clone(), cx)); + status.update(cx, |status, cx| status.set_zone(zone, cx)); + cx.new(|_| Middle(status)) + }) + .unwrap() + }); + monitor.update(cx, |monitor, cx| monitor.set_open(true, cx)); + cx.run_until_parked(); + cx.update_window(window, |_, window, cx| { + window.render_frame(cx); + ( + window.find("host-monitor").bounds(), + window.find("host-monitor-details").bounds(), + ) + }) + .unwrap() +} + +#[gpui_kit::test] +fn a_left_zone_opens_the_details_rightwards_from_the_summary(cx: &mut TestAppContext) { + let (summary, details) = details_in(Zone::Left, cx); + assert!( + (details.left() - summary.left()).abs() < px(16.), + "{details:?} starts at {summary:?}" + ); +} + +#[gpui_kit::test] +fn a_right_zone_opens_the_details_leftwards_from_the_summary(cx: &mut TestAppContext) { + let (summary, details) = details_in(Zone::Right, cx); + assert!( + (details.right() - summary.right()).abs() < px(16.), + "{details:?} ends at {summary:?}" + ); +} diff --git a/crates/monitor/src/container.rs b/crates/monitor/src/container.rs new file mode 100644 index 00000000..a6abc051 --- /dev/null +++ b/crates/monitor/src/container.rs @@ -0,0 +1,239 @@ +//! Own-cgroup CPU and memory facts; no parent-host counter substitution. +mod membership; +mod sampler; +pub(crate) use sampler::ContainerSampler; +mod script; +use crate::{MetricSet, Reading}; +use nocterm_session::ExecRequest; +use std::time::Duration; + +/// Cumulative CPU nanoseconds and cgroup memory bytes, independent of host totals. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct ContainerCounters { + pub cpu_nanoseconds: Option, + pub cpu_group: Option, + pub memory_current: Option, + /// None means unlimited or unavailable, never host memory capacity. + pub memory_limit: Option, +} +/// Current own-cgroup memory and an explicit one-CPU utilization basis. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct ContainerSnapshot { + pub memory_current: Option, + pub memory_limit: Option, +} +pub(crate) fn request(metrics: MetricSet, interval: Duration) -> ExecRequest { + script::request(metrics, interval) +} +pub(crate) fn parse(frame: &[String]) -> Reading { + let mut counters = ContainerCounters::default(); + let mut clock = None; + let members = section(frame, "@@cgroup"); + let mounts = section(frame, "@@mountinfo"); + let selected = section(frame, "@@selected"); + let cpu_version = value(&selected, "cpu_version"); + let memory_version = value(&selected, "memory_version"); + let cpu = verified( + &members, + &mounts, + &selected, + "cpu_path", + if cpu_version == Some("2") { + "v2" + } else { + "cpuacct" + }, + ); + let memory = verified( + &members, + &mounts, + &selected, + "memory_path", + if memory_version == Some("2") { + "v2" + } else { + "memory" + }, + ); + for line in section(frame, "@@container") { + let Some((key, value)) = line.split_once(' ') else { + continue; + }; + match key { + "clock" => { + clock = value + .parse::() + .ok() + .filter(|clock| clock.is_finite() && *clock >= 0.) + } + "cpu_us" if cpu.is_some() && cpu_version == Some("2") => { + counters.cpu_nanoseconds = value + .parse::() + .ok() + .and_then(|value| value.checked_mul(1000)) + } + "cpu_ns" if cpu.is_some() && cpu_version == Some("1") => { + counters.cpu_nanoseconds = value.parse().ok() + } + "memory" if memory.is_some() => counters.memory_current = value.parse().ok(), + "limit" if memory.is_some() && memory_version == Some("2") => { + counters.memory_limit = value.parse().ok().filter(|value| *value > 0) + } + "limit_v1" if memory.is_some() && memory_version == Some("1") => { + counters.memory_limit = value + .parse() + .ok() + .filter(|value| *value > 0 && *value < (1u64 << 60) && *value != 0x7ffff000) + } + _ => {} + } + } + counters.cpu_group = cpu; + Reading { + clock, + container: Some(counters), + ..Reading::default() + } +} +fn section<'a>(frame: &'a [String], name: &str) -> Vec<&'a str> { + frame + .iter() + .skip_while(|line| line.as_str() != name) + .skip(1) + .take_while(|line| !line.starts_with("@@")) + .map(String::as_str) + .collect() +} +fn value<'a>(section: &[&'a str], key: &str) -> Option<&'a str> { + let mut values = section + .iter() + .filter_map(|line| line.split_once(' ')) + .filter(|(field, _)| *field == key) + .map(|(_, value)| value); + let value = values.next()?; + values.next().is_none().then_some(value) +} +fn verified( + members: &[&str], + mounts: &[&str], + selected: &[&str], + key: &str, + controller: &str, +) -> Option { + let actual = value(selected, key)?; + let expected = membership::resolve(members, mounts, controller)?; + (!actual.is_empty() && actual == expected).then_some(format!("{controller}:{expected}")) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::Sampler; + fn frame(version: u8, path: &str, cpu: u64, memory: &str, limit: &str) -> Vec { + let (member, mount, unit, limit_key) = if version == 2 { + ( + "0::/a", + "1 2 0:1 / /cg ro - cgroup2 cgroup rw", + "cpu_us", + "limit", + ) + } else { + ( + "2:cpuacct,memory:/a", + "1 2 0:1 / /cg ro - cgroup cgroup rw,cpuacct,memory", + "cpu_ns", + "limit_v1", + ) + }; + format!("@@cgroup\n{member}\n@@mountinfo\n{mount}\n@@selected\ncpu_path {path}\nmemory_path {path}\ncpu_version {version}\nmemory_version {version}\n@@container\nclock 10\n{unit} {cpu}\nmemory {memory}\n{limit_key} {limit}\n").lines().map(str::to_owned).collect() + } + #[test] + fn v2_and_v1_use_their_declared_cpu_units_and_memory_scope() { + for (version, cpu) in [(2, 123), (1, 123000)] { + let reading = parse(&frame(version, "/cg/a", cpu, "55", "100")); + let counters = reading.container.unwrap(); + assert_eq!(counters.cpu_nanoseconds, Some(123000)); + assert_eq!(counters.memory_current, Some(55)); + assert_eq!(counters.memory_limit, Some(100)); + assert!( + reading.cpu.is_none() + && reading.memory.is_none() + && reading.cores.is_none() + && reading.load.is_none() + && reading.uptime.is_none() + ); + } + } + #[test] + fn limits_and_invalid_membership_never_substitute_parent_host_capacity() { + for (version, limit) in [(2, "max"), (1, "9223372036854771712"), (1, "2147479552")] { + let reading = parse(&frame(version, "/cg/a", 10, "55", limit)); + let counters = reading.container.unwrap(); + assert_eq!(counters.memory_limit, None); + assert_eq!(counters.memory_current, Some(55)); + } + let reading = parse(&frame(2, "/cg", 10, "55", "100")); + let counters = reading.container.unwrap(); + assert_eq!(counters.cpu_nanoseconds, None); + assert_eq!(counters.memory_current, None); + let mut missing = frame(2, "/cg/a", 10, "55", "100"); + missing.retain(|line| !line.starts_with("1 2")); + assert_eq!( + parse(&missing).container.unwrap(), + ContainerCounters::default() + ); + } + fn reading(clock: f64, usage: u64, group: &str, limit: Option) -> Reading { + Reading { + clock: Some(clock), + container: Some(ContainerCounters { + cpu_nanoseconds: Some(usage), + cpu_group: Some(group.into()), + memory_current: Some(55), + memory_limit: limit, + }), + ..Reading::default() + } + } + #[test] + fn one_cpu_basis_can_exceed_one_hundred_and_resets_on_clock_counter_or_group_changes() { + let mut sampler = Sampler::new(8); + sampler.apply(reading(10., 1_000_000_000, "a", Some(100))); + sampler.apply(reading(11., 3_000_000_000, "a", Some(100))); + assert_eq!(sampler.snapshot().cpu, Some(200.)); + assert_eq!(sampler.snapshot().memory.unwrap().percent(), 55.); + sampler.apply(reading(12., 1, "a", None)); + assert_eq!(sampler.snapshot().cpu, None); + assert_eq!(sampler.snapshot().memory, None); + assert_eq!( + sampler + .snapshot() + .container + .as_ref() + .unwrap() + .memory_current, + Some(55) + ); + sampler.apply(reading(13., 1_000_000_001, "b", None)); + assert_eq!(sampler.snapshot().cpu, None); + sampler.apply(reading(1., 2_000_000_001, "b", None)); + assert_eq!(sampler.snapshot().cpu, None); + sampler.apply(Reading::default()); + assert!(sampler.snapshot().container.is_none()); + } + #[test] + fn resource_script_never_reads_host_metric_sources_or_unrequested_counters() { + let script = script::request( + [crate::MonitorMetric::Memory].iter().collect(), + Duration::from_secs(5), + ); + let script = String::from_utf8(script.stdin.unwrap()).unwrap(); + assert!(!script.contains("/proc/stat")); + assert!(!script.contains("/proc/meminfo")); + assert!(!script.contains("/proc/loadavg")); + assert!(!script.contains("cpu_us %s")); + assert!(script.contains("memory.current")); + assert!(script.contains("cgroup.procs")); + assert!(script.contains("/proc/self/mountinfo")); + } +} diff --git a/crates/monitor/src/container/membership.rs b/crates/monitor/src/container/membership.rs new file mode 100644 index 00000000..90c81a33 --- /dev/null +++ b/crates/monitor/src/container/membership.rs @@ -0,0 +1,133 @@ +//! Checked mapping of own cgroup membership into one visible controller mount. +/// Ambiguous/malformed/escaped mounts never select a parent-root fallback. +pub(super) fn resolve(members: &[&str], mounts: &[&str], controller: &str) -> Option { + if members.len() > 256 || mounts.len() > 4096 { + return None; + } + let memberships: Vec<_> = members + .iter() + .filter_map(|line| { + let mut fields = line.splitn(3, ':'); + let hierarchy = fields.next()?; + let controllers = fields.next()?; + let path = fields.next()?; + ((controller == "v2" && hierarchy == "0" && controllers.is_empty()) + || controllers.split(',').any(|item| item == controller)) + .then_some(path) + }) + .collect(); + if memberships.len() != 1 { + return None; + } + let mut found = None; + for line in mounts { + if line.len() > 16384 { + return None; + } + let (before, after) = line.split_once(" - ")?; + let fields: Vec<_> = before.split_whitespace().collect(); + let filesystem: Vec<_> = after.split_whitespace().collect(); + if fields.len() < 6 || filesystem.len() < 3 { + return None; + } + let matches = if controller == "v2" { + filesystem[0] == "cgroup2" + } else { + filesystem[0] == "cgroup" && filesystem[2].split(',').any(|item| item == controller) + }; + if !matches { + continue; + } + if found.is_some() { + return None; + } + let root = decode(fields[3])?; + let mount = decode(fields[4])?; + let member = memberships[0]; + if !valid(member) || !valid(&root) || !valid(&mount) { + return None; + } + let relative = if root == "/" { + member + } else if member == root { + "/" + } else { + member + .strip_prefix(&format!("{root}/")) + .map_or(member, |rest| rest) + }; + let joined = if relative == "/" { + mount + } else { + format!( + "{}/{}", + mount.trim_end_matches('/'), + relative.trim_start_matches('/') + ) + }; + found = Some(joined); + } + found +} +fn valid(path: &str) -> bool { + path.starts_with('/') + && path.len() <= 4096 + && !path.contains(['\\', '\0', '\n', '\r']) + && !path.split('/').any(|part| matches!(part, "." | "..")) +} +fn decode(value: &str) -> Option { + let value = value.replace("\\040", " "); + valid(&value).then_some(value) +} + +#[cfg(test)] +mod tests { + use super::*; + fn mount(root: &str, mount: &str, fs: &str, options: &str) -> String { + format!("1 2 0:1 {root} {mount} ro - {fs} cgroup {options}") + } + #[test] + fn namespace_root_and_subtree_mounts_resolve_the_actual_membership() { + for (member, root, expected) in [ + ("/", "/docker/id", "/cg"), + ("/child", "/docker/id", "/cg/child"), + ("/docker/id/child", "/docker/id", "/cg/child"), + ("/docker/id", "/", "/cg/docker/id"), + ] { + assert_eq!( + resolve( + &[&format!("0::{member}")], + &[&mount(root, "/cg", "cgroup2", "rw")], + "v2" + ) + .as_deref(), + Some(expected) + ); + } + } + #[test] + fn separate_v1_controllers_and_escaped_spaces_are_exact() { + let mounts = [ + mount("/", "/cpu\\040group", "cgroup", "rw,cpu,cpuacct"), + mount("/", "/memory", "cgroup", "rw,memory"), + ]; + let mounts: Vec<_> = mounts.iter().map(String::as_str).collect(); + let members = ["2:cpu,cpuacct:/a", "3:memory:/b"]; + assert_eq!( + resolve(&members, &mounts, "cpuacct").as_deref(), + Some("/cpu group/a") + ); + assert_eq!( + resolve(&members, &mounts, "memory").as_deref(), + Some("/memory/b") + ); + } + #[test] + fn ambiguous_missing_and_outside_namespace_memberships_are_refused() { + let one = mount("/", "/cg", "cgroup2", "rw"); + assert!(resolve(&["0::/a"], &[&one, &one], "v2").is_none()); + assert!(resolve(&["0::/a", "0::/b"], &[&one], "v2").is_none()); + assert!(resolve(&["0::/../outside"], &[&one], "v2").is_none()); + assert!(resolve(&["0::/a"], &[], "v2").is_none()); + } +} diff --git a/crates/monitor/src/container/sampler.rs b/crates/monitor/src/container/sampler.rs new file mode 100644 index 00000000..125abc02 --- /dev/null +++ b/crates/monitor/src/container/sampler.rs @@ -0,0 +1,50 @@ +use super::{ContainerCounters, ContainerSnapshot}; +use crate::Usage; + +#[derive(Clone, Debug, Default)] +pub(crate) struct ContainerSampler { + previous: Option<(f64, u64, String)>, +} +pub(crate) struct Sample { + pub(crate) cpu: Option, + pub(crate) memory: Option, + pub(crate) facts: ContainerSnapshot, +} +impl ContainerSampler { + pub(crate) fn sample( + &mut self, + clock: Option, + counters: Option, + ) -> Option { + let previous = self.previous.take(); + let counters = counters?; + let cpu = clock + .zip(counters.cpu_nanoseconds) + .zip(counters.cpu_group) + .and_then(|((clock, usage), group)| { + self.previous = Some((clock, usage, group.clone())); + let (then, before, previous_group) = previous?; + if previous_group != group { + return None; + } + let elapsed = clock - then; + let delta = usage.checked_sub(before)?; + if elapsed <= 0. || !elapsed.is_finite() { + return None; + } + let percent = delta as f64 / 1_000_000_000. / elapsed * 100.; + (percent.is_finite() && percent <= f64::from(f32::MAX)).then_some(percent as f32) + }); + Some(Sample { + cpu, + memory: counters + .memory_current + .zip(counters.memory_limit) + .map(|(used, total)| Usage { used, total }), + facts: ContainerSnapshot { + memory_current: counters.memory_current, + memory_limit: counters.memory_limit, + }, + }) + } +} diff --git a/crates/monitor/src/container/script.rs b/crates/monitor/src/container/script.rs new file mode 100644 index 00000000..96432b3a --- /dev/null +++ b/crates/monitor/src/container/script.rs @@ -0,0 +1,99 @@ +//! Linux cgroup accounting only. CPU is explicitly measured against one CPU. +use crate::{MetricSet, MonitorMetric}; +use nocterm_session::ExecRequest; +use std::time::Duration; + +const RESOLVE: &str = r#"resolve() { +awk -v controller="$1" ' +function has(list,wanted){return index("," list ",","," wanted ",")!=0} +function path(value){gsub(/\\040/," ",value); return value} +FILENAME=="/proc/self/cgroup" { + if(length($0)>8192 || ++members>256) exit 1 + first=index($0,":"); rest=substr($0,first+1); second=index(rest,":") + controllers=substr(rest,1,second-1); name=substr(rest,second+1) + if((controller=="v2" && substr($0,1,first-1)=="0" && controllers=="") || has(controllers,controller)) { + member=name; memberships++ + } + next +} +{ + if(length($0)>16384 || ++mounts>4096) exit 1 + parts=split($0,halves," - "); if(parts!=2) next + split(halves[2],post," "); split(halves[1],pre," ") + if((controller=="v2" && post[1]=="cgroup2") || (post[1]=="cgroup" && has(post[3],controller))) { + root=path(pre[4]); mount=path(pre[5]); matches++ + } +} +END { + if(memberships!=1 || matches!=1 || member!~/^\// || root!~/^\// || mount!~/^\//) exit 1 + if(member~/\\/ || root~/\\/ || mount~/\\/ || member~/(^|\/)\.\.?($|\/)/) exit 1 + if(root=="/") relative=member + else if(member==root) relative="/" + else if(index(member,root "/")==1) relative=substr(member,length(root)+1) + else relative=member + if(relative=="/") relative="" + printf "%s%s\n",mount,relative +}' /proc/self/cgroup /proc/self/mountinfo +} +member() { + [ -r "$1/cgroup.procs" ] || return 1 + n=0 + while read -r pid; do + [ "$pid" = "$$" ] && return 0 + n=$((n+1)); [ "$n" -le 4096 ] || return 1 + done <"$1/cgroup.procs" + return 1 +} +V2=$(resolve v2) || V2= +CPU=; MEM=; CV=1; MV=1 +if [ -n "$V2" ] && member "$V2"; then + [ ! -r "$V2/cpu.stat" ] || { CPU=$V2; CV=2; } + [ ! -r "$V2/memory.current" ] || { MEM=$V2; MV=2; } +fi +if [ -z "$CPU" ]; then CPU=$(resolve cpuacct) || CPU=; [ -z "$CPU" ] || member "$CPU" || CPU=; fi +if [ -z "$MEM" ]; then MEM=$(resolve memory) || MEM=; [ -z "$MEM" ] || member "$MEM" || MEM=; fi +"#; +const CPU: &str = r#"if [ -n "$CPU" ] && member "$CPU"; then + if [ "$CV" = 2 ]; then + n=0; while read -r k v; do n=$((n+1)); [ "$n" -le 64 ] || break; [ "$k" != usage_usec ] || printf 'cpu_us %s\n' "$v"; done <"$CPU/cpu.stat" + else read -r v <"$CPU/cpuacct.usage" && printf 'cpu_ns %s\n' "$v" + fi +fi +"#; +const MEMORY: &str = r#"if [ -n "$MEM" ] && member "$MEM"; then + if [ "$MV" = 2 ]; then + read -r v <"$MEM/memory.current" && printf 'memory %s\n' "$v" + read -r v <"$MEM/memory.max" && printf 'limit %s\n' "$v" + else + read -r v <"$MEM/memory.usage_in_bytes" && printf 'memory %s\n' "$v" + read -r v <"$MEM/memory.limit_in_bytes" && printf 'limit_v1 %s\n' "$v" + fi +fi +"#; +const PROOF: &str = r#"proof() { +printf '@@cgroup\n' +awk 'NR>256 || length($0)>8192 {print "invalid";exit} {print}' /proc/self/cgroup +printf '@@mountinfo\n' +awk 'NR>4096 || length($0)>16384 {print "invalid";exit} {print}' /proc/self/mountinfo +printf '@@selected\ncpu_path %s\nmemory_path %s\ncpu_version %s\nmemory_version %s\n' "$CPU" "$MEM" "$CV" "$MV" +} +"#; +pub(super) fn request(metrics: MetricSet, interval: Duration) -> ExecRequest { + let mut source = String::from("{\nexec /dev/null\nexport LC_ALL=C\n"); + source.push_str(RESOLVE); + source.push_str(PROOF); + source.push_str( + "f() {\nproof +printf '@@container\\n'\nread -r clock _ Option { match self { Self::Linux => Some(linux::request(metrics, interval)), + Self::LinuxContainer => Some(crate::container::request(metrics, interval)), Self::Windows => Some(windows::request(metrics, interval)), Self::Unsupported(_) => None, } @@ -31,6 +34,7 @@ impl Platform { pub(crate) fn parse(&self, frame: &[String]) -> Reading { match self { Self::Linux => linux::parse(frame), + Self::LinuxContainer => crate::container::parse(frame), Self::Windows => windows::parse(frame), Self::Unsupported(_) => Reading::default(), } diff --git a/crates/monitor/src/reading.rs b/crates/monitor/src/reading.rs index f704e701..d876fd1b 100644 --- a/crates/monitor/src/reading.rs +++ b/crates/monitor/src/reading.rs @@ -7,6 +7,7 @@ pub struct Reading { /// Seconds on the host's monotonic clock when the frame was taken; the /// time base for rates. pub clock: Option, + pub container: Option, pub host: Option, pub cpu: Option, /// One entry per core, in the host's order. diff --git a/crates/monitor/src/sampler.rs b/crates/monitor/src/sampler.rs index 5acd7c15..c09def33 100644 --- a/crates/monitor/src/sampler.rs +++ b/crates/monitor/src/sampler.rs @@ -10,6 +10,7 @@ use crate::{ #[derive(Clone, Debug, Default, PartialEq)] pub struct Snapshot { pub host: Option, + pub container: Option, /// Total processor load, in percent. pub cpu: Option, /// Load of every core, in percent. @@ -80,6 +81,7 @@ pub struct Sampler { snapshot: Snapshot, history: History, cpu: Option, + container: crate::container::ContainerSampler, cores: Option>, network: Option<(f64, NetworkCounters)>, disk_io: Option<(f64, DiskCounters)>, @@ -132,6 +134,12 @@ impl Sampler { .zip(self.cpu) .and_then(|(now, before)| now.load_since(before)); self.cpu = reading.cpu; + if let Some(container) = self.container.sample(clock, reading.container) { + snapshot.cpu = container.cpu; + snapshot.memory = container.memory; + snapshot.container = Some(container.facts); + } + snapshot.cores = match (&reading.cores, &self.cores) { (Some(now), Some(before)) if now.len() == before.len() => Some( now.iter() diff --git a/crates/plugin-abi/Cargo.toml b/crates/plugin-abi/Cargo.toml new file mode 100644 index 00000000..026591ad --- /dev/null +++ b/crates/plugin-abi/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "nocterm-plugin-abi" +description = "The guest ABI of code plugins: export names, call and reply wire types and their postcard encoding." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "domain" + +# The crate is `no_std` with `alloc` (a guest links it too), so these do not take the workspace +# entries, which keep the default `std` features; the isolation check lists the direct +# dependencies. +[dependencies] +nocterm-plugin-api.workspace = true +nocterm-plugin-model.workspace = true +postcard = { version = "1.1.3", default-features = false, features = ["alloc"] } +serde = { version = "1", default-features = false, features = ["alloc", "derive"] } +thiserror = { version = "2", default-features = false } + +[lints] +workspace = true diff --git a/crates/plugin-abi/abi-golden.json b/crates/plugin-abi/abi-golden.json new file mode 100644 index 00000000..605685f1 --- /dev/null +++ b/crates/plugin-abi/abi-golden.json @@ -0,0 +1,48 @@ +{ + "abi_version": 1, + "exports": { + "memory": "memory", + "nocterm_abi_version": "() -> i32", + "nocterm_alloc": "(i32) -> i32", + "nocterm_call": "(i32, i32) -> i64" + }, + "samples": { + "call-render-empty": "0003626172000000010000", + "call-render-full": "00036261720609636c6f636b2e6e6f7700008085d41ba0380e776f726b73706163652e6b696e64000101010d6d6f6e69746f722e757361676500020000484100002042107472616e73666572732e636f756e747300030201116e6f74696669636174696f6e732e6f776e0004010104446f6e65096c617465722e636170010305026f6e0001046d6f6465010466617374047365747302020161016205636f756e74030d046369747904044f736c6f010573746172740a3137303030303030303000020401046f70656e00000102fa010104706f73740100000101", + "call-event-activated": "010570616e656c0002676f0609636c6f636b2e6e6f7700008085d41ba0380e776f726b73706163652e6b696e64000101010d6d6f6e69746f722e757361676500020000484100002042107472616e73666572732e636f756e747300030201116e6f74696669636174696f6e732e6f776e0004010104446f6e65096c617465722e636170010305026f6e0001046d6f6465010466617374047365747302020161016205636f756e74030d046369747904044f736c6f010573746172740a3137303030303030303000020401046f70656e00000102fa010104706f73740100000101", + "call-event-toggled": "010570616e656c0104666c6167010609636c6f636b2e6e6f7700008085d41ba0380e776f726b73706163652e6b696e64000101010d6d6f6e69746f722e757361676500020000484100002042107472616e73666572732e636f756e747300030201116e6f74696669636174696f6e732e6f776e0004010104446f6e65096c617465722e636170010305026f6e0001046d6f6465010466617374047365747302020161016205636f756e74030d046369747904044f736c6f010573746172740a3137303030303030303000020401046f70656e00000102fa010104706f73740100000101", + "call-event-visibility": "010570616e656c02000609636c6f636b2e6e6f7700008085d41ba0380e776f726b73706163652e6b696e64000101010d6d6f6e69746f722e757361676500020000484100002042107472616e73666572732e636f756e747300030201116e6f74696669636174696f6e732e6f776e0004010104446f6e65096c617465722e636170010305026f6e0001046d6f6465010466617374047365747302020161016205636f756e74030d046369747904044f736c6f010573746172740a3137303030303030303000020401046f70656e00000102fa010104706f73740100000101", + "call-event-settings": "010570616e656c030609636c6f636b2e6e6f7700008085d41ba0380e776f726b73706163652e6b696e64000101010d6d6f6e69746f722e757361676500020000484100002042107472616e73666572732e636f756e747300030201116e6f74696669636174696f6e732e6f776e0004010104446f6e65096c617465722e636170010305026f6e0001046d6f6465010466617374047365747302020161016205636f756e74030d046369747904044f736c6f010573746172740a3137303030303030303000020401046f70656e00000102fa010104706f73740100000101", + "reply-view-next-second": "000b000001000100000001010100030402010100020531323a3330030101000428000101000501330001010102676f0602476f0001010104666c6167070100010000080001080009000108000a000100010872656e6465726564", + "reply-view-next-minute": "000b000001000100000001010100030402010100020531323a3330030101000428000101000501330001010102676f0602476f0001010104666c6167070100010000080001080009000108000a00010100", + "reply-view-after-ms": "000b000001000100000001010100030402010100020531323a3330030101000428000101000501330001010102676f0602476f0001010104666c6167070100010000080001080009000108000a000102f40300", + "reply-effects": "010501046e6f746500000548656c6c6f010568656c6c6f00010570616e656c0100020570616e656c0103010473617665030573746172740a3137303030303030303000040573746172740201610162", + "reply-effects-none": "010000", + "reply-view-icons": "0007000000000100000300000100000301000100000302000100000303000100000304000100000305000000", + "call-render-workspace-kinds": "00036261720410776f726b73706163652e6b696e642e300001000110776f726b73706163652e6b696e642e310001010010776f726b73706163652e6b696e642e320001020110776f726b73706163652e6b696e642e33000103000000010000", + "call-render-zone-left": "0003626172000000000000", + "call-render-zone-center": "0003626172000000000100", + "call-render-zone-right": "0003626172000000000200", + "call-event-response": "010570616e656c040773657276657273c8010e7b2273657276657273223a7b7d7d000000010000", + "call-event-timer": "010570616e656c0504706f6c6c000000010000", + "call-event-terminal-line": "010570616e656c060373727606736179206869000000010000", + "call-event-terminal-closed": "010570616e656c0703737276000000010000", + "reply-effects-sessions": "01070102723005000a2f6170692f6974656d7300000102723105010a2f6170692f6974656d7301127b22616374696f6e223a227374617274227d000102723205020a2f6170692f6974656d7300000102723305030a2f6170692f6974656d7300000104706f6c6c06d00f00070570616e656c03737276054275696c64000008037372760f1b5b33326d72656164791b5b306d0a00", + "reply-view-code": "000100000b07610a2020202062010000", + "reply-view-glyphs": "004200000000010000030000010000030100010000030200010000030300010000030400010000030500010000030600010000030700010000030800010000030900010000030a00010000030b00010000030c00010000030d00010000030e00010000030f00010000031000010000031100010000031200010000031300010000031400010000031500010000031600010000031700010000031800010000031900010000031a00010000031b00010000031c00010000031d00010000031e00010000031f00010000032000010000032100010000032200010000032300010000032400010000032500010000032600010000032700010000032800010000032900010000032a00010000032b00010000032c00010000032d00010000032e00010000032f00010000033000010000033100010000033200010000033300010000033400010000033500010000033600010000033700010000033800010000033900010000033a00010000033b00010000033c00010000033d00010000033e00010000033f000100000340000000", + "reply-view-components": "0005000107736572766572730c07536572766572730101320000010001046f70656e0d054275696c64010c6f6e20c2b7206e6f64652031013201020101010473746f700e360453746f70000100000f0a6f6e65206973206f666601010000100343505503333825030000", + "call-event-file-list": "010570616e656c080373727600000e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-stat": "010570616e656c080373727601010e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-read": "010570616e656c080373727602020e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-write": "010570616e656c080373727603030e2f706c7567696e732f612e796d6c045457467501000000010000", + "call-event-file-create-dir": "010570616e656c080373727604040e2f706c7567696e732f612e796d6c000000010000", + "call-event-file-remove": "010570616e656c080373727605050e2f706c7567696e732f612e796d6c00000000010000", + "call-event-file-rename": "010570616e656c080373727606060e2f706c7567696e732f612e796d6c062f622e796d6c000000010000", + "reply-effects-files": "010800090373727600000105612e796d6c00010c0180e2cfaa0600090373727601010105612e796d6c00010c0180e2cfaa0600090373727602020454574675000903737276030300090373727604040000090373727605040404627573790104726561640501062f77726974650104545746750100070570616e656c03737276054275696c640100", + "call-render-toolbar": "0003626172000000020100", + "call-render-terminal-bar": "020570616e656c03737276000000020100", + "call-event-terminal-bar": "030570616e656c03737276000772657374617274000000020100", + "reply-effects-terminal-bar": "0105000a037372760100000a037372760101000a037372760102000a037372760103000a037372760000", + "reply-view-charts": "0003000001000100001101030c1e240164013c00010000110203040907030203050000030000" + } +} diff --git a/crates/plugin-abi/src/codec.rs b/crates/plugin-abi/src/codec.rs new file mode 100644 index 00000000..b68e0aa1 --- /dev/null +++ b/crates/plugin-abi/src/codec.rs @@ -0,0 +1,61 @@ +//! Encoding and decoding of calls and replies, with the size bounds of the ABI. + +use alloc::vec::Vec; + +use serde::{Serialize, de::DeserializeOwned}; +use thiserror::Error; + +use crate::{Call, MAX_INPUT_BYTES, MAX_REPLY_BYTES, Reply}; + +/// Why bytes are not a call or a reply. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum AbiError { + #[error("the value could not be encoded")] + Encode(#[source] postcard::Error), + #[error("the bytes are not a valid value of the ABI")] + Decode(#[source] postcard::Error), + #[error("the encoding is larger than {max} bytes")] + TooLarge { max: usize }, + #[error("bytes remain after the value")] + Trailing, +} + +fn encode(value: &T, max: usize) -> Result, AbiError> { + let bytes = postcard::to_allocvec(value).map_err(AbiError::Encode)?; + if bytes.len() > max { + return Err(AbiError::TooLarge { max }); + } + Ok(bytes) +} + +fn decode(bytes: &[u8], max: usize) -> Result { + if bytes.len() > max { + return Err(AbiError::TooLarge { max }); + } + let (value, rest) = postcard::take_from_bytes(bytes).map_err(AbiError::Decode)?; + if rest.is_empty() { + Ok(value) + } else { + Err(AbiError::Trailing) + } +} + +/// The bytes the host writes into guest memory, at most [`MAX_INPUT_BYTES`]. +pub fn encode_call(call: &Call) -> Result, AbiError> { + encode(call, MAX_INPUT_BYTES) +} + +/// A call as the guest reads it. +pub fn decode_call(bytes: &[u8]) -> Result { + decode(bytes, MAX_INPUT_BYTES) +} + +/// The bytes a guest returns, at most [`MAX_REPLY_BYTES`]. +pub fn encode_reply(reply: &Reply) -> Result, AbiError> { + encode(reply, MAX_REPLY_BYTES) +} + +/// A reply as the host reads it. +pub fn decode_reply(bytes: &[u8]) -> Result { + decode(bytes, MAX_REPLY_BYTES) +} diff --git a/crates/plugin-abi/src/lib.rs b/crates/plugin-abi/src/lib.rs new file mode 100644 index 00000000..95cdcd92 --- /dev/null +++ b/crates/plugin-abi/src/lib.rs @@ -0,0 +1,66 @@ +//! The ABI between the host and a code plugin: export names, the call and reply wire types and +//! their postcard encoding. Shared by the wasm runtime adapter and the guest SDK. +//! +//! Owns: ABI_VERSION, export names, Call/Reply/Wire* and their conversions from plugin-api +//! values (`Call::render`/`event`/`into_parts` carry a terminal bar's terminal in its own +//! call variants), size bounds, canonical samples for the golden file. +//! Does not own: running modules (nocterm-plugin-wasm), package files (nocterm-plugin-packages), +//! manifests and views (nocterm-plugin-api). +//! Ports: none. Settings: none. Globals: none. +//! Invariants: no_std + alloc; wire enums are append-only; any change to an existing sample's +//! bytes bumps ABI_VERSION (xtask). Under the xtask isolation rules (I1-I5). +//! +//! A guest exports `memory`, `nocterm_abi_version`, `nocterm_alloc` and `nocterm_call` +//! ([`names`]) and imports nothing. The host writes an encoded [`Call`] into the buffer +//! `nocterm_alloc` returns, calls `nocterm_call(ptr, len)` and reads an encoded [`Reply`] at the +//! `(ptr, len)` pair that [`unpack`] takes out of the returned `i64`. +#![no_std] + +extern crate alloc; +#[cfg(test)] +extern crate std; + +mod codec; +pub mod names; +mod samples; +mod wire; + +pub use codec::{AbiError, decode_call, decode_reply, encode_call, encode_reply}; +pub use samples::{Sample, samples}; +pub use wire::{Answer, Call, Reply, WireObserved, WireSetting, WireSnapshot}; + +/// The version of this ABI. A change to the bytes of any existing sample bumps it. +pub const ABI_VERSION: u32 = 1; +/// The largest encoded call the host writes into guest memory. +pub const MAX_INPUT_BYTES: usize = 256 * 1024; +/// The largest encoded reply the host reads from guest memory. The host decodes a reply on the +/// calling thread, so the bound is also a bound on that work; the largest valid view (256 nodes +/// of text) is about 200 KiB. +pub const MAX_REPLY_BYTES: usize = 256 * 1024; +/// Most log lines a reply carries. A reply with more is malformed and counts as a fault. +pub const MAX_LOG_LINES: usize = 8; +/// Characters of one log line the host keeps; it cuts the rest. +pub const MAX_LOG_LINE_CHARS: usize = 200; + +/// Fuel one guest call may burn before the host stops it (`Deadline`). The spike measured about +/// 1.2 M for a 256-node render. +pub const CALL_FUEL: u64 = 4_000_000; +/// Fuel the instantiation and the version check may burn. +pub const START_FUEL: u64 = 20_000_000; +/// The most linear memory one instance may hold, in bytes. +pub const MEMORY_BYTES: usize = 16 * 1024 * 1024; +/// The most live instances of code plugins, which bounds guest memory at 256 MiB. +pub const MAX_LIVE_INSTANCES: usize = 16; + +/// Packs a guest pointer and a length into the `i64` bits `nocterm_call` returns. +pub const fn pack(ptr: u32, len: u32) -> u64 { + ((ptr as u64) << 32) | len as u64 +} + +/// The pointer and length inside a value made by [`pack`]. +pub const fn unpack(packed: u64) -> (u32, u32) { + ((packed >> 32) as u32, packed as u32) +} + +#[cfg(test)] +mod tests; diff --git a/crates/plugin-abi/src/names.rs b/crates/plugin-abi/src/names.rs new file mode 100644 index 00000000..ee7ff74b --- /dev/null +++ b/crates/plugin-abi/src/names.rs @@ -0,0 +1,20 @@ +//! The exports of a guest module. A module has no imports. + +/// The linear memory the host reads and writes. +pub const EXPORT_MEMORY: &str = "memory"; +/// `() -> i32`: the ABI version the guest was built for. +pub const EXPORT_VERSION: &str = "nocterm_abi_version"; +/// `(i32) -> i32`: a buffer of the given length for the next call; the pointer stays valid +/// until the next `nocterm_alloc` or `nocterm_call`. +pub const EXPORT_ALLOC: &str = "nocterm_alloc"; +/// `(i32, i32) -> i64`: handles the call at `(ptr, len)` and returns the packed `(ptr, len)` of +/// the reply. +pub const EXPORT_CALL: &str = "nocterm_call"; + +/// Every export with its signature, in the order the documentation lists them. +pub const EXPORTS: [(&str, &str); 4] = [ + (EXPORT_MEMORY, "memory"), + (EXPORT_VERSION, "() -> i32"), + (EXPORT_ALLOC, "(i32) -> i32"), + (EXPORT_CALL, "(i32, i32) -> i64"), +]; diff --git a/crates/plugin-abi/src/samples.rs b/crates/plugin-abi/src/samples.rs new file mode 100644 index 00000000..7915cf57 --- /dev/null +++ b/crates/plugin-abi/src/samples.rs @@ -0,0 +1,598 @@ +//! Canonical values, encoded. xtask writes them to `abi-golden.json`; a change to the bytes of +//! an existing sample is a change of the ABI and needs a new `ABI_VERSION`. + +use alloc::{string::String, vec, vec::Vec}; + +use nocterm_plugin_api::{ + ContributionContext, Effect, EffectOutcome, EffectRequest, Effects, Event, Glyph, HostError, + Kind, LocalId, Node, NodeKind, PluginIcon, Refresh, Tone, View, Zone, + capability::{ + clock::ClockNow, + monitor::Usage, + net::{Method, Request}, + notifications::{NoticeTone, OwnNotice, OwnNotices, Post}, + panels::{SetBadge, SetOpen}, + storage::{Remove, Set}, + terminals::{Open, Write}, + timers::SetTimer, + transfers::Counts, + workspace::{WorkspaceKind, WorkspaceState}, + }, +}; + +use crate::{ + Answer, Call, Reply, WireObserved, WireSetting, WireSnapshot, encode_call, encode_reply, +}; + +/// A named encoding. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Sample { + pub name: &'static str, + pub bytes: Vec, +} + +mod bars; +mod charts; +mod files; + +fn local(name: &str) -> LocalId { + LocalId::parse(name).expect("a sample local name is valid") +} + +fn context() -> ContributionContext { + ContributionContext { + kind: Kind::Widget, + zone: Zone::Right, + } +} + +/// A snapshot that carries every observation and every setting kind. +fn full_snapshot() -> WireSnapshot { + WireSnapshot { + observations: vec![ + ( + String::from("clock.now"), + Ok(WireObserved::Clock(ClockNow { + unix_minutes: 29_000_000, + utc_offset_seconds: 3600, + })), + ), + ( + String::from("workspace.kind"), + Ok(WireObserved::Workspace(WorkspaceState { + kind: WorkspaceKind::Remote, + connected: true, + })), + ), + ( + String::from("monitor.usage"), + Ok(WireObserved::Usage(Usage { + cpu_percent: 12.5, + memory_percent: 40.0, + })), + ), + ( + String::from("transfers.counts"), + Ok(WireObserved::Counts(Counts { + active: 2, + failed: 1, + })), + ), + ( + String::from("notifications.own"), + Ok(WireObserved::Notices(OwnNotices(vec![OwnNotice { + tone: NoticeTone::Caution, + text: String::from("Done"), + }]))), + ), + (String::from("later.cap"), Err(HostError::Unavailable)), + ], + settings: vec![ + (String::from("on"), WireSetting::Bool(true)), + ( + String::from("mode"), + WireSetting::Choice(String::from("fast")), + ), + ( + String::from("sets"), + WireSetting::Choices(vec![String::from("a"), String::from("b")]), + ), + (String::from("count"), WireSetting::Number(-7)), + ( + String::from("city"), + WireSetting::Text(String::from("Oslo")), + ), + ], + storage: vec![(String::from("start"), String::from("1700000000"))], + context: context(), + outcomes: vec![ + EffectOutcome { + key: Some(String::from("open")), + result: Ok(()), + }, + EffectOutcome { + key: None, + result: Err(HostError::RateLimited { + retry_after_ms: 250, + }), + }, + EffectOutcome { + key: Some(String::from("post")), + result: Err(HostError::NotGranted), + }, + EffectOutcome { + key: None, + result: Err(HostError::Denied), + }, + ], + } +} + +fn empty_snapshot() -> WireSnapshot { + WireSnapshot { + observations: Vec::new(), + settings: Vec::new(), + storage: Vec::new(), + context: ContributionContext { + kind: Kind::Panel, + zone: Zone::Left, + }, + outcomes: Vec::new(), + } +} + +fn node(parent: Option, key: Option<&str>, kind: NodeKind, tone: Tone) -> Node { + Node { + parent, + key: key.map(String::from), + kind, + tone, + } +} + +/// A view with every node kind, tone and refresh. +fn full_view(refresh: Refresh) -> View { + View { + nodes: vec![ + node(None, None, NodeKind::Column, Tone::Default), + node(Some(0), None, NodeKind::Row, Tone::Muted), + node(Some(1), None, NodeKind::Icon(Glyph::Puzzle), Tone::Success), + node( + Some(1), + None, + NodeKind::Text(String::from("12:30")), + Tone::Caution, + ), + node(Some(1), None, NodeKind::Meter(40), Tone::Default), + node( + Some(1), + None, + NodeKind::Badge(String::from("3")), + Tone::Default, + ), + node( + Some(1), + Some("go"), + NodeKind::Button(String::from("Go")), + Tone::Default, + ), + node(Some(1), Some("flag"), NodeKind::Toggle(true), Tone::Default), + node(Some(0), None, NodeKind::List, Tone::Default), + node(Some(8), None, NodeKind::Divider, Tone::Default), + node(Some(8), None, NodeKind::Spacer, Tone::Default), + ], + refresh: Some(refresh), + } +} + +fn full_effects() -> Effects { + let request = |key: Option<&str>, effect| EffectRequest { + key: key.map(String::from), + effect, + }; + Effects(vec![ + request( + Some("note"), + Effect::NotificationsPost(Post { + tone: NoticeTone::Info, + text: String::from("Hello"), + notice_key: Some(String::from("hello")), + }), + ), + request( + None, + Effect::PanelsSetOpen(SetOpen { + local: local("panel"), + open: true, + }), + ), + request( + None, + Effect::PanelsSetBadge(SetBadge { + local: local("panel"), + badge: Some(3), + }), + ), + request( + Some("save"), + Effect::StorageSet(Set { + key: String::from("start"), + value: String::from("1700000000"), + }), + ), + request( + None, + Effect::StorageRemove(Remove { + key: String::from("start"), + }), + ), + ]) +} + +/// The glyphs and the components of a view. +fn component_samples() -> [Sample; 2] { + [ + sample( + "reply-view-glyphs", + encode_reply(&reply(Answer::View(glyphs_view()), &[])), + ), + sample( + "reply-view-components", + encode_reply(&reply(Answer::View(components_view()), &[])), + ), + ] +} + +/// A view with one icon node for every glyph, so a reordered glyph changes a sample. +fn glyphs_view() -> View { + let mut nodes = vec![node(None, None, NodeKind::Row, Tone::Default)]; + nodes.extend( + Glyph::ALL + .into_iter() + .map(|glyph| node(Some(0), None, NodeKind::Icon(glyph), Tone::Default)), + ); + View { + nodes, + refresh: None, + } +} + +/// A section of one item with an icon button, a caption and a field. +fn components_view() -> View { + View { + nodes: vec![ + node( + None, + Some("servers"), + NodeKind::Section { + title: String::from("Servers"), + count: Some(String::from("2")), + folded: false, + }, + Tone::Default, + ), + node( + Some(0), + Some("open"), + NodeKind::Item { + title: String::from("Build"), + detail: Some(String::from("on \u{b7} node 1")), + icon: Some(Glyph::Server), + selected: true, + }, + Tone::Success, + ), + node( + Some(1), + Some("stop"), + NodeKind::IconButton { + icon: Glyph::Stop, + label: String::from("Stop"), + }, + Tone::Default, + ), + node( + Some(0), + None, + NodeKind::Caption(String::from("one is off")), + Tone::Muted, + ), + node( + Some(0), + None, + NodeKind::Field { + label: String::from("CPU"), + value: String::from("38%"), + }, + Tone::Caution, + ), + ], + refresh: None, + } +} + +/// A view with one icon node for every icon, so a reordered icon changes a sample. +fn icons_view() -> View { + let mut nodes = vec![node(None, None, NodeKind::Row, Tone::Default)]; + nodes.extend( + PluginIcon::ALL + .into_iter() + .map(|icon| node(Some(0), None, NodeKind::Icon(icon.into()), Tone::Default)), + ); + View { + nodes, + refresh: None, + } +} + +/// A snapshot with the workspace observation of every kind, under one key each. +fn workspace_kinds_snapshot() -> WireSnapshot { + let observations = WorkspaceKind::ALL + .into_iter() + .enumerate() + .map(|(index, kind)| { + ( + alloc::format!("workspace.kind.{index}"), + Ok(WireObserved::Workspace(WorkspaceState { + kind, + connected: index % 2 == 0, + })), + ) + }) + .collect(); + WireSnapshot { + observations, + ..empty_snapshot() + } +} + +/// The names of the samples that pin one context each, in the order of [`Zone::ALL`]. +const ZONE_SAMPLES: [&str; Zone::ALL.len()] = [ + "call-render-zone-left", + "call-render-zone-center", + "call-render-zone-right", +]; + +fn reply(answer: Answer, log: &[&str]) -> Reply { + Reply { + answer, + log: log.iter().map(|line| String::from(*line)).collect(), + } +} + +fn sample(name: &'static str, bytes: Result, crate::AbiError>) -> Sample { + Sample { + name, + bytes: bytes.expect("a canonical sample fits the ABI bounds"), + } +} + +/// The reply samples and the workspace-kinds render sample, in golden-file order. +fn reply_samples() -> Vec { + vec![ + sample( + "reply-view-next-second", + encode_reply(&reply( + Answer::View(full_view(Refresh::NextSecond)), + &["rendered"], + )), + ), + sample( + "reply-view-next-minute", + encode_reply(&reply(Answer::View(full_view(Refresh::NextMinute)), &[])), + ), + sample( + "reply-view-after-ms", + encode_reply(&reply(Answer::View(full_view(Refresh::AfterMs(500))), &[])), + ), + sample( + "reply-effects", + encode_reply(&reply(Answer::Effects(full_effects()), &["a", "b"])), + ), + sample( + "reply-effects-none", + encode_reply(&reply(Answer::Effects(Effects::default()), &[])), + ), + sample( + "reply-view-icons", + encode_reply(&reply(Answer::View(icons_view()), &[])), + ), + sample( + "call-render-workspace-kinds", + encode_call(&Call::Render { + local: local("bar"), + snapshot: workspace_kinds_snapshot(), + }), + ), + ] +} + +/// Every canonical sample, in a fixed order. New samples are appended. +pub fn samples() -> Vec { + let events = [ + ( + "call-event-activated", + Event::Activated { + key: String::from("go"), + }, + ), + ( + "call-event-toggled", + Event::Toggled { + key: String::from("flag"), + on: true, + }, + ), + ( + "call-event-visibility", + Event::VisibilityChanged { visible: false }, + ), + ("call-event-settings", Event::SettingsChanged), + ]; + let mut samples = vec![ + sample( + "call-render-empty", + encode_call(&Call::Render { + local: local("bar"), + snapshot: empty_snapshot(), + }), + ), + sample( + "call-render-full", + encode_call(&Call::Render { + local: local("bar"), + snapshot: full_snapshot(), + }), + ), + ]; + samples.extend(events.into_iter().map(|(name, event)| { + sample( + name, + encode_call(&Call::Event { + local: local("panel"), + event, + snapshot: full_snapshot(), + }), + ) + })); + samples.extend(reply_samples()); + samples.extend(ZONE_SAMPLES.into_iter().zip(Zone::ALL).map(|(name, zone)| { + sample( + name, + encode_call(&Call::Render { + local: local("bar"), + snapshot: WireSnapshot { + context: ContributionContext { + kind: Kind::Widget, + zone, + }, + ..empty_snapshot() + }, + }), + ) + })); + samples.extend(session_samples()); + samples +} + +/// The samples of the requests, timers, terminals, code blocks and toolbars, appended after +/// the first set so its bytes stay as they were. +fn session_samples() -> Vec { + let events = [ + ( + "call-event-response", + Event::Response { + key: String::from("servers"), + status: 200, + body: String::from("{\"servers\":{}}"), + }, + ), + ( + "call-event-timer", + Event::Timer { + key: String::from("poll"), + }, + ), + ( + "call-event-terminal-line", + Event::TerminalLine { + terminal: String::from("srv"), + line: String::from("say hi"), + }, + ), + ( + "call-event-terminal-closed", + Event::TerminalClosed { + terminal: String::from("srv"), + }, + ), + ]; + let mut samples: Vec = events + .into_iter() + .map(|(name, event)| { + sample( + name, + encode_call(&Call::Event { + local: local("panel"), + event, + snapshot: empty_snapshot(), + }), + ) + }) + .collect(); + samples.push(sample( + "reply-effects-sessions", + encode_reply(&reply(Answer::Effects(session_effects()), &[])), + )); + samples.push(sample( + "reply-view-code", + encode_reply(&reply( + Answer::View(View { + nodes: vec![node( + None, + None, + NodeKind::Code(String::from("a\n b")), + Tone::Muted, + )], + refresh: None, + }), + &[], + )), + )); + samples.extend(component_samples()); + samples.extend(files::samples()); + samples.push(sample( + "call-render-toolbar", + encode_call(&Call::Render { + local: local("bar"), + snapshot: WireSnapshot { + context: ContributionContext { + kind: Kind::Toolbar, + zone: Zone::Center, + }, + ..empty_snapshot() + }, + }), + )); + samples.extend(bars::samples()); + samples.extend(charts::samples()); + samples +} + +/// A request of every method, a timer and both terminal effects. +fn session_effects() -> Effects { + let mut effects: Vec = Method::ALL + .into_iter() + .enumerate() + .map(|(index, method)| EffectRequest { + key: Some(alloc::format!("r{index}")), + effect: Effect::NetRequest(Request { + method, + path: String::from("/api/items"), + body: (method == Method::Post).then(|| String::from("{\"action\":\"start\"}")), + bytes: false, + }), + }) + .collect(); + effects.push(EffectRequest { + key: Some(String::from("poll")), + effect: Effect::TimersSet(SetTimer { after_ms: 2000 }), + }); + effects.push(EffectRequest { + key: None, + effect: Effect::TerminalsOpen(Open { + local: local("panel"), + terminal: String::from("srv"), + title: String::from("Build"), + files: false, + }), + }); + effects.push(EffectRequest { + key: None, + effect: Effect::TerminalsWrite(Write { + terminal: String::from("srv"), + text: String::from("\u{1b}[32mready\u{1b}[0m\n"), + }), + }); + Effects(effects) +} diff --git a/crates/plugin-abi/src/samples/bars.rs b/crates/plugin-abi/src/samples/bars.rs new file mode 100644 index 00000000..63a87ad8 --- /dev/null +++ b/crates/plugin-abi/src/samples/bars.rs @@ -0,0 +1,62 @@ +//! Samples of the bar of a plugin's terminal: the call that draws it, an event of it, and the +//! effects that show, move and take it away. + +use alloc::{string::String, vec, vec::Vec}; + +use nocterm_plugin_api::{ + ContributionContext, Edge, Effect, EffectRequest, Effects, Event, Kind, Zone, + capability::terminals::Bar, +}; + +use super::{Sample, empty_snapshot, local, reply, sample}; +use crate::{Answer, Call, WireSnapshot, encode_call, encode_reply}; + +fn bar_snapshot() -> WireSnapshot { + WireSnapshot { + context: ContributionContext { + kind: Kind::Toolbar, + zone: Zone::Center, + }, + ..empty_snapshot() + } +} + +pub(super) fn samples() -> Vec { + let effects = Edge::ALL + .into_iter() + .map(Some) + .chain([None]) + .map(|edge| EffectRequest { + key: None, + effect: Effect::TerminalsBar(Bar { + terminal: String::from("srv"), + edge, + }), + }) + .collect(); + vec![ + sample( + "call-render-terminal-bar", + encode_call(&Call::RenderTerminalBar { + local: local("panel"), + terminal: String::from("srv"), + snapshot: bar_snapshot(), + }), + ), + sample( + "call-event-terminal-bar", + encode_call(&Call::TerminalBarEvent { + local: local("panel"), + terminal: String::from("srv"), + event: Event::Activated { + key: String::from("restart"), + }, + snapshot: bar_snapshot(), + }), + ), + sample( + "reply-effects-terminal-bar", + encode_reply(&reply(Answer::Effects(Effects(effects)), &[])), + ), + ] +} diff --git a/crates/plugin-abi/src/samples/charts.rs b/crates/plugin-abi/src/samples/charts.rs new file mode 100644 index 00000000..c7e8c0fb --- /dev/null +++ b/crates/plugin-abi/src/samples/charts.rs @@ -0,0 +1,42 @@ +//! A sample of a view with charts: one series against a top value and a span, and two series +//! scaled to their largest value. + +use alloc::{vec, vec::Vec}; + +use nocterm_plugin_api::{NodeKind, Tone, View}; + +use super::{Sample, node, reply, sample}; +use crate::{Answer, encode_reply}; + +pub(super) fn samples() -> Vec { + let view = View { + nodes: vec![ + node(None, None, NodeKind::Column, Tone::Default), + node( + Some(0), + None, + NodeKind::Chart { + series: vec![vec![12, 30, 36]], + max: Some(100), + span: Some(60), + }, + Tone::Default, + ), + node( + Some(0), + None, + NodeKind::Chart { + series: vec![vec![4, 9, 7], vec![2, 3, 5]], + max: None, + span: None, + }, + Tone::Caution, + ), + ], + refresh: None, + }; + vec![sample( + "reply-view-charts", + encode_reply(&reply(Answer::View(view), &[])), + )] +} diff --git a/crates/plugin-abi/src/samples/files.rs b/crates/plugin-abi/src/samples/files.rs new file mode 100644 index 00000000..0a5fdddf --- /dev/null +++ b/crates/plugin-abi/src/samples/files.rs @@ -0,0 +1,126 @@ +//! Samples of the files of a plugin's terminal: a request of every kind, the answers to them, +//! and a tab and a request with bytes. + +use alloc::{string::String, vec, vec::Vec}; + +use nocterm_plugin_api::{ + Effect, EffectRequest, Effects, Event, + capability::{ + files::{FileAnswer, FileEntry, FileFailure, FileOp, FileReply}, + net::{Method, Request}, + terminals::Open, + }, +}; + +use super::{Sample, empty_snapshot, local, reply, sample}; +use crate::{Answer, Call, encode_call, encode_reply}; + +pub(super) fn samples() -> Vec { + let mut samples = requests(); + samples.push(sample( + "reply-effects-files", + encode_reply(&reply(Answer::Effects(answers()), &[])), + )); + samples +} + +/// One `file-request` event of every kind. +fn requests() -> Vec { + const NAMES: [&str; 7] = [ + "call-event-file-list", + "call-event-file-stat", + "call-event-file-read", + "call-event-file-write", + "call-event-file-create-dir", + "call-event-file-remove", + "call-event-file-rename", + ]; + let path = || String::from("/plugins/a.yml"); + let ops = [ + FileOp::List { path: path() }, + FileOp::Stat { path: path() }, + FileOp::Read { path: path() }, + FileOp::Write { + path: path(), + data: String::from("TWFu"), + create: true, + }, + FileOp::CreateDir { path: path() }, + FileOp::Remove { + path: path(), + directory: false, + }, + FileOp::Rename { + from: path(), + to: String::from("/b.yml"), + }, + ]; + ops.into_iter() + .zip(NAMES) + .enumerate() + .map(|(index, (op, name))| { + let event = Event::FileRequest { + terminal: String::from("srv"), + request: u32::try_from(index).unwrap_or(0), + op, + }; + sample( + name, + encode_call(&Call::Event { + local: local("panel"), + event, + snapshot: empty_snapshot(), + }), + ) + }) + .collect() +} + +/// An answer of every kind, a request with bytes and a tab that serves files. +fn answers() -> Effects { + let entry = FileEntry { + name: String::from("a.yml"), + directory: false, + size: Some(12), + modified: Some(1_700_000_000), + }; + let answers = [ + FileAnswer::Listed(vec![entry.clone()]), + FileAnswer::Found(Some(entry)), + FileAnswer::Content(String::from("TWFu")), + FileAnswer::Done, + FileAnswer::Failed(FileFailure::NotFound), + FileAnswer::Failed(FileFailure::Other(String::from("busy"))), + ]; + let mut effects: Vec = answers + .into_iter() + .enumerate() + .map(|(index, answer)| EffectRequest { + key: None, + effect: Effect::TerminalsFiles(FileReply { + terminal: String::from("srv"), + request: u32::try_from(index).unwrap_or(0), + answer, + }), + }) + .collect(); + effects.push(EffectRequest { + key: Some(String::from("read")), + effect: Effect::NetRequest(Request { + method: Method::Post, + path: String::from("/write"), + body: Some(String::from("TWFu")), + bytes: true, + }), + }); + effects.push(EffectRequest { + key: None, + effect: Effect::TerminalsOpen(Open { + local: local("panel"), + terminal: String::from("srv"), + title: String::from("Build"), + files: true, + }), + }); + Effects(effects) +} diff --git a/crates/plugin-abi/src/tests.rs b/crates/plugin-abi/src/tests.rs new file mode 100644 index 00000000..4fb3ab68 --- /dev/null +++ b/crates/plugin-abi/src/tests.rs @@ -0,0 +1,332 @@ +//! Behavior tests of the ABI types and codec. + +use alloc::{collections::BTreeMap, string::String, vec, vec::Vec}; + +use nocterm_plugin_api::{ + ContributionContext, EffectOutcome, Event, HostError, Kind, LocalId, NodeKind, Observed, + PluginIcon, Snapshot, Zone, + capability::{ + clock::ClockNow, + monitor::Usage, + notifications::{NoticeTone, OwnNotice, OwnNotices}, + transfers::Counts, + workspace::{WorkspaceKind, WorkspaceState}, + }, +}; +use nocterm_plugin_model::{SettingValue, SettingValues}; + +use crate::{ + ABI_VERSION, AbiError, Answer, Call, MAX_INPUT_BYTES, MAX_REPLY_BYTES, Reply, WireObserved, + WireSnapshot, decode_call, decode_reply, encode_call, encode_reply, pack, samples, unpack, +}; + +fn snapshot() -> Snapshot { + let mut settings = SettingValues::default(); + settings.insert("on", SettingValue::Bool(true)); + settings.insert("mode", SettingValue::Choice("fast".into())); + settings.insert("sets", SettingValue::Choices(vec!["a".into(), "b".into()])); + settings.insert("count", SettingValue::Number(-3)); + settings.insert("city", SettingValue::Text("Oslo".into())); + let mut observations = BTreeMap::new(); + observations.insert( + "clock.now".into(), + Ok(Observed::Clock(ClockNow { + unix_minutes: 5, + utc_offset_seconds: -3600, + })), + ); + observations.insert( + "workspace.kind".into(), + Ok(Observed::Workspace(WorkspaceState { + kind: WorkspaceKind::Container, + connected: false, + })), + ); + observations.insert( + "monitor.usage".into(), + Ok(Observed::Usage(Usage { + cpu_percent: 1.5, + memory_percent: 99.0, + })), + ); + observations.insert( + "transfers.counts".into(), + Ok(Observed::Counts(Counts { + active: 1, + failed: 0, + })), + ); + observations.insert( + "notifications.own".into(), + Ok(Observed::Notices(OwnNotices(vec![OwnNotice { + tone: NoticeTone::Info, + text: "x".into(), + }]))), + ); + observations.insert("future.cap".into(), Err(HostError::NotGranted)); + Snapshot { + observations, + settings, + storage: BTreeMap::from([("k".into(), "v".into())]), + context: ContributionContext { + kind: Kind::Panel, + zone: Zone::Center, + }, + outcomes: vec![EffectOutcome { + key: Some("a".into()), + result: Err(HostError::Denied), + }], + terminal: None, + } +} + +#[test] +fn a_snapshot_survives_the_wire_with_every_observation_and_setting_kind() { + let snapshot = snapshot(); + let wire = WireSnapshot::from(&snapshot); + assert_eq!(wire.observations.len(), 6); + assert_eq!(wire.settings.len(), 5); + let bytes = encode_call(&Call::Render { + local: LocalId::parse("bar").unwrap(), + snapshot: wire, + }) + .unwrap(); + let Call::Render { snapshot: back, .. } = decode_call(&bytes).unwrap() else { + panic!("a render call stays a render call"); + }; + assert_eq!(Snapshot::from(back), snapshot); +} + +#[test] +fn every_sample_decodes_and_encodes_to_the_same_bytes() { + let all = samples(); + assert!(all.len() >= 10); + for sample in &all { + let again = match decode_call(&sample.bytes) { + Ok(call) => encode_call(&call), + Err(_) => encode_reply(&decode_reply(&sample.bytes).expect(sample.name)), + }; + assert_eq!(again.as_ref(), Ok(&sample.bytes), "{}", sample.name); + } +} + +#[test] +fn sample_names_are_unique_and_the_bytes_are_stable_between_runs() { + let first = samples(); + let second = samples(); + assert_eq!(first, second); + let mut names: Vec<_> = first.iter().map(|s| s.name).collect(); + names.sort_unstable(); + names.dedup(); + assert_eq!(names.len(), first.len()); +} + +#[test] +fn the_samples_pin_every_icon_workspace_kind_and_zone() { + let all = samples(); + assert_eq!( + all.len(), + 13 + Zone::ALL.len() + 21, + "append a sample, never drop one" + ); + let bytes = |name: &str| { + &all.iter() + .find(|sample| sample.name == name) + .expect(name) + .bytes + }; + + let Answer::View(view) = decode_reply(bytes("reply-view-icons")).unwrap().answer else { + panic!("the icon sample is a view"); + }; + let icons: Vec<_> = view + .nodes + .iter() + .filter_map(|node| match node.kind { + NodeKind::Icon(icon) => Some(icon), + _ => None, + }) + .collect(); + assert_eq!(icons, PluginIcon::ALL.map(nocterm_plugin_api::Glyph::from)); + + let Call::Render { snapshot, .. } = decode_call(bytes("call-render-workspace-kinds")).unwrap() + else { + panic!("the workspace sample is a render call"); + }; + let kinds: Vec<_> = snapshot + .observations + .into_iter() + .filter_map(|(_, observed)| match observed { + Ok(WireObserved::Workspace(state)) => Some(state.kind), + _ => None, + }) + .collect(); + assert_eq!(kinds, WorkspaceKind::ALL); + + let zones: Vec<_> = all + .iter() + .filter(|sample| sample.name.starts_with("call-render-zone-")) + .map(|sample| { + decode_call(&sample.bytes) + .unwrap() + .into_parts() + .2 + .context + .zone + }) + .collect(); + assert_eq!(zones, Zone::ALL); +} + +#[test] +fn the_largest_snapshot_the_host_can_build_fits_the_input_bound_with_room_to_spare() { + use nocterm_plugin_api::capability::storage::{MAX_KEY, MAX_TOTAL, MAX_VALUE}; + let entries = MAX_TOTAL / MAX_VALUE; + let wire = WireSnapshot { + observations: vec![( + "notifications.own".into(), + Ok(WireObserved::Notices(OwnNotices( + (0..3) + .map(|_| OwnNotice { + tone: NoticeTone::Caution, + text: "\u{1f600}".repeat(200), + }) + .collect(), + ))), + )], + // The host caps the setting values of a plugin at 4 KiB. + settings: vec![( + "city".into(), + crate::WireSetting::Text("x".repeat(4 * 1024)), + )], + storage: (0..entries) + .map(|index| (alloc::format!("{index:0>MAX_KEY$}"), "v".repeat(MAX_VALUE))) + .collect(), + context: ContributionContext { + kind: Kind::Panel, + zone: Zone::Left, + }, + outcomes: (0..32) + .map(|_| EffectOutcome { + key: Some("k".repeat(32)), + result: Err(HostError::RateLimited { + retry_after_ms: u32::MAX, + }), + }) + .collect(), + }; + let bytes = encode_call(&Call::Render { + local: LocalId::parse("bar").unwrap(), + snapshot: wire, + }) + .expect("the worst case fits"); + assert!(bytes.len() * 2 < MAX_INPUT_BYTES, "{} bytes", bytes.len()); +} + +#[test] +fn a_reply_carries_its_answer_and_log() { + let reply = Reply { + answer: Answer::Effects(Default::default()), + log: vec![String::from("hello")], + }; + let bytes = encode_reply(&reply).unwrap(); + assert_eq!(decode_reply(&bytes), Ok(reply)); +} + +#[test] +fn bytes_that_are_not_a_value_or_have_a_tail_are_refused() { + assert!(matches!(decode_reply(&[]), Err(AbiError::Decode(_)))); + assert!(matches!( + decode_call(&[0xff, 0xff, 0xff]), + Err(AbiError::Decode(_)) + )); + let reply = Reply { + answer: Answer::Effects(Default::default()), + log: Vec::new(), + }; + let mut bytes = encode_reply(&reply).unwrap(); + bytes.push(0); + assert_eq!(decode_reply(&bytes), Err(AbiError::Trailing)); +} + +#[test] +fn the_bounds_apply_before_decoding_and_after_encoding() { + let big = vec![0u8; MAX_INPUT_BYTES + 1]; + assert_eq!( + decode_call(&big), + Err(AbiError::TooLarge { + max: MAX_INPUT_BYTES + }) + ); + let big = vec![0u8; MAX_REPLY_BYTES + 1]; + assert_eq!( + decode_reply(&big), + Err(AbiError::TooLarge { + max: MAX_REPLY_BYTES + }) + ); + let call = Call::Render { + local: LocalId::parse("bar").unwrap(), + snapshot: WireSnapshot { + storage: vec![("k".into(), "v".repeat(MAX_INPUT_BYTES))], + ..WireSnapshot::from(&Snapshot::new(ContributionContext { + kind: Kind::Widget, + zone: Zone::Left, + })) + }, + }; + assert_eq!( + encode_call(&call), + Err(AbiError::TooLarge { + max: MAX_INPUT_BYTES + }) + ); + let reply = Reply { + answer: Answer::Effects(Default::default()), + log: vec!["x".repeat(MAX_REPLY_BYTES)], + }; + assert_eq!( + encode_reply(&reply), + Err(AbiError::TooLarge { + max: MAX_REPLY_BYTES + }) + ); +} + +#[test] +fn a_decode_error_keeps_the_codec_cause() { + use core::error::Error as _; + let error = decode_reply(&[]).unwrap_err(); + assert!(error.source().is_some()); +} + +#[test] +fn a_pointer_and_a_length_pack_into_one_value_and_back() { + for (ptr, len) in [(0, 0), (1, 2), (u32::MAX, 0), (0, u32::MAX), (65536, 4096)] { + assert_eq!(unpack(pack(ptr, len)), (ptr, len)); + } + assert_eq!(ABI_VERSION, 1); +} + +#[test] +fn a_bar_call_names_its_terminal_and_any_other_call_none() { + let local = LocalId::parse("panel").unwrap(); + let mut snapshot = Snapshot::new(ContributionContext { + kind: Kind::Toolbar, + zone: Zone::Center, + }); + let plain = Call::render(local.clone(), &snapshot); + assert!(matches!(plain, Call::Render { .. })); + assert_eq!(plain.into_parts().2.terminal, None); + snapshot.terminal = Some("srv".into()); + let drawn = + decode_call(&encode_call(&Call::render(local.clone(), &snapshot)).unwrap()).unwrap(); + assert!(matches!(drawn, Call::RenderTerminalBar { .. })); + let (_, event, back) = drawn.into_parts(); + assert_eq!((event, back.terminal.as_deref()), (None, Some("srv"))); + let used = Event::Activated { + key: "restart".into(), + }; + let (_, event, back) = Call::event(local, used.clone(), &snapshot).into_parts(); + assert_eq!((event, back), (Some(used), snapshot)); +} diff --git a/crates/plugin-abi/src/wire.rs b/crates/plugin-abi/src/wire.rs new file mode 100644 index 00000000..e01746e4 --- /dev/null +++ b/crates/plugin-abi/src/wire.rs @@ -0,0 +1,258 @@ +//! What crosses the boundary. Postcard writes struct fields in order and enum variants by +//! index, so every type here is append-only: new variants go last, fields are never reordered +//! or removed, and nothing uses `skip_serializing_if`, `untagged` or `flatten`. The golden file +//! checked by xtask enforces it. + +use alloc::{collections::BTreeMap, string::String, vec::Vec}; + +use nocterm_plugin_api::{ + ContributionContext, EffectOutcome, Effects, Event, HostError, LocalId, Observed, Snapshot, + View, + capability::{ + clock::ClockNow, monitor::Usage, notifications::OwnNotices, transfers::Counts, + workspace::WorkspaceState, + }, +}; +use nocterm_plugin_model::{SettingValue, SettingValues}; +use serde::{Deserialize, Serialize}; + +/// What the host asks a guest to do. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum Call { + /// Draw a contribution. + Render { + local: LocalId, + snapshot: WireSnapshot, + }, + /// Answer an event of a contribution. + Event { + local: LocalId, + event: Event, + snapshot: WireSnapshot, + }, + /// Draw the bar of the plugin's terminal `terminal` (`terminals.bar`), by the contribution + /// the terminal reports to. + RenderTerminalBar { + local: LocalId, + terminal: String, + snapshot: WireSnapshot, + }, + /// Answer an event of the bar of the plugin's terminal `terminal`. + TerminalBarEvent { + local: LocalId, + terminal: String, + event: Event, + snapshot: WireSnapshot, + }, +} + +impl Call { + /// The call that asks `local` for its view: the bar of a terminal when the snapshot names + /// one. + pub fn render(local: LocalId, snapshot: &Snapshot) -> Self { + let wire = WireSnapshot::from(snapshot); + match &snapshot.terminal { + Some(terminal) => Self::RenderTerminalBar { + local, + terminal: terminal.clone(), + snapshot: wire, + }, + None => Self::Render { + local, + snapshot: wire, + }, + } + } + + /// The call that reports `event` to `local`: from the bar of a terminal when the snapshot + /// names one. + pub fn event(local: LocalId, event: Event, snapshot: &Snapshot) -> Self { + let wire = WireSnapshot::from(snapshot); + match &snapshot.terminal { + Some(terminal) => Self::TerminalBarEvent { + local, + terminal: terminal.clone(), + event, + snapshot: wire, + }, + None => Self::Event { + local, + event, + snapshot: wire, + }, + } + } + + /// The contribution, the event (none for a view) and the snapshot as the guest reads them, + /// with the terminal of a bar's call. + pub fn into_parts(self) -> (LocalId, Option, Snapshot) { + let (local, event, snapshot, terminal) = match self { + Self::Render { local, snapshot } => (local, None, snapshot, None), + Self::Event { + local, + event, + snapshot, + } => (local, Some(event), snapshot, None), + Self::RenderTerminalBar { + local, + terminal, + snapshot, + } => (local, None, snapshot, Some(terminal)), + Self::TerminalBarEvent { + local, + terminal, + event, + snapshot, + } => (local, Some(event), snapshot, Some(terminal)), + }; + let mut snapshot = Snapshot::from(snapshot); + snapshot.terminal = terminal; + (local, event, snapshot) + } +} + +/// What the guest answers. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum Answer { + /// The answer to [`Call::Render`]. + View(View), + /// The answer to [`Call::Event`]. + Effects(Effects), +} + +/// A guest's reply to one call. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct Reply { + pub answer: Answer, + /// Log lines, at most [`MAX_LOG_LINES`](crate::MAX_LOG_LINES): the host refuses a reply with + /// more, and sanitizes the lines it keeps. + pub log: Vec, +} + +/// A [`Snapshot`] as ordered lists, so the bytes do not depend on a map implementation. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub struct WireSnapshot { + pub observations: Vec<(String, Result)>, + pub settings: Vec<(String, WireSetting)>, + pub storage: Vec<(String, String)>, + pub context: ContributionContext, + pub outcomes: Vec, +} + +/// One-to-one with [`Observed`]. A new observation is appended. +#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] +pub enum WireObserved { + Clock(ClockNow), + Workspace(WorkspaceState), + Usage(Usage), + Counts(Counts), + Notices(OwnNotices), +} + +/// One-to-one with [`SettingValue`]. A new kind is appended. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum WireSetting { + Bool(bool), + Choice(String), + Choices(Vec), + Number(i32), + Text(String), +} + +impl From<&Observed> for WireObserved { + fn from(observed: &Observed) -> Self { + match observed { + Observed::Clock(data) => Self::Clock(*data), + Observed::Workspace(data) => Self::Workspace(*data), + Observed::Usage(data) => Self::Usage(*data), + Observed::Counts(data) => Self::Counts(*data), + Observed::Notices(data) => Self::Notices(data.clone()), + } + } +} + +impl From for Observed { + fn from(wire: WireObserved) -> Self { + match wire { + WireObserved::Clock(data) => Self::Clock(data), + WireObserved::Workspace(data) => Self::Workspace(data), + WireObserved::Usage(data) => Self::Usage(data), + WireObserved::Counts(data) => Self::Counts(data), + WireObserved::Notices(data) => Self::Notices(data), + } + } +} + +impl From<&SettingValue> for WireSetting { + fn from(value: &SettingValue) -> Self { + match value { + SettingValue::Bool(on) => Self::Bool(*on), + SettingValue::Choice(choice) => Self::Choice(choice.clone()), + SettingValue::Choices(choices) => Self::Choices(choices.clone()), + SettingValue::Number(number) => Self::Number(*number), + SettingValue::Text(text) => Self::Text(text.clone()), + } + } +} + +impl From for SettingValue { + fn from(wire: WireSetting) -> Self { + match wire { + WireSetting::Bool(on) => Self::Bool(on), + WireSetting::Choice(choice) => Self::Choice(choice), + WireSetting::Choices(choices) => Self::Choices(choices), + WireSetting::Number(number) => Self::Number(number), + WireSetting::Text(text) => Self::Text(text), + } + } +} + +impl From<&Snapshot> for WireSnapshot { + fn from(snapshot: &Snapshot) -> Self { + Self { + observations: snapshot + .observations + .iter() + .map(|(id, observed)| { + ( + id.clone(), + observed.as_ref().map(WireObserved::from).map_err(|e| *e), + ) + }) + .collect(), + settings: snapshot + .settings + .iter() + .map(|(key, value)| (String::from(key), WireSetting::from(value))) + .collect(), + storage: snapshot + .storage + .iter() + .map(|(key, value)| (key.clone(), value.clone())) + .collect(), + context: snapshot.context, + outcomes: snapshot.outcomes.clone(), + } + } +} + +impl From for Snapshot { + fn from(wire: WireSnapshot) -> Self { + let mut settings = SettingValues::default(); + for (key, value) in wire.settings { + settings.insert(key, SettingValue::from(value)); + } + Self { + observations: wire + .observations + .into_iter() + .map(|(id, observed)| (id, observed.map(Observed::from))) + .collect::>(), + settings, + storage: wire.storage.into_iter().collect(), + context: wire.context, + outcomes: wire.outcomes, + terminal: None, + } + } +} diff --git a/crates/plugin-api/Cargo.toml b/crates/plugin-api/Cargo.toml new file mode 100644 index 00000000..2c2113e4 --- /dev/null +++ b/crates/plugin-api/Cargo.toml @@ -0,0 +1,25 @@ +[package] +name = "nocterm-plugin-api" +description = "The user plugin contract: capability table, snapshot, view and effect data, manifests, grants and the declarative template interpreter." +version = "0.0.0" +edition.workspace = true +license-file.workspace = true +publish = false + +[package.metadata.nocterm] +layer = "domain" + +# The crate is `no_std` with `alloc`, so these do not take the workspace entries (which keep +# the default `std` features); the isolation check lists the direct dependencies. +[dependencies] +nocterm-plugin-model.workspace = true +serde = { version = "1", default-features = false, features = ["alloc", "derive"] } +thiserror = { version = "2", default-features = false } + +[dev-dependencies] +proptest = "1" +serde_json.workspace = true +toml.workspace = true + +[lints] +workspace = true diff --git a/crates/plugin-api/api-surface.json b/crates/plugin-api/api-surface.json new file mode 100644 index 00000000..57e2c572 --- /dev/null +++ b/crates/plugin-api/api-surface.json @@ -0,0 +1,940 @@ +{ + "api_version": "0.1", + "capabilities": [ + { + "consent": "See the current time and your time zone.", + "deprecated": null, + "doc": "The wall-clock time to the minute and the UTC offset of the user.", + "excluded": "A timer finer than a minute.", + "family": "observe", + "fields": { + "unix_minutes": "number", + "utc_offset_seconds": "number" + }, + "gesture": false, + "id": "clock.now", + "live_cap": null, + "rate": null, + "session_derived": true, + "since": "0.1", + "target": null + }, + { + "consent": "See what kind of workspace a window shows and whether it is connected.", + "deprecated": null, + "doc": "Whether the window shows a local, remote, container or folder workspace, and whether it is connected.", + "excluded": "Title, host, user, path and tab count.", + "family": "observe", + "fields": { + "connected": "bool", + "kind": "text" + }, + "gesture": false, + "id": "workspace.kind", + "live_cap": null, + "rate": null, + "session_derived": true, + "since": "0.1", + "target": null + }, + { + "consent": "See how busy this computer is.", + "deprecated": null, + "doc": "Processor and memory use in percent, or unavailable when no built-in sampler runs.", + "excluded": "Processes and host names.", + "family": "observe", + "fields": { + "cpu_percent": "number", + "memory_percent": "number" + }, + "gesture": false, + "id": "monitor.usage", + "live_cap": null, + "rate": null, + "session_derived": true, + "since": "0.1", + "target": null + }, + { + "consent": "See how many file transfers are running or failed.", + "deprecated": null, + "doc": "How many transfers run and how many failed.", + "excluded": "File names, paths and rates.", + "family": "observe", + "fields": { + "active": "number", + "failed": "number" + }, + "gesture": false, + "id": "transfers.counts", + "live_cap": null, + "rate": null, + "session_derived": true, + "since": "0.1", + "target": null + }, + { + "consent": "See the notices this plugin posted.", + "deprecated": null, + "doc": "The notices this plugin posted that are still shown.", + "excluded": "Notices of the host and of other plugins.", + "family": "observe", + "fields": null, + "gesture": false, + "id": "notifications.own", + "live_cap": null, + "rate": null, + "session_derived": false, + "since": "0.1", + "target": null + }, + { + "consent": "Show notices in the status bar.", + "deprecated": null, + "doc": "Posts an informational or caution notice of up to 200 characters.", + "excluded": "Actions, links and the error level.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "notifications.post", + "live_cap": 3, + "rate": { + "per_ms": 10000, + "uses": 1 + }, + "session_derived": false, + "since": "0.1", + "target": "host-presentation" + }, + { + "consent": "Open or close its own panels when you click it.", + "deprecated": null, + "doc": "Opens or closes one of the plugin's own panels after a click.", + "excluded": "Other contributions and moving focus.", + "family": "effect", + "fields": null, + "gesture": true, + "id": "panels.set-open", + "live_cap": null, + "rate": null, + "session_derived": false, + "since": "0.1", + "target": "own-contribution" + }, + { + "consent": "Show a number on its own panel or widget.", + "deprecated": null, + "doc": "Sets or clears a number badge on one of the plugin's own contributions.", + "excluded": "Attention requests.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "panels.set-badge", + "live_cap": null, + "rate": null, + "session_derived": false, + "since": "0.1", + "target": "own-contribution" + }, + { + "consent": "Keep a small amount of its own data.", + "deprecated": null, + "doc": "Stores a value under a key in the plugin's own data, up to 64 KiB in total.", + "excluded": "Data of other plugins.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "storage.set", + "live_cap": null, + "rate": null, + "session_derived": false, + "since": "0.1", + "target": "own-data" + }, + { + "consent": "Remove its own stored data.", + "deprecated": null, + "doc": "Removes a key from the plugin's own data.", + "excluded": "Data of other plugins.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "storage.remove", + "live_cap": null, + "rate": null, + "session_derived": false, + "since": "0.1", + "target": "own-data" + }, + { + "consent": "Send requests to the web address you enter in its settings, with the key you give it.", + "deprecated": null, + "doc": "Sends a request to the web address the user entered in the setting the `[network]` table names, with the credential the user entered, and answers with a `response` event under the request's key.", + "excluded": "Other addresses, cookies, redirects and reading the key.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "net.request", + "live_cap": 4, + "rate": { + "per_ms": 1000, + "uses": 8 + }, + "session_derived": false, + "since": "0.1", + "target": "user-origin" + }, + { + "consent": "Wake itself up after a delay to refresh what it shows.", + "deprecated": null, + "doc": "Wakes the plugin with a `timer` event under the effect's key after one second to one hour; setting a key that waits replaces it.", + "excluded": "Delays shorter than a second.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "timers.set", + "live_cap": 8, + "rate": { + "per_ms": 1000, + "uses": 8 + }, + "session_derived": false, + "since": "0.1", + "target": "own-timer" + }, + { + "consent": "Open terminal tabs that show what it writes and pass on what you type.", + "deprecated": null, + "doc": "Opens a terminal tab, or shows it, after a click. Lines typed into it, by the user or an AI chat it is attached to, arrive as `terminal-line` events.", + "excluded": "Your other terminals and running anything on this computer.", + "family": "effect", + "fields": null, + "gesture": true, + "id": "terminals.open", + "live_cap": 4, + "rate": null, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" + }, + { + "consent": "Write text to the terminal tabs it opened.", + "deprecated": null, + "doc": "Writes text with colours to one of the plugin's open terminal tabs.", + "excluded": "Other terminals, and control sequences other than colours.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "terminals.write", + "live_cap": null, + "rate": { + "per_ms": 1000, + "uses": 50 + }, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" + }, + { + "consent": "Show the files of the servers it opens as terminal tabs in your file browser, and let AI chats you attach those tabs to read and change them.", + "deprecated": null, + "doc": "Serves the files of a terminal tab the plugin opened with `files`: the file browser shows them while the tab is active and an AI chat it is attached to reads and changes them with its file tools. The host asks with `file-request` events and the plugin answers with this effect under the request's number.", + "excluded": "Files on this computer, and the files of your other terminals.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "terminals.files", + "live_cap": null, + "rate": { + "per_ms": 1000, + "uses": 50 + }, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" + }, + { + "consent": "Take its toolbar off one of the terminal tabs it opened, or put it back.", + "deprecated": null, + "doc": "Takes the plugin's toolbar for its own tabs off one terminal tab it opened, or puts it back. The window's layout places the toolbar (the manifest suggests, the user moves or hides it), so the edge named is not read.", + "excluded": "Other tabs, and anything outside the tabs it opened.", + "family": "effect", + "fields": null, + "gesture": false, + "id": "terminals.bar", + "live_cap": null, + "rate": { + "per_ms": 1000, + "uses": 10 + }, + "session_derived": false, + "since": "0.1", + "target": "own-terminal" + } + ], + "edges": [ + "top", + "bottom", + "left", + "right" + ], + "effect_requests": { + "net.request": { + "body": { + "required": false, + "type": "text" + }, + "bytes": { + "required": false, + "type": "bool" + }, + "method": { + "required": true, + "type": "choice", + "vocabulary": "http_methods" + }, + "path": { + "required": true, + "type": "text" + } + }, + "notifications.post": { + "notice_key": { + "required": false, + "type": "text" + }, + "text": { + "required": true, + "type": "text" + }, + "tone": { + "required": true, + "type": "choice", + "vocabulary": "notice_tones" + } + }, + "panels.set-badge": { + "badge": { + "max": 65535, + "min": 0, + "required": false, + "type": "integer" + }, + "local": { + "required": true, + "type": "text" + } + }, + "panels.set-open": { + "local": { + "required": true, + "type": "text" + }, + "open": { + "required": true, + "type": "bool" + } + }, + "storage.remove": { + "key": { + "required": true, + "type": "text" + } + }, + "storage.set": { + "key": { + "required": true, + "type": "text" + }, + "value": { + "required": true, + "type": "text" + } + }, + "terminals.bar": { + "edge": { + "required": false, + "type": "choice", + "vocabulary": "edges" + }, + "terminal": { + "required": true, + "type": "text" + } + }, + "terminals.files": { + "answer": { + "required": true, + "type": "any" + }, + "request": { + "max": 4294967295, + "min": 0, + "required": true, + "type": "integer" + }, + "terminal": { + "required": true, + "type": "text" + } + }, + "terminals.open": { + "files": { + "required": false, + "type": "bool" + }, + "local": { + "required": true, + "type": "text" + }, + "terminal": { + "required": true, + "type": "text" + }, + "title": { + "required": true, + "type": "text" + } + }, + "terminals.write": { + "terminal": { + "required": true, + "type": "text" + }, + "text": { + "required": true, + "type": "text" + } + }, + "timers.set": { + "after_ms": { + "max": 4294967295, + "min": 0, + "required": true, + "type": "integer" + } + } + }, + "events": [ + "activated", + "toggled", + "visibility-changed", + "settings-changed", + "response", + "timer", + "terminal-line", + "terminal-closed", + "file-request" + ], + "filters": [ + "round(n)", + "percent", + "hh-mm", + "threshold(low, high)" + ], + "glyphs": [ + "box", + "gem", + "hexagon", + "plug", + "puzzle", + "shapes", + "activity", + "arrow-down", + "arrow-left", + "arrow-up", + "bell", + "cable", + "calendar", + "check", + "chevron-down", + "chevron-left", + "chevron-right", + "circle-alert", + "circle-check", + "circle-x", + "clock", + "container", + "copy", + "cpu", + "ellipsis", + "external-link", + "eye", + "file", + "file-text", + "folder", + "folder-open", + "gauge", + "globe", + "hard-drive", + "inbox", + "info", + "key", + "loader", + "lock", + "memory", + "network", + "pause", + "pencil", + "play", + "plus", + "minus", + "refresh", + "rotate", + "search", + "send", + "server", + "server-off", + "settings", + "square", + "stop", + "star", + "terminal", + "thermometer", + "trash", + "triangle-alert", + "upload", + "user", + "wrench", + "x", + "zap" + ], + "host_errors": [ + "not-granted", + "denied", + "rate-limited", + "unavailable" + ], + "http_methods": [ + "get", + "post", + "put", + "delete" + ], + "icons": [ + "box", + "gem", + "hexagon", + "plug", + "puzzle", + "shapes" + ], + "kinds": [ + "widget", + "panel", + "toolbar" + ], + "limits": { + "badge-chars": 8, + "button-chars": 48, + "capabilities-per-package": 16, + "chart-points": 120, + "chart-series": 2, + "code-chars": 16384, + "contributions-per-package": 8, + "description-chars": 160, + "effect-key-chars": 32, + "effects-per-call": 8, + "file-bytes": 163840, + "file-entries": 1000, + "file-name-bytes": 255, + "file-path-bytes": 1024, + "file-reason-chars": 200, + "file-requests-waiting": 8, + "file-timeout-ms": 30000, + "list-items": 100, + "live-notices": 3, + "min-refresh-ms": 250, + "min-timer-ms": 1000, + "name-chars": 48, + "node-key-chars": 32, + "nodes-per-view": 256, + "notice-chars": 200, + "package-bytes": 262144, + "placeholders-per-view": 64, + "reason-chars": 160, + "request-body-bytes": 229376, + "request-path-bytes": 2048, + "request-timeout-ms": 15000, + "requests-in-flight": 4, + "response-body-bytes": 262144, + "response-bytes-mode-bytes": 163840, + "round-decimals": 2, + "setting-fields": 32, + "setting-groups": 8, + "setting-help-chars": 160, + "setting-key-chars": 32, + "setting-label-chars": 48, + "setting-options": 16, + "setting-text-chars": 256, + "storage-key-chars": 64, + "storage-total-bytes": 65536, + "storage-value-bytes": 4096, + "template-chars": 512, + "terminal-key-chars": 32, + "terminal-line-chars": 1024, + "terminal-title-chars": 48, + "terminal-write-bytes": 65536, + "terminals-open": 4, + "text-chars": 200, + "timer-max-ms": 3600000, + "timers-waiting": 8, + "view-depth": 8 + }, + "manifest": { + "contribution": { + "edge": { + "required": false, + "type": "choice", + "vocabulary": "edges" + }, + "icon": { + "required": false, + "type": "choice", + "vocabulary": "icons" + }, + "kind": { + "required": true, + "type": "choice", + "vocabulary": "kinds" + }, + "local": { + "required": true, + "type": "text" + }, + "order": { + "max": 65535, + "min": 0, + "required": false, + "type": "integer" + }, + "shown": { + "required": false, + "type": "bool" + }, + "tabs": { + "required": false, + "type": "choice", + "vocabulary": "tabs" + }, + "title": { + "required": true, + "type": "text" + }, + "view": { + "required": false, + "type": "table" + }, + "zone": { + "required": false, + "type": "choice", + "vocabulary": "zones" + } + }, + "contribution.view": { + "nodes": { + "required": true, + "type": "table-list" + }, + "refresh": { + "required": false, + "type": "any" + } + }, + "contribution.view.nodes": { + "detail": { + "required": false, + "type": "text" + }, + "each": { + "required": false, + "type": "text" + }, + "folded": { + "required": false, + "type": "bool" + }, + "icon": { + "required": false, + "type": "choice", + "vocabulary": "glyphs" + }, + "key": { + "required": false, + "type": "text" + }, + "kind": { + "required": true, + "type": "choice", + "vocabulary": "node_kinds" + }, + "parent": { + "max": 65535, + "min": 0, + "required": false, + "type": "integer" + }, + "text": { + "required": false, + "type": "text" + }, + "tone": { + "required": false, + "type": "choice", + "vocabulary": "tones" + }, + "tone_when": { + "required": false, + "type": "text" + }, + "value": { + "required": false, + "type": "text" + } + }, + "manifest": { + "api": { + "required": true, + "type": "text" + }, + "capabilities": { + "required": false, + "type": "text-list" + }, + "contribution": { + "required": false, + "type": "table-list" + }, + "description": { + "required": false, + "type": "text" + }, + "id": { + "required": true, + "type": "text" + }, + "name": { + "required": true, + "type": "text" + }, + "network": { + "required": false, + "type": "table" + }, + "optional_capabilities": { + "required": false, + "type": "text-list" + }, + "publisher": { + "required": true, + "type": "text" + }, + "reasons": { + "required": false, + "type": "text-map" + }, + "runtime": { + "required": false, + "type": "choice", + "vocabulary": "runtimes" + }, + "settings": { + "required": false, + "type": "table" + } + }, + "network": { + "credential_header": { + "required": false, + "type": "text" + }, + "credential_prefix": { + "required": false, + "type": "text" + }, + "credential_setting": { + "required": false, + "type": "text" + }, + "origin_setting": { + "required": true, + "type": "text" + } + }, + "settings": { + "group": { + "required": false, + "type": "table-list" + }, + "version": { + "max": 65535, + "min": 0, + "required": false, + "type": "integer" + } + }, + "settings.group": { + "contribution": { + "required": false, + "type": "text" + }, + "field": { + "required": false, + "type": "table-list" + }, + "title": { + "required": true, + "type": "text" + } + }, + "settings.group.field": { + "default": { + "required": false, + "type": "any" + }, + "help": { + "required": false, + "type": "text" + }, + "key": { + "required": true, + "type": "text" + }, + "kind": { + "required": true, + "type": "choice", + "vocabulary": "settings_kinds" + }, + "label": { + "required": true, + "type": "text" + }, + "max": { + "max": 2147483647, + "min": -2147483648, + "required": false, + "type": "integer" + }, + "max_chosen": { + "max": 255, + "min": 0, + "required": false, + "type": "integer" + }, + "max_len": { + "max": 65535, + "min": 0, + "required": false, + "type": "integer" + }, + "min": { + "max": 2147483647, + "min": -2147483648, + "required": false, + "type": "integer" + }, + "options": { + "required": false, + "type": "table-list" + }, + "ordered": { + "required": false, + "type": "bool" + }, + "placeholder": { + "required": false, + "type": "text" + }, + "renamed_from": { + "required": false, + "type": "text" + }, + "step": { + "max": 2147483647, + "min": 1, + "required": false, + "type": "integer" + }, + "unit": { + "required": false, + "type": "text" + } + }, + "settings.group.field.options": { + "help": { + "required": false, + "type": "text" + }, + "key": { + "required": true, + "type": "text" + }, + "label": { + "required": true, + "type": "text" + } + } + }, + "node_kinds": [ + "row", + "column", + "text", + "icon", + "meter", + "badge", + "button", + "toggle", + "list", + "divider", + "spacer", + "code", + "section", + "item", + "icon-button", + "caption", + "field", + "chart" + ], + "notice_tones": [ + "info", + "caution" + ], + "refreshes": [ + "next-second", + "next-minute", + "after-ms" + ], + "runtimes": [ + "declarative", + "wasm" + ], + "settings_kinds": [ + "toggle", + "choice", + "multi-choice", + "number", + "text", + "secret" + ], + "tabs": [ + "own", + "all" + ], + "tones": [ + "default", + "muted", + "success", + "caution" + ], + "workspace_kinds": [ + "local", + "remote", + "container", + "folder" + ], + "zones": [ + "left", + "center", + "right" + ] +} diff --git a/crates/plugin-api/src/base64.rs b/crates/plugin-api/src/base64.rs new file mode 100644 index 00000000..fd3a0bb5 --- /dev/null +++ b/crates/plugin-api/src/base64.rs @@ -0,0 +1,77 @@ +//! Standard base64 (RFC 4648, with padding), for the bytes of files and request bodies that a +//! plugin passes through as text. + +use alloc::{string::String, vec::Vec}; + +const ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +/// The base64 text of `bytes`. +pub fn encode(bytes: &[u8]) -> String { + let mut out = String::with_capacity(bytes.len().div_ceil(3) * 4); + for chunk in bytes.chunks(3) { + let b = [ + chunk[0], + chunk.get(1).copied().unwrap_or(0), + chunk.get(2).copied().unwrap_or(0), + ]; + let n = (u32::from(b[0]) << 16) | (u32::from(b[1]) << 8) | u32::from(b[2]); + for (i, shift) in [18, 12, 6, 0].into_iter().enumerate() { + if i <= chunk.len() { + out.push(char::from(ALPHABET[((n >> shift) & 63) as usize])); + } else { + out.push('='); + } + } + } + out +} + +fn value(byte: u8) -> Option { + match byte { + b'A'..=b'Z' => Some(u32::from(byte - b'A')), + b'a'..=b'z' => Some(u32::from(byte - b'a') + 26), + b'0'..=b'9' => Some(u32::from(byte - b'0') + 52), + b'+' => Some(62), + b'/' => Some(63), + _ => None, + } +} + +/// The bytes of base64 `text`, or `None` when it is not valid padded base64. +pub fn decode(text: &str) -> Option> { + let bytes = text.as_bytes(); + if !bytes.len().is_multiple_of(4) { + return None; + } + let mut out = Vec::with_capacity(bytes.len() / 4 * 3); + let quads = bytes.len() / 4; + for (index, quad) in bytes.chunks(4).enumerate() { + let last = index + 1 == quads; + let padding = quad.iter().rev().take_while(|&&byte| byte == b'=').count(); + if padding > 2 || (padding > 0 && !last) { + return None; + } + let mut n = 0u32; + for &byte in &quad[..4 - padding] { + n = (n << 6) | value(byte)?; + } + n <<= 6 * padding as u32; + let decoded = [(n >> 16) as u8, (n >> 8) as u8, n as u8]; + out.extend_from_slice(&decoded[..3 - padding]); + } + Some(out) +} + +/// How many bytes `text` decodes to, without decoding it; `None` when its length or padding +/// cannot be base64. Characters are checked by [`decode`]. +pub fn decoded_len(text: &str) -> Option { + let bytes = text.as_bytes(); + if !bytes.len().is_multiple_of(4) { + return None; + } + let padding = bytes.iter().rev().take_while(|&&byte| byte == b'=').count(); + if padding > 2 { + return None; + } + Some(bytes.len() / 4 * 3 - padding) +} diff --git a/crates/plugin-api/src/capability.rs b/crates/plugin-api/src/capability.rs new file mode 100644 index 00000000..faf87a57 --- /dev/null +++ b/crates/plugin-api/src/capability.rs @@ -0,0 +1,48 @@ +//! The capability table: the only things a user plugin can see or ask for. +//! +//! [`CAPABILITIES`] is the one source. Each entry has a hand-written sealed marker in the module +//! of its domain, naming the data it carries. Released entries never change: a successor entry +//! plus `deprecated` replaces one. + +pub mod clock; +mod fields; +pub mod files; +pub mod monitor; +pub mod net; +pub mod notifications; +pub mod panels; +pub mod storage; +mod table; +pub mod terminals; +pub mod timers; +pub mod transfers; +pub mod workspace; + +pub use fields::{Fields, Value, ValueType}; +pub use table::{ + CAPABILITIES, CapabilityEntry, Deprecation, EffectTarget, Family, TableError, capability, + validate_table, +}; + +/// The fields of the data a capability observes, or `None` for an id without named fields. +pub fn fields_of(capability: &str) -> Option<&'static [(&'static str, ValueType)]> { + match capability { + clock::Now::ID => Some(clock::ClockNow::FIELDS), + workspace::Kind::ID => Some(workspace::WorkspaceState::FIELDS), + monitor::UsageNow::ID => Some(monitor::Usage::FIELDS), + transfers::CountsNow::ID => Some(transfers::Counts::FIELDS), + _ => None, + } +} + +mod sealed { + pub trait Sealed {} +} + +/// A capability marker. Only this crate implements it. +pub trait Capability: sealed::Sealed { + /// The id in [`CAPABILITIES`]. + const ID: &'static str; + /// What the capability observes, or the request an effect carries. + type Value; +} diff --git a/crates/plugin-api/src/capability/clock.rs b/crates/plugin-api/src/capability/clock.rs new file mode 100644 index 00000000..d2f6e0cd --- /dev/null +++ b/crates/plugin-api/src/capability/clock.rs @@ -0,0 +1,38 @@ +//! `clock.now`. + +use serde::{Deserialize, Serialize}; + +use super::{Capability, Fields, Value, ValueType, sealed}; + +/// The wall clock to the minute. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct ClockNow { + pub unix_minutes: i64, + pub utc_offset_seconds: i32, +} + +impl Fields for ClockNow { + const FIELDS: &'static [(&'static str, ValueType)] = &[ + ("unix_minutes", ValueType::Number), + ("utc_offset_seconds", ValueType::Number), + ]; + + fn field(&self, name: &str) -> Option { + match name { + "unix_minutes" => Some(Value::Number(self.unix_minutes as f64)), + "utc_offset_seconds" => Some(Value::Number(f64::from(self.utc_offset_seconds))), + _ => None, + } + } +} + +/// Marker of `clock.now`. +pub struct Now; + +impl sealed::Sealed for Now {} + +impl Capability for Now { + const ID: &'static str = "clock.now"; + type Value = ClockNow; +} diff --git a/crates/plugin-api/src/capability/fields.rs b/crates/plugin-api/src/capability/fields.rs new file mode 100644 index 00000000..fc889a75 --- /dev/null +++ b/crates/plugin-api/src/capability/fields.rs @@ -0,0 +1,57 @@ +//! Named fields of observed data, as templates read them. + +use alloc::string::String; + +/// The type of a value a template reads. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum ValueType { + Number, + Text, + Bool, + /// A wall-clock instant: only the `hh-mm` filter reads it. + Time, +} + +impl ValueType { + /// The frozen name used in the API surface. + pub const fn name(self) -> &'static str { + match self { + Self::Number => "number", + Self::Text => "text", + Self::Bool => "bool", + Self::Time => "time", + } + } +} + +/// A value read from observed data or a setting. +#[derive(Clone, Debug, PartialEq)] +pub enum Value { + Number(f64), + Text(String), + Bool(bool), + Time { + unix_minutes: i64, + utc_offset_seconds: i32, + }, +} + +impl Value { + pub fn value_type(&self) -> ValueType { + match self { + Self::Number(_) => ValueType::Number, + Self::Text(_) => ValueType::Text, + Self::Bool(_) => ValueType::Bool, + Self::Time { .. } => ValueType::Time, + } + } +} + +/// A data transfer object whose fields a template may name. +pub trait Fields { + /// Every field name and its type. + const FIELDS: &'static [(&'static str, ValueType)]; + + /// The value of a field, or `None` for a name outside [`Self::FIELDS`]. + fn field(&self, name: &str) -> Option; +} diff --git a/crates/plugin-api/src/capability/files.rs b/crates/plugin-api/src/capability/files.rs new file mode 100644 index 00000000..f78697ad --- /dev/null +++ b/crates/plugin-api/src/capability/files.rs @@ -0,0 +1,227 @@ +//! `terminals.files`: the files of a plugin's terminal tab. +//! +//! A plugin that opens a terminal tab with `files` set serves that tab's files: the +//! application's file browser shows them while the tab is active, and an AI chat the tab is +//! attached to reads and changes them with its usual file tools. The host asks the plugin with +//! a `file-request` event and the plugin answers with this capability's effect, under the +//! request's number. Paths are absolute POSIX paths under `/`; nothing the plugin answers is +//! ever a file of this computer. File contents travel as base64, so binary files keep their +//! bytes, and are at most [`MAX_FILE_BYTES`] long. + +use alloc::{string::String, vec::Vec}; +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed, terminals::MAX_TERMINAL_KEY}; +use crate::{LimitError, error::key_syntax}; + +/// Most bytes of one file read or written through a plugin. +pub const MAX_FILE_BYTES: usize = 160 * 1024; +/// Most entries of one directory listing. +pub const MAX_FILE_ENTRIES: usize = 1000; +/// Longest path the host asks about, in bytes. +pub const MAX_FILE_PATH: usize = 1024; +/// Longest entry name, in bytes. +pub const MAX_FILE_NAME: usize = 255; +/// Longest failure reason, in characters. +pub const MAX_FILE_REASON: usize = 200; +/// Most file requests of one tab waiting for the plugin's answer. +pub const MAX_FILE_REQUESTS: usize = 8; +/// How long the host waits for an answer before the request fails. +pub const FILE_TIMEOUT_MS: u32 = 30_000; + +/// What the host asks of a plugin's file system. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub enum FileOp { + /// The entries of a directory, answered with [`FileAnswer::Listed`]. + List { path: String }, + /// One entry, answered with [`FileAnswer::Found`]. + Stat { path: String }, + /// A file's contents, answered with [`FileAnswer::Content`]. + Read { path: String }, + /// Writes a file. With `create` it must not exist yet. `data` is base64. + Write { + path: String, + data: String, + create: bool, + }, + /// Creates a directory, answered with [`FileAnswer::Done`]. + CreateDir { path: String }, + /// Removes a file, or an empty directory when `directory` is set. + Remove { path: String, directory: bool }, + /// Renames an entry; the destination must not exist. + Rename { from: String, to: String }, +} + +impl FileOp { + /// The paths the request names. + pub fn paths(&self) -> [Option<&str>; 2] { + match self { + Self::List { path } + | Self::Stat { path } + | Self::Read { path } + | Self::Write { path, .. } + | Self::CreateDir { path } + | Self::Remove { path, .. } => [Some(path), None], + Self::Rename { from, to } => [Some(from), Some(to)], + } + } + + /// Paths are absolute, without `.` or `..` steps, NUL or doubled slashes, and bounded; + /// written data is base64 of at most [`MAX_FILE_BYTES`]. + pub fn check(&self) -> Result<(), LimitError> { + for path in self.paths().into_iter().flatten() { + check_path(path)?; + } + if let Self::Write { data, .. } = self + && crate::base64::decoded_len(data).is_none_or(|len| len > MAX_FILE_BYTES) + { + return Err(LimitError::TooLong { + field: "data", + max: MAX_FILE_BYTES, + }); + } + Ok(()) + } +} + +/// An absolute POSIX path the host may ask about. +pub fn check_path(path: &str) -> Result<(), LimitError> { + let unusual = || LimitError::Syntax { field: "path" }; + if path.len() > MAX_FILE_PATH { + return Err(LimitError::TooLong { + field: "path", + max: MAX_FILE_PATH, + }); + } + if !path.starts_with('/') || path.contains('\0') { + return Err(unusual()); + } + if path == "/" { + return Ok(()); + } + let steps = path.trim_end_matches('/').split('/').skip(1); + for step in steps { + if step.is_empty() || step == "." || step == ".." { + return Err(unusual()); + } + } + Ok(()) +} + +/// One entry of a plugin's file system. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct FileEntry { + pub name: String, + pub directory: bool, + /// Size in bytes, for a file. + pub size: Option, + /// Last change, in seconds since the Unix epoch. + pub modified: Option, +} + +impl FileEntry { + /// A name of one path step: not empty, not `.` or `..`, without `/`, NUL, control, + /// invisible or direction characters, at most [`MAX_FILE_NAME`] bytes. + pub fn check(&self) -> Result<(), LimitError> { + let name = &self.name; + if name.len() > MAX_FILE_NAME { + return Err(LimitError::TooLong { + field: "name", + max: MAX_FILE_NAME, + }); + } + if name.is_empty() + || name == "." + || name == ".." + || name.chars().any(|c| c == '/' || c.is_control()) + { + return Err(LimitError::Syntax { field: "name" }); + } + // Invisible and direction characters could disguise a name, as they could view text. + if crate::sanitize_display(name, MAX_FILE_NAME) != *name { + return Err(LimitError::Unsanitized { field: "name" }); + } + Ok(()) + } +} + +/// Why a file request failed. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub enum FileFailure { + NotFound, + PermissionDenied, + AlreadyExists, + /// The plugin's server does not offer this operation. + Unsupported, + /// Anything else, in at most [`MAX_FILE_REASON`] characters. + Other(String), +} + +/// A plugin's answer to one file request. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case", deny_unknown_fields)] +pub enum FileAnswer { + Listed(Vec), + Found(Option), + /// A file's contents as base64. + Content(String), + Done, + Failed(FileFailure), +} + +/// The effect of `terminals.files`: the answer to the file request `request` of `terminal`. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct FileReply { + pub terminal: String, + pub request: u32, + pub answer: FileAnswer, +} + +impl FileReply { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal")?; + match &self.answer { + FileAnswer::Listed(entries) => { + if entries.len() > MAX_FILE_ENTRIES { + return Err(LimitError::TooLong { + field: "entries", + max: MAX_FILE_ENTRIES, + }); + } + entries.iter().try_for_each(FileEntry::check) + } + FileAnswer::Found(entry) => entry.as_ref().map_or(Ok(()), FileEntry::check), + FileAnswer::Content(data) => match crate::base64::decoded_len(data) { + Some(len) if len <= MAX_FILE_BYTES => Ok(()), + Some(_) => Err(LimitError::TooLong { + field: "content", + max: MAX_FILE_BYTES, + }), + None => Err(LimitError::Syntax { field: "content" }), + }, + FileAnswer::Failed(FileFailure::Other(reason)) + if reason.chars().count() > MAX_FILE_REASON => + { + Err(LimitError::TooLong { + field: "reason", + max: MAX_FILE_REASON, + }) + } + FileAnswer::Done | FileAnswer::Failed(_) => Ok(()), + } + } +} + +/// Marker of `terminals.files`. +pub struct ServeFiles; + +impl sealed::Sealed for ServeFiles {} + +impl Capability for ServeFiles { + const ID: &'static str = "terminals.files"; + type Value = FileReply; +} diff --git a/crates/plugin-api/src/capability/monitor.rs b/crates/plugin-api/src/capability/monitor.rs new file mode 100644 index 00000000..bf477b59 --- /dev/null +++ b/crates/plugin-api/src/capability/monitor.rs @@ -0,0 +1,54 @@ +//! `monitor.usage`. + +use serde::{Deserialize, Serialize}; + +use super::{Capability, Fields, Value, ValueType, sealed}; +use crate::LimitError; + +/// Processor and memory use of the host that runs Nocterm, in percent: no processes, no names. +#[derive(Clone, Copy, Debug, PartialEq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Usage { + pub cpu_percent: f32, + pub memory_percent: f32, +} + +impl Usage { + /// Both values are from 0 to 100. + pub fn check(&self) -> Result<(), LimitError> { + let percent = |value: f32| (0.0..=100.0).contains(&value); + if percent(self.cpu_percent) && percent(self.memory_percent) { + Ok(()) + } else { + Err(LimitError::Range { + field: "usage", + max: 100, + }) + } + } +} + +impl Fields for Usage { + const FIELDS: &'static [(&'static str, ValueType)] = &[ + ("cpu_percent", ValueType::Number), + ("memory_percent", ValueType::Number), + ]; + + fn field(&self, name: &str) -> Option { + match name { + "cpu_percent" => Some(Value::Number(f64::from(self.cpu_percent))), + "memory_percent" => Some(Value::Number(f64::from(self.memory_percent))), + _ => None, + } + } +} + +/// Marker of `monitor.usage`. +pub struct UsageNow; + +impl sealed::Sealed for UsageNow {} + +impl Capability for UsageNow { + const ID: &'static str = "monitor.usage"; + type Value = Usage; +} diff --git a/crates/plugin-api/src/capability/net.rs b/crates/plugin-api/src/capability/net.rs new file mode 100644 index 00000000..aee525c0 --- /dev/null +++ b/crates/plugin-api/src/capability/net.rs @@ -0,0 +1,265 @@ +//! `net.request`: a request to the web address the user entered in the plugin's settings. +//! +//! The plugin names a method, a path and a body; the host joins the path to the origin the user +//! typed into the setting the manifest's `[network]` table names, adds the credential the user +//! entered (which the plugin never reads), sends it without cookies and without following +//! redirects, and answers with a `response` event under the request's key. + +use alloc::string::String; +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::LimitError; + +/// Longest request path, query included, in bytes. +pub const MAX_PATH: usize = 2048; +/// Longest request body, in bytes. +pub const MAX_BODY: usize = 224 * 1024; +/// Longest response body a plugin receives, in bytes; a longer one is cut there. +pub const MAX_RESPONSE: usize = 256 * 1024; +/// Most bytes of an answer to a `bytes` request, which comes back as base64 (a third longer) +/// and must still fit in the call that hands it to the plugin; a longer answer is refused, not +/// cut. +pub const MAX_BYTES_RESPONSE: usize = 160 * 1024; +/// Most requests of one plugin in flight at once. +pub const MAX_IN_FLIGHT: usize = 4; +/// How long the host waits for an answer, in milliseconds. +pub const TIMEOUT_MS: u32 = 15_000; + +/// The method of a request. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Method { + Get, + Post, + Put, + Delete, +} + +impl Method { + /// Every method, in declaration order. + pub const ALL: [Self; 4] = [Self::Get, Self::Post, Self::Put, Self::Delete]; + + pub const fn name(self) -> &'static str { + match self { + Self::Get => "get", + Self::Post => "post", + Self::Put => "put", + Self::Delete => "delete", + } + } +} + +/// A request to the plugin's origin. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Request { + pub method: Method, + /// The path and query below the origin: starts with one `/`, holds visible ASCII only. + pub path: String, + /// A JSON body, sent as `application/json`; with `bytes`, base64 of the raw bytes, sent as + /// `application/octet-stream`. + #[serde(default)] + pub body: Option, + /// The body is base64 of bytes, and the answer's body comes back as base64 of the bytes + /// the server sent, so a binary file keeps every byte. + #[serde(default)] + pub bytes: bool, +} + +impl Request { + pub fn check(&self) -> Result<(), LimitError> { + check_path(&self.path)?; + if self.body.as_ref().is_some_and(|body| body.len() > MAX_BODY) { + return Err(LimitError::TooLong { + field: "body", + max: MAX_BODY, + }); + } + if self.bytes + && self + .body + .as_deref() + .is_some_and(|body| crate::base64::decode(body).is_none()) + { + return Err(LimitError::Syntax { field: "body" }); + } + Ok(()) + } +} + +/// A path the host can join to an origin without leaving it: one leading `/` (never `//`, which +/// would name another host), visible ASCII without `\`, `#` or spaces, and no `..` segment. +pub fn check_path(path: &str) -> Result<(), LimitError> { + if path.len() > MAX_PATH { + return Err(LimitError::TooLong { + field: "path", + max: MAX_PATH, + }); + } + let syntax = LimitError::Syntax { field: "path" }; + let visible = path + .bytes() + .all(|byte| byte.is_ascii_graphic() && !matches!(byte, b'\\' | b'#')); + if !path.starts_with('/') || path.starts_with("//") || !visible { + return Err(syntax); + } + let route = path.split('?').next().unwrap_or_default(); + if route + .split('/') + .any(|segment| segment == ".." || segment == ".") + { + return Err(syntax); + } + Ok(()) +} + +/// Marker of `net.request`. +pub struct NetRequest; + +impl sealed::Sealed for NetRequest {} + +impl Capability for NetRequest { + const ID: &'static str = "net.request"; + type Value = Request; +} + +/// The web address a user typed into the setting that names a plugin's origin: `http` or +/// `https`, a host with an optional port, and an optional base path every request goes below. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Origin { + secure: bool, + authority: String, + base: String, +} + +/// A typed address the host does not send requests to. +#[derive(Clone, Copy, Debug, PartialEq, Eq, thiserror::Error)] +pub enum OriginError { + #[error("no address is set")] + Empty, + #[error("the address does not start with http:// or https://")] + Scheme, + #[error("the address has no host, or one with a user name, a space or a bad port")] + Host, + #[error("the address has a query, a fragment or an unusual path")] + Path, +} + +impl Origin { + pub fn parse(text: &str) -> Result { + let text = text.trim(); + if text.is_empty() { + return Err(OriginError::Empty); + } + let lower = text.to_ascii_lowercase(); + let (secure, rest) = if lower.starts_with("https://") { + (true, &text[8..]) + } else if lower.starts_with("http://") { + (false, &text[7..]) + } else { + return Err(OriginError::Scheme); + }; + let split = rest.find(['/', '?', '#']).unwrap_or(rest.len()); + let (authority, path) = rest.split_at(split); + if !authority_allowed(authority) { + return Err(OriginError::Host); + } + let base = path.trim_end_matches('/'); + if !base.is_empty() && check_path(base).is_err() { + return Err(OriginError::Path); + } + Ok(Self { + secure, + authority: authority.to_ascii_lowercase(), + base: String::from(base), + }) + } + + pub fn is_secure(&self) -> bool { + self.secure + } + + /// `host` or `host:port`, lower case. + pub fn authority(&self) -> &str { + &self.authority + } + + /// The address of `path` below the origin. + pub fn url(&self, path: &str) -> Result { + check_path(path)?; + let scheme = if self.secure { "https" } else { "http" }; + Ok(alloc::format!( + "{scheme}://{}{}{path}", + self.authority, + self.base + )) + } +} + +/// A host name, an IPv4 address or a bracketed IPv6 address, with an optional port. +fn authority_allowed(authority: &str) -> bool { + let (host, port) = match authority.rsplit_once(':') { + // An IPv6 address outside brackets: refused below by the host characters. + Some((host, _)) if !host.ends_with(']') && host.contains(':') => (authority, None), + Some((host, port)) => (host, Some(port)), + None => (authority, None), + }; + let port_fine = port.is_none_or(|port| { + !port.is_empty() + && port.len() <= 5 + && port.bytes().all(|byte| byte.is_ascii_digit()) + && port + .parse::() + .is_ok_and(|port| (1..=65535).contains(&port)) + }); + let bracketed = host.starts_with('[') && host.ends_with(']'); + let host_fine = if bracketed { + host.len() > 2 + && host[1..host.len() - 1] + .bytes() + .all(|byte| byte.is_ascii_hexdigit() || byte == b':' || byte == b'.') + } else { + !host.is_empty() + && host.len() <= 253 + && !host.starts_with(['.', '-']) + && host + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'.' || byte == b'-') + }; + port_fine && host_fine +} + +/// One request, ready to be sent: the whole address and the credential header. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NetCall { + pub method: Method, + /// The address below `origin`, built by [`Origin::url`]. + pub url: String, + /// The origin the address must stay at; the adapter checks it again after parsing. + pub origin: Origin, + /// The body's bytes: JSON text, or raw bytes when `bytes` is set. + pub body: Option>, + /// The body is raw bytes, and the answer comes back as base64. + pub bytes: bool, + /// The credential header and its value. Never logged. + pub credential: Option<(String, String)>, +} + +/// What came back: the status and the body (cut to [`MAX_RESPONSE`], lossily decoded), or +/// status 0 and the host's words for why nothing came. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct NetAnswer { + pub status: u16, + pub body: String, +} + +impl NetAnswer { + /// No answer came, for `reason`. + pub fn failed(reason: impl Into) -> Self { + Self { + status: 0, + body: reason.into(), + } + } +} diff --git a/crates/plugin-api/src/capability/notifications.rs b/crates/plugin-api/src/capability/notifications.rs new file mode 100644 index 00000000..e64cdc76 --- /dev/null +++ b/crates/plugin-api/src/capability/notifications.rs @@ -0,0 +1,121 @@ +//! `notifications.own` and `notifications.post`. + +use alloc::{string::String, string::ToString, vec::Vec}; +use serde::{Deserialize, Serialize}; + +use super::{Capability, Fields, Value, ValueType, sealed}; +use crate::{LimitError, error::key_syntax, sanitize_display}; + +/// Longest notice text. +pub const MAX_NOTICE_TEXT: usize = 200; +/// Most live notices one plugin has. +pub const MAX_LIVE_NOTICES: usize = 3; +const MAX_KEY: usize = 32; + +/// How a notice a plugin posts is drawn. There is no error level. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum NoticeTone { + Info, + Caution, +} + +impl NoticeTone { + /// Every tone, in declaration order. + pub const ALL: [Self; 2] = [Self::Info, Self::Caution]; + + pub const fn name(self) -> &'static str { + match self { + Self::Info => "info", + Self::Caution => "caution", + } + } +} + +/// A notice the plugin posted and the host still shows. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct OwnNotice { + pub tone: NoticeTone, + pub text: String, +} + +impl Fields for OwnNotice { + const FIELDS: &'static [(&'static str, ValueType)] = + &[("text", ValueType::Text), ("tone", ValueType::Text)]; + + fn field(&self, name: &str) -> Option { + match name { + "text" => Some(Value::Text(self.text.clone())), + "tone" => Some(Value::Text(self.tone.name().to_string())), + _ => None, + } + } +} + +/// The plugin's own live notices, never the host's. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct OwnNotices(pub Vec); + +impl OwnNotices { + pub fn check(&self) -> Result<(), LimitError> { + if self.0.len() > MAX_LIVE_NOTICES { + return Err(LimitError::TooMany { + field: "notices", + max: MAX_LIVE_NOTICES, + }); + } + Ok(()) + } +} + +/// Marker of `notifications.own`. +pub struct Own; + +impl sealed::Sealed for Own {} + +impl Capability for Own { + const ID: &'static str = "notifications.own"; + type Value = OwnNotices; +} + +/// A notice to post, with the key that replaces an earlier one of the same key. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Post { + pub tone: NoticeTone, + pub text: String, + #[serde(default)] + pub notice_key: Option, +} + +impl Post { + pub fn check(&self) -> Result<(), LimitError> { + if self.text.is_empty() { + return Err(LimitError::Empty { field: "text" }); + } + if self.text.chars().count() > MAX_NOTICE_TEXT { + return Err(LimitError::TooLong { + field: "text", + max: MAX_NOTICE_TEXT, + }); + } + if sanitize_display(&self.text, MAX_NOTICE_TEXT) != self.text { + return Err(LimitError::Unsanitized { field: "text" }); + } + self.notice_key + .as_deref() + .map_or(Ok(()), |key| key_syntax(key, MAX_KEY, "notice_key")) + } +} + +/// Marker of `notifications.post`. +pub struct PostNotice; + +impl sealed::Sealed for PostNotice {} + +impl Capability for PostNotice { + const ID: &'static str = "notifications.post"; + type Value = Post; +} diff --git a/crates/plugin-api/src/capability/panels.rs b/crates/plugin-api/src/capability/panels.rs new file mode 100644 index 00000000..7dab5e05 --- /dev/null +++ b/crates/plugin-api/src/capability/panels.rs @@ -0,0 +1,42 @@ +//! `panels.set-open` and `panels.set-badge`. + +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::LocalId; + +/// Opens or closes one of the plugin's own panels. It never moves focus. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct SetOpen { + pub local: LocalId, + pub open: bool, +} + +/// Marker of `panels.set-open`. +pub struct SetPanelOpen; + +impl sealed::Sealed for SetPanelOpen {} + +impl Capability for SetPanelOpen { + const ID: &'static str = "panels.set-open"; + type Value = SetOpen; +} + +/// Sets or clears the badge number on one of the plugin's own contributions. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct SetBadge { + pub local: LocalId, + pub badge: Option, +} + +/// Marker of `panels.set-badge`. +pub struct SetPanelBadge; + +impl sealed::Sealed for SetPanelBadge {} + +impl Capability for SetPanelBadge { + const ID: &'static str = "panels.set-badge"; + type Value = SetBadge; +} diff --git a/crates/plugin-api/src/capability/storage.rs b/crates/plugin-api/src/capability/storage.rs new file mode 100644 index 00000000..28b1517f --- /dev/null +++ b/crates/plugin-api/src/capability/storage.rs @@ -0,0 +1,68 @@ +//! `storage.set` and `storage.remove`. + +use alloc::string::String; +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::{LimitError, error::key_syntax}; + +/// Longest storage key. +pub const MAX_KEY: usize = 64; +/// Longest stored value, in bytes. +pub const MAX_VALUE: usize = 4 * 1024; +/// Most bytes one plugin stores in total. +pub const MAX_TOTAL: usize = 64 * 1024; + +/// Stores a value under a key of the plugin's own data. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Set { + pub key: String, + pub value: String, +} + +impl Set { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.key, MAX_KEY, "key")?; + if self.value.len() > MAX_VALUE { + return Err(LimitError::TooLong { + field: "value", + max: MAX_VALUE, + }); + } + Ok(()) + } +} + +/// Marker of `storage.set`. +pub struct StoreSet; + +impl sealed::Sealed for StoreSet {} + +impl Capability for StoreSet { + const ID: &'static str = "storage.set"; + type Value = Set; +} + +/// Removes a key of the plugin's own data. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Remove { + pub key: String, +} + +impl Remove { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.key, MAX_KEY, "key") + } +} + +/// Marker of `storage.remove`. +pub struct StoreRemove; + +impl sealed::Sealed for StoreRemove {} + +impl Capability for StoreRemove { + const ID: &'static str = "storage.remove"; + type Value = Remove; +} diff --git a/crates/plugin-api/src/capability/table.rs b/crates/plugin-api/src/capability/table.rs new file mode 100644 index 00000000..51cf7253 --- /dev/null +++ b/crates/plugin-api/src/capability/table.rs @@ -0,0 +1,365 @@ +//! The table itself, and the rules every entry follows. + +use alloc::collections::BTreeSet; +use core::time::Duration; + +use thiserror::Error; + +use crate::{API_VERSION, ApiVersion}; + +/// Whether a capability reads host state or asks the host to act. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Family { + Observe, + Effect, +} + +/// What an effect may act on. Host objects are outside the model. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum EffectTarget { + /// The plugin's own contributions. + OwnContribution, + /// The plugin's own stored data. + OwnData, + /// How the host presents something the plugin said, such as a notice. + HostPresentation, + /// The web address the user entered in the plugin's settings. + UserOrigin, + /// Terminal tabs the plugin opened. + OwnTerminal, + /// The plugin's own timers. + OwnTimer, +} + +/// A capability that still works but has a successor. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Deprecation { + pub since: ApiVersion, + pub successor: &'static str, + pub note: &'static str, +} + +/// One row of the table. Fields can be added later without breaking authors. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct CapabilityEntry { + /// `.`, kebab-case. + pub id: &'static str, + pub family: Family, + /// What it is, for authors. + pub doc: &'static str, + /// What the consent dialog says in the host's words. + pub consent: &'static str, + /// What the capability deliberately does not give. + pub excluded: &'static str, + /// Computed from a connection, host, project, file or other non-plugin user data, or from + /// the user's locale or time zone. + pub session_derived: bool, + /// Set exactly for effects. + pub target: Option, + /// Needs a user gesture token. + pub gesture: bool, + /// At most this many uses in this time. + pub rate: Option<(u16, Duration)>, + /// Most live results per plugin. + pub live_cap: Option, + pub since: ApiVersion, + pub deprecated: Option, +} + +const V1: ApiVersion = ApiVersion::new(0, 1); + +/// Every capability of this API version. +pub const CAPABILITIES: &[CapabilityEntry] = &[ + CapabilityEntry { + id: "clock.now", + family: Family::Observe, + doc: "The wall-clock time to the minute and the UTC offset of the user.", + consent: "See the current time and your time zone.", + excluded: "A timer finer than a minute.", + session_derived: true, + target: None, + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "workspace.kind", + family: Family::Observe, + doc: "Whether the window shows a local, remote, container or folder workspace, and whether it is connected.", + consent: "See what kind of workspace a window shows and whether it is connected.", + excluded: "Title, host, user, path and tab count.", + session_derived: true, + target: None, + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "monitor.usage", + family: Family::Observe, + doc: "Processor and memory use in percent, or unavailable when no built-in sampler runs.", + consent: "See how busy this computer is.", + excluded: "Processes and host names.", + session_derived: true, + target: None, + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "transfers.counts", + family: Family::Observe, + doc: "How many transfers run and how many failed.", + consent: "See how many file transfers are running or failed.", + excluded: "File names, paths and rates.", + session_derived: true, + target: None, + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "notifications.own", + family: Family::Observe, + doc: "The notices this plugin posted that are still shown.", + consent: "See the notices this plugin posted.", + excluded: "Notices of the host and of other plugins.", + session_derived: false, + target: None, + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "notifications.post", + family: Family::Effect, + doc: "Posts an informational or caution notice of up to 200 characters.", + consent: "Show notices in the status bar.", + excluded: "Actions, links and the error level.", + session_derived: false, + target: Some(EffectTarget::HostPresentation), + gesture: false, + rate: Some((1, Duration::from_secs(10))), + live_cap: Some(3), + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "panels.set-open", + family: Family::Effect, + doc: "Opens or closes one of the plugin's own panels after a click.", + consent: "Open or close its own panels when you click it.", + excluded: "Other contributions and moving focus.", + session_derived: false, + target: Some(EffectTarget::OwnContribution), + gesture: true, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "panels.set-badge", + family: Family::Effect, + doc: "Sets or clears a number badge on one of the plugin's own contributions.", + consent: "Show a number on its own panel or widget.", + excluded: "Attention requests.", + session_derived: false, + target: Some(EffectTarget::OwnContribution), + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "storage.set", + family: Family::Effect, + doc: "Stores a value under a key in the plugin's own data, up to 64 KiB in total.", + consent: "Keep a small amount of its own data.", + excluded: "Data of other plugins.", + session_derived: false, + target: Some(EffectTarget::OwnData), + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "storage.remove", + family: Family::Effect, + doc: "Removes a key from the plugin's own data.", + consent: "Remove its own stored data.", + excluded: "Data of other plugins.", + session_derived: false, + target: Some(EffectTarget::OwnData), + gesture: false, + rate: None, + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "net.request", + family: Family::Effect, + doc: "Sends a request to the web address the user entered in the setting the `[network]` table names, with the credential the user entered, and answers with a `response` event under the request's key.", + consent: "Send requests to the web address you enter in its settings, with the key you give it.", + excluded: "Other addresses, cookies, redirects and reading the key.", + session_derived: false, + target: Some(EffectTarget::UserOrigin), + gesture: false, + rate: Some((8, Duration::from_secs(1))), + live_cap: Some(4), + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "timers.set", + family: Family::Effect, + doc: "Wakes the plugin with a `timer` event under the effect's key after one second to one hour; setting a key that waits replaces it.", + consent: "Wake itself up after a delay to refresh what it shows.", + excluded: "Delays shorter than a second.", + session_derived: false, + target: Some(EffectTarget::OwnTimer), + gesture: false, + rate: Some((8, Duration::from_secs(1))), + live_cap: Some(8), + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.open", + family: Family::Effect, + doc: "Opens a terminal tab, or shows it, after a click. Lines typed into it, by the user or an AI chat it is attached to, arrive as `terminal-line` events.", + consent: "Open terminal tabs that show what it writes and pass on what you type.", + excluded: "Your other terminals and running anything on this computer.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: true, + rate: None, + live_cap: Some(4), + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.write", + family: Family::Effect, + doc: "Writes text with colours to one of the plugin's open terminal tabs.", + consent: "Write text to the terminal tabs it opened.", + excluded: "Other terminals, and control sequences other than colours.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: false, + rate: Some((50, Duration::from_secs(1))), + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.files", + family: Family::Effect, + doc: "Serves the files of a terminal tab the plugin opened with `files`: the file browser shows them while the tab is active and an AI chat it is attached to reads and changes them with its file tools. The host asks with `file-request` events and the plugin answers with this effect under the request's number.", + consent: "Show the files of the servers it opens as terminal tabs in your file browser, and let AI chats you attach those tabs to read and change them.", + excluded: "Files on this computer, and the files of your other terminals.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: false, + rate: Some((50, Duration::from_secs(1))), + live_cap: None, + since: V1, + deprecated: None, + }, + CapabilityEntry { + id: "terminals.bar", + family: Family::Effect, + doc: "Takes the plugin's toolbar for its own tabs off one terminal tab it opened, or puts it back. The window's layout places the toolbar (the manifest suggests, the user moves or hides it), so the edge named is not read.", + consent: "Take its toolbar off one of the terminal tabs it opened, or put it back.", + excluded: "Other tabs, and anything outside the tabs it opened.", + session_derived: false, + target: Some(EffectTarget::OwnTerminal), + gesture: false, + rate: Some((10, Duration::from_secs(1))), + live_cap: None, + since: V1, + deprecated: None, + }, +]; + +/// The entry with `id`. +pub fn capability(id: &str) -> Option<&'static CapabilityEntry> { + CAPABILITIES.iter().find(|entry| entry.id == id) +} + +/// A table the rules refuse. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum TableError { + #[error("capability `{0}` is not a kebab-case `domain.name`")] + Id(&'static str), + #[error("capability `{0}` is listed twice")] + Duplicate(&'static str), + #[error("capability `{0}` has a target exactly when it should not")] + Target(&'static str), + #[error("capability `{0}` has no consent or excluded text")] + Text(&'static str), + #[error("capability `{0}` is newer than this API version")] + Future(&'static str), + #[error("capability `{0}` is deprecated in favour of an entry the table does not hold")] + Successor(&'static str), +} + +/// Checks the rules of the table: kebab-case `.` ids, unique ids, `target` set +/// exactly for effects, non-empty `consent` and `excluded`, no entry newer than this API, and +/// deprecated entries naming a successor in the table. +pub fn validate_table(table: &[CapabilityEntry]) -> Result<(), TableError> { + let mut seen = BTreeSet::new(); + for entry in table { + if !is_id(entry.id) { + return Err(TableError::Id(entry.id)); + } + if !seen.insert(entry.id) { + return Err(TableError::Duplicate(entry.id)); + } + if entry.target.is_some() != (entry.family == Family::Effect) { + return Err(TableError::Target(entry.id)); + } + if entry.consent.trim().is_empty() || entry.excluded.trim().is_empty() { + return Err(TableError::Text(entry.id)); + } + if entry.since > API_VERSION { + return Err(TableError::Future(entry.id)); + } + } + for entry in table { + if let Some(deprecation) = entry.deprecated + && !seen.contains(deprecation.successor) + { + return Err(TableError::Successor(entry.id)); + } + } + Ok(()) +} + +fn is_id(id: &str) -> bool { + let kebab = |part: &str| { + !part.is_empty() + && !part.starts_with('-') + && !part.ends_with('-') + && !part.contains("--") + && part + .bytes() + .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-') + }; + id.split_once('.') + .is_some_and(|(domain, name)| kebab(domain) && kebab(name)) +} diff --git a/crates/plugin-api/src/capability/terminals.rs b/crates/plugin-api/src/capability/terminals.rs new file mode 100644 index 00000000..1541a951 --- /dev/null +++ b/crates/plugin-api/src/capability/terminals.rs @@ -0,0 +1,205 @@ +//! `terminals.open`, `terminals.write` and `terminals.bar`: terminal tabs a plugin drives. +//! +//! A plugin terminal is a tab like any other terminal of the window: the user reads and selects +//! its text, and attaches it to an AI chat, whose agent reads it and types into it. The host +//! echoes what is typed and hands each finished line to the plugin as a `terminal-line` event; +//! the plugin writes text back. Nothing runs on this computer. A tab may carry a bar of the +//! plugin along one of its edges: the contribution the tab reports to draws it, told which +//! terminal it draws for. + +use alloc::string::String; +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::{Edge, LimitError, LocalId, error::key_syntax, sanitize_display}; +use nocterm_plugin_model::sanitize_text; + +/// Longest terminal key. +pub const MAX_TERMINAL_KEY: usize = 32; +/// Longest terminal title, in characters. +pub const MAX_TERMINAL_TITLE: usize = 48; +/// Most bytes one write carries. +pub const MAX_WRITE: usize = 64 * 1024; +/// Most terminals one plugin keeps open. +pub const MAX_TERMINALS: usize = 4; +/// Longest line the host hands the plugin, in characters; typing stops there. +pub const MAX_LINE: usize = 1024; + +/// Opens a terminal tab, or shows it if it is open. Lines typed into it are reported to `local`, +/// one of the plugin's own contributions. With `files` the plugin serves the tab's files, which +/// needs `terminals.files` (see [`super::files`]). +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Open { + pub local: LocalId, + pub terminal: String, + pub title: String, + #[serde(default)] + pub files: bool, +} + +impl Open { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal")?; + if self.title.chars().count() > MAX_TERMINAL_TITLE { + return Err(LimitError::TooLong { + field: "title", + max: MAX_TERMINAL_TITLE, + }); + } + if sanitize_display(&self.title, MAX_TERMINAL_TITLE) != self.title { + return Err(LimitError::Unsanitized { field: "title" }); + } + Ok(()) + } +} + +/// Writes text to an open terminal of the plugin. The host keeps printable text, line breaks, +/// tabs and colours, and drops every other control sequence. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Write { + pub terminal: String, + pub text: String, +} + +impl Write { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal")?; + if self.text.len() > MAX_WRITE { + return Err(LimitError::TooLong { + field: "text", + max: MAX_WRITE, + }); + } + Ok(()) + } +} + +/// Takes the plugin's toolbar for its own tabs off one of its open terminal tabs (`edge: None`) +/// or puts it back (any edge). Where the toolbar runs is the window layout's: the manifest's +/// `edge` and `shown` suggest it and the user moves or hides it, so the edge named here is not +/// read. The toolbar draws the bar: the host asks it for a view with the snapshot's `terminal` +/// set, and reports what the user does in the bar to it the same way. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Bar { + pub terminal: String, + #[serde(default)] + pub edge: Option, +} + +impl Bar { + pub fn check(&self) -> Result<(), LimitError> { + key_syntax(&self.terminal, MAX_TERMINAL_KEY, "terminal") + } +} + +/// Marker of `terminals.open`. +pub struct OpenTerminal; + +impl sealed::Sealed for OpenTerminal {} + +impl Capability for OpenTerminal { + const ID: &'static str = "terminals.open"; + type Value = Open; +} + +/// Marker of `terminals.write`. +pub struct WriteTerminal; + +impl sealed::Sealed for WriteTerminal {} + +impl Capability for WriteTerminal { + const ID: &'static str = "terminals.write"; + type Value = Write; +} + +/// Marker of `terminals.bar`. +pub struct ShowTerminalBar; + +impl sealed::Sealed for ShowTerminalBar {} + +impl Capability for ShowTerminalBar { + const ID: &'static str = "terminals.bar"; + type Value = Bar; +} + +/// The bytes a terminal shows for `text`: printable characters, `\n` as a line break, `\t`, +/// and SGR colour sequences (`ESC [ digits ; … m`). Every other control character and escape +/// sequence is dropped whole, so a plugin cannot set the title, move the cursor, write the +/// clipboard, open a link or answer queries through the screen. +pub fn terminal_output(text: &str) -> String { + let mut out = String::with_capacity(text.len() + text.len() / 16); + let mut run = String::new(); + let mut chars = text.chars().peekable(); + while let Some(c) = chars.next() { + if !c.is_control() { + run.push(c); + continue; + } + // A run of plain text loses invisible and direction characters as view text does. + out.push_str(&sanitize_text(&run)); + run.clear(); + match c { + '\n' => out.push_str("\r\n"), + '\t' => out.push('\t'), + '\u{1b}' => match chars.next() { + Some('[') => keep_colour(&mut chars, &mut out), + Some(']' | 'P' | '_' | '^' | 'X') => skip_string(&mut chars), + _ => {} + }, + '\u{9b}' => { + let _ = control_sequence(&mut chars); + } + '\u{90}' | '\u{98}' | '\u{9d}' | '\u{9e}' | '\u{9f}' => skip_string(&mut chars), + _ => {} + } + } + out.push_str(&sanitize_text(&run)); + out +} + +/// Reads a control sequence after its introducer: parameters, intermediates and the final byte. +/// Returns the parameters and the final byte when it is well formed. +fn control_sequence( + chars: &mut core::iter::Peekable>, +) -> Option<(String, char)> { + let mut params = String::new(); + let mut plain = true; + for next in chars.by_ref() { + match next { + '0'..='9' | ';' if plain => { + if params.len() < 32 { + params.push(next); + } + } + '\u{30}'..='\u{3f}' | '\u{20}'..='\u{2f}' => plain = false, + '\u{40}'..='\u{7e}' => return plain.then_some((params, next)), + _ => return None, + } + } + None +} + +fn keep_colour(chars: &mut core::iter::Peekable>, out: &mut String) { + if let Some((params, 'm')) = control_sequence(chars) { + out.push_str("\u{1b}["); + out.push_str(¶ms); + out.push('m'); + } +} + +/// Skips a string sequence (OSC, DCS, APC, PM, SOS) up to its BEL or string terminator. +fn skip_string(chars: &mut core::iter::Peekable>) { + while let Some(next) = chars.next() { + match next { + '\u{7}' | '\u{9c}' => return, + '\u{1b}' if chars.peek() == Some(&'\\') => { + chars.next(); + return; + } + _ => {} + } + } +} diff --git a/crates/plugin-api/src/capability/timers.rs b/crates/plugin-api/src/capability/timers.rs new file mode 100644 index 00000000..efa4d970 --- /dev/null +++ b/crates/plugin-api/src/capability/timers.rs @@ -0,0 +1,45 @@ +//! `timers.set`: a `timer` event after a delay, under the effect's key. + +use serde::{Deserialize, Serialize}; + +use super::{Capability, sealed}; +use crate::LimitError; + +/// The shortest delay, in milliseconds. +pub const MIN_TIMER_MS: u32 = 1000; +/// The longest delay, in milliseconds. +pub const MAX_TIMER_MS: u32 = 3_600_000; +/// Most timers one plugin has waiting; setting a key that waits replaces it. +pub const MAX_TIMERS: usize = 8; + +/// Wakes the plugin with a `timer` event after `after_ms`. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct SetTimer { + pub after_ms: u32, +} + +impl SetTimer { + pub fn check(&self) -> Result<(), LimitError> { + if self.after_ms < MIN_TIMER_MS { + return Err(LimitError::Syntax { field: "after_ms" }); + } + if self.after_ms > MAX_TIMER_MS { + return Err(LimitError::Range { + field: "after_ms", + max: MAX_TIMER_MS, + }); + } + Ok(()) + } +} + +/// Marker of `timers.set`. +pub struct Timer; + +impl sealed::Sealed for Timer {} + +impl Capability for Timer { + const ID: &'static str = "timers.set"; + type Value = SetTimer; +} diff --git a/crates/plugin-api/src/capability/transfers.rs b/crates/plugin-api/src/capability/transfers.rs new file mode 100644 index 00000000..f54b3543 --- /dev/null +++ b/crates/plugin-api/src/capability/transfers.rs @@ -0,0 +1,36 @@ +//! `transfers.counts`. + +use serde::{Deserialize, Serialize}; + +use super::{Capability, Fields, Value, ValueType, sealed}; + +/// How many transfers run and how many failed: no names, paths or rates. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct Counts { + pub active: u16, + pub failed: u16, +} + +impl Fields for Counts { + const FIELDS: &'static [(&'static str, ValueType)] = + &[("active", ValueType::Number), ("failed", ValueType::Number)]; + + fn field(&self, name: &str) -> Option { + match name { + "active" => Some(Value::Number(f64::from(self.active))), + "failed" => Some(Value::Number(f64::from(self.failed))), + _ => None, + } + } +} + +/// Marker of `transfers.counts`. +pub struct CountsNow; + +impl sealed::Sealed for CountsNow {} + +impl Capability for CountsNow { + const ID: &'static str = "transfers.counts"; + type Value = Counts; +} diff --git a/crates/plugin-api/src/capability/workspace.rs b/crates/plugin-api/src/capability/workspace.rs new file mode 100644 index 00000000..b536c321 --- /dev/null +++ b/crates/plugin-api/src/capability/workspace.rs @@ -0,0 +1,61 @@ +//! `workspace.kind`. + +use alloc::string::ToString; +use serde::{Deserialize, Serialize}; + +use super::{Capability, Fields, Value, ValueType, sealed}; + +/// What the window's workspace is. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum WorkspaceKind { + Local, + Remote, + Container, + Folder, +} + +impl WorkspaceKind { + /// Every kind, in declaration order. + pub const ALL: [Self; 4] = [Self::Local, Self::Remote, Self::Container, Self::Folder]; + + pub const fn name(self) -> &'static str { + match self { + Self::Local => "local", + Self::Remote => "remote", + Self::Container => "container", + Self::Folder => "folder", + } + } +} + +/// The kind of workspace and whether it is connected: no title, host, user, path or tab count. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct WorkspaceState { + pub kind: WorkspaceKind, + pub connected: bool, +} + +impl Fields for WorkspaceState { + const FIELDS: &'static [(&'static str, ValueType)] = + &[("kind", ValueType::Text), ("connected", ValueType::Bool)]; + + fn field(&self, name: &str) -> Option { + match name { + "kind" => Some(Value::Text(self.kind.name().to_string())), + "connected" => Some(Value::Bool(self.connected)), + _ => None, + } + } +} + +/// Marker of `workspace.kind`. +pub struct Kind; + +impl sealed::Sealed for Kind {} + +impl Capability for Kind { + const ID: &'static str = "workspace.kind"; + type Value = WorkspaceState; +} diff --git a/crates/plugin-api/src/contribution.rs b/crates/plugin-api/src/contribution.rs new file mode 100644 index 00000000..36e531f5 --- /dev/null +++ b/crates/plugin-api/src/contribution.rs @@ -0,0 +1,148 @@ +//! The author-facing words for where and what a contribution is. + +use nocterm_plugin_model::{ContributionKind, Edge as ModelEdge, ToolbarTabs, Zone as ModelZone}; +use serde::{Deserialize, Serialize}; + +/// A zone of the status bar, as an author names it. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Zone { + Left, + Center, + Right, +} + +impl Zone { + /// Every zone, in declaration order. + pub const ALL: [Self; 3] = [Self::Left, Self::Center, Self::Right]; + + pub const fn name(self) -> &'static str { + match self { + Self::Left => "left", + Self::Center => "center", + Self::Right => "right", + } + } + + pub const fn to_model(self) -> ModelZone { + match self { + Self::Left => ModelZone::Left, + Self::Center => ModelZone::Center, + Self::Right => ModelZone::Right, + } + } +} + +/// What a contribution is, as an author names it. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Kind { + Widget, + Panel, + /// A strip along an edge of the tab area. + Toolbar, +} + +impl Kind { + /// Every kind, in declaration order. + pub const ALL: [Self; 3] = [Self::Widget, Self::Panel, Self::Toolbar]; + + pub const fn name(self) -> &'static str { + match self { + Self::Widget => "widget", + Self::Panel => "panel", + Self::Toolbar => "toolbar", + } + } + + pub const fn to_model(self) -> ContributionKind { + match self { + Self::Widget => ContributionKind::Widget, + Self::Panel => ContributionKind::Panel, + Self::Toolbar => ContributionKind::Toolbar, + } + } + + /// The author's word for a kind of the shell. + pub const fn of_model(kind: ContributionKind) -> Self { + match kind { + ContributionKind::Widget => Self::Widget, + ContributionKind::Panel => Self::Panel, + ContributionKind::Toolbar => Self::Toolbar, + } + } +} + +/// The edge of the tab area a toolbar suggests, as an author names it. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Edge { + /// Under the tab bar. + Top, + Bottom, + Left, + Right, +} + +impl Edge { + /// Every edge, in declaration order. + pub const ALL: [Self; 4] = [Self::Top, Self::Bottom, Self::Left, Self::Right]; + + pub const fn name(self) -> &'static str { + match self { + Self::Top => "top", + Self::Bottom => "bottom", + Self::Left => "left", + Self::Right => "right", + } + } + + pub const fn to_model(self) -> ModelEdge { + match self { + Self::Top => ModelEdge::Top, + Self::Bottom => ModelEdge::Bottom, + Self::Left => ModelEdge::Left, + Self::Right => ModelEdge::Right, + } + } +} + +/// Which tabs a toolbar runs along, as an author names it. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Tabs { + /// Only the tabs the plugin opened, inside each of them. + #[default] + Own, + /// Every tab, along the window's tab area. + All, +} + +impl Tabs { + /// Every choice, in declaration order. + pub const ALL: [Self; 2] = [Self::Own, Self::All]; + + pub const fn name(self) -> &'static str { + match self { + Self::Own => "own", + Self::All => "all", + } + } + + pub const fn to_model(self) -> ToolbarTabs { + match self { + Self::Own => ToolbarTabs::Own, + Self::All => ToolbarTabs::All, + } + } +} + +/// Where a contribution is drawn, which a snapshot tells the plugin. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct ContributionContext { + pub kind: Kind, + /// The status bar zone of a widget or panel toggle. For a toolbar, its axis: `center` + /// across the top or the bottom, `left` or `right` along that side, as a column. + pub zone: Zone, +} diff --git a/crates/plugin-api/src/declarative.rs b/crates/plugin-api/src/declarative.rs new file mode 100644 index 00000000..668f4f71 --- /dev/null +++ b/crates/plugin-api/src/declarative.rs @@ -0,0 +1,56 @@ +//! The runtime of declarative packages: views are templates, and there is no code. + +use alloc::{boxed::Box, collections::BTreeMap, string::String}; + +use crate::{ + Effects, Event, LocalId, Package, PluginInstance, PluginRuntime, RuntimeError, Snapshot, View, + template::ViewTemplate, +}; + +/// Interprets the templates of a package. +#[derive(Clone, Copy, Debug, Default)] +pub struct DeclarativeRuntime; + +struct Declarative { + views: BTreeMap, +} + +impl PluginRuntime for DeclarativeRuntime { + fn instantiate(&self, package: &Package) -> Result, RuntimeError> { + let views = package + .manifest() + .views() + .map(|(local, view)| (String::from(local), view.clone())) + .collect(); + Ok(Box::new(Declarative { views })) + } +} + +impl PluginInstance for Declarative { + fn render(&self, local: &LocalId, snapshot: &Snapshot) -> Result { + let template = self + .views + .get(local.as_str()) + .ok_or_else(|| RuntimeError::UnknownContribution(local.clone()))?; + template.render(snapshot).map_err(|error| match error { + crate::template::RenderError::Invalid(view) => RuntimeError::View(view), + crate::template::RenderError::TooManyNodes => { + RuntimeError::View(crate::ViewError::TooManyNodes) + } + }) + } + + /// A declarative package has no code, so no event produces an effect. + fn on_event( + &self, + local: &LocalId, + _event: Event, + _snapshot: &Snapshot, + ) -> Result { + if self.views.contains_key(local.as_str()) { + Ok(Effects::default()) + } else { + Err(RuntimeError::UnknownContribution(local.clone())) + } + } +} diff --git a/crates/plugin-api/src/effects.rs b/crates/plugin-api/src/effects.rs new file mode 100644 index 00000000..8576f2aa --- /dev/null +++ b/crates/plugin-api/src/effects.rs @@ -0,0 +1,213 @@ +//! What a plugin asks the host to do, and what the host tells it happened. + +use alloc::{collections::BTreeSet, string::String, vec::Vec}; + +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use crate::{ + HostError, LimitError, LocalId, + capability::{ + Capability, + files::{FileOp, FileReply, ServeFiles}, + net::{NetRequest, Request}, + notifications::{Post, PostNotice}, + panels::{SetBadge, SetOpen, SetPanelBadge, SetPanelOpen}, + storage::{Remove, Set, StoreRemove, StoreSet}, + terminals::{Bar, Open, OpenTerminal, ShowTerminalBar, Write, WriteTerminal}, + timers::{SetTimer, Timer}, + }, + error::key_syntax, +}; + +/// Most effects one call may return. +pub const MAX_EFFECTS: usize = 8; +/// Longest correlation key. +pub const MAX_EFFECT_KEY: usize = 32; + +/// Something the user did to a plugin's view, or a change the plugin may react to. +/// +/// No key, character or modifier data reaches the plugin. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Event { + /// A button was activated by pointer, or by Enter or Space on a focused host control. + Activated { + key: String, + }, + Toggled { + key: String, + on: bool, + }, + VisibilityChanged { + visible: bool, + }, + SettingsChanged, + /// The answer to the `net.request` effect with this key. `status` is the HTTP status, or 0 + /// when no answer came (the body then says why, in the host's words). A body longer than + /// the response limit is cut there; bytes that are not UTF-8 are replaced. + Response { + key: String, + status: u16, + body: String, + }, + /// The `timers.set` effect with this key ran out. + Timer { + key: String, + }, + /// A line was entered in one of the plugin's terminals, by the user or an AI chat. + TerminalLine { + terminal: String, + line: String, + }, + /// The user closed one of the plugin's terminals. + TerminalClosed { + terminal: String, + }, + /// The file browser or an AI chat asks the files of one of the plugin's terminals, opened + /// with `files`; the plugin answers with a `terminals.files` effect under `request`. + FileRequest { + terminal: String, + request: u32, + op: FileOp, + }, +} + +/// One thing a plugin asks for. Each variant is one effect capability. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +pub enum Effect { + #[serde(rename = "notifications.post")] + NotificationsPost(Post), + #[serde(rename = "panels.set-open")] + PanelsSetOpen(SetOpen), + #[serde(rename = "panels.set-badge")] + PanelsSetBadge(SetBadge), + #[serde(rename = "storage.set")] + StorageSet(Set), + #[serde(rename = "storage.remove")] + StorageRemove(Remove), + #[serde(rename = "net.request")] + NetRequest(Request), + #[serde(rename = "timers.set")] + TimersSet(SetTimer), + #[serde(rename = "terminals.open")] + TerminalsOpen(Open), + #[serde(rename = "terminals.write")] + TerminalsWrite(Write), + #[serde(rename = "terminals.files")] + TerminalsFiles(FileReply), + #[serde(rename = "terminals.bar")] + TerminalsBar(Bar), +} + +impl Effect { + /// The id of the capability the effect needs. + pub fn capability(&self) -> &'static str { + match self { + Self::NotificationsPost(_) => PostNotice::ID, + Self::PanelsSetOpen(_) => SetPanelOpen::ID, + Self::PanelsSetBadge(_) => SetPanelBadge::ID, + Self::StorageSet(_) => StoreSet::ID, + Self::StorageRemove(_) => StoreRemove::ID, + Self::NetRequest(_) => NetRequest::ID, + Self::TimersSet(_) => Timer::ID, + Self::TerminalsOpen(_) => OpenTerminal::ID, + Self::TerminalsWrite(_) => WriteTerminal::ID, + Self::TerminalsFiles(_) => ServeFiles::ID, + Self::TerminalsBar(_) => ShowTerminalBar::ID, + } + } + + /// Whether the outcome comes back as an event under the request's key, which it then needs. + pub fn needs_key(&self) -> bool { + matches!(self, Self::NetRequest(_) | Self::TimersSet(_)) + } + + /// The own contribution the effect names, if it names one. + pub fn local(&self) -> Option<&LocalId> { + match self { + Self::PanelsSetOpen(request) => Some(&request.local), + Self::PanelsSetBadge(request) => Some(&request.local), + Self::TerminalsOpen(request) => Some(&request.local), + _ => None, + } + } + + fn check(&self) -> Result<(), LimitError> { + match self { + Self::NotificationsPost(request) => request.check(), + Self::StorageSet(request) => request.check(), + Self::StorageRemove(request) => request.check(), + Self::NetRequest(request) => request.check(), + Self::TimersSet(request) => request.check(), + Self::TerminalsOpen(request) => request.check(), + Self::TerminalsWrite(request) => request.check(), + Self::TerminalsFiles(request) => request.check(), + Self::TerminalsBar(request) => request.check(), + Self::PanelsSetOpen(_) | Self::PanelsSetBadge(_) => Ok(()), + } + } +} + +/// An effect and the key its outcome comes back under. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct EffectRequest { + #[serde(default)] + pub key: Option, + pub effect: Effect, +} + +/// What `on_event` returns: at most [`MAX_EFFECTS`] requests. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(transparent)] +pub struct Effects(pub Vec); + +/// Effects the host drops whole. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum EffectsError { + #[error("a call returns at most {MAX_EFFECTS} effects")] + TooMany, + #[error("key `{key}` is used by two effects")] + DuplicateKey { key: String }, + #[error("effect {index} answers with an event and needs a key")] + MissingKey { index: usize }, + #[error("effect {index}: {source}")] + Limit { + index: usize, + #[source] + source: LimitError, + }, +} + +impl Effects { + /// Checks the count, the keys and the limits of every effect. + pub fn check(&self) -> Result<(), EffectsError> { + if self.0.len() > MAX_EFFECTS { + return Err(EffectsError::TooMany); + } + let mut keys = BTreeSet::new(); + for (index, request) in self.0.iter().enumerate() { + let limit = |source| EffectsError::Limit { index, source }; + if let Some(key) = &request.key { + key_syntax(key, MAX_EFFECT_KEY, "key").map_err(limit)?; + if !keys.insert(key.as_str()) { + return Err(EffectsError::DuplicateKey { key: key.clone() }); + } + } + if request.key.is_none() && request.effect.needs_key() { + return Err(EffectsError::MissingKey { index }); + } + request.effect.check().map_err(limit)?; + } + Ok(()) + } +} + +/// What happened to an effect of the previous call. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case")] +pub struct EffectOutcome { + pub key: Option, + pub result: Result<(), HostError>, +} diff --git a/crates/plugin-api/src/error.rs b/crates/plugin-api/src/error.rs new file mode 100644 index 00000000..461a943e --- /dev/null +++ b/crates/plugin-api/src/error.rs @@ -0,0 +1,59 @@ +//! Errors the host reports to a plugin, and the limits of plugin data. + +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +/// Why the host did not give a plugin what it asked for. +/// +/// `Faulted` is a host state and never reaches a guest. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Error)] +#[serde(rename_all = "kebab-case")] +pub enum HostError { + /// The plugin never declared the capability, or the user did not grant it for this package. + #[error("not granted")] + NotGranted, + /// The host refused this use of the capability. + #[error("denied")] + Denied, + /// The capability was used too often. + #[error("rate limited, retry after {retry_after_ms} ms")] + RateLimited { retry_after_ms: u32 }, + /// The data is not available now, or the optional capability was not granted. + #[error("unavailable")] + Unavailable, +} + +/// A value of plugin data outside its limits. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum LimitError { + #[error("`{field}` is longer than {max} characters")] + TooLong { field: &'static str, max: usize }, + #[error("`{field}` is empty")] + Empty { field: &'static str }, + #[error("`{field}` does not follow its syntax")] + Syntax { field: &'static str }, + #[error("`{field}` must be at most {max}")] + Range { field: &'static str, max: u32 }, + #[error("`{field}` has more than {max} entries")] + TooMany { field: &'static str, max: usize }, + #[error("`{field}` holds text that sanitizing would change")] + Unsanitized { field: &'static str }, +} + +/// Longest local or storage key kept in a message. +pub(crate) fn key_syntax(text: &str, max: usize, field: &'static str) -> Result<(), LimitError> { + if text.is_empty() { + return Err(LimitError::Empty { field }); + } + if text.len() > max { + return Err(LimitError::TooLong { field, max }); + } + let plain = text + .bytes() + .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || b"-._".contains(&byte)); + if plain { + Ok(()) + } else { + Err(LimitError::Syntax { field }) + } +} diff --git a/crates/plugin-api/src/grant.rs b/crates/plugin-api/src/grant.rs new file mode 100644 index 00000000..c644a7c0 --- /dev/null +++ b/crates/plugin-api/src/grant.rs @@ -0,0 +1,146 @@ +//! What the user allowed one exact package to use. +//! +//! A grant is bound to `(PluginId, sha256)`: a package with a new hash has no grant. The +//! capabilities a package lists as required are granted all together at consent; optional ones +//! only when the user accepts them there. An optional capability that was not granted answers +//! `unavailable`, which is how a plugin detects the feature. + +use alloc::{collections::BTreeSet, string::String, vec::Vec}; + +use nocterm_plugin_model::PluginId; +use thiserror::Error; + +use crate::{ContentHash, HostError, Manifest, Package}; + +/// A grant the host refuses to make. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum GrantError { + #[error("`{0}` is not an optional capability of the package")] + NotOptional(String), +} + +/// The capabilities the user allowed for one exact package. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Grant { + plugin: PluginId, + hash: ContentHash, + required: BTreeSet, + declared_optional: BTreeSet, + granted_optional: BTreeSet, +} + +impl Grant { + /// The grant of a consent to `package`: every required capability, and the optional ones the + /// user accepted. Accepting an optional capability the package does not list is refused. + /// + /// The manifest and the hash come from one [`Package`], so a grant cannot join the + /// capabilities of one version with the hash of another. + pub fn consent(package: &Package, accepted: &[&str]) -> Result { + let manifest = package.manifest(); + let declared_optional: BTreeSet = + manifest.optional().iter().map(|c| c.id().into()).collect(); + let mut granted_optional = BTreeSet::new(); + for id in accepted { + let known = manifest.optional().iter().any(|optional| { + optional.id() == *id && matches!(optional, crate::OptionalCapability::Known(_)) + }); + if !known { + return Err(GrantError::NotOptional((*id).into())); + } + granted_optional.insert((*id).into()); + } + Ok(Self { + plugin: manifest.plugin().id().clone(), + hash: *package.hash(), + required: manifest.required().iter().map(|e| e.id.into()).collect(), + declared_optional, + granted_optional, + }) + } + + pub fn plugin(&self) -> &PluginId { + &self.plugin + } + + pub fn hash(&self) -> &ContentHash { + &self.hash + } + + /// Whether the grant was made for exactly this package. + pub fn covers(&self, plugin: &PluginId, hash: &ContentHash) -> bool { + self.plugin == *plugin && self.hash == *hash + } + + /// Whether `capability` may be used by the package with `hash`. + /// + /// A different package, or a capability the package never declared, is `NotGranted`. An + /// optional capability the user did not accept is `Unavailable`. + pub fn check( + &self, + plugin: &PluginId, + hash: &ContentHash, + capability: &str, + ) -> Result<(), HostError> { + if !self.covers(plugin, hash) { + return Err(HostError::NotGranted); + } + if self.required.contains(capability) || self.granted_optional.contains(capability) { + Ok(()) + } else if self.declared_optional.contains(capability) { + Err(HostError::Unavailable) + } else { + Err(HostError::NotGranted) + } + } + + /// Every capability the grant allows. + pub fn granted(&self) -> impl Iterator { + self.required + .iter() + .chain(&self.granted_optional) + .map(String::as_str) + } +} + +/// The capabilities and settings keys `next` adds over `previous`, for the update dialog to +/// highlight. An optional capability that `next` requires counts as added: the user may have +/// declined it before. +#[derive(Clone, Debug, Default, PartialEq, Eq)] +pub struct Additions { + pub capabilities: Vec, + pub settings: Vec, +} + +impl Additions { + pub fn between(previous: &Manifest, next: &Manifest) -> Self { + let was_required = |id: &str| previous.required().iter().any(|e| e.id == id); + let was_listed = + |id: &str| was_required(id) || previous.optional().iter().any(|c| c.id() == id); + let required = next + .required() + .iter() + .map(|entry| entry.id) + .filter(|id| !was_required(id)); + let optional = next + .optional() + .iter() + .map(|capability| capability.id()) + .filter(|id| !was_listed(id)); + let capabilities = required.chain(optional).map(Into::into).collect(); + let settings = next + .plugin() + .settings() + .fields() + .filter(|field| previous.plugin().settings().field(&field.key).is_none()) + .map(|field| field.key.as_ref().into()) + .collect(); + Self { + capabilities, + settings, + } + } + + pub fn is_empty(&self) -> bool { + self.capabilities.is_empty() && self.settings.is_empty() + } +} diff --git a/crates/plugin-api/src/icon.rs b/crates/plugin-api/src/icon.rs new file mode 100644 index 00000000..ac04d9c2 --- /dev/null +++ b/crates/plugin-api/src/icon.rs @@ -0,0 +1,288 @@ +//! The icons a plugin names: [`PluginIcon`] for its contributions, [`Glyph`] inside its views. + +use nocterm_plugin_model::Icon; +use serde::{Deserialize, Serialize}; + +/// An icon a plugin names. Names are frozen and the list is append-only. +/// +/// Each is an icon that no system plugin or core contribution draws, so a plugin toggle cannot +/// imitate a host function (`Icon::user_allowed`; the app checks the catalog against this set). +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +#[non_exhaustive] +pub enum PluginIcon { + Box, + Gem, + Hexagon, + Plug, + Puzzle, + Shapes, +} + +impl PluginIcon { + /// Every icon, in declaration order. + pub const ALL: [Self; 6] = [ + Self::Box, + Self::Gem, + Self::Hexagon, + Self::Plug, + Self::Puzzle, + Self::Shapes, + ]; + + /// The frozen kebab-case name. + pub const fn name(self) -> &'static str { + match self { + Self::Box => "box", + Self::Gem => "gem", + Self::Hexagon => "hexagon", + Self::Plug => "plug", + Self::Puzzle => "puzzle", + Self::Shapes => "shapes", + } + } + + /// The host icon this one draws as. + pub const fn to_icon(self) -> Icon { + match self { + Self::Box => Icon::Box, + Self::Gem => Icon::Gem, + Self::Hexagon => Icon::Hexagon, + Self::Plug => Icon::Plug, + Self::Puzzle => Icon::Puzzle, + Self::Shapes => Icon::Shapes, + } + } +} + +/// An icon inside a plugin's own content: a row's leading icon, an icon button, an icon node. +/// +/// A plugin may draw any of these in the frame the host gives it, where none can stand for a +/// function of the application; what a plugin shows outside that frame (the switcher of its +/// panel) is a [`PluginIcon`]. Names are frozen and the list is append-only; the first six are +/// the plugin icons, in the same order. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +#[non_exhaustive] +pub enum Glyph { + Box, + Gem, + Hexagon, + Plug, + Puzzle, + Shapes, + Activity, + ArrowDown, + ArrowLeft, + ArrowUp, + Bell, + Cable, + Calendar, + Check, + ChevronDown, + ChevronLeft, + ChevronRight, + CircleAlert, + CircleCheck, + CircleX, + Clock, + Container, + Copy, + Cpu, + Ellipsis, + ExternalLink, + Eye, + File, + FileText, + Folder, + FolderOpen, + Gauge, + Globe, + HardDrive, + Inbox, + Info, + Key, + Loader, + Lock, + Memory, + Network, + Pause, + Pencil, + Play, + Plus, + Minus, + Refresh, + Rotate, + Search, + Send, + Server, + ServerOff, + Settings, + Square, + Stop, + Star, + Terminal, + Thermometer, + Trash, + TriangleAlert, + Upload, + User, + Wrench, + X, + Zap, +} + +impl Glyph { + /// Every glyph, in declaration order. + pub const ALL: [Self; 65] = [ + Self::Box, + Self::Gem, + Self::Hexagon, + Self::Plug, + Self::Puzzle, + Self::Shapes, + Self::Activity, + Self::ArrowDown, + Self::ArrowLeft, + Self::ArrowUp, + Self::Bell, + Self::Cable, + Self::Calendar, + Self::Check, + Self::ChevronDown, + Self::ChevronLeft, + Self::ChevronRight, + Self::CircleAlert, + Self::CircleCheck, + Self::CircleX, + Self::Clock, + Self::Container, + Self::Copy, + Self::Cpu, + Self::Ellipsis, + Self::ExternalLink, + Self::Eye, + Self::File, + Self::FileText, + Self::Folder, + Self::FolderOpen, + Self::Gauge, + Self::Globe, + Self::HardDrive, + Self::Inbox, + Self::Info, + Self::Key, + Self::Loader, + Self::Lock, + Self::Memory, + Self::Network, + Self::Pause, + Self::Pencil, + Self::Play, + Self::Plus, + Self::Minus, + Self::Refresh, + Self::Rotate, + Self::Search, + Self::Send, + Self::Server, + Self::ServerOff, + Self::Settings, + Self::Square, + Self::Stop, + Self::Star, + Self::Terminal, + Self::Thermometer, + Self::Trash, + Self::TriangleAlert, + Self::Upload, + Self::User, + Self::Wrench, + Self::X, + Self::Zap, + ]; + + /// The frozen kebab-case name. + pub const fn name(self) -> &'static str { + match self { + Self::Box => "box", + Self::Gem => "gem", + Self::Hexagon => "hexagon", + Self::Plug => "plug", + Self::Puzzle => "puzzle", + Self::Shapes => "shapes", + Self::Activity => "activity", + Self::ArrowDown => "arrow-down", + Self::ArrowLeft => "arrow-left", + Self::ArrowUp => "arrow-up", + Self::Bell => "bell", + Self::Cable => "cable", + Self::Calendar => "calendar", + Self::Check => "check", + Self::ChevronDown => "chevron-down", + Self::ChevronLeft => "chevron-left", + Self::ChevronRight => "chevron-right", + Self::CircleAlert => "circle-alert", + Self::CircleCheck => "circle-check", + Self::CircleX => "circle-x", + Self::Clock => "clock", + Self::Container => "container", + Self::Copy => "copy", + Self::Cpu => "cpu", + Self::Ellipsis => "ellipsis", + Self::ExternalLink => "external-link", + Self::Eye => "eye", + Self::File => "file", + Self::FileText => "file-text", + Self::Folder => "folder", + Self::FolderOpen => "folder-open", + Self::Gauge => "gauge", + Self::Globe => "globe", + Self::HardDrive => "hard-drive", + Self::Inbox => "inbox", + Self::Info => "info", + Self::Key => "key", + Self::Loader => "loader", + Self::Lock => "lock", + Self::Memory => "memory", + Self::Network => "network", + Self::Pause => "pause", + Self::Pencil => "pencil", + Self::Play => "play", + Self::Plus => "plus", + Self::Minus => "minus", + Self::Refresh => "refresh", + Self::Rotate => "rotate", + Self::Search => "search", + Self::Send => "send", + Self::Server => "server", + Self::ServerOff => "server-off", + Self::Settings => "settings", + Self::Square => "square", + Self::Stop => "stop", + Self::Star => "star", + Self::Terminal => "terminal", + Self::Thermometer => "thermometer", + Self::Trash => "trash", + Self::TriangleAlert => "triangle-alert", + Self::Upload => "upload", + Self::User => "user", + Self::Wrench => "wrench", + Self::X => "x", + Self::Zap => "zap", + } + } +} + +impl From for Glyph { + fn from(icon: PluginIcon) -> Self { + match icon { + PluginIcon::Box => Self::Box, + PluginIcon::Gem => Self::Gem, + PluginIcon::Hexagon => Self::Hexagon, + PluginIcon::Plug => Self::Plug, + PluginIcon::Puzzle => Self::Puzzle, + PluginIcon::Shapes => Self::Shapes, + } + } +} diff --git a/crates/plugin-api/src/identity.rs b/crates/plugin-api/src/identity.rs new file mode 100644 index 00000000..b7f51bc8 --- /dev/null +++ b/crates/plugin-api/src/identity.rs @@ -0,0 +1,133 @@ +//! Identifiers a package and its host agree on: the local name of a contribution and the hash +//! of a package. + +use alloc::string::{String, ToString}; +use core::fmt; + +use serde::{Deserialize, Deserializer, Serialize, Serializer, de}; +use thiserror::Error; + +use crate::LimitError; + +const MAX_LOCAL: usize = 32; + +/// The part of a contribution id after the plugin's namespace: `[a-z0-9-]{1,32}`. +#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct LocalId(String); + +impl LocalId { + pub fn parse(text: &str) -> Result { + if text.is_empty() { + return Err(LimitError::Empty { field: "local" }); + } + if text.len() > MAX_LOCAL { + return Err(LimitError::TooLong { + field: "local", + max: MAX_LOCAL, + }); + } + let plain = text + .bytes() + .all(|byte| byte.is_ascii_lowercase() || byte.is_ascii_digit() || byte == b'-'); + if plain { + Ok(Self(text.to_string())) + } else { + Err(LimitError::Syntax { field: "local" }) + } + } + + pub fn as_str(&self) -> &str { + &self.0 + } +} + +impl fmt::Display for LocalId { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(&self.0) + } +} + +impl Serialize for LocalId { + fn serialize(&self, serializer: S) -> Result { + serializer.serialize_str(&self.0) + } +} + +impl<'de> Deserialize<'de> for LocalId { + fn deserialize>(deserializer: D) -> Result { + let text = String::deserialize(deserializer)?; + Self::parse(&text).map_err(de::Error::custom) + } +} + +/// A text that is not a SHA-256 hash in lowercase hexadecimal. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +#[error("not a 64-digit lowercase hexadecimal hash")] +pub struct HashError; + +/// The SHA-256 of the exact bytes of a package. Grants and records are bound to it. +#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)] +pub struct ContentHash([u8; 32]); + +impl ContentHash { + pub const fn new(bytes: [u8; 32]) -> Self { + Self(bytes) + } + + pub fn as_bytes(&self) -> &[u8; 32] { + &self.0 + } + + pub fn parse(text: &str) -> Result { + let digits = text.as_bytes(); + if digits.len() != 64 { + return Err(HashError); + } + let mut bytes = [0u8; 32]; + let (pairs, _) = digits.as_chunks::<2>(); + for (byte, [high, low]) in bytes.iter_mut().zip(pairs) { + *byte = (nibble(*high)? << 4) | nibble(*low)?; + } + Ok(Self(bytes)) + } + + /// The first 16 hexadecimal digits, which the consent dialog shows as the fingerprint. + pub fn fingerprint(&self) -> String { + let mut text = self.to_string(); + text.truncate(16); + text + } +} + +fn nibble(digit: u8) -> Result { + match digit { + b'0'..=b'9' => Ok(digit - b'0'), + b'a'..=b'f' => Ok(digit - b'a' + 10), + _ => Err(HashError), + } +} + +impl fmt::Display for ContentHash { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + self.0.iter().try_for_each(|byte| write!(f, "{byte:02x}")) + } +} + +impl fmt::Debug for ContentHash { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "ContentHash({self})") + } +} + +impl Serialize for ContentHash { + fn serialize(&self, serializer: S) -> Result { + serializer.collect_str(self) + } +} + +impl<'de> Deserialize<'de> for ContentHash { + fn deserialize>(deserializer: D) -> Result { + let text = String::deserialize(deserializer)?; + Self::parse(&text).map_err(de::Error::custom) + } +} diff --git a/crates/plugin-api/src/lib.rs b/crates/plugin-api/src/lib.rs new file mode 100644 index 00000000..b17ea44f --- /dev/null +++ b/crates/plugin-api/src/lib.rs @@ -0,0 +1,96 @@ +//! The contract between the host and plugins installed from a package. +//! +//! A plugin is a function from a snapshot of what it was allowed to see to a view, and from an +//! event to at most eight effects. This crate holds that data and the rules around it, and +//! nothing that runs a plugin or reads a file. +//! +//! Owns: the capability table (`CAPABILITIES`) with its sealed markers, `Snapshot`, `View` and +//! its arena rules, `Effects`, `Event`, `HostError`, `sanitize_display`, `PluginIcon` and its map +//! to the host icon, the author words `Zone` and `Kind`, `API_VERSION`, manifest DTOs and their +//! validation (ids, names, the clamp, the `[settings]` mapping onto `SettingsSchema`), grants +//! bound to `(PluginId, sha256)`, the declarative template language and its interpreter +//! (`DeclarativeRuntime`), the code package format (`package_format`: kind detection and the +//! bounded scan for the manifest section of a WebAssembly module), `Runtimes` (which picks the +//! declarative or the injected code runtime by the manifest), the requests and answers of a +//! terminal tab's files (`capability::files`) and the base64 their bytes travel in, the bar of a +//! terminal tab (`terminals.bar`) and the snapshot's `terminal` its calls name, and the +//! `PluginPackages`, `PluginRuntime` and `PluginNet` ports with their errors. +//! Does not own: file access, TOML parsing, hashing, the broker, rate limits, providers, views +//! or any GPUI type. +//! Ports used/provided: provides `PluginPackages` (implemented by `nocterm-plugin-packages`) +//! and `PluginRuntime` (implemented here for declarative packages and by `nocterm-plugin-wasm` +//! for code); uses none. +//! Settings section: none; user plugin settings live in the host's records, not in +//! `settings.toml`. Globals: none. +//! Invariants: the table has unique kebab-case ids and an effect target exactly for effects; +//! every guest string is sanitized and bounded before it is shown; a view is an arena with +//! `parent < index`, unique keys and depth at most 8; a package cannot express a built-in +//! privilege; a template value is inserted as literal text and never parsed again. +//! `no_std` with `alloc`; the isolation check keeps it free of I/O and `unsafe`. +#![no_std] + +extern crate alloc; +#[cfg(test)] +extern crate std; + +pub mod base64; +pub mod capability; +mod contribution; +mod declarative; +mod effects; +mod error; +mod grant; +mod icon; +mod identity; +mod manifest; +mod package_format; +mod ports; +mod runtimes; +mod sanitize; +mod snapshot; +mod surface; +pub mod template; +mod version; +mod view; + +pub use capability::{ + CAPABILITIES, Capability, CapabilityEntry, Deprecation, EffectTarget, Family, TableError, + validate_table, +}; +pub use contribution::{ContributionContext, Edge, Kind, Tabs, Zone}; +pub use declarative::DeclarativeRuntime; +pub use effects::{ + Effect, EffectOutcome, EffectRequest, Effects, EffectsError, Event, MAX_EFFECT_KEY, MAX_EFFECTS, +}; +pub use error::{HostError, LimitError}; +pub use grant::{Additions, Grant, GrantError}; +pub use icon::{Glyph, PluginIcon}; +pub use identity::{ContentHash, HashError, LocalId}; +pub use manifest::{ + ChoiceOptionDto, ContributionDto, ContributionError, Credential, DeclareError, DefaultDto, + FieldDto, FieldKindName, GroupDto, HostContext, MAX_CONTRIBUTIONS, MAX_DECLARED, + MAX_DESCRIPTION, MAX_NAME, MAX_REASON, Manifest, ManifestDto, ManifestError, ManifestHeader, + Network, NetworkDto, NetworkError, NetworkTableError, OptionalCapability, Runtime, SettingsDto, + SettingsError, +}; +pub use package_format::{MANIFEST_SECTION, PackageKind, SectionError, kind, manifest_section}; +pub use ports::{ + Cause, MAX_MODULE_BYTES, MAX_PACKAGE_BYTES, ModuleError, Package, PackageError, PluginInstance, + PluginNet, PluginPackages, PluginRuntime, ProtocolError, RuntimeError, +}; +pub use runtimes::Runtimes; +pub use sanitize::{sanitize_code, sanitize_display}; +pub use snapshot::{Observed, Snapshot}; +pub use surface::{ + EFFECT_REQUESTS, EVENTS, FILTERS, HOST_ERRORS, Key, KeyType, LIMITS, MANIFEST_KEYS, REFRESHES, + RUNTIMES, SETTINGS_KINDS, +}; +pub use version::{API_VERSION, ApiVersion, VersionError}; +pub use view::{ + MAX_BADGE, MAX_BUTTON, MAX_CHART_POINTS, MAX_CHART_SERIES, MAX_CODE, MAX_DEPTH, MAX_LIST_ITEMS, + MAX_NODE_KEY, MAX_NODES, MAX_TEXT, MIN_REFRESH_MS, Node, NodeKind, Refresh, Tone, View, + ViewError, +}; + +#[cfg(test)] +mod tests; diff --git a/crates/plugin-api/src/manifest.rs b/crates/plugin-api/src/manifest.rs new file mode 100644 index 00000000..ddeee6cb --- /dev/null +++ b/crates/plugin-api/src/manifest.rs @@ -0,0 +1,321 @@ +//! Package manifests: the data an author writes, and the checks before anything is shown for +//! consent. +//! +//! A package is one TOML file. The adapter parses it into [`ManifestDto`] (unknown keys are +//! refused, because a package is written for one API version); [`Manifest::validate`] then +//! builds the ids, clamps the contributions, maps the `[settings]` table onto a schema, resolves +//! every template against what the package declares and refuses anything that breaks a limit. +//! Nothing here reads a file. + +use alloc::{ + collections::BTreeMap, + string::{String, ToString}, + vec::Vec, +}; + +use nocterm_plugin_model::{ContributionId, IdError, PluginId, UserPlugin, UserPluginError}; +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use crate::{ + API_VERSION, ApiVersion, CapabilityEntry, LimitError, LocalId, VersionError, sanitize_display, + template::ViewTemplate, +}; + +mod contribution; +mod declare; +mod network; +mod settings; + +pub use contribution::{ContributionDto, ContributionError}; +pub use declare::{DeclareError, MAX_DECLARED, OptionalCapability}; +pub use network::{Credential, Network, NetworkDto, NetworkError, NetworkTableError}; +pub use settings::{ + ChoiceOptionDto, DefaultDto, FieldDto, FieldKindName, GroupDto, SettingsDto, SettingsError, +}; + +/// Most contributions in one package. +pub const MAX_CONTRIBUTIONS: usize = 8; +/// Longest plugin name, in characters. +pub const MAX_NAME: usize = 48; +/// Longest description, in characters. +pub const MAX_DESCRIPTION: usize = 160; +/// Longest reason an author gives for a capability, in characters. +pub const MAX_REASON: usize = 160; + +/// How a package runs. +#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Runtime { + /// Views are templates the host interprets; the package has no code. + #[default] + Declarative, + /// The package is a WebAssembly module that draws its views and answers events in code. + Wasm, +} + +/// The `api` key alone, read first, so a package written for a newer API is told so instead of +/// failing on keys this host does not know. +#[derive(Clone, Debug, PartialEq, Eq, Deserialize)] +pub struct ManifestHeader { + pub api: String, +} + +impl ManifestHeader { + /// The API version of the package, if this host can run it. + pub fn check(&self, host: ApiVersion) -> Result { + let api: ApiVersion = self.api.parse()?; + if api.runs_on(host) { + Ok(api) + } else { + Err(ManifestError::RequiresNewer { api }) + } + } +} + +/// A manifest as written. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct ManifestDto { + pub api: String, + #[serde(default)] + pub runtime: Runtime, + /// `[a-z0-9]+`; the host builds the plugin id `user--`. + pub publisher: String, + /// `[a-z0-9]+`. + pub id: String, + /// The display name, `[A-Za-z0-9 ._-]`, at most 48 characters. + pub name: String, + #[serde(default)] + pub description: String, + #[serde(default)] + pub capabilities: Vec, + #[serde(default)] + pub optional_capabilities: Vec, + /// Why a capability is wanted, by capability id. Shown as "Author says (unverified)". + #[serde(default)] + pub reasons: BTreeMap, + #[serde(default, rename = "contribution")] + pub contributions: Vec, + #[serde(default)] + pub settings: Option, + /// Where `net.request` goes and what it carries: the keys of [`NetworkDto`]. + #[serde(default)] + pub network: Option>, +} + +/// What the host knows that a manifest is checked against. +#[derive(Clone, Copy, Debug)] +pub struct HostContext<'a> { + pub api: ApiVersion, + /// Names a package may not take, compared without case: system plugin names and the titles of + /// built-in contributions and groups. "nocterm" is always refused. + pub reserved_names: &'a [&'a str], +} + +impl<'a> HostContext<'a> { + /// The context of a host that runs declarative packages only. + #[must_use] + pub const fn new(api: ApiVersion, reserved_names: &'a [&'a str]) -> Self { + Self { + api, + reserved_names, + } + } +} + +impl Default for HostContext<'_> { + fn default() -> Self { + Self::new(API_VERSION, &[]) + } +} + +/// A manifest the host refuses before consent. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum ManifestError { + #[error("invalid api version")] + Version(#[from] VersionError), + #[error("Requires a newer Nocterm (API {api})")] + RequiresNewer { api: ApiVersion }, + #[error("invalid id")] + Id(#[from] IdError), + #[error( + "the name `{0}` is not allowed: use letters, digits, spaces, `.`, `_` and `-` and avoid the names the app uses" + )] + Name(String), + #[error("a package has at most {MAX_CONTRIBUTIONS} contributions and needs at least one")] + Contributions, + #[error("a package value is outside its limits")] + Limit(#[from] LimitError), + #[error("capabilities")] + Declare(#[from] DeclareError), + #[error("contribution `{local}`")] + Contribution { + local: String, + #[source] + source: ContributionError, + }, + #[error("settings")] + Settings(#[from] SettingsError), + #[error("network")] + Network(#[from] NetworkError), + #[error("the plugin is invalid")] + Plugin(#[from] UserPluginError), +} + +/// A checked package manifest. +#[derive(Clone, Debug, PartialEq)] +pub struct Manifest { + api: ApiVersion, + runtime: Runtime, + publisher: String, + plugin: UserPlugin, + required: Vec<&'static CapabilityEntry>, + optional: Vec, + reasons: BTreeMap, + views: BTreeMap, + network: Option, +} + +impl Manifest { + /// Checks everything the host can check without running anything. + pub fn validate(dto: &ManifestDto, host: &HostContext<'_>) -> Result { + let api = ManifestHeader { + api: dto.api.clone(), + } + .check(host.api)?; + let id = PluginId::user(&dto.publisher, &dto.id)?; + let name = checked_name(&dto.name, host)?; + if dto.contributions.is_empty() || dto.contributions.len() > MAX_CONTRIBUTIONS { + return Err(ManifestError::Contributions); + } + let declaration = declare::Declaration::resolve( + &dto.capabilities, + &dto.optional_capabilities, + &dto.reasons, + )?; + match dto.runtime { + Runtime::Declarative => declaration.observation_only()?, + Runtime::Wasm => {} + } + let schema = settings::schema(&id, dto.settings.as_ref())?; + let requests = network::declares_requests( + declaration + .required + .iter() + .map(|entry| entry.id) + .chain(declaration.optional.iter().map(OptionalCapability::id)), + ); + let network = network::network(dto.network.as_ref(), requests, &schema)?; + let built = contribution::build( + &id, + dto.runtime, + &dto.contributions, + &declaration, + &schema, + host, + )?; + let description = sanitize_display(&dto.description, MAX_DESCRIPTION); + let plugin = UserPlugin::new(id, &name, &description, built.contributions, schema)?; + Ok(Self { + api, + runtime: dto.runtime, + publisher: dto.publisher.clone(), + plugin, + required: declaration.required, + optional: declaration.optional, + reasons: declaration.reasons, + views: built.views, + network, + }) + } + + /// Where the package's requests go and the credential they carry, when it declares + /// `net.request`. + pub fn network(&self) -> Option<&Network> { + self.network.as_ref() + } + + pub fn api(&self) -> ApiVersion { + self.api + } + + pub fn runtime(&self) -> Runtime { + self.runtime + } + + /// The publisher part of the id, which the host cannot verify. + pub fn publisher(&self) -> &str { + &self.publisher + } + + /// The plugin as the shell sees it: id, name, clamped contributions and settings schema. + pub fn plugin(&self) -> &UserPlugin { + &self.plugin + } + + /// Capabilities the user must grant for the package to be installed. + pub fn required(&self) -> &[&'static CapabilityEntry] { + &self.required + } + + /// Capabilities the user may grant at consent; without a grant they are unavailable. + pub fn optional(&self) -> &[OptionalCapability] { + &self.optional + } + + /// The author's reason for a capability, sanitized, to be quoted as unverified. + pub fn reason(&self, capability: &str) -> Option<&str> { + self.reasons.get(capability).map(String::as_str) + } + + /// Whether the package declares a contribution with this local name, whatever its runtime. + pub fn has_contribution(&self, local: &LocalId) -> bool { + ContributionId::in_plugin(self.plugin.id(), local.as_str()).is_ok_and(|id| { + self.plugin + .contributions() + .iter() + .any(|contribution| *contribution.id() == id) + }) + } + + /// The checked view of a declarative contribution, by its local name. A code package has + /// none: its contributions draw in code. + pub fn view(&self, local: &str) -> Option<&ViewTemplate> { + self.views.get(local) + } + + /// Every checked view with the local name of its contribution. + pub fn views(&self) -> impl Iterator { + self.views + .iter() + .map(|(local, view)| (local.as_str(), view)) + } +} + +/// A name or title the host shows as it is: not blank, at most `MAX_NAME` characters, and only +/// ASCII letters, digits, space, `.`, `_` and `-`. No invisible or look-alike character can +/// pass, so what the reserved-name check compares is what the user reads. +pub(crate) fn is_plain(name: &str) -> bool { + !name.trim().is_empty() + && name.chars().count() <= MAX_NAME + && name + .chars() + .all(|c| c.is_ascii_alphanumeric() || matches!(c, ' ' | '.' | '_' | '-')) +} + +fn checked_name(name: &str, host: &HostContext<'_>) -> Result { + if !is_plain(name) || is_reserved(name, host) { + return Err(ManifestError::Name(name.chars().take(MAX_NAME).collect())); + } + Ok(name.trim().to_string()) +} + +pub(crate) fn is_reserved(name: &str, host: &HostContext<'_>) -> bool { + let name = name.trim(); + name.eq_ignore_ascii_case("nocterm") + || host + .reserved_names + .iter() + .any(|reserved| reserved.trim().eq_ignore_ascii_case(name)) +} diff --git a/crates/plugin-api/src/manifest/contribution.rs b/crates/plugin-api/src/manifest/contribution.rs new file mode 100644 index 00000000..92aad233 --- /dev/null +++ b/crates/plugin-api/src/manifest/contribution.rs @@ -0,0 +1,159 @@ +//! The contributions of a package: clamped for the shell, with a checked view each. + +use alloc::{ + collections::{BTreeMap, BTreeSet}, + string::String, + vec::Vec, +}; + +use nocterm_plugin_model::{ContributionId, IdError, PluginId, SettingsSchema, UserContribution}; +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use super::{HostContext, ManifestError, Runtime, declare::Declaration, is_plain, is_reserved}; +use crate::{ + Edge, Kind, LimitError, LocalId, PluginIcon, Tabs, Zone, + template::{ViewDto, ViewTemplate, ViewTemplateError}, +}; + +/// A contribution as written. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct ContributionDto { + /// `[a-z0-9-]{1,32}`; the host builds the id `.`. + pub local: String, + pub kind: Option, + pub title: String, + #[serde(default)] + pub icon: Option, + /// Where it sits once the user places it. Default: right. + #[serde(default)] + pub zone: Option, + #[serde(default)] + pub order: u16, + /// The edge of the tab area a toolbar suggests. Default: top. Toolbars only. + #[serde(default)] + pub edge: Option, + /// Whether a toolbar is shown along its `edge` once the plugin is turned on, before the user + /// hides or moves it. Default: it waits until the user shows it. Toolbars only. + #[serde(default)] + pub shown: bool, + /// Which tabs a toolbar runs along: `own`, the tabs the plugin opened (the default), or + /// `all`. Toolbars only. + #[serde(default)] + pub tabs: Option, + /// The template of a declarative contribution. A code package draws in code and has none. + #[serde(default)] + pub view: Option, +} + +/// A contribution the host refuses. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum ContributionError { + #[error("invalid local name")] + Local(#[from] LimitError), + #[error("invalid id")] + Id(#[from] IdError), + #[error("it needs a `kind` of widget, panel or toolbar")] + NoKind, + #[error("only a toolbar runs along an `edge`")] + EdgeNotToolbar, + #[error("only a toolbar can be `shown` from the start; panels and widgets wait in the tray")] + ShownNotToolbar, + #[error("only a toolbar runs along `tabs`")] + TabsNotToolbar, + #[error("a plugin has one toolbar for its own tabs")] + SecondOwnTabsToolbar, + #[error( + "its title must be 1 to 48 letters, digits, spaces, `.`, `_` or `-`, and not a name the app reserves" + )] + Title, + #[error("it is declared twice")] + Duplicate, + #[error("its view")] + View(#[from] ViewTemplateError), + #[error("a declarative package needs a `view` for it")] + NoView, + #[error("a code package draws in code and cannot give it a `view`")] + ViewInCode, +} + +pub(super) struct Built { + pub(super) contributions: Vec, + pub(super) views: BTreeMap, +} + +pub(super) fn build( + plugin: &PluginId, + runtime: Runtime, + dtos: &[ContributionDto], + declaration: &Declaration, + schema: &SettingsSchema, + host: &HostContext<'_>, +) -> Result { + let observing = declaration.observing(); + let mut contributions = Vec::with_capacity(dtos.len()); + let mut views = BTreeMap::new(); + let mut locals = BTreeSet::new(); + let mut own_tabs = BTreeSet::new(); + for dto in dtos { + let fail = |source: ContributionError| ManifestError::Contribution { + local: dto.local.chars().take(32).collect(), + source, + }; + let local = LocalId::parse(&dto.local).map_err(|e| fail(e.into()))?; + let kind = dto.kind.ok_or_else(|| fail(ContributionError::NoKind))?; + if dto.edge.is_some() && kind != Kind::Toolbar { + return Err(fail(ContributionError::EdgeNotToolbar)); + } + if dto.shown && kind != Kind::Toolbar { + return Err(fail(ContributionError::ShownNotToolbar)); + } + if dto.tabs.is_some() && kind != Kind::Toolbar { + return Err(fail(ContributionError::TabsNotToolbar)); + } + let tabs = dto.tabs.unwrap_or_default(); + if kind == Kind::Toolbar && tabs == Tabs::Own && !own_tabs.insert(()) { + return Err(fail(ContributionError::SecondOwnTabsToolbar)); + } + if !is_plain(&dto.title) { + return Err(fail(ContributionError::Title)); + } + let id = ContributionId::in_plugin(plugin, local.as_str()).map_err(|e| fail(e.into()))?; + let view = match (runtime, dto.view.as_ref()) { + (Runtime::Declarative, Some(view)) => Some( + ViewTemplate::compile(view, &observing, schema) + .map_err(|e| fail(ContributionError::View(e)))?, + ), + (Runtime::Declarative, None) => return Err(fail(ContributionError::NoView)), + (Runtime::Wasm, Some(_)) => return Err(fail(ContributionError::ViewInCode)), + (Runtime::Wasm, None) => None, + }; + let clamped = UserContribution::clamp( + id, + kind.to_model(), + &dto.title, + dto.icon.unwrap_or(PluginIcon::Plug).to_icon(), + dto.zone.unwrap_or(Zone::Right).to_model(), + dto.order, + )? + .along(dto.edge.unwrap_or(Edge::Top).to_model()) + .shown_at_first(dto.shown) + .on_tabs(tabs.to_model()); + // The check that matters runs on the title the shell will show. + if is_reserved(clamped.title(), host) { + return Err(fail(ContributionError::Title)); + } + if !locals.insert(String::from(local.as_str())) { + return Err(fail(ContributionError::Duplicate)); + } + if let Some(view) = view { + views.insert(String::from(local.as_str()), view); + } + contributions.push(clamped); + } + Ok(Built { + contributions, + views, + }) +} diff --git a/crates/plugin-api/src/manifest/declare.rs b/crates/plugin-api/src/manifest/declare.rs new file mode 100644 index 00000000..3c086b29 --- /dev/null +++ b/crates/plugin-api/src/manifest/declare.rs @@ -0,0 +1,161 @@ +//! The capabilities a package declares, resolved against the table. + +use alloc::{ + collections::{BTreeMap, BTreeSet}, + string::{String, ToString}, + vec::Vec, +}; + +use thiserror::Error; + +use super::MAX_REASON; +use crate::{ + CapabilityEntry, + capability::{Family, capability}, + sanitize_display, +}; + +/// Most capabilities a package may list, required and optional together. +pub const MAX_DECLARED: usize = 16; + +/// An optional capability: one this API knows, or an id from a newer API. +#[derive(Clone, Debug, PartialEq)] +pub enum OptionalCapability { + Known(&'static CapabilityEntry), + /// Never granted; every use is `unavailable`. + Unknown(String), +} + +impl OptionalCapability { + pub fn id(&self) -> &str { + match self { + Self::Known(entry) => entry.id, + Self::Unknown(id) => id, + } + } +} + +/// A capability list the host refuses. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum DeclareError { + #[error("`{0}` is not a capability of this API; the package may need a newer Nocterm")] + UnknownRequired(String), + #[error("`{0}` is declared twice, or as both required and optional")] + Duplicate(String), + #[error("a package declares at most {MAX_DECLARED} capabilities")] + TooMany, + #[error("`{0}` is not a kebab-case `domain.name` id")] + Syntax(String), + #[error("a reason names `{0}`, which the package does not declare")] + ReasonFor(String), + #[error("`{0}` acts on the host, and a declarative package has no code that could use it")] + EffectInDeclarative(String), +} + +#[derive(Clone, Debug, PartialEq)] +pub(super) struct Declaration { + pub(super) required: Vec<&'static CapabilityEntry>, + pub(super) optional: Vec, + pub(super) reasons: BTreeMap, +} + +impl Declaration { + pub(super) fn resolve( + required: &[String], + optional: &[String], + reasons: &BTreeMap, + ) -> Result { + if required.len() + optional.len() > MAX_DECLARED { + return Err(DeclareError::TooMany); + } + let mut seen = BTreeSet::new(); + let mut entries = Vec::with_capacity(required.len()); + for id in required { + check_syntax(id)?; + if !seen.insert(id.as_str()) { + return Err(DeclareError::Duplicate(id.clone())); + } + entries.push(capability(id).ok_or_else(|| DeclareError::UnknownRequired(id.clone()))?); + } + let mut optionals = Vec::with_capacity(optional.len()); + for id in optional { + check_syntax(id)?; + if !seen.insert(id.as_str()) { + return Err(DeclareError::Duplicate(id.clone())); + } + optionals.push(match capability(id) { + Some(entry) => OptionalCapability::Known(entry), + None => OptionalCapability::Unknown(id.clone()), + }); + } + let mut kept = BTreeMap::new(); + for (id, reason) in reasons { + if !seen.contains(id.as_str()) { + return Err(DeclareError::ReasonFor(id.clone())); + } + kept.insert(id.clone(), sanitize_display(reason, MAX_REASON)); + } + Ok(Self { + required: entries, + optional: optionals, + reasons: kept, + }) + } + + /// Refuses effect capabilities. The declarative runtime produces no effect, so asking for + /// one would only put an unusable permission in front of the user at consent. + pub(super) fn observation_only(&self) -> Result<(), DeclareError> { + let optional = self + .optional + .iter() + .filter_map(|capability| match capability { + OptionalCapability::Known(entry) => Some(*entry), + OptionalCapability::Unknown(_) => None, + }); + match self + .required + .iter() + .copied() + .chain(optional) + .find(|entry| entry.family == Family::Effect) + { + Some(entry) => Err(DeclareError::EffectInDeclarative(entry.id.to_string())), + None => Ok(()), + } + } + + /// The observing capability ids a template may name: required and known optional ones. + pub(super) fn observing(&self) -> Vec<&'static str> { + let optional = self + .optional + .iter() + .filter_map(|capability| match capability { + OptionalCapability::Known(entry) => Some(*entry), + OptionalCapability::Unknown(_) => None, + }); + self.required + .iter() + .copied() + .chain(optional) + .map(|entry| entry.id) + .collect() + } +} + +fn check_syntax(id: &str) -> Result<(), DeclareError> { + let ok = id.split_once('.').is_some_and(|(domain, name)| { + [domain, name].iter().all(|part| { + !part.is_empty() + && part + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-') + }) + }); + if ok && id.len() <= 48 { + Ok(()) + } else { + Err(DeclareError::Syntax( + id.chars().take(48).collect::().to_string(), + )) + } +} diff --git a/crates/plugin-api/src/manifest/network.rs b/crates/plugin-api/src/manifest/network.rs new file mode 100644 index 00000000..ccadfe13 --- /dev/null +++ b/crates/plugin-api/src/manifest/network.rs @@ -0,0 +1,229 @@ +//! The `[network]` table: which settings hold the web address `net.request` reaches and the +//! credential the host sends with it. +//! +//! The origin is whatever the user types into the named text setting, so a plugin reaches only +//! the address its user chose. The credential is a secret setting the plugin never reads: the +//! host puts it into one request header, for that origin only. + +use alloc::{collections::BTreeMap, string::String}; + +use nocterm_plugin_model::{FieldKind, SettingsSchema}; +use serde::Serialize; +use thiserror::Error; + +use crate::capability::{Capability as _, net::NetRequest}; + +/// Longest credential header name. +const MAX_HEADER: usize = 64; +/// Longest credential prefix, in characters. +const MAX_PREFIX: usize = 32; + +/// The table as written. The manifest reads it as a table of text values, which +/// [`NetworkDto::try_from`] turns into this, refusing unknown keys. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize)] +#[serde(rename_all = "snake_case")] +pub struct NetworkDto { + /// The key of a text setting that holds the origin, such as `https://service.example.com`. + pub origin_setting: String, + /// The key of a secret text setting the host sends in `credential_header`. + #[serde(default)] + pub credential_setting: Option, + /// Default `Authorization`. Otherwise a header named `X-…`. + #[serde(default)] + pub credential_header: Option, + /// What comes before the credential in the header. Default `Bearer `. + #[serde(default)] + pub credential_prefix: Option, +} + +/// A `[network]` table that does not read. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum NetworkTableError { + #[error("missing field `origin_setting`")] + NoOrigin, + #[error("unknown field `{0}`")] + Unknown(String), +} + +impl TryFrom> for NetworkDto { + type Error = NetworkTableError; + + fn try_from(mut table: BTreeMap) -> Result { + let dto = Self { + origin_setting: table + .remove("origin_setting") + .ok_or(NetworkTableError::NoOrigin)?, + credential_setting: table.remove("credential_setting"), + credential_header: table.remove("credential_header"), + credential_prefix: table.remove("credential_prefix"), + }; + match table.into_keys().next() { + Some(key) => Err(NetworkTableError::Unknown(key)), + None => Ok(dto), + } + } +} + +/// Where a package's requests go and what they carry, checked against its settings. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Network { + origin_setting: String, + credential: Option, +} + +/// The secret setting a request carries, and how. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct Credential { + setting: String, + header: String, + prefix: String, +} + +/// A `[network]` table, or its absence, the host refuses. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum NetworkError { + #[error("the `[network]` table does not read")] + Table(#[from] NetworkTableError), + #[error("a package that declares `net.request` needs a `[network]` table")] + Missing, + #[error("a `[network]` table needs the `net.request` capability")] + Unused, + #[error("`origin_setting` names `{0}`, which is not a text setting that is not secret")] + Origin(String), + #[error("`credential_setting` names `{0}`, which is not a secret text setting")] + Credential(String), + #[error("the secret setting `{0}` is not the credential of the `[network]` table")] + StraySecret(String), + #[error("`credential_header` must be `Authorization` or a header named `X-…`")] + Header, + #[error("`credential_prefix` holds at most {MAX_PREFIX} visible characters and spaces")] + Prefix, +} + +impl Network { + /// The key of the setting whose value is the origin of every request. + pub fn origin_setting(&self) -> &str { + &self.origin_setting + } + + pub fn credential(&self) -> Option<&Credential> { + self.credential.as_ref() + } +} + +impl Credential { + /// The key of the secret setting. + pub fn setting(&self) -> &str { + &self.setting + } + + pub fn header(&self) -> &str { + &self.header + } + + pub fn prefix(&self) -> &str { + &self.prefix + } +} + +/// Checks the table against the declared capabilities and the settings schema. +pub(super) fn network( + table: Option<&BTreeMap>, + requests: bool, + schema: &SettingsSchema, +) -> Result, NetworkError> { + let dto = table.cloned().map(NetworkDto::try_from).transpose()?; + let Some(dto) = dto.as_ref() else { + if requests { + return Err(NetworkError::Missing); + } + return match stray_secret(schema, None) { + Some(key) => Err(NetworkError::StraySecret(key)), + None => Ok(None), + }; + }; + if !requests { + return Err(NetworkError::Unused); + } + let text = |key: &str| { + schema.field(key).and_then(|field| match field.kind { + FieldKind::Text { secret, .. } => Some(secret), + _ => None, + }) + }; + if text(&dto.origin_setting) != Some(false) { + return Err(NetworkError::Origin(dto.origin_setting.clone())); + } + let credential = match &dto.credential_setting { + None => { + if dto.credential_header.is_some() || dto.credential_prefix.is_some() { + return Err(NetworkError::Credential(String::new())); + } + None + } + Some(key) => { + if text(key) != Some(true) { + return Err(NetworkError::Credential(key.clone())); + } + let header = dto + .credential_header + .clone() + .unwrap_or_else(|| String::from("Authorization")); + if !header_allowed(&header) { + return Err(NetworkError::Header); + } + let prefix = dto + .credential_prefix + .clone() + .unwrap_or_else(|| String::from("Bearer ")); + if prefix.chars().count() > MAX_PREFIX + || !prefix + .bytes() + .all(|byte| byte.is_ascii_graphic() || byte == b' ') + { + return Err(NetworkError::Prefix); + } + Some(Credential { + setting: key.clone(), + header, + prefix, + }) + } + }; + if let Some(key) = stray_secret(schema, credential.as_ref()) { + return Err(NetworkError::StraySecret(key)); + } + Ok(Some(Network { + origin_setting: dto.origin_setting.clone(), + credential, + })) +} + +/// Whether a package declares `net.request`, required or optional. +pub(super) fn declares_requests<'a>(mut ids: impl Iterator) -> bool { + ids.any(|id| id == NetRequest::ID) +} + +/// A secret setting that is not the credential: a package may not collect secrets it never +/// sends. +fn stray_secret(schema: &SettingsSchema, credential: Option<&Credential>) -> Option { + schema + .fields() + .filter(|field| field.kind.is_secret()) + .find(|field| credential.is_none_or(|credential| credential.setting != field.key)) + .map(|field| String::from(&*field.key)) +} + +/// `Authorization`, or a header of the `X-` family, in token characters. +fn header_allowed(header: &str) -> bool { + let token = !header.is_empty() + && header.len() <= MAX_HEADER + && header + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-'); + let named = header.eq_ignore_ascii_case("authorization") + || header + .get(..2) + .is_some_and(|start| start.eq_ignore_ascii_case("x-")); + token && named +} diff --git a/crates/plugin-api/src/manifest/settings.rs b/crates/plugin-api/src/manifest/settings.rs new file mode 100644 index 00000000..befe75d5 --- /dev/null +++ b/crates/plugin-api/src/manifest/settings.rs @@ -0,0 +1,301 @@ +//! The `[settings]` table of a package, mapped onto the shared settings schema. +//! +//! The kinds are `toggle`, `choice`, `multi-choice`, `number`, `text` and `secret` (masked +//! text the plugin never reads, which a `[network]` table sends); the limits and the +//! validation are the schema's. Labels, help and option labels are sanitized, at most 48 and 160 +//! characters. + +use alloc::{borrow::Cow, string::String, vec::Vec}; + +use nocterm_plugin_model::{ + ChoiceOption, ContributionId, FieldGroup, FieldKind, FieldSpec, PluginId, SettingsSchema, +}; +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use crate::sanitize_display; + +const MAX_LABEL: usize = 48; +const MAX_HELP: usize = 160; + +/// The kinds of setting an author writes. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum FieldKindName { + Toggle, + Choice, + MultiChoice, + Number, + Text, + /// Masked single-line text the plugin never reads: the credential a `[network]` table sends. + Secret, +} + +/// The default of a field. +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(untagged)] +pub enum DefaultDto { + Bool(bool), + Number(i32), + Text(String), + List(Vec), +} + +/// An option of a choice or multi-choice field. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct ChoiceOptionDto { + pub key: String, + pub label: String, + #[serde(default)] + pub help: Option, +} + +/// One setting as written. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct FieldDto { + pub key: String, + pub label: String, + #[serde(default)] + pub help: Option, + pub kind: Option, + #[serde(default)] + pub renamed_from: Option, + #[serde(default)] + pub default: Option, + /// For `choice` and `multi-choice`. + #[serde(default)] + pub options: Vec, + /// For `multi-choice`: whether the order of the chosen options matters. + #[serde(default)] + pub ordered: Option, + /// For `multi-choice`: the most options that may be chosen. + #[serde(default)] + pub max_chosen: Option, + /// For `number`. + #[serde(default)] + pub min: Option, + #[serde(default)] + pub max: Option, + #[serde(default)] + pub step: Option, + #[serde(default)] + pub unit: Option, + /// For `text`, in characters. + #[serde(default)] + pub max_len: Option, + #[serde(default)] + pub placeholder: Option, +} + +/// A titled run of settings, for the plugin or for one of its contributions. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct GroupDto { + pub title: String, + /// The local name of the contribution the group is shown for; absent for the plugin. + #[serde(default)] + pub contribution: Option, + #[serde(default)] + pub field: Vec, +} + +/// The `[settings]` table. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct SettingsDto { + /// The revision of the schema, raised when fields change. Default 1. + #[serde(default)] + pub version: Option, + #[serde(default)] + pub group: Vec, +} + +/// A setting the host refuses. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum SettingsError { + #[error("setting `{key}` needs a `kind`")] + NoKind { key: String }, + #[error("setting `{key}` does not take `{field}` for this kind")] + Stray { key: String, field: &'static str }, + #[error("setting `{key}` needs `{field}`")] + Missing { key: String, field: &'static str }, + #[error("setting `{key}` has a default of the wrong type")] + Default { key: String }, + #[error("the group `{title}` names a contribution that is not a valid local name")] + Group { title: String }, +} + +pub(super) fn schema( + plugin: &PluginId, + dto: Option<&SettingsDto>, +) -> Result { + let Some(dto) = dto else { + return Ok(SettingsSchema::NONE); + }; + let mut groups = Vec::with_capacity(dto.group.len()); + for group in &dto.group { + let contribution = group + .contribution + .as_deref() + .map(|local| ContributionId::in_plugin(plugin, local)) + .transpose() + .map_err(|_| SettingsError::Group { + title: group.title.chars().take(MAX_LABEL).collect(), + })?; + let fields = group + .field + .iter() + .map(field) + .collect::, _>>()?; + groups.push(FieldGroup { + title: Cow::Owned(sanitize_display(&group.title, MAX_LABEL)), + contribution, + fields: Cow::Owned(fields), + }); + } + Ok(SettingsSchema { + version: dto.version.unwrap_or(1), + groups: Cow::Owned(groups), + }) +} + +fn field(dto: &FieldDto) -> Result { + let key = dto.key.clone(); + let kind_name = dto + .kind + .ok_or_else(|| SettingsError::NoKind { key: key.clone() })?; + stray(dto, kind_name)?; + let kind = match kind_name { + FieldKindName::Toggle => FieldKind::Toggle { + default: match &dto.default { + None => false, + Some(DefaultDto::Bool(value)) => *value, + Some(_) => return Err(SettingsError::Default { key }), + }, + }, + FieldKindName::Choice => FieldKind::Choice { + options: options(dto), + default: match &dto.default { + Some(DefaultDto::Text(value)) => Cow::Owned(value.clone()), + _ => return Err(SettingsError::Default { key }), + }, + }, + FieldKindName::MultiChoice => FieldKind::MultiChoice { + options: options(dto), + default: match &dto.default { + None => Cow::Owned(Vec::new()), + Some(DefaultDto::List(values)) => { + Cow::Owned(values.iter().cloned().map(Cow::Owned).collect()) + } + Some(_) => return Err(SettingsError::Default { key }), + }, + ordered: dto.ordered.unwrap_or(false), + max: dto.max_chosen.ok_or(SettingsError::Missing { + key, + field: "max_chosen", + })?, + }, + FieldKindName::Number => number(dto)?, + FieldKindName::Text | FieldKindName::Secret => FieldKind::Text { + default: match &dto.default { + None => Cow::Borrowed(""), + Some(DefaultDto::Text(value)) if kind_name == FieldKindName::Text => { + Cow::Owned(value.clone()) + } + Some(_) => return Err(SettingsError::Default { key }), + }, + max_len: dto.max_len.ok_or(SettingsError::Missing { + key, + field: "max_len", + })?, + placeholder: dto + .placeholder + .as_deref() + .map(|text| Cow::Owned(sanitize_display(text, MAX_HELP))), + secret: kind_name == FieldKindName::Secret, + }, + }; + Ok(FieldSpec { + key: Cow::Owned(dto.key.clone()), + label: Cow::Owned(sanitize_display(&dto.label, MAX_LABEL)), + help: dto + .help + .as_deref() + .map(|text| Cow::Owned(sanitize_display(text, MAX_HELP))), + kind, + renamed_from: dto.renamed_from.clone().map(Cow::Owned), + }) +} + +fn number(dto: &FieldDto) -> Result { + let missing = |field| SettingsError::Missing { + key: dto.key.clone(), + field, + }; + let min = dto.min.ok_or_else(|| missing("min"))?; + let default = match &dto.default { + None => min, + Some(DefaultDto::Number(value)) => *value, + Some(_) => { + return Err(SettingsError::Default { + key: dto.key.clone(), + }); + } + }; + Ok(FieldKind::Number { + min, + max: dto.max.ok_or_else(|| missing("max"))?, + step: dto.step.unwrap_or(1), + default, + unit: dto + .unit + .as_deref() + .map(|text| Cow::Owned(sanitize_display(text, 8))), + }) +} + +fn options(dto: &FieldDto) -> Cow<'static, [ChoiceOption]> { + Cow::Owned( + dto.options + .iter() + .map(|option| ChoiceOption { + key: Cow::Owned(option.key.clone()), + label: Cow::Owned(sanitize_display(&option.label, MAX_LABEL)), + help: option + .help + .as_deref() + .map(|text| Cow::Owned(sanitize_display(text, MAX_HELP))), + }) + .collect(), + ) +} + +/// Refuses a key that belongs to another kind, so a typo is an error and not a silent no-op. +fn stray(dto: &FieldDto, kind: FieldKindName) -> Result<(), SettingsError> { + use FieldKindName::{Choice, MultiChoice, Number, Secret, Text}; + let choosing = matches!(kind, Choice | MultiChoice); + let typing = matches!(kind, Text | Secret); + let found = [ + ("options", !dto.options.is_empty() && !choosing), + ("ordered", dto.ordered.is_some() && kind != MultiChoice), + ( + "max_chosen", + dto.max_chosen.is_some() && kind != MultiChoice, + ), + ("min", dto.min.is_some() && kind != Number), + ("max", dto.max.is_some() && kind != Number), + ("step", dto.step.is_some() && kind != Number), + ("unit", dto.unit.is_some() && kind != Number), + ("max_len", dto.max_len.is_some() && !typing), + ("placeholder", dto.placeholder.is_some() && !typing), + ]; + match found.into_iter().find(|(_, stray)| *stray) { + Some((field, _)) => Err(SettingsError::Stray { + key: dto.key.clone(), + field, + }), + None => Ok(()), + } +} diff --git a/crates/plugin-api/src/package_format.rs b/crates/plugin-api/src/package_format.rs new file mode 100644 index 00000000..3342dddf --- /dev/null +++ b/crates/plugin-api/src/package_format.rs @@ -0,0 +1,120 @@ +//! The two package formats and how a code package carries its manifest. +//! +//! A declarative package is one TOML file. A code package is one WebAssembly module whose custom +//! section [`MANIFEST_SECTION`] holds the same TOML. The kind is read from the first four bytes, +//! never from a file name. The section walk below is a pure, bounded scan of the section +//! headers: it does not validate the module (the runtime does that after consent), and it never +//! reads past the buffer. + +use thiserror::Error; + +/// The first four bytes of a WebAssembly module. +const MAGIC: [u8; 4] = *b"\0asm"; +/// The only module version this host reads. +const VERSION: [u8; 4] = [1, 0, 0, 0]; +/// A LEB128 `u32` takes at most this many bytes. +const MAX_LEB_BYTES: usize = 5; + +/// The custom section of a code package that holds its manifest TOML. +pub const MANIFEST_SECTION: &str = "nocterm.manifest"; + +/// What the bytes of a package are. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum PackageKind { + /// A declarative package: TOML text. + Manifest, + /// A code package: a WebAssembly module. + Module, +} + +impl PackageKind { + /// The file extension a package of this kind is stored under. + pub const fn extension(self) -> &'static str { + match self { + Self::Manifest => "toml", + Self::Module => "wasm", + } + } +} + +/// The kind of `bytes`: a module if they start with the WebAssembly magic, else a manifest. +pub fn kind(bytes: &[u8]) -> PackageKind { + if bytes.starts_with(&MAGIC) { + PackageKind::Module + } else { + PackageKind::Manifest + } +} + +/// A module whose manifest section cannot be read. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Error)] +pub enum SectionError { + #[error("the file is not a WebAssembly module of version 1")] + Header, + #[error("the module ends inside a section")] + Truncated, + #[error("a section size is not a valid 32-bit LEB128 number")] + Leb, + #[error("the module has no `{MANIFEST_SECTION}` section")] + Missing, + #[error("the module has more than one `{MANIFEST_SECTION}` section")] + Duplicate, +} + +/// The payload of the single `nocterm.manifest` custom section of a module. +/// +/// Walks the section headers only. Each size is checked against the bytes that remain before +/// the section is entered, so a hostile size cannot make the walk read or allocate beyond the +/// buffer. +pub fn manifest_section(bytes: &[u8]) -> Result<&[u8], SectionError> { + let rest = bytes + .strip_prefix(&MAGIC) + .and_then(|rest| rest.strip_prefix(&VERSION)) + .ok_or(SectionError::Header)?; + let mut rest = rest; + let mut found = None; + while let Some((&id, after_id)) = rest.split_first() { + let (size, after_size) = leb_u32(after_id)?; + let size = usize::try_from(size).map_err(|_| SectionError::Truncated)?; + if size > after_size.len() { + return Err(SectionError::Truncated); + } + let (payload, next) = after_size.split_at(size); + rest = next; + if id != 0 { + continue; + } + let (name_len, after_len) = leb_u32(payload)?; + let name_len = usize::try_from(name_len).map_err(|_| SectionError::Truncated)?; + if name_len > after_len.len() { + return Err(SectionError::Truncated); + } + let (name, data) = after_len.split_at(name_len); + if name == MANIFEST_SECTION.as_bytes() && found.replace(data).is_some() { + return Err(SectionError::Duplicate); + } + } + found.ok_or(SectionError::Missing) +} + +/// Reads one LEB128 `u32` and returns it with the bytes after it. +fn leb_u32(bytes: &[u8]) -> Result<(u32, &[u8]), SectionError> { + let mut value = 0u32; + for (index, &byte) in bytes.iter().take(MAX_LEB_BYTES).enumerate() { + let payload = u32::from(byte & 0x7f); + let shift = 7 * index as u32; + // The fifth byte holds bits 28..32 only: anything above them overflows a u32. + if index == MAX_LEB_BYTES - 1 && (byte & 0x80 != 0 || payload > 0x0f) { + return Err(SectionError::Leb); + } + value |= payload << shift; + if byte & 0x80 == 0 { + return Ok((value, &bytes[index + 1..])); + } + } + if bytes.len() < MAX_LEB_BYTES { + Err(SectionError::Truncated) + } else { + Err(SectionError::Leb) + } +} diff --git a/crates/plugin-api/src/ports.rs b/crates/plugin-api/src/ports.rs new file mode 100644 index 00000000..8bec0a6b --- /dev/null +++ b/crates/plugin-api/src/ports.rs @@ -0,0 +1,236 @@ +//! Ports of the user plugin track: where packages come from and how a package's code runs. +//! +//! The api crate only names them. The adapter `nocterm-plugin-packages` implements +//! [`PluginPackages`]; the declarative interpreter in this crate and the code runtime adapter +//! `nocterm-plugin-wasm` implement [`PluginRuntime`], and [`Runtimes`](crate::Runtimes) picks one +//! by the manifest. + +use alloc::{boxed::Box, string::String, sync::Arc, vec::Vec}; +use core::{error::Error as StdError, fmt}; + +use nocterm_plugin_model::PluginId; +use thiserror::Error; + +use crate::{ + ApiVersion, ContentHash, Effects, Event, LocalId, Manifest, ManifestError, PackageKind, + Runtime, SectionError, Snapshot, View, ViewError, +}; + +/// The largest declarative package (a TOML manifest), in bytes. +pub const MAX_PACKAGE_BYTES: u64 = 256 * 1024; +/// The largest code package (a WebAssembly module), in bytes. +pub const MAX_MODULE_BYTES: u64 = 2 * 1024 * 1024; + +/// A package read from its source: the exact bytes, their hash and the checked manifest. +#[derive(Clone, Debug, PartialEq)] +pub struct Package { + hash: ContentHash, + manifest: Manifest, + bytes: Vec, +} + +impl Package { + /// Joins what the adapter read. `hash` must be the SHA-256 of `bytes` and `manifest` must + /// have been validated from those same bytes: nothing here checks either, so only a + /// package adapter, which hashed and parsed one buffer, builds a `Package`. + pub fn new(hash: ContentHash, manifest: Manifest, bytes: Vec) -> Self { + Self { + hash, + manifest, + bytes, + } + } + + pub fn hash(&self) -> &ContentHash { + &self.hash + } + + pub fn manifest(&self) -> &Manifest { + &self.manifest + } + + /// The exact bytes that were hashed and parsed. + pub fn bytes(&self) -> &[u8] { + &self.bytes + } +} + +/// The error an adapter hit, kept so the chain of causes survives the port. +/// +/// It is equal only to itself, because the adapter's error types are not comparable. +#[derive(Clone, Debug)] +pub struct Cause(Arc); + +impl Cause { + pub fn new(error: impl StdError + Send + Sync + 'static) -> Self { + Self(Arc::new(error)) + } +} + +impl PartialEq for Cause { + fn eq(&self, other: &Self) -> bool { + Arc::ptr_eq(&self.0, &other.0) + } +} + +impl Eq for Cause {} + +impl fmt::Display for Cause { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + fmt::Display::fmt(&self.0, f) + } +} + +impl StdError for Cause { + fn source(&self) -> Option<&(dyn StdError + 'static)> { + self.0.source() + } +} + +/// Why a package could not be read, stored or removed. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum PackageError { + #[error("the package file is missing")] + NotFound, + #[error("the package is not a regular file")] + NotRegular, + #[error( + "the package is larger than its limit ({MAX_PACKAGE_BYTES} bytes of TOML, {MAX_MODULE_BYTES} bytes of code)" + )] + TooLarge, + #[error("the package is not valid UTF-8 text")] + Encoding, + #[error("the package is not valid TOML: {reason}")] + Syntax { reason: String }, + #[error("the module does not carry a readable manifest: {0}")] + Section(#[from] SectionError), + #[error("the manifest says `{declared:?}` but the file is {kind:?}")] + RuntimeMismatch { + declared: Runtime, + kind: PackageKind, + }, + #[error("the package is refused: {0}")] + Manifest(#[from] ManifestError), + #[error("a plugin with this id is already installed")] + Duplicate(PluginId), + #[error("the stored package no longer has the hash that was consented to")] + HashChanged, + #[error("the package could not be accessed: {reason}")] + Unreadable { reason: String }, + #[error("the package could not be stored or removed")] + Unwritable(#[source] Cause), + #[error("`{0}` is not the id of a user plugin, so it names no package file")] + NotUserId(PluginId), + #[error( + "the plugin has {max} retained package versions; finish or retry its pending publication first" + )] + PendingVersions { max: usize }, +} + +/// Reads, stores and removes packages. Implemented by an adapter. +pub trait PluginPackages: Send + Sync { + /// Reads a package at `location` without following links, hashes the exact bytes, and + /// parses the manifest. It reads only; nothing is stored. + fn read(&self, location: &str) -> Result; + + /// Stores the exact bytes privately under their hash without removing earlier consented + /// versions. A duplicate id is refused unless `replace` permits an update or staged retry. + fn publish(&self, package: &Package, replace: bool) -> Result<(), PackageError>; + + /// Loads an installed package and checks that its bytes still hash to `expected`. + fn load(&self, plugin: &PluginId, expected: &ContentHash) -> Result; + + /// Removes an installed package. Removing one that is not installed succeeds. + fn remove(&self, plugin: &PluginId) -> Result<(), PackageError>; + + /// Removes superseded package versions after the matching records document is durable. + /// The caller serializes package management until this operation finishes. + fn retain(&self, _plugin: &PluginId, _expected: &ContentHash) -> Result<(), PackageError> { + Ok(()) + } +} + +/// Why a code module cannot be run at all. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum ModuleError { + #[error("the module is not valid WebAssembly or breaks a limit")] + Invalid(#[source] Cause), + #[error("the module imports something; ABI 1 offers no imports")] + Imports, + #[error("the module lacks an export of the ABI or gives it the wrong type")] + Exports, + #[error("the module speaks ABI {found}")] + AbiVersion { found: i32 }, +} + +/// How a code plugin's reply broke the call protocol. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum ProtocolError { + /// A pointer or a length the guest gave does not lie inside its memory. + #[error("a buffer lies outside the guest's memory")] + OutsideMemory, + #[error("the reply is larger than {max} bytes")] + ReplyTooLarge { max: usize }, + #[error("the reply is not a value of the ABI")] + Undecodable(#[source] Cause), + /// A view where effects were asked for, or the other way round. + #[error("the answer is not the kind the call asked for")] + WrongAnswer, + #[error("the reply carries more than {max} log lines")] + TooManyLogLines { max: usize }, +} + +/// Why a runtime could not produce a view or effects. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum RuntimeError { + #[error("the package has no contribution `{0}`")] + UnknownContribution(LocalId), + #[error("the package needs API {needs}, newer than this host")] + Unsupported { needs: ApiVersion }, + #[error("the plugin trapped")] + Trap, + #[error("the plugin passed its deadline")] + Deadline, + #[error("no runtime is composed for `{0:?}` packages")] + NoRuntime(Runtime), + #[error("the module is refused")] + Module(#[from] ModuleError), + #[error("the plugin passed its memory limit")] + Memory, + #[error("the plugin broke the call protocol")] + Malformed(#[from] ProtocolError), + /// The host could not hand the call to the guest because its snapshot is too large. The + /// plugin did nothing wrong, so this is not a fault. + #[error("the call is larger than a guest accepts")] + InputTooLarge, + #[error("too many plugins are running")] + Capacity, + #[error("the view is invalid")] + View(#[from] ViewError), +} + +/// A package ready to be asked for views and event answers. +pub trait PluginInstance: Send + Sync { + /// The view of a contribution. It cannot produce effects, by signature. + fn render(&self, local: &LocalId, snapshot: &Snapshot) -> Result; + + /// The answer to an event: at most eight effects. + fn on_event( + &self, + local: &LocalId, + event: Event, + snapshot: &Snapshot, + ) -> Result; +} + +/// Sends a plugin's requests. An adapter implements it over an HTTP client: no cookies, no +/// redirects followed, a bounded body, a timeout, and the address checked again against the +/// origin. The host calls it on a background thread. +pub trait PluginNet: Send + Sync { + fn send(&self, call: &crate::capability::net::NetCall) -> crate::capability::net::NetAnswer; +} + +/// Turns a package into an instance: the declarative interpreter or a code runtime. +pub trait PluginRuntime: Send + Sync { + fn instantiate(&self, package: &Package) -> Result, RuntimeError>; +} diff --git a/crates/plugin-api/src/runtimes.rs b/crates/plugin-api/src/runtimes.rs new file mode 100644 index 00000000..404f2e63 --- /dev/null +++ b/crates/plugin-api/src/runtimes.rs @@ -0,0 +1,50 @@ +//! Picks the runtime of a package by what its manifest declares. + +use alloc::{boxed::Box, sync::Arc}; + +use crate::{DeclarativeRuntime, Package, PluginInstance, PluginRuntime, Runtime, RuntimeError}; + +/// The runtimes a host composes: the declarative interpreter, always, and a code runtime when +/// the application supplies one. A package whose runtime is not composed is refused with +/// [`RuntimeError::NoRuntime`], so a build without a code runtime never runs a module. +#[derive(Clone)] +pub struct Runtimes { + declarative: DeclarativeRuntime, + code: Option>, +} + +impl Runtimes { + /// Declarative packages only. + pub fn new() -> Self { + Self { + declarative: DeclarativeRuntime, + code: None, + } + } + + /// Adds the runtime of `Runtime::Wasm` packages. + #[must_use] + pub fn with_code(mut self, code: Arc) -> Self { + self.code = Some(code); + self + } +} + +impl Default for Runtimes { + fn default() -> Self { + Self::new() + } +} + +impl PluginRuntime for Runtimes { + fn instantiate(&self, package: &Package) -> Result, RuntimeError> { + match package.manifest().runtime() { + Runtime::Declarative => self.declarative.instantiate(package), + Runtime::Wasm => self + .code + .as_ref() + .ok_or(RuntimeError::NoRuntime(Runtime::Wasm))? + .instantiate(package), + } + } +} diff --git a/crates/plugin-api/src/sanitize.rs b/crates/plugin-api/src/sanitize.rs new file mode 100644 index 00000000..37f19b16 --- /dev/null +++ b/crates/plugin-api/src/sanitize.rs @@ -0,0 +1,27 @@ +//! Sanitizing of every string a guest supplies. + +use alloc::string::String; +use nocterm_plugin_model::sanitize_text; + +/// `text` as one inert line of at most `cap` characters. +/// +/// Strips C0 and C1 controls, bidirectional controls (U+202A to U+202E, U+2066 to U+2069), +/// zero-width characters, U+2028, U+2029 and U+FEFF, and keeps at most two combining marks per +/// base character. It applies to every guest string: view text, labels, help and option labels. +/// There is no normalization, because it would add a dependency and names are ASCII. +pub fn sanitize_display(text: &str, cap: usize) -> String { + sanitize_text(text).chars().take(cap).collect() +} + +/// `text` as inert lines of at most `cap` characters in all: each line as +/// [`sanitize_display`] keeps it, tabs as four spaces, and `\n` between the lines. +pub fn sanitize_code(text: &str, cap: usize) -> String { + let mut out = String::with_capacity(text.len().min(cap)); + for (index, line) in text.split('\n').enumerate() { + if index > 0 { + out.push('\n'); + } + out.push_str(&sanitize_text(&line.replace('\t', " "))); + } + out.chars().take(cap).collect() +} diff --git a/crates/plugin-api/src/snapshot.rs b/crates/plugin-api/src/snapshot.rs new file mode 100644 index 00000000..777b32bb --- /dev/null +++ b/crates/plugin-api/src/snapshot.rs @@ -0,0 +1,97 @@ +//! What the host tells a plugin each time it asks for a view or an event answer. + +use alloc::{collections::BTreeMap, string::String, vec::Vec}; + +use nocterm_plugin_model::SettingValues; + +use crate::{ + ContributionContext, EffectOutcome, HostError, + capability::{ + Capability, Fields, Value, clock::ClockNow, monitor::Usage, notifications::OwnNotices, + transfers::Counts, workspace::WorkspaceState, + }, +}; + +/// The data one observation carries. +#[derive(Clone, Debug, PartialEq)] +pub enum Observed { + Clock(ClockNow), + Workspace(WorkspaceState), + Usage(Usage), + Counts(Counts), + Notices(OwnNotices), +} + +impl Observed { + /// The id of the capability that produced it. + pub fn capability(&self) -> &'static str { + match self { + Self::Clock(_) => crate::capability::clock::Now::ID, + Self::Workspace(_) => crate::capability::workspace::Kind::ID, + Self::Usage(_) => crate::capability::monitor::UsageNow::ID, + Self::Counts(_) => crate::capability::transfers::CountsNow::ID, + Self::Notices(_) => crate::capability::notifications::Own::ID, + } + } + + /// A field of the data, for a template. + pub fn field(&self, name: &str) -> Option { + match self { + Self::Clock(data) => data.field(name), + Self::Workspace(data) => data.field(name), + Self::Usage(data) => data.field(name), + Self::Counts(data) => data.field(name), + Self::Notices(_) => None, + } + } + + /// The whole data as one value, when it is a single instant. + pub fn whole(&self) -> Option { + match self { + Self::Clock(clock) => Some(Value::Time { + unix_minutes: clock.unix_minutes, + utc_offset_seconds: clock.utc_offset_seconds, + }), + _ => None, + } + } +} + +/// The input of `render` and `on_event`. +/// +/// It carries no window id. Observations the plugin did not declare are absent; an optional +/// capability the user did not grant is `Err(HostError::Unavailable)`. +#[derive(Clone, Debug, PartialEq)] +pub struct Snapshot { + /// Granted observations by capability id. + pub observations: BTreeMap>, + /// The plugin's setting values, resolved against its schema. + pub settings: SettingValues, + /// The plugin's own storage, at most 64 KiB. + pub storage: BTreeMap, + pub context: ContributionContext, + /// What happened to the effects of the previous call, by their keys. + pub outcomes: Vec, + /// The plugin's terminal whose bar this call draws, or whose bar the user used + /// (`terminals.bar`); `None` for every other call. + pub terminal: Option, +} + +impl Snapshot { + /// An empty snapshot for `context`. + pub fn new(context: ContributionContext) -> Self { + Self { + observations: BTreeMap::new(), + settings: SettingValues::default(), + storage: BTreeMap::new(), + context, + outcomes: Vec::new(), + terminal: None, + } + } + + /// The observation of a capability, as the template interpreter and a guest read it. + pub fn observation(&self, capability: &str) -> Option<&Result> { + self.observations.get(capability) + } +} diff --git a/crates/plugin-api/src/surface.rs b/crates/plugin-api/src/surface.rs new file mode 100644 index 00000000..6a22bb6b --- /dev/null +++ b/crates/plugin-api/src/surface.rs @@ -0,0 +1,386 @@ +//! The names and limits that make up the API surface, for the generated `api-surface.json`. +//! +//! Everything here is a constant of the contract, listed once. Tests keep the key lists in step +//! with the DTOs they describe (names, which keys are required, and what each holds), so the +//! generated file cannot drift from what a package may write. A removed or renamed name, a +//! changed key type, a key that became required, or a tightened limit, is a breaking change. + +use nocterm_plugin_model::{ + MAX_FIELDS, MAX_GROUPS, MAX_HELP, MAX_KEY, MAX_LABEL, MAX_OPTIONS, MAX_TEXT_LEN, +}; + +use crate::{ + MAX_BADGE, MAX_BUTTON, MAX_CHART_POINTS, MAX_CHART_SERIES, MAX_CODE, MAX_CONTRIBUTIONS, + MAX_DECLARED, MAX_DEPTH, MAX_DESCRIPTION, MAX_EFFECT_KEY, MAX_EFFECTS, MAX_LIST_ITEMS, + MAX_NAME, MAX_NODE_KEY, MAX_NODES, MAX_PACKAGE_BYTES, MAX_REASON, MAX_TEXT, MIN_REFRESH_MS, + capability::{ + files::{ + FILE_TIMEOUT_MS, MAX_FILE_BYTES, MAX_FILE_ENTRIES, MAX_FILE_NAME, MAX_FILE_PATH, + MAX_FILE_REASON, MAX_FILE_REQUESTS, + }, + net::{MAX_BODY, MAX_BYTES_RESPONSE, MAX_IN_FLIGHT, MAX_PATH, MAX_RESPONSE, TIMEOUT_MS}, + notifications::{MAX_LIVE_NOTICES, MAX_NOTICE_TEXT}, + storage::{MAX_KEY as MAX_STORAGE_KEY, MAX_TOTAL, MAX_VALUE}, + terminals::{MAX_LINE, MAX_TERMINAL_KEY, MAX_TERMINAL_TITLE, MAX_TERMINALS, MAX_WRITE}, + timers::{MAX_TIMER_MS, MAX_TIMERS, MIN_TIMER_MS}, + }, + template::{MAX_PLACEHOLDERS, MAX_ROUND, MAX_TEMPLATE_LEN}, +}; + +/// What a manifest key holds, as the schema and the compatibility check see it. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum KeyType { + Text, + /// A whole number the host reads within `min..=max`. The range is part of the contract: a + /// narrower one refuses numbers a published package may hold. + Integer { + min: i64, + max: i64, + }, + Bool, + TextList, + /// A table of text values by name. + TextMap, + Table, + /// An array of tables. + TableList, + /// One text of a named vocabulary: the key of a list in the surface (`zones`, `icons`, ...). + Choice(&'static str), + /// More than one form, for example a refresh request or a setting's default. + Any, +} + +impl KeyType { + /// The name the surface file gives the type. + #[must_use] + pub const fn name(self) -> &'static str { + match self { + Self::Text => "text", + Self::Integer { .. } => "integer", + Self::Bool => "bool", + Self::TextList => "text-list", + Self::TextMap => "text-map", + Self::Table => "table", + Self::TableList => "table-list", + Self::Choice(_) => "choice", + Self::Any => "any", + } + } +} + +/// One key of a manifest table. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub struct Key { + /// As an author writes it (snake_case). + pub name: &'static str, + pub ty: KeyType, + /// A package without it is refused. + pub required: bool, +} + +const fn optional(name: &'static str, ty: KeyType) -> Key { + Key { + name, + ty, + required: false, + } +} + +const fn required(name: &'static str, ty: KeyType) -> Key { + Key { + name, + ty, + required: true, + } +} + +use KeyType::{Any, Bool, Choice, Table, TableList, Text, TextList, TextMap}; + +const fn integer(min: i64, max: i64) -> KeyType { + KeyType::Integer { min, max } +} + +/// A number the host keeps in a `u8`. +const U8: KeyType = integer(0, u8::MAX as i64); +/// A number the host keeps in a `u16`. +const U16: KeyType = integer(0, u16::MAX as i64); +/// A number the host keeps in an `i32`; it may be negative. +const I32: KeyType = integer(i32::MIN as i64, i32::MAX as i64); +/// A step of a number setting: the host refuses zero and below. +const STEP: KeyType = integer(1, i32::MAX as i64); + +/// The keys of each table of a package, by table path. +/// +/// A lower bound in [`LIMITS`] starts with `min-`; the compatibility check relies on the prefix +/// to tell which way a limit may move. +pub const MANIFEST_KEYS: &[(&str, &[Key])] = &[ + ( + "manifest", + &[ + required("api", Text), + optional("runtime", Choice("runtimes")), + required("publisher", Text), + required("id", Text), + required("name", Text), + optional("description", Text), + optional("capabilities", TextList), + optional("optional_capabilities", TextList), + optional("reasons", TextMap), + optional("contribution", TableList), + optional("settings", Table), + optional("network", Table), + ], + ), + ( + "contribution", + &[ + required("local", Text), + required("kind", Choice("kinds")), + required("title", Text), + optional("icon", Choice("icons")), + optional("zone", Choice("zones")), + optional("order", U16), + optional("edge", Choice("edges")), + optional("shown", Bool), + optional("tabs", Choice("tabs")), + optional("view", Table), + ], + ), + ( + "contribution.view", + &[optional("refresh", Any), required("nodes", TableList)], + ), + ( + "contribution.view.nodes", + &[ + required("kind", Choice("node_kinds")), + optional("parent", U16), + optional("key", Text), + optional("tone", Choice("tones")), + optional("tone_when", Text), + optional("text", Text), + optional("detail", Text), + optional("folded", Bool), + optional("icon", Choice("glyphs")), + optional("value", Text), + optional("each", Text), + ], + ), + ( + "settings", + &[optional("version", U16), optional("group", TableList)], + ), + ( + "settings.group", + &[ + required("title", Text), + optional("contribution", Text), + optional("field", TableList), + ], + ), + ( + "settings.group.field", + &[ + required("key", Text), + required("label", Text), + optional("help", Text), + required("kind", Choice("settings_kinds")), + optional("renamed_from", Text), + optional("default", Any), + optional("options", TableList), + optional("ordered", Bool), + optional("max_chosen", U8), + optional("min", I32), + optional("max", I32), + optional("step", STEP), + optional("unit", Text), + optional("max_len", U16), + optional("placeholder", Text), + ], + ), + ( + "settings.group.field.options", + &[ + required("key", Text), + required("label", Text), + optional("help", Text), + ], + ), + ( + "network", + &[ + required("origin_setting", Text), + optional("credential_setting", Text), + optional("credential_header", Text), + optional("credential_prefix", Text), + ], + ), +]; + +/// The request fields of each effect capability, with the type each holds and whether a guest +/// may leave it out. A field added later has to be optional, and the compatibility check refuses +/// one that is not. +pub const EFFECT_REQUESTS: &[(&str, &[Key])] = &[ + ( + "notifications.post", + &[ + required("tone", Choice("notice_tones")), + required("text", Text), + optional("notice_key", Text), + ], + ), + ( + "panels.set-open", + &[required("local", Text), required("open", Bool)], + ), + ( + "panels.set-badge", + &[required("local", Text), optional("badge", U16)], + ), + ( + "storage.set", + &[required("key", Text), required("value", Text)], + ), + ("storage.remove", &[required("key", Text)]), + ( + "net.request", + &[ + required("method", Choice("http_methods")), + required("path", Text), + optional("body", Text), + optional("bytes", Bool), + ], + ), + ( + "timers.set", + &[required("after_ms", integer(0, u32::MAX as i64))], + ), + ( + "terminals.open", + &[ + required("local", Text), + required("terminal", Text), + required("title", Text), + optional("files", Bool), + ], + ), + ( + "terminals.write", + &[required("terminal", Text), required("text", Text)], + ), + ( + "terminals.files", + &[ + required("terminal", Text), + required( + "request", + KeyType::Integer { + min: 0, + max: u32::MAX as i64, + }, + ), + required("answer", Any), + ], + ), + ( + "terminals.bar", + &[ + required("terminal", Text), + optional("edge", Choice("edges")), + ], + ), +]; + +/// The kinds of setting a package may declare. +pub const SETTINGS_KINDS: [&str; 6] = [ + "toggle", + "choice", + "multi-choice", + "number", + "text", + "secret", +]; + +/// The runtimes a package may name. +pub const RUNTIMES: [&str; 2] = ["declarative", "wasm"]; + +/// The template filters. +pub const FILTERS: [&str; 4] = ["round(n)", "percent", "hh-mm", "threshold(low, high)"]; + +/// The events a plugin receives. +pub const EVENTS: [&str; 9] = [ + "activated", + "toggled", + "visibility-changed", + "settings-changed", + "response", + "timer", + "terminal-line", + "terminal-closed", + "file-request", +]; + +/// The errors the host reports to a plugin. +pub const HOST_ERRORS: [&str; 4] = ["not-granted", "denied", "rate-limited", "unavailable"]; + +/// The refresh requests a view may make. +pub const REFRESHES: [&str; 3] = ["next-second", "next-minute", "after-ms"]; + +/// Every limit of the contract, by name. +pub const LIMITS: &[(&str, u64)] = &[ + ("badge-chars", MAX_BADGE as u64), + ("button-chars", MAX_BUTTON as u64), + ("capabilities-per-package", MAX_DECLARED as u64), + ("chart-points", MAX_CHART_POINTS as u64), + ("chart-series", MAX_CHART_SERIES as u64), + ("code-chars", MAX_CODE as u64), + ("contributions-per-package", MAX_CONTRIBUTIONS as u64), + ("description-chars", MAX_DESCRIPTION as u64), + ("effect-key-chars", MAX_EFFECT_KEY as u64), + ("effects-per-call", MAX_EFFECTS as u64), + ("file-bytes", MAX_FILE_BYTES as u64), + ("file-entries", MAX_FILE_ENTRIES as u64), + ("file-name-bytes", MAX_FILE_NAME as u64), + ("file-path-bytes", MAX_FILE_PATH as u64), + ("file-reason-chars", MAX_FILE_REASON as u64), + ("file-requests-waiting", MAX_FILE_REQUESTS as u64), + ("file-timeout-ms", FILE_TIMEOUT_MS as u64), + ("list-items", MAX_LIST_ITEMS as u64), + ("live-notices", MAX_LIVE_NOTICES as u64), + ("min-refresh-ms", MIN_REFRESH_MS as u64), + ("min-timer-ms", MIN_TIMER_MS as u64), + ("name-chars", MAX_NAME as u64), + ("node-key-chars", MAX_NODE_KEY as u64), + ("nodes-per-view", MAX_NODES as u64), + ("notice-chars", MAX_NOTICE_TEXT as u64), + ("package-bytes", MAX_PACKAGE_BYTES), + ("placeholders-per-view", MAX_PLACEHOLDERS as u64), + ("reason-chars", MAX_REASON as u64), + ("request-body-bytes", MAX_BODY as u64), + ("request-path-bytes", MAX_PATH as u64), + ("request-timeout-ms", TIMEOUT_MS as u64), + ("requests-in-flight", MAX_IN_FLIGHT as u64), + ("response-body-bytes", MAX_RESPONSE as u64), + ("response-bytes-mode-bytes", MAX_BYTES_RESPONSE as u64), + ("round-decimals", MAX_ROUND as u64), + ("setting-fields", MAX_FIELDS as u64), + ("setting-groups", MAX_GROUPS as u64), + ("setting-help-chars", MAX_HELP as u64), + ("setting-key-chars", MAX_KEY as u64), + ("setting-label-chars", MAX_LABEL as u64), + ("setting-options", MAX_OPTIONS as u64), + ("setting-text-chars", MAX_TEXT_LEN as u64), + ("storage-key-chars", MAX_STORAGE_KEY as u64), + ("storage-total-bytes", MAX_TOTAL as u64), + ("storage-value-bytes", MAX_VALUE as u64), + ("template-chars", MAX_TEMPLATE_LEN as u64), + ("terminal-key-chars", MAX_TERMINAL_KEY as u64), + ("terminal-line-chars", MAX_LINE as u64), + ("terminal-title-chars", MAX_TERMINAL_TITLE as u64), + ("terminal-write-bytes", MAX_WRITE as u64), + ("terminals-open", MAX_TERMINALS as u64), + ("text-chars", MAX_TEXT as u64), + ("timer-max-ms", MAX_TIMER_MS as u64), + ("timers-waiting", MAX_TIMERS as u64), + ("view-depth", MAX_DEPTH as u64), +]; diff --git a/crates/plugin-api/src/template.rs b/crates/plugin-api/src/template.rs new file mode 100644 index 00000000..5e4a741b --- /dev/null +++ b/crates/plugin-api/src/template.rs @@ -0,0 +1,243 @@ +//! The declarative template language. +//! +//! A template is literal text with `{placeholder}` holes. A placeholder is a source and filters: +//! `{clock.now|hh-mm}`, `{monitor.usage.cpu_percent|round(1)}`, `{settings.city}`. There is no +//! concatenation, repetition, regex or function. Values are inserted as literal text and are +//! never parsed again. `{{` and `}}` write a brace. +//! +//! Every placeholder is resolved against the declared capabilities, their fields and the +//! settings keys when the package is read, so a mistake is a manifest error shown before consent. + +use alloc::{string::String, string::ToString, vec::Vec}; + +use thiserror::Error; + +mod check; +mod eval; +mod view; + +pub use check::{Output, Scope}; +pub use eval::Context; +pub use view::{NodeDto, NodeKindName, RenderError, ViewDto, ViewTemplate, ViewTemplateError}; + +use crate::{CapabilityEntry, Family, capability::capability}; + +/// Longest template, in characters. +pub const MAX_TEMPLATE_LEN: usize = 512; +/// Most placeholders in one view. +pub const MAX_PLACEHOLDERS: usize = 64; +/// Most decimals `round` keeps. +pub const MAX_ROUND: u8 = 2; + +/// Where a placeholder reads from. +#[derive(Clone, Debug, PartialEq)] +pub enum Source { + /// A field of an observed capability, or the whole `clock.now` instant when `field` is `None`. + Capability { + capability: &'static CapabilityEntry, + field: Option, + }, + /// A setting of the plugin. + Setting(String), + /// A field of the current item of the nearest enclosing list with `each`. + Item(String), +} + +/// A step that turns a value into text, or into a tone. +#[derive(Clone, Copy, Debug, PartialEq)] +pub enum Filter { + /// A number with this many decimals. + Round(u8), + /// A number rounded to a whole percent, with a percent sign. + Percent, + /// A clock instant as local `HH:MM`. + HhMm, + /// A number as a tone: below `low` is `success`, from `low` below `high` is `default`, and + /// `high` or more is `caution`. + Threshold { low: f64, high: f64 }, +} + +/// One hole of a template. +#[derive(Clone, Debug, PartialEq)] +pub struct Placeholder { + pub source: Source, + pub filters: Vec, +} + +#[derive(Clone, Debug, PartialEq)] +enum Segment { + Literal(String), + Hole(Placeholder), +} + +/// Parsed template text. +#[derive(Clone, Debug, PartialEq)] +pub struct Template { + segments: Vec, +} + +/// A template, expression or filter the language refuses. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum TemplateError { + #[error("a template has at most {MAX_TEMPLATE_LEN} characters")] + TooLong, + #[error("a `{{` is not closed, or a `}}` has no opening brace")] + Braces, + #[error("`{0}` is not a placeholder like `capability.field`, `settings.key` or `item.field`")] + Source(String), + #[error("`{0}` is not an observing capability of this API")] + UnknownCapability(String), + #[error("capability `{0}` is not declared by the package")] + Undeclared(String), + #[error("`{0}` has no such field")] + UnknownField(String), + #[error("`{0}` needs a field")] + NeedsField(String), + #[error("there is no setting `{0}`, or it holds several values")] + UnknownSetting(String), + #[error("`item.{0}` is outside a list with `each`, or the item has no such field")] + Item(String), + #[error("`{0}` is not a filter: use round(n), percent, hh-mm or threshold(low, high)")] + Filter(String), + #[error("round keeps at most {MAX_ROUND} decimals")] + Round, + #[error("filter `{filter}` does not take {input}")] + FilterInput { + filter: &'static str, + input: &'static str, + }, + #[error("this place takes {expected}, but the expression gives {found}")] + Output { + expected: &'static str, + found: &'static str, + }, +} + +impl Template { + pub fn parse(text: &str) -> Result { + if text.chars().count() > MAX_TEMPLATE_LEN { + return Err(TemplateError::TooLong); + } + let mut segments = Vec::new(); + let mut literal = String::new(); + let mut chars = text.chars().peekable(); + while let Some(c) = chars.next() { + match (c, chars.peek()) { + ('{', Some('{')) | ('}', Some('}')) => { + literal.push(c); + chars.next(); + } + ('{', _) => { + let mut expression = String::new(); + loop { + match chars.next() { + Some('}') => break, + Some('{') | None => return Err(TemplateError::Braces), + Some(c) => expression.push(c), + } + } + if !literal.is_empty() { + segments.push(Segment::Literal(core::mem::take(&mut literal))); + } + segments.push(Segment::Hole(Placeholder::parse(&expression)?)); + } + ('}', _) => return Err(TemplateError::Braces), + _ => literal.push(c), + } + } + if !literal.is_empty() { + segments.push(Segment::Literal(literal)); + } + Ok(Self { segments }) + } + + /// The placeholders, in order. + pub fn placeholders(&self) -> impl Iterator { + self.segments.iter().filter_map(|segment| match segment { + Segment::Hole(hole) => Some(hole), + Segment::Literal(_) => None, + }) + } +} + +impl Placeholder { + /// Parses `source|filter|filter`, without braces. + pub fn parse(expression: &str) -> Result { + let mut parts = expression.split('|').map(str::trim); + let source = Source::parse(parts.next().unwrap_or_default())?; + let filters = parts.map(Filter::parse).collect::>()?; + Ok(Self { source, filters }) + } +} + +impl Source { + fn parse(text: &str) -> Result { + let parts: Vec<&str> = text.split('.').collect(); + if parts.iter().any(|part| part.is_empty()) { + return Err(TemplateError::Source(text.to_string())); + } + match parts.as_slice() { + ["settings", key] => Ok(Self::Setting((*key).to_string())), + ["item", field] => Ok(Self::Item((*field).to_string())), + [domain, name] => Self::capability(domain, name, None), + [domain, name, field] => Self::capability(domain, name, Some(field)), + _ => Err(TemplateError::Source(text.to_string())), + } + } + + fn capability(domain: &str, name: &str, field: Option<&str>) -> Result { + let id = alloc::format!("{domain}.{name}"); + let entry = capability(&id) + .filter(|entry| entry.family == Family::Observe) + .ok_or(TemplateError::UnknownCapability(id))?; + Ok(Self::Capability { + capability: entry, + field: field.map(ToString::to_string), + }) + } +} + +impl Filter { + fn parse(text: &str) -> Result { + let bad = || TemplateError::Filter(text.to_string()); + let (name, arguments) = match text.split_once('(') { + Some((name, rest)) => (name.trim(), Some(rest.strip_suffix(')').ok_or_else(bad)?)), + None => (text, None), + }; + let numbers = |arguments: &str| -> Result, TemplateError> { + arguments + .split(',') + .map(|number| number.trim().parse::().map_err(|_| bad())) + .collect() + }; + match (name, arguments) { + ("percent", None) => Ok(Self::Percent), + ("hh-mm", None) => Ok(Self::HhMm), + ("round", Some(arguments)) => match numbers(arguments)?.as_slice() { + &[digits] if digits >= 0.0 && digits == (digits as u8) as f64 => { + if digits > f64::from(MAX_ROUND) { + return Err(TemplateError::Round); + } + Ok(Self::Round(digits as u8)) + } + _ => Err(bad()), + }, + ("threshold", Some(arguments)) => match numbers(arguments)?.as_slice() { + &[low, high] if low.is_finite() && high.is_finite() && low <= high => { + Ok(Self::Threshold { low, high }) + } + _ => Err(bad()), + }, + _ => Err(bad()), + } + } + + pub(crate) const fn name(self) -> &'static str { + match self { + Self::Round(_) => "round", + Self::Percent => "percent", + Self::HhMm => "hh-mm", + Self::Threshold { .. } => "threshold", + } + } +} diff --git a/crates/plugin-api/src/template/check.rs b/crates/plugin-api/src/template/check.rs new file mode 100644 index 00000000..c34bfac8 --- /dev/null +++ b/crates/plugin-api/src/template/check.rs @@ -0,0 +1,171 @@ +//! Install-time resolution: every placeholder against the declared capabilities, their fields +//! and the plugin's settings. + +use alloc::string::ToString; + +use nocterm_plugin_model::{FieldKind, SettingsSchema}; + +use super::{Filter, Placeholder, Source, Template, TemplateError}; +use crate::capability::{ + Capability, Fields, ValueType, clock::Now, fields_of, notifications::OwnNotice, +}; + +/// What a template may name. +#[derive(Clone, Copy, Debug)] +pub struct Scope<'a> { + /// The capability ids the package declares, required or optional. + pub declared: &'a [&'a str], + pub settings: &'a SettingsSchema, + /// Whether `item.` names are allowed here, because an enclosing list has `each`. + pub item: bool, +} + +/// What an expression gives. +#[derive(Clone, Copy, Debug, PartialEq, Eq)] +pub enum Output { + Text, + Number, + Bool, + Tone, +} + +impl Output { + const fn name(self) -> &'static str { + match self { + Self::Text => "text", + Self::Number => "a number", + Self::Bool => "a switch value", + Self::Tone => "a tone", + } + } +} + +#[derive(Clone, Copy)] +enum Ty { + Value(ValueType), + Tone, +} + +const fn describe(ty: ValueType) -> &'static str { + match ty { + ValueType::Number => "a number", + ValueType::Text => "text", + ValueType::Bool => "a switch value", + ValueType::Time => "a clock instant", + } +} + +impl Template { + /// Checks that every placeholder resolves and gives something displayable. + pub fn check(&self, scope: &Scope<'_>) -> Result<(), TemplateError> { + for hole in self.placeholders() { + match hole.output(scope)? { + Output::Tone => { + return Err(TemplateError::Output { + expected: "text, a number or a switch value", + found: "a tone", + }); + } + Output::Text | Output::Number | Output::Bool => {} + } + } + Ok(()) + } +} + +impl Placeholder { + /// What the expression gives after its filters, if every name resolves. + pub fn output(&self, scope: &Scope<'_>) -> Result { + let mut ty = Ty::Value(source_type(&self.source, scope)?); + for filter in &self.filters { + ty = apply(*filter, ty)?; + } + match ty { + Ty::Tone => Ok(Output::Tone), + Ty::Value(ValueType::Number) => Ok(Output::Number), + Ty::Value(ValueType::Bool) => Ok(Output::Bool), + Ty::Value(ValueType::Text) => Ok(Output::Text), + Ty::Value(ValueType::Time) => Err(TemplateError::Output { + expected: "text, a number or a switch value", + found: describe(ValueType::Time), + }), + } + } + + /// Checks that the expression gives `wanted`, with no filter when `wanted` is a number or a + /// switch value. + pub fn expect(&self, scope: &Scope<'_>, wanted: Output) -> Result<(), TemplateError> { + let found = self.output(scope)?; + let plain = !matches!(wanted, Output::Number | Output::Bool) || self.filters.is_empty(); + if found == wanted && plain { + Ok(()) + } else { + Err(TemplateError::Output { + expected: wanted.name(), + found: found.name(), + }) + } + } +} + +fn source_type(source: &Source, scope: &Scope<'_>) -> Result { + match source { + Source::Capability { capability, field } => { + if !scope.declared.contains(&capability.id) { + return Err(TemplateError::Undeclared(capability.id.to_string())); + } + let Some(field) = field else { + return if capability.id == Now::ID { + Ok(ValueType::Time) + } else { + Err(TemplateError::NeedsField(capability.id.to_string())) + }; + }; + lookup(fields_of(capability.id), field).ok_or_else(|| { + TemplateError::UnknownField(alloc::format!("{}.{field}", capability.id)) + }) + } + Source::Setting(key) => scope + .settings + .field(key) + .and_then(|field| match field.kind { + FieldKind::Toggle { .. } => Some(ValueType::Bool), + // A template never shows a credential: it reads as an unknown setting. + FieldKind::Text { secret: true, .. } => None, + FieldKind::Choice { .. } | FieldKind::Text { .. } => Some(ValueType::Text), + FieldKind::Number { .. } => Some(ValueType::Number), + FieldKind::MultiChoice { .. } => None, + }) + .ok_or_else(|| TemplateError::UnknownSetting(key.clone())), + Source::Item(field) => scope + .item + .then(|| lookup(Some(OwnNotice::FIELDS), field)) + .flatten() + .ok_or_else(|| TemplateError::Item(field.clone())), + } +} + +fn lookup(fields: Option<&[(&str, ValueType)]>, name: &str) -> Option { + fields? + .iter() + .find(|(field, _)| *field == name) + .map(|(_, ty)| *ty) +} + +fn apply(filter: Filter, ty: Ty) -> Result { + let refuse = |input| { + Err(TemplateError::FilterInput { + filter: filter.name(), + input, + }) + }; + match (filter, ty) { + (Filter::Round(_) | Filter::Percent, Ty::Value(ValueType::Number)) => { + Ok(Ty::Value(ValueType::Text)) + } + (Filter::HhMm, Ty::Value(ValueType::Time)) => Ok(Ty::Value(ValueType::Text)), + (Filter::Threshold { .. }, Ty::Value(ValueType::Number)) => Ok(Ty::Tone), + (_, Ty::Tone) => refuse("a tone"), + (_, Ty::Value(value)) => refuse(describe(value)), + } +} diff --git a/crates/plugin-api/src/template/eval.rs b/crates/plugin-api/src/template/eval.rs new file mode 100644 index 00000000..404f433c --- /dev/null +++ b/crates/plugin-api/src/template/eval.rs @@ -0,0 +1,158 @@ +//! Evaluation of a checked template against a snapshot. + +use alloc::{format, string::String, string::ToString}; + +use nocterm_plugin_model::SettingValue; + +use super::{Filter, Placeholder, Segment, Source, Template}; +use crate::{ + Snapshot, Tone, + capability::{Value, notifications::OwnNotice}, +}; + +/// The shown text of a value the host could not supply. +pub(super) const UNAVAILABLE: &str = "n/a"; + +/// What a template reads when it is drawn. +#[derive(Clone, Copy, Debug)] +pub struct Context<'a> { + pub snapshot: &'a Snapshot, + /// The item of the enclosing `each` list that is being drawn. + pub item: Option<&'a OwnNotice>, +} + +/// What an expression gave. +enum Evaluated { + Value(Value), + Text(String), + Tone(Tone), +} + +impl Template { + /// The text with every placeholder replaced; an unavailable value shows as `n/a`. + /// + /// The result is not sanitized or cut: the caller applies `sanitize_display`. + pub fn render(&self, cx: &Context<'_>) -> String { + let mut text = String::new(); + for segment in &self.segments { + match segment { + Segment::Literal(literal) => text.push_str(literal), + Segment::Hole(hole) => match hole.evaluate(cx) { + Some(Evaluated::Value(value)) => text.push_str(&show(&value)), + Some(Evaluated::Text(shown)) => text.push_str(&shown), + Some(Evaluated::Tone(_)) | None => text.push_str(UNAVAILABLE), + }, + } + } + text + } +} + +impl Placeholder { + /// The number a number expression gives, if it is available. + pub fn number(&self, cx: &Context<'_>) -> Option { + match self.evaluate(cx)? { + Evaluated::Value(Value::Number(number)) if number.is_finite() => Some(number), + _ => None, + } + } + + /// The switch value a boolean expression gives, if it is available. + pub fn switch(&self, cx: &Context<'_>) -> Option { + match self.evaluate(cx)? { + Evaluated::Value(Value::Bool(on)) => Some(on), + _ => None, + } + } + + /// The tone a threshold expression gives, if it is available. + pub fn tone(&self, cx: &Context<'_>) -> Option { + match self.evaluate(cx)? { + Evaluated::Tone(tone) => Some(tone), + _ => None, + } + } + + fn evaluate(&self, cx: &Context<'_>) -> Option { + let mut current = Evaluated::Value(self.source.read(cx)?); + for filter in &self.filters { + current = filter.apply(current)?; + } + Some(current) + } +} + +impl Source { + fn read(&self, cx: &Context<'_>) -> Option { + match self { + Self::Capability { capability, field } => { + let observed = cx.snapshot.observation(capability.id)?.as_ref().ok()?; + match field { + Some(field) => observed.field(field), + None => observed.whole(), + } + } + Self::Setting(key) => match cx.snapshot.settings.get(key)? { + SettingValue::Bool(on) => Some(Value::Bool(*on)), + SettingValue::Choice(text) | SettingValue::Text(text) => { + Some(Value::Text(text.clone())) + } + SettingValue::Number(number) => Some(Value::Number(f64::from(*number))), + SettingValue::Choices(_) => None, + }, + Self::Item(field) => crate::capability::Fields::field(cx.item?, field), + } + } +} + +impl Filter { + fn apply(self, input: Evaluated) -> Option { + let Evaluated::Value(value) = input else { + return None; + }; + match (self, value) { + (Self::Round(digits), Value::Number(number)) if number.is_finite() => Some( + Evaluated::Text(format!("{number:.digits$}", digits = usize::from(digits))), + ), + (Self::Percent, Value::Number(number)) if number.is_finite() => { + Some(Evaluated::Text(format!("{number:.0}%"))) + } + ( + Self::HhMm, + Value::Time { + unix_minutes, + utc_offset_seconds, + }, + ) => { + let local = unix_minutes + i64::from(utc_offset_seconds.div_euclid(60)); + let minute = local.rem_euclid(24 * 60); + Some(Evaluated::Text(format!( + "{:02}:{:02}", + minute / 60, + minute % 60 + ))) + } + (Self::Threshold { low, high }, Value::Number(number)) if number.is_finite() => { + Some(Evaluated::Tone(if number < low { + Tone::Success + } else if number < high { + Tone::Default + } else { + Tone::Caution + })) + } + _ => None, + } + } +} + +fn show(value: &Value) -> String { + match value { + Value::Number(number) if number.is_finite() => number.to_string(), + Value::Number(_) => UNAVAILABLE.to_string(), + Value::Text(text) => text.clone(), + Value::Bool(true) => "on".to_string(), + Value::Bool(false) => "off".to_string(), + Value::Time { .. } => UNAVAILABLE.to_string(), + } +} diff --git a/crates/plugin-api/src/template/view.rs b/crates/plugin-api/src/template/view.rs new file mode 100644 index 00000000..ecc8762d --- /dev/null +++ b/crates/plugin-api/src/template/view.rs @@ -0,0 +1,642 @@ +//! Declarative views: the author's node list, checked once, drawn from snapshots. +//! +//! A package lists its nodes flat, each with the index of its parent, so a hostile file cannot +//! nest deeply enough to harm the parser. The list is compiled against the package's +//! declarations when it is read, and each draw expands it into a [`View`]. + +use alloc::{collections::BTreeSet, string::String, vec, vec::Vec}; + +use nocterm_plugin_model::SettingsSchema; +use serde::{Deserialize, Serialize}; +use thiserror::Error; + +use super::{Context, MAX_PLACEHOLDERS, Output, Placeholder, Scope, Template, TemplateError}; +use crate::{ + Glyph, LimitError, MAX_BADGE, MAX_BUTTON, MAX_DEPTH, MAX_LIST_ITEMS, MAX_NODE_KEY, MAX_NODES, + MAX_TEXT, MIN_REFRESH_MS, Node, NodeKind, Refresh, Snapshot, Tone, View, ViewError, + capability::Capability, capability::notifications::Own, error::key_syntax, sanitize_display, +}; + +/// The kinds of node an author writes. +#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum NodeKindName { + Row, + Column, + Text, + Icon, + Meter, + Badge, + Button, + Toggle, + List, + Divider, + Spacer, + /// A titled group: `text` is the title, `detail` an optional count, `folded` how it starts. + Section, + /// A row of a list: `text` is the title, `detail` the muted line, `icon` leads it. + Item, + /// A small button with an `icon`; `text` names its action in a tooltip. Needs a key. + IconButton, + /// A line of small muted `text`. + Caption, + /// A `text` label and its `detail` value on one line. + Field, +} + +/// A node as written in a package. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct NodeDto { + pub kind: Option, + /// The index of the parent in the list, which must come earlier. + #[serde(default)] + pub parent: Option, + #[serde(default)] + pub key: Option, + #[serde(default)] + pub tone: Option, + /// An expression ending in `threshold(low, high)` that sets the tone. + #[serde(default)] + pub tone_when: Option, + /// A template: the text of a text, badge, button or caption, the title of a section or an + /// item, the label of an icon button or a field. + #[serde(default)] + pub text: Option, + /// A template: the count of a section, the detail line of an item, the value of a field. + #[serde(default)] + pub detail: Option, + /// Whether a section starts folded; a section folds only when it has a key. + #[serde(default)] + pub folded: Option, + #[serde(default)] + pub icon: Option, + /// An expression: a number for a meter, a switch value for a toggle. + #[serde(default)] + pub value: Option, + /// For a list: the observing capability whose items its children repeat for. + #[serde(default)] + pub each: Option, +} + +/// A view as written in a package. +#[derive(Clone, Debug, Default, PartialEq, Eq, Serialize, Deserialize)] +#[serde(rename_all = "snake_case", deny_unknown_fields)] +pub struct ViewDto { + #[serde(default)] + pub refresh: Option, + pub nodes: Vec, +} + +/// A view the package declares wrongly. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum ViewTemplateError { + #[error("a view has at most {MAX_NODES} nodes")] + TooManyNodes, + #[error("a view has at most {MAX_PLACEHOLDERS} placeholders")] + TooManyPlaceholders, + #[error("a refresh must wait at least {MIN_REFRESH_MS} ms")] + Refresh, + #[error("node {index} has no `kind`")] + NoKind { index: usize }, + #[error("node {index}: `{field}` does not belong to a {kind} node")] + Field { + index: usize, + field: &'static str, + kind: &'static str, + }, + #[error("node {index} needs `{field}`")] + Missing { index: usize, field: &'static str }, + #[error("node {index} names a parent that does not come before it, or cannot hold children")] + Parent { index: usize }, + #[error("node {index} is nested deeper than {MAX_DEPTH} levels")] + TooDeep { index: usize }, + #[error("node {index}: `each` must name an observing capability that has items")] + Each { index: usize }, + #[error("node {index}: buttons, toggles and keys cannot repeat inside a list with `each`")] + RepeatedKey { index: usize }, + #[error("node {index}: its key is not valid")] + Key { + index: usize, + #[source] + source: LimitError, + }, + #[error("node {index}: its key is already used by an earlier node")] + DuplicateKey { index: usize }, + #[error("node {index}: {source}")] + Template { + index: usize, + #[source] + source: TemplateError, + }, +} + +/// A view that could not be drawn this time. +#[derive(Clone, Debug, PartialEq, Eq, Error)] +pub enum RenderError { + #[error("the view needs more than {MAX_NODES} nodes")] + TooManyNodes, + #[error("the view is invalid")] + Invalid(#[from] ViewError), +} + +#[derive(Clone, Debug, PartialEq)] +enum Content { + Row, + Column, + Text(Template), + Icon(Glyph), + Meter(Placeholder), + Badge(Template), + Button(Template), + Toggle(Placeholder), + List { + each: bool, + }, + Divider, + Spacer, + Section { + title: Template, + count: Option