Skip to content
Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
e8fb8fa
Add bundled OpenCode status wrapper
austinywang May 26, 2026
971a35a
Merge remote-tracking branch 'origin/main' into issue-1425-add-built-…
austinywang May 26, 2026
80e584f
Fix OpenCode prompt status notifications
austinywang May 26, 2026
0087bf8
Handle OpenCode status edge cases
austinywang May 26, 2026
d55134d
Harden OpenCode event notifications
austinywang May 26, 2026
25a221b
Track OpenCode idle transitions by state
austinywang May 26, 2026
55126ff
Localize OpenCode runtime status hooks
austinywang May 26, 2026
9559053
Fix OpenCode runtime hook session state
austinywang May 26, 2026
667bb4b
merge: resolve conflicts with main
austinywang Jun 6, 2026
0aa984f
fix: normalize project file after merge
austinywang Jun 6, 2026
f9c892d
fix: stabilize opencode runtime lifecycle hooks
austinywang Jun 6, 2026
a76c681
fix: bound opencode lifecycle state
austinywang Jun 6, 2026
753054e
fix: protect opencode prompt lifecycle
austinywang Jun 6, 2026
b93f159
fix: handle opencode run lifecycle
austinywang Jun 6, 2026
2e9d693
fix: resolve opencode prompt status
austinywang Jun 6, 2026
1a8f60b
fix: harden opencode lifecycle followups
austinywang Jun 6, 2026
61b6526
fix: respect opencode primary status fields
austinywang Jun 6, 2026
a8a2411
fix: protect opencode blocking lifecycle states
austinywang Jun 6, 2026
57bf740
fix: keep opencode lifecycle cache bounded
austinywang Jun 6, 2026
ac9a1a5
fix: preserve blocking opencode session updates
austinywang Jun 6, 2026
ac28cf1
Merge remote-tracking branch 'origin/main' into issue-1425-add-built-…
austinywang Jun 14, 2026
4968947
fix: regenerate Swift length budget
austinywang Jun 14, 2026
dda1d7d
Merge remote-tracking branch 'origin/main' into issue-1425-add-built-…
austinywang Jun 14, 2026
048ca9c
fix: address opencode PR review findings
austinywang Jun 14, 2026
1585506
fix: avoid checked-in opencode provider path
austinywang Jun 14, 2026
5e95211
fix: preserve opencode prompt lifecycle on updates
austinywang Jun 14, 2026
0965bdb
fix: keep opencode runtime status on shared guard paths
austinywang Jun 14, 2026
bce0a18
merge: sync with origin/main
austinywang Jun 14, 2026
c03082b
merge: resolve conflicts with main
austinywang Jun 18, 2026
e6fadb4
fix: address opencode hook review feedback
austinywang Jun 18, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions CLI/CMUXCLI+ExecutableResolution.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@ extension CMUXCLI {
return prefix.contains("cmux claude wrapper - injects hooks and session tracking")
}

func isCmuxOpenCodeWrapper(at path: String) -> Bool {
guard let data = FileManager.default.contents(atPath: path) else { return false }
let prefixData = data.prefix(512)
guard let prefix = String(data: prefixData, encoding: .utf8) else { return false }
return prefix.contains("cmux opencode wrapper - installs cmux plugins")
}

func resolveExecutableInSearchPath(
_ name: String,
searchPath: String?,
Expand Down Expand Up @@ -37,6 +44,14 @@ extension CMUXCLI {
resolveExecutableInSearchPath("codex", searchPath: searchPath)
}

func resolveOpenCodeExecutable(searchPath: String?) -> String? {
resolveExecutableInSearchPath(
"opencode",
searchPath: searchPath,
skip: { self.isCmuxOpenCodeWrapper(at: $0) }
)
}

func claudeTeamsHasExplicitTeammateMode(commandArgs: [String]) -> Bool {
commandArgs.contains { arg in
arg == "--teammate-mode" || arg.hasPrefix("--teammate-mode=")
Expand Down
157 changes: 150 additions & 7 deletions CLI/cmux.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17358,10 +17358,6 @@ struct CMUXCLI {

// MARK: - cmux omo (OpenCode + oh-my-openagent)

private func resolveOpenCodeExecutable(searchPath: String?) -> String? {
resolveExecutableInSearchPath("opencode", searchPath: searchPath)
}

private func createOMOShimDirectory() throws -> URL {
// tmux shim: redirects tmux commands to cmux __tmux-compat
// Handle -V locally (no socket needed) since __tmux-compat requires a connection.
Expand Down Expand Up @@ -23518,6 +23514,136 @@ function hookEnvironment(cwd) {
return env;
}

function cmuxExecutable() {
return process.env.CMUX_OPENCODE_CMUX_BIN || "cmux";
}

function cmuxArguments(args) {
if (process.env.CMUX_SOCKET_PATH) {
return ["--socket", process.env.CMUX_SOCKET_PATH, ...args];
}
return args;
}

function runCmux(args, options = {}) {
return spawnSync(cmuxExecutable(), cmuxArguments(args), options);
}

function openCodeProcessPid() {
const raw = process.env.CMUX_OPENCODE_PID;
if (raw && /^\d+$/.test(raw)) return raw;
return String(process.pid);
}

function notifyOpenCode(subtitle, body) {
if (process.env.CMUX_OPENCODE_HOOKS_DISABLED === "1") return;
if (!process.env.CMUX_SURFACE_ID) return;

const args = [
"notify",
"--title",
"OpenCode",
"--subtitle",
subtitle,
"--body",
body,
];
if (process.env.CMUX_WORKSPACE_ID) {
args.push("--workspace", process.env.CMUX_WORKSPACE_ID);
}
if (process.env.CMUX_SURFACE_ID) {
args.push("--surface", process.env.CMUX_SURFACE_ID);
}
try {
runCmux(args, {
encoding: "utf8",
env: hookEnvironment(process.cwd()),
stdio: ["ignore", "ignore", "ignore"],
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
timeout: 2000,
});
} catch (_) {}
}

function promptBody(props, fallback) {
return firstString(
props.message,
props.prompt,
props.question,
props.reason,
props.title,
props.error && props.error.message,
typeof props.error === "string" ? props.error : null,
fallback
) || fallback;
Comment thread
austinywang marked this conversation as resolved.
Resolved
}

Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
const STATUS_DESCRIPTORS = {
running: { value: "Running", icon: "bolt.fill", color: "#4C8DFF" },
retrying: { value: "Retrying", icon: "arrow.triangle.2.circlepath", color: "#FF9500", priority: "100" },
needsInput: { value: "Needs input", icon: "bell.fill", color: "#4C8DFF", priority: "100" },
idle: { value: "Idle", icon: "pause.circle.fill", color: "#8E8E93" },
error: { value: "Error", icon: "exclamationmark.triangle.fill", color: "#FF3B30", priority: "100" },
};

function statusWords(value, depth = 0) {
if (depth > 2 || value == null) return [];
if (typeof value === "string") return [value];
if (typeof value === "number" || typeof value === "boolean") return [String(value)];
if (Array.isArray(value)) return value.flatMap((item) => statusWords(item, depth + 1));
if (typeof value === "object") {
return ["type", "status", "state", "phase", "reason", "message", "error"]
.flatMap((key) => statusWords(value[key], depth + 1));
}
return [];
}

function descriptorForOpenCodeStatus(rawStatus) {
const lower = statusWords(rawStatus).join(" ").toLowerCase();
if (!lower) return null;
if (/(retry|rate.?limit|throttl|backoff)/.test(lower)) return STATUS_DESCRIPTORS.retrying;
if (/(error|fail|failure|exception|crash|panic)/.test(lower)) return STATUS_DESCRIPTORS.error;
if (/(idle|ready|done|complete|completed|stopped)/.test(lower)) return STATUS_DESCRIPTORS.idle;
if (/(run|running|busy|active|work|working|stream|streaming|process|processing|execute|executing)/.test(lower)) {
return STATUS_DESCRIPTORS.running;
}
return null;
}

function setStatus(descriptor) {
if (!descriptor) return;
if (process.env.CMUX_OPENCODE_HOOKS_DISABLED === "1") return;
if (!process.env.CMUX_SURFACE_ID) return;

const args = [
"set-status",
"opencode",
descriptor.value,
"--icon",
descriptor.icon,
"--color",
descriptor.color,
"--pid",
openCodeProcessPid(),
];
if (descriptor.priority) {
args.push("--priority", descriptor.priority);
}
if (process.env.CMUX_WORKSPACE_ID) {
args.push("--workspace", process.env.CMUX_WORKSPACE_ID);
}
if (process.env.CMUX_SURFACE_ID) {
args.push("--panel", process.env.CMUX_SURFACE_ID);
}
try {
runCmux(args, {
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
encoding: "utf8",
env: hookEnvironment(process.cwd()),
stdio: ["ignore", "ignore", "ignore"],
timeout: 2000,
});
} catch (_) {}
}

function sendHook(subcommand, ctx, event, extra = {}) {
if (process.env.CMUX_OPENCODE_HOOKS_DISABLED === "1") return;
if (!process.env.CMUX_SURFACE_ID) return;
Expand All @@ -23533,9 +23659,8 @@ function sendHook(subcommand, ctx, event, extra = {}) {
hook_event_name: event && event.type,
...extra,
};
const cmux = process.env.CMUX_OPENCODE_CMUX_BIN || "cmux";
try {
spawnSync(cmux, ["hooks", "opencode", subcommand], {
runCmux(["hooks", "opencode", subcommand], {
input: JSON.stringify(payload),
encoding: "utf8",
env: hookEnvironment(cwd),
Expand Down Expand Up @@ -23563,13 +23688,30 @@ const CMUXSessionRestore = async (ctx) => {
}
break;
case "session.status":
if (props.status && props.status.type === "idle") {
const descriptor = descriptorForOpenCodeStatus(props.status || props.info?.status || props);
setStatus(descriptor);
if (descriptor === STATUS_DESCRIPTORS.idle) {
sendHook("stop", ctx, event);
} else if (descriptor === STATUS_DESCRIPTORS.error) {
notifyOpenCode("Error", promptBody(props, "OpenCode reported an error"));
}
break;
case "session.idle":
setStatus(STATUS_DESCRIPTORS.idle);
sendHook("stop", ctx, event);
break;
case "permission.asked":
setStatus(STATUS_DESCRIPTORS.needsInput);
notifyOpenCode("Permission", promptBody(props, "OpenCode needs permission"));
break;
case "question.asked":
setStatus(STATUS_DESCRIPTORS.needsInput);
notifyOpenCode("Question", promptBody(props, "OpenCode needs input"));
break;
case "session.error":
setStatus(STATUS_DESCRIPTORS.error);
notifyOpenCode("Error", promptBody(props, "OpenCode reported an error"));
break;
case "session.deleted":
sendHook("session-end", ctx, event);
break;
Expand Down Expand Up @@ -26749,6 +26891,7 @@ export default function cmuxPiSessionExtension(pi: ExtensionAPI) {
case "cursor": envKey = "CMUX_CURSOR_PID"
case "gemini": envKey = "CMUX_GEMINI_PID"
case "antigravity": envKey = "CMUX_ANTIGRAVITY_PID"
case "opencode": envKey = "CMUX_OPENCODE_PID"
case "rovodev": envKey = "CMUX_ROVODEV_PID"
case "hermes-agent": envKey = "CMUX_HERMES_AGENT_PID"
case "copilot": envKey = "CMUX_COPILOT_PID"
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,8 @@ cmux hooks setup --agent opencode
`cmux hooks setup` installs supported agents it can find and prints a summary
for skipped agents. Supported resume integrations include Claude Code, Codex,
Grok, OpenCode, Pi, Amp, Cursor CLI, Gemini, Rovo Dev, Copilot, CodeBuddy,
Factory, and Qoder. Claude Code is handled by the cmux Claude wrapper when Claude
integration is enabled in Settings.
Factory, and Qoder. Claude Code and OpenCode are handled by bundled wrappers
inside cmux terminals.
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

Advanced users and integrations can attach a custom resume command to the
current terminal surface. This is useful for tools with their own durable state,
Expand Down
159 changes: 159 additions & 0 deletions Resources/bin/opencode
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
#!/usr/bin/env bash
# cmux opencode wrapper - installs cmux plugins and captures launch metadata.
#
# When running inside a cmux terminal with a live cmux socket, this wrapper
# installs the bundled OpenCode plugins before launching the real opencode
# binary. Outside cmux, it passes through unchanged.

find_real_opencode() {
local self_dir
self_dir="$(cd "$(dirname "$0")" && pwd)"
local IFS=:
for d in $PATH; do
[[ "$d" == "$self_dir" ]] && continue
local candidate="$d/opencode"
[[ -x "$candidate" ]] || continue
if [[ "$candidate" -ef "$0" ]]; then
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
continue
fi
printf '%s' "$candidate"
return 0
done
return 1
}

resolve_cmux_bin() {
local bundled_cli self_dir
bundled_cli="${CMUX_BUNDLED_CLI_PATH:-}"
if [[ -n "$bundled_cli" && -x "$bundled_cli" ]]; then
printf '%s' "$bundled_cli"
return 0
fi

self_dir="$(cd "$(dirname "$0")" && pwd)"
bundled_cli="$self_dir/cmux"
if [[ -x "$bundled_cli" ]]; then
printf '%s' "$bundled_cli"
return 0
fi

bundled_cli="$(command -v cmux || true)"
if [[ -n "$bundled_cli" ]]; then
printf '%s' "$bundled_cli"
return 0
fi

return 1
}

cmux_socket_available() {
local socket="${CMUX_SOCKET_PATH:-}"
[[ -n "$socket" && -S "$socket" ]] || return 1

local cmux_bin
cmux_bin="$(resolve_cmux_bin)" || return 1
CMUXTERM_CLI_RESPONSE_TIMEOUT_SEC=0.75 \
"$cmux_bin" --socket "$socket" ping >/dev/null 2>&1
}

encode_launch_argv() {
{
printf '%s\0' "$REAL_OPENCODE"
if (( $# > 0 )); then
printf '%s\0' "$@"
fi
} | base64 | tr -d '\n'
}

opencode_passthrough_option_flag() {
case "$1" in
--help|-h|--version|-v)
return 0
;;
esac
return 1
}

opencode_builtin_command_name() {
case "$1" in
completion|acp|mcp|attach|run|debug|providers|auth|agent|upgrade|\
uninstall|serve|web|models|stats|export|import|pr|github|session|\
plugin|plug|db|help|version)
return 0
;;
esac
return 1
}

opencode_command_like_invocation() {
local candidate="$1"
[[ -n "$candidate" ]] || return 1
case "$candidate" in
*[![:alnum:]_-]*)
return 1
;;
esac
opencode_builtin_command_name "$candidate"
}

should_install_opencode_hooks() {
(( $# == 0 )) && return 0

local arg
local skip_next=false
for arg in "$@"; do
if [[ "$skip_next" == true ]]; then
skip_next=false
continue
fi
case "$arg" in
--)
return 0
;;
--log-level|--port|--hostname|--mdns-domain|--cors|--file|-f|\
--model|-m|--session|-s|--prompt|--agent)
skip_next=true
continue
;;
-*)
if opencode_passthrough_option_flag "$arg"; then
return 1
fi
continue
;;
*)
if opencode_command_like_invocation "$arg"; then
return 1
fi
return 0
;;
esac
done

return 0
}

REAL_OPENCODE="$(find_real_opencode)" || { echo "Error: opencode not found in PATH" >&2; exit 127; }

IN_CMUX=0
if [[ -n "${CMUX_SURFACE_ID:-}" ]]; then
IN_CMUX=1
fi

if [[ "$IN_CMUX" == "1" && "${CMUX_OPENCODE_HOOKS_DISABLED:-}" != "1" ]] \
&& cmux_socket_available \
&& should_install_opencode_hooks "$@"; then
CMUX_BIN="$(resolve_cmux_bin || true)"
if [[ -n "$CMUX_BIN" ]]; then
CMUXTERM_CLI_RESPONSE_TIMEOUT_SEC=2 \
"$CMUX_BIN" --socket "$CMUX_SOCKET_PATH" hooks opencode install --yes >/dev/null 2>&1 || true
export CMUX_OPENCODE_CMUX_BIN="$CMUX_BIN"
fi
export CMUX_OPENCODE_PID=$$
export CMUX_AGENT_LAUNCH_KIND="opencode"
export CMUX_AGENT_LAUNCH_EXECUTABLE="$REAL_OPENCODE"
export CMUX_AGENT_LAUNCH_ARGV_B64="$(encode_launch_argv "$@")"
export CMUX_AGENT_LAUNCH_CWD="$PWD"
fi

exec "$REAL_OPENCODE" "$@"
Loading
Loading