Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix(a11y): release Shift+Tab from the Shell terminal and enable screen-reader mode

Louis Deconinck53af027e4f95
diff --git a/client/src/components/KeyboardHelp.jsx b/client/src/components/KeyboardHelp.jsx
index ab6c5fcbe1..4489800478 100644
--- a/client/src/components/KeyboardHelp.jsx
+++ b/client/src/components/KeyboardHelp.jsx
@@ -31,6 +31,14 @@ const SHORTCUT_SECTIONS = [
{ keys: ['E'], description: 'Interact with building' },
],
},
+ {
+ // The terminal keeps Tab for shell completion, so the way back out is a
+ // backtab — see useShellSession's attachCustomKeyEventHandler.
+ title: 'Shell',
+ shortcuts: [
+ { keys: ['Shift+Tab'], description: 'Move focus out of the terminal' },
+ ],
+ },
];
diff --git a/client/src/hooks/useShellSession.js b/client/src/hooks/useShellSession.js
index 1ad4b26869..61eefd3c21 100644
--- a/client/src/hooks/useShellSession.js
+++ b/client/src/hooks/useShellSession.js
@@ -7,6 +7,7 @@ import '@xterm/xterm/css/xterm.css';
import { useSocket } from './useSocket';
import { useThemeContext } from '../components/ThemeContext';
import { buildTerminalTheme, parseCssColorToHex } from '../lib/terminalTheme';
+import { isFocusEscapeKey } from '../lib/a11yKeyboard';
import { attachDictationBridge } from '../lib/terminalDictation';
import { fitTerminal } from '../lib/terminalFit';
import {
@@ -263,7 +264,14 @@ export function useShellSession({ isFullscreen } = {}) {
fontFamily: '"Roboto Mono for Powerline", "MesloLGS NF", "MesloLGS Nerd Font", "Hack Nerd Font", "FiraCode Nerd Font", "JetBrainsMono Nerd Font", Menlo, Monaco, "Courier New", monospace',
theme: readTerminalTheme(),
scrollback: 5000,
- allowProposedApi: true
+ allowProposedApi: true,
+ // Screen-reader mode is what makes xterm build its .xterm-accessibility
+ // live region — without it every rendered row stays aria-hidden and a
+ // screen-reader user focusing the terminal hears an empty textarea. It
+ // also relaxes _keyDown's blanket preventDefault on resolved keys, which
+ // the dictation bridge (lib/terminalDictation.js) is already written to
+ // defer to.
+ screenReaderMode: true
});
const webLinksAddon = new WebLinksAddon();
@@ -272,6 +280,17 @@ export function useShellSession({ isFullscreen } = {}) {
term.open(terminalRef.current);
+ // WCAG 2.1.2 (no keyboard trap): xterm's helper textarea is in the tab
+ // order but its _keyDown preventDefaults Tab, Shift+Tab and Escape alike,
+ // so a keyboard user who tabs in cannot leave without a mouse. The
+ // documented escape is Shift+Tab — the conventional "leave this widget"
+ // backtab: returning false makes _keyDown bail before it cancels the
+ // event, so the browser performs the focus move to the previous tabbable
+ // element and nothing reaches the PTY. Plain Tab must stay claimed — it is
+ // shell completion. The gesture itself is the shared isFocusEscapeKey
+ // predicate so the stand-down rule lives in one place.
+ term.attachCustomKeyEventHandler((event) => !isFocusEscapeKey(event));
+
// xterm binds no touch handlers of its own, so without this a swipe over the
// terminal does nothing. Alternate-screen TUIs need the matching wheel capture so
// OpenCode's supported PageUp/PageDown bindings receive scroll gestures instead
@@ -282,13 +301,17 @@ export function useShellSession({ isFullscreen } = {}) {
const detachTouchScroll = attachTerminalTouchScroll(term);
const detachWheelScroll = attachTerminalWheelScroll(term);
- requestAnimationFrame(() => {
+ // Deferred fit — cancelled in cleanup: an unmount inside the frame window
+ // would otherwise fit a disposed terminal (its _renderService.dimensions
+ // getter throws post-dispose — an uncaught error in a rAF callback).
+ const fitFrame = requestAnimationFrame(() => {
fitTerminal(term);
});
termInstanceRef.current = term;
return () => {
+ cancelAnimationFrame(fitFrame);
detachTouchScroll();
detachWheelScroll();
term.dispose();
diff --git a/client/src/hooks/useShellSession.test.jsx b/client/src/hooks/useShellSession.test.jsx
index 73c6a3fc1e..eabc985550 100644
--- a/client/src/hooks/useShellSession.test.jsx
+++ b/client/src/hooks/useShellSession.test.jsx
@@ -1,6 +1,7 @@
import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
-import { renderHook, act, cleanup } from '@testing-library/react';
+import { renderHook, render, act, cleanup } from '@testing-library/react';
import { MemoryRouter } from 'react-router';
+import { Terminal } from '@xterm/xterm';
// Capture the socket handlers the hook registers so the test can drive the
// shell:* protocol, and record every emit so we can assert the client half of
@@ -400,4 +401,70 @@ describe('useShellSession', () => {
expect(lastEmit('shell:attach')).toBeUndefined();
});
});
+
+ // The terminal-init effect only runs when `terminalRef` is attached, which the
+ // socket-level tests above never do — so these mount a probe component that
+ // renders a real host <div>. That constructs a REAL Terminal, which is what
+ // makes the keyboard-trap assertions meaningful: xterm's own `_keyDown` either
+ // cancels the keydown (trapping focus) or releases it for the browser's
+ // default backtab. See issue #7262 (WCAG 2.1.2) — without the custom handler,
+ // Tab/Shift+Tab/Escape are all swallowed by the textarea.
+ describe('terminal accessibility', () => {
+ let attachSpy;
+ const mountShellTerminal = () => {
+ attachSpy = vi.spyOn(Terminal.prototype, 'attachCustomKeyEventHandler');
+ const Probe = () => {
+ const { terminalRef } = useShellSession({});
+ return <div ref={terminalRef} />;
+ };
+ const rendered = render(<Probe />, { wrapper });
+ return { term: attachSpy.mock.contexts[0], host: rendered.container.firstChild };
+ };
+ afterEach(() => attachSpy?.mockRestore());
+
+ it('constructs the terminal with screenReaderMode, so output reaches a live region', () => {
+ const { term, host } = mountShellTerminal();
+ // Pin the option itself — a future options rewrite must not silently drop it.
+ expect(term.options.screenReaderMode).toBe(true);
+ // Its observable effect: the .xterm-accessibility live region only exists
+ // under screenReaderMode; without it every rendered row is aria-hidden.
+ expect(host.querySelector('.xterm-accessibility .live-region')).toBeTruthy();
+ });
+
+ it('installs a custom key handler that releases Shift+Tab but keeps Tab for the shell', () => {
+ mountShellTerminal();
+ expect(attachSpy).toHaveBeenCalledTimes(1);
+ const handler = attachSpy.mock.calls[0][0];
+ // Shift+Tab is the documented focus-escape gesture — false tells xterm not
+ // to process the event at all, so the browser performs the backtab.
+ expect(handler({ key: 'Tab', shiftKey: true })).toBe(false);
+ // Plain Tab stays claimed — it is shell completion.
+ expect(handler({ key: 'Tab', shiftKey: false })).toBe(true);
+ // Escape is NOT the way out: TUIs bind it, so it must keep reaching the PTY.
+ expect(handler({ key: 'Escape', shiftKey: false })).toBe(true);
+ });
+
+ it('lets a Shift+Tab keydown through uncancelled so focus can leave the terminal', () => {
+ const { term } = mountShellTerminal();
+ // Input only reaches the socket once a session is attached.
+ fire('shell:sessions', []);
+ fire('shell:started', { sessionId: 'abc' });
+ emitted.length = 0;
+
+ const backtab = new KeyboardEvent('keydown', { key: 'Tab', keyCode: 9, shiftKey: true, cancelable: true });
+ term.textarea.dispatchEvent(backtab);
+ expect(backtab.defaultPrevented).toBe(false);
+ // The escape must also be clean: without the custom handler, SR mode still
+ // releases the key but fires ESC[Z into the PTY (zsh reads it as reverse
+ // completion). The early return sends nothing.
+ expect(emitted).toHaveLength(0);
+
+ // The control case: plain Tab is still claimed and reaches the PTY as
+ // completion input.
+ const complete = new KeyboardEvent('keydown', { key: 'Tab', keyCode: 9, cancelable: true });
+ term.textarea.dispatchEvent(complete);
+ expect(complete.defaultPrevented).toBe(true);
+ expect(lastEmit('shell:input')).toEqual(['shell:input', { sessionId: 'abc', data: '\t' }]);
+ });
+ });
});
diff --git a/client/src/lib/README.md b/client/src/lib/README.md
index d61a0bfa8b..e30180c552 100644
--- a/client/src/lib/README.md
+++ b/client/src/lib/README.md
@@ -117,7 +117,7 @@ grep -i "what you want to do" client/src/lib/README.md
| Module | Purpose |
|---|---|
-| `a11yKeyboard.js` | `clickableProps(handler, { role?, disabled? })` → `{ role, tabIndex, onKeyDown }` and `onActivateKeyDown(handler)` — make a non-`<button>` element with an `onClick` keyboard-accessible (Enter/Space activation, focusable, `role="button"`). Spread `clickableProps` next to the existing `onClick`. Use instead of hand-rolling `role`/`tabIndex`/`onKeyDown` on clickable `<div>`/`<span>`/`<li>`. Also the shared predicate every app-global key handler consults: `shouldIgnoreGlobalKey(event, { enabledInDialog?, ignoreRepeat?, allowChords? })` — the single "must this handler stand down?" answer (editable target, native Space button activation, ⌘/Ctrl/Alt chord, OS auto-repeat, open `aria-modal` dialog), used by `useKeyboardShortcuts`, `useKeyCapture` AND the voice widget's push-to-talk hotkey so their guard lists can't drift. Add a new guard there, not at one caller. Its parts are exported too: `isEditableTarget(el)`, `isButtonActivation(event)` (true when Space would natively activate the focused button) and `isPressKey(event)` (Enter or Space by either `key` or `code`). Plus `noPointerFocusSurfaceProps` — spread on the ROOT of a surface that owns a key globally (a Space-scored drill, the Morse keyer, RapidReader, the OpenWorld HUD); one capture-phase handler stops a mouse click anywhere inside from parking focus on a button and taking that key over via native activation, covering controls added later too. |
+| `a11yKeyboard.js` | `clickableProps(handler, { role?, disabled? })` → `{ role, tabIndex, onKeyDown }` and `onActivateKeyDown(handler)` — make a non-`<button>` element with an `onClick` keyboard-accessible (Enter/Space activation, focusable, `role="button"`). Spread `clickableProps` next to the existing `onClick`. Use instead of hand-rolling `role`/`tabIndex`/`onKeyDown` on clickable `<div>`/`<span>`/`<li>`. Also the shared predicate every app-global key handler consults: `shouldIgnoreGlobalKey(event, { enabledInDialog?, ignoreRepeat?, allowChords? })` — the single "must this handler stand down?" answer (editable target, native Space button activation, ⌘/Ctrl/Alt chord, OS auto-repeat, open `aria-modal` dialog), used by `useKeyboardShortcuts`, `useKeyCapture` AND the voice widget's push-to-talk hotkey so their guard lists can't drift. Add a new guard there, not at one caller. Its parts are exported too: `isEditableTarget(el)`, `isButtonActivation(event)` (true when Space would natively activate the focused button) and `isPressKey(event)` (Enter or Space by either `key` or `code`). Also `isFocusEscapeKey(event)` — the shared "leave this widget" gesture (Shift+Tab backtab) that a key-capturing widget must release to the browser; the Shell terminal's `attachCustomKeyEventHandler` consults it so focus can escape the xterm trap. Plus `noPointerFocusSurfaceProps` — spread on the ROOT of a surface that owns a key globally (a Space-scored drill, the Morse keyer, RapidReader, the OpenWorld HUD); one capture-phase handler stops a mouse click anywhere inside from parking focus on a button and taking that key over via native activation, covering controls added later too. |
| `agentResumeOutcome.js` | `agentResumeMessage(result, messages, fallback)` — the toast wording for a CoS agent resume/relaunch result. The server answers with a `mode` (what it did to the task) plus `spawned`/`spawnHold` (whether it also STARTED it — a resumed task is force-spawned when a slot is free, and `spawnHold` names the refusal when it isn't). `messages` is keyed by mode as `{ queued, running? }`; a mode with no `running` variant is one that deliberately queues nothing, so it can never be reported as started. Shared by the Resume dialog (`cos/tabs/AgentsTab.jsx`) and `cos/tabs/RelaunchAgentModal.jsx` so neither can say "queued" for a run that already started, or omit the reason it didn't. |
| `appIdentity.js` | `PORTOS_APP_ID` — stable id of the baseline PortOS app. Re-exported from `server/lib/appIdentity.js`; dependency-free so a module reachable from a server test can import it. |
| `applyManuscriptEdits.js` | `applyEditsToContent(content, edits, anchorQuote)` — PREVIEW-ONLY client mirror of the server's accept splice (`server/services/pipeline/manuscriptFix.js`): locate each `find` (nearest the anchor when recurring), drop overlaps, replace bottom-up. Powers the Manuscript editor's whole-manuscript impact preview. |
diff --git a/client/src/lib/a11yKeyboard.js b/client/src/lib/a11yKeyboard.js
index f260e88c33..9698fe748a 100644
--- a/client/src/lib/a11yKeyboard.js
+++ b/client/src/lib/a11yKeyboard.js
@@ -177,6 +177,32 @@ export function shouldIgnoreGlobalKey(event, { enabledInDialog = false, ignoreRe
return false;
}
+/**
+ * True when a keystroke is the focus-escape gesture — a backtab (Shift+Tab),
+ * the conventional "leave this widget" move — that a key-capturing widget must
+ * release to the browser instead of consuming.
+ *
+ * The consumer is a widget whose input surface claims nearly every keystroke —
+ * the Shell page's xterm terminal, whose helper textarea sits in the tab order
+ * but whose `_keyDown` `preventDefault`s Tab, Shift+Tab and Escape alike
+ * (WCAG 2.1.2 keyboard trap). Installed as
+ * `term.attachCustomKeyEventHandler((e) => !isFocusEscapeKey(e))`, the `false`
+ * return makes xterm bail before it cancels the event, so the browser performs
+ * the backtab and focus lands on the previous tabbable element — and nothing
+ * is sent to the PTY. Plain Tab stays claimed: it is shell completion, and
+ * forward-leaving is what the controls laid out before the terminal are for.
+ *
+ * Chorded variants are deliberately not excluded — a Ctrl/Alt+Shift+Tab the
+ * browser or OS owns (previous browser tab, window switching) must pass
+ * through rather than be eaten by the widget too.
+ *
+ * @param {KeyboardEvent} event
+ * @returns {boolean}
+ */
+export function isFocusEscapeKey(event) {
+ return event?.key === 'Tab' && event?.shiftKey === true;
+}
+
/**
* Props for the ROOT of a surface that owns a key globally — a drill scored on
* Space, the Morse keyer, RapidReader, the OpenWorld rig.
diff --git a/client/src/lib/a11yKeyboard.test.js b/client/src/lib/a11yKeyboard.test.js
index 3091557012..312f98ca15 100644
--- a/client/src/lib/a11yKeyboard.test.js
+++ b/client/src/lib/a11yKeyboard.test.js
@@ -1,5 +1,5 @@
import { describe, it, expect, vi, afterEach } from 'vitest';
-import { onActivateKeyDown, clickableProps, isButtonActivation, isPressKey, isEditableTarget, shouldIgnoreGlobalKey, noPointerFocusSurfaceProps } from './a11yKeyboard.js';
+import { onActivateKeyDown, clickableProps, isButtonActivation, isPressKey, isEditableTarget, shouldIgnoreGlobalKey, isFocusEscapeKey, noPointerFocusSurfaceProps } from './a11yKeyboard.js';
describe('onActivateKeyDown', () => {
it('returns undefined when handler is not a function', () => {
@@ -206,6 +206,27 @@ describe('shouldIgnoreGlobalKey', () => {
});
});
+describe('isFocusEscapeKey', () => {
+ it('is true only for a backtab — Shift+Tab is the "leave this widget" gesture', () => {
+ expect(isFocusEscapeKey({ key: 'Tab', shiftKey: true })).toBe(true);
+ // Plain Tab stays with the widget (shell completion), as do other keys.
+ expect(isFocusEscapeKey({ key: 'Tab', shiftKey: false })).toBe(false);
+ expect(isFocusEscapeKey({ key: 'Tab' })).toBe(false);
+ expect(isFocusEscapeKey({ key: 'Escape', shiftKey: true })).toBe(false);
+ expect(isFocusEscapeKey({ key: 'a', shiftKey: true })).toBe(false);
+ });
+
+ it('does not exclude chords — a Ctrl/Alt+Shift+Tab belongs to the browser or OS, not the widget', () => {
+ expect(isFocusEscapeKey({ key: 'Tab', shiftKey: true, ctrlKey: true })).toBe(true);
+ expect(isFocusEscapeKey({ key: 'Tab', shiftKey: true, altKey: true })).toBe(true);
+ });
+
+ it('is false for a missing event', () => {
+ expect(isFocusEscapeKey(null)).toBe(false);
+ expect(isFocusEscapeKey(undefined)).toBe(false);
+ });
+});
+
describe('noPointerFocusSurfaceProps', () => {
const press = (el) => {
const event = new MouseEvent('mousedown', { bubbles: true, cancelable: true });
diff --git a/client/src/pages/Shell.jsx b/client/src/pages/Shell.jsx
index e85cb91e5b..e562973f0d 100644
--- a/client/src/pages/Shell.jsx
+++ b/client/src/pages/Shell.jsx
@@ -9,6 +9,7 @@ import TerminalHotKeys from '../components/shell/TerminalHotKeys';
import ShellSessionTabs from '../components/shell/ShellSessionTabs';
import ShellProviderLauncher from '../components/shell/ShellProviderLauncher';
import InfoTooltip from '../components/ui/InfoTooltip';
+import Kbd from '../components/ui/Kbd';
import { useInstanceFeatures } from '../hooks/useInstanceFeatures.js';
// Typed-into-the-current-session shortcuts only. The AI CLIs that used to sit
@@ -308,6 +309,14 @@ export default function Shell() {
/>
</div>
+ {/* The keyboard exit has to be visible text, not a title tooltip: the
+ terminal claims Tab for completion, so Shift+Tab (handled in
+ useShellSession's custom key handler) is the documented way back out —
+ WCAG 2.1.2 wants the escape discoverable, not just wired. */}
+ <p className="shrink-0 px-1 text-xs text-gray-500">
+ <Kbd size="sm">Shift+Tab</Kbd> moves focus out of the terminal
+ </p>
+
{/* Fullscreen control bar — compact, single-row, horizontally scrollable so
the TUI-driving keys stay reachable by thumb without the stacked toolbars. */}
{isFullscreen && (