Skip to content

Commit f65de52

Browse files
rohoswaggerclaude
andcommitted
feat(craft): opencode-serve transport with PodEventBus
Replaces the per-message opencode-acp subprocess with a long-lived opencode-serve per sandbox pod, driven from the api server via an in-process event multiplexer (PodEventBus). Fixes packet drops and dead streams from the prior ACP path. - Migration: add opencode_session_id, agent_provider, agent_model on BuildSession. agent_provider/agent_model become the per-prompt override on opencode's prompt_async — opencode-serve loads config once at startup and does not hot-reload (anomalyco/opencode#22213), so we can't change provider config in flight. - opencode.json multi-provider: every configured LLM provider is pre-loaded into the pod via OPENCODE_CONFIG_CONTENT (per-pod K8s Secret value, surfaced as env var). Without this, opencode falls back to its built-in opencode/big-pickle default and silently ignores the user's selected provider. - entrypoint.sh gates on AGENT_TRANSPORT so the ACP rollback path doesn't share XDG_DATA_HOME with a live opencode-serve (would corrupt the SQLite session store). - PodEventBus: one /event SSE subscription per pod, fanned out to per-session subscriber queues. Tracks parent/child sessions for subagent navigation. Aggressive reconnect with a give-up cap so orphan pods don't leak reader threads. - Subagent infrastructure: list_subagents / subscribe_to_opencode_session on SandboxManager; per-prompt model override threaded end-to-end. Tests: 138 new unit tests covering the translator, bus, send_message, and multi-provider opencode_config, plus a live external-dependency probe against the kind cluster. Co-Authored-By: Claude Opus 4.7 (1M context) <[email protected]>
1 parent 3dd0677 commit f65de52

24 files changed

Lines changed: 6270 additions & 251 deletions
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
"""Add opencode-serve fields to build_session
2+
3+
Revision ID: b02d7b35e48b
4+
Revises: 7f5b159041be
5+
Create Date: 2026-05-22 11:53:07.677442
6+
7+
Adds ``opencode_session_id`` + ``agent_provider`` + ``agent_model``.
8+
See ``docs/craft/opencode-serve-migration.md``.
9+
"""
10+
11+
from alembic import op
12+
import sqlalchemy as sa
13+
14+
15+
revision = "b02d7b35e48b"
16+
down_revision = "7f5b159041be"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade() -> None:
22+
op.add_column(
23+
"build_session",
24+
sa.Column("opencode_session_id", sa.String(), nullable=True),
25+
)
26+
op.add_column(
27+
"build_session",
28+
sa.Column("agent_provider", sa.String(), nullable=True),
29+
)
30+
op.add_column(
31+
"build_session",
32+
sa.Column("agent_model", sa.String(), nullable=True),
33+
)
34+
35+
36+
def downgrade() -> None:
37+
op.drop_column("build_session", "agent_model")
38+
op.drop_column("build_session", "agent_provider")
39+
op.drop_column("build_session", "opencode_session_id")

‎backend/onyx/db/models.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5302,6 +5302,12 @@ class BuildSession(Base):
53025302
default=SessionOrigin.INTERACTIVE,
53035303
server_default="INTERACTIVE",
53045304
)
5305+
# opencode-serve session id (populated lazily on first message
5306+
# under AGENT_TRANSPORT=serve) + user's LLM choice (sent as the
5307+
# per-prompt model override on opencode's prompt_async).
5308+
opencode_session_id: Mapped[str | None] = mapped_column(String, nullable=True)
5309+
agent_provider: Mapped[str | None] = mapped_column(String, nullable=True)
5310+
agent_model: Mapped[str | None] = mapped_column(String, nullable=True)
53055311

53065312
# Relationships
53075313
user: Mapped[User | None] = relationship("User", foreign_keys=[user_id])

‎backend/onyx/server/features/build/configs.py‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,62 @@ def _parse_sandbox_backend(raw: str) -> SandboxBackend:
180180
# This is the maximum time to wait for a complete response from the agent
181181
ACP_MESSAGE_TIMEOUT = float(os.environ.get("ACP_MESSAGE_TIMEOUT", "900.0"))
182182

183+
184+
class AgentTransport(str, Enum):
185+
"""Wire protocol used to drive the in-sandbox agent.
186+
187+
ACP: subprocess-per-message `opencode acp` over JSON-RPC (stdin/stdout).
188+
The historical default. Deprecated — opencode 1.15.7 non-deterministically
189+
drops the per-turn terminator; see docs/craft/opencode-serve-migration.md.
190+
SERVE: long-lived `opencode serve` HTTP server inside the sandbox pod.
191+
Streaming via /event SSE, prompts via POST /session/.../prompt_async.
192+
Target post-Phase 5.
193+
"""
194+
195+
ACP = "acp"
196+
SERVE = "serve"
197+
198+
199+
# Transport for driving the in-sandbox agent. Default ACP until serve has soaked.
200+
# The OpencodeServeClient path is gated on this; setting "serve" routes
201+
# SandboxManager.send_message through HTTP instead of the per-message exec'd
202+
# `opencode acp` subprocess. See docs/craft/opencode-serve-migration.md.
203+
AGENT_TRANSPORT = AgentTransport(
204+
os.environ.get("AGENT_TRANSPORT", AgentTransport.ACP.value)
205+
)
206+
207+
# Port `opencode serve` listens on inside the sandbox container.
208+
# Match against the EXPOSE directive in the sandbox Dockerfile.
209+
OPENCODE_SERVE_PORT = int(os.environ.get("OPENCODE_SERVE_PORT", "4096"))
210+
211+
# Name of the env var inside the sandbox container that holds the
212+
# per-pod HTTP Basic Auth password for opencode serve. The sandbox manager
213+
# is responsible for generating + provisioning the per-pod K8s Secret
214+
# (see docs/craft/opencode-serve-migration.md §Pod / image changes) and
215+
# mounting it under this name in the sandbox container's env.
216+
OPENCODE_SERVER_PASSWORD_ENV = os.environ.get(
217+
"OPENCODE_SERVER_PASSWORD_ENV", "OPENCODE_SERVER_PASSWORD"
218+
)
219+
220+
# Username for HTTP Basic Auth against opencode serve. Opencode's serve
221+
# implementation hard-codes the username to "opencode" when only
222+
# OPENCODE_SERVER_PASSWORD is set; using any other value yields a 401
223+
# (verified empirically against opencode 1.15.7, see test report).
224+
OPENCODE_SERVER_USERNAME = "opencode"
225+
226+
# Per-request HTTP timeouts when talking to opencode serve, in seconds.
227+
OPENCODE_SERVE_CONNECT_TIMEOUT = float(
228+
os.environ.get("OPENCODE_SERVE_CONNECT_TIMEOUT", "5.0")
229+
)
230+
OPENCODE_SERVE_REQUEST_TIMEOUT = float(
231+
os.environ.get("OPENCODE_SERVE_REQUEST_TIMEOUT", "30.0")
232+
)
233+
# Idle timeout for /event SSE. The reader reconnects (with backoff) if the
234+
# stream is silent for this long.
235+
OPENCODE_SERVE_EVENT_READ_TIMEOUT = float(
236+
os.environ.get("OPENCODE_SERVE_EVENT_READ_TIMEOUT", "60.0")
237+
)
238+
183239
# ============================================================================
184240
# Rate Limiting Configuration
185241
# ============================================================================

‎backend/onyx/server/features/build/db/build_session.py‎

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,21 @@ def create_build_session__no_commit(
3434
db_session: Session,
3535
name: str | None = None,
3636
origin: SessionOrigin = SessionOrigin.INTERACTIVE,
37+
agent_provider: str | None = None,
38+
agent_model: str | None = None,
3739
) -> BuildSession:
38-
"""Create a new build session for the given user.
40+
"""``flush()`` only — caller commits.
3941
40-
NOTE: This function uses flush() instead of commit(). The caller is
41-
responsible for committing the transaction when ready.
42-
43-
Args:
44-
user_id: The user ID
45-
db_session: Database session
46-
name: Optional session name
47-
origin: How the session was started. Defaults to INTERACTIVE
48-
(user-driven via the Craft UI); the scheduled-tasks executor
49-
passes SCHEDULED so the row is filtered out of the sidebar.
42+
``agent_provider`` / ``agent_model`` are nullable for legacy rows;
43+
the send-message path then falls back to opencode's startup default.
5044
"""
5145
session = BuildSession(
5246
user_id=user_id,
5347
name=name,
5448
status=BuildSessionStatus.ACTIVE,
5549
origin=origin,
50+
agent_provider=agent_provider,
51+
agent_model=agent_model,
5652
)
5753
db_session.add(session)
5854
db_session.flush()
@@ -599,3 +595,21 @@ def fetch_llm_provider_by_type_for_build_mode(
599595
if not provider_model:
600596
return None
601597
return LLMProviderView.from_model(provider_model)
598+
599+
600+
def fetch_all_build_mode_llm_providers(
601+
db_session: Session,
602+
) -> list[LLMProviderView]:
603+
"""Every ``build-mode-*`` LLM provider configured for this tenant."""
604+
provider_models = list(
605+
db_session.scalars(
606+
select(LLMProviderModel)
607+
.where(LLMProviderModel.name.like("build-mode-%"))
608+
.options(
609+
selectinload(LLMProviderModel.model_configurations),
610+
selectinload(LLMProviderModel.groups),
611+
selectinload(LLMProviderModel.personas),
612+
)
613+
)
614+
)
615+
return [LLMProviderView.from_model(p) for p in provider_models]

‎backend/onyx/server/features/build/sandbox/base.py‎

Lines changed: 64 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,9 +109,17 @@ def provision(
109109
tenant_id: str,
110110
llm_config: LLMProviderConfig,
111111
onyx_pat: str | None = None,
112+
*,
113+
all_llm_configs: list[LLMProviderConfig] | None = None,
112114
) -> SandboxInfo:
113115
"""Provision a new sandbox for a user.
114116
117+
``all_llm_configs`` (serve transport only): the full set of LLM
118+
providers the user has configured. K8s pre-loads each into
119+
opencode-serve's startup config so per-prompt model overrides
120+
can cross providers without restarting the pod. Defaults to
121+
``[llm_config]`` (single-provider, back-compat).
122+
115123
Creates the sandbox container/directory with:
116124
- sessions/ directory for per-session workspaces
117125
@@ -320,25 +328,68 @@ def send_message(
320328
sandbox_id: UUID,
321329
session_id: UUID,
322330
message: str,
331+
*,
332+
opencode_session_id: str | None = None,
333+
agent_provider: str | None = None,
334+
agent_model: str | None = None,
323335
) -> Generator[ACPEvent, None, None]:
324-
"""Send a message to the CLI agent and stream typed ACP events.
325-
326-
The agent runs in the session-specific workspace:
327-
sessions/$session_id/
336+
"""Stream typed ACP events for one user message.
328337
329-
Args:
330-
sandbox_id: The sandbox ID
331-
session_id: The session ID (determines workspace directory)
332-
message: The message content to send
338+
Serve-only kwargs (ignored by ACP transport):
333339
334-
Yields:
335-
Typed ACP schema event objects
336-
337-
Raises:
338-
RuntimeError: If agent communication fails
340+
- ``opencode_session_id``: persistent opencode-serve session id.
341+
Callers should pass ``BuildSession.opencode_session_id``; if
342+
``None`` the transport calls :meth:`ensure_opencode_session`.
343+
- ``agent_provider`` / ``agent_model``: per-prompt model
344+
override (``body["model"]``). Both must be set; either
345+
``None`` falls back to opencode's loaded default.
339346
"""
340347
...
341348

349+
def ensure_opencode_session(
350+
self,
351+
sandbox_id: UUID, # noqa: ARG002 — used by serve-transport subclasses
352+
session_id: UUID, # noqa: ARG002
353+
) -> str | None:
354+
"""Return a stable opencode-serve session id for this build session.
355+
356+
Used only when ``AGENT_TRANSPORT=serve``. The caller (session
357+
manager) persists the returned id on the ``BuildSession`` row so
358+
subsequent ``send_message`` calls can hit the same opencode
359+
session by id, eliminating the on-disk session/list heuristic
360+
the ACP path uses.
361+
362+
Default implementation returns ``None`` — applies to the ACP
363+
transport, which has no notion of a persistent session id and
364+
doesn't need this preflight.
365+
366+
Implementations on the serve path MUST be idempotent: calling
367+
twice for the same (sandbox, session) returns the same id.
368+
"""
369+
return None
370+
371+
def list_subagents(
372+
self,
373+
sandbox_id: UUID, # noqa: ARG002 — used by serve-transport subclasses
374+
parent_opencode_session_id: str, # noqa: ARG002
375+
) -> list[str]:
376+
"""Child opencode session ids spawned under the parent. Empty
377+
on transports that don't track subagents (e.g. docker/ACP)."""
378+
return []
379+
380+
def subscribe_to_opencode_session(
381+
self,
382+
sandbox_id: UUID, # noqa: ARG002
383+
opencode_session_id: str, # noqa: ARG002
384+
*,
385+
keepalive_seconds: float = 15.0, # noqa: ARG002
386+
) -> Generator["ACPEvent", None, None]:
387+
"""Stream events for an arbitrary opencode session without
388+
sending a prompt. Yields keepalives while idle. Empty on
389+
transports without a shared event bus."""
390+
return
391+
yield # pragma: no cover — make this a generator
392+
342393
@abstractmethod
343394
def list_directory(
344395
self, sandbox_id: UUID, session_id: UUID, path: str

‎backend/onyx/server/features/build/sandbox/docker/docker_sandbox_manager.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,8 @@ def provision(
502502
tenant_id: str,
503503
llm_config: LLMProviderConfig, # noqa: ARG002
504504
onyx_pat: str | None = None,
505+
*,
506+
all_llm_configs: list[LLMProviderConfig] | None = None, # noqa: ARG002 — serve-only
505507
) -> SandboxInfo:
506508
if not onyx_pat:
507509
raise ValueError("onyx_pat is required for Docker sandbox provisioning")
@@ -1038,6 +1040,10 @@ def send_message(
10381040
sandbox_id: UUID,
10391041
session_id: UUID,
10401042
message: str,
1043+
*,
1044+
opencode_session_id: str | None = None, # noqa: ARG002 — serve-only
1045+
agent_provider: str | None = None, # noqa: ARG002 — serve-only
1046+
agent_model: str | None = None, # noqa: ARG002 — serve-only
10411047
) -> Generator[ACPEvent, None, None]:
10421048
container = self._require_container(sandbox_id)
10431049
session_path = f"{SESSIONS_ROOT}/{session_id}"

‎backend/onyx/server/features/build/sandbox/kubernetes/docker/Dockerfile‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,8 @@ RUN chown -R sandbox:sandbox /workspace
101101
USER sandbox
102102
WORKDIR /workspace
103103

104-
# 3000: Next.js dev server, 8081: OpenCode ACP, 8731: push daemon
105-
EXPOSE 3000 8081 8731
104+
# 3000: Next.js dev server, 4096: opencode serve, 8081: reserved
105+
# (legacy ACP port; still declared on the K8s Service), 8731: push daemon
106+
EXPOSE 3000 4096 8081 8731
106107

107108
ENTRYPOINT ["/workspace/entrypoint.sh"]
Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,80 @@
11
#!/bin/bash
2-
set -e
3-
trap 'kill 0 2>/dev/null; exit' SIGTERM SIGINT
4-
sleep infinity &
5-
wait
2+
# Sandbox container supervisor.
3+
#
4+
# Runs `opencode serve` in a restart loop with exponential backoff —
5+
# but ONLY when AGENT_TRANSPORT=serve. The ACP rollback path (the
6+
# documented fallback target) execs `opencode acp` per message; running
7+
# `opencode serve` alongside that path shares XDG_DATA_HOME and can
8+
# corrupt the SQLite session store, recreating exactly the multi-process
9+
# flat-file bug the per-message ACP subprocess model existed to prevent.
10+
#
11+
# When AGENT_TRANSPORT != serve, this script idles (sleep loop) — the
12+
# container stays alive so `kubectl exec`-driven flows still have a
13+
# running pod to attach to.
14+
#
15+
# Environment contract:
16+
# AGENT_TRANSPORT "serve" → run opencode serve; anything else
17+
# (including unset) → idle and let the ACP
18+
# exec path own opencode invocation.
19+
# OPENCODE_SERVER_PASSWORD required when AGENT_TRANSPORT=serve. Mounted
20+
# from a per-pod K8s Secret by the sandbox
21+
# manager. opencode-serve uses HTTP Basic
22+
# auth; an empty value disables auth (a fact
23+
# we log loudly).
24+
# XDG_DATA_HOME set here so opencode's SQLite lands on the
25+
# shared workspace volume and is captured by
26+
# snapshots / survives container restarts
27+
# within the same pod.
28+
#
29+
# Logs go to stdout/stderr so kubectl logs and the sidecar-mirrored log
30+
# paths both pick them up.
31+
32+
set -euo pipefail
33+
34+
OPENCODE_PORT="${OPENCODE_SERVE_PORT:-4096}"
35+
WORKSPACE_DATA_HOME="/workspace/.opencode-data"
36+
TRANSPORT="${AGENT_TRANSPORT:-serve}"
37+
38+
export XDG_DATA_HOME="${XDG_DATA_HOME:-$WORKSPACE_DATA_HOME}"
39+
mkdir -p "$XDG_DATA_HOME"
40+
41+
# Forward SIGTERM/SIGINT to the child so kubectl delete and graceful
42+
# shutdown reach opencode.
43+
child_pid=
44+
trap 'if [ -n "$child_pid" ]; then kill -TERM "$child_pid" 2>/dev/null || true; fi; exit 0' SIGTERM SIGINT
45+
46+
if [ "$TRANSPORT" != "serve" ]; then
47+
echo "[entrypoint] AGENT_TRANSPORT=$TRANSPORT (not 'serve') — idling; ACP exec path owns opencode invocation"
48+
# Block forever (interrupted only by SIGTERM trap). Using ``tail -f /dev/null``
49+
# so the container has a deterministic, low-CPU foreground process.
50+
tail -f /dev/null &
51+
child_pid=$!
52+
wait "$child_pid"
53+
exit 0
54+
fi
55+
56+
if [ -z "${OPENCODE_SERVER_PASSWORD:-}" ]; then
57+
echo "[entrypoint] WARNING: OPENCODE_SERVER_PASSWORD is empty — opencode serve will run without auth"
58+
fi
59+
60+
backoff=1
61+
max_backoff=30
62+
63+
while true; do
64+
echo "[entrypoint] starting opencode serve on 0.0.0.0:$OPENCODE_PORT (XDG_DATA_HOME=$XDG_DATA_HOME)"
65+
set +e
66+
opencode serve --hostname 0.0.0.0 --port "$OPENCODE_PORT" --print-logs &
67+
child_pid=$!
68+
wait "$child_pid"
69+
exit_code=$?
70+
set -e
71+
child_pid=
72+
73+
echo "[entrypoint] opencode serve exited (code=$exit_code); restarting in ${backoff}s"
74+
sleep "$backoff"
75+
# Exponential backoff capped at max_backoff
76+
backoff=$((backoff * 2))
77+
if [ "$backoff" -gt "$max_backoff" ]; then
78+
backoff=$max_backoff
79+
fi
80+
done

0 commit comments

Comments
 (0)