From 91f74db4472218d653097b77907c1f9861077ac2 Mon Sep 17 00:00:00 2001 From: Duang777 Date: Thu, 26 Nov 2026 15:14:20 +0800 Subject: [PATCH] fix(nokv): preserve helper replies until stdout drains Signed-off-by: Duang777 --- ...shared-goal-authority-state-provider-v0.md | 7 ++ ...-goal-authority-state-provider-v0.zh-CN.md | 5 ++ .../coordination/nokv_jsonl_transport.ts | 11 ++- .../nokv_stdout_drain.test.ts | 87 +++++++++++++++++++ tests/fixtures/nokv_jsonl_fake_helper.py | 16 ++++ 5 files changed, 124 insertions(+), 2 deletions(-) create mode 100644 tests/control_plane_ts/nokv_stdout_drain.test.ts diff --git a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md index d52d780fe1..f9bd0ad96b 100644 --- a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md +++ b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.md @@ -54,6 +54,13 @@ RFC or claim formal D2. This checkpoint starts no cohort, soak or promotion. The dated checkpoints below retain source-specific implementation and validation history; instructions to remove still-live writers describe those baselines. +The bounded NoKV helper transport repair preserves complete JSON-lines replies +when process exit precedes stdout drain. Existing requests finish from buffered +replies; new requests to an exited helper are refused. Real subprocess tests +cover read, CAS publication, typed open refusal and incomplete replies. This +qualifies the source transport boundary, not a live NoKV SDK stack, installed +adoption, provider promotion or D2/D3. + ## Historical 1.x implementation context Dated records through 2026-10-12 follow; they retain their original source boundaries. diff --git a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md index 487aef8cc5..c196b949bb 100644 --- a/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md +++ b/docs/architecture/rfcs/shared-goal-authority-state-provider-v0.zh-CN.md @@ -45,6 +45,11 @@ 或晋升。下方具名 checkpoint 保留当时实现和验证历史;“仍活跃 writer 待删除”描述 对应旧基线。 +有界 NoKV helper 传输修复保留进程退出早于 stdout 排空时的完整 JSON-lines 回复。 +已有请求从缓冲回复完成;向已退出 helper 发起的新请求被拒绝。真实子进程测试覆盖 +读取、CAS 发布、带类型的 open 拒绝及不完整回复。这验证源码传输边界,不认证 +真实 NoKV SDK 栈、安装采用、provider 晋升或 D2/D3。 + ## 历史 1.x 实现背景 下方保留截至 2026-10-12 的具名历史记录及各自原源码边界。 diff --git a/loopx/control_plane/coordination/nokv_jsonl_transport.ts b/loopx/control_plane/coordination/nokv_jsonl_transport.ts index 5a784a4d4b..0863d1f7b9 100644 --- a/loopx/control_plane/coordination/nokv_jsonl_transport.ts +++ b/loopx/control_plane/coordination/nokv_jsonl_transport.ts @@ -162,12 +162,16 @@ export class NoKVJsonLinesTransport implements NoKVBlobTransport { // escape the provider boundary. this.child.stderr.on("data", () => {}); this.child.stdin.on("error", () => { - if (!this.closing) this.failUnavailable("NoKV helper stdin failed"); + if (!this.closing && this.child.exitCode === null && this.child.signalCode === null) { + this.failUnavailable("NoKV helper stdin failed"); + } }); this.child.on("error", () => { this.failUnavailable("NoKV helper failed to start"); }); - this.child.on("exit", (code, signal) => { + // Process exit precedes stdio drain. A complete publication reply still + // buffered in stdout must settle its request before disconnection does. + this.child.on("close", (code, signal) => { if (this.closing && this.pending.size === 0) return; this.failUnavailable( `NoKV helper disconnected (code=${String(code)}, signal=${String(signal)})`, @@ -280,6 +284,9 @@ export class NoKVJsonLinesTransport implements NoKVBlobTransport { private async exchange(operation: string, values: JsonObject): Promise { if (this.terminalError) throw this.terminalError; + if (this.child.exitCode !== null || this.child.signalCode !== null) { + throw new NoKVTransportUnavailableError("NoKV helper disconnected"); + } if (this.closing) { throw new NoKVTransportUnavailableError("NoKV helper transport is closed"); } diff --git a/tests/control_plane_ts/nokv_stdout_drain.test.ts b/tests/control_plane_ts/nokv_stdout_drain.test.ts new file mode 100644 index 0000000000..a770b687f0 --- /dev/null +++ b/tests/control_plane_ts/nokv_stdout_drain.test.ts @@ -0,0 +1,87 @@ +import assert from "node:assert/strict"; +import {spawn, type ChildProcessWithoutNullStreams} from "node:child_process"; +import {once} from "node:events"; +import test from "node:test"; +import {fileURLToPath} from "node:url"; + +import {NoKVHelperOpenRefusedError, NoKVJsonLinesTransport} from "../../loopx/control_plane/coordination/nokv_jsonl_transport.ts"; +import {NoKVTransportUnavailableError} from "../../loopx/control_plane/coordination/nokv_authority_store.ts"; +import {resolveTestPython} from "../../scripts/test-python.mjs"; + +const helperPath = fileURLToPath(new URL("../fixtures/nokv_jsonl_fake_helper.py", import.meta.url)); + +for (const operation of ["read", "publish"] as const) { + test(`a completed NoKV ${operation} reply survives process exit before stdout drain`, {timeout: 10_000}, async t => { + let helper: ChildProcessWithoutNullStreams | undefined; + const transport = await NoKVJsonLinesTransport.open({ + argv: [resolveTestPython(), helperPath, "reply_then_exit"], + config: {}, + process_factory(command, args, options) { + helper = spawn(command, args, {...options, stdio: ["pipe", "pipe", "pipe"]}); + return helper; + }, + }); + t.after(async () => {helper?.stdout.resume(); await transport.close();}); + assert.ok(helper); + // Hold pipe delivery while the real helper writes its complete reply and + // exits. Node flushes unread stdio after publishing the process exit event. + helper.stdout.pause(); + const order: string[] = []; + let refusedAfterExit: Promise | undefined; + helper.once("exit", () => { + order.push("exit"); + refusedAfterExit = assert.rejects( + transport.storeIdentity("authority-workbench"), NoKVTransportUnavailableError, + ); + }); + helper.stdout.on("data", () => {order.push("stdout");}); + const closed = once(helper, "close"); + const response = operation === "read" + ? transport.readBlob("authority-workbench", "metadata/head.json") + : transport.casPublishBlob({ + workbench: "authority-workbench", path: "metadata/head.json", + expected_generation: null, expected_workspace_incarnation_id: "a".repeat(32), + bytes: Buffer.from("committed payload"), operation_id: "b".repeat(32), + artifact_revision_id: "c".repeat(32), + }); + const outcome = await response.then(result => ({result}), error => ({error})); + await closed; + await refusedAfterExit; + assert.equal(order[0], "exit"); + assert.ok(order.includes("stdout"), "the completed helper reply must reach the pipe reader"); + assert.deepEqual(outcome, {result: operation === "read" + ? {status: "missing"} : {status: "applied", generation: 1}}); + await assert.rejects(transport.readBlob("authority-workbench", "metadata/head.json"), /disconnected/); + }); +} + +test("a complete NoKV open refusal retains its typed reason after helper exit", {timeout: 10_000}, async () => { + let closed: Promise | undefined; + await assert.rejects(NoKVJsonLinesTransport.open({ + argv: [resolveTestPython(), helperPath, "refuse_then_exit"], + config: {}, + process_factory(command, args, options) { + const helper = spawn(command, args, {...options, stdio: ["pipe", "pipe", "pipe"]}); + helper.stdout.pause(); + closed = once(helper, "close"); + return helper; + }, + }), error => { + assert.ok(error instanceof NoKVHelperOpenRefusedError); + assert.equal(error.reasonCode, "nokv_sdk_capability_mismatch"); + return true; + }); + await closed; +}); + +test("an unterminated NoKV reply remains unavailable after stdout drain", {timeout: 10_000}, async t => { + const transport = await NoKVJsonLinesTransport.open({ + argv: [resolveTestPython(), helperPath, "partial_then_exit"], config: {}, + }); + t.after(async () => {await transport.close();}); + await assert.rejects(transport.readBlob("authority-workbench", "metadata/head.json"), error => { + assert.ok(error instanceof NoKVTransportUnavailableError); + assert.match(error.message, /code=17/); + return true; + }); +}); diff --git a/tests/fixtures/nokv_jsonl_fake_helper.py b/tests/fixtures/nokv_jsonl_fake_helper.py index feb69b1ff7..69b8c4f526 100644 --- a/tests/fixtures/nokv_jsonl_fake_helper.py +++ b/tests/fixtures/nokv_jsonl_fake_helper.py @@ -16,6 +16,16 @@ def emit(value: object) -> None: first = json.loads(sys.stdin.readline()) +if mode == "refuse_then_exit": + emit( + { + "request_id": first["request_id"], + "status": "failed", + "reason_code": "nokv_sdk_capability_mismatch", + "reason": "required SDK publication fence is unavailable", + } + ) + raise SystemExit(17) emit({"request_id": first["request_id"], "status": "ready"}) for line in sys.stdin: @@ -27,6 +37,10 @@ def emit(value: object) -> None: sys.stderr.write("provider-private-diagnostic=must-not-escape\n") sys.stderr.flush() raise SystemExit(17) + if mode == "partial_then_exit": + sys.stdout.write(json.dumps({"request_id": request_id, "status": "missing"})) + sys.stdout.flush() + raise SystemExit(17) if mode == "invalid": emit({"request_id": request_id, "status": "loaded", "generation": True}) continue @@ -82,3 +96,5 @@ def emit(value: object) -> None: "reason": "unknown operation", } ) + if mode == "reply_then_exit": + raise SystemExit(17)