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

address review: switch-attach failure recovery; notify prior socket on session takeover

[._.]/ Adam Eivydc9a996bf6ad
diff --git a/.changelog/NEXT.md b/.changelog/NEXT.md
index 06bbf21d73..5668885a55 100644
--- a/.changelog/NEXT.md
+++ b/.changelog/NEXT.md
@@ -11,6 +11,16 @@
sessions) clears the URL back to `/shell`. The pre-existing `?session=<uuid>`
query-param (one-shot "attach to this session") still works alongside the
new path param.
+ - **Attach-failure recovery.** Switching sessions no longer pre-clears the
+ displayed session — `sessionIdRef` only swaps on `shell:attached`. If the
+ target session dies between list read and attach (race), `shell:error`
+ restores the URL + terminal to the previously displayed session (or falls
+ back to a live survivor) instead of stranding the UI on a dead URL.
+ - **Multi-tab handoff notification.** Opening the same `/shell/:sessionId`
+ in a second tab takes over the PTY socket (single-subscriber by design).
+ The server now emits `shell:detached` to the previous socket so the
+ original tab clears its disconnected view + navigates back to `/shell`
+ instead of sitting "Connected" with no output.
- **Universe Canon page — lock toggle, tag chips, and "from series" badge on every card.**
Phase 2a of the Universe-as-Canon UI. Each `CanonCard` (used on both the
diff --git a/client/src/pages/Shell.jsx b/client/src/pages/Shell.jsx
index 9d20bf6fd4..8dae5ff8f9 100644
--- a/client/src/pages/Shell.jsx
+++ b/client/src/pages/Shell.jsx
@@ -309,9 +309,12 @@ export default function Shell() {
const switchToSession = useCallback((sessionId, { fromUrl = false } = {}) => {
if (sessionId === sessionIdRef.current) return;
- clearActiveSession();
+ // Don't pre-clear — keep the previously displayed session in sessionIdRef until
+ // shell:attached lands (handleShellAttached → activateSession swaps atomically).
+ // If shell:error fires instead, handleShellError can restore URL/terminal to the
+ // session we were already showing rather than leaving the UI stranded on a dead URL.
attachToSession(sessionId, { intent: fromUrl ? undefined : 'push' });
- }, [attachToSession, clearActiveSession]);
+ }, [attachToSession]);
// User clicked "New" button — push intent so back/forward can return to the prior session.
const startNewSession = useCallback(() => {
@@ -425,13 +428,54 @@ export default function Shell() {
}
};
- const handleShellError = ({ error }) => {
+ const handleShellDetached = ({ sessionId: sid, reason }) => {
+ // Server notified us this session was taken over by another socket
+ // (typically the same user opening the deep link in another tab). The PTY
+ // stream now goes there; locally we drop the dead view rather than appear
+ // "Connected" forever with no output.
+ if (sid !== sessionIdRef.current) return;
+ clearActiveSession();
+ if (termInstanceRef.current) {
+ const note = reason === 'attached-elsewhere'
+ ? 'Session attached in another tab — disconnected here'
+ : 'Session detached';
+ termInstanceRef.current.writeln(`\r\n\x1b[33m[${note}]\x1b[0m`);
+ }
+ navigateRef.current('/shell', { replace: true });
+ };
+
+ const handleShellError = ({ error, sessionId: errSid }) => {
// Server rejected start/attach (e.g., session limit, session not found).
// No activateSession will fire to consume the intent, so reset here.
pendingNavIntentRef.current = 'replace';
if (termInstanceRef.current) {
termInstanceRef.current.writeln(`\r\n\x1b[31m[Error: ${error}]\x1b[0m`);
}
+ // Passive errors (shell:input / shell:stop on a missing session) carry a sessionId
+ // in the payload — no terminal state to recover.
+ if (errSid) return;
+ // Start/attach failure recovery. switchToSession leaves sessionIdRef pointing at
+ // the previously-displayed session, but attachToSession already cleared the
+ // terminal. Re-mirror the URL to the displayed session and re-attach to repaint;
+ // if the displayed session is also gone, fall back to a survivor.
+ const active = sessionIdRef.current;
+ if (!active) return;
+ const live = sessionsRef.current;
+ if (!live.some(s => s.sessionId === active)) {
+ clearActiveSession();
+ if (live.length > 0) {
+ const latest = live[live.length - 1];
+ navigateRef.current(`/shell/${latest.sessionId}`, { replace: true });
+ setTimeout(() => attachToSession(latest.sessionId), 100);
+ } else {
+ navigateRef.current('/shell', { replace: true });
+ }
+ return;
+ }
+ if (urlSessionIdRef.current && urlSessionIdRef.current !== active) {
+ navigateRef.current(`/shell/${active}`, { replace: true });
+ }
+ setTimeout(() => attachToSession(active), 100);
};
socket.on('connect', handleConnect);
@@ -441,6 +485,7 @@ export default function Shell() {
socket.on('shell:attached', handleShellAttached);
socket.on('shell:output', handleShellOutput);
socket.on('shell:exit', handleShellExit);
+ socket.on('shell:detached', handleShellDetached);
socket.on('shell:error', handleShellError);
if (socket.connected) {
@@ -455,6 +500,7 @@ export default function Shell() {
socket.off('shell:attached', handleShellAttached);
socket.off('shell:output', handleShellOutput);
socket.off('shell:exit', handleShellExit);
+ socket.off('shell:detached', handleShellDetached);
socket.off('shell:error', handleShellError);
// Don't kill session on unmount — it persists server-side
sessionIdRef.current = null;
diff --git a/server/services/shell.js b/server/services/shell.js
index 6c9da76755..6e2b074a2c 100644
--- a/server/services/shell.js
+++ b/server/services/shell.js
@@ -150,11 +150,20 @@ export function createShellSession(socket, options = {}) {
/**
* Attach an existing session to a new socket
+ *
+ * A shell session has a single attached socket — PTY output is fanned to that one
+ * socket only (see ptyProcess.onData). When a deep link is opened in a second tab,
+ * the new socket takes over and the previous tab would otherwise sit "Connected"
+ * with no output. Emit shell:detached on the prior socket so it can clear its
+ * local state instead of silently losing the stream.
*/
export function attachSession(sessionId, socket) {
const session = shellSessions.get(sessionId);
if (!session) return null;
- // Detach from old socket
+ const prevSocket = session.socket;
+ if (prevSocket && prevSocket !== socket) {
+ prevSocket.emit('shell:detached', { sessionId, reason: 'attached-elsewhere' });
+ }
session.socket = socket;
console.log(`🐚 Attached session ${sessionId.slice(0, 8)} to socket ${socket.id}`);
return {