22
33import { ensurePluginRuntime } from "@opencode/plugin/runtime"
44import { NodeRuntime , NodeServices } from "@effect/platform-node"
5- import { Cause , Effect } from "effect"
5+ import { Cause , Console , Effect } from "effect"
66import { getErrorReported } from "effect/Runtime"
77import { Commands } from "./commands/commands"
88import { Runtime } from "./framework/runtime"
@@ -17,86 +17,7 @@ import { Npm } from "@opencode/util/npm"
1717import { EffectFlock } from "@opencode/util/effect-flock"
1818import { Heap } from "./heap"
1919import { CpuProfile } from "./cpu-profile"
20- import fs from "node:fs"
21- import { format } from "node:util"
22-
23- // `packages/cli/bunfig.toml` loads `@opentui/solid/preload`, which leaves stdout
24- // and stderr nonblocking. A single write larger than one pipe buffer is then
25- // silently truncated past 65536 bytes (`--completions fish` is ~68 KiB, so
26- // its tail was lost when piped). Retrying full pipes in guarded chunks keeps
27- // that output from being lost.
28- // See https://github.com/anomalyco/opencode/issues/55525
29- const WRITE_CHUNK = 16 * 1024
30- const WRITE_WAIT = new Int32Array ( new SharedArrayBuffer ( 4 ) )
31- const WRITE_TIMEOUT_MS = 5_000
32- function writeAll ( fd : number , buffer : string | Buffer ) {
33- const data = typeof buffer === "string" ? Buffer . from ( buffer ) : buffer
34- const deadline = Date . now ( ) + WRITE_TIMEOUT_MS
35- let offset = 0
36- while ( offset < data . length ) {
37- let written : number | undefined
38- try {
39- written = fs . writeSync ( fd , data , offset , Math . min ( WRITE_CHUNK , data . length - offset ) )
40- } catch ( error ) {
41- // a closed or broken pipe (EPIPE etc.) is not retryable; only pause on
42- // a temporarily full one (EAGAIN) so the real exit code still wins.
43- if ( ( error as NodeJS . ErrnoException ) ?. code !== "EAGAIN" ) return
44- }
45- if ( written === undefined || written <= 0 ) {
46- if ( Date . now ( ) > deadline ) return
47- Atomics . wait ( WRITE_WAIT , 0 , 0 , 1 )
48- continue
49- }
50- offset += written
51- }
52- }
53- for ( const [ stream , fd ] of [
54- [ process . stdout , 1 ] ,
55- [ process . stderr , 2 ] ,
56- ] as const ) {
57- const write = stream . write . bind ( stream )
58- stream . write = ( ( chunk : string | Uint8Array , ...args : Array < string | ( ( ) => void ) > ) => {
59- if ( typeof chunk !== "string" && ! ( chunk instanceof Uint8Array ) )
60- return ( write as ( ...args : Array < unknown > ) => boolean ) ( chunk , ...args )
61- const lastArg = args . length > 0 ? args [ args . length - 1 ] : undefined
62- const hasCallback = typeof lastArg === "function"
63- const callback = hasCallback ? ( lastArg as ( ) => void ) : undefined
64- const encoding = typeof args [ 0 ] === "string" ? ( args [ 0 ] as BufferEncoding ) : "utf8"
65- const buffer = typeof chunk === "string" ? Buffer . from ( chunk , encoding ) : Buffer . from ( chunk )
66- if ( buffer . length <= WRITE_CHUNK ) return ( write as ( ...args : Array < unknown > ) => boolean ) ( chunk , ...args )
67- writeAll ( fd , buffer )
68- if ( callback ) queueMicrotask ( callback )
69- return true
70- } ) as typeof stream . write
71- }
72- const defaultConsole = globalThis . console
73-
74- // Console output does not go through the stream wrappers above under Bun.
75- console . log = ( ( ...args : Array < unknown > ) => {
76- const text = format ( ...args ) + "\n"
77- if ( text . length <= WRITE_CHUNK ) return defaultConsole . log ( ...args )
78- writeAll ( 1 , text )
79- } ) as typeof defaultConsole . log
80- console . info = ( ( ...args : Array < unknown > ) => {
81- const text = format ( ...args ) + "\n"
82- if ( text . length <= WRITE_CHUNK ) return defaultConsole . info ( ...args )
83- writeAll ( 1 , text )
84- } ) as typeof defaultConsole . info
85- console . debug = ( ( ...args : Array < unknown > ) => {
86- const text = format ( ...args ) + "\n"
87- if ( text . length <= WRITE_CHUNK ) return defaultConsole . debug ( ...args )
88- writeAll ( 1 , text )
89- } ) as typeof defaultConsole . debug
90- console . warn = ( ( ...args : Array < unknown > ) => {
91- const text = format ( ...args ) + "\n"
92- if ( text . length <= WRITE_CHUNK ) return defaultConsole . warn ( ...args )
93- writeAll ( 2 , text )
94- } ) as typeof defaultConsole . warn
95- console . error = ( ( ...args : Array < unknown > ) => {
96- const text = format ( ...args ) + "\n"
97- if ( text . length <= WRITE_CHUNK ) return defaultConsole . error ( ...args )
98- writeAll ( 2 , text )
99- } ) as typeof defaultConsole . error
20+ import { Output } from "./output"
10021
10122ensurePluginRuntime ( )
10223
@@ -194,6 +115,7 @@ Effect.gen(function* () {
194115 } ) . pipe ( Effect . andThen ( Effect . failCause ( cause ) ) ) ,
195116 ) ,
196117 Effect . annotateLogs ( { role : "cli" } ) ,
118+ Effect . provideService ( Console . Console , Output . console ) ,
197119 Effect . provide ( Config . layer ) ,
198120 Effect . provide ( Updater . layer ) ,
199121 Effect . provide (
@@ -216,6 +138,7 @@ Effect.gen(function* () {
216138 ) ,
217139 Effect . provide ( NodeServices . layer ) ,
218140 Effect . scoped ,
141+ Effect . ensuring ( Effect . promise ( Output . flush ) ) ,
219142 Effect . tap ( ( ) => Effect . sync ( ( ) => process . exit ( process . exitCode ?? 0 ) ) ) ,
220143 // runMain's default reporter logs the fatal cause to stdout. Write it to stderr instead: the
221144 // desktop and `Service.ensure` only capture stderr from `serve --service`, so this is the only
0 commit comments