mirror of
https://github.com/garrytan/gstack.git
synced 2026-06-28 04:30:01 +02:00
b315ccb0d4
The agent side of Commit 3 — the "magic" feature. A network blip (wifi
hiccup, MV3 panel suspend, brief Chromium pause) now silently reconnects
the sidebar to the SAME claude session with scrollback intact. No more
"Session ended" message + manual Restart click + losing your tool-call
output. Server-side /pty-session/reattach (25ef24e9) and the extension
re-attach loop (next commit) close the loop end-to-end.
Ring buffer (T10):
* Per-session frames: Buffer[] capped at 1 MB (env-overridable via
GSTACK_PTY_RING_BUFFER_BYTES). Each PTY write is one frame, so
eviction is at frame boundaries and never cuts a UTF-8 sequence or
ANSI CSI in half.
* appendToRingBuffer eviction loop keeps at least one frame even at
extreme caps — a single oversized frame can't empty the buffer.
* Alt-screen tracking via canonical xterm CSI ?1049h / CSI ?1049l
sequences. lastIndexOf comparison so trailing state wins when both
appear in one render frame (quick tool-call open+close).
Replay payload (T5 — codex outside-voice):
* buildReplayPayload prefixes DECSTR soft reset (\x1b[!p) and
conditionally re-enters alt-screen if claude was in a tool call at
detach. The client writes RIS (\x1bc) FIRST to clear pre-blip xterm
content; the server's prelude resets character attributes; the ring
buffer replays cleanly on top.
* Order is enforced by the {type:"reattach-begin"} text frame the
agent sends right before the binary replay — client waits for it,
writes RIS, then treats the next binary frame as the replay payload.
Detach state machine (T9):
* PtySession.liveWs decouples the PTY callback from the original ws
closure. On re-attach, swapping session.liveWs is enough — the
on-data callback writes to the new ws automatically.
* close(ws, code, _reason): codes 4001 (intentional restart), 4404
(no-claude), and 1000 (clean exit) trigger immediate dispose.
Anything else (1006 abnormal, 1001 going-away from network blip /
panel suspend) starts a 60s detach timer instead. claude keeps
running, output keeps accumulating in the ring buffer.
* Detach timer is unref'd so the bun process can still exit cleanly
on natural shutdown.
* Sessions without a sessionId (legacy single-shot grants) can't
re-attach by definition — those fall through to immediate dispose.
Re-attach lookup (T9):
* WS open() checks sessionsById[sessionId] FIRST. If a detached
session is sitting there, cancel its detach timer, swap liveWs,
rebind the WS-keyed map, restart keepalive, send reattach-begin
+ replay payload. The PTY process is unchanged.
* /internal/restart now cancels any pending detach timer before
disposal — otherwise the timer would later try to dispose an
already-disposed session.
Env knobs for e2e:
* GSTACK_PTY_RING_BUFFER_BYTES — compress to 256 for eviction tests.
* GSTACK_PTY_DETACH_WINDOW_MS — compress to 1000 for "did the timer
fire?" tests without waiting a minute per assertion.
Tests:
* browse/test/terminal-agent-detach-reattach.test.ts — 10 static-grep
tripwires for the load-bearing properties: interface shape, env
knobs, eviction floor, alt-screen tracking, replay prelude
composition, re-attach lookup, close-code routing, detach timer
unref, /internal/restart timer cancellation, on-data through
session.liveWs.
* browse/test/terminal-agent-session-routing.test.ts test 7 widened
to match the new close(ws, code, _reason) signature.
* browse/test/terminal-agent-keepalive.test.ts test 3 widened
similarly. Both stay regressions for the prior contract.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
97 lines
4.5 KiB
TypeScript
97 lines
4.5 KiB
TypeScript
import { describe, test, expect } from 'bun:test';
|
|
import * as fs from 'fs';
|
|
import * as path from 'path';
|
|
|
|
// v1.44 Commit 2 — terminal-agent sessionId routing + eager spawn.
|
|
//
|
|
// Live spawn tests would require a real claude binary on PATH and a Bun.serve
|
|
// listener; both are e2e-tier. These static-grep tripwires defend the load-
|
|
// bearing protocol changes:
|
|
// - validTokens carries the sessionId binding (Map, not Set)
|
|
// - sessionsById index exists for /internal/restart + (Commit 3) re-attach
|
|
// - /internal/restart is scoped to one sessionId (codex T2 fix)
|
|
// - {type:"start"} triggers spawn for eager UX after forceRestart
|
|
// - maybeSpawnPty helper is the single entry point for both spawn paths
|
|
|
|
const AGENT_TS = path.resolve(new URL(import.meta.url).pathname, '..', '..', 'src', 'terminal-agent.ts');
|
|
|
|
describe('terminal-agent session routing (v1.44+ Commit 2)', () => {
|
|
test('1. validTokens is a Map binding token → sessionId', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
// Pre-Commit 2 was `Set<string>`; the Map carries the sessionId
|
|
// binding that /internal/restart and (Commit 3) re-attach depend on.
|
|
expect(src).toMatch(/const validTokens = new Map<string, string \| null>\(\)/);
|
|
expect(src).not.toMatch(/const validTokens = new Set</);
|
|
});
|
|
|
|
test('2. sessionsById reverse index exists', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
expect(src).toMatch(/const sessionsById = new Map<string, PtySession>\(\)/);
|
|
// Populated in open() — required so /internal/restart can find the session.
|
|
expect(src).toMatch(/if \(sessionId\) sessionsById\.set\(sessionId, session\)/);
|
|
});
|
|
|
|
test('3. /internal/grant binds an optional sessionId to the token', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
const block = sliceBetween(src, "url.pathname === '/internal/grant'", "url.pathname === '/internal/revoke'");
|
|
expect(block).toContain('validTokens.set(body.token, sid)');
|
|
expect(block).toContain('body?.sessionId');
|
|
});
|
|
|
|
test('4. /internal/restart is scoped to one sessionId, not dispose-all', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
const block = sliceBetween(src, "url.pathname === '/internal/restart'", "// /claude-available");
|
|
expect(block).toContain('sessionsById.get(sid)');
|
|
expect(block).toContain('disposeSession(session)');
|
|
expect(block).toContain('sessionsById.delete(sid)');
|
|
// Negative: must NOT enumerate all live sessions and dispose them
|
|
// (codex T2 caught this — pre-spec the route killed every PTY on the
|
|
// agent, breaking multi-sidebar / pair-agent setups).
|
|
expect(block).not.toMatch(/for\s*\(\s*const\s+\[?ws/);
|
|
});
|
|
|
|
test('5. WS upgrade surfaces sessionId on ws.data', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
expect(src).toContain('validTokens.get(token) ?? null');
|
|
expect(src).toMatch(/data:\s*\{\s*cookie:\s*token,\s*sessionId\s*\}/);
|
|
});
|
|
|
|
test('6. eager spawn via {type:"start"} text frame', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
expect(src).toMatch(/msg\?\.type === 'start'/);
|
|
// Both spawn paths route through the same helper for parity.
|
|
expect(src).toContain('function maybeSpawnPty(');
|
|
expect(src).toMatch(/maybeSpawnPty\(ws, session\)/);
|
|
});
|
|
|
|
test('7. close() drops sessionsById entry alongside ws cleanup', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
// Commit 3 widened the close signature to `close(ws, code, _reason)`
|
|
// for the detach state machine. Match either shape so test is stable
|
|
// across the rest of the long-lived-sidebar PR.
|
|
const i = src.indexOf('close(ws');
|
|
expect(i).toBeGreaterThan(-1);
|
|
const j = src.indexOf('function handleTabState', i);
|
|
const block = src.slice(i, j);
|
|
expect(block).toContain('sessionsById.delete(session.sessionId)');
|
|
});
|
|
|
|
test('8. PtySession interface carries the sessionId field', () => {
|
|
const src = fs.readFileSync(AGENT_TS, 'utf-8');
|
|
// Whole interface — close paren is sufficient.
|
|
const i = src.indexOf('interface PtySession {');
|
|
expect(i).toBeGreaterThan(-1);
|
|
const j = src.indexOf('\n}', i);
|
|
const block = src.slice(i, j);
|
|
expect(block).toContain('sessionId: string | null');
|
|
});
|
|
});
|
|
|
|
function sliceBetween(source: string, start: string, end: string): string {
|
|
const i = source.indexOf(start);
|
|
if (i === -1) throw new Error(`marker not found: ${start}`);
|
|
const j = source.indexOf(end, i + start.length);
|
|
if (j === -1) throw new Error(`end marker not found: ${end}`);
|
|
return source.slice(i, j);
|
|
}
|