shellpuppet 0.4.1 → 0.5.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/README.md +26 -4
- package/dist/agents/agy/index.js +1 -1
- package/dist/agents/agy/index.js.map +1 -1
- package/dist/agents/claude/index.js +1 -1
- package/dist/agents/claude/index.js.map +1 -1
- package/dist/agents/codex/draft.js +13 -6
- package/dist/agents/codex/draft.js.map +1 -1
- package/dist/agents/codex/index.js +1 -1
- package/dist/agents/codex/index.js.map +1 -1
- package/dist/agents/codex/nudge.js +7 -1
- package/dist/agents/codex/nudge.js.map +1 -1
- package/dist/agents/grok/index.js +1 -1
- package/dist/agents/grok/index.js.map +1 -1
- package/dist/agents/grok/nudge.js +13 -5
- package/dist/agents/grok/nudge.js.map +1 -1
- package/dist/agents/hermes/frame.js +4 -4
- package/dist/agents/hermes/frame.js.map +1 -1
- package/dist/agents/hermes/index.js +1 -1
- package/dist/agents/hermes/index.js.map +1 -1
- package/dist/agents/mimo/index.js +1 -1
- package/dist/agents/mimo/index.js.map +1 -1
- package/dist/agents/muse/index.js +1 -1
- package/dist/agents/muse/index.js.map +1 -1
- package/dist/agents/muse/nudge.js +8 -3
- package/dist/agents/muse/nudge.js.map +1 -1
- package/dist/agents/nudgeReading.d.ts +2 -0
- package/dist/agents/nudgeReading.js +3 -3
- package/dist/agents/nudgeReading.js.map +1 -1
- package/dist/agents/opencode/index.js +1 -1
- package/dist/agents/opencode/index.js.map +1 -1
- package/dist/agents/qoder/index.js +1 -1
- package/dist/agents/qoder/index.js.map +1 -1
- package/dist/agents/types.d.ts +4 -0
- package/dist/cli/index.js +275 -16
- package/dist/cli/index.js.map +1 -1
- package/dist/client/http.d.ts +8 -0
- package/dist/client/http.js +15 -1
- package/dist/client/http.js.map +1 -1
- package/dist/client/input.d.ts +24 -0
- package/dist/client/input.js +29 -0
- package/dist/client/input.js.map +1 -0
- package/dist/core/mailbox.d.ts +18 -6
- package/dist/core/mailbox.js +35 -10
- package/dist/core/mailbox.js.map +1 -1
- package/dist/core/manager.d.ts +1 -2
- package/dist/core/manager.js +35 -16
- package/dist/core/manager.js.map +1 -1
- package/dist/core/session.d.ts +29 -0
- package/dist/core/session.js +65 -11
- package/dist/core/session.js.map +1 -1
- package/dist/core/types.d.ts +2 -0
- package/dist/core/types.js +2 -0
- package/dist/core/types.js.map +1 -1
- package/dist/core/windowsPtyStart.d.ts +2 -0
- package/dist/core/windowsPtyStart.js +6 -0
- package/dist/core/windowsPtyStart.js.map +1 -1
- package/dist/daemon/audit.d.ts +7 -7
- package/dist/daemon/audit.js +19 -17
- package/dist/daemon/audit.js.map +1 -1
- package/dist/daemon/config.d.ts +1 -1
- package/dist/daemon/config.js +1 -1
- package/dist/daemon/index.js +1 -0
- package/dist/daemon/index.js.map +1 -1
- package/dist/daemon/launchProfiles.d.ts +6 -0
- package/dist/daemon/launchProfiles.js +21 -4
- package/dist/daemon/launchProfiles.js.map +1 -1
- package/dist/daemon/liveness.d.ts +11 -0
- package/dist/daemon/liveness.js +39 -0
- package/dist/daemon/liveness.js.map +1 -1
- package/dist/daemon/schedules.d.ts +1 -1
- package/dist/daemon/schedules.js +8 -3
- package/dist/daemon/schedules.js.map +1 -1
- package/dist/daemon/server.d.ts +17 -0
- package/dist/daemon/server.js +411 -143
- package/dist/daemon/server.js.map +1 -1
- package/dist/daemon/sessionFeed.d.ts +1 -1
- package/dist/daemon/sessionFeed.js +3 -1
- package/dist/daemon/sessionFeed.js.map +1 -1
- package/dist/mcp/tools.js +55 -35
- package/dist/mcp/tools.js.map +1 -1
- package/dist/viewer/dashboard.css +21 -8
- package/dist/viewer/dashboard.js +129 -34
- package/dist/viewer/index.html +1 -0
- package/dist/viewer/pane.js +267 -20
- package/dist/viewer/viewer.css +1 -1
- package/dist/viewer/viewer.js +1 -0
- package/package.json +1 -1
package/dist/cli/index.js
CHANGED
|
@@ -12,16 +12,20 @@
|
|
|
12
12
|
*/
|
|
13
13
|
import { writeFile } from 'node:fs/promises';
|
|
14
14
|
import { readFileSync } from 'node:fs';
|
|
15
|
+
import { spawn } from 'node:child_process';
|
|
15
16
|
import { resolve } from 'node:path';
|
|
16
17
|
import process from 'node:process';
|
|
17
18
|
import { createInterface } from 'node:readline';
|
|
18
19
|
import { DaemonGoneError, daemonStartMode, discoverDaemon, ensureDaemon, request, waitForPendingDaemon, withDaemonStartLock } from '../client/http.js';
|
|
20
|
+
import { preparedInput } from '../client/input.js';
|
|
19
21
|
import { assertNodeVersion, packageVersion } from '../daemon/meta.js';
|
|
20
22
|
import { NO_MATCHES } from '../core/find.js';
|
|
23
|
+
import { claimRecoveryCommand } from '../core/mailbox.js';
|
|
21
24
|
import { visibleMessageText } from '../core/messageText.js';
|
|
22
25
|
import { resultRefusal } from '../core/reasoning.js';
|
|
23
26
|
import { inspectDaemonState, readDaemonState, shellpuppetHome } from '../daemon/paths.js';
|
|
24
27
|
import { clearRestartHold, readRestartHold, writeRestartHold } from '../daemon/restartHold.js';
|
|
28
|
+
import { observedProcessExit, readLinuxProcessIdentity } from '../daemon/liveness.js';
|
|
25
29
|
import { planPassedEnv } from '../mcp/config.js';
|
|
26
30
|
import { isEnvNamesProblem, readDaemonConfig } from '../daemon/config.js';
|
|
27
31
|
import { agentNameFor } from '../core/reasoning.js';
|
|
@@ -89,8 +93,9 @@ Usage:
|
|
|
89
93
|
reasoning. --hide-reasoning is accepted but changes nothing: result
|
|
90
94
|
always leaves reasoning out. Exits 3, with the reason on stderr, while
|
|
91
95
|
the agent is still working or when no answer is recognised.
|
|
92
|
-
shellpuppet type <id> <text> [--enter] [--verify] Type text;
|
|
93
|
-
|
|
96
|
+
shellpuppet type <id> <text> [--enter|--no-enter] [--verify] Type text; known agents submit by default.
|
|
97
|
+
--no-enter types without submitting; --enter submits explicitly.
|
|
98
|
+
Submission waits for a painted draft and quiet
|
|
94
99
|
screen, then checks again before Enter.
|
|
95
100
|
If the draft is absent, Enter is not sent.
|
|
96
101
|
--verify re-reads the screen.
|
|
@@ -148,7 +153,9 @@ Usage:
|
|
|
148
153
|
shellpuppet mcp Run the MCP stdio server (long-running)
|
|
149
154
|
shellpuppet msg send <to> <text|-> Send a message; - reads standard input
|
|
150
155
|
shellpuppet msg inbox|outbox|directory|wait Read messages and live names
|
|
151
|
-
shellpuppet msg claim <name
|
|
156
|
+
shellpuppet msg claim <name> [--replace] Claim a name, or recover it after losing the code
|
|
157
|
+
shellpuppet msg claim --replace --name=<name> Recover a name beginning with a hyphen
|
|
158
|
+
shellpuppet msg release|log Release an outside claim or inspect the log
|
|
152
159
|
shellpuppet inbox Alias for msg inbox
|
|
153
160
|
shellpuppet --version | --help
|
|
154
161
|
|
|
@@ -197,19 +204,29 @@ const VALUE_FLAGS = new Set([
|
|
|
197
204
|
'mark-read',
|
|
198
205
|
]);
|
|
199
206
|
/**
|
|
200
|
-
*
|
|
207
|
+
* Stopping at the first positional argument keeps `start` from eating the target command's
|
|
201
208
|
* own flags: `shellpuppet start [options] <command> [args...]`. Once the
|
|
202
209
|
* command appears, everything after it (including `--version`, `--help`,
|
|
203
210
|
* anything) belongs to the command verbatim: `start goose --version` hands
|
|
204
211
|
* `--version` to Goose.
|
|
205
212
|
*/
|
|
206
|
-
function parseArgs(argv,
|
|
213
|
+
function parseArgs(argv, verb) {
|
|
207
214
|
const positionals = [];
|
|
208
215
|
const flags = new Map();
|
|
209
216
|
const lists = new Map();
|
|
210
217
|
for (let i = 0; i < argv.length; i++) {
|
|
211
218
|
const arg = argv[i] ?? '';
|
|
212
|
-
if (
|
|
219
|
+
if (verb === 'msg' && positionals[0] === 'claim' && arg.startsWith('--name=')) {
|
|
220
|
+
if (flags.has('name'))
|
|
221
|
+
fail(2, 'Supply one claim name.');
|
|
222
|
+
flags.set('name', arg.slice('--name='.length));
|
|
223
|
+
continue;
|
|
224
|
+
}
|
|
225
|
+
if (arg === '--' && verb === 'msg' && positionals[0] === 'claim') {
|
|
226
|
+
positionals.push(...argv.slice(i + 1));
|
|
227
|
+
break;
|
|
228
|
+
}
|
|
229
|
+
if (!arg.startsWith('--') || (verb === 'start' && positionals.length > 0)) {
|
|
213
230
|
positionals.push(arg);
|
|
214
231
|
continue;
|
|
215
232
|
}
|
|
@@ -274,7 +291,8 @@ async function call(connection, method, path, body, options = {}) {
|
|
|
274
291
|
// did not work" without reading the message. Everything else, meaning
|
|
275
292
|
// 401, 403, 404, 409, 5xx and any transport failure, is the operation
|
|
276
293
|
// failing and stays 1.
|
|
277
|
-
|
|
294
|
+
const hint = typeof payload['hint'] === 'string' ? `\n${payload['hint']}` : '';
|
|
295
|
+
fail(response.status === 400 ? 2 : 1, `error (${String(response.status)}): ${message}${hint}`);
|
|
278
296
|
}
|
|
279
297
|
return payload;
|
|
280
298
|
}
|
|
@@ -300,7 +318,7 @@ async function main() {
|
|
|
300
318
|
assertNodeVersion();
|
|
301
319
|
const argv = process.argv.slice(2);
|
|
302
320
|
const verb = argv[0];
|
|
303
|
-
const parsed = parseArgs(argv.slice(1), verb
|
|
321
|
+
const parsed = parseArgs(argv.slice(1), verb);
|
|
304
322
|
const wantJson = parsed.flags.get('json') === true;
|
|
305
323
|
// `--help` and `-h` as the FIRST argument are answered here, with `help` and
|
|
306
324
|
// `--version`, before any daemon contact: the unknown-verb branch runs
|
|
@@ -359,13 +377,18 @@ async function main() {
|
|
|
359
377
|
payload = await call(connection, 'POST', '/messages', { to, text: input === '-' ? readFileSync(0, 'utf8') : input });
|
|
360
378
|
}
|
|
361
379
|
else if (sub === 'claim') {
|
|
362
|
-
const
|
|
363
|
-
|
|
380
|
+
const named = parsed.flags.get('name');
|
|
381
|
+
if (named !== undefined && parsed.positionals[1] !== undefined)
|
|
382
|
+
fail(2, 'Supply one claim name.');
|
|
383
|
+
const name = typeof named === 'string' ? named : parsed.positionals[1] ?? fail(2, 'msg claim needs a name.');
|
|
384
|
+
const replace = parsed.flags.get('replace') === true;
|
|
385
|
+
payload = await call(connection, 'POST', '/messages/claims', { name, ...(replace ? { replace } : {}) });
|
|
364
386
|
if (!wantJson) {
|
|
365
387
|
const claim = `${String(payload['claimId'])}:${String(payload['secret'])}`;
|
|
366
388
|
console.log(`SHELLPUPPET_CLAIM=${claim}`);
|
|
367
389
|
console.log(`PowerShell: $env:SHELLPUPPET_CLAIM='${claim}'`);
|
|
368
390
|
console.log(`POSIX shell: export SHELLPUPPET_CLAIM='${claim}'`);
|
|
391
|
+
console.log(`If you lose this code, run shellpuppet ${claimRecoveryCommand(name)} to obtain a new one.`);
|
|
369
392
|
return;
|
|
370
393
|
}
|
|
371
394
|
}
|
|
@@ -619,11 +642,15 @@ async function main() {
|
|
|
619
642
|
case 'type': {
|
|
620
643
|
const id = parsed.positionals[0] ?? fail(2, 'type needs a session id.');
|
|
621
644
|
const text = parsed.positionals[1] ?? fail(2, 'type needs text.');
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
});
|
|
645
|
+
if (parsed.flags.get('enter') === true && parsed.flags.get('no-enter') === true)
|
|
646
|
+
fail(2, 'Choose --enter or --no-enter.');
|
|
647
|
+
const intent = parsed.flags.get('enter') === true ? true : parsed.flags.get('no-enter') === true ? false : undefined;
|
|
648
|
+
const prepared = preparedInput(connection, text, intent, parsed.flags.get('verify') === true);
|
|
649
|
+
const result = await call(connection, 'POST', `/sessions/${id}/input`, prepared.body, prepared.options);
|
|
650
|
+
if (!wantJson && typeof result['outcome'] === 'string') {
|
|
651
|
+
const reason = typeof result['reason'] === 'string' ? ` (${result['reason']})` : '';
|
|
652
|
+
console.log(`Submit outcome: ${result['outcome']}${reason}.`);
|
|
653
|
+
}
|
|
627
654
|
if (wantJson)
|
|
628
655
|
printJson(result);
|
|
629
656
|
else if (result['submitted'] === false) {
|
|
@@ -861,6 +888,108 @@ async function main() {
|
|
|
861
888
|
}
|
|
862
889
|
process.exit(0);
|
|
863
890
|
}
|
|
891
|
+
class DaemonHandleMissingError extends Error {
|
|
892
|
+
}
|
|
893
|
+
class DaemonWatcherNotReadyError extends Error {
|
|
894
|
+
}
|
|
895
|
+
/** Hold a Windows kernel process handle before asking that process to stop. */
|
|
896
|
+
async function openDaemonExitWatcher(pid, deadline) {
|
|
897
|
+
if (!Number.isSafeInteger(pid) || pid <= 0)
|
|
898
|
+
throw new Error('Invalid daemon PID for exit observation.');
|
|
899
|
+
const testDelay = process.env['SHELLPUPPET_TEST_WATCHER_DELAY_MS'];
|
|
900
|
+
const delayMs = testDelay !== undefined && /^\d{1,5}$/.test(testDelay) ? Math.min(Number(testDelay), 60_000) : 0;
|
|
901
|
+
const script = `$ErrorActionPreference='Stop'; ` +
|
|
902
|
+
(delayMs > 0 ? `Start-Sleep -Milliseconds ${String(delayMs)}; ` : '') +
|
|
903
|
+
`try { ` +
|
|
904
|
+
`try { $target=[System.Diagnostics.Process]::GetProcessById(${String(pid)}); ` +
|
|
905
|
+
`$null=$target.Handle } catch [System.ArgumentException] { ` +
|
|
906
|
+
`[Console]::WriteLine('MISSING'); [Console]::Out.Flush(); exit 1 ` +
|
|
907
|
+
`} catch [System.InvalidOperationException] { ` +
|
|
908
|
+
`[Console]::WriteLine('MISSING'); [Console]::Out.Flush(); exit 1 }; ` +
|
|
909
|
+
`[Console]::WriteLine('READY'); [Console]::Out.Flush(); ` +
|
|
910
|
+
`$target.WaitForExit(); [Console]::WriteLine('EXIT'); [Console]::Out.Flush() ` +
|
|
911
|
+
`} catch { [Console]::WriteLine('ERROR'); [Console]::Out.Flush(); exit 1 ` +
|
|
912
|
+
`} finally { if ($target) { $target.Dispose() } }`;
|
|
913
|
+
const child = spawn('powershell.exe', ['-NoProfile', '-NonInteractive', '-Command', script], {
|
|
914
|
+
stdio: ['ignore', 'pipe', 'ignore'], windowsHide: true,
|
|
915
|
+
});
|
|
916
|
+
let ready = false;
|
|
917
|
+
let completed = false;
|
|
918
|
+
let acceptReady;
|
|
919
|
+
let rejectReady;
|
|
920
|
+
let finishExit;
|
|
921
|
+
const readyPromise = new Promise((resolve, reject) => { acceptReady = resolve; rejectReady = reject; });
|
|
922
|
+
const exited = new Promise((resolve) => { finishExit = resolve; });
|
|
923
|
+
const lines = createInterface({ input: child.stdout });
|
|
924
|
+
lines.on('line', (line) => {
|
|
925
|
+
if (line === 'READY') {
|
|
926
|
+
ready = true;
|
|
927
|
+
acceptReady();
|
|
928
|
+
}
|
|
929
|
+
if (line === 'EXIT') {
|
|
930
|
+
completed = true;
|
|
931
|
+
finishExit(true);
|
|
932
|
+
}
|
|
933
|
+
if (line === 'MISSING') {
|
|
934
|
+
rejectReady(new DaemonHandleMissingError('The daemon process ended before its handle could be opened.'));
|
|
935
|
+
finishExit(false);
|
|
936
|
+
}
|
|
937
|
+
if (line === 'ERROR') {
|
|
938
|
+
rejectReady(new Error('Could not hold a daemon process handle.'));
|
|
939
|
+
finishExit(false);
|
|
940
|
+
}
|
|
941
|
+
});
|
|
942
|
+
child.once('error', (error) => { rejectReady(error); finishExit(false); });
|
|
943
|
+
child.once('close', () => {
|
|
944
|
+
if (!ready)
|
|
945
|
+
rejectReady(new Error('Daemon process watcher ended before it held a handle.'));
|
|
946
|
+
if (!completed)
|
|
947
|
+
finishExit(false);
|
|
948
|
+
lines.close();
|
|
949
|
+
});
|
|
950
|
+
const close = () => {
|
|
951
|
+
lines.close();
|
|
952
|
+
if (child.exitCode === null && child.signalCode === null)
|
|
953
|
+
child.kill();
|
|
954
|
+
};
|
|
955
|
+
let readyTimer;
|
|
956
|
+
try {
|
|
957
|
+
await Promise.race([readyPromise, new Promise((_resolve, reject) => {
|
|
958
|
+
readyTimer = setTimeout(() => reject(new DaemonWatcherNotReadyError('Timed out opening the daemon process handle.')), Math.max(0, deadline - Date.now()));
|
|
959
|
+
})]);
|
|
960
|
+
}
|
|
961
|
+
catch (error) {
|
|
962
|
+
close();
|
|
963
|
+
throw error;
|
|
964
|
+
}
|
|
965
|
+
finally {
|
|
966
|
+
if (readyTimer !== undefined)
|
|
967
|
+
clearTimeout(readyTimer);
|
|
968
|
+
}
|
|
969
|
+
return { exited, close };
|
|
970
|
+
}
|
|
971
|
+
async function waitForStandaloneExit(connection, watcher, originalTick, deadline) {
|
|
972
|
+
if (process.platform === 'win32') {
|
|
973
|
+
if (watcher === null)
|
|
974
|
+
return false;
|
|
975
|
+
let timer;
|
|
976
|
+
try {
|
|
977
|
+
return await Promise.race([watcher.exited, new Promise((resolve) => {
|
|
978
|
+
timer = setTimeout(() => resolve(false), Math.max(0, deadline - Date.now()));
|
|
979
|
+
})]);
|
|
980
|
+
}
|
|
981
|
+
finally {
|
|
982
|
+
if (timer !== undefined)
|
|
983
|
+
clearTimeout(timer);
|
|
984
|
+
}
|
|
985
|
+
}
|
|
986
|
+
while (Date.now() < deadline) {
|
|
987
|
+
if (observedProcessExit(connection.pid, originalTick))
|
|
988
|
+
return true;
|
|
989
|
+
await new Promise((resolve) => setTimeout(resolve, Math.min(150, Math.max(1, deadline - Date.now()))));
|
|
990
|
+
}
|
|
991
|
+
return observedProcessExit(connection.pid, originalTick);
|
|
992
|
+
}
|
|
864
993
|
async function daemonVerb(parsed, wantJson) {
|
|
865
994
|
const sub = parsed.positionals[0];
|
|
866
995
|
const force = parsed.flags.get('force') === true;
|
|
@@ -882,6 +1011,87 @@ async function daemonVerb(parsed, wantJson) {
|
|
|
882
1011
|
const warningFields = () => shutdownWarnings.length ? { warnings: shutdownWarnings } : {};
|
|
883
1012
|
/** Stop the daemon, or say false when it had already gone. */
|
|
884
1013
|
const stopRunning = async (connection) => {
|
|
1014
|
+
let stopInfo = null;
|
|
1015
|
+
if (sub === 'stop') {
|
|
1016
|
+
let preflight;
|
|
1017
|
+
try {
|
|
1018
|
+
preflight = await request(connection, 'GET', '/daemon/stop-info', undefined, {
|
|
1019
|
+
logger: stderrLogger, actor: 'cli', ...daemonOnly,
|
|
1020
|
+
});
|
|
1021
|
+
}
|
|
1022
|
+
catch (error) {
|
|
1023
|
+
if (error instanceof DaemonGoneError)
|
|
1024
|
+
return false;
|
|
1025
|
+
throw error;
|
|
1026
|
+
}
|
|
1027
|
+
if (preflight.status === 200) {
|
|
1028
|
+
const completion = typeof preflight.body === 'object' && preflight.body !== null
|
|
1029
|
+
? preflight.body['completion'] : undefined;
|
|
1030
|
+
if (completion !== undefined && completion !== null) {
|
|
1031
|
+
if (typeof completion !== 'object' || completion === null ||
|
|
1032
|
+
completion['version'] !== 1 ||
|
|
1033
|
+
(completion['role'] !== 'standalone' && completion['role'] !== 'embedded') ||
|
|
1034
|
+
!Number.isSafeInteger(completion['pid']) ||
|
|
1035
|
+
typeof completion['instanceId'] !== 'string') {
|
|
1036
|
+
throw new Error('The daemon returned invalid stop capability information.');
|
|
1037
|
+
}
|
|
1038
|
+
stopInfo = completion;
|
|
1039
|
+
if (stopInfo.pid !== connection.pid || stopInfo.instanceId !== connection.instanceId) {
|
|
1040
|
+
throw new Error('The daemon changed before shutdown; its exit cannot be verified.');
|
|
1041
|
+
}
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
else if (preflight.status !== 404) {
|
|
1045
|
+
throw new Error(`Could not read daemon stop information (HTTP ${String(preflight.status)}).`);
|
|
1046
|
+
}
|
|
1047
|
+
}
|
|
1048
|
+
const standalone = stopInfo?.role === 'standalone';
|
|
1049
|
+
const stopDeadline = Date.now() + 65_000;
|
|
1050
|
+
const originalTick = standalone ? readLinuxProcessIdentity(connection.pid)?.startTick ?? null : null;
|
|
1051
|
+
let watcher = null;
|
|
1052
|
+
if (standalone && process.platform === 'win32') {
|
|
1053
|
+
try {
|
|
1054
|
+
watcher = await openDaemonExitWatcher(connection.pid, stopDeadline);
|
|
1055
|
+
}
|
|
1056
|
+
catch (error) {
|
|
1057
|
+
if (!(error instanceof DaemonHandleMissingError || error instanceof DaemonWatcherNotReadyError))
|
|
1058
|
+
throw error;
|
|
1059
|
+
// A missing PID or unanswered watcher is insufficient: the record or
|
|
1060
|
+
// PID may now name another process. Check discovery once, then ask the
|
|
1061
|
+
// preflight endpoint with its original token before reporting a stop.
|
|
1062
|
+
const discovered = await discoverDaemon({ logger: stderrLogger });
|
|
1063
|
+
if (discovered !== null) {
|
|
1064
|
+
throw new Error('The daemon changed or is still running; its process handle could not be established. Try again.', { cause: error });
|
|
1065
|
+
}
|
|
1066
|
+
try {
|
|
1067
|
+
await request(connection, 'GET', '/daemon/stop-info', undefined, {
|
|
1068
|
+
logger: stderrLogger, actor: 'cli', ...daemonOnly,
|
|
1069
|
+
});
|
|
1070
|
+
}
|
|
1071
|
+
catch (probeError) {
|
|
1072
|
+
if (probeError instanceof DaemonGoneError)
|
|
1073
|
+
return false;
|
|
1074
|
+
throw probeError;
|
|
1075
|
+
}
|
|
1076
|
+
throw new Error('The preflight daemon still answers; its process handle could not be established. Try again.', { cause: error });
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
const unproved = (message) => {
|
|
1080
|
+
if (wantJson)
|
|
1081
|
+
printJson({ stopped: false, teardown: 'in-progress', pid: connection.pid,
|
|
1082
|
+
instanceId: stopInfo?.instanceId, ...warningFields() });
|
|
1083
|
+
throw new Error(message);
|
|
1084
|
+
};
|
|
1085
|
+
const reconcilePosixExit = async () => {
|
|
1086
|
+
if (process.platform === 'win32')
|
|
1087
|
+
return;
|
|
1088
|
+
// The POSIX exit proof can finish after request() has already decided the
|
|
1089
|
+
// daemon went away. Discovery clears that stale record, as it does for a
|
|
1090
|
+
// daemon that vanishes before stop starts.
|
|
1091
|
+
if (await discoverDaemon({ logger: stderrLogger }) !== null) {
|
|
1092
|
+
throw new Error('A daemon is still running after the original process exited. Try again.');
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
885
1095
|
let response;
|
|
886
1096
|
try {
|
|
887
1097
|
response = await request(connection, 'POST', '/daemon/shutdown', { force, ...(sub === 'restart' || holdSeconds !== undefined ? { restart: true } : {}) }, {
|
|
@@ -891,17 +1101,43 @@ async function daemonVerb(parsed, wantJson) {
|
|
|
891
1101
|
});
|
|
892
1102
|
}
|
|
893
1103
|
catch (error) {
|
|
1104
|
+
if (standalone) {
|
|
1105
|
+
try {
|
|
1106
|
+
if (!await waitForStandaloneExit(connection, watcher, originalTick, stopDeadline)) {
|
|
1107
|
+
unproved(`Daemon teardown is still in progress (pid ${String(connection.pid)}, instance ${String(stopInfo?.instanceId)}). Check daemon.log.`);
|
|
1108
|
+
}
|
|
1109
|
+
await reconcilePosixExit();
|
|
1110
|
+
if (holdSeconds !== undefined)
|
|
1111
|
+
writeRestartHold(shellpuppetHome(), holdSeconds);
|
|
1112
|
+
return true;
|
|
1113
|
+
}
|
|
1114
|
+
finally {
|
|
1115
|
+
watcher?.close();
|
|
1116
|
+
}
|
|
1117
|
+
}
|
|
894
1118
|
if (error instanceof DaemonGoneError)
|
|
895
1119
|
return false;
|
|
896
1120
|
throw error;
|
|
897
1121
|
}
|
|
898
1122
|
if (response.status === 409) {
|
|
899
|
-
|
|
1123
|
+
watcher?.close();
|
|
1124
|
+
throw new Error(`${String(response.body['error'])} (use --force)`);
|
|
900
1125
|
}
|
|
901
1126
|
if (response.status >= 400) {
|
|
1127
|
+
watcher?.close();
|
|
902
1128
|
const message = response.body['error'];
|
|
903
1129
|
throw new Error(typeof message === 'string' ? message : 'Daemon shutdown failed.');
|
|
904
1130
|
}
|
|
1131
|
+
if (stopInfo !== null) {
|
|
1132
|
+
const completion = response.body['completion'];
|
|
1133
|
+
if (typeof completion !== 'object' || completion === null ||
|
|
1134
|
+
completion['version'] !== 1 || completion['role'] !== stopInfo.role ||
|
|
1135
|
+
completion['pid'] !== stopInfo.pid ||
|
|
1136
|
+
completion['instanceId'] !== stopInfo.instanceId) {
|
|
1137
|
+
watcher?.close();
|
|
1138
|
+
throw new Error('The daemon changed while accepting shutdown; its exit cannot be verified.');
|
|
1139
|
+
}
|
|
1140
|
+
}
|
|
905
1141
|
const warnings = response.body['warnings'];
|
|
906
1142
|
if (Array.isArray(warnings)) {
|
|
907
1143
|
for (const warning of warnings) {
|
|
@@ -912,6 +1148,29 @@ async function daemonVerb(parsed, wantJson) {
|
|
|
912
1148
|
console.error(warning);
|
|
913
1149
|
}
|
|
914
1150
|
}
|
|
1151
|
+
if (sub === 'stop' && holdSeconds !== undefined) {
|
|
1152
|
+
try {
|
|
1153
|
+
// Cover the exit wait, then restart the requested interval on success.
|
|
1154
|
+
writeRestartHold(shellpuppetHome(), Math.min(600, holdSeconds + 70));
|
|
1155
|
+
}
|
|
1156
|
+
catch (error) {
|
|
1157
|
+
watcher?.close();
|
|
1158
|
+
throw error;
|
|
1159
|
+
}
|
|
1160
|
+
}
|
|
1161
|
+
if (standalone) {
|
|
1162
|
+
try {
|
|
1163
|
+
if (!await waitForStandaloneExit(connection, watcher, originalTick, stopDeadline)) {
|
|
1164
|
+
unproved(`Daemon teardown is still in progress (pid ${String(connection.pid)}, instance ${String(stopInfo?.instanceId)}). Check daemon.log.`);
|
|
1165
|
+
}
|
|
1166
|
+
await reconcilePosixExit();
|
|
1167
|
+
return true;
|
|
1168
|
+
}
|
|
1169
|
+
finally {
|
|
1170
|
+
watcher?.close();
|
|
1171
|
+
}
|
|
1172
|
+
}
|
|
1173
|
+
watcher?.close();
|
|
915
1174
|
// Wait for the port to actually go quiet so restart cannot race it.
|
|
916
1175
|
const deadline = Date.now() + 10_000;
|
|
917
1176
|
while (Date.now() < deadline) {
|