@causyne/agent 1.20.105 → 1.20.107
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/tui/resizeRecovery.js +6 -3
- package/src/tui/resizeRecovery.ts +5 -3
- package/src/tui/sessionSidebar.js +15 -5
- package/src/tui/sessionSidebar.ts +13 -5
- package/src/tui/stage.js +272 -265
- package/src/tui/stage.ts +360 -0
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
/** A temporary small terminal must not permanently discard the live surface. */
|
|
2
2
|
export function settleStageResize(current, suspended) {
|
|
3
3
|
if (current) {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
if (current.resize())
|
|
5
|
+
return { current, suspended: null, restored: false };
|
|
6
|
+
// A surface may close without updating its old ceiling. Do not reopen it
|
|
7
|
+
// using stale geometry while the terminal remains below the minimum.
|
|
8
|
+
current.stage.maxHeight = 0;
|
|
9
|
+
return { current: null, suspended: current, restored: false };
|
|
7
10
|
}
|
|
8
11
|
if (suspended) {
|
|
9
12
|
suspended.resize();
|
|
@@ -13,9 +13,11 @@ export interface ResizeState<T extends ResizableStage> {
|
|
|
13
13
|
/** A temporary small terminal must not permanently discard the live surface. */
|
|
14
14
|
export function settleStageResize<T extends ResizableStage>(current: T | null, suspended: T | null): ResizeState<T> {
|
|
15
15
|
if (current) {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
16
|
+
if (current.resize()) return { current, suspended: null, restored: false };
|
|
17
|
+
// A surface may close without updating its old ceiling. Do not reopen it
|
|
18
|
+
// using stale geometry while the terminal remains below the minimum.
|
|
19
|
+
current.stage.maxHeight = 0;
|
|
20
|
+
return { current: null, suspended: current, restored: false };
|
|
19
21
|
}
|
|
20
22
|
if (suspended) {
|
|
21
23
|
suspended.resize();
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { padEnd, safeDisplay, truncate } from './text.js';
|
|
1
|
+
import { padEnd, safeDisplay, stripAnsi, truncate } from './text.js';
|
|
2
2
|
// The terminal theme is still a standalone JavaScript module in the TS migration.
|
|
3
3
|
// @ts-expect-error theme.js has no TypeScript declaration yet.
|
|
4
4
|
import { bold, dim, fill, paint } from './theme.js';
|
|
@@ -32,6 +32,7 @@ export function sessionComposerWidth(columns) {
|
|
|
32
32
|
}
|
|
33
33
|
/** Render only facts the runtime can actually report. */
|
|
34
34
|
export function sessionSidebarRows(facts, height) {
|
|
35
|
+
const count = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1;
|
|
35
36
|
const inner = SIDEBAR_WIDTH - 4;
|
|
36
37
|
const rows = [];
|
|
37
38
|
const line = (value = '') => {
|
|
@@ -84,14 +85,24 @@ export function sessionSidebarRows(facts, height) {
|
|
|
84
85
|
}
|
|
85
86
|
if (facts.plan?.length) {
|
|
86
87
|
const completed = facts.plan.filter(item => item.status === 'completed').length;
|
|
87
|
-
|
|
88
|
+
// Budget from the actual metadata, including optional skill/model rows.
|
|
89
|
+
// Preserve space for the focused step and overflow summaries on short panes.
|
|
90
|
+
const planBudget = Math.min(6, Math.max(0, count - 3));
|
|
91
|
+
while (rows.length > count - planBudget) {
|
|
92
|
+
const blank = rows.findIndex(row => stripAnsi(row).replace('│', '').trim() === '');
|
|
93
|
+
if (blank < 0)
|
|
94
|
+
break;
|
|
95
|
+
rows.splice(blank, 1);
|
|
96
|
+
}
|
|
97
|
+
if (rows.length > count - planBudget)
|
|
98
|
+
rows.length = Math.max(0, count - planBudget);
|
|
88
99
|
styled(bold(`Plan ${completed}/${facts.plan.length}`));
|
|
89
100
|
// Keep plan steps in execution order and keep the active step in view.
|
|
90
|
-
const maxSteps = Math.max(
|
|
101
|
+
const maxSteps = Math.max(1, Math.min(10, count - rows.length - 3));
|
|
91
102
|
const activeIndex = facts.plan.findIndex(item => item.status === 'in_progress');
|
|
92
103
|
const pendingIndex = facts.plan.findIndex(item => item.status !== 'completed');
|
|
93
104
|
const focus = activeIndex >= 0 ? activeIndex : pendingIndex >= 0 ? pendingIndex : facts.plan.length - 1;
|
|
94
|
-
const start = Math.max(0, Math.min(focus - 2, facts.plan.length - maxSteps));
|
|
105
|
+
const start = Math.max(0, Math.min(focus - Math.min(2, maxSteps - 1), facts.plan.length - maxSteps));
|
|
95
106
|
const planItems = facts.plan.slice(start, start + maxSteps);
|
|
96
107
|
if (start > 0)
|
|
97
108
|
line(`${start} earlier plan steps`);
|
|
@@ -103,7 +114,6 @@ export function sessionSidebarRows(facts, height) {
|
|
|
103
114
|
if (remaining > 0)
|
|
104
115
|
line(`${remaining} later plan steps`);
|
|
105
116
|
}
|
|
106
|
-
const count = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1;
|
|
107
117
|
const visible = rows.slice(0, count);
|
|
108
118
|
while (visible.length < count - 1)
|
|
109
119
|
lineFor(visible);
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { padEnd, safeDisplay, truncate } from './text.js';
|
|
1
|
+
import { padEnd, safeDisplay, stripAnsi, truncate } from './text.js';
|
|
2
2
|
// The terminal theme is still a standalone JavaScript module in the TS migration.
|
|
3
3
|
// @ts-expect-error theme.js has no TypeScript declaration yet.
|
|
4
4
|
import { bold, dim, fill, paint } from './theme.js';
|
|
@@ -48,6 +48,7 @@ export function sessionComposerWidth(columns: number): number {
|
|
|
48
48
|
|
|
49
49
|
/** Render only facts the runtime can actually report. */
|
|
50
50
|
export function sessionSidebarRows(facts: SidebarFacts, height: number): string[] {
|
|
51
|
+
const count = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1;
|
|
51
52
|
const inner = SIDEBAR_WIDTH - 4;
|
|
52
53
|
const rows: string[] = [];
|
|
53
54
|
const line = (value = ''): void => {
|
|
@@ -100,14 +101,22 @@ export function sessionSidebarRows(facts: SidebarFacts, height: number): string[
|
|
|
100
101
|
|
|
101
102
|
if (facts.plan?.length) {
|
|
102
103
|
const completed = facts.plan.filter(item => item.status === 'completed').length;
|
|
103
|
-
|
|
104
|
+
// Budget from the actual metadata, including optional skill/model rows.
|
|
105
|
+
// Preserve space for the focused step and overflow summaries on short panes.
|
|
106
|
+
const planBudget = Math.min(6, Math.max(0, count - 3));
|
|
107
|
+
while (rows.length > count - planBudget) {
|
|
108
|
+
const blank = rows.findIndex(row => stripAnsi(row).replace('│', '').trim() === '');
|
|
109
|
+
if (blank < 0) break;
|
|
110
|
+
rows.splice(blank, 1);
|
|
111
|
+
}
|
|
112
|
+
if (rows.length > count - planBudget) rows.length = Math.max(0, count - planBudget);
|
|
104
113
|
styled(bold(`Plan ${completed}/${facts.plan.length}`));
|
|
105
114
|
// Keep plan steps in execution order and keep the active step in view.
|
|
106
|
-
const maxSteps = Math.max(
|
|
115
|
+
const maxSteps = Math.max(1, Math.min(10, count - rows.length - 3));
|
|
107
116
|
const activeIndex = facts.plan.findIndex(item => item.status === 'in_progress');
|
|
108
117
|
const pendingIndex = facts.plan.findIndex(item => item.status !== 'completed');
|
|
109
118
|
const focus = activeIndex >= 0 ? activeIndex : pendingIndex >= 0 ? pendingIndex : facts.plan.length - 1;
|
|
110
|
-
const start = Math.max(0, Math.min(focus - 2, facts.plan.length - maxSteps));
|
|
119
|
+
const start = Math.max(0, Math.min(focus - Math.min(2, maxSteps - 1), facts.plan.length - maxSteps));
|
|
111
120
|
const planItems = facts.plan.slice(start, start + maxSteps);
|
|
112
121
|
if (start > 0) line(`${start} earlier plan steps`);
|
|
113
122
|
for (const item of planItems) {
|
|
@@ -118,7 +127,6 @@ export function sessionSidebarRows(facts: SidebarFacts, height: number): string[
|
|
|
118
127
|
if (remaining > 0) line(`${remaining} later plan steps`);
|
|
119
128
|
}
|
|
120
129
|
|
|
121
|
-
const count = Number.isFinite(height) ? Math.max(1, Math.floor(height)) : 1;
|
|
122
130
|
const visible = rows.slice(0, count);
|
|
123
131
|
while (visible.length < count - 1) lineFor(visible);
|
|
124
132
|
if (visible.length < count) visible.push(fill('surface', `${paint('rule', '│')} ${padEnd(dim('Causyne Agent'), inner)} `));
|
package/src/tui/stage.js
CHANGED
|
@@ -31,15 +31,12 @@
|
|
|
31
31
|
// user-driven rate. Both are cheap at 40x200 in pure JS.
|
|
32
32
|
//
|
|
33
33
|
// Pure Node: no dependency, no shell.
|
|
34
|
-
|
|
35
34
|
import { visibleWidth, truncate } from './text.js';
|
|
36
35
|
import { runWithStageWrite } from './consoleGuard.js';
|
|
37
|
-
|
|
38
36
|
// Below this the band cannot hold a viewport plus an input box plus a footer,
|
|
39
37
|
// and a cramped stage is worse than honest scrollback. The fallback is not a
|
|
40
38
|
// degraded stage; it is the scrollback renderer, unchanged.
|
|
41
39
|
export const MIN_STAGE_ROWS = 16;
|
|
42
|
-
|
|
43
40
|
// Synchronized output. A terminal that does not implement it ignores both
|
|
44
41
|
// halves, so this is free; one that does composites the whole repaint at once,
|
|
45
42
|
// which is what stops a scroll from tearing.
|
|
@@ -51,33 +48,32 @@ const HIDE_CURSOR = '\x1b[?25l';
|
|
|
51
48
|
const SHOW_CURSOR = '\x1b[?25h';
|
|
52
49
|
const ERASE_LINE = '\x1b[K';
|
|
53
50
|
const ERASE_BELOW = '\x1b[J';
|
|
54
|
-
|
|
55
51
|
/**
|
|
56
52
|
* How tall the band should be, or 0 for "do not use a stage".
|
|
57
53
|
*
|
|
58
54
|
* CAUSYNE_STAGE: off | auto | <rows> | <pct>%
|
|
59
55
|
*/
|
|
60
56
|
export function stageHeight(out = process.stdout, env = process.env) {
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
57
|
+
const setting = String(env.CAUSYNE_STAGE ?? 'auto').trim().toLowerCase();
|
|
58
|
+
if (setting === 'off' || setting === '0' || setting === 'false')
|
|
59
|
+
return 0;
|
|
60
|
+
const rows = Number(out && out.rows);
|
|
61
|
+
if (!Number.isFinite(rows) || rows <= 0)
|
|
62
|
+
return 0;
|
|
63
|
+
// The seam: never take the last row of the user's screen. It costs one row
|
|
64
|
+
// and it is the difference between "a region" and "the screen".
|
|
65
|
+
const ceiling = rows - 1;
|
|
66
|
+
let target = ceiling;
|
|
67
|
+
const percent = /^(\d{1,3})%$/.exec(setting);
|
|
68
|
+
if (percent)
|
|
69
|
+
target = Math.round((rows * Number(percent[1])) / 100);
|
|
70
|
+
else if (/^\d+$/.test(setting))
|
|
71
|
+
target = Number(setting);
|
|
72
|
+
target = Math.min(target, ceiling);
|
|
73
|
+
if (!Number.isFinite(target) || target < MIN_STAGE_ROWS)
|
|
74
|
+
return 0;
|
|
75
|
+
return target;
|
|
79
76
|
}
|
|
80
|
-
|
|
81
77
|
/**
|
|
82
78
|
* May we run a stage on this stream at all?
|
|
83
79
|
*
|
|
@@ -85,253 +81,264 @@ export function stageHeight(out = process.stdout, env = process.env) {
|
|
|
85
81
|
* answer is the scrollback renderer rather than a stage that misbehaves.
|
|
86
82
|
*/
|
|
87
83
|
export function stageEnabled(out = process.stdout, env = process.env) {
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
84
|
+
if (!out || !out.isTTY)
|
|
85
|
+
return false;
|
|
86
|
+
if (String(env.TERM || '') === 'dumb')
|
|
87
|
+
return false;
|
|
88
|
+
if (env.CAUSYNE_SIMPLE_INPUT === '1')
|
|
89
|
+
return false;
|
|
90
|
+
return stageHeight(out, env) > 0;
|
|
92
91
|
}
|
|
93
|
-
|
|
94
92
|
export class Stage {
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
/** Rows available to the caller for content, i.e. the whole band. */
|
|
130
|
-
get rows() {
|
|
131
|
-
return this.height;
|
|
132
|
-
}
|
|
133
|
-
|
|
134
|
-
write(text) {
|
|
135
|
-
if (!text) return;
|
|
136
|
-
this.stats.bytes += text.length;
|
|
137
|
-
if (typeof runWithStageWrite === 'function') {
|
|
138
|
-
runWithStageWrite(() => this.out.write(text));
|
|
139
|
-
} else {
|
|
140
|
-
this.out.write(text);
|
|
93
|
+
out;
|
|
94
|
+
env;
|
|
95
|
+
maxHeight;
|
|
96
|
+
height;
|
|
97
|
+
reserved;
|
|
98
|
+
cursor;
|
|
99
|
+
prev;
|
|
100
|
+
caret;
|
|
101
|
+
stats;
|
|
102
|
+
constructor({ out = process.stdout, env = process.env, height = 0 } = {}) {
|
|
103
|
+
this.out = out;
|
|
104
|
+
this.env = env;
|
|
105
|
+
// The CEILING, not the size. A band claimed by printing newlines costs the
|
|
106
|
+
// user a real row of terminal for every row it takes, so it takes only what
|
|
107
|
+
// it has something to put in and grows from there. Sizing it to the screen
|
|
108
|
+
// is free on an alternate screen — which is what opencode is — and on a
|
|
109
|
+
// reserved band it is 73 rows of void and a banner scrolled out of sight.
|
|
110
|
+
this.maxHeight = height > 0 ? Math.floor(height) : stageHeight(out, env);
|
|
111
|
+
// Rows actually claimed. Zero until reserve().
|
|
112
|
+
this.height = 0;
|
|
113
|
+
this.reserved = false;
|
|
114
|
+
// Where the caret is, as a row index inside the band. reserve() leaves it
|
|
115
|
+
// on the band's last row, because that is where the newlines end.
|
|
116
|
+
this.cursor = 0;
|
|
117
|
+
// What is currently on screen, row for row. The diff is against this.
|
|
118
|
+
this.prev = [];
|
|
119
|
+
this.caret = { row: 0, col: 0 };
|
|
120
|
+
// Measured, so the tests can assert the diff actually diffs.
|
|
121
|
+
// `claimed` is what makes the newline invariant checkable: every newline the
|
|
122
|
+
// Stage ever emits is a row it claimed, and paint() emits none. The old
|
|
123
|
+
// phrasing — "nothing after reserve emits a newline" — could not survive a
|
|
124
|
+
// band that grows, and conflated the claim path with the paint path.
|
|
125
|
+
this.stats = { paints: 0, rowsWritten: 0, bytes: 0, claimed: 0 };
|
|
141
126
|
}
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
* Claim the band.
|
|
146
|
-
*
|
|
147
|
-
* Emitting `height` newlines is the whole mechanism. It scrolls whatever was
|
|
148
|
-
* on screen up into the terminal's own scrollback — the same thing any
|
|
149
|
-
* program that prints does — and leaves the caret on the band's last row.
|
|
150
|
-
* With height = rows - 1 this lands on the bottom row from any starting
|
|
151
|
-
* position, which is what anchors the band without absolute addressing.
|
|
152
|
-
*/
|
|
153
|
-
reserve(rows = this.maxHeight) {
|
|
154
|
-
if (this.reserved || this.maxHeight < 1) return false;
|
|
155
|
-
const claim = Math.max(1, Math.min(Math.floor(rows), this.maxHeight));
|
|
156
|
-
this.write('\n'.repeat(claim));
|
|
157
|
-
this.stats.claimed += claim;
|
|
158
|
-
this.reserved = true;
|
|
159
|
-
this.height = claim;
|
|
160
|
-
this.cursor = claim - 1;
|
|
161
|
-
this.prev = new Array(claim).fill(null);
|
|
162
|
-
this.caret = { row: claim - 1, col: 0 };
|
|
163
|
-
return true;
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* Claim more rows, up to the ceiling.
|
|
168
|
-
*
|
|
169
|
-
* Newlines emitted from the band's last row scroll the screen and leave the
|
|
170
|
-
* new rows at the BOTTOM, so in band coordinates growth appends: rows 0..h-1
|
|
171
|
-
* keep what they had and the new ones arrive blank. That is why `prev` is
|
|
172
|
-
* extended with nulls rather than rebuilt — the diff keeps everything it
|
|
173
|
-
* already knows and only pays for what actually changed.
|
|
174
|
-
*
|
|
175
|
-
* Growth scrolls whatever is above the band up by the same amount, which is
|
|
176
|
-
* what any program that prints does, and is the only thing UX-01 rev 2 clause
|
|
177
|
-
* (b) permits to move it.
|
|
178
|
-
*/
|
|
179
|
-
grow(delta) {
|
|
180
|
-
if (!this.reserved || delta <= 0) return 0;
|
|
181
|
-
const room = this.maxHeight - this.height;
|
|
182
|
-
const claim = Math.min(Math.floor(delta), room);
|
|
183
|
-
if (claim <= 0) return 0;
|
|
184
|
-
|
|
185
|
-
this.moveTo(this.height - 1);
|
|
186
|
-
this.write('\n'.repeat(claim));
|
|
187
|
-
this.stats.claimed += claim;
|
|
188
|
-
this.height += claim;
|
|
189
|
-
this.cursor = this.height - 1;
|
|
190
|
-
for (let i = 0; i < claim; i++) this.prev.push(null);
|
|
191
|
-
return claim;
|
|
192
|
-
}
|
|
193
|
-
|
|
194
|
-
/**
|
|
195
|
-
* Normalise a caller's rows to exactly `height` rows, each safe to write.
|
|
196
|
-
*
|
|
197
|
-
* The width clamp is the invariant everything rests on and it is the same one
|
|
198
|
-
* ChatPrompt.clampRows() enforces: a row wider than the terminal occupies TWO
|
|
199
|
-
* physical rows, and one wrap desynchronises every relative move that follows
|
|
200
|
-
* it. The spare column also avoids the terminal's deferred-wrap state at the
|
|
201
|
-
* exact margin.
|
|
202
|
-
*/
|
|
203
|
-
normalize(rows) {
|
|
204
|
-
const limit = Math.max(1, this.width() - 1);
|
|
205
|
-
const out = new Array(this.height);
|
|
206
|
-
for (let i = 0; i < this.height; i++) {
|
|
207
|
-
let row = rows && rows[i] != null ? String(rows[i]) : '';
|
|
208
|
-
if (row.includes('\n')) row = row.split('\n')[0];
|
|
209
|
-
out[i] = visibleWidth(row) <= limit ? row : truncate(row, limit, '');
|
|
127
|
+
width() {
|
|
128
|
+
const columns = Number(this.out && this.out.columns);
|
|
129
|
+
return Number.isFinite(columns) && columns > 0 ? Math.max(1, Math.floor(columns)) : 80;
|
|
210
130
|
}
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
moveTo(row) {
|
|
215
|
-
const target = Math.max(0, Math.min(row, this.height - 1));
|
|
216
|
-
const delta = target - this.cursor;
|
|
217
|
-
// CUU/CUD only. Never a newline: on the band's last row a newline scrolls
|
|
218
|
-
// the terminal, which would move the whole block out from under the diff.
|
|
219
|
-
if (delta < 0) this.write(`\x1b[${-delta}A`);
|
|
220
|
-
else if (delta > 0) this.write(`\x1b[${delta}B`);
|
|
221
|
-
this.cursor = target;
|
|
222
|
-
}
|
|
223
|
-
|
|
224
|
-
/**
|
|
225
|
-
* Repaint the band. Rows that did not change cost nothing.
|
|
226
|
-
*
|
|
227
|
-
* Returns the number of rows actually written, so a test can assert that an
|
|
228
|
-
* idle frame is free.
|
|
229
|
-
*/
|
|
230
|
-
paint(rows, caret = null) {
|
|
231
|
-
if (!this.reserved) return 0;
|
|
232
|
-
|
|
233
|
-
const next = this.normalize(rows);
|
|
234
|
-
const target = caret && Number.isFinite(caret.row)
|
|
235
|
-
? { row: Math.max(0, Math.min(Math.floor(caret.row), this.height - 1)), col: Math.max(0, Math.min(this.width() - 2, Math.floor(caret.col) || 0)), hideCursor: Boolean(caret.hideCursor) }
|
|
236
|
-
: { row: this.height - 1, col: 0, hideCursor: Boolean(caret?.hideCursor) };
|
|
237
|
-
|
|
238
|
-
const changed = [];
|
|
239
|
-
for (let i = 0; i < this.height; i++) {
|
|
240
|
-
if (next[i] !== this.prev[i]) changed.push(i);
|
|
131
|
+
/** Rows available to the caller for content, i.e. the whole band. */
|
|
132
|
+
get rows() {
|
|
133
|
+
return this.height;
|
|
241
134
|
}
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
135
|
+
write(text) {
|
|
136
|
+
if (!text)
|
|
137
|
+
return;
|
|
138
|
+
this.stats.bytes += text.length;
|
|
139
|
+
if (typeof runWithStageWrite === 'function') {
|
|
140
|
+
runWithStageWrite(() => this.out.write(text));
|
|
141
|
+
}
|
|
142
|
+
else {
|
|
143
|
+
this.out.write(text);
|
|
144
|
+
}
|
|
252
145
|
}
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
146
|
+
/**
|
|
147
|
+
* Claim the band.
|
|
148
|
+
*
|
|
149
|
+
* Emitting `height` newlines is the whole mechanism. It scrolls whatever was
|
|
150
|
+
* on screen up into the terminal's own scrollback — the same thing any
|
|
151
|
+
* program that prints does — and leaves the caret on the band's last row.
|
|
152
|
+
* With height = rows - 1 this lands on the bottom row from any starting
|
|
153
|
+
* position, which is what anchors the band without absolute addressing.
|
|
154
|
+
*/
|
|
155
|
+
reserve(rows = this.maxHeight) {
|
|
156
|
+
if (this.reserved || this.maxHeight < 1)
|
|
157
|
+
return false;
|
|
158
|
+
const claim = Math.max(1, Math.min(Math.floor(rows), this.maxHeight));
|
|
159
|
+
this.write('\n'.repeat(claim));
|
|
160
|
+
this.stats.claimed += claim;
|
|
161
|
+
this.reserved = true;
|
|
162
|
+
this.height = claim;
|
|
163
|
+
this.cursor = claim - 1;
|
|
164
|
+
this.prev = new Array(claim).fill(null);
|
|
165
|
+
this.caret = { row: claim - 1, col: 0 };
|
|
166
|
+
return true;
|
|
167
|
+
}
|
|
168
|
+
/**
|
|
169
|
+
* Claim more rows, up to the ceiling.
|
|
170
|
+
*
|
|
171
|
+
* Newlines emitted from the band's last row scroll the screen and leave the
|
|
172
|
+
* new rows at the BOTTOM, so in band coordinates growth appends: rows 0..h-1
|
|
173
|
+
* keep what they had and the new ones arrive blank. That is why `prev` is
|
|
174
|
+
* extended with nulls rather than rebuilt — the diff keeps everything it
|
|
175
|
+
* already knows and only pays for what actually changed.
|
|
176
|
+
*
|
|
177
|
+
* Growth scrolls whatever is above the band up by the same amount, which is
|
|
178
|
+
* what any program that prints does, and is the only thing UX-01 rev 2 clause
|
|
179
|
+
* (b) permits to move it.
|
|
180
|
+
*/
|
|
181
|
+
grow(delta) {
|
|
182
|
+
if (!this.reserved || delta <= 0)
|
|
183
|
+
return 0;
|
|
184
|
+
const room = this.maxHeight - this.height;
|
|
185
|
+
const claim = Math.min(Math.floor(delta), room);
|
|
186
|
+
if (claim <= 0)
|
|
187
|
+
return 0;
|
|
188
|
+
this.moveTo(this.height - 1);
|
|
189
|
+
this.write('\n'.repeat(claim));
|
|
190
|
+
this.stats.claimed += claim;
|
|
191
|
+
this.height += claim;
|
|
192
|
+
this.cursor = this.height - 1;
|
|
193
|
+
for (let i = 0; i < claim; i++)
|
|
194
|
+
this.prev.push(null);
|
|
195
|
+
return claim;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Normalise a caller's rows to exactly `height` rows, each safe to write.
|
|
199
|
+
*
|
|
200
|
+
* The width clamp is the invariant everything rests on and it is the same one
|
|
201
|
+
* ChatPrompt.clampRows() enforces: a row wider than the terminal occupies TWO
|
|
202
|
+
* physical rows, and one wrap desynchronises every relative move that follows
|
|
203
|
+
* it. The spare column also avoids the terminal's deferred-wrap state at the
|
|
204
|
+
* exact margin.
|
|
205
|
+
*/
|
|
206
|
+
normalize(rows) {
|
|
207
|
+
const limit = Math.max(1, this.width() - 1);
|
|
208
|
+
const out = new Array(this.height);
|
|
209
|
+
for (let i = 0; i < this.height; i++) {
|
|
210
|
+
let row = rows && rows[i] != null ? String(rows[i]) : '';
|
|
211
|
+
if (row.includes('\n'))
|
|
212
|
+
row = row.split('\n')[0] ?? '';
|
|
213
|
+
out[i] = visibleWidth(row) <= limit ? row : truncate(row, limit, '');
|
|
214
|
+
}
|
|
215
|
+
return out;
|
|
216
|
+
}
|
|
217
|
+
moveTo(row) {
|
|
218
|
+
const target = Math.max(0, Math.min(row, this.height - 1));
|
|
219
|
+
const delta = target - this.cursor;
|
|
220
|
+
// CUU/CUD only. Never a newline: on the band's last row a newline scrolls
|
|
221
|
+
// the terminal, which would move the whole block out from under the diff.
|
|
222
|
+
if (delta < 0)
|
|
223
|
+
this.write(`\x1b[${-delta}A`);
|
|
224
|
+
else if (delta > 0)
|
|
225
|
+
this.write(`\x1b[${delta}B`);
|
|
226
|
+
this.cursor = target;
|
|
227
|
+
}
|
|
228
|
+
/**
|
|
229
|
+
* Repaint the band. Rows that did not change cost nothing.
|
|
230
|
+
*
|
|
231
|
+
* Returns the number of rows actually written, so a test can assert that an
|
|
232
|
+
* idle frame is free.
|
|
233
|
+
*/
|
|
234
|
+
paint(rows, caret = null) {
|
|
235
|
+
if (!this.reserved)
|
|
236
|
+
return 0;
|
|
237
|
+
const next = this.normalize(rows);
|
|
238
|
+
const target = caret && Number.isFinite(caret.row)
|
|
239
|
+
? { row: Math.max(0, Math.min(Math.floor(caret.row), this.height - 1)), col: Math.max(0, Math.min(Math.max(0, this.width() - 2), Math.floor(caret.col) || 0)), hideCursor: Boolean(caret.hideCursor) }
|
|
240
|
+
: { row: this.height - 1, col: 0, hideCursor: Boolean(caret?.hideCursor) };
|
|
241
|
+
const changed = [];
|
|
242
|
+
for (let i = 0; i < this.height; i++) {
|
|
243
|
+
if (next[i] !== this.prev[i])
|
|
244
|
+
changed.push(i);
|
|
245
|
+
}
|
|
246
|
+
const caretMoved = target.row !== this.caret.row || target.col !== this.caret.col || target.hideCursor !== this.caret.hideCursor;
|
|
247
|
+
// A frame with nothing to say emits zero bytes. opencode's renderer makes
|
|
248
|
+
// the same bet and calls it out as its main anti-flicker mechanism.
|
|
249
|
+
if (changed.length === 0 && !caretMoved)
|
|
250
|
+
return 0;
|
|
251
|
+
this.write(SYNC_ON + HIDE_CURSOR + NOWRAP_ON);
|
|
252
|
+
for (const i of changed) {
|
|
253
|
+
this.moveTo(i);
|
|
254
|
+
this.write('\r' + (next[i] ?? '') + ERASE_LINE);
|
|
255
|
+
}
|
|
256
|
+
this.moveTo(target.row);
|
|
257
|
+
this.write('\r');
|
|
258
|
+
if (target.col > 0)
|
|
259
|
+
this.write(`\x1b[${target.col}C`);
|
|
260
|
+
this.write(NOWRAP_OFF + (target.hideCursor ? HIDE_CURSOR : SHOW_CURSOR + '\x1b[6 q') + SYNC_OFF);
|
|
261
|
+
this.prev = next;
|
|
262
|
+
this.caret = target;
|
|
263
|
+
this.stats.paints++;
|
|
264
|
+
this.stats.rowsWritten += changed.length;
|
|
265
|
+
return changed.length;
|
|
266
|
+
}
|
|
267
|
+
/**
|
|
268
|
+
* Give the band back.
|
|
269
|
+
*
|
|
270
|
+
* Erases from the band's top row down. ED 0 clears from the caret to the end
|
|
271
|
+
* of the screen, which is exactly the band and nothing above it. It is not
|
|
272
|
+
* the whole-screen erase (ED 2) that UX-01 bans, and the source scan in
|
|
273
|
+
* tests/chatSurface.test.js is what keeps the two from being confused.
|
|
274
|
+
*/
|
|
275
|
+
collapse() {
|
|
276
|
+
if (!this.reserved)
|
|
277
|
+
return;
|
|
278
|
+
this.write(SYNC_ON);
|
|
279
|
+
this.moveTo(0);
|
|
280
|
+
this.write('\r' + ERASE_BELOW + SHOW_CURSOR + '\x1b[0 q' + SYNC_OFF);
|
|
281
|
+
this.reserved = false;
|
|
282
|
+
this.cursor = 0;
|
|
283
|
+
this.prev = [];
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* The exit contract: the session becomes permanent terminal history.
|
|
287
|
+
*
|
|
288
|
+
* Call after collapse(). opencode's alternate-screen TUI leaves nothing
|
|
289
|
+
* behind and its split-footer mode leaves everything but cannot scroll;
|
|
290
|
+
* doing both is the point of the Stage.
|
|
291
|
+
*/
|
|
292
|
+
printThrough(lines) {
|
|
293
|
+
if (!Array.isArray(lines) || lines.length === 0)
|
|
294
|
+
return 0;
|
|
295
|
+
let written = 0;
|
|
296
|
+
let chunk = '';
|
|
297
|
+
for (const line of lines) {
|
|
298
|
+
chunk += (line == null ? '' : String(line)) + '\n';
|
|
299
|
+
written++;
|
|
300
|
+
if (chunk.length > 16384) {
|
|
301
|
+
this.write(chunk);
|
|
302
|
+
chunk = '';
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
if (chunk)
|
|
306
|
+
this.write(chunk);
|
|
307
|
+
return written;
|
|
308
|
+
}
|
|
309
|
+
/**
|
|
310
|
+
* Terminal resized.
|
|
311
|
+
*
|
|
312
|
+
* Collapse and re-reserve rather than trying to grow or shrink in place. The
|
|
313
|
+
* band's geometry is the one thing the diff cannot recover from being wrong
|
|
314
|
+
* about, and a resize already costs a full reflow of the transcript; one
|
|
315
|
+
* extra scroll per settled gesture is the cheap half. Callers must debounce
|
|
316
|
+
* (ChatPrompt already settles resize at 60ms).
|
|
317
|
+
*/
|
|
318
|
+
resize(height = 0, claim = 0) {
|
|
319
|
+
const next = height > 0 ? Math.floor(height) : stageHeight(this.out, this.env);
|
|
320
|
+
const wasReserved = this.reserved;
|
|
321
|
+
if (wasReserved)
|
|
322
|
+
this.collapse();
|
|
323
|
+
this.maxHeight = next;
|
|
324
|
+
this.height = 0;
|
|
325
|
+
if (next < 1)
|
|
326
|
+
return false;
|
|
327
|
+
if (wasReserved)
|
|
328
|
+
return this.reserve(claim > 0 ? claim : next);
|
|
329
|
+
return false;
|
|
330
|
+
}
|
|
331
|
+
/**
|
|
332
|
+
* Force the next paint to redraw every row.
|
|
333
|
+
*
|
|
334
|
+
* Anything that writes to the terminal behind the Stage's back (an evicted
|
|
335
|
+
* frame flushed to scrollback, a print-through) invalidates the diff.
|
|
336
|
+
*/
|
|
337
|
+
invalidate() {
|
|
338
|
+
this.prev = new Array(this.height).fill(null);
|
|
339
|
+
}
|
|
340
|
+
/** Rows still available before the band hits its ceiling. */
|
|
341
|
+
get room() {
|
|
342
|
+
return Math.max(0, this.maxHeight - this.height);
|
|
298
343
|
}
|
|
299
|
-
if (chunk) this.write(chunk);
|
|
300
|
-
return written;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
/**
|
|
304
|
-
* Terminal resized.
|
|
305
|
-
*
|
|
306
|
-
* Collapse and re-reserve rather than trying to grow or shrink in place. The
|
|
307
|
-
* band's geometry is the one thing the diff cannot recover from being wrong
|
|
308
|
-
* about, and a resize already costs a full reflow of the transcript; one
|
|
309
|
-
* extra scroll per settled gesture is the cheap half. Callers must debounce
|
|
310
|
-
* (ChatPrompt already settles resize at 60ms).
|
|
311
|
-
*/
|
|
312
|
-
resize(height = 0, claim = 0) {
|
|
313
|
-
const next = height > 0 ? Math.floor(height) : stageHeight(this.out, this.env);
|
|
314
|
-
const wasReserved = this.reserved;
|
|
315
|
-
if (wasReserved) this.collapse();
|
|
316
|
-
this.maxHeight = next;
|
|
317
|
-
this.height = 0;
|
|
318
|
-
if (next < 1) return false;
|
|
319
|
-
if (wasReserved) return this.reserve(claim > 0 ? claim : next);
|
|
320
|
-
return false;
|
|
321
|
-
}
|
|
322
|
-
|
|
323
|
-
/**
|
|
324
|
-
* Force the next paint to redraw every row.
|
|
325
|
-
*
|
|
326
|
-
* Anything that writes to the terminal behind the Stage's back (an evicted
|
|
327
|
-
* frame flushed to scrollback, a print-through) invalidates the diff.
|
|
328
|
-
*/
|
|
329
|
-
invalidate() {
|
|
330
|
-
this.prev = new Array(this.height).fill(null);
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
/** Rows still available before the band hits its ceiling. */
|
|
334
|
-
get room() {
|
|
335
|
-
return Math.max(0, this.maxHeight - this.height);
|
|
336
|
-
}
|
|
337
344
|
}
|
package/src/tui/stage.ts
ADDED
|
@@ -0,0 +1,360 @@
|
|
|
1
|
+
// The Stage — a bounded, bottom-anchored region Causyne owns and repaints.
|
|
2
|
+
//
|
|
3
|
+
// WHY THIS IS NOT AN ALTERNATE SCREEN.
|
|
4
|
+
//
|
|
5
|
+
// opencode's full TUI gets its "the chat scrolls inside a box, the terminal
|
|
6
|
+
// does not scroll" behaviour the blunt way: it writes \x1b[?1049h and owns the
|
|
7
|
+
// whole screen, so there is no terminal scrollback left to scroll
|
|
8
|
+
// (packages/tui/src/app.tsx:190-205, screenMode defaults to "alternate-screen").
|
|
9
|
+
// UX-01 forbids Causyne doing that, and the reason is specific and correct: the
|
|
10
|
+
// old src/tui/screen.js wrote ALT_ENTER + HOME + CLEAR_ALL unconditionally,
|
|
11
|
+
// there is no capability detection for the alternate buffer anywhere, and a
|
|
12
|
+
// terminal that silently IGNORES ?1049h does not throw — so the \x1b[2J then
|
|
13
|
+
// wiped the user's real screen.
|
|
14
|
+
//
|
|
15
|
+
// The Stage owns a REGION instead of a screen. Every escape it emits is
|
|
16
|
+
// relative, so it is structurally incapable of touching a row it did not write:
|
|
17
|
+
//
|
|
18
|
+
// * the band is reserved by emitting newlines, never by absolute addressing;
|
|
19
|
+
// * movement is CUU/CUD only — a newline inside paint() would scroll the
|
|
20
|
+
// terminal out from under the block, so paint() emits none;
|
|
21
|
+
// * one seam row is left above the band, which is the visible proof that this
|
|
22
|
+
// is not an alternate screen;
|
|
23
|
+
// * on exit the band is collapsed and the transcript is flushed to real
|
|
24
|
+
// scrollback (printThrough), so the session stays permanent terminal
|
|
25
|
+
// history — the property UX-01 was actually protecting.
|
|
26
|
+
//
|
|
27
|
+
// Rendering is a ROW diff, not a cell diff. opencode can afford per-cell
|
|
28
|
+
// diffing because @opentui/core ships per-platform Zig binaries over FFI;
|
|
29
|
+
// ZM-06 bars that here, so the region is bounded and the granularity is coarser.
|
|
30
|
+
// A spinner tick touches one row; a scroll touches all of them, at a
|
|
31
|
+
// user-driven rate. Both are cheap at 40x200 in pure JS.
|
|
32
|
+
//
|
|
33
|
+
// Pure Node: no dependency, no shell.
|
|
34
|
+
|
|
35
|
+
import { visibleWidth, truncate } from './text.js';
|
|
36
|
+
import { runWithStageWrite } from './consoleGuard.js';
|
|
37
|
+
|
|
38
|
+
export interface StageOutput {
|
|
39
|
+
columns?: number;
|
|
40
|
+
rows?: number;
|
|
41
|
+
isTTY?: boolean;
|
|
42
|
+
write(text: string): unknown;
|
|
43
|
+
}
|
|
44
|
+
export interface StageEnvironment {
|
|
45
|
+
CAUSYNE_STAGE?: string;
|
|
46
|
+
TERM?: string;
|
|
47
|
+
CAUSYNE_SIMPLE_INPUT?: string;
|
|
48
|
+
}
|
|
49
|
+
export interface StageCaret { row: number; col: number; hideCursor?: boolean }
|
|
50
|
+
interface StageOptions { out?: StageOutput; env?: StageEnvironment; height?: number }
|
|
51
|
+
|
|
52
|
+
// Below this the band cannot hold a viewport plus an input box plus a footer,
|
|
53
|
+
// and a cramped stage is worse than honest scrollback. The fallback is not a
|
|
54
|
+
// degraded stage; it is the scrollback renderer, unchanged.
|
|
55
|
+
export const MIN_STAGE_ROWS = 16;
|
|
56
|
+
|
|
57
|
+
// Synchronized output. A terminal that does not implement it ignores both
|
|
58
|
+
// halves, so this is free; one that does composites the whole repaint at once,
|
|
59
|
+
// which is what stops a scroll from tearing.
|
|
60
|
+
const SYNC_ON = '\x1b[?2026h';
|
|
61
|
+
const SYNC_OFF = '\x1b[?2026l';
|
|
62
|
+
const NOWRAP_ON = '\x1b[?7l';
|
|
63
|
+
const NOWRAP_OFF = '\x1b[?7h';
|
|
64
|
+
const HIDE_CURSOR = '\x1b[?25l';
|
|
65
|
+
const SHOW_CURSOR = '\x1b[?25h';
|
|
66
|
+
const ERASE_LINE = '\x1b[K';
|
|
67
|
+
const ERASE_BELOW = '\x1b[J';
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* How tall the band should be, or 0 for "do not use a stage".
|
|
71
|
+
*
|
|
72
|
+
* CAUSYNE_STAGE: off | auto | <rows> | <pct>%
|
|
73
|
+
*/
|
|
74
|
+
export function stageHeight(out: StageOutput = process.stdout, env: StageEnvironment = process.env): number {
|
|
75
|
+
const setting = String(env.CAUSYNE_STAGE ?? 'auto').trim().toLowerCase();
|
|
76
|
+
if (setting === 'off' || setting === '0' || setting === 'false') return 0;
|
|
77
|
+
|
|
78
|
+
const rows = Number(out && out.rows);
|
|
79
|
+
if (!Number.isFinite(rows) || rows <= 0) return 0;
|
|
80
|
+
|
|
81
|
+
// The seam: never take the last row of the user's screen. It costs one row
|
|
82
|
+
// and it is the difference between "a region" and "the screen".
|
|
83
|
+
const ceiling = rows - 1;
|
|
84
|
+
|
|
85
|
+
let target = ceiling;
|
|
86
|
+
const percent = /^(\d{1,3})%$/.exec(setting);
|
|
87
|
+
if (percent) target = Math.round((rows * Number(percent[1])) / 100);
|
|
88
|
+
else if (/^\d+$/.test(setting)) target = Number(setting);
|
|
89
|
+
|
|
90
|
+
target = Math.min(target, ceiling);
|
|
91
|
+
if (!Number.isFinite(target) || target < MIN_STAGE_ROWS) return 0;
|
|
92
|
+
return target;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* May we run a stage on this stream at all?
|
|
97
|
+
*
|
|
98
|
+
* Everything here is a reason the region could not be repainted safely, so the
|
|
99
|
+
* answer is the scrollback renderer rather than a stage that misbehaves.
|
|
100
|
+
*/
|
|
101
|
+
export function stageEnabled(out: StageOutput = process.stdout, env: StageEnvironment = process.env): boolean {
|
|
102
|
+
if (!out || !out.isTTY) return false;
|
|
103
|
+
if (String(env.TERM || '') === 'dumb') return false;
|
|
104
|
+
if (env.CAUSYNE_SIMPLE_INPUT === '1') return false;
|
|
105
|
+
return stageHeight(out, env) > 0;
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
export class Stage {
|
|
109
|
+
out: StageOutput;
|
|
110
|
+
env: StageEnvironment;
|
|
111
|
+
maxHeight: number;
|
|
112
|
+
height: number;
|
|
113
|
+
reserved: boolean;
|
|
114
|
+
cursor: number;
|
|
115
|
+
prev: Array<string | null>;
|
|
116
|
+
caret: StageCaret;
|
|
117
|
+
stats: { paints: number; rowsWritten: number; bytes: number; claimed: number };
|
|
118
|
+
constructor({ out = process.stdout, env = process.env, height = 0 }: StageOptions = {}) {
|
|
119
|
+
this.out = out;
|
|
120
|
+
this.env = env;
|
|
121
|
+
|
|
122
|
+
// The CEILING, not the size. A band claimed by printing newlines costs the
|
|
123
|
+
// user a real row of terminal for every row it takes, so it takes only what
|
|
124
|
+
// it has something to put in and grows from there. Sizing it to the screen
|
|
125
|
+
// is free on an alternate screen — which is what opencode is — and on a
|
|
126
|
+
// reserved band it is 73 rows of void and a banner scrolled out of sight.
|
|
127
|
+
this.maxHeight = height > 0 ? Math.floor(height) : stageHeight(out, env);
|
|
128
|
+
// Rows actually claimed. Zero until reserve().
|
|
129
|
+
this.height = 0;
|
|
130
|
+
|
|
131
|
+
this.reserved = false;
|
|
132
|
+
// Where the caret is, as a row index inside the band. reserve() leaves it
|
|
133
|
+
// on the band's last row, because that is where the newlines end.
|
|
134
|
+
this.cursor = 0;
|
|
135
|
+
// What is currently on screen, row for row. The diff is against this.
|
|
136
|
+
this.prev = [];
|
|
137
|
+
this.caret = { row: 0, col: 0 };
|
|
138
|
+
|
|
139
|
+
// Measured, so the tests can assert the diff actually diffs.
|
|
140
|
+
// `claimed` is what makes the newline invariant checkable: every newline the
|
|
141
|
+
// Stage ever emits is a row it claimed, and paint() emits none. The old
|
|
142
|
+
// phrasing — "nothing after reserve emits a newline" — could not survive a
|
|
143
|
+
// band that grows, and conflated the claim path with the paint path.
|
|
144
|
+
this.stats = { paints: 0, rowsWritten: 0, bytes: 0, claimed: 0 };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
width() {
|
|
148
|
+
const columns = Number(this.out && this.out.columns);
|
|
149
|
+
return Number.isFinite(columns) && columns > 0 ? Math.max(1, Math.floor(columns)) : 80;
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
/** Rows available to the caller for content, i.e. the whole band. */
|
|
153
|
+
get rows() {
|
|
154
|
+
return this.height;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
write(text: string): void {
|
|
158
|
+
if (!text) return;
|
|
159
|
+
this.stats.bytes += text.length;
|
|
160
|
+
if (typeof runWithStageWrite === 'function') {
|
|
161
|
+
runWithStageWrite(() => this.out.write(text));
|
|
162
|
+
} else {
|
|
163
|
+
this.out.write(text);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
/**
|
|
168
|
+
* Claim the band.
|
|
169
|
+
*
|
|
170
|
+
* Emitting `height` newlines is the whole mechanism. It scrolls whatever was
|
|
171
|
+
* on screen up into the terminal's own scrollback — the same thing any
|
|
172
|
+
* program that prints does — and leaves the caret on the band's last row.
|
|
173
|
+
* With height = rows - 1 this lands on the bottom row from any starting
|
|
174
|
+
* position, which is what anchors the band without absolute addressing.
|
|
175
|
+
*/
|
|
176
|
+
reserve(rows: number = this.maxHeight): boolean {
|
|
177
|
+
if (this.reserved || this.maxHeight < 1) return false;
|
|
178
|
+
const claim = Math.max(1, Math.min(Math.floor(rows), this.maxHeight));
|
|
179
|
+
this.write('\n'.repeat(claim));
|
|
180
|
+
this.stats.claimed += claim;
|
|
181
|
+
this.reserved = true;
|
|
182
|
+
this.height = claim;
|
|
183
|
+
this.cursor = claim - 1;
|
|
184
|
+
this.prev = new Array(claim).fill(null);
|
|
185
|
+
this.caret = { row: claim - 1, col: 0 };
|
|
186
|
+
return true;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* Claim more rows, up to the ceiling.
|
|
191
|
+
*
|
|
192
|
+
* Newlines emitted from the band's last row scroll the screen and leave the
|
|
193
|
+
* new rows at the BOTTOM, so in band coordinates growth appends: rows 0..h-1
|
|
194
|
+
* keep what they had and the new ones arrive blank. That is why `prev` is
|
|
195
|
+
* extended with nulls rather than rebuilt — the diff keeps everything it
|
|
196
|
+
* already knows and only pays for what actually changed.
|
|
197
|
+
*
|
|
198
|
+
* Growth scrolls whatever is above the band up by the same amount, which is
|
|
199
|
+
* what any program that prints does, and is the only thing UX-01 rev 2 clause
|
|
200
|
+
* (b) permits to move it.
|
|
201
|
+
*/
|
|
202
|
+
grow(delta: number): number {
|
|
203
|
+
if (!this.reserved || delta <= 0) return 0;
|
|
204
|
+
const room = this.maxHeight - this.height;
|
|
205
|
+
const claim = Math.min(Math.floor(delta), room);
|
|
206
|
+
if (claim <= 0) return 0;
|
|
207
|
+
|
|
208
|
+
this.moveTo(this.height - 1);
|
|
209
|
+
this.write('\n'.repeat(claim));
|
|
210
|
+
this.stats.claimed += claim;
|
|
211
|
+
this.height += claim;
|
|
212
|
+
this.cursor = this.height - 1;
|
|
213
|
+
for (let i = 0; i < claim; i++) this.prev.push(null);
|
|
214
|
+
return claim;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
/**
|
|
218
|
+
* Normalise a caller's rows to exactly `height` rows, each safe to write.
|
|
219
|
+
*
|
|
220
|
+
* The width clamp is the invariant everything rests on and it is the same one
|
|
221
|
+
* ChatPrompt.clampRows() enforces: a row wider than the terminal occupies TWO
|
|
222
|
+
* physical rows, and one wrap desynchronises every relative move that follows
|
|
223
|
+
* it. The spare column also avoids the terminal's deferred-wrap state at the
|
|
224
|
+
* exact margin.
|
|
225
|
+
*/
|
|
226
|
+
normalize(rows: readonly unknown[]): string[] {
|
|
227
|
+
const limit = Math.max(1, this.width() - 1);
|
|
228
|
+
const out = new Array(this.height);
|
|
229
|
+
for (let i = 0; i < this.height; i++) {
|
|
230
|
+
let row = rows && rows[i] != null ? String(rows[i]) : '';
|
|
231
|
+
if (row.includes('\n')) row = row.split('\n')[0] ?? '';
|
|
232
|
+
out[i] = visibleWidth(row) <= limit ? row : truncate(row, limit, '');
|
|
233
|
+
}
|
|
234
|
+
return out;
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
moveTo(row: number): void {
|
|
238
|
+
const target = Math.max(0, Math.min(row, this.height - 1));
|
|
239
|
+
const delta = target - this.cursor;
|
|
240
|
+
// CUU/CUD only. Never a newline: on the band's last row a newline scrolls
|
|
241
|
+
// the terminal, which would move the whole block out from under the diff.
|
|
242
|
+
if (delta < 0) this.write(`\x1b[${-delta}A`);
|
|
243
|
+
else if (delta > 0) this.write(`\x1b[${delta}B`);
|
|
244
|
+
this.cursor = target;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Repaint the band. Rows that did not change cost nothing.
|
|
249
|
+
*
|
|
250
|
+
* Returns the number of rows actually written, so a test can assert that an
|
|
251
|
+
* idle frame is free.
|
|
252
|
+
*/
|
|
253
|
+
paint(rows: readonly unknown[], caret: StageCaret | null = null): number {
|
|
254
|
+
if (!this.reserved) return 0;
|
|
255
|
+
|
|
256
|
+
const next = this.normalize(rows);
|
|
257
|
+
const target = caret && Number.isFinite(caret.row)
|
|
258
|
+
? { row: Math.max(0, Math.min(Math.floor(caret.row), this.height - 1)), col: Math.max(0, Math.min(Math.max(0, this.width() - 2), Math.floor(caret.col) || 0)), hideCursor: Boolean(caret.hideCursor) }
|
|
259
|
+
: { row: this.height - 1, col: 0, hideCursor: Boolean(caret?.hideCursor) };
|
|
260
|
+
|
|
261
|
+
const changed = [];
|
|
262
|
+
for (let i = 0; i < this.height; i++) {
|
|
263
|
+
if (next[i] !== this.prev[i]) changed.push(i);
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
const caretMoved = target.row !== this.caret.row || target.col !== this.caret.col || target.hideCursor !== this.caret.hideCursor;
|
|
267
|
+
// A frame with nothing to say emits zero bytes. opencode's renderer makes
|
|
268
|
+
// the same bet and calls it out as its main anti-flicker mechanism.
|
|
269
|
+
if (changed.length === 0 && !caretMoved) return 0;
|
|
270
|
+
|
|
271
|
+
this.write(SYNC_ON + HIDE_CURSOR + NOWRAP_ON);
|
|
272
|
+
for (const i of changed) {
|
|
273
|
+
this.moveTo(i);
|
|
274
|
+
this.write('\r' + (next[i] ?? '') + ERASE_LINE);
|
|
275
|
+
}
|
|
276
|
+
this.moveTo(target.row);
|
|
277
|
+
this.write('\r');
|
|
278
|
+
if (target.col > 0) this.write(`\x1b[${target.col}C`);
|
|
279
|
+
this.write(NOWRAP_OFF + (target.hideCursor ? HIDE_CURSOR : SHOW_CURSOR + '\x1b[6 q') + SYNC_OFF);
|
|
280
|
+
|
|
281
|
+
this.prev = next;
|
|
282
|
+
this.caret = target;
|
|
283
|
+
this.stats.paints++;
|
|
284
|
+
this.stats.rowsWritten += changed.length;
|
|
285
|
+
return changed.length;
|
|
286
|
+
}
|
|
287
|
+
|
|
288
|
+
/**
|
|
289
|
+
* Give the band back.
|
|
290
|
+
*
|
|
291
|
+
* Erases from the band's top row down. ED 0 clears from the caret to the end
|
|
292
|
+
* of the screen, which is exactly the band and nothing above it. It is not
|
|
293
|
+
* the whole-screen erase (ED 2) that UX-01 bans, and the source scan in
|
|
294
|
+
* tests/chatSurface.test.js is what keeps the two from being confused.
|
|
295
|
+
*/
|
|
296
|
+
collapse() {
|
|
297
|
+
if (!this.reserved) return;
|
|
298
|
+
this.write(SYNC_ON);
|
|
299
|
+
this.moveTo(0);
|
|
300
|
+
this.write('\r' + ERASE_BELOW + SHOW_CURSOR + '\x1b[0 q' + SYNC_OFF);
|
|
301
|
+
this.reserved = false;
|
|
302
|
+
this.cursor = 0;
|
|
303
|
+
this.prev = [];
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* The exit contract: the session becomes permanent terminal history.
|
|
308
|
+
*
|
|
309
|
+
* Call after collapse(). opencode's alternate-screen TUI leaves nothing
|
|
310
|
+
* behind and its split-footer mode leaves everything but cannot scroll;
|
|
311
|
+
* doing both is the point of the Stage.
|
|
312
|
+
*/
|
|
313
|
+
printThrough(lines: readonly unknown[]): number {
|
|
314
|
+
if (!Array.isArray(lines) || lines.length === 0) return 0;
|
|
315
|
+
let written = 0;
|
|
316
|
+
let chunk = '';
|
|
317
|
+
for (const line of lines) {
|
|
318
|
+
chunk += (line == null ? '' : String(line)) + '\n';
|
|
319
|
+
written++;
|
|
320
|
+
if (chunk.length > 16384) { this.write(chunk); chunk = ''; }
|
|
321
|
+
}
|
|
322
|
+
if (chunk) this.write(chunk);
|
|
323
|
+
return written;
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Terminal resized.
|
|
328
|
+
*
|
|
329
|
+
* Collapse and re-reserve rather than trying to grow or shrink in place. The
|
|
330
|
+
* band's geometry is the one thing the diff cannot recover from being wrong
|
|
331
|
+
* about, and a resize already costs a full reflow of the transcript; one
|
|
332
|
+
* extra scroll per settled gesture is the cheap half. Callers must debounce
|
|
333
|
+
* (ChatPrompt already settles resize at 60ms).
|
|
334
|
+
*/
|
|
335
|
+
resize(height = 0, claim = 0) {
|
|
336
|
+
const next = height > 0 ? Math.floor(height) : stageHeight(this.out, this.env);
|
|
337
|
+
const wasReserved = this.reserved;
|
|
338
|
+
if (wasReserved) this.collapse();
|
|
339
|
+
this.maxHeight = next;
|
|
340
|
+
this.height = 0;
|
|
341
|
+
if (next < 1) return false;
|
|
342
|
+
if (wasReserved) return this.reserve(claim > 0 ? claim : next);
|
|
343
|
+
return false;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Force the next paint to redraw every row.
|
|
348
|
+
*
|
|
349
|
+
* Anything that writes to the terminal behind the Stage's back (an evicted
|
|
350
|
+
* frame flushed to scrollback, a print-through) invalidates the diff.
|
|
351
|
+
*/
|
|
352
|
+
invalidate() {
|
|
353
|
+
this.prev = new Array(this.height).fill(null);
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
/** Rows still available before the band hits its ceiling. */
|
|
357
|
+
get room() {
|
|
358
|
+
return Math.max(0, this.maxHeight - this.height);
|
|
359
|
+
}
|
|
360
|
+
}
|