@myrialabs/clopen 0.2.19 → 0.2.20

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -19,6 +19,7 @@ import { debug } from '$shared/utils/logger';
19
19
  const OPENCODE_HOST = '127.0.0.1';
20
20
  const DB_KEY = 'opencode.server.url';
21
21
  const HEALTH_TIMEOUT = 1500;
22
+ const SERVER_START_TIMEOUT = 30_000; // 30s — generous for slow devices
22
23
 
23
24
  let serverHandle: { url: string; close(): void } | null = null;
24
25
  let client: OpencodeClient | null = null;
@@ -26,10 +27,9 @@ let initPromise: Promise<void> | null = null;
26
27
  let ready = false;
27
28
  let ownsProcess = false;
28
29
 
29
- function resetState(): void {
30
+ function clearClientState(): void {
30
31
  client = null;
31
32
  ready = false;
32
- initPromise = null;
33
33
  serverHandle = null;
34
34
  ownsProcess = false;
35
35
  }
@@ -51,22 +51,36 @@ async function isServerAlive(url: string): Promise<boolean> {
51
51
  * If the server died (bun --watch restart, crash, etc.), state is reset and
52
52
  * init() is re-invoked — the same DB-check → reuse-or-spawn flow handles
53
53
  * recovery automatically.
54
+ *
55
+ * Race-condition guard: after any `await` (health check), we re-check
56
+ * `initPromise` before starting a new init — another caller may have
57
+ * already kicked one off during our async gap.
54
58
  */
55
59
  export async function ensureClient(): Promise<OpencodeClient> {
60
+ // Wait for any in-progress init first
61
+ if (initPromise) {
62
+ try { await initPromise; } catch { /* handled below */ }
63
+ if (client && ready) return client;
64
+ }
65
+
66
+ // Fast path: server is up and healthy
56
67
  if (client && ready && serverHandle) {
57
68
  if (await isServerAlive(serverHandle.url)) return client;
58
69
 
59
- // Server disconnected — purge stale DB entry and re-init
70
+ // Server disconnected — purge stale DB entry
71
+ // Do NOT clear initPromise — another caller may have started one
60
72
  debug.log('engine', 'Open Code server disconnected, recovering...');
61
73
  settingsQueries.delete(DB_KEY);
62
- resetState();
74
+ clearClientState();
63
75
  }
64
76
 
77
+ // Re-check after async gap — another caller may have started init
65
78
  if (initPromise) {
66
- await initPromise;
67
- return client!;
79
+ try { await initPromise; } catch { /* handled below */ }
80
+ if (client && ready) return client;
68
81
  }
69
82
 
83
+ // Start init — no other caller is initializing at this point
70
84
  initPromise = init();
71
85
  try {
72
86
  await initPromise;
@@ -102,17 +116,34 @@ async function init(): Promise<void> {
102
116
  // 2. Spawn a new server — port 0 lets opencode pick an OS-assigned port
103
117
  const { createOpencode } = await import('@opencode-ai/sdk');
104
118
 
105
- const mcpConfig = getOpenCodeMcpConfig();
119
+ // Build MCP config — but only inject if the MCP endpoint is actually reachable.
120
+ // The opencode binary may initialize MCP connections synchronously before printing
121
+ // "opencode server listening", so an unreachable/slow MCP endpoint can stall startup.
122
+ let mcpConfig = getOpenCodeMcpConfig();
106
123
  if (Object.keys(mcpConfig).length > 0) {
107
- debug.log('engine', `Open Code server: injecting ${Object.keys(mcpConfig).length} MCP server(s)`);
108
- for (const [name, config] of Object.entries(mcpConfig)) {
109
- debug.log('engine', ` → ${name}: ${config.type} (${(config as any).url || (config as any).command?.join(' ')})`);
124
+ const mcpUrls = Object.values(mcpConfig).map(c => (c as any).url).filter(Boolean);
125
+ const reachable = await Promise.all(
126
+ mcpUrls.map(url => isServerAlive(url))
127
+ );
128
+
129
+ if (reachable.every(Boolean)) {
130
+ debug.log('engine', `Open Code server: injecting ${Object.keys(mcpConfig).length} MCP server(s)`);
131
+ for (const [name, config] of Object.entries(mcpConfig)) {
132
+ debug.log('engine', ` → ${name}: ${config.type} (${(config as any).url || (config as any).command?.join(' ')})`);
133
+ }
134
+ } else {
135
+ debug.warn('engine', 'Open Code server: MCP endpoint not reachable, starting without MCP');
136
+ mcpConfig = {};
110
137
  }
111
138
  }
112
139
 
140
+ // No retry — SDK does not kill the child process on timeout, so retrying
141
+ // would leave zombie `opencode serve` processes running in the background.
142
+ // 30s timeout is generous enough for slow devices.
113
143
  const result = await createOpencode({
114
144
  hostname: OPENCODE_HOST,
115
145
  port: 0,
146
+ timeout: SERVER_START_TIMEOUT,
116
147
  ...(Object.keys(mcpConfig).length > 0 && {
117
148
  config: { mcp: mcpConfig },
118
149
  }),
@@ -152,6 +183,7 @@ export async function disposeOpenCodeClient(): Promise<void> {
152
183
  }
153
184
  }
154
185
 
155
- resetState();
186
+ clearClientState();
187
+ initPromise = null;
156
188
  debug.log('engine', 'Open Code client disposed');
157
189
  }
package/bin/clopen.ts CHANGED
@@ -383,15 +383,43 @@ async function startServer(options: CLIOptions) {
383
383
  if (options.host) env.CLOPEN_HOST = options.host;
384
384
  if (options.log) env.CLOPEN_DEBUG = 'true';
385
385
 
386
+ startSpinner('Starting server...');
387
+
386
388
  const serverProc = Bun.spawn(['bun', startScript], {
387
389
  cwd: __dirname,
388
- stdout: 'inherit',
389
- stderr: 'inherit',
390
+ stdout: 'pipe',
391
+ stderr: 'pipe',
390
392
  stdin: 'inherit',
391
393
  env,
392
394
  });
393
395
 
394
- await serverProc.exited;
396
+ // Forward subprocess output, stopping spinner on first chunk
397
+ let spinnerActive = true;
398
+
399
+ async function pipeStream(stream: ReadableStream<Uint8Array>, target: NodeJS.WriteStream) {
400
+ const reader = stream.getReader();
401
+ try {
402
+ while (true) {
403
+ const { done, value } = await reader.read();
404
+ if (done) break;
405
+ if (spinnerActive) {
406
+ stopSpinner();
407
+ spinnerActive = false;
408
+ }
409
+ target.write(value);
410
+ }
411
+ } finally {
412
+ reader.releaseLock();
413
+ }
414
+ }
415
+
416
+ await Promise.all([
417
+ pipeStream(serverProc.stdout, process.stdout),
418
+ pipeStream(serverProc.stderr, process.stderr),
419
+ serverProc.exited,
420
+ ]);
421
+
422
+ if (spinnerActive) stopSpinner();
395
423
  }
396
424
 
397
425
  // ============================================================
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@myrialabs/clopen",
3
- "version": "0.2.19",
3
+ "version": "0.2.20",
4
4
  "description": "All-in-one web workspace for Claude Code & OpenCode — chat, terminal, git, browser preview, checkpoints, and real-time collaboration",
5
5
  "author": "Myria Labs",
6
6
  "license": "MIT",