Skip to content

Add host mode support for OpenCode adapter#71

Merged
gricha merged 1 commit intomainfrom
opencode-host-mode
Jan 9, 2026
Merged

Add host mode support for OpenCode adapter#71
gricha merged 1 commit intomainfrom
opencode-host-mode

Conversation

@gricha
Copy link
Copy Markdown
Owner

@gricha gricha commented Jan 9, 2026

Summary

  • Enable OpenCode adapter to run directly on the host machine instead of only inside Docker containers
  • Uses native fetch() for HTTP requests and Bun.spawn() for server management in host mode
  • Preserves existing container logic via execInContainer for non-host mode

Changes

  • Add isHost and hostServerProcess properties to track mode and manage host server lifecycle
  • Add host-mode helpers: startServerHost(), findAvailablePortHost(), isServerRunningHost()
  • Branch createSession(), sendAndStream(), startSSEStream() with if (this.isHost) for native vs container execution
  • Clean up host server process in dispose()

Fixes the "OpenCode adapter does not support host mode" error when connecting through chat to OpenCode on a host machine.

Comment on lines +161 to +165
private async startServerHost(): Promise<number> {
const port = await this.findAvailablePortHost();

if (await this.isServerRunningHost(port)) {
return port;

This comment was marked as outdated.

Comment on lines +186 to +189

this.hostServerProcess.kill();
this.hostServerProcess = null;
throw new Error('Failed to start OpenCode server on host');

This comment was marked as outdated.

Enable OpenCode adapter to run directly on the host machine instead of
only inside Docker containers. Uses native fetch() for HTTP requests
and Bun.spawn() for server management in host mode, while preserving
existing container logic via execInContainer.

- Reuse host server across sessions (module-level state like container mode)
- Properly await process.exited on startup failure to prevent zombies
@gricha gricha force-pushed the opencode-host-mode branch from 7d8ec84 to 4412712 Compare January 9, 2026 20:37
Comment on lines +210 to +217
private async findAvailablePortHost(): Promise<number> {
const server = Bun.serve({
port: 0,
fetch: () => new Response(''),
});
const port = server.port!;
server.stop();
return port;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bug: The findAvailablePortHost function doesn't await server.stop(), creating a race condition where the port may be returned before it's fully released by the OS.
Severity: HIGH

🔍 Detailed Analysis

In findAvailablePortHost, the call to server.stop() is not awaited. Since server.stop() is an asynchronous operation that returns a Promise, the function returns the port number before the temporary server has fully shut down and released the port to the operating system. This creates a race condition. If the subsequent code immediately tries to bind to this port, it may fail with an "address already in use" error, leading to intermittent failures when starting the OpenCode server.

💡 Suggested Fix

The server.stop() call should be awaited to ensure the port is fully released before the function returns. Change server.stop(); to await server.stop(); in the findAvailablePortHost function.

🤖 Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: src/session-manager/adapters/opencode.ts#L210-L217

Potential issue: In `findAvailablePortHost`, the call to `server.stop()` is not awaited.
Since `server.stop()` is an asynchronous operation that returns a Promise, the function
returns the port number before the temporary server has fully shut down and released the
port to the operating system. This creates a race condition. If the subsequent code
immediately tries to bind to this port, it may fail with an "address already in use"
error, leading to intermittent failures when starting the OpenCode server.

Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8418176

@gricha gricha merged commit d566d61 into main Jan 9, 2026
8 checks passed
@gricha gricha deleted the opencode-host-mode branch January 9, 2026 20:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant