@aragon-agent/cli 0.6.31 → 0.6.32
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/CHANGELOG.md +100 -0
- package/README.md +2957 -2826
- package/dist/agent/controller.d.ts +46 -0
- package/dist/agent/controller.js +175 -7
- package/dist/agent/job-notices.d.ts +143 -0
- package/dist/agent/job-notices.js +182 -0
- package/dist/agent/reducer.d.ts +21 -1
- package/dist/agent/reducer.js +7 -0
- package/dist/agent/system-prompt.d.ts +12 -0
- package/dist/agent/system-prompt.js +2 -0
- package/dist/cli.js +232 -8
- package/dist/commands/builtins.js +23 -8
- package/dist/commands/perf.d.ts +54 -1
- package/dist/commands/perf.js +28 -4
- package/dist/config/cli-commands.d.ts +27 -0
- package/dist/config/cli-commands.js +54 -1
- package/dist/config/env.js +38 -0
- package/dist/config/load.js +50 -1
- package/dist/config/schema.d.ts +73 -0
- package/dist/config/schema.js +34 -0
- package/dist/config/store.js +14 -1
- package/dist/exec/events.d.ts +29 -3
- package/dist/exec/events.js +3 -12
- package/dist/exec/runner.d.ts +24 -0
- package/dist/exec/runner.js +79 -0
- package/dist/input/cursor-report.d.ts +61 -0
- package/dist/input/cursor-report.js +97 -0
- package/dist/input/stdin-filter.d.ts +22 -0
- package/dist/input/stdin-filter.js +48 -3
- package/dist/proc/limits.d.ts +76 -2
- package/dist/proc/limits.js +83 -2
- package/dist/proc/log-mirror.d.ts +59 -0
- package/dist/proc/log-mirror.js +203 -0
- package/dist/proc/log-ring.d.ts +22 -0
- package/dist/proc/log-ring.js +20 -1
- package/dist/proc/notices.d.ts +57 -0
- package/dist/proc/notices.js +101 -0
- package/dist/proc/progress.d.ts +40 -0
- package/dist/proc/progress.js +135 -0
- package/dist/proc/prompt.d.ts +24 -2
- package/dist/proc/prompt.js +51 -2
- package/dist/proc/supervisor.d.ts +35 -2
- package/dist/proc/supervisor.js +142 -43
- package/dist/proc/types.d.ts +89 -1
- package/dist/proc/wait.d.ts +57 -0
- package/dist/proc/wait.js +133 -0
- package/dist/session/persist.js +3 -1
- package/dist/session/validate-session.js +17 -0
- package/dist/tools/bash-tool.d.ts +12 -1
- package/dist/tools/bash-tool.js +100 -13
- package/dist/tools/index.d.ts +26 -2
- package/dist/tools/index.js +43 -2
- package/dist/tools/job-tools.d.ts +54 -0
- package/dist/tools/job-tools.js +329 -0
- package/dist/tools/proc-tools.d.ts +1 -1
- package/dist/tools/proc-tools.js +49 -17
- package/dist/ui/ActivityLine.js +7 -0
- package/dist/ui/App.js +64 -6
- package/dist/ui/ScrollbackShell.d.ts +30 -4
- package/dist/ui/ScrollbackShell.js +8 -3
- package/dist/ui/StatusBar.d.ts +5 -0
- package/dist/ui/StatusBar.js +1 -0
- package/dist/ui/Transcript.d.ts +14 -0
- package/dist/ui/Transcript.js +14 -2
- package/dist/ui/entries/ServiceCard.d.ts +17 -1
- package/dist/ui/entries/ServiceCard.js +39 -4
- package/dist/ui/layout/status-layout.d.ts +13 -1
- package/dist/ui/layout/status-layout.js +10 -0
- package/dist/ui/layout/virtual-window.js +14 -1
- package/dist/ui/run-status-row.d.ts +12 -0
- package/dist/ui/screen.d.ts +17 -4
- package/dist/ui/screen.js +14 -6
- package/dist/ui/scrollback/LiveTail.d.ts +64 -0
- package/dist/ui/scrollback/LiveTail.js +27 -0
- package/dist/ui/scrollback/band.d.ts +105 -0
- package/dist/ui/scrollback/band.js +109 -0
- package/dist/ui/scrollback/budget.d.ts +32 -3
- package/dist/ui/scrollback/budget.js +43 -6
- package/dist/ui/scrollback/canvas.d.ts +99 -0
- package/dist/ui/scrollback/canvas.js +113 -0
- package/dist/ui/scrollback/cursor-probe.d.ts +71 -0
- package/dist/ui/scrollback/cursor-probe.js +133 -0
- package/dist/ui/scrollback/limits.d.ts +49 -0
- package/dist/ui/scrollback/limits.js +49 -0
- package/dist/ui/scrollback/scrollback-writer.d.ts +104 -16
- package/dist/ui/scrollback/scrollback-writer.js +368 -82
- package/dist/ui/scrollback/use-scrollback-view.d.ts +11 -0
- package/dist/ui/scrollback/use-scrollback-view.js +37 -6
- package/dist/ui/scrollback/writer-shapes.d.ts +68 -2
- package/dist/ui/scrollback/writer-shapes.js +53 -8
- package/package.json +3 -3
|
@@ -0,0 +1,109 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Height and column budget of the native-scroll PANEL BAND
|
|
3
|
+
* (release-gate-and-native-panels §6.2).
|
|
4
|
+
*
|
|
5
|
+
* Native scroll mode drops the fixed frame, so it used to drop the panels with it:
|
|
6
|
+
* the TODO rail lived in `AppShell`'s middle band and the subagent roster in the
|
|
7
|
+
* viewport footer, and neither is rendered once `ScrollbackShell` takes over. The
|
|
8
|
+
* band is the replacement - one row pool shared by the roster (`TeamPanel`), the
|
|
9
|
+
* streaming tail and the rail (`TodoPanel`), sitting directly above the composer.
|
|
10
|
+
*
|
|
11
|
+
* PURE: no Ink, no clock, no terminal, no config read. Every invariant below is
|
|
12
|
+
* therefore established BY CONSTRUCTION for all input combinations and can be
|
|
13
|
+
* enumerated by a property test (AC-9..AC-14, AC-31):
|
|
14
|
+
*
|
|
15
|
+
* - `bandRows <= remaining` and therefore `liveRows <= rows - 1` (I-4). Ink writes
|
|
16
|
+
* `clearTerminal` - which contains `ESC[3J`, erasing the user's scrollback - as
|
|
17
|
+
* soon as a frame is as tall as the terminal; that one row is the difference
|
|
18
|
+
* between a tidy region and data loss.
|
|
19
|
+
* - `previewCols + railCols === contentCols`, always (AC-10). The rail is a column
|
|
20
|
+
* INSIDE the region, so the preview loses exactly the columns the rail takes and
|
|
21
|
+
* nothing else (I-5: printing keeps the full `contentCols`).
|
|
22
|
+
* - `teamRows > 0` leaves room for the rail plus one preview row (AC-12).
|
|
23
|
+
* - `previewRows <= previewCap`, so the structural caps
|
|
24
|
+
* `maxLiveTailRows` / `liveTailRowShare` keep binding (I-10).
|
|
25
|
+
*
|
|
26
|
+
* THERE IS EXACTLY ONE CALL SITE: `buildScrollbackBudget` (I-8). This module is
|
|
27
|
+
* exported so it can be tested, not so a second caller can allocate rows of its own
|
|
28
|
+
* - `remaining` is computed there, and a second copy of it is a second authority.
|
|
29
|
+
*/
|
|
30
|
+
import { TODO_LIMITS } from '../../todo/limits.js';
|
|
31
|
+
import { SCROLLBACK_LIMITS } from './limits.js';
|
|
32
|
+
/** Invalid geometry becomes zero, exactly like `layout/team-panel.ts::nonNegativeInt`. */
|
|
33
|
+
function nonNegativeInt(value) {
|
|
34
|
+
return Number.isFinite(value) ? Math.max(0, Math.floor(value)) : 0;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Allocate the band. THE ORDER OF THE STEPS IS THE SPECIFICATION - reordering them
|
|
38
|
+
* changes the priority between "what this run is doing" (rail + roster) and "the
|
|
39
|
+
* words arriving right now" (preview), which is a product decision, not a detail.
|
|
40
|
+
*
|
|
41
|
+
* 0. Normalise every numeric input (`NaN` / `Infinity` / negatives become 0).
|
|
42
|
+
* 1. An open overlay takes the whole pool (the band would otherwise double-count it).
|
|
43
|
+
* 2. Seat the roster first, but never at the cost of the rail plus one preview row:
|
|
44
|
+
* a dispatch is transient, the plan is the shape of the turn, both must fit.
|
|
45
|
+
* 3. Decide the rail's visibility, in the same reason order as `buildTodoRailLayout`.
|
|
46
|
+
* 4. RESERVE the rail's rows (`panelFullRows` floor - the 3-5 row compact form cannot
|
|
47
|
+
* show which item is running, and the band reaches 6 rows far more often than the
|
|
48
|
+
* full-screen rail does). A reservation, not the final value: see step 6.
|
|
49
|
+
* 5. The preview takes what is left, clamped by `previewCap`.
|
|
50
|
+
* 6. `bandRows = max(railReserved, teamRows + previewRows)`: with no unsettled
|
|
51
|
+
* content `previewRows` is 0, yet the rail must still be there (the user is
|
|
52
|
+
* reading a result and still needs the list).
|
|
53
|
+
* 7. Columns: the input `contentCols` is the only width authority.
|
|
54
|
+
* 8. Hand the late-arriving values (the hysteresis) back out for the next frame.
|
|
55
|
+
*/
|
|
56
|
+
export function buildLiveBand(input) {
|
|
57
|
+
const remaining = nonNegativeInt(input.remaining);
|
|
58
|
+
const contentCols = nonNegativeInt(input.contentCols);
|
|
59
|
+
const railCols = nonNegativeInt(input.railCols);
|
|
60
|
+
const previewCap = nonNegativeInt(input.previewCap);
|
|
61
|
+
const previewWanted = nonNegativeInt(input.previewWanted);
|
|
62
|
+
const itemCount = nonNegativeInt(input.itemCount);
|
|
63
|
+
const teamRowsNatural = nonNegativeInt(input.teamRowsNatural);
|
|
64
|
+
const heldRailRows = nonNegativeInt(input.heldRailRows);
|
|
65
|
+
const heldTeamRows = nonNegativeInt(input.heldTeamRows);
|
|
66
|
+
if (input.overlayOpen) {
|
|
67
|
+
return {
|
|
68
|
+
reason: 'overlay', bandRows: remaining, teamRows: 0, previewRows: 0,
|
|
69
|
+
railRows: 0, railCols: 0, previewCols: contentCols, heldRailRows: 0, heldTeamRows: 0,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const teamCeiling = Math.min(teamRowsNatural, Math.max(0, remaining - TODO_LIMITS.panelMinRows - 1));
|
|
73
|
+
// THE ROSTER IS SEATED AT ITS NATURAL (CLAMPED) HEIGHT, NOT AT THE REMEMBERED ONE.
|
|
74
|
+
// Seeding from `heldTeamRows` deadlocks the panel: a fresh dispatch starts with no
|
|
75
|
+
// memory, so frame one would seat zero rows, remember zero, and no later frame could
|
|
76
|
+
// ever seat the roster - the panel this feature exists to restore would never appear
|
|
77
|
+
// (found while implementing; recorded as IF-1 in the spec). The memory below only
|
|
78
|
+
// ever ADDS height back after a squeeze; it never withholds it.
|
|
79
|
+
const teamRows = teamCeiling;
|
|
80
|
+
const reason = !input.panelEnabled ? 'disabled'
|
|
81
|
+
: itemCount === 0 ? 'empty'
|
|
82
|
+
: railCols === 0 ? 'narrow'
|
|
83
|
+
: remaining - teamRows < SCROLLBACK_LIMITS.railBandMinRows ? 'short'
|
|
84
|
+
: 'visible';
|
|
85
|
+
const railVisible = reason === 'visible';
|
|
86
|
+
const railReserved = railVisible
|
|
87
|
+
? Math.min(Math.max(heldRailRows, TODO_LIMITS.panelFullRows), Math.max(0, remaining - teamRows - 1))
|
|
88
|
+
: 0;
|
|
89
|
+
const previewRows = Math.min(previewWanted, previewCap, Math.max(0, remaining - teamRows - railReserved));
|
|
90
|
+
const bandRows = Math.max(railReserved, teamRows + previewRows);
|
|
91
|
+
const railRows = railVisible ? bandRows : 0;
|
|
92
|
+
const railColsOut = railVisible ? railCols : 0;
|
|
93
|
+
return {
|
|
94
|
+
reason,
|
|
95
|
+
bandRows,
|
|
96
|
+
teamRows,
|
|
97
|
+
previewRows,
|
|
98
|
+
railRows,
|
|
99
|
+
railCols: railColsOut,
|
|
100
|
+
previewCols: Math.max(0, contentCols - railColsOut),
|
|
101
|
+
// MONOTONE MEMORY, NOT A SHRINKING ONE (I-6). A rail that collapsed to zero rows
|
|
102
|
+
// because the terminal shrank for two frames would otherwise come back four rows
|
|
103
|
+
// shorter and the band would visibly jump on every resize wobble. The RESET points
|
|
104
|
+
// (an emptied plan, a finished dispatch, a resize) live in the one place that owns
|
|
105
|
+
// the lifecycle, `use-scrollback-view.ts`; this function only remembers.
|
|
106
|
+
heldRailRows: Math.max(heldRailRows, railRows),
|
|
107
|
+
heldTeamRows: Math.max(heldTeamRows, teamRows),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
@@ -10,15 +10,37 @@
|
|
|
10
10
|
* The composer, popup and status rows come from the SAME `buildFrameBudget` the
|
|
11
11
|
* full-screen layout uses, so the two modes cannot disagree about how tall the
|
|
12
12
|
* editor is. What differs is the top: there is no header, so the row it would
|
|
13
|
-
* occupy joins the pool shared by the overlay and the streaming
|
|
13
|
+
* occupy joins the pool shared by the overlay, the panel band and the streaming
|
|
14
|
+
* preview.
|
|
15
|
+
*
|
|
16
|
+
* THIS IS THE ONLY CALL SITE OF `buildLiveBand` (I-8). `remaining` is computed here
|
|
17
|
+
* once and handed to the band; an `App` that computed its own copy would make
|
|
18
|
+
* `liveRows <= rows - 1` depend on two numbers happening to agree instead of on the
|
|
19
|
+
* construction that proves it.
|
|
14
20
|
*/
|
|
21
|
+
import { type LiveBand, type LiveBandInput } from './band.js';
|
|
15
22
|
import { type FrameBudgetInput } from '../layout/budget.js';
|
|
23
|
+
/**
|
|
24
|
+
* Everything the band needs that this module derives for itself: `remaining`,
|
|
25
|
+
* `contentCols`, `railCols`, `previewCap` and `previewWanted` are computed below, and
|
|
26
|
+
* `overlayOpen` is taken from `ScrollbackBudgetInput` so the two can never disagree.
|
|
27
|
+
* The caller supplies only the facts it alone knows (plan, panel switch, roster size,
|
|
28
|
+
* hysteresis), never a number this module is supposed to own (I-8).
|
|
29
|
+
*/
|
|
30
|
+
export type ScrollbackBandInput = Omit<LiveBandInput, 'remaining' | 'contentCols' | 'railCols' | 'previewCap' | 'previewWanted' | 'overlayOpen'>;
|
|
16
31
|
export interface ScrollbackBudgetInput extends FrameBudgetInput {
|
|
17
32
|
overlayOpen: boolean;
|
|
18
33
|
/** Preview rows the content would like; the budget grants what fits. */
|
|
19
34
|
liveTailWanted: number;
|
|
20
35
|
/** The writer tripped its tall-frame guard: degrade to the placeholder until a resize. */
|
|
21
36
|
forceInactive?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* REQUIRED, not optional (the same rule `TeamPanel`'s header records as D-13). An
|
|
39
|
+
* optional band would mean "forgot to pass it" renders silently without panels -
|
|
40
|
+
* exactly the defect this feature exists to remove - while a required field is a
|
|
41
|
+
* compile error at the one place that forgets.
|
|
42
|
+
*/
|
|
43
|
+
band: ScrollbackBandInput;
|
|
22
44
|
}
|
|
23
45
|
export interface ScrollbackBudget {
|
|
24
46
|
rows: number;
|
|
@@ -34,10 +56,17 @@ export interface ScrollbackBudget {
|
|
|
34
56
|
popupRows: number;
|
|
35
57
|
popupMaxHeight: number;
|
|
36
58
|
overlayRows: number;
|
|
59
|
+
/** Preview rows: the band's projection, 0 while an overlay is open. */
|
|
37
60
|
liveTailRows: number;
|
|
38
|
-
/** Rows above the composer:
|
|
61
|
+
/** Rows above the composer: `= band.bandRows` (the identity below is unchanged). */
|
|
39
62
|
topRows: number;
|
|
40
|
-
/** Total live-region height; what Ink renders. */
|
|
63
|
+
/** Total live-region height; what Ink renders (`topRows + composer + status`). */
|
|
41
64
|
liveRows: number;
|
|
65
|
+
/** Height and column allocation of the panel band; the single authority is `band.ts`. */
|
|
66
|
+
band: LiveBand;
|
|
67
|
+
/** Preview/tail width: `contentCols - railCols`. Printing keeps `contentCols`. */
|
|
68
|
+
previewCols: number;
|
|
69
|
+
/** Rows the preview wanted before any clipping (feeds hysteresis and `/perf`). */
|
|
70
|
+
previewWanted: number;
|
|
42
71
|
}
|
|
43
72
|
export declare function buildScrollbackBudget(input: ScrollbackBudgetInput): ScrollbackBudget;
|
|
@@ -10,29 +10,65 @@
|
|
|
10
10
|
* The composer, popup and status rows come from the SAME `buildFrameBudget` the
|
|
11
11
|
* full-screen layout uses, so the two modes cannot disagree about how tall the
|
|
12
12
|
* editor is. What differs is the top: there is no header, so the row it would
|
|
13
|
-
* occupy joins the pool shared by the overlay and the streaming
|
|
13
|
+
* occupy joins the pool shared by the overlay, the panel band and the streaming
|
|
14
|
+
* preview.
|
|
15
|
+
*
|
|
16
|
+
* THIS IS THE ONLY CALL SITE OF `buildLiveBand` (I-8). `remaining` is computed here
|
|
17
|
+
* once and handed to the band; an `App` that computed its own copy would make
|
|
18
|
+
* `liveRows <= rows - 1` depend on two numbers happening to agree instead of on the
|
|
19
|
+
* construction that proves it.
|
|
14
20
|
*/
|
|
21
|
+
import { buildLiveBand } from './band.js';
|
|
15
22
|
import { buildFrameBudget } from '../layout/budget.js';
|
|
23
|
+
import { todoRailWidth } from '../layout/rail.js';
|
|
16
24
|
import { SCROLLBACK_LIMITS } from './limits.js';
|
|
25
|
+
/**
|
|
26
|
+
* The band of a frame that shows no content at all (below the 40x12 floor, or after
|
|
27
|
+
* the writer's tall-frame guard tripped). It is a REAL value rather than `undefined`
|
|
28
|
+
* because `/perf` and the render tests must be able to read the reason on exactly the
|
|
29
|
+
* frames where something went wrong (I-14 / AC-23).
|
|
30
|
+
*/
|
|
31
|
+
function degradedBand(contentCols) {
|
|
32
|
+
return {
|
|
33
|
+
reason: 'short', bandRows: 0, teamRows: 0, previewRows: 0,
|
|
34
|
+
railRows: 0, railCols: 0, previewCols: contentCols, heldRailRows: 0, heldTeamRows: 0,
|
|
35
|
+
};
|
|
36
|
+
}
|
|
17
37
|
export function buildScrollbackBudget(input) {
|
|
18
38
|
const base = buildFrameBudget(input);
|
|
19
39
|
const contentCols = Math.max(1, base.cols - 1);
|
|
40
|
+
// `liveTailWanted` comes from a measurement, but a caller that never measured passes
|
|
41
|
+
// `undefined`/`NaN`; normalising here keeps `NaN` out of `/perf` and out of the band.
|
|
42
|
+
const previewWanted = Number.isFinite(input.liveTailWanted)
|
|
43
|
+
? Math.max(0, Math.floor(input.liveTailWanted))
|
|
44
|
+
: 0;
|
|
20
45
|
if (base.inactive || input.forceInactive === true) {
|
|
21
46
|
return {
|
|
22
47
|
rows: base.rows, cols: base.cols, contentCols, inactive: true, statusRows: 0,
|
|
23
48
|
composerRows: 0, composerSlotRows: 0, composerCols: base.composerCols, popupRows: 0,
|
|
24
49
|
popupMaxHeight: 0, overlayRows: 0, liveTailRows: 0, topRows: 0,
|
|
25
50
|
liveRows: Math.max(0, Math.min(1, base.rows - 1)),
|
|
51
|
+
band: degradedBand(contentCols), previewCols: contentCols, previewWanted,
|
|
26
52
|
};
|
|
27
53
|
}
|
|
28
54
|
// The full-screen frame spends one row on the header; here it joins the pool.
|
|
29
55
|
const remaining = Math.max(0, base.viewportRows + base.headerRows);
|
|
30
|
-
const overlayRows = input.overlayOpen ? remaining : 0;
|
|
31
56
|
const tailCap = Math.min(SCROLLBACK_LIMITS.maxLiveTailRows, Math.floor(base.rows * SCROLLBACK_LIMITS.liveTailRowShare));
|
|
32
|
-
const
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
57
|
+
const band = buildLiveBand({
|
|
58
|
+
...input.band,
|
|
59
|
+
remaining,
|
|
60
|
+
contentCols,
|
|
61
|
+
railCols: todoRailWidth(contentCols),
|
|
62
|
+
previewCap: tailCap,
|
|
63
|
+
previewWanted,
|
|
64
|
+
// Taken from THIS input, never from `input.band`: an overlay that the frame knows
|
|
65
|
+
// about but the band did not would draw no overlay at all, and the shell keys the
|
|
66
|
+
// overlay's rows off the band.
|
|
67
|
+
overlayOpen: input.overlayOpen,
|
|
68
|
+
});
|
|
69
|
+
const overlayRows = band.reason === 'overlay' ? band.bandRows : 0;
|
|
70
|
+
const liveTailRows = band.previewRows;
|
|
71
|
+
const topRows = band.bandRows;
|
|
36
72
|
const liveRows = topRows + base.composerSlotRows + base.statusRows;
|
|
37
73
|
return {
|
|
38
74
|
rows: base.rows, cols: base.cols, contentCols, inactive: false,
|
|
@@ -40,5 +76,6 @@ export function buildScrollbackBudget(input) {
|
|
|
40
76
|
composerSlotRows: base.composerSlotRows, composerCols: base.composerCols,
|
|
41
77
|
popupRows: base.popupRows, popupMaxHeight: base.popupMaxHeight, overlayRows,
|
|
42
78
|
liveTailRows, topRows, liveRows,
|
|
79
|
+
band, previewCols: band.previewCols, previewWanted,
|
|
43
80
|
};
|
|
44
81
|
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canvas arithmetic for native-scroll mode (tui-canvas-bottom-anchor §5.3).
|
|
3
|
+
*
|
|
4
|
+
* The defect this exists for: the bottom anchor of
|
|
5
|
+
* `tui-bottom-anchored-live-region` pinned the live region to the last two rows by
|
|
6
|
+
* prepending `pad` blank rows to the region's own block, so the blank space of a
|
|
7
|
+
* conversation that does not fill the screen sat BETWEEN the newest printed row and
|
|
8
|
+
* the composer. The user sees a reply that is not attached to the input box.
|
|
9
|
+
*
|
|
10
|
+
* The fix is a position change, not a value change: every row above the parked row
|
|
11
|
+
* becomes a CANVAS the writer owns, laid out top-down as `[gap][content][region]`.
|
|
12
|
+
* The blank space therefore lives ABOVE every visible content row, and the newest
|
|
13
|
+
* content row always touches the top row of the live region. The steady state is
|
|
14
|
+
* unchanged: `gap` equals the old `pad` row for row (the screen holds the same
|
|
15
|
+
* number of content rows either way).
|
|
16
|
+
*
|
|
17
|
+
* EVERYTHING HERE IS PURE: no I/O, no terminal, no clock, no `node:*` import. The
|
|
18
|
+
* writer's state machine decides which measurement is authoritative; this module
|
|
19
|
+
* only does the arithmetic, so the arithmetic can be property-tested over every
|
|
20
|
+
* combination instead of being sampled through a screen.
|
|
21
|
+
*/
|
|
22
|
+
/**
|
|
23
|
+
* How far the canvas may reach and how tall it may be, as far as anything measured.
|
|
24
|
+
*
|
|
25
|
+
* `pinned` carries BOTH numbers because they are not independent: the writer parks
|
|
26
|
+
* the cursor on the last row, so `top + capacity === rows - 1` at all times
|
|
27
|
+
* (invariant LB-3). Storing one and deriving the other is what makes an off-by-one
|
|
28
|
+
* invisible until a blank row lands in the scrollback.
|
|
29
|
+
*/
|
|
30
|
+
export type CanvasAnchor = {
|
|
31
|
+
kind: 'unknown';
|
|
32
|
+
} | {
|
|
33
|
+
kind: 'pinned';
|
|
34
|
+
top: number;
|
|
35
|
+
capacity: number;
|
|
36
|
+
};
|
|
37
|
+
/** Nothing measured yet: the pre-canvas append path, byte for byte (I-A3 / I-A8). */
|
|
38
|
+
export declare const UNKNOWN_CANVAS: CanvasAnchor;
|
|
39
|
+
/** Where a `pinned` anchor came from; `/perf` reports it so a fallback is visible. */
|
|
40
|
+
export type AnchorSource = 'probe' | 'fill' | 'none';
|
|
41
|
+
export interface CanvasLayoutInput {
|
|
42
|
+
/** Rows the canvas may occupy, as measured (`rows - top`). */
|
|
43
|
+
capacity: number;
|
|
44
|
+
/** Canvas top row (0-based). */
|
|
45
|
+
top: number;
|
|
46
|
+
/** Content rows already on screen. */
|
|
47
|
+
heldRows: number;
|
|
48
|
+
/** Content rows arriving in this batch. */
|
|
49
|
+
statRows: number;
|
|
50
|
+
/** Height of the live region in this batch. */
|
|
51
|
+
frameRows: number;
|
|
52
|
+
/** 1 when the batch is a re-seed and starts with its own CRLF, else 0. */
|
|
53
|
+
lead: number;
|
|
54
|
+
}
|
|
55
|
+
/** The whole geometry of one batch, as pure numbers (never strings). */
|
|
56
|
+
export interface CanvasLayout {
|
|
57
|
+
/** Content rows the canvas holds after this batch, i.e. after the trim. */
|
|
58
|
+
heldRows: number;
|
|
59
|
+
/** Blank rows above the content. Skipped with CUD, never written (I-B3). */
|
|
60
|
+
gap: number;
|
|
61
|
+
/** Rows this batch writes past the screen bottom; the terminal scrolls them away. */
|
|
62
|
+
scroll: number;
|
|
63
|
+
/** Content rows dropped from the head because they left the screen for good. */
|
|
64
|
+
dropped: number;
|
|
65
|
+
/** The canvas top after this batch. */
|
|
66
|
+
top: number;
|
|
67
|
+
/** The canvas height after this batch, which is the next erase distance. */
|
|
68
|
+
canvasRows: number;
|
|
69
|
+
}
|
|
70
|
+
/** The capacity above the parked row: `rows - 1`. Degrades to 0, never negative. */
|
|
71
|
+
export declare function screenCapacity(rows: number | undefined): number;
|
|
72
|
+
/**
|
|
73
|
+
* Clamp a measured "rows below the cursor" figure into `[0, rows - 1]`.
|
|
74
|
+
*
|
|
75
|
+
* `rows` unknown is treated as `0`: the caller is then exactly at the bottom, which
|
|
76
|
+
* is the conservative branch (no canvas to open up).
|
|
77
|
+
*/
|
|
78
|
+
export declare function clampSpaceBelow(spaceBelow: number, rows: number | undefined): number;
|
|
79
|
+
/**
|
|
80
|
+
* A terminal that grew adds rows BELOW the cursor, so the capacity grows by the
|
|
81
|
+
* exact difference (D6). Shrinking is NOT handled here: it re-measures instead of
|
|
82
|
+
* guessing (D5), so a smaller value would only hide a lost measurement.
|
|
83
|
+
*/
|
|
84
|
+
export declare function applyResizeGrowth(capacity: number, rowsPrev: number, rowsNow: number): number;
|
|
85
|
+
/**
|
|
86
|
+
* The layout of one batch.
|
|
87
|
+
*
|
|
88
|
+
* `heldRows` / `statRows` / `frameRows` are COUNTS, never strings, so this function
|
|
89
|
+
* can be property-tested over every combination without building a screen.
|
|
90
|
+
*
|
|
91
|
+
* TWO BRANCHES, and the difference is physical rather than cosmetic:
|
|
92
|
+
* - FIT: the content plus the region fits the capacity, so the leftover becomes the
|
|
93
|
+
* `gap` at the TOP of the canvas and nothing scrolls.
|
|
94
|
+
* - SCROLL: the canvas cannot hold everything. The batch writes every content row
|
|
95
|
+
* (the caller passes them untrimmed for exactly this reason), the terminal scrolls
|
|
96
|
+
* `scroll` rows away, and the rows that leave are the oldest content rows plus the
|
|
97
|
+
* host output above the canvas. `dropped` is how many content rows left.
|
|
98
|
+
*/
|
|
99
|
+
export declare function resolveCanvasLayout(input: CanvasLayoutInput): CanvasLayout;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canvas arithmetic for native-scroll mode (tui-canvas-bottom-anchor §5.3).
|
|
3
|
+
*
|
|
4
|
+
* The defect this exists for: the bottom anchor of
|
|
5
|
+
* `tui-bottom-anchored-live-region` pinned the live region to the last two rows by
|
|
6
|
+
* prepending `pad` blank rows to the region's own block, so the blank space of a
|
|
7
|
+
* conversation that does not fill the screen sat BETWEEN the newest printed row and
|
|
8
|
+
* the composer. The user sees a reply that is not attached to the input box.
|
|
9
|
+
*
|
|
10
|
+
* The fix is a position change, not a value change: every row above the parked row
|
|
11
|
+
* becomes a CANVAS the writer owns, laid out top-down as `[gap][content][region]`.
|
|
12
|
+
* The blank space therefore lives ABOVE every visible content row, and the newest
|
|
13
|
+
* content row always touches the top row of the live region. The steady state is
|
|
14
|
+
* unchanged: `gap` equals the old `pad` row for row (the screen holds the same
|
|
15
|
+
* number of content rows either way).
|
|
16
|
+
*
|
|
17
|
+
* EVERYTHING HERE IS PURE: no I/O, no terminal, no clock, no `node:*` import. The
|
|
18
|
+
* writer's state machine decides which measurement is authoritative; this module
|
|
19
|
+
* only does the arithmetic, so the arithmetic can be property-tested over every
|
|
20
|
+
* combination instead of being sampled through a screen.
|
|
21
|
+
*/
|
|
22
|
+
/** Nothing measured yet: the pre-canvas append path, byte for byte (I-A3 / I-A8). */
|
|
23
|
+
export const UNKNOWN_CANVAS = { kind: 'unknown' };
|
|
24
|
+
/**
|
|
25
|
+
* `Math.floor` with the two answers a measurement must never produce collapsed onto
|
|
26
|
+
* `fallback`: `NaN` and `Infinity`. A `NaN` row count would make `Array(n)` throw and
|
|
27
|
+
* would turn `rows - 1 - n` into a lie, so a broken number degrades to "nothing".
|
|
28
|
+
*/
|
|
29
|
+
function whole(value, fallback) {
|
|
30
|
+
return Number.isFinite(value) ? Math.floor(value) : fallback;
|
|
31
|
+
}
|
|
32
|
+
/** The capacity above the parked row: `rows - 1`. Degrades to 0, never negative. */
|
|
33
|
+
export function screenCapacity(rows) {
|
|
34
|
+
if (rows === undefined || !Number.isFinite(rows))
|
|
35
|
+
return 0;
|
|
36
|
+
return Math.max(0, Math.floor(rows) - 1);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Clamp a measured "rows below the cursor" figure into `[0, rows - 1]`.
|
|
40
|
+
*
|
|
41
|
+
* `rows` unknown is treated as `0`: the caller is then exactly at the bottom, which
|
|
42
|
+
* is the conservative branch (no canvas to open up).
|
|
43
|
+
*/
|
|
44
|
+
export function clampSpaceBelow(spaceBelow, rows) {
|
|
45
|
+
const upper = screenCapacity(rows);
|
|
46
|
+
const value = Number.isFinite(spaceBelow) ? Math.floor(spaceBelow) : 0;
|
|
47
|
+
return Math.min(upper, Math.max(0, value));
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* A terminal that grew adds rows BELOW the cursor, so the capacity grows by the
|
|
51
|
+
* exact difference (D6). Shrinking is NOT handled here: it re-measures instead of
|
|
52
|
+
* guessing (D5), so a smaller value would only hide a lost measurement.
|
|
53
|
+
*/
|
|
54
|
+
export function applyResizeGrowth(capacity, rowsPrev, rowsNow) {
|
|
55
|
+
// No capacity to grow: a broken number must not become a canvas by arithmetic.
|
|
56
|
+
if (!Number.isFinite(capacity))
|
|
57
|
+
return 0;
|
|
58
|
+
const base = Math.max(0, Math.floor(capacity));
|
|
59
|
+
const delta = Number.isFinite(rowsNow) && Number.isFinite(rowsPrev) ? rowsNow - rowsPrev : 0;
|
|
60
|
+
return base + Math.max(0, Math.floor(delta));
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* The layout of one batch.
|
|
64
|
+
*
|
|
65
|
+
* `heldRows` / `statRows` / `frameRows` are COUNTS, never strings, so this function
|
|
66
|
+
* can be property-tested over every combination without building a screen.
|
|
67
|
+
*
|
|
68
|
+
* TWO BRANCHES, and the difference is physical rather than cosmetic:
|
|
69
|
+
* - FIT: the content plus the region fits the capacity, so the leftover becomes the
|
|
70
|
+
* `gap` at the TOP of the canvas and nothing scrolls.
|
|
71
|
+
* - SCROLL: the canvas cannot hold everything. The batch writes every content row
|
|
72
|
+
* (the caller passes them untrimmed for exactly this reason), the terminal scrolls
|
|
73
|
+
* `scroll` rows away, and the rows that leave are the oldest content rows plus the
|
|
74
|
+
* host output above the canvas. `dropped` is how many content rows left.
|
|
75
|
+
*/
|
|
76
|
+
export function resolveCanvasLayout(input) {
|
|
77
|
+
// A re-seed writes its own leading CRLF, which eats one row of the space below the
|
|
78
|
+
// cursor and starts the canvas one row lower (P1-1 in the canvas model).
|
|
79
|
+
const lead = Math.max(0, whole(input.lead, 0));
|
|
80
|
+
const capacity = Math.max(0, whole(input.capacity, 0) - lead);
|
|
81
|
+
const held = Math.max(0, whole(input.heldRows, 0));
|
|
82
|
+
const stat = Math.max(0, whole(input.statRows, 0));
|
|
83
|
+
const frame = Math.max(0, whole(input.frameRows, 0));
|
|
84
|
+
const top = Math.max(0, whole(input.top, 0)) + lead;
|
|
85
|
+
const content = held + stat;
|
|
86
|
+
const need = content + frame;
|
|
87
|
+
if (need <= capacity) {
|
|
88
|
+
return {
|
|
89
|
+
heldRows: content,
|
|
90
|
+
gap: capacity - need,
|
|
91
|
+
scroll: 0,
|
|
92
|
+
dropped: 0,
|
|
93
|
+
top,
|
|
94
|
+
canvasRows: capacity,
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
const scroll = need - capacity;
|
|
98
|
+
// Rows above the canvas are the host's old output: they leave the screen first.
|
|
99
|
+
const hostUsed = Math.min(scroll, top);
|
|
100
|
+
const grown = capacity + hostUsed;
|
|
101
|
+
const dropped = Math.min(scroll - hostUsed, content);
|
|
102
|
+
const keptContent = content - dropped;
|
|
103
|
+
return {
|
|
104
|
+
heldRows: keptContent,
|
|
105
|
+
// Zero whenever anything scrolled, and that is what keeps blank rows out of the
|
|
106
|
+
// scrollback: a skipped gap is un-written cells, a scrolled one is a written row.
|
|
107
|
+
gap: Math.max(0, grown - keptContent - frame),
|
|
108
|
+
scroll,
|
|
109
|
+
dropped,
|
|
110
|
+
top: top - hostUsed,
|
|
111
|
+
canvasRows: grown,
|
|
112
|
+
};
|
|
113
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor-position probe — the one measurement the bottom anchor cannot do without
|
|
3
|
+
* (tui-bottom-anchored-live-region §4.4 / §6.3).
|
|
4
|
+
*
|
|
5
|
+
* WHY IT EXISTS. "How many rows are left below the cursor" cannot be derived from
|
|
6
|
+
* anything the process already knows: `CUD` clamps silently at the last row and
|
|
7
|
+
* reports nothing, and the only other way to reach the bottom is to print `rows - 1`
|
|
8
|
+
* newlines, which scrolls the user's screen. `ESC[6n` asks instead of guessing, and
|
|
9
|
+
* every target terminal answers it — Windows Terminal, conhost through ConPTY, tmux,
|
|
10
|
+
* screen, xterm/vte/alacritty/kitty/wezterm/foot/ghostty, the VS Code terminal and an
|
|
11
|
+
* SSH session's far end.
|
|
12
|
+
*
|
|
13
|
+
* WHAT IT DOES NOT DO:
|
|
14
|
+
* - It does not touch raw mode. Raising raw is the HOST's job and must happen BEFORE
|
|
15
|
+
* the query (P0-1 / I-A10): in canonical mode the reply is line-buffered until a
|
|
16
|
+
* newline and the terminal ECHOES it as `^[[12;1R` onto the screen, so the probe
|
|
17
|
+
* would time out forever on Linux/macOS while printing garbage.
|
|
18
|
+
* - It does not consume input. The filter ahead of Ink owns every byte; this module
|
|
19
|
+
* is a second `'data'` listener that never `pause()`s, `read()`s or `unshift()`s
|
|
20
|
+
* (D5 / R2).
|
|
21
|
+
* - It does not retry. One query, one bounded wait, then the caller degrades.
|
|
22
|
+
*
|
|
23
|
+
* FAILURE IS NORMAL, NOT AN ERROR: a terminal that never answers is a terminal that
|
|
24
|
+
* gets the one-off screen fill instead (rung 2 of the ladder). Nothing here throws,
|
|
25
|
+
* and nothing here can keep the process alive: the timer is `unref()`ed and the
|
|
26
|
+
* listener is always removed.
|
|
27
|
+
*/
|
|
28
|
+
export interface CursorProbeOptions {
|
|
29
|
+
/** The REAL stdin — not the filter's PassThrough, and not Ink's stream. */
|
|
30
|
+
stdin: NodeJS.ReadStream;
|
|
31
|
+
stdout: NodeJS.WriteStream;
|
|
32
|
+
/** Terminal height, read at answer time so a mid-wait resize is not fatal. */
|
|
33
|
+
rows: () => number | undefined;
|
|
34
|
+
/** Overrides `SCROLLBACK_LIMITS.anchorProbeTimeoutMs` (tests). */
|
|
35
|
+
timeoutMs?: number;
|
|
36
|
+
/**
|
|
37
|
+
* Keep the event loop alive while waiting for the answer.
|
|
38
|
+
*
|
|
39
|
+
* THE STARTUP PROBE MUST SET THIS, and the reason is measured rather than
|
|
40
|
+
* theoretical: an `unref()`ed timer is invisible to the event loop, so on a session
|
|
41
|
+
* where nothing is referenced yet — stdin already at EOF, a piped stdin, a console
|
|
42
|
+
* whose reader is paused — the process can exit BEFORE the first frame is ever
|
|
43
|
+
* drawn. `screen.test.ts`'s subprocess probe caught exactly that: exit code 0,
|
|
44
|
+
* `ESC[6n` on screen and no application at all.
|
|
45
|
+
*
|
|
46
|
+
* Background re-probes keep the default (`false`): a teardown must never be
|
|
47
|
+
* delayed, and by then the UI itself is holding the loop.
|
|
48
|
+
*/
|
|
49
|
+
holdProcess?: boolean;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Arm or disarm the switch. `true` is permanent for the process: there is no
|
|
53
|
+
* legitimate reason to probe after the screen has been handed back.
|
|
54
|
+
*/
|
|
55
|
+
export declare function setAnchorProbeDisabled(value: boolean): void;
|
|
56
|
+
/** Test hook — the switch is process-wide by design, so tests must reset it. */
|
|
57
|
+
export declare function resetAnchorProbeForTests(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Ask the terminal where the cursor is and return the rows left below it
|
|
60
|
+
* (`0` = already on the last row). Returns `null` for every failure mode:
|
|
61
|
+
*
|
|
62
|
+
* | Situation | Result |
|
|
63
|
+
* |----------------------------------|---------------------------------|
|
|
64
|
+
* | switches off / not a TTY / rows unknown | `null`, synchronously, zero bytes |
|
|
65
|
+
* | terminal answers | `rows - 1 - (row - 1)` |
|
|
66
|
+
* | timeout (`anchorProbeTimeoutMs`) | `null`, timer and listener removed |
|
|
67
|
+
* | concurrent call | the same promise; one query only |
|
|
68
|
+
* | `stdout.write` throws | `null` |
|
|
69
|
+
* | a late reply after the timeout | ignored here; the filter strips it |
|
|
70
|
+
*/
|
|
71
|
+
export declare function probeAnchorSpaceBelow(o: CursorProbeOptions): Promise<number | null>;
|
|
@@ -0,0 +1,133 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Cursor-position probe — the one measurement the bottom anchor cannot do without
|
|
3
|
+
* (tui-bottom-anchored-live-region §4.4 / §6.3).
|
|
4
|
+
*
|
|
5
|
+
* WHY IT EXISTS. "How many rows are left below the cursor" cannot be derived from
|
|
6
|
+
* anything the process already knows: `CUD` clamps silently at the last row and
|
|
7
|
+
* reports nothing, and the only other way to reach the bottom is to print `rows - 1`
|
|
8
|
+
* newlines, which scrolls the user's screen. `ESC[6n` asks instead of guessing, and
|
|
9
|
+
* every target terminal answers it — Windows Terminal, conhost through ConPTY, tmux,
|
|
10
|
+
* screen, xterm/vte/alacritty/kitty/wezterm/foot/ghostty, the VS Code terminal and an
|
|
11
|
+
* SSH session's far end.
|
|
12
|
+
*
|
|
13
|
+
* WHAT IT DOES NOT DO:
|
|
14
|
+
* - It does not touch raw mode. Raising raw is the HOST's job and must happen BEFORE
|
|
15
|
+
* the query (P0-1 / I-A10): in canonical mode the reply is line-buffered until a
|
|
16
|
+
* newline and the terminal ECHOES it as `^[[12;1R` onto the screen, so the probe
|
|
17
|
+
* would time out forever on Linux/macOS while printing garbage.
|
|
18
|
+
* - It does not consume input. The filter ahead of Ink owns every byte; this module
|
|
19
|
+
* is a second `'data'` listener that never `pause()`s, `read()`s or `unshift()`s
|
|
20
|
+
* (D5 / R2).
|
|
21
|
+
* - It does not retry. One query, one bounded wait, then the caller degrades.
|
|
22
|
+
*
|
|
23
|
+
* FAILURE IS NORMAL, NOT AN ERROR: a terminal that never answers is a terminal that
|
|
24
|
+
* gets the one-off screen fill instead (rung 2 of the ladder). Nothing here throws,
|
|
25
|
+
* and nothing here can keep the process alive: the timer is `unref()`ed and the
|
|
26
|
+
* listener is always removed.
|
|
27
|
+
*/
|
|
28
|
+
import { SCROLLBACK_LIMITS } from './limits.js';
|
|
29
|
+
import { formatCursorReportQuery, spaceBelowFromReport, splitCursorReports, } from '../../input/cursor-report.js';
|
|
30
|
+
/** Longest acceptable report prefix we buffer before giving up on the chunk. */
|
|
31
|
+
const MAX_BUFFER_CHARS = 64;
|
|
32
|
+
/**
|
|
33
|
+
* THE DEATH SWITCH (I-A9 / P1-6).
|
|
34
|
+
*
|
|
35
|
+
* Once the session is over — `finalizeNativeScreen()`, `disposeFrameWriter()`, a
|
|
36
|
+
* disposed filter, the process `exit` hook — a query must never reach the terminal
|
|
37
|
+
* again. It would be written at a terminal already handed back to the shell, where
|
|
38
|
+
* nothing strips the reply, and the user's prompt would gain a literal `^[[12;1R`.
|
|
39
|
+
* The flag lives at module level because `cli.tsx`'s four exit paths converge on one
|
|
40
|
+
* call and must not each own a copy of the state.
|
|
41
|
+
*/
|
|
42
|
+
let disabled = false;
|
|
43
|
+
/** Single-flight: a drag can ask twice inside one settle window. */
|
|
44
|
+
let inFlight = null;
|
|
45
|
+
/**
|
|
46
|
+
* Arm or disarm the switch. `true` is permanent for the process: there is no
|
|
47
|
+
* legitimate reason to probe after the screen has been handed back.
|
|
48
|
+
*/
|
|
49
|
+
export function setAnchorProbeDisabled(value) {
|
|
50
|
+
disabled = value;
|
|
51
|
+
if (value)
|
|
52
|
+
inFlight = null;
|
|
53
|
+
}
|
|
54
|
+
/** Test hook — the switch is process-wide by design, so tests must reset it. */
|
|
55
|
+
export function resetAnchorProbeForTests() {
|
|
56
|
+
disabled = false;
|
|
57
|
+
inFlight = null;
|
|
58
|
+
}
|
|
59
|
+
/**
|
|
60
|
+
* Ask the terminal where the cursor is and return the rows left below it
|
|
61
|
+
* (`0` = already on the last row). Returns `null` for every failure mode:
|
|
62
|
+
*
|
|
63
|
+
* | Situation | Result |
|
|
64
|
+
* |----------------------------------|---------------------------------|
|
|
65
|
+
* | switches off / not a TTY / rows unknown | `null`, synchronously, zero bytes |
|
|
66
|
+
* | terminal answers | `rows - 1 - (row - 1)` |
|
|
67
|
+
* | timeout (`anchorProbeTimeoutMs`) | `null`, timer and listener removed |
|
|
68
|
+
* | concurrent call | the same promise; one query only |
|
|
69
|
+
* | `stdout.write` throws | `null` |
|
|
70
|
+
* | a late reply after the timeout | ignored here; the filter strips it |
|
|
71
|
+
*/
|
|
72
|
+
export function probeAnchorSpaceBelow(o) {
|
|
73
|
+
if (disabled)
|
|
74
|
+
return Promise.resolve(null);
|
|
75
|
+
if (!o.stdin.isTTY || !o.stdout.isTTY)
|
|
76
|
+
return Promise.resolve(null);
|
|
77
|
+
if (inFlight)
|
|
78
|
+
return inFlight;
|
|
79
|
+
const rowsNow = o.rows();
|
|
80
|
+
if (rowsNow === undefined || !Number.isFinite(rowsNow) || rowsNow < 1) {
|
|
81
|
+
return Promise.resolve(null);
|
|
82
|
+
}
|
|
83
|
+
const promise = new Promise((resolve) => {
|
|
84
|
+
const timeoutMs = o.timeoutMs ?? SCROLLBACK_LIMITS.anchorProbeTimeoutMs;
|
|
85
|
+
let buffer = '';
|
|
86
|
+
let settled = false;
|
|
87
|
+
let timer = null;
|
|
88
|
+
const finish = (value) => {
|
|
89
|
+
if (settled)
|
|
90
|
+
return;
|
|
91
|
+
settled = true;
|
|
92
|
+
if (timer) {
|
|
93
|
+
clearTimeout(timer);
|
|
94
|
+
timer = null;
|
|
95
|
+
}
|
|
96
|
+
o.stdin.removeListener('data', onData);
|
|
97
|
+
resolve(value);
|
|
98
|
+
};
|
|
99
|
+
function onData(chunk) {
|
|
100
|
+
if (settled)
|
|
101
|
+
return;
|
|
102
|
+
buffer += typeof chunk === 'string' ? chunk : chunk.toString('utf8');
|
|
103
|
+
if (buffer.length > MAX_BUFFER_CHARS)
|
|
104
|
+
buffer = buffer.slice(-MAX_BUFFER_CHARS);
|
|
105
|
+
const split = splitCursorReports(buffer);
|
|
106
|
+
const report = split.reports[0];
|
|
107
|
+
if (!report)
|
|
108
|
+
return;
|
|
109
|
+
finish(spaceBelowFromReport(report, o.rows() ?? undefined));
|
|
110
|
+
}
|
|
111
|
+
try {
|
|
112
|
+
o.stdout.write(formatCursorReportQuery());
|
|
113
|
+
}
|
|
114
|
+
catch {
|
|
115
|
+
// A closed or broken stdout is an ordinary failure, not a crash on startup.
|
|
116
|
+
finish(null);
|
|
117
|
+
return;
|
|
118
|
+
}
|
|
119
|
+
o.stdin.on('data', onData);
|
|
120
|
+
timer = setTimeout(() => finish(null), Math.max(1, Math.floor(timeoutMs)));
|
|
121
|
+
// `unref()` by default (see `holdProcess`), so a stray re-probe can never keep a
|
|
122
|
+
// teardown alive. The startup probe opts back in: there it is the ONLY thing
|
|
123
|
+
// holding the loop, and losing the session to save 150 ms is not a trade.
|
|
124
|
+
if (!o.holdProcess)
|
|
125
|
+
timer.unref?.();
|
|
126
|
+
});
|
|
127
|
+
inFlight = promise;
|
|
128
|
+
void promise.then(() => {
|
|
129
|
+
if (inFlight === promise)
|
|
130
|
+
inFlight = null;
|
|
131
|
+
});
|
|
132
|
+
return promise;
|
|
133
|
+
}
|