mirror of
https://github.com/binaricat/Netcatty.git
synced 2026-11-20 07:04:53 +00:00
fix(perf): unify SFTP lifecycles and bound runtime resources
This commit is contained in:
1 parent
49918fd54a
commit
fe737ec555
240 files changed
+23968
-4865
No files matched your search
@@ -4,9 +4,15 @@ const test = require("node:test");
|
||||
const assert = require("node:assert/strict");
|
||||
const {
|
||||
createScriptRuntime,
|
||||
SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS,
|
||||
SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS,
|
||||
SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS,
|
||||
SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS,
|
||||
wrapScriptSource,
|
||||
interruptibleSleep,
|
||||
normalizeDialogFormSpec,
|
||||
_getActiveScriptWorkerCountForTests,
|
||||
_getActiveScriptHostRequestCountForTests,
|
||||
} = require("./scriptRuntime.cjs");
|
||||
const { SessionOutputBuffer } = require("./sessionOutputBuffer.cjs");
|
||||
|
||||
@@ -89,6 +95,299 @@ test("createScriptRuntime executes simple log script", async () => {
|
||||
assert.deepEqual(logs, ["hello"]);
|
||||
});
|
||||
|
||||
test("createScriptRuntime releases its isolated worker after normal completion", async () => {
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-bounded-sync",
|
||||
appendLog: () => {},
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
});
|
||||
|
||||
await runtime.execute("void 0;");
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime stops a synchronous infinite loop", async () => {
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-infinite-loop",
|
||||
appendLog: () => {},
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
syncExecutionTimeoutMs: 20,
|
||||
});
|
||||
|
||||
await assert.rejects(runtime.execute("while (true) {}"), /timed out/i);
|
||||
});
|
||||
|
||||
test("createScriptRuntime stop forcibly terminates a blocked worker", async () => {
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-explicit-stop",
|
||||
appendLog: () => {},
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
syncExecutionTimeoutMs: 5_000,
|
||||
});
|
||||
|
||||
const startedAt = Date.now();
|
||||
const execution = runtime.execute(`
|
||||
await nct.sleep(1);
|
||||
/^(a+)+$/.test('a'.repeat(30) + '!');
|
||||
`);
|
||||
setTimeout(() => runtime.stop(new Error("Stopped by user")), 50);
|
||||
await assert.rejects(execution, /stopped by user/i);
|
||||
assert.ok(Date.now() - startedAt < 1_000, "explicit stop did not terminate the worker promptly");
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime enforces the worker deadline across promise continuations", async () => {
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-post-await-infinite-loop",
|
||||
appendLog: () => {},
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
syncExecutionTimeoutMs: 20,
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
runtime.execute("await Promise.resolve(); while (true) {}"),
|
||||
/timed out/i,
|
||||
);
|
||||
await assert.rejects(
|
||||
runtime.execute("await nct.sleep(1); while (true) {}"),
|
||||
/timed out/i,
|
||||
);
|
||||
await assert.rejects(
|
||||
runtime.execute(`
|
||||
await nct.sleep(1);
|
||||
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 250);
|
||||
`),
|
||||
/blocking atomics|sharedarraybuffer/i,
|
||||
);
|
||||
const startedAt = Date.now();
|
||||
await assert.rejects(
|
||||
runtime.execute(`
|
||||
await nct.sleep(1);
|
||||
/^(a+)+$/.test('a'.repeat(28) + '!');
|
||||
`),
|
||||
/timed out/i,
|
||||
);
|
||||
assert.ok(Date.now() - startedAt < 1_000, "catastrophic regexp was not terminated promptly");
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime drains normal host-backed promise continuations", async () => {
|
||||
const logs = [];
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-post-await-normal",
|
||||
appendLog: (_id, message) => logs.push(message),
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
syncExecutionTimeoutMs: 20,
|
||||
});
|
||||
|
||||
await runtime.execute(`
|
||||
await nct.sleep(1);
|
||||
nct.log('first');
|
||||
await Promise.resolve();
|
||||
await nct.sleep(1);
|
||||
nct.log(nct.session.connected ? 'second' : 'disconnected');
|
||||
`);
|
||||
assert.deepEqual(logs, ["first", "second"]);
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime heartbeat remains healthy while a host request is paused", async () => {
|
||||
let paused = true;
|
||||
const writes = [];
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-paused-host-request",
|
||||
appendLog: () => {},
|
||||
writeToSession: (_sessionId, data) => writes.push(data),
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => paused,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
syncExecutionTimeoutMs: 20,
|
||||
});
|
||||
|
||||
const execution = runtime.execute("await nct.screen.send('after-pause');");
|
||||
setTimeout(() => { paused = false; }, 80);
|
||||
await execution;
|
||||
assert.deepEqual(writes, ["after-pause"]);
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime terminates unawaited host requests at the worker boundary", async () => {
|
||||
let finishDialog;
|
||||
let disconnectCalls = 0;
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-unawaited-request",
|
||||
appendLog: () => {},
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: () => new Promise((resolve) => { finishDialog = resolve; }),
|
||||
disconnectSession: async () => { disconnectCalls += 1; },
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
});
|
||||
|
||||
await runtime.execute(`
|
||||
void nct.dialog.confirm('background').then(() => nct.session.disconnect());
|
||||
nct.log('done');
|
||||
`);
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
const cleanupStartedAt = Date.now();
|
||||
while (_getActiveScriptHostRequestCountForTests() > 0 && Date.now() - cleanupStartedAt < 500) {
|
||||
await new Promise((resolve) => setTimeout(resolve, 10));
|
||||
}
|
||||
assert.equal(_getActiveScriptHostRequestCountForTests(), 0);
|
||||
finishDialog(true);
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
assert.equal(disconnectCalls, 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime terminates a 20k unawaited host-request fan-out at the pending limit", async () => {
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-host-request-fanout",
|
||||
appendLog: () => {},
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
syncExecutionTimeoutMs: 5_000,
|
||||
});
|
||||
|
||||
const startedAt = Date.now();
|
||||
await assert.rejects(
|
||||
runtime.execute("for (let i = 0; i < 20000; i += 1) void nct.sleep(60000);"),
|
||||
new RegExp(`${SCRIPT_WORKER_MAX_PENDING_HOST_REQUESTS} pending host request limit`, "i"),
|
||||
);
|
||||
assert.ok(Date.now() - startedAt < 1_000, "fan-out was not stopped promptly");
|
||||
assert.equal(_getActiveScriptHostRequestCountForTests(), 0);
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime still permits normal sequential host requests", async () => {
|
||||
const logs = [];
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-sequential-host-requests",
|
||||
appendLog: (_id, message) => logs.push(message),
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => {},
|
||||
});
|
||||
|
||||
await runtime.execute(`
|
||||
for (let i = 0; i < 5; i += 1) await nct.sleep(0);
|
||||
nct.log('done');
|
||||
`);
|
||||
assert.deepEqual(logs, ["done"]);
|
||||
assert.equal(_getActiveScriptHostRequestCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime bounds a 200k log notification flood", async () => {
|
||||
let deliveredLogs = 0;
|
||||
let statusUpdates = 0;
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-log-flood",
|
||||
appendLog: () => { deliveredLogs += 1; },
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => { statusUpdates += 1; },
|
||||
syncExecutionTimeoutMs: 5_000,
|
||||
});
|
||||
|
||||
const startedAt = Date.now();
|
||||
await assert.rejects(
|
||||
runtime.execute("for (let i = 0; i < 200000; i += 1) nct.log('x');"),
|
||||
new RegExp(`${SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS} log notification limit`, "i"),
|
||||
);
|
||||
assert.ok(Date.now() - startedAt < 1_000);
|
||||
assert.equal(deliveredLogs, SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS);
|
||||
assert.equal(statusUpdates, SCRIPT_WORKER_MAX_LOG_NOTIFICATIONS);
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("createScriptRuntime coalesces large progress floods before the total budget", async () => {
|
||||
let statusUpdates = 0;
|
||||
const runtime = createScriptRuntime({
|
||||
sessionId: "s1",
|
||||
runId: "r-progress-flood",
|
||||
appendLog: () => {},
|
||||
writeToSession: () => {},
|
||||
getOutputBuffer: () => ({ getText: () => "" }),
|
||||
getSessionMeta: () => ({ connected: true }),
|
||||
showDialog: async () => true,
|
||||
isPaused: () => false,
|
||||
isAborted: () => false,
|
||||
onStatusChange: () => { statusUpdates += 1; },
|
||||
syncExecutionTimeoutMs: 5_000,
|
||||
});
|
||||
|
||||
await assert.rejects(
|
||||
runtime.execute(`
|
||||
nct.progress.start('many', 200000);
|
||||
for (let i = 0; i < 200000; i += 1) nct.progress.step('item ' + i);
|
||||
`),
|
||||
new RegExp(`${SCRIPT_WORKER_MAX_TOTAL_NOTIFICATIONS} notification limit`, "i"),
|
||||
);
|
||||
assert.ok(
|
||||
statusUpdates <= SCRIPT_WORKER_IMMEDIATE_PROGRESS_NOTIFICATIONS + 4,
|
||||
`progress broadcast count was not coalesced: ${statusUpdates}`,
|
||||
);
|
||||
assert.equal(_getActiveScriptWorkerCountForTests(), 0);
|
||||
});
|
||||
|
||||
test("sensitive script input is masked in UI and logs and remains host-bypassed", async () => {
|
||||
const logs = [];
|
||||
const writes = [];
|
||||
|
||||
Reference in new issue
Block a user