Skip to content

Dodaios is an open-source agent OS for your desktop: it runs your coding agents, browser and apps by voice or chat, on your own machine. Formerly Personal Jarvis.

fix(agents): let dialogs and fields own Escape, keep spot reservations across roster changes

rubenluetke10-beep0deb2a6e5421
diff --git a/jarvis/ui/web/frontend/src/components/society/office/OfficePlayer.keys.test.ts b/jarvis/ui/web/frontend/src/components/society/office/OfficePlayer.keys.test.ts
new file mode 100644
index 0000000000..04c2405acd
--- /dev/null
+++ b/jarvis/ui/web/frontend/src/components/society/office/OfficePlayer.keys.test.ts
@@ -0,0 +1,22 @@
+import { afterEach, describe, expect, it } from "vitest";
+import { ownsKeyboard } from "./OfficePlayer";
+
+afterEach(() => { document.body.innerHTML = ""; });
+
+describe("ownsKeyboard", () => {
+ it("leaves plain page focus to the character", () => {
+ expect(ownsKeyboard(document.body)).toBe(false);
+ });
+ it("gives text fields their keys", () => {
+ const input = document.createElement("input");
+ document.body.append(input);
+ expect(ownsKeyboard(input)).toBe(true);
+ });
+ it("gives an open dialog every key, even with focus outside it", () => {
+ const dialog = document.createElement("div");
+ dialog.setAttribute("role", "dialog");
+ dialog.setAttribute("data-state", "open");
+ document.body.append(dialog);
+ expect(ownsKeyboard(document.body)).toBe(true);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/society/office/OfficePlayer.tsx b/jarvis/ui/web/frontend/src/components/society/office/OfficePlayer.tsx
index cfadd423ec..048ddea8f7 100644
--- a/jarvis/ui/web/frontend/src/components/society/office/OfficePlayer.tsx
+++ b/jarvis/ui/web/frontend/src/components/society/office/OfficePlayer.tsx
@@ -26,6 +26,8 @@ const MOVE_KEYS: Record<string, [number, number]> = {
/** Keys typed into a field, a dialog or any other DOM control never walk the character. */
export function ownsKeyboard(target: EventTarget | null): boolean {
+ // An open modal owns every key, wherever focus happens to sit.
+ if (typeof document !== "undefined" && document.querySelector("[role='dialog'][data-state='open'], [role='alertdialog'][data-state='open']")) return true;
if (!(target instanceof HTMLElement)) return false;
if (target.isContentEditable) return true;
const tag = target.tagName;
diff --git a/jarvis/ui/web/frontend/src/components/society/office/OfficeStage.tsx b/jarvis/ui/web/frontend/src/components/society/office/OfficeStage.tsx
index dffe58f53f..24e33eb6a9 100644
--- a/jarvis/ui/web/frontend/src/components/society/office/OfficeStage.tsx
+++ b/jarvis/ui/web/frontend/src/components/society/office/OfficeStage.tsx
@@ -23,6 +23,7 @@ import { knownAgents } from "./walkerRegistry";
import { loadProfile, saveProfile, type PlayerProfile } from "./playerProfile";
import { AgentPanel, CheckpointPanel, type OfficeActions } from "./OfficePanels";
import type { WalkerContext } from "./OfficeAgents";
+import { ownsKeyboard } from "./OfficePlayer";
import "./office.css";
import "./officeHud.css";
@@ -71,6 +72,9 @@ function useNewcomers(active: SocietyAgent[]): ReadonlySet<string> {
useEffect(() => {
if (active.length === 0) return;
if (knownAgents.size === 0) { active.forEach((a) => knownAgents.add(a.agentId)); return; }
+ // Forget agents that left, so the set never outgrows the roster.
+ const present = new Set(active.map((a) => a.agentId));
+ for (const id of [...knownAgents]) if (!present.has(id)) knownAgents.delete(id);
const fresh = active.filter((a) => !knownAgents.has(a.agentId)).map((a) => a.agentId);
if (fresh.length === 0) return;
fresh.forEach((id) => knownAgents.add(id));
@@ -105,7 +109,10 @@ export function OfficeStage({ onOpenLedger, onSelectAgent, onCreateAgent, onOpen
// eslint-disable-next-line react-hooks/exhaustive-deps
[seatingKey]);
const grid = useMemo(() => buildNavGrid(layout), [layout]);
- const book = useMemo(() => new SpotBook(), [layout]);
+ // One reservation book for the whole visit: a rebuilt floor plan (someone
+ // joined or left) must not forget who already sits on which couch.
+ const [book] = useState(() => new SpotBook());
+ useEffect(() => { book.retain(new Set(layout.spots.map((spot) => spot.id))); }, [book, layout]);
const agentsRef = useRef(agents);
agentsRef.current = agents;
const walkers = useMemo<WalkerContext>(() => {
@@ -127,7 +134,8 @@ export function OfficeStage({ onOpenLedger, onSelectAgent, onCreateAgent, onOpen
// Escape closes an open panel before it can leave the map.
useEffect(() => {
const onKey = (event: KeyboardEvent) => {
- if (event.key !== "Escape" || !useOfficeStore.getState().selection) return;
+ // A field or a dialog (e.g. the create dialog opened from reception) owns its own Escape.
+ if (event.key !== "Escape" || !useOfficeStore.getState().selection || ownsKeyboard(event.target)) return;
event.preventDefault();
event.stopPropagation();
select(null);
@@ -136,6 +144,14 @@ export function OfficeStage({ onOpenLedger, onSelectAgent, onCreateAgent, onOpen
return () => document.removeEventListener("keydown", onKey, true);
}, [select]);
+ // Leaving the map forgets panels and calls; the office opens fresh next time.
+ useEffect(() => () => {
+ const store = useOfficeStore.getState();
+ store.select(null);
+ store.setNearby(null);
+ store.clearSummons();
+ }, []);
+
// A panel for an agent that left the roster closes itself.
useEffect(() => {
if (selection?.kind === "agent" && roster.data && !agents.has(selection.id)) select(null);
diff --git a/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.test.ts b/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.test.ts
index 4e254a49c9..937e59a1c5 100644
--- a/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.test.ts
+++ b/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.test.ts
@@ -153,3 +153,16 @@ describe("planFor", () => {
expect(book.isFree(idle.spotId!)).toBe(true);
});
});
+
+describe("SpotBook.retain", () => {
+ it("keeps claims on surviving spots and drops the rest", () => {
+ const book = new SpotBook();
+ book.claim("couch-a1", "a1");
+ book.claim("window-dept-9", "a2");
+ book.retain(new Set(["couch-a1"]));
+ expect(book.holder("couch-a1")).toBe("a1");
+ expect(book.isFree("window-dept-9")).toBe(true);
+ // The dropped agent can claim again.
+ expect(book.claim("couch-a2", "a2")).toBe(true);
+ });
+});
diff --git a/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.ts b/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.ts
index 833aa73679..05502cd949 100644
--- a/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.ts
+++ b/jarvis/ui/web/frontend/src/components/society/office/officeBehavior.ts
@@ -69,6 +69,13 @@ export class SpotBook {
if (this.bySpot.get(spotId) === agentId) this.bySpot.delete(spotId);
}
+ /** Keep only claims on spots that still exist (after the floor plan was rebuilt). */
+ retain(spotIds: ReadonlySet<string>): void {
+ for (const [spotId, agentId] of [...this.bySpot]) {
+ if (!spotIds.has(spotId)) { this.bySpot.delete(spotId); this.byAgent.delete(agentId); }
+ }
+ }
+
holder(spotId: string): string | null {
return this.bySpot.get(spotId) ?? null;
}