From 9ccfe83ec01f8db9b04d684330a0bc9283014c09 Mon Sep 17 00:00:00 2001 From: Alexander Kucera Date: Thu, 19 Nov 2026 10:34:16 +0100 Subject: [PATCH] refactor(acp): land PR #199 audit fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reviewer fanout (ponytail/security/general) over #199 found no behavior defects. Apply the accepted non-behavioral findings: - requestOutcome helper dedups the two outcome tasks; the two discarded wedge tasks drop their unread do/catch scaffolding - awaitBounded doc names its real bound (window is green-world only; the RED-world watchdog is the fake's /bin/sleep 30 exit) - send() gains a ponytail ceiling comment: queued sends are unbounded and a send racing close() writes onto a closed handle (benign, probe-verified) — bounded by the model's sequential sends; guard if a looping caller appears - request() doc notes a timed-out request's bytes may still land on the wire (late replies dropped, ids never reused) Suite 697/697 green post-change; format+lint clean. --- Museion/ACPEngineClient.swift | 11 +++++ MuseionTests/EngineSmokeTests.swift | 76 +++++++++++++---------------- 2 files changed, 46 insertions(+), 41 deletions(-) diff --git a/Museion/ACPEngineClient.swift b/Museion/ACPEngineClient.swift index 539a0bb..0d0d4a9 100644 --- a/Museion/ACPEngineClient.swift +++ b/Museion/ACPEngineClient.swift @@ -157,6 +157,10 @@ final nonisolated class ACPEngineClient: @unchecked Sendable { /// Send a JSON-RPC request and await its matching response result. /// A request that gets no answer within `timeout` fails — a hung engine /// must surface as an error (→ restart prompt), never an eternal await. + /// brief-27: a timed-out request's bytes may still reach the engine once + /// the child unblocks (the deadline fails the caller, never dequeues the + /// write) — phantom engine work only: ids are never reused and the + /// response path drops unknown ids, so a late reply can never misroute. /// (No default arguments: the only caller is EngineHost, which rides the /// seam — `EngineTransport.request`'s convenience owns the default shape.) func request(_ method: String, params: JSONValue?, timeout: TimeInterval) async throws -> JSONValue? { @@ -261,6 +265,13 @@ final nonisolated class ACPEngineClient: @unchecked Sendable { /// brief-27: the write runs on the WRITER queue, never the state queue — /// a blocked write here starves no deadline and no teardown. private func send(_ data: Data) { + // ponytail: queued sends are unbounded, and a send racing close() writes + // onto a closed handle (benign — write throws a catchable CocoaError 512 + // that try? swallows; probe-verified by two independent reviewers). + // Bounded today by MentorSessionModel's sequential-send discipline (a + // request is awaited before the next; worst case one prompt + one + // cancel). Add a closed/depth guard here if a looping or + // non-MainActor caller ever appears. writerQueue.async { var framed = Data(data) framed.append(0x0A) diff --git a/MuseionTests/EngineSmokeTests.swift b/MuseionTests/EngineSmokeTests.swift index c62ca53..ab44c45 100644 --- a/MuseionTests/EngineSmokeTests.swift +++ b/MuseionTests/EngineSmokeTests.swift @@ -222,11 +222,14 @@ struct EngineSmokeTests { // MARK: - brief-27 backpressure smokes (blocked-writer regression rig) - /// brief-27: bounded await for operations that HANG on the unfixed - /// transport (the RED shape) — nil when the task hasn't finished inside - /// `window` seconds. One deadline per call (the house rule). The losing - /// leg is cancelled; the task itself keeps running and settles at the - /// test's teardown (SIGKILL of the rig child unblocks the writer). + /// brief-27: await an operation that may HANG on the unfixed transport + /// (the RED shape) and report what it answered — nil when the window leg + /// won. One deadline per call (the house rule). The bound is `window` + /// only in the green world: withTaskGroup awaits stragglers at scope + /// exit and `await task.value` is cancellation-blind, so a HUNG task + /// holds this helper until IT settles — the real RED-world watchdog is + /// the fake's `exec /bin/sleep 30` exit (~30 s, inside the rail's 60 s + /// per-test timeout). Keep the fake's sleep short. @MainActor private func awaitBounded(_ task: Task, window: TimeInterval) async -> T? { await withTaskGroup(of: T?.self) { group in @@ -241,6 +244,22 @@ struct EngineSmokeTests { } } + /// brief-27: run one request on a task and string-encode its outcome + /// ("returned" or the error) — the observable the pins match on. + @MainActor + private func requestOutcome( + _ client: ACPEngineClient, method: String, params: JSONValue?, timeout: TimeInterval, + ) -> Task { + Task { + do { + _ = try await client.request(method, params: params, timeout: timeout) + return "returned" + } catch { + return String(describing: error) + } + } + } + /// brief-27: the blocked-writer rig — a real ACPEngineClient over the /// zsh fake in `neverRead` mode, which execs /bin/sleep without ever /// reading stdin (the finding's inert non-reading child; no real @@ -273,14 +292,7 @@ struct EngineSmokeTests { kill(pid, SIGKILL) // settle the blocked pipe FIRST — close's boundedness is the next smoke's pin, not this one's client.close() } - let outcome = Task { - do { - _ = try await client.request("session/prompt", params: blockedWriterPayload, timeout: 0.5) - return "returned" - } catch { - return String(describing: error) - } - } + let outcome = requestOutcome(client, method: "session/prompt", params: blockedWriterPayload, timeout: 0.5) let result = await awaitBounded(outcome, window: 5) #expect( result?.contains("timed out") == true, @@ -303,29 +315,17 @@ struct EngineSmokeTests { kill(pid, SIGKILL) client.close() } - _ = Task { - do { - _ = try await client.request("session/prompt", params: blockedWriterPayload, timeout: 8) - } catch { - return String(describing: error) - } - return "returned" - } + // Wedge the writer: the oversized prompt fills the pipe (outcome unobserved). + Task { _ = try? await client.request("session/prompt", params: blockedWriterPayload, timeout: 8) } // Let the oversized write reach the pipe and wedge the writer first. try await Task.sleep(nanoseconds: 200_000_000) client.notify("session/cancel", params: .object(["sessionId": .string("sess-1")])) - let second = Task { - do { - _ = try await client.request( - "session/new", - params: .object(["cwd": .string("/tmp"), "mcpServers": .array([])]), - timeout: 0.5, - ) - return "returned" - } catch { - return String(describing: error) - } - } + let second = requestOutcome( + client, + method: "session/new", + params: .object(["cwd": .string("/tmp"), "mcpServers": .array([])]), + timeout: 0.5, + ) let result = await awaitBounded(second, window: 5) #expect( result?.contains("timed out") == true, @@ -345,14 +345,8 @@ struct EngineSmokeTests { kill(pid, SIGKILL) client.close() } - _ = Task { - do { - _ = try await client.request("session/prompt", params: blockedWriterPayload, timeout: 8) - } catch { - return String(describing: error) - } - return "returned" - } + // Wedge the writer: the oversized prompt fills the pipe (outcome unobserved). + Task { _ = try? await client.request("session/prompt", params: blockedWriterPayload, timeout: 8) } // The write is in the pipe and the writer wedged before close runs. try await Task.sleep(nanoseconds: 200_000_000) let closeTask = Task { client.close() }