fix: terminal reconnect uses federation connect path for remote sessions

The reconnect logic in connectWebSocket() always called local
/api/sessions/{name}/connect even for remote sessions, causing 404.
Fix: check remoteId (captured from outer scope) and route through
/api/federation/{remoteId}/connect/{name} for remote sessions.
This commit is contained in:
Brian Krabach
2026-04-06 18:18:21 -07:00
parent add9e03718
commit ad3c961770
2 changed files with 32 additions and 1 deletions
+9 -1
View File
@@ -165,7 +165,15 @@ function connectWebSocket(name, remoteId) {
// response (plus an 800ms settle delay for ttyd to bind its port). The early return
// prevents falling through to the direct _connectWebSocket() call below.
if (_reconnectAttempts >= 2 && _currentSession) {
fetch('/api/sessions/' + encodeURIComponent(_currentSession) + '/connect', {
var connectPath;
if (remoteId) {
// Remote session: route through federation proxy
connectPath = '/api/federation/' + encodeURIComponent(remoteId) + '/connect/' + encodeURIComponent(_currentSession);
} else {
// Local session
connectPath = '/api/sessions/' + encodeURIComponent(_currentSession) + '/connect';
}
fetch(connectPath, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
})
+23
View File
@@ -1012,4 +1012,27 @@ test('terminal.js Ctrl+Shift+V handler calls preventDefault to suppress double p
);
});
// --- Federation reconnect routing ---
test('terminal.js reconnect uses federation connect path for remote sessions', () => {
// Regression: connect() inside connectWebSocket() always called local
// /api/sessions/{name}/connect even when remoteId was set, causing 404 for remote sessions.
const source = fs.readFileSync(new URL('../terminal.js', import.meta.url), 'utf8');
// Find the connect() function inside connectWebSocket
const connectFnIdx = source.indexOf('function connect()');
assert.ok(connectFnIdx !== -1, 'must have a connect() function inside connectWebSocket');
// Extract enough chars to cover the full reconnect block (incl. long comment preamble)
const connectFn = source.substring(connectFnIdx, connectFnIdx + 1000);
assert.ok(
connectFn.includes('remoteId'),
'reconnect connect() must check remoteId to choose federation vs local routing',
);
assert.ok(
connectFn.includes('/api/federation/'),
'reconnect connect() must use /api/federation/{remoteId}/connect/{name} for remote sessions',
);
});