mirror of
https://github.com/Ark0N/Codeman.git
synced 2026-10-06 23:49:41 +02:00
fix(terminal): keep row alignment in the full-history pane replay
Switching to a session left the caret one row below the composer's input line, on the box border, and every cursor-relative update the CLI sent afterwards was measured from the wrong row. Any fresh output repaired it, because the CLI then repainted the whole frame. Two things were wrong with the full-history replay, and they compound. The capture never restored the cursor. The visible-frame path ends with an absolute cursor move back to the pane's position; the linear path returned its text and left the caret wherever the last character landed, which for an agent CLI is the bottom-most row carrying text — the status line. The rows it addressed did not line up with the pane's rows either. Four transforms ran over the capture and each can delete a line: the trailing blank rows were stripped, redraw-bloat stripping ran, the trim that cuts everything above the Claude banner ran, and leading whitespace was removed. All four are right for a byte stream of successive frames. A capture is the rendered pane, one line per screen row, so each deletion shifted the frame out from under the restored cursor. The full-history path now appends the pane's own cursor position and keeps every row, so row N of the reply is row N of the pane. The visible-frame and tail paths are untouched. Restoring the cursor is what makes row alignment load-bearing here, and neither CLAUDE.md nor the architecture invariants said so — which is how four line-deleting transforms accumulated on the path. Both now record it. Verified against a live 315x59 pane: the reply carries 59 rows, its row 55 is the composer's input line matching tmux, and it ends with the cursor move that lands there.
This commit is contained in:
@@ -851,6 +851,33 @@ describe('session-routes', () => {
|
||||
);
|
||||
});
|
||||
|
||||
it('full reload (?full=1) keeps every leading row so the restored cursor lands on the right line', async () => {
|
||||
// The capture ends with an absolute cursor move, so its rows and the pane's
|
||||
// rows must line up one for one. Three transforms used to run over it and
|
||||
// each could delete a leading line: redraw-bloat stripping, the trim that
|
||||
// cuts everything above the Claude banner, and a leading-whitespace strip.
|
||||
// Any one of them shifted the frame up and left the caret a row off.
|
||||
harness.ctx._session.mode = 'claude';
|
||||
harness.ctx._session.terminalBuffer = '';
|
||||
// A blank first row, then the banner — the shape a real pane has.
|
||||
const rendered = ['', '\x1b[1mClaude Code v2.1.266', 'conversation', '\u276f ', '\x1b[4;3H'].join('\r\n');
|
||||
(harness.ctx.mux as { captureActivePaneBuffer?: unknown }).captureActivePaneBuffer = vi.fn(
|
||||
(_name: string, opts?: { fullHistory?: boolean }) => (opts?.fullHistory ? rendered : 'visible frame')
|
||||
);
|
||||
|
||||
const res = await harness.app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/sessions/${harness.ctx._sessionId}/terminal?full=1`,
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const body = JSON.parse(res.body);
|
||||
expect(body.data.source).toBe('mux-full-history');
|
||||
// The blank first row survives, so row N of the reply is row N of the pane.
|
||||
expect(body.data.terminalBuffer.startsWith('\r\n')).toBe(true);
|
||||
expect(body.data.terminalBuffer.split('\r\n')).toHaveLength(rendered.split('\r\n').length);
|
||||
});
|
||||
|
||||
it('full reload (?full=1) falls back to the byte history when the capture is unavailable', async () => {
|
||||
harness.ctx._session.mode = 'claude';
|
||||
harness.ctx._session.terminalBuffer = 'byte history survives';
|
||||
|
||||
@@ -15,7 +15,7 @@ import { describe, expect, it } from 'vitest';
|
||||
describe('tmux full-history pane capture (COD-47)', () => {
|
||||
const source = readFileSync(resolve(import.meta.dirname, '../src/tmux-manager.ts'), 'utf8');
|
||||
const methodStart = source.indexOf('capturePaneBuffer(muxName: string');
|
||||
const methodBody = source.slice(methodStart, methodStart + 4000);
|
||||
const methodBody = source.slice(methodStart, methodStart + 6500);
|
||||
|
||||
it('capturePaneBuffer accepts pane-capture options with a fullHistory flag', () => {
|
||||
expect(methodStart).toBeGreaterThan(-1);
|
||||
@@ -42,13 +42,32 @@ describe('tmux full-history pane capture (COD-47)', () => {
|
||||
});
|
||||
|
||||
it('returns full-history capture as raw scrollback (skips the single-screen repaint)', () => {
|
||||
// When fullHistory, return the raw buffer BEFORE the formatPaneSnapshot
|
||||
// repaint (which is single-screen and would clip a multi-screen history).
|
||||
const earlyReturn = methodBody.indexOf('return normalizeScrollbackEol(buffer);');
|
||||
// When fullHistory, return the normalized buffer BEFORE the
|
||||
// formatPaneSnapshot repaint (which is single-screen and would clip a
|
||||
// multi-screen history).
|
||||
const normalize = methodBody.indexOf('normalizeScrollbackEol(buffer)');
|
||||
const snapshot = methodBody.indexOf('formatPaneSnapshot(');
|
||||
expect(earlyReturn).toBeGreaterThan(-1);
|
||||
expect(normalize).toBeGreaterThan(-1);
|
||||
expect(snapshot).toBeGreaterThan(-1);
|
||||
expect(earlyReturn).toBeLessThan(snapshot);
|
||||
expect(normalize).toBeLessThan(snapshot);
|
||||
});
|
||||
|
||||
it('appends the pane cursor to the full-history capture', () => {
|
||||
// A linear replay leaves the caret wherever the last character landed — the
|
||||
// status line, for an agent CLI — and every cursor-relative update the CLI
|
||||
// sends afterwards is then measured from the wrong row.
|
||||
const restore = methodBody.indexOf('return `${normalized}');
|
||||
const snapshot = methodBody.indexOf('formatPaneSnapshot(');
|
||||
expect(restore).toBeGreaterThan(-1);
|
||||
expect(restore).toBeLessThan(snapshot);
|
||||
expect(methodBody).toContain('cursorY + 1};${cursorX + 1}H');
|
||||
});
|
||||
|
||||
it('keeps the trailing rows of a full-history capture', () => {
|
||||
// The visible path drops trailing blank rows because it repaints each row
|
||||
// absolutely afterwards. Dropping them on the linear path would move the
|
||||
// frame up and leave the restored cursor pointing at the wrong line.
|
||||
expect(methodBody).toContain("fullHistory ? rawCapture.replace(/\\n$/, '')");
|
||||
});
|
||||
|
||||
it('captureActivePaneBuffer forwards the capture options', () => {
|
||||
|
||||
Reference in New Issue
Block a user