shellpuppet 0.0.1 → 0.2.0
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/LICENSE +202 -0
- package/NOTICE +31 -0
- package/README.md +499 -8
- package/dist/cli/desktopShortcut.d.ts +22 -0
- package/dist/cli/desktopShortcut.js +157 -0
- package/dist/cli/desktopShortcut.js.map +1 -0
- package/dist/cli/index.d.ts +13 -0
- package/dist/cli/index.js +846 -0
- package/dist/cli/index.js.map +1 -0
- package/dist/client/http.d.ts +143 -0
- package/dist/client/http.js +581 -0
- package/dist/client/http.js.map +1 -0
- package/dist/core/dialogs.d.ts +96 -0
- package/dist/core/dialogs.js +226 -0
- package/dist/core/dialogs.js.map +1 -0
- package/dist/core/echo.d.ts +71 -0
- package/dist/core/echo.js +249 -0
- package/dist/core/echo.js.map +1 -0
- package/dist/core/find.d.ts +34 -0
- package/dist/core/find.js +41 -0
- package/dist/core/find.js.map +1 -0
- package/dist/core/keys.d.ts +22 -0
- package/dist/core/keys.js +80 -0
- package/dist/core/keys.js.map +1 -0
- package/dist/core/manager.d.ts +96 -0
- package/dist/core/manager.js +271 -0
- package/dist/core/manager.js.map +1 -0
- package/dist/core/nativePty.d.ts +73 -0
- package/dist/core/nativePty.js +161 -0
- package/dist/core/nativePty.js.map +1 -0
- package/dist/core/policy.d.ts +61 -0
- package/dist/core/policy.js +143 -0
- package/dist/core/policy.js.map +1 -0
- package/dist/core/promptDetector.d.ts +109 -0
- package/dist/core/promptDetector.js +897 -0
- package/dist/core/promptDetector.js.map +1 -0
- package/dist/core/reasoning.d.ts +265 -0
- package/dist/core/reasoning.js +933 -0
- package/dist/core/reasoning.js.map +1 -0
- package/dist/core/regexBudget.d.ts +64 -0
- package/dist/core/regexBudget.js +103 -0
- package/dist/core/regexBudget.js.map +1 -0
- package/dist/core/renderer.d.ts +127 -0
- package/dist/core/renderer.js +316 -0
- package/dist/core/renderer.js.map +1 -0
- package/dist/core/selection.d.ts +6 -0
- package/dist/core/selection.js +48 -0
- package/dist/core/selection.js.map +1 -0
- package/dist/core/session.d.ts +565 -0
- package/dist/core/session.js +1450 -0
- package/dist/core/session.js.map +1 -0
- package/dist/core/types.d.ts +316 -0
- package/dist/core/types.js +71 -0
- package/dist/core/types.js.map +1 -0
- package/dist/core/waiter.d.ts +94 -0
- package/dist/core/waiter.js +310 -0
- package/dist/core/waiter.js.map +1 -0
- package/dist/core/windowsConsoleHost.d.ts +147 -0
- package/dist/core/windowsConsoleHost.js +588 -0
- package/dist/core/windowsConsoleHost.js.map +1 -0
- package/dist/core/windowsPtyStart.d.ts +161 -0
- package/dist/core/windowsPtyStart.js +236 -0
- package/dist/core/windowsPtyStart.js.map +1 -0
- package/dist/daemon/audit.d.ts +23 -0
- package/dist/daemon/audit.js +51 -0
- package/dist/daemon/audit.js.map +1 -0
- package/dist/daemon/auth.d.ts +197 -0
- package/dist/daemon/auth.js +366 -0
- package/dist/daemon/auth.js.map +1 -0
- package/dist/daemon/config.d.ts +119 -0
- package/dist/daemon/config.js +568 -0
- package/dist/daemon/config.js.map +1 -0
- package/dist/daemon/folderPicker.d.ts +27 -0
- package/dist/daemon/folderPicker.js +115 -0
- package/dist/daemon/folderPicker.js.map +1 -0
- package/dist/daemon/index.d.ts +7 -0
- package/dist/daemon/index.js +88 -0
- package/dist/daemon/index.js.map +1 -0
- package/dist/daemon/launchProfiles.d.ts +155 -0
- package/dist/daemon/launchProfiles.js +554 -0
- package/dist/daemon/launchProfiles.js.map +1 -0
- package/dist/daemon/liveness.d.ts +36 -0
- package/dist/daemon/liveness.js +71 -0
- package/dist/daemon/liveness.js.map +1 -0
- package/dist/daemon/logger.d.ts +7 -0
- package/dist/daemon/logger.js +29 -0
- package/dist/daemon/logger.js.map +1 -0
- package/dist/daemon/meta.d.ts +9 -0
- package/dist/daemon/meta.js +24 -0
- package/dist/daemon/meta.js.map +1 -0
- package/dist/daemon/paths.d.ts +85 -0
- package/dist/daemon/paths.js +154 -0
- package/dist/daemon/paths.js.map +1 -0
- package/dist/daemon/screenshot.d.ts +144 -0
- package/dist/daemon/screenshot.js +402 -0
- package/dist/daemon/screenshot.js.map +1 -0
- package/dist/daemon/server.d.ts +453 -0
- package/dist/daemon/server.js +3954 -0
- package/dist/daemon/server.js.map +1 -0
- package/dist/daemon/sessionFeed.d.ts +48 -0
- package/dist/daemon/sessionFeed.js +101 -0
- package/dist/daemon/sessionFeed.js.map +1 -0
- package/dist/daemon/terminalReports.d.ts +38 -0
- package/dist/daemon/terminalReports.js +84 -0
- package/dist/daemon/terminalReports.js.map +1 -0
- package/dist/daemon/viewerLauncher.d.ts +226 -0
- package/dist/daemon/viewerLauncher.js +562 -0
- package/dist/daemon/viewerLauncher.js.map +1 -0
- package/dist/daemon/viewerSync.d.ts +38 -0
- package/dist/daemon/viewerSync.js +83 -0
- package/dist/daemon/viewerSync.js.map +1 -0
- package/dist/mcp/config.d.ts +50 -0
- package/dist/mcp/config.js +95 -0
- package/dist/mcp/config.js.map +1 -0
- package/dist/mcp/index.d.ts +10 -0
- package/dist/mcp/index.js +78 -0
- package/dist/mcp/index.js.map +1 -0
- package/dist/mcp/tools.d.ts +66 -0
- package/dist/mcp/tools.js +638 -0
- package/dist/mcp/tools.js.map +1 -0
- package/dist/viewer/dashboard-folders.js +20 -0
- package/dist/viewer/dashboard-layout.js +52 -0
- package/dist/viewer/dashboard.css +67 -0
- package/dist/viewer/dashboard.html +44 -0
- package/dist/viewer/dashboard.js +1518 -0
- package/dist/viewer/icon-16.png +0 -0
- package/dist/viewer/icon-180.png +0 -0
- package/dist/viewer/icon-32.png +0 -0
- package/dist/viewer/icon-512.png +0 -0
- package/dist/viewer/icon.ico +0 -0
- package/dist/viewer/icon.svg +66 -0
- package/dist/viewer/index.html +61 -0
- package/dist/viewer/pane.js +736 -0
- package/dist/viewer/viewer.css +95 -0
- package/dist/viewer/viewer.js +84 -0
- package/package.json +66 -7
- package/index.js +0 -4
|
@@ -0,0 +1,310 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wait predicates: block until a condition holds on a live screen.
|
|
3
|
+
*
|
|
4
|
+
* The single most important primitive for reliable terminal automation: stop
|
|
5
|
+
* guessing at sleeps (design §3.1). Supports text, regex and quiescence
|
|
6
|
+
* conditions, terminated by process exit or timeout.
|
|
7
|
+
*
|
|
8
|
+
* Pure logic over an abstract target. No I/O; timers only. The daemon adapts a
|
|
9
|
+
* session to `WaitTarget`.
|
|
10
|
+
*/
|
|
11
|
+
import { DEFAULT_WAIT_TIMEOUT_MS, MAX_WAIT_TIMEOUT_MS, WAIT_POLL_INTERVAL_MS, } from './types.js';
|
|
12
|
+
import { MusePromptSettler } from './promptDetector.js';
|
|
13
|
+
import { echoEnd } from './echo.js';
|
|
14
|
+
import { RegexBudgetError, withRegexBudget } from './regexBudget.js';
|
|
15
|
+
/** Question and options only. The filter's rows stay off anything a caller serialises. */
|
|
16
|
+
function promptForCaller(prompt) {
|
|
17
|
+
return {
|
|
18
|
+
question: prompt.question,
|
|
19
|
+
options: prompt.options,
|
|
20
|
+
signature: prompt.signature,
|
|
21
|
+
...(prompt.selectedOption === undefined ? {} : { selectedOption: prompt.selectedOption }),
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/** Thrown for caller mistakes: bad condition shape or an unparsable regex. */
|
|
25
|
+
export class WaitConditionError extends Error {
|
|
26
|
+
constructor(message) {
|
|
27
|
+
super(message);
|
|
28
|
+
this.name = 'WaitConditionError';
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Resolve when the condition holds, the process exits or the timeout lapses.
|
|
33
|
+
* Never rejects for runtime reasons: the result's `matched`/`reason` carry the
|
|
34
|
+
* outcome, because "it timed out" is an answer, not an error. It rejects only
|
|
35
|
+
* for the caller's own mistakes: a malformed condition, which throws
|
|
36
|
+
* synchronously, and a pattern that overruns its evaluation budget, which
|
|
37
|
+
* cannot be known until it is run and so rejects (`regexBudget.ts`). Both are
|
|
38
|
+
* `WaitConditionError`, which the daemon answers with a 400.
|
|
39
|
+
*
|
|
40
|
+
* Semantics worth knowing:
|
|
41
|
+
* - `text` is a case-insensitive substring match; `regex` is case-insensitive.
|
|
42
|
+
* - **The echo trap:** matching text you have just typed matches the *echo* of
|
|
43
|
+
* the input (measured at 0 ms), not the program's output. By default the
|
|
44
|
+
* waiter does not tell them apart, so wait on output-only text or use
|
|
45
|
+
* `quiet`. `matchAfterInput: true` closes the trap: matches at or before the
|
|
46
|
+
* echo of the last input are ignored (see `echo.ts` for how the echo is
|
|
47
|
+
* placed, and when it cannot be). Both behaviours are tested.
|
|
48
|
+
* - `quiet` holds once the program has PAINTED something and then produced no
|
|
49
|
+
* output for the given time. A program that has never painted is not quiet,
|
|
50
|
+
* it is not started: silence counted from session creation would report a
|
|
51
|
+
* CLI ready before its first byte, and a prompt typed on that false ready
|
|
52
|
+
* would be discarded. A test pins this.
|
|
53
|
+
* - `debounce` (text/regex/prompt): the condition must hold on every poll for
|
|
54
|
+
* the given time. A poll without a match restarts settling, while output
|
|
55
|
+
* elsewhere on the screen does not. With hidden reasoning, each poll checks
|
|
56
|
+
* the filtered screen. The reply carries the screen at settlement. The
|
|
57
|
+
* overall timeout still applies while settling.
|
|
58
|
+
* - `prompt` resolves as soon as a permission prompt is on screen, INCLUDING
|
|
59
|
+
* one already there when the wait began. It means "wait until a prompt is
|
|
60
|
+
* showing", not "wait for the next one". There is one exception, on by
|
|
61
|
+
* default: the prompt that was showing when the caller's last input was
|
|
62
|
+
* sent is treated as answered until the screen has been seen without it
|
|
63
|
+
* (`StalePromptGuard`). A supervision loop that has just answered one
|
|
64
|
+
* prompt gets the NEXT one, never the one it answered on a screen
|
|
65
|
+
* the program has not yet repainted. `matchAfterInput: false` restores the
|
|
66
|
+
* plain rule.
|
|
67
|
+
* - Match is checked before exit: an exited process whose final screen contains
|
|
68
|
+
* the target still reports the match.
|
|
69
|
+
*/
|
|
70
|
+
export function waitFor(target, condition, options = {}) {
|
|
71
|
+
const provided = [
|
|
72
|
+
condition.text !== undefined,
|
|
73
|
+
condition.regex !== undefined,
|
|
74
|
+
condition.quiet !== undefined,
|
|
75
|
+
condition.prompt === true,
|
|
76
|
+
].filter(Boolean).length;
|
|
77
|
+
if (provided !== 1) {
|
|
78
|
+
throw new WaitConditionError(`Exactly one of text, regex, quiet or prompt must be provided (got ${String(provided)}).`);
|
|
79
|
+
}
|
|
80
|
+
if (condition.quiet !== undefined && (!Number.isFinite(condition.quiet) || condition.quiet < 0)) {
|
|
81
|
+
throw new WaitConditionError('quiet must be a non-negative number of milliseconds.');
|
|
82
|
+
}
|
|
83
|
+
// timeout/debounce get the same scrutiny as quiet. A NaN timeout (the CLI's
|
|
84
|
+
// `--timeout abc` becomes Number('abc')) would make the deadline comparison
|
|
85
|
+
// below permanently false: the wait, and the one-shot client awaiting the
|
|
86
|
+
// HTTP response, would hang forever, the exact failure class this project
|
|
87
|
+
// exists to eliminate. A NaN debounce would instead never "settle" and
|
|
88
|
+
// mislabel a real match as a timeout.
|
|
89
|
+
if (options.timeout !== undefined &&
|
|
90
|
+
(!Number.isFinite(options.timeout) || options.timeout < 0)) {
|
|
91
|
+
throw new WaitConditionError('timeout must be a non-negative number of milliseconds.');
|
|
92
|
+
}
|
|
93
|
+
if (options.debounce !== undefined &&
|
|
94
|
+
(!Number.isFinite(options.debounce) || options.debounce < 0)) {
|
|
95
|
+
throw new WaitConditionError('debounce must be a non-negative number of milliseconds.');
|
|
96
|
+
}
|
|
97
|
+
// Both text and regex compile to a global, case-insensitive pattern so that
|
|
98
|
+
// a search can start at an offset (the echo guard below) without
|
|
99
|
+
// lowercasing the screen, which can change its length for some Unicode
|
|
100
|
+
// and misalign every offset. `text` is escaped, so it stays a plain
|
|
101
|
+
// substring match.
|
|
102
|
+
let pattern = null;
|
|
103
|
+
let patternReason = 'text';
|
|
104
|
+
/** What to name in a budget refusal: the caller's own words for it. */
|
|
105
|
+
let patternSource = condition.text ?? '';
|
|
106
|
+
if (condition.regex !== undefined) {
|
|
107
|
+
patternSource = condition.regex;
|
|
108
|
+
try {
|
|
109
|
+
// Multiline: the screen is lines joined with \n, and `^42` written by a
|
|
110
|
+
// caller means "a line starting with 42", the same per-line semantics
|
|
111
|
+
// terminal_find has. Without `m` the anchor silently binds to the start
|
|
112
|
+
// of the whole screen and never matches.
|
|
113
|
+
pattern = new RegExp(condition.regex, 'gim');
|
|
114
|
+
patternReason = 'regex';
|
|
115
|
+
}
|
|
116
|
+
catch (cause) {
|
|
117
|
+
throw new WaitConditionError(`Invalid regular expression: ${condition.regex} (${String(cause)})`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
else if (condition.text !== undefined) {
|
|
121
|
+
pattern = new RegExp(condition.text.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'), 'gi');
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Index of the first match at or after `from`, or -1.
|
|
125
|
+
*
|
|
126
|
+
* A caller's own regex runs under a budget, because a pattern that
|
|
127
|
+
* backtracks catastrophically blocks the event loop for longer than this
|
|
128
|
+
* wait's own timeout: the poll timer that would give up never gets to run,
|
|
129
|
+
* and neither does anything else the daemon owes anyone (see
|
|
130
|
+
* `regexBudget.ts`). Per evaluation, not per wait: a screen the pattern
|
|
131
|
+
* copes with costs nothing, and one it does not is refused the moment it
|
|
132
|
+
* appears.
|
|
133
|
+
*
|
|
134
|
+
* A `text` condition is not bounded and does not need to be: it is escaped
|
|
135
|
+
* into a literal above, which has no quantifiers to backtrack through, and
|
|
136
|
+
* a wait can poll thousands of times before its deadline.
|
|
137
|
+
*/
|
|
138
|
+
const bounded = condition.regex !== undefined;
|
|
139
|
+
const matchAt = (screen, from) => {
|
|
140
|
+
if (pattern === null)
|
|
141
|
+
return -1;
|
|
142
|
+
const search = () => {
|
|
143
|
+
pattern.lastIndex = from;
|
|
144
|
+
return pattern.exec(screen);
|
|
145
|
+
};
|
|
146
|
+
const found = bounded ? withRegexBudget(patternSource, search) : search();
|
|
147
|
+
return found === null ? -1 : found.index;
|
|
148
|
+
};
|
|
149
|
+
// Default: on for prompt (a supervisor never wants the question it has
|
|
150
|
+
// just answered handed back), off for text/regex (an opt-in guard).
|
|
151
|
+
const afterInput = options.matchAfterInput ?? condition.prompt === true;
|
|
152
|
+
const timeoutMs = Math.min(Math.max(options.timeout ?? DEFAULT_WAIT_TIMEOUT_MS, 0), MAX_WAIT_TIMEOUT_MS);
|
|
153
|
+
const debounceMs = Math.max(options.debounce ?? 0, 0);
|
|
154
|
+
const pollMs = options.pollIntervalMs ?? WAIT_POLL_INTERVAL_MS;
|
|
155
|
+
const startedAt = Date.now();
|
|
156
|
+
let matchedAt = null;
|
|
157
|
+
let matchedReason = null;
|
|
158
|
+
let matchedPrompt = null;
|
|
159
|
+
const promptSettler = new MusePromptSettler();
|
|
160
|
+
/** Last tick: the only prompt on screen was the one the last input answered. */
|
|
161
|
+
let staleOnly = false;
|
|
162
|
+
/** Last tick: matches existed, but only at or before the echo of the last input. */
|
|
163
|
+
let echoOnly = false;
|
|
164
|
+
return new Promise((resolve, reject) => {
|
|
165
|
+
/**
|
|
166
|
+
* The next poll, so an abort can cancel it rather than let it fire once
|
|
167
|
+
* more into a request nobody is reading.
|
|
168
|
+
*/
|
|
169
|
+
let pollTimer = null;
|
|
170
|
+
const onAbort = () => {
|
|
171
|
+
if (pollTimer !== null)
|
|
172
|
+
clearTimeout(pollTimer);
|
|
173
|
+
pollTimer = null;
|
|
174
|
+
finish(false, 'aborted');
|
|
175
|
+
};
|
|
176
|
+
// Only a non-match carries an explanation, and only where the waiter
|
|
177
|
+
// knows one. Each is a different answer from "the condition never
|
|
178
|
+
// held", and the caller's next move depends on which it was.
|
|
179
|
+
const explain = () => {
|
|
180
|
+
if (condition.quiet !== undefined && !target.hasPainted()) {
|
|
181
|
+
return 'The program has not painted anything yet, so quiet cannot hold: it is still starting, or prints nothing.';
|
|
182
|
+
}
|
|
183
|
+
if (condition.prompt === true && staleOnly) {
|
|
184
|
+
return ('The prompt on screen is the one that was showing when your last input was sent, and the ' +
|
|
185
|
+
'screen has not been seen without it since. The input may not have reached the program. ' +
|
|
186
|
+
'Snapshot to check; pass matchAfterInput:false to accept it anyway.');
|
|
187
|
+
}
|
|
188
|
+
if (pattern !== null && echoOnly) {
|
|
189
|
+
return ('Matches were found only at or before the echo of your last input and were ignored ' +
|
|
190
|
+
'(matchAfterInput). If the output you expect repeats what you typed, wait for something ' +
|
|
191
|
+
'else or use quiet.');
|
|
192
|
+
}
|
|
193
|
+
return undefined;
|
|
194
|
+
};
|
|
195
|
+
let settled = false;
|
|
196
|
+
const finish = (matched, reason, screen = target.getScreen()) => {
|
|
197
|
+
if (settled)
|
|
198
|
+
return;
|
|
199
|
+
settled = true;
|
|
200
|
+
options.signal?.removeEventListener('abort', onAbort);
|
|
201
|
+
const detail = matched ? undefined : reason === 'prompt-typed'
|
|
202
|
+
? 'The prompt is still in the composer; press Enter again to submit it.'
|
|
203
|
+
: explain();
|
|
204
|
+
resolve({
|
|
205
|
+
matched,
|
|
206
|
+
reason,
|
|
207
|
+
waitedMs: Date.now() - startedAt,
|
|
208
|
+
screen,
|
|
209
|
+
...(matchedPrompt !== null ? { prompt: promptForCaller(matchedPrompt) } : {}),
|
|
210
|
+
...(detail !== undefined ? { detail } : {}),
|
|
211
|
+
});
|
|
212
|
+
};
|
|
213
|
+
const step = () => {
|
|
214
|
+
const now = Date.now();
|
|
215
|
+
const screen = (pattern !== null || condition.prompt === true) && options.screenForMatch !== undefined
|
|
216
|
+
? options.screenForMatch()
|
|
217
|
+
: target.getScreen();
|
|
218
|
+
if (pattern !== null) {
|
|
219
|
+
// The echo guard applies on every poll: a match at or before the last
|
|
220
|
+
// input's echo does not keep a debounced condition alive.
|
|
221
|
+
const from = afterInput ? (echoEnd(screen, target.lastInput() ?? '') ?? 0) : 0;
|
|
222
|
+
if (matchAt(screen, from) !== -1) {
|
|
223
|
+
matchedAt ??= now;
|
|
224
|
+
matchedReason = patternReason;
|
|
225
|
+
echoOnly = false;
|
|
226
|
+
}
|
|
227
|
+
else {
|
|
228
|
+
matchedAt = null;
|
|
229
|
+
matchedReason = null;
|
|
230
|
+
echoOnly = from > 0 && matchAt(screen, 0) !== -1;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
else if (condition.prompt === true) {
|
|
234
|
+
// Detect the current prompt on each poll so the reply carries the
|
|
235
|
+
// question actually showing when the wait settles.
|
|
236
|
+
const detected = target.answerablePrompt !== undefined
|
|
237
|
+
? target.answerablePrompt(screen, now)
|
|
238
|
+
: promptSettler.detect(screen, now);
|
|
239
|
+
target.observePrompt(detected?.signature ?? null);
|
|
240
|
+
staleOnly = detected !== null && afterInput && target.isStalePrompt(detected.signature);
|
|
241
|
+
if (detected !== null && !staleOnly) {
|
|
242
|
+
matchedAt ??= now;
|
|
243
|
+
matchedReason = 'prompt';
|
|
244
|
+
matchedPrompt = detected;
|
|
245
|
+
}
|
|
246
|
+
else {
|
|
247
|
+
matchedAt = null;
|
|
248
|
+
matchedReason = null;
|
|
249
|
+
matchedPrompt = null;
|
|
250
|
+
}
|
|
251
|
+
}
|
|
252
|
+
if (matchedAt !== null) {
|
|
253
|
+
// An exited process's final screen cannot repaint, so a match there
|
|
254
|
+
// still resolves before the exit check below.
|
|
255
|
+
if (now - matchedAt >= debounceMs || target.isExited()) {
|
|
256
|
+
finish(true, matchedReason ?? 'text', screen);
|
|
257
|
+
return;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
else if (options.composerPending?.() === true) {
|
|
261
|
+
finish(false, 'prompt-typed', screen);
|
|
262
|
+
return;
|
|
263
|
+
}
|
|
264
|
+
else if (condition.quiet !== undefined &&
|
|
265
|
+
// No paint, no quiet: silence from a program that has not drawn
|
|
266
|
+
// anything is "not started", not "finished and idle".
|
|
267
|
+
target.hasPainted() &&
|
|
268
|
+
now - target.lastChangeAt() >= condition.quiet) {
|
|
269
|
+
finish(true, 'quiet', screen);
|
|
270
|
+
return;
|
|
271
|
+
}
|
|
272
|
+
if (target.isExited()) {
|
|
273
|
+
finish(false, 'exited', screen);
|
|
274
|
+
return;
|
|
275
|
+
}
|
|
276
|
+
if (now - startedAt >= timeoutMs) {
|
|
277
|
+
finish(false, 'timeout', screen);
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
pollTimer = setTimeout(tick, pollMs);
|
|
281
|
+
};
|
|
282
|
+
/**
|
|
283
|
+
* A pattern that overruns its budget is the caller's mistake, so it is
|
|
284
|
+
* reported as one rather than left to escape from a timer callback, where
|
|
285
|
+
* nothing is listening and the daemon's uncaughtException handler would
|
|
286
|
+
* answer it by stopping.
|
|
287
|
+
*/
|
|
288
|
+
const tick = () => {
|
|
289
|
+
try {
|
|
290
|
+
step();
|
|
291
|
+
}
|
|
292
|
+
catch (error) {
|
|
293
|
+
if (error instanceof RegexBudgetError) {
|
|
294
|
+
reject(new WaitConditionError(error.message));
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
reject(error instanceof Error ? error : new Error(String(error)));
|
|
298
|
+
}
|
|
299
|
+
};
|
|
300
|
+
// Checked before the first look as well as after: a caller that has
|
|
301
|
+
// already gone is not owed one.
|
|
302
|
+
if (options.signal?.aborted === true) {
|
|
303
|
+
finish(false, 'aborted');
|
|
304
|
+
return;
|
|
305
|
+
}
|
|
306
|
+
options.signal?.addEventListener('abort', onAbort, { once: true });
|
|
307
|
+
tick();
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
//# sourceMappingURL=waiter.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"waiter.js","sourceRoot":"","sources":["../../src/core/waiter.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAEH,OAAO,EACL,uBAAuB,EACvB,mBAAmB,EACnB,qBAAqB,GACtB,MAAM,YAAY,CAAC;AAEpB,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAC;AAExD,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AACpC,OAAO,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E,CAAC;gBACf,IAAI,KAAK,YAAY,gBAAgB,EAAE,CAAC;oBACtC,MAAM,CAAC,IAAI,kBAAkB,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC;oBAC9C,OAAO;gBACT,CAAC;gBACD,MAAM,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,IAAI,KAAK,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACpE,CAAC;QACH,CAAC,CAAC;QAEF,oEAAoE;QACpE,gCAAgC;QAChC,IAAI,OAAO,CAAC,MAAM,EAAE,OAAO,KAAK,IAAI,EAAE,CAAC;YACrC,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;YACzB,OAAO;QACT,CAAC;QACD,OAAO,CAAC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,IAAI,EAAE,CAAC,CAAC;QACnE,IAAI,EAAE,CAAC;IACT,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Windows console hosts that node-pty leaves behind.
|
|
3
|
+
*
|
|
4
|
+
* ConPTY creates the host while `spawn` runs, and that call returns only
|
|
5
|
+
* after the host exists. The session stores the time immediately before and
|
|
6
|
+
* after it. Nothing is listed then, and a start does not wait.
|
|
7
|
+
*
|
|
8
|
+
* After the session ends, it is queued. One short-lived PowerShell runs
|
|
9
|
+
* `Get-CimInstance` for every session queued within a few hundred
|
|
10
|
+
* milliseconds and asks for the daemon's children. The host that is ended is
|
|
11
|
+
* the one `conhost.exe` or `OpenConsole.exe` whose parent is the daemon and
|
|
12
|
+
* whose creation time falls in that exact window. Candidates are assigned
|
|
13
|
+
* across the batch and other known session windows before any host is ended.
|
|
14
|
+
* A host near another known window outside the batch is protected by a
|
|
15
|
+
* 16 ms guard. Ambiguous and failed items are retried up to three times;
|
|
16
|
+
* a work cap or exhausted retry budget leaves the host running and logs why.
|
|
17
|
+
*
|
|
18
|
+
* The list is not on any path a caller waits on. It compiles nothing: no
|
|
19
|
+
* `Add-Type`, no P/Invoke. Production tracking is on only for Windows.
|
|
20
|
+
* POSIX never queues.
|
|
21
|
+
*
|
|
22
|
+
* Ending the host reclaims the process. Windows PowerShell 5.1's Get-Process
|
|
23
|
+
* holds no handle, and Process.Kill opens its own by pid. The end reads the
|
|
24
|
+
* process handle first, which opens and caches it; a failure to open is the
|
|
25
|
+
* host gone. It then checks the name and the listed creation time, and kills
|
|
26
|
+
* through that cached handle only when both match. The open handle keeps
|
|
27
|
+
* Windows from reusing the pid until the script releases it. A call with no
|
|
28
|
+
* creation time is refused before any process is opened. Every host in the
|
|
29
|
+
* batch is ended by one PowerShell, started asynchronously, so the daemon
|
|
30
|
+
* keeps answering while it runs. The queue settles only after that call
|
|
31
|
+
* finishes. The daemon's own pseudoconsole handle stays open until the
|
|
32
|
+
* daemon exits, and the host is given no CTRL_CLOSE_EVENT.
|
|
33
|
+
*/
|
|
34
|
+
/** Windows only. Sessions queue a console host only when this is set. */
|
|
35
|
+
export declare const TRACK_WINDOWS_CONSOLE_HOST: boolean;
|
|
36
|
+
/** Sessions that end within this of each other share one process list. */
|
|
37
|
+
export declare const CONSOLE_HOST_COALESCE_MS = 300;
|
|
38
|
+
/**
|
|
39
|
+
* How long one list may take. A CIM query of one parent's children takes
|
|
40
|
+
* about ten seconds on this machine, cold or warm.
|
|
41
|
+
*/
|
|
42
|
+
export declare const CONSOLE_HOST_QUERY_TIMEOUT_MS = 45000;
|
|
43
|
+
/** A Windows clock tick can be 15.625 ms; protect neighbouring live windows. */
|
|
44
|
+
export declare const CONSOLE_HOST_GUARD_MS = 16;
|
|
45
|
+
/** Limits synchronous matching even when a daemon has many sessions. */
|
|
46
|
+
export declare const CONSOLE_HOST_MAX_CANDIDATES = 1024;
|
|
47
|
+
export declare const CONSOLE_HOST_MAX_WINDOWS = 256;
|
|
48
|
+
export declare const CONSOLE_HOST_MAX_ATTEMPTS = 3;
|
|
49
|
+
/** A console host as one list returned it. Creation time is Unix milliseconds. */
|
|
50
|
+
export interface ConsoleHostProcess {
|
|
51
|
+
pid: number;
|
|
52
|
+
parentPid: number;
|
|
53
|
+
name: string;
|
|
54
|
+
creationTime: number;
|
|
55
|
+
}
|
|
56
|
+
/** Times taken immediately before and after the synchronous spawn. */
|
|
57
|
+
export interface SpawnWindow {
|
|
58
|
+
t0: number;
|
|
59
|
+
t1: number;
|
|
60
|
+
}
|
|
61
|
+
export interface KnownConsoleHostWindow {
|
|
62
|
+
id: string;
|
|
63
|
+
window: SpawnWindow;
|
|
64
|
+
}
|
|
65
|
+
/** What one ended session asks the shared list to do. */
|
|
66
|
+
export interface QueuedConsoleHost {
|
|
67
|
+
id: string;
|
|
68
|
+
window: SpawnWindow;
|
|
69
|
+
log: (level: 'info' | 'warn', message: string, meta: Record<string, unknown>) => void;
|
|
70
|
+
/** True once this host was ended or found gone; false if the queue gave up. */
|
|
71
|
+
onSettled?: (ended: boolean) => void;
|
|
72
|
+
}
|
|
73
|
+
export interface ConsoleHostQueue {
|
|
74
|
+
push(item: QueuedConsoleHost): void;
|
|
75
|
+
/** Resolves when every queued batch has listed and its ends have finished. */
|
|
76
|
+
whenSettled(): Promise<void>;
|
|
77
|
+
}
|
|
78
|
+
/** Why a list could not be used. A timeout is its own reason. */
|
|
79
|
+
export declare function queryFailureReason(error: unknown): string;
|
|
80
|
+
/** One attributed host handed to a batch end. `error === null` means it was ended. */
|
|
81
|
+
export interface ConsoleHostEndResult {
|
|
82
|
+
pid: number;
|
|
83
|
+
error: unknown;
|
|
84
|
+
}
|
|
85
|
+
export interface ConsoleHostQueueOptions {
|
|
86
|
+
parentPid: number;
|
|
87
|
+
list: (parentPid: number) => Promise<readonly ConsoleHostProcess[]>;
|
|
88
|
+
/**
|
|
89
|
+
* End one attributed host. May return a promise: the queue settles only
|
|
90
|
+
* after it finishes. Not called when `endBatch` is set.
|
|
91
|
+
*/
|
|
92
|
+
endProcess?: (pid: number, host?: ConsoleHostProcess) => void | Promise<void>;
|
|
93
|
+
/**
|
|
94
|
+
* End every host this batch attributed, in one call. The queue settles
|
|
95
|
+
* only after the promise finishes. `ESRCH` finishes that item; any other
|
|
96
|
+
* error retries it.
|
|
97
|
+
*/
|
|
98
|
+
endBatch?: (items: readonly {
|
|
99
|
+
pid: number;
|
|
100
|
+
host: ConsoleHostProcess;
|
|
101
|
+
}[]) => Promise<readonly ConsoleHostEndResult[]>;
|
|
102
|
+
coalesceMs?: number;
|
|
103
|
+
/** Includes live sessions and ended sessions awaiting a retry. */
|
|
104
|
+
knownWindows?: () => readonly KnownConsoleHostWindow[];
|
|
105
|
+
/** Called when an ended session is finished, including a bounded leak. */
|
|
106
|
+
resolved?: (id: string) => void;
|
|
107
|
+
}
|
|
108
|
+
/**
|
|
109
|
+
* Queue ended sessions and list once for each batch. A host is ended only
|
|
110
|
+
* after one-to-one attribution has chosen that process. The queue settles
|
|
111
|
+
* only once those ends have finished.
|
|
112
|
+
*/
|
|
113
|
+
export declare function createConsoleHostQueue(options: ConsoleHostQueueOptions): ConsoleHostQueue;
|
|
114
|
+
/** One CIM query: console hosts whose parent is `parentPid`. No compiled code. */
|
|
115
|
+
export declare function listConsoleHosts(parentPid: number, timeoutMs?: number): Promise<ConsoleHostProcess[]>;
|
|
116
|
+
/**
|
|
117
|
+
* End every listed host in one asynchronous PowerShell. Each pid was read
|
|
118
|
+
* from a process list, so by the time this runs Windows may have given it to
|
|
119
|
+
* another process. A bare `process.kill` would then reach that process,
|
|
120
|
+
* including one on another console.
|
|
121
|
+
*/
|
|
122
|
+
export declare function endConsoleHostProcesses(targets: readonly {
|
|
123
|
+
pid: number;
|
|
124
|
+
creationTime?: number;
|
|
125
|
+
}[]): Promise<readonly ConsoleHostEndResult[]>;
|
|
126
|
+
/**
|
|
127
|
+
* End `pid` only when it is still the console host listed at `creationTime`.
|
|
128
|
+
* The time is required: without it the call is refused and nothing is opened.
|
|
129
|
+
* Reads that process's handle first, which opens and caches it, then checks
|
|
130
|
+
* the name and the start time, and kills through the cached handle. A pid
|
|
131
|
+
* reused after the open is not the one killed, and the open handle keeps
|
|
132
|
+
* Windows from reusing the pid until it is closed. Rejects with `ESRCH` when
|
|
133
|
+
* the pid is gone, the handle cannot be opened, or the process is no longer
|
|
134
|
+
* that host, and the caller leaves that process running. The PowerShell is
|
|
135
|
+
* asynchronous.
|
|
136
|
+
*/
|
|
137
|
+
export declare function endConsoleHostProcess(pid: number, creationTime?: number): Promise<void>;
|
|
138
|
+
/** Keep live and unresolved windows available while another batch lists. */
|
|
139
|
+
export declare function registerConsoleHostWindow(id: string, window: SpawnWindow, platform?: NodeJS.Platform): void;
|
|
140
|
+
/**
|
|
141
|
+
* Resolves when the daemon's queue has finished the batches already pushed.
|
|
142
|
+
* Nothing is listed at the call. Tests wait here instead of polling; the
|
|
143
|
+
* daemon does not.
|
|
144
|
+
*/
|
|
145
|
+
export declare function whenConsoleHostBatchSettled(): Promise<void>;
|
|
146
|
+
/** Queue one ended session. The list runs later, off the caller's stack. */
|
|
147
|
+
export declare function queueConsoleHostEnd(item: QueuedConsoleHost): void;
|