feat(process): bound a finalization interval before forced capture timeout
Add optional finalization_ms to keep observing a captured root after SIGTERM, record the actual finalizer exit and each signal attempt, and escalate once when needed. Bind the initial root identity to captured ownership and preserve observations through cleanup uncertainty. Fixes #1253. Co-authored-by: Mykola Dementiev <16836724+nikolay-dementiev@users.noreply.github.com> Co-authored-by: N0zoM1z0 <161784452+N0zoM1z0@users.noreply.github.com>
This commit is contained in:
co-authored by
Mykola Dementiev
N0zoM1z0
parent
1e51c732bb
commit
c1efa6467d
Vendored
+43
-18
@@ -1,6 +1,7 @@
|
||||
import { Client } from "@modelcontextprotocol/client";
|
||||
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
|
||||
import { Ajv2020 } from "ajv/dist/2020.js";
|
||||
import type { ValidateFunction } from "ajv";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { expect, onTestFinished } from "vitest";
|
||||
import { z } from "zod";
|
||||
@@ -27,31 +28,55 @@ export async function connectLocalToolsMcp() {
|
||||
});
|
||||
await client.connect(transport);
|
||||
const tools = (await client.listTools()).tools;
|
||||
const validators = new Map(
|
||||
tools.flatMap((tool) => {
|
||||
new Ajv2020({ strict: false, validateFormats: false }).compile(
|
||||
tool.inputSchema,
|
||||
const schemaValidator = new Ajv2020({
|
||||
strict: false,
|
||||
validateFormats: false,
|
||||
});
|
||||
for (const tool of tools)
|
||||
for (const [kind, schema] of [
|
||||
["inputSchema", tool.inputSchema],
|
||||
["outputSchema", tool.outputSchema],
|
||||
] as const) {
|
||||
if (schema === undefined) continue;
|
||||
const valid = schemaValidator.validateSchema(
|
||||
z.record(z.string(), z.unknown()).parse(schema),
|
||||
);
|
||||
return tool.outputSchema === undefined
|
||||
? []
|
||||
: [
|
||||
[
|
||||
tool.name,
|
||||
new Ajv2020({ strict: false, validateFormats: false }).compile(
|
||||
z.record(z.string(), z.unknown()).parse(tool.outputSchema),
|
||||
),
|
||||
] as const,
|
||||
];
|
||||
}),
|
||||
);
|
||||
expect(
|
||||
valid,
|
||||
`${tool.name}.${kind}: ${schemaValidator.errorsText(schemaValidator.errors)}`,
|
||||
).toBe(true);
|
||||
}
|
||||
const definitions = new Map(tools.map((tool) => [tool.name, tool]));
|
||||
const compiledInputs = new Set<string>();
|
||||
const validators = new Map<string, ValidateFunction<unknown>>();
|
||||
const call = async (name: string, arguments_: Record<string, unknown>) => {
|
||||
const definition = definitions.get(name);
|
||||
if (definition !== undefined && !compiledInputs.has(name)) {
|
||||
new Ajv2020({ strict: false, validateFormats: false }).compile(
|
||||
definition.inputSchema,
|
||||
);
|
||||
compiledInputs.add(name);
|
||||
}
|
||||
const response = await client.callTool({ name, arguments: arguments_ });
|
||||
const validate = validators.get(name);
|
||||
if (response.structuredContent !== undefined && validate !== undefined)
|
||||
if (
|
||||
response.structuredContent !== undefined &&
|
||||
definition?.outputSchema !== undefined
|
||||
) {
|
||||
let validate = validators.get(name);
|
||||
if (validate === undefined) {
|
||||
validate = new Ajv2020({
|
||||
strict: false,
|
||||
validateFormats: false,
|
||||
}).compile<unknown>(
|
||||
z.record(z.string(), z.unknown()).parse(definition.outputSchema),
|
||||
);
|
||||
validators.set(name, validate);
|
||||
}
|
||||
expect(
|
||||
validate(response.structuredContent),
|
||||
JSON.stringify(validate.errors),
|
||||
).toBe(true);
|
||||
}
|
||||
return response;
|
||||
};
|
||||
return { client, call };
|
||||
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
import { spawn } from "node:child_process";
|
||||
import { mkdirSync, writeFileSync } from "node:fs";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
// Writes a periodic report, then behaves by mode:
|
||||
// - "cooperative": stays alive; SIGTERM prints a line, writes a final report
|
||||
// and exits cleanly.
|
||||
// - "ignoring": stays alive and swallows SIGTERM and SIGINT, so only SIGKILL
|
||||
// ends the process.
|
||||
// - "exits": exits on its own at once.
|
||||
// - "descendant": cooperates while leaving a SIGTERM-ignoring child for owned cleanup.
|
||||
// An optional third argument is the exit code a cooperative finalizer uses.
|
||||
const [mode, directory, finalExitCode = "0"] = process.argv.slice(2);
|
||||
|
||||
writeFileSync(join(directory, "pid.txt"), String(process.pid));
|
||||
writeFileSync(
|
||||
join(directory, "periodic.json"),
|
||||
JSON.stringify({ phase: "periodic" }),
|
||||
);
|
||||
|
||||
if (mode === "exits") process.exit(0);
|
||||
|
||||
if (mode === "descendant") {
|
||||
const childDirectory = join(directory, "descendant");
|
||||
mkdirSync(childDirectory);
|
||||
spawn(
|
||||
process.execPath,
|
||||
[fileURLToPath(import.meta.url), "ignoring", childDirectory],
|
||||
{ detached: true, stdio: "ignore" },
|
||||
);
|
||||
}
|
||||
|
||||
if (mode === "ignoring") {
|
||||
process.on("SIGTERM", () => {
|
||||
writeFileSync(join(directory, "sigterm-received"), "received");
|
||||
});
|
||||
process.on("SIGINT", () => undefined);
|
||||
} else {
|
||||
process.on("SIGTERM", () => {
|
||||
writeFileSync(join(directory, "sigterm-received"), "received");
|
||||
process.stdout.write("finalized\n");
|
||||
writeFileSync(
|
||||
join(directory, "final.json"),
|
||||
JSON.stringify({ phase: "final" }),
|
||||
);
|
||||
process.exit(Number(finalExitCode));
|
||||
});
|
||||
}
|
||||
|
||||
setInterval(() => undefined, 1_000);
|
||||
Reference in New Issue
Block a user