From cd053f184e25d292eb7ef9a7548b64a821aff1f9 Mon Sep 17 00:00:00 2001 From: omesser Date: Wed, 23 Sep 2026 02:44:51 +0300 Subject: [PATCH 1/2] refactor(overlay): Null the bubble fields at emit and delete forOverlay The Shell knows the bubble owner, so the payload now arrives correct rather than the webview filtering it back. Co-Authored-By: Claude Opus 5 (1M context) --- src-tauri/src/frame_loop.rs | 24 +--------- src-tauri/src/main.rs | 95 +++++++++++++++++++++++++++++++++++-- src/bubble.js | 8 ---- src/main.js | 15 +++--- 4 files changed, 101 insertions(+), 41 deletions(-) diff --git a/src-tauri/src/frame_loop.rs b/src-tauri/src/frame_loop.rs index 94c7490b5..f6822e28d 100644 --- a/src-tauri/src/frame_loop.rs +++ b/src-tauri/src/frame_loop.rs @@ -6,7 +6,7 @@ use std::time::{Duration, Instant, SystemTime, UNIX_EPOCH}; use ai_buddy_core::director::{self, Context, Happened, Wake}; use ai_buddy_core::dispatch::{dispatch, DenyList, DispatchContext, InstanceInfo}; -use ai_buddy_core::engine::{BehaviorProposal, Cue, State, Verb}; +use ai_buddy_core::engine::{BehaviorProposal, State, Verb}; use ai_buddy_core::input::press_target; use ai_buddy_core::overlay::{bubble_owner, display_index_for, place_sprite}; use ai_buddy_core::roster::{InstanceId, Roster}; @@ -1756,27 +1756,7 @@ pub(crate) fn run_frame_loop( // Addressed, not emitted to all: an untargeted listener would draw the last display's rects. let sprites = placed .iter() - .map(|instance| { - let local = instance.sprite.in_overlay(*display); - SpritePlacement { - id: &instance.id, - character: &instance.character, - x: local.x, - y: local.y, - width: instance.width, - height: instance.height, - animation: &instance.animation, - frame_index: instance.frame_index, - mirror: instance.mirror, - dialogue: instance.dialogue.clone(), - thinking: instance.thinking, - // Every overlay draws the art; one draws the - // bubble (#178, `bubble_owner`), and `forOverlay` - // strips the rest from the ones that lost. - bubble: instance.owner == Some(index), - cue: instance.cue.map(Cue::name), - } - }) + .map(|instance| SpritePlacement::new(instance, *display, index)) .collect(); let placement = Placement { diff --git a/src-tauri/src/main.rs b/src-tauri/src/main.rs index 083eeb441..5b3ec95b9 100644 --- a/src-tauri/src/main.rs +++ b/src-tauri/src/main.rs @@ -333,20 +333,52 @@ struct SpritePlacement<'a> { /// mirroring would turn it back round. Hit-test uses the same answer. mirror: i8, /// A line to speak on this tick only. Dialogue is an event, not a state. - /// #119: the webview latches it and owns display duration. + /// #119: the webview latches it and owns display duration. `None` on every + /// overlay but the bubble owner's. dialogue: Option, /// Whether to show the thinking ellipsis. Derived from what the Instance /// has on the wire. #119: grace and min-hold are in the webview so the - /// Engine stays tick-pure. + /// Engine stays tick-pure. False on every overlay but the bubble owner's. thinking: bool, /// Whether this overlay draws this Instance's bubble (#178, `bubble_owner`). + /// Still sent to the overlays that lost, which drop the bubble they were + /// showing on the tick the answer changes. bubble: bool, /// Cue to play this tick only, by the name the webview keys visual and - /// sound by. Only the bubble owner acts (`forOverlay`): every overlay + /// sound by. `None` on every overlay but the bubble owner's: every overlay /// draws the art, so a cue from all of them is one sound per display. #277 cue: Option<&'static str>, } +impl<'a> SpritePlacement<'a> { + /// One Instance as one overlay is told about it, in that overlay's own + /// coordinates. + /// + /// Every overlay draws the art. Only the bubble owner is told the line, + /// the indicator and the cue (#178, #277). Decided here because the Shell + /// already knows the owner: the webview used to strip these itself, which + /// made it reconstruct an answer it had been handed. + fn new(instance: &'a Placed, display: Rect, index: usize) -> Self { + let local = instance.sprite.in_overlay(display); + let bubble = instance.owner == Some(index); + Self { + id: &instance.id, + character: &instance.character, + x: local.x, + y: local.y, + width: instance.width, + height: instance.height, + animation: &instance.animation, + frame_index: instance.frame_index, + mirror: instance.mirror, + dialogue: instance.dialogue.clone().filter(|_| bubble), + thinking: bubble && instance.thinking, + bubble, + cue: instance.cue.filter(|_| bubble).map(Cue::name), + } + } +} + /// One tick's instruction to the renderer. Pushed so the webview holds no /// state. One message for every sprite, because the list is also which /// Instances still exist; sent separately, a dismiss would look like a late frame. @@ -3688,6 +3720,63 @@ mod tests { ); } + /// #178 and #277: every overlay is told about every Instance, and only the + /// one that owns the bubble is told the line, the indicator and the cue. + /// The webview used to strip these for itself. + #[test] + fn only_the_bubble_owner_is_told_the_line_the_indicator_and_the_cue() { + let left = Rect { + x: 0.0, + y: 0.0, + width: 1920.0, + height: 1080.0, + }; + let right = Rect { x: 1920.0, ..left }; + let placed = Placed { + id: "one".to_string(), + character: "bmo".to_string(), + sprite: SpriteRect { + x: 2000, + y: 400, + scale: 2, + }, + width: 128, + height: 128, + animation: "idle".to_string(), + frame_index: 0, + mirror: 1, + dialogue: Some("Yare yare daze.".to_string()), + thinking: true, + cue: Some(Cue::Poke), + owner: Some(1), + mask: ai_buddy_core::overlay::AlphaMask::from_png(PATCHY, 128) + .expect("the 2x2 fixture decodes"), + }; + + let owner = SpritePlacement::new(&placed, right, 1); + assert_eq!(owner.dialogue.as_deref(), Some("Yare yare daze.")); + assert!(owner.thinking); + assert!(owner.bubble); + assert_eq!(owner.cue, Some("poke")); + + let elsewhere = SpritePlacement::new(&placed, left, 0); + assert_eq!(elsewhere.dialogue, None, "no line to latch off the owner"); + assert!(!elsewhere.thinking, "no indicator to arm off the owner"); + assert!(!elsewhere.bubble); + assert_eq!(elsewhere.cue, None, "or the cue sounds once per display"); + + assert_eq!( + (elsewhere.animation, elsewhere.frame_index, elsewhere.mirror), + (owner.animation, owner.frame_index, owner.mirror), + "both draw the same art" + ); + assert_eq!( + (owner.x, elsewhere.x), + (80, 2000), + "each in its own overlay's coordinates, so the halves meet on the seam" + ); + } + fn instance_row(id: &str, name: &str) -> InstanceRow { InstanceRow { id: id.to_string(), diff --git a/src/bubble.js b/src/bubble.js index e1670f135..ed08578a7 100644 --- a/src/bubble.js +++ b/src/bubble.js @@ -185,11 +185,3 @@ export function placeBubble(spriteRect, bubbleSize, displayBounds) { return { x, y, tailOffset, inverted: wouldCoverSprite }; } - -// A placement as this overlay may act on it: the shell names one bubble owner -// per Instance, and stripping the bubble fields keeps a losing overlay from -// arming the thinking grace. The cue goes too, or it would sound once per display. -export function forOverlay(placement) { - if (placement.bubble) return placement; - return { ...placement, dialogue: null, thinking: false, cue: null }; -} diff --git a/src/main.js b/src/main.js index 38c225802..942336a37 100644 --- a/src/main.js +++ b/src/main.js @@ -5,7 +5,6 @@ import { arrived, interpolate, onDisplay } from "./interpolate.js"; import { createBubbleMachine, - forOverlay, wrapText, placeBubble, } from "./bubble.js"; @@ -370,15 +369,15 @@ async function start() { view.sprite.style.zIndex = `${index * 2}`; view.cueLayer.style.zIndex = `${index * 2}`; - // One overlay owns each Instance's bubble (`bubble_owner`). Speech hides - // on its reading timer, not on the next frame, so the overlay that just - // lost ownership drops its bubble once, on the change. - const owned = forOverlay(sprite); + // One overlay owns each Instance's bubble (`bubble_owner`), and the + // Shell sends the line, the indicator and the cue to that one only. + // Speech hides on its reading timer, not on the next frame, so the + // overlay that just lost ownership drops its bubble once, on the change. if (!sprite.bubble && view.latest?.bubble !== false) view.bubbles.hideAllNow(); view.previous = view.latest; view.latest = { - ...owned, + ...sprite, visible: payload.visible, fade_ms: payload.fade_ms, // Whether a cue may be heard as well as seen. Settings and Do Not @@ -389,9 +388,9 @@ async function start() { // Dialogue rides one tick and `latest` keeps only the newest placement, // so an Engine outpacing the display would lose pulses before `draw` // read them. The machine latches the pulse here, where every delivery is seen. - view.bubbles.event(owned); + view.bubbles.event(sprite); // A cue is the same shape of pulse, latched in the same place. It reads - // `latest` rather than `owned` for the two answers that belong to the + // `latest` rather than `sprite` for the two answers that belong to the // desktop: whether the Character is on screen, and whether it may be heard. view.cues.event(view.latest); From 570893fa4c7f16927f2192ef1e52b8029c80d648 Mon Sep 17 00:00:00 2001 From: omesser Date: Wed, 23 Sep 2026 02:44:59 +0300 Subject: [PATCH 2/2] test(bubble): Pin the Shell's carry window to the bubble's reading clamp Co-Authored-By: Claude Opus 5 (1M context) --- tests/bubble.test.js | 52 ++++++++++++++++++++++++-------------------- 1 file changed, 29 insertions(+), 23 deletions(-) diff --git a/tests/bubble.test.js b/tests/bubble.test.js index 59196e5f3..b152e4a4e 100644 --- a/tests/bubble.test.js +++ b/tests/bubble.test.js @@ -11,7 +11,6 @@ import { wrapText, placeBubble, createBubbleMachine, - forOverlay, THINKING_GRACE_MS, THINKING_MIN_HOLD_MS, } from "../src/bubble.js"; @@ -30,6 +29,23 @@ test("bubble duration is 900ms + 55ms per character, clamped to 2-8s", () => { assert.equal(bubbleDuration(long), 8000, "max clamp"); }); +// One Speech duration. `bubbleDuration` clamps how long the bubble may show a +// line; the Shell's `CARRY_WINDOW` is how long it may re-say one to a new bubble +// owner. A longer carry resurrects a line nobody could still be reading, and a +// shorter one drops one still on screen (#178). +test("the Shell carries a line for exactly as long as the bubble can show one", () => { + const dir = dirname(fileURLToPath(import.meta.url)); + const shell = readFileSync(join(dir, "../src-tauri/src/main.rs"), "utf8"); + + const carry = shell.match(/const CARRY_WINDOW: Duration = Duration::from_secs\((\d+)\)/); + assert.ok(carry, "the Shell names a carry window"); + assert.equal( + Number(carry[1]) * 1000, + bubbleDuration("a".repeat(1000)), + "the carry window is bubbleDuration's max clamp", + ); +}); + test("wrap text at max width", () => { const short = "hi"; const { lines, truncated } = wrapText(short, 260, testMeasureFn); @@ -441,30 +457,20 @@ test("a reply landing in the post-speech grace never flashes the indicator", () ); }); -// --- One overlay owns the bubble; the rest draw the art only. --- - // --- #178: one overlay owns the bubble; the rest draw the art only. --- +// The Shell nulls `dialogue`, `thinking` and `cue` on every overlay but the +// owner's, so the placements below are what a losing overlay is really handed. -test("a placement this overlay does not own carries no bubble", () => { - const spoken = { dialogue: "Yare yare daze.", thinking: true, bubble: true, x: 1 }; - assert.equal(forOverlay(spoken), spoken, "the owner sees it untouched"); - - const elsewhere = forOverlay({ ...spoken, bubble: false }); - assert.equal(elsewhere.dialogue, null, "no line to latch on the wrong display"); - assert.equal(elsewhere.thinking, false, "no thinking to arm on the wrong display"); - assert.equal(elsewhere.x, 1, "everything the art needs is left alone"); -}); - -test("a losing overlay never arms the indicator off a thinking it does not own", () => { +test("a losing overlay is told no thinking, so it never arms the indicator", () => { const { machine, advance, placement, surface } = machineHarness(); - machine.frame(forOverlay(placement({ thinking: true, bubble: false }))); + machine.frame(placement({ thinking: false, bubble: false })); advance(THINKING_GRACE_MS + THINKING_MIN_HOLD_MS); assert.equal(surface(), null, "grace never armed: this display is not the owner"); - machine.frame(forOverlay(placement({ thinking: true, bubble: true }))); + machine.frame(placement({ thinking: true, bubble: true })); advance(THINKING_GRACE_MS); - assert.equal(surface(), "thinking", "the same frame, owned, arms it"); + assert.equal(surface(), "thinking", "the same turn, owned, arms it"); }); test("a line crossing the seam hides on the old display before it shows on the new", () => { @@ -474,16 +480,16 @@ test("a line crossing the seam hides on the old display before it shows on the n const a = machineHarness(); const b = machineHarness(); - a.machine.event(forOverlay(a.placement({ dialogue: "hi", bubble: true }))); - a.machine.frame(forOverlay(a.placement({ bubble: true }))); - b.machine.event(forOverlay(b.placement({ dialogue: "hi", bubble: false }))); + a.machine.event(a.placement({ dialogue: "hi", bubble: true })); + a.machine.frame(a.placement({ bubble: true })); + b.machine.event(b.placement({ dialogue: null, bubble: false })); assert.equal(a.surface(), "speech", "the owner shows the line"); - assert.equal(b.surface(), null, "the other display never latched it"); + assert.equal(b.surface(), null, "the other display was never told the line"); // Mid-reading, ownership flips: the shell re-pulses to b and stops naming a. a.machine.hideAllNow(); - b.machine.event(forOverlay(b.placement({ dialogue: "hi", bubble: true }))); - b.machine.frame(forOverlay(b.placement({ bubble: true }))); + b.machine.event(b.placement({ dialogue: "hi", bubble: true })); + b.machine.frame(b.placement({ bubble: true })); assert.equal(a.surface(), null, "the old display is already clear"); assert.equal(b.surface(), "speech", "and the new one shows the same line"); });