mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-27 11:56:20 +00:00
* feat(tabs): move a tab to its own window without reconnecting (#4201) Adds "Move Tab to New Window" to the tab context menu. Instead of opening a new connection like "Copy Tab to New Window", the tab is hidden from the tab bar and an attach popup takes over the same live PTY, so bastion sessions are not re-authenticated and no extra seat is used. Closing the popup returns the tab (with its screen state) to the window it came from. - popup lifecycle notifies the home window via terminal-popup:attach-closed - new runtime poppedOut flag; popped-out tabs still persist for restore and are not labelled as AI silent sessions in the tray * fix(tabs): keep moved tabs alive when their source window closes - A moved-tab popup registers as the lifetime heir of its session. If the source window is destroyed, ownership passes to the popup instead of the session being closed (worker and in-process paths). AI observe popups are unchanged. - If the source window is gone when the popup closes, skip the hand-back so the popup's ownership closes the session. - Only offer/allow Move Tab to New Window for connected sessions; attach mode cannot connect or reconnect a backend. * fix(tabs): expose Move Tab to New Window through TerminalHost The handler was registered but not passed into the terminal domain bag, so AppView received undefined and the menu item never rendered. * fix(tabs): address move-to-window focus, crash ownership, live count, history, and AI context review findings * fix(tabs): activate moved-tab lifetime heir only after output rebind succeeds * fix(tabs): wait for popup attachment before hiding tab and exclude plugin sessions * fix(tabs): confirm renderer reattach before resolving move and await output restore before unhiding tab * fix(tabs): preserve detached output across popup crashes and avoid focusing main window from tray * fix(tabs): deliver attach snapshots and output restore directly to peer session windows * fix(tabs): schedule automatic output restore retries when popup recovery fails * fix(tabs): handle reopen replay preservation, stale restore cancellation, startup workspace move, and shutdown log capture * fix(tabs): reassert popped-out state after tray reopen * test(terminal): provide log finalizer stubs in popup snapshot harness --------- Co-authored-by: bincxz <16399091+binaricat@users.noreply.github.com>
353 lines
11 KiB
JavaScript
353 lines
11 KiB
JavaScript
"use strict";
|
|
|
|
/**
|
|
* Tiny registry so the terminal popup window can restore output routing after
|
|
* an AI observe/attach popup is closed or destroyed, without creating a
|
|
* windowManager <-> terminalBridge require cycle.
|
|
*/
|
|
|
|
let restoreImpl = null;
|
|
let attachHomeLookup = null;
|
|
let fanoutExitImpl = null;
|
|
const attachPopupAuthorizations = new Map();
|
|
const pendingAttachRestores = new Set();
|
|
const pendingAttachRestoreListeners = new Map();
|
|
const detachedReplayBuffers = new Map();
|
|
const MAX_DETACHED_REPLAY_CHARS = 512 * 1024;
|
|
|
|
function startDetachedReplayCapture(sessionId) {
|
|
if (!sessionId) return;
|
|
// Preserve any retained replay data from an unrecovered crash so reopening
|
|
// the session does not drop output missing from the home terminal.
|
|
if (!detachedReplayBuffers.has(sessionId)) {
|
|
detachedReplayBuffers.set(sessionId, { chunks: [], length: 0 });
|
|
}
|
|
}
|
|
|
|
function peekDetachedReplayData(sessionId) {
|
|
if (!sessionId) return "";
|
|
const entry = detachedReplayBuffers.get(sessionId);
|
|
if (!entry || entry.chunks.length === 0) return "";
|
|
return entry.chunks.join("");
|
|
}
|
|
|
|
function appendDetachedReplayData(sessionId, data) {
|
|
if (!sessionId || typeof data !== "string" || data.length === 0) return;
|
|
const entry = detachedReplayBuffers.get(sessionId);
|
|
if (!entry) return;
|
|
if (data.length >= MAX_DETACHED_REPLAY_CHARS) {
|
|
const tail = data.slice(-MAX_DETACHED_REPLAY_CHARS);
|
|
entry.chunks = [tail];
|
|
entry.length = tail.length;
|
|
return;
|
|
}
|
|
entry.chunks.push(data);
|
|
entry.length += data.length;
|
|
while (entry.length > MAX_DETACHED_REPLAY_CHARS && entry.chunks.length > 0) {
|
|
const overflow = entry.length - MAX_DETACHED_REPLAY_CHARS;
|
|
const head = entry.chunks[0];
|
|
if (head.length <= overflow) {
|
|
entry.chunks.shift();
|
|
entry.length -= head.length;
|
|
} else {
|
|
entry.chunks[0] = head.slice(overflow);
|
|
entry.length -= overflow;
|
|
}
|
|
}
|
|
}
|
|
|
|
function takeDetachedReplayData(sessionId) {
|
|
if (!sessionId) return "";
|
|
const entry = detachedReplayBuffers.get(sessionId);
|
|
if (!entry) return "";
|
|
detachedReplayBuffers.delete(sessionId);
|
|
return entry.chunks.join("");
|
|
}
|
|
|
|
function clearDetachedReplayCapture(sessionId) {
|
|
if (sessionId) detachedReplayBuffers.delete(sessionId);
|
|
}
|
|
|
|
function notifyAttachRestoreListeners(sessionId) {
|
|
const listeners = pendingAttachRestoreListeners.get(sessionId);
|
|
if (!listeners) return;
|
|
pendingAttachRestoreListeners.delete(sessionId);
|
|
for (const listener of listeners) {
|
|
try {
|
|
listener();
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
}
|
|
|
|
function onAttachOutputRestored(sessionId, listener) {
|
|
if (!sessionId || typeof listener !== "function") return () => {};
|
|
let listeners = pendingAttachRestoreListeners.get(sessionId);
|
|
if (!listeners) {
|
|
listeners = new Set();
|
|
pendingAttachRestoreListeners.set(sessionId, listeners);
|
|
}
|
|
listeners.add(listener);
|
|
return () => {
|
|
const current = pendingAttachRestoreListeners.get(sessionId);
|
|
if (!current) return;
|
|
current.delete(listener);
|
|
if (current.size === 0) pendingAttachRestoreListeners.delete(sessionId);
|
|
};
|
|
}
|
|
|
|
function settleAttachPopupOutcome(grant, outcome) {
|
|
if (!grant || grant.outcomeSettled) return;
|
|
grant.outcomeSettled = true;
|
|
try {
|
|
grant.resolveOutcome?.(outcome);
|
|
} catch {
|
|
// ignore
|
|
}
|
|
}
|
|
|
|
function clearAttachRestoreListeners(sessionId) {
|
|
if (sessionId) pendingAttachRestoreListeners.delete(sessionId);
|
|
}
|
|
|
|
function registerAttachPopupAuthorization(token, sessionId, webContentsId, options = {}) {
|
|
if (!token || !sessionId || typeof webContentsId !== "number") return false;
|
|
// A new attachment supersedes any stale pending-restore listeners from an earlier popup.
|
|
clearAttachRestoreListeners(sessionId);
|
|
let resolveOutcome = null;
|
|
const outcomePromise = new Promise((resolve) => {
|
|
resolveOutcome = resolve;
|
|
});
|
|
attachPopupAuthorizations.set(token, {
|
|
sessionId,
|
|
webContentsId,
|
|
closePrepared: false,
|
|
// A user tab moved into the popup: becomes the lifetime heir only once
|
|
// output has been rebound to the popup so closing the source mid-startup
|
|
// does not leave a live PTY stranded behind a failed attachment.
|
|
ownsLifetime: options?.ownsLifetime === true,
|
|
attached: false,
|
|
outcomeSettled: false,
|
|
outcomePromise,
|
|
resolveOutcome,
|
|
});
|
|
return true;
|
|
}
|
|
|
|
function markAttachPopupAttached(token, sessionId, webContentsId) {
|
|
if (!validateAttachPopupAuthorization(token, sessionId, webContentsId)) return false;
|
|
const grant = attachPopupAuthorizations.get(token);
|
|
grant.attached = true;
|
|
clearAttachRestoreListeners(sessionId);
|
|
startDetachedReplayCapture(sessionId);
|
|
return true;
|
|
}
|
|
|
|
function markAttachPopupReady(token, sessionId, webContentsId) {
|
|
if (!validateAttachPopupAuthorization(token, sessionId, webContentsId)) return false;
|
|
const grant = attachPopupAuthorizations.get(token);
|
|
if (!grant.attached) return false;
|
|
settleAttachPopupOutcome(grant, { success: true });
|
|
return true;
|
|
}
|
|
|
|
function failAttachPopupAuthorization(token, sessionId, webContentsId, error) {
|
|
if (!validateAttachPopupAuthorization(token, sessionId, webContentsId)) return false;
|
|
const grant = attachPopupAuthorizations.get(token);
|
|
settleAttachPopupOutcome(grant, {
|
|
success: false,
|
|
error: typeof error === "string" && error.trim() ? error.trim() : "Failed to attach terminal popup",
|
|
});
|
|
return true;
|
|
}
|
|
|
|
function isAttachPopupAttached(token) {
|
|
return attachPopupAuthorizations.get(token)?.attached === true;
|
|
}
|
|
|
|
function waitForAttachPopupOutcome(token, timeoutMs = 10000) {
|
|
const grant = token ? attachPopupAuthorizations.get(token) : null;
|
|
if (!grant) {
|
|
return Promise.resolve({ success: false, error: "Attach popup closed before attaching" });
|
|
}
|
|
return new Promise((resolve) => {
|
|
let done = false;
|
|
const finish = (outcome) => {
|
|
if (done) return;
|
|
done = true;
|
|
clearTimeout(timer);
|
|
resolve(outcome);
|
|
};
|
|
const timer = setTimeout(() => {
|
|
const failure = { success: false, error: "Timed out waiting for terminal popup to attach" };
|
|
settleAttachPopupOutcome(grant, failure);
|
|
finish(failure);
|
|
}, timeoutMs);
|
|
void grant.outcomePromise.then(finish);
|
|
});
|
|
}
|
|
|
|
function clearAttachPopupAttached(token, sessionId, webContentsId) {
|
|
if (!validateAttachPopupAuthorization(token, sessionId, webContentsId)) return false;
|
|
attachPopupAuthorizations.get(token).attached = false;
|
|
return true;
|
|
}
|
|
|
|
/** webContentsId of an attached moved-tab popup that should inherit sessionId if its home dies. */
|
|
function getLifetimeHeirWebContentsId(sessionId) {
|
|
if (!sessionId) return null;
|
|
for (const grant of attachPopupAuthorizations.values()) {
|
|
if (grant.ownsLifetime && grant.attached && grant.sessionId === sessionId) return grant.webContentsId;
|
|
}
|
|
return null;
|
|
}
|
|
|
|
function validateAttachPopupAuthorization(token, sessionId, webContentsId) {
|
|
if (!token || !sessionId || typeof webContentsId !== "number") return false;
|
|
const grant = attachPopupAuthorizations.get(token);
|
|
return Boolean(
|
|
grant
|
|
&& grant.sessionId === sessionId
|
|
&& grant.webContentsId === webContentsId,
|
|
);
|
|
}
|
|
|
|
function markAttachPopupClosePrepared(token, sessionId, webContentsId) {
|
|
if (!validateAttachPopupAuthorization(token, sessionId, webContentsId)) return false;
|
|
attachPopupAuthorizations.get(token).closePrepared = true;
|
|
return true;
|
|
}
|
|
|
|
function isAttachPopupClosePrepared(token) {
|
|
return attachPopupAuthorizations.get(token)?.closePrepared === true;
|
|
}
|
|
|
|
function releaseAttachPopupAuthorization(token) {
|
|
if (!token) return;
|
|
const grant = attachPopupAuthorizations.get(token);
|
|
if (grant) {
|
|
settleAttachPopupOutcome(grant, { success: false, error: "Attach popup closed before attaching" });
|
|
}
|
|
attachPopupAuthorizations.delete(token);
|
|
}
|
|
|
|
function releaseAttachedSessionState(sessionId) {
|
|
if (!sessionId) return;
|
|
pendingAttachRestores.delete(sessionId);
|
|
clearDetachedReplayCapture(sessionId);
|
|
notifyAttachRestoreListeners(sessionId);
|
|
for (const [token, grant] of attachPopupAuthorizations) {
|
|
if (grant?.sessionId === sessionId) {
|
|
settleAttachPopupOutcome(grant, { success: false, error: "Session closed before popup attached" });
|
|
attachPopupAuthorizations.delete(token);
|
|
}
|
|
}
|
|
}
|
|
|
|
function setRestoreAttachedSessionOutput(fn) {
|
|
restoreImpl = typeof fn === "function" ? fn : null;
|
|
}
|
|
|
|
async function restoreAttachedSessionOutput(sessionId, preferredHomeWebContentsId = null) {
|
|
if (!sessionId || typeof restoreImpl !== "function") {
|
|
return { success: false, restored: false, unavailable: true };
|
|
}
|
|
try {
|
|
const result = await restoreImpl(sessionId, preferredHomeWebContentsId)
|
|
|| { success: true, restored: false };
|
|
if (result.success) {
|
|
pendingAttachRestores.delete(sessionId);
|
|
notifyAttachRestoreListeners(sessionId);
|
|
} else {
|
|
pendingAttachRestores.add(sessionId);
|
|
}
|
|
return result;
|
|
} catch (err) {
|
|
pendingAttachRestores.add(sessionId);
|
|
return { success: false, restored: false, error: err?.message || String(err) };
|
|
}
|
|
}
|
|
|
|
async function retryPendingAttachedSessionOutputs() {
|
|
await Promise.all(Array.from(pendingAttachRestores, (sessionId) => (
|
|
restoreAttachedSessionOutput(sessionId)
|
|
)));
|
|
}
|
|
|
|
async function retryPendingAttachedSessionOutput(sessionId, preferredHomeWebContentsId = null) {
|
|
if (!pendingAttachRestores.has(sessionId)) return { success: true, restored: false };
|
|
return await restoreAttachedSessionOutput(sessionId, preferredHomeWebContentsId);
|
|
}
|
|
|
|
function setAttachHomeLookup(fn) {
|
|
attachHomeLookup = typeof fn === "function" ? fn : null;
|
|
}
|
|
|
|
function getAttachHomeWebContentsId(sessionId) {
|
|
if (!sessionId || typeof attachHomeLookup !== "function") return null;
|
|
try {
|
|
const id = attachHomeLookup(sessionId);
|
|
return typeof id === "number" ? id : null;
|
|
} catch {
|
|
return null;
|
|
}
|
|
}
|
|
|
|
function setFanoutSessionExit(fn) {
|
|
fanoutExitImpl = typeof fn === "function" ? fn : null;
|
|
}
|
|
|
|
function fanoutSessionExit(sessionId, primaryContents, payload) {
|
|
const primaryWebContentsId = typeof primaryContents === "number"
|
|
? primaryContents
|
|
: primaryContents?.id;
|
|
if (typeof fanoutExitImpl === "function") {
|
|
try {
|
|
fanoutExitImpl(sessionId, primaryWebContentsId, payload);
|
|
return true;
|
|
} catch {
|
|
// fall through
|
|
}
|
|
}
|
|
// Session implementations can run before terminalBridge wires the shared
|
|
// registry. Preserve the original primary-renderer notification then.
|
|
try {
|
|
primaryContents?.send?.("netcatty:exit", payload);
|
|
return typeof primaryContents?.send === "function";
|
|
} catch {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
module.exports = {
|
|
registerAttachPopupAuthorization,
|
|
validateAttachPopupAuthorization,
|
|
markAttachPopupAttached,
|
|
markAttachPopupReady,
|
|
failAttachPopupAuthorization,
|
|
isAttachPopupAttached,
|
|
waitForAttachPopupOutcome,
|
|
clearAttachPopupAttached,
|
|
onAttachOutputRestored,
|
|
clearAttachRestoreListeners,
|
|
startDetachedReplayCapture,
|
|
appendDetachedReplayData,
|
|
peekDetachedReplayData,
|
|
takeDetachedReplayData,
|
|
clearDetachedReplayCapture,
|
|
markAttachPopupClosePrepared,
|
|
isAttachPopupClosePrepared,
|
|
getLifetimeHeirWebContentsId,
|
|
releaseAttachPopupAuthorization,
|
|
releaseAttachedSessionState,
|
|
setRestoreAttachedSessionOutput,
|
|
restoreAttachedSessionOutput,
|
|
retryPendingAttachedSessionOutputs,
|
|
retryPendingAttachedSessionOutput,
|
|
setAttachHomeLookup,
|
|
getAttachHomeWebContentsId,
|
|
setFanoutSessionExit,
|
|
fanoutSessionExit,
|
|
};
|