@henols/c64-re-tools 2.2.0 → 2.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/host/main.ts +3 -1
- package/src/host/server.ts +7 -7
- package/src/host/vice/session.ts +4 -1
- package/src/mcp/main.ts +3 -3
- package/src/native/c64run.ts +34 -4
- package/src/native/local.ts +2 -2
- package/src/native/processes.ts +20 -3
package/package.json
CHANGED
package/src/host/main.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
#!/usr/bin/env tsx
|
|
2
2
|
|
|
3
|
+
import { constants } from "node:os";
|
|
3
4
|
import { parseArgs } from "node:util";
|
|
4
5
|
|
|
5
6
|
import { DEFAULT_HOST_PORT } from "../protocol/messages.ts";
|
|
@@ -138,7 +139,8 @@ async function main(): Promise<number> {
|
|
|
138
139
|
process.stdout.write(`c64-re-tools-host listening on ${address}\n`);
|
|
139
140
|
trace().event("host.listening", { addresses: [address] });
|
|
140
141
|
|
|
141
|
-
|
|
142
|
+
// A second signal exits at once: the exit guard SIGKILLs every group that is still running.
|
|
143
|
+
trace().event("host.stop", { signal: await stopSignal(log, (signal) => process.exit(128 + constants.signals[signal])) });
|
|
142
144
|
await server.close();
|
|
143
145
|
await supervisor.stopAll();
|
|
144
146
|
return 0;
|
package/src/host/server.ts
CHANGED
|
@@ -308,24 +308,24 @@ export function parsePort(text: string): number | undefined {
|
|
|
308
308
|
}
|
|
309
309
|
|
|
310
310
|
/**
|
|
311
|
-
* Resolves with the first SIGINT or
|
|
312
|
-
* signal is logged and
|
|
313
|
-
*
|
|
311
|
+
* Resolves with the first SIGINT, SIGTERM or SIGHUP. The handlers stay: a
|
|
312
|
+
* later signal is logged and calls again, which stops the host at once
|
|
313
|
+
* instead of waiting for the graceful stop of each emulator and tool.
|
|
314
314
|
*/
|
|
315
|
-
export function stopSignal(log: (line: string) => void): Promise<NodeJS.Signals> {
|
|
315
|
+
export function stopSignal(log: (line: string) => void, again: (signal: NodeJS.Signals) => void): Promise<NodeJS.Signals> {
|
|
316
316
|
return new Promise((resolve) => {
|
|
317
317
|
let stopping = false;
|
|
318
318
|
const stop = (signal: NodeJS.Signals) => {
|
|
319
319
|
if (stopping) {
|
|
320
|
-
log(`received ${signal} again;
|
|
320
|
+
log(`received ${signal} again; stopping at once`);
|
|
321
|
+
again(signal);
|
|
321
322
|
return;
|
|
322
323
|
}
|
|
323
324
|
stopping = true;
|
|
324
325
|
log(`received ${signal}, stopping`);
|
|
325
326
|
resolve(signal);
|
|
326
327
|
};
|
|
327
|
-
|
|
328
|
-
process.on("SIGTERM", stop);
|
|
328
|
+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.on(signal, stop);
|
|
329
329
|
});
|
|
330
330
|
}
|
|
331
331
|
|
package/src/host/vice/session.ts
CHANGED
|
@@ -288,7 +288,9 @@ export class ViceSession implements ViceSessionHandle {
|
|
|
288
288
|
/**
|
|
289
289
|
* A VICE ended or dropped its monitor without the session asking. A window
|
|
290
290
|
* VICE goes back headless from the state of when the window opened;
|
|
291
|
-
* any other loss is final
|
|
291
|
+
* any other loss is final, and the lost VICE is stopped at once: a VICE
|
|
292
|
+
* that hangs without its monitor ends, and its group and scratch directory
|
|
293
|
+
* do not wait for the session to close.
|
|
292
294
|
*/
|
|
293
295
|
#ended(vice: ViceProcess, why: string): void {
|
|
294
296
|
if (this.#closed || this.#vice !== vice || this.#lost || this.#windowClosing) return;
|
|
@@ -302,6 +304,7 @@ export class ViceSession implements ViceSessionHandle {
|
|
|
302
304
|
return;
|
|
303
305
|
}
|
|
304
306
|
this.#lost = true;
|
|
307
|
+
void vice.stop().catch((error: unknown) => this.#log(`stopping the lost VICE failed: ${error instanceof Error ? error.message : String(error)}`));
|
|
305
308
|
}
|
|
306
309
|
|
|
307
310
|
/** Restarts headless from the handover saved when the window opened. */
|
package/src/mcp/main.ts
CHANGED
|
@@ -123,10 +123,10 @@ async function main(): Promise<void> {
|
|
|
123
123
|
process.exit(0);
|
|
124
124
|
};
|
|
125
125
|
// The harness ends the server by closing stdin or by a signal; both end the session and its emulator.
|
|
126
|
+
// Only the client holds the writing end of stdin: its end, close or error means that the client is gone.
|
|
126
127
|
server.onclose = () => void stop();
|
|
127
|
-
process.stdin.once(
|
|
128
|
-
|
|
129
|
-
process.once("SIGTERM", () => void stop());
|
|
128
|
+
for (const event of ["end", "close", "error"] as const) process.stdin.once(event, () => void stop());
|
|
129
|
+
for (const signal of ["SIGINT", "SIGTERM", "SIGHUP"] as const) process.once(signal, () => void stop());
|
|
130
130
|
|
|
131
131
|
await server.connect(new StdioServerTransport());
|
|
132
132
|
}
|
package/src/native/c64run.ts
CHANGED
|
@@ -18,6 +18,21 @@ export const C64RUN_PROTOCOL = 2;
|
|
|
18
18
|
export const PAL_FRAME_CYCLES = 19_656;
|
|
19
19
|
|
|
20
20
|
const STOP_MS = 5_000;
|
|
21
|
+
/** How long c64run may take to answer a command, beyond the run up to a target tick. */
|
|
22
|
+
const COMMAND_MS = 30_000;
|
|
23
|
+
/** The slowest run that a command limit allows for: c64run runs about 6 million cycles per second. */
|
|
24
|
+
const SLOWEST_CYCLES_PER_SECOND = 1_000_000;
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* The time limit of a command, so that a core that hangs cannot keep the
|
|
28
|
+
* script running. run, run-pc and run-reads end with the absolute tick that
|
|
29
|
+
* they run to at most; the others answer at once.
|
|
30
|
+
*/
|
|
31
|
+
export function commandLimitMs(text: string): number {
|
|
32
|
+
const [name, ...rest] = text.split(" ");
|
|
33
|
+
const target = name === "run" || name === "run-pc" || name === "run-reads" ? Number(rest.at(-1)) : 0;
|
|
34
|
+
return COMMAND_MS + (Number.isFinite(target) ? (target * 1000) / SLOWEST_CYCLES_PER_SECOND : 0);
|
|
35
|
+
}
|
|
21
36
|
|
|
22
37
|
/** The machine state that most commands answer with. */
|
|
23
38
|
export interface CoreStatus {
|
|
@@ -82,7 +97,8 @@ export class C64RunSession {
|
|
|
82
97
|
createInterface({ input: child.stdout! }).on("line", (line) => this.#waiting.shift()?.resolve(line));
|
|
83
98
|
void process.exited.then((status) => {
|
|
84
99
|
const how = status.spawnError !== undefined ? `did not start (${status.spawnError})` : `ended (exit ${status.code ?? status.signal})`;
|
|
85
|
-
|
|
100
|
+
// A core stopped because it did not answer keeps that reason.
|
|
101
|
+
this.#ended ??= new WireFailure(status.spawnError !== undefined ? "installation-incomplete" : "operation-failed", `c64run ${how}.${this.#stderr === "" ? "" : ` ${this.#stderr.trim()}`}`);
|
|
86
102
|
for (const waiting of this.#waiting.splice(0)) waiting.reject(this.#ended);
|
|
87
103
|
});
|
|
88
104
|
}
|
|
@@ -103,10 +119,24 @@ export class C64RunSession {
|
|
|
103
119
|
return session;
|
|
104
120
|
}
|
|
105
121
|
|
|
106
|
-
/**
|
|
107
|
-
|
|
122
|
+
/**
|
|
123
|
+
* Sends one command and gives its "ok ..." answer. An "err ..." answer throws operation-failed.
|
|
124
|
+
* A core that does not answer within the limit is stopped: every command then fails.
|
|
125
|
+
*/
|
|
126
|
+
command(text: string, limitMs = commandLimitMs(text)): Promise<string> {
|
|
108
127
|
if (this.#ended !== undefined) return Promise.reject(this.#ended);
|
|
109
|
-
const answer = new Promise<string>((resolve, reject) =>
|
|
128
|
+
const answer = new Promise<string>((resolve, reject) => {
|
|
129
|
+
const timer = setTimeout(() => {
|
|
130
|
+
this.#ended = new WireFailure("operation-failed", `c64run ${text.split(" ")[0]} did not answer in ${Math.ceil(limitMs / 1000)} s; the core is stopped.`);
|
|
131
|
+
for (const waiting of this.#waiting.splice(0)) waiting.reject(this.#ended);
|
|
132
|
+
void this.#process.stop().catch(() => {});
|
|
133
|
+
}, limitMs);
|
|
134
|
+
const done = () => clearTimeout(timer);
|
|
135
|
+
this.#waiting.push({
|
|
136
|
+
resolve: (line) => (done(), resolve(line)),
|
|
137
|
+
reject: (error) => (done(), reject(error)),
|
|
138
|
+
});
|
|
139
|
+
});
|
|
110
140
|
this.#process.child.stdin!.write(`${text}\n`);
|
|
111
141
|
return answer.then((line) => {
|
|
112
142
|
if (line.startsWith("ok")) return line;
|
package/src/native/local.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// The context for running native tools inside a skill script or the CLI:
|
|
2
2
|
// one supervisor whose exit guard stops every tool process group when
|
|
3
|
-
// the script ends, also on Ctrl+C or
|
|
3
|
+
// the script ends, also on Ctrl+C, SIGTERM or SIGHUP, and whose watchdog does the same
|
|
4
4
|
// when the script dies without its exit guard, for example by SIGKILL.
|
|
5
5
|
// A line that a tool adapter logs goes to the trace of the script.
|
|
6
6
|
|
|
@@ -23,7 +23,7 @@ export function localToolContext(): LocalToolContext {
|
|
|
23
23
|
supervisor.startWatchdog("skill-script");
|
|
24
24
|
const controller = new AbortController();
|
|
25
25
|
// A signal ends the script through process.exit, so the exit guard runs.
|
|
26
|
-
for (const [signal, code] of [["SIGINT", 130], ["SIGTERM", 143]] as const) {
|
|
26
|
+
for (const [signal, code] of [["SIGINT", 130], ["SIGTERM", 143], ["SIGHUP", 129]] as const) {
|
|
27
27
|
process.once(signal, () => {
|
|
28
28
|
controller.abort();
|
|
29
29
|
process.exit(code);
|
package/src/native/processes.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { spawn, spawnSync, type ChildProcess, type StdioOptions } from "node:child_process";
|
|
2
2
|
import type { Writable } from "node:stream";
|
|
3
|
-
import { mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
3
|
+
import { existsSync, mkdtempSync, readdirSync, readFileSync, renameSync, rmSync, writeFileSync } from "node:fs";
|
|
4
4
|
import { tmpdir } from "node:os";
|
|
5
5
|
import { join } from "node:path";
|
|
6
6
|
import { setTimeout as sleep } from "node:timers/promises";
|
|
@@ -14,6 +14,8 @@ export const STOP_GRACE_MS = 5_000;
|
|
|
14
14
|
/** How long to wait for the group to vanish after SIGKILL before giving up. */
|
|
15
15
|
const KILL_WAIT_MS = 2_000;
|
|
16
16
|
const POLL_MS = 25;
|
|
17
|
+
/** How many times a killed watchdog is replaced. A watchdog that cannot run at all must not start new ones forever. */
|
|
18
|
+
const WATCHDOG_RESPAWNS = 5;
|
|
17
19
|
|
|
18
20
|
export interface SpawnOptions {
|
|
19
21
|
cwd?: string;
|
|
@@ -170,6 +172,7 @@ export class ProcessSupervisor {
|
|
|
170
172
|
/** The registry file that the watchdog reads, once a watchdog runs. */
|
|
171
173
|
#registry: string | undefined;
|
|
172
174
|
#watchdogPid: number | undefined;
|
|
175
|
+
#watchdogRespawns = 0;
|
|
173
176
|
/** The writing end of the watchdog's pipe. Kept open until this process ends: its end tells the watchdog that this process is gone. */
|
|
174
177
|
#watchdogPipe: Writable | undefined;
|
|
175
178
|
|
|
@@ -270,10 +273,20 @@ export class ProcessSupervisor {
|
|
|
270
273
|
const directory = mkdtempSync(join(tmpdir(), `c64-re-tools-${owner}-watchdog-`));
|
|
271
274
|
this.#registry = join(directory, "registry.json");
|
|
272
275
|
this.#writeRegistry();
|
|
276
|
+
this.#spawnWatchdog(this.#registry);
|
|
277
|
+
this.installExitGuard();
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
/**
|
|
281
|
+
* A watchdog that ends while its registry is still there did not end by
|
|
282
|
+
* itself (it removes the registry when it cleans up, as the exit guard does):
|
|
283
|
+
* something killed it. A new one takes over the same registry, a few times at most.
|
|
284
|
+
*/
|
|
285
|
+
#spawnWatchdog(registry: string): void {
|
|
273
286
|
// The watchdog runs in the temporary directory, never in the caller's directory: Windows refuses
|
|
274
287
|
// to remove a directory that a running process uses. Its standard input is a pipe from this
|
|
275
288
|
// process, which the system closes when this process ends.
|
|
276
|
-
const watchdog = spawn(process.execPath, [...process.execArgv, WATCHDOG, String(process.pid),
|
|
289
|
+
const watchdog = spawn(process.execPath, [...process.execArgv, WATCHDOG, String(process.pid), registry], { cwd: tmpdir(), detached: true, stdio: ["pipe", "ignore", "ignore"], windowsHide: true });
|
|
277
290
|
watchdog.unref();
|
|
278
291
|
// The pipe does not keep this process running, and an error on it does not end this process.
|
|
279
292
|
const pipe = watchdog.stdin as (Writable & { unref?: () => void }) | null;
|
|
@@ -281,7 +294,11 @@ export class ProcessSupervisor {
|
|
|
281
294
|
pipe?.unref?.();
|
|
282
295
|
this.#watchdogPipe = pipe ?? undefined;
|
|
283
296
|
this.#watchdogPid = watchdog.pid;
|
|
284
|
-
|
|
297
|
+
watchdog.once("exit", () => {
|
|
298
|
+
if (!existsSync(registry) || this.#watchdogRespawns >= WATCHDOG_RESPAWNS) return;
|
|
299
|
+
this.#watchdogRespawns++;
|
|
300
|
+
this.#spawnWatchdog(registry);
|
|
301
|
+
});
|
|
285
302
|
}
|
|
286
303
|
|
|
287
304
|
#writeRegistry(): void {
|