Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions src-tauri/src/frame_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ use super::settings::SettingsOp;
use super::{
apply_menu_action, chat_label, close_chat, describe_menu, dev_flags, harness, menu, model,
note_happened, open_chat, overlay_label, paced, place_overlays, platform, publish_instances,
push_chat_opening, remember_instances, spawn_live, switch_instance, tray, ChatMsg, ChatReply,
ChatStatus, ChatStatusPush, DirectorRun, Drawn, FrameExtras, InstanceState, MenuChannel,
MenuHold, MenuSignal, Placed, Placement, SpritePlacement, Traced, TrayHandle, CHAT_EVENT,
CHAT_STATUS_EVENT, ENGINE_TICK, FRAME_EVENT, MENU_HOLD_TIMEOUT, SENSE_INTERVAL,
push_chat_opening, push_chat_openings, remember_instances, spawn_live, switch_instance, tray,
ChatMsg, ChatReply, ChatStatus, ChatStatusPush, DirectorRun, Drawn, FrameExtras, InstanceState,
MenuChannel, MenuHold, MenuSignal, Placed, Placement, SpritePlacement, Traced, TrayHandle,
CHAT_EVENT, CHAT_STATUS_EVENT, ENGINE_TICK, FRAME_EVENT, MENU_HOLD_TIMEOUT, SENSE_INTERVAL,
};

/// One overlay's last applied shape: the mask, then x, y, width and height.
Expand Down Expand Up @@ -430,6 +430,7 @@ pub(crate) fn run_frame_loop(
}

let mut settings_ops = false;
let mut reload_chat = false;
while let Ok(op) = ops.try_recv() {
settings_ops = true;
match op {
Expand Down Expand Up @@ -463,7 +464,9 @@ pub(crate) fn run_frame_loop(
&config,
&director,
);
push_chat_opening(&app, &roster, &id);
if let Ok(inspect) = inspect.lock() {
push_chat_opening(&app, &roster, &id, &inspect);
}
}
} else {
eprintln!("settings: no Character named {character}");
Expand Down Expand Up @@ -512,6 +515,7 @@ pub(crate) fn run_frame_loop(
);
}
}
SettingsOp::ReloadChat => reload_chat = true,
}
remember_instances(&roster, &settings, &settings_path);
}
Expand Down Expand Up @@ -657,6 +661,13 @@ pub(crate) fn run_frame_loop(
}
}

if reload_chat {
if let Ok(mut inspect) = inspect.lock() {
inspect.harness = harness::attached().map(|session| session.inspect());
push_chat_openings(&app, &roster, &inspect);
}
}

if menu_closed {
for live in lives.iter_mut() {
live.menu_hold = None;
Expand Down
113 changes: 83 additions & 30 deletions src-tauri/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ const CHAT_EVENT: &str = "chat";
/// different, whether or not anyone has typed.
const CHAT_STATUS_EVENT: &str = "chat-status";

/// The event carrying who an already-open Chat surface belongs to, after a
/// Character switch. Name and Character only — whether anything can answer
/// stays on the `chat_opening` command. An event rather than a second
/// command, because the window is already listening. #375.
/// The event carrying a full opening to an already-open Chat surface.
///
/// Name and Character after a switch (#375), and configured/enabled/login
/// after a Director or Completer-source change, so `attached()` can re-run
/// without a webview reload. An event rather than a second command, because
/// the window is already listening. #473.
const CHAT_OPENING_EVENT: &str = "chat-opening";

/// The event carrying a forwarded `session/request_permission` to every open
Expand Down Expand Up @@ -905,7 +907,7 @@ fn note_happened(happened: &mut Happened, what: Happened) {
}

/// What a Chat surface needs to draw itself before anything is typed.
#[derive(Serialize)]
#[derive(Clone, Serialize)]
struct ChatOpening {
name: String,
character: String,
Expand All @@ -919,18 +921,16 @@ struct ChatOpening {
login: Option<String>,
}

/// Name and Character an already-open Chat surface should paint. Read off the
/// Roster after a switch so this path cannot disagree with `retarget`. #375.
#[derive(Clone, Serialize)]
struct ChatWho {
name: String,
character: String,
}

fn chat_who_from(instance: &roster::Instance) -> ChatWho {
ChatWho {
fn chat_opening_from(instance: &roster::Instance, inspect: &model::DirectorInspect) -> ChatOpening {
ChatOpening {
name: instance.name.clone(),
character: instance.character_name().to_string(),
configured: inspect.configured,
enabled: inspect.enabled,
login: inspect
.harness
.as_ref()
.and_then(|attached| attached.login.clone()),
}
}

Expand Down Expand Up @@ -979,19 +979,25 @@ fn permission_answer(request: String, option: String) {
}
}

/// Push name and Character to an already-open Chat surface, without creating
/// one. Always after switch: a chosen Instance name stays, but the Character
/// line still has to move. Does not carry configured/enabled — those stay on
/// the `chat_opening` command, which reads `DirectorInspect`. #375.
fn push_chat_opening(app: &tauri::AppHandle, roster: &Roster, id: &InstanceId) {
/// Push a full opening to an already-open Chat surface, without creating one.
/// Always after switch: a chosen Instance name stays, but the Character line
/// still has to move. Configured/enabled/login too: the window asked once at
/// start, and a Director or Completer-source change has to re-run `attached()`.
/// #375, #473.
fn push_chat_opening(
app: &tauri::AppHandle,
roster: &Roster,
id: &InstanceId,
inspect: &model::DirectorInspect,
) {
let Some(instance) = roster.get(id) else {
return;
};
let who = chat_who_from(instance);
let opening = chat_opening_from(instance, inspect);
let label = chat_label(id);
let title = who.name.clone();
let title = opening.name.clone();
let handle = app.clone();
let _ = app.emit_to(label.clone(), CHAT_OPENING_EVENT, who);
let _ = app.emit_to(label.clone(), CHAT_OPENING_EVENT, opening);
if let Err(why) = app.run_on_main_thread(move || {
if let Some(window) = handle.get_webview_window(&label) {
if let Err(why) = window.set_title(&title) {
Expand All @@ -1003,6 +1009,13 @@ fn push_chat_opening(app: &tauri::AppHandle, roster: &Roster, id: &InstanceId) {
}
}

fn push_chat_openings(app: &tauri::AppHandle, roster: &Roster, inspect: &model::DirectorInspect) {
let ids: Vec<_> = roster.list().into_iter().map(|(id, _)| id).collect();
for id in ids {
push_chat_opening(app, roster, &id, inspect);
}
}

struct ChatLine {
instance: InstanceId,
text: String,
Expand Down Expand Up @@ -1301,7 +1314,9 @@ fn apply_menu_action(
config,
director,
);
push_chat_opening(app, roster, instance_id);
if let Ok(inspect) = inspect.lock() {
push_chat_opening(app, roster, instance_id, &inspect);
}
if let Ok(mut settings) = settings.lock() {
settings.character = name.clone();
persist_settings(&settings, settings_path);
Expand Down Expand Up @@ -1336,6 +1351,7 @@ fn apply_menu_action(
config.apply_switch(settings.director_enabled);
if let Ok(mut inspect) = inspect.lock() {
inspect.enabled = config.enabled;
push_chat_openings(app, roster, &inspect);
}
persist_settings(&settings, settings_path);
eprintln!(
Expand Down Expand Up @@ -2400,6 +2416,17 @@ mod tests {
assert_eq!(loaded[0].0.name, "Pip");
}

fn stub_inspect() -> model::DirectorInspect {
model::DirectorInspect {
enabled: true,
configured: true,
ambient_wakes: true,
wake_secs: 60,
last_payload: None,
harness: None,
}
}

/// Production change that would fail this: emitting the pre-switch name or
/// Character, or stuffing `character.name` into both fields.
#[test]
Expand All @@ -2412,10 +2439,10 @@ mod tests {
assert!(roster.retarget(&id, &second));
let instance = roster.get(&id).expect("still there");

let who = chat_who_from(instance);
assert_eq!(who.name, "nim", "the payload name is the Instance's");
let opening = chat_opening_from(instance, &stub_inspect());
assert_eq!(opening.name, "nim", "the payload name is the Instance's");
assert_eq!(
who.character, "nim",
opening.character, "nim",
"the payload Character is the Instance's"
);
}
Expand All @@ -2432,9 +2459,35 @@ mod tests {
assert!(roster.retarget(&id, &second));
let instance = roster.get(&id).expect("still there");

let who = chat_who_from(instance);
assert_eq!(who.name, "Pip");
assert_eq!(who.character, "nim");
let opening = chat_opening_from(instance, &stub_inspect());
assert_eq!(opening.name, "Pip");
assert_eq!(opening.character, "nim");
}

/// Production change that would fail this: an opening whose enabled bit
/// still matches the pre-toggle inspect. #473.
#[test]
fn chat_opening_from_inspect_carries_configured_and_enabled() {
let memory = MemoryManifest::new(std::env::temp_dir().join("test-chat-opening-mode.md"));
let mut roster = Roster::new(memory);
let character = stub_character("nim");
let id = roster.spawn(&character, "Pip".to_string(), Point { x: 10.0, y: 20.0 });
let instance = roster.get(&id).expect("still there");
let inspect = model::DirectorInspect {
enabled: false,
configured: true,
ambient_wakes: true,
wake_secs: 60,
last_payload: None,
harness: None,
};

let opening = chat_opening_from(instance, &inspect);
assert_eq!(opening.name, "Pip");
assert_eq!(opening.character, "nim");
assert!(opening.configured);
assert!(!opening.enabled);
assert_eq!(opening.login, None);
}

/// #17: losing a line that is waiting in `happened` would answer a question
Expand Down
97 changes: 97 additions & 0 deletions src-tauri/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,10 @@ pub enum SettingsOp {
ambient_allowed: bool,
configured: bool,
},
/// Director switch or Completer source changed: already-open Chat
/// surfaces must re-run `attached()` from a full opening. Not a second
/// session (ADR-0008). #473.
ReloadChat,
}

/// Whether what a secure field holds is a key somebody typed.
Expand Down Expand Up @@ -482,6 +486,28 @@ fn completer_retargets(settings: &Settings, patch: &SettingsPatch) -> bool {
.is_some_and(|secs| secs != &settings.director_wake_secs)
}

/// Whether an already-open Chat surface must hear a new opening.
///
/// Director on/off never retargets, and Completer source is a next-launch
/// restart (ADR-0008, #436). Both still change what `chat_opening` would say,
/// and the window only asked once. #473.
fn chat_surface_reloads(settings: &Settings, patch: &SettingsPatch) -> bool {
patch
.director_enabled
.is_some_and(|on| on != settings.director_enabled)
|| harness_source_changed(settings, patch)
}

fn harness_source_changed(settings: &Settings, patch: &SettingsPatch) -> bool {
// Presence of the row is not a change: both windows commit on blur.
if patch.harness.is_none() && patch.harness_command.is_none() {
return false;
}
let mut next = settings.clone();
next.apply(patch.clone());
next.harness_source() != settings.harness_source()
}

/// Which of the Director tab's fields hold an edit.
///
/// A redraw asks this so it can leave a staged field alone and still take
Expand Down Expand Up @@ -691,6 +717,7 @@ impl SettingsSession {
let prompt_sr = patch.use_screen_recording == Some(true);
let mut settings = self.settings.lock().map_err(|error| error.to_string())?;
let retarget = completer_retargets(&settings, &patch);
let reload_chat = chat_surface_reloads(&settings, &patch);
// Seeded before `retarget_payload`, which rebuilds the Endpoint from
// the live timeout and reply cap.
apply_and_seed(&mut settings, patch);
Expand All @@ -717,6 +744,9 @@ impl SettingsSession {
Err(why) => eprintln!("director: secret store: {why}"),
}
}
if reload_chat {
let _ = self.ops.send(SettingsOp::ReloadChat);
}
if let Some(spec) = rebind {
(self.on_rebind)(&self.app, &spec);
}
Expand Down Expand Up @@ -3044,6 +3074,73 @@ mod tests {
assert!(!completer_retargets(&settings, &patch));
}

/// Production change that would fail this: a Director-off patch that does
/// not tell an already-open Chat surface. Payload is `chat_opening_from`
/// with inspect after `apply_switch`. #473.
#[test]
fn a_director_off_patch_tells_an_open_chat_surface_it_is_disabled() {
let settings = Settings::default();
assert!(settings.director_enabled);
let patch = SettingsPatch {
director_enabled: Some(false),
..SettingsPatch::default()
};
assert!(
chat_surface_reloads(&settings, &patch),
"an already-open surface must hear the new opening"
);
}

/// Production change that would fail this: Director on again that does
/// not tell an already-open Chat surface. #473.
#[test]
fn a_director_on_patch_readies_the_composer_when_configured() {
let settings = Settings {
director_enabled: false,
..Settings::default()
};
let patch = SettingsPatch {
director_enabled: Some(true),
..SettingsPatch::default()
};
assert!(chat_surface_reloads(&settings, &patch));
}

#[test]
fn an_unchanged_director_switch_does_not_reload_chat() {
let settings = Settings::default();
let patch = SettingsPatch {
director_enabled: Some(true),
..SettingsPatch::default()
};
assert!(!chat_surface_reloads(&settings, &patch));
}

#[test]
fn a_sound_patch_does_not_reload_chat() {
let patch = SettingsPatch {
sound: Some(false),
..SettingsPatch::default()
};
assert!(!chat_surface_reloads(&Settings::default(), &patch));
}

/// Completer source is not a Retarget; Chat still has to hear a new
/// opening, from inspect as it stands until the next launch. #473, ADR-0008.
#[test]
fn a_harness_source_patch_tells_an_open_chat_surface_the_inspect_mode() {
let settings = Settings {
harness: "hermes".into(),
..Settings::default()
};
let mut patch = SettingsPatch::default();
patch.set_text(TextField::Harness, form::HARNESS_OFF);
assert!(
chat_surface_reloads(&settings, &patch),
"an already-open surface must hear the opening"
);
}

/// The bug that mattered most: a source row naming a Harness this machine
/// has not got leaves the handle in place, and freezing the HTTP rows on
/// that presence would leave no reachable Completer at all (#452).
Expand Down
7 changes: 4 additions & 3 deletions src/chat.js
Original file line number Diff line number Diff line change
Expand Up @@ -367,13 +367,14 @@ async function start() {
{ target: chat.label },
);

// Name and Character only. The command is one-shot at start() for
// whether anything can answer; a Character switch has to reach a
// window that is already listening. #375.
// Full opening, not only name and Character: a Director or Completer-source
// change has to re-run `attached()` on a window that is already listening.
// #375, #473.
await listen(
"chat-opening",
({ payload }) => {
showWho(payload);
attached(payload);
},
{ target: chat.label },
);
Expand Down
Loading