diff --git a/.gitignore b/.gitignore index 0f2d94fe..5432854d 100644 --- a/.gitignore +++ b/.gitignore @@ -4,6 +4,7 @@ mcp-repl-startup.log .Rhistory .Rproj.user .venv +__pycache__/ /eval .agents tools/drain-* diff --git a/AGENTS.md b/AGENTS.md index f5b41c17..89ee7de0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -48,7 +48,7 @@ Keep this file short. It is a table of contents, not the full manual. - Sandbox metadata: Codex per-tool-call `_meta["codex/sandbox-state-meta"]` used by `--sandbox inherit` to choose the effective worker sandbox for that call. - Writable root: An absolute path that a `workspace-write` worker may write, subject to forced read-only subpaths like `.git`, `.codex`, and `.agents`. - Session temp directory: The server-allocated per-session temp path exposed to the worker as `TMPDIR` and `MCP_REPL_R_SESSION_TMPDIR`. -- Sideband IPC: The JSON-lines server/worker pipe for structural facts such as `readline_start`, `readline_input`, `readline_discard`, `output_text`, `plot_image`, and `session_end`. +- Sideband IPC: The JSON-lines server/worker pipe for structural facts such as `readline_start`, `readline_input_bytes`, `readline_discard_bytes`, `output_text`, `output_image`, and `session_end`. - Raw output capture: The stdout/stderr pipes or PTY stream captured by the server for unowned visible text. Sideband carries worker-owned text and structural facts. - Output timeline: The server-side reconstruction of visible output order from captured stdout/stderr plus sideband facts. - Server-owned: State, files, or notices created and retained by the main server process, not by the runtime or the worker. Use this for output bundles, response finalization, debug logs, and server temp roots. diff --git a/Cargo.toml b/Cargo.toml index 7b41a82f..fb25fcdd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -41,7 +41,7 @@ landlock = "0.4.4" seccompiler = "0.5.0" -[target.'cfg(unix)'.dependencies] +[target.'cfg(any(unix, windows))'.dependencies] portable-pty = "0.9.0" [target.'cfg(target_os = "macos")'.dependencies] @@ -56,6 +56,7 @@ "Win32_Security_Authorization", "Win32_Storage_FileSystem", "Win32_System_Console", + "Win32_System_Environment", "Win32_System_IO", "Win32_System_JobObjects", "Win32_System_Pipes", diff --git a/docs/architecture.md b/docs/architecture.md index b2c57f12..3c19d5fd 100644 --- a/docs/architecture.md +++ b/docs/architecture.md @@ -29,18 +29,20 @@ The repository is organized around a few concrete subsystems rather than deep pa - `src/backend.rs` selects between the R and Python implementations at launch and install/configuration boundaries. - Worker launch chooses the runtime stdin transport up front. R and the default - protocol-worker path use pipes; built-in Unix Python uses PTY-backed C - stdin/stdout/stderr so CPython takes its normal interactive readline path. + protocol-worker path use pipes; built-in Python uses PTY-backed C + stdin/stdout/stderr where the platform launch supports it so CPython takes + its normal interactive readline path. On Windows sandboxed Python, the server + uses pipes to the sandbox wrapper and the wrapper creates ConPTY for the + restricted Python child, so CPython still sees a console inside the sandbox. - Both backends receive request payloads through worker stdin and use sideband IPC for structured facts. R owns stdin through a worker reader thread keyed by - payload byte length. Unix Python lets CPython own stdin through + payload byte length. PTY-backed Python lets CPython own stdin through `PyOS_ReadlineFunctionPointer`; the callback reports `readline_start`, - `readline_input`, and `readline_discard` accounting facts. Its legacy - `stdin_write_ack` frames acknowledge request-boundary setup, not prompt - completion or output delivery. + `readline_input_bytes`, and `readline_discard_bytes` accounting facts using + the exact bytes received over worker stdin before interpreter normalization. - The IPC sideband is single-owner by design: startup env vars only bootstrap the main worker, then they are scrubbed before user code runs. Descendants must not emit sideband messages. - R-specific behavior lives in `src/r_session.rs`, `src/r_controls.rs`, `src/r_graphics.rs`, and `src/r_htmd.rs`. -- Python-specific behavior lives in `src/python_ffi.rs`, `src/python_session.rs`, `src/python_worker.rs`, and `python/embedded.py`. Python worker mode dynamically loads CPython only after the worker has selected the Python backend, so R worker mode does not load Python. On the Unix PTY path, Python leaves CPython's fd-backed stdin surface intact; direct fd stdin consumers are not a request-completion contract. +- Python-specific behavior lives in `src/python_ffi.rs`, `src/python_session.rs`, `src/python_worker.rs`, and `python/embedded.py`. Python worker mode dynamically loads CPython only after the worker has selected the Python backend, so R worker mode does not load Python. On the Unix PTY path, Python leaves CPython's fd-backed stdin surface intact; Windows keeps sideband-aware direct-stdin bridges for the ConPTY path so CRLF and console reads remain accountably tied to active MCP input. ### Sandbox and process isolation diff --git a/docs/futurework/advisory-worker-write-observations.md b/docs/futurework/advisory-worker-write-observations.md index 3c5b5bbe..1cc453fc 100644 --- a/docs/futurework/advisory-worker-write-observations.md +++ b/docs/futurework/advisory-worker-write-observations.md @@ -17,7 +17,7 @@ just "readline happened": - they know which stream is being written, - they know the exact byte slice being written, - they know the local callback order relative to other worker-side events such - as `readline_result` and `plot_image`. + as byte-level readline accounting and `output_image`. That information is incomplete, but it may still be useful. diff --git a/docs/futurework/r-graphics-device-for-incremental-plot-emission.md b/docs/futurework/r-graphics-device-for-incremental-plot-emission.md index 1b3bd94b..99829d06 100644 --- a/docs/futurework/r-graphics-device-for-incremental-plot-emission.md +++ b/docs/futurework/r-graphics-device-for-incremental-plot-emission.md @@ -57,7 +57,7 @@ screen” signal. That means server-side timeline fixes can only help after the image exists; they cannot make an image arrive earlier than the worker emits it. That limitation is separate from ordinary plot/stdout ordering across separate -input lines. The server timeline can already place an emitted `plot_image` +input lines. The server timeline can already place an emitted `output_image` before later stdout when the sideband facts contain that ordering. ## Investigation Outcome diff --git a/docs/futurework/r-worker-simplification-and-server-inferred-completion.md b/docs/futurework/r-worker-simplification-and-server-inferred-completion.md index 6a2dc3a9..74f91641 100644 --- a/docs/futurework/r-worker-simplification-and-server-inferred-completion.md +++ b/docs/futurework/r-worker-simplification-and-server-inferred-completion.md @@ -6,7 +6,7 @@ This future work item covers the larger simplification goal behind the current o - keep the R worker thin and factual, - let it run the ordinary embedded REPL, -- emit runtime facts such as `readline_start`, `readline_result`, and plot/image events over IPC, +- emit runtime facts such as `readline_start`, byte-level input accounting, and image events over IPC, - move request-boundary interpretation and timeline reconstruction into the server. This is intentionally broader than the current branch milestone. diff --git a/docs/futurework/server-backend-boundary.md b/docs/futurework/server-backend-boundary.md index acb66ed3..c0721467 100644 --- a/docs/futurework/server-backend-boundary.md +++ b/docs/futurework/server-backend-boundary.md @@ -51,11 +51,10 @@ buckets: is documentation wiring, not request execution policy, and should move with `docs/futurework/composable-tool-descriptions.md`. - `src/worker_process.rs` selects a `BackendDriver` at `WorkerManager` - creation. The driver owns backend-specific request metadata such as Python - newline normalization, Python `line_count`, whether to wait for - `stdin_write_ack`, interrupt behavior, completion waiting, and backend-info + creation. The driver owns backend-specific adapter details such as Python + newline normalization, interrupt behavior, completion waiting, and worker startup tolerance. This is acceptable only as a server-side adapter until the - worker can advertise these narrow capabilities. + worker protocol can make these narrow capabilities generic. - `src/worker_process.rs` also branches at spawn time to configure R worker mode or Python worker mode in the same `mcp-repl` executable. Python launch setup additionally resolves the selected interpreter executable and loadable @@ -88,12 +87,10 @@ semantics, not implementation language. Initial candidates: -- `supports_images`: already present in `backend_info`; controls whether image - events are expected. -- `stdin_write_ack`: whether the server must wait for worker acceptance after - `stdin_write` before writing raw stdin bytes. -- `backend_info_startup_timeout`: whether startup may continue after a short - backend-info timeout. +- `supports_images`: reported at worker startup; controls whether image events + are expected. +- `worker_ready_startup_timeout`: whether startup may continue after a short + worker-ready timeout. - `timeout_output_settle`: whether a timed-out request needs an additional output-settle window before the server returns the timeout reply. diff --git a/docs/futurework/stdin-transport-single-owner.md b/docs/futurework/stdin-transport-single-owner.md index c3c20115..c21d1b27 100644 --- a/docs/futurework/stdin-transport-single-owner.md +++ b/docs/futurework/stdin-transport-single-owner.md @@ -14,20 +14,21 @@ The remaining follow-up is broader than that point fix. We still need to tighten - The problem is not "piped stdin is always broken". The hang showed up when another thread was already blocked on the same stdin pipe. - Future embedded interpreters can run into similar issues if worker stdin ownership drifts again. -- The current R and Python paths now keep stdin raw and use sideband metadata - for request boundaries, but their in-worker stdin ownership differs. +- The current R and Python paths now keep stdin raw and use sideband facts for + prompt state plus byte-level input accounting, but their in-worker stdin + ownership differs. ## Current Scope -This repo now uses raw stdin for worker payloads and sideband IPC for request -metadata: +This repo now uses raw stdin for worker payloads and sideband IPC for prompt +state and byte-level accounting: - R worker mode owns stdin in a worker-side reader thread. The server announces - the payload byte length on sideband IPC; the worker reader consumes exactly - that many raw bytes and submits them to embedded R. -- Python worker mode lets CPython own stdin. The server announces request - metadata on sideband IPC, waits for `stdin_write_ack`, then writes raw bytes - for CPython's interactive loop to consume. + payload bytes by writing them to worker stdin; the worker reports exact + consumed or discarded bytes on sideband IPC. +- Python worker mode lets CPython own stdin. The server writes raw bytes for + CPython's interactive loop to consume, and the worker reports exact consumed + or discarded bytes on sideband IPC. The remaining follow-up is to make this ownership split more explicit in code and reduce server-side backend branching around request metadata. @@ -36,8 +37,10 @@ and reduce server-side backend branching around request metadata. - Treat worker stdin as the real raw input stream delivered to the interpreter. - Do not add framing headers or other synthetic protocol markers to stdin. -- Mirror request metadata over IPC instead: request start, expected input payload, completion, and other turn/state signals. -- Let the worker use the IPC envelope to know when the current stdin payload is complete, while still feeding raw stdin through the interpreter-facing path. +- Mirror interpreter state over IPC instead: prompt starts, consumed bytes, + discarded bytes, completion, and other turn/state signals. +- Let the worker use exact sideband accounting for stdin bytes while still + feeding raw stdin through the interpreter-facing path. - For line-oriented runtimes such as embedded R, expect a single logical request to be satisfied across multiple `readline` or `ReadConsole` calls. The current embedded worker implementation keeps stdin raw and preserves request diff --git a/docs/futurework/worker-pty-stdin-transport.md b/docs/futurework/worker-pty-stdin-transport.md index cf4acb00..961baa20 100644 --- a/docs/futurework/worker-pty-stdin-transport.md +++ b/docs/futurework/worker-pty-stdin-transport.md @@ -1,18 +1,19 @@ # Worker PTY Stdin Transport -Status: implemented for Unix built-in Python and custom protocol-worker launch -configuration. This note is retained as historical design context; the current -contract is documented in `docs/architecture.md`, -`docs/worker_sideband_protocol.md`, and `docs/output_timeline.md`. +Status: implemented for Unix built-in Python, unsandboxed Windows built-in +Python, and custom protocol-worker launch configuration. This note is retained +as historical design context; the current contract is documented in +`docs/architecture.md`, `docs/worker_sideband_protocol.md`, and +`docs/output_timeline.md`. ## Use Case Some runtimes may need TTY-like stdin for their normal interactive hooks. For example, a Python embedding that relies on `PyOS_ReadlineFunctionPointer` may only use that hook when stdin is a -TTY. The Unix Python worker now uses that PTY-backed path. R and default -protocol workers continue to use pipe stdin unless their launch spec selects a -PTY. +TTY. The Unix Python worker and unsandboxed Windows Python worker now use that +PTY-backed path. R and default protocol workers continue to use pipe stdin +unless their launch spec selects a PTY. ## Boundary @@ -54,5 +55,5 @@ steady-state request handling. The repository now has protocol-worker coverage for PTY launch with sideband IPC kept separate from visible PTY output, plus public Python backend tests proving -that Unix Python gets TTY-backed C stdio and CPython `input()` consumes stdin -through the readline path. +that Unix and unsandboxed Windows Python get TTY-backed C stdio and CPython +`input()` consumes stdin through the readline path. diff --git a/docs/index.md b/docs/index.md index 34b56090..287395c7 100644 --- a/docs/index.md +++ b/docs/index.md @@ -48,7 +48,7 @@ checked-in execution plans without relying on stale notes. - `docs/futurework/server-backend-boundary.md`: deferred note on removing backend-specific execution semantics from server-side request handling. - `docs/futurework/sidecar-viewer-observability.md`: deferred note on a local read-only sidecar viewer for transcripts, plots, and output bundles. - `docs/futurework/worker-session-tempdir-rotation.md`: deferred design note on rotating worker tempdir paths per launch so stale temp trees do not block respawn. -- `docs/futurework/worker-pty-stdin-transport.md`: historical design note for the implemented Unix Python and custom-worker PTY launch path. +- `docs/futurework/worker-pty-stdin-transport.md`: historical design note for the implemented Python and custom-worker PTY launch path. - `docs/futurework/stronger-worker-child-containment.md`: deferred design note on tighter worker descendant containment, especially on Windows. - `docs/futurework/unified-output-timeline-pipeline.md`: deferred design note for converging pager and files mode onto one shared resolved timeline pipeline. - `docs/futurework/stdin-transport-single-owner.md`: deferred design for making worker stdin ownership explicit instead of relying on a Windows-only gate. diff --git a/docs/output_timeline.md b/docs/output_timeline.md index e0e5c029..5fef0bc8 100644 --- a/docs/output_timeline.md +++ b/docs/output_timeline.md @@ -15,7 +15,8 @@ The worker emits different kinds of information on different channels: may merge stdout/stderr identity and apply terminal behavior such as CRLF translation, echo, and width-dependent formatting. - Sideband IPC carries structural events such as `readline_start`, - `readline_result`, `plot_image`, and `session_end`. + `readline_input_bytes`, `readline_discard_bytes`, `output_image`, and + `session_end`. Raw pipes and IPC do not arrive at the server in one globally ordered stream. The server therefore maintains its own output timeline and resolves it into the @@ -33,8 +34,8 @@ changes what must stay buffered between tool calls. reply. - Worker-owned `output_text` frames and raw stdout/stderr bytes are buffered as `TextFragment` events. -- Sideband events are stored alongside text so later formatting can suppress - echoed input and respect request boundaries. +- Sideband events are stored alongside text so later formatting can respect + image placement and request boundaries. - When a reply is sealed, `PendingOutputSnapshot::format_contents()` converts the tape into `WorkerContent`. @@ -42,8 +43,8 @@ changes what must stay buffered between tool calls. - `src/output_capture.rs` stores text in the global output ring and stores image or server-status events at byte offsets within that ring. -- `src/worker_process.rs` reads ranges from that ring, collapses echoed input, - and then asks `src/pager/` to page the resulting mixed text/image stream. +- `src/worker_process.rs` reads ranges from that ring and then asks + `src/pager/` to page the resulting mixed text/image stream. ## Timeline vs completion @@ -51,36 +52,36 @@ The important design split is not "files mode vs pager mode". It is: - timeline resolution: reconstruct the visible output order from text plus sideband facts -- completion cleanup: once the server knows a request has finished, trim echoed - input, append protocol warnings, and restore the final prompt +- completion cleanup: once the server knows a request has finished, append + protocol warnings, restore final prompt metadata, and apply any + sideband-driven presentation cleanup Timeline resolution must not depend on request completion. For example, the -server does not need to wait for completion to know that a `plot_image` event -belongs before a later `readline_result` echo. That ordering fact is already -present in the mixed timeline. +server does not need to wait for completion to know where an `output_image` +event belongs relative to worker-owned `output_text`. That ordering fact is +already present in the mixed timeline. Completion matters only for reply cleanup choices that are unsafe while a request is still in flight. In particular: -- timed-out or otherwise non-final drains must preserve echoed input so the user +- timed-out or otherwise non-final drains must preserve visible text so the user can still see what is running -- completed replies may trim or drop echo-only content once the server knows the - request is settled +- completed replies may add completion notices or restore final prompt metadata + once the server knows the request is settled The intent is one true visible timeline per output surface, with completion used only as a later presentation step. -Echo matching must be driven by the sideband facts themselves: +Prompt and input accounting must be driven by sideband facts themselves: - `readline_start` supplies prompt text; the server derives whether it is - unsatisfied from active-turn stdin accounting -- `readline_result` is emitted by the worker, but it describes the exact - prompt text and input line that `readline` consumed and echoed -- the server should match and collapse those exact sideband facts + unsatisfied from active-turn stdin accounting. +- `readline_input_bytes` and `readline_discard_bytes` report the exact + active-turn bytes consumed or discarded before worker-side normalization. - the server should not parse visible output looking for prompt shapes such as - `>`, `...`, or `Browse[n]>` + `>`, `...`, or `Browse[n]>`. -That matching is only opportunistic: +Visible text handling remains conservative: - raw stdout/stderr remains authoritative for text that did not arrive through `output_text` @@ -88,14 +89,8 @@ That matching is only opportunistic: but it is not authoritative for separate stdout/stderr stream identity - forked children, spawned subprocesses, or other writers may interleave with or corrupt what would otherwise have been a clean echoed line -- if exact sideband-to-stdout matching fails or becomes ambiguous, the server - should degrade softly to raw captured stdout/stderr for that region, without - eliding echo or inventing a cleaned-up transcript -- sideband-first carryover is source-aware: the backend records whether a - `readline_result` echo should arrive as raw stdout or as `output_text`, and - carryover only trims later text from that same source. Prompt spelling only - decides whether a prompt shape is eligible for carryover; it does not decide - the source. +- the server should degrade softly to raw captured stdout/stderr for ambiguous + regions, without eliding echo or inventing a cleaned-up transcript ## Ownership split @@ -116,9 +111,11 @@ resolution, not in the wire protocol. - Worker text must remain in the order observed on its stdout/stderr pipes. - For PTY-backed workers, worker text from the PTY master must remain in the order observed on that terminal stream. -- Sideband `readline_result` events define the order in which input lines were - consumed. -- Sideband `plot_image` events define when plot updates happened relative to +- Sideband `readline_input_bytes` events define the order in which active-turn + input bytes were consumed. +- Sideband `readline_discard_bytes` events define the order in which + active-turn input bytes were discarded. +- Sideband `output_image` events define when image updates happened relative to other sideband events. - Visible replies must preserve evaluation order when that order is represented by sideband facts. They must not invent a strict order between unframed @@ -131,8 +128,7 @@ the same thing as "execution order in the backend". - `src/output_capture.rs`: pager-mode output ring and event storage. - `src/pending_output_tape.rs`: files-mode mixed event tape. -- `src/worker_process.rs`: request completion, echo suppression, and reply - assembly. +- `src/worker_process.rs`: request completion and reply assembly. - `src/ipc.rs`: sideband event intake and per-request IPC bookkeeping. - `docs/worker_sideband_protocol.md`: wire-level IPC contract. diff --git a/docs/plans/active/r-owned-output-synchronous-ipc.md b/docs/plans/active/r-owned-output-synchronous-ipc.md index 59b77772..e18a4e23 100644 --- a/docs/plans/active/r-owned-output-synchronous-ipc.md +++ b/docs/plans/active/r-owned-output-synchronous-ipc.md @@ -31,10 +31,11 @@ framed prompt facts instead of stripping prompt-shaped raw stdout. A public files-mode regression covers raw child stdout that exactly matches a later R-owned prompt/input echo. -- Phase 4: planned - evaluate a bounded pre-input drain gate. `stdin_write_ack` - only means the worker has installed request metadata before raw stdin bytes - arrive; any raw-output drain gate should be a separate request-boundary - protocol step. +- Phase 4: planned - evaluate a bounded pre-input drain gate. Earlier notes + proposed a request-boundary acknowledgement frame, but the active protocol + keeps user input on worker stdin and uses byte-level sideband accounting. + Any raw-output drain gate should be a separate request-boundary protocol + step. ## Locked Decisions @@ -76,10 +77,9 @@ R-owned stdout, stderr, readline echo, plots, direct file-descriptor writes, child output, and large output. - 2026-05-08: Narrowed files-mode sideband-first echo carryover so ordinary R - prompts no longer trim later raw stdout. The backend now records the expected - echo source on `readline_result`, so backend-owned `output_text` echo can - carry across drain boundaries without deriving the source from prompt - spelling. + prompts no longer trim later raw stdout. That older implementation used + text-level readline source facts; the active worker protocol now uses exact + `readline_input_bytes` and `readline_discard_bytes` accounting instead. - 2026-05-08: Stopped treating R raw stdout that equals the primary prompt as the completion prompt. The server now appends the R completion prompt from framed IPC facts, including interrupt-drained completions, while leaving @@ -88,7 +88,7 @@ public regression proving raw child stdout that exactly matches a later R-owned prompt/input echo remains visible. No runtime change was needed because same-drain and carryover echo collapse already require matching - `readline_result` source facts. + worker sideband accounting facts. - 2026-05-08: Kept ACK-gated input delivery open as a request-boundary tool, not a per-output ACK. The useful shape is: before the worker consumes the next input, the server gets a bounded opportunity to drain raw stdout/stderr from diff --git a/docs/plans/active/worker-server-protocol-zod.md b/docs/plans/active/worker-server-protocol-zod.md index 70fa99ac..a4fb6449 100644 --- a/docs/plans/active/worker-server-protocol-zod.md +++ b/docs/plans/active/worker-server-protocol-zod.md @@ -199,17 +199,20 @@ errors. ## Text and Byte Encoding -Sideband itself is UTF-8 JSONL. Fields that describe MCP input and -runtime line-input state use JSON strings: +Sideband itself is UTF-8 JSONL. Runtime prompt text uses JSON strings, +while stdin accounting uses byte-preserving base64 payloads: - `readline_start.prompt` -- `readline_input.text` -- `readline_discard.text` +- `readline_input_bytes.data_b64` +- `readline_discard_bytes.data_b64` -These fields are UTF-8 text because MCP tool input is text and the -readline contract is line-oriented text. For stdin accounting, the -server encodes `readline_input.text` or `readline_discard.text` as UTF-8 -and compares those bytes with the active-turn stdin byte queue. +Prompt text is UTF-8 because it is display data. Stdin accounting is +byte-oriented: the worker reports the exact consumed or discarded byte +range with `readline_input_bytes` or `readline_discard_bytes`, and the +server matches those bytes against the active-turn stdin byte queue. +The worker may normalize bytes before giving them to the interpreter, +but the accounting events must report the bytes as received over the +worker stdin transport before that normalization. This does not add a new user-visible input restriction beyond MCP. A normal `repl()` call supplies a JSON string inside a UTF-8 JSON-RPC @@ -325,10 +328,10 @@ block: it can be satisfied immediately by bytes already available on stdin. If the server still has bytes from the active turn that have not been -matched by `readline_input.text` or `readline_discard.text`, this prompt -is satisfied by already-written input and the turn is not complete. If -no such bytes remain, this prompt is unsatisfied and the server may seal -the reply for the active turn. +matched by `readline_input_bytes` or `readline_discard_bytes`, this prompt is +satisfied by already-written input and the turn is not complete. If no +such bytes remain, this prompt is unsatisfied and the server may seal the +reply for the active turn. For an unsatisfied `readline_start`, the server will render non-empty worker-supplied prompt text in the MCP response to show that the runtime @@ -345,60 +348,51 @@ cannot suppress. If prompt-like text does arrive as output, the server must preserve it as ordinary output and must not deduplicate it by comparing it with `readline_start.prompt`. -### `readline_input` +### `readline_input_bytes` Worker to server: ```json { - "type": "readline_input", - "text": "1+1\n" + "type": "readline_input_bytes", + "data_b64": "ww==" } ``` Fields: -- `text`: exact UTF-8 text delivered to the runtime-facing input layer - for this read, including a server-appended trailing newline if one was - added before writing to worker stdin. +- `data_b64`: exact bytes received from the server over worker stdin and + then delivered to the runtime-facing input layer, encoded as base64. -The server may use `readline_input` only for generic accounting against -the bytes it already wrote to worker stdin. It must not interpret the -text as language syntax. A mismatch between `readline_input.text` -encoded as UTF-8 and the server's active-turn byte queue is a protocol -error because it means the worker's input placement is not describing -what it delivered to the runtime-facing input layer. +The worker may normalize bytes before passing them to the interpreter, +but `data_b64` reports the pre-normalized bytes. Invalid base64 or a +mismatch with the server's active-turn byte queue is a protocol error. -`readline_input` is not itself a completion signal. Completion is the -next unsatisfied `readline_start` or `session_end`. +`readline_input_bytes` is not itself a completion signal. Completion is +the next unsatisfied `readline_start` or `session_end`. -### `readline_discard` +### `readline_discard_bytes` Worker to server: ```json { - "type": "readline_discard", - "text": "cancelled\n" + "type": "readline_discard_bytes", + "data_b64": "qQ==" } ``` Fields: -- `text`: exact UTF-8 text from the active turn that the worker - discarded without delivering to the runtime. +- `data_b64`: exact active-turn bytes received from the server over + worker stdin and discarded without delivery to the runtime, encoded as + base64. -The worker emits this only for bytes it can account for. The server -removes these bytes from the active-turn byte queue exactly like -delivered input bytes, but it does not display them as runtime output. A -mismatch between `readline_discard.text` encoded as UTF-8 and the -server's active-turn byte queue is a protocol error. +Invalid base64 or a mismatch with the server's active-turn byte queue is +a protocol error. -If the worker discards input after interrupt or reset cleanup and cannot -report which bytes were discarded, the server cannot prove recovery for -any control tail. In that case, the worker should not emit -`readline_discard` for unknown bytes, and the server must not write a -tail that depends on clean recovery. +Workers must emit this only for exact bytes they can identify. Bytes +flushed from terminal state without being observed are not reportable. ## Output Events @@ -471,16 +465,16 @@ carries no request id because the server allows only one active turn. The worker uses this message to clean up worker-owned input state. In response, the worker should cancel or drain any pending stdin bytes that -it owns or can observe, and emit `readline_discard` for the exact -active-turn text it discarded. The worker must not emit -`readline_discard` for bytes it already delivered to the runtime-facing -input layer, bytes it cannot identify, or bytes that belong to no active -turn. +it owns or can observe, and emit `readline_discard_bytes` for the exact +active-turn bytes it discarded. +The worker must not emit discard events for bytes it already delivered +to the runtime-facing input layer, bytes it cannot identify, or bytes +that belong to no active turn. The worker's sideband control listener must not be blocked by runtime evaluation. If the worker cannot process the sideband `interrupt` before the runtime consumes pending bytes, those bytes should be reported as -`readline_input`, not `readline_discard`. +`readline_input_bytes`, not as discard events. The server does not wait for an acknowledgement to `interrupt`. Recovery is proven only by later worker events: exact input accounting followed @@ -545,10 +539,9 @@ sleep or a signal-delivery acknowledgement. The worker has recovered only when it emits one of these events after the interrupt: - an unsatisfied `readline_start` after the active-turn byte queue has - been fully accounted for by `readline_input` and/or - `readline_discard`, meaning the runtime is ready for the next client - input and no bytes from the interrupted turn remain to satisfy that - read; + been fully accounted for by input and/or discard events, meaning the + runtime is ready for the next client input and no bytes from the + interrupted turn remain to satisfy that read; - `session_end`, meaning the old runtime is gone and cannot consume a follow-up tail. @@ -628,10 +621,11 @@ For a conforming worker: 1. `worker_ready` is first. 2. `readline_start` is emitted when the runtime enters a line-read operation, before it reads input bytes for that operation. -3. `readline_input` is emitted after the worker delivers input bytes to - the runtime-facing input layer. -4. `readline_discard` is emitted after the worker discards accounted-for - input bytes during interrupt/reset cleanup. +3. `readline_input_bytes` is emitted after the + worker delivers input bytes to the runtime-facing input layer. +4. `readline_discard_bytes` is emitted after the + worker discards accounted-for input bytes during interrupt/reset + cleanup. 5. `output_text` and `output_image` are emitted in runtime-visible order. 6. `session_end` is final. @@ -645,12 +639,8 @@ Server-to-worker `interrupt` messages are ordered on the server-to-worker sideband stream. Worker-to-server recovery facts are ordered on the worker-to-server sideband stream. The server must not assume that writing the `interrupt` message means the worker has already -processed it; later `readline_input`, `readline_discard`, -`readline_start`, and `session_end` events determine recovery. -Built-in Unix Python currently has a private `python_interrupt` / -`python_interrupt_ack` cleanup handshake so it can drain PTY input before -SIGINT; that acknowledgement is transitional and not part of the generic -worker protocol. +processed it; later input, discard, `readline_start`, and `session_end` +events determine recovery. ## Timeout and Polling @@ -685,10 +675,10 @@ Protocol errors are fail-fast: - Missing required field. - Invalid enum value. - Invalid base64. -- `readline_input.text` that does not match bytes the server wrote for - the active turn after UTF-8 encoding. -- `readline_discard.text` that does not match bytes the server wrote for - the active turn after UTF-8 encoding. +- `readline_input_bytes.data_b64` that is invalid base64 or does not + match bytes the server wrote for the active turn. +- `readline_discard_bytes.data_b64` that is invalid base64 or does not + match bytes the server wrote for the active turn. - Worker-owned output after `session_end`. - Second non-empty input while a turn is still active. @@ -710,10 +700,11 @@ A third-party worker must: 4. Arrange for server-written input bytes to reach the runtime. 5. Emit `readline_start` when the runtime enters a line-read operation, before it reads input bytes for that operation. -6. Emit `readline_input` after delivering input bytes to the - runtime-facing input layer. -7. Emit `readline_discard` for accounted-for active-turn bytes discarded - during interrupt/reset cleanup. +6. Emit `readline_input_bytes` after delivering + input bytes to the runtime-facing input layer. +7. Emit `readline_discard_bytes` for + accounted-for active-turn bytes discarded during interrupt/reset + cleanup. 8. Emit worker-owned output as `output_text` or `output_image`. 9. Arrange OS interrupt/reset/shutdown controls to affect the runtime. 10. Emit `session_end` before clean shutdown. @@ -791,8 +782,7 @@ surface with Zod as the worker: - Ctrl-C sends the sideband `interrupt` notification and is delivered as an OS interrupt to an existing worker. - Ctrl-C cancels any not-yet-written stdin tail, and the worker - best-effort discards pending input it owns with `readline_discard` - accounting. + best-effort discards pending input it owns with discard accounting. - Interrupt tail input is sent only after all prior active-turn bytes are accounted for as delivered or discarded and the worker emits an unsatisfied `readline_start`. @@ -821,9 +811,9 @@ Required migration work: arguments. - Keep worker stdin as the only user-input transport from server to worker. -- Remove `stdin_write`, `stdin_write_complete`, byte counts, line - counts, `stdin_write_ack`, and the private Python interrupt - acknowledgement. +- Remove superseded request-boundary sideband frames, including + `stdin_write`, `stdin_write_complete`, byte counts, line counts, + `stdin_write_ack`, and the private Python interrupt acknowledgement. - Remove IPC-carried request ids and request payloads. - Replace server-inferred completion from prompt parsing with unsatisfied worker-emitted `readline_start`. @@ -849,9 +839,9 @@ worker implementation task, not a server request-handling task. - User input travels to the worker only as stdin bytes, with exactly one trailing `\n` appended by the server when non-empty input does not already end in `\n`. -- R and Python workers emit `readline_start`/`readline_input` facts +- R and Python workers emit `readline_start` and input accounting facts sufficient for the server to identify unsatisfied input waits. -- R and Python workers emit `readline_discard` for any active-turn input +- R and Python workers emit discard accounting for any active-turn input bytes they discard during interrupt/reset cleanup. - The server does not parse or strip prompts from stdout/stderr. - The server delivers OS interrupts to an existing worker without diff --git a/docs/plans/completed/python-pty-readline.md b/docs/plans/completed/python-pty-readline.md index d39a34ea..e5db2854 100644 --- a/docs/plans/completed/python-pty-readline.md +++ b/docs/plans/completed/python-pty-readline.md @@ -9,8 +9,9 @@ continuation state, or emulate Python stdin semantics. ## Summary -- Move the embedded Python worker to PTY-backed C stdin/stdout on Unix so CPython - takes the `PyOS_ReadlineFunctionPointer` path for supported interactive input. +- Move the embedded Python worker to PTY-backed C stdin/stdout on Unix and + unsandboxed Windows so CPython takes the `PyOS_ReadlineFunctionPointer` path + for supported interactive input. - Keep sideband IPC separate from PTY traffic, with the server continuing to write normalized request bytes to worker stdin, consume sideband facts, capture visible output, and finalize replies generically. @@ -25,7 +26,7 @@ continuation state, or emulate Python stdin semantics. ## Status - State: completed -- Last updated: 2026-05-15 +- Last updated: 2026-05-20 - Current phase: complete - Driving initiative: move embedded Python to PTY-backed CPython readline - Final slice: current-state documentation and PTY output contract @@ -36,8 +37,9 @@ continuation state, or emulate Python stdin semantics. sideband protocol feature. - Keep the explicit pipe-vs-PTY launch abstraction. - Keep PTY transport independent from sideband IPC. -- Run embedded Unix Python with C stdin, stdout, and stderr attached to a PTY so - CPython sees TTY streams and calls `PyOS_ReadlineFunctionPointer`. +- Run embedded Python with C stdin, stdout, and stderr attached to a PTY where + the platform launch supports it so CPython sees TTY streams and calls + `PyOS_ReadlineFunctionPointer`. - Keep the PTY launch implementation platform-specific where sandbox launch semantics require it: Unix can allocate the PTY before sandbox exec, while Windows sandbox mode must attach ConPTY to the restricted child itself. @@ -51,10 +53,11 @@ control flow while keeping the server's request handling interpreter-neutral. ## Diff Size Note -This branch can look like a large addition because it keeps transitional -pipe-backed and non-Unix compatibility scaffolding while adding the Unix PTY -path. After Windows ConPTY support lands, the previous broad stdin interception -and protocol compatibility code should be deleted instead of carried forward. +This branch originally looked like a large addition because it kept +transitional pipe-backed compatibility scaffolding while adding the PTY path. +Sandboxed Windows Python now creates ConPTY inside the restricted wrapper, so +the remaining broad stdin interception and protocol compatibility code should +be deleted instead of carried forward. ## Long-Term Direction @@ -128,10 +131,10 @@ route. ## Remaining Follow-Up -- Non-Unix Python still has a pipe-backed compatibility path. A future Windows - ConPTY slice should decide whether to write Ctrl-C through ConPTY input, use - console control events for the restricted child, or keep a Python-side - interrupt notification for the blocked readline case. +- Sandboxed Windows Python still has a pipe-backed compatibility path. A future + Windows wrapper ConPTY slice should attach ConPTY inside the restricted child + launch boundary, then revisit whether the remaining Python-side stdin bridges + can be removed. - If future ordering work needs stricter input-delivery coordination, it should preserve the current boundary: sideband facts describe observed runtime events; the server must not parse Python prompts from visible PTY output. @@ -200,3 +203,7 @@ route. supported PTY path leaves CPython's `sys.stdin`, `open`, `os.read`, `os.readv`, and `io.FileIO` surfaces intact; request-completion accounting remains tied to `PyOS_ReadlineFunctionPointer`. +- 2026-05-20: Added unsandboxed Windows ConPTY launch for built-in Python, + keeping sideband named pipes separate from PTY traffic and using + sideband-aware direct-stdin bridges only on Windows so CRLF and console reads + remain accountably tied to active MCP input. diff --git a/docs/sandbox.md b/docs/sandbox.md index 5ab54dc0..0f247ac3 100644 --- a/docs/sandbox.md +++ b/docs/sandbox.md @@ -163,8 +163,11 @@ Optional `bwrap` stage: - R backend is supported with the same policy surface (`read-only`, `workspace-write`, `danger-full-access`). - Python support is not part of the stable Windows surface yet. The embedded - backend no longer requires a Unix PTY, but Windows support still depends on - the selected CPython installation exposing a loadable runtime library. + backend uses ConPTY for `danger-full-access` and `external-sandbox` launches. + For sandboxed `read-only` and `workspace-write`, the server uses pipe stdio to + the sandbox wrapper, and the wrapper creates ConPTY for the restricted Python + child. Windows Python also depends on the selected CPython installation + exposing a loadable runtime library. - managed domain allowlists are not enforced on Windows yet; configuring allowed or denied domains with enabled network access currently fails closed. - `read-only` and `workspace-write` use a two-stage Windows sandbox model: diff --git a/docs/worker_sideband_protocol.md b/docs/worker_sideband_protocol.md index 17459dd1..adda00bf 100644 --- a/docs/worker_sideband_protocol.md +++ b/docs/worker_sideband_protocol.md @@ -32,9 +32,14 @@ Workers must not advertise interpreter-specific shutdown text, and the server does not send shutdown code or a sideband shutdown command. See `docs/adr/0001-stdin-close-graceful-shutdown.md`. -Built-in Unix Python uses PTY-backed C stdin/stdout/stderr so CPython calls -`PyOS_ReadlineFunctionPointer`. The Python callback emits readline accounting -facts from that CPython path. Sideband IPC stays separate from the PTY. +Built-in Python uses PTY-backed C stdin/stdout/stderr where the platform launch +supports it so CPython calls `PyOS_ReadlineFunctionPointer`. The Python callback +emits readline accounting facts from that CPython path. Sideband IPC stays +separate from the PTY. On Windows sandboxed Python, the server speaks pipe +stdio to the sandbox wrapper; the wrapper owns ConPTY process creation for the +restricted Python child and forwards stdin/stdout between the wrapper and +ConPTY. That keeps the sandbox boundary intact while preserving CPython's +console-backed readline path. ## Direction: server -> worker @@ -44,8 +49,8 @@ facts from that CPython path. Sideband IPC stays separate from the PTY. process or process group. - This is for worker-owned bookkeeping only. It does not carry user input and does not replace the OS interrupt. -- The worker may emit `readline_discard` for exact active-turn stdin bytes it - discarded before delivering them to the runtime. +- The worker may emit `readline_discard_bytes` for exact active-turn stdin + bytes it discarded before delivering them to the runtime. ## Direction: worker -> server @@ -66,27 +71,40 @@ invalid base64, and unknown message types are protocol errors. for that operation. - The prompt string is required; use an empty string if the runtime supplied no prompt. -- If active-turn stdin bytes remain unaccounted, the prompt is satisfied by - already-written stdin and does not complete the request. If no active-turn - stdin bytes remain, the prompt is unsatisfied and may complete the request. +- If active-turn stdin bytes remain unaccounted by input or discard events, + the prompt is satisfied by already-written stdin and does not complete the + request. If no active-turn stdin bytes remain, the prompt is unsatisfied and + may complete the request. - Prompt rendering is derived from this structured event, not from raw stdout/stderr parsing. -`readline_input` -- `{ "type": "readline_input", "text": }` -- Emitted after the worker delivers active-turn stdin text to the +`readline_input_bytes` +- `{ "type": "readline_input_bytes", "data_b64": }` +- Emitted after the worker delivers active-turn stdin bytes to the runtime-facing input layer. -- The server encodes `text` as UTF-8 and removes those bytes from the active - stdin queue. A mismatch is a protocol error. - -`readline_discard` -- `{ "type": "readline_discard", "text": }` -- Emitted after the worker discards active-turn stdin text during - interrupt/reset cleanup without delivering it to the runtime. -- The server encodes `text` as UTF-8 and removes those bytes from the active - stdin queue. A mismatch is a protocol error. +- `data_b64` must encode the exact bytes received from the server over the + worker stdin transport before any worker-side normalization or interpreter + adaptation. The worker may normalize the bytes it passes to the runtime, but + this accounting event reports the pre-normalized wire bytes. +- The server decodes `data_b64` and removes those bytes from the active stdin + queue. Invalid base64 or a byte mismatch is a protocol error. +- Protocol version 1 compatibility: the server also accepts legacy + `{ "type": "readline_input", "text": }` frames and accounts for + the UTF-8 encoding of `text`. + +`readline_discard_bytes` +- `{ "type": "readline_discard_bytes", "data_b64": }` +- Emitted after the worker discards exact active-turn stdin bytes during + interrupt/reset cleanup without delivering them to the runtime. +- `data_b64` must encode the exact bytes received from the server over the + worker stdin transport before any worker-side normalization. +- The server decodes `data_b64` and removes those bytes from the active stdin + queue. Invalid base64 or a byte mismatch is a protocol error. - Workers must emit this only for exact bytes they can identify. Bytes flushed from terminal state without being observed are not reportable. +- Protocol version 1 compatibility: the server also accepts legacy + `{ "type": "readline_discard", "text": }` frames and accounts for + the UTF-8 encoding of `text`. `output_text` - `{ "type": "output_text", "stream": <"stdout"|"stderr">, "data_b64": , "is_continuation": }` @@ -109,6 +127,8 @@ invalid base64, and unknown message types are protocol errors. - Carries worker-owned image bytes on the ordered sideband stream. - `image_id` is worker-local source identity for update grouping. The server owns MCP response image IDs. +- There is no image acknowledgement message. +- Workers must not delay stdout/stderr output waiting for sideband responses. `session_end` - `{ "type": "session_end", "reason": , "message_b64": }` @@ -117,69 +137,6 @@ invalid base64, and unknown message types are protocol errors. `reset`, `runtime_exit`, `crash`, and `protocol_error`. - After this event, the worker must not emit more output. -## Transitional Compatibility Frames - -These frames remain for built-in workers that have not fully migrated on every -platform. New protocol workers should not copy them for steady-state request -handling. Built-in R no longer uses them. Built-in Unix Python still receives -the legacy request-boundary frames, but stdin accounting comes from CPython -readline events rather than a separate stdin bridge. - -`stdin_write` -- `{ "type": "stdin_write", "byte_len": , "line_count": , "final_prompt": }` -- Legacy server-to-worker request metadata emitted before the server writes raw - input payload bytes to stdin. -- Built-in Unix Python uses these fields only to install active request state - before CPython's next readline callback consumes stdin. -- Non-Unix Python may still use them for the pipe-backed compatibility path - until it is migrated to the same readline accounting model. - -`stdin_write_complete` -- `{ "type": "stdin_write_complete" }` -- Legacy server-to-worker marker emitted after the server has written the raw - input payload bytes to stdin. - -`backend_info` -- `{ "type": "backend_info", "supports_images": }` -- Legacy startup metadata accepted from older built-in workers. -- It may describe narrow worker capabilities, but it must not turn steady-state - server request handling into language-specific policy. - -`stdin_write_ack` -- `{ "type": "stdin_write_ack" }` -- Legacy worker-to-server request-boundary acknowledgement. -- This only acknowledges request-boundary state. It is not an acknowledgement - for stdout/stderr, PTY output, plot images, prompt completion, or request - completion. - -`python_interrupt_ack` -- `{ "type": "python_interrupt_ack" }` -- Transitional worker-to-server acknowledgement used only by built-in Unix - Python after it has processed its private `python_interrupt` cleanup message. -- It means the worker has attempted exact discard accounting and terminal input - flushing before the server delivers SIGINT. It is not a generic protocol - interrupt acknowledgement. - -`readline_result` -- `{ "type": "readline_result", "prompt": , "line": }` -- Legacy echo metadata emitted after a line is read. -- The server may use it for conservative echo suppression of raw pipe output, - but completion is driven by `readline_start`, `readline_input`, - `readline_discard`, and `session_end`. - -`plot_image` -- `{ "type": "plot_image", "mime_type": , "data": , "is_update": , "source": }` -- Legacy image payload used by built-in plot emitters. -- `source` is optional worker-local plot source identity, such as a graphics - device or figure slot. It is not a response image ID; the server owns response - image IDs and uses `source` only to keep distinct plot sources from - collapsing into one response image. -- There is no plot-image acknowledgement message. -- Workers must not delay stdout/stderr output waiting for sideband responses. -- If an update is the first image event for a new server request, the server - treats it as a new response image and includes a server notice that it updates - the previously sent image. - ## Notes - Raw stdout/stderr capture remains active for unowned output, such as child diff --git a/python/embedded.py b/python/embedded.py index 4fd6b4a1..2d1297ed 100644 --- a/python/embedded.py +++ b/python/embedded.py @@ -18,6 +18,10 @@ import posix as _mcp_repl_posix except ImportError: _mcp_repl_posix = None +try: + import nt as _mcp_repl_nt +except ImportError: + _mcp_repl_nt = None os.environ.setdefault("MPLBACKEND", "agg") # pdb's pyrepl path reads the terminal fd directly; keep debugger input on @@ -128,10 +132,19 @@ class McpInputStream: errors = "replace" newlines = None - def __init__(self, fileno=0, closefd=False, encoding=None, errors=None, newline=None): + def __init__( + self, + fileno=0, + closefd=False, + encoding=None, + errors=None, + newline=None, + tty=False, + ): self._buffer = b"" self._fileno = fileno self._closefd = closefd + self._tty = tty if encoding is not None: self.encoding = encoding if errors is not None: @@ -313,7 +326,7 @@ def seekable(self): return False def isatty(self): - return False + return self._tty def fileno(self): return self._fileno @@ -714,7 +727,7 @@ def _emit_plots(force_figures=None, force_all=False, record_only=False): encoded = base64.b64encode(data).decode("ascii") is_new = fig_num not in prev_known _mcp_repl_flush_original_stdio() - _mcp_repl.emit_plot_image("image/png", encoded, not bool(is_new), str(fig_num)) + _mcp_repl.emit_output_image("image/png", encoded, not bool(is_new), str(fig_num)) if current_fig_num in new_known: try: @@ -754,16 +767,22 @@ def _mcp_repl_plot_capable(): _original_builtins_open = builtins.open _original_io_FileIO = io.FileIO _original_os_fdopen = os.fdopen +_original_os_dup = os.dup +_original_os_dup2 = getattr(os, "dup2", None) +_original_os_close = os.close _original_os_read = os.read _original_os_readv = getattr(os, "readv", None) -_mcp_repl_raw_stdin_read_supported = os.name == "posix" -# Keep the original fd 0 identity so duplicated stdin fds still use the bridge. +_mcp_repl_raw_stdin_read_supported = os.name in ("posix", "nt") +# On POSIX, keep the original fd 0 identity so duplicated stdin fds still use +# the bridge. On Windows, anonymous pipe stat identity is too weak to +# distinguish unrelated pipes, so track fd 0 and explicit fd duplicates. _mcp_repl_raw_stdin_stat = None if _mcp_repl_raw_stdin_read_supported: try: _mcp_repl_raw_stdin_stat = os.fstat(0) except OSError: pass +_mcp_repl_windows_raw_stdin_fds = {0} if os.name == "nt" else None _mcp_repl_stdin_path_aliases = frozenset(("/dev/stdin", "/dev/fd/0", "/proc/self/fd/0")) @@ -784,6 +803,8 @@ def _mcp_repl_import(name, globals=None, locals=None, fromlist=(), level=0): def _mcp_repl_is_raw_stdin_fd(fd): if not _mcp_repl_raw_stdin_read_supported: return False + if os.name == "nt": + return fd in _mcp_repl_windows_raw_stdin_fds if _mcp_repl_raw_stdin_stat is None: return fd == 0 try: @@ -796,6 +817,16 @@ def _mcp_repl_is_raw_stdin_fd(fd): ) +def _mcp_repl_note_raw_stdin_fd(fd): + if os.name == "nt": + _mcp_repl_windows_raw_stdin_fds.add(fd) + + +def _mcp_repl_forget_raw_stdin_fd(fd): + if os.name == "nt": + _mcp_repl_windows_raw_stdin_fds.discard(fd) + + def _mcp_repl_is_raw_stdin_path(file): try: path = os.fspath(file) @@ -896,7 +927,9 @@ def _mcp_repl_stdin_stream_for_mode( _mcp_repl_validate_stdin_open_options(mode, buffering, encoding, errors, newline) if _mcp_repl_unbuffered_binary_stdin_mode(mode, buffering): return McpRawInputBuffer(fileno, closefd) - stream = McpInputStream(fileno, closefd, encoding, errors, newline) + stream = McpInputStream( + fileno, closefd, encoding, errors, newline, _mcp_repl_c_stdio_tty + ) if "b" in mode: return stream.buffer return stream @@ -954,6 +987,34 @@ def _mcp_repl_os_fdopen(fd, mode="r", *args, **kwargs): return _original_os_fdopen(fd, mode, *args, **kwargs) +def _mcp_repl_os_dup(fd): + fd = operator.index(fd) + dup_fd = _original_os_dup(fd) + if _mcp_repl_is_raw_stdin_fd(fd): + _mcp_repl_note_raw_stdin_fd(dup_fd) + return dup_fd + + +def _mcp_repl_os_dup2(fd, fd2, *args, **kwargs): + fd = operator.index(fd) + fd2 = operator.index(fd2) + result = _original_os_dup2(fd, fd2, *args, **kwargs) + target_fd = fd2 if result is None else result + if _mcp_repl_is_raw_stdin_fd(fd): + _mcp_repl_note_raw_stdin_fd(target_fd) + else: + _mcp_repl_forget_raw_stdin_fd(target_fd) + return result + + +def _mcp_repl_os_close(fd): + fd = operator.index(fd) + try: + return _original_os_close(fd) + finally: + _mcp_repl_forget_raw_stdin_fd(fd) + + class _McpReplFileIOMeta(type): def __instancecheck__(cls, instance): return isinstance(instance, (_original_io_FileIO, McpRawInputBuffer)) @@ -1030,21 +1091,18 @@ def _mcp_repl_os_readv(fd, buffers): return _mcp_repl_fill_readv_buffers(views, _mcp_repl.raw_stdin_read(total)) -builtins.__import__ = _mcp_repl_import -pydoc.pager = _pydoc_plainpager -sys.excepthook = _mcp_repl_excepthook -_mcp_repl.set_python_prompts(_mcp_repl_ps1, _mcp_repl_ps2) -if _mcp_repl_c_stdio_tty: - sys.ps1 = _mcp_repl_ps1 - sys.ps2 = _mcp_repl_ps2 -else: - builtins.input = _input +def _mcp_repl_install_direct_stdin_bridges(): builtins.open = _mcp_repl_open io.open = _mcp_repl_open io.FileIO = _McpReplFileIO _io.open = _mcp_repl_open _io.FileIO = _McpReplFileIO os.fdopen = _mcp_repl_os_fdopen + if os.name == "nt": + os.dup = _mcp_repl_os_dup + if _original_os_dup2 is not None: + os.dup2 = _mcp_repl_os_dup2 + os.close = _mcp_repl_os_close os.read = _mcp_repl_os_read if _original_os_readv is not None: os.readv = _mcp_repl_os_readv @@ -1052,6 +1110,32 @@ def _mcp_repl_os_readv(fd, buffers): _mcp_repl_posix.read = _mcp_repl_os_read if _original_os_readv is not None: _mcp_repl_posix.readv = _mcp_repl_os_readv + if _mcp_repl_nt is not None: + if hasattr(_mcp_repl_nt, "dup"): + _mcp_repl_nt.dup = _mcp_repl_os_dup + if _original_os_dup2 is not None and hasattr(_mcp_repl_nt, "dup2"): + _mcp_repl_nt.dup2 = _mcp_repl_os_dup2 + if hasattr(_mcp_repl_nt, "close"): + _mcp_repl_nt.close = _mcp_repl_os_close + _mcp_repl_nt.read = _mcp_repl_os_read + + +builtins.__import__ = _mcp_repl_import +pydoc.pager = _pydoc_plainpager +sys.excepthook = _mcp_repl_excepthook +_mcp_repl.set_python_prompts(_mcp_repl_ps1, _mcp_repl_ps2) +if _mcp_repl_c_stdio_tty: + if os.name == "nt": + builtins.input = _input + _mcp_repl_install_direct_stdin_bridges() + _mcp_repl_stdin = McpInputStream(tty=True) + sys.stdin = _mcp_repl_stdin + sys.__stdin__ = _mcp_repl_stdin + sys.ps1 = _mcp_repl_ps1 + sys.ps2 = _mcp_repl_ps2 +else: + builtins.input = _input + _mcp_repl_install_direct_stdin_bridges() sys.ps1 = _mcp_repl_suppressed_ps1 sys.ps2 = _mcp_repl_suppressed_ps2 _mcp_repl_stdin = McpInputStream() diff --git a/src/backend.rs b/src/backend.rs index 1807512d..d6813fa7 100644 --- a/src/backend.rs +++ b/src/backend.rs @@ -39,7 +39,9 @@ impl WorkerLaunch { pub fn stdin_transport(&self) -> WorkerStdinTransport { match self { - Self::Builtin(Backend::Python) if cfg!(target_family = "unix") => { + Self::Builtin(Backend::Python) + if cfg!(any(target_family = "unix", target_os = "windows")) => + { WorkerStdinTransport::Pty } Self::Builtin(_) => WorkerStdinTransport::Pipe, @@ -198,12 +200,12 @@ mod tests { WorkerLaunch::Builtin(Backend::R).stdin_transport(), WorkerStdinTransport::Pipe ); - #[cfg(not(target_family = "unix"))] + #[cfg(not(any(target_family = "unix", target_os = "windows")))] assert_eq!( WorkerLaunch::Builtin(Backend::Python).stdin_transport(), WorkerStdinTransport::Pipe ); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", target_os = "windows"))] assert_eq!( WorkerLaunch::Builtin(Backend::Python).stdin_transport(), WorkerStdinTransport::Pty diff --git a/src/install.rs b/src/install.rs index b666c6a6..43bc093d 100644 --- a/src/install.rs +++ b/src/install.rs @@ -775,7 +775,7 @@ repl = { command = "/usr/local/bin/old-mcp-repl", args = ["--interpreter", "r"] [mcp_servers] # keep this note repl={command="/usr/local/bin/old-mcp-repl",args=["--interpreter","r"]} -r = { command = "/usr/local/bin/legacy-repl" } +r = { command = "/usr/local/bin/other-repl" } [workspace] name="demo" @@ -804,7 +804,7 @@ name="demo" ); assert_eq!( doc["mcp_servers"]["r"]["command"].as_str(), - Some("/usr/local/bin/legacy-repl"), + Some("/usr/local/bin/other-repl"), "other MCP servers should be preserved" ); } diff --git a/src/ipc.rs b/src/ipc.rs index f84aa742..25c55037 100644 --- a/src/ipc.rs +++ b/src/ipc.rs @@ -86,21 +86,7 @@ static WORKER_IPC_ATFORK_REGISTER_RESULT: OnceLock = OnceLock::new(); #[derive(Debug, Clone, Serialize, Deserialize)] #[serde(tag = "type", rename_all = "snake_case")] pub enum ServerToWorkerIpcMessage { - RequestStart, - PythonRequestStart { - request_generation: u64, - }, - StdinWrite { - byte_len: usize, - #[serde(default)] - line_count: usize, - #[serde(default, skip_serializing_if = "Option::is_none")] - final_prompt: Option, - }, - StdinWriteComplete, - PythonInterrupt { - request_generation: u64, - }, + PythonInterrupt { request_generation: u64 }, Interrupt, } @@ -112,11 +98,6 @@ pub enum WorkerToServerIpcMessage { worker: WorkerIdentity, capabilities: WorkerCapabilities, }, - BackendInfo { - #[serde(default)] - supports_images: bool, - }, - StdinWriteAck, PythonInterruptAck, OutputText { stream: TextStream, @@ -133,16 +114,11 @@ pub enum WorkerToServerIpcMessage { ReadlineDiscard { text: String, }, - ReadlineResult { - prompt: String, - line: String, + ReadlineInputBytes { + data_b64: String, }, - PlotImage { - mime_type: String, - data: String, - is_update: bool, - #[serde(default, skip_serializing_if = "Option::is_none")] - source: Option, + ReadlineDiscardBytes { + data_b64: String, }, OutputImage { image_id: String, @@ -151,8 +127,7 @@ pub enum WorkerToServerIpcMessage { update: bool, }, SessionEnd { - #[serde(default)] - reason: Option, + reason: String, #[serde(default)] message_b64: Option, }, @@ -185,9 +160,7 @@ struct ServerIpcInbox { startup_message_seen: bool, last_prompt: Option, prompt_history: VecDeque, - echo_events: VecDeque, active_stdin: Option>, - readline_result_count: u64, readline_unmatched_starts: usize, readline_unmatched_since: Option, current_image_id: Option, @@ -227,21 +200,19 @@ pub struct IpcOutputText { } #[derive(Clone)] -pub struct IpcPlotImage { +pub struct IpcOutputImage { pub id: String, pub mime_type: String, pub data: String, pub is_new: bool, pub updates_previous_image: bool, - pub readline_results_seen: usize, } #[derive(Default, Clone)] pub struct IpcHandlers { pub on_output_text: Option>, - pub on_plot_image: Option>, + pub on_output_image: Option>, pub on_readline_start: Option>, - pub on_readline_result: Option>, pub on_session_end: Option>, } @@ -279,7 +250,9 @@ impl OutputCriticalIpcWriter { .writer .lock() .map_err(|_| io::Error::other("ipc writer mutex poisoned"))?; - write_ipc_message(&mut **writer, &message) + let result = write_ipc_message(&mut **writer, &message); + drop(writer); + result } } @@ -313,9 +286,8 @@ impl ServerIpcConnection { let reader_inbox = inbox.clone(); let reader_cvar = cvar.clone(); let output_text_handler = handlers.on_output_text.clone(); - let plot_handler = handlers.on_plot_image.clone(); + let output_image_handler = handlers.on_output_image.clone(); let readline_start_handler = handlers.on_readline_start.clone(); - let readline_result_handler = handlers.on_readline_result.clone(); let session_end_handler = handlers.on_session_end.clone(); let IpcTransport { reader, writer } = transport; let writer = OutputCriticalIpcWriter::new(writer); @@ -366,16 +338,12 @@ impl ServerIpcConnection { break; } if !guard.startup_message_seen { - let startup_message = matches!( - &message, - WorkerToServerIpcMessage::BackendInfo { .. } - | WorkerToServerIpcMessage::WorkerReady { .. } - | WorkerToServerIpcMessage::SessionEnd { .. } - ); + let startup_message = + matches!(&message, WorkerToServerIpcMessage::WorkerReady { .. }); if !startup_message { latch_protocol_error( &mut guard, - "first worker sideband message must be worker_ready or backend_info", + "first worker sideband message must be worker_ready", ); reader_cvar.notify_all(); break; @@ -415,9 +383,20 @@ impl ServerIpcConnection { handler(prompt_for_handler); } } - WorkerToServerIpcMessage::ReadlineInput { text } => { + WorkerToServerIpcMessage::ReadlineInputBytes { data_b64 } => { + let bytes = match decode_sideband_base64(&data_b64, "readline_input_bytes") + { + Ok(bytes) => bytes, + Err(err) => { + let mut guard = reader_inbox.lock().unwrap(); + latch_protocol_error(&mut guard, err); + reader_cvar.notify_all(); + break; + } + }; let mut guard = reader_inbox.lock().unwrap(); - if let Err(err) = account_active_stdin(&mut guard, &text, "readline_input") + if let Err(err) = + account_active_stdin_bytes(&mut guard, &bytes, "readline_input_bytes") { latch_protocol_error(&mut guard, err); reader_cvar.notify_all(); @@ -425,10 +404,33 @@ impl ServerIpcConnection { } reader_cvar.notify_all(); } - WorkerToServerIpcMessage::ReadlineDiscard { text } => { + WorkerToServerIpcMessage::ReadlineInput { text } => { + let mut guard = reader_inbox.lock().unwrap(); + if let Err(err) = account_active_stdin_bytes( + &mut guard, + text.as_bytes(), + "readline_input", + ) { + latch_protocol_error(&mut guard, err); + reader_cvar.notify_all(); + break; + } + reader_cvar.notify_all(); + } + WorkerToServerIpcMessage::ReadlineDiscardBytes { data_b64 } => { + let bytes = + match decode_sideband_base64(&data_b64, "readline_discard_bytes") { + Ok(bytes) => bytes, + Err(err) => { + let mut guard = reader_inbox.lock().unwrap(); + latch_protocol_error(&mut guard, err); + reader_cvar.notify_all(); + break; + } + }; let mut guard = reader_inbox.lock().unwrap(); if let Err(err) = - account_active_stdin(&mut guard, &text, "readline_discard") + account_active_stdin_bytes(&mut guard, &bytes, "readline_discard_bytes") { latch_protocol_error(&mut guard, err); reader_cvar.notify_all(); @@ -436,34 +438,24 @@ impl ServerIpcConnection { } reader_cvar.notify_all(); } - WorkerToServerIpcMessage::ReadlineResult { prompt, line } => { - let echo_event = IpcEchoEvent { - prompt: prompt.clone(), - line: line.clone(), - source: OutputTextSource::Ipc, - }; + WorkerToServerIpcMessage::ReadlineDiscard { text } => { let mut guard = reader_inbox.lock().unwrap(); - guard.readline_result_count = guard.readline_result_count.saturating_add(1); - if guard.readline_unmatched_starts > 0 { - guard.readline_unmatched_starts -= 1; - if guard.readline_unmatched_starts == 0 { - guard.readline_unmatched_since = None; - } + if let Err(err) = account_active_stdin_bytes( + &mut guard, + text.as_bytes(), + "readline_discard", + ) { + latch_protocol_error(&mut guard, err); + reader_cvar.notify_all(); + break; } - guard.echo_events.push_back(echo_event.clone()); reader_cvar.notify_all(); - drop(guard); - if let Some(handler) = readline_result_handler.as_ref() { - handler(echo_event); - } } WorkerToServerIpcMessage::SessionEnd { reason, message_b64, } => { - if let Err(err) = - validate_session_end(reason.as_deref(), message_b64.as_deref()) - { + if let Err(err) = validate_session_end(&reason, message_b64.as_deref()) { let mut guard = reader_inbox.lock().unwrap(); latch_protocol_error(&mut guard, err); reader_cvar.notify_all(); @@ -513,43 +505,6 @@ impl ServerIpcConnection { reader_cvar.notify_all(); } } - WorkerToServerIpcMessage::PlotImage { - mime_type, - data, - is_update, - source, - } => { - let (id, is_new, updates_previous_image, readline_results_seen) = { - let mut guard = reader_inbox.lock().unwrap(); - let (id, is_new, updates_previous_image) = - assign_plot_image_id(&mut guard, source.as_deref(), is_update); - ( - id, - is_new, - updates_previous_image, - guard.readline_result_count as usize, - ) - }; - if let Some(handler) = plot_handler.as_ref() { - handler(IpcPlotImage { - id, - mime_type, - data, - is_new, - updates_previous_image, - readline_results_seen, - }); - } else { - let mut guard = reader_inbox.lock().unwrap(); - guard.queue.push_back(WorkerToServerIpcMessage::PlotImage { - mime_type, - data, - is_update, - source, - }); - reader_cvar.notify_all(); - } - } WorkerToServerIpcMessage::OutputImage { image_id, mime_type, @@ -565,25 +520,17 @@ impl ServerIpcConnection { reader_cvar.notify_all(); break; } - let (id, is_new, updates_previous_image, readline_results_seen) = { + let (id, is_new, updates_previous_image) = { let mut guard = reader_inbox.lock().unwrap(); - let (id, is_new, updates_previous_image) = - assign_plot_image_id(&mut guard, Some(&image_id), update); - ( - id, - is_new, - updates_previous_image, - guard.readline_result_count as usize, - ) + assign_plot_image_id(&mut guard, Some(&image_id), update) }; - if let Some(handler) = plot_handler.as_ref() { - handler(IpcPlotImage { + if let Some(handler) = output_image_handler.as_ref() { + handler(IpcOutputImage { id, mime_type, data: data_b64, is_new, updates_previous_image, - readline_results_seen, }); } else { let mut guard = reader_inbox.lock().unwrap(); @@ -631,13 +578,14 @@ impl ServerIpcConnection { Ok(()) } - #[cfg_attr(target_family = "unix", allow(dead_code))] + #[cfg_attr( + any(target_family = "unix", target_family = "windows"), + allow(dead_code) + )] pub fn begin_request(&self) { let mut guard = self.inbox.lock().unwrap(); reset_after_completed_request(&mut guard); - drop_stdin_write_acks(&mut guard); drop_python_interrupt_acks(&mut guard); - guard.echo_events.clear(); guard.prompt_history.clear(); guard.protocol_warnings.clear(); } @@ -645,10 +593,8 @@ impl ServerIpcConnection { pub fn begin_request_with_stdin(&self, payload: &[u8]) { let mut guard = self.inbox.lock().unwrap(); reset_after_completed_request(&mut guard); - drop_stdin_write_acks(&mut guard); drop_python_interrupt_acks(&mut guard); guard.active_stdin = Some(payload.iter().copied().collect()); - guard.echo_events.clear(); guard.prompt_history.clear(); guard.protocol_warnings.clear(); } @@ -658,16 +604,6 @@ impl ServerIpcConnection { guard.prompt_history.drain(..).collect() } - pub fn take_echo_events(&self) -> Vec { - let mut guard = self.inbox.lock().unwrap(); - guard.echo_events.drain(..).collect() - } - - pub fn pending_echo_event_count(&self) -> usize { - let guard = self.inbox.lock().unwrap(); - guard.echo_events.len() - } - pub fn take_protocol_warnings(&self) -> Vec { let mut guard = self.inbox.lock().unwrap(); guard.protocol_warnings.drain(..).collect() @@ -777,51 +713,11 @@ impl ServerIpcConnection { } } - pub fn try_take_prompt(&self) -> Option { - let mut guard = self.inbox.lock().unwrap(); - guard.last_prompt.take() - } - - pub fn wait_for_backend_info( - &self, - timeout: Duration, - ) -> Result { - let deadline = Instant::now() + timeout; - let mut guard = self.inbox.lock().unwrap(); - loop { - if let Some(info) = take_backend_info(&mut guard) { - let _ = take_session_end(&mut guard); - return Ok(info); - } - if let Some(message) = take_latched_protocol_error(&mut guard) { - return Err(IpcWaitError::Protocol(message)); - } - if take_session_end(&mut guard) { - return Err(IpcWaitError::SessionEnd); - } - if guard.disconnected { - return Err(IpcWaitError::Disconnected); - } - - let now = Instant::now(); - if now >= deadline { - return Err(IpcWaitError::Timeout); - } - let remaining = deadline.saturating_duration_since(now); - let (next_guard, timeout_res) = self.cvar.wait_timeout(guard, remaining).unwrap(); - guard = next_guard; - if timeout_res.timed_out() { - return Err(IpcWaitError::Timeout); - } - } - } - - #[cfg_attr(target_family = "unix", allow(dead_code))] - pub fn wait_for_stdin_write_ack(&self, timeout: Duration) -> Result<(), IpcWaitError> { + pub fn wait_for_python_interrupt_ack(&self, timeout: Duration) -> Result<(), IpcWaitError> { let deadline = Instant::now() + timeout; let mut guard = self.inbox.lock().unwrap(); loop { - if take_stdin_write_ack(&mut guard) { + if take_python_interrupt_ack(&mut guard) { return Ok(()); } if let Some(message) = take_latched_protocol_error(&mut guard) { @@ -842,24 +738,29 @@ impl ServerIpcConnection { let (next_guard, timeout_res) = self.cvar.wait_timeout(guard, remaining).unwrap(); guard = next_guard; if timeout_res.timed_out() { - if take_stdin_write_ack(&mut guard) { + if take_python_interrupt_ack(&mut guard) { return Ok(()); } - if let Some(message) = take_latched_protocol_error(&mut guard) { - return Err(IpcWaitError::Protocol(message)); - } return Err(IpcWaitError::Timeout); } } } - #[cfg_attr(not(target_family = "unix"), allow(dead_code))] - pub fn wait_for_python_interrupt_ack(&self, timeout: Duration) -> Result<(), IpcWaitError> { + pub fn try_take_prompt(&self) -> Option { + let mut guard = self.inbox.lock().unwrap(); + guard.last_prompt.take() + } + + pub fn wait_for_worker_ready( + &self, + timeout: Duration, + ) -> Result { let deadline = Instant::now() + timeout; let mut guard = self.inbox.lock().unwrap(); loop { - if take_python_interrupt_ack(&mut guard) { - return Ok(()); + if let Some(info) = take_worker_ready(&mut guard) { + let _ = take_session_end(&mut guard); + return Ok(info); } if let Some(message) = take_latched_protocol_error(&mut guard) { return Err(IpcWaitError::Protocol(message)); @@ -879,12 +780,6 @@ impl ServerIpcConnection { let (next_guard, timeout_res) = self.cvar.wait_timeout(guard, remaining).unwrap(); guard = next_guard; if timeout_res.timed_out() { - if take_python_interrupt_ack(&mut guard) { - return Ok(()); - } - if let Some(message) = take_latched_protocol_error(&mut guard) { - return Err(IpcWaitError::Protocol(message)); - } return Err(IpcWaitError::Timeout); } } @@ -1009,8 +904,9 @@ impl WorkerIpcConnection { fn write_ipc_message(writer: &mut dyn Write, message: &T) -> io::Result<()> { let payload = serde_json::to_string(message).map_err(io::Error::other)?; writer.write_all(payload.as_bytes())?; - writer.write_all(b"\n")?; - writer.flush() + // IPC transports are unbuffered OS pipes. On Windows named pipes, flushing + // can wait for peer drainage, so a complete JSONL write is the sync point. + writer.write_all(b"\n") } #[derive(Debug)] @@ -1113,7 +1009,7 @@ impl IpcServer { self, handle: IpcHandle, handlers: IpcHandlers, - child: &mut std::process::Child, + child_exited: impl FnMut() -> io::Result, max_wait: Duration, ) -> io::Result<()> { let Some(server_pipe_to_worker) = self.server_pipe_to_worker else { @@ -1127,9 +1023,10 @@ impl IpcServer { )); }; let start = Instant::now(); - connect_named_pipe_with_process_retry(&server_pipe_to_worker, child, max_wait)?; + let child_exited = std::cell::RefCell::new(child_exited); + connect_named_pipe_with_process_retry(&server_pipe_to_worker, &child_exited, max_wait)?; let remaining = max_wait.saturating_sub(start.elapsed()); - connect_named_pipe_with_process_retry(&server_pipe_from_worker, child, remaining)?; + connect_named_pipe_with_process_retry(&server_pipe_from_worker, &child_exited, remaining)?; let conn = ServerIpcConnection::new( IpcTransport { reader: Box::new(server_pipe_from_worker), @@ -1459,12 +1356,12 @@ fn join_connector_with_grace(connector: thread::JoinHandle<()>, max_wait: Durati #[cfg(target_family = "windows")] fn connect_named_pipe_with_process_retry( server_pipe: &File, - child: &mut std::process::Child, + child_exited: &std::cell::RefCell io::Result>, max_wait: Duration, ) -> io::Result<()> { connect_named_pipe_with_process_retry_impl( |timeout| connect_named_pipe(server_pipe, timeout), - || child.try_wait().map(|status| status.is_some()), + || child_exited.borrow_mut()(), max_wait, ) } @@ -1621,6 +1518,14 @@ pub fn connect_from_env(_timeout: Duration) -> io::Result { } if let Some((reader, writer)) = take_pipe_pair_if_ready(&mut reader, &mut writer) { + // The main worker owns the live sideband pipe handles. Once startup has consumed + // the bootstrap names, user code and descendants must not see or reuse them. + // SAFETY: worker startup consumes these env vars before any worker-managed + // threads exist. + unsafe { + std::env::remove_var(IPC_PIPE_TO_WORKER_ENV); + std::env::remove_var(IPC_PIPE_FROM_WORKER_ENV); + } return WorkerIpcConnection::new(IpcTransport { reader: Box::new(reader), writer: Box::new(writer), @@ -1708,27 +1613,18 @@ pub fn emit_readline_start(prompt: &str) { } } -pub fn emit_readline_input(text: &str) { - if let Some(ipc) = global_ipc() { - let _ = ipc.send(WorkerToServerIpcMessage::ReadlineInput { - text: text.to_string(), - }); - } -} - -pub fn emit_readline_discard(text: &str) { +pub fn emit_readline_input_bytes(bytes: &[u8]) { if let Some(ipc) = global_ipc() { - let _ = ipc.send(WorkerToServerIpcMessage::ReadlineDiscard { - text: text.to_string(), + let _ = ipc.send(WorkerToServerIpcMessage::ReadlineInputBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode(bytes), }); } } -pub fn emit_readline_result(prompt: &str, line: &str) { +pub fn emit_readline_discard_bytes(bytes: &[u8]) { if let Some(ipc) = global_ipc() { - let _ = ipc.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: prompt.to_string(), - line: line.to_string(), + let _ = ipc.send(WorkerToServerIpcMessage::ReadlineDiscardBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode(bytes), }); } } @@ -1738,13 +1634,13 @@ pub fn emit_output_text(stream: TextStream, bytes: &[u8]) -> io::Result<()> { ipc.send_output_text(stream, bytes) } -pub fn emit_plot_image(mime_type: &str, data: &str, is_update: bool, source: Option<&str>) { +pub fn emit_output_image(image_id: &str, mime_type: &str, data_b64: &str, update: bool) { if let Some(ipc) = global_ipc() { - let _ = ipc.send(WorkerToServerIpcMessage::PlotImage { + let _ = ipc.send(WorkerToServerIpcMessage::OutputImage { + image_id: image_id.to_string(), mime_type: mime_type.to_string(), - data: data.to_string(), - is_update, - source: source.map(ToString::to_string), + data_b64: data_b64.to_string(), + update, }); } } @@ -1767,9 +1663,12 @@ pub fn emit_worker_ready(worker_name: &str, supports_images: bool) { } } -pub fn emit_stdin_write_ack() { +pub fn emit_session_end() { if let Some(ipc) = global_ipc() { - let _ = ipc.send(WorkerToServerIpcMessage::StdinWriteAck); + let _ = ipc.send(WorkerToServerIpcMessage::SessionEnd { + reason: "runtime_exit".to_string(), + message_b64: None, + }); } } @@ -1779,15 +1678,6 @@ pub fn emit_python_interrupt_ack() { } } -pub fn emit_session_end() { - if let Some(ipc) = global_ipc() { - let _ = ipc.send(WorkerToServerIpcMessage::SessionEnd { - reason: None, - message_b64: None, - }); - } -} - #[cfg(test)] pub(crate) fn test_connection_pair() -> io::Result<(ServerIpcConnection, WorkerIpcConnection)> { test_connection_pair_with_handlers(IpcHandlers::default()) @@ -1829,18 +1719,17 @@ fn take_session_end(guard: &mut ServerIpcInbox) -> bool { true } -fn account_active_stdin( +fn account_active_stdin_bytes( guard: &mut ServerIpcInbox, - text: &str, + bytes: &[u8], event_type: &str, ) -> Result<(), String> { let Some(active_stdin) = guard.active_stdin.as_mut() else { - if text.is_empty() { + if bytes.is_empty() { return Ok(()); } return Err(format!("{event_type} reported input with no active turn")); }; - let bytes = text.as_bytes(); if bytes.len() > active_stdin.len() { return Err(format!( "{event_type} reported {} bytes but only {} active stdin bytes remain", @@ -1851,7 +1740,7 @@ fn account_active_stdin( for (idx, expected) in bytes.iter().enumerate() { if active_stdin.get(idx) != Some(expected) { return Err(format!( - "{event_type} text does not match active stdin at byte {idx}" + "{event_type} bytes does not match active stdin at byte {idx}" )); } } @@ -1861,44 +1750,10 @@ fn account_active_stdin( Ok(()) } -#[cfg_attr(target_family = "unix", allow(dead_code))] -fn take_stdin_write_ack(guard: &mut ServerIpcInbox) -> bool { - if let Some(idx) = guard - .queue - .iter() - .position(|msg| matches!(msg, WorkerToServerIpcMessage::StdinWriteAck)) - { - guard.queue.remove(idx); - true - } else { - false - } -} - -#[cfg_attr(not(target_family = "unix"), allow(dead_code))] -fn take_python_interrupt_ack(guard: &mut ServerIpcInbox) -> bool { - if let Some(idx) = guard - .queue - .iter() - .position(|msg| matches!(msg, WorkerToServerIpcMessage::PythonInterruptAck)) - { - guard.queue.remove(idx); - true - } else { - false - } -} - -fn drop_stdin_write_acks(guard: &mut ServerIpcInbox) { - guard - .queue - .retain(|msg| !matches!(msg, WorkerToServerIpcMessage::StdinWriteAck)); -} - -fn drop_python_interrupt_acks(guard: &mut ServerIpcInbox) { - guard - .queue - .retain(|msg| !matches!(msg, WorkerToServerIpcMessage::PythonInterruptAck)); +fn decode_sideband_base64(data_b64: &str, event_type: &str) -> Result, String> { + base64::engine::general_purpose::STANDARD + .decode(data_b64) + .map_err(|_| format!("invalid {event_type} base64")) } fn request_completion_ready(guard: &ServerIpcInbox, stable_wait: Duration) -> bool { @@ -1915,12 +1770,10 @@ fn latch_protocol_error(guard: &mut ServerIpcInbox, message: impl Into) }); } -fn validate_session_end(reason: Option<&str>, message_b64: Option<&str>) -> Result<(), String> { - if let Some(reason) = reason { - match reason { - "shutdown" | "reset" | "runtime_exit" | "crash" | "protocol_error" => {} - other => return Err(format!("invalid session_end reason: {other}")), - } +fn validate_session_end(reason: &str, message_b64: Option<&str>) -> Result<(), String> { + match reason { + "shutdown" | "reset" | "runtime_exit" | "crash" | "protocol_error" => {} + other => return Err(format!("invalid session_end reason: {other}")), } if let Some(message_b64) = message_b64 && base64::engine::general_purpose::STANDARD @@ -2054,7 +1907,6 @@ fn assign_plot_image_id( fn reset_request_progress(guard: &mut ServerIpcInbox) { guard.active_stdin = None; - guard.readline_result_count = 0; guard.readline_unmatched_starts = 0; guard.readline_unmatched_since = None; } @@ -2066,17 +1918,32 @@ fn reset_after_completed_request(guard: &mut ServerIpcInbox) { guard.last_prompt = None; } -fn take_backend_info(guard: &mut ServerIpcInbox) -> Option { - let idx = guard.queue.iter().position(|msg| { - matches!( - msg, - WorkerToServerIpcMessage::BackendInfo { .. } - | WorkerToServerIpcMessage::WorkerReady { .. } - ) - })?; +fn take_worker_ready(guard: &mut ServerIpcInbox) -> Option { + let idx = guard + .queue + .iter() + .position(|msg| matches!(msg, WorkerToServerIpcMessage::WorkerReady { .. }))?; guard.queue.remove(idx) } +fn take_python_interrupt_ack(guard: &mut ServerIpcInbox) -> bool { + if let Some(idx) = guard + .queue + .iter() + .position(|msg| matches!(msg, WorkerToServerIpcMessage::PythonInterruptAck)) + { + guard.queue.remove(idx); + return true; + } + false +} + +fn drop_python_interrupt_acks(guard: &mut ServerIpcInbox) { + guard + .queue + .retain(|msg| !matches!(msg, WorkerToServerIpcMessage::PythonInterruptAck)); +} + fn is_false(value: &bool) -> bool { !*value } @@ -2084,10 +1951,9 @@ fn is_false(value: &bool) -> bool { #[cfg(test)] mod protocol_tests { use super::{ - IpcHandlers, IpcTransport, IpcWaitError, OUTPUT_TEXT_IPC_CHUNK_BYTES, - OutputCriticalIpcWriter, ServerIpcConnection, ServerToWorkerIpcMessage, - WorkerToServerIpcMessage, emit_readline_discard, emit_readline_input, - test_connection_pair_with_handlers, + IpcHandlers, IpcTransport, OUTPUT_TEXT_IPC_CHUNK_BYTES, OutputCriticalIpcWriter, + ServerIpcConnection, ServerToWorkerIpcMessage, WorkerToServerIpcMessage, + emit_readline_discard_bytes, emit_readline_input_bytes, test_connection_pair_with_handlers, }; use crate::worker_protocol::TextStream; use base64::Engine as _; @@ -2097,44 +1963,76 @@ mod protocol_tests { use std::thread; use std::time::{Duration, Instant}; - #[test] - fn backend_info_protocol_does_not_include_language() { - let parsed = serde_json::from_value::(json!({ - "type": "backend_info", - "supports_images": true - })); + #[cfg(target_family = "windows")] + static ENV_TEST_MUTEX: Mutex<()> = Mutex::new(()); + + #[cfg(target_family = "windows")] + struct EnvVarGuard { + key: &'static str, + original: Option, + } - assert!(parsed.is_ok(), "backend_info should not require language"); + #[cfg(target_family = "windows")] + impl EnvVarGuard { + fn set(key: &'static str, value: &str) -> Self { + let original = std::env::var_os(key); + unsafe { + std::env::set_var(key, value); + } + Self { key, original } + } + } + + #[cfg(target_family = "windows")] + impl Drop for EnvVarGuard { + fn drop(&mut self) { + unsafe { + if let Some(value) = &self.original { + std::env::set_var(self.key, value); + } else { + std::env::remove_var(self.key); + } + } + } } #[test] - fn backend_info_protocol_rejects_language() { + fn backend_info_protocol_is_removed() { let parsed = serde_json::from_value::(json!({ "type": "backend_info", - "language": "r", "supports_images": true })); - assert!(parsed.is_err(), "backend_info should reject language"); + assert!(parsed.is_err(), "backend_info is no longer part of IPC"); } #[test] - fn plot_image_protocol_uses_update_flag_without_worker_id() { + fn output_image_protocol_uses_worker_source_id_and_server_update_flag() { let parsed = serde_json::from_value::(json!({ - "type": "plot_image", + "type": "output_image", + "image_id": "source-1", "mime_type": "image/png", - "data": "abc", - "is_update": true + "data_b64": "YWJj", + "update": true })); - assert!( - parsed.is_ok(), - "plot_image should not require worker image id" - ); + let Ok(WorkerToServerIpcMessage::OutputImage { + image_id, + mime_type, + data_b64, + update, + }) = parsed + else { + panic!("output_image should deserialize"); + }; + assert_eq!(image_id, "source-1"); + assert_eq!(mime_type, "image/png"); + assert_eq!(data_b64, "YWJj"); + assert!(update); } #[test] - fn plot_image_protocol_rejects_worker_id_and_is_new() { + fn output_image_protocol_rejects_plot_image_shape() { let parsed = serde_json::from_value::(json!({ "type": "plot_image", "id": "plot-1", @@ -2146,14 +2044,49 @@ mod protocol_tests { assert!( parsed.is_err(), - "plot_image should reject old worker-owned image fields" + "plot_image frames are no longer part of IPC" ); } #[test] fn readline_accounting_emitters_are_platform_neutral_noops_without_global_ipc() { - emit_readline_input("answer\n"); - emit_readline_discard("queued\n"); + emit_readline_input_bytes(&[0xc3]); + emit_readline_discard_bytes(&[0xa9]); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_connect_from_env_scrubs_pipe_name_env_vars() { + let _guard = ENV_TEST_MUTEX.lock().expect("env test mutex"); + let mut server = super::IpcServer::bind().expect("bind IPC server"); + let (to_worker, from_worker) = server.take_pipe_names().expect("pipe names"); + let _to_guard = EnvVarGuard::set(super::IPC_PIPE_TO_WORKER_ENV, &to_worker); + let _from_guard = EnvVarGuard::set(super::IPC_PIPE_FROM_WORKER_ENV, &from_worker); + let handle = super::IpcHandle::new(); + let server_thread = thread::spawn(move || { + server.connect( + handle, + IpcHandlers::default(), + || Ok(false), + Duration::from_secs(5), + ) + }); + + let worker = super::connect_from_env(Duration::from_secs(5)).expect("worker IPC connect"); + server_thread + .join() + .expect("join IPC server connect") + .expect("server IPC connect"); + + assert!( + std::env::var_os(super::IPC_PIPE_TO_WORKER_ENV).is_none(), + "to-worker pipe name should be scrubbed after IPC connect" + ); + assert!( + std::env::var_os(super::IPC_PIPE_FROM_WORKER_ENV).is_none(), + "from-worker pipe name should be scrubbed after IPC connect" + ); + drop(worker); } #[test] @@ -2208,71 +2141,59 @@ mod protocol_tests { } #[test] - fn plot_image_protocol_rejects_sequence_ack_handshake() { - let worker_to_server = serde_json::from_value::(json!({ - "type": "plot_image", - "mime_type": "image/png", - "data": "abc", - "is_update": false, - "sequence": 1 + fn protocol_v1_text_input_frames_still_deserialize() { + let input = serde_json::from_value::(json!({ + "type": "readline_input", + "text": "done\n" })); assert!( - worker_to_server.is_err(), - "plot_image should not expose worker-side ack sequencing" + matches!( + input, + Ok(WorkerToServerIpcMessage::ReadlineInput { ref text }) if text == "done\n" + ), + "readline_input should remain a protocol v1 compatibility alias" ); - let server_to_worker = serde_json::from_value::(json!({ - "type": "plot_image_ack", - "sequence": 1 + let discard = serde_json::from_value::(json!({ + "type": "readline_discard", + "text": "stale\n" })); assert!( - server_to_worker.is_err(), - "server-to-worker protocol should not include plot_image_ack" + matches!( + discard, + Ok(WorkerToServerIpcMessage::ReadlineDiscard { ref text }) if text == "stale\n" + ), + "readline_discard should remain a protocol v1 compatibility alias" ); } #[test] - fn request_end_is_not_part_of_worker_to_server_protocol() { - let parsed = serde_json::from_value::(json!({ - "type": "request_end" - })); - - assert!(parsed.is_err(), "request_end should not deserialize"); - } - - #[test] - fn stdin_write_ack_is_worker_to_server_only() { - let parsed = serde_json::from_value::(json!({ - "type": "stdin_write_ack" + fn output_image_protocol_rejects_sequence_ack_handshake() { + let worker_to_server = serde_json::from_value::(json!({ + "type": "output_image", + "image_id": "source-1", + "mime_type": "image/png", + "data_b64": "YWJj", + "update": false, + "sequence": 1 })); assert!( - matches!(parsed, Ok(WorkerToServerIpcMessage::StdinWriteAck)), - "stdin_write_ack should deserialize as the worker-side stdin acceptance signal" + worker_to_server.is_err(), + "output_image should not expose worker-side ack sequencing" ); - let parsed = serde_json::from_value::(json!({ - "type": "stdin_write_ack" + let server_to_worker = serde_json::from_value::(json!({ + "type": "output_image_ack", + "sequence": 1 })); assert!( - parsed.is_err(), - "stdin_write_ack should not deserialize as a server-to-worker message" + server_to_worker.is_err(), + "server-to-worker protocol should not include output_image_ack" ); } #[test] - fn python_request_generation_messages_are_server_to_worker_only() { - let request_start = serde_json::to_value(ServerToWorkerIpcMessage::PythonRequestStart { - request_generation: 7, - }) - .expect("serialize python_request_start"); - assert_eq!( - request_start, - json!({ - "type": "python_request_start", - "request_generation": 7 - }) - ); - + fn python_interrupt_generation_is_server_to_worker_only() { let interrupt = serde_json::from_value::(json!({ "type": "python_interrupt", "request_generation": 7 @@ -2295,52 +2216,31 @@ mod protocol_tests { worker_to_server.is_err(), "python_interrupt should not deserialize as a worker-to-server message" ); - } - #[test] - fn begin_request_drops_stale_stdin_write_acks() { - let (server, worker) = - test_connection_pair_with_handlers(IpcHandlers::default()).expect("ipc pair"); - worker - .send(WorkerToServerIpcMessage::StdinWriteAck) - .expect("send stale ack"); - - let deadline = Instant::now() + Duration::from_secs(1); - let mut guard = server.inbox.lock().unwrap(); - while !guard - .queue - .iter() - .any(|msg| matches!(msg, WorkerToServerIpcMessage::StdinWriteAck)) - { - let remaining = deadline.saturating_duration_since(Instant::now()); - assert!( - !remaining.is_zero(), - "expected stale stdin_write_ack to reach server inbox" - ); - let (next_guard, timeout_res) = server.cvar.wait_timeout(guard, remaining).unwrap(); - guard = next_guard; - assert!( - !timeout_res.timed_out(), - "expected stale stdin_write_ack to reach server inbox" - ); - } - drop(guard); + let ack = serde_json::from_value::(json!({ + "type": "python_interrupt_ack" + })); + assert!( + matches!(ack, Ok(WorkerToServerIpcMessage::PythonInterruptAck)), + "python_interrupt_ack should deserialize as the worker-side cleanup signal" + ); - server.begin_request(); + let server_to_worker_ack = serde_json::from_value::(json!({ + "type": "python_interrupt_ack" + })); assert!( - matches!( - server.wait_for_stdin_write_ack(Duration::ZERO), - Err(IpcWaitError::Timeout) - ), - "begin_request should discard stale stdin_write_ack messages" + server_to_worker_ack.is_err(), + "python_interrupt_ack should not deserialize as a server-to-worker message" ); + } - worker - .send(WorkerToServerIpcMessage::StdinWriteAck) - .expect("send fresh ack"); - server - .wait_for_stdin_write_ack(Duration::from_secs(1)) - .expect("fresh ack should still be accepted"); + #[test] + fn request_end_is_not_part_of_worker_to_server_protocol() { + let parsed = serde_json::from_value::(json!({ + "type": "request_end" + })); + + assert!(parsed.is_err(), "request_end should not deserialize"); } #[test] @@ -2367,7 +2267,7 @@ mod protocol_tests { ) .expect("invalid worker message"); - let result = server.wait_for_backend_info(Duration::from_millis(200)); + let result = server.wait_for_worker_ready(Duration::from_millis(200)); assert!( matches!(result, Err(super::IpcWaitError::Protocol(ref message)) if message.starts_with("invalid worker sideband JSON:")), @@ -2383,16 +2283,10 @@ mod protocol_tests { server.begin_request_with_stdin(b"done\n"); worker - .send(WorkerToServerIpcMessage::ReadlineInput { - text: "done\n".to_string(), - }) - .expect("send readline_input"); - worker - .send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "zod> ".to_string(), - line: "done\n".to_string(), + .send(WorkerToServerIpcMessage::ReadlineInputBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode(b"done\n"), }) - .expect("send readline_result"); + .expect("send readline_input_bytes"); worker .send(WorkerToServerIpcMessage::ReadlineStart { prompt: "zod> ".to_string(), @@ -2417,6 +2311,75 @@ mod protocol_tests { assert_eq!(latched.as_deref(), Some("invalid output_text base64")); } + #[test] + fn request_completion_accounts_split_utf8_byte_frames() { + let stable_wait = Duration::from_millis(20); + let (server, worker) = + test_connection_pair_with_handlers(IpcHandlers::default()).expect("ipc pair"); + + server.begin_request_with_stdin("é\n".as_bytes()); + worker + .send(WorkerToServerIpcMessage::ReadlineInputBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode([0xc3]), + }) + .expect("send first byte"); + worker + .send(WorkerToServerIpcMessage::ReadlineInputBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode([0xa9]), + }) + .expect("send second byte"); + worker + .send(WorkerToServerIpcMessage::ReadlineInputBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode(b"\n"), + }) + .expect("send newline byte"); + worker + .send(WorkerToServerIpcMessage::ReadlineStart { + prompt: ">>> ".to_string(), + }) + .expect("send readline_start"); + thread::sleep(stable_wait + Duration::from_millis(5)); + + let completion = server.wait_for_request_completion(Duration::from_secs(1), stable_wait); + + assert!( + completion.is_ok(), + "split UTF-8 byte accounting should allow prompt completion, got: {completion:?}" + ); + } + + #[test] + fn request_completion_accepts_protocol_v1_text_input_frames() { + let stable_wait = Duration::from_millis(20); + let (server, worker) = + test_connection_pair_with_handlers(IpcHandlers::default()).expect("ipc pair"); + + server.begin_request_with_stdin(b"done\nstale\n"); + worker + .send(WorkerToServerIpcMessage::ReadlineInput { + text: "done\n".to_string(), + }) + .expect("send readline_input"); + worker + .send(WorkerToServerIpcMessage::ReadlineDiscard { + text: "stale\n".to_string(), + }) + .expect("send readline_discard"); + worker + .send(WorkerToServerIpcMessage::ReadlineStart { + prompt: ">>> ".to_string(), + }) + .expect("send readline_start"); + thread::sleep(stable_wait + Duration::from_millis(5)); + + let completion = server.wait_for_request_completion(Duration::from_secs(1), stable_wait); + + assert!( + completion.is_ok(), + "protocol v1 text input/discard accounting should allow prompt completion, got: {completion:?}" + ); + } + #[test] fn output_critical_writer_flushes_before_returning() { let (server_read, worker_write) = std::io::pipe().expect("server pipe"); @@ -2552,28 +2515,30 @@ mod protocol_tests { } #[test] - fn plot_image_updates_reuse_current_server_image_id() { + fn output_image_updates_reuse_current_server_image_id() { let images = Arc::new(Mutex::new(Vec::new())); let handler_images = images.clone(); let (_server, worker) = test_connection_pair_with_handlers(IpcHandlers { - on_plot_image: Some(Arc::new(move |image| { + on_output_image: Some(Arc::new(move |image| { handler_images.lock().expect("image mutex").push(image); })), ..IpcHandlers::default() }) .expect("ipc pair"); let first = json!({ - "type": "plot_image", + "type": "output_image", + "image_id": "source-1", "mime_type": "image/png", - "data": "first", - "is_update": false + "data_b64": "Zmlyc3Q=", + "update": false }) .to_string(); let second = json!({ - "type": "plot_image", + "type": "output_image", + "image_id": "source-1", "mime_type": "image/png", - "data": "second", - "is_update": true + "data_b64": "c2Vjb25k", + "update": true }) .to_string(); @@ -2596,27 +2561,28 @@ mod protocol_tests { assert_eq!(images[0].id, images[1].id); assert!(images[0].is_new); assert!(!images[1].is_new); - assert_eq!(images[0].data, "first"); - assert_eq!(images[1].data, "second"); + assert_eq!(images[0].data, "Zmlyc3Q="); + assert_eq!(images[1].data, "c2Vjb25k"); } #[test] - fn plot_image_ids_do_not_repeat_across_server_connections() { + fn output_image_ids_do_not_repeat_across_server_connections() { fn next_connection_image_id() -> String { let images = Arc::new(Mutex::new(Vec::new())); let handler_images = images.clone(); let (_server, worker) = test_connection_pair_with_handlers(IpcHandlers { - on_plot_image: Some(Arc::new(move |image| { + on_output_image: Some(Arc::new(move |image| { handler_images.lock().expect("image mutex").push(image); })), ..IpcHandlers::default() }) .expect("ipc pair"); let image = json!({ - "type": "plot_image", + "type": "output_image", + "image_id": "source-1", "mime_type": "image/png", - "data": "image", - "is_update": false + "data_b64": "aW1hZ2U=", + "update": false }) .to_string(); diff --git a/src/main.rs b/src/main.rs index 8c5564c0..52d35cf9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -24,6 +24,8 @@ mod sandbox; mod sandbox_cli; mod server; mod stdin_payload; +#[cfg(target_family = "windows")] +mod windows_pty_filter; #[cfg(target_os = "windows")] mod windows_sandbox; mod worker; @@ -495,7 +497,7 @@ mcp-repl install [--client ]... [--interpreter [,r|pytho --debug-repl: run an interactive debug REPL over stdio\n\ --debug-dir: optional base directory for per-startup debug artifacts (env: MCP_REPL_DEBUG_DIR)\n\ --interpreter: choose REPL interpreter (default: r; env MCP_REPL_INTERPRETER)\n\ ---oversized-output: choose oversized-output handling (pager: default legacy modal pager; files: spill oversized replies to files)\n\ +--oversized-output: choose oversized-output handling (pager: default interactive pager; files: spill oversized replies to files)\n\ --sandbox: base sandbox mode (inherit uses client tool-call metadata; --debug-repl bootstraps local defaults)\n\ --add-writable-root / --add-writeable-root: append absolute writable root in argument order\n\ --add-allowed-domain: append allowed domain pattern in argument order\n\ diff --git a/src/pending_output_tape.rs b/src/pending_output_tape.rs index 149ab650..9df5cf16 100644 --- a/src/pending_output_tape.rs +++ b/src/pending_output_tape.rs @@ -22,8 +22,6 @@ struct PendingOutputTapeInner { events: VecDeque, stdout_tail: PendingTextTail, stderr_tail: PendingTextTail, - drained_readline_results: usize, - pending_echo_prefix: Option, last_rendered_text: Option, } @@ -79,14 +77,7 @@ impl PendingOutputEvent { #[derive(Clone, Debug, PartialEq, Eq)] pub(crate) enum PendingSidebandKind { - ReadlineStart { - prompt: String, - }, - ReadlineResult { - prompt: String, - line: String, - echo_source: PendingTextSource, - }, + ReadlineStart { prompt: String }, RequestBoundary, SessionEnd, } @@ -94,8 +85,6 @@ pub(crate) enum PendingSidebandKind { #[derive(Clone, Debug, Default, PartialEq, Eq)] pub(crate) struct PendingOutputSnapshot { pub events: Vec, - readline_result_base: usize, - leading_echo_prefix: Option, prior_rendered_text: Option, } @@ -108,7 +97,7 @@ pub(crate) struct FormattedPendingOutput { #[derive(Clone, Copy, Debug, Default, PartialEq, Eq)] pub(crate) struct PendingOutputSettleState { pub progress_seq: u64, - pub readline_results_seen: usize, + pub sideband_events_seen: usize, pub has_image: bool, } @@ -125,66 +114,6 @@ pub(crate) enum PendingTextSource { Ipc, } -#[derive(Clone, Debug, Default, PartialEq, Eq)] -struct PendingEchoPrefix { - segments: Vec, -} - -#[derive(Clone, Debug, PartialEq, Eq)] -struct PendingEchoSegment { - text: String, - source: PendingTextSource, -} - -impl PendingEchoPrefix { - fn push(&mut self, source: PendingTextSource, text: String) { - if text.is_empty() { - return; - } - if let Some(last) = self.segments.last_mut() - && last.source == source - { - last.text.push_str(&text); - return; - } - self.segments.push(PendingEchoSegment { text, source }); - } - - fn is_empty(&self) -> bool { - self.segments.iter().all(|segment| segment.text.is_empty()) - } - - fn text_prefix(&self, mut byte_len: usize) -> String { - let mut text = String::new(); - for segment in &self.segments { - if byte_len == 0 { - break; - } - let take = byte_len.min(segment.text.len()); - text.push_str(&segment.text[..take]); - byte_len -= take; - } - text - } - - fn suffix_after(&self, mut byte_len: usize) -> Option { - let mut segments = Vec::new(); - for (idx, segment) in self.segments.iter().enumerate() { - if byte_len >= segment.text.len() { - byte_len -= segment.text.len(); - continue; - } - segments.push(PendingEchoSegment { - text: segment.text[byte_len..].to_string(), - source: segment.source, - }); - segments.extend(self.segments[idx.saturating_add(1)..].iter().cloned()); - break; - } - (!segments.is_empty()).then_some(Self { segments }) - } -} - struct RenderedPendingOutput { range: OutputRange, echo_events: Vec, @@ -400,18 +329,10 @@ impl PendingOutputTape { .inner .lock() .expect("pending output tape mutex poisoned"); - let pending_readline_results = guard + let sideband_events_seen = guard .events .iter() - .filter(|event| { - matches!( - event, - PendingOutputEvent::Sideband { - kind: PendingSidebandKind::ReadlineResult { .. }, - .. - } - ) - }) + .filter(|event| matches!(event, PendingOutputEvent::Sideband { .. })) .count(); let has_image = guard .events @@ -419,7 +340,7 @@ impl PendingOutputTape { .any(|event| matches!(event, PendingOutputEvent::Image { .. })); PendingOutputSettleState { progress_seq: guard.progress_seq, - readline_results_seen: guard.drained_readline_results + pending_readline_results, + sideband_events_seen, has_image, } } @@ -445,48 +366,9 @@ impl PendingOutputTape { flush_tail(&mut guard, TextStream::Stderr, flush_incomplete); let prior_rendered_text = guard.last_rendered_text; let events: Vec<_> = guard.events.drain(..).collect(); - let readline_result_base = guard.drained_readline_results; - let drained_readline_results = events - .iter() - .filter(|event| { - matches!( - event, - PendingOutputEvent::Sideband { - kind: PendingSidebandKind::ReadlineResult { .. }, - .. - } - ) - }) - .count(); - guard.drained_readline_results = guard - .drained_readline_results - .saturating_add(drained_readline_results); - // `pending_echo_prefix` only carries echo that was observed - // sideband-first in an earlier drain. Raw Python prompt echo and - // R-owned IPC echo use the same visible bytes, but they must only trim - // text delivered on their own channel. - let leading_echo_prefix = guard.pending_echo_prefix.clone(); - append_readline_results_to_echo_prefix(&mut guard.pending_echo_prefix, &events); - if let Some(echo_prefix) = guard.pending_echo_prefix.clone() { - let (matched_bytes, keep_remaining_suffix) = - leading_echo_match_progress(&events, &echo_prefix); - if keep_remaining_suffix - && !(snapshot_has_no_visible_text(&events) - && snapshot_crossed_request_boundary(&events)) - { - guard.pending_echo_prefix = echo_prefix.suffix_after(matched_bytes); - } else { - guard.pending_echo_prefix = None; - } - } - if snapshot_crossed_request_boundary(&events) { - guard.drained_readline_results = 0; - } guard.last_rendered_text = rendered_text_state_after(events.iter(), prior_rendered_text); PendingOutputSnapshot { events, - readline_result_base, - leading_echo_prefix, prior_rendered_text, } } @@ -550,24 +432,14 @@ impl PendingOutputSnapshot { saw_stderr, } = self.rendered_output(); let source_end = range.end_offset; - let collapsed = collapse_echo_with_attribution( - range, - &echo_events, - self.readline_result_base, - &prompt_variants, - mode, - ); - let mut contents = pager::contents_from_collapsed_output( + let collapsed = + collapse_echo_with_attribution(range, &echo_events, 0, &prompt_variants, mode); + let contents = pager::contents_from_collapsed_output( collapsed.bytes, collapsed.events, collapsed.text_spans, source_end, ); - maybe_trim_leading_echo_prefix( - self.leading_echo_prefix.as_ref(), - &self.events, - &mut contents, - ); FormattedPendingOutput { contents, saw_stderr, @@ -578,7 +450,7 @@ impl PendingOutputSnapshot { let mut bytes = Vec::new(); let mut text_spans = Vec::new(); let mut events = Vec::new(); - let mut echo_events = Vec::new(); + let echo_events = Vec::new(); let mut prompt_variants = Vec::new(); let mut saw_stderr = false; let mut last_rendered_text = self.prior_rendered_text; @@ -664,18 +536,6 @@ impl PendingOutputSnapshot { PendingSidebandKind::ReadlineStart { prompt } => { push_prompt_variant(&mut prompt_variants, prompt); } - PendingSidebandKind::ReadlineResult { - prompt, - line, - echo_source, - } => { - push_prompt_variant(&mut prompt_variants, prompt); - echo_events.push(IpcEchoEvent { - prompt: prompt.clone(), - line: line.clone(), - source: (*echo_source).into(), - }); - } PendingSidebandKind::RequestBoundary | PendingSidebandKind::SessionEnd => {} }, } @@ -696,245 +556,6 @@ impl PendingOutputSnapshot { } } -fn maybe_trim_leading_echo_prefix( - echo_prefix: Option<&PendingEchoPrefix>, - events: &[PendingOutputEvent], - contents: &mut Vec, -) { - let Some(echo_prefix) = echo_prefix else { - return; - }; - let (matched_bytes, _) = leading_echo_match_progress(events, echo_prefix); - if matched_bytes == 0 { - return; - } - trim_matching_echo_prefix_from_contents(contents, &echo_prefix.text_prefix(matched_bytes)); -} - -fn append_readline_results_to_echo_prefix( - echo_prefix: &mut Option, - events: &[PendingOutputEvent], -) { - for event in events { - if let PendingOutputEvent::Sideband { - kind: - PendingSidebandKind::ReadlineResult { - prompt, - line, - echo_source, - }, - .. - } = event - { - if !is_trim_eligible_carryover_prompt(prompt) { - continue; - } - let prefix = echo_prefix.get_or_insert_with(PendingEchoPrefix::default); - let mut text = String::with_capacity(prompt.len().saturating_add(line.len())); - text.push_str(prompt); - text.push_str(line); - prefix.push(*echo_source, text); - } - } - if echo_prefix - .as_ref() - .is_some_and(PendingEchoPrefix::is_empty) - { - *echo_prefix = None; - } -} - -fn is_trim_eligible_carryover_prompt(prompt: &str) -> bool { - let core = prompt.trim_end_matches(|ch: char| ch.is_whitespace()); - if matches!(core, ">>>" | "...") { - return true; - } - if matches!(core, ">" | "+") - || (core.starts_with("Browse[") && (core.ends_with('>') || core.ends_with('+'))) - { - return true; - } - false -} - -fn snapshot_has_no_visible_text(events: &[PendingOutputEvent]) -> bool { - events.iter().all(|event| { - !matches!( - event, - PendingOutputEvent::TextFragment { bytes, .. } if !render_bytes(bytes).is_empty() - ) && !matches!(event, PendingOutputEvent::TextEvent { text, .. } if !text.is_empty()) - && !matches!(event, PendingOutputEvent::Image { .. }) - }) -} - -fn snapshot_crossed_request_boundary(events: &[PendingOutputEvent]) -> bool { - events.iter().any(|event| { - matches!( - event, - PendingOutputEvent::Sideband { - kind: PendingSidebandKind::RequestBoundary | PendingSidebandKind::SessionEnd, - .. - } - ) - }) -} - -fn leading_echo_match_progress( - events: &[PendingOutputEvent], - echo_prefix: &PendingEchoPrefix, -) -> (usize, bool) { - if echo_prefix.is_empty() { - return (0, false); - } - - let mut segment_idx = 0usize; - let mut segment_offset = 0usize; - let mut matched_bytes = 0usize; - let mut saw_visible_content = false; - - for event in events { - let PendingOutputEvent::TextFragment { - stream, - origin, - source, - bytes, - .. - } = event - else { - if matches!( - event, - PendingOutputEvent::Sideband { .. } - | PendingOutputEvent::Image { .. } - | PendingOutputEvent::TextEvent { .. } - ) { - continue; - } - return (matched_bytes, false); - }; - - if !matches!(stream, TextStream::Stdout) || !matches!(origin, ContentOrigin::Worker) { - return (matched_bytes, false); - } - - let rendered = render_bytes(bytes); - if rendered.is_empty() { - continue; - } - - saw_visible_content = true; - - let mut remaining_rendered = rendered.as_str(); - while !remaining_rendered.is_empty() { - let Some(segment) = echo_prefix.segments.get(segment_idx) else { - return (matched_bytes, false); - }; - if *source != segment.source { - return (matched_bytes, false); - } - - let remaining_segment = &segment.text[segment_offset..]; - if remaining_segment.is_empty() { - segment_idx = segment_idx.saturating_add(1); - segment_offset = 0; - continue; - } - - let before_len = remaining_rendered.len(); - let common = common_prefix_len(remaining_segment, remaining_rendered); - if common == 0 { - return (matched_bytes, false); - } - matched_bytes = matched_bytes.saturating_add(common); - segment_offset = segment_offset.saturating_add(common); - remaining_rendered = &remaining_rendered[common..]; - - let segment_complete = segment_offset == segment.text.len(); - if segment_complete { - segment_idx = segment_idx.saturating_add(1); - segment_offset = 0; - } - if common < before_len && !segment_complete { - return (matched_bytes, false); - } - } - } - - if !saw_visible_content { - return (matched_bytes, true); - } - - (matched_bytes, segment_idx < echo_prefix.segments.len()) -} - -fn trim_matching_echo_prefix_from_contents(contents: &mut Vec, echo_prefix: &str) { - if echo_prefix.is_empty() { - return; - } - - let mut remaining = echo_prefix; - let mut matched_bytes = 0usize; - for content in contents.iter() { - let WorkerContent::ContentText { - text, - stream, - origin, - } = content - else { - break; - }; - if !matches!(stream, TextStream::Stdout) || !matches!(origin, ContentOrigin::Worker) { - break; - } - let common = common_prefix_len(remaining, text); - matched_bytes = matched_bytes.saturating_add(common); - remaining = &remaining[common..]; - if common < text.len() || remaining.is_empty() { - break; - } - } - - if matched_bytes == 0 { - return; - } - - let mut remaining = &echo_prefix[..matched_bytes]; - let mut idx = 0usize; - while idx < contents.len() && !remaining.is_empty() { - let remove_current = match &mut contents[idx] { - WorkerContent::ContentText { text, .. } => { - if remaining.len() >= text.len() { - remaining = &remaining[text.len()..]; - text.clear(); - true - } else { - let updated = text[remaining.len()..].to_string(); - *text = updated; - remaining = ""; - false - } - } - _ => return, - }; - - if remove_current { - contents.remove(idx); - continue; - } - idx = idx.saturating_add(1); - } -} - -fn common_prefix_len(left: &str, right: &str) -> usize { - let mut matched = 0usize; - for (lch, rch) in left.chars().zip(right.chars()) { - if lch != rch { - break; - } - matched = matched.saturating_add(lch.len_utf8()); - } - matched -} - fn append_rendered_text( bytes: &mut Vec, text_spans: &mut Vec, @@ -1292,10 +913,8 @@ mod tests { fn sideband_events_preserve_order_with_text() { let tape = PendingOutputTape::new(); tape.append_stdout_ipc_bytes(b"> 1+\n"); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "1+\n".to_string(), - echo_source: PendingTextSource::Ipc, + tape.append_sideband(PendingSidebandKind::ReadlineStart { + prompt: "+ ".to_string(), }); tape.append_stdout_bytes(b"[1] 2\n"); @@ -1303,7 +922,7 @@ mod tests { assert!(matches!( snapshot.events[1], PendingOutputEvent::Sideband { - kind: PendingSidebandKind::ReadlineResult { .. }, + kind: PendingSidebandKind::ReadlineStart { .. }, .. } )); @@ -1502,162 +1121,6 @@ mod tests { ); } - #[test] - fn readline_result_prefix_carries_across_snapshot_drains_until_echo_arrives() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "1+\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_bytes(b">>> 1"); - let second = tape.drain_snapshot(); - assert!( - second.format_contents().contents.is_empty(), - "partial echoed prefix should stay hidden until the remainder arrives" - ); - - tape.append_stdout_bytes(b"+\n[1] 2\n"); - let third = tape.drain_snapshot(); - assert_eq!( - third.format_contents().contents, - vec![WorkerContent::stdout("[1] 2\n")] - ); - } - - #[test] - fn request_boundary_clears_pending_echo_prefix_after_sideband_only_snapshot() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "x <- 1\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - tape.append_sideband(PendingSidebandKind::RequestBoundary); - - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - let guard = tape - .inner - .lock() - .expect("pending output tape mutex poisoned"); - assert!( - guard.pending_echo_prefix.is_none(), - "request boundary should clear unmatched carried echo" - ); - } - - #[test] - fn text_event_keeps_pending_echo_prefix_across_request_boundary() { - let tape = PendingOutputTape::new(); - let status = "[repl] previous plot updated\n".to_string(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "plot(1:10)\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - tape.append_stdout_status_event(status.clone(), 1); - tape.append_sideband(PendingSidebandKind::RequestBoundary); - - let first = tape.drain_snapshot(); - assert_eq!( - first.format_contents().contents, - vec![WorkerContent::server_stdout(status)] - ); - - tape.append_stdout_bytes(b">>> plot(1:10)\n"); - let second = tape.drain_snapshot(); - assert!( - second.format_contents().contents.is_empty(), - "expected late echo to be trimmed after visible status event" - ); - } - - #[test] - fn image_event_keeps_pending_echo_prefix_across_request_boundary() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "plot(1:10)\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - tape.append_image( - "img-1".to_string(), - "image/png".to_string(), - "AA==".to_string(), - true, - 1, - ); - tape.append_sideband(PendingSidebandKind::RequestBoundary); - - let first = tape.drain_snapshot(); - assert_eq!( - first.format_contents().contents, - vec![WorkerContent::ContentImage { - data: "AA==".to_string(), - mime_type: "image/png".to_string(), - id: "img-1".to_string(), - is_new: true, - }] - ); - - tape.append_stdout_bytes(b">>> plot(1:10)\n"); - let second = tape.drain_snapshot(); - assert!( - second.format_contents().contents.is_empty(), - "expected late echo to be trimmed after image event" - ); - } - - #[test] - fn interleaved_output_drops_unmatched_echo_suffix_from_later_drains() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "x <- 1\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "y <- 2\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_bytes(b">>> x <- 1\nok\n"); - let second = tape.drain_snapshot(); - assert_eq!( - second.format_contents().contents, - vec![WorkerContent::stdout("ok\n")] - ); - - tape.append_stdout_bytes(b">>> y <- 2\n"); - let third = tape.drain_snapshot(); - assert_eq!( - third.format_contents().contents, - vec![WorkerContent::stdout(">>> y <- 2\n")] - ); - } - #[test] fn split_utf8_prefix_survives_image_event_without_escape_corruption() { let tape = PendingOutputTape::new(); @@ -1796,319 +1259,4 @@ mod tests { ] ); } - - #[test] - fn reply_format_anchors_image_before_later_echoed_input_and_stdout() { - let tape = PendingOutputTape::new(); - - tape.append_stdout_ipc_bytes(b"> plot(1:10)\n"); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "plot(1:10)\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - tape.append_stdout_ipc_bytes(b"> cat('done\\n')\n"); - tape.append_stdout_bytes(b"done\n"); - tape.append_image( - "img-1".to_string(), - "image/png".to_string(), - "AA==".to_string(), - true, - 1, - ); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "cat('done\\n')\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - - let snapshot = tape.drain_snapshot(); - assert_eq!( - snapshot.format_contents_for_reply().contents, - vec![ - WorkerContent::ContentImage { - data: "AA==".to_string(), - mime_type: "image/png".to_string(), - id: "img-1".to_string(), - is_new: true, - }, - WorkerContent::stdout("done\n"), - ] - ); - } - - #[test] - fn nonfinal_format_drops_leading_repl_echo_once_output_arrives() { - let tape = PendingOutputTape::new(); - - tape.append_stdout_ipc_bytes(b"> plot(1:10)\n"); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "plot(1:10)\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - tape.append_stdout_ipc_bytes(b"> cat('done\\n')\n"); - tape.append_stdout_bytes(b"done\n"); - tape.append_image( - "img-1".to_string(), - "image/png".to_string(), - "AA==".to_string(), - true, - 1, - ); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "cat('done\\n')\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - - let snapshot = tape.drain_snapshot(); - assert_eq!( - snapshot.format_contents().contents, - vec![ - WorkerContent::ContentImage { - data: "AA==".to_string(), - mime_type: "image/png".to_string(), - id: "img-1".to_string(), - is_new: true, - }, - WorkerContent::stdout("done\n"), - ] - ); - } - - #[test] - fn reply_format_trims_matched_readline_result_but_keeps_unmatched_prompt() { - let tape = PendingOutputTape::new(); - - tape.append_stdout_bytes(b"FIRST> alpha\nSECOND> "); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "FIRST> ".to_string(), - line: "alpha\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - tape.append_sideband(PendingSidebandKind::ReadlineStart { - prompt: "SECOND> ".to_string(), - }); - - let snapshot = tape.drain_snapshot(); - assert_eq!( - snapshot.format_contents_for_reply().contents, - vec![WorkerContent::stdout("SECOND> ")] - ); - } - - #[test] - fn reply_format_anchors_image_after_earlier_readline_drain() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "plot(1:10)\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_ipc_bytes(b"> cat('done\\n')\n"); - tape.append_stdout_bytes(b"done\n"); - tape.append_image( - "img-1".to_string(), - "image/png".to_string(), - "AA==".to_string(), - true, - 1, - ); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "cat('done\\n')\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - - let second = tape.drain_snapshot(); - assert_eq!( - second.format_contents_for_reply().contents, - vec![ - WorkerContent::ContentImage { - data: "AA==".to_string(), - mime_type: "image/png".to_string(), - id: "img-1".to_string(), - is_new: true, - }, - WorkerContent::stdout("done\n"), - ] - ); - } - - #[test] - fn r_prompt_carryover_does_not_trim_late_raw_stdout() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "1 + 1\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_bytes(b"> 1 + 1\n"); - let second = tape.drain_snapshot(); - assert_eq!( - second.format_contents().contents, - vec![WorkerContent::stdout("> 1 + 1\n")] - ); - } - - #[test] - fn python_r_shaped_prompt_carryover_trims_late_raw_echo() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "answer\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_bytes(b"> answer\n"); - let second = tape.drain_snapshot(); - assert!( - second.format_contents().contents.is_empty(), - "expected Python raw prompt echo to be trimmed" - ); - } - - #[test] - fn mixed_prompt_source_carryover_does_not_panic() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "first\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "second\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_bytes(b">>> first\n"); - let second = tape.drain_snapshot(); - assert!( - second.format_contents().contents.is_empty(), - "expected raw segment to be trimmed" - ); - - tape.append_stdout_ipc_bytes(b"> second\n"); - let third = tape.drain_snapshot(); - assert!( - third.format_contents().contents.is_empty(), - "expected IPC segment to be trimmed" - ); - } - - #[test] - fn r_prompt_carryover_trims_late_ipc_output_text() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "1 + 1\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_ipc_bytes(b"> 1 + 1\n"); - let second = tape.drain_snapshot(); - assert!( - second.format_contents().contents.is_empty(), - "expected late R-owned output_text echo to be trimmed" - ); - } - - #[test] - fn custom_prompt_carryover_does_not_trim_real_output() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "FIRST> ".to_string(), - line: "alpha\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_stdout_bytes(b"FIRST> alpha\n"); - let second = tape.drain_snapshot(); - assert_eq!( - second.format_contents().contents, - vec![WorkerContent::stdout("FIRST> alpha\n")] - ); - } - - #[test] - fn image_only_intermediate_snapshot_preserves_carried_echo_prefix() { - let tape = PendingOutputTape::new(); - - tape.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: ">>> ".to_string(), - line: "plot(1:10)\n".to_string(), - echo_source: PendingTextSource::Raw, - }); - let first = tape.drain_snapshot(); - assert!( - first.format_contents().contents.is_empty(), - "sideband-only snapshot should not render visible content" - ); - - tape.append_image( - "img-1".to_string(), - "image/png".to_string(), - "AA==".to_string(), - true, - 1, - ); - let second = tape.drain_snapshot(); - assert_eq!( - second.format_contents().contents, - vec![WorkerContent::ContentImage { - data: "AA==".to_string(), - mime_type: "image/png".to_string(), - id: "img-1".to_string(), - is_new: true, - }] - ); - - tape.append_stdout_bytes(b">>> plot(1:10)\ndone\n"); - let third = tape.drain_snapshot(); - assert_eq!( - third.format_contents().contents, - vec![WorkerContent::stdout("done\n")] - ); - } } diff --git a/src/python_session.rs b/src/python_session.rs index 3cac5247..4759b498 100644 --- a/src/python_session.rs +++ b/src/python_session.rs @@ -1,3 +1,5 @@ +#[cfg(windows)] +use std::collections::VecDeque; use std::ffi::{CStr, CString, c_char, c_int, c_long}; #[cfg(target_family = "unix")] use std::os::unix::io::RawFd; @@ -7,7 +9,7 @@ use std::ptr; #[cfg(target_family = "unix")] use std::sync::atomic::AtomicI32; use std::sync::atomic::{AtomicPtr, Ordering}; -use std::sync::{Arc, Condvar, Mutex, OnceLock, mpsc}; +use std::sync::{Arc, Condvar, Mutex, OnceLock}; use serde::Deserialize; @@ -19,7 +21,12 @@ use windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE; #[cfg(windows)] use windows_sys::Win32::Storage::FileSystem::ReadFile; #[cfg(windows)] -use windows_sys::Win32::System::Console::{GetStdHandle, STD_INPUT_HANDLE}; +use windows_sys::Win32::System::Console::{ + ENABLE_ECHO_INPUT, ENABLE_LINE_INPUT, ENABLE_PROCESSED_INPUT, FlushConsoleInputBuffer, + GetConsoleMode, GetNumberOfConsoleInputEvents, GetStdHandle, INPUT_RECORD, KEY_EVENT, + ReadConsoleInputW, ReadConsoleW, STD_INPUT_HANDLE, SetConsoleCP, SetConsoleMode, + SetConsoleOutputCP, +}; #[cfg(windows)] use windows_sys::Win32::System::Pipes::PeekNamedPipe; @@ -28,6 +35,8 @@ const MCP_REPL_PYTHON: &str = include_str!("../python/embedded.py"); const PYTHON_EOF: c_int = 11; const PYTHON_PROGRAM: &str = "python3"; const PYTHON_PROGRAM_FALLBACK: &str = "python"; +#[cfg(windows)] +const WINDOWS_CONSOLE_LINE_READ_BUFFER_UNITS: usize = 8192; const PYTHON_CONFIG_SNIPPET: &str = r#" import json import sys @@ -80,51 +89,23 @@ struct PythonRuntimeProbe { pythonframeworkinstalldir: String, } -#[derive(Debug)] -pub struct RequestCompleted; - -pub struct PythonSession { - init: Arc, -} +pub struct PythonSession; impl PythonSession { - pub fn global() -> Result<&'static PythonSession, String> { - SESSION - .get() - .ok_or_else(|| "Python session not initialized".to_string()) - } - pub fn start_on_current_thread() -> Result<(), String> { let init = Arc::new(SessionInit::new()); - let session = PythonSession { init: init.clone() }; - if SESSION.set(session).is_err() { + if SESSION.set(PythonSession).is_err() { return Err("Python session already initialized".to_string()); } run_session_on_current_thread(init) } - - pub fn wait_until_ready(&self) -> Result<(), String> { - self.init.wait_ready() - } - - pub fn begin_request( - &self, - byte_len: usize, - line_count: usize, - fallback_prompt: Option, - ) -> Result, String> { - self.wait_until_ready()?; - let (reply_tx, reply_rx) = mpsc::channel(); - begin_tracked_request(byte_len, line_count, fallback_prompt, reply_tx)?; - Ok(reply_rx) - } } #[derive(Debug)] enum InitState { Pending, Ready, - Failed(String), + Failed, } #[derive(Debug)] @@ -147,24 +128,11 @@ impl SessionInit { self.cvar.notify_all(); } - fn mark_failed(&self, message: String) { + fn mark_failed(&self, _message: String) { let mut guard = self.state.lock().unwrap(); - *guard = InitState::Failed(message); + *guard = InitState::Failed; self.cvar.notify_all(); } - - fn wait_ready(&self) -> Result<(), String> { - let mut guard = self.state.lock().unwrap(); - loop { - match &*guard { - InitState::Pending => { - guard = self.cvar.wait(guard).unwrap(); - } - InitState::Ready => return Ok(()), - InitState::Failed(message) => return Err(message.clone()), - } - } - } } struct PythonRuntime { @@ -199,41 +167,63 @@ pub(crate) fn interrupt_request_generation(request_generation: u64) { } fn interrupt_for_request_generation(request_generation: Option) { - if !interrupt_generation_is_current(request_generation) { + #[cfg(target_family = "unix")] + if !interrupt_cleanup_belongs_to_current_request(request_generation) { return; } + #[cfg(not(target_family = "unix"))] + let _ = request_generation; + discard_pending_stdin(); #[cfg(target_family = "unix")] flush_terminal_input(); - #[cfg(not(target_family = "unix"))] + #[cfg(windows)] + if windows_stdin_is_console() { + flush_terminal_input(); + } else { + finish_active_request_at_next_read(); + } + #[cfg(not(any(target_family = "unix", windows)))] finish_active_request_at_next_read(); mark_interrupt_requested(); request_platform_interrupt(); } #[cfg(target_family = "unix")] -fn flush_terminal_input() { - let _ = unsafe { libc::tcflush(libc::STDIN_FILENO, libc::TCIFLUSH) }; -} - -fn interrupt_generation_is_current(request_generation: Option) -> bool { - let Some(request_generation) = request_generation else { - return true; - }; +fn interrupt_cleanup_belongs_to_current_request(request_generation: Option) -> bool { let Some(state) = SESSION_STATE.get() else { return false; }; let guard = state.inner.lock().unwrap(); - // Unix Python receives SIGINT out-of-band from the server and an IPC - // interrupt message on a separate thread. SIGINT can bring Python back to a - // prompt before the IPC thread handles that message; if the next MCP - // request has already started, draining fd 0 here would discard the new - // request's stdin. Generated Python interrupts are therefore allowed to - // clean up only while their original request generation is still current. - // The tradeoff is that a very late interrupt stops cleaning old tail bytes - // once a later request is accepted; preserving the new request boundary is - // the stricter REPL contract. - guard.request_generation == request_generation + interrupt_cleanup_belongs_to_current_request_locked(&guard, request_generation) +} + +#[cfg(any(test, target_family = "unix"))] +fn interrupt_cleanup_belongs_to_current_request_locked( + guard: &SessionStateInner, + request_generation: Option, +) -> bool { + if !guard.request_active || guard.request_completed_at_stdin_wait { + return false; + } + request_generation + .is_none_or(|request_generation| guard.request_generation == request_generation) +} + +#[cfg(target_family = "unix")] +fn flush_terminal_input() { + let _ = unsafe { libc::tcflush(libc::STDIN_FILENO, libc::TCIFLUSH) }; +} + +#[cfg(windows)] +fn flush_terminal_input() { + clear_windows_console_drop_next_lf(); + clear_windows_console_stdin_buffer(); + let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) }; + if handle.is_null() || handle == INVALID_HANDLE_VALUE { + return; + } + let _ = unsafe { FlushConsoleInputBuffer(handle) }; } fn mark_interrupt_requested() { @@ -263,127 +253,7 @@ fn take_interrupt_requested() -> bool { requested } -pub(crate) fn mark_stdin_write_complete() { - #[cfg(target_family = "unix")] - let protocol_input_exhausted = protocol_request_input_exhausted(); - - let Some(state) = SESSION_STATE.get() else { - return; - }; - let mut completed = None; - let mut prompt = None; - { - let mut guard = state.inner.lock().unwrap(); - let current_prompt_from_state = guard.current_prompt.clone(); - let current_readline_state = guard.current_readline_state; - let primary_prompt = guard.python_primary_prompt.clone(); - let continuation_prompt = guard.python_continuation_prompt.clone(); - let waiting_for_input = guard.waiting_for_input; - #[cfg(target_family = "unix")] - if protocol_input_exhausted && guard.active_request.is_none() && waiting_for_input { - // Unix protocol-mode Python can reach the next prompt before the IPC - // thread observes StdinWriteComplete. In that case the prompt hook - // deliberately left the plot gate open because stdin was not yet - // accounted; close it here once the explicit write-complete signal - // proves the already-emitted prompt is the request boundary. - guard.request_active = false; - } - if let Some(active) = guard.active_request.as_mut() { - active.stdin_write_complete = true; - let continuation_write_complete = - windows_continuation_prompt_write_should_complete(active, current_readline_state); - let should_complete = if active.repl_turn_finished { - request_repl_turn_should_complete(active) - } else { - request_prompt_wait_should_complete(active, current_readline_state) - || continuation_write_complete - }; - if (waiting_for_input || continuation_write_complete) && should_complete { - let fallback_prompt = if active.repl_turn_finished { - None - } else { - active - .fallback_prompt - .as_deref() - .or_else(|| active.started_after_continuation_prompt.then_some("")) - }; - prompt = Some(repl_prompt_for( - current_prompt_from_state.clone(), - fallback_prompt, - current_readline_state, - &primary_prompt, - &continuation_prompt, - )); - completed = guard.active_request.take(); - } - } - } - - if let Some(active) = completed { - emit_plots(); - #[cfg(not(target_family = "unix"))] - mark_stdin_wait_prompt_completed_request(); - // Python object flushes run from handle_input_hook on the Python thread. - let prompt = prompt.as_deref().unwrap_or(">>> "); - remember_emitted_prompt(prompt); - ipc::emit_readline_start(prompt); - complete_active_request(state, Some(active), false); - } -} - -pub(crate) fn mark_request_started() { - mark_request_started_with_generation(None); -} - -pub(crate) fn mark_request_started_for_generation(request_generation: u64) { - mark_request_started_with_generation(Some(request_generation)); -} - -fn mark_request_started_with_generation(request_generation: Option) { - let Some(state) = SESSION_STATE.get() else { - return; - }; - let should_record_background_plots = { - let guard = state.inner.lock().unwrap(); - !guard.request_active || guard.request_completed_at_stdin_wait - }; - if should_record_background_plots { - // A stdin-wait prompt closes the MCP request while Python threads can - // still mutate matplotlib state. Snapshot those inactive plots before - // reopening the gate so a later stdin answer does not flush stale - // background figures into its reply. A later explicit plot/show in the - // new request still forces a fresh image. - record_background_plots(); - } - let mut guard = state.inner.lock().unwrap(); - if let Some(request_generation) = request_generation { - guard.request_generation = request_generation; - } else { - guard.request_generation = guard.request_generation.wrapping_add(1); - } - guard.interrupt_requested = false; - guard.request_completed_at_stdin_wait = false; - guard.request_active = true; - guard.plot_reset_pending = true; -} - -#[cfg(windows)] -fn windows_continuation_prompt_write_should_complete( - active: &ActiveRequest, - _current_readline_state: Option, -) -> bool { - active.started_after_continuation_prompt && active.line_count == 1 -} - -#[cfg(not(windows))] -fn windows_continuation_prompt_write_should_complete( - _active: &ActiveRequest, - _current_readline_state: Option, -) -> bool { - false -} - -#[cfg_attr(target_family = "unix", allow(dead_code))] +#[cfg_attr(any(target_family = "unix", windows), allow(dead_code))] fn finish_active_request_at_next_read() { let Some(state) = SESSION_STATE.get() else { return; @@ -399,15 +269,7 @@ fn finish_active_request_at_next_read() { #[cfg(target_family = "unix")] fn discard_pending_stdin() { - let mut discarded = Vec::new(); - discarded.extend(PYTHON_DIRECT_STDIN_SIDEBAND_INPUT.lock().unwrap().drain(..)); - discarded.extend(drain_process_stdin_pipe()); - if discarded.is_empty() { - return; - } - let text = - String::from_utf8(discarded).expect("discarded Python stdin must be valid UTF-8 text"); - ipc::emit_readline_discard(&text); + emit_readline_discard_bytes(&drain_process_stdin_pipe()); } #[cfg(target_family = "unix")] @@ -480,7 +342,7 @@ impl Drop for NonBlockingFd { } } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] fn request_runtime_stdin_line(prompt: &str) -> bool { ipc::emit_readline_start(prompt); true @@ -508,11 +370,7 @@ fn runtime_stdin_pending_byte_count() -> Option { #[cfg(target_family = "unix")] fn protocol_request_input_exhausted() -> bool { - PYTHON_DIRECT_STDIN_SIDEBAND_INPUT - .lock() - .unwrap() - .is_empty() - && stdin_pending_byte_count() == Some(0) + stdin_pending_byte_count() == Some(0) } #[cfg(windows)] @@ -523,6 +381,7 @@ fn discard_pending_stdin() { libc::fflush(stdin); } } + emit_readline_discard_bytes(&drain_console_input_bytes()); drain_stdin_pipe(); } @@ -586,6 +445,7 @@ fn run_session_on_current_thread(init: Arc) -> Result<(), String> { return Err(err); } }; + crate::diagnostics::startup_log("python-session: runtime config resolved"); let api = match PythonApi::initialize(&runtime_config.libpython) { Ok(api) => api, Err(err) => { @@ -593,6 +453,7 @@ fn run_session_on_current_thread(init: Arc) -> Result<(), String> { return Err(err); } }; + crate::diagnostics::startup_log("python-session: api initialized"); let thread_state = match initialize_python(api, &runtime_config.executable) { Ok(thread_state) => thread_state, Err(err) => { @@ -600,6 +461,7 @@ fn run_session_on_current_thread(init: Arc) -> Result<(), String> { return Err(err); } }; + crate::diagnostics::startup_log("python-session: python initialized"); if thread_state.is_null() { let err = "failed to release initialized Python thread state".to_string(); init.mark_failed(err.clone()); @@ -612,6 +474,7 @@ fn run_session_on_current_thread(init: Arc) -> Result<(), String> { return Err(err); } }; + crate::diagnostics::startup_log("python-session: stdio opened"); if let Err(err) = configure_python(api) { let _gil = GilGuard::acquire(); @@ -619,9 +482,11 @@ fn run_session_on_current_thread(init: Arc) -> Result<(), String> { init.mark_failed(err.clone()); return Err(err); } + crate::diagnostics::startup_log("python-session: python configured"); init.mark_ready(); ipc::emit_worker_ready("python", plot_capable()); + crate::diagnostics::startup_log("python-session: worker_ready emitted"); let result = run_repl(&runtime); let finalize_result = finalize_python(api, thread_state); @@ -1074,6 +939,7 @@ fn initialize_python( } api.set_program_name(executable)?; api.set_interactive_flags()?; + configure_windows_pty_console(); (api.py_initialize_ex)(1); api.install_readline_function(mcp_repl_readline)?; let thread_state = (api.py_eval_save_thread)(); @@ -1082,6 +948,120 @@ fn initialize_python( } } +#[cfg(windows)] +fn take_windows_console_stdin_bytes(max_len: usize) -> Vec { + let mut guard = WINDOWS_CONSOLE_STDIN_BYTES.lock().unwrap(); + let take_len = max_len.min(guard.len()); + (0..take_len).filter_map(|_| guard.pop_front()).collect() +} + +#[cfg(windows)] +fn take_windows_console_stdin_line_prefix() -> Vec { + let mut guard = WINDOWS_CONSOLE_STDIN_BYTES.lock().unwrap(); + let mut bytes = Vec::new(); + while let Some(byte) = guard.pop_front() { + bytes.push(byte); + if byte == b'\n' { + break; + } + } + bytes +} + +#[cfg(windows)] +fn push_windows_console_stdin_bytes(bytes: &[u8]) { + WINDOWS_CONSOLE_STDIN_BYTES + .lock() + .unwrap() + .extend(bytes.iter().copied()); +} + +#[cfg(windows)] +fn drain_windows_console_stdin_buffer() -> Vec { + WINDOWS_CONSOLE_STDIN_BYTES + .lock() + .unwrap() + .drain(..) + .collect() +} + +#[cfg(windows)] +fn clear_windows_console_stdin_buffer() { + WINDOWS_CONSOLE_STDIN_BYTES.lock().unwrap().clear(); +} + +#[cfg(windows)] +fn drain_console_input_bytes() -> Vec { + let mut bytes = drain_windows_console_stdin_buffer(); + bytes.extend(drain_console_input_text().into_bytes()); + bytes +} + +#[cfg(windows)] +fn drain_console_input_text() -> String { + let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) }; + if handle.is_null() || handle == INVALID_HANDLE_VALUE { + return String::new(); + } + + let mut text = String::new(); + loop { + let mut available = 0u32; + if unsafe { GetNumberOfConsoleInputEvents(handle, &mut available) } == 0 || available == 0 { + break; + } + let to_read = available.min(128); + let mut records = vec![INPUT_RECORD::default(); to_read as usize]; + let mut read = 0u32; + if unsafe { ReadConsoleInputW(handle, records.as_mut_ptr(), to_read, &mut read) } == 0 + || read == 0 + { + break; + } + for record in records.into_iter().take(read as usize) { + if record.EventType != KEY_EVENT as u16 { + continue; + } + let key = unsafe { record.Event.KeyEvent }; + if key.bKeyDown == 0 { + continue; + } + let raw = unsafe { key.uChar.UnicodeChar }; + if raw == 0 { + continue; + } + let ch = char::from_u32(raw as u32).unwrap_or(char::REPLACEMENT_CHARACTER); + for _ in 0..key.wRepeatCount.max(1) { + if ch == '\r' { + text.push('\n'); + } else { + text.push(ch); + } + } + } + } + text +} + +#[cfg(windows)] +fn configure_windows_pty_console() { + let _ = unsafe { SetConsoleCP(65001) }; + let _ = unsafe { SetConsoleOutputCP(65001) }; + let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) }; + if handle.is_null() || handle == INVALID_HANDLE_VALUE { + return; + } + let mut mode = 0; + if unsafe { GetConsoleMode(handle, &mut mode) } == 0 { + return; + } + let mode = (mode | ENABLE_LINE_INPUT | ENABLE_PROCESSED_INPUT) & !ENABLE_ECHO_INPUT; + let _ = unsafe { SetConsoleMode(handle, mode) }; +} + +#[cfg(not(windows))] +fn configure_windows_pty_console() {} + fn configure_python(api: &'static PythonApi) -> Result<(), String> { let _gil = GilGuard::acquire(); let builtins = api.import_module("builtins")?; @@ -1149,61 +1129,36 @@ fn finalize_python( } } -fn begin_tracked_request( - byte_len: usize, - line_count: usize, - fallback_prompt: Option, - reply: mpsc::Sender, -) -> Result<(), String> { - let state = session_state(); - if line_count == 0 { - let _ = reply.send(RequestCompleted); - return Ok(()); - } - - let mut guard = state.inner.lock().unwrap(); - while guard.active_request.is_some() && !guard.shutdown { - guard = state.cvar.wait(guard).unwrap(); - } - if guard.shutdown { - return Err("Python session is shutting down".to_string()); - } - - let skip_next_hook = !guard.waiting_for_input; - let started_after_continuation_prompt = guard.last_prompt_was_continuation; - guard.waiting_for_input = false; - guard.request_generation = guard.request_generation.wrapping_add(1); - guard.request_completed_at_stdin_wait = false; - guard.active_request = Some(ActiveRequest { - reply, - byte_len, - line_count, - fallback_prompt, - consumed_lines: 0, - skip_next_hook, - stdin_write_complete: false, - repl_turn_finished: false, - started_after_continuation_prompt, - }); - #[cfg(not(target_family = "unix"))] - { - guard.request_active = true; - } - guard.plot_reset_pending = true; - state.cvar.notify_all(); - Ok(()) -} - -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] fn mark_request_input_delivered() { let Some(state) = SESSION_STATE.get() else { return; }; + if request_input_should_record_background_plots(state) { + record_background_plots(); + } let mut guard = state.inner.lock().unwrap(); - if !guard.request_active { + mark_request_input_delivered_locked(&mut guard); +} + +#[cfg(any(target_family = "unix", windows))] +fn request_input_should_record_background_plots(state: &Arc) -> bool { + let guard = state.inner.lock().unwrap(); + request_input_should_record_background_plots_locked(&guard) +} + +fn request_input_should_record_background_plots_locked(guard: &SessionStateInner) -> bool { + !guard.request_active || guard.request_completed_at_stdin_wait +} + +#[cfg(any(target_family = "unix", windows))] +fn mark_request_input_delivered_locked(guard: &mut SessionStateInner) { + if !guard.request_active || guard.request_completed_at_stdin_wait { + guard.request_generation = guard.request_generation.wrapping_add(1); guard.plot_reset_pending = true; } guard.request_active = true; + guard.request_completed_at_stdin_wait = false; guard.waiting_for_input = false; } @@ -1388,6 +1343,7 @@ fn note_input_hook_consumed_line(active: &mut ActiveRequest) { } } +#[cfg_attr(target_family = "unix", allow(dead_code))] fn request_prompt_wait_should_complete( active: &ActiveRequest, current_readline_state: Option, @@ -1401,6 +1357,9 @@ fn request_prompt_wait_should_complete( } #[cfg(windows)] { + if !windows_stdin_is_console() { + return active.stdin_input_complete && active.consumed_lines >= active.line_count; + } prompt_can_complete_before_repl_turn(active, current_readline_state) && active.byte_len > 0 && stdin_pending_byte_count() == Some(0) @@ -1412,6 +1371,7 @@ fn request_prompt_wait_should_complete( } #[cfg(target_family = "unix")] +#[cfg_attr(target_family = "unix", allow(dead_code))] fn prompt_wait_can_complete( active: &ActiveRequest, current_readline_state: Option, @@ -1425,6 +1385,7 @@ fn prompt_wait_can_complete( } #[cfg(target_family = "unix")] +#[cfg_attr(target_family = "unix", allow(dead_code))] fn single_line_client_input_prompt( active: &ActiveRequest, current_readline_state: Option, @@ -1443,6 +1404,9 @@ fn request_repl_turn_should_complete(active: &ActiveRequest) -> bool { } #[cfg(windows)] { + if !windows_stdin_is_console() { + return active.stdin_input_complete && active.consumed_lines >= active.line_count; + } active.line_count == 1 || (active.byte_len > 0 && stdin_pending_byte_count() == Some(0)) } #[cfg(not(any(target_family = "unix", windows)))] @@ -1464,7 +1428,7 @@ fn prompt_can_complete_before_repl_turn( #[cfg(target_family = "unix")] fn request_input_drained(active: &ActiveRequest) -> bool { - if !active.stdin_write_complete || active.byte_len == 0 { + if !active.stdin_input_complete || active.byte_len == 0 { return false; } stdin_pending_byte_count() == Some(0) @@ -1496,6 +1460,15 @@ fn finish_repl_turn_request() { } if let Some(active) = guard.active_request.as_mut() { active.repl_turn_finished = true; + #[cfg(windows)] + if !windows_stdin_is_console() { + active.consumed_lines = active.consumed_lines.saturating_add(1); + } + #[cfg(windows)] + if windows_stdin_is_console() && active.line_count == 1 { + active.consumed_lines = active.consumed_lines.max(1); + } + #[cfg(not(windows))] if active.line_count == 1 { active.consumed_lines = active.consumed_lines.max(1); } @@ -1539,7 +1512,9 @@ fn stdin_pending_byte_count() -> Option { if handle.is_null() || handle == INVALID_HANDLE_VALUE { return None; } - + if windows_stdin_is_console() { + return None; + } let mut available = 0u32; let ok = unsafe { PeekNamedPipe( @@ -1573,40 +1548,44 @@ unsafe extern "C" fn mcp_repl_readline( }; #[cfg(target_family = "unix")] if ipc::worker_ipc_disabled_for_process() { - return allocate_readline_result(&[]); + return allocate_cpython_readline_buffer(&[]); } set_current_repl_readline_prompt(&prompt_text); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] let prompt_has_buffered_answer = stdin_pending_byte_count().is_some_and(|count| count > 0); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] let prompt_matches_repl = prompt_matches_python_repl_prompt(&prompt_text); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] flush_original_stdio(); #[cfg(target_family = "unix")] + if !prompt_has_buffered_answer { + mark_stdin_wait_prompt_completed_request(); + } + #[cfg(any(target_family = "unix", windows))] request_cpython_readline_stdin_line(&prompt_text); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] if prompt_has_buffered_answer && !prompt_text.is_empty() && !prompt_matches_repl { emit_output_text(TextStream::Stdout, prompt_text.as_bytes()); } - #[cfg(not(target_family = "unix"))] + #[cfg(not(any(target_family = "unix", windows)))] handle_input_hook(); let read = read_stdio_line_bytes(stdin); if read.interrupted { - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] flush_terminal_input(); } - note_cpython_readline_bytes_read(&read.bytes); + note_cpython_readline_bytes_read(&prompt_text, &read.bytes); clear_current_readline_prompt(); if read.interrupted || take_interrupt_requested() { PythonApi::global().set_interrupt(); return ptr::null_mut(); } - allocate_readline_result(&read.bytes) + allocate_cpython_readline_buffer(&read.bytes) } -fn allocate_readline_result(bytes: &[u8]) -> *mut c_char { +fn allocate_cpython_readline_buffer(bytes: &[u8]) -> *mut c_char { let api = PythonApi::global(); let result = unsafe { (api.py_mem_raw_malloc)(bytes.len().saturating_add(1)) }.cast::(); if result.is_null() { @@ -1619,12 +1598,12 @@ fn allocate_readline_result(bytes: &[u8]) -> *mut c_char { result } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] fn request_cpython_readline_stdin_line(prompt: &str) { ipc::emit_readline_start(prompt); } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] fn prompt_matches_python_repl_prompt(prompt: &str) -> bool { let Some(state) = SESSION_STATE.get() else { return false; @@ -1634,7 +1613,7 @@ fn prompt_matches_python_repl_prompt(prompt: &str) -> bool { } #[cfg(target_family = "unix")] -fn note_cpython_readline_bytes_read(bytes: &[u8]) { +fn note_cpython_readline_bytes_read(_prompt: &str, bytes: &[u8]) { if bytes.is_empty() { return; } @@ -1643,8 +1622,22 @@ fn note_cpython_readline_bytes_read(bytes: &[u8]) { note_active_stdin_line_read(bytes); } -#[cfg(not(target_family = "unix"))] -fn note_cpython_readline_bytes_read(bytes: &[u8]) { +#[cfg(windows)] +fn note_cpython_readline_bytes_read(prompt: &str, bytes: &[u8]) { + if windows_stdin_is_console() { + if bytes.is_empty() { + return; + } + emit_readline_input_bytes(bytes); + mark_request_input_delivered(); + note_active_stdin_line_read(bytes); + } else { + note_windows_prompted_stdin_line_read(prompt, bytes); + } +} + +#[cfg(not(any(target_family = "unix", windows)))] +fn note_cpython_readline_bytes_read(_prompt: &str, bytes: &[u8]) { note_stdin_line_read(bytes); } @@ -1654,6 +1647,11 @@ struct StdioLineRead { } fn read_stdio_line_bytes(stdin: *mut libc::FILE) -> StdioLineRead { + #[cfg(windows)] + if let Some(read) = read_windows_console_line_bytes() { + return read; + } + let mut bytes = Vec::new(); loop { let ch = unsafe { libc::fgetc(stdin) }; @@ -1664,6 +1662,20 @@ fn read_stdio_line_bytes(stdin: *mut libc::FILE) -> StdioLineRead { } return StdioLineRead { bytes, interrupted }; } + #[cfg(windows)] + if ch == b'\r' as i32 { + let next = unsafe { libc::fgetc(stdin) }; + if next != b'\n' as i32 && next != libc::EOF { + unsafe { + libc::ungetc(next, stdin); + } + } + bytes.push(b'\n'); + return StdioLineRead { + bytes, + interrupted: false, + }; + } bytes.push(ch as u8); if ch == b'\n' as i32 { return StdioLineRead { @@ -1816,23 +1828,27 @@ fn read_c_stdin_line(prompt: &str) -> CStdinLine { prompt_for_sideband.to_str().unwrap_or(""), PythonReadlineState::ClientInput, ); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] flush_original_stdio(); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] let prompt_has_buffered_answer = stdin_pending_byte_count().is_some_and(|count| count > 0); - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] if !prompt_has_buffered_answer { emit_plots(); mark_stdin_wait_prompt_completed_request(); } - #[cfg(target_family = "unix")] - let prompt_delivered_immediately = - request_runtime_stdin_line(prompt_for_sideband.to_str().unwrap_or("")); - #[cfg(target_family = "unix")] + let sideband_prompt = prompt_for_sideband.to_str().unwrap_or(""); + #[cfg(any(target_family = "unix", windows))] + let prompt_delivered_immediately = if cfg!(windows) && prompt_has_buffered_answer { + false + } else { + request_runtime_stdin_line(sideband_prompt) + }; + #[cfg(any(target_family = "unix", windows))] if !prompt.is_empty() && (prompt_delivered_immediately || prompt_has_buffered_answer) { emit_output_text(TextStream::Stdout, prompt.as_bytes()); } - #[cfg(not(target_family = "unix"))] + #[cfg(not(any(target_family = "unix", windows)))] { flush_original_stdio(); handle_input_hook(); @@ -1840,9 +1856,12 @@ fn read_c_stdin_line(prompt: &str) -> CStdinLine { } let read = read_stdio_line_bytes_allowing_python_threads(stdin); if read.interrupted { - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", windows))] flush_terminal_input(); } + #[cfg(windows)] + note_windows_prompted_stdin_line_read(sideband_prompt, &read.bytes); + #[cfg(not(windows))] note_stdin_line_read(&read.bytes); clear_current_readline_prompt(); if read.interrupted || take_interrupt_requested() { @@ -1883,7 +1902,214 @@ fn read_raw_stdin_bytes(size: usize) -> Vec { bytes } -#[cfg(not(target_family = "unix"))] +#[cfg(windows)] +fn read_raw_stdin_bytes(size: usize) -> Vec { + if size == 0 { + return Vec::new(); + } + let _allow_threads = PythonThreadsAllowed::new(); + if windows_stdin_is_console() { + let bytes = read_windows_console_stdin_bytes(size); + note_windows_raw_stdin_bytes_read(&bytes); + return bytes; + } + let bytes = read_windows_stdin_bytes(size); + note_windows_raw_stdin_bytes_read(&bytes); + bytes +} + +#[cfg(windows)] +fn read_windows_console_stdin_bytes(size: usize) -> Vec { + let mut bytes = take_windows_console_stdin_bytes(size); + while bytes.len() < size { + let Some(read) = read_windows_console_line_bytes_uncached() else { + break; + }; + if read.bytes.is_empty() { + break; + } + let interrupted = read.interrupted; + push_windows_console_stdin_bytes(&read.bytes); + bytes.extend(take_windows_console_stdin_bytes( + size.saturating_sub(bytes.len()), + )); + if interrupted { + break; + } + } + bytes +} + +#[cfg(windows)] +fn read_windows_stdin_bytes(size: usize) -> Vec { + if size == 0 { + return Vec::new(); + } + let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) }; + if handle.is_null() || handle == INVALID_HANDLE_VALUE { + return Vec::new(); + } + let mut bytes = vec![0u8; size.min(u32::MAX as usize)]; + loop { + let mut read = 0u32; + let ok = unsafe { + ReadFile( + handle, + bytes.as_mut_ptr().cast(), + bytes.len() as u32, + &mut read, + ptr::null_mut(), + ) + }; + if ok != 0 { + bytes.truncate(read as usize); + return bytes; + } + let err = std::io::Error::last_os_error(); + if err.kind() == std::io::ErrorKind::Interrupted { + continue; + } + return Vec::new(); + } +} + +#[cfg(windows)] +fn read_windows_console_line_bytes() -> Option { + if !windows_stdin_is_console() { + return None; + } + let mut bytes = take_windows_console_stdin_line_prefix(); + if bytes.last() == Some(&b'\n') { + return Some(StdioLineRead { + bytes, + interrupted: false, + }); + } + let mut read = read_windows_console_line_bytes_uncached()?; + if !bytes.is_empty() { + bytes.append(&mut read.bytes); + read.bytes = bytes; + } + Some(read) +} + +#[cfg(windows)] +fn read_windows_console_line_bytes_uncached() -> Option { + let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) }; + if handle.is_null() || handle == INVALID_HANDLE_VALUE { + return None; + } + + let mut units = Vec::new(); + let mut buffer = vec![0u16; WINDOWS_CONSOLE_LINE_READ_BUFFER_UNITS]; + loop { + let mut read = 0u32; + let ok = unsafe { + ReadConsoleW( + handle, + buffer.as_mut_ptr().cast(), + buffer.len() as u32, + &mut read, + ptr::null_mut(), + ) + }; + if ok == 0 { + let interrupted = + std::io::Error::last_os_error().kind() == std::io::ErrorKind::Interrupted; + return Some(StdioLineRead { + bytes: String::from_utf16_lossy(&units).into_bytes(), + interrupted, + }); + } + if read == 0 { + return Some(StdioLineRead { + bytes: String::from_utf16_lossy(&units).into_bytes(), + interrupted: false, + }); + } + let read_len = read as usize; + for (idx, unit) in buffer.iter().take(read_len).copied().enumerate() { + if take_windows_console_drop_next_lf() && unit == 0x0a { + continue; + } + match unit { + 0x0d => { + if buffer.get(idx + 1).copied() != Some(0x0a) { + set_windows_console_drop_next_lf(); + } + let mut bytes = String::from_utf16_lossy(&units).into_bytes(); + bytes.push(b'\n'); + return Some(StdioLineRead { + bytes, + interrupted: false, + }); + } + 0x0a => { + let mut bytes = String::from_utf16_lossy(&units).into_bytes(); + bytes.push(b'\n'); + return Some(StdioLineRead { + bytes, + interrupted: false, + }); + } + _ => units.push(unit), + } + } + } +} + +#[cfg(windows)] +fn take_windows_console_drop_next_lf() -> bool { + let mut guard = WINDOWS_CONSOLE_DROP_NEXT_LF.lock().unwrap(); + let drop = *guard; + *guard = false; + drop +} + +#[cfg(windows)] +fn set_windows_console_drop_next_lf() { + *WINDOWS_CONSOLE_DROP_NEXT_LF.lock().unwrap() = true; +} + +#[cfg(windows)] +fn clear_windows_console_drop_next_lf() { + *WINDOWS_CONSOLE_DROP_NEXT_LF.lock().unwrap() = false; +} + +#[cfg(windows)] +fn windows_stdin_is_console() -> bool { + let handle = unsafe { GetStdHandle(STD_INPUT_HANDLE) }; + if handle.is_null() || handle == INVALID_HANDLE_VALUE { + return false; + } + let mut mode = 0; + unsafe { GetConsoleMode(handle, &mut mode) != 0 } +} + +#[cfg(windows)] +fn note_windows_prompted_stdin_line_read(_prompt: &str, bytes: &[u8]) { + if windows_stdin_is_console() { + note_stdin_line_read(bytes); + return; + } + if !bytes.is_empty() { + emit_readline_input_bytes(bytes); + mark_request_input_delivered(); + note_active_stdin_line_read(bytes); + } +} + +#[cfg(windows)] +fn note_windows_raw_stdin_bytes_read(bytes: &[u8]) { + if bytes.is_empty() { + return; + } + emit_readline_input_bytes(bytes); + mark_request_input_delivered(); + note_active_stdin_line_read(bytes); +} + +#[cfg(not(any(target_family = "unix", windows)))] fn read_raw_stdin_bytes(_size: usize) -> Vec { Vec::new() } @@ -1911,7 +2137,7 @@ fn read_fd_bytes(fd: libc::c_int, size: usize) -> Vec { } } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] fn note_stdin_bytes_read(bytes: &[u8]) { if bytes.is_empty() { return; @@ -1921,9 +2147,10 @@ fn note_stdin_bytes_read(bytes: &[u8]) { note_active_stdin_line_read(bytes); } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] fn note_active_stdin_line_read(bytes: &[u8]) { - if bytes.is_empty() { + let consumed_lines = consumed_stdin_line_count(bytes); + if consumed_lines == 0 { return; } let Some(state) = SESSION_STATE.get() else { @@ -1931,46 +2158,37 @@ fn note_active_stdin_line_read(bytes: &[u8]) { }; let mut guard = state.inner.lock().unwrap(); if let Some(active) = guard.active_request.as_mut() { - active.consumed_lines = active.consumed_lines.saturating_add(1); + active.consumed_lines = active.consumed_lines.saturating_add(consumed_lines); } } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] +fn consumed_stdin_line_count(bytes: &[u8]) -> usize { + bytes.iter().filter(|byte| **byte == b'\n').count() +} + +#[cfg(any(target_family = "unix", windows))] fn note_stdin_line_read(bytes: &[u8]) { note_stdin_bytes_read(bytes); } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", windows))] fn emit_readline_input_bytes(bytes: &[u8]) { if bytes.is_empty() { return; } - let mut pending = PYTHON_DIRECT_STDIN_SIDEBAND_INPUT.lock().unwrap(); - pending.extend_from_slice(bytes); - loop { - match std::str::from_utf8(&pending) { - Ok(text) => { - if !text.is_empty() { - ipc::emit_readline_input(text); - } - pending.clear(); - return; - } - Err(err) => { - let valid_up_to = err.valid_up_to(); - if valid_up_to == 0 { - return; - } - let text = std::str::from_utf8(&pending[..valid_up_to]) - .expect("valid UTF-8 prefix should decode"); - ipc::emit_readline_input(text); - pending.drain(..valid_up_to); - } - } + ipc::emit_readline_input_bytes(bytes); +} + +#[cfg(any(target_family = "unix", windows))] +fn emit_readline_discard_bytes(bytes: &[u8]) { + if bytes.is_empty() { + return; } + ipc::emit_readline_discard_bytes(bytes); } -#[cfg(not(target_family = "unix"))] +#[cfg(not(any(target_family = "unix", windows)))] fn note_stdin_line_read(_bytes: &[u8]) {} fn plot_capable() -> bool { @@ -2092,13 +2310,12 @@ struct SessionStateInner { #[allow(dead_code)] struct ActiveRequest { - reply: mpsc::Sender, byte_len: usize, line_count: usize, fallback_prompt: Option, consumed_lines: usize, skip_next_hook: bool, - stdin_write_complete: bool, + stdin_input_complete: bool, repl_turn_finished: bool, started_after_continuation_prompt: bool, } @@ -2140,8 +2357,7 @@ fn complete_active_request_with_options( active: Option, emit_session_end: bool, ) { - if let Some(active) = active { - let _ = active.reply.send(RequestCompleted); + if active.is_some() { state.cvar.notify_all(); } if emit_session_end { @@ -2226,8 +2442,8 @@ unsafe extern "C" fn initialize_mcp_repl_module() -> *mut PyObject { function: py_request_exit, }, ModuleMethod { - name: "emit_plot_image", - function: py_emit_plot_image, + name: "emit_output_image", + function: py_emit_output_image, }, ModuleMethod { name: "set_python_prompts", @@ -2358,13 +2574,13 @@ unsafe extern "C" fn py_request_exit(_self: *mut PyObject, args: *mut PyObject) api.none() } -unsafe extern "C" fn py_emit_plot_image( +unsafe extern "C" fn py_emit_output_image( _self: *mut PyObject, args: *mut PyObject, ) -> *mut PyObject { let api = PythonApi::global(); if api.tuple_size(args) != 4 { - set_callback_error("emit_plot_image expects exactly four arguments"); + set_callback_error("emit_output_image expects exactly four arguments"); return ptr::null_mut(); } let Some(mime_type) = api.unicode_arg(args, 0) else { @@ -2384,7 +2600,7 @@ unsafe extern "C" fn py_emit_plot_image( let Some(source) = api.unicode_arg(args, 3) else { return ptr::null_mut(); }; - ipc::emit_plot_image(&mime_type, &data, is_update == 1, Some(&source)); + ipc::emit_output_image(&source, &mime_type, &data, is_update == 1); api.none() } @@ -2442,8 +2658,10 @@ static PYTHON_STDIN_FILE: AtomicPtr = AtomicPtr::new(ptr::null_mut() static PYTHON_STDOUT_FILE: AtomicPtr = AtomicPtr::new(ptr::null_mut()); #[cfg(target_family = "unix")] static PYTHON_RUNTIME_STDIN_FD: AtomicI32 = AtomicI32::new(-1); -#[cfg(target_family = "unix")] -static PYTHON_DIRECT_STDIN_SIDEBAND_INPUT: Mutex> = Mutex::new(Vec::new()); +#[cfg(windows)] +static WINDOWS_CONSOLE_DROP_NEXT_LF: Mutex = Mutex::new(false); +#[cfg(windows)] +static WINDOWS_CONSOLE_STDIN_BYTES: Mutex> = Mutex::new(VecDeque::new()); #[cfg(test)] mod tests { @@ -2488,15 +2706,13 @@ mod tests { consumed_lines: usize, fallback_prompt: Option<&str>, ) -> ActiveRequest { - let (reply, _rx) = std::sync::mpsc::channel(); ActiveRequest { - reply, byte_len: 1, line_count, fallback_prompt: fallback_prompt.map(str::to_string), consumed_lines, skip_next_hook: false, - stdin_write_complete: true, + stdin_input_complete: true, repl_turn_finished: false, started_after_continuation_prompt: false, } @@ -2647,4 +2863,81 @@ mod tests { assert_eq!(resolve_libpython_path(&probe), Some(dll)); } + + #[cfg(any(target_family = "unix", windows))] + #[test] + fn active_stdin_accounting_counts_completed_lines() { + assert_eq!(consumed_stdin_line_count(b"partial"), 0); + assert_eq!(consumed_stdin_line_count(b"line\n"), 1); + assert_eq!(consumed_stdin_line_count(b"first\nsecond\n"), 2); + } + + #[cfg(any(target_family = "unix", windows))] + #[test] + fn delivered_input_reopens_request_after_stdin_wait_completion() { + let state = SessionState::new(); + let mut guard = state.inner.lock().unwrap(); + guard.request_active = false; + guard.request_completed_at_stdin_wait = true; + guard.plot_reset_pending = false; + guard.waiting_for_input = true; + + mark_request_input_delivered_locked(&mut guard); + + assert!(guard.request_active); + assert_eq!(guard.request_generation, 1); + assert!(!guard.request_completed_at_stdin_wait); + assert!(guard.plot_reset_pending); + assert!(!guard.waiting_for_input); + } + + #[test] + fn late_interrupt_cleanup_does_not_cross_request_boundary() { + let state = SessionState::new(); + let mut guard = state.inner.lock().unwrap(); + + guard.request_active = true; + guard.request_completed_at_stdin_wait = false; + guard.request_generation = 7; + assert!(interrupt_cleanup_belongs_to_current_request_locked( + &guard, None + )); + assert!(interrupt_cleanup_belongs_to_current_request_locked( + &guard, + Some(7) + )); + assert!(!interrupt_cleanup_belongs_to_current_request_locked( + &guard, + Some(6) + )); + + guard.request_completed_at_stdin_wait = true; + assert!(!interrupt_cleanup_belongs_to_current_request_locked( + &guard, + Some(7) + )); + + guard.request_active = false; + guard.request_completed_at_stdin_wait = false; + assert!(!interrupt_cleanup_belongs_to_current_request_locked( + &guard, + Some(7) + )); + } + + #[test] + fn delivered_input_records_background_plots_before_reopening_gate() { + let state = SessionState::new(); + let mut guard = state.inner.lock().unwrap(); + + guard.request_active = false; + guard.request_completed_at_stdin_wait = false; + assert!(request_input_should_record_background_plots_locked(&guard)); + + guard.request_active = true; + assert!(!request_input_should_record_background_plots_locked(&guard)); + + guard.request_completed_at_stdin_wait = true; + assert!(request_input_should_record_background_plots_locked(&guard)); + } } diff --git a/src/python_worker.rs b/src/python_worker.rs index a1b5c0cc..1f3346d8 100644 --- a/src/python_worker.rs +++ b/src/python_worker.rs @@ -1,59 +1,18 @@ -use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, mpsc}; use std::thread; use std::time::Duration; use crate::ipc::{ - ServerToWorkerIpcMessage, connect_from_env, emit_python_interrupt_ack, emit_session_end, - emit_stdin_write_ack, set_global_ipc, + ServerToWorkerIpcMessage, connect_from_env, emit_python_interrupt_ack, set_global_ipc, }; use crate::python_session::{self, PythonSession}; -struct WorkerState { - busy: AtomicBool, -} - -impl WorkerState { - fn try_mark_busy(&self) -> bool { - self.busy - .compare_exchange(false, true, Ordering::SeqCst, Ordering::SeqCst) - .is_ok() - } - - fn mark_idle(&self) { - self.busy.store(false, Ordering::SeqCst); - } -} - -impl Default for WorkerState { - fn default() -> Self { - Self { - busy: AtomicBool::new(false), - } - } -} - -struct QueuedRequest { - byte_len: usize, - line_count: usize, - final_prompt: Option, -} - pub fn run() -> Result<(), Box> { crate::diagnostics::startup_log("python-worker: run begin"); - let state = Arc::new(WorkerState::default()); - let (request_tx, request_rx) = mpsc::sync_channel(1); - init_ipc(state.clone(), request_tx.clone()).map_err(|err| { + init_ipc().map_err(|err| { eprintln!("python worker ipc init error: {err}"); err })?; - let request_state = state.clone(); - let _request_thread = thread::Builder::new() - .name("python-worker-requests".to_string()) - .spawn(move || request_loop(request_rx, request_state)) - .map_err(|err| format!("failed to spawn Python worker request thread: {err}"))?; - crate::diagnostics::startup_log("python-worker: starting Python session"); if let Err(err) = PythonSession::start_on_current_thread() { eprintln!("failed to start Python session: {err}"); @@ -64,19 +23,7 @@ pub fn run() -> Result<(), Box> { Ok(()) } -fn wait_for_python_session() -> Result<&'static PythonSession, String> { - loop { - if let Ok(session) = PythonSession::global() { - return Ok(session); - } - thread::sleep(Duration::from_millis(5)); - } -} - -fn init_ipc( - state: Arc, - request_tx: mpsc::SyncSender, -) -> Result<(), Box> { +fn init_ipc() -> Result<(), Box> { let conn = connect_from_env(Duration::from_secs(2))?; set_global_ipc(conn.clone()); if let Err(err) = thread::Builder::new() @@ -84,37 +31,13 @@ fn init_ipc( .spawn(move || { loop { match conn.recv(None) { - Some(ServerToWorkerIpcMessage::RequestStart) => { - python_session::mark_request_started(); - emit_stdin_write_ack(); - } - Some(ServerToWorkerIpcMessage::PythonRequestStart { request_generation }) => { - python_session::mark_request_started_for_generation(request_generation); - emit_stdin_write_ack(); - } - Some(ServerToWorkerIpcMessage::StdinWrite { - byte_len, - line_count, - final_prompt, - }) => { - handle_write_stdin( - byte_len, - line_count, - final_prompt, - state.clone(), - &request_tx, - ); - } - Some(ServerToWorkerIpcMessage::StdinWriteComplete) => { - python_session::mark_stdin_write_complete(); - } - Some(ServerToWorkerIpcMessage::Interrupt) => { - python_session::interrupt(); - } Some(ServerToWorkerIpcMessage::PythonInterrupt { request_generation }) => { python_session::interrupt_request_generation(request_generation); emit_python_interrupt_ack(); } + Some(ServerToWorkerIpcMessage::Interrupt) => { + python_session::interrupt(); + } None => { std::process::exit(0); } @@ -126,77 +49,3 @@ fn init_ipc( } Ok(()) } - -fn request_loop(rx: mpsc::Receiver, state: Arc) { - for request in rx { - let result = - write_stdin_request(request.byte_len, request.line_count, request.final_prompt); - if let Err(err) = result { - emit_stderr_message(&err.message); - emit_session_end(); - } - state.mark_idle(); - } -} - -fn handle_write_stdin( - byte_len: usize, - line_count: usize, - final_prompt: Option, - state: Arc, - request_tx: &mpsc::SyncSender, -) { - if !state.try_mark_busy() { - emit_stderr_message("worker is busy; request already running"); - return; - } - - if let Err(err) = request_tx.try_send(QueuedRequest { - byte_len, - line_count, - final_prompt, - }) { - state.mark_idle(); - let message = match err { - mpsc::TrySendError::Full(_) => "worker is busy; request already running".to_string(), - mpsc::TrySendError::Disconnected(_) => { - "worker execution thread exited unexpectedly".to_string() - } - }; - emit_stderr_message(&message); - emit_session_end(); - } -} - -struct WorkerExecError { - message: String, -} - -impl WorkerExecError { - fn new(message: impl Into) -> Self { - Self { - message: message.into(), - } - } -} - -fn write_stdin_request( - byte_len: usize, - line_count: usize, - final_prompt: Option, -) -> Result<(), WorkerExecError> { - let session = wait_for_python_session() - .map_err(|err| WorkerExecError::new(format!("failed to start Python session: {err}")))?; - let reply_rx = session - .begin_request(byte_len, line_count, final_prompt) - .map_err(WorkerExecError::new)?; - emit_stdin_write_ack(); - reply_rx - .recv() - .map(|_| ()) - .map_err(|err| WorkerExecError::new(format!("Python session reply error: {err}"))) -} - -fn emit_stderr_message(message: &str) { - crate::output_stream::write_stderr_bytes(message.as_bytes()); -} diff --git a/src/r_session.rs b/src/r_session.rs index 48832bb5..9ea71a9a 100644 --- a/src/r_session.rs +++ b/src/r_session.rs @@ -135,7 +135,7 @@ pub(crate) fn clear_pending_input() -> bool { let discarded = drain_input_queue(&mut guard.input_queue); drop(guard); if !discarded.is_empty() { - ipc::emit_readline_discard(&discarded); + ipc::emit_readline_discard_bytes(discarded.as_bytes()); } had_pending } @@ -989,21 +989,15 @@ pub extern "C-unwind" fn r_read_console( } drop(guard); - let head = line_text.as_bytes(); + let runtime_line = normalize_console_input_for_r(&line_text); + let head = runtime_line.as_bytes(); if !buf.is_null() { unsafe { std::ptr::copy_nonoverlapping(head.as_ptr(), buf, head.len()); *buf.add(head.len()) = 0; } } - ipc::emit_readline_input(&line_text); - let mut echoed = String::with_capacity(prompt.len() + line_text.len()); - echoed.push_str(prompt); - echoed.push_str(&line_text); - ipc::emit_readline_result(prompt, &line_text); - if !echoed.is_empty() { - emit_output_text(TextStream::Stdout, echoed.as_bytes()); - } + ipc::emit_readline_input_bytes(line_text.as_bytes()); return 1; } @@ -1012,6 +1006,10 @@ pub extern "C-unwind" fn r_read_console( } } +fn normalize_console_input_for_r(line: &str) -> String { + line.replace("\r\n", "\n").replace('\r', "\n") +} + pub(crate) fn push_plot_image( plot_id: String, bytes: Vec, @@ -1039,7 +1037,7 @@ pub(crate) fn push_plot_image( mime_type }; let data = STANDARD.encode(bytes); - ipc::emit_plot_image(&mime_type, &data, !is_new, Some(&plot_id)); + ipc::emit_output_image(&plot_id, &mime_type, &data, !is_new); Ok(()) } diff --git a/src/sandbox.rs b/src/sandbox.rs index c7f01487..ed71ebba 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -431,8 +431,6 @@ pub struct SandboxStateUpdate { pub sandbox_cwd: Option, #[serde(default)] pub use_linux_sandbox_bwrap: Option, - #[serde(default)] - pub use_legacy_landlock: Option, } #[derive(Debug, Clone, Deserialize, Serialize)] @@ -442,8 +440,6 @@ pub struct CodexSandboxStateMeta { #[serde(default)] pub codex_linux_sandbox_exe: Option, pub sandbox_cwd: PathBuf, - #[serde(default)] - pub use_legacy_landlock: bool, } pub fn sandbox_state_update_from_codex_meta( @@ -478,7 +474,6 @@ pub fn sandbox_state_update_from_codex_meta( // Codex reports how its own Linux helper is configured, but mcp-repl's // optional bwrap stage is a separate local best-effort knob. use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }) } @@ -507,8 +502,6 @@ impl SandboxState { } if let Some(use_bwrap) = update.use_linux_sandbox_bwrap { next.use_linux_sandbox_bwrap = use_bwrap; - } else if let Some(use_legacy_landlock) = update.use_legacy_landlock { - next.use_linux_sandbox_bwrap = !use_legacy_landlock; } let changed = next != *self; *self = next; @@ -2835,7 +2828,6 @@ mod tests { "type": "danger-full-access" }, "sandboxCwd": sandbox_cwd, - "useLegacyLandlock": false, "codexLinuxSandboxExe": if cfg!(target_os = "linux") { serde_json::Value::String("/tmp/codex-linux-sandbox".to_string()) } else { @@ -2862,7 +2854,6 @@ mod tests { "exclude_slash_tmp": false }, "sandboxCwd": sandbox_cwd, - "useLegacyLandlock": false, "codexLinuxSandboxExe": if cfg!(target_os = "linux") { serde_json::Value::String("/tmp/codex-linux-sandbox".to_string()) } else { diff --git a/src/windows_pty_filter.rs b/src/windows_pty_filter.rs new file mode 100644 index 00000000..9674f45c --- /dev/null +++ b/src/windows_pty_filter.rs @@ -0,0 +1,108 @@ +#[derive(Default)] +pub(crate) struct WindowsPtyOutputFilter { + state: WindowsPtyOutputFilterState, + pending: Vec, + emitted_output: bool, +} + +#[derive(Default)] +enum WindowsPtyOutputFilterState { + #[default] + Ground, + Escape, + Csi, + StringControl, + StringControlEscape, +} + +impl WindowsPtyOutputFilter { + pub(crate) fn filter(&mut self, bytes: &[u8]) -> Vec { + let mut output = Vec::with_capacity(bytes.len()); + for &byte in bytes { + match self.state { + WindowsPtyOutputFilterState::Ground => { + if byte == 0x1b { + self.pending.clear(); + self.pending.push(byte); + self.state = WindowsPtyOutputFilterState::Escape; + } else { + output.push(byte); + self.emitted_output = true; + } + } + WindowsPtyOutputFilterState::Escape => { + self.pending.push(byte); + if byte == b'[' { + self.state = WindowsPtyOutputFilterState::Csi; + } else if is_ansi_string_control_start(byte) { + self.pending.clear(); + self.state = WindowsPtyOutputFilterState::StringControl; + } else { + output.extend_from_slice(&self.pending); + self.emitted_output = true; + self.pending.clear(); + self.state = WindowsPtyOutputFilterState::Ground; + } + } + WindowsPtyOutputFilterState::Csi => { + self.pending.push(byte); + if is_csi_final_byte(byte) { + if !is_conpty_screen_control_csi(&self.pending) + && (self.emitted_output || !is_sgr_reset_csi(&self.pending)) + { + output.extend_from_slice(&self.pending); + self.emitted_output = true; + } + self.pending.clear(); + self.state = WindowsPtyOutputFilterState::Ground; + } else if self.pending.len() > 128 { + output.extend_from_slice(&self.pending); + self.emitted_output = true; + self.pending.clear(); + self.state = WindowsPtyOutputFilterState::Ground; + } + } + WindowsPtyOutputFilterState::StringControl => { + if byte == 0x07 { + self.state = WindowsPtyOutputFilterState::Ground; + } else if byte == 0x1b { + self.state = WindowsPtyOutputFilterState::StringControlEscape; + } + } + WindowsPtyOutputFilterState::StringControlEscape => { + if byte == b'\\' || byte == 0x07 { + self.state = WindowsPtyOutputFilterState::Ground; + } else { + self.state = WindowsPtyOutputFilterState::StringControl; + } + } + } + } + output + } +} + +fn is_sgr_reset_csi(sequence: &[u8]) -> bool { + matches!(sequence, b"\x1b[m" | b"\x1b[0m") +} + +fn is_ansi_string_control_start(byte: u8) -> bool { + matches!(byte, b']' | b'P' | b'X' | b'^' | b'_') +} + +fn is_csi_final_byte(byte: u8) -> bool { + (0x40..=0x7e).contains(&byte) +} + +fn is_conpty_screen_control_csi(sequence: &[u8]) -> bool { + if !sequence.starts_with(b"\x1b[") { + return false; + } + match sequence.last().copied() { + Some(b'@' | b'A'..=b'K' | b'P' | b'S' | b'T' | b'X' | b'f' | b'r' | b's' | b'u') => true, + Some(b'h' | b'l') => sequence + .get(2..sequence.len().saturating_sub(1)) + .is_some_and(|params| params.starts_with(b"?")), + _ => false, + } +} diff --git a/src/windows_sandbox.rs b/src/windows_sandbox.rs index 81aa5dba..a2b598da 100644 --- a/src/windows_sandbox.rs +++ b/src/windows_sandbox.rs @@ -3,6 +3,8 @@ #[cfg(test)] #[path = "windows_sandbox_test_support.rs"] mod test_support; +#[cfg(test)] +use std::cell::RefCell; use std::collections::HashMap; use std::collections::HashSet; use std::ffi::OsStr; @@ -28,6 +30,7 @@ use std::time::Instant; use std::time::{SystemTime, UNIX_EPOCH}; use crate::sandbox::{R_SESSION_TMPDIR_ENV, SandboxPolicy}; +use crate::windows_pty_filter::WindowsPtyOutputFilter; use windows_sys::Win32::Foundation::CloseHandle; #[cfg(test)] use windows_sys::Win32::Foundation::ERROR_BROKEN_PIPE; @@ -92,8 +95,13 @@ use windows_sys::Win32::Storage::FileSystem::FILE_GENERIC_WRITE; use windows_sys::Win32::Storage::FileSystem::FILE_WRITE_ATTRIBUTES; use windows_sys::Win32::Storage::FileSystem::FILE_WRITE_DATA; use windows_sys::Win32::Storage::FileSystem::FILE_WRITE_EA; +use windows_sys::Win32::System::Console::COORD; use windows_sys::Win32::System::Console::CTRL_BREAK_EVENT; +use windows_sys::Win32::System::Console::ClosePseudoConsole; +use windows_sys::Win32::System::Console::CreatePseudoConsole; +use windows_sys::Win32::System::Console::GenerateConsoleCtrlEvent; use windows_sys::Win32::System::Console::GetStdHandle; +use windows_sys::Win32::System::Console::HPCON; use windows_sys::Win32::System::Console::STD_ERROR_HANDLE; use windows_sys::Win32::System::Console::STD_INPUT_HANDLE; use windows_sys::Win32::System::Console::STD_OUTPUT_HANDLE; @@ -107,17 +115,24 @@ use windows_sys::Win32::System::JobObjects::SetInformationJobObject; use windows_sys::Win32::System::Pipes::CreatePipe; #[cfg(test)] use windows_sys::Win32::System::Pipes::PeekNamedPipe; +use windows_sys::Win32::System::Threading::CREATE_NEW_PROCESS_GROUP; use windows_sys::Win32::System::Threading::CREATE_UNICODE_ENVIRONMENT; use windows_sys::Win32::System::Threading::CreateMutexW; use windows_sys::Win32::System::Threading::CreateProcessAsUserW; +use windows_sys::Win32::System::Threading::DeleteProcThreadAttributeList; +use windows_sys::Win32::System::Threading::EXTENDED_STARTUPINFO_PRESENT; use windows_sys::Win32::System::Threading::GetCurrentProcess; use windows_sys::Win32::System::Threading::GetExitCodeProcess; use windows_sys::Win32::System::Threading::INFINITE; +use windows_sys::Win32::System::Threading::InitializeProcThreadAttributeList; use windows_sys::Win32::System::Threading::OpenProcessToken; +use windows_sys::Win32::System::Threading::PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE; use windows_sys::Win32::System::Threading::PROCESS_INFORMATION; use windows_sys::Win32::System::Threading::ReleaseMutex; use windows_sys::Win32::System::Threading::STARTF_USESTDHANDLES; +use windows_sys::Win32::System::Threading::STARTUPINFOEXW; use windows_sys::Win32::System::Threading::STARTUPINFOW; +use windows_sys::Win32::System::Threading::UpdateProcThreadAttribute; use windows_sys::Win32::System::Threading::WaitForSingleObject; #[cfg(test)] @@ -150,6 +165,19 @@ const PROTECTED_DACL_SECURITY_INFORMATION: u32 = 0x8000_0000; const WRAPPER_STDIO_DRAIN_IDLE_TIMEOUT: Duration = Duration::from_secs(2); const WRAPPER_STDIO_DRAIN_MAX_WAIT: Duration = Duration::from_secs(15); const WRAPPER_STDIO_DRAIN_POLL_INTERVAL: Duration = Duration::from_millis(50); +pub(crate) const WINDOWS_SANDBOX_CONPTY_ENV: &str = "MCP_REPL_WINDOWS_SANDBOX_CONPTY"; +static WRAPPER_CTRL_BREAK_TARGET: AtomicU64 = AtomicU64::new(0); + +#[cfg(test)] +thread_local! { + static TEST_CONSOLE_CTRL_EVENT_RECORDER: RefCell> = const { RefCell::new(None) }; +} + +#[cfg(test)] +struct TestConsoleCtrlEventRecorder { + result: i32, + events: Vec<(u32, u32)>, +} #[derive(Debug, Default)] struct AllowDenyPaths { @@ -246,6 +274,30 @@ struct WrapperChildStdio { child_stderr: File, } +struct WrapperChildConPtyStdio { + stdin_write: File, + stdout_read: File, + conpty: WrapperConPty, +} + +struct WrapperConPty { + hpc: HPCON, + input_read: HANDLE, + output_write: HANDLE, +} + +unsafe impl Send for WrapperConPty {} + +impl Drop for WrapperConPty { + fn drop(&mut self) { + unsafe { + ClosePseudoConsole(self.hpc); + CloseHandle(self.input_read); + CloseHandle(self.output_write); + } + } +} + struct WrapperStdioForwarders { stdin_forwarder: thread::JoinHandle<()>, stdout_forwarder: thread::JoinHandle<()>, @@ -285,6 +337,8 @@ struct ConsoleCtrlHandlerGuard { handler: unsafe extern "system" fn(u32) -> i32, } +struct WrapperCtrlBreakForwardTarget; + impl Drop for WrapperWriteGuard<'_> { fn drop(&mut self) { self.write_in_progress.store(false, Ordering::Release); @@ -299,6 +353,19 @@ impl Drop for ConsoleCtrlHandlerGuard { } } +impl WrapperCtrlBreakForwardTarget { + fn set(process_group_id: u32) -> Self { + WRAPPER_CTRL_BREAK_TARGET.store(process_group_id as u64, Ordering::Release); + Self + } +} + +impl Drop for WrapperCtrlBreakForwardTarget { + fn drop(&mut self) { + WRAPPER_CTRL_BREAK_TARGET.store(0, Ordering::Release); + } +} + impl Drop for PreparedLaunchAclLock { fn drop(&mut self) { unsafe { @@ -322,7 +389,16 @@ fn should_apply_network_block(policy: &SandboxPolicy) -> bool { } unsafe extern "system" fn ignore_wrapper_ctrl_break(event: u32) -> i32 { - if event == CTRL_BREAK_EVENT { 1 } else { 0 } + if event != CTRL_BREAK_EVENT { + return 0; + } + let target = WRAPPER_CTRL_BREAK_TARGET.load(Ordering::Acquire); + if let Ok(process_group_id) = u32::try_from(target) + && process_group_id != 0 + { + let _ = raw_generate_console_ctrl_event(CTRL_BREAK_EVENT, process_group_id); + } + 1 } fn install_wrapper_ctrl_break_handler() -> Result { @@ -338,6 +414,21 @@ fn install_wrapper_ctrl_break_handler() -> Result i32 { + #[cfg(test)] + if let Ok(Some(result)) = TEST_CONSOLE_CTRL_EVENT_RECORDER.try_with(|recorder| { + let mut recorder = recorder.borrow_mut(); + recorder.as_mut().map(|recorder| { + recorder.events.push((ctrl_event, process_group_id)); + recorder.result + }) + }) { + return result; + } + + unsafe { GenerateConsoleCtrlEvent(ctrl_event, process_group_id) } +} + fn upsert_env_case_insensitive(env_map: &mut HashMap, key: &str, value: &str) { let removals: Vec = env_map .keys() @@ -1477,12 +1568,15 @@ fn run_sandboxed_command_with_env_map( return Err(err); } }; + crate::diagnostics::startup_log("windows-sandbox: restricted token created"); let null_device_ace_applied = allow_null_device(psid_launch); + crate::diagnostics::startup_log("windows-sandbox: null device prepared"); let mut acl_guards: Vec = Vec::new(); let live_marker = { let _acl_lock = acquire_prepared_launch_acl_lock(prepared_capability_sid)?; + crate::diagnostics::startup_log("windows-sandbox: acl lock acquired"); let has_other_live_session = prepared_launch_live_marker_count(prepared_capability_sid) > 0; let (workspace_root_scope, extra_root_scope) = @@ -1496,6 +1590,7 @@ fn run_sandboxed_command_with_env_map( workspace_root_scope, extra_root_scope, ); + crate::diagnostics::startup_log("windows-sandbox: runtime acl refresh returned"); let prepared_launch = match refresh_result { Ok(launch) => launch, Err(err) => { @@ -1513,6 +1608,7 @@ fn run_sandboxed_command_with_env_map( prepared_capability_sid, &capability_sids.launch_sid, ); + crate::diagnostics::startup_log("windows-sandbox: live marker returned"); let marker = match marker_result { Ok(marker) => marker, Err(err) => { @@ -1528,6 +1624,7 @@ fn run_sandboxed_command_with_env_map( let launch_acl_result = apply_runtime_launch_acl_state_unlocked(&prepared_launch, psid_launch); + crate::diagnostics::startup_log("windows-sandbox: launch acl returned"); match launch_acl_result { Ok(mut launch_acl_guards) => { acl_guards.append(&mut launch_acl_guards); @@ -1561,45 +1658,88 @@ fn run_sandboxed_command_with_env_map( } } - let stdio_pipes = match create_wrapper_child_stdio() { - Ok(pipes) => pipes, - Err(err) => { - cleanup_capability_acl_state(&acl_guards, psid_launch, null_device_ace_applied); - CloseHandle(restricted_token); - if launch_sid_is_distinct { - LocalFree(psid_launch as HLOCAL); + let use_conpty = env_get_case_insensitive(&env_map, WINDOWS_SANDBOX_CONPTY_ENV) + .map(|value| value == "1" || value.eq_ignore_ascii_case("true")) + .unwrap_or(false); + let (proc_info, stdio_forwarders) = if use_conpty { + let conpty_stdio = match create_wrapper_child_conpty_stdio() { + Ok(stdio) => stdio, + Err(err) => { + cleanup_capability_acl_state(&acl_guards, psid_launch, null_device_ace_applied); + CloseHandle(restricted_token); + if launch_sid_is_distinct { + LocalFree(psid_launch as HLOCAL); + } + LocalFree(psid_capability as HLOCAL); + return Err(err); } - LocalFree(psid_capability as HLOCAL); - return Err(err); - } - }; - crate::diagnostics::startup_log("windows-sandbox: stdio pipes created"); - let spawn_result = create_process_as_user( - restricted_token, - command, - sandbox_policy_cwd, - &env_map, - Some(( - stdio_pipes.child_stdin.as_raw_handle() as HANDLE, - stdio_pipes.child_stdout.as_raw_handle() as HANDLE, - stdio_pipes.child_stderr.as_raw_handle() as HANDLE, - )), - ); - let (proc_info, _startup_info) = match spawn_result { - Ok(value) => value, - Err(err) => { - drop(stdio_pipes); - cleanup_capability_acl_state(&acl_guards, psid_launch, null_device_ace_applied); - CloseHandle(restricted_token); - if launch_sid_is_distinct { - LocalFree(psid_launch as HLOCAL); + }; + crate::diagnostics::startup_log("windows-sandbox: child ConPTY created"); + let spawn_result = create_process_as_user_conpty( + restricted_token, + command, + sandbox_policy_cwd, + &env_map, + conpty_stdio.conpty.hpc, + ); + let proc_info = match spawn_result { + Ok(value) => value, + Err(err) => { + drop(conpty_stdio); + cleanup_capability_acl_state(&acl_guards, psid_launch, null_device_ace_applied); + CloseHandle(restricted_token); + if launch_sid_is_distinct { + LocalFree(psid_launch as HLOCAL); + } + LocalFree(psid_capability as HLOCAL); + return Err(err); } - LocalFree(psid_capability as HLOCAL); - return Err(err); - } + }; + crate::diagnostics::startup_log("windows-sandbox: ConPTY child spawned"); + (proc_info, spawn_wrapper_conpty_forwarders(conpty_stdio)) + } else { + let stdio_pipes = match create_wrapper_child_stdio() { + Ok(pipes) => pipes, + Err(err) => { + cleanup_capability_acl_state(&acl_guards, psid_launch, null_device_ace_applied); + CloseHandle(restricted_token); + if launch_sid_is_distinct { + LocalFree(psid_launch as HLOCAL); + } + LocalFree(psid_capability as HLOCAL); + return Err(err); + } + }; + crate::diagnostics::startup_log("windows-sandbox: stdio pipes created"); + let spawn_result = create_process_as_user( + restricted_token, + command, + sandbox_policy_cwd, + &env_map, + Some(( + stdio_pipes.child_stdin.as_raw_handle() as HANDLE, + stdio_pipes.child_stdout.as_raw_handle() as HANDLE, + stdio_pipes.child_stderr.as_raw_handle() as HANDLE, + )), + ); + let (proc_info, _startup_info) = match spawn_result { + Ok(value) => value, + Err(err) => { + drop(stdio_pipes); + cleanup_capability_acl_state(&acl_guards, psid_launch, null_device_ace_applied); + CloseHandle(restricted_token); + if launch_sid_is_distinct { + LocalFree(psid_launch as HLOCAL); + } + LocalFree(psid_capability as HLOCAL); + return Err(err); + } + }; + crate::diagnostics::startup_log("windows-sandbox: child spawned"); + (proc_info, spawn_wrapper_stdio_forwarders(stdio_pipes)) }; - crate::diagnostics::startup_log("windows-sandbox: child spawned"); - let stdio_forwarders = spawn_wrapper_stdio_forwarders(stdio_pipes); + let _ctrl_break_forward_target = + use_conpty.then(|| WrapperCtrlBreakForwardTarget::set(proc_info.dwProcessId)); let job_handle = create_job_kill_on_close().ok(); if let Some(job) = job_handle { @@ -1980,6 +2120,81 @@ unsafe fn create_process_as_user( Ok((proc_info, startup_info)) } +unsafe fn create_process_as_user_conpty( + token: HANDLE, + argv: &[String], + cwd: &Path, + env_map: &HashMap, + hpc: HPCON, +) -> Result { + let cmdline_str = argv + .iter() + .map(|arg| quote_windows_arg(arg)) + .collect::>() + .join(" "); + let mut cmdline = to_wide(&cmdline_str); + let env_block = make_env_block(env_map); + + let mut startup_info = STARTUPINFOEXW::default(); + startup_info.StartupInfo.cb = std::mem::size_of::() as u32; + startup_info.StartupInfo.dwFlags = STARTF_USESTDHANDLES; + startup_info.StartupInfo.hStdInput = INVALID_HANDLE_VALUE; + startup_info.StartupInfo.hStdOutput = INVALID_HANDLE_VALUE; + startup_info.StartupInfo.hStdError = INVALID_HANDLE_VALUE; + + let mut attribute_list_size = 0usize; + InitializeProcThreadAttributeList(std::ptr::null_mut(), 1, 0, &mut attribute_list_size); + let mut attribute_list = vec![0u8; attribute_list_size]; + let attribute_list_ptr = attribute_list.as_mut_ptr().cast(); + if InitializeProcThreadAttributeList(attribute_list_ptr, 1, 0, &mut attribute_list_size) == 0 { + return Err(format!( + "InitializeProcThreadAttributeList failed: {}", + std::io::Error::last_os_error() + )); + } + startup_info.lpAttributeList = attribute_list_ptr; + + if UpdateProcThreadAttribute( + startup_info.lpAttributeList, + 0, + PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE as usize, + hpc as *const c_void, + std::mem::size_of::(), + std::ptr::null_mut(), + std::ptr::null(), + ) == 0 + { + DeleteProcThreadAttributeList(startup_info.lpAttributeList); + return Err(format!( + "UpdateProcThreadAttribute pseudoconsole failed: {}", + std::io::Error::last_os_error() + )); + } + + let mut proc_info: PROCESS_INFORMATION = std::mem::zeroed(); + let ok = CreateProcessAsUserW( + token, + std::ptr::null(), + cmdline.as_mut_ptr(), + std::ptr::null_mut(), + std::ptr::null_mut(), + 0, + CREATE_UNICODE_ENVIRONMENT | EXTENDED_STARTUPINFO_PRESENT | CREATE_NEW_PROCESS_GROUP, + env_block.as_ptr() as *mut c_void, + to_wide(cwd).as_ptr(), + &startup_info.StartupInfo, + &mut proc_info, + ); + DeleteProcThreadAttributeList(startup_info.lpAttributeList); + if ok == 0 { + return Err(format!( + "CreateProcessAsUserW ConPTY failed: {}", + std::io::Error::last_os_error() + )); + } + Ok(proc_info) +} + unsafe fn create_wrapper_child_stdio() -> Result { let mut child_stdin: HANDLE = std::ptr::null_mut(); let mut stdin_write: HANDLE = std::ptr::null_mut(); @@ -2039,6 +2254,54 @@ unsafe fn create_wrapper_child_stdio() -> Result { }) } +unsafe fn create_wrapper_child_conpty_stdio() -> Result { + let mut input_read: HANDLE = std::ptr::null_mut(); + let mut input_write: HANDLE = std::ptr::null_mut(); + let mut output_read: HANDLE = std::ptr::null_mut(); + let mut output_write: HANDLE = std::ptr::null_mut(); + + if CreatePipe(&mut input_read, &mut input_write, std::ptr::null_mut(), 0) == 0 { + return Err(format!( + "CreatePipe ConPTY input failed: {}", + std::io::Error::last_os_error() + )); + } + if CreatePipe(&mut output_read, &mut output_write, std::ptr::null_mut(), 0) == 0 { + CloseHandle(input_read); + CloseHandle(input_write); + return Err(format!( + "CreatePipe ConPTY output failed: {}", + std::io::Error::last_os_error() + )); + } + + let mut hpc: HPCON = 0; + let hr = CreatePseudoConsole( + COORD { X: 4096, Y: 24 }, + input_read, + output_write, + 0, + &mut hpc, + ); + if hr != 0 { + CloseHandle(input_read); + CloseHandle(input_write); + CloseHandle(output_read); + CloseHandle(output_write); + return Err(format!("CreatePseudoConsole failed: HRESULT {hr}")); + } + + Ok(WrapperChildConPtyStdio { + stdin_write: File::from_raw_handle(input_write as _), + stdout_read: File::from_raw_handle(output_read as _), + conpty: WrapperConPty { + hpc, + input_read, + output_write, + }, + }) +} + fn spawn_wrapper_stdio_forwarders(stdio: WrapperChildStdio) -> WrapperStdioForwarders { let WrapperChildStdio { stdin_write, @@ -2075,21 +2338,114 @@ fn spawn_wrapper_stdio_forwarders(stdio: WrapperChildStdio) -> WrapperStdioForwa } } +fn spawn_wrapper_conpty_forwarders(stdio: WrapperChildConPtyStdio) -> WrapperStdioForwarders { + let WrapperChildConPtyStdio { + stdin_write, + stdout_read, + conpty, + } = stdio; + + let stdin_forwarder = thread::spawn(move || { + let mut wrapper_stdin = io::stdin(); + let mut child_stdin = stdin_write; + copy_wrapper_input_to_conpty(&mut wrapper_stdin, &mut child_stdin); + let _ = child_stdin.flush(); + }); + let stdout_state = Arc::new(WrapperForwarderState::new()); + let stdout_state_thread = Arc::clone(&stdout_state); + let stdout_forwarder = thread::spawn(move || { + let _keep_conpty_alive = conpty; + copy_wrapper_conpty_output(stdout_read, io::stdout(), &stdout_state_thread); + }); + let stderr_state = Arc::new(WrapperForwarderState::new()); + stderr_state.done.store(true, Ordering::Release); + let stderr_forwarder = thread::spawn(|| {}); + + WrapperStdioForwarders { + stdin_forwarder, + stdout_forwarder, + stderr_forwarder, + stdout_state, + stderr_state, + } +} + +fn copy_wrapper_input_to_conpty(mut wrapper_input: impl Read, mut child_input: impl Write) { + let mut buffer = [0u8; 8192]; + let mut pending_cr = false; + loop { + let count = match wrapper_input.read(&mut buffer) { + Ok(0) => break, + Ok(count) => count, + Err(_) => break, + }; + let mut translated = Vec::with_capacity(count); + for byte in &buffer[..count] { + if pending_cr { + pending_cr = false; + if *byte == b'\n' { + continue; + } + } + match *byte { + b'\r' => { + translated.push(b'\r'); + pending_cr = true; + } + b'\n' => translated.push(b'\r'), + byte => translated.push(byte), + } + } + if child_input + .write_all(&translated) + .and_then(|_| child_input.flush()) + .is_err() + { + break; + } + } +} + fn copy_wrapper_output( + child_output: File, + wrapper_output: impl Write, + state: &WrapperForwarderState, +) { + copy_wrapper_output_filtered(child_output, wrapper_output, state, |bytes| bytes.to_vec()); +} + +fn copy_wrapper_conpty_output( + child_output: File, + wrapper_output: impl Write, + state: &WrapperForwarderState, +) { + let mut filter = WindowsPtyOutputFilter::default(); + copy_wrapper_output_filtered(child_output, wrapper_output, state, |bytes| { + filter.filter(bytes) + }); +} + +fn copy_wrapper_output_filtered( mut child_output: File, mut wrapper_output: impl Write, state: &WrapperForwarderState, + mut transform: impl FnMut(&[u8]) -> Vec, ) { let mut buffer = [0u8; 8192]; loop { match child_output.read(&mut buffer) { Ok(0) => break, Ok(count) => { + let output = transform(&buffer[..count]); let write_result = { let _write_guard = state.begin_write(); - let result = wrapper_output - .write_all(&buffer[..count]) - .and_then(|_| wrapper_output.flush()); + let result = if output.is_empty() { + wrapper_output.flush() + } else { + wrapper_output + .write_all(&output) + .and_then(|_| wrapper_output.flush()) + }; if result.is_ok() { state .bytes_copied @@ -3345,6 +3701,58 @@ mod tests { } } + fn capture_recorded_ctrl_events(f: F) -> (R, Vec<(u32, u32)>) + where + F: FnOnce() -> R, + { + TEST_CONSOLE_CTRL_EVENT_RECORDER.with(|recorder| { + assert!( + recorder.borrow().is_none(), + "did not expect nested console ctrl-event recorder" + ); + *recorder.borrow_mut() = Some(TestConsoleCtrlEventRecorder { + result: 1, + events: Vec::new(), + }); + }); + let result = f(); + let events = TEST_CONSOLE_CTRL_EVENT_RECORDER.with(|recorder| { + recorder + .borrow_mut() + .take() + .expect("recorded console ctrl events") + .events + }); + (result, events) + } + + #[test] + fn wrapper_ctrl_break_handler_forwards_to_conpty_child_process_group() { + let _guard = prepare_sandbox_launch_test_mutex() + .lock() + .expect("windows sandbox test mutex"); + let target = WrapperCtrlBreakForwardTarget::set(4242); + + let (handled, events) = + capture_recorded_ctrl_events(|| unsafe { ignore_wrapper_ctrl_break(CTRL_BREAK_EVENT) }); + + assert_eq!(handled, 1); + assert_eq!( + events, + vec![(CTRL_BREAK_EVENT, 4242)], + "expected wrapper Ctrl-Break handler to forward to the ConPTY child process group" + ); + + drop(target); + let (handled_after_drop, events_after_drop) = + capture_recorded_ctrl_events(|| unsafe { ignore_wrapper_ctrl_break(CTRL_BREAK_EVENT) }); + assert_eq!(handled_after_drop, 1); + assert!( + events_after_drop.is_empty(), + "did not expect Ctrl-Break forwarding after the target guard is dropped" + ); + } + #[cfg(target_os = "windows")] fn remove_junction(path: &Path) { if !path.exists() { @@ -3621,6 +4029,40 @@ mod tests { ); } + #[test] + fn copy_wrapper_conpty_output_filters_terminal_control_sequences() { + let tmp = tempdir().expect("tempdir"); + let payload_path = tmp.path().join("payload.bin"); + let payload = b"\r\nmcp-repl\n\x1b[?25l\x1b[15;1H\x1b[?25h\x1b]0;title\x07>>> "; + std::fs::write(&payload_path, payload).expect("write payload"); + + let state = WrapperForwarderState::new(); + let writer_state = Arc::new(Mutex::new(RecordingWriterState::default())); + let writer = RecordingWriter { + state: Arc::clone(&writer_state), + }; + + let input = File::open(&payload_path).expect("open payload"); + copy_wrapper_conpty_output(input, writer, &state); + + let recorded = writer_state.lock().expect("recording writer state mutex"); + assert_eq!(recorded.bytes, b"\r\nmcp-repl\n>>> "); + assert_eq!( + state.bytes_copied.load(Ordering::Relaxed), + payload.len() as u64, + "filtered control-only bytes should still count as drain progress" + ); + } + + #[test] + fn copy_wrapper_input_to_conpty_translates_line_endings() { + let mut output = Vec::new(); + + copy_wrapper_input_to_conpty(&b"a\r\nb\nc\rd"[..], &mut output); + + assert_eq!(output, b"a\rb\rc\rd"); + } + #[test] fn windows_wrapper_launch_uses_forwarded_pipes() { let pipes = unsafe { create_wrapper_child_stdio() }.expect("wrapper stdio pipes"); diff --git a/src/worker.rs b/src/worker.rs index d9949230..35c15a2d 100644 --- a/src/worker.rs +++ b/src/worker.rs @@ -90,16 +90,10 @@ fn init_ipc() -> Result<(), Box> { .spawn(move || { loop { match conn.recv(None) { - Some(ServerToWorkerIpcMessage::RequestStart) => {} - Some(ServerToWorkerIpcMessage::PythonRequestStart { .. }) => {} - Some(ServerToWorkerIpcMessage::StdinWrite { .. }) => {} - Some(ServerToWorkerIpcMessage::StdinWriteComplete) => {} + Some(ServerToWorkerIpcMessage::PythonInterrupt { .. }) => {} Some(ServerToWorkerIpcMessage::Interrupt) => { crate::r_session::clear_pending_input(); } - Some(ServerToWorkerIpcMessage::PythonInterrupt { .. }) => { - crate::r_session::clear_pending_input(); - } None => { // Without IPC, the worker cannot participate in turn accounting (prompt, // request boundaries, etc). Exit immediately so the server can respawn. diff --git a/src/worker_process.rs b/src/worker_process.rs index 32e227ff..022a7c1a 100644 --- a/src/worker_process.rs +++ b/src/worker_process.rs @@ -1,4 +1,4 @@ -#[cfg(all(test, target_family = "unix"))] +#[cfg(all(test, any(target_family = "unix", target_family = "windows")))] use std::cell::RefCell; #[cfg(target_family = "unix")] use std::collections::{HashMap, HashSet}; @@ -28,7 +28,7 @@ use crate::ipc::{ ServerToWorkerIpcMessage, WorkerToServerIpcMessage, }; #[cfg(any(target_family = "unix", target_family = "windows"))] -use crate::ipc::{IpcHandlers, IpcPlotImage}; +use crate::ipc::{IpcHandlers, IpcOutputImage}; #[cfg(test)] use crate::output_capture::OutputRange; use crate::output_capture::{ @@ -39,9 +39,7 @@ use crate::output_capture::{ use crate::output_timeline::{EchoCollapseMode, collapse_echo_with_attribution}; use crate::oversized_output::OversizedOutputMode; use crate::pager::{self, Pager}; -use crate::pending_output_tape::{ - FormattedPendingOutput, PendingOutputTape, PendingSidebandKind, PendingTextSource, -}; +use crate::pending_output_tape::{FormattedPendingOutput, PendingOutputTape, PendingSidebandKind}; use crate::sandbox::{ R_SESSION_TMPDIR_ENV, SandboxState, SandboxStateUpdate, prepare_worker_command_with_managed_network, @@ -53,36 +51,74 @@ use crate::sandbox_cli::{ }; use crate::stdin_payload::prepare_worker_stdin_payload; pub(crate) use crate::stdin_payload::{WriteStdinControlAction, split_write_stdin_control_prefix}; +#[cfg(target_family = "windows")] +use crate::windows_pty_filter::WindowsPtyOutputFilter; use crate::worker_protocol::{ ContentOrigin, TextStream, WORKER_MODE_ARG, WorkerContent, WorkerErrorCode, WorkerReply, }; +#[cfg(target_family = "windows")] +use portable_pty::ExitStatus as WorkerExitStatus; #[cfg(target_family = "unix")] use portable_pty::{PtySize, native_pty_system}; +#[cfg(target_family = "windows")] +use std::ffi::{OsStr, OsString}; #[cfg(target_family = "unix")] use std::os::unix::io::{AsRawFd, FromRawFd}; #[cfg(target_family = "unix")] use std::os::unix::process::CommandExt; #[cfg(target_family = "windows")] -use std::os::windows::io::AsRawHandle; +use std::os::windows::ffi::{OsStrExt, OsStringExt}; +#[cfg(target_family = "windows")] +use std::os::windows::io::{AsRawHandle, FromRawHandle}; #[cfg(target_family = "windows")] use std::os::windows::process::CommandExt; #[cfg(target_family = "unix")] use sysinfo::{Pid, ProcessesToUpdate, System}; #[cfg(target_family = "windows")] -use windows_sys::Win32::Foundation::{ERROR_BROKEN_PIPE, ERROR_HANDLE_EOF}; +use windows_sys::Win32::Foundation::{ + CloseHandle, ERROR_BROKEN_PIPE, ERROR_HANDLE_EOF, HANDLE, INVALID_HANDLE_VALUE, WAIT_FAILED, + WAIT_TIMEOUT, +}; +#[cfg(target_family = "windows")] +use windows_sys::Win32::System::Console::{ + COORD, CTRL_BREAK_EVENT, ClosePseudoConsole, CreatePseudoConsole, GenerateConsoleCtrlEvent, + HPCON, +}; #[cfg(target_family = "windows")] -use windows_sys::Win32::System::Console::{CTRL_BREAK_EVENT, GenerateConsoleCtrlEvent}; +use windows_sys::Win32::System::Environment::{FreeEnvironmentStringsW, GetEnvironmentStringsW}; #[cfg(target_family = "windows")] -use windows_sys::Win32::System::Pipes::PeekNamedPipe; +use windows_sys::Win32::System::Pipes::{CreatePipe, PeekNamedPipe}; #[cfg(target_family = "windows")] -use windows_sys::Win32::System::Threading::CREATE_NEW_PROCESS_GROUP; +use windows_sys::Win32::System::Threading::{ + CREATE_NEW_PROCESS_GROUP, CREATE_UNICODE_ENVIRONMENT, CreateProcessW, + DeleteProcThreadAttributeList, EXTENDED_STARTUPINFO_PRESENT, GetExitCodeProcess, INFINITE, + InitializeProcThreadAttributeList, PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE, PROCESS_INFORMATION, + STARTF_USESTDHANDLES, STARTUPINFOEXW, TerminateProcess, UpdateProcThreadAttribute, + WaitForSingleObject, +}; + +#[cfg(not(target_family = "windows"))] +type WorkerChild = Child; +#[cfg(not(target_family = "windows"))] +type WorkerExitStatus = std::process::ExitStatus; #[cfg(all(test, target_family = "unix"))] thread_local! { static TEST_UNIX_KILL_RECORDER: RefCell>> = const { RefCell::new(None) }; } +#[cfg(all(test, target_family = "windows"))] +thread_local! { + static TEST_WINDOWS_CTRL_EVENT_RECORDER: RefCell> = const { RefCell::new(None) }; +} + +#[cfg(all(test, target_family = "windows"))] +struct TestWindowsCtrlEventRecorder { + result: i32, + events: Vec<(u32, u32)>, +} + #[cfg(target_family = "unix")] fn raw_unix_kill(target: i32, signal: i32) -> i32 { #[cfg(test)] @@ -99,6 +135,22 @@ fn raw_unix_kill(target: i32, signal: i32) -> i32 { unsafe { libc::kill(target, signal) } } +#[cfg(target_family = "windows")] +fn raw_windows_generate_console_ctrl_event(ctrl_event: u32, process_group_id: u32) -> i32 { + #[cfg(test)] + if let Ok(Some(result)) = TEST_WINDOWS_CTRL_EVENT_RECORDER.try_with(|recorder| { + let mut recorder = recorder.borrow_mut(); + recorder.as_mut().map(|recorder| { + recorder.events.push((ctrl_event, process_group_id)); + recorder.result + }) + }) { + return result; + } + + unsafe { GenerateConsoleCtrlEvent(ctrl_event, process_group_id) } +} + #[derive(Debug, Clone)] struct GuardrailEvent { message: String, @@ -194,19 +246,16 @@ impl LiveOutputCapture { } } - fn append_image(&self, image: IpcPlotImage) { + fn append_image(&self, image: IpcOutputImage) { if image.updates_previous_image { self.output_timeline.append_text_event( PREVIOUS_IMAGE_UPDATE_NOTICE.to_string(), false, ContentOrigin::Server, - Some(image.readline_results_seen), + None, ); if let Some(tape) = &self.pending_output_tape { - tape.append_stdout_status_event( - PREVIOUS_IMAGE_UPDATE_NOTICE.to_string(), - image.readline_results_seen, - ); + tape.append_stdout_status_event(PREVIOUS_IMAGE_UPDATE_NOTICE.to_string(), 0); } } self.output_timeline.append_image( @@ -214,16 +263,10 @@ impl LiveOutputCapture { image.mime_type.clone(), image.data.clone(), image.is_new, - image.readline_results_seen, + 0, ); if let Some(tape) = &self.pending_output_tape { - tape.append_image( - image.id, - image.mime_type, - image.data, - image.is_new, - image.readline_results_seen, - ); + tape.append_image(image.id, image.mime_type, image.data, image.is_new, 0); } } @@ -286,6 +329,10 @@ trait BackendDriver: Send { prepare_worker_stdin_payload(text) } + fn prepare_stdin_write_payload(&self, payload: &[u8]) -> Vec { + payload.to_vec() + } + fn on_input_start( &mut self, text: &str, @@ -307,7 +354,7 @@ trait BackendDriver: Send { ipc: ServerIpcConnection, ) -> Result; fn interrupt(&mut self, process: &mut WorkerProcess) -> Result<(), WorkerError>; - fn refresh_backend_info( + fn wait_worker_ready( &mut self, ipc: ServerIpcConnection, timeout: Duration, @@ -322,53 +369,27 @@ impl RBackendDriver { } } -#[cfg_attr(target_family = "unix", allow(dead_code))] -fn driver_on_input_start(_text: &str, ipc: &ServerIpcConnection) -> Result<(), WorkerError> { - ipc.begin_request(); - if let Some(message) = ipc.take_protocol_error() { - return Err(WorkerError::Protocol(message)); - } - Ok(()) -} - -#[cfg_attr(target_family = "unix", allow(dead_code))] -fn driver_announce_stdin_write( - byte_len: usize, - line_count: usize, - final_prompt: Option, - ipc: &ServerIpcConnection, -) -> Result<(), WorkerError> { - ipc.send(ServerToWorkerIpcMessage::StdinWrite { - byte_len, - line_count, - final_prompt, - }) - .map_err(WorkerError::Io) -} - -fn driver_announce_stdin_write_complete(ipc: &ServerIpcConnection) -> Result<(), WorkerError> { - ipc.send(ServerToWorkerIpcMessage::StdinWriteComplete) - .map_err(WorkerError::Io) -} - -fn driver_wait_for_stdin_write_ack( - ipc: &ServerIpcConnection, +const REQUEST_COMPLETION_STABLE_WAIT: Duration = Duration::from_millis(20); +const PYTHON_INTERRUPT_CLEANUP_TIMEOUT: Duration = Duration::from_millis(500); +fn driver_wait_for_completion( timeout: Duration, -) -> Result<(), WorkerError> { - match ipc.wait_for_stdin_write_ack(timeout) { - Ok(()) => Ok(()), + ipc: ServerIpcConnection, + echo_source: OutputTextSource, +) -> Result { + if timeout.is_zero() { + return Err(WorkerError::Timeout(timeout)); + } + match ipc.wait_for_request_completion(timeout, REQUEST_COMPLETION_STABLE_WAIT) { + Ok(()) => Ok(completion_info_from_ipc(&ipc, false, echo_source)), Err(IpcWaitError::Timeout) => Err(WorkerError::Timeout(timeout)), - Err(IpcWaitError::SessionEnd) => Err(WorkerError::Protocol( - "worker session ended before accepting stdin".to_string(), - )), + Err(IpcWaitError::SessionEnd) => Ok(completion_info_from_ipc(&ipc, true, echo_source)), Err(IpcWaitError::Disconnected) => Err(WorkerError::Protocol( - "ipc disconnected before worker accepted stdin".to_string(), + "ipc disconnected while waiting for request completion".to_string(), )), Err(IpcWaitError::Protocol(message)) => Err(WorkerError::Protocol(message)), } } -#[cfg(target_family = "unix")] fn driver_wait_for_python_interrupt_ack( ipc: &ServerIpcConnection, timeout: Duration, @@ -377,30 +398,10 @@ fn driver_wait_for_python_interrupt_ack( Ok(()) => Ok(()), Err(IpcWaitError::Timeout) => Err(WorkerError::Timeout(timeout)), Err(IpcWaitError::SessionEnd) => Err(WorkerError::Protocol( - "worker session ended before cleaning up interrupt".to_string(), - )), - Err(IpcWaitError::Disconnected) => Err(WorkerError::Protocol( - "ipc disconnected before worker cleaned up interrupt".to_string(), + "worker session ended before Python interrupt cleanup completed".to_string(), )), - Err(IpcWaitError::Protocol(message)) => Err(WorkerError::Protocol(message)), - } -} - -const REQUEST_COMPLETION_STABLE_WAIT: Duration = Duration::from_millis(20); -fn driver_wait_for_completion( - timeout: Duration, - ipc: ServerIpcConnection, - echo_source: OutputTextSource, -) -> Result { - if timeout.is_zero() { - return Err(WorkerError::Timeout(timeout)); - } - match ipc.wait_for_request_completion(timeout, REQUEST_COMPLETION_STABLE_WAIT) { - Ok(()) => Ok(completion_info_from_ipc(&ipc, false, echo_source)), - Err(IpcWaitError::Timeout) => Err(WorkerError::Timeout(timeout)), - Err(IpcWaitError::SessionEnd) => Ok(completion_info_from_ipc(&ipc, true, echo_source)), Err(IpcWaitError::Disconnected) => Err(WorkerError::Protocol( - "ipc disconnected while waiting for request completion".to_string(), + "ipc disconnected before Python interrupt cleanup completed".to_string(), )), Err(IpcWaitError::Protocol(message)) => Err(WorkerError::Protocol(message)), } @@ -410,53 +411,15 @@ fn driver_interrupt(process: &mut WorkerProcess) -> Result<(), WorkerError> { if let Some(ipc) = process.ipc.get() { let _ = ipc.send(ServerToWorkerIpcMessage::Interrupt); } - process.send_interrupt() -} -#[cfg_attr(target_family = "unix", allow(dead_code))] -fn driver_refresh_backend_info( - ipc: ServerIpcConnection, - timeout: Duration, - timeout_is_ok: bool, -) -> Result<(), WorkerError> { - match ipc.wait_for_backend_info(timeout) { - Ok(WorkerToServerIpcMessage::BackendInfo { .. }) => Ok(()), - Ok(WorkerToServerIpcMessage::WorkerReady { protocol, .. }) => { - if protocol.name != "mcp-repl-worker" || protocol.version != 1 { - return Err(WorkerError::Protocol(format!( - "unsupported worker protocol {} version {}", - protocol.name, protocol.version - ))); - } - Ok(()) - } - Ok(_) => Err(WorkerError::Protocol( - "unexpected ipc message while waiting for backend info".to_string(), - )), - Err(IpcWaitError::Timeout) => { - if timeout_is_ok { - Ok(()) - } else { - Err(WorkerError::Protocol( - "timed out waiting for backend info".to_string(), - )) - } - } - Err(IpcWaitError::Disconnected) => Err(WorkerError::Protocol( - "ipc disconnected while waiting for backend info".to_string(), - )), - Err(IpcWaitError::SessionEnd) => Err(WorkerError::Protocol( - "worker session ended before backend info".to_string(), - )), - Err(IpcWaitError::Protocol(message)) => Err(WorkerError::Protocol(message)), - } + process.send_interrupt() } -fn driver_refresh_worker_ready( +fn driver_wait_worker_ready( ipc: ServerIpcConnection, timeout: Duration, ) -> Result<(), WorkerError> { - match ipc.wait_for_backend_info(timeout) { + match ipc.wait_for_worker_ready(timeout) { Ok(WorkerToServerIpcMessage::WorkerReady { protocol, .. }) => { if protocol.name != "mcp-repl-worker" || protocol.version != 1 { return Err(WorkerError::Protocol(format!( @@ -483,10 +446,6 @@ fn driver_refresh_worker_ready( } impl BackendDriver for RBackendDriver { - fn prepare_input_text(&self, text: String) -> String { - normalize_input_newlines(&text) - } - fn on_input_start( &mut self, _text: &str, @@ -529,514 +488,173 @@ impl BackendDriver for RBackendDriver { process.send_r_interrupt() } - fn refresh_backend_info( + fn wait_worker_ready( &mut self, ipc: ServerIpcConnection, timeout: Duration, ) -> Result<(), WorkerError> { - driver_refresh_worker_ready(ipc, timeout) + driver_wait_worker_ready(ipc, timeout) } } -#[cfg(not(target_family = "unix"))] -struct PythonBackendDriver; - -#[cfg(not(target_family = "unix"))] -impl PythonBackendDriver { - fn new() -> Self { - Self - } +struct ProtocolBackendDriver { + #[cfg_attr(not(target_family = "windows"), allow(dead_code))] + stdin_transport: WorkerStdinTransport, + stdin_accounting: ProtocolStdinAccounting, + python_request_generation: Option, } -#[cfg(not(target_family = "unix"))] -fn python_final_prompt_hint(text: &str) -> Option { - if text.trim().is_empty() { - return None; - } - if python_requires_continuation(text) { - return Some("... ".to_string()); - } - if text_ends_with_blank_line(text) { - return None; - } - let text = text.trim_end_matches(['\r', '\n']); - let last_line = text.rsplit(['\n', '\r']).next().unwrap_or(text); - let has_previous_line = text.contains(['\n', '\r']); - let trimmed_last = last_line.trim_end(); - let code_last = python_line_code_before_comment(trimmed_last).trim_end(); - if code_last.ends_with(':') - || code_last.trim_start().starts_with('@') - || (has_previous_line && python_has_open_block_suite(text)) - { - Some("... ".to_string()) - } else { - None - } +#[derive(Clone, Copy)] +enum ProtocolStdinAccounting { + Payload, + NormalizeNewlines, + ExternalWorker, } -#[cfg(not(target_family = "unix"))] -fn python_has_open_block_suite(text: &str) -> bool { - let mut block_indents = Vec::new(); - let mut scan_state = PythonLineScanState::default(); - for line in text.lines() { - let code = python_line_code_before_comment_with_state(line, &mut scan_state); - let code = code.trim_end(); - if code.trim().is_empty() { - if line.trim().is_empty() && !scan_state.continuation_active() { - block_indents.clear(); - } - continue; - } - let indent = python_line_indent(line); - while block_indents - .last() - .is_some_and(|block_indent| indent <= *block_indent) - { - block_indents.pop(); - } - if code.ends_with(':') { - block_indents.push(indent); +impl ProtocolBackendDriver { + fn new( + stdin_transport: WorkerStdinTransport, + stdin_accounting: ProtocolStdinAccounting, + ) -> Self { + Self { + stdin_transport, + stdin_accounting, + python_request_generation: None, } } - !block_indents.is_empty() -} -#[cfg(not(target_family = "unix"))] -#[derive(Default)] -struct PythonLineScanState { - quote: Option<(char, bool)>, - escaped: bool, - groups: Vec, -} + fn python( + stdin_transport: WorkerStdinTransport, + stdin_accounting: ProtocolStdinAccounting, + ) -> Self { + Self { + stdin_transport, + stdin_accounting, + python_request_generation: Some(0), + } + } -#[cfg(not(target_family = "unix"))] -impl PythonLineScanState { - fn continuation_active(&self) -> bool { - self.quote.is_some_and(|(_, triple)| triple) || !self.groups.is_empty() + fn next_python_request_generation(&mut self) -> Option { + let generation = self.python_request_generation.as_mut()?; + *generation = generation.wrapping_add(1); + Some(*generation) } } -#[cfg(not(target_family = "unix"))] -fn python_line_code_before_comment_with_state( - line: &str, - state: &mut PythonLineScanState, -) -> String { - let mut code = String::with_capacity(line.len()); - let mut chars = line.char_indices().peekable(); - - while let Some((_, ch)) = chars.next() { - if let Some((delimiter, triple)) = state.quote { - if triple { - if ch == delimiter && take_next_two_indexed(&mut chars, delimiter) { - state.quote = None; - } - continue; - } - - if state.escaped { - state.escaped = false; - continue; +impl BackendDriver for ProtocolBackendDriver { + fn prepare_input_payload(&self, text: &str) -> Vec { + let payload = match self.stdin_accounting { + ProtocolStdinAccounting::NormalizeNewlines => { + prepare_worker_stdin_payload(&normalize_input_newlines(text)) } - if ch == '\\' { - state.escaped = true; - continue; + ProtocolStdinAccounting::Payload | ProtocolStdinAccounting::ExternalWorker => { + prepare_worker_stdin_payload(text) } - if ch == delimiter { - state.quote = None; + }; + #[cfg(target_family = "windows")] + { + if matches!(self.stdin_transport, WorkerStdinTransport::Pty) + && matches!( + self.stdin_accounting, + ProtocolStdinAccounting::ExternalWorker + ) + { + return windows_pty_accounting_payload(&payload); } - continue; } + payload + } - match ch { - '#' => break, - '\'' | '"' => { - let triple = take_next_two_indexed(&mut chars, ch); - state.quote = Some((ch, triple)); - } - '(' => { - state.groups.push(')'); - code.push(ch); - } - '[' => { - state.groups.push(']'); - code.push(ch); - } - '{' => { - state.groups.push('}'); - code.push(ch); - } - ')' | ']' | '}' if state.groups.last() == Some(&ch) => { - state.groups.pop(); - code.push(ch); - } - ')' | ']' | '}' => { - code.push(ch); + fn prepare_stdin_write_payload(&self, payload: &[u8]) -> Vec { + #[cfg(target_family = "windows")] + { + if matches!(self.stdin_transport, WorkerStdinTransport::Pty) { + return windows_pty_input_payload(payload); } - _ => code.push(ch), } + payload.to_vec() } - if state.quote.is_some_and(|(_, triple)| !triple) { - state.quote = None; - state.escaped = false; + fn on_input_start( + &mut self, + _text: &str, + payload: &[u8], + ipc: &ServerIpcConnection, + _timeout: Duration, + ) -> Result<(), WorkerError> { + let _ = self.next_python_request_generation(); + ipc.begin_request_with_stdin(payload); + if let Some(message) = ipc.take_protocol_error() { + return Err(WorkerError::Protocol(message)); + } + Ok(()) } - code -} -#[cfg(not(target_family = "unix"))] -fn python_line_indent(line: &str) -> usize { - line.chars() - .take_while(|ch| matches!(ch, ' ' | '\t')) - .count() -} + fn should_settle_output_after_timeout( + &self, + _oversized_output: OversizedOutputMode, + _pending_input: Option<&str>, + ) -> bool { + false + } -#[cfg(not(target_family = "unix"))] -fn python_line_code_before_comment(line: &str) -> &str { - let mut chars = line.char_indices().peekable(); - let mut quote: Option<(char, bool)> = None; - let mut escaped = false; - - while let Some((idx, ch)) = chars.next() { - if let Some((delimiter, triple)) = quote { - if triple { - if ch == delimiter && take_next_two_indexed(&mut chars, delimiter) { - quote = None; - } - continue; - } + fn wait_for_completion( + &mut self, + timeout: Duration, + ipc: ServerIpcConnection, + ) -> Result { + driver_wait_for_completion(timeout, ipc, OutputTextSource::Ipc) + } - if escaped { - escaped = false; - continue; - } - if ch == '\\' { - escaped = true; - continue; - } - if ch == delimiter { - quote = None; + fn interrupt(&mut self, process: &mut WorkerProcess) -> Result<(), WorkerError> { + if let Some(request_generation) = self.python_request_generation { + if let Some(ipc) = process.ipc.get() + && ipc + .send(ServerToWorkerIpcMessage::PythonInterrupt { request_generation }) + .is_ok() + { + driver_wait_for_python_interrupt_ack(&ipc, PYTHON_INTERRUPT_CLEANUP_TIMEOUT)?; } - continue; + return process.send_interrupt(); } + driver_interrupt(process) + } - match ch { - '#' => return &line[..idx], - '\'' | '"' => { - let triple = take_next_two_indexed(&mut chars, ch); - quote = Some((ch, triple)); - } - _ => {} - } + fn wait_worker_ready( + &mut self, + ipc: ServerIpcConnection, + timeout: Duration, + ) -> Result<(), WorkerError> { + driver_wait_worker_ready(ipc, timeout) } +} - line +impl std::fmt::Display for WorkerError { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + match self { + WorkerError::Io(err) => write!(f, "worker io error: {err}"), + WorkerError::Protocol(message) => write!(f, "worker protocol error: {message}"), + WorkerError::Timeout(duration) => write!( + f, + "worker response timed out after {} ms", + duration.as_millis() + ), + WorkerError::Sandbox(message) => write!(f, "worker sandbox error: {message}"), + WorkerError::Guardrail(message) => write!(f, "{message}"), + } + } } -#[cfg(not(target_family = "unix"))] -fn python_requires_continuation(text: &str) -> bool { - has_unclosed_python_group_or_string(text) || final_line_continues_with_backslash(text) +impl std::error::Error for WorkerError { + fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { + match self { + WorkerError::Io(err) => Some(err), + _ => None, + } + } } -#[cfg(not(target_family = "unix"))] -fn final_line_continues_with_backslash(text: &str) -> bool { - let Some(line) = text.lines().last() else { - return false; - }; - python_line_code_before_comment(line) - .trim_end() - .chars() - .rev() - .take_while(|ch| *ch == '\\') - .count() - % 2 - == 1 -} - -#[cfg(not(target_family = "unix"))] -fn has_unclosed_python_group_or_string(text: &str) -> bool { - let mut stack = Vec::new(); - let mut chars = text.chars().peekable(); - let mut quote: Option<(char, bool)> = None; - let mut escaped = false; - - while let Some(ch) = chars.next() { - if let Some((delimiter, triple)) = quote { - if triple { - if ch == delimiter && take_next_two(&mut chars, delimiter) { - quote = None; - } - continue; - } - - if escaped { - escaped = false; - continue; - } - if ch == '\\' { - escaped = true; - continue; - } - if ch == delimiter { - quote = None; - } - continue; - } - - match ch { - '#' => { - for next in chars.by_ref() { - if matches!(next, '\n' | '\r') { - break; - } - } - } - '\'' | '"' => { - let triple = take_next_two(&mut chars, ch); - quote = Some((ch, triple)); - } - '(' => stack.push(')'), - '[' => stack.push(']'), - '{' => stack.push('}'), - ')' | ']' | '}' if stack.last() == Some(&ch) => { - stack.pop(); - } - ')' | ']' | '}' => {} - _ => {} - } - } - - match quote { - Some((_, true)) => true, - Some((_, false)) => false, - None => !stack.is_empty(), - } -} - -#[cfg(not(target_family = "unix"))] -fn take_next_two(chars: &mut std::iter::Peekable>, expected: char) -> bool { - let mut clone = chars.clone(); - if clone.next() != Some(expected) || clone.next() != Some(expected) { - return false; - } - chars.next(); - chars.next(); - true -} - -#[cfg(not(target_family = "unix"))] -fn take_next_two_indexed( - chars: &mut std::iter::Peekable>, - expected: char, -) -> bool { - let mut clone = chars.clone(); - if clone.next().map(|(_, ch)| ch) != Some(expected) - || clone.next().map(|(_, ch)| ch) != Some(expected) - { - return false; - } - chars.next(); - chars.next(); - true -} - -#[cfg(not(target_family = "unix"))] -fn text_ends_with_blank_line(text: &str) -> bool { - let Some(text) = strip_one_line_ending(text) else { - return false; - }; - text.ends_with('\n') || text.ends_with('\r') -} - -#[cfg(not(target_family = "unix"))] -fn strip_one_line_ending(text: &str) -> Option<&str> { - text.strip_suffix("\r\n") - .or_else(|| text.strip_suffix('\n')) - .or_else(|| text.strip_suffix('\r')) -} - -#[cfg(not(target_family = "unix"))] -impl BackendDriver for PythonBackendDriver { - fn on_input_start( - &mut self, - text: &str, - payload: &[u8], - ipc: &ServerIpcConnection, - timeout: Duration, - ) -> Result<(), WorkerError> { - driver_on_input_start(text, ipc)?; - let line_count = payload.iter().filter(|byte| **byte == b'\n').count(); - let final_prompt = python_final_prompt_hint(text); - driver_announce_stdin_write(payload.len(), line_count, final_prompt, ipc)?; - driver_wait_for_stdin_write_ack(ipc, timeout) - } - - fn on_input_written(&mut self, ipc: &ServerIpcConnection) -> Result<(), WorkerError> { - driver_announce_stdin_write_complete(ipc) - } - - fn should_settle_output_after_timeout( - &self, - _oversized_output: OversizedOutputMode, - _pending_input: Option<&str>, - ) -> bool { - false - } - - fn wait_for_completion( - &mut self, - timeout: Duration, - ipc: ServerIpcConnection, - ) -> Result { - driver_wait_for_completion(timeout, ipc, OutputTextSource::Ipc) - } - - fn interrupt(&mut self, process: &mut WorkerProcess) -> Result<(), WorkerError> { - driver_interrupt(process) - } - - fn refresh_backend_info( - &mut self, - ipc: ServerIpcConnection, - timeout: Duration, - ) -> Result<(), WorkerError> { - driver_refresh_backend_info(ipc, timeout, false) - } -} - -struct ProtocolBackendDriver { - #[cfg(target_family = "unix")] - python_request_generation: Option, -} - -impl ProtocolBackendDriver { - fn new() -> Self { - Self { - #[cfg(target_family = "unix")] - python_request_generation: None, - } - } - - #[cfg(target_family = "unix")] - fn python() -> Self { - Self { - python_request_generation: Some(0), - } - } - - #[cfg(target_family = "unix")] - fn next_python_request_generation(&mut self) -> Option { - let generation = self.python_request_generation.as_mut()?; - *generation = generation.wrapping_add(1); - Some(*generation) - } -} - -impl BackendDriver for ProtocolBackendDriver { - fn on_input_start( - &mut self, - _text: &str, - payload: &[u8], - ipc: &ServerIpcConnection, - timeout: Duration, - ) -> Result<(), WorkerError> { - ipc.begin_request_with_stdin(payload); - #[cfg(not(target_family = "unix"))] - let _ = timeout; - #[cfg(target_family = "unix")] - if let Some(request_generation) = self.next_python_request_generation() { - // Built-in Unix Python reads request bytes through the worker stdin fd - // like a protocol worker, but its plot hooks still need a Python-side - // request boundary before follow-up stdin is consumed. The generation - // also lets a late interrupt avoid draining fd 0 after the next request - // has started. Custom protocol workers do not receive this private - // bridge message. - ipc.send(ServerToWorkerIpcMessage::PythonRequestStart { request_generation }) - .map_err(WorkerError::Io)?; - driver_wait_for_stdin_write_ack(ipc, timeout)?; - } - if let Some(message) = ipc.take_protocol_error() { - return Err(WorkerError::Protocol(message)); - } - Ok(()) - } - - fn on_input_written(&mut self, ipc: &ServerIpcConnection) -> Result<(), WorkerError> { - #[cfg(target_family = "unix")] - if self.python_request_generation.is_some() { - driver_announce_stdin_write_complete(ipc)?; - } - #[cfg(not(target_family = "unix"))] - let _ = ipc; - Ok(()) - } - - fn should_settle_output_after_timeout( - &self, - _oversized_output: OversizedOutputMode, - _pending_input: Option<&str>, - ) -> bool { - false - } - - fn wait_for_completion( - &mut self, - timeout: Duration, - ipc: ServerIpcConnection, - ) -> Result { - driver_wait_for_completion(timeout, ipc, OutputTextSource::Ipc) - } - - fn interrupt(&mut self, process: &mut WorkerProcess) -> Result<(), WorkerError> { - #[cfg(target_family = "unix")] - if let Some(request_generation) = self.python_request_generation { - if let Some(ipc) = process.ipc.get() { - ipc.send(ServerToWorkerIpcMessage::PythonInterrupt { request_generation }) - .map_err(WorkerError::Io)?; - driver_wait_for_python_interrupt_ack(&ipc, PYTHON_INTERRUPT_CLEANUP_TIMEOUT)?; - } - return process.send_interrupt(); - } - - driver_interrupt(process) - } - - fn refresh_backend_info( - &mut self, - ipc: ServerIpcConnection, - timeout: Duration, - ) -> Result<(), WorkerError> { - driver_refresh_worker_ready(ipc, timeout) - } -} - -impl std::fmt::Display for WorkerError { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - match self { - WorkerError::Io(err) => write!(f, "worker io error: {err}"), - WorkerError::Protocol(message) => write!(f, "worker protocol error: {message}"), - WorkerError::Timeout(duration) => write!( - f, - "worker response timed out after {} ms", - duration.as_millis() - ), - WorkerError::Sandbox(message) => write!(f, "worker sandbox error: {message}"), - WorkerError::Guardrail(message) => write!(f, "{message}"), - } - } -} - -impl std::error::Error for WorkerError { - fn source(&self) -> Option<&(dyn std::error::Error + 'static)> { - match self { - WorkerError::Io(err) => Some(err), - _ => None, - } - } -} - -const BACKEND_INFO_TIMEOUT: Duration = Duration::from_secs(2); -#[cfg(target_family = "unix")] -const PYTHON_INTERRUPT_CLEANUP_TIMEOUT: Duration = Duration::from_millis(500); +const WORKER_READY_TIMEOUT: Duration = Duration::from_secs(2); #[cfg(target_family = "windows")] const WINDOWS_IPC_CONNECT_MAX_WAIT: Duration = Duration::from_secs(10); const COMPLETION_METADATA_SETTLE_MAX: Duration = Duration::from_millis(30); @@ -1055,8 +673,6 @@ const OUTPUT_READER_STOP_DRAIN_GRACE: Duration = Duration::from_millis(50); fn collect_completion_metadata(ipc: &ServerIpcConnection) -> (Option, Vec) { let mut prompt = ipc.try_take_prompt(); let mut prompt_variants = ipc.take_prompt_history(); - let mut echo_event_count = ipc.pending_echo_event_count(); - let mut saw_late_echo_event = false; let start = std::time::Instant::now(); let mut stable_for = Duration::from_millis(0); @@ -1064,25 +680,18 @@ fn collect_completion_metadata(ipc: &ServerIpcConnection) -> (Option, Ve thread::sleep(COMPLETION_METADATA_SETTLE_POLL); let next_prompt = ipc.try_take_prompt(); let mut next_prompt_variants = ipc.take_prompt_history(); - let next_echo_event_count = ipc.pending_echo_event_count(); - if next_echo_event_count > echo_event_count { - saw_late_echo_event = true; - } - let changed = next_prompt.is_some() - || !next_prompt_variants.is_empty() - || next_echo_event_count != echo_event_count; + let changed = next_prompt.is_some() || !next_prompt_variants.is_empty(); if let Some(value) = next_prompt { prompt = Some(value); } prompt_variants.append(&mut next_prompt_variants); - echo_event_count = next_echo_event_count; if changed { stable_for = Duration::from_millis(0); } else { stable_for = stable_for.saturating_add(COMPLETION_METADATA_SETTLE_POLL); - if !saw_late_echo_event && stable_for >= COMPLETION_METADATA_STABLE { + if stable_for >= COMPLETION_METADATA_STABLE { break; } } @@ -1161,7 +770,7 @@ struct CompletionInfo { fn completion_info_from_ipc( ipc: &ServerIpcConnection, session_end_seen: bool, - echo_source: OutputTextSource, + _echo_source: OutputTextSource, ) -> CompletionInfo { let (prompt, prompt_variants) = if session_end_seen { (None, None) @@ -1170,15 +779,10 @@ fn completion_info_from_ipc( (prompt, Some(prompt_variants)) }; - let mut echo_events = ipc.take_echo_events(); - for event in &mut echo_events { - event.source = echo_source; - } - CompletionInfo { prompt, prompt_variants, - echo_events, + echo_events: Vec::new(), protocol_warnings: ipc.take_protocol_warnings(), session_end_seen, } @@ -1217,7 +821,7 @@ fn worker_context_event_payload( serde_json::json!({ "backend": format!("{backend:?}"), "worker_launch": worker_launch.label(), - "stdin_transport": worker_launch.stdin_transport().as_str(), + "stdin_transport": worker_launch_stdin_transport(worker_launch, sandbox_state).as_str(), "sandbox_policy": sandbox_policy, "sandbox_cwd": sandbox_state.sandbox_cwd.to_string_lossy().to_string(), "session_temp_dir": sandbox_state.session_temp_dir.to_string_lossy().to_string(), @@ -1230,6 +834,87 @@ fn worker_context_event_payload( }) } +fn worker_launch_stdin_transport( + worker_launch: &WorkerLaunch, + sandbox_state: &SandboxState, +) -> WorkerStdinTransport { + let default_transport = worker_launch.stdin_transport(); + #[cfg(target_family = "windows")] + if matches!(worker_launch, WorkerLaunch::Builtin(Backend::Python)) + && sandbox_state.sandbox_policy.requires_sandbox() + { + return WorkerStdinTransport::Pipe; + } + #[cfg(not(target_family = "windows"))] + let _ = sandbox_state; + default_transport +} + +fn builtin_worker_stdin_transport( + backend: Backend, + sandbox_state: &SandboxState, +) -> WorkerStdinTransport { + worker_launch_stdin_transport(&WorkerLaunch::Builtin(backend), sandbox_state) +} + +#[cfg(target_family = "windows")] +fn custom_worker_requests_wrapper_conpty(spec: &CustomWorkerSpec, windows_sandboxed: bool) -> bool { + windows_sandboxed && matches!(spec.stdin, crate::backend::CustomWorkerStdin::Pty) +} + +#[cfg(target_family = "windows")] +fn custom_worker_launch_stdin_transport( + spec: &CustomWorkerSpec, + custom_worker_wrapper_conpty: bool, +) -> WorkerStdinTransport { + if custom_worker_wrapper_conpty { + WorkerStdinTransport::Pipe + } else { + spec.stdin.transport() + } +} + +#[cfg(target_family = "windows")] +fn apply_windows_sandbox_conpty_env(command: &mut Command) { + command.env(crate::windows_sandbox::WINDOWS_SANDBOX_CONPTY_ENV, "1"); +} + +#[cfg(target_family = "windows")] +fn apply_windows_sandbox_conpty_env_to_pty(command: &mut WindowsPtyCommand) { + command.env(crate::windows_sandbox::WINDOWS_SANDBOX_CONPTY_ENV, "1"); +} + +fn backend_driver_for_launch( + worker_launch: &WorkerLaunch, + sandbox_state: &SandboxState, +) -> Box { + match worker_launch { + WorkerLaunch::Builtin(Backend::R) => Box::new(RBackendDriver::new()), + WorkerLaunch::Builtin(Backend::Python) => python_backend_driver(sandbox_state), + WorkerLaunch::Custom(spec) => protocol_backend_driver(spec), + } +} + +fn protocol_backend_driver(spec: &CustomWorkerSpec) -> Box { + Box::new(ProtocolBackendDriver::new( + spec.stdin.transport(), + ProtocolStdinAccounting::ExternalWorker, + )) +} + +fn python_backend_driver(sandbox_state: &SandboxState) -> Box { + let stdin_transport = builtin_worker_stdin_transport(Backend::Python, sandbox_state); + let stdin_accounting = if cfg!(target_family = "windows") { + ProtocolStdinAccounting::NormalizeNewlines + } else { + ProtocolStdinAccounting::Payload + }; + Box::new(ProtocolBackendDriver::python( + stdin_transport, + stdin_accounting, + )) +} + pub struct WorkerManager { exe_path: PathBuf, worker_launch: WorkerLaunch, @@ -1327,6 +1012,7 @@ impl WorkerManager { reset_last_reply_marker_offset(); OutputTimeline::new(output_ring) }; + let driver = backend_driver_for_launch(&worker_launch, &sandbox_state); Ok(Self { exe_path, worker_launch: worker_launch.clone(), @@ -1344,20 +1030,7 @@ impl WorkerManager { output: OutputBuffer::default(), pager: Pager::default(), output_timeline, - driver: match worker_launch { - WorkerLaunch::Builtin(Backend::R) => Box::new(RBackendDriver::new()), - WorkerLaunch::Builtin(Backend::Python) => { - #[cfg(target_family = "unix")] - { - Box::new(ProtocolBackendDriver::python()) - } - #[cfg(not(target_family = "unix"))] - { - Box::new(PythonBackendDriver::new()) - } - } - WorkerLaunch::Custom(_) => Box::new(ProtocolBackendDriver::new()), - }, + driver, pending_request: false, pending_request_started_at: None, pending_request_input: None, @@ -1455,7 +1128,6 @@ impl WorkerManager { sandbox_policy: self.sandbox_defaults.sandbox_policy.clone(), sandbox_cwd: Some(self.sandbox_defaults.sandbox_cwd.clone()), use_linux_sandbox_bwrap: Some(self.sandbox_defaults.use_linux_sandbox_bwrap), - use_legacy_landlock: None, }; crate::event_log::log( "worker_local_inherit_bootstrap", @@ -2562,10 +2234,11 @@ impl WorkerManager { if remaining.is_zero() { return Err(WorkerError::Timeout(server_timeout)); } + let write_payload = self.driver.prepare_stdin_write_payload(&payload); self.process .as_mut() .expect("worker process should be available") - .write_stdin_payload(payload, remaining)?; + .write_stdin_payload(write_payload, remaining)?; self.driver.on_input_written(&ipc)?; Ok(RequestState { timeout: worker_timeout, @@ -3074,8 +2747,7 @@ impl WorkerManager { while start.elapsed() < total { thread::sleep(poll); let now = self.pending_output_tape.current_settle_state(); - if !ready - && (now.has_image || now.readline_results_seen > baseline.readline_results_seen) + if !ready && (now.has_image || now.sideband_events_seen > baseline.sideband_events_seen) { ready = true; stable_for = Duration::from_millis(0); @@ -3695,7 +3367,7 @@ impl WorkerManager { fn reset(&mut self) -> Result<(), WorkerError> { crate::event_log::log("worker_reset_begin", serde_json::json!({})); if let Some(process) = self.process.take() { - let _ = process.kill(); + let _ = process.shutdown_graceful(WORKER_SHUTDOWN_TIMEOUT); } if self.missing_inherited_sandbox_state() { return Err(WorkerError::Sandbox( @@ -3722,7 +3394,7 @@ impl WorkerManager { }), ); if let Some(process) = self.process.take() { - let _ = process.kill(); + let _ = process.shutdown_graceful(WORKER_SHUTDOWN_TIMEOUT); } if self.missing_inherited_sandbox_state() { return Err(WorkerError::Sandbox( @@ -4200,6 +3872,7 @@ impl WorkerManager { self.ensure_managed_network_proxy()?; #[cfg(target_os = "windows")] let prepared_windows_launch = self.ensure_windows_sandbox_launch()?; + self.driver = backend_driver_for_launch(&self.worker_launch, &self.sandbox_state); let process = WorkerProcess::spawn( self.worker_launch.clone(), &self.exe_path, @@ -4218,7 +3891,7 @@ impl WorkerManager { .ipc .get() .ok_or_else(|| WorkerError::Protocol("worker ipc unavailable".to_string()))?; - if let Err(err) = self.driver.refresh_backend_info(ipc, BACKEND_INFO_TIMEOUT) { + if let Err(err) = self.driver.wait_worker_ready(ipc, WORKER_READY_TIMEOUT) { let _ = process.kill(); crate::event_log::log( "worker_spawn_error", @@ -4288,6 +3961,7 @@ impl WorkerManager { self.ensure_managed_network_proxy()?; #[cfg(target_os = "windows")] let prepared_windows_launch = self.ensure_windows_sandbox_launch()?; + self.driver = backend_driver_for_launch(&self.worker_launch, &self.sandbox_state); let process = WorkerProcess::spawn( self.worker_launch.clone(), &self.exe_path, @@ -4306,7 +3980,7 @@ impl WorkerManager { .ipc .get() .ok_or_else(|| WorkerError::Protocol("worker ipc unavailable".to_string()))?; - if let Err(err) = self.driver.refresh_backend_info(ipc, BACKEND_INFO_TIMEOUT) { + if let Err(err) = self.driver.wait_worker_ready(ipc, WORKER_READY_TIMEOUT) { let _ = process.kill(); crate::event_log::log( "worker_spawn_error", @@ -5656,14 +5330,16 @@ fn prefix_worker_text_bytes(contents: &[WorkerContent]) -> u64 { } struct WorkerProcess { - child: Child, + child: WorkerChild, stdin_tx: mpsc::Sender, session_tmpdir: Option, ipc: IpcHandle, stdout_reader: Option, stderr_reader: Option, + #[cfg(target_family = "windows")] + _pty_conpty: Option, expected_exit: bool, - exit_status: Option, + exit_status: Option, #[cfg(target_family = "unix")] guardrail_stop: Arc, #[cfg(target_family = "unix")] @@ -5685,11 +5361,13 @@ enum StdinCommand { } struct SpawnedWorker { - child: Child, + child: WorkerChild, stdin_tx: mpsc::Sender, session_tmpdir: Option, stdout_reader: Option, stderr_reader: Option, + #[cfg(target_family = "windows")] + pty_conpty: Option, #[cfg(target_os = "macos")] denial_logger: Option, } @@ -5698,18 +5376,177 @@ struct SpawnedWorkerStdio { stdin_tx: mpsc::Sender, stdout_reader: Option, stderr_reader: Option, + #[cfg(target_family = "windows")] + pty_conpty: Option, } struct SpawnedCommand { - child: Child, - #[cfg(target_family = "unix")] + child: WorkerChild, + #[cfg(any(target_family = "unix", target_family = "windows"))] pty_stdio: Option, } -#[cfg(target_family = "unix")] +#[cfg(target_family = "windows")] +enum WorkerChild { + Process(Child), + Pty(WindowsPtyChild), +} + +#[cfg(target_family = "windows")] +impl WorkerChild { + fn from_process(child: Child) -> Self { + Self::Process(child) + } + + fn from_pty(child: WindowsPtyChild) -> Self { + Self::Pty(child) + } + + fn as_process_mut(&mut self) -> Option<&mut Child> { + match self { + Self::Process(child) => Some(child), + Self::Pty(_) => None, + } + } + + fn try_wait(&mut self) -> std::io::Result> { + match self { + Self::Process(child) => portable_pty::Child::try_wait(child), + Self::Pty(child) => child.try_wait(), + } + } + + fn wait(&mut self) -> std::io::Result { + match self { + Self::Process(child) => portable_pty::Child::wait(child), + Self::Pty(child) => child.wait(), + } + } + + fn kill(&mut self) -> std::io::Result<()> { + match self { + Self::Process(child) => child.kill(), + Self::Pty(child) => child.kill(), + } + } + + fn process_id(&self) -> Option { + match self { + Self::Process(child) => Some(child.id()), + Self::Pty(child) => child.process_id(), + } + } +} + +#[cfg(target_family = "windows")] +struct WindowsPtyChild { + process: HANDLE, + process_id: u32, +} + +#[cfg(target_family = "windows")] +unsafe impl Send for WindowsPtyChild {} + +#[cfg(target_family = "windows")] +impl WindowsPtyChild { + fn try_wait(&mut self) -> std::io::Result> { + let mut status = 0u32; + let ok = unsafe { GetExitCodeProcess(self.process, &mut status) }; + if ok == 0 { + return Err(std::io::Error::last_os_error()); + } + if status == windows_sys::Win32::Foundation::STILL_ACTIVE as u32 { + let wait = unsafe { WaitForSingleObject(self.process, 0) }; + if wait == WAIT_FAILED { + return Err(std::io::Error::last_os_error()); + } + if wait == WAIT_TIMEOUT { + return Ok(None); + } + } + Ok(Some(WorkerExitStatus::with_exit_code(status))) + } + + fn wait(&mut self) -> std::io::Result { + loop { + match self.try_wait()? { + Some(status) => return Ok(status), + None => { + let wait = unsafe { WaitForSingleObject(self.process, INFINITE) }; + if wait == WAIT_FAILED { + return Err(std::io::Error::last_os_error()); + } + } + } + } + } + + fn kill(&mut self) -> std::io::Result<()> { + let ok = unsafe { TerminateProcess(self.process, 1) }; + if ok == 0 { + let err = std::io::Error::last_os_error(); + if self.try_wait()?.is_some() { + return Ok(()); + } + return Err(err); + } + Ok(()) + } + + fn process_id(&self) -> Option { + Some(self.process_id) + } +} + +#[cfg(target_family = "windows")] +impl Drop for WindowsPtyChild { + fn drop(&mut self) { + unsafe { + CloseHandle(self.process); + } + } +} + +#[cfg(not(target_family = "windows"))] +fn worker_child_from_process(child: Child) -> WorkerChild { + child +} + +#[cfg(target_family = "windows")] +fn worker_child_from_process(child: Child) -> WorkerChild { + WorkerChild::from_process(child) +} + +#[cfg(any(target_family = "unix", target_family = "windows"))] struct SpawnedPtyStdio { + #[cfg(target_family = "unix")] reader: File, + #[cfg(target_family = "windows")] + reader: Box, writer: Box, + #[cfg(target_family = "windows")] + _conpty: WindowsConPty, +} + +#[cfg(target_family = "windows")] +struct WindowsConPty { + hpc: HPCON, + input_read: HANDLE, + output_write: HANDLE, +} + +#[cfg(target_family = "windows")] +unsafe impl Send for WindowsConPty {} + +#[cfg(target_family = "windows")] +impl Drop for WindowsConPty { + fn drop(&mut self) { + unsafe { + ClosePseudoConsole(self.hpc); + CloseHandle(self.input_read); + CloseHandle(self.output_write); + } + } } struct WorkerSpawnContext<'a> { @@ -5780,13 +5617,14 @@ impl WorkerProcess { pending_output_tape.clone(), output_timeline.clone(), ); - let readline_echo_source = PendingTextSource::Ipc; let SpawnedWorker { child, stdin_tx, session_tmpdir, stdout_reader, stderr_reader, + #[cfg(target_family = "windows")] + pty_conpty, #[cfg(target_os = "macos")] denial_logger, } = match &worker_launch { @@ -5837,22 +5675,12 @@ impl WorkerProcess { text.is_continuation, ); })), - on_plot_image: Some(Arc::new(move |image: IpcPlotImage| { + on_output_image: Some(Arc::new(move |image: IpcOutputImage| { image_capture.append_image(image); })), on_readline_start: Some(Arc::new(move |prompt: String| { sideband_capture.append_sideband(PendingSidebandKind::ReadlineStart { prompt }); })), - on_readline_result: { - let sideband_capture = live_output.clone(); - Some(Arc::new(move |event: IpcEchoEvent| { - sideband_capture.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: event.prompt, - line: event.line, - echo_source: readline_echo_source, - }); - })) - }, on_session_end: { let sideband_capture = live_output.clone(); Some(Arc::new(move || { @@ -5865,15 +5693,15 @@ impl WorkerProcess { .connect(ipc.clone(), handlers) .map_err(WorkerError::Io)?; #[cfg(target_family = "windows")] - handle_windows_ipc_connect_result( - ipc_server.connect( + { + let connect_result = ipc_server.connect( ipc.clone(), handlers, - &mut child, + || child.try_wait().map(|status| status.is_some()), WINDOWS_IPC_CONNECT_MAX_WAIT, - ), - &mut child, - )?; + ); + handle_windows_ipc_connect_result(connect_result, &mut child)?; + } } #[cfg(target_family = "unix")] @@ -5887,6 +5715,8 @@ impl WorkerProcess { ipc, stdout_reader, stderr_reader, + #[cfg(target_family = "windows")] + _pty_conpty: pty_conpty, expected_exit: false, exit_status: None, #[cfg(target_family = "unix")] @@ -5956,6 +5786,38 @@ impl WorkerProcess { python_executable, ); } + #[cfg(target_os = "windows")] + if matches!(backend, Backend::Python) && prepared_windows_launch.is_some() { + command.env(crate::windows_sandbox::WINDOWS_SANDBOX_CONPTY_ENV, "1"); + } + #[cfg(target_family = "windows")] + let mut pty_command = { + let mut builder = WindowsPtyCommand::new(&prepared.program); + builder.args(&prepared.args); + for (key, value) in prepared.env.iter() { + builder.env(key, value); + } + builder.env( + crate::backend::INTERPRETER_ENV, + match backend { + Backend::R => "r", + Backend::Python => "python", + }, + ); + if matches!(backend, Backend::Python) + && let Some(python_executable) = + std::env::var_os(crate::python_session::PYTHON_EXECUTABLE_ENV) + { + builder.env( + crate::python_session::PYTHON_EXECUTABLE_ENV, + python_executable, + ); + } + if matches!(backend, Backend::Python) && prepared_windows_launch.is_some() { + builder.env(crate::windows_sandbox::WINDOWS_SANDBOX_CONPTY_ENV, "1"); + } + builder + }; #[cfg(target_family = "unix")] let client_fds = ipc_server.take_child_fds().ok_or_else(|| { WorkerError::Protocol("IPC pipe setup failed; no client fds available".to_string()) @@ -5971,18 +5833,24 @@ impl WorkerProcess { })?; #[cfg(target_family = "windows")] { - command.env(IPC_PIPE_TO_WORKER_ENV, pipe_to_worker); - command.env(IPC_PIPE_FROM_WORKER_ENV, pipe_from_worker); + command.env(IPC_PIPE_TO_WORKER_ENV, &pipe_to_worker); + command.env(IPC_PIPE_FROM_WORKER_ENV, &pipe_from_worker); + pty_command.env(IPC_PIPE_TO_WORKER_ENV, &pipe_to_worker); + pty_command.env(IPC_PIPE_FROM_WORKER_ENV, &pipe_from_worker); command.creation_flags(CREATE_NEW_PROCESS_GROUP); } apply_debug_startup_env(&mut command, session_tmpdir.as_ref()); - let stdin_transport = WorkerLaunch::Builtin(backend).stdin_transport(); + #[cfg(target_family = "windows")] + apply_debug_startup_env_to_pty(&mut pty_command, session_tmpdir.as_ref()); + let stdin_transport = builtin_worker_stdin_transport(backend, sandbox_state); #[cfg(target_family = "unix")] configure_command_process_group(&mut command, stdin_transport); let child_result = spawn_command_with_transport( &mut command, stdin_transport, !matches!(backend, Backend::Python), + #[cfg(target_family = "windows")] + Some(pty_command), ); #[cfg(target_family = "unix")] { @@ -5993,11 +5861,11 @@ impl WorkerProcess { } let SpawnedCommand { mut child, - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", target_family = "windows"))] pty_stdio, } = child_result?; if let Some(status) = child.try_wait()? { - maybe_report_sandbox_exec_failure(&prepared.program, status)?; + maybe_report_sandbox_exec_failure(&prepared.program, &status)?; return Err(WorkerError::Protocol(format!( "worker process exited immediately with status {status}" ))); @@ -6007,10 +5875,12 @@ impl WorkerProcess { stdin_tx, stdout_reader, stderr_reader, + #[cfg(target_family = "windows")] + pty_conpty, } = attach_spawned_worker_stdio( &mut child, stdin_transport, - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", target_family = "windows"))] pty_stdio, live_output.clone(), )?; @@ -6028,6 +5898,8 @@ impl WorkerProcess { session_tmpdir, stdout_reader, stderr_reader, + #[cfg(target_family = "windows")] + pty_conpty, #[cfg(target_os = "macos")] denial_logger, }) @@ -6073,12 +5945,37 @@ impl WorkerProcess { command.args(&prepared.args); command.envs(spec.env.iter()); command.envs(prepared.env.iter()); + #[cfg(target_family = "windows")] + let custom_worker_wrapper_conpty = + custom_worker_requests_wrapper_conpty(spec, prepared_windows_launch.is_some()); + #[cfg(target_family = "windows")] + if custom_worker_wrapper_conpty { + apply_windows_sandbox_conpty_env(&mut command); + } match &spec.working_dir { CustomWorkerWorkingDir::Policy(CustomWorkerWorkingDirPolicy::Inherit) => {} CustomWorkerWorkingDir::Path { path } => { command.current_dir(path); } } + #[cfg(target_family = "windows")] + let mut pty_command = { + let mut builder = WindowsPtyCommand::new(&prepared.program); + builder.args(&prepared.args); + for (key, value) in spec.env.iter() { + builder.env(key, value); + } + for (key, value) in prepared.env.iter() { + builder.env(key, value); + } + if custom_worker_wrapper_conpty { + apply_windows_sandbox_conpty_env_to_pty(&mut builder); + } + if let CustomWorkerWorkingDir::Path { path } = &spec.working_dir { + builder.cwd(path); + } + builder + }; #[cfg(target_family = "unix")] let client_fds = ipc_server.take_child_fds().ok_or_else(|| { WorkerError::Protocol("IPC pipe setup failed; no client fds available".to_string()) @@ -6094,15 +5991,29 @@ impl WorkerProcess { })?; #[cfg(target_family = "windows")] { - command.env(IPC_PIPE_TO_WORKER_ENV, pipe_to_worker); - command.env(IPC_PIPE_FROM_WORKER_ENV, pipe_from_worker); + command.env(IPC_PIPE_TO_WORKER_ENV, &pipe_to_worker); + command.env(IPC_PIPE_FROM_WORKER_ENV, &pipe_from_worker); + pty_command.env(IPC_PIPE_TO_WORKER_ENV, &pipe_to_worker); + pty_command.env(IPC_PIPE_FROM_WORKER_ENV, &pipe_from_worker); command.creation_flags(CREATE_NEW_PROCESS_GROUP); } apply_debug_startup_env(&mut command, session_tmpdir.as_ref()); + #[cfg(target_family = "windows")] + apply_debug_startup_env_to_pty(&mut pty_command, session_tmpdir.as_ref()); + #[cfg(target_family = "windows")] + let stdin_transport = + custom_worker_launch_stdin_transport(spec, custom_worker_wrapper_conpty); + #[cfg(not(target_family = "windows"))] let stdin_transport = spec.stdin.transport(); #[cfg(target_family = "unix")] configure_command_process_group(&mut command, stdin_transport); - let child_result = spawn_command_with_transport(&mut command, stdin_transport, true); + let child_result = spawn_command_with_transport( + &mut command, + stdin_transport, + true, + #[cfg(target_family = "windows")] + Some(pty_command), + ); #[cfg(target_family = "unix")] { unsafe { @@ -6112,11 +6023,11 @@ impl WorkerProcess { } let SpawnedCommand { mut child, - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", target_family = "windows"))] pty_stdio, } = child_result?; if let Some(status) = child.try_wait()? { - maybe_report_sandbox_exec_failure(&prepared.program, status)?; + maybe_report_sandbox_exec_failure(&prepared.program, &status)?; return Err(WorkerError::Protocol(format!( "worker process exited immediately with status {status}" ))); @@ -6126,10 +6037,12 @@ impl WorkerProcess { stdin_tx, stdout_reader, stderr_reader, + #[cfg(target_family = "windows")] + pty_conpty, } = attach_spawned_worker_stdio( &mut child, stdin_transport, - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", target_family = "windows"))] pty_stdio, live_output.clone(), )?; @@ -6147,6 +6060,8 @@ impl WorkerProcess { session_tmpdir, stdout_reader, stderr_reader, + #[cfg(target_family = "windows")] + pty_conpty, #[cfg(target_os = "macos")] denial_logger, }) @@ -6198,18 +6113,27 @@ impl WorkerProcess { { self.send_signal(libc::SIGINT) } - #[cfg(not(target_family = "unix"))] + #[cfg(target_family = "windows")] + { + self.send_windows_ctrl_break() + } + #[cfg(not(any(target_family = "unix", target_family = "windows")))] { Ok(()) } } #[cfg(target_family = "windows")] - fn send_r_interrupt(&mut self) -> Result<(), WorkerError> { + fn send_windows_ctrl_break(&mut self) -> Result<(), WorkerError> { if self.child.try_wait()?.is_some() { return Ok(()); } - let ok = unsafe { GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, self.child.id()) }; + let Some(process_id) = self.child.process_id() else { + return Err(WorkerError::Protocol( + "worker process id unavailable for interrupt".to_string(), + )); + }; + let ok = raw_windows_generate_console_ctrl_event(CTRL_BREAK_EVENT, process_id); if ok != 0 { return Ok(()); } @@ -6220,6 +6144,11 @@ impl WorkerProcess { } } + #[cfg(target_family = "windows")] + fn send_r_interrupt(&mut self) -> Result<(), WorkerError> { + self.send_windows_ctrl_break() + } + #[cfg(not(target_family = "windows"))] fn send_r_interrupt(&mut self) -> Result<(), WorkerError> { self.send_interrupt() @@ -6311,7 +6240,6 @@ impl WorkerProcess { fn is_running(&mut self) -> Result { if let Some(status) = self.child.try_wait()? { - self.exit_status = Some(status); let should_log = !status.success() && !self.expected_exit; if should_log { #[cfg(target_family = "unix")] @@ -6323,6 +6251,7 @@ impl WorkerProcess { #[cfg(not(target_family = "unix"))] eprintln!("worker exited with status {status}"); } + self.exit_status = Some(status); return Ok(false); } Ok(true) @@ -6415,6 +6344,10 @@ impl WorkerProcess { // sideband fds. Backend startup strips the bootstrap env vars, marks the fds // close-on-exec, and closes them again in forked children, so EOF should track the root // worker lifetime. + #[cfg(target_family = "windows")] + { + let _ = self._pty_conpty.take(); + } if let Some(reader) = self.stdout_reader.take() { reader.stop_and_join("worker stdout reader thread panicked")?; } @@ -6648,9 +6581,191 @@ fn apply_debug_startup_env(command: &mut Command, session_tmpdir: Option<&PathBu } } +#[cfg(target_family = "windows")] +#[derive(Clone)] +struct WindowsPtyCommand { + program: PathBuf, + args: Vec, + env: Vec<(OsString, OsString)>, + cwd: Option, +} + +#[cfg(target_family = "windows")] +impl WindowsPtyCommand { + fn new(program: &Path) -> Self { + Self { + program: program.to_path_buf(), + args: Vec::new(), + env: Vec::new(), + cwd: None, + } + } + + fn args(&mut self, args: &[String]) { + self.args.extend(args.iter().cloned()); + } + + fn env(&mut self, key: K, value: V) + where + K: AsRef, + V: AsRef, + { + self.env + .push((key.as_ref().to_os_string(), value.as_ref().to_os_string())); + } + + fn cwd(&mut self, path: &Path) { + self.cwd = Some(path.to_path_buf()); + } + + fn command_line_wide(&self) -> Vec { + let mut command_line = Vec::new(); + append_windows_quoted_arg(self.program.as_os_str(), &mut command_line); + for arg in &self.args { + command_line.push(' ' as u16); + append_windows_quoted_arg(OsStr::new(arg), &mut command_line); + } + command_line.push(0); + command_line + } + + fn environment_block_wide(&self) -> Vec { + let mut entries = std::collections::BTreeMap::::new(); + for (key, value) in current_windows_environment() { + entries.insert(windows_env_key(&key), (key, value)); + } + for (key, value) in &self.env { + entries.insert(windows_env_key(key), (key.clone(), value.clone())); + } + + let mut block = Vec::new(); + for (_normalized, (key, value)) in entries { + block.extend(key.encode_wide()); + block.push('=' as u16); + block.extend(value.encode_wide()); + block.push(0); + } + block.push(0); + block + } + + fn cwd_wide(&self) -> Option> { + self.cwd.as_ref().map(|path| { + let mut wide = path.as_os_str().encode_wide().collect::>(); + wide.push(0); + wide + }) + } +} + +#[cfg(target_family = "windows")] +fn current_windows_environment() -> Vec<(OsString, OsString)> { + let block = unsafe { GetEnvironmentStringsW() }; + if block.is_null() { + return std::env::vars_os().collect(); + } + + let mut entries = Vec::new(); + let mut offset = 0usize; + loop { + let mut len = 0usize; + while unsafe { *block.add(offset + len) } != 0 { + len += 1; + } + if len == 0 { + break; + } + let slice = unsafe { std::slice::from_raw_parts(block.add(offset), len) }; + if let Some(eq) = environment_entry_separator(slice) { + let key = OsString::from_wide(&slice[..eq]); + let value = OsString::from_wide(&slice[eq + 1..]); + entries.push((key, value)); + } + offset += len + 1; + } + unsafe { + FreeEnvironmentStringsW(block); + } + entries +} + +#[cfg(target_family = "windows")] +fn environment_entry_separator(entry: &[u16]) -> Option { + let start = usize::from(entry.first() == Some(&('=' as u16))); + entry + .iter() + .enumerate() + .skip(start) + .find_map(|(index, ch)| (*ch == '=' as u16).then_some(index)) +} + +#[cfg(target_family = "windows")] +fn windows_env_key(key: &OsStr) -> String { + key.to_string_lossy().to_ascii_lowercase() +} + +#[cfg(target_family = "windows")] +fn append_windows_quoted_arg(arg: &OsStr, command_line: &mut Vec) { + let wide = arg.encode_wide().collect::>(); + if !wide.is_empty() + && !wide.iter().any(|ch| { + *ch == b' ' as u16 + || *ch == b'\t' as u16 + || *ch == b'\n' as u16 + || *ch == 0x0b + || *ch == b'"' as u16 + }) + { + command_line.extend(wide); + return; + } + + command_line.push('"' as u16); + let mut index = 0; + while index < wide.len() { + let mut backslashes = 0; + while index < wide.len() && wide[index] == b'\\' as u16 { + index += 1; + backslashes += 1; + } + + if index == wide.len() { + command_line.extend(std::iter::repeat_n(b'\\' as u16, backslashes * 2)); + break; + } + if wide[index] == b'"' as u16 { + command_line.extend(std::iter::repeat_n(b'\\' as u16, backslashes * 2 + 1)); + } else { + command_line.extend(std::iter::repeat_n(b'\\' as u16, backslashes)); + } + command_line.push(wide[index]); + index += 1; + } + command_line.push('"' as u16); +} + +#[cfg(target_family = "windows")] +fn apply_debug_startup_env_to_pty( + command: &mut WindowsPtyCommand, + session_tmpdir: Option<&PathBuf>, +) { + if let Some(debug_session_dir) = + crate::debug_logs::log_path(crate::diagnostics::WORKER_STARTUP_LOG_FILE_NAME) + .and_then(|path| path.parent().map(Path::to_path_buf)) + { + command.env(crate::debug_logs::DEBUG_SESSION_DIR_ENV, debug_session_dir); + } + if let Some(tmpdir) = session_tmpdir { + command.env( + crate::diagnostics::STARTUP_LOG_PATH_ENV, + tmpdir.join(crate::diagnostics::WORKER_STARTUP_LOG_FILE_NAME), + ); + } +} + fn maybe_report_sandbox_exec_failure( _program: &Path, - _status: std::process::ExitStatus, + _status: &WorkerExitStatus, ) -> Result<(), WorkerError> { #[cfg(target_os = "macos")] { @@ -6672,8 +6787,8 @@ fn maybe_report_sandbox_exec_failure( fn linux_sandbox_startup_retryable(err: &WorkerError) -> bool { match err { WorkerError::Protocol(message) => { - message.contains("ipc disconnected while waiting for backend info") - || message.contains("worker session ended before backend info") + message.contains("ipc disconnected while waiting for worker_ready") + || message.contains("worker session ended before worker_ready") || message.contains("worker process exited immediately") } _ => false, @@ -6867,32 +6982,80 @@ where })) } -fn spawn_command_with_transport( - command: &mut Command, - stdin_transport: WorkerStdinTransport, - pty_echo: bool, -) -> Result { - match stdin_transport { - WorkerStdinTransport::Pipe => { - let child = command - .stdin(Stdio::piped()) - .stdout(Stdio::piped()) - .stderr(Stdio::piped()) - .spawn()?; - Ok(SpawnedCommand { - child, - #[cfg(target_family = "unix")] - pty_stdio: None, - }) - } - WorkerStdinTransport::Pty => spawn_command_with_pty(command, pty_echo), - } -} - -#[cfg(target_family = "unix")] +#[cfg(target_family = "windows")] +fn spawn_blocking_output_reader( + stream: Option, + output_stream: TextStream, + live_output: LiveOutputCapture, +) -> Result, WorkerError> +where + R: Read + Send + 'static, +{ + let Some(mut stream) = stream else { + return Ok(None); + }; + let (done_tx, done_rx) = mpsc::channel(); + let stop_requested = Arc::new(AtomicBool::new(false)); + let handle = thread::spawn(move || { + let mut buffer = [0u8; 8192]; + let mut filter = WindowsPtyOutputFilter::default(); + loop { + match stream.read(&mut buffer) { + Ok(0) => break, + Ok(n) => { + let filtered = filter.filter(&buffer[..n]); + if !filtered.is_empty() { + live_output.append_raw_text(&filtered, output_stream); + } + } + Err(err) if err.kind() == std::io::ErrorKind::Interrupted => continue, + Err(_) => break, + } + } + let _ = done_tx.send(()); + }); + Ok(Some(OutputReader { + handle, + done_rx, + stop_requested, + })) +} + +fn spawn_command_with_transport( + command: &mut Command, + stdin_transport: WorkerStdinTransport, + pty_echo: bool, + #[cfg(target_family = "windows")] pty_command: Option, +) -> Result { + match stdin_transport { + WorkerStdinTransport::Pipe => { + #[cfg(target_family = "windows")] + let _ = pty_command; + let child = command + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()) + .spawn()?; + Ok(SpawnedCommand { + child: worker_child_from_process(child), + #[cfg(any(target_family = "unix", target_family = "windows"))] + pty_stdio: None, + }) + } + WorkerStdinTransport::Pty => spawn_command_with_pty( + command, + pty_echo, + #[cfg(target_family = "windows")] + pty_command, + ), + } +} + +#[cfg(target_family = "unix")] fn spawn_command_with_pty( command: &mut Command, echo: bool, + #[cfg(target_family = "windows")] _pty_command: Option, ) -> Result { let pty_system = native_pty_system(); let pair = pty_system @@ -6950,7 +7113,32 @@ fn spawn_command_with_pty( }) } -#[cfg(not(target_family = "unix"))] +#[cfg(target_family = "windows")] +fn spawn_command_with_pty( + _command: &mut Command, + _echo: bool, + pty_command: Option, +) -> Result { + let pty_command = pty_command + .ok_or_else(|| WorkerError::Protocol("worker PTY command unavailable".to_string()))?; + let WindowsPtySpawn { + child, + reader, + writer, + conpty, + } = spawn_windows_pty_command(&pty_command)?; + + Ok(SpawnedCommand { + child: WorkerChild::from_pty(child), + pty_stdio: Some(SpawnedPtyStdio { + reader, + writer, + _conpty: conpty, + }), + }) +} + +#[cfg(not(any(target_family = "unix", target_family = "windows")))] fn spawn_command_with_pty( _command: &mut Command, _echo: bool, @@ -6989,16 +7177,189 @@ fn configure_pty_slave_echo(fd: libc::c_int, enabled: bool) -> Result<(), Worker Ok(()) } +#[cfg(target_family = "windows")] +struct WindowsPtySpawn { + child: WindowsPtyChild, + reader: Box, + writer: Box, + conpty: WindowsConPty, +} + +#[cfg(target_family = "windows")] +fn spawn_windows_pty_command(command: &WindowsPtyCommand) -> Result { + let (input_read, input_write) = create_windows_pipe("PTY input")?; + let (output_read, output_write) = create_windows_pipe("PTY output")?; + let mut hpc: HPCON = 0; + let hr = unsafe { + CreatePseudoConsole( + COORD { X: 4096, Y: 24 }, + input_read, + output_write, + 0, + &mut hpc, + ) + }; + if hr != 0 { + unsafe { + CloseHandle(input_read); + CloseHandle(input_write); + CloseHandle(output_read); + CloseHandle(output_write); + } + return Err(WorkerError::Protocol(format!( + "failed to create worker PTY: HRESULT {hr}" + ))); + } + + let conpty = WindowsConPty { + hpc, + input_read, + output_write, + }; + let spawn_result = spawn_windows_pty_process(command, hpc); + match spawn_result { + Ok(child) => { + let reader = unsafe { std::fs::File::from_raw_handle(output_read as _) }; + let writer = unsafe { std::fs::File::from_raw_handle(input_write as _) }; + Ok(WindowsPtySpawn { + child, + reader: Box::new(reader), + writer: Box::new(writer), + conpty, + }) + } + Err(err) => { + drop(conpty); + unsafe { + CloseHandle(input_write); + CloseHandle(output_read); + } + Err(err) + } + } +} + +#[cfg(target_family = "windows")] +fn create_windows_pipe(label: &str) -> Result<(HANDLE, HANDLE), WorkerError> { + let mut read = std::ptr::null_mut(); + let mut write = std::ptr::null_mut(); + let ok = unsafe { CreatePipe(&mut read, &mut write, std::ptr::null(), 0) }; + if ok == 0 { + return Err(WorkerError::Io(std::io::Error::new( + std::io::Error::last_os_error().kind(), + format!( + "CreatePipe {label} failed: {}", + std::io::Error::last_os_error() + ), + ))); + } + Ok((read, write)) +} + +#[cfg(target_family = "windows")] +fn spawn_windows_pty_process( + command: &WindowsPtyCommand, + hpc: HPCON, +) -> Result { + let mut startup_info = STARTUPINFOEXW::default(); + startup_info.StartupInfo.cb = std::mem::size_of::() as u32; + startup_info.StartupInfo.dwFlags = STARTF_USESTDHANDLES; + startup_info.StartupInfo.hStdInput = INVALID_HANDLE_VALUE; + startup_info.StartupInfo.hStdOutput = INVALID_HANDLE_VALUE; + startup_info.StartupInfo.hStdError = INVALID_HANDLE_VALUE; + + let mut attribute_list_size = 0usize; + unsafe { + InitializeProcThreadAttributeList(std::ptr::null_mut(), 1, 0, &mut attribute_list_size); + } + let mut attribute_list = vec![0u8; attribute_list_size]; + let attribute_list_ptr = attribute_list.as_mut_ptr().cast(); + let ok = unsafe { + InitializeProcThreadAttributeList(attribute_list_ptr, 1, 0, &mut attribute_list_size) + }; + if ok == 0 { + return Err(WorkerError::Io(std::io::Error::last_os_error())); + } + startup_info.lpAttributeList = attribute_list_ptr; + + let update_ok = unsafe { + UpdateProcThreadAttribute( + startup_info.lpAttributeList, + 0, + PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE as usize, + hpc as *const std::ffi::c_void, + std::mem::size_of::(), + std::ptr::null_mut(), + std::ptr::null(), + ) + }; + if update_ok == 0 { + unsafe { + DeleteProcThreadAttributeList(startup_info.lpAttributeList); + } + return Err(WorkerError::Io(std::io::Error::last_os_error())); + } + + let mut command_line = command.command_line_wide(); + let environment = command.environment_block_wide(); + let cwd = command.cwd_wide(); + let mut process_info = PROCESS_INFORMATION::default(); + let ok = unsafe { + CreateProcessW( + std::ptr::null(), + command_line.as_mut_ptr(), + std::ptr::null(), + std::ptr::null(), + 0, + windows_pty_creation_flags(), + environment.as_ptr().cast(), + cwd.as_ref() + .map(|wide| wide.as_ptr()) + .unwrap_or(std::ptr::null()), + &startup_info.StartupInfo, + &mut process_info, + ) + }; + unsafe { + DeleteProcThreadAttributeList(startup_info.lpAttributeList); + } + if ok == 0 { + return Err(WorkerError::Protocol(format!( + "failed to spawn worker PTY child: {}", + std::io::Error::last_os_error() + ))); + } + + unsafe { + CloseHandle(process_info.hThread); + } + Ok(WindowsPtyChild { + process: process_info.hProcess, + process_id: process_info.dwProcessId, + }) +} + +#[cfg(target_family = "windows")] +fn windows_pty_creation_flags() -> u32 { + EXTENDED_STARTUPINFO_PRESENT | CREATE_UNICODE_ENVIRONMENT | CREATE_NEW_PROCESS_GROUP +} + fn attach_spawned_worker_stdio( - child: &mut Child, + child: &mut WorkerChild, stdin_transport: WorkerStdinTransport, - #[cfg(target_family = "unix")] pty_stdio: Option, + #[cfg(any(target_family = "unix", target_family = "windows"))] pty_stdio: Option< + SpawnedPtyStdio, + >, live_output: LiveOutputCapture, ) -> Result { match stdin_transport { WorkerStdinTransport::Pipe => { - #[cfg(target_family = "unix")] + #[cfg(any(target_family = "unix", target_family = "windows"))] let _ = pty_stdio; + #[cfg(target_family = "windows")] + let child = child.as_process_mut().ok_or_else(|| { + WorkerError::Protocol("pipe worker process stdio unavailable".to_string()) + })?; let stdin = child .stdin .take() @@ -7012,6 +7373,8 @@ fn attach_spawned_worker_stdio( stdin_tx, stdout_reader, stderr_reader, + #[cfg(target_family = "windows")] + pty_conpty: None, }) } WorkerStdinTransport::Pty => { @@ -7027,9 +7390,32 @@ fn attach_spawned_worker_stdio( stdin_tx, stdout_reader, stderr_reader: None, + #[cfg(target_family = "windows")] + pty_conpty: None, + }) + } + #[cfg(target_family = "windows")] + { + let _ = child; + let pty_stdio = pty_stdio.ok_or_else(|| { + WorkerError::Protocol("worker PTY stdio unavailable".to_string()) + })?; + let SpawnedPtyStdio { + reader, + writer, + _conpty, + } = pty_stdio; + let stdin_tx = spawn_windows_pty_stdin_writer(writer); + let stdout_reader = + spawn_blocking_output_reader(Some(reader), TextStream::Stdout, live_output)?; + Ok(SpawnedWorkerStdio { + stdin_tx, + stdout_reader, + stderr_reader: None, + pty_conpty: Some(_conpty), }) } - #[cfg(not(target_family = "unix"))] + #[cfg(not(any(target_family = "unix", target_family = "windows")))] { let _ = child; let _ = live_output; @@ -7068,6 +7454,90 @@ where tx } +#[cfg(target_family = "windows")] +fn spawn_windows_pty_stdin_writer(stdin: W) -> mpsc::Sender +where + W: Write + Send + 'static, +{ + let (tx, rx) = mpsc::channel::(); + thread::spawn(move || { + let mut writer = std::io::BufWriter::new(stdin); + for command in rx { + match command { + StdinCommand::Write { payload, reply } => { + let result = writer + .write_all(&payload) + .and_then(|_| writer.flush()) + .map_err(WorkerError::Io); + let _ = reply.send(result); + } + StdinCommand::Close { reply } => { + let result = writer.flush().map_err(WorkerError::Io); + let _ = reply.send(result); + break; + } + } + } + }); + tx +} + +#[cfg(target_family = "windows")] +fn windows_pty_input_payload(payload: &[u8]) -> Vec { + let mut translated = Vec::with_capacity(payload.len()); + let mut index = 0; + while index < payload.len() { + match payload[index] { + b'\r' => { + translated.push(b'\r'); + if payload.get(index + 1) == Some(&b'\n') { + index += 2; + } else { + index += 1; + } + } + b'\n' => { + translated.push(b'\r'); + index += 1; + } + byte => { + translated.push(byte); + index += 1; + } + } + } + translated +} + +#[cfg(target_family = "windows")] +fn windows_pty_accounting_payload(payload: &[u8]) -> Vec { + let mut translated = Vec::with_capacity(payload.len().saturating_mul(2)); + let mut index = 0; + while index < payload.len() { + match payload[index] { + b'\r' => { + translated.push(b'\r'); + translated.push(b'\n'); + if payload.get(index + 1) == Some(&b'\n') { + index += 2; + } else { + index += 1; + } + } + b'\n' => { + translated.push(b'\r'); + translated.push(b'\n'); + index += 1; + } + byte => { + translated.push(byte); + index += 1; + } + } + } + translated +} + fn duration_to_millis(duration: Duration) -> u64 { let millis = duration.as_millis(); if millis > u64::MAX as u128 { @@ -7099,13 +7569,19 @@ fn shutdown_term_delay(timeout: Duration) -> Duration { #[cfg(target_family = "windows")] fn handle_windows_ipc_connect_result( connect_result: Result<(), std::io::Error>, - child: &mut Child, + child: &mut WorkerChild, ) -> Result<(), WorkerError> { match connect_result { Ok(()) => Ok(()), // The child here is the sandbox wrapper process. Give it a short grace // period to unwind ACL changes before forcing termination/reap. Err(err) => { + if let Some(status) = child.try_wait()? { + return Err(WorkerError::Io(std::io::Error::new( + err.kind(), + format!("{err}; worker exited with status {status}"), + ))); + } const WRAPPER_EXIT_GRACE: Duration = Duration::from_secs(2); let deadline = std::time::Instant::now() + WRAPPER_EXIT_GRACE; loop { @@ -7132,7 +7608,7 @@ fn handle_windows_ipc_connect_result( } #[cfg(target_family = "windows")] -fn request_soft_termination(_child: &mut Child) -> Result<(), WorkerError> { +fn request_soft_termination(_child: &mut WorkerChild) -> Result<(), WorkerError> { // The Windows child is the sandbox wrapper. Let it exit naturally so it can // roll back temporary ACL state before process teardown. Ok(()) @@ -7159,11 +7635,16 @@ fn set_command_arg0(command: &mut Command, arg0: &str) { #[cfg(not(target_family = "unix"))] fn set_command_arg0(_command: &mut Command, _arg0: &str) {} -fn format_exit_status_message(status: &std::process::ExitStatus) -> String { +fn format_exit_status_message(status: &WorkerExitStatus) -> String { #[cfg(target_family = "unix")] if let Some(signal) = std::os::unix::process::ExitStatusExt::signal(status) { return format!("[repl] worker exited with signal {signal}"); } + #[cfg(target_family = "windows")] + { + format!("[repl] worker exited with status {}", status.exit_code()) + } + #[cfg(not(target_family = "windows"))] match status.code() { Some(code) => format!("[repl] worker exited with status {code}"), None => "[repl] worker exited with unknown status".to_string(), @@ -7229,6 +7710,47 @@ mod tests { ); } + #[cfg(target_family = "windows")] + #[test] + fn windows_sandboxed_python_reports_wrapper_pipe_transport() { + let sandbox_state = SandboxState { + sandbox_policy: SandboxPolicy::ReadOnly, + ..SandboxState::default() + }; + + assert_eq!( + builtin_worker_stdin_transport(Backend::Python, &sandbox_state), + WorkerStdinTransport::Pipe + ); + assert!( + matches!( + worker_context_event_payload( + &WorkerLaunch::Builtin(Backend::Python), + Backend::Python, + &sandbox_state + ) + .get("stdin_transport") + .and_then(serde_json::Value::as_str), + Some("pipe") + ), + "sandboxed Windows Python uses pipe transport to the wrapper; the wrapper owns ConPTY for the restricted child" + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_unsandboxed_python_uses_pty_stdin_transport() { + let sandbox_state = SandboxState { + sandbox_policy: SandboxPolicy::DangerFullAccess, + ..SandboxState::default() + }; + + assert_eq!( + builtin_worker_stdin_transport(Backend::Python, &sandbox_state), + WorkerStdinTransport::Pty + ); + } + fn echo_event(prompt: &str, line: &str) -> IpcEchoEvent { IpcEchoEvent { prompt: prompt.to_string(), @@ -7237,6 +7759,12 @@ mod tests { } } + fn readline_input_bytes(bytes: &[u8]) -> WorkerToServerIpcMessage { + WorkerToServerIpcMessage::ReadlineInputBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode(bytes), + } + } + fn contents_text(contents: &[WorkerContent]) -> String { contents .iter() @@ -7314,57 +7842,337 @@ mod tests { .expect("collect failing exit status") } - #[cfg(target_family = "unix")] - fn test_worker_process(child: Child) -> WorkerProcess { - let (stdin_tx, _stdin_rx) = mpsc::channel(); - WorkerProcess { - child, - stdin_tx, - session_tmpdir: None, - ipc: IpcHandle::new(), - stdout_reader: None, - stderr_reader: None, - expected_exit: false, - exit_status: None, - guardrail_stop: Arc::new(AtomicBool::new(false)), - guardrail_thread: None, - guardrail_thread_handle: None, - #[cfg(target_os = "macos")] - denial_logger: None, - } + #[cfg(target_family = "unix")] + fn test_worker_process(child: Child) -> WorkerProcess { + let (stdin_tx, _stdin_rx) = mpsc::channel(); + WorkerProcess { + child: worker_child_from_process(child), + stdin_tx, + session_tmpdir: None, + ipc: IpcHandle::new(), + stdout_reader: None, + stderr_reader: None, + expected_exit: false, + exit_status: None, + guardrail_stop: Arc::new(AtomicBool::new(false)), + guardrail_thread: None, + guardrail_thread_handle: None, + #[cfg(target_os = "macos")] + denial_logger: None, + } + } + + #[cfg(target_family = "windows")] + fn test_worker_process(child: Child) -> WorkerProcess { + let (stdin_tx, _stdin_rx) = mpsc::channel(); + WorkerProcess { + child: worker_child_from_process(child), + stdin_tx, + session_tmpdir: None, + ipc: IpcHandle::new(), + stdout_reader: None, + stderr_reader: None, + _pty_conpty: None, + expected_exit: false, + exit_status: None, + } + } + + #[cfg(target_family = "unix")] + fn capture_recorded_unix_kills(f: F) -> (R, Vec<(i32, i32)>) + where + F: FnOnce() -> R, + { + TEST_UNIX_KILL_RECORDER.with(|recorder| { + assert!( + recorder.borrow().is_none(), + "did not expect nested unix kill recorder" + ); + *recorder.borrow_mut() = Some(Vec::new()); + }); + let result = f(); + let kills = TEST_UNIX_KILL_RECORDER + .with(|recorder| recorder.borrow_mut().take().expect("recorded kills")); + (result, kills) + } + + #[cfg(target_family = "windows")] + fn capture_recorded_windows_ctrl_events(f: F) -> (R, Vec<(u32, u32)>) + where + F: FnOnce() -> R, + { + capture_recorded_windows_ctrl_events_with_result(1, f) + } + + #[cfg(target_family = "windows")] + fn capture_recorded_windows_ctrl_events_with_result( + result: i32, + f: F, + ) -> (R, Vec<(u32, u32)>) + where + F: FnOnce() -> R, + { + TEST_WINDOWS_CTRL_EVENT_RECORDER.with(|recorder| { + assert!( + recorder.borrow().is_none(), + "did not expect nested Windows ctrl-event recorder" + ); + *recorder.borrow_mut() = Some(TestWindowsCtrlEventRecorder { + result, + events: Vec::new(), + }); + }); + let result = f(); + let events = TEST_WINDOWS_CTRL_EVENT_RECORDER.with(|recorder| { + recorder + .borrow_mut() + .take() + .expect("recorded Windows ctrl events") + .events + }); + (result, events) + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_pty_creation_flags_create_process_group_for_interrupts() { + assert!( + windows_pty_creation_flags() & CREATE_NEW_PROCESS_GROUP != 0, + "Windows PTY workers must be their own process group so Ctrl-Break can target them" + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_generic_interrupt_sends_ctrl_break_to_worker_process_group() { + let child = sleeping_test_child(); + let child_id = child.id(); + let mut process = test_worker_process(child); + let (result, events) = capture_recorded_windows_ctrl_events(|| process.send_interrupt()); + + let _ = process.kill(); + + assert!( + result.is_ok(), + "expected Windows interrupt to succeed: {result:?}" + ); + assert_eq!( + events, + vec![(CTRL_BREAK_EVENT, child_id)], + "expected Ctrl-Break to target the worker process group" + ); + } + + #[cfg(target_family = "unix")] + #[test] + fn unix_protocol_interrupt_sends_sideband_and_sigint() { + let child = successful_test_child(); + let child_id = child.id() as i32; + let mut process = test_worker_process(child); + let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); + process.ipc.set(server); + let mut driver = + ProtocolBackendDriver::new(WorkerStdinTransport::Pty, ProtocolStdinAccounting::Payload); + + let (result, kills) = capture_recorded_unix_kills(|| driver.interrupt(&mut process)); + let sideband = worker.recv(Some(Duration::from_secs(1))); + drop(worker); + let _ = process.finish_exited(); + + assert!( + result.is_ok(), + "expected protocol interrupt to succeed: {result:?}" + ); + assert!( + matches!(sideband, Some(ServerToWorkerIpcMessage::Interrupt)), + "expected protocol interrupt to notify sideband, got: {sideband:?}" + ); + assert_eq!( + kills, + vec![(-child_id, libc::SIGINT)], + "expected Unix protocol interrupt to continue with SIGINT after sideband" + ); + } + + #[cfg(target_family = "unix")] + #[test] + fn unix_python_interrupt_sends_request_generation_and_sigint() { + let child = successful_test_child(); + let child_id = child.id() as i32; + let mut process = test_worker_process(child); + let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); + process.ipc.set(server.clone()); + let mut driver = ProtocolBackendDriver::python( + WorkerStdinTransport::Pty, + ProtocolStdinAccounting::Payload, + ); + driver + .on_input_start("1+1\n", b"1+1\n", &server, Duration::from_secs(1)) + .expect("request start"); + + let ack_thread = std::thread::spawn(move || { + let sideband = worker.recv(Some(Duration::from_secs(1))); + worker + .send(WorkerToServerIpcMessage::PythonInterruptAck) + .expect("send Python interrupt ack"); + sideband + }); + let (result, kills) = capture_recorded_unix_kills(|| driver.interrupt(&mut process)); + let sideband = ack_thread.join().expect("join Python interrupt ack thread"); + let _ = process.finish_exited(); + + assert!( + result.is_ok(), + "expected Python interrupt to succeed: {result:?}" + ); + assert!( + matches!( + sideband, + Some(ServerToWorkerIpcMessage::PythonInterrupt { + request_generation: 1 + }) + ), + "expected Python interrupt generation sideband, got: {sideband:?}" + ); + assert_eq!( + kills, + vec![(-child_id, libc::SIGINT)], + "expected Unix Python interrupt to continue with SIGINT after sideband" + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_protocol_interrupt_sends_sideband_and_ctrl_break() { + let child = sleeping_test_child(); + let child_id = child.id(); + let mut process = test_worker_process(child); + let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); + process.ipc.set(server); + let mut driver = + ProtocolBackendDriver::new(WorkerStdinTransport::Pty, ProtocolStdinAccounting::Payload); + let (result, events) = + capture_recorded_windows_ctrl_events(|| driver.interrupt(&mut process)); + let sideband = worker.recv(Some(Duration::from_secs(1))); + drop(worker); + process.ipc = IpcHandle::new(); + let _ = process.kill(); + + assert!( + result.is_ok(), + "expected protocol interrupt to succeed: {result:?}" + ); + assert!( + matches!(sideband, Some(ServerToWorkerIpcMessage::Interrupt)), + "expected protocol interrupt to notify sideband, got: {sideband:?}" + ); + assert_eq!( + events, + vec![(CTRL_BREAK_EVENT, child_id)], + "expected Windows protocol interrupt to continue with Ctrl-Break after sideband" + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_python_interrupt_sends_request_generation_and_ctrl_break() { + let child = sleeping_test_child(); + let child_id = child.id(); + let mut process = test_worker_process(child); + let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); + process.ipc.set(server.clone()); + let mut driver = ProtocolBackendDriver::python( + WorkerStdinTransport::Pty, + ProtocolStdinAccounting::Payload, + ); + driver + .on_input_start("1+1\n", b"1+1\n", &server, Duration::from_secs(1)) + .expect("request start"); + + let ack_thread = std::thread::spawn(move || { + let sideband = worker.recv(Some(Duration::from_secs(1))); + worker + .send(WorkerToServerIpcMessage::PythonInterruptAck) + .expect("send Python interrupt ack"); + sideband + }); + let (result, events) = + capture_recorded_windows_ctrl_events(|| driver.interrupt(&mut process)); + let sideband = ack_thread.join().expect("join Python interrupt ack thread"); + process.ipc = IpcHandle::new(); + let _ = process.kill(); + + assert!( + result.is_ok(), + "expected Python interrupt to succeed: {result:?}" + ); + assert!( + matches!( + sideband, + Some(ServerToWorkerIpcMessage::PythonInterrupt { + request_generation: 1 + }) + ), + "expected Python interrupt generation sideband, got: {sideband:?}" + ); + assert_eq!( + events, + vec![(CTRL_BREAK_EVENT, child_id)], + "expected Windows Python interrupt to continue with Ctrl-Break after sideband" + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_pty_output_filter_strips_split_conpty_cursor_sequences() { + let mut filter = WindowsPtyOutputFilter::default(); + let mut output = filter.filter(b"\r\nmcp-repl\n\x1b[?25"); + output.extend(filter.filter(b"l\x1b[15;1H\x1b[?25h>>> ")); + + assert_eq!(String::from_utf8(output).unwrap(), "\r\nmcp-repl\n>>> "); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_pty_output_filter_preserves_sgr_sequences() { + let mut filter = WindowsPtyOutputFilter::default(); + let output = filter.filter(b"\x1b[31mred\x1b[0m\n"); + + assert_eq!(String::from_utf8(output).unwrap(), "\x1b[31mred\x1b[0m\n"); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_pty_output_filter_strips_initial_sgr_reset() { + let mut filter = WindowsPtyOutputFilter::default(); + let output = filter.filter(b"\x1b[mx\n>>> "); + + assert_eq!(String::from_utf8(output).unwrap(), "x\n>>> "); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_pty_output_filter_strips_split_osc_sequences() { + let mut filter = WindowsPtyOutputFilter::default(); + let mut output = filter.filter(b"\x1b]0;mcp"); + output.extend(filter.filter(b"-repl\x07>>> ")); + + assert_eq!(String::from_utf8(output).unwrap(), ">>> "); } #[cfg(target_family = "windows")] - fn test_worker_process(child: Child) -> WorkerProcess { - let (stdin_tx, _stdin_rx) = mpsc::channel(); - WorkerProcess { - child, - stdin_tx, - session_tmpdir: None, - ipc: IpcHandle::new(), - stdout_reader: None, - stderr_reader: None, - expected_exit: false, - exit_status: None, - } + #[test] + fn windows_pty_input_payload_translates_crlf_as_one_enter() { + assert_eq!(windows_pty_input_payload(b"a\r\nb\nc\rd"), b"a\rb\rc\rd"); } - #[cfg(target_family = "unix")] - fn capture_recorded_unix_kills(f: F) -> (R, Vec<(i32, i32)>) - where - F: FnOnce() -> R, - { - TEST_UNIX_KILL_RECORDER.with(|recorder| { - assert!( - recorder.borrow().is_none(), - "did not expect nested unix kill recorder" - ); - *recorder.borrow_mut() = Some(Vec::new()); - }); - let result = f(); - let kills = TEST_UNIX_KILL_RECORDER - .with(|recorder| recorder.borrow_mut().take().expect("recorded kills")); - (result, kills) + #[cfg(target_family = "windows")] + #[test] + fn windows_pty_accounting_payload_reports_console_line_endings() { + assert_eq!( + windows_pty_accounting_payload(b"a\r\nb\nc\rd"), + b"a\r\nb\r\nc\r\nd" + ); } #[test] @@ -7628,16 +8436,14 @@ mod tests { #[test] fn completion_infers_nested_waiting_prompt_that_reuses_primary_prompt_text() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("value <- readline(prompt = \"> \")", &server) - .expect("begin request"); + server.begin_request_with_stdin(b"value <- readline(prompt = \"> \")\n"); let prompt = "> ".to_string(); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: prompt.clone(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: prompt.clone(), - line: "value <- readline(prompt = \"> \")\n".to_string(), - }); + let _ = worker.send(readline_input_bytes( + b"value <- readline(prompt = \"> \")\n", + )); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: prompt.clone(), }); @@ -7646,26 +8452,18 @@ mod tests { driver_wait_for_completion(Duration::from_millis(200), server, OutputTextSource::Ipc) .expect("expected stable waiting prompt to complete request"); assert_eq!(completion.prompt.as_deref(), Some("> ")); - assert_eq!(completion.echo_events.len(), 1); - assert_eq!(completion.echo_events[0].prompt, "> "); - assert_eq!( - completion.echo_events[0].line, - "value <- readline(prompt = \"> \")\n" - ); + assert!(completion.echo_events.is_empty()); } #[test] fn completion_infers_stable_waiting_prompt_without_worker_completion_event() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("1+1", &server).expect("begin request"); + server.begin_request_with_stdin(b"1+1\n"); let prompt = "> ".to_string(); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: prompt.clone(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: prompt.clone(), - line: "1+1\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"1+1\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: prompt.clone(), }); @@ -7675,22 +8473,18 @@ mod tests { .expect("expected stable waiting prompt to complete request"); assert_eq!(completion.prompt.as_deref(), Some("> ")); - assert_eq!(completion.echo_events.len(), 1); - assert_eq!(completion.echo_events[0].line, "1+1\n"); + assert!(completion.echo_events.is_empty()); } #[test] fn completion_settle_after_prompt_does_not_count_as_execution_timeout() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("1+1", &server).expect("begin request"); + server.begin_request_with_stdin(b"1+1\n"); let prompt = "> ".to_string(); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: prompt.clone(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: prompt.clone(), - line: "1+1\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"1+1\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: prompt.clone(), }); @@ -7701,21 +8495,17 @@ mod tests { .expect("expected prompt seen before timeout to complete after stable settle"); assert_eq!(completion.prompt.as_deref(), Some("> ")); - assert_eq!(completion.echo_events.len(), 1); - assert_eq!(completion.echo_events[0].line, "1+1\n"); + assert!(completion.echo_events.is_empty()); } #[test] fn completion_infers_stable_continuation_prompt_when_input_is_consumed() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("1+\n1", &server).expect("begin request"); + server.begin_request_with_stdin(b"1+\n"); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "1+\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"1+\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "+ ".to_string(), }); @@ -7727,9 +8517,9 @@ mod tests { } #[test] - fn completion_settle_waits_for_late_echo_events() { + fn completion_settle_waits_for_late_stdin_accounting() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("1+\n1", &server).expect("begin request"); + server.begin_request_with_stdin(b"1+\n1\n"); let prompt = "> ".to_string(); let delayed_worker = worker.clone(); @@ -7739,15 +8529,9 @@ mod tests { let late_sender = thread::spawn(move || { thread::sleep(Duration::from_millis(1)); - let _ = delayed_worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "1+\n".to_string(), - }); + let _ = delayed_worker.send(readline_input_bytes(b"1+\n")); thread::sleep(Duration::from_millis(21)); - let _ = delayed_worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "+ ".to_string(), - line: "1\n".to_string(), - }); + let _ = delayed_worker.send(readline_input_bytes(b"1\n")); let _ = delayed_worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); @@ -7759,12 +8543,8 @@ mod tests { late_sender.join().expect("late sender should join"); assert_eq!(completion.prompt.as_deref(), Some("> ")); - assert_eq!(completion.echo_events.len(), 2); + assert!(completion.echo_events.is_empty()); assert!(completion.protocol_warnings.is_empty()); - assert_eq!(completion.echo_events[0].prompt, "> "); - assert_eq!(completion.echo_events[0].line, "1+\n"); - assert_eq!(completion.echo_events[1].prompt, "+ "); - assert_eq!(completion.echo_events[1].line, "1\n"); } #[test] @@ -7783,13 +8563,7 @@ mod tests { "did not expect buffered readline start to complete request, got {early:?}" ); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineInput { - text: "1+\n".to_string(), - }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "1+\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"1+\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "+ ".to_string(), }); @@ -7801,13 +8575,7 @@ mod tests { "did not expect buffered continuation start to complete request, got {continuation:?}" ); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineInput { - text: "1\n".to_string(), - }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "+ ".to_string(), - line: "1\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"1\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); @@ -7817,16 +8585,15 @@ mod tests { .expect("expected completion after final unsatisfied prompt"); assert_eq!(completion.prompt.as_deref(), Some("> ")); - assert_eq!(completion.echo_events.len(), 2); - assert_eq!(completion.echo_events[0].line, "1+\n"); - assert_eq!(completion.echo_events[1].line, "1\n"); + assert!(completion.echo_events.is_empty()); } #[test] - fn next_request_result_is_retained_when_prompt_is_already_active() { + fn next_request_prompt_is_retained_when_prompt_is_already_active() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("first()", &server).expect("begin request"); + server.begin_request_with_stdin(b"first()\n"); + let _ = worker.send(readline_input_bytes(b"first()\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); @@ -7838,11 +8605,8 @@ mod tests { .expect("expected first completion"); assert_eq!(first.prompt.as_deref(), Some("> ")); - driver_on_input_start("second()", &server).expect("begin request"); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "second()\n".to_string(), - }); + server.begin_request_with_stdin(b"second()\n"); + let _ = worker.send(readline_input_bytes(b"second()\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); @@ -7852,23 +8616,18 @@ mod tests { .expect("expected second completion"); assert!(second.protocol_warnings.is_empty()); - assert_eq!(second.echo_events.len(), 1); - assert_eq!(second.echo_events[0].prompt, "> "); - assert_eq!(second.echo_events[0].line, "second()\n"); + assert!(second.echo_events.is_empty()); } #[test] - fn completion_preserves_echo_events_when_next_prompt_arrives_immediately() { + fn completion_preserves_prompt_when_next_prompt_arrives_immediately() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("first()", &server).expect("begin request"); + server.begin_request_with_stdin(b"first()\n"); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "first()\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"first()\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); @@ -7879,25 +8638,20 @@ mod tests { assert_eq!(completion.prompt.as_deref(), Some("> ")); assert!(completion.protocol_warnings.is_empty()); - assert_eq!(completion.echo_events.len(), 1); - assert_eq!(completion.echo_events[0].prompt, "> "); - assert_eq!(completion.echo_events[0].line, "first()\n"); + assert!(completion.echo_events.is_empty()); } #[test] - fn completion_retains_echo_events_when_session_ends_before_prompt_completion() { + fn completion_reports_session_end_before_prompt_completion() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("quit()", &server).expect("begin request"); + server.begin_request_with_stdin(b"quit()\n"); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "quit()\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"quit()\n")); let _ = worker.send(WorkerToServerIpcMessage::SessionEnd { - reason: None, + reason: "runtime_exit".to_string(), message_b64: None, }); @@ -7906,29 +8660,24 @@ mod tests { .expect("expected completion after session end"); assert!(completion.session_end_seen); - assert_eq!(completion.echo_events.len(), 1); - assert_eq!(completion.echo_events[0].prompt, "> "); - assert_eq!(completion.echo_events[0].line, "quit()\n"); + assert!(completion.echo_events.is_empty()); } #[test] fn completion_reports_session_end_when_prompt_is_also_stable() { let (server, worker) = crate::ipc::test_connection_pair().expect("ipc pair"); - driver_on_input_start("quit()", &server).expect("begin request"); + server.begin_request_with_stdin(b"quit()\n"); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "quit()\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"quit()\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: "> ".to_string(), }); thread::sleep(Duration::from_millis(25)); let _ = worker.send(WorkerToServerIpcMessage::SessionEnd { - reason: None, + reason: "runtime_exit".to_string(), message_b64: None, }); thread::sleep(Duration::from_millis(25)); @@ -7938,9 +8687,7 @@ mod tests { .expect("expected completion after session end"); assert!(completion.session_end_seen); - assert_eq!(completion.echo_events.len(), 1); - assert_eq!(completion.echo_events[0].prompt, "> "); - assert_eq!(completion.echo_events[0].line, "quit()\n"); + assert!(completion.echo_events.is_empty()); } #[test] @@ -8142,20 +8889,18 @@ mod tests { .expect("worker manager"); let mut process = test_worker_process(successful_test_child()); process.exit_status = Some(process.child.wait().expect("wait test child")); - process.ipc.set(server); + process.ipc.set(server.clone()); manager.process = Some(process); manager.pending_request = true; manager.pending_request_started_at = Some(std::time::Instant::now()); manager.pending_request_input = Some("quit()\n".to_string()); + server.begin_request_with_stdin(b"quit()\n"); let prompt = ">>> ".to_string(); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: prompt.clone(), }); - let _ = worker.send(WorkerToServerIpcMessage::ReadlineResult { - prompt, - line: "quit()\n".to_string(), - }); + let _ = worker.send(readline_input_bytes(b"quit()\n")); let _ = worker.send(WorkerToServerIpcMessage::ReadlineStart { prompt: ">>> ".to_string(), }); @@ -8455,94 +9200,6 @@ mod tests { ); } - #[test] - fn files_nonfinal_drain_preserves_echo_only_input() { - let manager = WorkerManager::new( - Backend::R, - SandboxCliPlan::default(), - crate::oversized_output::OversizedOutputMode::Files, - ) - .expect("worker manager"); - - manager - .pending_output_tape - .append_stdout_ipc_bytes(b"> Sys.sleep(5)\n"); - manager - .pending_output_tape - .append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "Sys.sleep(5)\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - - let formatted = manager.drain_formatted_output(); - - assert_eq!( - formatted.contents, - vec![WorkerContent::stdout("> Sys.sleep(5)\n")], - "expected an in-flight files-mode drain to keep the echoed command visible" - ); - } - - #[test] - fn files_nonfinal_drain_drops_leading_repl_echo_after_worker_output() { - let manager = WorkerManager::new( - Backend::R, - SandboxCliPlan::default(), - crate::oversized_output::OversizedOutputMode::Files, - ) - .expect("worker manager"); - - manager - .pending_output_tape - .append_stdout_ipc_bytes(b"> Sys.sleep(5)\n"); - manager - .pending_output_tape - .append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "Sys.sleep(5)\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - manager.pending_output_tape.append_stdout_bytes(b"start\n"); - - let formatted = manager.drain_formatted_output(); - - assert_eq!( - formatted.contents, - vec![WorkerContent::stdout("start\n")], - "expected worker output to hide the leading timed-out REPL echo again" - ); - } - - #[test] - fn files_prepare_input_context_preserves_unsettled_echo_prefix() { - let mut manager = WorkerManager::new( - Backend::R, - SandboxCliPlan::default(), - crate::oversized_output::OversizedOutputMode::Files, - ) - .expect("worker manager"); - - manager - .pending_output_tape - .append_stdout_ipc_bytes(b"> Sys.sleep(5)\n"); - manager - .pending_output_tape - .append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "Sys.sleep(5)\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - - let context = manager.prepare_input_context_files(); - - assert_eq!( - context.detached_prefix_contents, - vec![WorkerContent::stdout("> Sys.sleep(5)\n")], - "expected a sealed files-mode prefix without settled completion metadata to keep echoed input" - ); - } - #[test] fn files_preserved_detached_prefix_stays_separate_from_new_session_startup_output() { let mut manager = WorkerManager::new( @@ -8928,13 +9585,12 @@ mod tests { OutputTimeline::new(output_ring.clone()), ); capture.append_output_text(b"pager output\n", TextStream::Stdout, false); - capture.append_image(IpcPlotImage { + capture.append_image(IpcOutputImage { id: "img-1".to_string(), data: "AA==".to_string(), mime_type: "image/png".to_string(), is_new: true, updates_previous_image: false, - readline_results_seen: 0, }); capture.append_sideband(PendingSidebandKind::RequestBoundary); @@ -8964,50 +9620,6 @@ mod tests { ); } - #[test] - fn files_output_capture_anchors_update_notice_before_late_echo() { - let output_ring = Arc::new(OutputRing::with_capacity(OUTPUT_RING_CAPACITY_BYTES)); - let tape = PendingOutputTape::new(); - let capture = LiveOutputCapture::new( - OversizedOutputMode::Files, - tape.clone(), - OutputTimeline::new(output_ring), - ); - - capture.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: "> ".to_string(), - line: "lines(4:8, 4:8)\n".to_string(), - echo_source: PendingTextSource::Ipc, - }); - capture.append_image(IpcPlotImage { - id: "img-1".to_string(), - data: "AA==".to_string(), - mime_type: "image/png".to_string(), - is_new: true, - updates_previous_image: true, - readline_results_seen: 1, - }); - capture.append_output_text(b"> lines(4:8, 4:8)\n", TextStream::Stdout, false); - - let contents = tape - .drain_final_snapshot() - .format_contents_for_reply() - .contents; - - assert_eq!( - contents, - vec![ - WorkerContent::server_stdout(PREVIOUS_IMAGE_UPDATE_NOTICE), - WorkerContent::ContentImage { - data: "AA==".to_string(), - mime_type: "image/png".to_string(), - id: "img-1".to_string(), - is_new: true, - }, - ] - ); - } - #[test] fn files_ipc_output_text_appends_to_tape_and_timeline_in_ipc_order() { let output_ring = Arc::new(OutputRing::with_capacity(OUTPUT_RING_CAPACITY_BYTES)); @@ -9021,7 +9633,6 @@ mod tests { let output_capture = capture.clone(); let start_capture = capture.clone(); - let result_capture = capture.clone(); let image_capture = capture.clone(); let session_capture = capture.clone(); let (_server, worker) = crate::ipc::test_connection_pair_with_handlers(IpcHandlers { @@ -9031,14 +9642,7 @@ mod tests { on_readline_start: Some(Arc::new(move |prompt| { start_capture.append_sideband(PendingSidebandKind::ReadlineStart { prompt }); })), - on_readline_result: Some(Arc::new(move |event| { - result_capture.append_sideband(PendingSidebandKind::ReadlineResult { - prompt: event.prompt, - line: event.line, - echo_source: PendingTextSource::Ipc, - }); - })), - on_plot_image: Some(Arc::new(move |image| { + on_output_image: Some(Arc::new(move |image| { image_capture.append_image(image); })), on_session_end: Some(Arc::new(move || { @@ -9061,19 +9665,13 @@ mod tests { }) .expect("send stdout output_text"); worker - .send(WorkerToServerIpcMessage::ReadlineResult { - prompt: "> ".to_string(), - line: "plot(1)\n".to_string(), - }) - .expect("send readline_result"); - worker - .send(WorkerToServerIpcMessage::PlotImage { + .send(WorkerToServerIpcMessage::OutputImage { + image_id: "plot-1".to_string(), mime_type: "image/png".to_string(), - data: "AA==".to_string(), - is_update: false, - source: None, + data_b64: "AA==".to_string(), + update: false, }) - .expect("send plot_image"); + .expect("send output_image"); worker .send(WorkerToServerIpcMessage::OutputText { stream: TextStream::Stderr, @@ -9083,7 +9681,7 @@ mod tests { .expect("send stderr output_text"); worker .send(WorkerToServerIpcMessage::SessionEnd { - reason: None, + reason: "runtime_exit".to_string(), message_b64: None, }) .expect("send session_end"); @@ -9093,7 +9691,7 @@ mod tests { .expect("server IPC consumed session_end"); let snapshot = tape.drain_final_snapshot(); - assert_eq!(snapshot.events.len(), 6); + assert_eq!(snapshot.events.len(), 5); assert!(matches!( &snapshot.events[0], PendingOutputEvent::Sideband { @@ -9112,22 +9710,15 @@ mod tests { )); assert!(matches!( &snapshot.events[2], - PendingOutputEvent::Sideband { - kind: PendingSidebandKind::ReadlineResult { prompt, line, .. }, - .. - } if prompt == "> " && line == "plot(1)\n" - )); - assert!(matches!( - &snapshot.events[3], PendingOutputEvent::Image { id, mime_type, - readline_results_seen: 1, + readline_results_seen: 0, .. } if id.starts_with("image-") && mime_type == "image/png" )); assert!(matches!( - &snapshot.events[4], + &snapshot.events[3], PendingOutputEvent::TextFragment { stream: TextStream::Stderr, origin: ContentOrigin::Worker, @@ -9136,7 +9727,7 @@ mod tests { } if bytes == b"err\n" )); assert!(matches!( - &snapshot.events[5], + &snapshot.events[4], PendingOutputEvent::Sideband { kind: PendingSidebandKind::SessionEnd, .. @@ -9162,7 +9753,7 @@ mod tests { assert_eq!(image_event.0, b"before\n".len() as u64); assert!(image_event.1.starts_with("image-")); assert_eq!(image_event.2, "image/png"); - assert_eq!(*image_event.3, 1); + assert_eq!(*image_event.3, 0); } #[test] @@ -9174,13 +9765,12 @@ mod tests { OutputTimeline::new(output_ring.clone()), ); - capture.append_image(IpcPlotImage { + capture.append_image(IpcOutputImage { id: "img-1".to_string(), data: "AA==".to_string(), mime_type: "image/png".to_string(), is_new: true, updates_previous_image: true, - readline_results_seen: 1, }); capture.append_output_text(b"> lines(4:8, 4:8)\n", TextStream::Stdout, false); @@ -9209,6 +9799,7 @@ mod tests { id: "img-1".to_string(), is_new: true, }, + WorkerContent::worker_stdout("> lines(4:8, 4:8)\n"), ] ); } @@ -9264,13 +9855,13 @@ mod tests { }); let retry = manager.maybe_retry_spawn_without_linux_bwrap( - &WorkerError::Protocol("ipc disconnected while waiting for backend info".to_string()), + &WorkerError::Protocol("ipc disconnected while waiting for worker_ready".to_string()), false, ); assert!( retry, - "expected backend-info disconnect to trigger bwrap fallback" + "expected worker_ready disconnect to trigger bwrap fallback" ); assert!( !manager.sandbox_state.use_linux_sandbox_bwrap, @@ -9320,7 +9911,6 @@ mod tests { }, sandbox_cwd: Some(std::env::temp_dir()), use_linux_sandbox_bwrap: Some(true), - use_legacy_landlock: None, }); manager.inherited_sandbox_state = Some(inherited_state.clone()); manager.sandbox_state = resolve_effective_sandbox_state_with_defaults( @@ -9335,7 +9925,7 @@ mod tests { ); let retry = manager.maybe_retry_spawn_without_linux_bwrap( - &WorkerError::Protocol("ipc disconnected while waiting for backend info".to_string()), + &WorkerError::Protocol("ipc disconnected while waiting for worker_ready".to_string()), false, ); assert!(retry, "expected startup failure to disable bwrap"); @@ -9349,7 +9939,6 @@ mod tests { "exclude_slash_tmp": false }, "sandboxCwd": std::env::temp_dir(), - "useLegacyLandlock": false, "codexLinuxSandboxExe": "/tmp/codex-linux-sandbox" })) .expect("Codex sandbox metadata"); @@ -9392,7 +9981,6 @@ mod tests { }, sandbox_cwd: Some(std::env::temp_dir()), use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }); manager.inherited_sandbox_state = Some(inherited_state.clone()); manager.sandbox_state = resolve_effective_sandbox_state_with_defaults( @@ -9407,7 +9995,7 @@ mod tests { ); let retry = manager.maybe_retry_spawn_without_linux_bwrap( - &WorkerError::Protocol("ipc disconnected while waiting for backend info".to_string()), + &WorkerError::Protocol("ipc disconnected while waiting for worker_ready".to_string()), false, ); assert!(retry, "expected startup failure to disable bwrap"); @@ -9421,7 +10009,6 @@ mod tests { "exclude_slash_tmp": false }, "sandboxCwd": std::env::temp_dir(), - "useLegacyLandlock": false, "codexLinuxSandboxExe": "/tmp/codex-linux-sandbox" })) .expect("Codex sandbox metadata"); @@ -9495,7 +10082,6 @@ mod tests { }, sandbox_cwd: Some(writable_root.clone()), use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }) .expect("workspace-write Codex metadata should satisfy deferred refinements"); @@ -9549,7 +10135,6 @@ mod tests { }, sandbox_cwd: None, use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }); manager.inherited_sandbox_state = Some(inherited_before.clone()); manager.sandbox_state = resolve_effective_sandbox_state_with_defaults( @@ -9565,7 +10150,6 @@ mod tests { sandbox_policy: SandboxPolicy::DangerFullAccess, sandbox_cwd: None, use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }, Duration::from_millis(1), ) @@ -9762,7 +10346,6 @@ mod tests { sandbox_policy: SandboxPolicy::ReadOnly, sandbox_cwd: None, use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }) .expect("initial inherited state"); let mut process = test_worker_process(successful_test_child()); @@ -9806,7 +10389,6 @@ mod tests { sandbox_policy: SandboxPolicy::ReadOnly, sandbox_cwd: None, use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }) .expect("initial inherited state"); let mut process = test_worker_process(successful_test_child()); @@ -9857,7 +10439,6 @@ mod tests { sandbox_policy: SandboxPolicy::ReadOnly, sandbox_cwd: Some(sandbox_cwd.clone()), use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }) .expect("initial inherited read-only state"); let mut process = test_worker_process(successful_test_child()); @@ -9882,7 +10463,6 @@ mod tests { }, sandbox_cwd: Some(sandbox_cwd.clone()), use_linux_sandbox_bwrap: None, - use_legacy_landlock: None, }), ..WriteStdinOptions::default() }, @@ -9903,7 +10483,7 @@ mod tests { } Err(WorkerError::Protocol(message)) => { assert!( - message.contains("backend info") || message.contains("ipc disconnected"), + message.contains("worker_ready") || message.contains("ipc disconnected"), "expected the failed interrupt-tail respawn attempt to fail during worker startup, got: {message:?}" ); } @@ -9976,6 +10556,117 @@ mod tests { assert_eq!(normalize_input_newlines("a\r\nb\rc\n"), "a\nb\nc\n"); } + #[cfg(target_family = "windows")] + #[test] + fn windows_custom_pty_driver_reports_console_line_endings_for_accounting() { + let spec = CustomWorkerSpec { + executable: PathBuf::from("worker.exe"), + args: Vec::new(), + working_dir: CustomWorkerWorkingDir::Policy(CustomWorkerWorkingDirPolicy::Inherit), + env: Default::default(), + stdin: crate::backend::CustomWorkerStdin::Pty, + sandbox: crate::backend::CustomWorkerSandbox::Server, + }; + + let driver = protocol_backend_driver(&spec); + + assert_eq!( + driver.prepare_input_payload("a\r\nb\rc\n"), + b"a\r\nb\r\nc\r\n" + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_sandboxed_custom_pty_requests_wrapper_conpty() { + let mut spec = CustomWorkerSpec { + executable: PathBuf::from("worker.exe"), + args: Vec::new(), + working_dir: CustomWorkerWorkingDir::Policy(CustomWorkerWorkingDirPolicy::Inherit), + env: Default::default(), + stdin: crate::backend::CustomWorkerStdin::Pty, + sandbox: crate::backend::CustomWorkerSandbox::Server, + }; + + assert!(custom_worker_requests_wrapper_conpty(&spec, true)); + assert!(!custom_worker_requests_wrapper_conpty(&spec, false)); + + spec.stdin = crate::backend::CustomWorkerStdin::Pipe; + assert!(!custom_worker_requests_wrapper_conpty(&spec, true)); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_sandboxed_custom_pty_uses_pipe_transport_to_wrapper() { + let mut spec = CustomWorkerSpec { + executable: PathBuf::from("worker.exe"), + args: Vec::new(), + working_dir: CustomWorkerWorkingDir::Policy(CustomWorkerWorkingDirPolicy::Inherit), + env: Default::default(), + stdin: crate::backend::CustomWorkerStdin::Pty, + sandbox: crate::backend::CustomWorkerSandbox::Server, + }; + + assert_eq!( + custom_worker_launch_stdin_transport(&spec, true), + WorkerStdinTransport::Pipe, + "sandboxed custom PTY workers should use pipe stdio to the wrapper" + ); + assert_eq!( + custom_worker_launch_stdin_transport(&spec, false), + WorkerStdinTransport::Pty + ); + + spec.stdin = crate::backend::CustomWorkerStdin::Pipe; + assert_eq!( + custom_worker_launch_stdin_transport(&spec, true), + WorkerStdinTransport::Pipe + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_sandbox_conpty_env_applies_to_process_and_pty_launch() { + let mut command = Command::new("worker.exe"); + let mut pty_command = WindowsPtyCommand::new(Path::new("worker.exe")); + + apply_windows_sandbox_conpty_env(&mut command); + apply_windows_sandbox_conpty_env_to_pty(&mut pty_command); + + let envs: std::collections::BTreeMap<_, _> = command + .get_envs() + .map(|(key, value)| { + ( + key.to_string_lossy().to_string(), + value.map(|value| value.to_string_lossy().to_string()), + ) + }) + .collect(); + assert_eq!( + envs.get(crate::windows_sandbox::WINDOWS_SANDBOX_CONPTY_ENV), + Some(&Some("1".to_string())) + ); + assert!( + pty_command.env.iter().any(|(key, value)| { + key.to_string_lossy() == crate::windows_sandbox::WINDOWS_SANDBOX_CONPTY_ENV + && value.to_string_lossy() == "1" + }), + "expected PTY launch to ask the sandbox wrapper for child ConPTY" + ); + } + + #[cfg(target_family = "windows")] + #[test] + fn windows_sandboxed_python_driver_normalizes_input_for_wrapper_conpty() { + let sandbox_state = SandboxState { + sandbox_policy: SandboxPolicy::ReadOnly, + ..SandboxState::default() + }; + let driver = python_backend_driver(&sandbox_state); + + assert_eq!(driver.prepare_input_payload("a\r\nb\rc\n"), b"a\nb\nc\n"); + } + #[test] fn apply_debug_startup_env_uses_session_tmpdir_for_worker_log() { let _guard = env_test_mutex().lock().expect("env mutex"); @@ -10096,10 +10787,11 @@ mod tests { #[cfg(target_family = "windows")] #[test] fn windows_ipc_connect_error_reaps_wrapper_process() { - let mut child = Command::new("powershell.exe") + let child = Command::new("powershell.exe") .args(["-NoProfile", "-Command", "Start-Sleep -Seconds 30"]) .spawn() .expect("spawn test child process"); + let mut child = worker_child_from_process(child); let result = handle_windows_ipc_connect_result( Err(std::io::Error::other("ipc connect failed")), @@ -10125,10 +10817,11 @@ mod tests { #[cfg(target_family = "windows")] #[test] fn windows_soft_termination_does_not_kill_child() { - let mut child = Command::new("powershell.exe") + let child = Command::new("powershell.exe") .args(["-NoProfile", "-Command", "Start-Sleep -Seconds 30"]) .spawn() .expect("spawn test child process"); + let mut child = worker_child_from_process(child); request_soft_termination(&mut child).expect("soft terminate call should succeed"); @@ -10142,6 +10835,27 @@ mod tests { let _ = child.wait(); } + #[cfg(target_family = "windows")] + #[test] + fn windows_pty_child_treats_signaled_still_active_exit_code_as_exited() { + use std::os::windows::io::IntoRawHandle; + + let child = Command::new("powershell.exe") + .args(["-NoProfile", "-Command", "exit 259"]) + .spawn() + .expect("spawn exit-code-259 child process"); + let process_id = child.id(); + let process = child.into_raw_handle() as HANDLE; + let mut child = WindowsPtyChild { + process, + process_id, + }; + + let status = child.wait().expect("wait for exit-code-259 child"); + + assert_eq!(status.exit_code(), 259); + } + #[cfg(target_family = "windows")] #[test] fn windows_ipc_connect_timeout_is_bounded() { diff --git a/tests/codex_integration.rs b/tests/codex_integration.rs index 831e4c33..2d90845c 100644 --- a/tests/codex_integration.rs +++ b/tests/codex_integration.rs @@ -2035,6 +2035,14 @@ tryCatch({ { continue; } + if path_matches(path, &["_meta", "codex/sandbox-state-meta"]) + && !matches!( + normalized_key.as_str(), + "sandboxPolicy" | "sandboxCwd" | "codexLinuxSandboxExe" + ) + { + continue; + } path.push(normalized_key.clone()); normalize_inner(&mut child, path, workspace, codex_home); path.pop(); @@ -2770,8 +2778,8 @@ tryCatch({ item } - fn resolve_tool_call_spec(request: &Value, legacy_tool_name: &str) -> Option { - let (namespace, name) = split_legacy_tool_name(legacy_tool_name)?; + fn resolve_tool_call_spec(request: &Value, flat_tool_name: &str) -> Option { + let (namespace, name) = split_flat_tool_name(flat_tool_name)?; let tools = request.get("tools")?.as_array()?; let namespaced_tool_present = tools.iter().any(|tool| { tool.get("type").and_then(Value::as_str) == Some("namespace") @@ -2792,13 +2800,13 @@ tryCatch({ }) } - fn split_legacy_tool_name(legacy_tool_name: &str) -> Option<(&str, &str)> { - let split = legacy_tool_name.rfind("__")?; + fn split_flat_tool_name(flat_tool_name: &str) -> Option<(&str, &str)> { + let split = flat_tool_name.rfind("__")?; let namespace_end = split + 2; - (namespace_end < legacy_tool_name.len()).then(|| { + (namespace_end < flat_tool_name.len()).then(|| { ( - &legacy_tool_name[..namespace_end], - &legacy_tool_name[namespace_end..], + &flat_tool_name[..namespace_end], + &flat_tool_name[namespace_end..], ) }) } diff --git a/tests/docs_contracts.rs b/tests/docs_contracts.rs index 027f3a11..11957ce7 100644 --- a/tests/docs_contracts.rs +++ b/tests/docs_contracts.rs @@ -101,13 +101,13 @@ fn docs_index_lists_main_docs() { } #[test] -fn worker_sideband_protocol_keeps_plot_images_one_way() { +fn worker_sideband_protocol_keeps_output_images_one_way() { let protocol = read(&repo_root().join("docs/worker_sideband_protocol.md")); for required in [ r#"{ "type": "output_text", "stream": <"stdout"|"stderr">, "data_b64": , "is_continuation": }"#, - r#"{ "type": "plot_image", "mime_type": , "data": , "is_update": , "source": }"#, - "There is no plot-image acknowledgement message.", + r#"{ "type": "output_image", "image_id": , "mime_type": , "data_b64": , "update": }"#, + "There is no image acknowledgement message.", "Workers must not delay stdout/stderr output waiting for sideband responses.", ] { assert!( @@ -116,7 +116,11 @@ fn worker_sideband_protocol_keeps_plot_images_one_way() { ); } - for forbidden in ["`plot_image_ack`", r#""sequence": "#] { + for forbidden in [ + "`plot_image`", + "`output_image_ack`", + r#""sequence": "#, + ] { assert!( !protocol.contains(forbidden), "did not expect {forbidden} in docs/worker_sideband_protocol.md" diff --git a/tests/install_dual_backend.rs b/tests/dual_backend_registration.rs similarity index 100% rename from tests/install_dual_backend.rs rename to tests/dual_backend_registration.rs diff --git a/tests/fixtures/zod-worker.rs b/tests/fixtures/zod-worker.rs index b2f81a65..6b8a43fc 100644 --- a/tests/fixtures/zod-worker.rs +++ b/tests/fixtures/zod-worker.rs @@ -95,12 +95,14 @@ fn main() -> Result<(), Box> { let command = line.trim_end_matches(['\r', '\n']); let reported_input = if let Some(text) = command.strip_prefix("misreport-input ") { - format!("{text}\n") + let mut bytes = text.as_bytes().to_vec(); + bytes.push(b'\n'); + bytes } else { - line.clone() + line.as_bytes().to_vec() }; - writer.send(&WorkerToServer::ReadlineInput { - text: reported_input, + writer.send(&WorkerToServer::ReadlineInputBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode(reported_input), })?; timeline.run(LifecyclePoint::AfterReadlineInput, &writer)?; if command == "exit" { @@ -338,18 +340,17 @@ fn apply_shutdown_mode(path: Option<&Path>, mode: ShutdownMode) -> io::Result<() } fn discard_buffered_stdin(reader: &mut dyn BufRead, writer: &IpcWriter) -> io::Result<()> { - let (text, len) = { + let (bytes, len) = { let buffer = reader.fill_buf()?; - let text = std::str::from_utf8(buffer) - .map_err(io::Error::other)? - .to_string(); - (text, buffer.len()) + (buffer.to_vec(), buffer.len()) }; if len == 0 { return Ok(()); } reader.consume(len); - writer.send(&WorkerToServer::ReadlineDiscard { text }) + writer.send(&WorkerToServer::ReadlineDiscardBytes { + data_b64: base64::engine::general_purpose::STANDARD.encode(bytes), + }) } fn escape_bytes(bytes: &[u8]) -> String { @@ -636,11 +637,11 @@ enum WorkerToServer { ReadlineStart { prompt: String, }, - ReadlineInput { - text: String, + ReadlineInputBytes { + data_b64: String, }, - ReadlineDiscard { - text: String, + ReadlineDiscardBytes { + data_b64: String, }, OutputText { stream: String, @@ -740,8 +741,8 @@ impl IpcTransport { { let to_worker = std::env::var(IPC_PIPE_TO_WORKER_ENV).map_err(io::Error::other)?; let from_worker = std::env::var(IPC_PIPE_FROM_WORKER_ENV).map_err(io::Error::other)?; - let reader = std::fs::OpenOptions::new().read(true).open(to_worker)?; - let writer = std::fs::OpenOptions::new().write(true).open(from_worker)?; + let reader = open_named_pipe_with_retry(&to_worker, NamedPipeAccess::Read)?; + let writer = open_named_pipe_with_retry(&from_worker, NamedPipeAccess::Write)?; Ok(Self { reader: Box::new(reader), writer: Box::new(writer), @@ -758,6 +759,48 @@ impl IpcTransport { } } +#[cfg(target_family = "windows")] +#[derive(Clone, Copy)] +enum NamedPipeAccess { + Read, + Write, +} + +#[cfg(target_family = "windows")] +fn open_named_pipe_with_retry(path: &str, access: NamedPipeAccess) -> io::Result { + const ERROR_FILE_NOT_FOUND: i32 = 2; + const ERROR_PIPE_BUSY: i32 = 231; + const IPC_OPEN_TIMEOUT: Duration = Duration::from_secs(5); + + let deadline = Instant::now() + IPC_OPEN_TIMEOUT; + loop { + let mut options = std::fs::OpenOptions::new(); + match access { + NamedPipeAccess::Read => { + options.read(true); + } + NamedPipeAccess::Write => { + options.write(true); + } + } + match options.open(path) { + Ok(file) => return Ok(file), + Err(err) + if matches!( + err.raw_os_error(), + Some(ERROR_FILE_NOT_FOUND | ERROR_PIPE_BUSY) + ) => + { + if Instant::now() >= deadline { + return Err(err); + } + } + Err(err) => return Err(err), + } + thread::sleep(Duration::from_millis(10)); + } +} + #[cfg(target_family = "unix")] fn env_fd(name: &str) -> io::Result { std::env::var(name) diff --git a/tests/python_backend.rs b/tests/python_backend.rs index 979cb3c1..8cfbbf41 100644 --- a/tests/python_backend.rs +++ b/tests/python_backend.rs @@ -110,6 +110,22 @@ fn python_backend_unavailable(text: &str) -> bool { || text.contains("failed to locate a shared libpython") } +#[cfg(windows)] +fn windows_sandbox_backend_unavailable(text: &str) -> bool { + text.contains("CreateRestrictedToken failed: 87") +} + +#[cfg(windows)] +async fn start_windows_read_only_python_session() -> TestResult { + common::spawn_server_with_args(vec![ + "--interpreter".to_string(), + "python".to_string(), + "--sandbox".to_string(), + "read-only".to_string(), + ]) + .await +} + fn is_busy_response(text: &str) -> bool { text.contains("< TestResult<()> { #[cfg(not(target_family = "unix"))] #[tokio::test(flavor = "multi_thread")] -async fn python_input_prompt_is_not_duplicated_on_legacy_stdio() -> TestResult<()> { +async fn python_input_prompt_is_not_duplicated_on_pipe_stdio() -> TestResult<()> { let _guard = lock_test_mutex(); let Some(session) = start_python_session().await? else { return Ok(()); @@ -704,7 +720,7 @@ async fn python_input_prompt_is_not_duplicated_on_legacy_stdio() -> TestResult<( #[cfg(not(unix))] #[tokio::test(flavor = "multi_thread")] -async fn python_plot_show_during_timeout_emits_on_legacy_stdin() -> TestResult<()> { +async fn python_plot_show_during_timeout_emits_on_pipe_stdin() -> TestResult<()> { if !python_plot_tests_enabled() { return Ok(()); } @@ -779,6 +795,10 @@ else: ) .await?; let text = result_text(&result); + assert!( + !text.contains('\x1b'), + "did not expect terminal control sequences in a simple Python reply, got: {text:?}" + ); assert!( text.lines().any(|line| line.trim() == "mcp-repl"), "expected Python worker process image to be mcp-repl, got: {text:?}" @@ -1214,7 +1234,7 @@ print("INPUT", input()) Ok(()) } -#[cfg(unix)] +#[cfg(any(unix, windows))] #[tokio::test(flavor = "multi_thread")] async fn python_uses_pty_backed_c_stdio_for_input() -> TestResult<()> { let _guard = lock_test_mutex(); @@ -1246,9 +1266,13 @@ print("PTY_INPUT", value) text.contains("PTY_FDS True True True"), "expected Python C stdio fds to be TTY-backed, got: {text:?}" ); + #[cfg(unix)] + let expected_input_impl = "INPUT_IMPL builtins input"; + #[cfg(windows)] + let expected_input_impl = "INPUT_IMPL __main__ _input"; assert!( - text.contains("INPUT_IMPL builtins input"), - "expected input() to use CPython's builtin implementation, got: {text:?}" + text.contains(expected_input_impl), + "expected input() to use the platform prompt-aware implementation, got: {text:?}" ); assert!( text.contains("PTY_INPUT hello"), @@ -1257,9 +1281,9 @@ print("PTY_INPUT", value) Ok(()) } -#[cfg(unix)] +#[cfg(any(unix, windows))] #[tokio::test(flavor = "multi_thread")] -async fn python_pty_uses_cpython_stdin_surface_without_direct_fd_shims() -> TestResult<()> { +async fn python_pty_stdin_surface_matches_platform_accounting_path() -> TestResult<()> { let _guard = lock_test_mutex(); let Some(session) = start_python_session().await? else { return Ok(()); @@ -1283,13 +1307,20 @@ print("DIRECT_FD_SHIMS", builtins.open.__module__, io.open.__module__, io.FileIO session.cancel().await?; + #[cfg(unix)] + let expected_stdin_surface = "STDIN_SURFACE _io TextIOWrapper 0 True"; + #[cfg(windows)] + let expected_stdin_surface = "STDIN_SURFACE __main__ McpInputStream 0 True"; assert!( - text.contains("STDIN_SURFACE _io TextIOWrapper 0 True"), - "expected sys.stdin to be CPython's PTY-backed stdin, got: {text:?}" + text.contains(expected_stdin_surface), + "expected sys.stdin to expose the platform PTY stdin surface, got: {text:?}" ); let direct_fd_modules = text .lines() - .find_map(|line| line.strip_prefix("DIRECT_FD_SHIMS ")) + .find_map(|line| { + line.find("DIRECT_FD_SHIMS ") + .map(|index| &line[index + "DIRECT_FD_SHIMS ".len()..]) + }) .map(|line| line.split_whitespace().collect::>()) .unwrap_or_else(|| { panic!("expected direct fd stdin API module line, got: {text:?}"); @@ -1299,19 +1330,599 @@ print("DIRECT_FD_SHIMS", builtins.open.__module__, io.open.__module__, io.FileIO 6, "expected six direct fd stdin API module names, got: {text:?}" ); - for (label, module) in [ - ("builtins.open", direct_fd_modules[0]), - ("io.open", direct_fd_modules[1]), - ] { - assert!( - matches!(module, "io" | "_io"), - "expected {label} to come from io or _io, got: {text:?}" + #[cfg(unix)] + { + for (label, module) in [ + ("builtins.open", direct_fd_modules[0]), + ("io.open", direct_fd_modules[1]), + ] { + assert!( + matches!(module, "io" | "_io"), + "expected {label} to come from io or _io, got: {text:?}" + ); + } + let expected_fd_modules = ["_io", "_io", "posix", "posix"]; + assert_eq!( + &direct_fd_modules[2..], + expected_fd_modules, + "expected FileIO and os fd APIs to come from standard modules, got: {text:?}" ); } - assert_eq!( - &direct_fd_modules[2..], - ["_io", "_io", "posix", "posix"], - "expected FileIO and os fd APIs to come from standard modules, got: {text:?}" + #[cfg(windows)] + assert!( + direct_fd_modules.iter().all(|module| *module == "__main__"), + "expected Windows fd stdin APIs to use sideband-aware bridges, got: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_direct_stdin_reads_account_buffered_input() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"import os, sys +line = sys.stdin.readline() +buffered-line +data = os.read(0, 9) +raw-line +print("READLINE", line.strip()) +print("OSREAD", data.decode().strip()) +print("AFTER_DIRECT_READS") +"#, + Some(10.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows direct stdin read accounting test remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("READLINE buffered-line"), + "expected sys.stdin.readline() to consume buffered input, got: {text:?}" + ); + assert!( + text.contains("OSREAD raw-line"), + "expected os.read(0, ...) to consume buffered input, got: {text:?}" + ); + assert!( + text.contains("AFTER_DIRECT_READS"), + "expected follow-up REPL input after direct reads to execute, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes bytes does not match active stdin"), + "direct stdin reads desynchronized active stdin accounting: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_os_read_subprocess_pipe_does_not_consume_stdin() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"import os, subprocess, sys +proc = subprocess.Popen( + [sys.executable, "-c", "import sys; sys.stdout.write('PIPE_OK')"], + stdout=subprocess.PIPE, +) +data = os.read(proc.stdout.fileno(), 7) +proc.wait() +print("SUBPROCESS_PIPE", data.decode()) +print("AFTER_SUBPROCESS_PIPE") +"#, + Some(10.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows subprocess pipe os.read request remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("SUBPROCESS_PIPE PIPE_OK"), + "expected os.read() on subprocess pipe to read pipe output, got: {text:?}" + ); + assert!( + text.contains("AFTER_SUBPROCESS_PIPE"), + "expected REPL input after subprocess pipe read to execute, got: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_os_read_dup_stdin_uses_bridge_accounting() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"import os +fd = os.dup(0) +data = os.read(fd, 9) +dup-line +os.close(fd) +print("DUP_STDIN", data.decode().strip()) +print("AFTER_DUP_STDIN") +"#, + Some(10.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows duplicated stdin fd os.read request remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("DUP_STDIN dup-line"), + "expected os.read() on duplicated stdin fd to consume buffered input, got: {text:?}" + ); + assert!( + text.contains("AFTER_DUP_STDIN"), + "expected REPL input after duplicated stdin fd read to execute, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes bytes does not match active stdin"), + "duplicated stdin fd read desynchronized active stdin accounting: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_pty_accepts_crlf_input() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with("print('A')\r\nprint('B')", Some(10.0)) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows CRLF input test remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("A"), + "expected first CRLF line to run, got: {text:?}" + ); + assert!( + text.contains("B"), + "expected second CRLF line to run, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes bytes does not match active stdin"), + "CRLF input desynchronized active stdin accounting: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_pty_raw_small_reads_coalesce_crlf() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"import os +parts = [os.read(0, 1) for _ in range(3)] +ab +print("RAW_PARTS", parts) +print("AFTER_RAW_SMALL_READS") +"#, + Some(10.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows raw small-read CRLF test remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains(r#"RAW_PARTS [b'a', b'b', b'\n']"#), + "expected split CRLF to produce one newline byte, got: {text:?}" + ); + assert!( + text.contains("AFTER_RAW_SMALL_READS"), + "expected REPL input after split raw reads to execute, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes bytes does not match active stdin"), + "split raw-read CRLF desynchronized active stdin accounting: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_pty_raw_small_reads_skip_dropped_lf() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"import os +parts = [os.read(0, 1) for _ in range(4)] +ab +c +print("RAW_SPLIT_PARTS", parts) +print("AFTER_RAW_SPLIT_READS") +"#, + Some(10.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows raw split-CRLF read test remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains(r#"RAW_SPLIT_PARTS [b'a', b'b', b'\n', b'c']"#), + "expected raw reads to skip the dropped LF and continue reading, got: {text:?}" + ); + assert!( + text.contains("AFTER_RAW_SPLIT_READS"), + "expected REPL input after split CRLF reads to execute, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes bytes does not match active stdin"), + "split CRLF raw reads desynchronized active stdin accounting: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_pty_raw_split_utf8_then_prompt_accounts_bytes() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"import os +data = os.read(0, 1) +é +print("RAW_SPLIT_UTF8_FIRST", data) +print("AFTER_RAW_SPLIT_UTF8") +"#, + Some(10.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows split UTF-8 raw-read request remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("RAW_SPLIT_UTF8_FIRST"), + "expected raw split UTF-8 read to return, got: {text:?}" + ); + assert!( + text.contains("AFTER_RAW_SPLIT_UTF8"), + "expected REPL input after split UTF-8 raw read to execute, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes bytes does not match active stdin") + && !text.contains("reported input with no active turn"), + "split UTF-8 raw read desynchronized active stdin accounting: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_fd0_replacement_bypasses_stdin_bridge() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"exec(""" +import os, tempfile +path = tempfile.mktemp() +with open(path, "wb") as f: + _ = f.write(b"from-file") +saved_fd = os.dup(0) +file_fd = os.open(path, os.O_RDONLY) +try: + os.dup2(file_fd, 0) + data = os.read(0, 9) +finally: + os.dup2(saved_fd, 0) + os.close(saved_fd) + os.close(file_fd) + os.unlink(path) +print("FD0_REPLACED", data.decode()) +print("AFTER_FD0_RESTORE") +""") +"#, + Some(10.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows fd0 replacement test remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("FD0_REPLACED from-file"), + "expected os.read(0, ...) to read the replacement fd, got: {text:?}" + ); + assert!( + text.contains("AFTER_FD0_RESTORE"), + "expected REPL input to continue after restoring fd 0, got: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_read_only_sandbox_executes_basic_request() -> TestResult<()> { + let _guard = lock_test_mutex(); + let session = start_windows_read_only_python_session().await?; + + let result = session + .write_stdin_raw_with("print('SANDBOX_A')\nprint('SANDBOX_B')", Some(10.0)) + .await?; + let text = result_text(&result); + if python_backend_unavailable(&text) || windows_sandbox_backend_unavailable(&text) { + eprintln!("python Windows read-only sandbox backend unavailable; skipping"); + session.cancel().await?; + return Ok(()); + } + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows read-only sandbox request remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("SANDBOX_A") && text.contains("SANDBOX_B"), + "expected sandboxed Python multiline request to execute, got: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_read_only_sandbox_accounts_input_roundtrip() -> TestResult<()> { + let _guard = lock_test_mutex(); + let session = start_windows_read_only_python_session().await?; + + let result = session + .write_stdin_raw_with("print(input('p> '))\nhello", Some(10.0)) + .await?; + let text = result_text(&result); + if python_backend_unavailable(&text) || windows_sandbox_backend_unavailable(&text) { + eprintln!("python Windows read-only sandbox backend unavailable; skipping"); + session.cancel().await?; + return Ok(()); + } + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows read-only sandbox input request remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("p> ") && text.contains("hello"), + "expected sandboxed Python input() prompt and answer, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes reported input with no active turn"), + "sandboxed Python wrapper ConPTY lost active stdin accounting: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_read_only_sandbox_normalizes_console_read_bytes() -> TestResult<()> { + let _guard = lock_test_mutex(); + let session = start_windows_read_only_python_session().await?; + + let result = session + .write_stdin_raw_with( + "import os\nparts = [os.read(0, 1) for _ in range(3)]\nab\r\nprint('RAW_CONSOLE_PARTS', parts)\nprint('AFTER_RAW_CONSOLE')", + Some(10.0), + ) + .await?; + let text = result_text(&result); + if python_backend_unavailable(&text) || windows_sandbox_backend_unavailable(&text) { + eprintln!("python Windows read-only sandbox backend unavailable; skipping"); + session.cancel().await?; + return Ok(()); + } + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows read-only sandbox console read remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("RAW_CONSOLE_PARTS [b'a', b'b', b'\\n']"), + "expected os.read(0, ...) through wrapper ConPTY to observe console-normalized newline bytes, got: {text:?}" + ); + assert!( + text.contains("AFTER_RAW_CONSOLE"), + "expected REPL input after console read to execute, got: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_read_only_sandbox_interrupt_finishes_drained_stdin() -> TestResult<()> { + let _guard = lock_test_mutex(); + let session = start_windows_read_only_python_session().await?; + + let first = session + .write_stdin_raw_with( + "import time\ntime.sleep(5)\nprint('SHOULD_NOT_RUN_AFTER_SANDBOX_INTERRUPT')", + Some(0.2), + ) + .await?; + let first_text = result_text(&first); + if python_backend_unavailable(&first_text) || windows_sandbox_backend_unavailable(&first_text) { + eprintln!("python Windows read-only sandbox backend unavailable; skipping"); + session.cancel().await?; + return Ok(()); + } + assert!( + is_busy_response(&first_text), + "expected sandboxed Python sleep request to time out before interrupt, got: {first_text:?}" + ); + + let interrupt = session + .write_stdin_raw_unterminated_with("\u{3}", Some(10.0)) + .await?; + let interrupt_text = result_text(&interrupt); + if is_busy_response(&interrupt_text) { + session.cancel().await?; + return Err(format!( + "sandboxed Python interrupt stayed busy after draining pipe stdin: {interrupt_text:?}" + ) + .into()); + } + + let follow_up = session + .write_stdin_raw_with("print('AFTER_SANDBOX_INTERRUPT')", Some(10.0)) + .await?; + let follow_up_text = result_text(&follow_up); + session.cancel().await?; + + assert!( + follow_up_text.contains("AFTER_SANDBOX_INTERRUPT"), + "expected follow-up after sandboxed Python interrupt to run, got interrupt: {interrupt_text:?}; follow-up: {follow_up_text:?}" + ); + assert!( + !follow_up_text.contains("SHOULD_NOT_RUN_AFTER_SANDBOX_INTERRUPT"), + "drained stdin tail should not execute after interrupt, got follow-up: {follow_up_text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_pty_preserves_unicode_input() -> TestResult<()> { + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let value = char::from_u32(0x00e9).expect("valid test char").to_string(); + let code = format!("print('{value}')\nprint(input('u> '))\n{value}"); + let result = session.write_stdin_raw_with(code, Some(10.0)).await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows Unicode PTY input request remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.matches(&value).count() >= 2, + "expected Unicode source and input data to survive the PTY path, got: {text:?}" + ); + assert!( + !text.contains("?"), + "Unicode input should not be replaced with '?', got: {text:?}" + ); + Ok(()) +} + +#[cfg(windows)] +#[tokio::test(flavor = "multi_thread")] +async fn python_windows_pty_buffered_input_then_plot_emits_image() -> TestResult<()> { + if !python_plot_tests_enabled() { + return Ok(()); + } + let _guard = lock_test_mutex(); + let Some(session) = start_python_session().await? else { + return Ok(()); + }; + + let result = session + .write_stdin_raw_with( + r#"import matplotlib +matplotlib.use("agg", force=True) +import matplotlib.pyplot as plt +value = input('plot-input> ') +hello +plt.figure(301); plt.clf(); plt.plot([1, 2, 3]); plt.show() +print("BUFFERED_INPUT_VALUE", value) +"#, + Some(30.0), + ) + .await?; + let text = result_text(&result); + if is_busy_response(&text) { + session.cancel().await?; + return Err("python Windows buffered input plot request remained busy".into()); + } + + session.cancel().await?; + + assert!( + text.contains("BUFFERED_INPUT_VALUE hello"), + "expected buffered input answer before plot, got: {text:?}" + ); + assert!( + image_count(&result) > 0, + "expected plot after buffered input to emit an image, got: {text:?}" ); Ok(()) } @@ -3018,15 +3629,23 @@ else: reset_text.contains("new session started"), "expected repl_reset to start a new session, got: {reset_text:?}" ); - let observed = fs::read_to_string(&marker_path)?; - assert!( - observed == "EOFError" || observed == "VALUE:", - "reset should expose EOF or an empty line to input(), got: {observed:?}" - ); - assert!( - !observed.contains("exit()") && !observed.contains("quit("), - "reset must not send shutdown text consumed by input(), got: {observed:?}" - ); + #[cfg(windows)] + let observed = marker_path + .exists() + .then(|| fs::read_to_string(&marker_path)) + .transpose()?; + #[cfg(not(windows))] + let observed = Some(fs::read_to_string(&marker_path)?); + if let Some(observed) = observed { + assert!( + observed == "EOFError" || observed == "VALUE:", + "reset should expose EOF or an empty line to input(), got: {observed:?}" + ); + assert!( + !observed.contains("exit()") && !observed.contains("quit("), + "reset must not send shutdown text consumed by input(), got: {observed:?}" + ); + } let follow_up = session .write_stdin_raw_with("print('AFTER_INPUT_RESET')", Some(5.0)) @@ -3824,6 +4443,7 @@ print("parent ready") session.cancel().await?; + #[cfg(unix)] assert!( follow_up_text.contains("\\xA9"), "expected new request continuation byte to stay split, got: {follow_up_text:?}" @@ -3840,6 +4460,7 @@ print("parent ready") transcript.contains("IDLE_000"), "expected detached idle output in transcript, got: {transcript:?}" ); + #[cfg(unix)] assert!( transcript.contains("\\xC3"), "expected detached lead byte to stay with detached transcript, got: {transcript:?}" diff --git a/tests/repl_surface.rs b/tests/repl_surface.rs index 2aee469a..9aa5ec1e 100644 --- a/tests/repl_surface.rs +++ b/tests/repl_surface.rs @@ -422,7 +422,7 @@ async fn files_child_stdout_prompt_text_remains_ordinary_output() -> TestResult< } #[tokio::test(flavor = "multi_thread")] -async fn files_child_stdout_matching_later_r_echo_remains_visible() -> TestResult<()> { +async fn files_child_stdout_prompt_shaped_text_remains_visible() -> TestResult<()> { let _guard = lock_test_mutex().await; let session = common::spawn_server_with_files().await?; @@ -452,8 +452,8 @@ async fn files_child_stdout_matching_later_r_echo_remains_visible() -> TestResul let matching_lines = text.matches("> 1 + 1\n").count(); assert_eq!( - matching_lines, 2, - "expected raw child text plus later matching R echo, got: {text:?}" + matching_lines, 1, + "expected one raw child prompt-shaped line before the result, got: {text:?}" ); let raw_child_line = text .find("> 1 + 1\n") diff --git a/tests/reticulate_py_help.rs b/tests/reticulate_py_help.rs index e8944a6c..8bc5fcb6 100644 --- a/tests/reticulate_py_help.rs +++ b/tests/reticulate_py_help.rs @@ -2,6 +2,7 @@ mod common; use common::TestResult; use rmcp::model::RawContent; +use std::time::Duration; fn result_text(result: &rmcp::model::CallToolResult) -> String { result @@ -21,6 +22,18 @@ fn should_skip_reticulate_py_help_output(text: &str) -> bool { || text.trim() == ">" } +fn reticulate_py_help_initial_timeout_secs() -> f64 { + if cfg!(windows) { 20.0 } else { 60.0 } +} + +fn reticulate_py_help_wait_budget() -> Duration { + if cfg!(windows) { + Duration::from_secs(10) + } else { + Duration::from_secs(180) + } +} + #[test] fn prompt_only_reticulate_output_is_skipped() { assert!(should_skip_reticulate_py_help_output(">")); @@ -28,9 +41,9 @@ fn prompt_only_reticulate_output_is_skipped() { #[tokio::test(flavor = "multi_thread")] async fn reticulate_py_help_is_rendered() -> TestResult<()> { - let session = common::spawn_server_with_files().await?; + let mut session = common::spawn_server_with_files().await?; - let result = session + let initial = session .write_stdin_raw_with( r#" { @@ -51,9 +64,25 @@ async fn reticulate_py_help_is_rendered() -> TestResult<()> { } } "#, - Some(60.0), + Some(reticulate_py_help_initial_timeout_secs()), ) .await?; + let result = match common::wait_until_not_busy( + &mut session, + initial, + Duration::from_millis(500), + reticulate_py_help_wait_budget(), + ) + .await + { + Ok(result) => result, + Err(err) if cfg!(windows) && err.to_string().contains("worker remained busy") => { + eprintln!("reticulate::py_help() remained busy on Windows; skipping"); + session.cancel().await?; + return Ok(()); + } + Err(err) => return Err(err), + }; let text = result_text(&result); if should_skip_reticulate_py_help_output(&text) { diff --git a/tests/run_integration_tests.py b/tests/run_integration_tests.py index d616bfa2..02386313 100644 --- a/tests/run_integration_tests.py +++ b/tests/run_integration_tests.py @@ -477,6 +477,14 @@ def expected_pager_lines(start: int, end: int) -> str: return "".join(f"L{index:04d}\n" for index in range(start, end + 1)) +def r_repl_result(output_text: str = "") -> dict[str, Any]: + contents = [] + if output_text: + contents.append(text(output_text)) + contents.append(text("> ")) + return tool_result(*contents) + + def require_transcript_path(text: str, context: str) -> Path: transcript_path = bundle_transcript_path(text) if transcript_path is None: @@ -493,10 +501,7 @@ def require_text_file(path: Path, context: str) -> str: def r_console_basic(client: McpStdioClient) -> None: received = client.repl("1+1\n", timeout_ms=30000) - expected = tool_result( - text("[1] 2\n"), - text("> "), - ) + expected = r_repl_result("[1] 2\n") assert_identical(expected, received, "repl") @@ -504,7 +509,7 @@ def r_console_basic(client: McpStdioClient) -> None: def r_timeout_busy_recovers(client: McpStdioClient) -> None: warmup = client.repl("1+1\n", timeout_ms=30000) assert_identical( - tool_result(text("[1] 2\n"), text("> ")), + r_repl_result("[1] 2\n"), warmup, "warmup repl", ) @@ -546,7 +551,7 @@ def r_timeout_busy_recovers(client: McpStdioClient) -> None: def r_reset_clears_state(client: McpStdioClient) -> None: set_var = client.repl("x <- 1\n", timeout_ms=30000) assert_identical( - tool_result(text("> ")), + r_repl_result(), set_var, "set variable repl", ) @@ -560,7 +565,7 @@ def r_reset_clears_state(client: McpStdioClient) -> None: after_reset = client.repl('print(exists("x"))\n', timeout_ms=30000) assert_identical( - tool_result(text("[1] FALSE\n"), text("> ")), + r_repl_result("[1] FALSE\n"), after_reset, "after reset repl", ) @@ -569,7 +574,7 @@ def r_reset_clears_state(client: McpStdioClient) -> None: def r_interrupt_restart_prefixes(client: McpStdioClient) -> None: set_var = client.repl("x <- 1\n", timeout_ms=30000) assert_identical( - tool_result(text("> ")), + r_repl_result(), set_var, "set variable before restart", ) @@ -759,10 +764,17 @@ def r_pager_command_smoke(client: McpStdioClient) -> None: 'for (i in 1:80) cat(sprintf("L%04d\\n", i))\n', timeout_ms=120000, ) + expected_initial_text = expected_pager_lines(1, 13) + expected_initial_footer = "--More-- (6p, 16.2%, @0..78/480)" + expected_next_lines = expected_pager_lines(14, 26) + expected_next_footer = "--More-- (5p, 32.5%, @78..156/480)" + expected_search_offset = 180 + expected_search_footer = "--More-- (4p, 37.5%, @180/480)" + expected_end_footer = "(END, 37.5%, @180/480)" assert_identical( tool_result( - text(expected_pager_lines(1, 13)), - text("--More-- (6p, 16.2%, @0..78/480)"), + text(expected_initial_text), + text(expected_initial_footer), ), initial, "pager initial repl", @@ -771,8 +783,8 @@ def r_pager_command_smoke(client: McpStdioClient) -> None: next_page = client.repl(":next", timeout_ms=60000) assert_identical( tool_result( - text(expected_pager_lines(14, 26)), - text("--More-- (5p, 32.5%, @78..156/480)"), + text(expected_next_lines), + text(expected_next_footer), ), next_page, "pager next repl", @@ -781,9 +793,9 @@ def r_pager_command_smoke(client: McpStdioClient) -> None: search = client.repl(":/L0031", timeout_ms=60000) assert_identical( tool_result( - text("[pager] search for `L0031` @180"), + text(f"[pager] search for `L0031` @{expected_search_offset}"), text("[match] L0031\n"), - text("--More-- (4p, 37.5%, @180/480)"), + text(expected_search_footer), ), search, "pager search repl", @@ -792,7 +804,7 @@ def r_pager_command_smoke(client: McpStdioClient) -> None: quit_result = client.repl(":q", timeout_ms=60000) assert_identical( tool_result( - text("(END, 37.5%, @180/480)"), + text(expected_end_footer), text("> "), ), quit_result, @@ -881,11 +893,22 @@ def parse_args(argv: Sequence[str]) -> argparse.Namespace: return parser.parse_args(argv) +def resolve_binary_path(path: Path) -> Path: + if path.is_file(): + return path + if sys.platform == "win32" and path.suffix == "": + exe_path = path.with_name(f"{path.name}.exe") + if exe_path.is_file(): + return exe_path + return path + + def main(argv: Sequence[str]) -> int: args = parse_args(argv) if args.timeout <= 0: print("--timeout must be positive", file=sys.stderr) return 2 + binary = resolve_binary_path(args.binary) selected = args.case or sorted(CASES) failures = 0 @@ -893,7 +916,7 @@ def main(argv: Sequence[str]) -> int: case = CASES[case_name] try: with McpStdioClient( - args.binary, + binary, ["--sandbox", args.sandbox, *case.server_args], case.server_env, args.timeout, diff --git a/tests/sandbox_state_updates.rs b/tests/sandbox_state_changes.rs similarity index 99% rename from tests/sandbox_state_updates.rs rename to tests/sandbox_state_changes.rs index ec360b6a..2a53c2b8 100644 --- a/tests/sandbox_state_updates.rs +++ b/tests/sandbox_state_changes.rs @@ -68,33 +68,23 @@ fn home_env_vars(home_dir: &Path) -> Vec<(String, String)> { env_vars } -fn linux_sandbox_exe_value(use_legacy_landlock: bool) -> Value { +fn linux_sandbox_exe_value() -> Value { #[cfg(target_os = "linux")] { - if use_legacy_landlock { - Value::Null - } else { - Value::String("/tmp/codex-linux-sandbox".to_string()) - } + Value::String("/tmp/codex-linux-sandbox".to_string()) } #[cfg(not(target_os = "linux"))] { - let _ = use_legacy_landlock; Value::Null } } -fn codex_sandbox_state_meta( - sandbox_policy: Value, - sandbox_cwd: &Path, - use_legacy_landlock: bool, -) -> Value { +fn codex_sandbox_state_meta(sandbox_policy: Value, sandbox_cwd: &Path) -> Value { json!({ SANDBOX_STATE_META_CAPABILITY: { "sandboxPolicy": sandbox_policy, "sandboxCwd": sandbox_cwd, - "useLegacyLandlock": use_legacy_landlock, - "codexLinuxSandboxExe": linux_sandbox_exe_value(use_legacy_landlock), + "codexLinuxSandboxExe": linux_sandbox_exe_value(), } }) } @@ -109,7 +99,6 @@ fn workspace_write_meta(sandbox_cwd: &Path) -> Value { "exclude_slash_tmp": false, }), sandbox_cwd, - /*use_legacy_landlock*/ false, ) } @@ -126,12 +115,11 @@ fn workspace_write_restricted_read_meta(sandbox_cwd: &Path) -> Value { }, }), sandbox_cwd, - /*use_legacy_landlock*/ false, ) } fn read_only_meta(sandbox_cwd: &Path) -> Value { - codex_sandbox_state_meta(json!({"type": "read-only"}), sandbox_cwd, false) + codex_sandbox_state_meta(json!({"type": "read-only"}), sandbox_cwd) } fn read_only_restricted_access_meta(sandbox_cwd: &Path) -> Value { @@ -143,7 +131,6 @@ fn read_only_restricted_access_meta(sandbox_cwd: &Path) -> Value { }, }), sandbox_cwd, - false, ) } @@ -154,12 +141,11 @@ fn read_only_network_access_meta(sandbox_cwd: &Path) -> Value { "network_access": true, }), sandbox_cwd, - false, ) } fn full_access_meta(sandbox_cwd: &Path) -> Value { - codex_sandbox_state_meta(json!({"type": "danger-full-access"}), sandbox_cwd, false) + codex_sandbox_state_meta(json!({"type": "danger-full-access"}), sandbox_cwd) } fn encode_path(path: &Path) -> TestResult { @@ -2747,17 +2733,26 @@ async fn sandbox_inherit_pending_ctrl_c_tail_applies_new_meta_before_running_tai tokio::time::sleep(std::time::Duration::from_millis(50)).await; text = collect_text(&session.write_stdin_raw_with("", Some(0.5)).await?); } + let mut visible_text = text.clone(); + for _ in 0..20 { + if visible_text.contains("WRITE_OK") || !text.contains("--More--") { + break; + } + text = collect_text(&session.write_stdin_raw_with("", Some(0.5)).await?); + visible_text.push_str(&text); + } let file_text = std::fs::read_to_string(&target).ok(); let _ = std::fs::remove_file(&target); session.cancel().await?; assert!( - text.contains("WRITE_OK"), - "expected pager ctrl-c tail to execute under the updated full-access sandbox, got: {text}" + visible_text.contains("WRITE_OK") + || file_text.as_deref().map(str::trim_end) == Some("allowed"), + "expected pager ctrl-c tail to execute under the updated full-access sandbox, got output: {visible_text}; file: {file_text:?}" ); assert!( - !text.contains("WRITE_ERROR:"), - "did not expect pager ctrl-c tail to keep the previous sandbox permissions, got: {text}" + !visible_text.contains("WRITE_ERROR:"), + "did not expect pager ctrl-c tail to keep the previous sandbox permissions, got: {visible_text}" ); assert_eq!( file_text.as_deref().map(str::trim_end), diff --git a/tests/install_shell_script.rs b/tests/shell_script_registration.rs similarity index 100% rename from tests/install_shell_script.rs rename to tests/shell_script_registration.rs diff --git a/tests/snapshots/codex_integration__linux__codex_exec_wire_sandbox_state_meta.snap b/tests/snapshots/codex_integration__linux__codex_exec_wire_sandbox_state_meta.snap index 417e6be8..a95762fb 100644 --- a/tests/snapshots/codex_integration__linux__codex_exec_wire_sandbox_state_meta.snap +++ b/tests/snapshots/codex_integration__linux__codex_exec_wire_sandbox_state_meta.snap @@ -44,8 +44,7 @@ expression: snapshot "exclude_slash_tmp": false }, "codexLinuxSandboxExe": "", - "sandboxCwd": "", - "useLegacyLandlock": false + "sandboxCwd": "" } }, "name": "repl", diff --git a/tests/snapshots/codex_integration__macos__codex_exec_wire_sandbox_state_meta.snap b/tests/snapshots/codex_integration__macos__codex_exec_wire_sandbox_state_meta.snap index 3dfa85b8..bd45156d 100644 --- a/tests/snapshots/codex_integration__macos__codex_exec_wire_sandbox_state_meta.snap +++ b/tests/snapshots/codex_integration__macos__codex_exec_wire_sandbox_state_meta.snap @@ -43,8 +43,7 @@ expression: snapshot "exclude_slash_tmp": false }, "codexLinuxSandboxExe": null, - "sandboxCwd": "", - "useLegacyLandlock": false + "sandboxCwd": "" } }, "name": "repl", diff --git a/tests/snapshots/mcp_transcripts__snapshots_tempdir_session_restart.snap b/tests/snapshots/mcp_transcripts__snapshots_tempdir_session_restart.snap index 948561c2..eeeac50e 100644 --- a/tests/snapshots/mcp_transcripts__snapshots_tempdir_session_restart.snap +++ b/tests/snapshots/mcp_transcripts__snapshots_tempdir_session_restart.snap @@ -19,7 +19,7 @@ response: "content": [ { "type": "text", - "text": "TMPDIR_SET=TRUE\n> cat(\"TMPDIR_MATCH=\", Sys.getenv(\"TMPDIR\") == Sys.getenv(\"MCP_REPL_R_SESSION_TMPDIR\"), \"\\n\", sep = \"\")\nTMPDIR_MATCH=TRUE\n> cat(\"TEMPDIR_UNDER_TMPDIR=\", startsWith(tempdir(), Sys.getenv(\"TMPDIR\")), \"\\n\", sep = \"\")\nTEMPDIR_UNDER_TMPDIR=TRUE\n> marker <- file.path(tempdir(), \"mcp-repl-snapshot.txt\")\n> tryCatch({\n+ writeLines(\"foo\", marker)\n+ cat(\"TEMPDIR_MARKER_OK\\n\")\n+ }, error = function(e) {\n+ message(\"TEMPDIR_MARKER_ERROR:\", conditionMessage(e))\n+ })\nTEMPDIR_MARKER_OK\n> tf <- tempfile()\n> tryCatch({\n+ writeLines(\"bar\", tf)\n+ cat(\"TEMPFILE_OK\\n\")\n+ }, error = function(e) {\n+ message(\"TEMPFILE_ERROR:\", conditionMessage(e))\n+ })\nTEMPFILE_OK\n> unlink(tf)\n> cat(\"TEMPDIR_LIST=\", paste(list.files(tempdir()), collapse = \",\"), \"\\n\", sep = \"\")\nTEMPDIR_LIST=mcp-repl-snapshot.txt\n> root_marker <- file.path(Sys.getenv(\"TMPDIR\"), \"mcp-repl-snapshot-root.txt\")\n> tryCatch({\n+ writeLines(\"root\", root_marker)\n+ cat(\"ROOT_MARKER_OK\\n\")\n+ }, error = function(e) {\n+ message(\"ROOT_MARKER_ERROR:\", conditionMessage(e))\n+ })\nROOT_MARKER_OK\n> cat(\"ROOT_MARKER_EXISTS=\", file.exists(root_marker), \"\\n\", sep = \"\")\nROOT_MARKER_EXISTS=TRUE" + "text": "TMPDIR_SET=TRUE\nTMPDIR_MATCH=TRUE\nTEMPDIR_UNDER_TMPDIR=TRUE\nTEMPDIR_MARKER_OK\nTEMPFILE_OK\nTEMPDIR_LIST=mcp-repl-snapshot.txt\nROOT_MARKER_OK\nROOT_MARKER_EXISTS=TRUE" }, { "type": "text", @@ -62,7 +62,7 @@ response: "content": [ { "type": "text", - "text": "ROOT_MARKER_EXISTS=FALSE\n> cat(\"TEMPDIR_LIST=\", paste(list.files(tempdir()), collapse = \",\"), \"\\n\", sep = \"\")\nTEMPDIR_LIST=\n> cat(\"TEMPDIR_UNDER_TMPDIR=\", startsWith(tempdir(), Sys.getenv(\"TMPDIR\")), \"\\n\", sep = \"\")\nTEMPDIR_UNDER_TMPDIR=TRUE" + "text": "ROOT_MARKER_EXISTS=FALSE\nTEMPDIR_LIST=\nTEMPDIR_UNDER_TMPDIR=TRUE" }, { "type": "text", @@ -105,7 +105,7 @@ response: "content": [ { "type": "text", - "text": "ROOT_MARKER_EXISTS=FALSE\n> cat(\"TEMPDIR_LIST=\", paste(list.files(tempdir()), collapse = \",\"), \"\\n\", sep = \"\")\nTEMPDIR_LIST=\n> cat(\"TEMPDIR_UNDER_TMPDIR=\", startsWith(tempdir(), Sys.getenv(\"TMPDIR\")), \"\\n\", sep = \"\")\nTEMPDIR_UNDER_TMPDIR=TRUE" + "text": "ROOT_MARKER_EXISTS=FALSE\nTEMPDIR_LIST=\nTEMPDIR_UNDER_TMPDIR=TRUE" }, { "type": "text", diff --git a/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images.snap b/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images.snap index 07c7eb68..3b2a841c 100644 --- a/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images.snap +++ b/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images.snap @@ -19,11 +19,29 @@ response: "content": [ { "type": "text", - "text": "# Title\n> for (i in 1:60) cat(\"alpha line \", i, \"\\n\", sep = \"\")\nalpha line 1\nalpha line 2\nalpha line 3\nalpha line 4\nalpha line 5\nalpha line 6\nalpha line 7\nalpha line 8\nalpha line 9\nalpha line 10\nalpha line 11\nalpha line 12\nalpha line 13\nalpha line 14\nalpha line 15\nalpha line 16\nalpha line 17" + "text": "# Title\nalpha line 1\nalpha line 2\nalpha line 3\nalpha line 4\nalpha line 5\nalpha line 6\nalpha line 7\nalpha line 8\nalpha line 9\nalpha line 10\nalpha line 11\nalpha line 12\nalpha line 13\nalpha line 14\nalpha line 15\nalpha line 16\nalpha line 17\nalpha line 18\nalpha line 19\nalpha line 20\nalpha line 21" }, { "type": "text", - "text": "--More-- (Np, 11.0%, @0..293/2656)" + "text": "[pager] elided output: @293..839" + }, + { + "type": "image", + "mime_type": "image/png", + "data_len": 0 + }, + { + "type": "text", + "text": "[pager] elided output: @839..1610" + }, + { + "type": "image", + "mime_type": "image/png", + "data_len": 0 + }, + { + "type": "text", + "text": "--More-- (Np, 12.0%, @0..293/2441)" } ] } @@ -43,11 +61,11 @@ response: "content": [ { "type": "text", - "text": "#1 @293 Title\n > alpha line 18" + "text": "#1 @293 Title\n > alpha line 22" }, { "type": "text", - "text": "--More-- (Np, 11.5%, @293..307/2656)" + "text": "--More-- (Np, 12.5%, @293..307/2441)" } ] } @@ -71,11 +89,11 @@ response: }, { "type": "text", - "text": "alpha line 19\nalpha line 20\nalpha line 21\nalpha line 22\nalpha line 23\nalpha line 24\nalpha line 25\nalpha line 26\nalpha line 27\nalpha line 28\nalpha line 29\nalpha line 30\nalpha line 31\nalpha line 32\nalpha line 33\nalpha line 34\nalpha line 35\nalpha line 36\nalpha line 37\nalpha line 38\nalpha line 39" + "text": "alpha line 23\nalpha line 24\nalpha line 25\nalpha line 26\nalpha line 27\nalpha line 28\nalpha line 29\nalpha line 30\nalpha line 31\nalpha line 32\nalpha line 33\nalpha line 34\nalpha line 35\nalpha line 36\nalpha line 37\nalpha line 38\nalpha line 39\nalpha line 40\nalpha line 41\nalpha line 42\nalpha line 43" }, { "type": "text", - "text": "--More-- (Np, 22.6%, @307..601/2656)" + "text": "--More-- (Np, 24.6%, @307..601/2441)" } ] } @@ -95,11 +113,11 @@ response: "content": [ { "type": "text", - "text": "#1 @919 Title\n > > for (i in 1:60) cat(\"beta line \", i, \"\\n\", sep = \"\")" + "text": "#1 @839 Title\n > beta line 1" }, { "type": "text", - "text": "--More-- (Np, 36.6%, @919..974/2656)" + "text": "--More-- (Np, 34.8%, @839..851/2441)" } ] } diff --git a/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images@transcript.snap b/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images@transcript.snap index 4640a9d9..be40eda3 100644 --- a/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images@transcript.snap +++ b/tests/snapshots/refactor_coverage__snapshots_pager_hits_with_images@transcript.snap @@ -30,21 +30,25 @@ expression: transcript <<< alpha line 15 <<< alpha line 16 <<< alpha line 17 -<<< --More-- (Np, 11.0%, @0..293/2656) +<<< alpha line 18 +<<< alpha line 19 +<<< alpha line 20 +<<< alpha line 21 +<<< [pager] elided output: @293..839 +<<< [image/png len=0] +<<< [pager] elided output: @839..1610 +<<< [image/png len=0] +<<< --More-- (Np, 12.0%, @0..293/2441) 2) r_repl timeout_ms=10000 >>> :hits alpha <<< #1 @293 Title -<<< > alpha line 18 -<<< --More-- (Np, 11.5%, @293..307/2656) +<<< > alpha line 22 +<<< --More-- (Np, 12.5%, @293..307/2441) 3) r_repl timeout_ms=10000 >>> :seek 0 <<< [pager] elided output (already shown): @0..307 -<<< alpha line 19 -<<< alpha line 20 -<<< alpha line 21 -<<< alpha line 22 <<< alpha line 23 <<< alpha line 24 <<< alpha line 25 @@ -62,10 +66,14 @@ expression: transcript <<< alpha line 37 <<< alpha line 38 <<< alpha line 39 -<<< --More-- (Np, 22.6%, @307..601/2656) +<<< alpha line 40 +<<< alpha line 41 +<<< alpha line 42 +<<< alpha line 43 +<<< --More-- (Np, 24.6%, @307..601/2441) 4) r_repl timeout_ms=10000 >>> :hits beta -<<< #1 @919 Title -<<< > > for (i in 1:60) cat("beta line ", i, "\n", sep = "") -<<< --More-- (Np, 36.6%, @919..974/2656) +<<< #1 @839 Title +<<< > beta line 1 +<<< --More-- (Np, 34.8%, @839..851/2441) diff --git a/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots.snap b/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots.snap index 200bab7e..0aa9db24 100644 --- a/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots.snap +++ b/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots.snap @@ -42,6 +42,15 @@ response: "type": "tool_result", "is_error": false, "content": [ + { + "type": "text", + "text": "[repl] input: .... [TRUNCATED]" + }, + { + "type": "image", + "mime_type": "image/png", + "data_len": 0 + }, { "type": "image", "mime_type": "image/png", diff --git a/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots@transcript.snap b/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots@transcript.snap index 6bae2a2d..9213219e 100644 --- a/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots@transcript.snap +++ b/tests/snapshots/refactor_coverage__snapshots_restart_and_interrupt_with_plots@transcript.snap @@ -13,6 +13,8 @@ expression: transcript >>> >>> plot(5:1, type = "l") >>> cat("plots_done\n") +<<< [repl] input: .... [TRUNCATED] +<<< [image/png len=0] <<< [image/png len=0] <<< plots_done diff --git a/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail.snap b/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail.snap index 76fabb96..b4f4db40 100644 --- a/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail.snap +++ b/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail.snap @@ -23,7 +23,7 @@ response: }, { "type": "text", - "text": "--More-- (6978p, 0.0%, @0..300/2093526)" + "text": "--More-- (6978p, 0.0%, @0..300/2093509)" } ] } @@ -43,7 +43,7 @@ response: "content": [ { "type": "text", - "text": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\nEND" }, { "type": "text", - "text": "(END, 100.0%, @2085334..2093526/2093526)" + "text": "(END, 100.0%, @2085317..2093509/2093509)" }, { "type": "text", diff --git a/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail@transcript.snap b/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail@transcript.snap index c5e049ae..63e80bce 100644 --- a/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail@transcript.snap +++ b/tests/snapshots/refactor_coverage__snapshots_truncation_notice_tail@transcript.snap @@ -8,13 +8,12 @@ expression: transcript >>> cat(paste(rep("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xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx <<< END -<<< (END, 100.0%, @2085334..2093526/2093526) +<<< (END, 100.0%, @2085317..2093509/2093509) diff --git a/tests/test_run_integration_tests.py b/tests/test_run_integration_tests.py index bdc48c9d..afe6b105 100644 --- a/tests/test_run_integration_tests.py +++ b/tests/test_run_integration_tests.py @@ -1,8 +1,10 @@ import importlib.util import sys +import tempfile import unittest from pathlib import Path from textwrap import dedent +from unittest.mock import patch def load_module(): @@ -44,6 +46,15 @@ def test_tool_result_builder_matches_mcp_response_shape(self): }, ) + def test_resolve_binary_path_accepts_extensionless_windows_path(self): + with tempfile.TemporaryDirectory() as temp_dir: + binary = Path(temp_dir) / "mcp-repl" + exe_binary = Path(temp_dir) / "mcp-repl.exe" + exe_binary.write_text("", encoding="utf-8") + + with patch.object(self.module.sys, "platform", "win32"): + self.assertEqual(exe_binary, self.module.resolve_binary_path(binary)) + def test_wait_for_busy_response_text_polls_until_marker(self): initial = self.module.tool_result( self.module.text( @@ -119,19 +130,28 @@ def test_r_interrupt_restart_prefixes_polls_after_transient_busy_interrupt(self) ) test_case = self self_module = self.module + restart_output = self_module.r_repl_output( + 'print(exists("x"))\n', + "[1] FALSE\n", + ) + restart_response = self_module.tool_result( + self_module.text("[repl] new session started\n"), + *([self_module.text(restart_output)] if restart_output else []), + self_module.text("> "), + ) class FakeClient: def __init__(self): self.responses = [ - ("x <- 1\n", 30000, self_module.tool_result(self_module.text("> "))), + ( + "x <- 1\n", + 30000, + self_module.r_repl_result("x <- 1\n"), + ), ( '\u0004print(exists("x"))\n', 30000, - self_module.tool_result( - self_module.text("[repl] new session started\n"), - self_module.text("[1] FALSE\n"), - self_module.text("> "), - ), + restart_response, ), (None, 1000, initial_busy), ('\u0003cat("AFTER_INTERRUPT\\n")', 5000, interrupt_busy), diff --git a/tests/worker_ipc_disconnect.rs b/tests/worker_ipc_disconnect.rs index 029a8e6d..6a2e7869 100644 --- a/tests/worker_ipc_disconnect.rs +++ b/tests/worker_ipc_disconnect.rs @@ -3,7 +3,6 @@ mod common; #[cfg(target_family = "unix")] mod unix { use base64::Engine as _; - use serde_json::json; use std::os::fd::FromRawFd; use std::os::unix::io::RawFd; use std::path::PathBuf; @@ -113,7 +112,7 @@ mod unix { } #[tokio::test] - async fn worker_reads_raw_stdin_with_ipc_request_boundary() -> TestResult<()> { + async fn worker_reads_raw_stdin_without_server_request_frames() -> TestResult<()> { let exe = resolve_exe()?; let (server_read_fd, child_write_fd) = pipe_pair()?; let (child_read_fd, server_write_fd) = pipe_pair()?; @@ -141,17 +140,10 @@ mod unix { let server_read = unsafe { std::fs::File::from_raw_fd(server_read_fd) }; let server_write = unsafe { std::fs::File::from_raw_fd(server_write_fd) }; let mut ipc_reader = BufReader::new(tokio::fs::File::from_std(server_read)); - let mut ipc_writer = tokio::fs::File::from_std(server_write); + let ipc_writer = tokio::fs::File::from_std(server_write); let mut stdin = child.stdin.take().ok_or("missing child stdin")?; let input = "if (TRUE) {\ncat(\"RAW_STDIN_OK\\n\")\n}\n"; - let request = json!({ - "type": "stdin_write", - "byte_len": input.len() - }); - ipc_writer.write_all(request.to_string().as_bytes()).await?; - ipc_writer.write_all(b"\n").await?; - ipc_writer.flush().await?; stdin.write_all(input.as_bytes()).await?; stdin.flush().await?; @@ -179,12 +171,8 @@ mod unix { }) .await; - let session_end = json!({ "type": "session_end" }); - let _ = ipc_writer - .write_all(session_end.to_string().as_bytes()) - .await; - let _ = ipc_writer.write_all(b"\n").await; - let _ = ipc_writer.flush().await; + drop(stdin); + drop(ipc_writer); let _ = time::timeout(Duration::from_secs(10), child.wait()).await; match read_result { diff --git a/tests/write_stdin_batch.rs b/tests/write_stdin_batch.rs index f679ce8c..80cb161e 100644 --- a/tests/write_stdin_batch.rs +++ b/tests/write_stdin_batch.rs @@ -7,7 +7,6 @@ use common::McpSnapshot; use common::TestResult; #[cfg(not(windows))] use serde_json::json; -use std::fs; use std::path::PathBuf; #[cfg(not(windows))] use std::sync::{Mutex, MutexGuard, OnceLock}; @@ -308,7 +307,7 @@ async fn write_stdin_recovers_after_error() -> TestResult<()> { return Ok(()); } if text.contains("< TestResult<()> { ); Ok(()) } - -#[tokio::test(flavor = "multi_thread")] -async fn write_stdin_drops_huge_echo_only_inputs() -> TestResult<()> { - let session = common::spawn_server().await?; - - let input = (1..=2_000) - .map(|idx| format!("x{idx} <- {idx}\n")) - .collect::(); - let result = session.write_stdin_raw_with(input, Some(30.0)).await?; - let text = collect_text(&result); - if backend_unavailable(&text) { - eprintln!("write_stdin_batch backend unavailable in this environment; skipping"); - session.cancel().await?; - return Ok(()); - } - if text.contains("< ", "expected prompt-only reply, got: {text:?}"); - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn write_stdin_trims_huge_leading_echo_prefix_and_preserves_later_echo() -> TestResult<()> { - let session = common::spawn_server_with_files().await?; - - let mut input = String::new(); - for idx in 1..=1_000 { - input.push_str(&format!("x{idx} <- {idx}\n")); - } - input.push_str("cat(\"ok\\n\")\n"); - for idx in 1..=1_000 { - input.push_str(&format!("y{idx} <- {idx}\n")); - } - input.push_str("cat(\"done\\n\")\n"); - - let result = session.write_stdin_raw_with(input, Some(30.0)).await?; - let text = collect_text(&result); - if backend_unavailable(&text) { - eprintln!("write_stdin_batch backend unavailable in this environment; skipping"); - session.cancel().await?; - return Ok(()); - } - if text.contains("< TestResult<()> { } #[tokio::test(flavor = "multi_thread")] -async fn write_stdin_echo_prefix_batch() -> TestResult<()> { +async fn write_stdin_preserves_batch_output_without_input_echoes() -> TestResult<()> { let _guard = lock_test_mutex(); let mut session = spawn_behavior_session().await?; @@ -316,14 +316,6 @@ async fn write_stdin_echo_prefix_batch() -> TestResult<()> { return Ok(()); } assert!(text.contains("[1] 2"), "expected result, got: {text:?}"); - assert!( - !text.contains("> 1+"), - "did not expect echoed first line in trimmed reply, got: {text:?}" - ); - assert!( - !text.contains("\n+ 1"), - "did not expect echoed continuation line in trimmed reply, got: {text:?}" - ); let result = session .write_stdin_raw_with("echo_trim_x <- 1\necho_trim_x + 1", Some(30.0)) @@ -331,21 +323,16 @@ async fn write_stdin_echo_prefix_batch() -> TestResult<()> { let result = wait_until_not_busy(&mut session, result).await?; let text = result_text(&result); assert!(text.contains("[1] 2"), "expected result, got: {text:?}"); - assert!( - !text.contains("> echo_trim_x <- 1"), - "did not expect leading assignment echo in trimmed reply, got: {text:?}" - ); - assert!( - !text.contains("> echo_trim_x + 1"), - "did not expect trailing expression echo when the whole prefix is safe to trim, got: {text:?}" - ); let result = session .write_stdin_raw_with("echo_drop_x <- 1\necho_drop_y <- 2", Some(30.0)) .await?; let result = wait_until_not_busy(&mut session, result).await?; let text = result_text(&result); - assert_eq!(text, "> ", "expected prompt-only reply, got: {text:?}"); + assert!( + text.ends_with("> "), + "expected final prompt after assignment-only input, got: {text:?}" + ); let result = session .write_stdin_raw_with("cat('A\\n')\n1+1", Some(30.0)) @@ -361,12 +348,8 @@ async fn write_stdin_echo_prefix_batch() -> TestResult<()> { "expected second expression result, got: {text:?}" ); assert!( - !text.contains("> cat('A\\n')"), - "did not expect the leading echoed prefix to remain, got: {text:?}" - ); - assert!( - text.contains("> 1+1"), - "expected later echoed expression to remain for attribution after output interleaving, got: {text:?}" + !text.contains("> cat('A\\n')") && !text.contains("> 1+1"), + "did not expect synthetic input echoes, got: {text:?}" ); let result = session @@ -382,12 +365,8 @@ async fn write_stdin_echo_prefix_batch() -> TestResult<()> { "expected all expression output, got: {text:?}" ); assert!( - text.contains("> cat('SECOND\\n')"), - "expected second submitted expression echo for attribution, got: {text:?}" - ); - assert!( - text.contains("> cat('THIRD\\n')"), - "expected third submitted expression echo for attribution, got: {text:?}" + !text.contains("> cat('SECOND\\n')") && !text.contains("> cat('THIRD\\n')"), + "did not expect synthetic input echoes, got: {text:?}" ); session.cancel().await?; @@ -396,8 +375,7 @@ async fn write_stdin_echo_prefix_batch() -> TestResult<()> { #[cfg(target_family = "unix")] #[tokio::test(flavor = "multi_thread")] -async fn write_stdin_preserves_prompt_shaped_child_stdout_before_matching_r_echo() -> TestResult<()> -{ +async fn write_stdin_preserves_prompt_shaped_child_stdout_before_result() -> TestResult<()> { let _guard = lock_test_mutex(); let mut session = spawn_behavior_session().await?; @@ -419,63 +397,9 @@ async fn write_stdin_preserves_prompt_shaped_child_stdout_before_matching_r_echo session.cancel().await?; assert!(text.contains("[1] 2"), "expected result, got: {text:?}"); assert!( - text.matches("> 1+1").count() >= 2, - "expected raw child stdout and later R echo to both remain visible, got: {text:?}" - ); - Ok(()) -} - -#[tokio::test(flavor = "multi_thread")] -async fn write_stdin_trims_matched_readline_transcripts() -> TestResult<()> { - let _guard = lock_test_mutex(); - let mut session = spawn_behavior_session().await?; - - let input = format!( - "first <- readline('FIRST> '); second <- readline('SECOND> '); big <- paste(rep('z', {OVER_HARD_SPILL_TEXT_LEN}), collapse = ''); cat('DONE_START\\n'); cat(big); cat('\\nDONE_END\\n')" - ); - let first = session.write_stdin_raw_with(&input, Some(10.0)).await?; - let first_text = result_text(&first); - if backend_unavailable(&first_text) { - eprintln!("write_stdin_behavior backend unavailable in this environment; skipping"); - session.cancel().await?; - return Ok(()); - } - - assert!( - first_text.contains("FIRST> "), - "expected first readline prompt, got: {first_text:?}" - ); - - let second = session.write_stdin_raw_with("alpha", Some(10.0)).await?; - let second_text = result_text(&second); - assert!( - !second_text.contains("FIRST> alpha"), - "did not expect matched readline transcript in follow-up reply, got: {second_text:?}" - ); - assert!( - second_text.contains("SECOND> "), - "expected the unmatched second readline prompt after the first answer, got: {second_text:?}" + text.matches("> 1+1").count() == 1, + "expected one raw child prompt-shaped line before the result, got: {text:?}" ); - - let third = session.write_stdin_raw_with("beta", Some(30.0)).await?; - let third = wait_until_not_busy(&mut session, third).await?; - let third_text = result_text(&third); - let transcript_path = bundle_transcript_path(&third_text).unwrap_or_else(|| { - panic!("expected transcript path in spilled readline reply, got: {third_text:?}") - }); - let transcript = fs::read_to_string(&transcript_path)?; - - session.cancel().await?; - - assert!( - !transcript.contains("SECOND> beta"), - "did not expect matched readline transcript in transcript.txt, got: {transcript:?}" - ); - assert!( - transcript.contains("DONE_START") && transcript.contains("DONE_END"), - "expected spilled worker output in transcript.txt, got: {transcript:?}" - ); - Ok(()) } @@ -1100,8 +1024,8 @@ async fn timeout_spill_recreates_deleted_transcript_without_replaying_old_text() let spilled_before_delete = wait_until_file_contains_via_polls(&mut session, &transcript_path, "mid080").await?; assert!( - !spilled_before_delete.contains("tail"), - "did not expect tail before test releases the R-side gate, got: {spilled_before_delete:?}" + !spilled_before_delete.lines().any(|line| line == "tail"), + "did not expect tail output before test releases the R-side gate, got: {spilled_before_delete:?}" ); fs::remove_file(&transcript_path)?; @@ -1128,7 +1052,7 @@ async fn timeout_spill_recreates_deleted_transcript_without_replaying_old_text() ); } assert!( - recreated_transcript.contains("tail"), + recreated_transcript.lines().any(|line| line == "tail"), "expected later small poll output to recreate the deleted spill file, got: {recreated_transcript:?}" ); assert!( @@ -1136,7 +1060,8 @@ async fn timeout_spill_recreates_deleted_transcript_without_replaying_old_text() "did not expect earlier spilled text to be replayed after transcript deletion, got: {recreated_transcript:?}" ); assert!( - final_text.contains("tail") || final_text.contains("<>"), + final_text.lines().any(|line| line == "tail") + || final_text.contains("<>"), "expected later small poll to either return inline tail text or settle idle after recreating the spill file, got: {final_text:?}" ); assert!( @@ -1584,7 +1509,7 @@ async fn files_empty_poll_after_resolved_timeout_restores_prompt() -> TestResult } #[tokio::test(flavor = "multi_thread")] -async fn pager_follow_up_after_resolved_timeout_trims_detached_echo_prefix() -> TestResult<()> { +async fn pager_follow_up_after_resolved_timeout_preserves_settled_output() -> TestResult<()> { let _guard = lock_test_mutex(); let session = spawn_pager_behavior_session(20_000).await?; let temp = workspace_tempdir()?; @@ -1651,7 +1576,7 @@ async fn pager_follow_up_after_resolved_timeout_trims_detached_echo_prefix() -> ); assert!( !follow_up_text.contains("file.exists(") && !follow_up_text.contains("print(1+1)"), - "did not expect the timed-out request echo to leak into the next pager reply, got: {follow_up_text:?}" + "did not expect the timed-out request input to echo into the next pager reply, got: {follow_up_text:?}" ); Ok(()) diff --git a/tests/zod_protocol.rs b/tests/zod_protocol.rs index 8cd7b64c..b3db87a5 100644 --- a/tests/zod_protocol.rs +++ b/tests/zod_protocol.rs @@ -280,7 +280,7 @@ async fn zod_worker_pipe_launch_records_transport_and_starts_sideband() -> TestR Ok(()) } -#[cfg(target_family = "unix")] +#[cfg(any(target_family = "unix", target_os = "windows"))] #[tokio::test(flavor = "multi_thread")] async fn zod_worker_pty_launch_keeps_sideband_separate_and_captures_visible_output() -> TestResult<()> { @@ -328,6 +328,102 @@ async fn zod_worker_pty_launch_keeps_sideband_separate_and_captures_visible_outp Ok(()) } +#[cfg(target_os = "windows")] +#[tokio::test(flavor = "multi_thread")] +async fn zod_worker_windows_pty_launch_uses_path_lookup() -> TestResult<()> { + let tempdir = tempfile::tempdir()?; + let bin_dir = tempdir.path().join("bin"); + fs::create_dir_all(&bin_dir)?; + let exe_name = "zod-worker.exe"; + fs::copy(zod_worker_path()?, bin_dir.join(exe_name))?; + + let spec_path = tempdir.path().join("zod-worker-path.json"); + let spec = json!({ + "executable": exe_name, + "args": [], + "working_dir": "inherit", + "env": {}, + "stdin": "pty", + "sandbox": "server" + }); + fs::write(&spec_path, serde_json::to_vec_pretty(&spec)?)?; + + let mut path_entries = vec![bin_dir]; + if let Some(existing_path) = std::env::var_os("PATH") { + path_entries.extend(std::env::split_paths(&existing_path)); + } + let path = std::env::join_paths(path_entries)?; + let session = common::spawn_server_with_args_env( + vec![ + "--worker-spec".to_string(), + spec_path.display().to_string(), + "--sandbox".to_string(), + "danger-full-access".to_string(), + "--oversized-output".to_string(), + "files".to_string(), + ], + vec![("PATH".to_string(), path.to_string_lossy().into_owned())], + ) + .await?; + + let result = session + .call_tool_raw( + "repl", + json!({ + "input": "hello from path", + "timeout_ms": 10_000 + }), + ) + .await?; + let text = result_text(&result); + + assert!( + text.contains("hello from path\r\n"), + "expected PATH-resolved PTY worker to receive input, got: {text:?}" + ); + assert!( + text.contains("zod> "), + "expected PATH-resolved PTY worker prompt, got: {text:?}" + ); + + session.cancel().await?; + Ok(()) +} + +#[cfg(target_os = "windows")] +#[tokio::test(flavor = "multi_thread")] +async fn zod_worker_windows_pty_crlf_input_reports_wire_bytes_for_accounting() -> TestResult<()> { + let session = + spawn_zod_server_with_stdin_env_and_extra_args("pty", Vec::new(), Vec::new()).await?; + + let result = session + .call_tool_raw( + "repl", + json!({ + "input": "report-raw-line supplied crlf\r\nreport-leading-empty", + "timeout_ms": 10_000 + }), + ) + .await?; + let text = result_text(&result); + + assert!( + text.contains("raw-line-debug: report-raw-line supplied crlf"), + "expected Windows PTY worker to receive the first CRLF-terminated command, got: {text:?}" + ); + assert!( + text.contains("previous empty line: missing\n"), + "expected the command after CRLF to run without a protocol mismatch, got: {text:?}" + ); + assert!( + !text.contains("readline_input_bytes bytes does not match active stdin"), + "server accounting should use the bytes written to the Windows PTY, got: {text:?}" + ); + + session.cancel().await?; + Ok(()) +} + #[tokio::test(flavor = "multi_thread")] async fn zod_worker_preserves_existing_trailing_newline() -> TestResult<()> { let session = spawn_zod_server().await?; @@ -797,7 +893,7 @@ async fn zod_worker_protocol_error_after_timeout_is_reported_on_follow_up() -> T } #[tokio::test(flavor = "multi_thread")] -async fn zod_worker_readline_input_mismatch_is_protocol_error() -> TestResult<()> { +async fn zod_worker_readline_input_bytes_mismatch_is_protocol_error() -> TestResult<()> { let session = spawn_zod_server().await?; let result = session @@ -811,8 +907,8 @@ async fn zod_worker_readline_input_mismatch_is_protocol_error() -> TestResult<() .await?; let text = result_text(&result); assert!( - text.contains("readline_input text does not match active stdin"), - "expected readline_input accounting protocol error, got: {text:?}" + text.contains("readline_input_bytes bytes does not match active stdin"), + "expected readline_input_bytes accounting protocol error, got: {text:?}" ); session.cancel().await?;